{"commit":"0ca031c6d255e0d104161cf37703e54e4c2aca16","subject":"sum-properties: \u00b0","message":"sum-properties: \u00b0\n","repos":"crypto-agda\/crypto-agda","old_file":"sum-properties.agda","new_file":"sum-properties.agda","new_contents":"module sum-properties where\n\nopen import Type\nimport Level as L\nopen import Data.Bool.NP\nopen Data.Bool.NP.Indexed\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\nopen import Function.NP\nopen import sum\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_ ; _\u2257_ ; _\u2257\u2082_)\n\nmodule _ {A : \u2605} (\u03bcA : SumProp A) (f g : A \u2192 \u2115) where\n open \u2261.\u2261-Reasoning\n\n sum-\u2293-\u2238 : sum \u03bcA f \u2261 sum \u03bcA (f \u2293\u00b0 g) + sum \u03bcA (f \u2238\u00b0 g)\n sum-\u2293-\u2238 = sum \u03bcA f \u2261\u27e8 sum-ext \u03bcA (f \u27e8 a\u2261a\u2293b+a\u2238b \u27e9\u00b0 g) \u27e9\n sum \u03bcA ((f \u2293\u00b0 g) +\u00b0 (f \u2238\u00b0 g)) \u2261\u27e8 sum-hom \u03bcA (f \u2293\u00b0 g) (f \u2238\u00b0 g) \u27e9\n sum \u03bcA (f \u2293\u00b0 g) + sum \u03bcA (f \u2238\u00b0 g) \u220e\n\n sum-\u2294-\u2293 : sum \u03bcA f + sum \u03bcA g \u2261 sum \u03bcA (f \u2294\u00b0 g) + sum \u03bcA (f \u2293\u00b0 g)\n sum-\u2294-\u2293 = sum \u03bcA f + sum \u03bcA g \u2261\u27e8 \u2261.sym (sum-hom \u03bcA f g) \u27e9\n sum \u03bcA (f +\u00b0 g) \u2261\u27e8 sum-ext \u03bcA (f \u27e8 a+b\u2261a\u2294b+a\u2293b \u27e9\u00b0 g) \u27e9\n sum \u03bcA (f \u2294\u00b0 g +\u00b0 f \u2293\u00b0 g) \u2261\u27e8 sum-hom \u03bcA (f \u2294\u00b0 g) (f \u2293\u00b0 g) \u27e9\n sum \u03bcA (f \u2294\u00b0 g) + sum \u03bcA (f \u2293\u00b0 g) \u220e\n\n sum-\u2294 : sum \u03bcA (f \u2294\u00b0 g) \u2264 sum \u03bcA f + sum \u03bcA g\n sum-\u2294 = \u2115\u2264.trans (sum-mono \u03bcA (f \u27e8 \u2294\u2264+ \u27e9\u00b0 g)) (\u2115\u2264.reflexive (sum-hom \u03bcA f g))\n\nmodule _M2 {A : \u2605} (\u03bcA : SumProp A) (f g : A \u2192 Bool) where\n count-\u2227-not : count \u03bcA f \u2261 count \u03bcA (f \u2227\u00b0 g) + count \u03bcA (f \u2227\u00b0 not\u00b0 g)\n count-\u2227-not rewrite sum-\u2293-\u2238 \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g)\n | sum-ext \u03bcA (f \u27e8 to\u2115-\u2293 \u27e9\u00b0 g)\n | sum-ext \u03bcA (f \u27e8 to\u2115-\u2238 \u27e9\u00b0 g)\n = \u2261.refl\n\n count-\u2228-\u2227 : count \u03bcA f + count \u03bcA g \u2261 count \u03bcA (f \u2228\u00b0 g) + count \u03bcA (f \u2227\u00b0 g)\n count-\u2228-\u2227 rewrite sum-\u2294-\u2293 \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g)\n | sum-ext \u03bcA (f \u27e8 to\u2115-\u2294 \u27e9\u00b0 g)\n | sum-ext \u03bcA (f \u27e8 to\u2115-\u2293 \u27e9\u00b0 g)\n = \u2261.refl\n\n count-\u2228\u2264+ : count \u03bcA (f \u2228\u00b0 g) \u2264 count \u03bcA f + count \u03bcA g\n count-\u2228\u2264+ = \u2115\u2264.trans (\u2115\u2264.reflexive (sum-ext \u03bcA (\u2261.sym \u2218 (f \u27e8 to\u2115-\u2294 \u27e9\u00b0 g))))\n (sum-\u2294 \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g))\n\nsum-ext\u2082 : \u2200 {A B}{f g : A \u2192 B \u2192 \u2115}(\u03bcA : SumProp A)(\u03bcB : SumProp B) \u2192 f \u2257\u2082 g \u2192 sum \u03bcA (sum \u03bcB \u2218 f) \u2261 sum \u03bcA (sum \u03bcB \u2218 g)\nsum-ext\u2082 \u03bcA \u03bcB f\u2257g = sum-ext \u03bcA (sum-ext \u03bcB \u2218 f\u2257g)\n\nInjective : \u2200 {a b}{A : Set a}{B : Set b}(f : A \u2192 B) \u2192 Set (a L.\u2294 b)\nInjective f = \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y\n\nStableUnderInjection : \u2200 {A} \u2192 SumProp A \u2192 Set\nStableUnderInjection \u03bc = \u2200 p \u2192 Injective p \u2192 SumStableUnder \u03bc p\n\nCountStableUnderInjection : \u2200 {A} \u2192 SumProp A \u2192 Set\nCountStableUnderInjection \u03bc = \u2200 p \u2192 Injective p \u2192 CountStableUnder \u03bc p\n\nmodule SumFinProperties where\n open import bijection-fin\n open import Data.Fin using (Fin; zero; suc)\n open import Data.Vec.NP renaming (sum to vsum)\n\n sumFin : \u2200 n \u2192 Sum (Fin n)\n sumFin n f = vsum (tabulate f)\n\n sumFin-spec : \u2200 n \u2192 sumFin (suc n) \u2257 sum (\u03bcFinSuc n)\n sumFin-spec zero f = \u2115\u00b0.+-comm (f zero) 0\n sumFin-spec (suc n) f = \u2261.cong (_+_ (f zero)) (sumFin-spec n (f \u2218 suc))\n\n sumFinSUI : \u2200 n f p \u2192 Injective p \u2192 sumFin n f \u2261 sumFin n (f \u2218 p)\n sumFinSUI n f p p-inj = count-perm f p (\u03bb _ _ \u2192 p-inj)\n\n \u03bcFinSUI : \u2200 {n} \u2192 StableUnderInjection (\u03bcFinSuc n)\n \u03bcFinSUI {n} p p-inj f rewrite \u2261.sym (sumFin-spec n f)\n | \u2261.sym (sumFin-spec n (f \u2218 p))\n = sumFinSUI (suc n) f p p-inj\n\n#-StableUnderInjection : \u2200 {A}{\u03bc : SumProp A} \u2192 StableUnderInjection \u03bc \u2192 CountStableUnderInjection \u03bc\n#-StableUnderInjection sui p p-inj f = sui p p-inj (to\u2115 \u2218 f)\n","old_contents":"module sum-properties where\n\nopen import Type\n\nimport Level as L\n\nopen import Data.Bool.NP\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\n\nopen import Function.NP\n\nopen import sum\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_ ; _\u2257_ ; _\u2257\u2082_)\n\nsum-lem : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA f \u2261 sum \u03bcA (\u03bb x \u2192 f x \u2293 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2238 g x)\nsum-lem \u03bcA f g = \u2261.trans (sum-ext \u03bcA f\u2257f\u2293g+f\u2238g) (sum-hom \u03bcA (\u03bb x \u2192 f x \u2293 g x) (\u03bb x \u2192 f x \u2238 g x))\n where\n f\u2257f\u2293g+f\u2238g : f \u2257 (\u03bb x \u2192 f x \u2293 g x + (f x \u2238 g x))\n f\u2257f\u2293g+f\u2238g x = a\u2261a\u2293b+a\u2238b (f x) (g x)\n\nsum-lem\u2082 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA f + sum \u03bcA g \u2261 sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2293 g x)\nsum-lem\u2082 \u03bcA f g =\n sum \u03bcA f + sum \u03bcA g \u2261\u27e8 \u2261.sym (sum-hom \u03bcA f g) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x + g x) \u2261\u27e8 sum-ext \u03bcA (\u03bb x \u2192 lemma (f x) (g x)) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x \u2294 g x + f x \u2293 g x) \u2261\u27e8 sum-hom \u03bcA (\u03bb x \u2192 f x \u2294 g x) (\u03bb x \u2192 f x \u2293 g x) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2293 g x) \u220e\n where\n open \u2261.\u2261-Reasoning\n lemma : \u2200 a b \u2192 a + b \u2261 a \u2294 b + a \u2293 b\n lemma zero b rewrite \u2115\u00b0.+-comm b 0 = \u2261.refl\n lemma (suc a) zero = \u2261.refl\n lemma (suc a) (suc b) rewrite +-assoc-comm a 1 b\n | +-assoc-comm (a \u2294 b) 1 (a \u2293 b) = \u2261.cong (suc \u2218 suc) (lemma a b)\n\nto\u2115-\u2293 : \u2200 a b \u2192 to\u2115 a \u2293 to\u2115 b \u2261 to\u2115 (a \u2227 b)\nto\u2115-\u2293 true true = \u2261.refl\nto\u2115-\u2293 true false = \u2261.refl\nto\u2115-\u2293 false b = \u2261.refl\n\nto\u2115-\u2294 : \u2200 a b \u2192 to\u2115 a \u2294 to\u2115 b \u2261 to\u2115 (a \u2228 b)\nto\u2115-\u2294 true true = \u2261.refl\nto\u2115-\u2294 true false = \u2261.refl\nto\u2115-\u2294 false b = \u2261.refl\n\nto\u2115-\u2238 : \u2200 a b \u2192 to\u2115 a \u2238 to\u2115 b \u2261 to\u2115 (a \u2227 not b)\nto\u2115-\u2238 true true = \u2261.refl\nto\u2115-\u2238 true false = \u2261.refl\nto\u2115-\u2238 false true = \u2261.refl\nto\u2115-\u2238 false false = \u2261.refl\n\ncount-lem : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool)\n \u2192 count \u03bcA f \u2261 count \u03bcA (\u03bb x \u2192 f x \u2227 g x) + count \u03bcA (\u03bb x \u2192 f x \u2227 not (g x))\ncount-lem \u03bcA f g rewrite sum-lem \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g)\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2293 (f x) (g x)) \n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2238 (f x) (g x)) = \u2261.refl\n\ncount-lem\u2082 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool) \u2192 count \u03bcA f + count \u03bcA g \u2261 count \u03bcA (\u03bb x \u2192 f x \u2228 g x) + count \u03bcA (\u03bb x \u2192 f x \u2227 g x)\ncount-lem\u2082 \u03bcA f g rewrite sum-lem\u2082 \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g)\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2294 (f x) (g x))\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2293 (f x) (g x)) = \u2261.refl\n\n\nsum-\u2294 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) \u2264 sum \u03bcA f + sum \u03bcA g\nsum-\u2294 \u03bcA f g = \u2115\u2264.trans\n (sum-mono \u03bcA (\u03bb x \u2192 \u2294\u2264+ (f x) (g x)))\n (\u2115\u2264.reflexive (sum-hom \u03bcA f g))\n where\n \u2294\u2264+ : \u2200 a b \u2192 a \u2294 b \u2264 a + b\n \u2294\u2264+ zero b = \u2115\u2264.refl\n \u2294\u2264+ (suc a) zero = \u2115\u2264.reflexive (\u2261.cong suc (\u2115\u00b0.+-comm 0 a))\n \u2294\u2264+ (suc a) (suc b) = s\u2264s (\u2115\u2264.trans (\u2294\u2264+ a b) (\u2115\u2264.trans (\u2264-step \u2115\u2264.refl) (\u2115\u2264.reflexive (+-assoc-comm 1 a b))))\n\ncount-\u2228 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool) \u2192 count \u03bcA (\u03bb x \u2192 f x \u2228 g x) \u2264 count \u03bcA f + count \u03bcA g\ncount-\u2228 \u03bcA f g = \u2115\u2264.trans (\u2115\u2264.reflexive (sum-ext \u03bcA (\u03bb x \u2192 \u2261.sym (to\u2115-\u2294 (f x) (g x))))) \n (sum-\u2294 \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g))\n\n\nsum-ext\u2082 : \u2200 {A B}{f g : A \u2192 B \u2192 \u2115}(\u03bcA : SumProp A)(\u03bcB : SumProp B) \u2192 f \u2257\u2082 g \u2192 sum \u03bcA (sum \u03bcB \u2218 f) \u2261 sum \u03bcA (sum \u03bcB \u2218 g)\nsum-ext\u2082 \u03bcA \u03bcB f\u2257g = sum-ext \u03bcA (sum-ext \u03bcB \u2218 f\u2257g)\n\nInjective : \u2200 {a b}{A : Set a}{B : Set b}(f : A \u2192 B) \u2192 Set (a L.\u2294 b)\nInjective f = \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y\n\nStableUnderInjection : \u2200 {A} \u2192 SumProp A \u2192 Set\nStableUnderInjection \u03bc = \u2200 p \u2192 Injective p \u2192 SumStableUnder \u03bc p\n\nCountStableUnderInjection : \u2200 {A} \u2192 SumProp A \u2192 Set\nCountStableUnderInjection \u03bc = \u2200 p \u2192 Injective p \u2192 CountStableUnder \u03bc p\n\nmodule _ where\n open import bijection-fin\n open import Data.Fin using (Fin; zero; suc)\n open import Data.Vec.NP renaming (sum to vsum)\n\n sumFin : \u2200 n \u2192 Sum (Fin n)\n sumFin n f = vsum (tabulate f)\n\n sumFin-spec : \u2200 n \u2192 sumFin (suc n) \u2257 sum (\u03bcFinSuc n)\n sumFin-spec zero f = \u2115\u00b0.+-comm (f zero) 0\n sumFin-spec (suc n) f = \u2261.cong (_+_ (f zero)) (sumFin-spec n (f \u2218 suc))\n\n sumFinSUI : \u2200 n f p \u2192 Injective p \u2192 sumFin n f \u2261 sumFin n (f \u2218 p)\n sumFinSUI n f p p-inj = count-perm f p (\u03bb x y \u2192 p-inj)\n\n \u03bcFinSUI : \u2200 {n} \u2192 StableUnderInjection (\u03bcFinSuc n)\n \u03bcFinSUI {n} p p-inj f rewrite \u2261.sym (sumFin-spec n f)\n | \u2261.sym (sumFin-spec n (f \u2218 p))\n = sumFinSUI (suc n) f p p-inj\n\n#-StableUnderInjection : \u2200 {A}{\u03bc : SumProp A} \u2192 StableUnderInjection \u03bc \u2192 CountStableUnderInjection \u03bc\n#-StableUnderInjection sui p p-inj f = sui p p-inj (to\u2115 \u2218 f)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"6bd42bf5ef43bf32db7fb57583c7d7b7b052fce1","subject":"Fix wrong module name: A -> Extension-00","message":"Fix wrong module name: A -> Extension-00\n\nOld-commit-hash: f363f922f62f640837bc441403a2bae4c5aa71af\n","repos":"inc-lc\/ilc-agda","old_file":"nats\/Extension-00.agda","new_file":"nats\/Extension-00.agda","new_contents":"{-\n\nChecklist of stuff to add when adding syntactic constructs\n\n- derive (symbolic derivation; most important!)\n- weaken\n- \u27e6_\u27e7Term\n- weaken-sound\n- validity-of-derive\n- correctness-of-derive\n\n-}\n\nmodule Extension-00 where\n\nopen import Data.Product\nopen import Data.Nat using (\u2115)\nopen import Data.Unit using (\u22a4)\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Level using\n (zero ; Level ; suc)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\npostulate extensionality : Extensionality zero zero\n\ndata Type : Set where\n nats : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n\u27e8\u2205\u27e9 : \u2205 \u2261 \u2205\n\u27e8\u2205\u27e9 = refl\n\n_\u27e8\u2022\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} \u2192 \u03c4\u2081 \u2261 \u03c4\u2082 \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u03c4\u2081 \u2022 \u0393\u2081 \u2261 \u03c4\u2082 \u2022 \u0393\u2082\n_\u27e8\u2022\u27e9_ = cong\u2082 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n -- ad-hoc extensions\n foldNat : \u2200 {\u0393 \u03c4} \u2192 (n : Term \u0393 nats) \u2192\n (f : Term \u0393 (\u03c4 \u21d2 \u03c4)) \u2192 (z : Term \u0393 \u03c4)\n \u2192 Term \u0393 \u03c4\n\n -- Change to nats = replacement Church pairs\n -- 3 -> 5 ::= \u03bbf. f 3 5\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u227c-reflexivity : Reflexive _\u227c_\n\u227c-reflexivity {\u2205} = \u2205\u227c\u2205\n\u227c-reflexivity {\u03c4 \u2022 x} = keep \u03c4 \u2022 \u227c-reflexivity\n\n\u227c-reflexive : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u0393\u2081 \u227c \u0393\u2082\n\u227c-reflexive refl = \u227c-reflexivity\n\n\u227c-transitive : Transitive _\u227c_\n\u227c-transitive \u2205\u227c\u2205 rel1 = rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n keep \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (keep \u03c4 \u2022 rel0) rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n drop \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (drop \u03c4 \u2022 rel0) rel1\n\n\u227c-isPreorder : IsPreorder _\u2261_ _\u227c_\n\u227c-isPreorder = record\n { isEquivalence = isEquivalence\n ; reflexive = \u227c-reflexive\n ; trans = \u227c-transitive\n }\n\n\u227c-preorder : Preorder _ _ _\n\u227c-preorder = record\n { Carrier = Context\n ; _\u2248_ = _\u2261_\n ; isPreorder = \u227c-isPreorder\n }\n\nmodule \u227c-reasoning where\n open import Relation.Binary.PreorderReasoning \u227c-preorder public\n renaming\n ( _\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_\n ; _\u223c\u27e8_\u27e9_ to _\u227c\u27e8_\u27e9_\n ; _\u2248\u27e8\u27e9_ to _\u2261\u27e8\u27e9_\n )\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\n\nweaken subctx (foldNat n f z) = {!!}\n\nrecord Meaning (Syntax : Set) {\u2113 : Level} : Set (suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 nats \u27e7Type = \u2115\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\n\u27e6 foldNat n f z \u27e7Term \u03c1 = {!!}\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = cong\u2082 (\u03bb x y \u2192 x y)\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = \u2261-app (weaken-sound t\u2081 \u03c1) (weaken-sound t\u2082 \u03c1)\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (foldNat n f z) \u03c1 = {!!}\n\n-- Changes to \u2115 are replacement Church pairs. The only arguments\n-- of conern are `fst` and `snd`, so the Church pairs don't have\n-- to be polymorphic.\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type nats = (nats \u21d2 nats \u21d2 nats) \u21d2 nats\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\n-- It is clear that \u27e6\u229d\u27e7 exists on the semantic level:\n-- there exists an Agda value to describe the change between any\n-- two Agda values denoted by terms. If we have (not dependently-\n-- typed) arrays, no term denotes the change between two arrays\n-- of different lengths. Thus no full abstraction.\n\n\u27e6derive\u27e7 : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ninfixl 6 _\u27e6\u2295\u27e7_\ninfixl 6 _\u27e6\u229d\u27e7_\n\n\u27e6fst\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e6snd\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\n\u27e6derive\u27e7 {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 f (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f v\n\u27e6derive\u27e7 {nats} n = \u03bb f \u2192 f n n\n\n_\u27e6\u229d\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 f\u2081 (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f\u2082 v\n_\u27e6\u229d\u27e7_ {nats} m n = \u03bb f \u2192 f n m\n-- m \u27e6\u229d\u27e7 n ::= replace n by m\n\n_\u27e6\u2295\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n_\u27e6\u2295\u27e7_ {nats} n dn = dn \u27e6snd\u27e7\n\n\u27e6fst\u27e7 m n = m\n\u27e6snd\u27e7 m n = n\n\n-- Strong validity!\ndata \u27e6Valid\u0394\u27e7 : {\u03c4 : Type} \u2192 (v : \u27e6 \u03c4 \u27e7) \u2192 (dv : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n -- Following Pierce's case names `T-Var`, `T-Abs`, `T-App`\n -- with Agda's capitalization convention\n -- generalized to the semantic (value) domain\n v-nat : (n : \u2115) \u2192 (dn : (\u2115 \u2192 \u2115 \u2192 \u2115) \u2192 \u2115) \u2192\n n \u2261 dn \u27e6fst\u27e7 \u2192\n \u27e6Valid\u0394\u27e7 n dn\n\n v-fun : {\u03c4\u2081 \u03c4\u2082 : Type} \u2192\n (f : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) \u2192 (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) \u2192\n -- A strong antecedent: f and df map invalid changes to\n -- valid changes! So long as invalid changes exist,\n -- this is NOT satisfied by\n --\n -- f = \u27e6 \u03bb x. x \u27e7 = identity\n -- df = \u27e6 \u03bb x dx. dx \u27e7 = \u27e6snd\u27e7,\n --\n -- negating any hope of \u27e6Valid\u0394\u27e7 \u27e6 t \u27e7 \u27e6 derive t \u27e7.\n (\u2200 {s ds} \u2192 {-Valid\u0394 s ds \u2192-} \u27e6Valid\u0394\u27e7 (f s) (df s ds)) \u2192\n (\u2200 {s ds} \u2192 (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 (f s \u27e6\u2295\u27e7 df s ds)) \u2192\n \u27e6Valid\u0394\u27e7 f df\n\nabsurdity-of-0=1 : 0 \u2261 1 \u2192 (\u2200 {A : Set} \u2192 A)\nabsurdity-of-0=1 ()\n\nit-is-absurd-that : \u27e6Valid\u0394\u27e7 {nats} 0 (\u03bb f \u2192 f 1 1) \u2192 (\u2200 {A : Set} \u2192 A)\nit-is-absurd-that (v-nat .0 .(\u03bb f \u2192 f 1 1) 0=1) = absurdity-of-0=1 0=1\n\n-- If (\u03bb x dx \u2192 dx) is a \u27e6Valid\u0394\u27e7 to (\u03bb x \u2192 x),\n-- then impossible is nothing.\nno-way : \u27e6Valid\u0394\u27e7 {nats \u21d2 nats} (\u03bb x \u2192 x) (\u03bb x dx \u2192 dx) \u2192\n (\u2200 {A : Set} \u2192 A)\nno-way (v-fun .(\u03bb x \u2192 x) .(\u03bb x dx \u2192 dx) validity correctness) =\n it-is-absurd-that (validity {0} {\u03bb f \u2192 f 1 1})\n\n-- Cool lemmas\n\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u229d\u27e7 f \u2261 \u27e6derive\u27e7 f\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {nats} f = refl\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = refl\n\nf\u2295[g\u229df]=g : \u2200 {\u03c4 : Type} (f g : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) \u2261 g\n\nf\u2295\u0394f=f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (\u27e6derive\u27e7 f) \u2261 f\n\nf\u2295[g\u229df]=g {nats} m n = refl\nf\u2295[g\u229df]=g {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = f x} {y = f x} refl\n (cong\u2082 _\u27e6\u229d\u27e7_ (cong g (f\u2295\u0394f=f x)) refl) \u27e9\n f x \u27e6\u2295\u27e7 (g x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (g x) \u27e9\n g x\n \u220e\n ) where open \u2261-Reasoning\n\nf\u2295\u0394f=f {nats} n = refl\nf\u2295\u0394f=f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 \u27e6derive\u27e7 f) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (f (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong (\u03bb hole \u2192 f x \u27e6\u2295\u27e7 (f hole \u27e6\u229d\u27e7 f x)) (f\u2295\u0394f=f x) \u27e9\n f x \u27e6\u2295\u27e7 (f x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (f x) \u27e9\n f x\n \u220e\n ) where open \u2261-Reasoning\n\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {nats} n dn = n \u2261 dn \u27e6fst\u27e7\nvalid-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (s : \u27e6 \u03c4\u2081 \u27e7) (ds : \u27e6 \u0394-Type \u03c4\u2081 \u27e7) (R[s,ds] : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7 -- (valid-\u0394:1)\n (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 f s \u27e6\u2295\u27e7 df s ds -- (valid-\u0394:2)\n\nR[f,g\u229df] : \u2200 {\u03c4} (f g : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (g \u27e6\u229d\u27e7 f)\nR[f,g\u229df] {nats} m n = refl\nR[f,g\u229df] {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb x dx R[x,dx] \u2192\n R[f,g\u229df] {\u03c4\u2082} (f x) (g (x \u27e6\u2295\u27e7 dx)) -- (valid-\u0394:1)\n , -- tuple constructor\n (begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 refl \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7\n (g ((x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 \u27e6derive\u27e7 (x \u27e6\u2295\u27e7 dx)) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 cong (\u03bb hole \u2192 f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g hole \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx)))\n (f\u2295\u0394f=f (x \u27e6\u2295\u27e7 dx)) \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 f\u2295[g\u229df]=g (f (x \u27e6\u2295\u27e7 dx)) (g (x \u27e6\u2295\u27e7 dx)) \u27e9\n g (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (f x) (g (x \u27e6\u2295\u27e7 dx))) \u27e9\n f x \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) x dx\n \u220e)\n where open \u2261-Reasoning\n\nR[f,\u0394f] : \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (\u27e6derive\u27e7 f)\nR[f,\u0394f] f rewrite sym (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f f) = R[f,g\u229df] f f\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393 -- push \u03c4, then push \u0394\u03c4.\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 (keep \u03c4 \u2022 \u0393\u227c\u0394\u0393)\n\n-- Data type to hold proofs that environments are valid\ndata Valid-\u0394env : {\u0393 : Context} (\u03c1 : \u27e6 \u0393 \u27e7) (\u0394\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is its own valid \u0394-environment.\n \u03c1=\u2205 : Valid-\u0394env {\u2205} \u2205 \u2205\n -- Induction case: the change introduced therein should be valid,\n -- and the smaller \u0394-environment should be valid as well.\n \u03c1=v\u2022\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {\u03c1\u2080 : \u27e6 \u0393\u2080 \u27e7} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Valid-\u0394env \u03c1\u2080 d\u03c1\u2080 \u2192\n Valid-\u0394env {\u03c4 \u2022 \u0393\u2080} (v \u2022 \u03c1\u2080) (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- Data type to hold proofs that a \u0394-env is consistent with self\ndata Consistent-\u0394env : {\u0393 : Context} (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is consistent with itself\n d\u03c1=\u2205 : Consistent-\u0394env {\u2205} \u2205\n -- Induction case: the change introduced at top level\n -- should be valid.\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Consistent-\u0394env d\u03c1\u2080 \u2192\n Consistent-\u0394env {\u03c4 \u2022 \u0393\u2080} (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- If a \u0394-environment is valid for some other environment,\n-- then it is also consistent with itself.\n\nvalid-\u0394env-is-consistent :\n \u2200 {\u0393 : Context} {\u03c1 : \u27e6 \u0393 \u27e7} {d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Valid-\u0394env \u03c1 d\u03c1 \u2192 Consistent-\u0394env d\u03c1\n\nvalid-\u0394env-is-consistent \u03c1=\u2205 = d\u03c1=\u2205\nvalid-\u0394env-is-consistent (\u03c1=v\u2022\u03c1\u2080 valid[v,dv] valid[\u03c1\u2080,d\u03c1\u2080]) =\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] (valid-\u0394env-is-consistent valid[\u03c1\u2080,d\u03c1\u2080])\n\n-- finally, update and ignore\n\nupdate : \u2200 {\u0393} \u2192 (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env d\u03c1} \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 {d\u03c1=\u2205} = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 d\u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] consistent[d\u03c1\u2080]} =\n (v \u27e6\u2295\u27e7 dv) \u2022 update d\u03c1 {consistent[d\u03c1\u2080]}\n\n-- Ignorance is bliss (don't have to pass consistency proofs around :D)\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 -- Using a proof to describe computation\n\n-- Naming scheme follows weakenVar\/weaken\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- derive(n) = \u03bbf. f n n\nderive (nat n) = abs (app (app (var this) (nat n)) (nat n))\n\n-- derive(x) = dx\nderive (var x) = var (deriveVar x)\n\n-- derive(\u03bbx. t) = \u03bbx. \u03bbdx. derive(t)\nderive (abs t) = abs (abs (derive t))\n\n-- derive(f s) = derive(f) s derive(s)\nderive (app f s) = app (app (derive f) (weaken \u0393\u227c\u0394\u0393 s)) (derive s)\n\n-- derive (foldNat f n z) =\nderive (foldNat f n z) = {!!}\n\n-- Extensional equivalence for changes\ndata as-\u0394_is_ext-equiv-to_ :\n \u2200 (\u03c4 : Type) \u2192 (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 (dg : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n ext-\u0394 : \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n (\u2200 (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192\n (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)) \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg\n\ninfix 3 as-\u0394_is_ext-equiv-to_\n\n-- Extractor for extensional-equivalence-as-changes:\n-- Given a value of the data type holding the proof,\n-- returns the proof in applicable form.\n--\n-- It would not be necessary if such\n-- proof-holding types were defined as a function in\n-- the first place, say in the manner of `valid-\u0394`.\n--\nextract-\u0394equiv :\n \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg \u2192\n (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192 (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)\n\nextract-\u0394equiv (ext-\u0394 proof-method) = proof-method\n\n-- Distribution lemmas of validity over \u27e6\u229d\u27e7 and \u27e6\u2295\u27e7\n\napplication-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v) \u2261 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\napplication-over-\u2295-and-\u229d f g df v =\n begin\n f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (cong\u2082 _\u27e6\u2295\u27e7_\n (cong f ((sym (f\u2295\u0394f=f v))))\n (cong\u2082 df\n (sym (f\u2295\u0394f=f v))\n (cong \u27e6derive\u27e7 (sym (f\u2295\u0394f=f v)))))\n refl \u27e9\n f (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v)\n \u27e6\u2295\u27e7 df (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v) (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v))\n \u27e6\u229d\u27e7 f v\n \u2261\u27e8 refl \u27e9\n (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e where open \u2261-Reasoning\n\nvalidity-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n valid-\u0394 g (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) \u2192\n valid-\u0394 (g v) (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\nvalidity-over-\u2295-and-\u229d f g df v R[g,f\u2295df]\n rewrite application-over-\u2295-and-\u229d f g df v =\n proj\u2081 (R[g,f\u2295df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))\n\n-- diff-apply\ndf=f\u2295df\u229df :\n \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n valid-\u0394 f df \u2192 as-\u0394 \u03c4 is df ext-equiv-to f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f\n\n-- Case nat: this REFL is more obvious to Agda than to a human.\ndf=f\u2295df\u229df {nats} n dn valid-f-df =\n ext-\u0394 (\u03bb m valid-m-dn valid-rhs \u2192 refl)\n\ndf=f\u2295df\u229df {\u03c4\u2081 \u21d2 \u03c4\u2082} f df R[f,df] = ext-\u0394 (\n \u03bb g R[g,df] R[g,f\u2295df\u229df] \u2192\n extensionality (\u03bb v \u2192\n begin\n g v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n \u2261\u27e8 extract-\u0394equiv\n (df=f\u2295df\u229df\n (f v) (df v (\u27e6derive\u27e7 v))\n (proj\u2081 (R[f,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))))\n (g v)\n (proj\u2081 (R[g,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v)))\n (validity-over-\u2295-and-\u229d f g df v R[g,f\u2295df\u229df]) \u27e9\n g v \u27e6\u2295\u27e7 (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = g v} {y = g v} refl\n (application-over-\u2295-and-\u229d f g df v) \u27e9\n g v \u27e6\u2295\u27e7 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e\n )\n )\n where open \u2261-Reasoning\n\n\n\ncorrectness-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 deriveVar x \u27e7 \u03c1\n ext-equiv-to\n \u27e6 x \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 x \u27e7 (ignore \u03c1)\n\ncorrectness-of-deriveVar {\u03c4 \u2022 \u0393\u2080} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n this = df=f\u2295df\u229df {\u03c4} v dv R[v,dv]\n\ncorrectness-of-deriveVar {\u03c4\u2080 \u2022 \u0393\u2080} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4\u2080} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n (that x) = correctness-of-deriveVar \u03c1 x\n\n\n\ncorrectness-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 derive t \u27e7 \u03c1\n ext-equiv-to\n \u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 t \u27e7 (ignore \u03c1)\n\n-- Mutually recursive lemma: derivatives are valid\nvalidity-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 deriveVar x \u27e7 \u03c1)\n\nvalidity-of-deriveVar {\u03c4 \u2022 \u0393} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n this = R[v,dv]\n\nvalidity-of-deriveVar {\u03c4\u2080 \u2022 \u0393} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n (that x) = validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 {consistency} (var x) =\n validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 (nat n) = refl\n\nvalidity-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs t) = \u03bb v dv R[v,dv] \u2192\n validity-of-derive (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency} t\n , -- tuple constructor\n (begin\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n ((R[f,g\u229df] (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 f\u2295[g\u229df]=g (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u2261\u27e8 cong \u27e6 t \u27e7 (cong\u2082 _\u2022_ (f\u2295\u0394f=f (v \u27e6\u2295\u27e7 dv)) refl) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency}))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 sym (extract-\u0394equiv\n (correctness-of-derive\n ((dv \u2022 v \u2022 \u03c1))\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (R[f,g\u229df] (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u220e)\n where open \u2261-Reasoning\n\nvalidity-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} t\u2081 t\u2082)\n = R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7]\n where\n open \u2261-Reasoning\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 : \u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1) \u2261 v\u2081 v\u2082\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 rewrite (sym v\u2082=old-v\u2082) = refl\n\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 : \u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1 \u2261 dv\u2081 v\u2082 dv\u2082\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = refl\n\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] : valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (dv\u2081 v\u2082 dv\u2082)\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] rewrite \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 = R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n -- What I want to write:\n {-\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] rewrite \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n -}\n\n -- What I have to write:\n\n R : {\u03c4 : Type} \u2192 {v : \u27e6 \u03c4 \u27e7} \u2192 {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 valid-\u0394 v dv\u2081 \u2192 valid-\u0394 v dv\u2082\n\n R {nats} dv\u2081=dv\u2082 refl = cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl\n\n --R {\u03c4\u2081 \u21d2 \u03c4\u2082} dv\u2081=dv\u2082 valid-dv\u2081 rewrite dv\u2081=dv\u2082 = {!valid-dv\u2081!}\n R {\u03c4\u2081 \u21d2 \u03c4\u2082} {v} {dv\u2081} {dv\u2082} dv\u2081=dv\u2082 valid-dv\u2081 =\n \u03bb s ds R[s,ds] \u2192\n R {\u03c4\u2082} {v s} {dv\u2081 s ds} {dv\u2082 s ds}\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl)\n (proj\u2081 (valid-dv\u2081 s ds R[s,ds]))\n ,\n (begin\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2082 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v (s \u27e6\u2295\u27e7 ds)} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) (sym dv\u2081=dv\u2082) refl) refl) \u27e9\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2081 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 sym (proj\u2082 (valid-dv\u2081\n (s \u27e6\u2295\u27e7 ds)\n (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n (R[f,\u0394f] (s \u27e6\u2295\u27e7 ds)))) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds \u27e6\u2295\u27e7 \u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong (v \u27e6\u2295\u27e7 dv\u2081) (f\u2295\u0394f=f (s \u27e6\u2295\u27e7 ds)) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds)\n \u2261\u27e8 proj\u2082 (valid-dv\u2081 s ds R[s,ds]) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2081 s ds\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v s} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2082 s ds\n \u220e) where open \u2261-Reasoning\n\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] = R \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n\nvalidity-of-derive \u03c1 {consistency} (foldNat n f z) = {!!}\n\n\ncorrectness-of-derive \u03c1 (var x) = correctness-of-deriveVar \u03c1 x\n\ncorrectness-of-derive \u03c1 (nat n) = ext-\u0394 (\u03bb _ _ _ \u2192 refl)\n\ncorrectness-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs {.\u03c4\u2081} {.\u03c4\u2082} t) =\n ext-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n extensionality {\u27e6 \u03c4\u2081 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4\u2082 \u27e7} (\u03bb x \u2192\n begin\n f x \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive {\u03c4\u2081 \u2022 \u0393} {\u03c4\u2082}\n (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency}\n t)\n (f x)\n (proj\u2081 (R[f,\u0394t] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x)))\n (proj\u2081 (R[f,t\u2032\u229dt] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x))) \u27e9\n f x\n \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 (update (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency})\n \u27e6\u229d\u27e7 \u27e6 t \u27e7 (x \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u220e\n )) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} {\u0393} t\u2081 t\u2082) =\n ext-\u0394 {\u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n begin\n f \u27e6\u2295\u27e7 \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n \u2261\u27e8 extract-\u0394equiv ext-\u0394[t\u2081t\u2082] f R[f,\u0394t] R[f,t\u2032\u229dt] \u27e9\n f \u27e6\u2295\u27e7\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n \u220e)\n where\n open \u2261-Reasoning\n\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n v\u2081\u2295dv\u2081=v\u2081\u2032 : v\u2081 \u27e6\u2295\u27e7 dv\u2081 \u2261 v\u2081\u2032\n v\u2081\u2295dv\u2081=v\u2081\u2032 =\n begin\n v\u2081 \u27e6\u2295\u27e7 dv\u2081\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2081)\n v\u2081 valid-dv\u2081 (R[f,g\u229df] v\u2081 v\u2081\u2032) \u27e9\n v\u2081 \u27e6\u2295\u27e7 (v\u2081\u2032 \u27e6\u229d\u27e7 v\u2081)\n \u2261\u27e8 f\u2295[g\u229df]=g v\u2081 v\u2081\u2032 \u27e9\n v\u2081\u2032\n \u220e\n\n -- TODO: remove code duplication.\n v\u2082\u2295dv\u2082=v\u2082\u2032 : v\u2082 \u27e6\u2295\u27e7 dv\u2082 \u2261 v\u2082\u2032\n v\u2082\u2295dv\u2082=v\u2082\u2032 rewrite v\u2082=old-v\u2082 =\n begin\n old-v\u2082 \u27e6\u2295\u27e7 dv\u2082\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2082)\n old-v\u2082\n (validity-of-derive \u03c1 {consistency} t\u2082)\n (R[f,g\u229df] old-v\u2082 v\u2082\u2032) \u27e9\n old-v\u2082 \u27e6\u2295\u27e7 (v\u2082\u2032 \u27e6\u229d\u27e7 old-v\u2082)\n \u2261\u27e8 f\u2295[g\u229df]=g old-v\u2082 v\u2082\u2032 \u27e9\n v\u2082\u2032\n \u220e\n where old-v\u2082 = \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] : v\u2081\u2032 v\u2082\u2032 \u2261 (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082)\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] = sym (cong\u2082 (\u03bb f x \u2192 f x) v\u2081\u2295dv\u2081=v\u2081\u2032 v\u2082\u2295dv\u2082=v\u2082\u2032)\n\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 :\n (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082) \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 = proj\u2082 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082 \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n cong\u2082 _\u27e6\u229d\u27e7_\n (trans v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082)\n refl\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n as-\u0394 \u03c4\u2082 is dv\u2081 v\u2082 dv\u2082 ext-equiv-to v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n df=f\u2295df\u229df (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082) R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 : as-\u0394 \u03c4\u2082 is dv\u2081 v\u2082 dv\u2082 ext-equiv-to v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 rewrite v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082\n\n ext-\u0394[t\u2081t\u2082] :\n as-\u0394 \u03c4\u2082 is\n \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n ext-equiv-to\n \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency}) (\u27e6 t\u2082 \u27e7 (update \u03c1 {consistency}))\n \u27e6\u229d\u27e7 \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n ext-\u0394[t\u2081t\u2082] rewrite sym v\u2082=old-v\u2082 = dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082\n\ncorrectness-of-derive {\u0393} {\u03c4}\n \u03c1 {consistency} (foldNat n f z) = {!!}\n\ncorrectness-on-closed-terms : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192\n \u2200 (f : Term \u2205 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192\n \u2200 (s : Term \u2205 \u03c4\u2081) (ds : Term \u2205 (\u0394-Type \u03c4\u2081))\n {R[v,dv] : valid-\u0394 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)} \u2192\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205 \u27e6\u2295\u27e7 \u27e6 ds \u27e7 \u2205)\n \u2261\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)\n\ncorrectness-on-closed-terms {\u03c4\u2081} {\u03c4\u2082} f s ds {R[v,dv]} =\n begin\n h (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (f\u2295[g\u229df]=g h h))\n refl \u27e9\n (h \u27e6\u2295\u27e7 (h \u27e6\u229d\u27e7 h)) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (extract-\u0394equiv\n (correctness-of-derive \u2205 f)\n h\n (validity-of-derive \u2205 {d\u03c1=\u2205} f)\n (R[f,g\u229df] h h)))\n refl \u27e9\n (h \u27e6\u2295\u27e7 \u0394h) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 proj\u2082 (validity-of-derive \u2205 {d\u03c1=\u2205} f v dv R[v,dv]) \u27e9\n h v \u27e6\u2295\u27e7 \u0394h v dv\n \u220e\n where\n open \u2261-Reasoning\n h : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n h = \u27e6 f \u27e7 \u2205\n \u0394h : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n \u0394h = \u27e6 derive f \u27e7 \u2205\n v : \u27e6 \u03c4\u2081 \u27e7\n v = \u27e6 s \u27e7 \u2205\n dv : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv = \u27e6 ds \u27e7 \u2205\n\n","old_contents":"{-\n\nChecklist of stuff to add when adding syntactic constructs\n\n- derive (symbolic derivation; most important!)\n- weaken\n- \u27e6_\u27e7Term\n- weaken-sound\n- validity-of-derive\n- correctness-of-derive\n\n-}\n\nmodule A where\n\nopen import Data.Product\nopen import Data.Nat using (\u2115)\nopen import Data.Unit using (\u22a4)\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Level using\n (zero ; Level ; suc)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\npostulate extensionality : Extensionality zero zero\n\ndata Type : Set where\n nats : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n\u27e8\u2205\u27e9 : \u2205 \u2261 \u2205\n\u27e8\u2205\u27e9 = refl\n\n_\u27e8\u2022\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} \u2192 \u03c4\u2081 \u2261 \u03c4\u2082 \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u03c4\u2081 \u2022 \u0393\u2081 \u2261 \u03c4\u2082 \u2022 \u0393\u2082\n_\u27e8\u2022\u27e9_ = cong\u2082 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n -- ad-hoc extensions\n foldNat : \u2200 {\u0393 \u03c4} \u2192 (n : Term \u0393 nats) \u2192\n (f : Term \u0393 (\u03c4 \u21d2 \u03c4)) \u2192 (z : Term \u0393 \u03c4)\n \u2192 Term \u0393 \u03c4\n\n -- Change to nats = replacement Church pairs\n -- 3 -> 5 ::= \u03bbf. f 3 5\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u227c-reflexivity : Reflexive _\u227c_\n\u227c-reflexivity {\u2205} = \u2205\u227c\u2205\n\u227c-reflexivity {\u03c4 \u2022 x} = keep \u03c4 \u2022 \u227c-reflexivity\n\n\u227c-reflexive : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u0393\u2081 \u227c \u0393\u2082\n\u227c-reflexive refl = \u227c-reflexivity\n\n\u227c-transitive : Transitive _\u227c_\n\u227c-transitive \u2205\u227c\u2205 rel1 = rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n keep \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (keep \u03c4 \u2022 rel0) rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n drop \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (drop \u03c4 \u2022 rel0) rel1\n\n\u227c-isPreorder : IsPreorder _\u2261_ _\u227c_\n\u227c-isPreorder = record\n { isEquivalence = isEquivalence\n ; reflexive = \u227c-reflexive\n ; trans = \u227c-transitive\n }\n\n\u227c-preorder : Preorder _ _ _\n\u227c-preorder = record\n { Carrier = Context\n ; _\u2248_ = _\u2261_\n ; isPreorder = \u227c-isPreorder\n }\n\nmodule \u227c-reasoning where\n open import Relation.Binary.PreorderReasoning \u227c-preorder public\n renaming\n ( _\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_\n ; _\u223c\u27e8_\u27e9_ to _\u227c\u27e8_\u27e9_\n ; _\u2248\u27e8\u27e9_ to _\u2261\u27e8\u27e9_\n )\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\n\nweaken subctx (foldNat n f z) = {!!}\n\nrecord Meaning (Syntax : Set) {\u2113 : Level} : Set (suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 nats \u27e7Type = \u2115\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\n\u27e6 foldNat n f z \u27e7Term \u03c1 = {!!}\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = cong\u2082 (\u03bb x y \u2192 x y)\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = \u2261-app (weaken-sound t\u2081 \u03c1) (weaken-sound t\u2082 \u03c1)\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (foldNat n f z) \u03c1 = {!!}\n\n-- Changes to \u2115 are replacement Church pairs. The only arguments\n-- of conern are `fst` and `snd`, so the Church pairs don't have\n-- to be polymorphic.\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type nats = (nats \u21d2 nats \u21d2 nats) \u21d2 nats\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\n-- It is clear that \u27e6\u229d\u27e7 exists on the semantic level:\n-- there exists an Agda value to describe the change between any\n-- two Agda values denoted by terms. If we have (not dependently-\n-- typed) arrays, no term denotes the change between two arrays\n-- of different lengths. Thus no full abstraction.\n\n\u27e6derive\u27e7 : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ninfixl 6 _\u27e6\u2295\u27e7_\ninfixl 6 _\u27e6\u229d\u27e7_\n\n\u27e6fst\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e6snd\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\n\u27e6derive\u27e7 {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 f (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f v\n\u27e6derive\u27e7 {nats} n = \u03bb f \u2192 f n n\n\n_\u27e6\u229d\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 f\u2081 (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f\u2082 v\n_\u27e6\u229d\u27e7_ {nats} m n = \u03bb f \u2192 f n m\n-- m \u27e6\u229d\u27e7 n ::= replace n by m\n\n_\u27e6\u2295\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n_\u27e6\u2295\u27e7_ {nats} n dn = dn \u27e6snd\u27e7\n\n\u27e6fst\u27e7 m n = m\n\u27e6snd\u27e7 m n = n\n\n-- Strong validity!\ndata \u27e6Valid\u0394\u27e7 : {\u03c4 : Type} \u2192 (v : \u27e6 \u03c4 \u27e7) \u2192 (dv : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n -- Following Pierce's case names `T-Var`, `T-Abs`, `T-App`\n -- with Agda's capitalization convention\n -- generalized to the semantic (value) domain\n v-nat : (n : \u2115) \u2192 (dn : (\u2115 \u2192 \u2115 \u2192 \u2115) \u2192 \u2115) \u2192\n n \u2261 dn \u27e6fst\u27e7 \u2192\n \u27e6Valid\u0394\u27e7 n dn\n\n v-fun : {\u03c4\u2081 \u03c4\u2082 : Type} \u2192\n (f : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) \u2192 (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) \u2192\n -- A strong antecedent: f and df map invalid changes to\n -- valid changes! So long as invalid changes exist,\n -- this is NOT satisfied by\n --\n -- f = \u27e6 \u03bb x. x \u27e7 = identity\n -- df = \u27e6 \u03bb x dx. dx \u27e7 = \u27e6snd\u27e7,\n --\n -- negating any hope of \u27e6Valid\u0394\u27e7 \u27e6 t \u27e7 \u27e6 derive t \u27e7.\n (\u2200 {s ds} \u2192 {-Valid\u0394 s ds \u2192-} \u27e6Valid\u0394\u27e7 (f s) (df s ds)) \u2192\n (\u2200 {s ds} \u2192 (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 (f s \u27e6\u2295\u27e7 df s ds)) \u2192\n \u27e6Valid\u0394\u27e7 f df\n\nabsurdity-of-0=1 : 0 \u2261 1 \u2192 (\u2200 {A : Set} \u2192 A)\nabsurdity-of-0=1 ()\n\nit-is-absurd-that : \u27e6Valid\u0394\u27e7 {nats} 0 (\u03bb f \u2192 f 1 1) \u2192 (\u2200 {A : Set} \u2192 A)\nit-is-absurd-that (v-nat .0 .(\u03bb f \u2192 f 1 1) 0=1) = absurdity-of-0=1 0=1\n\n-- If (\u03bb x dx \u2192 dx) is a \u27e6Valid\u0394\u27e7 to (\u03bb x \u2192 x),\n-- then impossible is nothing.\nno-way : \u27e6Valid\u0394\u27e7 {nats \u21d2 nats} (\u03bb x \u2192 x) (\u03bb x dx \u2192 dx) \u2192\n (\u2200 {A : Set} \u2192 A)\nno-way (v-fun .(\u03bb x \u2192 x) .(\u03bb x dx \u2192 dx) validity correctness) =\n it-is-absurd-that (validity {0} {\u03bb f \u2192 f 1 1})\n\n-- Cool lemmas\n\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u229d\u27e7 f \u2261 \u27e6derive\u27e7 f\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {nats} f = refl\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = refl\n\nf\u2295[g\u229df]=g : \u2200 {\u03c4 : Type} (f g : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) \u2261 g\n\nf\u2295\u0394f=f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (\u27e6derive\u27e7 f) \u2261 f\n\nf\u2295[g\u229df]=g {nats} m n = refl\nf\u2295[g\u229df]=g {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = f x} {y = f x} refl\n (cong\u2082 _\u27e6\u229d\u27e7_ (cong g (f\u2295\u0394f=f x)) refl) \u27e9\n f x \u27e6\u2295\u27e7 (g x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (g x) \u27e9\n g x\n \u220e\n ) where open \u2261-Reasoning\n\nf\u2295\u0394f=f {nats} n = refl\nf\u2295\u0394f=f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 \u27e6derive\u27e7 f) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (f (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong (\u03bb hole \u2192 f x \u27e6\u2295\u27e7 (f hole \u27e6\u229d\u27e7 f x)) (f\u2295\u0394f=f x) \u27e9\n f x \u27e6\u2295\u27e7 (f x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (f x) \u27e9\n f x\n \u220e\n ) where open \u2261-Reasoning\n\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {nats} n dn = n \u2261 dn \u27e6fst\u27e7\nvalid-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (s : \u27e6 \u03c4\u2081 \u27e7) (ds : \u27e6 \u0394-Type \u03c4\u2081 \u27e7) (R[s,ds] : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7 -- (valid-\u0394:1)\n (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 f s \u27e6\u2295\u27e7 df s ds -- (valid-\u0394:2)\n\nR[f,g\u229df] : \u2200 {\u03c4} (f g : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (g \u27e6\u229d\u27e7 f)\nR[f,g\u229df] {nats} m n = refl\nR[f,g\u229df] {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb x dx R[x,dx] \u2192\n R[f,g\u229df] {\u03c4\u2082} (f x) (g (x \u27e6\u2295\u27e7 dx)) -- (valid-\u0394:1)\n , -- tuple constructor\n (begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 refl \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7\n (g ((x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 \u27e6derive\u27e7 (x \u27e6\u2295\u27e7 dx)) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 cong (\u03bb hole \u2192 f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g hole \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx)))\n (f\u2295\u0394f=f (x \u27e6\u2295\u27e7 dx)) \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 f\u2295[g\u229df]=g (f (x \u27e6\u2295\u27e7 dx)) (g (x \u27e6\u2295\u27e7 dx)) \u27e9\n g (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (f x) (g (x \u27e6\u2295\u27e7 dx))) \u27e9\n f x \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) x dx\n \u220e)\n where open \u2261-Reasoning\n\nR[f,\u0394f] : \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (\u27e6derive\u27e7 f)\nR[f,\u0394f] f rewrite sym (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f f) = R[f,g\u229df] f f\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393 -- push \u03c4, then push \u0394\u03c4.\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 (keep \u03c4 \u2022 \u0393\u227c\u0394\u0393)\n\n-- Data type to hold proofs that environments are valid\ndata Valid-\u0394env : {\u0393 : Context} (\u03c1 : \u27e6 \u0393 \u27e7) (\u0394\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is its own valid \u0394-environment.\n \u03c1=\u2205 : Valid-\u0394env {\u2205} \u2205 \u2205\n -- Induction case: the change introduced therein should be valid,\n -- and the smaller \u0394-environment should be valid as well.\n \u03c1=v\u2022\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {\u03c1\u2080 : \u27e6 \u0393\u2080 \u27e7} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Valid-\u0394env \u03c1\u2080 d\u03c1\u2080 \u2192\n Valid-\u0394env {\u03c4 \u2022 \u0393\u2080} (v \u2022 \u03c1\u2080) (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- Data type to hold proofs that a \u0394-env is consistent with self\ndata Consistent-\u0394env : {\u0393 : Context} (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is consistent with itself\n d\u03c1=\u2205 : Consistent-\u0394env {\u2205} \u2205\n -- Induction case: the change introduced at top level\n -- should be valid.\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Consistent-\u0394env d\u03c1\u2080 \u2192\n Consistent-\u0394env {\u03c4 \u2022 \u0393\u2080} (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- If a \u0394-environment is valid for some other environment,\n-- then it is also consistent with itself.\n\nvalid-\u0394env-is-consistent :\n \u2200 {\u0393 : Context} {\u03c1 : \u27e6 \u0393 \u27e7} {d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Valid-\u0394env \u03c1 d\u03c1 \u2192 Consistent-\u0394env d\u03c1\n\nvalid-\u0394env-is-consistent \u03c1=\u2205 = d\u03c1=\u2205\nvalid-\u0394env-is-consistent (\u03c1=v\u2022\u03c1\u2080 valid[v,dv] valid[\u03c1\u2080,d\u03c1\u2080]) =\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] (valid-\u0394env-is-consistent valid[\u03c1\u2080,d\u03c1\u2080])\n\n-- finally, update and ignore\n\nupdate : \u2200 {\u0393} \u2192 (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env d\u03c1} \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 {d\u03c1=\u2205} = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 d\u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] consistent[d\u03c1\u2080]} =\n (v \u27e6\u2295\u27e7 dv) \u2022 update d\u03c1 {consistent[d\u03c1\u2080]}\n\n-- Ignorance is bliss (don't have to pass consistency proofs around :D)\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 -- Using a proof to describe computation\n\n-- Naming scheme follows weakenVar\/weaken\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- derive(n) = \u03bbf. f n n\nderive (nat n) = abs (app (app (var this) (nat n)) (nat n))\n\n-- derive(x) = dx\nderive (var x) = var (deriveVar x)\n\n-- derive(\u03bbx. t) = \u03bbx. \u03bbdx. derive(t)\nderive (abs t) = abs (abs (derive t))\n\n-- derive(f s) = derive(f) s derive(s)\nderive (app f s) = app (app (derive f) (weaken \u0393\u227c\u0394\u0393 s)) (derive s)\n\n-- derive (foldNat f n z) =\nderive (foldNat f n z) = {!!}\n\n-- Extensional equivalence for changes\ndata as-\u0394_is_ext-equiv-to_ :\n \u2200 (\u03c4 : Type) \u2192 (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 (dg : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n ext-\u0394 : \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n (\u2200 (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192\n (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)) \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg\n\ninfix 3 as-\u0394_is_ext-equiv-to_\n\n-- Extractor for extensional-equivalence-as-changes:\n-- Given a value of the data type holding the proof,\n-- returns the proof in applicable form.\n--\n-- It would not be necessary if such\n-- proof-holding types were defined as a function in\n-- the first place, say in the manner of `valid-\u0394`.\n--\nextract-\u0394equiv :\n \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg \u2192\n (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192 (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)\n\nextract-\u0394equiv (ext-\u0394 proof-method) = proof-method\n\n-- Distribution lemmas of validity over \u27e6\u229d\u27e7 and \u27e6\u2295\u27e7\n\napplication-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v) \u2261 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\napplication-over-\u2295-and-\u229d f g df v =\n begin\n f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (cong\u2082 _\u27e6\u2295\u27e7_\n (cong f ((sym (f\u2295\u0394f=f v))))\n (cong\u2082 df\n (sym (f\u2295\u0394f=f v))\n (cong \u27e6derive\u27e7 (sym (f\u2295\u0394f=f v)))))\n refl \u27e9\n f (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v)\n \u27e6\u2295\u27e7 df (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v) (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v))\n \u27e6\u229d\u27e7 f v\n \u2261\u27e8 refl \u27e9\n (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e where open \u2261-Reasoning\n\nvalidity-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n valid-\u0394 g (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) \u2192\n valid-\u0394 (g v) (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\nvalidity-over-\u2295-and-\u229d f g df v R[g,f\u2295df]\n rewrite application-over-\u2295-and-\u229d f g df v =\n proj\u2081 (R[g,f\u2295df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))\n\n-- diff-apply\ndf=f\u2295df\u229df :\n \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n valid-\u0394 f df \u2192 as-\u0394 \u03c4 is df ext-equiv-to f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f\n\n-- Case nat: this REFL is more obvious to Agda than to a human.\ndf=f\u2295df\u229df {nats} n dn valid-f-df =\n ext-\u0394 (\u03bb m valid-m-dn valid-rhs \u2192 refl)\n\ndf=f\u2295df\u229df {\u03c4\u2081 \u21d2 \u03c4\u2082} f df R[f,df] = ext-\u0394 (\n \u03bb g R[g,df] R[g,f\u2295df\u229df] \u2192\n extensionality (\u03bb v \u2192\n begin\n g v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n \u2261\u27e8 extract-\u0394equiv\n (df=f\u2295df\u229df\n (f v) (df v (\u27e6derive\u27e7 v))\n (proj\u2081 (R[f,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))))\n (g v)\n (proj\u2081 (R[g,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v)))\n (validity-over-\u2295-and-\u229d f g df v R[g,f\u2295df\u229df]) \u27e9\n g v \u27e6\u2295\u27e7 (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = g v} {y = g v} refl\n (application-over-\u2295-and-\u229d f g df v) \u27e9\n g v \u27e6\u2295\u27e7 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e\n )\n )\n where open \u2261-Reasoning\n\n\n\ncorrectness-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 deriveVar x \u27e7 \u03c1\n ext-equiv-to\n \u27e6 x \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 x \u27e7 (ignore \u03c1)\n\ncorrectness-of-deriveVar {\u03c4 \u2022 \u0393\u2080} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n this = df=f\u2295df\u229df {\u03c4} v dv R[v,dv]\n\ncorrectness-of-deriveVar {\u03c4\u2080 \u2022 \u0393\u2080} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4\u2080} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n (that x) = correctness-of-deriveVar \u03c1 x\n\n\n\ncorrectness-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 derive t \u27e7 \u03c1\n ext-equiv-to\n \u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 t \u27e7 (ignore \u03c1)\n\n-- Mutually recursive lemma: derivatives are valid\nvalidity-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 deriveVar x \u27e7 \u03c1)\n\nvalidity-of-deriveVar {\u03c4 \u2022 \u0393} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n this = R[v,dv]\n\nvalidity-of-deriveVar {\u03c4\u2080 \u2022 \u0393} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n (that x) = validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 {consistency} (var x) =\n validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 (nat n) = refl\n\nvalidity-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs t) = \u03bb v dv R[v,dv] \u2192\n validity-of-derive (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency} t\n , -- tuple constructor\n (begin\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n ((R[f,g\u229df] (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 f\u2295[g\u229df]=g (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u2261\u27e8 cong \u27e6 t \u27e7 (cong\u2082 _\u2022_ (f\u2295\u0394f=f (v \u27e6\u2295\u27e7 dv)) refl) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency}))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 sym (extract-\u0394equiv\n (correctness-of-derive\n ((dv \u2022 v \u2022 \u03c1))\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (R[f,g\u229df] (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u220e)\n where open \u2261-Reasoning\n\nvalidity-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} t\u2081 t\u2082)\n = R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7]\n where\n open \u2261-Reasoning\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 : \u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1) \u2261 v\u2081 v\u2082\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 rewrite (sym v\u2082=old-v\u2082) = refl\n\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 : \u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1 \u2261 dv\u2081 v\u2082 dv\u2082\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = refl\n\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] : valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (dv\u2081 v\u2082 dv\u2082)\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] rewrite \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 = R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n -- What I want to write:\n {-\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] rewrite \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n -}\n\n -- What I have to write:\n\n R : {\u03c4 : Type} \u2192 {v : \u27e6 \u03c4 \u27e7} \u2192 {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 valid-\u0394 v dv\u2081 \u2192 valid-\u0394 v dv\u2082\n\n R {nats} dv\u2081=dv\u2082 refl = cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl\n\n --R {\u03c4\u2081 \u21d2 \u03c4\u2082} dv\u2081=dv\u2082 valid-dv\u2081 rewrite dv\u2081=dv\u2082 = {!valid-dv\u2081!}\n R {\u03c4\u2081 \u21d2 \u03c4\u2082} {v} {dv\u2081} {dv\u2082} dv\u2081=dv\u2082 valid-dv\u2081 =\n \u03bb s ds R[s,ds] \u2192\n R {\u03c4\u2082} {v s} {dv\u2081 s ds} {dv\u2082 s ds}\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl)\n (proj\u2081 (valid-dv\u2081 s ds R[s,ds]))\n ,\n (begin\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2082 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v (s \u27e6\u2295\u27e7 ds)} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) (sym dv\u2081=dv\u2082) refl) refl) \u27e9\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2081 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 sym (proj\u2082 (valid-dv\u2081\n (s \u27e6\u2295\u27e7 ds)\n (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n (R[f,\u0394f] (s \u27e6\u2295\u27e7 ds)))) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds \u27e6\u2295\u27e7 \u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong (v \u27e6\u2295\u27e7 dv\u2081) (f\u2295\u0394f=f (s \u27e6\u2295\u27e7 ds)) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds)\n \u2261\u27e8 proj\u2082 (valid-dv\u2081 s ds R[s,ds]) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2081 s ds\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v s} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2082 s ds\n \u220e) where open \u2261-Reasoning\n\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] = R \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n\nvalidity-of-derive \u03c1 {consistency} (foldNat n f z) = {!!}\n\n\ncorrectness-of-derive \u03c1 (var x) = correctness-of-deriveVar \u03c1 x\n\ncorrectness-of-derive \u03c1 (nat n) = ext-\u0394 (\u03bb _ _ _ \u2192 refl)\n\ncorrectness-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs {.\u03c4\u2081} {.\u03c4\u2082} t) =\n ext-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n extensionality {\u27e6 \u03c4\u2081 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4\u2082 \u27e7} (\u03bb x \u2192\n begin\n f x \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive {\u03c4\u2081 \u2022 \u0393} {\u03c4\u2082}\n (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency}\n t)\n (f x)\n (proj\u2081 (R[f,\u0394t] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x)))\n (proj\u2081 (R[f,t\u2032\u229dt] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x))) \u27e9\n f x\n \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 (update (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency})\n \u27e6\u229d\u27e7 \u27e6 t \u27e7 (x \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u220e\n )) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} {\u0393} t\u2081 t\u2082) =\n ext-\u0394 {\u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n begin\n f \u27e6\u2295\u27e7 \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n \u2261\u27e8 extract-\u0394equiv ext-\u0394[t\u2081t\u2082] f R[f,\u0394t] R[f,t\u2032\u229dt] \u27e9\n f \u27e6\u2295\u27e7\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n \u220e)\n where\n open \u2261-Reasoning\n\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n v\u2081\u2295dv\u2081=v\u2081\u2032 : v\u2081 \u27e6\u2295\u27e7 dv\u2081 \u2261 v\u2081\u2032\n v\u2081\u2295dv\u2081=v\u2081\u2032 =\n begin\n v\u2081 \u27e6\u2295\u27e7 dv\u2081\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2081)\n v\u2081 valid-dv\u2081 (R[f,g\u229df] v\u2081 v\u2081\u2032) \u27e9\n v\u2081 \u27e6\u2295\u27e7 (v\u2081\u2032 \u27e6\u229d\u27e7 v\u2081)\n \u2261\u27e8 f\u2295[g\u229df]=g v\u2081 v\u2081\u2032 \u27e9\n v\u2081\u2032\n \u220e\n\n -- TODO: remove code duplication.\n v\u2082\u2295dv\u2082=v\u2082\u2032 : v\u2082 \u27e6\u2295\u27e7 dv\u2082 \u2261 v\u2082\u2032\n v\u2082\u2295dv\u2082=v\u2082\u2032 rewrite v\u2082=old-v\u2082 =\n begin\n old-v\u2082 \u27e6\u2295\u27e7 dv\u2082\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2082)\n old-v\u2082\n (validity-of-derive \u03c1 {consistency} t\u2082)\n (R[f,g\u229df] old-v\u2082 v\u2082\u2032) \u27e9\n old-v\u2082 \u27e6\u2295\u27e7 (v\u2082\u2032 \u27e6\u229d\u27e7 old-v\u2082)\n \u2261\u27e8 f\u2295[g\u229df]=g old-v\u2082 v\u2082\u2032 \u27e9\n v\u2082\u2032\n \u220e\n where old-v\u2082 = \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] : v\u2081\u2032 v\u2082\u2032 \u2261 (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082)\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] = sym (cong\u2082 (\u03bb f x \u2192 f x) v\u2081\u2295dv\u2081=v\u2081\u2032 v\u2082\u2295dv\u2082=v\u2082\u2032)\n\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 :\n (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082) \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 = proj\u2082 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082 \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n cong\u2082 _\u27e6\u229d\u27e7_\n (trans v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082)\n refl\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n as-\u0394 \u03c4\u2082 is dv\u2081 v\u2082 dv\u2082 ext-equiv-to v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n df=f\u2295df\u229df (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082) R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 : as-\u0394 \u03c4\u2082 is dv\u2081 v\u2082 dv\u2082 ext-equiv-to v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 rewrite v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082\n\n ext-\u0394[t\u2081t\u2082] :\n as-\u0394 \u03c4\u2082 is\n \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n ext-equiv-to\n \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency}) (\u27e6 t\u2082 \u27e7 (update \u03c1 {consistency}))\n \u27e6\u229d\u27e7 \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n ext-\u0394[t\u2081t\u2082] rewrite sym v\u2082=old-v\u2082 = dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082\n\ncorrectness-of-derive {\u0393} {\u03c4}\n \u03c1 {consistency} (foldNat n f z) = {!!}\n\ncorrectness-on-closed-terms : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192\n \u2200 (f : Term \u2205 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192\n \u2200 (s : Term \u2205 \u03c4\u2081) (ds : Term \u2205 (\u0394-Type \u03c4\u2081))\n {R[v,dv] : valid-\u0394 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)} \u2192\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205 \u27e6\u2295\u27e7 \u27e6 ds \u27e7 \u2205)\n \u2261\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)\n\ncorrectness-on-closed-terms {\u03c4\u2081} {\u03c4\u2082} f s ds {R[v,dv]} =\n begin\n h (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (f\u2295[g\u229df]=g h h))\n refl \u27e9\n (h \u27e6\u2295\u27e7 (h \u27e6\u229d\u27e7 h)) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (extract-\u0394equiv\n (correctness-of-derive \u2205 f)\n h\n (validity-of-derive \u2205 {d\u03c1=\u2205} f)\n (R[f,g\u229df] h h)))\n refl \u27e9\n (h \u27e6\u2295\u27e7 \u0394h) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 proj\u2082 (validity-of-derive \u2205 {d\u03c1=\u2205} f v dv R[v,dv]) \u27e9\n h v \u27e6\u2295\u27e7 \u0394h v dv\n \u220e\n where\n open \u2261-Reasoning\n h : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n h = \u27e6 f \u27e7 \u2205\n \u0394h : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n \u0394h = \u27e6 derive f \u27e7 \u2205\n v : \u27e6 \u03c4\u2081 \u27e7\n v = \u27e6 s \u27e7 \u2205\n dv : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv = \u27e6 ds \u27e7 \u2205\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"749ba243d78e595650cc5d4473375de38b55da9a","subject":"fromBinTree{,-ind}","message":"fromBinTree{,-ind}\n","repos":"crypto-agda\/explore","old_file":"lib\/Explore\/BinTree.agda","new_file":"lib\/Explore\/BinTree.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule Explore.BinTree where\n\nopen import Data.Tree.Binary\n\nopen import Explore.Core\nopen import Explore.Properties\n\nfromBinTree : \u2200 {m} {A} \u2192 BinTree A \u2192 Explore m A\n-- fromBinTree empty = empty-explore\nfromBinTree (leaf x) = point-explore x\nfromBinTree (fork \u2113 r) = merge-explore (fromBinTree \u2113) (fromBinTree r)\n\nfromBinTree-ind : \u2200 {m p A} (t : BinTree A) \u2192 ExploreInd p (fromBinTree {m} t)\n-- fromBinTree-ind empty = empty-explore-ind\nfromBinTree-ind (leaf x) = point-explore-ind x\nfromBinTree-ind (fork \u2113 r) = merge-explore-ind (fromBinTree-ind \u2113)\n (fromBinTree-ind r)\n{-\nfromBinTree : \u2200 {m A} \u2192 BinTree A \u2192 Explore m A\nfromBinTree (leaf x) _ _ f = f x\nfromBinTree (fork \u2113 r) \u03b5 _\u2219_ f = fromBinTree \u2113 \u03b5 _\u2219_ f \u2219 fromBinTree r \u03b5 _\u2219_ f\n\nfromBinTree-ind : \u2200 {m p A} (t : BinTree A) \u2192 ExploreInd p (fromBinTree {m} t)\nfromBinTree-ind (leaf x) P _ P\u2219 Pf = Pf x\nfromBinTree-ind (fork \u2113 r) P P\u03b5 P\u2219 Pf = P\u2219 (fromBinTree-ind \u2113 P P\u03b5 P\u2219 Pf)\n (fromBinTree-ind r P P\u03b5 P\u2219 Pf)\n-}\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule Explore.BinTree where\n\nopen import Data.Tree.Binary\n\nopen import Explore.Core\nopen import Explore.Properties\n\nfromBinTree : \u2200 {m A} \u2192 BinTree A \u2192 Explore m A\nfromBinTree (leaf x) _ _ f = f x\nfromBinTree (fork \u2113 r) \u03b5 _\u2219_ f = fromBinTree \u2113 \u03b5 _\u2219_ f \u2219 fromBinTree r \u03b5 _\u2219_ f\n\nfromBinTree-ind : \u2200 {m p A} (t : BinTree A) \u2192 ExploreInd p (fromBinTree {m} t)\nfromBinTree-ind (leaf x) P _ P\u2219 Pf = Pf x\nfromBinTree-ind (fork \u2113 r) P P\u03b5 P\u2219 Pf = P\u2219 (fromBinTree-ind \u2113 P P\u03b5 P\u2219 Pf)\n (fromBinTree-ind r P P\u03b5 P\u2219 Pf)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"86af75a4ffb1a4d9451d5976769c197b168dd27e","subject":"More sensible fix","message":"More sensible fix\n\nParametric\/Change\/Specification.agda still makes Agda crash.\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Validity.agda","new_file":"Parametric\/Change\/Validity.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e)\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Denotation.Value as Value\nopen import Base.Change.Algebra as CA\n using (ChangeAlgebraFamily)\n\nmodule Parametric.Change.Validity\n {Base : Type.Structure}\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Extension Point: Change algebras for base types\nStructure : Set\u2081\nStructure = ChangeAlgebraFamily zero \u27e6_\u27e7Base\n\nmodule Structure (change-algebra-base : Structure) where\n -- change algebras\n\n open CA public renaming\n ( [] to \u2205\n ; _\u2237_ to _\u2022_\n )\n\n -- We provide: change algebra for every type\n instance\n change-algebra : \u2200 \u03c4 \u2192 ChangeAlgebra zero \u27e6 \u03c4 \u27e7Type\n change-algebra (base \u03b9) = change-algebra\u208d_\u208e {{change-algebra-base}} \u03b9\n change-algebra (\u03c4\u2081 \u21d2 \u03c4\u2082) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}}\n\n change-algebra-family : ChangeAlgebraFamily zero \u27e6_\u27e7Type\n change-algebra-family = family change-algebra\n\n -- function changes\n\n module _ {\u03c4\u2081 \u03c4\u2082 : Type} where\n open FunctionChanges.FunctionChange {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}} public\n renaming\n ( correct to is-valid\n ; apply to call-change\n )\n\n -- We also provide: change environments (aka. environment changes).\n\n open ListChanges \u27e6_\u27e7Type {{change-algebra-family}} public using () renaming\n ( changeAlgebra to environment-changes\n )\n\n after-env : \u2200 {\u0393 : Context} \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} (d\u03c1 : \u0394\u208d_\u208e {{environment-changes}} \u0393 \u03c1) \u2192 \u27e6 \u0393 \u27e7\n after-env {\u0393} = after\u208d_\u208e \u0393\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e)\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Denotation.Value as Value\nopen import Base.Change.Algebra as CA\n using (ChangeAlgebraFamily)\n\nmodule Parametric.Change.Validity\n {Base : Type.Structure}\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Extension Point: Change algebras for base types\nStructure : Set\u2081\nStructure = ChangeAlgebraFamily zero \u27e6_\u27e7Base\n\nmodule Structure (change-algebra-base : Structure) where\n -- change algebras\n\n open CA public renaming\n ( [] to \u2205\n ; _\u2237_ to _\u2022_\n )\n\n -- We provide: change algebra for every type\n instance\n change-algebra : \u2200 \u03c4 \u2192 ChangeAlgebra zero \u27e6 \u03c4 \u27e7Type\n change-algebra (base \u03b9) = change-algebra\u208d_\u208e {{change-algebra-base}} \u03b9\n change-algebra (\u03c4\u2081 \u21d2 \u03c4\u2082) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}}\n\n change-algebra-family : ChangeAlgebraFamily zero \u27e6_\u27e7Type\n change-algebra-family = family change-algebra\n\n -- function changes\n\n module _ {\u03c4\u2081 \u03c4\u2082 : Type} where\n open FunctionChanges.FunctionChange public\n renaming\n ( correct to is-valid\n ; apply to call-change\n )\n\n -- We also provide: change environments (aka. environment changes).\n\n open ListChanges \u27e6_\u27e7Type {{change-algebra-family}} public using () renaming\n ( changeAlgebra to environment-changes\n )\n\n after-env : \u2200 {\u0393 : Context} \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} (d\u03c1 : \u0394\u208d_\u208e {{environment-changes}} \u0393 \u03c1) \u2192 \u27e6 \u0393 \u27e7\n after-env {\u0393} = after\u208d_\u208e \u0393\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"07045b7aa32a14de23ec233029b6fc6134b24685","subject":"Upgrade to Data.Two","message":"Upgrade to Data.Two\n","repos":"crypto-agda\/agda-nplib","old_file":"experiment\/liftVec.agda","new_file":"experiment\/liftVec.agda","new_contents":"open import Function\nopen import Data.Nat using (\u2115 ; suc ; zero)\nopen import Data.Fin using (Fin)\nopen import Data.Vec\nopen import Data.Vec.N-ary.NP\nopen import Relation.Binary.PropositionalEquality\n\nmodule liftVec where\nmodule single (A : Set)(_+_ : A \u2192 A \u2192 A)where\n\n data Tm : Set where\n var : Tm\n cst : A \u2192 Tm\n fun : Tm \u2192 Tm \u2192 Tm\n\n eval : Tm \u2192 A \u2192 A\n eval var x = x\n eval (cst c) x = c\n eval (fun tm tm') x = eval tm x + eval tm' x\n\n veval : {m : \u2115} \u2192 Tm \u2192 Vec A m \u2192 Vec A m\n veval var xs = xs\n veval (cst x) xs = replicate x\n veval (fun tm tm') xs = zipWith _+_ (veval tm xs) (veval tm' xs)\n\n record Fm : Set where\n constructor _=='_\n field\n lhs : Tm\n rhs : Tm\n\n s[_] : Fm \u2192 Set\n s[ lhs ==' rhs ] = (x : A) \u2192 eval lhs x \u2261 eval rhs x\n\n v[_] : Fm \u2192 Set\n v[ lhs ==' rhs ] = {m : \u2115}(xs : Vec A m) \u2192 veval lhs xs \u2261 veval rhs xs\n\n lemma1 : (t : Tm) \u2192 veval t [] \u2261 []\n lemma1 var = refl\n lemma1 (cst x) = refl\n lemma1 (fun t t') rewrite lemma1 t | lemma1 t' = refl\n\n lemma2 : {m : \u2115}(x : A)(xs : Vec A m)(t : Tm) \u2192 veval t (x \u2237 xs) \u2261 eval t x \u2237 veval t xs\n lemma2 _ _ var = refl\n lemma2 _ _ (cst c) = refl\n lemma2 x xs (fun t t') rewrite lemma2 x xs t | lemma2 x xs t' = refl\n\n prf : (t : Fm) \u2192 s[ t ] \u2192 v[ t ]\n prf (lhs ==' rhs) pr [] rewrite lemma1 lhs | lemma1 rhs = refl\n prf (l ==' r) pr (x \u2237 xs) rewrite lemma2 x xs l | lemma2 x xs r = cong\u2082 (_\u2237_) (pr x) (prf (l ==' r) pr xs)\n\nmodule MultiDataType {A : Set} {nrVar : \u2115} where\n data Tm : Set where\n var : Fin nrVar \u2192 Tm\n cst : A \u2192 Tm\n fun : Tm \u2192 Tm \u2192 Tm\n\n infixl 6 _+'_\n _+'_ : Tm \u2192 Tm \u2192 Tm\n _+'_ = fun\n\n infix 4 _\u2261'_\n record TmEq : Set where\n constructor _\u2261'_\n field\n lhs rhs : Tm\n\nmodule multi (A : Set)(_+_ : A \u2192 A \u2192 A)(nrVar : \u2115) where\n open MultiDataType {A} {nrVar} public\n\n sEnv : Set\n sEnv = Vec A nrVar\n\n vEnv : \u2115 \u2192 Set\n vEnv m = Vec (Vec A m) nrVar\n\n Var : Set\n Var = Fin nrVar\n\n _!_ : {X : Set} \u2192 Vec X nrVar \u2192 Var \u2192 X\n E ! v = lookup v E\n\n eval : Tm \u2192 sEnv \u2192 A\n eval (var x) G = G ! x\n eval (cst c) G = c\n eval (fun t t') G = eval t G + eval t' G\n\n veval : {m : \u2115} \u2192 Tm \u2192 vEnv m \u2192 Vec A m\n veval (var x) G = G ! x\n veval (cst c) G = replicate c\n veval (fun t t') G = zipWith _+_ (veval t G) (veval t' G)\n\n lemma1 : {xs : Vec (Vec A 0) nrVar} (t : Tm) \u2192 veval t xs \u2261 []\n lemma1 {xs} (var x) with xs ! x\n ... | [] = refl\n lemma1 (cst x) = refl\n lemma1 {xs} (fun t t') rewrite lemma1 {xs} t | lemma1 {xs} t' = refl\n\n lemVar : {m n : \u2115}(G : Vec (Vec A (suc m)) n)(i : Fin n) \u2192 lookup i G \u2261 lookup i (map head G) \u2237 lookup i (map tail G)\n lemVar [] ()\n lemVar ((x \u2237 G) \u2237 G\u2081) Data.Fin.zero = refl\n lemVar (G \u2237 G') (Data.Fin.suc i) = lemVar G' i\n\n lemma2 : {n : \u2115}(xs : vEnv (suc n))(t : Tm) \u2192 veval t xs \u2261 eval t (map head xs) \u2237 veval t (map tail xs)\n lemma2 G (var x) = lemVar G x\n lemma2 _ (cst x) = refl\n lemma2 G (fun t t') rewrite lemma2 G t | lemma2 G t' = refl\n\n s[_==_] : Tm \u2192 Tm \u2192 Set\n s[ l == r ] = (G : Vec A nrVar) \u2192 eval l G \u2261 eval r G\n\n v[_==_] : Tm \u2192 Tm \u2192 Set\n v[ l == r ] = {m : \u2115}(G : Vec (Vec A m) nrVar) \u2192 veval l G \u2261 veval r G\n\n prf : (l r : Tm) \u2192 s[ l == r ] \u2192 v[ l == r ]\n prf l r pr {zero} G rewrite lemma1 {G} l | lemma1 {G} r = refl\n prf l r pr {suc m} G rewrite lemma2 G l | lemma2 G r\n = cong\u2082 _\u2237_ (pr (replicate head \u229b G)) (prf l r pr (map tail G))\n\nmodule Full (A : Set)(_+_ : A \u2192 A \u2192 A) (m : \u2115) n where\n open multi A _+_ n public\n\n solve : \u2200 (l r : Tm) \u2192 \u2200\u207f n (curry\u207f\u2032 (\u03bb G \u2192 eval l G \u2261 eval r G))\n \u2192 \u2200\u207f n (curry\u207f\u2032 (\u03bb G \u2192 veval {m} l G \u2261 veval r G))\n solve l r x = curry\u207f {n} {A = Vec A m} {B = curry\u207f\u2032 f}\n (\u03bb xs \u2192 subst id (sym (curry-$\u207f\u2032 f xs))\n (prf l r (\u03bb G \u2192 subst id (curry-$\u207f\u2032 g G) (x $\u207f G)) xs))\n where f = \u03bb G \u2192 veval {m} l G \u2261 veval r G\n g = \u03bb G \u2192 eval l G \u2261 eval r G\n\n mkTm : N-ary n Tm TmEq \u2192 TmEq\n mkTm = go (tabulate id) where\n go : {m : \u2115} -> Vec (Fin n) m \u2192 N-ary m Tm TmEq \u2192 TmEq\n go [] f = f\n go (x \u2237 args) f = go args (f (var x))\n\n prove : \u2200 (t : N-ary n Tm TmEq) \u2192\n let\n l = TmEq.lhs (mkTm t)\n r = TmEq.rhs (mkTm t)\n in \u2200\u207f n (curry\u207f\u2032 (\u03bb G \u2192 eval l G \u2261 eval r G))\n \u2192 \u2200\u207f n (curry\u207f\u2032 (\u03bb G \u2192 veval {m} l G \u2261 veval r G))\n prove t x = solve (TmEq.lhs (mkTm t)) (TmEq.rhs (mkTm t)) x\n\nopen import Data.Two\n\nmodule example where\n\n open import Data.Fin\n open import Data.Product using (\u03a3 ; proj\u2081)\n\n module VecBoolXorProps {m} = Full \ud835\udfda _xor_ m\n open MultiDataType\n\n coolTheorem : {m : \u2115} \u2192 (xs ys : Vec \ud835\udfda m) \u2192 zipWith _xor_ xs ys \u2261 zipWith _xor_ ys xs\n coolTheorem = VecBoolXorProps.prove 2 (\u03bb x y \u2192 x +' y \u2261' y +' x) Xor\u00b0.+-comm\n\n coolTheorem2 : {m : \u2115} \u2192 (xs : Vec \ud835\udfda m) \u2192 _\n coolTheorem2 = VecBoolXorProps.prove 1 (\u03bb x \u2192 (x +' x) \u2261' (cst 0\u2082)) (proj\u2081 Xor\u00b0.-\u203finverse)\n\ntest = example.coolTheorem (1\u2082 \u2237 0\u2082 \u2237 []) (0\u2082 \u2237 0\u2082 \u2237 [])\n","old_contents":"open import Function\nopen import Data.Nat using (\u2115 ; suc ; zero)\nopen import Data.Fin using (Fin)\nopen import Data.Vec\nopen import Data.Vec.N-ary.NP\nopen import Relation.Binary.PropositionalEquality\n\nmodule liftVec where\nmodule single (A : Set)(_+_ : A \u2192 A \u2192 A)where\n\n data Tm : Set where\n var : Tm\n cst : A \u2192 Tm\n fun : Tm \u2192 Tm \u2192 Tm\n\n eval : Tm \u2192 A \u2192 A\n eval var x = x\n eval (cst c) x = c\n eval (fun tm tm') x = eval tm x + eval tm' x\n\n veval : {m : \u2115} \u2192 Tm \u2192 Vec A m \u2192 Vec A m\n veval var xs = xs\n veval (cst x) xs = replicate x\n veval (fun tm tm') xs = zipWith _+_ (veval tm xs) (veval tm' xs)\n\n record Fm : Set where\n constructor _=='_\n field\n lhs : Tm\n rhs : Tm\n\n s[_] : Fm \u2192 Set\n s[ lhs ==' rhs ] = (x : A) \u2192 eval lhs x \u2261 eval rhs x\n\n v[_] : Fm \u2192 Set\n v[ lhs ==' rhs ] = {m : \u2115}(xs : Vec A m) \u2192 veval lhs xs \u2261 veval rhs xs\n\n lemma1 : (t : Tm) \u2192 veval t [] \u2261 []\n lemma1 var = refl\n lemma1 (cst x) = refl\n lemma1 (fun t t') rewrite lemma1 t | lemma1 t' = refl\n\n lemma2 : {m : \u2115}(x : A)(xs : Vec A m)(t : Tm) \u2192 veval t (x \u2237 xs) \u2261 eval t x \u2237 veval t xs\n lemma2 _ _ var = refl\n lemma2 _ _ (cst c) = refl\n lemma2 x xs (fun t t') rewrite lemma2 x xs t | lemma2 x xs t' = refl\n\n prf : (t : Fm) \u2192 s[ t ] \u2192 v[ t ]\n prf (lhs ==' rhs) pr [] rewrite lemma1 lhs | lemma1 rhs = refl\n prf (l ==' r) pr (x \u2237 xs) rewrite lemma2 x xs l | lemma2 x xs r = cong\u2082 (_\u2237_) (pr x) (prf (l ==' r) pr xs)\n\nmodule MultiDataType {A : Set} {nrVar : \u2115} where\n data Tm : Set where\n var : Fin nrVar \u2192 Tm\n cst : A \u2192 Tm\n fun : Tm \u2192 Tm \u2192 Tm\n\n infixl 6 _+'_\n _+'_ : Tm \u2192 Tm \u2192 Tm\n _+'_ = fun \n\n infix 4 _\u2261'_\n record TmEq : Set where\n constructor _\u2261'_ \n field \n lhs rhs : Tm\n\nmodule multi (A : Set)(_+_ : A \u2192 A \u2192 A)(nrVar : \u2115) where\n open MultiDataType {A} {nrVar} public\n\n sEnv : Set\n sEnv = Vec A nrVar\n\n vEnv : \u2115 \u2192 Set\n vEnv m = Vec (Vec A m) nrVar\n\n Var : Set\n Var = Fin nrVar\n\n _!_ : {X : Set} \u2192 Vec X nrVar \u2192 Var \u2192 X\n E ! v = lookup v E\n\n eval : Tm \u2192 sEnv \u2192 A\n eval (var x) G = G ! x\n eval (cst c) G = c\n eval (fun t t') G = eval t G + eval t' G\n\n veval : {m : \u2115} \u2192 Tm \u2192 vEnv m \u2192 Vec A m\n veval (var x) G = G ! x\n veval (cst c) G = replicate c\n veval (fun t t') G = zipWith _+_ (veval t G) (veval t' G)\n\n lemma1 : {xs : Vec (Vec A 0) nrVar} (t : Tm) \u2192 veval t xs \u2261 []\n lemma1 {xs} (var x) with xs ! x\n ... | [] = refl\n lemma1 (cst x) = refl\n lemma1 {xs} (fun t t') rewrite lemma1 {xs} t | lemma1 {xs} t' = refl\n\n lemVar : {m n : \u2115}(G : Vec (Vec A (suc m)) n)(i : Fin n) \u2192 lookup i G \u2261 lookup i (map head G) \u2237 lookup i (map tail G)\n lemVar [] ()\n lemVar ((x \u2237 G) \u2237 G\u2081) Data.Fin.zero = refl\n lemVar (G \u2237 G') (Data.Fin.suc i) = lemVar G' i\n\n lemma2 : {n : \u2115}(xs : vEnv (suc n))(t : Tm) \u2192 veval t xs \u2261 eval t (map head xs) \u2237 veval t (map tail xs)\n lemma2 G (var x) = lemVar G x\n lemma2 _ (cst x) = refl\n lemma2 G (fun t t') rewrite lemma2 G t | lemma2 G t' = refl\n\n s[_==_] : Tm \u2192 Tm \u2192 Set\n s[ l == r ] = (G : Vec A nrVar) \u2192 eval l G \u2261 eval r G\n\n v[_==_] : Tm \u2192 Tm \u2192 Set\n v[ l == r ] = {m : \u2115}(G : Vec (Vec A m) nrVar) \u2192 veval l G \u2261 veval r G\n\n prf : (l r : Tm) \u2192 s[ l == r ] \u2192 v[ l == r ]\n prf l r pr {zero} G rewrite lemma1 {G} l | lemma1 {G} r = refl\n prf l r pr {suc m} G rewrite lemma2 G l | lemma2 G r\n = cong\u2082 _\u2237_ (pr (replicate head \u229b G)) (prf l r pr (map tail G))\n\nmodule Full (A : Set)(_+_ : A \u2192 A \u2192 A) (m : \u2115) n where\n open multi A _+_ n public\n\n solve : \u2200 (l r : Tm) \u2192 \u2200\u207f n (curry\u207f\u2032 (\u03bb G \u2192 eval l G \u2261 eval r G))\n \u2192 \u2200\u207f n (curry\u207f\u2032 (\u03bb G \u2192 veval {m} l G \u2261 veval r G))\n solve l r x = curry\u207f {n} {A = Vec A m} {B = curry\u207f\u2032 f}\n (\u03bb xs \u2192 subst id (sym (curry-$\u207f\u2032 f xs))\n (prf l r (\u03bb G \u2192 subst id (curry-$\u207f\u2032 g G) (x $\u207f G)) xs))\n where f = \u03bb G \u2192 veval {m} l G \u2261 veval r G\n g = \u03bb G \u2192 eval l G \u2261 eval r G\n\n mkTm : N-ary n Tm TmEq \u2192 TmEq\n mkTm = go (tabulate id) where\n go : {m : \u2115} -> Vec (Fin n) m \u2192 N-ary m Tm TmEq \u2192 TmEq\n go [] f = f\n go (x \u2237 args) f = go args (f (var x))\n\n prove : \u2200 (t : N-ary n Tm TmEq) \u2192 \n let \n l = TmEq.lhs (mkTm t)\n r = TmEq.rhs (mkTm t)\n in \u2200\u207f n (curry\u207f\u2032 (\u03bb G \u2192 eval l G \u2261 eval r G))\n \u2192 \u2200\u207f n (curry\u207f\u2032 (\u03bb G \u2192 veval {m} l G \u2261 veval r G))\n prove t x = solve (TmEq.lhs (mkTm t)) (TmEq.rhs (mkTm t)) x\n\nopen import Data.Bool\n\nmodule example where\n\n open import Data.Fin\n open import Data.Bool.NP\n open import Data.Product using (\u03a3 ; proj\u2081)\n\n module VecBoolXorProps {m} = Full Bool _xor_ m\n open MultiDataType\n\n coolTheorem : {m : \u2115} \u2192 (xs ys : Vec Bool m) \u2192 zipWith _xor_ xs ys \u2261 zipWith _xor_ ys xs\n coolTheorem = VecBoolXorProps.prove 2 (\u03bb x y \u2192 x +' y \u2261' y +' x) Xor\u00b0.+-comm \n\n coolTheorem2 : {m : \u2115} \u2192 (xs : Vec Bool m) \u2192 _\n coolTheorem2 = VecBoolXorProps.prove 1 (\u03bb x \u2192 (x +' x) \u2261' (cst false)) (proj\u2081 Xor\u00b0.-\u203finverse) \n \ntest = example.coolTheorem (true \u2237 false \u2237 []) (false \u2237 false \u2237 [])","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"56546fb3e9f31a8dfc5dd4dfbf825e8509a62b9c","subject":"Added TODO: Direct proof using of pred-N using N-least-pre-fixed.","message":"Added TODO: Direct proof using of pred-N using N-least-pre-fixed.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/LeastFixedPoints\/N.agda","new_file":"notes\/fixed-points\/LeastFixedPoints\/N.agda","new_contents":"------------------------------------------------------------------------------\n-- From N as the least fixed-point to N using Agda's data constructor\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- We want to represent the total natural numbers data type\n--\n-- data N : D \u2192 Set where\n-- nzero : N zero\n-- nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n--\n-- using the representation of N as the least fixed-point.\n\nmodule LeastFixedPoints.N where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- Basic definitions\n\n-- Pre-fixed point : d is a pre-fixed point of f if f d \u2264 d\n\n-- Post-fixed point : d is a post-fixed point of f if d \u2264 f d\n\n-- Fixed-point : d is a fixed-point of f if f d = d\n\n-- Least pre-fixed point : d is the least pre-fixed point of f if\n-- 1. f d \u2264 d -- d is a pre-fixed point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Least fixed-point : d is the least fixed-point of f if\n-- 1. f d = d -- d is a fixed-point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Thm: If d is the least pre-fixed point of f, then d is the least\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n-- Thm: If d is the greatest post-fixed point of f, then d is the greatest\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n------------------------------------------------------------------------------\n-- Auxiliary definitions and properties\n\nflip : {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\nflip f b a = f a b\n\ncong : (f : D \u2192 D) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2261 f y\ncong f refl = refl\n\n------------------------------------------------------------------------------\n-- N is a least fixed-point of a functor\n\n-- The functor.\nNatF : (D \u2192 Set) \u2192 D \u2192 Set\nNatF A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n\n-- The natural numbers are the least fixed-point of NatF.\npostulate\n N : D \u2192 Set\n\n -- N is a pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the introduction rules.\n N-in : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n\n -- The higher-order version.\n N-in-ho : \u2200 {n} \u2192 NatF N n \u2192 N n\n\n -- N is the least pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the elimination rule of an inductively\n -- defined predicate.\n N-least-pre-fixed :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\n\n -- Higher-order version (incomplete?)\n N-least-pre-fixed-ho :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (NatF A n \u2192 A n) \u2192\n N n \u2192 A n\n\n------------------------------------------------------------------------------\n-- From\/to N-in\/N-in-ho.\n\nN-in\u2081 : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\nN-in\u2081 = N-in-ho\n\nN-in-ho\u2081 : \u2200 {n} \u2192 NatF N n \u2192 N n\nN-in-ho\u2081 = N-in\u2081\n\n------------------------------------------------------------------------------\n-- From\/to N-least-pre-fixed\/N-least-pre-fixed-ho\nN-least-pre-fixed' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\nN-least-pre-fixed' = N-least-pre-fixed-ho\n\nN-least-pre-fixed-ho' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (NatF A n \u2192 A n) \u2192\n N n \u2192 A n\nN-least-pre-fixed-ho' = N-least-pre-fixed\n\n------------------------------------------------------------------------------\n-- The data constructors of N.\nnzero : N zero\nnzero = N-in (inj\u2081 refl)\n\nnsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\nnsucc Nn = N-in (inj\u2082 (_ , refl , Nn))\n\n------------------------------------------------------------------------------\n-- Because N is the least pre-fixed point of NatF (i.e. N-in and\n-- N-ind), we can proof that N is also a post-fixed point of NatF.\n\n-- N is a post-fixed point of NatF.\nN-post-fixed : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n')\nN-post-fixed = N-least-pre-fixed A h\n where\n A : D \u2192 Set\n A m = m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 N m')\n\n h : \u2200 {m} \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 prf) = inj\u2081 prf\n h (inj\u2082 (m' , prf , Am')) = inj\u2082 (m' , prf , helper Am')\n where\n helper : A m' \u2192 N m'\n helper (inj\u2081 prf') = subst N (sym prf') nzero\n helper (inj\u2082 (m'' , prf' , Am'')) = subst N (sym prf') (nsucc Am'')\n\n------------------------------------------------------------------------------\n-- The induction principle for N *with* the hypothesis N n in the\n-- induction step.\nN-ind\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\nN-ind\u2081 A A0 is {n} Nn = N-least-pre-fixed A h Nn\n where\n h : n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n\n h (inj\u2081 prf) = subst A (sym prf) A0\n h (inj\u2082 (n' , prf , An')) = subst A (sym prf) (is helper An')\n where\n helper : N n'\n helper with N-post-fixed Nn\n ... | inj\u2081 n\u22610 = \u22a5-elim (0\u2262S (trans (sym n\u22610) prf))\n ... | inj\u2082 (m' , prf' , Nm') = subst N (succInjective (trans (sym prf') prf)) Nm'\n\n------------------------------------------------------------------------------\n-- The induction principle for N *without* the hypothesis N n in the\n-- induction step.\nN-ind\u2082 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\nN-ind\u2082 A A0 is {n} = N-least-pre-fixed A h\n where\n h : n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n\n h (inj\u2081 prf) = subst A (sym prf) A0\n h (inj\u2082 (n' , prf , An')) = subst A (sym prf) (is An')\n\n------------------------------------------------------------------------------\n-- Example: We will use N-least-pre-fixed as the induction principle on N.\n\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ\u2081 d + e \u2261 succ\u2081 (d + e)\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity n = +-0x n\n\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = N-least-pre-fixed A h Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n h : m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 prf) = subst N (cong (flip _+_ n) (sym prf)) A0\n where\n A0 : A zero\n A0 = subst N (sym (+-leftIdentity n)) Nn\n h (inj\u2082 (m' , m\u2261Sm' , Am')) = subst N (cong (flip _+_ n) (sym m\u2261Sm')) (is Am')\n where\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} ih = subst N (sym (+-Sx i n)) (nsucc ih)\n\n------------------------------------------------------------------------------\n-- Example: Indirect proof using N-least-pre-fixed.\n\n-- TODO (18 December 2013): Direct proof using of pred-N using\n-- N-least-pre-fixed.\n\npred-N\u2081 : \u2200 {n} \u2192 N n \u2192 N (pred\u2081 n)\npred-N\u2081 = N-ind\u2081 A A0 is\n where\n A : D \u2192 Set\n A i = N (pred\u2081 i)\n\n A0 : A zero\n A0 = subst N (sym pred-0) nzero\n\n is : \u2200 {i} \u2192 N i \u2192 A i \u2192 A (succ\u2081 i)\n is {i} Ni ih = subst N (sym (pred-S i)) Ni\n\n------------------------------------------------------------------------------\n-- From\/to N as a least fixed-point to\/from N as data type.\n\nopen import FOTC.Data.Nat.Type renaming\n ( N to N'\n ; nsucc to nsucc'\n ; nzero to nzero'\n )\n\nN'\u2192N : \u2200 {n} \u2192 N' n \u2192 N n\nN'\u2192N nzero' = nzero\nN'\u2192N (nsucc' Nn) = nsucc (N'\u2192N Nn)\n\n-- Using N-ind\u2081.\nN\u2192N' : \u2200 {n} \u2192 N n \u2192 N' n\nN\u2192N' = N-ind\u2081 N' nzero' (\u03bb _ \u2192 nsucc')\n\n-- Using N-ind\u2082.\nN\u2192N'\u2081 : \u2200 {n} \u2192 N n \u2192 N' n\nN\u2192N'\u2081 = N-ind\u2082 N' nzero' nsucc'\n","old_contents":"------------------------------------------------------------------------------\n-- From N as the least fixed-point to N using Agda's data constructor\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- We want to represent the total natural numbers data type\n--\n-- data N : D \u2192 Set where\n-- nzero : N zero\n-- nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n--\n-- using the representation of N as the least fixed-point.\n\nmodule LeastFixedPoints.N where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- Basic definitions\n\n-- Pre-fixed point : d is a pre-fixed point of f if f d \u2264 d\n\n-- Post-fixed point : d is a post-fixed point of f if d \u2264 f d\n\n-- Fixed-point : d is a fixed-point of f if f d = d\n\n-- Least pre-fixed point : d is the least pre-fixed point of f if\n-- 1. f d \u2264 d -- d is a pre-fixed point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Least fixed-point : d is the least fixed-point of f if\n-- 1. f d = d -- d is a fixed-point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Thm: If d is the least pre-fixed point of f, then d is the least\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n-- Thm: If d is the greatest post-fixed point of f, then d is the greatest\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n------------------------------------------------------------------------------\n-- Auxiliary definitions and properties\n\nflip : {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\nflip f b a = f a b\n\ncong : (f : D \u2192 D) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2261 f y\ncong f refl = refl\n\n------------------------------------------------------------------------------\n-- N is a least fixed-point of a functor\n\n-- The functor.\nNatF : (D \u2192 Set) \u2192 D \u2192 Set\nNatF A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n\n-- The natural numbers are the least fixed-point of NatF.\npostulate\n N : D \u2192 Set\n\n -- N is a pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the introduction rules.\n N-in : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n\n -- The higher-order version.\n N-in-ho : \u2200 {n} \u2192 NatF N n \u2192 N n\n\n -- N is the least pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the elimination rule of an inductively\n -- defined predicate.\n N-least-pre-fixed :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\n\n -- Higher-order version (incomplete?)\n N-least-pre-fixed-ho :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (NatF A n \u2192 A n) \u2192\n N n \u2192 A n\n\n------------------------------------------------------------------------------\n-- From\/to N-in\/N-in-ho.\n\nN-in\u2081 : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\nN-in\u2081 = N-in-ho\n\nN-in-ho\u2081 : \u2200 {n} \u2192 NatF N n \u2192 N n\nN-in-ho\u2081 = N-in\u2081\n\n------------------------------------------------------------------------------\n-- From\/to N-least-pre-fixed\/N-least-pre-fixed-ho\nN-least-pre-fixed' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\nN-least-pre-fixed' = N-least-pre-fixed-ho\n\nN-least-pre-fixed-ho' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (NatF A n \u2192 A n) \u2192\n N n \u2192 A n\nN-least-pre-fixed-ho' = N-least-pre-fixed\n\n------------------------------------------------------------------------------\n-- The data constructors of N.\nnzero : N zero\nnzero = N-in (inj\u2081 refl)\n\nnsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\nnsucc Nn = N-in (inj\u2082 (_ , refl , Nn))\n\n------------------------------------------------------------------------------\n-- Because N is the least pre-fixed point of NatF (i.e. N-in and\n-- N-ind), we can proof that N is also a post-fixed point of NatF.\n\n-- N is a post-fixed point of NatF.\nN-post-fixed : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n')\nN-post-fixed = N-least-pre-fixed A h\n where\n A : D \u2192 Set\n A m = m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 N m')\n\n h : \u2200 {m} \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 prf) = inj\u2081 prf\n h (inj\u2082 (m' , prf , Am')) = inj\u2082 (m' , prf , helper Am')\n where\n helper : A m' \u2192 N m'\n helper (inj\u2081 prf') = subst N (sym prf') nzero\n helper (inj\u2082 (m'' , prf' , Am'')) = subst N (sym prf') (nsucc Am'')\n\n------------------------------------------------------------------------------\n-- The induction principle for N *with* the hypothesis N n in the\n-- induction step.\nN-ind\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\nN-ind\u2081 A A0 is {n} Nn = N-least-pre-fixed A h Nn\n where\n h : n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n\n h (inj\u2081 prf) = subst A (sym prf) A0\n h (inj\u2082 (n' , prf , An')) = subst A (sym prf) (is helper An')\n where\n helper : N n'\n helper with N-post-fixed Nn\n ... | inj\u2081 n\u22610 = \u22a5-elim (0\u2262S (trans (sym n\u22610) prf))\n ... | inj\u2082 (m' , prf' , Nm') = subst N (succInjective (trans (sym prf') prf)) Nm'\n\n------------------------------------------------------------------------------\n-- The induction principle for N *without* the hypothesis N n in the\n-- induction step.\nN-ind\u2082 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\nN-ind\u2082 A A0 is {n} = N-least-pre-fixed A h\n where\n h : n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n\n h (inj\u2081 prf) = subst A (sym prf) A0\n h (inj\u2082 (n' , prf , An')) = subst A (sym prf) (is An')\n\n------------------------------------------------------------------------------\n-- Example: We will use N-least-pre-fixed as the induction principle on N.\n\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ\u2081 d + e \u2261 succ\u2081 (d + e)\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity n = +-0x n\n\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = N-least-pre-fixed A h Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n h : m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 prf) = subst N (cong (flip _+_ n) (sym prf)) A0\n where\n A0 : A zero\n A0 = subst N (sym (+-leftIdentity n)) Nn\n h (inj\u2082 (m' , m\u2261Sm' , Am')) = subst N (cong (flip _+_ n) (sym m\u2261Sm')) (is Am')\n where\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} ih = subst N (sym (+-Sx i n)) (nsucc ih)\n\n------------------------------------------------------------------------------\n-- Example: Indirect proof using N-least-pre-fixed.\n\npred-N\u2081 : \u2200 {n} \u2192 N n \u2192 N (pred\u2081 n)\npred-N\u2081 = N-ind\u2081 A A0 is\n where\n A : D \u2192 Set\n A i = N (pred\u2081 i)\n\n A0 : A zero\n A0 = subst N (sym pred-0) nzero\n\n is : \u2200 {i} \u2192 N i \u2192 A i \u2192 A (succ\u2081 i)\n is {i} Ni ih = subst N (sym (pred-S i)) Ni\n\n------------------------------------------------------------------------------\n-- From\/to N as a least fixed-point to\/from N as data type.\n\nopen import FOTC.Data.Nat.Type renaming\n ( N to N'\n ; nsucc to nsucc'\n ; nzero to nzero'\n )\n\nN'\u2192N : \u2200 {n} \u2192 N' n \u2192 N n\nN'\u2192N nzero' = nzero\nN'\u2192N (nsucc' Nn) = nsucc (N'\u2192N Nn)\n\n-- Using N-ind\u2081.\nN\u2192N' : \u2200 {n} \u2192 N n \u2192 N' n\nN\u2192N' = N-ind\u2081 N' nzero' (\u03bb _ \u2192 nsucc')\n\n-- Using N-ind\u2082.\nN\u2192N'\u2081 : \u2200 {n} \u2192 N n \u2192 N' n\nN\u2192N'\u2081 = N-ind\u2082 N' nzero' nsucc'\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"16c3de301de3b361124574abbf79bebae8568e23","subject":"Cosmetic changes","message":"Cosmetic changes\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/SIRelBigStep\/FundLemma.agda","new_file":"Thesis\/SIRelBigStep\/FundLemma.agda","new_contents":"module Thesis.SIRelBigStep.FundLemma where\n\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\n\nopen import Thesis.SIRelBigStep.IlcSILR\n\nrfundamentalV3v : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 \u2200 \u03c11 d\u03c1 \u03c12 (\u03c1\u03c1 : rrel\u03c13 \u0393 \u03c11 d\u03c1 \u03c12 n) \u2192 rrelV3 \u03c4 (\u27e6 x \u27e7Var \u03c11) (D.\u27e6 x \u27e7Var d\u03c1) (\u27e6 x \u27e7Var \u03c12) n\nrfundamentalV3v x n \u03c11 d\u03c1 \u03c12 \u03c1\u03c1 = \u27e6 x \u27e7RelVar3 \u03c1\u03c1\n\nrfundamental3constV : \u2200 {\u03c4} k (c : Const \u03c4) \u2192\n rrelV3 \u03c4 (eval-const c) (deval (derive-const c) \u2205 \u2205) (eval-const c) k\nrfundamental3constV k (lit n) = refl\n\nrfundamental3 : \u2200 {\u03c4 \u0393} k (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c11 d\u03c1 \u03c12 \u2192 (\u03c1\u03c1 : rrel\u03c13 \u0393 \u03c11 d\u03c1 \u03c12 k) \u2192\n rrelT3 t (derive-dterm t) t \u03c11 d\u03c1 \u03c12 k\n\nrfundamental3svv : \u2200 {\u03c4 \u0393} k (sv : SVal \u0393 \u03c4) \u2192\n \u2200 \u03c11 d\u03c1 \u03c12 \u2192 (\u03c1\u03c1 : rrel\u03c13 \u0393 \u03c11 d\u03c1 \u03c12 k) \u2192 rrelV3 \u03c4 (eval sv \u03c11) (deval (derive-dsval sv) \u03c11 d\u03c1) (eval sv \u03c12) k\nrfundamental3svv k (var x) \u03c11 d\u03c1 \u03c12 \u03c1\u03c1 = rfundamentalV3v x k \u03c11 d\u03c1 \u03c12 \u03c1\u03c1\nrfundamental3svv k (cons sv1 sv2) \u03c11 d\u03c1 \u03c12 \u03c1\u03c1 = rfundamental3svv k sv1 \u03c11 d\u03c1 \u03c12 \u03c1\u03c1 , rfundamental3svv k sv2 \u03c11 d\u03c1 \u03c12 \u03c1\u03c1\nrfundamental3svv k (const c) \u03c11 d\u03c1 \u03c12 \u03c1\u03c1 rewrite deval-derive-const-inv c \u03c11 d\u03c1 = rfundamental3constV k c\nrfundamental3svv k (abs t) \u03c11 d\u03c1 \u03c12 \u03c1\u03c1 = (refl , refl) , refl , rrel\u03c13\u2192\u2295 \u03c11 d\u03c1 \u03c12 \u03c1\u03c1 , refl , refl ,\n \u03bb k\u2081 k We remove the separation transformation and consider that\n-- the unexecuted transformations are done on those separated elements.\n\n-- > The input tree contains subtrees that represent exactly the input of the coms it can execute.\n\n-- 2 TWO\n-- We need three structures.\n-- THe first is used to identify inputs from the same com. When a tranformation splits these coms, we also split the set into two sets. Next we track all the inputs that are part of the tranformation, be it sets of inputs of a specific com or individual inputs. From all these sets, at least one item from each set needs to be the input of the next com.\n-- The set of coms that are used to allow for commutation of inputs.\n\nmodule _ where\n\n open import Data.Vec\n\n mutual \n data Descendant {u} : Set (lsuc u) where\n orig : Descendant\n dec : \u2115 \u2192 \u2200{i ll} \u2192 SetLLD {i} {u} ll \u2192 Descendant\n \n \n \n data SetLLD {i : Size} {u} : LinLogic i {u} \u2192 Set (lsuc u) where\n \u2193 : \u2200{ll} \u2192 Descendant {u} \u2192 SetLLD ll\n _\u2190\u2227 : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD (ls \u2227 rs)\n \u2227\u2192_ : \u2200{rs ls} \u2192 SetLLD rs \u2192 SetLLD (ls \u2227 rs)\n _\u2190\u2227\u2192_ : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD rs \u2192 SetLLD (ls \u2227 rs)\n _\u2190\u2228 : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD (ls \u2228 rs)\n \u2228\u2192_ : \u2200{rs ls} \u2192 SetLLD rs \u2192 SetLLD (ls \u2228 rs)\n _\u2190\u2228\u2192_ : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD rs \u2192 SetLLD (ls \u2228 rs)\n _\u2190\u2202 : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD (ls \u2202 rs)\n \u2202\u2192_ : \u2200{rs ls} \u2192 SetLLD rs \u2192 SetLLD (ls \u2202 rs)\n _\u2190\u2202\u2192_ : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD rs \u2192 SetLLD (ls \u2202 rs)\n \n \n \n data MSetLLD {i : Size} {u} : LinLogic i {u} \u2192 Set (lsuc u) where\n \u2205 : \u2200{ll} \u2192 MSetLLD ll\n \u00ac\u2205 : \u2200{ll} \u2192 SetLLD ll \u2192 MSetLLD ll\n \n -- TODO We shouldn't need this. When issue agda #2409 is resolved, remove this.\n drsize : \u2200{i u ll} \u2192 {j : Size< \u2191 i} \u2192 SetLLD {i} {u} ll \u2192 SetLLD {j} ll\n drsize (\u2193 mm) = (\u2193 mm)\n drsize (x \u2190\u2227) = (drsize x) \u2190\u2227\n drsize (\u2227\u2192 x) = \u2227\u2192 (drsize x)\n drsize (x \u2190\u2227\u2192 x\u2081) = (drsize x \u2190\u2227\u2192 drsize x\u2081)\n drsize (x \u2190\u2228) = (drsize x) \u2190\u2228\n drsize (\u2228\u2192 x) = \u2228\u2192 (drsize x)\n drsize (x \u2190\u2228\u2192 x\u2081) = (drsize x \u2190\u2228\u2192 drsize x\u2081)\n drsize (x \u2190\u2202) = (drsize x) \u2190\u2202\n drsize (\u2202\u2192 x) = \u2202\u2192 (drsize x)\n drsize (x \u2190\u2202\u2192 x\u2081) = (drsize x \u2190\u2202\u2192 drsize x\u2081)\n \n\n fillAllLowerD : \u2200{i u} \u2192 \u2200 ll \u2192 SetLLD {i} {u} ll\n fillAllLowerD \u2205 = \u2193 orig\n fillAllLowerD (\u03c4 x) = \u2193 orig\n fillAllLowerD (ll \u2227 ll\u2081) = (fillAllLowerD ll) \u2190\u2227\u2192 fillAllLowerD ll\u2081\n fillAllLowerD (ll \u2228 ll\u2081) = (fillAllLowerD ll) \u2190\u2228\u2192 fillAllLowerD ll\u2081\n fillAllLowerD (ll \u2202 ll\u2081) = (fillAllLowerD ll) \u2190\u2202\u2192 fillAllLowerD ll\u2081\n fillAllLowerD (call x) = \u2193 orig\n\n\n compose : \u2200{u i} \u2192 {j : Size< \u2191 i} \u2192 \u2200 {oll ll} \u2192 SetLLD {i} {u} oll \u2192 SetLLRem {_} {j} oll ll \u2192 SetLLD ll \u2192 SetLLD oll\n compose sdo sr (\u2193 x) = {!!}\n compose sdo sr (sd \u2190\u2227) = {!!}\n compose sdo sr (\u2227\u2192 sd) = {!!}\n compose sdo sr (sd \u2190\u2227\u2192 sd\u2081) = {!!}\n compose sdo sr (sd \u2190\u2228) = {!!}\n compose sdo sr (\u2228\u2192 sd) = {!!}\n compose sdo sr (sd \u2190\u2228\u2192 sd\u2081) = {!!}\n compose sdo sr (sd \u2190\u2202) = {!!}\n compose sdo sr (\u2202\u2192 sd) = {!!}\n compose sdo sr (sd \u2190\u2202\u2192 sd\u2081) = {!!} \n\n findNextCom : \u2200{u i} \u2192 {j : Size< \u2191 i} \u2192 {rll : LinLogic j {u}} \u2192 {ll : LinLogic i {u}} \u2192 LFun {rll = rll} {ll = ll} \u2192 SetLLD ll\n findNextCom {u} {i = pi} {ll = oll} lf = {!!} where\n findNextCom` : \u2200{pi} \u2192 {oll : LinLogic pi {u}} \u2192 {i : Size< \u2191 pi} \u2192 {j : Size< \u2191 i} \u2192 {rll : LinLogic j {u}} \u2192 {ll : LinLogic i {u}} \u2192 LFun {rll = rll} {ll = ll} \u2192 SetLLRem oll ll \u2192 SetLLRem oll rll \u00d7 SetLLD oll \n findNextCom` {oll = oll} I sr = (sr , fillAllLowerD oll)\n findNextCom` (_\u2282_ {pll = pll} {ll = ll} {ell = ell} {ind = ind} lf\u2081 lf\u2082) sr with (findNextCom` lf\u2081 (fillAllLowerRem pll))\n ... | (r\u2081 , r\u2082) with (findNextCom` lf\u2082 (fillAllLowerRem (replLL ll ind ell)))\n ... | (g\u2081 , g\u2082) = {!!}\n findNextCom` (tr lf\u2081) sr = {!!}\n findNextCom` (obs lf\u2081) sr = {!!}\n findNextCom` (com df lf\u2081) sr = {!!}\n findNextCom` (call x lf\u2081) sr = {!!}\n\n\n\n","old_contents":"module WellFormedLF where\n\nopen import Common\nopen import LinLogic\nopen import SetLL\n\n--data IndexLF : \u2200{u} \u2192 {i : Size} \u2192 {j : Size< \u2191 i} \u2192 {rll : LinLogic j {u}} \u2192 {ll : LinLogic i {u}} \u2192 LFun {u} {i} {j} {rll} {ll} \u2192 Set where\n-- \u2193 : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 \u2200{u rll ll} \u2192 (lf : LFun {u} {i} {j} {rll} {ll}) \u2192 IndexLF lf\n-- _\u2190\u2282_ : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 {k : Size< \u2191 j} \u2192 \u2200{u rll pll ell ll ind elf prf lf}\n-- \u2192 IndexLF elf\n-- \u2192 IndexLF (_\u2282_ {u} {i} {j} {k} {pll} {ll} {ell} {rll} {ind} elf {{prf}} lf)\n-- _\u2282\u2192_ : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 {k : Size< \u2191 j} \u2192 \u2200{u rll pll ell ll ind elf prf lf}\n-- \u2192 IndexLF lf\n-- \u2192 IndexLF (_\u2282_ {u} {i} {j} {k} {pll} {ll} {ell} {rll} {ind} elf {{prf}} lf)\n-- tr : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 \u2200{u ll orll rll} \u2192 {{ltr : LLTr orll ll}} \u2192 {lf : LFun {u} {i} {j} {rll} {orll}}\n-- \u2192 IndexLF lf \u2192 IndexLF (tr {{ltr = ltr}} lf) \n--\n\n-- _\u2190\u2282_ : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 {rll : LinLogic j {u}} \u2192 {ll : LinLogic i {u}} \u2192 (lf : LFun {u} {i} {j} {rll} {ll}) \u2192 IndexLF lf\n\n-- \u2193 probably the subtrees that contain all the inputs. \n-- We need to keep truck of all the latest subtrees that are outputs of coms. We can then check whether a transformation permutates them. If so , the tr is acceptable.\n-- transformations inside a subtree are acceptable.\n-- tranformations between two subtrees are only acceptable if the next com depends on both of them.\n-- If more than one subtree depends on a specific subtree, but the two do not depend on each other, we first separate the elements for the first subtree and then for the other.\n-- Since these coms can be independently executed, there can be tranformations of one subtree that can not be done while the others are ready. --> We remove the separation transformation and consider that\n-- the unexecuted transformations are done on those separated elements.\n\n-- > The input tree contains subtrees that represent exactly the input of the coms it can execute.\n\n-- 2 TWO\n-- We need three structures.\n-- THe first is used to identify inputs from the same com. When a tranformation splits these coms, we also split the set into two sets. Next we track all the inputs that are part of the tranformation, be it sets of inputs of a specific com or individual inputs. From all these sets, at least one item from each set needs to be the input of the next com.\n-- The set of coms that are used to allow for commutation of inputs.\n\nmodule _ where\n\n open import Data.Vec\n \n data Ancestor : Set where\n orig : Ancestor\n anc : \u2115 \u2192 Ancestor \u2192 Ancestor\n manc : \u2115 \u2192 \u2200{n} \u2192 Vec Ancestor n \u2192 Ancestor \u2192 Ancestor\n\n\n \n data SetLLD {i : Size} {u} : LinLogic i {u} \u2192 Set (lsuc u) where\n \u2193 : \u2200{ll} \u2192 Ancestor \u2192 SetLLD ll\n _\u2190\u2227 : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD (ls \u2227 rs)\n \u2227\u2192_ : \u2200{rs ls} \u2192 SetLLD rs \u2192 SetLLD (ls \u2227 rs)\n _\u2190\u2227\u2192_ : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD rs \u2192 SetLLD (ls \u2227 rs)\n _\u2190\u2228 : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD (ls \u2228 rs)\n \u2228\u2192_ : \u2200{rs ls} \u2192 SetLLD rs \u2192 SetLLD (ls \u2228 rs)\n _\u2190\u2228\u2192_ : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD rs \u2192 SetLLD (ls \u2228 rs)\n _\u2190\u2202 : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD (ls \u2202 rs)\n \u2202\u2192_ : \u2200{rs ls} \u2192 SetLLD rs \u2192 SetLLD (ls \u2202 rs)\n _\u2190\u2202\u2192_ : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD rs \u2192 SetLLD (ls \u2202 rs)\n \n \n \n data MSetLLD {i : Size} {u} : LinLogic i {u} \u2192 Set (lsuc u) where\n \u2205 : \u2200{ll} \u2192 MSetLLD ll\n \u00ac\u2205 : \u2200{ll} \u2192 SetLLD ll \u2192 MSetLLD ll\n \n -- TODO We shouldn't need this. When issue agda #2409 is resolved, remove this.\n drsize : \u2200{i u ll} \u2192 {j : Size< \u2191 i} \u2192 SetLLD {i} {u} ll \u2192 SetLLD {j} ll\n drsize (\u2193 mm) = (\u2193 mm)\n drsize (x \u2190\u2227) = (drsize x) \u2190\u2227\n drsize (\u2227\u2192 x) = \u2227\u2192 (drsize x)\n drsize (x \u2190\u2227\u2192 x\u2081) = (drsize x \u2190\u2227\u2192 drsize x\u2081)\n drsize (x \u2190\u2228) = (drsize x) \u2190\u2228\n drsize (\u2228\u2192 x) = \u2228\u2192 (drsize x)\n drsize (x \u2190\u2228\u2192 x\u2081) = (drsize x \u2190\u2228\u2192 drsize x\u2081)\n drsize (x \u2190\u2202) = (drsize x) \u2190\u2202\n drsize (\u2202\u2192 x) = \u2202\u2192 (drsize x)\n drsize (x \u2190\u2202\u2192 x\u2081) = (drsize x \u2190\u2202\u2192 drsize x\u2081)\n \n fillAllLowerD : \u2200{i u} \u2192 \u2200 ll \u2192 SetLLD {i} {u} ll\n fillAllLowerD ll = {!!}\n \n \n \n","returncode":0,"stderr":"","license":"mpl-2.0","lang":"Agda"} {"commit":"120a6dd883e6567cf125c6d37bb76c3063ce2241","subject":"Fixed an theorem.","message":"Fixed an theorem.\n\nIgnore-this: a7c05b0bdd220240bb7c76cb5a83c29e\n\ndarcs-hash:20120303194550-3bd4e-12021126bfb9a318cdc8105eeab0d45f82ee7a8f.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOL\/TheoremsATP.agda","new_file":"src\/FOL\/TheoremsATP.agda","new_contents":"------------------------------------------------------------------------------\n-- FOL theorems\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module contains some examples showing the use of the ATPs for\n-- proving FOL theorems.\n\nmodule FOL.TheoremsATP where\n\nopen import FOL.Base\n\n------------------------------------------------------------------------------\n-- We postulate some formulae and propositional functions.\npostulate\n A : Set\n A\u00b9 B\u00b9 : D \u2192 Set\n A\u00b2 : D \u2192 D \u2192 Set\n\n-- The introduction and elimination rules for the quantifiers are theorems.\n{-\n \u03c6(x)\n ----------- \u2200-intro\n \u2200x.\u03c6(x)\n\n \u2200x.\u03c6(x)\n ----------- \u2200-elim\n \u03c6(t)\n\n \u03c6(t)\n ----------- \u2203-intro\n \u2203x.\u03c6(x)\n\n \u2203x.\u03c6(x) \u03c6(x) \u2192 \u03c8\n ---------------------- \u2203-elim\n \u03c8\n-}\n\npostulate\n -- TODO: 2012-03-02. Fix? the universal introduction\n \u2200-intro : (\u2200 x \u2192 A\u00b9 x) \u2192 \u2200 x \u2192 A\u00b9 x\n \u2200-elim : (\u2200 x \u2192 A\u00b9 x) \u2192 (t : D) \u2192 A\u00b9 t\n -- It is necessary to assume a non-empty domain. See\n -- FOL.NonEmptyDomain.TheoremsI\/ATP.\u2203I.\n --\n -- TODO: 2012-02-28. Fix the existential introduction rule.\n -- \u2203-intro : ((t : D) \u2192 A\u00b9 t) \u2192 \u2203 A\u00b9\n \u2203-elim' : \u2203 A\u00b9 \u2192 ((x : D) \u2192 A\u00b9 x \u2192 A) \u2192 A\n-- {-# ATP prove \u2200-intro #-}\n{-# ATP prove \u2200-elim #-}\n-- {-# ATP prove \u2203-intro #-}\n{-# ATP prove \u2203-elim' #-}\n\n-- Generalization of De Morgan's laws.\npostulate\n gDM\u2081 : \u00ac (\u2200 x \u2192 A\u00b9 x) \u2194 (\u2203[ x ] \u00ac (A\u00b9 x))\n gDM\u2082 : \u00ac (\u2203 A\u00b9) \u2194 (\u2200 x \u2192 \u00ac (A\u00b9 x))\n gDM\u2083 : (\u2200 x \u2192 A\u00b9 x) \u2194 \u00ac (\u2203[ x ] \u00ac (A\u00b9 x))\n gDM\u2084 : \u2203 A\u00b9 \u2194 \u00ac (\u2200 x \u2192 \u00ac (A\u00b9 x))\n{-# ATP prove gDM\u2081 #-}\n{-# ATP prove gDM\u2082 #-}\n{-# ATP prove gDM\u2083 #-}\n{-# ATP prove gDM\u2084 #-}\n\n-- The order of quantifiers of the same sort is irrelevant.\npostulate\n \u2200-ord : (\u2200 x y \u2192 A\u00b2 x y) \u2194 (\u2200 y x \u2192 A\u00b2 x y)\n \u2203-ord : (\u2203[ x ] \u2203[ y ] A\u00b2 x y) \u2194 (\u2203[ y ] \u2203[ x ] A\u00b2 x y)\n{-# ATP prove \u2200-ord #-}\n{-# ATP prove \u2203-ord #-}\n\n-- Quantification over a variable that does not occur can be erased or\n-- added.\npostulate\n -- TODO: 2012-02-03. The ATPs are not proving the theorem.\n -- \u2200-erase-add : ((x : D) \u2192 A) \u2194 A\n \u2203-erase-add : (\u2203[ x ] A \u2227 A\u00b9 x) \u2194 A \u2227 (\u2203[ x ] A\u00b9 x)\n-- {-# ATP prove \u2200-erase-add #-}\n{-# ATP prove \u2203-erase-add #-}\n\n-- Distributes laws for the quantifiers.\npostulate\n \u2200-dist : (\u2200 x \u2192 A\u00b9 x \u2227 B\u00b9 x) \u2194 ((\u2200 x \u2192 A\u00b9 x) \u2227 (\u2200 x \u2192 B\u00b9 x))\n \u2203-dist : (\u2203[ x ] A\u00b9 x \u2228 B\u00b9 x) \u2194 (\u2203 A\u00b9 \u2228 \u2203 B\u00b9)\n{-# ATP prove \u2200-dist #-}\n{-# ATP prove \u2203-dist #-}\n\n-- Interchange of quantifiers.\n-- The related theorem \u2200x\u2203y.Axy \u2192 \u2203y\u2200x.Axy is not (classically) valid.\npostulate \u2203\u2200 : \u2203[ x ] (\u2200 y \u2192 A\u00b2 x y) \u2192 \u2200 y \u2192 \u2203[ x ] A\u00b2 x y\n{-# ATP prove \u2203\u2200 #-}\n\n-- \u2203 in terms of \u2200 and \u00ac.\npostulate\n \u2203\u2192\u00ac\u2200\u00ac : \u2203[ x ] A\u00b9 x \u2192 \u00ac (\u2200 {x} \u2192 \u00ac A\u00b9 x)\n \u2203\u00ac\u2192\u00ac\u2200 : \u2203[ x ] \u00ac A\u00b9 x \u2192 \u00ac (\u2200 {x} \u2192 A\u00b9 x)\n{-# ATP prove \u2203\u2192\u00ac\u2200\u00ac #-}\n{-# ATP prove \u2203\u00ac\u2192\u00ac\u2200 #-}\n\n-- \u2200 in terms of \u2203 and \u00ac.\npostulate\n \u2200\u2192\u00ac\u2203\u00ac : (\u2200 {x} \u2192 A\u00b9 x) \u2192 \u00ac (\u2203[ x ] \u00ac A\u00b9 x)\n \u2200\u00ac\u2192\u00ac\u2203 : (\u2200 {x} \u2192 \u00ac A\u00b9 x) \u2192 \u00ac (\u2203[ x ] A\u00b9 x)\n{-# ATP prove \u2200\u2192\u00ac\u2203\u00ac #-}\n{-# ATP prove \u2200\u00ac\u2192\u00ac\u2203 #-}\n","old_contents":"------------------------------------------------------------------------------\n-- FOL theorems\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module contains some examples showing the use of the ATPs for\n-- proving FOL theorems.\n\nmodule FOL.TheoremsATP where\n\nopen import FOL.Base\n\n------------------------------------------------------------------------------\n-- We postulate some formulae and propositional functions.\npostulate\n A : Set\n A\u00b9 B\u00b9 : D \u2192 Set\n A\u00b2 : D \u2192 D \u2192 Set\n\n-- The introduction and elimination rules for the quantifiers are theorems.\n{-\n \u03c6(x)\n ----------- \u2200-intro\n \u2200x.\u03c6(x)\n\n \u2200x.\u03c6(x)\n ----------- \u2200-elim\n \u03c6(t)\n\n \u03c6(t)\n ----------- \u2203-intro\n \u2203x.\u03c6(x)\n\n \u2203x.\u03c6(x) \u03c6(x) \u2192 \u03c8\n ---------------------- \u2203-elim\n \u03c8\n-}\n\npostulate\n -- TODO: 2012-03-02. Fix? the universal introduction\n \u2200-intro : (\u2200 x \u2192 A\u00b9 x) \u2192 \u2200 x \u2192 A\u00b9 x\n \u2200-elim : (\u2200 x \u2192 A\u00b9 x) \u2192 (t : D) \u2192 A\u00b9 t\n -- It is necessary to assume a non-empty domain. See\n -- FOL.NonEmptyDomain.TheoremsI\/ATP.\u2203I.\n --\n -- TODO: 2012-02-28. Fix the existential introduction rule.\n -- \u2203-intro : ((t : D) \u2192 A\u00b9 t) \u2192 \u2203 A\u00b9\n \u2203-elim' : \u2203 A\u00b9 \u2192 ((x : D) \u2192 A\u00b9 x \u2192 A) \u2192 A\n-- {-# ATP prove \u2200-intro #-}\n{-# ATP prove \u2200-elim #-}\n-- {-# ATP prove \u2203-intro #-}\n{-# ATP prove \u2203-elim' #-}\n\n-- Generalization of De Morgan's laws.\npostulate\n gDM\u2081 : \u00ac (\u2200 x \u2192 A\u00b9 x) \u2194 (\u2203[ x ] \u00ac (A\u00b9 x))\n gDM\u2082 : \u00ac (\u2203 A\u00b9) \u2194 (\u2200 x \u2192 \u00ac (A\u00b9 x))\n gDM\u2083 : (\u2200 x \u2192 A\u00b9 x) \u2194 \u00ac (\u2203[ x ] \u00ac (A\u00b9 x))\n gDM\u2084 : \u2203 A\u00b9 \u2194 \u00ac (\u2200 x \u2192 \u00ac (A\u00b9 x))\n{-# ATP prove gDM\u2081 #-}\n{-# ATP prove gDM\u2082 #-}\n{-# ATP prove gDM\u2083 #-}\n{-# ATP prove gDM\u2084 #-}\n\n-- The order of quantifiers of the same sort is irrelevant.\npostulate\n \u2200-ord : (\u2200 x y \u2192 A\u00b2 x y) \u2194 (\u2200 y x \u2192 A\u00b2 x y)\n \u2203-ord : (\u2203[ x ] \u2203[ y ] A\u00b2 x y) \u2194 (\u2203[ y ] \u2203[ x ] A\u00b2 x y)\n{-# ATP prove \u2200-ord #-}\n{-# ATP prove \u2203-ord #-}\n\n-- Quantification over a variable that does not occur can be erased or\n-- added.\npostulate\n -- TODO: 2012-02-03. The ATPs are not proving the theorem.\n -- \u2200-erase-add : ((x : D) \u2192 A) \u2194 A\n \u2203-erase-add : (\u2203[ x ] A \u2227 A\u00b9 x) \u2194 A \u2227 (\u2203[ x ] A\u00b9 x)\n-- {-# ATP prove \u2200-erase-add #-}\n{-# ATP prove \u2203-erase-add #-}\n\n-- Distributes laws for the quantifiers.\npostulate\n \u2200-dist : \u2200 x \u2192 A\u00b9 x \u2227 B\u00b9 x \u2194 ((\u2200 x \u2192 A\u00b9 x) \u2227 (\u2200 x \u2192 B\u00b9 x))\n \u2203-dist : (\u2203[ x ] A\u00b9 x \u2228 B\u00b9 x) \u2194 (\u2203 A\u00b9 \u2228 \u2203 B\u00b9)\n{-# ATP prove \u2200-dist #-}\n{-# ATP prove \u2203-dist #-}\n\n-- Interchange of quantifiers.\n-- The related theorem \u2200x\u2203y.Axy \u2192 \u2203y\u2200x.Axy is not (classically) valid.\npostulate \u2203\u2200 : \u2203[ x ] (\u2200 y \u2192 A\u00b2 x y) \u2192 \u2200 y \u2192 \u2203[ x ] A\u00b2 x y\n{-# ATP prove \u2203\u2200 #-}\n\n-- \u2203 in terms of \u2200 and \u00ac.\npostulate\n \u2203\u2192\u00ac\u2200\u00ac : \u2203[ x ] A\u00b9 x \u2192 \u00ac (\u2200 {x} \u2192 \u00ac A\u00b9 x)\n \u2203\u00ac\u2192\u00ac\u2200 : \u2203[ x ] \u00ac A\u00b9 x \u2192 \u00ac (\u2200 {x} \u2192 A\u00b9 x)\n{-# ATP prove \u2203\u2192\u00ac\u2200\u00ac #-}\n{-# ATP prove \u2203\u00ac\u2192\u00ac\u2200 #-}\n\n-- \u2200 in terms of \u2203 and \u00ac.\npostulate\n \u2200\u2192\u00ac\u2203\u00ac : (\u2200 {x} \u2192 A\u00b9 x) \u2192 \u00ac (\u2203[ x ] \u00ac A\u00b9 x)\n \u2200\u00ac\u2192\u00ac\u2203 : (\u2200 {x} \u2192 \u00ac A\u00b9 x) \u2192 \u00ac (\u2203[ x ] A\u00b9 x)\n{-# ATP prove \u2200\u2192\u00ac\u2203\u00ac #-}\n{-# ATP prove \u2200\u00ac\u2192\u00ac\u2203 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"6b698f52d57559a1c01ed1ef0ab1afb783796b9a","subject":"Strong-Fiat-Shamir: some progress and re-org","message":"Strong-Fiat-Shamir: some progress and re-org\n","repos":"crypto-agda\/crypto-agda","old_file":"ZK\/Strong-Fiat-Shamir.agda","new_file":"ZK\/Strong-Fiat-Shamir.agda","new_contents":"open import Type\nopen import Function.NP\nopen import Data.Maybe\nopen import Data.Zero\nopen import Data.One\nopen import Data.Two\nopen import Data.Sum using (_\u228e_)\nopen import Data.List.NP renaming (map to map\u1d38)\nopen import Data.Product.NP renaming (proj\u2081 to fst; proj\u2082 to snd)\nopen import Relation.Nullary.Decidable\nopen import Relation.Nullary\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Control.Strategy using (Strategy; module TranscriptRun; done; ask)\n\nmodule ZK.Strong-Fiat-Shamir\n {W \u039b : \u2605}{L : W \u2192 \u039b \u2192 \u2605}\n (any-W : W)\n {RS : \u2605}\n (L? : \u2200 w Y \u2192 Dec (L w Y))\n (\u039b? : Decidable (_\u2261_ {A = \u039b}))\n (Eps : \u2605)\n (\u03b5[_] : Eps \u2192 \u2605)\n (\u03b50 : Eps)\n (\u03b5[0] : \u03b5[ \u03b50 ] \u2261 \ud835\udfd8)\n where\n\nmodule _ {R : \u2605} where\n _\u224b_ : (f g : R \u2192 \ud835\udfda) \u2192 \u2605\u2081\n f \u224b g = (\u03a3 R (\u2713 \u2218 f)) \u2261 (\u03a3 R (\u2713 \u2218 g))\n\n _\u2248_ : (f g : R \u2192 \ud835\udfda) \u2192 \u2605\u2081\n f \u2248 g = \u2203\u2082 \u03bb \u03b5\u2080 \u03b5\u2081 \u2192\n (\u03a3 R (\u2713 \u2218 f) \u228e \u03b5[ \u03b5\u2080 ]) \u2261 (\u03a3 R (\u2713 \u2218 g) \u228e \u03b5[ \u03b5\u2081 ])\n\n \u224b\u2192\u2248 : \u2200 {f g} \u2192 f \u224b g \u2192 f \u2248 g\n \u224b\u2192\u2248 f\u224bg = \u03b50 , \u03b50 , ap (flip _\u228e_ \u03b5[ \u03b50 ]) f\u224bg\n\n{-\nRandom-Oracle-List : \u2605\nRandom-Oracle-List = List (Q \u00d7 Resp)\n-}\n\nmodule Game-Types (Q Resp : \u2605)(Prf : \u039b \u2192 \u2605) where\n Random-Oracle : \u2605\n Random-Oracle = Q \u2192 Resp\n\n data Adversary-Query : \u2605 where\n query-H : (q : Q) \u2192 Adversary-Query\n query-create-proof : (w : W)(Y : \u039b) \u2192 Adversary-Query\n\n Challenger-Resp : Adversary-Query \u2192 \u2605\n Challenger-Resp (query-H s) = Resp\n Challenger-Resp (query-create-proof w Y) = Maybe (Prf Y)\n\n Adversary : \u2605 \u2192 \u2605\n Adversary = Strategy Adversary-Query Challenger-Resp\n\n Transcript = List (\u03a3 Adversary-Query Challenger-Resp)\n\n Prfs : \u2605\n Prfs = List (\u03a3 \u039b Prf)\n\nrecord Proof-System (RP : \u2605)(Prf : \u039b \u2192 \u2605) : \u2605 where\n field\n Prove : RP \u2192 (w : W)(Y : \u039b) \u2192 Prf Y\n Verify : (Y : \u039b)(\u03c0 : Prf Y) \u2192 \ud835\udfda\n\n Complete : \u2605\n Complete = \u2200 rp {w Y} \u2192 L w Y \u2192 Verify Y (Prove rp w Y) \u2261 1\u2082\n\n -- Not in the paper but...\n Sound : \u2605\n Sound = \u2200 rp {w Y} \u2192 Verify Y (Prove rp w Y) \u2261 1\u2082 \u2192 L w Y\n\nrecord Simulator (Q : \u2605)(Resp : \u2605){Prf RP}(PF : Proof-System RP Prf) : \u2605 where\n open Proof-System PF\n open Game-Types Q Resp Prf\n\n field\n -- Computing the compound patch to the random oracle\n H-patch : RS \u2192 Transcript \u2192 Random-Oracle \u2192 Random-Oracle\n\n -- Simulate does not patch itself but H-patch does\n Simulate : RS \u2192 Transcript \u2192 (Y : \u039b) \u2192 Prf Y\n verify-sim-spec : \u2200 rs t Y \u2192 Verify Y (Simulate rs t Y) \u2261 1\u2082\n\n open Proof-System PF public\n open Game-Types Q Resp Prf public\n\nmodule Is-Zero-Knowledge\n {RP Prf Q Resp}\n (L-to-Prf : \u2200 {w Y} \u2192 L w Y \u2192 Prf Y)\n (PF : Proof-System RP Prf)\n (open Proof-System PF)\n (sim : Simulator Q Resp PF)\n (open Simulator sim)\n (ro : Random-Oracle)\n where\n\n simulated-Challenger : RS \u2192 Transcript \u2192 \u2200 q \u2192 Challenger-Resp q\n simulated-Challenger rs t (query-H q) = H-patch rs t ro q\n simulated-Challenger rs t (query-create-proof w Y) with L? w Y\n ... | yes p = just (Simulate rs t Y)\n ... | no \u00acp = nothing\n\n real-Challenger : \u2200 q \u2192 Challenger-Resp q\n real-Challenger (query-H q) = ro q\n real-Challenger (query-create-proof w Y) with L? w Y\n ... | yes p = just (L-to-Prf p)\n ... | no _ = nothing\n\n challenger : \ud835\udfda \u2192 RS \u2192 Transcript \u2192 \u2200 q \u2192 Challenger-Resp q\n challenger 0\u2082 _ _ = real-Challenger\n challenger 1\u2082 rs t = simulated-Challenger rs t\n\n open TranscriptRun\n -- \u03b2 = 0\u2082 is real\n -- \u03b2 = 1\u2082 is simulated\n Experiment : \u2200 {Output} \u2192 \ud835\udfda \u2192 Adversary Output \u2192 RS \u2192 Output \u00d7 Transcript\n Experiment \u03b2 adv rs = runT (challenger \u03b2 rs) adv []\n\n EXP\u2080 EXP\u2081 : Adversary \ud835\udfda \u2192 RS \u2192 \ud835\udfda\n EXP\u2080 Adv = fst \u2218 Experiment 0\u2082 Adv\n EXP\u2081 Adv = fst \u2218 Experiment 1\u2082 Adv\n\n -- Too strong\n IsZK = \u2200 Adv \u2192 EXP\u2080 Adv \u2248 EXP\u2081 Adv\n\n{-\n-- there exists a simulator, such that for all adversaries they are clueless if\n-- they are in the real or simulated Experiment\nZero-Knowledge : Proof-System \u2192 \u2605\nZero-Knowledge PF = \u03a3 (Simulator PF) (\u03bb sim \u2192 {!!})\n-}\n\nmodule Simulation-Sound-Extractability\n {RP}{Prf : \u039b \u2192 \u2605}\n (PF : Proof-System RP Prf)\n (Prf? : \u2200 {Y Y'} \u2192 Prf Y \u2192 Prf Y' \u2192 \ud835\udfda)\n (Q Resp : \u2605)\n -- (Prf? : \u2200 Y \u2192 Decidable (_\u2261_ {A = Prf Y}))\n where\n open Proof-System PF\n open Game-Types Q Resp Prf\n\n Prf-in-Q : \u2200 {Y} \u2192 Prf Y \u2192 \u03a3 Adversary-Query Challenger-Resp \u2192 \ud835\udfda\n Prf-in-Q \u03c0 (query-create-proof _ _ , just \u03c0') = Prf? \u03c0 \u03c0'\n Prf-in-Q \u03c0 _ = 0\u2082\n\n HistoryForExtractor = List (Prfs \u00d7 Transcript)\n\n ExtractorServerPart =\n (past-history : HistoryForExtractor) {- previous invocations of Adv -}\n (on-going-transcript : Transcript) {- about the current invocation of Adv -}\n \u2192 \u03a0 Adversary-Query Challenger-Resp\n\n Extractor : \u2605\n Extractor = Prfs \u2192 (init-transcript : Transcript)\n \u2192 ExtractorServerPart\n \u00d7 Strategy \ud835\udfd9 (const (Prfs \u00d7 Transcript)) (List W)\n\n valid-witness? : W \u2192 \u039b \u2192 \ud835\udfda\n valid-witness? w Y = \u230a L? w Y \u230b\n\n valid-witnesses? : List W \u2192 List \u039b \u2192 \ud835\udfda\n valid-witnesses? [] [] = 1\u2082\n valid-witnesses? (w \u2237 ws) (prf \u2237 prfs) = valid-witness? w prf \u2227 valid-witnesses? ws prfs\n valid-witnesses? _ _ = 0\u2082\n\n open TranscriptRun\n\n module _ (t : Transcript) where\n Prf-in-Transcript : \u2200 {Y} \u2192 Prf Y \u2192 \ud835\udfda\n Prf-in-Transcript \u03c0 = any (Prf-in-Q \u03c0) t\n\n K-winning-prf : \u03a3 \u039b Prf \u2192 \ud835\udfda\n K-winning-prf (Y , \u03c0) = not (Verify Y \u03c0)\n \u2228 Prf-in-Transcript \u03c0\n\n K-winning-prfs : Prfs \u2192 \ud835\udfda\n K-winning-prfs [] = 1\u2082\n K-winning-prfs prfs = any K-winning-prf prfs\n\n module Game\n (L-to-Prf : \u2200 {w Y} \u2192 L w Y \u2192 Prf Y)\n (sim : Simulator Q Resp PF)\n (open Is-Zero-Knowledge L-to-Prf PF sim)\n {RA : \u2605}\n\n {- The malicious prover -}\n (Adv : RA \u2192 Adversary Prfs)\n (w : RA)(rs : RS)(ro : Q \u2192 Resp)(K' : Extractor) where\n\n initial-result = Experiment ro 1\u2082 (Adv w) rs\n\n initial-prfs : Prfs\n initial-prfs = fst initial-result\n\n initial-transcript : Transcript\n initial-transcript = snd initial-result\n\n K-winning-intial-run : \ud835\udfda\n K-winning-intial-run = K-winning-prfs initial-transcript initial-prfs\n\n -- Second run\n\n K = K' initial-prfs initial-transcript\n\n Kf = fst K\n Ks = snd K\n\n ws = fst (runT (\u03bb h _ \u2192 runT (\u03bb t q \u2192 Kf (map\u1d38 snd h) t q) (Adv w) []) Ks [])\n\n K-winning-second-run : \ud835\udfda\n K-winning-second-run = valid-witnesses? ws (map\u1d38 fst initial-prfs)\n\nmodule Sigma-Protocol\n (Commitment Challenge : \u2605)\n (\u03a3-Prf : \u039b \u2192 \u2605)\n {RP R\u03a3P : \u2605}\n (any-R\u03a3P : R\u03a3P)\n where\n\n record \u03a3-Prover : \u2605 where\n field\n get-A : R\u03a3P \u2192 (Y : \u039b) \u2192 Commitment\n get-f : R\u03a3P \u2192 (Y : \u039b) \u2192 (c : Challenge) \u2192 \u03a3-Prf Y\n\n record \u03a3-Transcript (Y : \u039b) : \u2605 where\n constructor mk\n field\n get-A : Commitment\n get-c : Challenge\n get-f : \u03a3-Prf Y\n\n \u03a3-Verifier : \u2605\n \u03a3-Verifier = (Y : \u039b)(t : \u03a3-Transcript Y) \u2192 \ud835\udfda\n\n record \u03a3-Protocol : \u2605 where\n constructor _,_\n field\n \u03a3-verifier : \u03a3-Verifier\n \u03a3-prover : \u03a3-Prover\n open \u03a3-Prover \u03a3-prover public\n\n \u03a3-game : (r : R\u03a3P)(Y : \u039b)(c : Challenge) \u2192 \ud835\udfda\n \u03a3-game r Y c = \u03a3-verifier Y (mk A c f)\n where\n A = get-A r Y\n f = get-f r Y c\n\n _\u21c4_ : \u03a3-Verifier \u2192 \u03a3-Prover \u2192 R\u03a3P \u2192 \u039b \u2192 Challenge \u2192 \ud835\udfda\n (v \u21c4 p) r Y c = \u03a3-Protocol.\u03a3-game (v , p) r Y c\n\n Correct : \u03a3-Protocol \u2192 \u2605\n Correct (v , p) = \u2200 {Y w} \u2192 L w Y \u2192 (r : R\u03a3P)(c : _) \u2192\n let open \u03a3-Prover p\n in v Y (mk (get-A r Y) c (get-f r Y c)) \u2261 1\u2082\n\n record Special-Honest-Verifier-Zero-Knowledge (\u03a3-proto : \u03a3-Protocol) : \u2605 where\n open \u03a3-Protocol \u03a3-proto\n field\n Simulate : (Y : \u039b)(c : Challenge)(f : \u03a3-Prf Y) \u2192 Commitment\n Simulate-ok : \u2200 Y c f \u2192 \u03a3-verifier Y (mk (Simulate Y c f) c f) \u2261 1\u2082\n -- If (c,f) uniformly distributed then (Simulate Y c f , c , f) is\n -- distributed equaly to ((\u03a3-verifier \u21c4 \u03a3-prover) r Y c)\n\n -- A pair of \"\u03a3-Transcript\"s such that the commitment is shared\n -- and the challenges are different.\n record \u03a3-Transcript\u00b2 Y : \u2605 where\n constructor mk\n field\n -- The commitment is shared\n get-A : Commitment\n\n -- The challenges...\n get-c\u2080 get-c\u2081 : Challenge\n\n -- ...are different\n c\u2080\u2262c\u2081 : get-c\u2080 \u2262 get-c\u2081\n\n -- The proofs are arbitrary\n get-f\u2080 get-f\u2081 : \u03a3-Prf Y\n\n record Special-Soundness : \u2605 where\n field\n Extract : \u2200 {Y}(t : \u03a3-Transcript\u00b2 Y) \u2192 W\n Extract-ok : \u2200 {Y}(t : \u03a3-Transcript\u00b2 Y) \u2192 L (Extract t) Y\n\n module Fiat-Shamir-Transformation\n (\u03a3-proto : \u03a3-Protocol)\n (shvzk : Special-Honest-Verifier-Zero-Knowledge \u03a3-proto)\n where\n\n open \u03a3-Protocol \u03a3-proto\n open Special-Honest-Verifier-Zero-Knowledge shvzk\n\n FS-Prf : \u039b \u2192 \u2605\n FS-Prf Y = Challenge \u00d7 \u03a3-Prf Y\n\n sFS : (H : (\u039b \u00d7 Commitment) \u2192 Challenge) \u2192 Proof-System R\u03a3P FS-Prf\n sFS H = record { Prove = sFS-Prove ; Verify = sFS-Verify }\n where\n sFS-Prove : R\u03a3P \u2192 W \u2192 (Y : \u039b) \u2192 FS-Prf Y\n sFS-Prove r w Y = let c = H (Y , get-A r Y) in c , get-f r Y c\n sFS-Verify : \u2200 Y \u2192 Challenge \u00d7 \u03a3-Prf Y \u2192 \ud835\udfda\n sFS-Verify Y (c , \u03c0) = \u03a3-verifier Y (mk (Simulate Y c \u03c0) c \u03c0)\n\n -- The weak fiat-shamir is like the strong one but the H function do not get to see\n -- the statement, hence the 'snd'\n wFS : (H : Commitment \u2192 Challenge) \u2192 Proof-System R\u03a3P (\u03bb Y \u2192 Challenge \u00d7 \u03a3-Prf Y)\n wFS H = sFS (H \u2218 snd)\n\n module Theorem1\n (\u03a3-proto : \u03a3-Protocol)\n -- (\u03a3-correct : Correct \u03a3-proto)\n (shvzk : Special-Honest-Verifier-Zero-Knowledge \u03a3-proto)\n (ssound : Special-Soundness)\n (open \u03a3-Protocol \u03a3-proto)\n (H : (\u039b \u00d7 Commitment) \u2192 Challenge)\n where\n module SHVZK = Special-Honest-Verifier-Zero-Knowledge shvzk\n\n module FST = Fiat-Shamir-Transformation \u03a3-proto shvzk\n open FST using (FS-Prf)\n\n Q = \u039b \u00d7 Commitment\n Resp = Challenge\n\n sFS : Proof-System R\u03a3P FS-Prf\n sFS = FST.sFS H\n module SFS = Proof-System sFS\n\n FS-Prf? : {Y Y' : \u039b} \u2192 FS-Prf Y \u2192 FS-Prf Y' \u2192 \ud835\udfda\n FS-Prf? \u03c0 \u03c0' = {!!}\n\n open Simulation-Sound-Extractability sFS FS-Prf? Q Resp\n\n L-to-FS-Prf : \u2200 {w Y} \u2192 L w Y \u2192 FS-Prf Y\n L-to-FS-Prf {w} w\u2208Y = SFS.Prove any-R\u03a3P w _\n\n FS-Random-Oracle = (\u039b \u00d7 Commitment) \u2192 Challenge\n FS-Patch = FS-Random-Oracle \u2192 FS-Random-Oracle\n\n open Special-Soundness ssound\n open Game-Types Q Resp FS-Prf\n\n module _ (rnd-c : \u039b \u2192 Challenge)(rnd-\u03a3-Prf : \u2200 Y \u2192 \u03a3-Prf Y) where\n\n module _ (rs : RS) where\n {-\n \u03a3-t\u00b2 : \u2200{Y} \u2192 SFS.Transcript \u2192 Maybe (\u03a3-Transcript\u00b2 Y)\n \u03a3-t\u00b2 t = {!!}\n -}\n\n\n {-\nmodule H-def (ro : Random-Oracle)(t : Random-Oracle-List)(q : Q) where\n H : Resp\n H with find (\u03bb { (query-H q' , r) \u2192 \u230a Q? q' q \u230b\n ; (query-create-proof _ _ , _) \u2192 0\u2082 }) t\n ... | just (query-H q\u2081 , r) = r\n ... | _ = ro q\n -}\n H-patch-1 : (q : Adversary-Query) \u2192 Challenger-Resp q \u2192 FS-Random-Oracle \u2192 FS-Random-Oracle\n H-patch-1 (query-H q) r = id\n H-patch-1 (query-create-proof w Y) r ro (Y' , c')\n = ro (Y' , case \u230a \u039b? Y Y' \u230b 0: c' 1: SHVZK.Simulate Y (rnd-c Y) (rnd-\u03a3-Prf Y))\n\n H-patch : Transcript \u2192 FS-Patch\n H-patch [] = id\n H-patch ((q , r) \u2237 t) = H-patch-1 q r \u2218 H-patch t\n\n module _ (t : Transcript)(Y : \u039b) where\n Simulate : FS-Prf Y\n Simulate = rnd-c Y , rnd-\u03a3-Prf Y\n -- SFS.Prove any-R\u03a3P (maybe\u2032 (Extract {Y}) any-W (\u03a3-t\u00b2 t)) Y\n\n c : Challenge\n c = fst Simulate\n\n \u03a3-prf : \u03a3-Prf Y\n \u03a3-prf = snd Simulate\n\n verify-sim-spec : SFS.Verify Y Simulate \u2261 1\u2082\n verify-sim-spec = SHVZK.Simulate-ok Y c \u03a3-prf\n\n S : Simulator (\u039b \u00d7 Commitment) Challenge sFS\n S = record { H-patch = H-patch\n ; Simulate = Simulate\n ; verify-sim-spec = verify-sim-spec }\n\n open Is-Zero-Knowledge L-to-FS-Prf sFS S {!!}\n\n module _ Adv where\n is-zk' : EXP\u2080 Adv \u224b EXP\u2081 Adv\n is-zk' = {!!}\n\n is-zk : IsZK\n is-zk Adv = \u224b\u2192\u2248 (is-zk' Adv)\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"open import Type\nopen import Function.NP\nopen import Data.Maybe\nopen import Data.One\nopen import Data.Two\nopen import Data.List.NP renaming (map to map\u1d38)\nopen import Data.Product.NP renaming (proj\u2081 to fst; proj\u2082 to snd)\nopen import Relation.Nullary.Decidable\nopen import Relation.Nullary\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\n\nopen import Control.Strategy\n\nmodule ZK.Strong-Fiat-Shamir\n {W \u039b : \u2605}{L : W \u2192 \u039b \u2192 \u2605}{Prf : \u039b \u2192 \u2605}\n {RS : \u2605}{Q : \u2605}{Resp : \u2605}\n (L? : \u2200 w Y \u2192 Dec (L w Y))\n (L-to-Prf : \u2200 {w Y} \u2192 L w Y \u2192 Prf Y)\n (Prf? : \u2200 {Y Y'} \u2192 Prf Y \u2192 Prf Y' \u2192 \ud835\udfda)\n -- (Prf? : \u2200 Y \u2192 Decidable (_\u2261_ {A = Prf Y}))\n -- (Q? : Decidable (_\u2261_ {A = Q}))\n where\n\nRandom-Oracle : \u2605\nRandom-Oracle = Q \u2192 Resp\n\nState : (S A : \u2605) \u2192 \u2605\nState S A = S \u2192 A \u00d7 S\n\ndata Adversary-Query : \u2605 where\n query-H : (q : Q) \u2192 Adversary-Query\n query-create-proof : (w : W)(Y : \u039b) \u2192 Adversary-Query\n\nChallenger-Resp : Adversary-Query \u2192 \u2605\nChallenger-Resp (query-H s) = Resp\nChallenger-Resp (query-create-proof w Y) = Maybe (Prf Y)\n\nAdversary : \u2605 \u2192 \u2605\nAdversary A = Strategy Adversary-Query Challenger-Resp A\n\nTranscript = List (\u03a3 Adversary-Query Challenger-Resp)\n\nPrfs : \u2605\nPrfs = List (\u03a3 \u039b Prf)\n\nRandom-Oracle-List : \u2605\nRandom-Oracle-List = List (Q \u00d7 Resp)\n\nrecord Proof-System (RP : \u2605)(Prf : \u039b \u2192 \u2605) : \u2605 where\n field\n Prove : RP \u2192 (w : W)(Y : \u039b) \u2192 Prf Y\n Verify : (Y : \u039b)(\u03c0 : Prf Y) \u2192 \ud835\udfda\n\nComplete-Proof-System : {RP : \u2605}{Prf : \u039b \u2192 \u2605} \u2192 Proof-System RP Prf \u2192 \u2605\nComplete-Proof-System PS = \u2200 rp w Y \u2192 Verify Y (Prove rp w Y) \u2261 1\u2082\n where open Proof-System PS\n\n {-\nmodule H-def (ro : Random-Oracle)(t : Random-Oracle-List)(q : Q) where\n H : Resp\n H with find (\u03bb { (query-H q' , r) \u2192 \u230a Q? q' q \u230b\n ; (query-create-proof _ _ , _) \u2192 0\u2082 }) t\n ... | just (query-H q\u2081 , r) = r\n ... | _ = ro q\n -}\n\nrecord Simulator {RP}(PF : Proof-System RP Prf) : \u2605 where\n open Proof-System PF\n\n field\n -- Computing the compound patch to the random oracle\n H-patch : RS \u2192 Transcript \u2192 Random-Oracle \u2192 Random-Oracle\n\n -- Simulate does not patch itself but H-patch does\n Simulate : RS \u2192 Transcript \u2192 (Y : \u039b) \u2192 Prf Y\n verify-sim-spec : \u2200 rs t Y \u2192\n let \u03c0 = Simulate rs t Y\n in Verify Y \u03c0 \u2261 1\u2082\n\nmodule Is-Zero-Knowledge\n {RP}\n (PF : Proof-System RP Prf)\n (sim : Simulator PF)\n where\n\n open Simulator sim\n\n module _ (ro : Random-Oracle) where\n\n simulated-Challenger : RS \u2192 Transcript \u2192 \u2200 q \u2192 Challenger-Resp q\n simulated-Challenger rs t (query-H q) = H-patch rs t ro q\n simulated-Challenger rs t (query-create-proof w Y) with L? w Y\n ... | yes p = just (Simulate rs t Y)\n ... | no \u00acp = nothing\n\n real-Challenger : \u2200 q \u2192 Challenger-Resp q\n real-Challenger (query-H q) = ro q\n real-Challenger (query-create-proof w Y) with L? w Y\n ... | yes p = just (L-to-Prf p)\n ... | no _ = nothing\n\n challenger : \ud835\udfda \u2192 RS \u2192 Transcript \u2192 \u2200 q \u2192 Challenger-Resp q\n challenger 0\u2082 _ _ = real-Challenger\n challenger 1\u2082 rs t = simulated-Challenger rs t\n\n open TranscriptRun\n -- \u03b2 = 0\u2082 is real\n -- \u03b2 = 1\u2082 is simulated\n Experiment : \u2200 {A} \u2192 \ud835\udfda \u2192 RS \u2192 Adversary A \u2192 A \u00d7 Transcript\n Experiment \u03b2 rs adv = runT (challenger \u03b2 rs) adv []\n\n{-\n-- there exists a simulator, such that for all adversaries they are clueless if\n-- they are in the real or simulated Experiment\nZero-Knowledge : Proof-System \u2192 \u2605\nZero-Knowledge PF = \u03a3 (Simulator PF) (\u03bb sim \u2192 {!!})\n-}\n\nmodule Sigma-Protocol\n (Commitment Challenge : \u2605)\n (\u03a3-Prf : \u039b \u2192 \u2605)\n {RP R\u03a3P : \u2605}\n (PF : Proof-System RP Prf)\n where\n open Proof-System PF\n\n record \u03a3-Prover : \u2605 where\n field\n get-A : R\u03a3P \u2192 (Y : \u039b) \u2192 Commitment\n get-f : R\u03a3P \u2192 (Y : \u039b) \u2192 (c : Challenge) \u2192 \u03a3-Prf Y\n\n record \u03a3-Transcript (Y : \u039b) : \u2605 where\n constructor mk\n field\n get-A : Commitment\n get-c : Challenge\n get-f : \u03a3-Prf Y\n\n \u03a3-Verifier : \u2605\n \u03a3-Verifier = (Y : \u039b)(t : \u03a3-Transcript Y) \u2192 \ud835\udfda\n\n record \u03a3-Protocol : \u2605 where\n constructor _,_\n field\n \u03a3-verifier : \u03a3-Verifier\n \u03a3-prover : \u03a3-Prover\n open \u03a3-Prover \u03a3-prover public\n\n \u03a3-game : (r : R\u03a3P)(Y : \u039b)(c : Challenge) \u2192 \ud835\udfda\n \u03a3-game r Y c = \u03a3-verifier Y (mk A c f)\n where\n A = get-A r Y\n f = get-f r Y c\n\n _\u21c4_ : \u03a3-Verifier \u2192 \u03a3-Prover \u2192 R\u03a3P \u2192 \u039b \u2192 Challenge \u2192 \ud835\udfda\n (v \u21c4 p) r Y c = \u03a3-Protocol.\u03a3-game (v , p) r Y c\n\n Correct : \u03a3-Protocol \u2192 \u2605\n Correct (v , p) = \u2200 {Y w} \u2192 L w Y \u2192 (r : R\u03a3P)(c : _) \u2192\n let open \u03a3-Prover p\n in v Y (mk (get-A r Y) c (get-f r Y c)) \u2261 1\u2082\n\n module Special-Honest-Verifier-Zero-Knowledge where\n\n record SHVZK (\u03a3-proto : \u03a3-Protocol) : \u2605 where\n open \u03a3-Protocol \u03a3-proto\n field\n Simulate : (Y : \u039b)(c : Challenge)(f : \u03a3-Prf Y) \u2192 Commitment\n Simulate-ok : \u2200 Y c f \u2192 \u03a3-verifier Y (mk (Simulate Y c f) c f) \u2261 1\u2082\n -- If (c,f) uniformly distributed then (Simulate Y c f , c , f) is\n -- distributed equaly to ((\u03a3-verifier \u21c4 \u03a3-prover) r Y c)\n\n module Special-Soundness where\n\n record SpSo : \u2605 where\n field\n Extract : (Y : \u039b)(t\u2080 t\u2081 : \u03a3-Transcript Y) \u2192 W\n Extract-ok : \u2200 Y A c\u2080 c\u2081 f\u2080 f\u2081 \u2192 (c\u2080 \u2262 c\u2081) \u2192 L (Extract Y (mk A c\u2080 f\u2080) (mk A c\u2081 f\u2081)) Y\n\n module Simulation-Sound-Extractability where\n\n Prf-in-Q : \u2200 {Y} \u2192 Prf Y \u2192 \u03a3 Adversary-Query Challenger-Resp \u2192 \ud835\udfda\n Prf-in-Q \u03c0 (query-H _ , _) = 0\u2082\n Prf-in-Q \u03c0 (query-create-proof _ _ , just \u03c0') = Prf? \u03c0 \u03c0'\n Prf-in-Q \u03c0 (query-create-proof _ _ , nothing) = 0\u2082\n\n HistoryForExtractor = List (Prfs \u00d7 Transcript)\n\n ExtractorServerPart =\n (past-history : HistoryForExtractor) {- previous invocations of Adv -}\n (on-going-transcript : Transcript) {- about the current invocation of Adv -}\n \u2192 \u03a0 Adversary-Query Challenger-Resp\n\n Extractor : \u2605\n Extractor = Prfs \u2192 (init-transcript : Transcript)\n \u2192 ExtractorServerPart\n \u00d7 Strategy \ud835\udfd9 (const (Prfs \u00d7 Transcript)) (List W)\n\n valid-witness? : W \u2192 \u039b \u2192 \ud835\udfda\n valid-witness? w Y = \u230a L? w Y \u230b\n\n valid-witnesses? : List W \u2192 List \u039b \u2192 \ud835\udfda\n valid-witnesses? [] [] = 1\u2082\n valid-witnesses? (w \u2237 ws) (prf \u2237 prfs) = valid-witness? w prf \u2227 valid-witnesses? ws prfs\n valid-witnesses? _ _ = 0\u2082\n\n open TranscriptRun\n open StatefulRun\n\n module _ (t : Transcript) where\n Prf-in-Transcript : \u2200 {Y} \u2192 Prf Y \u2192 \ud835\udfda\n Prf-in-Transcript \u03c0 = any (Prf-in-Q \u03c0) t\n\n K-winning-prf : \u03a3 \u039b Prf \u2192 \ud835\udfda\n K-winning-prf (Y , \u03c0) = not (Verify Y \u03c0)\n \u2228 Prf-in-Transcript \u03c0\n\n K-winning-prfs : Prfs \u2192 \ud835\udfda\n K-winning-prfs [] = 1\u2082\n K-winning-prfs prfs = any K-winning-prf prfs\n\n module Game\n (sim : Simulator PF)\n (open Is-Zero-Knowledge PF sim)\n {RA : \u2605}\n\n {- The malicious prover -}\n (Adv : RA \u2192 Adversary Prfs)\n (w : RA)(rs : RS)(ro : Random-Oracle)(K' : Extractor) where\n\n initial-result = Experiment ro 1\u2082 rs (Adv w)\n\n initial-prfs : Prfs\n initial-prfs = fst initial-result\n\n initial-transcript : Transcript\n initial-transcript = snd initial-result\n\n K-winning-intial-run : \ud835\udfda\n K-winning-intial-run = K-winning-prfs initial-transcript initial-prfs\n\n -- Second run\n\n K = K' initial-prfs initial-transcript\n\n Kf = fst K\n Ks = snd K\n\n ws = fst (runT (\u03bb h _ \u2192 runT (\u03bb t q \u2192 Kf (map\u1d38 snd h) t q) (Adv w) []) Ks [])\n\n K-winning-second-run : \ud835\udfda\n K-winning-second-run = valid-witnesses? ws (map\u1d38 fst initial-prfs)\n\n module Fiat-Shamir-Transformation\n (\u03a3-proto : \u03a3-Protocol)\n (shvzk : Special-Honest-Verifier-Zero-Knowledge.SHVZK \u03a3-proto)\n where\n\n open \u03a3-Protocol \u03a3-proto\n open Special-Honest-Verifier-Zero-Knowledge.SHVZK shvzk\n\n sFS : (H : (\u039b \u00d7 Commitment) \u2192 Challenge) \u2192 Proof-System R\u03a3P (\u03bb Y \u2192 Challenge \u00d7 \u03a3-Prf Y)\n sFS H = record { Prove = sFS-Prove ; Verify = sFS-Verify }\n where\n sFS-Prove : R\u03a3P \u2192 W \u2192 (Y : \u039b) \u2192 (Challenge \u00d7 \u03a3-Prf Y)\n sFS-Prove r w Y = let c = H (Y , get-A r Y) in c , get-f r Y c\n sFS-Verify : \u2200 Y \u2192 Challenge \u00d7 \u03a3-Prf Y \u2192 \ud835\udfda\n sFS-Verify Y (c , \u03c0) = \u03a3-verifier Y (mk (Simulate Y c \u03c0) c \u03c0)\n\n -- The weak fiat-shamir is like the strong one but the H function do not get to see\n -- the statement, hence the 'snd'\n wFS : (H : Commitment \u2192 Challenge) \u2192 Proof-System R\u03a3P (\u03bb Y \u2192 Challenge \u00d7 \u03a3-Prf Y)\n wFS H = sFS (H \u2218 snd)\n\n module Theorem1\n (\u03a3-proto : \u03a3-Protocol)\n (shvzk : Special-Honest-Verifier-Zero-Knowledge.SHVZK \u03a3-proto)\n (ssound : Special-Soundness.SpSo)\n where\n open Simulation-Sound-Extractability\n {-\n S : Simulator PF\n S = record { H-patch = {!!}\n ; Simulate = {!!}\n ; verify-sim-spec = {!!} }\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"81d49a84159ca3aee47134efe64ff3235d29314e","subject":"Added doc.","message":"Added doc.\n\nIgnore-this: b0f0381caf8a0f958cf0aadd900b632\n\ndarcs-hash:20120105172321-3bd4e-4cf58940f8066449afbaaf5df946f7aa92362520.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Data\/Nat\/PropertiesByInductionATP.agda","new_file":"src\/FOTC\/Data\/Nat\/PropertiesByInductionATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties (using induction on the FOTC natural numbers type)\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Usually our proofs use pattern matching instead of the induction\n-- principle associated with the FOTC natural numbers. The following\n-- examples show some proofs using it.\n\nmodule FOTC.Data.Nat.PropertiesByInductionATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity n = +-0x n\n\n-- The predicate is not inside the where clause because the\n-- translation of projection-like functions is not implemented.\n+-rightIdentity-P : D \u2192 Set\n+-rightIdentity-P i = i + zero \u2261 i\n{-# ATP definition +-rightIdentity-P #-}\n\n+-rightIdentity : \u2200 {n} \u2192 N n \u2192 n + zero \u2261 n\n+-rightIdentity Nn = indN +-rightIdentity-P P0 is Nn\n where\n postulate P0 : +-rightIdentity-P zero\n {-# ATP prove P0 #-}\n\n postulate is : \u2200 {i} \u2192 +-rightIdentity-P i \u2192 +-rightIdentity-P (succ\u2081 i)\n {-# ATP prove is #-}\n\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {n = n} Nm Nn = indN P P0 is Nm\n where\n P : D \u2192 Set\n P i = N (i + n)\n {-# ATP definition P #-}\n\n postulate P0 : P zero\n {-# ATP prove P0 #-}\n\n postulate is : \u2200 {i} \u2192 P i \u2192 P (succ\u2081 i)\n -- Metis 2.3 (release 20110926): SZS status Unknown (using timeout 180 sec).\n {-# ATP prove is #-}\n\n+-assoc : \u2200 {m n o} \u2192 N m \u2192 N n \u2192 N o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc {n = n} {o} Nm Nn No = indN P P0 is Nm\n where\n P : D \u2192 Set\n P i = i + n + o \u2261 i + (n + o)\n {-# ATP definition P #-}\n\n postulate P0 : P zero\n -- Metis 2.3 (release 20110926): SZS status Unknown (using timeout 180 sec).\n {-# ATP prove P0 #-}\n\n postulate is : \u2200 {i} \u2192 P i \u2192 P (succ\u2081 i)\n -- Metis 2.3 (release 20110926): SZS status Unknown (using timeout 180 sec).\n {-# ATP prove is #-}\n\n-- A proof without use ATPs definitions.\n+-assoc' : \u2200 {m n o} \u2192 N m \u2192 N n \u2192 N o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc' {n = n} {o} Nm Nn No = indN P P0 is Nm\n where\n P : D \u2192 Set\n P i = i + n + o \u2261 i + (n + o)\n\n postulate P0 : zero + n + o \u2261 zero + (n + o)\n {-# ATP prove P0 #-}\n\n postulate\n is : \u2200 {i} \u2192\n i + n + o \u2261 i + (n + o) \u2192 -- IH.\n succ\u2081 i + n + o \u2261 succ\u2081 i + (n + o)\n {-# ATP prove is #-}\n\nx+Sy\u2261S[x+y] : \u2200 {m} n \u2192 N m \u2192 m + succ\u2081 n \u2261 succ\u2081 (m + n)\nx+Sy\u2261S[x+y] n Nm = indN P P0 is Nm\n where\n P : D \u2192 Set\n P i = i + succ\u2081 n \u2261 succ\u2081 (i + n)\n {-# ATP definition P #-}\n\n postulate P0 : P zero\n {-# ATP prove P0 #-}\n\n postulate is : \u2200 {i} \u2192 P i \u2192 P (succ\u2081 i)\n -- Metis 2.3 (release 20110926): SZS status Unknown (using timeout 180 sec).\n {-# ATP prove is #-}\n\n+-comm : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m + n \u2261 n + m\n+-comm {n = n} Nm Nn = indN P P0 is Nm\n where\n P : D \u2192 Set\n P i = i + n \u2261 n + i\n {-# ATP definition P #-}\n\n postulate P0 : P zero\n -- Metis 2.3 (release 20110926): SZS status Unknown (using timeout 180 sec).\n {-# ATP prove P0 +-rightIdentity #-}\n\n postulate is : \u2200 {i} \u2192 P i \u2192 P (succ\u2081 i)\n -- Metis 2.3 (release 20110926): SZS status Unknown (using timeout 180 sec).\n {-# ATP prove is x+Sy\u2261S[x+y] #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties (using induction on the FOTC natural numbers type)\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Usually our proofs use pattern matching instead of the induction\n-- principle associated with the FOTC natural numbers. The following\n-- examples show some proofs using it.\n\n-- TODO: We have not tested which ATPs fail on the ATP conjectures.\n\nmodule FOTC.Data.Nat.PropertiesByInductionATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity n = +-0x n\n\n-- The predicate is not inside the where clause because the\n-- translation of projection-like functions is not implemented.\n+-rightIdentity-P : D \u2192 Set\n+-rightIdentity-P i = i + zero \u2261 i\n{-# ATP definition +-rightIdentity-P #-}\n\n+-rightIdentity : \u2200 {n} \u2192 N n \u2192 n + zero \u2261 n\n+-rightIdentity Nn = indN +-rightIdentity-P P0 is Nn\n where\n postulate P0 : +-rightIdentity-P zero\n {-# ATP prove P0 #-}\n\n postulate is : \u2200 {i} \u2192 +-rightIdentity-P i \u2192 +-rightIdentity-P (succ\u2081 i)\n {-# ATP prove is #-}\n\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {n = n} Nm Nn = indN P P0 is Nm\n where\n P : D \u2192 Set\n P i = N (i + n)\n {-# ATP definition P #-}\n\n postulate P0 : P zero\n {-# ATP prove P0 #-}\n\n postulate is : \u2200 {i} \u2192 P i \u2192 P (succ\u2081 i)\n {-# ATP prove is #-}\n\n+-assoc : \u2200 {m n o} \u2192 N m \u2192 N n \u2192 N o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc {n = n} {o} Nm Nn No = indN P P0 is Nm\n where\n P : D \u2192 Set\n P i = i + n + o \u2261 i + (n + o)\n {-# ATP definition P #-}\n\n postulate P0 : P zero\n {-# ATP prove P0 #-}\n\n postulate is : \u2200 {i} \u2192 P i \u2192 P (succ\u2081 i)\n {-# ATP prove is #-}\n\n-- A proof without use ATPs definitions.\n+-assoc' : \u2200 {m n o} \u2192 N m \u2192 N n \u2192 N o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc' {n = n} {o} Nm Nn No = indN P P0 is Nm\n where\n P : D \u2192 Set\n P i = i + n + o \u2261 i + (n + o)\n\n postulate P0 : zero + n + o \u2261 zero + (n + o)\n {-# ATP prove P0 #-}\n\n postulate\n is : \u2200 {i} \u2192\n i + n + o \u2261 i + (n + o) \u2192 -- IH.\n succ\u2081 i + n + o \u2261 succ\u2081 i + (n + o)\n {-# ATP prove is #-}\n\nx+Sy\u2261S[x+y] : \u2200 {m} n \u2192 N m \u2192 m + succ\u2081 n \u2261 succ\u2081 (m + n)\nx+Sy\u2261S[x+y] n Nm = indN P P0 is Nm\n where\n P : D \u2192 Set\n P i = i + succ\u2081 n \u2261 succ\u2081 (i + n)\n {-# ATP definition P #-}\n\n postulate P0 : P zero\n {-# ATP prove P0 #-}\n\n postulate is : \u2200 {i} \u2192 P i \u2192 P (succ\u2081 i)\n {-# ATP prove is #-}\n\n+-comm : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m + n \u2261 n + m\n+-comm {n = n} Nm Nn = indN P P0 is Nm\n where\n P : D \u2192 Set\n P i = i + n \u2261 n + i\n {-# ATP definition P #-}\n\n postulate P0 : P zero\n {-# ATP prove P0 +-rightIdentity #-}\n\n postulate is : \u2200 {i} \u2192 P i \u2192 P (succ\u2081 i)\n {-# ATP prove is x+Sy\u2261S[x+y] #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"655a42bd4923f174009da823127bf76a5c29ee83","subject":"flipbased-tree: probability results for return, toss, map, zip, weaken","message":"flipbased-tree: probability results for return, toss, map, zip, weaken\n","repos":"crypto-agda\/crypto-agda","old_file":"flipbased-tree.agda","new_file":"flipbased-tree.agda","new_contents":"module flipbased-tree where\n\nopen import Function\nopen import Data.Bits\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\nopen import bintree\nopen import Data.Product\n\nimport flipbased\n\n-- \u201c\u21ba n A\u201d reads like: \u201ctoss n coins and then return a value of type A\u201d\n\u21ba : \u2200 {a} n (A : Set a) \u2192 Set a\n\u21ba = flip Tree\n\nreturn\u21ba : \u2200 {c a} {A : Set a} \u2192 A \u2192 \u21ba c A\nreturn\u21ba = leaf\n\nrunDet : \u2200 {a} {A : Set a} \u2192 \u21ba 0 A \u2192 A\nrunDet (leaf x) = x\n\ntoss : \u21ba 1 Bit\ntoss = fork (leaf 0b) (leaf 1b)\n\nweaken\u2264 : \u2200 {m c a} {A : Set a} \u2192 m \u2264 c \u2192 \u21ba m A \u2192 \u21ba c A\nweaken\u2264 p (leaf x) = leaf x\nweaken\u2264 (s\u2264s p) (fork left right) = fork (weaken\u2264 p left) (weaken\u2264 p right)\n\nm\u2264n+m : \u2200 m n \u2192 m \u2264 n + m\nm\u2264n+m m n = \u2115\u2264.trans (m\u2264m+n m n) (\u2115\u2264.reflexive (\u2115\u00b0.+-comm m n))\n\nweaken+ : \u2200 c {m a} {A : Set a} \u2192 \u21ba m A \u2192 \u21ba (c + m) A\nweaken+ c = weaken\u2264 (m\u2264n+m _ c)\n\nmap\u21ba : \u2200 {c a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 \u21ba c A \u2192 \u21ba c B\nmap\u21ba f (leaf x) = leaf (f x)\nmap\u21ba f (fork left right) = fork (map\u21ba f left) (map\u21ba f right)\n\njoin\u21ba : \u2200 {c\u2081 c\u2082 a} {A : Set a} \u2192 \u21ba c\u2081 (\u21ba c\u2082 A) \u2192 \u21ba (c\u2081 + c\u2082) A\njoin\u21ba {c} (leaf x) = weaken+ c x\njoin\u21ba (fork left right) = fork (join\u21ba left) (join\u21ba right)\n\nopen flipbased \u21ba toss weaken\u2264 leaf map\u21ba join\u21ba public\n\nopen import Data.Bool\nopen import Data.Bits\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\n\ninfix 4 _\/2+_\/2\ninfix 6 _\/2\npostulate\n [0,1] : Set\n 0\/1 : [0,1]\n 1\/1 : [0,1]\n _\/2 : [0,1] \u2192 [0,1]\n _\/2+_\/2 : [0,1] \u2192 [0,1] \u2192 [0,1]\n _*\/_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n-- sym _\/2+_\/2\n-- 1 \/2+ 1 \/2 = 1\/1\n-- p \/2+ p \/2 = p\n-- p \/2+ (1- p) \/2 = 1\/2\n\n -- \u00b7\/1+_ : \u2115 \u2192 Set\n -- \/+\/ : \u2115 \u2192 [0,1] \u2192 [0,1] \u2192 [0,1]\n\n 0\/2+p\/2\u2261p\/2 : \u2200 p \u2192 (0\/1 \/2+ p \/2) \u2261 p \/2\n _\/2+_\/2-comm : \u2200 x y \u2192 x \/2+ y \/2 \u2261 y \/2+ x \/2\n 1*q\u2261q : \u2200 q \u2192 1\/1 *\/ q \u2261 q\n 0*q\u2261q : \u2200 q \u2192 0\/1 *\/ q \u2261 0\/1\n distr-*-\/2+\/2 : \u2200 x y z \u2192 (x *\/ z) \/2+ (y *\/ z) \/2 \u2261 (x \/2+ y \/2) *\/ z\n distr-\/2-* : \u2200 p q \u2192 (p \/2) *\/ (q \/2) \u2261 ((p *\/ q) \/2) \/2\n 0\/2\u22610 : 0\/1 \/2 \u2261 0\/1\n\n1\/2^_ : \u2115 \u2192 [0,1]\n1\/2^ zero = 1\/1\n1\/2^ suc n = (1\/2^ n)\/2\n\n1\/2 : [0,1]\n1\/2 = 1\/2^ 1\n1\/4 : [0,1]\n1\/4 = 1\/2^ 2\n\ndata Pr[return\u21ba_\u2261_]\u2261_ {a} {A : Set a} (x y : A) : [0,1] \u2192 Set a where\n Pr-\u2261 : x \u2261 y \u2192 Pr[return\u21ba x \u2261 y ]\u2261 1\/1\n Pr-\u2262 : x \u2262 y \u2192 Pr[return\u21ba x \u2261 y ]\u2261 0\/1\n\ninfix 2 Pr[_\u2261_]\u2261_\ndata Pr[_\u2261_]\u2261_ {a} {A : Set a} : \u2200 {c} \u2192 \u21ba c A \u2192 A \u2192 [0,1] \u2192 Set a where\n Pr-return : \u2200 {c x y pr} (pf : Pr[return\u21ba x \u2261 y ]\u2261 pr) \u2192 Pr[ return\u21ba {c = c} x \u2261 y ]\u2261 pr\n\n Pr-fork : \u2200 {c} {left right : \u21ba c A} {x p q r}\n (eq : p \/2+ q \/2 \u2261 r)\n (pf\u2080 : Pr[ left \u2261 x ]\u2261 p)\n (pf\u2081 : Pr[ right \u2261 x ]\u2261 q)\n \u2192 Pr[ fork left right \u2261 x ]\u2261 r\n\nPr-fork\u2032 : \u2200 {c a} {A : Set a} {left right : \u21ba c A} {x p q}\n \u2192 Pr[ left \u2261 x ]\u2261 p\n \u2192 Pr[ right \u2261 x ]\u2261 q\n \u2192 Pr[ fork left right \u2261 x ]\u2261 (p \/2+ q \/2)\nPr-fork\u2032 = Pr-fork refl\n\nPr-return-\u2261 : \u2200 {c a} {A : Set a} {x : A} \u2192 Pr[ return\u21ba {c = c} x \u2261 x ]\u2261 1\/1\nPr-return-\u2261 = Pr-return (Pr-\u2261 refl)\n\nPr-return-\u2262 : \u2200 {c a} {A : Set a} {x y : A} \u2192 x \u2262 y \u2192 Pr[ return\u21ba {c = c} x \u2261 y ]\u2261 0\/1\nPr-return-\u2262 = Pr-return \u2218 Pr-\u2262\n\nimport Function.Equality as F\u2261\nimport Function.Equivalence as F\u2248\n\n_\u2248\u21d2_ : \u2200 {c a} {A : Set a} (p\u2081 p\u2082 : \u21ba c A) \u2192 Set a\np\u2081 \u2248\u21d2 p\u2082 = \u2200 {x pr} \u2192 Pr[ p\u2081 \u2261 x ]\u2261 pr \u2192 Pr[ p\u2082 \u2261 x ]\u2261 pr\n\n_\u2248_ : \u2200 {c a} {A : Set a} (p\u2081 p\u2082 : \u21ba c A) \u2192 Set a\np\u2081 \u2248 p\u2082 = \u2200 {x pr} \u2192 (Pr[ p\u2081 \u2261 x ]\u2261 pr) F\u2248.\u21d4 (Pr[ p\u2082 \u2261 x ]\u2261 pr)\n\n\u2248-refl : \u2200 {c a} {A : Set a} \u2192 Reflexive {A = \u21ba c A} _\u2248_\n\u2248-refl = F\u2248.id\n\n\u2248-sym : \u2200 {c a} {A : Set a} {p\u2081 p\u2082 : \u21ba c A} \u2192 p\u2081 \u2248 p\u2082 \u2192 p\u2082 \u2248 p\u2081\n\u2248-sym \u03b7 = F\u2248.sym \u03b7\n\n\u2248-trans : \u2200 {c a} {A : Set a} \u2192 Transitive {A = \u21ba c A} _\u2248_\n\u2248-trans f g = g F\u2248.\u2218 f\n\nfork-sym\u21d2 : \u2200 {c a} {A : Set a} {p\u2081 p\u2082 : \u21ba c A} \u2192 fork p\u2081 p\u2082 \u2248\u21d2 fork p\u2082 p\u2081\nfork-sym\u21d2 (Pr-fork {p = p} {q} refl pf\u2081 pf\u2080) rewrite p \/2+ q \/2-comm = Pr-fork\u2032 pf\u2080 pf\u2081\n\nfork-sym : \u2200 {c a} {A : Set a} {p\u2081 p\u2082 : \u21ba c A} \u2192 fork p\u2081 p\u2082 \u2248 fork p\u2082 p\u2081\nfork-sym = F\u2248.equivalence fork-sym\u21d2 fork-sym\u21d2\n\nPr-fork-0 : \u2200 {c a} {A : Set a} {left right : \u21ba c A} {x : A} {p}\n \u2192 Pr[ left \u2261 x ]\u2261 0\/1\n \u2192 Pr[ right \u2261 x ]\u2261 p\n \u2192 Pr[ fork left right \u2261 x ]\u2261 p \/2\nPr-fork-0 {p = p} eq\u2081 eq\u2082 rewrite sym (0\/2+p\/2\u2261p\/2 p) = Pr-fork\u2032 eq\u2081 eq\u2082\n\nex\u2081 : \u2200 x \u2192 Pr[ toss \u2261 x ]\u2261 1\/2\nex\u2081 1b = Pr-fork-0 (Pr-return-\u2262 (\u03bb ())) Pr-return-\u2261\nex\u2081 0b = F\u2248.Equivalence.to fork-sym F\u2261.\u27e8$\u27e9 (Pr-fork-0 (Pr-return-\u2262 (\u03bb ())) Pr-return-\u2261)\n\nPr-map : \u2200 {c a b} {A : Set a} {B : Set b} {Alg : \u21ba c A} {x pr} {f : A \u2192 B} \u2192\n (f-inj : \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y) \u2192\n Pr[ Alg \u2261 x ]\u2261 pr \u2192\n Pr[ \u27ea f \u00b7 Alg \u27eb \u2261 f x ]\u2261 pr\nPr-map f-inj (Pr-return (Pr-\u2261 refl)) = Pr-return (Pr-\u2261 refl)\nPr-map f-inj (Pr-return (Pr-\u2262 x\u2262y)) = Pr-return (Pr-\u2262 (x\u2262y \u2218 f-inj))\nPr-map f-inj (Pr-fork eq pf\u2080 pf\u2081) = Pr-fork eq (Pr-map f-inj pf\u2080) (Pr-map f-inj pf\u2081)\n\nPr-same : \u2200 {c a} {A : Set a} {Alg : \u21ba c A} {x pr\u2080 pr\u2081} \u2192\n pr\u2080 \u2261 pr\u2081 \u2192\n Pr[ Alg \u2261 x ]\u2261 pr\u2080 \u2192\n Pr[ Alg \u2261 x ]\u2261 pr\u2081\nPr-same refl = id\n\nPr-weaken\u2264 : \u2200 {c\u2080 c\u2081 a} {A : Set a} {Alg : \u21ba c\u2080 A} {x pr} \u2192\n (c\u2080\u2264c\u2081 : c\u2080 \u2264 c\u2081) \u2192\n Pr[ Alg \u2261 x ]\u2261 pr \u2192\n Pr[ weaken\u2264 c\u2080\u2264c\u2081 Alg \u2261 x ]\u2261 pr\nPr-weaken\u2264 p (Pr-return pf) = Pr-return pf\nPr-weaken\u2264 (s\u2264s c\u2080\u2264c\u2081) (Pr-fork eq pf\u2080 pf\u2081)\n = Pr-fork eq (Pr-weaken\u2264 c\u2080\u2264c\u2081 pf\u2080) (Pr-weaken\u2264 c\u2080\u2264c\u2081 pf\u2081)\n\nPr-weaken+ : \u2200 {c\u2080} c\u2081 {a} {A : Set a} {Alg : \u21ba c\u2080 A} {x pr} \u2192\n Pr[ Alg \u2261 x ]\u2261 pr \u2192\n Pr[ weaken+ c\u2081 Alg \u2261 x ]\u2261 pr\nPr-weaken+ c\u2081 = Pr-weaken\u2264 (m\u2264n+m _ c\u2081)\n\nPr-map-0 : \u2200 {c a b} {A : Set a} {B : Set b} {Alg : \u21ba c A} {f : A \u2192 B} {x} \u2192 (\u2200 y \u2192 f y \u2262 x)\n \u2192 Pr[ map\u21ba f Alg \u2261 x ]\u2261 0\/1\nPr-map-0 {Alg = leaf x} f-prop = Pr-return (Pr-\u2262 (f-prop x))\nPr-map-0 {Alg = fork Alg Alg\u2081} f-prop = Pr-fork (trans (0\/2+p\/2\u2261p\/2 0\/1) 0\/2\u22610)\n (Pr-map-0 f-prop) (Pr-map-0 f-prop)\n\nPr-zip : \u2200 {c\u2080 c\u2081 a b} {A : Set a} {B : Set b} {Alg\u2080 : \u21ba c\u2080 A} {Alg\u2081 : \u21ba c\u2081 B} {x y pr\u2081 pr\u2082} \u2192\n Pr[ Alg\u2080 \u2261 x ]\u2261 pr\u2081 \u2192\n Pr[ Alg\u2081 \u2261 y ]\u2261 pr\u2082 \u2192\n Pr[ zip\u21ba Alg\u2080 Alg\u2081 \u2261 (x , y) ]\u2261 (pr\u2081 *\/ pr\u2082)\nPr-zip {c\u2080} {pr\u2082 = pr\u2082} (Pr-return {x = x} (Pr-\u2261 refl)) pf\u2082\n rewrite 1*q\u2261q pr\u2082 = Pr-weaken+ c\u2080 (Pr-map {f = _,_ x} (cong proj\u2082) pf\u2082)\nPr-zip {c\u2080} {pr\u2082 = pr\u2082} (Pr-return {x = x} (Pr-\u2262 pf)) pf\u2082\n rewrite 0*q\u2261q pr\u2082 = Pr-weaken+ c\u2080 (Pr-map-0 (\u03bb y x\u2081 \u2192 pf (cong proj\u2081 x\u2081)))\nPr-zip (Pr-fork refl pf\u2081 pf\u2082) pf\u2083 = Pr-fork (distr-*-\/2+\/2 _ _ _) (Pr-zip pf\u2081 pf\u2083) (Pr-zip pf\u2082 pf\u2083)\n\nex\u2082 : \u2200 x y \u2192 Pr[ toss \u27e8,\u27e9 toss \u2261 (x , y) ]\u2261 1\/4\nex\u2082 x y = Pr-same (trans (distr-\/2-* _ _) (cong (_\/2 \u2218 _\/2) (1*q\u2261q _)))\n (Pr-zip {Alg\u2080 = toss} {Alg\u2081 = toss} (ex\u2081 x) (ex\u2081 y))\n\npostulate\n ex\u2083 : \u2200 {n} (x : Bits n) \u2192 Pr[ random \u2261 x ]\u2261 1\/2^ n\n","old_contents":"module flipbased-tree where\n\nopen import Function\nopen import Data.Bits\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\nopen import Data.Vec\n\nimport flipbased\n\ndata Tree {a} (A : Set a) : \u2115 \u2192 Set a\n\n\u21ba : \u2200 {a} n (A : Set a) \u2192 Set a\n\u21ba = flip Tree\n\n-- \u201c\u21ba n A\u201d reads like: \u201ctoss n coins and then return a value of type A\u201d\ndata Tree {a} (A : Set a) where\n return\u21ba : \u2200 {c} \u2192 A \u2192 \u21ba c A\n fork : \u2200 {c} \u2192 (left right : \u21ba c A) \u2192 \u21ba (suc c) A\n\nrunDet : \u2200 {a} {A : Set a} \u2192 \u21ba 0 A \u2192 A\nrunDet (return\u21ba x) = x\n\ntoss : \u21ba 1 Bit\ntoss = fork (return\u21ba 0b) (return\u21ba 1b)\n\nmap\u21ba : \u2200 {c a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 \u21ba c A \u2192 \u21ba c B\nmap\u21ba f (return\u21ba x) = return\u21ba (f x)\nmap\u21ba f (fork left right) = fork (map\u21ba f left) (map\u21ba f right)\n\nweaken\u2264 : \u2200 {m c a} {A : Set a} \u2192 m \u2264 c \u2192 \u21ba m A \u2192 \u21ba c A\nweaken\u2264 p (return\u21ba x) = return\u21ba x\nweaken\u2264 (s\u2264s p) (fork left right) = fork (weaken\u2264 p left) (weaken\u2264 p right)\n\nweaken+ : \u2200 {m c a} {A : Set a} \u2192 \u21ba m A \u2192 \u21ba (c + m) A\nweaken+ {m} {c} = weaken\u2264 (\u2115\u2264.trans (m\u2264m+n m c) (\u2115\u2264.reflexive (\u2115\u00b0.+-comm m c)))\n\njoin\u21ba : \u2200 {c\u2081 c\u2082 a} {A : Set a} \u2192 \u21ba c\u2081 (\u21ba c\u2082 A) \u2192 \u21ba (c\u2081 + c\u2082) A\njoin\u21ba {c} (return\u21ba x) = weaken+ {_} {c} x\njoin\u21ba (fork left right) = fork (join\u21ba left) (join\u21ba right)\n\nopen flipbased \u21ba toss weaken\u2264 return\u21ba map\u21ba join\u21ba public\n\nopen import Data.Bool\nopen import Data.Bits\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\n\ninfix 4 _\/2+_\/2\ninfix 6 _\/2\npostulate\n [0,1] : Set\n 0\/1 : [0,1]\n 1\/1 : [0,1]\n _\/2 : [0,1] \u2192 [0,1]\n _\/2+_\/2 : [0,1] \u2192 [0,1] \u2192 [0,1]\n-- sym _\/2+_\/2\n-- 1 \/2+ 1 \/2 = 1\/1\n-- p \/2+ p \/2 = p\n-- p \/2+ (1- p) \/2 = 1\/2\n\n1\/2^_ : \u2115 \u2192 [0,1]\n1\/2^ zero = 1\/1\n1\/2^ suc n = (1\/2^ n)\/2\n\n1\/2 : [0,1]\n1\/2 = 1\/2^ 1\n1\/4 : [0,1]\n1\/4 = 1\/2^ 2\n\npostulate\n Pr[_\u2261_] : \u2200 {c a} {A : Set a} \u2192 \u21ba c A \u2192 A \u2192 [0,1]\n\n_\u2248_ : \u2200 {c a} {A : Set a} (p\u2081 p\u2082 : \u21ba c A) \u2192 Set a\np\u2081 \u2248 p\u2082 = \u2200 x \u2192 Pr[ p\u2081 \u2261 x ] \u2261 Pr[ p\u2082 \u2261 x ]\n\npostulate\n fork-sym : \u2200 {c a} {A : Set a} (p\u2081 p\u2082 : \u21ba c A) \u2192 fork p\u2081 p\u2082 \u2248 fork p\u2082 p\u2081\n\n Pr-return-\u2261 : \u2200 {c a} {A : Set a} (x : A) \u2192 Pr[ return\u21ba {c = c} x \u2261 x ] \u2261 1\/1\n\n Pr-return-\u2262 : \u2200 {c a} {A : Set a} {x y : A} \u2192 x \u2262 y \u2192 Pr[ return\u21ba {c = c} x \u2261 y ] \u2261 0\/1\n\n Pr-fork : \u2200 {c a} {A : Set a} {left right : \u21ba c A} {x : A} {p q}\n \u2192 Pr[ left \u2261 x ] \u2261 p\n \u2192 Pr[ right \u2261 x ] \u2261 q\n \u2192 Pr[ fork left right \u2261 x ] \u2261 p \/2+ q \/2\n\n 0\/2+p\/2\u2261p\/2 : \u2200 p \u2192 (0\/1 \/2+ p \/2) \u2261 p \/2\n\nPr-fork-0 : \u2200 {c a} {A : Set a} {left right : \u21ba c A} {x : A} {p}\n \u2192 Pr[ left \u2261 x ] \u2261 0\/1\n \u2192 Pr[ right \u2261 x ] \u2261 p\n \u2192 Pr[ fork left right \u2261 x ] \u2261 p \/2\nPr-fork-0 {p = p} eq\u2081 eq\u2082 rewrite sym (0\/2+p\/2\u2261p\/2 p) = Pr-fork eq\u2081 eq\u2082\n\nex\u2081 : \u2200 x \u2192 Pr[ toss \u2261 x ] \u2261 1\/2\nex\u2081 1b = Pr-fork-0 (Pr-return-\u2262 (\u03bb ())) (Pr-return-\u2261 1b)\nex\u2081 0b rewrite fork-sym {0} (return\u21ba 0b) (return\u21ba 1b) 0b = Pr-fork-0 (Pr-return-\u2262 (\u03bb ())) (Pr-return-\u2261 0b)\n\npostulate\n ex\u2082 : \u2200 x y \u2192 Pr[ toss \u27e8,\u27e9 toss \u2261 (x , y) ] \u2261 1\/2^ 2\n\n ex\u2083 : \u2200 {n} (x : Bits n) \u2192 Pr[ random \u2261 x ] \u2261 1\/2^ n\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"a1713e8fd0cf292f0524a71143e066f20b571f3a","subject":"Nat: avoid a name conflict (temporary)","message":"Nat: avoid a name conflict (temporary)\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Nat\/NP.agda","new_file":"lib\/Data\/Nat\/NP.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Nat.NP where\n\nimport Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat public hiding (module GeneralisedArithmetic; module \u2264-Reasoning; fold)\nopen import Data.Nat.Properties as Props\nopen import Data.Nat.Logical\nopen import Data.Bool.NP hiding (_==_; module ==)\nopen import Data.Product using (proj\u2081; proj\u2082; \u2203; _,_)\nopen import Data.Sum renaming (map to \u228e-map)\nopen import Data.Empty using (\u22a5-elim; \u22a5)\nopen import Function.NP\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_; _\u2257_; module \u2261-Reasoning)\n\nmodule \u2115\u00b0 = Algebra.CommutativeSemiring Props.commutativeSemiring\nmodule \u2115cmp = StrictTotalOrder Props.strictTotalOrder\nmodule \u2115\u2264 = DecTotalOrder decTotalOrder\nmodule \u2294\u00b0 = Algebra.CommutativeSemiringWithoutOne \u2294-\u2293-0-commutativeSemiringWithoutOne\n\nmodule \u2264-Reasoning where\n open Preorder-Reasoning \u2115\u2264.preorder public renaming (_\u223c\u27e8_\u27e9_ to _\u2264\u27e8_\u27e9_)\n infixr 2 _\u2261\u27e8_\u27e9_\n _\u2261\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x \u2261 y \u2192 y \u2264 z \u2192 x \u2264 z\n _ \u2261\u27e8 \u2261.refl \u27e9 p = p\n infixr 2 _<\u27e8_\u27e9_\n _<\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x < y \u2192 y \u2264 z \u2192 x < z\n x <\u27e8 p \u27e9 q = suc x \u2264\u27e8 p \u27e9 q\n\nsuc-injective : \u2200 {n m : \u2115} \u2192 (suc n \u2236 \u2115) \u2261 suc m \u2192 n \u2261 m\nsuc-injective = \u2261.cong pred\n\n+-\u2264-inj : \u2200 x {y z} \u2192 x + y \u2264 x + z \u2192 y \u2264 z\n+-\u2264-inj zero p = p\n+-\u2264-inj (suc x) (s\u2264s p) = +-\u2264-inj x p\n\nsucx\u2270x : \u2200 x \u2192 suc x \u2270 x\nsucx\u2270x zero = \u03bb()\nsucx\u2270x (suc x) = sucx\u2270x x \u2218 \u2264-pred\n\n\u00acn\u2264x dist (x * y) (x * z) == x * dist y z))\n\ninfix 8 _^_\n_^_ : \u2115 \u2192 \u2115 \u2192 \u2115\nb ^ zero = 1\nb ^ suc n = b * b ^ n\n\n2^_ : \u2115 \u2192 \u2115\n2^ n = \u27e82^ n * 1 \u27e9\n\n2^-spec : \u2200 n \u2192 2^ n \u2261 2 ^ n\n2^-spec zero = \u2261.refl\n2^-spec (suc n) rewrite 2^-spec n | 2*-spec (2 ^ n) = \u2261.refl\n\n2^*-spec : \u2200 m n \u2192 2^\u27e8 m \u27e9* n \u2261 2 ^ m * n\n2^*-spec zero n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\n2^*-spec (suc m) n rewrite 2^*-spec m n\n | \u2115\u00b0.*-assoc 2 (2 ^ m) n\n | \u2115\u00b0.+-comm (2 ^ m * n) 0 = \u2261.refl\n\n1\u22642^ : \u2200 n \u2192 2^ n \u2265 1\n1+\u22642^ : \u2200 n \u2192 2^ n \u2265 1 + n\n1+\u22642^ zero = s\u2264s z\u2264n\n1+\u22642^ (suc n) = (1\u22642^ n) +-mono (1+\u22642^ n)\n\n1\u22642^ n = \u2115\u2264.trans (s\u2264s z\u2264n) (1+\u22642^ n)\n\n\u2264-steps\u2032 : \u2200 {x} y \u2192 x \u2264 x + y\n\u2264-steps\u2032 {x} y rewrite \u2115\u00b0.+-comm x y = \u2264-steps y \u2115\u2264.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Hyper_operator\n_\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\na \u2191\u27e8 suc n \u27e9 (suc b) = a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\na \u2191\u27e8 0 \u27e9 b = suc b\na \u2191\u27e8 1 \u27e9 0 = a\na \u2191\u27e8 2 \u27e9 0 = 0\na \u2191\u27e8 suc (suc (suc n)) \u27e9 0 = 1\n\nmodule \u2191-Props where\n \u2191-fold : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 b \u2261 fold (a \u2191\u27e8 suc n \u27e9 0) (_\u2191\u27e8_\u27e9_ a n) b\n \u2191-fold a n zero = \u2261.refl\n \u2191-fold a n (suc b) = \u2261.cong (_\u2191\u27e8_\u27e9_ a n) (\u2191-fold a n b)\n\n \u2191\u2080-+ : \u2200 a b \u2192 a \u2191\u27e8 0 \u27e9 b \u2261 1 + b\n \u2191\u2080-+ a b = \u2261.refl\n\n \u2191\u2081-+ : \u2200 a b \u2192 a \u2191\u27e8 1 \u27e9 b \u2261 b + a\n \u2191\u2081-+ a zero = \u2261.refl\n \u2191\u2081-+ a (suc b) = \u2261.cong suc (\u2191\u2081-+ a b)\n\n \u2191\u2082-* : \u2200 a b \u2192 a \u2191\u27e8 2 \u27e9 b \u2261 b * a\n \u2191\u2082-* a zero = \u2261.refl\n \u2191\u2082-* a (suc b) rewrite \u2191\u2082-* a b\n | \u2191\u2081-+ a (b * a)\n | \u2115\u00b0.+-comm (b * a) a\n = \u2261.refl\n\n -- fold 1 (_*_ a) b \u2261 a ^ b\n \u2191\u2083-^ : \u2200 a b \u2192 a \u2191\u27e8 3 \u27e9 b \u2261 a ^ b\n \u2191\u2083-^ a zero = \u2261.refl\n \u2191\u2083-^ a (suc b) rewrite \u2191\u2083-^ a b\n | \u2191\u2082-* a (a ^ b)\n | \u2115\u00b0.*-comm (a ^ b) a\n = \u2261.refl\n\n _\u2191\u27e8_\u27e90 : \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u27e8 0 \u27e90 = 1\n a \u2191\u27e8 1 \u27e90 = a\n a \u2191\u27e8 2 \u27e90 = 0\n a \u2191\u27e8 suc (suc (suc n)) \u27e90 = 1\n\n _`\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _`\u2191\u27e8_\u27e9_ a 0 = suc\n _`\u2191\u27e8_\u27e9_ a (suc n) = fold (a \u2191\u27e8 suc n \u27e90) (_`\u2191\u27e8_\u27e9_ a n)\n\n \u2191-ind-rule : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 (suc b) \u2261 a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\n \u2191-ind-rule a n b = \u2261.refl\n\n _\u2191\u2032\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u2032\u27e8 0 \u27e9 b = suc b\n a \u2191\u2032\u27e8 1 \u27e9 b = a + b\n a \u2191\u2032\u27e8 2 \u27e9 b = a * b\n a \u2191\u2032\u27e8 3 \u27e9 b = a ^ b\n a \u2191\u2032\u27e8 suc (suc (suc (suc n))) \u27e9 b = a \u2191\u27e8 4 + n \u27e9 b\n\n \u2191-\u2191\u2032 : \u2200 a n b \u2192 a \u2191\u27e8 n \u27e9 b \u2261 a \u2191\u2032\u27e8 n \u27e9 b\n \u2191-\u2191\u2032 a 0 b = \u2261.refl\n \u2191-\u2191\u2032 a 1 b = \u2261.trans (\u2191\u2081-+ a b) (\u2115\u00b0.+-comm b a)\n \u2191-\u2191\u2032 a 2 b = \u2261.trans (\u2191\u2082-* a b) (\u2115\u00b0.*-comm b a)\n \u2191-\u2191\u2032 a 3 b = \u2191\u2083-^ a b\n \u2191-\u2191\u2032 a (suc (suc (suc (suc _)))) b = \u2261.refl\n\n _\u21912+\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _\u21912+\u27e8_\u27e9_ a = fold (_*_ a) (fold 1)\n\nmodule InflModule where\n -- Inflationary functions\n Infl< : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl< f = \u2200 {x} \u2192 x < f x\n\n Infl : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl f = Infl< (suc \u2218 f)\n\n InflT< : (f : Endo (Endo \u2115)) \u2192 Set\n InflT< f = \u2200 {g} \u2192 Infl< g \u2192 Infl< (f g)\n\n{-\n\u2200 {x} \u2192 x \u2264 f x\n\n\n 1 + x \u2264 1 + f x\n x < 1 + f x\n x < (suc \u2218 f) x\n Infl< (suc \u2218 f)\n\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n\nInfl< f \u2192 Infl f\nInfl< f \u2192 Infl< (suc \u2218 f)\n-}\n\n id-infl : Infl id\n id-infl = \u2115\u2264.refl\n\n \u2218-infl< : \u2200 {f g} \u2192 Infl< f \u2192 Infl< g \u2192 Infl< (f \u2218 g)\n \u2218-infl< inflf< inflg< = <-trans inflg< inflf<\n\n suc-infl< : Infl< suc\n suc-infl< = \u2115\u2264.refl\n\n suc-infl : Infl suc\n suc-infl = s\u2264s (\u2264-step \u2115\u2264.refl)\n\n nest-infl : \u2200 f (finfl : Infl f) n \u2192 Infl (nest n f)\n nest-infl f _ zero = \u2115\u2264.refl\n nest-infl f finfl (suc n) = \u2115\u2264.trans (nest-infl f finfl n) finfl\n\n nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n nest-infl< f finfl< zero = finfl<\n nest-infl< f finfl< (suc n) = <-trans (nest-infl< f finfl< n) finfl< \n\n-- See Function.NP.nest-Properties for more properties\nmodule fold-Props where\n\n fold-suc : \u2200 m n \u2192 fold m suc n \u2261 n + m\n fold-suc m zero = \u2261.refl\n fold-suc m (suc n) rewrite fold-suc m n = \u2261.refl\n\n fold-+ : \u2200 m n \u2192 fold 0 (_+_ m) n \u2261 n * m\n fold-+ m zero = \u2261.refl\n fold-+ m (suc n) rewrite fold-+ m n = \u2261.refl\n\n fold-* : \u2200 m n \u2192 fold 1 (_*_ m) n \u2261 m ^ n\n fold-* m zero = \u2261.refl\n fold-* m (suc n) rewrite fold-* m n = \u2261.refl\n\n{- TODO\n fold-^ : \u2200 m n \u2192 fold 1 (flip _^_ m) n \u2261 m \u2191\u27e8 4 \u27e9 n\n fold-^ m zero = \u2261.refl\n fold-^ m (suc n) rewrite fold-^ m n = ?\n-}\n\n cong-fold : \u2200 {A : Set} {f g : Endo A} (f\u2257g : f \u2257 g) {z} \u2192 fold z f \u2257 fold z g\n cong-fold eq zero = \u2261.refl\n cong-fold eq {z} (suc x) rewrite cong-fold eq {z} x = eq _\n\n private\n open \u2191-Props\n module Alt\u21912 where\n alt : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n alt a 0 = \u03bb b \u2192 a \u2191\u27e8 2 \u27e9 b\n alt a (suc n) = fold 1 (alt a n)\n\n alt-ok : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 alt a n\n alt-ok a zero = \u2261.sym \u2218 \u2191-\u2191\u2032 a 2\n alt-ok a (suc n) = cong-fold (alt-ok a n)\n\n{- TODO\n alt' : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 _\u2191\u27e8_\u27e9_ a (2 + n)\n alt' a zero b = \u2261.sym (\u2191-\u2191\u2032 a 2 _)\n alt' a (suc n) zero = \u2261.refl\n alt' a (suc n) (suc x) rewrite alt' a n (_\u21912+\u27e8_\u27e9_ a (suc n) x)\n = \u2261.cong (_\u2191\u27e8_\u27e9_ a (2 + n)) { fold 1 (fold (_*_ a) (fold 1) n) x} {a \u2191\u27e8 suc (suc (suc n)) \u27e9 x} (alt' a (suc n) x)\n-}\n\n open InflModule\n{-\n fold1-infl : \u2200 f \u2192 Infl f \u2192 Infl (fold 1 f)\n fold1-infl f finfl {zero} = {!z\u2264n!}\n fold1-infl f finfl {suc x} =\n x <\u27e8 {!s\u2264s (fold1-infl f finfl {x})!} \u27e9\n fold 1 f x <\u27e8 {!!} \u27e9\n {- f (fold 1 f x) \u2264\u27e8 finfl \u27e9\n f (fold 1 f x) \u2261\u27e8 \u2261.refl \u27e9 -}\n fold 1 f (1 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 f \u2218 suc)\n fold1+1-infl< f finfl< {zero} = finfl<\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + f (fold 1 f x) \u2264\u27e8 finfl< {f (fold 1 f x)} \u27e9\n f (suc (f (fold 1 f x))) \u2264\u27e8 {!!} \u27e9\n fold 1 f (2 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 (f \u2218 suc))\n fold1+1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 suc) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 suc) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+-infl< : \u2200 k f \u2192 Infl< (f \u2218\u2032 _+_ k) \u2192 Infl< (fold 1 (f \u2218\u2032 _+_ k))\n fold1+-infl< k f finfl< {zero} = s\u2264s z\u2264n\n fold1+-infl< k f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-infl< k f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 _+_ k) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 _+_ k) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n \n{-\n fold1+-infl< : \u2200 f \u2192 Infl+2< f \u2192 Infl+2< (fold 1 f)\n fold1+-infl< f finfl< {zero} _ = \u2115\u2264.refl\n fold1+-infl< f finfl< {suc zero} _ = {!!}\n fold1+-infl< f finfl< {suc (suc zero)} _ = {!!}\n fold1+-infl< f finfl< {suc (suc (suc x))} x>1 =\n 4 + x \u2264\u27e8 s\u2264s (fold1+-infl< f finfl< {suc (suc x)} (m\u2264m+n 2 x)) \u27e9\n 1 + fold 1 f (2 + x) \u2264\u27e8 finfl< {!!} \u27e9\n fold 1 f (3 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 f x \u2264\u27e8 finfl< \u27e9\n fold 1 f (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\nmodule ^-Props where\n open \u2261-Reasoning\n\n ^1 : \u2200 n \u2192 n ^ 1 \u2261 n\n ^1 zero = \u2261.refl\n ^1 (suc n) = \u2261.cong suc (proj\u2082 \u2115\u00b0.*-identity n)\n\n ^-+ : \u2200 b m n \u2192 b ^ (m + n) \u2261 b ^ m * b ^ n\n ^-+ b zero n = \u2261.sym (proj\u2082 \u2115\u00b0.+-identity (b ^ n))\n ^-+ b (suc m) n rewrite ^-+ b m n = \u2261.sym (\u2115\u00b0.*-assoc b (b ^ m) (b ^ n))\n\n ^-* : \u2200 b m n \u2192 b ^ (m * n) \u2261 (b ^ n) ^ m\n ^-* b zero n = \u2261.refl\n ^-* b (suc m) n = b ^ (n + m * n)\n \u2261\u27e8 ^-+ b n (m * n) \u27e9\n b ^ n * b ^ (m * n)\n \u2261\u27e8 \u2261.cong (_*_ (b ^ n)) (^-* b m n) \u27e9\n b ^ n * (b ^ n) ^ m \u220e\n\nack : \u2115 \u2192 \u2115 \u2192 \u2115\nack zero n = suc n\nack (suc m) zero = ack m (suc zero)\nack (suc m) (suc n) = ack m (ack (suc m) n)\n\n\u2264\u21d2\u2203 : \u2200 {m n} \u2192 m \u2264 n \u2192 \u2203 \u03bb k \u2192 m + k \u2261 n\n\u2264\u21d2\u2203 z\u2264n = _ , \u2261.refl\n\u2264\u21d2\u2203 (s\u2264s pf) = _ , \u2261.cong suc (proj\u2082 (\u2264\u21d2\u2203 pf))\n\nmodule ack-Props where\n lem\u2238 : \u2200 {m n} \u2192 m \u2264 n \u2192 m + (n \u2238 m) \u2261 n\n lem\u2238 z\u2264n = \u2261.refl\n lem\u2238 (s\u2264s {m} {n} m\u2264n) = \u2261.cong suc (lem\u2238 m\u2264n)\n\n -- n >= m \u2192 m + (n \u2238 m) \u2261 n\n -- n >= m \u2192 \u2203 k \u2192 n \u2261 m + k\n -- \u2203 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 m + ((m + k) \u2238 m) \u2261 m + k\n -- \u2200 k \u2192 m + k \u2261 m + k\n\n -- 3 \u2264 x \u2192 P x \u2192 \u2203 \u03bb k \u2192 P (3 + k)\n\n open InflModule\n\n fold1+-inflT< : \u2200 {z} \u2192 InflT< (fold (suc z))\n fold1+-inflT< _ {zero} = s\u2264s z\u2264n\n fold1+-inflT< {z} {f} finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-inflT< {z} {f} finfl< {x}) \u27e9\n 1 + fold (suc z) f x \u2264\u27e8 finfl< \u27e9\n f (fold (suc z) f x) \u2261\u27e8 \u2261.refl \u27e9\n fold (suc z) f (1 + x)\n \u220e\n where open \u2264-Reasoning\n\n Mon : (f : \u2115 \u2192 \u2115) \u2192 Set\n Mon f = \u2200 {x y} \u2192 x \u2264 y \u2192 f x \u2264 f y\n open InflModule\n\n\n \u2115-ind : \u2200 (P : \u2115 \u2192 Set) \u2192 P zero \u2192 (\u2200 {n} \u2192 P n \u2192 P (suc n)) \u2192 \u2200 {n} \u2192 P n\n \u2115-ind P P0 PS {zero} = P0\n \u2115-ind P P0 PS {suc n} = PS (\u2115-ind P P0 PS)\n\n open fold-Props\n fold-infl< : \u2200 {f g} \u2192 Infl< f \u2192 InflT< g \u2192 \u2200 {n} \u2192 Infl< (fold f g n)\n fold-infl< {f} {g} inflf< inflTg< {n} = \u2115-ind (Infl< \u2218 fold f g) inflf< inflTg< {n}\n\n fold-mon : \u2200 {f} (fmon : Mon f) (finfl< : Infl< f) {z} \u2192 Mon (fold z f)\n fold-mon fmon finfl< {_} {y = 0} z\u2264n = \u2115\u2264.refl\n fold-mon {f} fmon finfl< {z} {y = suc n} z\u2264n = z \u2264\u27e8 \u2264-step \u2115\u2264.refl \u27e9\n suc z \u2264\u27e8 nest-infl< f finfl< n {z} \u27e9\n fold z f (suc n) \u2261\u27e8 \u2261.refl \u27e9\n fold z f (suc n) \u220e\n where open \u2264-Reasoning\n fold-mon fmon finfl< (s\u2264s m\u2264n) = fmon (fold-mon fmon finfl< m\u2264n)\n\n MonT : Endo (Endo \u2115) \u2192 Set\n MonT g = \u2200 {f} \u2192 Mon f \u2192 Infl< f \u2192 Mon (g f)\n\n fold-mon' : \u2200 {f g} \u2192 Mon f \u2192 Infl< f \u2192 MonT g \u2192 InflT< g \u2192 \u2200 {n} \u2192 Mon (fold f g n)\n fold-mon' {f} {g} mon-f infl-f mont-g inflTg< {n} = go n where\n go : \u2200 n \u2192 Mon (fold f g n)\n go zero = mon-f\n go (suc n) = mont-g (go n) (fold-infl< infl-f inflTg< {n})\n\n\n -- +-fold : \u2200 a b \u2192 a + b \u2261 fold a suc b\n -- *-fold : \u2200 a b \u2192 a * b \u2261 fold 0 (_+_ a) b\n\n 1\u22641+a^b : \u2200 a b \u2192 1 \u2264 (1 + a) ^ b\n 1\u22641+a^b a zero = s\u2264s z\u2264n\n 1\u22641+a^b a (suc b) = 1\u22641+a^b a b +-mono z\u2264n\n\n 1+a^-mon : \u2200 {a} \u2192 Mon (_^_ (1 + a))\n 1+a^-mon {a} (z\u2264n {b}) = 1\u22641+a^b a b\n 1+a^-mon {a} (s\u2264s {m} {n} m\u2264n)\n = (1 + a) ^ (1 + m) \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ m + a * (1 + a) ^ m \u2264\u27e8 (1+a^-mon m\u2264n) +-mono ((a \u220e) *-mono (1+a^-mon m\u2264n)) \u27e9\n (1 + a) ^ n + a * (1 + a) ^ n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ (1 + n) \u220e\n where open \u2264-Reasoning\n\n lem2^3 : \u2200 n \u2192 2 ^ 3 \u2264 2 ^ (3 + n)\n lem2^3 n = 1+a^-mon {1} {3} {3 + n} (s\u2264s (s\u2264s (s\u2264s z\u2264n)))\n\n lem2221 : \u2200 m \u2192 2 \u2261 2 \u2191\u27e8 2 + m \u27e9 1\n lem2221 zero = \u2261.refl\n lem2221 (suc m) = lem2221 m\n\n lem4212 : \u2200 m \u2192 4 \u2261 2 \u2191\u27e8 1 + m \u27e9 2\n lem4212 zero = \u2261.refl\n lem4212 (suc m) = 4 \u2261\u27e8 lem4212 m \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 2 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 (1 + m)) (lem2221 m) \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 (2 \u2191\u27e8 2 + m \u27e9 1) \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 2 + m \u27e9 2 \u220e\n where open \u2261-Reasoning\n\n 2* = _*_ 2\n\n -- fold 2* n 1 \u2261 2 ^ n\n\n -- nest-* : \u2200 m n \u2192 nest (m * n) f \u2257 nest m (nest n f)\n\n nest-2* : \u2200 m n \u2192 nest (m * n) (fold 1) 2* \u2261 nest m (nest n (fold 1)) 2*\n nest-2* m n = nest-Properties.nest-* (fold 1) m n 2*\n\n{-\n lem3\u2264 : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 2\n lem3\u2264 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264 (suc n) = \n 3 \u2264\u27e8 {!!} \u27e9\n fold 2* f1 (1 + n) 2 \u220e\n where open \u2264-Reasoning\n f1 = fold 1\n\n lem3\u2264' : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 3\n lem3\u2264' 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264' (suc n) = \n 3 \u2264\u27e8 lem3\u2264 n \u27e9\n fold 2* (fold 1) n 2 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) n (fold 2* (fold 1) 3 1) \u2261\u27e8 {!!} \u27e9\n{- \u2261\u27e8 nest-Properties.nest-* {!2*!} {!!} {!!} {!!} \u27e9\n fold 2* (fold 1) (3 * n) 1 \u2261\u27e8 {!!} \u27e9\n-}\n fold 1 (fold 2* (fold 1) n) 3 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) (1 + n) 3 \u220e\n where open \u2264-Reasoning\n-}\n---\n\n{-\nfold 2* (fold 1) (3 * n) 1 \u2264\n\nfold 2* (fold 1) n (fold 2* (fold 1) n (fold 2* (fold 1) n 1)) \u2264\n\nfold 1 (fold 2* (fold 1) n) 3 \u2264\nfold 2* (fold 1) (1 + n) 3\n-}\n\n{-\n open \u2191-Props\n\n -*\u27e81+n\u27e9-infl : \u2200 n \u2192 Infl (_*_ (1 + n))\n -*\u27e81+n\u27e9-infl n = {!!}\n\n lem121 : \u2200 a b \u2192 b < (1 + a) \u2191\u27e8 2 \u27e9 (1 + b)\n lem121 a b rewrite \u2191\u2082-* (1 + a) (1 + b)\n = 1 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (1 + a) * (1 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (1 + a) (1 + b) \u27e9\n (1 + b) * (1 + a)\n \u220e\n where open \u2264-Reasoning\n\n -- \u2200 a b \u2192 b < (2 + a) * b\n lem222 : \u2200 a b \u2192 b < (2 + a) \u2191\u27e8 2 \u27e9 (2 + b)\n lem222 a b rewrite \u2191\u2082-* (2 + a) (2 + b)\n = 1 + b\n \u2264\u27e8 suc-infl \u27e9\n 2 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (2 + a) * (2 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (2 + a) (2 + b) \u27e9\n (2 + b) * (2 + a)\n \u220e\n where open \u2264-Reasoning\n-}\n{-\n open fold-Props\n\n module Foo a where\n f = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b\n f-lem : \u2200 n \u2192 f (suc n) \u2257 fold 1 (f n)\n f-lem n x = \u2261.refl\n\n f2 = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n f2-lem : \u2200 n \u2192 f2 (suc n) \u2257 fold 1 (f2 n)\n f2-lem n x = {!!}\n\n -- b < (1 + a) * (1 + b)\n\n -- nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n infl3< : \u2200 a n \u2192 Infl< (\u03bb b \u2192 (1 + a) \u21912+\u27e8 n \u27e9 (1 + b))\n infl3< a zero {b} = lem121 a b\n infl3< a (suc n) {b}\n = 1 + b\n -- fold1-infl< : \u2200 f \u2192 Infl< f \u2192 \u2200 x \u2192 x < fold 1 f x\n \u2264\u27e8 {!fold1f2-infl< {_}!} \u27e9\n fold 1 f1 b\n \u2264\u27e8 {!!} \u27e9\n{-\n \u2264\u27e8 fold1-infl< (f 0) (\u03bb {x} \u2192 {!infl3< a n {x}!}) {{!!}} \u27e9\n {!!}\n \u2264\u27e8 {!!} \u27e9\n-}\n{-\n 1 + b\n (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n \u2264\u27e8 {!nest-infl< (\u03bb b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b) ? b {(2 + a) \u21912+\u27e8 n \u27e9 (2 + b)}!} \u27e9\n fold (f 0 1) (f 0) (1 + b)\n \u2264\u27e8 {!\u2115\u2264.reflexive (\u2261.cong (\u03bb g \u2192 g 1) (nest-Properties.nest-+ (f 0) 1 (1 + b)))!} \u27e9\n-}\n fold 1 (f 0) (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n f 1 (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) \u21912+\u27e8 suc n \u27e9 (1 + b)\n \u220e\n\n where\n f = \u03bb k b \u2192 (1 + a) \u21912+\u27e8 k + n \u27e9 b\n f1 = f 0 \u2218 suc\n\n -- fold1f2-infl< : Infl< (fold 1 f2)\n -- fold1f2-infl< = fold1-infl< f2 {!(\u03bb b \u2192 infl3< a n b ?)!}\n\n fold1f1-infl<' : Infl< (fold 1 f1)\n fold1f1-infl<' = {!fold1+1-infl< (f 0) {!infl3< a n!}!}\n lem : \u2200 x \u2192 f 0 (1 + x) < f 0 (f 0 x)\n lem x = {!!}\n postulate\n finfl : Infl< (f 0)\n open \u2264-Reasoning\n-}\n\n_\u21d1\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\na \u21d1\u27e8 n , k \u27e9 b = fold k f n\n module M\u21d1 where \n f : \u2115 \u2192 \u2115\n f x = a \u2191\u27e8 2 + x \u27e9 b\n\nmodule \u21d1-Props where\n _\u21d1\u2032\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\n a \u21d1\u2032\u27e8 n\u2080 , k \u27e9 b = g n\u2080\n module M\u21d1\u2032 where\n h : \u2115 \u2192 \u2115\n h x = a \u2191\u27e8 2 + x \u27e9 b\n\n g : \u2115 \u2192 \u2115\n g 0 = k\n g (suc n) = h (g n)\n\n g-nest : \u2200 n \u2192 g n \u2261 h $\u27e8 n \u27e9 k\n g-nest zero = \u2261.refl\n g-nest (suc n) rewrite g-nest n = \u2261.refl\n\n \u21d1\u2080 : \u2200 a k b \u2192 a \u21d1\u27e8 0 , k \u27e9 b \u2261 k\n \u21d1\u2080 _ _ _ = \u2261.refl\n\n \u21d1\u2081 : \u2200 a k b \u2192 a \u21d1\u27e8 1 , k \u27e9 b \u2261 a \u2191\u27e8 2 + k \u27e9 b\n \u21d1\u2081 _ _ _ = \u2261.refl\n\n \u21d1-\u21d1\u2032 : \u2200 a n k b \u2192 a \u21d1\u27e8 n , k \u27e9 b \u2261 a \u21d1\u2032\u27e8 n , k \u27e9 b\n \u21d1-\u21d1\u2032 a n k b rewrite M\u21d1\u2032.g-nest a n k b n = \u2261.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Graham%27s_number\nGraham's-number : \u2115\nGraham's-number = 3 \u21d1\u27e8 64 , 4 \u27e9 3\n -- = (f\u2218\u2076\u2074\u2026\u2218f)4\n -- where f x = 3 \u2191\u27e8 2 + x \u27e9 3\n\n{-\nmodule GeneralisedArithmetic {a} {A : Set a} (0# : A) (1+ : A \u2192 A) where\n\n 1# : A\n 1# = 1+ 0#\n\n open Data.Nat.GeneralisedArithmetic 0# 1+ public\n\n exp : (* : A \u2192 A \u2192 A) \u2192 (\u2115 \u2192 A \u2192 A)\n exp _*_ n b = fold 1# (\u03bb s \u2192 b * s) n\n-}\n -- hyperop a n b = fold exp\n\nmodule == where\n _\u2248_ : (m n : \u2115) \u2192 Set\n m \u2248 n = T (m == n)\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {zero} {zero} _ = id\n subst P {suc _} {suc _} p = subst (P \u2218 suc) p\n subst _ {zero} {suc _} ()\n subst _ {suc _} {zero} ()\n\n sound : \u2200 m n \u2192 T (m == n) \u2192 m \u2261 n\n sound m n p = subst (_\u2261_ m) p \u2261.refl\n\n refl : Reflexive _\u2248_\n refl {zero} = _\n refl {suc n} = refl {n}\n\n sym : Symmetric _\u2248_\n sym {m} {n} eq rewrite sound m n eq = refl {n}\n\n trans : Transitive _\u2248_\n trans {m} {n} {o} m\u2248n n\u2248o rewrite sound m n m\u2248n | sound n o n\u2248o = refl {o}\n\n setoid : Setoid _ _\n setoid = record { Carrier = \u2115; _\u2248_ = _\u2248_\n ; isEquivalence =\n record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} } }\n\n open Setoid setoid public hiding (refl; sym; trans; _\u2248_)\n\n{-\ndata _`\u2264?`_\u219d_ : (m n : \u2115) \u2192 Dec (m \u2264 n) \u2192 Set where\n z\u2264?n : \u2200 {n} \u2192 zero `\u2264?` n \u219d yes z\u2264n\n s\u2264?z : \u2200 {m} \u2192 suc m `\u2264?` zero \u219d no \u03bb()\n s\u2264?s-yes : \u2200 {m n m\u2264n} \u2192 m `\u2264?` n \u219d yes m\u2264n \u2192 suc m `\u2264?` suc n \u219d yes (s\u2264s m\u2264n)\n s\u2264?s-no : \u2200 {m n m\u2270n} \u2192 m `\u2264?` n \u219d no m\u2270n \u2192 suc m `\u2264?` suc n \u219d no (m\u2270n \u2218 \u2264-pred)\n\n`\u2264?`-complete : \u2200 m n \u2192 m `\u2264?` n \u219d (m \u2264? n)\n`\u2264?`-complete zero n = z\u2264?n\n`\u2264?`-complete (suc n) zero = {!s\u2264?z!}\n`\u2264?`-complete (suc m) (suc n) with m \u2264? n | `\u2264?`-complete m n\n... | yes q | r = s\u2264?s-yes r\n... | no q | r = s\u2264?s-no {!!}\n-}\n\n_<=_ : (x y : \u2115) \u2192 Bool\nzero <= _ = true\nsuc _ <= zero = false\nsuc m <= suc n = m <= n\n\nmodule <= where\n \u211b : \u2115 \u2192 \u2115 \u2192 Set\n \u211b x y = T (x <= y)\n\n sound : \u2200 m n \u2192 \u211b m n \u2192 m \u2264 n\n sound zero _ _ = z\u2264n\n sound (suc m) (suc n) p = s\u2264s (sound m n p)\n sound (suc m) zero ()\n\n complete : \u2200 {m n} \u2192 m \u2264 n \u2192 \u211b m n\n complete z\u2264n = _\n complete (s\u2264s m\u2264n) = complete m\u2264n\n\n isTotalOrder : IsTotalOrder _\u2261_ \u211b\n isTotalOrder = record { isPartialOrder = isPartialOrder; total = \u03bb x y \u2192 \u228e-map complete complete (\u2115\u2264.total x y) }\n where\n reflexive : \u2200 {i j} \u2192 i \u2261 j \u2192 \u211b i j\n reflexive {i} \u2261.refl = complete (\u2115\u2264.refl {i})\n trans : Transitive \u211b\n trans {x} {y} {z} p q = complete (\u2115\u2264.trans (sound x y p) (sound y z q))\n isPreorder : IsPreorder _\u2261_ \u211b\n isPreorder = record { isEquivalence = \u2261.isEquivalence\n ; reflexive = reflexive\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n antisym : Antisymmetric _\u2261_ \u211b\n antisym {x} {y} p q = \u2115\u2264.antisym (sound x y p) (sound y x q)\n isPartialOrder : IsPartialOrder _\u2261_ \u211b\n isPartialOrder = record { isPreorder = isPreorder; antisym = antisym }\n\n open IsTotalOrder isTotalOrder public\n\n<=-steps\u2032 : \u2200 {x} y \u2192 T (x <= (x + y))\n<=-steps\u2032 {x} y = <=.complete (\u2264-steps\u2032 {x} y)\n\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 : \u2200 x y \u2192 suc x \u2238 y \u2264 suc (x \u2238 y)\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x zero = \u2115\u2264.refl\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 zero (suc y) rewrite 0\u2238n\u22610 y = z\u2264n\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 (suc x) (suc y) = sucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x y\n\nx\u22642y\u2032\u2192x\u2238y\u2264y : \u2200 x y \u2192 x \u2264 2*\u2032 y \u2192 x \u2238 y \u2264 y\nx\u22642y\u2032\u2192x\u2238y\u2264y x zero p = p\nx\u22642y\u2032\u2192x\u2238y\u2264y zero (suc y) p = z\u2264n\nx\u22642y\u2032\u2192x\u2238y\u2264y (suc zero) (suc y) (s\u2264s p) rewrite 0\u2238n\u22610 y = z\u2264n\nx\u22642y\u2032\u2192x\u2238y\u2264y (suc (suc x)) (suc y) (s\u2264s (s\u2264s p))\n = \u2115\u2264.trans (sucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x y) (s\u2264s (x\u22642y\u2032\u2192x\u2238y\u2264y x y p))\n\nx<2y\u2032\u2192x\u2238y \u00aca \u00acb c = \u22a5-elim (p (\u2264-pred c))\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (Props.\u2264-steps 1 1+n\u2264m)\n\nnot<=\u2192< : \u2200 x y \u2192 T (not (x <= y)) \u2192 T (suc y <= x)\nnot<=\u2192< x y p = <=.complete (\u2270\u2192< x y (T'not'\u00ac p \u2218 <=.complete))\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Nat.NP where\n\nimport Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat public hiding (module GeneralisedArithmetic; module \u2264-Reasoning; fold)\nopen import Data.Nat.Properties as Props\nopen import Data.Nat.Logical\nopen import Data.Bool.NP hiding (_==_; module ==)\nopen import Data.Product using (proj\u2081; proj\u2082; \u2203; _,_)\nopen import Data.Sum renaming (map to \u228e-map)\nopen import Data.Empty using (\u22a5-elim; \u22a5)\nopen import Function.NP\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_; _\u2257_; module \u2261-Reasoning)\n\nmodule \u2115\u00b0 = Algebra.CommutativeSemiring Props.commutativeSemiring\nmodule \u2115cmp = StrictTotalOrder Props.strictTotalOrder\nmodule \u2115\u2264 = DecTotalOrder decTotalOrder\nmodule \u2294\u00b0 = Algebra.CommutativeSemiringWithoutOne \u2294-\u2293-0-commutativeSemiringWithoutOne\n\nmodule \u2264-Reasoning where\n open Preorder-Reasoning \u2115\u2264.preorder public renaming (_\u223c\u27e8_\u27e9_ to _\u2264\u27e8_\u27e9_)\n infixr 2 _\u2261\u27e8_\u27e9_\n _\u2261\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x \u2261 y \u2192 y \u2264 z \u2192 x \u2264 z\n _ \u2261\u27e8 \u2261.refl \u27e9 p = p\n infixr 2 _<\u27e8_\u27e9_\n _<\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x < y \u2192 y \u2264 z \u2192 x < z\n x <\u27e8 p \u27e9 q = suc x \u2264\u27e8 p \u27e9 q\n\nsuc-injective : \u2200 {n m : \u2115} \u2192 (suc n \u2236 \u2115) \u2261 suc m \u2192 n \u2261 m\nsuc-injective = \u2261.cong pred\n\n+-\u2264-inj : \u2200 x {y z} \u2192 x + y \u2264 x + z \u2192 y \u2264 z\n+-\u2264-inj zero p = p\n+-\u2264-inj (suc x) (s\u2264s p) = +-\u2264-inj x p\n\nsucx\u2270x : \u2200 x \u2192 suc x \u2270 x\nsucx\u2270x zero = \u03bb()\nsucx\u2270x (suc x) = sucx\u2270x x \u2218 \u2264-pred\n\n\u00acn\u2264x dist (x * y) (x * z) == x * dist y z))\n\ninfix 8 _^_\n_^_ : \u2115 \u2192 \u2115 \u2192 \u2115\nb ^ zero = 1\nb ^ suc n = b * b ^ n\n\n2^_ : \u2115 \u2192 \u2115\n2^ n = \u27e82^ n * 1 \u27e9\n\n2^-spec : \u2200 n \u2192 2^ n \u2261 2 ^ n\n2^-spec zero = \u2261.refl\n2^-spec (suc n) rewrite 2^-spec n | 2*-spec (2 ^ n) = \u2261.refl\n\n2^*-spec : \u2200 m n \u2192 2^\u27e8 m \u27e9* n \u2261 2 ^ m * n\n2^*-spec zero n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\n2^*-spec (suc m) n rewrite 2^*-spec m n\n | \u2115\u00b0.*-assoc 2 (2 ^ m) n\n | \u2115\u00b0.+-comm (2 ^ m * n) 0 = \u2261.refl\n\n1\u22642^ : \u2200 n \u2192 2^ n \u2265 1\n1+\u22642^ : \u2200 n \u2192 2^ n \u2265 1 + n\n1+\u22642^ zero = s\u2264s z\u2264n\n1+\u22642^ (suc n) = (1\u22642^ n) +-mono (1+\u22642^ n)\n\n1\u22642^ n = \u2115\u2264.trans (s\u2264s z\u2264n) (1+\u22642^ n)\n\n\u2264-steps\u2032 : \u2200 {x} y \u2192 x \u2264 x + y\n\u2264-steps\u2032 {x} y rewrite \u2115\u00b0.+-comm x y = \u2264-steps y \u2115\u2264.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Hyper_operator\n_\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\na \u2191\u27e8 suc n \u27e9 (suc b) = a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\na \u2191\u27e8 0 \u27e9 b = suc b\na \u2191\u27e8 1 \u27e9 0 = a\na \u2191\u27e8 2 \u27e9 0 = 0\na \u2191\u27e8 suc (suc (suc n)) \u27e9 0 = 1\n\nmodule \u2191-Props where\n \u2191-fold : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 b \u2261 fold (a \u2191\u27e8 suc n \u27e9 0) (_\u2191\u27e8_\u27e9_ a n) b\n \u2191-fold a n zero = \u2261.refl\n \u2191-fold a n (suc b) = \u2261.cong (_\u2191\u27e8_\u27e9_ a n) (\u2191-fold a n b)\n\n \u2191\u2080-+ : \u2200 a b \u2192 a \u2191\u27e8 0 \u27e9 b \u2261 1 + b\n \u2191\u2080-+ a b = \u2261.refl\n\n \u2191\u2081-+ : \u2200 a b \u2192 a \u2191\u27e8 1 \u27e9 b \u2261 b + a\n \u2191\u2081-+ a zero = \u2261.refl\n \u2191\u2081-+ a (suc b) = \u2261.cong suc (\u2191\u2081-+ a b)\n\n \u2191\u2082-* : \u2200 a b \u2192 a \u2191\u27e8 2 \u27e9 b \u2261 b * a\n \u2191\u2082-* a zero = \u2261.refl\n \u2191\u2082-* a (suc b) rewrite \u2191\u2082-* a b\n | \u2191\u2081-+ a (b * a)\n | \u2115\u00b0.+-comm (b * a) a\n = \u2261.refl\n\n -- fold 1 (_*_ a) b \u2261 a ^ b\n \u2191\u2083-^ : \u2200 a b \u2192 a \u2191\u27e8 3 \u27e9 b \u2261 a ^ b\n \u2191\u2083-^ a zero = \u2261.refl\n \u2191\u2083-^ a (suc b) rewrite \u2191\u2083-^ a b\n | \u2191\u2082-* a (a ^ b)\n | \u2115\u00b0.*-comm (a ^ b) a\n = \u2261.refl\n\n _\u2191\u27e8_\u27e90 : \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u27e8 0 \u27e90 = 1\n a \u2191\u27e8 1 \u27e90 = a\n a \u2191\u27e8 2 \u27e90 = 0\n a \u2191\u27e8 suc (suc (suc n)) \u27e90 = 1\n\n _`\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _`\u2191\u27e8_\u27e9_ a 0 = suc\n _`\u2191\u27e8_\u27e9_ a (suc n) = fold (a \u2191\u27e8 suc n \u27e90) (_`\u2191\u27e8_\u27e9_ a n)\n\n \u2191-ind-rule : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 (suc b) \u2261 a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\n \u2191-ind-rule a n b = \u2261.refl\n\n _\u2191\u2032\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u2032\u27e8 0 \u27e9 b = suc b\n a \u2191\u2032\u27e8 1 \u27e9 b = a + b\n a \u2191\u2032\u27e8 2 \u27e9 b = a * b\n a \u2191\u2032\u27e8 3 \u27e9 b = a ^ b\n a \u2191\u2032\u27e8 suc (suc (suc (suc n))) \u27e9 b = a \u2191\u27e8 4 + n \u27e9 b\n\n \u2191-\u2191\u2032 : \u2200 a n b \u2192 a \u2191\u27e8 n \u27e9 b \u2261 a \u2191\u2032\u27e8 n \u27e9 b\n \u2191-\u2191\u2032 a 0 b = \u2261.refl\n \u2191-\u2191\u2032 a 1 b = \u2261.trans (\u2191\u2081-+ a b) (\u2115\u00b0.+-comm b a)\n \u2191-\u2191\u2032 a 2 b = \u2261.trans (\u2191\u2082-* a b) (\u2115\u00b0.*-comm b a)\n \u2191-\u2191\u2032 a 3 b = \u2191\u2083-^ a b\n \u2191-\u2191\u2032 a (suc (suc (suc (suc _)))) b = \u2261.refl\n\n _\u21912+\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _\u21912+\u27e8_\u27e9_ a = fold (_*_ a) (fold 1)\n\nmodule InflModule where\n -- Inflationary functions\n Infl< : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl< f = \u2200 {x} \u2192 x < f x\n\n Infl : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl f = Infl< (suc \u2218 f)\n\n InflT< : (f : Endo (Endo \u2115)) \u2192 Set\n InflT< f = \u2200 {g} \u2192 Infl< g \u2192 Infl< (f g)\n\n{-\n\u2200 {x} \u2192 x \u2264 f x\n\n\n 1 + x \u2264 1 + f x\n x < 1 + f x\n x < (suc \u2218 f) x\n Infl< (suc \u2218 f)\n\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n\nInfl< f \u2192 Infl f\nInfl< f \u2192 Infl< (suc \u2218 f)\n-}\n\n id-infl : Infl id\n id-infl = \u2115\u2264.refl\n\n \u2218-infl< : \u2200 {f g} \u2192 Infl< f \u2192 Infl< g \u2192 Infl< (f \u2218 g)\n \u2218-infl< inflf< inflg< = <-trans inflg< inflf<\n\n suc-infl< : Infl< suc\n suc-infl< = \u2115\u2264.refl\n\n suc-infl : Infl suc\n suc-infl = s\u2264s (\u2264-step \u2115\u2264.refl)\n\n nest-infl : \u2200 f (finfl : Infl f) n \u2192 Infl (nest n f)\n nest-infl f _ zero = \u2115\u2264.refl\n nest-infl f finfl (suc n) = \u2115\u2264.trans (nest-infl f finfl n) finfl\n\n nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n nest-infl< f finfl< zero = finfl<\n nest-infl< f finfl< (suc n) = <-trans (nest-infl< f finfl< n) finfl< \n\n-- See Function.NP.nest-Properties for more properties\nmodule fold-Props where\n\n fold-suc : \u2200 m n \u2192 fold m suc n \u2261 n + m\n fold-suc m zero = \u2261.refl\n fold-suc m (suc n) rewrite fold-suc m n = \u2261.refl\n\n fold-+ : \u2200 m n \u2192 fold 0 (_+_ m) n \u2261 n * m\n fold-+ m zero = \u2261.refl\n fold-+ m (suc n) rewrite fold-+ m n = \u2261.refl\n\n fold-* : \u2200 m n \u2192 fold 1 (_*_ m) n \u2261 m ^ n\n fold-* m zero = \u2261.refl\n fold-* m (suc n) rewrite fold-* m n = \u2261.refl\n\n{- TODO\n fold-^ : \u2200 m n \u2192 fold 1 (flip _^_ m) n \u2261 m \u2191\u27e8 4 \u27e9 n\n fold-^ m zero = \u2261.refl\n fold-^ m (suc n) rewrite fold-^ m n = ?\n-}\n\n cong-fold : \u2200 {A : Set} {f g : Endo A} (f\u2257g : f \u2257 g) {z} \u2192 fold z f \u2257 fold z g\n cong-fold eq zero = \u2261.refl\n cong-fold eq {z} (suc x) rewrite cong-fold eq {z} x = eq _\n\n private\n open \u2191-Props\n module Alt\u21912 where\n alt : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n alt a 0 = \u03bb b \u2192 a \u2191\u27e8 2 \u27e9 b\n alt a (suc n) = fold 1 (alt a n)\n\n alt-ok : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 alt a n\n alt-ok a zero = \u2261.sym \u2218 \u2191-\u2191\u2032 a 2\n alt-ok a (suc n) = cong-fold (alt-ok a n)\n\n{- TODO\n alt' : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 _\u2191\u27e8_\u27e9_ a (2 + n)\n alt' a zero b = \u2261.sym (\u2191-\u2191\u2032 a 2 _)\n alt' a (suc n) zero = \u2261.refl\n alt' a (suc n) (suc x) rewrite alt' a n (_\u21912+\u27e8_\u27e9_ a (suc n) x)\n = \u2261.cong (_\u2191\u27e8_\u27e9_ a (2 + n)) { fold 1 (fold (_*_ a) (fold 1) n) x} {a \u2191\u27e8 suc (suc (suc n)) \u27e9 x} (alt' a (suc n) x)\n-}\n\n open InflModule\n{-\n fold1-infl : \u2200 f \u2192 Infl f \u2192 Infl (fold 1 f)\n fold1-infl f finfl {zero} = {!z\u2264n!}\n fold1-infl f finfl {suc x} =\n x <\u27e8 {!s\u2264s (fold1-infl f finfl {x})!} \u27e9\n fold 1 f x <\u27e8 {!!} \u27e9\n {- f (fold 1 f x) \u2264\u27e8 finfl \u27e9\n f (fold 1 f x) \u2261\u27e8 \u2261.refl \u27e9 -}\n fold 1 f (1 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 f \u2218 suc)\n fold1+1-infl< f finfl< {zero} = finfl<\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + f (fold 1 f x) \u2264\u27e8 finfl< {f (fold 1 f x)} \u27e9\n f (suc (f (fold 1 f x))) \u2264\u27e8 {!!} \u27e9\n fold 1 f (2 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 (f \u2218 suc))\n fold1+1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 suc) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 suc) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+-infl< : \u2200 k f \u2192 Infl< (f \u2218\u2032 _+_ k) \u2192 Infl< (fold 1 (f \u2218\u2032 _+_ k))\n fold1+-infl< k f finfl< {zero} = s\u2264s z\u2264n\n fold1+-infl< k f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-infl< k f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 _+_ k) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 _+_ k) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n \n{-\n fold1+-infl< : \u2200 f \u2192 Infl+2< f \u2192 Infl+2< (fold 1 f)\n fold1+-infl< f finfl< {zero} _ = \u2115\u2264.refl\n fold1+-infl< f finfl< {suc zero} _ = {!!}\n fold1+-infl< f finfl< {suc (suc zero)} _ = {!!}\n fold1+-infl< f finfl< {suc (suc (suc x))} x>1 =\n 4 + x \u2264\u27e8 s\u2264s (fold1+-infl< f finfl< {suc (suc x)} (m\u2264m+n 2 x)) \u27e9\n 1 + fold 1 f (2 + x) \u2264\u27e8 finfl< {!!} \u27e9\n fold 1 f (3 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 f x \u2264\u27e8 finfl< \u27e9\n fold 1 f (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\nmodule ^-Props where\n open \u2261-Reasoning\n\n ^1 : \u2200 n \u2192 n ^ 1 \u2261 n\n ^1 zero = \u2261.refl\n ^1 (suc n) = \u2261.cong suc (proj\u2082 \u2115\u00b0.*-identity n)\n\n ^-+ : \u2200 b m n \u2192 b ^ (m + n) \u2261 b ^ m * b ^ n\n ^-+ b zero n = \u2261.sym (proj\u2082 \u2115\u00b0.+-identity (b ^ n))\n ^-+ b (suc m) n rewrite ^-+ b m n = \u2261.sym (\u2115\u00b0.*-assoc b (b ^ m) (b ^ n))\n\n ^-* : \u2200 b m n \u2192 b ^ (m * n) \u2261 (b ^ n) ^ m\n ^-* b zero n = \u2261.refl\n ^-* b (suc m) n = b ^ (n + m * n)\n \u2261\u27e8 ^-+ b n (m * n) \u27e9\n b ^ n * b ^ (m * n)\n \u2261\u27e8 \u2261.cong (_*_ (b ^ n)) (^-* b m n) \u27e9\n b ^ n * (b ^ n) ^ m \u220e\n\nack : \u2115 \u2192 \u2115 \u2192 \u2115\nack zero n = suc n\nack (suc m) zero = ack m (suc zero)\nack (suc m) (suc n) = ack m (ack (suc m) n)\n\n\u2264\u21d2\u2203 : \u2200 {m n} \u2192 m \u2264 n \u2192 \u2203 \u03bb k \u2192 m + k \u2261 n\n\u2264\u21d2\u2203 z\u2264n = _ , \u2261.refl\n\u2264\u21d2\u2203 (s\u2264s pf) = _ , \u2261.cong suc (proj\u2082 (\u2264\u21d2\u2203 pf))\n\nmodule ack-Props where\n lem\u2238 : \u2200 {m n} \u2192 m \u2264 n \u2192 m + (n \u2238 m) \u2261 n\n lem\u2238 z\u2264n = \u2261.refl\n lem\u2238 (s\u2264s {m} {n} m\u2264n) = \u2261.cong suc (lem\u2238 m\u2264n)\n\n -- n >= m \u2192 m + (n \u2238 m) \u2261 n\n -- n >= m \u2192 \u2203 k \u2192 n \u2261 m + k\n -- \u2203 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 m + ((m + k) \u2238 m) \u2261 m + k\n -- \u2200 k \u2192 m + k \u2261 m + k\n\n -- 3 \u2264 x \u2192 P x \u2192 \u2203 \u03bb k \u2192 P (3 + k)\n\n open InflModule\n\n fold1+-inflT< : \u2200 {z} \u2192 InflT< (fold (suc z))\n fold1+-inflT< _ {zero} = s\u2264s z\u2264n\n fold1+-inflT< {z} {f} finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-inflT< {z} {f} finfl< {x}) \u27e9\n 1 + fold (suc z) f x \u2264\u27e8 finfl< \u27e9\n f (fold (suc z) f x) \u2261\u27e8 \u2261.refl \u27e9\n fold (suc z) f (1 + x)\n \u220e\n where open \u2264-Reasoning\n\n Mon : (f : \u2115 \u2192 \u2115) \u2192 Set\n Mon f = \u2200 {x y} \u2192 x \u2264 y \u2192 f x \u2264 f y\n open InflModule\n\n\n \u2115-ind : \u2200 (P : \u2115 \u2192 Set) \u2192 P zero \u2192 (\u2200 {n} \u2192 P n \u2192 P (suc n)) \u2192 \u2200 {n} \u2192 P n\n \u2115-ind P P0 PS {zero} = P0\n \u2115-ind P P0 PS {suc n} = PS (\u2115-ind P P0 PS)\n\n open fold-Props\n fold-infl< : \u2200 {f g} \u2192 Infl< f \u2192 InflT< g \u2192 \u2200 {n} \u2192 Infl< (fold f g n)\n fold-infl< {f} {g} inflf< inflTg< {n} = \u2115-ind (Infl< \u2218 fold f g) inflf< inflTg< {n}\n\n fold-mon : \u2200 {f} (fmon : Mon f) (finfl< : Infl< f) {z} \u2192 Mon (fold z f)\n fold-mon fmon finfl< {_} {y = 0} z\u2264n = \u2115\u2264.refl\n fold-mon {f} fmon finfl< {z} {y = suc n} z\u2264n = z \u2264\u27e8 \u2264-step \u2115\u2264.refl \u27e9\n suc z \u2264\u27e8 nest-infl< f finfl< n {z} \u27e9\n fold z f (suc n) \u2261\u27e8 \u2261.refl \u27e9\n fold z f (suc n) \u220e\n where open \u2264-Reasoning\n fold-mon fmon finfl< (s\u2264s m\u2264n) = fmon (fold-mon fmon finfl< m\u2264n)\n\n MonT : Endo (Endo \u2115) \u2192 Set\n MonT g = \u2200 {f} \u2192 Mon f \u2192 Infl< f \u2192 Mon (g f)\n\n fold-mon' : \u2200 {f g} \u2192 Mon f \u2192 Infl< f \u2192 MonT g \u2192 InflT< g \u2192 \u2200 {n} \u2192 Mon (fold f g n)\n fold-mon' {f} {g} mon-f infl-f mont-g inflTg< {n} = go n where\n go : \u2200 n \u2192 Mon (fold f g n)\n go zero = mon-f\n go (suc n) = mont-g (go n) (fold-infl< infl-f inflTg< {n})\n\n\n -- +-fold : \u2200 a b \u2192 a + b \u2261 fold a suc b\n -- *-fold : \u2200 a b \u2192 a * b \u2261 fold 0 (_+_ a) b\n\n 1\u22641+a^b : \u2200 a b \u2192 1 \u2264 (1 + a) ^ b\n 1\u22641+a^b a zero = s\u2264s z\u2264n\n 1\u22641+a^b a (suc b) = 1\u22641+a^b a b +-mono z\u2264n\n\n 1+a^-mon : \u2200 {a} \u2192 Mon (_^_ (1 + a))\n 1+a^-mon {a} (z\u2264n {b}) = 1\u22641+a^b a b\n 1+a^-mon {a} (s\u2264s {m} {n} m\u2264n)\n = (1 + a) ^ (1 + m) \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ m + a * (1 + a) ^ m \u2264\u27e8 (1+a^-mon m\u2264n) +-mono ((a \u220e) *-mono (1+a^-mon m\u2264n)) \u27e9\n (1 + a) ^ n + a * (1 + a) ^ n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ (1 + n) \u220e\n where open \u2264-Reasoning\n\n lem2^3 : \u2200 n \u2192 2 ^ 3 \u2264 2 ^ (3 + n)\n lem2^3 n = 1+a^-mon {1} {3} {3 + n} (s\u2264s (s\u2264s (s\u2264s z\u2264n)))\n\n lem2221 : \u2200 m \u2192 2 \u2261 2 \u2191\u27e8 2 + m \u27e9 1\n lem2221 zero = \u2261.refl\n lem2221 (suc m) = lem2221 m\n\n lem4212 : \u2200 m \u2192 4 \u2261 2 \u2191\u27e8 1 + m \u27e9 2\n lem4212 zero = \u2261.refl\n lem4212 (suc m) = 4 \u2261\u27e8 lem4212 m \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 2 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 (1 + m)) (lem2221 m) \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 (2 \u2191\u27e8 2 + m \u27e9 1) \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 2 + m \u27e9 2 \u220e\n where open \u2261-Reasoning\n\n 2* = _*_ 2\n\n -- fold 2* n 1 \u2261 2 ^ n\n\n -- nest-* : \u2200 m n \u2192 nest (m * n) f \u2257 nest m (nest n f)\n\n nest-2* : \u2200 m n \u2192 nest (m * n) (fold 1) 2* \u2261 nest m (nest n (fold 1)) 2*\n nest-2* m n = nest-Properties.nest-* (fold 1) m n 2*\n\n{-\n lem3\u2264 : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 2\n lem3\u2264 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264 (suc n) = \n 3 \u2264\u27e8 {!!} \u27e9\n fold 2* f1 (1 + n) 2 \u220e\n where open \u2264-Reasoning\n f1 = fold 1\n\n lem3\u2264' : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 3\n lem3\u2264' 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264' (suc n) = \n 3 \u2264\u27e8 lem3\u2264 n \u27e9\n fold 2* (fold 1) n 2 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) n (fold 2* (fold 1) 3 1) \u2261\u27e8 {!!} \u27e9\n{- \u2261\u27e8 nest-Properties.nest-* {!2*!} {!!} {!!} {!!} \u27e9\n fold 2* (fold 1) (3 * n) 1 \u2261\u27e8 {!!} \u27e9\n-}\n fold 1 (fold 2* (fold 1) n) 3 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) (1 + n) 3 \u220e\n where open \u2264-Reasoning\n-}\n---\n\n{-\nfold 2* (fold 1) (3 * n) 1 \u2264\n\nfold 2* (fold 1) n (fold 2* (fold 1) n (fold 2* (fold 1) n 1)) \u2264\n\nfold 1 (fold 2* (fold 1) n) 3 \u2264\nfold 2* (fold 1) (1 + n) 3\n-}\n\n{-\n open \u2191-Props\n\n -*\u27e81+n\u27e9-infl : \u2200 n \u2192 Infl (_*_ (1 + n))\n -*\u27e81+n\u27e9-infl n = {!!}\n\n lem121 : \u2200 a b \u2192 b < (1 + a) \u2191\u27e8 2 \u27e9 (1 + b)\n lem121 a b rewrite \u2191\u2082-* (1 + a) (1 + b)\n = 1 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (1 + a) * (1 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (1 + a) (1 + b) \u27e9\n (1 + b) * (1 + a)\n \u220e\n where open \u2264-Reasoning\n\n -- \u2200 a b \u2192 b < (2 + a) * b\n lem222 : \u2200 a b \u2192 b < (2 + a) \u2191\u27e8 2 \u27e9 (2 + b)\n lem222 a b rewrite \u2191\u2082-* (2 + a) (2 + b)\n = 1 + b\n \u2264\u27e8 suc-infl \u27e9\n 2 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (2 + a) * (2 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (2 + a) (2 + b) \u27e9\n (2 + b) * (2 + a)\n \u220e\n where open \u2264-Reasoning\n-}\n{-\n open fold-Props\n\n module Foo a where\n f = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b\n f-lem : \u2200 n \u2192 f (suc n) \u2257 fold 1 (f n)\n f-lem n x = \u2261.refl\n\n f2 = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n f2-lem : \u2200 n \u2192 f2 (suc n) \u2257 fold 1 (f2 n)\n f2-lem n x = {!!}\n\n -- b < (1 + a) * (1 + b)\n\n -- nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n infl3< : \u2200 a n \u2192 Infl< (\u03bb b \u2192 (1 + a) \u21912+\u27e8 n \u27e9 (1 + b))\n infl3< a zero {b} = lem121 a b\n infl3< a (suc n) {b}\n = 1 + b\n -- fold1-infl< : \u2200 f \u2192 Infl< f \u2192 \u2200 x \u2192 x < fold 1 f x\n \u2264\u27e8 {!fold1f2-infl< {_}!} \u27e9\n fold 1 f1 b\n \u2264\u27e8 {!!} \u27e9\n{-\n \u2264\u27e8 fold1-infl< (f 0) (\u03bb {x} \u2192 {!infl3< a n {x}!}) {{!!}} \u27e9\n {!!}\n \u2264\u27e8 {!!} \u27e9\n-}\n{-\n 1 + b\n (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n \u2264\u27e8 {!nest-infl< (\u03bb b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b) ? b {(2 + a) \u21912+\u27e8 n \u27e9 (2 + b)}!} \u27e9\n fold (f 0 1) (f 0) (1 + b)\n \u2264\u27e8 {!\u2115\u2264.reflexive (\u2261.cong (\u03bb g \u2192 g 1) (nest-Properties.nest-+ (f 0) 1 (1 + b)))!} \u27e9\n-}\n fold 1 (f 0) (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n f 1 (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) \u21912+\u27e8 suc n \u27e9 (1 + b)\n \u220e\n\n where\n f = \u03bb k b \u2192 (1 + a) \u21912+\u27e8 k + n \u27e9 b\n f1 = f 0 \u2218 suc\n\n -- fold1f2-infl< : Infl< (fold 1 f2)\n -- fold1f2-infl< = fold1-infl< f2 {!(\u03bb b \u2192 infl3< a n b ?)!}\n\n fold1f1-infl<' : Infl< (fold 1 f1)\n fold1f1-infl<' = {!fold1+1-infl< (f 0) {!infl3< a n!}!}\n lem : \u2200 x \u2192 f 0 (1 + x) < f 0 (f 0 x)\n lem x = {!!}\n postulate\n finfl : Infl< (f 0)\n open \u2264-Reasoning\n-}\n\n_\u21d1\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\na \u21d1\u27e8 n , k \u27e9 b = fold k f n\n module M\u21d1 where \n f : \u2115 \u2192 \u2115\n f x = a \u2191\u27e8 2 + x \u27e9 b\n\nmodule \u21d1-Props where\n _\u21d1\u2032\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\n a \u21d1\u2032\u27e8 n\u2080 , k \u27e9 b = g n\u2080\n module M\u21d1\u2032 where\n h : \u2115 \u2192 \u2115\n h x = a \u2191\u27e8 2 + x \u27e9 b\n\n g : \u2115 \u2192 \u2115\n g 0 = k\n g (suc n) = h (g n)\n\n g-nest : \u2200 n \u2192 g n \u2261 h $\u27e8 n \u27e9 k\n g-nest zero = \u2261.refl\n g-nest (suc n) rewrite g-nest n = \u2261.refl\n\n \u21d1\u2080 : \u2200 a k b \u2192 a \u21d1\u27e8 0 , k \u27e9 b \u2261 k\n \u21d1\u2080 _ _ _ = \u2261.refl\n\n \u21d1\u2081 : \u2200 a k b \u2192 a \u21d1\u27e8 1 , k \u27e9 b \u2261 a \u2191\u27e8 2 + k \u27e9 b\n \u21d1\u2081 _ _ _ = \u2261.refl\n\n \u21d1-\u21d1\u2032 : \u2200 a n k b \u2192 a \u21d1\u27e8 n , k \u27e9 b \u2261 a \u21d1\u2032\u27e8 n , k \u27e9 b\n \u21d1-\u21d1\u2032 a n k b rewrite M\u21d1\u2032.g-nest a n k b n = \u2261.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Graham%27s_number\nGraham's-number : \u2115\nGraham's-number = 3 \u21d1\u27e8 64 , 4 \u27e9 3\n -- = (f\u2218\u2076\u2074\u2026\u2218f)4\n -- where f x = 3 \u2191\u27e8 2 + x \u27e9 3\n\n{-\nmodule GeneralisedArithmetic {a} {A : Set a} (0# : A) (1+ : A \u2192 A) where\n\n 1# : A\n 1# = 1+ 0#\n\n open Data.Nat.GeneralisedArithmetic 0# 1+ public\n\n exp : (* : A \u2192 A \u2192 A) \u2192 (\u2115 \u2192 A \u2192 A)\n exp _*_ n b = fold 1# (\u03bb s \u2192 b * s) n\n-}\n -- hyperop a n b = fold exp\n\nmodule == where\n _\u2248_ : (m n : \u2115) \u2192 Set\n m \u2248 n = T (m == n)\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {zero} {zero} _ = id\n subst P {suc _} {suc _} p = subst (P \u2218 suc) p\n subst _ {zero} {suc _} ()\n subst _ {suc _} {zero} ()\n\n sound : \u2200 m n \u2192 T (m == n) \u2192 m \u2261 n\n sound m n p = subst (_\u2261_ m) p \u2261.refl\n\n refl : Reflexive _\u2248_\n refl {zero} = _\n refl {suc n} = refl {n}\n\n sym : Symmetric _\u2248_\n sym {m} {n} eq rewrite sound m n eq = refl {n}\n\n trans : Transitive _\u2248_\n trans {m} {n} {o} m\u2248n n\u2248o rewrite sound m n m\u2248n | sound n o n\u2248o = refl {o}\n\n setoid : Setoid _ _\n setoid = record { Carrier = \u2115; _\u2248_ = _\u2248_\n ; isEquivalence =\n record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} } }\n\n open Setoid setoid public hiding (refl; sym; trans; _\u2248_)\n\n{-\ndata _`\u2264?`_\u219d_ : (m n : \u2115) \u2192 Dec (m \u2264 n) \u2192 Set where\n z\u2264?n : \u2200 {n} \u2192 zero `\u2264?` n \u219d yes z\u2264n\n s\u2264?z : \u2200 {m} \u2192 suc m `\u2264?` zero \u219d no \u03bb()\n s\u2264?s-yes : \u2200 {m n m\u2264n} \u2192 m `\u2264?` n \u219d yes m\u2264n \u2192 suc m `\u2264?` suc n \u219d yes (s\u2264s m\u2264n)\n s\u2264?s-no : \u2200 {m n m\u2270n} \u2192 m `\u2264?` n \u219d no m\u2270n \u2192 suc m `\u2264?` suc n \u219d no (m\u2270n \u2218 \u2264-pred)\n\n`\u2264?`-complete : \u2200 m n \u2192 m `\u2264?` n \u219d (m \u2264? n)\n`\u2264?`-complete zero n = z\u2264?n\n`\u2264?`-complete (suc n) zero = {!s\u2264?z!}\n`\u2264?`-complete (suc m) (suc n) with m \u2264? n | `\u2264?`-complete m n\n... | yes q | r = s\u2264?s-yes r\n... | no q | r = s\u2264?s-no {!!}\n-}\n\n_<=_ : (x y : \u2115) \u2192 Bool\nzero <= _ = true\nsuc _ <= zero = false\nsuc m <= suc n = m <= n\n\nmodule <= where\n \u211b : \u2115 \u2192 \u2115 \u2192 Set\n \u211b x y = T (x <= y)\n\n sound : \u2200 m n \u2192 \u211b m n \u2192 m \u2264 n\n sound zero _ _ = z\u2264n\n sound (suc m) (suc n) p = s\u2264s (sound m n p)\n sound (suc m) zero ()\n\n complete : \u2200 {m n} \u2192 m \u2264 n \u2192 \u211b m n\n complete z\u2264n = _\n complete (s\u2264s m\u2264n) = complete m\u2264n\n\n isTotalOrder : IsTotalOrder _\u2261_ \u211b\n isTotalOrder = record { isPartialOrder = isPartialOrder; total = \u03bb x y \u2192 \u228e-map complete complete (\u2115\u2264.total x y) }\n where\n reflexive : \u2200 {i j} \u2192 i \u2261 j \u2192 \u211b i j\n reflexive {i} \u2261.refl = complete (\u2115\u2264.refl {i})\n trans : Transitive \u211b\n trans {x} {y} {z} p q = complete (\u2115\u2264.trans (sound x y p) (sound y z q))\n isPreorder : IsPreorder _\u2261_ \u211b\n isPreorder = record { isEquivalence = \u2261.isEquivalence\n ; reflexive = reflexive\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n antisym : Antisymmetric _\u2261_ \u211b\n antisym {x} {y} p q = \u2115\u2264.antisym (sound x y p) (sound y x q)\n isPartialOrder : IsPartialOrder _\u2261_ \u211b\n isPartialOrder = record { isPreorder = isPreorder; antisym = antisym }\n\n open IsTotalOrder isTotalOrder public\n\n<=-steps\u2032 : \u2200 {x} y \u2192 T (x <= (x + y))\n<=-steps\u2032 {x} y = <=.complete (\u2264-steps\u2032 {x} y)\n\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 : \u2200 x y \u2192 suc x \u2238 y \u2264 suc (x \u2238 y)\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x zero = \u2115\u2264.refl\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 zero (suc y) rewrite 0\u2238n\u22610 y = z\u2264n\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 (suc x) (suc y) = sucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x y\n\nx\u22642y\u2032\u2192x\u2238y\u2264y : \u2200 x y \u2192 x \u2264 2*\u2032 y \u2192 x \u2238 y \u2264 y\nx\u22642y\u2032\u2192x\u2238y\u2264y x zero p = p\nx\u22642y\u2032\u2192x\u2238y\u2264y zero (suc y) p = z\u2264n\nx\u22642y\u2032\u2192x\u2238y\u2264y (suc zero) (suc y) (s\u2264s p) rewrite 0\u2238n\u22610 y = z\u2264n\nx\u22642y\u2032\u2192x\u2238y\u2264y (suc (suc x)) (suc y) (s\u2264s (s\u2264s p))\n = \u2115\u2264.trans (sucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x y) (s\u2264s (x\u22642y\u2032\u2192x\u2238y\u2264y x y p))\n\nx<2y\u2032\u2192x\u2238y \u00aca \u00acb c = \u22a5-elim (p (\u2264-pred c))\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (Props.\u2264-steps 1 1+n\u2264m)\n\nnot<=\u2192< : \u2200 x y \u2192 T (not (x <= y)) \u2192 T (suc y <= x)\nnot<=\u2192< x y p = <=.complete (\u2270\u2192< x y (T'not'\u00ac p \u2218 <=.complete))\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"a5427687a4fde38a5d0017db3fe19a46e49f6ec2","subject":"[ test-suite ] Fixed.","message":"[ test-suite ] Fixed.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOL\/PropositionalLogic\/TheoremsATP.agda","new_file":"src\/fot\/FOL\/PropositionalLogic\/TheoremsATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Propositional logic theorems\n------------------------------------------------------------------------------\n\n{-# OPTIONS --exact-split #-}\n{-# OPTIONS --no-sized-types #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module contains some examples showing the use of the ATPs to\n-- prove theorems from propositional logic.\n\nmodule FOL.PropositionalLogic.TheoremsATP where\n\nopen import FOL.Base\n\n------------------------------------------------------------------------------\n-- We postulate some propositional formulae (which are translated as\n-- 0-ary predicates).\npostulate P Q R : Set\n\n------------------------------------------------------------------------------\n-- The introduction and elimination rules for the intuitionist\n-- propositional connectives are theorems.\n\npostulate\n \u2192I : (P \u2192 Q) \u2192 P \u2192 Q\n \u2192E : (P \u2192 Q) \u2192 P \u2192 Q\n \u2192I' : (P \u2192 Q) \u2192 P \u2192 Q\n \u2192E' : (P \u21d2 Q) \u21d2 P \u21d2 Q\n{-# ATP prove \u2192I #-}\n{-# ATP prove \u2192E #-}\n{-# ATP prove \u2192I' #-}\n{-# ATP prove \u2192E' #-}\n\npostulate\n \u2227I : P \u2192 Q \u2192 P \u2227 Q\n \u2227E\u2081 : P \u2227 Q \u2192 P\n \u2227E\u2082 : P \u2227 Q \u2192 Q\n \u2227I' : P \u21d2 Q \u21d2 P \u2227 Q\n \u2227E\u2082' : P \u2227 Q \u21d2 Q\n \u2227E\u2081' : P \u2227 Q \u21d2 P\n{-# ATP prove \u2227I #-}\n{-# ATP prove \u2227E\u2081 #-}\n{-# ATP prove \u2227E\u2082 #-}\n{-# ATP prove \u2227I' #-}\n{-# ATP prove \u2227E\u2081' #-}\n{-# ATP prove \u2227E\u2082' #-}\n\npostulate\n \u2228I\u2081 : P \u2192 P \u2228 Q\n \u2228I\u2082 : Q \u2192 P \u2228 Q\n \u2228E : (P \u2192 R) \u2192 (Q \u2192 R) \u2192 P \u2228 Q \u2192 R\n \u2228I\u2081' : P \u21d2 P \u2228 Q\n \u2228I\u2082' : Q \u21d2 P \u2228 Q\n \u2228E' : (P \u21d2 R) \u21d2 (Q \u21d2 R) \u21d2 P \u2228 Q \u21d2 R\n{-# ATP prove \u2228I\u2081 #-}\n{-# ATP prove \u2228I\u2082 #-}\n{-# ATP prove \u2228E #-}\n{-# ATP prove \u2228I\u2081' #-}\n{-# ATP prove \u2228I\u2082' #-}\n{-# ATP prove \u2228E' #-}\n\npostulate\n \u22a5E : \u22a5 \u2192 P\n \u22a5E' : \u22a5 \u21d2 P\n{-# ATP prove \u22a5E #-}\n{-# ATP prove \u22a5E' #-}\n\n------------------------------------------------------------------------------\n-- Boolean laws (there are some non-intuitionistic laws)\n\npostulate\n \u2227-ident : P \u2227 \u22a4 \u2194 P\n \u2228-ident : P \u2228 \u22a5 \u2194 P\n \u2227-dom : P \u2227 \u22a5 \u2194 \u22a5\n \u2228-dom : P \u2228 \u22a4 \u2194 \u22a4\n \u2227-idemp : P \u2227 P \u2194 P\n \u2228-idemp : P \u2228 P \u2194 P\n dn : \u00ac \u00ac P \u2194 P\n \u2227-comm : P \u2227 Q \u2192 Q \u2227 P\n \u2228-comm : P \u2228 Q \u2192 Q \u2228 P\n \u2227-assoc : (P \u2227 Q) \u2227 R \u2194 P \u2227 Q \u2227 R\n \u2228-assoc : (P \u2228 Q) \u2228 R \u2194 P \u2228 Q \u2228 R\n \u2227\u2228-dist : P \u2227 (Q \u2228 R) \u2194 P \u2227 Q \u2228 P \u2227 R\n \u2228\u2227-dist : P \u2228 Q \u2227 R \u2194 (P \u2228 Q) \u2227 (P \u2228 R)\n DM\u2081 : \u00ac (P \u2228 Q) \u2194 \u00ac P \u2227 \u00ac Q\n DM\u2082 : \u00ac (P \u2227 Q) \u2194 \u00ac P \u2228 \u00ac Q\n abs\u2081 : P \u2228 P \u2227 Q \u2194 P\n abs\u2082 : P \u2227 (P \u2228 Q) \u2194 P\n \u2227-neg : P \u2227 \u00ac P \u2194 \u22a5\n \u2228-neg : P \u2228 \u00ac P \u2194 \u22a4\n{-# ATP prove \u2227-ident #-}\n{-# ATP prove \u2228-ident #-}\n{-# ATP prove \u2227-dom #-}\n{-# ATP prove \u2228-dom #-}\n{-# ATP prove \u2227-idemp #-}\n{-# ATP prove \u2228-idemp #-}\n{-# ATP prove dn #-}\n{-# ATP prove \u2227-comm #-}\n{-# ATP prove \u2228-comm #-}\n{-# ATP prove \u2227-assoc #-}\n{-# ATP prove \u2228-assoc #-}\n{-# ATP prove \u2227\u2228-dist #-}\n{-# ATP prove \u2228\u2227-dist #-}\n{-# ATP prove DM\u2081 #-}\n{-# ATP prove DM\u2082 #-}\n{-# ATP prove abs\u2081 #-}\n{-# ATP prove abs\u2082 #-}\n{-# ATP prove \u2227-neg #-}\n{-# ATP prove \u2228-neg #-}\n\n------------------------------------------------------------------------------\n-- Theorems related with the definition of logical connectives in\n-- terms of others\n\npostulate\n \u2194-def : (P \u2194 Q) \u2194 (P \u2192 Q) \u2227 (Q \u2192 P)\n \u2192-def : P \u2192 Q \u2194 \u00ac P \u2228 Q\n \u2228\u2081-def : P \u2228 Q \u2194 \u00ac P \u2192 Q\n \u2228\u2082-def : P \u2228 Q \u2194 \u00ac (\u00ac P \u2227 \u00ac Q)\n \u2227-def : P \u2227 Q \u2194 \u00ac (\u00ac P \u2228 \u00ac Q)\n \u00ac-def : \u00ac P \u2194 P \u2192 \u22a5\n \u22a5-def : \u22a5 \u2194 P \u2227 \u00ac P\n \u22a4-def : \u22a4 \u2194 \u00ac \u22a5\n{-# ATP prove \u2194-def #-}\n{-# ATP prove \u2192-def #-}\n{-# ATP prove \u2228\u2081-def #-}\n{-# ATP prove \u2228\u2082-def #-}\n{-# ATP prove \u2227-def #-}\n{-# ATP prove \u00ac-def #-}\n{-# ATP prove \u22a5-def #-}\n{-# ATP prove \u22a4-def #-}\n\n------------------------------------------------------------------------------\n-- Some intuitionistic theorems\n\npostulate \u2192-transposition : (P \u2192 Q) \u2192 \u00ac Q \u2192 \u00ac P\n{-# ATP prove \u2192-transposition #-}\n\npostulate\n i\u2081 : P \u2192 Q \u2192 P\n i\u2082 : (P \u2192 Q \u2192 R) \u2192 (P \u2192 Q) \u2192 P \u2192 R\n i\u2083 : P \u2192 \u00ac \u00ac P\n i\u2084 : \u00ac \u00ac \u00ac P \u2192 \u00ac P\n i\u2085 : ((P \u2227 Q) \u2192 R) \u2194 (P \u2192 (Q \u2192 R))\n i\u2086 : \u00ac \u00ac (P \u2228 \u00ac P)\n i\u2087 : (P \u2228 \u00ac P) \u2192 \u00ac \u00ac P \u2192 P\n{-# ATP prove i\u2081 #-}\n{-# ATP prove i\u2082 #-}\n{-# ATP prove i\u2083 #-}\n{-# ATP prove i\u2084 #-}\n{-# ATP prove i\u2085 #-}\n{-# ATP prove i\u2086 #-}\n{-# ATP prove i\u2087 #-}\n\n------------------------------------------------------------------------------\n-- Some non-intuitionistic theorems\n\npostulate \u2190-transposition : (\u00ac Q \u2192 \u00ac P) \u2192 P \u2192 Q\n{-# ATP prove \u2190-transposition #-}\n\npostulate\n ni\u2081 : ((P \u2192 Q) \u2192 P) \u2192 P\n ni\u2082 : P \u2228 \u00ac P\n ni\u2083 : \u00ac \u00ac P \u2192 P\n ni\u2084 : (P \u2192 Q) \u2192 (\u00ac P \u2192 Q) \u2192 Q\n ni\u2085 : (P \u2228 Q \u2192 P) \u2228 (P \u2228 Q \u2192 Q)\n ni\u2086 : (\u00ac \u00ac P \u2192 P) \u2192 P \u2228 \u00ac P\n{-# ATP prove ni\u2081 #-}\n{-# ATP prove ni\u2082 #-}\n{-# ATP prove ni\u2083 #-}\n{-# ATP prove ni\u2084 #-}\n{-# ATP prove ni\u2085 #-}\n{-# ATP prove ni\u2086 #-}\n\n------------------------------------------------------------------------------\n-- The principle of the excluded middle implies the double negation\n-- elimination\npostulate\n pem\u2192\u00ac\u00ac-elim : (P \u2228 \u00ac P) \u2192 \u00ac \u00ac P \u2192 P\n{-# ATP prove pem\u2192\u00ac\u00ac-elim #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Propositional logic theorems\n------------------------------------------------------------------------------\n\n{-# OPTIONS --exact-split #-}\n{-# OPTIONS --no-sized-types #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module contains some examples showing the use of the ATPs to\n-- prove theorems from propositional logic.\n\nmodule FOL.PropositionalLogic.TheoremsATP where\n\nopen import FOL.Base\n\n------------------------------------------------------------------------------\n-- We postulate some propositional formulae (which are translated as\n-- 0-ary predicates).\npostulate P Q R : Set\n\n------------------------------------------------------------------------------\n-- The introduction and elimination rules for the intuitionist\n-- propositional connectives are theorems.\n\npostulate\n \u2192I : (P \u2192 Q) \u2192 P \u2192 Q\n \u2192E : (P \u2192 Q) \u2192 P \u2192 Q\n \u2192I' : (P \u2192 Q) \u2192 P \u2192 Q\n \u2192E' : (P \u21d2 Q) \u21d2 P \u21d2 Q\n{-# ATP prove \u2192I #-}\n{-# ATP prove \u2192E #-}\n{-# ATP prove \u2192I' #-}\n{-# ATP prove \u2192E' #-}\n\npostulate\n \u2227I : P \u2192 Q \u2192 P \u2227 Q\n \u2227E\u2081 : P \u2227 Q \u2192 P\n \u2227E\u2082 : P \u2227 Q \u2192 Q\n \u2227I' : P \u21d2 Q \u21d2 P \u2227 Q\n \u2227E\u2082' : P \u2227 Q \u21d2 Q\n \u2227E\u2081' : P \u2227 Q \u21d2 P\n{-# ATP prove \u2227I #-}\n{-# ATP prove \u2227E\u2081 #-}\n{-# ATP prove \u2227E\u2082 #-}\n{-# ATP prove \u2227I' #-}\n{-# ATP prove \u2227E\u2081' #-}\n{-# ATP prove \u2227E\u2082' #-}\n\npostulate\n \u2228I\u2081 : P \u2192 P \u2228 Q\n \u2228I\u2082 : Q \u2192 P \u2228 Q\n \u2228E : (P \u2192 R) \u2192 (Q \u2192 R) \u2192 P \u2228 Q \u2192 R\n \u2228I\u2081' : P \u21d2 P \u2228 Q\n \u2228I\u2082' : Q \u21d2 P \u2228 Q\n \u2228E' : (P \u21d2 R) \u21d2 (Q \u21d2 R) \u21d2 P \u2228 Q \u21d2 R\n{-# ATP prove \u2228I\u2081 #-}\n{-# ATP prove \u2228I\u2082 #-}\n{-# ATP prove \u2228E #-}\n{-# ATP prove \u2228I\u2081' #-}\n{-# ATP prove \u2228I\u2082' #-}\n{-# ATP prove \u2228E' #-}\n\npostulate\n \u22a5E : \u22a5 \u2192 P\n \u22a5E' : \u22a5 \u21d2 P\n{-# ATP prove \u22a5E #-}\n{-# ATP prove \u22a5E' #-}\n\n------------------------------------------------------------------------------\n-- Boolean laws (there are some non-intuitionistic laws)\n\npostulate\n \u2227-ident : P \u2227 \u22a4 \u2194 P\n \u2228-ident : P \u2228 \u22a5 \u2194 P\n \u2227-dom : P \u2227 \u22a5 \u2194 \u22a5\n \u2228-dom : P \u2228 \u22a4 \u2194 \u22a4\n \u2227-idemp : P \u2227 P \u2194 P\n \u2228-idemp : P \u2228 P \u2194 P\n dn : \u00ac \u00ac P \u2194 P\n \u2227-comm : P \u2227 Q \u2192 Q \u2227 P\n \u2228-comm : P \u2228 Q \u2192 Q \u2228 P\n \u2227-assoc : (P \u2227 Q) \u2227 R \u2194 P \u2227 Q \u2227 R\n \u2228-assoc : (P \u2228 Q) \u2228 R \u2194 P \u2228 Q \u2228 R\n \u2227\u2228-dist : P \u2227 (Q \u2228 R) \u2194 P \u2227 Q \u2228 P \u2227 R\n \u2228\u2227-dist : P \u2228 Q \u2227 R \u2194 (P \u2228 Q) \u2227 (P \u2228 R)\n DM\u2081 : \u00ac (P \u2228 Q) \u2194 \u00ac P \u2227 \u00ac Q\n DM\u2082 : \u00ac (P \u2227 Q) \u2194 \u00ac P \u2228 \u00ac Q\n abs\u2081 : P \u2228 P \u2227 Q \u2194 P\n abs\u2082 : P \u2227 (P \u2228 Q) \u2194 P\n \u2227-neg : P \u2227 \u00ac P \u2194 \u22a5\n \u2228-neg : P \u2228 \u00ac P \u2194 \u22a4\n{-# ATP prove \u2227-ident #-}\n{-# ATP prove \u2228-ident #-}\n{-# ATP prove \u2227-dom #-}\n{-# ATP prove \u2228-dom #-}\n{-# ATP prove \u2227-idemp #-}\n{-# ATP prove \u2228-idemp #-}\n{-# ATP prove dn #-}\n{-# ATP prove \u2227-comm #-}\n{-# ATP prove \u2228-comm #-}\n{-# ATP prove \u2227-assoc #-}\n{-# ATP prove \u2228-assoc #-}\n{-# ATP prove \u2227\u2228-dist #-}\n{-# ATP prove \u2228\u2227-dist #-}\n{-# ATP prove DM\u2081 #-}\n{-# ATP prove DM\u2082 #-}\n{-# ATP prove abs\u2081 #-}\n{-# ATP prove abs\u2082 #-}\n{-# ATP prove \u2227-neg #-}\n{-# ATP prove \u2228-neg #-}\n\n------------------------------------------------------------------------------\n-- Theorems related with the definition of logical connectives in\n-- terms of others\n\npostulate\n \u2194-def : (P \u2194 Q) \u2194 (P \u2192 Q) \u2227 (Q \u2192 P)\n \u2192-def : P \u2192 Q \u2194 \u00ac P \u2228 Q\n \u2228\u2081-def : P \u2228 Q \u2194 \u00ac P \u2192 Q\n \u2228\u2082-def : P \u2228 Q \u2194 \u00ac (\u00ac P \u2227 \u00ac Q)\n \u2227-def : P \u2227 Q \u2194 \u00ac (\u00ac P \u2228 \u00ac Q)\n \u00ac-def : \u00ac P \u2194 P \u2192 \u22a5\n \u22a5-def : \u22a5 \u2194 P \u2227 \u00ac P\n \u22a4-def : \u22a4 \u2194 \u00ac \u22a5\n{-# ATP prove \u2194-def #-}\n{-# ATP prove \u2192-def #-}\n{-# ATP prove \u2228\u2081-def #-}\n{-# ATP prove \u2228\u2082-def #-}\n{-# ATP prove \u2227-def #-}\n{-# ATP prove \u00ac-def #-}\n{-# ATP prove \u22a5-def #-}\n{-# ATP prove \u22a4-def #-}\n\n------------------------------------------------------------------------------\n-- Some intuitionistic theorems\n\npostulate \u2192-transposition : (P \u2192 Q) \u2192 \u00ac Q \u2192 \u00ac P\n{-# ATP prove \u2192-transposition #-}\n\npostulate\n i\u2081 : P \u2192 Q \u2192 P\n i\u2082 : (P \u2192 Q \u2192 R) \u2192 (P \u2192 Q) \u2192 P \u2192 R\n i\u2083 : P \u2192 \u00ac \u00ac P\n i\u2084 : \u00ac \u00ac \u00ac P \u2192 \u00ac P\n i\u2085 : ((P \u2227 Q) \u2192 R) \u2194 (P \u2192 (Q \u2192 R))\n i\u2086 : \u00ac \u00ac (P \u2228 \u00ac P)\n i\u2087 : (P \u2228 \u00ac P) \u2192 \u00ac \u00ac P \u2192 P\n{-# ATP prove i\u2081 #-}\n{-# ATP prove i\u2082 #-}\n{-# ATP prove i\u2083 #-}\n{-# ATP prove i\u2084 #-}\n{-# ATP prove i\u2085 #-}\n{-# ATP prove i\u2086 #-}\n{-# ATP prove i\u2087 #-}\n\n------------------------------------------------------------------------------\n-- Some non-intuitionistic theorems\n\npostulate \u2190-transposition : (\u00ac Q \u2192 \u00ac P) \u2192 P \u2192 Q\n{-# ATP prove \u2190-transposition #-}\n\npostulate\n ni\u2081 : ((P \u2192 Q) \u2192 P) \u2192 P\n ni\u2082 : P \u2228 \u00ac P\n ni\u2083 : \u00ac \u00ac P \u2192 P\n ni\u2084 : (P \u2192 Q) \u2192 (\u00ac P \u2192 Q) \u2192 Q\n ni\u2085 : (P \u2228 Q \u2192 P) \u2228 (P \u2228 Q \u2192 Q)\n ni\u2086 : (\u00ac \u00ac P \u2192 P) \u2192 P \u2228 \u00ac P\n{-# ATP prove ni\u2081 #-}\n{-# ATP prove ni\u2082 #-}\n{-# ATP prove ni\u2083 #-}\n{-# ATP prove ni\u2084 #-}\n{-# ATP prove ni\u2085 #-}\n{-# ATP prove ni\u2086 #-}\n\n------------------------------------------------------------------------------\n-- The principle of the excluded middle implies the double negation\n-- elimination\npostulate\n pem\u2192\u00ac\u00ac-elim : \u2200 {P} \u2192 (P \u2228 \u00ac P) \u2192 \u00ac \u00ac P \u2192 P\n{-# ATP prove pem\u2192\u00ac\u00ac-elim #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"1876902b81c7e19fc063a50114b2c1354fa59a0c","subject":"agda : wouter \/ PT : cleanup","message":"agda : wouter \/ PT : cleanup\n","repos":"haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc","old_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/wouter-swierstra-predicate-transformers-6fe8f13\/PredicateTransformers.agda","new_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/wouter-swierstra-predicate-transformers-6fe8f13\/PredicateTransformers.agda","new_contents":"module PredicateTransformers where\n\nopen import Prelude hiding (map; all)\nopen import Level hiding (lift)\n\n------------------------------------------------------------------------------\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\n------------------------------------------------------------------------------\nABSTRACT\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nrefinement type https:\/\/en.wikipedia.org\/wiki\/Refinement_(computing)\n- type with a predicate which must hold for any element of the refined type\n- can express\n - preconditions when used as function args\n - postconditions when used as return types\n\nthe problem statement is centered around \/compositionality\/\n- pure functions enable compositional reasoning\n- effectful programs require reasoning about context\n- paper provides mechanism for reasoning about effectful programs (and for program calculation)\n\nmain idea\n- represent effectful computation as a free monad\n- write an \"interpreter\" that computes a predicate transformer\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to use this refinement relation to\n - show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\nmodule Free where\n -- Free : represents syntax of an effectful computation\n -- C : type of commands\n -- Free C R : returns an 'a' or issues command c : C\n -- For each c : C, there is a set of responses R c\n -- - R : C -> Set : is type family of responses for commands\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\" input to output\n -- input\n -- - pure function 'a\u2192b : a -> b'\n -- - postcondition 'b -> Set' (on output of a\u2192b)\n -- output\n -- - weakest precondition 'a -> Set' on input to a\u2192b that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n module WP0Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data isEven : \u2115 \u2192 Set where\n even-z : isEven Nat.zero\n even-s : {n : \u2115} \u2192 isEven n \u2192 isEven (Nat.suc (Nat.suc n))\n\n isEvenWP : \u2115 -> Set\n isEvenWP = wp0 (_+ 2) isEven\n\n _ : isEvenWP \u2261 isEven \u2218 (_+ 2)\n _ = refl\n\n _ : Set\n _ = isEvenWP 5\n\n _ : isEvenWP 5 \u2261 isEven 7\n _ = refl\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - enable via making dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n -- every element a that satisfies P is contained in the set of things that satifies Q\n --\n -- implication between predicates\n --\n -- P a says a \u2208 P\n -- if the extents of P, Q : A \u2192 Set is taken to be\n -- the subset of A satisfying predicates P, Q\n --\n -- the extent of P : A -> Set is the subset of terms of type A satisfying P.\n -- \"extent\" indicates a kind of black-box view of things,\n -- e.g., the \"extent\" of a function is the set of its input-output pairs.\n --\n -- https:\/\/en.wikipedia.org\/wiki\/Extension_%28semantics%29\n -- the extension of a concept, idea, or sign consists of the things to which it applies\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n module \u2286Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data _\u2264_ : \u2115 \u2192 \u2115 \u2192 Set where\n z\u2264n : \u2200 {n : \u2115} \u2192 0 \u2264 n\n s\u2264s : \u2200 {m n : \u2115} \u2192 m \u2264 n \u2192 Nat.suc m \u2264 Nat.suc n\n\n \u22641\u2286\u22642 : (_\u2264 1) \u2286 (_\u2264 2)\n -- P \u2286 Q\n -- \u2200 a -> P a -> Q a\n -- (a : \u2115) -> (a \u2264 1) -> (a \u2264 2)\n \u22641\u2286\u22642 Zero _ = z\u2264n\n \u22641\u2286\u22642 (Succ Zero) (s\u2264s _) = s\u2264s z\u2264n\n\n {- cannot be proved\n \u22642\u2286\u22641 : (_\u2264 2) \u2286 (_\u2264 1)\n \u22642\u2286\u22641 Zero _ = z\u2264n\n \u22642\u2286\u22641 (Succ a) (s\u2264s p) = {!!}\n -}\n\n -- refinement relation between PTs : pt1 refined by pt2\n -- (same thing as \u2286, but one level up)\n -- for every post condition, pt2 weakens the precondition\n -- \"weakest\" is the right side : pt2 P\n --\n -- pt1 , pt2 : preconditions for satisfying a program\n -- refinement : pt1 maybe be a stronger requirement than pt2\n --\n -- pt2 is a refinement because it weakens what is required by pt1\n -- e.g., pt1 pt2\n -- >= 20 >= 10\n --\n -- weakening the input assumptions gives a stronger result\n --\n -- contract says establish a pre and I will give you a post, i.e.,\n -- \"stronger\" post condition\n -- or \"weaker\" pre condition\n --\n -- refinement of predicate transformers\n --\n -- use case: relate different implementations of the \"same program\"\n -- pt1 \u2291 pt2 : pt1 is refined by pt2\n -- - if pt1 is understood as giving preconditions which satisfy\n -- postcondition P for some given program f1\n -- then his says the precondition can be \/weakened\/ and still guarantee the postcondition\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g\n -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation to\n - relate PT semantics between programs and specifications\n - show a program satisfies its specification\n - show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n - corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n note : for =Free C R b= programs means generating the same AST\n\n instead, define predicate transformers that give \/interpretations\/\n to a particular choice of command type C and response family R\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public using (_+_; _>_; _*_; s\u2264s; z\u2264n)\n renaming (\u2115 to Nat; zero to Zero; suc to Succ)\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n -- one command\n data C : Set where\n Abort : C -- no continuation\n\n -- response code : the program does not execute further\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n -- BIG-STEP SEMANTICS specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n module \u21d3Test where\n _ : Expr\n _ = Val 3\n _ : Expr\n _ = Div (Val 3) (Val 0)\n\n _ : Val 0 \u21d3 0\n _ = \u21d3Base\n\n _ : Val 3 \u21d3 3\n _ = \u21d3Base\n\n _ : Div (Val 3) (Val 3) \u21d3 1\n _ = \u21d3Step \u21d3Base \u21d3Base\n\n _ : Div (Val 9) (Val 3) \u21d3 3\n _ = \u21d3Step \u21d3Base \u21d3Base\n\n _ : Div (Val 9) (Div (Val 3) (Val 1)) \u21d3 3\n _ = \u21d3Step \u21d3Base (\u21d3Step \u21d3Base \u21d3Base)\n\n _ : Div (Div (Val 9) (Val 1)) (Div (Val 3) (Val 1)) \u21d3 3\n _ = \u21d3Step (\u21d3Step \u21d3Base \u21d3Base) (\u21d3Step \u21d3Base \u21d3Base)\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val n \u27e7 = return n\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\nl ->\n \u27e6 er \u27e7 >>= \\nr ->\n nl \u00f7 nr\n\n module InterpTest where\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n _ : evv \u2261 Pure 3\n _ = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n _ : evd \u2261 Pure 1\n _ = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n _ : evd0 \u2261 Step Abort (\u03bb ())\n _ = refl\n\n -- relate big step semantics to monadic interpreter\n\n -- convert post-condition for a pure function 'f : (x : A) -> b x'\n -- to post-condition on partial functions 'f : (x : A) -> Partial (b x)'\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba -- P holds if operation produces a value\n mustPT _ _ (Step Abort _) = \u22a5 -- if operation fails, there is no result to apply P to\n -- note: could give \u22a4 here, but want to rule out failure\n -- (total correctness)\n\n module MustPTTest where\n _ : Expr -> Nat -> Set\n _ = _\u21d3_\n\n _ : Set\n _ = Val 1 \u21d3 1\n\n _ : Expr -> Partial Nat -> Set\n _ = mustPT _\u21d3_\n\n _ : Set\n _ = mustPT _\u21d3_ (Val 1) (Pure 1)\n\n _ : mustPT _\u21d3_ (Val 1) (Pure 1)\n _ = \u21d3Base\n\n _ : Set\n _ = mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n {- a value of this type cannot be constructed\n _ : mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n _ = {!!}\n -}\n _ : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb n -> Pure 3 >>= _\u00f7 n))\n _ = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n _ : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n _ = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n _ : Set\n _ = mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n\n {-\n to relate big step semantics to monadic interpreter\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - _\u21d3_ relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n\n to call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results P' : Partial b -> Set\n - done via 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so mustPT Abort case returns empty type\n -}\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n module WpPartialTest where\n _ : Expr -> Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n _ = refl\n\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n _ = \u21d3Base\n\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n _ = \u21d3Step \u21d3Base \u21d3Base\n _ : Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n\n {- this type cannot be constructed\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n _ = {!!}\n -}\n _ : Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n\n {-\n wpPartial \u27e6_\u27e7 _\u21d3_ : a predicate on expressions\n - a way to express SUCCESSFUL evaluation of an Expr with intended semantics\n\n \u2200 exprs satisfying this predicate\n - monadic interpreter and\n - relational big-step specification\n must agree on result of evaluation\n\n for a given Expr, satisfaction of predicate means\n - Expr evaluates to a value by big-step operational semantics\n - if interpreter returns a value at all\n then that value agrees with big step semantics\n - if interpreter aborts\n then have a proof of \u22a5 (so, it does not abort)\n which will NOT agree with \u21d3 big-step semantics since \u21d3 does not allow div-by-0\n\n What does this say about correctness of interpreter?\n\n does not specify how many Expr that satisfy that characterization also satisfy wpPartial \u27e6_\u27e7 _\u21d3_\n\n to understand wpPartial \u27e6_\u27e7 _\u21d3_ better\n - define SafeDiv : safety criteria that we believe in\n - prove every expression that satisfies SafeDiv satisfies wpPartial \u27e6_\u27e7 _\u21d3_\n - SafeDiv refines wpPartial \u27e6_\u27e7 _\u21d3_\n - if we gave \u22a4 above, then 'correct' would not say that SafeDiv rules out undefined behavior\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val _) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n module SafeDivTest where\n _ : SafeDiv (Val 3) \u2261 \u22a4\n _ = refl\n _ : SafeDiv (Val 0) \u2261 \u22a4\n _ = refl\n _ : Set\n _ = SafeDiv (Val 0)\n\n _ : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 0) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n _ = refl\n _ : Set\n _ = SafeDiv (Div (Val 3) (Val 3))\n\n _ : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val 0 \u21d3 0) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n _ = refl\n _ : Set\n _ = SafeDiv (Div (Val 3) (Val 0))\n\n {-\n expect : any expr : e for which SafeDiv e holds can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n -- (a : Expr) (SafeDiv a) (wpPartial \u27e6_\u27e7 _\u21d3_ a)\n correct (Val _) _ = \u21d3Base\n correct (Div el er) (er\u21d30\u2192\u22a5 , (sdel , sder))\n with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n ... | Pure _ | Pure Zero | _ | er\u21d30 = magic (er\u21d30\u2192\u22a5 er\u21d30)\n ... | Pure _ | Pure (Succ _) | el\u21d3nl | er\u21d3Snr = \u21d3Step el\u21d3nl er\u21d3Snr\n ... | Pure _ | Step Abort _ | _ | ()\n ... | Step Abort _ | _ | () | _\n\n module CorrectTest where\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n _ = \u21d3Base\n\n _ : Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 1))\n _ = \u21d3Step \u21d3Base \u21d3Base\n {-\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 0))\n _ = {!!}\n -}\n {-\n Generalization.\n Instead of manually defining SafeDiv,\n define more general predicate\n characterising the domain of a partial function using wpPartial.\n -}\n\n -- domain is well-defined Exprs (i.e., no div-by-0)\n -- returns \u22a4 on Pure; \u22a5 on Step Abort ...\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n module DomTest where\n _ : dom \u27e6_\u27e7 \u2261 \u03bb z \u2192 mustPT (\u03bb x _ \u2192 \u22a4' zero) z \u27e6 z \u27e7\n _ = refl\n _ : dom \u27e6_\u27e7 (Val 0) \u2261 \u22a4' zero\n _ = refl\n _ : dom \u27e6_\u27e7 (Div (Val 1) (Val 0)) \u2261 \u22a5\n _ = refl\n\n -- can show that the two semantics agree precisely on the domain of the interpreter:\n\n -- everything in domain of interpreter\n -- gets evaluated in agreement with big step semantics\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n -- only things interpreted in agreement with big step semantics\n -- are those that are in domain of interpreter\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on argument expression\n\n sound (Val _) _ = \u21d3Base\n sound (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div _ _) () | Pure _ | Pure Zero | _ | _\n sound (Div _ _) _ | Pure nl | Pure (Succ nr) | \u22a4'zero\u2192el\u21d3nl | \u22a4'zero\u2192er\u21d3Succnr\n = \u21d3Step (\u22a4'zero\u2192el\u21d3nl tt) (\u22a4'zero\u2192er\u21d3Succnr tt)\n sound (Div _ _) () | Pure _ | Step Abort _ | _ | _\n sound (Div _ _) () | Step Abort _ | _ | _ | _\n\n inDom : {n : Nat}\n -> (e : Expr)\n -> \u27e6 e \u27e7 == Pure n\n -> dom \u27e6_\u27e7 e\n inDom (Val _) _ = tt\n inDom (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div _ _) () | Pure _ | Pure Zero\n inDom (Div _ _) _ | Pure _ | Pure (Succ _) = tt\n inDom (Div _ _) () | Pure _ | Step Abort _\n inDom (Div _ _) () | Step Abort _ | _\n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n : (e : Expr) (n : Nat)\n -> \u27e6 e \u27e7 \u2261 Pure n\n -> e \u21d3 n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n e _ eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} Diveler\u21d3_nldivSuccn\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 { _} { _} () | Pure _ | _ | Pure Zero\n wpPartial1 {el} { _} _ | Pure nl | [[[ eqnl ]]] | Pure (Succ _) = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n el nl eqnl\n wpPartial1 { _} { _} () | Pure _ | _ | Step Abort _\n wpPartial1 { _} { _} () | Step Abort _ | _ | _\n\n wpPartial2 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} _ with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 { _} {er} _ | Pure _ | _ | Pure nr | [[[ eqnr ]]] = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n er nr eqnr\n wpPartial2 { _} { _} () | Pure _ | _ | Step Abort _ | _\n wpPartial2 { _} { _} () | Step Abort _ | _ | _ | _\n\n complete (Val _) _ = tt\n complete (Div el er) h\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div _ _) h | Pure nl | [[[ \u27e6el\u27e7\u2261Purenl ]]] | Pure Zero | [[[ \u27e6er\u27e7\u2261Pure0 ]]] | _ | _\n -- Goal\n -- mustPT (\u03bb _ _ \u2192 \u22a4' zero)\n -- (Div el er) (Pure nl >>= (\u03bb nl' \u2192 Pure Zero >>= _\u00f7_ nl'))\n -- \u22a5\n -- Context\n -- h : mustPT _\u21d3_ (Div el er) (\u27e6 el \u27e7 >>= (\u03bb nl' \u2192 \u27e6 er \u27e7 >>= _\u00f7_ nl'))\n rewrite\n \u27e6el\u27e7\u2261Purenl -- h : mustPT _\u21d3_ (Div el er) (\u27e6 er \u27e7 >>= _\u00f7_ nl)\n | \u27e6er\u27e7\u2261Pure0 -- h : \u22a5\n = h -- magic h\n complete (Div _ _) _ | Pure _ | _ | Pure (Succ _) | _ | _ | _ = tt\n complete (Div _ _) _ | Pure _ | _ | Step Abort _ | _ | _ | ()\n complete (Div _ _) _ | Step Abort _ | _ | _ | _ | () | _\n\n module CompleteTest where\n {-\n _ : mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n _ = {!!}\n -}\n _ : Set\n _ = mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) <-> (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement\n - to relate Kleisli morphisms\n - to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n this example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre and post condition\n - to its implementation\n\n add top two elements; fails if stack has too few elements\n\n show how to prove definition meets its specification\n -}\n\n -- define specification in terms of a pre\/post condition\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n -- [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n -- for non-dependent examples (e.g., type b does not depend on x : a)\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function (to discard unused arg of type a)\n\n -- describes desired postcondition for addition function\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n -- spec for addition function\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n -- pre post\n {-\n need to relate spec to an implementation\n 'wpPartial' assigns predicate transformer semantics to functions\n 'wpSpec' assigns predicate transformer semantics to specifications\n -}\n -- given a spec, Spec a b\n -- computes weakest precondition that satisfies an arbitrary postcondition P\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\a -> (pre a) -- i.e., spec\u2019s precondition should hold and\n \u2227 (post a \u2286 P a) -- spec's postcondition must imply P\n\n -- using 'wpSpec' can now find a program 'add' that \"refines\" 'addSpec'\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs0 =\n pop xs0 >>= \\{(x1 , xs1) ->\n pop xs1 >>= \\{(x2 , xs2) ->\n return ((x1 + x2) :: xs2)}}\n\n -- verify correct\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd _ (_ :: Nil) (s\u2264s () , _)\n correctnessAdd P a@(x :: y :: xs ) (s\u2264s (s\u2264s z\u2264n) , post_addSpec_a_\u2286P_a)\n -- wpPartial add P (x :: y :: xs)\n = post_addSpec_a_\u2286P_a (x + y :: xs) AddStep\n -- paper version has \"extra\" 'Nil\" case\n--correctnessAdd P Nil ( () , _)\n--correctnessAdd P (_ :: Nil) (s\u2264s () , _)\n--correctnessAdd P (x :: y :: xs ) ( _ , H) = H (x + y :: xs) AddStep\n\n {-\n repeat: this example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition\n - to its implementation\n\n compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n get : State s\n get = Step Get return\n\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n statePT : forall {l l'} -> {b : Set l} -> (b \u00d7 s -> Set l') -> State b -> (s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> ( a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> ( a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s') = (flatten t' == (seq (s) (size t))) \u2227 (s + size t == s')\n\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n correctnessRelabel : forall {a : Set} -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set} -> (c : State a) (f : a -> State b) ->\n \u2200 i P -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification,\n -- by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence,\n -- specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b} (mx : State a) (f : a -> State b) P i ->\n statePT (wpState f \\_ -> P) mx i -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b} (mx : State a) (f : a -> State b) spec ->\n \u2200 P i -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i) ->\n Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P)) ->\n statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s} -> SpecK {zero} (a \u00d7 s) (b \u00d7 s) -> a -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a) (trans\n (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr ->\n Pair (fl \u2261 seq s sl) (s + sl \u2261 s') ->\n Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'') ->\n Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s -> wpSpec relabelSpec P (t , s) -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set} ->\n ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set} -> (c : Free C R a) (f : a -> Free C R b) ->\n \u2200 P -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P =\n cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R} -> (a -> Free C R b) -> (b -> Free C R c) -> a -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set} -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c) ->\n wpCR f1 \u2291 wpCR f2 ->\n wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","old_contents":"module PredicateTransformers where\n\nopen import Prelude hiding (map; all)\nopen import Level hiding (lift)\n\n------------------------------------------------------------------------------\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\n------------------------------------------------------------------------------\nABSTRACT\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nrefinement type https:\/\/en.wikipedia.org\/wiki\/Refinement_(computing)\n- type with a predicate which must hold for any element of the refined type\n- can express\n - preconditions when used as function args\n - postconditions when used as return types\n\nthe problem statement is centered around \/compositionality\/\n- pure functions enable compositional reasoning\n- effectful programs require reasoning about context\n- paper provides mechanism for reasoning about effectful programs (and for program calculation)\n\nmain idea\n- represent effectful computation as a free monad\n- write an \"interpreter\" that computes a predicate transformer\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to use this refinement relation to\n - show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\nmodule Free where\n -- Free : represents syntax of an effectful computation\n -- C : type of commands\n -- Free C R : returns an 'a' or issues command c : C\n -- For each c : C, there is a set of responses R c\n -- - R : C -> Set : is type family of responses for commands\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\" input to output\n -- input\n -- - pure function 'a\u2192b : a -> b'\n -- - postcondition 'b -> Set' (on output of a\u2192b)\n -- output\n -- - weakest precondition 'a -> Set' on input to a\u2192b that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n module WP0Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data isEven : \u2115 \u2192 Set where\n even-z : isEven Nat.zero\n even-s : {n : \u2115} \u2192 isEven n \u2192 isEven (Nat.suc (Nat.suc n))\n\n isEvenPT : \u2115 -> Set\n isEvenPT = wp0 (_+ 2) isEven\n\n isEven' : isEvenPT \u2261 isEven \u2218 (_+ 2)\n isEven' = refl\n\n isEven5 : Set\n isEven5 = isEvenPT 5\n\n isEven5' : isEvenPT 5 \u2261 isEven 7\n isEven5' = refl\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - enable via making dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n -- every element a that satisfies P is contained in the set of things that satifies Q\n --\n -- implication between predicates\n --\n -- P a says a \u2208 P\n -- if the extents of P, Q : A \u2192 Set is taken to be\n -- the subset of A satisfying predicates P, Q\n --\n -- the extent of P : A -> Set is the subset of terms of type A satisfying P.\n -- \"extent\" indicates a kind of black-box view of things,\n -- e.g., the \"extent\" of a function is the set of its input-output pairs.\n --\n -- https:\/\/en.wikipedia.org\/wiki\/Extension_%28semantics%29\n -- the extension of a concept, idea, or sign consists of the things to which it applies\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n module \u2286Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data _\u2264_ : \u2115 \u2192 \u2115 \u2192 Set where\n z\u2264n : \u2200 {n : \u2115} \u2192 0 \u2264 n\n s\u2264s : \u2200 {m n : \u2115} \u2192 m \u2264 n \u2192 Nat.suc m \u2264 Nat.suc n\n\n \u22641\u2286\u22642 : (_\u2264 1) \u2286 (_\u2264 2)\n -- P \u2286 Q\n -- \u2200 a -> P a -> Q a\n -- (a : \u2115) -> (a \u2264 1) -> (a \u2264 2)\n \u22641\u2286\u22642 Zero _ = z\u2264n\n \u22641\u2286\u22642 (Succ Zero) (s\u2264s _) = s\u2264s z\u2264n\n\n {- cannot be proved\n \u22642\u2286\u22641 : (_\u2264 2) \u2286 (_\u2264 1)\n \u22642\u2286\u22641 Zero _ = z\u2264n\n \u22642\u2286\u22641 (Succ a) (s\u2264s p) = {!!}\n -}\n\n -- refinement relation between PTs : pt1 refined by pt2\n --\n -- (same thing as \u2286, but one level up)\n -- for every post condition, pt2 weakens the precondition\n -- \"weakest\" is the right side : pt2 P\n --\n -- pt1 , pt2 : preconditions for satisfying a program\n -- refinement : pt1 maybe be a stronger requirement than pt2\n --\n -- pt2 is a refinement because it weakens what is required by pt1\n -- e.g., pt1 pt2\n -- >= 20 >= 10\n --\n -- weakening the input assumptions gives a stronger result\n --\n -- contract says establish a pre and I will give you a post, i.e.,\n -- \"stronger\" post condition\n -- or \"weaker\" pre condition\n --\n -- refinement of predicate transformers\n --\n -- use case: relate different implementations of the \"same program\"\n -- pt1 \u2291 pt2 : pt1 is refined by pt2\n -- - if pt1 is understood as giving preconditions which satisfy\n -- postcondition P for some given program f1\n -- then his says the precondition can be \/weakened\/ and still guarantee the postcondition\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g\n -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation to\n - relate PT semantics between programs and specifications\n - show a program satisfies its specification\n - show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n - corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n note : for =Free C R b= programs means generating the same AST\n\n instead, define predicate transformers that give \/interpretations\/\n to a particular choice of command type C and response family R\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public using (_+_; _>_; _*_)\n renaming (\u2115 to Nat; zero to Zero; suc to Succ)\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n -- one command\n data C : Set where\n Abort : C -- no continuation\n\n -- response code : the program does not execute further\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n -- BIG-STEP SEMANTICS specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n module \u21d3Test where\n exv : Expr\n exv = Val 3\n exd : Expr\n exd = Div (Val 3) (Val 0)\n\n exb0 : Val 0 \u21d3 0\n exb0 = \u21d3Base\n\n exb3 : Val 3 \u21d3 3\n exb3 = \u21d3Base\n\n exs331 : Div (Val 3) (Val 3) \u21d3 1\n exs331 = \u21d3Step \u21d3Base \u21d3Base\n\n exs931 : Div (Val 9) (Val 3) \u21d3 3\n exs931 = \u21d3Step \u21d3Base \u21d3Base\n\n exs9d31 : Div (Val 9) (Div (Val 3) (Val 1)) \u21d3 3\n exs9d31 = \u21d3Step \u21d3Base (\u21d3Step \u21d3Base \u21d3Base)\n\n exsd91d31 : Div (Div (Val 9) (Val 1)) (Div (Val 3) (Val 1)) \u21d3 3\n exsd91d31 = \u21d3Step (\u21d3Step \u21d3Base \u21d3Base) (\u21d3Step \u21d3Base \u21d3Base)\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val n \u27e7 = return n\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\nl ->\n \u27e6 er \u27e7 >>= \\nr ->\n nl \u00f7 nr\n\n module InterpTest where\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n evv' : evv \u2261 Pure 3\n evv' = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n evd' : evd \u2261 Pure 1\n evd' = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n evd0' : evd0 \u2261 Step Abort (\u03bb ())\n evd0' = refl\n\n -- relate big step semantics to monadic interpreter\n\n -- convert post-condition for a pure function 'f : (x : A) -> b x'\n -- to post-condition on partial functions 'f : (x : A) -> Partial (b x)'\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba -- P holds if operation produces a value\n mustPT _ _ (Step Abort _) = \u22a5 -- if operation fails, there is no result to apply P to\n -- note: could give \u22a4 here, but want to rule out failure\n -- (total correctness)\n\n module MustPTTest where\n bs\u21d3 : Expr -> Nat -> Set\n bs\u21d3 = _\u21d3_\n\n bs\u21d3' : Set\n bs\u21d3' = Val 1 \u21d3 1\n\n bsp\u21d3 : Expr -> Partial Nat -> Set\n bsp\u21d3 = mustPT _\u21d3_\n\n bsp\u21d3' : Set\n bsp\u21d3' = mustPT _\u21d3_ (Val 1) (Pure 1)\n\n bsp\u21d3'x : mustPT _\u21d3_ (Val 1) (Pure 1)\n bsp\u21d3'x = \u21d3Base\n\n bsp\u21d3'' : Set\n bsp\u21d3'' = mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n {- nothing can be constructed with this type\n bsp\u21d3''x : mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n bsp\u21d3''x = {!!}\n -}\n xxx : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb n -> Pure 3 >>= _\u00f7 n))\n xxx = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx' : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n xxx' = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx'' : Set\n xxx'' = mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n\n {-\n to relate big step semantics to monadic interpreter\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - _\u21d3_ relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n\n to call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results P' : Partial b -> Set\n - done via 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so mustPT Abort case returns empty type\n -}\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n module WpPartialTest where\n exwpp : Expr -> Set\n exwpp = wpPartial \u27e6_\u27e7 _\u21d3_\n exwpp' : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n exwpp' = refl\n\n wppv1 : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n wppv1 = \u21d3Base\n\n wppd11 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n wppd11 = \u21d3Step \u21d3Base \u21d3Base\n wppd11' : Set\n wppd11' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n\n {- this type cannot be constructed\n wppd10 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n wppd10 = {!!}\n -}\n wppd10' : Set\n wppd10' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n\n {-\n wpPartial \u27e6_\u27e7 _\u21d3_ : a predicate on expressions\n - a way to express SUCCESSFUL evaluation of an Expr with intended semantics\n\n \u2200 exprs satisfying this predicate\n - monadic interpreter and\n - relational big-step specification\n must agree on result of evaluation\n\n for a given Expr, satisfaction of predicate means\n - Expr evaluates to a value by big-step operational semantics\n - if interpreter returns a value at all\n then that value agrees with big step semantics\n - if interpreter aborts\n then have a proof of \u22a5 (so, it does not abort)\n which will NOT agree with \u21d3 big-step semantics since \u21d3 does not allow div-by-0\n\n What does this say about correctness of interpreter?\n\n does not specify how many Expr that satisfy that characterization also satisfy wpPartial \u27e6_\u27e7 _\u21d3_\n\n to understand wpPartial \u27e6_\u27e7 _\u21d3_ better\n - define SafeDiv : safety criteria that we believe in\n - prove every expression that satisfies SafeDiv satisfies wpPartial \u27e6_\u27e7 _\u21d3_\n - SafeDiv refines wpPartial \u27e6_\u27e7 _\u21d3_\n - if we gave \u22a4 above, then 'correct' would not say that SafeDiv rules out undefined behavior\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val _) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n module SafeDivTest where\n exsdv3 : SafeDiv (Val 3) \u2261 \u22a4\n exsdv3 = refl\n exsdv0 : SafeDiv (Val 0) \u2261 \u22a4\n exsdv0 = refl\n exsdv0' : Set\n exsdv0' = SafeDiv (Val 0)\n\n exsd33 : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd33 = refl\n exsd33' : Set\n exsd33' = SafeDiv (Div (Val 3) (Val 3))\n\n exsd30 : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val Zero \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd30 = refl\n exsd30' : Set\n exsd30' = SafeDiv (Div (Val 3) (Val 0))\n\n {-\n expect : any expr : e for which SafeDiv e holds can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n -- (a : Expr) (SafeDiv a) (wpPartial \u27e6_\u27e7 _\u21d3_ a)\n correct (Val _) _ = \u21d3Base\n correct (Div el er) (er\u21d30\u2192\u22a5 , (sdel , sder))\n with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n ... | Pure _ | Pure Zero | _ | er\u21d30 = magic (er\u21d30\u2192\u22a5 er\u21d30)\n ... | Pure _ | Pure (Succ _) | el\u21d3nl | er\u21d3Snr = \u21d3Step el\u21d3nl er\u21d3Snr\n ... | Pure _ | Step Abort _ | _ | ()\n ... | Step Abort _ | _ | () | _\n\n module CorrectTest where\n xxx : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n xxx = \u21d3Base\n\n xxx' : Set\n xxx' = wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n\n yyy : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 1))\n yyy = \u21d3Step \u21d3Base \u21d3Base\n {-\n zzz : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 0))\n zzz = {!!}\n -}\n {-\n Generalization.\n Instead of manually defining SafeDiv,\n define more general predicate\n characterising the domain of a partial function using wpPartial.\n -}\n\n -- returns \u22a4 on Pure; \u22a5 on Step Abort ...\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n module DomTest where\n domIs : dom \u27e6_\u27e7 \u2261 \u03bb z \u2192 mustPT (\u03bb x _ \u2192 \u22a4' zero) z \u27e6 z \u27e7\n domIs = refl\n domIs' : dom \u27e6_\u27e7 (Val 0) \u2261 \u22a4' zero\n domIs' = refl\n domIs'' : dom \u27e6_\u27e7 (Div (Val 1) (Val 0)) \u2261 \u22a5\n domIs'' = refl\n\n -- can show that the two semantics agree precisely on the domain of the interpreter:\n\n -- everything in domain of interpreter\n -- gets evaluated in agreement with big step semantics\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n -- only things interpreted in agreement with big step semantics\n -- are those that are in domain of interpreter\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on argument expression\n\n sound (Val _) _ = \u21d3Base\n sound (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div _ _) () | Pure _ | Pure Zero | _ | _\n sound (Div _ _) _ | Pure nl | Pure (Succ nr) | \u22a4'zero\u2192el\u21d3nl | \u22a4'zero\u2192er\u21d3Succnr\n = \u21d3Step (\u22a4'zero\u2192el\u21d3nl tt) (\u22a4'zero\u2192er\u21d3Succnr tt)\n sound (Div _ _) () | Pure _ | Step Abort _ | _ | _\n sound (Div _ _) () | Step Abort _ | _ | _ | _\n\n inDom : {n : Nat}\n -> (e : Expr)\n -> \u27e6 e \u27e7 == Pure n\n -> dom \u27e6_\u27e7 e\n inDom (Val _) _ = tt\n inDom (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div _ _) () | Pure _ | Pure Zero\n inDom (Div _ _) _ | Pure _ | Pure (Succ _) = tt\n inDom (Div _ _) () | Pure _ | Step Abort _\n inDom (Div _ _) () | Step Abort _ | _\n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n : (e : Expr) (n : Nat)\n -> \u27e6 e \u27e7 \u2261 Pure n\n -> e \u21d3 n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n e _ eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} Diveler\u21d3_nldivSuccn\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 { _} { _} () | Pure _ | _ | Pure Zero\n wpPartial1 {el} { _} _ | Pure nl | [[[ eqnl ]]] | Pure (Succ _) = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n el nl eqnl\n wpPartial1 { _} { _} () | Pure _ | _ | Step Abort _\n wpPartial1 { _} { _} () | Step Abort _ | _ | _\n\n wpPartial2 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} _ with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 { _} {er} _ | Pure _ | _ | Pure nr | [[[ eqnr ]]] = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n er nr eqnr\n wpPartial2 { _} { _} () | Pure _ | _ | Step Abort _ | _\n wpPartial2 { _} { _} () | Step Abort _ | _ | _ | _\n\n complete (Val _) _ = tt\n complete (Div el er) h\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div _ _) h | Pure nl | [[[ \u27e6el\u27e7\u2261Purenl ]]] | Pure Zero | [[[ \u27e6er\u27e7\u2261Pure0 ]]] | _ | _\n -- Goal\n -- mustPT (\u03bb _ _ \u2192 \u22a4' zero)\n -- (Div el er) (Pure nl >>= (\u03bb nl' \u2192 Pure Zero >>= _\u00f7_ nl'))\n -- \u22a5\n -- Context\n -- h : mustPT _\u21d3_ (Div el er) (\u27e6 el \u27e7 >>= (\u03bb nl' \u2192 \u27e6 er \u27e7 >>= _\u00f7_ nl'))\n rewrite\n \u27e6el\u27e7\u2261Purenl -- h : mustPT _\u21d3_ (Div el er) (\u27e6 er \u27e7 >>= _\u00f7_ nl)\n | \u27e6er\u27e7\u2261Pure0 -- h : \u22a5\n = h -- magic h\n complete (Div _ _) _ | Pure _ | _ | Pure (Succ _) | _ | _ | _ = tt\n complete (Div _ _) _ | Pure _ | _ | Step Abort _ | _ | _ | ()\n complete (Div _ _) _ | Step Abort _ | _ | _ | _ | () | _\n\n module CompleteTest where\n {-\n xxx : mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n xxx = {!!}\n -}\n xxx' : Set\n xxx' = mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement to relate Kleisli morphisms,\n and to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n add top two elements; can fail fail if stack has too few elements\n\n below shows how to prove the definition meets its specification\n\n Define specification in terms of a pre\/post condition.\n -}\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n {-\n [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n for non-dependent examples (e.g., type b does not depend on x : a)\n -}\n\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function\n\n -- specification for addition function : describes the desired postcondition\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n\n {-\n How to relate this specification to an implementation?\n Note: 'wpPartial' assigns predicate transformer semantics to functions\n - but do not yet have a corresponding predicate transform semantics for specifications.\n wpSpec function does that:\n -}\n\n -- Given a specification, Spec a b\n -- computes the weakest precondition that satisfies an arbitrary postcondition P\n -- i.e., the spec\u2019s precondition should hold and its postcondition must imply P.\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\x -> (pre x) \u2227 (post x \u2286 P x)\n\n -- Can now formulate program\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs =\n pop xs >>= \\{(x1 , xs) ->\n pop xs >>= \\{(x2 , xs) ->\n return ((x1 + x2) :: xs)}}\n\n -- that refines the specification given by addSpec:\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd P Nil (() , _)\n correctnessAdd P (x :: Nil) (Data.Nat.s\u2264s () , _)\n correctnessAdd P (x :: y :: xs) (_ , H) = H (x + y :: xs) AddStep\n\n {-\n This example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition,\n - to its implementation.\n\n Compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n get : State s\n get = Step Get return\n\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n statePT : forall {l l'} -> {b : Set l} -> (b \u00d7 s -> Set l') -> State b -> (s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> (a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> (a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s') = (flatten t' == (seq (s) (size t))) \u2227 (s + size t == s')\n\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n correctnessRelabel : forall {a : Set} -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set} -> (c : State a) (f : a -> State b) ->\n \u2200 i P -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification,\n -- by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence,\n -- specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b} (mx : State a) (f : a -> State b) P i ->\n statePT (wpState f \\_ -> P) mx i -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b} (mx : State a) (f : a -> State b) spec ->\n \u2200 P i -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i) ->\n Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P)) ->\n statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s} -> SpecK {zero} (a \u00d7 s) (b \u00d7 s) -> a -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a) (trans\n (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr ->\n Pair (fl \u2261 seq s sl) (s + sl \u2261 s') ->\n Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'') ->\n Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s -> wpSpec relabelSpec P (t , s) -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set} ->\n ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set} -> (c : Free C R a) (f : a -> Free C R b) ->\n \u2200 P -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P =\n cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R} -> (a -> Free C R b) -> (b -> Free C R c) -> a -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set} -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c) ->\n wpCR f1 \u2291 wpCR f2 ->\n wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"c5a95702845021cedc3dfa83878c60e6f9654389","subject":"Rename","message":"Rename\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Denotation\/CachingEvaluation.agda","new_file":"Parametric\/Denotation\/CachingEvaluation.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Caching evaluation\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\n\nmodule Parametric.Denotation.CachingEvaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\n\nopen import Base.Denotation.Notation\n\nopen import Relation.Binary.PropositionalEquality\n\nmodule Structure where\n {-\n -- Prototype here the type-correctness of a simple non-standard semantics.\n -- This describes a simplified version of the transformation by Liu and\n -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language\n -- terms, we produce host language terms to take advantage of the richer type\n -- system of the host language (in particular, here we need the unit type,\n -- product types and *existentials*).\n --\n -- As usual, we'll later switch to a real term transformation.\n -}\n open import Data.Product hiding (map)\n open import Data.Sum hiding (map)\n open import Data.Unit\n open import Level\n\n -- Type semantics for this scenario.\n \u27e6_\u27e7TypeHidCache : (\u03c4 : Type) \u2192 Set\u2081\n \u27e6 base \u03b9 \u27e7TypeHidCache = Lift \u27e6 base \u03b9 \u27e7\n \u27e6 \u03c3 \u21d2 \u03c4 \u27e7TypeHidCache = \u27e6 \u03c3 \u27e7TypeHidCache \u2192 (\u03a3[ \u03c4\u2081 \u2208 Set ] \u27e6 \u03c4 \u27e7TypeHidCache \u00d7 \u03c4\u2081 )\n\n open import Parametric.Syntax.MType as MType\n open MType.Structure Base\n open import Parametric.Syntax.MTerm as MTerm\n open MTerm.Structure Const\n\n open import Function hiding (const)\n\n \u27e6_\u27e7ValType : (\u03c4 : ValType) \u2192 Set\n \u27e6_\u27e7CompType : (\u03c4 : CompType) \u2192 Set\n\n \u27e6 U c \u27e7ValType = \u27e6 c \u27e7CompType\n \u27e6 B \u03b9 \u27e7ValType = \u27e6 base \u03b9 \u27e7\n \u27e6 vUnit \u27e7ValType = \u22a4\n \u27e6 \u03c4\u2081 v\u00d7 \u03c4\u2082 \u27e7ValType = \u27e6 \u03c4\u2081 \u27e7ValType \u00d7 \u27e6 \u03c4\u2082 \u27e7ValType\n \u27e6 \u03c4\u2081 v+ \u03c4\u2082 \u27e7ValType = \u27e6 \u03c4\u2081 \u27e7ValType \u228e \u27e6 \u03c4\u2082 \u27e7ValType\n\n \u27e6 F \u03c4 \u27e7CompType = \u27e6 \u03c4 \u27e7ValType\n \u27e6 \u03c3 \u21db \u03c4 \u27e7CompType = \u27e6 \u03c3 \u27e7ValType \u2192 \u27e6 \u03c4 \u27e7CompType\n\n -- XXX: below we need to override just a few cases. Inheritance would handle\n -- this precisely; without inheritance, we might want to use one of the\n -- standard encodings of related features (delegation?).\n\n \u27e6_\u27e7ValTypeHidCacheWrong : (\u03c4 : ValType) \u2192 Set\u2081\n \u27e6_\u27e7CompTypeHidCacheWrong : (\u03c4 : CompType) \u2192 Set\u2081\n\n -- This line is the only change, up to now, for the caching semantics starting from CBPV.\n \u27e6 F \u03c4 \u27e7CompTypeHidCacheWrong = (\u03a3[ \u03c4\u2081 \u2208 Set ] \u27e6 \u03c4 \u27e7ValTypeHidCacheWrong \u00d7 \u03c4\u2081 )\n -- Delegation upward.\n \u27e6 \u03c4 \u27e7CompTypeHidCacheWrong = Lift \u27e6 \u03c4 \u27e7CompType\n \u27e6_\u27e7ValTypeHidCacheWrong = Lift \u2218 \u27e6_\u27e7ValType\n -- The above does not override what happens in recursive occurrences.\n\n {-# NO_TERMINATION_CHECK #-}\n \u27e6_\u27e7ValTypeHidCache : (\u03c4 : ValType) \u2192 Set\n \u27e6_\u27e7CompTypeHidCache : (\u03c4 : CompType) \u2192 Set\n\n \u27e6 U c \u27e7ValTypeHidCache = \u27e6 c \u27e7CompTypeHidCache\n \u27e6 B \u03b9 \u27e7ValTypeHidCache = \u27e6 base \u03b9 \u27e7\n \u27e6 vUnit \u27e7ValTypeHidCache = \u22a4\n \u27e6 \u03c4\u2081 v\u00d7 \u03c4\u2082 \u27e7ValTypeHidCache = \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache \u00d7 \u27e6 \u03c4\u2082 \u27e7ValTypeHidCache\n \u27e6 \u03c4\u2081 v+ \u03c4\u2082 \u27e7ValTypeHidCache = \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache \u228e \u27e6 \u03c4\u2082 \u27e7ValTypeHidCache\n\n -- This line is the only change, up to now, for the caching semantics.\n --\n -- XXX The termination checker isn't happy with it, and it may be right \u2500 if\n -- you keep substituting \u03c4\u2081 = U (F \u03c4), you can make the cache arbitrarily big.\n -- I think we don't do that unless we are caching a non-terminating\n -- computation to begin with, but I'm not entirely sure.\n --\n -- However, the termination checker can't prove that the function is\n -- terminating because it's not structurally recursive, but one call of the\n -- function will produce another call of the function stuck on a neutral term:\n -- So the computation will have terminated, just in an unusual way!\n --\n -- Anyway, I need not mechanize this part of the proof for my goals.\n \u27e6 F \u03c4 \u27e7CompTypeHidCache = (\u03a3[ \u03c4\u2081 \u2208 ValType ] \u27e6 \u03c4 \u27e7ValTypeHidCache \u00d7 \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache )\n \u27e6 \u03c3 \u21db \u03c4 \u27e7CompTypeHidCache = \u27e6 \u03c3 \u27e7ValTypeHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n\n \u27e6_\u27e7CtxHidCache : (\u0393 : Context) \u2192 Set\u2081\n \u27e6_\u27e7CtxHidCache = DependentList \u27e6_\u27e7TypeHidCache\n\n \u27e6_\u27e7ValCtxHidCache : (\u0393 : ValContext) \u2192 Set\n \u27e6_\u27e7ValCtxHidCache = DependentList \u27e6_\u27e7ValTypeHidCache\n\n {-\n \u27e6_\u27e7CompCtxHidCache : (\u0393 : CompContext) \u2192 Set\u2081\n \u27e6_\u27e7CompCtxHidCache = DependentList \u27e6_\u27e7CompTypeHidCache\n -}\n\n -- It's questionable that this is not polymorphic enough.\n \u27e6_\u27e7VarHidCache : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7CtxHidCache \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n \u27e6 this \u27e7VarHidCache (v \u2022 \u03c1) = v\n \u27e6 that x \u27e7VarHidCache (v \u2022 \u03c1) = \u27e6 x \u27e7VarHidCache \u03c1\n\n -- Now, let us define a caching semantics for terms.\n\n -- This proves to be hard, because we need to insert and remove caches where\n -- we apply constants.\n\n -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.\n\n --\n -- Inserting and removing caches --\n --\n\n -- Implementation note: The mutual recursion looks like a fold on exponentials, where you need to define the function and the inverse at the same time.\n -- Indeed, both functions seem structurally recursive on \u03c4.\n dropCache : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7TypeHidCache \u2192 \u27e6 \u03c4 \u27e7\n extend : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n\n dropCache {base \u03b9} v = lower v\n dropCache {\u03c3 \u21d2 \u03c4} v x = dropCache (proj\u2081 (proj\u2082 (v (extend x))))\n\n extend {base \u03b9} v = lift v\n extend {\u03c3 \u21d2 \u03c4} v = \u03bb x \u2192 , (extend (v (dropCache x)) , tt)\n\n -- OK, this version is syntax-directed, luckily enough, except on primitives\n -- (as expected). This reveals a bug of ours on higher-order primitives.\n --\n -- Moreover, we can somewhat safely assume that each call to extend and to\n -- dropCache is bad: then we see that the handling of constants is bad. That's\n -- correct, because constants will not return intermediate results in this\n -- schema :-(.\n \u27e6_\u27e7TermCache : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7CtxHidCache \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n \u27e6_\u27e7TermCache (const c args) \u03c1 = extend (\u27e6 const c args \u27e7 (map dropCache \u03c1))\n \u27e6_\u27e7TermCache (var x) \u03c1 = \u27e6 x \u27e7VarHidCache \u03c1\n\n -- It seems odd (a probable bug?) that the result of t needn't be stripped of\n -- its cache.\n \u27e6_\u27e7TermCache (app s t) \u03c1 = proj\u2081 (proj\u2082 ((\u27e6 s \u27e7TermCache \u03c1) (\u27e6 t \u27e7TermCache \u03c1)))\n\n -- Provide an empty cache!\n \u27e6_\u27e7TermCache (abs t) \u03c1 x = , (\u27e6 t \u27e7TermCache (x \u2022 \u03c1) , tt)\n\n -- The solution is to distinguish among different kinds of constants. Some are\n -- value constructors (and thus do not return caches), while others are\n -- computation constructors (and thus should return caches). For products, I\n -- believe we will only use the products which are values, not computations\n -- (XXX check CBPV paper for the name).\n \u27e6_\u27e7CompTermCache : \u2200 {\u03c4 \u0393} \u2192 Comp \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n \u27e6_\u27e7ValTermCache : \u2200 {\u03c4 \u0393} \u2192 Val \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n\n open import Base.Denotation.Environment ValType \u27e6_\u27e7ValTypeHidCache public\n using ()\n renaming (\u27e6_\u27e7Var to \u27e6_\u27e7ValVar)\n\n -- This says that the environment does not contain caches... sounds wrong!\n -- Either we add extra variables for the caches, or we store computations in\n -- the environment (but that does not make sense), or we store caches in\n -- values, by acting not on F but on something else (U?).\n\n -- I suspect the plan was to use extra variables; that's annoying to model in\n -- Agda but easier in implementations.\n\n \u27e6 vVar x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7ValVar \u03c1\n \u27e6 vThunk x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7CompTermCache \u03c1\n\n -- The real deal, finally.\n open import UNDEFINED\n -- XXX constants are still a slight mess because I'm abusing CBPV...\n -- (Actually, I just forgot the difference, and believe I had too little clue\n -- when I wrote these constructors... but some of them did make sense).\n \u27e6_\u27e7CompTermCache (cConst c args) \u03c1 = reveal UNDEFINED\n \u27e6_\u27e7CompTermCache (cConstV c args) \u03c1 = reveal UNDEFINED\n \u27e6_\u27e7CompTermCache (cConstV2 c args) \u03c1 = reveal UNDEFINED\n\n -- Also, where are introduction forms for pairs and sums among values? With\n -- them, we should see that we can interpret them without adding a cache.\n\n -- Thunks keep seeming noops.\n \u27e6_\u27e7CompTermCache (cForce x) \u03c1 = \u27e6 x \u27e7ValTermCache \u03c1\n\n -- Here, in an actual implementation, we would return the actual cache with\n -- all variables.\n --\n -- The effect of F is a writer monad of cached values, where the monoid is\n -- (isomorphic to) the free monoid over (\u2203 \u03c4 . \u03c4), but we push the\n -- existentials up when pairing things!\n\n -- That's what we're interpreting computations in. XXX maybe consider using\n -- monads directly. But that doesn't deal with arity.\n \u27e6_\u27e7CompTermCache (cReturn v) \u03c1 = vUnit , (\u27e6 v \u27e7ValTermCache \u03c1 , tt)\n\n -- For this to be enough, a lambda needs to return a produce, not to forward\n -- the underlying one (unless there are no intermediate results). The correct\n -- requirements can maybe be enforced through a linear typing discipline.\n\n {-\n -- Here we'd have a problem with the original into from CBPV, because it does\n -- not require converting expressions to the \"CBPV A-normal form\".\n\n \u27e6_\u27e7CompTermCache (v\u2081 into v\u2082) \u03c1 =\n -- Sequence commands and combine their caches.\n {-\n let (_ , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (_ , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in , (r\u2082 , (c\u2081 ,\u2032 c\u2082))\n -}\n\n -- The code above does not work, because we only guarantee that v\u2082 is\n -- a computation, not that it's an F-computation - v\u2082 could also be function\n -- type or a computation product.\n\n -- We need a restricted CBPV, where these two possibilities are forbidden. If\n -- you want to save something between different lambdas, you need to add an F\n -- U to reflect that. (Double-check the papers which show how to encode arity\n -- using CBPV, it seems that they should find the same problem --- XXX they\n -- don't).\n\n -- Instead, just drop the first cache (XXX WRONG).\n \u27e6 v\u2082 \u27e7CompTermCache (proj\u2081 (proj\u2082 (\u27e6 v\u2081 \u27e7CompTermCache \u03c1)) \u2022 \u03c1)\n -}\n\n -- But if we alter _into_ as described above, composing the caches works!\n -- However, we should not forget we also need to save the new intermediate\n -- result, that is the one produced by the first part of the let.\n \u27e6_\u27e7CompTermCache (_into_ {\u03c3} {\u03c4} v\u2081 v\u2082) \u03c1 =\n let (\u03c4\u2081 , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (\u03c4\u2082 , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in (\u03c3 v\u00d7 \u03c4\u2081 v\u00d7 \u03c4\u2082) , (r\u2082 ,\u2032 ( r\u2081 ,\u2032 c\u2081 ,\u2032 c\u2082))\n\n -- Note the compositionality and luck: we don't need to do anything at the\n -- cReturn time, we just need the nested into to do their job, because as I\n -- said intermediate results are a writer monad.\n --\n -- Q: But then, do we still need to do all the other stuff? IOW, do we still\n -- need to forbid (\u03bb x . y <- f args; g args') and replace it with (\u03bb x . y <-\n -- f args; z <- g args'; z)?\n --\n -- A: One thing we still need is using the monadic version of into for the\n -- same reasons - which makes sense, since it has the type of monadic bind.\n --\n -- Maybe not: if we use a monad, which respects left and right identity, the\n -- two above forms are equivalent. But what about associativity? We don't have\n -- associativity with nested tuples in the middle. That's why the monad uses\n -- lists! We can also use nested tuple, as long as in the into case we don't\n -- do (a, b) but append a b (ahem, where?), which decomposes the first list\n -- and prepends it to the second. To this end, we need to know the type of the\n -- first element, or to ensure it's always a pair. Maybe we just want to reuse\n -- an HList.\n\n -- In abstractions, we should start collecting all variables...\n\n -- Here, unlike in \u27e6_\u27e7TermCache, we don't need to invent an empty cache,\n -- that's moved into the handling of cReturn. This makes *the* difference for\n -- nested lambdas, where we don't need to create caches multiple times!\n\n \u27e6_\u27e7CompTermCache (cAbs v) \u03c1 x = \u27e6 v \u27e7CompTermCache (x \u2022 \u03c1)\n\n -- Here we see that we are in a sort of A-normal form, because the argument is\n -- a value (not quite ANF though, since values can be thunks - that is,\n -- computations which haven't been run yet, I guess. Do we have an use for\n -- that? That allows passing lambdas as arguments directly - which is fine,\n -- because producing a closure indeed does not have intermediate results!).\n \u27e6_\u27e7CompTermCache (cApp t v) \u03c1 = \u27e6 t \u27e7CompTermCache \u03c1 (\u27e6 v \u27e7ValTermCache \u03c1)\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Caching evaluation\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\n\nmodule Parametric.Denotation.CachingEvaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\n\nopen import Base.Denotation.Notation\n\nopen import Relation.Binary.PropositionalEquality\n\nmodule Structure where\n {-\n -- Prototype here the type-correctness of a simple non-standard semantics.\n -- This describes a simplified version of the transformation by Liu and\n -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language\n -- terms, we produce host language terms to take advantage of the richer type\n -- system of the host language (in particular, here we need the unit type,\n -- product types and *existentials*).\n --\n -- As usual, we'll later switch to a real term transformation.\n -}\n open import Data.Product hiding (map)\n open import Data.Sum hiding (map)\n open import Data.Unit\n open import Level\n\n -- Type semantics for this scenario.\n \u27e6_\u27e7TypeHidCache : (\u03c4 : Type) \u2192 Set\u2081\n \u27e6 base \u03b9 \u27e7TypeHidCache = Lift \u27e6 base \u03b9 \u27e7\n \u27e6 \u03c3 \u21d2 \u03c4 \u27e7TypeHidCache = \u27e6 \u03c3 \u27e7TypeHidCache \u2192 (\u03a3[ \u03c4\u2081 \u2208 Set ] \u27e6 \u03c4 \u27e7TypeHidCache \u00d7 \u03c4\u2081 )\n\n open import Parametric.Syntax.MType as MType\n open MType.Structure Base\n open import Parametric.Syntax.MTerm as MTerm\n open MTerm.Structure Const\n\n open import Function hiding (const)\n\n \u27e6_\u27e7ValType : (\u03c4 : ValType) \u2192 Set\n \u27e6_\u27e7CompType : (\u03c4 : CompType) \u2192 Set\n\n \u27e6 U c \u27e7ValType = \u27e6 c \u27e7CompType\n \u27e6 B \u03b9 \u27e7ValType = \u27e6 base \u03b9 \u27e7\n \u27e6 vUnit \u27e7ValType = \u22a4\n \u27e6 \u03c4\u2081 v\u00d7 \u03c4\u2082 \u27e7ValType = \u27e6 \u03c4\u2081 \u27e7ValType \u00d7 \u27e6 \u03c4\u2082 \u27e7ValType\n \u27e6 \u03c4\u2081 v+ \u03c4\u2082 \u27e7ValType = \u27e6 \u03c4\u2081 \u27e7ValType \u228e \u27e6 \u03c4\u2082 \u27e7ValType\n\n \u27e6 F \u03c4 \u27e7CompType = \u27e6 \u03c4 \u27e7ValType\n \u27e6 \u03c3 \u21db \u03c4 \u27e7CompType = \u27e6 \u03c3 \u27e7ValType \u2192 \u27e6 \u03c4 \u27e7CompType\n\n -- XXX: below we need to override just a few cases. Inheritance would handle\n -- this precisely; without inheritance, we might want to use one of the\n -- standard encodings of related features (delegation?).\n\n \u27e6_\u27e7ValTypeHidCacheWrong : (\u03c4 : ValType) \u2192 Set\u2081\n \u27e6_\u27e7CompTypeHidCacheWrong : (\u03c4 : CompType) \u2192 Set\u2081\n\n -- This line is the only change, up to now, for the caching semantics starting from CBPV.\n \u27e6 F \u03c4 \u27e7CompTypeHidCacheWrong = (\u03a3[ \u03c4\u2081 \u2208 Set ] \u27e6 \u03c4 \u27e7ValTypeHidCacheWrong \u00d7 \u03c4\u2081 )\n -- Delegation upward.\n \u27e6 \u03c4 \u27e7CompTypeHidCacheWrong = Lift \u27e6 \u03c4 \u27e7CompType\n \u27e6_\u27e7ValTypeHidCacheWrong = Lift \u2218 \u27e6_\u27e7ValType\n -- The above does not override what happens in recursive occurrences.\n\n {-# NO_TERMINATION_CHECK #-}\n \u27e6_\u27e7ValTypeHidCache : (\u03c4 : ValType) \u2192 Set\n \u27e6_\u27e7CompTypeHidCache : (\u03c4 : CompType) \u2192 Set\n\n \u27e6 U c \u27e7ValTypeHidCache = \u27e6 c \u27e7CompTypeHidCache\n \u27e6 B \u03b9 \u27e7ValTypeHidCache = \u27e6 base \u03b9 \u27e7\n \u27e6 vUnit \u27e7ValTypeHidCache = \u22a4\n \u27e6 \u03c4\u2081 v\u00d7 \u03c4\u2082 \u27e7ValTypeHidCache = \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache \u00d7 \u27e6 \u03c4\u2082 \u27e7ValTypeHidCache\n \u27e6 \u03c4\u2081 v+ \u03c4\u2082 \u27e7ValTypeHidCache = \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache \u228e \u27e6 \u03c4\u2082 \u27e7ValTypeHidCache\n\n -- This line is the only change, up to now, for the caching semantics.\n --\n -- XXX The termination checker isn't happy with it, and it may be right \u2500 if\n -- you keep substituting \u03c4\u2081 = U (F \u03c4), you can make the cache arbitrarily big.\n -- I think we don't do that unless we are caching a non-terminating\n -- computation to begin with, but I'm not entirely sure.\n --\n -- However, the termination checker can't prove that the function is\n -- terminating because it's not structurally recursive, but one call of the\n -- function will produce another call of the function stuck on a neutral term:\n -- So the computation will have terminated, just in an unusual way!\n --\n -- Anyway, I need not mechanize this part of the proof for my goals.\n \u27e6 F \u03c4 \u27e7CompTypeHidCache = (\u03a3[ \u03c4\u2081 \u2208 ValType ] \u27e6 \u03c4 \u27e7ValTypeHidCache \u00d7 \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache )\n \u27e6 \u03c3 \u21db \u03c4 \u27e7CompTypeHidCache = \u27e6 \u03c3 \u27e7ValTypeHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n\n \u27e6_\u27e7CtxHidCache : (\u0393 : Context) \u2192 Set\u2081\n \u27e6_\u27e7CtxHidCache = DependentList \u27e6_\u27e7TypeHidCache\n\n \u27e6_\u27e7ValCtxHidCache : (\u0393 : ValContext) \u2192 Set\n \u27e6_\u27e7ValCtxHidCache = DependentList \u27e6_\u27e7ValTypeHidCache\n\n {-\n \u27e6_\u27e7CompCtxHidCache : (\u0393 : CompContext) \u2192 Set\u2081\n \u27e6_\u27e7CompCtxHidCache = DependentList \u27e6_\u27e7CompTypeHidCache\n -}\n\n -- It's questionable that this is not polymorphic enough.\n \u27e6_\u27e7VarHidCache : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7CtxHidCache \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n \u27e6 this \u27e7VarHidCache (v \u2022 \u03c1) = v\n \u27e6 that x \u27e7VarHidCache (v \u2022 \u03c1) = \u27e6 x \u27e7VarHidCache \u03c1\n\n -- Now, let us define a caching semantics for terms.\n\n -- This proves to be hard, because we need to insert and remove caches where\n -- we apply constants.\n\n -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.\n\n --\n -- Inserting and removing caches --\n --\n\n -- Implementation note: The mutual recursion looks like a fold on exponentials, where you need to define the function and the inverse at the same time.\n -- Indeed, both functions seem structurally recursive on \u03c4.\n dropCache : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7TypeHidCache \u2192 \u27e6 \u03c4 \u27e7\n extend : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n\n dropCache {base \u03b9} v = lower v\n dropCache {\u03c3 \u21d2 \u03c4} v x = dropCache (proj\u2081 (proj\u2082 (v (extend x))))\n\n extend {base \u03b9} v = lift v\n extend {\u03c3 \u21d2 \u03c4} v = \u03bb x \u2192 , (extend (v (dropCache x)) , tt)\n\n -- OK, this version is syntax-directed, luckily enough, except on primitives\n -- (as expected). This reveals a bug of ours on higher-order primitives.\n --\n -- Moreover, we can somewhat safely assume that each call to extend and to\n -- dropCache is bad: then we see that the handling of constants is bad. That's\n -- correct, because constants will not return intermediate results in this\n -- schema :-(.\n \u27e6_\u27e7TermCache2 : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7CtxHidCache \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n \u27e6_\u27e7TermCache2 (const c args) \u03c1 = extend (\u27e6 const c args \u27e7 (map dropCache \u03c1))\n \u27e6_\u27e7TermCache2 (var x) \u03c1 = \u27e6 x \u27e7VarHidCache \u03c1\n\n -- It seems odd (a probable bug?) that the result of t needn't be stripped of\n -- its cache.\n \u27e6_\u27e7TermCache2 (app s t) \u03c1 = proj\u2081 (proj\u2082 ((\u27e6 s \u27e7TermCache2 \u03c1) (\u27e6 t \u27e7TermCache2 \u03c1)))\n\n -- Provide an empty cache!\n \u27e6_\u27e7TermCache2 (abs t) \u03c1 x = , (\u27e6 t \u27e7TermCache2 (x \u2022 \u03c1) , tt)\n\n -- The solution is to distinguish among different kinds of constants. Some are\n -- value constructors (and thus do not return caches), while others are\n -- computation constructors (and thus should return caches). For products, I\n -- believe we will only use the products which are values, not computations\n -- (XXX check CBPV paper for the name).\n \u27e6_\u27e7CompTermCache : \u2200 {\u03c4 \u0393} \u2192 Comp \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n \u27e6_\u27e7ValTermCache : \u2200 {\u03c4 \u0393} \u2192 Val \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n\n open import Base.Denotation.Environment ValType \u27e6_\u27e7ValTypeHidCache public\n using ()\n renaming (\u27e6_\u27e7Var to \u27e6_\u27e7ValVar)\n\n -- This says that the environment does not contain caches... sounds wrong!\n -- Either we add extra variables for the caches, or we store computations in\n -- the environment (but that does not make sense), or we store caches in\n -- values, by acting not on F but on something else (U?).\n\n -- I suspect the plan was to use extra variables; that's annoying to model in\n -- Agda but easier in implementations.\n\n \u27e6 vVar x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7ValVar \u03c1\n \u27e6 vThunk x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7CompTermCache \u03c1\n\n -- The real deal, finally.\n open import UNDEFINED\n -- XXX constants are still a slight mess because I'm abusing CBPV...\n -- (Actually, I just forgot the difference, and believe I had too little clue\n -- when I wrote these constructors... but some of them did make sense).\n \u27e6_\u27e7CompTermCache (cConst c args) \u03c1 = reveal UNDEFINED\n \u27e6_\u27e7CompTermCache (cConstV c args) \u03c1 = reveal UNDEFINED\n \u27e6_\u27e7CompTermCache (cConstV2 c args) \u03c1 = reveal UNDEFINED\n\n -- Also, where are introduction forms for pairs and sums among values? With\n -- them, we should see that we can interpret them without adding a cache.\n\n -- Thunks keep seeming noops.\n \u27e6_\u27e7CompTermCache (cForce x) \u03c1 = \u27e6 x \u27e7ValTermCache \u03c1\n\n -- Here, in an actual implementation, we would return the actual cache with\n -- all variables.\n --\n -- The effect of F is a writer monad of cached values, where the monoid is\n -- (isomorphic to) the free monoid over (\u2203 \u03c4 . \u03c4), but we push the\n -- existentials up when pairing things!\n\n -- That's what we're interpreting computations in. XXX maybe consider using\n -- monads directly. But that doesn't deal with arity.\n \u27e6_\u27e7CompTermCache (cReturn v) \u03c1 = vUnit , (\u27e6 v \u27e7ValTermCache \u03c1 , tt)\n\n -- For this to be enough, a lambda needs to return a produce, not to forward\n -- the underlying one (unless there are no intermediate results). The correct\n -- requirements can maybe be enforced through a linear typing discipline.\n\n {-\n -- Here we'd have a problem with the original into from CBPV, because it does\n -- not require converting expressions to the \"CBPV A-normal form\".\n\n \u27e6_\u27e7CompTermCache (v\u2081 into v\u2082) \u03c1 =\n -- Sequence commands and combine their caches.\n {-\n let (_ , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (_ , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in , (r\u2082 , (c\u2081 ,\u2032 c\u2082))\n -}\n\n -- The code above does not work, because we only guarantee that v\u2082 is\n -- a computation, not that it's an F-computation - v\u2082 could also be function\n -- type or a computation product.\n\n -- We need a restricted CBPV, where these two possibilities are forbidden. If\n -- you want to save something between different lambdas, you need to add an F\n -- U to reflect that. (Double-check the papers which show how to encode arity\n -- using CBPV, it seems that they should find the same problem --- XXX they\n -- don't).\n\n -- Instead, just drop the first cache (XXX WRONG).\n \u27e6 v\u2082 \u27e7CompTermCache (proj\u2081 (proj\u2082 (\u27e6 v\u2081 \u27e7CompTermCache \u03c1)) \u2022 \u03c1)\n -}\n\n -- But if we alter _into_ as described above, composing the caches works!\n -- However, we should not forget we also need to save the new intermediate\n -- result, that is the one produced by the first part of the let.\n \u27e6_\u27e7CompTermCache (_into_ {\u03c3} {\u03c4} v\u2081 v\u2082) \u03c1 =\n let (\u03c4\u2081 , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (\u03c4\u2082 , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in (\u03c3 v\u00d7 \u03c4\u2081 v\u00d7 \u03c4\u2082) , (r\u2082 ,\u2032 ( r\u2081 ,\u2032 c\u2081 ,\u2032 c\u2082))\n\n -- Note the compositionality and luck: we don't need to do anything at the\n -- cReturn time, we just need the nested into to do their job, because as I\n -- said intermediate results are a writer monad.\n --\n -- Q: But then, do we still need to do all the other stuff? IOW, do we still\n -- need to forbid (\u03bb x . y <- f args; g args') and replace it with (\u03bb x . y <-\n -- f args; z <- g args'; z)?\n --\n -- A: One thing we still need is using the monadic version of into for the\n -- same reasons - which makes sense, since it has the type of monadic bind.\n --\n -- Maybe not: if we use a monad, which respects left and right identity, the\n -- two above forms are equivalent. But what about associativity? We don't have\n -- associativity with nested tuples in the middle. That's why the monad uses\n -- lists! We can also use nested tuple, as long as in the into case we don't\n -- do (a, b) but append a b (ahem, where?), which decomposes the first list\n -- and prepends it to the second. To this end, we need to know the type of the\n -- first element, or to ensure it's always a pair. Maybe we just want to reuse\n -- an HList.\n\n -- In abstractions, we should start collecting all variables...\n\n -- Here, unlike in \u27e6_\u27e7TermCache2, we don't need to invent an empty cache,\n -- that's moved into the handling of cReturn. This makes *the* difference for\n -- nested lambdas, where we don't need to create caches multiple times!\n\n \u27e6_\u27e7CompTermCache (cAbs v) \u03c1 x = \u27e6 v \u27e7CompTermCache (x \u2022 \u03c1)\n\n -- Here we see that we are in a sort of A-normal form, because the argument is\n -- a value (not quite ANF though, since values can be thunks - that is,\n -- computations which haven't been run yet, I guess. Do we have an use for\n -- that? That allows passing lambdas as arguments directly - which is fine,\n -- because producing a closure indeed does not have intermediate results!).\n \u27e6_\u27e7CompTermCache (cApp t v) \u03c1 = \u27e6 t \u27e7CompTermCache \u03c1 (\u27e6 v \u27e7ValTermCache \u03c1)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a79e9939c6512db4827fc831f2689871d99cfc26","subject":"Move function-specific comment right before function","message":"Move function-specific comment right before function\n\nCurrently, this seems a comment about the whole module.\n\nOld-commit-hash: c25e918e712496981adfe4c74f94a8d80e8e5052\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Context.agda","new_file":"Base\/Change\/Context.agda","new_contents":"module Base.Change.Context\n {Type : Set}\n (\u0394Type : Type \u2192 Type) where\n\nopen import Base.Syntax.Context Type\n\n-- Transform a context of values into a context of values and\n-- changes.\n\n\u0394Context : Context \u2192 Context\n\u0394Context \u2205 = \u2205\n\u0394Context (\u03c4 \u2022 \u0393) = \u0394Type \u03c4 \u2022 \u03c4 \u2022 \u0394Context \u0393\n\n-- like \u0394Context, but \u0394Type \u03c4 and \u03c4 are swapped\n\u0394Context\u2032 : Context \u2192 Context\n\u0394Context\u2032 \u2205 = \u2205\n\u0394Context\u2032 (\u03c4 \u2022 \u0393) = \u03c4 \u2022 \u0394Type \u03c4 \u2022 \u0394Context\u2032 \u0393\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u227c\u0394\u0393\n","old_contents":"module Base.Change.Context\n {Type : Set}\n (\u0394Type : Type \u2192 Type) where\n\n-- Transform a context of values into a context of values and\n-- changes.\n\nopen import Base.Syntax.Context Type\n\n\u0394Context : Context \u2192 Context\n\u0394Context \u2205 = \u2205\n\u0394Context (\u03c4 \u2022 \u0393) = \u0394Type \u03c4 \u2022 \u03c4 \u2022 \u0394Context \u0393\n\n-- like \u0394Context, but \u0394Type \u03c4 and \u03c4 are swapped\n\u0394Context\u2032 : Context \u2192 Context\n\u0394Context\u2032 \u2205 = \u2205\n\u0394Context\u2032 (\u03c4 \u2022 \u0393) = \u03c4 \u2022 \u0394Type \u03c4 \u2022 \u0394Context\u2032 \u0393\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u227c\u0394\u0393\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"06d988ce4bc3692e4e60e39bdb8f2e1daddb660e","subject":"[WIP] add type annotations to Denotation.Change.Popl14","message":"[WIP] add type annotations to Denotation.Change.Popl14\n\nOld-commit-hash: 90b06722aecf9d0b5c6c69547217cb2c522912be\n","repos":"inc-lc\/ilc-agda","old_file":"Denotation\/Change\/Popl14.agda","new_file":"Denotation\/Change\/Popl14.agda","new_contents":"module Denotation.Change.Popl14 where\n\n-- Changes for Calculus Popl14\n--\n-- Contents\n-- - Mutually recursive concepts: \u0394Val, validity.\n-- Under module Syntax, the corresponding concepts of\n-- \u0394Type and \u0394Context reside in separate files.\n-- Now they have to be together due to mutual recursiveness.\n-- - `diff` and `apply` on semantic values of changes:\n-- they have to be here as well because they are mutually\n-- recursive with validity.\n-- - The lemma diff-is-valid: it has to be here because it is\n-- mutually recursive with `apply`\n-- - The lemma apply-diff: it is mutually recursive with `apply`\n-- and `diff`\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Popl14.Denotation.Value\nopen import Theorem.Groups-Popl14\nopen import Postulate.Extensionality\nopen import Data.Unit\nopen import Data.Product\nopen import Data.Integer\nopen import Structure.Bag.Popl14\n\n---------------\n-- Interface --\n---------------\n\n\u0394Val : Type \u2192 Set\nvalid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\n\ninfixl 6 _\u229e_ _\u229f_ -- as with + - in GHC.Num\n-- apply \u0394v v ::= v \u229e \u0394v\n_\u229e_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n-- diff u v ::= u \u229f v\n_\u229f_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\n\n-- Lemma diff-is-valid\nR[v,u-v] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229f v)\n\n-- Lemma apply-diff\nv+[u-v]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192\n let\n _+_ = _\u229e_ {\u03c4}\n _-_ = _\u229f_ {\u03c4}\n in\n v + (u - v) \u2261 u\n\n--------------------\n-- Implementation --\n--------------------\n\n-- \u0394Val \u03c4 is intended to be the semantic domain for changes of values\n-- of type \u03c4.\n--\n-- \u0394Val \u03c4 is the target of the denotational specification \u27e6_\u27e7\u0394.\n-- Detailed motivation:\n--\n-- https:\/\/github.com\/ps-mr\/ilc\/blob\/184a6291ac6eef80871c32d2483e3e62578baf06\/POPL14\/paper\/sec-formal.tex\n--\n-- \u0394Val : Type \u2192 Set\n\u0394Val (base base-int) = \u2124\n\u0394Val (base base-bag) = Bag\n\u0394Val (\u03c3 \u21d2 \u03c4) = (v : \u27e6 \u03c3 \u27e7) \u2192 (dv : \u0394Val \u03c3) \u2192 valid v dv \u2192 \u0394Val \u03c4\n\n-- _\u229e_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n_\u229e_ {base base-int} n \u0394n = n + \u0394n\n_\u229e_ {base base-bag} b \u0394b = b ++ \u0394b\n_\u229e_ {\u03c3 \u21d2 \u03c4} f \u0394f = \u03bb v \u2192 f v \u229e \u0394f v (v \u229f v) (R[v,u-v] {\u03c3})\n\n-- _\u229f_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\n_\u229f_ {base base-int} m n = m - n\n_\u229f_ {base base-bag} d b = d \\\\ b\n_\u229f_ {\u03c3 \u21d2 \u03c4} g f = \u03bb v \u0394v R[v,\u0394v] \u2192 g (v \u229e \u0394v) \u229f f v\n\n-- valid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\nvalid {base base-int} n \u0394n = \u22a4\nvalid {base base-bag} b \u0394b = \u22a4\nvalid {\u03c3 \u21d2 \u03c4} f \u0394f =\n \u2200 (v : \u27e6 \u03c3 \u27e7) (\u0394v : \u0394Val \u03c3) (R[v,\u0394v] : valid v \u0394v)\n \u2192 valid (f v) (\u0394f v \u0394v R[v,\u0394v])\n -- \u00d7 (f \u229e \u0394f) (v \u229e \u0394v) \u2261 f v \u229e \u0394f v \u0394v R[v,\u0394v]\n \u00d7 (_\u229e_ {\u03c3 \u21d2 \u03c4} f \u0394f) (v \u229e \u0394v) \u2261 f v \u229e \u0394f v \u0394v R[v,\u0394v]\n\n-- v+[u-v]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 v \u229e (u \u229f v) \u2261 u\nv+[u-v]=u {base base-int} {u} {v} = n+[m-n]=m {v} {u}\nv+[u-v]=u {base base-bag} {u} {v} = a++[b\\\\a]=b {v} {u}\nv+[u-v]=u {\u03c3 \u21d2 \u03c4} {u} {v} =\n let\n _+_ = _\u229e_ {\u03c3 \u21d2 \u03c4}\n _-_ = _\u229f_ {\u03c3 \u21d2 \u03c4}\n _+\u2080_ = _\u229e_ {\u03c3}\n _-\u2080_ = _\u229f_ {\u03c3}\n _+\u2081_ = _\u229e_ {\u03c4}\n _-\u2081_ = _\u229f_ {\u03c4}\n in\n ext {-\u27e6 \u03c3 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4 \u27e7-} (\u03bb w \u2192\n begin\n (v + (u - v)) w\n \u2261\u27e8 refl \u27e9\n v w +\u2081 (u (w +\u2080 (w -\u2080 w)) -\u2081 v w)\n \u2261\u27e8 cong (\u03bb hole \u2192 v w +\u2081 (u hole -\u2081 v w)) (v+[u-v]=u {\u03c3}) \u27e9\n v w +\u2081 (u w -\u2081 v w)\n \u2261\u27e8 v+[u-v]=u {\u03c4} \u27e9\n u w\n \u220e) where\n open \u2261-Reasoning\n\nR[v,u-v] {base base-int} {u} {v} = tt\nR[v,u-v] {base base-bag} {u} {v} = tt\nR[v,u-v] {\u03c3 \u21d2 \u03c4} {u} {v} = \u03bb w \u0394w R[w,\u0394w] \u2192\n let\n w\u2032 = w \u229e \u0394w\n _+_ = _\u229e_ {\u03c3 \u21d2 \u03c4}\n _-_ = _\u229f_ {\u03c3 \u21d2 \u03c4}\n _+\u2081_ = _\u229e_ {\u03c4}\n _-\u2081_ = _\u229f_ {\u03c4}\n in\n R[v,u-v] {\u03c4}\n ,\n (begin\n (v + (u - v)) w\u2032\n \u2261\u27e8 cong (\u03bb hole \u2192 hole w\u2032) (v+[u-v]=u {\u03c3 \u21d2 \u03c4} {u} {v}) \u27e9\n u w\u2032\n \u2261\u27e8 sym (v+[u-v]=u {\u03c4} {u w\u2032} {v w}) \u27e9\n v w +\u2081 (u - v) w \u0394w R[w,\u0394w]\n \u220e) where open \u2261-Reasoning\n\n-- `diff` and `apply`, without validity proofs\ninfixl 6 _\u27e6\u2295\u27e7_ _\u27e6\u229d\u27e7_\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7\n\n_\u27e6\u2295\u27e7_ {base base-int} n \u0394n = n + \u0394n\n_\u27e6\u2295\u27e7_ {base base-bag} b \u0394b = b ++ \u0394b\n_\u27e6\u2295\u27e7_ {\u03c3 \u21d2 \u03c4} f \u0394f = \u03bb v \u2192\n let\n _-\u2080_ = _\u27e6\u229d\u27e7_ {\u03c3}\n _+\u2081_ = _\u27e6\u2295\u27e7_ {\u03c4}\n in\n f v +\u2081 \u0394f v (v -\u2080 v)\n\n_\u27e6\u229d\u27e7_ {base base-int} m n = m - n\n_\u27e6\u229d\u27e7_ {base base-bag} a b = a \\\\ b\n_\u27e6\u229d\u27e7_ {\u03c3 \u21d2 \u03c4} g f = \u03bb v \u0394v \u2192 _\u27e6\u229d\u27e7_ {\u03c4} (g (_\u27e6\u2295\u27e7_ {\u03c3} v \u0394v)) (f v)\n","old_contents":"module Denotation.Change.Popl14 where\n\n-- Changes for Calculus Popl14\n--\n-- Contents\n-- - Mutually recursive concepts: \u0394Val, validity.\n-- Under module Syntax, the corresponding concepts of\n-- \u0394Type and \u0394Context reside in separate files.\n-- Now they have to be together due to mutual recursiveness.\n-- - `diff` and `apply` on semantic values of changes:\n-- they have to be here as well because they are mutually\n-- recursive with validity.\n-- - The lemma diff-is-valid: it has to be here because it is\n-- mutually recursive with `apply`\n-- - The lemma apply-diff: it is mutually recursive with `apply`\n-- and `diff`\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Popl14.Denotation.Value\nopen import Theorem.Groups-Popl14\nopen import Postulate.Extensionality\nopen import Data.Unit\nopen import Data.Product\nopen import Data.Integer\nopen import Structure.Bag.Popl14\n\n---------------\n-- Interface --\n---------------\n\n\u0394Val : Type \u2192 Set\nvalid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\n\ninfixl 6 _\u229e_ _\u229f_ -- as with + - in GHC.Num\n-- apply \u0394v v ::= v \u229e \u0394v\n_\u229e_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n-- diff u v ::= u \u229f v\n_\u229f_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\n\n-- Lemma diff-is-valid\nR[v,u-v] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229f v)\n\n-- Lemma apply-diff\nv+[u-v]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 v \u229e (u \u229f v) \u2261 u\n\n--------------------\n-- Implementation --\n--------------------\n\n-- \u0394Val \u03c4 is intended to be the semantic domain for changes of values\n-- of type \u03c4.\n--\n-- \u0394Val \u03c4 is the target of the denotational specification \u27e6_\u27e7\u0394.\n-- Detailed motivation:\n--\n-- https:\/\/github.com\/ps-mr\/ilc\/blob\/184a6291ac6eef80871c32d2483e3e62578baf06\/POPL14\/paper\/sec-formal.tex\n--\n-- \u0394Val : Type \u2192 Set\n\u0394Val int = \u2124\n\u0394Val bag = Bag\n\u0394Val (\u03c3 \u21d2 \u03c4) = (v : \u27e6 \u03c3 \u27e7) \u2192 (dv : \u0394Val \u03c3) \u2192 valid v dv \u2192 \u0394Val \u03c4\n\n-- _\u229e_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n_\u229e_ {int} n \u0394n = n + \u0394n\n_\u229e_ {bag} b \u0394b = b ++ \u0394b\n_\u229e_ {\u03c3 \u21d2 \u03c4} f \u0394f = \u03bb v \u2192 f v \u229e \u0394f v (v \u229f v) (R[v,u-v] {\u03c3})\n\n-- _\u229f_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\n_\u229f_ {int} m n = m - n\n_\u229f_ {bag} d b = d \\\\ b\n_\u229f_ {\u03c3 \u21d2 \u03c4} g f = \u03bb v \u0394v R[v,\u0394v] \u2192 g (v \u229e \u0394v) \u229f f v\n\n-- valid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\nvalid {int} n \u0394n = \u22a4\nvalid {bag} b \u0394b = \u22a4\nvalid {\u03c3 \u21d2 \u03c4} f \u0394f =\n \u2200 (v : \u27e6 \u03c3 \u27e7) (\u0394v : \u0394Val \u03c3) (R[v,\u0394v] : valid v \u0394v)\n \u2192 valid (f v) (\u0394f v \u0394v R[v,\u0394v])\n \u00d7 (f \u229e \u0394f) (v \u229e \u0394v) \u2261 f v \u229e \u0394f v \u0394v R[v,\u0394v]\n\n-- v+[u-v]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 v \u229e (u \u229f v) \u2261 u\nv+[u-v]=u {int} {u} {v} = n+[m-n]=m {v} {u}\nv+[u-v]=u {bag} {u} {v} = a++[b\\\\a]=b {v} {u}\nv+[u-v]=u {\u03c3 \u21d2 \u03c4} {u} {v} = ext (\u03bb w \u2192\n begin\n (v \u229e (u \u229f v)) w\n \u2261\u27e8 refl \u27e9\n v w \u229e (u (w \u229e (w \u229f w)) \u229f v w)\n \u2261\u27e8 cong (\u03bb hole \u2192 v w \u229e (u hole \u229f v w)) v+[u-v]=u \u27e9\n v w \u229e (u w \u229f v w)\n \u2261\u27e8 v+[u-v]=u \u27e9\n u w\n \u220e) where open \u2261-Reasoning\n\nR[v,u-v] {int} {u} {v} = tt\nR[v,u-v] {bag} {u} {v} = tt\nR[v,u-v] {\u03c3 \u21d2 \u03c4} {u} {v} = \u03bb w \u0394w R[w,\u0394w] \u2192\n let\n w\u2032 = w \u229e \u0394w\n in\n R[v,u-v] {\u03c4}\n ,\n (begin\n (v \u229e (u \u229f v)) w\u2032\n \u2261\u27e8 cong (\u03bb hole \u2192 hole w\u2032) (v+[u-v]=u {u = u} {v}) \u27e9\n u w\u2032\n \u2261\u27e8 sym (v+[u-v]=u {u = u w\u2032} {v w}) \u27e9\n v w \u229e (u \u229f v) w \u0394w R[w,\u0394w]\n \u220e) where open \u2261-Reasoning\n\n-- `diff` and `apply`, without validity proofs\ninfixl 6 _\u27e6\u2295\u27e7_ _\u27e6\u229d\u27e7_\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7\n\n_\u27e6\u2295\u27e7_ {int} n \u0394n = n + \u0394n\n_\u27e6\u2295\u27e7_ {bag} b \u0394b = b ++ \u0394b\n_\u27e6\u2295\u27e7_ {\u03c3 \u21d2 \u03c4} f \u0394f = \u03bb v \u2192 f v \u27e6\u2295\u27e7 \u0394f v (v \u27e6\u229d\u27e7 v)\n\n_\u27e6\u229d\u27e7_ {int} m n = m - n\n_\u27e6\u229d\u27e7_ {bag} a b = a \\\\ b\n_\u27e6\u229d\u27e7_ {\u03c3 \u21d2 \u03c4} g f = \u03bb v \u0394v \u2192 g (v \u27e6\u2295\u27e7 \u0394v) \u27e6\u229d\u27e7 f v\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"2c95a0ed6648adae17543219c076caabca2bf732","subject":"State proof obligations for constants","message":"State proof obligations for constants\n","repos":"inc-lc\/ilc-agda","old_file":"New\/Correctness.agda","new_file":"New\/Correctness.agda","new_contents":"module New.Correctness where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\nopen import Data.Product\n\nopen import New.Lang\nopen import New.Changes\nopen import New.Derive\nopen import New.LangChanges\n\n-- Lemmas\nalternate : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7Context \u2192 eCh \u0393 \u2192 \u27e6 \u0394\u0393 \u0393 \u27e7Context\nalternate {\u2205} \u2205 \u2205 = \u2205\nalternate {\u03c4 \u2022 \u0393} (v \u2022 \u03c1) (dv \u2022 d\u03c1) = dv \u2022 v \u2022 alternate \u03c1 d\u03c1\n\n\u27e6\u0393\u227c\u0394\u0393\u27e7 : \u2200 {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n \u03c1 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7\u227c (alternate \u03c1 d\u03c1)\n\u27e6\u0393\u227c\u0394\u0393\u27e7 \u2205 \u2205 = refl\n\u27e6\u0393\u227c\u0394\u0393\u27e7 (v \u2022 \u03c1) (dv \u2022 d\u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1)\n\nfit-sound : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n \u27e6 t \u27e7Term \u03c1 \u2261 \u27e6 fit t \u27e7Term (alternate \u03c1 d\u03c1)\nfit-sound t \u03c1 d\u03c1 = trans\n (cong \u27e6 t \u27e7Term (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1))\n (sym (weaken-sound t _))\n\n-- The change semantics is just the semantics composed with derivation!\nchangeSemVar : \u2200 {\u0393 \u03c4} \u2192 (t : Var \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 Cht \u03c4\nchangeSemVar t \u03c1 d\u03c1 = \u27e6 deriveVar t \u27e7Var (alternate \u03c1 d\u03c1)\n\nchangeSem : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 Cht \u03c4\nchangeSem t \u03c1 d\u03c1 = \u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1)\n\n-- XXX Should try to simply relate the semantics to the nil change, and prove\n-- that validity can be carried over, instead of proving separately validity and\n-- correctness; elsewhere this does make things simpler.\n\nvalidDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192\n (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n valid\u0393 \u03c1 d\u03c1 \u2192 valid (\u27e6 x \u27e7Var \u03c1) (\u27e6 deriveVar x \u27e7Var (alternate \u03c1 d\u03c1))\n\nvalidDeriveVar this (v \u2022 \u03c1) (dv \u2022 d\u03c1) (vdv , \u03c1d\u03c1) = vdv\nvalidDeriveVar (that x) (v \u2022 \u03c1) (dv \u2022 d\u03c1) (vdv , \u03c1d\u03c1) = validDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\n\ncorrectDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (v : Var \u0393 \u03c4) \u2192\n IsDerivative \u27e6 v \u27e7Var (changeSemVar v)\ncorrectDeriveVar this (v \u2022 \u03c1) (dv \u2022 d\u03c1) \u03c1d\u03c1 = refl\ncorrectDeriveVar (that x) (v \u2022 \u03c1) (dv \u2022 d\u03c1) (vdv , \u03c1d\u03c1) = correctDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\n\nvalidDerive : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 valid\u0393 \u03c1 d\u03c1 \u2192\n valid (\u27e6 t \u27e7Term \u03c1) (\u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1))\ncorrectDerive : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n IsDerivative \u27e6 t \u27e7Term (changeSem t)\n\nsemConst-rewrite : \u2200 {\u03c4 \u0393} (c : Const \u03c4) (\u03c1 : \u27e6 \u0393 \u27e7Context) d\u03c1 \u2192 changeSem (const c) \u03c1 d\u03c1 \u2261 \u27e6 deriveConst c \u27e7Term \u2205\nsemConst-rewrite c \u03c1 d\u03c1 rewrite weaken-sound {\u0393\u2081\u227c\u0393\u2082 = \u2205\u227c\u0393} (deriveConst c) (alternate \u03c1 d\u03c1) | \u27e6\u2205\u227c\u0393\u27e7-\u2205 (alternate \u03c1 d\u03c1) = refl\n\ncorrectDeriveConst : \u2200 {\u03c4} (c : Const \u03c4) \u2192 \u27e6 c \u27e7Const \u2261 \u27e6 c \u27e7Const \u2295 \u27e6 deriveConst c \u27e7Term \u2205\ncorrectDeriveConst ()\n\nvalidDeriveConst : \u2200 {\u03c4} (c : Const \u03c4) \u2192 valid \u27e6 c \u27e7Const (\u27e6 deriveConst c \u27e7Term \u2205)\nvalidDeriveConst ()\n\ncorrectDerive (const c) \u03c1 d\u03c1 \u03c1d\u03c1 rewrite semConst-rewrite c \u03c1 d\u03c1 = correctDeriveConst c\ncorrectDerive (var x) \u03c1 d\u03c1 \u03c1d\u03c1 = correctDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\ncorrectDerive (app s t) \u03c1 d\u03c1 \u03c1d\u03c1 rewrite sym (fit-sound t \u03c1 d\u03c1) =\n let\n open \u2261-Reasoning\n a0 = \u27e6 t \u27e7Term \u03c1\n da0 = \u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1)\n a0da0 = validDerive t \u03c1 d\u03c1 \u03c1d\u03c1\n in\n begin\n \u27e6 s \u27e7Term (\u03c1 \u2295 d\u03c1) (\u27e6 t \u27e7Term (\u03c1 \u2295 d\u03c1))\n \u2261\u27e8 correctDerive s \u03c1 d\u03c1 \u03c1d\u03c1 \u27e8$\u27e9 correctDerive t \u03c1 d\u03c1 \u03c1d\u03c1 \u27e9\n (\u27e6 s \u27e7Term \u03c1 \u2295 changeSem s \u03c1 d\u03c1) (\u27e6 t \u27e7Term \u03c1 \u2295 changeSem t \u03c1 d\u03c1)\n \u2261\u27e8 proj\u2082 (validDerive s \u03c1 d\u03c1 \u03c1d\u03c1 a0 da0 a0da0) \u27e9\n \u27e6 s \u27e7Term \u03c1 (\u27e6 t \u27e7Term \u03c1) \u2295 (changeSem s \u03c1 d\u03c1) (\u27e6 t \u27e7Term \u03c1) (changeSem t \u03c1 d\u03c1)\n \u220e\n where\n open import Theorem.CongApp\ncorrectDerive (abs t) \u03c1 d\u03c1 \u03c1d\u03c1 = ext (\u03bb a \u2192\n let\n open \u2261-Reasoning\n \u03c11 = a \u2022 \u03c1\n d\u03c11 = nil a \u2022 d\u03c1\n \u03c11d\u03c11 : valid \u03c11 d\u03c11\n \u03c11d\u03c11 = nil-valid a , \u03c1d\u03c1\n in\n begin\n \u27e6 t \u27e7Term (a \u2022 \u03c1 \u2295 d\u03c1)\n \u2261\u27e8 cong (\u03bb a\u2032 \u2192 \u27e6 t \u27e7Term (a\u2032 \u2022 \u03c1 \u2295 d\u03c1)) (sym (update-nil a)) \u27e9\n \u27e6 t \u27e7Term (\u03c11 \u2295 d\u03c11)\n \u2261\u27e8 correctDerive t \u03c11 d\u03c11 \u03c11d\u03c11 \u27e9\n \u27e6 t \u27e7Term \u03c11 \u2295 changeSem t \u03c11 d\u03c11\n \u220e)\n\nvalidDerive (app s t) \u03c1 d\u03c1 \u03c1d\u03c1 =\n let\n f = \u27e6 s \u27e7Term \u03c1\n df = \u27e6 derive s \u27e7Term (alternate \u03c1 d\u03c1)\n v = \u27e6 t \u27e7Term \u03c1\n dv = \u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1)\n vdv = validDerive t \u03c1 d\u03c1 \u03c1d\u03c1\n fdf = validDerive s \u03c1 d\u03c1 \u03c1d\u03c1\n fvdfv = proj\u2081 (fdf v dv vdv)\n in subst (\u03bb v\u2032 \u2192 valid (f v) (df v\u2032 dv)) (fit-sound t \u03c1 d\u03c1) fvdfv\nvalidDerive (abs t) \u03c1 d\u03c1 \u03c1d\u03c1 =\n \u03bb a da ada \u2192\n let\n fv = \u27e6 t \u27e7Term (a \u2022 \u03c1)\n dfvdv = \u27e6 derive t \u27e7Term (da \u2022 a \u2022 alternate \u03c1 d\u03c1)\n rdr = validDerive t (a \u2022 \u03c1) (da \u2022 d\u03c1) (ada , \u03c1d\u03c1)\n \u03c11 = a \u2295 da \u2022 \u03c1\n d\u03c11 = nil (a \u2295 da) \u2022 d\u03c1\n \u03c12 = a \u2022 \u03c1\n d\u03c12 = da \u2022 d\u03c1\n \u03c11d\u03c11 : valid \u03c11 d\u03c11\n \u03c11d\u03c11 = nil-valid (a \u2295 da) , \u03c1d\u03c1\n \u03c12d\u03c12 : valid \u03c12 d\u03c12\n \u03c12d\u03c12 = ada , \u03c1d\u03c1\n open \u2261-Reasoning\n in\n rdr ,\n (begin\n \u27e6 t \u27e7Term \u03c11 \u2295\n \u27e6 derive t \u27e7Term (alternate \u03c11 d\u03c11)\n \u2261\u27e8 sym ( correctDerive t \u03c11 d\u03c11 \u03c11d\u03c11) \u27e9\n \u27e6 t \u27e7Term (\u03c11 \u2295 d\u03c11)\n \u2261\u27e8\u27e9\n \u27e6 t \u27e7Term (a \u2295 da \u2295 (nil (a \u2295 da)) \u2022 \u03c1 \u2295 d\u03c1)\n \u2261\u27e8 cong (\u03bb a\u2032 \u2192 \u27e6 t \u27e7Term (a\u2032 \u2022 \u03c1 \u2295 d\u03c1)) (update-nil (a \u2295 da)) \u27e9\n \u27e6 t \u27e7Term (a \u2295 da \u2022 \u03c1 \u2295 d\u03c1)\n \u2261\u27e8 correctDerive t \u03c12 d\u03c12 \u03c12d\u03c12 \u27e9\n \u27e6 t \u27e7Term (a \u2022 \u03c1) \u2295 \u27e6 derive t \u27e7Term (da \u2022 a \u2022 alternate \u03c1 d\u03c1)\n \u220e)\nvalidDerive (var x) \u03c1 d\u03c1 \u03c1d\u03c1 = validDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\nvalidDerive (const c) \u03c1 d\u03c1 \u03c1d\u03c1 rewrite semConst-rewrite c \u03c1 d\u03c1 = validDeriveConst c\n","old_contents":"module New.Correctness where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\nopen import Data.Product\n\nopen import New.Lang\nopen import New.Changes\nopen import New.Derive\nopen import New.LangChanges\n\n-- Lemmas\nalternate : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7Context \u2192 eCh \u0393 \u2192 \u27e6 \u0394\u0393 \u0393 \u27e7Context\nalternate {\u2205} \u2205 \u2205 = \u2205\nalternate {\u03c4 \u2022 \u0393} (v \u2022 \u03c1) (dv \u2022 d\u03c1) = dv \u2022 v \u2022 alternate \u03c1 d\u03c1\n\n\u27e6\u0393\u227c\u0394\u0393\u27e7 : \u2200 {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n \u03c1 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7\u227c (alternate \u03c1 d\u03c1)\n\u27e6\u0393\u227c\u0394\u0393\u27e7 \u2205 \u2205 = refl\n\u27e6\u0393\u227c\u0394\u0393\u27e7 (v \u2022 \u03c1) (dv \u2022 d\u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1)\n\nfit-sound : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n \u27e6 t \u27e7Term \u03c1 \u2261 \u27e6 fit t \u27e7Term (alternate \u03c1 d\u03c1)\nfit-sound t \u03c1 d\u03c1 = trans\n (cong \u27e6 t \u27e7Term (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1))\n (sym (weaken-sound t _))\n\n-- The change semantics is just the semantics composed with derivation!\nchangeSemVar : \u2200 {\u0393 \u03c4} \u2192 (t : Var \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 Cht \u03c4\nchangeSemVar t \u03c1 d\u03c1 = \u27e6 deriveVar t \u27e7Var (alternate \u03c1 d\u03c1)\n\nchangeSem : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 Cht \u03c4\nchangeSem t \u03c1 d\u03c1 = \u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1)\n\n-- XXX Should try to simply relate the semantics to the nil change, and prove\n-- that validity can be carried over, instead of proving separately validity and\n-- correctness; elsewhere this does make things simpler.\n\nvalidDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192\n (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n valid\u0393 \u03c1 d\u03c1 \u2192 valid (\u27e6 x \u27e7Var \u03c1) (\u27e6 deriveVar x \u27e7Var (alternate \u03c1 d\u03c1))\n\nvalidDeriveVar this (v \u2022 \u03c1) (dv \u2022 d\u03c1) (vdv , \u03c1d\u03c1) = vdv\nvalidDeriveVar (that x) (v \u2022 \u03c1) (dv \u2022 d\u03c1) (vdv , \u03c1d\u03c1) = validDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\n\ncorrectDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (v : Var \u0393 \u03c4) \u2192\n IsDerivative \u27e6 v \u27e7Var (changeSemVar v)\ncorrectDeriveVar this (v \u2022 \u03c1) (dv \u2022 d\u03c1) \u03c1d\u03c1 = refl\ncorrectDeriveVar (that x) (v \u2022 \u03c1) (dv \u2022 d\u03c1) (vdv , \u03c1d\u03c1) = correctDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\n\nvalidDerive : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 valid\u0393 \u03c1 d\u03c1 \u2192\n valid (\u27e6 t \u27e7Term \u03c1) (\u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1))\ncorrectDerive : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n IsDerivative \u27e6 t \u27e7Term (changeSem t)\n\ncorrectDerive (const ()) \u03c1 d\u03c1 \u03c1d\u03c1\ncorrectDerive (var x) \u03c1 d\u03c1 \u03c1d\u03c1 = correctDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\ncorrectDerive (app s t) \u03c1 d\u03c1 \u03c1d\u03c1 rewrite sym (fit-sound t \u03c1 d\u03c1) =\n let\n open \u2261-Reasoning\n a0 = \u27e6 t \u27e7Term \u03c1\n da0 = \u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1)\n a0da0 = validDerive t \u03c1 d\u03c1 \u03c1d\u03c1\n in\n begin\n \u27e6 s \u27e7Term (\u03c1 \u2295 d\u03c1) (\u27e6 t \u27e7Term (\u03c1 \u2295 d\u03c1))\n \u2261\u27e8 correctDerive s \u03c1 d\u03c1 \u03c1d\u03c1 \u27e8$\u27e9 correctDerive t \u03c1 d\u03c1 \u03c1d\u03c1 \u27e9\n (\u27e6 s \u27e7Term \u03c1 \u2295 changeSem s \u03c1 d\u03c1) (\u27e6 t \u27e7Term \u03c1 \u2295 changeSem t \u03c1 d\u03c1)\n \u2261\u27e8 proj\u2082 (validDerive s \u03c1 d\u03c1 \u03c1d\u03c1 a0 da0 a0da0) \u27e9\n \u27e6 s \u27e7Term \u03c1 (\u27e6 t \u27e7Term \u03c1) \u2295 (changeSem s \u03c1 d\u03c1) (\u27e6 t \u27e7Term \u03c1) (changeSem t \u03c1 d\u03c1)\n \u220e\n where\n open import Theorem.CongApp\ncorrectDerive (abs t) \u03c1 d\u03c1 \u03c1d\u03c1 = ext (\u03bb a \u2192\n let\n open \u2261-Reasoning\n \u03c11 = a \u2022 \u03c1\n d\u03c11 = nil a \u2022 d\u03c1\n \u03c11d\u03c11 : valid \u03c11 d\u03c11\n \u03c11d\u03c11 = nil-valid a , \u03c1d\u03c1\n in\n begin\n \u27e6 t \u27e7Term (a \u2022 \u03c1 \u2295 d\u03c1)\n \u2261\u27e8 cong (\u03bb a\u2032 \u2192 \u27e6 t \u27e7Term (a\u2032 \u2022 \u03c1 \u2295 d\u03c1)) (sym (update-nil a)) \u27e9\n \u27e6 t \u27e7Term (\u03c11 \u2295 d\u03c11)\n \u2261\u27e8 correctDerive t \u03c11 d\u03c11 \u03c11d\u03c11 \u27e9\n \u27e6 t \u27e7Term \u03c11 \u2295 changeSem t \u03c11 d\u03c11\n \u220e)\n\nvalidDerive (app s t) \u03c1 d\u03c1 \u03c1d\u03c1 =\n let\n f = \u27e6 s \u27e7Term \u03c1\n df = \u27e6 derive s \u27e7Term (alternate \u03c1 d\u03c1)\n v = \u27e6 t \u27e7Term \u03c1\n dv = \u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1)\n vdv = validDerive t \u03c1 d\u03c1 \u03c1d\u03c1\n fdf = validDerive s \u03c1 d\u03c1 \u03c1d\u03c1\n fvdfv = proj\u2081 (fdf v dv vdv)\n in subst (\u03bb v\u2032 \u2192 valid (f v) (df v\u2032 dv)) (fit-sound t \u03c1 d\u03c1) fvdfv\nvalidDerive (abs t) \u03c1 d\u03c1 \u03c1d\u03c1 =\n \u03bb a da ada \u2192\n let\n fv = \u27e6 t \u27e7Term (a \u2022 \u03c1)\n dfvdv = \u27e6 derive t \u27e7Term (da \u2022 a \u2022 alternate \u03c1 d\u03c1)\n rdr = validDerive t (a \u2022 \u03c1) (da \u2022 d\u03c1) (ada , \u03c1d\u03c1)\n \u03c11 = a \u2295 da \u2022 \u03c1\n d\u03c11 = nil (a \u2295 da) \u2022 d\u03c1\n \u03c12 = a \u2022 \u03c1\n d\u03c12 = da \u2022 d\u03c1\n \u03c11d\u03c11 : valid \u03c11 d\u03c11\n \u03c11d\u03c11 = nil-valid (a \u2295 da) , \u03c1d\u03c1\n \u03c12d\u03c12 : valid \u03c12 d\u03c12\n \u03c12d\u03c12 = ada , \u03c1d\u03c1\n open \u2261-Reasoning\n in\n rdr ,\n (begin\n \u27e6 t \u27e7Term \u03c11 \u2295\n \u27e6 derive t \u27e7Term (alternate \u03c11 d\u03c11)\n \u2261\u27e8 sym ( correctDerive t \u03c11 d\u03c11 \u03c11d\u03c11) \u27e9\n \u27e6 t \u27e7Term (\u03c11 \u2295 d\u03c11)\n \u2261\u27e8\u27e9\n \u27e6 t \u27e7Term (a \u2295 da \u2295 (nil (a \u2295 da)) \u2022 \u03c1 \u2295 d\u03c1)\n \u2261\u27e8 cong (\u03bb a\u2032 \u2192 \u27e6 t \u27e7Term (a\u2032 \u2022 \u03c1 \u2295 d\u03c1)) (update-nil (a \u2295 da)) \u27e9\n \u27e6 t \u27e7Term (a \u2295 da \u2022 \u03c1 \u2295 d\u03c1)\n \u2261\u27e8 correctDerive t \u03c12 d\u03c12 \u03c12d\u03c12 \u27e9\n \u27e6 t \u27e7Term (a \u2022 \u03c1) \u2295 \u27e6 derive t \u27e7Term (da \u2022 a \u2022 alternate \u03c1 d\u03c1)\n \u220e)\nvalidDerive (var x) \u03c1 d\u03c1 \u03c1d\u03c1 = validDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\nvalidDerive (const ()) \u03c1 d\u03c1 \u03c1d\u03c1\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"4a40c50c8d82aaf4b7461c877efc293d49fbd8c7","subject":"flipbased-counting: two new relations \u2248\u1d2c and \u2248\u1d2c\u2032","message":"flipbased-counting: two new relations \u2248\u1d2c and \u2248\u1d2c\u2032\n","repos":"crypto-agda\/crypto-agda","old_file":"flipbased-counting.agda","new_file":"flipbased-counting.agda","new_contents":"open import Function\nopen import Data.Nat.NP\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2257_; _\u2261_)\n\nopen import Data.Bits\nimport flipbased\n\nmodule flipbased-counting\n (\u21ba : \u2200 {a} \u2192 \u2115 \u2192 Set a \u2192 Set a)\n (toss : \u21ba 1 Bit)\n (return\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A)\n (map\u21ba : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B)\n (join\u21ba : \u2200 {n\u2081 n\u2082 a} {A : Set a} \u2192 \u21ba n\u2081 (\u21ba n\u2082 A) \u2192 \u21ba (n\u2081 + n\u2082) A)\n (count\u21ba : \u2200 {n} \u2192 \u21ba n Bit \u2192 \u2115)\n where\n\nopen flipbased \u21ba toss return\u21ba map\u21ba join\u21ba\n\ninfix 4 _\u2248\u21ba_ _\u224b\u21ba_ _\u2248\u2141?_ _\u2248\u1d2c_ _\u2248\u1d2c\u2032_\n\n-- f \u2248\u21ba g when f and g return the same number of 1 (and 0).\n_\u2248\u21ba_ : \u2200 {n} (f g : EXP n) \u2192 Set\n_\u2248\u21ba_ = _\u2261_ on count\u21ba\n\n_\u2248\u2141?_ : \u2200 {c} (g\u2080 g\u2081 : \u2141? c) \u2192 Set\ng\u2080 \u2248\u2141? g\u2081 = \u2200 b \u2192 g\u2080 b \u2248\u21ba g\u2081 b\n\n_\u2248\u1d2c_ : \u2200 {n a} {A : Set a} (f g : \u21ba n A) \u2192 Set _\n_\u2248\u1d2c_ {n} {A = A} f g = \u2200 (Adv : A \u2192 Bit) \u2192 \u27ea Adv \u00b7 f \u27eb \u2248\u21ba \u27ea Adv \u00b7 g \u27eb\n\n_\u2248\u1d2c\u2032_ : \u2200 {n a} {A : Set a} (f g : \u21ba n A) \u2192 Set _\n_\u2248\u1d2c\u2032_ {n} {A = A} f g = \u2200 {ca} (Adv : A \u2192 \u21ba ca Bit) \u2192 f >>= Adv \u2248\u21ba g >>= Adv\n\n{- Restore if we find more use of it\nRelatedEXP : (\u2115 \u2192 \u2115 \u2192 Set) \u2192 \u2200 {m n} \u2192 EXP m \u2192 EXP n \u2192 Set\nRelatedEXP _\u223c_ {m} {n} f g = \u27e82^ n * count\u21ba f \u27e9 \u223c \u27e82^ m * count\u21ba g \u27e9\n\nmodule ... z where\n x \u223c y = dist x y > ... z ...\n-}\n\n_\u224b\u21ba_ : \u2200 {m n} \u2192 EXP m \u2192 EXP n \u2192 Set\n-- _\u224b\u21ba_ = RelatedEXP _\u2261_\n_\u224b\u21ba_ {m} {n} f g = \u27e82^ n * count\u21ba f \u27e9 \u2261 \u27e82^ m * count\u21ba g \u27e9\n\nSafe\u2141? : \u2200 {c} (f : \u2141? c) \u2192 Set\nSafe\u2141? f = f 0b \u2248\u21ba f 1b\n\nReversible\u2141? : \u2200 {c} (g : \u2141? c) \u2192 Set\nReversible\u2141? g = g \u2248\u2141? g \u2218 not\n\n\u2248\u21d2\u224b\u21ba : \u2200 {n} {f g : EXP n} \u2192 f \u2248\u21ba g \u2192 f \u224b\u21ba g\n\u2248\u21d2\u224b\u21ba eq rewrite eq = \u2261.refl\n\n\u224b\u21d2\u2248\u21ba : \u2200 {n} {f g : EXP n} \u2192 f \u224b\u21ba g \u2192 f \u2248\u21ba g\n\u224b\u21d2\u2248\u21ba {n} = 2^-inj n\n\nmodule \u2248\u2141? {n} where\n setoid : Setoid _ _\n setoid = record { Carrier = C; _\u2248_ = \u211b; isEquivalence = isEquivalence }\n where\n C : Set _\n C = \u2141? n\n\n \u211b : C \u2192 C \u2192 Set _\n \u211b = _\u2248\u2141?_\n\n refl : Reflexive \u211b\n refl b = \u2261.refl\n\n sym : Symmetric \u211b\n sym p b = \u2261.sym (p b)\n\n trans : Transitive \u211b\n trans p q b = \u2261.trans (p b) (q b)\n\n isEquivalence : IsEquivalence \u211b\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x y} \u2192 sym {x} {y}\n ; trans = \u03bb {x y z} \u2192 trans {x} {y} {z} }\n\n module Reasoning = Setoid-Reasoning setoid \n open Setoid setoid public\n\nmodule \u224b\u21ba {n} where\n setoid : Setoid _ _\n setoid = record { Carrier = C; _\u2248_ = \u211b; isEquivalence = isEquivalence }\n where\n C : Set _\n C = EXP n\n\n \u211b : C \u2192 C \u2192 Set _\n \u211b = _\u224b\u21ba_\n\n refl : Reflexive \u211b\n refl = \u2261.refl\n\n sym : Symmetric \u211b\n sym = \u2261.sym\n\n trans : Transitive \u211b\n trans = \u2261.trans\n\n isEquivalence : IsEquivalence \u211b\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x y} \u2192 sym {x} {y}\n ; trans = \u03bb {x y z} \u2192 trans {x} {y} {z} }\n\n module Reasoning = Setoid-Reasoning setoid \n open Setoid setoid public\n\nmodule \u2248\u21ba {n} where\n setoid : Setoid _ _\n setoid = record { Carrier = C; _\u2248_ = \u211b; isEquivalence = isEquivalence }\n where\n C : Set _\n C = EXP n\n\n \u211b : C \u2192 C \u2192 Set _\n \u211b = _\u2248\u21ba_\n\n refl : Reflexive \u211b\n refl = \u2261.refl\n\n sym : Symmetric \u211b\n sym = \u2261.sym\n\n trans : Transitive \u211b\n trans = \u2261.trans\n\n isEquivalence : IsEquivalence \u211b\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x y} \u2192 sym {x} {y}\n ; trans = \u03bb {x y z} \u2192 trans {x} {y} {z} }\n\n module Reasoning = Setoid-Reasoning setoid \n open Setoid setoid public\n\nmodule \u2248\u1d2c {n} {a} {A : Set a} where\n setoid : Setoid _ _\n setoid = record { Carrier = C; _\u2248_ = \u211b; isEquivalence = isEquivalence }\n where\n C : Set _\n C = \u21ba n A\n\n \u211b : C \u2192 C \u2192 Set _\n \u211b = _\u2248\u1d2c_\n\n refl : Reflexive \u211b\n refl A = \u2261.refl\n\n sym : Symmetric \u211b\n sym p A = \u2261.sym (p A)\n\n trans : Transitive \u211b\n trans p q A = \u2261.trans (p A) (q A)\n\n isEquivalence : IsEquivalence \u211b\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x y} \u2192 sym {x} {y}\n ; trans = \u03bb {x y z} \u2192 trans {x} {y} {z} }\n\n module Reasoning = Setoid-Reasoning setoid\n open Setoid setoid public\n\nmodule \u2248\u1d2c\u2032 {n} {a} {A : Set a} where\n setoid : Setoid _ _\n setoid = record { Carrier = C; _\u2248_ = \u211b; isEquivalence = isEquivalence }\n where\n C : Set _\n C = \u21ba n A\n\n \u211b : C \u2192 C \u2192 Set _\n \u211b = _\u2248\u1d2c\u2032_\n\n refl : Reflexive \u211b\n refl A = \u2261.refl\n\n sym : Symmetric \u211b\n sym p A = \u2261.sym (p A)\n\n trans : Transitive \u211b\n trans p q A = \u2261.trans (p A) (q A)\n\n isEquivalence : IsEquivalence \u211b\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x y} \u2192 sym {x} {y}\n ; trans = \u03bb {x y z} \u2192 trans {x} {y} {z} }\n\n module Reasoning = Setoid-Reasoning setoid\n open Setoid setoid public\n\nmodule \u2141? {n} where\n join : EXP n \u2192 EXP n \u2192 \u2141? n\n join f g b = if b then f else g\n\n safe-sym : \u2200 {g : \u2141? n} \u2192 Safe\u2141? g \u2192 Safe\u2141? (g \u2218 not)\n safe-sym {g} g-safe = \u2248\u21ba.sym {n} {g 0b} {g 1b} g-safe\n\n Reversible\u21d2Safe : \u2200 {c} (g : \u2141? c) \u2192 Reversible\u2141? g \u2192 Safe\u2141? g\n Reversible\u21d2Safe g g\u2248g\u2218not = g\u2248g\u2218not 0b\n\ndata Rat : Set where _\/_ : (num denom : \u2115) \u2192 Rat\n\nPr[_\u22611] : \u2200 {n} (f : EXP n) \u2192 Rat\nPr[_\u22611] {n} f = count\u21ba f \/ 2^ n\n","old_contents":"open import Function\nopen import Data.Nat.NP\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2257_; _\u2261_)\n\nopen import Data.Bits\nimport flipbased\n\nmodule flipbased-counting\n (\u21ba : \u2200 {a} \u2192 \u2115 \u2192 Set a \u2192 Set a)\n (toss : \u21ba 1 Bit)\n (return\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A)\n (map\u21ba : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B)\n (join\u21ba : \u2200 {n\u2081 n\u2082 a} {A : Set a} \u2192 \u21ba n\u2081 (\u21ba n\u2082 A) \u2192 \u21ba (n\u2081 + n\u2082) A)\n (count\u21ba : \u2200 {n} \u2192 \u21ba n Bit \u2192 \u2115)\n where\n\nopen flipbased \u21ba toss return\u21ba map\u21ba join\u21ba\n\ninfix 4 _\u2248\u21ba_ _\u224b\u21ba_ _\u2248\u2141?_\n\n-- f \u2248\u21ba g when f and g return the same number of 1 (and 0).\n_\u2248\u21ba_ : \u2200 {n} (f g : EXP n) \u2192 Set\n_\u2248\u21ba_ = _\u2261_ on count\u21ba\n\n_\u2248\u2141?_ : \u2200 {c} (g\u2080 g\u2081 : \u2141? c) \u2192 Set\ng\u2080 \u2248\u2141? g\u2081 = \u2200 b \u2192 g\u2080 b \u2248\u21ba g\u2081 b\n\n_\u223c[_]EXP_ : \u2200 {m n} \u2192 EXP m \u2192 (\u2115 \u2192 \u2115 \u2192 Set) \u2192 EXP n \u2192 Set\n_\u223c[_]EXP_ {m} {n} f _\u223c_ g = \u27e82^ n * count\u21ba f \u27e9 \u223c \u27e82^ m * count\u21ba g \u27e9\n\n_\u224b\u21ba_ : \u2200 {m n} \u2192 EXP m \u2192 EXP n \u2192 Set\nf \u224b\u21ba g = f \u223c[ _\u2261_ ]EXP g\n\nSafe\u2141? : \u2200 {c} (f : \u2141? c) \u2192 Set\nSafe\u2141? f = f 0b \u2248\u21ba f 1b\n\nReversible\u2141? : \u2200 {c} (g : \u2141? c) \u2192 Set\nReversible\u2141? g = g \u2248\u2141? g \u2218 not\n\n\u2248\u21d2\u224b\u21ba : \u2200 {n} {f g : EXP n} \u2192 f \u2248\u21ba g \u2192 f \u224b\u21ba g\n\u2248\u21d2\u224b\u21ba eq rewrite eq = \u2261.refl\n\n\u224b\u21d2\u2248\u21ba : \u2200 {n} {f g : EXP n} \u2192 f \u224b\u21ba g \u2192 f \u2248\u21ba g\n\u224b\u21d2\u2248\u21ba {n} = 2^-inj n\n\nmodule \u2248\u2141? {n} where\n setoid : Setoid _ _\n setoid = record { Carrier = C; _\u2248_ = \u211b; isEquivalence = isEquivalence }\n where\n C : Set _\n C = \u2141? n\n\n \u211b : C \u2192 C \u2192 Set _\n \u211b = _\u2248\u2141?_\n\n refl : Reflexive \u211b\n refl b = \u2261.refl\n\n sym : Symmetric \u211b\n sym p b = \u2261.sym (p b)\n\n trans : Transitive \u211b\n trans p q b = \u2261.trans (p b) (q b)\n\n isEquivalence : IsEquivalence \u211b\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x y} \u2192 sym {x} {y}\n ; trans = \u03bb {x y z} \u2192 trans {x} {y} {z} }\n\n module Reasoning = Setoid-Reasoning setoid \n open Setoid setoid public\n\nmodule \u224b\u21ba {n} where\n setoid : Setoid _ _\n setoid = record { Carrier = C; _\u2248_ = \u211b; isEquivalence = isEquivalence }\n where\n C : Set _\n C = EXP n\n\n \u211b : C \u2192 C \u2192 Set _\n \u211b = _\u224b\u21ba_\n\n refl : Reflexive \u211b\n refl = \u2261.refl\n\n sym : Symmetric \u211b\n sym = \u2261.sym\n\n trans : Transitive \u211b\n trans = \u2261.trans\n\n isEquivalence : IsEquivalence \u211b\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x y} \u2192 sym {x} {y}\n ; trans = \u03bb {x y z} \u2192 trans {x} {y} {z} }\n\n module Reasoning = Setoid-Reasoning setoid \n open Setoid setoid public\n\nmodule \u2248\u21ba {n} where\n setoid : Setoid _ _\n setoid = record { Carrier = C; _\u2248_ = \u211b; isEquivalence = isEquivalence }\n where\n C : Set _\n C = EXP n\n\n \u211b : C \u2192 C \u2192 Set _\n \u211b = _\u2248\u21ba_\n\n refl : Reflexive \u211b\n refl = \u2261.refl\n\n sym : Symmetric \u211b\n sym = \u2261.sym\n\n trans : Transitive \u211b\n trans = \u2261.trans\n\n isEquivalence : IsEquivalence \u211b\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x y} \u2192 sym {x} {y}\n ; trans = \u03bb {x y z} \u2192 trans {x} {y} {z} }\n\n module Reasoning = Setoid-Reasoning setoid \n open Setoid setoid public\n\nmodule \u2141? {n} where\n join : EXP n \u2192 EXP n \u2192 \u2141? n\n join f g b = if b then f else g\n\n safe-sym : \u2200 {g : \u2141? n} \u2192 Safe\u2141? g \u2192 Safe\u2141? (g \u2218 not)\n safe-sym {g} g-safe = \u2248\u21ba.sym {n} {g 0b} {g 1b} g-safe\n\n Reversible\u21d2Safe : \u2200 {c} (g : \u2141? c) \u2192 Reversible\u2141? g \u2192 Safe\u2141? g\n Reversible\u21d2Safe g g\u2248g\u2218not = g\u2248g\u2218not 0b\n\ndata Rat : Set where _\/_ : (num denom : \u2115) \u2192 Rat\n\nPr[_\u22611] : \u2200 {n} (f : EXP n) \u2192 Rat\nPr[_\u22611] {n} f = count\u21ba f \/ 2^ n\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"a2a3722c55d4534ac22294785bb1d8d1e115042d","subject":"Add incrementalization lemma for binary functions","message":"Add incrementalization lemma for binary functions\n\nSurprisingly it seems this was never stated, even though it would have\nbeen useful in some proofs (and even though it's a natural lemma for\nbinary functions).\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Algebra.agda","new_file":"Base\/Change\/Algebra.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Change Structures.\n--\n-- This module defines the notion of change structures,\n-- as well as change structures for groups, functions and lists.\n--\n-- This module corresponds to Section 2 of the PLDI paper.\n------------------------------------------------------------------------\n\nmodule Base.Change.Algebra where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Change Algebras\n-- ===============\n--\n-- In the paper, change algebras are called \"change structures\"\n-- and they are described in Section 2.1. We follow the design of\n-- the Agda standard library and define change algebras as two\n-- records, so Definition 2.1 from the PLDI paper maps to the\n-- records IsChangeAlgebra and ChangeAlgebra.\n--\n-- A value of type (IsChangeAlgebra Change update diff) proves that\n-- Change, update and diff together form a change algebra.\n--\n-- A value of type (ChangeAlgebra Carrier) contains the necessary\n-- ingredients to create a change algebra for a carrier set.\n--\n-- In the paper, Carrier is called V (Def. 2.1a),\n-- Change is called \u0394 (Def. 2.1b),\n-- update is written as infix \u2295 (Def. 2.1c),\n-- diff is written as infix \u229d (Def. 2.1d),\n-- and update-diff is specified in Def. 2.1e.\n\nrecord IsChangeAlgebra\n {c}\n {Carrier : Set c}\n (Change : Carrier \u2192 Set c)\n (update : (v : Carrier) (dv : Change v) \u2192 Carrier)\n (diff : (u v : Carrier) \u2192 Change v)\n (nil : (u : Carrier) \u2192 Change u) : Set (c) where\n field\n update-diff : \u2200 u v \u2192 update v (diff u v) \u2261 u\n -- This corresponds to Lemma 2.3 from the paper.\n update-nil : \u2200 v \u2192 update v (nil v) \u2261 v\n\n -- abbreviations\n before : \u2200 {v} \u2192 Change v \u2192 Carrier\n before {v} _ = v\n\n after : \u2200 {v} \u2192 Change v \u2192 Carrier\n after {v} dv = update v dv\n\nrecord ChangeAlgebra {c}\n (Carrier : Set c) : Set (suc c) where\n field\n Change : Carrier \u2192 Set c\n update : (v : Carrier) (dv : Change v) \u2192 Carrier\n diff : (u v : Carrier) \u2192 Change v\n\n -- This generalizes Def. 2.2. from the paper.\n nil : (u : Carrier) \u2192 Change u\n\n isChangeAlgebra : IsChangeAlgebra Change update diff nil\n\n\n infixl 6 update diff\n\n open IsChangeAlgebra isChangeAlgebra public\n\n-- The following open ... public statement lets us use infix \u229e\n-- and \u229f for update and diff. In the paper, we use infix \u2295 and\n-- \u229d.\n\nopen ChangeAlgebra {{...}} public\n using\n ( update-diff\n ; update-nil\n ; nil\n ; before\n ; after\n )\n renaming\n ( Change to \u0394\n ; update to _\u229e_\n ; diff to _\u229f_\n )\n\n-- Families of Change Algebras\n-- ===========================\n--\n-- This is some Agda machinery to allow subscripting change\n-- algebra operations to avoid ambiguity. In the paper,\n-- we simply write (in the paragraph after Def. 2.1):\n--\n-- We overload operators \u2206, \u229d and \u2295 to refer to the\n-- corresponding operations of different change structures; we\n-- will subscript these symbols when needed to prevent\n-- ambiguity.\n--\n-- The following definitions implement this idea formally.\n\nrecord ChangeAlgebraFamily {a p} {A : Set a} (P : A \u2192 Set p): Set (suc p \u2294 a) where\n constructor\n family\n field\n change-algebra : \u2200 x \u2192 ChangeAlgebra (P x)\n\n module _ x where\n open ChangeAlgebra (change-algebra x) public\n\nmodule Family = ChangeAlgebraFamily {{...}}\n\nopen Family public\n using\n (\n )\n renaming\n ( Change to \u0394\u208d_\u208e\n ; nil to nil\u208d_\u208e\n ; update-diff to update-diff\u208d_\u208e\n ; update-nil to update-nil\u208d_\u208e\n ; change-algebra to change-algebra\u208d_\u208e\n ; before to before\u208d_\u208e\n ; after to after\u208d_\u208e\n )\n\n-- XXX not clear this is ever used\ninstance\n change-algebra-family-inst = change-algebra\u208d_\u208e\n\ninfixl 6 update\u2032 diff\u2032\n\nupdate\u2032 = Family.update\nsyntax update\u2032 x v dv = v \u229e\u208d x \u208e dv\n\ndiff\u2032 = Family.diff\nsyntax diff\u2032 x u v = u \u229f\u208d x \u208e v\n\n-- Derivatives\n-- ===========\n--\n-- This corresponds to Def. 2.4 in the paper.\n\nRawChange : \u2200 {a b} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra A}} \u2192\n {{CB : ChangeAlgebra B}} \u2192\n (f : A \u2192 B) \u2192\n Set (a \u2294 b)\nRawChange f = \u2200 a (da : \u0394 a) \u2192 \u0394 (f a)\n\nIsDerivative : \u2200 {a b} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra A}} \u2192\n {{CB : ChangeAlgebra B}} \u2192\n (f : A \u2192 B) \u2192\n (df : RawChange f) \u2192\n Set (a \u2294 b)\nIsDerivative f df = \u2200 a da \u2192 f a \u229e df a da \u2261 f (a \u229e da)\n\n-- This is a variant of IsDerivative for change algebra families.\nRawChange\u208d_,_\u208e : \u2200 {a b p q} {A : Set a} {B : Set b} {P : A \u2192 Set p} {Q : B \u2192 Set q} \u2192\n {{CP : ChangeAlgebraFamily P}} \u2192\n {{CQ : ChangeAlgebraFamily Q}} \u2192\n (x : A) \u2192\n (y : B) \u2192\n (f : P x \u2192 Q y) \u2192 Set (p \u2294 q)\nRawChange\u208d_,_\u208e x y f = \u2200 px (dpx : \u0394\u208d_\u208e x px) \u2192 \u0394\u208d_\u208e y (f px)\n\nIsDerivative\u208d_,_\u208e : \u2200 {a b p q} {A : Set a} {B : Set b} {P : A \u2192 Set p} {Q : B \u2192 Set q} \u2192\n {{CP : ChangeAlgebraFamily P}} \u2192\n {{CQ : ChangeAlgebraFamily Q}} \u2192\n (x : A) \u2192\n (y : B) \u2192\n (f : P x \u2192 Q y) \u2192\n (df : RawChange\u208d_,_\u208e x y f) \u2192\n Set (p \u2294 q)\nIsDerivative\u208d_,_\u208e {P = P} {{CP}} {{CQ}} x y f df = IsDerivative {{change-algebra\u208d _ \u208e}} {{change-algebra\u208d _ \u208e}} f df where\n CPx = change-algebra\u208d_\u208e {{CP}} x\n CQy = change-algebra\u208d_\u208e {{CQ}} y\n\n-- Lemma 2.5 appears in Base.Change.Equivalence.\n\n-- Abelian Groups Induce Change Algebras\n-- =====================================\n--\n-- In the paper, as the first example for change structures after\n-- Def. 2.1, we mention that \"each abelian group ... induces a\n-- change structure\". This is the formalization of this result.\n--\n-- The module GroupChanges below takes a proof that A forms an\n-- abelian group and provides a changeAlgebra for A. The proof of\n-- Def 2.1e is by equational reasoning using the group axioms, in\n-- the definition of changeAlgebra.isChangeAlgebra.update-diff.\n\nopen import Algebra.Structures\nopen import Data.Product\nopen import Function\n\nmodule GroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isAbelianGroup : IsAbelianGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsAbelianGroup isAbelianGroup\n hiding\n ( refl\n )\n renaming\n ( _-_ to _\u229d_\n ; \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n changeAlgebraGroup : ChangeAlgebra A\n changeAlgebraGroup = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; nil = const \u03b5\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8 comm _ _ \u27e9\n (u \u229d v ) \u2295 v\n \u2261\u27e8\u27e9\n (u \u2295 (v \u207b\u00b9)) \u2295 v\n \u2261\u27e8 assoc _ _ _ \u27e9\n u \u2295 ((v \u207b\u00b9) \u2295 v)\n \u2261\u27e8 refl \u27e8\u2295\u27e9 proj\u2081 inverse v \u27e9\n u \u2295 \u03b5\n \u2261\u27e8 proj\u2082 identity u \u27e9\n u\n \u220e\n ; update-nil = proj\u2082 identity\n }\n }\n\n-- Function Changes\n-- ================\n--\n-- This is one of our most important results: Change structures\n-- can be lifted to function spaces. We formalize this as a module\n-- FunctionChanges that takes the change algebras for A and B as\n-- arguments, and provides a changeAlgebra for (A \u2192 B). The proofs\n-- are by equational reasoning using 2.1e for A and B.\n\nmodule FunctionChanges\n {a} {b} (A : Set a) (B : Set b) {{CA : ChangeAlgebra A}} {{CB : ChangeAlgebra B}}\n where\n -- This corresponds to Definition 2.6 in the paper.\n record FunctionChange (f : A \u2192 B) : Set (a \u2294 b) where\n field\n -- Definition 2.6a\n apply : RawChange f\n\n -- Definition 2.6b.\n -- (for some reason, the version in the paper has the arguments of \u2261\n -- flipped. Since \u2261 is symmetric, this doesn't matter).\n correct : (a : A) (da : \u0394 a) \u2192\n f (a \u229e da) \u229e apply (a \u229e da) (nil (a \u229e da)) \u2261 f a \u229e apply a da\n\n open FunctionChange public\n open \u2261-Reasoning\n open import Postulate.Extensionality\n\n funDiff : (g f : A \u2192 B) \u2192 FunctionChange f\n funDiff = \u03bb g f \u2192 record\n { apply = \u03bb a da \u2192 g (a \u229e da) \u229f f a\n -- the proof of correct is the first half of what we\n -- have to prove for Theorem 2.7.\n ; correct = \u03bb a da \u2192\n begin\n f (a \u229e da) \u229e (g (a \u229e da \u229e nil (a \u229e da)) \u229f f (a \u229e da))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f (a \u229e da) \u229e (g \u25a1 \u229f f (a \u229e da)))\n (update-nil (a \u229e da)) \u27e9\n f (a \u229e da) \u229e (g (a \u229e da) \u229f f (a \u229e da))\n \u2261\u27e8 update-diff (g (a \u229e da)) (f (a \u229e da)) \u27e9\n g (a \u229e da)\n \u2261\u27e8 sym (update-diff (g (a \u229e da)) (f a)) \u27e9\n f a \u229e (g (a \u229e da) \u229f f a)\n \u220e\n }\n\n funUpdate : \u2200 (f : A \u2192 B) (df : FunctionChange f) \u2192 A \u2192 B\n funUpdate = \u03bb f df a \u2192 f a \u229e apply df a (nil a)\n funNil = \u03bb f \u2192 funDiff f f\n\n mutual\n -- I have to write the type of funUpdateDiff without using changeAlgebra,\n -- so I just use the underlying implementations.\n funUpdateDiff : \u2200 u v \u2192 funUpdate v (funDiff u v) \u2261 u\n instance\n changeAlgebraFun : ChangeAlgebra (A \u2192 B)\n\n changeAlgebraFun = record\n { Change = FunctionChange\n -- in the paper, update and diff below are in Def. 2.7\n ; update = funUpdate\n ; diff = funDiff\n ; nil = funNil\n ; isChangeAlgebra = record\n -- the proof of update-diff is the second half of what\n -- we have to prove for Theorem 2.8.\n { update-diff = funUpdateDiff\n ; update-nil = \u03bb v \u2192 funUpdateDiff v v\n }\n }\n -- XXX remove mutual recursion by inlining the algebra in here?\n funUpdateDiff = \u03bb g f \u2192 ext (\u03bb a \u2192\n begin\n f a \u229e (g (a \u229e (nil a)) \u229f f a)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f a \u229e (g \u25a1 \u229f f a)) (update-nil a) \u27e9\n f a \u229e (g a \u229f f a)\n \u2261\u27e8 update-diff (g a) (f a) \u27e9\n g a\n \u220e)\n\n -- This is Theorem 2.9 in the paper.\n incrementalization : \u2200 (f : A \u2192 B) df a da \u2192\n (f \u229e df) (a \u229e da) \u2261 f a \u229e apply df a da\n incrementalization f df a da = correct df a da\n\n -- This is Theorem 2.10 in the paper. However, the derivative of f is just\n -- the apply component of `nil f`, not the full `nil f`, which also includes\n -- a proof. This is not an issue in the paper, which is formulated in a\n -- proof-irrelevant metalanguage.\n nil-is-derivative : \u2200 (f : A \u2192 B) \u2192\n IsDerivative f (apply (nil f))\n nil-is-derivative f a da =\n begin\n f a \u229e apply (nil f) a da\n \u2261\u27e8 sym (incrementalization f (nil f) a da) \u27e9\n (f \u229e nil f) (a \u229e da)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (a \u229e da))\n (update-nil f) \u27e9\n f (a \u229e da)\n \u220e\n\n -- Show that any derivative is a valid function change.\n\n -- In the paper, this is never actually stated. We just prove that nil\n -- changes are derivatives; the paper keeps talking about \"the derivative\",\n -- suggesting derivatives are unique. If derivatives were unique, we could\n -- say that the nil change is *the* derivative, hence the derivative is the\n -- nil change (hence also a change).\n --\n -- In fact, derivatives are only unique up to change equivalence and\n -- extensional equality; this is proven in Base.Change.Equivalence.derivative-unique.\n --\n Derivative-is-valid : \u2200 {f : A \u2192 B} df (IsDerivative-f-df : IsDerivative f df) a da \u2192\n f (a \u229e da) \u229e (df (a \u229e da) (nil (a \u229e da))) \u2261 f a \u229e df a da\n Derivative-is-valid {f} df IsDerivative-f-df a da rewrite IsDerivative-f-df (a \u229e da) (nil (a \u229e da)) | update-nil (a \u229e da) = sym (IsDerivative-f-df a da)\n\n DerivativeAsChange : \u2200 {f : A \u2192 B} {df} (IsDerivative-f-df : IsDerivative f df) \u2192 \u0394 f\n DerivativeAsChange {df = df} IsDerivative-f-df = record { apply = df ; correct = Derivative-is-valid df IsDerivative-f-df }\n -- In Equivalence.agda, derivative-is-nil-alternative then proves that a derivative is also a nil change.\n\n-- Reexport a few members with A and B marked as implicit parameters. This\n-- matters especially for changeAlgebra, since otherwise it can't be used for\n-- instance search.\nmodule _\n {a} {b} {A : Set a} {B : Set b} {{CA : ChangeAlgebra A}} {{CB : ChangeAlgebra B}}\n where\n open FunctionChanges A B {{CA}} {{CB}} public\n using\n ( changeAlgebraFun\n ; apply\n ; correct\n ; incrementalization\n ; DerivativeAsChange\n ; FunctionChange\n )\n\nmodule BinaryFunctionChanges\n {a} {b} {c} (A : Set a) (B : Set b) (C : Set c) {{CA : ChangeAlgebra A}} {{CB : ChangeAlgebra B}} {{CC : ChangeAlgebra C}} where\n incrementalization-binary : \u2200 (f : A \u2192 B \u2192 C) df a da b db \u2192\n (f \u229e df) (a \u229e da) (b \u229e db) \u2261 f a b \u229e apply (apply df a da) b db\n incrementalization-binary f df x dx y dy\n rewrite cong (\u03bb \u25a1 \u2192 \u25a1 (y \u229e dy)) (incrementalization f df x dx)\n = incrementalization (f x) (apply df x dx) y dy\n\n-- List (== Environment) Changes\n-- =============================\n--\n-- Here, we define a change structure on environments, given a\n-- change structure on the values in the environments. In the\n-- paper, we describe this in Definition 3.5. But note that this\n-- Agda formalization uses de Bruijn indices instead of names, so\n-- environments are just lists. Therefore, when we use Definition\n-- 3.5 in the paper, in this formalization, we use the list-like\n-- change structure defined here.\n\nopen import Data.List\nopen import Data.List.All\n\ndata All\u2032 {a p q} {A : Set a}\n {P : A \u2192 Set p}\n (Q : {x : A} \u2192 P x \u2192 Set q)\n : {xs : List A} (pxs : All P xs) \u2192 Set (p \u2294 q \u2294 a) where\n [] : All\u2032 Q []\n _\u2237_ : \u2200 {x xs} {px : P x} {pxs : All P xs} (qx : Q px) (qxs : All\u2032 Q pxs) \u2192 All\u2032 Q (px \u2237 pxs)\n\nmodule ListChanges\n {a} {A : Set a} (P : A \u2192 Set) {{C : ChangeAlgebraFamily P}}\n where\n update-all : \u2200 {xs} \u2192 (pxs : All P xs) \u2192 All\u2032 \u0394 pxs \u2192 All P xs\n update-all {[]} [] [] = []\n update-all {x \u2237 xs} (px \u2237 pxs) (dpx \u2237 dpxs) = (px \u229e dpx) \u2237 update-all pxs dpxs\n\n diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 All\u2032 \u0394 pxs\n diff-all [] [] = []\n diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = (px\u2032 \u229f px) \u2237 diff-all pxs\u2032 pxs\n\n update-diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 update-all pxs (diff-all pxs\u2032 pxs) \u2261 pxs\u2032\n update-diff-all [] [] = refl\n update-diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = cong\u2082 _\u2237_ (update-diff px\u2032 px) (update-diff-all pxs\u2032 pxs)\n\n instance\n changeAlgebraListChanges : ChangeAlgebraFamily (All P)\n\n changeAlgebraListChanges = record\n { change-algebra = \u03bb xs \u2192 record\n { Change = All\u2032 \u0394\n ; update = update-all\n ; diff = diff-all\n ; nil = \u03bb xs \u2192 diff-all xs xs\n ; isChangeAlgebra = record\n { update-diff = update-diff-all\n ; update-nil = \u03bb xs\u2081 \u2192 update-diff-all xs\u2081 xs\u2081\n }\n }\n }\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Change Structures.\n--\n-- This module defines the notion of change structures,\n-- as well as change structures for groups, functions and lists.\n--\n-- This module corresponds to Section 2 of the PLDI paper.\n------------------------------------------------------------------------\n\nmodule Base.Change.Algebra where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Change Algebras\n-- ===============\n--\n-- In the paper, change algebras are called \"change structures\"\n-- and they are described in Section 2.1. We follow the design of\n-- the Agda standard library and define change algebras as two\n-- records, so Definition 2.1 from the PLDI paper maps to the\n-- records IsChangeAlgebra and ChangeAlgebra.\n--\n-- A value of type (IsChangeAlgebra Change update diff) proves that\n-- Change, update and diff together form a change algebra.\n--\n-- A value of type (ChangeAlgebra Carrier) contains the necessary\n-- ingredients to create a change algebra for a carrier set.\n--\n-- In the paper, Carrier is called V (Def. 2.1a),\n-- Change is called \u0394 (Def. 2.1b),\n-- update is written as infix \u2295 (Def. 2.1c),\n-- diff is written as infix \u229d (Def. 2.1d),\n-- and update-diff is specified in Def. 2.1e.\n\nrecord IsChangeAlgebra\n {c}\n {Carrier : Set c}\n (Change : Carrier \u2192 Set c)\n (update : (v : Carrier) (dv : Change v) \u2192 Carrier)\n (diff : (u v : Carrier) \u2192 Change v)\n (nil : (u : Carrier) \u2192 Change u) : Set (c) where\n field\n update-diff : \u2200 u v \u2192 update v (diff u v) \u2261 u\n -- This corresponds to Lemma 2.3 from the paper.\n update-nil : \u2200 v \u2192 update v (nil v) \u2261 v\n\n -- abbreviations\n before : \u2200 {v} \u2192 Change v \u2192 Carrier\n before {v} _ = v\n\n after : \u2200 {v} \u2192 Change v \u2192 Carrier\n after {v} dv = update v dv\n\nrecord ChangeAlgebra {c}\n (Carrier : Set c) : Set (suc c) where\n field\n Change : Carrier \u2192 Set c\n update : (v : Carrier) (dv : Change v) \u2192 Carrier\n diff : (u v : Carrier) \u2192 Change v\n\n -- This generalizes Def. 2.2. from the paper.\n nil : (u : Carrier) \u2192 Change u\n\n isChangeAlgebra : IsChangeAlgebra Change update diff nil\n\n\n infixl 6 update diff\n\n open IsChangeAlgebra isChangeAlgebra public\n\n-- The following open ... public statement lets us use infix \u229e\n-- and \u229f for update and diff. In the paper, we use infix \u2295 and\n-- \u229d.\n\nopen ChangeAlgebra {{...}} public\n using\n ( update-diff\n ; update-nil\n ; nil\n ; before\n ; after\n )\n renaming\n ( Change to \u0394\n ; update to _\u229e_\n ; diff to _\u229f_\n )\n\n-- Families of Change Algebras\n-- ===========================\n--\n-- This is some Agda machinery to allow subscripting change\n-- algebra operations to avoid ambiguity. In the paper,\n-- we simply write (in the paragraph after Def. 2.1):\n--\n-- We overload operators \u2206, \u229d and \u2295 to refer to the\n-- corresponding operations of different change structures; we\n-- will subscript these symbols when needed to prevent\n-- ambiguity.\n--\n-- The following definitions implement this idea formally.\n\nrecord ChangeAlgebraFamily {a p} {A : Set a} (P : A \u2192 Set p): Set (suc p \u2294 a) where\n constructor\n family\n field\n change-algebra : \u2200 x \u2192 ChangeAlgebra (P x)\n\n module _ x where\n open ChangeAlgebra (change-algebra x) public\n\nmodule Family = ChangeAlgebraFamily {{...}}\n\nopen Family public\n using\n (\n )\n renaming\n ( Change to \u0394\u208d_\u208e\n ; nil to nil\u208d_\u208e\n ; update-diff to update-diff\u208d_\u208e\n ; update-nil to update-nil\u208d_\u208e\n ; change-algebra to change-algebra\u208d_\u208e\n ; before to before\u208d_\u208e\n ; after to after\u208d_\u208e\n )\n\n-- XXX not clear this is ever used\ninstance\n change-algebra-family-inst = change-algebra\u208d_\u208e\n\ninfixl 6 update\u2032 diff\u2032\n\nupdate\u2032 = Family.update\nsyntax update\u2032 x v dv = v \u229e\u208d x \u208e dv\n\ndiff\u2032 = Family.diff\nsyntax diff\u2032 x u v = u \u229f\u208d x \u208e v\n\n-- Derivatives\n-- ===========\n--\n-- This corresponds to Def. 2.4 in the paper.\n\nRawChange : \u2200 {a b} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra A}} \u2192\n {{CB : ChangeAlgebra B}} \u2192\n (f : A \u2192 B) \u2192\n Set (a \u2294 b)\nRawChange f = \u2200 a (da : \u0394 a) \u2192 \u0394 (f a)\n\nIsDerivative : \u2200 {a b} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra A}} \u2192\n {{CB : ChangeAlgebra B}} \u2192\n (f : A \u2192 B) \u2192\n (df : RawChange f) \u2192\n Set (a \u2294 b)\nIsDerivative f df = \u2200 a da \u2192 f a \u229e df a da \u2261 f (a \u229e da)\n\n-- This is a variant of IsDerivative for change algebra families.\nRawChange\u208d_,_\u208e : \u2200 {a b p q} {A : Set a} {B : Set b} {P : A \u2192 Set p} {Q : B \u2192 Set q} \u2192\n {{CP : ChangeAlgebraFamily P}} \u2192\n {{CQ : ChangeAlgebraFamily Q}} \u2192\n (x : A) \u2192\n (y : B) \u2192\n (f : P x \u2192 Q y) \u2192 Set (p \u2294 q)\nRawChange\u208d_,_\u208e x y f = \u2200 px (dpx : \u0394\u208d_\u208e x px) \u2192 \u0394\u208d_\u208e y (f px)\n\nIsDerivative\u208d_,_\u208e : \u2200 {a b p q} {A : Set a} {B : Set b} {P : A \u2192 Set p} {Q : B \u2192 Set q} \u2192\n {{CP : ChangeAlgebraFamily P}} \u2192\n {{CQ : ChangeAlgebraFamily Q}} \u2192\n (x : A) \u2192\n (y : B) \u2192\n (f : P x \u2192 Q y) \u2192\n (df : RawChange\u208d_,_\u208e x y f) \u2192\n Set (p \u2294 q)\nIsDerivative\u208d_,_\u208e {P = P} {{CP}} {{CQ}} x y f df = IsDerivative {{change-algebra\u208d _ \u208e}} {{change-algebra\u208d _ \u208e}} f df where\n CPx = change-algebra\u208d_\u208e {{CP}} x\n CQy = change-algebra\u208d_\u208e {{CQ}} y\n\n-- Lemma 2.5 appears in Base.Change.Equivalence.\n\n-- Abelian Groups Induce Change Algebras\n-- =====================================\n--\n-- In the paper, as the first example for change structures after\n-- Def. 2.1, we mention that \"each abelian group ... induces a\n-- change structure\". This is the formalization of this result.\n--\n-- The module GroupChanges below takes a proof that A forms an\n-- abelian group and provides a changeAlgebra for A. The proof of\n-- Def 2.1e is by equational reasoning using the group axioms, in\n-- the definition of changeAlgebra.isChangeAlgebra.update-diff.\n\nopen import Algebra.Structures\nopen import Data.Product\nopen import Function\n\nmodule GroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isAbelianGroup : IsAbelianGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsAbelianGroup isAbelianGroup\n hiding\n ( refl\n )\n renaming\n ( _-_ to _\u229d_\n ; \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n changeAlgebraGroup : ChangeAlgebra A\n changeAlgebraGroup = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; nil = const \u03b5\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8 comm _ _ \u27e9\n (u \u229d v ) \u2295 v\n \u2261\u27e8\u27e9\n (u \u2295 (v \u207b\u00b9)) \u2295 v\n \u2261\u27e8 assoc _ _ _ \u27e9\n u \u2295 ((v \u207b\u00b9) \u2295 v)\n \u2261\u27e8 refl \u27e8\u2295\u27e9 proj\u2081 inverse v \u27e9\n u \u2295 \u03b5\n \u2261\u27e8 proj\u2082 identity u \u27e9\n u\n \u220e\n ; update-nil = proj\u2082 identity\n }\n }\n\n-- Function Changes\n-- ================\n--\n-- This is one of our most important results: Change structures\n-- can be lifted to function spaces. We formalize this as a module\n-- FunctionChanges that takes the change algebras for A and B as\n-- arguments, and provides a changeAlgebra for (A \u2192 B). The proofs\n-- are by equational reasoning using 2.1e for A and B.\n\nmodule FunctionChanges\n {a} {b} (A : Set a) (B : Set b) {{CA : ChangeAlgebra A}} {{CB : ChangeAlgebra B}}\n where\n -- This corresponds to Definition 2.6 in the paper.\n record FunctionChange (f : A \u2192 B) : Set (a \u2294 b) where\n field\n -- Definition 2.6a\n apply : RawChange f\n\n -- Definition 2.6b.\n -- (for some reason, the version in the paper has the arguments of \u2261\n -- flipped. Since \u2261 is symmetric, this doesn't matter).\n correct : (a : A) (da : \u0394 a) \u2192\n f (a \u229e da) \u229e apply (a \u229e da) (nil (a \u229e da)) \u2261 f a \u229e apply a da\n\n open FunctionChange public\n open \u2261-Reasoning\n open import Postulate.Extensionality\n\n funDiff : (g f : A \u2192 B) \u2192 FunctionChange f\n funDiff = \u03bb g f \u2192 record\n { apply = \u03bb a da \u2192 g (a \u229e da) \u229f f a\n -- the proof of correct is the first half of what we\n -- have to prove for Theorem 2.7.\n ; correct = \u03bb a da \u2192\n begin\n f (a \u229e da) \u229e (g (a \u229e da \u229e nil (a \u229e da)) \u229f f (a \u229e da))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f (a \u229e da) \u229e (g \u25a1 \u229f f (a \u229e da)))\n (update-nil (a \u229e da)) \u27e9\n f (a \u229e da) \u229e (g (a \u229e da) \u229f f (a \u229e da))\n \u2261\u27e8 update-diff (g (a \u229e da)) (f (a \u229e da)) \u27e9\n g (a \u229e da)\n \u2261\u27e8 sym (update-diff (g (a \u229e da)) (f a)) \u27e9\n f a \u229e (g (a \u229e da) \u229f f a)\n \u220e\n }\n\n funUpdate : \u2200 (f : A \u2192 B) (df : FunctionChange f) \u2192 A \u2192 B\n funUpdate = \u03bb f df a \u2192 f a \u229e apply df a (nil a)\n funNil = \u03bb f \u2192 funDiff f f\n\n mutual\n -- I have to write the type of funUpdateDiff without using changeAlgebra,\n -- so I just use the underlying implementations.\n funUpdateDiff : \u2200 u v \u2192 funUpdate v (funDiff u v) \u2261 u\n instance\n changeAlgebraFun : ChangeAlgebra (A \u2192 B)\n\n changeAlgebraFun = record\n { Change = FunctionChange\n -- in the paper, update and diff below are in Def. 2.7\n ; update = funUpdate\n ; diff = funDiff\n ; nil = funNil\n ; isChangeAlgebra = record\n -- the proof of update-diff is the second half of what\n -- we have to prove for Theorem 2.8.\n { update-diff = funUpdateDiff\n ; update-nil = \u03bb v \u2192 funUpdateDiff v v\n }\n }\n -- XXX remove mutual recursion by inlining the algebra in here?\n funUpdateDiff = \u03bb g f \u2192 ext (\u03bb a \u2192\n begin\n f a \u229e (g (a \u229e (nil a)) \u229f f a)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f a \u229e (g \u25a1 \u229f f a)) (update-nil a) \u27e9\n f a \u229e (g a \u229f f a)\n \u2261\u27e8 update-diff (g a) (f a) \u27e9\n g a\n \u220e)\n\n -- This is Theorem 2.9 in the paper.\n incrementalization : \u2200 (f : A \u2192 B) df a da \u2192\n (f \u229e df) (a \u229e da) \u2261 f a \u229e apply df a da\n incrementalization f df a da = correct df a da\n\n -- This is Theorem 2.10 in the paper. However, the derivative of f is just\n -- the apply component of `nil f`, not the full `nil f`, which also includes\n -- a proof. This is not an issue in the paper, which is formulated in a\n -- proof-irrelevant metalanguage.\n nil-is-derivative : \u2200 (f : A \u2192 B) \u2192\n IsDerivative f (apply (nil f))\n nil-is-derivative f a da =\n begin\n f a \u229e apply (nil f) a da\n \u2261\u27e8 sym (incrementalization f (nil f) a da) \u27e9\n (f \u229e nil f) (a \u229e da)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (a \u229e da))\n (update-nil f) \u27e9\n f (a \u229e da)\n \u220e\n\n -- Show that any derivative is a valid function change.\n\n -- In the paper, this is never actually stated. We just prove that nil\n -- changes are derivatives; the paper keeps talking about \"the derivative\",\n -- suggesting derivatives are unique. If derivatives were unique, we could\n -- say that the nil change is *the* derivative, hence the derivative is the\n -- nil change (hence also a change).\n --\n -- In fact, derivatives are only unique up to change equivalence and\n -- extensional equality; this is proven in Base.Change.Equivalence.derivative-unique.\n --\n Derivative-is-valid : \u2200 {f : A \u2192 B} df (IsDerivative-f-df : IsDerivative f df) a da \u2192\n f (a \u229e da) \u229e (df (a \u229e da) (nil (a \u229e da))) \u2261 f a \u229e df a da\n Derivative-is-valid {f} df IsDerivative-f-df a da rewrite IsDerivative-f-df (a \u229e da) (nil (a \u229e da)) | update-nil (a \u229e da) = sym (IsDerivative-f-df a da)\n\n DerivativeAsChange : \u2200 {f : A \u2192 B} {df} (IsDerivative-f-df : IsDerivative f df) \u2192 \u0394 f\n DerivativeAsChange {df = df} IsDerivative-f-df = record { apply = df ; correct = Derivative-is-valid df IsDerivative-f-df }\n -- In Equivalence.agda, derivative-is-nil-alternative then proves that a derivative is also a nil change.\n\n-- Reexport a few members with A and B marked as implicit parameters. This\n-- matters especially for changeAlgebra, since otherwise it can't be used for\n-- instance search.\nmodule _\n {a} {b} {A : Set a} {B : Set b} {{CA : ChangeAlgebra A}} {{CB : ChangeAlgebra B}}\n where\n open FunctionChanges A B {{CA}} {{CB}} public\n using\n ( changeAlgebraFun\n ; apply\n ; correct\n ; incrementalization\n ; DerivativeAsChange\n ; FunctionChange\n )\n\n-- List (== Environment) Changes\n-- =============================\n--\n-- Here, we define a change structure on environments, given a\n-- change structure on the values in the environments. In the\n-- paper, we describe this in Definition 3.5. But note that this\n-- Agda formalization uses de Bruijn indices instead of names, so\n-- environments are just lists. Therefore, when we use Definition\n-- 3.5 in the paper, in this formalization, we use the list-like\n-- change structure defined here.\n\nopen import Data.List\nopen import Data.List.All\n\ndata All\u2032 {a p q} {A : Set a}\n {P : A \u2192 Set p}\n (Q : {x : A} \u2192 P x \u2192 Set q)\n : {xs : List A} (pxs : All P xs) \u2192 Set (p \u2294 q \u2294 a) where\n [] : All\u2032 Q []\n _\u2237_ : \u2200 {x xs} {px : P x} {pxs : All P xs} (qx : Q px) (qxs : All\u2032 Q pxs) \u2192 All\u2032 Q (px \u2237 pxs)\n\nmodule ListChanges\n {a} {A : Set a} (P : A \u2192 Set) {{C : ChangeAlgebraFamily P}}\n where\n update-all : \u2200 {xs} \u2192 (pxs : All P xs) \u2192 All\u2032 \u0394 pxs \u2192 All P xs\n update-all {[]} [] [] = []\n update-all {x \u2237 xs} (px \u2237 pxs) (dpx \u2237 dpxs) = (px \u229e dpx) \u2237 update-all pxs dpxs\n\n diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 All\u2032 \u0394 pxs\n diff-all [] [] = []\n diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = (px\u2032 \u229f px) \u2237 diff-all pxs\u2032 pxs\n\n update-diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 update-all pxs (diff-all pxs\u2032 pxs) \u2261 pxs\u2032\n update-diff-all [] [] = refl\n update-diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = cong\u2082 _\u2237_ (update-diff px\u2032 px) (update-diff-all pxs\u2032 pxs)\n\n instance\n changeAlgebraListChanges : ChangeAlgebraFamily (All P)\n\n changeAlgebraListChanges = record\n { change-algebra = \u03bb xs \u2192 record\n { Change = All\u2032 \u0394\n ; update = update-all\n ; diff = diff-all\n ; nil = \u03bb xs \u2192 diff-all xs xs\n ; isChangeAlgebra = record\n { update-diff = update-diff-all\n ; update-nil = \u03bb xs\u2081 \u2192 update-diff-all xs\u2081 xs\u2081\n }\n }\n }\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d9a3d68c58bd5c8ae49738e7a8f68fd5e89b4bce","subject":"Improve reuse (see #28).","message":"Improve reuse (see #28).\n\nOld-commit-hash: 7da11aabf971e192ab71542164b98c80cbe26325\n","repos":"inc-lc\/ilc-agda","old_file":"lambda.agda","new_file":"lambda.agda","new_contents":"module lambda where\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types public\nopen import Syntactic.Contexts Type public\n\nopen import Denotational.Notation\nopen import Denotational.Values public\nopen import Denotational.Environments Type \u27e6_\u27e7Type public\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n true false : \u2200 {\u0393} \u2192 Term \u0393 bool\n cond : \u2200 {\u0393 \u03c4} \u2192 (e\u2081 : Term \u0393 bool) (e\u2082 e\u2083 : Term \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\n\u27e6 true \u27e7Term \u03c1 = true\n\u27e6 false \u27e7Term \u03c1 = false\n\u27e6 cond t\u2081 t\u2082 t\u2083 \u27e7Term \u03c1 with \u27e6 t\u2081 \u27e7Term \u03c1\n... | true = \u27e6 t\u2082 \u27e7Term \u03c1\n... | false = \u27e6 t\u2083 \u27e7Term \u03c1\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n-- NATURAL SEMANTICS\n\n-- Syntax\n\ndata Env : Context \u2192 Set\ndata Val : Type \u2192 Set\n\ndata Val where\n \u27e8abs_,_\u27e9 : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) (\u03c1 : Env \u0393) \u2192 Val (\u03c4\u2081 \u21d2 \u03c4\u2082)\n true : Val bool\n false : Val bool\n\ndata Env where\n \u2205 : Env \u2205\n _\u2022_ : \u2200 {\u0393 \u03c4} \u2192 Val \u03c4 \u2192 Env \u0393 \u2192 Env (\u03c4 \u2022 \u0393)\n\n-- Lookup\n\ninfixr 8 _\u22a2_\u2193_ _\u22a2_\u21a6_\n\ndata _\u22a2_\u21a6_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Var \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n this : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {v : Val \u03c4} \u2192\n v \u2022 \u03c1 \u22a2 this \u21a6 v\n that : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 x} {\u03c1 : Env \u0393} {v\u2081 : Val \u03c4\u2081} {v\u2082 : Val \u03c4\u2082} \u2192\n \u03c1 \u22a2 x \u21a6 v\u2082 \u2192\n v\u2081 \u2022 \u03c1 \u22a2 that x \u21a6 v\u2082\n\n-- Reduction\n\ndata _\u22a2_\u2193_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Term \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c1} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n \u03c1 \u22a2 abs t \u2193 \u27e8abs t , \u03c1 \u27e9\n app : \u2200 {\u0393 \u0393\u2032 \u03c4\u2081 \u03c4\u2082 \u03c1 \u03c1\u2032 v\u2082 v\u2032} {t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2082 : Term \u0393 \u03c4\u2081} {t\u2032 : Term (\u03c4\u2081 \u2022 \u0393\u2032) \u03c4\u2082} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 \u27e8abs t\u2032 , \u03c1\u2032 \u27e9 \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n v\u2082 \u2022 \u03c1\u2032 \u22a2 t\u2032 \u2193 v\u2032 \u2192\n \u03c1 \u22a2 app t\u2081 t\u2082 \u2193 v\u2032\n var : \u2200 {\u0393 \u03c4 x} {\u03c1 : Env \u0393} {v : Val \u03c4}\u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u03c1 \u22a2 var x \u2193 v\n cond-true : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {t\u2081 t\u2082 t\u2083} {v\u2082 : Val \u03c4} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 true \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n \u03c1 \u22a2 cond t\u2081 t\u2082 t\u2083 \u2193 v\u2082\n cond-false : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {t\u2081 t\u2082 t\u2083} {v\u2083 : Val \u03c4} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 false \u2192\n \u03c1 \u22a2 t\u2083 \u2193 v\u2083 \u2192\n \u03c1 \u22a2 cond t\u2081 t\u2082 t\u2083 \u2193 v\u2083\n\n-- SOUNDNESS of natural semantics\n\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 Env \u0393 \u2192 \u27e6 \u0393 \u27e7\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 \u27e8abs t , \u03c1 \u27e9 \u27e7Val = \u03bb v \u2192 \u27e6 t \u27e7 (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\u27e6 true \u27e7Val = true\n\u27e6 false \u27e7Val = false\n\nmeaningOfEnv : \u2200 {\u0393} \u2192 Meaning (Env \u0393)\nmeaningOfEnv = meaning \u27e6_\u27e7Env\n\nmeaningOfVal : \u2200 {\u03c4} \u2192 Meaning (Val \u03c4)\nmeaningOfVal = meaning \u27e6_\u27e7Val\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) = trans (cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082)) (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n\u2193-sound (cond-true \u2193\u2081 \u2193\u2082) rewrite \u2193-sound \u2193\u2081 = \u2193-sound \u2193\u2082\n\u2193-sound (cond-false \u2193\u2081 \u2193\u2083) rewrite \u2193-sound \u2193\u2081 = \u2193-sound \u2193\u2083\n\n-- WEAKENING\n\n-- Weaken a term to a super context\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken {\u0393\u2081} {\u0393\u2082} (abs {\u03c4\u2081 = \u03c4} t) = abs (weaken {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} t)\nweaken {\u0393\u2081} {\u0393\u2082} (app t\u2081 t\u2082) = app (weaken {\u0393\u2081} {\u0393\u2082} t\u2081) (weaken {\u0393\u2081} {\u0393\u2082} t\u2082)\nweaken {\u0393\u2081} {\u0393\u2082} (var x) = var (lift {\u0393\u2081} {\u0393\u2082} x)\nweaken {\u0393\u2081} {\u0393\u2082} true = true\nweaken {\u0393\u2081} {\u0393\u2082} false = false\nweaken {\u0393\u2081} {\u0393\u2082} (cond e\u2081 e\u2082 e\u2083) = cond (weaken {\u0393\u2081} {\u0393\u2082} e\u2081) (weaken {\u0393\u2081} {\u0393\u2082} e\u2082) (weaken {\u0393\u2081} {\u0393\u2082} e\u2083)\n","old_contents":"module lambda where\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Denotational.Notation\n\n-- SIMPLE TYPES\n\n-- Syntax\n\ndata Type : Set where\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n bool : Type\n\ninfixr 5 _\u21d2_\n\n-- Denotational Semantics\n\ndata Bool : Set where\n true false : Bool\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 bool \u27e7Type = Bool\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\n-- TYPING CONTEXTS, VARIABLES and WEAKENING\n\nopen import Syntactic.Contexts Type public\nopen import Denotational.Environments Type \u27e6_\u27e7Type public\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n true false : \u2200 {\u0393} \u2192 Term \u0393 bool\n cond : \u2200 {\u0393 \u03c4} \u2192 (e\u2081 : Term \u0393 bool) (e\u2082 e\u2083 : Term \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\n\u27e6 true \u27e7Term \u03c1 = true\n\u27e6 false \u27e7Term \u03c1 = false\n\u27e6 cond t\u2081 t\u2082 t\u2083 \u27e7Term \u03c1 with \u27e6 t\u2081 \u27e7Term \u03c1\n... | true = \u27e6 t\u2082 \u27e7Term \u03c1\n... | false = \u27e6 t\u2083 \u27e7Term \u03c1\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n-- NATURAL SEMANTICS\n\n-- Syntax\n\ndata Env : Context \u2192 Set\ndata Val : Type \u2192 Set\n\ndata Val where\n \u27e8abs_,_\u27e9 : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) (\u03c1 : Env \u0393) \u2192 Val (\u03c4\u2081 \u21d2 \u03c4\u2082)\n true : Val bool\n false : Val bool\n\ndata Env where\n \u2205 : Env \u2205\n _\u2022_ : \u2200 {\u0393 \u03c4} \u2192 Val \u03c4 \u2192 Env \u0393 \u2192 Env (\u03c4 \u2022 \u0393)\n\n-- Lookup\n\ninfixr 8 _\u22a2_\u2193_ _\u22a2_\u21a6_\n\ndata _\u22a2_\u21a6_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Var \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n this : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {v : Val \u03c4} \u2192\n v \u2022 \u03c1 \u22a2 this \u21a6 v\n that : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 x} {\u03c1 : Env \u0393} {v\u2081 : Val \u03c4\u2081} {v\u2082 : Val \u03c4\u2082} \u2192\n \u03c1 \u22a2 x \u21a6 v\u2082 \u2192\n v\u2081 \u2022 \u03c1 \u22a2 that x \u21a6 v\u2082\n\n-- Reduction\n\ndata _\u22a2_\u2193_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Term \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c1} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n \u03c1 \u22a2 abs t \u2193 \u27e8abs t , \u03c1 \u27e9\n app : \u2200 {\u0393 \u0393\u2032 \u03c4\u2081 \u03c4\u2082 \u03c1 \u03c1\u2032 v\u2082 v\u2032} {t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2082 : Term \u0393 \u03c4\u2081} {t\u2032 : Term (\u03c4\u2081 \u2022 \u0393\u2032) \u03c4\u2082} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 \u27e8abs t\u2032 , \u03c1\u2032 \u27e9 \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n v\u2082 \u2022 \u03c1\u2032 \u22a2 t\u2032 \u2193 v\u2032 \u2192\n \u03c1 \u22a2 app t\u2081 t\u2082 \u2193 v\u2032\n var : \u2200 {\u0393 \u03c4 x} {\u03c1 : Env \u0393} {v : Val \u03c4}\u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u03c1 \u22a2 var x \u2193 v\n cond-true : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {t\u2081 t\u2082 t\u2083} {v\u2082 : Val \u03c4} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 true \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n \u03c1 \u22a2 cond t\u2081 t\u2082 t\u2083 \u2193 v\u2082\n cond-false : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {t\u2081 t\u2082 t\u2083} {v\u2083 : Val \u03c4} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 false \u2192\n \u03c1 \u22a2 t\u2083 \u2193 v\u2083 \u2192\n \u03c1 \u22a2 cond t\u2081 t\u2082 t\u2083 \u2193 v\u2083\n\n-- SOUNDNESS of natural semantics\n\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 Env \u0393 \u2192 \u27e6 \u0393 \u27e7\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 \u27e8abs t , \u03c1 \u27e9 \u27e7Val = \u03bb v \u2192 \u27e6 t \u27e7 (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\u27e6 true \u27e7Val = true\n\u27e6 false \u27e7Val = false\n\nmeaningOfEnv : \u2200 {\u0393} \u2192 Meaning (Env \u0393)\nmeaningOfEnv = meaning \u27e6_\u27e7Env\n\nmeaningOfVal : \u2200 {\u03c4} \u2192 Meaning (Val \u03c4)\nmeaningOfVal = meaning \u27e6_\u27e7Val\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) = trans (cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082)) (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n\u2193-sound (cond-true \u2193\u2081 \u2193\u2082) rewrite \u2193-sound \u2193\u2081 = \u2193-sound \u2193\u2082\n\u2193-sound (cond-false \u2193\u2081 \u2193\u2083) rewrite \u2193-sound \u2193\u2081 = \u2193-sound \u2193\u2083\n\n-- WEAKENING\n\n-- Weaken a term to a super context\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken {\u0393\u2081} {\u0393\u2082} (abs {\u03c4\u2081 = \u03c4} t) = abs (weaken {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} t)\nweaken {\u0393\u2081} {\u0393\u2082} (app t\u2081 t\u2082) = app (weaken {\u0393\u2081} {\u0393\u2082} t\u2081) (weaken {\u0393\u2081} {\u0393\u2082} t\u2082)\nweaken {\u0393\u2081} {\u0393\u2082} (var x) = var (lift {\u0393\u2081} {\u0393\u2082} x)\nweaken {\u0393\u2081} {\u0393\u2082} true = true\nweaken {\u0393\u2081} {\u0393\u2082} false = false\nweaken {\u0393\u2081} {\u0393\u2082} (cond e\u2081 e\u2082 e\u2083) = cond (weaken {\u0393\u2081} {\u0393\u2082} e\u2081) (weaken {\u0393\u2081} {\u0393\u2082} e\u2082) (weaken {\u0393\u2081} {\u0393\u2082} e\u2083)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a249c6b82c824f612dd5b7102544cf27e5484428","subject":"Cosmetic changes.","message":"Cosmetic changes.\n\nIgnore-this: 9ee4877688487f11241cec13ff852b2f\n\ndarcs-hash:20120120153646-3bd4e-cec9166ec957acc3b1450bf6c3b823058fc5a184.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Program\/GCD\/Total\/TotalityATP.agda","new_file":"src\/FOTC\/Program\/GCD\/Total\/TotalityATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Totality properties of the gcd\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Program.GCD.Total.TotalityATP where\n\nopen import Common.Function\n\nopen import FOTC.Base\nopen import FOTC.Base.Properties\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Induction.NonAcc.LexicographicATP\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.EliminationProperties\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Program.GCD.Total.Definitions\nopen import FOTC.Program.GCD.Total.GCD\n\n------------------------------------------------------------------------------\n-- gcd 0 0 is total.\npostulate gcd-00-N : N (gcd zero zero)\n{-# ATP prove gcd-00-N #-}\n\n------------------------------------------------------------------------------\n-- gcd 0 (succ n) is total.\npostulate gcd-0S-N : \u2200 {n} \u2192 N n \u2192 N (gcd zero (succ\u2081 n))\n{-# ATP prove gcd-0S-N #-}\n\n------------------------------------------------------------------------------\n-- gcd (succ\u2081 n) 0 is total.\npostulate gcd-S0-N : \u2200 {n} \u2192 N n \u2192 N (gcd (succ\u2081 n) zero)\n{-# ATP prove gcd-S0-N #-}\n\n------------------------------------------------------------------------------\n-- gcd (succ\u2081 m) (succ\u2081 n) when succ\u2081 m > succ\u2081 n is total.\npostulate\n gcd-S>S-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192\n N (gcd (succ\u2081 m \u2238 succ\u2081 n) (succ\u2081 n)) \u2192\n GT (succ\u2081 m) (succ\u2081 n) \u2192\n N (gcd (succ\u2081 m) (succ\u2081 n))\n-- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n{-# ATP prove gcd-S>S-N #-}\n\n------------------------------------------------------------------------------\n-- gcd (succ\u2081 m) (succ\u2081 n) when succ\u2081 m \u226f succ\u2081 n is total.\npostulate\n gcd-S\u226fS-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192\n N (gcd (succ\u2081 m) (succ\u2081 n \u2238 succ\u2081 m)) \u2192\n NGT (succ\u2081 m) (succ\u2081 n) \u2192\n N (gcd (succ\u2081 m) (succ\u2081 n))\n{-# ATP prove gcd-S\u226fS-N #-}\n\n------------------------------------------------------------------------------\n-- gcd m n when m > n is total.\ngcd-x>y-N :\n \u2200 {m n} \u2192 N m \u2192 N n \u2192\n (\u2200 {o p} \u2192 N o \u2192 N p \u2192 LT\u2082 o p m n \u2192 N (gcd o p)) \u2192\n GT m n \u2192\n N (gcd m n)\ngcd-x>y-N zN Nn _ 0>n = \u22a5-elim $ 0>x\u2192\u22a5 Nn 0>n\ngcd-x>y-N (sN Nm) zN _ _ = gcd-S0-N Nm\ngcd-x>y-N (sN {m} Nm) (sN {n} Nn) accH Sm>Sn =\n gcd-S>S-N Nm Nn ih Sm>Sn\n where\n -- Inductive hypothesis.\n ih : N (gcd (succ\u2081 m \u2238 succ\u2081 n) (succ\u2081 n))\n ih = accH {succ\u2081 m \u2238 succ\u2081 n}\n {succ\u2081 n}\n (\u2238-N (sN Nm) (sN Nn))\n (sN Nn)\n ([Sx\u2238Sy,Sy]<[Sx,Sy] Nm Nn)\n\n------------------------------------------------------------------------------\n-- gcd m n when m \u226f n is total.\ngcd-x\u226fy-N :\n \u2200 {m n} \u2192 N m \u2192 N n \u2192\n (\u2200 {o p} \u2192 N o \u2192 N p \u2192 LT\u2082 o p m n \u2192 N (gcd o p)) \u2192\n NGT m n \u2192\n N (gcd m n)\ngcd-x\u226fy-N zN zN _ _ = gcd-00-N\ngcd-x\u226fy-N zN (sN Nn) _ _ = gcd-0S-N Nn\ngcd-x\u226fy-N (sN {m} Nm) zN _ Sm\u226f0 = \u22a5-elim $ S\u226f0\u2192\u22a5 Sm\u226f0\ngcd-x\u226fy-N (sN {m} Nm) (sN {n} Nn) accH Sm\u226fSn = gcd-S\u226fS-N Nm Nn ih Sm\u226fSn\n where\n -- Inductive hypothesis.\n ih : N (gcd (succ\u2081 m) (succ\u2081 n \u2238 succ\u2081 m))\n ih = accH {succ\u2081 m}\n {succ\u2081 n \u2238 succ\u2081 m}\n (sN Nm)\n (\u2238-N (sN Nn) (sN Nm))\n ([Sx,Sy\u2238Sx]<[Sx,Sy] Nm Nn)\n\n------------------------------------------------------------------------------\n-- gcd m n is total.\ngcd-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (gcd m n)\ngcd-N = wfInd-LT\u2082 P istep\n where\n P : D \u2192 D \u2192 Set\n P i j = N (gcd i j)\n\n istep : \u2200 {i j} \u2192 N i \u2192 N j \u2192 (\u2200 {k l} \u2192 N k \u2192 N l \u2192 LT\u2082 k l i j \u2192 P k l) \u2192\n P i j\n istep Ni Nj accH =\n [ gcd-x>y-N Ni Nj accH\n , gcd-x\u226fy-N Ni Nj accH\n ] (x>y\u2228x\u226fy Ni Nj)\n","old_contents":"------------------------------------------------------------------------------\n-- Totality properties of the gcd\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Program.GCD.Total.TotalityATP where\n\nopen import Common.Function\n\nopen import FOTC.Base\nopen import FOTC.Base.Properties\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Induction.NonAcc.LexicographicATP\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.EliminationProperties\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Program.GCD.Total.Definitions\nopen import FOTC.Program.GCD.Total.GCD\n\n------------------------------------------------------------------------------\n-- gcd 0 0 is total.\npostulate gcd-00-N : N (gcd zero zero)\n{-# ATP prove gcd-00-N #-}\n\n------------------------------------------------------------------------------\n-- gcd 0 (succ n) is total.\npostulate gcd-0S-N : \u2200 {n} \u2192 N n \u2192 N (gcd zero (succ\u2081 n))\n{-# ATP prove gcd-0S-N #-}\n\n------------------------------------------------------------------------------\n-- gcd (succ\u2081 n) 0 is total.\npostulate gcd-S0-N : \u2200 {n} \u2192 N n \u2192 N (gcd (succ\u2081 n) zero)\n{-# ATP prove gcd-S0-N #-}\n\n------------------------------------------------------------------------------\n-- gcd (succ\u2081 m) (succ\u2081 n) when succ\u2081 m > succ\u2081 n is total.\npostulate\n gcd-S>S-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192\n N (gcd (succ\u2081 m \u2238 succ\u2081 n) (succ\u2081 n)) \u2192\n GT (succ\u2081 m) (succ\u2081 n) \u2192\n N (gcd (succ\u2081 m) (succ\u2081 n))\n-- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n{-# ATP prove gcd-S>S-N #-}\n\n------------------------------------------------------------------------------\n-- gcd (succ\u2081 m) (succ\u2081 n) when succ\u2081 m \u226f succ\u2081 n is total.\npostulate\n gcd-S\u226fS-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192\n N (gcd (succ\u2081 m) (succ\u2081 n \u2238 succ\u2081 m)) \u2192\n NGT (succ\u2081 m) (succ\u2081 n) \u2192\n N (gcd (succ\u2081 m) (succ\u2081 n))\n{-# ATP prove gcd-S\u226fS-N #-}\n\n------------------------------------------------------------------------------\n-- gcd m n when m > n is total.\ngcd-x>y-N :\n \u2200 {m n} \u2192 N m \u2192 N n \u2192\n (\u2200 {o p} \u2192 N o \u2192 N p \u2192 LT\u2082 o p m n \u2192 N (gcd o p)) \u2192\n GT m n \u2192\n N (gcd m n)\ngcd-x>y-N zN Nn _ 0>n = \u22a5-elim $ 0>x\u2192\u22a5 Nn 0>n\ngcd-x>y-N (sN Nm) zN _ _ = gcd-S0-N Nm\ngcd-x>y-N (sN {m} Nm) (sN {n} Nn) accH Sm>Sn =\n gcd-S>S-N Nm Nn ih Sm>Sn\n where\n -- Inductive hypothesis.\n ih : N (gcd (succ\u2081 m \u2238 succ\u2081 n) (succ\u2081 n))\n ih = accH {succ\u2081 m \u2238 succ\u2081 n}\n {succ\u2081 n}\n (\u2238-N (sN Nm) (sN Nn))\n (sN Nn)\n ([Sx\u2238Sy,Sy]<[Sx,Sy] Nm Nn)\n\n------------------------------------------------------------------------------\n-- gcd m n when m \u226f n is total.\ngcd-x\u226fy-N :\n \u2200 {m n} \u2192 N m \u2192 N n \u2192\n (\u2200 {o p} \u2192 N o \u2192 N p \u2192 LT\u2082 o p m n \u2192 N (gcd o p)) \u2192\n NGT m n \u2192\n N (gcd m n)\ngcd-x\u226fy-N zN zN _ _ = gcd-00-N\ngcd-x\u226fy-N zN (sN Nn) _ _ = gcd-0S-N Nn\ngcd-x\u226fy-N (sN {m} Nm) zN _ Sm\u226f0 = \u22a5-elim $ S\u226f0\u2192\u22a5 Sm\u226f0\ngcd-x\u226fy-N (sN {m} Nm) (sN {n} Nn) accH Sm\u226fSn =\n gcd-S\u226fS-N Nm Nn ih Sm\u226fSn\n where\n -- Inductive hypothesis.\n ih : N (gcd (succ\u2081 m) (succ\u2081 n \u2238 succ\u2081 m))\n ih = accH {succ\u2081 m}\n {succ\u2081 n \u2238 succ\u2081 m}\n (sN Nm)\n (\u2238-N (sN Nn) (sN Nm))\n ([Sx,Sy\u2238Sx]<[Sx,Sy] Nm Nn)\n\n------------------------------------------------------------------------------\n-- gcd m n is total.\ngcd-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (gcd m n)\ngcd-N = wfInd-LT\u2082 P istep\n where\n P : D \u2192 D \u2192 Set\n P i j = N (gcd i j)\n\n istep : \u2200 {i j} \u2192 N i \u2192 N j \u2192 (\u2200 {k l} \u2192 N k \u2192 N l \u2192 LT\u2082 k l i j \u2192 P k l) \u2192\n P i j\n istep Ni Nj accH =\n [ gcd-x>y-N Ni Nj accH\n , gcd-x\u226fy-N Ni Nj accH\n ] (x>y\u2228x\u226fy Ni Nj)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"84495ac29d0b2817ceb8faeb8f9c51c0dc4c1d44","subject":"IP: update","message":"IP: update\n","repos":"crypto-agda\/protocols","old_file":"Control\/Protocol\/IP.agda","new_file":"Control\/Protocol\/IP.agda","new_contents":"open import Type\nopen import Function\nopen import Data.One\nopen import Data.Two\nopen import Data.Nat\nopen import Data.Product\nopen import Data.Maybe\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\n\nopen import Control.Protocol\n\nmodule Control.Protocol.IP (Query : \u2605) (Resp : Query \u2192 \u2605) where\n\nAccept? = \ud835\udfda\naccept! = 1\u2082\nreject! = 0\u2082\nAccept! = \u2713\n\n-- One round from the point of view of the verifier\nVerifierRound : Proto\nVerifierRound = send \u03bb (q : Query) \u2192\n recv \u03bb (r : Resp q) \u2192\n end\n\nProverRound : Proto\nProverRound = dual VerifierRound\n\nmodule Rounds (#rounds : \u2115) where\n Verifier-rounds\u1d3e = replicate\u1d3e #rounds VerifierRound\n\n Verifier-end\u1d3e = send \u03bb (_ : Accept?) \u2192 end\n\n Verifier\u1d3e = Verifier-rounds\u1d3e >> Verifier-end\u1d3e\n\n Prover-end\u1d3e = dual Verifier-end\u1d3e\n\n Prover\u1d3e = dual Verifier\u1d3e\n\n Transcript\u1d3e = source-of Verifier\u1d3e\n\n Prover : \u2605\n Prover = \u27e6 Prover\u1d3e \u27e7\n\n Verifier : \u2605\n Verifier = \u27e6 Verifier\u1d3e \u27e7\n\n Transcript : \u2605\n Transcript = \u27e6 Transcript\u1d3e \u27e7\n\n >>-Transcript : \u2605\n >>-Transcript = Log Verifier-rounds\u1d3e \u00d7 \u27e6 Verifier-end\u1d3e \u27e7 \u00d7 \u27e6 Verifier-end\u1d3e \u22a5\u27e7\n\n accepting-transcript? : >>-Transcript \u2192 Accept?\n accepting-transcript? (_ , (a , _) , _) = a\n\n _\u21c6_ : Prover \u2192 Verifier \u2192 Accept?\n p \u21c6 v = accepting-transcript? (>>-telecom Verifier-rounds\u1d3e v p)\n\nrecord IP (#rounds : \u2115) {A : \u2605} (\u2112 : A \u2192 \u2605) : \u2605 where\n open Rounds #rounds public\n\n field\n verifier : Verifier\n\n Convincing : Prover \u2192 \u2605\n Convincing = \u03bb p \u2192 Accept! (p \u21c6 verifier)\n\n Complete : \u2605\n Complete = \u2200 x \u2192 \u2112 x \u2192 \u03a3 Prover Convincing\n\n Sound : \u2605\n Sound = \u2200 x \u2192 \u00ac(\u2112 x) \u2192 (p : Prover) \u2192 \u00ac(Convincing p)\n\nNP-Verifier : \u2605\nNP-Verifier = \u03a3 Query \u03bb q \u2192 Resp q \u2192 Accept? \u00d7 End\n\nNP-Verifier\u2261Verifier1 : NP-Verifier \u2261 Rounds.Verifier 1\nNP-Verifier\u2261Verifier1 = refl\n\nNP : {A : \u2605} (\u2112 : A \u2192 \u2605) \u2192 \u2605\nNP = IP 1\n\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"open import Type\nopen import Function\nopen import Control.Protocol.Choreography\nopen import Data.One\nopen import Data.Nat\nopen import Data.Product\nopen import Data.Maybe\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\n\nmodule Control.Protocol.IP (Query : \u2605) (Resp : Query \u2192 \u2605) where\n\n-- One round from the point of view of the verifier\nVerifierRound : Proto\nVerifierRound = \u03a3\u1d3e Query \u03bb q \u2192\n \u03a0\u1d3e (Resp q) \u03bb r \u2192\n end\n\nProverRound : Proto\nProverRound = dual VerifierRound\n\nmodule Rounds (#rounds : \u2115) where\n Verifier\u1d3e : Proto\n Verifier\u1d3e = replicate\u1d3e #rounds VerifierRound >> (\u03a3\u1d3e Accept? \u03bb _ \u2192 end)\n\n Prover\u1d3e : Proto\n Prover\u1d3e = dual Verifier\u1d3e\n\n Transcript\u1d3e : Proto\n Transcript\u1d3e = source-of Verifier\u1d3e\n\n Prover : \u2605\n Prover = \u27e6 Prover\u1d3e \u27e7\n\n Verifier : \u2605\n Verifier = \u27e6 Verifier\u1d3e \u27e7\n\n Transcript : \u2605\n Transcript = \u27e6 Transcript\u1d3e \u27e7\n\n _\u21c6_ : Prover \u2192 Verifier \u2192 Accept?\n _\u21c6_ = \u03bb p v \u2192 case >>-com (replicate\u1d3e #rounds VerifierRound) v p of \u03bb { (_ , (a , _) , _) \u2192 a }\n\nrecord IP (#rounds : \u2115) {A : \u2605} (\u2112 : A \u2192 \u2605) : \u2605 where\n open Rounds #rounds public\n\n field\n verifier : Verifier\n\n Convincing : Prover \u2192 \u2605\n Convincing = \u03bb p \u2192 Is-accept (p \u21c6 verifier)\n\n Complete : \u2605\n Complete = \u2200 x \u2192 \u2112 x \u2192 \u03a3 Prover Convincing\n\n Sound : \u2605\n Sound = \u2200 x \u2192 \u00ac(\u2112 x) \u2192 (p : Prover) \u2192 \u00ac(Convincing p)\n\nNP-Verifier = \u03a3 Query \u03bb q \u2192 Resp q \u2192 Accept? \u00d7 \ud835\udfd9\n\nNP-Verifier\u2261Verifier1 : NP-Verifier \u2261 Rounds.Verifier 1\nNP-Verifier\u2261Verifier1 = refl\n\nNP : {A : \u2605} (\u2112 : A \u2192 \u2605) \u2192 \u2605\nNP = IP 1\n\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"fc40c04b0d9ef151e73902e8feab75d63d0b32b4","subject":"Modified the postulate about the post-fixed point of BISI.","message":"Modified the postulate about the post-fixed point of BISI.\n\nIgnore-this: b89225245b6b620ae7b127c781a39f03\n\ndarcs-hash:20101101113300-3bd4e-62d3bad9121044a9f69c855c0d7438b4261ab3aa.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/Bisimilarity\/BisimilarityPostFixedPoint.agda","new_file":"Draft\/Bisimilarity\/BisimilarityPostFixedPoint.agda","new_contents":"module BisimilarityPostFixedPoint where\n\n-- open import LTC.Minimal\n-- open import LTC.MinimalER\n\ninfixr 5 _\u2237_\ninfix 4 _\u2248_\ninfixr 4 _,_\ninfix 3 _\u2261_\ninfixr 2 _\u2227_\n\n------------------------------------------------------------------------------\n-- LTC stuff\n\n-- The universal domain.\npostulate D : Set\n\n -- LTC lists.\npostulate\n _\u2237_ : D \u2192 D \u2192 D\n\n-- The existential quantifier type on D.\ndata \u2203D (P : D \u2192 Set) : Set where\n _,_ : (d : D) (Pd : P d) \u2192 \u2203D P\n\n\u2203D-proj\u2081 : {P : D \u2192 Set} \u2192 \u2203D P \u2192 D\n\u2203D-proj\u2081 (x , _ ) = x\n\n\u2203D-proj\u2082 : {P : D \u2192 Set} \u2192 (x-px : \u2203D P) \u2192 P (\u2203D-proj\u2081 x-px)\n\u2203D-proj\u2082 (_ , px) = px\n\n-- The identity type on D.\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n-- Identity properties\n\nsym : {x y : D} \u2192 x \u2261 y \u2192 y \u2261 x\nsym refl = refl\n\nsubst : (P : D \u2192 Set){x y : D} \u2192 x \u2261 y \u2192 P x \u2192 P y\nsubst P refl px = px\n\n-- The conjunction data type.\ndata _\u2227_ (A B : Set) : Set where\n _,_ : A \u2192 B \u2192 A \u2227 B\n\n\u2227-proj\u2081 : {A B : Set} \u2192 A \u2227 B \u2192 A\n\u2227-proj\u2081 (x , y) = x\n\n\u2227-proj\u2082 : {A B : Set} \u2192 A \u2227 B \u2192 B\n\u2227-proj\u2082 (x , y) = y\n\n------------------------------------------------------------------------------\n\nBISI : (D \u2192 D \u2192 Set) \u2192 D \u2192 D \u2192 Set\nBISI R xs ys =\n \u2203D (\u03bb x' \u2192\n \u2203D (\u03bb y' \u2192\n \u2203D (\u03bb xs' \u2192\n \u2203D (\u03bb ys' \u2192\n x' \u2261 y' \u2227 R xs' ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 y' \u2237 ys'))))\n\npostulate\n -- The bisimilarity relation.\n _\u2248_ : D \u2192 D \u2192 Set\n\n -- The bisimilarity relation is a post-fixed point of BISI.\n -\u2248-gfp\u2081 : {xs ys : D} \u2192 xs \u2248 ys \u2192\n \u2203D (\u03bb x' \u2192\n \u2203D (\u03bb xs' \u2192\n \u2203D (\u03bb ys' \u2192 xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys')))\n\n-\u2248\u2192BISI\u2248 : (xs ys : D) \u2192 xs \u2248 ys \u2192 BISI _\u2248_ xs ys\n-\u2248\u2192BISI\u2248 xs ys xs\u2248ys =\n x' , x' , xs' , ys' , refl , xs'\u2248ys' , xs\u2261x'\u2237xs' , ys\u2261x'\u2237ys'\n where\n x' : D\n x' = \u2203D-proj\u2081 (-\u2248-gfp\u2081 xs\u2248ys)\n\n xs' : D\n xs' = \u2203D-proj\u2081 (\u2203D-proj\u2082 (-\u2248-gfp\u2081 xs\u2248ys))\n\n ys' : D\n ys' = \u2203D-proj\u2081 (\u2203D-proj\u2082 (\u2203D-proj\u2082 (-\u2248-gfp\u2081 xs\u2248ys)))\n\n aux : xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n aux = \u2203D-proj\u2082 (\u2203D-proj\u2082 (\u2203D-proj\u2082 (-\u2248-gfp\u2081 xs\u2248ys)))\n\n xs'\u2248ys' : xs' \u2248 ys'\n xs'\u2248ys' = \u2227-proj\u2081 aux\n\n xs\u2261x'\u2237xs' : xs \u2261 x' \u2237 xs'\n xs\u2261x'\u2237xs' = \u2227-proj\u2081 (\u2227-proj\u2082 aux)\n\n ys\u2261x'\u2237ys' : ys \u2261 x' \u2237 ys'\n ys\u2261x'\u2237ys' = \u2227-proj\u2082 (\u2227-proj\u2082 aux)\n","old_contents":"module BisimilarityPostFixedPoint where\n\n-- open import LTC.Minimal\n-- open import LTC.MinimalER\n\ninfixr 5 _\u2237_\ninfix 4 _\u2248_\ninfixr 4 _,_\ninfix 3 _\u2261_\ninfixr 2 _\u2227_\n\n------------------------------------------------------------------------------\n-- LTC stuff\n\n-- The universal domain.\npostulate D : Set\n\n -- LTC lists.\npostulate\n _\u2237_ : D \u2192 D \u2192 D\n\n-- The existential quantifier type on D.\ndata \u2203D (P : D \u2192 Set) : Set where\n _,_ : (d : D) (Pd : P d) \u2192 \u2203D P\n\n-- The identity type on D.\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n-- Identity properties\n\nsym : {x y : D} \u2192 x \u2261 y \u2192 y \u2261 x\nsym refl = refl\n\nsubst : (P : D \u2192 Set){x y : D} \u2192 x \u2261 y \u2192 P x \u2192 P y\nsubst P refl px = px\n\n-- The conjunction data type.\ndata _\u2227_ (A B : Set) : Set where\n _,_ : A \u2192 B \u2192 A \u2227 B\n\n------------------------------------------------------------------------------\nBISI : (D \u2192 D \u2192 Set) \u2192 D \u2192 D \u2192 Set\nBISI R xs ys =\n \u2203D (\u03bb x' \u2192\n \u2203D (\u03bb y' \u2192\n \u2203D (\u03bb xs' \u2192\n \u2203D (\u03bb ys' \u2192\n x' \u2261 y' \u2227 R xs' ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 y' \u2237 ys'))))\n\n-- From Peter email:\n\n-- For the case of bisimilarity \u2248 we have\n\n-- (i) a post-fixed point of BISI, is the following first order axiom\n\n-- forall xs,ys,x,y. x = y & xs \u2248 ys -> x :: xs \u2248 y :: ys\n\npostulate\n -- The bisimilarity relation.\n _\u2248_ : D \u2192 D \u2192 Set\n\n -- The bisimilarity relation is a post-fixed point of BISI.\n -\u2248-gfp\u2081 : {x y xs ys : D} \u2192 x \u2261 y \u2227 xs \u2248 ys \u2192 x \u2237 xs \u2248 y \u2237 ys\n\nfoo : (xs ys : D) \u2192 xs \u2248 ys \u2192 BISI _\u2248_ xs ys\nfoo xs ys xs\u2248ys = {!!}\n\nbar : (xs ys : D) \u2192 BISI _\u2248_ xs ys \u2192 xs \u2248 ys\nbar xs ys (x' , y' , xs' , ys' , x'\u2261y' , xs'\u2248ys' , xs\u2261x'\u2237xs' , ys\u2261y'\u2237ys)\n = subst (\u03bb t\u2081 \u2192 t\u2081 \u2248 ys)\n (sym xs\u2261x'\u2237xs')\n (subst (\u03bb t\u2082 \u2192 x' \u2237 xs' \u2248 t\u2082)\n (sym ys\u2261y'\u2237ys)\n (-\u2248-gfp\u2081 (x'\u2261y' , xs'\u2248ys')))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"feafec5e00a3ade91e928f7090883031876b8747","subject":"FunUniverse.Agda: Two can be left abstract now","message":"FunUniverse.Agda: Two can be left abstract now\n","repos":"crypto-agda\/crypto-agda","old_file":"FunUniverse\/Agda.agda","new_file":"FunUniverse\/Agda.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule FunUniverse.Agda where\n\nopen import Type\nopen import Data.Two using (proj\u2032)\nimport Data.Vec.NP as V\nimport Function as F\nimport Data.Product as \u00d7\nopen F using (const; _\u2218\u2032_)\nopen V using ([]; _\u2237_)\nopen \u00d7 using (_\u00d7_; _,_; proj\u2081; proj\u2082; uncurry)\n\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Two using (\ud835\udfda)\nopen import Data.Vec using (Vec)\nopen import Data.Bit using (0b; 1b)\n\nopen import FunUniverse.Data\nopen import FunUniverse.Category\nopen import FunUniverse.Rewiring.Linear\nopen import FunUniverse.Core\n\n-\u2192- : \u2605 \u2192 \u2605 \u2192 \u2605\n-\u2192- A B = A \u2192 B\n\nfunCat : Category -\u2192-\nfunCat = F.id , _\u2218\u2032_\n\nmodule Abstract\ud835\udfda (some\ud835\udfda : \u2605) where\n\n private\n \u2605-U' : Universe \u2605\u2080\n \u2605-U' = mk \ud835\udfd9 some\ud835\udfda _\u00d7_ Vec\n\n agdaFunU : FunUniverse \u2605\n agdaFunU = \u2605-U' , -\u2192-\n\n module AgdaFunUniverse = FunUniverse agdaFunU\n \n funLin : LinRewiring agdaFunU\n funLin = mk funCat\n (\u03bb f \u2192 \u00d7.map f F.id)\n \u00d7.swap (\u03bb {((x , y) , z) \u2192 x , (y , z) }) (\u03bb x \u2192 _ , x) proj\u2082\n (\u03bb f g \u2192 \u00d7.map f g) (\u03bb f \u2192 \u00d7.map F.id f)\n (F.const []) _ (uncurry _\u2237_) V.uncons\n\n funRewiring : Rewiring agdaFunU\n funRewiring = mk funLin _ (\u03bb x \u2192 x , x) (F.const []) \u00d7.<_,_> proj\u2081 proj\u2082\n V.rewire V.rewireTbl\n\nopen Abstract\ud835\udfda \ud835\udfda public\n\nfunFork : HasFork agdaFunU\nfunFork = (\u03bb { (b , xy) \u2192 proj\u2032 xy b })\n , (\u03bb { f g (b , x) \u2192 proj\u2032 (f , g) b x })\n\nagdaFunOps : FunOps agdaFunU\nagdaFunOps = mk funRewiring funFork (F.const 0b) (F.const 1b)\n\nmodule AgdaFunOps = FunOps agdaFunOps\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule FunUniverse.Agda where\n\nopen import Type\nopen import Data.Two using (proj\u2032)\nimport Data.Vec.NP as V\nimport Function as F\nimport Data.Product as \u00d7\nopen F using (const; _\u2218\u2032_)\nopen V using ([]; _\u2237_)\nopen \u00d7 using (_\u00d7_; _,_; proj\u2081; proj\u2082; uncurry)\n\nopen import Data.Bit using (0b; 1b)\n\nopen import FunUniverse.Data\nopen import FunUniverse.Category\nopen import FunUniverse.Rewiring.Linear\nopen import FunUniverse.Core\n\n-\u2192- : \u2605 \u2192 \u2605 \u2192 \u2605\n-\u2192- A B = A \u2192 B\n\nagdaFunU : FunUniverse \u2605\nagdaFunU = \u2605-U , -\u2192-\n\nmodule AgdaFunUniverse = FunUniverse agdaFunU\n\nfunCat : Category -\u2192-\nfunCat = F.id , _\u2218\u2032_\n\nfunLin : LinRewiring agdaFunU\nfunLin = mk funCat\n (\u03bb f \u2192 \u00d7.map f F.id)\n \u00d7.swap (\u03bb {((x , y) , z) \u2192 x , (y , z) }) (\u03bb x \u2192 _ , x) proj\u2082\n (\u03bb f g \u2192 \u00d7.map f g) (\u03bb f \u2192 \u00d7.map F.id f)\n (F.const []) _ (uncurry _\u2237_) V.uncons\n\nfunRewiring : Rewiring agdaFunU\nfunRewiring = mk funLin _ (\u03bb x \u2192 x , x) (F.const []) \u00d7.<_,_> proj\u2081 proj\u2082\n V.rewire V.rewireTbl\n\nfunFork : HasFork agdaFunU\nfunFork = (\u03bb { (b , xy) \u2192 proj\u2032 xy b })\n , (\u03bb { f g (b , x) \u2192 proj\u2032 (f , g) b x })\n\nagdaFunOps : FunOps agdaFunU\nagdaFunOps = mk funRewiring funFork (F.const 0b) (F.const 1b)\n\nmodule AgdaFunOps = FunOps agdaFunOps\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"19e8dae6dd8e0a7eff78868e12bf17f07250eaa9","subject":"Complete correctness proof for calculus with bags (Fixes #51)","message":"Complete correctness proof for calculus with bags (Fixes #51)\n\nOld-commit-hash: 93097a6e0bdf65daa5b90ce77bb36b077316fff5\n","repos":"inc-lc\/ilc-agda","old_file":"experimental\/NatBag.agda","new_file":"experimental\/NatBag.agda","new_contents":"{-\nThe goal of this file is to make the 3rd example\ndescribed in \/examples.md, \"Map.mapValues\", fast:\n\n inc :: Bag Integer -> Bag Integer\n inc = map (+1)\n\n old = fromList [1, 2 .. n - 1, n]\n res = inc old = [2, 3 .. n, n + 1]\n\nTODO\n1. Make sure this file has no hole\n X. Replace \u2115 by \u2124\n X. Replace \u2124 by \u2115 -- our bags are bags of nats now.\n X. Introduce addition\n X. Add MapBags and map\n2. Test it out with `inc` as primitive\n3. Finish ExplicitNils\n 0. Figure out a way to communicate to a derivative that\n certain changes are always nil (in this case, `+1`).\n4. Consider appending ExplicitNils\n\n\nChecklist: Adding syntactic constructs\n\n- weaken\n- \u27e6_\u27e7Term\n- weaken-sound\n- derive (symbolic derivation)\n- validity-of-derive\n- correctness-of-derive\n\nChecklist: Adding types\n\n- \u27e6_\u27e7Type\n- \u0394-Type\n- \u27e6derive\u27e7\n- _\u27e6\u229d\u27e7_\n- _\u27e6\u2295\u27e7_\n- f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f\n- f\u2295[g\u229df]=g\n- f\u2295\u0394f=f\n- valid-\u0394\n- R[f,g\u229df]\n- df=f\u2295df\u229df\n- R (inside validity-of-derive)\n\n-}\n\nmodule NatBag where\n\nopen import Data.NatBag renaming\n (map to mapBag ; empty to emptyBag ; update to updateBag)\nopen import Relation.Binary.PropositionalEquality\n\n-- This module has holes and can't be imported.\n-- We postulate necessary properties now to be able\n-- to work on derivation and remove them later.\n--\n-- Perhaps proving that bags form a group with\n-- emptyBag, ++, \\\\ is a necessity?\n--\n-- open import Data.NatBag.Properties\npostulate b\\\\b=\u2205 : \u2200 {{b : Bag}} \u2192 b \\\\ b \u2261 emptyBag\npostulate b++\u2205=b : \u2200 {{b : Bag}} \u2192 b ++ emptyBag \u2261 b\npostulate \u2205++b=b : \u2200 {{b : Bag}} \u2192 emptyBag ++ b \u2261 b\npostulate b++[d\\\\b]=d : \u2200 {b d} \u2192 b ++ (d \\\\ b) \u2261 d\npostulate [b++d]\\\\b=d : \u2200 {b d} \u2192 (b ++ d) \\\\ b \u2261 d\n\n-- postulate a\\\\[b++c]=a\\\\b\\\\c : \u2200 {a b c} \u2192 a \\\\ (b ++ c) \u2261 a \\\\ b \\\\ c\n-- postulate [a++b]\\\\c=a\\\\c++b : \u2200 {a b c} \u2192 (a ++ b) \\\\ c \u2261 a \\\\ c ++ b\n-- postulate [a++b]\\\\c=a++b\\\\c : \u2200 {a b c} \u2192 (a ++ b) \\\\ c \u2261 a ++ b \\\\ c\n-- \n-- and consequently:\n\npostulate\n [a++b]\\\\[c++d]=[a\\\\c]++[b\\\\d] : \u2200 {a b c d} \u2192\n (a ++ b) \\\\ (c ++ d) \u2261 (a \\\\ c) ++ (b \\\\ d)\npostulate\n [a\\\\b]\\\\[c\\\\d]=[a\\\\c]\\\\[b\\\\d] : \u2200 {a b c d} \u2192\n (a \\\\ b) \\\\ (c \\\\ d) \u2261 (a \\\\ c) \\\\ (b \\\\ d)\n\nopen import Data.Nat\n\nopen import Data.Unit using\n (\u22a4 ; tt)\n\nimport Data.Integer as \u2124\n\nopen import Data.Product using\n (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\nimport Level\nimport Data.Product as Product\n\npostulate extensionality : Extensionality Level.zero Level.zero\n\n\ndata Type : Set where\n nats : Type\n bags : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u03c4 \u0393} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u03c4 \u03c4\u2032 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n bag : \u2200 {\u0393} \u2192 (b : Bag) \u2192 Term \u0393 bags\n add : \u2200 {\u0393} \u2192 (t\u2081 : Term \u0393 nats) \u2192 (t\u2082 : Term \u0393 nats) \u2192 Term \u0393 nats\n map : \u2200 {\u0393} \u2192 (f : Term \u0393 (nats \u21d2 nats)) \u2192 (b : Term \u0393 bags) \u2192 Term \u0393 bags\n union : \u2200 {\u0393} \u2192 (b\u2081 : Term \u0393 bags) \u2192 (b\u2082 : Term \u0393 bags) \u2192 Term \u0393 bags\n diff : \u2200 {\u0393} \u2192 (b\u2081 : Term \u0393 bags) \u2192 (b\u2082 : Term \u0393 bags) \u2192 Term \u0393 bags\n\n var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n -- Change to nats = replacement Church pairs\n -- 3 -> 5 ::= \u03bbf. f 3 5\n\n -- Change to bags = a summand\n -- b\u2081 -> b\u2082 ::= b\u2082 \\\\ b\u2081\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u0393\u227c\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0393\n\u0393\u227c\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0393 {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u0393\u227c\u0393 {\u0393}\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\nweaken subctx (bag b) = bag b\nweaken subctx (add t\u2081 t\u2082) = add (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (map f b) = map (weaken subctx f) (weaken subctx b)\nweaken subctx (union b\u2081 b\u2082) = union (weaken subctx b\u2081) (weaken subctx b\u2082)\nweaken subctx (diff b\u2081 b\u2082) = diff (weaken subctx b\u2081) (weaken subctx b\u2082)\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 nats \u27e7Type = \u2115\n\u27e6 bags \u27e7Type = Bag\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nidentity-weakening : \u2200 {\u0393} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192 \u27e6 \u0393\u227c\u0393 {\u0393} \u27e7 \u03c1 \u2261 \u03c1\nidentity-weakening {\u2205} {\u2205} = refl\nidentity-weakening {\u03c4 \u2022 \u0393} {v \u2022 \u03c1} =\n cong\u2082 _\u2022_ {x = v} refl (identity-weakening {\u0393} {\u03c1})\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\u27e6 bag b \u27e7Term \u03c1 = b\n\u27e6 add m n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 map f b \u27e7Term \u03c1 = mapBag (\u27e6 f \u27e7Term \u03c1) (\u27e6 b \u27e7Term \u03c1)\n\u27e6 union b d \u27e7Term \u03c1 = \u27e6 b \u27e7Term \u03c1 ++ \u27e6 d \u27e7Term \u03c1\n\u27e6 diff b d \u27e7Term \u03c1 = \u27e6 b \u27e7Term \u03c1 \\\\ \u27e6 d \u27e7Term \u03c1\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n_\u27e8$\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\n\n-- infix 0 $ in Haskell\ninfixl 0 _\u27e8$\u27e9_\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = weaken-sound t\u2081 \u03c1 \u27e8$\u27e9 weaken-sound t\u2082 \u03c1\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (bag b) \u03c1 = refl\nweaken-sound (add m n) \u03c1 = cong\u2082 _+_ (weaken-sound m \u03c1) (weaken-sound n \u03c1)\nweaken-sound (map f b) \u03c1 =\n cong\u2082 mapBag (weaken-sound f \u03c1) (weaken-sound b \u03c1)\nweaken-sound (union b d) \u03c1 =\n cong\u2082 _++_ (weaken-sound b \u03c1) (weaken-sound d \u03c1)\nweaken-sound (diff b d) \u03c1 =\n cong\u2082 _\\\\_ (weaken-sound b \u03c1) (weaken-sound d \u03c1)\n\n-- Changes to \u2115 are replacement Church pairs. The only arguments\n-- of conern are `fst` and `snd`, so the Church pairs don't have\n-- to be polymorphic.\n--\n-- Changes on bags are bags. They allow negative multiplicities\n-- to begin with.\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type nats = (nats \u21d2 nats \u21d2 nats) \u21d2 nats\n\u0394-Type bags = bags\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393 -- push \u03c4, then push \u0394\u03c4.\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 (keep \u03c4 \u2022 \u0393\u227c\u0394\u0393)\n\n-- It is clear that \u27e6\u229d\u27e7 exists on the semantic level:\n-- there exists an Agda value to describe the change between any\n-- two Agda values denoted by terms. If we have (not dependently-\n-- typed) arrays, no term denotes the change between two arrays\n-- of different lengths. Thus no full abstraction.\n\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n_\u229d_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4)\n_\u2295_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4) \u2192 Term \u0393 \u03c4\n\ninfixl 6 _\u27e6\u2295\u27e7_\ninfixl 6 _\u27e6\u229d\u27e7_\ninfixl 6 _\u229d_\ninfixl 6 _\u2295_\n\n\u27e6fst\u27e7 \u27e6snd\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e6fst\u27e7 m n = m\n\u27e6snd\u27e7 m n = n\n\nfst snd : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\nfst = abs (abs (var (that this)))\nsnd = abs (abs (var this))\n\n\u27e6derive\u27e7 : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\u27e6derive\u27e7 {nats} n = \u03bb f \u2192 f n n\n\u27e6derive\u27e7 {bags} b = emptyBag\n\u27e6derive\u27e7 {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 f (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f v\n\n_\u27e6\u229d\u27e7_ {nats} m n = \u03bb f \u2192 f n m\n_\u27e6\u229d\u27e7_ {bags} b\u2081 b\u2082 = b\u2081 \\\\ b\u2082\n_\u27e6\u229d\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 f\u2081 (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f\u2082 v\n-- m \u27e6\u229d\u27e7 n ::= replace n by m\n\n-- m \u229d n = \u03bb f. f n m\n_\u229d_ {nats} m n =\n abs (app (app (var this)\n (weaken (drop _ \u2022 \u0393\u227c\u0393) n)) (weaken (drop _ \u2022 \u0393\u227c\u0393) m))\n-- d \u229d b = d \\\\ b\n_\u229d_ {bags} d b = diff d b\n-- g \u229d f = \u03bb x. \u03bb dx. g (x \u2295 dx) \u229d f x -- Incurs recomputation!\n_\u229d_ {\u03c4 \u21d2 \u03c4\u2081} g f =\n abs (abs ((app (weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) g)\n (var (that this) \u2295 var this))\n \u229d app (weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) f) (var (that this))))\n\n_\u2295_ {nats} n dn = app dn snd\n_\u2295_ {bags} b db = union b db\n-- f \u2295 df = \u03bb x. f x \u2295 df x (x \u229d x) -- Incurs recomputation!\n_\u2295_ {\u03c4 \u21d2 \u03c4\u2081} f df =\n abs (app (weaken (drop _ \u2022 \u0393\u227c\u0393) f) (var this)\n \u2295 app (app (weaken (drop _ \u2022 \u0393\u227c\u0393) df)\n (var this)) ((var this) \u229d (var this)))\n\n\n_\u27e6\u2295\u27e7_ {nats} n dn = dn \u27e6snd\u27e7\n_\u27e6\u2295\u27e7_ {bags} b db = b ++ db\n_\u27e6\u2295\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n\n-- Cool lemmas\n\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u229d\u27e7 f \u2261 \u27e6derive\u27e7 f\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {nats} f = refl\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {bags} b = b\\\\b=\u2205\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = refl\n\nf\u2295[g\u229df]=g : \u2200 {\u03c4 : Type} (f g : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) \u2261 g\n\nf\u2295\u0394f=f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (\u27e6derive\u27e7 f) \u2261 f\n\nf\u2295[g\u229df]=g {nats} m n = refl\nf\u2295[g\u229df]=g {bags} b\u2081 b\u2082 = b++[d\\\\b]=d {b\u2081} {b\u2082}\nf\u2295[g\u229df]=g {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = f x} {y = f x} refl\n (cong\u2082 _\u27e6\u229d\u27e7_ (cong g (f\u2295\u0394f=f x)) refl) \u27e9\n f x \u27e6\u2295\u27e7 (g x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (g x) \u27e9\n g x\n \u220e\n ) where open \u2261-Reasoning\n\nf\u2295\u0394f=f {nats} n = refl\nf\u2295\u0394f=f {bags} b = b++\u2205=b\nf\u2295\u0394f=f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 \u27e6derive\u27e7 f) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (f (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong (\u03bb hole \u2192 f x \u27e6\u2295\u27e7 (f hole \u27e6\u229d\u27e7 f x)) (f\u2295\u0394f=f x) \u27e9\n f x \u27e6\u2295\u27e7 (f x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (f x) \u27e9\n f x\n \u220e\n ) where open \u2261-Reasoning\n\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {nats} n dn = n \u2261 dn \u27e6fst\u27e7\nvalid-\u0394 {bags} b db = \u22a4 -- all bags are vald for all other bags\nvalid-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (s : \u27e6 \u03c4\u2081 \u27e7) (ds : \u27e6 \u0394-Type \u03c4\u2081 \u27e7) (R[s,ds] : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7 -- (valid-\u0394:1)\n (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 f s \u27e6\u2295\u27e7 df s ds -- (valid-\u0394:2)\n\nR[f,g\u229df] : \u2200 {\u03c4} (f g : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (g \u27e6\u229d\u27e7 f)\nR[f,g\u229df] {nats} m n = refl\nR[f,g\u229df] {bags} b\u2081 b\u2082 = tt\nR[f,g\u229df] {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb x dx R[x,dx] \u2192\n R[f,g\u229df] {\u03c4\u2082} (f x) (g (x \u27e6\u2295\u27e7 dx)) -- (valid-\u0394:1)\n , -- tuple constructor\n (begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 refl \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7\n (g ((x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 \u27e6derive\u27e7 (x \u27e6\u2295\u27e7 dx)) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 cong (\u03bb hole \u2192 f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g hole \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx)))\n (f\u2295\u0394f=f (x \u27e6\u2295\u27e7 dx)) \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 f\u2295[g\u229df]=g (f (x \u27e6\u2295\u27e7 dx)) (g (x \u27e6\u2295\u27e7 dx)) \u27e9\n g (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (f x) (g (x \u27e6\u2295\u27e7 dx))) \u27e9\n f x \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) x dx\n \u220e)\n where open \u2261-Reasoning\n\nR[f,\u0394f] : \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (\u27e6derive\u27e7 f)\nR[f,\u0394f] f rewrite sym (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f f) = R[f,g\u229df] f f\n\n-- Data type to hold proofs that a \u0394-env is consistent with self\ndata Consistent-\u0394env : {\u0393 : Context} (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is consistent with itself\n d\u03c1=\u2205 : Consistent-\u0394env {\u2205} \u2205\n -- Induction case: the change introduced at top level\n -- should be valid.\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Consistent-\u0394env d\u03c1\u2080 \u2192\n Consistent-\u0394env {\u03c4 \u2022 \u0393\u2080} (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- finally, update and ignore\n\nupdate : \u2200 {\u0393} \u2192 (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env d\u03c1} \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 {d\u03c1=\u2205} = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 d\u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] consistent[d\u03c1\u2080]} =\n (v \u27e6\u2295\u27e7 dv) \u2022 update d\u03c1 {consistent[d\u03c1\u2080]}\n\n-- Ignorance is bliss (don't have to pass consistency proofs around :D)\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 -- Using a proof to describe computation\n\n-- Naming scheme follows weakenVar\/weaken\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- derive(n) = \u03bbf. f n n\nderive (nat n) = abs (app (app (var this) (nat n)) (nat n))\n\n-- derive(x) = dx\nderive (var x) = var (deriveVar x)\n\n-- derive(\u03bbx. t) = \u03bbx. \u03bbdx. derive(t)\nderive (abs t) = abs (abs (derive t))\n\n-- derive(f s) = derive(f) s derive(s)\nderive (app f s) = app (app (derive f) (weaken \u0393\u227c\u0394\u0393 s)) (derive s)\n\n-- derive(x + y) = replace (x + y) by (dx snd + dy snd)\n-- = \u03bb f. f (x + y) (dx snd + dy snd)\nderive (add x y) =\n abs (app (app (var this)\n (add (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) x) (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) y)))\n (add (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive x)) snd)\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive y)) snd)))\n\n-- derive(bag) = \u2205\nderive (bag b) = bag emptyBag\n\n-- derive(map f b) = map (f \u2295 df) (b \u2295 db) \u229d map f b\nderive (map f b) = map ((weaken \u0393\u227c\u0394\u0393 f) \u2295 derive f)\n ((weaken \u0393\u227c\u0394\u0393 b) \u2295 derive b)\n \u229d weaken \u0393\u227c\u0394\u0393 (map f b)\n\n-- derive(b ++ d) = derive(b) ++ derive(d)\nderive (union b d) = union (derive b) (derive d)\n\n-- derive(b \\\\ d) = derive(b) \\\\ derive(d)\nderive (diff b d) = diff (derive b) (derive d)\n\n\n-- Extensional equivalence for changes\ndata Extensionally-equivalent-as-changes :\n \u2200 (\u03c4 : Type) \u2192 (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 (dg : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n ext-\u0394 : \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n (\u2200 (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192\n (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)) \u2192\n Extensionally-equivalent-as-changes \u03c4 df dg\n\nsyntax Extensionally-equivalent-as-changes \u03c4 df dg = df \u2248 dg :\u0394 \u03c4\n\n-- Question: How to declare fixity for infix syntax?\n-- infix 4 _\u2248_:\u0394_ -- same as \u2261\n\n-- Extractor for extensional-equivalence-as-changes:\n-- Given a value of the data type holding the proof,\n-- returns the proof in applicable form.\n--\n-- It would not be necessary if such\n-- proof-holding types were defined as a function in\n-- the first place, say in the manner of `valid-\u0394`.\n--\nextract-\u0394equiv :\n \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n df \u2248 dg :\u0394 \u03c4 \u2192\n (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192 (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)\n\nextract-\u0394equiv (ext-\u0394 proof-method) = proof-method\n\n-- Distribution lemmas of validity over \u27e6\u229d\u27e7 and \u27e6\u2295\u27e7\n\napplication-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v) \u2261 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\napplication-over-\u2295-and-\u229d f g df v =\n begin\n f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (cong\u2082 _\u27e6\u2295\u27e7_\n (cong f ((sym (f\u2295\u0394f=f v))))\n (cong\u2082 df\n (sym (f\u2295\u0394f=f v))\n (cong \u27e6derive\u27e7 (sym (f\u2295\u0394f=f v)))))\n refl \u27e9\n f (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v)\n \u27e6\u2295\u27e7 df (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v) (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v))\n \u27e6\u229d\u27e7 f v\n \u2261\u27e8 refl \u27e9\n (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e where open \u2261-Reasoning\n\nvalidity-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n valid-\u0394 g (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) \u2192\n valid-\u0394 (g v) (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\nvalidity-over-\u2295-and-\u229d f g df v R[g,f\u2295df]\n rewrite application-over-\u2295-and-\u229d f g df v =\n proj\u2081 (R[g,f\u2295df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))\n\n-- diff-apply\ndf=f\u2295df\u229df :\n \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n valid-\u0394 f df \u2192 df \u2248 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) :\u0394 \u03c4\n\n-- Case nat: this REFL is more obvious to Agda than to a human.\ndf=f\u2295df\u229df {nats} n dn valid-n-dn =\n ext-\u0394 (\u03bb m valid-m-dn valid-rhs \u2192 refl)\n\n-- Case bags: rely on set-theoretic interpretation of bags.\ndf=f\u2295df\u229df {bags} b db valid-b-db = ext-\u0394 (\u03bb d _ _ \u2192\n begin -- Reasoning done in 1 step. Here for clarity only.\n d \u27e6\u2295\u27e7 db\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_ {x = d} refl (sym ([b++d]\\\\b=d {b} {db})) \u27e9\n d \u27e6\u2295\u27e7 (b \u27e6\u2295\u27e7 db \u27e6\u229d\u27e7 b)\n \u220e) where open \u2261-Reasoning\n\ndf=f\u2295df\u229df {\u03c4\u2081 \u21d2 \u03c4\u2082} f df R[f,df] = ext-\u0394 (\n \u03bb g R[g,df] R[g,f\u2295df\u229df] \u2192\n extensionality (\u03bb v \u2192\n begin\n g v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n \u2261\u27e8 extract-\u0394equiv\n (df=f\u2295df\u229df\n (f v) (df v (\u27e6derive\u27e7 v))\n (proj\u2081 (R[f,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))))\n (g v)\n (proj\u2081 (R[g,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v)))\n (validity-over-\u2295-and-\u229d f g df v R[g,f\u2295df\u229df]) \u27e9\n g v \u27e6\u2295\u27e7 (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = g v} {y = g v} refl\n (application-over-\u2295-and-\u229d f g df v) \u27e9\n g v \u27e6\u2295\u27e7 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e\n )\n )\n where open \u2261-Reasoning\n\ncorrectness-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n (\u27e6 deriveVar x \u27e7 \u03c1)\n \u2248\n (\u27e6 x \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 x \u27e7 (ignore \u03c1)) :\u0394 \u03c4\n\ncorrectness-of-deriveVar {\u03c4 \u2022 \u0393\u2080} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n this = df=f\u2295df\u229df {\u03c4} v dv R[v,dv]\n\ncorrectness-of-deriveVar {\u03c4\u2080 \u2022 \u0393\u2080} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4\u2080} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n (that x) = correctness-of-deriveVar \u03c1 x\n\nmodule Foo where\n\ncorrectness-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n\n (\u27e6 derive t \u27e7 \u03c1)\n \u2248 (\u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 t \u27e7 (ignore \u03c1)) :\u0394 \u03c4\n\ncorrectness-of-derive\u2032 : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n\n (\u27e6 derive t \u27e7 \u03c1)\n \u2248 (\u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 \u03c1) :\u0394 \u03c4\n\ncorrectness-of-derive\u2032 \u03c1 {consistency} t\n rewrite weaken-sound {subctx = \u0393\u227c\u0394\u0393} t \u03c1\n = correctness-of-derive \u03c1 {consistency} t\n\n-- Mutually recursive lemmas\n\n-- diff-apply holds with propositional equality on bags\n\ndb=b\u2295db\u229db : \u2200 {\u0393 : Context} \u2192\n (b : Term \u0393 bags) \u2192\n {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} {consistency : Consistent-\u0394env \u03c1} \u2192\n \u27e6 derive b \u27e7 \u03c1 \u2261 \u27e6 b \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 b \u27e7 (ignore \u03c1)\ndb=b\u2295db\u229db b {\u03c1 = \u03c1} {consistency = consistency} =\n begin\n \u27e6 derive b \u27e7 \u03c1\n \u2261\u27e8 sym (\u2205++b=b {{\u27e6 derive b \u27e7 \u03c1}}) \u27e9\n emptyBag ++ \u27e6 derive b \u27e7 \u03c1\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} b)\n emptyBag tt tt \u27e9\n emptyBag ++ (\u27e6 b \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 b \u27e7 (ignore \u03c1))\n \u2261\u27e8 \u2205++b=b {{\u27e6 b \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 b \u27e7 (ignore \u03c1)}} \u27e9\n \u27e6 b \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 b \u27e7 (ignore \u03c1)\n \u220e where open \u2261-Reasoning\n\n-- Semantic brackets preserve \u2295 and \u229d\n\n\u2295-preservation : \u2200 {\u03c4 \u0393} \u2192\n {t : Term \u0393 \u03c4} {dt : Term \u0393 (\u0394-Type \u03c4)} \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 t \u2295 dt \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 \u03c1 \u27e6\u2295\u27e7 \u27e6 dt \u27e7 \u03c1\n\n\u229d-preservation : \u2200 {\u03c4 \u0393} \u2192\n {s : Term \u0393 \u03c4} {t : Term \u0393 \u03c4} \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 s \u229d t \u27e7 \u03c1 \u2261 \u27e6 s \u27e7 \u03c1 \u27e6\u229d\u27e7 \u27e6 t \u27e7 \u03c1\n\n\u2295-preservation {nats} {\u0393} {t} {dt} {\u03c1} = refl\n\u2295-preservation {bags} {\u0393} {t} {dt} {\u03c1} = refl\n\u2295-preservation {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u0393} {t} {dt} {\u03c1} = extensionality (\u03bb x \u2192\n begin\n \u27e6 app (weaken (drop \u03c4\u2081 \u2022 \u0393\u227c\u0393) t) (var this) \u2295\n app (app (weaken (drop \u03c4\u2081 \u2022 \u0393\u227c\u0393) dt) (var this))\n (var this \u229d var this) \u27e7 (x \u2022 \u03c1)\n \u2261\u27e8 \u2295-preservation \u27e9\n \u27e6 app (weaken (drop \u03c4\u2081 \u2022 \u0393\u227c\u0393) t) (var this) \u27e7 (x \u2022 \u03c1)\n \u27e6\u2295\u27e7\n \u27e6 app (app (weaken (drop \u03c4\u2081 \u2022 \u0393\u227c\u0393) dt) (var this))\n (var this \u229d var this) \u27e7 (x \u2022 \u03c1)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n (trans (weaken-sound {subctx = drop _ \u2022 \u0393\u227c\u0393} t (x \u2022 \u03c1))\n (cong \u27e6 t \u27e7 identity-weakening)\n \u27e8$\u27e9 refl)\n (trans (weaken-sound {subctx = drop _ \u2022 \u0393\u227c\u0393} dt (x \u2022 \u03c1))\n (cong \u27e6 dt \u27e7 identity-weakening)\n \u27e8$\u27e9 refl \u27e8$\u27e9 refl) \u27e9\n \u27e6 t \u27e7 \u03c1 x \u27e6\u2295\u27e7 \u27e6 dt \u27e7 \u03c1 x (\u27e6 (var this \u229d var this) \u27e7 (x \u2022 \u03c1))\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 \u03c1 x \u27e6\u2295\u27e7 \u27e6 dt \u27e7 \u03c1 x hole)\n (\u229d-preservation {\u03c4\u2081} {\u03c4\u2081 \u2022 \u0393} {var this} {var this}) \u27e9\n \u27e6 t \u27e7 \u03c1 x \u27e6\u2295\u27e7 \u27e6 dt \u27e7 \u03c1 x (x \u27e6\u229d\u27e7 x)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 \u03c1 x \u27e6\u2295\u27e7 \u27e6 dt \u27e7 \u03c1 x hole) (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f x) \u27e9\n \u27e6 t \u27e7 \u03c1 x \u27e6\u2295\u27e7 \u27e6 dt \u27e7 \u03c1 x (\u27e6derive\u27e7 x)\n \u220e) where open \u2261-Reasoning\n\n\u229d-preservation {nats} {\u0393} {s} {t} {\u03c1} = extensionality (\u03bb f \u2192\n cong\u2082 f\n (trans (weaken-sound t (f \u2022 \u03c1)) (cong \u27e6 t \u27e7 identity-weakening))\n (trans (weaken-sound s (f \u2022 \u03c1)) (cong \u27e6 s \u27e7 identity-weakening)))\n\u229d-preservation {bags} {\u0393} {s} {t} {\u03c1} = refl\n\u229d-preservation {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u0393} {s} {t} {\u03c1} =\n extensionality (\u03bb x \u2192\n extensionality (\u03bb dx \u2192\n begin\n \u27e6 app (weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) s)\n (var (that this) \u2295 var this)\n \u229d\n app (weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) t) (var (that this)) \u27e7\n (dx \u2022 x \u2022 \u03c1)\n \u2261\u27e8 \u229d-preservation\n {s = app (weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) s)\n (var (that this) \u2295 var this)}\n {t = app (weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) t)\n (var (that this))} \u27e9\n \u27e6 weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) s \u27e7 (dx \u2022 x \u2022 \u03c1)\n (\u27e6 var (that this) \u2295 var this \u27e7 (dx \u2022 x \u2022 \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) t \u27e7 (dx \u2022 x \u2022 \u03c1)\n (\u27e6 (var (that this)) \u27e7 (dx \u2022 x \u2022 \u03c1))\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (trans (weaken-sound s (dx \u2022 x \u2022 \u03c1))\n (cong \u27e6 s \u27e7 identity-weakening)\n \u27e8$\u27e9 \u2295-preservation {t = var (that this)} {dt = var this})\n (trans (weaken-sound t (dx \u2022 x \u2022 \u03c1))\n (cong \u27e6 t \u27e7 identity-weakening)\n \u27e8$\u27e9 refl) \u27e9\n \u27e6 s \u27e7 \u03c1 (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 \u27e6 t \u27e7 \u03c1 x\n \u220e)) where open \u2261-Reasoning\n\n-- Derivatives are valid\n\nvalidity-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-derive\u2032 : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 \u03c1) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-derive\u2032 \u03c1 {consistency} t\n rewrite weaken-sound {subctx = \u0393\u227c\u0394\u0393} t \u03c1\n = validity-of-derive \u03c1 {consistency} t\n\nvalidity-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 deriveVar x \u27e7 \u03c1)\n\nvalidity-of-deriveVar {\u03c4 \u2022 \u0393} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n this = R[v,dv]\n\nvalidity-of-deriveVar {\u03c4\u2080 \u2022 \u0393} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n (that x) = validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 {consistency} (var x) =\n validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs t) = \u03bb v dv R[v,dv] \u2192\n validity-of-derive (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency} t\n , -- tuple constructor\n (begin\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n ((R[f,g\u229df] (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 f\u2295[g\u229df]=g (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u2261\u27e8 cong \u27e6 t \u27e7 (cong\u2082 _\u2022_ (f\u2295\u0394f=f (v \u27e6\u2295\u27e7 dv)) refl) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency}))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 sym (extract-\u0394equiv\n (correctness-of-derive\n ((dv \u2022 v \u2022 \u03c1))\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (R[f,g\u229df] (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u220e)\n where open \u2261-Reasoning\n\nvalidity-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} t\u2081 t\u2082)\n = R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7]\n where\n open \u2261-Reasoning\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 : \u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1) \u2261 v\u2081 v\u2082\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 rewrite (sym v\u2082=old-v\u2082) = refl\n\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 : \u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1 \u2261 dv\u2081 v\u2082 dv\u2082\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = refl\n\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] : valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (dv\u2081 v\u2082 dv\u2082)\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] rewrite \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 = R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n -- What I want to write:\n {-\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] rewrite \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n -}\n\n -- What I have to write:\n\n R : {\u03c4 : Type} \u2192 {v : \u27e6 \u03c4 \u27e7} \u2192 {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 valid-\u0394 v dv\u2081 \u2192 valid-\u0394 v dv\u2082\n\n R {nats} dv\u2081=dv\u2082 refl = cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl\n\n R {bags} dv\u2081=dv\u2082 _ = tt\n\n --R {\u03c4\u2081 \u21d2 \u03c4\u2082} dv\u2081=dv\u2082 valid-dv\u2081 rewrite dv\u2081=dv\u2082 = {!valid-dv\u2081!}\n R {\u03c4\u2081 \u21d2 \u03c4\u2082} {v} {dv\u2081} {dv\u2082} dv\u2081=dv\u2082 valid-dv\u2081 =\n \u03bb s ds R[s,ds] \u2192\n R {\u03c4\u2082} {v s} {dv\u2081 s ds} {dv\u2082 s ds}\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl)\n (proj\u2081 (valid-dv\u2081 s ds R[s,ds]))\n ,\n (begin\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2082 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v (s \u27e6\u2295\u27e7 ds)} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) (sym dv\u2081=dv\u2082) refl) refl) \u27e9\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2081 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 sym (proj\u2082 (valid-dv\u2081\n (s \u27e6\u2295\u27e7 ds)\n (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n (R[f,\u0394f] (s \u27e6\u2295\u27e7 ds)))) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds \u27e6\u2295\u27e7 \u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong (v \u27e6\u2295\u27e7 dv\u2081) (f\u2295\u0394f=f (s \u27e6\u2295\u27e7 ds)) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds)\n \u2261\u27e8 proj\u2082 (valid-dv\u2081 s ds R[s,ds]) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2081 s ds\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v s} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2082 s ds\n \u220e) where open \u2261-Reasoning\n\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] = R \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n\n-- Validity of deriving bag-typed terms is trivial.\n\nvalidity-of-derive \u03c1 (bag b) = tt\n\nvalidity-of-derive \u03c1 (map f b) = tt\n\nvalidity-of-derive \u03c1 (union b d) = tt\n\nvalidity-of-derive \u03c1 (diff d b) = tt\n\nvalidity-of-derive \u03c1 (nat n) = refl\n\nvalidity-of-derive \u03c1 {consistency} (add m n) =\n begin\n \u27e6 m \u27e7 (ignore \u03c1) + \u27e6 n \u27e7 (ignore \u03c1)\n \u2261\u27e8 cong\u2082 _+_\n (sym (weaken-sound m (\u27e6fst\u27e7 \u2022 \u03c1)))\n (sym (weaken-sound n (\u27e6fst\u27e7 \u2022 \u03c1))) \u27e9\n (\u27e6 weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) m \u27e7 (\u27e6fst\u27e7 \u2022 \u03c1) +\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) n \u27e7 (\u27e6fst\u27e7 \u2022 \u03c1))\n \u2261\u27e8 refl \u27e9\n \u27e6 abs (app (app (var this)\n (add (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) m) (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) n)))\n (add (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive m)) snd)\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive n)) snd)))\n \u27e7 \u03c1 \u27e6fst\u27e7\n \u2261\u27e8 refl \u27e9\n \u27e6 derive (add m n) \u27e7 \u03c1 \u27e6fst\u27e7\n \u220e where\n open \u2261-Reasoning\n blah : \u2115\n blah =\n \u27e6 add (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive m)) snd)\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive n)) snd) \u27e7\n (\u27e6fst\u27e7 \u2022 \u03c1)\n\ncorrectness-of-derive \u03c1 (var x) = correctness-of-deriveVar \u03c1 x\n\ncorrectness-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs {.\u03c4\u2081} {.\u03c4\u2082} t) =\n ext-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n extensionality {\u27e6 \u03c4\u2081 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4\u2082 \u27e7} (\u03bb x \u2192\n begin\n f x \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive {\u03c4\u2081 \u2022 \u0393} {\u03c4\u2082}\n (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency}\n t)\n (f x)\n (proj\u2081 (R[f,\u0394t] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x)))\n (proj\u2081 (R[f,t\u2032\u229dt] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x))) \u27e9\n f x\n \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 (update (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency})\n \u27e6\u229d\u27e7 \u27e6 t \u27e7 (x \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u220e\n )) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} {\u0393} t\u2081 t\u2082) =\n ext-\u0394 {\u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n begin\n f \u27e6\u2295\u27e7 \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n \u2261\u27e8 extract-\u0394equiv ext-\u0394[t\u2081t\u2082] f R[f,\u0394t] R[f,t\u2032\u229dt] \u27e9\n f \u27e6\u2295\u27e7\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n \u220e)\n where\n open \u2261-Reasoning\n\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n v\u2081\u2295dv\u2081=v\u2081\u2032 : v\u2081 \u27e6\u2295\u27e7 dv\u2081 \u2261 v\u2081\u2032\n v\u2081\u2295dv\u2081=v\u2081\u2032 =\n begin\n v\u2081 \u27e6\u2295\u27e7 dv\u2081\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2081)\n v\u2081 valid-dv\u2081 (R[f,g\u229df] v\u2081 v\u2081\u2032) \u27e9\n v\u2081 \u27e6\u2295\u27e7 (v\u2081\u2032 \u27e6\u229d\u27e7 v\u2081)\n \u2261\u27e8 f\u2295[g\u229df]=g v\u2081 v\u2081\u2032 \u27e9\n v\u2081\u2032\n \u220e\n\n -- TODO: remove code duplication.\n v\u2082\u2295dv\u2082=v\u2082\u2032 : v\u2082 \u27e6\u2295\u27e7 dv\u2082 \u2261 v\u2082\u2032\n v\u2082\u2295dv\u2082=v\u2082\u2032 rewrite v\u2082=old-v\u2082 =\n begin\n old-v\u2082 \u27e6\u2295\u27e7 dv\u2082\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2082)\n old-v\u2082\n (validity-of-derive \u03c1 {consistency} t\u2082)\n (R[f,g\u229df] old-v\u2082 v\u2082\u2032) \u27e9\n old-v\u2082 \u27e6\u2295\u27e7 (v\u2082\u2032 \u27e6\u229d\u27e7 old-v\u2082)\n \u2261\u27e8 f\u2295[g\u229df]=g old-v\u2082 v\u2082\u2032 \u27e9\n v\u2082\u2032\n \u220e\n where old-v\u2082 = \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] : v\u2081\u2032 v\u2082\u2032 \u2261 (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082)\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] = sym (cong\u2082 (\u03bb f x \u2192 f x) v\u2081\u2295dv\u2081=v\u2081\u2032 v\u2082\u2295dv\u2082=v\u2082\u2032)\n\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 :\n (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082) \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 = proj\u2082 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082 \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n cong\u2082 _\u27e6\u229d\u27e7_\n (trans v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082)\n refl\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n (dv\u2081 v\u2082 dv\u2082) \u2248 (v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082) :\u0394 \u03c4\u2082\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n df=f\u2295df\u229df (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082) R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 : (dv\u2081 v\u2082 dv\u2082) \u2248 (v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082) :\u0394 \u03c4\u2082\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 rewrite v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082\n\n ext-\u0394[t\u2081t\u2082] :\n (\u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1))\n \u2248\n (\u27e6 t\u2081 \u27e7 (update \u03c1 {consistency}) (\u27e6 t\u2082 \u27e7 (update \u03c1 {consistency}))\n \u27e6\u229d\u27e7 \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n :\u0394 \u03c4\u2082\n ext-\u0394[t\u2081t\u2082] rewrite sym v\u2082=old-v\u2082 = dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082\n\ncorrectness-of-derive \u03c1 (nat n) = ext-\u0394 (\u03bb _ _ _ \u2192 refl)\n\ncorrectness-of-derive \u03c1 (bag b) = ext-\u0394 (\u03bb d _ _ \u2192\n begin\n d ++ emptyBag\n \u2261\u27e8 cong\u2082 _++_ {x = d} refl (sym (b\\\\b=\u2205)) \u27e9\n d ++ (b \\\\ b)\n \u220e) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (add m n) = ext-\u0394 (\u03bb _ _ _ \u2192\n begin\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (derive m) \u27e7 (\u27e6snd\u27e7 \u2022 \u03c1) \u27e6snd\u27e7\n +\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (derive n) \u27e7 (\u27e6snd\u27e7 \u2022 \u03c1) \u27e6snd\u27e7\n \u2261\u27e8 refl \u27e9\n (\u27e6 m \u27e7 (ignore \u03c1) \u27e6\u2295\u27e7 \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (derive m) \u27e7 (\u27e6snd\u27e7 \u2022 \u03c1))\n +\n (\u27e6 n \u27e7 (ignore \u03c1) \u27e6\u2295\u27e7 \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (derive n) \u27e7 (\u27e6snd\u27e7 \u2022 \u03c1))\n \u2261\u27e8 cong\u2082 _+_\n (cong\u2082 _\u27e6\u2295\u27e7_ {x = \u27e6 m \u27e7 (ignore \u03c1)} refl\n (weaken-derivative {t = m} {\u03c1 = \u03c1}))\n ((cong\u2082 _\u27e6\u2295\u27e7_ {x = \u27e6 n \u27e7 (ignore \u03c1)} refl\n (weaken-derivative {t = n} {\u03c1 = \u03c1}))) \u27e9\n (\u27e6 m \u27e7 (ignore \u03c1) \u27e6\u2295\u27e7 \u27e6 derive m \u27e7 \u03c1)\n +\n (\u27e6 n \u27e7 (ignore \u03c1) \u27e6\u2295\u27e7 \u27e6 derive n \u27e7 \u03c1)\n \u2261\u27e8 cong\u2082 _+_\n (extract-\u0394equiv (correctness-of-derive \u03c1 {consistency} m)\n (\u27e6 m \u27e7 (ignore \u03c1))\n (validity-of-derive \u03c1 {consistency} m)\n (R[f,g\u229df] (\u27e6 m \u27e7 (ignore \u03c1)) (\u27e6 m \u27e7 (update \u03c1 {consistency}))))\n ((extract-\u0394equiv (correctness-of-derive \u03c1 {consistency} n)\n (\u27e6 n \u27e7 (ignore \u03c1))\n (validity-of-derive \u03c1 {consistency} n)\n (R[f,g\u229df] (\u27e6 n \u27e7 (ignore \u03c1)) (\u27e6 n \u27e7 (update \u03c1 {consistency}))))) \u27e9\n \u27e6 m \u27e7 (update \u03c1) + \u27e6 n \u27e7 (update \u03c1)\n \u220e\n ) where\n open \u2261-Reasoning\n weaken-derivative :\n \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7}\n \u2192 \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (derive t) \u27e7 (\u27e6snd\u27e7 \u2022 \u03c1)\n \u2261 \u27e6 derive t \u27e7 \u03c1\n weaken-derivative {t = t} {\u03c1 = \u03c1}\n rewrite weaken-sound {subctx = drop _ \u2022 \u0393\u227c\u0393} (derive t) (\u27e6snd\u27e7 \u2022 \u03c1)\n | identity-weakening {\u03c1 = \u03c1}\n = refl\n\ncorrectness-of-derive \u03c1 {consistency} (union b d) = ext-\u0394 (\u03bb c _ _ \u2192\n begin\n c ++ (\u27e6 derive b \u27e7 \u03c1 ++ \u27e6 derive d \u27e7 \u03c1)\n \u2261\u27e8 cong\u2082 _++_\n {x = c} refl\n (cong\u2082 _++_\n (db=b\u2295db\u229db b)\n (db=b\u2295db\u229db d)) \u27e9\n c ++ ((\u27e6 b \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 b \u27e7 (ignore \u03c1))\n ++ (\u27e6 d \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 d \u27e7 (ignore \u03c1)))\n \u2261\u27e8 cong\u2082 _++_\n {x = c} refl\n (sym ([a++b]\\\\[c++d]=[a\\\\c]++[b\\\\d]\n {\u27e6 b \u27e7 (update \u03c1 {consistency})} \n {\u27e6 d \u27e7 (update \u03c1 {consistency})}\n {\u27e6 b \u27e7 (ignore \u03c1)} {\u27e6 d \u27e7 (ignore \u03c1)})) \u27e9\n c ++ ((\u27e6 b \u27e7 (update \u03c1 {consistency})\n ++ \u27e6 d \u27e7 (update \u03c1 {consistency}))\n \\\\(\u27e6 b \u27e7 (ignore \u03c1) ++ \u27e6 d \u27e7 (ignore \u03c1)))\n \u220e) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (diff b d) = ext-\u0394 (\u03bb c _ _ \u2192\n begin\n c ++ \u27e6 derive b \u27e7 \u03c1 \\\\ \u27e6 derive d \u27e7 \u03c1\n \u2261\u27e8 cong\u2082 _++_\n {x = c} refl\n (cong\u2082 _\\\\_\n (db=b\u2295db\u229db b)\n (db=b\u2295db\u229db d)) \u27e9\n c ++ (\u27e6 b \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 b \u27e7 (ignore \u03c1))\n \\\\ (\u27e6 d \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 d \u27e7 (ignore \u03c1))\n \u2261\u27e8 cong\u2082 _++_\n {x = c} refl\n ([a\\\\b]\\\\[c\\\\d]=[a\\\\c]\\\\[b\\\\d]\n {\u27e6 b \u27e7 (update \u03c1 {consistency})} {\u27e6 b \u27e7 (ignore \u03c1)}\n {\u27e6 d \u27e7 (update \u03c1 {consistency})} {\u27e6 d \u27e7 (ignore \u03c1)}) \u27e9\n c ++ (\u27e6 b \u27e7 (update \u03c1) \\\\ \u27e6 d \u27e7 (update \u03c1))\n \\\\ (\u27e6 b \u27e7 (ignore \u03c1) \\\\ \u27e6 d \u27e7 (ignore \u03c1))\n \u220e) where open \u2261-Reasoning\n\n\ncorrectness-of-derive \u03c1 {consistency} (map f b) = ext-\u0394 (\u03bb c _ _ \u2192\n begin\n c ++\n (mapBag (\u27e6 weaken \u0393\u227c\u0394\u0393 f \u2295 derive f \u27e7 \u03c1)\n (\u27e6 weaken \u0393\u227c\u0394\u0393 b \u27e7 \u03c1 ++ \u27e6 derive b \u27e7 \u03c1))\n \\\\\n (mapBag (\u27e6 weaken \u0393\u227c\u0394\u0393 f \u27e7 \u03c1) (\u27e6 weaken \u0393\u227c\u0394\u0393 b \u27e7 \u03c1))\n \u2261\u27e8 cong\u2082 _++_\n {x = c} refl\n (cong\u2082 _\\\\_\n (cong\u2082 mapBag\n (\u2295-preservation { t = weaken \u0393\u227c\u0394\u0393 f}\n {dt = derive f} {\u03c1 = \u03c1})\n (extract-\u0394equiv\n (correctness-of-derive\u2032 \u03c1 {consistency} b)\n (\u27e6 weaken \u0393\u227c\u0394\u0393 b \u27e7 \u03c1) tt tt))\n (cong\u2082 mapBag\n (weaken-sound f \u03c1) (weaken-sound b \u03c1))) \u27e9\n c ++\n (mapBag (\u27e6 weaken \u0393\u227c\u0394\u0393 f \u27e7 \u03c1 \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u03c1) -- \u2295-preservation\n (\u27e6 weaken \u0393\u227c\u0394\u0393 b \u27e7 \u03c1 ++ -- correctness\u2032\n \u27e6 b \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 weaken \u0393\u227c\u0394\u0393 b \u27e7 \u03c1))\n \\\\\n (mapBag (\u27e6 f \u27e7 (ignore \u03c1)) (\u27e6 b \u27e7 (ignore \u03c1))) -- weaken-sound\n \u2261\u27e8 cong\u2082 (\u03bb hole-f hole-b \u2192 c ++ mapBag hole-f hole-b\n \\\\ mapBag (\u27e6 f \u27e7 (ignore \u03c1)) (\u27e6 b \u27e7 (ignore \u03c1)))\n (trans\n (extract-\u0394equiv\n (correctness-of-derive\u2032 \u03c1 {consistency} f)\n (\u27e6 weaken \u0393\u227c\u0394\u0393 f \u27e7 \u03c1)\n (validity-of-derive\u2032 \u03c1 {consistency} f)\n (R[f,g\u229df] (\u27e6 weaken \u0393\u227c\u0394\u0393 f \u27e7 \u03c1) (\u27e6 f \u27e7 (update \u03c1))))\n (f\u2295[g\u229df]=g (\u27e6 weaken \u0393\u227c\u0394\u0393 f \u27e7 \u03c1) (\u27e6 f \u27e7 (update \u03c1))))\n (b++[d\\\\b]=d {\u27e6 weaken \u0393\u227c\u0394\u0393 b \u27e7 \u03c1} {\u27e6 b \u27e7 (update \u03c1)})\n \u27e9\n c ++ mapBag (\u27e6 f \u27e7 (update \u03c1)) (\u27e6 b \u27e7 (update \u03c1))\n \\\\ mapBag (\u27e6 f \u27e7 (ignore \u03c1)) (\u27e6 b \u27e7 (ignore \u03c1))\n \u220e) where open \u2261-Reasoning\n\n\ncorrectness-on-closed-terms : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192\n \u2200 (f : Term \u2205 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192\n \u2200 (s : Term \u2205 \u03c4\u2081) (ds : Term \u2205 (\u0394-Type \u03c4\u2081))\n {R[v,dv] : valid-\u0394 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)} \u2192\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205 \u27e6\u2295\u27e7 \u27e6 ds \u27e7 \u2205)\n \u2261\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)\n\ncorrectness-on-closed-terms {\u03c4\u2081} {\u03c4\u2082} f s ds {R[v,dv]} =\n begin\n h (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (f\u2295[g\u229df]=g h h))\n refl \u27e9\n (h \u27e6\u2295\u27e7 (h \u27e6\u229d\u27e7 h)) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (extract-\u0394equiv\n (correctness-of-derive \u2205 f)\n h\n (validity-of-derive \u2205 {d\u03c1=\u2205} f)\n (R[f,g\u229df] h h)))\n refl \u27e9\n (h \u27e6\u2295\u27e7 \u0394h) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 proj\u2082 (validity-of-derive \u2205 {d\u03c1=\u2205} f v dv R[v,dv]) \u27e9\n h v \u27e6\u2295\u27e7 \u0394h v dv\n \u220e\n where\n open \u2261-Reasoning\n h : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n h = \u27e6 f \u27e7 \u2205\n \u0394h : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n \u0394h = \u27e6 derive f \u27e7 \u2205\n v : \u27e6 \u03c4\u2081 \u27e7\n v = \u27e6 s \u27e7 \u2205\n dv : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv = \u27e6 ds \u27e7 \u2205\n","old_contents":"{-\nThe goal of this file is to make the 3rd example\ndescribed in \/examples.md, \"Map.mapValues\", fast:\n\n inc :: Bag Integer -> Bag Integer\n inc = map (+1)\n\n old = fromList [1, 2 .. n - 1, n]\n res = inc old = [2, 3 .. n, n + 1]\n\nTODO\n1. Make sure this file has no hole\n X. Replace \u2115 by \u2124\n 0. Replace \u2124 by \u2115 -- our bags are bags of nats now.\n 0. Introduce addition\n 0. Add MapBags and map\n 0. Figure out a way to communicate to a derivative that\n certain changes are always nil (in this case, `+1`).\n2. Finish ExplicitNils\n3. Consider appending ExplicitNils\n\n\nChecklist: Adding syntactic constructs\n\n- weaken\n- \u27e6_\u27e7Term\n- weaken-sound\n- derive (symbolic derivation)\n- validity-of-derive\n- correctness-of-derive\n\nChecklist: Adding types\n\n- \u27e6_\u27e7Type\n- \u0394-Type\n- \u27e6derive\u27e7\n- _\u27e6\u229d\u27e7_\n- _\u27e6\u2295\u27e7_\n- f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f\n- f\u2295[g\u229df]=g\n- f\u2295\u0394f=f\n- valid-\u0394\n- R[f,g\u229df]\n- df=f\u2295df\u229df\n- R (inside validity-of-derive)\n\n-}\n\nmodule NatBag where\n\nopen import Data.NatBag renaming\n (map to mapBag ; empty to emptyBag ; update to updateBag)\nopen import Relation.Binary.PropositionalEquality\n\n-- This module has holes and can't be imported.\n-- We postulate necessary properties now to be able\n-- to work on derivation and remove them later.\n--\n-- Perhaps proving that bags form a group with\n-- emptyBag, ++, \\\\ is a necessity?\n--\n-- open import Data.NatBag.Properties\npostulate b\\\\b=\u2205 : \u2200 {{b : Bag}} \u2192 b \\\\ b \u2261 emptyBag\npostulate b++\u2205=b : \u2200 {{b : Bag}} \u2192 b ++ emptyBag \u2261 b\npostulate \u2205++b=b : \u2200 {{b : Bag}} \u2192 emptyBag ++ b \u2261 b\npostulate b++[d\\\\b]=d : \u2200 {b d} \u2192 b ++ (d \\\\ b) \u2261 d\npostulate [b++d]\\\\b=d : \u2200 {b d} \u2192 (b ++ d) \\\\ b \u2261 d\n\n-- postulate a\\\\[b++c]=a\\\\b\\\\c : \u2200 {a b c} \u2192 a \\\\ (b ++ c) \u2261 a \\\\ b \\\\ c\n-- postulate [a++b]\\\\c=a\\\\c++b : \u2200 {a b c} \u2192 (a ++ b) \\\\ c \u2261 a \\\\ c ++ b\n-- postulate [a++b]\\\\c=a++b\\\\c : \u2200 {a b c} \u2192 (a ++ b) \\\\ c \u2261 a ++ b \\\\ c\n-- \n-- and consequently:\n\npostulate\n [a++b]\\\\[c++d]=[a\\\\c]++[b\\\\d] : \u2200 {a b c d} \u2192\n (a ++ b) \\\\ (c ++ d) \u2261 (a \\\\ c) ++ (b \\\\ d)\n\nopen import Data.Nat\n\nopen import Data.Unit using\n (\u22a4 ; tt)\n\nimport Data.Integer as \u2124\n\nopen import Data.Product using\n (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\nimport Level\nimport Data.Product as Product\n\npostulate extensionality : Extensionality Level.zero Level.zero\n\n\ndata Type : Set where\n nats : Type\n bags : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u03c4 \u0393} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u03c4 \u03c4\u2032 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n bag : \u2200 {\u0393} \u2192 (b : Bag) \u2192 Term \u0393 bags\n add : \u2200 {\u0393} \u2192 (t\u2081 : Term \u0393 nats) \u2192 (t\u2082 : Term \u0393 nats) \u2192 Term \u0393 nats\n map : \u2200 {\u0393} \u2192 (f : Term \u0393 (nats \u21d2 nats)) \u2192 (b : Term \u0393 bags) \u2192 Term \u0393 bags\n union : \u2200 {\u0393} \u2192 (b\u2081 : Term \u0393 bags) \u2192 (b\u2082 : Term \u0393 bags) \u2192 Term \u0393 bags\n diff : \u2200 {\u0393} \u2192 (b\u2081 : Term \u0393 bags) \u2192 (b\u2082 : Term \u0393 bags) \u2192 Term \u0393 bags\n\n var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n -- Change to nats = replacement Church pairs\n -- 3 -> 5 ::= \u03bbf. f 3 5\n\n -- Change to bags = a summand\n -- b\u2081 -> b\u2082 ::= b\u2082 \\\\ b\u2081\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u0393\u227c\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0393\n\u0393\u227c\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0393 {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u0393\u227c\u0393 {\u0393}\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\nweaken subctx (bag b) = bag b\nweaken subctx (add t\u2081 t\u2082) = add (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (map f b) = map (weaken subctx f) (weaken subctx b)\nweaken subctx (union b\u2081 b\u2082) = union (weaken subctx b\u2081) (weaken subctx b\u2082)\nweaken subctx (diff b\u2081 b\u2082) = diff (weaken subctx b\u2081) (weaken subctx b\u2082)\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 nats \u27e7Type = \u2115\n\u27e6 bags \u27e7Type = Bag\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\u27e6 bag b \u27e7Term \u03c1 = b\n\u27e6 add m n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 map f b \u27e7Term \u03c1 = mapBag (\u27e6 f \u27e7Term \u03c1) (\u27e6 b \u27e7Term \u03c1)\n\u27e6 union b d \u27e7Term \u03c1 = \u27e6 b \u27e7Term \u03c1 ++ \u27e6 d \u27e7Term \u03c1\n\u27e6 diff b d \u27e7Term \u03c1 = \u27e6 b \u27e7Term \u03c1 \\\\ \u27e6 d \u27e7Term \u03c1\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n_\u27e8$\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\ninfix 0 _\u27e8$\u27e9_ -- infix 0 $ in Haskell\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = weaken-sound t\u2081 \u03c1 \u27e8$\u27e9 weaken-sound t\u2082 \u03c1\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (bag b) \u03c1 = refl\nweaken-sound (add m n) \u03c1 = cong\u2082 _+_ (weaken-sound m \u03c1) (weaken-sound n \u03c1)\nweaken-sound (map f b) \u03c1 =\n cong\u2082 mapBag (weaken-sound f \u03c1) (weaken-sound b \u03c1)\nweaken-sound (union b d) \u03c1 =\n cong\u2082 _++_ (weaken-sound b \u03c1) (weaken-sound d \u03c1)\nweaken-sound (diff b d) \u03c1 =\n cong\u2082 _\\\\_ (weaken-sound b \u03c1) (weaken-sound d \u03c1)\n\n-- Changes to \u2115 are replacement Church pairs. The only arguments\n-- of conern are `fst` and `snd`, so the Church pairs don't have\n-- to be polymorphic.\n--\n-- Changes on bags are bags. They allow negative multiplicities\n-- to begin with.\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type nats = (nats \u21d2 nats \u21d2 nats) \u21d2 nats\n\u0394-Type bags = bags\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393 -- push \u03c4, then push \u0394\u03c4.\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 (keep \u03c4 \u2022 \u0393\u227c\u0394\u0393)\n\n-- It is clear that \u27e6\u229d\u27e7 exists on the semantic level:\n-- there exists an Agda value to describe the change between any\n-- two Agda values denoted by terms. If we have (not dependently-\n-- typed) arrays, no term denotes the change between two arrays\n-- of different lengths. Thus no full abstraction.\n\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n_\u229d_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4)\n_\u2295_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4) \u2192 Term \u0393 \u03c4\n\ninfixl 6 _\u27e6\u2295\u27e7_\ninfixl 6 _\u27e6\u229d\u27e7_\ninfixl 6 _\u229d_\ninfixl 6 _\u2295_\n\n\u27e6fst\u27e7 \u27e6snd\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e6fst\u27e7 m n = m\n\u27e6snd\u27e7 m n = n\n\nfst snd : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\nfst = abs (abs (var (that this)))\nsnd = abs (abs (var this))\n\n\u27e6derive\u27e7 : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\u27e6derive\u27e7 {nats} n = \u03bb f \u2192 f n n\n\u27e6derive\u27e7 {bags} b = emptyBag\n\u27e6derive\u27e7 {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 f (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f v\n\n_\u27e6\u229d\u27e7_ {nats} m n = \u03bb f \u2192 f n m\n_\u27e6\u229d\u27e7_ {bags} b\u2081 b\u2082 = b\u2081 \\\\ b\u2082\n_\u27e6\u229d\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 f\u2081 (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f\u2082 v\n-- m \u27e6\u229d\u27e7 n ::= replace n by m\n\n-- m \u229d n = \u03bb f. f n m\n_\u229d_ {nats} m n =\n abs (app (app (var this)\n (weaken (drop _ \u2022 \u0393\u227c\u0393) n)) (weaken (drop _ \u2022 \u0393\u227c\u0393) m))\n-- d \u229d b = d \\\\ b\n_\u229d_ {bags} d b = weaken \u0393\u227c\u0393 (diff d b)\n-- g \u229d f = \u03bb x. \u03bb dx. g (x \u2295 dx) \u229d f x -- Incurs recomputation!\n_\u229d_ {\u03c4 \u21d2 \u03c4\u2081} g f =\n abs (abs ((app (weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) g)\n (var (that this) \u2295 var this))\n \u229d app (weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) f) (var (that this))))\n\n_\u2295_ {nats} n dn = app dn snd\n_\u2295_ {bags} b db = union b db\n-- f \u2295 df = \u03bb x. f x \u2295 df x (x \u229d x) -- Incurs recomputation!\n_\u2295_ {\u03c4 \u21d2 \u03c4\u2081} f df =\n abs (app (weaken (drop _ \u2022 \u0393\u227c\u0393) f) (var this)\n \u2295 app (app (weaken (drop _ \u2022 \u0393\u227c\u0393) df)\n (var this)) ((var this) \u229d (var this)))\n\n\n_\u27e6\u2295\u27e7_ {nats} n dn = dn \u27e6snd\u27e7\n_\u27e6\u2295\u27e7_ {bags} b db = b ++ db\n_\u27e6\u2295\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n\n-- Cool lemmas\n\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u229d\u27e7 f \u2261 \u27e6derive\u27e7 f\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {nats} f = refl\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {bags} b = b\\\\b=\u2205\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = refl\n\nf\u2295[g\u229df]=g : \u2200 {\u03c4 : Type} (f g : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) \u2261 g\n\nf\u2295\u0394f=f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (\u27e6derive\u27e7 f) \u2261 f\n\nf\u2295[g\u229df]=g {nats} m n = refl\nf\u2295[g\u229df]=g {bags} b\u2081 b\u2082 = b++[d\\\\b]=d {b\u2081} {b\u2082}\nf\u2295[g\u229df]=g {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = f x} {y = f x} refl\n (cong\u2082 _\u27e6\u229d\u27e7_ (cong g (f\u2295\u0394f=f x)) refl) \u27e9\n f x \u27e6\u2295\u27e7 (g x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (g x) \u27e9\n g x\n \u220e\n ) where open \u2261-Reasoning\n\nf\u2295\u0394f=f {nats} n = refl\nf\u2295\u0394f=f {bags} b = b++\u2205=b\nf\u2295\u0394f=f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 \u27e6derive\u27e7 f) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (f (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong (\u03bb hole \u2192 f x \u27e6\u2295\u27e7 (f hole \u27e6\u229d\u27e7 f x)) (f\u2295\u0394f=f x) \u27e9\n f x \u27e6\u2295\u27e7 (f x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (f x) \u27e9\n f x\n \u220e\n ) where open \u2261-Reasoning\n\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {nats} n dn = n \u2261 dn \u27e6fst\u27e7\nvalid-\u0394 {bags} b db = \u22a4 -- all bags are vald for all other bags\nvalid-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (s : \u27e6 \u03c4\u2081 \u27e7) (ds : \u27e6 \u0394-Type \u03c4\u2081 \u27e7) (R[s,ds] : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7 -- (valid-\u0394:1)\n (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 f s \u27e6\u2295\u27e7 df s ds -- (valid-\u0394:2)\n\nR[f,g\u229df] : \u2200 {\u03c4} (f g : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (g \u27e6\u229d\u27e7 f)\nR[f,g\u229df] {nats} m n = refl\nR[f,g\u229df] {bags} b\u2081 b\u2082 = tt\nR[f,g\u229df] {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb x dx R[x,dx] \u2192\n R[f,g\u229df] {\u03c4\u2082} (f x) (g (x \u27e6\u2295\u27e7 dx)) -- (valid-\u0394:1)\n , -- tuple constructor\n (begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 refl \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7\n (g ((x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 \u27e6derive\u27e7 (x \u27e6\u2295\u27e7 dx)) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 cong (\u03bb hole \u2192 f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g hole \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx)))\n (f\u2295\u0394f=f (x \u27e6\u2295\u27e7 dx)) \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 f\u2295[g\u229df]=g (f (x \u27e6\u2295\u27e7 dx)) (g (x \u27e6\u2295\u27e7 dx)) \u27e9\n g (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (f x) (g (x \u27e6\u2295\u27e7 dx))) \u27e9\n f x \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) x dx\n \u220e)\n where open \u2261-Reasoning\n\nR[f,\u0394f] : \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (\u27e6derive\u27e7 f)\nR[f,\u0394f] f rewrite sym (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f f) = R[f,g\u229df] f f\n\n-- Data type to hold proofs that a \u0394-env is consistent with self\ndata Consistent-\u0394env : {\u0393 : Context} (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is consistent with itself\n d\u03c1=\u2205 : Consistent-\u0394env {\u2205} \u2205\n -- Induction case: the change introduced at top level\n -- should be valid.\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Consistent-\u0394env d\u03c1\u2080 \u2192\n Consistent-\u0394env {\u03c4 \u2022 \u0393\u2080} (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- finally, update and ignore\n\nupdate : \u2200 {\u0393} \u2192 (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env d\u03c1} \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 {d\u03c1=\u2205} = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 d\u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] consistent[d\u03c1\u2080]} =\n (v \u27e6\u2295\u27e7 dv) \u2022 update d\u03c1 {consistent[d\u03c1\u2080]}\n\n-- Ignorance is bliss (don't have to pass consistency proofs around :D)\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 -- Using a proof to describe computation\n\n-- Naming scheme follows weakenVar\/weaken\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- derive(n) = \u03bbf. f n n\nderive (nat n) = abs (app (app (var this) (nat n)) (nat n))\n\n-- derive(x) = dx\nderive (var x) = var (deriveVar x)\n\n-- derive(\u03bbx. t) = \u03bbx. \u03bbdx. derive(t)\nderive (abs t) = abs (abs (derive t))\n\n-- derive(f s) = derive(f) s derive(s)\nderive (app f s) = app (app (derive f) (weaken \u0393\u227c\u0394\u0393 s)) (derive s)\n\n-- derive(x + y) = replace (x + y) by (dx snd + dy snd)\n-- = \u03bb f. f (x + y) (dx snd + dy snd)\nderive (add x y) =\n abs (app (app (var this)\n (add (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) x) (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) y)))\n (add (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive x)) snd)\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive y)) snd)))\n\n-- derive(bag) = \u2205\nderive (bag b) = bag emptyBag\n\n-- derive(map f b) = map (f \u2295 df) (b \u2295 db) \u229d map f b\nderive (map f b) = map ((weaken \u0393\u227c\u0394\u0393 f) \u2295 derive f)\n ((weaken \u0393\u227c\u0394\u0393 b) \u2295 derive b)\n \u229d weaken \u0393\u227c\u0394\u0393 (map f b)\n\n-- derive(b ++ d) = derive(b) ++ derive(d)\nderive (union b d) = union (derive b) (derive d)\n\n-- derive(b \\\\ d) = derive(b) \\\\ derive(d)\nderive (diff b d) = diff (derive b) (derive d)\n\n\n-- Extensional equivalence for changes\ndata Extensionally-equivalent-as-changes :\n \u2200 (\u03c4 : Type) \u2192 (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 (dg : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n ext-\u0394 : \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n (\u2200 (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192\n (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)) \u2192\n Extensionally-equivalent-as-changes \u03c4 df dg\n\nsyntax Extensionally-equivalent-as-changes \u03c4 df dg = df \u2248 dg :\u0394 \u03c4\n\n-- Question: How to declare fixity for infix syntax?\n-- infix 4 _\u2248_:\u0394_ -- same as \u2261\n\n-- Extractor for extensional-equivalence-as-changes:\n-- Given a value of the data type holding the proof,\n-- returns the proof in applicable form.\n--\n-- It would not be necessary if such\n-- proof-holding types were defined as a function in\n-- the first place, say in the manner of `valid-\u0394`.\n--\nextract-\u0394equiv :\n \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n df \u2248 dg :\u0394 \u03c4 \u2192\n (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192 (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)\n\nextract-\u0394equiv (ext-\u0394 proof-method) = proof-method\n\n-- Distribution lemmas of validity over \u27e6\u229d\u27e7 and \u27e6\u2295\u27e7\n\napplication-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v) \u2261 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\napplication-over-\u2295-and-\u229d f g df v =\n begin\n f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (cong\u2082 _\u27e6\u2295\u27e7_\n (cong f ((sym (f\u2295\u0394f=f v))))\n (cong\u2082 df\n (sym (f\u2295\u0394f=f v))\n (cong \u27e6derive\u27e7 (sym (f\u2295\u0394f=f v)))))\n refl \u27e9\n f (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v)\n \u27e6\u2295\u27e7 df (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v) (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v))\n \u27e6\u229d\u27e7 f v\n \u2261\u27e8 refl \u27e9\n (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e where open \u2261-Reasoning\n\nvalidity-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n valid-\u0394 g (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) \u2192\n valid-\u0394 (g v) (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\nvalidity-over-\u2295-and-\u229d f g df v R[g,f\u2295df]\n rewrite application-over-\u2295-and-\u229d f g df v =\n proj\u2081 (R[g,f\u2295df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))\n\n-- diff-apply\ndf=f\u2295df\u229df :\n \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n valid-\u0394 f df \u2192 df \u2248 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) :\u0394 \u03c4\n\n-- Case nat: this REFL is more obvious to Agda than to a human.\ndf=f\u2295df\u229df {nats} n dn valid-n-dn =\n ext-\u0394 (\u03bb m valid-m-dn valid-rhs \u2192 refl)\n\n-- Case bags: rely on set-theoretic interpretation of bags.\ndf=f\u2295df\u229df {bags} b db valid-b-db = ext-\u0394 (\u03bb d _ _ \u2192\n begin -- Reasoning done in 1 step. Here for clarity only.\n d \u27e6\u2295\u27e7 db\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_ {x = d} refl (sym ([b++d]\\\\b=d {b} {db})) \u27e9\n d \u27e6\u2295\u27e7 (b \u27e6\u2295\u27e7 db \u27e6\u229d\u27e7 b)\n \u220e) where open \u2261-Reasoning\n\ndf=f\u2295df\u229df {\u03c4\u2081 \u21d2 \u03c4\u2082} f df R[f,df] = ext-\u0394 (\n \u03bb g R[g,df] R[g,f\u2295df\u229df] \u2192\n extensionality (\u03bb v \u2192\n begin\n g v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n \u2261\u27e8 extract-\u0394equiv\n (df=f\u2295df\u229df\n (f v) (df v (\u27e6derive\u27e7 v))\n (proj\u2081 (R[f,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))))\n (g v)\n (proj\u2081 (R[g,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v)))\n (validity-over-\u2295-and-\u229d f g df v R[g,f\u2295df\u229df]) \u27e9\n g v \u27e6\u2295\u27e7 (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = g v} {y = g v} refl\n (application-over-\u2295-and-\u229d f g df v) \u27e9\n g v \u27e6\u2295\u27e7 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e\n )\n )\n where open \u2261-Reasoning\n\ncorrectness-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n (\u27e6 deriveVar x \u27e7 \u03c1)\n \u2248\n (\u27e6 x \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 x \u27e7 (ignore \u03c1)) :\u0394 \u03c4\n\ncorrectness-of-deriveVar {\u03c4 \u2022 \u0393\u2080} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n this = df=f\u2295df\u229df {\u03c4} v dv R[v,dv]\n\ncorrectness-of-deriveVar {\u03c4\u2080 \u2022 \u0393\u2080} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4\u2080} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n (that x) = correctness-of-deriveVar \u03c1 x\n\nmodule Foo where\n\ncorrectness-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n\n (\u27e6 derive t \u27e7 \u03c1)\n \u2248 (\u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 t \u27e7 (ignore \u03c1)) :\u0394 \u03c4\n\ncorrectness-of-derive\u2032 : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n\n (\u27e6 derive t \u27e7 \u03c1)\n \u2248 (\u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 \u03c1) :\u0394 \u03c4\n\ncorrectness-of-derive\u2032 \u03c1 {consistency} t\n rewrite weaken-sound {subctx = \u0393\u227c\u0394\u0393} t \u03c1\n = correctness-of-derive \u03c1 {consistency} t\n\n-- Mutually recursive lemma: diff-apply holds with propositional\n-- equality on bags\n\ndb=b\u2295db\u229db : \u2200 {\u0393 : Context} \u2192\n (b : Term \u0393 bags) \u2192\n {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} {consistency : Consistent-\u0394env \u03c1} \u2192\n \u27e6 derive b \u27e7 \u03c1 \u2261 \u27e6 b \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 b \u27e7 (ignore \u03c1)\ndb=b\u2295db\u229db b {\u03c1 = \u03c1} {consistency = consistency} =\n begin\n \u27e6 derive b \u27e7 \u03c1\n \u2261\u27e8 sym (\u2205++b=b {{\u27e6 derive b \u27e7 \u03c1}}) \u27e9\n emptyBag ++ \u27e6 derive b \u27e7 \u03c1\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} b)\n emptyBag tt tt \u27e9\n emptyBag ++ (\u27e6 b \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 b \u27e7 (ignore \u03c1))\n \u2261\u27e8 \u2205++b=b {{\u27e6 b \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 b \u27e7 (ignore \u03c1)}} \u27e9\n \u27e6 b \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 b \u27e7 (ignore \u03c1)\n \u220e where open \u2261-Reasoning\n\n-- Mutually recursive lemma: derivatives are valid\n\nvalidity-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 deriveVar x \u27e7 \u03c1)\n\nvalidity-of-deriveVar {\u03c4 \u2022 \u0393} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n this = R[v,dv]\n\nvalidity-of-deriveVar {\u03c4\u2080 \u2022 \u0393} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n (that x) = validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 {consistency} (var x) =\n validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs t) = \u03bb v dv R[v,dv] \u2192\n validity-of-derive (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency} t\n , -- tuple constructor\n (begin\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n ((R[f,g\u229df] (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 f\u2295[g\u229df]=g (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u2261\u27e8 cong \u27e6 t \u27e7 (cong\u2082 _\u2022_ (f\u2295\u0394f=f (v \u27e6\u2295\u27e7 dv)) refl) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency}))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 sym (extract-\u0394equiv\n (correctness-of-derive\n ((dv \u2022 v \u2022 \u03c1))\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (R[f,g\u229df] (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u220e)\n where open \u2261-Reasoning\n\nvalidity-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} t\u2081 t\u2082)\n = R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7]\n where\n open \u2261-Reasoning\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 : \u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1) \u2261 v\u2081 v\u2082\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 rewrite (sym v\u2082=old-v\u2082) = refl\n\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 : \u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1 \u2261 dv\u2081 v\u2082 dv\u2082\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = refl\n\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] : valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (dv\u2081 v\u2082 dv\u2082)\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] rewrite \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 = R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n -- What I want to write:\n {-\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] rewrite \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n -}\n\n -- What I have to write:\n\n R : {\u03c4 : Type} \u2192 {v : \u27e6 \u03c4 \u27e7} \u2192 {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 valid-\u0394 v dv\u2081 \u2192 valid-\u0394 v dv\u2082\n\n R {nats} dv\u2081=dv\u2082 refl = cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl\n\n R {bags} dv\u2081=dv\u2082 _ = tt\n\n --R {\u03c4\u2081 \u21d2 \u03c4\u2082} dv\u2081=dv\u2082 valid-dv\u2081 rewrite dv\u2081=dv\u2082 = {!valid-dv\u2081!}\n R {\u03c4\u2081 \u21d2 \u03c4\u2082} {v} {dv\u2081} {dv\u2082} dv\u2081=dv\u2082 valid-dv\u2081 =\n \u03bb s ds R[s,ds] \u2192\n R {\u03c4\u2082} {v s} {dv\u2081 s ds} {dv\u2082 s ds}\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl)\n (proj\u2081 (valid-dv\u2081 s ds R[s,ds]))\n ,\n (begin\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2082 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v (s \u27e6\u2295\u27e7 ds)} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) (sym dv\u2081=dv\u2082) refl) refl) \u27e9\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2081 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 sym (proj\u2082 (valid-dv\u2081\n (s \u27e6\u2295\u27e7 ds)\n (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n (R[f,\u0394f] (s \u27e6\u2295\u27e7 ds)))) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds \u27e6\u2295\u27e7 \u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong (v \u27e6\u2295\u27e7 dv\u2081) (f\u2295\u0394f=f (s \u27e6\u2295\u27e7 ds)) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds)\n \u2261\u27e8 proj\u2082 (valid-dv\u2081 s ds R[s,ds]) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2081 s ds\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v s} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2082 s ds\n \u220e) where open \u2261-Reasoning\n\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] = R \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n\n-- Validity of deriving bag-typed terms is trivial.\n\nvalidity-of-derive \u03c1 (bag b) = tt\n\nvalidity-of-derive \u03c1 (map f b) = tt\n\nvalidity-of-derive \u03c1 (union b d) = tt\n\nvalidity-of-derive \u03c1 (diff d b) = tt\n\nvalidity-of-derive \u03c1 (nat n) = refl\n\nvalidity-of-derive \u03c1 {consistency} (add m n) =\n begin\n \u27e6 m \u27e7 (ignore \u03c1) + \u27e6 n \u27e7 (ignore \u03c1)\n \u2261\u27e8 cong\u2082 _+_\n (sym (weaken-sound m (\u27e6fst\u27e7 \u2022 \u03c1)))\n (sym (weaken-sound n (\u27e6fst\u27e7 \u2022 \u03c1))) \u27e9\n (\u27e6 weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) m \u27e7 (\u27e6fst\u27e7 \u2022 \u03c1) +\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) n \u27e7 (\u27e6fst\u27e7 \u2022 \u03c1))\n \u2261\u27e8 refl \u27e9\n \u27e6 abs (app (app (var this)\n (add (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) m) (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) n)))\n (add (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive m)) snd)\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive n)) snd)))\n \u27e7 \u03c1 \u27e6fst\u27e7\n \u2261\u27e8 refl \u27e9\n \u27e6 derive (add m n) \u27e7 \u03c1 \u27e6fst\u27e7\n \u220e where\n open \u2261-Reasoning\n blah : \u2115\n blah =\n \u27e6 add (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive m)) snd)\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive n)) snd) \u27e7\n (\u27e6fst\u27e7 \u2022 \u03c1)\n\ncorrectness-of-derive \u03c1 (var x) = correctness-of-deriveVar \u03c1 x\n\ncorrectness-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs {.\u03c4\u2081} {.\u03c4\u2082} t) =\n ext-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n extensionality {\u27e6 \u03c4\u2081 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4\u2082 \u27e7} (\u03bb x \u2192\n begin\n f x \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive {\u03c4\u2081 \u2022 \u0393} {\u03c4\u2082}\n (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency}\n t)\n (f x)\n (proj\u2081 (R[f,\u0394t] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x)))\n (proj\u2081 (R[f,t\u2032\u229dt] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x))) \u27e9\n f x\n \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 (update (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency})\n \u27e6\u229d\u27e7 \u27e6 t \u27e7 (x \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u220e\n )) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} {\u0393} t\u2081 t\u2082) =\n ext-\u0394 {\u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n begin\n f \u27e6\u2295\u27e7 \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n \u2261\u27e8 extract-\u0394equiv ext-\u0394[t\u2081t\u2082] f R[f,\u0394t] R[f,t\u2032\u229dt] \u27e9\n f \u27e6\u2295\u27e7\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n \u220e)\n where\n open \u2261-Reasoning\n\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n v\u2081\u2295dv\u2081=v\u2081\u2032 : v\u2081 \u27e6\u2295\u27e7 dv\u2081 \u2261 v\u2081\u2032\n v\u2081\u2295dv\u2081=v\u2081\u2032 =\n begin\n v\u2081 \u27e6\u2295\u27e7 dv\u2081\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2081)\n v\u2081 valid-dv\u2081 (R[f,g\u229df] v\u2081 v\u2081\u2032) \u27e9\n v\u2081 \u27e6\u2295\u27e7 (v\u2081\u2032 \u27e6\u229d\u27e7 v\u2081)\n \u2261\u27e8 f\u2295[g\u229df]=g v\u2081 v\u2081\u2032 \u27e9\n v\u2081\u2032\n \u220e\n\n -- TODO: remove code duplication.\n v\u2082\u2295dv\u2082=v\u2082\u2032 : v\u2082 \u27e6\u2295\u27e7 dv\u2082 \u2261 v\u2082\u2032\n v\u2082\u2295dv\u2082=v\u2082\u2032 rewrite v\u2082=old-v\u2082 =\n begin\n old-v\u2082 \u27e6\u2295\u27e7 dv\u2082\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2082)\n old-v\u2082\n (validity-of-derive \u03c1 {consistency} t\u2082)\n (R[f,g\u229df] old-v\u2082 v\u2082\u2032) \u27e9\n old-v\u2082 \u27e6\u2295\u27e7 (v\u2082\u2032 \u27e6\u229d\u27e7 old-v\u2082)\n \u2261\u27e8 f\u2295[g\u229df]=g old-v\u2082 v\u2082\u2032 \u27e9\n v\u2082\u2032\n \u220e\n where old-v\u2082 = \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] : v\u2081\u2032 v\u2082\u2032 \u2261 (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082)\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] = sym (cong\u2082 (\u03bb f x \u2192 f x) v\u2081\u2295dv\u2081=v\u2081\u2032 v\u2082\u2295dv\u2082=v\u2082\u2032)\n\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 :\n (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082) \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 = proj\u2082 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082 \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n cong\u2082 _\u27e6\u229d\u27e7_\n (trans v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082)\n refl\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n (dv\u2081 v\u2082 dv\u2082) \u2248 (v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082) :\u0394 \u03c4\u2082\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n df=f\u2295df\u229df (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082) R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 : (dv\u2081 v\u2082 dv\u2082) \u2248 (v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082) :\u0394 \u03c4\u2082\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 rewrite v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082\n\n ext-\u0394[t\u2081t\u2082] :\n (\u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1))\n \u2248\n (\u27e6 t\u2081 \u27e7 (update \u03c1 {consistency}) (\u27e6 t\u2082 \u27e7 (update \u03c1 {consistency}))\n \u27e6\u229d\u27e7 \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n :\u0394 \u03c4\u2082\n ext-\u0394[t\u2081t\u2082] rewrite sym v\u2082=old-v\u2082 = dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082\n\ncorrectness-of-derive \u03c1 (nat n) = ext-\u0394 (\u03bb _ _ _ \u2192 refl)\n\ncorrectness-of-derive \u03c1 (bag b) = ext-\u0394 (\u03bb d _ _ \u2192\n begin\n d ++ emptyBag\n \u2261\u27e8 cong\u2082 _++_ {x = d} refl (sym (b\\\\b=\u2205)) \u27e9\n d ++ (b \\\\ b)\n \u220e) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (add m n) = ext-\u0394 (\u03bb _ _ _ \u2192\n begin\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (derive m) \u27e7 (\u27e6snd\u27e7 \u2022 \u03c1) \u27e6snd\u27e7\n +\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (derive n) \u27e7 (\u27e6snd\u27e7 \u2022 \u03c1) \u27e6snd\u27e7\n \u2261\u27e8 refl \u27e9\n (\u27e6 m \u27e7 (ignore \u03c1) \u27e6\u2295\u27e7 \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (derive m) \u27e7 (\u27e6snd\u27e7 \u2022 \u03c1))\n +\n (\u27e6 n \u27e7 (ignore \u03c1) \u27e6\u2295\u27e7 \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (derive n) \u27e7 (\u27e6snd\u27e7 \u2022 \u03c1))\n \u2261\u27e8 cong\u2082 _+_\n (cong\u2082 _\u27e6\u2295\u27e7_ {x = \u27e6 m \u27e7 (ignore \u03c1)} refl\n (weaken-derivative {t = m} {\u03c1 = \u03c1}))\n ((cong\u2082 _\u27e6\u2295\u27e7_ {x = \u27e6 n \u27e7 (ignore \u03c1)} refl\n (weaken-derivative {t = n} {\u03c1 = \u03c1}))) \u27e9\n (\u27e6 m \u27e7 (ignore \u03c1) \u27e6\u2295\u27e7 \u27e6 derive m \u27e7 \u03c1)\n +\n (\u27e6 n \u27e7 (ignore \u03c1) \u27e6\u2295\u27e7 \u27e6 derive n \u27e7 \u03c1)\n \u2261\u27e8 cong\u2082 _+_\n (extract-\u0394equiv (correctness-of-derive \u03c1 {consistency} m)\n (\u27e6 m \u27e7 (ignore \u03c1))\n (validity-of-derive \u03c1 {consistency} m)\n (R[f,g\u229df] (\u27e6 m \u27e7 (ignore \u03c1)) (\u27e6 m \u27e7 (update \u03c1 {consistency}))))\n ((extract-\u0394equiv (correctness-of-derive \u03c1 {consistency} n)\n (\u27e6 n \u27e7 (ignore \u03c1))\n (validity-of-derive \u03c1 {consistency} n)\n (R[f,g\u229df] (\u27e6 n \u27e7 (ignore \u03c1)) (\u27e6 n \u27e7 (update \u03c1 {consistency}))))) \u27e9\n \u27e6 m \u27e7 (update \u03c1) + \u27e6 n \u27e7 (update \u03c1)\n \u220e\n ) where\n open \u2261-Reasoning\n identity-weakening : \u2200 {\u0393} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192 \u27e6 \u0393\u227c\u0393 {\u0393} \u27e7 \u03c1 \u2261 \u03c1\n identity-weakening {\u2205} {\u2205} = refl\n identity-weakening {\u03c4 \u2022 \u0393} {v \u2022 \u03c1} =\n cong\u2082 _\u2022_ {x = v} refl (identity-weakening {\u0393} {\u03c1})\n weaken-derivative :\n \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7}\n \u2192 \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (derive t) \u27e7 (\u27e6snd\u27e7 \u2022 \u03c1)\n \u2261 \u27e6 derive t \u27e7 \u03c1\n weaken-derivative {t = t} {\u03c1 = \u03c1}\n rewrite weaken-sound {subctx = drop _ \u2022 \u0393\u227c\u0393} (derive t) (\u27e6snd\u27e7 \u2022 \u03c1)\n | identity-weakening {\u03c1 = \u03c1}\n = refl\n\ncorrectness-of-derive \u03c1 {consistency} (union b d) = ext-\u0394 (\u03bb c _ _ \u2192\n begin\n c ++ (\u27e6 derive b \u27e7 \u03c1 ++ \u27e6 derive d \u27e7 \u03c1)\n \u2261\u27e8 cong\u2082 _++_\n {x = c} refl\n (cong\u2082 _++_\n (db=b\u2295db\u229db b)\n (db=b\u2295db\u229db d)) \u27e9\n c ++ ((\u27e6 b \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 b \u27e7 (ignore \u03c1))\n ++ (\u27e6 d \u27e7 (update \u03c1 {consistency}) \\\\ \u27e6 d \u27e7 (ignore \u03c1)))\n \u2261\u27e8 cong\u2082 _++_\n {x = c} refl\n (sym ([a++b]\\\\[c++d]=[a\\\\c]++[b\\\\d]\n {\u27e6 b \u27e7 (update \u03c1 {consistency})} \n {\u27e6 d \u27e7 (update \u03c1 {consistency})}\n {\u27e6 b \u27e7 (ignore \u03c1)} {\u27e6 d \u27e7 (ignore \u03c1)})) \u27e9\n c ++ ((\u27e6 b \u27e7 (update \u03c1 {consistency})\n ++ \u27e6 d \u27e7 (update \u03c1 {consistency}))\n \\\\(\u27e6 b \u27e7 (ignore \u03c1) ++ \u27e6 d \u27e7 (ignore \u03c1)))\n \u220e) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} _ = {!!}\n\n\ncorrectness-on-closed-terms : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192\n \u2200 (f : Term \u2205 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192\n \u2200 (s : Term \u2205 \u03c4\u2081) (ds : Term \u2205 (\u0394-Type \u03c4\u2081))\n {R[v,dv] : valid-\u0394 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)} \u2192\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205 \u27e6\u2295\u27e7 \u27e6 ds \u27e7 \u2205)\n \u2261\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)\n\ncorrectness-on-closed-terms {\u03c4\u2081} {\u03c4\u2082} f s ds {R[v,dv]} =\n begin\n h (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (f\u2295[g\u229df]=g h h))\n refl \u27e9\n (h \u27e6\u2295\u27e7 (h \u27e6\u229d\u27e7 h)) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (extract-\u0394equiv\n (correctness-of-derive \u2205 f)\n h\n (validity-of-derive \u2205 {d\u03c1=\u2205} f)\n (R[f,g\u229df] h h)))\n refl \u27e9\n (h \u27e6\u2295\u27e7 \u0394h) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 proj\u2082 (validity-of-derive \u2205 {d\u03c1=\u2205} f v dv R[v,dv]) \u27e9\n h v \u27e6\u2295\u27e7 \u0394h v dv\n \u220e\n where\n open \u2261-Reasoning\n h : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n h = \u27e6 f \u27e7 \u2205\n \u0394h : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n \u0394h = \u27e6 derive f \u27e7 \u2205\n v : \u27e6 \u03c4\u2081 \u27e7\n v = \u27e6 s \u27e7 \u2205\n dv : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv = \u27e6 ds \u27e7 \u2205\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"fbc8ffee14d64846f584fcb0c90726b733b42e48","subject":"Removed unnecessary imports.","message":"Removed unnecessary imports.\n\nIgnore-this: 223d6f86e9cb2503a0a973330e080f71\n\ndarcs-hash:20110306132538-3bd4e-6d77c41b16ebbd00eb0ed7cc16b12f780f291484.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Data\/List\/Induction\/Acc\/WellFounded.agda","new_file":"src\/FOTC\/Data\/List\/Induction\/Acc\/WellFounded.agda","new_contents":"------------------------------------------------------------------------------\n-- Generic well-founded induction on lists\n------------------------------------------------------------------------------\n\n-- Adapted from FOTC.Data.Nat.Induction.Acc.WellFounded.\n\nmodule FOTC.Data.List.Induction.Acc.WellFounded where\n\nopen import FOTC.Base\n\nopen import Common.Relation.Unary\n\nopen import FOTC.Data.List.Type\n\n------------------------------------------------------------------------------\n-- The accessibility predicate: x is accessible if everything which is\n-- smaller than x is also accessible (inductively).\ndata Acc (_<_ : D \u2192 D \u2192 Set) : D \u2192 Set where\n acc : \u2200 {xs} \u2192 List xs \u2192 (\u2200 {ys} \u2192 List ys \u2192 ys < xs \u2192 Acc _<_ ys) \u2192\n Acc _<_ xs\n\naccFold : {P : D \u2192 Set} (_<_ : D \u2192 D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 List xs \u2192 (\u2200 {ys} \u2192 List ys \u2192 ys < xs \u2192 P ys) \u2192 P xs) \u2192\n \u2200 {xs} \u2192 List xs \u2192 Acc _<_ xs \u2192 P xs\naccFold _<_ f Lxs (acc _ h) =\n f Lxs (\u03bb Lys ys0\u2192x\u2238y0\u2192x\u2238y dist (x * y) (x * z) == x * dist y z))\n\ninfix 8 _^_\n_^_ : \u2115 \u2192 \u2115 \u2192 \u2115\nb ^ zero = 1\nb ^ suc n = b * b ^ n\n\n2^_ : \u2115 \u2192 \u2115\n2^ n = \u27e82^ n * 1 \u27e9\n\n2^-spec : \u2200 n \u2192 2^ n \u2261 2 ^ n\n2^-spec zero = \u2261.refl\n2^-spec (suc n) rewrite 2^-spec n | 2*-spec (2 ^ n) = \u2261.refl\n\n2^*-spec : \u2200 m n \u2192 2^\u27e8 m \u27e9* n \u2261 2 ^ m * n\n2^*-spec zero n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\n2^*-spec (suc m) n rewrite 2^*-spec m n\n | \u2115\u00b0.*-assoc 2 (2 ^ m) n\n | \u2115\u00b0.+-comm (2 ^ m * n) 0 = \u2261.refl\n\n1\u22642^ : \u2200 n \u2192 2^ n \u2265 1\n1+\u22642^ : \u2200 n \u2192 2^ n \u2265 1 + n\n1+\u22642^ zero = s\u2264s z\u2264n\n1+\u22642^ (suc n) = (1\u22642^ n) +-mono (1+\u22642^ n)\n\n1\u22642^ n = \u2115\u2264.trans (s\u2264s z\u2264n) (1+\u22642^ n)\n\n\u2264-steps\u2032 : \u2200 {x} y \u2192 x \u2264 x + y\n\u2264-steps\u2032 {x} y rewrite \u2115\u00b0.+-comm x y = \u2264-steps y \u2115\u2264.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Hyper_operator\n_\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\na \u2191\u27e8 suc n \u27e9 (suc b) = a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\na \u2191\u27e8 0 \u27e9 b = suc b\na \u2191\u27e8 1 \u27e9 0 = a\na \u2191\u27e8 2 \u27e9 0 = 0\na \u2191\u27e8 suc (suc (suc n)) \u27e9 0 = 1\n\nmodule \u2191-Props where\n \u2191-fold : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 b \u2261 fold (a \u2191\u27e8 suc n \u27e9 0) (_\u2191\u27e8_\u27e9_ a n) b\n \u2191-fold a n zero = \u2261.refl\n \u2191-fold a n (suc b) = \u2261.cong (_\u2191\u27e8_\u27e9_ a n) (\u2191-fold a n b)\n\n \u2191\u2080-+ : \u2200 a b \u2192 a \u2191\u27e8 0 \u27e9 b \u2261 1 + b\n \u2191\u2080-+ a b = \u2261.refl\n\n \u2191\u2081-+ : \u2200 a b \u2192 a \u2191\u27e8 1 \u27e9 b \u2261 b + a\n \u2191\u2081-+ a zero = \u2261.refl\n \u2191\u2081-+ a (suc b) = \u2261.cong suc (\u2191\u2081-+ a b)\n\n \u2191\u2082-* : \u2200 a b \u2192 a \u2191\u27e8 2 \u27e9 b \u2261 b * a\n \u2191\u2082-* a zero = \u2261.refl\n \u2191\u2082-* a (suc b) rewrite \u2191\u2082-* a b\n | \u2191\u2081-+ a (b * a)\n | \u2115\u00b0.+-comm (b * a) a\n = \u2261.refl\n\n -- fold 1 (_*_ a) b \u2261 a ^ b\n \u2191\u2083-^ : \u2200 a b \u2192 a \u2191\u27e8 3 \u27e9 b \u2261 a ^ b\n \u2191\u2083-^ a zero = \u2261.refl\n \u2191\u2083-^ a (suc b) rewrite \u2191\u2083-^ a b\n | \u2191\u2082-* a (a ^ b)\n | \u2115\u00b0.*-comm (a ^ b) a\n = \u2261.refl\n\n _\u2191\u27e8_\u27e90 : \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u27e8 0 \u27e90 = 1\n a \u2191\u27e8 1 \u27e90 = a\n a \u2191\u27e8 2 \u27e90 = 0\n a \u2191\u27e8 suc (suc (suc n)) \u27e90 = 1\n\n _`\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _`\u2191\u27e8_\u27e9_ a 0 = suc\n _`\u2191\u27e8_\u27e9_ a (suc n) = fold (a \u2191\u27e8 suc n \u27e90) (_`\u2191\u27e8_\u27e9_ a n)\n\n \u2191-ind-rule : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 (suc b) \u2261 a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\n \u2191-ind-rule a n b = \u2261.refl\n\n _\u2191\u2032\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u2032\u27e8 0 \u27e9 b = suc b\n a \u2191\u2032\u27e8 1 \u27e9 b = a + b\n a \u2191\u2032\u27e8 2 \u27e9 b = a * b\n a \u2191\u2032\u27e8 3 \u27e9 b = a ^ b\n a \u2191\u2032\u27e8 suc (suc (suc (suc n))) \u27e9 b = a \u2191\u27e8 4 + n \u27e9 b\n\n \u2191-\u2191\u2032 : \u2200 a n b \u2192 a \u2191\u27e8 n \u27e9 b \u2261 a \u2191\u2032\u27e8 n \u27e9 b\n \u2191-\u2191\u2032 a 0 b = \u2261.refl\n \u2191-\u2191\u2032 a 1 b = \u2261.trans (\u2191\u2081-+ a b) (\u2115\u00b0.+-comm b a)\n \u2191-\u2191\u2032 a 2 b = \u2261.trans (\u2191\u2082-* a b) (\u2115\u00b0.*-comm b a)\n \u2191-\u2191\u2032 a 3 b = \u2191\u2083-^ a b\n \u2191-\u2191\u2032 a (suc (suc (suc (suc _)))) b = \u2261.refl\n\n _\u21912+\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _\u21912+\u27e8_\u27e9_ a = fold (_*_ a) (fold 1)\n\nmodule InflModule where\n -- Inflationary functions\n Infl< : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl< f = \u2200 {x} \u2192 x < f x\n\n Infl : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl f = Infl< (suc \u2218 f)\n\n InflT< : (f : Endo (Endo \u2115)) \u2192 Set\n InflT< f = \u2200 {g} \u2192 Infl< g \u2192 Infl< (f g)\n\n{-\n\u2200 {x} \u2192 x \u2264 f x\n\n\n 1 + x \u2264 1 + f x\n x < 1 + f x\n x < (suc \u2218 f) x\n Infl< (suc \u2218 f)\n\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n\nInfl< f \u2192 Infl f\nInfl< f \u2192 Infl< (suc \u2218 f)\n-}\n\n id-infl : Infl id\n id-infl = \u2115\u2264.refl\n\n \u2218-infl< : \u2200 {f g} \u2192 Infl< f \u2192 Infl< g \u2192 Infl< (f \u2218 g)\n \u2218-infl< inflf< inflg< = <-trans inflg< inflf<\n\n suc-infl< : Infl< suc\n suc-infl< = \u2115\u2264.refl\n\n suc-infl : Infl suc\n suc-infl = s\u2264s (\u2264-step \u2115\u2264.refl)\n\n nest-infl : \u2200 f (finfl : Infl f) n \u2192 Infl (nest n f)\n nest-infl f _ zero = \u2115\u2264.refl\n nest-infl f finfl (suc n) = \u2115\u2264.trans (nest-infl f finfl n) finfl\n\n nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n nest-infl< f finfl< zero = finfl<\n nest-infl< f finfl< (suc n) = <-trans (nest-infl< f finfl< n) finfl< \n\n-- See Function.NP.nest-Properties for more properties\nmodule fold-Props where\n\n fold-suc : \u2200 m n \u2192 fold m suc n \u2261 n + m\n fold-suc m zero = \u2261.refl\n fold-suc m (suc n) rewrite fold-suc m n = \u2261.refl\n\n fold-+ : \u2200 m n \u2192 fold 0 (_+_ m) n \u2261 n * m\n fold-+ m zero = \u2261.refl\n fold-+ m (suc n) rewrite fold-+ m n = \u2261.refl\n\n fold-* : \u2200 m n \u2192 fold 1 (_*_ m) n \u2261 m ^ n\n fold-* m zero = \u2261.refl\n fold-* m (suc n) rewrite fold-* m n = \u2261.refl\n\n{- TODO\n fold-^ : \u2200 m n \u2192 fold 1 (flip _^_ m) n \u2261 m \u2191\u27e8 4 \u27e9 n\n fold-^ m zero = \u2261.refl\n fold-^ m (suc n) rewrite fold-^ m n = ?\n-}\n\n cong-fold : \u2200 {A : Set} {f g : Endo A} (f\u2257g : f \u2257 g) {z} \u2192 fold z f \u2257 fold z g\n cong-fold eq zero = \u2261.refl\n cong-fold eq {z} (suc x) rewrite cong-fold eq {z} x = eq _\n\n private\n open \u2191-Props\n module Alt\u21912 where\n alt : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n alt a 0 = \u03bb b \u2192 a \u2191\u27e8 2 \u27e9 b\n alt a (suc n) = fold 1 (alt a n)\n\n alt-ok : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 alt a n\n alt-ok a zero = \u2261.sym \u2218 \u2191-\u2191\u2032 a 2\n alt-ok a (suc n) = cong-fold (alt-ok a n)\n\n{- TODO\n alt' : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 _\u2191\u27e8_\u27e9_ a (2 + n)\n alt' a zero b = \u2261.sym (\u2191-\u2191\u2032 a 2 _)\n alt' a (suc n) zero = \u2261.refl\n alt' a (suc n) (suc x) rewrite alt' a n (_\u21912+\u27e8_\u27e9_ a (suc n) x)\n = \u2261.cong (_\u2191\u27e8_\u27e9_ a (2 + n)) { fold 1 (fold (_*_ a) (fold 1) n) x} {a \u2191\u27e8 suc (suc (suc n)) \u27e9 x} (alt' a (suc n) x)\n-}\n\n open InflModule\n{-\n fold1-infl : \u2200 f \u2192 Infl f \u2192 Infl (fold 1 f)\n fold1-infl f finfl {zero} = {!z\u2264n!}\n fold1-infl f finfl {suc x} =\n x <\u27e8 {!s\u2264s (fold1-infl f finfl {x})!} \u27e9\n fold 1 f x <\u27e8 {!!} \u27e9\n {- f (fold 1 f x) \u2264\u27e8 finfl \u27e9\n f (fold 1 f x) \u2261\u27e8 \u2261.refl \u27e9 -}\n fold 1 f (1 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 f \u2218 suc)\n fold1+1-infl< f finfl< {zero} = finfl<\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + f (fold 1 f x) \u2264\u27e8 finfl< {f (fold 1 f x)} \u27e9\n f (suc (f (fold 1 f x))) \u2264\u27e8 {!!} \u27e9\n fold 1 f (2 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 (f \u2218 suc))\n fold1+1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 suc) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 suc) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+-infl< : \u2200 k f \u2192 Infl< (f \u2218\u2032 _+_ k) \u2192 Infl< (fold 1 (f \u2218\u2032 _+_ k))\n fold1+-infl< k f finfl< {zero} = s\u2264s z\u2264n\n fold1+-infl< k f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-infl< k f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 _+_ k) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 _+_ k) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n \n{-\n fold1+-infl< : \u2200 f \u2192 Infl+2< f \u2192 Infl+2< (fold 1 f)\n fold1+-infl< f finfl< {zero} _ = \u2115\u2264.refl\n fold1+-infl< f finfl< {suc zero} _ = {!!}\n fold1+-infl< f finfl< {suc (suc zero)} _ = {!!}\n fold1+-infl< f finfl< {suc (suc (suc x))} x>1 =\n 4 + x \u2264\u27e8 s\u2264s (fold1+-infl< f finfl< {suc (suc x)} (m\u2264m+n 2 x)) \u27e9\n 1 + fold 1 f (2 + x) \u2264\u27e8 finfl< {!!} \u27e9\n fold 1 f (3 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 f x \u2264\u27e8 finfl< \u27e9\n fold 1 f (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\nmodule ^-Props where\n open \u2261-Reasoning\n\n ^1 : \u2200 n \u2192 n ^ 1 \u2261 n\n ^1 zero = \u2261.refl\n ^1 (suc n) = \u2261.cong suc (proj\u2082 \u2115\u00b0.*-identity n)\n\n ^-+ : \u2200 b m n \u2192 b ^ (m + n) \u2261 b ^ m * b ^ n\n ^-+ b zero n = \u2261.sym (proj\u2082 \u2115\u00b0.+-identity (b ^ n))\n ^-+ b (suc m) n rewrite ^-+ b m n = \u2261.sym (\u2115\u00b0.*-assoc b (b ^ m) (b ^ n))\n\n ^-* : \u2200 b m n \u2192 b ^ (m * n) \u2261 (b ^ n) ^ m\n ^-* b zero n = \u2261.refl\n ^-* b (suc m) n = b ^ (n + m * n)\n \u2261\u27e8 ^-+ b n (m * n) \u27e9\n b ^ n * b ^ (m * n)\n \u2261\u27e8 \u2261.cong (_*_ (b ^ n)) (^-* b m n) \u27e9\n b ^ n * (b ^ n) ^ m \u220e\n\nack : \u2115 \u2192 \u2115 \u2192 \u2115\nack zero n = suc n\nack (suc m) zero = ack m (suc zero)\nack (suc m) (suc n) = ack m (ack (suc m) n)\n\n\u2264\u21d2\u2203 : \u2200 {m n} \u2192 m \u2264 n \u2192 \u2203 \u03bb k \u2192 m + k \u2261 n\n\u2264\u21d2\u2203 z\u2264n = _ , \u2261.refl\n\u2264\u21d2\u2203 (s\u2264s pf) = _ , \u2261.cong suc (proj\u2082 (\u2264\u21d2\u2203 pf))\n\nmodule ack-Props where\n lem\u2238 : \u2200 {m n} \u2192 m \u2264 n \u2192 m + (n \u2238 m) \u2261 n\n lem\u2238 z\u2264n = \u2261.refl\n lem\u2238 (s\u2264s {m} {n} m\u2264n) = \u2261.cong suc (lem\u2238 m\u2264n)\n\n -- n >= m \u2192 m + (n \u2238 m) \u2261 n\n -- n >= m \u2192 \u2203 k \u2192 n \u2261 m + k\n -- \u2203 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 m + ((m + k) \u2238 m) \u2261 m + k\n -- \u2200 k \u2192 m + k \u2261 m + k\n\n -- 3 \u2264 x \u2192 P x \u2192 \u2203 \u03bb k \u2192 P (3 + k)\n\n open InflModule\n\n fold1+-inflT< : \u2200 {z} \u2192 InflT< (fold (suc z))\n fold1+-inflT< _ {zero} = s\u2264s z\u2264n\n fold1+-inflT< {z} {f} finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-inflT< {z} {f} finfl< {x}) \u27e9\n 1 + fold (suc z) f x \u2264\u27e8 finfl< \u27e9\n f (fold (suc z) f x) \u2261\u27e8 \u2261.refl \u27e9\n fold (suc z) f (1 + x)\n \u220e\n where open \u2264-Reasoning\n\n Mon : (f : \u2115 \u2192 \u2115) \u2192 Set\n Mon f = \u2200 {x y} \u2192 x \u2264 y \u2192 f x \u2264 f y\n open InflModule\n\n\n \u2115-ind : \u2200 (P : \u2115 \u2192 Set) \u2192 P zero \u2192 (\u2200 {n} \u2192 P n \u2192 P (suc n)) \u2192 \u2200 {n} \u2192 P n\n \u2115-ind P P0 PS {zero} = P0\n \u2115-ind P P0 PS {suc n} = PS (\u2115-ind P P0 PS)\n\n open fold-Props\n fold-infl< : \u2200 {f g} \u2192 Infl< f \u2192 InflT< g \u2192 \u2200 {n} \u2192 Infl< (fold f g n)\n fold-infl< {f} {g} inflf< inflTg< {n} = \u2115-ind (Infl< \u2218 fold f g) inflf< inflTg< {n}\n\n fold-mon : \u2200 {f} (fmon : Mon f) (finfl< : Infl< f) {z} \u2192 Mon (fold z f)\n fold-mon fmon finfl< {_} {y = 0} z\u2264n = \u2115\u2264.refl\n fold-mon {f} fmon finfl< {z} {y = suc n} z\u2264n = z \u2264\u27e8 \u2264-step \u2115\u2264.refl \u27e9\n suc z \u2264\u27e8 nest-infl< f finfl< n {z} \u27e9\n fold z f (suc n) \u2261\u27e8 \u2261.refl \u27e9\n fold z f (suc n) \u220e\n where open \u2264-Reasoning\n fold-mon fmon finfl< (s\u2264s m\u2264n) = fmon (fold-mon fmon finfl< m\u2264n)\n\n MonT : Endo (Endo \u2115) \u2192 Set\n MonT g = \u2200 {f} \u2192 Mon f \u2192 Infl< f \u2192 Mon (g f)\n\n fold-mon' : \u2200 {f g} \u2192 Mon f \u2192 Infl< f \u2192 MonT g \u2192 InflT< g \u2192 \u2200 {n} \u2192 Mon (fold f g n)\n fold-mon' {f} {g} mon-f infl-f mont-g inflTg< {n} = go n where\n go : \u2200 n \u2192 Mon (fold f g n)\n go zero = mon-f\n go (suc n) = mont-g (go n) (fold-infl< infl-f inflTg< {n})\n\n\n -- +-fold : \u2200 a b \u2192 a + b \u2261 fold a suc b\n -- *-fold : \u2200 a b \u2192 a * b \u2261 fold 0 (_+_ a) b\n\n 1\u22641+a^b : \u2200 a b \u2192 1 \u2264 (1 + a) ^ b\n 1\u22641+a^b a zero = s\u2264s z\u2264n\n 1\u22641+a^b a (suc b) = 1\u22641+a^b a b +-mono z\u2264n\n\n 1+a^-mon : \u2200 {a} \u2192 Mon (_^_ (1 + a))\n 1+a^-mon {a} (z\u2264n {b}) = 1\u22641+a^b a b\n 1+a^-mon {a} (s\u2264s {m} {n} m\u2264n)\n = (1 + a) ^ (1 + m) \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ m + a * (1 + a) ^ m \u2264\u27e8 (1+a^-mon m\u2264n) +-mono ((a \u220e) *-mono (1+a^-mon m\u2264n)) \u27e9\n (1 + a) ^ n + a * (1 + a) ^ n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ (1 + n) \u220e\n where open \u2264-Reasoning\n\n lem2^3 : \u2200 n \u2192 2 ^ 3 \u2264 2 ^ (3 + n)\n lem2^3 n = 1+a^-mon {1} {3} {3 + n} (s\u2264s (s\u2264s (s\u2264s z\u2264n)))\n\n lem2221 : \u2200 m \u2192 2 \u2261 2 \u2191\u27e8 2 + m \u27e9 1\n lem2221 zero = \u2261.refl\n lem2221 (suc m) = lem2221 m\n\n lem4212 : \u2200 m \u2192 4 \u2261 2 \u2191\u27e8 1 + m \u27e9 2\n lem4212 zero = \u2261.refl\n lem4212 (suc m) = 4 \u2261\u27e8 lem4212 m \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 2 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 (1 + m)) (lem2221 m) \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 (2 \u2191\u27e8 2 + m \u27e9 1) \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 2 + m \u27e9 2 \u220e\n where open \u2261-Reasoning\n\n 2* = _*_ 2\n\n -- fold 2* n 1 \u2261 2 ^ n\n\n -- nest-* : \u2200 m n \u2192 nest (m * n) f \u2257 nest m (nest n f)\n\n nest-2* : \u2200 m n \u2192 nest (m * n) (fold 1) 2* \u2261 nest m (nest n (fold 1)) 2*\n nest-2* m n = nest-Properties.nest-* (fold 1) m n 2*\n\n{-\n lem3\u2264 : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 2\n lem3\u2264 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264 (suc n) = \n 3 \u2264\u27e8 {!!} \u27e9\n fold 2* f1 (1 + n) 2 \u220e\n where open \u2264-Reasoning\n f1 = fold 1\n\n lem3\u2264' : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 3\n lem3\u2264' 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264' (suc n) = \n 3 \u2264\u27e8 lem3\u2264 n \u27e9\n fold 2* (fold 1) n 2 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) n (fold 2* (fold 1) 3 1) \u2261\u27e8 {!!} \u27e9\n{- \u2261\u27e8 nest-Properties.nest-* {!2*!} {!!} {!!} {!!} \u27e9\n fold 2* (fold 1) (3 * n) 1 \u2261\u27e8 {!!} \u27e9\n-}\n fold 1 (fold 2* (fold 1) n) 3 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) (1 + n) 3 \u220e\n where open \u2264-Reasoning\n-}\n---\n\n{-\nfold 2* (fold 1) (3 * n) 1 \u2264\n\nfold 2* (fold 1) n (fold 2* (fold 1) n (fold 2* (fold 1) n 1)) \u2264\n\nfold 1 (fold 2* (fold 1) n) 3 \u2264\nfold 2* (fold 1) (1 + n) 3\n-}\n\n{-\n open \u2191-Props\n\n -*\u27e81+n\u27e9-infl : \u2200 n \u2192 Infl (_*_ (1 + n))\n -*\u27e81+n\u27e9-infl n = {!!}\n\n lem121 : \u2200 a b \u2192 b < (1 + a) \u2191\u27e8 2 \u27e9 (1 + b)\n lem121 a b rewrite \u2191\u2082-* (1 + a) (1 + b)\n = 1 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (1 + a) * (1 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (1 + a) (1 + b) \u27e9\n (1 + b) * (1 + a)\n \u220e\n where open \u2264-Reasoning\n\n -- \u2200 a b \u2192 b < (2 + a) * b\n lem222 : \u2200 a b \u2192 b < (2 + a) \u2191\u27e8 2 \u27e9 (2 + b)\n lem222 a b rewrite \u2191\u2082-* (2 + a) (2 + b)\n = 1 + b\n \u2264\u27e8 suc-infl \u27e9\n 2 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (2 + a) * (2 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (2 + a) (2 + b) \u27e9\n (2 + b) * (2 + a)\n \u220e\n where open \u2264-Reasoning\n-}\n{-\n open fold-Props\n\n module Foo a where\n f = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b\n f-lem : \u2200 n \u2192 f (suc n) \u2257 fold 1 (f n)\n f-lem n x = \u2261.refl\n\n f2 = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n f2-lem : \u2200 n \u2192 f2 (suc n) \u2257 fold 1 (f2 n)\n f2-lem n x = {!!}\n\n -- b < (1 + a) * (1 + b)\n\n -- nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n infl3< : \u2200 a n \u2192 Infl< (\u03bb b \u2192 (1 + a) \u21912+\u27e8 n \u27e9 (1 + b))\n infl3< a zero {b} = lem121 a b\n infl3< a (suc n) {b}\n = 1 + b\n -- fold1-infl< : \u2200 f \u2192 Infl< f \u2192 \u2200 x \u2192 x < fold 1 f x\n \u2264\u27e8 {!fold1f2-infl< {_}!} \u27e9\n fold 1 f1 b\n \u2264\u27e8 {!!} \u27e9\n{-\n \u2264\u27e8 fold1-infl< (f 0) (\u03bb {x} \u2192 {!infl3< a n {x}!}) {{!!}} \u27e9\n {!!}\n \u2264\u27e8 {!!} \u27e9\n-}\n{-\n 1 + b\n (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n \u2264\u27e8 {!nest-infl< (\u03bb b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b) ? b {(2 + a) \u21912+\u27e8 n \u27e9 (2 + b)}!} \u27e9\n fold (f 0 1) (f 0) (1 + b)\n \u2264\u27e8 {!\u2115\u2264.reflexive (\u2261.cong (\u03bb g \u2192 g 1) (nest-Properties.nest-+ (f 0) 1 (1 + b)))!} \u27e9\n-}\n fold 1 (f 0) (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n f 1 (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) \u21912+\u27e8 suc n \u27e9 (1 + b)\n \u220e\n\n where\n f = \u03bb k b \u2192 (1 + a) \u21912+\u27e8 k + n \u27e9 b\n f1 = f 0 \u2218 suc\n\n -- fold1f2-infl< : Infl< (fold 1 f2)\n -- fold1f2-infl< = fold1-infl< f2 {!(\u03bb b \u2192 infl3< a n b ?)!}\n\n fold1f1-infl<' : Infl< (fold 1 f1)\n fold1f1-infl<' = {!fold1+1-infl< (f 0) {!infl3< a n!}!}\n lem : \u2200 x \u2192 f 0 (1 + x) < f 0 (f 0 x)\n lem x = {!!}\n postulate\n finfl : Infl< (f 0)\n open \u2264-Reasoning\n-}\n\n_\u21d1\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\na \u21d1\u27e8 n , k \u27e9 b = fold k f n\n module M\u21d1 where \n f : \u2115 \u2192 \u2115\n f x = a \u2191\u27e8 2 + x \u27e9 b\n\nmodule \u21d1-Props where\n _\u21d1\u2032\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\n a \u21d1\u2032\u27e8 n\u2080 , k \u27e9 b = g n\u2080\n module M\u21d1\u2032 where\n h : \u2115 \u2192 \u2115\n h x = a \u2191\u27e8 2 + x \u27e9 b\n\n g : \u2115 \u2192 \u2115\n g 0 = k\n g (suc n) = h (g n)\n\n g-nest : \u2200 n \u2192 g n \u2261 h $\u27e8 n \u27e9 k\n g-nest zero = \u2261.refl\n g-nest (suc n) rewrite g-nest n = \u2261.refl\n\n \u21d1\u2080 : \u2200 a k b \u2192 a \u21d1\u27e8 0 , k \u27e9 b \u2261 k\n \u21d1\u2080 _ _ _ = \u2261.refl\n\n \u21d1\u2081 : \u2200 a k b \u2192 a \u21d1\u27e8 1 , k \u27e9 b \u2261 a \u2191\u27e8 2 + k \u27e9 b\n \u21d1\u2081 _ _ _ = \u2261.refl\n\n \u21d1-\u21d1\u2032 : \u2200 a n k b \u2192 a \u21d1\u27e8 n , k \u27e9 b \u2261 a \u21d1\u2032\u27e8 n , k \u27e9 b\n \u21d1-\u21d1\u2032 a n k b rewrite M\u21d1\u2032.g-nest a n k b n = \u2261.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Graham%27s_number\nGraham's-number : \u2115\nGraham's-number = 3 \u21d1\u27e8 64 , 4 \u27e9 3\n -- = (f\u2218\u2076\u2074\u2026\u2218f)4\n -- where f x = 3 \u2191\u27e8 2 + x \u27e9 3\n\n{-\nmodule GeneralisedArithmetic {a} {A : Set a} (0# : A) (1+ : A \u2192 A) where\n\n 1# : A\n 1# = 1+ 0#\n\n open Data.Nat.GeneralisedArithmetic 0# 1+ public\n\n exp : (* : A \u2192 A \u2192 A) \u2192 (\u2115 \u2192 A \u2192 A)\n exp _*_ n b = fold 1# (\u03bb s \u2192 b * s) n\n-}\n -- hyperop a n b = fold exp\n\nmodule == where\n _\u2248_ : (m n : \u2115) \u2192 Set\n m \u2248 n = T (m == n)\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {zero} {zero} _ = id\n subst P {suc _} {suc _} p = subst (P \u2218 suc) p\n subst _ {zero} {suc _} ()\n subst _ {suc _} {zero} ()\n\n sound : \u2200 m n \u2192 T (m == n) \u2192 m \u2261 n\n sound m n p = subst (_\u2261_ m) p \u2261.refl\n\n refl : Reflexive _\u2248_\n refl {zero} = _\n refl {suc n} = refl {n}\n\n sym : Symmetric _\u2248_\n sym {m} {n} eq rewrite sound m n eq = refl {n}\n\n trans : Transitive _\u2248_\n trans {m} {n} {o} m\u2248n n\u2248o rewrite sound m n m\u2248n | sound n o n\u2248o = refl {o}\n\n setoid : Setoid _ _\n setoid = record { Carrier = \u2115; _\u2248_ = _\u2248_\n ; isEquivalence =\n record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} } }\n\n open Setoid setoid public hiding (refl; sym; trans; _\u2248_)\n\n{-\ndata _`\u2264?`_\u219d_ : (m n : \u2115) \u2192 Dec (m \u2264 n) \u2192 Set where\n z\u2264?n : \u2200 {n} \u2192 zero `\u2264?` n \u219d yes z\u2264n\n s\u2264?z : \u2200 {m} \u2192 suc m `\u2264?` zero \u219d no \u03bb()\n s\u2264?s-yes : \u2200 {m n m\u2264n} \u2192 m `\u2264?` n \u219d yes m\u2264n \u2192 suc m `\u2264?` suc n \u219d yes (s\u2264s m\u2264n)\n s\u2264?s-no : \u2200 {m n m\u2270n} \u2192 m `\u2264?` n \u219d no m\u2270n \u2192 suc m `\u2264?` suc n \u219d no (m\u2270n \u2218 \u2264-pred)\n\n`\u2264?`-complete : \u2200 m n \u2192 m `\u2264?` n \u219d (m \u2264? n)\n`\u2264?`-complete zero n = z\u2264?n\n`\u2264?`-complete (suc n) zero = {!s\u2264?z!}\n`\u2264?`-complete (suc m) (suc n) with m \u2264? n | `\u2264?`-complete m n\n... | yes q | r = s\u2264?s-yes r\n... | no q | r = s\u2264?s-no {!!}\n-}\n\n_<=_ : (x y : \u2115) \u2192 Bool\nzero <= _ = true\nsuc _ <= zero = false\nsuc m <= suc n = m <= n\n\nmodule <= where\n \u211b : \u2115 \u2192 \u2115 \u2192 Set\n \u211b x y = T (x <= y)\n\n sound : \u2200 m n \u2192 \u211b m n \u2192 m \u2264 n\n sound zero _ _ = z\u2264n\n sound (suc m) (suc n) p = s\u2264s (sound m n p)\n sound (suc m) zero ()\n\n complete : \u2200 {m n} \u2192 m \u2264 n \u2192 \u211b m n\n complete z\u2264n = _\n complete (s\u2264s m\u2264n) = complete m\u2264n\n\n isTotalOrder : IsTotalOrder _\u2261_ \u211b\n isTotalOrder = record { isPartialOrder = isPartialOrder; total = \u03bb x y \u2192 \u228e-map complete complete (\u2115\u2264.total x y) }\n where\n reflexive : \u2200 {i j} \u2192 i \u2261 j \u2192 \u211b i j\n reflexive {i} \u2261.refl = complete (\u2115\u2264.refl {i})\n trans : Transitive \u211b\n trans {x} {y} {z} p q = complete (\u2115\u2264.trans (sound x y p) (sound y z q))\n isPreorder : IsPreorder _\u2261_ \u211b\n isPreorder = record { isEquivalence = \u2261.isEquivalence\n ; reflexive = reflexive\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n antisym : Antisymmetric _\u2261_ \u211b\n antisym {x} {y} p q = \u2115\u2264.antisym (sound x y p) (sound y x q)\n isPartialOrder : IsPartialOrder _\u2261_ \u211b\n isPartialOrder = record { isPreorder = isPreorder; antisym = antisym }\n\n open IsTotalOrder isTotalOrder public\n\n<=-steps\u2032 : \u2200 {x} y \u2192 T (x <= (x + y))\n<=-steps\u2032 {x} y = <=.complete (\u2264-steps\u2032 {x} y)\n\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 : \u2200 x y \u2192 suc x \u2238 y \u2264 suc (x \u2238 y)\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x zero = \u2115\u2264.refl\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 zero (suc y) rewrite 0\u2238n\u22610 y = z\u2264n\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 (suc x) (suc y) = sucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x y\n\nx\u22642y\u2032\u2192x\u2238y\u2264y : \u2200 x y \u2192 x \u2264 2*\u2032 y \u2192 x \u2238 y \u2264 y\nx\u22642y\u2032\u2192x\u2238y\u2264y x zero p = p\nx\u22642y\u2032\u2192x\u2238y\u2264y zero (suc y) p = z\u2264n\nx\u22642y\u2032\u2192x\u2238y\u2264y (suc zero) (suc y) (s\u2264s p) rewrite 0\u2238n\u22610 y = z\u2264n\nx\u22642y\u2032\u2192x\u2238y\u2264y (suc (suc x)) (suc y) (s\u2264s (s\u2264s p))\n = \u2115\u2264.trans (sucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x y) (s\u2264s (x\u22642y\u2032\u2192x\u2238y\u2264y x y p))\n\nx<2y\u2032\u2192x\u2238y \u00aca \u00acb c = \u22a5-elim (p (\u2264-pred c))\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (Props.\u2264-steps 1 1+n\u2264m)\n\nnot<=\u2192< : \u2200 x y \u2192 T (not (x <= y)) \u2192 T (suc y <= x)\nnot<=\u2192< x y p = <=.complete (\u2270\u2192< x y (T'not'\u00ac p \u2218 <=.complete))\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Nat.NP where\n\nimport Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat public hiding (module GeneralisedArithmetic; module \u2264-Reasoning; fold)\nopen import Data.Nat.Properties as Props\nopen import Data.Nat.Logical\nopen import Data.Bool.NP hiding (_==_; module ==)\nopen import Data.Product using (proj\u2081; proj\u2082; \u2203; _,_)\nopen import Data.Sum renaming (map to \u228e-map)\nopen import Data.Empty using (\u22a5-elim; \u22a5)\nopen import Function.NP\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_; _\u2257_; module \u2261-Reasoning)\n\nmodule \u2115\u00b0 = Algebra.CommutativeSemiring Props.commutativeSemiring\nmodule \u2115cmp = StrictTotalOrder Props.strictTotalOrder\nmodule \u2115\u2264 = DecTotalOrder decTotalOrder\nmodule \u2294\u00b0 = Algebra.CommutativeSemiringWithoutOne \u2294-\u2293-0-commutativeSemiringWithoutOne\n\nmodule \u2264-Reasoning where\n open Preorder-Reasoning \u2115\u2264.preorder public renaming (_\u223c\u27e8_\u27e9_ to _\u2264\u27e8_\u27e9_)\n infixr 2 _\u2261\u27e8_\u27e9_\n _\u2261\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x \u2261 y \u2192 y \u2264 z \u2192 x \u2264 z\n _ \u2261\u27e8 \u2261.refl \u27e9 p = p\n infixr 2 _<\u27e8_\u27e9_\n _<\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x < y \u2192 y \u2264 z \u2192 x < z\n x <\u27e8 p \u27e9 q = suc x \u2264\u27e8 p \u27e9 q\n\nsuc-injective : \u2200 {n m : \u2115} \u2192 (suc n \u2236 \u2115) \u2261 suc m \u2192 n \u2261 m\nsuc-injective = \u2261.cong pred\n\n+-\u2264-inj : \u2200 x {y z} \u2192 x + y \u2264 x + z \u2192 y \u2264 z\n+-\u2264-inj zero p = p\n+-\u2264-inj (suc x) (s\u2264s p) = +-\u2264-inj x p\n\nsucx\u2270x : \u2200 x \u2192 suc x \u2270 x\nsucx\u2270x zero = \u03bb()\nsucx\u2270x (suc x) = sucx\u2270x x \u2218 \u2264-pred\n\n\u00acn\u2264x dist (x * y) (x * z) == x * dist y z))\n\ninfix 8 _^_\n_^_ : \u2115 \u2192 \u2115 \u2192 \u2115\nb ^ zero = 1\nb ^ suc n = b * b ^ n\n\n2^_ : \u2115 \u2192 \u2115\n2^ n = \u27e82^ n * 1 \u27e9\n\n2^-spec : \u2200 n \u2192 2^ n \u2261 2 ^ n\n2^-spec zero = \u2261.refl\n2^-spec (suc n) rewrite 2^-spec n | 2*-spec (2 ^ n) = \u2261.refl\n\n2^*-spec : \u2200 m n \u2192 2^\u27e8 m \u27e9* n \u2261 2 ^ m * n\n2^*-spec zero n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\n2^*-spec (suc m) n rewrite 2^*-spec m n\n | \u2115\u00b0.*-assoc 2 (2 ^ m) n\n | \u2115\u00b0.+-comm (2 ^ m * n) 0 = \u2261.refl\n\n1\u22642^ : \u2200 n \u2192 2^ n \u2265 1\n1+\u22642^ : \u2200 n \u2192 2^ n \u2265 1 + n\n1+\u22642^ zero = s\u2264s z\u2264n\n1+\u22642^ (suc n) = (1\u22642^ n) +-mono (1+\u22642^ n)\n\n1\u22642^ n = \u2115\u2264.trans (s\u2264s z\u2264n) (1+\u22642^ n)\n\n\u2264-steps\u2032 : \u2200 {x} y \u2192 x \u2264 x + y\n\u2264-steps\u2032 {x} y rewrite \u2115\u00b0.+-comm x y = \u2264-steps y \u2115\u2264.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Hyper_operator\n_\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\na \u2191\u27e8 suc n \u27e9 (suc b) = a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\na \u2191\u27e8 0 \u27e9 b = suc b\na \u2191\u27e8 1 \u27e9 0 = a\na \u2191\u27e8 2 \u27e9 0 = 0\na \u2191\u27e8 suc (suc (suc n)) \u27e9 0 = 1\n\nmodule \u2191-Props where\n \u2191-fold : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 b \u2261 fold (a \u2191\u27e8 suc n \u27e9 0) (_\u2191\u27e8_\u27e9_ a n) b\n \u2191-fold a n zero = \u2261.refl\n \u2191-fold a n (suc b) = \u2261.cong (_\u2191\u27e8_\u27e9_ a n) (\u2191-fold a n b)\n\n \u2191\u2080-+ : \u2200 a b \u2192 a \u2191\u27e8 0 \u27e9 b \u2261 1 + b\n \u2191\u2080-+ a b = \u2261.refl\n\n \u2191\u2081-+ : \u2200 a b \u2192 a \u2191\u27e8 1 \u27e9 b \u2261 b + a\n \u2191\u2081-+ a zero = \u2261.refl\n \u2191\u2081-+ a (suc b) = \u2261.cong suc (\u2191\u2081-+ a b)\n\n \u2191\u2082-* : \u2200 a b \u2192 a \u2191\u27e8 2 \u27e9 b \u2261 b * a\n \u2191\u2082-* a zero = \u2261.refl\n \u2191\u2082-* a (suc b) rewrite \u2191\u2082-* a b\n | \u2191\u2081-+ a (b * a)\n | \u2115\u00b0.+-comm (b * a) a\n = \u2261.refl\n\n -- fold 1 (_*_ a) b \u2261 a ^ b\n \u2191\u2083-^ : \u2200 a b \u2192 a \u2191\u27e8 3 \u27e9 b \u2261 a ^ b\n \u2191\u2083-^ a zero = \u2261.refl\n \u2191\u2083-^ a (suc b) rewrite \u2191\u2083-^ a b\n | \u2191\u2082-* a (a ^ b)\n | \u2115\u00b0.*-comm (a ^ b) a\n = \u2261.refl\n\n _\u2191\u27e8_\u27e90 : \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u27e8 0 \u27e90 = 1\n a \u2191\u27e8 1 \u27e90 = a\n a \u2191\u27e8 2 \u27e90 = 0\n a \u2191\u27e8 suc (suc (suc n)) \u27e90 = 1\n\n _`\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _`\u2191\u27e8_\u27e9_ a 0 = suc\n _`\u2191\u27e8_\u27e9_ a (suc n) = fold (a \u2191\u27e8 suc n \u27e90) (_`\u2191\u27e8_\u27e9_ a n)\n\n \u2191-ind-rule : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 (suc b) \u2261 a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\n \u2191-ind-rule a n b = \u2261.refl\n\n _\u2191\u2032\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u2032\u27e8 0 \u27e9 b = suc b\n a \u2191\u2032\u27e8 1 \u27e9 b = a + b\n a \u2191\u2032\u27e8 2 \u27e9 b = a * b\n a \u2191\u2032\u27e8 3 \u27e9 b = a ^ b\n a \u2191\u2032\u27e8 suc (suc (suc (suc n))) \u27e9 b = a \u2191\u27e8 4 + n \u27e9 b\n\n \u2191-\u2191\u2032 : \u2200 a n b \u2192 a \u2191\u27e8 n \u27e9 b \u2261 a \u2191\u2032\u27e8 n \u27e9 b\n \u2191-\u2191\u2032 a 0 b = \u2261.refl\n \u2191-\u2191\u2032 a 1 b = \u2261.trans (\u2191\u2081-+ a b) (\u2115\u00b0.+-comm b a)\n \u2191-\u2191\u2032 a 2 b = \u2261.trans (\u2191\u2082-* a b) (\u2115\u00b0.*-comm b a)\n \u2191-\u2191\u2032 a 3 b = \u2191\u2083-^ a b\n \u2191-\u2191\u2032 a (suc (suc (suc (suc _)))) b = \u2261.refl\n\n _\u21912+\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _\u21912+\u27e8_\u27e9_ a = fold (_*_ a) (fold 1)\n\nmodule InflModule where\n -- Inflationary functions\n Infl< : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl< f = \u2200 {x} \u2192 x < f x\n\n Infl : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl f = Infl< (suc \u2218 f)\n\n InflT< : (f : Endo (Endo \u2115)) \u2192 Set\n InflT< f = \u2200 {g} \u2192 Infl< g \u2192 Infl< (f g)\n\n{-\n\u2200 {x} \u2192 x \u2264 f x\n\n\n 1 + x \u2264 1 + f x\n x < 1 + f x\n x < (suc \u2218 f) x\n Infl< (suc \u2218 f)\n\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n\nInfl< f \u2192 Infl f\nInfl< f \u2192 Infl< (suc \u2218 f)\n-}\n\n id-infl : Infl id\n id-infl = \u2115\u2264.refl\n\n \u2218-infl< : \u2200 {f g} \u2192 Infl< f \u2192 Infl< g \u2192 Infl< (f \u2218 g)\n \u2218-infl< inflf< inflg< = <-trans inflg< inflf<\n\n suc-infl< : Infl< suc\n suc-infl< = \u2115\u2264.refl\n\n suc-infl : Infl suc\n suc-infl = s\u2264s (\u2264-step \u2115\u2264.refl)\n\n nest-infl : \u2200 f (finfl : Infl f) n \u2192 Infl (nest n f)\n nest-infl f _ zero = \u2115\u2264.refl\n nest-infl f finfl (suc n) = \u2115\u2264.trans (nest-infl f finfl n) finfl\n\n nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n nest-infl< f finfl< zero = finfl<\n nest-infl< f finfl< (suc n) = <-trans (nest-infl< f finfl< n) finfl< \n\n-- See Function.NP.nest-Properties for more properties\nmodule fold-Props where\n\n fold-suc : \u2200 m n \u2192 fold m suc n \u2261 n + m\n fold-suc m zero = \u2261.refl\n fold-suc m (suc n) rewrite fold-suc m n = \u2261.refl\n\n fold-+ : \u2200 m n \u2192 fold 0 (_+_ m) n \u2261 n * m\n fold-+ m zero = \u2261.refl\n fold-+ m (suc n) rewrite fold-+ m n = \u2261.refl\n\n fold-* : \u2200 m n \u2192 fold 1 (_*_ m) n \u2261 m ^ n\n fold-* m zero = \u2261.refl\n fold-* m (suc n) rewrite fold-* m n = \u2261.refl\n\n{- TODO\n fold-^ : \u2200 m n \u2192 fold 1 (flip _^_ m) n \u2261 m \u2191\u27e8 4 \u27e9 n\n fold-^ m zero = \u2261.refl\n fold-^ m (suc n) rewrite fold-^ m n = ?\n-}\n\n cong-fold : \u2200 {A : Set} {f g : Endo A} (f\u2257g : f \u2257 g) {z} \u2192 fold z f \u2257 fold z g\n cong-fold eq zero = \u2261.refl\n cong-fold eq {z} (suc x) rewrite cong-fold eq {z} x = eq _\n\n private\n open \u2191-Props\n module Alt\u21912 where\n alt : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n alt a 0 = \u03bb b \u2192 a \u2191\u27e8 2 \u27e9 b\n alt a (suc n) = fold 1 (alt a n)\n\n alt-ok : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 alt a n\n alt-ok a zero = \u2261.sym \u2218 \u2191-\u2191\u2032 a 2\n alt-ok a (suc n) = cong-fold (alt-ok a n)\n\n{- TODO\n alt' : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 _\u2191\u27e8_\u27e9_ a (2 + n)\n alt' a zero b = \u2261.sym (\u2191-\u2191\u2032 a 2 _)\n alt' a (suc n) zero = \u2261.refl\n alt' a (suc n) (suc x) rewrite alt' a n (_\u21912+\u27e8_\u27e9_ a (suc n) x)\n = \u2261.cong (_\u2191\u27e8_\u27e9_ a (2 + n)) { fold 1 (fold (_*_ a) (fold 1) n) x} {a \u2191\u27e8 suc (suc (suc n)) \u27e9 x} (alt' a (suc n) x)\n-}\n\n open InflModule\n{-\n fold1-infl : \u2200 f \u2192 Infl f \u2192 Infl (fold 1 f)\n fold1-infl f finfl {zero} = {!z\u2264n!}\n fold1-infl f finfl {suc x} =\n x <\u27e8 {!s\u2264s (fold1-infl f finfl {x})!} \u27e9\n fold 1 f x <\u27e8 {!!} \u27e9\n {- f (fold 1 f x) \u2264\u27e8 finfl \u27e9\n f (fold 1 f x) \u2261\u27e8 \u2261.refl \u27e9 -}\n fold 1 f (1 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 f \u2218 suc)\n fold1+1-infl< f finfl< {zero} = finfl<\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + f (fold 1 f x) \u2264\u27e8 finfl< {f (fold 1 f x)} \u27e9\n f (suc (f (fold 1 f x))) \u2264\u27e8 {!!} \u27e9\n fold 1 f (2 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 (f \u2218 suc))\n fold1+1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 suc) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 suc) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+-infl< : \u2200 k f \u2192 Infl< (f \u2218\u2032 _+_ k) \u2192 Infl< (fold 1 (f \u2218\u2032 _+_ k))\n fold1+-infl< k f finfl< {zero} = s\u2264s z\u2264n\n fold1+-infl< k f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-infl< k f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 _+_ k) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 _+_ k) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n \n{-\n fold1+-infl< : \u2200 f \u2192 Infl+2< f \u2192 Infl+2< (fold 1 f)\n fold1+-infl< f finfl< {zero} _ = \u2115\u2264.refl\n fold1+-infl< f finfl< {suc zero} _ = {!!}\n fold1+-infl< f finfl< {suc (suc zero)} _ = {!!}\n fold1+-infl< f finfl< {suc (suc (suc x))} x>1 =\n 4 + x \u2264\u27e8 s\u2264s (fold1+-infl< f finfl< {suc (suc x)} (m\u2264m+n 2 x)) \u27e9\n 1 + fold 1 f (2 + x) \u2264\u27e8 finfl< {!!} \u27e9\n fold 1 f (3 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 f x \u2264\u27e8 finfl< \u27e9\n fold 1 f (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\nmodule ^-Props where\n open \u2261-Reasoning\n\n ^1 : \u2200 n \u2192 n ^ 1 \u2261 n\n ^1 zero = \u2261.refl\n ^1 (suc n) = \u2261.cong suc (proj\u2082 \u2115\u00b0.*-identity n)\n\n ^-+ : \u2200 b m n \u2192 b ^ (m + n) \u2261 b ^ m * b ^ n\n ^-+ b zero n = \u2261.sym (proj\u2082 \u2115\u00b0.+-identity (b ^ n))\n ^-+ b (suc m) n rewrite ^-+ b m n = \u2261.sym (\u2115\u00b0.*-assoc b (b ^ m) (b ^ n))\n\n ^-* : \u2200 b m n \u2192 b ^ (m * n) \u2261 (b ^ n) ^ m\n ^-* b zero n = \u2261.refl\n ^-* b (suc m) n = b ^ (n + m * n)\n \u2261\u27e8 ^-+ b n (m * n) \u27e9\n b ^ n * b ^ (m * n)\n \u2261\u27e8 \u2261.cong (_*_ (b ^ n)) (^-* b m n) \u27e9\n b ^ n * (b ^ n) ^ m \u220e\n\nack : \u2115 \u2192 \u2115 \u2192 \u2115\nack zero n = suc n\nack (suc m) zero = ack m (suc zero)\nack (suc m) (suc n) = ack m (ack (suc m) n)\n\n\u2264\u21d2\u2203 : \u2200 {m n} \u2192 m \u2264 n \u2192 \u2203 \u03bb k \u2192 m + k \u2261 n\n\u2264\u21d2\u2203 z\u2264n = _ , \u2261.refl\n\u2264\u21d2\u2203 (s\u2264s pf) = _ , \u2261.cong suc (proj\u2082 (\u2264\u21d2\u2203 pf))\n\nmodule ack-Props where\n lem\u2238 : \u2200 {m n} \u2192 m \u2264 n \u2192 m + (n \u2238 m) \u2261 n\n lem\u2238 z\u2264n = \u2261.refl\n lem\u2238 (s\u2264s {m} {n} m\u2264n) = \u2261.cong suc (lem\u2238 m\u2264n)\n\n -- n >= m \u2192 m + (n \u2238 m) \u2261 n\n -- n >= m \u2192 \u2203 k \u2192 n \u2261 m + k\n -- \u2203 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 m + ((m + k) \u2238 m) \u2261 m + k\n -- \u2200 k \u2192 m + k \u2261 m + k\n\n -- 3 \u2264 x \u2192 P x \u2192 \u2203 \u03bb k \u2192 P (3 + k)\n\n open InflModule\n\n fold1+-inflT< : \u2200 {z} \u2192 InflT< (fold (suc z))\n fold1+-inflT< _ {zero} = s\u2264s z\u2264n\n fold1+-inflT< {z} {f} finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-inflT< {z} {f} finfl< {x}) \u27e9\n 1 + fold (suc z) f x \u2264\u27e8 finfl< \u27e9\n f (fold (suc z) f x) \u2261\u27e8 \u2261.refl \u27e9\n fold (suc z) f (1 + x)\n \u220e\n where open \u2264-Reasoning\n\n Mon : (f : \u2115 \u2192 \u2115) \u2192 Set\n Mon f = \u2200 {x y} \u2192 x \u2264 y \u2192 f x \u2264 f y\n open InflModule\n\n\n \u2115-ind : \u2200 (P : \u2115 \u2192 Set) \u2192 P zero \u2192 (\u2200 {n} \u2192 P n \u2192 P (suc n)) \u2192 \u2200 {n} \u2192 P n\n \u2115-ind P P0 PS {zero} = P0\n \u2115-ind P P0 PS {suc n} = PS (\u2115-ind P P0 PS)\n\n open fold-Props\n fold-infl< : \u2200 {f g} \u2192 Infl< f \u2192 InflT< g \u2192 \u2200 {n} \u2192 Infl< (fold f g n)\n fold-infl< {f} {g} inflf< inflTg< {n} = \u2115-ind (Infl< \u2218 fold f g) inflf< inflTg< {n}\n\n fold-mon : \u2200 {f} (fmon : Mon f) (finfl< : Infl< f) {z} \u2192 Mon (fold z f)\n fold-mon fmon finfl< {_} {y = 0} z\u2264n = \u2115\u2264.refl\n fold-mon {f} fmon finfl< {z} {y = suc n} z\u2264n = z \u2264\u27e8 \u2264-step \u2115\u2264.refl \u27e9\n suc z \u2264\u27e8 nest-infl< f finfl< n {z} \u27e9\n fold z f (suc n) \u2261\u27e8 \u2261.refl \u27e9\n fold z f (suc n) \u220e\n where open \u2264-Reasoning\n fold-mon fmon finfl< (s\u2264s m\u2264n) = fmon (fold-mon fmon finfl< m\u2264n)\n\n MonT : Endo (Endo \u2115) \u2192 Set\n MonT g = \u2200 {f} \u2192 Mon f \u2192 Infl< f \u2192 Mon (g f)\n\n fold-mon' : \u2200 {f g} \u2192 Mon f \u2192 Infl< f \u2192 MonT g \u2192 InflT< g \u2192 \u2200 {n} \u2192 Mon (fold f g n)\n fold-mon' {f} {g} mon-f infl-f mont-g inflTg< {n} = go n where\n go : \u2200 n \u2192 Mon (fold f g n)\n go zero = mon-f\n go (suc n) = mont-g (go n) (fold-infl< infl-f inflTg< {n})\n\n\n -- +-fold : \u2200 a b \u2192 a + b \u2261 fold a suc b\n -- *-fold : \u2200 a b \u2192 a * b \u2261 fold 0 (_+_ a) b\n\n 1\u22641+a^b : \u2200 a b \u2192 1 \u2264 (1 + a) ^ b\n 1\u22641+a^b a zero = s\u2264s z\u2264n\n 1\u22641+a^b a (suc b) = 1\u22641+a^b a b +-mono z\u2264n\n\n 1+a^-mon : \u2200 {a} \u2192 Mon (_^_ (1 + a))\n 1+a^-mon {a} (z\u2264n {b}) = 1\u22641+a^b a b\n 1+a^-mon {a} (s\u2264s {m} {n} m\u2264n)\n = (1 + a) ^ (1 + m) \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ m + a * (1 + a) ^ m \u2264\u27e8 (1+a^-mon m\u2264n) +-mono ((a \u220e) *-mono (1+a^-mon m\u2264n)) \u27e9\n (1 + a) ^ n + a * (1 + a) ^ n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ (1 + n) \u220e\n where open \u2264-Reasoning\n\n lem2^3 : \u2200 n \u2192 2 ^ 3 \u2264 2 ^ (3 + n)\n lem2^3 n = 1+a^-mon {1} {3} {3 + n} (s\u2264s (s\u2264s (s\u2264s z\u2264n)))\n\n lem2221 : \u2200 m \u2192 2 \u2261 2 \u2191\u27e8 2 + m \u27e9 1\n lem2221 zero = \u2261.refl\n lem2221 (suc m) = lem2221 m\n\n lem4212 : \u2200 m \u2192 4 \u2261 2 \u2191\u27e8 1 + m \u27e9 2\n lem4212 zero = \u2261.refl\n lem4212 (suc m) = 4 \u2261\u27e8 lem4212 m \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 2 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 (1 + m)) (lem2221 m) \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 (2 \u2191\u27e8 2 + m \u27e9 1) \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 2 + m \u27e9 2 \u220e\n where open \u2261-Reasoning\n\n 2* = _*_ 2\n\n -- fold 2* n 1 \u2261 2 ^ n\n\n -- nest-* : \u2200 m n \u2192 nest (m * n) f \u2257 nest m (nest n f)\n\n nest-2* : \u2200 m n \u2192 nest (m * n) (fold 1) 2* \u2261 nest m (nest n (fold 1)) 2*\n nest-2* m n = nest-Properties.nest-* (fold 1) m n 2*\n\n{-\n lem3\u2264 : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 2\n lem3\u2264 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264 (suc n) = \n 3 \u2264\u27e8 {!!} \u27e9\n fold 2* f1 (1 + n) 2 \u220e\n where open \u2264-Reasoning\n f1 = fold 1\n\n lem3\u2264' : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 3\n lem3\u2264' 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264' (suc n) = \n 3 \u2264\u27e8 lem3\u2264 n \u27e9\n fold 2* (fold 1) n 2 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) n (fold 2* (fold 1) 3 1) \u2261\u27e8 {!!} \u27e9\n{- \u2261\u27e8 nest-Properties.nest-* {!2*!} {!!} {!!} {!!} \u27e9\n fold 2* (fold 1) (3 * n) 1 \u2261\u27e8 {!!} \u27e9\n-}\n fold 1 (fold 2* (fold 1) n) 3 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) (1 + n) 3 \u220e\n where open \u2264-Reasoning\n-}\n---\n\n{-\nfold 2* (fold 1) (3 * n) 1 \u2264\n\nfold 2* (fold 1) n (fold 2* (fold 1) n (fold 2* (fold 1) n 1)) \u2264\n\nfold 1 (fold 2* (fold 1) n) 3 \u2264\nfold 2* (fold 1) (1 + n) 3\n-}\n\n{-\n open \u2191-Props\n\n -*\u27e81+n\u27e9-infl : \u2200 n \u2192 Infl (_*_ (1 + n))\n -*\u27e81+n\u27e9-infl n = {!!}\n\n lem121 : \u2200 a b \u2192 b < (1 + a) \u2191\u27e8 2 \u27e9 (1 + b)\n lem121 a b rewrite \u2191\u2082-* (1 + a) (1 + b)\n = 1 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (1 + a) * (1 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (1 + a) (1 + b) \u27e9\n (1 + b) * (1 + a)\n \u220e\n where open \u2264-Reasoning\n\n -- \u2200 a b \u2192 b < (2 + a) * b\n lem222 : \u2200 a b \u2192 b < (2 + a) \u2191\u27e8 2 \u27e9 (2 + b)\n lem222 a b rewrite \u2191\u2082-* (2 + a) (2 + b)\n = 1 + b\n \u2264\u27e8 suc-infl \u27e9\n 2 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (2 + a) * (2 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (2 + a) (2 + b) \u27e9\n (2 + b) * (2 + a)\n \u220e\n where open \u2264-Reasoning\n-}\n{-\n open fold-Props\n\n module Foo a where\n f = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b\n f-lem : \u2200 n \u2192 f (suc n) \u2257 fold 1 (f n)\n f-lem n x = \u2261.refl\n\n f2 = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n f2-lem : \u2200 n \u2192 f2 (suc n) \u2257 fold 1 (f2 n)\n f2-lem n x = {!!}\n\n -- b < (1 + a) * (1 + b)\n\n -- nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n infl3< : \u2200 a n \u2192 Infl< (\u03bb b \u2192 (1 + a) \u21912+\u27e8 n \u27e9 (1 + b))\n infl3< a zero {b} = lem121 a b\n infl3< a (suc n) {b}\n = 1 + b\n -- fold1-infl< : \u2200 f \u2192 Infl< f \u2192 \u2200 x \u2192 x < fold 1 f x\n \u2264\u27e8 {!fold1f2-infl< {_}!} \u27e9\n fold 1 f1 b\n \u2264\u27e8 {!!} \u27e9\n{-\n \u2264\u27e8 fold1-infl< (f 0) (\u03bb {x} \u2192 {!infl3< a n {x}!}) {{!!}} \u27e9\n {!!}\n \u2264\u27e8 {!!} \u27e9\n-}\n{-\n 1 + b\n (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n \u2264\u27e8 {!nest-infl< (\u03bb b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b) ? b {(2 + a) \u21912+\u27e8 n \u27e9 (2 + b)}!} \u27e9\n fold (f 0 1) (f 0) (1 + b)\n \u2264\u27e8 {!\u2115\u2264.reflexive (\u2261.cong (\u03bb g \u2192 g 1) (nest-Properties.nest-+ (f 0) 1 (1 + b)))!} \u27e9\n-}\n fold 1 (f 0) (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n f 1 (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) \u21912+\u27e8 suc n \u27e9 (1 + b)\n \u220e\n\n where\n f = \u03bb k b \u2192 (1 + a) \u21912+\u27e8 k + n \u27e9 b\n f1 = f 0 \u2218 suc\n\n -- fold1f2-infl< : Infl< (fold 1 f2)\n -- fold1f2-infl< = fold1-infl< f2 {!(\u03bb b \u2192 infl3< a n b ?)!}\n\n fold1f1-infl<' : Infl< (fold 1 f1)\n fold1f1-infl<' = {!fold1+1-infl< (f 0) {!infl3< a n!}!}\n lem : \u2200 x \u2192 f 0 (1 + x) < f 0 (f 0 x)\n lem x = {!!}\n postulate\n finfl : Infl< (f 0)\n open \u2264-Reasoning\n-}\n\n_\u21d1\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\na \u21d1\u27e8 n , k \u27e9 b = fold k f n\n module M\u21d1 where \n f : \u2115 \u2192 \u2115\n f x = a \u2191\u27e8 2 + x \u27e9 b\n\nmodule \u21d1-Props where\n _\u21d1\u2032\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\n a \u21d1\u2032\u27e8 n\u2080 , k \u27e9 b = g n\u2080\n module M\u21d1\u2032 where\n h : \u2115 \u2192 \u2115\n h x = a \u2191\u27e8 2 + x \u27e9 b\n\n g : \u2115 \u2192 \u2115\n g 0 = k\n g (suc n) = h (g n)\n\n g-nest : \u2200 n \u2192 g n \u2261 h $\u27e8 n \u27e9 k\n g-nest zero = \u2261.refl\n g-nest (suc n) rewrite g-nest n = \u2261.refl\n\n \u21d1\u2080 : \u2200 a k b \u2192 a \u21d1\u27e8 0 , k \u27e9 b \u2261 k\n \u21d1\u2080 _ _ _ = \u2261.refl\n\n \u21d1\u2081 : \u2200 a k b \u2192 a \u21d1\u27e8 1 , k \u27e9 b \u2261 a \u2191\u27e8 2 + k \u27e9 b\n \u21d1\u2081 _ _ _ = \u2261.refl\n\n \u21d1-\u21d1\u2032 : \u2200 a n k b \u2192 a \u21d1\u27e8 n , k \u27e9 b \u2261 a \u21d1\u2032\u27e8 n , k \u27e9 b\n \u21d1-\u21d1\u2032 a n k b rewrite M\u21d1\u2032.g-nest a n k b n = \u2261.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Graham%27s_number\nGraham's-number : \u2115\nGraham's-number = 3 \u21d1\u27e8 64 , 4 \u27e9 3\n -- = (f\u2218\u2076\u2074\u2026\u2218f)4\n -- where f x = 3 \u2191\u27e8 2 + x \u27e9 3\n\n{-\nmodule GeneralisedArithmetic {a} {A : Set a} (0# : A) (1+ : A \u2192 A) where\n\n 1# : A\n 1# = 1+ 0#\n\n open Data.Nat.GeneralisedArithmetic 0# 1+ public\n\n exp : (* : A \u2192 A \u2192 A) \u2192 (\u2115 \u2192 A \u2192 A)\n exp _*_ n b = fold 1# (\u03bb s \u2192 b * s) n\n-}\n -- hyperop a n b = fold exp\n\nmodule == where\n _\u2248_ : (m n : \u2115) \u2192 Set\n m \u2248 n = T (m == n)\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {zero} {zero} _ = id\n subst P {suc _} {suc _} p = subst (P \u2218 suc) p\n subst _ {zero} {suc _} ()\n subst _ {suc _} {zero} ()\n\n sound : \u2200 m n \u2192 T (m == n) \u2192 m \u2261 n\n sound m n p = subst (_\u2261_ m) p \u2261.refl\n\n refl : Reflexive _\u2248_\n refl {zero} = _\n refl {suc n} = refl {n}\n\n sym : Symmetric _\u2248_\n sym {m} {n} eq rewrite sound m n eq = refl {n}\n\n trans : Transitive _\u2248_\n trans {m} {n} {o} m\u2248n n\u2248o rewrite sound m n m\u2248n | sound n o n\u2248o = refl {o}\n\n setoid : Setoid _ _\n setoid = record { Carrier = \u2115; _\u2248_ = _\u2248_\n ; isEquivalence =\n record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} } }\n\n open Setoid setoid public hiding (refl; sym; trans; _\u2248_)\n\n{-\ndata _`\u2264?`_\u219d_ : (m n : \u2115) \u2192 Dec (m \u2264 n) \u2192 Set where\n z\u2264?n : \u2200 {n} \u2192 zero `\u2264?` n \u219d yes z\u2264n\n s\u2264?z : \u2200 {m} \u2192 suc m `\u2264?` zero \u219d no \u03bb()\n s\u2264?s-yes : \u2200 {m n m\u2264n} \u2192 m `\u2264?` n \u219d yes m\u2264n \u2192 suc m `\u2264?` suc n \u219d yes (s\u2264s m\u2264n)\n s\u2264?s-no : \u2200 {m n m\u2270n} \u2192 m `\u2264?` n \u219d no m\u2270n \u2192 suc m `\u2264?` suc n \u219d no (m\u2270n \u2218 \u2264-pred)\n\n`\u2264?`-complete : \u2200 m n \u2192 m `\u2264?` n \u219d (m \u2264? n)\n`\u2264?`-complete zero n = z\u2264?n\n`\u2264?`-complete (suc n) zero = {!s\u2264?z!}\n`\u2264?`-complete (suc m) (suc n) with m \u2264? n | `\u2264?`-complete m n\n... | yes q | r = s\u2264?s-yes r\n... | no q | r = s\u2264?s-no {!!}\n-}\n\n_<=_ : (x y : \u2115) \u2192 Bool\nzero <= _ = true\nsuc _ <= zero = false\nsuc m <= suc n = m <= n\n\nmodule <= where\n \u211b : \u2115 \u2192 \u2115 \u2192 Set\n \u211b x y = T (x <= y)\n\n sound : \u2200 m n \u2192 \u211b m n \u2192 m \u2264 n\n sound zero _ _ = z\u2264n\n sound (suc m) (suc n) p = s\u2264s (sound m n p)\n sound (suc m) zero ()\n\n complete : \u2200 {m n} \u2192 m \u2264 n \u2192 \u211b m n\n complete z\u2264n = _\n complete (s\u2264s m\u2264n) = complete m\u2264n\n\n isTotalOrder : IsTotalOrder _\u2261_ \u211b\n isTotalOrder = record { isPartialOrder = isPartialOrder; total = \u03bb x y \u2192 \u228e-map complete complete (\u2115\u2264.total x y) }\n where\n reflexive : \u2200 {i j} \u2192 i \u2261 j \u2192 \u211b i j\n reflexive {i} \u2261.refl = complete (\u2115\u2264.refl {i})\n trans : Transitive \u211b\n trans {x} {y} {z} p q = complete (\u2115\u2264.trans (sound x y p) (sound y z q))\n isPreorder : IsPreorder _\u2261_ \u211b\n isPreorder = record { isEquivalence = \u2261.isEquivalence\n ; reflexive = reflexive\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n antisym : Antisymmetric _\u2261_ \u211b\n antisym {x} {y} p q = \u2115\u2264.antisym (sound x y p) (sound y x q)\n isPartialOrder : IsPartialOrder _\u2261_ \u211b\n isPartialOrder = record { isPreorder = isPreorder; antisym = antisym }\n\n open IsTotalOrder isTotalOrder public\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (Props.\u2264-steps 1 1+n\u2264m)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"0cd9ddebb8cf75d4df2bc04ee0c195c0587029af","subject":"Missing cosmetic change.","message":"Missing cosmetic change.\n\nIgnore-this: 6a19bce792411fcc36f0fc4e790efa77\n\ndarcs-hash:20110728171007-3bd4e-be138098f352695feefb52c5b0e2ae5403c7deea.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Data\/Nat\/List\/Type.agda","new_file":"src\/FOTC\/Data\/Nat\/List\/Type.agda","new_contents":"----------------------------------------------------------------------------\n-- The FOTC list of natural numbers type\n----------------------------------------------------------------------------\n\nmodule FOTC.Data.Nat.List.Type where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Nat.Type\n\n------------------------------------------------------------------------------\n-- The FOTC list of natural numbers type.\ndata ListN : D \u2192 Set where\n nilLN : ListN []\n consLN : \u2200 {n ns} \u2192 N n \u2192 ListN ns \u2192 ListN (n \u2237 ns)\n{-# ATP axiom nilLN #-}\n{-# ATP axiom consLN #-}\n","old_contents":"----------------------------------------------------------------------------\n-- The FOTC list of natural numbers type\n----------------------------------------------------------------------------\n\nmodule FOTC.Data.Nat.List.Type where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Nat.Type\n\n------------------------------------------------------------------------------\n-- The FOTC list of natural numbers type.\ndata ListN : D \u2192 Set where\n nilLN : ListN []\n consLN : \u2200 {n ns} \u2192 N n \u2192 (LNns : ListN ns) \u2192 ListN (n \u2237 ns)\n{-# ATP axiom nilLN #-}\n{-# ATP axiom consLN #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3376865b1a5c84c532cfba6c4ae34e4e90a0965f","subject":"Disable option '--universal-quantified-functions'.","message":"Disable option '--universal-quantified-functions'.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/LambdaLifting.agda","new_file":"notes\/LambdaLifting.agda","new_contents":"","old_contents":"------------------------------------------------------------------------------\n-- Example of lambda-lifting\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --universal-quantified-functions #-}\n{-# OPTIONS --without-K #-}\n\nmodule LambdaLifting where\n\ninfixl 9 _\u00b7_\ninfix 8 if_then_else_\ninfix 7 _\u2261_\n\ninfixl 10 _*_\ninfixl 9 _+_\n\n------------------------------------------------------------------------------\n\npostulate\n D : Set\n zero true false : D\n succ iszero pred : D \u2192 D\n _\u00b7_ : D \u2192 D \u2192 D\n if_then_else_ : D \u2192 D \u2192 D \u2192 D\n lam fix : (D \u2192 D) \u2192 D\n\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n-- Conversion rules\n\n-- Conversion rules for Booleans.\npostulate\n if-true : \u2200 d\u2081 {d\u2082} \u2192 if true then d\u2081 else d\u2082 \u2261 d\u2081\n if-false : \u2200 {d\u2081} d\u2082 \u2192 if false then d\u2081 else d\u2082 \u2261 d\u2082\n{-# ATP axiom if-true if-false #-}\n\n-- Conversion rules for pred.\npostulate\n -- N.B. We don't need this equation.\n -- pred-0 : pred zero \u2261 zero\n pred-S : \u2200 d \u2192 pred (succ d) \u2261 d\n{-# ATP axiom pred-S #-}\n\n-- Conversion rules for iszero.\npostulate\n iszero-0 : iszero zero \u2261 true\n iszero-S : \u2200 d \u2192 iszero (succ d) \u2261 false\n{-# ATP axiom iszero-0 iszero-S #-}\n\n-- Conversion rule for the \u03bb-abstraction and the application.\npostulate beta : \u2200 f a \u2192 lam f \u00b7 a \u2261 f a\n{-# ATP axiom beta #-}\n\n-- Conversion rule for the fixed-pointed operator.\npostulate fix-eq : \u2200 f \u2192 fix f \u2261 f (fix f)\n{-# ATP axiom fix-eq #-}\n\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ d + e \u2261 succ (d + e)\n{-# ATP axiom +-0x +-Sx #-}\n\npostulate\n _*_ : D \u2192 D \u2192 D\n *-0x : \u2200 d \u2192 zero * d \u2261 zero\n *-Sx : \u2200 d e \u2192 succ d * e \u2261 e + d * e\n{-# ATP axiom *-0x *-Sx #-}\n\n------------------------------------------------------------------------------\n-- The original fach\n-- fach : D \u2192 D\n-- fach f = lam (\u03bb n \u2192 if (iszero n) then (succ zero) else n * (f \u00b7 (pred n)))\n\n-- Lambda-lifting via super-combinators (Hughes. Super-combinators. 1982).\n\n\u03b1 : D \u2192 D \u2192 D\n\u03b1 f n = if (iszero n) then (succ zero) else n * (f \u00b7 (pred n))\n{-# ATP definition \u03b1 #-}\n\nfach : D \u2192 D\nfach f = lam (\u03b1 f)\n{-# ATP definition fach #-}\n\nfac : D \u2192 D\nfac n = fix fach \u00b7 n\n{-# ATP definition fac #-}\n\npostulate fac0 : fac zero \u2261 succ zero\n{-# ATP prove fac0 #-}\n\npostulate fac1 : fac (succ zero) \u2261 succ zero\n{-# ATP prove fac1 #-}\n\npostulate fac2 : fac (succ (succ zero)) \u2261 succ (succ zero)\n{-# ATP prove fac2 #-}\n\n------------------------------------------------------------------------------\n-- Ouput:\n--\n-- $ agda2atp -inotes --non-fol-function notes\/LambdaLifting.agda\n-- Proving the conjecture in \/tmp\/LambdaLifting\/95-fac1.tptp ...\n-- E 1.6 Tiger Hill proved the conjecture in \/tmp\/LambdaLifting\/95-fac1.tptp\n-- Proving the conjecture in \/tmp\/LambdaLifting\/99-fac2.tptp ...\n-- E 1.6 Tiger Hill proved the conjecture in \/tmp\/LambdaLifting\/99-fac2.tptp\n-- Proving the conjecture in \/tmp\/LambdaLifting\/91-fac0.tptp ...\n-- Vampire 0.6 (revision 903) proved the conjecture in \/tmp\/LambdaLifting\/91-fac0.tptp\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"eeb64c8072203504365523df010e26b7cd75ace3","subject":"Revert \"+-interchange is now with implicit arguments...\"","message":"Revert \"+-interchange is now with implicit arguments...\"\n\nThis reverts commit dd40e9080437a9a681f55282bbd3b79a51c7b3d5.\n","repos":"crypto-agda\/crypto-agda","old_file":"Neglible.agda","new_file":"Neglible.agda","new_contents":"{-# OPTIONS --copatterns #-}\nopen import Algebra\n\nopen import Function\nopen import Function.Extensionality\n\nopen import Data.Nat.NP\nopen import Data.Nat.Distance\nopen import Data.Nat.Properties\nopen import Data.Two\nopen import Data.Zero\nopen import Data.Product\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\n\nopen import HoTT\nopen Equivalences\n\nopen import Explore.Core\nopen import Explore.Universe.Type {\ud835\udfd8}\nopen import Explore.Universe.Base\n\nmodule Neglible where\n\nmodule prop = CommutativeSemiring commutativeSemiring\nmodule OR = Poset (DecTotalOrder.poset decTotalOrder)\n\n\u2264-*-cancel : \u2200 {x m n} \u2192 1 \u2264 x \u2192 x * m \u2264 x * n \u2192 m \u2264 n\n\u2264-*-cancel {suc x} {m} {n} (s\u2264s le) mn\n rewrite prop.*-comm (suc x) m | prop.*-comm (suc x) n = cancel-*-right-\u2264 _ _ _ mn\n\nrecord \u2115\u2192\u211a : Set where\n constructor _\/_[_]\n field\n \u03b5N : (n : \u2115) \u2192 \u2115\n \u03b5D : (n : \u2115) \u2192 \u2115\n \u03b5D-pos : \u2200 n \u2192 \u03b5D n > 0\n\nrecord Is-Neg (\u03b5 : \u2115\u2192\u211a) : Set where\n constructor mk\n open \u2115\u2192\u211a \u03b5\n field\n c\u2099 : (c : \u2115) \u2192 \u2115\n prf : \u2200(c n : \u2115) \u2192 n > c\u2099 n \u2192 n ^ c * \u03b5N n \u2264 \u03b5D n\nopen Is-Neg\n\n0\u2115\u211a : \u2115\u2192\u211a\n\u2115\u2192\u211a.\u03b5N 0\u2115\u211a _ = 0\n\u2115\u2192\u211a.\u03b5D 0\u2115\u211a _ = 1\n\u2115\u2192\u211a.\u03b5D-pos 0\u2115\u211a _ = s\u2264s z\u2264n\n\n0\u2115\u211a-neg : Is-Neg 0\u2115\u211a\nc\u2099 0\u2115\u211a-neg _ = 0\nprf 0\u2115\u211a-neg c n x = OR.trans (OR.reflexive (proj\u2082 prop.zero (n ^ c))) z\u2264n\n\n_+\u2115\u211a_ : \u2115\u2192\u211a \u2192 \u2115\u2192\u211a \u2192 \u2115\u2192\u211a\n\u2115\u2192\u211a.\u03b5N ((\u03b5N \/ \u03b5D [ _ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ _ ])) n = \u03b5N n * \u03bcD n + \u03bcN n * \u03b5D n\n\u2115\u2192\u211a.\u03b5D ((\u03b5N \/ \u03b5D [ _ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ _ ])) n = \u03b5D n * \u03bcD n\n\u2115\u2192\u211a.\u03b5D-pos ((\u03b5N \/ \u03b5D [ \u03b5D+ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ \u03bcD+ ])) n = \u03b5D+ n *-mono \u03bcD+ n\n\n\n+\u2115\u211a-neg : {\u03b5 \u03bc : \u2115\u2192\u211a} \u2192 Is-Neg \u03b5 \u2192 Is-Neg \u03bc \u2192 Is-Neg (\u03b5 +\u2115\u211a \u03bc)\nc\u2099 (+\u2115\u211a-neg \u03b5 \u03bc) n = 1 + c\u2099 \u03b5 n + c\u2099 \u03bc n\nprf (+\u2115\u211a-neg {\u03b5M} {\u03bcM} \u03b5 \u03bc) c n n>nc = \u2264-*-cancel {x = n} (OR.trans (s\u2264s z\u2264n) n>nc) lemma\n where\n\n open \u2264-Reasoning\n open \u2115\u2192\u211a \u03b5M\n open \u2115\u2192\u211a \u03bcM renaming (\u03b5N to \u03bcN; \u03b5D to \u03bcD; \u03b5D-pos to \u03bcD-pos)\n\n lemma = n * (n ^ c * (\u03b5N n * \u03bcD n + \u03bcN n * \u03b5D n))\n \u2261\u27e8 ! prop.*-assoc n (n ^ c) _\n \u2219 proj\u2081 prop.distrib (n ^ (1 + c)) (\u03b5N n * \u03bcD n) (\u03bcN n * \u03b5D n)\n \u2219 ap\u2082 _+_ (! prop.*-assoc (n ^ (1 + c)) (\u03b5N n) (\u03bcD n))\n (! (prop.*-assoc (n ^ (1 + c)) (\u03bcN n) (\u03b5D n))) \u27e9\n n ^ (1 + c) * \u03b5N n * \u03bcD n + n ^ (1 + c) * \u03bcN n * \u03b5D n\n \u2264\u27e8 (prf \u03b5 (1 + c) n (OR.trans (s\u2264s (\u2264-step (m\u2264m+n (c\u2099 \u03b5 n) (c\u2099 \u03bc n)))) n>nc) *-mono (\u03bcD n \u220e))\n +-mono (prf \u03bc (1 + c) n (OR.trans (s\u2264s (\u2264-step (n\u2264m+n (c\u2099 \u03b5 n) (c\u2099 \u03bc n)))) n>nc) *-mono (\u03b5D n \u220e)) \u27e9\n \u03b5D n * \u03bcD n + \u03bcD n * \u03b5D n\n \u2261\u27e8 ap\u2082 _+_ (refl {x = \u03b5D n * \u03bcD n}) (prop.*-comm (\u03bcD n) (\u03b5D n) \u2219 ! proj\u2082 prop.+-identity (\u03b5D n * \u03bcD n)) \u27e9\n 2 * (\u03b5D n * \u03bcD n)\n \u2264\u27e8 OR.trans (s\u2264s (s\u2264s z\u2264n)) n>nc *-mono (\u03b5D n * \u03bcD n \u220e) \u27e9\n n * (\u03b5D n * \u03bcD n)\n \u220e\n\ninfix 4 _\u2264\u2192_\nrecord _\u2264\u2192_ (f g : \u2115\u2192\u211a) : Set where\n constructor mk\n open \u2115\u2192\u211a f renaming (\u03b5N to fN; \u03b5D to fD)\n open \u2115\u2192\u211a g renaming (\u03b5N to gN; \u03b5D to gD)\n field\n -- fN k \/ fD k \u2264 gN k \/ gD k\n \u2264\u2192 : \u2200 k \u2192 fN k * gD k \u2264 gN k * fD k\n\n\u2264\u2192-refl : \u2200 {f} \u2192 f \u2264\u2192 f\n_\u2264\u2192_.\u2264\u2192 \u2264\u2192-refl k = OR.refl\n\n\u2264\u2192-trans : \u2200 {f g h} \u2192 f \u2264\u2192 g \u2192 g \u2264\u2192 h \u2192 f \u2264\u2192 h\n_\u2264\u2192_.\u2264\u2192 (\u2264\u2192-trans {fN \/ fD [ fD-pos ]} {gN \/ gD [ gD-pos ]} {hN \/ hD [ hD-pos ]} (mk fg) (mk gh)) k\n = \u2264-*-cancel (gD-pos k) lemma\n where\n open \u2264-Reasoning\n lemma : gD k * (fN k * hD k) \u2264 gD k * (hN k * fD k)\n lemma = gD k * (fN k * hD k)\n \u2261\u27e8 ! prop.*-assoc (gD k) (fN k) (hD k)\n \u2219 ap (flip _*_ (hD k)) (prop.*-comm (gD k) (fN k))\n \u27e9\n (fN k * gD k) * hD k\n \u2264\u27e8 fg k *-mono OR.refl \u27e9\n (gN k * fD k) * hD k\n \u2261\u27e8 prop.*-assoc (gN k) (fD k) (hD k)\n \u2219 ap (_*_ (gN k)) (prop.*-comm (fD k) (hD k))\n \u2219 ! prop.*-assoc (gN k) (hD k) (fD k)\n \u27e9\n (gN k * hD k) * fD k\n \u2264\u27e8 gh k *-mono OR.refl \u27e9\n (hN k * gD k) * fD k\n \u2261\u27e8 ap (flip _*_ (fD k)) (prop.*-comm (hN k) (gD k))\n \u2219 prop.*-assoc (gD k) (hN k) (fD k)\n \u27e9\n gD k * (hN k * fD k)\n \u220e\n\n+\u2115\u211a-mono : \u2200 {f f' g g'} \u2192 f \u2264\u2192 f' \u2192 g \u2264\u2192 g' \u2192 f +\u2115\u211a g \u2264\u2192 f' +\u2115\u211a g'\n_\u2264\u2192_.\u2264\u2192 (+\u2115\u211a-mono {fN \/ fD [ _ ]} {f'N \/ f'D [ _ ]} {gN \/ gD [ _ ]} {g'N \/ g'D [ _ ]} (mk ff) (mk gg)) k\n = (fN k * gD k + gN k * fD k) * (f'D k * g'D k)\n \u2261\u27e8 proj\u2082 prop.distrib (f'D k * g'D k) (fN k * gD k) (gN k * fD k) \u27e9\n fN k * gD k * (f'D k * g'D k) + gN k * fD k * (f'D k * g'D k)\n \u2261\u27e8 ap\u2082 _+_ (*-interchange (fN k) (gD k) (f'D k) (g'D k) \u2219 ap (_*_ (fN k * f'D k)) (prop.*-comm (gD k) (g'D k)))\n (ap (_*_ (gN k * fD k)) (prop.*-comm (f'D k) (g'D k)) \u2219 *-interchange (gN k) (fD k) (g'D k) (f'D k))\n \u27e9\n fN k * f'D k * (g'D k * gD k) + gN k * g'D k * (fD k * f'D k)\n \u2264\u27e8 (ff k *-mono OR.refl) +-mono (gg k *-mono OR.refl) \u27e9\n f'N k * fD k * (g'D k * gD k) + g'N k * gD k * (fD k * f'D k)\n \u2261\u27e8 ap\u2082 _+_ (*-interchange (f'N k) (fD k) (g'D k) (gD k))\n (ap (_*_ (g'N k * gD k)) (prop.*-comm (fD k) (f'D k))\n \u2219 *-interchange (g'N k) (gD k) (f'D k) (fD k)\n \u2219 ap (_*_ (g'N k * f'D k)) (prop.*-comm (gD k) (fD k)))\n \u27e9\n f'N k * g'D k * (fD k * gD k) + g'N k * f'D k * (fD k * gD k)\n \u2261\u27e8 ! proj\u2082 prop.distrib (fD k * gD k) (f'N k * g'D k) (g'N k * f'D k) \u27e9\n (f'N k * g'D k + g'N k * f'D k) * (fD k * gD k)\n \u220e\n where\n open \u2264-Reasoning\n\nrecord NegBounded (f : \u2115\u2192\u211a) : Set where\n constructor mk\n field\n \u03b5 : \u2115\u2192\u211a\n \u03b5-neg : Is-Neg \u03b5\n bounded : f \u2264\u2192 \u03b5\n\nmodule _ where\n open NegBounded\n\n fromNeg : {f : \u2115\u2192\u211a} \u2192 Is-Neg f \u2192 NegBounded f\n \u03b5 (fromNeg f-neg) = _\n \u03b5-neg (fromNeg f-neg) = f-neg\n bounded (fromNeg f-neg) = \u2264\u2192-refl\n\n \u2264-NB : {f g : \u2115\u2192\u211a} \u2192 f \u2264\u2192 g \u2192 NegBounded g \u2192 NegBounded f\n \u03b5 (\u2264-NB le nb) = \u03b5 nb\n \u03b5-neg (\u2264-NB le nb) = \u03b5-neg nb\n bounded (\u2264-NB le nb) = \u2264\u2192-trans le (bounded nb)\n\n _+NB_ : {f g : \u2115\u2192\u211a} \u2192 NegBounded f \u2192 NegBounded g \u2192 NegBounded (f +\u2115\u211a g)\n \u03b5 (fNB +NB gNB) = \u03b5 fNB +\u2115\u211a \u03b5 gNB\n \u03b5-neg (fNB +NB gNB) = +\u2115\u211a-neg (\u03b5-neg fNB) (\u03b5-neg gNB)\n bounded (fNB +NB gNB) = +\u2115\u211a-mono (bounded fNB) (bounded gNB)\n\nmodule ~-NegBounded (R\u1d41 : \u2115 \u2192 U)(let R = \u03bb n \u2192 El (R\u1d41 n))(inh : \u2200 x \u2192 0 < Card (R\u1d41 x)) where\n\n # : \u2200 {n} \u2192 Count (R n)\n # {n} = count (R\u1d41 n)\n\n ~dist : (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) \u2192 \u2115\u2192\u211a\n \u2115\u2192\u211a.\u03b5N (~dist f g) n = dist (# (f n)) (# (g n))\n \u2115\u2192\u211a.\u03b5D (~dist f g) n = Card (R\u1d41 n)\n \u2115\u2192\u211a.\u03b5D-pos (~dist f g) n = inh n\n\n ~dist-sum : \u2200 f g h \u2192 ~dist f h \u2264\u2192 ~dist f g +\u2115\u211a ~dist g h\n _\u2264\u2192_.\u2264\u2192 (~dist-sum f g h) k\n = #fh * (|R| * |R|)\n \u2264\u27e8 dist-sum #f #g #h *-mono OR.refl \u27e9\n (#fg + #gh) * (|R| * |R|)\n \u2261\u27e8 ! prop.*-assoc (#fg + #gh) |R| |R| \u2219 ap (flip _*_ |R|) (proj\u2082 prop.distrib |R| #fg #gh) \u27e9\n (#fg * |R| + #gh * |R|) * |R|\n \u220e\n where\n open \u2264-Reasoning\n |R| = Card (R\u1d41 k)\n #f = # (f k)\n #g = # (g k)\n #h = # (h k)\n #fh = dist #f #h\n #fg = dist #f #g\n #gh = dist #g #h\n\n record _~_ (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) : Set where\n constructor mk\n field\n ~ : NegBounded (~dist f g)\n\n ~-trans : Transitive _~_\n _~_.~ (~-trans {f}{g}{h} (mk fg) (mk gh)) = \u2264-NB (~dist-sum f g h) (fg +NB gh)\n\n ~-Inv : {{_ : FunExt}}{{_ : UA}}(\u03c0 : \u2200 n \u2192 R n \u2243 R n)(f g : \u2200 x \u2192 R x \u2192 \ud835\udfda)\n (eq : \u2200 x (r : R x) \u2192 f x r \u2261 g x (proj\u2081 (\u03c0 x) r)) \u2192 f ~ g\n _~_.~ (~-Inv \u03c0 f g eq) = \u2264-NB lemma (fromNeg 0\u2115\u211a-neg)\n where\n open \u2264-Reasoning\n lemma : ~dist f g \u2264\u2192 0\u2115\u211a\n _\u2264\u2192_.\u2264\u2192 lemma k = dist (# (f k)) (# (g k)) * 1\n \u2261\u27e8 proj\u2082 prop.*-identity _ \u27e9\n dist (# (f k)) (# (g k))\n \u2261\u27e8 ap (flip dist (# (g k))) (count-ext (R\u1d41 k) (eq k)) \u27e9\n dist (# (g k \u2218 proj\u2081 (\u03c0 k))) (# (g k))\n \u2261\u27e8 ap (flip dist (# (g k))) (sumStableUnder (R\u1d41 k) (\u03c0 k) (\ud835\udfda\u25b9\u2115 \u2218 g k)) \u27e9\n dist (# (g k)) (# (g k))\n \u2261\u27e8 dist-refl (# (g k)) \u27e9\n 0\n \u220e\n\nmodule ~-Inlined (R\u1d41 : \u2115 \u2192 U)(let R = \u03bb n \u2192 El (R\u1d41 n)) where\n\n # : \u2200 {n} \u2192 Count (R n)\n # {n} = count (R\u1d41 n)\n\n record _~_ (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) : Set where\n constructor mk\n field\n \u03b5 : \u2115\u2192\u211a\n open \u2115\u2192\u211a \u03b5\n field\n \u03b5-neg : Is-Neg \u03b5\n bounded : \u2200 k \u2192 \u03b5D k * dist (# (f k)) (# (g k)) \u2264 Card (R\u1d41 k) * \u03b5N k\n\n\n ~-trans : Transitive _~_\n _~_.\u03b5 (~-trans x x\u2081) = _\n _~_.\u03b5-neg (~-trans x x\u2081) = +\u2115\u211a-neg (_~_.\u03b5-neg x) (_~_.\u03b5-neg x\u2081)\n _~_.bounded (~-trans {f}{g}{h}(mk \u03b5\u2080 \u03b5\u2080-neg fg) (mk \u03b5\u2081 \u03b5\u2081-neg gh)) k\n = (b * d) * dist #f #h\n \u2264\u27e8 (b * d \u220e) *-mono dist-sum #f #g #h \u27e9\n (b * d) * (dist #f #g + dist #g #h)\n \u2261\u27e8 proj\u2081 prop.distrib (b * d) (dist #f #g) (dist #g #h)\n \u2219 ap\u2082 _+_ (ap\u2082 _*_ (prop.*-comm b d) refl\n \u2219 prop.*-assoc d b (dist #f #g)) (prop.*-assoc b d (dist #g #h))\n \u27e9\n d * (b * dist #f #g) + b * (d * dist #g #h)\n \u2264\u27e8 ((d \u220e) *-mono fg k) +-mono ((b \u220e) *-mono gh k) \u27e9\n d * (|R| * a) + b * (|R| * c)\n \u2261\u27e8 ap\u2082 _+_ (rot d |R| a) (rot b |R| c) \u2219 ! proj\u2081 prop.distrib |R| (a * d) (c * b) \u27e9\n |R| * \u2115\u2192\u211a.\u03b5N (\u03b5\u2080 +\u2115\u211a \u03b5\u2081) k\n \u220e\n where\n open \u2264-Reasoning\n rot : \u2200 x y z \u2192 x * (y * z) \u2261 y * (z * x)\n rot x y z = prop.*-comm x (y * z) \u2219 prop.*-assoc y z x\n |R| = Card (R\u1d41 k)\n #f = # (f k)\n #g = # (g k)\n #h = # (h k)\n a = \u2115\u2192\u211a.\u03b5N \u03b5\u2080 k\n b = \u2115\u2192\u211a.\u03b5D \u03b5\u2080 k\n c = \u2115\u2192\u211a.\u03b5N \u03b5\u2081 k\n d = \u2115\u2192\u211a.\u03b5D \u03b5\u2081 k\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --copatterns #-}\nopen import Algebra\n\nopen import Function\nopen import Function.Extensionality\n\nopen import Data.Nat.NP\nopen import Data.Nat.Distance\nopen import Data.Nat.Properties\nopen import Data.Two\nopen import Data.Zero\nopen import Data.Product\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\n\nopen import HoTT\nopen Equivalences\n\nopen import Explore.Core\nopen import Explore.Universe.Type {\ud835\udfd8}\nopen import Explore.Universe.Base\n\nmodule Neglible where\n\nmodule prop = CommutativeSemiring commutativeSemiring\nmodule OR = Poset (DecTotalOrder.poset decTotalOrder)\n\n\u2264-*-cancel : \u2200 {x m n} \u2192 1 \u2264 x \u2192 x * m \u2264 x * n \u2192 m \u2264 n\n\u2264-*-cancel {suc x} {m} {n} (s\u2264s le) mn\n rewrite prop.*-comm (suc x) m | prop.*-comm (suc x) n = cancel-*-right-\u2264 _ _ _ mn\n\nrecord \u2115\u2192\u211a : Set where\n constructor _\/_[_]\n field\n \u03b5N : (n : \u2115) \u2192 \u2115\n \u03b5D : (n : \u2115) \u2192 \u2115\n \u03b5D-pos : \u2200 n \u2192 \u03b5D n > 0\n\nrecord Is-Neg (\u03b5 : \u2115\u2192\u211a) : Set where\n constructor mk\n open \u2115\u2192\u211a \u03b5\n field\n c\u2099 : (c : \u2115) \u2192 \u2115\n prf : \u2200(c n : \u2115) \u2192 n > c\u2099 n \u2192 n ^ c * \u03b5N n \u2264 \u03b5D n\nopen Is-Neg\n\n0\u2115\u211a : \u2115\u2192\u211a\n\u2115\u2192\u211a.\u03b5N 0\u2115\u211a _ = 0\n\u2115\u2192\u211a.\u03b5D 0\u2115\u211a _ = 1\n\u2115\u2192\u211a.\u03b5D-pos 0\u2115\u211a _ = s\u2264s z\u2264n\n\n0\u2115\u211a-neg : Is-Neg 0\u2115\u211a\nc\u2099 0\u2115\u211a-neg _ = 0\nprf 0\u2115\u211a-neg c n x = OR.trans (OR.reflexive (proj\u2082 prop.zero (n ^ c))) z\u2264n\n\n_+\u2115\u211a_ : \u2115\u2192\u211a \u2192 \u2115\u2192\u211a \u2192 \u2115\u2192\u211a\n\u2115\u2192\u211a.\u03b5N ((\u03b5N \/ \u03b5D [ _ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ _ ])) n = \u03b5N n * \u03bcD n + \u03bcN n * \u03b5D n\n\u2115\u2192\u211a.\u03b5D ((\u03b5N \/ \u03b5D [ _ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ _ ])) n = \u03b5D n * \u03bcD n\n\u2115\u2192\u211a.\u03b5D-pos ((\u03b5N \/ \u03b5D [ \u03b5D+ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ \u03bcD+ ])) n = \u03b5D+ n *-mono \u03bcD+ n\n\n\n+\u2115\u211a-neg : {\u03b5 \u03bc : \u2115\u2192\u211a} \u2192 Is-Neg \u03b5 \u2192 Is-Neg \u03bc \u2192 Is-Neg (\u03b5 +\u2115\u211a \u03bc)\nc\u2099 (+\u2115\u211a-neg \u03b5 \u03bc) n = 1 + c\u2099 \u03b5 n + c\u2099 \u03bc n\nprf (+\u2115\u211a-neg {\u03b5M} {\u03bcM} \u03b5 \u03bc) c n n>nc = \u2264-*-cancel {x = n} (OR.trans (s\u2264s z\u2264n) n>nc) lemma\n where\n\n open \u2264-Reasoning\n open \u2115\u2192\u211a \u03b5M\n open \u2115\u2192\u211a \u03bcM renaming (\u03b5N to \u03bcN; \u03b5D to \u03bcD; \u03b5D-pos to \u03bcD-pos)\n\n lemma = n * (n ^ c * (\u03b5N n * \u03bcD n + \u03bcN n * \u03b5D n))\n \u2261\u27e8 ! prop.*-assoc n (n ^ c) _\n \u2219 proj\u2081 prop.distrib (n ^ (1 + c)) (\u03b5N n * \u03bcD n) (\u03bcN n * \u03b5D n)\n \u2219 ap\u2082 _+_ (! prop.*-assoc (n ^ (1 + c)) (\u03b5N n) (\u03bcD n))\n (! (prop.*-assoc (n ^ (1 + c)) (\u03bcN n) (\u03b5D n))) \u27e9\n n ^ (1 + c) * \u03b5N n * \u03bcD n + n ^ (1 + c) * \u03bcN n * \u03b5D n\n \u2264\u27e8 (prf \u03b5 (1 + c) n (OR.trans (s\u2264s (\u2264-step (m\u2264m+n (c\u2099 \u03b5 n) (c\u2099 \u03bc n)))) n>nc) *-mono (\u03bcD n \u220e))\n +-mono (prf \u03bc (1 + c) n (OR.trans (s\u2264s (\u2264-step (n\u2264m+n (c\u2099 \u03b5 n) (c\u2099 \u03bc n)))) n>nc) *-mono (\u03b5D n \u220e)) \u27e9\n \u03b5D n * \u03bcD n + \u03bcD n * \u03b5D n\n \u2261\u27e8 ap\u2082 _+_ (refl {x = \u03b5D n * \u03bcD n}) (prop.*-comm (\u03bcD n) (\u03b5D n) \u2219 ! proj\u2082 prop.+-identity (\u03b5D n * \u03bcD n)) \u27e9\n 2 * (\u03b5D n * \u03bcD n)\n \u2264\u27e8 OR.trans (s\u2264s (s\u2264s z\u2264n)) n>nc *-mono (\u03b5D n * \u03bcD n \u220e) \u27e9\n n * (\u03b5D n * \u03bcD n)\n \u220e\n\ninfix 4 _\u2264\u2192_\nrecord _\u2264\u2192_ (f g : \u2115\u2192\u211a) : Set where\n constructor mk\n open \u2115\u2192\u211a f renaming (\u03b5N to fN; \u03b5D to fD)\n open \u2115\u2192\u211a g renaming (\u03b5N to gN; \u03b5D to gD)\n field\n -- fN k \/ fD k \u2264 gN k \/ gD k\n \u2264\u2192 : \u2200 k \u2192 fN k * gD k \u2264 gN k * fD k\n\n\u2264\u2192-refl : \u2200 {f} \u2192 f \u2264\u2192 f\n_\u2264\u2192_.\u2264\u2192 \u2264\u2192-refl k = OR.refl\n\n\u2264\u2192-trans : \u2200 {f g h} \u2192 f \u2264\u2192 g \u2192 g \u2264\u2192 h \u2192 f \u2264\u2192 h\n_\u2264\u2192_.\u2264\u2192 (\u2264\u2192-trans {fN \/ fD [ fD-pos ]} {gN \/ gD [ gD-pos ]} {hN \/ hD [ hD-pos ]} (mk fg) (mk gh)) k\n = \u2264-*-cancel (gD-pos k) lemma\n where\n open \u2264-Reasoning\n lemma : gD k * (fN k * hD k) \u2264 gD k * (hN k * fD k)\n lemma = gD k * (fN k * hD k)\n \u2261\u27e8 ! prop.*-assoc (gD k) (fN k) (hD k)\n \u2219 ap (flip _*_ (hD k)) (prop.*-comm (gD k) (fN k))\n \u27e9\n (fN k * gD k) * hD k\n \u2264\u27e8 fg k *-mono OR.refl \u27e9\n (gN k * fD k) * hD k\n \u2261\u27e8 prop.*-assoc (gN k) (fD k) (hD k)\n \u2219 ap (_*_ (gN k)) (prop.*-comm (fD k) (hD k))\n \u2219 ! prop.*-assoc (gN k) (hD k) (fD k)\n \u27e9\n (gN k * hD k) * fD k\n \u2264\u27e8 gh k *-mono OR.refl \u27e9\n (hN k * gD k) * fD k\n \u2261\u27e8 ap (flip _*_ (fD k)) (prop.*-comm (hN k) (gD k))\n \u2219 prop.*-assoc (gD k) (hN k) (fD k)\n \u27e9\n gD k * (hN k * fD k)\n \u220e\n\n+\u2115\u211a-mono : \u2200 {f f' g g'} \u2192 f \u2264\u2192 f' \u2192 g \u2264\u2192 g' \u2192 f +\u2115\u211a g \u2264\u2192 f' +\u2115\u211a g'\n_\u2264\u2192_.\u2264\u2192 (+\u2115\u211a-mono {fN \/ fD [ _ ]} {f'N \/ f'D [ _ ]} {gN \/ gD [ _ ]} {g'N \/ g'D [ _ ]} (mk ff) (mk gg)) k\n = (fN k * gD k + gN k * fD k) * (f'D k * g'D k)\n \u2261\u27e8 proj\u2082 prop.distrib (f'D k * g'D k) (fN k * gD k) (gN k * fD k) \u27e9\n fN k * gD k * (f'D k * g'D k) + gN k * fD k * (f'D k * g'D k)\n \u2261\u27e8 ap\u2082 _+_ (*-interchange {fN k} {gD k} {f'D k} {g'D k} \u2219 ap (_*_ (fN k * f'D k)) (prop.*-comm (gD k) (g'D k)))\n (ap (_*_ (gN k * fD k)) (prop.*-comm (f'D k) (g'D k)) \u2219 *-interchange {gN k} {fD k} {g'D k} {f'D k})\n \u27e9\n fN k * f'D k * (g'D k * gD k) + gN k * g'D k * (fD k * f'D k)\n \u2264\u27e8 (ff k *-mono OR.refl) +-mono (gg k *-mono OR.refl) \u27e9\n f'N k * fD k * (g'D k * gD k) + g'N k * gD k * (fD k * f'D k)\n \u2261\u27e8 ap\u2082 _+_ (*-interchange {f'N k} {fD k} {g'D k} {gD k})\n (ap (_*_ (g'N k * gD k)) (prop.*-comm (fD k) (f'D k))\n \u2219 *-interchange {g'N k} {gD k} {f'D k} {fD k}\n \u2219 ap (_*_ (g'N k * f'D k)) (prop.*-comm (gD k) (fD k)))\n \u27e9\n f'N k * g'D k * (fD k * gD k) + g'N k * f'D k * (fD k * gD k)\n \u2261\u27e8 ! proj\u2082 prop.distrib (fD k * gD k) (f'N k * g'D k) (g'N k * f'D k) \u27e9\n (f'N k * g'D k + g'N k * f'D k) * (fD k * gD k)\n \u220e\n where\n open \u2264-Reasoning\n\nrecord NegBounded (f : \u2115\u2192\u211a) : Set where\n constructor mk\n field\n \u03b5 : \u2115\u2192\u211a\n \u03b5-neg : Is-Neg \u03b5\n bounded : f \u2264\u2192 \u03b5\n\nmodule _ where\n open NegBounded\n\n fromNeg : {f : \u2115\u2192\u211a} \u2192 Is-Neg f \u2192 NegBounded f\n \u03b5 (fromNeg f-neg) = _\n \u03b5-neg (fromNeg f-neg) = f-neg\n bounded (fromNeg f-neg) = \u2264\u2192-refl\n\n \u2264-NB : {f g : \u2115\u2192\u211a} \u2192 f \u2264\u2192 g \u2192 NegBounded g \u2192 NegBounded f\n \u03b5 (\u2264-NB le nb) = \u03b5 nb\n \u03b5-neg (\u2264-NB le nb) = \u03b5-neg nb\n bounded (\u2264-NB le nb) = \u2264\u2192-trans le (bounded nb)\n\n _+NB_ : {f g : \u2115\u2192\u211a} \u2192 NegBounded f \u2192 NegBounded g \u2192 NegBounded (f +\u2115\u211a g)\n \u03b5 (fNB +NB gNB) = \u03b5 fNB +\u2115\u211a \u03b5 gNB\n \u03b5-neg (fNB +NB gNB) = +\u2115\u211a-neg (\u03b5-neg fNB) (\u03b5-neg gNB)\n bounded (fNB +NB gNB) = +\u2115\u211a-mono (bounded fNB) (bounded gNB)\n\nmodule ~-NegBounded (R\u1d41 : \u2115 \u2192 U)(let R = \u03bb n \u2192 El (R\u1d41 n))(inh : \u2200 x \u2192 0 < Card (R\u1d41 x)) where\n\n # : \u2200 {n} \u2192 Count (R n)\n # {n} = count (R\u1d41 n)\n\n ~dist : (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) \u2192 \u2115\u2192\u211a\n \u2115\u2192\u211a.\u03b5N (~dist f g) n = dist (# (f n)) (# (g n))\n \u2115\u2192\u211a.\u03b5D (~dist f g) n = Card (R\u1d41 n)\n \u2115\u2192\u211a.\u03b5D-pos (~dist f g) n = inh n\n\n ~dist-sum : \u2200 f g h \u2192 ~dist f h \u2264\u2192 ~dist f g +\u2115\u211a ~dist g h\n _\u2264\u2192_.\u2264\u2192 (~dist-sum f g h) k\n = #fh * (|R| * |R|)\n \u2264\u27e8 dist-sum #f #g #h *-mono OR.refl \u27e9\n (#fg + #gh) * (|R| * |R|)\n \u2261\u27e8 ! prop.*-assoc (#fg + #gh) |R| |R| \u2219 ap (flip _*_ |R|) (proj\u2082 prop.distrib |R| #fg #gh) \u27e9\n (#fg * |R| + #gh * |R|) * |R|\n \u220e\n where\n open \u2264-Reasoning\n |R| = Card (R\u1d41 k)\n #f = # (f k)\n #g = # (g k)\n #h = # (h k)\n #fh = dist #f #h\n #fg = dist #f #g\n #gh = dist #g #h\n\n record _~_ (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) : Set where\n constructor mk\n field\n ~ : NegBounded (~dist f g)\n\n ~-trans : Transitive _~_\n _~_.~ (~-trans {f}{g}{h} (mk fg) (mk gh)) = \u2264-NB (~dist-sum f g h) (fg +NB gh)\n\n ~-Inv : {{_ : FunExt}}{{_ : UA}}(\u03c0 : \u2200 n \u2192 R n \u2243 R n)(f g : \u2200 x \u2192 R x \u2192 \ud835\udfda)\n (eq : \u2200 x (r : R x) \u2192 f x r \u2261 g x (proj\u2081 (\u03c0 x) r)) \u2192 f ~ g\n _~_.~ (~-Inv \u03c0 f g eq) = \u2264-NB lemma (fromNeg 0\u2115\u211a-neg)\n where\n open \u2264-Reasoning\n lemma : ~dist f g \u2264\u2192 0\u2115\u211a\n _\u2264\u2192_.\u2264\u2192 lemma k = dist (# (f k)) (# (g k)) * 1\n \u2261\u27e8 proj\u2082 prop.*-identity _ \u27e9\n dist (# (f k)) (# (g k))\n \u2261\u27e8 ap (flip dist (# (g k))) (count-ext (R\u1d41 k) (eq k)) \u27e9\n dist (# (g k \u2218 proj\u2081 (\u03c0 k))) (# (g k))\n \u2261\u27e8 ap (flip dist (# (g k))) (sumStableUnder (R\u1d41 k) (\u03c0 k) (\ud835\udfda\u25b9\u2115 \u2218 g k)) \u27e9\n dist (# (g k)) (# (g k))\n \u2261\u27e8 dist-refl (# (g k)) \u27e9\n 0\n \u220e\n\nmodule ~-Inlined (R\u1d41 : \u2115 \u2192 U)(let R = \u03bb n \u2192 El (R\u1d41 n)) where\n\n # : \u2200 {n} \u2192 Count (R n)\n # {n} = count (R\u1d41 n)\n\n record _~_ (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) : Set where\n constructor mk\n field\n \u03b5 : \u2115\u2192\u211a\n open \u2115\u2192\u211a \u03b5\n field\n \u03b5-neg : Is-Neg \u03b5\n bounded : \u2200 k \u2192 \u03b5D k * dist (# (f k)) (# (g k)) \u2264 Card (R\u1d41 k) * \u03b5N k\n\n\n ~-trans : Transitive _~_\n _~_.\u03b5 (~-trans x x\u2081) = _\n _~_.\u03b5-neg (~-trans x x\u2081) = +\u2115\u211a-neg (_~_.\u03b5-neg x) (_~_.\u03b5-neg x\u2081)\n _~_.bounded (~-trans {f}{g}{h}(mk \u03b5\u2080 \u03b5\u2080-neg fg) (mk \u03b5\u2081 \u03b5\u2081-neg gh)) k\n = (b * d) * dist #f #h\n \u2264\u27e8 (b * d \u220e) *-mono dist-sum #f #g #h \u27e9\n (b * d) * (dist #f #g + dist #g #h)\n \u2261\u27e8 proj\u2081 prop.distrib (b * d) (dist #f #g) (dist #g #h)\n \u2219 ap\u2082 _+_ (ap\u2082 _*_ (prop.*-comm b d) refl\n \u2219 prop.*-assoc d b (dist #f #g)) (prop.*-assoc b d (dist #g #h))\n \u27e9\n d * (b * dist #f #g) + b * (d * dist #g #h)\n \u2264\u27e8 ((d \u220e) *-mono fg k) +-mono ((b \u220e) *-mono gh k) \u27e9\n d * (|R| * a) + b * (|R| * c)\n \u2261\u27e8 ap\u2082 _+_ (rot d |R| a) (rot b |R| c) \u2219 ! proj\u2081 prop.distrib |R| (a * d) (c * b) \u27e9\n |R| * \u2115\u2192\u211a.\u03b5N (\u03b5\u2080 +\u2115\u211a \u03b5\u2081) k\n \u220e\n where\n open \u2264-Reasoning\n rot : \u2200 x y z \u2192 x * (y * z) \u2261 y * (z * x)\n rot x y z = prop.*-comm x (y * z) \u2219 prop.*-assoc y z x\n |R| = Card (R\u1d41 k)\n #f = # (f k)\n #g = # (g k)\n #h = # (h k)\n a = \u2115\u2192\u211a.\u03b5N \u03b5\u2080 k\n b = \u2115\u2192\u211a.\u03b5D \u03b5\u2080 k\n c = \u2115\u2192\u211a.\u03b5N \u03b5\u2081 k\n d = \u2115\u2192\u211a.\u03b5D \u03b5\u2081 k\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"57708b770c00d1ba6276cda0f02752e147f2f533","subject":"Added a non-required totality hypothesis.","message":"Added a non-required totality hypothesis.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Program\/McCarthy91\/PropertiesATP.agda","new_file":"src\/fot\/FOTC\/Program\/McCarthy91\/PropertiesATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Main properties of the McCarthy 91 function\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- The main properties proved of the McCarthy 91 function (called\n-- f\u2089\u2081) are\n\n-- 1. The function always terminates.\n-- 2. For all n, n < f\u2089\u2081 n + 11.\n-- 3. For all n > 100, then f\u2089\u2081 n = n - 10.\n-- 4. For all n <= 100, then f\u2089\u2081 n = 91.\n\n-- N.B This module does not contain combined proofs, but it imports\n-- modules which contain combined proofs.\n\nmodule FOTC.Program.McCarthy91.PropertiesATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.EliminationPropertiesATP\n using ( x>y\u2192x\u2264y\u2192\u22a5 )\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\n using ( x>y\u2228x\u226fy\n ; x\u226fy\u2192x\u2264y\n ; x\u226fSy\u2192x\u226fy\u2228x\u2261Sy\n ; x+k 100, then f\u2089\u2081 n = n - 10.\n--\n-- N.B. (21 November 2013). The hypothesis N n is not necessary.\npostulate f\u2089\u2081-x>100 : \u2200 n \u2192 N n \u2192 n > 100' \u2192 f\u2089\u2081 n \u2261 n \u2238 10'\n{-# ATP prove f\u2089\u2081-x>100 #-}\n\n-- For all n <= 100, then f\u2089\u2081 n = 91.\nf\u2089\u2081-x\u226f100 : \u2200 {n} \u2192 N n \u2192 n \u226f 100' \u2192 f\u2089\u2081 n \u2261 91'\nf\u2089\u2081-x\u226f100 = \u25c1-wfind A h\n where\n A : D \u2192 Set\n A d = d \u226f 100' \u2192 f\u2089\u2081 d \u2261 91'\n\n h : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 k \u25c1 m \u2192 A k) \u2192 A m\n h {m} Nm f with x>y\u2228x\u226fy Nm 100-N\n ... | inj\u2081 m>100 = \u03bb m\u226f100 \u2192\n \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nm 100-N m>100 (x\u226fy\u2192x\u2264y Nm 100-N m\u226f100))\n ... | inj\u2082 m\u226f100 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 99-N m\u226f100\n ... | inj\u2082 m\u2261100 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-100 m\u2261100\n ... | inj\u2081 m\u226f99 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 98-N m\u226f99\n ... | inj\u2082 m\u226199 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-99 m\u226199\n ... | inj\u2081 m\u226f98 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 97-N m\u226f98\n ... | inj\u2082 m\u226198 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-98 m\u226198\n ... | inj\u2081 m\u226f97 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 96-N m\u226f97\n ... | inj\u2082 m\u226197 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-97 m\u226197\n ... | inj\u2081 m\u226f96 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 95-N m\u226f96\n ... | inj\u2082 m\u226196 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-96 m\u226196\n ... | inj\u2081 m\u226f95 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 94-N m\u226f95\n ... | inj\u2082 m\u226195 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-95 m\u226195\n ... | inj\u2081 m\u226f94 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 93-N m\u226f94\n ... | inj\u2082 m\u226194 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-94 m\u226194\n ... | inj\u2081 m\u226f93 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 92-N m\u226f93\n ... | inj\u2082 m\u226193 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-93 m\u226193\n ... | inj\u2081 m\u226f92 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 91-N m\u226f92\n ... | inj\u2082 m\u226192 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-92 m\u226192\n ... | inj\u2081 m\u226f91 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 90-N m\u226f91\n ... | inj\u2082 m\u226191 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-91 m\u226191\n ... | inj\u2081 m\u226f90 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 89-N m\u226f90\n ... | inj\u2082 m\u226190 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-90 m\u226190\n ... | inj\u2081 m\u226f89 = \u03bb _ \u2192 f\u2089\u2081-m\u226f89\n where\n m\u226489 : m \u2264 89'\n m\u226489 = x\u226fy\u2192x\u2264y Nm 89-N m\u226f89\n\n f\u2089\u2081-m+11 : m + 11' \u226f 100' \u2192 f\u2089\u2081 (m + 11') \u2261 91'\n f\u2089\u2081-m+11 = f (x+11-N Nm) (<\u2192\u25c1 (x+11-N Nm) Nm m\u226f100 (xy\u2228x\u226fy Nn 100-N\n... | inj\u2081 n>100 = subst N (sym (f\u2089\u2081-x>100 n Nn n>100)) (\u2238-N Nn 10-N)\n... | inj\u2082 n\u226f100 = subst N (sym (f\u2089\u2081-x\u226f100 Nn n\u226f100)) 91-N\n\n-- For all n, n < f\u2089\u2081 n + 11.\nf\u2089\u2081-ineq : \u2200 {n} \u2192 N n \u2192 n < f\u2089\u2081 n + 11'\nf\u2089\u2081-ineq = \u25c1-wfind A h\n where\n A : D \u2192 Set\n A d = d < f\u2089\u2081 d + 11'\n\n h : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 k \u25c1 m \u2192 A k) \u2192 A m\n h {m} Nm f with x>y\u2228x\u226fy Nm 100-N\n ... | inj\u2081 m>100 = x>100\u2192x100\n ... | inj\u2082 m\u226f100 =\n let f\u2089\u2081-m+11-N : N (f\u2089\u2081 (m + 11'))\n f\u2089\u2081-m+11-N = f\u2089\u2081-N (+-N Nm 11-N)\n\n h\u2081 : A (m + 11')\n h\u2081 = f (x+11-N Nm) (<\u2192\u25c1 (x+11-N Nm) Nm m\u226f100 (x 100, then f\u2089\u2081 n = n - 10.\n-- 4. For all n <= 100, then f\u2089\u2081 n = 91.\n\n-- N.B This module does not contain combined proofs, but it imports\n-- modules which contain combined proofs.\n\nmodule FOTC.Program.McCarthy91.PropertiesATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.EliminationPropertiesATP\n using ( x>y\u2192x\u2264y\u2192\u22a5 )\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\n using ( x>y\u2228x\u226fy\n ; x\u226fy\u2192x\u2264y\n ; x\u226fSy\u2192x\u226fy\u2228x\u2261Sy\n ; x+k 100, then f\u2089\u2081 n = n - 10.\npostulate f\u2089\u2081-x>100 : \u2200 n \u2192 n > 100' \u2192 f\u2089\u2081 n \u2261 n \u2238 10'\n{-# ATP prove f\u2089\u2081-x>100 #-}\n\n-- For all n <= 100, then f\u2089\u2081 n = 91.\nf\u2089\u2081-x\u226f100 : \u2200 {n} \u2192 N n \u2192 n \u226f 100' \u2192 f\u2089\u2081 n \u2261 91'\nf\u2089\u2081-x\u226f100 = \u25c1-wfind A h\n where\n A : D \u2192 Set\n A d = d \u226f 100' \u2192 f\u2089\u2081 d \u2261 91'\n\n h : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 k \u25c1 m \u2192 A k) \u2192 A m\n h {m} Nm f with x>y\u2228x\u226fy Nm 100-N\n ... | inj\u2081 m>100 = \u03bb m\u226f100 \u2192\n \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nm 100-N m>100 (x\u226fy\u2192x\u2264y Nm 100-N m\u226f100))\n ... | inj\u2082 m\u226f100 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 99-N m\u226f100\n ... | inj\u2082 m\u2261100 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-100 m\u2261100\n ... | inj\u2081 m\u226f99 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 98-N m\u226f99\n ... | inj\u2082 m\u226199 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-99 m\u226199\n ... | inj\u2081 m\u226f98 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 97-N m\u226f98\n ... | inj\u2082 m\u226198 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-98 m\u226198\n ... | inj\u2081 m\u226f97 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 96-N m\u226f97\n ... | inj\u2082 m\u226197 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-97 m\u226197\n ... | inj\u2081 m\u226f96 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 95-N m\u226f96\n ... | inj\u2082 m\u226196 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-96 m\u226196\n ... | inj\u2081 m\u226f95 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 94-N m\u226f95\n ... | inj\u2082 m\u226195 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-95 m\u226195\n ... | inj\u2081 m\u226f94 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 93-N m\u226f94\n ... | inj\u2082 m\u226194 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-94 m\u226194\n ... | inj\u2081 m\u226f93 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 92-N m\u226f93\n ... | inj\u2082 m\u226193 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-93 m\u226193\n ... | inj\u2081 m\u226f92 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 91-N m\u226f92\n ... | inj\u2082 m\u226192 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-92 m\u226192\n ... | inj\u2081 m\u226f91 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 90-N m\u226f91\n ... | inj\u2082 m\u226191 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-91 m\u226191\n ... | inj\u2081 m\u226f90 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 89-N m\u226f90\n ... | inj\u2082 m\u226190 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-90 m\u226190\n ... | inj\u2081 m\u226f89 = \u03bb _ \u2192 f\u2089\u2081-m\u226f89\n where\n m\u226489 : m \u2264 89'\n m\u226489 = x\u226fy\u2192x\u2264y Nm 89-N m\u226f89\n\n f\u2089\u2081-m+11 : m + 11' \u226f 100' \u2192 f\u2089\u2081 (m + 11') \u2261 91'\n f\u2089\u2081-m+11 = f (x+11-N Nm) (<\u2192\u25c1 (x+11-N Nm) Nm m\u226f100 (xy\u2228x\u226fy Nn 100-N\n... | inj\u2081 n>100 = subst N (sym (f\u2089\u2081-x>100 n n>100)) (\u2238-N Nn 10-N)\n... | inj\u2082 n\u226f100 = subst N (sym (f\u2089\u2081-x\u226f100 Nn n\u226f100)) 91-N\n\n-- For all n, n < f\u2089\u2081 n + 11.\nf\u2089\u2081-ineq : \u2200 {n} \u2192 N n \u2192 n < f\u2089\u2081 n + 11'\nf\u2089\u2081-ineq = \u25c1-wfind A h\n where\n A : D \u2192 Set\n A d = d < f\u2089\u2081 d + 11'\n\n h : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 k \u25c1 m \u2192 A k) \u2192 A m\n h {m} Nm f with x>y\u2228x\u226fy Nm 100-N\n ... | inj\u2081 m>100 = x>100\u2192x100\n ... | inj\u2082 m\u226f100 =\n let f\u2089\u2081-m+11-N : N (f\u2089\u2081 (m + 11'))\n f\u2089\u2081-m+11-N = f\u2089\u2081-N (+-N Nm 11-N)\n\n h\u2081 : A (m + 11')\n h\u2081 = f (x+11-N Nm) (<\u2192\u25c1 (x+11-N Nm) Nm m\u226f100 (x_ = {!!}\n }\nSquare snr (B shape shape\u2081) = {!!}\n -- where\n -- open SemiNearRing snr\n\n -- S = Sq s shape\n\n -- open Operations s _\u2219\u209b_ _+\u209b_\n -- renaming (_+_ to _+S_; _*_ to _\u2219S_)\n\n -- 0S = One 0\u209b\n\n -- -- _\u2243S_ : S \u2192 S \u2192 Set\n -- -- _\u2243S_ m n with shape\n -- -- ... | sh = ?\n\n\n -- SNR : SemiNearRing\n -- SNR =\n -- record\n -- { s = S\n -- ; _\u2243\u209b_ = {!!}\n -- ; 0\u209b = {!!}\n -- ; _+\u209b_ = {!!}\n -- ; _\u2219\u209b_ = {!!}\n -- ; isCommMon = {!!}\n -- ; zero\u02e1 = {!!}\n -- ; zero\u02b3 = {!!}\n -- ; _<\u2219>_ = {!!}\n -- }\n","old_contents":"-- Matrices indexed by shape\nmodule SquareMatrix where\n\nopen import Shape\nopen import Matrix\n\n-- Square matrices have the same shape in both dimensions\nSq : Set \u2192 Shape \u2192 Set\nSq a shape = M a shape shape\n\nopen import SemiNearRingRecord\n-- TODO: imported module missing?\n\n-- Lifting a SNR to a to a Square matrix of some shape\n-- TODO: need to look at shape to make this one work?\nSquare : SemiNearRing \u2192 Shape \u2192 SemiNearRing\nSquare snr L = SNR\n where\n open SemiNearRing snr\n\n S = Sq s L\n\n _+S_ : S \u2192 S \u2192 S\n One x +S One x\u2081 = One (x +\u209b x\u2081)\n\n _\u2219S_ : S \u2192 S \u2192 S\n One x \u2219S One x\u2081 = One (x \u2219\u209b x\u2081)\n\n 0S = One 0\u209b\n\n _\u2243S_ : S \u2192 S \u2192 Set\n One x \u2243S One x\u2081 = x \u2243\u209b x\u2081\n\n SNR : SemiNearRing\n SNR = record\n { s = S\n ; _\u2243\u209b_ = _\u2243S_\n ; 0\u209b = 0S\n ; _+\u209b_ = _+S_\n ; _\u2219\u209b_ = _\u2219S_\n ; isCommMon = {!!}\n ; zero\u02e1 = {!!}\n ; zero\u02b3 = {!!}\n ; _<\u2219>_ = {!!}\n }\nSquare snr (B shape shape\u2081) = {!!}\n -- where\n -- open SemiNearRing snr\n\n -- S = Sq s shape\n\n -- open Operations s _\u2219\u209b_ _+\u209b_\n -- renaming (_+_ to _+S_; _*_ to _\u2219S_)\n\n -- 0S = One 0\u209b\n\n -- -- _\u2243S_ : S \u2192 S \u2192 Set\n -- -- _\u2243S_ m n with shape\n -- -- ... | sh = ?\n\n\n -- SNR : SemiNearRing\n -- SNR =\n -- record\n -- { s = S\n -- ; _\u2243\u209b_ = {!!}\n -- ; 0\u209b = {!!}\n -- ; _+\u209b_ = {!!}\n -- ; _\u2219\u209b_ = {!!}\n -- ; isCommMon = {!!}\n -- ; zero\u02e1 = {!!}\n -- ; zero\u02b3 = {!!}\n -- ; _<\u2219>_ = {!!}\n -- }\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"145e1c87374595fe5964750f55adf91496b78092","subject":"lift-+ forgot to add this one","message":"lift-+ forgot to add this one\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Function\/Related\/TypeIsomorphisms\/NP.agda","new_file":"lib\/Function\/Related\/TypeIsomorphisms\/NP.agda","new_contents":"module Function.Related.TypeIsomorphisms.NP where\n\nimport Level as L\nopen import Algebra\nimport Algebra.FunctionProperties as FP\nopen L using (Lift; lower; lift)\nopen import Type\n\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Vec using (Vec; []; _\u2237_)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_) renaming (_^_ to _**_)\nopen import Data.Maybe.NP\nopen import Data.Product renaming (map to map\u00d7)\n--open import Data.Product.N-ary\nopen import Data.Sum renaming (map to map\u228e)\nopen import Data.Unit\nopen import Data.Empty\n\nimport Function as F\nimport Function.Equality as FE\nopen FE using (_\u27e8$\u27e9_)\nopen import Function.Related as FR\nopen import Function.Related.TypeIsomorphisms public\nimport Function.Inverse as Inv\nopen Inv using (_\u2194_; _\u2218_; sym; id; module Inverse)\nopen import Relation.Binary\nopen import Relation.Binary.Product.Pointwise\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (\u2192-to-\u27f6)\n\n\nInv-isEquivalence : IsEquivalence (Inv.Inverse {f\u2081 = L.zero} {f\u2082 = L.zero})\nInv-isEquivalence = record\n { refl = Inv.id\n ; sym = Inv.sym\n ; trans = F.flip Inv._\u2218_ }\n\nSEToid : Set _\nSEToid = Setoid L.zero L.zero\n\nSEToid\u2081 : Setoid _ _\nSEToid\u2081 = record\n { Carrier = Setoid L.zero L.zero\n ; _\u2248_ = Inv.Inverse\n ; isEquivalence = Inv-isEquivalence }\n\n\u228e-ICommutativeMonoid : CommutativeMonoid _ _\n\u228e-ICommutativeMonoid = record {\n Carrier = SEToid ;\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u228e-setoid_;\n \u03b5 = \u2261.setoid \u22a5;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv-isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u228e-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \u22a5) _\u228e-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = [ (\u03bb ()) , F.id ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = inj\u2082\n ; cong = \u2082\u223c\u2082\n }\n ; inverse-of = record\n { left-inverse-of = [ (\u03bb ()) , (\u03bb x \u2192 \u2082\u223c\u2082 refl) ]\n ; right-inverse-of = \u03bb x \u2192 refl\n }\n } where open Setoid A\n cong-to : Setoid._\u2248_ (\u2261.setoid \u22a5 \u228e-setoid A) =[ _ ]\u21d2 _\u2248_\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y) rewrite x\u223c\u2081y = refl\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = x\u223c\u2082y\n\n assoc : Associative _\u228e-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = [ [ inj\u2081 , inj\u2082 F.\u2218 inj\u2081 ] , inj\u2082 F.\u2218 inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = [ inj\u2081 F.\u2218 inj\u2081 , [ inj\u2081 F.\u2218 inj\u2082 , inj\u2082 ] ]\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = [ [ (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2081\u223c\u2081 (refl A))) , (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2082\u223c\u2082 (refl B)) ) ] , (\u03bb _ \u2192 \u2082\u223c\u2082 (refl C)) ]\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (refl A)) , [ (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2081\u223c\u2081 (refl B))) , (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2082\u223c\u2082 (refl C))) ] ]\n }\n } where\n open Setoid\n cong-to : _\u2248_ ((A \u228e-setoid B) \u228e-setoid C) =[ _ ]\u21d2 _\u2248_ (A \u228e-setoid (B \u228e-setoid C))\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2082 ()))\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 x\u223c\u2081y\n cong-to (\u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2082y)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = \u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)\n\n cong-from : _\u2248_ (A \u228e-setoid (B \u228e-setoid C)) =[ _ ]\u21d2 _\u2248_ ((A \u228e-setoid B) \u228e-setoid C)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 x\u223c\u2081y) = \u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2082 ()))\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 x\u223c\u2082y\n\n comm : Commutative _\u228e-setoid_\n comm A B = record\n { to = swap'\n ; from = swap'\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B : SEToid} \u2192 A \u228e-setoid B FE.\u27f6 B \u228e-setoid A\n swap' {A} {B} = record\n { _\u27e8$\u27e9_ = [ inj\u2082 , inj\u2081 ]\n ; cong = cong\n } where\n cong : Setoid._\u2248_ (A \u228e-setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (B \u228e-setoid A)\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2082\u223c\u2082 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2081\u223c\u2081 x\u223c\u2082y\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u228e-setoid B) (swap' FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (Setoid.refl A)) , (\u03bb _ \u2192 \u2082\u223c\u2082 (Setoid.refl B)) ]\n\n\u00d7-ICommutativeMonoid : CommutativeMonoid _ _\n\u00d7-ICommutativeMonoid = record {\n Carrier = SEToid ;\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u00d7-setoid_;\n \u03b5 = \u2261.setoid \u22a4;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv-isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u00d7-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \u22a4) _\u00d7-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2082\n ; cong = proj\u2082\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 _ , x\n ; cong = \u03bb x \u2192 \u2261.refl , x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u2261.refl , (Setoid.refl A)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl A\n }\n }\n\n assoc : Associative _\u00d7-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n ; cong = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n }\n ; from = record\n { _\u27e8$\u27e9_ = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n ; cong = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb _ \u2192 Setoid.refl ((A \u00d7-setoid B) \u00d7-setoid C)\n ; right-inverse-of = \u03bb _ \u2192 Setoid.refl (A \u00d7-setoid (B \u00d7-setoid C))\n }\n }\n\n comm : Commutative _\u00d7-setoid_\n comm A B = record\n { to = swap' {A} {B}\n ; from = swap' {B} {A}\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B : SEToid} \u2192 A \u00d7-setoid B FE.\u27f6 B \u00d7-setoid A\n swap' {A}{B} = record { _\u27e8$\u27e9_ = swap; cong = swap }\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u00d7-setoid B) (swap' {B} {A} FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = \u03bb x \u2192 Setoid.refl (A \u00d7-setoid B)\n\n\u00d7\u228e-ICommutativeSemiring : CommutativeSemiring _ _\n\u00d7\u228e-ICommutativeSemiring = record\n { Carrier = SEToid\n ; _\u2248_ = Inv.Inverse\n ; _+_ = _\u228e-setoid_\n ; _*_ = _\u00d7-setoid_\n ; 0# = \u2261.setoid \u22a5\n ; 1# = \u2261.setoid \u22a4\n ; isCommutativeSemiring = record\n { +-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u228e-ICommutativeMonoid\n ; *-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u00d7-ICommutativeMonoid\n ; distrib\u02b3 = distrib\u02b3\n ; zero\u02e1 = zero\u02e1\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n distrib\u02b3 : _\u00d7-setoid_ DistributesOver\u02b3 _\u228e-setoid_\n distrib\u02b3 A B C = record\n { to = record\n { _\u27e8$\u27e9_ = uncurry [ curry inj\u2081 , curry inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = from\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = uncurry [ (\u03bb x y \u2192 (\u2081\u223c\u2081 (refl B)) , (refl A)) , (\u03bb x y \u2192 (\u2082\u223c\u2082 (refl C)) , (refl A)) ]\n ; right-inverse-of = [ uncurry (\u03bb x y \u2192 \u2081\u223c\u2081 ((refl B {x}) , (refl A {y})))\n , uncurry (\u03bb x y \u2192 \u2082\u223c\u2082 ((refl C {x}) , (refl A {y}))) ]\n }\n } where\n open Setoid\n A' = Carrier A\n B' = Carrier B\n C' = Carrier C\n\n cong-to : _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A) =[ _ ]\u21d2 _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A))\n cong-to (\u2081\u223c\u2082 () , _)\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y , A-rel) = \u2081\u223c\u2081 (x\u223c\u2081y , A-rel)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y , A-rel) = \u2082\u223c\u2082 (x\u223c\u2082y , A-rel)\n\n from : B' \u00d7 A' \u228e C' \u00d7 A' \u2192 (B' \u228e C') \u00d7 A'\n from = [ map\u00d7 inj\u2081 F.id , map\u00d7 inj\u2082 F.id ]\n\n cong-from : _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A)) =[ _ ]\u21d2 _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 (B-rel , A-rel)) = (\u2081\u223c\u2081 B-rel , A-rel)\n cong-from (\u2082\u223c\u2082 (C-rel , A-rel)) = \u2082\u223c\u2082 C-rel , A-rel\n\n zero\u02e1 : LeftZero (\u2261.setoid \u22a5) _\u00d7-setoid_\n zero\u02e1 A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2081\n ; cong = proj\u2081\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u22a5-elim\n ; cong = \u03bb { {()} x }\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u22a5-elim (proj\u2081 x)\n ; right-inverse-of = \u03bb x \u2192 \u22a5-elim x\n }\n }\n\nmodule \u00d7-CMon = CommutativeMonoid (\u00d7-CommutativeMonoid FR.bijection L.zero)\nmodule \u228e-CMon = CommutativeMonoid (\u228e-CommutativeMonoid FR.bijection L.zero)\nmodule \u00d7\u228e\u00b0 = CommutativeSemiring (\u00d7\u228e-CommutativeSemiring FR.bijection L.zero)\n\nmodule \u00d7-ICMon = CommutativeMonoid \u00d7-ICommutativeMonoid\nmodule \u228e-ICMon = CommutativeMonoid \u228e-ICommutativeMonoid\nmodule \u00d7\u228e\u00b0I = CommutativeSemiring \u00d7\u228e-ICommutativeSemiring\n\nlift-\u228e : \u2200 {A B : Set} \u2192 Inv.Inverse ((\u2261.setoid A) \u228e-setoid (\u2261.setoid B)) (\u2261.setoid (A \u228e B))\nlift-\u228e {A}{B} = record\n { to = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = cong\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = \u03bb x \u2192 Setoid.reflexive (\u2261.setoid A \u228e-setoid \u2261.setoid B) x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid A \u228e-setoid \u2261.setoid B)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid (A \u228e B))\n }\n } where\n cong : Setoid._\u2248_ (\u2261.setoid A \u228e-setoid \u2261.setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (\u2261.setoid (A \u228e B))\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2261.cong inj\u2081 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2261.cong inj\u2082 x\u223c\u2082y\n\nswap-iso : \u2200 {A B} \u2192 (A \u00d7 B) \u2194 (B \u00d7 A)\nswap-iso = \u00d7-CMon.comm _ _\n\nMaybe\u2194\u22a4\u228e : \u2200 {a} {A : Set a} \u2192 Maybe A \u2194 (\u22a4 \u228e A)\nMaybe\u2194\u22a4\u228e\n = record { to = \u2192-to-\u27f6 (maybe inj\u2082 (inj\u2081 _))\n ; from = \u2192-to-\u27f6 [ F.const nothing , just ]\n ; inverse-of\n = record { left-inverse-of = maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ] } }\n\nMaybe-cong : \u2200 {a b} {A : Set a} {B : Set b} \u2192 A \u2194 B \u2192 Maybe A \u2194 Maybe B \nMaybe-cong A\u2194B = sym Maybe\u2194\u22a4\u228e \u2218 id \u228e-cong A\u2194B \u2218 Maybe\u2194\u22a4\u228e\n\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe : \u2200 {A} n \u2192 Maybe (Maybe^ n A) \u2194 Maybe^ n (Maybe A)\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe zero = id\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe (suc n) = Maybe-cong (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n)\n\nMaybe^-cong : \u2200 {A B} n \u2192 A \u2194 B \u2192 Maybe^ n A \u2194 Maybe^ n B\nMaybe^-cong zero = F.id\nMaybe^-cong (suc n) = Maybe-cong F.\u2218 Maybe^-cong n\n\nVec0\u2194\u22a4 : \u2200 {a} {A : Set a} \u2192 Vec A 0 \u2194 \u22a4\nVec0\u2194\u22a4 = record { to = \u2192-to-\u27f6 _\n ; from = \u2192-to-\u27f6 (F.const [])\n ; inverse-of = record { left-inverse-of = \u03bb { [] \u2192 \u2261.refl }\n ; right-inverse-of = \u03bb _ \u2192 \u2261.refl } }\n\nVec\u2218suc\u2194A\u00d7Vec : \u2200 {a} {A : Set a} {n} \u2192 Vec A (suc n) \u2194 (A \u00d7 Vec A n)\nVec\u2218suc\u2194A\u00d7Vec\n = record { to = \u2192-to-\u27f6 (\u03bb { (x \u2237 xs) \u2192 x , xs })\n ; from = \u2192-to-\u27f6 (uncurry _\u2237_)\n ; inverse-of = record { left-inverse-of = \u03bb { (x \u2237 xs) \u2192 \u2261.refl }\n ; right-inverse-of = \u03bb _ \u2192 \u2261.refl } }\n\ninfix 8 _^_\n\n_^_ : \u2605 \u2192 \u2115 \u2192 \u2605\nA ^ 0 = \u22a4\nA ^ suc n = A \u00d7 A ^ n\n\n^\u2194Vec : \u2200 {A} n \u2192 (A ^ n) \u2194 Vec A n\n^\u2194Vec zero = sym Vec0\u2194\u22a4\n^\u2194Vec (suc n) = sym Vec\u2218suc\u2194A\u00d7Vec \u2218 (id \u00d7-cong (^\u2194Vec n))\n\nFin0\u2194\u22a5 : Fin 0 \u2194 \u22a5\nFin0\u2194\u22a5 = record { to = \u2192-to-\u27f6 \u03bb()\n ; from = \u2192-to-\u27f6 \u03bb()\n ; inverse-of = record { left-inverse-of = \u03bb()\n ; right-inverse-of = \u03bb() } }\n\nFin\u2218suc\u2194Maybe\u2218Fin : \u2200 {n} \u2192 Fin (suc n) \u2194 Maybe (Fin n)\nFin\u2218suc\u2194Maybe\u2218Fin {n}\n = record { to = \u2192-to-\u27f6 to\n ; from = \u2192-to-\u27f6 (maybe suc zero)\n ; inverse-of\n = record { left-inverse-of = \u03bb { zero \u2192 \u2261.refl ; (suc n) \u2192 \u2261.refl }\n ; right-inverse-of = maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl } }\n where to : Fin (suc n) \u2192 Maybe (Fin n)\n to zero = nothing\n to (suc n) = just n\n\nLift\u2194id : \u2200 {a} {A : Set a} \u2192 Lift {a} {a} A \u2194 A\nLift\u2194id = record { to = \u2192-to-\u27f6 lower\n ; from = \u2192-to-\u27f6 lift\n ; inverse-of = record { left-inverse-of = \u03bb { (lift x) \u2192 \u2261.refl }\n ; right-inverse-of = \u03bb _ \u2192 \u2261.refl } }\n\n\u22a4\u00d7A\u2194A : \u2200 {A : \u2605} \u2192 (\u22a4 \u00d7 A) \u2194 A\n\u22a4\u00d7A\u2194A = proj\u2081 \u00d7-CMon.identity _ \u2218 sym Lift\u2194id \u00d7-cong id\n\nA\u00d7\u22a4\u2194A : \u2200 {A : \u2605} \u2192 (A \u00d7 \u22a4) \u2194 A\nA\u00d7\u22a4\u2194A = proj\u2082 \u00d7-CMon.identity _ \u2218 id \u00d7-cong sym Lift\u2194id\n\n\u22a5\u228eA\u2194A : \u2200 {A : \u2605} \u2192 (\u22a5 \u228e A) \u2194 A\n\u22a5\u228eA\u2194A = proj\u2081 \u228e-CMon.identity _ \u2218 sym Lift\u2194id \u228e-cong id\n\nA\u228e\u22a5\u2194A : \u2200 {A : \u2605} \u2192 (A \u228e \u22a5) \u2194 A\nA\u228e\u22a5\u2194A = proj\u2082 \u228e-CMon.identity _ \u2218 id \u228e-cong sym Lift\u2194id\n\n\u22a5\u00d7A\u2194\u22a5 : \u2200 {A : \u2605} \u2192 (\u22a5 \u00d7 A) \u2194 \u22a5\n\u22a5\u00d7A\u2194\u22a5 = Lift\u2194id \u2218 proj\u2081 \u00d7\u228e\u00b0.zero _ \u2218 sym (Lift\u2194id \u00d7-cong id)\n\nMaybe\u22a5\u2194\u22a4 : Maybe \u22a5 \u2194 \u22a4\nMaybe\u22a5\u2194\u22a4 = A\u228e\u22a5\u2194A \u2218 Maybe\u2194\u22a4\u228e\n\nMaybe^\u22a5\u2194Fin : \u2200 n \u2192 Maybe^ n \u22a5 \u2194 Fin n\nMaybe^\u22a5\u2194Fin zero = sym Fin0\u2194\u22a5\nMaybe^\u22a5\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\u22a5\u2194Fin n)\n\nMaybe^\u22a4\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \u22a4 \u2194 Fin (suc n)\nMaybe^\u22a4\u2194Fin1+ n = Maybe^\u22a5\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\u22a5\u2194\u22a4)\n\nMaybe-\u228e : \u2200 {A B : \u2605} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e = sym Maybe\u2194\u22a4\u228e \u2218 \u228e-CMon.assoc \u22a4 _ _ \u2218 (Maybe\u2194\u22a4\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \u22a5 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \u22a5\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n\nFin-\u228e-+ : \u2200 m n \u2192 (Fin m \u228e Fin n) \u2194 Fin (m + n)\nFin-\u228e-+ m n = Maybe^\u22a5\u2194Fin (m + n)\n \u2218 Maybe^-\u228e-+ m n\n \u2218 sym (Maybe^\u22a5\u2194Fin m \u228e-cong Maybe^\u22a5\u2194Fin n)\n\nFin\u2218suc\u2194\u22a4\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2194 (\u22a4 \u228e Fin n)\nFin\u2218suc\u2194\u22a4\u228eFin = Maybe\u2194\u22a4\u228e \u2218 Fin\u2218suc\u2194Maybe\u2218Fin\n\nFin-\u00d7-* : \u2200 m n \u2192 (Fin m \u00d7 Fin n) \u2194 Fin (m * n)\nFin-\u00d7-* zero n = (Fin 0 \u00d7 Fin n) \u2194\u27e8 Fin0\u2194\u22a5 \u00d7-cong id \u27e9 \n (\u22a5 \u00d7 Fin n) \u2194\u27e8 \u22a5\u00d7A\u2194\u22a5 \u27e9\n \u22a5 \u2194\u27e8 sym Fin0\u2194\u22a5 \u27e9\n Fin 0 \u220e\n where open EquationalReasoning hiding (sym)\nFin-\u00d7-* (suc m) n = (Fin (suc m) \u00d7 Fin n) \u2194\u27e8 Fin\u2218suc\u2194\u22a4\u228eFin \u00d7-cong id \u27e9\n ((\u22a4 \u228e Fin m) \u00d7 Fin n) \u2194\u27e8 \u00d7\u228e\u00b0.distrib\u02b3 (Fin n) \u22a4 (Fin m) \u27e9\n ((\u22a4 \u00d7 Fin n) \u228e (Fin m \u00d7 Fin n)) \u2194\u27e8 \u22a4\u00d7A\u2194A \u228e-cong Fin-\u00d7-* m n \u27e9\n (Fin n \u228e Fin (m * n)) \u2194\u27e8 Fin-\u228e-+ n (m * n) \u27e9\n Fin (suc m * n) \u220e\n where open EquationalReasoning hiding (sym)\n","old_contents":"module Function.Related.TypeIsomorphisms.NP where\n\nimport Level as L\nopen import Algebra\nimport Algebra.FunctionProperties as FP\nopen L using (Lift; lower; lift)\nopen import Type\n\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Vec using (Vec; []; _\u2237_)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_) renaming (_^_ to _**_)\nopen import Data.Maybe.NP\nopen import Data.Product renaming (map to map\u00d7)\n--open import Data.Product.N-ary\nopen import Data.Sum renaming (map to map\u228e)\nopen import Data.Unit\nopen import Data.Empty\n\nimport Function as F\nimport Function.Equality as FE\nopen FE using (_\u27e8$\u27e9_)\nopen import Function.Related as FR\nopen import Function.Related.TypeIsomorphisms public\nimport Function.Inverse as Inv\nopen Inv using (_\u2194_; _\u2218_; sym; id; module Inverse)\nopen import Relation.Binary\nopen import Relation.Binary.Product.Pointwise\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (\u2192-to-\u27f6)\n\n\nInv-isEquivalence : IsEquivalence (Inv.Inverse {f\u2081 = L.zero} {f\u2082 = L.zero})\nInv-isEquivalence = record\n { refl = Inv.id\n ; sym = Inv.sym\n ; trans = F.flip Inv._\u2218_ }\n\nSEToid : Set _\nSEToid = Setoid L.zero L.zero\n\nSEToid\u2081 : Setoid _ _\nSEToid\u2081 = record\n { Carrier = Setoid L.zero L.zero\n ; _\u2248_ = Inv.Inverse\n ; isEquivalence = Inv-isEquivalence }\n\n\u228e-ICommutativeMonoid : CommutativeMonoid _ _\n\u228e-ICommutativeMonoid = record {\n Carrier = SEToid ;\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u228e-setoid_;\n \u03b5 = \u2261.setoid \u22a5;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv-isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u228e-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \u22a5) _\u228e-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = [ (\u03bb ()) , F.id ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = inj\u2082\n ; cong = \u2082\u223c\u2082\n }\n ; inverse-of = record\n { left-inverse-of = [ (\u03bb ()) , (\u03bb x \u2192 \u2082\u223c\u2082 refl) ]\n ; right-inverse-of = \u03bb x \u2192 refl\n }\n } where open Setoid A\n cong-to : Setoid._\u2248_ (\u2261.setoid \u22a5 \u228e-setoid A) =[ _ ]\u21d2 _\u2248_\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y) rewrite x\u223c\u2081y = refl\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = x\u223c\u2082y\n\n assoc : Associative _\u228e-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = [ [ inj\u2081 , inj\u2082 F.\u2218 inj\u2081 ] , inj\u2082 F.\u2218 inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = [ inj\u2081 F.\u2218 inj\u2081 , [ inj\u2081 F.\u2218 inj\u2082 , inj\u2082 ] ]\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = [ [ (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2081\u223c\u2081 (refl A))) , (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2082\u223c\u2082 (refl B)) ) ] , (\u03bb _ \u2192 \u2082\u223c\u2082 (refl C)) ]\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (refl A)) , [ (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2081\u223c\u2081 (refl B))) , (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2082\u223c\u2082 (refl C))) ] ]\n }\n } where\n open Setoid\n cong-to : _\u2248_ ((A \u228e-setoid B) \u228e-setoid C) =[ _ ]\u21d2 _\u2248_ (A \u228e-setoid (B \u228e-setoid C))\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2082 ()))\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 x\u223c\u2081y\n cong-to (\u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2082y)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = \u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)\n\n cong-from : _\u2248_ (A \u228e-setoid (B \u228e-setoid C)) =[ _ ]\u21d2 _\u2248_ ((A \u228e-setoid B) \u228e-setoid C)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 x\u223c\u2081y) = \u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2082 ()))\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 x\u223c\u2082y\n\n comm : Commutative _\u228e-setoid_\n comm A B = record\n { to = swap'\n ; from = swap'\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B : SEToid} \u2192 A \u228e-setoid B FE.\u27f6 B \u228e-setoid A\n swap' {A} {B} = record\n { _\u27e8$\u27e9_ = [ inj\u2082 , inj\u2081 ]\n ; cong = cong\n } where\n cong : Setoid._\u2248_ (A \u228e-setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (B \u228e-setoid A)\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2082\u223c\u2082 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2081\u223c\u2081 x\u223c\u2082y\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u228e-setoid B) (swap' FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (Setoid.refl A)) , (\u03bb _ \u2192 \u2082\u223c\u2082 (Setoid.refl B)) ]\n\n\u00d7-ICommutativeMonoid : CommutativeMonoid _ _\n\u00d7-ICommutativeMonoid = record {\n Carrier = SEToid ;\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u00d7-setoid_;\n \u03b5 = \u2261.setoid \u22a4;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv-isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u00d7-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \u22a4) _\u00d7-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2082\n ; cong = proj\u2082\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 _ , x\n ; cong = \u03bb x \u2192 \u2261.refl , x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u2261.refl , (Setoid.refl A)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl A\n }\n }\n\n assoc : Associative _\u00d7-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n ; cong = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n }\n ; from = record\n { _\u27e8$\u27e9_ = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n ; cong = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb _ \u2192 Setoid.refl ((A \u00d7-setoid B) \u00d7-setoid C)\n ; right-inverse-of = \u03bb _ \u2192 Setoid.refl (A \u00d7-setoid (B \u00d7-setoid C))\n }\n }\n\n comm : Commutative _\u00d7-setoid_\n comm A B = record\n { to = swap' {A} {B}\n ; from = swap' {B} {A}\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B : SEToid} \u2192 A \u00d7-setoid B FE.\u27f6 B \u00d7-setoid A\n swap' {A}{B} = record { _\u27e8$\u27e9_ = swap; cong = swap }\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u00d7-setoid B) (swap' {B} {A} FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = \u03bb x \u2192 Setoid.refl (A \u00d7-setoid B)\n\n\u00d7\u228e-ICommutativeSemiring : CommutativeSemiring _ _\n\u00d7\u228e-ICommutativeSemiring = record\n { Carrier = SEToid\n ; _\u2248_ = Inv.Inverse\n ; _+_ = _\u228e-setoid_\n ; _*_ = _\u00d7-setoid_\n ; 0# = \u2261.setoid \u22a5\n ; 1# = \u2261.setoid \u22a4\n ; isCommutativeSemiring = record\n { +-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u228e-ICommutativeMonoid\n ; *-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u00d7-ICommutativeMonoid\n ; distrib\u02b3 = distrib\u02b3\n ; zero\u02e1 = zero\u02e1\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n distrib\u02b3 : _\u00d7-setoid_ DistributesOver\u02b3 _\u228e-setoid_\n distrib\u02b3 A B C = record\n { to = record\n { _\u27e8$\u27e9_ = uncurry [ curry inj\u2081 , curry inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = from\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = uncurry [ (\u03bb x y \u2192 (\u2081\u223c\u2081 (refl B)) , (refl A)) , (\u03bb x y \u2192 (\u2082\u223c\u2082 (refl C)) , (refl A)) ]\n ; right-inverse-of = [ uncurry (\u03bb x y \u2192 \u2081\u223c\u2081 ((refl B {x}) , (refl A {y})))\n , uncurry (\u03bb x y \u2192 \u2082\u223c\u2082 ((refl C {x}) , (refl A {y}))) ]\n }\n } where\n open Setoid\n A' = Carrier A\n B' = Carrier B\n C' = Carrier C\n\n cong-to : _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A) =[ _ ]\u21d2 _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A))\n cong-to (\u2081\u223c\u2082 () , _)\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y , A-rel) = \u2081\u223c\u2081 (x\u223c\u2081y , A-rel)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y , A-rel) = \u2082\u223c\u2082 (x\u223c\u2082y , A-rel)\n\n from : B' \u00d7 A' \u228e C' \u00d7 A' \u2192 (B' \u228e C') \u00d7 A'\n from = [ map\u00d7 inj\u2081 F.id , map\u00d7 inj\u2082 F.id ]\n\n cong-from : _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A)) =[ _ ]\u21d2 _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 (B-rel , A-rel)) = (\u2081\u223c\u2081 B-rel , A-rel)\n cong-from (\u2082\u223c\u2082 (C-rel , A-rel)) = \u2082\u223c\u2082 C-rel , A-rel\n\n zero\u02e1 : LeftZero (\u2261.setoid \u22a5) _\u00d7-setoid_\n zero\u02e1 A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2081\n ; cong = proj\u2081\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u22a5-elim\n ; cong = \u03bb { {()} x }\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u22a5-elim (proj\u2081 x)\n ; right-inverse-of = \u03bb x \u2192 \u22a5-elim x\n }\n }\n\nmodule \u00d7-CMon = CommutativeMonoid (\u00d7-CommutativeMonoid FR.bijection L.zero)\nmodule \u228e-CMon = CommutativeMonoid (\u228e-CommutativeMonoid FR.bijection L.zero)\nmodule \u00d7\u228e\u00b0 = CommutativeSemiring (\u00d7\u228e-CommutativeSemiring FR.bijection L.zero)\n\nmodule \u00d7-ICMon = CommutativeMonoid \u00d7-ICommutativeMonoid\nmodule \u228e-ICMon = CommutativeMonoid \u228e-ICommutativeMonoid\nmodule \u00d7\u228e\u00b0I = CommutativeSemiring \u00d7\u228e-ICommutativeSemiring\n\nswap-iso : \u2200 {A B} \u2192 (A \u00d7 B) \u2194 (B \u00d7 A)\nswap-iso = \u00d7-CMon.comm _ _\n\nMaybe\u2194\u22a4\u228e : \u2200 {a} {A : Set a} \u2192 Maybe A \u2194 (\u22a4 \u228e A)\nMaybe\u2194\u22a4\u228e\n = record { to = \u2192-to-\u27f6 (maybe inj\u2082 (inj\u2081 _))\n ; from = \u2192-to-\u27f6 [ F.const nothing , just ]\n ; inverse-of\n = record { left-inverse-of = maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ] } }\n\nMaybe-cong : \u2200 {a b} {A : Set a} {B : Set b} \u2192 A \u2194 B \u2192 Maybe A \u2194 Maybe B \nMaybe-cong A\u2194B = sym Maybe\u2194\u22a4\u228e \u2218 id \u228e-cong A\u2194B \u2218 Maybe\u2194\u22a4\u228e\n\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe : \u2200 {A} n \u2192 Maybe (Maybe^ n A) \u2194 Maybe^ n (Maybe A)\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe zero = id\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe (suc n) = Maybe-cong (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n)\n\nMaybe^-cong : \u2200 {A B} n \u2192 A \u2194 B \u2192 Maybe^ n A \u2194 Maybe^ n B\nMaybe^-cong zero = F.id\nMaybe^-cong (suc n) = Maybe-cong F.\u2218 Maybe^-cong n\n\nVec0\u2194\u22a4 : \u2200 {a} {A : Set a} \u2192 Vec A 0 \u2194 \u22a4\nVec0\u2194\u22a4 = record { to = \u2192-to-\u27f6 _\n ; from = \u2192-to-\u27f6 (F.const [])\n ; inverse-of = record { left-inverse-of = \u03bb { [] \u2192 \u2261.refl }\n ; right-inverse-of = \u03bb _ \u2192 \u2261.refl } }\n\nVec\u2218suc\u2194A\u00d7Vec : \u2200 {a} {A : Set a} {n} \u2192 Vec A (suc n) \u2194 (A \u00d7 Vec A n)\nVec\u2218suc\u2194A\u00d7Vec\n = record { to = \u2192-to-\u27f6 (\u03bb { (x \u2237 xs) \u2192 x , xs })\n ; from = \u2192-to-\u27f6 (uncurry _\u2237_)\n ; inverse-of = record { left-inverse-of = \u03bb { (x \u2237 xs) \u2192 \u2261.refl }\n ; right-inverse-of = \u03bb _ \u2192 \u2261.refl } }\n\ninfix 8 _^_\n\n_^_ : \u2605 \u2192 \u2115 \u2192 \u2605\nA ^ 0 = \u22a4\nA ^ suc n = A \u00d7 A ^ n\n\n^\u2194Vec : \u2200 {A} n \u2192 (A ^ n) \u2194 Vec A n\n^\u2194Vec zero = sym Vec0\u2194\u22a4\n^\u2194Vec (suc n) = sym Vec\u2218suc\u2194A\u00d7Vec \u2218 (id \u00d7-cong (^\u2194Vec n))\n\nFin0\u2194\u22a5 : Fin 0 \u2194 \u22a5\nFin0\u2194\u22a5 = record { to = \u2192-to-\u27f6 \u03bb()\n ; from = \u2192-to-\u27f6 \u03bb()\n ; inverse-of = record { left-inverse-of = \u03bb()\n ; right-inverse-of = \u03bb() } }\n\nFin\u2218suc\u2194Maybe\u2218Fin : \u2200 {n} \u2192 Fin (suc n) \u2194 Maybe (Fin n)\nFin\u2218suc\u2194Maybe\u2218Fin {n}\n = record { to = \u2192-to-\u27f6 to\n ; from = \u2192-to-\u27f6 (maybe suc zero)\n ; inverse-of\n = record { left-inverse-of = \u03bb { zero \u2192 \u2261.refl ; (suc n) \u2192 \u2261.refl }\n ; right-inverse-of = maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl } }\n where to : Fin (suc n) \u2192 Maybe (Fin n)\n to zero = nothing\n to (suc n) = just n\n\nLift\u2194id : \u2200 {a} {A : Set a} \u2192 Lift {a} {a} A \u2194 A\nLift\u2194id = record { to = \u2192-to-\u27f6 lower\n ; from = \u2192-to-\u27f6 lift\n ; inverse-of = record { left-inverse-of = \u03bb { (lift x) \u2192 \u2261.refl }\n ; right-inverse-of = \u03bb _ \u2192 \u2261.refl } }\n\n\u22a4\u00d7A\u2194A : \u2200 {A : \u2605} \u2192 (\u22a4 \u00d7 A) \u2194 A\n\u22a4\u00d7A\u2194A = proj\u2081 \u00d7-CMon.identity _ \u2218 sym Lift\u2194id \u00d7-cong id\n\nA\u00d7\u22a4\u2194A : \u2200 {A : \u2605} \u2192 (A \u00d7 \u22a4) \u2194 A\nA\u00d7\u22a4\u2194A = proj\u2082 \u00d7-CMon.identity _ \u2218 id \u00d7-cong sym Lift\u2194id\n\n\u22a5\u228eA\u2194A : \u2200 {A : \u2605} \u2192 (\u22a5 \u228e A) \u2194 A\n\u22a5\u228eA\u2194A = proj\u2081 \u228e-CMon.identity _ \u2218 sym Lift\u2194id \u228e-cong id\n\nA\u228e\u22a5\u2194A : \u2200 {A : \u2605} \u2192 (A \u228e \u22a5) \u2194 A\nA\u228e\u22a5\u2194A = proj\u2082 \u228e-CMon.identity _ \u2218 id \u228e-cong sym Lift\u2194id\n\n\u22a5\u00d7A\u2194\u22a5 : \u2200 {A : \u2605} \u2192 (\u22a5 \u00d7 A) \u2194 \u22a5\n\u22a5\u00d7A\u2194\u22a5 = Lift\u2194id \u2218 proj\u2081 \u00d7\u228e\u00b0.zero _ \u2218 sym (Lift\u2194id \u00d7-cong id)\n\nMaybe\u22a5\u2194\u22a4 : Maybe \u22a5 \u2194 \u22a4\nMaybe\u22a5\u2194\u22a4 = A\u228e\u22a5\u2194A \u2218 Maybe\u2194\u22a4\u228e\n\nMaybe^\u22a5\u2194Fin : \u2200 n \u2192 Maybe^ n \u22a5 \u2194 Fin n\nMaybe^\u22a5\u2194Fin zero = sym Fin0\u2194\u22a5\nMaybe^\u22a5\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\u22a5\u2194Fin n)\n\nMaybe^\u22a4\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \u22a4 \u2194 Fin (suc n)\nMaybe^\u22a4\u2194Fin1+ n = Maybe^\u22a5\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\u22a5\u2194\u22a4)\n\nMaybe-\u228e : \u2200 {A B : \u2605} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e = sym Maybe\u2194\u22a4\u228e \u2218 \u228e-CMon.assoc \u22a4 _ _ \u2218 (Maybe\u2194\u22a4\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \u22a5 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \u22a5\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n\nFin-\u228e-+ : \u2200 m n \u2192 (Fin m \u228e Fin n) \u2194 Fin (m + n)\nFin-\u228e-+ m n = Maybe^\u22a5\u2194Fin (m + n)\n \u2218 Maybe^-\u228e-+ m n\n \u2218 sym (Maybe^\u22a5\u2194Fin m \u228e-cong Maybe^\u22a5\u2194Fin n)\n\nFin\u2218suc\u2194\u22a4\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2194 (\u22a4 \u228e Fin n)\nFin\u2218suc\u2194\u22a4\u228eFin = Maybe\u2194\u22a4\u228e \u2218 Fin\u2218suc\u2194Maybe\u2218Fin\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"b41896e427242ac85988c84587d5f0432db092c3","subject":"most of the type assignment rules","message":"most of the type assignment rules\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"core.agda","new_file":"core.agda","new_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data \u03c4\u0307 : Set where\n b : \u03c4\u0307\n \u2987\u2988 : \u03c4\u0307\n _==>_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 \u03c4\u0307\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data e\u0307 : Set where\n c : e\u0307\n _\u00b7:_ : e\u0307 \u2192 \u03c4\u0307 \u2192 e\u0307\n X : Nat \u2192 e\u0307\n \u00b7\u03bb : Nat \u2192 e\u0307 \u2192 e\u0307\n \u00b7\u03bb_[_]_ : Nat \u2192 \u03c4\u0307 \u2192 e\u0307 \u2192 e\u0307\n \u2987\u2988[_] : Nat \u2192 e\u0307\n \u2987_\u2988[_] : e\u0307 \u2192 Nat \u2192 e\u0307\n _\u2218_ : e\u0307 \u2192 e\u0307 \u2192 e\u0307\n\n subst : Set -- todo: no idea if this is right\n subst = e\u0307 ctx\n\n -- expressions without ascriptions but with casts\n data \u00eb : Set where\n c : \u00eb\n X : Nat \u2192 \u00eb\n \u00b7\u03bb_[_]_ : Nat \u2192 \u03c4\u0307 \u2192 \u00eb \u2192 \u00eb\n \u2987\u2988[_] : (Nat \u00d7 subst) \u2192 \u00eb\n \u2987_\u2988[_] : \u00eb \u2192 (Nat \u00d7 subst) \u2192 \u00eb\n _\u2218_ : \u00eb \u2192 \u00eb \u2192 \u00eb\n <_>_ : \u00eb \u2192 \u03c4\u0307 \u2192 \u00eb\n\n -- type consistency\n data _~_ : (t1 : \u03c4\u0307) \u2192 (t2 : \u03c4\u0307) \u2192 Set where\n TCRefl : {t : \u03c4\u0307} \u2192 t ~ t\n TCHole1 : {t : \u03c4\u0307} \u2192 t ~ \u2987\u2988\n TCHole2 : {t : \u03c4\u0307} \u2192 \u2987\u2988 ~ t\n TCArr : {t1 t2 t1' t2' : \u03c4\u0307} \u2192\n t1 ~ t1' \u2192\n t2 ~ t2' \u2192\n t1 ==> t2 ~ t1' ==> t2'\n\n -- type inconsistency\n data _~\u0338_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n ICBaseArr1 : {t1 t2 : \u03c4\u0307} \u2192 b ~\u0338 t1 ==> t2\n ICBaseArr2 : {t1 t2 : \u03c4\u0307} \u2192 t1 ==> t2 ~\u0338 b\n ICArr1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t1 ~\u0338 t3 \u2192\n t1 ==> t2 ~\u0338 t3 ==> t4\n ICArr2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t2 ~\u0338 t4 \u2192\n t1 ==> t2 ~\u0338 t3 ==> t4\n\n --- matching for arrows, sums, and products\n data _\u25b8arr_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {t1 t2 : \u03c4\u0307} \u2192 t1 ==> t2 \u25b8arr t1 ==> t2\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = \u03c4\u0307 ctx\n\n hctx : Set\n hctx = (\u03c4\u0307 ctx \u00d7 \u03c4\u0307) ctx\n\n postulate -- todo: write this stuff later\n id : {A : Set} \u2192 A ctx \u2192 subst\n [_]_ : subst \u2192 \u00eb \u2192 \u00eb\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 \u03c4\u0307 \u2192 (Nat \u00d7 tctx \u00d7 \u03c4\u0307)\n u ::[ \u0393 ] t = u , \u0393 , t\n\n\n -- bidirectional type checking judgements for e\u0307\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) \u2192 (e : e\u0307) \u2192 (t : \u03c4\u0307) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : e\u0307} {t : \u03c4\u0307} \u2192\n \u0393 \u22a2 e <= t \u2192\n \u0393 \u22a2 (e \u00b7: t) => t\n SVar : {\u0393 : tctx} {t : \u03c4\u0307} {n : Nat} \u2192\n (n , t) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => t\n SAp : {\u0393 : tctx} {e1 e2 : e\u0307} {t t' t2 : \u03c4\u0307} \u2192\n \u0393 \u22a2 e1 => t \u2192\n t \u25b8arr t2 ==> t' \u2192\n \u0393 \u22a2 e2 <= t2 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => t'\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of n?\n SNEHole : {\u0393 : tctx} {e : e\u0307} {t : \u03c4\u0307} {u : Nat} \u2192 -- todo: uniqueness of n?\n \u0393 \u22a2 e => t \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : e\u0307} {t1 t2 : \u03c4\u0307} {x : Nat} \u2192\n x # \u0393 \u2192 -- todo\n (\u0393 ,, (x , t1)) \u22a2 e => t2 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ t1 ] e => t1 ==> t2\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : \u03c4\u0307 ctx) \u2192 (e : e\u0307) \u2192 (t : \u03c4\u0307) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : e\u0307} {t t' : \u03c4\u0307} \u2192\n \u0393 \u22a2 e => t' \u2192\n t ~ t' \u2192\n \u0393 \u22a2 e <= t\n ALam : {\u0393 : tctx} {e : e\u0307} {t t1 t2 : \u03c4\u0307} {x : Nat} \u2192\n x # \u0393 \u2192\n t \u25b8arr t1 ==> t2 \u2192\n (\u0393 ,, (x , t1)) \u22a2 e <= t2 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= t\n\n -- todo: do we care about completeness of e\u0307 or e-umlauts?\n -- those types without holes anywhere\n tcomplete : \u03c4\u0307 \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (t1 ==> t2) = tcomplete t1 \u00d7 tcomplete t2\n\n -- those expressions without holes anywhere\n ecomplete : e\u0307 \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: t) = ecomplete e1 \u00d7 tcomplete t\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ t ] e) = tcomplete t \u00d7 ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : e\u0307) (t : \u03c4\u0307) (e' : \u00eb) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x t} \u2192 (\u0393 ,, (x , t)) \u22a2 X x \u21d2 t ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x t1 t2 e e' \u0394 } \u2192\n (\u0393 ,, (x , t1)) \u22a2 e \u21d2 t2 ~> e' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ t1 ] e \u21d2 (t1 ==> t2) ~> \u00b7\u03bb x [ t1 ] e' \u22a3 \u2205\n\n -- todo: really ought to check disjointness of domains here ..\n ESAp1 : \u2200{\u0393 e1 e2 e2' e1' \u03941 t2 t1 \u03942} \u2192\n \u0393 \u22a2 e1 => \u2987\u2988 \u2192\n \u0393 \u22a2 e2 \u21d0 \u2987\u2988 ~> e2' :: t2 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u21d0 (t2 ==> \u2987\u2988) ~> e1' :: t1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u2987\u2988 ~> (< e1' > t2) \u2218 e2' \u22a3 (\u03941 \u222a \u03942)\n ESAp2 : \u2200{\u0393 e1 t2 t e1' e2' \u03941 \u03942 t2' e2} \u2192\n \u0393 \u22a2 e1 \u21d2 (t2 ==> t) ~> e1' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 t2 ~> e2' :: t2' \u22a3 \u03942 \u2192\n (t2 == t2' \u2192 \u22a5) \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 t ~> e1' \u2218 (< e2' > t2) \u22a3 (\u03941 \u222a \u03942)\n ESAp3 : \u2200{\u0393 e1 t e1' \u03941 e2 t2 e2' \u03942 } \u2192\n \u0393 \u22a2 e1 \u21d2 (t2 ==> t) ~> e1' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 t2 ~> e2' :: t2 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 t ~> e1' \u2218 e2' \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988[ u , id \u0393 ] \u22a3 \u27e6 u ::[ \u0393 ] \u2987\u2988 \u27e7\n ESNEHole : \u2200{ \u0393 e t e' u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 t ~> e' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 e' \u2988[ u , id \u0393 ] \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc1 : \u2200 {\u0393 e t e' t' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 t ~> e' :: t' \u22a3 \u0394 \u2192\n (t == t' \u2192 \u22a5) \u2192\n \u0393 \u22a2 (e \u00b7: t) \u21d2 t ~> (< e' > t) \u22a3 \u0394\n ESAsc2 : \u2200{\u0393 e t e' t' \u0394 } \u2192\n \u0393 \u22a2 e \u21d0 t ~> e' :: t' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: t) \u21d2 t ~> e' \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : e\u0307) (t : \u03c4\u0307) (e' : \u00eb) (t' : \u03c4\u0307)(\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x t1 t2 e e' t2' \u0394 } \u2192\n (\u0393 ,, (x , t1)) \u22a2 e \u21d0 t2 ~> e' :: t2' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 (t1 ==> t2) ~> \u00b7\u03bb x [ t1 ] e' :: (t1 ==> t2') \u22a3 \u0394\n EASubsume : \u2200{e u m \u0393 t' e' \u0394 t} \u2192\n (e == \u2987\u2988[ u ] \u2192 \u22a5) \u2192\n (e == \u2987 m \u2988[ u ] \u2192 \u22a5) \u2192\n \u0393 \u22a2 e \u21d2 t' ~> e' \u22a3 \u0394 \u2192\n t ~ t' \u2192\n \u0393 \u22a2 e \u21d0 t ~> e' :: t' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u t } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 t ~> \u2987\u2988[ u , id \u0393 ] :: t \u22a3 \u27e6 u ::[ \u0393 ] t \u27e7\n EANEHole : \u2200{ \u0393 e u t e' t' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 t' ~> e' \u22a3 \u0394 \u2192\n t ~ t' \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 t ~> \u2987 e' \u2988[ u , id \u0393 ] :: t \u22a3 (\u0394 ,, u ::[ \u0393 ] t)\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (e' : \u00eb) (t : \u03c4\u0307) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x t} \u2192 \u0394 , (\u0393 ,, (x , t)) \u22a2 X x :: t\n TALam : \u2200{ \u0394 \u0393 x t1 e t2} \u2192\n \u0394 , (\u0393 ,, (x , t1)) \u22a2 e :: t2 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ t1 ] e :: (t1 ==> t2)\n TAAp : \u2200{ \u0394 \u0393 e1 e2 t1 t2 t} \u2192\n \u0394 , \u0393 \u22a2 e1 :: t1 \u2192\n t1 \u25b8arr (t2 ==> t) \u2192\n \u0394 , \u0393 \u22a2 e2 :: t2 \u2192\n \u0394 , \u0393 \u22a2 e1 \u2218 e2 :: t\n -- TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' } \u2192 -- todo: overloaded form in the paper here\n -- TANEHole : \u2200 { \u0394 \u0393 e t' \u0393' u \u03c3 t }\n TACast : \u2200{ \u0394 \u0393 e t t'} \u2192\n \u0394 , \u0393 \u22a2 e :: t' \u2192\n t ~ t' \u2192\n \u0394 , \u0393 \u22a2 < e > t :: t\n\n -- todo: substition goes here\n\n -- value\n data _val : \u00eb \u2192 Set where\n VConst : c val\n VLam : \u2200{x t e} \u2192 (\u00b7\u03bb x [ t ] e) val\n\n -- error\n data _err[_] : \u00eb \u2192 hctx \u2192 Set where\n\n mutual\n -- indeterminate\n data _indet : \u00eb \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988[ u , \u03c3 ] indet\n INEHole : \u2200{e u \u03c3} \u2192 e final \u2192 \u2987 e \u2988[ u , \u03c3 ] indet\n IAp : \u2200{e1 e2} \u2192 e1 indet \u2192 e2 final \u2192 (e1 \u2218 e2) indet\n ICast : \u2200{e t} \u2192 e indet \u2192 (< e > t) indet\n\n -- final\n data _final : \u00eb \u2192 Set where\n FVal : \u2200{e} \u2192 e val \u2192 e final\n FIndet : \u2200{e} \u2192 e indet \u2192 e final\n\n -- small step semantics\n data _\u21a6_ : \u00eb \u2192 \u00eb \u2192 Set where\n","old_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data \u03c4\u0307 : Set where\n b : \u03c4\u0307\n \u2987\u2988 : \u03c4\u0307\n _==>_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 \u03c4\u0307\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data e\u0307 : Set where\n c : e\u0307\n _\u00b7:_ : e\u0307 \u2192 \u03c4\u0307 \u2192 e\u0307\n X : Nat \u2192 e\u0307\n \u00b7\u03bb : Nat \u2192 e\u0307 \u2192 e\u0307\n \u00b7\u03bb_[_]_ : Nat \u2192 \u03c4\u0307 \u2192 e\u0307 \u2192 e\u0307\n \u2987\u2988[_] : Nat \u2192 e\u0307\n \u2987_\u2988[_] : e\u0307 \u2192 Nat \u2192 e\u0307\n _\u2218_ : e\u0307 \u2192 e\u0307 \u2192 e\u0307\n\n subst : Set -- todo: no idea if this is right\n subst = e\u0307 ctx\n\n -- expressions without ascriptions but with casts\n data \u00eb : Set where\n c : \u00eb\n X : Nat \u2192 \u00eb\n \u00b7\u03bb_[_]_ : Nat \u2192 \u03c4\u0307 \u2192 \u00eb \u2192 \u00eb\n \u2987\u2988[_&_] : Nat \u2192 subst \u2192 \u00eb\n \u2987_\u2988[_&_] : \u00eb \u2192 Nat \u2192 subst \u2192 \u00eb\n _\u2218_ : \u00eb \u2192 \u00eb \u2192 \u00eb\n <_>_ : \u00eb \u2192 \u03c4\u0307 \u2192 \u00eb\n\n -- type consistency\n data _~_ : (t1 : \u03c4\u0307) \u2192 (t2 : \u03c4\u0307) \u2192 Set where\n TCRefl : {t : \u03c4\u0307} \u2192 t ~ t\n TCHole1 : {t : \u03c4\u0307} \u2192 t ~ \u2987\u2988\n TCHole2 : {t : \u03c4\u0307} \u2192 \u2987\u2988 ~ t\n TCArr : {t1 t2 t1' t2' : \u03c4\u0307} \u2192\n t1 ~ t1' \u2192\n t2 ~ t2' \u2192\n t1 ==> t2 ~ t1' ==> t2'\n\n -- type inconsistency\n data _~\u0338_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n ICBaseArr1 : {t1 t2 : \u03c4\u0307} \u2192 b ~\u0338 t1 ==> t2\n ICBaseArr2 : {t1 t2 : \u03c4\u0307} \u2192 t1 ==> t2 ~\u0338 b\n ICArr1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t1 ~\u0338 t3 \u2192\n t1 ==> t2 ~\u0338 t3 ==> t4\n ICArr2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t2 ~\u0338 t4 \u2192\n t1 ==> t2 ~\u0338 t3 ==> t4\n\n --- matching for arrows, sums, and products\n data _\u25b8arr_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {t1 t2 : \u03c4\u0307} \u2192 t1 ==> t2 \u25b8arr t1 ==> t2\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = \u03c4\u0307 ctx\n\n hctx : Set\n hctx = (\u03c4\u0307 ctx \u00d7 \u03c4\u0307) ctx\n\n postulate -- todo: write this stuff later\n id : {A : Set} \u2192 A ctx \u2192 subst\n [_]_ : subst \u2192 \u00eb \u2192 \u00eb\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 \u03c4\u0307 \u2192 (Nat \u00d7 tctx \u00d7 \u03c4\u0307)\n u ::[ \u0393 ] t = u , \u0393 , t\n\n\n -- bidirectional type checking judgements for e\u0307\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) \u2192 (e : e\u0307) \u2192 (t : \u03c4\u0307) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : e\u0307} {t : \u03c4\u0307} \u2192\n \u0393 \u22a2 e <= t \u2192\n \u0393 \u22a2 (e \u00b7: t) => t\n SVar : {\u0393 : tctx} {t : \u03c4\u0307} {n : Nat} \u2192\n (n , t) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => t\n SAp : {\u0393 : tctx} {e1 e2 : e\u0307} {t t' t2 : \u03c4\u0307} \u2192\n \u0393 \u22a2 e1 => t \u2192\n t \u25b8arr t2 ==> t' \u2192\n \u0393 \u22a2 e2 <= t2 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => t'\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of n?\n SNEHole : {\u0393 : tctx} {e : e\u0307} {t : \u03c4\u0307} {u : Nat} \u2192 -- todo: uniqueness of n?\n \u0393 \u22a2 e => t \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : e\u0307} {t1 t2 : \u03c4\u0307} {x : Nat} \u2192\n x # \u0393 \u2192 -- todo\n (\u0393 ,, (x , t1)) \u22a2 e => t2 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ t1 ] e => t1 ==> t2\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : \u03c4\u0307 ctx) \u2192 (e : e\u0307) \u2192 (t : \u03c4\u0307) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : e\u0307} {t t' : \u03c4\u0307} \u2192\n \u0393 \u22a2 e => t' \u2192\n t ~ t' \u2192\n \u0393 \u22a2 e <= t\n ALam : {\u0393 : tctx} {e : e\u0307} {t t1 t2 : \u03c4\u0307} {x : Nat} \u2192\n x # \u0393 \u2192\n t \u25b8arr t1 ==> t2 \u2192\n (\u0393 ,, (x , t1)) \u22a2 e <= t2 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= t\n\n -- todo: do we care about completeness of e\u0307 or e-umlauts?\n -- those types without holes anywhere\n tcomplete : \u03c4\u0307 \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (t1 ==> t2) = tcomplete t1 \u00d7 tcomplete t2\n\n -- those expressions without holes anywhere\n ecomplete : e\u0307 \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: t) = ecomplete e1 \u00d7 tcomplete t\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ t ] e) = tcomplete t \u00d7 ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : e\u0307) (t : \u03c4\u0307) (e' : \u00eb) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x t} \u2192 (\u0393 ,, (x , t)) \u22a2 X x \u21d2 t ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x t1 t2 e e' \u0394 } \u2192\n (\u0393 ,, (x , t1)) \u22a2 e \u21d2 t2 ~> e' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ t1 ] e \u21d2 (t1 ==> t2) ~> \u00b7\u03bb x [ t1 ] e' \u22a3 \u2205\n\n -- todo: really ought to check disjointness of domains here ..\n ESAp1 : \u2200{\u0393 e1 e2 e2' e1' \u03941 t2 t1 \u03942} \u2192\n \u0393 \u22a2 e1 => \u2987\u2988 \u2192\n \u0393 \u22a2 e2 \u21d0 \u2987\u2988 ~> e2' :: t2 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u21d0 (t2 ==> \u2987\u2988) ~> e1' :: t1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u2987\u2988 ~> (< e1' > t2) \u2218 e2' \u22a3 (\u03941 \u222a \u03942)\n ESAp2 : \u2200{\u0393 e1 t2 t e1' e2' \u03941 \u03942 t2' e2} \u2192\n \u0393 \u22a2 e1 \u21d2 (t2 ==> t) ~> e1' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 t2 ~> e2' :: t2' \u22a3 \u03942 \u2192\n (t2 == t2' \u2192 \u22a5) \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 t ~> e1' \u2218 (< e2' > t2) \u22a3 (\u03941 \u222a \u03942)\n ESAp3 : \u2200{\u0393 e1 t e1' \u03941 e2 t2 e2' \u03942 } \u2192\n \u0393 \u22a2 e1 \u21d2 (t2 ==> t) ~> e1' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 t2 ~> e2' :: t2 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 t ~> e1' \u2218 e2' \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988[ u & id \u0393 ] \u22a3 \u27e6 u ::[ \u0393 ] \u2987\u2988 \u27e7\n ESNEHole : \u2200{ \u0393 e t e' u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 t ~> e' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 e' \u2988[ u & id \u0393 ] \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc1 : \u2200 {\u0393 e t e' t' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 t ~> e' :: t' \u22a3 \u0394 \u2192\n (t == t' \u2192 \u22a5) \u2192\n \u0393 \u22a2 (e \u00b7: t) \u21d2 t ~> (< e' > t) \u22a3 \u0394\n ESAsc2 : \u2200{\u0393 e t e' t' \u0394 } \u2192\n \u0393 \u22a2 e \u21d0 t ~> e' :: t' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: t) \u21d2 t ~> e' \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : e\u0307) (t : \u03c4\u0307) (e' : \u00eb) (t' : \u03c4\u0307)(\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x t1 t2 e e' t2' \u0394 } \u2192\n (\u0393 ,, (x , t1)) \u22a2 e \u21d0 t2 ~> e' :: t2' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 (t1 ==> t2) ~> \u00b7\u03bb x [ t1 ] e' :: (t1 ==> t2') \u22a3 \u0394\n EASubsume : \u2200{e u m \u0393 t' e' \u0394 t} \u2192\n (e == \u2987\u2988[ u ] \u2192 \u22a5) \u2192\n (e == \u2987 m \u2988[ u ] \u2192 \u22a5) \u2192\n \u0393 \u22a2 e \u21d2 t' ~> e' \u22a3 \u0394 \u2192\n t ~ t' \u2192\n \u0393 \u22a2 e \u21d0 t ~> e' :: t' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u t } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 t ~> \u2987\u2988[ u & id \u0393 ] :: t \u22a3 \u27e6 u ::[ \u0393 ] t \u27e7\n EANEHole : \u2200{ \u0393 e u t e' t' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 t' ~> e' \u22a3 \u0394 \u2192\n t ~ t' \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 t ~> \u2987 e' \u2988[ u & id \u0393 ] :: t \u22a3 (\u0394 ,, u ::[ \u0393 ] t)\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (e' : \u00eb) (t : \u03c4\u0307) \u2192 Set where\n\n -- value\n data _val : \u00eb \u2192 Set where\n VConst : c val\n VLam : \u2200{x t e} \u2192 (\u00b7\u03bb x [ t ] e) val\n\n -- error\n data _err[_] : \u00eb \u2192 hctx \u2192 Set where -- todo not a context\n\n mutual\n -- indeterminate\n data _indet : \u00eb \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988[ u & \u03c3 ] indet\n INEHole : \u2200{e u \u03c3} \u2192 e final \u2192 \u2987 e \u2988[ u & \u03c3 ] indet\n IAp : \u2200{e1 e2} \u2192 e1 indet \u2192 e2 final \u2192 (e1 \u2218 e2) indet\n ICast : \u2200{e t} \u2192 e indet \u2192 (< e > t) indet\n\n -- final\n data _final : \u00eb \u2192 Set where\n FVal : \u2200{e} \u2192 e val \u2192 e final\n FIndet : \u2200{e} \u2192 e indet \u2192 e final\n\n -- small step semantics\n data _\u21a6_ : \u00eb \u2192 \u00eb \u2192 Set where\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9bfafe6e37ae77bcf7e5a7466815272929d3c877","subject":"More Squared proofs","message":"More Squared proofs\n","repos":"DSLsofMath\/DSLsofMath","old_file":"FLABloM\/SquareMatrix.agda","new_file":"FLABloM\/SquareMatrix.agda","new_contents":"-- Matrices indexed by shape\nmodule SquareMatrix where\n\nopen import Shape\nopen import Matrix\n\n-- Square matrices have the same shape in both dimensions\nSq : Set \u2192 Shape \u2192 Set\nSq a shape = M a shape shape\n\nopen import SemiNearRingRecord\nopen import Algebra.Structures\nopen import Relation.Binary\nopen import Data.Product\n\nopen import Relation.Binary.PropositionalEquality hiding (trans; sym) renaming (refl to refl-\u2261)\nimport Relation.Binary.EqReasoning as EqReasoning\n\n\n-- Lifting a SNR to a to a Square matrix of some shape\nSquare : SemiNearRing \u2192 Shape \u2192 SemiNearRing\nSquare snr shape = SNR\n where\n open SemiNearRing snr\n\n S = Sq s shape\n\n open Operations s _\u2219\u209b_ _+\u209b_\n renaming (_+_ to _+S_; _*_ to _\u2219S_)\n\n 0S : (r c : Shape) \u2192 M s r c\n 0S L L = One 0\u209b\n 0S L (B s s\u2081) = Row (0S L s) (0S L s\u2081)\n 0S (B r r\u2081) L = Col (0S r L) (0S r\u2081 L)\n 0S (B r r\u2081) (B s s\u2081) =\n Q (0S r s) (0S r s\u2081)\n (0S r\u2081 s) (0S r\u2081 s\u2081)\n\n \u2243S : (r c : Shape) \u2192\n M s r c \u2192 M s r c \u2192 Set\n \u2243S L L (One x) (One x\u2081) = x \u2243\u209b x\u2081\n \u2243S L (B c\u2081 c\u2082) (Row m m\u2081) (Row n n\u2081) = \u2243S L c\u2081 m n \u00d7 \u2243S L c\u2082 m\u2081 n\u2081\n \u2243S (B r\u2081 r\u2082) L (Col m m\u2081) (Col n n\u2081) = \u2243S r\u2081 L m n \u00d7 \u2243S r\u2082 L m\u2081 n\u2081\n \u2243S (B r\u2081 r\u2082) (B c\u2081 c\u2082) (Q m00 m01 m10 m11) (Q n00 n01 n10 n11) =\n \u2243S r\u2081 c\u2081 m00 n00 \u00d7 \u2243S r\u2081 c\u2082 m01 n01 \u00d7\n \u2243S r\u2082 c\u2081 m10 n10 \u00d7 \u2243S r\u2082 c\u2082 m11 n11\n\n\n _\u2243S'_ : {r c : Shape} \u2192 M s r c \u2192 M s r c \u2192 Set\n _\u2243S'_ {r} {c} m n = \u2243S r c m n\n\n\n reflS : (r c : Shape) \u2192\n {X : M s r c} \u2192 X \u2243S' X\n reflS L L {X = One x} = refl\u209b {x}\n reflS L (B c\u2081 c\u2082) {X = Row X Y} = reflS L c\u2081 , reflS L c\u2082\n reflS (B r\u2081 r\u2082) L {X = Col X Y} = reflS r\u2081 L , reflS r\u2082 L\n reflS (B r\u2081 r\u2082) (B c\u2081 c\u2082) {X = Q X Y Z W} =\n reflS r\u2081 c\u2081 , reflS r\u2081 c\u2082 ,\n reflS r\u2082 c\u2081 , reflS r\u2082 c\u2082\n\n symS : (r c : Shape) \u2192\n {i j : M s r c} \u2192 i \u2243S' j \u2192 j \u2243S' i\n symS L L {One x} {One x\u2081} p = sym\u209b p\n symS L (B c\u2081 c\u2082) {Row i\u2081 i\u2082} {Row j\u2081 j\u2082} (p , q) = symS L c\u2081 p , symS L c\u2082 q\n symS (B r\u2081 r\u2082) L {Col i\u2081 i} {Col j j\u2081} (p , q) = symS r\u2081 L p , symS r\u2082 L q\n symS (B r r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j j\u2081 j\u2082 j\u2083} (p , q , x , y) =\n symS r c\u2081 p , symS r c\u2082 q ,\n symS r\u2082 c\u2081 x , symS r\u2082 c\u2082 y\n\n transS : (r c : Shape) \u2192\n {i j k : M s r c} \u2192 i \u2243S' j \u2192 j \u2243S' k \u2192 i \u2243S' k\n transS L L {One x} {One x\u2081} {One x\u2082} p q = trans p q\n transS L (B c\u2081 c\u2082) {Row i i\u2081} {Row j j\u2081} {Row k k\u2081} (p , q) (p' , q') =\n transS L c\u2081 p p' , transS L c\u2082 q q'\n transS (B r\u2081 r\u2082) L {Col i i\u2081} {Col j j\u2081} {Col k k\u2081} (p , q) (p' , q') =\n transS r\u2081 L p p' , transS r\u2082 L q q'\n transS (B r\u2081 r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j\u2082 j j\u2083 j\u2081} {Q k k\u2081 k\u2082 k\u2083}\n (p , q , x , y) (p' , q' , x' , y') =\n transS r\u2081 c\u2081 p p' , transS r\u2081 c\u2082 q q' ,\n transS r\u2082 c\u2081 x x' , transS r\u2082 c\u2082 y y'\n\n isEquivS =\n record\n { refl = reflS shape shape\n ; sym = symS shape shape\n ; trans = transS shape shape }\n\n liftAssoc : (r c : Shape) (x y z : M s r c) \u2192 ((x +S y) +S z) \u2243S' (x +S (y +S z))\n liftAssoc L L (One x) (One y) (One z) = assoc\u209b x y z\n liftAssoc L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) (Row z z\u2081) =\n liftAssoc L c x y z , liftAssoc L c\u2081 x\u2081 y\u2081 z\u2081\n liftAssoc (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) (Col z z\u2081) =\n liftAssoc r L x y z , liftAssoc r\u2081 L x\u2081 y\u2081 z\u2081\n liftAssoc (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n (liftAssoc r c x y z) , (liftAssoc r c\u2081 x\u2081 y\u2081 z\u2081) ,\n (liftAssoc r\u2081 c x\u2082 y\u2082 z\u2082) , (liftAssoc r\u2081 c\u2081 x\u2083 y\u2083 z\u2083)\n\n <+S> : (r c : Shape) {x y u v : M s r c} \u2192\n x \u2243S' y \u2192 u \u2243S' v \u2192 (x +S u) \u2243S' (y +S v)\n <+S> L L {One x} {One x\u2081} {One x\u2082} {One x\u2083} p q = p <+> q\n <+S> L (B c c\u2081) {Row x x\u2081} {Row y y\u2081} {Row u u\u2081} {Row v v\u2081} (p , p\u2081) (q , q\u2081) =\n <+S> L c p q , <+S> L c\u2081 p\u2081 q\u2081\n <+S> (B r r\u2081) L {Col x x\u2081} {Col y y\u2081} {Col u u\u2081} {Col v v\u2081} (p , p\u2081) (q , q\u2081) =\n <+S> r L p q , <+S> r\u2081 L p\u2081 q\u2081\n <+S> (B r r\u2081) (B c c\u2081) {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083}\n (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081 , q\u2082 , q\u2083) =\n <+S> r c p q , <+S> r c\u2081 p\u2081 q\u2081 ,\n <+S> r\u2081 c p\u2082 q\u2082 , <+S> r\u2081 c\u2081 p\u2083 q\u2083\n\n isSemgroupS =\n record\n { isEquivalence = isEquivS\n ; assoc = liftAssoc shape shape\n ; \u2219-cong = <+S> shape shape }\n\n\n identS\u02e1 : (r c : Shape) (x : M s r c) \u2192\n 0S r c +S x \u2243S' x\n identS\u02e1 L L (One x) = identity\u02e1\u209b x\n identS\u02e1 L (B c c\u2081) (Row x x\u2081) = identS\u02e1 L c x , identS\u02e1 L c\u2081 x\u2081\n identS\u02e1 (B r r\u2081) L (Col x x\u2081) = identS\u02e1 r L x , identS\u02e1 r\u2081 L x\u2081\n identS\u02e1 (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n identS\u02e1 r c x , identS\u02e1 r c\u2081 x\u2081 ,\n identS\u02e1 r\u2081 c x\u2082 , identS\u02e1 r\u2081 c\u2081 x\u2083\n\n liftComm : (r c : Shape) \u2192 (x y : M s r c) \u2192\n (x +S y) \u2243S' (y +S x)\n liftComm L L (One x) (One x\u2081) = comm\u209b x x\u2081\n liftComm L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) = (liftComm L c x y) , (liftComm L c\u2081 x\u2081 y\u2081)\n liftComm (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) = (liftComm r L x y) , (liftComm r\u2081 L x\u2081 y\u2081)\n liftComm (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) =\n liftComm r c x y , liftComm r c\u2081 x\u2081 y\u2081 ,\n liftComm r\u2081 c x\u2082 y\u2082 , liftComm r\u2081 c\u2081 x\u2083 y\u2083\n\n isCommMonS =\n record\n { isSemigroup = isSemgroupS\n ; identity\u02e1 = identS\u02e1 shape shape\n ; comm = liftComm shape shape }\n\n\n setoidS : (r c : Shape) \u2192 Setoid _ _\n setoidS r c =\n record\n { Carrier = M s r c\n ; _\u2248_ = \u2243S r c\n ; isEquivalence =\n record\n { refl = reflS r c ; sym = symS r c ; trans = transS r c } }\n\n\n zeroS\u02e1 : (a b c : Shape) (x : M s b c) \u2192\n (0S a b \u2219S x) \u2243S' 0S a c\n zeroS\u02e1 L L L (One x) = zero\u02e1 x\n zeroS\u02e1 L L (B c c\u2081) (Row x x\u2081) = (zeroS\u02e1 L L c x) , (zeroS\u02e1 L L c\u2081 x\u2081)\n zeroS\u02e1 L (B b b\u2081) L (Col x x\u2081) =\n let\n open EqReasoning (setoidS L L)\n ih = zeroS\u02e1 L b L x\n ih\u2081 = zeroS\u02e1 L b\u2081 L x\u2081\n in begin\n (Row (0S L b) (0S L b\u2081) \u2219S (Col x x\u2081))\n \u2261\u27e8 refl-\u2261 \u27e9\n (0S L b) \u2219S x +S (0S L b\u2081) \u2219S x\u2081\n \u2248\u27e8 <+S> L L {0S L b \u2219S x} {0S L L} {0S L b\u2081 \u2219S x\u2081} {0S L L} ih ih\u2081 \u27e9\n 0S L L +S 0S L L\n \u2248\u27e8 identS\u02e1 L L (0S L L) \u27e9\n 0S L L\n \u220e\n zeroS\u02e1 L (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (let\n open EqReasoning (setoidS L c)\n ih = zeroS\u02e1 L b c x\n ih\u2081 = zeroS\u02e1 L b\u2081 c x\u2082\n in begin\n 0S L b \u2219S x +S 0S L b\u2081 \u2219S x\u2082\n \u2248\u27e8 <+S> L c ih ih\u2081 \u27e9\n 0S L c +S 0S L c\n \u2248\u27e8 identS\u02e1 L c (0S L c) \u27e9\n 0S L c\n \u220e) ,\n (let\n open EqReasoning (setoidS L c\u2081)\n ih = zeroS\u02e1 L b c\u2081 x\u2081\n ih\u2081 = zeroS\u02e1 L b\u2081 c\u2081 x\u2083\n in begin\n 0S L b \u2219S x\u2081 +S 0S L b\u2081 \u2219S x\u2083\n \u2248\u27e8 <+S> L c\u2081 ih ih\u2081 \u27e9\n 0S L c\u2081 +S 0S L c\u2081\n \u2248\u27e8 identS\u02e1 L c\u2081 _ \u27e9\n 0S L c\u2081\n \u220e)\n zeroS\u02e1 (B a a\u2081) L L (One x) = zeroS\u02e1 a L L (One x) , zeroS\u02e1 a\u2081 L L (One x)\n zeroS\u02e1 (B a a\u2081) L (B c c\u2081) (Row x x\u2081) =\n zeroS\u02e1 a L c x , zeroS\u02e1 a L c\u2081 x\u2081 ,\n zeroS\u02e1 a\u2081 L c x , zeroS\u02e1 a\u2081 L c\u2081 x\u2081\n zeroS\u02e1 (B a a\u2081) (B b b\u2081) L (Col x x\u2081) =\n (let\n open EqReasoning (setoidS a L)\n ih = zeroS\u02e1 a b L x\n ih\u2081 = zeroS\u02e1 a b\u2081 L x\u2081\n in begin\n 0S a b \u2219S x +S 0S a b\u2081 \u2219S x\u2081\n \u2248\u27e8 <+S> a L ih ih\u2081 \u27e9\n 0S a L +S 0S a L\n \u2248\u27e8 identS\u02e1 a L _ \u27e9\n 0S a L\n \u220e) ,\n (let\n open EqReasoning (setoidS a\u2081 L)\n ih = zeroS\u02e1 a\u2081 b L x\n ih\u2081 = zeroS\u02e1 a\u2081 b\u2081 L x\u2081\n in begin\n 0S a\u2081 b \u2219S x +S 0S a\u2081 b\u2081 \u2219S x\u2081\n \u2248\u27e8 <+S> a\u2081 L ih ih\u2081 \u27e9\n 0S a\u2081 L +S 0S a\u2081 L\n \u2248\u27e8 identS\u02e1 a\u2081 L _ \u27e9\n 0S a\u2081 L\n \u220e)\n zeroS\u02e1 (B a a\u2081) (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (let\n open EqReasoning (setoidS a c)\n ih = zeroS\u02e1 a b c x\n ih\u2081 = zeroS\u02e1 a b\u2081 c x\u2082\n in begin\n 0S a b \u2219S x +S 0S a b\u2081 \u2219S x\u2082\n \u2248\u27e8 <+S> a c ih ih\u2081 \u27e9\n 0S a c +S 0S a c\n \u2248\u27e8 identS\u02e1 a c _ \u27e9\n 0S a c\n \u220e) ,\n (let\n open EqReasoning (setoidS a c\u2081)\n ih = zeroS\u02e1 a b c\u2081 x\u2081\n ih\u2081 = zeroS\u02e1 a b\u2081 c\u2081 x\u2083\n in begin\n 0S a b \u2219S x\u2081 +S 0S a b\u2081 \u2219S x\u2083\n \u2248\u27e8 <+S> a c\u2081 ih ih\u2081 \u27e9\n 0S a c\u2081 +S 0S a c\u2081\n \u2248\u27e8 identS\u02e1 a c\u2081 _ \u27e9\n 0S a c\u2081\n \u220e) ,\n (let\n open EqReasoning (setoidS a\u2081 c)\n ih = zeroS\u02e1 a\u2081 b c x\n ih\u2081 = zeroS\u02e1 a\u2081 b\u2081 c x\u2082\n in begin\n 0S a\u2081 b \u2219S x +S 0S a\u2081 b\u2081 \u2219S x\u2082\n \u2248\u27e8 <+S> a\u2081 c ih ih\u2081 \u27e9\n 0S a\u2081 c +S 0S a\u2081 c\n \u2248\u27e8 identS\u02e1 a\u2081 c _ \u27e9\n 0S a\u2081 c\n \u220e) ,\n (let\n open EqReasoning (setoidS a\u2081 c\u2081)\n ih = zeroS\u02e1 a\u2081 b c\u2081 x\u2081\n ih\u2081 = zeroS\u02e1 a\u2081 b\u2081 c\u2081 x\u2083\n in begin\n 0S a\u2081 b \u2219S x\u2081 +S 0S a\u2081 b\u2081 \u2219S x\u2083\n \u2248\u27e8 <+S> a\u2081 c\u2081 ih ih\u2081 \u27e9\n 0S a\u2081 c\u2081 +S 0S a\u2081 c\u2081\n \u2248\u27e8 identS\u02e1 a\u2081 c\u2081 _ \u27e9\n 0S a\u2081 c\u2081\n \u220e)\n\n zeroS\u02b3 : (a b c : Shape) (x : M s a b) \u2192\n (x \u2219S 0S b c) \u2243S' 0S a c\n zeroS\u02b3 L L L (One x) = zero\u02b3 x\n zeroS\u02b3 L L (B c c\u2081) (One x) =\n (zeroS\u02b3 L L c (One x)) , (zeroS\u02b3 L L c\u2081 (One x))\n zeroS\u02b3 L (B b b\u2081) L (Row x x\u2081) =\n let\n open EqReasoning (setoidS L L)\n ih = zeroS\u02b3 L b L x\n ih\u2081 = zeroS\u02b3 L b\u2081 L x\u2081\n in begin\n Row x x\u2081 \u2219S Col (0S b L) (0S b\u2081 L)\n \u2261\u27e8 refl-\u2261 \u27e9\n (x \u2219S 0S b L) +S (x\u2081 \u2219S 0S b\u2081 L)\n \u2248\u27e8 <+S> L L {x \u2219S 0S b L} {0S L L} {x\u2081 \u2219S 0S b\u2081 L} {0S L L} ih ih\u2081 \u27e9\n 0S L L +S 0S L L\n \u2248\u27e8 identS\u02e1 L L (0S L L) \u27e9\n 0S L L\n \u220e\n zeroS\u02b3 L (B b b\u2081) (B c c\u2081) (Row x x\u2081) =\n (let\n open EqReasoning (setoidS L c)\n ih = zeroS\u02b3 L b c x\n ih\u2081 = zeroS\u02b3 L b\u2081 c x\u2081\n in begin\n x \u2219S 0S b c +S x\u2081 \u2219S 0S b\u2081 c\n \u2248\u27e8 <+S> L c ih ih\u2081 \u27e9\n 0S L c +S 0S L c\n \u2248\u27e8 identS\u02e1 L c (0S L c) \u27e9\n 0S L c\n \u220e) ,\n (let\n open EqReasoning (setoidS L c\u2081)\n ih = zeroS\u02b3 L b c\u2081 x\n ih\u2081 = zeroS\u02b3 L b\u2081 c\u2081 x\u2081\n in begin\n x \u2219S 0S b c\u2081 +S x\u2081 \u2219S 0S b\u2081 c\u2081\n \u2248\u27e8 <+S> L c\u2081 ih ih\u2081 \u27e9\n 0S L c\u2081 +S 0S L c\u2081\n \u2248\u27e8 identS\u02e1 L c\u2081 (0S L c\u2081) \u27e9\n 0S L c\u2081\n \u220e)\n zeroS\u02b3 (B a a\u2081) L L (Col x x\u2081) = zeroS\u02b3 a L L x , zeroS\u02b3 a\u2081 L L x\u2081\n zeroS\u02b3 (B a a\u2081) L (B c c\u2081) (Col x x\u2081) =\n zeroS\u02b3 a L c x ,\n zeroS\u02b3 a L c\u2081 x ,\n zeroS\u02b3 a\u2081 L c x\u2081 ,\n zeroS\u02b3 a\u2081 L c\u2081 x\u2081\n zeroS\u02b3 (B a a\u2081) (B b b\u2081) L (Q x x\u2081 x\u2082 x\u2083) =\n (let\n open EqReasoning (setoidS _ _)\n ih = zeroS\u02b3 a b L x\n ih\u2081 = zeroS\u02b3 a b\u2081 L x\u2081\n in begin\n x \u2219S 0S b L +S x\u2081 \u2219S 0S b\u2081 L\n \u2248\u27e8 <+S> a L ih ih\u2081 \u27e9\n 0S a L +S 0S a L\n \u2248\u27e8 identS\u02e1 a L (0S _ _) \u27e9\n 0S a L\n \u220e) ,\n (let\n open EqReasoning (setoidS _ _)\n ih = zeroS\u02b3 a\u2081 b L x\u2082\n ih\u2081 = zeroS\u02b3 a\u2081 b\u2081 L x\u2083\n in begin\n x\u2082 \u2219S 0S b L +S x\u2083 \u2219S 0S b\u2081 L\n \u2248\u27e8 <+S> a\u2081 L ih ih\u2081 \u27e9\n 0S _ _ +S 0S _ _\n \u2248\u27e8 identS\u02e1 a\u2081 L (0S _ _) \u27e9\n 0S a\u2081 L\n \u220e)\n zeroS\u02b3 (B a a\u2081) (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (let\n open EqReasoning (setoidS _ _)\n ih = zeroS\u02b3 a b c x\n ih\u2081 = zeroS\u02b3 a b\u2081 c x\u2081\n in begin\n x \u2219S 0S b c +S x\u2081 \u2219S 0S b\u2081 c\n \u2248\u27e8 <+S> a c ih ih\u2081 \u27e9\n 0S _ _ +S 0S _ _\n \u2248\u27e8 identS\u02e1 a c (0S _ _) \u27e9\n 0S _ _\n \u220e) ,\n (let\n -- open EqReasoning (setoidS _ _)\n ih = zeroS\u02b3 a b c\u2081 x\n ih\u2081 = zeroS\u02b3 a b\u2081 c\u2081 x\u2081\n in transS a c\u2081 (<+S> a c\u2081 ih ih\u2081) (identS\u02e1 a c\u2081 (0S _ _))\n -- begin\n -- x \u2219S 0S b c\u2081 +S x\u2081 \u2219S 0S b\u2081 c\u2081\n -- \u2248\u27e8 <+S> a c\u2081 ih ih\u2081 \u27e9\n -- 0S _ _ +S 0S _ _\n -- \u2248\u27e8 identS\u02e1 a c\u2081 (0S _ _) \u27e9\n -- 0S a c\u2081\n -- \u220e\n ) ,\n (let\n open EqReasoning (setoidS _ _)\n ih = zeroS\u02b3 a\u2081 b c x\u2082\n ih\u2081 = zeroS\u02b3 a\u2081 b\u2081 c x\u2083\n in begin\n x\u2082 \u2219S 0S b c +S x\u2083 \u2219S 0S b\u2081 c\n \u2248\u27e8 <+S> a\u2081 c ih ih\u2081 \u27e9\n 0S _ _ +S 0S _ _\n \u2248\u27e8 identS\u02e1 a\u2081 c (0S _ _) \u27e9\n 0S a\u2081 c\n \u220e) ,\n (let\n open EqReasoning (setoidS _ _)\n ih = zeroS\u02b3 a\u2081 b c\u2081 x\u2082\n ih\u2081 = zeroS\u02b3 a\u2081 b\u2081 c\u2081 x\u2083\n in begin\n x\u2082 \u2219S 0S b c\u2081 +S x\u2083 \u2219S 0S b\u2081 c\u2081\n \u2248\u27e8 <+S> a\u2081 c\u2081 ih ih\u2081 \u27e9\n 0S _ _ +S 0S _ _\n \u2248\u27e8 identS\u02e1 a\u2081 c\u2081 (0S _ _) \u27e9\n 0S a\u2081 c\u2081\n \u220e)\n\n <\u2219S> : (a b c : Shape) {x y : M s a b} {u v : M s b c} \u2192\n x \u2243S' y \u2192 u \u2243S' v \u2192 (x \u2219S u) \u2243S' (y \u2219S v)\n <\u2219S> L L L {One x} {One x\u2081} {One x\u2082} {One x\u2083} p q = p <\u2219> q\n <\u2219S> L L (B c c\u2081) {One x} {One x\u2081} {Row u u\u2081} {Row v v\u2081} p (q , q\u2081) =\n (<\u2219S> L L c {One x} {One x\u2081} {u} {v} p q) ,\n <\u2219S> L L c\u2081 {One x} {One x\u2081} {u\u2081} {v\u2081} p q\u2081\n <\u2219S> L (B b b\u2081) L {Row x x\u2081} {Row y y\u2081} {Col u u\u2081} {Col v v\u2081} (p , p\u2081) (q , q\u2081) =\n -- Row x x\u2081 \u2219S Col u u\u2081 \u2243S' Row y y\u2081 \u2219S Col v v\u2081\n let\n open EqReasoning (setoidS _ _)\n ih = <\u2219S> _ _ _ {x} {y} {u} {v} p q\n ih\u2081 = <\u2219S> _ _ _ {x\u2081} {y\u2081} {u\u2081} {v\u2081} p\u2081 q\u2081\n in begin\n Row x x\u2081 \u2219S Col u u\u2081\n \u2261\u27e8 refl-\u2261 \u27e9\n x \u2219S u +S x\u2081 \u2219S u\u2081\n \u2248\u27e8 <+S> L L {x \u2219S u} {y \u2219S v} {x\u2081 \u2219S u\u2081} {y\u2081 \u2219S v\u2081} ih ih\u2081 \u27e9\n y \u2219S v +S y\u2081 \u2219S v\u2081\n \u220e\n <\u2219S> L (B b b\u2081) (B c c\u2081) {Row x x\u2081} {Row y y\u2081} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083} (p , p\u2081) (q , q\u2081 , q\u2082 , q\u2083) =\n (let\n ih = <\u2219S> L b c p q\n ih\u2081 = <\u2219S> L b\u2081 c p\u2081 q\u2082\n in <+S> L c ih ih\u2081) ,\n <+S> L c\u2081 (<\u2219S> L b c\u2081 p q\u2081) (<\u2219S> L b\u2081 c\u2081 p\u2081 q\u2083)\n <\u2219S> (B a a\u2081) L L {Col x x\u2081} {Col y y\u2081} {One x\u2082} {One x\u2083} (p , p\u2081) q =\n <\u2219S> a L L p q ,\n <\u2219S> a\u2081 L L p\u2081 q\n <\u2219S> (B a a\u2081) L (B c c\u2081) {Col x x\u2081} {Col y y\u2081} {Row u u\u2081} {Row v v\u2081} (p , p\u2081) (q , q\u2081) =\n <\u2219S> a L c p q ,\n <\u2219S> a L c\u2081 p q\u2081 ,\n <\u2219S> a\u2081 L c p\u2081 q ,\n <\u2219S> a\u2081 L c\u2081 p\u2081 q\u2081\n <\u2219S> (B a a\u2081) (B b b\u2081) L {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Col u u\u2081} {Col v v\u2081} (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081) =\n <+S> a L (<\u2219S> a b L p q) (<\u2219S> a b\u2081 L p\u2081 q\u2081) ,\n <+S> a\u2081 L (<\u2219S> a\u2081 b L p\u2082 q) (<\u2219S> a\u2081 b\u2081 L p\u2083 q\u2081 )\n <\u2219S> (B a a\u2081) (B b b\u2081) (B c c\u2081) {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083} (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081 , q\u2082 , q\u2083) =\n <+S> a c (<\u2219S> a b c p q) (<\u2219S> a b\u2081 c p\u2081 q\u2082) ,\n <+S> a c\u2081 (<\u2219S> a b c\u2081 p q\u2081) (<\u2219S> a b\u2081 c\u2081 p\u2081 q\u2083) ,\n <+S> a\u2081 c (<\u2219S> a\u2081 b c p\u2082 q) (<\u2219S> a\u2081 b\u2081 c p\u2083 q\u2082) ,\n <+S> a\u2081 c\u2081 (<\u2219S> a\u2081 b c\u2081 p\u2082 q\u2081) (<\u2219S> a\u2081 b\u2081 c\u2081 p\u2083 q\u2083)\n\n idemS : (r c : Shape) (x : M s r c) \u2192 x +S x \u2243S' x\n idemS L L (One x) = idem x\n idemS L (B c c\u2081) (Row x x\u2081) = (idemS _ _ x) , (idemS _ _ x\u2081)\n idemS (B r r\u2081) L (Col x x\u2081) = (idemS _ _ x) , (idemS _ _ x\u2081)\n idemS (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (idemS _ _ x) , (idemS _ _ x\u2081 , (idemS _ _ x\u2082 , idemS _ _ x\u2083))\n\n distlS : (a b c : Shape) (x : M s a b) (y z : M s b c) \u2192\n (x \u2219S (y +S z)) \u2243S' ((x \u2219S y) +S (x \u2219S z))\n distlS L L L (One x) (One y) (One z) = distl x y z\n distlS L L (B c c\u2081) (One x) (Row y y\u2081) (Row z z\u2081) =\n distlS L L c (One x) y z ,\n distlS L L c\u2081 (One x) y\u2081 z\u2081\n distlS L (B b b\u2081) L (Row x x\u2081) (Col y y\u2081) (Col z z\u2081) =\n {!!}\n distlS L (B b b\u2081) (B c c\u2081) (Row x x\u2081) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n {!!} ,\n {!!}\n distlS (B a a\u2081) L L (Col x x\u2081) (One x\u2082) (One x\u2083) = {!!}\n distlS (B a a\u2081) L (B c c\u2081) (Col x x\u2081) (Row y y\u2081) (Row z z\u2081) = {!!}\n distlS (B a a\u2081) (B b b\u2081) L (Q x x\u2081 x\u2082 x\u2083) (Col y y\u2081) (Col z z\u2081) = {!!}\n distlS (B a a\u2081) (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) = {!!}\n\n SNR : SemiNearRing\n SNR =\n record\n { s = S\n ; _\u2243\u209b_ = \u2243S shape shape\n ; 0\u209b = 0S shape shape\n ; _+\u209b_ = _+S_\n ; _\u2219\u209b_ = _\u2219S_\n ; isCommMon = isCommMonS\n ; zero\u02e1 = zeroS\u02e1 shape shape shape\n ; zero\u02b3 = zeroS\u02b3 shape shape shape\n ; _<\u2219>_ = <\u2219S> shape shape shape\n ; idem = idemS shape shape\n ; distl = {!!}\n ; distr = {!!}\n }\n","old_contents":"-- Matrices indexed by shape\nmodule SquareMatrix where\n\nopen import Shape\nopen import Matrix\n\n-- Square matrices have the same shape in both dimensions\nSq : Set \u2192 Shape \u2192 Set\nSq a shape = M a shape shape\n\nopen import SemiNearRingRecord\nopen import Algebra.Structures\nopen import Relation.Binary\nopen import Data.Product\n\nopen import Relation.Binary.PropositionalEquality hiding (trans; sym) renaming (refl to refl-\u2261)\nimport Relation.Binary.EqReasoning as EqReasoning\n\n\n-- Lifting a SNR to a to a Square matrix of some shape\nSquare : SemiNearRing \u2192 Shape \u2192 SemiNearRing\nSquare snr shape = SNR\n where\n open SemiNearRing snr\n\n S = Sq s shape\n\n open Operations s _\u2219\u209b_ _+\u209b_\n renaming (_+_ to _+S_; _*_ to _\u2219S_)\n\n 0S : (r c : Shape) \u2192 M s r c\n 0S L L = One 0\u209b\n 0S L (B s s\u2081) = Row (0S L s) (0S L s\u2081)\n 0S (B r r\u2081) L = Col (0S r L) (0S r\u2081 L)\n 0S (B r r\u2081) (B s s\u2081) =\n Q (0S r s) (0S r s\u2081)\n (0S r\u2081 s) (0S r\u2081 s\u2081)\n\n \u2243S : (r c : Shape) \u2192\n M s r c \u2192 M s r c \u2192 Set\n \u2243S L L (One x) (One x\u2081) = x \u2243\u209b x\u2081\n \u2243S L (B c\u2081 c\u2082) (Row m m\u2081) (Row n n\u2081) = \u2243S L c\u2081 m n \u00d7 \u2243S L c\u2082 m\u2081 n\u2081\n \u2243S (B r\u2081 r\u2082) L (Col m m\u2081) (Col n n\u2081) = \u2243S r\u2081 L m n \u00d7 \u2243S r\u2082 L m\u2081 n\u2081\n \u2243S (B r\u2081 r\u2082) (B c\u2081 c\u2082) (Q m00 m01 m10 m11) (Q n00 n01 n10 n11) =\n \u2243S r\u2081 c\u2081 m00 n00 \u00d7 \u2243S r\u2081 c\u2082 m01 n01 \u00d7\n \u2243S r\u2082 c\u2081 m10 n10 \u00d7 \u2243S r\u2082 c\u2082 m11 n11\n\n\n _\u2243S'_ : {r c : Shape} \u2192 M s r c \u2192 M s r c \u2192 Set\n _\u2243S'_ {r} {c} m n = \u2243S r c m n\n\n\n reflS : (r c : Shape) \u2192\n {X : M s r c} \u2192 X \u2243S' X\n reflS L L {X = One x} = refl\u209b {x}\n reflS L (B c\u2081 c\u2082) {X = Row X Y} = reflS L c\u2081 , reflS L c\u2082\n reflS (B r\u2081 r\u2082) L {X = Col X Y} = reflS r\u2081 L , reflS r\u2082 L\n reflS (B r\u2081 r\u2082) (B c\u2081 c\u2082) {X = Q X Y Z W} =\n reflS r\u2081 c\u2081 , reflS r\u2081 c\u2082 ,\n reflS r\u2082 c\u2081 , reflS r\u2082 c\u2082\n\n symS : (r c : Shape) \u2192\n {i j : M s r c} \u2192 i \u2243S' j \u2192 j \u2243S' i\n symS L L {One x} {One x\u2081} p = sym\u209b p\n symS L (B c\u2081 c\u2082) {Row i\u2081 i\u2082} {Row j\u2081 j\u2082} (p , q) = symS L c\u2081 p , symS L c\u2082 q\n symS (B r\u2081 r\u2082) L {Col i\u2081 i} {Col j j\u2081} (p , q) = symS r\u2081 L p , symS r\u2082 L q\n symS (B r r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j j\u2081 j\u2082 j\u2083} (p , q , x , y) =\n symS r c\u2081 p , symS r c\u2082 q ,\n symS r\u2082 c\u2081 x , symS r\u2082 c\u2082 y\n\n transS : (r c : Shape) \u2192\n {i j k : M s r c} \u2192 i \u2243S' j \u2192 j \u2243S' k \u2192 i \u2243S' k\n transS L L {One x} {One x\u2081} {One x\u2082} p q = trans p q\n transS L (B c\u2081 c\u2082) {Row i i\u2081} {Row j j\u2081} {Row k k\u2081} (p , q) (p' , q') =\n transS L c\u2081 p p' , transS L c\u2082 q q'\n transS (B r\u2081 r\u2082) L {Col i i\u2081} {Col j j\u2081} {Col k k\u2081} (p , q) (p' , q') =\n transS r\u2081 L p p' , transS r\u2082 L q q'\n transS (B r\u2081 r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j\u2082 j j\u2083 j\u2081} {Q k k\u2081 k\u2082 k\u2083}\n (p , q , x , y) (p' , q' , x' , y') =\n transS r\u2081 c\u2081 p p' , transS r\u2081 c\u2082 q q' ,\n transS r\u2082 c\u2081 x x' , transS r\u2082 c\u2082 y y'\n\n isEquivS =\n record\n { refl = reflS shape shape\n ; sym = symS shape shape\n ; trans = transS shape shape }\n\n liftAssoc : (r c : Shape) (x y z : M s r c) \u2192 ((x +S y) +S z) \u2243S' (x +S (y +S z))\n liftAssoc L L (One x) (One y) (One z) = assoc\u209b x y z\n liftAssoc L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) (Row z z\u2081) =\n liftAssoc L c x y z , liftAssoc L c\u2081 x\u2081 y\u2081 z\u2081\n liftAssoc (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) (Col z z\u2081) =\n liftAssoc r L x y z , liftAssoc r\u2081 L x\u2081 y\u2081 z\u2081\n liftAssoc (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n (liftAssoc r c x y z) , (liftAssoc r c\u2081 x\u2081 y\u2081 z\u2081) ,\n (liftAssoc r\u2081 c x\u2082 y\u2082 z\u2082) , (liftAssoc r\u2081 c\u2081 x\u2083 y\u2083 z\u2083)\n\n <+S> : (r c : Shape) {x y u v : M s r c} \u2192\n x \u2243S' y \u2192 u \u2243S' v \u2192 (x +S u) \u2243S' (y +S v)\n <+S> L L {One x} {One x\u2081} {One x\u2082} {One x\u2083} p q = p <+> q\n <+S> L (B c c\u2081) {Row x x\u2081} {Row y y\u2081} {Row u u\u2081} {Row v v\u2081} (p , p\u2081) (q , q\u2081) =\n <+S> L c p q , <+S> L c\u2081 p\u2081 q\u2081\n <+S> (B r r\u2081) L {Col x x\u2081} {Col y y\u2081} {Col u u\u2081} {Col v v\u2081} (p , p\u2081) (q , q\u2081) =\n <+S> r L p q , <+S> r\u2081 L p\u2081 q\u2081\n <+S> (B r r\u2081) (B c c\u2081) {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083}\n (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081 , q\u2082 , q\u2083) =\n <+S> r c p q , <+S> r c\u2081 p\u2081 q\u2081 ,\n <+S> r\u2081 c p\u2082 q\u2082 , <+S> r\u2081 c\u2081 p\u2083 q\u2083\n\n isSemgroupS =\n record\n { isEquivalence = isEquivS\n ; assoc = liftAssoc shape shape\n ; \u2219-cong = <+S> shape shape }\n\n\n identS\u02e1 : (r c : Shape) (x : M s r c) \u2192\n 0S r c +S x \u2243S' x\n identS\u02e1 L L (One x) = identity\u02e1\u209b x\n identS\u02e1 L (B c c\u2081) (Row x x\u2081) = identS\u02e1 L c x , identS\u02e1 L c\u2081 x\u2081\n identS\u02e1 (B r r\u2081) L (Col x x\u2081) = identS\u02e1 r L x , identS\u02e1 r\u2081 L x\u2081\n identS\u02e1 (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n identS\u02e1 r c x , identS\u02e1 r c\u2081 x\u2081 ,\n identS\u02e1 r\u2081 c x\u2082 , identS\u02e1 r\u2081 c\u2081 x\u2083\n\n liftComm : (r c : Shape) \u2192 (x y : M s r c) \u2192\n (x +S y) \u2243S' (y +S x)\n liftComm L L (One x) (One x\u2081) = comm\u209b x x\u2081\n liftComm L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) = (liftComm L c x y) , (liftComm L c\u2081 x\u2081 y\u2081)\n liftComm (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) = (liftComm r L x y) , (liftComm r\u2081 L x\u2081 y\u2081)\n liftComm (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) =\n liftComm r c x y , liftComm r c\u2081 x\u2081 y\u2081 ,\n liftComm r\u2081 c x\u2082 y\u2082 , liftComm r\u2081 c\u2081 x\u2083 y\u2083\n\n isCommMonS =\n record\n { isSemigroup = isSemgroupS\n ; identity\u02e1 = identS\u02e1 shape shape\n ; comm = liftComm shape shape }\n\n\n setoidS : (r c : Shape) \u2192 Setoid _ _\n setoidS r c =\n record\n { Carrier = M s r c\n ; _\u2248_ = \u2243S r c\n ; isEquivalence =\n record\n { refl = reflS r c ; sym = symS r c ; trans = transS r c } }\n\n\n zeroS\u02e1 : (a b c : Shape) (x : M s b c) \u2192\n (0S a b \u2219S x) \u2243S' 0S a c\n zeroS\u02e1 L L L (One x) = zero\u02e1 x\n zeroS\u02e1 L L (B c c\u2081) (Row x x\u2081) = (zeroS\u02e1 L L c x) , (zeroS\u02e1 L L c\u2081 x\u2081)\n zeroS\u02e1 L (B b b\u2081) L (Col x x\u2081) =\n let\n open EqReasoning (setoidS L L)\n ih = zeroS\u02e1 L b L x\n ih\u2081 = zeroS\u02e1 L b\u2081 L x\u2081\n in begin\n (Row (0S L b) (0S L b\u2081) \u2219S (Col x x\u2081))\n \u2261\u27e8 refl-\u2261 \u27e9\n (0S L b) \u2219S x +S (0S L b\u2081) \u2219S x\u2081\n \u2248\u27e8 <+S> L L {0S L b \u2219S x} {0S L L} {0S L b\u2081 \u2219S x\u2081} {0S L L} ih ih\u2081 \u27e9\n 0S L L +S 0S L L\n \u2248\u27e8 identS\u02e1 L L (0S L L) \u27e9\n 0S L L\n \u220e\n zeroS\u02e1 L (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (let\n open EqReasoning (setoidS L c)\n ih = zeroS\u02e1 L b c x\n ih\u2081 = zeroS\u02e1 L b\u2081 c x\u2082\n in begin\n 0S L b \u2219S x +S 0S L b\u2081 \u2219S x\u2082\n \u2248\u27e8 <+S> L c ih ih\u2081 \u27e9\n 0S L c +S 0S L c\n \u2248\u27e8 identS\u02e1 L c (0S L c) \u27e9\n 0S L c\n \u220e) ,\n (let\n open EqReasoning (setoidS L c\u2081)\n ih = zeroS\u02e1 L b c\u2081 x\u2081\n ih\u2081 = zeroS\u02e1 L b\u2081 c\u2081 x\u2083\n in begin\n 0S L b \u2219S x\u2081 +S 0S L b\u2081 \u2219S x\u2083\n \u2248\u27e8 <+S> L c\u2081 ih ih\u2081 \u27e9\n 0S L c\u2081 +S 0S L c\u2081\n \u2248\u27e8 identS\u02e1 L c\u2081 _ \u27e9\n 0S L c\u2081\n \u220e)\n zeroS\u02e1 (B a a\u2081) L L (One x) = zeroS\u02e1 a L L (One x) , zeroS\u02e1 a\u2081 L L (One x)\n zeroS\u02e1 (B a a\u2081) L (B c c\u2081) (Row x x\u2081) =\n zeroS\u02e1 a L c x , zeroS\u02e1 a L c\u2081 x\u2081 ,\n zeroS\u02e1 a\u2081 L c x , zeroS\u02e1 a\u2081 L c\u2081 x\u2081\n zeroS\u02e1 (B a a\u2081) (B b b\u2081) L (Col x x\u2081) =\n (let\n open EqReasoning (setoidS a L)\n ih = zeroS\u02e1 a b L x\n ih\u2081 = zeroS\u02e1 a b\u2081 L x\u2081\n in begin\n 0S a b \u2219S x +S 0S a b\u2081 \u2219S x\u2081\n \u2248\u27e8 <+S> a L ih ih\u2081 \u27e9\n 0S a L +S 0S a L\n \u2248\u27e8 identS\u02e1 a L _ \u27e9\n 0S a L\n \u220e) ,\n (let\n open EqReasoning (setoidS a\u2081 L)\n ih = zeroS\u02e1 a\u2081 b L x\n ih\u2081 = zeroS\u02e1 a\u2081 b\u2081 L x\u2081\n in begin\n 0S a\u2081 b \u2219S x +S 0S a\u2081 b\u2081 \u2219S x\u2081\n \u2248\u27e8 <+S> a\u2081 L ih ih\u2081 \u27e9\n 0S a\u2081 L +S 0S a\u2081 L\n \u2248\u27e8 identS\u02e1 a\u2081 L _ \u27e9\n 0S a\u2081 L\n \u220e)\n zeroS\u02e1 (B a a\u2081) (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (let\n open EqReasoning (setoidS a c)\n ih = zeroS\u02e1 a b c x\n ih\u2081 = zeroS\u02e1 a b\u2081 c x\u2082\n in begin\n 0S a b \u2219S x +S 0S a b\u2081 \u2219S x\u2082\n \u2248\u27e8 <+S> a c ih ih\u2081 \u27e9\n 0S a c +S 0S a c\n \u2248\u27e8 identS\u02e1 a c _ \u27e9\n 0S a c\n \u220e) ,\n (let\n open EqReasoning (setoidS a c\u2081)\n ih = zeroS\u02e1 a b c\u2081 x\u2081\n ih\u2081 = zeroS\u02e1 a b\u2081 c\u2081 x\u2083\n in begin\n 0S a b \u2219S x\u2081 +S 0S a b\u2081 \u2219S x\u2083\n \u2248\u27e8 <+S> a c\u2081 ih ih\u2081 \u27e9\n 0S a c\u2081 +S 0S a c\u2081\n \u2248\u27e8 identS\u02e1 a c\u2081 _ \u27e9\n 0S a c\u2081\n \u220e) ,\n (let\n open EqReasoning (setoidS a\u2081 c)\n ih = zeroS\u02e1 a\u2081 b c x\n ih\u2081 = zeroS\u02e1 a\u2081 b\u2081 c x\u2082\n in begin\n 0S a\u2081 b \u2219S x +S 0S a\u2081 b\u2081 \u2219S x\u2082\n \u2248\u27e8 <+S> a\u2081 c ih ih\u2081 \u27e9\n 0S a\u2081 c +S 0S a\u2081 c\n \u2248\u27e8 identS\u02e1 a\u2081 c _ \u27e9\n 0S a\u2081 c\n \u220e) ,\n (let\n open EqReasoning (setoidS a\u2081 c\u2081)\n ih = zeroS\u02e1 a\u2081 b c\u2081 x\u2081\n ih\u2081 = zeroS\u02e1 a\u2081 b\u2081 c\u2081 x\u2083\n in begin\n 0S a\u2081 b \u2219S x\u2081 +S 0S a\u2081 b\u2081 \u2219S x\u2083\n \u2248\u27e8 <+S> a\u2081 c\u2081 ih ih\u2081 \u27e9\n 0S a\u2081 c\u2081 +S 0S a\u2081 c\u2081\n \u2248\u27e8 identS\u02e1 a\u2081 c\u2081 _ \u27e9\n 0S a\u2081 c\u2081\n \u220e)\n\n zeroS\u02b3 : (a b c : Shape) (x : M s a b) \u2192\n (x \u2219S 0S b c) \u2243S' 0S a c\n zeroS\u02b3 L L L (One x) = zero\u02b3 x\n zeroS\u02b3 L L (B c c\u2081) (One x) =\n (zeroS\u02b3 L L c (One x)) , (zeroS\u02b3 L L c\u2081 (One x))\n zeroS\u02b3 L (B b b\u2081) L (Row x x\u2081) =\n let\n open EqReasoning (setoidS L L)\n ih = zeroS\u02b3 L b L x\n ih\u2081 = zeroS\u02b3 L b\u2081 L x\u2081\n in begin\n Row x x\u2081 \u2219S Col (0S b L) (0S b\u2081 L)\n \u2261\u27e8 refl-\u2261 \u27e9\n (x \u2219S 0S b L) +S (x\u2081 \u2219S 0S b\u2081 L)\n \u2248\u27e8 <+S> L L {x \u2219S 0S b L} {0S L L} {x\u2081 \u2219S 0S b\u2081 L} {0S L L} ih ih\u2081 \u27e9\n 0S L L +S 0S L L\n \u2248\u27e8 identS\u02e1 L L (0S L L) \u27e9\n 0S L L\n \u220e\n zeroS\u02b3 L (B b b\u2081) (B c c\u2081) (Row x x\u2081) =\n (let\n open EqReasoning (setoidS L c)\n ih = zeroS\u02b3 L b c x\n ih\u2081 = zeroS\u02b3 L b\u2081 c x\u2081\n in begin\n x \u2219S 0S b c +S x\u2081 \u2219S 0S b\u2081 c\n \u2248\u27e8 <+S> L c ih ih\u2081 \u27e9\n 0S L c +S 0S L c\n \u2248\u27e8 identS\u02e1 L c (0S L c) \u27e9\n 0S L c\n \u220e) ,\n (let\n open EqReasoning (setoidS L c\u2081)\n ih = zeroS\u02b3 L b c\u2081 x\n ih\u2081 = zeroS\u02b3 L b\u2081 c\u2081 x\u2081\n in begin\n x \u2219S 0S b c\u2081 +S x\u2081 \u2219S 0S b\u2081 c\u2081\n \u2248\u27e8 <+S> L c\u2081 ih ih\u2081 \u27e9\n 0S L c\u2081 +S 0S L c\u2081\n \u2248\u27e8 identS\u02e1 L c\u2081 (0S L c\u2081) \u27e9\n 0S L c\u2081\n \u220e)\n zeroS\u02b3 (B a a\u2081) L L (Col x x\u2081) = zeroS\u02b3 a L L x , zeroS\u02b3 a\u2081 L L x\u2081\n zeroS\u02b3 (B a a\u2081) L (B c c\u2081) (Col x x\u2081) =\n zeroS\u02b3 a L c x ,\n zeroS\u02b3 a L c\u2081 x ,\n zeroS\u02b3 a\u2081 L c x\u2081 ,\n zeroS\u02b3 a\u2081 L c\u2081 x\u2081\n zeroS\u02b3 (B a a\u2081) (B b b\u2081) L (Q x x\u2081 x\u2082 x\u2083) =\n (let\n open EqReasoning (setoidS _ _)\n ih = zeroS\u02b3 a b L x\n ih\u2081 = zeroS\u02b3 a b\u2081 L x\u2081\n in begin\n x \u2219S 0S b L +S x\u2081 \u2219S 0S b\u2081 L\n \u2248\u27e8 <+S> a L ih ih\u2081 \u27e9\n 0S a L +S 0S a L\n \u2248\u27e8 identS\u02e1 a L (0S _ _) \u27e9\n 0S a L\n \u220e) ,\n (let\n open EqReasoning (setoidS _ _)\n ih = zeroS\u02b3 a\u2081 b L x\u2082\n ih\u2081 = zeroS\u02b3 a\u2081 b\u2081 L x\u2083\n in begin\n x\u2082 \u2219S 0S b L +S x\u2083 \u2219S 0S b\u2081 L\n \u2248\u27e8 <+S> a\u2081 L ih ih\u2081 \u27e9\n 0S _ _ +S 0S _ _\n \u2248\u27e8 identS\u02e1 a\u2081 L (0S _ _) \u27e9\n 0S a\u2081 L\n \u220e)\n zeroS\u02b3 (B a a\u2081) (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (let\n open EqReasoning (setoidS _ _)\n ih = zeroS\u02b3 a b c x\n ih\u2081 = zeroS\u02b3 a b\u2081 c x\u2081\n in begin\n x \u2219S 0S b c +S x\u2081 \u2219S 0S b\u2081 c\n \u2248\u27e8 <+S> a c ih ih\u2081 \u27e9\n 0S _ _ +S 0S _ _\n \u2248\u27e8 identS\u02e1 a c (0S _ _) \u27e9\n 0S _ _\n \u220e) ,\n (let\n -- open EqReasoning (setoidS _ _)\n ih = zeroS\u02b3 a b c\u2081 x\n ih\u2081 = zeroS\u02b3 a b\u2081 c\u2081 x\u2081\n in transS a c\u2081 (<+S> a c\u2081 ih ih\u2081) (identS\u02e1 a c\u2081 (0S _ _))\n -- begin\n -- x \u2219S 0S b c\u2081 +S x\u2081 \u2219S 0S b\u2081 c\u2081\n -- \u2248\u27e8 <+S> a c\u2081 ih ih\u2081 \u27e9\n -- 0S _ _ +S 0S _ _\n -- \u2248\u27e8 identS\u02e1 a c\u2081 (0S _ _) \u27e9\n -- 0S a c\u2081\n -- \u220e\n ) ,\n (let\n open EqReasoning (setoidS _ _)\n ih = zeroS\u02b3 a\u2081 b c x\u2082\n ih\u2081 = zeroS\u02b3 a\u2081 b\u2081 c x\u2083\n in begin\n x\u2082 \u2219S 0S b c +S x\u2083 \u2219S 0S b\u2081 c\n \u2248\u27e8 <+S> a\u2081 c ih ih\u2081 \u27e9\n 0S _ _ +S 0S _ _\n \u2248\u27e8 identS\u02e1 a\u2081 c (0S _ _) \u27e9\n 0S a\u2081 c\n \u220e) ,\n (let\n open EqReasoning (setoidS _ _)\n ih = zeroS\u02b3 a\u2081 b c\u2081 x\u2082\n ih\u2081 = zeroS\u02b3 a\u2081 b\u2081 c\u2081 x\u2083\n in begin\n x\u2082 \u2219S 0S b c\u2081 +S x\u2083 \u2219S 0S b\u2081 c\u2081\n \u2248\u27e8 <+S> a\u2081 c\u2081 ih ih\u2081 \u27e9\n 0S _ _ +S 0S _ _\n \u2248\u27e8 identS\u02e1 a\u2081 c\u2081 (0S _ _) \u27e9\n 0S a\u2081 c\u2081\n \u220e)\n\n <\u2219S> : (a b c : Shape) {x y : M s a b} {u v : M s b c} \u2192\n x \u2243S' y \u2192 u \u2243S' v \u2192 (x \u2219S u) \u2243S' (y \u2219S v)\n <\u2219S> L L L {One x} {One x\u2081} {One x\u2082} {One x\u2083} p q = p <\u2219> q\n <\u2219S> L L (B c c\u2081) {One x} {One x\u2081} {Row u u\u2081} {Row v v\u2081} p (q , q\u2081) =\n (<\u2219S> L L c {One x} {One x\u2081} {u} {v} p q) ,\n <\u2219S> L L c\u2081 {One x} {One x\u2081} {u\u2081} {v\u2081} p q\u2081\n <\u2219S> L (B b b\u2081) L {Row x x\u2081} {Row y y\u2081} {Col u u\u2081} {Col v v\u2081} (p , p\u2081) (q , q\u2081) =\n -- Row x x\u2081 \u2219S Col u u\u2081 \u2243S' Row y y\u2081 \u2219S Col v v\u2081\n let\n open EqReasoning (setoidS _ _)\n ih = <\u2219S> _ _ _ {x} {y} {u} {v} p q\n ih\u2081 = <\u2219S> _ _ _ {x\u2081} {y\u2081} {u\u2081} {v\u2081} p\u2081 q\u2081\n in begin\n Row x x\u2081 \u2219S Col u u\u2081\n \u2261\u27e8 refl-\u2261 \u27e9\n x \u2219S u +S x\u2081 \u2219S u\u2081\n \u2248\u27e8 <+S> L L {x \u2219S u} {y \u2219S v} {x\u2081 \u2219S u\u2081} {y\u2081 \u2219S v\u2081} ih ih\u2081 \u27e9\n y \u2219S v +S y\u2081 \u2219S v\u2081\n \u220e\n <\u2219S> L (B b b\u2081) (B c c\u2081) {Row x x\u2081} {Row y y\u2081} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083} (p , p\u2081) (q , q\u2081 , q\u2082 , q\u2083) =\n {!!} ,\n {!!}\n <\u2219S> (B a a\u2081) L L {Col x x\u2081} {Col y y\u2081} {One x\u2082} {One x\u2083} (p , p\u2081) q =\n {!!} ,\n {!!}\n <\u2219S> (B a a\u2081) L (B c c\u2081) {Col x x\u2081} {Col y y\u2081} {Row u u\u2081} {Row v v\u2081} (p , p\u2081) (q , q\u2081) =\n {!!} ,\n {!!} ,\n {!!} ,\n {!!}\n <\u2219S> (B a a\u2081) (B b b\u2081) L {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Col u u\u2081} {Col v v\u2081} (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081) =\n {!!} ,\n {!!}\n <\u2219S> (B a a\u2081) (B b b\u2081) (B c c\u2081) {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083} (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081 , q\u2082 , q\u2083) =\n {!!}\n\n idemS : (r c : Shape) (x : M s r c) \u2192 x +S x \u2243S' x\n idemS L L (One x) = idem x\n idemS L (B c c\u2081) (Row x x\u2081) = (idemS _ _ x) , (idemS _ _ x\u2081)\n idemS (B r r\u2081) L (Col x x\u2081) = (idemS _ _ x) , (idemS _ _ x\u2081)\n idemS (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (idemS _ _ x) , (idemS _ _ x\u2081 , (idemS _ _ x\u2082 , idemS _ _ x\u2083))\n\n SNR : SemiNearRing\n SNR =\n record\n { s = S\n ; _\u2243\u209b_ = \u2243S shape shape\n ; 0\u209b = 0S shape shape\n ; _+\u209b_ = _+S_\n ; _\u2219\u209b_ = _\u2219S_\n ; isCommMon = isCommMonS\n ; zero\u02e1 = zeroS\u02e1 shape shape shape\n ; zero\u02b3 = zeroS\u02b3 shape shape shape\n ; _<\u2219>_ = <\u2219S> shape shape shape\n ; idem = idemS shape shape\n ; distl = {!!}\n ; distr = {!!}\n }\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"8c441c2498ec2d52054d910e48b8ba0d0e32b1e8","subject":"Fixed iterate-Stream.","message":"Fixed iterate-Stream.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Program\/MapIterate\/MapIterateI.agda","new_file":"src\/fot\/FOTC\/Program\/MapIterate\/MapIterateI.agda","new_contents":"------------------------------------------------------------------------------\n-- The map-iterate property: A property using co-induction\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- The map-iterate property (Gibbons and Hutton, 2005):\n-- map f (iterate f x) = iterate f (f \u00b7 x)\n\nmodule FOTC.Program.MapIterate.MapIterateI where\n\nopen import Common.FOL.Relation.Binary.EqReasoning\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Data.List\nopen import FOTC.Data.List.PropertiesI\nopen import FOTC.Data.Stream.Type\nopen import FOTC.Relation.Binary.Bisimilarity.Type\n\n------------------------------------------------------------------------------\n\nunfoldMapIterate : \u2200 f x \u2192\n map f (iterate f x) \u2261 f \u00b7 x \u2237 map f (iterate f (f \u00b7 x))\nunfoldMapIterate f x =\n map f (iterate f x) \u2261\u27e8 mapCong\u2082 (iterate-eq f x) \u27e9\n map f (x \u2237 iterate f (f \u00b7 x)) \u2261\u27e8 map-\u2237 f x (iterate f (f \u00b7 x)) \u27e9\n f \u00b7 x \u2237 map f (iterate f (f \u00b7 x)) \u220e\n\nmap-iterate-Stream : \u2200 f x \u2192 Stream (map f (iterate f x))\nmap-iterate-Stream f x = Stream-coind A h (x , refl)\n where\n A : D \u2192 Set\n A xs = \u2203[ y ] xs \u2261 map f (iterate f y)\n\n h : \u2200 {xs} \u2192 A xs \u2192 \u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs'\n h (y , prf) = f \u00b7 y\n , map f (iterate f (f \u00b7 y))\n , trans prf (unfoldMapIterate f y)\n , f \u00b7 y , refl\n\niterate-Stream : \u2200 f x \u2192 Stream (iterate f (f \u00b7 x))\niterate-Stream f x = Stream-coind A h (x , refl)\n where\n A : D \u2192 Set\n A xs = \u2203[ y ] xs \u2261 iterate f (f \u00b7 y)\n\n h : \u2200 {xs} \u2192 A xs \u2192 \u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs'\n h (y , prf) = f \u00b7 y\n , iterate f (f \u00b7 (f \u00b7 y))\n , trans prf (iterate-eq f (f \u00b7 y))\n , f \u00b7 y , refl\n\n-- The map-iterate property.\n\u2248-map-iterate : \u2200 f x \u2192 map f (iterate f x) \u2248 iterate f (f \u00b7 x)\n\u2248-map-iterate f x = \u2248-coind B h (x , refl , refl)\n where\n -- Based on the relation used by (Gim\u00e9nez and Cast\u00e9ran, 2007).\n B : D \u2192 D \u2192 Set\n B xs ys = \u2203[ y ] xs \u2261 map f (iterate f y) \u2227 ys \u2261 iterate f (f \u00b7 y)\n\n h : \u2200 {xs} {ys} \u2192 B xs ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys' \u2227 B xs' ys'\n h (y , prf\u2081 , prf\u2082) =\n f \u00b7 y\n , map f (iterate f (f \u00b7 y))\n , iterate f (f \u00b7 (f \u00b7 y))\n , trans prf\u2081 (unfoldMapIterate f y)\n , trans prf\u2082 (iterate-eq f (f \u00b7 y))\n , ((f \u00b7 y) , refl , refl)\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Gim\u00e9nez, Eduardo and Caster\u00e1n, Pierre (2007). A Tutorial on\n-- [Co-]Inductive Types in Coq.\n--\n-- Gibbons, Jeremy and Hutton, Graham (2005). Proof Methods for\n-- Corecursive Programs. In: Fundamenta Informaticae XX, pp. 1\u201314.\n","old_contents":"------------------------------------------------------------------------------\n-- The map-iterate property: A property using co-induction\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- The map-iterate property (Gibbons and Hutton, 2005):\n-- map f (iterate f x) = iterate f (f \u00b7 x)\n\nmodule FOTC.Program.MapIterate.MapIterateI where\n\nopen import Common.FOL.Relation.Binary.EqReasoning\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Data.List\nopen import FOTC.Data.List.PropertiesI\nopen import FOTC.Data.Stream.Type\nopen import FOTC.Relation.Binary.Bisimilarity.Type\n\n------------------------------------------------------------------------------\n\nunfoldMapIterate : \u2200 f x \u2192\n map f (iterate f x) \u2261 f \u00b7 x \u2237 map f (iterate f (f \u00b7 x))\nunfoldMapIterate f x =\n map f (iterate f x) \u2261\u27e8 mapCong\u2082 (iterate-eq f x) \u27e9\n map f (x \u2237 iterate f (f \u00b7 x)) \u2261\u27e8 map-\u2237 f x (iterate f (f \u00b7 x)) \u27e9\n f \u00b7 x \u2237 map f (iterate f (f \u00b7 x)) \u220e\n\nmap-iterate-Stream : \u2200 f x \u2192 Stream (map f (iterate f x))\nmap-iterate-Stream f x = Stream-coind A h (x , refl)\n where\n A : D \u2192 Set\n A xs = \u2203[ y ] xs \u2261 map f (iterate f y)\n\n h : \u2200 {xs} \u2192 A xs \u2192 \u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs'\n h (y , prf) = f \u00b7 y\n , map f (iterate f (f \u00b7 y))\n , (trans prf ((unfoldMapIterate f y)))\n , f \u00b7 y , refl\n\n-- TODO (23 December 2013).\n-- map-iterate-Stream\u2082 : \u2200 f x \u2192 Stream (iterate f (f \u00b7 x))\n\n-- The map-iterate property.\n\u2248-map-iterate : \u2200 f x \u2192 map f (iterate f x) \u2248 iterate f (f \u00b7 x)\n\u2248-map-iterate f x = \u2248-coind B h (x , refl , refl)\n where\n -- Based on the relation used by (Gim\u00e9nez and Cast\u00e9ran, 2007).\n B : D \u2192 D \u2192 Set\n B xs ys = \u2203[ y ] xs \u2261 map f (iterate f y) \u2227 ys \u2261 iterate f (f \u00b7 y)\n\n h : \u2200 {xs} {ys} \u2192 B xs ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys' \u2227 B xs' ys'\n h (y , prf\u2081 , prf\u2082) =\n f \u00b7 y\n , map f (iterate f (f \u00b7 y))\n , iterate f (f \u00b7 (f \u00b7 y))\n , trans prf\u2081 (unfoldMapIterate f y)\n , trans prf\u2082 (iterate-eq f (f \u00b7 y))\n , ((f \u00b7 y) , refl , refl)\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Gim\u00e9nez, Eduardo and Caster\u00e1n, Pierre (2007). A Tutorial on\n-- [Co-]Inductive Types in Coq.\n--\n-- Gibbons, Jeremy and Hutton, Graham (2005). Proof Methods for\n-- Corecursive Programs. In: Fundamenta Informaticae XX, pp. 1\u201314.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"347a67ed51a8af9f918731c0f060d6e580fe7cf3","subject":"Success on Nehemiah.Syntax.Term","message":"Success on Nehemiah.Syntax.Term\n","repos":"inc-lc\/ilc-agda","old_file":"Nehemiah\/Syntax\/Term.agda","new_file":"Nehemiah\/Syntax\/Term.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- The syntax of terms with the Nehemiah plugin.\n------------------------------------------------------------------------\n\nmodule Nehemiah.Syntax.Term where\n\nopen import Nehemiah.Syntax.Type\n\nopen import Data.Integer\n\nimport Parametric.Syntax.Term Base as Term\n\ndata Const : Term.Structure where\n intlit-const : (n : \u2124) \u2192 Const \u2205 int\n add-const : Const (int \u2022 int \u2022 \u2205) int\n minus-const : Const (int \u2022 \u2205) (int)\n\n empty-const : Const \u2205 (bag)\n insert-const : Const (int \u2022 bag \u2022 \u2205) (bag)\n union-const : Const (bag \u2022 bag \u2022 \u2205) (bag)\n negate-const : Const (bag \u2022 \u2205) (bag)\n\n flatmap-const : Const ((int \u21d2 bag) \u2022 bag \u2022 \u2205) (bag)\n sum-const : Const (bag \u2022 \u2205) (int)\n\n\nopen Term.Structure Const public\n\n-- Import Base.Data.DependentList again here, as part of the workaround for\n-- agda\/agda#1985; see Parametric.Syntax.Term for info.\n--\n-- XXX This import is needed to define the patterns in the right scope; if we\n-- don't, \u2022 gets bound to a different occurrence, and we notice that during\n-- typechecking, which happens at use site.\nopen import Base.Data.DependentList public\n\n-- Shorthands of constants\n\npattern intlit n = const (intlit-const n) \u2205\npattern add s t = const add-const (s \u2022 t \u2022 \u2205)\npattern minus t = const minus-const (t \u2022 \u2205)\npattern empty = const empty-const \u2205\npattern insert s t = const insert-const (s \u2022 t \u2022 \u2205)\npattern union s t = const union-const (s \u2022 t \u2022 \u2205)\npattern negate t = const negate-const (t \u2022 \u2205)\npattern flatmap s t = const flatmap-const (s \u2022 t \u2022 \u2205)\npattern sum t = const sum-const (t \u2022 \u2205)\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- The syntax of terms with the Nehemiah plugin.\n------------------------------------------------------------------------\n\nmodule Nehemiah.Syntax.Term where\n\nopen import Nehemiah.Syntax.Type\n\nopen import Data.Integer\n\nimport Parametric.Syntax.Term Base as Term\n\ndata Const : Term.Structure where\n intlit-const : (n : \u2124) \u2192 Const \u2205 int\n add-const : Const (int \u2022 int \u2022 \u2205) int\n minus-const : Const (int \u2022 \u2205) (int)\n\n empty-const : Const \u2205 (bag)\n insert-const : Const (int \u2022 bag \u2022 \u2205) (bag)\n union-const : Const (bag \u2022 bag \u2022 \u2205) (bag)\n negate-const : Const (bag \u2022 \u2205) (bag)\n\n flatmap-const : Const ((int \u21d2 bag) \u2022 bag \u2022 \u2205) (bag)\n sum-const : Const (bag \u2022 \u2205) (int)\n\n\n--open Term.Structure --works\nopen Term.Structure Const --fails, even with the rest disabled.\n{-\nAn internal error has occurred. Please report this as a bug.\nLocation of the error: src\/full\/Agda\/TypeChecking\/Substitute.hs:183\n-}\n\n{-\n-- Shorthands of constants\n\npattern intlit n = const (intlit-const n) \u2205\npattern add s t = const add-const (s \u2022 t \u2022 \u2205)\npattern minus t = const minus-const (t \u2022 \u2205)\npattern empty = const empty-const \u2205\npattern insert s t = const insert-const (s \u2022 t \u2022 \u2205)\npattern union s t = const union-const (s \u2022 t \u2022 \u2205)\npattern negate t = const negate-const (t \u2022 \u2205)\npattern flatmap s t = const flatmap-const (s \u2022 t \u2022 \u2205)\npattern sum t = const sum-const (t \u2022 \u2205)\n-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c94ea256c17bed12b70ebbdf415c500dd2705e28","subject":"Minor changes.","message":"Minor changes.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/GreatestFixedPoints\/Conat.agda","new_file":"notes\/fixed-points\/GreatestFixedPoints\/Conat.agda","new_contents":"------------------------------------------------------------------------------\n-- Co-inductive natural numbers\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule GreatestFixedPoints.Conat where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- Conat is a greatest fixed-point of a functor\n\n-- The functor.\nNatF : (D \u2192 Set) \u2192 D \u2192 Set\nNatF A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n\n-- The co-natural numbers are the greatest fixed-point of NatF.\npostulate\n Conat : D \u2192 Set\n\n -- Conat is a post-fixed point of NatF, i.e.\n --\n -- Conat \u2264 NatF Conat.\n Conat-unf : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\n\n -- The higher-order version.\n Conat-unf-ho : \u2200 {n} \u2192 Conat n \u2192 NatF Conat n\n\n -- Conat is the greatest post-fixed point of NatF, i.e.\n --\n -- \u2200 P. P \u2264 NatF P \u21d2 P \u2264 Conat.\n Conat-coind :\n (A : D \u2192 Set) \u2192\n -- A is post-fixed point of ConatF.\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n -- Conat is greater than A.\n \u2200 {n} \u2192 A n \u2192 Conat n\n\n -- The higher-order version.\n Conat-coind-ho :\n (A : D \u2192 Set) \u2192 (\u2200 {n} \u2192 A n \u2192 NatF A n) \u2192 \u2200 {n} \u2192 A n \u2192 Conat n\n\n -- 22 December 2013. This is a stronger induction principle. If we\n -- use it, we can use the trivial predicate A = \u03bb x \u2192 x \u2261 x in the\n -- proofs. Unfortunately, we don't have a justification\/proof for\n -- this principle.\n Conat-coind-stronger :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n A n \u2192 Conat n\n\n------------------------------------------------------------------------------\n-- Conat-unf and Conat-unf-ho are equivalents\n\nConat-unf' : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\nConat-unf' = Conat-unf-ho\n\nConat-unf-ho' : \u2200 {n} \u2192 Conat n \u2192 NatF Conat n\nConat-unf-ho' = Conat-unf\n\n------------------------------------------------------------------------------\n-- Conat-coind and Conat-coind-ho are equivalents\n\nConat-coind' :\n (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind' = Conat-coind-ho\n\nConat-coind-ho' :\n (A : D \u2192 Set) \u2192 (\u2200 {n} \u2192 A n \u2192 NatF A n) \u2192 \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind-ho' = Conat-coind\n\n------------------------------------------------------------------------------\n-- From Conat-coind\/Conat-coind-stronger to Conat-coind-stronger\/Conat-coind\n\nConat-coind'' :\n (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind'' A h An = Conat-coind-stronger A h An\n\n-- 22 December 2013: We cannot prove Conat-coind-stronger using\n-- Conat-coind.\nConat-coind-stronger'' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n A n \u2192 Conat n\nConat-coind-stronger'' A h An = Conat-coind A {!!} An\n\n------------------------------------------------------------------------------\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Conat predicate is also a pre-fixed point of the functional NatF,\n-- i.e.\n--\n-- NatF Conat \u2264 Conat.\nConat-in : \u2200 {n} \u2192\n n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n') \u2192\n Conat n\nConat-in h = Conat-coind A h' h\n where\n A : D \u2192 Set\n A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\n\n h' : \u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n h' (inj\u2081 n\u22610) = inj\u2081 n\u22610\n h' (inj\u2082 (n' , prf , Cn')) = inj\u2082 (n' , prf , Conat-unf Cn')\n\nConat-in-ho : \u2200 {n} \u2192 NatF Conat n \u2192 Conat n\nConat-in-ho = Conat-in\n\n-- A different definition.\nConat-in' : (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')) \u2192\n \u2200 {n} \u2192 Conat n\nConat-in' h = Conat-coind (\u03bb m \u2192 m \u2261 m) h' refl\n where\n h' : \u2200 {m} \u2192 m \u2261 m \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 m' \u2261 m')\n h' _ with h\n ... | inj\u2081 m\u22610 = inj\u2081 m\u22610\n ... | inj\u2082 (m' , prf , _) = inj\u2082 (m' , prf , refl)\n\nConat-in-ho' : (\u2200 {n} \u2192 NatF Conat n) \u2192 \u2200 {n} \u2192 Conat n\nConat-in-ho' = Conat-in'\n","old_contents":"------------------------------------------------------------------------------\n-- Co-inductive natural numbers\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule GreatestFixedPoints.Conat where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- Conat is a greatest fixed-point of a functor\n\n-- The functor.\nNatF : (D \u2192 Set) \u2192 D \u2192 Set\nNatF A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n\n-- The co-natural numbers are the greatest fixed-point of NatF.\npostulate\n Conat : D \u2192 Set\n\n -- Conat is a post-fixed point of NatF, i.e.\n --\n -- Conat \u2264 NatF Conat.\n Conat-unf : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\n\n -- The higher-order version.\n Conat-unf-ho : \u2200 {n} \u2192 Conat n \u2192 NatF Conat n\n\n -- Conat is the greatest post-fixed point of NatF, i.e.\n --\n -- \u2200 P. P \u2264 NatF P \u21d2 P \u2264 Conat.\n Conat-coind :\n \u2200 (A : D \u2192 Set) \u2192\n -- A is post-fixed point of ConatF.\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n -- Conat is greater than A.\n \u2200 {n} \u2192 A n \u2192 Conat n\n\n -- The higher-order version.\n Conat-coind-ho :\n \u2200 (A : D \u2192 Set) \u2192 (\u2200 {n} \u2192 A n \u2192 NatF A n) \u2192 \u2200 {n} \u2192 A n \u2192 Conat n\n\n -- 22 December 2013. This is a stronger induction principle. If we\n -- use it, we can use the trivial A = \u03bb x \u2192 x \u2261 x in the\n -- proofs. Unfortunately, we don't have a justification for this\n -- principle.\n Conat-coind-stronger :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n A n \u2192 Conat n\n\n------------------------------------------------------------------------------\n-- Conat-unf and Conat-unf-ho are equivalents\n\nConat-unf' : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\nConat-unf' = Conat-unf-ho\n\nConat-unf-ho' : \u2200 {n} \u2192 Conat n \u2192 NatF Conat n\nConat-unf-ho' = Conat-unf\n\n------------------------------------------------------------------------------\n-- Conat-coind and Conat-coind-ho are equivalents\n\nConat-coind' :\n \u2200 (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind' = Conat-coind-ho\n\nConat-coind-ho' :\n \u2200 (A : D \u2192 Set) \u2192 (\u2200 {n} \u2192 A n \u2192 NatF A n) \u2192 \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind-ho' = Conat-coind\n\n------------------------------------------------------------------------------\n-- From Conat-coind\/Conat-coind-stronger to Conat-coind-stronger\/Conat-coind\n\nConat-coind'' :\n \u2200 (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind'' A h An = Conat-coind-stronger A h An\n\n-- 22 December 2013: We cannot prove Conat-coind-stronger using\n-- Conat-coind.\nConat-coind-stronger'' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n A n \u2192 Conat n\nConat-coind-stronger'' A h An = Conat-coind A {!!} An\n\n------------------------------------------------------------------------------\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Conat predicate is also a pre-fixed point of the functional NatF,\n-- i.e.\n--\n-- NatF Conat \u2264 Conat.\nConat-in : \u2200 {n} \u2192\n n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n') \u2192\n Conat n\nConat-in h = Conat-coind A h' h\n where\n A : D \u2192 Set\n A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\n\n h' : \u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n h' (inj\u2081 n\u22610) = inj\u2081 n\u22610\n h' (inj\u2082 (n' , prf , Cn')) = inj\u2082 (n' , prf , Conat-unf Cn')\n\nConat-in-ho : \u2200 {n} \u2192 NatF Conat n \u2192 Conat n\nConat-in-ho = Conat-in\n\n-- A different definition.\nConat-in' : (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')) \u2192\n \u2200 {n} \u2192 Conat n\nConat-in' h = Conat-coind (\u03bb m \u2192 m \u2261 m) h' refl\n where\n h' : \u2200 {m} \u2192 m \u2261 m \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 m' \u2261 m')\n h' _ with h\n ... | inj\u2081 m\u22610 = inj\u2081 m\u22610\n ... | inj\u2082 (m' , prf , _) = inj\u2082 (m' , prf , refl)\n\nConat-in-ho' : (\u2200 {n} \u2192 NatF Conat n) \u2192 \u2200 {n} \u2192 Conat n\nConat-in-ho' = Conat-in'\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"1c48ce021e56df25a7ff43d8a72ede5b311b39f6","subject":"Minor changes.","message":"Minor changes.\n\nIgnore-this: ef30ed12e72587ba03034ce04f2cfc3c\n\ndarcs-hash:20110805170456-3bd4e-69989ba5a71269fe59cf5f6b2bc15a85ca16fadc.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/FOTC\/Data\/List\/PostulatesVersusDataTypes.agda","new_file":"Draft\/FOTC\/Data\/List\/PostulatesVersusDataTypes.agda","new_contents":"module Draft.FOTC.Data.List.PostulatesVersusDataTypes where\n\n-- See Agda mailing list.\n-- Subject: Agda's unification: postulates versus data types\n\nmodule M\u2081 where\n data D : Set where\n _\u2237_ : D \u2192 D \u2192 D\n\n data List : D \u2192 Set where\n cons : \u2200 x xs \u2192 List xs \u2192 List (x \u2237 xs)\n\n tail : \u2200 {x xs} \u2192 List (x \u2237 xs) \u2192 List xs\n tail {x} {xs} (cons .x .xs xsL) = xsL\n\nmodule M\u2082 where\n postulate\n D : Set\n _\u2237_ : D \u2192 D \u2192 D\n\n data List : D \u2192 Set where\n cons : \u2200 x xs \u2192 List xs \u2192 List (x \u2237 xs)\n\n tail : \u2200 {x xs} \u2192 List (x \u2237 xs) \u2192 List xs\n tail l = {!!} -- C-c C-c fails\n\nmodule M\u2083 where\n postulate D : Set\n\n _\u2237_ : D \u2192 D \u2192 D\n x \u2237 xs = {!!}\n\n data List : D \u2192 Set where\n cons : \u2200 x xs \u2192 List xs \u2192 List (x \u2237 xs)\n\n tail : \u2200 {x xs} \u2192 List (x \u2237 xs) \u2192 List xs\n tail l = {!!} -- C-c C-c fails\n","old_contents":"module Draft.FOTC.Data.List.PostulatesVersusDataTypes where\n\ndata U : Set where\n _::_ : U \u2192 U \u2192 U\n\ndata ListU : U \u2192 Set where\n cons : \u2200 x xs \u2192 ListU xs \u2192 ListU (x :: xs)\n\ntailU : \u2200 {x xs} \u2192 ListU (x :: xs) \u2192 ListU xs\ntailU {x} {xs} (cons .x .xs xsL) = xsL\n\npostulate\n D : Set\n _\u2237_ : D \u2192 D \u2192 D\n\ndata List : D \u2192 Set where\n cons : \u2200 x xs \u2192 List xs \u2192 List (x \u2237 xs)\n\ntail : \u2200 {x xs} \u2192 List (x \u2237 xs) \u2192 List xs\ntail l = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9f817edc266f0802e386fb96bb430f34dc7fe8e4","subject":"Added doc.","message":"Added doc.\n\nIgnore-this: 5f699f5008e64b7007bf8c7d75ecaf1b\n\ndarcs-hash:20120519150052-3bd4e-b7efba30df6ebeca5137647cff8f0bcd11e4c169.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/Common\/Function.agda","new_file":"src\/Common\/Function.agda","new_contents":"------------------------------------------------------------------------------\n-- Operations on and with functions\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Common.Function where\n\n-- The same fixity used in the standard library.\ninfixr 0 _$_\n\n------------------------------------------------------------------------------\n-- The right associative application operator.\n--\n-- N.B. The operator is not first-order, so it cannot be used with\n-- types\/terms which will be translated to FOL.\n_$_ : {A : Set}{B : A \u2192 Set} \u2192 ((x : A) \u2192 B x) \u2192 (x : A) \u2192 B x\nf $ x = f x\n\n-- N.B. The functions is not first-order, so it cannot be used with\n-- types\/terms which will be translated to FOL.\nflip : {A : Set} \u2192 (A \u2192 A \u2192 A) \u2192 A \u2192 A \u2192 A\nflip f y x = f x y\n","old_contents":"------------------------------------------------------------------------------\n-- Operations on and with functions\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Common.Function where\n\n-- The same fixity used in the standard library.\ninfixr 0 _$_\n\n------------------------------------------------------------------------------\n-- The right associative application operator.\n-- N.B. It cannot be used with types\/terms which will be translated to FOL.\n_$_ : {A : Set}{B : A \u2192 Set} \u2192 ((x : A) \u2192 B x) \u2192 (x : A) \u2192 B x\nf $ x = f x\n\n-- N.B. It cannot be used with types\/terms which will be translated to FOL.\nflip : {A : Set} \u2192 (A \u2192 A \u2192 A) \u2192 A \u2192 A \u2192 A\nflip f y x = f x y\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9755ab9f86f1e00b28c36e99421e4e9be389a95f","subject":"Comment out holes to ensure Everything compiles again","message":"Comment out holes to ensure Everything compiles again\n\nOld-commit-hash: dfc28a00f9301015066e19d4be2c4179f8e0849b\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Products.agda","new_file":"Base\/Change\/Products.agda","new_contents":"module Base.Change.Products where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\nopen import Base.Change.Algebra\n\nopen import Data.Product\n\n-- Also try defining sectioned change structures on the positives halves of\n-- groups? Or on arbitrary subsets?\n\n-- Restriction: we pair sets on the same level (because right now everything\n-- else would risk getting in the way).\nmodule ProductChanges \u2113 (A B : Set \u2113) {{CA : ChangeAlgebra \u2113 A}} {{CB : ChangeAlgebra \u2113 B}} where\n open \u2261-Reasoning\n\n -- The simplest possible definition of changes for products.\n\n -- The following is probably bullshit:\n -- Does not handle products of functions - more accurately, writing the\n -- derivative of fst and snd for products of functions is hard: fst' p dp must return the change of fst p\n PChange : A \u00d7 B \u2192 Set \u2113\n PChange (a , b) = \u0394 a \u00d7 \u0394 b\n\n _\u2295_ : (v : A \u00d7 B) \u2192 PChange v \u2192 A \u00d7 B\n _\u2295_ (a , b) (da , db) = a \u229e da , b \u229e db\n _\u229d_ : A \u00d7 B \u2192 (v : A \u00d7 B) \u2192 PChange v\n _\u229d_ (aNew , bNew) (a , b) = aNew \u229f a , bNew \u229f b\n\n p-update-diff : (u v : A \u00d7 B) \u2192 v \u2295 (u \u229d v) \u2261 u\n p-update-diff (ua , ub) (va , vb) =\n let u = (ua , ub)\n v = (va , vb)\n in\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8\u27e9\n (va \u229e (ua \u229f va) , vb \u229e (ub \u229f vb))\n --v \u2295 ((ua \u229f va , ub \u229f vb))\n \u2261\u27e8 cong\u2082 _,_ (update-diff ua va) (update-diff ub vb)\u27e9\n (ua , ub)\n \u2261\u27e8\u27e9\n u\n \u220e\n\n changeAlgebra : ChangeAlgebra \u2113 (A \u00d7 B)\n changeAlgebra = record\n { Change = PChange\n ; update = _\u2295_\n ; diff = _\u229d_\n ; isChangeAlgebra = record\n { update-diff = p-update-diff\n }\n }\n\n proj\u2081\u2032 : (v : A \u00d7 B) \u2192 \u0394 v \u2192 \u0394 (proj\u2081 v)\n proj\u2081\u2032 (a , b) (da , db) = da\n\n proj\u2081\u2032Derivative : Derivative proj\u2081 proj\u2081\u2032\n -- Implementation note: we do not need to pattern match on v and dv because\n -- they are records, hence Agda knows that pattern matching on records cannot\n -- fail. Technically, the required feature is the eta-rule on records.\n proj\u2081\u2032Derivative v dv = refl\n\n -- An extended explanation.\n proj\u2081\u2032Derivative\u2081 : Derivative proj\u2081 proj\u2081\u2032\n proj\u2081\u2032Derivative\u2081 (a , b) (da , db) =\n let v = (a , b)\n dv = (da , db)\n in\n begin\n proj\u2081 v \u229e proj\u2081\u2032 v dv\n \u2261\u27e8\u27e9\n a \u229e da\n \u2261\u27e8\u27e9\n proj\u2081 (v \u229e dv)\n \u220e\n\n -- Same for the second extractor.\n proj\u2082\u2032 : (v : A \u00d7 B) \u2192 \u0394 v \u2192 \u0394 (proj\u2082 v)\n proj\u2082\u2032 (a , b) (da , db) = db\n\n proj\u2082\u2032Derivative : Derivative proj\u2082 proj\u2082\u2032\n proj\u2082\u2032Derivative v dv = refl\n\n -- We should do the same for uncurry instead.\n\n -- What one could wrongly expect to be the derivative of the constructor:\n _,_\u2032 : (a : A) \u2192 (da : \u0394 a) \u2192 (b : B) \u2192 (db : \u0394 b) \u2192 \u0394 (a , b)\n _,_\u2032 a da b db = da , db\n\n -- That has the correct behavior, in a sense, and it would be in the\n -- subset-based formalization in the paper.\n --\n -- But the above is not even a change, because it does not contain a proof of its own validity, and because after application it does not contain a proof\n\n -- As a consequence, proving that's a derivative seems too insanely hard. We\n -- might want to provide a proof schema for curried functions at once,\n -- starting from the right correctness equation.\n\n B\u2192A\u00d7B = FunctionChanges.changeAlgebra {c = \u2113} {d = \u2113} B (A \u00d7 B)\n A\u2192B\u2192A\u00d7B = FunctionChanges.changeAlgebra {c = \u2113} {d = \u2113} A (B \u2192 A \u00d7 B) {{CA}} {{B\u2192A\u00d7B}}\n module \u0394BA\u00d7B = FunctionChanges B (A \u00d7 B) {{CB}} {{changeAlgebra}}\n module \u0394A\u2192B\u2192A\u00d7B = FunctionChanges A (B \u2192 A \u00d7 B) {{CA}} {{B\u2192A\u00d7B}}\n\n\n _,_\u2032-real : \u0394 _,_\n _,_\u2032-real = nil _,_\n _,_\u2032-real-Derivative : Derivative {{CA}} {{B\u2192A\u00d7B}} _,_ (\u0394A\u2192B\u2192A\u00d7B.apply _,_\u2032-real)\n _,_\u2032-real-Derivative =\n FunctionChanges.nil-is-derivative A (B \u2192 A \u00d7 B) {{CA}} {{B\u2192A\u00d7B}} _,_\n\n _,_\u2032\u2032 : (a : A) \u2192 \u0394 a \u2192\n \u0394 {{B\u2192A\u00d7B}} (\u03bb b \u2192 (a , b))\n _,_\u2032\u2032 a da = record\n { apply = _,_\u2032 a da\n ; correct = \u03bb b db \u2192\n begin\n (a , b \u229e db) \u229e (_,_\u2032 a da) (b \u229e db) (nil (b \u229e db))\n \u2261\u27e8\u27e9\n a \u229e da , b \u229e db \u229e (nil (b \u229e db))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 a \u229e da , \u25a1) (update-nil (b \u229e db)) \u27e9\n a \u229e da , b \u229e db\n \u2261\u27e8\u27e9\n (a , b) \u229e (_,_\u2032 a da) b db\n \u220e\n }\n{-\n _,_\u2032\u2032\u2032 : \u0394 {{A\u2192B\u2192A\u00d7B}} _,_\n _,_\u2032\u2032\u2032 = record\n { apply = _,_\u2032\u2032\n ; correct = \u03bb a da \u2192\n begin\n update\n (_,_ (a \u229e da))\n (_,_\u2032\u2032 (a \u229e da) (nil (a \u229e da)))\n \u2261\u27e8 {!!} \u27e9\n update (_,_ a) (_,_\u2032\u2032 a da)\n \u220e\n }\n where\n -- This is needed to use update above.\n -- Passing the change structure seems hard with the given operators; maybe I'm just using them wrongly.\n open ChangeAlgebra B\u2192A\u00d7B hiding (nil)\n{-\n {!\n begin\n (_,_ (a \u229e da)) \u229e _,_\u2032\u2032 (a \u229e da) (nil (a \u229e da))\n {- \u2261\u27e8\u27e9\n a \u229e da , b \u229e db \u229e (nil (b \u229e db)) -}\n \u2261\u27e8 ? \u27e9\n {-a \u229e da , b \u229e db\n \u2261\u27e8\u27e9-}\n (_,_ a) \u229e (_,_\u2032\u2032 a da)\n \u220e!}\n }\n-}\n open import Postulate.Extensionality\n\n\n _,_\u2032Derivative :\n Derivative {{CA}} {{B\u2192A\u00d7B}} _,_ _,_\u2032\u2032\n _,_\u2032Derivative a da =\n begin\n _\u229e_ {{B\u2192A\u00d7B}} (_,_ a) (_,_\u2032\u2032 a da)\n \u2261\u27e8\u27e9\n (\u03bb b \u2192 (a , b) \u229e \u0394BA\u00d7B.apply (_,_\u2032\u2032 a da) b (nil b))\n --ext (\u03bb b \u2192 cong (\u03bb \u25a1 \u2192 (a , b) \u229e \u25a1) (update-nil {{?}} b))\n \u2261\u27e8 {!!} \u27e9\n (\u03bb b \u2192 (a , b) \u229e \u0394BA\u00d7B.apply (_,_\u2032\u2032 a da) b (nil b))\n \u2261\u27e8 sym {!\u0394A\u2192B\u2192A\u00d7B.incrementalization _,_ _,_\u2032\u2032\u2032 a da!} \u27e9\n --FunctionChanges.incrementalization A (B \u2192 A \u00d7 B) {{CA}} {{{!B\u2192A\u00d7B!}}} _,_ {!!} {!!} {!!}\n _,_ (a \u229e da)\n \u220e\n-}\n","old_contents":"module Base.Change.Products where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\nopen import Base.Change.Algebra\n\nopen import Data.Product\n\n-- Also try defining sectioned change structures on the positives halves of\n-- groups? Or on arbitrary subsets?\n\n-- Restriction: we pair sets on the same level (because right now everything\n-- else would risk getting in the way).\nmodule ProductChanges \u2113 (A B : Set \u2113) {{CA : ChangeAlgebra \u2113 A}} {{CB : ChangeAlgebra \u2113 B}} where\n open \u2261-Reasoning\n\n -- The simplest possible definition of changes for products.\n\n -- The following is probably bullshit:\n -- Does not handle products of functions - more accurately, writing the\n -- derivative of fst and snd for products of functions is hard: fst' p dp must return the change of fst p\n PChange : A \u00d7 B \u2192 Set \u2113\n PChange (a , b) = \u0394 a \u00d7 \u0394 b\n\n _\u2295_ : (v : A \u00d7 B) \u2192 PChange v \u2192 A \u00d7 B\n _\u2295_ (a , b) (da , db) = a \u229e da , b \u229e db\n _\u229d_ : A \u00d7 B \u2192 (v : A \u00d7 B) \u2192 PChange v\n _\u229d_ (aNew , bNew) (a , b) = aNew \u229f a , bNew \u229f b\n\n p-update-diff : (u v : A \u00d7 B) \u2192 v \u2295 (u \u229d v) \u2261 u\n p-update-diff (ua , ub) (va , vb) =\n let u = (ua , ub)\n v = (va , vb)\n in\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8\u27e9\n (va \u229e (ua \u229f va) , vb \u229e (ub \u229f vb))\n --v \u2295 ((ua \u229f va , ub \u229f vb))\n \u2261\u27e8 cong\u2082 _,_ (update-diff ua va) (update-diff ub vb)\u27e9\n (ua , ub)\n \u2261\u27e8\u27e9\n u\n \u220e\n\n changeAlgebra : ChangeAlgebra \u2113 (A \u00d7 B)\n changeAlgebra = record\n { Change = PChange\n ; update = _\u2295_\n ; diff = _\u229d_\n ; isChangeAlgebra = record\n { update-diff = p-update-diff\n }\n }\n\n proj\u2081\u2032 : (v : A \u00d7 B) \u2192 \u0394 v \u2192 \u0394 (proj\u2081 v)\n proj\u2081\u2032 (a , b) (da , db) = da\n\n proj\u2081\u2032Derivative : Derivative proj\u2081 proj\u2081\u2032\n -- Implementation note: we do not need to pattern match on v and dv because\n -- they are records, hence Agda knows that pattern matching on records cannot\n -- fail. Technically, the required feature is the eta-rule on records.\n proj\u2081\u2032Derivative v dv = refl\n\n -- An extended explanation.\n proj\u2081\u2032Derivative\u2081 : Derivative proj\u2081 proj\u2081\u2032\n proj\u2081\u2032Derivative\u2081 (a , b) (da , db) =\n let v = (a , b)\n dv = (da , db)\n in\n begin\n proj\u2081 v \u229e proj\u2081\u2032 v dv\n \u2261\u27e8\u27e9\n a \u229e da\n \u2261\u27e8\u27e9\n proj\u2081 (v \u229e dv)\n \u220e\n\n -- Same for the second extractor.\n proj\u2082\u2032 : (v : A \u00d7 B) \u2192 \u0394 v \u2192 \u0394 (proj\u2082 v)\n proj\u2082\u2032 (a , b) (da , db) = db\n\n proj\u2082\u2032Derivative : Derivative proj\u2082 proj\u2082\u2032\n proj\u2082\u2032Derivative v dv = refl\n\n -- We should do the same for uncurry instead.\n\n -- What one could wrongly expect to be the derivative of the constructor:\n _,_\u2032 : (a : A) \u2192 (da : \u0394 a) \u2192 (b : B) \u2192 (db : \u0394 b) \u2192 \u0394 (a , b)\n _,_\u2032 a da b db = da , db\n\n -- That has the correct behavior, in a sense, and it would be in the\n -- subset-based formalization in the paper.\n --\n -- But the above is not even a change, because it does not contain a proof of its own validity, and because after application it does not contain a proof\n\n -- As a consequence, proving that's a derivative seems too insanely hard. We\n -- might want to provide a proof schema for curried functions at once,\n -- starting from the right correctness equation.\n\n B\u2192A\u00d7B = FunctionChanges.changeAlgebra {c = \u2113} {d = \u2113} B (A \u00d7 B)\n A\u2192B\u2192A\u00d7B = FunctionChanges.changeAlgebra {c = \u2113} {d = \u2113} A (B \u2192 A \u00d7 B) {{CA}} {{B\u2192A\u00d7B}}\n module \u0394BA\u00d7B = FunctionChanges B (A \u00d7 B) {{CB}} {{changeAlgebra}}\n module \u0394A\u2192B\u2192A\u00d7B = FunctionChanges A (B \u2192 A \u00d7 B) {{CA}} {{B\u2192A\u00d7B}}\n\n\n _,_\u2032-real : \u0394 _,_\n _,_\u2032-real = nil _,_\n _,_\u2032-real-Derivative : Derivative {{CA}} {{B\u2192A\u00d7B}} _,_ (\u0394A\u2192B\u2192A\u00d7B.apply _,_\u2032-real)\n _,_\u2032-real-Derivative =\n FunctionChanges.nil-is-derivative A (B \u2192 A \u00d7 B) {{CA}} {{B\u2192A\u00d7B}} _,_\n\n _,_\u2032\u2032 : (a : A) \u2192 \u0394 a \u2192\n \u0394 {{B\u2192A\u00d7B}} (\u03bb b \u2192 (a , b))\n _,_\u2032\u2032 a da = record\n { apply = _,_\u2032 a da\n ; correct = \u03bb b db \u2192\n begin\n (a , b \u229e db) \u229e (_,_\u2032 a da) (b \u229e db) (nil (b \u229e db))\n \u2261\u27e8\u27e9\n a \u229e da , b \u229e db \u229e (nil (b \u229e db))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 a \u229e da , \u25a1) (update-nil (b \u229e db)) \u27e9\n a \u229e da , b \u229e db\n \u2261\u27e8\u27e9\n (a , b) \u229e (_,_\u2032 a da) b db\n \u220e\n }\n\n _,_\u2032\u2032\u2032 : \u0394 {{A\u2192B\u2192A\u00d7B}} _,_\n _,_\u2032\u2032\u2032 = record\n { apply = _,_\u2032\u2032\n ; correct = \u03bb a da \u2192\n begin\n update\n (_,_ (a \u229e da))\n (_,_\u2032\u2032 (a \u229e da) (nil (a \u229e da)))\n \u2261\u27e8 {!!} \u27e9\n update (_,_ a) (_,_\u2032\u2032 a da)\n \u220e\n }\n where\n -- This is needed to use update above.\n -- Passing the change structure seems hard with the given operators; maybe I'm just using them wrongly.\n open ChangeAlgebra B\u2192A\u00d7B hiding (nil)\n{-\n {!\n begin\n (_,_ (a \u229e da)) \u229e _,_\u2032\u2032 (a \u229e da) (nil (a \u229e da))\n {- \u2261\u27e8\u27e9\n a \u229e da , b \u229e db \u229e (nil (b \u229e db)) -}\n \u2261\u27e8 ? \u27e9\n {-a \u229e da , b \u229e db\n \u2261\u27e8\u27e9-}\n (_,_ a) \u229e (_,_\u2032\u2032 a da)\n \u220e!}\n }\n-}\n open import Postulate.Extensionality\n\n _,_\u2032Derivative :\n Derivative {{CA}} {{B\u2192A\u00d7B}} _,_ _,_\u2032\u2032\n _,_\u2032Derivative a da =\n begin\n _\u229e_ {{B\u2192A\u00d7B}} (_,_ a) (_,_\u2032\u2032 a da)\n \u2261\u27e8\u27e9\n (\u03bb b \u2192 (a , b) \u229e \u0394BA\u00d7B.apply (_,_\u2032\u2032 a da) b (nil b))\n --ext (\u03bb b \u2192 cong (\u03bb \u25a1 \u2192 (a , b) \u229e \u25a1) (update-nil {{?}} b))\n \u2261\u27e8 {!!} \u27e9\n (\u03bb b \u2192 (a , b) \u229e \u0394BA\u00d7B.apply (_,_\u2032\u2032 a da) b (nil b))\n \u2261\u27e8 sym {!\u0394A\u2192B\u2192A\u00d7B.incrementalization _,_ _,_\u2032\u2032\u2032 a da!} \u27e9\n --FunctionChanges.incrementalization A (B \u2192 A \u00d7 B) {{CA}} {{{!B\u2192A\u00d7B!}}} _,_ {!!} {!!} {!!}\n _,_ (a \u229e da)\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ee41ad5dcc5d4a31d83ce3eb986b4f2f5a9ee268","subject":"lift the left zero of matrix multiplication","message":"lift the left zero of matrix multiplication\n","repos":"DSLsofMath\/DSLsofMath","old_file":"FLABloM\/SquareMatrix.agda","new_file":"FLABloM\/SquareMatrix.agda","new_contents":"-- Matrices indexed by shape\nmodule SquareMatrix where\n\nopen import Shape\nopen import Matrix\n\n-- Square matrices have the same shape in both dimensions\nSq : Set \u2192 Shape \u2192 Set\nSq a shape = M a shape shape\n\nopen import SemiNearRingRecord\nopen import Algebra.Structures\nopen import Relation.Binary\nopen import Data.Product\n\nopen import Relation.Binary.PropositionalEquality hiding (trans; sym) renaming (refl to refl-\u2261)\nimport Relation.Binary.EqReasoning as EqReasoning\n\n-- Lifting a SNR to a to a Square matrix of some shape\nSquare : SemiNearRing \u2192 Shape \u2192 SemiNearRing\nSquare snr shape = SNR\n where\n open SemiNearRing snr\n\n S = Sq s shape\n\n open Operations s _\u2219\u209b_ _+\u209b_\n renaming (_+_ to _+S_; _*_ to _\u2219S_)\n\n 0S : (r c : Shape) \u2192 M s r c\n 0S L L = One 0\u209b\n 0S L (B s s\u2081) = Row (0S L s) (0S L s\u2081)\n 0S (B r r\u2081) L = Col (0S r L) (0S r\u2081 L)\n 0S (B r r\u2081) (B s s\u2081) =\n Q (0S r s) (0S r s\u2081)\n (0S r\u2081 s) (0S r\u2081 s\u2081)\n\n \u2243S : (r c : Shape) \u2192\n M s r c \u2192 M s r c \u2192 Set\n \u2243S L L (One x) (One x\u2081) = x \u2243\u209b x\u2081\n \u2243S L (B c\u2081 c\u2082) (Row m m\u2081) (Row n n\u2081) = \u2243S L c\u2081 m n \u00d7 \u2243S L c\u2082 m\u2081 n\u2081\n \u2243S (B r\u2081 r\u2082) L (Col m m\u2081) (Col n n\u2081) = \u2243S r\u2081 L m n \u00d7 \u2243S r\u2082 L m\u2081 n\u2081\n \u2243S (B r\u2081 r\u2082) (B c\u2081 c\u2082) (Q m00 m01 m10 m11) (Q n00 n01 n10 n11) =\n \u2243S r\u2081 c\u2081 m00 n00 \u00d7 \u2243S r\u2081 c\u2082 m01 n01 \u00d7\n \u2243S r\u2082 c\u2081 m10 n10 \u00d7 \u2243S r\u2082 c\u2082 m11 n11\n\n\n _\u2243S'_ : {r c : Shape} \u2192 M s r c \u2192 M s r c \u2192 Set\n _\u2243S'_ {r} {c} m n = \u2243S r c m n\n\n\n reflS : (r c : Shape) \u2192\n {X : M s r c} \u2192 X \u2243S' X\n reflS L L {X = One x} = refl\u209b {x}\n reflS L (B c\u2081 c\u2082) {X = Row X Y} = reflS L c\u2081 , reflS L c\u2082\n reflS (B r\u2081 r\u2082) L {X = Col X Y} = reflS r\u2081 L , reflS r\u2082 L\n reflS (B r\u2081 r\u2082) (B c\u2081 c\u2082) {X = Q X Y Z W} =\n reflS r\u2081 c\u2081 , reflS r\u2081 c\u2082 ,\n reflS r\u2082 c\u2081 , reflS r\u2082 c\u2082\n\n symS : (r c : Shape) \u2192\n {i j : M s r c} \u2192 i \u2243S' j \u2192 j \u2243S' i\n symS L L {One x} {One x\u2081} p = sym\u209b p\n symS L (B c\u2081 c\u2082) {Row i\u2081 i\u2082} {Row j\u2081 j\u2082} (p , q) = symS L c\u2081 p , symS L c\u2082 q\n symS (B r\u2081 r\u2082) L {Col i\u2081 i} {Col j j\u2081} (p , q) = symS r\u2081 L p , symS r\u2082 L q\n symS (B r r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j j\u2081 j\u2082 j\u2083} (p , q , x , y) =\n symS r c\u2081 p , symS r c\u2082 q ,\n symS r\u2082 c\u2081 x , symS r\u2082 c\u2082 y\n\n transS : (r c : Shape) \u2192\n {i j k : M s r c} \u2192 i \u2243S' j \u2192 j \u2243S' k \u2192 i \u2243S' k\n transS L L {One x} {One x\u2081} {One x\u2082} p q = trans p q\n transS L (B c\u2081 c\u2082) {Row i i\u2081} {Row j j\u2081} {Row k k\u2081} (p , q) (p' , q') =\n transS L c\u2081 p p' , transS L c\u2082 q q'\n transS (B r\u2081 r\u2082) L {Col i i\u2081} {Col j j\u2081} {Col k k\u2081} (p , q) (p' , q') =\n transS r\u2081 L p p' , transS r\u2082 L q q'\n transS (B r\u2081 r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j\u2082 j j\u2083 j\u2081} {Q k k\u2081 k\u2082 k\u2083}\n (p , q , x , y) (p' , q' , x' , y') =\n transS r\u2081 c\u2081 p p' , transS r\u2081 c\u2082 q q' ,\n transS r\u2082 c\u2081 x x' , transS r\u2082 c\u2082 y y'\n\n isEquivS =\n record\n { refl = reflS shape shape\n ; sym = symS shape shape\n ; trans = transS shape shape }\n\n liftAssoc : (r c : Shape) (x y z : M s r c) \u2192 ((x +S y) +S z) \u2243S' (x +S (y +S z))\n liftAssoc L L (One x) (One y) (One z) = assoc\u209b x y z\n liftAssoc L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) (Row z z\u2081) =\n liftAssoc L c x y z , liftAssoc L c\u2081 x\u2081 y\u2081 z\u2081\n liftAssoc (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) (Col z z\u2081) =\n liftAssoc r L x y z , liftAssoc r\u2081 L x\u2081 y\u2081 z\u2081\n liftAssoc (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n (liftAssoc r c x y z) , (liftAssoc r c\u2081 x\u2081 y\u2081 z\u2081) ,\n (liftAssoc r\u2081 c x\u2082 y\u2082 z\u2082) , (liftAssoc r\u2081 c\u2081 x\u2083 y\u2083 z\u2083)\n\n <+S> : (r c : Shape) {x y u v : M s r c} \u2192\n x \u2243S' y \u2192 u \u2243S' v \u2192 (x +S u) \u2243S' (y +S v)\n <+S> L L {One x} {One x\u2081} {One x\u2082} {One x\u2083} p q = p <+> q\n <+S> L (B c c\u2081) {Row x x\u2081} {Row y y\u2081} {Row u u\u2081} {Row v v\u2081} (p , p\u2081) (q , q\u2081) =\n <+S> L c p q , <+S> L c\u2081 p\u2081 q\u2081\n <+S> (B r r\u2081) L {Col x x\u2081} {Col y y\u2081} {Col u u\u2081} {Col v v\u2081} (p , p\u2081) (q , q\u2081) =\n <+S> r L p q , <+S> r\u2081 L p\u2081 q\u2081\n <+S> (B r r\u2081) (B c c\u2081) {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083}\n (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081 , q\u2082 , q\u2083) =\n <+S> r c p q , <+S> r c\u2081 p\u2081 q\u2081 ,\n <+S> r\u2081 c p\u2082 q\u2082 , <+S> r\u2081 c\u2081 p\u2083 q\u2083\n\n isSemgroupS =\n record\n { isEquivalence = isEquivS\n ; assoc = liftAssoc shape shape\n ; \u2219-cong = <+S> shape shape }\n\n\n identS\u02e1 : (r c : Shape) (x : M s r c) \u2192\n 0S r c +S x \u2243S' x\n identS\u02e1 L L (One x) = identity\u02e1\u209b x\n identS\u02e1 L (B c c\u2081) (Row x x\u2081) = identS\u02e1 L c x , identS\u02e1 L c\u2081 x\u2081\n identS\u02e1 (B r r\u2081) L (Col x x\u2081) = identS\u02e1 r L x , identS\u02e1 r\u2081 L x\u2081\n identS\u02e1 (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n identS\u02e1 r c x , identS\u02e1 r c\u2081 x\u2081 ,\n identS\u02e1 r\u2081 c x\u2082 , identS\u02e1 r\u2081 c\u2081 x\u2083\n\n liftComm : (r c : Shape) \u2192 (x y : M s r c) \u2192\n (x +S y) \u2243S' (y +S x)\n liftComm L L (One x) (One x\u2081) = comm\u209b x x\u2081\n liftComm L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) = (liftComm L c x y) , (liftComm L c\u2081 x\u2081 y\u2081)\n liftComm (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) = (liftComm r L x y) , (liftComm r\u2081 L x\u2081 y\u2081)\n liftComm (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) =\n liftComm r c x y , liftComm r c\u2081 x\u2081 y\u2081 ,\n liftComm r\u2081 c x\u2082 y\u2082 , liftComm r\u2081 c\u2081 x\u2083 y\u2083\n\n isCommMonS =\n record\n { isSemigroup = isSemgroupS\n ; identity\u02e1 = identS\u02e1 shape shape\n ; comm = liftComm shape shape }\n\n\n setoidS : (r c : Shape) \u2192 Setoid _ _\n setoidS r c =\n record\n { Carrier = M s r c\n ; _\u2248_ = \u2243S r c\n ; isEquivalence =\n record\n { refl = reflS r c ; sym = symS r c ; trans = transS r c } }\n\n\n zeroS\u02e1 : (a b c : Shape) (x : M s b c) \u2192\n (0S a b \u2219S x) \u2243S' 0S a c\n zeroS\u02e1 L L L (One x) = zero\u02e1 x\n zeroS\u02e1 L L (B c c\u2081) (Row x x\u2081) = (zeroS\u02e1 L L c x) , (zeroS\u02e1 L L c\u2081 x\u2081)\n zeroS\u02e1 L (B b b\u2081) L (Col x x\u2081) =\n let\n open EqReasoning (setoidS L L)\n ih = zeroS\u02e1 L b L x\n ih\u2081 = zeroS\u02e1 L b\u2081 L x\u2081\n in begin\n (Row (0S L b) (0S L b\u2081) \u2219S (Col x x\u2081))\n \u2261\u27e8 refl-\u2261 \u27e9\n (0S L b) \u2219S x +S (0S L b\u2081) \u2219S x\u2081\n \u2248\u27e8 <+S> L L {0S L b \u2219S x} {0S L L} {0S L b\u2081 \u2219S x\u2081} {0S L L} ih ih\u2081 \u27e9 -- TODO: make nicer?\n 0S L L +S 0S L L\n \u2248\u27e8 identS\u02e1 L L (0S L L) \u27e9\n 0S L L\n \u220e\n zeroS\u02e1 L (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (let\n open EqReasoning (setoidS L c)\n ih = zeroS\u02e1 L b c x\n ih\u2081 = zeroS\u02e1 L b\u2081 c x\u2082\n in begin\n 0S L b \u2219S x +S 0S L b\u2081 \u2219S x\u2082\n \u2248\u27e8 <+S> L c ih ih\u2081 \u27e9\n 0S L c +S 0S L c\n \u2248\u27e8 identS\u02e1 L c (0S L c) \u27e9\n 0S L c\n \u220e) ,\n (let\n open EqReasoning (setoidS L c\u2081)\n ih = zeroS\u02e1 L b c\u2081 x\u2081\n ih\u2081 = zeroS\u02e1 L b\u2081 c\u2081 x\u2083\n in begin\n 0S L b \u2219S x\u2081 +S 0S L b\u2081 \u2219S x\u2083\n \u2248\u27e8 <+S> L c\u2081 ih ih\u2081 \u27e9\n 0S L c\u2081 +S 0S L c\u2081\n \u2248\u27e8 identS\u02e1 L c\u2081 _ \u27e9\n 0S L c\u2081\n \u220e)\n zeroS\u02e1 (B a a\u2081) L L (One x) = zeroS\u02e1 a L L (One x) , zeroS\u02e1 a\u2081 L L (One x)\n zeroS\u02e1 (B a a\u2081) L (B c c\u2081) (Row x x\u2081) =\n zeroS\u02e1 a L c x , zeroS\u02e1 a L c\u2081 x\u2081 ,\n zeroS\u02e1 a\u2081 L c x , zeroS\u02e1 a\u2081 L c\u2081 x\u2081\n zeroS\u02e1 (B a a\u2081) (B b b\u2081) L (Col x x\u2081) =\n (let\n open EqReasoning (setoidS a L)\n ih = zeroS\u02e1 a b L x\n ih\u2081 = zeroS\u02e1 a b\u2081 L x\u2081\n in begin\n 0S a b \u2219S x +S 0S a b\u2081 \u2219S x\u2081\n \u2248\u27e8 <+S> a L ih ih\u2081 \u27e9\n 0S a L +S 0S a L\n \u2248\u27e8 identS\u02e1 a L _ \u27e9\n 0S a L\n \u220e) ,\n (let\n open EqReasoning (setoidS a\u2081 L)\n ih = zeroS\u02e1 a\u2081 b L x\n ih\u2081 = zeroS\u02e1 a\u2081 b\u2081 L x\u2081\n in begin\n 0S a\u2081 b \u2219S x +S 0S a\u2081 b\u2081 \u2219S x\u2081\n \u2248\u27e8 <+S> a\u2081 L ih ih\u2081 \u27e9\n 0S a\u2081 L +S 0S a\u2081 L\n \u2248\u27e8 identS\u02e1 a\u2081 L _ \u27e9\n 0S a\u2081 L\n \u220e)\n zeroS\u02e1 (B a a\u2081) (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (let\n open EqReasoning (setoidS a c)\n ih = zeroS\u02e1 a b c x\n ih\u2081 = zeroS\u02e1 a b\u2081 c x\u2082\n in begin\n 0S a b \u2219S x +S 0S a b\u2081 \u2219S x\u2082\n \u2248\u27e8 <+S> a c ih ih\u2081 \u27e9\n 0S a c +S 0S a c\n \u2248\u27e8 identS\u02e1 a c _ \u27e9\n 0S a c\n \u220e) ,\n (let\n open EqReasoning (setoidS a c\u2081)\n ih = zeroS\u02e1 a b c\u2081 x\u2081\n ih\u2081 = zeroS\u02e1 a b\u2081 c\u2081 x\u2083\n in begin\n 0S a b \u2219S x\u2081 +S 0S a b\u2081 \u2219S x\u2083\n \u2248\u27e8 <+S> a c\u2081 ih ih\u2081 \u27e9\n 0S a c\u2081 +S 0S a c\u2081\n \u2248\u27e8 identS\u02e1 a c\u2081 _ \u27e9\n 0S a c\u2081\n \u220e) ,\n (let\n open EqReasoning (setoidS a\u2081 c)\n ih = zeroS\u02e1 a\u2081 b c x\n ih\u2081 = zeroS\u02e1 a\u2081 b\u2081 c x\u2082\n in begin\n 0S a\u2081 b \u2219S x +S 0S a\u2081 b\u2081 \u2219S x\u2082\n \u2248\u27e8 <+S> a\u2081 c ih ih\u2081 \u27e9\n 0S a\u2081 c +S 0S a\u2081 c\n \u2248\u27e8 identS\u02e1 a\u2081 c _ \u27e9\n 0S a\u2081 c\n \u220e) ,\n (let\n open EqReasoning (setoidS a\u2081 c\u2081)\n ih = zeroS\u02e1 a\u2081 b c\u2081 x\u2081\n ih\u2081 = zeroS\u02e1 a\u2081 b\u2081 c\u2081 x\u2083\n in begin\n 0S a\u2081 b \u2219S x\u2081 +S 0S a\u2081 b\u2081 \u2219S x\u2083\n \u2248\u27e8 <+S> a\u2081 c\u2081 ih ih\u2081 \u27e9\n 0S a\u2081 c\u2081 +S 0S a\u2081 c\u2081\n \u2248\u27e8 identS\u02e1 a\u2081 c\u2081 _ \u27e9\n 0S a\u2081 c\u2081\n \u220e)\n\n\n SNR : SemiNearRing\n SNR =\n record\n { s = S\n ; _\u2243\u209b_ = \u2243S shape shape\n ; 0\u209b = 0S shape shape\n ; _+\u209b_ = _+S_\n ; _\u2219\u209b_ = _\u2219S_\n ; isCommMon = isCommMonS\n ; zero\u02e1 = zeroS\u02e1 shape shape shape\n ; zero\u02b3 = {!!}\n ; _<\u2219>_ = {!!}\n }\n","old_contents":"-- Matrices indexed by shape\nmodule SquareMatrix where\n\nopen import Shape\nopen import Matrix\n\n-- Square matrices have the same shape in both dimensions\nSq : Set \u2192 Shape \u2192 Set\nSq a shape = M a shape shape\n\nopen import SemiNearRingRecord\nopen import Algebra.Structures\nopen import Relation.Binary\nopen import Data.Product\n\n-- Lifting a SNR to a to a Square matrix of some shape\nSquare : SemiNearRing \u2192 Shape \u2192 SemiNearRing\nSquare snr shape = SNR\n where\n open SemiNearRing snr\n\n S = Sq s shape\n\n open Operations s _\u2219\u209b_ _+\u209b_\n renaming (_+_ to _+S_; _*_ to _\u2219S_)\n\n 0S : (r c : Shape) \u2192 M s r c\n 0S L L = One 0\u209b\n 0S L (B s s\u2081) = Row (0S L s) (0S L s\u2081)\n 0S (B r r\u2081) L = Col (0S r L) (0S r\u2081 L)\n 0S (B r r\u2081) (B s s\u2081) =\n Q (0S r s) (0S r s\u2081)\n (0S r\u2081 s) (0S r\u2081 s\u2081)\n\n \u2243S : (r c : Shape) \u2192\n M s r c \u2192 M s r c \u2192 Set\n \u2243S L L (One x) (One x\u2081) = x \u2243\u209b x\u2081\n \u2243S L (B c\u2081 c\u2082) (Row m m\u2081) (Row n n\u2081) = \u2243S L c\u2081 m n \u00d7 \u2243S L c\u2082 m\u2081 n\u2081\n \u2243S (B r\u2081 r\u2082) L (Col m m\u2081) (Col n n\u2081) = \u2243S r\u2081 L m n \u00d7 \u2243S r\u2082 L m\u2081 n\u2081\n \u2243S (B r\u2081 r\u2082) (B c\u2081 c\u2082) (Q m00 m01 m10 m11) (Q n00 n01 n10 n11) =\n \u2243S r\u2081 c\u2081 m00 n00 \u00d7 \u2243S r\u2081 c\u2082 m01 n01 \u00d7\n \u2243S r\u2082 c\u2081 m10 n10 \u00d7 \u2243S r\u2082 c\u2082 m11 n11\n\n\n _\u2243S'_ : {r c : Shape} \u2192 M s r c \u2192 M s r c \u2192 Set\n _\u2243S'_ {r} {c} m n = \u2243S r c m n\n\n\n reflS : (r c : Shape) \u2192\n {X : M s r c} \u2192 X \u2243S' X\n reflS L L {X = One x} = refl\u209b {x}\n reflS L (B c\u2081 c\u2082) {X = Row X Y} = reflS L c\u2081 , reflS L c\u2082\n reflS (B r\u2081 r\u2082) L {X = Col X Y} = reflS r\u2081 L , reflS r\u2082 L\n reflS (B r\u2081 r\u2082) (B c\u2081 c\u2082) {X = Q X Y Z W} =\n reflS r\u2081 c\u2081 , reflS r\u2081 c\u2082 ,\n reflS r\u2082 c\u2081 , reflS r\u2082 c\u2082\n\n symS : (r c : Shape) \u2192\n {i j : M s r c} \u2192 i \u2243S' j \u2192 j \u2243S' i\n symS L L {One x} {One x\u2081} p = sym\u209b p\n symS L (B c\u2081 c\u2082) {Row i\u2081 i\u2082} {Row j\u2081 j\u2082} (p , q) = symS L c\u2081 p , symS L c\u2082 q\n symS (B r\u2081 r\u2082) L {Col i\u2081 i} {Col j j\u2081} (p , q) = symS r\u2081 L p , symS r\u2082 L q\n symS (B r r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j j\u2081 j\u2082 j\u2083} (p , q , x , y) =\n symS r c\u2081 p , symS r c\u2082 q ,\n symS r\u2082 c\u2081 x , symS r\u2082 c\u2082 y\n\n transS : (r c : Shape) \u2192\n {i j k : M s r c} \u2192 i \u2243S' j \u2192 j \u2243S' k \u2192 i \u2243S' k\n transS L L {One x} {One x\u2081} {One x\u2082} p q = trans p q\n transS L (B c\u2081 c\u2082) {Row i i\u2081} {Row j j\u2081} {Row k k\u2081} (p , q) (p' , q') =\n transS L c\u2081 p p' , transS L c\u2082 q q'\n transS (B r\u2081 r\u2082) L {Col i i\u2081} {Col j j\u2081} {Col k k\u2081} (p , q) (p' , q') =\n transS r\u2081 L p p' , transS r\u2082 L q q'\n transS (B r\u2081 r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j\u2082 j j\u2083 j\u2081} {Q k k\u2081 k\u2082 k\u2083}\n (p , q , x , y) (p' , q' , x' , y') =\n transS r\u2081 c\u2081 p p' , transS r\u2081 c\u2082 q q' ,\n transS r\u2082 c\u2081 x x' , transS r\u2082 c\u2082 y y'\n\n isEquivS =\n record\n { refl = reflS shape shape\n ; sym = symS shape shape\n ; trans = transS shape shape }\n\n liftAssoc : (r c : Shape) (x y z : M s r c) \u2192 ((x +S y) +S z) \u2243S' (x +S (y +S z))\n liftAssoc L L (One x) (One y) (One z) = assoc\u209b x y z\n liftAssoc L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) (Row z z\u2081) =\n liftAssoc L c x y z , liftAssoc L c\u2081 x\u2081 y\u2081 z\u2081\n liftAssoc (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) (Col z z\u2081) =\n liftAssoc r L x y z , liftAssoc r\u2081 L x\u2081 y\u2081 z\u2081\n liftAssoc (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n (liftAssoc r c x y z) , (liftAssoc r c\u2081 x\u2081 y\u2081 z\u2081) ,\n (liftAssoc r\u2081 c x\u2082 y\u2082 z\u2082) , (liftAssoc r\u2081 c\u2081 x\u2083 y\u2083 z\u2083)\n\n lift<+> : (r c : Shape) {x y u v : M s r c} \u2192\n x \u2243S' y \u2192 u \u2243S' v \u2192 (x +S u) \u2243S' (y +S v)\n lift<+> L L {One x} {One x\u2081} {One x\u2082} {One x\u2083} p q = p <+> q\n lift<+> L (B c c\u2081) {Row x x\u2081} {Row y y\u2081} {Row u u\u2081} {Row v v\u2081} (p , p\u2081) (q , q\u2081) =\n lift<+> L c p q , lift<+> L c\u2081 p\u2081 q\u2081\n lift<+> (B r r\u2081) L {Col x x\u2081} {Col y y\u2081} {Col u u\u2081} {Col v v\u2081} (p , p\u2081) (q , q\u2081) =\n lift<+> r L p q , lift<+> r\u2081 L p\u2081 q\u2081\n lift<+> (B r r\u2081) (B c c\u2081) {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083}\n (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081 , q\u2082 , q\u2083) =\n lift<+> r c p q , lift<+> r c\u2081 p\u2081 q\u2081 ,\n lift<+> r\u2081 c p\u2082 q\u2082 , lift<+> r\u2081 c\u2081 p\u2083 q\u2083\n\n isSemgroupS =\n record\n { isEquivalence = isEquivS\n ; assoc = liftAssoc shape shape\n ; \u2219-cong = lift<+> shape shape }\n\n\n liftIdent\u02e1 : (r c : Shape) (x : M s r c) \u2192\n 0S r c +S x \u2243S' x\n liftIdent\u02e1 L L (One x) = identity\u02e1\u209b x\n liftIdent\u02e1 L (B c c\u2081) (Row x x\u2081) = liftIdent\u02e1 L c x , liftIdent\u02e1 L c\u2081 x\u2081\n liftIdent\u02e1 (B r r\u2081) L (Col x x\u2081) = liftIdent\u02e1 r L x , liftIdent\u02e1 r\u2081 L x\u2081\n liftIdent\u02e1 (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n liftIdent\u02e1 r c x , liftIdent\u02e1 r c\u2081 x\u2081 ,\n liftIdent\u02e1 r\u2081 c x\u2082 , liftIdent\u02e1 r\u2081 c\u2081 x\u2083\n\n liftComm : (r c : Shape) \u2192 (x y : M s r c) \u2192\n (x +S y) \u2243S' (y +S x)\n liftComm L L (One x) (One x\u2081) = comm\u209b x x\u2081\n liftComm L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) = (liftComm L c x y) , (liftComm L c\u2081 x\u2081 y\u2081)\n liftComm (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) = (liftComm r L x y) , (liftComm r\u2081 L x\u2081 y\u2081)\n liftComm (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) =\n liftComm r c x y , liftComm r c\u2081 x\u2081 y\u2081 ,\n liftComm r\u2081 c x\u2082 y\u2082 , liftComm r\u2081 c\u2081 x\u2083 y\u2083\n\n isCommMonS =\n record\n { isSemigroup = isSemgroupS\n ; identity\u02e1 = liftIdent\u02e1 shape shape\n ; comm = liftComm shape shape }\n\n\n -- TODO: can I use \u2243S to 'rewrite' types?\n liftZero\u02e1 : (a b c : Shape) (x : M s b c) \u2192\n let 0\u02e1 = 0S a b\n 0\u02b3 = 0S a c\n in (0\u02e1 \u2219S x) \u2243S' 0\u02b3\n liftZero\u02e1 L L L (One x) = zero\u02e1 x\n liftZero\u02e1 L L (B c c\u2081) (Row x x\u2081) = (liftZero\u02e1 L L c x) , (liftZero\u02e1 L L c\u2081 x\u2081)\n liftZero\u02e1 L (B b b\u2081) L (Col x x\u2081) = {!!}\n liftZero\u02e1 L (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) = {!!} , {!!}\n liftZero\u02e1 (B a a\u2081) L L (One x) = liftZero\u02e1 a L L (One x) , liftZero\u02e1 a\u2081 L L (One x)\n liftZero\u02e1 (B a a\u2081) L (B c c\u2081) (Row x x\u2081) =\n liftZero\u02e1 a L c x , liftZero\u02e1 a L c\u2081 x\u2081 ,\n liftZero\u02e1 a\u2081 L c x , liftZero\u02e1 a\u2081 L c\u2081 x\u2081\n liftZero\u02e1 (B a a\u2081) (B b b\u2081) L (Col x x\u2081) = {!!} , {!!}\n liftZero\u02e1 (B a a\u2081) (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n {!liftZero\u02e1 a b c x!} , ({!!} , ({!!} , {!!}))\n\n\n SNR : SemiNearRing\n SNR =\n record\n { s = S\n ; _\u2243\u209b_ = \u2243S shape shape\n ; 0\u209b = 0S shape shape\n ; _+\u209b_ = _+S_\n ; _\u2219\u209b_ = _\u2219S_\n ; isCommMon = isCommMonS\n ; zero\u02e1 = liftZero\u02e1 shape shape shape\n ; zero\u02b3 = {!!}\n ; _<\u2219>_ = {!!}\n }\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"07b3b64ff9795f6941b64231e7b2a0dd630e0de4","subject":"Update the Examples","message":"Update the Examples\n","repos":"crypto-agda\/explore","old_file":"lib\/Explore\/Examples.agda","new_file":"lib\/Explore\/Examples.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule Explore.Examples where\n\nopen import Type\nopen import Level.NP\nopen import Data.Maybe.NP\nopen import Data.List\nopen import Data.Zero\nopen import Data.One\nopen import Data.Two\nopen import Data.Product\nopen import Data.Sum.NP\nopen import HoTT using (UA)\nopen import Function.NP\nopen import Function.Extensionality using (FunExt)\nopen import Relation.Binary.PropositionalEquality hiding ([_])\n\nopen import Explore.Core\nopen import Explore.Explorable\nopen import Explore.Universe.Type {\ud835\udfd8}\nopen import Explore.Universe.Base\nopen import Explore.Monad {\u2080} \u2080 public renaming (map to map-explore)\nopen import Explore.Two\nopen import Explore.Product\nopen Explore.Product.Operators\n\nmodule E where\n open FromExplore public\n\nmodule M {Msg Digest : \u2605}\n (_==_ : Digest \u2192 Digest \u2192 \ud835\udfda)\n (H : Msg \u2192 Digest)\n (exploreMsg : \u2200 {\u2113} \u2192 Explore \u2113 Msg)\n (d : Digest) where\n\n module V1 where\n list-H\u207b\u00b9 : List Msg\n list-H\u207b\u00b9 = exploreMsg [] _++_ (\u03bb m \u2192 [0: [] 1: [ m ] ] (H m == d))\n\n module ExploreMsg = FromExplore {A = Msg} exploreMsg\n\n module V2 where\n first-H\u207b\u00b9 : Maybe Msg\n first-H\u207b\u00b9 = ExploreMsg.findKey (\u03bb m \u2192 H m == d)\n\n module V3 where\n explore-H\u207b\u00b9 : Explore \u2080 Msg\n explore-H\u207b\u00b9 \u03b5 _\u2295_ f = exploreMsg \u03b5 _\u2295_ (\u03bb m \u2192 [0: \u03b5 1: f m ] (H m == d))\n\n module V4 where\n explore-H\u207b\u00b9 : Explore \u2080 Msg\n explore-H\u207b\u00b9 = exploreMsg >>= \u03bb m \u2192 [0: empty-explore 1: point-explore m ] (H m == d)\n\n module V5 where\n\n explore-H\u207b\u00b9 : \u2200 {\u2113} \u2192 Explore \u2113 Msg\n explore-H\u207b\u00b9 = filter-explore (\u03bb m \u2192 H m == d) exploreMsg\n\n list-H\u207b\u00b9 : List Msg\n list-H\u207b\u00b9 = E.list explore-H\u207b\u00b9\n\n first-H\u207b\u00b9 : Maybe Msg\n first-H\u207b\u00b9 = E.first explore-H\u207b\u00b9\n\n module V6 where\n explore-H\u207b\u00b9 : \u2200 {\u2113} \u2192 Explore \u2113 Msg\n explore-H\u207b\u00b9 = explore-endo (filter-explore (\u03bb m \u2192 H m == d) exploreMsg)\n\n list-H\u207b\u00b9 : List Msg\n list-H\u207b\u00b9 = E.list explore-H\u207b\u00b9\n\n first-H\u207b\u00b9 : Maybe Msg\n first-H\u207b\u00b9 = E.first explore-H\u207b\u00b9\n\n last-H\u207b\u00b9 : Maybe Msg\n last-H\u207b\u00b9 = E.last explore-H\u207b\u00b9\n\nMsg = \ud835\udfda \u00d7 \ud835\udfda\nDigest = \ud835\udfda\n-- _==_ : Digest \u2192 Digest \u2192 \ud835\udfda\nH : Msg \u2192 Digest\nH (x , y) = x xor y\nMsg\u1d49 : \u2200 {\u2113} \u2192 Explore \u2113 Msg\nMsg\u1d49 = \ud835\udfda\u1d49 \u00d7\u1d49 \ud835\udfda\u1d49\nmodule N5 = M.V5 _==_ H Msg\u1d49\nmodule N6 = M.V6 _==_ H Msg\u1d49\ntest5 = N5.list-H\u207b\u00b9\ntest6-list : N6.list-H\u207b\u00b9 0\u2082 \u2261 (0\u2082 , 0\u2082) \u2237 (1\u2082 , 1\u2082) \u2237 []\ntest6-list = refl\ntest6-rev-list : E.list (E.backward (N6.explore-H\u207b\u00b9 0\u2082)) \u2261 (1\u2082 , 1\u2082) \u2237 (0\u2082 , 0\u2082) \u2237 []\ntest6-rev-list = refl\ntest6-first : N6.first-H\u207b\u00b9 0\u2082 \u2261 just (0\u2082 , 0\u2082)\ntest6-first = refl\ntest6-last : N6.last-H\u207b\u00b9 0\u2082 \u2261 just (1\u2082 , 1\u2082)\ntest6-last = refl\n-- -}\n\n\ud835\udfdb\u1d41 : U\n\ud835\udfdb\u1d41 = \ud835\udfd9\u1d41 \u228e\u1d41 \ud835\udfda\u1d41\n\nprop-\u2227-comm : \ud835\udfda \u00d7 \ud835\udfda \u2192 \ud835\udfda\nprop-\u2227-comm (x , y) = x \u2227 y == y \u2227 x\n\nmodule _ {{_ : UA}}{{_ : FunExt}} where\n check-\u2227-comm : \u2200 x y \u2192 \u2713 (x \u2227 y == y \u2227 x)\n check-\u2227-comm x y = check! (\ud835\udfda\u1d41 \u00d7\u1d41 \ud835\udfda\u1d41) prop-\u2227-comm (x , y)\n\nprop-\u2227-\u2228-distr : \ud835\udfda \u00d7 \ud835\udfda \u00d7 \ud835\udfda \u2192 \ud835\udfda\nprop-\u2227-\u2228-distr (x , y , z) = x \u2227 (y \u2228 z) == x \u2227 y \u2228 x \u2227 z\n\nmodule _ {{_ : UA}}{{_ : FunExt}} where\n check-\u2227-\u2228-distr : \u2200 x y z \u2192 \u2713 (x \u2227 (y \u2228 z) == x \u2227 y \u2228 x \u2227 z)\n check-\u2227-\u2228-distr x y z =\n check! (\ud835\udfda\u1d41 \u00d7\u1d41 \ud835\udfda\u1d41 \u00d7\u1d41 \ud835\udfda\u1d41) prop-\u2227-\u2228-distr (x , y , z)\n\nlist22 = list (\ud835\udfda\u1d41 \u2192\u1d41 \ud835\udfda\u1d41)\nlist33 = list (\ud835\udfdb\u1d41 \u2192\u1d41 \ud835\udfdb\u1d41)\n\n{-\nmodule _ {{_ : UA}}{{_ : FunExt}} where\n module _ (f\u1d41 : El (\ud835\udfda\u1d41 \u2192\u1d41 \ud835\udfda\u1d41)) x where\n f = \u2192\u1d41\u2192\u2192 \ud835\udfda\u1d41 \ud835\udfda\u1d41 f\u1d41\n check22 : \u2713 (f x == f (f (f x)))\n check22 = check! ((\ud835\udfda\u1d41 \u2192\u1d41 \ud835\udfda\u1d41) \u00d7\u1d41 \ud835\udfda\u1d41) (\u03bb { (f , x) \u2192 let f' = \u2192\u1d41\u2192\u2192 \ud835\udfda\u1d41 \ud835\udfda\u1d41 f in f' x == f' (f' (f' x)) }) {{!!}} ((f 0\u2082 , f 1\u2082) , x)\n {-\n check22 : \u2200 (f : \ud835\udfda \u2192 \ud835\udfda) x \u2192 \u2713 (f x == f (f (f x)))\n check22 f x = let k = check! ((\ud835\udfda\u1d41 \u2192\u1d41 \ud835\udfda\u1d41) \u00d7\u1d41 \ud835\udfda\u1d41) (\u03bb { (f , x) \u2192 let f' = \u2192\u1d41\u2192\u2192 \ud835\udfda\u1d41 \ud835\udfda\u1d41 f in f' x == f' (f' (f' x)) }) {{!!}} ((f 0\u2082 , f 1\u2082) , x) in {!k!}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule Explore.Examples where\n\nopen import Type\nopen import Level.NP\nopen import Data.Maybe.NP\nopen import Data.List\nopen import Data.Two\nopen import Data.Product\nopen import Data.Sum.NP\nopen import Data.One\nopen import HoTT using (UA)\nopen import Function.Extensionality using (FunExt)\nopen import Relation.Binary.PropositionalEquality hiding ([_])\n\nopen import Explore.Core\nopen import Explore.Explorable\nopen import Explore.Universe.Base\nopen import Explore.Monad {\u2080} \u2080 public renaming (map to map-explore)\nopen import Explore.Two\nopen import Explore.Product\nopen Explore.Product.Operators\n\nmodule E where\n open FromExplore public\n\nmodule M {Msg Digest : \u2605}\n (_==_ : Digest \u2192 Digest \u2192 \ud835\udfda)\n (H : Msg \u2192 Digest)\n (exploreMsg : \u2200 {\u2113} \u2192 Explore \u2113 Msg)\n (d : Digest) where\n\n module V1 where\n list-H\u207b\u00b9 : List Msg\n list-H\u207b\u00b9 = exploreMsg [] _++_ (\u03bb m \u2192 [0: [] 1: [ m ] ] (H m == d))\n\n module ExploreMsg = FromExplore {A = Msg} exploreMsg\n\n module V2 where\n first-H\u207b\u00b9 : Maybe Msg\n first-H\u207b\u00b9 = ExploreMsg.findKey (\u03bb m \u2192 H m == d)\n\n module V3 where\n explore-H\u207b\u00b9 : Explore \u2080 Msg\n explore-H\u207b\u00b9 \u03b5 _\u2295_ f = exploreMsg \u03b5 _\u2295_ (\u03bb m \u2192 [0: \u03b5 1: f m ] (H m == d))\n\n module V4 where\n explore-H\u207b\u00b9 : Explore \u2080 Msg\n explore-H\u207b\u00b9 = exploreMsg >>= \u03bb m \u2192 [0: empty-explore 1: point-explore m ] (H m == d)\n\n module V5 where\n\n explore-H\u207b\u00b9 : \u2200 {\u2113} \u2192 Explore \u2113 Msg\n explore-H\u207b\u00b9 = filter-explore (\u03bb m \u2192 H m == d) exploreMsg\n\n list-H\u207b\u00b9 : List Msg\n list-H\u207b\u00b9 = E.list explore-H\u207b\u00b9\n\n first-H\u207b\u00b9 : Maybe Msg\n first-H\u207b\u00b9 = E.first explore-H\u207b\u00b9\n\n module V6 where\n explore-H\u207b\u00b9 : \u2200 {\u2113} \u2192 Explore \u2113 Msg\n explore-H\u207b\u00b9 = explore-endo (filter-explore (\u03bb m \u2192 H m == d) exploreMsg)\n\n list-H\u207b\u00b9 : List Msg\n list-H\u207b\u00b9 = E.list explore-H\u207b\u00b9\n\n first-H\u207b\u00b9 : Maybe Msg\n first-H\u207b\u00b9 = E.first explore-H\u207b\u00b9\n\n last-H\u207b\u00b9 : Maybe Msg\n last-H\u207b\u00b9 = E.last explore-H\u207b\u00b9\n\nMsg = \ud835\udfda \u00d7 \ud835\udfda\nDigest = \ud835\udfda\n-- _==_ : Digest \u2192 Digest \u2192 \ud835\udfda\nH : Msg \u2192 Digest\nH (x , y) = x xor y\nMsg\u1d49 : \u2200 {\u2113} \u2192 Explore \u2113 Msg\nMsg\u1d49 = \ud835\udfda\u1d49 \u00d7\u1d49 \ud835\udfda\u1d49\nmodule N5 = M.V5 _==_ H Msg\u1d49\nmodule N6 = M.V6 _==_ H Msg\u1d49\ntest5 = N5.list-H\u207b\u00b9\ntest6-list : N6.list-H\u207b\u00b9 0\u2082 \u2261 (0\u2082 , 0\u2082) \u2237 (1\u2082 , 1\u2082) \u2237 []\ntest6-list = refl\ntest6-rev-list : E.list (E.backward (N6.explore-H\u207b\u00b9 0\u2082)) \u2261 (1\u2082 , 1\u2082) \u2237 (0\u2082 , 0\u2082) \u2237 []\ntest6-rev-list = refl\ntest6-first : N6.first-H\u207b\u00b9 0\u2082 \u2261 just (0\u2082 , 0\u2082)\ntest6-first = refl\ntest6-last : N6.last-H\u207b\u00b9 0\u2082 \u2261 just (1\u2082 , 1\u2082)\ntest6-last = refl\n-- -}\n\n{-\n\ud835\udfdb\u1d41 : U\n\ud835\udfdb\u1d41 = \ud835\udfd9\u1d41 \u228e\u1d41 \ud835\udfda\u1d41\n\nlist22 = list (\ud835\udfda\u1d41 \u2192\u1d41 \ud835\udfda\u1d41)\nlist33 = list (\ud835\udfdb\u1d41 \u2192\u1d41 \ud835\udfdb\u1d41)\n-}\n\n{-\nmodule _ {{_ : UA}}{{_ : FunExt}} where\n check22 : \u2200 (f : \ud835\udfda \u2192 \ud835\udfda) x \u2192 \u2713 (f x == f (f (f x)))\n check22 f x = {!check! ((\ud835\udfda\u1d41 \u2192\u1d41 \ud835\udfda\u1d41) \u00d7\u1d41 \ud835\udfda\u1d41) (\u03bb { (f , x) \u2192 let f' = \u2192\u1d41\u2192\u2192 \ud835\udfda\u1d41 \ud835\udfda\u1d41 f in f' x == f' (f' (f' x)) }) {{!!}} ((f 0\u2082 , f 1\u2082) , x)!}\n-}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"a2b2f80eb2dc1347647f543f266d07cf0d394055","subject":"Added subst\u2082 and subst\u2084.","message":"Added subst\u2082 and subst\u2084.\n\nIgnore-this: 1eeae450127ce8aab0562fe232b5adb6\n\ndarcs-hash:20101230033155-3bd4e-4bbb40106385aee572b763114a84201e41cb3489.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/Common\/Relation\/Binary\/PropositionalEquality.agda","new_file":"src\/Common\/Relation\/Binary\/PropositionalEquality.agda","new_contents":"------------------------------------------------------------------------------\n-- Propositional equality\n------------------------------------------------------------------------------\n\nmodule Common.Relation.Binary.PropositionalEquality where\n\nopen import Common.Universe using ( D )\n\n-- We add 3 to the fixities of the standard library.\ninfix 7 _\u2261_\n\n------------------------------------------------------------------------------\n-- The identity type on the universe of discourse.\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n-- Identity properties\n\nsym : {x y : D} \u2192 x \u2261 y \u2192 y \u2261 x\nsym refl = refl\n\ntrans : {x y z : D} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\ntrans refl y\u2261z = y\u2261z\n\nsubst : (P : D \u2192 Set){x y : D} \u2192 x \u2261 y \u2192 P x \u2192 P y\nsubst P refl px = px\n\nsubst\u2082 : (P : D \u2192 D \u2192 Set){x\u2081 x\u2082 y\u2081 y\u2082 : D} \u2192 x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192 P x\u2081 x\u2082 \u2192\n P y\u2081 y\u2082\nsubst\u2082 P refl refl Px\u2081x\u2082 = Px\u2081x\u2082\n\nsubst\u2084 : (P : D \u2192 D \u2192 D \u2192 D \u2192 Set){x\u2081 x\u2082 x\u2083 x\u2084 y\u2081 y\u2082 y\u2083 y\u2084 : D} \u2192\n x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192 x\u2083 \u2261 y\u2083 \u2192 x\u2084 \u2261 y\u2084 \u2192 P x\u2081 x\u2082 x\u2083 x\u2084 \u2192\n P y\u2081 y\u2082 y\u2083 y\u2084\nsubst\u2084 P refl refl refl refl Px\u2081x\u2082x\u2083x\u2084 = Px\u2081x\u2082x\u2083x\u2084\n","old_contents":"------------------------------------------------------------------------------\n-- Propositional equality\n------------------------------------------------------------------------------\n\nmodule Common.Relation.Binary.PropositionalEquality where\n\nopen import Common.Universe using ( D )\n\n-- We add 3 to the fixities of the standard library.\ninfix 7 _\u2261_\n\n------------------------------------------------------------------------------\n-- The identity type on the universe of discourse.\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n-- Identity properties\n\nsym : {x y : D} \u2192 x \u2261 y \u2192 y \u2261 x\nsym refl = refl\n\ntrans : {x y z : D} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\ntrans refl y\u2261z = y\u2261z\n\nsubst : (P : D \u2192 Set){x y : D} \u2192 x \u2261 y \u2192 P x \u2192 P y\nsubst P refl px = px\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d049e0b6b3b5223ab566d6bf96810ae399bc3889","subject":"Cosmetic change.","message":"Cosmetic change.\n\nIgnore-this: b8c55355628668b9f638d321205a4fb2\n\ndarcs-hash:20120217235342-3bd4e-3344406004ebcaedec8b6c2e5d45eb44b360df48.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/Common\/Relation\/Binary\/PropositionalEquality.agda","new_file":"src\/Common\/Relation\/Binary\/PropositionalEquality.agda","new_contents":"------------------------------------------------------------------------------\n-- Propositional equality\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module is re-exported by (some) \"base\" modules.\n\nmodule Common.Relation.Binary.PropositionalEquality where\n\nopen import Common.Universe using ( D )\n\n------------------------------------------------------------------------------\n-- The propositional equality via pattern matching.\n\nmodule Inductive where\n -- We add 3 to the fixities of the standard library.\n infix 7 _\u2261_\n\n -- The identity type on the universe of discourse.\n data _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n -- Identity properties\n\n sym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\n sym refl = refl\n\n trans : \u2200 {x y z} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n trans refl h = h\n\n trans\u2082 : \u2200 {w x y z} \u2192 w \u2261 x \u2192 x \u2261 y \u2192 y \u2261 z \u2192 w \u2261 z\n trans\u2082 refl refl h = h\n\n subst : (P : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 P x \u2192 P y\n subst P refl Px = Px\n\n subst\u2082 : (P : D \u2192 D \u2192 Set) \u2192 \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192\n x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192\n P x\u2081 x\u2082 \u2192\n P y\u2081 y\u2082\n subst\u2082 P refl refl Pxs = Pxs\n\n subst\u2083 : (P : D \u2192 D \u2192 D \u2192 Set) \u2192 \u2200 {x\u2081 x\u2082 x\u2083 y\u2081 y\u2082 y\u2083} \u2192\n x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192 x\u2083 \u2261 y\u2083 \u2192\n P x\u2081 x\u2082 x\u2083 \u2192\n P y\u2081 y\u2082 y\u2083\n subst\u2083 P refl refl refl Pxs = Pxs\n\n subst\u2084 : (P : D \u2192 D \u2192 D \u2192 D \u2192 Set) \u2192 \u2200 {x\u2081 x\u2082 x\u2083 x\u2084 y\u2081 y\u2082 y\u2083 y\u2084} \u2192\n x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192 x\u2083 \u2261 y\u2083 \u2192 x\u2084 \u2261 y\u2084 \u2192\n P x\u2081 x\u2082 x\u2083 x\u2084 \u2192\n P y\u2081 y\u2082 y\u2083 y\u2084\n subst\u2084 P refl refl refl refl Pxs = Pxs\n\n cong : (f : D \u2192 D) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2261 f y\n cong f refl = refl\n\n cong\u2082 : (f : D \u2192 D \u2192 D) \u2192 \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192\n f x\u2081 x\u2082 \u2261 f y\u2081 y\u2082\n cong\u2082 f refl refl = refl\n\n------------------------------------------------------------------------------\n-- The propositional equality via its induction principle.\n\nmodule NonInductive where\n\n -- We add 3 to the fixities of the standard library.\n infix 7 _\u2261_\n\n -- The identity type on the universe of discourse.\n postulate\n _\u2261_ : D \u2192 D \u2192 Set\n refl : \u2200 {x} \u2192 x \u2261 x\n J : (P : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 P x \u2192 P y\n\n -- Identity properties\n\n sym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\n sym {x} h = J (\u03bb y' \u2192 y' \u2261 x) h refl\n\n trans : \u2200 {x y z} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n trans {x} h\u2081 h\u2082 = J (\u03bb y' \u2192 x \u2261 y') h\u2082 h\u2081\n\n trans\u2082 : \u2200 {w x y z} \u2192 w \u2261 x \u2192 x \u2261 y \u2192 y \u2261 z \u2192 w \u2261 z\n trans\u2082 h\u2081 h\u2082 h\u2083 = trans (trans h\u2081 h\u2082) h\u2083\n\n subst : (P : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 P x \u2192 P y\n subst = J\n\n subst\u2082 : (P : D \u2192 D \u2192 Set) \u2192 \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192\n x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192\n P x\u2081 x\u2082 \u2192\n P y\u2081 y\u2082\n subst\u2082 P {x\u2081} {x\u2082} {y\u2081} {y\u2082} h\u2081 h\u2082 h\u2083 =\n subst (\u03bb y\u2081' \u2192 P y\u2081' y\u2082) h\u2081 (subst (\u03bb y\u2082' \u2192 P x\u2081 y\u2082') h\u2082 h\u2083)\n\n cong : (f : D \u2192 D) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2261 f y\n cong f {x} h = subst (\u03bb x' \u2192 f x \u2261 f x') h refl\n\n cong\u2082 : (f : D \u2192 D \u2192 D) \u2192 \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192\n f x\u2081 x\u2082 \u2261 f y\u2081 y\u2082\n cong\u2082 f {x\u2081} {x\u2082} {y\u2081} {y\u2082} h\u2081 h\u2082 =\n subst (\u03bb x\u2081' \u2192 f x\u2081 x\u2082 \u2261 f x\u2081' y\u2082)\n h\u2081\n (subst (\u03bb x\u2082' \u2192 f x\u2081 x\u2082 \u2261 f x\u2081 x\u2082') h\u2082 refl)\n","old_contents":"------------------------------------------------------------------------------\n-- Propositional equality\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module is re-exported by (some) \"base\" modules.\n\nmodule Common.Relation.Binary.PropositionalEquality where\n\nopen import Common.Universe using ( D )\n\n------------------------------------------------------------------------------\n-- The propositional equality via pattern matching.\n\nmodule Inductive where\n -- We add 3 to the fixities of the standard library.\n infix 7 _\u2261_\n\n -- The identity type on the universe of discourse.\n data _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n -- Identity properties\n\n sym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\n sym refl = refl\n\n trans : \u2200 {x y z} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n trans refl h = h\n\n trans\u2082 : \u2200 {w x y z} \u2192 w \u2261 x \u2192 x \u2261 y \u2192 y \u2261 z \u2192 w \u2261 z\n trans\u2082 refl refl h = h\n\n subst : (P : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 P x \u2192 P y\n subst P refl Px = Px\n\n subst\u2082 : (P : D \u2192 D \u2192 Set) \u2192 \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192\n x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192\n P x\u2081 x\u2082 \u2192\n P y\u2081 y\u2082\n subst\u2082 P refl refl Pxs = Pxs\n\n subst\u2083 : (P : D \u2192 D \u2192 D \u2192 Set) \u2192 \u2200 {x\u2081 x\u2082 x\u2083 y\u2081 y\u2082 y\u2083} \u2192\n x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192 x\u2083 \u2261 y\u2083 \u2192\n P x\u2081 x\u2082 x\u2083 \u2192\n P y\u2081 y\u2082 y\u2083\n subst\u2083 P refl refl refl Pxs = Pxs\n\n subst\u2084 : (P : D \u2192 D \u2192 D \u2192 D \u2192 Set) \u2192 \u2200 {x\u2081 x\u2082 x\u2083 x\u2084 y\u2081 y\u2082 y\u2083 y\u2084} \u2192\n x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192 x\u2083 \u2261 y\u2083 \u2192 x\u2084 \u2261 y\u2084 \u2192\n P x\u2081 x\u2082 x\u2083 x\u2084 \u2192\n P y\u2081 y\u2082 y\u2083 y\u2084\n subst\u2084 P refl refl refl refl Pxs = Pxs\n\n cong : (f : D \u2192 D) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2261 f y\n cong f refl = refl\n\n cong\u2082 : (f : D \u2192 D \u2192 D) \u2192 \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192\n f x\u2081 x\u2082 \u2261 f y\u2081 y\u2082\n cong\u2082 f refl refl = refl\n\n------------------------------------------------------------------------------\n-- The propositional equality via its induction principle.\n\nmodule NonInductive where\n\n -- We add 3 to the fixities of the standard library.\n infix 7 _\u2261_\n\n -- The identity type on the universe of discourse.\n postulate\n _\u2261_ : D \u2192 D \u2192 Set\n refl : \u2200 {x} \u2192 x \u2261 x\n J : (P : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 P x \u2192 P y\n\n -- Identity properties\n\n sym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\n sym {x} h = J (\u03bb y' \u2192 y' \u2261 x) h refl\n\n trans : \u2200 {x y z} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n trans {x} h\u2081 h\u2082 = J (\u03bb y' \u2192 x \u2261 y') h\u2082 h\u2081\n\n trans\u2082 : \u2200 {w x y z} \u2192 w \u2261 x \u2192 x \u2261 y \u2192 y \u2261 z \u2192 w \u2261 z\n trans\u2082 h\u2081 h\u2082 h\u2083 = trans (trans h\u2081 h\u2082) h\u2083\n\n subst : (P : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 P x \u2192 P y\n subst = J\n\n subst\u2082 : (P : D \u2192 D \u2192 Set) \u2192 \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192\n x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192\n P x\u2081 x\u2082 \u2192\n P y\u2081 y\u2082\n subst\u2082 P {x\u2081} {x\u2082} {y\u2081} {y\u2082} x\u2081\u2261y\u2081 x\u2082\u2261y\u2082 Px\u2081x\u2082 =\n subst (\u03bb y\u2081' \u2192 P y\u2081' y\u2082)\n x\u2081\u2261y\u2081\n (subst (\u03bb y\u2082' \u2192 P x\u2081 y\u2082') x\u2082\u2261y\u2082 Px\u2081x\u2082)\n\n cong : (f : D \u2192 D) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2261 f y\n cong f {x} h = subst (\u03bb x' \u2192 f x \u2261 f x') h refl\n\n cong\u2082 : (f : D \u2192 D \u2192 D) \u2192 \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192\n f x\u2081 x\u2082 \u2261 f y\u2081 y\u2082\n cong\u2082 f {x\u2081} {x\u2082} {y\u2081} {y\u2082} h\u2081 h\u2082 =\n subst (\u03bb x\u2081' \u2192 f x\u2081 x\u2082 \u2261 f x\u2081' y\u2082)\n h\u2081\n (subst (\u03bb x\u2082' \u2192 f x\u2081 x\u2082 \u2261 f x\u2081 x\u2082') h\u2082 refl)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d48e16c2367a2ad929f94f7e393beb62c52409cb","subject":"layout","message":"layout\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits\/Search.agda","new_file":"lib\/Data\/Bits\/Search.agda","new_contents":"open import Data.Nat.NP hiding (_==_) renaming (_<=_ to _\u2115<=_)\nopen import Data.Bits\nopen import Data.Bool.Properties using (not-involutive)\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nopen import Algebra.FunctionProperties.NP\n\nmodule Data.Bits.Search where\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u2219_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u2219 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u2219 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u2219-comm : \u2200 {m} (x y : A m) \u2192 x \u2219 y \u2261 y \u2219 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u2219-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nmodule SimpleSearch {a} {A : Set a} (_\u2219_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u2219_ public\n\n module SearchUnit \u03b5 (\u03b5\u2219\u03b5 : \u03b5 \u2219 \u03b5 \u2261 \u03b5) where\n search-const\u03b5\u2261\u03b5 : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-const\u03b5\u2261\u03b5 zero = refl\n search-const\u03b5\u2261\u03b5 (suc n) rewrite search-const\u03b5\u2261\u03b5 n = \u03b5\u2219\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n module SearchInterchange (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u2219 f\u2081 x) \u2261 search f\u2080 \u2219 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u2219-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e where open \u2261-Reasoning\n\n search-0\u21941 : \u2200 {n} (f : Bits n \u2192 A) \u2192 search {n} (f \u2218 0\u21941) \u2261 search {n} f\n search-0\u21941 {zero} _ = refl\n search-0\u21941 {suc zero} _ = refl\n search-0\u21941 {suc (suc n)} _ = \u2219-interchange _ _ _ _\n\n module Bij (\u2219-comm : Commutative _\u2261_ _\u2219_)\n (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n open SearchInterchange \u2219-interchange using (search-0\u21941)\n open import Data.Bits.OperationSyntax hiding (_\u2219_)\n search-bij : \u2200 {n} f (g : Bits n \u2192 A) \u2192 search (g \u2218 eval f) \u2261 search g\n search-bij `id _ = refl\n search-bij `0\u21941 f = search-0\u21941 f\n search-bij (f `\u204f g) h\n rewrite search-bij f (h \u2218 eval g)\n | search-bij g h\n = refl\n search-bij {suc n} (`id `\u2237 f) g\n rewrite search-bij (f 0b) (g \u2218 0\u2237_)\n | search-bij (f 1b) (g \u2218 1\u2237_)\n = refl\n search-bij {suc n} (`not\u1d2e `\u2237 f) g\n rewrite search-bij (f 1b) (g \u2218 0\u2237_)\n | search-bij (f 0b) (g \u2218 1\u2237_)\n = \u2219-comm _ _\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nopen SimpleSearch\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192\n search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = search-\u2257 op _ _ (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n","old_contents":"open import Data.Nat.NP hiding (_==_) renaming (_<=_ to _\u2115<=_)\nopen import Data.Bits\nopen import Data.Bool.Properties using (not-involutive)\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nopen import Algebra.FunctionProperties.NP\n\nmodule Data.Bits.Search where\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u2219_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u2219 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u2219 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u2219-comm : \u2200 {m} (x y : A m) \u2192 x \u2219 y \u2261 y \u2219 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u2219-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nmodule SimpleSearch {a} {A : Set a} (_\u2219_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u2219_ public\n\n module SearchUnit \u03b5 (\u03b5\u2219\u03b5 : \u03b5 \u2219 \u03b5 \u2261 \u03b5) where\n search-const\u03b5\u2261\u03b5 : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-const\u03b5\u2261\u03b5 zero = refl\n search-const\u03b5\u2261\u03b5 (suc n) rewrite search-const\u03b5\u2261\u03b5 n = \u03b5\u2219\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n module SearchInterchange (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u2219 f\u2081 x) \u2261 search f\u2080 \u2219 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u2219-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e where open \u2261-Reasoning\n\n search-0\u21941 : \u2200 {n} (f : Bits n \u2192 A) \u2192 search {n} (f \u2218 0\u21941) \u2261 search {n} f\n search-0\u21941 {zero} _ = refl\n search-0\u21941 {suc zero} _ = refl\n search-0\u21941 {suc (suc n)} _ = \u2219-interchange _ _ _ _\n\n module Bij (\u2219-comm : Commutative _\u2261_ _\u2219_)\n (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n open SearchInterchange \u2219-interchange using (search-0\u21941)\n open import Data.Bits.OperationSyntax hiding (_\u2219_)\n search-bij : \u2200 {n} f (g : Bits n \u2192 A) \u2192 search (g \u2218 eval f) \u2261 search g\n search-bij `id _ = refl\n search-bij `0\u21941 f = search-0\u21941 f\n search-bij (f `\u204f g) h\n rewrite search-bij f (h \u2218 eval g)\n | search-bij g h\n = refl\n search-bij {suc n} (`id `\u2237 f) g\n rewrite search-bij (f 0b) (g \u2218 0\u2237_)\n | search-bij (f 1b) (g \u2218 1\u2237_)\n = refl\n search-bij {suc n} (`not\u1d2e `\u2237 f) g\n rewrite search-bij (f 1b) (g \u2218 0\u2237_)\n | search-bij (f 0b) (g \u2218 1\u2237_)\n = \u2219-comm _ _\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nopen SimpleSearch\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192\n search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = search-\u2257 op _ _ (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"b4c39231ae725a5b759926a08d76642ed2df680d","subject":"Base.Change.Products: Simplify code","message":"Base.Change.Products: Simplify code\n\nThese simplifications are inspired by Base.Change.Sums. But they're\nstill modest.\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Products.agda","new_file":"Base\/Change\/Products.agda","new_contents":"module Base.Change.Products where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\nopen import Base.Change.Algebra\n\nopen import Data.Product\n\n-- Also try defining sectioned change structures on the positives halves of\n-- groups? Or on arbitrary subsets?\n\n-- Restriction: we pair sets on the same level (because right now everything\n-- else would risk getting in the way).\nmodule ProductChanges \u2113 (A B : Set \u2113) {{CA : ChangeAlgebra \u2113 A}} {{CB : ChangeAlgebra \u2113 B}} where\n open \u2261-Reasoning\n\n -- The simplest possible definition of changes for products.\n\n PChange : A \u00d7 B \u2192 Set \u2113\n PChange (a , b) = \u0394 a \u00d7 \u0394 b\n\n -- An interesting alternative definition allows omitting the nil change of a\n -- component when that nil change can be computed from the type. For instance, the nil change for integers is always the same.\n\n -- However, the nil change for function isn't always the same (unless we\n -- defunctionalize them first), so nil changes for functions can't be omitted.\n\n _\u2295_ : (v : A \u00d7 B) \u2192 PChange v \u2192 A \u00d7 B\n _\u2295_ (a , b) (da , db) = a \u229e da , b \u229e db\n _\u229d_ : A \u00d7 B \u2192 (v : A \u00d7 B) \u2192 PChange v\n _\u229d_ (aNew , bNew) (a , b) = aNew \u229f a , bNew \u229f b\n\n p-nil : (v : A \u00d7 B) \u2192 PChange v\n p-nil (a , b) = (nil a , nil b)\n\n p-update-diff : (u v : A \u00d7 B) \u2192 v \u2295 (u \u229d v) \u2261 u\n p-update-diff (ua , ub) (va , vb) =\n let u = (ua , ub)\n v = (va , vb)\n in\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8\u27e9\n (va \u229e (ua \u229f va) , vb \u229e (ub \u229f vb))\n --v \u2295 ((ua \u229f va , ub \u229f vb))\n \u2261\u27e8 cong\u2082 _,_ (update-diff ua va) (update-diff ub vb)\u27e9\n (ua , ub)\n \u2261\u27e8\u27e9\n u\n \u220e\n\n p-update-nil : (v : A \u00d7 B) \u2192 v \u2295 (p-nil v) \u2261 v\n p-update-nil (a , b) =\n let v = (a , b)\n in\n begin\n v \u2295 (p-nil v)\n \u2261\u27e8\u27e9\n (a \u229e nil a , b \u229e nil b)\n \u2261\u27e8 cong\u2082 _,_ (update-nil a) (update-nil b)\u27e9\n (a , b)\n \u2261\u27e8\u27e9\n v\n \u220e\n\n changeAlgebra : ChangeAlgebra \u2113 (A \u00d7 B)\n changeAlgebra = record\n { Change = PChange\n ; update = _\u2295_\n ; diff = _\u229d_\n ; nil = p-nil\n ; isChangeAlgebra = record\n { update-diff = p-update-diff\n ; update-nil = p-update-nil\n }\n }\n\n proj\u2081\u2032 : (v : A \u00d7 B) \u2192 \u0394 v \u2192 \u0394 (proj\u2081 v)\n proj\u2081\u2032 (a , b) (da , db) = da\n\n proj\u2081\u2032Derivative : Derivative proj\u2081 proj\u2081\u2032\n -- Implementation note: we do not need to pattern match on v and dv because\n -- they are records, hence Agda knows that pattern matching on records cannot\n -- fail. Technically, the required feature is the eta-rule on records.\n proj\u2081\u2032Derivative v dv = refl\n\n -- An extended explanation.\n proj\u2081\u2032Derivative\u2081 : Derivative proj\u2081 proj\u2081\u2032\n proj\u2081\u2032Derivative\u2081 (a , b) (da , db) =\n let v = (a , b)\n dv = (da , db)\n in\n begin\n proj\u2081 v \u229e proj\u2081\u2032 v dv\n \u2261\u27e8\u27e9\n a \u229e da\n \u2261\u27e8\u27e9\n proj\u2081 (v \u229e dv)\n \u220e\n\n -- Same for the second extractor.\n proj\u2082\u2032 : (v : A \u00d7 B) \u2192 \u0394 v \u2192 \u0394 (proj\u2082 v)\n proj\u2082\u2032 (a , b) (da , db) = db\n\n proj\u2082\u2032Derivative : Derivative proj\u2082 proj\u2082\u2032\n proj\u2082\u2032Derivative v dv = refl\n\n B\u2192A\u00d7B = FunctionChanges.changeAlgebra B (A \u00d7 B)\n A\u2192B\u2192A\u00d7B = FunctionChanges.changeAlgebra A (B \u2192 A \u00d7 B) {{CA}} {{B\u2192A\u00d7B}}\n\n -- Morally, the following is a change:\n -- What one could wrongly expect to be the derivative of the constructor:\n _,_\u2032-realizer : (a : A) \u2192 (da : \u0394 a) \u2192 (b : B) \u2192 (db : \u0394 b) \u2192 \u0394 (a , b)\n _,_\u2032-realizer a da b db = da , db\n\n -- That has the correct behavior, in a sense, and it would be in the\n -- subset-based formalization in the paper.\n --\n -- But the above is not even a change, because it does not contain a proof of\n -- its own validity, and because after application it does not contain a\n -- proof.\n --\n -- However, the above is (morally) a \"realizer\" of the actual change, since it\n -- only represents its computational behavior, not its proof manipulation.\n\n -- Hence, we need to do some additional work.\n\n _,_\u2032-realizer-correct _,_\u2032-realizer-correct-detailed :\n (a : A) \u2192 (da : \u0394 a) \u2192 (b : B) \u2192 (db : \u0394 b) \u2192\n (a , b \u229e db) \u229e (_,_\u2032-realizer a da (b \u229e db) (nil (b \u229e db))) \u2261 (a , b) \u229e (_,_\u2032-realizer a da b db)\n _,_\u2032-realizer-correct a da b db rewrite update-nil (b \u229e db) = refl\n\n _,_\u2032-realizer-correct-detailed a da b db =\n begin\n (a , b \u229e db) \u229e (_,_\u2032-realizer a da) (b \u229e db) (nil (b \u229e db))\n \u2261\u27e8\u27e9\n a \u229e da , b \u229e db \u229e (nil (b \u229e db))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 a \u229e da , \u25a1) (update-nil (b \u229e db)) \u27e9\n a \u229e da , b \u229e db\n \u2261\u27e8\u27e9\n (a , b) \u229e (_,_\u2032-realizer a da) b db\n \u220e\n\n _,_\u2032 : (a : A) \u2192 (da : \u0394 a) \u2192 \u0394 (_,_ a)\n _,_\u2032 a da = record { apply = _,_\u2032-realizer a da ; correct = \u03bb b db \u2192 _,_\u2032-realizer-correct a da b db }\n\n _,_\u2032-Derivative : Derivative _,_ _,_\u2032\n _,_\u2032-Derivative a da = ext (\u03bb b \u2192 cong (_,_ (a \u229e da)) (update-nil b))\n where\n open import Postulate.Extensionality\n\n -- Define specialized variant of uncurry, and derive it.\n uncurry\u2080 : \u2200 {C : Set \u2113} \u2192 (A \u2192 B \u2192 C) \u2192 A \u00d7 B \u2192 C\n uncurry\u2080 f (a , b) = f a b\n\n module _ {C : Set \u2113} {{CC : ChangeAlgebra \u2113 C}} where\n B\u2192C : ChangeAlgebra \u2113 (B \u2192 C)\n B\u2192C = FunctionChanges.changeAlgebra B C\n A\u2192B\u2192C : ChangeAlgebra \u2113 (A \u2192 B \u2192 C)\n A\u2192B\u2192C = FunctionChanges.changeAlgebra A (B \u2192 C)\n A\u00d7B\u2192C : ChangeAlgebra \u2113 (A \u00d7 B \u2192 C)\n A\u00d7B\u2192C = FunctionChanges.changeAlgebra (A \u00d7 B) C\n open FunctionChanges using (apply; correct)\n\n uncurry\u2080\u2032-realizer : (f : A \u2192 B \u2192 C) \u2192 \u0394 f \u2192 (p : A \u00d7 B) \u2192 \u0394 p \u2192 \u0394 (uncurry\u2080 f p)\n uncurry\u2080\u2032-realizer f df (a , b) (da , db) = apply (apply df a da) b db\n\n uncurry\u2080\u2032-realizer-correct uncurry\u2080\u2032-realizer-correct-detailed :\n \u2200 (f : A \u2192 B \u2192 C) (df : \u0394 f) (p : A \u00d7 B) (dp : \u0394 p) \u2192\n uncurry\u2080 f (p \u2295 dp) \u229e uncurry\u2080\u2032-realizer f df (p \u2295 dp) (nil (p \u229e dp)) \u2261 uncurry\u2080 f p \u229e uncurry\u2080\u2032-realizer f df p dp\n\n -- Hard to read\n uncurry\u2080\u2032-realizer-correct f df (a , b) (da , db)\n rewrite sym (correct (apply df (a \u229e da) (nil (a \u229e da))) (b \u229e db) (nil (b \u229e db)))\n | update-nil (b \u229e db)\n | {- cong (\u03bb \u25a1 \u2192 \u25a1 (b \u229e db)) -} (sym (correct df (a \u229e da) (nil (a \u229e da))))\n | update-nil (a \u229e da)\n | cong (\u03bb \u25a1 \u2192 \u25a1 (b \u229e db)) (correct df a da)\n | correct (apply df a da) b db\n = refl\n\n -- Verbose, but it shows all the intermediate steps.\n uncurry\u2080\u2032-realizer-correct-detailed f df (a , b) (da , db) =\n begin\n uncurry\u2080 f (a \u229e da , b \u229e db) \u229e uncurry\u2080\u2032-realizer f df (a \u229e da , b \u229e db) (nil (a \u229e da , b \u229e db))\n \u2261\u27e8\u27e9\n f (a \u229e da) (b \u229e db) \u229e apply (apply df (a \u229e da) (nil (a \u229e da))) (b \u229e db) (nil (b \u229e db))\n \u2261\u27e8 sym (correct (apply df (a \u229e da) (nil (a \u229e da))) (b \u229e db) (nil (b \u229e db))) \u27e9\n (f (a \u229e da) \u229e apply df (a \u229e da) (nil (a \u229e da))) ((b \u229e db) \u229e (nil (b \u229e db)))\n \u2261\u27e8 cong-lem\u2080 \u27e9\n (f (a \u229e da) \u229e apply df (a \u229e da) (nil (a \u229e da))) (b \u229e db)\n \u2261\u27e8 sym cong-lem\u2082 \u27e9\n ((f \u229e df) ((a \u229e da) \u229e (nil (a \u229e da)))) (b \u229e db)\n \u2261\u27e8 cong-lem\u2081 \u27e9\n (f \u229e df) (a \u229e da) (b \u229e db)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (b \u229e db)) (correct df a da) \u27e9\n (f a \u229e apply df a da) (b \u229e db)\n \u2261\u27e8 correct (apply df a da) b db \u27e9\n f a b \u229e apply (apply df a da) b db\n \u2261\u27e8\u27e9\n uncurry\u2080 f (a , b) \u229e uncurry\u2080\u2032-realizer f df (a , b) (da , db)\n \u220e\n where\n cong-lem\u2080 :\n (f (a \u229e da) \u229e apply df (a \u229e da) (nil (a \u229e da))) ((b \u229e db) \u229e (nil (b \u229e db)))\n \u2261\n (f (a \u229e da) \u229e apply df (a \u229e da) (nil (a \u229e da))) (b \u229e db)\n cong-lem\u2080 rewrite update-nil (b \u229e db) = refl\n\n cong-lem\u2081 :\n ((f \u229e df) ((a \u229e da) \u229e (nil (a \u229e da)))) (b \u229e db)\n \u2261\n (f \u229e df) (a \u229e da) (b \u229e db)\n cong-lem\u2081 rewrite update-nil (a \u229e da) = refl\n\n cong-lem\u2082 :\n ((f \u229e df) ((a \u229e da) \u229e (nil (a \u229e da)))) (b \u229e db)\n \u2261\n (f (a \u229e da) \u229e apply df (a \u229e da) (nil (a \u229e da))) (b \u229e db)\n cong-lem\u2082 = cong (\u03bb \u25a1 \u2192 \u25a1 (b \u229e db)) (correct df (a \u229e da) (nil (a \u229e da)))\n\n uncurry\u2080\u2032 : (f : A \u2192 B \u2192 C) \u2192 \u0394 f \u2192 \u0394 (uncurry f)\n uncurry\u2080\u2032 f df = record\n { apply = uncurry\u2080\u2032-realizer f df\n ; correct = uncurry\u2080\u2032-realizer-correct f df }\n\n -- Now proving that uncurry\u2080\u2032 is a derivative is trivial!\n uncurry\u2080\u2032Derivative\u2080 : Derivative {{CB = A\u00d7B\u2192C}} uncurry\u2080 uncurry\u2080\u2032\n uncurry\u2080\u2032Derivative\u2080 f df = refl\n\n -- If you wonder what's going on, here's the step-by-step proof, going purely by definitional equality.\n uncurry\u2080\u2032Derivative : Derivative {{CB = A\u00d7B\u2192C}} uncurry\u2080 uncurry\u2080\u2032\n uncurry\u2080\u2032Derivative f df =\n begin\n uncurry\u2080 f \u229e uncurry\u2080\u2032 f df\n \u2261\u27e8\u27e9\n (\u03bb {(a , b) \u2192 uncurry\u2080 f (a , b) \u229e apply (uncurry\u2080\u2032 f df) (a , b) (nil (a , b))})\n \u2261\u27e8\u27e9\n (\u03bb {(a , b) \u2192 f a b \u229e apply (apply df a (nil a)) b (nil b)})\n \u2261\u27e8\u27e9\n (\u03bb {(a , b) \u2192 (f a \u229e apply df a (nil a)) b})\n \u2261\u27e8\u27e9\n (\u03bb {(a , b) \u2192 (f \u229e df) a b})\n \u2261\u27e8\u27e9\n (\u03bb {(a , b) \u2192 uncurry\u2080 (f \u229e df) (a , b)})\n \u2261\u27e8\u27e9\n uncurry\u2080 (f \u229e df)\n \u220e\n","old_contents":"module Base.Change.Products where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\nopen import Base.Change.Algebra\n\nopen import Data.Product\n\n-- Also try defining sectioned change structures on the positives halves of\n-- groups? Or on arbitrary subsets?\n\n-- Restriction: we pair sets on the same level (because right now everything\n-- else would risk getting in the way).\nmodule ProductChanges \u2113 (A B : Set \u2113) {{CA : ChangeAlgebra \u2113 A}} {{CB : ChangeAlgebra \u2113 B}} where\n open \u2261-Reasoning\n\n -- The simplest possible definition of changes for products.\n\n PChange : A \u00d7 B \u2192 Set \u2113\n PChange (a , b) = \u0394 a \u00d7 \u0394 b\n\n -- An interesting alternative definition allows omitting the nil change of a\n -- component when that nil change can be computed from the type. For instance, the nil change for integers is always the same.\n\n -- However, the nil change for function isn't always the same (unless we\n -- defunctionalize them first), so nil changes for functions can't be omitted.\n\n _\u2295_ : (v : A \u00d7 B) \u2192 PChange v \u2192 A \u00d7 B\n _\u2295_ (a , b) (da , db) = a \u229e da , b \u229e db\n _\u229d_ : A \u00d7 B \u2192 (v : A \u00d7 B) \u2192 PChange v\n _\u229d_ (aNew , bNew) (a , b) = aNew \u229f a , bNew \u229f b\n\n p-nil : (v : A \u00d7 B) \u2192 PChange v\n p-nil (a , b) = (nil a , nil b)\n\n p-update-diff : (u v : A \u00d7 B) \u2192 v \u2295 (u \u229d v) \u2261 u\n p-update-diff (ua , ub) (va , vb) =\n let u = (ua , ub)\n v = (va , vb)\n in\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8\u27e9\n (va \u229e (ua \u229f va) , vb \u229e (ub \u229f vb))\n --v \u2295 ((ua \u229f va , ub \u229f vb))\n \u2261\u27e8 cong\u2082 _,_ (update-diff ua va) (update-diff ub vb)\u27e9\n (ua , ub)\n \u2261\u27e8\u27e9\n u\n \u220e\n\n p-update-nil : (v : A \u00d7 B) \u2192 v \u2295 (p-nil v) \u2261 v\n p-update-nil (a , b) =\n let v = (a , b)\n in\n begin\n v \u2295 (p-nil v)\n \u2261\u27e8\u27e9\n (a \u229e nil a , b \u229e nil b)\n \u2261\u27e8 cong\u2082 _,_ (update-nil a) (update-nil b)\u27e9\n (a , b)\n \u2261\u27e8\u27e9\n v\n \u220e\n\n changeAlgebra : ChangeAlgebra \u2113 (A \u00d7 B)\n changeAlgebra = record\n { Change = PChange\n ; update = _\u2295_\n ; diff = _\u229d_\n ; nil = p-nil\n ; isChangeAlgebra = record\n { update-diff = p-update-diff\n ; update-nil = p-update-nil\n }\n }\n\n proj\u2081\u2032 : (v : A \u00d7 B) \u2192 \u0394 v \u2192 \u0394 (proj\u2081 v)\n proj\u2081\u2032 (a , b) (da , db) = da\n\n proj\u2081\u2032Derivative : Derivative proj\u2081 proj\u2081\u2032\n -- Implementation note: we do not need to pattern match on v and dv because\n -- they are records, hence Agda knows that pattern matching on records cannot\n -- fail. Technically, the required feature is the eta-rule on records.\n proj\u2081\u2032Derivative v dv = refl\n\n -- An extended explanation.\n proj\u2081\u2032Derivative\u2081 : Derivative proj\u2081 proj\u2081\u2032\n proj\u2081\u2032Derivative\u2081 (a , b) (da , db) =\n let v = (a , b)\n dv = (da , db)\n in\n begin\n proj\u2081 v \u229e proj\u2081\u2032 v dv\n \u2261\u27e8\u27e9\n a \u229e da\n \u2261\u27e8\u27e9\n proj\u2081 (v \u229e dv)\n \u220e\n\n -- Same for the second extractor.\n proj\u2082\u2032 : (v : A \u00d7 B) \u2192 \u0394 v \u2192 \u0394 (proj\u2082 v)\n proj\u2082\u2032 (a , b) (da , db) = db\n\n proj\u2082\u2032Derivative : Derivative proj\u2082 proj\u2082\u2032\n proj\u2082\u2032Derivative v dv = refl\n\n B\u2192A\u00d7B = FunctionChanges.changeAlgebra {c = \u2113} {d = \u2113} B (A \u00d7 B)\n A\u2192B\u2192A\u00d7B = FunctionChanges.changeAlgebra {c = \u2113} {d = \u2113} A (B \u2192 A \u00d7 B) {{CA}} {{B\u2192A\u00d7B}}\n module \u0394BA\u00d7B = FunctionChanges B (A \u00d7 B) {{CB}} {{changeAlgebra}}\n module \u0394A\u2192B\u2192A\u00d7B = FunctionChanges A (B \u2192 A \u00d7 B) {{CA}} {{B\u2192A\u00d7B}}\n\n -- Morally, the following is a change:\n -- What one could wrongly expect to be the derivative of the constructor:\n _,_\u2032-realizer : (a : A) \u2192 (da : \u0394 a) \u2192 (b : B) \u2192 (db : \u0394 b) \u2192 \u0394 (a , b)\n _,_\u2032-realizer a da b db = da , db\n\n -- That has the correct behavior, in a sense, and it would be in the\n -- subset-based formalization in the paper.\n --\n -- But the above is not even a change, because it does not contain a proof of\n -- its own validity, and because after application it does not contain a\n -- proof.\n --\n -- However, the above is (morally) a \"realizer\" of the actual change, since it\n -- only represents its computational behavior, not its proof manipulation.\n\n -- Hence, we need to do some additional work.\n\n _,_\u2032-realizer-correct _,_\u2032-realizer-correct-detailed :\n (a : A) \u2192 (da : \u0394 a) \u2192 (b : B) \u2192 (db : \u0394 b) \u2192\n (a , b \u229e db) \u229e (_,_\u2032-realizer a da (b \u229e db) (nil (b \u229e db))) \u2261 (a , b) \u229e (_,_\u2032-realizer a da b db)\n _,_\u2032-realizer-correct a da b db rewrite update-nil (b \u229e db) = refl\n\n _,_\u2032-realizer-correct-detailed a da b db =\n begin\n (a , b \u229e db) \u229e (_,_\u2032-realizer a da) (b \u229e db) (nil (b \u229e db))\n \u2261\u27e8\u27e9\n a \u229e da , b \u229e db \u229e (nil (b \u229e db))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 a \u229e da , \u25a1) (update-nil (b \u229e db)) \u27e9\n a \u229e da , b \u229e db\n \u2261\u27e8\u27e9\n (a , b) \u229e (_,_\u2032-realizer a da) b db\n \u220e\n\n _,_\u2032 : (a : A) \u2192 (da : \u0394 a) \u2192 \u0394 (_,_ a)\n _,_\u2032 a da = record { apply = _,_\u2032-realizer a da ; correct = \u03bb b db \u2192 _,_\u2032-realizer-correct a da b db }\n\n _,_\u2032-Derivative : Derivative _,_ _,_\u2032\n _,_\u2032-Derivative a da = ext (\u03bb b \u2192 cong (_,_ (a \u229e da)) (update-nil b))\n where\n open import Postulate.Extensionality\n\n -- Define specialized variant of uncurry, and derive it.\n uncurry\u2080 : \u2200 {C : Set \u2113} \u2192 (A \u2192 B \u2192 C) \u2192 A \u00d7 B \u2192 C\n uncurry\u2080 f (a , b) = f a b\n\n module _ {C : Set \u2113} {{CC : ChangeAlgebra \u2113 C}} where\n B\u2192C : ChangeAlgebra \u2113 (B \u2192 C)\n B\u2192C = FunctionChanges.changeAlgebra B C\n A\u2192B\u2192C : ChangeAlgebra \u2113 (A \u2192 B \u2192 C)\n A\u2192B\u2192C = FunctionChanges.changeAlgebra A (B \u2192 C)\n A\u00d7B\u2192C : ChangeAlgebra \u2113 (A \u00d7 B \u2192 C)\n A\u00d7B\u2192C = FunctionChanges.changeAlgebra (A \u00d7 B) C\n module \u0394B\u2192C = FunctionChanges B C {{CB}} {{CC}}\n module \u0394A\u2192B\u2192C = FunctionChanges A (B \u2192 C) {{CA}} {{B\u2192C}}\n module \u0394A\u00d7B\u2192C = FunctionChanges (A \u00d7 B) C {{changeAlgebra}} {{CC}}\n\n uncurry\u2080\u2032-realizer : (f : A \u2192 B \u2192 C) \u2192 \u0394 f \u2192 (p : A \u00d7 B) \u2192 \u0394 p \u2192 \u0394 (uncurry\u2080 f p)\n uncurry\u2080\u2032-realizer f df (a , b) (da , db) = \u0394B\u2192C.apply (\u0394A\u2192B\u2192C.apply df a da) b db\n\n uncurry\u2080\u2032-realizer-correct uncurry\u2080\u2032-realizer-correct-detailed :\n \u2200 (f : A \u2192 B \u2192 C) (df : \u0394 f) (p : A \u00d7 B) (dp : \u0394 p) \u2192\n uncurry\u2080 f (p \u2295 dp) \u229e uncurry\u2080\u2032-realizer f df (p \u2295 dp) (nil (p \u229e dp)) \u2261 uncurry\u2080 f p \u229e uncurry\u2080\u2032-realizer f df p dp\n\n -- Hard to read\n uncurry\u2080\u2032-realizer-correct f df (a , b) (da , db)\n rewrite sym (\u0394B\u2192C.incrementalization (f (a \u229e da)) (\u0394A\u2192B\u2192C.apply df (a \u229e da) (nil (a \u229e da))) (b \u229e db) (nil (b \u229e db)))\n | update-nil (b \u229e db)\n | {- cong (\u03bb \u25a1 \u2192 \u25a1 (b \u229e db)) -} (sym (\u0394A\u2192B\u2192C.incrementalization f df (a \u229e da) (nil (a \u229e da))))\n | update-nil (a \u229e da)\n | cong (\u03bb \u25a1 \u2192 \u25a1 (b \u229e db)) (\u0394A\u2192B\u2192C.incrementalization f df a da)\n | \u0394B\u2192C.incrementalization (f a) (\u0394A\u2192B\u2192C.apply df a da) b db\n = refl\n\n -- Verbose, but it shows all the intermediate steps.\n uncurry\u2080\u2032-realizer-correct-detailed f df (a , b) (da , db) =\n begin\n uncurry\u2080 f (a \u229e da , b \u229e db) \u229e uncurry\u2080\u2032-realizer f df (a \u229e da , b \u229e db) (nil (a \u229e da , b \u229e db))\n \u2261\u27e8\u27e9\n f (a \u229e da) (b \u229e db) \u229e \u0394B\u2192C.apply (\u0394A\u2192B\u2192C.apply df (a \u229e da) (nil (a \u229e da))) (b \u229e db) (nil (b \u229e db))\n \u2261\u27e8 sym (\u0394B\u2192C.incrementalization (f (a \u229e da)) (\u0394A\u2192B\u2192C.apply df (a \u229e da) (nil (a \u229e da))) (b \u229e db) (nil (b \u229e db))) \u27e9\n (f (a \u229e da) \u229e \u0394A\u2192B\u2192C.apply df (a \u229e da) (nil (a \u229e da))) ((b \u229e db) \u229e (nil (b \u229e db)))\n \u2261\u27e8 cong-lem\u2080 \u27e9\n (f (a \u229e da) \u229e \u0394A\u2192B\u2192C.apply df (a \u229e da) (nil (a \u229e da))) (b \u229e db)\n \u2261\u27e8 sym cong-lem\u2082 \u27e9\n ((f \u229e df) ((a \u229e da) \u229e (nil (a \u229e da)))) (b \u229e db)\n \u2261\u27e8 cong-lem\u2081 \u27e9\n (f \u229e df) (a \u229e da) (b \u229e db)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (b \u229e db)) (\u0394A\u2192B\u2192C.incrementalization f df a da) \u27e9\n (f a \u229e \u0394A\u2192B\u2192C.apply df a da) (b \u229e db)\n \u2261\u27e8 \u0394B\u2192C.incrementalization (f a) (\u0394A\u2192B\u2192C.apply df a da) b db \u27e9\n f a b \u229e \u0394B\u2192C.apply (\u0394A\u2192B\u2192C.apply df a da) b db\n \u2261\u27e8\u27e9\n uncurry\u2080 f (a , b) \u229e uncurry\u2080\u2032-realizer f df (a , b) (da , db)\n \u220e\n where\n cong-lem\u2080 :\n (f (a \u229e da) \u229e \u0394A\u2192B\u2192C.apply df (a \u229e da) (nil (a \u229e da))) ((b \u229e db) \u229e (nil (b \u229e db)))\n \u2261\n (f (a \u229e da) \u229e \u0394A\u2192B\u2192C.apply df (a \u229e da) (nil (a \u229e da))) (b \u229e db)\n cong-lem\u2080 rewrite update-nil (b \u229e db) = refl\n\n cong-lem\u2081 :\n ((f \u229e df) ((a \u229e da) \u229e (nil (a \u229e da)))) (b \u229e db)\n \u2261\n (f \u229e df) (a \u229e da) (b \u229e db)\n cong-lem\u2081 rewrite update-nil (a \u229e da) = refl\n\n cong-lem\u2082 :\n ((f \u229e df) ((a \u229e da) \u229e (nil (a \u229e da)))) (b \u229e db)\n \u2261\n (f (a \u229e da) \u229e \u0394A\u2192B\u2192C.apply df (a \u229e da) (nil (a \u229e da))) (b \u229e db)\n cong-lem\u2082 = cong (\u03bb \u25a1 \u2192 \u25a1 (b \u229e db)) (\u0394A\u2192B\u2192C.incrementalization f df (a \u229e da) (nil (a \u229e da)))\n\n uncurry\u2080\u2032 : (f : A \u2192 B \u2192 C) \u2192 \u0394 f \u2192 \u0394 (uncurry f)\n uncurry\u2080\u2032 f df = record\n { apply = uncurry\u2080\u2032-realizer f df\n ; correct = uncurry\u2080\u2032-realizer-correct f df }\n\n -- Now proving that uncurry\u2080\u2032 is a derivative is trivial!\n uncurry\u2080\u2032Derivative\u2080 : Derivative {{CB = A\u00d7B\u2192C}} uncurry\u2080 uncurry\u2080\u2032\n uncurry\u2080\u2032Derivative\u2080 f df = refl\n\n -- If you wonder what's going on, here's the step-by-step proof, going purely by definitional equality.\n uncurry\u2080\u2032Derivative : Derivative {{CB = A\u00d7B\u2192C}} uncurry\u2080 uncurry\u2080\u2032\n uncurry\u2080\u2032Derivative f df =\n begin\n uncurry\u2080 f \u229e uncurry\u2080\u2032 f df\n \u2261\u27e8\u27e9\n (\u03bb {(a , b) \u2192 uncurry\u2080 f (a , b) \u229e \u0394A\u00d7B\u2192C.apply (uncurry\u2080\u2032 f df) (a , b) (nil (a , b))})\n \u2261\u27e8\u27e9\n (\u03bb {(a , b) \u2192 f a b \u229e \u0394B\u2192C.apply (\u0394A\u2192B\u2192C.apply df a (nil a)) b (nil b)})\n \u2261\u27e8\u27e9\n (\u03bb {(a , b) \u2192 (f a \u229e \u0394A\u2192B\u2192C.apply df a (nil a)) b})\n \u2261\u27e8\u27e9\n (\u03bb {(a , b) \u2192 (f \u229e df) a b})\n \u2261\u27e8\u27e9\n (\u03bb {(a , b) \u2192 uncurry\u2080 (f \u229e df) (a , b)})\n \u2261\u27e8\u27e9\n uncurry\u2080 (f \u229e df)\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9808544b980c3cc5c39b301c6bee2f86b426bae6","subject":"Compile fix","message":"Compile fix\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Algebra.agda","new_file":"Base\/Change\/Algebra.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Change Structures.\n--\n-- This module defines the notion of change structures,\n-- as well as change structures for groups, functions and lists.\n--\n-- This module corresponds to Section 2 of the PLDI paper.\n------------------------------------------------------------------------\n\nmodule Base.Change.Algebra where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Change Algebras\n-- ===============\n--\n-- In the paper, change algebras are called \"change structures\"\n-- and they are described in Section 2.1. We follow the design of\n-- the Agda standard library and define change algebras as two\n-- records, so Definition 2.1 from the PLDI paper maps to the\n-- records IsChangeAlgebra and ChangeAlgebra.\n--\n-- A value of type (IsChangeAlgebra Change update diff) proves that\n-- Change, update and diff together form a change algebra.\n--\n-- A value of type (ChangeAlgebra Carrier) contains the necessary\n-- ingredients to create a change algebra for a carrier set.\n--\n-- In the paper, Carrier is called V (Def. 2.1a),\n-- Change is called \u0394 (Def. 2.1b),\n-- update is written as infix \u2295 (Def. 2.1c),\n-- diff is written as infix \u229d (Def. 2.1d),\n-- and update-diff is specified in Def. 2.1e.\n\nrecord IsChangeAlgebra\n {c}\n {Carrier : Set c}\n (Change : Carrier \u2192 Set c)\n (update : (v : Carrier) (dv : Change v) \u2192 Carrier)\n (diff : (u v : Carrier) \u2192 Change v)\n (nil : (u : Carrier) \u2192 Change u) : Set (c) where\n field\n update-diff : \u2200 u v \u2192 update v (diff u v) \u2261 u\n -- This corresponds to Lemma 2.3 from the paper.\n update-nil : \u2200 v \u2192 update v (nil v) \u2261 v\n\n -- abbreviations\n before : \u2200 {v} \u2192 Change v \u2192 Carrier\n before {v} _ = v\n\n after : \u2200 {v} \u2192 Change v \u2192 Carrier\n after {v} dv = update v dv\n\nrecord ChangeAlgebra {c}\n (Carrier : Set c) : Set (suc c) where\n field\n Change : Carrier \u2192 Set c\n update : (v : Carrier) (dv : Change v) \u2192 Carrier\n diff : (u v : Carrier) \u2192 Change v\n\n -- This generalizes Def. 2.2. from the paper.\n nil : (u : Carrier) \u2192 Change u\n\n isChangeAlgebra : IsChangeAlgebra Change update diff nil\n\n\n infixl 6 update diff\n\n open IsChangeAlgebra isChangeAlgebra public\n\n-- The following open ... public statement lets us use infix \u229e\n-- and \u229f for update and diff. In the paper, we use infix \u2295 and\n-- \u229d.\n\nopen ChangeAlgebra {{...}} public\n using\n ( update-diff\n ; update-nil\n ; nil\n ; before\n ; after\n )\n renaming\n ( Change to \u0394\n ; update to _\u229e_\n ; diff to _\u229f_\n )\n\n-- Families of Change Algebras\n-- ===========================\n--\n-- This is some Agda machinery to allow subscripting change\n-- algebra operations to avoid ambiguity. In the paper,\n-- we simply write (in the paragraph after Def. 2.1):\n--\n-- We overload operators \u2206, \u229d and \u2295 to refer to the\n-- corresponding operations of different change structures; we\n-- will subscript these symbols when needed to prevent\n-- ambiguity.\n--\n-- The following definitions implement this idea formally.\n\nrecord ChangeAlgebraFamily {a p} {A : Set a} (P : A \u2192 Set p): Set (suc p \u2294 a) where\n constructor\n family\n field\n change-algebra : \u2200 x \u2192 ChangeAlgebra (P x)\n\n module _ x where\n open ChangeAlgebra (change-algebra x) public\n\nmodule Family = ChangeAlgebraFamily {{...}}\n\nopen Family public\n using\n (\n )\n renaming\n ( Change to \u0394\u208d_\u208e\n ; nil to nil\u208d_\u208e\n ; update-diff to update-diff\u208d_\u208e\n ; update-nil to update-nil\u208d_\u208e\n ; change-algebra to change-algebra\u208d_\u208e\n ; before to before\u208d_\u208e\n ; after to after\u208d_\u208e\n )\n\n-- XXX not clear this is ever used\ninstance\n change-algebra-family-inst = change-algebra\u208d_\u208e\n\ninfixl 6 update\u2032 diff\u2032\n\nupdate\u2032 = Family.update\nsyntax update\u2032 x v dv = v \u229e\u208d x \u208e dv\n\ndiff\u2032 = Family.diff\nsyntax diff\u2032 x u v = u \u229f\u208d x \u208e v\n\n-- Derivatives\n-- ===========\n--\n-- This corresponds to Def. 2.4 in the paper.\n\nRawChange : \u2200 {a b} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra A}} \u2192\n {{CB : ChangeAlgebra B}} \u2192\n (f : A \u2192 B) \u2192\n Set (a \u2294 b)\nRawChange f = \u2200 a (da : \u0394 a) \u2192 \u0394 (f a)\n\nIsDerivative : \u2200 {a b} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra A}} \u2192\n {{CB : ChangeAlgebra B}} \u2192\n (f : A \u2192 B) \u2192\n (df : RawChange f) \u2192\n Set (a \u2294 b)\nIsDerivative f df = \u2200 a da \u2192 f a \u229e df a da \u2261 f (a \u229e da)\n\n-- This is a variant of IsDerivative for change algebra families.\nRawChange\u208d_,_\u208e : \u2200 {a b p q} {A : Set a} {B : Set b} {P : A \u2192 Set p} {Q : B \u2192 Set q} \u2192\n {{CP : ChangeAlgebraFamily P}} \u2192\n {{CQ : ChangeAlgebraFamily Q}} \u2192\n (x : A) \u2192\n (y : B) \u2192\n (f : P x \u2192 Q y) \u2192 Set (p \u2294 q)\nRawChange\u208d x , y \u208e f = \u2200 px (dpx : \u0394\u208d x \u208e px) \u2192 \u0394\u208d y \u208e (f px)\n\nIsDerivative\u208d_,_\u208e : \u2200 {a b p q} {A : Set a} {B : Set b} {P : A \u2192 Set p} {Q : B \u2192 Set q} \u2192\n {{CP : ChangeAlgebraFamily P}} \u2192\n {{CQ : ChangeAlgebraFamily Q}} \u2192\n (x : A) \u2192\n (y : B) \u2192\n (f : P x \u2192 Q y) \u2192\n (df : RawChange\u208d_,_\u208e x y f) \u2192\n Set (p \u2294 q)\nIsDerivative\u208d_,_\u208e {P = P} {{CP}} {{CQ}} x y f df = IsDerivative {{change-algebra\u208d _ \u208e}} {{change-algebra\u208d _ \u208e}} f df where\n CPx = change-algebra\u208d_\u208e {{CP}} x\n CQy = change-algebra\u208d_\u208e {{CQ}} y\n\n-- Lemma 2.5 appears in Base.Change.Equivalence.\n\n-- Abelian Groups Induce Change Algebras\n-- =====================================\n--\n-- In the paper, as the first example for change structures after\n-- Def. 2.1, we mention that \"each abelian group ... induces a\n-- change structure\". This is the formalization of this result.\n--\n-- The module GroupChanges below takes a proof that A forms an\n-- abelian group and provides a changeAlgebra for A. The proof of\n-- Def 2.1e is by equational reasoning using the group axioms, in\n-- the definition of changeAlgebra.isChangeAlgebra.update-diff.\n\nopen import Algebra.Structures\nopen import Data.Product\nopen import Function\n\n-- For non-abelian groups\nmodule PlainGroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isGroup : IsGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsGroup isGroup\n hiding\n ( refl\n ; sym\n )\n renaming\n ( \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n _\u229d_ : A \u2192 A \u2192 A\n v \u229d u = (u \u207b\u00b9) \u2295 v\n\n changeAlgebraGroup : ChangeAlgebra A\n changeAlgebraGroup = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; nil = const \u03b5\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8\u27e9\n v \u2295 ((v \u207b\u00b9) \u2295 u)\n \u2261\u27e8 sym (assoc _ _ _) \u27e9\n (v \u2295 (v \u207b\u00b9)) \u2295 u\n \u2261\u27e8 proj\u2082 inverse v \u27e8\u2295\u27e9 refl \u27e9\n \u03b5 \u2295 u\n \u2261\u27e8 proj\u2081 identity u \u27e9\n u\n \u220e\n ; update-nil = proj\u2082 identity\n }\n }\n\nmodule GroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isAbelianGroup : IsAbelianGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsAbelianGroup isAbelianGroup\n hiding\n ( refl\n )\n renaming\n ( _-_ to _\u229d_\n ; \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n changeAlgebraGroup : ChangeAlgebra A\n changeAlgebraGroup = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; nil = const \u03b5\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8 comm _ _ \u27e9\n (u \u229d v ) \u2295 v\n \u2261\u27e8\u27e9\n (u \u2295 (v \u207b\u00b9)) \u2295 v\n \u2261\u27e8 assoc _ _ _ \u27e9\n u \u2295 ((v \u207b\u00b9) \u2295 v)\n \u2261\u27e8 refl \u27e8\u2295\u27e9 proj\u2081 inverse v \u27e9\n u \u2295 \u03b5\n \u2261\u27e8 proj\u2082 identity u \u27e9\n u\n \u220e\n ; update-nil = proj\u2082 identity\n }\n }\n\n-- Function Changes\n-- ================\n--\n-- This is one of our most important results: Change structures\n-- can be lifted to function spaces. We formalize this as a module\n-- FunctionChanges that takes the change algebras for A and B as\n-- arguments, and provides a changeAlgebra for (A \u2192 B). The proofs\n-- are by equational reasoning using 2.1e for A and B.\n\nmodule FunctionChanges\n {a} {b} (A : Set a) (B : Set b) {{CA : ChangeAlgebra A}} {{CB : ChangeAlgebra B}}\n where\n -- This corresponds to Definition 2.6 in the paper.\n record FunctionChange (f : A \u2192 B) : Set (a \u2294 b) where\n field\n -- Definition 2.6a\n apply : RawChange f\n\n -- Definition 2.6b.\n -- (for some reason, the version in the paper has the arguments of \u2261\n -- flipped. Since \u2261 is symmetric, this doesn't matter).\n correct : (a : A) (da : \u0394 a) \u2192\n f (a \u229e da) \u229e apply (a \u229e da) (nil (a \u229e da)) \u2261 f a \u229e apply a da\n\n open FunctionChange public\n open \u2261-Reasoning\n open import Postulate.Extensionality\n\n funDiff : (g f : A \u2192 B) \u2192 FunctionChange f\n funDiff = \u03bb g f \u2192 record\n { apply = \u03bb a da \u2192 g (a \u229e da) \u229f f a\n -- the proof of correct is the first half of what we\n -- have to prove for Theorem 2.7.\n ; correct = \u03bb a da \u2192\n begin\n f (a \u229e da) \u229e (g (a \u229e da \u229e nil (a \u229e da)) \u229f f (a \u229e da))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f (a \u229e da) \u229e (g \u25a1 \u229f f (a \u229e da)))\n (update-nil (a \u229e da)) \u27e9\n f (a \u229e da) \u229e (g (a \u229e da) \u229f f (a \u229e da))\n \u2261\u27e8 update-diff (g (a \u229e da)) (f (a \u229e da)) \u27e9\n g (a \u229e da)\n \u2261\u27e8 sym (update-diff (g (a \u229e da)) (f a)) \u27e9\n f a \u229e (g (a \u229e da) \u229f f a)\n \u220e\n }\n\n funUpdate : \u2200 (f : A \u2192 B) (df : FunctionChange f) \u2192 A \u2192 B\n funUpdate = \u03bb f df a \u2192 f a \u229e apply df a (nil a)\n\n funNil : (f : A \u2192 B) \u2192 FunctionChange f\n funNil = \u03bb f \u2192 funDiff f f\n private\n -- Realizer for funNil\n funNil-realizer : (f : A \u2192 B) \u2192 RawChange f\n funNil-realizer f = \u03bb a da \u2192 f (a \u229e da) \u229f f a\n -- Note that it cannot use nil (f a), that would not be a correct function change!\n funNil-correct : \u2200 f a da \u2192 funNil-realizer f a da \u2261 apply (funNil f) a da\n funNil-correct f a da = refl\n\n mutual\n -- I have to write the type of funUpdateDiff without using changeAlgebra,\n -- so I just use the underlying implementations.\n funUpdateDiff : \u2200 u v \u2192 funUpdate v (funDiff u v) \u2261 u\n instance\n changeAlgebraFun : ChangeAlgebra (A \u2192 B)\n\n changeAlgebraFun = record\n { Change = FunctionChange\n -- in the paper, update and diff below are in Def. 2.7\n ; update = funUpdate\n ; diff = funDiff\n ; nil = funNil\n ; isChangeAlgebra = record\n -- the proof of update-diff is the second half of what\n -- we have to prove for Theorem 2.8.\n { update-diff = funUpdateDiff\n ; update-nil = \u03bb v \u2192 funUpdateDiff v v\n }\n }\n -- XXX remove mutual recursion by inlining the algebra in here?\n funUpdateDiff = \u03bb g f \u2192 ext (\u03bb a \u2192\n begin\n f a \u229e (g (a \u229e (nil a)) \u229f f a)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f a \u229e (g \u25a1 \u229f f a)) (update-nil a) \u27e9\n f a \u229e (g a \u229f f a)\n \u2261\u27e8 update-diff (g a) (f a) \u27e9\n g a\n \u220e)\n\n -- This is Theorem 2.9 in the paper.\n incrementalization : \u2200 (f : A \u2192 B) df a da \u2192\n (f \u229e df) (a \u229e da) \u2261 f a \u229e apply df a da\n incrementalization f df a da = correct df a da\n\n -- This is Theorem 2.10 in the paper. However, the derivative of f is just\n -- the apply component of `nil f`, not the full `nil f`, which also includes\n -- a proof. This is not an issue in the paper, which is formulated in a\n -- proof-irrelevant metalanguage.\n nil-is-derivative : \u2200 (f : A \u2192 B) \u2192\n IsDerivative f (apply (nil f))\n nil-is-derivative f a da =\n begin\n f a \u229e apply (nil f) a da\n \u2261\u27e8 sym (incrementalization f (nil f) a da) \u27e9\n (f \u229e nil f) (a \u229e da)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (a \u229e da))\n (update-nil f) \u27e9\n f (a \u229e da)\n \u220e\n\n -- Show that any derivative is a valid function change.\n\n -- In the paper, this is never actually stated. We just prove that nil\n -- changes are derivatives; the paper keeps talking about \"the derivative\",\n -- suggesting derivatives are unique. If derivatives were unique, we could\n -- say that the nil change is *the* derivative, hence the derivative is the\n -- nil change (hence also a change).\n --\n -- In fact, derivatives are only unique up to change equivalence and\n -- extensional equality; this is proven in Base.Change.Equivalence.derivative-unique.\n --\n Derivative-is-valid : \u2200 {f : A \u2192 B} df (IsDerivative-f-df : IsDerivative f df) a da \u2192\n f (a \u229e da) \u229e (df (a \u229e da) (nil (a \u229e da))) \u2261 f a \u229e df a da\n Derivative-is-valid {f} df IsDerivative-f-df a da rewrite IsDerivative-f-df (a \u229e da) (nil (a \u229e da)) | update-nil (a \u229e da) = sym (IsDerivative-f-df a da)\n\n DerivativeAsChange : \u2200 {f : A \u2192 B} {df} (IsDerivative-f-df : IsDerivative f df) \u2192 \u0394 f\n DerivativeAsChange {df = df} IsDerivative-f-df = record { apply = df ; correct = Derivative-is-valid df IsDerivative-f-df }\n -- In Equivalence.agda, derivative-is-nil-alternative then proves that a derivative is also a nil change.\n\n-- Reexport a few members with A and B marked as implicit parameters. This\n-- matters especially for changeAlgebra, since otherwise it can't be used for\n-- instance search.\nmodule _\n {a} {b} {A : Set a} {B : Set b} {{CA : ChangeAlgebra A}} {{CB : ChangeAlgebra B}}\n where\n open FunctionChanges A B {{CA}} {{CB}} public\n using\n ( changeAlgebraFun\n ; apply\n ; correct\n ; incrementalization\n ; DerivativeAsChange\n ; FunctionChange\n ; nil-is-derivative\n )\n\nmodule BinaryFunctionChanges\n {a} {b} {c} (A : Set a) (B : Set b) (C : Set c) {{CA : ChangeAlgebra A}} {{CB : ChangeAlgebra B}} {{CC : ChangeAlgebra C}} where\n incrementalization-binary : \u2200 (f : A \u2192 B \u2192 C) df a da b db \u2192\n (f \u229e df) (a \u229e da) (b \u229e db) \u2261 f a b \u229e apply (apply df a da) b db\n incrementalization-binary f df x dx y dy\n rewrite cong (\u03bb \u25a1 \u2192 \u25a1 (y \u229e dy)) (incrementalization f df x dx)\n = incrementalization (f x) (apply df x dx) y dy\n\n-- List (== Environment) Changes\n-- =============================\n--\n-- Here, we define a change structure on environments, given a\n-- change structure on the values in the environments. In the\n-- paper, we describe this in Definition 3.5. But note that this\n-- Agda formalization uses de Bruijn indices instead of names, so\n-- environments are just lists. Therefore, when we use Definition\n-- 3.5 in the paper, in this formalization, we use the list-like\n-- change structure defined here.\n\nopen import Data.List\nopen import Data.List.All\n\ndata All\u2032 {a p q} {A : Set a}\n {P : A \u2192 Set p}\n (Q : {x : A} \u2192 P x \u2192 Set q)\n : {xs : List A} (pxs : All P xs) \u2192 Set (p \u2294 q \u2294 a) where\n [] : All\u2032 Q []\n _\u2237_ : \u2200 {x xs} {px : P x} {pxs : All P xs} (qx : Q px) (qxs : All\u2032 Q pxs) \u2192 All\u2032 Q (px \u2237 pxs)\n\nmodule ListChanges\n {a} {A : Set a} (P : A \u2192 Set) {{C : ChangeAlgebraFamily P}}\n where\n update-all : \u2200 {xs} \u2192 (pxs : All P xs) \u2192 All\u2032 \u0394 pxs \u2192 All P xs\n update-all {[]} [] [] = []\n update-all {x \u2237 xs} (px \u2237 pxs) (dpx \u2237 dpxs) = (px \u229e dpx) \u2237 update-all pxs dpxs\n\n diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 All\u2032 \u0394 pxs\n diff-all [] [] = []\n diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = (px\u2032 \u229f px) \u2237 diff-all pxs\u2032 pxs\n\n update-diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 update-all pxs (diff-all pxs\u2032 pxs) \u2261 pxs\u2032\n update-diff-all [] [] = refl\n update-diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = cong\u2082 _\u2237_ (update-diff px\u2032 px) (update-diff-all pxs\u2032 pxs)\n\n instance\n changeAlgebraListChanges : ChangeAlgebraFamily (All P)\n\n changeAlgebraListChanges = record\n { change-algebra = \u03bb xs \u2192 record\n { Change = All\u2032 \u0394\n ; update = update-all\n ; diff = diff-all\n ; nil = \u03bb xs \u2192 diff-all xs xs\n ; isChangeAlgebra = record\n { update-diff = update-diff-all\n ; update-nil = \u03bb xs\u2081 \u2192 update-diff-all xs\u2081 xs\u2081\n }\n }\n }\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Change Structures.\n--\n-- This module defines the notion of change structures,\n-- as well as change structures for groups, functions and lists.\n--\n-- This module corresponds to Section 2 of the PLDI paper.\n------------------------------------------------------------------------\n\nmodule Base.Change.Algebra where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Change Algebras\n-- ===============\n--\n-- In the paper, change algebras are called \"change structures\"\n-- and they are described in Section 2.1. We follow the design of\n-- the Agda standard library and define change algebras as two\n-- records, so Definition 2.1 from the PLDI paper maps to the\n-- records IsChangeAlgebra and ChangeAlgebra.\n--\n-- A value of type (IsChangeAlgebra Change update diff) proves that\n-- Change, update and diff together form a change algebra.\n--\n-- A value of type (ChangeAlgebra Carrier) contains the necessary\n-- ingredients to create a change algebra for a carrier set.\n--\n-- In the paper, Carrier is called V (Def. 2.1a),\n-- Change is called \u0394 (Def. 2.1b),\n-- update is written as infix \u2295 (Def. 2.1c),\n-- diff is written as infix \u229d (Def. 2.1d),\n-- and update-diff is specified in Def. 2.1e.\n\nrecord IsChangeAlgebra\n {c}\n {Carrier : Set c}\n (Change : Carrier \u2192 Set c)\n (update : (v : Carrier) (dv : Change v) \u2192 Carrier)\n (diff : (u v : Carrier) \u2192 Change v)\n (nil : (u : Carrier) \u2192 Change u) : Set (c) where\n field\n update-diff : \u2200 u v \u2192 update v (diff u v) \u2261 u\n -- This corresponds to Lemma 2.3 from the paper.\n update-nil : \u2200 v \u2192 update v (nil v) \u2261 v\n\n -- abbreviations\n before : \u2200 {v} \u2192 Change v \u2192 Carrier\n before {v} _ = v\n\n after : \u2200 {v} \u2192 Change v \u2192 Carrier\n after {v} dv = update v dv\n\nrecord ChangeAlgebra {c}\n (Carrier : Set c) : Set (suc c) where\n field\n Change : Carrier \u2192 Set c\n update : (v : Carrier) (dv : Change v) \u2192 Carrier\n diff : (u v : Carrier) \u2192 Change v\n\n -- This generalizes Def. 2.2. from the paper.\n nil : (u : Carrier) \u2192 Change u\n\n isChangeAlgebra : IsChangeAlgebra Change update diff nil\n\n\n infixl 6 update diff\n\n open IsChangeAlgebra isChangeAlgebra public\n\n-- The following open ... public statement lets us use infix \u229e\n-- and \u229f for update and diff. In the paper, we use infix \u2295 and\n-- \u229d.\n\nopen ChangeAlgebra {{...}} public\n using\n ( update-diff\n ; update-nil\n ; nil\n ; before\n ; after\n )\n renaming\n ( Change to \u0394\n ; update to _\u229e_\n ; diff to _\u229f_\n )\n\n-- Families of Change Algebras\n-- ===========================\n--\n-- This is some Agda machinery to allow subscripting change\n-- algebra operations to avoid ambiguity. In the paper,\n-- we simply write (in the paragraph after Def. 2.1):\n--\n-- We overload operators \u2206, \u229d and \u2295 to refer to the\n-- corresponding operations of different change structures; we\n-- will subscript these symbols when needed to prevent\n-- ambiguity.\n--\n-- The following definitions implement this idea formally.\n\nrecord ChangeAlgebraFamily {a p} {A : Set a} (P : A \u2192 Set p): Set (suc p \u2294 a) where\n constructor\n family\n field\n change-algebra : \u2200 x \u2192 ChangeAlgebra (P x)\n\n module _ x where\n open ChangeAlgebra (change-algebra x) public\n\nmodule Family = ChangeAlgebraFamily {{...}}\n\nopen Family public\n using\n (\n )\n renaming\n ( Change to \u0394\u208d_\u208e\n ; nil to nil\u208d_\u208e\n ; update-diff to update-diff\u208d_\u208e\n ; update-nil to update-nil\u208d_\u208e\n ; change-algebra to change-algebra\u208d_\u208e\n ; before to before\u208d_\u208e\n ; after to after\u208d_\u208e\n )\n\n-- XXX not clear this is ever used\ninstance\n change-algebra-family-inst = change-algebra\u208d_\u208e\n\ninfixl 6 update\u2032 diff\u2032\n\nupdate\u2032 = Family.update\nsyntax update\u2032 x v dv = v \u229e\u208d x \u208e dv\n\ndiff\u2032 = Family.diff\nsyntax diff\u2032 x u v = u \u229f\u208d x \u208e v\n\n-- Derivatives\n-- ===========\n--\n-- This corresponds to Def. 2.4 in the paper.\n\nRawChange : \u2200 {a b} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra A}} \u2192\n {{CB : ChangeAlgebra B}} \u2192\n (f : A \u2192 B) \u2192\n Set (a \u2294 b)\nRawChange f = \u2200 a (da : \u0394 a) \u2192 \u0394 (f a)\n\nIsDerivative : \u2200 {a b} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra A}} \u2192\n {{CB : ChangeAlgebra B}} \u2192\n (f : A \u2192 B) \u2192\n (df : RawChange f) \u2192\n Set (a \u2294 b)\nIsDerivative f df = \u2200 a da \u2192 f a \u229e df a da \u2261 f (a \u229e da)\n\n-- This is a variant of IsDerivative for change algebra families.\nRawChange\u208d_,_\u208e : \u2200 {a b p q} {A : Set a} {B : Set b} {P : A \u2192 Set p} {Q : B \u2192 Set q} \u2192\n {{CP : ChangeAlgebraFamily P}} \u2192\n {{CQ : ChangeAlgebraFamily Q}} \u2192\n (x : A) \u2192\n (y : B) \u2192\n (f : P x \u2192 Q y) \u2192 Set (p \u2294 q)\nRawChange\u208d x , y \u208e f = \u2200 px (dpx : \u0394\u208d x \u208e px) \u2192 \u0394\u208d y \u208e (f px)\n\nIsDerivative\u208d_,_\u208e : \u2200 {a b p q} {A : Set a} {B : Set b} {P : A \u2192 Set p} {Q : B \u2192 Set q} \u2192\n {{CP : ChangeAlgebraFamily P}} \u2192\n {{CQ : ChangeAlgebraFamily Q}} \u2192\n (x : A) \u2192\n (y : B) \u2192\n (f : P x \u2192 Q y) \u2192\n (df : RawChange\u208d_,_\u208e x y f) \u2192\n Set (p \u2294 q)\nIsDerivative\u208d_,_\u208e {P = P} {{CP}} {{CQ}} x y f df = IsDerivative {{change-algebra\u208d _ \u208e}} {{change-algebra\u208d _ \u208e}} f df where\n CPx = change-algebra\u208d_\u208e {{CP}} x\n CQy = change-algebra\u208d_\u208e {{CQ}} y\n\n-- Lemma 2.5 appears in Base.Change.Equivalence.\n\n-- Abelian Groups Induce Change Algebras\n-- =====================================\n--\n-- In the paper, as the first example for change structures after\n-- Def. 2.1, we mention that \"each abelian group ... induces a\n-- change structure\". This is the formalization of this result.\n--\n-- The module GroupChanges below takes a proof that A forms an\n-- abelian group and provides a changeAlgebra for A. The proof of\n-- Def 2.1e is by equational reasoning using the group axioms, in\n-- the definition of changeAlgebra.isChangeAlgebra.update-diff.\n\nopen import Algebra.Structures\nopen import Data.Product\nopen import Function\n\nmodule GroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isGroup : IsGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsGroup isGroup\n hiding\n ( refl\n ; sym\n )\n renaming\n ( \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n _\u229d_ : A \u2192 A \u2192 A\n v \u229d u = (u \u207b\u00b9) \u2295 v\n\n changeAlgebraGroup : ChangeAlgebra A\n changeAlgebraGroup = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; nil = const \u03b5\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8\u27e9\n v \u2295 ((v \u207b\u00b9) \u2295 u)\n \u2261\u27e8 sym (assoc _ _ _) \u27e9\n (v \u2295 (v \u207b\u00b9)) \u2295 u\n \u2261\u27e8 proj\u2082 inverse v \u27e8\u2295\u27e9 refl \u27e9\n \u03b5 \u2295 u\n \u2261\u27e8 proj\u2081 identity u \u27e9\n u\n \u220e\n ; update-nil = proj\u2082 identity\n }\n }\n\nmodule AbelianGroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isAbelianGroup : IsAbelianGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsAbelianGroup isAbelianGroup\n hiding\n ( refl\n )\n renaming\n ( _-_ to _\u229d_\n ; \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n changeAlgebraGroup : ChangeAlgebra A\n changeAlgebraGroup = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; nil = const \u03b5\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8 comm _ _ \u27e9\n (u \u229d v ) \u2295 v\n \u2261\u27e8\u27e9\n (u \u2295 (v \u207b\u00b9)) \u2295 v\n \u2261\u27e8 assoc _ _ _ \u27e9\n u \u2295 ((v \u207b\u00b9) \u2295 v)\n \u2261\u27e8 refl \u27e8\u2295\u27e9 proj\u2081 inverse v \u27e9\n u \u2295 \u03b5\n \u2261\u27e8 proj\u2082 identity u \u27e9\n u\n \u220e\n ; update-nil = proj\u2082 identity\n }\n }\n\n-- Function Changes\n-- ================\n--\n-- This is one of our most important results: Change structures\n-- can be lifted to function spaces. We formalize this as a module\n-- FunctionChanges that takes the change algebras for A and B as\n-- arguments, and provides a changeAlgebra for (A \u2192 B). The proofs\n-- are by equational reasoning using 2.1e for A and B.\n\nmodule FunctionChanges\n {a} {b} (A : Set a) (B : Set b) {{CA : ChangeAlgebra A}} {{CB : ChangeAlgebra B}}\n where\n -- This corresponds to Definition 2.6 in the paper.\n record FunctionChange (f : A \u2192 B) : Set (a \u2294 b) where\n field\n -- Definition 2.6a\n apply : RawChange f\n\n -- Definition 2.6b.\n -- (for some reason, the version in the paper has the arguments of \u2261\n -- flipped. Since \u2261 is symmetric, this doesn't matter).\n correct : (a : A) (da : \u0394 a) \u2192\n f (a \u229e da) \u229e apply (a \u229e da) (nil (a \u229e da)) \u2261 f a \u229e apply a da\n\n open FunctionChange public\n open \u2261-Reasoning\n open import Postulate.Extensionality\n\n funDiff : (g f : A \u2192 B) \u2192 FunctionChange f\n funDiff = \u03bb g f \u2192 record\n { apply = \u03bb a da \u2192 g (a \u229e da) \u229f f a\n -- the proof of correct is the first half of what we\n -- have to prove for Theorem 2.7.\n ; correct = \u03bb a da \u2192\n begin\n f (a \u229e da) \u229e (g (a \u229e da \u229e nil (a \u229e da)) \u229f f (a \u229e da))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f (a \u229e da) \u229e (g \u25a1 \u229f f (a \u229e da)))\n (update-nil (a \u229e da)) \u27e9\n f (a \u229e da) \u229e (g (a \u229e da) \u229f f (a \u229e da))\n \u2261\u27e8 update-diff (g (a \u229e da)) (f (a \u229e da)) \u27e9\n g (a \u229e da)\n \u2261\u27e8 sym (update-diff (g (a \u229e da)) (f a)) \u27e9\n f a \u229e (g (a \u229e da) \u229f f a)\n \u220e\n }\n\n funUpdate : \u2200 (f : A \u2192 B) (df : FunctionChange f) \u2192 A \u2192 B\n funUpdate = \u03bb f df a \u2192 f a \u229e apply df a (nil a)\n\n funNil : (f : A \u2192 B) \u2192 FunctionChange f\n funNil = \u03bb f \u2192 funDiff f f\n private\n -- Realizer for funNil\n funNil-realizer : (f : A \u2192 B) \u2192 RawChange f\n funNil-realizer f = \u03bb a da \u2192 f (a \u229e da) \u229f f a\n -- Note that it cannot use nil (f a), that would not be a correct function change!\n funNil-correct : \u2200 f a da \u2192 funNil-realizer f a da \u2261 apply (funNil f) a da\n funNil-correct f a da = refl\n\n mutual\n -- I have to write the type of funUpdateDiff without using changeAlgebra,\n -- so I just use the underlying implementations.\n funUpdateDiff : \u2200 u v \u2192 funUpdate v (funDiff u v) \u2261 u\n instance\n changeAlgebraFun : ChangeAlgebra (A \u2192 B)\n\n changeAlgebraFun = record\n { Change = FunctionChange\n -- in the paper, update and diff below are in Def. 2.7\n ; update = funUpdate\n ; diff = funDiff\n ; nil = funNil\n ; isChangeAlgebra = record\n -- the proof of update-diff is the second half of what\n -- we have to prove for Theorem 2.8.\n { update-diff = funUpdateDiff\n ; update-nil = \u03bb v \u2192 funUpdateDiff v v\n }\n }\n -- XXX remove mutual recursion by inlining the algebra in here?\n funUpdateDiff = \u03bb g f \u2192 ext (\u03bb a \u2192\n begin\n f a \u229e (g (a \u229e (nil a)) \u229f f a)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f a \u229e (g \u25a1 \u229f f a)) (update-nil a) \u27e9\n f a \u229e (g a \u229f f a)\n \u2261\u27e8 update-diff (g a) (f a) \u27e9\n g a\n \u220e)\n\n -- This is Theorem 2.9 in the paper.\n incrementalization : \u2200 (f : A \u2192 B) df a da \u2192\n (f \u229e df) (a \u229e da) \u2261 f a \u229e apply df a da\n incrementalization f df a da = correct df a da\n\n -- This is Theorem 2.10 in the paper. However, the derivative of f is just\n -- the apply component of `nil f`, not the full `nil f`, which also includes\n -- a proof. This is not an issue in the paper, which is formulated in a\n -- proof-irrelevant metalanguage.\n nil-is-derivative : \u2200 (f : A \u2192 B) \u2192\n IsDerivative f (apply (nil f))\n nil-is-derivative f a da =\n begin\n f a \u229e apply (nil f) a da\n \u2261\u27e8 sym (incrementalization f (nil f) a da) \u27e9\n (f \u229e nil f) (a \u229e da)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (a \u229e da))\n (update-nil f) \u27e9\n f (a \u229e da)\n \u220e\n\n -- Show that any derivative is a valid function change.\n\n -- In the paper, this is never actually stated. We just prove that nil\n -- changes are derivatives; the paper keeps talking about \"the derivative\",\n -- suggesting derivatives are unique. If derivatives were unique, we could\n -- say that the nil change is *the* derivative, hence the derivative is the\n -- nil change (hence also a change).\n --\n -- In fact, derivatives are only unique up to change equivalence and\n -- extensional equality; this is proven in Base.Change.Equivalence.derivative-unique.\n --\n Derivative-is-valid : \u2200 {f : A \u2192 B} df (IsDerivative-f-df : IsDerivative f df) a da \u2192\n f (a \u229e da) \u229e (df (a \u229e da) (nil (a \u229e da))) \u2261 f a \u229e df a da\n Derivative-is-valid {f} df IsDerivative-f-df a da rewrite IsDerivative-f-df (a \u229e da) (nil (a \u229e da)) | update-nil (a \u229e da) = sym (IsDerivative-f-df a da)\n\n DerivativeAsChange : \u2200 {f : A \u2192 B} {df} (IsDerivative-f-df : IsDerivative f df) \u2192 \u0394 f\n DerivativeAsChange {df = df} IsDerivative-f-df = record { apply = df ; correct = Derivative-is-valid df IsDerivative-f-df }\n -- In Equivalence.agda, derivative-is-nil-alternative then proves that a derivative is also a nil change.\n\n-- Reexport a few members with A and B marked as implicit parameters. This\n-- matters especially for changeAlgebra, since otherwise it can't be used for\n-- instance search.\nmodule _\n {a} {b} {A : Set a} {B : Set b} {{CA : ChangeAlgebra A}} {{CB : ChangeAlgebra B}}\n where\n open FunctionChanges A B {{CA}} {{CB}} public\n using\n ( changeAlgebraFun\n ; apply\n ; correct\n ; incrementalization\n ; DerivativeAsChange\n ; FunctionChange\n ; nil-is-derivative\n )\n\nmodule BinaryFunctionChanges\n {a} {b} {c} (A : Set a) (B : Set b) (C : Set c) {{CA : ChangeAlgebra A}} {{CB : ChangeAlgebra B}} {{CC : ChangeAlgebra C}} where\n incrementalization-binary : \u2200 (f : A \u2192 B \u2192 C) df a da b db \u2192\n (f \u229e df) (a \u229e da) (b \u229e db) \u2261 f a b \u229e apply (apply df a da) b db\n incrementalization-binary f df x dx y dy\n rewrite cong (\u03bb \u25a1 \u2192 \u25a1 (y \u229e dy)) (incrementalization f df x dx)\n = incrementalization (f x) (apply df x dx) y dy\n\n-- List (== Environment) Changes\n-- =============================\n--\n-- Here, we define a change structure on environments, given a\n-- change structure on the values in the environments. In the\n-- paper, we describe this in Definition 3.5. But note that this\n-- Agda formalization uses de Bruijn indices instead of names, so\n-- environments are just lists. Therefore, when we use Definition\n-- 3.5 in the paper, in this formalization, we use the list-like\n-- change structure defined here.\n\nopen import Data.List\nopen import Data.List.All\n\ndata All\u2032 {a p q} {A : Set a}\n {P : A \u2192 Set p}\n (Q : {x : A} \u2192 P x \u2192 Set q)\n : {xs : List A} (pxs : All P xs) \u2192 Set (p \u2294 q \u2294 a) where\n [] : All\u2032 Q []\n _\u2237_ : \u2200 {x xs} {px : P x} {pxs : All P xs} (qx : Q px) (qxs : All\u2032 Q pxs) \u2192 All\u2032 Q (px \u2237 pxs)\n\nmodule ListChanges\n {a} {A : Set a} (P : A \u2192 Set) {{C : ChangeAlgebraFamily P}}\n where\n update-all : \u2200 {xs} \u2192 (pxs : All P xs) \u2192 All\u2032 \u0394 pxs \u2192 All P xs\n update-all {[]} [] [] = []\n update-all {x \u2237 xs} (px \u2237 pxs) (dpx \u2237 dpxs) = (px \u229e dpx) \u2237 update-all pxs dpxs\n\n diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 All\u2032 \u0394 pxs\n diff-all [] [] = []\n diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = (px\u2032 \u229f px) \u2237 diff-all pxs\u2032 pxs\n\n update-diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 update-all pxs (diff-all pxs\u2032 pxs) \u2261 pxs\u2032\n update-diff-all [] [] = refl\n update-diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = cong\u2082 _\u2237_ (update-diff px\u2032 px) (update-diff-all pxs\u2032 pxs)\n\n instance\n changeAlgebraListChanges : ChangeAlgebraFamily (All P)\n\n changeAlgebraListChanges = record\n { change-algebra = \u03bb xs \u2192 record\n { Change = All\u2032 \u0394\n ; update = update-all\n ; diff = diff-all\n ; nil = \u03bb xs \u2192 diff-all xs xs\n ; isChangeAlgebra = record\n { update-diff = update-diff-all\n ; update-nil = \u03bb xs\u2081 \u2192 update-diff-all xs\u2081 xs\u2081\n }\n }\n }\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ddad4849616d2fea806aa829236f7932b15bd3a3","subject":"Tests","message":"Tests\n","repos":"np\/agda-parametricity","old_file":"lib\/Reflection\/Param\/Tests.agda","new_file":"lib\/Reflection\/Param\/Tests.agda","new_contents":"{-# OPTIONS -vtc.unquote.decl:20 -vtc.unquote.def:20 #-}\n{-# OPTIONS --without-K #-}\nopen import Level hiding (zero; suc)\nopen import Data.Unit renaming (\u22a4 to \ud835\udfd9; tt to 0\u2081)\nopen import Data.Bool\n using (not)\n renaming (Bool to \ud835\udfda; false to 0\u2082; true to 1\u2082)\nopen import Data.String.Core using (String)\nopen import Data.Float using (Float)\nopen import Function\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Nat hiding (_\u225f_)\nopen import Data.List using (List; []; _\u2237_)\nopen import Relation.Binary.PropositionalEquality\n using (_\u2261_; refl) renaming (_\u2257_ to _~_)\n\nopen import Function.Param.Unary\nopen import Function.Param.Binary\nopen import Type.Param.Unary\nopen import Type.Param.Binary\nopen import Data.Two.Param.Binary\nopen import Data.Nat.Param.Binary\nopen import Reflection.NP\nopen import Reflection.Param\nopen import Reflection.Param.Env\n\nmodule Reflection.Param.Tests where\n\nimport Reflection.Printer as Pr\nopen Pr using (var;con;def;lam;pi;sort;unknown;showTerm;showType;showDef;showFunDef;showClauses)\n\n-- Local \"imports\" to avoid depending on nplib\nprivate\n postulate\n opaque : \u2200 {a b} {A : Set a} {B : Set b} \u2192 A \u2192 B \u2192 B\n -- opaque-rule : \u2200 {x} y \u2192 opaque x y \u2261 y\n\n \u2605\u2080 = Set\u2080\n \u2605\u2081 = Set\u2081\n\ninfixr 1 _[\u2080\u2192\u2080]_\n_[\u2080\u2192\u2080]_ : \u2200 {A : Set\u2080} (A\u209a : A \u2192 Set\u2080)\n {B : Set\u2080} (B\u209a : B \u2192 Set\u2080)\n (f : A \u2192 B) \u2192 Set\u2080\n_[\u2080\u2192\u2080]_ = \u03bb {A} A\u209a {B} B\u209a f \u2192 \u2200 {a} (a\u209a : A\u209a a) \u2192 B\u209a (f a)\n\ninfixr 1 _[\u2080\u2192\u2081]_\n_[\u2080\u2192\u2081]_ : \u2200 {A : Set\u2080} (A\u209a : A \u2192 Set\u2080)\n {B : Set\u2081} (B\u209a : B \u2192 Set\u2081)\n (f : A \u2192 B) \u2192 Set\u2081\n_[\u2080\u2192\u2081]_ = \u03bb {A} A\u209a {B} B\u209a f \u2192 \u2200 {a} (a\u209a : A\u209a a) \u2192 B\u209a (f a)\n\ninfixr 1 _[\u2081\u2192\u2081]_\n_[\u2081\u2192\u2081]_ : \u2200 {A : Set\u2081} (A\u209a : A \u2192 Set\u2081)\n {B : Set\u2081} (B\u209a : B \u2192 Set\u2081)\n (f : A \u2192 B) \u2192 Set\u2081\n_[\u2081\u2192\u2081]_ = \u03bb {A} A\u209a {B} B\u209a f \u2192 \u2200 {a} (a\u209a : A\u209a a) \u2192 B\u209a (f a)\n\ninfixr 1 _[\u2081\u2192\u2082]_\n_[\u2081\u2192\u2082]_ : \u2200 {A : Set\u2081} (A\u209a : A \u2192 Set\u2081)\n {B : Set\u2082} (B\u209a : B \u2192 Set\u2082)\n (f : A \u2192 B) \u2192 Set\u2082\n_[\u2081\u2192\u2082]_ = \u03bb {A} A\u209a {B} B\u209a f \u2192 \u2200 {a} (a\u209a : A\u209a a) \u2192 B\u209a (f a)\n\n[[Set\u2080]] : ([Set\u2080] [\u2081\u2192\u2082] [Set\u2081]) [Set\u2080]\n[[Set\u2080]] = \u03bb A \u2192 A [\u2080\u2192\u2081] [Set\u2080]\n\n{-\nEqEnv = {!!}\nEqRes = {!!}\n\neqTerm : EqEnv \u2192 Term \u2192 Term \u2192 EqRes\neqTerm \u0393 (var x args) (var x\u2081 args\u2081) = {!!}\neqTerm \u0393 (def f\u2080 args\u2080) (def f\u2081 args\u2081) = {!!}\neqTerm \u0393 (con c\u2080 args\u2080) (con c\u2081 args\u2081) = {!!}\neqTerm \u0393 (lam v t) (lam v' t') = {!!}\neqTerm \u0393 (pi t\u2081 t\u2082) (pi u\u2081 u\u2082) = {!!}\neqTerm \u0393 (sort s\u2080) (sort s\u2081) = {!!}\neqTerm \u0393 (lit l\u2080) (lit l\u2081) = {!!}\neqTerm \u0393 unknown unknown = {!!}\neqTerm \u0393 (def f args) u = {!!}\n--eqTerm \u0393 (pat-lam cs args) u = {!!}\neqTerm _ _ = ?\n-}\n\n{-\nimport Reflection.Simple as Si\nopen Si using (var;con;def;lam;pi;sort;unknown;simple;showTerm)\n-}\n\n_\u2261-no-hints_ : Term \u2192 Term \u2192 Set\nt \u2261-no-hints u = noHintsTerm t \u2261 noHintsTerm u\n\n_\u2261-def-no-hints_ : Definition \u2192 Definition \u2192 Set\nt \u2261-def-no-hints u = noHintsDefinition t \u2261 noHintsDefinition u\n\np[Set\u2080]-type = param-type-by-name (\u03b5 1) (quote [Set\u2080])\np[Set\u2080] = param-clauses-by-name (\u03b5 1) (quote [Set\u2080])\nq[[Set\u2080]] = definition (quote [[Set\u2080]]) -- quoteTerm [[Set\u2080]]\ntest-type-p[Set\u2080] : ([Set\u2080] [\u2081\u2192\u2082] [Set\u2081]) [Set\u2080] \u2261 unquote (unEl p[Set\u2080]-type)\ntest-type-p[Set\u2080] = refl\ntest-term-p[Set\u2080] : quoteTerm [[Set\u2080]] \u2261-no-hints Get-term.from-clauses p[Set\u2080]\ntest-term-p[Set\u2080] = refl\nu-p[Set\u2080] : ([Set\u2080] [\u2081\u2192\u2082] [Set\u2081]) [Set\u2080]\nunquoteDef u-p[Set\u2080] = p[Set\u2080]\n\nFalse : Set\u2081\nFalse = (A : Set) \u2192 A\n\nparam1-False-type = param-type-by-name (\u03b5 1) (quote False)\nparam1-False-term = param-term-by-name (\u03b5 1) (quote False)\n\nparam1-False-type-check : [Set\u2081] False \u2261 unquote (unEl param1-False-type)\nparam1-False-type-check = refl\n\n[False] : unquote (unEl param1-False-type)\n[False] = unquote param1-False-term\n\n[Level] : [Set\u2080] Level\n[Level] _ = \ud835\udfd9\n\n[String] : [Set\u2080] String\n[String] _ = \ud835\udfd9\n\n[Float] : [Set\u2080] Float\n[Float] _ = \ud835\udfd9\n\n-- Levels are parametric, hence the relation is total\n\u27e6Level\u27e7 : \u27e6Set\u2080\u27e7 Level Level\n\u27e6Level\u27e7 _ _ = \ud835\udfd9\n\n\u27e6String\u27e7 : \u27e6Set\u2080\u27e7 String String\n\u27e6String\u27e7 = _\u2261_\n\n\u27e6Float\u27e7 : \u27e6Set\u2080\u27e7 Float Float\n\u27e6Float\u27e7 = _\u2261_\n\ndata [\ud835\udfda] : [Set\u2080] \ud835\udfda where\n [0\u2082] : [\ud835\udfda] 0\u2082\n [1\u2082] : [\ud835\udfda] 1\u2082\n\ndata [\u2115] : [Set\u2080] \u2115 where\n [zero] : [\u2115] zero\n [suc] : ([\u2115] [\u2192] [\u2115]) suc\n\n[pred] : ([\u2115] [\u2192] [\u2115]) pred\n[pred] [zero] = [zero]\n[pred] ([suc] x\u209a) = x\u209a\n\ndefDefEnv1 : Name \u2192 Name\ndefDefEnv1 (quote \ud835\udfda) = quote [\ud835\udfda]\ndefDefEnv1 (quote \u2115) = quote [\u2115]\ndefDefEnv1 (quote String) = quote [String]\ndefDefEnv1 (quote Float) = quote [Float]\ndefDefEnv1 (quote \u2605\u2080) = quote [Set\u2080]\ndefDefEnv1 (quote \u2605\u2081) = quote [Set\u2081]\ndefDefEnv1 (quote False) = quote [False]\ndefDefEnv1 (quote Level) = quote [Level]\ndefDefEnv1 n = opaque \"defDefEnv1\" n\n\ndefConEnv1 : Name \u2192 Name\ndefConEnv1 (quote 0\u2082) = quote [0\u2082]\ndefConEnv1 (quote 1\u2082) = quote [1\u2082]\ndefConEnv1 (quote \u2115.zero) = quote [zero]\ndefConEnv1 (quote \u2115.suc) = quote [suc]\ndefConEnv1 (quote Level.zero) = quote 0\u2081\ndefConEnv1 (quote Level.suc) = quote 0\u2081\ndefConEnv1 n = opaque \"defConEnv1\" n\n\ndefDefEnv2 : Name \u2192 Name\ndefDefEnv2 (quote \ud835\udfda) = quote \u27e6\ud835\udfda\u27e7\ndefDefEnv2 (quote \u2115) = quote \u27e6\u2115\u27e7\ndefDefEnv2 (quote \u2605\u2080) = quote \u27e6Set\u2080\u27e7\ndefDefEnv2 (quote \u2605\u2081) = quote \u27e6Set\u2081\u27e7\ndefDefEnv2 (quote String) = quote \u27e6String\u27e7\ndefDefEnv2 (quote Float) = quote \u27e6Float\u27e7\ndefDefEnv2 (quote Level) = quote \u27e6Level\u27e7\ndefDefEnv2 n = opaque \"defDefEnv\" n\n\ndefConEnv2 : Name \u2192 Name\ndefConEnv2 (quote 0\u2082) = quote \u27e60\u2082\u27e7\ndefConEnv2 (quote 1\u2082) = quote \u27e61\u2082\u27e7\ndefConEnv2 (quote \u2115.zero) = quote \u27e6\u2115\u27e7.\u27e6zero\u27e7\ndefConEnv2 (quote \u2115.suc) = quote \u27e6\u2115\u27e7.\u27e6suc\u27e7\ndefConEnv2 (quote Level.zero) = quote 0\u2081\ndefConEnv2 (quote Level.suc) = quote 0\u2081\ndefConEnv2 n = opaque \"defConEnv2\" n\n\ndefEnv0 : Env' 0\ndefEnv0 = record (\u03b5 0)\n { pConT = con\n ; pConP = con\n ; pDef = id }\n\ndefEnv1 : Env' 1\ndefEnv1 = record (\u03b5 1)\n { pConP = con \u2218\u2032 defConEnv1\n ; pConT = con \u2218\u2032 defConEnv1\n ; pDef = defDefEnv1 }\n\ndefEnv2 : Env' 2\ndefEnv2 = record (\u03b5 2)\n { pConP = con \u2218\u2032 defConEnv2\n ; pConT = con \u2218\u2032 defConEnv2\n ; pDef = defDefEnv2 }\n\nparam1-[False]-type = param-type-by-name defEnv1 (quote [False])\nparam1-[False]-term = param-term-by-name defEnv1 (quote [False])\n\n{-\nmodule Const where\n postulate\n A : Set\u2080\n A\u1d63 : A \u2192 A \u2192 Set\u2080\n data Wrapper : Set where\n wrap : A \u2192 Wrapper\n\n idWrapper : Wrapper \u2192 Wrapper\n idWrapper (wrap x) = wrap x\n\n data \u27e6Wrapper\u27e7 : Wrapper \u2192 Wrapper \u2192 Set\u2080 where\n \u27e6wrap\u27e7 : (A\u1d63 \u27e6\u2192\u27e7 \u27e6Wrapper\u27e7) wrap wrap\n\n wrapperEnv = record (\u03b5 2)\n { pDef = [ quote Wrapper \u2254 quote \u27e6Wrapper\u27e7 ] id\n ; pConP = [ quote Wrapper.wrap \u2254 con (quote \u27e6Wrapper\u27e7.\u27e6wrap\u27e7) ] con\n ; pConT = [ quote Wrapper.wrap \u2254 con (quote \u27e6Wrapper\u27e7.\u27e6wrap\u27e7) ] con\n }\n\n unquoteDecl \u27e6idWrapper\u27e7 = param-rec-def-by-name wrapperEnv (quote idWrapper) \u27e6idWrapper\u27e7\n-}\n\ndata Wrapper (A : Set\u2080) : Set\u2080 where\n wrap : A \u2192 Wrapper A\n\nidWrapper : \u2200 {A} \u2192 Wrapper A \u2192 Wrapper A\nidWrapper (wrap x) = wrap x\n\ndata [Wrapper] {A : Set} (A\u209a : A \u2192 Set\u2080)\n : Wrapper A \u2192 Set\u2080 where\n [wrap] : (A\u209a [\u2192] [Wrapper] A\u209a) wrap\n\n[Wrapper]-env = record (\u03b5 1)\n { pDef = [ quote Wrapper \u2254 quote [Wrapper] ] id\n ; pConP = [ quote wrap \u2254 con (quote [wrap]) ] con\n ; pConT = [ quote wrap \u2254 conSkip' 2 (quote [wrap]) ] con\n }\n\nunquoteDecl [idWrapper] =\n param-rec-def-by-name [Wrapper]-env (quote idWrapper) [idWrapper]\n\n {-\n[idWrapper] = {! param-rec-def-by-name [Wrapper]-env (quote idWrapper) [idWrapper]!}\n-- [idWrapper] = {! param-rec-def-by-name [Wrapper]-env (quote idWrapper) [idWrapper]!}\n-}\n\n{-\n-- The generated type bigger since it is a familly for no reason.\ndata \u27e6Wrapper\u27e7 : (\u27e6Set\u2080\u27e7 \u27e6\u2192\u27e7 \u27e6Set\u2081\u27e7) Wrapper Wrapper\n\nprivate\n \u27e6Wrapper\u27e7-ctor = \u03bb c \u2192 unEl (param-ctor-by-name (extDefEnv [ quote Wrapper \u2254 quote \u27e6Wrapper\u27e7 ] (\u03b5 2)) c)\n\ndata \u27e6Wrapper\u27e7 where\n \u27e6wrap\u27e7 : unquote (\u27e6Wrapper\u27e7-ctor (quote Wrapper.wrap))\n-}\ndata \u27e6Wrapper\u27e7 {A\u2080 A\u2081 : Set} (A\u1d63 : A\u2080 \u2192 A\u2081 \u2192 Set\u2080)\n : Wrapper A\u2080 \u2192 Wrapper A\u2081 \u2192 Set\u2080 where\n \u27e6wrap\u27e7 : (A\u1d63 \u27e6\u2192\u27e7 \u27e6Wrapper\u27e7 A\u1d63) wrap wrap\n\n\u27e6Wrapper\u27e7-env = record (\u03b5 2)\n { pDef = [ quote Wrapper \u2254 quote \u27e6Wrapper\u27e7 ] id\n ; pConP = [ quote wrap \u2254 con (quote \u27e6wrap\u27e7) ] con\n ; pConT = [ quote wrap \u2254 conSkip' 3 (quote \u27e6wrap\u27e7) ] con\n }\n\n\u27e6idWrapper\u27e71 : (\u2200\u27e8 A \u2236 \u27e6Set\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Wrapper\u27e7 A \u27e6\u2192\u27e7 \u27e6Wrapper\u27e7 A) idWrapper idWrapper\n\u27e6idWrapper\u27e71 {x0} {x1} (x2) {._} {._} (\u27e6wrap\u27e7 {x3} {x4} x5)\n = \u27e6wrap\u27e7 {_} {_} {_} {x3} {x4} x5\n\n\u27e6idWrapper\u27e7-clauses =\n clause\n (arg (arg-info hidden relevant) (var \"A0\") \u2237\n arg (arg-info hidden relevant) (var \"A1\") \u2237\n arg (arg-info visible relevant) (var \"Ar\") \u2237\n arg (arg-info hidden relevant) dot \u2237\n arg (arg-info hidden relevant) dot \u2237\n arg (arg-info visible relevant)\n (con (quote \u27e6wrap\u27e7)\n (arg (arg-info hidden relevant) (var \"x0\") \u2237\n arg (arg-info hidden relevant) (var \"x1\") \u2237\n arg (arg-info visible relevant) (var \"xr\") \u2237 []))\n \u2237 [])\n (con (quote \u27e6wrap\u27e7)\n (arg (arg-info hidden relevant) unknown \u2237\n arg (arg-info hidden relevant) unknown \u2237\n arg (arg-info hidden relevant) unknown \u2237\n arg (arg-info hidden relevant) (var 2 []) \u2237\n arg (arg-info hidden relevant) (var 1 []) \u2237\n arg (arg-info visible relevant) (var 0 []) \u2237 []))\n \u2237 []\n\n\u27e6idWrapper\u27e72 : (\u2200\u27e8 A \u2236 \u27e6Set\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Wrapper\u27e7 A \u27e6\u2192\u27e7 \u27e6Wrapper\u27e7 A) idWrapper idWrapper\nunquoteDef \u27e6idWrapper\u27e72 = \u27e6idWrapper\u27e7-clauses\n\nunquoteDecl \u27e6idWrapper\u27e7 =\n param-rec-def-by-name \u27e6Wrapper\u27e7-env (quote idWrapper) \u27e6idWrapper\u27e7\n\ndata Bot (A : Set\u2080) : Set\u2080 where\n bot : Bot A \u2192 Bot A\n\ngobot : \u2200 {A} \u2192 Bot A \u2192 A\ngobot (bot x) = gobot x\n\ndata [Bot] {A : Set} (A\u209a : A \u2192 Set\u2080)\n : Bot A \u2192 Set\u2080 where\n [bot] : ([Bot] A\u209a [\u2192] [Bot] A\u209a) bot\n\n[Bot]-env = record (\u03b5 1)\n { pDef = [ quote Bot \u2254 quote [Bot] ] id\n ; pConP = [ quote bot \u2254 con (quote [bot]) ] con\n ; pConT = [ quote bot \u2254 conSkip' 2 (quote [bot]) ] con\n }\n\n[gobot]' : (\u2200\u27e8 A \u2236 [Set\u2080] \u27e9[\u2192] [Bot] A [\u2192] A) gobot\n[gobot]' {x0} (x1) {._} ([bot] {x2} x3)\n = [gobot]' {x0} x1 {x2} x3\n\n-- [gobot]' = {!showClauses \"[gobot]'\" (param-rec-clauses-by-name [Bot]-env (quote gobot) (quote [gobot]'))!}\n\n[gobot]2 : (\u2200\u27e8 A \u2236 [Set\u2080] \u27e9[\u2192] [Bot] A [\u2192] A) gobot\n\n[gobot]2-clauses =\n clause\n (arg (arg-info hidden relevant) (var \"A\u1d620\") \u2237\n arg (arg-info visible relevant) (var \"A\u1d63\") \u2237\n arg (arg-info hidden relevant) dot \u2237\n arg (arg-info visible relevant)\n (con (quote [bot])\n (arg (arg-info hidden relevant) (var \"x\u1d620\") \u2237\n arg (arg-info visible relevant) (var \"x\u1d63\") \u2237 []))\n \u2237 [])\n (def (quote [gobot]2)\n (arg (arg-info hidden relevant) (var 4 []) \u2237\n arg (arg-info visible relevant) (var 3 []) \u2237\n arg (arg-info hidden relevant) (var 1 []) \u2237\n arg (arg-info visible relevant) (var 0 []) \u2237 []))\n \u2237 []\n\nunquoteDef [gobot]2 = [gobot]2-clauses\n\nunquoteDecl [gobot] =\n param-rec-def-by-name [Bot]-env (quote gobot) [gobot]\n\ndata \u27e6Bot\u27e7 {A\u2080 A\u2081 : Set} (A\u1d63 : A\u2080 \u2192 A\u2081 \u2192 Set\u2080)\n : Bot A\u2080 \u2192 Bot A\u2081 \u2192 Set\u2080 where\n \u27e6bot\u27e7 : (\u27e6Bot\u27e7 A\u1d63 \u27e6\u2192\u27e7 \u27e6Bot\u27e7 A\u1d63) bot bot\n\n\u27e6Bot\u27e7-env = record (\u03b5 2)\n { pDef = [ quote Bot \u2254 quote \u27e6Bot\u27e7 ] id\n ; pConP = [ quote bot \u2254 con (quote \u27e6bot\u27e7) ] con\n ; pConT = [ quote bot \u2254 conSkip' 3 (quote \u27e6bot\u27e7) ] con\n }\n\n\u27e6gobot\u27e71 : (\u2200\u27e8 A \u2236 \u27e6Set\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Bot\u27e7 A \u27e6\u2192\u27e7 A) gobot gobot\n\u27e6gobot\u27e71 {x0} {x1} x2 {._} {._} (\u27e6bot\u27e7 {x3} {x4} x5)\n = \u27e6gobot\u27e71 {x0} {x1} x2 {x3} {x4} x5\n\nunquoteDecl \u27e6gobot\u27e7 =\n param-rec-def-by-name \u27e6Bot\u27e7-env (quote gobot) \u27e6gobot\u27e7\n\nid\u2080 : {A : Set\u2080} \u2192 A \u2192 A\nid\u2080 x = x\n\n\u27e6id\u2080\u27e7 : (\u2200\u27e8 A \u2236 \u27e6Set\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 A \u27e6\u2192\u27e7 A) id\u2080 id\u2080\n\u27e6id\u2080\u27e7 = \u03bb {x\u2081} {x\u2082} x\u1d63 {x\u2083} {x\u2084} x\u1d63\u2081 \u2192 x\u1d63\u2081\n\ndata List\u2080 (A : Set) : Set where\n [] : List\u2080 A\n _\u2237_ : A \u2192 List\u2080 A \u2192 List\u2080 A\n\nmap\u2080 : \u2200 {A B} (f : A \u2192 B) (xs : List\u2080 A) \u2192 List\u2080 B\nmap\u2080 f [] = []\nmap\u2080 f (x \u2237 xs) = f x \u2237 map\u2080 f xs\n\nidList\u2080 : \u2200 {A} \u2192 List\u2080 A \u2192 List\u2080 A\n-- idList\u2080 : List\u2080 \u2115 \u2192 List\u2080 \u2115\nidList\u2080 [] = []\nidList\u2080 {A} (x \u2237 xs) = idList\u2080 {A} xs\n\ndata \u27e6List\u2080\u27e7 {A\u2080 A\u2081 : Set} (A\u1d63 : A\u2080 \u2192 A\u2081 \u2192 Set\u2080) : List\u2080 A\u2080 \u2192 List\u2080 A\u2081 \u2192 Set\u2080 where\n \u27e6[]\u27e7 : \u27e6List\u2080\u27e7 A\u1d63 [] []\n _\u27e6\u2237\u27e7_ : (A\u1d63 \u27e6\u2192\u27e7 \u27e6List\u2080\u27e7 A\u1d63 \u27e6\u2192\u27e7 \u27e6List\u2080\u27e7 A\u1d63) _\u2237_ _\u2237_\n\ncon\u27e6List\u2080\u27e7 = conSkip' 3\n\u27e6List\u2080\u27e7-env = record (\u03b5 2)\n { pDef = [ quote List\u2080 \u2254 quote \u27e6List\u2080\u27e7 ] id\n ; pConP = [ quote List\u2080.[] \u2254 con (quote \u27e6List\u2080\u27e7.\u27e6[]\u27e7) ]\n ([ quote List\u2080._\u2237_ \u2254 con (quote \u27e6List\u2080\u27e7._\u27e6\u2237\u27e7_) ]\n con)\n ; pConT = [ quote List\u2080.[] \u2254 con\u27e6List\u2080\u27e7 (quote \u27e6List\u2080\u27e7.\u27e6[]\u27e7) ]\n ([ quote List\u2080._\u2237_ \u2254 con\u27e6List\u2080\u27e7 (quote \u27e6List\u2080\u27e7._\u27e6\u2237\u27e7_) ]\n con)\n }\n\n\u27e6idList\u2080\u27e7 : unquote (unEl (param-type-by-name \u27e6List\u2080\u27e7-env (quote idList\u2080)))\n-- \u27e6idList\u2080\u27e7 : (\u2200\u27e8 A \u2236 \u27e6Set\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6List\u2080\u27e7 A \u27e6\u2192\u27e7 \u27e6List\u2080\u27e7 A) idList\u2080 idList\u2080\nunquoteDef \u27e6idList\u2080\u27e7 = param-rec-clauses-by-name \u27e6List\u2080\u27e7-env (quote idList\u2080) (quote \u27e6idList\u2080\u27e7)\n\n{-\n\u27e6map\u2080\u27e7 : {x0 : Set0} \u2192 {x1 : Set0} \u2192 (x2 : (x2 : x0) \u2192 (x3 : x1) \u2192 Set0) \u2192 {x3 : Set0} \u2192 {x4 : Set0} \u2192 (x5 : (x5 : x3) \u2192 (x6 : x4) \u2192 Set0) \u2192 {x6 : (x6 : x0) \u2192 x3} \u2192 {x7 : (x7 : x1) \u2192 x4} \u2192 (x8 : {x8 : x0} \u2192 {x9 : x1} \u2192 (x10 : x2 (x8) (x9)) \u2192 x5 (x6 (x8)) (x7 (x9))) \u2192 {x9 : List\u2080 (x0)} \u2192 {x10 : List\u2080 (x1)} \u2192 (x11 : \u27e6List\u2080\u27e7 {x0} {x1} (x2) (x9) (x10)) \u2192 \u27e6List\u2080\u27e7 {x3} {x4} (x5) (map\u2080 {x0} {x3} (x6) (x9)) (map\u2080 {x1} {x4} (x7) (x10))\n\u27e6map\u2080\u27e7 {x0} {x1} (x2) {x3} {x4} (x5) {x6} {x7} (x8) {._} {._} (\u27e6[]\u27e7 ) = \u27e6[]\u27e7\n\u27e6map\u2080\u27e7 {x0} {x1} (x2) {x3} {x4} (x5) {x6} {x7} (x8) {._} {._} (_\u27e6\u2237\u27e7_ {x11} {x12} (x13) {x14} {x15} (x16) ) = _\u27e6\u2237\u27e7_ {x6 (x11)} {x7 (x12)} (x8 {x11} {x12} (x13)) {map\u2080 {x0} {x3} (x6) (x14)} {map\u2080 {x1} {x4} (x7) (x15)} (\u27e6map\u2080\u27e7 {x0} {x1} (x2) {x3} {x4} (x5) {x6} {x7} (x8) {x14} {x15} (x16))\n-}\n\nunquoteDecl \u27e6map\u2080\u27e7\n = param-rec-def-by-name \u27e6List\u2080\u27e7-env (quote map\u2080) \u27e6map\u2080\u27e7\n\n{-\nmap-nat : \u2200 (f : \u2200 {X} \u2192 List\u2080 X \u2192 List\u2080 X)\n {A B : Set} (g : A \u2192 B)\n \u2192 f \u2218 map\u2080 g ~ map\u2080 g \u2218 f\nmap-nat f g x = {!\u27e6map\u2080\u27e7 _\u2261_ _\u2261_ {g}!}\n\n -- The generated type is bigger since it is a familly for no reason.\n data \u27e6List\u2080\u27e7 : (\u27e6Set\u2080\u27e7 \u27e6\u2192\u27e7 \u27e6Set\u2081\u27e7) List\u2080 List\u2080\n\n private\n \u27e6List\u2080\u27e7-ctor = \u03bb c \u2192 unEl (param-ctor-by-name (extDefEnv [ quote List\u2080 \u2254 quote \u27e6List\u2080\u27e7 ] (\u03b5 2)) c)\n\n data \u27e6List\u2080\u27e7 where\n \u27e6[]\u27e7 : unquote (\u27e6List\u2080\u27e7-ctor (quote List\u2080.[]))\n _\u27e6\u2237\u27e7_ : unquote (\u27e6List\u2080\u27e7-ctor (quote List\u2080._\u2237_))\n\n \u27e6List\u2080\u27e7-env = extConEnv ([ quote List\u2080.[] \u2254 quote \u27e6List\u2080\u27e7.\u27e6[]\u27e7 ] \u2218\n [ quote List\u2080._\u2237_ \u2254 quote \u27e6List\u2080\u27e7._\u27e6\u2237\u27e7_ ])\n (extDefEnv [ quote List\u2080 \u2254 quote \u27e6List\u2080\u27e7 ] (\u03b5 2))\n-}\n\ndata Maybe' (A : Set) : Set\u2081 where\n nothing : Maybe' A\n just : A \u2192 Maybe' A\n\n{-\n-- Set\u2081 is here because \u27e6List\u2080\u27e7 is not using parameters, hence gets bigger.\n-- This only happens without-K given the new rules for data types.\ndata List\u2080 : (A : Set) \u2192 Set\u2081 where\n [] : \u2200 {A} \u2192 List\u2080 A\n _\u2237_ : \u2200 {A} \u2192 A \u2192 List\u2080 A \u2192 List\u2080 A\n\nmap\u2080 : \u2200 {A B} \u2192 (A \u2192 B) \u2192 List\u2080 A \u2192 List\u2080 B\nmap\u2080 f [] = []\nmap\u2080 f (x \u2237 xs) = f x \u2237 map\u2080 f xs\n\nidList\u2080 : \u2200 {A} \u2192 List\u2080 A \u2192 List\u2080 A\nidList\u2080 [] = []\nidList\u2080 (x \u2237 xs) = x \u2237 idList\u2080 xs\n\n-- The generated type bigger since it is a familly for no reason.\ndata \u27e6List\u2080\u27e7 : (\u27e6Set\u2080\u27e7 \u27e6\u2192\u27e7 \u27e6Set\u2081\u27e7) List\u2080 List\u2080\n\nprivate\n \u27e6List\u2080\u27e7-ctor = \u03bb c \u2192 unEl (param-ctor-by-name (extDefEnv [ quote List\u2080 \u2254 quote \u27e6List\u2080\u27e7 ] (\u03b5 2)) c)\n\ndata \u27e6List\u2080\u27e7 where\n \u27e6[]\u27e7 : unquote (\u27e6List\u2080\u27e7-ctor (quote List\u2080.[]))\n _\u27e6\u2237\u27e7_ : unquote (\u27e6List\u2080\u27e7-ctor (quote List\u2080._\u2237_))\n\n\u27e6List\u2080\u27e7-env = extConEnv ([ quote List\u2080.[] \u2254 quote \u27e6List\u2080\u27e7.\u27e6[]\u27e7 ] \u2218\n [ quote List\u2080._\u2237_ \u2254 quote \u27e6List\u2080\u27e7._\u27e6\u2237\u27e7_ ])\n (extDefEnv [ quote List\u2080 \u2254 quote \u27e6List\u2080\u27e7 ] (\u03b5 2))\n\n-- test = \u27e6[]\u27e7 {{!showType (type (quote List\u2080.[]))!}} {{!!}} {!!}\n-}\n\n{-\n\u27e6idList\u2080\u27e7 : unquote (unEl (param-type-by-name \u27e6List\u2080\u27e7-env (quote idList\u2080)))\nunquoteDef \u27e6idList\u2080\u27e7 = param-rec-clauses-by-name \u27e6List\u2080\u27e7-env (quote idList\u2080) (quote \u27e6idList\u2080\u27e7)\n-}\n\n{-\n\u27e6map\u2080\u27e7 : unquote (unEl (param-type-by-name \u27e6List\u2080\u27e7-env (quote map\u2080)))\n\u27e6map\u2080\u27e7 {x} {x\u2081} {x\u2082} {x\u2083} {x\u2084} {x\u2085} {x\u2086} {x\u2087} x\u2088 {[]} {[]} \u27e6[]\u27e7\n = \u27e6[]\u27e7\n\u27e6map\u2080\u27e7 {x} {x\u2081} {x\u2082} {x\u2083} {x\u2084} {x\u2085} {x\u2086} {x\u2087} x\u2088 {._ \u2237 ._}\n {._ \u2237 ._}\n (_\u27e6\u2237\u27e7_ {x\u2081\u2083} {x\u2081\u2084} x\u2081\u2085 {x\u2081\u2086} {x\u2081\u2087} x\u2081\u2088)\n = _\u27e6\u2237\u27e7_ {x\u2081\u2080 x\u2081\u2083} {x\u2081\u2081 x\u2081\u2084} (x\u2081\u2082 {x\u2081\u2083} {x\u2081\u2084} x\u2081\u2085)\n {map\u2080 {x\u2084} {x\u2087} x\u2081\u2080 x\u2081\u2086} {map\u2080 {x\u2085} {x\u2088} x\u2081\u2081 x\u2081\u2087}\n (\u27e6map\u2080\u27e7 {x\u2084} {x\u2085} {x\u2086} {x\u2087} {x\u2088} {x\u2089} {x\u2081\u2080} {x\u2081\u2081} x\u2081\u2082 {x\u2081\u2086} {x\u2081\u2087}\n x\u2081\u2088)\n-}\n\n{-\nunquoteDef \u27e6map\u2080\u27e7 = param-rec-clauses-by-name \u27e6List\u2080\u27e7-env (quote map\u2080) (quote \u27e6map\u2080\u27e7)\n-}\n\n\n{-\nfoo : {x0 : Set0} \u2192 {x1 : Set0} \u2192 (x2 : (x2 : x0) \u2192 (x3 : x1) \u2192 Set0) \u2192 {x3 : Set0} \u2192 {x4 : Set0} \u2192 (x5 : (x5 : x3) \u2192 (x6 : x4) \u2192 Set0) \u2192 {x6 : (x6 : x0) \u2192 x3} \u2192 {x7 : (x7 : x1) \u2192 x4} \u2192 (x8 : {x8 : x0} \u2192 {x9 : x1} \u2192 (x10 : x2 (x8) (x9)) \u2192 x5 (x6 (x8)) (x7 (x9))) \u2192 {x9 : Reflection.Param.List\u2080 (x0)} \u2192 {x10 : Reflection.Param.List\u2080 (x1)} \u2192 (x11 : Reflection.Param.\u27e6List\u2080\u27e7 {x0} {x1} (x2) (x9) (x10)) \u2192 Reflection.Param.\u27e6List\u2080\u27e7 {x3} {x4} (x5) (Reflection.Param.map\u2080 {x0} {x3} (x6) (x9)) (Reflection.Param.map\u2080 {x1} {x4} (x7) (x10))\nfoo {A} {A} (A) {B} {B} (B) {f} {f} (f) {._} {._} (Reflection.Param.\u27e6List\u2080\u27e7.\u27e6[]\u27e7 ) = Reflection.Param.\u27e6List\u2080\u27e7.\u27e6[]\u27e7\nfoo {A} {A} (A) {B} {B} (B) {f} {f} (f) {._} {._} (Reflection.Param.\u27e6List\u2080\u27e7._\u27e6\u2237\u27e7_ {x} {x} (x) {xs} {xs} (xs) ) = Reflection.Param.\u27e6List\u2080\u27e7._\u27e6\u2237\u27e7_ {x0 (x0)} {x0 (x0)} (x0 {x0} {x0} (x0)) {Reflection.Param.map\u2080 {x0} {x0} (x0) (x0)} {Reflection.Param.map\u2080 {x0} {x0} (x0) (x0)} (Reflection.Param.test' {x0} {x0} (x0) {x0} {x0} (x0) {x0} {x0} (x0) {x0} {x0} (x0))\n-}\n\n-- test' = {! showFunDef \"foo\" (param-rec-def-by-name \u27e6List\u2080\u27e7-env (quote map\u2080) (quote test'))!}\n\nopen import Function.Param.Unary\n\nrevealed-[\u2192] = Reveal-args.n\u00e5me (quote _[\u2080\u2192\u2080]_)\n\nrevealed-[\u2192]' : \u2200 (A : Set\u2080) (A\u209a : A \u2192 Set\u2080)\n (B : Set\u2080) (B\u209a : B \u2192 Set\u2080)\n (f : A \u2192 B) \u2192 Set\u2080\nunquoteDef revealed-[\u2192]' = Get-clauses.from-def revealed-[\u2192]\n\nrevelator-[\u2192] : ({A : Set} (A\u209a : A \u2192 Set) {B : Set} (B\u209a : B \u2192 Set) (f : A \u2192 B) \u2192 Set)\n \u2192 (A : Set) (A\u209a : A \u2192 Set) (B : Set) (B\u209a : B \u2192 Set) (f : A \u2192 B) \u2192 Set\nunquoteDef revelator-[\u2192] = Revelator.clauses (type (quote _[\u2080\u2192\u2080]_))\n\np-[\u2192]-type = param-type-by-name (\u03b5 1) (quote _[\u2080\u2192\u2080]_)\np-[\u2192] = param-clauses-by-name (\u03b5 1) (quote _[\u2080\u2192\u2080]_)\n\np-[\u2192]' = \u2200 {A : Set\u2080} (A\u2080\u209a : A \u2192 Set\u2080)\n {A\u209a : A \u2192 Set\u2080} (A\u2081\u209a : {x : A} \u2192 A\u2080\u209a x \u2192 A\u209a x \u2192 Set\u2080)\n {B : Set\u2080} (B\u2080\u209a : B \u2192 Set\u2080)\n {B\u209a : B \u2192 Set\u2080} (B\u2081\u209a : {x : B} \u2192 B\u2080\u209a x \u2192 B\u209a x \u2192 Set\u2080)\n {f : A \u2192 B} (f\u209a : {x : A} \u2192 A\u2080\u209a x \u2192 B\u2080\u209a (f x))\n \u2192 (A\u209a [\u2080\u2192\u2080] B\u209a) f\n \u2192 Set\n\np-[\u2192]'-test : p-[\u2192]' \u2261 unquote (unEl p-[\u2192]-type)\np-[\u2192]'-test = refl\n\n[[\u2192]] : unquote (unEl p-[\u2192]-type)\nunquoteDef [[\u2192]] = p-[\u2192]\n\ndata [[\u2115]] : [[Set\u2080]] [\u2115] [\u2115] where\n [[zero]] : [[\u2115]] [zero] [zero]\n [[suc]] : [[\u2192]] [\u2115] [[\u2115]] [\u2115] [[\u2115]] [suc] [suc]\n\n_\/2 : \u2115 \u2192 \u2115\nzero \/2 = zero\nsuc zero \/2 = zero\nsuc (suc n) \/2 = suc (n \/2)\n\n_\u27e6\/2\u27e7 : (\u27e6\u2115\u27e7 \u27e6\u2080\u2192\u2080\u27e7 \u27e6\u2115\u27e7) _\/2 _\/2\n\u27e6zero\u27e7 \u27e6\/2\u27e7 = \u27e6zero\u27e7\n\u27e6suc\u27e7 \u27e6zero\u27e7 \u27e6\/2\u27e7 = \u27e6zero\u27e7\n\u27e6suc\u27e7 (\u27e6suc\u27e7 n) \u27e6\/2\u27e7 = \u27e6suc\u27e7 (n \u27e6\/2\u27e7)\n\n_+\u2115_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero +\u2115 n = n\nsuc m +\u2115 n = suc (m +\u2115 n)\n\npred' : \u2115 \u2192 \u2115\npred' = \u03bb { zero \u2192 zero\n ; (suc m) \u2192 m }\n\n\u27e6pred'\u27e7 : (\u27e6\u2115\u27e7 \u27e6\u2192\u27e7 \u27e6\u2115\u27e7) pred' pred'\nunquoteDef \u27e6pred'\u27e7 = param-clauses-by-name defEnv2 (quote pred')\n\n_\u27e6+\u2115\u27e7_ : (\u27e6\u2115\u27e7 \u27e6\u2080\u2192\u2080\u27e7 \u27e6\u2115\u27e7 \u27e6\u2080\u2192\u2080\u27e7 \u27e6\u2115\u27e7) _+\u2115_ _+\u2115_\n\u27e6zero\u27e7 \u27e6+\u2115\u27e7 n = n\n\u27e6suc\u27e7 m \u27e6+\u2115\u27e7 n = \u27e6suc\u27e7 (m \u27e6+\u2115\u27e7 n)\n\n\u27e6\u27e6Set\u2080\u27e7\u27e7 : (\u27e6Set\u2080\u27e7 \u27e6\u2081\u2192\u2082\u27e7 \u27e6Set\u2080\u27e7 \u27e6\u2081\u2192\u2082\u27e7 \u27e6Set\u2081\u27e7) \u27e6Set\u2080\u27e7 \u27e6Set\u2080\u27e7\n\u27e6\u27e6Set\u2080\u27e7\u27e7 = \u03bb A\u2080 A\u2081 \u2192 A\u2080 \u27e6\u2080\u2192\u2081\u27e7 A\u2081 \u27e6\u2080\u2192\u2081\u27e7 \u27e6Set\u2080\u27e7\n\n\u27e6\u27e6Set\u2080\u27e7\u27e7' : {x\u2081 x\u2082 : Set} (x\u1d63 : x\u2081 \u2192 x\u2082 \u2192 Set) {x\u2083 : Set} {x\u2084 : Set}\n (x\u1d63\u2081 : x\u2083 \u2192 x\u2084 \u2192 Set) \u2192\n (x\u2081 \u2192 x\u2083 \u2192 Set) \u2192 (x\u2082 \u2192 x\u2084 \u2192 Set) \u2192 Set\u2081\n\u27e6\u27e6Set\u2080\u27e7\u27e7' = \u03bb A\u2080 A\u2081 f\u2081 f\u2082 \u2192\n \u2200 {x\u2081} {x\u2082} (x\u1d63 : A\u2080 x\u2081 x\u2082)\n {x\u2083} {x\u2084} (x\u1d63\u2081 : A\u2081 x\u2083 x\u2084) \u2192\n f\u2081 x\u2081 x\u2083 \u2192 f\u2082 x\u2082 x\u2084 \u2192 Set\n\n-- Since quoteTerm normalises\ntest-\u27e6\u27e6Set\u2080\u27e7\u27e7 : quoteTerm \u27e6\u27e6Set\u2080\u27e7\u27e7 \u2261-no-hints quoteTerm \u27e6\u27e6Set\u2080\u27e7\u27e7'\ntest-\u27e6\u27e6Set\u2080\u27e7\u27e7 = refl\n\n\u27e6\u27e6Set\u2080\u27e7\u27e7-type = unquote (unEl (type (quote \u27e6\u27e6Set\u2080\u27e7\u27e7)))\ntest-\u27e6\u27e6Set\u2080\u27e7\u27e7-type : \u27e6\u27e6Set\u2080\u27e7\u27e7-type \u2261 unquote (unEl (type (quote \u27e6\u27e6Set\u2080\u27e7\u27e7')))\ntest-\u27e6\u27e6Set\u2080\u27e7\u27e7-type = refl\n\npSet\u2080 = pTerm defEnv2 `\u2605\u2080\nppSet\u2080 = pTerm defEnv2 pSet\u2080\np\u27e6Set\u2080\u27e7 = param-clauses-by-name defEnv2 (quote \u27e6Set\u2080\u27e7)\ntest-pSet\u2080 : pSet\u2080 \u2261-no-hints quoteTerm \u27e6Set\u2080\u27e7\ntest-pSet\u2080 = refl\ntest-ppSet\u2080 : ppSet\u2080 \u2261-no-hints quoteTerm \u27e6\u27e6Set\u2080\u27e7\u27e7\ntest-ppSet\u2080 = refl\ntest-ppSet\u2080'' : ppSet\u2080 \u2261-no-hints Get-term.from-clauses p\u27e6Set\u2080\u27e7\ntest-ppSet\u2080'' = refl\n\n\u27e6\u27e6Set\u2080\u27e7\u27e7'' : (\u27e6Set\u2080\u27e7 \u27e6\u2081\u2192\u2082\u27e7 \u27e6Set\u2080\u27e7 \u27e6\u2081\u2192\u2082\u27e7 \u27e6Set\u2081\u27e7) \u27e6Set\u2080\u27e7 \u27e6Set\u2080\u27e7\nunquoteDef \u27e6\u27e6Set\u2080\u27e7\u27e7'' = p\u27e6Set\u2080\u27e7\n\ntest-\u27e6\u27e6Set\u2080\u27e7\u27e7'' : _\u2261_ {A = \u27e6\u27e6Set\u2080\u27e7\u27e7-type} \u27e6\u27e6Set\u2080\u27e7\u27e7'' \u27e6\u27e6Set\u2080\u27e7\u27e7\ntest-\u27e6\u27e6Set\u2080\u27e7\u27e7'' = refl\n\ntest-p0-\u27e6Set\u2080\u27e7 : pTerm defEnv0 (quoteTerm \u27e6Set\u2080\u27e7) \u2261-no-hints quoteTerm \u27e6Set\u2080\u27e7\ntest-p0-\u27e6Set\u2080\u27e7 = refl\n\ndata \u27e6\u27e6\ud835\udfda\u27e7\u27e7 : (\u27e6\u27e6Set\u2080\u27e7\u27e7 \u27e6\ud835\udfda\u27e7 \u27e6\ud835\udfda\u27e7) \u27e6\ud835\udfda\u27e7 \u27e6\ud835\udfda\u27e7 where\n \u27e6\u27e60\u2082\u27e7\u27e7 : \u27e6\u27e6\ud835\udfda\u27e7\u27e7 \u27e60\u2082\u27e7 \u27e60\u2082\u27e7 \u27e60\u2082\u27e7 \u27e60\u2082\u27e7\n \u27e6\u27e61\u2082\u27e7\u27e7 : \u27e6\u27e6\ud835\udfda\u27e7\u27e7 \u27e61\u2082\u27e7 \u27e61\u2082\u27e7 \u27e61\u2082\u27e7 \u27e61\u2082\u27e7\n\np1\u2115\u2192\u2115 = pTerm defEnv1 (quoteTerm (\u2115 \u2192 \u2115))\n[\u2115\u2192\u2115] = [\u2115] [\u2192] [\u2115]\ntest-p1\u2115\u2192\u2115 : unquote p1\u2115\u2192\u2115 \u2261 [\u2115\u2192\u2115]\ntest-p1\u2115\u2192\u2115 = refl\n\np2\u2115\u2192\u2115 = pTerm defEnv2 (quoteTerm (\u2115 \u2192 \u2115))\n\u27e6\u2115\u2192\u2115\u27e7 = \u27e6\u2115\u27e7 \u27e6\u2192\u27e7 \u27e6\u2115\u27e7\ntest-p2\u2115\u2192\u2115 : unquote p2\u2115\u2192\u2115 \u2261 \u27e6\u2115\u2192\u2115\u27e7\ntest-p2\u2115\u2192\u2115 = refl\n\np\u2115\u2192\u2115\u2192\u2115 = pTerm defEnv2 (quoteTerm (\u2115 \u2192 \u2115 \u2192 \u2115))\n\u27e6\u2115\u2192\u2115\u2192\u2115\u27e7 = \u27e6\u2115\u27e7 \u27e6\u2192\u27e7 \u27e6\u2115\u27e7 \u27e6\u2192\u27e7 \u27e6\u2115\u27e7\ntest-p\u2115\u2192\u2115\u2192\u2115 : p\u2115\u2192\u2115\u2192\u2115 \u2261-no-hints quoteTerm \u27e6\u2115\u2192\u2115\u2192\u2115\u27e7\ntest-p\u2115\u2192\u2115\u2192\u2115 = refl\nZERO : Set\u2081\nZERO = (A : Set\u2080) \u2192 A\n\u27e6ZERO\u27e7 : ZERO \u2192 ZERO \u2192 Set\u2081\n\u27e6ZERO\u27e7 f\u2080 f\u2081 =\n {A\u2080 A\u2081 : Set\u2080} (A\u1d63 : A\u2080 \u2192 A\u2081 \u2192 Set\u2080)\n \u2192 A\u1d63 (f\u2080 A\u2080) (f\u2081 A\u2081)\npZERO = pTerm (\u03b5 2) (quoteTerm ZERO)\nq\u27e6ZERO\u27e7 = quoteTerm \u27e6ZERO\u27e7\ntest-pZERO : pZERO \u2261-no-hints q\u27e6ZERO\u27e7\ntest-pZERO = refl\nID : Set\u2081\nID = (A : Set\u2080) \u2192 A \u2192 A\n\u27e6ID\u27e7 : ID \u2192 ID \u2192 Set\u2081\n\u27e6ID\u27e7 f\u2080 f\u2081 =\n {A\u2080 A\u2081 : Set\u2080} (A\u1d63 : A\u2080 \u2192 A\u2081 \u2192 Set\u2080)\n {x\u2080 : A\u2080} {x\u2081 : A\u2081} (x : A\u1d63 x\u2080 x\u2081)\n \u2192 A\u1d63 (f\u2080 A\u2080 x\u2080) (f\u2081 A\u2081 x\u2081)\npID = pTerm (\u03b5 2) (quoteTerm ID)\nq\u27e6ID\u27e7 = quoteTerm \u27e6ID\u27e7\ntest-ID : q\u27e6ID\u27e7 \u2261-no-hints pID\ntest-ID = refl\n\n\u27e6not\u27e7' : (\u27e6\ud835\udfda\u27e7 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) not not\nunquoteDef \u27e6not\u27e7' = param-clauses-by-name defEnv2 (quote not)\ntest-not : \u2200 {x\u2080 x\u2081 : \ud835\udfda} (x\u1d63 : \u27e6\ud835\udfda\u27e7 x\u2080 x\u2081) \u2192 \u27e6not\u27e7 x\u1d63 \u2261 \u27e6not\u27e7' x\u1d63\ntest-not \u27e60\u2082\u27e7 = refl\ntest-not \u27e61\u2082\u27e7 = refl\n\n[pred]' : ([\u2115] [\u2192] [\u2115]) pred\nunquoteDef [pred]' = param-clauses-by-name defEnv1 (quote pred)\n\ntest-p1-pred : \u2200 {n} (n\u209a : [\u2115] n) \u2192 [pred]' n\u209a \u2261 [pred] n\u209a\ntest-p1-pred [zero] = refl\ntest-p1-pred ([suc] n\u209a) = refl\n\n\u27e6pred\u27e7' : (\u27e6\u2115\u27e7 \u27e6\u2192\u27e7 \u27e6\u2115\u27e7) pred pred\nunquoteDef \u27e6pred\u27e7' = param-clauses-by-name defEnv2 (quote pred)\n\ntest-p2-pred : \u2200 {n\u2080 n\u2081} (n\u1d63 : \u27e6\u2115\u27e7 n\u2080 n\u2081) \u2192 \u27e6pred\u27e7' n\u1d63 \u2261 \u27e6pred\u27e7 n\u1d63\ntest-p2-pred \u27e6zero\u27e7 = refl\ntest-p2-pred (\u27e6suc\u27e7 n\u1d63) = refl\n\np\/2 = param-rec-def-by-name defEnv2 (quote _\/2)\nq\u27e6\/2\u27e7 = definition (quote _\u27e6\/2\u27e7)\nunquoteDecl _\u27e6\/2\u27e7' = p\/2 _\u27e6\/2\u27e7'\ntest-\/2 : function (p\/2 (quote _\u27e6\/2\u27e7)) \u2261-def-no-hints q\u27e6\/2\u27e7\ntest-\/2 = refl\ntest-\/2' : \u2200 {n\u2080 n\u2081} (n\u1d63 : \u27e6\u2115\u27e7 n\u2080 n\u2081) \u2192 n\u1d63 \u27e6\/2\u27e7' \u2261 n\u1d63 \u27e6\/2\u27e7\ntest-\/2' \u27e6zero\u27e7 = refl\ntest-\/2' (\u27e6suc\u27e7 \u27e6zero\u27e7) = refl\ntest-\/2' (\u27e6suc\u27e7 (\u27e6suc\u27e7 n\u1d63)) rewrite test-\/2' n\u1d63 = refl\n\np+ = param-rec-def-by-name defEnv2 (quote _+\u2115_)\nq\u27e6+\u27e7 = definition (quote _\u27e6+\u2115\u27e7_)\nunquoteDecl _\u27e6+\u27e7'_ = p+ _\u27e6+\u27e7'_\ntest-+ : function (p+ (quote _\u27e6+\u2115\u27e7_)) \u2261-def-no-hints q\u27e6+\u27e7\ntest-+ = refl\ntest-+' : \u2200 {n\u2080 n\u2081} (n\u1d63 : \u27e6\u2115\u27e7 n\u2080 n\u2081) {n'\u2080 n'\u2081} (n'\u1d63 : \u27e6\u2115\u27e7 n'\u2080 n'\u2081) \u2192 n\u1d63 \u27e6+\u27e7' n'\u1d63 \u2261 n\u1d63 \u27e6+\u2115\u27e7 n'\u1d63\ntest-+' \u27e6zero\u27e7 n'\u1d63 = refl\ntest-+' (\u27e6suc\u27e7 n\u1d63) n'\u1d63 rewrite test-+' n\u1d63 n'\u1d63 = refl\n\n{-\nis-good : String \u2192 \ud835\udfda\nis-good \"good\" = 1\u2082\nis-good _ = 0\u2082\n\n\u27e6is-good\u27e7 : (\u27e6String\u27e7 \u27e6\u2080\u2192\u2080\u27e7 \u27e6\ud835\udfda\u27e7) is-good is-good\n\u27e6is-good\u27e7 {\"good\"} refl = \u27e61\u2082\u27e7\n\u27e6is-good\u27e7 {_} refl = {!!}\n\nmy-good = unquote (lit (string \"good\"))\nmy-good-test : my-good \u2261 \"good\"\nmy-good-test = refl\n-}\n\n{-\n\u27e6is-good\u27e7' : (\u27e6String\u27e7 \u27e6\u2080\u2192\u2080\u27e7 \u27e6\ud835\udfda\u27e7) is-good is-good\nunquoteDef \u27e6is-good\u27e7' = param-clauses-by-name defEnv2 (quote is-good)\ntest-is-good = {!!}\n-}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS -vtc.unquote.decl:20 -vtc.unquote.def:20 #-}\n{-# OPTIONS --without-K #-}\nopen import Level hiding (zero; suc)\nopen import Data.Unit renaming (\u22a4 to \ud835\udfd9; tt to 0\u2081)\nopen import Data.Bool\n using (not)\n renaming (Bool to \ud835\udfda; false to 0\u2082; true to 1\u2082)\nopen import Data.String.Core using (String)\nopen import Data.Float using (Float)\nopen import Function\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Nat hiding (_\u225f_)\nopen import Data.List using (List; []; _\u2237_)\nopen import Relation.Binary.PropositionalEquality\n using (_\u2261_; refl) renaming (_\u2257_ to _~_)\n\nopen import Function.Param.Unary\nopen import Function.Param.Binary\nopen import Type.Param.Unary\nopen import Type.Param.Binary\nopen import Data.Two.Param.Binary\nopen import Data.Nat.Param.Binary\nopen import Reflection.NP\nopen import Reflection.Param\nopen import Reflection.Param.Env\n\nmodule Reflection.Param.Tests where\n\nimport Reflection.Printer as Pr\nopen Pr using (var;con;def;lam;pi;sort;unknown;showTerm;showType;showDef;showFunDef;showClauses)\n\n-- Local \"imports\" to avoid depending on nplib\nprivate\n postulate\n opaque : \u2200 {a b} {A : Set a} {B : Set b} \u2192 A \u2192 B \u2192 B\n -- opaque-rule : \u2200 {x} y \u2192 opaque x y \u2261 y\n\n \u2605\u2080 = Set\u2080\n \u2605\u2081 = Set\u2081\n\ninfixr 1 _[\u2080\u2192\u2080]_\n_[\u2080\u2192\u2080]_ : \u2200 {A : Set\u2080} (A\u209a : A \u2192 Set\u2080)\n {B : Set\u2080} (B\u209a : B \u2192 Set\u2080)\n (f : A \u2192 B) \u2192 Set\u2080\n_[\u2080\u2192\u2080]_ = \u03bb {A} A\u209a {B} B\u209a f \u2192 \u2200 {a} (a\u209a : A\u209a a) \u2192 B\u209a (f a)\n\ninfixr 1 _[\u2080\u2192\u2081]_\n_[\u2080\u2192\u2081]_ : \u2200 {A : Set\u2080} (A\u209a : A \u2192 Set\u2080)\n {B : Set\u2081} (B\u209a : B \u2192 Set\u2081)\n (f : A \u2192 B) \u2192 Set\u2081\n_[\u2080\u2192\u2081]_ = \u03bb {A} A\u209a {B} B\u209a f \u2192 \u2200 {a} (a\u209a : A\u209a a) \u2192 B\u209a (f a)\n\ninfixr 1 _[\u2081\u2192\u2081]_\n_[\u2081\u2192\u2081]_ : \u2200 {A : Set\u2081} (A\u209a : A \u2192 Set\u2081)\n {B : Set\u2081} (B\u209a : B \u2192 Set\u2081)\n (f : A \u2192 B) \u2192 Set\u2081\n_[\u2081\u2192\u2081]_ = \u03bb {A} A\u209a {B} B\u209a f \u2192 \u2200 {a} (a\u209a : A\u209a a) \u2192 B\u209a (f a)\n\ninfixr 1 _[\u2081\u2192\u2082]_\n_[\u2081\u2192\u2082]_ : \u2200 {A : Set\u2081} (A\u209a : A \u2192 Set\u2081)\n {B : Set\u2082} (B\u209a : B \u2192 Set\u2082)\n (f : A \u2192 B) \u2192 Set\u2082\n_[\u2081\u2192\u2082]_ = \u03bb {A} A\u209a {B} B\u209a f \u2192 \u2200 {a} (a\u209a : A\u209a a) \u2192 B\u209a (f a)\n\n[[Set\u2080]] : ([Set\u2080] [\u2081\u2192\u2082] [Set\u2081]) [Set\u2080]\n[[Set\u2080]] = \u03bb A \u2192 A [\u2080\u2192\u2081] [Set\u2080]\n\n{-\nEqEnv = {!!}\nEqRes = {!!}\n\neqTerm : EqEnv \u2192 Term \u2192 Term \u2192 EqRes\neqTerm \u0393 (var x args) (var x\u2081 args\u2081) = {!!}\neqTerm \u0393 (def f\u2080 args\u2080) (def f\u2081 args\u2081) = {!!}\neqTerm \u0393 (con c\u2080 args\u2080) (con c\u2081 args\u2081) = {!!}\neqTerm \u0393 (lam v t) (lam v' t') = {!!}\neqTerm \u0393 (pi t\u2081 t\u2082) (pi u\u2081 u\u2082) = {!!}\neqTerm \u0393 (sort s\u2080) (sort s\u2081) = {!!}\neqTerm \u0393 (lit l\u2080) (lit l\u2081) = {!!}\neqTerm \u0393 unknown unknown = {!!}\neqTerm \u0393 (def f args) u = {!!}\n--eqTerm \u0393 (pat-lam cs args) u = {!!}\neqTerm _ _ = ?\n-}\n\n{-\nimport Reflection.Simple as Si\nopen Si using (var;con;def;lam;pi;sort;unknown;simple;showTerm)\n-}\n\n_\u2261-no-hints_ : Term \u2192 Term \u2192 Set\nt \u2261-no-hints u = noHintsTerm t \u2261 noHintsTerm u\n\n_\u2261-def-no-hints_ : Definition \u2192 Definition \u2192 Set\nt \u2261-def-no-hints u = noHintsDefinition t \u2261 noHintsDefinition u\n\np[Set\u2080]-type = param-type-by-name (\u03b5 1) (quote [Set\u2080])\np[Set\u2080] = param-clauses-by-name (\u03b5 1) (quote [Set\u2080])\nq[[Set\u2080]] = definition (quote [[Set\u2080]]) -- quoteTerm [[Set\u2080]]\ntest-type-p[Set\u2080] : ([Set\u2080] [\u2081\u2192\u2082] [Set\u2081]) [Set\u2080] \u2261 unquote (unEl p[Set\u2080]-type)\ntest-type-p[Set\u2080] = refl\ntest-term-p[Set\u2080] : quoteTerm [[Set\u2080]] \u2261-no-hints Get-term.from-clauses p[Set\u2080]\ntest-term-p[Set\u2080] = refl\nu-p[Set\u2080] : ([Set\u2080] [\u2081\u2192\u2082] [Set\u2081]) [Set\u2080]\nunquoteDef u-p[Set\u2080] = p[Set\u2080]\n\nFalse : Set\u2081\nFalse = (A : Set) \u2192 A\n\nparam1-False-type = param-type-by-name (\u03b5 1) (quote False)\nparam1-False-term = param-term-by-name (\u03b5 1) (quote False)\n\nparam1-False-type-check : [Set\u2081] False \u2261 unquote (unEl param1-False-type)\nparam1-False-type-check = refl\n\n[False] : unquote (unEl param1-False-type)\n[False] = unquote param1-False-term\n\n[Level] : [Set\u2080] Level\n[Level] _ = \ud835\udfd9\n\n[String] : [Set\u2080] String\n[String] _ = \ud835\udfd9\n\n[Float] : [Set\u2080] Float\n[Float] _ = \ud835\udfd9\n\n-- Levels are parametric, hence the relation is total\n\u27e6Level\u27e7 : \u27e6Set\u2080\u27e7 Level Level\n\u27e6Level\u27e7 _ _ = \ud835\udfd9\n\n\u27e6String\u27e7 : \u27e6Set\u2080\u27e7 String String\n\u27e6String\u27e7 = _\u2261_\n\n\u27e6Float\u27e7 : \u27e6Set\u2080\u27e7 Float Float\n\u27e6Float\u27e7 = _\u2261_\n\ndata [\ud835\udfda] : [Set\u2080] \ud835\udfda where\n [0\u2082] : [\ud835\udfda] 0\u2082\n [1\u2082] : [\ud835\udfda] 1\u2082\n\ndata [\u2115] : [Set\u2080] \u2115 where\n [zero] : [\u2115] zero\n [suc] : ([\u2115] [\u2192] [\u2115]) suc\n\n[pred] : ([\u2115] [\u2192] [\u2115]) pred\n[pred] [zero] = [zero]\n[pred] ([suc] x\u209a) = x\u209a\n\ndefDefEnv1 : Name \u2192 Name\ndefDefEnv1 (quote \ud835\udfda) = quote [\ud835\udfda]\ndefDefEnv1 (quote \u2115) = quote [\u2115]\ndefDefEnv1 (quote String) = quote [String]\ndefDefEnv1 (quote Float) = quote [Float]\ndefDefEnv1 (quote \u2605\u2080) = quote [Set\u2080]\ndefDefEnv1 (quote \u2605\u2081) = quote [Set\u2081]\ndefDefEnv1 (quote False) = quote [False]\ndefDefEnv1 (quote Level) = quote [Level]\ndefDefEnv1 n = opaque \"defDefEnv1\" n\n\ndefConEnv1 : Name \u2192 Name\ndefConEnv1 (quote 0\u2082) = quote [0\u2082]\ndefConEnv1 (quote 1\u2082) = quote [1\u2082]\ndefConEnv1 (quote \u2115.zero) = quote [zero]\ndefConEnv1 (quote \u2115.suc) = quote [suc]\ndefConEnv1 (quote Level.zero) = quote 0\u2081\ndefConEnv1 (quote Level.suc) = quote 0\u2081\ndefConEnv1 n = opaque \"defConEnv1\" n\n\ndefDefEnv2 : Name \u2192 Name\ndefDefEnv2 (quote \ud835\udfda) = quote \u27e6\ud835\udfda\u27e7\ndefDefEnv2 (quote \u2115) = quote \u27e6\u2115\u27e7\ndefDefEnv2 (quote \u2605\u2080) = quote \u27e6Set\u2080\u27e7\ndefDefEnv2 (quote \u2605\u2081) = quote \u27e6Set\u2081\u27e7\ndefDefEnv2 (quote String) = quote \u27e6String\u27e7\ndefDefEnv2 (quote Float) = quote \u27e6Float\u27e7\ndefDefEnv2 (quote Level) = quote \u27e6Level\u27e7\ndefDefEnv2 n = opaque \"defDefEnv\" n\n\ndefConEnv2 : Name \u2192 Name\ndefConEnv2 (quote 0\u2082) = quote \u27e60\u2082\u27e7\ndefConEnv2 (quote 1\u2082) = quote \u27e61\u2082\u27e7\ndefConEnv2 (quote \u2115.zero) = quote \u27e6\u2115\u27e7.\u27e6zero\u27e7\ndefConEnv2 (quote \u2115.suc) = quote \u27e6\u2115\u27e7.\u27e6suc\u27e7\ndefConEnv2 (quote Level.zero) = quote 0\u2081\ndefConEnv2 (quote Level.suc) = quote 0\u2081\ndefConEnv2 n = opaque \"defConEnv2\" n\n\ndefEnv0 : Env' 0\ndefEnv0 = record (\u03b5 0)\n { pConT = con\n ; pConP = con\n ; pDef = id }\n\ndefEnv1 : Env' 1\ndefEnv1 = record (\u03b5 1)\n { pConP = con \u2218\u2032 defConEnv1\n ; pConT = con \u2218\u2032 defConEnv1\n ; pDef = defDefEnv1 }\n\ndefEnv2 : Env' 2\ndefEnv2 = record (\u03b5 2)\n { pConP = con \u2218\u2032 defConEnv2\n ; pConT = con \u2218\u2032 defConEnv2\n ; pDef = defDefEnv2 }\n\nparam1-[False]-type = param-type-by-name defEnv1 (quote [False])\nparam1-[False]-term = param-term-by-name defEnv1 (quote [False])\n\n{-\nmodule Const where\n postulate\n A : Set\u2080\n A\u1d63 : A \u2192 A \u2192 Set\u2080\n data Wrapper : Set where\n wrap : A \u2192 Wrapper\n\n idWrapper : Wrapper \u2192 Wrapper\n idWrapper (wrap x) = wrap x\n\n data \u27e6Wrapper\u27e7 : Wrapper \u2192 Wrapper \u2192 Set\u2080 where\n \u27e6wrap\u27e7 : (A\u1d63 \u27e6\u2192\u27e7 \u27e6Wrapper\u27e7) wrap wrap\n\n wrapperEnv = record (\u03b5 2)\n { pDef = [ quote Wrapper \u2254 quote \u27e6Wrapper\u27e7 ] id\n ; pConP = [ quote Wrapper.wrap \u2254 con (quote \u27e6Wrapper\u27e7.\u27e6wrap\u27e7) ] con\n ; pConT = [ quote Wrapper.wrap \u2254 con (quote \u27e6Wrapper\u27e7.\u27e6wrap\u27e7) ] con\n }\n\n unquoteDecl \u27e6idWrapper\u27e7 = param-rec-def-by-name wrapperEnv (quote idWrapper) \u27e6idWrapper\u27e7\n-}\n\ndata Wrapper (A : Set\u2080) : Set\u2080 where\n wrap : A \u2192 Wrapper A\n\nidWrapper : \u2200 {A} \u2192 Wrapper A \u2192 Wrapper A\nidWrapper (wrap x) = wrap x\n\ndata [Wrapper] {A : Set} (A\u209a : A \u2192 Set\u2080)\n : Wrapper A \u2192 Set\u2080 where\n [wrap] : (A\u209a [\u2192] [Wrapper] A\u209a) wrap\n\n[Wrapper]-env = record (\u03b5 1)\n { pDef = [ quote Wrapper \u2254 quote [Wrapper] ] id\n ; pConP = [ quote wrap \u2254 con (quote [wrap]) ] con\n ; pConT = [ quote wrap \u2254 conSkip' 2 (quote [wrap]) ] con\n }\n\nunquoteDecl [idWrapper] =\n param-rec-def-by-name [Wrapper]-env (quote idWrapper) [idWrapper]\n\n {-\n[idWrapper] = {! param-rec-def-by-name [Wrapper]-env (quote idWrapper) [idWrapper]!}\n-- [idWrapper] = {! param-rec-def-by-name [Wrapper]-env (quote idWrapper) [idWrapper]!}\n-}\n\n{-\n-- The generated type bigger since it is a familly for no reason.\ndata \u27e6Wrapper\u27e7 : (\u27e6Set\u2080\u27e7 \u27e6\u2192\u27e7 \u27e6Set\u2081\u27e7) Wrapper Wrapper\n\nprivate\n \u27e6Wrapper\u27e7-ctor = \u03bb c \u2192 unEl (param-ctor-by-name (extDefEnv [ quote Wrapper \u2254 quote \u27e6Wrapper\u27e7 ] (\u03b5 2)) c)\n\ndata \u27e6Wrapper\u27e7 where\n \u27e6wrap\u27e7 : unquote (\u27e6Wrapper\u27e7-ctor (quote Wrapper.wrap))\n-}\ndata \u27e6Wrapper\u27e7 {A\u2080 A\u2081 : Set} (A\u1d63 : A\u2080 \u2192 A\u2081 \u2192 Set\u2080)\n : Wrapper A\u2080 \u2192 Wrapper A\u2081 \u2192 Set\u2080 where\n \u27e6wrap\u27e7 : (A\u1d63 \u27e6\u2192\u27e7 \u27e6Wrapper\u27e7 A\u1d63) wrap wrap\n\n\u27e6Wrapper\u27e7-env = record (\u03b5 2)\n { pDef = [ quote Wrapper \u2254 quote \u27e6Wrapper\u27e7 ] id\n ; pConP = [ quote wrap \u2254 con (quote \u27e6wrap\u27e7) ] con\n ; pConT = [ quote wrap \u2254 conSkip' 3 (quote \u27e6wrap\u27e7) ] con\n }\n\n\u27e6idWrapper\u27e71 : (\u2200\u27e8 A \u2236 \u27e6Set\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Wrapper\u27e7 A \u27e6\u2192\u27e7 \u27e6Wrapper\u27e7 A) idWrapper idWrapper\n\u27e6idWrapper\u27e71 {x0} {x1} (x2) {._} {._} (\u27e6wrap\u27e7 {x3} {x4} x5)\n = \u27e6wrap\u27e7 {_} {_} {_} {x3} {x4} x5\n\n\u27e6idWrapper\u27e7-clauses =\n clause\n (arg (arg-info hidden relevant) (var \"A0\") \u2237\n arg (arg-info hidden relevant) (var \"A1\") \u2237\n arg (arg-info visible relevant) (var \"Ar\") \u2237\n arg (arg-info hidden relevant) dot \u2237\n arg (arg-info hidden relevant) dot \u2237\n arg (arg-info visible relevant)\n (con (quote \u27e6wrap\u27e7)\n (arg (arg-info hidden relevant) (var \"x0\") \u2237\n arg (arg-info hidden relevant) (var \"x1\") \u2237\n arg (arg-info visible relevant) (var \"xr\") \u2237 []))\n \u2237 [])\n (con (quote \u27e6wrap\u27e7)\n (arg (arg-info hidden relevant) unknown \u2237\n arg (arg-info hidden relevant) unknown \u2237\n arg (arg-info hidden relevant) unknown \u2237\n arg (arg-info hidden relevant) (var 2 []) \u2237\n arg (arg-info hidden relevant) (var 1 []) \u2237\n arg (arg-info visible relevant) (var 0 []) \u2237 []))\n \u2237 []\n\n\u27e6idWrapper\u27e72 : (\u2200\u27e8 A \u2236 \u27e6Set\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Wrapper\u27e7 A \u27e6\u2192\u27e7 \u27e6Wrapper\u27e7 A) idWrapper idWrapper\nunquoteDef \u27e6idWrapper\u27e72 = \u27e6idWrapper\u27e7-clauses\n\nunquoteDecl \u27e6idWrapper\u27e7 =\n param-rec-def-by-name \u27e6Wrapper\u27e7-env (quote idWrapper) \u27e6idWrapper\u27e7\n\ndata Bot (A : Set\u2080) : Set\u2080 where\n bot : Bot A \u2192 Bot A\n\ngobot : \u2200 {A} \u2192 Bot A \u2192 A\ngobot (bot x) = gobot x\n\ndata [Bot] {A : Set} (A\u209a : A \u2192 Set\u2080)\n : Bot A \u2192 Set\u2080 where\n [bot] : ([Bot] A\u209a [\u2192] [Bot] A\u209a) bot\n\n[Bot]-env = record (\u03b5 1)\n { pDef = [ quote Bot \u2254 quote [Bot] ] id\n ; pConP = [ quote bot \u2254 con (quote [bot]) ] con\n ; pConT = [ quote bot \u2254 conSkip' 2 (quote [bot]) ] con\n }\n\n[gobot]' : (\u2200\u27e8 A \u2236 [Set\u2080] \u27e9[\u2192] [Bot] A [\u2192] A) gobot\n[gobot]' {x0} (x1) {._} ([bot] {x2} x3)\n = [gobot]' {x0} x1 {x2} x3\n\n-- [gobot]' = {!showClauses \"[gobot]'\" (param-rec-clauses-by-name [Bot]-env (quote gobot) (quote [gobot]'))!}\n\n[gobot]2 : (\u2200\u27e8 A \u2236 [Set\u2080] \u27e9[\u2192] [Bot] A [\u2192] A) gobot\n\n[gobot]2-clauses =\n clause\n (arg (arg-info hidden relevant) (var \"A\u1d620\") \u2237\n arg (arg-info visible relevant) (var \"A\u1d63\") \u2237\n arg (arg-info hidden relevant) dot \u2237\n arg (arg-info visible relevant)\n (con (quote [bot])\n (arg (arg-info hidden relevant) (var \"x\u1d620\") \u2237\n arg (arg-info visible relevant) (var \"x\u1d63\") \u2237 []))\n \u2237 [])\n (def (quote [gobot]2)\n (arg (arg-info hidden relevant) (var 4 []) \u2237\n arg (arg-info visible relevant) (var 3 []) \u2237\n arg (arg-info hidden relevant) (var 1 []) \u2237\n arg (arg-info visible relevant) (var 0 []) \u2237 []))\n \u2237 []\n\nunquoteDef [gobot]2 = [gobot]2-clauses\n\nunquoteDecl [gobot] =\n param-rec-def-by-name [Bot]-env (quote gobot) [gobot]\n\ndata \u27e6Bot\u27e7 {A\u2080 A\u2081 : Set} (A\u1d63 : A\u2080 \u2192 A\u2081 \u2192 Set\u2080)\n : Bot A\u2080 \u2192 Bot A\u2081 \u2192 Set\u2080 where\n \u27e6bot\u27e7 : (\u27e6Bot\u27e7 A\u1d63 \u27e6\u2192\u27e7 \u27e6Bot\u27e7 A\u1d63) bot bot\n\n\u27e6Bot\u27e7-env = record (\u03b5 2)\n { pDef = [ quote Bot \u2254 quote \u27e6Bot\u27e7 ] id\n ; pConP = [ quote bot \u2254 con (quote \u27e6bot\u27e7) ] con\n ; pConT = [ quote bot \u2254 conSkip' 3 (quote \u27e6bot\u27e7) ] con\n }\n\n\u27e6gobot\u27e71 : (\u2200\u27e8 A \u2236 \u27e6Set\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Bot\u27e7 A \u27e6\u2192\u27e7 A) gobot gobot\n\u27e6gobot\u27e71 {x0} {x1} x2 {._} {._} (\u27e6bot\u27e7 {x3} {x4} x5)\n = \u27e6gobot\u27e71 {x0} {x1} x2 {x3} {x4} x5\n\nunquoteDecl \u27e6gobot\u27e7 =\n param-rec-def-by-name \u27e6Bot\u27e7-env (quote gobot) \u27e6gobot\u27e7\n\nid\u2080 : {A : Set\u2080} \u2192 A \u2192 A\nid\u2080 x = x\n\n\u27e6id\u2080\u27e7 : (\u2200\u27e8 A \u2236 \u27e6Set\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 A \u27e6\u2192\u27e7 A) id\u2080 id\u2080\n\u27e6id\u2080\u27e7 = \u03bb {x\u2081} {x\u2082} x\u1d63 {x\u2083} {x\u2084} x\u1d63\u2081 \u2192 x\u1d63\u2081\n\ndata List\u2080 (A : Set) : Set where\n [] : List\u2080 A\n _\u2237_ : A \u2192 List\u2080 A \u2192 List\u2080 A\n\nmap\u2080 : \u2200 {A B} (f : A \u2192 B) (xs : List\u2080 A) \u2192 List\u2080 B\nmap\u2080 f [] = []\nmap\u2080 f (x \u2237 xs) = f x \u2237 map\u2080 f xs\n\nidList\u2080 : \u2200 {A} \u2192 List\u2080 A \u2192 List\u2080 A\n-- idList\u2080 : List\u2080 \u2115 \u2192 List\u2080 \u2115\nidList\u2080 [] = []\nidList\u2080 {A} (x \u2237 xs) = idList\u2080 {A} xs\n\ndata \u27e6List\u2080\u27e7 {A\u2080 A\u2081 : Set} (A\u1d63 : A\u2080 \u2192 A\u2081 \u2192 Set\u2080) : List\u2080 A\u2080 \u2192 List\u2080 A\u2081 \u2192 Set\u2080 where\n \u27e6[]\u27e7 : \u27e6List\u2080\u27e7 A\u1d63 [] []\n _\u27e6\u2237\u27e7_ : (A\u1d63 \u27e6\u2192\u27e7 \u27e6List\u2080\u27e7 A\u1d63 \u27e6\u2192\u27e7 \u27e6List\u2080\u27e7 A\u1d63) _\u2237_ _\u2237_\n\ncon\u27e6List\u2080\u27e7 = conSkip' 3\n\u27e6List\u2080\u27e7-env = record (\u03b5 2)\n { pDef = [ quote List\u2080 \u2254 quote \u27e6List\u2080\u27e7 ]\n ([ quote \u2115 \u2254 quote \u27e6\u2115\u27e7 ]\n ([ quote id\u2080 \u2254 quote \u27e6id\u2080\u27e7 ] id))\n ; pConP = [ quote List\u2080.[] \u2254 con (quote \u27e6List\u2080\u27e7.\u27e6[]\u27e7) ]\n ([ quote List\u2080._\u2237_ \u2254 con (quote \u27e6List\u2080\u27e7._\u27e6\u2237\u27e7_) ]\n con)\n ; pConT = [ quote List\u2080.[] \u2254 con\u27e6List\u2080\u27e7 (quote \u27e6List\u2080\u27e7.\u27e6[]\u27e7) ]\n ([ quote List\u2080._\u2237_ \u2254 con\u27e6List\u2080\u27e7 (quote \u27e6List\u2080\u27e7._\u27e6\u2237\u27e7_) ]\n con)\n }\n\n\u27e6idList\u2080\u27e7 : unquote (unEl (param-type-by-name \u27e6List\u2080\u27e7-env (quote idList\u2080)))\n-- \u27e6idList\u2080\u27e7 : (\u2200\u27e8 A \u2236 \u27e6Set\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6List\u2080\u27e7 A \u27e6\u2192\u27e7 \u27e6List\u2080\u27e7 A) idList\u2080 idList\u2080\nunquoteDef \u27e6idList\u2080\u27e7 = param-rec-clauses-by-name \u27e6List\u2080\u27e7-env (quote idList\u2080) (quote \u27e6idList\u2080\u27e7)\n\n{-\n\u27e6map\u2080\u27e7 : {x0 : Set0} \u2192 {x1 : Set0} \u2192 (x2 : (x2 : x0) \u2192 (x3 : x1) \u2192 Set0) \u2192 {x3 : Set0} \u2192 {x4 : Set0} \u2192 (x5 : (x5 : x3) \u2192 (x6 : x4) \u2192 Set0) \u2192 {x6 : (x6 : x0) \u2192 x3} \u2192 {x7 : (x7 : x1) \u2192 x4} \u2192 (x8 : {x8 : x0} \u2192 {x9 : x1} \u2192 (x10 : x2 (x8) (x9)) \u2192 x5 (x6 (x8)) (x7 (x9))) \u2192 {x9 : List\u2080 (x0)} \u2192 {x10 : List\u2080 (x1)} \u2192 (x11 : \u27e6List\u2080\u27e7 {x0} {x1} (x2) (x9) (x10)) \u2192 \u27e6List\u2080\u27e7 {x3} {x4} (x5) (map\u2080 {x0} {x3} (x6) (x9)) (map\u2080 {x1} {x4} (x7) (x10))\n\u27e6map\u2080\u27e7 {x0} {x1} (x2) {x3} {x4} (x5) {x6} {x7} (x8) {._} {._} (\u27e6[]\u27e7 ) = \u27e6[]\u27e7\n\u27e6map\u2080\u27e7 {x0} {x1} (x2) {x3} {x4} (x5) {x6} {x7} (x8) {._} {._} (_\u27e6\u2237\u27e7_ {x11} {x12} (x13) {x14} {x15} (x16) ) = _\u27e6\u2237\u27e7_ {x6 (x11)} {x7 (x12)} (x8 {x11} {x12} (x13)) {map\u2080 {x0} {x3} (x6) (x14)} {map\u2080 {x1} {x4} (x7) (x15)} (\u27e6map\u2080\u27e7 {x0} {x1} (x2) {x3} {x4} (x5) {x6} {x7} (x8) {x14} {x15} (x16))\n-}\n\nunquoteDecl \u27e6map\u2080\u27e7 = param-rec-def-by-name \u27e6List\u2080\u27e7-env (quote map\u2080) \u27e6map\u2080\u27e7\n\n{-\n -- The generated type is bigger since it is a familly for no reason.\n data \u27e6List\u2080\u27e7 : (\u27e6Set\u2080\u27e7 \u27e6\u2192\u27e7 \u27e6Set\u2081\u27e7) List\u2080 List\u2080\n\n private\n \u27e6List\u2080\u27e7-ctor = \u03bb c \u2192 unEl (param-ctor-by-name (extDefEnv [ quote List\u2080 \u2254 quote \u27e6List\u2080\u27e7 ] (\u03b5 2)) c)\n\n data \u27e6List\u2080\u27e7 where\n \u27e6[]\u27e7 : unquote (\u27e6List\u2080\u27e7-ctor (quote List\u2080.[]))\n _\u27e6\u2237\u27e7_ : unquote (\u27e6List\u2080\u27e7-ctor (quote List\u2080._\u2237_))\n\n \u27e6List\u2080\u27e7-env = extConEnv ([ quote List\u2080.[] \u2254 quote \u27e6List\u2080\u27e7.\u27e6[]\u27e7 ] \u2218\n [ quote List\u2080._\u2237_ \u2254 quote \u27e6List\u2080\u27e7._\u27e6\u2237\u27e7_ ])\n (extDefEnv [ quote List\u2080 \u2254 quote \u27e6List\u2080\u27e7 ] (\u03b5 2))\n-}\n\ndata Maybe' (A : Set) : Set\u2081 where\n nothing : Maybe' A\n just : A \u2192 Maybe' A\n\n{-\n-- Set\u2081 is here because \u27e6List\u2080\u27e7 is not using parameters, hence gets bigger.\n-- This only happens without-K given the new rules for data types.\ndata List\u2080 : (A : Set) \u2192 Set\u2081 where\n [] : \u2200 {A} \u2192 List\u2080 A\n _\u2237_ : \u2200 {A} \u2192 A \u2192 List\u2080 A \u2192 List\u2080 A\n\nmap\u2080 : \u2200 {A B} \u2192 (A \u2192 B) \u2192 List\u2080 A \u2192 List\u2080 B\nmap\u2080 f [] = []\nmap\u2080 f (x \u2237 xs) = f x \u2237 map\u2080 f xs\n\nidList\u2080 : \u2200 {A} \u2192 List\u2080 A \u2192 List\u2080 A\nidList\u2080 [] = []\nidList\u2080 (x \u2237 xs) = x \u2237 idList\u2080 xs\n\n-- The generated type bigger since it is a familly for no reason.\ndata \u27e6List\u2080\u27e7 : (\u27e6Set\u2080\u27e7 \u27e6\u2192\u27e7 \u27e6Set\u2081\u27e7) List\u2080 List\u2080\n\nprivate\n \u27e6List\u2080\u27e7-ctor = \u03bb c \u2192 unEl (param-ctor-by-name (extDefEnv [ quote List\u2080 \u2254 quote \u27e6List\u2080\u27e7 ] (\u03b5 2)) c)\n\ndata \u27e6List\u2080\u27e7 where\n \u27e6[]\u27e7 : unquote (\u27e6List\u2080\u27e7-ctor (quote List\u2080.[]))\n _\u27e6\u2237\u27e7_ : unquote (\u27e6List\u2080\u27e7-ctor (quote List\u2080._\u2237_))\n\n\u27e6List\u2080\u27e7-env = extConEnv ([ quote List\u2080.[] \u2254 quote \u27e6List\u2080\u27e7.\u27e6[]\u27e7 ] \u2218\n [ quote List\u2080._\u2237_ \u2254 quote \u27e6List\u2080\u27e7._\u27e6\u2237\u27e7_ ])\n (extDefEnv [ quote List\u2080 \u2254 quote \u27e6List\u2080\u27e7 ] (\u03b5 2))\n\n-- test = \u27e6[]\u27e7 {{!showType (type (quote List\u2080.[]))!}} {{!!}} {!!}\n-}\n\n{-\n\u27e6idList\u2080\u27e7 : unquote (unEl (param-type-by-name \u27e6List\u2080\u27e7-env (quote idList\u2080)))\nunquoteDef \u27e6idList\u2080\u27e7 = param-rec-clauses-by-name \u27e6List\u2080\u27e7-env (quote idList\u2080) (quote \u27e6idList\u2080\u27e7)\n-}\n\n{-\n\u27e6map\u2080\u27e7 : unquote (unEl (param-type-by-name \u27e6List\u2080\u27e7-env (quote map\u2080)))\n\u27e6map\u2080\u27e7 {x} {x\u2081} {x\u2082} {x\u2083} {x\u2084} {x\u2085} {x\u2086} {x\u2087} x\u2088 {[]} {[]} \u27e6[]\u27e7\n = \u27e6[]\u27e7\n\u27e6map\u2080\u27e7 {x} {x\u2081} {x\u2082} {x\u2083} {x\u2084} {x\u2085} {x\u2086} {x\u2087} x\u2088 {._ \u2237 ._}\n {._ \u2237 ._}\n (_\u27e6\u2237\u27e7_ {x\u2081\u2083} {x\u2081\u2084} x\u2081\u2085 {x\u2081\u2086} {x\u2081\u2087} x\u2081\u2088)\n = _\u27e6\u2237\u27e7_ {x\u2081\u2080 x\u2081\u2083} {x\u2081\u2081 x\u2081\u2084} (x\u2081\u2082 {x\u2081\u2083} {x\u2081\u2084} x\u2081\u2085)\n {map\u2080 {x\u2084} {x\u2087} x\u2081\u2080 x\u2081\u2086} {map\u2080 {x\u2085} {x\u2088} x\u2081\u2081 x\u2081\u2087}\n (\u27e6map\u2080\u27e7 {x\u2084} {x\u2085} {x\u2086} {x\u2087} {x\u2088} {x\u2089} {x\u2081\u2080} {x\u2081\u2081} x\u2081\u2082 {x\u2081\u2086} {x\u2081\u2087}\n x\u2081\u2088)\n-}\n\n{-\nunquoteDef \u27e6map\u2080\u27e7 = param-rec-clauses-by-name \u27e6List\u2080\u27e7-env (quote map\u2080) (quote \u27e6map\u2080\u27e7)\n-}\n\n\n{-\nfoo : {x0 : Set0} \u2192 {x1 : Set0} \u2192 (x2 : (x2 : x0) \u2192 (x3 : x1) \u2192 Set0) \u2192 {x3 : Set0} \u2192 {x4 : Set0} \u2192 (x5 : (x5 : x3) \u2192 (x6 : x4) \u2192 Set0) \u2192 {x6 : (x6 : x0) \u2192 x3} \u2192 {x7 : (x7 : x1) \u2192 x4} \u2192 (x8 : {x8 : x0} \u2192 {x9 : x1} \u2192 (x10 : x2 (x8) (x9)) \u2192 x5 (x6 (x8)) (x7 (x9))) \u2192 {x9 : Reflection.Param.List\u2080 (x0)} \u2192 {x10 : Reflection.Param.List\u2080 (x1)} \u2192 (x11 : Reflection.Param.\u27e6List\u2080\u27e7 {x0} {x1} (x2) (x9) (x10)) \u2192 Reflection.Param.\u27e6List\u2080\u27e7 {x3} {x4} (x5) (Reflection.Param.map\u2080 {x0} {x3} (x6) (x9)) (Reflection.Param.map\u2080 {x1} {x4} (x7) (x10))\nfoo {A} {A} (A) {B} {B} (B) {f} {f} (f) {._} {._} (Reflection.Param.\u27e6List\u2080\u27e7.\u27e6[]\u27e7 ) = Reflection.Param.\u27e6List\u2080\u27e7.\u27e6[]\u27e7\nfoo {A} {A} (A) {B} {B} (B) {f} {f} (f) {._} {._} (Reflection.Param.\u27e6List\u2080\u27e7._\u27e6\u2237\u27e7_ {x} {x} (x) {xs} {xs} (xs) ) = Reflection.Param.\u27e6List\u2080\u27e7._\u27e6\u2237\u27e7_ {x0 (x0)} {x0 (x0)} (x0 {x0} {x0} (x0)) {Reflection.Param.map\u2080 {x0} {x0} (x0) (x0)} {Reflection.Param.map\u2080 {x0} {x0} (x0) (x0)} (Reflection.Param.test' {x0} {x0} (x0) {x0} {x0} (x0) {x0} {x0} (x0) {x0} {x0} (x0))\n-}\n\n-- test' = {! showFunDef \"foo\" (param-rec-def-by-name \u27e6List\u2080\u27e7-env (quote map\u2080) (quote test'))!}\n\nopen import Function.Param.Unary\n\nrevealed-[\u2192] = Reveal-args.n\u00e5me (quote _[\u2080\u2192\u2080]_)\n\nrevealed-[\u2192]' : \u2200 (A : Set\u2080) (A\u209a : A \u2192 Set\u2080)\n (B : Set\u2080) (B\u209a : B \u2192 Set\u2080)\n (f : A \u2192 B) \u2192 Set\u2080\nunquoteDef revealed-[\u2192]' = Get-clauses.from-def revealed-[\u2192]\n\nrevelator-[\u2192] : ({A : Set} (A\u209a : A \u2192 Set) {B : Set} (B\u209a : B \u2192 Set) (f : A \u2192 B) \u2192 Set)\n \u2192 (A : Set) (A\u209a : A \u2192 Set) (B : Set) (B\u209a : B \u2192 Set) (f : A \u2192 B) \u2192 Set\nunquoteDef revelator-[\u2192] = Revelator.clauses (type (quote _[\u2080\u2192\u2080]_))\n\np-[\u2192]-type = param-type-by-name (\u03b5 1) (quote _[\u2080\u2192\u2080]_)\np-[\u2192] = param-clauses-by-name (\u03b5 1) (quote _[\u2080\u2192\u2080]_)\n\np-[\u2192]' = \u2200 {A : Set\u2080} (A\u2080\u209a : A \u2192 Set\u2080)\n {A\u209a : A \u2192 Set\u2080} (A\u2081\u209a : {x : A} \u2192 A\u2080\u209a x \u2192 A\u209a x \u2192 Set\u2080)\n {B : Set\u2080} (B\u2080\u209a : B \u2192 Set\u2080)\n {B\u209a : B \u2192 Set\u2080} (B\u2081\u209a : {x : B} \u2192 B\u2080\u209a x \u2192 B\u209a x \u2192 Set\u2080)\n {f : A \u2192 B} (f\u209a : {x : A} \u2192 A\u2080\u209a x \u2192 B\u2080\u209a (f x))\n \u2192 (A\u209a [\u2080\u2192\u2080] B\u209a) f\n \u2192 Set\n\np-[\u2192]'-test : p-[\u2192]' \u2261 unquote (unEl p-[\u2192]-type)\np-[\u2192]'-test = refl\n\n[[\u2192]] : unquote (unEl p-[\u2192]-type)\nunquoteDef [[\u2192]] = p-[\u2192]\n\ndata [[\u2115]] : [[Set\u2080]] [\u2115] [\u2115] where\n [[zero]] : [[\u2115]] [zero] [zero]\n [[suc]] : [[\u2192]] [\u2115] [[\u2115]] [\u2115] [[\u2115]] [suc] [suc]\n\n_\/2 : \u2115 \u2192 \u2115\nzero \/2 = zero\nsuc zero \/2 = zero\nsuc (suc n) \/2 = suc (n \/2)\n\n_\u27e6\/2\u27e7 : (\u27e6\u2115\u27e7 \u27e6\u2080\u2192\u2080\u27e7 \u27e6\u2115\u27e7) _\/2 _\/2\n\u27e6zero\u27e7 \u27e6\/2\u27e7 = \u27e6zero\u27e7\n\u27e6suc\u27e7 \u27e6zero\u27e7 \u27e6\/2\u27e7 = \u27e6zero\u27e7\n\u27e6suc\u27e7 (\u27e6suc\u27e7 n) \u27e6\/2\u27e7 = \u27e6suc\u27e7 (n \u27e6\/2\u27e7)\n\n_+\u2115_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero +\u2115 n = n\nsuc m +\u2115 n = suc (m +\u2115 n)\n\npred' : \u2115 \u2192 \u2115\npred' = \u03bb { zero \u2192 zero\n ; (suc m) \u2192 m }\n\n\u27e6pred'\u27e7 : (\u27e6\u2115\u27e7 \u27e6\u2192\u27e7 \u27e6\u2115\u27e7) pred' pred'\nunquoteDef \u27e6pred'\u27e7 = param-clauses-by-name defEnv2 (quote pred')\n\n_\u27e6+\u2115\u27e7_ : (\u27e6\u2115\u27e7 \u27e6\u2080\u2192\u2080\u27e7 \u27e6\u2115\u27e7 \u27e6\u2080\u2192\u2080\u27e7 \u27e6\u2115\u27e7) _+\u2115_ _+\u2115_\n\u27e6zero\u27e7 \u27e6+\u2115\u27e7 n = n\n\u27e6suc\u27e7 m \u27e6+\u2115\u27e7 n = \u27e6suc\u27e7 (m \u27e6+\u2115\u27e7 n)\n\n\u27e6\u27e6Set\u2080\u27e7\u27e7 : (\u27e6Set\u2080\u27e7 \u27e6\u2081\u2192\u2082\u27e7 \u27e6Set\u2080\u27e7 \u27e6\u2081\u2192\u2082\u27e7 \u27e6Set\u2081\u27e7) \u27e6Set\u2080\u27e7 \u27e6Set\u2080\u27e7\n\u27e6\u27e6Set\u2080\u27e7\u27e7 = \u03bb A\u2080 A\u2081 \u2192 A\u2080 \u27e6\u2080\u2192\u2081\u27e7 A\u2081 \u27e6\u2080\u2192\u2081\u27e7 \u27e6Set\u2080\u27e7\n\n\u27e6\u27e6Set\u2080\u27e7\u27e7' : {x\u2081 x\u2082 : Set} (x\u1d63 : x\u2081 \u2192 x\u2082 \u2192 Set) {x\u2083 : Set} {x\u2084 : Set}\n (x\u1d63\u2081 : x\u2083 \u2192 x\u2084 \u2192 Set) \u2192\n (x\u2081 \u2192 x\u2083 \u2192 Set) \u2192 (x\u2082 \u2192 x\u2084 \u2192 Set) \u2192 Set\u2081\n\u27e6\u27e6Set\u2080\u27e7\u27e7' = \u03bb A\u2080 A\u2081 f\u2081 f\u2082 \u2192\n \u2200 {x\u2081} {x\u2082} (x\u1d63 : A\u2080 x\u2081 x\u2082)\n {x\u2083} {x\u2084} (x\u1d63\u2081 : A\u2081 x\u2083 x\u2084) \u2192\n f\u2081 x\u2081 x\u2083 \u2192 f\u2082 x\u2082 x\u2084 \u2192 Set\n\n-- Since quoteTerm normalises\ntest-\u27e6\u27e6Set\u2080\u27e7\u27e7 : quoteTerm \u27e6\u27e6Set\u2080\u27e7\u27e7 \u2261-no-hints quoteTerm \u27e6\u27e6Set\u2080\u27e7\u27e7'\ntest-\u27e6\u27e6Set\u2080\u27e7\u27e7 = refl\n\n\u27e6\u27e6Set\u2080\u27e7\u27e7-type = unquote (unEl (type (quote \u27e6\u27e6Set\u2080\u27e7\u27e7)))\ntest-\u27e6\u27e6Set\u2080\u27e7\u27e7-type : \u27e6\u27e6Set\u2080\u27e7\u27e7-type \u2261 unquote (unEl (type (quote \u27e6\u27e6Set\u2080\u27e7\u27e7')))\ntest-\u27e6\u27e6Set\u2080\u27e7\u27e7-type = refl\n\npSet\u2080 = pTerm defEnv2 `\u2605\u2080\nppSet\u2080 = pTerm defEnv2 pSet\u2080\np\u27e6Set\u2080\u27e7 = param-clauses-by-name defEnv2 (quote \u27e6Set\u2080\u27e7)\ntest-pSet\u2080 : pSet\u2080 \u2261-no-hints quoteTerm \u27e6Set\u2080\u27e7\ntest-pSet\u2080 = refl\ntest-ppSet\u2080 : ppSet\u2080 \u2261-no-hints quoteTerm \u27e6\u27e6Set\u2080\u27e7\u27e7\ntest-ppSet\u2080 = refl\ntest-ppSet\u2080'' : ppSet\u2080 \u2261-no-hints Get-term.from-clauses p\u27e6Set\u2080\u27e7\ntest-ppSet\u2080'' = refl\n\n\u27e6\u27e6Set\u2080\u27e7\u27e7'' : (\u27e6Set\u2080\u27e7 \u27e6\u2081\u2192\u2082\u27e7 \u27e6Set\u2080\u27e7 \u27e6\u2081\u2192\u2082\u27e7 \u27e6Set\u2081\u27e7) \u27e6Set\u2080\u27e7 \u27e6Set\u2080\u27e7\nunquoteDef \u27e6\u27e6Set\u2080\u27e7\u27e7'' = p\u27e6Set\u2080\u27e7\n\ntest-\u27e6\u27e6Set\u2080\u27e7\u27e7'' : _\u2261_ {A = \u27e6\u27e6Set\u2080\u27e7\u27e7-type} \u27e6\u27e6Set\u2080\u27e7\u27e7'' \u27e6\u27e6Set\u2080\u27e7\u27e7\ntest-\u27e6\u27e6Set\u2080\u27e7\u27e7'' = refl\n\ntest-p0-\u27e6Set\u2080\u27e7 : pTerm defEnv0 (quoteTerm \u27e6Set\u2080\u27e7) \u2261-no-hints quoteTerm \u27e6Set\u2080\u27e7\ntest-p0-\u27e6Set\u2080\u27e7 = refl\n\ndata \u27e6\u27e6\ud835\udfda\u27e7\u27e7 : (\u27e6\u27e6Set\u2080\u27e7\u27e7 \u27e6\ud835\udfda\u27e7 \u27e6\ud835\udfda\u27e7) \u27e6\ud835\udfda\u27e7 \u27e6\ud835\udfda\u27e7 where\n \u27e6\u27e60\u2082\u27e7\u27e7 : \u27e6\u27e6\ud835\udfda\u27e7\u27e7 \u27e60\u2082\u27e7 \u27e60\u2082\u27e7 \u27e60\u2082\u27e7 \u27e60\u2082\u27e7\n \u27e6\u27e61\u2082\u27e7\u27e7 : \u27e6\u27e6\ud835\udfda\u27e7\u27e7 \u27e61\u2082\u27e7 \u27e61\u2082\u27e7 \u27e61\u2082\u27e7 \u27e61\u2082\u27e7\n\np1\u2115\u2192\u2115 = pTerm defEnv1 (quoteTerm (\u2115 \u2192 \u2115))\n[\u2115\u2192\u2115] = [\u2115] [\u2192] [\u2115]\ntest-p1\u2115\u2192\u2115 : unquote p1\u2115\u2192\u2115 \u2261 [\u2115\u2192\u2115]\ntest-p1\u2115\u2192\u2115 = refl\n\np2\u2115\u2192\u2115 = pTerm defEnv2 (quoteTerm (\u2115 \u2192 \u2115))\n\u27e6\u2115\u2192\u2115\u27e7 = \u27e6\u2115\u27e7 \u27e6\u2192\u27e7 \u27e6\u2115\u27e7\ntest-p2\u2115\u2192\u2115 : unquote p2\u2115\u2192\u2115 \u2261 \u27e6\u2115\u2192\u2115\u27e7\ntest-p2\u2115\u2192\u2115 = refl\n\np\u2115\u2192\u2115\u2192\u2115 = pTerm defEnv2 (quoteTerm (\u2115 \u2192 \u2115 \u2192 \u2115))\n\u27e6\u2115\u2192\u2115\u2192\u2115\u27e7 = \u27e6\u2115\u27e7 \u27e6\u2192\u27e7 \u27e6\u2115\u27e7 \u27e6\u2192\u27e7 \u27e6\u2115\u27e7\ntest-p\u2115\u2192\u2115\u2192\u2115 : p\u2115\u2192\u2115\u2192\u2115 \u2261-no-hints quoteTerm \u27e6\u2115\u2192\u2115\u2192\u2115\u27e7\ntest-p\u2115\u2192\u2115\u2192\u2115 = refl\nZERO : Set\u2081\nZERO = (A : Set\u2080) \u2192 A\n\u27e6ZERO\u27e7 : ZERO \u2192 ZERO \u2192 Set\u2081\n\u27e6ZERO\u27e7 f\u2080 f\u2081 =\n {A\u2080 A\u2081 : Set\u2080} (A\u1d63 : A\u2080 \u2192 A\u2081 \u2192 Set\u2080)\n \u2192 A\u1d63 (f\u2080 A\u2080) (f\u2081 A\u2081)\npZERO = pTerm (\u03b5 2) (quoteTerm ZERO)\nq\u27e6ZERO\u27e7 = quoteTerm \u27e6ZERO\u27e7\ntest-pZERO : pZERO \u2261-no-hints q\u27e6ZERO\u27e7\ntest-pZERO = refl\nID : Set\u2081\nID = (A : Set\u2080) \u2192 A \u2192 A\n\u27e6ID\u27e7 : ID \u2192 ID \u2192 Set\u2081\n\u27e6ID\u27e7 f\u2080 f\u2081 =\n {A\u2080 A\u2081 : Set\u2080} (A\u1d63 : A\u2080 \u2192 A\u2081 \u2192 Set\u2080)\n {x\u2080 : A\u2080} {x\u2081 : A\u2081} (x : A\u1d63 x\u2080 x\u2081)\n \u2192 A\u1d63 (f\u2080 A\u2080 x\u2080) (f\u2081 A\u2081 x\u2081)\npID = pTerm (\u03b5 2) (quoteTerm ID)\nq\u27e6ID\u27e7 = quoteTerm \u27e6ID\u27e7\ntest-ID : q\u27e6ID\u27e7 \u2261-no-hints pID\ntest-ID = refl\n\n\u27e6not\u27e7' : (\u27e6\ud835\udfda\u27e7 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) not not\nunquoteDef \u27e6not\u27e7' = param-clauses-by-name defEnv2 (quote not)\ntest-not : \u2200 {x\u2080 x\u2081 : \ud835\udfda} (x\u1d63 : \u27e6\ud835\udfda\u27e7 x\u2080 x\u2081) \u2192 \u27e6not\u27e7 x\u1d63 \u2261 \u27e6not\u27e7' x\u1d63\ntest-not \u27e60\u2082\u27e7 = refl\ntest-not \u27e61\u2082\u27e7 = refl\n\n[pred]' : ([\u2115] [\u2192] [\u2115]) pred\nunquoteDef [pred]' = param-clauses-by-name defEnv1 (quote pred)\n\ntest-p1-pred : \u2200 {n} (n\u209a : [\u2115] n) \u2192 [pred]' n\u209a \u2261 [pred] n\u209a\ntest-p1-pred [zero] = refl\ntest-p1-pred ([suc] n\u209a) = refl\n\n\u27e6pred\u27e7' : (\u27e6\u2115\u27e7 \u27e6\u2192\u27e7 \u27e6\u2115\u27e7) pred pred\nunquoteDef \u27e6pred\u27e7' = param-clauses-by-name defEnv2 (quote pred)\n\ntest-p2-pred : \u2200 {n\u2080 n\u2081} (n\u1d63 : \u27e6\u2115\u27e7 n\u2080 n\u2081) \u2192 \u27e6pred\u27e7' n\u1d63 \u2261 \u27e6pred\u27e7 n\u1d63\ntest-p2-pred \u27e6zero\u27e7 = refl\ntest-p2-pred (\u27e6suc\u27e7 n\u1d63) = refl\n\np\/2 = param-rec-def-by-name defEnv2 (quote _\/2)\nq\u27e6\/2\u27e7 = definition (quote _\u27e6\/2\u27e7)\nunquoteDecl _\u27e6\/2\u27e7' = p\/2 _\u27e6\/2\u27e7'\ntest-\/2 : function (p\/2 (quote _\u27e6\/2\u27e7)) \u2261-def-no-hints q\u27e6\/2\u27e7\ntest-\/2 = refl\ntest-\/2' : \u2200 {n\u2080 n\u2081} (n\u1d63 : \u27e6\u2115\u27e7 n\u2080 n\u2081) \u2192 n\u1d63 \u27e6\/2\u27e7' \u2261 n\u1d63 \u27e6\/2\u27e7\ntest-\/2' \u27e6zero\u27e7 = refl\ntest-\/2' (\u27e6suc\u27e7 \u27e6zero\u27e7) = refl\ntest-\/2' (\u27e6suc\u27e7 (\u27e6suc\u27e7 n\u1d63)) rewrite test-\/2' n\u1d63 = refl\n\np+ = param-rec-def-by-name defEnv2 (quote _+\u2115_)\nq\u27e6+\u27e7 = definition (quote _\u27e6+\u2115\u27e7_)\nunquoteDecl _\u27e6+\u27e7'_ = p+ _\u27e6+\u27e7'_\ntest-+ : function (p+ (quote _\u27e6+\u2115\u27e7_)) \u2261-def-no-hints q\u27e6+\u27e7\ntest-+ = refl\ntest-+' : \u2200 {n\u2080 n\u2081} (n\u1d63 : \u27e6\u2115\u27e7 n\u2080 n\u2081) {n'\u2080 n'\u2081} (n'\u1d63 : \u27e6\u2115\u27e7 n'\u2080 n'\u2081) \u2192 n\u1d63 \u27e6+\u27e7' n'\u1d63 \u2261 n\u1d63 \u27e6+\u2115\u27e7 n'\u1d63\ntest-+' \u27e6zero\u27e7 n'\u1d63 = refl\ntest-+' (\u27e6suc\u27e7 n\u1d63) n'\u1d63 rewrite test-+' n\u1d63 n'\u1d63 = refl\n\n{-\nis-good : String \u2192 \ud835\udfda\nis-good \"good\" = 1\u2082\nis-good _ = 0\u2082\n\n\u27e6is-good\u27e7 : (\u27e6String\u27e7 \u27e6\u2080\u2192\u2080\u27e7 \u27e6\ud835\udfda\u27e7) is-good is-good\n\u27e6is-good\u27e7 {\"good\"} refl = \u27e61\u2082\u27e7\n\u27e6is-good\u27e7 {_} refl = {!!}\n\nmy-good = unquote (lit (string \"good\"))\nmy-good-test : my-good \u2261 \"good\"\nmy-good-test = refl\n-}\n\n{-\n\u27e6is-good\u27e7' : (\u27e6String\u27e7 \u27e6\u2080\u2192\u2080\u27e7 \u27e6\ud835\udfda\u27e7) is-good is-good\nunquoteDef \u27e6is-good\u27e7' = param-clauses-by-name defEnv2 (quote is-good)\ntest-is-good = {!!}\n-}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"e4e6824d73e23b701e1ba92ba7fbe57713592123","subject":"Added examples for an issue.","message":"Added examples for an issue.\n\nIgnore-this: d41f2b18e5032bdc71396e32315466b7\n\ndarcs-hash:20120619011758-3bd4e-b614e3f5267122084c9b6e13b2bb79830a0f802a.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Issues\/BadErase.agda","new_file":"Issues\/BadErase.agda","new_contents":"------------------------------------------------------------------------------\n-- The translation is badly erasing the universal quantification\n------------------------------------------------------------------------------\n\n-- Found on 14 March 2012.\n\nmodule Issues.BadErase where\n\npostulate\n _\u2194_ : Set \u2192 Set \u2192 Set\n D : Set\n A : Set\n\npostulate bad\u2081 : ((x : D) \u2192 A) \u2192 A\n{-# ATP prove bad\u2081 #-}\n\npostulate bad\u2082 : A \u2192 ((x : D) \u2192 A)\n{-# ATP prove bad\u2082 #-}\n\npostulate bad\u2083 : ((x : D) \u2192 A) \u2194 A\n{-# ATP prove bad\u2083 #-}\n","old_contents":"------------------------------------------------------------------------------\n-- The translation is badly erasing the universal quantification\n------------------------------------------------------------------------------\n\n-- Found on 14 March 2012.\n\nmodule Issues.BadErase where\n\npostulate\n D : Set\n A : Set\n\npostulate bad : ((x : D) \u2192 A) \u2192 A\n{-# ATP prove bad #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c312b0e9806924742644194bb8a96393e20e9309","subject":"composable: More abstraction madness","message":"composable: More abstraction madness\n","repos":"crypto-agda\/crypto-agda","old_file":"composable.agda","new_file":"composable.agda","new_contents":"module composable where\n\nopen import Level\nopen import Function\nopen import Data.Unit using (\u22a4)\nopen import Relation.Binary\n\nArrow : \u2200 {i} \u2192 Set i \u2192 \u2200 j \u2192 Set (suc j \u2294 i)\nArrow = Rel\n\nComposition : \u2200 {a \u2113} {A : Set a} \u2192 Arrow A \u2113 \u2192 Set _\nComposition = Transitive\n\nIdentity : \u2200 {a \u2113} {A : Set a} \u2192 Arrow A \u2113 \u2192 Set _\nIdentity = Reflexive\n\nIArrow : \u2200 {i j t} {I : Set i} (_\u219d\u1d62_ : Arrow I j) (T : I \u2192 Set t) a \u2192 Set _\nIArrow _\u219d\u1d62_ T a = \u2200 {i\u2080 i\u2081} \u2192 i\u2080 \u219d\u1d62 i\u2081 \u2192 T i\u2080 \u2192 T i\u2081 \u2192 Set a\n\nIReflexivity : \u2200 {a i j t} {I : Set i} {R : Rel I j} {T : I \u2192 Set t} \u2192 Reflexive R \u2192 IArrow R T a \u2192 Set _\nIReflexivity R-refl Arr = \u2200 {i A} \u2192 Arr (R-refl {i}) A A\n\nIIdentity : \u2200 {a i j t} {I : Set i} {_\u219d\u1d62_ : Arrow I j} {T : I \u2192 Set t} \u2192 Identity _\u219d\u1d62_ \u2192 IArrow _\u219d\u1d62_ T a \u2192 Set _\nIIdentity = IReflexivity\n\nITrans : \u2200 {i j t a} {I : Set i} {R\u2080 R\u2081 R\u2082 : Rel I j} {T : I \u2192 Set t}\n (R-trans : Trans R\u2080 R\u2081 R\u2082)\n (Arr\u2080 : IArrow R\u2080 T a)\n (Arr\u2080 : IArrow R\u2081 T a)\n (Arr\u2080 : IArrow R\u2082 T a)\n \u2192 Set _\nITrans R-trans Arr\u2080 Arr\u2081 Arr\u2082\n = \u2200 {i\u2080 i\u2081 i\u2082 j\u2080 j\u2081} \u2192 Trans (Arr\u2080 j\u2080) (Arr\u2081 j\u2081) (Arr\u2082 (R-trans {i\u2080} {i\u2081} {i\u2082} j\u2080 j\u2081))\n\nITransitive : \u2200 {i j t a} {I : Set i} {R : Rel I j} {T : I \u2192 Set t}\n \u2192 Transitive R \u2192 IArrow R T a \u2192 Set _\nITransitive {R = R} R-trans Arr = ITrans {R\u2080 = R} {R} {R} R-trans Arr Arr Arr\n\nIComposition : \u2200 {i j t a} {I : Set i} {_\u219d\u1d62_ : Arrow I j} {T : I \u2192 Set t}\n (_\u00b7_ : Composition _\u219d\u1d62_)\n (\u27e8_\u27e9_\u219d_ : IArrow _\u219d\u1d62_ T a) \u2192 Set _\nIComposition = ITransitive\n\nrecord IComposable {i j t a} {I : Set i} {_\u219d\u1d62_ : Arrow I j} {T : I \u2192 Set t}\n (_\u00b7_ : Composition _\u219d\u1d62_)\n (\u27e8_\u27e9_\u219d_ : IArrow _\u219d\u1d62_ T a)\n : Set (a \u2294 t \u2294 i \u2294 j) where\n constructor mk\n infixr 1 _>>>_\n field\n-- _>>>_ : \u2200 {i\u2080 i\u2081 i\u2082} {ix\u2080 : i\u2080 \u219d\u1d62 i\u2081} {ix\u2081 : i\u2081 \u219d\u1d62 i\u2082} {A B C}\n-- \u2192 (\u27e8 ix\u2080 \u27e9 A \u219d B) \u2192 (\u27e8 ix\u2081 \u27e9 B \u219d C) \u2192 (\u27e8 ix\u2080 \u00b7 ix\u2081 \u27e9 A \u219d C)\n _>>>_ : IComposition (\u03bb {\u03b7} \u2192 _\u00b7_ {\u03b7}) \u27e8_\u27e9_\u219d_\n\nopen import Relation.Binary.PropositionalEquality\nRefl-Unit : \u2200 {\u2113 a} {A : Set a} {R : Rel A \u2113} \u2192 Reflexive R \u2192 Transitive R \u2192 Set _\nRefl-Unit {R = R} R-refl R-trans = \u2200 {x y} (p : R x y) \u2192 R-trans R-refl p \u2261 p\n\n{-\nrecord ICat {i j t a} {I : Set i} {_\u219d\u1d62_ : Arrow I j} {T : I \u2192 Set t}\n {_\u00b7_ : Composition _\u219d\u1d62_}\n {\u27e8_\u27e9_\u219d_ : IArrow a _\u219d\u1d62_ T}\n (comp : IComposable _\u00b7_ \u27e8_\u27e9_\u219d_)\n {id\u1d62 : Identity _\u219d\u1d62_}\n (id : IIdentity (\u03bb {\u03b7} \u2192 id\u1d62 {\u03b7}) \u27e8_\u27e9_\u219d_)\n (_\u2248\u1d62_ : \u2200 {a b} (i j : a \u219d\u1d62 b) \u2192 Set)\n (_\u2248_ : \u2200 {i j A B} \u2192 i \u2248\u1d62 j \u2192 \u27e8 i \u27e9 A \u219d B \u2192 \u27e8 j \u27e9 A \u219d B \u2192 Set) : Set\n where\n constructor mk\n open IComposable comp\n field\n id-unit->>> :\n \u2200 f \u2192 \u27e8 id >>> f \u2248 f\n-}\nopen import Data.Unit using (\u22a4)\n\nConstArr : \u2200 {a} (A : Set a) \u2192 \u22a4 \u2192 \u22a4 \u2192 Set a\nConstArr A _ _ = A\n\nComposable : \u2200 {t a} {T : Set t} (_\u219d_ : T \u2192 T \u2192 Set a) \u2192 Set (t \u2294 a)\nComposable _\u219d_ = IComposable {i = zero} {_\u219d\u1d62_ = ConstArr \u22a4} _ (const _\u219d_)\n\n{- Composable, unfolded:\nrecord Composable {t a} {T : Set t} (_\u219d_ : T \u2192 T \u2192 Set a) : Set (t \u2294 a) where\n constructor mk\n infixr 1 _>>>_\n field\n _>>>_ : \u2200 {A B C} \u2192 (A \u219d B) \u2192 (B \u219d C) \u2192 (A \u219d C)\n-}\n\nconstComp' : \u2200 {a} {A : Set a} (_\u00b7_ : A \u2192 A \u2192 A) \u2192 Composition (ConstArr A)\nconstComp' _\u00b7_ = _\u00b7_\n\nconstComp : \u2200 {a} {A : Set a} (_\u00b7_ : A \u2192 A \u2192 A) \u2192 Composable (ConstArr A)\nconstComp _\u00b7_ = mk _\u00b7_\n\nmodule Composable = IComposable\n\nixFunComp : \u2200 {ix t} {Ix : Set ix} (F : Ix \u2192 Set t) \u2192 Composable (\u03bb i o \u2192 F i \u2192 F o)\nixFunComp _ = mk (\u03bb f g x \u2192 g (f x))\n\nfunComp : \u2200 {t} \u2192 Composable (\u03bb (A B : Set t) \u2192 A \u2192 B)\nfunComp = ixFunComp id\n\nopComp : \u2200 {t a} {T : Set t} {_\u219d_ : T \u2192 T \u2192 Set a} \u2192 Composable _\u219d_ \u2192 Composable (flip _\u219d_)\nopComp (mk _>>>_) = mk (flip _>>>_)\n\nopen import Data.Vec\nvecFunComp : \u2200 {a} (A : Set a) \u2192 Composable (\u03bb i o \u2192 Vec A i \u2192 Vec A o)\nvecFunComp A = ixFunComp (Vec A)\n\nopen import Data.Bits\nbitsFunComp : Composable (\u03bb i o \u2192 Bits i \u2192 Bits o)\nbitsFunComp = ixFunComp Bits\n\n-- open import Data.Fin\n-- funRewireComp : Composable (\u03bb i o \u2192 Fin o \u2192 Fin i)\n-- FunRewireComp = opComp (ixFunComp Fin)\n\n{-\nopen import bintree\n\nopen import Data.Nat\nCircuitType : Set\u2081\nCircuitType = (i o : \u2115) \u2192 Set\n\nRewireTbl : CircuitType\nRewireTbl i o = Vec (Fin i) o\n\nrewireTblComp : Composable RewireTbl\nrewireTblComp = {!!}\n-}\n","old_contents":"module composable where\n\nopen import Level\nopen import Function\nopen import Data.Unit using (\u22a4)\n\n-- Monoid M \u2245 Cat (ConstArr M)\n\nComposition : \u2200 {i a} {I : Set i} (_\u219d_ : I \u2192 I \u2192 Set a) \u2192 Set (i \u2294 a)\nComposition _\u219d_ = \u2200 {A B C} \u2192 (A \u219d B) \u2192 (B \u219d C) \u2192 (A \u219d C)\n\nrecord IComposable {i j s t} {I : Set i} {_\u219d\u1d62_ : I \u2192 I \u2192 Set j} {S : I \u2192 Set s}\n (_\u00b7_ : Composition _\u219d\u1d62_)\n (\u27e8_\u27e9_\u219d_ : \u2200 {i\u2080 i\u2081} \u2192 i\u2080 \u219d\u1d62 i\u2081 \u2192 S i\u2080 \u2192 S i\u2081 \u2192 Set t) : Set (t \u2294 s \u2294 i \u2294 j) where\n constructor mk\n infixr 1 _>>>_\n field\n _>>>_ : \u2200 {i\u2080 i\u2081 i\u2082} {ix\u2080 : i\u2080 \u219d\u1d62 i\u2081} {ix\u2081 : i\u2081 \u219d\u1d62 i\u2082} {A B C}\n \u2192 (\u27e8 ix\u2080 \u27e9 A \u219d B) \u2192 (\u27e8 ix\u2081 \u27e9 B \u219d C) \u2192 (\u27e8 ix\u2080 \u00b7 ix\u2081 \u27e9 A \u219d C)\n\nopen import Data.Unit using (\u22a4)\n\nConstArr : \u2200 {a} (A : Set a) \u2192 \u22a4 \u2192 \u22a4 \u2192 Set a\nConstArr A _ _ = A\n\nComposable : \u2200 {s t} {S : Set s} (_\u219d_ : S \u2192 S \u2192 Set t) \u2192 Set (s \u2294 t)\nComposable _\u219d_ = IComposable {i = zero} {_\u219d\u1d62_ = ConstArr \u22a4} _ (const _\u219d_)\n\n{- Composable, unfolded:\nrecord Composable {s t} {S : Set s} (_\u219d_ : S \u2192 S \u2192 Set t) : Set (s \u2294 t) where\n constructor mk\n infixr 1 _>>>_\n field\n _>>>_ : \u2200 {A B C : S} \u2192 (A \u219d B) \u2192 (B \u219d C) \u2192 (A \u219d C)\n-}\n\nconstComp' : \u2200 {a} {A : Set a} (_\u00b7_ : A \u2192 A \u2192 A) \u2192 Composition (ConstArr A)\nconstComp' _\u00b7_ = _\u00b7_\n\nconstComp : \u2200 {a} {A : Set a} (_\u00b7_ : A \u2192 A \u2192 A) \u2192 Composable (ConstArr A)\nconstComp _\u00b7_ = mk _\u00b7_\n\nmodule Composable = IComposable\n\nixFunComp : \u2200 {ix s} {Ix : Set ix} (F : Ix \u2192 Set s) \u2192 Composable (\u03bb i o \u2192 F i \u2192 F o)\nixFunComp _ = mk (\u03bb f g x \u2192 g (f x))\n\nfunComp : \u2200 {s} \u2192 Composable (\u03bb (A B : Set s) \u2192 A \u2192 B)\nfunComp = ixFunComp id\n\nopComp : \u2200 {s t} {S : Set s} {_\u219d_ : S \u2192 S \u2192 Set t} \u2192 Composable _\u219d_ \u2192 Composable (flip _\u219d_)\nopComp (mk _>>>_) = mk (flip _>>>_)\n\nopen import Data.Vec\nvecFunComp : \u2200 {a} (A : Set a) \u2192 Composable (\u03bb i o \u2192 Vec A i \u2192 Vec A o)\nvecFunComp A = ixFunComp (Vec A)\n\nopen import Data.Bits\nbitsFunComp : Composable (\u03bb i o \u2192 Bits i \u2192 Bits o)\nbitsFunComp = ixFunComp Bits\n\n-- open import Data.Fin\n-- funRewireComp : Composable (\u03bb i o \u2192 Fin o \u2192 Fin i)\n-- FunRewireComp = opComp (ixFunComp Fin)\n\n{-\nopen import bintree\n\nopen import Data.Nat\nCircuitType : Set\u2081\nCircuitType = (i o : \u2115) \u2192 Set\n\nRewireTbl : CircuitType\nRewireTbl i o = Vec (Fin i) o\n\nrewireTblComp : Composable RewireTbl\nrewireTblComp = {!!}\n-}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"ea35ca82c47da6c485dee266192a700be4ab6a64","subject":"Inline \u0394Type.","message":"Inline \u0394Type.\n\nOld-commit-hash: 01465077ba29a751a7f67c946f2516def3753a94\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Term.agda","new_file":"Parametric\/Change\/Term.agda","new_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Constant\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\nmodule Structure where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n lift-diff-apply :\n (\u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base (\u0394Base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4) \u00d7 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-diff-apply diff apply {base \u03b9} = diff , apply\n lift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\n lift-diff :\n (\u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base (\u0394Base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n\n lift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n lift-apply :\n (\u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base (\u0394Base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Constant\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\nmodule Structure where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n lift-diff-apply :\n (\u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394Type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4) \u00d7 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-diff-apply diff apply {base \u03b9} = diff , apply\n lift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\n lift-diff :\n (\u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394Type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n\n lift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n lift-apply :\n (\u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394Type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"5957a0a5f21ebc9ad88ea001c4afa6b381372c04","subject":"Document P.C.Type.","message":"Document P.C.Type.\n\nOld-commit-hash: 11e38d018843f21411d014020ce42181e1f77d32\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Type.agda","new_file":"Parametric\/Change\/Type.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Simply-typed changes (Fig. 3 and Fig. 4d)\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\n\nmodule Parametric.Change.Type\n (Base : Type.Structure)\n where\n\nopen Type.Structure Base\n\n-- Extension point: Simply-typed changes of base types.\nStructure : Set\nStructure = Base \u2192 Base\n\nmodule Structure (\u0394Base : Structure) where\n -- We provide: Simply-typed changes on simple types.\n \u0394Type : Type \u2192 Type\n \u0394Type (base \u03b9) = base (\u0394Base \u03b9)\n \u0394Type (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394Type \u03c3 \u21d2 \u0394Type \u03c4\n\n -- And we also provide context merging.\n open import Base.Change.Context \u0394Type public\n","old_contents":"import Parametric.Syntax.Type as Type\n\nmodule Parametric.Change.Type\n (Base : Type.Structure)\n where\n\nopen Type.Structure Base\n\nStructure : Set\nStructure = Base \u2192 Base\n\nmodule Structure (\u0394Base : Structure) where\n \u0394Type : Type \u2192 Type\n \u0394Type (base \u03b9) = base (\u0394Base \u03b9)\n \u0394Type (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394Type \u03c3 \u21d2 \u0394Type \u03c4\n\n open import Base.Change.Context \u0394Type public\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"22c2c8987fb43682069ff83b47bf139b730261ee","subject":"Data.Maybe.NP: export monad plus in M?","message":"Data.Maybe.NP: export monad plus in M?\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Maybe\/NP.agda","new_file":"lib\/Data\/Maybe\/NP.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Maybe.NP where\n\nopen import Function\nimport Level as L\nopen L using (_\u2294_; lift)\nopen import Data.Maybe public\nopen import Category.Applicative\nimport Category.Monad as Cat\nopen import Relation.Binary.PropositionalEquality as \u2261 using (_\u2261_;_\u2257_)\nopen import Relation.Binary.Logical\nopen import Function using (type-signature;_$_;flip;id)\nopen import Data.Empty using (\u22a5)\nopen import Data.Unit using (\u22a4)\nopen import Data.Nat using (\u2115; zero; suc)\n\nmodule M? \u2113 where\n open Cat.RawMonadPlus (monadPlus {\u2113}) public\n\n\u03a0? : \u2200 {a b} (A : Set a) (B : A \u2192 Set b) \u2192 Set _\n\u03a0? A B = (x : A) \u2192 Maybe (B x)\n\n_\u2192?_ : \u2200 {a b} \u2192 Set a \u2192 Set b \u2192 Set _\nA \u2192? B = A \u2192 Maybe B\n\nmap? : \u2200 {a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 Maybe A \u2192 Maybe B\nmap? f = maybe (just \u2218\u2032 f) nothing\n-- map? = M?._<$>_ _ <= not universe-polymorphic enough\n\n-- more universe-polymorphic than M?.join\njoin? : \u2200 {a} {A : Set a} \u2192 Maybe (Maybe A) \u2192 Maybe A\njoin? nothing = nothing\njoin? (just x) = x\n\nMaybe^ : \u2115 \u2192 Set \u2192 Set\nMaybe^ zero = id\nMaybe^ (suc n) = Maybe \u2218 Maybe^ n\n\njust-injective : \u2200 {a} {A : Set a} {x y : A}\n \u2192 (just x \u2236 Maybe A) \u2261 just y \u2192 x \u2261 y\njust-injective \u2261.refl = \u2261.refl\n\nmaybe-just-nothing : \u2200 {a} {A : Set a} \u2192 maybe {A = A} just nothing \u2257 id\nmaybe-just-nothing (just _) = \u2261.refl\nmaybe-just-nothing nothing = \u2261.refl\n\napplicative : \u2200 {f} \u2192 RawApplicative {f} Maybe\napplicative = record { pure = just ; _\u229b_ = _\u229b_ }\n where\n _\u229b_ : \u2200 {a b}{A : Set a}{B : Set b} \u2192 Maybe (A \u2192 B) \u2192 Maybe A \u2192 Maybe B\n just f \u229b just x = just (f x)\n _ \u229b _ = nothing\n\njust? : \u2200 {a} {A : Set a} \u2192 Maybe A \u2192 Set\njust? nothing = \u22a5\njust? (just _) = \u22a4\n\ndata \u27e6Maybe\u27e7 {a b r} {A : Set a} {B : Set b} (_\u223c_ : A \u2192 B \u2192 Set r) : Maybe A \u2192 Maybe B \u2192 Set (a \u2294 b \u2294 r) where\n \u27e6just\u27e7 : \u2200 {x\u2081 x\u2082} \u2192 (x\u1d63 : x\u2081 \u223c x\u2082) \u2192 \u27e6Maybe\u27e7 _\u223c_ (just x\u2081) (just x\u2082)\n \u27e6nothing\u27e7 : \u27e6Maybe\u27e7 _\u223c_ nothing nothing\n\n\u27e6maybe\u27e7 : \u2200 {a b} \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 a \u27e9\u27e6\u2192\u27e7 (\u2200\u27e8 B\u1d63 \u2236 \u27e6Set\u27e7 b \u27e9\u27e6\u2192\u27e7 ((A\u1d63 \u27e6\u2192\u27e7 B\u1d63) \u27e6\u2192\u27e7 (B\u1d63 \u27e6\u2192\u27e7 (\u27e6Maybe\u27e7 A\u1d63 \u27e6\u2192\u27e7 B\u1d63)))))\n (maybe {a} {b}) (maybe {a} {b})\n\u27e6maybe\u27e7 _ _ just\u1d63 nothing\u1d63 (\u27e6just\u27e7 x\u1d63) = just\u1d63 x\u1d63\n\u27e6maybe\u27e7 _ _ just\u1d63 nothing\u1d63 \u27e6nothing\u27e7 = nothing\u1d63\n\n_\u27e6\u2192?\u27e7_ : \u2200 {a b c d} \u2192 (\u27e6Set\u27e7 {a} {b} c \u27e6\u2192\u27e7 \u27e6Set\u27e7 {a} {b} d \u27e6\u2192\u27e7 \u27e6Set\u27e7 _) _\u2192?_ _\u2192?_\nA\u1d63 \u27e6\u2192?\u27e7 B\u1d63 = A\u1d63 \u27e6\u2192\u27e7 \u27e6Maybe\u27e7 B\u1d63\n\nmodule \u27e6Maybe\u27e7-Properties where\n\n refl : \u2200 {a} {A : Set a} {_\u223c_ : A \u2192 A \u2192 Set} (refl-A : \u2200 x \u2192 x \u223c x) (mx : Maybe A) \u2192 \u27e6Maybe\u27e7 _\u223c_ mx mx\n refl refl-A (just x) = \u27e6just\u27e7 (refl-A x)\n refl refl-A nothing = \u27e6nothing\u27e7\n\n sym : \u2200 {a b} {A : Set a} {B : Set b} {_\u223c\u2081_ : A \u2192 B \u2192 Set} {_\u223c\u2082_ : B \u2192 A \u2192 Set}\n (sym-AB : \u2200 {x y} \u2192 x \u223c\u2081 y \u2192 y \u223c\u2082 x) {mx : Maybe A} {my : Maybe B}\n \u2192 \u27e6Maybe\u27e7 _\u223c\u2081_ mx my \u2192 \u27e6Maybe\u27e7 _\u223c\u2082_ my mx\n sym sym-A (\u27e6just\u27e7 x\u223c\u2081y) = \u27e6just\u27e7 (sym-A x\u223c\u2081y)\n sym sym-A \u27e6nothing\u27e7 = \u27e6nothing\u27e7\n\n trans : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c}\n {_\u27e6AB\u27e7_ : A \u2192 B \u2192 Set}\n {_\u27e6BC\u27e7_ : B \u2192 C \u2192 Set}\n {_\u27e6AC\u27e7_ : A \u2192 C \u2192 Set}\n (trans : \u2200 {x y z} \u2192 x \u27e6AB\u27e7 y \u2192 y \u27e6BC\u27e7 z \u2192 x \u27e6AC\u27e7 z)\n {mx : Maybe A} {my : Maybe B} {mz : Maybe C}\n \u2192 \u27e6Maybe\u27e7 _\u27e6AB\u27e7_ mx my \u2192 \u27e6Maybe\u27e7 _\u27e6BC\u27e7_ my mz\n \u2192 \u27e6Maybe\u27e7 _\u27e6AC\u27e7_ mx mz\n trans trans' (\u27e6just\u27e7 x\u223cy) (\u27e6just\u27e7 y\u223cz) = \u27e6just\u27e7 (trans' x\u223cy y\u223cz)\n trans trans' \u27e6nothing\u27e7 \u27e6nothing\u27e7 = \u27e6nothing\u27e7\n\n subst-\u27e6AB\u27e7 : \u2200 {a b} {A : Set a} {B : Set b}\n (P : Maybe A \u2192 Set)\n (Q : Maybe B \u2192 Set)\n (\u27e6AB\u27e7 : A \u2192 B \u2192 Set)\n (subst-\u27e6AB\u27e7 : \u2200 {x y} \u2192 \u27e6AB\u27e7 x y \u2192 P (just x) \u2192 Q (just y))\n (Pnothing\u2192Qnothing : P nothing \u2192 Q nothing)\n {mx : Maybe A} {my : Maybe B}\n \u2192 (\u27e6Maybe\u27e7 \u27e6AB\u27e7 mx my) \u2192 P mx \u2192 Q my\n subst-\u27e6AB\u27e7 _ _ _ subst-\u27e6AB\u27e7 _ (\u27e6just\u27e7 x\u223cy) Pmx = subst-\u27e6AB\u27e7 x\u223cy Pmx\n subst-\u27e6AB\u27e7 _ _ _ _ f \u27e6nothing\u27e7 Pnothing = f Pnothing\n\n subst : \u2200 {a} {A : Set a}\n (P : Maybe A \u2192 Set)\n (A\u1d63 : A \u2192 A \u2192 Set)\n (subst-A\u1d63 : \u2200 {x y} \u2192 A\u1d63 x y \u2192 P (just x) \u2192 P (just y))\n {mx my}\n \u2192 (\u27e6Maybe\u27e7 A\u1d63 mx my) \u2192 P mx \u2192 P my\n subst P A\u1d63 subst-A\u1d63 = subst-\u27e6AB\u27e7 P P A\u1d63 subst-A\u1d63 id\n\nIsNothing'\u2261nothing : \u2200 {a} {A : Set a} {x : Maybe A} \u2192 IsNothing x \u2192 x \u2261 nothing\nIsNothing'\u2261nothing nothing = \u2261.refl\nIsNothing'\u2261nothing (just (lift ()))\n\n\u2261nothing'IsNothing : \u2200 {a} {A : Set a} {x : Maybe A} \u2192 x \u2261 nothing \u2192 IsNothing x\n\u2261nothing'IsNothing \u2261.refl = nothing\n\n_\u2261JAll_ : \u2200 {a} {A : Set a} (x y : Maybe A) \u2192 Set a\nx \u2261JAll y = All (\u03bb y' \u2192 All (_\u2261_ y') y) x\n\n_\u2261JAny_ : \u2200 {a} {A : Set a} (x y : Maybe A) \u2192 Set a\nx \u2261JAny y = Any (\u03bb y' \u2192 Any (_\u2261_ y') y) x\n\nmodule MonadLemmas where\n\n open M? L.zero public\n -- open RawApplicative applicative public\n cong-Maybe : \u2200 {A B : Set}\n -- cong-Maybe : \u2200 {a b} {A : Set a} {B : Set b}\n (f : A \u2192 B) {x y} \u2192 x \u2261 pure y \u2192 f <$> x \u2261 pure (f y)\n cong-Maybe f \u2261.refl = \u2261.refl\n\n cong\u2082-Maybe : \u2200 {A B C : Set}\n (f : A \u2192 B \u2192 C) {x y u v} \u2192 x \u2261 pure y \u2192 u \u2261 pure v \u2192 pure f \u229b x \u229b u \u2261 pure (f y v)\n cong\u2082-Maybe f \u2261.refl \u2261.refl = \u2261.refl\n\n Maybe-comm-monad :\n \u2200 {A B C} {x y} {f : A \u2192 B \u2192 Maybe C} \u2192\n (x >>= \u03bb x' \u2192 y >>= \u03bb y' \u2192 f x' y')\n \u2261 (y >>= \u03bb y' \u2192 x >>= \u03bb x' \u2192 f x' y')\n Maybe-comm-monad {x = nothing} {nothing} = \u2261.refl\n Maybe-comm-monad {x = nothing} {just _} = \u2261.refl\n Maybe-comm-monad {x = just _} {nothing} = \u2261.refl\n Maybe-comm-monad {x = just _} {just _} = \u2261.refl\n\n Maybe-comm-appl : \u2200 {A B} {f : Maybe (A \u2192 B)} {x} \u2192 f \u229b x \u2261 (flip _$_) <$> x \u229b f\n Maybe-comm-appl {f = nothing} {nothing} = \u2261.refl\n Maybe-comm-appl {f = nothing} {just _} = \u2261.refl\n Maybe-comm-appl {f = just _} {nothing} = \u2261.refl\n Maybe-comm-appl {f = just _} {just _} = \u2261.refl\n\n Maybe-comm-appl\u2082 : \u2200 {A B C} {f : A \u2192 B \u2192 C} {x y} \u2192 f <$> x \u229b y \u2261 flip f <$> y \u229b x\n Maybe-comm-appl\u2082 {x = nothing} {nothing} = \u2261.refl\n Maybe-comm-appl\u2082 {x = nothing} {just _} = \u2261.refl\n Maybe-comm-appl\u2082 {x = just _} {nothing} = \u2261.refl\n Maybe-comm-appl\u2082 {x = just _} {just _} = \u2261.refl\n\nmodule FunctorLemmas where\n open M? L.zero\n\n <$>-injective\u2081 : \u2200 {A B : Set}\n {f : A \u2192 B} {x y : Maybe A}\n (f-inj : \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y)\n \u2192 f <$> x \u2261 f <$> y \u2192 x \u2261 y\n <$>-injective\u2081 {x = just _} {just _} f-inj eq = \u2261.cong just (f-inj (just-injective eq))\n <$>-injective\u2081 {x = nothing} {nothing} _ _ = \u2261.refl\n <$>-injective\u2081 {x = just _} {nothing} _ ()\n <$>-injective\u2081 {x = nothing} {just _} _ ()\n\n <$>-assoc : \u2200 {A B C : Set} {f : A \u2192 B} {g : C \u2192 A} (x : Maybe C) \u2192 f \u2218 g <$> x \u2261 f <$> (g <$> x)\n <$>-assoc (just _) = \u2261.refl\n <$>-assoc nothing = \u2261.refl\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Maybe.NP where\n\nopen import Function\nimport Level as L\nopen L using (_\u2294_; lift)\nopen import Data.Maybe public\nopen import Category.Applicative\nimport Category.Monad as Cat\nopen import Relation.Binary.PropositionalEquality as \u2261 using (_\u2261_;_\u2257_)\nopen import Relation.Binary.Logical\nopen import Function using (type-signature;_$_;flip;id)\nopen import Data.Empty using (\u22a5)\nopen import Data.Unit using (\u22a4)\nopen import Data.Nat using (\u2115; zero; suc)\n\nmodule M? \u2113 where\n open Cat.RawMonad (monad {\u2113}) public\n\n\u03a0? : \u2200 {a b} (A : Set a) (B : A \u2192 Set b) \u2192 Set _\n\u03a0? A B = (x : A) \u2192 Maybe (B x)\n\n_\u2192?_ : \u2200 {a b} \u2192 Set a \u2192 Set b \u2192 Set _\nA \u2192? B = A \u2192 Maybe B\n\nmap? : \u2200 {a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 Maybe A \u2192 Maybe B\nmap? f = maybe (just \u2218\u2032 f) nothing\n-- map? = M?._<$>_ _ <= not universe-polymorphic enough\n\n-- more universe-polymorphic than M?.join\njoin? : \u2200 {a} {A : Set a} \u2192 Maybe (Maybe A) \u2192 Maybe A\njoin? nothing = nothing\njoin? (just x) = x\n\nMaybe^ : \u2115 \u2192 Set \u2192 Set\nMaybe^ zero = id\nMaybe^ (suc n) = Maybe \u2218 Maybe^ n\n\njust-injective : \u2200 {a} {A : Set a} {x y : A}\n \u2192 (just x \u2236 Maybe A) \u2261 just y \u2192 x \u2261 y\njust-injective \u2261.refl = \u2261.refl\n\nmaybe-just-nothing : \u2200 {a} {A : Set a} \u2192 maybe {A = A} just nothing \u2257 id\nmaybe-just-nothing (just _) = \u2261.refl\nmaybe-just-nothing nothing = \u2261.refl\n\napplicative : \u2200 {f} \u2192 RawApplicative {f} Maybe\napplicative = record { pure = just ; _\u229b_ = _\u229b_ }\n where\n _\u229b_ : \u2200 {a b}{A : Set a}{B : Set b} \u2192 Maybe (A \u2192 B) \u2192 Maybe A \u2192 Maybe B\n just f \u229b just x = just (f x)\n _ \u229b _ = nothing\n\njust? : \u2200 {a} {A : Set a} \u2192 Maybe A \u2192 Set\njust? nothing = \u22a5\njust? (just _) = \u22a4\n\ndata \u27e6Maybe\u27e7 {a b r} {A : Set a} {B : Set b} (_\u223c_ : A \u2192 B \u2192 Set r) : Maybe A \u2192 Maybe B \u2192 Set (a \u2294 b \u2294 r) where\n \u27e6just\u27e7 : \u2200 {x\u2081 x\u2082} \u2192 (x\u1d63 : x\u2081 \u223c x\u2082) \u2192 \u27e6Maybe\u27e7 _\u223c_ (just x\u2081) (just x\u2082)\n \u27e6nothing\u27e7 : \u27e6Maybe\u27e7 _\u223c_ nothing nothing\n\n\u27e6maybe\u27e7 : \u2200 {a b} \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 a \u27e9\u27e6\u2192\u27e7 (\u2200\u27e8 B\u1d63 \u2236 \u27e6Set\u27e7 b \u27e9\u27e6\u2192\u27e7 ((A\u1d63 \u27e6\u2192\u27e7 B\u1d63) \u27e6\u2192\u27e7 (B\u1d63 \u27e6\u2192\u27e7 (\u27e6Maybe\u27e7 A\u1d63 \u27e6\u2192\u27e7 B\u1d63)))))\n (maybe {a} {b}) (maybe {a} {b})\n\u27e6maybe\u27e7 _ _ just\u1d63 nothing\u1d63 (\u27e6just\u27e7 x\u1d63) = just\u1d63 x\u1d63\n\u27e6maybe\u27e7 _ _ just\u1d63 nothing\u1d63 \u27e6nothing\u27e7 = nothing\u1d63\n\n_\u27e6\u2192?\u27e7_ : \u2200 {a b c d} \u2192 (\u27e6Set\u27e7 {a} {b} c \u27e6\u2192\u27e7 \u27e6Set\u27e7 {a} {b} d \u27e6\u2192\u27e7 \u27e6Set\u27e7 _) _\u2192?_ _\u2192?_\nA\u1d63 \u27e6\u2192?\u27e7 B\u1d63 = A\u1d63 \u27e6\u2192\u27e7 \u27e6Maybe\u27e7 B\u1d63\n\nmodule \u27e6Maybe\u27e7-Properties where\n\n refl : \u2200 {a} {A : Set a} {_\u223c_ : A \u2192 A \u2192 Set} (refl-A : \u2200 x \u2192 x \u223c x) (mx : Maybe A) \u2192 \u27e6Maybe\u27e7 _\u223c_ mx mx\n refl refl-A (just x) = \u27e6just\u27e7 (refl-A x)\n refl refl-A nothing = \u27e6nothing\u27e7\n\n sym : \u2200 {a b} {A : Set a} {B : Set b} {_\u223c\u2081_ : A \u2192 B \u2192 Set} {_\u223c\u2082_ : B \u2192 A \u2192 Set}\n (sym-AB : \u2200 {x y} \u2192 x \u223c\u2081 y \u2192 y \u223c\u2082 x) {mx : Maybe A} {my : Maybe B}\n \u2192 \u27e6Maybe\u27e7 _\u223c\u2081_ mx my \u2192 \u27e6Maybe\u27e7 _\u223c\u2082_ my mx\n sym sym-A (\u27e6just\u27e7 x\u223c\u2081y) = \u27e6just\u27e7 (sym-A x\u223c\u2081y)\n sym sym-A \u27e6nothing\u27e7 = \u27e6nothing\u27e7\n\n trans : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c}\n {_\u27e6AB\u27e7_ : A \u2192 B \u2192 Set}\n {_\u27e6BC\u27e7_ : B \u2192 C \u2192 Set}\n {_\u27e6AC\u27e7_ : A \u2192 C \u2192 Set}\n (trans : \u2200 {x y z} \u2192 x \u27e6AB\u27e7 y \u2192 y \u27e6BC\u27e7 z \u2192 x \u27e6AC\u27e7 z)\n {mx : Maybe A} {my : Maybe B} {mz : Maybe C}\n \u2192 \u27e6Maybe\u27e7 _\u27e6AB\u27e7_ mx my \u2192 \u27e6Maybe\u27e7 _\u27e6BC\u27e7_ my mz\n \u2192 \u27e6Maybe\u27e7 _\u27e6AC\u27e7_ mx mz\n trans trans' (\u27e6just\u27e7 x\u223cy) (\u27e6just\u27e7 y\u223cz) = \u27e6just\u27e7 (trans' x\u223cy y\u223cz)\n trans trans' \u27e6nothing\u27e7 \u27e6nothing\u27e7 = \u27e6nothing\u27e7\n\n subst-\u27e6AB\u27e7 : \u2200 {a b} {A : Set a} {B : Set b}\n (P : Maybe A \u2192 Set)\n (Q : Maybe B \u2192 Set)\n (\u27e6AB\u27e7 : A \u2192 B \u2192 Set)\n (subst-\u27e6AB\u27e7 : \u2200 {x y} \u2192 \u27e6AB\u27e7 x y \u2192 P (just x) \u2192 Q (just y))\n (Pnothing\u2192Qnothing : P nothing \u2192 Q nothing)\n {mx : Maybe A} {my : Maybe B}\n \u2192 (\u27e6Maybe\u27e7 \u27e6AB\u27e7 mx my) \u2192 P mx \u2192 Q my\n subst-\u27e6AB\u27e7 _ _ _ subst-\u27e6AB\u27e7 _ (\u27e6just\u27e7 x\u223cy) Pmx = subst-\u27e6AB\u27e7 x\u223cy Pmx\n subst-\u27e6AB\u27e7 _ _ _ _ f \u27e6nothing\u27e7 Pnothing = f Pnothing\n\n subst : \u2200 {a} {A : Set a}\n (P : Maybe A \u2192 Set)\n (A\u1d63 : A \u2192 A \u2192 Set)\n (subst-A\u1d63 : \u2200 {x y} \u2192 A\u1d63 x y \u2192 P (just x) \u2192 P (just y))\n {mx my}\n \u2192 (\u27e6Maybe\u27e7 A\u1d63 mx my) \u2192 P mx \u2192 P my\n subst P A\u1d63 subst-A\u1d63 = subst-\u27e6AB\u27e7 P P A\u1d63 subst-A\u1d63 id\n\nIsNothing'\u2261nothing : \u2200 {a} {A : Set a} {x : Maybe A} \u2192 IsNothing x \u2192 x \u2261 nothing\nIsNothing'\u2261nothing nothing = \u2261.refl\nIsNothing'\u2261nothing (just (lift ()))\n\n\u2261nothing'IsNothing : \u2200 {a} {A : Set a} {x : Maybe A} \u2192 x \u2261 nothing \u2192 IsNothing x\n\u2261nothing'IsNothing \u2261.refl = nothing\n\n_\u2261JAll_ : \u2200 {a} {A : Set a} (x y : Maybe A) \u2192 Set a\nx \u2261JAll y = All (\u03bb y' \u2192 All (_\u2261_ y') y) x\n\n_\u2261JAny_ : \u2200 {a} {A : Set a} (x y : Maybe A) \u2192 Set a\nx \u2261JAny y = Any (\u03bb y' \u2192 Any (_\u2261_ y') y) x\n\nmodule MonadLemmas where\n\n open M? L.zero public\n -- open RawApplicative applicative public\n cong-Maybe : \u2200 {A B : Set}\n -- cong-Maybe : \u2200 {a b} {A : Set a} {B : Set b}\n (f : A \u2192 B) {x y} \u2192 x \u2261 pure y \u2192 f <$> x \u2261 pure (f y)\n cong-Maybe f \u2261.refl = \u2261.refl\n\n cong\u2082-Maybe : \u2200 {A B C : Set}\n (f : A \u2192 B \u2192 C) {x y u v} \u2192 x \u2261 pure y \u2192 u \u2261 pure v \u2192 pure f \u229b x \u229b u \u2261 pure (f y v)\n cong\u2082-Maybe f \u2261.refl \u2261.refl = \u2261.refl\n\n Maybe-comm-monad :\n \u2200 {A B C} {x y} {f : A \u2192 B \u2192 Maybe C} \u2192\n (x >>= \u03bb x' \u2192 y >>= \u03bb y' \u2192 f x' y')\n \u2261 (y >>= \u03bb y' \u2192 x >>= \u03bb x' \u2192 f x' y')\n Maybe-comm-monad {x = nothing} {nothing} = \u2261.refl\n Maybe-comm-monad {x = nothing} {just _} = \u2261.refl\n Maybe-comm-monad {x = just _} {nothing} = \u2261.refl\n Maybe-comm-monad {x = just _} {just _} = \u2261.refl\n\n Maybe-comm-appl : \u2200 {A B} {f : Maybe (A \u2192 B)} {x} \u2192 f \u229b x \u2261 (flip _$_) <$> x \u229b f\n Maybe-comm-appl {f = nothing} {nothing} = \u2261.refl\n Maybe-comm-appl {f = nothing} {just _} = \u2261.refl\n Maybe-comm-appl {f = just _} {nothing} = \u2261.refl\n Maybe-comm-appl {f = just _} {just _} = \u2261.refl\n\n Maybe-comm-appl\u2082 : \u2200 {A B C} {f : A \u2192 B \u2192 C} {x y} \u2192 f <$> x \u229b y \u2261 flip f <$> y \u229b x\n Maybe-comm-appl\u2082 {x = nothing} {nothing} = \u2261.refl\n Maybe-comm-appl\u2082 {x = nothing} {just _} = \u2261.refl\n Maybe-comm-appl\u2082 {x = just _} {nothing} = \u2261.refl\n Maybe-comm-appl\u2082 {x = just _} {just _} = \u2261.refl\n\nmodule FunctorLemmas where\n open M? L.zero\n\n <$>-injective\u2081 : \u2200 {A B : Set}\n {f : A \u2192 B} {x y : Maybe A}\n (f-inj : \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y)\n \u2192 f <$> x \u2261 f <$> y \u2192 x \u2261 y\n <$>-injective\u2081 {x = just _} {just _} f-inj eq = \u2261.cong just (f-inj (just-injective eq))\n <$>-injective\u2081 {x = nothing} {nothing} _ _ = \u2261.refl\n <$>-injective\u2081 {x = just _} {nothing} _ ()\n <$>-injective\u2081 {x = nothing} {just _} _ ()\n\n <$>-assoc : \u2200 {A B C : Set} {f : A \u2192 B} {g : C \u2192 A} (x : Maybe C) \u2192 f \u2218 g <$> x \u2261 f <$> (g <$> x)\n <$>-assoc (just _) = \u2261.refl\n <$>-assoc nothing = \u2261.refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"27037592477c7d9112554d56337e06c5a2c6f0b9","subject":"Remove remaining duplication between abs\u2081 ... abs\u2086","message":"Remove remaining duplication between abs\u2081 ... abs\u2086\n\nThis relies on Agda 2.4.0's varying arity in a more essential way.\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Syntax\/Term.agda","new_file":"Parametric\/Syntax\/Term.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- The syntax of terms (Fig. 1a and 1b).\n------------------------------------------------------------------------\n\n-- The syntax of terms depends on the syntax of simple types\n-- (because terms are indexed by types in order to rule out\n-- ill-typed terms). But we are in the Parametric.* hierarchy, so\n-- we don't know the full syntax of types, only how to lift the\n-- syntax of base types into the syntax of simple types. This\n-- means that we have to be parametric in the syntax of base\n-- types, too.\n--\n-- In such parametric modules that depend on other parametric\n-- modules, we first import our dependencies under a more\n-- convenient name.\n\nimport Parametric.Syntax.Type as Type\n\n-- Then we start the module proper, with parameters for all\n-- extension points of our dependencies. Note that here, the\n-- \"Structure\" naming convenion makes some sense, because we can\n-- say that we need some \"Type.Structure\" in order to define the\n-- \"Term.Structure\".\n\nmodule Parametric.Syntax.Term\n (Base : Type.Structure)\n where\n\n-- Now inside the module, we can open our dependencies with the\n-- parameters for their extension points. Again, here the name\n-- \"Structure\" makes some sense, because we can say that we want\n-- to access the \"Type.Structure\" that is induced by Base.\n\nopen Type.Structure Base\n\n-- At this point, we have dealt with the extension points of our\n-- dependencies, and we have all the definitions about simple\n-- types, contexts, variables, and variable sets in scope that we\n-- provided in Parametric.Syntax.Type. Now we can proceed to\n-- define our own extension point, following the pattern\n-- explained in Parametric.Syntax.Type.\n\nopen import Relation.Binary.PropositionalEquality hiding ([_])\nopen import Function using (_\u2218_)\nopen import Data.Unit\nopen import Data.Sum\n\n-- Our extension point is a set of primitives, indexed by the\n-- types of their arguments and their return type. In general, if\n-- you're confused about what an extension point means, you might\n-- want to open the corresponding module in the Nehemiah\n-- hierarchy to see how it is implemented in the example\n-- plugin. In this case, that would be the Nehemiah.Syntax.Term\n-- module.\n\nStructure : Set\u2081\nStructure = Context \u2192 Type \u2192 Set\n\nmodule Structure (Const : Structure) where\n import Base.Data.DependentList as DependentList\n open DependentList public using (\u2205 ; _\u2022_)\n open DependentList\n\n -- Declarations of Term and Terms to enable mutual recursion.\n --\n -- Note that terms are indexed by contexts and types. In the\n -- paper, we define the abstract syntax of terms in Fig 1a and\n -- then define a type system in Fig 1b. All lemmas and theorems\n -- then explicitly specify that they only hold for well-typed\n -- terms. Here, we use the indices to define a type that can\n -- only hold well-typed terms in the first place.\n data Term\n (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set\n\n -- (Terms \u0393 \u03a3) represents a list of terms with types from \u03a3\n -- with free variables bound in \u0393.\n Terms : Context \u2192 Context \u2192 Set\n Terms \u0393 = DependentList (Term \u0393)\n\n -- (Term \u0393 \u03c4) represents a term of type \u03c4\n -- with free variables bound in \u0393.\n data Term \u0393 where\n -- constants aka. primitives can only occur fully applied.\n const : \u2200 {\u03a3 \u03c4} \u2192\n (c : Const \u03a3 \u03c4) \u2192\n (args : Terms \u0393 \u03a3) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indicies, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\n -- Free variables\n FV : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Vars \u0393\n FV-terms : \u2200 {\u03a3 \u0393} \u2192 Terms \u0393 \u03a3 \u2192 Vars \u0393\n\n FV (const \u03b9 ts) = FV-terms ts\n FV (var x) = singleton x\n FV (abs t) = tail (FV t)\n FV (app s t) = FV s \u222a FV t\n\n FV-terms \u2205 = none\n FV-terms (t \u2022 ts) = FV t \u222a FV-terms ts\n\n closed? : \u2200 {\u03c4 \u0393} \u2192 (t : Term \u0393 \u03c4) \u2192 (FV t \u2261 none) \u228e \u22a4\n closed? t = empty? (FV t)\n\n -- Weakening\n\n weaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\n\n weaken-terms : \u2200 {\u0393\u2081 \u0393\u2082 \u03a3} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Terms \u0393\u2081 \u03a3 \u2192\n Terms \u0393\u2082 \u03a3\n\n weaken \u0393\u2081\u227c\u0393\u2082 (const c ts) = const c (weaken-terms \u0393\u2081\u227c\u0393\u2082 ts)\n weaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (weaken-var \u0393\u2081\u227c\u0393\u2082 x)\n weaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\n weaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\n weaken-terms \u0393\u2081\u227c\u0393\u2082 \u2205 = \u2205\n weaken-terms \u0393\u2081\u227c\u0393\u2082 (t \u2022 ts) = weaken \u0393\u2081\u227c\u0393\u2082 t \u2022 weaken-terms \u0393\u2081\u227c\u0393\u2082 ts\n\n -- Specialized weakening\n weaken\u2081 : \u2200 {\u0393 \u03c3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4\n weaken\u2081 t = weaken (drop _ \u2022 \u227c-refl) t\n\n weaken\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u0393) \u03c4\n weaken\u2082 t = weaken (drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n weaken\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u03b3 \u2022 \u0393) \u03c4\n weaken\u2083 t = weaken (drop _ \u2022 drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n -- Shorthands for nested applications\n app\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3\n app\u2082 f x = app (app f x)\n\n app\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4\n app\u2083 f x = app\u2082 (app f x)\n\n app\u2084 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5\n app\u2084 f x = app\u2083 (app f x)\n\n app\u2085 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6\n app\u2085 f x = app\u2084 (app f x)\n\n app\u2086 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6 \u21d2 \u03b7) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6 \u2192 Term \u0393 \u03b7\n app\u2086 f x = app\u2085 (app f x)\n\n UncurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\n UncurriedTermConstructor \u0393 \u03a3 \u03c4 = Terms \u0393 \u03a3 \u2192 Term \u0393 \u03c4\n\n uncurriedConst : \u2200 {\u03a3 \u03c4} \u2192 Const \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 UncurriedTermConstructor \u0393 \u03a3 \u03c4\n uncurriedConst constant = const constant\n\n CurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\n CurriedTermConstructor \u0393 \u2205 \u03c4\u2032 = Term \u0393 \u03c4\u2032\n CurriedTermConstructor \u0393 (\u03c4 \u2022 \u03a3) \u03c4\u2032 = Term \u0393 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\u2032\n\n curryTermConstructor : \u2200 {\u03a3 \u0393 \u03c4} \u2192 UncurriedTermConstructor \u0393 \u03a3 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\n curryTermConstructor {\u2205} k = k \u2205\n curryTermConstructor {\u03c4 \u2022 \u03a3} k = \u03bb t \u2192 curryTermConstructor (\u03bb ts \u2192 k (t \u2022 ts))\n\n curriedConst : \u2200 {\u03a3 \u03c4} \u2192 Const \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\n curriedConst constant = curryTermConstructor (uncurriedConst constant)\n\n\n -- HOAS-like smart constructors for lambdas, for different arities.\n\n -- We could also write this:\n module NamespaceForBadAbs\u2081 where\n abs\u2081\u2032 :\n \u2200 {\u0393 \u03c4\u2081 \u03c4} \u2192\n (Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2081 \u2192 Term (\u03c4\u2081 \u2022 \u0393) \u03c4) \u2192\n (Term \u0393 (\u03c4\u2081 \u21d2 \u03c4))\n abs\u2081\u2032 {\u0393} {\u03c4\u2081} = \u03bb f \u2192 abs (f (var this))\n\n -- However, this is less general, and it is harder to reuse. In particular,\n -- this cannot be used inside abs\u2082, ..., abs\u2086.\n\n -- Now, let's write other variants with a loop!\n open import Data.Vec using (_\u2237_; []; Vec; foldr; [_])\n open import Data.Nat\n module AbsNHelpers where\n open import Function\n hoasArgType : \u2200 {n} \u2192 Context \u2192 Type \u2192 Vec Type n \u2192 Set\n hoasArgType \u0393 \u03c4 = foldr _ (\u03bb a b \u2192 a \u2192 b) (Term \u0393 \u03c4) \u2218 Data.Vec.map (Term \u0393)\n -- That is,\n --hoasArgType \u0393 \u03c4 [] = Term \u0393 \u03c4\n --hoasArgType \u0393 \u03c4 (\u03c4\u2080 \u2237 \u03c4s) = Term \u0393 \u03c4\u2080 \u2192 hoasArgType \u0393 \u03c4 \u03c4s\n\n hoasResType : \u2200 {n} \u2192 Type \u2192 Vec Type n \u2192 Type\n hoasResType \u03c4 = foldr _ _\u21d2_ \u03c4\n\n absNType : {n : \u2115} \u2192 Vec _ n \u2192 Set\n absNType \u03c4s = \u2200 {\u0393 \u03c4} \u2192\n (f : \u2200 {\u0393\u2032} \u2192 {\u0393\u227c\u0393\u2032 : \u0393 \u227c \u0393\u2032} \u2192 hoasArgType \u0393\u2032 \u03c4 \u03c4s) \u2192\n Term \u0393 (hoasResType \u03c4 \u03c4s)\n\n -- A better type for absN but a mess to use due to the proofs (which aren't synthesized, even though maybe they should be?)\n -- absN : (n : \u2115) \u2192 {_ : n > 0} \u2192 absNType n\n -- XXX See \"how to keep your neighbours in order\" for tricks.\n\n -- Please the termination checker by keeping this case separate.\n absNBase : \u2200 {\u03c4\u2081} \u2192 absNType [ \u03c4\u2081 ]\n absNBase {\u03c4\u2081} f = abs (f {\u0393\u227c\u0393\u2032 = drop \u03c4\u2081 \u2022 \u227c-refl} (var this))\n -- Otherwise, the recursive step of absN would invoke absN twice, and the\n -- termination checker does not figure out that the calls are in fact\n -- terminating.\n\n -- What I'd like to write, avoiding the need for absNBase, but can't because of the termination checker.\n {-\n absN {zero} (\u03c4\u2081 \u2237 []) f = abs (f {\u0393\u227c\u0393\u2032 = drop \u03c4\u2081 \u2022 \u227c-refl} (var this))\n absN {suc n} (\u03c4\u2081 \u2237 \u03c4\u2082 \u2237 \u03c4s) f =\n absN (\u03c4\u2081 \u2237 []) (\u03bb {_} {\u0393\u227c\u0393\u2032} x\u2081 \u2192\n absN {n} (\u03c4\u2082 \u2237 \u03c4s) (\u03bb {\u0393\u2032\u2081} {\u0393\u2032\u227c\u0393\u2032\u2081} \u2192\n f {\u0393\u227c\u0393\u2032 = \u227c-trans \u0393\u227c\u0393\u2032 \u0393\u2032\u227c\u0393\u2032\u2081} (weaken \u0393\u2032\u227c\u0393\u2032\u2081 x\u2081)))\n -}\n --What I have to write instead:\n\n absN : {n : \u2115} \u2192 (\u03c4s : Vec _ (suc n)) \u2192 absNType \u03c4s\n absN {zero} (\u03c4\u2081 \u2237 []) = absNBase\n absN {suc n} (\u03c4\u2081 \u2237 \u03c4\u2082 \u2237 \u03c4s) f =\n absNBase (\u03bb {_} {\u0393\u227c\u0393\u2032} x\u2081 \u2192\n absN {n} (\u03c4\u2082 \u2237 \u03c4s) (\u03bb {\u0393\u2032\u2081} {\u0393\u2032\u227c\u0393\u2032\u2081} \u2192\n f {\u0393\u227c\u0393\u2032 = \u227c-trans \u0393\u227c\u0393\u2032 \u0393\u2032\u227c\u0393\u2032\u2081} (weaken \u0393\u2032\u227c\u0393\u2032\u2081 x\u2081)))\n\n -- Using a similar trick, we can declare absV which takes the N implicit\n -- type arguments individually, collects them and passes them on to absN.\n -- This is inspired by what's shown in the Agda 2.4.0 release notes, and\n -- relies critically on support for varying arity. To collect them, we need\n -- to use an accumulator argument.\n\n absVType : \u2200 n {m} (\u03c4s : Vec Type m) \u2192 Set\n absVType 0 \u03c4s = absNType \u03c4s\n absVType (suc n) \u03c4s = {\u03c4\u1d62 : Type} \u2192 absVType n (\u03c4\u1d62 \u2237 \u03c4s)\n\n -- XXX\n absVAux : \u2200 {m} \u2192 (\u03c4s : Vec Type m) \u2192 \u2200 n \u2192 absVType (suc n) \u03c4s\n absVAux \u03c4s zero {\u03c4\u1d62} = absN (\u03c4\u1d62 \u2237 \u03c4s)\n absVAux \u03c4s (suc n) {\u03c4\u1d62} = absVAux (\u03c4\u1d62 \u2237 \u03c4s) n\n\n absV = absVAux []\n\n open AbsNHelpers using (absV) public\n\n -- Declare abs\u2081 .. abs\u2086 wrappers for more convenient use, allowing implicit\n -- type arguments to be synthesized. Somehow, Agda does not manage to\n -- synthesize \u03c4s by unification.\n -- Implicit arguments are reversed when assembling the list, but that's no real problem.\n\n abs\u2081 = absV 0\n abs\u2082 = absV 1\n abs\u2083 = absV 2\n abs\u2084 = absV 3\n abs\u2085 = absV 4\n abs\u2086 = absV 5\n\n{-\nabs\u2081\nHave: {\u03c4\u2081 : Type} {\u0393 : Context} {\u03c4 : Type} \u2192\n ({\u0393\u2032 : Context} {\u0393\u227c\u0393\u2032 : \u0393 \u227c \u0393\u2032} \u2192 Term \u0393\u2032 \u03c4\u2081 \u2192 Term \u0393\u2032 \u03c4) \u2192\n Term \u0393 (\u03c4\u2081 Type.Structure.\u21d2 \u03c4)\n\nabs\u2082\nHave: {\u03c4\u2081 : Type} {\u03c4\u2081 = \u03c4\u2082 : Type} {\u0393 : Context} {\u03c4 : Type} \u2192\n ({\u0393\u2032 : Context} {\u0393\u227c\u0393\u2032 : \u0393 \u227c \u0393\u2032} \u2192\n Term \u0393\u2032 \u03c4\u2082 \u2192 Term \u0393\u2032 \u03c4\u2081 \u2192 Term \u0393\u2032 \u03c4) \u2192\n Term \u0393 (\u03c4\u2082 Type.Structure.\u21d2 \u03c4\u2081 Type.Structure.\u21d2 \u03c4)\n\n-}\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- The syntax of terms (Fig. 1a and 1b).\n------------------------------------------------------------------------\n\n-- The syntax of terms depends on the syntax of simple types\n-- (because terms are indexed by types in order to rule out\n-- ill-typed terms). But we are in the Parametric.* hierarchy, so\n-- we don't know the full syntax of types, only how to lift the\n-- syntax of base types into the syntax of simple types. This\n-- means that we have to be parametric in the syntax of base\n-- types, too.\n--\n-- In such parametric modules that depend on other parametric\n-- modules, we first import our dependencies under a more\n-- convenient name.\n\nimport Parametric.Syntax.Type as Type\n\n-- Then we start the module proper, with parameters for all\n-- extension points of our dependencies. Note that here, the\n-- \"Structure\" naming convenion makes some sense, because we can\n-- say that we need some \"Type.Structure\" in order to define the\n-- \"Term.Structure\".\n\nmodule Parametric.Syntax.Term\n (Base : Type.Structure)\n where\n\n-- Now inside the module, we can open our dependencies with the\n-- parameters for their extension points. Again, here the name\n-- \"Structure\" makes some sense, because we can say that we want\n-- to access the \"Type.Structure\" that is induced by Base.\n\nopen Type.Structure Base\n\n-- At this point, we have dealt with the extension points of our\n-- dependencies, and we have all the definitions about simple\n-- types, contexts, variables, and variable sets in scope that we\n-- provided in Parametric.Syntax.Type. Now we can proceed to\n-- define our own extension point, following the pattern\n-- explained in Parametric.Syntax.Type.\n\nopen import Relation.Binary.PropositionalEquality\nopen import Function using (_\u2218_)\nopen import Data.Unit\nopen import Data.Sum\n\n-- Our extension point is a set of primitives, indexed by the\n-- types of their arguments and their return type. In general, if\n-- you're confused about what an extension point means, you might\n-- want to open the corresponding module in the Nehemiah\n-- hierarchy to see how it is implemented in the example\n-- plugin. In this case, that would be the Nehemiah.Syntax.Term\n-- module.\n\nStructure : Set\u2081\nStructure = Context \u2192 Type \u2192 Set\n\nmodule Structure (Const : Structure) where\n import Base.Data.DependentList as DependentList\n open DependentList public using (\u2205 ; _\u2022_)\n open DependentList\n\n -- Declarations of Term and Terms to enable mutual recursion.\n --\n -- Note that terms are indexed by contexts and types. In the\n -- paper, we define the abstract syntax of terms in Fig 1a and\n -- then define a type system in Fig 1b. All lemmas and theorems\n -- then explicitly specify that they only hold for well-typed\n -- terms. Here, we use the indices to define a type that can\n -- only hold well-typed terms in the first place.\n data Term\n (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set\n\n -- (Terms \u0393 \u03a3) represents a list of terms with types from \u03a3\n -- with free variables bound in \u0393.\n Terms : Context \u2192 Context \u2192 Set\n Terms \u0393 = DependentList (Term \u0393)\n\n -- (Term \u0393 \u03c4) represents a term of type \u03c4\n -- with free variables bound in \u0393.\n data Term \u0393 where\n -- constants aka. primitives can only occur fully applied.\n const : \u2200 {\u03a3 \u03c4} \u2192\n (c : Const \u03a3 \u03c4) \u2192\n (args : Terms \u0393 \u03a3) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indicies, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\n -- Free variables\n FV : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Vars \u0393\n FV-terms : \u2200 {\u03a3 \u0393} \u2192 Terms \u0393 \u03a3 \u2192 Vars \u0393\n\n FV (const \u03b9 ts) = FV-terms ts\n FV (var x) = singleton x\n FV (abs t) = tail (FV t)\n FV (app s t) = FV s \u222a FV t\n\n FV-terms \u2205 = none\n FV-terms (t \u2022 ts) = FV t \u222a FV-terms ts\n\n closed? : \u2200 {\u03c4 \u0393} \u2192 (t : Term \u0393 \u03c4) \u2192 (FV t \u2261 none) \u228e \u22a4\n closed? t = empty? (FV t)\n\n -- Weakening\n\n weaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\n\n weaken-terms : \u2200 {\u0393\u2081 \u0393\u2082 \u03a3} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Terms \u0393\u2081 \u03a3 \u2192\n Terms \u0393\u2082 \u03a3\n\n weaken \u0393\u2081\u227c\u0393\u2082 (const c ts) = const c (weaken-terms \u0393\u2081\u227c\u0393\u2082 ts)\n weaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (weaken-var \u0393\u2081\u227c\u0393\u2082 x)\n weaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\n weaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\n weaken-terms \u0393\u2081\u227c\u0393\u2082 \u2205 = \u2205\n weaken-terms \u0393\u2081\u227c\u0393\u2082 (t \u2022 ts) = weaken \u0393\u2081\u227c\u0393\u2082 t \u2022 weaken-terms \u0393\u2081\u227c\u0393\u2082 ts\n\n -- Specialized weakening\n weaken\u2081 : \u2200 {\u0393 \u03c3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4\n weaken\u2081 t = weaken (drop _ \u2022 \u227c-refl) t\n\n weaken\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u0393) \u03c4\n weaken\u2082 t = weaken (drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n weaken\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u03b3 \u2022 \u0393) \u03c4\n weaken\u2083 t = weaken (drop _ \u2022 drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n -- Shorthands for nested applications\n app\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3\n app\u2082 f x = app (app f x)\n\n app\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4\n app\u2083 f x = app\u2082 (app f x)\n\n app\u2084 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5\n app\u2084 f x = app\u2083 (app f x)\n\n app\u2085 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6\n app\u2085 f x = app\u2084 (app f x)\n\n app\u2086 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6 \u21d2 \u03b7) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6 \u2192 Term \u0393 \u03b7\n app\u2086 f x = app\u2085 (app f x)\n\n UncurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\n UncurriedTermConstructor \u0393 \u03a3 \u03c4 = Terms \u0393 \u03a3 \u2192 Term \u0393 \u03c4\n\n uncurriedConst : \u2200 {\u03a3 \u03c4} \u2192 Const \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 UncurriedTermConstructor \u0393 \u03a3 \u03c4\n uncurriedConst constant = const constant\n\n CurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\n CurriedTermConstructor \u0393 \u2205 \u03c4\u2032 = Term \u0393 \u03c4\u2032\n CurriedTermConstructor \u0393 (\u03c4 \u2022 \u03a3) \u03c4\u2032 = Term \u0393 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\u2032\n\n curryTermConstructor : \u2200 {\u03a3 \u0393 \u03c4} \u2192 UncurriedTermConstructor \u0393 \u03a3 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\n curryTermConstructor {\u2205} k = k \u2205\n curryTermConstructor {\u03c4 \u2022 \u03a3} k = \u03bb t \u2192 curryTermConstructor (\u03bb ts \u2192 k (t \u2022 ts))\n\n curriedConst : \u2200 {\u03a3 \u03c4} \u2192 Const \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\n curriedConst constant = curryTermConstructor (uncurriedConst constant)\n\n\n -- HOAS-like smart constructors for lambdas, for different arities.\n\n -- We could also write this:\n module NamespaceForBadAbs\u2081 where\n abs\u2081\u2032 :\n \u2200 {\u0393 \u03c4\u2081 \u03c4} \u2192\n (Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2081 \u2192 Term (\u03c4\u2081 \u2022 \u0393) \u03c4) \u2192\n (Term \u0393 (\u03c4\u2081 \u21d2 \u03c4))\n abs\u2081\u2032 {\u0393} {\u03c4\u2081} = \u03bb f \u2192 abs (f (var this))\n\n -- However, this is less general, and it is harder to reuse. In particular,\n -- this cannot be used inside abs\u2082, ..., abs\u2086.\n\n -- Now, let's write other variants with a loop!\n open import Data.Vec using (_\u2237_; []; Vec; foldr)\n open import Data.Nat\n module AbsNHelpers where\n open import Function\n hoasArgType : \u2200 {n} \u2192 Context \u2192 Type \u2192 Vec Type n \u2192 Set\n hoasArgType \u0393 \u03c4 = foldr _ (\u03bb a b \u2192 a \u2192 b) (Term \u0393 \u03c4) \u2218 Data.Vec.map (Term \u0393)\n -- That is,\n --hoasArgType \u0393 \u03c4 [] = Term \u0393 \u03c4\n --hoasArgType \u0393 \u03c4 (\u03c4\u2080 \u2237 \u03c4s) = Term \u0393 \u03c4\u2080 \u2192 hoasArgType \u0393 \u03c4 \u03c4s\n\n hoasResType : \u2200 {n} \u2192 Type \u2192 Vec Type n \u2192 Type\n hoasResType \u03c4 = foldr _ _\u21d2_ \u03c4\n\n absNType : {n : \u2115} \u2192 Vec _ n \u2192 Set\n absNType \u03c4s = \u2200 {\u0393 \u03c4} \u2192\n (f : \u2200 {\u0393\u2032} \u2192 {\u0393\u227c\u0393\u2032 : \u0393 \u227c \u0393\u2032} \u2192 hoasArgType \u0393\u2032 \u03c4 \u03c4s) \u2192\n Term \u0393 (hoasResType \u03c4 \u03c4s)\n\n -- A better type for absN but a mess to use due to the proofs (which aren't synthesized, even though maybe they should be?)\n -- absN : (n : \u2115) \u2192 {_ : n > 0} \u2192 absNType n\n -- XXX See \"how to keep your neighbours in order\" for tricks.\n\n -- Please the termination checker by keeping this case separate.\n absNBase : \u2200 {\u03c4\u2081} \u2192 absNType (\u03c4\u2081 \u2237 [])\n absNBase {\u03c4\u2081} f = abs (f {\u0393\u227c\u0393\u2032 = drop \u03c4\u2081 \u2022 \u227c-refl} (var this))\n -- Otherwise, the recursive step of absN would invoke absN twice, and the\n -- termination checker does not figure out that the calls are in fact\n -- terminating.\n\n open AbsNHelpers using (absNType; absNBase)\n\n -- XXX: could we be using the same trick as above to take the N implicit\n -- arguments individually, rather than in a vector? I can't figure out how,\n -- at least not without trying it. But it seems that's what's shown in Agda 2.4.0 release notes!\n absN : {n : \u2115} \u2192 (\u03c4s : Vec _ (suc n)) \u2192 absNType \u03c4s\n absN {zero} (\u03c4\u2081 \u2237 []) = absNBase\n absN {suc n} (\u03c4\u2081 \u2237 \u03c4\u2082 \u2237 \u03c4s) f =\n absNBase (\u03bb {_} {\u0393\u227c\u0393\u2032} x\u2081 \u2192\n absN {n} (\u03c4\u2082 \u2237 \u03c4s) (\u03bb {\u0393\u2032\u2081} {\u0393\u2032\u227c\u0393\u2032\u2081} \u2192\n f {\u0393\u227c\u0393\u2032 = \u227c-trans \u0393\u227c\u0393\u2032 \u0393\u2032\u227c\u0393\u2032\u2081} (weaken \u0393\u2032\u227c\u0393\u2032\u2081 x\u2081)))\n\n -- What I'd like to write, avoiding the need for absNBase, but can't because of the termination checker.\n {-\n absN {zero} (\u03c4\u2081 \u2237 []) f = abs (f {\u0393\u227c\u0393\u2032 = drop \u03c4\u2081 \u2022 \u227c-refl} (var this))\n absN {suc n} (\u03c4\u2081 \u2237 \u03c4\u2082 \u2237 \u03c4s) f =\n absN (\u03c4\u2081 \u2237 []) (\u03bb {_} {\u0393\u227c\u0393\u2032} x\u2081 \u2192\n absN {n} (\u03c4\u2082 \u2237 \u03c4s) (\u03bb {\u0393\u2032\u2081} {\u0393\u2032\u227c\u0393\u2032\u2081} \u2192\n f {\u0393\u227c\u0393\u2032 = \u227c-trans \u0393\u227c\u0393\u2032 \u0393\u2032\u227c\u0393\u2032\u2081} (weaken \u0393\u2032\u227c\u0393\u2032\u2081 x\u2081)))\n -}\n\n -- Declare abs\u2081 .. abs\u2086 wrappers for more convenient use, allowing implicit\n -- type arguments to be synthesized. Somehow, Agda does not manage to\n -- synthesize \u03c4s by unification.\n -- Implicit arguments are reversed when assembling the list, but that's no real problem.\n module _ {\u03c4\u2081 : Type} where\n \u03c4s\u2081 = \u03c4\u2081 \u2237 []\n abs\u2081 : absNType \u03c4s\u2081\n abs\u2081 = absN \u03c4s\u2081\n module _ {\u03c4\u2082 : Type} where\n \u03c4s\u2082 = \u03c4\u2082 \u2237 \u03c4s\u2081\n abs\u2082 : absNType \u03c4s\u2082\n abs\u2082 = absN \u03c4s\u2082\n module _ {\u03c4\u2083 : Type} where\n \u03c4s\u2083 = \u03c4\u2083 \u2237 \u03c4s\u2082\n abs\u2083 : absNType \u03c4s\u2083\n abs\u2083 = absN \u03c4s\u2083\n module _ {\u03c4\u2084 : Type} where\n \u03c4s\u2084 = \u03c4\u2084 \u2237 \u03c4s\u2083\n abs\u2084 : absNType \u03c4s\u2084\n abs\u2084 = absN \u03c4s\u2084\n module _ {\u03c4\u2085 : Type} where\n \u03c4s\u2085 = \u03c4\u2085 \u2237 \u03c4s\u2084\n abs\u2085 : absNType \u03c4s\u2085\n abs\u2085 = absN \u03c4s\u2085\n module _ {\u03c4\u2086 : Type} where\n \u03c4s\u2086 = \u03c4\u2086 \u2237 \u03c4s\u2085\n abs\u2086 : absNType \u03c4s\u2086\n abs\u2086 = absN \u03c4s\u2086\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3b48cfb7880c48f67527b01c85ad20363d89e8fa","subject":"Function.NP: Two","message":"Function.NP: Two\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Function\/NP.agda","new_file":"lib\/Function\/NP.agda","new_contents":"{-# OPTIONS --without-K #-}\n{-# OPTIONS --universe-polymorphism #-}\nmodule Function.NP where\n\nimport Level as L\nopen import Type hiding (\u2605)\nopen import Algebra\nopen import Algebra.Structures\nopen import Function public\nopen import Data.Nat using (\u2115; zero; suc; _+_; _*_; fold)\nopen import Data.Bool renaming (Bool to \ud835\udfda)\nopen import Data.Product\nopen import Data.Vec.N-ary using (N-ary; N-ary-level)\nimport Category.Monad.Identity as Id\nopen import Category.Monad renaming (module RawMonad to Monad; RawMonad to Monad)\nopen import Category.Applicative renaming (module RawApplicative to Applicative; RawApplicative to Applicative)\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_)\nopen import Relation.Unary.Logical\nopen import Relation.Binary.Logical\nopen Relation.Unary.Logical public using (_[\u2192]_; [\u03a0]; [\u03a0]e; [\u2200])\nopen Relation.Binary.Logical public using (_\u27e6\u2192\u27e7_; \u27e6\u03a0\u27e7; \u27e6\u03a0\u27e7e; \u27e6\u2200\u27e7)\n\n\n\u03a0 : \u2200 {a b} (A : \u2605 a) \u2192 (B : A \u2192 \u2605 b) \u2192 \u2605 _\n\u03a0 A B = (x : A) \u2192 B x\n\n\u03a0\u03a0 : \u2200 {a b c} (A : \u2605 a) (B : A \u2192 \u2605 b) (C : \u03a3 A B \u2192 \u2605 c) \u2192 \u2605 _\n\u03a0\u03a0 A B C = \u03a0 A \u03bb x \u2192 \u03a0 (B x) \u03bb y \u2192 C (x , y)\n\n\u03a0\u03a0\u03a0 : \u2200 {a b c d} (A : \u2605 a) (B : A \u2192 \u2605 b)\n (C : \u03a3 A B \u2192 \u2605 c) (D : \u03a3 (\u03a3 A B) C \u2192 \u2605 d) \u2192 \u2605 _\n\u03a0\u03a0\u03a0 A B C D = \u03a0 A \u03bb x \u2192 \u03a0 (B x) \u03bb y \u2192 \u03a0 (C (x , y)) \u03bb z \u2192 D ((x , y) , z)\n\nid-app : \u2200 {f} \u2192 Applicative {f} id\nid-app = rawIApplicative\n where open Monad Id.IdentityMonad\n\n-\u2192- : \u2200 {a b} (A : \u2605 a) (B : \u2605 b) \u2192 \u2605 (a L.\u2294 b)\n-\u2192- A B = A \u2192 B\n\n_\u2192\u27e8_\u27e9_ : \u2200 {a b} (A : \u2605 a) (n : \u2115) (B : \u2605 b) \u2192 \u2605 (N-ary-level a b n)\nA \u2192\u27e8 n \u27e9 B = N-ary n A B\n\n_\u2192\u27e8_\u27e9\u2080_ : \u2200 (A : \u2605\u2080) (n : \u2115) (B : \u2605\u2080) \u2192 \u2605\u2080\nA \u2192\u27e8 zero \u27e9\u2080 B = B\nA \u2192\u27e8 suc n \u27e9\u2080 B = A \u2192 A \u2192\u27e8 n \u27e9\u2080 B\n\n_\u2192\u27e8_\u27e9\u2081_ : \u2200 (A : \u2605\u2080) (n : \u2115) (B : \u2605\u2081) \u2192 \u2605\u2081\nA \u2192\u27e8 zero \u27e9\u2081 B = B\nA \u2192\u27e8 suc n \u27e9\u2081 B = A \u2192 A \u2192\u27e8 n \u27e9\u2081 B\n\nEndo : \u2200 {a} \u2192 \u2605 a \u2192 \u2605 a\nEndo A = A \u2192 A\n\n[Endo] : \u2200 {a} \u2192 ([\u2605] {a} a [\u2192] [\u2605] _) Endo\n[Endo] A\u209a = A\u209a [\u2192] A\u209a\n\n\u27e6Endo\u27e7 : \u2200 {a} \u2192 (\u27e6\u2605\u27e7 {a} {a} a \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 _) Endo Endo\n\u27e6Endo\u27e7 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 A\u1d63\n\nCmp : \u2200 {a} \u2192 \u2605 a \u2192 \u2605 a\nCmp A = A \u2192 A \u2192 \ud835\udfda\n\n{- needs [\ud835\udfda] and \u27e6\ud835\udfda\u27e7 potentially move these to Data.Two\n\n[Cmp] : \u2200 {a} \u2192 ([\u2605] {a} a [\u2192] [\u2605] _ [\u2192] [\u2605] _) Cmp\n[Cmp] A\u209a = A\u209a [\u2192] A\u209a [\u2192] [\ud835\udfda]\n\n\u27e6Cmp\u27e7 : \u2200 {a} \u2192 (\u27e6\u2605\u27e7 {a} {a} a \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 _) Endo Endo\n\u27e6Cmp\u27e7 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7\n-}\n\n-- More properties about fold are in Data.Nat.NP\nnest : \u2200 {a} {A : \u2605 a} \u2192 \u2115 \u2192 Endo (Endo A)\n-- TMP nest n f x = fold x f n\nnest zero f x = x\nnest (suc n) f x = f (nest n f x)\n\nmodule nest-Properties {a} {A : \u2605 a} (f : Endo A) where\n nest\u2080 : nest 0 f \u2261 id\n nest\u2080 = \u2261.refl\n nest\u2081 : nest 1 f \u2261 f\n nest\u2081 = \u2261.refl\n nest\u2082 : nest 2 f \u2261 f \u2218 f\n nest\u2082 = \u2261.refl\n nest\u2083 : nest 3 f \u2261 f \u2218 f \u2218 f\n nest\u2083 = \u2261.refl\n\n nest-+ : \u2200 m n \u2192 nest (m + n) f \u2261 nest m f \u2218 nest n f\n nest-+ zero n = \u2261.refl\n nest-+ (suc m) n = \u2261.cong (_\u2218_ f) (nest-+ m n)\n\n nest-+' : \u2200 m n \u2192 nest (m + n) f \u2257 nest m f \u2218 nest n f\n nest-+' m n x = \u2261.cong (flip _$_ x) (nest-+ m n)\n\n nest-* : \u2200 m n \u2192 nest (m * n) f \u2257 nest m (nest n f)\n nest-* zero n x = \u2261.refl\n nest-* (suc m) n x =\n nest (suc m * n) f x \u2261\u27e8 \u2261.refl \u27e9\n nest (n + m * n) f x \u2261\u27e8 nest-+' n (m * n) x \u27e9\n (nest n f \u2218 nest (m * n) f) x \u2261\u27e8 \u2261.cong (nest n f) (nest-* m n x) \u27e9\n (nest n f \u2218 nest m (nest n f)) x \u2261\u27e8 \u2261.refl \u27e9\n nest n f (nest m (nest n f) x) \u2261\u27e8 \u2261.refl \u27e9\n nest (suc m) (nest n f) x \u220e\n where open \u2261.\u2261-Reasoning\n\n{- WRONG\nmodule more-nest-Properties {a} {A : \u2605 a} where\n nest-+'' : \u2200 (f : Endo (Endo A)) g m n \u2192 nest m f g \u2218 nest n f g \u2257 nest (m + n) f g\n nest-+'' f g zero n = {!!}\n nest-+'' f g (suc m) n = {!!}\n-}\n\n_$\u27e8_\u27e9_ : \u2200 {a} {A : \u2605 a} \u2192 Endo A \u2192 \u2115 \u2192 Endo A\n_$\u27e8_\u27e9_ f n = nest n f\n\n-- If you run a version of Agda without the support of instance\n-- arguments, simply comment this definition, very little code rely on it.\n\u2026 : \u2200 {a} {A : \u2605 a} \u2983 x : A \u2984 \u2192 A\n\u2026 \u2983 x \u2984 = x\n\n_\u27e8_\u27e9\u00b0_ : \u2200 {i a b c} {Ix : \u2605 i} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : (x : A) \u2192 B x \u2192 \u2605 c}\n \u2192 (f : Ix \u2192 A)\n \u2192 (op : (x : A) (y : B x) \u2192 C x y)\n \u2192 (g : (i : Ix) \u2192 B (f i))\n \u2192 (i : Ix) \u2192 C (f i) (g i)\n(f \u27e8 _\u2219_ \u27e9\u00b0 g) x = f x \u2219 g x\n\nmodule Combinators where\n S : \u2200 {A B C : \u2605\u2080} \u2192\n (A \u2192 B \u2192 C) \u2192\n (A \u2192 B) \u2192\n (A \u2192 C)\n S = _\u02e2_\n\n K : \u2200 {A B : \u2605\u2080} \u2192 A \u2192 B \u2192 A\n K = const\n\n -- B \u2257 _\u2218_\n B : \u2200 {A B C : \u2605\u2080} \u2192 (B \u2192 C) \u2192 (A \u2192 B) \u2192 A \u2192 C\n B = S (K S) K\n\n -- C \u2257 flip\n C : \u2200 {A B C : \u2605\u2080} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\n C = S (S (K (S (K S) K)) S) (K K)\n\nmodule EndoMonoid-\u2248 {a \u2113} {A : \u2605 a}\n {_\u2248_ : Endo A \u2192 Endo A \u2192 \u2605 \u2113}\n (isEquivalence : IsEquivalence _\u2248_)\n (\u2218-cong : _\u2218\u2032_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_)\n where\n private\n module \u2248 = IsEquivalence isEquivalence\n isSemigroup : IsSemigroup _\u2248_ _\u2218\u2032_\n isSemigroup = record { isEquivalence = isEquivalence; assoc = \u03bb _ _ _ \u2192 \u2248.refl; \u2219-cong = \u2218-cong }\n\n monoid : Monoid a \u2113\n monoid = record { Carrier = Endo A; _\u2248_ = _\u2248_; _\u2219_ = _\u2218\u2032_; \u03b5 = id\n ; isMonoid = record { isSemigroup = isSemigroup\n ; identity = (\u03bb _ \u2192 \u2248.refl) , (\u03bb _ \u2192 \u2248.refl) } }\n\n open Monoid monoid public\n\nmodule EndoMonoid-\u2261 {a} (A : \u2605 a) = EndoMonoid-\u2248 {A = A} \u2261.isEquivalence (\u2261.cong\u2082 _\u2218\u2032_)\n\nmodule EndoMonoid-\u2257 {a} (A : \u2605 a) = EndoMonoid-\u2248 (Setoid.isEquivalence (A \u2261.\u2192-setoid A))\n (\u03bb {f} {g} {h} {i} p q x \u2192 \u2261.trans (p (h x)) (\u2261.cong g (q x)))\n\n\u27e6id\u27e7 : (\u2200\u27e8 A\u1d63 \u2236 \u27e6\u2605\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63) id id\n\u27e6id\u27e7 _ x\u1d63 = x\u1d63\n\n\u27e6\u2218\u2032\u27e7 : (\u2200\u27e8 A\u1d63 \u2236 \u27e6\u2605\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 B\u1d63 \u2236 \u27e6\u2605\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 C\u1d63 \u2236 \u27e6\u2605\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 (B\u1d63 \u27e6\u2192\u27e7 C\u1d63) \u27e6\u2192\u27e7 (A\u1d63 \u27e6\u2192\u27e7 B\u1d63) \u27e6\u2192\u27e7 (A\u1d63 \u27e6\u2192\u27e7 C\u1d63)) _\u2218\u2032_ _\u2218\u2032_\n\u27e6\u2218\u2032\u27e7 _ _ _ f\u1d63 g\u1d63 x\u1d63 = f\u1d63 (g\u1d63 x\u1d63)\n","old_contents":"{-# OPTIONS --without-K #-}\n{-# OPTIONS --universe-polymorphism #-}\nmodule Function.NP where\n\nimport Level as L\nopen import Type hiding (\u2605)\nopen import Algebra\nopen import Algebra.Structures\nopen import Function public\nopen import Data.Nat using (\u2115; zero; suc; _+_; _*_; fold)\nopen import Data.Bool using (Bool)\nopen import Data.Product\nopen import Data.Vec.N-ary using (N-ary; N-ary-level)\nimport Category.Monad.Identity as Id\nopen import Category.Monad renaming (module RawMonad to Monad; RawMonad to Monad)\nopen import Category.Applicative renaming (module RawApplicative to Applicative; RawApplicative to Applicative)\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_)\nopen import Relation.Unary.Logical\nopen import Relation.Binary.Logical\nopen Relation.Unary.Logical public using (_[\u2192]_; [\u03a0]; [\u03a0]e; [\u2200])\nopen Relation.Binary.Logical public using (_\u27e6\u2192\u27e7_; \u27e6\u03a0\u27e7; \u27e6\u03a0\u27e7e; \u27e6\u2200\u27e7)\n\n\n\u03a0 : \u2200 {a b} (A : \u2605 a) \u2192 (B : A \u2192 \u2605 b) \u2192 \u2605 _\n\u03a0 A B = (x : A) \u2192 B x\n\n\u03a0\u03a0 : \u2200 {a b c} (A : \u2605 a) (B : A \u2192 \u2605 b) (C : \u03a3 A B \u2192 \u2605 c) \u2192 \u2605 _\n\u03a0\u03a0 A B C = \u03a0 A \u03bb x \u2192 \u03a0 (B x) \u03bb y \u2192 C (x , y)\n\n\u03a0\u03a0\u03a0 : \u2200 {a b c d} (A : \u2605 a) (B : A \u2192 \u2605 b)\n (C : \u03a3 A B \u2192 \u2605 c) (D : \u03a3 (\u03a3 A B) C \u2192 \u2605 d) \u2192 \u2605 _\n\u03a0\u03a0\u03a0 A B C D = \u03a0 A \u03bb x \u2192 \u03a0 (B x) \u03bb y \u2192 \u03a0 (C (x , y)) \u03bb z \u2192 D ((x , y) , z)\n\nid-app : \u2200 {f} \u2192 Applicative {f} id\nid-app = rawIApplicative\n where open Monad Id.IdentityMonad\n\n-\u2192- : \u2200 {a b} (A : \u2605 a) (B : \u2605 b) \u2192 \u2605 (a L.\u2294 b)\n-\u2192- A B = A \u2192 B\n\n_\u2192\u27e8_\u27e9_ : \u2200 {a b} (A : \u2605 a) (n : \u2115) (B : \u2605 b) \u2192 \u2605 (N-ary-level a b n)\nA \u2192\u27e8 n \u27e9 B = N-ary n A B\n\n_\u2192\u27e8_\u27e9\u2080_ : \u2200 (A : \u2605\u2080) (n : \u2115) (B : \u2605\u2080) \u2192 \u2605\u2080\nA \u2192\u27e8 zero \u27e9\u2080 B = B\nA \u2192\u27e8 suc n \u27e9\u2080 B = A \u2192 A \u2192\u27e8 n \u27e9\u2080 B\n\n_\u2192\u27e8_\u27e9\u2081_ : \u2200 (A : \u2605\u2080) (n : \u2115) (B : \u2605\u2081) \u2192 \u2605\u2081\nA \u2192\u27e8 zero \u27e9\u2081 B = B\nA \u2192\u27e8 suc n \u27e9\u2081 B = A \u2192 A \u2192\u27e8 n \u27e9\u2081 B\n\nEndo : \u2200 {a} \u2192 \u2605 a \u2192 \u2605 a\nEndo A = A \u2192 A\n\n[Endo] : \u2200 {a} \u2192 ([\u2605] {a} a [\u2192] [\u2605] _) Endo\n[Endo] A\u209a = A\u209a [\u2192] A\u209a\n\n\u27e6Endo\u27e7 : \u2200 {a} \u2192 (\u27e6\u2605\u27e7 {a} {a} a \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 _) Endo Endo\n\u27e6Endo\u27e7 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 A\u1d63\n\nCmp : \u2200 {a} \u2192 \u2605 a \u2192 \u2605 a\nCmp A = A \u2192 A \u2192 Bool\n\n{- needs [Bool] and \u27e6Bool\u27e7 potentially move these to Data.Bool.NP\n\n[Cmp] : \u2200 {a} \u2192 ([\u2605] {a} a [\u2192] [\u2605] _ [\u2192] [\u2605] _) Cmp\n[Cmp] A\u209a = A\u209a [\u2192] A\u209a [\u2192] [Bool]\n\n\u27e6Cmp\u27e7 : \u2200 {a} \u2192 (\u27e6\u2605\u27e7 {a} {a} a \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 _) Endo Endo\n\u27e6Cmp\u27e7 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 \u27e6Bool\u27e7\n-}\n\n-- More properties about fold are in Data.Nat.NP\nnest : \u2200 {a} {A : \u2605 a} \u2192 \u2115 \u2192 Endo (Endo A)\n-- TMP nest n f x = fold x f n\nnest zero f x = x\nnest (suc n) f x = f (nest n f x)\n\nmodule nest-Properties {a} {A : \u2605 a} (f : Endo A) where\n nest\u2080 : nest 0 f \u2261 id\n nest\u2080 = \u2261.refl\n nest\u2081 : nest 1 f \u2261 f\n nest\u2081 = \u2261.refl\n nest\u2082 : nest 2 f \u2261 f \u2218 f\n nest\u2082 = \u2261.refl\n nest\u2083 : nest 3 f \u2261 f \u2218 f \u2218 f\n nest\u2083 = \u2261.refl\n\n nest-+ : \u2200 m n \u2192 nest (m + n) f \u2261 nest m f \u2218 nest n f\n nest-+ zero n = \u2261.refl\n nest-+ (suc m) n = \u2261.cong (_\u2218_ f) (nest-+ m n)\n\n nest-+' : \u2200 m n \u2192 nest (m + n) f \u2257 nest m f \u2218 nest n f\n nest-+' m n x = \u2261.cong (flip _$_ x) (nest-+ m n)\n\n nest-* : \u2200 m n \u2192 nest (m * n) f \u2257 nest m (nest n f)\n nest-* zero n x = \u2261.refl\n nest-* (suc m) n x =\n nest (suc m * n) f x \u2261\u27e8 \u2261.refl \u27e9\n nest (n + m * n) f x \u2261\u27e8 nest-+' n (m * n) x \u27e9\n (nest n f \u2218 nest (m * n) f) x \u2261\u27e8 \u2261.cong (nest n f) (nest-* m n x) \u27e9\n (nest n f \u2218 nest m (nest n f)) x \u2261\u27e8 \u2261.refl \u27e9\n nest n f (nest m (nest n f) x) \u2261\u27e8 \u2261.refl \u27e9\n nest (suc m) (nest n f) x \u220e\n where open \u2261.\u2261-Reasoning\n\n{- WRONG\nmodule more-nest-Properties {a} {A : \u2605 a} where\n nest-+'' : \u2200 (f : Endo (Endo A)) g m n \u2192 nest m f g \u2218 nest n f g \u2257 nest (m + n) f g\n nest-+'' f g zero n = {!!}\n nest-+'' f g (suc m) n = {!!}\n-}\n\n_$\u27e8_\u27e9_ : \u2200 {a} {A : \u2605 a} \u2192 Endo A \u2192 \u2115 \u2192 Endo A\n_$\u27e8_\u27e9_ f n = nest n f\n\n-- If you run a version of Agda without the support of instance\n-- arguments, simply comment this definition, very little code rely on it.\n\u2026 : \u2200 {a} {A : \u2605 a} \u2983 x : A \u2984 \u2192 A\n\u2026 \u2983 x \u2984 = x\n\n_\u27e8_\u27e9\u00b0_ : \u2200 {i a b c} {Ix : \u2605 i} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : (x : A) \u2192 B x \u2192 \u2605 c}\n \u2192 (f : Ix \u2192 A)\n \u2192 (op : (x : A) (y : B x) \u2192 C x y)\n \u2192 (g : (i : Ix) \u2192 B (f i))\n \u2192 (i : Ix) \u2192 C (f i) (g i)\n(f \u27e8 _\u2219_ \u27e9\u00b0 g) x = f x \u2219 g x\n\nmodule Combinators where\n S : \u2200 {A B C : \u2605\u2080} \u2192\n (A \u2192 B \u2192 C) \u2192\n (A \u2192 B) \u2192\n (A \u2192 C)\n S = _\u02e2_\n\n K : \u2200 {A B : \u2605\u2080} \u2192 A \u2192 B \u2192 A\n K = const\n\n -- B \u2257 _\u2218_\n B : \u2200 {A B C : \u2605\u2080} \u2192 (B \u2192 C) \u2192 (A \u2192 B) \u2192 A \u2192 C\n B = S (K S) K\n\n -- C \u2257 flip\n C : \u2200 {A B C : \u2605\u2080} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\n C = S (S (K (S (K S) K)) S) (K K)\n\nmodule EndoMonoid-\u2248 {a \u2113} {A : \u2605 a}\n {_\u2248_ : Endo A \u2192 Endo A \u2192 \u2605 \u2113}\n (isEquivalence : IsEquivalence _\u2248_)\n (\u2218-cong : _\u2218\u2032_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_)\n where\n private\n module \u2248 = IsEquivalence isEquivalence\n isSemigroup : IsSemigroup _\u2248_ _\u2218\u2032_\n isSemigroup = record { isEquivalence = isEquivalence; assoc = \u03bb _ _ _ \u2192 \u2248.refl; \u2219-cong = \u2218-cong }\n\n monoid : Monoid a \u2113\n monoid = record { Carrier = Endo A; _\u2248_ = _\u2248_; _\u2219_ = _\u2218\u2032_; \u03b5 = id\n ; isMonoid = record { isSemigroup = isSemigroup\n ; identity = (\u03bb _ \u2192 \u2248.refl) , (\u03bb _ \u2192 \u2248.refl) } }\n\n open Monoid monoid public\n\nmodule EndoMonoid-\u2261 {a} (A : \u2605 a) = EndoMonoid-\u2248 {A = A} \u2261.isEquivalence (\u2261.cong\u2082 _\u2218\u2032_)\n\nmodule EndoMonoid-\u2257 {a} (A : \u2605 a) = EndoMonoid-\u2248 (Setoid.isEquivalence (A \u2261.\u2192-setoid A))\n (\u03bb {f} {g} {h} {i} p q x \u2192 \u2261.trans (p (h x)) (\u2261.cong g (q x)))\n\n\u27e6id\u27e7 : (\u2200\u27e8 A\u1d63 \u2236 \u27e6\u2605\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63) id id\n\u27e6id\u27e7 _ x\u1d63 = x\u1d63\n\n\u27e6\u2218\u2032\u27e7 : (\u2200\u27e8 A\u1d63 \u2236 \u27e6\u2605\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 B\u1d63 \u2236 \u27e6\u2605\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 C\u1d63 \u2236 \u27e6\u2605\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 (B\u1d63 \u27e6\u2192\u27e7 C\u1d63) \u27e6\u2192\u27e7 (A\u1d63 \u27e6\u2192\u27e7 B\u1d63) \u27e6\u2192\u27e7 (A\u1d63 \u27e6\u2192\u27e7 C\u1d63)) _\u2218\u2032_ _\u2218\u2032_\n\u27e6\u2218\u2032\u27e7 _ _ _ f\u1d63 g\u1d63 x\u1d63 = f\u1d63 (g\u1d63 x\u1d63)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"ffe54a9fc4523ccfe8c3343695d569a1de89cfa7","subject":"Constants are values, so evaluating them takes 0 steps!","message":"Constants are values, so evaluating them takes 0 steps!\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/FunBigStepSILR2.agda","new_file":"Thesis\/FunBigStepSILR2.agda","new_contents":"-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\" (ISAC below), POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\n-- CHEATS:\n-- \"Fuctional big-step semantics\" requires an external termination proof for the\n-- semantics. There it is also mechanized, here it isn't. Worse, the same\n-- termination problem affects some lemmas about the semantics.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen import Data.Nat.Properties.Simple\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n\n-- WARNING: ISAC's big-step semantics produces a step count as \"output\". But\n-- that would not help Agda establish termination. That's only a problem for a\n-- functional big-step semantics, not for a relational semantics.\n--\n-- So, instead, I tried to use a sort of writer monad: the interpreter gets fuel\n-- and returns the remaining fuel. That's the same trick as in \"functional\n-- big-step semantics\". That *makes* the function terminating, even though Agda\n-- cannot see this because it does not know that the returned fuel is no bigger.\n\n-- Since we focus for now on STLC, unlike that\n-- paper, we could avoid error values because we keep types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) n = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\neval (app s t) \u03c1 (suc n) = (eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)) n\n\neval-const-dec : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-const-dec (lit v) n0 .n0 refl = \u2264-refl\n\n{-# TERMINATING #-}\neval-dec : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-dec (const c) \u03c1 v n0 n1 eq = eval-const-dec c n0 n1 eq\neval-dec (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (app s t) \u03c1 v zero n1 ()\neval-dec (app s t) \u03c1 v (suc n0) n3 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv sn1 | [ seq ] with eval t \u03c1 sn1 | inspect (eval t \u03c1) sn1\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv@(closure st s\u03c1) sn1 | [ seq ] | (Done tv tn2) | [ teq ] = \u2264-step (\u2264-trans (\u2264-trans (eval-dec st _ _ _ _ eq) (eval-dec t _ _ _ _ teq)) (eval-dec s _ _ _ _ seq))\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | Error | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | TimeOut | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Error | [ seq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | TimeOut | [ seq ]\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1)\neval-const-mono (lit v) n0 .n0 refl = refl\n\n-- ARGH\n{-# TERMINATING #-}\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1)\neval-mono (const c) \u03c1 v n0 n1 eq = eval-const-mono c n0 n1 eq\neval-mono (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\neval-mono (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\neval-mono (app s t) \u03c1 v zero n1 ()\neval-mono (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-mono t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n\neval-adjust-plus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1)\neval-adjust-plus zero t \u03c1 v n0 n1 eq = eq\neval-adjust-plus (suc d) t \u03c1 v n0 n1 eq = eval-mono t \u03c1 v (d + n0) (d + n1) (eval-adjust-plus d t \u03c1 v n0 n1 eq)\n\neval-const-strengthen : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1) \u2192 evalConst c n0 \u2261 Done v n1\neval-const-strengthen (lit v) n0 .n0 refl = refl\n\n-- {-# TERMINATING #-}\n-- eval-strengthen : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-strengthen (const c) \u03c1 v n0 n1 eq = eval-const-strengthen c n0 n1 eq\n-- eval-strengthen (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq = {!!}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 (suc n0) | inspect (eval s \u03c1) (suc n0)\n-- -- eval-strengthen (app s t) \u03c1 v\u2081 (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 n0 = {!eval-strengthen s \u03c1 v n0 n1 seq !}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-strengthen t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | Error | [ seq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | TimeOut | [ seq ] = {!!}\n\n-- eval-adjust-minus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 {\u03c1 v} n0 n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-adjust-minus zero t n0 n1 eq = eq\n-- eval-adjust-minus (suc d) t n0 n1 eq = eval-adjust-minus d t n0 n1 (eval-strengthen t _ _ (d + n0) (d + n1) eq)\n\nmutual\n -- This is not the same definition of relT, but it is equivalent.\n relT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\n -- This equation is a lemma in the original definition.\n relT {\u03c4} t1 t2 \u03c11 \u03c12 zero = \u22a4\n -- To compare this definition, note that the original k is suc n here.\n relT {\u03c4} t1 t2 \u03c11 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n -- Originally we have 0 \u2264 j < k, so j < suc n, so k - j = suc n - j.\n -- It follows that 0 < k - j \u2264 k, hence suc n - j \u2264 suc n, or n - j \u2264 n.\n -- Here, instead of binding j we bind n-j = n - j, require n - j \u2264 n, and\n -- use suc n-j instead of k - j.\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n -- The next assumption is important. This still says that evaluation consumes j steps.\n -- Since j \u2264 n, it is OK to start evaluation with n steps.\n -- Starting with (suc n) and getting suc n-j is equivalent, per eval-mono\n -- and eval-strengthen. But in practice this version is easier to use.\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 0 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n -- Here, computing t2 is allowed to take an unbounded number of steps. Having to write a number at all is annoying.\n\n relV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n relV nat v1 v2 n = v1 \u2261 v2\n relV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) n =\n \u2200 (k : \u2115) (k\u2264n : k < n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\n -- Here, in the conclusion, I'm not relating app (closure t1 \u03c11) v1 with app\n -- (closure t2 \u03c12) v2 (or some encoding of that that actually works), but the\n -- result of taking a step from that configuration. That is important, because\n -- both Pitts' \"Step-Indexed Biorthogonality: a Tutorial Example\" and\n -- \"Imperative Self-Adjusting Computation\" do the same thing (and point out it's\n -- important).\n\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono m n m\u2264n nat v1 v2 vv = vv\nrelV-mono m n m\u2264n (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) ff k k\u2264m = ff k (\u2264-trans k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 .n n-j\u2264n refl = v2 , zero , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\nlt1 : \u2200 {k n} \u2192 k < n \u2192 k \u2264 n\nlt1 (s\u2264s p) = \u2264-step p\n\n-- relV-apply : \u2200 {\u03c3 \u03c4 \u0393} (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t v1 \u03c12 n-j\n-- n1 sv1 sv2\n-- (svv : relV (\u03c3 \u21d2 \u03c4) sv1 sv2 n1)\n-- n2 tv1 tv2\n-- (tvv : relV \u03c3 tv1 tv2 (suc (suc n2)))\n-- (eq : apply sv1 tv1 n2 \u2261 Done v1 n-j) \u2192\n-- -- (tvv)\n-- -- (eqv1)\n-- \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval (app s t) \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n-- relV-apply s t v1 \u03c1 n-j n1 (closure st \u03c11) (closure st2 \u03c12) svv n2 tv1 tv2 tvv eq = {! !}\n-- -- relV-apply s t v1 \u03c12 n-j _ _ sv2 svv _ tv1 tv2 tvv eq\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\nfundamental (const (lit v)) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(intV v) .n n-j\u2264n refl = intV v , zero , refl , refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(closure t \u03c11) .n n-j\u2264n refl = closure t \u03c12 , zero , refl , (\u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k (suc n) (lt1 k\u2264n) _ _ _ \u03c1\u03c1))\nfundamental (app s t) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq with eval s \u03c11 n | inspect (eval s \u03c11) n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done sv1 n1 | [ s1eq ] with eval-dec s _ _ n n1 s1eq | eval t \u03c11 n1 | inspect (eval t \u03c11) n1\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done (closure st1 s\u03c11) n1 | [ s1eq ] | n1\u2264n | Done tv1 n2 | [ t1eq ] with eval-dec t _ _ n1 n2 t1eq\n... | n2\u2264n1 with fundamental s (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) (suc n1) (s\u2264s n1\u2264n) (eval-mono s \u03c11 (closure st1 s\u03c11) n n1 s1eq)\n | fundamental t (suc (suc n1)) \u03c11 \u03c12 (rel\u03c1-mono (suc (suc n1)) (suc (suc n)) (s\u2264s (s\u2264s n1\u2264n)) _ _ _ \u03c1\u03c1) tv1 (suc n2) (s\u2264s n2\u2264n1) (eval-mono t \u03c11 tv1 n1 n2 t1eq)\n... | sv2@(closure st2 s\u03c12) , sn3 , s2eq , svv | tv2 , tn3 , t2eq , tvv with svv (suc n2) (s\u2264s (s\u2264s n2\u2264n1)) tv1 tv2 (relV-mono _ _ (s\u2264s (n\u22641+n n2)) _ _ _ tvv ) v1 n-j (eval-dec st1 _ _ _ _ eq) eq\n... | v2 , n3 , eq2 , vv = v2 , suc (sn3 + (tn3 + n3)) , comp , vv\n where\n s2eq-adj : eval s \u03c12 (sn3 + (tn3 + n3)) \u2261 Done (closure st2 s\u03c12) (tn3 + n3)\n s2eq-adj rewrite +-comm sn3 (tn3 + n3)| cong (Done (closure st2 s\u03c12)) (sym (+-right-identity (tn3 + n3))) = eval-adjust-plus (tn3 + n3) s _ sv2 _ _ s2eq\n t2eq-adj : eval t \u03c12 (tn3 + n3) \u2261 Done tv2 n3\n t2eq-adj rewrite +-comm tn3 n3 | cong (Done tv2) (sym (+-right-identity n3)) = eval-adjust-plus n3 t _ tv2 _ _ t2eq\n\n comp : (eval s \u03c12 >>= (\u03bb sv \u2192 eval t \u03c12 >>= apply sv))\n (sn3 + (tn3 + n3))\n \u2261 Done v2 0\n comp rewrite s2eq-adj | t2eq-adj = eq2\n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | Error | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | TimeOut | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Error | [ s1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | TimeOut | [ s1eq ]\n","old_contents":"-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\" (ISAC below), POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\n-- CHEATS:\n-- \"Fuctional big-step semantics\" requires an external termination proof for the\n-- semantics. There it is also mechanized, here it isn't. Worse, the same\n-- termination problem affects some lemmas about the semantics.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen import Data.Nat.Properties.Simple\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n\n-- WARNING: ISAC's big-step semantics produces a step count as \"output\". But\n-- that would not help Agda establish termination. That's only a problem for a\n-- functional big-step semantics, not for a relational semantics.\n--\n-- So, instead, I tried to use a sort of writer monad: the interpreter gets fuel\n-- and returns the remaining fuel. That's the same trick as in \"functional\n-- big-step semantics\". That *makes* the function terminating, even though Agda\n-- cannot see this because it does not know that the returned fuel is no bigger.\n\n-- Since we focus for now on STLC, unlike that\n-- paper, we could avoid error values because we keep types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) zero = TimeOut\nevalConst (lit v) (suc n) = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\neval (app s t) \u03c1 (suc n) = (eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)) n\n\neval-const-dec : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-const-dec (lit v) zero n1 ()\neval-const-dec (lit v) (suc n0) .n0 refl = \u2264-step \u2264-refl\n\n{-# TERMINATING #-}\neval-dec : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-dec (const c) \u03c1 v n0 n1 eq = eval-const-dec c n0 n1 eq\neval-dec (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (app s t) \u03c1 v zero n1 ()\neval-dec (app s t) \u03c1 v (suc n0) n3 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv sn1 | [ seq ] with eval t \u03c1 sn1 | inspect (eval t \u03c1) sn1\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv@(closure st s\u03c1) sn1 | [ seq ] | (Done tv tn2) | [ teq ] = \u2264-step (\u2264-trans (\u2264-trans (eval-dec st _ _ _ _ eq) (eval-dec t _ _ _ _ teq)) (eval-dec s _ _ _ _ seq))\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | Error | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | TimeOut | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Error | [ seq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | TimeOut | [ seq ]\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1)\neval-const-mono (lit v) zero n1 ()\neval-const-mono (lit v) (suc n0) .n0 refl = refl\n\n-- ARGH\n{-# TERMINATING #-}\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1)\neval-mono (const c) \u03c1 v n0 n1 eq = eval-const-mono c n0 n1 eq\neval-mono (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\neval-mono (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\neval-mono (app s t) \u03c1 v zero n1 ()\neval-mono (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-mono t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n\neval-adjust-plus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1)\neval-adjust-plus zero t \u03c1 v n0 n1 eq = eq\neval-adjust-plus (suc d) t \u03c1 v n0 n1 eq = eval-mono t \u03c1 v (d + n0) (d + n1) (eval-adjust-plus d t \u03c1 v n0 n1 eq)\n\neval-const-strengthen : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1) \u2192 evalConst c n0 \u2261 Done v n1\neval-const-strengthen (lit v) zero n1 ()\neval-const-strengthen (lit v) (suc n0) .n0 refl = refl\n\n-- {-# TERMINATING #-}\n-- eval-strengthen : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-strengthen (const c) \u03c1 v n0 n1 eq = eval-const-strengthen c n0 n1 eq\n-- eval-strengthen (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq = {!!}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 (suc n0) | inspect (eval s \u03c1) (suc n0)\n-- -- eval-strengthen (app s t) \u03c1 v\u2081 (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 n0 = {!eval-strengthen s \u03c1 v n0 n1 seq !}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-strengthen t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | Error | [ seq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | TimeOut | [ seq ] = {!!}\n\n-- eval-adjust-minus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 {\u03c1 v} n0 n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-adjust-minus zero t n0 n1 eq = eq\n-- eval-adjust-minus (suc d) t n0 n1 eq = eval-adjust-minus d t n0 n1 (eval-strengthen t _ _ (d + n0) (d + n1) eq)\n\nmutual\n -- This is not the same definition of relT, but it is equivalent.\n relT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\n -- This equation is a lemma in the original definition.\n relT {\u03c4} t1 t2 \u03c11 \u03c12 zero = \u22a4\n -- To compare this definition, note that the original k is suc n here.\n relT {\u03c4} t1 t2 \u03c11 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n -- Originally we have 0 \u2264 j < k, so j < suc n, so k - j = suc n - j.\n -- It follows that 0 < k - j \u2264 k, hence suc n - j \u2264 suc n, or n - j \u2264 n.\n -- Here, instead of binding j we bind n-j = n - j, require n - j \u2264 n, and\n -- use suc n-j instead of k - j.\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n -- The next assumption is important. This still says that evaluation consumes j steps.\n -- Since j \u2264 n, it is OK to start evaluation with n steps.\n -- Starting with (suc n) and getting suc n-j is equivalent, per eval-mono\n -- and eval-strengthen. But in practice this version is easier to use.\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 0 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n -- Here, computing t2 is allowed to take an unbounded number of steps. Having to write a number at all is annoying.\n\n relV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n relV nat v1 v2 n = v1 \u2261 v2\n relV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) n =\n \u2200 (k : \u2115) (k\u2264n : k < n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\n -- Here, in the conclusion, I'm not relating app (closure t1 \u03c11) v1 with app\n -- (closure t2 \u03c12) v2 (or some encoding of that that actually works), but the\n -- result of taking a step from that configuration. That is important, because\n -- both Pitts' \"Step-Indexed Biorthogonality: a Tutorial Example\" and\n -- \"Imperative Self-Adjusting Computation\" do the same thing (and point out it's\n -- important).\n\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono m n m\u2264n nat v1 v2 vv = vv\nrelV-mono m n m\u2264n (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) ff k k\u2264m = ff k (\u2264-trans k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 .n n-j\u2264n refl = v2 , zero , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\nlt1 : \u2200 {k n} \u2192 k < n \u2192 k \u2264 n\nlt1 (s\u2264s p) = \u2264-step p\n\n-- relV-apply : \u2200 {\u03c3 \u03c4 \u0393} (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t v1 \u03c12 n-j\n-- n1 sv1 sv2\n-- (svv : relV (\u03c3 \u21d2 \u03c4) sv1 sv2 n1)\n-- n2 tv1 tv2\n-- (tvv : relV \u03c3 tv1 tv2 (suc (suc n2)))\n-- (eq : apply sv1 tv1 n2 \u2261 Done v1 n-j) \u2192\n-- -- (tvv)\n-- -- (eqv1)\n-- \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval (app s t) \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n-- relV-apply s t v1 \u03c1 n-j n1 (closure st \u03c11) (closure st2 \u03c12) svv n2 tv1 tv2 tvv eq = {! !}\n-- -- relV-apply s t v1 \u03c12 n-j _ _ sv2 svv _ tv1 tv2 tvv eq\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\nfundamental (const (lit v)) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (const (lit v)) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 .(intV v) .n n-j\u2264n refl = intV v , suc zero , refl , refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(closure t \u03c11) .n n-j\u2264n refl = closure t \u03c12 , zero , refl , (\u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k (suc n) (lt1 k\u2264n) _ _ _ \u03c1\u03c1))\nfundamental (app s t) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq with eval s \u03c11 n | inspect (eval s \u03c11) n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done sv1 n1 | [ s1eq ] with eval-dec s _ _ n n1 s1eq | eval t \u03c11 n1 | inspect (eval t \u03c11) n1\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done (closure st1 s\u03c11) n1 | [ s1eq ] | n1\u2264n | Done tv1 n2 | [ t1eq ] with eval-dec t _ _ n1 n2 t1eq\n... | n2\u2264n1 with fundamental s (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) (suc n1) (s\u2264s n1\u2264n) (eval-mono s \u03c11 (closure st1 s\u03c11) n n1 s1eq)\n | fundamental t (suc (suc n1)) \u03c11 \u03c12 (rel\u03c1-mono (suc (suc n1)) (suc (suc n)) (s\u2264s (s\u2264s n1\u2264n)) _ _ _ \u03c1\u03c1) tv1 (suc n2) (s\u2264s n2\u2264n1) (eval-mono t \u03c11 tv1 n1 n2 t1eq)\n... | sv2@(closure st2 s\u03c12) , sn3 , s2eq , svv | tv2 , tn3 , t2eq , tvv with svv (suc n2) (s\u2264s (s\u2264s n2\u2264n1)) tv1 tv2 (relV-mono _ _ (s\u2264s (n\u22641+n n2)) _ _ _ tvv ) v1 n-j (eval-dec st1 _ _ _ _ eq) eq\n... | v2 , n3 , eq2 , vv = v2 , suc (sn3 + (tn3 + n3)) , comp , vv\n where\n s2eq-adj : eval s \u03c12 (sn3 + (tn3 + n3)) \u2261 Done (closure st2 s\u03c12) (tn3 + n3)\n s2eq-adj rewrite +-comm sn3 (tn3 + n3)| cong (Done (closure st2 s\u03c12)) (sym (+-right-identity (tn3 + n3))) = eval-adjust-plus (tn3 + n3) s _ sv2 _ _ s2eq\n t2eq-adj : eval t \u03c12 (tn3 + n3) \u2261 Done tv2 n3\n t2eq-adj rewrite +-comm tn3 n3 | cong (Done tv2) (sym (+-right-identity n3)) = eval-adjust-plus n3 t _ tv2 _ _ t2eq\n\n comp : (eval s \u03c12 >>= (\u03bb sv \u2192 eval t \u03c12 >>= apply sv))\n (sn3 + (tn3 + n3))\n \u2261 Done v2 0\n comp rewrite s2eq-adj | t2eq-adj = eq2\n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | Error | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | TimeOut | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Error | [ s1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | TimeOut | [ s1eq ]\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"55b2f0787bf88248a412e30594fbe37030561ef9","subject":"Typo.","message":"Typo.\n\nIgnore-this: f327954fb8615da978c4ee2b943b6e71\n\ndarcs-hash:20120228172002-3bd4e-bc34798e02a6e5bb6901a2a5416c36d60541a168.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/Existential.agda","new_file":"notes\/Existential.agda","new_contents":"-- Tested with the development version of Agda on 27 February 2012.\n\n-- Thm: (\u2203x)A(x), (\u2200x)(A(x) \u21d2 B(x)) \u22a2 (\u2203x)B(x)\n\n-- From: Elliott Mendelson. Introduction to mathematical logic. Chapman &\n-- Hall, 4th edition, 1997, p. 155.\n\n-- Rule A4: (\u2200x)A(x) \u21d2 A(t) (with some conditions)\n-- Rule E4: A(t) \u21d2 (\u2203x)A(x) (with some conditions)\n-- Rule C: From (\u2203x)A(x) to A(t) (with some conditions)\n\n-- 1. (\u2203x)A(x) Hyp\n-- 2. (\u2200x)(A(x) \u21d2 B(x)) Hyp\n-- 3. A(b) 1, rule C\n-- 4. A(b) \u21d2 B(b) 2, rule A4\n-- 5. B(b) 4,3 MP\n-- 6. (\u2203x)B(x) 5, rule E4\n\nmodule Existential where\n\npostulate\n D : Set\n A B : D \u2192 Set\n\nmodule Witness where\n\n infixr 7 _,_\n\n data \u2203 (A : D \u2192 Set) : Set where\n _,_ : \u2200 x \u2192 A x \u2192 \u2203 A\n\n \u2203-elim : {A : D \u2192 Set}{B : Set} \u2192 \u2203 A \u2192 (\u2200 x \u2192 A x \u2192 B) \u2192 B\n \u2203-elim (x , Ax) h = h x Ax\n\n -- A proof using the existential elimination.\n prf\u2081 : \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B x) \u2192 \u2203 B\n prf\u2081 h\u2081 h\u2082 = \u2203-elim h\u2081 (\u03bb x Ax \u2192 x , (h\u2082 Ax))\n\n -- A proof using pattern matching.\n prf\u2082 : \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B x) \u2192 \u2203 B\n prf\u2082 (x , Ax) h = x , h Ax\n\nmodule NonWitness\u2081 where\n\n data \u2203 (A : D \u2192 Set) : Set where\n \u2203-intro : \u2200 {x} \u2192 A x \u2192 \u2203 A\n\n \u2203-elim : {A : D \u2192 Set}{B : Set} \u2192 \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B) \u2192 B\n \u2203-elim (\u2203-intro Ax) h = h Ax\n\n -- A proof using the existential elimination.\n prf\u2081 : \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B x) \u2192 \u2203 B\n prf\u2081 h\u2081 h\u2082 = \u2203-elim h\u2081 (\u03bb Ax \u2192 \u2203-intro (h\u2082 Ax))\n\n -- A proof using pattern matching.\n prf\u2082 : \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B x) \u2192 \u2203 B\n prf\u2082 (\u2203-intro Ax) h = \u2203-intro (h Ax)\n\nmodule NonWitness\u2082 where\n\n -- We add 3 to the fixities of the standard library.\n infix 6 \u00ac_\n\n -- The empty type.\n data \u22a5 : Set where\n\n \u22a5-elim : {A : Set} \u2192 \u22a5 \u2192 A\n \u22a5-elim ()\n\n \u00ac_ : Set \u2192 Set\n \u00ac A = A \u2192 \u22a5\n\n \u2203 : (D \u2192 Set) \u2192 Set\n \u2203 A = \u00ac (\u2200 x \u2192 \u00ac (A x))\n\n prf : \u2203 A \u2192 (\u2200 x \u2192 A x \u2192 B x) \u2192 \u2203 B\n prf h\u2081 h\u2082 h\u2083 = h\u2081 (\u03bb x Ax \u2192 h\u2083 x (h\u2082 x Ax))\n","old_contents":"-- Tested with the development version of Agda on 27 February 2012.\n\n-- Thm: (\u2203x)A(x), (\u2200x)(A(x) \u21d2 B(x)) \u22a2 (\u2203x)B(x)\n\n-- From: Elliott Mendelson. Introduction to mathematical logic. Chapman &\n-- Hall, 4th edition, 1997, p. 155.\n\n-- Rule A4: (\u2200x)A(x) \u21d2 B(t) (with some conditions)\n-- Rule E4: A(t) \u21d2 (\u2203x)A(x) (with some conditions)\n-- Rule C: From (\u2203x)A(x) to A(t) (with some conditions)\n\n-- 1. (\u2203x)A(x) Hyp\n-- 2. (\u2200x)(A(x) \u21d2 B(x)) Hyp\n-- 3. A(b) 1, rule C\n-- 4. A(b) \u21d2 B(b) 2, rule A4\n-- 5. B(b) 4,3 MP\n-- 6. (\u2203x)B(x) 5, rule E4\n\nmodule Existential where\n\npostulate\n D : Set\n A B : D \u2192 Set\n\nmodule Witness where\n\n infixr 7 _,_\n\n data \u2203 (A : D \u2192 Set) : Set where\n _,_ : \u2200 x \u2192 A x \u2192 \u2203 A\n\n \u2203-elim : {A : D \u2192 Set}{B : Set} \u2192 \u2203 A \u2192 (\u2200 x \u2192 A x \u2192 B) \u2192 B\n \u2203-elim (x , Ax) h = h x Ax\n\n -- A proof using the existential elimination.\n prf\u2081 : \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B x) \u2192 \u2203 B\n prf\u2081 h\u2081 h\u2082 = \u2203-elim h\u2081 (\u03bb x Ax \u2192 x , (h\u2082 Ax))\n\n -- A proof using pattern matching.\n prf\u2082 : \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B x) \u2192 \u2203 B\n prf\u2082 (x , Ax) h = x , h Ax\n\nmodule NonWitness\u2081 where\n\n data \u2203 (A : D \u2192 Set) : Set where\n \u2203-intro : \u2200 {x} \u2192 A x \u2192 \u2203 A\n\n \u2203-elim : {A : D \u2192 Set}{B : Set} \u2192 \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B) \u2192 B\n \u2203-elim (\u2203-intro Ax) h = h Ax\n\n -- A proof using the existential elimination.\n prf\u2081 : \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B x) \u2192 \u2203 B\n prf\u2081 h\u2081 h\u2082 = \u2203-elim h\u2081 (\u03bb Ax \u2192 \u2203-intro (h\u2082 Ax))\n\n -- A proof using pattern matching.\n prf\u2082 : \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B x) \u2192 \u2203 B\n prf\u2082 (\u2203-intro Ax) h = \u2203-intro (h Ax)\n\nmodule NonWitness\u2082 where\n\n -- We add 3 to the fixities of the standard library.\n infix 6 \u00ac_\n\n -- The empty type.\n data \u22a5 : Set where\n\n \u22a5-elim : {A : Set} \u2192 \u22a5 \u2192 A\n \u22a5-elim ()\n\n \u00ac_ : Set \u2192 Set\n \u00ac A = A \u2192 \u22a5\n\n \u2203 : (D \u2192 Set) \u2192 Set\n \u2203 A = \u00ac (\u2200 x \u2192 \u00ac (A x))\n\n prf : \u2203 A \u2192 (\u2200 x \u2192 A x \u2192 B x) \u2192 \u2203 B\n prf h\u2081 h\u2082 h\u2083 = h\u2081 (\u03bb x Ax \u2192 h\u2083 x (h\u2082 x Ax))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"136efbaf5221fe6e7c1dae7424af260ceef56102","subject":"Fixed bug. An error message was added to the agda2atp tool.","message":"Fixed bug. An error message was added to the agda2atp tool.\n\nIgnore-this: e3fe97343f77a9366ac14b0a92728536\n ++ (see agda2atp\/Test\/Fail\/NotErasedProofTerm.agda).\n\ndarcs-hash:20110322144816-3bd4e-62f67beb1ec58cc25136c00d89668a68571b8e8e.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/Bugs\/BadErrorMessage1.agda","new_file":"Draft\/Bugs\/BadErrorMessage1.agda","new_contents":"","old_contents":"-- Reported by Ana\n\nmodule Draft.Bugs.BadErrorMessage1 where\n\nopen import LTC.Base\nopen import LTC.Data.Nat\n\nS\u2238N2N : \u2200 {n} \u2192 N n \u2192 (\u2200 m \u2192 N m \u2192 N (n \u2238 m)) \u2192 \u2200 m \u2192 N m \u2192 N (succ n \u2238 m)\nS\u2238N2N {n} Nn h m = indN P' p0 pS\n where P' : D \u2192 Set\n P' x = N (succ n \u2238 x)\n postulate p0 : P' zero\n pS : \u2200 {x} \u2192 N x \u2192 P' x \u2192 P' (succ x)\n {-# ATP prove p0 #-}\n\n-- agda2atp reports:\n\n-- An internal error has occurred. Please report this as a bug.\n-- Location of the error: src\/AgdaLib\/Syntax\/DeBruijn.hs:286\n\n-- The internal type of p0 is\n\n-- \u2200 (n : D) (Nn : N n) (h : (\u2200 m \u2192 N m \u2192 N (n \u2238 m))) \u2192 ...\n\n-- The agda2atp can erase the proof term Nn, but it cannot erase the\n-- proof term h.\n\n-- TODO: To change the error message.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"abc963f0df7baaa7fe401fc451e1ad71041763d1","subject":"Search\/Searchable\/Fin.agda","message":"Search\/Searchable\/Fin.agda\n","repos":"crypto-agda\/crypto-agda","old_file":"Search\/Searchable\/Fin.agda","new_file":"Search\/Searchable\/Fin.agda","new_contents":"module Search.Searchable.Fin where\n\nopen import Function\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Nat\n\nopen import Search.Type\nopen import Search.Searchable\n\nmodule _ {A : Set}(\u03bcA : Searchable A) where\n\n sA = search \u03bcA\n\n extend : \u2200 {n} \u2192 A \u2192 (Fin n \u2192 A) \u2192 Fin (suc n) \u2192 A\n extend x g zero = x\n extend x g (suc i) = g i\n\n \u00acFin0 : Fin 0 \u2192 A\n \u00acFin0 ()\n\n -- There is one function Fin 0 \u2192 A (called abs) so this should be fine\n -- if not there is a version below that forces the domain to be non-empty\n sFun : \u2200 n \u2192 Search _ (Fin n \u2192 A)\n sFun zero op f = f \u00acFin0\n sFun (suc n) op f = sA op (\u03bb x \u2192 sFun n op (f \u2218 extend x))\n\n ind : \u2200 n \u2192 SearchInd _ (sFun n)\n ind zero P P\u2219 Pf = Pf _\n ind (suc n) P P\u2219 Pf =\n search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (\u03bb x \u2192 sFun n op (f \u2218 extend x))))\n P\u2219\n (\u03bb x \u2192 ind n (\u03bb sf \u2192 P (\u03bb op f \u2192 sf op (f \u2218 extend x)))\n P\u2219 (Pf \u2218 extend x))\n\n sumFun : \u2200 n \u2192 Sum (Fin n \u2192 A)\n sumFun n = sFun n _+_\n\n ade : \u2200 n \u2192 AdequateSum (sumFun n)\n ade n = {!!}\n\n \u03bcFun : \u2200 {n} \u2192 Searchable (Fin n \u2192 A)\n \u03bcFun = mk _ (ind _) (ade _)\n","old_contents":"module Search.Searchable.Fin where\n\nopen import Function\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Nat\n\nopen import Search.Type\nopen import Search.Searchable\n\nmodule _ {A : Set}(\u03bcA : Searchable A) where\n\n sA = search \u03bcA\n\n extend : \u2200 {n} \u2192 A \u2192 (Fin n \u2192 A) \u2192 Fin (suc n) \u2192 A\n extend x g zero = x\n extend x g (suc i) = g i\n\n \u00acFin0 : Fin 0 \u2192 A\n \u00acFin0 ()\n\n -- There is one function Fin 0 \u2192 A (called abs) so this should be fine\n -- if not there is a version below that forces the domain to be non-empty\n sFun : \u2200 n \u2192 Search _ (Fin n \u2192 A)\n sFun zero op f = f \u00acFin0\n sFun (suc n) op f = sA op (\u03bb x \u2192 sFun n op (f \u2218 extend x))\n\n ind : \u2200 n \u2192 SearchInd _ (sFun n)\n ind zero P P\u2219 Pf = Pf _\n ind (suc n) P P\u2219 Pf =\n search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (\u03bb x \u2192 sFun n op (f \u2218 extend x))))\n P\u2219\n (\u03bb x \u2192 ind n (\u03bb sf \u2192 P (\u03bb op f \u2192 sf op (f \u2218 extend x)))\n P\u2219 (Pf \u2218 extend x))\n\n \u03bcFun : \u2200 {n} \u2192 Searchable (Fin n \u2192 A)\n \u03bcFun = mk _ (ind _) {!!}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"2593d4971f53b881129ee934af1df7f0b139f956","subject":"Add curried version of Mu constructor to PropDesc model.","message":"Add curried version of Mu constructor to PropDesc model.\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/Examples\/PropositionalDesc.agda","new_file":"formalization\/agda\/Spire\/Examples\/PropositionalDesc.agda","new_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nmodule Spire.Examples.PropositionalDesc where\n\n----------------------------------------------------------------------\n\nelimEq : (A : Set) (x : A) (P : (y : A) \u2192 x \u2261 y \u2192 Set)\n \u2192 P x refl\n \u2192 (y : A) (p : x \u2261 y) \u2192 P y p\nelimEq A .x P prefl x refl = prefl\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nCases : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nCases [] P = \u22a4\nCases (l \u2237 E) P = P here \u00d7 Cases E \u03bb t \u2192 P (there t)\n\ncase : (E : Enum) (P : Tag E \u2192 Set) (cs : Cases E P) (t : Tag E) \u2192 P t\ncase (l \u2237 E) P (c , cs) here = c\ncase (l \u2237 E) P (c , cs) (there t) = case E (\u03bb t \u2192 P (there t)) cs t\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n `End : (i : I) \u2192 Desc I\n `Rec : (i : I) (D : Desc I) \u2192 Desc I\n `Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n `RecFun : (A : Set) (B : A \u2192 I) (D : Desc I) \u2192 Desc I\n\nISet : Set \u2192 Set\u2081\nISet I = I \u2192 Set\n\nEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 ISet I\nEl I (`End j) X i = j \u2261 i\nEl I (`Rec j D) X i = X j \u00d7 El I D X i\nEl I (`Arg A B) X i = \u03a3 A (\u03bb a \u2192 El I (B a) X i)\nEl I (`RecFun A B D) X i = ((a : A) \u2192 X (B a)) \u00d7 El I D X i\n\ndata \u03bc (I : Set) (D : Desc I) (i : I) : Set where\n con : El I D (\u03bc I D) i \u2192 \u03bc I D i\n\nAll : (I : Set) (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El I D X i) \u2192 Set\nAll I (`End j) X P i q = \u22a4\nAll I (`Rec j D) X P i (x , xs) = P j x \u00d7 All I D X P i xs\nAll I (`Arg A B) X P i (a , b) = All I (B a) X P i b\nAll I (`RecFun A B D) X P i (f , xs) = ((a : A) \u2192 P (B a) (f a)) \u00d7 All I D X P i xs\n\ncaseD : (E : Enum) (I : Set) (cs : Cases E (\u03bb _ \u2192 Desc I)) (t : Tag E) \u2192 Desc I\ncaseD E I cs t = case E (\u03bb _ \u2192 Desc I) cs t\n\n----------------------------------------------------------------------\n\nind :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : (i : I) (xs : El I D (\u03bc I D) i) \u2192 All I D (\u03bc I D) P i xs \u2192 P i (con xs))\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\n\nhyps :\n (I : Set)\n (D\u2081 : Desc I)\n (P : (i : I) \u2192 \u03bc I D\u2081 i \u2192 Set)\n (pcon : (i : I) (xs : El I D\u2081 (\u03bc I D\u2081) i) \u2192 All I D\u2081 (\u03bc I D\u2081) P i xs \u2192 P i (con xs))\n (D\u2082 : Desc I)\n (i : I)\n (xs : El I D\u2082 (\u03bc I D\u2081) i)\n \u2192 All I D\u2082 (\u03bc I D\u2081) P i xs\n\nind I D P pcon i (con xs) = pcon i xs (hyps I D P pcon D i xs)\n\nhyps I D P pcon (`End j) i q = tt\nhyps I D P pcon (`Rec j A) i (x , xs) = ind I D P pcon j x , hyps I D P pcon A i xs\nhyps I D P pcon (`Arg A B) i (a , b) = hyps I D P pcon (B a) i b\nhyps I D P pcon (`RecFun A B E) i (f , xs) = (\u03bb a \u2192 ind I D P pcon (B a) (f a)) , hyps I D P pcon E i xs\n\n----------------------------------------------------------------------\n\nUncurried : (I : Set) (D : Desc I) (X : I \u2192 Set) \u2192 Set\nUncurried I D X = {i : I} \u2192 El I D X i \u2192 X i\n\nCurried : (I : Set) (D : Desc I) (X : I \u2192 Set) \u2192 Set\nCurried I (`End i) X = X i\nCurried I (`Rec j D) X = (x : X j) \u2192 Curried I D X\nCurried I (`Arg A B) X = (a : A) \u2192 Curried I (B a) X\nCurried I (`RecFun A B D) X = ((a : A) \u2192 X (B a)) \u2192 Curried I D X\n\ncurry : (I : Set) (D : Desc I) (X : I \u2192 Set)\n (cn : Uncurried I D X) \u2192 Curried I D X\ncurry I (`End i) X cn = cn refl\ncurry I (`Rec i D) X cn = \u03bb x \u2192 curry I D X (\u03bb xs \u2192 cn (x , xs))\ncurry I (`Arg A B) X cn = \u03bb a \u2192 curry I (B a) X (\u03bb xs \u2192 cn (a , xs))\ncurry I (`RecFun A B D) X cn = \u03bb f \u2192 curry I D X (\u03bb xs \u2192 cn (f , xs))\n\nuncurry : (I : Set) (D : Desc I) (X : I \u2192 Set)\n (cn : Curried I D X) \u2192 Uncurried I D X\nuncurry I (`End i) X cn refl = cn\nuncurry I (`Rec i D) X cn (x , xs) = uncurry I D X (cn x) xs\nuncurry I (`Arg A B) X cn (a , xs) = uncurry I (B a) X (cn a) xs\nuncurry I (`RecFun A B D) X cn (f , xs) = uncurry I D X (cn f) xs\n\ncon2 : (I : Set) (D : Desc I) \u2192 Curried I D (\u03bc I D)\ncon2 I D = curry I D (\u03bc I D) con\n\n----------------------------------------------------------------------\n\nmodule Sugared where\n\n data \u2115T : Set where `zero `suc : \u2115T\n data VecT : Set where `nil `cons : VecT\n\n \u2115D : Desc \u22a4\n \u2115D = `Arg \u2115T \u03bb\n { `zero \u2192 `End tt\n ; `suc \u2192 `Rec tt (`End tt)\n }\n\n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n\n zero : \u2115 tt\n zero = con (`zero , refl)\n\n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (`suc , n , refl)\n\n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg VecT \u03bb\n { `nil \u2192 `End zero\n ; `cons \u2192 `Arg (\u2115 tt) \u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n))\n }\n\n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n\n nil : (A : Set) \u2192 Vec A zero\n nil A = con (`nil , refl)\n\n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (`cons , n , x , xs , refl)\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 n\n ; tt (`suc , m , q) (ih , tt) n \u2192 suc (ih n)\n }\n )\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 zero\n ; tt (`suc , m , q) (ih , tt) n \u2192 add n (ih n)\n }\n )\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) ih n ys \u2192 ys\n ; .(con (`suc , m , refl)) (`cons , m , x , xs , refl) (ih , tt) n ys \u2192 cons A (add m n) x (ih n ys)\n }\n )\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) tt \u2192 nil A\n ; .(con (`suc , n , refl)) (`cons , n , xs , xss , refl) (ih , tt) \u2192 append A m xs (mult n m) ih\n }\n )\n\n----------------------------------------------------------------------\n\nmodule Desugared where\n\n \u2115T : Enum\n \u2115T = \"zero\" \u2237 \"suc\" \u2237 []\n \n VecT : Enum\n VecT = \"nil\" \u2237 \"cons\" \u2237 []\n \n \u2115C : Tag \u2115T \u2192 Desc \u22a4\n \u2115C = caseD \u2115T \u22a4\n ( `End tt\n , `Rec tt (`End tt)\n , tt\n )\n \n \u2115D : Desc \u22a4\n \u2115D = `Arg (Tag \u2115T) \u2115C\n \n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n \n zero : \u2115 tt\n zero = con (here , refl)\n \n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (there here , n , refl)\n \n zero2 : \u2115 tt\n zero2 = con2 \u22a4 \u2115D here\n\n suc2 : \u2115 tt \u2192 \u2115 tt\n suc2 = con2 \u22a4 \u2115D (there here)\n\n VecC : (A : Set) \u2192 Tag VecT \u2192 Desc (\u2115 tt)\n VecC A = caseD VecT (\u2115 tt)\n ( `End zero\n , `Arg (\u2115 tt) (\u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n)))\n , tt\n )\n \n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg (Tag VecT) (VecC A)\n \n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n \n nil : (A : Set) \u2192 Vec A zero\n nil A = con (here , refl)\n \n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (there here , n , x , xs , refl)\n \n nil2 : (A : Set) \u2192 Vec A zero\n nil2 A = con2 (\u2115 tt) (VecD A) here\n\n cons2 : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons2 A = con2 (\u2115 tt) (VecD A) (there here)\n\n----------------------------------------------------------------------\n\n module Induction where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 n)\n , (\u03bb m,q ih,tt n \u2192 suc (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 zero)\n , (\u03bb m,q ih,tt n \u2192 add n (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb m t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) m)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)) m (t , c))\n (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n )\n ( (\u03bb q ih n ys \u2192 subst (\u03bb m \u2192 Vec A (add m n)) q ys)\n , (\u03bb m',x,xs,q ih,tt n ys \u2192\n let m' = proj\u2081 m',x,xs,q\n x = proj\u2081 (proj\u2082 m',x,xs,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 m',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb m \u2192 Vec A (add m n)) q (cons A (add m' n) x (ih n ys))\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC (Vec A m) t) (Vec (Vec A m)) n)\n (ih : All (\u2115 tt) (VecD (Vec A m)) (Vec (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m)) n (t , c))\n \u2192 Vec A (mult n m)\n )\n ( (\u03bb q ih \u2192 subst (\u03bb n \u2192 Vec A (mult n m)) q (nil A))\n , (\u03bb n',xs,xss,q ih,tt \u2192\n let n' = proj\u2081 n',xs,xss,q\n xs = proj\u2081 (proj\u2082 n',xs,xss,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 n',xs,xss,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb n \u2192 Vec A (mult n m)) q (append A m xs (mult n' m) ih)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n module Eliminator where\n\n elim\u2115 : (P : (\u2115 tt) \u2192 Set)\n (pzero : P zero)\n (psuc : (m : \u2115 tt) \u2192 P m \u2192 P (suc m))\n (n : \u2115 tt)\n \u2192 P n\n elim\u2115 P pzero psuc = ind \u22a4 \u2115D (\u03bb u n \u2192 P n)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 P n) u (t , c))\n \u2192 P (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (here , q)))\n pzero\n u q\n )\n , (\u03bb n,q ih,tt \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (there here , proj\u2081 n,q , q)))\n (psuc (proj\u2081 n,q) (proj\u2081 ih,tt))\n u (proj\u2082 n,q)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n\n elimVec : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set)\n (pnil : P zero (nil A))\n (pcons : (n : \u2115 tt) (a : A) (xs : Vec A n) \u2192 P n xs \u2192 P (suc n) (cons A n a xs))\n (n : \u2115 tt)\n (xs : Vec A n)\n \u2192 P n xs\n elimVec A P pnil pcons = ind (\u2115 tt) (VecD A) (\u03bb n xs \u2192 P n xs)\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) n)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb n xs \u2192 P n xs) n (t , c))\n \u2192 P n (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq (\u2115 tt) zero (\u03bb n q \u2192 P n (con (here , q)))\n pnil\n n q\n )\n , (\u03bb n',x,xs,q ih,tt \u2192\n let n' = proj\u2081 n',x,xs,q\n x = proj\u2081 (proj\u2082 n',x,xs,q)\n xs = proj\u2081 (proj\u2082 (proj\u2082 n',x,xs,q))\n q = proj\u2082 (proj\u2082 (proj\u2082 n',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n elimEq (\u2115 tt) (suc n') (\u03bb n q \u2192 P n (con (there here , n' , x , xs , q)))\n (pcons n' x xs ih )\n n q\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elimVec A (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elimVec (Vec A m) (\u03bb n xss \u2192 Vec A (mult n m))\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n \n----------------------------------------------------------------------\n","old_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nmodule Spire.Examples.PropositionalDesc where\n\n----------------------------------------------------------------------\n\nelimEq : (A : Set) (x : A) (P : (y : A) \u2192 x \u2261 y \u2192 Set)\n \u2192 P x refl\n \u2192 (y : A) (p : x \u2261 y) \u2192 P y p\nelimEq A .x P prefl x refl = prefl\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nCases : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nCases [] P = \u22a4\nCases (l \u2237 E) P = P here \u00d7 Cases E \u03bb t \u2192 P (there t)\n\ncase : (E : Enum) (P : Tag E \u2192 Set) (cs : Cases E P) (t : Tag E) \u2192 P t\ncase (l \u2237 E) P (c , cs) here = c\ncase (l \u2237 E) P (c , cs) (there t) = case E (\u03bb t \u2192 P (there t)) cs t\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n `End : (i : I) \u2192 Desc I\n `Rec : (i : I) (D : Desc I) \u2192 Desc I\n `Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n `RecFun : (A : Set) (B : A \u2192 I) (D : Desc I) \u2192 Desc I\n\nISet : Set \u2192 Set\u2081\nISet I = I \u2192 Set\n\nEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 ISet I\nEl I (`End j) X i = j \u2261 i\nEl I (`Rec j D) X i = X j \u00d7 El I D X i\nEl I (`Arg A B) X i = \u03a3 A (\u03bb a \u2192 El I (B a) X i)\nEl I (`RecFun A B D) X i = ((a : A) \u2192 X (B a)) \u00d7 El I D X i\n\ndata \u03bc (I : Set) (D : Desc I) (i : I) : Set where\n con : El I D (\u03bc I D) i \u2192 \u03bc I D i\n\nAll : (I : Set) (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El I D X i) \u2192 Set\nAll I (`End j) X P i q = \u22a4\nAll I (`Rec j D) X P i (x , xs) = P j x \u00d7 All I D X P i xs\nAll I (`Arg A B) X P i (a , b) = All I (B a) X P i b\nAll I (`RecFun A B D) X P i (f , xs) = ((a : A) \u2192 P (B a) (f a)) \u00d7 All I D X P i xs\n\ncaseD : (E : Enum) (I : Set) (cs : Cases E (\u03bb _ \u2192 Desc I)) (t : Tag E) \u2192 Desc I\ncaseD E I cs t = case E (\u03bb _ \u2192 Desc I) cs t\n\n----------------------------------------------------------------------\n\nind :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : (i : I) (xs : El I D (\u03bc I D) i) \u2192 All I D (\u03bc I D) P i xs \u2192 P i (con xs))\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\n\nhyps :\n (I : Set)\n (D\u2081 : Desc I)\n (P : (i : I) \u2192 \u03bc I D\u2081 i \u2192 Set)\n (pcon : (i : I) (xs : El I D\u2081 (\u03bc I D\u2081) i) \u2192 All I D\u2081 (\u03bc I D\u2081) P i xs \u2192 P i (con xs))\n (D\u2082 : Desc I)\n (i : I)\n (xs : El I D\u2082 (\u03bc I D\u2081) i)\n \u2192 All I D\u2082 (\u03bc I D\u2081) P i xs\n\nind I D P pcon i (con xs) = pcon i xs (hyps I D P pcon D i xs)\n\nhyps I D P pcon (`End j) i q = tt\nhyps I D P pcon (`Rec j A) i (x , xs) = ind I D P pcon j x , hyps I D P pcon A i xs\nhyps I D P pcon (`Arg A B) i (a , b) = hyps I D P pcon (B a) i b\nhyps I D P pcon (`RecFun A B E) i (f , xs) = (\u03bb a \u2192 ind I D P pcon (B a) (f a)) , hyps I D P pcon E i xs\n\n----------------------------------------------------------------------\n\nmodule Sugared where\n\n data \u2115T : Set where `zero `suc : \u2115T\n data VecT : Set where `nil `cons : VecT\n\n \u2115D : Desc \u22a4\n \u2115D = `Arg \u2115T \u03bb\n { `zero \u2192 `End tt\n ; `suc \u2192 `Rec tt (`End tt)\n }\n\n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n\n zero : \u2115 tt\n zero = con (`zero , refl)\n\n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (`suc , n , refl)\n\n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg VecT \u03bb\n { `nil \u2192 `End zero\n ; `cons \u2192 `Arg (\u2115 tt) \u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n))\n }\n\n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n\n nil : (A : Set) \u2192 Vec A zero\n nil A = con (`nil , refl)\n\n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (`cons , n , x , xs , refl)\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 n\n ; tt (`suc , m , q) (ih , tt) n \u2192 suc (ih n)\n }\n )\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 zero\n ; tt (`suc , m , q) (ih , tt) n \u2192 add n (ih n)\n }\n )\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) ih n ys \u2192 ys\n ; .(con (`suc , m , refl)) (`cons , m , x , xs , refl) (ih , tt) n ys \u2192 cons A (add m n) x (ih n ys)\n }\n )\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) tt \u2192 nil A\n ; .(con (`suc , n , refl)) (`cons , n , xs , xss , refl) (ih , tt) \u2192 append A m xs (mult n m) ih\n }\n )\n\n----------------------------------------------------------------------\n\nmodule Desugared where\n\n \u2115T : Enum\n \u2115T = \"zero\" \u2237 \"suc\" \u2237 []\n \n VecT : Enum\n VecT = \"nil\" \u2237 \"cons\" \u2237 []\n \n \u2115C : Tag \u2115T \u2192 Desc \u22a4\n \u2115C = caseD \u2115T \u22a4\n ( `End tt\n , `Rec tt (`End tt)\n , tt\n )\n \n \u2115D : Desc \u22a4\n \u2115D = `Arg (Tag \u2115T) \u2115C\n \n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n \n zero : \u2115 tt\n zero = con (here , refl)\n \n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (there here , n , refl)\n \n VecC : (A : Set) \u2192 Tag VecT \u2192 Desc (\u2115 tt)\n VecC A = caseD VecT (\u2115 tt)\n ( `End zero\n , `Arg (\u2115 tt) (\u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n)))\n , tt\n )\n \n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg (Tag VecT) (VecC A)\n \n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n \n nil : (A : Set) \u2192 Vec A zero\n nil A = con (here , refl)\n \n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (there here , n , x , xs , refl)\n \n----------------------------------------------------------------------\n\n module Induction where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 n)\n , (\u03bb m,q ih,tt n \u2192 suc (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 zero)\n , (\u03bb m,q ih,tt n \u2192 add n (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb m t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) m)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)) m (t , c))\n (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n )\n ( (\u03bb q ih n ys \u2192 subst (\u03bb m \u2192 Vec A (add m n)) q ys)\n , (\u03bb m',x,xs,q ih,tt n ys \u2192\n let m' = proj\u2081 m',x,xs,q\n x = proj\u2081 (proj\u2082 m',x,xs,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 m',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb m \u2192 Vec A (add m n)) q (cons A (add m' n) x (ih n ys))\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC (Vec A m) t) (Vec (Vec A m)) n)\n (ih : All (\u2115 tt) (VecD (Vec A m)) (Vec (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m)) n (t , c))\n \u2192 Vec A (mult n m)\n )\n ( (\u03bb q ih \u2192 subst (\u03bb n \u2192 Vec A (mult n m)) q (nil A))\n , (\u03bb n',xs,xss,q ih,tt \u2192\n let n' = proj\u2081 n',xs,xss,q\n xs = proj\u2081 (proj\u2082 n',xs,xss,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 n',xs,xss,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb n \u2192 Vec A (mult n m)) q (append A m xs (mult n' m) ih)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n module Eliminator where\n\n elim\u2115 : (P : (\u2115 tt) \u2192 Set)\n (pzero : P zero)\n (psuc : (m : \u2115 tt) \u2192 P m \u2192 P (suc m))\n (n : \u2115 tt)\n \u2192 P n\n elim\u2115 P pzero psuc = ind \u22a4 \u2115D (\u03bb u n \u2192 P n)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 P n) u (t , c))\n \u2192 P (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (here , q)))\n pzero\n u q\n )\n , (\u03bb n,q ih,tt \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (there here , proj\u2081 n,q , q)))\n (psuc (proj\u2081 n,q) (proj\u2081 ih,tt))\n u (proj\u2082 n,q)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n\n elimVec : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set)\n (pnil : P zero (nil A))\n (pcons : (n : \u2115 tt) (a : A) (xs : Vec A n) \u2192 P n xs \u2192 P (suc n) (cons A n a xs))\n (n : \u2115 tt)\n (xs : Vec A n)\n \u2192 P n xs\n elimVec A P pnil pcons = ind (\u2115 tt) (VecD A) (\u03bb n xs \u2192 P n xs)\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) n)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb n xs \u2192 P n xs) n (t , c))\n \u2192 P n (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq (\u2115 tt) zero (\u03bb n q \u2192 P n (con (here , q)))\n pnil\n n q\n )\n , (\u03bb n',x,xs,q ih,tt \u2192\n let n' = proj\u2081 n',x,xs,q\n x = proj\u2081 (proj\u2082 n',x,xs,q)\n xs = proj\u2081 (proj\u2082 (proj\u2082 n',x,xs,q))\n q = proj\u2082 (proj\u2082 (proj\u2082 n',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n elimEq (\u2115 tt) (suc n') (\u03bb n q \u2192 P n (con (there here , n' , x , xs , q)))\n (pcons n' x xs ih )\n n q\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elimVec A (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elimVec (Vec A m) (\u03bb n xss \u2192 Vec A (mult n m))\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n \n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"239bfdccd7560ddd4c55a28bd209ee38842311cd","subject":"figured out the last cecastfail case","message":"figured out the last cecastfail case\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress-checks.agda","new_file":"progress-checks.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import type-assignment-unicity\nopen import lemmas-progress-checks\n\n-- taken together, the theorems in this file argue that for any expression\n-- d, at most one summand of the labeled sum that results from progress may\n-- be true at any time, i.e. that boxed values, indeterminates, cast\n-- errors, and expressions that step are pairwise disjoint. (note that as a\n-- consequence of currying and comutativity of products, this means that\n-- there are six theorems to prove)\nmodule progress-checks where\n -- boxed values and indeterminates are disjoint\n boxedval-not-indet : \u2200{d} \u2192 d boxedval \u2192 d indet \u2192 \u22a5\n boxedval-not-indet (BVVal VConst) ()\n boxedval-not-indet (BVVal VLam) ()\n boxedval-not-indet (BVArrCast x bv) (ICastArr x\u2081 ind) = boxedval-not-indet bv ind\n boxedval-not-indet (BVHoleCast x bv) (ICastGroundHole x\u2081 ind) = boxedval-not-indet bv ind\n boxedval-not-indet (BVHoleCast x bv) (ICastHoleGround x\u2081 ind x\u2082) = boxedval-not-indet bv ind\n\n -- boxed values and errors are disjoint\n boxedval-not-err : \u2200{d} \u2192 d boxedval \u2192 d casterr \u2192 \u22a5\n boxedval-not-err (BVVal ()) (CECastFail x\u2081 x\u2082 x\u2083 x\u2084)\n boxedval-not-err (BVHoleCast x bv) (CECastFail x\u2081 x\u2082 () x\u2084)\n boxedval-not-err (BVArrCast x bv) (CECong FHOuter er) = boxedval-not-err bv (ce-castarr er)\n boxedval-not-err (BVArrCast x bv) (CECong (FHCast x\u2081) er) = boxedval-not-err bv (CECong x\u2081 er)\n boxedval-not-err (BVHoleCast x bv) (CECong FHOuter er) = boxedval-not-err bv (ce-castth er)\n boxedval-not-err (BVHoleCast x bv) (CECong (FHCast x\u2081) er) = boxedval-not-err bv (CECong x\u2081 er)\n boxedval-not-err (BVVal x) (CECong FHOuter er) = boxedval-not-err (BVVal x) er\n boxedval-not-err (BVVal ()) (CECong (FHAp1 x\u2081) er)\n boxedval-not-err (BVVal ()) (CECong (FHAp2 x\u2081 x\u2082) er)\n boxedval-not-err (BVVal ()) (CECong (FHNEHole x\u2081) er)\n boxedval-not-err (BVVal ()) (CECong (FHCast x\u2081) er)\n\n -- boxed values and expressions that step are disjoint\n boxedval-not-step : \u2200{d} \u2192 d boxedval \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n boxedval-not-step (BVVal VConst) (d' , Step FHOuter () x\u2083)\n boxedval-not-step (BVVal VLam) (d' , Step FHOuter () x\u2083)\n boxedval-not-step (BVArrCast x bv) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n boxedval-not-step (BVArrCast x bv) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = boxedval-not-step bv (_ , Step x\u2081 x\u2082 x\u2083)\n boxedval-not-step (BVHoleCast () bv) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n boxedval-not-step (BVHoleCast x bv) (d' , Step FHOuter (ITCastSucceed x\u2081 ()) FHOuter)\n boxedval-not-step (BVHoleCast GHole bv) (_ , Step FHOuter (ITGround x\u2081 x\u2082) FHOuter) = x\u2082 refl\n boxedval-not-step (BVHoleCast x bv) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = boxedval-not-step bv (_ , Step x\u2081 x\u2082 x\u2083)\n\n -- todo: what class of P is this true for?\n -- lem-something : \u2200{ d \u03b5 d'} \u2192 d == \u03b5 \u27e6 d' \u27e7 \u2192 P d' \u2192 P d\n\n mutual\n -- indeterminates and errors are disjoint\n indet-not-err : \u2200{d} \u2192 d indet \u2192 d casterr \u2192 \u22a5\n indet-not-err IEHole (CECong FHOuter err) = indet-not-err IEHole err\n indet-not-err (INEHole x) (CECong FHOuter err) = final-not-err x (ce-nehole err)\n indet-not-err (INEHole x) (CECong (FHNEHole x\u2081) err) = final-not-err x (CECong x\u2081 err)\n indet-not-err (IAp x indet x\u2081) (CECong FHOuter err)\n with ce-ap err\n ... | Inl d1err = indet-not-err indet d1err\n ... | Inr d2err = final-not-err x\u2081 d2err\n indet-not-err (IAp x indet x\u2081) (CECong (FHAp1 x\u2082) err) = indet-not-err indet (CECong x\u2082 err)\n indet-not-err (IAp x indet x\u2081) (CECong (FHAp2 x\u2082 x\u2083) err) = final-not-err x\u2081 (CECong x\u2083 err)\n indet-not-err (ICastArr x indet) (CECong FHOuter err) = indet-not-err indet (ce-castarr err)\n indet-not-err (ICastArr x indet) (CECong (FHCast x\u2081) err) = indet-not-err indet (CECong x\u2081 err)\n indet-not-err (ICastGroundHole x indet) (CECastFail x\u2081 x\u2082 () x\u2084)\n indet-not-err (ICastGroundHole x indet) (CECong FHOuter err) = indet-not-err indet (ce-castth err)\n indet-not-err (ICastGroundHole x indet) (CECong (FHCast x\u2081) err) = indet-not-err indet (CECong x\u2081 err)\n indet-not-err (ICastHoleGround x indet x\u2081) (CECastFail x\u2082 x\u2083 x\u2084 x\u2085) = x _ _ refl\n indet-not-err (ICastHoleGround x indet x\u2081) (CECong FHOuter err) = indet-not-err indet (ce-castht err x)\n indet-not-err (ICastHoleGround x indet x\u2081) (CECong (FHCast x\u2082) err) = indet-not-err indet (CECong x\u2082 err)\n\n -- final expressions are not errors\n final-not-err : \u2200{d} \u2192 d final \u2192 d casterr \u2192 \u22a5\n final-not-err (FBoxed x) err = boxedval-not-err x err\n final-not-err (FIndet x) err = indet-not-err x err\n\n mutual\n -- indeterminates and expressions that step are disjoint\n indet-not-step : \u2200{d} \u2192 d indet \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n indet-not-step IEHole (d' , Step FHOuter () FHOuter)\n indet-not-step (INEHole x) (d' , Step FHOuter () FHOuter)\n indet-not-step (INEHole x) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = final-sub-not-trans x x\u2081 x\u2082\n indet-not-step (IAp x\u2081 () x\u2082) (_ , Step FHOuter (ITLam x\u2083) FHOuter)\n indet-not-step (IAp x (ICastArr x\u2081 ind) x\u2082) (_ , Step FHOuter (ITApCast x\u2083 x\u2084) FHOuter) = x _ _ _ _ _ refl\n indet-not-step (IAp x ind _) (_ , Step (FHAp1 x\u2082) x\u2083 (FHAp1 x\u2084)) = indet-not-step ind (_ , Step x\u2082 x\u2083 x\u2084)\n indet-not-step (IAp x ind f) (_ , Step (FHAp2 x\u2082 x\u2083) x\u2084 (FHAp2 x\u2085 x\u2086)) = final-not-step f (_ , Step x\u2083 x\u2084 x\u2086)\n indet-not-step (ICastArr x ind) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n indet-not-step (ICastArr x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n indet-not-step (ICastGroundHole () ind) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n indet-not-step (ICastGroundHole x ind) (d' , Step FHOuter (ITCastSucceed x\u2081 ()) FHOuter)\n indet-not-step (ICastGroundHole GHole ind) (_ , Step FHOuter (ITGround _ y) FHOuter) = y refl\n indet-not-step (ICastGroundHole x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n indet-not-step (ICastHoleGround x ind ()) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n indet-not-step (ICastHoleGround x ind g) (d' , Step FHOuter (ITCastSucceed x\u2081 x\u2082) FHOuter) = x _ _ refl\n indet-not-step (ICastHoleGround x ind GHole) (_ , Step FHOuter (ITExpand x\u2081 x\u2082) FHOuter) = x\u2082 refl\n indet-not-step (ICastHoleGround x ind g) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n\n -- final expressions don't step\n final-not-step : \u2200{d} \u2192 d final \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 \u22a5\n final-not-step (FBoxed x) stp = boxedval-not-step x stp\n final-not-step (FIndet x) stp = indet-not-step x stp\n\n -- errors and expressions that step are disjoint\n err-not-step : \u2200{d} \u2192 d casterr \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n -- cast fail cases\n err-not-step (CECastFail x x\u2081 () x\u2083) (_ , Step FHOuter (ITCastID x\u2084) FHOuter)\n err-not-step (CECastFail x x\u2081 x\u2082 x\u2083) (_ , Step FHOuter (ITCastSucceed x\u2084 x\u2085) FHOuter) = x\u2083 refl\n err-not-step (CECastFail x x\u2081 GHole x\u2083) (_ , Step FHOuter (ITExpand x\u2084 x\u2085) FHOuter) = x\u2085 refl\n err-not-step (CECastFail x () x\u2082 x\u2083) (_ , Step (FHCast FHOuter) (ITCastID x\u2084) (FHCast FHOuter))\n err-not-step (CECastFail x () x\u2082 x\u2083) (_ , Step (FHCast FHOuter) (ITCastSucceed x\u2084 x\u2085) (FHCast FHOuter))\n err-not-step (CECastFail x GHole x\u2082 x\u2083) (_ , Step (FHCast FHOuter) (ITGround x\u2084 x\u2085) (FHCast FHOuter)) = x\u2085 refl\n err-not-step (CECastFail x x\u2081 x\u2082 x\u2083) (_ , Step (FHCast (FHCast x\u2084)) x\u2085 (FHCast (FHCast x\u2086))) = final-not-step x (_ , Step x\u2084 x\u2085 x\u2086)\n\n -- congruence cases\n err-not-step (CECong FHOuter ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = err-not-step ce (\u03c01 , Step FHOuter x\u2082 FHOuter)\n err-not-step (CECong (FHAp1 FHOuter) (CECong FHOuter ce)) (_ , Step FHOuter (ITLam x\u2082) FHOuter) = boxedval-not-err (BVVal VLam) ce\n err-not-step (CECong (FHAp1 x) ce) (_ , Step FHOuter (ITApCast x\u2081 x\u2082) FHOuter) = {!!} -- fe x\u2081 (CECong {!ce-out-cast ce!} ce)\n err-not-step (CECong (FHAp2 x x\u2081) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!ce-out-cast ce x\u2081!}\n err-not-step (CECong (FHNEHole x) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!!}\n err-not-step (CECong (FHCast x) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!!}\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHAp1 x\u2081) x\u2082 (FHAp1 x\u2083))\n with ce-ap ce\n ... | Inl d1err = err-not-step d1err (_ , Step x\u2081 x\u2082 x\u2083)\n ... | Inr d2err = {!Step x\u2081 x\u2082 x\u2083!} -- cyrus this is a counter example: d2 is a casterror but d1 isn't yet a value so the whole thing steps\n err-not-step (CECong (FHAp1 x) ce) (_ , Step (FHAp1 x\u2081) x\u2082 (FHAp1 x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n err-not-step (CECong (FHAp2 x x\u2081) ce) (_ , Step (FHAp1 x\u2082) x\u2083 (FHAp1 x\u2084)) = final-not-step x (_ , Step x\u2082 x\u2083 x\u2084)\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHAp2 x\u2081 x\u2082) x\u2083 (FHAp2 x\u2084 x\u2085))\n with ce-ap ce\n ... | Inl d1err = final-not-err x\u2084 d1err\n ... | Inr d2err = err-not-step d2err (_ , Step x\u2082 x\u2083 x\u2085)\n err-not-step (CECong (FHAp1 x) ce) (_ , Step (FHAp2 x\u2081 x\u2082) x\u2083 (FHAp2 x\u2084 x\u2085)) = {!!}\n err-not-step (CECong (FHAp2 x x\u2081) ce) (_ , Step (FHAp2 x\u2082 x\u2083) x\u2084 (FHAp2 x\u2085 x\u2086)) = {!!}\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = err-not-step (ce-nehole ce) (_ , Step x\u2081 x\u2082 x\u2083)\n err-not-step (CECong (FHNEHole x) ce) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = {!!} -- err-not-step {!!} (_ , Step x\u2081 x\u2082 x\u2083) -- this might not work\n err-not-step (CECong (FHCast x) ce) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import type-assignment-unicity\nopen import lemmas-progress-checks\n\n-- taken together, the theorems in this file argue that for any expression\n-- d, at most one summand of the labeled sum that results from progress may\n-- be true at any time, i.e. that boxed values, indeterminates, cast\n-- errors, and expressions that step are pairwise disjoint. (note that as a\n-- consequence of currying and comutativity of products, this means that\n-- there are six theorems to prove)\nmodule progress-checks where\n -- boxed values and indeterminates are disjoint\n boxedval-not-indet : \u2200{d} \u2192 d boxedval \u2192 d indet \u2192 \u22a5\n boxedval-not-indet (BVVal VConst) ()\n boxedval-not-indet (BVVal VLam) ()\n boxedval-not-indet (BVArrCast x bv) (ICastArr x\u2081 ind) = boxedval-not-indet bv ind\n boxedval-not-indet (BVHoleCast x bv) (ICastGroundHole x\u2081 ind) = boxedval-not-indet bv ind\n boxedval-not-indet (BVHoleCast x bv) (ICastHoleGround x\u2081 ind x\u2082) = boxedval-not-indet bv ind\n\n -- boxed values and errors are disjoint\n boxedval-not-err : \u2200{d} \u2192 d boxedval \u2192 d casterr \u2192 \u22a5\n boxedval-not-err (BVVal ()) (CECastFail x\u2081 x\u2082 x\u2083 x\u2084)\n boxedval-not-err (BVHoleCast x bv) (CECastFail x\u2081 x\u2082 () x\u2084)\n boxedval-not-err (BVArrCast x bv) (CECong FHOuter er) = boxedval-not-err bv (ce-castarr er)\n boxedval-not-err (BVArrCast x bv) (CECong (FHCast x\u2081) er) = boxedval-not-err bv (CECong x\u2081 er)\n boxedval-not-err (BVHoleCast x bv) (CECong FHOuter er) = boxedval-not-err bv (ce-castth er)\n boxedval-not-err (BVHoleCast x bv) (CECong (FHCast x\u2081) er) = boxedval-not-err bv (CECong x\u2081 er)\n boxedval-not-err (BVVal x) (CECong FHOuter er) = boxedval-not-err (BVVal x) er\n boxedval-not-err (BVVal ()) (CECong (FHAp1 x\u2081) er)\n boxedval-not-err (BVVal ()) (CECong (FHAp2 x\u2081 x\u2082) er)\n boxedval-not-err (BVVal ()) (CECong (FHNEHole x\u2081) er)\n boxedval-not-err (BVVal ()) (CECong (FHCast x\u2081) er)\n\n -- boxed values and expressions that step are disjoint\n boxedval-not-step : \u2200{d} \u2192 d boxedval \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n boxedval-not-step (BVVal VConst) (d' , Step FHOuter () x\u2083)\n boxedval-not-step (BVVal VLam) (d' , Step FHOuter () x\u2083)\n boxedval-not-step (BVArrCast x bv) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n boxedval-not-step (BVArrCast x bv) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = boxedval-not-step bv (_ , Step x\u2081 x\u2082 x\u2083)\n boxedval-not-step (BVHoleCast () bv) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n boxedval-not-step (BVHoleCast x bv) (d' , Step FHOuter (ITCastSucceed x\u2081 ()) FHOuter)\n boxedval-not-step (BVHoleCast GHole bv) (_ , Step FHOuter (ITGround x\u2081 x\u2082) FHOuter) = x\u2082 refl\n boxedval-not-step (BVHoleCast x bv) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = boxedval-not-step bv (_ , Step x\u2081 x\u2082 x\u2083)\n\n -- todo: what class of P is this true for?\n -- lem-something : \u2200{ d \u03b5 d'} \u2192 d == \u03b5 \u27e6 d' \u27e7 \u2192 P d' \u2192 P d\n\n mutual\n -- indeterminates and errors are disjoint\n indet-not-err : \u2200{d} \u2192 d indet \u2192 d casterr \u2192 \u22a5\n indet-not-err IEHole (CECong FHOuter err) = indet-not-err IEHole err\n indet-not-err (INEHole x) (CECong FHOuter err) = final-not-err x (ce-nehole err)\n indet-not-err (INEHole x) (CECong (FHNEHole x\u2081) err) = final-not-err x (CECong x\u2081 err)\n indet-not-err (IAp x indet x\u2081) (CECong FHOuter err)\n with ce-ap err\n ... | Inl d1err = indet-not-err indet d1err\n ... | Inr d2err = final-not-err x\u2081 d2err\n indet-not-err (IAp x indet x\u2081) (CECong (FHAp1 x\u2082) err) = indet-not-err indet (CECong x\u2082 err)\n indet-not-err (IAp x indet x\u2081) (CECong (FHAp2 x\u2082 x\u2083) err) = final-not-err x\u2081 (CECong x\u2083 err)\n indet-not-err (ICastArr x indet) (CECong FHOuter err) = indet-not-err indet (ce-castarr err)\n indet-not-err (ICastArr x indet) (CECong (FHCast x\u2081) err) = indet-not-err indet (CECong x\u2081 err)\n indet-not-err (ICastGroundHole x indet) (CECastFail x\u2081 x\u2082 () x\u2084)\n indet-not-err (ICastGroundHole x indet) (CECong FHOuter err) = indet-not-err indet (ce-castth err)\n indet-not-err (ICastGroundHole x indet) (CECong (FHCast x\u2081) err) = indet-not-err indet (CECong x\u2081 err)\n indet-not-err (ICastHoleGround x indet x\u2081) (CECastFail x\u2082 x\u2083 x\u2084 x\u2085) = x _ _ refl\n indet-not-err (ICastHoleGround x indet x\u2081) (CECong FHOuter err) = indet-not-err indet (ce-castht err x)\n indet-not-err (ICastHoleGround x indet x\u2081) (CECong (FHCast x\u2082) err) = indet-not-err indet (CECong x\u2082 err)\n\n -- final expressions are not errors\n final-not-err : \u2200{d} \u2192 d final \u2192 d casterr \u2192 \u22a5\n final-not-err (FBoxed x) err = boxedval-not-err x err\n final-not-err (FIndet x) err = indet-not-err x err\n\n mutual\n -- indeterminates and expressions that step are disjoint\n indet-not-step : \u2200{d} \u2192 d indet \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n indet-not-step IEHole (d' , Step FHOuter () FHOuter)\n indet-not-step (INEHole x) (d' , Step FHOuter () FHOuter)\n indet-not-step (INEHole x) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = final-sub-not-trans x x\u2081 x\u2082\n indet-not-step (IAp x\u2081 () x\u2082) (_ , Step FHOuter (ITLam x\u2083) FHOuter)\n indet-not-step (IAp x (ICastArr x\u2081 ind) x\u2082) (_ , Step FHOuter (ITApCast x\u2083 x\u2084) FHOuter) = x _ _ _ _ _ refl\n indet-not-step (IAp x ind _) (_ , Step (FHAp1 x\u2082) x\u2083 (FHAp1 x\u2084)) = indet-not-step ind (_ , Step x\u2082 x\u2083 x\u2084)\n indet-not-step (IAp x ind f) (_ , Step (FHAp2 x\u2082 x\u2083) x\u2084 (FHAp2 x\u2085 x\u2086)) = final-not-step f (_ , Step x\u2083 x\u2084 x\u2086)\n indet-not-step (ICastArr x ind) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n indet-not-step (ICastArr x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n indet-not-step (ICastGroundHole () ind) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n indet-not-step (ICastGroundHole x ind) (d' , Step FHOuter (ITCastSucceed x\u2081 ()) FHOuter)\n indet-not-step (ICastGroundHole GHole ind) (_ , Step FHOuter (ITGround _ y) FHOuter) = y refl\n indet-not-step (ICastGroundHole x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n indet-not-step (ICastHoleGround x ind ()) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n indet-not-step (ICastHoleGround x ind g) (d' , Step FHOuter (ITCastSucceed x\u2081 x\u2082) FHOuter) = x _ _ refl\n indet-not-step (ICastHoleGround x ind GHole) (_ , Step FHOuter (ITExpand x\u2081 x\u2082) FHOuter) = x\u2082 refl\n indet-not-step (ICastHoleGround x ind g) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n\n -- final expressions don't step\n final-not-step : \u2200{d} \u2192 d final \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 \u22a5\n final-not-step (FBoxed x) stp = boxedval-not-step x stp\n final-not-step (FIndet x) stp = indet-not-step x stp\n\n -- errors and expressions that step are disjoint\n err-not-step : \u2200{d} \u2192 d casterr \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n -- cast fail caserr-not-step\n err-not-step (CECastFail x x\u2081 () x\u2083) (_ , Step FHOuter (ITCastID x\u2084) FHOuter)\n err-not-step (CECastFail x x\u2081 x\u2082 x\u2083) (_ , Step FHOuter (ITCastSucceed x\u2084 x\u2085) FHOuter) = x\u2083 refl\n err-not-step (CECastFail x x\u2081 GHole x\u2083) (_ , Step FHOuter (ITExpand x\u2084 x\u2085) FHOuter) = x\u2085 refl\n err-not-step (CECastFail x x\u2081 x\u2082 x\u2083) (_ , Step (FHCast x\u2084) x\u2085 (FHCast x\u2086)) = {!!}\n\n -- congruence caserr-not-step\n err-not-step (CECong FHOuter ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = err-not-step ce (\u03c01 , Step FHOuter x\u2082 FHOuter)\n err-not-step (CECong (FHAp1 FHOuter) (CECong FHOuter ce)) (_ , Step FHOuter (ITLam x\u2082) FHOuter) = boxedval-not-err (BVVal VLam) ce\n err-not-step (CECong (FHAp1 x) ce) (_ , Step FHOuter (ITApCast x\u2081 x\u2082) FHOuter) = {!!} -- fe x\u2081 (CECong {!ce-out-cast ce!} ce)\n err-not-step (CECong (FHAp2 x x\u2081) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!ce-out-cast ce x\u2081!}\n err-not-step (CECong (FHNEHole x) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!!}\n err-not-step (CECong (FHCast x) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!!}\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHAp1 x\u2081) x\u2082 (FHAp1 x\u2083))\n with ce-ap ce\n ... | Inl d1err = err-not-step d1err (_ , Step x\u2081 x\u2082 x\u2083)\n ... | Inr d2err = {!Step x\u2081 x\u2082 x\u2083!} -- this is a counter example: d2 is a casterror but d1 isn't yet a value so the whole thing steps\n err-not-step (CECong (FHAp1 x) ce) (_ , Step (FHAp1 x\u2081) x\u2082 (FHAp1 x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n err-not-step (CECong (FHAp2 x x\u2081) ce) (_ , Step (FHAp1 x\u2082) x\u2083 (FHAp1 x\u2084)) = final-not-step x (_ , Step x\u2082 x\u2083 x\u2084)\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHAp2 x\u2081 x\u2082) x\u2083 (FHAp2 x\u2084 x\u2085))\n with ce-ap ce\n ... | Inl d1err = final-not-err x\u2084 d1err\n ... | Inr d2err = err-not-step d2err (_ , Step x\u2082 x\u2083 x\u2085)\n err-not-step (CECong (FHAp1 x) ce) (_ , Step (FHAp2 x\u2081 x\u2082) x\u2083 (FHAp2 x\u2084 x\u2085)) = {!!}\n err-not-step (CECong (FHAp2 x x\u2081) ce) (_ , Step (FHAp2 x\u2082 x\u2083) x\u2084 (FHAp2 x\u2085 x\u2086)) = {!!}\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = err-not-step (ce-nehole ce) (_ , Step x\u2081 x\u2082 x\u2083)\n err-not-step (CECong (FHNEHole x) ce) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = {!!} -- err-not-step {!!} (_ , Step x\u2081 x\u2082 x\u2083) -- this might not work\n err-not-step (CECong (FHCast x) ce) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"05536450804504640dcab4d2a372088bbc7ef7b3","subject":"Revert \"Added a non-required totality hypothesis.\"","message":"Revert \"Added a non-required totality hypothesis.\"\n\nThis reverts commit 57708b770c00d1ba6276cda0f02752e147f2f533.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Program\/McCarthy91\/PropertiesATP.agda","new_file":"src\/fot\/FOTC\/Program\/McCarthy91\/PropertiesATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Main properties of the McCarthy 91 function\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- The main properties proved of the McCarthy 91 function (called\n-- f\u2089\u2081) are\n\n-- 1. The function always terminates.\n-- 2. For all n, n < f\u2089\u2081 n + 11.\n-- 3. For all n > 100, then f\u2089\u2081 n = n - 10.\n-- 4. For all n <= 100, then f\u2089\u2081 n = 91.\n\n-- N.B This module does not contain combined proofs, but it imports\n-- modules which contain combined proofs.\n\nmodule FOTC.Program.McCarthy91.PropertiesATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.EliminationPropertiesATP\n using ( x>y\u2192x\u2264y\u2192\u22a5 )\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\n using ( x>y\u2228x\u226fy\n ; x\u226fy\u2192x\u2264y\n ; x\u226fSy\u2192x\u226fy\u2228x\u2261Sy\n ; x+k 100, then f\u2089\u2081 n = n - 10.\npostulate f\u2089\u2081-x>100 : \u2200 n \u2192 n > 100' \u2192 f\u2089\u2081 n \u2261 n \u2238 10'\n{-# ATP prove f\u2089\u2081-x>100 #-}\n\n-- For all n <= 100, then f\u2089\u2081 n = 91.\nf\u2089\u2081-x\u226f100 : \u2200 {n} \u2192 N n \u2192 n \u226f 100' \u2192 f\u2089\u2081 n \u2261 91'\nf\u2089\u2081-x\u226f100 = \u25c1-wfind A h\n where\n A : D \u2192 Set\n A d = d \u226f 100' \u2192 f\u2089\u2081 d \u2261 91'\n\n h : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 k \u25c1 m \u2192 A k) \u2192 A m\n h {m} Nm f with x>y\u2228x\u226fy Nm 100-N\n ... | inj\u2081 m>100 = \u03bb m\u226f100 \u2192\n \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nm 100-N m>100 (x\u226fy\u2192x\u2264y Nm 100-N m\u226f100))\n ... | inj\u2082 m\u226f100 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 99-N m\u226f100\n ... | inj\u2082 m\u2261100 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-100 m\u2261100\n ... | inj\u2081 m\u226f99 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 98-N m\u226f99\n ... | inj\u2082 m\u226199 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-99 m\u226199\n ... | inj\u2081 m\u226f98 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 97-N m\u226f98\n ... | inj\u2082 m\u226198 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-98 m\u226198\n ... | inj\u2081 m\u226f97 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 96-N m\u226f97\n ... | inj\u2082 m\u226197 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-97 m\u226197\n ... | inj\u2081 m\u226f96 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 95-N m\u226f96\n ... | inj\u2082 m\u226196 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-96 m\u226196\n ... | inj\u2081 m\u226f95 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 94-N m\u226f95\n ... | inj\u2082 m\u226195 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-95 m\u226195\n ... | inj\u2081 m\u226f94 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 93-N m\u226f94\n ... | inj\u2082 m\u226194 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-94 m\u226194\n ... | inj\u2081 m\u226f93 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 92-N m\u226f93\n ... | inj\u2082 m\u226193 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-93 m\u226193\n ... | inj\u2081 m\u226f92 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 91-N m\u226f92\n ... | inj\u2082 m\u226192 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-92 m\u226192\n ... | inj\u2081 m\u226f91 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 90-N m\u226f91\n ... | inj\u2082 m\u226191 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-91 m\u226191\n ... | inj\u2081 m\u226f90 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 89-N m\u226f90\n ... | inj\u2082 m\u226190 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-90 m\u226190\n ... | inj\u2081 m\u226f89 = \u03bb _ \u2192 f\u2089\u2081-m\u226f89\n where\n m\u226489 : m \u2264 89'\n m\u226489 = x\u226fy\u2192x\u2264y Nm 89-N m\u226f89\n\n f\u2089\u2081-m+11 : m + 11' \u226f 100' \u2192 f\u2089\u2081 (m + 11') \u2261 91'\n f\u2089\u2081-m+11 = f (x+11-N Nm) (<\u2192\u25c1 (x+11-N Nm) Nm m\u226f100 (xy\u2228x\u226fy Nn 100-N\n... | inj\u2081 n>100 = subst N (sym (f\u2089\u2081-x>100 n n>100)) (\u2238-N Nn 10-N)\n... | inj\u2082 n\u226f100 = subst N (sym (f\u2089\u2081-x\u226f100 Nn n\u226f100)) 91-N\n\n-- For all n, n < f\u2089\u2081 n + 11.\nf\u2089\u2081-ineq : \u2200 {n} \u2192 N n \u2192 n < f\u2089\u2081 n + 11'\nf\u2089\u2081-ineq = \u25c1-wfind A h\n where\n A : D \u2192 Set\n A d = d < f\u2089\u2081 d + 11'\n\n h : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 k \u25c1 m \u2192 A k) \u2192 A m\n h {m} Nm f with x>y\u2228x\u226fy Nm 100-N\n ... | inj\u2081 m>100 = x>100\u2192x100\n ... | inj\u2082 m\u226f100 =\n let f\u2089\u2081-m+11-N : N (f\u2089\u2081 (m + 11'))\n f\u2089\u2081-m+11-N = f\u2089\u2081-N (+-N Nm 11-N)\n\n h\u2081 : A (m + 11')\n h\u2081 = f (x+11-N Nm) (<\u2192\u25c1 (x+11-N Nm) Nm m\u226f100 (x 100, then f\u2089\u2081 n = n - 10.\n-- 4. For all n <= 100, then f\u2089\u2081 n = 91.\n\n-- N.B This module does not contain combined proofs, but it imports\n-- modules which contain combined proofs.\n\nmodule FOTC.Program.McCarthy91.PropertiesATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.EliminationPropertiesATP\n using ( x>y\u2192x\u2264y\u2192\u22a5 )\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\n using ( x>y\u2228x\u226fy\n ; x\u226fy\u2192x\u2264y\n ; x\u226fSy\u2192x\u226fy\u2228x\u2261Sy\n ; x+k 100, then f\u2089\u2081 n = n - 10.\n--\n-- N.B. (21 November 2013). The hypothesis N n is not necessary.\npostulate f\u2089\u2081-x>100 : \u2200 n \u2192 N n \u2192 n > 100' \u2192 f\u2089\u2081 n \u2261 n \u2238 10'\n{-# ATP prove f\u2089\u2081-x>100 #-}\n\n-- For all n <= 100, then f\u2089\u2081 n = 91.\nf\u2089\u2081-x\u226f100 : \u2200 {n} \u2192 N n \u2192 n \u226f 100' \u2192 f\u2089\u2081 n \u2261 91'\nf\u2089\u2081-x\u226f100 = \u25c1-wfind A h\n where\n A : D \u2192 Set\n A d = d \u226f 100' \u2192 f\u2089\u2081 d \u2261 91'\n\n h : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 k \u25c1 m \u2192 A k) \u2192 A m\n h {m} Nm f with x>y\u2228x\u226fy Nm 100-N\n ... | inj\u2081 m>100 = \u03bb m\u226f100 \u2192\n \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nm 100-N m>100 (x\u226fy\u2192x\u2264y Nm 100-N m\u226f100))\n ... | inj\u2082 m\u226f100 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 99-N m\u226f100\n ... | inj\u2082 m\u2261100 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-100 m\u2261100\n ... | inj\u2081 m\u226f99 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 98-N m\u226f99\n ... | inj\u2082 m\u226199 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-99 m\u226199\n ... | inj\u2081 m\u226f98 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 97-N m\u226f98\n ... | inj\u2082 m\u226198 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-98 m\u226198\n ... | inj\u2081 m\u226f97 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 96-N m\u226f97\n ... | inj\u2082 m\u226197 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-97 m\u226197\n ... | inj\u2081 m\u226f96 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 95-N m\u226f96\n ... | inj\u2082 m\u226196 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-96 m\u226196\n ... | inj\u2081 m\u226f95 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 94-N m\u226f95\n ... | inj\u2082 m\u226195 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-95 m\u226195\n ... | inj\u2081 m\u226f94 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 93-N m\u226f94\n ... | inj\u2082 m\u226194 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-94 m\u226194\n ... | inj\u2081 m\u226f93 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 92-N m\u226f93\n ... | inj\u2082 m\u226193 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-93 m\u226193\n ... | inj\u2081 m\u226f92 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 91-N m\u226f92\n ... | inj\u2082 m\u226192 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-92 m\u226192\n ... | inj\u2081 m\u226f91 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 90-N m\u226f91\n ... | inj\u2082 m\u226191 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-91 m\u226191\n ... | inj\u2081 m\u226f90 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 89-N m\u226f90\n ... | inj\u2082 m\u226190 = \u03bb _ \u2192 f\u2089\u2081-x\u2261y f\u2089\u2081-90 m\u226190\n ... | inj\u2081 m\u226f89 = \u03bb _ \u2192 f\u2089\u2081-m\u226f89\n where\n m\u226489 : m \u2264 89'\n m\u226489 = x\u226fy\u2192x\u2264y Nm 89-N m\u226f89\n\n f\u2089\u2081-m+11 : m + 11' \u226f 100' \u2192 f\u2089\u2081 (m + 11') \u2261 91'\n f\u2089\u2081-m+11 = f (x+11-N Nm) (<\u2192\u25c1 (x+11-N Nm) Nm m\u226f100 (xy\u2228x\u226fy Nn 100-N\n... | inj\u2081 n>100 = subst N (sym (f\u2089\u2081-x>100 n Nn n>100)) (\u2238-N Nn 10-N)\n... | inj\u2082 n\u226f100 = subst N (sym (f\u2089\u2081-x\u226f100 Nn n\u226f100)) 91-N\n\n-- For all n, n < f\u2089\u2081 n + 11.\nf\u2089\u2081-ineq : \u2200 {n} \u2192 N n \u2192 n < f\u2089\u2081 n + 11'\nf\u2089\u2081-ineq = \u25c1-wfind A h\n where\n A : D \u2192 Set\n A d = d < f\u2089\u2081 d + 11'\n\n h : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 k \u25c1 m \u2192 A k) \u2192 A m\n h {m} Nm f with x>y\u2228x\u226fy Nm 100-N\n ... | inj\u2081 m>100 = x>100\u2192x100\n ... | inj\u2082 m\u226f100 =\n let f\u2089\u2081-m+11-N : N (f\u2089\u2081 (m + 11'))\n f\u2089\u2081-m+11-N = f\u2089\u2081-N (+-N Nm 11-N)\n\n h\u2081 : A (m + 11')\n h\u2081 = f (x+11-N Nm) (<\u2192\u25c1 (x+11-N Nm) Nm m\u226f100 (x Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : (I : Set)(i : I) -> IDescl I\nvarl I i = con (lvar , i)\n\nconstl : (I : Set)(X : Set) -> IDescl I\nconstl I X = con (lconst , X)\n\nprodl : (I : Set)(D D' : IDescl I) -> IDescl I\nprodl I D D' = con (lprod , (D , D'))\n\npil : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\npil I S T = con (lpi , ( S , T))\n\nsigmal : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal I S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : (I : Set) \n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases I ( lvar , i ) hs = var i\ncases I ( lconst , X ) hs = const X\ncases I ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases I ( lpi , ( S , T ) ) hs = pi S hs\ncases I ( lsigma , ( S , T ) ) hs = sigma S hs\n\niso1 : (I : Set) -> IDescl I -> IDesc I\niso1 I d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases I) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\niso2 : (I : Set) -> IDesc I -> IDescl I\niso2 I (var i) = varl I i\niso2 I (const X) = constl I X\niso2 I (prod D D') = prodl I (iso2 I D) (iso2 I D')\niso2 I (pi S T) = pil I S (\\s -> iso2 I (T s))\niso2 I (sigma S T) = sigmal I S (\\s -> iso2 I (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-iso1-iso2 : (I : Set) -> (D : IDesc I) -> iso1 I (iso2 I D) == D\nproof-iso1-iso2 I (var i) = refl\nproof-iso1-iso2 I (const x) = refl\nproof-iso1-iso2 I (prod D D') with proof-iso1-iso2 I D | proof-iso1-iso2 I D'\n... | p | q = cong2 prod p q\nproof-iso1-iso2 I (pi S T) = cong (pi S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\nproof-iso1-iso2 I (sigma S T) = cong (sigma S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\n\n\n-- From embedding to embedding\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = iso2 I (iso1 I D) == D\n\nproof-iso2-iso1 : (I : Set) -> (D : IDescl I) -> iso2 I (iso1 I D) == D\nproof-iso2-iso1 I D = induction (\u03bb _ \u2192 descD I)\n (P I)\n (proof-iso2-iso1-casesW I) \n Void\n D\n where proof-iso2-iso1-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-iso2-iso1-cases I (lvar , i) hs = refl\n proof-iso2-iso1-cases I (lconst , x) hs = refl\n proof-iso2-iso1-cases I (lprod , ( D , D' )) ( p , q ) = cong2 (prodl I) p q \n proof-iso2-iso1-cases I (lpi , ( S , T )) hs = cong (pil I S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-cases I (lsigma , ( S , T )) hs = cong (sigmal I S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-iso2-iso1-casesW I Void = proof-iso2-iso1-cases I\n \n\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : (I : Set)(i : I) -> IDescl I\nvarl I i = con (lvar , i)\n\nconstl : (I : Set)(X : Set) -> IDescl I\nconstl I X = con (lconst , X)\n\nprodl : (I : Set)(D D' : IDescl I) -> IDescl I\nprodl I D D' = con (lprod , (D , D'))\n\npil : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\npil I S T = con (lpi , ( S , T))\n\nsigmal : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal I S T = con (lsigma , ( S , T))\n\ncases : (I : Set) \n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases I ( lvar , i ) hs = var i\ncases I ( lconst , X ) hs = const X\ncases I ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases I ( lpi , ( S , T ) ) hs = pi S hs\ncases I ( lsigma , ( S , T ) ) hs = sigma S hs\n\niso1 : (I : Set) -> IDescl I -> IDesc I\niso1 I d = induction Unit (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases I) Void d\n\niso2 : (I : Set) -> IDesc I -> IDescl I\niso2 I (var i) = varl I i\niso2 I (const X) = constl I X\niso2 I (prod D D') = prodl I (iso2 I D) (iso2 I D')\niso2 I (pi S T) = pil I S (\\s -> iso2 I (T s))\niso2 I (sigma S T) = sigmal I S (\\s -> iso2 I (T s))\n\n\n\nproof-iso1-iso2 : (I : Set) -> (D : IDesc I) -> iso1 I (iso2 I D) == D\nproof-iso1-iso2 I (var i) = refl\nproof-iso1-iso2 I (const x) = refl\nproof-iso1-iso2 I (prod D D') with proof-iso1-iso2 I D | proof-iso1-iso2 I D'\n... | p | q = cong2 prod p q\nproof-iso1-iso2 I (pi S T) = cong (pi S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\nproof-iso1-iso2 I (sigma S T) = cong (sigma S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\n\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = iso2 I (iso1 I D) == D\n\n\nproof-iso2-iso1 : (I : Set) -> (D : IDescl I) -> iso2 I (iso1 I D) == D\nproof-iso2-iso1 I D = induction Unit \n (\u03bb _ \u2192 descD I)\n (P I)\n (proof-iso2-iso1-casesW I) \n Void\n D\n where proof-iso2-iso1-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-iso2-iso1-cases I (lvar , i) hs = refl\n proof-iso2-iso1-cases I (lconst , x) hs = refl\n proof-iso2-iso1-cases I (lprod , ( D , D' )) ( p , q ) = cong2 (prodl I) p q \n proof-iso2-iso1-cases I (lpi , ( S , T )) hs = cong (pil I S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-cases I (lsigma , ( S , T )) hs = cong (sigmal I S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-iso2-iso1-casesW I Void = proof-iso2-iso1-cases I\n \n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d7f9323c845f517eaad0556192d53f826bb6063c","subject":"Desc model: cases implicit.","message":"Desc model: cases implicit.","repos":"brixen\/Epigram,kwangkim\/pigment,kwangkim\/pigment,kwangkim\/pigment","old_file":"models\/IDesc_type_type.agda","new_file":"models\/IDesc_type_type.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\niso1 : (I : Set) -> IDescl I -> IDesc I\niso1 I d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\niso2 : (I : Set) -> IDesc I -> IDescl I\niso2 I (var i) = varl i\niso2 I (const X) = constl X\niso2 I (prod D D') = prodl (iso2 I D) (iso2 I D')\niso2 I (pi S T) = pil S (\\s -> iso2 I (T s))\niso2 I (sigma S T) = sigmal S (\\s -> iso2 I (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-iso1-iso2 : (I : Set) -> (D : IDesc I) -> iso1 I (iso2 I D) == D\nproof-iso1-iso2 I (var i) = refl\nproof-iso1-iso2 I (const x) = refl\nproof-iso1-iso2 I (prod D D') with proof-iso1-iso2 I D | proof-iso1-iso2 I D'\n... | p | q = cong2 prod p q\nproof-iso1-iso2 I (pi S T) = cong (pi S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\nproof-iso1-iso2 I (sigma S T) = cong (sigma S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\n\n\n-- From embedding to embedding\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = iso2 I (iso1 I D) == D\n\nproof-iso2-iso1 : (I : Set) -> (D : IDescl I) -> iso2 I (iso1 I D) == D\nproof-iso2-iso1 I D = induction (\u03bb _ \u2192 descD I)\n (P I)\n (proof-iso2-iso1-casesW I) \n Void\n D\n where proof-iso2-iso1-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-iso2-iso1-cases I (lvar , i) hs = refl\n proof-iso2-iso1-cases I (lconst , x) hs = refl\n proof-iso2-iso1-cases I (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-iso2-iso1-cases I (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-cases I (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-iso2-iso1-casesW I Void = proof-iso2-iso1-cases I\n \n\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : (I : Set) \n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases I ( lvar , i ) hs = var i\ncases I ( lconst , X ) hs = const X\ncases I ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases I ( lpi , ( S , T ) ) hs = pi S hs\ncases I ( lsigma , ( S , T ) ) hs = sigma S hs\n\niso1 : (I : Set) -> IDescl I -> IDesc I\niso1 I d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases I) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\niso2 : (I : Set) -> IDesc I -> IDescl I\niso2 I (var i) = varl i\niso2 I (const X) = constl X\niso2 I (prod D D') = prodl (iso2 I D) (iso2 I D')\niso2 I (pi S T) = pil S (\\s -> iso2 I (T s))\niso2 I (sigma S T) = sigmal S (\\s -> iso2 I (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-iso1-iso2 : (I : Set) -> (D : IDesc I) -> iso1 I (iso2 I D) == D\nproof-iso1-iso2 I (var i) = refl\nproof-iso1-iso2 I (const x) = refl\nproof-iso1-iso2 I (prod D D') with proof-iso1-iso2 I D | proof-iso1-iso2 I D'\n... | p | q = cong2 prod p q\nproof-iso1-iso2 I (pi S T) = cong (pi S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\nproof-iso1-iso2 I (sigma S T) = cong (sigma S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\n\n\n-- From embedding to embedding\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = iso2 I (iso1 I D) == D\n\nproof-iso2-iso1 : (I : Set) -> (D : IDescl I) -> iso2 I (iso1 I D) == D\nproof-iso2-iso1 I D = induction (\u03bb _ \u2192 descD I)\n (P I)\n (proof-iso2-iso1-casesW I) \n Void\n D\n where proof-iso2-iso1-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-iso2-iso1-cases I (lvar , i) hs = refl\n proof-iso2-iso1-cases I (lconst , x) hs = refl\n proof-iso2-iso1-cases I (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-iso2-iso1-cases I (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-cases I (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-iso2-iso1-casesW I Void = proof-iso2-iso1-cases I\n \n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"94c667f3d542af3d0d5f3f603fe4d1aeccaf6e95","subject":"Add change structure for changes","message":"Add change structure for changes\n","repos":"inc-lc\/ilc-agda","old_file":"New\/Changes.agda","new_file":"New\/Changes.agda","new_contents":"module New.Changes where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\nopen import Data.Unit\nopen import Data.Product\nopen import Data.Sum\n\nrecord IsChAlg {\u2113 : Level} (A : Set \u2113) (Ch : Set \u2113) : Set (suc \u2113) where\n field\n _\u2295_ : A \u2192 Ch \u2192 A\n _\u229d_ : A \u2192 A \u2192 Ch\n valid : A \u2192 Ch \u2192 Set \u2113\n \u229d-valid : \u2200 (a b : A) \u2192 valid a (b \u229d a)\n \u2295-\u229d : (b a : A) \u2192 a \u2295 (b \u229d a) \u2261 b\n infixl 6 _\u2295_ _\u229d_\n\n \u0394 : A \u2192 Set \u2113\n \u0394 a = \u03a3[ da \u2208 Ch ] (valid a da)\n\n update-diff = \u2295-\u229d\n\n nil : A \u2192 Ch\n nil a = a \u229d a\n nil-valid : (a : A) \u2192 valid a (nil a)\n nil-valid a = \u229d-valid a a\n update-nil : (a : A) \u2192 a \u2295 nil a \u2261 a\n update-nil a = update-diff a a\n\nrecord ChAlg {\u2113 : Level} (A : Set \u2113) : Set (suc \u2113) where\n field\n Ch : Set \u2113\n isChAlg : IsChAlg A Ch\n open IsChAlg isChAlg public\n\nopen ChAlg {{...}} public hiding (Ch)\nCh : \u2200 {\u2113} (A : Set \u2113) \u2192 {{CA : ChAlg A}} \u2192 Set \u2113\nCh A {{CA}} = ChAlg.Ch CA\n\n-- Huge hack, but it does gives the output you want to see in all cases I've seen.\n\n{-# DISPLAY IsChAlg.valid x = valid #-}\n{-# DISPLAY ChAlg.valid x = valid #-}\n{-# DISPLAY IsChAlg._\u2295_ x = _\u2295_ #-}\n{-# DISPLAY ChAlg._\u2295_ x = _\u2295_ #-}\n{-# DISPLAY IsChAlg.nil x = nil #-}\n{-# DISPLAY ChAlg.nil x = nil #-}\n{-# DISPLAY IsChAlg._\u229d_ x = _\u229d_ #-}\n{-# DISPLAY ChAlg._\u229d_ x = _\u229d_ #-}\n\nmodule _ {\u2113\u2081} {\u2113\u2082}\n {A : Set \u2113\u2081} {B : Set \u2113\u2082} {{CA : ChAlg A}} {{CB : ChAlg B}} where\n private\n fCh = A \u2192 Ch A \u2192 Ch B\n _f\u2295_ : (A \u2192 B) \u2192 fCh \u2192 A \u2192 B\n _f\u2295_ = \u03bb f df a \u2192 f a \u2295 df a (nil a)\n _f\u229d_ : (g f : A \u2192 B) \u2192 fCh\n _f\u229d_ = \u03bb g f a da \u2192 g (a \u2295 da) \u229d f a\n open \u2261-Reasoning\n open import Postulate.Extensionality\n\n IsDerivative : \u2200 (f : A \u2192 B) \u2192 (df : fCh) \u2192 Set (\u2113\u2081 \u2294 \u2113\u2082)\n IsDerivative f df = \u2200 a da (v : valid a da) \u2192 f (a \u2295 da) \u2261 f a \u2295 df a da\n\n instance\n funCA : ChAlg (A \u2192 B)\n private\n funUpdateDiff : \u2200 g f a \u2192 (f f\u2295 (g f\u229d f)) a \u2261 g a\n funUpdateDiff g f a rewrite update-nil a = update-diff (g a) (f a)\n funCA = record\n { Ch = A \u2192 Ch A \u2192 Ch B\n ; isChAlg = record\n { _\u2295_ = _f\u2295_\n ; _\u229d_ = _f\u229d_\n ; valid = \u03bb f df \u2192 \u2200 a da (v : valid a da) \u2192\n valid (f a) (df a da) \u00d7\n (f f\u2295 df) (a \u2295 da) \u2261 f a \u2295 df a da\n ; \u229d-valid = \u03bb f g a da (v : valid a da) \u2192\n \u229d-valid (f a) (g (a \u2295 da))\n , ( begin\n f (a \u2295 da) \u2295 (g (a \u2295 da \u2295 nil (a \u2295 da)) \u229d f (a \u2295 da))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f (a \u2295 da) \u2295 (g \u25a1 \u229d f (a \u2295 da)))\n (update-nil (a \u2295 da)) \u27e9\n f (a \u2295 da) \u2295 (g (a \u2295 da) \u229d f (a \u2295 da))\n \u2261\u27e8 update-diff (g (a \u2295 da)) (f (a \u2295 da)) \u27e9\n g (a \u2295 da)\n \u2261\u27e8 sym (update-diff (g (a \u2295 da)) (f a)) \u27e9\n f a \u2295 (g (a \u2295 da) \u229d f a)\n \u220e)\n ; \u2295-\u229d = \u03bb g f \u2192 ext (funUpdateDiff g f)\n } }\n\n nil-is-derivative : \u2200 (f : A \u2192 B) \u2192 IsDerivative f (nil f)\n nil-is-derivative f a da v =\n begin\n f (a \u2295 da)\n \u2261\u27e8 sym (cong (\u03bb \u25a1 \u2192 \u25a1 (_\u2295_ a da)) (update-nil f)) \u27e9\n (f \u2295 nil f) (a \u2295 da)\n \u2261\u27e8 proj\u2082 (nil-valid f a da v) \u27e9\n f a \u2295 (nil f a da)\n \u220e\n\n private\n _p\u2295_ : A \u00d7 B \u2192 Ch A \u00d7 Ch B \u2192 A \u00d7 B\n _p\u2295_ (a , b) (da , db) = a \u2295 da , b \u2295 db\n _p\u229d_ : A \u00d7 B \u2192 A \u00d7 B \u2192 Ch A \u00d7 Ch B\n _p\u229d_ (a2 , b2) (a1 , b1) = a2 \u229d a1 , b2 \u229d b1\n pvalid : A \u00d7 B \u2192 Ch A \u00d7 Ch B \u2192 Set (\u2113\u2082 \u2294 \u2113\u2081)\n pvalid (a , b) (da , db) = valid a da \u00d7 valid b db\n p\u229d-valid : (p1 p2 : A \u00d7 B) \u2192 pvalid p1 (p2 p\u229d p1)\n p\u229d-valid (a1 , b1) (a2 , b2) = \u229d-valid a1 a2 , \u229d-valid b1 b2\n p\u2295-\u229d : (p2 p1 : A \u00d7 B) \u2192 p1 p\u2295 (p2 p\u229d p1) \u2261 p2\n p\u2295-\u229d (a2 , b2) (a1 , b1) = cong\u2082 _,_ (\u2295-\u229d a2 a1) (\u2295-\u229d b2 b1)\n instance\n pairCA : ChAlg (A \u00d7 B)\n pairCA = record\n { Ch = Ch A \u00d7 Ch B\n ; isChAlg = record\n { _\u2295_ = _p\u2295_\n ; _\u229d_ = _p\u229d_\n ; valid = pvalid\n ; \u229d-valid = p\u229d-valid\n ; \u2295-\u229d = p\u2295-\u229d\n } }\n\n private\n SumChange = (Ch A \u228e Ch B) \u228e (A \u228e B)\n\n data SumChange2 : Set (\u2113\u2081 \u2294 \u2113\u2082) where\n ch\u2081 : (da : Ch A) \u2192 SumChange2\n ch\u2082 : (db : Ch B) \u2192 SumChange2\n rp : (s : A \u228e B) \u2192 SumChange2\n\n convert : SumChange \u2192 SumChange2\n convert (inj\u2081 (inj\u2081 da)) = ch\u2081 da\n convert (inj\u2081 (inj\u2082 db)) = ch\u2082 db\n convert (inj\u2082 s) = rp s\n convert\u2081 : SumChange2 \u2192 SumChange\n convert\u2081 (ch\u2081 da) = inj\u2081 (inj\u2081 da)\n convert\u2081 (ch\u2082 db) = inj\u2081 (inj\u2082 db)\n convert\u2081 (rp s) = inj\u2082 s\n\n data SValid : A \u228e B \u2192 SumChange \u2192 Set (\u2113\u2081 \u2294 \u2113\u2082) where\n sv\u2081 : \u2200 (a : A) (da : Ch A) (ada : valid a da) \u2192 SValid (inj\u2081 a) (convert\u2081 (ch\u2081 da))\n sv\u2082 : \u2200 (b : B) (db : Ch B) (bdb : valid b db) \u2192 SValid (inj\u2082 b) (convert\u2081 (ch\u2082 db))\n svrp : \u2200 (s\u2081 s\u2082 : A \u228e B) \u2192 SValid s\u2081 (convert\u2081 (rp s\u2082))\n\n inv1 : \u2200 ds \u2192 convert\u2081 (convert ds) \u2261 ds\n inv1 (inj\u2081 (inj\u2081 da)) = refl\n inv1 (inj\u2081 (inj\u2082 db)) = refl\n inv1 (inj\u2082 s) = refl\n inv2 : \u2200 ds \u2192 convert (convert\u2081 ds) \u2261 ds\n inv2 (ch\u2081 da) = refl\n inv2 (ch\u2082 db) = refl\n inv2 (rp s) = refl\n\n private\n s\u22952 : A \u228e B \u2192 SumChange2 \u2192 A \u228e B\n s\u22952 (inj\u2081 a) (ch\u2081 da) = inj\u2081 (a \u2295 da)\n s\u22952 (inj\u2082 b) (ch\u2082 db) = inj\u2082 (b \u2295 db)\n s\u22952 (inj\u2082 b) (ch\u2081 da) = inj\u2082 b -- invalid\n s\u22952 (inj\u2081 a) (ch\u2082 db) = inj\u2081 a -- invalid\n s\u22952 s (rp s\u2081) = s\u2081\n\n s\u2295 : A \u228e B \u2192 SumChange \u2192 A \u228e B\n s\u2295 s ds = s\u22952 s (convert ds)\n\n s\u229d2 : A \u228e B \u2192 A \u228e B \u2192 SumChange2\n s\u229d2 (inj\u2081 x2) (inj\u2081 x1) = ch\u2081 (x2 \u229d x1)\n s\u229d2 (inj\u2082 y2) (inj\u2082 y1) = ch\u2082 (y2 \u229d y1)\n s\u229d2 s2 s1 = rp s2\n\n s\u229d : A \u228e B \u2192 A \u228e B \u2192 SumChange\n s\u229d s2 s1 = convert\u2081 (s\u229d2 s2 s1)\n\n s\u229d-valid : (a b : A \u228e B) \u2192 SValid a (s\u229d b a)\n s\u229d-valid (inj\u2081 x1) (inj\u2081 x2) = sv\u2081 x1 (x2 \u229d x1) (\u229d-valid x1 x2)\n s\u229d-valid (inj\u2082 y1) (inj\u2082 y2) = sv\u2082 y1 (y2 \u229d y1) (\u229d-valid y1 y2)\n s\u229d-valid s1@(inj\u2081 x) s2@(inj\u2082 y) = svrp s1 s2\n s\u229d-valid s1@(inj\u2082 y) s2@(inj\u2081 x) = svrp s1 s2\n\n s\u2295-\u229d : (b a : A \u228e B) \u2192 s\u2295 a (s\u229d b a) \u2261 b\n s\u2295-\u229d (inj\u2081 x2) (inj\u2081 x1) rewrite \u2295-\u229d x2 x1 = refl\n s\u2295-\u229d (inj\u2081 x2) (inj\u2082 y1) = refl\n s\u2295-\u229d (inj\u2082 y2) (inj\u2081 x1) = refl\n s\u2295-\u229d (inj\u2082 y2) (inj\u2082 y1) rewrite \u2295-\u229d y2 y1 = refl\n\n sumCA = record\n { Ch = SumChange\n ; isChAlg = record\n { _\u2295_ = s\u2295\n ; _\u229d_ = s\u229d\n ; valid = SValid\n ; \u229d-valid = s\u229d-valid\n ; \u2295-\u229d = s\u2295-\u229d\n } }\n\nopen import Data.Integer\nopen import Theorem.Groups-Nehemiah\n\ninstance\n intCA : ChAlg \u2124\nintCA = record\n { Ch = \u2124\n ; isChAlg = record\n { _\u2295_ = _+_\n ; _\u229d_ = _-_\n ; valid = \u03bb a b \u2192 \u22a4\n ; \u229d-valid = \u03bb a b \u2192 tt\n ; \u2295-\u229d = \u03bb b a \u2192 n+[m-n]=m {a} {b} } }\n","old_contents":"module New.Changes where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\nopen import Data.Unit\nopen import Data.Product\n\nrecord IsChAlg {\u2113 : Level} (A : Set \u2113) (Ch : Set \u2113) : Set (suc \u2113) where\n field\n _\u2295_ : A \u2192 Ch \u2192 A\n _\u229d_ : A \u2192 A \u2192 Ch\n valid : A \u2192 Ch \u2192 Set \u2113\n \u229d-valid : \u2200 (a b : A) \u2192 valid a (b \u229d a)\n \u2295-\u229d : (b a : A) \u2192 a \u2295 (b \u229d a) \u2261 b\n infixl 6 _\u2295_ _\u229d_\n\n \u0394 : A \u2192 Set \u2113\n \u0394 a = \u03a3[ da \u2208 Ch ] (valid a da)\n\n update-diff = \u2295-\u229d\n\n nil : A \u2192 Ch\n nil a = a \u229d a\n nil-valid : (a : A) \u2192 valid a (nil a)\n nil-valid a = \u229d-valid a a\n update-nil : (a : A) \u2192 a \u2295 nil a \u2261 a\n update-nil a = update-diff a a\n\nrecord ChAlg {\u2113 : Level} (A : Set \u2113) : Set (suc \u2113) where\n field\n Ch : Set \u2113\n isChAlg : IsChAlg A Ch\n open IsChAlg isChAlg public\n\nopen ChAlg {{...}} public hiding (Ch)\nCh : \u2200 {\u2113} (A : Set \u2113) \u2192 {{CA : ChAlg A}} \u2192 Set \u2113\nCh A {{CA}} = ChAlg.Ch CA\n\n-- Huge hack, but it does gives the output you want to see in all cases I've seen.\n\n{-# DISPLAY IsChAlg.valid x = valid #-}\n{-# DISPLAY ChAlg.valid x = valid #-}\n{-# DISPLAY IsChAlg._\u2295_ x = _\u2295_ #-}\n{-# DISPLAY ChAlg._\u2295_ x = _\u2295_ #-}\n{-# DISPLAY IsChAlg.nil x = nil #-}\n{-# DISPLAY ChAlg.nil x = nil #-}\n{-# DISPLAY IsChAlg._\u229d_ x = _\u229d_ #-}\n{-# DISPLAY ChAlg._\u229d_ x = _\u229d_ #-}\n\nmodule _ {\u2113\u2081} {\u2113\u2082}\n {A : Set \u2113\u2081} {B : Set \u2113\u2082} {{CA : ChAlg A}} {{CB : ChAlg B}} where\n private\n fCh = A \u2192 Ch A \u2192 Ch B\n _f\u2295_ : (A \u2192 B) \u2192 fCh \u2192 A \u2192 B\n _f\u2295_ = \u03bb f df a \u2192 f a \u2295 df a (nil a)\n _f\u229d_ : (g f : A \u2192 B) \u2192 fCh\n _f\u229d_ = \u03bb g f a da \u2192 g (a \u2295 da) \u229d f a\n open \u2261-Reasoning\n open import Postulate.Extensionality\n\n IsDerivative : \u2200 (f : A \u2192 B) \u2192 (df : fCh) \u2192 Set (\u2113\u2081 \u2294 \u2113\u2082)\n IsDerivative f df = \u2200 a da (v : valid a da) \u2192 f (a \u2295 da) \u2261 f a \u2295 df a da\n\n instance\n funCA : ChAlg (A \u2192 B)\n private\n funUpdateDiff : \u2200 g f a \u2192 (f f\u2295 (g f\u229d f)) a \u2261 g a\n funUpdateDiff g f a rewrite update-nil a = update-diff (g a) (f a)\n funCA = record\n { Ch = A \u2192 Ch A \u2192 Ch B\n ; isChAlg = record\n { _\u2295_ = _f\u2295_\n ; _\u229d_ = _f\u229d_\n ; valid = \u03bb f df \u2192 \u2200 a da (v : valid a da) \u2192\n valid (f a) (df a da) \u00d7\n (f f\u2295 df) (a \u2295 da) \u2261 f a \u2295 df a da\n ; \u229d-valid = \u03bb f g a da (v : valid a da) \u2192\n \u229d-valid (f a) (g (a \u2295 da))\n , ( begin\n f (a \u2295 da) \u2295 (g (a \u2295 da \u2295 nil (a \u2295 da)) \u229d f (a \u2295 da))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f (a \u2295 da) \u2295 (g \u25a1 \u229d f (a \u2295 da)))\n (update-nil (a \u2295 da)) \u27e9\n f (a \u2295 da) \u2295 (g (a \u2295 da) \u229d f (a \u2295 da))\n \u2261\u27e8 update-diff (g (a \u2295 da)) (f (a \u2295 da)) \u27e9\n g (a \u2295 da)\n \u2261\u27e8 sym (update-diff (g (a \u2295 da)) (f a)) \u27e9\n f a \u2295 (g (a \u2295 da) \u229d f a)\n \u220e)\n ; \u2295-\u229d = \u03bb g f \u2192 ext (funUpdateDiff g f)\n } }\n\n nil-is-derivative : \u2200 (f : A \u2192 B) \u2192 IsDerivative f (nil f)\n nil-is-derivative f a da v =\n begin\n f (a \u2295 da)\n \u2261\u27e8 sym (cong (\u03bb \u25a1 \u2192 \u25a1 (_\u2295_ a da)) (update-nil f)) \u27e9\n (f \u2295 nil f) (a \u2295 da)\n \u2261\u27e8 proj\u2082 (nil-valid f a da v) \u27e9\n f a \u2295 (nil f a da)\n \u220e\n\n private\n _p\u2295_ : A \u00d7 B \u2192 Ch A \u00d7 Ch B \u2192 A \u00d7 B\n _p\u2295_ (a , b) (da , db) = a \u2295 da , b \u2295 db\n _p\u229d_ : A \u00d7 B \u2192 A \u00d7 B \u2192 Ch A \u00d7 Ch B\n _p\u229d_ (a2 , b2) (a1 , b1) = a2 \u229d a1 , b2 \u229d b1\n pvalid : A \u00d7 B \u2192 Ch A \u00d7 Ch B \u2192 Set (\u2113\u2082 \u2294 \u2113\u2081)\n pvalid (a , b) (da , db) = valid a da \u00d7 valid b db\n p\u229d-valid : (p1 p2 : A \u00d7 B) \u2192 pvalid p1 (p2 p\u229d p1)\n p\u229d-valid (a1 , b1) (a2 , b2) = \u229d-valid a1 a2 , \u229d-valid b1 b2\n p\u2295-\u229d : (p2 p1 : A \u00d7 B) \u2192 p1 p\u2295 (p2 p\u229d p1) \u2261 p2\n p\u2295-\u229d (a2 , b2) (a1 , b1) = cong\u2082 _,_ (\u2295-\u229d a2 a1) (\u2295-\u229d b2 b1)\n instance\n pairCA : ChAlg (A \u00d7 B)\n pairCA = record\n { Ch = Ch A \u00d7 Ch B\n ; isChAlg = record\n { _\u2295_ = _p\u2295_\n ; _\u229d_ = _p\u229d_\n ; valid = pvalid\n ; \u229d-valid = p\u229d-valid\n ; \u2295-\u229d = p\u2295-\u229d\n } }\n\nopen import Data.Integer\nopen import Theorem.Groups-Nehemiah\n\ninstance\n intCA : ChAlg \u2124\nintCA = record\n { Ch = \u2124\n ; isChAlg = record\n { _\u2295_ = _+_\n ; _\u229d_ = _-_\n ; valid = \u03bb a b \u2192 \u22a4\n ; \u229d-valid = \u03bb a b \u2192 tt\n ; \u2295-\u229d = \u03bb b a \u2192 n+[m-n]=m {a} {b} } }\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"1be6c300fb4dd0a219451a50d15f5bc25c730bc7","subject":"Model examples change","message":"Model examples change\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/DarkwingDuck\/Examples.agda","new_file":"formalization\/agda\/Spire\/DarkwingDuck\/Examples.agda","new_contents":"{-# OPTIONS --type-in-type --no-pattern-matching #-}\nopen import Spire.DarkwingDuck.Primitive\nopen import Spire.DarkwingDuck.Derived\nmodule Spire.DarkwingDuck.Examples where\n\n----------------------------------------------------------------------\n\nNatN : String\nNatN = \"Nat\"\n\nNatP : Tel\nNatP = Emp\n\nNatI : Scope NatP \u2192 Tel\nNatI _ = Emp\n\nNatE : Enum\nNatE = \"zero\" \u2237 \"suc\" \u2237 []\n\nNatB : (p : Scope NatP) \u2192 BranchesD NatE (NatI p)\nNatB _ = End tt , Rec tt (End tt) , tt\n\nNat : Set\nNat = Form NatN NatP NatI NatE NatB\n\nzero : Nat\nzero = inj NatN NatP NatI NatE NatB here\n\nsuc : Nat \u2192 Nat\nsuc = inj NatN NatP NatI NatE NatB (there here)\n\nelimNat : (P : Nat \u2192 Set)\n (pz : P zero)\n (ps : (n : Nat) \u2192 P n \u2192 P (suc n))\n (n : Nat) \u2192 P n\nelimNat = elim NatN NatP NatI NatE NatB\n\n----------------------------------------------------------------------\n\nVecN : String\nVecN = \"Vec\"\n\nVecP : Tel\nVecP = Ext Set \u03bb _ \u2192 Emp\n\nVecI : Scope VecP \u2192 Tel\nVecI _ = Ext Nat \u03bb _ \u2192 Emp\n\nVecE : Enum\nVecE = \"nil\" \u2237 \"cons\" \u2237 []\n\nVecB : (p : Scope VecP) \u2192 BranchesD VecE (VecI p)\nVecB = uncurryScope VecP (\u03bb p \u2192 BranchesD VecE (VecI p)) \u03bb A\n \u2192 End (zero , tt)\n , Arg Nat (\u03bb n \u2192 Arg A \u03bb _ \u2192 Rec (n , tt) (End (suc n , tt)))\n , tt\n\nVec : (A : Set) \u2192 Nat \u2192 Set\nVec = Form VecN VecP VecI VecE VecB\n\nnil : (A : Set) \u2192 Vec A zero\nnil A = inj VecN VecP VecI VecE VecB A here\n\ncons : (A : Set) (n : Nat) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\ncons A = inj VecN VecP VecI VecE VecB A (there here)\n\nelimVec : (A : Set) (P : (n : Nat) \u2192 Vec A n \u2192 Set)\n (pn : P zero (nil A))\n (pc : (n : Nat) (x : A) (xs : Vec A n) \u2192 P n xs \u2192 P (suc n) (cons A n x xs))\n (n : Nat) (xs : Vec A n) \u2192 P n xs\nelimVec = elim VecN VecP VecI VecE VecB\n\n----------------------------------------------------------------------\n\nadd : Nat \u2192 Nat \u2192 Nat\nadd = elimNat\n (\u03bb n \u2192 Nat \u2192 Nat)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n\nmult : Nat \u2192 Nat \u2192 Nat\nmult = elimNat\n (\u03bb n \u2192 Nat \u2192 Nat)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n\nappend : (A : Set) (m n : Nat) (xs : Vec A m) (ys : Vec A n) \u2192 Vec A (add m n)\nappend A m n = elimVec A\n (\u03bb m xs \u2192 (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb ys \u2192 ys)\n (\u03bb m' x xs ih ys \u2192 cons A (add m' n) x (ih ys))\n m\n\nconcat : (A : Set) (m n : Nat) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\nconcat A m n = elimVec (Vec A m)\n (\u03bb n xss \u2192 Vec A (mult n m))\n (nil A)\n (\u03bb m' xs xss ih \u2192 append A m (mult m' m) xs ih)\n n\n\n----------------------------------------------------------------------\n\none : Nat\none = suc zero\n\ntwo : Nat\ntwo = suc one\n\nthree : Nat\nthree = suc two\n\n[1,2] : Vec Nat two\n[1,2] = cons Nat one one (cons Nat zero two (nil Nat))\n\n[3] : Vec Nat one\n[3] = cons Nat zero three (nil Nat)\n\n[1,2,3] : Vec Nat three\n[1,2,3] = cons Nat two one (cons Nat one two (cons Nat zero three (nil Nat)))\n\ntest-append : [1,2,3] \u2261 append Nat two one [1,2] [3]\ntest-append = refl\n\n----------------------------------------------------------------------\n","old_contents":"{-# OPTIONS --type-in-type --no-pattern-matching #-}\nopen import Spire.DarkwingDuck.Primitive\nopen import Spire.DarkwingDuck.Derived\nmodule Spire.DarkwingDuck.Examples where\n\n----------------------------------------------------------------------\n\nNatN : String\nNatN = \"Nat\"\n\nNatP : Tel\nNatP = Emp\n\nNatI : Scope NatP \u2192 Tel\nNatI _ = Emp\n\nNatE : Enum\nNatE = \"zero\" \u2237 \"suc\" \u2237 []\n\nNatB : (p : Scope NatP) \u2192 BranchesD NatE (NatI p)\nNatB _ = End tt , Rec tt (End tt) , tt\n\nNat : Set\nNat = Form NatN NatP NatI NatE NatB\n\ninjNat : CurriedScope NatP \u03bb p\n \u2192 CurriedFunc (Scope (NatI p))\n (sumD NatE (NatI p) (NatB p))\n (FormUncurried NatN NatP NatI NatE NatB p)\ninjNat = inj NatN NatP NatI NatE NatB\n\nelimNat : CurriedScope NatP \u03bb p\n \u2192 (M : CurriedScope (NatI p) (\u03bb i \u2192 FormUncurried NatN NatP NatI NatE NatB p i \u2192 Set))\n \u2192 let unM = uncurryScope (NatI p) (\u03bb i \u2192 FormUncurried NatN NatP NatI NatE NatB p i \u2192 Set) M\n in CurriedBranches NatE\n (SumCurriedHyps NatN NatP NatI NatE NatB p M)\n (CurriedScope (NatI p) (\u03bb i \u2192 (x : FormUncurried NatN NatP NatI NatE NatB p i) \u2192 unM i x))\nelimNat = elim NatN NatP NatI NatE NatB\n\nzero : Nat\nzero = injNat here\n\nsuc : Nat \u2192 Nat\nsuc = injNat (there here)\n\n----------------------------------------------------------------------\n\nVecN : String\nVecN = \"Vec\"\n\nVecP : Tel\nVecP = Ext Set \u03bb _ \u2192 Emp\n\nVecI : Scope VecP \u2192 Tel\nVecI _ = Ext Nat \u03bb _ \u2192 Emp\n\nVecE : Enum\nVecE = \"nil\" \u2237 \"cons\" \u2237 []\n\nVecB : (p : Scope VecP) \u2192 BranchesD VecE (VecI p)\nVecB = uncurryScope VecP (\u03bb p \u2192 BranchesD VecE (VecI p)) \u03bb A\n \u2192 End (zero , tt)\n , Arg Nat (\u03bb n \u2192 Arg A \u03bb _ \u2192 Rec (n , tt) (End (suc n , tt)))\n , tt\n\nVec : (A : Set) \u2192 Nat \u2192 Set\nVec = Form VecN VecP VecI VecE VecB\n\ninjVec : CurriedScope VecP \u03bb p\n \u2192 CurriedFunc (Scope (VecI p))\n (sumD VecE (VecI p) (VecB p))\n (FormUncurried VecN VecP VecI VecE VecB p)\ninjVec = inj VecN VecP VecI VecE VecB\n\nelimVec : CurriedScope VecP \u03bb p\n \u2192 (M : CurriedScope (VecI p) (\u03bb i \u2192 FormUncurried VecN VecP VecI VecE VecB p i \u2192 Set))\n \u2192 let unM = uncurryScope (VecI p) (\u03bb i \u2192 FormUncurried VecN VecP VecI VecE VecB p i \u2192 Set) M\n in CurriedBranches VecE\n (SumCurriedHyps VecN VecP VecI VecE VecB p M)\n (CurriedScope (VecI p) (\u03bb i \u2192 (x : FormUncurried VecN VecP VecI VecE VecB p i) \u2192 unM i x))\nelimVec = elim VecN VecP VecI VecE VecB\n\nnil : (A : Set) \u2192 Vec A zero\nnil A = injVec A here\n\ncons : (A : Set) (n : Nat) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\ncons A = injVec A (there here)\n\n----------------------------------------------------------------------\n\nadd : Nat \u2192 Nat \u2192 Nat\nadd = elimNat\n (\u03bb n \u2192 Nat \u2192 Nat)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n\nmult : Nat \u2192 Nat \u2192 Nat\nmult = elimNat\n (\u03bb n \u2192 Nat \u2192 Nat)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n\nappend : (A : Set) (m n : Nat) (xs : Vec A m) (ys : Vec A n) \u2192 Vec A (add m n)\nappend A m n = elimVec A\n (\u03bb m xs \u2192 (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb ys \u2192 ys)\n (\u03bb m' x xs ih ys \u2192 cons A (add m' n) x (ih ys))\n m\n\nconcat : (A : Set) (m n : Nat) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\nconcat A m n = elimVec (Vec A m)\n (\u03bb n xss \u2192 Vec A (mult n m))\n (nil A)\n (\u03bb m' xs xss ih \u2192 append A m (mult m' m) xs ih)\n n\n\n----------------------------------------------------------------------\n\none : Nat\none = suc zero\n\ntwo : Nat\ntwo = suc one\n\nthree : Nat\nthree = suc two\n\n[1,2] : Vec Nat two\n[1,2] = cons Nat one one (cons Nat zero two (nil Nat))\n\n[3] : Vec Nat one\n[3] = cons Nat zero three (nil Nat)\n\n[1,2,3] : Vec Nat three\n[1,2,3] = cons Nat two one (cons Nat one two (cons Nat zero three (nil Nat)))\n\ntest-append : [1,2,3] \u2261 append Nat two one [1,2] [3]\ntest-append = refl\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"9af23cf22e1ab362777619cd58cbabe4e108b6db","subject":"Forgotten an ATP pragma.","message":"Forgotten an ATP pragma.\n\nIgnore-this: 258014be53497c276baad0e0bb8d150\n\ndarcs-hash:20100525132333-3bd4e-e9de1d5b04d7e26b8ba973256477a726f831a691.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Relation\/Equalities\/Properties.agda","new_file":"LTC\/Relation\/Equalities\/Properties.agda","new_contents":"------------------------------------------------------------------------------\n-- Functions on equalities\n------------------------------------------------------------------------------\n\nmodule LTC.Relation.Equalities.Properties where\n\nopen import LTC.Minimal\n\n------------------------------------------------------------------------------\n\npostulate\n sym : {x y : D} \u2192 x \u2261 y \u2192 y \u2261 x\n{-# ATP prove sym #-}\n\npostulate\n trans : {x y z : D} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n{-# ATP prove trans #-}\n\npostulate\n \u00acS\u22610 : {d : D} \u2192 \u00ac (succ d \u2261 zero)\n{-# ATP prove \u00acS\u22610 #-}\n\nx\u2261y\u2192Sx\u2261Sy : {m n : D} \u2192 m \u2261 n \u2192 succ m \u2261 succ n\nx\u2261y\u2192Sx\u2261Sy refl = refl\n","old_contents":"------------------------------------------------------------------------------\n-- Functions on equalities\n------------------------------------------------------------------------------\n\nmodule LTC.Relation.Equalities.Properties where\n\nopen import LTC.Minimal\n\n------------------------------------------------------------------------------\n\npostulate\n sym : {x y : D} \u2192 x \u2261 y \u2192 y \u2261 x\n{-# ATP prove sym #-}\n\npostulate\n trans : {x y z : D} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n\npostulate\n \u00acS\u22610 : {d : D} \u2192 \u00ac (succ d \u2261 zero)\n{-# ATP prove \u00acS\u22610 #-}\n\nx\u2261y\u2192Sx\u2261Sy : {m n : D} \u2192 m \u2261 n \u2192 succ m \u2261 succ n\nx\u2261y\u2192Sx\u2261Sy refl = refl\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ae4b3986e9fb874d12f823c58e64f325d8167830","subject":"Hutton's razor: nop, don't try to compute context, life's too short for that.","message":"Hutton's razor: nop, don't try to compute context, life's too short for that.","repos":"kwangkim\/pigment,kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram","old_file":"models\/RevisedHutton.agda","new_file":"models\/RevisedHutton.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule RevisedHutton where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\ndata Vec (A : Set) : Nat -> Set where\n vnil : Vec A ze\n vcons : {n : Nat} -> A -> Vec A n -> Vec A (su n)\n\ndata Fin : Nat -> Set where\n fze : {n : Nat} -> Fin (su n)\n fsu : {n : Nat} -> Fin n -> Fin (su n)\n\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n pair : Type -> Type -> Type\n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\n-- Fix menu:\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void))\n\n-- Index-dependent menu:\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\nchoiceFreeMenu (pair x y) = nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void) -- plus x y\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void ) -- le x y\nchoiceFreeDessert (pair x y) = Void\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\n-- Tagged description of expressions\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\n-- Free monadic expressions\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\nVal (pair x y) = (Val x) * (Val y)\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = toIDesc Type (exprFree ** Val)\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : {ty : Type} -> IMu closeTerm ty -> Val ty\neval {ty} term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n\n--********************************\n-- [Open terms]\n--********************************\n\n-- Context and context lookup \nContext : Nat -> Set\nContext n = Vec (Sigma Type (\\ty -> IMu closeTerm ty)) n\n\ntypeAt : {n : Nat}(c : Context n) -> Fin n -> Type\ntypeAt {ze} c ()\ntypeAt {su _} (vcons x _) fze = fst x\ntypeAt {su _} (vcons _ xs) (fsu y) = typeAt xs y\n\nlookup : {n : Nat}(c : Context n)(i : Fin n) -> IMu closeTerm (typeAt c i)\nlookup {ze} c ()\nlookup {su _} (vcons x _) fze = snd x\nlookup {su _} (vcons _ xs) (fsu y) = lookup xs y\n\n-- A variable is an index into the context *and* a proof that the\n-- context contains the expected stuff\nVar : {n : Nat} -> Context n -> Type -> Set\nVar {n} c ty = Sigma (Fin n) (\\i -> typeAt c i == ty)\n\n-- Open term: holes are either values or variables in a context\nopenTerm : {n : Nat} -> Context n -> Type -> IDesc Type\nopenTerm c = toIDesc Type (exprFree ** (\\ty -> Val ty + Var c ty))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\n-- |discharge| is the local substitution expected by |substI|. It is\n-- just sugar around context lookup, taking care of transmitting the\n-- proof\ndischarge : {n : Nat}\n {context : Context n}\n (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm ty\ndischarge ty (l value) = con (EZe , value)\ndischarge {n} {c} ty (r variable) = \n subst (cong (IMu closeTerm) (snd variable)) \n (lookup c (fst variable))\n\n-- |substExpr| is the specialized |substI| to expressions. We get it\n-- generically from the free monad construction.\nsubstExpr : {n : Nat}\n {ty : Type}\n (context : Context n)\n (sigma : (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm ty\nsubstExpr {n} {ty} c sig term = \n substI (\\ty -> Val ty + Var c ty) Val exprFree sig ty term\n\n-- By first doing substitution to close the term, we can use\n-- evaluation of closed terms, obtaining evaluation of open terms\n-- under a valid context.\nevalOpen : {n : Nat}{ty : Type}\n (context : Context n) ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen context tm = eval (substExpr context discharge tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- V 0 :-> true, V 1 :-> 2, V 2 :-> ( false , 1 )\ntestContext : Context _\ntestContext = vcons (bool , con (EZe , true )) \n (vcons (nat , con (EZe , su (su ze)) ) \n (vcons (pair bool nat , con (EZe , ( false , su ze )))\n vnil))\n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , r ( fsu fze , refl ) )\n\ntestSubst1 : IMu closeTerm nat\ntestSubst1 = substExpr testContext \n discharge \n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con (ESu (ESu EZe) , (con (EZe , l (su ze)) , con ( EZe , r (fsu fze , refl) )) )\n\ntestSubst2 : IMu closeTerm nat\ntestSubst2 = substExpr testContext \n discharge \n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext test2\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu EZe , (con (EZe , r (fze , refl)) ,\n (con (EZe , r (fsu fze , refl)) ,\n con (EZe , l ze))))\n\ntestSubst3 : IMu closeTerm nat\ntestSubst3 = substExpr testContext \n discharge \n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext test3\n\n-- V 2\ntest4 : IMu (openTerm testContext) (pair bool nat)\ntest4 = con (EZe , r ( fsu (fsu fze) , refl ) )\n\ntestSubst4 : IMu closeTerm (pair bool nat)\ntestSubst4 = substExpr testContext \n discharge \n test4\n-- = (false , 1)\ntestEval4 : Val (pair bool nat)\ntestEval4 = evalOpen testContext test4\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule RevisedHutton where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\ndata List (A : Set) : Set where\n [] : List A\n _::_ : A -> List A -> List A\n\ndata Vec (A : Set) : Nat -> Set where\n vnil : Vec A ze\n vcons : {n : Nat} -> A -> Vec A n -> Vec A (su n)\n\ndata Fin : Nat -> Set where\n fze : {n : Nat} -> Fin (su n)\n fsu : {n : Nat} -> Fin n -> Fin (su n)\n\ndata Comp : Set where\n LT : Comp\n EQ : Comp\n GT : Comp\n\ncomp : {n : Nat} -> Fin n -> Fin n -> Comp\ncomp fze fze = EQ\ncomp fze _ = LT\ncomp _ fze = GT\ncomp (fsu n) (fsu n') = comp n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n pair : Type -> Type -> Type\n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\n-- Fix menu:\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void))\n\n-- Index-dependent menu:\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\nchoiceFreeMenu (pair x y) = nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void) -- plus x y\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void ) -- le x y\nchoiceFreeDessert (pair x y) = Void\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\n-- Tagged description of expressions\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\n-- Free monadic expressions\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\nVal (pair x y) = (Val x) * (Val y)\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = toIDesc Type (exprFree ** Val)\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : {ty : Type} -> IMu closeTerm ty -> Val ty\neval {ty} term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n\n--********************************\n-- [Open terms]\n--********************************\n\n-- Context and context lookup \nContext : Nat -> Set\nContext n = Vec (Sigma Type (\\ty -> IMu closeTerm ty)) n\n\ntypeAt : {n : Nat}(c : Context n) -> Fin n -> Type\ntypeAt {ze} c ()\ntypeAt {su _} (vcons x _) fze = fst x\ntypeAt {su _} (vcons _ xs) (fsu y) = typeAt xs y\n\nlookup : {n : Nat}(c : Context n)(i : Fin n) -> IMu closeTerm (typeAt c i)\nlookup {ze} c ()\nlookup {su _} (vcons x _) fze = snd x\nlookup {su _} (vcons _ xs) (fsu y) = lookup xs y\n\n-- A variable is an index into the context *and* a proof that the\n-- context contains the expected stuff\nVar : Nat -> Type -> Set\nVar n ty = Fin n\n\n-- Open term: holes are either values or variables in a context\nopenTerm : Nat -> Type -> IDesc Type\nopenTerm n = toIDesc Type (exprFree ** (\\ty -> Val ty + Var n ty))\n\ndata Maybe (A : Set) : Set where\n Nothing : Maybe A\n Just : A -> Maybe A\n\nmergeVars : {n : Nat} ->\n Maybe (List (Fin n * Type)) ->\n Maybe (List (Fin n * Type)) ->\n Maybe (List (Fin n * Type))\nmergeVars Nothing _ = Nothing\nmergeVars _ Nothing = Nothing\nmergeVars (Just []) (Just y) = Just y\nmergeVars (Just x) (Just []) = Just x\nmergeVars (Just (x :: xs)) (Just (y :: ys)) with comp (fst x) (fst y) \n | mergeVars (Just xs) (Just (y :: ys)) \n | mergeVars (Just (x :: xs)) (Just ys)\n... | LT | _ | Nothing = Nothing\n... | LT | _ | Just xys = Just (y :: xys)\n... | EQ | _ | _ = Nothing\n... | GT | Nothing | _ = Nothing\n... | GT | Just xys | _ = Just (x :: xys)\n\ncollectVars : {ty : Type}{n : Nat} -> IMu (openTerm n) ty -> Maybe (List (Fin n * Type))\ncollectVars {ty} {n} t = cata Type (openTerm n) (\\_ -> Maybe (List (Fin n * Type))) collectVarsHelp ty t\n where collectVarsHelp : (i : Type) -> [| openTerm n i |] (\\ _ -> Maybe (List (Fin n * Type))) -> Maybe (List (Fin n * Type))\n collectVarsHelp ty (EZe , r n) = Just ((n , ty) :: [])\n collectVarsHelp ty (EZe , l _) = Just []\n collectVarsHelp ty (ESu EZe , (t1 , ( t2 , t3))) = mergeVars (mergeVars t1 t2) t3\n collectVarsHelp nat (ESu (ESu EZe) , (x , y)) = mergeVars x y\n collectVarsHelp nat (ESu (ESu (ESu ())) , _) \n collectVarsHelp bool (ESu (ESu EZe) , (x , y) ) = mergeVars x y\n collectVarsHelp bool (ESu (ESu (ESu ())) , _) \n collectVarsHelp (pair x y) (ESu (ESu ()) , _)\n \n\nValidContext : {ty : Type}{n : Nat}(c : Context n)(tm : IMu (openTerm n) ty) -> Set\nValidContext {ty} {n} c tm with collectVars tm\n... | Nothing = Zero\n... | (Just y) = checkContext y\n where checkContext : List (Fin n * Type) -> Set\n checkContext [] = Unit\n checkContext ((x , ty) :: xs) = ((typeAt c x) == ty) * checkContext xs \n\n--********************************\n-- Evaluation of open terms\n--********************************\n\n-- |discharge| is the local substitution expected by |substI|. It is\n-- just sugar around context lookup, taking care of transmitting the\n-- proof\ndischarge : {n : Nat}\n {context : Context n}\n (ty : Type) ->\n (Val ty + Var n ty) ->\n IMu closeTerm ty\ndischarge ty (l value) = con (EZe , value)\ndischarge {n} {c} ty (r variable) = {!lookup c variable!}\n-- (lookup c (fst variable))\n\n-- |substExpr| is the specialized |substI| to expressions. We get it\n-- generically from the free monad construction.\nsubstExpr : {n : Nat}\n {ty : Type}\n (context : Context n)\n (sigma : (ty : Type) ->\n (Val ty + Var n ty) ->\n IMu closeTerm ty) ->\n (tm : IMu (openTerm n) ty) ->\n ValidContext context tm ->\n IMu closeTerm ty\nsubstExpr {n} {ty} c sig term valid = \n substI (\\ty -> Val ty + Var n ty) Val exprFree sig ty term\n\n\n\n{-\n\n-- By first doing substitution to close the term, we can use\n-- evaluation of closed terms, obtaining evaluation of open terms\n-- under a valid context.\nevalOpen : {n : Nat}{ty : Type}\n (context : Context n) ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen context tm = eval (substExpr context discharge tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- V 0 :-> true, V 1 :-> 2, V 2 :-> ( false , 1 )\ntestContext : Context _\ntestContext = vcons (bool , con (EZe , true )) \n (vcons (nat , con (EZe , su (su ze)) ) \n (vcons (pair bool nat , con (EZe , ( false , su ze )))\n vnil))\n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , r ( fsu fze , refl ) )\n\ntestSubst1 : IMu closeTerm nat\ntestSubst1 = substExpr testContext \n discharge \n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con (ESu (ESu EZe) , (con (EZe , l (su ze)) , con ( EZe , r (fsu fze , refl) )) )\n\ntestSubst2 : IMu closeTerm nat\ntestSubst2 = substExpr testContext \n discharge \n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext test2\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu EZe , (con (EZe , r (fze , refl)) ,\n (con (EZe , r (fsu fze , refl)) ,\n con (EZe , l ze))))\n\ntestSubst3 : IMu closeTerm nat\ntestSubst3 = substExpr testContext \n discharge \n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext test3\n\n-- V 2\ntest4 : IMu (openTerm testContext) (pair bool nat)\ntest4 = con (EZe , r ( fsu (fsu fze) , refl ) )\n\ntestSubst4 : IMu closeTerm (pair bool nat)\ntestSubst4 = substExpr testContext \n discharge \n test4\n-- = (false , 1)\ntestEval4 : Val (pair bool nat)\ntestEval4 = evalOpen testContext test4\n-}","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"052a571080bb4647e0d34a8cc8fb5454c47c7ec4","subject":"agda: prove completely that diff-is-valid (#33)","message":"agda: prove completely that diff-is-valid (#33)\n\nThe proof is much more different from the one of derive-is-valid than\nI'd have expected, but it was relatively easy to complete directly in\nAgda, without doing it on paper beforehand.\n\nderive-is-valid is proved as a corollary in one line.\n\nOld-commit-hash: fd47a2b1346fb45e5e94db8d814e83e9fdf1442d\n","repos":"inc-lc\/ilc-agda","old_file":"total.agda","new_file":"total.agda","new_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Note that \u0394 is *not* the same as the \u2202 operator in\n-- definition\/intro.tex. See discussion at:\n--\n-- https:\/\/github.com\/ps-mr\/ilc\/pull\/34#discussion_r4290325\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nopen import Data.Product\nopen import Data.Unit\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Changes\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\n\nopen import Changes\nopen import ChangeContexts\n\n-- DEFINITION of valid changes via a logical relation\n\n{-\nWhat I wanted to write:\n\ndata Valid\u0394 : {T : Type} \u2192 (v : \u27e6 T \u27e7) \u2192 (dv : \u27e6 \u0394-Type T \u27e7) \u2192 Set where\n base : (v : \u27e6 bool \u27e7) \u2192 (dv : \u27e6 \u0394-Type bool \u27e7) \u2192 Valid\u0394 v dv\n fun : \u2200 {S T} \u2192 (f : \u27e6 S \u21d2 T \u27e7) \u2192 (df : \u27e6 \u0394-Type (S \u21d2 T) \u27e7) \u2192\n (\u2200 (s : \u27e6 S \u27e7) ds (valid : Valid\u0394 s ds) \u2192 (Valid\u0394 (f s) (df s ds)) \u00d7 ((apply df f) (apply ds s) \u2261 apply (df s ds) (f s))) \u2192 \n Valid\u0394 f df\n-}\n-- What I had to write:\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {bool} v dv = \u22a4\nvalid-\u0394 {S \u21d2 T} f df =\n \u2200 (s : \u27e6 S \u27e7) ds (valid-w : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7\n (apply df f) (apply ds s) \u2261 apply (df s ds) (f s)\n\ndiff-is-valid : \u2200 {\u03c4} (v\u2032 v : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} v (diff v\u2032 v)\ndiff-is-valid {bool} v\u2032 v = tt\ndiff-is-valid {\u03c4 \u21d2 \u03c4\u2081} v\u2032 v =\n \u03bb s ds valid-\u0394-s-ds \u2192\n diff-is-valid (v\u2032 (apply ds s)) (v s) , (\n begin\n apply (diff v\u2032 v) v (apply ds s)\n \u2261\u27e8 refl \u27e9\n apply\n (diff (v\u2032 (apply (derive (apply ds s)) (apply ds s))) (v (apply ds s)))\n (v (apply ds s))\n \u2261\u27e8 \u2261-cong (\u03bb x \u2192 apply (diff (v\u2032 x) (v (apply ds s))) (v (apply ds s))) (apply-derive (apply ds s)) \u27e9\n apply (diff (v\u2032 (apply ds s)) (v (apply ds s))) (v (apply ds s))\n \u2261\u27e8 apply-diff (v (apply ds s)) (v\u2032 (apply ds s)) \u27e9\n v\u2032 (apply ds s)\n \u2261\u27e8 sym (apply-diff (v s) (v\u2032 (apply ds s))) \u27e9\n apply ((diff v\u2032 v) s ds) (v s)\n \u220e) where open \u2261-Reasoning\n\nderive-is-valid : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} v (derive v)\nderive-is-valid v rewrite sym (diff-derive v) = diff-is-valid v v\n\n-- This is a postulate elsewhere, but here I want to start developing a proper proof.\n\ndiff-apply-proof : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n (valid-\u0394 v dv) \u2192 diff (apply dv v) v \u2261 dv\n\ndiff-apply-proof {\u03c4\u2081 \u21d2 \u03c4\u2082} df f df-valid = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n begin\n diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (sym (proj\u2082 (df-valid (apply dv v) (derive (apply dv v)) (derive-is-valid (apply dv v))))) \u27e9\n diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff (apply df f x) (f v))\n (apply-derive (apply dv v)) \u27e9\n diff ((apply df f) (apply dv v)) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (proj\u2082 (df-valid v dv {!!})) \u27e9\n diff (apply (df v dv) (f v)) (f v)\n \u2261\u27e8 diff-apply-proof {\u03c4\u2082} (df v dv) (f v) (proj\u2081 (df-valid v dv {!!})) \u27e9\n df v dv\n \u220e)) where open \u2261-Reasoning\n\ndiff-apply-proof {bool} db b _ = xor-cancellative b db\n\n-- LIFTING terms into \u0394-Contexts\n\nlift-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nlift-term {\u0393\u2081} {\u0393\u2082} {{\u0393\u2032}} = weaken (\u227c-trans \u227c-\u0394-Context \u0393\u2032)\n\n-- PROPERTIES of lift-term\n\nlift-term-ignore : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} {\u03c1 : \u27e6 \u0393\u2082 \u27e7} (t : Term \u0393\u2081 \u03c4) \u2192\n \u27e6 lift-term {{\u0393\u2032}} t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1))\nlift-term-ignore {{\u0393\u2032}} {\u03c1} t = let \u0393\u2033 = \u227c-trans \u227c-\u0394-Context \u0393\u2032 in\n begin\n \u27e6 lift-term {{\u0393\u2032}} t \u27e7 \u03c1\n \u2261\u27e8\u27e9\n \u27e6 weaken \u0393\u2033 t \u27e7 \u03c1\n \u2261\u27e8 weaken-sound t \u03c1 \u27e9\n \u27e6 t \u27e7 (\u27e6 \u227c-trans \u227c-\u0394-Context \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 cong (\u03bb x \u2192 \u27e6 t \u27e7 x) (\u27e6\u27e7-\u227c-trans \u227c-\u0394-Context \u0393\u2032 \u03c1) \u27e9\n \u27e6 t \u27e7Term (\u27e6 \u227c-\u0394-Context \u27e7\u227c (\u27e6 \u0393\u2032 \u27e7\u227c \u03c1))\n \u2261\u27e8\u27e9\n \u27e6 t \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1))\n \u220e where open \u2261-Reasoning\n\n-- PROPERTIES of \u0394\n\n\u0394-abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} (t : Term (\u03c4\u2081 \u2022 \u0393\u2081) \u03c4\u2082) \u2192\n let \u0393\u2033 = keep \u0394-Type \u03c4\u2081 \u2022 keep \u03c4\u2081 \u2022 \u0393\u2032 in\n \u0394 {{\u0393\u2032}} (abs t) \u2248 abs (abs (\u0394 {\u03c4\u2081 \u2022 \u0393\u2081} t))\n\u0394-abs t = ext-t (\u03bb \u03c1 \u2192 refl)\n\n\u0394-app : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4\u2081 \u03c4\u2082} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} (t\u2081 : Term \u0393\u2081 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393\u2081 \u03c4\u2081) \u2192\n \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082) \u2248 app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n\u0394-app {{\u0393\u2032}} t\u2081 t\u2082 = \u2248-sym (ext-t (\u03bb \u03c1\u2032 \u2192 let \u03c1 = \u27e6 \u0393\u2032 \u27e7 \u03c1\u2032 in\n begin\n \u27e6 app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082) \u27e7 \u03c1\u2032\n \u2261\u27e8\u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 lift-term {{\u0393\u2032}} t\u2082 \u27e7 \u03c1\u2032)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 lift-term {{\u0393\u2032}} t\u2082 \u27e7 \u03c1\u2032))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))) x))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) x))\n (lift-term-ignore {{\u0393\u2032}} t\u2082) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff (\u27e6 t\u2081 \u27e7 (update \u03c1) x) (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (apply-diff (\u27e6 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 t\u2082 \u27e7 (update \u03c1))) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8\u27e9\n \u27e6 \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082) \u27e7 \u03c1\u2032\n \u220e)) where open \u2261-Reasoning\n\n-- SYMBOLIC DERIVATION\n\nderive-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-var {\u03c4 \u2022 \u0393} this = this\nderive-var {\u03c4 \u2022 \u0393} (that x) = that (that (derive-var x))\n\nderive-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 (\u0394-Type \u03c4)\nderive-term {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) = abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t))\n where \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term {{\u0393\u2032}} (app t\u2081 t\u2082) = app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\nderive-term {{\u0393\u2032}} (var x) = var (lift \u0393\u2032 (derive-var x))\nderive-term {{\u0393\u2032}} true = false\nderive-term {{\u0393\u2032}} false = false\nderive-term {{\u0393\u2032}} (if c t e) =\n if ((derive-term {{\u0393\u2032}} c) and (lift-term {{\u0393\u2032}} c))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} e) (lift-term {{\u0393\u2032}} e)) (lift-term {{\u0393\u2032}} t))\n (if ((derive-term {{\u0393\u2032}} c) and (lift-term {{\u0393\u2032}} (! c)))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} t) (lift-term {{\u0393\u2032}} t)) (lift-term {{\u0393\u2032}} e))\n (if (lift-term {{\u0393\u2032}} c)\n (derive-term {{\u0393\u2032}} t)\n (derive-term {{\u0393\u2032}} e)))\nderive-term {{\u0393\u2032}} (\u0394 {{\u0393\u2033}} t) = \u0394 {{\u0393\u2032}} (derive-term {{\u0393\u2033}} t)\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 (t : Term \u0393\u2081 \u03c4) \u2192\n \u0394 {{\u0393\u2032}} t \u2248 derive-term {{\u0393\u2032}} t\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 {\u03c4 \u2022 \u0393\u2081} t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct {\u03c4 \u2022 \u0393\u2081} t)) \u27e9\n abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} t))\n \u2261\u27e8\u27e9\n derive-term (abs t)\n \u220e where\n open \u2248-Reasoning\n \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct {{\u0393\u2032}} t\u2081) \u2248-refl) (derive-term-correct {{\u0393\u2032}} t\u2082) \u27e9\n app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (var x) = ext-t (\u03bb \u03c1 \u2192\n begin\n \u27e6 \u0394 {{\u0393\u2032}} (var x) \u27e7 \u03c1\n \u2261\u27e8\u27e9\n diff\n (\u27e6 x \u27e7 (update (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n (\u27e6 x \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n \u2261\u27e8 derive-var-correct {\u0393\u2081} (\u27e6 \u0393\u2032 \u27e7 \u03c1) x \u27e9\n \u27e6 derive-var x \u27e7Var (\u27e6 \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 sym (lift-sound \u0393\u2032 (derive-var x) \u03c1) \u27e9\n \u27e6 lift \u0393\u2032 (derive-var x) \u27e7Var \u03c1\n \u220e) where open \u2261-Reasoning\nderive-term-correct true = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\n","old_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Note that \u0394 is *not* the same as the \u2202 operator in\n-- definition\/intro.tex. See discussion at:\n--\n-- https:\/\/github.com\/ps-mr\/ilc\/pull\/34#discussion_r4290325\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nopen import Data.Product\nopen import Data.Unit\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Changes\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\n\nopen import Changes\nopen import ChangeContexts\n\n-- DEFINITION of valid changes via a logical relation\n\n{-\nWhat I wanted to write:\n\ndata Valid\u0394 : {T : Type} \u2192 (v : \u27e6 T \u27e7) \u2192 (dv : \u27e6 \u0394-Type T \u27e7) \u2192 Set where\n base : (v : \u27e6 bool \u27e7) \u2192 (dv : \u27e6 \u0394-Type bool \u27e7) \u2192 Valid\u0394 v dv\n fun : \u2200 {S T} \u2192 (f : \u27e6 S \u21d2 T \u27e7) \u2192 (df : \u27e6 \u0394-Type (S \u21d2 T) \u27e7) \u2192\n (\u2200 (s : \u27e6 S \u27e7) ds (valid : Valid\u0394 s ds) \u2192 (Valid\u0394 (f s) (df s ds)) \u00d7 ((apply df f) (apply ds s) \u2261 apply (df s ds) (f s))) \u2192 \n Valid\u0394 f df\n-}\n-- What I had to write:\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {bool} v dv = \u22a4\nvalid-\u0394 {S \u21d2 T} f df =\n \u2200 (s : \u27e6 S \u27e7) ds (valid-w : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7\n (apply df f) (apply ds s) \u2261 apply (df s ds) (f s)\n\n\ndiff-is-valid : \u2200 {\u03c4} (v\u2032 v : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} v (diff v\u2032 v)\ndiff-is-valid = {!!}\n\n-- This proof could be finished using diff-is-valid:\nderive-is-valid : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} v (derive v)\nderive-is-valid {bool} v = tt\nderive-is-valid {\u03c4\u2081 \u21d2 \u03c4\u2082} v =\n \u03bb s ds valid-\u0394-s-ds \u2192 diff-is-valid (v (apply ds s)) (v s) , (\n begin\n (apply (derive v) v) (apply ds s)\n \u2261\u27e8 \u2261-cong (\u03bb x \u2192 x (apply ds s)) (apply-derive v) \u27e9\n v (apply ds s)\n \u2261\u27e8 sym (apply-diff (v s) (v (apply ds s))) \u27e9\n apply (derive v s ds) (v s)\n \u220e) where open \u2261-Reasoning\n\n-- This is a postulate elsewhere, but here I want to start developing a proper proof.\n\ndiff-apply-proof : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n (valid-\u0394 v dv) \u2192 diff (apply dv v) v \u2261 dv\n\ndiff-apply-proof {\u03c4\u2081 \u21d2 \u03c4\u2082} df f df-valid = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n begin\n diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (sym (proj\u2082 (df-valid (apply dv v) (derive (apply dv v)) (derive-is-valid (apply dv v))))) \u27e9\n diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff (apply df f x) (f v))\n (apply-derive (apply dv v)) \u27e9\n diff ((apply df f) (apply dv v)) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (proj\u2082 (df-valid v dv {!!})) \u27e9\n diff (apply (df v dv) (f v)) (f v)\n \u2261\u27e8 diff-apply-proof {\u03c4\u2082} (df v dv) (f v) (proj\u2081 (df-valid v dv {!!})) \u27e9\n df v dv\n \u220e)) where open \u2261-Reasoning\n\ndiff-apply-proof {bool} db b _ = xor-cancellative b db\n\n-- LIFTING terms into \u0394-Contexts\n\nlift-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nlift-term {\u0393\u2081} {\u0393\u2082} {{\u0393\u2032}} = weaken (\u227c-trans \u227c-\u0394-Context \u0393\u2032)\n\n-- PROPERTIES of lift-term\n\nlift-term-ignore : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} {\u03c1 : \u27e6 \u0393\u2082 \u27e7} (t : Term \u0393\u2081 \u03c4) \u2192\n \u27e6 lift-term {{\u0393\u2032}} t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1))\nlift-term-ignore {{\u0393\u2032}} {\u03c1} t = let \u0393\u2033 = \u227c-trans \u227c-\u0394-Context \u0393\u2032 in\n begin\n \u27e6 lift-term {{\u0393\u2032}} t \u27e7 \u03c1\n \u2261\u27e8\u27e9\n \u27e6 weaken \u0393\u2033 t \u27e7 \u03c1\n \u2261\u27e8 weaken-sound t \u03c1 \u27e9\n \u27e6 t \u27e7 (\u27e6 \u227c-trans \u227c-\u0394-Context \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 cong (\u03bb x \u2192 \u27e6 t \u27e7 x) (\u27e6\u27e7-\u227c-trans \u227c-\u0394-Context \u0393\u2032 \u03c1) \u27e9\n \u27e6 t \u27e7Term (\u27e6 \u227c-\u0394-Context \u27e7\u227c (\u27e6 \u0393\u2032 \u27e7\u227c \u03c1))\n \u2261\u27e8\u27e9\n \u27e6 t \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1))\n \u220e where open \u2261-Reasoning\n\n-- PROPERTIES of \u0394\n\n\u0394-abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} (t : Term (\u03c4\u2081 \u2022 \u0393\u2081) \u03c4\u2082) \u2192\n let \u0393\u2033 = keep \u0394-Type \u03c4\u2081 \u2022 keep \u03c4\u2081 \u2022 \u0393\u2032 in\n \u0394 {{\u0393\u2032}} (abs t) \u2248 abs (abs (\u0394 {\u03c4\u2081 \u2022 \u0393\u2081} t))\n\u0394-abs t = ext-t (\u03bb \u03c1 \u2192 refl)\n\n\u0394-app : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4\u2081 \u03c4\u2082} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} (t\u2081 : Term \u0393\u2081 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393\u2081 \u03c4\u2081) \u2192\n \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082) \u2248 app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n\u0394-app {{\u0393\u2032}} t\u2081 t\u2082 = \u2248-sym (ext-t (\u03bb \u03c1\u2032 \u2192 let \u03c1 = \u27e6 \u0393\u2032 \u27e7 \u03c1\u2032 in\n begin\n \u27e6 app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082) \u27e7 \u03c1\u2032\n \u2261\u27e8\u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 lift-term {{\u0393\u2032}} t\u2082 \u27e7 \u03c1\u2032)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 lift-term {{\u0393\u2032}} t\u2082 \u27e7 \u03c1\u2032))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))) x))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) x))\n (lift-term-ignore {{\u0393\u2032}} t\u2082) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff (\u27e6 t\u2081 \u27e7 (update \u03c1) x) (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (apply-diff (\u27e6 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 t\u2082 \u27e7 (update \u03c1))) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8\u27e9\n \u27e6 \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082) \u27e7 \u03c1\u2032\n \u220e)) where open \u2261-Reasoning\n\n-- SYMBOLIC DERIVATION\n\nderive-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-var {\u03c4 \u2022 \u0393} this = this\nderive-var {\u03c4 \u2022 \u0393} (that x) = that (that (derive-var x))\n\nderive-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 (\u0394-Type \u03c4)\nderive-term {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) = abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t))\n where \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term {{\u0393\u2032}} (app t\u2081 t\u2082) = app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\nderive-term {{\u0393\u2032}} (var x) = var (lift \u0393\u2032 (derive-var x))\nderive-term {{\u0393\u2032}} true = false\nderive-term {{\u0393\u2032}} false = false\nderive-term {{\u0393\u2032}} (if c t e) =\n if ((derive-term {{\u0393\u2032}} c) and (lift-term {{\u0393\u2032}} c))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} e) (lift-term {{\u0393\u2032}} e)) (lift-term {{\u0393\u2032}} t))\n (if ((derive-term {{\u0393\u2032}} c) and (lift-term {{\u0393\u2032}} (! c)))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} t) (lift-term {{\u0393\u2032}} t)) (lift-term {{\u0393\u2032}} e))\n (if (lift-term {{\u0393\u2032}} c)\n (derive-term {{\u0393\u2032}} t)\n (derive-term {{\u0393\u2032}} e)))\nderive-term {{\u0393\u2032}} (\u0394 {{\u0393\u2033}} t) = \u0394 {{\u0393\u2032}} (derive-term {{\u0393\u2033}} t)\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 (t : Term \u0393\u2081 \u03c4) \u2192\n \u0394 {{\u0393\u2032}} t \u2248 derive-term {{\u0393\u2032}} t\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 {\u03c4 \u2022 \u0393\u2081} t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct {\u03c4 \u2022 \u0393\u2081} t)) \u27e9\n abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} t))\n \u2261\u27e8\u27e9\n derive-term (abs t)\n \u220e where\n open \u2248-Reasoning\n \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct {{\u0393\u2032}} t\u2081) \u2248-refl) (derive-term-correct {{\u0393\u2032}} t\u2082) \u27e9\n app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (var x) = ext-t (\u03bb \u03c1 \u2192\n begin\n \u27e6 \u0394 {{\u0393\u2032}} (var x) \u27e7 \u03c1\n \u2261\u27e8\u27e9\n diff\n (\u27e6 x \u27e7 (update (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n (\u27e6 x \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n \u2261\u27e8 derive-var-correct {\u0393\u2081} (\u27e6 \u0393\u2032 \u27e7 \u03c1) x \u27e9\n \u27e6 derive-var x \u27e7Var (\u27e6 \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 sym (lift-sound \u0393\u2032 (derive-var x) \u03c1) \u27e9\n \u27e6 lift \u0393\u2032 (derive-var x) \u27e7Var \u03c1\n \u220e) where open \u2261-Reasoning\nderive-term-correct true = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"34efeb45d59b4e286806101d366893665f25f04c","subject":"agda\/...: lift-term\u2032 \u0394 (see #22)","message":"agda\/...: lift-term\u2032 \u0394 (see #22)\n\nOld-commit-hash: 751e41dfecd3ae6383acc772da530f50c6b1ce83\n","repos":"inc-lc\/ilc-agda","old_file":"total.agda","new_file":"total.agda","new_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nimport Relation.Binary as B\n\nopen import Relation.Binary using\n (IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)\nimport Relation.Binary.EqReasoning as EqR\n\nopen import Relation.Nullary using (\u00ac_)\n\nopen import meaning\nopen import IlcModel\nopen import Changes\nopen import ChangeContexts\nopen import binding Type \u27e6_\u27e7Type\nopen import TotalTerms\n\n-- LIFTING terms into \u0394-Contexts\n\nlift-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) \u03c4\nlift-var this = that this\nlift-var (that x) = that (that (lift-var x))\n\nlift-var\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-var\u2032 \u2205 x = lift-var x\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) this = this\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) (that x) = that (lift-var\u2032 \u0393\u2032 x)\n\nlift-term\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-term\u2032 \u0393\u2032 (abs t) = abs (lift-term\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term\u2032 \u0393\u2032 (app t\u2081 t\u2082) = app (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082)\nlift-term\u2032 \u0393\u2032 (var x) = var (lift-var\u2032 \u0393\u2032 x)\nlift-term\u2032 \u0393\u2032 true = true\nlift-term\u2032 \u0393\u2032 false = false\nlift-term\u2032 \u0393\u2032 (if t\u2081 t\u2082 t\u2083) = if (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082) (lift-term\u2032 \u0393\u2032 t\u2083)\nlift-term\u2032 {.(\u0394-Context \u0393)} {.(\u0394-Type \u03c4)} \u0393\u2032 (\u0394 {\u0393} {\u03c4} t) = weakenMore t\n where\n open import Relation.Binary.PropositionalEquality using (sym)\n\n doWeakenMore : \u2200 \u0393prefix \u0393rest {\u03c4} \u2192\n Term (\u0393prefix \u22ce \u0393rest) (\u0394-Type \u03c4) \u2192\n Term (\u0393prefix \u22ce \u0394-Context \u0393rest) (\u0394-Type \u03c4)\n\n doWeakenMore \u0393prefix \u2205 t\u2081 = t\u2081\n doWeakenMore \u2205 (\u03c4\u2082 \u2022 \u0393rest) t\u2081 =\n weakenOne \u2205 (\u0394-Type \u03c4\u2082) (doWeakenMore (\u03c4\u2082 \u2022 \u2205) \u0393rest t\u2081)\n doWeakenMore \u0393prefix (\u03c4\u2082 \u2022 \u0393rest) {\u03c4} t\u2081 =\n weakenOne \u0393prefix (\u0394-Type \u03c4\u2082)\n (substTerm (sym (move-prefix \u0393prefix \u03c4\u2082 (\u0394-Context \u0393rest)))\n (doWeakenMore (\u0393prefix \u22ce (\u03c4\u2082 \u2022 \u2205)) \u0393rest\n (substTerm (move-prefix \u0393prefix \u03c4\u2082 \u0393rest) t\u2081)))\n\n prefix : \u2200 \u0393 \u2192 (\u0393\u2032 : Prefix (\u0394-Context \u0393)) \u2192 Context\n prefix \u0393 \u0393\u2032 = take (\u0394-Context \u0393) \u0393\u2032\n\n rest : \u2200 \u0393 \u2192 (\u0393\u2032 : Prefix (\u0394-Context \u0393)) \u2192 Context\n rest \u0393 \u0393\u2032 = drop (\u0394-Context \u0393) \u0393\u2032\n\n weakenMore2 : \u2200 \u0393 \u0393\u2032 {\u03c4} \u2192\n Term \u0393 \u03c4 \u2192\n Term (prefix \u0393 \u0393\u2032 \u22ce \u0394-Context (rest \u0393 \u0393\u2032)) (\u0394-Type \u03c4)\n weakenMore2 \u0393 \u0393\u2032 t =\n doWeakenMore (prefix \u0393 \u0393\u2032) (rest \u0393 \u0393\u2032) (\n substTerm (sym (take-drop (\u0394-Context \u0393) \u0393\u2032)) (\u0394 t))\n\n weakenMore : --\u2200 {\u0393 \u03c4} \u0393\u2032 \u2192\n Term \u0393 \u03c4 \u2192 Term (\u0394-Context\u2032 (\u0394-Context \u0393) \u0393\u2032) (\u0394-Type \u03c4)\n weakenMore t =\n substTerm\n (sym (take-\u22ce-\u0394-Context-drop-\u0394-Context\u2032 (\u0394-Context \u0393) \u0393\u2032))\n (weakenMore2 \u0393 \u0393\u2032 t)\n\nlift-term\u2032 {._} {_} _ (weakenOne _ _ {_} {._} _) = {!!}\n\nlift-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) \u03c4\nlift-term = lift-term\u2032 \u2205\n\n-- PROPERTIES of lift-term\n\nlift-var-ignore : \u2200 {\u0393 \u03c4} (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore \u03c1)\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) (that x) = lift-var-ignore \u03c1 x\n\nlift-var-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) (\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var\u2032 \u0393\u2032 x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-var-ignore\u2032 \u2205 \u03c1 x = lift-var-ignore \u03c1 x\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) (that x) = lift-var-ignore\u2032 \u0393\u2032 \u03c1 x\n\nlift-term-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) {\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term\u2032 \u0393\u2032 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-term-ignore\u2032 \u0393\u2032 (abs t) =\n ext (\u03bb v \u2192 lift-term-ignore\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term-ignore\u2032 \u0393\u2032 (app t\u2081 t\u2082) =\n \u2261-app (lift-term-ignore\u2032 \u0393\u2032 t\u2081) (lift-term-ignore\u2032 \u0393\u2032 t\u2082)\nlift-term-ignore\u2032 \u0393\u2032 (var x) = lift-var-ignore\u2032 \u0393\u2032 _ x\nlift-term-ignore\u2032 \u0393\u2032 true = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 false = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 {\u03c1} (if t\u2081 t\u2082 t\u2083)\n with \u27e6 lift-term\u2032 \u0393\u2032 t\u2081 \u27e7 \u03c1\n | \u27e6 t\u2081 \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\n | lift-term-ignore\u2032 \u0393\u2032 {\u03c1} t\u2081\n... | true | true | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2082\n... | false | false | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2083\nlift-term-ignore\u2032 \u0393\u2032 (\u0394 t) = {!!}\nlift-term-ignore\u2032 _ (weakenOne _ _ {_} {._} _) = {!!}\n\nlift-term-ignore : \u2200 {\u0393 \u03c4} {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore \u03c1)\nlift-term-ignore = lift-term-ignore\u2032 \u2205\n\n\n-- PROPERTIES of \u0394\n\n\u0394-abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192\n \u0394 (abs t) \u2248 abs (abs (\u0394 t))\n\u0394-abs t = ext (\u03bb \u03c1 \u2192 \u2261-refl)\n\n\u0394-app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192\n \u0394 (app t\u2081 t\u2082) \u2248 app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n\u0394-app t\u2081 t\u2082 = \u2248-sym (ext (\u03bb \u03c1 \u2192\n begin\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 lift-term t\u2082 \u27e7 \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 lift-term t\u2082 \u27e7 \u03c1))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))) x))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) x))\n (lift-term-ignore {\u03c1 = \u03c1} t\u2082) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff (\u27e6 t\u2081 \u27e7 (update \u03c1) x) (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (apply-diff (\u27e6 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 t\u2082 \u27e7 (update \u03c1))) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u220e)) where open \u2261-Reasoning\n\n-- SYMBOLIC DERIVATION\n\nderive-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-var this = this\nderive-var (that x) = that (that (derive-var x))\n\ndiff-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4)\ndiff-term = {!!}\n\napply-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 (\u0394-Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4\napply-term = {!!}\n\n_and_ : \u2200 {\u0393} \u2192 Term \u0393 bool \u2192 Term \u0393 bool \u2192 Term \u0393 bool\na and b = {!!}\n\n!_ : \u2200 {\u0393} \u2192 Term \u0393 bool \u2192 Term \u0393 bool\n! x = {!!}\n\nderive-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-term (abs t) = abs (abs (derive-term t))\nderive-term (app t\u2081 t\u2082) = app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\nderive-term (var x) = var (derive-var x)\nderive-term true = false\nderive-term false = false\nderive-term (if c t e) =\n if ((derive-term c) and (lift-term c))\n (diff-term (apply-term (derive-term e) (lift-term e)) (lift-term t))\n (if ((derive-term c) and (lift-term (! c)))\n (diff-term (apply-term (derive-term t) (lift-term t)) (lift-term e))\n (if (lift-term c)\n (derive-term t)\n (derive-term e)))\n\nderive-term (\u0394 t) = \u0394 (derive-term t)\nderive-term (weakenOne \u0393\u2081 \u03c4\u2082 {\u0393\u2083} t) =\n substTerm (\u0394-Context-\u22ce-expanded \u0393\u2081 \u03c4\u2082 \u0393\u2083)\n (weakenOne (\u0394-Context \u0393\u2081) (\u0394-Type \u03c4\u2082)\n (weakenOne (\u0394-Context \u0393\u2081) \u03c4\u2082\n (substTerm (\u0394-Context-\u22ce \u0393\u2081 \u0393\u2083)\n (derive-term t))))\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n \u0394 t \u2248 derive-term t\nderive-term-correct {\u0393} (abs t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct t)) \u27e9\n abs (abs (derive-term t))\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (abs t)\n \u220e where open \u2248-Reasoning\nderive-term-correct (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct t\u2081) \u2248-refl) (derive-term-correct t\u2082) \u27e9\n app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct (var x) = ext (\u03bb \u03c1 \u2192 derive-var-correct \u03c1 x)\nderive-term-correct true = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\nderive-term-correct (weakenOne _ _ t) = {!!}\n","old_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nimport Relation.Binary as B\n\nopen import Relation.Binary using\n (IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)\nimport Relation.Binary.EqReasoning as EqR\n\nopen import Relation.Nullary using (\u00ac_)\n\nopen import meaning\nopen import IlcModel\nopen import Changes\nopen import ChangeContexts\nopen import binding Type \u27e6_\u27e7Type\nopen import TotalTerms\n\n-- LIFTING terms into \u0394-Contexts\n\nlift-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) \u03c4\nlift-var this = that this\nlift-var (that x) = that (that (lift-var x))\n\nlift-var\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-var\u2032 \u2205 x = lift-var x\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) this = this\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) (that x) = that (lift-var\u2032 \u0393\u2032 x)\n\nlift-term\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-term\u2032 \u0393\u2032 (abs t) = abs (lift-term\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term\u2032 \u0393\u2032 (app t\u2081 t\u2082) = app (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082)\nlift-term\u2032 \u0393\u2032 (var x) = var (lift-var\u2032 \u0393\u2032 x)\nlift-term\u2032 \u0393\u2032 true = true\nlift-term\u2032 \u0393\u2032 false = false\nlift-term\u2032 \u0393\u2032 (if t\u2081 t\u2082 t\u2083) = if (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082) (lift-term\u2032 \u0393\u2032 t\u2083)\nlift-term\u2032 {.(\u0394-Context \u0393)} {.(\u0394-Type \u03c4)} \u0393\u2032 (\u0394 {\u0393} {\u03c4} t) = {!!}\nlift-term\u2032 {._} {_} _ (weakenOne _ _ {_} {._} _) = {!!}\n\nlift-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) \u03c4\nlift-term = lift-term\u2032 \u2205\n\n-- PROPERTIES of lift-term\n\nlift-var-ignore : \u2200 {\u0393 \u03c4} (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore \u03c1)\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) (that x) = lift-var-ignore \u03c1 x\n\nlift-var-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) (\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var\u2032 \u0393\u2032 x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-var-ignore\u2032 \u2205 \u03c1 x = lift-var-ignore \u03c1 x\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) (that x) = lift-var-ignore\u2032 \u0393\u2032 \u03c1 x\n\nlift-term-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) {\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term\u2032 \u0393\u2032 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-term-ignore\u2032 \u0393\u2032 (abs t) =\n ext (\u03bb v \u2192 lift-term-ignore\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term-ignore\u2032 \u0393\u2032 (app t\u2081 t\u2082) =\n \u2261-app (lift-term-ignore\u2032 \u0393\u2032 t\u2081) (lift-term-ignore\u2032 \u0393\u2032 t\u2082)\nlift-term-ignore\u2032 \u0393\u2032 (var x) = lift-var-ignore\u2032 \u0393\u2032 _ x\nlift-term-ignore\u2032 \u0393\u2032 true = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 false = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 {\u03c1} (if t\u2081 t\u2082 t\u2083)\n with \u27e6 lift-term\u2032 \u0393\u2032 t\u2081 \u27e7 \u03c1\n | \u27e6 t\u2081 \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\n | lift-term-ignore\u2032 \u0393\u2032 {\u03c1} t\u2081\n... | true | true | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2082\n... | false | false | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2083\nlift-term-ignore\u2032 \u0393\u2032 (\u0394 t) = {!!}\nlift-term-ignore\u2032 _ (weakenOne _ _ {_} {._} _) = {!!}\n\nlift-term-ignore : \u2200 {\u0393 \u03c4} {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore \u03c1)\nlift-term-ignore = lift-term-ignore\u2032 \u2205\n\n\n-- PROPERTIES of \u0394\n\n\u0394-abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192\n \u0394 (abs t) \u2248 abs (abs (\u0394 t))\n\u0394-abs t = ext (\u03bb \u03c1 \u2192 \u2261-refl)\n\n\u0394-app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192\n \u0394 (app t\u2081 t\u2082) \u2248 app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n\u0394-app t\u2081 t\u2082 = \u2248-sym (ext (\u03bb \u03c1 \u2192\n begin\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 lift-term t\u2082 \u27e7 \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 lift-term t\u2082 \u27e7 \u03c1))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))) x))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) x))\n (lift-term-ignore {\u03c1 = \u03c1} t\u2082) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff (\u27e6 t\u2081 \u27e7 (update \u03c1) x) (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (apply-diff (\u27e6 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 t\u2082 \u27e7 (update \u03c1))) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u220e)) where open \u2261-Reasoning\n\n-- SYMBOLIC DERIVATION\n\nderive-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-var this = this\nderive-var (that x) = that (that (derive-var x))\n\ndiff-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4)\ndiff-term = {!!}\n\napply-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 (\u0394-Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4\napply-term = {!!}\n\n_and_ : \u2200 {\u0393} \u2192 Term \u0393 bool \u2192 Term \u0393 bool \u2192 Term \u0393 bool\na and b = {!!}\n\n!_ : \u2200 {\u0393} \u2192 Term \u0393 bool \u2192 Term \u0393 bool\n! x = {!!}\n\nderive-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-term (abs t) = abs (abs (derive-term t))\nderive-term (app t\u2081 t\u2082) = app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\nderive-term (var x) = var (derive-var x)\nderive-term true = false\nderive-term false = false\nderive-term (if c t e) =\n if ((derive-term c) and (lift-term c))\n (diff-term (apply-term (derive-term e) (lift-term e)) (lift-term t))\n (if ((derive-term c) and (lift-term (! c)))\n (diff-term (apply-term (derive-term t) (lift-term t)) (lift-term e))\n (if (lift-term c)\n (derive-term t)\n (derive-term e)))\n\nderive-term (\u0394 t) = \u0394 (derive-term t)\nderive-term (weakenOne \u0393\u2081 \u03c4\u2082 {\u0393\u2083} t) =\n substTerm (\u0394-Context-\u22ce-expanded \u0393\u2081 \u03c4\u2082 \u0393\u2083)\n (weakenOne (\u0394-Context \u0393\u2081) (\u0394-Type \u03c4\u2082)\n (weakenOne (\u0394-Context \u0393\u2081) \u03c4\u2082\n (substTerm (\u0394-Context-\u22ce \u0393\u2081 \u0393\u2083)\n (derive-term t))))\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n \u0394 t \u2248 derive-term t\nderive-term-correct {\u0393} (abs t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct t)) \u27e9\n abs (abs (derive-term t))\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (abs t)\n \u220e where open \u2248-Reasoning\nderive-term-correct (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct t\u2081) \u2248-refl) (derive-term-correct t\u2082) \u27e9\n app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct (var x) = ext (\u03bb \u03c1 \u2192 derive-var-correct \u03c1 x)\nderive-term-correct true = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\nderive-term-correct (weakenOne _ _ t) = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f1c2df98ed89a3de0f4b42407cb950eded1c5ed4","subject":"Increase overloading and confusion.","message":"Increase overloading and confusion.\n\nOld-commit-hash: df682306832e5b833f837c92dec32b3c428a7ab9\n","repos":"inc-lc\/ilc-agda","old_file":"total.agda","new_file":"total.agda","new_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Note that \u0394 is *not* the same as the \u2202 operator in\n-- definition\/intro.tex. See discussion at:\n--\n-- https:\/\/github.com\/ps-mr\/ilc\/pull\/34#discussion_r4290325\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Changes\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\nopen import Denotational.ValidChanges\n\nopen import Changes\nopen import ChangeContexts\nopen import ChangeContextLifting\nopen import PropsDelta\nopen import SymbolicDerivation\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 (t : Term \u0393\u2081 \u03c4) \u2192\n \u0394 {{\u0393\u2032}} t \u2248 derive-term {{\u0393\u2032}} t\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) =\n begin\n \u0394 {{\u0393\u2032}} (abs t)\n \u2248\u27e8 \u0394-abs {{\u0393\u2032}} t \u27e9\n abs (abs (\u0394 {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t)) \u27e9\n abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t))\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (abs t)\n \u220e where\n open \u2248-Reasoning\n \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (app t\u2081 t\u2082) =\n begin\n \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app {{\u0393\u2032}} t\u2081 t\u2082 \u27e9\n app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct {{\u0393\u2032}} t\u2081) \u2248-refl) (derive-term-correct {{\u0393\u2032}} t\u2082) \u27e9\n app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (var x) = ext-t (\u03bb \u03c1 \u2192\n begin\n \u27e6 \u0394 {{\u0393\u2032}} (var x) \u27e7 \u03c1\n \u2261\u27e8\u27e9\n diff\n (\u27e6 x \u27e7 (update (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n (\u27e6 x \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n \u2261\u27e8 derive-var-correct {\u0393\u2081} (\u27e6 \u0393\u2032 \u27e7 \u03c1) x \u27e9\n \u27e6 derive-var x \u27e7 (\u27e6 \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 sym (lift-sound \u0393\u2032 (derive-var x) \u03c1) \u27e9\n \u27e6 lift \u0393\u2032 (derive-var x) \u27e7 \u03c1\n \u220e) where open \u2261-Reasoning\nderive-term-correct true = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct {{\u0393\u2032}} (if t\u2081 t\u2082 t\u2083) =\n begin\n \u0394 (if t\u2081 t\u2082 t\u2083)\n \u2248\u27e8 \u0394-if {{\u0393\u2032}} t\u2081 t\u2082 t\u2083 \u27e9\n if (\u0394 t\u2081)\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (diff-term (apply-term (\u0394 {{\u0393\u2032}} t\u2083) (lift-term {{\u0393\u2032}} t\u2083)) (lift-term {{\u0393\u2032}} t\u2082))\n (diff-term (apply-term (\u0394 {{\u0393\u2032}} t\u2082) (lift-term {{\u0393\u2032}} t\u2082)) (lift-term {{\u0393\u2032}} t\u2083)))\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (\u0394 {{\u0393\u2032}} t\u2082)\n (\u0394 {{\u0393\u2032}} t\u2083))\n \u2248\u27e8 \u2248-if (derive-term-correct {{\u0393\u2032}} t\u2081)\n (\u2248-if (\u2248-refl)\n (\u2248-diff-term (\u2248-apply-term (derive-term-correct {{\u0393\u2032}} t\u2083) \u2248-refl) \u2248-refl)\n (\u2248-diff-term (\u2248-apply-term (derive-term-correct {{\u0393\u2032}} t\u2082) \u2248-refl) \u2248-refl))\n (\u2248-if (\u2248-refl)\n (derive-term-correct {{\u0393\u2032}} t\u2082)\n (derive-term-correct {{\u0393\u2032}} t\u2083)) \u27e9\n if (derive-term {{\u0393\u2032}} t\u2081)\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (diff-term (apply-term (derive-term {{\u0393\u2032}} t\u2083) (lift-term {{\u0393\u2032}} t\u2083)) (lift-term {{\u0393\u2032}} t\u2082))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} t\u2082) (lift-term {{\u0393\u2032}} t\u2082)) (lift-term {{\u0393\u2032}} t\u2083)))\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (derive-term {{\u0393\u2032}} t\u2082)\n (derive-term {{\u0393\u2032}} t\u2083))\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (if t\u2081 t\u2082 t\u2083)\n \u220e where open \u2248-Reasoning\n\nderive-term-correct {{\u0393\u2032}} (\u0394 {{\u0393\u2033}} t) = \u2248-\u0394 {{\u0393\u2032}} (derive-term-correct {{\u0393\u2033}} t)\n","old_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Note that \u0394 is *not* the same as the \u2202 operator in\n-- definition\/intro.tex. See discussion at:\n--\n-- https:\/\/github.com\/ps-mr\/ilc\/pull\/34#discussion_r4290325\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Changes\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\nopen import Denotational.ValidChanges\n\nopen import Changes\nopen import ChangeContexts\nopen import ChangeContextLifting\nopen import PropsDelta\nopen import SymbolicDerivation\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 (t : Term \u0393\u2081 \u03c4) \u2192\n \u0394 {{\u0393\u2032}} t \u2248 derive-term {{\u0393\u2032}} t\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) =\n begin\n \u0394 {{\u0393\u2032}} (abs t)\n \u2248\u27e8 \u0394-abs {{\u0393\u2032}} t \u27e9\n abs (abs (\u0394 {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t)) \u27e9\n abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t))\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (abs t)\n \u220e where\n open \u2248-Reasoning\n \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (app t\u2081 t\u2082) =\n begin\n \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app {{\u0393\u2032}} t\u2081 t\u2082 \u27e9\n app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct {{\u0393\u2032}} t\u2081) \u2248-refl) (derive-term-correct {{\u0393\u2032}} t\u2082) \u27e9\n app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (var x) = ext-t (\u03bb \u03c1 \u2192\n begin\n \u27e6 \u0394 {{\u0393\u2032}} (var x) \u27e7 \u03c1\n \u2261\u27e8\u27e9\n diff\n (\u27e6 x \u27e7 (update (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n (\u27e6 x \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n \u2261\u27e8 derive-var-correct {\u0393\u2081} (\u27e6 \u0393\u2032 \u27e7 \u03c1) x \u27e9\n \u27e6 derive-var x \u27e7Var (\u27e6 \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 sym (lift-sound \u0393\u2032 (derive-var x) \u03c1) \u27e9\n \u27e6 lift \u0393\u2032 (derive-var x) \u27e7Var \u03c1\n \u220e) where open \u2261-Reasoning\nderive-term-correct true = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct {{\u0393\u2032}} (if t\u2081 t\u2082 t\u2083) =\n begin\n \u0394 (if t\u2081 t\u2082 t\u2083)\n \u2248\u27e8 \u0394-if {{\u0393\u2032}} t\u2081 t\u2082 t\u2083 \u27e9\n if (\u0394 t\u2081)\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (diff-term (apply-term (\u0394 {{\u0393\u2032}} t\u2083) (lift-term {{\u0393\u2032}} t\u2083)) (lift-term {{\u0393\u2032}} t\u2082))\n (diff-term (apply-term (\u0394 {{\u0393\u2032}} t\u2082) (lift-term {{\u0393\u2032}} t\u2082)) (lift-term {{\u0393\u2032}} t\u2083)))\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (\u0394 {{\u0393\u2032}} t\u2082)\n (\u0394 {{\u0393\u2032}} t\u2083))\n \u2248\u27e8 \u2248-if (derive-term-correct {{\u0393\u2032}} t\u2081)\n (\u2248-if (\u2248-refl)\n (\u2248-diff-term (\u2248-apply-term (derive-term-correct {{\u0393\u2032}} t\u2083) \u2248-refl) \u2248-refl)\n (\u2248-diff-term (\u2248-apply-term (derive-term-correct {{\u0393\u2032}} t\u2082) \u2248-refl) \u2248-refl))\n (\u2248-if (\u2248-refl)\n (derive-term-correct {{\u0393\u2032}} t\u2082)\n (derive-term-correct {{\u0393\u2032}} t\u2083)) \u27e9\n if (derive-term {{\u0393\u2032}} t\u2081)\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (diff-term (apply-term (derive-term {{\u0393\u2032}} t\u2083) (lift-term {{\u0393\u2032}} t\u2083)) (lift-term {{\u0393\u2032}} t\u2082))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} t\u2082) (lift-term {{\u0393\u2032}} t\u2082)) (lift-term {{\u0393\u2032}} t\u2083)))\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (derive-term {{\u0393\u2032}} t\u2082)\n (derive-term {{\u0393\u2032}} t\u2083))\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (if t\u2081 t\u2082 t\u2083)\n \u220e where open \u2248-Reasoning\n\nderive-term-correct {{\u0393\u2032}} (\u0394 {{\u0393\u2033}} t) = \u2248-\u0394 {{\u0393\u2032}} (derive-term-correct {{\u0393\u2033}} t)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f7dfdf9faeb8aa7389195922929e02b45dd68aed","subject":"ANormalUntyped: WIP progress","message":"ANormalUntyped: WIP progress\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/ANormalUntyped.agda","new_file":"Thesis\/ANormalUntyped.agda","new_contents":"{-# OPTIONS --allow-unsolved-metas #-}\nmodule Thesis.ANormalUntyped where\n\nopen import Data.Empty\nopen import Data.Unit using (\u22a4)\nopen import Data.Product\nopen import Data.Nat\nimport Data.Integer.Base as I\nopen I using (\u2124)\nopen import Data.Integer.Base using (\u2124)\nopen import Relation.Binary.PropositionalEquality\n\n{- Typed deBruijn indexes for untyped languages. -}\n\n-- Using a record gives an eta rule saying that all types are equal.\nrecord Type : Set where\n constructor Uni\n\nrecord DType : Set where\n constructor DUni\n\nopen import Base.Syntax.Context Type public\nimport Base.Syntax.Context DType as DC\n\ndata Term (\u0393 : Context) : Set where\n var : (x : Var \u0393 Uni) \u2192\n Term \u0393\n lett : (f : Var \u0393 Uni) \u2192 (x : Var \u0393 Uni) \u2192 Term (Uni \u2022 \u0393) \u2192 Term \u0393\n\n\u0394\u0394 : Context \u2192 DC.Context\n\u0394\u0394 \u2205 = \u2205\n\u0394\u0394 (\u03c4 \u2022 \u0393) = DUni \u2022 \u0394\u0394 \u0393\n\nderive-dvar : \u2200 {\u0394} \u2192 (x : Var \u0394 Uni) \u2192 DC.Var (\u0394\u0394 \u0394) DUni\nderive-dvar this = DC.this\nderive-dvar (that x) = DC.that (derive-dvar x)\n\ndata DTerm : (\u0394 : Context) \u2192 Set where\n dvar : \u2200 {\u0394} (x : DC.Var (\u0394\u0394 \u0394) DUni) \u2192\n DTerm \u0394\n dlett : \u2200 {\u0394} \u2192\n (f : Var \u0394 Uni) \u2192\n (x : Var \u0394 Uni) \u2192\n (t : Term (Uni \u2022 \u0394)) \u2192\n (df : DC.Var (\u0394\u0394 \u0394) DUni) \u2192\n (dx : DC.Var (\u0394\u0394 \u0394) DUni) \u2192\n (dt : DTerm (Uni \u2022 \u0394)) \u2192\n DTerm \u0394\n\nderive-dterm : \u2200 {\u0394} \u2192 (t : Term \u0394) \u2192 DTerm \u0394\nderive-dterm (var x) = dvar (derive-dvar x)\nderive-dterm (lett f x t) =\n dlett f x t (derive-dvar f) (derive-dvar x) (derive-dterm t)\n\n{-\nderiveC \u0394 (lett f x t) = dlett df x dx\n-}\n\n-- data \u0394CTerm (\u0393 : Context) (\u03c4 : Type) (\u0394 : Context) : Set where\n-- data \u0394CTerm (\u0393 : Context) (\u03c4 : Type) (\u0394 : Context) : Set where\n-- cvar : (x : Var \u0393 \u03c4) \u0394 \u2192\n-- \u0394CTerm \u0393 \u03c4 \u0394\n-- clett : \u2200 {\u03c3 \u03c4\u2081 \u03ba} \u2192 (f : Var \u0393 (\u03c3 \u21d2 \u03c4\u2081)) \u2192 (x : Var \u0393 \u03c3) \u2192\n-- \u0394CTerm (\u03c4\u2081 \u2022 \u0393) \u03c4 (? \u2022 \u0394) \u2192\n-- \u0394CTerm \u0393 \u03c4 \u0394\n\nweaken-term : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u2192\n Term \u0393\u2082\nweaken-term \u0393\u2081\u227c\u0393\u2082 (var x) = var (weaken-var \u0393\u2081\u227c\u0393\u2082 x)\nweaken-term \u0393\u2081\u227c\u0393\u2082 (lett f x t) = lett (weaken-var \u0393\u2081\u227c\u0393\u2082 f) (weaken-var \u0393\u2081\u227c\u0393\u2082 x) (weaken-term (keep _ \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\n-- I don't necessarily recommend having a separate syntactic category for\n-- functions, but we should prove a fundamental lemma for them too, somehow.\n-- I'll probably end up with some ANF allowing lambdas to do the semantics.\ndata Fun (\u0393 : Context) : Set where\n term : Term \u0393 \u2192 Fun \u0393\n abs : \u2200 {\u03c3} \u2192 Fun (\u03c3 \u2022 \u0393) \u2192 Fun \u0393\n\ndata DFun (\u0394 : Context) : Set where\n dterm : DTerm \u0394 \u2192 DFun \u0394\n dabs : DFun (Uni \u2022 \u0394) \u2192 DFun \u0394\n\nderive-dfun : \u2200 {\u0394} \u2192 (t : Fun \u0394) \u2192 DFun \u0394\nderive-dfun (term t) = dterm (derive-dterm t)\nderive-dfun (abs f) = dabs (derive-dfun f)\n\nweaken-fun : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Fun \u0393\u2081 \u2192\n Fun \u0393\u2082\nweaken-fun \u0393\u2081\u227c\u0393\u2082 (term x) = term (weaken-term \u0393\u2081\u227c\u0393\u2082 x)\nweaken-fun \u0393\u2081\u227c\u0393\u2082 (abs f) = abs (weaken-fun (keep _ \u2022 \u0393\u2081\u227c\u0393\u2082) f)\n\ndata Val : Type \u2192 Set\ndata DVal : DType \u2192 Set\n-- data Val (\u03c4 : Type) : Set\n\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\nimport Base.Denotation.Environment DType DVal as D\n\n-- data Val (\u03c4 : Type) where\ndata Val where\n closure : \u2200 {\u0393} \u2192 (t : Fun (Uni \u2022 \u0393)) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val Uni\n intV : \u2200 (n : \u2115) \u2192 Val Uni\n\ndata DVal where\n dclosure : \u2200 {\u0393} \u2192 (dt : DFun (Uni \u2022 \u0393)) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 (d\u03c1 : D.\u27e6 \u0394\u0394 \u0393 \u27e7Context) \u2192 DVal DUni\n dintV : \u2200 (n : \u2124) \u2192 DVal DUni\n\nCh\u0394 : \u2200 (\u0394 : Context) \u2192 Set\nCh\u0394 \u0394 = D.\u27e6 \u0394\u0394 \u0394 \u27e7Context\n\n-- \u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 \u27e6 \u03c4 \u27e7Type\n-- \u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7Var \u03c1\n-- \u27e6 lett f x t \u27e7Term \u03c1 = \u27e6 t \u27e7Term (\u27e6 f \u27e7Var \u03c1 (\u27e6 x \u27e7Var \u03c1) \u2022 \u03c1)\n\n-- XXX separate syntax is a bit dangerous. Also, do I want to be so accurate relative to the original model?\ndata _\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) : Term \u0393 \u2192 \u2115 \u2192 Val Uni \u2192 Set\n\ndata _F\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) : Fun \u0393 \u2192 \u2115 \u2192 Val Uni \u2192 Set where\n abs : \u2200 {t : Fun (Uni \u2022 \u0393)} \u2192\n \u03c1 F\u22a2 abs t \u2193[ 0 ] closure t \u03c1\n term : \u2200 {v} n (t : Term \u0393) \u2192\n \u03c1 \u22a2 t \u2193[ n ] v \u2192\n \u03c1 F\u22a2 term t \u2193[ n ] v\n\ndata _\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) where\n var : \u2200 (x : Var \u0393 Uni) \u2192\n \u03c1 \u22a2 var x \u2193[ 0 ] (\u27e6 x \u27e7Var \u03c1)\n lett : \u2200 n1 n2 {\u0393' \u03c1\u2032 v1 v2 v3} {f x t t'} \u2192\n \u03c1 \u22a2 var f \u2193[ 0 ] closure {\u0393'} t' \u03c1\u2032 \u2192\n \u03c1 \u22a2 var x \u2193[ 0 ] v1 \u2192\n (v1 \u2022 \u03c1\u2032) F\u22a2 t' \u2193[ n1 ] v2 \u2192\n (v2 \u2022 \u03c1) \u22a2 t \u2193[ n2 ] v3 \u2192\n \u03c1 \u22a2 lett f x t \u2193[ suc (suc (n1 + n2)) ] v3\n -- lit : \u2200 n \u2192\n -- \u03c1 \u22a2 const (lit n) \u2193[ 0 ] intV n\n\n-- data _D_\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : D.\u27e6 \u0394\u0394 \u0393 \u27e7Context) : DTerm \u0393 \u2192 \u2115 \u2192 DVal DUni \u2192 Set where\n-- dvar : \u2200 (x : DC.Var (\u0394\u0394 \u0393) DUni) \u2192\n-- \u03c1 D d\u03c1 \u22a2 dvar x \u2193[ 0 ] (D.\u27e6 x \u27e7Var d\u03c1)\n-- dlett : \u2200 n1 n2 n3 n4 {\u0393' \u03c1\u2032 \u03c1'' d\u03c1' v1 v2 v3 dv1 dv2 dv3} {f x t df dx dt t' dt'} \u2192\n-- \u03c1 \u22a2 var f \u2193[ 0 ] closure {\u0393'} t' \u03c1\u2032 \u2192\n-- \u03c1 \u22a2 var x \u2193[ 0 ] v1 \u2192\n-- (v1 \u2022 \u03c1\u2032) F\u22a2 t' \u2193[ n1 ] v2 \u2192\n-- (v2 \u2022 \u03c1) \u22a2 t \u2193[ n2 ] v3 \u2192\n-- -- With a valid input \u03c1' and \u03c1'' coincide? Varies among plausible validity\n-- -- definitions.\n-- \u03c1 D d\u03c1 \u22a2 dvar df \u2193[ 0 ] dclosure {\u0393'} dt' \u03c1'' d\u03c1' \u2192\n-- \u03c1 D d\u03c1 \u22a2 dvar dx \u2193[ 0 ] dv1 \u2192\n-- (v1 \u2022 \u03c1'') D (dv1 \u2022 d\u03c1') F\u22a2 dt' \u2193[ n3 ] dv2 \u2192\n-- (v2 \u2022 \u03c1) D (dv2 \u2022 d\u03c1) \u22a2 dt \u2193[ n4 ] dv3 \u2192\n-- \u03c1 D d\u03c1 \u22a2 dlett f x t df dx dt \u2193[ suc (suc (n1 + n2)) ] dv3\n\n\n-- Do I need to damn count steps here? No.\n\ndata _D_\u22a2_\u2193_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) : DTerm \u0393 \u2192 DVal DUni \u2192 Set\n\ndata _D_F\u22a2_\u2193_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) : DFun \u0393 \u2192 DVal DUni \u2192 Set where\n dabs : \u2200 {dt : DFun (Uni \u2022 \u0393)} \u2192\n \u03c1 D d\u03c1 F\u22a2 dabs dt \u2193 dclosure dt \u03c1 d\u03c1\n dterm : \u2200 {dv} (dt : DTerm \u0393) \u2192\n \u03c1 D d\u03c1 \u22a2 dt \u2193 dv \u2192\n \u03c1 D d\u03c1 F\u22a2 dterm dt \u2193 dv\n\ndata _D_\u22a2_\u2193_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) where\n dvar : \u2200 (x : DC.Var (\u0394\u0394 \u0393) DUni) \u2192\n \u03c1 D d\u03c1 \u22a2 dvar x \u2193 (D.\u27e6 x \u27e7Var d\u03c1)\n dlett : \u2200 n1 n2 {\u0393' \u03c1\u2032 \u03c1'' d\u03c1' v1 v2 v3 dv1 dv2 dv3} {f x t df dx dt t' dt'} \u2192\n \u03c1 \u22a2 var f \u2193[ 0 ] closure {\u0393'} t' \u03c1\u2032 \u2192\n \u03c1 \u22a2 var x \u2193[ 0 ] v1 \u2192\n (v1 \u2022 \u03c1\u2032) F\u22a2 t' \u2193[ n1 ] v2 \u2192\n (v2 \u2022 \u03c1) \u22a2 t \u2193[ n2 ] v3 \u2192\n -- With a valid input \u03c1' and \u03c1'' coincide? Varies among plausible validity\n -- definitions.\n \u03c1 D d\u03c1 \u22a2 dvar df \u2193 dclosure {\u0393'} dt' \u03c1'' d\u03c1' \u2192\n \u03c1 D d\u03c1 \u22a2 dvar dx \u2193 dv1 \u2192\n (v1 \u2022 \u03c1'') D (dv1 \u2022 d\u03c1') F\u22a2 dt' \u2193 dv2 \u2192\n (v2 \u2022 \u03c1) D (dv2 \u2022 d\u03c1) \u22a2 dt \u2193 dv3 \u2192\n \u03c1 D d\u03c1 \u22a2 dlett f x t df dx dt \u2193 dv3\n\nopen import Data.Nat.Properties\nopen import Data.Nat.Properties.Simple\nopen import Relation.Binary hiding (_\u21d2_)\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\nsuc\u2238 : \u2200 m n \u2192 n \u2264 m \u2192 suc (m \u2238 n) \u2261 suc m \u2238 n\nsuc\u2238 m zero z\u2264n = refl\nsuc\u2238 (suc m) (suc n) (s\u2264s n\u2264m) = suc\u2238 m n n\u2264m\n\nsuc\u2238suc : \u2200 m n \u2192 n < m \u2192 suc (m \u2238 suc n) \u2261 m \u2238 n\nsuc\u2238suc (suc m) zero (s\u2264s n Derivative f (apply df).\n -- Now, we try to prove that if Derivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil {{CA}} x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil {{CA}} x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n derivative-is-nil-alternative : \u2200 {f : A \u2192 B} df \u2192\n (Derivative-f-df : Derivative f df) \u2192 DerivativeAsChange df Derivative-f-df \u2259 nil f\n derivative-is-nil-alternative df Derivative-f-df = derivative-is-nil (DerivativeAsChange df Derivative-f-df) Derivative-f-df\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 Derivative f (apply df) \u2192 Derivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 \u2259-sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n\n -- This is Lemma 2.5 in the paper. Note that the statement in the paper uses\n -- (incorrectly) normal equality instead of delta-observational equivalence.\n deriv-zero :\n (f : A \u2192 B) \u2192 (df : (a : A) (da : \u0394 a) \u2192 \u0394 (f a)) \u2192 Derivative f df \u2192\n \u2200 v \u2192 df v (nil {{CA}} v) \u2259 nil {{CB}} (f v)\n deriv-zero f df proof v = doe lemma\n where\n open \u2261-Reasoning\n lemma : f v \u229e df v (nil v) \u2261 f v \u229e nil {{CB}} (f v)\n lemma =\n begin\n f v \u229e df v (nil {{CA}} v)\n \u2261\u27e8 proof v (nil {{CA}} v) \u27e9\n f (v \u229e (nil {{CA}} v))\n \u2261\u27e8 cong f (update-nil {{CA}} v) \u27e9\n f v\n \u2261\u27e8 sym (update-nil {{CB}} (f v)) \u27e9\n f v \u229e nil {{CB}} (f v)\n \u220e\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\nopen import Function\n\nopen import Base.Change.Equivalence.Base public\nopen import Base.Change.Equivalence.EqReasoning public\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- By update-nil, if dx = nil x, then x \u229e dx \u2261 x.\n -- As a consequence, if dx \u2259 nil x, then x \u229e dx \u2261 x\n nil-is-\u229e-unit : \u2200 (dx : \u0394 {{ca}} x) \u2192 dx \u2259 nil x \u2192 x \u229e dx \u2261 x\n nil-is-\u229e-unit dx dx\u2259nil-x =\n begin\n x \u229e dx\n \u2261\u27e8 proof dx\u2259nil-x \u27e9\n x \u229e (nil {{ca}} x)\n \u2261\u27e8 update-nil {{ca}} x \u27e9\n x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Here we prove the inverse:\n \u229e-unit-is-nil : \u2200 (dx : \u0394 {{ca}} x) \u2192 x \u229e dx \u2261 x \u2192 _\u2259_ {{ca}} dx (nil {{ca}} x)\n \u229e-unit-is-nil dx x\u229edx\u2261x = doe $\n begin\n x \u229e dx\n \u2261\u27e8 x\u229edx\u2261x \u27e9\n x\n \u2261\u27e8 sym (update-nil {{ca}} x) \u27e9\n x \u229e nil {{ca}} x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Let's show that nil x is d.o.e. to x \u229f x\n nil-x-is-x\u229fx : nil x \u2259 x \u229f x\n nil-x-is-x\u229fx = \u2259-sym (\u229e-unit-is-nil (x \u229f x) (update-diff {{ca}} x x))\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n -- This results pairs with update-diff.\n diff-update : \u2200 {dx : \u0394 {{ca}} x} \u2192 (x \u229e dx) \u229f x \u2259 dx\n diff-update {dx} = doe lemma\n where\n lemma : _\u229e_ {{ca}} x (x \u229e dx \u229f x) \u2261 x \u229e dx\n lemma = update-diff {{ca}} (x \u229e dx) x\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization; DerivativeAsChange)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n _\u2259_ {{changeAlgebra}} df\u2081 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = doe lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 \u2259-cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 \u2259-cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n open import Postulate.Extensionality\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} df-x-dx\u2259dg-x-dx = doe lemma\u2082\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 x dx = proof $ df-x-dx\u2259dg-x-dx x dx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n\n -- You could think that the function should relate equivalent changes, but\n -- that's a stronger hypothesis, which doesn't give you extra guarantees. But\n -- here's the statement and proof, for completeness:\n\n delta-ext\u2082 : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx\u2081 dx\u2082 \u2192 _\u2259_ {{CA}} dx\u2081 dx\u2082 \u2192 apply df x dx\u2081 \u2259 apply dg x dx\u2082) \u2192 df \u2259 dg\n delta-ext\u2082 {f} {df} {dg} df-x-dx\u2259dg-x-dx = delta-ext (\u03bb x dx \u2192 df-x-dx\u2259dg-x-dx x dx dx \u2259-refl)\n\n -- We know that Derivative f (apply (nil f)) (by nil-is-derivative).\n -- That is, df = nil f -> Derivative f (apply df).\n -- Now, we try to prove that if Derivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil {{CA}} x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil {{CA}} x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n derivative-is-nil-alternative : \u2200 {f : A \u2192 B} df \u2192\n (Derivative-f-df : Derivative f df) \u2192 DerivativeAsChange df Derivative-f-df \u2259 nil f\n derivative-is-nil-alternative df Derivative-f-df = derivative-is-nil (DerivativeAsChange df Derivative-f-df) Derivative-f-df\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 Derivative f (apply df) \u2192 Derivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 \u2259-sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n\n -- This is Lemma 2.5 in the paper. Note that the statement in the paper uses\n -- (incorrectly) normal equality instead of delta-observational equivalence.\n deriv-zero :\n (f : A \u2192 B) \u2192 (df : (a : A) (da : \u0394 a) \u2192 \u0394 (f a)) \u2192 Derivative f df \u2192\n \u2200 v \u2192 df v (nil {{CA}} v) \u2259 nil {{CB}} (f v)\n deriv-zero f df proof v = doe lemma\n where\n open \u2261-Reasoning\n lemma : f v \u229e df v (nil v) \u2261 f v \u229e nil {{CB}} (f v)\n lemma =\n begin\n f v \u229e df v (nil {{CA}} v)\n \u2261\u27e8 proof v (nil {{CA}} v) \u27e9\n f (v \u229e (nil {{CA}} v))\n \u2261\u27e8 cong f (update-nil {{CA}} v) \u27e9\n f v\n \u2261\u27e8 sym (update-nil {{CB}} (f v)) \u27e9\n f v \u229e nil {{CB}} (f v)\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"757995f811b789374410d560566e8717a68f1e24","subject":"Revert \"Show how badly reasoning for \u2259 and \u2261 combine\"","message":"Revert \"Show how badly reasoning for \u2259 and \u2261 combine\"\n\nThis reverts commit 24faa2c5edd91d1db64977d5124ef40e01de433f. The commit\nshowed how badly something worked, so let's leave in the old version for\nnow.\n\nOld-commit-hash: 9f5f49cf0319ba809356655a204ec83725dfceb4\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Equivalence.agda","new_file":"Base\/Change\/Equivalence.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\nopen import Function\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n\n -- To avoid unification problems, use a one-field record.\n record _\u2259_ dx dy : Set a where\n -- doe = Delta-Observational Equivalence.\n constructor doe\n field\n proof : x \u229e dx \u2261 x \u229e dy\n\n open _\u2259_ public\n\n -- Same priority as \u2261\n infix 4 _\u2259_\n\n open import Relation.Binary\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = doe refl\n\n \u2259-sym : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-sym \u2259 = doe $ sym $ proof \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = doe $ trans (proof \u2259\u2081) (proof \u2259\u2082)\n\n -- That's standard congruence applied to \u2259\n \u2259-cong : \u2200 {b} {B : Set b}\n (f : A \u2192 B) {dx dy} \u2192 dx \u2259 dy \u2192 f (x \u229e dx) \u2261 f (x \u229e dy)\n \u2259-cong f da\u2259db = cong f $ proof da\u2259db\n\n \u2259-isEquivalence : IsEquivalence (_\u2259_)\n \u2259-isEquivalence = record\n { refl = \u2259-refl\n ; sym = \u2259-sym\n ; trans = \u2259-trans\n }\n\n \u2259-setoid : Setoid \u2113 a\n \u2259-setoid = record\n { Carrier = \u0394 x\n ; _\u2248_ = _\u2259_\n ; isEquivalence = \u2259-isEquivalence\n }\n\n\n ------------------------------------------------------------------------\n -- Convenient syntax for equational reasoning\n\n import Relation.Binary.EqReasoning as EqR\n\n module \u2259-Reasoning where\n open EqR \u2259-setoid public\n renaming (_\u2248\u27e8_\u27e9_ to _\u2259\u27e8_\u27e9_)\n\n -- By update-nil, if dx = nil x, then x \u229e dx \u2261 x.\n -- As a consequence, if dx \u2259 nil x, then x \u229e dx \u2261 x\n nil-is-\u229e-unit : \u2200 dx \u2192 dx \u2259 nil x \u2192 x \u229e dx \u2261 x\n nil-is-\u229e-unit dx dx\u2259nil-x =\n begin\n x \u229e dx\n \u2261\u27e8 proof dx\u2259nil-x \u27e9\n x \u229e (nil x)\n \u2261\u27e8 update-nil x \u27e9\n x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Here we prove the inverse:\n \u229e-unit-is-nil : \u2200 dx \u2192 x \u229e dx \u2261 x \u2192 dx \u2259 nil x\n \u229e-unit-is-nil dx x\u229edx\u2261x = doe $\n begin\n x \u229e dx\n \u2261\u27e8 x\u229edx\u2261x \u27e9\n x\n \u2261\u27e8 sym (update-nil x) \u27e9\n x \u229e nil x\n \u220e\n where\n open \u2261-Reasoning\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n -- This results pairs with update-diff.\n diff-update : \u2200 {dx} \u2192 (x \u229e dx) \u229f x \u2259 dx\n diff-update {dx} = doe lemma\n where\n lemma : x \u229e (x \u229e dx \u229f x) \u2261 x \u229e dx\n lemma = update-diff (x \u229e dx) x\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = doe lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 \u2259-cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 \u2259-cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n open import Postulate.Extensionality\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} df-x-dx\u2259dg-x-dx = doe lemma\u2082\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 x dx = proof $ df-x-dx\u2259dg-x-dx x dx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n\n -- We know that Derivative f (apply (nil f)) (by nil-is-derivative).\n -- That is, df = nil f -> Derivative f (apply df).\n -- Now, we try to prove that if Derivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 Derivative f (apply df) \u2192 Derivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 \u2259-sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n -- Unused, but just to test that inference works.\n lemma : nil f \u2259 dg\n lemma = \u2259-sym (derivative-is-nil dg fdg)\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\nopen import Function\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n\n -- To avoid unification problems, use a one-field record.\n record _\u2259_ dx dy : Set a where\n -- doe = Delta-Observational Equivalence.\n constructor doe\n field\n proof : x \u229e dx \u2261 x \u229e dy\n\n open _\u2259_ public\n\n -- Same priority as \u2261\n infix 4 _\u2259_\n\n open import Relation.Binary\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = doe refl\n\n \u2259-sym : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-sym \u2259 = doe $ sym $ proof \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = doe $ trans (proof \u2259\u2081) (proof \u2259\u2082)\n\n -- That's standard congruence applied to \u2259\n \u2259-cong : \u2200 {b} {B : Set b}\n (f : A \u2192 B) {dx dy} \u2192 dx \u2259 dy \u2192 f (x \u229e dx) \u2261 f (x \u229e dy)\n \u2259-cong f da\u2259db = cong f $ proof da\u2259db\n\n \u2259-isEquivalence : IsEquivalence (_\u2259_)\n \u2259-isEquivalence = record\n { refl = \u2259-refl\n ; sym = \u2259-sym\n ; trans = \u2259-trans\n }\n\n \u2259-setoid : Setoid \u2113 a\n \u2259-setoid = record\n { Carrier = \u0394 x\n ; _\u2248_ = _\u2259_\n ; isEquivalence = \u2259-isEquivalence\n }\n\n\n ------------------------------------------------------------------------\n -- Convenient syntax for equational reasoning\n\n import Relation.Binary.EqReasoning as EqR\n\n module \u2259-Reasoning where\n open EqR \u2259-setoid public\n renaming (_\u2248\u27e8_\u27e9_ to _\u2259\u27e8_\u27e9_)\n\n -- By update-nil, if dx = nil x, then x \u229e dx \u2261 x.\n -- As a consequence, if dx \u2259 nil x, then x \u229e dx \u2261 x\n nil-is-\u229e-unit : \u2200 dx \u2192 dx \u2259 nil x \u2192 x \u229e dx \u2261 x\n nil-is-\u229e-unit dx dx\u2259nil-x =\n trans\n (proof (let open \u2259-Reasoning in\n begin\n dx\n \u2259\u27e8 dx\u2259nil-x \u27e9\n nil x\n \u220e))\n (let open \u2261-Reasoning in\n begin\n x \u229e (nil x)\n \u2261\u27e8 update-nil x \u27e9\n x\n \u220e)\n\n -- Here we prove the inverse:\n \u229e-unit-is-nil : \u2200 dx \u2192 x \u229e dx \u2261 x \u2192 dx \u2259 nil x\n \u229e-unit-is-nil dx x\u229edx\u2261x = doe $\n begin\n x \u229e dx\n \u2261\u27e8 x\u229edx\u2261x \u27e9\n x\n \u2261\u27e8 sym (update-nil x) \u27e9\n x \u229e nil x\n \u220e\n where\n open \u2261-Reasoning\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n -- This results pairs with update-diff.\n diff-update : \u2200 {dx} \u2192 (x \u229e dx) \u229f x \u2259 dx\n diff-update {dx} = doe lemma\n where\n lemma : x \u229e (x \u229e dx \u229f x) \u2261 x \u229e dx\n lemma = update-diff (x \u229e dx) x\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = doe lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 \u2259-cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 \u2259-cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n open import Postulate.Extensionality\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} df-x-dx\u2259dg-x-dx = doe lemma\u2082\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 x dx = proof $ df-x-dx\u2259dg-x-dx x dx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n\n -- We know that Derivative f (apply (nil f)) (by nil-is-derivative).\n -- That is, df = nil f -> Derivative f (apply df).\n -- Now, we try to prove that if Derivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 Derivative f (apply df) \u2192 Derivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 \u2259-sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n -- Unused, but just to test that inference works.\n lemma : nil f \u2259 dg\n lemma = \u2259-sym (derivative-is-nil dg fdg)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"df0a589fd6ba99849aba42de50fadcedc5ab274c","subject":"agda: prove update-diff","message":"agda: prove update-diff\n\nAlso, add infix declaration needed to state the result conveniently.\n\nOld-commit-hash: 1a2ad0add537966641273bb1ce153a976e3e6703\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Equivalence.agda","new_file":"Base\/Change\/Equivalence.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set a\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n -- Same priority as \u2261\n infix 4 _\u2259_\n\n open import Relation.Binary\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n \u2259-isEquivalence : IsEquivalence (_\u2259_)\n \u2259-isEquivalence = record\n { refl = \u2259-refl\n ; sym = \u2259-symm\n ; trans = \u2259-trans\n }\n\n \u2259-setoid : Setoid \u2113 a\n \u2259-setoid = record\n { Carrier = \u0394 x\n ; _\u2248_ = _\u2259_\n ; isEquivalence = \u2259-isEquivalence\n }\n\n\n ------------------------------------------------------------------------\n -- Convenient syntax for equational reasoning\n\n import Relation.Binary.EqReasoning as EqR\n\n -- Relation.Binary.EqReasoning is more convenient to use with _\u2261_ if\n -- the combinators take the type argument (a) as a hidden argument,\n -- instead of being locked to a fixed type at module instantiation\n -- time.\n\n module \u2259-Reasoning where\n open EqR \u2259-setoid public\n renaming (_\u2248\u27e8_\u27e9_ to _\u2259\u27e8_\u27e9_)\n\n -- By update-nil, if dx = nil x, then x \u229e dx \u2261 x.\n -- As a consequence, if dx \u2259 nil x, then x \u229e dx \u2261 x\n nil-is-\u229e-unit : \u2200 dx \u2192 dx \u2259 nil x \u2192 x \u229e dx \u2261 x\n nil-is-\u229e-unit dx dx\u2259nil-x =\n begin\n x \u229e dx\n \u2261\u27e8 dx\u2259nil-x \u27e9\n x \u229e (nil x)\n \u2261\u27e8 update-nil x \u27e9\n x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Here we prove the inverse:\n \u229e-unit-is-nil : \u2200 dx \u2192 x \u229e dx \u2261 x \u2192 dx \u2259 nil x\n \u229e-unit-is-nil dx x\u229edx\u2261x =\n begin\n x \u229e dx\n \u2261\u27e8 x\u229edx\u2261x \u27e9\n x\n \u2261\u27e8 sym (update-nil x) \u27e9\n x \u229e nil x\n \u220e\n where\n open \u2261-Reasoning\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n -- This results pairs with update-diff.\n diff-update : \u2200 {dx} \u2192 (x \u229e dx) \u229f x \u2259 dx\n diff-update {dx} = lemma\n where\n lemma : x \u229e (x \u229e dx \u229f x) \u2261 x \u229e dx\n lemma = update-diff (x \u229e dx) x\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n open import Postulate.Extensionality\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} df-x-dx\u2259dg-x-dx = lemma\u2082\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 = df-x-dx\u2259dg-x-dx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n\n -- We know that Derivative f (apply (nil f)) (by nil-is-derivative).\n -- That is, df = nil f -> Derivative f (apply df).\n -- Now, we try to prove that if Derivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 Derivative f (apply df) \u2192 Derivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n{-\n lemma : nil f \u2259 dg\n -- Goes through\n --lemma = sym (derivative-is-nil dg fdg)\n -- Generates tons of crazy yellow ambiguities of equality type. Apparently, unifying against \u2259 does not work so well.\n lemma = \u2259-symm {{changeAlgebra}} {f} {dg} {nil f} (derivative-is-nil dg fdg)\n-}\n\n -- We could also use derivative-is-\u229e-unit, but the proof above matches better\n -- with the text above.\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set a\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n open import Relation.Binary\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n \u2259-isEquivalence : IsEquivalence (_\u2259_)\n \u2259-isEquivalence = record\n { refl = \u2259-refl\n ; sym = \u2259-symm\n ; trans = \u2259-trans\n }\n\n \u2259-setoid : Setoid \u2113 a\n \u2259-setoid = record\n { Carrier = \u0394 x\n ; _\u2248_ = _\u2259_\n ; isEquivalence = \u2259-isEquivalence\n }\n\n\n ------------------------------------------------------------------------\n -- Convenient syntax for equational reasoning\n\n import Relation.Binary.EqReasoning as EqR\n\n -- Relation.Binary.EqReasoning is more convenient to use with _\u2261_ if\n -- the combinators take the type argument (a) as a hidden argument,\n -- instead of being locked to a fixed type at module instantiation\n -- time.\n\n module \u2259-Reasoning where\n open EqR \u2259-setoid public\n renaming (_\u2248\u27e8_\u27e9_ to _\u2259\u27e8_\u27e9_)\n\n -- By update-nil, if dx = nil x, then x \u229e dx \u2261 x.\n -- As a consequence, if dx \u2259 nil x, then x \u229e dx \u2261 x\n nil-is-\u229e-unit : \u2200 dx \u2192 dx \u2259 nil x \u2192 x \u229e dx \u2261 x\n nil-is-\u229e-unit dx dx\u2259nil-x =\n begin\n x \u229e dx\n \u2261\u27e8 dx\u2259nil-x \u27e9\n x \u229e (nil x)\n \u2261\u27e8 update-nil x \u27e9\n x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Here we prove the inverse:\n \u229e-unit-is-nil : \u2200 dx \u2192 x \u229e dx \u2261 x \u2192 dx \u2259 nil x\n \u229e-unit-is-nil dx x\u229edx\u2261x =\n begin\n x \u229e dx\n \u2261\u27e8 x\u229edx\u2261x \u27e9\n x\n \u2261\u27e8 sym (update-nil x) \u27e9\n x \u229e nil x\n \u220e\n where\n open \u2261-Reasoning\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n open import Postulate.Extensionality\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} df-x-dx\u2259dg-x-dx = lemma\u2082\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 = df-x-dx\u2259dg-x-dx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n\n -- We know that Derivative f (apply (nil f)) (by nil-is-derivative).\n -- That is, df = nil f -> Derivative f (apply df).\n -- Now, we try to prove that if Derivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 Derivative f (apply df) \u2192 Derivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n{-\n lemma : nil f \u2259 dg\n -- Goes through\n --lemma = sym (derivative-is-nil dg fdg)\n -- Generates tons of crazy yellow ambiguities of equality type. Apparently, unifying against \u2259 does not work so well.\n lemma = \u2259-symm {{changeAlgebra}} {f} {dg} {nil f} (derivative-is-nil dg fdg)\n-}\n\n -- We could also use derivative-is-\u229e-unit, but the proof above matches better\n -- with the text above.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"bd5737fe4a6205941af51d14021d29b56781ae08","subject":"Minor change.","message":"Minor change.\n\nIgnore-this: a8fb51b7be37e1ae1a4d30cb3ddc2f0c\n\ndarcs-hash:20120201152333-3bd4e-84e00d6e13b1bdffb892ef4a6040cb657dd398bb.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/GroupTheory\/Base.agda","new_file":"src\/GroupTheory\/Base.agda","new_contents":"------------------------------------------------------------------------------\n-- Group theory base\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule GroupTheory.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfix 11 _\u207b\u00b9\ninfixl 10 _\u00b7_ -- The symbol is '\\cdot'.\n\n------------------------------------------------------------------------------\n-- The group universe\nopen import Common.Universe public renaming ( D to G )\n\n-- The equality\n-- The equality is the propositional identity on the group universe.\nimport Common.Relation.Binary.PropositionalEquality\nopen module Eq =\n Common.Relation.Binary.PropositionalEquality.Inductive public\n\n-- Logical constants\nopen import Common.LogicalConstants public\n\n-- Group theory axioms\npostulate\n \u03b5 : G -- The identity element.\n _\u00b7_ : G \u2192 G \u2192 G -- The binary operation.\n _\u207b\u00b9 : G \u2192 G -- The inverse function.\n\n assoc : \u2200 x y z \u2192 x \u00b7 y \u00b7 z \u2261 x \u00b7 (y \u00b7 z)\n leftIdentity : \u2200 x \u2192 \u03b5 \u00b7 x \u2261 x\n rightIdentity : \u2200 x \u2192 x \u00b7 \u03b5 \u2261 x\n leftInverse : \u2200 x \u2192 x \u207b\u00b9 \u00b7 x \u2261 \u03b5\n rightInverse : \u2200 x \u2192 x \u00b7 x \u207b\u00b9 \u2261 \u03b5\n{-# ATP axiom assoc leftIdentity rightIdentity leftInverse rightInverse #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Group theory base\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule GroupTheory.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfix 11 _\u207b\u00b9\ninfixl 10 _\u00b7_ -- The symbol is '\\cdot'.\n\n------------------------------------------------------------------------------\n-- The group universe\nopen import Common.Universe public renaming ( D to G )\n\n-- The equality\n-- The equality is the propositional identity on the group universe.\nimport Common.Relation.Binary.PropositionalEquality\nopen module Eq =\n Common.Relation.Binary.PropositionalEquality.Inductive public\n\n-- Logical constants\nopen import Common.LogicalConstants public\n\n-- Group theory axioms\npostulate\n \u03b5 : G -- The identity element.\n _\u00b7_ : G \u2192 G \u2192 G -- The group binary operation.\n _\u207b\u00b9 : G \u2192 G -- The inverse function.\n\n assoc : \u2200 x y z \u2192 x \u00b7 y \u00b7 z \u2261 x \u00b7 (y \u00b7 z)\n leftIdentity : \u2200 x \u2192 \u03b5 \u00b7 x \u2261 x\n rightIdentity : \u2200 x \u2192 x \u00b7 \u03b5 \u2261 x\n leftInverse : \u2200 x \u2192 x \u207b\u00b9 \u00b7 x \u2261 \u03b5\n rightInverse : \u2200 x \u2192 x \u00b7 x \u207b\u00b9 \u2261 \u03b5\n{-# ATP axiom assoc leftIdentity rightIdentity leftInverse rightInverse #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"df40931154a8e60ffb751bf5fb6436018582169b","subject":"Some unfinished experiment","message":"Some unfinished experiment\n","repos":"crypto-agda\/crypto-agda","old_file":"Solver\/Linear\/Examples.agda","new_file":"Solver\/Linear\/Examples.agda","new_contents":"module Solver.Linear.Examples where\n\nopen import Solver.Linear.Syntax\nopen import Solver.Linear\nimport Data.String as String\nopen import Data.Zero\nopen import Relation.Binary.NP\nopen import Relation.Nullary.Decidable\nopen import Data.Vec\nopen import Data.Nat\nopen import Data.Product\nopen import Data.Unit\nopen import Function\nopen import FunUniverse.Agda\nopen import Relation.Binary.PropositionalEquality\n\nmodule #Vars {a} {A : Set a}\n (_\u225f\u1d2c_ : Decidable (_\u2261_ {A = A})) where\n #vars : Syn A \u2192 \u2115\n #vars tt = 0\n #vars (var x) = 1\n #vars (t , u) = #vars t + #vars u\n\n open import Data.List\n vars : (t : Syn A) \u2192 List A \u2192 List A\n vars tt = id\n vars (var x) = _\u2237_ x\n vars (t , u) = vars t \u2218 vars u\n\n lookupVar : \u2200 {b} {B : Set b} (t : Syn A) \u2192 Vec B (#vars t)\n \u2192 A \u2192 B \u2192 B\n lookupVar (var x) bs a\u2081 = {!!}\n lookupVar tt bs a\u2081 = {!!}\n lookupVar (t , t\u2081) bs a\u2081 = {!lookupVar t bs!}\n\nmodule Syntax\u02e2' {a} {A : Set a} {funU} linRewiring where\n open Syntax (\u03bb x y \u2192 \u230a String\u2264._' : \u2200 {A} \u2192 (A \u00d7 \u22a4) \u27f6 A\n \u207b\u00b9' : \u2200 {A} \u2192 A \u27f6 (A \u00d7 \u22a4)\n ' : \u2200 {A} \u2192 (\u22a4 \u00d7 A) \u27f6 A\n \u207b\u00b9' : \u2200 {A} \u2192 A \u27f6 (\u22a4 \u00d7 A)\n \u27e8_\u00d7'_\u27e9 : \u2200 {A B C D} \u2192 A \u27f6 C \u2192 B \u27f6 D \u2192 (A \u00d7 B) \u27f6 (C \u00d7 D)\n first : \u2200 {A B C} \u2192 A \u27f6 B \u2192 A \u00d7 C \u27f6 B \u00d7 C\n second : \u2200 {A B C} \u2192 B \u27f6 C \u2192 A \u00d7 B \u27f6 A \u00d7 C \n assoc' : \u2200 {A B C} \u2192 (A \u00d7 (B \u00d7 C)) \u27f6 ((A \u00d7 B) \u00d7 C)\n assoc\u207b\u00b9' : \u2200 {A B C} \u2192 ((A \u00d7 B) \u00d7 C) \u27f6 (A \u00d7 (B \u00d7 C))\n swap' : \u2200 {A B} \u2192 (A \u00d7 B) \u27f6 (B \u00d7 A)\n \n\n module STest {n} M = Syntax _\u00d7_ \u22a4 _\u27f6_ id' _\u223b'_ ' \u207b\u00b9' ' \u207b\u00b9' \u27e8_\u00d7'_\u27e9 first second assoc' assoc\u207b\u00b9' swap' {n} M\n\n test2 : (A B C : Ty) \u2192 (A \u00d7 B) \u00d7 C \u27f6 (B \u00d7 A) \u00d7 C\n test2 A B C = rewire ((# 0 , # 1) , # 2) ((# 1 , # 0) , # 2) _ where\n open STest (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n\nmodule example\u2083 where\n\n open import Data.Unit\n open import Data.Product\n open import Data.Vec\n\n open import Function using (flip ; const)\n \n open import Function.Inverse\n open import Function.Related.TypeIsomorphisms.NP\n\n open \u00d7-CMon using () renaming (\u2219-cong to \u00d7-cong ; assoc to \u00d7-assoc)\n\n module STest {n} M = Syntax _\u00d7_ \u22a4 _\u2194_ id (flip _\u2218_) A\u00d7\u22a4\u2194A (sym A\u00d7\u22a4\u2194A) (A\u00d7\u22a4\u2194A \u2218 swap-iso) (swap-iso \u2218 sym A\u00d7\u22a4\u2194A)\n \u00d7-cong first-iso (\u03bb x \u2192 second-iso (const x))\n (sym (\u00d7-assoc _ _ _)) (\u00d7-assoc _ _ _) swap-iso {n} M\n\n test : \u2200 A B C \u2192 ((A \u00d7 B) \u00d7 C) \u2194 (C \u00d7 (B \u00d7 A))\n test A B C = rewire ((# 0 , # 1) , # 2) (# 2 , (# 1 , # 0)) _ where\n open STest (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"module Solver.Linear.Examples where\n\nopen import Solver.Linear\nopen import Data.String\nopen import Data.Zero\nopen import Relation.Binary.NP\nopen import Relation.Nullary.Decidable\nopen import Data.Vec\nopen import Data.Product\nopen import Data.Unit\nopen import Function\nopen import FunUniverse.Agda\n\nmodule example1 where\n\n -- need to etaexpand this because otherwise we get an error\n open Syntax\u02e2 funLin\n test : (A B C : Set) \u2192 (A \u00d7 B) \u00d7 C \u2192 (B \u00d7 A) \u00d7 C\n test A B C = rewire\u02e2 \u0393 \"(A,B),C\u21a6(B,A),C\"\n where\n \u0393 : String \u2192 _\n \u0393 \"A\" = A\n \u0393 \"B\" = B\n \u0393 \"C\" = C\n \u0393 _ = \ud835\udfd8\n\nmodule example2 where\n open Syntax\u1da0 funLin\n test2 : (A B C : Set) \u2192 (A \u00d7 B) \u00d7 C \u2192 (B \u00d7 A) \u00d7 C\n test2 A B C = rewire\u1da0 (A \u2237 B \u2237 C \u2237 []) (\u03bb a b c \u2192 (a , b) , c \u21a6 (b , a) , c)\n\n {-\nmodule example3 where\n\n open import Data.Vec\n\n data Ty : Set where\n _\u00d7_ : Ty \u2192 Ty \u2192 Ty\n \u22a4 : Ty\n\n infix 4 _\u27f6_ \n\n data _\u27f6_ : Ty \u2192 Ty \u2192 Set where\n id' : \u2200 {A} \u2192 A \u27f6 A\n _\u223b'_ : \u2200 {A B C} \u2192 A \u27f6 B \u2192 B \u27f6 C \u2192 A \u27f6 C\n ' : \u2200 {A} \u2192 (A \u00d7 \u22a4) \u27f6 A\n \u207b\u00b9' : \u2200 {A} \u2192 A \u27f6 (A \u00d7 \u22a4)\n ' : \u2200 {A} \u2192 (\u22a4 \u00d7 A) \u27f6 A\n \u207b\u00b9' : \u2200 {A} \u2192 A \u27f6 (\u22a4 \u00d7 A)\n \u27e8_\u00d7'_\u27e9 : \u2200 {A B C D} \u2192 A \u27f6 C \u2192 B \u27f6 D \u2192 (A \u00d7 B) \u27f6 (C \u00d7 D)\n first : \u2200 {A B C} \u2192 A \u27f6 B \u2192 A \u00d7 C \u27f6 B \u00d7 C\n second : \u2200 {A B C} \u2192 B \u27f6 C \u2192 A \u00d7 B \u27f6 A \u00d7 C \n assoc' : \u2200 {A B C} \u2192 (A \u00d7 (B \u00d7 C)) \u27f6 ((A \u00d7 B) \u00d7 C)\n assoc\u207b\u00b9' : \u2200 {A B C} \u2192 ((A \u00d7 B) \u00d7 C) \u27f6 (A \u00d7 (B \u00d7 C))\n swap' : \u2200 {A B} \u2192 (A \u00d7 B) \u27f6 (B \u00d7 A)\n \n\n module STest {n} M = Syntax _\u00d7_ \u22a4 _\u27f6_ id' _\u223b'_ ' \u207b\u00b9' ' \u207b\u00b9' \u27e8_\u00d7'_\u27e9 first second assoc' assoc\u207b\u00b9' swap' {n} M\n\n test2 : (A B C : Ty) \u2192 (A \u00d7 B) \u00d7 C \u27f6 (B \u00d7 A) \u00d7 C\n test2 A B C = rewire ((# 0 , # 1) , # 2) ((# 1 , # 0) , # 2) _ where\n open STest (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n\nmodule example\u2083 where\n\n open import Data.Unit\n open import Data.Product\n open import Data.Vec\n\n open import Function using (flip ; const)\n \n open import Function.Inverse\n open import Function.Related.TypeIsomorphisms.NP\n\n open \u00d7-CMon using () renaming (\u2219-cong to \u00d7-cong ; assoc to \u00d7-assoc)\n\n module STest {n} M = Syntax _\u00d7_ \u22a4 _\u2194_ id (flip _\u2218_) A\u00d7\u22a4\u2194A (sym A\u00d7\u22a4\u2194A) (A\u00d7\u22a4\u2194A \u2218 swap-iso) (swap-iso \u2218 sym A\u00d7\u22a4\u2194A)\n \u00d7-cong first-iso (\u03bb x \u2192 second-iso (const x))\n (sym (\u00d7-assoc _ _ _)) (\u00d7-assoc _ _ _) swap-iso {n} M\n\n test : \u2200 A B C \u2192 ((A \u00d7 B) \u00d7 C) \u2194 (C \u00d7 (B \u00d7 A))\n test A B C = rewire ((# 0 , # 1) , # 2) (# 2 , (# 1 , # 0)) _ where\n open STest (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"c1d53d1c793d91e002e6c8b4ffbbcc8dca4c6eda","subject":"README.agda: Update recommended version of Agda","message":"README.agda: Update recommended version of Agda\n\nRationale: 2.4.0 doesn't install any more.\n","repos":"inc-lc\/ilc-agda","old_file":"README.agda","new_file":"README.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Machine-checked formalization of the theoretical results presented\n-- in the paper:\n--\n-- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann.\n-- A Theory of Changes for Higher-Order Languages:\n-- Incrementalizing \u03bb-Calculi by Static Differentiation.\n-- To appear at PLDI, ACM 2014.\n--\n-- We claim that this formalization\n--\n-- (1) proves every lemma and theorem in Sec. 2 and 3 of the paper,\n-- (2) formally specifies the interface between our incrementalization\n-- framework and potential plugins, and\n-- (3) shows that the plugin interface can be instantiated.\n--\n-- The first claim is the main reason for a machine-checked\n-- proof: We want to be sure that we got the proofs right.\n--\n-- The second claim is about reusability and applicability: Only\n-- a clearly defined interface allows other researchers to\n-- provide plugins for our framework.\n--\n-- The third claim is to show that the plugin interface is\n-- consistent: An inconsistent plugin interface would allow to\n-- prove arbitrary results in the framework.\n------------------------------------------------------------------------\n\nmodule README where\n\n-- We know two good ways to read this code base. You can either\n-- use Emacs with agda2-mode to interact with the source files,\n-- or you can use a web browser to view the pretty-printed and\n-- hyperlinked source files. For Agda power users, we also\n-- include basic setup information for your own machine.\n\n\n-- IF YOU WANT TO USE A BROWSER\n-- ============================\n--\n-- Start with *HTML* version of this readme. On the AEC\n-- submission website (online or inside the VM), follow the link\n-- \"view the Agda code in their browser\" to the file\n-- agda\/README.html.\n--\n-- The source code is syntax highlighted and hyperlinked. You can\n-- click on module names to open the corresponding files, or you\n-- can click on identifiers to jump to their definition. In\n-- general, a Agda file with name `Foo\/Bar\/Baz.agda` contains a\n-- module `Foo.Bar.Baz` and is shown in an HTML file\n-- `Foo.Bar.Baz.html`.\n--\n-- Note that we also include the HTML files generated for our\n-- transitive dependencies from the Agda standard library. This\n-- allows you to follow hyperlinks to the Agda standard\n-- library. It is the default behavior of `agda --html` which we\n-- used to generate the HTML.\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE EMACS\n-- ========================\n--\n-- Open this file in Emacs with agda2-mode installed. See below\n-- for which Agda version you need to install. On the VM image,\n-- everything is setup for you and you can just open this file in\n-- Emacs.\n--\n-- C-c C-l Load code. Type checks and syntax highlights. Use\n-- this if the code is not syntax highlighted, or\n-- after you changed something that you want to type\n-- check.\n--\n-- M-. Jump to definition of identifier at the point.\n-- M-* Jump back.\n--\n-- Note that README.agda imports Everything.agda which imports\n-- every Agda file in our formalization. So if README.agda type\n-- checks successfully, everything does. If you want to type\n-- check everything from scratch, delete the *.agdai files to\n-- disable separate compilation. You can use \"find . -name\n-- '*.agdai' | xargs rm\" to do that.\n--\n-- More information on the Agda mode is available on the Agda wiki:\n--\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Docs.EmacsModeKeyCombinations\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE YOUR OWN SETUP\n-- =================================\n--\n-- To typecheck this formalization, you need to install the appropriate version\n-- of Agda (2.4.0.*), the Agda standard library (version 0.8), generate\n-- Everything.agda with the attached Haskell helper, and finally run Agda on\n-- this file.\n--\n-- Given a Unix-like environment (including Cygwin), running the .\/agdaCheck.sh\n-- script and following instructions given on output will eventually generate\n-- Everything.agda and proceed to type check everything on command line.\n--\n-- If you're not an Agda power user, it is probably easier to use\n-- the VM image or look at the pretty-printed and hyperlinked\n-- HTML files, see above.\n\n\n\n-- WHERE TO START READING?\n-- =======================\n--\n-- modules.pdf\n-- The graph of dependencies between Agda modules.\n-- Good if you want to get a broad overview.\n--\n-- README.agda\n-- This file. A coarse-grained introduction to the Agda\n-- formalization. Good if you want to begin at the beginning\n-- and understand the structure of our code.\n--\n-- PLDI14-List-of-Theorems.agda\n-- Pointers to the Agda formalizations of all theorems, lemmas\n-- or definitions in the PLDI paper. Good if you want to read\n-- the paper and the Agda code side by side.\n--\n-- Here is an import of this file, so you can jump to it\n-- directly (use M-. in Emacs or click the module name in the\n-- Browser):\n\nimport PLDI14-List-of-Theorems\n\n-- Everything.agda\n-- Imports every Agda module in our formalization. Good if you\n-- want to make sure you don't miss anything.\n--\n-- Again, here's is an import of this file so you can navigate\n-- there:\n\nimport Everything\n\n-- (This import is also important to ensure that if we typecheck\n-- README.agda, we also typecheck every other file of our\n-- formalization).\n\n\n\n-- THE AGDA CODE\n-- =============\n--\n-- The formalization has four parts:\n--\n-- 1. A formalization of change structures. This lives in\n-- Base.Change.Algebra. (What we call \"change structure\" in the\n-- paper, we call \"change algebra\" in the Agda code. We changed\n-- the name when writing the paper, and never got around to\n-- updating the name in the Agda code).\n--\n-- 2. Incrementalization framework for first-class functions,\n-- with extension points for plugging in data types and their\n-- incrementalization. This lives in the Parametric.*\n-- hierarchy.\n--\n-- 3. An example plugin that provides integers and bags\n-- with negative multiplicity. This lives in the Nehemiah.*\n-- hierarchy. (For some reason, we choose to call this\n-- particular incarnation of the plugin Nehemiah).\n--\n-- 4. Other material that is unrelated to the framework\/plugin\n-- distinction. This is all other files.\n\n\n\n-- FORMALIZATION OF CHANGE STRUCTURES\n-- ==================================\n--\n-- Section 2 of the paper, and Base.Change.Algebra in Agda.\n\nimport Base.Change.Algebra\n\n\n\n-- INCREMENTALIZATION FRAMEWORK\n-- ============================\n--\n-- Section 3 of the paper, and Parametric.* hierarchy in Agda.\n--\n-- The extension points are implemented as module parameters. See\n-- detailed explanation in Parametric.Syntax.Type and\n-- Parametric.Syntax.Term. Some extension points are for types,\n-- some for values, and some for proof fragments. In Agda, these\n-- three kinds of entities are unified anyway, so we can encode\n-- all of them as module parameters.\n--\n-- Every module in the Parametric.* hierarchy adds at least one\n-- extension point, so the module hierarchy of a plugin will\n-- typically mirror the Parametric.* hierarchy, defining the\n-- values for these extension points.\n--\n-- Firstly, we have the syntax of types and terms, the erased\n-- change structure for function types and incrementalization as\n-- a term-to-term transformation. The contents of these modules\n-- formalizes Sec. 3.1 and 3.2 of the paper, except for the\n-- second and third line of Figure 3, which is formalized further\n-- below.\n\nimport Parametric.Syntax.Type -- syntax of types\nimport Parametric.Syntax.Term -- syntax of terms\nimport Parametric.Change.Type -- simply-typed changes\nimport Parametric.Change.Derive -- incrementalization\n\n-- Secondly, we define the usual denotational semantics of the\n-- simply-typed lambda calculus in terms of total functions, and\n-- the change semantics in terms of a change structure on total\n-- functions. The contents of these modules formalize Sec. 3.3,\n-- 3.4 and 3.5 of the paper.\n\nimport Parametric.Denotation.Value -- standard values\nimport Parametric.Denotation.Evaluation -- standard evaluation\nimport Parametric.Change.Validity -- dependently-typed changes\nimport Parametric.Change.Specification -- change evaluation\n\n-- Thirdly, we define terms that operate on simply-typed changes,\n-- and connect them to their values. The concents of these\n-- modules formalize the second and third line of Figure 3, as\n-- well as the semantics of these lines.\n\nimport Parametric.Change.Term -- terms that operate on simply-typed changes\nimport Parametric.Change.Value -- the values of these terms\nimport Parametric.Change.Evaluation -- connecting the terms and their values\n\n-- Finally, we prove correctness by connecting the (syntactic)\n-- incrementalization to the (semantic) change evaluation by a\n-- logical relation, and a proof that the values of terms\n-- produced by the incrementalization transformation are related\n-- to the change values of the original terms. The contents of\n-- these modules formalize Sec. 3.6.\n\nimport Parametric.Change.Implementation -- logical relation\nimport Parametric.Change.Correctness -- main correctness proof\n\n\n\n-- EXAMPLE PLUGIN\n-- ==============\n--\n-- Sec. 3.7 in the paper, and the Nehemiah.* hierarchy in Agda.\n--\n-- The module structure of the plugin follows the structure of\n-- the Parametric.* hierarchy. For example, the extension point\n-- defined in Parametric.Syntax.Term is instantiated in\n-- Nehemiah.Syntax.Term.\n--\n-- As discussed in Sec. 3.7 of the paper, the point of this\n-- plugin is not to speed up any real programs, but to show \"that\n-- the interface for proof plugins can be implemented\". As a\n-- first step towards proving correctness of the more complicated\n-- plugin with integers, bags and finite maps we implement in\n-- Scala, we choose to define plugin with integers and bags in\n-- Agda. Instead of implementing bags (with negative\n-- multiplicities, like in the paper) in Agda, though, we\n-- postulate that a group of such bags exist. Note that integer\n-- bags with integer multiplicities are actually the free group\n-- given a singleton operation `Integer -> Bag`, so this should\n-- be easy to formalize in principle.\n\n-- Before we start with the plugin, we postulate an abstract data\n-- type for integer bags.\nimport Postulate.Bag-Nehemiah\n\n-- Firstly, we extend the syntax of types and terms, the erased\n-- change structure for function types, and incrementalization as\n-- a term-to-term transformation to account for the data types of\n-- the Nehemiah language. The contents of these modules\n-- instantiate the extension points in Sec. 3.1 and 3.2 of the\n-- paper, except for the second and third line of Figure 3, which\n-- is instantiated further below.\n\nimport Nehemiah.Syntax.Type\nimport Nehemiah.Syntax.Term\nimport Nehemiah.Change.Type\nimport Nehemiah.Change.Derive\n\n-- Secondly, we extend the usual denotational semantics and the\n-- change semantics to account for the data types of the Nehemiah\n-- language. The contents of these modules instantiate the\n-- extension points in Sec. 3.3, 3.4 and 3.5 of the paper.\n\nimport Nehemiah.Denotation.Value\nimport Nehemiah.Denotation.Evaluation\nimport Nehemiah.Change.Validity\nimport Nehemiah.Change.Specification\n\n-- Thirdly, we extend the terms that operate on simply-typed\n-- changes, and the connection to their values to account for the\n-- data types of the Nehemiah language. The concents of these\n-- modules instantiate the extension points in the second and\n-- third line of Figure 3.\n\nimport Nehemiah.Change.Term\nimport Nehemiah.Change.Value\nimport Nehemiah.Change.Evaluation\n\n-- Finally, we extend the logical relation and the main\n-- correctness proof to account for the data types in the\n-- Nehemiah language. The contents of these modules instantiate\n-- the extension points defined in Sec. 3.6.\n\nimport Nehemiah.Change.Implementation\nimport Nehemiah.Change.Correctness\n\n\n\n-- OTHER MATERIAL\n-- ==============\n--\n-- We postulate extensionality of Agda functions. This postulate\n-- is well known to be compatible with Agda's type theory.\n\nimport Postulate.Extensionality\n\n-- For historical reasons, we reexport Data.List.All from the\n-- standard library under the name DependentList.\n\nimport Base.Data.DependentList\n\n-- This module supports overloading the \u27e6_\u27e7 notation based on\n-- Agda's instance arguments.\n\nimport Base.Denotation.Notation\n\n-- These modules implement contexts including typed de Bruijn\n-- indices to represent bound variables, sets of bound variables,\n-- and environments. These modules are parametric in the set of\n-- types (that are stored in contexts) and the set of values\n-- (that are stored in environments). So these modules are even\n-- more parametric than the Parametric.* hierarchy.\n\nimport Base.Syntax.Context\nimport Base.Syntax.Vars\nimport Base.Denotation.Environment\n\n-- This module contains some helper definitions to merge a\n-- context of values and a context of changes.\nimport Base.Change.Context\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Machine-checked formalization of the theoretical results presented\n-- in the paper:\n--\n-- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann.\n-- A Theory of Changes for Higher-Order Languages:\n-- Incrementalizing \u03bb-Calculi by Static Differentiation.\n-- To appear at PLDI, ACM 2014.\n--\n-- We claim that this formalization\n--\n-- (1) proves every lemma and theorem in Sec. 2 and 3 of the paper,\n-- (2) formally specifies the interface between our incrementalization\n-- framework and potential plugins, and\n-- (3) shows that the plugin interface can be instantiated.\n--\n-- The first claim is the main reason for a machine-checked\n-- proof: We want to be sure that we got the proofs right.\n--\n-- The second claim is about reusability and applicability: Only\n-- a clearly defined interface allows other researchers to\n-- provide plugins for our framework.\n--\n-- The third claim is to show that the plugin interface is\n-- consistent: An inconsistent plugin interface would allow to\n-- prove arbitrary results in the framework.\n------------------------------------------------------------------------\n\nmodule README where\n\n-- We know two good ways to read this code base. You can either\n-- use Emacs with agda2-mode to interact with the source files,\n-- or you can use a web browser to view the pretty-printed and\n-- hyperlinked source files. For Agda power users, we also\n-- include basic setup information for your own machine.\n\n\n-- IF YOU WANT TO USE A BROWSER\n-- ============================\n--\n-- Start with *HTML* version of this readme. On the AEC\n-- submission website (online or inside the VM), follow the link\n-- \"view the Agda code in their browser\" to the file\n-- agda\/README.html.\n--\n-- The source code is syntax highlighted and hyperlinked. You can\n-- click on module names to open the corresponding files, or you\n-- can click on identifiers to jump to their definition. In\n-- general, a Agda file with name `Foo\/Bar\/Baz.agda` contains a\n-- module `Foo.Bar.Baz` and is shown in an HTML file\n-- `Foo.Bar.Baz.html`.\n--\n-- Note that we also include the HTML files generated for our\n-- transitive dependencies from the Agda standard library. This\n-- allows you to follow hyperlinks to the Agda standard\n-- library. It is the default behavior of `agda --html` which we\n-- used to generate the HTML.\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE EMACS\n-- ========================\n--\n-- Open this file in Emacs with agda2-mode installed. See below\n-- for which Agda version you need to install. On the VM image,\n-- everything is setup for you and you can just open this file in\n-- Emacs.\n--\n-- C-c C-l Load code. Type checks and syntax highlights. Use\n-- this if the code is not syntax highlighted, or\n-- after you changed something that you want to type\n-- check.\n--\n-- M-. Jump to definition of identifier at the point.\n-- M-* Jump back.\n--\n-- Note that README.agda imports Everything.agda which imports\n-- every Agda file in our formalization. So if README.agda type\n-- checks successfully, everything does. If you want to type\n-- check everything from scratch, delete the *.agdai files to\n-- disable separate compilation. You can use \"find . -name\n-- '*.agdai' | xargs rm\" to do that.\n--\n-- More information on the Agda mode is available on the Agda wiki:\n--\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Docs.EmacsModeKeyCombinations\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE YOUR OWN SETUP\n-- =================================\n--\n-- To typecheck this formalization, you need to install the appropriate version\n-- of Agda (2.4.0), the Agda standard library (version 0.8), generate\n-- Everything.agda with the attached Haskell helper, and finally run Agda on\n-- this file.\n--\n-- Given a Unix-like environment (including Cygwin), running the .\/agdaCheck.sh\n-- script and following instructions given on output will eventually generate\n-- Everything.agda and proceed to type check everything on command line.\n--\n-- If you're not an Agda power user, it is probably easier to use\n-- the VM image or look at the pretty-printed and hyperlinked\n-- HTML files, see above.\n\n\n\n-- WHERE TO START READING?\n-- =======================\n--\n-- modules.pdf\n-- The graph of dependencies between Agda modules.\n-- Good if you want to get a broad overview.\n--\n-- README.agda\n-- This file. A coarse-grained introduction to the Agda\n-- formalization. Good if you want to begin at the beginning\n-- and understand the structure of our code.\n--\n-- PLDI14-List-of-Theorems.agda\n-- Pointers to the Agda formalizations of all theorems, lemmas\n-- or definitions in the PLDI paper. Good if you want to read\n-- the paper and the Agda code side by side.\n--\n-- Here is an import of this file, so you can jump to it\n-- directly (use M-. in Emacs or click the module name in the\n-- Browser):\n\nimport PLDI14-List-of-Theorems\n\n-- Everything.agda\n-- Imports every Agda module in our formalization. Good if you\n-- want to make sure you don't miss anything.\n--\n-- Again, here's is an import of this file so you can navigate\n-- there:\n\nimport Everything\n\n-- (This import is also important to ensure that if we typecheck\n-- README.agda, we also typecheck every other file of our\n-- formalization).\n\n\n\n-- THE AGDA CODE\n-- =============\n--\n-- The formalization has four parts:\n--\n-- 1. A formalization of change structures. This lives in\n-- Base.Change.Algebra. (What we call \"change structure\" in the\n-- paper, we call \"change algebra\" in the Agda code. We changed\n-- the name when writing the paper, and never got around to\n-- updating the name in the Agda code).\n--\n-- 2. Incrementalization framework for first-class functions,\n-- with extension points for plugging in data types and their\n-- incrementalization. This lives in the Parametric.*\n-- hierarchy.\n--\n-- 3. An example plugin that provides integers and bags\n-- with negative multiplicity. This lives in the Nehemiah.*\n-- hierarchy. (For some reason, we choose to call this\n-- particular incarnation of the plugin Nehemiah).\n--\n-- 4. Other material that is unrelated to the framework\/plugin\n-- distinction. This is all other files.\n\n\n\n-- FORMALIZATION OF CHANGE STRUCTURES\n-- ==================================\n--\n-- Section 2 of the paper, and Base.Change.Algebra in Agda.\n\nimport Base.Change.Algebra\n\n\n\n-- INCREMENTALIZATION FRAMEWORK\n-- ============================\n--\n-- Section 3 of the paper, and Parametric.* hierarchy in Agda.\n--\n-- The extension points are implemented as module parameters. See\n-- detailed explanation in Parametric.Syntax.Type and\n-- Parametric.Syntax.Term. Some extension points are for types,\n-- some for values, and some for proof fragments. In Agda, these\n-- three kinds of entities are unified anyway, so we can encode\n-- all of them as module parameters.\n--\n-- Every module in the Parametric.* hierarchy adds at least one\n-- extension point, so the module hierarchy of a plugin will\n-- typically mirror the Parametric.* hierarchy, defining the\n-- values for these extension points.\n--\n-- Firstly, we have the syntax of types and terms, the erased\n-- change structure for function types and incrementalization as\n-- a term-to-term transformation. The contents of these modules\n-- formalizes Sec. 3.1 and 3.2 of the paper, except for the\n-- second and third line of Figure 3, which is formalized further\n-- below.\n\nimport Parametric.Syntax.Type -- syntax of types\nimport Parametric.Syntax.Term -- syntax of terms\nimport Parametric.Change.Type -- simply-typed changes\nimport Parametric.Change.Derive -- incrementalization\n\n-- Secondly, we define the usual denotational semantics of the\n-- simply-typed lambda calculus in terms of total functions, and\n-- the change semantics in terms of a change structure on total\n-- functions. The contents of these modules formalize Sec. 3.3,\n-- 3.4 and 3.5 of the paper.\n\nimport Parametric.Denotation.Value -- standard values\nimport Parametric.Denotation.Evaluation -- standard evaluation\nimport Parametric.Change.Validity -- dependently-typed changes\nimport Parametric.Change.Specification -- change evaluation\n\n-- Thirdly, we define terms that operate on simply-typed changes,\n-- and connect them to their values. The concents of these\n-- modules formalize the second and third line of Figure 3, as\n-- well as the semantics of these lines.\n\nimport Parametric.Change.Term -- terms that operate on simply-typed changes\nimport Parametric.Change.Value -- the values of these terms\nimport Parametric.Change.Evaluation -- connecting the terms and their values\n\n-- Finally, we prove correctness by connecting the (syntactic)\n-- incrementalization to the (semantic) change evaluation by a\n-- logical relation, and a proof that the values of terms\n-- produced by the incrementalization transformation are related\n-- to the change values of the original terms. The contents of\n-- these modules formalize Sec. 3.6.\n\nimport Parametric.Change.Implementation -- logical relation\nimport Parametric.Change.Correctness -- main correctness proof\n\n\n\n-- EXAMPLE PLUGIN\n-- ==============\n--\n-- Sec. 3.7 in the paper, and the Nehemiah.* hierarchy in Agda.\n--\n-- The module structure of the plugin follows the structure of\n-- the Parametric.* hierarchy. For example, the extension point\n-- defined in Parametric.Syntax.Term is instantiated in\n-- Nehemiah.Syntax.Term.\n--\n-- As discussed in Sec. 3.7 of the paper, the point of this\n-- plugin is not to speed up any real programs, but to show \"that\n-- the interface for proof plugins can be implemented\". As a\n-- first step towards proving correctness of the more complicated\n-- plugin with integers, bags and finite maps we implement in\n-- Scala, we choose to define plugin with integers and bags in\n-- Agda. Instead of implementing bags (with negative\n-- multiplicities, like in the paper) in Agda, though, we\n-- postulate that a group of such bags exist. Note that integer\n-- bags with integer multiplicities are actually the free group\n-- given a singleton operation `Integer -> Bag`, so this should\n-- be easy to formalize in principle.\n\n-- Before we start with the plugin, we postulate an abstract data\n-- type for integer bags.\nimport Postulate.Bag-Nehemiah\n\n-- Firstly, we extend the syntax of types and terms, the erased\n-- change structure for function types, and incrementalization as\n-- a term-to-term transformation to account for the data types of\n-- the Nehemiah language. The contents of these modules\n-- instantiate the extension points in Sec. 3.1 and 3.2 of the\n-- paper, except for the second and third line of Figure 3, which\n-- is instantiated further below.\n\nimport Nehemiah.Syntax.Type\nimport Nehemiah.Syntax.Term\nimport Nehemiah.Change.Type\nimport Nehemiah.Change.Derive\n\n-- Secondly, we extend the usual denotational semantics and the\n-- change semantics to account for the data types of the Nehemiah\n-- language. The contents of these modules instantiate the\n-- extension points in Sec. 3.3, 3.4 and 3.5 of the paper.\n\nimport Nehemiah.Denotation.Value\nimport Nehemiah.Denotation.Evaluation\nimport Nehemiah.Change.Validity\nimport Nehemiah.Change.Specification\n\n-- Thirdly, we extend the terms that operate on simply-typed\n-- changes, and the connection to their values to account for the\n-- data types of the Nehemiah language. The concents of these\n-- modules instantiate the extension points in the second and\n-- third line of Figure 3.\n\nimport Nehemiah.Change.Term\nimport Nehemiah.Change.Value\nimport Nehemiah.Change.Evaluation\n\n-- Finally, we extend the logical relation and the main\n-- correctness proof to account for the data types in the\n-- Nehemiah language. The contents of these modules instantiate\n-- the extension points defined in Sec. 3.6.\n\nimport Nehemiah.Change.Implementation\nimport Nehemiah.Change.Correctness\n\n\n\n-- OTHER MATERIAL\n-- ==============\n--\n-- We postulate extensionality of Agda functions. This postulate\n-- is well known to be compatible with Agda's type theory.\n\nimport Postulate.Extensionality\n\n-- For historical reasons, we reexport Data.List.All from the\n-- standard library under the name DependentList.\n\nimport Base.Data.DependentList\n\n-- This module supports overloading the \u27e6_\u27e7 notation based on\n-- Agda's instance arguments.\n\nimport Base.Denotation.Notation\n\n-- These modules implement contexts including typed de Bruijn\n-- indices to represent bound variables, sets of bound variables,\n-- and environments. These modules are parametric in the set of\n-- types (that are stored in contexts) and the set of values\n-- (that are stored in environments). So these modules are even\n-- more parametric than the Parametric.* hierarchy.\n\nimport Base.Syntax.Context\nimport Base.Syntax.Vars\nimport Base.Denotation.Environment\n\n-- This module contains some helper definitions to merge a\n-- context of values and a context of changes.\nimport Base.Change.Context\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"93a456c3a471b05be1db7d20a63cfa4aea8a6a1a","subject":"agda Wouter Predicate Transformer","message":"agda Wouter Predicate Transformer\n","repos":"haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc","old_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/x.agda","new_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/x.agda","new_contents":"{-# OPTIONS --type-in-type #-} -- NOT SOUND!\n\nopen import Data.Empty using (\u22a5)\nopen import Data.List hiding (map; [_])\nopen import Data.Nat renaming (\u2115 to Nat)\nopen import Data.Nat.DivMod\nopen import Data.Product hiding (map)\nopen import Data.Unit using (\u22a4; tt)\n------------------------------------------------------------------------------\nopen import Relation.Binary.PropositionalEquality using (_\u2261_; refl)\n\nmodule x where\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\nabstract\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to\n - use this refinement relation to show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\n-- C : type of commands\n-- Free C R : returns an 'a' or issues command c : C\n-- For each c : C, there is a set of responses R c\n-- 2nd arg of Step is continuation : how to proceed after receiving response R c\ndata Free (C : Set) (R : C \u2192 Set) (a : Set) : Set where\n Pure : a \u2192 Free C R a\n Step : (c : C) \u2192 (R c \u2192 Free C R a) \u2192 Free C R a\n\n-- show that 'Free' is a monad:\n\nmap : \u2200 {a b C : Set} {R : C \u2192 Set} \u2192 (a \u2192 b) \u2192 Free C R a \u2192 Free C R b\nmap f (Pure x) = Pure (f x)\nmap f (Step c k) = Step c (\u03bb r \u2192 map f (k r))\n\nreturn : \u2200 {a C : Set} {R : C \u2192 Set} \u2192 a \u2192 Free C R a\nreturn = Pure\n\n_>>=_ : \u2200 {a b C : Set} {R : C \u2192 Set} \u2192 Free C R a \u2192 (a \u2192 Free C R b) \u2192 Free C R b\nPure x >>= f = f x\nStep c x >>= f = Step c (\u03bb r \u2192 x r >>= f)\n\n{-\ndifferent effects choose C and R differently, depending on their ops\n\n--------------------------------------------------\nWeakest precondition semantics\n\nidea of associating weakest precondition semantics with imperative programs\ndates to Dijkstra\u2019s Guarded Command Language [1975]\n\nways to specify behaviour of function f : a \u2192 b\n- reference implementation\n- define a relation R : a \u2192 b \u2192 Set\n- write contracts and test cases\n- PT semantics\n\ncall values of type a \u2192 Set : predicate on type a\n\nPTs are functions between predicates\ne.g., weakest precondition:\n-}\n\n-- \"maps\"\n-- function f : a \u2192 b and\n-- desired postcondition on the function\u2019s output, b \u2192 Set\n-- to weakest precondition a \u2192 Set on function\u2019s input that ensures postcondition satisfied\n--\n-- non-dependent version\n-- note: definition is just reverse function composition\nwp0 : \u2200 {a : Set} {b : Set} (f : a \u2192 b) \u2192 (b \u2192 Set) \u2192 (a \u2192 Set)\nwp0 f P = \u03bb x \u2192 P (f x)\n{-\nabove wp semantics is sometimes too restrictive\n- no way to specify that output is related to input\n- fix via making f dependent:\n-}\n-- dependent version\nwp : \u2200 {a : Set} {b : a \u2192 Set} (f : (x : a) \u2192 b x) \u2192 ((x : a) \u2192 b x \u2192 Set) \u2192 (a \u2192 Set)\nwp f P = \u03bb x \u2192 P x (f x)\n\n-- shorthand for working with predicates and predicates transformers\n_\u2286_ : \u2200 {a : Set} \u2192 (a \u2192 Set) \u2192 (a \u2192 Set) \u2192 Set\nP \u2286 Q = \u2200 x \u2192 P x \u2192 Q x\n\n-- refinement relation defined between PTs\n_\u2291_ : \u2200 {a : Set} {b : a \u2192 Set} \u2192 (pt1 pt2 : ((x : a) \u2192 b x \u2192 Set) \u2192 (a \u2192 Set)) \u2192 Set\u2081\npt1 \u2291 pt2 = \u2200 P \u2192 pt1 P \u2286 pt2 P\n\n{-\nuse refinement relation\n- to relate PT semantics between programs and specifications\n- to show a program satisfies its specification; or\n- to show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\nin pure setting, this refinement relation is not interesting:\nthe refinement relation corresponds to extensional equality between functions:\n\nlemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\nrefinement : \u2200 (f g : a \u2192 b) \u2192 (wp f \u2291 wp g) \u2194 (\u2200 x \u2192 f x \u2261 g x)\n\nthis paper defines PT semantics for Kleisli arrows of form\n\n a \u2192 Free C R b\n\ncould use 'wp' to assign semantics to these computations directly,\nbut typically not interested in syntactic equality between free monads\n\nrather want to study semantics of effectful programs they represent\n\nto define a PT semantics for effects\ndefine a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a \u2192 Set) \u2192 Free C R a \u2192 Set\n\n'pt' def depends on semantics desired for a particulr free monad\n\nCrucially, choice of\n- pt and\n- weakest precondition semantics : wp\ntogether give a way to assign weakest precondition semantics\nto Kleisli arrows representing effectful computations\n\n------------------------------------------------------------------------------\n3 PARTIALITY\n\nPartial computations : i.e., 'Maybe'\n\nmake choices for commands C and responses R\n-}\n\ndata C : Set where\n Abort : C -- no continuation\n\nR : C \u2192 Set\nR Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\nPartial : Set \u2192 Set\nPartial = Free C R\n\n-- smart constructor for failure:\nabort : \u2200 {a : Set} \u2192 Partial a\nabort = Step Abort (\u03bb ())\n\n{-\ncomputation of type Partial a will either\n- return a value of type a or\n- fail, issuing abort command\n\nNote that responses to Abort command are empty;\nabort smart constructor abort uses this to discharge the continuation\nin the second argument of the Step constructor\n\n--------------------------------------------------\nExample: division\n\nexpression language, closed under division and natural numbers:\n-}\n\ndata Expr : Set where\n Val : Nat \u2192 Expr\n Div : Expr \u2192 Expr \u2192 Expr\n\nexv : Expr\nexv = Val 3\nexd : Expr\nexd = Div (Val 3) (Val 3)\n\n-- semantics specified using inductively defined RELATION:\n-- def rules out erroneous results by requiring the divisor evaluates to non-zero\ndata _\u21d3_ : Expr \u2192 Nat \u2192 Set where\n \u21d3Base : \u2200 {x : Nat}\n \u2192 Val x \u21d3 x\n \u21d3Step : \u2200 {l r : Expr} {v1 v2 : Nat}\n \u2192 l \u21d3 v1\n \u2192 r \u21d3 (suc v2)\n \u2192 Div l r \u21d3 (v1 div (suc v2))\n\nexb : Val 3 \u21d3 3\nexb = \u21d3Base\n\nexs : Div (Val 3) (Val 3) \u21d3 1\nexs = \u21d3Step \u21d3Base \u21d3Base\n\n-- alternatively, semantics specified by an INTERPRETER\n-- evaluate Expr via monadic INTERPRETER, using Partial to handle division-by-zero\n\n-- define operation used by \u27e6_\u27e7 interpreter\n_\u00f7_ : Nat \u2192 Nat \u2192 Partial Nat\nn \u00f7 zero = abort\nn \u00f7 (suc k) = return (n div (suc k))\n\n\u27e6_\u27e7 : Expr \u2192 Partial Nat\n\u27e6 Val x \u27e7 = return x\n\u27e6 Div e1 e2 \u27e7 = \u27e6 e1 \u27e7 >>= \u03bb v1 \u2192 \u27e6 e2 \u27e7 >>= \u03bb v2 \u2192 v1 \u00f7 v2\n\nevv : Free C R Nat\nevv = \u27e6 Val 3 \u27e7\nevv' : evv \u2261 Pure 3\nevv' = refl\n\nevd : Free C R Nat\nevd = \u27e6 Div (Val 3) (Val 3) \u27e7\nevd' : evd \u2261 Pure 1\nevd' = refl\n\nevd0 : Free C R Nat\nevd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\nevd0' : evd0 \u2261 Step Abort (\u03bb ())\nevd0' = refl\n\n{-\nHow to relate two definitions:\n- std lib 'div' requires implicit proof that divisor is non-zero\n - \u21d3 relation generates via pattern matching\n - _\u00f7_ does explicit check\n- interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\nAssign a weakest precondition semantics to Kleisli arrows of the form\n\n a \u2192 Partial b\n-}\n\nmustPT : \u2200 {a : Set} {b : a \u2192 Set}\n \u2192 (P : (x : a) \u2192 b x \u2192 Set)\n \u2192 (x : a)\n \u2192 Partial (b x)\n \u2192 Set\nmustPT P _ (Pure y) = P _ y\nmustPT P _ (Step Abort _) = \u22a5\n\nwpPartial : \u2200 {a : Set} {b : a \u2192 Set}\n \u2192 (f : (x : a) \u2192 Partial (b x))\n \u2192 (P : (x : a) \u2192 b x \u2192 Set)\n \u2192 (a \u2192 Set)\nwpPartial f P = wp f (mustPT P)\n{-\nTo call 'wp', must show how to transform\n- predicate P : b \u2192 Set\n- to a predicate on partial results : Partial b \u2192 Set\nDone via proposition 'mustPT P c'\n- holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\nparticular PT semantics of partial computations determined by def mustPT\nhere: rule out failure entirely\n- so case Abort returns empty type\n\nGiven this PT semantics for Kleisli arrows in general,\ncan now study semantics of above monadic interpreter\nvia passing\n- interpreter: \u27e6_\u27e7\n- desired postcondition : _\u21d3_\nas arguments to wpPartial:\n-}\n\nexwpp : Expr \u2192 Set\nexwpp = wpPartial \u27e6_\u27e7 _\u21d3_\nexwpp' : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb z \u2192 mustPT _\u21d3_ z \u27e6 z \u27e7\nexwpp' = refl\n\nwppv1 : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\nwppv1 = \u21d3Base\nwppd : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\nwppd = \u21d3Step \u21d3Base \u21d3Base\n\n\n{-\nresulting in a predicate on expressions\n\nfor all expressions satisfying this predicate,\nthe monadic interpreter and the relational specification, _\u21d3_,\nmust agree on the result of evaluation\n\nWhat does this say about correctness of interpreter?\nTo understand the predicate better, consider defining this predicate on expressions:\n-}\n\nSafeDiv : Expr \u2192 Set\nSafeDiv (Val x) = \u22a4\nSafeDiv (Div e1 e2) = (e2 \u21d3 zero \u2192 \u22a5) {-\u2227-} \u00d7 SafeDiv e1 {-\u2227-} \u00d7 SafeDiv e2\n\nexsdv : SafeDiv (Val 3) \u2261 \u22a4\nexsdv = refl\nexsdd : SafeDiv (Div (Val 3) (Val 3)) \u2261 \u03a3 ((x : Val 3 \u21d3 zero) \u2192 \u22a5) (\u03bb _ \u2192 \u03a3 \u22a4 (\u03bb _ \u2192 \u22a4))\nexsdd = refl\n\n{-\nExpect : any expr e for which SafeDiv e holds\ncan be evaluated without division-by-zero\n\ncan prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n-- lemma relates the two semantics\n-- expressed as a relation and an evaluator\n-- for those expressions that satisfy the SafeDiv property\n-}\n\n-- correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n{-\ncorrect (Val n) tt = \u21d3Base\ncorrect (Div exprL exprR) (exprR\u21d3zero\u2192\u22a5 , saveDivExprL , safeDivExprR)\n with (correct exprL) saveDivExprL\n... | mustPT_\u21d3_exL\u27e6exL\u27e7\n with (correct exprR) safeDivExprR\n... | mustPT_\u21d3_exR\u27e6exR\u27e7\n = {!!}\n-}\n{-\n----- another try\ncorrect (Val x) tt =\n \u21d3Base\ncorrect (Div (Val x1) (Val x2)) (r2\u21d3zero\u2192\u22a5 , tt , tt) =\n {!!}\ncorrect (Div (Val x) (Div expr2 expr3)) (r2\u21d3zero\u2192\u22a5 , tt , fst , snd) =\n {!!}\ncorrect (Div (Div expr1 expr3) (Val x)) (r2\u21d3zero\u2192\u22a5 , (fst , snd) , tt) =\n {!!}\ncorrect (Div (Div expr1 expr3) (Div expr2 expr4)) (r2\u21d3zero\u2192\u22a5 , safeDivExpr1 , safeDivExpr2) =\n {!!}\n-}\n\n{-\nInstead of manually defining SafeDiv,\ndefine more general predicate characterising the domain of a partial function:\n-}\n\ndom : \u2200 {a : Set} {b : a \u2192 Set}\n \u2192 ((x : a)\n \u2192 Partial (b x))\n \u2192 (a \u2192 Set)\ndom f = wpPartial f (\u03bb _ _ \u2192 \u22a4)\n\n{-\ncan show that the two semantics agree precisely on the domain of the interpreter:\n\nsound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\ncomplete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\nboth proofs proceed by induction on the argument expression\n\n--------------------------------------------------\nRefinement\n\nweakest precondition semantics on partial computations give rise\nto a refinement relation on Kleisli arrows of the form a \u2192 Partial b\n\ncan characterise this relation by proving:\n\nrefinement : (f g : a \u2192 Maybe b)\n \u2192 (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x \u2192 (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\nuse refinement to relate Kleisli morphisms,\nand to relate a program to a specification given by a pre- and postcondition\n\n--------------------------------------------------\nExample: Add (interpreter for stack machine)\n\nadd top two elements; can fail fail if stack has too few elements\n\nbelow shows how to prove the definition meets its specification\n\nDefine specification in terms of a pre\/post condition.\n-}\n-- specification of a function of type (x : a) \u2192 b x consists of:\nrecord Spec (a : Set) (b : a \u2192 Set) : Set where\n constructor [_,_]\n field\n pre : a \u2192 Set -- precondition on 'a'\n post : (x : a) \u2192 b x \u2192 Set -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n{-\n[ P , Q ] : specification consisting of precondition P and postcondition Q\n\nfor non-dependent examples (e.g., type b does not depend on x : a)\n-}\n\nK : {A B : Set} \u2192 A \u2192 B \u2192 A\nK x y = x\n\nSpecK : Set \u2192 Set \u2192 Set\nSpecK a b = Spec a (K b)\n\n-- specification for addition function : describes the desired postcondition\ndata Add : List Nat \u2192 List Nat \u2192 Set where\n AddStep : {x1 x2 : Nat} {xs : List Nat}\n \u2192 Add (x1 \u2237 x2 \u2237 xs) ((x1 + x2 ) \u2237 xs)\n\naddSpec : SpecK (List Nat) (List Nat)\naddSpec = [ (\u03bb xs \u2192 length xs > 1) , Add ]\n\n{-\nHow to relate this specification to an implementation?\nNote: 'wpPartial' assigns predicate transformer semantics to functions\n- but do not yet have a corresponding predicate transform semantics for specifications.\nwpSpec function does that:\n-}\n\n-- Given a specification, Spec a b\n-- computes the weakest precondition that satisfies an arbitrary postcondition P\n-- i.e., the spec\u2019s precondition should hold and its postcondition must imply P.\nwpSpec : {a : Set} {b : a \u2192 Set}\n \u2192 Spec a b \u2192 (P : (x : a) \u2192 b x \u2192 Set) \u2192 (a \u2192 Set)\nwpSpec [ pre , post ] P = \u03bb x \u2192 (pre x) \u00d7 {-\u2227-} (post x \u2286 P x)\n\n-- Can now formulate program\nadd : List Nat \u2192 Partial (List Nat)\nadd xs =\n pop xs >>= \u03bb { (x1 , xs) \u2192\n pop xs >>= \u03bb { (x2 , xs) \u2192\n return ((x1 + x2 ) \u2237 xs) } }\n where\n pop : {a : Set} \u2192 List a \u2192 Partial (a \u00d7 List a)\n pop Data.List.[] = abort\n pop (x \u2237 xs) = return (x , xs)\n\n-- that refines the specification given by addSpec:\n{-\ncorrectness : wpSpec addSpec \u2291 wpPartial add\ncorrectness addstep xs (pre-addSpec-xs , post-addSpec-xs-\u2286-addstep-xs) =\n {!!}\n-}\n{-\nThis example illustrates how to use the refinement relation\n- to relate a specification\n - given in terms of a pre- and postcondition,\n- to its implementation.\n\nCompared to the refinement calculus\n- have not yet described how to mix code and specifications (see Section 7)\n\n--------------------------------------------------\nAlternative semantics\n-}\n","old_contents":"{-# OPTIONS --type-in-type #-} -- NOT SOUND!\n\nopen import Data.Empty using (\u22a5)\nopen import Data.Nat renaming (\u2115 to Nat)\nopen import Data.Nat.DivMod\nopen import Data.Product hiding (map)\nopen import Data.Unit using (\u22a4)\n------------------------------------------------------------------------------\nopen import Relation.Binary.PropositionalEquality using (_\u2261_; refl)\n\nmodule x where\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\nabstract\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nINTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to\n - use this refinement relation to show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n2 BACKGROUND\n\nFree monads\n-}\n\n-- C : type of commands\n-- Free C R : returns an 'a' or issues command c : C\n-- For each c : C, there is a set of responses R c\n-- 2nd arg of Step is continuation : how to proceed after receiving response R c\ndata Free (C : Set) (R : C \u2192 Set) (a : Set) : Set where\n Pure : a \u2192 Free C R a\n Step : (c : C) \u2192 (R c \u2192 Free C R a) \u2192 Free C R a\n\n-- show that 'Free' is a monad:\n\nmap : \u2200 {a b C : Set} {R : C \u2192 Set} \u2192 (a \u2192 b) \u2192 Free C R a \u2192 Free C R b\nmap f (Pure x) = Pure (f x)\nmap f (Step c k) = Step c (\u03bb r \u2192 map f (k r))\n\nreturn : \u2200 {a C : Set} {R : C \u2192 Set} \u2192 a \u2192 Free C R a\nreturn = Pure\n\n_>>=_ : \u2200 {a b C : Set} {R : C \u2192 Set} \u2192 Free C R a \u2192 (a \u2192 Free C R b) \u2192 Free C R b\nPure x >>= f = f x\nStep c x >>= f = Step c (\u03bb r \u2192 x r >>= f)\n\n{-\ndifferent effects choose C and R differently, depending on their ops\n\nWeakest precondition semantics\n\nidea of associating weakest precondition semantics with imperative programs\ndates to Dijkstra\u2019s Guarded Command Language [1975]\n\nways to specify behaviour of function f : a \u2192 b\n- reference implementation\n- define a relation R : a \u2192 b \u2192 Set\n- write contracts and test cases\n- PT semantics\n\ncall values of type a \u2192 Set : predicate on type a\n\nPTs are functions between predicates\ne.g., weakest precondition:\n-}\n\n-- \"maps\"\n-- function f : a \u2192 b and\n-- desired postcondition on the function\u2019s output, b \u2192 Set\n-- to weakest precondition a \u2192 Set on function\u2019s input that ensures postcondition satisfied\n--\n-- note: definition is just reverse function composition\n-- wp0 : \u2200 {a b : Set} \u2192 (f : a \u2192 b) \u2192 (b \u2192 Set) \u2192 (a \u2192 Set)\nwp0 : \u2200 {a : Set} {b : Set} (f : a \u2192 b) \u2192 (b \u2192 Set) \u2192 (a \u2192 Set)\nwp0 f P = \u03bb x \u2192 P (f x)\n{-\nabove wp semantics is sometimes too restrictive\n- no way to specify that output is related to input\n- fix via making f dependent:\n-}\nwp : \u2200 {a : Set} {b : a \u2192 Set} (f : (x : a) \u2192 b x) \u2192 ((x : a) \u2192 b x \u2192 Set) \u2192 (a \u2192 Set)\nwp f P = \u03bb x \u2192 P x (f x)\n\n-- shorthand for working with predicates and predicates transformers\n_\u2286_ : \u2200 {a : Set} \u2192 (a \u2192 Set) \u2192 (a \u2192 Set) \u2192 Set\nP \u2286 Q = \u2200 x \u2192 P x \u2192 Q x\n\n-- refinement relation defined between PTs\n_\u2291_ : \u2200 {a : Set} {b : a \u2192 Set} \u2192 (pt1 pt2 : ((x : a) \u2192 b x \u2192 Set) \u2192 (a \u2192 Set)) \u2192 Set\u2081\npt1 \u2291 pt2 = \u2200 P \u2192 pt1 P \u2286 pt2 P\n\n{-\nuse refinement relation\n- to relate PT semantics between programs and specifications\n- to show a program satisfies its specification; or\n- to show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\nin pure setting, this refinement relation is not interesting:\nthe refinement relation corresponds to extensional equality between functions:\n\nlemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\nrefinement : \u2200 (f g : a \u2192 b) \u2192 (wp f \u2291 wp g) \u2194 (\u2200 x \u2192 f x \u2261 g x)\n\nthis paper defines PT semantics for Kleisli arrows of form\n\n a \u2192 Free C R b\n\ncould use 'wp' to assign semantics to these computations directly,\nbut typically not interested in syntactic equality between free monads\n\nrather want to study semantics of effectful programs they represent\n\nto define a PT semantics for effects\ndefine a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a \u2192 Set) \u2192 Free C R a \u2192 Set\n\n'pt' def depends on semantics desired for a particulr free monad\n\nCrucially,\nchoice of pt and weakest precondition semantics, wp, together\ngive a way to assign weakest precondition semantics to Kleisli arrows\nrepresenting effectful computations\n\n3 PARTIALITY\n\nPartial computations : i.e., 'Maybe'\n\nmake choices for commands C and responses R\n-}\n\ndata C : Set where\n Abort : C -- no continuation\n\nR : C \u2192 Set\nR Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\nPartial : Set \u2192 Set\nPartial = Free C R\n\n-- smart constructor for failure:\nabort : \u2200 {a : Set} \u2192 Partial a\nabort = Step Abort (\u03bb ())\n\n{-\ncomputation of type Partial a will either\n- return a value of type a or\n- fail, issuing abort command\n\nNote that responses to Abort command are empty;\nabort smart constructor abort uses this to discharge the continuation\nin the second argument of the Step constructor\n\nExample: division\n\nexpression language, closed under division and natural numbers:\n-}\n\ndata Expr : Set where\n Val : Nat \u2192 Expr\n Div : Expr \u2192 Expr \u2192 Expr\n\nexv : Expr\nexv = Val 3\nexd : Expr\nexd = Div (Val 3) (Val 3)\n\n-- semantics specified using inductively defined RELATION:\n-- def rules out erroneous results by requiring the divisor evaluates to non-zero\ndata _\u21d3_ : Expr \u2192 Nat \u2192 Set where\n Base : \u2200 {x : Nat}\n \u2192 Val x \u21d3 x\n Step : \u2200 {l r : Expr} {v1 v2 : Nat}\n \u2192 l \u21d3 v1\n \u2192 r \u21d3 (suc v2)\n \u2192 Div l r \u21d3 (v1 div (suc v2))\n\nexb : Val 3 \u21d3 3\nexb = Base\n\nexs : Div (Val 3) (Val 3) \u21d3 1\nexs = Step Base Base\n\n-- Alternatively\n-- evaluate Expr via monadic INTERPRETER, using Partial to handle division-by-zero\n\n-- op used by \u27e6_\u27e7 interpreter\n_\u00f7_ : Nat \u2192 Nat \u2192 Partial Nat\nn \u00f7 zero = abort\nn \u00f7 (suc k) = return (n div (suc k))\n\n\u27e6_\u27e7 : Expr \u2192 Partial Nat\n\u27e6 Val x \u27e7 = return x\n\u27e6 Div e1 e2 \u27e7 = \u27e6 e1 \u27e7 >>= \u03bb v1 \u2192 \u27e6 e2 \u27e7 >>= \u03bb v2 \u2192 v1 \u00f7 v2\n\nevv : Free C R Nat\nevv = \u27e6 Val 3 \u27e7\nevv' : evv \u2261 Pure 3\nevv' = refl\n\nevd : Free C R Nat\nevd = \u27e6 Div (Val 3) (Val 3) \u27e7\nevd' : evd \u2261 Pure 1\nevd' = refl\n\nevd0 : Free C R Nat\nevd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\nevd0' : evd0 \u2261 Step Abort (\u03bb ())\nevd0' = refl\n\n{-\nHow to relate two definitions:\n- std lib 'div' requires implicit proof that divisor is non-zero\n - \u21d3 relation generates via pattern matching\n - _\u00f7_ does explicit check\n- interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\nAssign a weakest precondition semantics to Kleisli arrows of the form\n\n a \u2192 Partial b\n-}\n\nwpPartial : \u2200 {a : Set} {b : a \u2192 Set}\n \u2192 (f : (x : a) \u2192 Partial (b x))\n \u2192 (P : (x : a) \u2192 b x \u2192 Set)\n \u2192 (a \u2192 Set)\nwpPartial f P = wp f (mustPT P)\n where\n mustPT : \u2200 {a : Set} {b : a \u2192 Set}\n \u2192 (P : (x : a) \u2192 b x \u2192 Set)\n \u2192 (x : a)\n \u2192 Partial (b x) \u2192 Set\n mustPT P _ (Pure y) = P _ y\n mustPT P _ (Step Abort _) = \u22a5\n{-\nTo call 'wp', must show how to transform\n- predicate P : b \u2192 Set\n- to a predicate on partial results : Partial b \u2192 Set\nDone via proposition 'mustPT P c'\n- holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\nparticular PT semantics of partial computations determined by def mustPT\nhere: rule out failure entirely\n- so case Abort returns empty type\n\nGiven this PT semantics for Kleisli arrows in general,\ncan now study semantics of above monadic interpreter\nvia passing\n- interpreter: \u27e6_\u27e7\n- desired postcondition : _\u21d3_\nas arguments to wpPartial:\n\n wpPartial \u27e6_\u27e7 _\u21d3_ : Expr \u2192 Set\n\nresulting in a predicate on expressions\n\nfor all expressions satisfying this predicate,\nthe monadic interpreter and the relational specification, _\u21d3_,\nmust agree on the result of evaluation\n\nWhat does this say about correctness of interpreter?\nTo understand the predicate better, consider defining this predicate on expressions:\n-}\n\nSafeDiv : Expr \u2192 Set\nSafeDiv (Val x) = \u22a4\nSafeDiv (Div e1 e2) = (e2 \u21d3 zero \u2192 \u22a5) {-\u2227-} \u00d7 SafeDiv e1 {-\u2227-} \u00d7 SafeDiv e2\n\n{-\nExpect : any expr e for which SafeDiv e holds\ncan be evaluated without division-by-zero\n\ncan prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n-- lemma relates the two semantics\n-- expressed as a relation and an evaluator\n-- for those expressions that satisfy the SafeDiv property\ncorrect : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\nInstead of manually defining SafeDiv, define more general predicate\ncharacterising the domain of a partial function:\n-}\n\ndom : \u2200 {a : Set} {b : a \u2192 Set}\n \u2192 ((x : a)\n \u2192 Partial (b x))\n \u2192 (a \u2192 Set)\ndom f = wpPartial f (\u03bb _ _ \u2192 \u22a4)\n\n{-\ncan show that the two semantics agree precisely on the domain of the interpreter:\n\nsound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\ncomplete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\nboth proofs proceed by induction on the argument expression\n\nRefinement\n\nweakest precondition semantics on partial computations give rise\nto a refinement relation on Kleisli arrows of the form a \u2192 Partial b\n\ncan characterise this relation by proving:\n\nrefinement : (f g : a \u2192 Maybe b)\n \u2192 (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x \u2192 (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\nuse refinement to relate Kleisli morphisms,\nand to relate a program to a specification given by a pre- and postcondition\n\n\nExample: Add (interpreter for stack machine)\n\nadd top two elements; can fail fail if stack has too few elements\n\nbelow shows how to prove the definition meets its specification\n\nDefine specification in terms of a pre\/post condition.\nThe specification of a function of type (x : a) \u2192 b x consists of\n-}\nrecord Spec (a : Set) (b : a \u2192 Set) : Set where\n constructor [_,_]\n field\n pre : a \u2192 Set -- a precondition on a, and\n post : (x : a) \u2192 b x \u2192 Set -- a postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"71d08e755840c84e86e2846d5b94d249142a7219","subject":"scraps","message":"scraps\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"core.agda","new_file":"core.agda","new_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data \u03c4\u0307 : Set where\n num : \u03c4\u0307\n \u2987\u2988 : \u03c4\u0307\n _==>_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 \u03c4\u0307\n _\u2295_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 \u03c4\u0307\n _\u2297_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 \u03c4\u0307\n\n -- expressions\n data e\u0307 : Set where\n _\u00b7:_ : e\u0307 \u2192 \u03c4\u0307 \u2192 e\u0307\n X : Nat \u2192 e\u0307\n \u00b7\u03bb : Nat \u2192 e\u0307 \u2192 e\u0307\n N : Nat \u2192 e\u0307\n _\u00b7+_ : e\u0307 \u2192 e\u0307 \u2192 e\u0307\n \u2987\u2988[_] : Nat \u2192 e\u0307\n \u2987_\u2988[_] : e\u0307 \u2192 Nat \u2192 e\u0307\n _\u2218_ : e\u0307 \u2192 e\u0307 \u2192 e\u0307\n inl : e\u0307 \u2192 e\u0307\n inr : e\u0307 \u2192 e\u0307\n case : e\u0307 \u2192 Nat \u2192 e\u0307 \u2192 Nat \u2192 e\u0307 \u2192 e\u0307\n [_,_] : e\u0307 \u2192 e\u0307 \u2192 e\u0307\n fst : e\u0307 \u2192 e\u0307\n snd : e\u0307 \u2192 e\u0307\n\n data subst : Set where -- todo; reuse contexts? do we need access to an enumeration of the domain?\n\n -- expressions without ascriptions but with casts\n data \u00eb : Set where\n X : Nat \u2192 \u00eb\n \u00b7\u03bb : Nat \u2192 \u00eb \u2192 \u00eb\n N : Nat \u2192 \u00eb\n _\u00b7+_ : \u00eb \u2192 \u00eb \u2192 \u00eb\n \u2987\u2988[_,_] : Nat \u2192 subst \u2192 \u00eb\n \u2987_\u2988[_,_] : \u00eb \u2192 Nat \u2192 subst \u2192 \u00eb\n _\u2218_ : \u00eb \u2192 \u00eb \u2192 \u00eb\n inl : \u00eb \u2192 \u00eb\n inr : \u00eb \u2192 \u00eb\n case : \u00eb \u2192 Nat \u2192 \u00eb \u2192 Nat \u2192 \u00eb \u2192 \u00eb\n [_,_] : \u00eb \u2192 \u00eb \u2192 \u00eb\n fst : \u00eb \u2192 \u00eb\n snd : \u00eb \u2192 \u00eb\n <_>_ : \u00eb \u2192 \u03c4\u0307 \u2192 \u00eb\n\n -- type consistency\n data _~_ : (t1 : \u03c4\u0307) \u2192 (t2 : \u03c4\u0307) \u2192 Set where\n TCRefl : {t : \u03c4\u0307} \u2192 t ~ t\n TCHole1 : {t : \u03c4\u0307} \u2192 t ~ \u2987\u2988\n TCHole2 : {t : \u03c4\u0307} \u2192 \u2987\u2988 ~ t\n TCArr : {t1 t2 t1' t2' : \u03c4\u0307} \u2192\n t1 ~ t1' \u2192\n t2 ~ t2' \u2192\n (t1 ==> t2) ~ (t1' ==> t2')\n TCSum : {t1 t2 t1' t2' : \u03c4\u0307} \u2192\n t1 ~ t1' \u2192\n t2 ~ t2' \u2192\n (t1 \u2295 t2) ~ (t1' \u2295 t2')\n TCPro : {t1 t2 t1' t2' : \u03c4\u0307} \u2192\n t1 ~ t1' \u2192\n t2 ~ t2' \u2192\n (t1 \u2297 t2) ~ (t1' \u2297 t2')\n\n -- type inconsistency\n data _~\u0338_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n ICNumArr1 : {t1 t2 : \u03c4\u0307} \u2192 num ~\u0338 (t1 ==> t2)\n ICNumArr2 : {t1 t2 : \u03c4\u0307} \u2192 (t1 ==> t2) ~\u0338 num\n ICArr1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t1 ~\u0338 t3 \u2192\n (t1 ==> t2) ~\u0338 (t3 ==> t4)\n ICArr2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t2 ~\u0338 t4 \u2192\n (t1 ==> t2) ~\u0338 (t3 ==> t4)\n\n ICNumSum1 : {t1 t2 : \u03c4\u0307} \u2192 num ~\u0338 (t1 \u2295 t2)\n ICNumSum2 : {t1 t2 : \u03c4\u0307} \u2192 (t1 \u2295 t2) ~\u0338 num\n ICSum1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t1 ~\u0338 t3 \u2192\n (t1 \u2295 t2) ~\u0338 (t3 \u2295 t4)\n ICSum2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t2 ~\u0338 t4 \u2192\n (t1 \u2295 t2) ~\u0338 (t3 \u2295 t4)\n ICSumArr1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192 (t1 \u2295 t2) ~\u0338 (t3 ==> t4)\n ICSumArr2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192 (t1 ==> t2) ~\u0338 (t3 \u2295 t4)\n\n ICNumPro1 : {t1 t2 : \u03c4\u0307} \u2192 num ~\u0338 (t1 \u2297 t2)\n ICNumPro2 : {t1 t2 : \u03c4\u0307} \u2192 (t1 \u2297 t2) ~\u0338 num\n ICPro1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t1 ~\u0338 t3 \u2192\n (t1 \u2297 t2) ~\u0338 (t3 \u2297 t4)\n ICPro2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t2 ~\u0338 t4 \u2192\n (t1 \u2297 t2) ~\u0338 (t3 \u2297 t4)\n ICProArr1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192 (t1 \u2297 t2) ~\u0338 (t3 ==> t4)\n ICProArr2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192 (t1 ==> t2) ~\u0338 (t3 \u2297 t4)\n\n ICProSum1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192 (t1 \u2297 t2) ~\u0338 (t3 \u2295 t4)\n ICProSum2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192 (t1 \u2295 t2) ~\u0338 (t3 \u2297 t4)\n\n\n --- matching for arrows, sums, and products\n data _\u25b8arr_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr (\u2987\u2988 ==> \u2987\u2988)\n MAArr : {t1 t2 : \u03c4\u0307} \u2192 (t1 ==> t2) \u25b8arr (t1 ==> t2)\n\n data _\u25b8sum_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n MSHole : \u2987\u2988 \u25b8sum (\u2987\u2988 \u2295 \u2987\u2988)\n MSPlus : {t1 t2 : \u03c4\u0307} \u2192 (t1 \u2295 t2) \u25b8sum (t1 \u2295 t2)\n\n data _\u25b8pro_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n MPHole : \u2987\u2988 \u25b8pro (\u2987\u2988 \u2297 \u2987\u2988)\n MPPlus : {t1 t2 : \u03c4\u0307} \u2192 (t1 \u2297 t2) \u25b8pro (t1 \u2297 t2)\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = \u03c4\u0307 ctx\n\n hctx : Set\n hctx = (\u03c4\u0307 ctx \u00d7 \u03c4\u0307) ctx\n\n -- bidirectional type checking judgements for e\u0307\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) \u2192 (e : e\u0307) \u2192 (t : \u03c4\u0307) \u2192 Set where\n SAsc : {\u0393 : tctx} {e : e\u0307} {t : \u03c4\u0307} \u2192\n \u0393 \u22a2 e <= t \u2192\n \u0393 \u22a2 (e \u00b7: t) => t\n SVar : {\u0393 : tctx} {t : \u03c4\u0307} {n : Nat} \u2192\n (n , t) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => t\n SAp : {\u0393 : tctx} {e1 e2 : e\u0307} {t t' t2 : \u03c4\u0307} \u2192\n \u0393 \u22a2 e1 => t \u2192\n t \u25b8arr (t2 ==> t') \u2192\n \u0393 \u22a2 e2 <= t2 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => t'\n SNum : {\u0393 : tctx} {n : Nat} \u2192\n \u0393 \u22a2 N n => num\n SPlus : {\u0393 : tctx} {e1 e2 : e\u0307} \u2192\n \u0393 \u22a2 e1 <= num \u2192\n \u0393 \u22a2 e2 <= num \u2192\n \u0393 \u22a2 (e1 \u00b7+ e2) => num\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of n?\n SNEHole : {\u0393 : tctx} {e : e\u0307} {t : \u03c4\u0307} {u : Nat} \u2192 -- todo: uniqueness of n?\n \u0393 \u22a2 e => t \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n\n --todo: add rules for products in both jugements\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : \u03c4\u0307 ctx) \u2192 (e : e\u0307) \u2192 (t : \u03c4\u0307) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : e\u0307} {t t' : \u03c4\u0307} \u2192\n \u0393 \u22a2 e => t' \u2192\n t ~ t' \u2192\n \u0393 \u22a2 e <= t\n ALam : {\u0393 : tctx} {e : e\u0307} {t t1 t2 : \u03c4\u0307} {x : Nat} \u2192\n x # \u0393 \u2192\n t \u25b8arr (t1 ==> t2) \u2192\n (\u0393 ,, (x , t1)) \u22a2 e <= t2 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= t\n AInl : {\u0393 : tctx} {e : e\u0307} {t+ t1 t2 : \u03c4\u0307} \u2192\n t+ \u25b8sum (t1 \u2295 t2) \u2192\n \u0393 \u22a2 e <= t1 \u2192\n \u0393 \u22a2 inl e <= t+\n AInr : {\u0393 : tctx} {e : e\u0307} {t+ t1 t2 : \u03c4\u0307} \u2192\n t+ \u25b8sum (t1 \u2295 t2) \u2192\n \u0393 \u22a2 e <= t2 \u2192\n \u0393 \u22a2 inr e <= t+\n ACase : {\u0393 : tctx} {e e1 e2 : e\u0307} {t t+ t1 t2 : \u03c4\u0307} {x y : Nat} \u2192\n x # \u0393 \u2192\n y # \u0393 \u2192\n t+ \u25b8sum (t1 \u2295 t2) \u2192\n \u0393 \u22a2 e => t+ \u2192\n (\u0393 ,, (x , t1)) \u22a2 e1 <= t \u2192\n (\u0393 ,, (y , t2)) \u22a2 e2 <= t \u2192\n \u0393 \u22a2 case e x e1 y e2 <= t\n\n -- those types without holes anywhere\n tcomplete : \u03c4\u0307 \u2192 Set\n tcomplete num = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (t1 ==> t2) = tcomplete t1 \u00d7 tcomplete t2\n tcomplete (t1 \u2295 t2) = tcomplete t1 \u00d7 tcomplete t2\n tcomplete (t1 \u2297 t2) = tcomplete t1 \u00d7 tcomplete t2\n\n -- those expressions without holes anywhere\n ecomplete : e\u0307 \u2192 Set\n ecomplete (e1 \u00b7: t) = ecomplete e1 \u00d7 tcomplete t\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete (N x) = \u22a4\n ecomplete (e1 \u00b7+ e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (inl e) = ecomplete e\n ecomplete (inr e) = ecomplete e\n ecomplete (case e x e1 y e2) = ecomplete e \u00d7 ecomplete e1 \u00d7 ecomplete e2\n ecomplete [ e1 , e2 ] = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (fst e) = ecomplete e\n ecomplete (snd e) = ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : e\u0307) (t : \u03c4\u0307) (e' : \u00eb) (\u0394 : hctx) \u2192 Set where\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : e\u0307) (t : \u03c4\u0307) (e' : \u00eb) (t' : \u03c4\u0307)(\u0394 : hctx) \u2192 Set where\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (e' : \u00eb) (t : \u03c4\u0307) \u2192 Set where\n\n -- value\n data _val : \u00eb \u2192 Set where\n\n -- indeterminate\n data _indet : \u00eb \u2192 Set where\n\n -- error\n data _err[_] : \u00eb \u2192 hctx \u2192 Set where -- todo not a context\n\n -- final\n data _final : \u00eb \u2192 Set where\n\n -- small step semantics\n data _\u21a6_ : \u00eb \u2192 \u00eb \u2192 Set where\n","old_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data \u03c4\u0307 : Set where\n num : \u03c4\u0307\n \u2987\u2988 : \u03c4\u0307\n _==>_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 \u03c4\u0307\n _\u2295_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 \u03c4\u0307\n _\u2297_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 \u03c4\u0307\n\n -- expressions\n data e\u0307 : Set where\n _\u00b7:_ : e\u0307 \u2192 \u03c4\u0307 \u2192 e\u0307\n X : Nat \u2192 e\u0307\n \u00b7\u03bb : Nat \u2192 e\u0307 \u2192 e\u0307\n N : Nat \u2192 e\u0307\n _\u00b7+_ : e\u0307 \u2192 e\u0307 \u2192 e\u0307\n \u2987\u2988[_] : Nat \u2192 e\u0307\n \u2987_\u2988[_] : e\u0307 \u2192 Nat \u2192 e\u0307\n _\u2218_ : e\u0307 \u2192 e\u0307 \u2192 e\u0307\n inl : e\u0307 \u2192 e\u0307\n inr : e\u0307 \u2192 e\u0307\n case : e\u0307 \u2192 Nat \u2192 e\u0307 \u2192 Nat \u2192 e\u0307 \u2192 e\u0307\n [_,_] : e\u0307 \u2192 e\u0307 \u2192 e\u0307\n fst : e\u0307 \u2192 e\u0307\n snd : e\u0307 \u2192 e\u0307\n\n data subst : Set where -- todo\n\n -- expressions without ascriptions but with casts\n data \u00eb : Set where\n X : Nat \u2192 \u00eb\n \u00b7\u03bb : Nat \u2192 \u00eb \u2192 \u00eb\n N : Nat \u2192 \u00eb\n _\u00b7+_ : \u00eb \u2192 \u00eb \u2192 \u00eb\n \u2987\u2988[_,_] : Nat \u2192 subst \u2192 \u00eb\n \u2987_\u2988[_,_] : \u00eb \u2192 Nat \u2192 subst \u2192 \u00eb\n _\u2218_ : \u00eb \u2192 \u00eb \u2192 \u00eb\n inl : \u00eb \u2192 \u00eb\n inr : \u00eb \u2192 \u00eb\n case : \u00eb \u2192 Nat \u2192 \u00eb \u2192 Nat \u2192 \u00eb \u2192 \u00eb\n [_,_] : \u00eb \u2192 \u00eb \u2192 \u00eb\n fst : \u00eb \u2192 \u00eb\n snd : \u00eb \u2192 \u00eb\n <_>_ : \u00eb \u2192 \u03c4\u0307 \u2192 \u00eb\n\n -- type consistency\n data _~_ : (t1 : \u03c4\u0307) \u2192 (t2 : \u03c4\u0307) \u2192 Set where\n TCRefl : {t : \u03c4\u0307} \u2192 t ~ t\n TCHole1 : {t : \u03c4\u0307} \u2192 t ~ \u2987\u2988\n TCHole2 : {t : \u03c4\u0307} \u2192 \u2987\u2988 ~ t\n TCArr : {t1 t2 t1' t2' : \u03c4\u0307} \u2192\n t1 ~ t1' \u2192\n t2 ~ t2' \u2192\n (t1 ==> t2) ~ (t1' ==> t2')\n TCSum : {t1 t2 t1' t2' : \u03c4\u0307} \u2192\n t1 ~ t1' \u2192\n t2 ~ t2' \u2192\n (t1 \u2295 t2) ~ (t1' \u2295 t2')\n TCPro : {t1 t2 t1' t2' : \u03c4\u0307} \u2192\n t1 ~ t1' \u2192\n t2 ~ t2' \u2192\n (t1 \u2297 t2) ~ (t1' \u2297 t2')\n\n -- type inconsistency\n data _~\u0338_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n ICNumArr1 : {t1 t2 : \u03c4\u0307} \u2192 num ~\u0338 (t1 ==> t2)\n ICNumArr2 : {t1 t2 : \u03c4\u0307} \u2192 (t1 ==> t2) ~\u0338 num\n ICArr1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t1 ~\u0338 t3 \u2192\n (t1 ==> t2) ~\u0338 (t3 ==> t4)\n ICArr2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t2 ~\u0338 t4 \u2192\n (t1 ==> t2) ~\u0338 (t3 ==> t4)\n\n ICNumSum1 : {t1 t2 : \u03c4\u0307} \u2192 num ~\u0338 (t1 \u2295 t2)\n ICNumSum2 : {t1 t2 : \u03c4\u0307} \u2192 (t1 \u2295 t2) ~\u0338 num\n ICSum1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t1 ~\u0338 t3 \u2192\n (t1 \u2295 t2) ~\u0338 (t3 \u2295 t4)\n ICSum2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t2 ~\u0338 t4 \u2192\n (t1 \u2295 t2) ~\u0338 (t3 \u2295 t4)\n ICSumArr1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192 (t1 \u2295 t2) ~\u0338 (t3 ==> t4)\n ICSumArr2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192 (t1 ==> t2) ~\u0338 (t3 \u2295 t4)\n\n ICNumPro1 : {t1 t2 : \u03c4\u0307} \u2192 num ~\u0338 (t1 \u2297 t2)\n ICNumPro2 : {t1 t2 : \u03c4\u0307} \u2192 (t1 \u2297 t2) ~\u0338 num\n ICPro1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t1 ~\u0338 t3 \u2192\n (t1 \u2297 t2) ~\u0338 (t3 \u2297 t4)\n ICPro2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t2 ~\u0338 t4 \u2192\n (t1 \u2297 t2) ~\u0338 (t3 \u2297 t4)\n ICProArr1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192 (t1 \u2297 t2) ~\u0338 (t3 ==> t4)\n ICProArr2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192 (t1 ==> t2) ~\u0338 (t3 \u2297 t4)\n\n ICProSum1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192 (t1 \u2297 t2) ~\u0338 (t3 \u2295 t4)\n ICProSum2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192 (t1 \u2295 t2) ~\u0338 (t3 \u2297 t4)\n\n\n --- matching for arrows, sums, and products\n data _\u25b8arr_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr (\u2987\u2988 ==> \u2987\u2988)\n MAArr : {t1 t2 : \u03c4\u0307} \u2192 (t1 ==> t2) \u25b8arr (t1 ==> t2)\n\n data _\u25b8sum_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n MSHole : \u2987\u2988 \u25b8sum (\u2987\u2988 \u2295 \u2987\u2988)\n MSPlus : {t1 t2 : \u03c4\u0307} \u2192 (t1 \u2295 t2) \u25b8sum (t1 \u2295 t2)\n\n data _\u25b8pro_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n MPHole : \u2987\u2988 \u25b8pro (\u2987\u2988 \u2297 \u2987\u2988)\n MPPlus : {t1 t2 : \u03c4\u0307} \u2192 (t1 \u2297 t2) \u25b8pro (t1 \u2297 t2)\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = \u03c4\u0307 ctx\n\n hctx : Set\n hctx = (\u03c4\u0307 ctx \u00d7 \u03c4\u0307) ctx\n\n -- bidirectional type checking judgements for e\u0307\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) \u2192 (e : e\u0307) \u2192 (t : \u03c4\u0307) \u2192 Set where\n SAsc : {\u0393 : tctx} {e : e\u0307} {t : \u03c4\u0307} \u2192\n \u0393 \u22a2 e <= t \u2192\n \u0393 \u22a2 (e \u00b7: t) => t\n SVar : {\u0393 : tctx} {t : \u03c4\u0307} {n : Nat} \u2192\n (n , t) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => t\n SAp : {\u0393 : tctx} {e1 e2 : e\u0307} {t t' t2 : \u03c4\u0307} \u2192\n \u0393 \u22a2 e1 => t \u2192\n t \u25b8arr (t2 ==> t') \u2192\n \u0393 \u22a2 e2 <= t2 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => t'\n SNum : {\u0393 : tctx} {n : Nat} \u2192\n \u0393 \u22a2 N n => num\n SPlus : {\u0393 : tctx} {e1 e2 : e\u0307} \u2192\n \u0393 \u22a2 e1 <= num \u2192\n \u0393 \u22a2 e2 <= num \u2192\n \u0393 \u22a2 (e1 \u00b7+ e2) => num\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of n?\n SNEHole : {\u0393 : tctx} {e : e\u0307} {t : \u03c4\u0307} {u : Nat} \u2192 -- todo: uniqueness of n?\n \u0393 \u22a2 e => t \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n\n --todo: add rules for products in both jugements\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : \u03c4\u0307 ctx) \u2192 (e : e\u0307) \u2192 (t : \u03c4\u0307) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : e\u0307} {t t' : \u03c4\u0307} \u2192\n \u0393 \u22a2 e => t' \u2192\n t ~ t' \u2192\n \u0393 \u22a2 e <= t\n ALam : {\u0393 : tctx} {e : e\u0307} {t t1 t2 : \u03c4\u0307} {x : Nat} \u2192\n x # \u0393 \u2192\n t \u25b8arr (t1 ==> t2) \u2192\n (\u0393 ,, (x , t1)) \u22a2 e <= t2 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= t\n AInl : {\u0393 : tctx} {e : e\u0307} {t+ t1 t2 : \u03c4\u0307} \u2192\n t+ \u25b8sum (t1 \u2295 t2) \u2192\n \u0393 \u22a2 e <= t1 \u2192\n \u0393 \u22a2 inl e <= t+\n AInr : {\u0393 : tctx} {e : e\u0307} {t+ t1 t2 : \u03c4\u0307} \u2192\n t+ \u25b8sum (t1 \u2295 t2) \u2192\n \u0393 \u22a2 e <= t2 \u2192\n \u0393 \u22a2 inr e <= t+\n ACase : {\u0393 : tctx} {e e1 e2 : e\u0307} {t t+ t1 t2 : \u03c4\u0307} {x y : Nat} \u2192\n x # \u0393 \u2192\n y # \u0393 \u2192\n t+ \u25b8sum (t1 \u2295 t2) \u2192\n \u0393 \u22a2 e => t+ \u2192\n (\u0393 ,, (x , t1)) \u22a2 e1 <= t \u2192\n (\u0393 ,, (y , t2)) \u22a2 e2 <= t \u2192\n \u0393 \u22a2 case e x e1 y e2 <= t\n\n -- those types without holes anywhere\n tcomplete : \u03c4\u0307 \u2192 Set\n tcomplete num = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (t1 ==> t2) = tcomplete t1 \u00d7 tcomplete t2\n tcomplete (t1 \u2295 t2) = tcomplete t1 \u00d7 tcomplete t2\n tcomplete (t1 \u2297 t2) = tcomplete t1 \u00d7 tcomplete t2\n\n -- those expressions without holes anywhere\n ecomplete : e\u0307 \u2192 Set\n ecomplete (e1 \u00b7: t) = ecomplete e1 \u00d7 tcomplete t\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete (N x) = \u22a4\n ecomplete (e1 \u00b7+ e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (inl e) = ecomplete e\n ecomplete (inr e) = ecomplete e\n ecomplete (case e x e1 y e2) = ecomplete e \u00d7 ecomplete e1 \u00d7 ecomplete e2\n ecomplete [ e1 , e2 ] = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (fst e) = ecomplete e\n ecomplete (snd e) = ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : e\u0307) (t : \u03c4\u0307) (e' : \u00eb) (\u0394 : hctx) \u2192 Set where\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : e\u0307) (t : \u03c4\u0307) (e' : \u00eb) (t' : \u03c4\u0307)(\u0394 : hctx) \u2192 Set where\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (e' : \u00eb) (t : \u03c4\u0307) \u2192 Set where\n\n -- value\n data _val : \u00eb \u2192 Set where\n\n -- indeterminate\n data _indet : \u00eb \u2192 Set where\n\n -- error\n data _err[_] : \u00eb \u2192 hctx \u2192 Set where -- todo not a context\n\n -- final\n data _final : \u00eb \u2192 Set where\n\n -- small step semantics\n data _\u21a6_ : \u00eb \u2192 \u00eb \u2192 Set where\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"464a985aa70ecceb7a7917d62fc7cb3e05dd59c2","subject":"Update Desc model. Follow ICFP paper. With type-in-type, but termination-checker-friendly.","message":"Update Desc model. Follow ICFP paper. With type-in-type, but termination-checker-friendly.","repos":"kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram,kwangkim\/pigment","old_file":"models\/Desc.agda","new_file":"models\/Desc.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule Desc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata Desc : Set where\n id : Desc\n const : Set -> Desc\n prod : Desc -> Desc -> Desc\n sigma : (S : Set) -> (S -> Desc) -> Desc\n pi : (S : Set) -> (S -> Desc) -> Desc\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|]_ : Desc -> Set -> Set\n[| id |] Z = Z\n[| const X |] Z = X\n[| prod D D' |] Z = [| D |] Z * [| D' |] Z\n[| sigma S T |] Z = Sigma S (\\s -> [| T s |] Z)\n[| pi S T |] Z = (s : S) -> [| T s |] Z\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata Mu (D : Desc) : Set where\n con : [| D |] (Mu D) -> Mu D\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : (D : Desc)(X : Set)(P : X -> Set) -> [| D |] X -> Set\nAll id X P x = P x\nAll (const Z) X P x = Z\nAll (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')\nAll (sigma S T) X P (a , b) = All (T a) X P b\nAll (pi S T) X P f = (s : S) -> All (T s) X P (f s)\n\nall : (D : Desc)(X : Set)(P : X -> Set)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x\nall id X P R x = R x\nall (const Z) X P R z = z\nall (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'\nall (sigma S T) X P R (a , b) = all (T a) X P R b\nall (pi S T) X P R f = \\ s -> all (T s) X P R (f s)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n{-\ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms (con xs) = ms xs (all D (Mu D) P (\\x -> induction D P ms x) xs)\n-}\n\nmodule Elim (D : Desc)\n (P : Mu D -> Set)\n (ms : (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x))\n where\n\n mutual\n induction : (x : Mu D) -> P x\n induction (con xs) = ms xs (hyps D xs)\n \n hyps : (D' : Desc)\n (xs : [| D' |] (Mu D)) ->\n All D' (Mu D) P xs\n hyps id x = induction x\n hyps (const Z) z = z\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (sigma S T) (a , b) = hyps (T a) b\n hyps (pi S T) f = \\s -> hyps (T s) (f s)\n \ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms x = Elim.induction D P ms x\n\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Suc : NatConst\n\nnatCases : NatConst -> Desc\nnatCases Ze = const Unit\nnatCases Suc = id\n\nNatD : Desc\nNatD = sigma NatConst natCases\n\nNat : Set\nNat = Mu NatD\n\nze : Nat\nze = con (Ze , Void)\n\nsuc : Nat -> Nat\nsuc n = con (Suc , n)\n\n--****************\n-- List\n--****************\n\ndata ListConst : Set where\n Nil : ListConst\n Cons : ListConst\n\nlistCases : Set -> ListConst -> Desc\nlistCases X Nil = const Unit\nlistCases X Cons = sigma X (\\_ -> id)\n\nListD : Set -> Desc\nListD X = sigma ListConst (listCases X)\n\nList : Set -> Set\nList X = Mu (ListD X)\n\nnil : {X : Set} -> List X\nnil = con ( Nil , Void )\n\ncons : {X : Set} -> X -> List X -> List X\ncons x t = con ( Cons , ( x , t ))\n\n--****************\n-- Tree\n--****************\n\ndata TreeConst : Set where\n Leaf : TreeConst\n Node : TreeConst\n\ntreeCases : Set -> TreeConst -> Desc\ntreeCases X Leaf = const Unit\ntreeCases X Node = sigma X (\\_ -> prod id id)\n\nTreeD : Set -> Desc\nTreeD X = sigma TreeConst (treeCases X)\n\nTree : Set -> Set\nTree X = Mu (TreeD X)\n\nleaf : {X : Set} -> Tree X\nleaf = con (Leaf , Void)\n\nnode : {X : Set} -> X -> Tree X -> Tree X -> Tree X\nnode x le ri = con (Node , (x , (le , ri)))\n\n--********************************************\n-- Finite sets\n--********************************************\n\nEnumU : Set\nEnumU = Nat\n\nnilE : EnumU\nnilE = ze\n\nconsE : EnumU -> EnumU\nconsE e = suc e\n\n{-\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n-}\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\ncasesSpi : (xs : [| NatD |] Nat) -> \n All NatD Nat (\\e -> (P' : EnumT e -> Set) -> Set) xs -> \n (P' : EnumT (con xs) -> Set) -> Set\ncasesSpi (Ze , Void) hs P' = Unit\ncasesSpi (Suc , n) hs P' = P' EZe * hs (\\e -> P' (ESu e))\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi e P = induction NatD (\\e -> (P : EnumT e -> Set) -> Set) casesSpi e P\n\n{-\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n-}\n\n\ncasesSwitch : (xs : [| NatD |] Nat) ->\n All NatD Nat (\\e -> (P' : EnumT e -> Set)(b' : spi e P')(x' : EnumT e) -> P' x') xs ->\n (P' : EnumT (con xs) -> Set)(b' : spi (con xs) P')(x' : EnumT (con xs)) -> P' x'\ncasesSwitch (Ze , Void) hs P' b' ()\ncasesSwitch (Suc , n) hs P' b' EZe = fst b'\ncasesSwitch (Suc , n) hs P' b' (ESu e') = hs (\\e -> P' (ESu e)) (snd b') e'\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch e P b x = induction NatD (\\e -> (P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x) casesSwitch e P b x\n\n{-\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n-}\n\n--********************************************\n-- Tagged description\n--********************************************\n\nTagDesc : Set\nTagDesc = Sigma EnumU (\\e -> spi e (\\_ -> Desc))\n\ntoDesc : TagDesc -> Desc\ntoDesc (B , F) = sigma (EnumT B) (\\e -> switch B (\\_ -> Desc) F e)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (D : Desc)\n (T : Set) ->\n ([| D |] T -> T) ->\n (Mu D) -> T\ncata D T phi x = induction D (\\_ -> T) (\\x ms -> phi (replace D T x ms )) x\n where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\\_ -> T) xs) -> [| D' |] T\n replace id T x y = y\n replace (const Z) T z z' = z'\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : TagDesc -> (X : Set) -> TagDesc\n(e , D) ** X = consE e , (const X , D)\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : (D : TagDesc)(X Y : Set) ->\n (X -> Mu (toDesc (D ** Y))) ->\n [| toDesc (D ** X) |] (Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\napply (E , B) X Y sig (EZe , x) = sig x\napply (E , B) X Y sig (ESu n , t) = con (ESu n , t)\n\nsubst : (D : TagDesc)(X Y : Set) ->\n Mu (toDesc (D ** X)) ->\n (X -> Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\nsubst D X Y x sig = cata (toDesc (D ** X)) (Mu (toDesc (D ** Y))) (apply D X Y sig) x\n","old_contents":"{-# OPTIONS --type-in-type\n --no-termination-check\n --no-positivity-check #-}\n\nmodule Desc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\nrecord One : Set where\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n\n\n--********************************************\n-- Desc code\n--********************************************\n\n-- Inductive types are implemented as a Universe. Hence, in this\n-- section, we implement their code.\n\n-- We can read this code as follow (see Conor's \"Ornamental\n-- algebras\"): a description in |Desc| is a program to read one node\n-- of the described tree.\n\ndata Desc : Set where\n Arg : (X : Set) -> (X -> Desc) -> Desc\n -- Read a field in |X|; continue, given its value\n\n -- Often, |X| is an |EnumT|, hence allowing to choose a constructor\n -- among a finite set of constructors\n\n Ind : (H : Set) -> Desc -> Desc\n -- Read a field in H; read a recursive subnode given the field,\n -- continue regarless of the subnode\n\n -- Often, |H| is |1|, hence |Ind| simplifies to |Inf : Desc -> Desc|,\n -- meaning: read a recursive subnode and continue regardless\n\n Done : Desc\n -- Stop reading\n\n\n--********************************************\n-- Desc decoder\n--********************************************\n\n-- Provided the type of the recursive subnodes |R|, we decode a\n-- description as a record describing the node.\n\n[|_|]_ : Desc -> Set -> Set\n[| Arg A D |] R = Sigma A (\\ a -> [| D a |] R)\n[| Ind H D |] R = (H -> R) * [| D |] R\n[| Done |] R = One\n\n\n--********************************************\n-- Functions on codes\n--********************************************\n\n-- Saying that a \"predicate\" |p| holds everywhere in |v| amounts to\n-- write something of the following type:\n\nEverywhere : (d : Desc) (D : Set) (bp : D -> Set) (V : [| d |] D) -> Set\nEverywhere (Arg A f) d p v = Everywhere (f (fst v)) d p (snd v)\n-- It must hold for this constructor\n\nEverywhere (Ind H x) d p v = ((y : H) -> p (fst v y)) * Everywhere x d p (snd v)\n-- It must hold for the subtrees \n\nEverywhere Done _ _ _ = _\n-- It trivially holds at endpoints\n\n-- Then, we can build terms that inhabits this type. That is, a\n-- function that takes a \"predicate\" |bp| and makes it hold everywhere\n-- in the data-structure. It is the equivalent of a \"map\", but in a\n-- dependently-typed setting.\n\neverywhere : (d : Desc) (D : Set) (bp : D -> Set) ->\n ((y : D) -> bp y) -> (v : [| d |] D) ->\n Everywhere d D bp v\neverywhere (Arg a f) d bp p v = everywhere (f (fst v)) d bp p (snd v)\n-- It holds everywhere on this constructor\n\neverywhere (Ind H x) d bp p v = (\\y -> p (fst v y)) , everywhere x d bp p (snd v)\n-- It holds here, and down in the recursive subtrees\n\neverywhere Done _ _ _ _ = One\n-- Nothing needs to be done on endpoints\n\n\n-- Looking at the decoder, a natural thing to do is to define its\n-- fixpoint |Mu D|, hence instantiating |R| with |Mu D| itself.\n\ndata Mu (D : Desc) : Set where\n Con : [| D |] (Mu D) -> Mu D\n\n-- Using the \"map\" defined by |everywhere|, we can implement a \"fold\"\n-- over the |Mu| fixpoint:\n\nfoldDesc : (D : Desc) (bp : Mu D -> Set) ->\n ((x : [| D |] (Mu D)) -> Everywhere D (Mu D) bp x -> bp (Con x)) ->\n (v : Mu D) ->\n bp v\nfoldDesc D bp p (Con v) = p v (everywhere D (Mu D) bp (\\x -> foldDesc D bp p x) v) \n\n\n--********************************************\n-- Nat\n--********************************************\n\ndata NatConst : Set where\n ZE : NatConst\n SU : NatConst\n\nnatc : NatConst -> Desc\nnatc ZE = Done\nnatc SU = Ind One Done\n\nnatd : Desc\nnatd = Arg NatConst natc\n\nnat : Set\nnat = Mu natd\n\nzero : nat\nzero = Con ( ZE , _ )\n\nsuc : nat -> nat\nsuc n = Con ( SU , ( (\\_ -> n) , _ ) )\n\ntwo : nat\ntwo = suc (suc zero)\n\nfour : nat\nfour = suc (suc (suc (suc zero)))\n\nsum : nat -> \n ((x : Sigma NatConst (\\ a -> [| natc a |] Mu (Arg NatConst natc))) ->\n Everywhere (natc (fst x)) (Mu (Arg NatConst natc)) (\\ _ -> Mu (Arg NatConst natc)) (snd x) ->\n Mu (Arg NatConst natc))\nsum n2 (ZE , _) p = n2\nsum n2 (SU , f) p = suc ( fst p _) \n\n\nplus : nat -> nat -> nat\nplus n1 n2 = foldDesc natd (\\_ -> nat) (sum n2) n1 \n\nx : nat\nx = plus two two\n\n\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"08b4ad7acb56f1425bfb337f9989dcc1ae4d2a5b","subject":"agda: start boilerplate for derive-term-correct (if ...) (#25)","message":"agda: start boilerplate for derive-term-correct (if ...) (#25)\n\nThe normalized goal is rather huge, suggesting this proof will be\nrather big, like the derivation of if. Separate lemmas will probably\nbe needed.\n\nOld-commit-hash: 487d21a8c626e591fe5804201a1109b104e97426\n","repos":"inc-lc\/ilc-agda","old_file":"total.agda","new_file":"total.agda","new_contents":"","old_contents":"","returncode":0,"stderr":"unknown","license":"mit","lang":"Agda"} {"commit":"67653281f7cca9492f6f7c442a4d08a8909fd342","subject":"progress progress #3","message":"progress progress #3\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress.agda","new_file":"progress.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import lemmas-ground\n\nopen import progress-checks\n\nopen import canonical-boxed-forms\nopen import canonical-value-forms\nopen import canonical-indeterminate-forms\n\nopen import ground-decidable\nopen import htype-decidable\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d boxedval + d indet + \u03a3[ d' \u2208 dhexp ] (d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n BV : \u2200{d \u0394} \u2192 d boxedval \u2192 ok d \u0394\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = BV (BVVal VConst)\n\n -- variables\n progress (TAVar x\u2081) = abort (somenotnone (! x\u2081))\n\n -- lambdas\n progress (TALam wt) = BV (BVVal VLam)\n\n -- applications\n progress (TAAp wt1 wt2)\n with progress wt1 | progress wt2\n -- if the left steps, the whole thing steps\n progress (TAAp wt1 wt2) | S (_ , Step x y z) | _ = S (_ , Step (FHAp1 x) y (FHAp1 z))\n -- if the left is indeterminate, step the right\n progress (TAAp wt1 wt2) | I i | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n -- if they're both indeterminate, step when the cast steps and indet otherwise\n progress (TAAp wt1 wt2) | I x | I x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | I y | CIFACast (_ , _ , _ , _ , _ , refl , _ , _ ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | I y | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n -- if the left is indetermiante but the right is a value\n progress (TAAp wt1 wt2) | I x | BV x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | BV y | CIFACast (_ , _ , _ , _ , _ , refl , _ , _ ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | BV y | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n -- if the left is a boxed value, inspect the right\n progress (TAAp wt1 wt2) | BV v | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n progress (TAAp wt1 wt2) | BV v | I i\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (_ , _ , refl , _) = S (_ , Step FHOuter ITLam FHOuter)\n ... | Inr (_ , _ , _ , refl , _ , _) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | BV v | BV v\u2082\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (_ , _ , refl , _) = S (_ , Step FHOuter ITLam FHOuter)\n ... | Inr (_ , _ , _ , refl , _ , _) = S (_ , Step FHOuter ITApCast FHOuter)\n\n -- empty holes\n progress (TAEHole x x\u2081) = I IEHole\n\n -- nonempty holes\n progress (TANEHole xin wt x\u2081)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHNEHole x) y (FHNEHole z))\n ... | I x = I (INEHole (FIndet x))\n ... | BV x = I (INEHole (FBoxed x))\n\n -- casts\n progress (TACast wt con)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHCast x) y (FHCast z))\n -- indet cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | I x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast {\u03c41 = \u03c41} wt TCHole1) | I x\n with \u03c41\n progress (TACast wt TCHole1) | I x | b = I (ICastGroundHole GBase x)\n progress (TACast wt TCHole1) | I x | \u2987\u2988 = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole1) | I x | \u03c411 ==> \u03c412\n with ground-decidable (\u03c411 ==> \u03c412)\n progress (TACast wt TCHole1) | I x\u2081 | .\u2987\u2988 ==> .\u2987\u2988 | Inl GHole = I (ICastGroundHole GHole x\u2081)\n progress (TACast wt TCHole1) | I x\u2081 | \u03c411 ==> \u03c412 | Inr x = S (_ , Step FHOuter (ITGround (MGArr (ground-arr-not-hole x))) FHOuter)\n progress (TACast wt (TCHole2 {b})) | I x\n with canonical-indeterminate-forms-hole wt x\n progress (TACast wt (TCHole2 {b})) | I x | Inl (_ , _ , _ , refl , f) = I (ICastHoleGround (\u03bb _ _ ()) x GBase)\n progress (TACast wt (TCHole2 {b})) | I x | Inr (Inl (_ , _ , _ , _ , _ , refl , _ , _ , _)) = I (ICastHoleGround (\u03bb _ _ ()) x GBase)\n progress (TACast wt (TCHole2 {b})) | I x | Inr (Inr (Inl (_ , _ , _ , refl , _ , _ , _ , _ , _))) = I (ICastHoleGround (\u03bb _ _ ()) x GBase)\n progress (TACast wt (TCHole2 {b})) | I x | Inr (Inr (Inr (_ , \u03c4 , refl , _ , _)))\n with htype-dec \u03c4 b\n progress (TACast wt (TCHole2 {b})) | I x\u2081 | Inr (Inr (Inr (_ , ._ , refl , gnd , _))) | Inl refl = S (_ , Step FHOuter (ITCastSucceed gnd) FHOuter)\n progress (TACast wt (TCHole2 {b})) | I x\u2081 | Inr (Inr (Inr (_ , _ , refl , gnd , _))) | Inr neq = S (_ , Step FHOuter (ITCastFail gnd GBase neq) FHOuter)\n\n progress (TACast wt (TCHole2 {\u2987\u2988})) | I x = S (_ , Step FHOuter ITCastID FHOuter)\n\n progress (TACast wt (TCHole2 {\u03c411 ==> \u03c412})) | I x\n with ground-decidable (\u03c411 ==> \u03c412)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x\u2081 | Inl GHole\n with canonical-indeterminate-forms-hole wt x\u2081\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | Inl (_ , _ , _ , refl , _) = I (ICastHoleGround (\u03bb _ _ ()) x GHole)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | Inr (Inl (_ , _ , _ , _ , _ , refl , _ , _ , _)) = I (ICastHoleGround (\u03bb _ _ ()) x GHole)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | Inr (Inr (Inl (_ , _ , _ , refl , _ , _ , _ , _ , _))) = I (ICastHoleGround (\u03bb _ _ ()) x GHole)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | Inr (Inr (Inr (_ , ._ , refl , GBase , _))) = S (_ , Step FHOuter (ITCastFail GBase GHole (\u03bb ())) FHOuter )\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | Inr (Inr (Inr (_ , ._ , refl , GHole , _))) = S (_ , Step FHOuter (ITCastSucceed GHole) FHOuter)\n\n progress (TACast wt (TCHole2 {\u03c411 ==> \u03c412})) | I x\u2081 | Inr x = S (_ , Step FHOuter (ITExpand (MGArr (ground-arr-not-hole x))) FHOuter)\n\n progress (TACast wt (TCArr {\u03c41} {\u03c42} {\u03c41'} {\u03c42'} c1 c2)) | I x\n with htype-dec (\u03c41 ==> \u03c42) (\u03c41' ==> \u03c42')\n progress (TACast wt (TCArr c1 c2)) | I x\u2081 | Inl refl = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCArr c1 c2)) | I x\u2081 | Inr x = I (ICastArr x x\u2081)\n\n -- boxed value cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | BV x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCHole1 {\u03c4 = \u03c4})) | BV x\n with ground-decidable \u03c4\n progress (TACast wt TCHole1) | BV x\u2081 | Inl g = BV (BVHoleCast g x\u2081)\n progress (TACast wt (TCHole1 {b})) | BV x\u2081 | Inr x = abort (x GBase)\n progress (TACast wt (TCHole1 {\u2987\u2988})) | BV x\u2081 | Inr x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCHole1 {\u03c41 ==> \u03c42})) | BV x\u2081 | Inr x\n with (htype-dec (\u03c41 ==> \u03c42) (\u2987\u2988 ==> \u2987\u2988))\n progress (TACast wt (TCHole1 {.\u2987\u2988 ==> .\u2987\u2988})) | BV x\u2082 | Inr x\u2081 | Inl refl = BV (BVHoleCast GHole x\u2082)\n progress (TACast wt (TCHole1 {\u03c41 ==> \u03c42})) | BV x\u2082 | Inr x\u2081 | Inr x = S (_ , Step FHOuter (ITGround (MGArr x)) FHOuter)\n\n\n -- this is the case i was working on on friday; this part seems ok if maybe redundant\n progress {\u03c4 = \u03c4} (TACast wt TCHole2) | BV x\n with ground-decidable \u03c4 | canonical-boxed-forms-hole wt x\n progress {\u03c4 = \u03c4} (TACast wt TCHole2) | BV x\u2081 | Inl x | d' , \u03c4' , refl , gnd , wt'\n with htype-dec \u03c4' \u03c4\n progress (TACast wt TCHole2) | BV x\u2081 | Inl x\u2082 | d' , \u03c4 , refl , gnd , wt' | Inl refl = S (_ , Step FHOuter (ITCastSucceed gnd) FHOuter )\n progress (TACast wt TCHole2) | BV x\u2081 | Inl x\u2082 | d' , \u03c4' , refl , gnd , wt' | Inr x = S (_ , Step FHOuter (ITCastFail gnd x\u2082 x) FHOuter )\n progress {\u03c4 = \u03c4} (TACast wt TCHole2) | BV x\u2081 | Inr x | d' , \u03c4' , refl , gnd , wt' -- this case goes off the rails here\n with htype-dec \u03c4' \u03c4\n progress (TACast wt TCHole2) | BV x\u2081 | Inr x\u2082 | d' , \u03c4 , refl , gnd , wt' | Inl refl = abort (x\u2082 gnd)\n progress (TACast wt TCHole2) | BV x\u2081 | Inr x\u2082 | d' , \u03c4' , refl , gnd , wt' | Inr x = {!!} -- S (_ , Step {!!} (ITCastFail gnd {!!} x) {!!})\n\n -- this is the beginning of the next case\n progress (TACast wt (TCArr {\u03c41} {\u03c42} {\u03c41'} {\u03c42'} c1 c2)) | BV x\n with htype-dec (\u03c41 ==> \u03c42) (\u03c41' ==> \u03c42')\n progress (TACast wt (TCArr c1 c2)) | BV x\u2081 | Inl refl = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCArr c1 c2)) | BV x\u2081 | Inr x = BV (BVArrCast x x\u2081)\n\n -- failed casts\n progress (TAFailedCast wt y z w)\n with progress wt\n progress (TAFailedCast wt y z w) | S (d' , Step x a q) = S(_ , Step (FHFailedCast (FHCast x)) a (FHFailedCast (FHCast q)))\n progress (TAFailedCast wt y z w) | I x = I (IFailedCast (FIndet x) y z w)\n progress (TAFailedCast wt y z w) | BV x = I (IFailedCast (FBoxed x) y z w)\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import lemmas-ground\n\nopen import progress-checks\n\nopen import canonical-boxed-forms\nopen import canonical-value-forms\nopen import canonical-indeterminate-forms\n\nopen import ground-decidable\nopen import htype-decidable\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d boxedval + d indet + \u03a3[ d' \u2208 dhexp ] (d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n BV : \u2200{d \u0394} \u2192 d boxedval \u2192 ok d \u0394\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = BV (BVVal VConst)\n\n -- variables\n progress (TAVar x\u2081) = abort (somenotnone (! x\u2081))\n\n -- lambdas\n progress (TALam wt) = BV (BVVal VLam)\n\n -- applications\n progress (TAAp wt1 wt2)\n with progress wt1 | progress wt2\n -- if the left steps, the whole thing steps\n progress (TAAp wt1 wt2) | S (_ , Step x y z) | _ = S (_ , Step (FHAp1 x) y (FHAp1 z))\n -- if the left is indeterminate, step the right\n progress (TAAp wt1 wt2) | I i | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n -- if they're both indeterminate, step when the cast steps and indet otherwise\n progress (TAAp wt1 wt2) | I x | I x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | I y | CIFACast (_ , _ , _ , _ , _ , refl , _ , _ ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | I y | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n -- if the left is indetermiante but the right is a value\n progress (TAAp wt1 wt2) | I x | BV x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | BV y | CIFACast (_ , _ , _ , _ , _ , refl , _ , _ ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | BV y | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n -- if the left is a boxed value, inspect the right\n progress (TAAp wt1 wt2) | BV v | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n progress (TAAp wt1 wt2) | BV v | I i\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (_ , _ , refl , _) = S (_ , Step FHOuter ITLam FHOuter)\n ... | Inr (_ , _ , _ , refl , _ , _) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | BV v | BV v\u2082\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (_ , _ , refl , _) = S (_ , Step FHOuter ITLam FHOuter)\n ... | Inr (_ , _ , _ , refl , _ , _) = S (_ , Step FHOuter ITApCast FHOuter)\n\n -- empty holes\n progress (TAEHole x x\u2081) = I IEHole\n\n -- nonempty holes\n progress (TANEHole xin wt x\u2081)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHNEHole x) y (FHNEHole z))\n ... | I x = I (INEHole (FIndet x))\n ... | BV x = I (INEHole (FBoxed x))\n\n -- casts\n progress (TACast wt con)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHCast x) y (FHCast z))\n -- indet cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | I x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast {\u03c41 = \u03c41} wt TCHole1) | I x\n with \u03c41\n progress (TACast wt TCHole1) | I x | b = I (ICastGroundHole GBase x)\n progress (TACast wt TCHole1) | I x | \u2987\u2988 = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole1) | I x | \u03c411 ==> \u03c412\n with ground-decidable (\u03c411 ==> \u03c412)\n progress (TACast wt TCHole1) | I x\u2081 | .\u2987\u2988 ==> .\u2987\u2988 | Inl GHole = I (ICastGroundHole GHole x\u2081)\n progress (TACast wt TCHole1) | I x\u2081 | \u03c411 ==> \u03c412 | Inr x = S (_ , Step FHOuter (ITGround (MGArr (ground-arr-not-hole x))) FHOuter)\n progress (TACast wt (TCHole2 {b})) | I x\n with canonical-indeterminate-forms-hole wt x\n progress (TACast wt (TCHole2 {b})) | I x | Inl (_ , _ , _ , refl , f) = I (ICastHoleGround (\u03bb _ _ ()) x GBase)\n progress (TACast wt (TCHole2 {b})) | I x | Inr (Inl (_ , _ , _ , _ , _ , refl , _ , _ , _)) = I (ICastHoleGround (\u03bb _ _ ()) x GBase)\n progress (TACast wt (TCHole2 {b})) | I x | Inr (Inr (Inl (_ , _ , _ , refl , _ , _ , _ , _ , _))) = I (ICastHoleGround (\u03bb _ _ ()) x GBase)\n progress (TACast wt (TCHole2 {b})) | I x | Inr (Inr (Inr (_ , \u03c4 , refl , _ , _)))\n with htype-dec \u03c4 b\n progress (TACast wt (TCHole2 {b})) | I x\u2081 | Inr (Inr (Inr (_ , ._ , refl , gnd , _))) | Inl refl = S (_ , Step FHOuter (ITCastSucceed gnd) FHOuter)\n progress (TACast wt (TCHole2 {b})) | I x\u2081 | Inr (Inr (Inr (_ , _ , refl , gnd , _))) | Inr neq = S (_ , Step FHOuter (ITCastFail gnd GBase neq) FHOuter)\n\n progress (TACast wt (TCHole2 {\u2987\u2988})) | I x = S (_ , Step FHOuter ITCastID FHOuter)\n\n progress (TACast wt (TCHole2 {\u03c411 ==> \u03c412})) | I x\n with ground-decidable (\u03c411 ==> \u03c412)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x\u2081 | Inl GHole\n with canonical-indeterminate-forms-hole wt x\u2081\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | Inl (_ , _ , _ , refl , _) = I (ICastHoleGround (\u03bb _ _ ()) x GHole)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | Inr (Inl (_ , _ , _ , _ , _ , refl , _ , _ , _)) = I (ICastHoleGround (\u03bb _ _ ()) x GHole)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | Inr (Inr (Inl (_ , _ , _ , refl , _ , _ , _ , _ , _))) = I (ICastHoleGround (\u03bb _ _ ()) x GHole)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | Inr (Inr (Inr (_ , ._ , refl , GBase , _))) = S (_ , Step FHOuter (ITCastFail GBase GHole (\u03bb ())) FHOuter )\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | Inr (Inr (Inr (_ , ._ , refl , GHole , _))) = S (_ , Step FHOuter (ITCastSucceed GHole) FHOuter)\n\n progress (TACast wt (TCHole2 {\u03c411 ==> \u03c412})) | I x\u2081 | Inr x = S (_ , Step FHOuter (ITExpand (MGArr (ground-arr-not-hole x))) FHOuter)\n\n progress (TACast wt (TCArr {\u03c41} {\u03c42} {\u03c41'} {\u03c42'} c1 c2)) | I x\n with htype-dec (\u03c41 ==> \u03c42) (\u03c41' ==> \u03c42')\n progress (TACast wt (TCArr c1 c2)) | I x\u2081 | Inl refl = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCArr c1 c2)) | I x\u2081 | Inr x = I (ICastArr x x\u2081)\n\n -- boxed value cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | BV x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCHole1 {\u03c4 = \u03c4})) | BV x\n with ground-decidable \u03c4\n progress (TACast wt TCHole1) | BV x\u2081 | Inl g = BV (BVHoleCast g x\u2081)\n progress (TACast wt (TCHole1 {b})) | BV x\u2081 | Inr x = abort (x GBase)\n progress (TACast wt (TCHole1 {\u2987\u2988})) | BV x\u2081 | Inr x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCHole1 {\u03c41 ==> \u03c42})) | BV x\u2081 | Inr x\n with (htype-dec (\u03c41 ==> \u03c42) (\u2987\u2988 ==> \u2987\u2988))\n progress (TACast wt (TCHole1 {.\u2987\u2988 ==> .\u2987\u2988})) | BV x\u2082 | Inr x\u2081 | Inl refl = BV (BVHoleCast GHole x\u2082)\n progress (TACast wt (TCHole1 {\u03c41 ==> \u03c42})) | BV x\u2082 | Inr x\u2081 | Inr x = S (_ , Step FHOuter (ITGround (MGArr x)) FHOuter)\n\n\n -- this is the case i was working on on friday; this part seems ok if maybe redundant\n progress {\u03c4 = \u03c4} (TACast wt TCHole2) | BV x\n with ground-decidable \u03c4 | canonical-boxed-forms-hole wt x\n progress {\u03c4 = \u03c4} (TACast wt TCHole2) | BV x\u2081 | Inl x | d' , \u03c4' , refl , gnd , wt'\n with htype-dec \u03c4' \u03c4\n progress (TACast wt TCHole2) | BV x\u2081 | Inl x\u2082 | d' , \u03c4 , refl , gnd , wt' | Inl refl = S (_ , Step FHOuter (ITCastSucceed gnd) FHOuter )\n progress (TACast wt TCHole2) | BV x\u2081 | Inl x\u2082 | d' , \u03c4' , refl , gnd , wt' | Inr x = S (_ , Step FHOuter (ITCastFail gnd x\u2082 x) FHOuter )\n progress {\u03c4 = \u03c4} (TACast wt TCHole2) | BV x\u2081 | Inr x | d' , \u03c4' , refl , gnd , wt' -- this case goes off the rails here\n with htype-dec \u03c4' \u03c4\n progress (TACast wt TCHole2) | BV x\u2081 | Inr x\u2082 | d' , \u03c4 , refl , gnd , wt' | Inl refl = abort (x\u2082 gnd)\n progress (TACast wt TCHole2) | BV x\u2081 | Inr x\u2082 | d' , \u03c4' , refl , gnd , wt' | Inr x = {!ITCastFail ? ? x!}\n\n -- this is the beginning of the next case -- cyrus\n progress (TACast wt (TCArr c1 c2)) | BV x = {!!}\n\n -- failed casts\n progress (TAFailedCast wt y z w)\n with progress wt\n progress (TAFailedCast wt y z w) | S (d' , Step x a q) = S(_ , Step (FHFailedCast (FHCast x)) a (FHFailedCast (FHCast q)))\n progress (TAFailedCast wt y z w) | I x = I (IFailedCast (FIndet x) y z w)\n progress (TAFailedCast wt y z w) | BV x = I (IFailedCast (FBoxed x) y z w)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"543a4a08de01259162b6b65a657416b56f011a40","subject":"flipbased: \u25b9\u21ba as flip map\u21ba","message":"flipbased: \u25b9\u21ba as flip map\u21ba\n","repos":"crypto-agda\/crypto-agda","old_file":"flipbased.agda","new_file":"flipbased.agda","new_contents":"open import Algebra\nimport Level as L\nopen L using () renaming (_\u2294_ to _L\u2294_)\nopen import Function hiding (_\u27e8_\u27e9_)\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Bool\nopen import Data.Nat.Properties\nopen import Data.Product using (proj\u2081; proj\u2082; _,_; swap; _\u00d7_)\nopen import Data.Bits\nopen import Data.Bool\nopen import Data.Vec using (Vec; []; _\u2237_; take; drop; head; tail) renaming (map to vmap)\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_)\n\nmodule flipbased\n (\u21ba : \u2200 {a} \u2192 \u2115 \u2192 Set a \u2192 Set a)\n (toss : \u21ba 1 Bit)\n (weaken\u2264 : \u2200 {m n a} {A : Set a} \u2192 m \u2264 n \u2192 \u21ba m A \u2192 \u21ba n A)\n (return\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A)\n (map\u21ba : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B)\n (join\u21ba : \u2200 {n\u2081 n\u2082 a} {A : Set a} \u2192 \u21ba n\u2081 (\u21ba n\u2082 A) \u2192 \u21ba (n\u2081 + n\u2082) A)\n where\n\nCoins = \u2115\n\n-- If you are not allowed to toss any coin, then you are deterministic.\nDet : \u2200 {a} \u2192 Set a \u2192 Set a\nDet = \u21ba 0\n\nreturn\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\nreturn\u1d30 = return\u21ba\n\npure\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\npure\u1d30 = return\u1d30\n\ncoerce : \u2200 {m n a} {A : Set a} \u2192 m \u2261 n \u2192 \u21ba m A \u2192 \u21ba n A\ncoerce \u2261.refl = id\n\n_>>=_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 A \u2192 (A \u2192 \u21ba n\u2082 B) \u2192 \u21ba (n\u2081 + n\u2082) B\n_>>=_ x f = join\u21ba (map\u21ba f x)\n\n_=<<_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n (A \u2192 \u21ba n\u2081 B) \u2192 \u21ba n\u2082 A \u2192 \u21ba (n\u2081 + n\u2082) B\n_=<<_ {n\u2081} {n\u2082} rewrite \u2115\u00b0.+-comm n\u2081 n\u2082 = flip _>>=_\n\n_>>_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 A \u2192 \u21ba n\u2082 B \u2192 \u21ba (n\u2081 + n\u2082) B\n_>>_ {n\u2081} x y = x >>= const y\n\n_>=>_ : \u2200 {n\u2081 n\u2082 a b c} {A : Set a} {B : Set b} {C : Set c}\n \u2192 (A \u2192 \u21ba n\u2081 B) \u2192 (B \u2192 \u21ba n\u2082 C) \u2192 A \u2192 \u21ba (n\u2081 + n\u2082) C\n(f >=> g) x = f x >>= g\n\nweaken : \u2200 {m n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba (m + n) A\nweaken {m} x = return\u21ba {m} 0 >> x\n\nweaken\u2032 : \u2200 {m n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba (n + m) A\nweaken\u2032 x = x >>= return\u21ba\n\npure\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A\npure\u21ba = return\u21ba\n\n-- Weakened version of toss\ntoss\u1d42 : \u2200 {n} \u2192 \u21ba (1 + n) Bit\ntoss\u1d42 = toss >>= return\u21ba\n\n_\u25b9\u21ba_ : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 \u21ba n A \u2192 (A \u2192 B) \u2192 \u21ba n B\nx \u25b9\u21ba f = map\u21ba f x\n\n\u27ea_\u27eb : \u2200 {n} {a} {A : Set a} \u2192 A \u2192 \u21ba n A\n\u27ea_\u27eb = pure\u21ba\n\n\u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n\u27ea_\u27eb\u1d30 = pure\u1d30\n\n\u27ea_\u00b7_\u27eb : \u2200 {a b} {A : Set a} {B : Set b} {n} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B\n\u27ea f \u00b7 x \u27eb = map\u21ba f x\n\ninfixl 4 _\u229b_\n_\u229b_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 (A \u2192 B) \u2192 \u21ba n\u2082 A \u2192 \u21ba (n\u2081 + n\u2082) B\n_\u229b_ {n\u2081} mf mx = mf >>= \u03bb f \u2192 \u27ea f \u00b7 mx \u27eb \n\n\u27ea_\u00b7_\u00b7_\u27eb : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba (m + n) C\n\u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n\u27ea_\u00b7_\u00b7_\u00b7_\u27eb : \u2200 {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} {m n o} \u2192\n (A \u2192 B \u2192 C \u2192 D) \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba o C \u2192 \u21ba (m + n + o) D\n\u27ea f \u00b7 x \u00b7 y \u00b7 z \u27eb = map\u21ba f x \u229b y \u229b z\n\nchoose : \u2200 {n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba n A \u2192 \u21ba (suc n) A\nchoose x y = toss >>= \u03bb b \u2192 if b then x else y\n\nzip\u21ba : \u2200 {c\u2080 c\u2081 a b} {A : Set a} {B : Set b} \u2192 \u21ba c\u2080 A \u2192 \u21ba c\u2081 B \u2192 \u21ba (c\u2080 + c\u2081) (A \u00d7 B)\nzip\u21ba x y = \u27ea _,_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8,\u27e9_ : \u2200 {a b} {A : Set a} {B : Set b} {m n} \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba (m + n) (A \u00d7 B)\n_\u27e8,\u27e9_ = zip\u21ba\n\n_\u27e8xor\u27e9_ : \u2200 {n\u2081 n\u2082} \u2192 \u21ba n\u2081 Bit \u2192 \u21ba n\u2082 Bit \u2192 \u21ba (n\u2081 + n\u2082) Bit\nx \u27e8xor\u27e9 y = \u27ea _xor_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8\u2295\u27e9_ : \u2200 {n\u2081 n\u2082 m} \u2192 \u21ba n\u2081 (Bits m) \u2192 \u21ba n\u2082 (Bits m) \u2192 \u21ba (n\u2081 + n\u2082) (Bits m)\nx \u27e8\u2295\u27e9 y = \u27ea _\u2295_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8==\u27e9_ : \u2200 {n\u2081 n\u2082 m} \u2192 \u21ba n\u2081 (Bits m) \u2192 \u21ba n\u2082 (Bits m) \u2192 \u21ba (n\u2081 + n\u2082) Bit\nx \u27e8==\u27e9 y = \u27ea _==_ \u00b7 x \u00b7 y \u27eb\n\nT\u21ba : \u2200 {k} \u2192 \u21ba k Bit \u2192 \u21ba k Set\nT\u21ba p = \u27ea T \u00b7 p \u27eb\n\nreplicate\u21ba : \u2200 {n m} {a} {A : Set a} \u2192 \u21ba m A \u2192 \u21ba (n * m) (Vec A n)\nreplicate\u21ba {zero} _ = \u27ea [] \u27eb\nreplicate\u21ba {suc _} x = \u27ea _\u2237_ \u00b7 x \u00b7 replicate\u21ba x \u27eb\n\nrandom : \u2200 {n} \u2192 \u21ba n (Bits n)\n-- random = coerce ? (replicate\u21ba toss) -- specialized version for now to avoid coerce\nrandom {zero} = \u27ea [] \u27eb\nrandom {suc _} = \u27ea _\u2237_ \u00b7 toss \u00b7 random \u27eb\n\nrandomTbl : \u2200 m n \u2192 \u21ba (2 ^ m * n) (Vec (Bits n) (2 ^ m))\nrandomTbl m n = replicate\u21ba random\n\nrandomFun : \u2200 m n \u2192 \u21ba (2 ^ m * n) (Bits m \u2192 Bits n)\nrandomFun m n = \u27ea funFromTbl \u00b7 randomTbl m n \u27eb\n\nrandomFunExt : \u2200 {n k a} {A : Set a} \u2192 \u21ba k (Bits n \u2192 A) \u2192 \u21ba (k + k) (Bits (suc n) \u2192 A)\nrandomFunExt f = \u27ea comb \u00b7 f \u00b7 f \u27eb where comb = \u03bb g\u2081 g\u2082 xs \u2192 (if head xs then g\u2081 else g\u2082) (tail xs)\n\ncostRndFun : \u2115 \u2192 \u2115 \u2192 \u2115\ncostRndFun zero n = n\ncostRndFun (suc m) n = 2* (costRndFun m n)\n\ncostRndFun-lem : \u2200 m n \u2192 costRndFun m n \u2261 2 ^ m * n\ncostRndFun-lem zero n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\ncostRndFun-lem (suc m) n rewrite costRndFun-lem m n | \u2115\u00b0.*-assoc 2 (2 ^ m) n | \u2115\u00b0.+-comm (2 ^ m * n) 0 = \u2261.refl\n\nrandomFun\u2032 : \u2200 {m n} \u2192 \u21ba (costRndFun m n) (Bits m \u2192 Bits n)\nrandomFun\u2032 {zero} = \u27ea const \u00b7 random \u27eb\nrandomFun\u2032 {suc m} = randomFunExt (randomFun\u2032 {m})\n\nrecord ProgEquiv a \u2113 : Set (L.suc \u2113 L\u2294 L.suc a) where\n infix 2 _\u2248_ _\u224b_\n field\n _\u2248_ : \u2200 {n} {A : Set a} \u2192 Rel (\u21ba n A) \u2113\n\n refl : \u2200 {n A} \u2192 Reflexive {A = \u21ba n A} _\u2248_\n sym : \u2200 {n A} \u2192 Symmetric {A = \u21ba n A} _\u2248_\n\n -- not strictly transitive\n\n reflexive : \u2200 {n A} \u2192 _\u2261_ \u21d2 _\u2248_ {n} {A}\n reflexive \u2261.refl = refl\n\n _\u224b_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 A \u2192 Set \u2113\n _\u224b_ {n\u2081} {n\u2082} p\u2081 p\u2082 = _\u2248_ {n = n\u2081 \u2294 n\u2082} (weaken\u2264 (m\u2264m\u2294n _ _) p\u2081) (weaken\u2264 (m\u2264n\u2294m _ n\u2081) p\u2082)\n where m\u2264n\u2294m : \u2200 m n \u2192 m \u2264 n \u2294 m\n m\u2264n\u2294m m n rewrite \u2294\u00b0.+-comm n m = m\u2264m\u2294n m n\n\n -- Another name for _\u224b_\n _looks_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 A \u2192 Set \u2113\n _looks_ = _\u224b_\n\nmodule WithEquiv (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n\n SecPRG : \u2200 {k n} (prg : (key : Bits k) \u2192 Bits n) \u2192 Set\n SecPRG prg = this looks random where this = \u27ea prg \u00b7 random \u27eb\n\n record PRG k n : Set where\n constructor _,_\n field\n prg : Bits k \u2192 Bits n\n sec : SecPRG prg\n\n OneTimeSecPRF : \u2200 {k m n} (prf : (key : Bits k) (msg : Bits m) \u2192 Bits n) \u2192 Set\n OneTimeSecPRF prf = \u2200 {xs} \u2192 let this = \u27ea prf \u00b7 random \u00b7 \u27ea xs \u27eb\u1d30 \u27eb in\n this looks random\n\n record PRF k m n : Set where\n constructor _,_\n field\n prf : Bits k \u2192 Bits m \u2192 Bits n\n sec : OneTimeSecPRF prf\n\nOTP : \u2200 {n} \u2192 Bits n \u2192 Bits n \u2192 Bits n\nOTP key msg = key \u2295 msg\n\ninit : \u2200 {k a} {A : Set a} \u2192 (Bits k \u2192 A) \u2192 \u21ba k A\ninit f = \u27ea f \u00b7 random \u27eb\n\nmodule Examples (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n open WithEquiv progEq\n\n left-unit-law = \u2200 {A B : Set} {n} {x : A} {f : A \u2192 \u21ba n B} \u2192 return\u1d30 x >>= f \u2248 f x\n\n right-unit-law = \u2200 {A : Set} {n} {x : \u21ba n A} \u2192 return\u1d30 =<< x \u2248 x\n\n assoc-law = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : \u21ba n\u2081 A} {f : A \u2192 \u21ba n\u2082 B} {g : B \u2192 \u21ba n\u2083 C}\n \u2192 (x >>= f) >>= g \u224b x >>= (\u03bb x \u2192 f x >>= g)\n\n assoc-law\u2032 = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : \u21ba n\u2081 A} {f : A \u2192 \u21ba n\u2082 B} {g : B \u2192 \u21ba n\u2083 C}\n \u2192 (x >>= f) >>= g \u2248 coerce (\u2261.sym (\u2115\u00b0.+-assoc n\u2081 n\u2082 n\u2083)) (x >>= (\u03bb x \u2192 f x >>= g))\n\n ex\u2081 = \u2200 {x} \u2192 toss \u27e8xor\u27e9 \u27ea x \u27eb\u1d30 \u2248 \u27ea x \u27eb\n\n ex\u2082 = p \u2248 map\u21ba swap p where p = toss \u27e8,\u27e9 toss\n\n ex\u2083 = \u2200 {n} \u2192 OneTimeSecPRF {n} OTP\n\n ex\u2084 = \u2200 {k n} (prg : PRG k n) \u2192 OneTimeSecPRF (\u03bb key xs \u2192 xs \u2295 PRG.prg prg key)\n\n ex\u2085 = \u2200 {k n} \u2192 PRG k n \u2192 PRF k n n\n","old_contents":"open import Algebra\nimport Level as L\nopen L using () renaming (_\u2294_ to _L\u2294_)\nopen import Function hiding (_\u27e8_\u27e9_)\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Bool\nopen import Data.Nat.Properties\nopen import Data.Product using (proj\u2081; proj\u2082; _,_; swap; _\u00d7_)\nopen import Data.Bits\nopen import Data.Bool\nopen import Data.Vec using (Vec; []; _\u2237_; take; drop; head; tail) renaming (map to vmap)\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_)\n\nmodule flipbased\n (\u21ba : \u2200 {a} \u2192 \u2115 \u2192 Set a \u2192 Set a)\n (toss : \u21ba 1 Bit)\n (weaken\u2264 : \u2200 {m n a} {A : Set a} \u2192 m \u2264 n \u2192 \u21ba m A \u2192 \u21ba n A)\n (return\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A)\n (map\u21ba : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B)\n (join\u21ba : \u2200 {n\u2081 n\u2082 a} {A : Set a} \u2192 \u21ba n\u2081 (\u21ba n\u2082 A) \u2192 \u21ba (n\u2081 + n\u2082) A)\n where\n\nCoins = \u2115\n\n-- If you are not allowed to toss any coin, then you are deterministic.\nDet : \u2200 {a} \u2192 Set a \u2192 Set a\nDet = \u21ba 0\n\nreturn\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\nreturn\u1d30 = return\u21ba\n\npure\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\npure\u1d30 = return\u1d30\n\ncoerce : \u2200 {m n a} {A : Set a} \u2192 m \u2261 n \u2192 \u21ba m A \u2192 \u21ba n A\ncoerce \u2261.refl = id\n\n_>>=_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 A \u2192 (A \u2192 \u21ba n\u2082 B) \u2192 \u21ba (n\u2081 + n\u2082) B\n_>>=_ x f = join\u21ba (map\u21ba f x)\n\n_=<<_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n (A \u2192 \u21ba n\u2081 B) \u2192 \u21ba n\u2082 A \u2192 \u21ba (n\u2081 + n\u2082) B\n_=<<_ {n\u2081} {n\u2082} rewrite \u2115\u00b0.+-comm n\u2081 n\u2082 = flip _>>=_\n\n_>>_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 A \u2192 \u21ba n\u2082 B \u2192 \u21ba (n\u2081 + n\u2082) B\n_>>_ {n\u2081} x y = x >>= const y\n\n_>=>_ : \u2200 {n\u2081 n\u2082 a b c} {A : Set a} {B : Set b} {C : Set c}\n \u2192 (A \u2192 \u21ba n\u2081 B) \u2192 (B \u2192 \u21ba n\u2082 C) \u2192 A \u2192 \u21ba (n\u2081 + n\u2082) C\n(f >=> g) x = f x >>= g\n\nweaken : \u2200 {m n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba (m + n) A\nweaken {m} x = return\u21ba {m} 0 >> x\n\nweaken\u2032 : \u2200 {m n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba (n + m) A\nweaken\u2032 x = x >>= return\u21ba\n\npure\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A\npure\u21ba = return\u21ba\n\n-- Weakened version of toss\ntoss\u1d42 : \u2200 {n} \u2192 \u21ba (1 + n) Bit\ntoss\u1d42 = toss >>= return\u21ba\n\n\u27ea_\u27eb : \u2200 {n} {a} {A : Set a} \u2192 A \u2192 \u21ba n A\n\u27ea_\u27eb = pure\u21ba\n\n\u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n\u27ea_\u27eb\u1d30 = pure\u1d30\n\n\u27ea_\u00b7_\u27eb : \u2200 {a b} {A : Set a} {B : Set b} {n} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B\n\u27ea f \u00b7 x \u27eb = map\u21ba f x\n\ninfixl 4 _\u229b_\n_\u229b_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 (A \u2192 B) \u2192 \u21ba n\u2082 A \u2192 \u21ba (n\u2081 + n\u2082) B\n_\u229b_ {n\u2081} mf mx = mf >>= \u03bb f \u2192 \u27ea f \u00b7 mx \u27eb \n\n\u27ea_\u00b7_\u00b7_\u27eb : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba (m + n) C\n\u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n\u27ea_\u00b7_\u00b7_\u00b7_\u27eb : \u2200 {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} {m n o} \u2192\n (A \u2192 B \u2192 C \u2192 D) \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba o C \u2192 \u21ba (m + n + o) D\n\u27ea f \u00b7 x \u00b7 y \u00b7 z \u27eb = map\u21ba f x \u229b y \u229b z\n\nchoose : \u2200 {n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba n A \u2192 \u21ba (suc n) A\nchoose x y = toss >>= \u03bb b \u2192 if b then x else y\n\nzip\u21ba : \u2200 {c\u2080 c\u2081 a b} {A : Set a} {B : Set b} \u2192 \u21ba c\u2080 A \u2192 \u21ba c\u2081 B \u2192 \u21ba (c\u2080 + c\u2081) (A \u00d7 B)\nzip\u21ba x y = \u27ea _,_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8,\u27e9_ : \u2200 {a b} {A : Set a} {B : Set b} {m n} \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba (m + n) (A \u00d7 B)\n_\u27e8,\u27e9_ = zip\u21ba\n\n_\u27e8xor\u27e9_ : \u2200 {n\u2081 n\u2082} \u2192 \u21ba n\u2081 Bit \u2192 \u21ba n\u2082 Bit \u2192 \u21ba (n\u2081 + n\u2082) Bit\nx \u27e8xor\u27e9 y = \u27ea _xor_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8\u2295\u27e9_ : \u2200 {n\u2081 n\u2082 m} \u2192 \u21ba n\u2081 (Bits m) \u2192 \u21ba n\u2082 (Bits m) \u2192 \u21ba (n\u2081 + n\u2082) (Bits m)\nx \u27e8\u2295\u27e9 y = \u27ea _\u2295_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8==\u27e9_ : \u2200 {n\u2081 n\u2082 m} \u2192 \u21ba n\u2081 (Bits m) \u2192 \u21ba n\u2082 (Bits m) \u2192 \u21ba (n\u2081 + n\u2082) Bit\nx \u27e8==\u27e9 y = \u27ea _==_ \u00b7 x \u00b7 y \u27eb\n\nT\u21ba : \u2200 {k} \u2192 \u21ba k Bit \u2192 \u21ba k Set\nT\u21ba p = \u27ea T \u00b7 p \u27eb\n\nreplicate\u21ba : \u2200 {n m} {a} {A : Set a} \u2192 \u21ba m A \u2192 \u21ba (n * m) (Vec A n)\nreplicate\u21ba {zero} _ = \u27ea [] \u27eb\nreplicate\u21ba {suc _} x = \u27ea _\u2237_ \u00b7 x \u00b7 replicate\u21ba x \u27eb\n\nrandom : \u2200 {n} \u2192 \u21ba n (Bits n)\n-- random = coerce ? (replicate\u21ba toss) -- specialized version for now to avoid coerce\nrandom {zero} = \u27ea [] \u27eb\nrandom {suc _} = \u27ea _\u2237_ \u00b7 toss \u00b7 random \u27eb\n\nrandomTbl : \u2200 m n \u2192 \u21ba (2 ^ m * n) (Vec (Bits n) (2 ^ m))\nrandomTbl m n = replicate\u21ba random\n\nrandomFun : \u2200 m n \u2192 \u21ba (2 ^ m * n) (Bits m \u2192 Bits n)\nrandomFun m n = \u27ea funFromTbl \u00b7 randomTbl m n \u27eb\n\nrandomFunExt : \u2200 {n k a} {A : Set a} \u2192 \u21ba k (Bits n \u2192 A) \u2192 \u21ba (k + k) (Bits (suc n) \u2192 A)\nrandomFunExt f = \u27ea comb \u00b7 f \u00b7 f \u27eb where comb = \u03bb g\u2081 g\u2082 xs \u2192 (if head xs then g\u2081 else g\u2082) (tail xs)\n\ncostRndFun : \u2115 \u2192 \u2115 \u2192 \u2115\ncostRndFun zero n = n\ncostRndFun (suc m) n = 2* (costRndFun m n)\n\ncostRndFun-lem : \u2200 m n \u2192 costRndFun m n \u2261 2 ^ m * n\ncostRndFun-lem zero n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\ncostRndFun-lem (suc m) n rewrite costRndFun-lem m n | \u2115\u00b0.*-assoc 2 (2 ^ m) n | \u2115\u00b0.+-comm (2 ^ m * n) 0 = \u2261.refl\n\nrandomFun\u2032 : \u2200 {m n} \u2192 \u21ba (costRndFun m n) (Bits m \u2192 Bits n)\nrandomFun\u2032 {zero} = \u27ea const \u00b7 random \u27eb\nrandomFun\u2032 {suc m} = randomFunExt (randomFun\u2032 {m})\n\nrecord ProgEquiv a \u2113 : Set (L.suc \u2113 L\u2294 L.suc a) where\n infix 2 _\u2248_ _\u224b_\n field\n _\u2248_ : \u2200 {n} {A : Set a} \u2192 Rel (\u21ba n A) \u2113\n\n refl : \u2200 {n A} \u2192 Reflexive {A = \u21ba n A} _\u2248_\n sym : \u2200 {n A} \u2192 Symmetric {A = \u21ba n A} _\u2248_\n\n -- not strictly transitive\n\n reflexive : \u2200 {n A} \u2192 _\u2261_ \u21d2 _\u2248_ {n} {A}\n reflexive \u2261.refl = refl\n\n _\u224b_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 A \u2192 Set \u2113\n _\u224b_ {n\u2081} {n\u2082} p\u2081 p\u2082 = _\u2248_ {n = n\u2081 \u2294 n\u2082} (weaken\u2264 (m\u2264m\u2294n _ _) p\u2081) (weaken\u2264 (m\u2264n\u2294m _ n\u2081) p\u2082)\n where m\u2264n\u2294m : \u2200 m n \u2192 m \u2264 n \u2294 m\n m\u2264n\u2294m m n rewrite \u2294\u00b0.+-comm n m = m\u2264m\u2294n m n\n\n -- Another name for _\u224b_\n _looks_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 A \u2192 Set \u2113\n _looks_ = _\u224b_\n\nmodule WithEquiv (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n\n SecPRG : \u2200 {k n} (prg : (key : Bits k) \u2192 Bits n) \u2192 Set\n SecPRG prg = this looks random where this = \u27ea prg \u00b7 random \u27eb\n\n record PRG k n : Set where\n constructor _,_\n field\n prg : Bits k \u2192 Bits n\n sec : SecPRG prg\n\n OneTimeSecPRF : \u2200 {k m n} (prf : (key : Bits k) (msg : Bits m) \u2192 Bits n) \u2192 Set\n OneTimeSecPRF prf = \u2200 {xs} \u2192 let this = \u27ea prf \u00b7 random \u00b7 \u27ea xs \u27eb\u1d30 \u27eb in\n this looks random\n\n record PRF k m n : Set where\n constructor _,_\n field\n prf : Bits k \u2192 Bits m \u2192 Bits n\n sec : OneTimeSecPRF prf\n\nOTP : \u2200 {n} \u2192 Bits n \u2192 Bits n \u2192 Bits n\nOTP key msg = key \u2295 msg\n\ninit : \u2200 {k a} {A : Set a} \u2192 (Bits k \u2192 A) \u2192 \u21ba k A\ninit f = \u27ea f \u00b7 random \u27eb\n\nmodule Examples (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n open WithEquiv progEq\n\n left-unit-law = \u2200 {A B : Set} {n} {x : A} {f : A \u2192 \u21ba n B} \u2192 return\u1d30 x >>= f \u2248 f x\n\n right-unit-law = \u2200 {A : Set} {n} {x : \u21ba n A} \u2192 return\u1d30 =<< x \u2248 x\n\n assoc-law = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : \u21ba n\u2081 A} {f : A \u2192 \u21ba n\u2082 B} {g : B \u2192 \u21ba n\u2083 C}\n \u2192 (x >>= f) >>= g \u224b x >>= (\u03bb x \u2192 f x >>= g)\n\n assoc-law\u2032 = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : \u21ba n\u2081 A} {f : A \u2192 \u21ba n\u2082 B} {g : B \u2192 \u21ba n\u2083 C}\n \u2192 (x >>= f) >>= g \u2248 coerce (\u2261.sym (\u2115\u00b0.+-assoc n\u2081 n\u2082 n\u2083)) (x >>= (\u03bb x \u2192 f x >>= g))\n\n ex\u2081 = \u2200 {x} \u2192 toss \u27e8xor\u27e9 \u27ea x \u27eb\u1d30 \u2248 \u27ea x \u27eb\n\n ex\u2082 = p \u2248 map\u21ba swap p where p = toss \u27e8,\u27e9 toss\n\n ex\u2083 = \u2200 {n} \u2192 OneTimeSecPRF {n} OTP\n\n ex\u2084 = \u2200 {k n} (prg : PRG k n) \u2192 OneTimeSecPRF (\u03bb key xs \u2192 xs \u2295 PRG.prg prg key)\n\n ex\u2085 = \u2200 {k n} \u2192 PRG k n \u2192 PRF k n n\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"7f1a4e72fc0081b46fe06904e4627bb80b3240ac","subject":"Add Coq's \"simp\" tactic.","message":"Add Coq's \"simp\" tactic.\n","repos":"spire\/spire","old_file":"proposal\/examples\/TacticsMatchingFunctionCalls.agda","new_file":"proposal\/examples\/TacticsMatchingFunctionCalls.agda","new_contents":"{- \nThis file demonstrates a technique that simulates the ability to\npattern match against function calls rather than just variables\nor constructors.\n\nThis technique is necessary to write certain kinds of tactics\nas generic functions.\n-}\n\nmodule TacticsMatchingFunctionCalls where\nopen import Data.Bool hiding ( _\u225f_ )\nopen import Data.Nat\nopen import Data.Fin hiding ( _+_ )\nopen import Data.Product hiding ( map )\nopen import Data.List\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\nopen import Function\n\n----------------------------------------------------------------------\n\n{-\nZero is the right identity of addition on the natural numbers.\n-}\n\nplusrident : (n : \u2115) \u2192 n \u2261 n + 0\nplusrident zero = refl\nplusrident (suc n) = cong suc (plusrident n)\n\n----------------------------------------------------------------------\n\n{-\nArith is a universe of codes representing the natural numbers\nalong with a limited set of functions over them. We would like to write\ntactics that pattern match against types indexed by applications of\nthese functions.\n-}\n\ndata Arith : Set where\n `zero : Arith\n `suc : (a : Arith) \u2192 Arith\n _`+_ _`*_ : (a\u2081 a\u2082 : Arith) \u2192 Arith\n\neval : Arith \u2192 \u2115\neval `zero = zero\neval (`suc a) = suc (eval a)\neval (a\u2081 `+ a\u2082) = eval a\u2081 + eval a\u2082\neval (a\u2081 `* a\u2082) = eval a\u2081 * eval a\u2082\n\n----------------------------------------------------------------------\n\n{-\n\u03a6 (standing for *function*-indexed types)\nrepresents an indexed type, but we *can* pattern\nmatch on function calls in its index position.\n\nA - The type of codes, including constructors and functions.\n i.e. Arith\nA\u2032 - The model underlying the codes.\n i.e. \u2115\nel - The evaluation function from code values to model values.\n i.e. eval\nB - The genuine indexed datatype that \u03a6 represents.\n i.e. Fin\na - The index value as a code, which may be a function call and hence supports\n pattern matching.\n i.e. (a `+ `zero)\n-}\n\ndata \u03a6 (A A\u2032 : Set) (el : A \u2192 A\u2032) (B : A\u2032 \u2192 Set) (a : A) : Set where\n \u03c6 : (b : B (el a)) \u2192 \u03a6 A A\u2032 el B a\n\n----------------------------------------------------------------------\n\n{-\nAlternatively, we can specialize \u03a6 to our Arith universe. This may be\na good use of our ability to cheaply define new datatypes via Desc.\n-}\n\ndata \u03a6Arith (B : \u2115 \u2192 Set) (a : Arith) : Set where\n \u03c6 : (b : B (eval a)) \u2192 \u03a6Arith B a\n\n----------------------------------------------------------------------\n\n{-\nGoing further, we could even specialize B in \u03a6Arith to Fin.\n-}\n\ndata Fin\u2082 (a : Arith) : Set where\n fin : (i : Fin (eval a)) \u2192 Fin\u2082 a\n\n----------------------------------------------------------------------\n\n{-\nA small universe of dependent types that is sufficient for the\nexamples given below.\n-}\n\ndata Type : Set\n\u27e6_\u27e7 : Type \u2192 Set\n\ndata Type where\n `Bool `\u2115 `Arith : Type\n `Fin : (n : \u2115) \u2192 Type\n `\u03a6 : (A A\u2032 : Type)\n (el : \u27e6 A \u27e7 \u2192 \u27e6 A\u2032 \u27e7)\n (B : \u27e6 A\u2032 \u27e7 \u2192 Type)\n (a : \u27e6 A \u27e7)\n \u2192 Type\n `\u03a6Arith : (B : \u2115 \u2192 Type) (a : Arith) \u2192 Type\n `Fin\u2082 : (a : Arith) \u2192 Type\n `\u03a0 `\u03a3 : (A : Type) (B : \u27e6 A \u27e7 \u2192 Type) \u2192 Type\n `Id : (A : Type) (x y : \u27e6 A \u27e7) \u2192 Type\n\n\u27e6 `Bool \u27e7 = Bool\n\u27e6 `\u2115 \u27e7 = \u2115\n\u27e6 `Arith \u27e7 = Arith\n\u27e6 `Fin n \u27e7 = Fin n\n\u27e6 `\u03a6 A A\u2032 el B a \u27e7 = \u03a6 \u27e6 A \u27e7 \u27e6 A\u2032 \u27e7 el (\u03bb a \u2192 \u27e6 B a \u27e7) a\n\u27e6 `\u03a6Arith B a \u27e7 = \u03a6Arith (\u03bb x \u2192 \u27e6 B x \u27e7) a\n\u27e6 `Fin\u2082 a \u27e7 = Fin\u2082 a\n\u27e6 `\u03a0 A B \u27e7 = (a : \u27e6 A \u27e7) \u2192 \u27e6 B a \u27e7\n\u27e6 `\u03a3 A B \u27e7 = \u03a3 \u27e6 A \u27e7 (\u03bb a \u2192 \u27e6 B a \u27e7)\n\u27e6 `Id A x y \u27e7 = x \u2261 y\n\n_`\u2192_ : (A B : Type) \u2192 Type\nA `\u2192 B = `\u03a0 A (const B)\n\n_`\u00d7_ : (A B : Type) \u2192 Type\nA `\u00d7 B = `\u03a3 A (const B)\n\n----------------------------------------------------------------------\n\n{-\nTactics are represented as generic functions taking a Dynamic value (of\nany type), and returning a Dynamic value (at a possibly different type).\nThe convention is to behave like the identity function if the tactic\ndoesn't match the current value or fails.\n\nIn practice, the context will be a List of Dynamic values, and a tactic\nwill map a Context to a Context. For simplicity, the tactics I give below\nonly map a Dynamic to a Dynamic.\n-}\n\nDynamic : Set\nDynamic = \u03a3 Type \u27e6_\u27e7\n\nTactic : Set\nTactic = Dynamic \u2192 Dynamic\n\n----------------------------------------------------------------------\n\n{-\nHere is an example of a tactic that only needs to pattern match on a\nvariable or constructor, but not a function. We don't encounter\nany problems when writing this kind of a tactic.\n\nThe tactic below changes a `Fin n` value into a `Fin (n + 0)` value.\n-}\n\nadd-plus0-Fin : Tactic\nadd-plus0-Fin (`Fin n , i) = `Fin (n + 0) , subst Fin (plusrident n) i\nadd-plus0-Fin x = x\n\n----------------------------------------------------------------------\n\n{-\nIn contrast, it is not straightforward to write tactics that need\nto pattern match on function calls.\nFor example, below is ideally what we want to write to change\na value of `Fin (n + 0)` to a value of `Fin n`.\n-}\n\n-- rm-plus0-Fin : Tactic\n-- rm-plus0-Fin (`Fin (n + 0) , i) = `Fin n , subst Fin (sym (plusrident n)) i\n-- rm-plus0-Fin x = x\n\n----------------------------------------------------------------------\n\n{-\nInstead of matching directly on `Fin, we can match on a `\u03a6 that\nrepresents `Fin. This allows us to match on the the `+ function call\nin the index position.\n\nBecause we cannot match against the function `el` (we would like to match\nit against `eval`), we add to our return type that proves the\nextentional equality between `el` and `eval`.\n\nAlso note that the tactic below can be used for any type indexed by\n\u2115, not just Fin.\n-}\n\nrm-plus0 : Tactic\nrm-plus0 (`\u03a6 `Arith `\u2115 el B (a `+ `zero) , \u03c6 b) =\n `\u03a0 `Arith (\u03bb x \u2192 `Id `\u2115 (el x) (eval x))\n `\u2192\n `\u03a6 `Arith `\u2115 el B a\n ,\n \u03bb f \u2192 \u03c6\n (subst (\u03bb x \u2192 \u27e6 B x \u27e7)\n (sym (f a))\n (subst (\u03bb x \u2192 \u27e6 B x \u27e7)\n (sym (plusrident (eval a)))\n (subst (\u03bb x \u2192 \u27e6 B x \u27e7)\n (f (a `+ `zero))\n b)))\nrm-plus0 x = x\n\n----------------------------------------------------------------------\n\n{-\nNow it is possible to apply the generic tactic to a specific\nvalue and have the desired behavior occur. Notice that the generated\nextentional equality premise is trivially provable because\neval is always equal to eval.\n-}\n\neg-rm-plus0 : (a : Arith)\n \u2192 \u27e6 `\u03a6 `Arith `\u2115 eval `Fin (a `+ `zero) \u27e7\n \u2192 \u27e6 `\u03a6 `Arith `\u2115 eval `Fin a \u27e7\neg-rm-plus0 a (\u03c6 i) = proj\u2082\n (rm-plus0 (`\u03a6 `Arith `\u2115 eval `Fin (a `+ `zero) , \u03c6 i))\n (\u03bb _ \u2192 refl)\n\n----------------------------------------------------------------------\n\n{-\nHere is the same tactic but using the specialized \u03a6Arith type.\n-}\n\nrm-plus0-Arith : Tactic\nrm-plus0-Arith (`\u03a6Arith B (a `+ `zero) , \u03c6 i) =\n `\u03a6Arith B a\n ,\n \u03c6 (subst (\u03bb x \u2192 \u27e6 B x \u27e7) (sym (plusrident (eval a))) i)\nrm-plus0-Arith x = x\n\n----------------------------------------------------------------------\n\n{-\nAnd the same example usage, this time without needing to prove a premise.\n-}\n\neg-rm-plus0-Arith : (a : Arith)\n \u2192 \u27e6 `\u03a6Arith `Fin (a `+ `zero) \u27e7\n \u2192 \u27e6 `\u03a6Arith `Fin a \u27e7\neg-rm-plus0-Arith a (\u03c6 i) = proj\u2082\n (rm-plus0-Arith (`\u03a6Arith `Fin (a `+ `zero) , \u03c6 i))\n\n----------------------------------------------------------------------\n\n{-\nFinally, here is the same tactic but using the even more \nspecialized Fin\u2082 type.\n-}\n\nrm-plus0-Fin : Tactic\nrm-plus0-Fin (`Fin\u2082 (a `+ `zero) , fin i) =\n `Fin\u2082 a\n ,\n fin (subst (\u03bb x \u2192 \u27e6 `Fin x \u27e7) (sym (plusrident (eval a))) i)\nrm-plus0-Fin x = x\n\n----------------------------------------------------------------------\n\neg-rm-plus0-Fin : (a : Arith)\n \u2192 \u27e6 `Fin\u2082 (a `+ `zero) \u27e7\n \u2192 \u27e6 `Fin\u2082 a \u27e7\neg-rm-plus0-Fin a (fin i) = proj\u2082\n (rm-plus0-Fin (`Fin\u2082 (a `+ `zero) , fin i))\n\n----------------------------------------------------------------------\n\n{-\nA tactic to simplify by one step of `+.\n-}\n\nsimp-step-plus0-Fin : Tactic\nsimp-step-plus0-Fin (`Fin\u2082 (`zero `+ a) , fin i) =\n `Fin\u2082 a , fin i\nsimp-step-plus0-Fin (`Fin\u2082 (`suc a\u2081 `+ a\u2082) , fin i) =\n `Fin\u2082 (`suc (a\u2081 `+ a\u2082)) , fin i\nsimp-step-plus0-Fin x = x\n\n----------------------------------------------------------------------\n\n{-\nA semantics-preserving definitional evaluator, and a tactic\nto simplify as far as possible using definitional equality.\nThis is like the \"simp\" tactic in Coq.\n-}\n\neval\u2082 : (a\u2081 : Arith) \u2192 \u03a3 Arith (\u03bb a\u2082 \u2192 eval a\u2081 \u2261 eval a\u2082)\neval\u2082 `zero = `zero , refl\neval\u2082 (`suc a)\n with eval\u2082 a\n... | a\u2032 , p\n rewrite p = `suc a\u2032 , refl\neval\u2082 (`zero `+ a\u2082) = eval\u2082 a\u2082\neval\u2082 (`suc a\u2081 `+ a\u2082)\n with eval\u2082 a\u2081 | eval\u2082 a\u2082\n... | a\u2081\u2032 , p | a\u2082\u2032 , q\n rewrite p | q = `suc (a\u2081\u2032 `+ a\u2082\u2032) , refl\neval\u2082 (a\u2081 `+ a\u2082)\n with eval\u2082 a\u2081 | eval\u2082 a\u2082\n... | a\u2081\u2032 , p | a\u2082\u2032 , q\n rewrite p | q = (a\u2081\u2032 `+ a\u2082\u2032) , refl\neval\u2082 (`zero `* a\u2082) = `zero , refl\neval\u2082 (`suc a\u2081 `* a\u2082)\n with eval\u2082 a\u2081 | eval\u2082 a\u2082\n... | a\u2081\u2032 , p | a\u2082\u2032 , q\n rewrite p | q = a\u2082\u2032 `+ (a\u2081\u2032 `* a\u2082\u2032) , refl\neval\u2082 (a\u2081 `* a\u2082)\n with eval\u2082 a\u2081 | eval\u2082 a\u2082\n... | a\u2081\u2032 , p | a\u2082\u2032 , q\n rewrite p | q = (a\u2081\u2032 `* a\u2082\u2032) , refl\n\nsimp-Fin : Tactic\nsimp-Fin (`Fin\u2082 a , fin i)\n with eval\u2082 a\n... | a\u2032 , p rewrite p =\n `Fin\u2082 a\u2032 , fin i\nsimp-Fin x = x\n\n----------------------------------------------------------------------\n","old_contents":"{- \nThis file demonstrates a technique that simulates the ability to\npattern match against function calls rather than just variables\nor constructors.\n\nThis technique is necessary to write certain kinds of tactics\nas generic functions.\n-}\n\nmodule TacticsMatchingFunctionCalls where\nopen import Data.Bool hiding ( _\u225f_ )\nopen import Data.Nat\nopen import Data.Fin hiding ( _+_ )\nopen import Data.Product hiding ( map )\nopen import Data.List\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\nopen import Function\n\n----------------------------------------------------------------------\n\n{-\nZero is the right identity of addition on the natural numbers.\n-}\n\nplusrident : (n : \u2115) \u2192 n \u2261 n + 0\nplusrident zero = refl\nplusrident (suc n) = cong suc (plusrident n)\n\n----------------------------------------------------------------------\n\n{-\nArith is a universe of codes representing the natural numbers\nalong with a limited set of functions over them. We would like to write\ntactics that pattern match against types indexed by applications of\nthese functions.\n-}\n\ndata Arith : Set where\n `zero : Arith\n `suc : (a : Arith) \u2192 Arith\n _`+_ _`*_ : (a\u2081 a\u2082 : Arith) \u2192 Arith\n\neval : Arith \u2192 \u2115\neval `zero = zero\neval (`suc a) = suc (eval a)\neval (a\u2081 `+ a\u2082) = eval a\u2081 + eval a\u2082\neval (a\u2081 `* a\u2082) = eval a\u2081 * eval a\u2082\n\n----------------------------------------------------------------------\n\n{-\n\u03a6 (standing for *function*-indexed types)\nrepresents an indexed type, but we *can* pattern\nmatch on function calls in its index position.\n\nA - The type of codes, including constructors and functions.\n i.e. Arith\nA\u2032 - The model underlying the codes.\n i.e. \u2115\nel - The evaluation function from code values to model values.\n i.e. eval\nB - The genuine indexed datatype that \u03a6 represents.\n i.e. Fin\na - The index value as a code, which may be a function call and hence supports\n pattern matching.\n i.e. (a `+ `zero)\n-}\n\ndata \u03a6 (A A\u2032 : Set) (el : A \u2192 A\u2032) (B : A\u2032 \u2192 Set) (a : A) : Set where\n \u03c6 : (b : B (el a)) \u2192 \u03a6 A A\u2032 el B a\n\n----------------------------------------------------------------------\n\n{-\nAlternatively, we can specialize \u03a6 to our Arith universe. This may be\na good use of our ability to cheaply define new datatypes via Desc.\n-}\n\ndata \u03a6Arith (B : \u2115 \u2192 Set) (a : Arith) : Set where\n \u03c6 : (b : B (eval a)) \u2192 \u03a6Arith B a\n\n----------------------------------------------------------------------\n\n{-\nGoing further, we could even specialize B in \u03a6Arith to Fin.\n-}\n\ndata Fin\u2082 (a : Arith) : Set where\n fin : (i : Fin (eval a)) \u2192 Fin\u2082 a\n\n----------------------------------------------------------------------\n\n{-\nA small universe of dependent types that is sufficient for the\nexamples given below.\n-}\n\ndata Type : Set\n\u27e6_\u27e7 : Type \u2192 Set\n\ndata Type where\n `Bool `\u2115 `Arith : Type\n `Fin : (n : \u2115) \u2192 Type\n `\u03a6 : (A A\u2032 : Type)\n (el : \u27e6 A \u27e7 \u2192 \u27e6 A\u2032 \u27e7)\n (B : \u27e6 A\u2032 \u27e7 \u2192 Type)\n (a : \u27e6 A \u27e7)\n \u2192 Type\n `\u03a6Arith : (B : \u2115 \u2192 Type) (a : Arith) \u2192 Type\n `Fin\u2082 : (a : Arith) \u2192 Type\n `\u03a0 `\u03a3 : (A : Type) (B : \u27e6 A \u27e7 \u2192 Type) \u2192 Type\n `Id : (A : Type) (x y : \u27e6 A \u27e7) \u2192 Type\n\n\u27e6 `Bool \u27e7 = Bool\n\u27e6 `\u2115 \u27e7 = \u2115\n\u27e6 `Arith \u27e7 = Arith\n\u27e6 `Fin n \u27e7 = Fin n\n\u27e6 `\u03a6 A A\u2032 el B a \u27e7 = \u03a6 \u27e6 A \u27e7 \u27e6 A\u2032 \u27e7 el (\u03bb a \u2192 \u27e6 B a \u27e7) a\n\u27e6 `\u03a6Arith B a \u27e7 = \u03a6Arith (\u03bb x \u2192 \u27e6 B x \u27e7) a\n\u27e6 `Fin\u2082 a \u27e7 = Fin\u2082 a\n\u27e6 `\u03a0 A B \u27e7 = (a : \u27e6 A \u27e7) \u2192 \u27e6 B a \u27e7\n\u27e6 `\u03a3 A B \u27e7 = \u03a3 \u27e6 A \u27e7 (\u03bb a \u2192 \u27e6 B a \u27e7)\n\u27e6 `Id A x y \u27e7 = x \u2261 y\n\n_`\u2192_ : (A B : Type) \u2192 Type\nA `\u2192 B = `\u03a0 A (const B)\n\n_`\u00d7_ : (A B : Type) \u2192 Type\nA `\u00d7 B = `\u03a3 A (const B)\n\n----------------------------------------------------------------------\n\n{-\nTactics are represented as generic functions taking a Dynamic value (of\nany type), and returning a Dynamic value (at a possibly different type).\nThe convention is to behave like the identity function if the tactic\ndoesn't match the current value or fails.\n\nIn practice, the context will be a List of Dynamic values, and a tactic\nwill map a Context to a Context. For simplicity, the tactics I give below\nonly map a Dynamic to a Dynamic.\n-}\n\nDynamic : Set\nDynamic = \u03a3 Type \u27e6_\u27e7\n\nTactic : Set\nTactic = Dynamic \u2192 Dynamic\n\n----------------------------------------------------------------------\n\n{-\nHere is an example of a tactic that only needs to pattern match on a\nvariable or constructor, but not a function. We don't encounter\nany problems when writing this kind of a tactic.\n\nThe tactic below changes a `Fin n` value into a `Fin (n + 0)` value.\n-}\n\nadd-plus0-Fin : Tactic\nadd-plus0-Fin (`Fin n , i) = `Fin (n + 0) , subst Fin (plusrident n) i\nadd-plus0-Fin x = x\n\n----------------------------------------------------------------------\n\n{-\nIn contrast, it is not straightforward to write tactics that need\nto pattern match on function calls.\nFor example, below is ideally what we want to write to change\na value of `Fin (n + 0)` to a value of `Fin n`.\n-}\n\n-- rm-plus0-Fin : Tactic\n-- rm-plus0-Fin (`Fin (n + 0) , i) = `Fin n , subst Fin (sym (plusrident n)) i\n-- rm-plus0-Fin x = x\n\n----------------------------------------------------------------------\n\n{-\nInstead of matching directly on `Fin, we can match on a `\u03a6 that\nrepresents `Fin. This allows us to match on the the `+ function call\nin the index position.\n\nBecause we cannot match against the function `el` (we would like to match\nit against `eval`), we add to our return type that proves the\nextentional equality between `el` and `eval`.\n\nAlso note that the tactic below can be used for any type indexed by\n\u2115, not just Fin.\n-}\n\nrm-plus0 : Tactic\nrm-plus0 (`\u03a6 `Arith `\u2115 el B (a `+ `zero) , \u03c6 b) =\n `\u03a0 `Arith (\u03bb x \u2192 `Id `\u2115 (el x) (eval x))\n `\u2192\n `\u03a6 `Arith `\u2115 el B a\n ,\n \u03bb f \u2192 \u03c6\n (subst (\u03bb x \u2192 \u27e6 B x \u27e7)\n (sym (f a))\n (subst (\u03bb x \u2192 \u27e6 B x \u27e7)\n (sym (plusrident (eval a)))\n (subst (\u03bb x \u2192 \u27e6 B x \u27e7)\n (f (a `+ `zero))\n b)))\nrm-plus0 x = x\n\n----------------------------------------------------------------------\n\n{-\nNow it is possible to apply the generic tactic to a specific\nvalue and have the desired behavior occur. Notice that the generated\nextentional equality premise is trivially provable because\neval is always equal to eval.\n-}\n\neg-rm-plus0 : (a : Arith)\n \u2192 \u27e6 `\u03a6 `Arith `\u2115 eval `Fin (a `+ `zero) \u27e7\n \u2192 \u27e6 `\u03a6 `Arith `\u2115 eval `Fin a \u27e7\neg-rm-plus0 a (\u03c6 i) = proj\u2082\n (rm-plus0 (`\u03a6 `Arith `\u2115 eval `Fin (a `+ `zero) , \u03c6 i))\n (\u03bb _ \u2192 refl)\n\n----------------------------------------------------------------------\n\n{-\nHere is the same tactic but using the specialized \u03a6Arith type.\n-}\n\nrm-plus0-Arith : Tactic\nrm-plus0-Arith (`\u03a6Arith B (a `+ `zero) , \u03c6 i) =\n `\u03a6Arith B a\n ,\n \u03c6 (subst (\u03bb x \u2192 \u27e6 B x \u27e7) (sym (plusrident (eval a))) i)\nrm-plus0-Arith x = x\n\n----------------------------------------------------------------------\n\n{-\nAnd the same example usage, this time without needing to prove a premise.\n-}\n\neg-rm-plus0-Arith : (a : Arith)\n \u2192 \u27e6 `\u03a6Arith `Fin (a `+ `zero) \u27e7\n \u2192 \u27e6 `\u03a6Arith `Fin a \u27e7\neg-rm-plus0-Arith a (\u03c6 i) = proj\u2082\n (rm-plus0-Arith (`\u03a6Arith `Fin (a `+ `zero) , \u03c6 i))\n\n----------------------------------------------------------------------\n\n{-\nFinally, here is the same tactic but using the even more \nspecialized Fin\u2082 type.\n-}\n\nrm-plus0-Fin : Tactic\nrm-plus0-Fin (`Fin\u2082 (a `+ `zero) , fin i) =\n `Fin\u2082 a\n ,\n fin (subst (\u03bb x \u2192 \u27e6 `Fin x \u27e7) (sym (plusrident (eval a))) i)\nrm-plus0-Fin x = x\n\n----------------------------------------------------------------------\n\neg-rm-plus0-Fin : (a : Arith)\n \u2192 \u27e6 `Fin\u2082 (a `+ `zero) \u27e7\n \u2192 \u27e6 `Fin\u2082 a \u27e7\neg-rm-plus0-Fin a (fin i) = proj\u2082\n (rm-plus0-Fin (`Fin\u2082 (a `+ `zero) , fin i))\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"09aec6bcdb623c07e28d0f2d1ebe65cda4a07269","subject":"Changes due to Agda issue 953.","message":"Changes due to Agda issue 953.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/FOT\/Common\/FOL\/Relation\/Binary\/PropositionalEquality\/NoPatternMatchingOnRefl.agda","new_file":"notes\/FOT\/Common\/FOL\/Relation\/Binary\/PropositionalEquality\/NoPatternMatchingOnRefl.agda","new_contents":"------------------------------------------------------------------------------\n-- Propositional equality without using pattern matching on refl\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.Common.FOL.Relation.Binary.PropositionalEquality.NoPatternMatchingOnRefl where\n\nopen import Common.FOL.FOL using ( D )\n\n-- We add 3 to the fixities of the standard library.\ninfix 7 _\u2261_\n\n------------------------------------------------------------------------------\n-- The identity type on the universe of discourse.\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n-- Elimination rule.\nsubst : (A : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\nsubst A refl Ax = Ax\n\n-- Identity properties\n\nsym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\nsym {x} h = subst (\u03bb y' \u2192 y' \u2261 x) h refl\n\ntrans : \u2200 {x y z} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\ntrans {x} h\u2081 h\u2082 = subst (_\u2261_ x) h\u2082 h\u2081\n\ntrans\u2082 : \u2200 {w x y z} \u2192 w \u2261 x \u2192 x \u2261 y \u2192 y \u2261 z \u2192 w \u2261 z\ntrans\u2082 h\u2081 h\u2082 h\u2083 = trans (trans h\u2081 h\u2082) h\u2083\n\nsubst\u2082 : (A : D \u2192 D \u2192 Set) \u2192 \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192\n x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192\n A x\u2081 x\u2082 \u2192\n A y\u2081 y\u2082\nsubst\u2082 A {x\u2081} {x\u2082} {y\u2081} {y\u2082} h\u2081 h\u2082 h\u2083 =\n subst (\u03bb y\u2081' \u2192 A y\u2081' y\u2082) h\u2081 (subst (A x\u2081) h\u2082 h\u2083)\n\ncong : (f : D \u2192 D) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2261 f y\ncong f {x} h = subst (\u03bb x' \u2192 f x \u2261 f x') h refl\n\ncong\u2082 : (f : D \u2192 D \u2192 D) \u2192 \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192\n f x\u2081 x\u2082 \u2261 f y\u2081 y\u2082\ncong\u2082 f {x\u2081} {x\u2082} {y\u2081} {y\u2082} h\u2081 h\u2082 =\n subst (\u03bb x\u2081' \u2192 f x\u2081 x\u2082 \u2261 f x\u2081' y\u2082)\n h\u2081\n (subst (\u03bb x\u2082' \u2192 f x\u2081 x\u2082 \u2261 f x\u2081 x\u2082') h\u2082 refl)\n","old_contents":"------------------------------------------------------------------------------\n-- Propositional equality without using pattern matching on refl\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule\n FOT.Common.FOL.Relation.Binary.PropositionalEquality.NoPatternMatchingOnRefl\nwhere\n\nopen import Common.FOL.FOL using ( D )\n\n-- We add 3 to the fixities of the standard library.\ninfix 7 _\u2261_\n\n------------------------------------------------------------------------------\n-- The identity type on the universe of discourse.\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n-- Elimination rule.\nsubst : (A : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\nsubst A refl Ax = Ax\n\n-- Identity properties\n\nsym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\nsym {x} h = subst (\u03bb y' \u2192 y' \u2261 x) h refl\n\ntrans : \u2200 {x y z} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\ntrans {x} h\u2081 h\u2082 = subst (_\u2261_ x) h\u2082 h\u2081\n\ntrans\u2082 : \u2200 {w x y z} \u2192 w \u2261 x \u2192 x \u2261 y \u2192 y \u2261 z \u2192 w \u2261 z\ntrans\u2082 h\u2081 h\u2082 h\u2083 = trans (trans h\u2081 h\u2082) h\u2083\n\nsubst\u2082 : (A : D \u2192 D \u2192 Set) \u2192 \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192\n x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192\n A x\u2081 x\u2082 \u2192\n A y\u2081 y\u2082\nsubst\u2082 A {x\u2081} {x\u2082} {y\u2081} {y\u2082} h\u2081 h\u2082 h\u2083 =\n subst (\u03bb y\u2081' \u2192 A y\u2081' y\u2082) h\u2081 (subst (A x\u2081) h\u2082 h\u2083)\n\ncong : (f : D \u2192 D) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2261 f y\ncong f {x} h = subst (\u03bb x' \u2192 f x \u2261 f x') h refl\n\ncong\u2082 : (f : D \u2192 D \u2192 D) \u2192 \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192\n f x\u2081 x\u2082 \u2261 f y\u2081 y\u2082\ncong\u2082 f {x\u2081} {x\u2082} {y\u2081} {y\u2082} h\u2081 h\u2082 =\n subst (\u03bb x\u2081' \u2192 f x\u2081 x\u2082 \u2261 f x\u2081' y\u2082)\n h\u2081\n (subst (\u03bb x\u2082' \u2192 f x\u2081 x\u2082 \u2261 f x\u2081 x\u2082') h\u2082 refl)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"0f86619e6453a8b94f6c1ee2eca3f697e673f834","subject":"Proved that parametricity implies causality for signals.","message":"Proved that parametricity implies causality for signals.\n","repos":"agda\/agda-frp-js,agda\/agda-frp-js","old_file":"src\/agda\/FRP\/JS\/Model.agda","new_file":"src\/agda\/FRP\/JS\/Model.agda","new_contents":"open import FRP.JS.Level using ( Level ; _\u2294_ ) renaming ( zero to o ; suc to \u2191 )\nopen import FRP.JS.Time using ( Time ; _\u225f_ ; _\u2264_ ; _<_ )\nopen import FRP.JS.True using ( True )\nopen import FRP.JS.Nat using ( \u2115 ; zero ; suc )\n\nmodule FRP.JS.Model where\n\n-- Preliminaries\n\ninfixr 4 _+_\n\nrecord \u22a4 {\u03b1} : Set \u03b1 where\n constructor tt\n\nopen \u22a4 public\n\nrecord \u03a3 {\u03b1 \u03b2} (A : Set \u03b1) (B : A \u2192 Set \u03b2) : Set (\u03b1 \u2294 \u03b2) where\n constructor _,_\n field\n proj\u2081 : A\n proj\u2082 : B proj\u2081\n\nopen \u03a3 public\n\n_\u00d7_ : \u2200 {\u03b1 \u03b2} \u2192 Set \u03b1 \u2192 Set \u03b2 \u2192 Set (\u03b1 \u2294 \u03b2)\nA \u00d7 B = \u03a3 A (\u03bb a \u2192 B)\n\ndata _\u2261_ {\u03b1} {A : Set \u03b1} (a : A) : A \u2192 Set \u03b1 where\n refl : a \u2261 a\n\npostulate\n \u2264-refl : \u2200 t \u2192 True (t \u2264 t)\n \u2264-trans : \u2200 t u v \u2192 True (t \u2264 u) \u2192 True (u \u2264 v) \u2192 True (t \u2264 v)\n\n-- Relations on Set\n\n_\u220b_\u2194_ : \u2200 \u03b1 \u2192 Set \u03b1 \u2192 Set \u03b1 \u2192 Set (\u2191 \u03b1)\n\u03b1 \u220b A \u2194 B = A \u2192 B \u2192 Set \u03b1\n\n-- RSets and relations on RSet\n\nRSet : \u2200 \u03b1 \u2192 Set (\u2191 \u03b1)\nRSet \u03b1 = Time \u2192 Set \u03b1\n\nRSet\u2080 = RSet o\nRSet\u2081 = RSet (\u2191 o)\n\n_\u220b_\u21d4_ : \u2200 \u03b1 \u2192 RSet \u03b1 \u2192 RSet \u03b1 \u2192 Set (\u2191 \u03b1)\n\u03b1 \u220b A \u21d4 B = \u2200 t \u2192 (\u03b1 \u220b A t \u2194 B t)\n\n--- Level sequences\n\ndata Levels : Set where\n \u03b5 : Levels\n _,_ : \u2200 (\u03b1s : Levels) (\u03b1 : Level) \u2192 Levels\n\nmax : Levels \u2192 Level\nmax \u03b5 = o\nmax (\u03b1s , \u03b1) = max \u03b1s \u2294 \u03b1\n\n-- Sequences of Sets\n\nSets : \u2200 \u03b1s \u2192 Set (\u2191 (max \u03b1s))\nSets \u03b5 = \u22a4\nSets (\u03b1s , \u03b1) = Sets \u03b1s \u00d7 Set \u03b1\n\n\u27e8_\u220b_\u27e9 : \u2200 \u03b1s \u2192 Sets \u03b1s \u2192 Set (max \u03b1s)\n\u27e8 \u03b5 \u220b tt \u27e9 = \u22a4\n\u27e8 (\u03b1s , \u03b1) \u220b (As , A) \u27e9 = \u27e8 \u03b1s \u220b As \u27e9 \u00d7 A\n\n_\u220b_\u2194*_ : \u2200 \u03b1s \u2192 Sets \u03b1s \u2192 Sets \u03b1s \u2192 Set (\u2191 (max \u03b1s))\n\u03b5 \u220b tt \u2194* tt = \u22a4\n(\u03b1s , \u03b1) \u220b (As , A) \u2194* (Bs , B) = (\u03b1s \u220b As \u2194* Bs) \u00d7 (\u03b1 \u220b A \u2194 B)\n\n\u27e8_\u220b_\u27e9\u00b2 : \u2200 \u03b1s {As Bs} \u2192 (\u03b1s \u220b As \u2194* Bs) \u2192 (max \u03b1s \u220b \u27e8 \u03b1s \u220b As \u27e9 \u2194 \u27e8 \u03b1s \u220b Bs \u27e9)\n\u27e8 \u03b5 \u220b tt \u27e9\u00b2 tt tt = \u22a4\n\u27e8 (\u03b1s , \u03b1) \u220b (\u211cs , \u211c) \u27e9\u00b2 (as , a) (bs , b) = (\u27e8 \u03b1s \u220b \u211cs \u27e9\u00b2 as bs) \u00d7 (\u211c a b)\n\n-- Sequences of RSets\n\nRSets : \u2200 \u03b1s \u2192 Set (\u2191 (max \u03b1s))\nRSets \u03b5 = \u22a4\nRSets (\u03b1s , \u03b1) = RSets \u03b1s \u00d7 RSet \u03b1\n\n_\u220b_\u21d4*_ : \u2200 \u03b1s \u2192 RSets \u03b1s \u2192 RSets \u03b1s \u2192 Set (\u2191 (max \u03b1s))\n\u03b5 \u220b tt \u21d4* tt = \u22a4\n(\u03b1s , \u03b1) \u220b (As , A) \u21d4* (Bs , B) = (\u03b1s \u220b As \u21d4* Bs) \u00d7 (\u03b1 \u220b A \u21d4 B)\n\n-- Concatenation of sequences\n\n_+_ : Levels \u2192 Levels \u2192 Levels\n\u03b1s + \u03b5 = \u03b1s\n\u03b1s + (\u03b2s , \u03b2) = (\u03b1s + \u03b2s) , \u03b2\n\n_\u220b_++_\u220b_ : \u2200 \u03b1s \u2192 RSets \u03b1s \u2192 \u2200 \u03b2s \u2192 RSets \u03b2s \u2192 RSets (\u03b1s + \u03b2s)\n\u03b1s \u220b As ++ \u03b5 \u220b tt = As\n\u03b1s \u220b As ++ (\u03b2s , \u03b2) \u220b (Bs , B) = ((\u03b1s \u220b As ++ \u03b2s \u220b Bs) , B)\n\n_\u220b_++\u00b2_\u220b_ : \u2200 \u03b1s {As Bs} \u2192 (\u03b1s \u220b As \u21d4* Bs) \u2192 \u2200 \u03b2s {Cs Ds} \u2192 (\u03b2s \u220b Cs \u21d4* Ds) \u2192 \n ((\u03b1s + \u03b2s) \u220b (\u03b1s \u220b As ++ \u03b2s \u220b Cs) \u21d4* (\u03b1s \u220b Bs ++ \u03b2s \u220b Ds))\n\u03b1s \u220b \u211cs ++\u00b2 \u03b5 \u220b tt = \u211cs\n\u03b1s \u220b \u211cs ++\u00b2 (\u03b2s , \u03b2) \u220b (\u2111s , \u2111) = ((\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) , \u2111)\n\n-- Intervals\n\n_[_,_] : \u2200 {\u03b1} \u2192 RSet \u03b1 \u2192 Time \u2192 Time \u2192 Set \u03b1\nA [ s , u ] = \u2200 t \u2192 True (s \u2264 t) \u2192 True (t \u2264 u) \u2192 A t\n\n_[_,_]\u00b2 : \u2200 {\u03b1 A B} \u2192 (\u03b1 \u220b A \u21d4 B) \u2192 \u2200 s u \u2192 (\u03b1 \u220b A [ s , u ] \u2194 B [ s , u ])\n(\u211c [ s , u ]\u00b2) \u03c3 \u03c4 = \u2200 t s\u2264t t\u2264u \u2192 \u211c t (\u03c3 t s\u2264t t\u2264u) (\u03c4 t s\u2264t t\u2264u)\n\n-- Type variables\n\ndata TVar : Levels \u2192 Set\u2081 where\n zero : \u2200 {\u03b1s \u03b1} \u2192 TVar (\u03b1s , \u03b1)\n suc : \u2200 {\u03b1s \u03b1} \u2192 (\u03c4 : TVar \u03b1s) \u2192 TVar (\u03b1s , \u03b1)\n\n\u03c4level : \u2200 {\u03b1s} \u2192 TVar \u03b1s \u2192 Level\n\u03c4level (zero {\u03b1 = \u03b1}) = \u03b1\n\u03c4level (suc \u03c4) = \u03c4level \u03c4\n\n\u03c4\u27e6_\u27e7 : \u2200 {\u03b1s} (\u03c4 : TVar \u03b1s) \u2192 RSets \u03b1s \u2192 RSet (\u03c4level \u03c4)\n\u03c4\u27e6 zero \u27e7 (As , A) = A\n\u03c4\u27e6 suc \u03c4 \u27e7 (As , A) = \u03c4\u27e6 \u03c4 \u27e7 As\n\n\u03c4\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} (\u03c4 : TVar \u03b1s) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 (\u03c4level \u03c4 \u220b \u03c4\u27e6 \u03c4 \u27e7 As \u21d4 \u03c4\u27e6 \u03c4 \u27e7 Bs)\n\u03c4\u27e6 zero \u27e7\u00b2 (\u211cs , \u211c) t a b = \u211c t a b\n\u03c4\u27e6 suc \u03c4 \u27e7\u00b2 (\u211cs , \u211c) t a b = \u03c4\u27e6 \u03c4 \u27e7\u00b2 \u211cs t a b\n\n-- Types\n\ndata Typ (\u03b1s : Levels) : Set\u2081 where\n \u27e8_\u27e9 : (A : Set) \u2192 Typ \u03b1s\n _\u2227_ _\u21d2_ _\u22b5_ : (T : Typ \u03b1s) \u2192 (U : Typ \u03b1s) \u2192 Typ \u03b1s\n tvar : (\u03c4 : TVar \u03b1s) \u2192 Typ \u03b1s\n univ : \u2200 \u03b1 \u2192 (T : Typ (\u03b1s , \u03b1)) \u2192 Typ \u03b1s\n\ntlevel : \u2200 {\u03b1s} \u2192 Typ \u03b1s \u2192 Level\ntlevel \u27e8 A \u27e9 = o\ntlevel (T \u2227 U) = tlevel T \u2294 tlevel U\ntlevel (T \u21d2 U) = tlevel T \u2294 tlevel U\ntlevel (T \u22b5 U) = tlevel T \u2294 tlevel U\ntlevel (tvar \u03c4) = \u03c4level \u03c4\ntlevel (univ \u03b1 T) = \u2191 \u03b1 \u2294 tlevel T\n\nT\u27e6_\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u2192 RSets \u03b1s \u2192 RSet (tlevel T)\nT\u27e6 \u27e8 A \u27e9 \u27e7 As t = A\nT\u27e6 T \u2227 U \u27e7 As t = T\u27e6 T \u27e7 As t \u00d7 T\u27e6 U \u27e7 As t\nT\u27e6 T \u21d2 U \u27e7 As t = T\u27e6 T \u27e7 As t \u2192 T\u27e6 U \u27e7 As t\nT\u27e6 T \u22b5 U \u27e7 As t = \u2200 u \u2192 True (t \u2264 u) \u2192 T\u27e6 T \u27e7 As [ t , u ] \u2192 T\u27e6 U \u27e7 As u\nT\u27e6 tvar \u03c4 \u27e7 As t = \u03c4\u27e6 \u03c4 \u27e7 As t\nT\u27e6 univ \u03b1 T \u27e7 As t = \u2200 (A : RSet \u03b1) \u2192 T\u27e6 T \u27e7 (As , A) t\n\nT\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} (T : Typ \u03b1s) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 (tlevel T \u220b T\u27e6 T \u27e7 As \u21d4 T\u27e6 T \u27e7 Bs)\nT\u27e6 \u27e8 A \u27e9 \u27e7\u00b2 \u211cs t a b = a \u2261 b\nT\u27e6 T \u2227 U \u27e7\u00b2 \u211cs t (a , b) (c , d) = T\u27e6 T \u27e7\u00b2 \u211cs t a c \u00d7 T\u27e6 U \u27e7\u00b2 \u211cs t b d\nT\u27e6 T \u21d2 U \u27e7\u00b2 \u211cs t f g = \u2200 {a b} \u2192 T\u27e6 T \u27e7\u00b2 \u211cs t a b \u2192 T\u27e6 U \u27e7\u00b2 \u211cs t (f a) (g b)\nT\u27e6 T \u22b5 U \u27e7\u00b2 \u211cs t f g = \u2200 u t\u2264u {\u03c3 \u03c4} \u2192 (T\u27e6 T \u27e7\u00b2 \u211cs [ t , u ]\u00b2) \u03c3 \u03c4 \u2192 T\u27e6 U \u27e7\u00b2 \u211cs u (f u t\u2264u \u03c3) (g u t\u2264u \u03c4)\nT\u27e6 tvar \u03c4 \u27e7\u00b2 \u211cs t v w = \u03c4\u27e6 \u03c4 \u27e7\u00b2 \u211cs t v w\nT\u27e6 univ \u03b1 T \u27e7\u00b2 \u211cs t f g = \u2200 {A B} (\u211c : \u03b1 \u220b A \u21d4 B) \u2192 T\u27e6 T \u27e7\u00b2 (\u211cs , \u211c) t (f A) (g B)\n\n-- Contexts\n\ndata Ctxt (\u03b1s : Levels) : Set\u2081 where\n \u03b5 : Ctxt \u03b1s\n _,_at_ : (\u0393 : Ctxt \u03b1s) (T : Typ \u03b1s) (t : Time) \u2192 Ctxt \u03b1s\n\nclevels : \u2200 {\u03b1s} \u2192 Ctxt \u03b1s \u2192 Levels\nclevels \u03b5 = \u03b5\nclevels (\u0393 , T at t) = (clevels \u0393 , tlevel T)\n\n\u0393\u27e6_\u27e7 : \u2200 {\u03b1s} (\u0393 : Ctxt \u03b1s) \u2192 RSets \u03b1s \u2192 Sets (clevels \u0393)\n\u0393\u27e6 \u03b5 \u27e7 As = tt\n\u0393\u27e6 \u0393 , T at t \u27e7 As = (\u0393\u27e6 \u0393 \u27e7 As , T\u27e6 T \u27e7 As t)\n\n\u0393\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} (\u0393 : Ctxt \u03b1s) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 (clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7 As \u2194* \u0393\u27e6 \u0393 \u27e7 Bs)\n\u0393\u27e6 \u03b5 \u27e7\u00b2 \u211cs = tt\n\u0393\u27e6 \u0393 , T at t \u27e7\u00b2 \u211cs = (\u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs , T\u27e6 T \u27e7\u00b2 \u211cs t)\n\n-- Weakening of type variables\n\n\u03c4weaken : \u2200 {\u03b1s} \u03b1 \u03b2s \u2192 TVar (\u03b1s + \u03b2s) \u2192 TVar ((\u03b1s , \u03b1) + \u03b2s)\n\u03c4weaken \u03b1 \u03b5 \u03c4 = suc \u03c4\n\u03c4weaken \u03b1 (\u03b2s , \u03b2) zero = zero\n\u03c4weaken \u03b1 (\u03b2s , \u03b2) (suc \u03c4) = suc (\u03c4weaken \u03b1 \u03b2s \u03c4)\n\n\u27e6\u03c4weaken\u27e7 : \u2200 {\u03b1s} \u03b1 \u03b2s (\u03c4 : TVar (\u03b1s + \u03b2s)) As A Bs t \u2192 \n \u03c4\u27e6 \u03c4 \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t \u2192 \u03c4\u27e6 \u03c4weaken \u03b1 \u03b2s \u03c4 \u27e7 ((\u03b1s , \u03b1) \u220b (As , A) ++ \u03b2s \u220b Bs) t\n\u27e6\u03c4weaken\u27e7 \u03b1 \u03b5 \u03c4 As A Bs t a = a\n\u27e6\u03c4weaken\u27e7 \u03b1 (\u03b2s , \u03b2) zero As A Bs t a = a\n\u27e6\u03c4weaken\u27e7 \u03b1 (\u03b2s , \u03b2) (suc \u03c4) As A (Bs , B) t a = \u27e6\u03c4weaken\u27e7 \u03b1 \u03b2s \u03c4 As A Bs t a\n\n\u27e6\u03c4weaken\u207b\u00b9\u27e7 : \u2200 {\u03b1s} \u03b1 \u03b2s (\u03c4 : TVar (\u03b1s + \u03b2s)) As A Bs t \u2192 \n \u03c4\u27e6 \u03c4weaken \u03b1 \u03b2s \u03c4 \u27e7 ((\u03b1s , \u03b1) \u220b (As , A) ++ \u03b2s \u220b Bs) t \u2192 \u03c4\u27e6 \u03c4 \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t\n\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b5 \u03c4 As A Bs t a = a\n\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 (\u03b2s , \u03b2) zero As A Bs t a = a\n\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 (\u03b2s , \u03b2) (suc \u03c4) As A (Bs , B) t a = \u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u03c4 As A Bs t a\n\n\u27e6\u03c4weaken\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 \u03b2s (\u03c4 : TVar (\u03b1s + \u03b2s)) {As Bs A B Cs Ds} \u211cs \u211c \u2111s t {a b} \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192 \n \u03c4\u27e6 \u03c4weaken \u03b1 \u03b2s \u03c4 \u27e7\u00b2 ((\u03b1s , \u03b1) \u220b (\u211cs , \u211c) ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6\u03c4weaken\u27e7 \u03b1 \u03b2s \u03c4 As A Cs t a) (\u27e6\u03c4weaken\u27e7 \u03b1 \u03b2s \u03c4 Bs B Ds t b)\n\u27e6\u03c4weaken\u27e7\u00b2 \u03b1 \u03b5 \u03c4 \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4weaken\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) zero \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4weaken\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) (suc \u03c4) \u211cs \u211c (\u2111s , \u2111) t a\u211cb = \u27e6\u03c4weaken\u27e7\u00b2 \u03b1 \u03b2s \u03c4 \u211cs \u211c \u2111s t a\u211cb\n\n\u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 \u03b2s (\u03c4 : TVar (\u03b1s + \u03b2s)) {As Bs A B Cs Ds} \u211cs \u211c \u2111s t {a b} \u2192\n \u03c4\u27e6 \u03c4weaken \u03b1 \u03b2s \u03c4 \u27e7\u00b2 ((\u03b1s , \u03b1) \u220b (\u211cs , \u211c) ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u03c4 As A Cs t a) (\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u03c4 Bs B Ds t b)\n\u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b5 \u03c4 \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) zero \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) (suc \u03c4) \u211cs \u211c (\u2111s , \u2111) t a\u211cb = \u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s \u03c4 \u211cs \u211c \u2111s t a\u211cb\n\n-- Weakening of types\n\ntweaken : \u2200 {\u03b1s} \u03b1 \u03b2s \u2192 Typ (\u03b1s + \u03b2s) \u2192 Typ ((\u03b1s , \u03b1) + \u03b2s)\ntweaken \u03b1 \u03b2s \u27e8 A \u27e9 = \u27e8 A \u27e9\ntweaken \u03b1 \u03b2s (T \u2227 U) = tweaken \u03b1 \u03b2s T \u2227 tweaken \u03b1 \u03b2s U\ntweaken \u03b1 \u03b2s (T \u21d2 U) = tweaken \u03b1 \u03b2s T \u21d2 tweaken \u03b1 \u03b2s U\ntweaken \u03b1 \u03b2s (T \u22b5 U) = tweaken \u03b1 \u03b2s T \u22b5 tweaken \u03b1 \u03b2s U\ntweaken \u03b1 \u03b2s (tvar \u03c4) = tvar (\u03c4weaken \u03b1 \u03b2s \u03c4)\ntweaken \u03b1 \u03b2s (univ \u03b2 T) = univ \u03b2 (tweaken \u03b1 (\u03b2s , \u03b2) T)\n\nmutual\n\n \u27e6tweaken\u27e7 : \u2200 {\u03b1s} \u03b1 \u03b2s (T : Typ (\u03b1s + \u03b2s)) As A Bs t \u2192 \n T\u27e6 T \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t \u2192 \n T\u27e6 tweaken \u03b1 \u03b2s T \u27e7 ((\u03b1s , \u03b1) \u220b (As , A) ++ \u03b2s \u220b Bs) t\n \u27e6tweaken\u27e7 \u03b1 \u03b2s \u27e8 B \u27e9 As A Bs t a = a\n \u27e6tweaken\u27e7 \u03b1 \u03b2s (T \u2227 U) As A Bs t (a , b) = (\u27e6tweaken\u27e7 \u03b1 \u03b2s T As A Bs t a , \u27e6tweaken\u27e7 \u03b1 \u03b2s U As A Bs t b)\n \u27e6tweaken\u27e7 \u03b1 \u03b2s (T \u21d2 U) As A Bs t f = \u03bb a \u2192 \u27e6tweaken\u27e7 \u03b1 \u03b2s U As A Bs t (f (\u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s T As A Bs t a))\n \u27e6tweaken\u27e7 \u03b1 \u03b2s (T \u22b5 U) As A Bs t f = \u03bb v t\u2264v \u03c3 \u2192 \u27e6tweaken\u27e7 \u03b1 \u03b2s U As A Bs v (f v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s T As A Bs u (\u03c3 u t\u2264u u\u2264v)))\n \u27e6tweaken\u27e7 \u03b1 \u03b2s (tvar \u03c4) As A Bs t a = \u27e6\u03c4weaken\u27e7 \u03b1 \u03b2s \u03c4 As A Bs t a\n \u27e6tweaken\u27e7 \u03b1 \u03b2s (univ \u03b2 T) As A Bs t f = \u03bb B \u2192 \u27e6tweaken\u27e7 \u03b1 (\u03b2s , \u03b2) T As A (Bs , B) t (f B)\n\n \u27e6tweaken\u207b\u00b9\u27e7 : \u2200 {\u03b1s} \u03b1 \u03b2s (T : Typ (\u03b1s + \u03b2s)) As A Bs t \u2192 \n T\u27e6 tweaken \u03b1 \u03b2s T \u27e7 ((\u03b1s , \u03b1) \u220b (As , A) ++ \u03b2s \u220b Bs) t \u2192\n T\u27e6 T \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u27e8 B \u27e9 As A Bs t a = a\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s (T \u2227 U) As A Bs t (a , b) = (\u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s T As A Bs t a , \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s U As A Bs t b)\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s (T \u21d2 U) As A Bs t f = \u03bb a \u2192 \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s U As A Bs t (f (\u27e6tweaken\u27e7 \u03b1 \u03b2s T As A Bs t a))\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s (T \u22b5 U) As A Bs t f = \u03bb v t\u2264v \u03c3 \u2192 \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s U As A Bs v (f v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tweaken\u27e7 \u03b1 \u03b2s T As A Bs u (\u03c3 u t\u2264u u\u2264v)))\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s (tvar \u03c4) As A Bs t a = \u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u03c4 As A Bs t a\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s (univ \u03b2 T) As A Bs t f = \u03bb B \u2192 \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 (\u03b2s , \u03b2) T As A (Bs , B) t (f B)\n\nmutual\n\n \u27e6tweaken\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 \u03b2s (T : Typ (\u03b1s + \u03b2s)) {As Bs A B Cs Ds} \u211cs \u211c \u2111s t {a b} \u2192\n T\u27e6 T \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192 \n T\u27e6 tweaken \u03b1 \u03b2s T \u27e7\u00b2 ((\u03b1s , \u03b1) \u220b (\u211cs , \u211c) ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6tweaken\u27e7 \u03b1 \u03b2s T As A Cs t a) (\u27e6tweaken\u27e7 \u03b1 \u03b2s T Bs B Ds t b)\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s \u27e8 B \u27e9 \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s (T \u2227 U) \u211cs \u211c \u2111s t (a\u211cb , c\u211cd) = (\u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s t a\u211cb , \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s t c\u211cd)\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s (T \u21d2 U) \u211cs \u211c \u2111s t f\u211cg = \u03bb a\u211cb \u2192 \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s t (f\u211cg (\u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s t a\u211cb))\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s (T \u22b5 U) \u211cs \u211c \u2111s t f\u211cg = \u03bb v t\u2264v \u03c3\u211c\u03c4 \u2192 \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s v (f\u211cg v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s u (\u03c3\u211c\u03c4 u t\u2264u u\u2264v)))\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s (tvar \u03c4) \u211cs \u211c \u2111s t a\u211cb = \u27e6\u03c4weaken\u27e7\u00b2 \u03b1 \u03b2s \u03c4 \u211cs \u211c \u2111s t a\u211cb\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s (univ \u03b2 T) \u211cs \u211c \u2111s t f\u211cg = \u03bb \u2111 \u2192 \u27e6tweaken\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) T \u211cs \u211c (\u2111s , \u2111) t (f\u211cg \u2111)\n\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 \u03b2s (T : Typ (\u03b1s + \u03b2s)) {As Bs A B Cs Ds} \u211cs \u211c \u2111s t {a b} \u2192\n T\u27e6 tweaken \u03b1 \u03b2s T \u27e7\u00b2 ((\u03b1s , \u03b1) \u220b (\u211cs , \u211c) ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n T\u27e6 T \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s T As A Cs t a) (\u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s T Bs B Ds t b)\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s \u27e8 B \u27e9 \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s (T \u2227 U) \u211cs \u211c \u2111s t (a\u211cb , c\u211cd) = (\u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s t a\u211cb , \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s t c\u211cd)\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s (T \u21d2 U) \u211cs \u211c \u2111s t f\u211cg = \u03bb a\u211cb \u2192 \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s t (f\u211cg (\u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s t a\u211cb))\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s (T \u22b5 U) \u211cs \u211c \u2111s t f\u211cg = \u03bb v t\u2264v \u03c3\u211c\u03c4 \u2192 \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s v (f\u211cg v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s u (\u03c3\u211c\u03c4 u t\u2264u u\u2264v)))\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s (tvar \u03c4) \u211cs \u211c \u2111s t a\u211cb = \u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s \u03c4 \u211cs \u211c \u2111s t a\u211cb\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s (univ \u03b2 T) \u211cs \u211c \u2111s t f\u211cg = \u03bb \u2111 \u2192 \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) T \u211cs \u211c (\u2111s , \u2111) t (f\u211cg \u2111)\n\n-- Weakening of contexts\n\ncweaken : \u2200 {\u03b1s} \u03b1 \u2192 Ctxt \u03b1s \u2192 Ctxt (\u03b1s , \u03b1)\ncweaken \u03b1 \u03b5 = \u03b5\ncweaken \u03b1 (\u0393 , T at t) = (cweaken \u03b1 \u0393 , tweaken \u03b1 \u03b5 T at t)\n \n\u27e6cweaken\u27e7 : \u2200 {\u03b1s} \u03b1 (\u0393 : Ctxt \u03b1s) As A \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7 As \u27e9 \u2192 \u27e8 clevels (cweaken \u03b1 \u0393) \u220b \u0393\u27e6 cweaken \u03b1 \u0393 \u27e7 (As , A) \u27e9\n\u27e6cweaken\u27e7 \u03b1 \u03b5 As A tt = tt\n\u27e6cweaken\u27e7 \u03b1 (\u0393 , T at t) As A (as , a) = (\u27e6cweaken\u27e7 \u03b1 \u0393 As A as , \u27e6tweaken\u27e7 \u03b1 \u03b5 T As A tt t a)\n\n\u27e6cweaken\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 (\u0393 : Ctxt \u03b1s) {As Bs A B} \u211cs \u211c {as bs} \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs \u27e9\u00b2 as bs \u2192\n \u27e8 clevels (cweaken \u03b1 \u0393) \u220b \u0393\u27e6 cweaken \u03b1 \u0393 \u27e7\u00b2 (\u211cs , \u211c) \u27e9\u00b2 (\u27e6cweaken\u27e7 \u03b1 \u0393 As A as) (\u27e6cweaken\u27e7 \u03b1 \u0393 Bs B bs)\n\u27e6cweaken\u27e7\u00b2 \u03b1 \u03b5 \u211cs \u211c tt = tt\n\u27e6cweaken\u27e7\u00b2 \u03b1 (\u0393 , T at t) \u211cs \u211c (as\u211cbs , a\u211cb) = (\u27e6cweaken\u27e7\u00b2 \u03b1 \u0393 \u211cs \u211c as\u211cbs , \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b5 T \u211cs \u211c tt t a\u211cb)\n\n-- Substitution into type variables\n\n\u03c4subst : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s \u2192 TVar ((\u03b1s , tlevel T) + \u03b2s) \u2192 Typ (\u03b1s + \u03b2s)\n\u03c4subst T \u03b5 zero = T\n\u03c4subst T \u03b5 (suc \u03c4) = tvar \u03c4\n\u03c4subst T (\u03b2s , \u03b2) zero = tvar zero\n\u03c4subst T (\u03b2s , \u03b2) (suc \u03c4) = tweaken \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4)\n\n\u27e6\u03c4subst\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (\u03c4 : TVar ((\u03b1s , tlevel T) + \u03b2s)) As Bs t \u2192\n \u03c4\u27e6 \u03c4 \u27e7 ((\u03b1s , tlevel T) \u220b (As , T\u27e6 T \u27e7 As) ++ \u03b2s \u220b Bs) t \u2192 \n T\u27e6 \u03c4subst T \u03b2s \u03c4 \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t\n\u27e6\u03c4subst\u27e7 T \u03b5 zero As Bs t a = a\n\u27e6\u03c4subst\u27e7 T \u03b5 (suc \u03c4) As Bs t a = a\n\u27e6\u03c4subst\u27e7 T (\u03b2s , \u03b2) zero As Bs t a = a \n\u27e6\u03c4subst\u27e7 {\u03b1s} T (\u03b2s , \u03b2) (suc \u03c4) As (Bs , B) t a = \n \u27e6tweaken\u27e7 \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4) (\u03b1s \u220b As ++ \u03b2s \u220b Bs) B tt t \n (\u27e6\u03c4subst\u27e7 T \u03b2s \u03c4 As Bs t a)\n\n\u27e6\u03c4subst\u207b\u00b9\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (\u03c4 : TVar ((\u03b1s , tlevel T) + \u03b2s)) As Bs t \u2192\n T\u27e6 \u03c4subst T \u03b2s \u03c4 \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t \u2192\n \u03c4\u27e6 \u03c4 \u27e7 ((\u03b1s , tlevel T) \u220b (As , T\u27e6 T \u27e7 As) ++ \u03b2s \u220b Bs) t\n\u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b5 zero As Bs t a = a\n\u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b5 (suc \u03c4) As Bs t a = a\n\u27e6\u03c4subst\u207b\u00b9\u27e7 T (\u03b2s , \u03b2) zero As Bs t a = a \n\u27e6\u03c4subst\u207b\u00b9\u27e7 {\u03b1s} T (\u03b2s , \u03b2) (suc \u03c4) As (Bs , B) t a = \n \u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b2s \u03c4 As Bs t \n (\u27e6tweaken\u207b\u00b9\u27e7 \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4) (\u03b1s \u220b As ++ \u03b2s \u220b Bs) B tt t a)\n\n\u27e6\u03c4subst\u27e7\u00b2 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (\u03c4 : TVar ((\u03b1s , tlevel T) + \u03b2s)) {As Bs Cs Ds} \u211cs \u2111s t {a b} \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 ((\u03b1s , tlevel T) \u220b (\u211cs , T\u27e6 T \u27e7\u00b2 \u211cs) ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n T\u27e6 \u03c4subst T \u03b2s \u03c4 \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6\u03c4subst\u27e7 T \u03b2s \u03c4 As Bs t a) (\u27e6\u03c4subst\u27e7 T \u03b2s \u03c4 Cs Ds t b)\n\u27e6\u03c4subst\u27e7\u00b2 T \u03b5 zero \u211cs \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4subst\u27e7\u00b2 T \u03b5 (suc \u03c4) \u211cs \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4subst\u27e7\u00b2 T (\u03b2s , \u03b2) zero \u211cs \u2111s t a\u211cb = a\u211cb \n\u27e6\u03c4subst\u27e7\u00b2 {\u03b1s} T (\u03b2s , \u03b2) (suc \u03c4) \u211cs (\u2111s , \u2111) t a\u211cb = \n \u27e6tweaken\u27e7\u00b2 \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4) (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) \u2111 tt t \n (\u27e6\u03c4subst\u27e7\u00b2 T \u03b2s \u03c4 \u211cs \u2111s t a\u211cb)\n\n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (\u03c4 : TVar ((\u03b1s , tlevel T) + \u03b2s)) {As Bs Cs Ds} \u211cs \u2111s t {a b} \u2192\n T\u27e6 \u03c4subst T \u03b2s \u03c4 \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 ((\u03b1s , tlevel T) \u220b (\u211cs , T\u27e6 T \u27e7\u00b2 \u211cs) ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b2s \u03c4 As Bs t a) (\u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b2s \u03c4 Cs Ds t b)\n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T \u03b5 zero \u211cs \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T \u03b5 (suc \u03c4) \u211cs \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T (\u03b2s , \u03b2) zero \u211cs \u2111s t a\u211cb = a\u211cb \n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 {\u03b1s} T (\u03b2s , \u03b2) (suc \u03c4) \u211cs (\u2111s , \u2111) t a\u211cb = \n \u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T \u03b2s \u03c4 \u211cs \u2111s t \n (\u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4) (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) \u2111 tt t a\u211cb)\n\n-- Substitution into types\n\ntsubst : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s \u2192 Typ ((\u03b1s , tlevel T) + \u03b2s) \u2192 Typ (\u03b1s + \u03b2s)\ntsubst T \u03b2s \u27e8 A \u27e9 = \u27e8 A \u27e9\ntsubst T \u03b2s (U \u2227 V) = tsubst T \u03b2s U \u2227 tsubst T \u03b2s V\ntsubst T \u03b2s (U \u21d2 V) = tsubst T \u03b2s U \u21d2 tsubst T \u03b2s V\ntsubst T \u03b2s (U \u22b5 V) = tsubst T \u03b2s U \u22b5 tsubst T \u03b2s V\ntsubst T \u03b2s (tvar \u03c4) = \u03c4subst T \u03b2s \u03c4\ntsubst T \u03b2s (univ \u03b2 U) = univ \u03b2 (tsubst T (\u03b2s , \u03b2) U)\n\nmutual\n\n \u27e6tsubst\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (U : Typ ((\u03b1s , tlevel T) + \u03b2s)) As Bs t \u2192\n T\u27e6 U \u27e7 ((\u03b1s , tlevel T) \u220b (As , T\u27e6 T \u27e7 As) ++ \u03b2s \u220b Bs) t \u2192 \n T\u27e6 tsubst T \u03b2s U \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t\n \u27e6tsubst\u27e7 T \u03b2s \u27e8 A \u27e9 As Bs t a = a\n \u27e6tsubst\u27e7 T \u03b2s (U \u2227 V) As Bs t (a , b) = (\u27e6tsubst\u27e7 T \u03b2s U As Bs t a , \u27e6tsubst\u27e7 T \u03b2s V As Bs t b)\n \u27e6tsubst\u27e7 T \u03b2s (U \u21d2 V) As Bs t f = \u03bb a \u2192 \u27e6tsubst\u27e7 T \u03b2s V As Bs t (f (\u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s U As Bs t a))\n \u27e6tsubst\u27e7 T \u03b2s (U \u22b5 V) As Bs t f = \u03bb v t\u2264v \u03c3 \u2192 \u27e6tsubst\u27e7 T \u03b2s V As Bs v (f v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s U As Bs u (\u03c3 u t\u2264u u\u2264v)))\n \u27e6tsubst\u27e7 T \u03b2s (tvar \u03c4) As Bs t a = \u27e6\u03c4subst\u27e7 T \u03b2s \u03c4 As Bs t a\n \u27e6tsubst\u27e7 T \u03b2s (univ \u03b2 U) As Bs t f = \u03bb B \u2192 \u27e6tsubst\u27e7 T (\u03b2s , \u03b2) U As (Bs , B) t (f B)\n\n \u27e6tsubst\u207b\u00b9\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (U : Typ ((\u03b1s , tlevel T) + \u03b2s)) As Bs t \u2192\n T\u27e6 tsubst T \u03b2s U \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t \u2192\n T\u27e6 U \u27e7 ((\u03b1s , tlevel T) \u220b (As , T\u27e6 T \u27e7 As) ++ \u03b2s \u220b Bs) t\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s \u27e8 A \u27e9 As Bs t a = a\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s (U \u2227 V) As Bs t (a , b) = (\u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s U As Bs t a , \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s V As Bs t b)\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s (U \u21d2 V) As Bs t f = \u03bb a \u2192 \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s V As Bs t (f (\u27e6tsubst\u27e7 T \u03b2s U As Bs t a))\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s (U \u22b5 V) As Bs t f = \u03bb v t\u2264v \u03c3 \u2192 \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s V As Bs v (f v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tsubst\u27e7 T \u03b2s U As Bs u (\u03c3 u t\u2264u u\u2264v)))\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s (tvar \u03c4) As Bs t a = \u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b2s \u03c4 As Bs t a\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s (univ \u03b2 U) As Bs t f = \u03bb B \u2192 \u27e6tsubst\u207b\u00b9\u27e7 T (\u03b2s , \u03b2) U As (Bs , B) t (f B)\n\nmutual\n\n \u27e6tsubst\u27e7\u00b2 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (U : Typ ((\u03b1s , tlevel T) + \u03b2s)) {As Bs Cs Ds} \u211cs \u2111s t {a b} \u2192\n T\u27e6 U \u27e7\u00b2 ((\u03b1s , tlevel T) \u220b (\u211cs , T\u27e6 T \u27e7\u00b2 \u211cs) ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n T\u27e6 tsubst T \u03b2s U \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6tsubst\u27e7 T \u03b2s U As Bs t a) (\u27e6tsubst\u27e7 T \u03b2s U Cs Ds t b)\n \u27e6tsubst\u27e7\u00b2 T \u03b2s \u27e8 A \u27e9 \u211cs \u2111s t a\u211cb = a\u211cb\n \u27e6tsubst\u27e7\u00b2 T \u03b2s (U \u2227 V) \u211cs \u2111s t (a\u211cb , c\u211cd) = (\u27e6tsubst\u27e7\u00b2 T \u03b2s U \u211cs \u2111s t a\u211cb , \u27e6tsubst\u27e7\u00b2 T \u03b2s V \u211cs \u2111s t c\u211cd)\n \u27e6tsubst\u27e7\u00b2 T \u03b2s (U \u21d2 V) \u211cs \u2111s t f\u211cg = \u03bb a\u211cb \u2192 \u27e6tsubst\u27e7\u00b2 T \u03b2s V \u211cs \u2111s t (f\u211cg (\u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s U \u211cs \u2111s t a\u211cb))\n \u27e6tsubst\u27e7\u00b2 T \u03b2s (U \u22b5 V) \u211cs \u2111s t f\u211cg = \u03bb v t\u2264v \u03c3\u211c\u03c4 \u2192 \u27e6tsubst\u27e7\u00b2 T \u03b2s V \u211cs \u2111s v (f\u211cg v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s U \u211cs \u2111s u (\u03c3\u211c\u03c4 u t\u2264u u\u2264v)))\n \u27e6tsubst\u27e7\u00b2 T \u03b2s (tvar \u03c4) \u211cs \u2111s t a\u211cb = \u27e6\u03c4subst\u27e7\u00b2 T \u03b2s \u03c4 \u211cs \u2111s t a\u211cb\n \u27e6tsubst\u27e7\u00b2 T \u03b2s (univ \u03b2 U) \u211cs \u2111s t f\u211cg = \u03bb \u2111 \u2192 \u27e6tsubst\u27e7\u00b2 T (\u03b2s , \u03b2) U \u211cs (\u2111s , \u2111) t (f\u211cg \u2111)\n\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (U : Typ ((\u03b1s , tlevel T) + \u03b2s)) {As Bs Cs Ds} \u211cs \u2111s t {a b} \u2192\n T\u27e6 tsubst T \u03b2s U \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n T\u27e6 U \u27e7\u00b2 ((\u03b1s , tlevel T) \u220b (\u211cs , T\u27e6 T \u27e7\u00b2 \u211cs) ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s U As Bs t a) (\u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s U Cs Ds t b)\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s \u27e8 A \u27e9 \u211cs \u2111s t a\u211cb = a\u211cb\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s (U \u2227 V) \u211cs \u2111s t (a\u211cb , c\u211cd) = (\u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s U \u211cs \u2111s t a\u211cb , \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s V \u211cs \u2111s t c\u211cd)\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s (U \u21d2 V) \u211cs \u2111s t f\u211cg = \u03bb a\u211cb \u2192 \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s V \u211cs \u2111s t (f\u211cg (\u27e6tsubst\u27e7\u00b2 T \u03b2s U \u211cs \u2111s t a\u211cb))\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s (U \u22b5 V) \u211cs \u2111s t f\u211cg = \u03bb v t\u2264v \u03c3\u211c\u03c4 \u2192 \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s V \u211cs \u2111s v (f\u211cg v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tsubst\u27e7\u00b2 T \u03b2s U \u211cs \u2111s u (\u03c3\u211c\u03c4 u t\u2264u u\u2264v)))\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s (tvar \u03c4) \u211cs \u2111s t a\u211cb = \u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T \u03b2s \u03c4 \u211cs \u2111s t a\u211cb\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s (univ \u03b2 U) \u211cs \u2111s t f\u211cg = \u03bb \u2111 \u2192 \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T (\u03b2s , \u03b2) U \u211cs (\u2111s , \u2111) t (f\u211cg \u2111)\n\n-- Variables\n\ndata Var {\u03b1s} (T : Typ \u03b1s) (t : Time) : (\u0393 : Ctxt \u03b1s) \u2192 Set\u2081 where\n zero : \u2200 {\u0393 : Ctxt \u03b1s} \u2192 Var T t (\u0393 , T at t)\n suc : \u2200 {\u0393 : Ctxt \u03b1s} {U : Typ \u03b1s} {u} \u2192 Var T t \u0393 \u2192 Var T t (\u0393 , U at u)\n\nv\u27e6_\u27e7 : \u2200 {\u03b1s} {\u0393 : Ctxt \u03b1s} {T : Typ \u03b1s} {t} \u2192 Var T t \u0393 \u2192 \u2200 As \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7 As \u27e9 \u2192 T\u27e6 T \u27e7 As t\nv\u27e6 zero \u27e7 As (as , a) = a\nv\u27e6 suc v \u27e7 As (as , a) = v\u27e6 v \u27e7 As as\n\nv\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} {\u0393 : Ctxt \u03b1s} {T : Typ \u03b1s} {t} (\u03c4 : Var T t \u0393) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 \n \u2200 {as bs} \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs \u27e9\u00b2 as bs \u2192 T\u27e6 T \u27e7\u00b2 \u211cs t (v\u27e6 \u03c4 \u27e7 As as) (v\u27e6 \u03c4 \u27e7 Bs bs)\nv\u27e6 zero \u27e7\u00b2 \u211cs (as\u211cbs , a\u211cb) = a\u211cb\nv\u27e6 suc v \u27e7\u00b2 \u211cs (as\u211cbs , a\u211cb) = v\u27e6 v \u27e7\u00b2 \u211cs as\u211cbs\n\n-- Expressions\n\ndata Exp : \u2200 {\u03b1s} \u2192 Ctxt \u03b1s \u2192 Typ \u03b1s \u2192 RSet\u2081 where\n const : \u2200 {\u03b1s \u0393 A t} \u2192 (a : A) \u2192 Exp {\u03b1s} \u0393 \u27e8 A \u27e9 t\n pair : \u2200 {\u03b1s \u0393 T U t} \u2192 (e : Exp \u0393 T t) \u2192 (f : Exp \u0393 U t) \u2192 Exp {\u03b1s} \u0393 (T \u2227 U) t\n fst : \u2200 {\u03b1s \u0393 T U t} \u2192 (e : Exp \u0393 (T \u2227 U) t) \u2192 Exp {\u03b1s} \u0393 T t\n snd : \u2200 {\u03b1s \u0393 T U t} \u2192 (e : Exp \u0393 (T \u2227 U) t) \u2192 Exp {\u03b1s} \u0393 U t\n abs : \u2200 {\u03b1s \u0393 T U t} \u2192 (e : Exp (\u0393 , T at t) U t) \u2192 Exp {\u03b1s} \u0393 (T \u21d2 U) t\n app : \u2200 {\u03b1s \u0393 T U t} \u2192 (f : Exp \u0393 (T \u21d2 U) t) \u2192 (e : Exp \u0393 T t) \u2192 Exp {\u03b1s} \u0393 U t\n var : \u2200 {\u03b1s \u0393 T t} \u2192 (v : Var T t \u0393) \u2192 Exp {\u03b1s} \u0393 T t\n tabs : \u2200 {\u03b1s \u0393} \u03b1 {T t} \u2192 (e : Exp (cweaken \u03b1 \u0393) T t) \u2192 Exp {\u03b1s} \u0393 (univ \u03b1 T) t\n tapp : \u2200 {\u03b1s \u0393 t} (T : Typ \u03b1s) {U} \u2192 (e : Exp \u0393 (univ (tlevel T) U) t) \u2192 Exp {\u03b1s} \u0393 (tsubst T \u03b5 U) t\n\ne\u27e6_\u27e7 : \u2200 {\u03b1s} {\u0393 : Ctxt \u03b1s} {T : Typ \u03b1s} {t} \u2192 Exp \u0393 T t \u2192 \u2200 As \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7 As \u27e9 \u2192 T\u27e6 T \u27e7 As t\ne\u27e6 const a \u27e7 As as = a\ne\u27e6 pair e f \u27e7 As as = (e\u27e6 e \u27e7 As as , e\u27e6 f \u27e7 As as)\ne\u27e6 fst e \u27e7 As as = proj\u2081 (e\u27e6 e \u27e7 As as)\ne\u27e6 snd e \u27e7 As as = proj\u2082 (e\u27e6 e \u27e7 As as)\ne\u27e6 abs e \u27e7 As as = \u03bb a \u2192 e\u27e6 e \u27e7 As (as , a)\ne\u27e6 app f e \u27e7 As as = e\u27e6 f \u27e7 As as (e\u27e6 e \u27e7 As as)\ne\u27e6 var v \u27e7 As as = v\u27e6 v \u27e7 As as\ne\u27e6 tabs {\u0393 = \u0393} \u03b1 e \u27e7 As as = \u03bb A \u2192 e\u27e6 e \u27e7 (As , A) (\u27e6cweaken\u27e7 \u03b1 \u0393 As A as)\ne\u27e6 tapp {t = t} T {U = U} e \u27e7 As as = \u27e6tsubst\u27e7 T \u03b5 U As tt t (e\u27e6 e \u27e7 As as (T\u27e6 T \u27e7 As))\n\ne\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} {\u0393 : Ctxt \u03b1s} {T : Typ \u03b1s} {t} (e : Exp \u0393 T t) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 \n \u2200 {as bs} \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs \u27e9\u00b2 as bs \u2192 T\u27e6 T \u27e7\u00b2 \u211cs t (e\u27e6 e \u27e7 As as) (e\u27e6 e \u27e7 Bs bs)\ne\u27e6 const a \u27e7\u00b2 \u211cs as\u211cbs = refl\ne\u27e6 pair e f \u27e7\u00b2 \u211cs as\u211cbs = (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs , e\u27e6 f \u27e7\u00b2 \u211cs as\u211cbs)\ne\u27e6 fst e \u27e7\u00b2 \u211cs as\u211cbs = proj\u2081 (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs)\ne\u27e6 snd e \u27e7\u00b2 \u211cs as\u211cbs = proj\u2082 (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs)\ne\u27e6 abs e \u27e7\u00b2 \u211cs as\u211cbs = \u03bb a\u211cb \u2192 e\u27e6 e \u27e7\u00b2 \u211cs (as\u211cbs , a\u211cb)\ne\u27e6 app f e \u27e7\u00b2 \u211cs as\u211cbs = e\u27e6 f \u27e7\u00b2 \u211cs as\u211cbs (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs)\ne\u27e6 var v \u27e7\u00b2 \u211cs as\u211cbs = v\u27e6 v \u27e7\u00b2 \u211cs as\u211cbs\ne\u27e6 tabs {\u0393 = \u0393} \u03b1 e \u27e7\u00b2 \u211cs as\u211cbs = \u03bb \u211c \u2192 e\u27e6 e \u27e7\u00b2 (\u211cs , \u211c) (\u27e6cweaken\u27e7\u00b2 \u03b1 \u0393 \u211cs \u211c as\u211cbs)\ne\u27e6 tapp {t = t} T {U = U} e \u27e7\u00b2 \u211cs as\u211cbs = \u27e6tsubst\u27e7\u00b2 T \u03b5 U \u211cs tt t (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs (T\u27e6 T \u27e7\u00b2 \u211cs))\n\n-- Surface level types\n\ndata STyp : Set\u2081 where\n \u27e8_\u27e9 : (A : Set) \u2192 STyp\n _\u2227_ _\u21d2_ : (T : STyp) \u2192 (U : STyp) \u2192 STyp\n \u25a1 : (T : STyp) \u2192 STyp\n\n-- Translation of surface level types into types\n\n\u27ea_\u27eb : STyp \u2192 Typ (\u03b5 , o)\n\u27ea \u27e8 A \u27e9 \u27eb = \u27e8 A \u27e9\n\u27ea T \u2227 U \u27eb = \u27ea T \u27eb \u2227 \u27ea U \u27eb\n\u27ea T \u21d2 U \u27eb = \u27ea T \u27eb \u21d2 \u27ea U \u27eb\n\u27ea \u25a1 T \u27eb = tvar zero \u22b5 \u27ea T \u27eb\n\nT\u27ea_\u27eb : STyp \u2192 RSet\u2080\nT\u27ea \u27e8 A \u27e9 \u27eb t = A\nT\u27ea T \u2227 U \u27eb t = T\u27ea T \u27eb t \u00d7 T\u27ea U \u27eb t\nT\u27ea T \u21d2 U \u27eb t = T\u27ea T \u27eb t \u2192 T\u27ea U \u27eb t\nT\u27ea \u25a1 T \u27eb t = \u2200 u \u2192 True (t \u2264 u) \u2192 T\u27ea T \u27eb u\n\nWorld : RSet\u2080\nWorld t = \u22a4\n\nmutual\n\n trans : \u2200 T {t} \u2192 T\u27ea T \u27eb t \u2192 T\u27e6 \u27ea T \u27eb \u27e7 (tt , World) t\n trans \u27e8 A \u27e9 a = a\n trans (T \u2227 U) (a , b) = (trans T a , trans U b)\n trans (T \u21d2 U) f = \u03bb a \u2192 trans U (f (trans\u207b\u00b9 T a))\n trans (\u25a1 T) \u03c3 = \u03bb u t\u2264u \u03c4 \u2192 trans T (\u03c3 u t\u2264u)\n\n trans\u207b\u00b9 : \u2200 T {t} \u2192 T\u27e6 \u27ea T \u27eb \u27e7 (tt , World) t \u2192 T\u27ea T \u27eb t\n trans\u207b\u00b9 \u27e8 A \u27e9 a = a\n trans\u207b\u00b9 (T \u2227 U) (a , b) = (trans\u207b\u00b9 T a , trans\u207b\u00b9 U b)\n trans\u207b\u00b9 (T \u21d2 U) f = \u03bb a \u2192 trans\u207b\u00b9 U (f (trans T a))\n trans\u207b\u00b9 (\u25a1 T) \u03c3 = \u03bb u t\u2264u \u2192 trans\u207b\u00b9 T (\u03c3 u t\u2264u _)\n\n-- Causality\n\n_at_\u220b_\u2248[_\u2235_]_ : \u2200 T t \u2192 T\u27ea T \u27eb t \u2192 \u2200 u \u2192 True (t \u2264 u) \u2192 T\u27ea T \u27eb t \u2192 Set\n\u27e8 A \u27e9 at t \u220b a \u2248[ u \u2235 t\u2264u ] b = a \u2261 b\n(T \u2227 U) at t \u220b (a , b) \u2248[ u \u2235 t\u2264u ] (c , d) = (T at t \u220b a \u2248[ u \u2235 t\u2264u ] c) \u00d7 (U at t \u220b b \u2248[ u \u2235 t\u2264u ] d)\n(T \u21d2 U) at t \u220b f \u2248[ u \u2235 t\u2264u ] g = \u2200 {a b} \u2192 (T at t \u220b a \u2248[ u \u2235 t\u2264u ] b) \u2192 (U at t \u220b f a \u2248[ u \u2235 t\u2264u ] g b)\n(\u25a1 T) at s \u220b \u03c3 \u2248[ u \u2235 s\u2264u ] \u03c4 = \u2200 t s\u2264t t\u2264u \u2192 (T at t \u220b \u03c3 t s\u2264t \u2248[ u \u2235 t\u2264u ] \u03c4 t s\u2264t)\n\nCausal : \u2200 T U t \u2192 T\u27ea T \u21d2 U \u27eb t \u2192 Set\nCausal T U t f = \u2200 u t\u2264u {a b} \u2192 (T at t \u220b a \u2248[ u \u2235 t\u2264u ] b) \u2192 (U at t \u220b f a \u2248[ u \u2235 t\u2264u ] f b)\n\n-- Parametricity implies causality\n\n\u211c[_] : Time \u2192 (o \u220b World \u21d4 World)\n\u211c[ u ] t tt tt = True (t \u2264 u)\n\nmutual\n\n \u211c-impl-\u2248 : \u2200 T t u t\u2264u {a b} \u2192\n T\u27e6 \u27ea T \u27eb \u27e7\u00b2 (tt , \u211c[ u ]) t a b \u2192\n (T at t \u220b trans\u207b\u00b9 T a \u2248[ u \u2235 t\u2264u ] trans\u207b\u00b9 T b)\n \u211c-impl-\u2248 \u27e8 A \u27e9 t u t\u2264u a\u211cb = a\u211cb\n \u211c-impl-\u2248 (T \u2227 U) t u t\u2264u (a\u211cc , b\u211cd) = (\u211c-impl-\u2248 T t u t\u2264u a\u211cc , \u211c-impl-\u2248 U t u t\u2264u b\u211cd)\n \u211c-impl-\u2248 (T \u21d2 U) t u t\u2264u f\u211cg = \u03bb a\u2248b \u2192 \u211c-impl-\u2248 U t u t\u2264u (f\u211cg (\u2248-impl-\u211c T t u t\u2264u a\u2248b))\n \u211c-impl-\u2248 (\u25a1 T) s v s\u2264v \u03c3\u211c\u03c4 = \u03bb u s\u2264u u\u2264v \u2192 \u211c-impl-\u2248 T u v u\u2264v (\u03c3\u211c\u03c4 u s\u2264u (\u03bb t s\u2264t t\u2264u \u2192 \u2264-trans t u v t\u2264u u\u2264v))\n\n \u2248-impl-\u211c : \u2200 T t u t\u2264u {a b} \u2192\n (T at t \u220b a \u2248[ u \u2235 t\u2264u ] b) \u2192\n T\u27e6 \u27ea T \u27eb \u27e7\u00b2 (tt , \u211c[ u ]) t (trans T a) (trans T b)\n \u2248-impl-\u211c \u27e8 A \u27e9 t u t\u2264u a\u2248b = a\u2248b\n \u2248-impl-\u211c (T \u2227 U) t u t\u2264u (a\u2248c , b\u2248d) = (\u2248-impl-\u211c T t u t\u2264u a\u2248c , \u2248-impl-\u211c U t u t\u2264u b\u2248d)\n \u2248-impl-\u211c (T \u21d2 U) t u t\u2264u f\u2248g = \u03bb a\u211cb \u2192 \u2248-impl-\u211c U t u t\u2264u (f\u2248g (\u211c-impl-\u2248 T t u t\u2264u a\u211cb))\n \u2248-impl-\u211c (\u25a1 T) s v s\u2264v \u03c3\u2248\u03c4 = \u03bb u s\u2264u \u03c1 \u2192 \u2248-impl-\u211c T u v (\u03c1 u s\u2264u (\u2264-refl u)) (\u03c3\u2248\u03c4 u s\u2264u (\u03c1 u s\u2264u (\u2264-refl u)))\n\n-- Every expression is causal\n\ne\u27ea_at_\u220b_\u27eb : \u2200 T t \u2192 Exp \u03b5 \u27ea T \u27eb t \u2192 T\u27ea T \u27eb t\ne\u27ea T at t \u220b e \u27eb = trans\u207b\u00b9 T (e\u27e6 e \u27e7 (tt , World) tt)\n\ncausality : \u2200 T U t f \u2192 Causal T U t e\u27ea (T \u21d2 U) at t \u220b f \u27eb \ncausality T U t f u t\u2264u = \u211c-impl-\u2248 (T \u21d2 U) t u t\u2264u (e\u27e6 f \u27e7\u00b2 (tt , \u211c[ u ]) tt)\n","old_contents":"open import FRP.JS.Level using ( Level ; _\u2294_ ) renaming ( zero to o ; suc to \u2191 )\nopen import FRP.JS.Time using ( Time ; _\u225f_ ; _\u2264_ ; _<_ )\nopen import FRP.JS.True using ( True )\nopen import FRP.JS.Nat using ( \u2115 ; zero ; suc )\n\nmodule FRP.JS.Model where\n\n-- Preliminaries\n\ninfixr 4 _+_\n\nrecord \u22a4 {\u03b1} : Set \u03b1 where\n constructor tt\n\nopen \u22a4 public\n\nrecord \u03a3 {\u03b1 \u03b2} (A : Set \u03b1) (B : A \u2192 Set \u03b2) : Set (\u03b1 \u2294 \u03b2) where\n constructor _,_\n field\n proj\u2081 : A\n proj\u2082 : B proj\u2081\n\nopen \u03a3 public\n\n_\u00d7_ : \u2200 {\u03b1 \u03b2} \u2192 Set \u03b1 \u2192 Set \u03b2 \u2192 Set (\u03b1 \u2294 \u03b2)\nA \u00d7 B = \u03a3 A (\u03bb a \u2192 B)\n\ndata _\u2261_ {\u03b1} {A : Set \u03b1} (a : A) : A \u2192 Set \u03b1 where\n refl : a \u2261 a\n\n-- Relations on Set\n\n_\u220b_\u2194_ : \u2200 \u03b1 \u2192 Set \u03b1 \u2192 Set \u03b1 \u2192 Set (\u2191 \u03b1)\n\u03b1 \u220b A \u2194 B = A \u2192 B \u2192 Set \u03b1\n\n-- RSets and relations on RSet\n\nRSet : \u2200 \u03b1 \u2192 Set (\u2191 \u03b1)\nRSet \u03b1 = Time \u2192 Set \u03b1\n\nRSet\u2080 = RSet o\nRSet\u2081 = RSet (\u2191 o)\n\n_\u220b_\u21d4_ : \u2200 \u03b1 \u2192 RSet \u03b1 \u2192 RSet \u03b1 \u2192 Set (\u2191 \u03b1)\n\u03b1 \u220b A \u21d4 B = \u2200 t \u2192 (\u03b1 \u220b A t \u2194 B t)\n\n--- Level sequences\n\ndata Levels : Set where\n \u03b5 : Levels\n _,_ : \u2200 (\u03b1s : Levels) (\u03b1 : Level) \u2192 Levels\n\nmax : Levels \u2192 Level\nmax \u03b5 = o\nmax (\u03b1s , \u03b1) = max \u03b1s \u2294 \u03b1\n\n-- Sequences of Sets\n\nSets : \u2200 \u03b1s \u2192 Set (\u2191 (max \u03b1s))\nSets \u03b5 = \u22a4\nSets (\u03b1s , \u03b1) = Sets \u03b1s \u00d7 Set \u03b1\n\n\u27e8_\u220b_\u27e9 : \u2200 \u03b1s \u2192 Sets \u03b1s \u2192 Set (max \u03b1s)\n\u27e8 \u03b5 \u220b tt \u27e9 = \u22a4\n\u27e8 (\u03b1s , \u03b1) \u220b (As , A) \u27e9 = \u27e8 \u03b1s \u220b As \u27e9 \u00d7 A\n\n_\u220b_\u2194*_ : \u2200 \u03b1s \u2192 Sets \u03b1s \u2192 Sets \u03b1s \u2192 Set (\u2191 (max \u03b1s))\n\u03b5 \u220b tt \u2194* tt = \u22a4\n(\u03b1s , \u03b1) \u220b (As , A) \u2194* (Bs , B) = (\u03b1s \u220b As \u2194* Bs) \u00d7 (\u03b1 \u220b A \u2194 B)\n\n\u27e8_\u220b_\u27e9\u00b2 : \u2200 \u03b1s {As Bs} \u2192 (\u03b1s \u220b As \u2194* Bs) \u2192 (max \u03b1s \u220b \u27e8 \u03b1s \u220b As \u27e9 \u2194 \u27e8 \u03b1s \u220b Bs \u27e9)\n\u27e8 \u03b5 \u220b tt \u27e9\u00b2 tt tt = \u22a4\n\u27e8 (\u03b1s , \u03b1) \u220b (\u211cs , \u211c) \u27e9\u00b2 (as , a) (bs , b) = (\u27e8 \u03b1s \u220b \u211cs \u27e9\u00b2 as bs) \u00d7 (\u211c a b)\n\n-- Sequences of RSets\n\nRSets : \u2200 \u03b1s \u2192 Set (\u2191 (max \u03b1s))\nRSets \u03b5 = \u22a4\nRSets (\u03b1s , \u03b1) = RSets \u03b1s \u00d7 RSet \u03b1\n\n_\u220b_\u21d4*_ : \u2200 \u03b1s \u2192 RSets \u03b1s \u2192 RSets \u03b1s \u2192 Set (\u2191 (max \u03b1s))\n\u03b5 \u220b tt \u21d4* tt = \u22a4\n(\u03b1s , \u03b1) \u220b (As , A) \u21d4* (Bs , B) = (\u03b1s \u220b As \u21d4* Bs) \u00d7 (\u03b1 \u220b A \u21d4 B)\n\n-- Concatenation of sequences\n\n_+_ : Levels \u2192 Levels \u2192 Levels\n\u03b1s + \u03b5 = \u03b1s\n\u03b1s + (\u03b2s , \u03b2) = (\u03b1s + \u03b2s) , \u03b2\n\n_\u220b_++_\u220b_ : \u2200 \u03b1s \u2192 RSets \u03b1s \u2192 \u2200 \u03b2s \u2192 RSets \u03b2s \u2192 RSets (\u03b1s + \u03b2s)\n\u03b1s \u220b As ++ \u03b5 \u220b tt = As\n\u03b1s \u220b As ++ (\u03b2s , \u03b2) \u220b (Bs , B) = ((\u03b1s \u220b As ++ \u03b2s \u220b Bs) , B)\n\n_\u220b_++\u00b2_\u220b_ : \u2200 \u03b1s {As Bs} \u2192 (\u03b1s \u220b As \u21d4* Bs) \u2192 \u2200 \u03b2s {Cs Ds} \u2192 (\u03b2s \u220b Cs \u21d4* Ds) \u2192 \n ((\u03b1s + \u03b2s) \u220b (\u03b1s \u220b As ++ \u03b2s \u220b Cs) \u21d4* (\u03b1s \u220b Bs ++ \u03b2s \u220b Ds))\n\u03b1s \u220b \u211cs ++\u00b2 \u03b5 \u220b tt = \u211cs\n\u03b1s \u220b \u211cs ++\u00b2 (\u03b2s , \u03b2) \u220b (\u2111s , \u2111) = ((\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) , \u2111)\n\n-- Type variables\n\ndata TVar : Levels \u2192 Set\u2081 where\n zero : \u2200 {\u03b1s \u03b1} \u2192 TVar (\u03b1s , \u03b1)\n suc : \u2200 {\u03b1s \u03b1} \u2192 (\u03c4 : TVar \u03b1s) \u2192 TVar (\u03b1s , \u03b1)\n\n\u03c4level : \u2200 {\u03b1s} \u2192 TVar \u03b1s \u2192 Level\n\u03c4level (zero {\u03b1 = \u03b1}) = \u03b1\n\u03c4level (suc \u03c4) = \u03c4level \u03c4\n\n\u03c4\u27e6_\u27e7 : \u2200 {\u03b1s} (\u03c4 : TVar \u03b1s) \u2192 RSets \u03b1s \u2192 RSet (\u03c4level \u03c4)\n\u03c4\u27e6 zero \u27e7 (As , A) = A\n\u03c4\u27e6 suc \u03c4 \u27e7 (As , A) = \u03c4\u27e6 \u03c4 \u27e7 As\n\n\u03c4\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} (\u03c4 : TVar \u03b1s) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 (\u03c4level \u03c4 \u220b \u03c4\u27e6 \u03c4 \u27e7 As \u21d4 \u03c4\u27e6 \u03c4 \u27e7 Bs)\n\u03c4\u27e6 zero \u27e7\u00b2 (\u211cs , \u211c) t a b = \u211c t a b\n\u03c4\u27e6 suc \u03c4 \u27e7\u00b2 (\u211cs , \u211c) t a b = \u03c4\u27e6 \u03c4 \u27e7\u00b2 \u211cs t a b\n\n-- Types\n\ndata Typ (\u03b1s : Levels) : Set\u2081 where\n \u27e8_\u27e9 : (A : Set) \u2192 Typ \u03b1s\n _\u2227_ _\u21d2_ : (T : Typ \u03b1s) \u2192 (U : Typ \u03b1s) \u2192 Typ \u03b1s\n var : (\u03c4 : TVar \u03b1s) \u2192 Typ \u03b1s\n univ : \u2200 \u03b1 \u2192 (T : Typ (\u03b1s , \u03b1)) \u2192 Typ \u03b1s\n\ntlevel : \u2200 {\u03b1s} \u2192 Typ \u03b1s \u2192 Level\ntlevel \u27e8 A \u27e9 = o\ntlevel (T \u2227 U) = tlevel T \u2294 tlevel U\ntlevel (T \u21d2 U) = tlevel T \u2294 tlevel U\ntlevel (var \u03c4) = \u03c4level \u03c4\ntlevel (univ \u03b1 T) = \u2191 \u03b1 \u2294 tlevel T\n\nT\u27e6_\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u2192 RSets \u03b1s \u2192 RSet (tlevel T)\nT\u27e6 \u27e8 A \u27e9 \u27e7 As t = A\nT\u27e6 T \u2227 U \u27e7 As t = T\u27e6 T \u27e7 As t \u00d7 T\u27e6 U \u27e7 As t\nT\u27e6 T \u21d2 U \u27e7 As t = T\u27e6 T \u27e7 As t \u2192 T\u27e6 U \u27e7 As t\nT\u27e6 var \u03c4 \u27e7 As t = \u03c4\u27e6 \u03c4 \u27e7 As t\nT\u27e6 univ \u03b1 T \u27e7 As t = \u2200 (A : RSet \u03b1) \u2192 T\u27e6 T \u27e7 (As , A) t\n\nT\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} (T : Typ \u03b1s) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 (tlevel T \u220b T\u27e6 T \u27e7 As \u21d4 T\u27e6 T \u27e7 Bs)\nT\u27e6 \u27e8 A \u27e9 \u27e7\u00b2 \u211cs t a b = a \u2261 b\nT\u27e6 T \u2227 U \u27e7\u00b2 \u211cs t (a , b) (c , d) = T\u27e6 T \u27e7\u00b2 \u211cs t a c \u00d7 T\u27e6 U \u27e7\u00b2 \u211cs t b d\nT\u27e6 T \u21d2 U \u27e7\u00b2 \u211cs t f g = \u2200 {a b} \u2192 T\u27e6 T \u27e7\u00b2 \u211cs t a b \u2192 T\u27e6 U \u27e7\u00b2 \u211cs t (f a) (g b)\nT\u27e6 var \u03c4 \u27e7\u00b2 \u211cs t v w = \u03c4\u27e6 \u03c4 \u27e7\u00b2 \u211cs t v w\nT\u27e6 univ \u03b1 T \u27e7\u00b2 \u211cs t f g = \u2200 {A B} (\u211c : \u03b1 \u220b A \u21d4 B) \u2192 T\u27e6 T \u27e7\u00b2 (\u211cs , \u211c) t (f A) (g B)\n\n-- Contexts\n\ndata Ctxt (\u03b1s : Levels) : Set\u2081 where\n \u03b5 : Ctxt \u03b1s\n _,_at_ : (\u0393 : Ctxt \u03b1s) (T : Typ \u03b1s) (t : Time) \u2192 Ctxt \u03b1s\n\nclevels : \u2200 {\u03b1s} \u2192 Ctxt \u03b1s \u2192 Levels\nclevels \u03b5 = \u03b5\nclevels (\u0393 , T at t) = (clevels \u0393 , tlevel T)\n\n\u0393\u27e6_\u27e7 : \u2200 {\u03b1s} (\u0393 : Ctxt \u03b1s) \u2192 RSets \u03b1s \u2192 Sets (clevels \u0393)\n\u0393\u27e6 \u03b5 \u27e7 As = tt\n\u0393\u27e6 \u0393 , T at t \u27e7 As = (\u0393\u27e6 \u0393 \u27e7 As , T\u27e6 T \u27e7 As t)\n\n\u0393\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} (\u0393 : Ctxt \u03b1s) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 (clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7 As \u2194* \u0393\u27e6 \u0393 \u27e7 Bs)\n\u0393\u27e6 \u03b5 \u27e7\u00b2 \u211cs = tt\n\u0393\u27e6 \u0393 , T at t \u27e7\u00b2 \u211cs = (\u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs , T\u27e6 T \u27e7\u00b2 \u211cs t)\n\n-- Weakening of type variables\n\n\u03c4weaken : \u2200 {\u03b1s} \u03b1 \u03b2s \u2192 TVar (\u03b1s + \u03b2s) \u2192 TVar ((\u03b1s , \u03b1) + \u03b2s)\n\u03c4weaken \u03b1 \u03b5 \u03c4 = suc \u03c4\n\u03c4weaken \u03b1 (\u03b2s , \u03b2) zero = zero\n\u03c4weaken \u03b1 (\u03b2s , \u03b2) (suc \u03c4) = suc (\u03c4weaken \u03b1 \u03b2s \u03c4)\n\n\u27e6\u03c4weaken\u27e7 : \u2200 {\u03b1s} \u03b1 \u03b2s (\u03c4 : TVar (\u03b1s + \u03b2s)) As A Bs t \u2192 \n \u03c4\u27e6 \u03c4 \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t \u2192 \u03c4\u27e6 \u03c4weaken \u03b1 \u03b2s \u03c4 \u27e7 ((\u03b1s , \u03b1) \u220b (As , A) ++ \u03b2s \u220b Bs) t\n\u27e6\u03c4weaken\u27e7 \u03b1 \u03b5 \u03c4 As A Bs t a = a\n\u27e6\u03c4weaken\u27e7 \u03b1 (\u03b2s , \u03b2) zero As A Bs t a = a\n\u27e6\u03c4weaken\u27e7 \u03b1 (\u03b2s , \u03b2) (suc \u03c4) As A (Bs , B) t a = \u27e6\u03c4weaken\u27e7 \u03b1 \u03b2s \u03c4 As A Bs t a\n\n\u27e6\u03c4weaken\u207b\u00b9\u27e7 : \u2200 {\u03b1s} \u03b1 \u03b2s (\u03c4 : TVar (\u03b1s + \u03b2s)) As A Bs t \u2192 \n \u03c4\u27e6 \u03c4weaken \u03b1 \u03b2s \u03c4 \u27e7 ((\u03b1s , \u03b1) \u220b (As , A) ++ \u03b2s \u220b Bs) t \u2192 \u03c4\u27e6 \u03c4 \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t\n\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b5 \u03c4 As A Bs t a = a\n\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 (\u03b2s , \u03b2) zero As A Bs t a = a\n\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 (\u03b2s , \u03b2) (suc \u03c4) As A (Bs , B) t a = \u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u03c4 As A Bs t a\n\n\u27e6\u03c4weaken\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 \u03b2s (\u03c4 : TVar (\u03b1s + \u03b2s)) {As Bs A B Cs Ds} \u211cs \u211c \u2111s t {a b} \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192 \n \u03c4\u27e6 \u03c4weaken \u03b1 \u03b2s \u03c4 \u27e7\u00b2 ((\u03b1s , \u03b1) \u220b (\u211cs , \u211c) ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6\u03c4weaken\u27e7 \u03b1 \u03b2s \u03c4 As A Cs t a) (\u27e6\u03c4weaken\u27e7 \u03b1 \u03b2s \u03c4 Bs B Ds t b)\n\u27e6\u03c4weaken\u27e7\u00b2 \u03b1 \u03b5 \u03c4 \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4weaken\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) zero \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4weaken\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) (suc \u03c4) \u211cs \u211c (\u2111s , \u2111) t a\u211cb = \u27e6\u03c4weaken\u27e7\u00b2 \u03b1 \u03b2s \u03c4 \u211cs \u211c \u2111s t a\u211cb\n\n\u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 \u03b2s (\u03c4 : TVar (\u03b1s + \u03b2s)) {As Bs A B Cs Ds} \u211cs \u211c \u2111s t {a b} \u2192\n \u03c4\u27e6 \u03c4weaken \u03b1 \u03b2s \u03c4 \u27e7\u00b2 ((\u03b1s , \u03b1) \u220b (\u211cs , \u211c) ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u03c4 As A Cs t a) (\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u03c4 Bs B Ds t b)\n\u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b5 \u03c4 \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) zero \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) (suc \u03c4) \u211cs \u211c (\u2111s , \u2111) t a\u211cb = \u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s \u03c4 \u211cs \u211c \u2111s t a\u211cb\n\n-- Weakening of types\n\ntweaken : \u2200 {\u03b1s} \u03b1 \u03b2s \u2192 Typ (\u03b1s + \u03b2s) \u2192 Typ ((\u03b1s , \u03b1) + \u03b2s)\ntweaken \u03b1 \u03b2s \u27e8 A \u27e9 = \u27e8 A \u27e9\ntweaken \u03b1 \u03b2s (T \u2227 U) = tweaken \u03b1 \u03b2s T \u2227 tweaken \u03b1 \u03b2s U\ntweaken \u03b1 \u03b2s (T \u21d2 U) = tweaken \u03b1 \u03b2s T \u21d2 tweaken \u03b1 \u03b2s U\ntweaken \u03b1 \u03b2s (var \u03c4) = var (\u03c4weaken \u03b1 \u03b2s \u03c4)\ntweaken \u03b1 \u03b2s (univ \u03b2 T) = univ \u03b2 (tweaken \u03b1 (\u03b2s , \u03b2) T)\n\nmutual\n\n \u27e6tweaken\u27e7 : \u2200 {\u03b1s} \u03b1 \u03b2s (T : Typ (\u03b1s + \u03b2s)) As A Bs t \u2192 \n T\u27e6 T \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t \u2192 T\u27e6 tweaken \u03b1 \u03b2s T \u27e7 ((\u03b1s , \u03b1) \u220b (As , A) ++ \u03b2s \u220b Bs) t\n \u27e6tweaken\u27e7 \u03b1 \u03b2s \u27e8 B \u27e9 As A Bs t a = a\n \u27e6tweaken\u27e7 \u03b1 \u03b2s (T \u2227 U) As A Bs t (a , b) = (\u27e6tweaken\u27e7 \u03b1 \u03b2s T As A Bs t a , \u27e6tweaken\u27e7 \u03b1 \u03b2s U As A Bs t b)\n \u27e6tweaken\u27e7 \u03b1 \u03b2s (T \u21d2 U) As A Bs t f = \u03bb a \u2192 \u27e6tweaken\u27e7 \u03b1 \u03b2s U As A Bs t (f (\u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s T As A Bs t a))\n \u27e6tweaken\u27e7 \u03b1 \u03b2s (var \u03c4) As A Bs t a = \u27e6\u03c4weaken\u27e7 \u03b1 \u03b2s \u03c4 As A Bs t a\n \u27e6tweaken\u27e7 \u03b1 \u03b2s (univ \u03b2 T) As A Bs t f = \u03bb B \u2192 \u27e6tweaken\u27e7 \u03b1 (\u03b2s , \u03b2) T As A (Bs , B) t (f B)\n\n \u27e6tweaken\u207b\u00b9\u27e7 : \u2200 {\u03b1s} \u03b1 \u03b2s (T : Typ (\u03b1s + \u03b2s)) As A Bs t \u2192 \n T\u27e6 tweaken \u03b1 \u03b2s T \u27e7 ((\u03b1s , \u03b1) \u220b (As , A) ++ \u03b2s \u220b Bs) t \u2192 T\u27e6 T \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u27e8 B \u27e9 As A Bs t a = a\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s (T \u2227 U) As A Bs t (a , b) = (\u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s T As A Bs t a , \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s U As A Bs t b)\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s (T \u21d2 U) As A Bs t f = \u03bb a \u2192 \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s U As A Bs t (f (\u27e6tweaken\u27e7 \u03b1 \u03b2s T As A Bs t a))\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s (var \u03c4) As A Bs t a = \u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u03c4 As A Bs t a\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s (univ \u03b2 T) As A Bs t f = \u03bb B \u2192 \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 (\u03b2s , \u03b2) T As A (Bs , B) t (f B)\n\nmutual\n\n \u27e6tweaken\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 \u03b2s (T : Typ (\u03b1s + \u03b2s)) {As Bs A B Cs Ds} \u211cs \u211c \u2111s t {a b} \u2192\n T\u27e6 T \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192 \n T\u27e6 tweaken \u03b1 \u03b2s T \u27e7\u00b2 ((\u03b1s , \u03b1) \u220b (\u211cs , \u211c) ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6tweaken\u27e7 \u03b1 \u03b2s T As A Cs t a) (\u27e6tweaken\u27e7 \u03b1 \u03b2s T Bs B Ds t b)\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s \u27e8 B \u27e9 \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s (T \u2227 U) \u211cs \u211c \u2111s t (a\u211cb , c\u211cd) = (\u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s t a\u211cb , \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s t c\u211cd)\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s (T \u21d2 U) \u211cs \u211c \u2111s t f\u211cg = \u03bb a\u211cb \u2192 \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s t (f\u211cg (\u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s t a\u211cb))\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s (var \u03c4) \u211cs \u211c \u2111s t a\u211cb = \u27e6\u03c4weaken\u27e7\u00b2 \u03b1 \u03b2s \u03c4 \u211cs \u211c \u2111s t a\u211cb\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s (univ \u03b2 T) \u211cs \u211c \u2111s t f\u211cg = \u03bb \u2111 \u2192 \u27e6tweaken\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) T \u211cs \u211c (\u2111s , \u2111) t (f\u211cg \u2111)\n\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 \u03b2s (T : Typ (\u03b1s + \u03b2s)) {As Bs A B Cs Ds} \u211cs \u211c \u2111s t {a b} \u2192\n T\u27e6 tweaken \u03b1 \u03b2s T \u27e7\u00b2 ((\u03b1s , \u03b1) \u220b (\u211cs , \u211c) ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n T\u27e6 T \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s T As A Cs t a) (\u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s T Bs B Ds t b)\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s \u27e8 B \u27e9 \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s (T \u2227 U) \u211cs \u211c \u2111s t (a\u211cb , c\u211cd) = (\u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s t a\u211cb , \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s t c\u211cd)\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s (T \u21d2 U) \u211cs \u211c \u2111s t f\u211cg = \u03bb a\u211cb \u2192 \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s t (f\u211cg (\u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s t a\u211cb))\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s (var \u03c4) \u211cs \u211c \u2111s t a\u211cb = \u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s \u03c4 \u211cs \u211c \u2111s t a\u211cb\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s (univ \u03b2 T) \u211cs \u211c \u2111s t f\u211cg = \u03bb \u2111 \u2192 \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) T \u211cs \u211c (\u2111s , \u2111) t (f\u211cg \u2111)\n\n-- Weakening of contexts\n\ncweaken : \u2200 {\u03b1s} \u03b1 \u2192 Ctxt \u03b1s \u2192 Ctxt (\u03b1s , \u03b1)\ncweaken \u03b1 \u03b5 = \u03b5\ncweaken \u03b1 (\u0393 , T at t) = (cweaken \u03b1 \u0393 , tweaken \u03b1 \u03b5 T at t)\n \n\u27e6cweaken\u27e7 : \u2200 {\u03b1s} \u03b1 (\u0393 : Ctxt \u03b1s) As A \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7 As \u27e9 \u2192 \u27e8 clevels (cweaken \u03b1 \u0393) \u220b \u0393\u27e6 cweaken \u03b1 \u0393 \u27e7 (As , A) \u27e9\n\u27e6cweaken\u27e7 \u03b1 \u03b5 As A tt = tt\n\u27e6cweaken\u27e7 \u03b1 (\u0393 , T at t) As A (as , a) = (\u27e6cweaken\u27e7 \u03b1 \u0393 As A as , \u27e6tweaken\u27e7 \u03b1 \u03b5 T As A tt t a)\n\n\u27e6cweaken\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 (\u0393 : Ctxt \u03b1s) {As Bs A B} \u211cs \u211c {as bs} \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs \u27e9\u00b2 as bs \u2192\n \u27e8 clevels (cweaken \u03b1 \u0393) \u220b \u0393\u27e6 cweaken \u03b1 \u0393 \u27e7\u00b2 (\u211cs , \u211c) \u27e9\u00b2 (\u27e6cweaken\u27e7 \u03b1 \u0393 As A as) (\u27e6cweaken\u27e7 \u03b1 \u0393 Bs B bs)\n\u27e6cweaken\u27e7\u00b2 \u03b1 \u03b5 \u211cs \u211c tt = tt\n\u27e6cweaken\u27e7\u00b2 \u03b1 (\u0393 , T at t) \u211cs \u211c (as\u211cbs , a\u211cb) = (\u27e6cweaken\u27e7\u00b2 \u03b1 \u0393 \u211cs \u211c as\u211cbs , \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b5 T \u211cs \u211c tt t a\u211cb)\n\n-- Substitution into type variables\n\n\u03c4subst : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s \u2192 TVar ((\u03b1s , tlevel T) + \u03b2s) \u2192 Typ (\u03b1s + \u03b2s)\n\u03c4subst T \u03b5 zero = T\n\u03c4subst T \u03b5 (suc \u03c4) = var \u03c4\n\u03c4subst T (\u03b2s , \u03b2) zero = var zero\n\u03c4subst T (\u03b2s , \u03b2) (suc \u03c4) = tweaken \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4)\n\n\u27e6\u03c4subst\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (\u03c4 : TVar ((\u03b1s , tlevel T) + \u03b2s)) As Bs t \u2192\n \u03c4\u27e6 \u03c4 \u27e7 ((\u03b1s , tlevel T) \u220b (As , T\u27e6 T \u27e7 As) ++ \u03b2s \u220b Bs) t \u2192 \n T\u27e6 \u03c4subst T \u03b2s \u03c4 \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t\n\u27e6\u03c4subst\u27e7 T \u03b5 zero As Bs t a = a\n\u27e6\u03c4subst\u27e7 T \u03b5 (suc \u03c4) As Bs t a = a\n\u27e6\u03c4subst\u27e7 T (\u03b2s , \u03b2) zero As Bs t a = a \n\u27e6\u03c4subst\u27e7 {\u03b1s} T (\u03b2s , \u03b2) (suc \u03c4) As (Bs , B) t a = \n \u27e6tweaken\u27e7 \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4) (\u03b1s \u220b As ++ \u03b2s \u220b Bs) B tt t \n (\u27e6\u03c4subst\u27e7 T \u03b2s \u03c4 As Bs t a)\n\n\u27e6\u03c4subst\u207b\u00b9\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (\u03c4 : TVar ((\u03b1s , tlevel T) + \u03b2s)) As Bs t \u2192\n T\u27e6 \u03c4subst T \u03b2s \u03c4 \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t \u2192\n \u03c4\u27e6 \u03c4 \u27e7 ((\u03b1s , tlevel T) \u220b (As , T\u27e6 T \u27e7 As) ++ \u03b2s \u220b Bs) t\n\u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b5 zero As Bs t a = a\n\u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b5 (suc \u03c4) As Bs t a = a\n\u27e6\u03c4subst\u207b\u00b9\u27e7 T (\u03b2s , \u03b2) zero As Bs t a = a \n\u27e6\u03c4subst\u207b\u00b9\u27e7 {\u03b1s} T (\u03b2s , \u03b2) (suc \u03c4) As (Bs , B) t a = \n \u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b2s \u03c4 As Bs t \n (\u27e6tweaken\u207b\u00b9\u27e7 \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4) (\u03b1s \u220b As ++ \u03b2s \u220b Bs) B tt t a)\n\n\u27e6\u03c4subst\u27e7\u00b2 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (\u03c4 : TVar ((\u03b1s , tlevel T) + \u03b2s)) {As Bs Cs Ds} \u211cs \u2111s t {a b} \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 ((\u03b1s , tlevel T) \u220b (\u211cs , T\u27e6 T \u27e7\u00b2 \u211cs) ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n T\u27e6 \u03c4subst T \u03b2s \u03c4 \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6\u03c4subst\u27e7 T \u03b2s \u03c4 As Bs t a) (\u27e6\u03c4subst\u27e7 T \u03b2s \u03c4 Cs Ds t b)\n\u27e6\u03c4subst\u27e7\u00b2 T \u03b5 zero \u211cs \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4subst\u27e7\u00b2 T \u03b5 (suc \u03c4) \u211cs \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4subst\u27e7\u00b2 T (\u03b2s , \u03b2) zero \u211cs \u2111s t a\u211cb = a\u211cb \n\u27e6\u03c4subst\u27e7\u00b2 {\u03b1s} T (\u03b2s , \u03b2) (suc \u03c4) \u211cs (\u2111s , \u2111) t a\u211cb = \n \u27e6tweaken\u27e7\u00b2 \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4) (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) \u2111 tt t \n (\u27e6\u03c4subst\u27e7\u00b2 T \u03b2s \u03c4 \u211cs \u2111s t a\u211cb)\n\n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (\u03c4 : TVar ((\u03b1s , tlevel T) + \u03b2s)) {As Bs Cs Ds} \u211cs \u2111s t {a b} \u2192\n T\u27e6 \u03c4subst T \u03b2s \u03c4 \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 ((\u03b1s , tlevel T) \u220b (\u211cs , T\u27e6 T \u27e7\u00b2 \u211cs) ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b2s \u03c4 As Bs t a) (\u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b2s \u03c4 Cs Ds t b)\n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T \u03b5 zero \u211cs \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T \u03b5 (suc \u03c4) \u211cs \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T (\u03b2s , \u03b2) zero \u211cs \u2111s t a\u211cb = a\u211cb \n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 {\u03b1s} T (\u03b2s , \u03b2) (suc \u03c4) \u211cs (\u2111s , \u2111) t a\u211cb = \n \u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T \u03b2s \u03c4 \u211cs \u2111s t \n (\u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4) (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) \u2111 tt t a\u211cb)\n\n-- Substitution into types\n\ntsubst : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s \u2192 Typ ((\u03b1s , tlevel T) + \u03b2s) \u2192 Typ (\u03b1s + \u03b2s)\ntsubst T \u03b2s \u27e8 A \u27e9 = \u27e8 A \u27e9\ntsubst T \u03b2s (U \u2227 V) = tsubst T \u03b2s U \u2227 tsubst T \u03b2s V\ntsubst T \u03b2s (U \u21d2 V) = tsubst T \u03b2s U \u21d2 tsubst T \u03b2s V\ntsubst T \u03b2s (var \u03c4) = \u03c4subst T \u03b2s \u03c4\ntsubst T \u03b2s (univ \u03b2 U) = univ \u03b2 (tsubst T (\u03b2s , \u03b2) U)\n\nmutual\n\n \u27e6tsubst\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (U : Typ ((\u03b1s , tlevel T) + \u03b2s)) As Bs t \u2192\n T\u27e6 U \u27e7 ((\u03b1s , tlevel T) \u220b (As , T\u27e6 T \u27e7 As) ++ \u03b2s \u220b Bs) t \u2192 \n T\u27e6 tsubst T \u03b2s U \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t\n \u27e6tsubst\u27e7 T \u03b2s \u27e8 A \u27e9 As Bs t a = a\n \u27e6tsubst\u27e7 T \u03b2s (U \u2227 V) As Bs t (a , b) = (\u27e6tsubst\u27e7 T \u03b2s U As Bs t a , \u27e6tsubst\u27e7 T \u03b2s V As Bs t b)\n \u27e6tsubst\u27e7 T \u03b2s (U \u21d2 V) As Bs t f = \u03bb a \u2192 \u27e6tsubst\u27e7 T \u03b2s V As Bs t (f (\u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s U As Bs t a))\n \u27e6tsubst\u27e7 T \u03b2s (var \u03c4) As Bs t a = \u27e6\u03c4subst\u27e7 T \u03b2s \u03c4 As Bs t a\n \u27e6tsubst\u27e7 T \u03b2s (univ \u03b2 U) As Bs t f = \u03bb B \u2192 \u27e6tsubst\u27e7 T (\u03b2s , \u03b2) U As (Bs , B) t (f B)\n\n \u27e6tsubst\u207b\u00b9\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (U : Typ ((\u03b1s , tlevel T) + \u03b2s)) As Bs t \u2192\n T\u27e6 tsubst T \u03b2s U \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t \u2192\n T\u27e6 U \u27e7 ((\u03b1s , tlevel T) \u220b (As , T\u27e6 T \u27e7 As) ++ \u03b2s \u220b Bs) t\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s \u27e8 A \u27e9 As Bs t a = a\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s (U \u2227 V) As Bs t (a , b) = (\u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s U As Bs t a , \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s V As Bs t b)\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s (U \u21d2 V) As Bs t f = \u03bb a \u2192 \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s V As Bs t (f (\u27e6tsubst\u27e7 T \u03b2s U As Bs t a))\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s (var \u03c4) As Bs t a = \u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b2s \u03c4 As Bs t a\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s (univ \u03b2 U) As Bs t f = \u03bb B \u2192 \u27e6tsubst\u207b\u00b9\u27e7 T (\u03b2s , \u03b2) U As (Bs , B) t (f B)\n\nmutual\n\n \u27e6tsubst\u27e7\u00b2 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (U : Typ ((\u03b1s , tlevel T) + \u03b2s)) {As Bs Cs Ds} \u211cs \u2111s t {a b} \u2192\n T\u27e6 U \u27e7\u00b2 ((\u03b1s , tlevel T) \u220b (\u211cs , T\u27e6 T \u27e7\u00b2 \u211cs) ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n T\u27e6 tsubst T \u03b2s U \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6tsubst\u27e7 T \u03b2s U As Bs t a) (\u27e6tsubst\u27e7 T \u03b2s U Cs Ds t b)\n \u27e6tsubst\u27e7\u00b2 T \u03b2s \u27e8 A \u27e9 \u211cs \u2111s t a\u211cb = a\u211cb\n \u27e6tsubst\u27e7\u00b2 T \u03b2s (U \u2227 V) \u211cs \u2111s t (a\u211cb , c\u211cd) = (\u27e6tsubst\u27e7\u00b2 T \u03b2s U \u211cs \u2111s t a\u211cb , \u27e6tsubst\u27e7\u00b2 T \u03b2s V \u211cs \u2111s t c\u211cd)\n \u27e6tsubst\u27e7\u00b2 T \u03b2s (U \u21d2 V) \u211cs \u2111s t f\u211cg = \u03bb a\u211cb \u2192 \u27e6tsubst\u27e7\u00b2 T \u03b2s V \u211cs \u2111s t (f\u211cg (\u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s U \u211cs \u2111s t a\u211cb))\n \u27e6tsubst\u27e7\u00b2 T \u03b2s (var \u03c4) \u211cs \u2111s t a\u211cb = \u27e6\u03c4subst\u27e7\u00b2 T \u03b2s \u03c4 \u211cs \u2111s t a\u211cb\n \u27e6tsubst\u27e7\u00b2 T \u03b2s (univ \u03b2 U) \u211cs \u2111s t f\u211cg = \u03bb \u2111 \u2192 \u27e6tsubst\u27e7\u00b2 T (\u03b2s , \u03b2) U \u211cs (\u2111s , \u2111) t (f\u211cg \u2111)\n\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (U : Typ ((\u03b1s , tlevel T) + \u03b2s)) {As Bs Cs Ds} \u211cs \u2111s t {a b} \u2192\n T\u27e6 tsubst T \u03b2s U \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n T\u27e6 U \u27e7\u00b2 ((\u03b1s , tlevel T) \u220b (\u211cs , T\u27e6 T \u27e7\u00b2 \u211cs) ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s U As Bs t a) (\u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s U Cs Ds t b)\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s \u27e8 A \u27e9 \u211cs \u2111s t a\u211cb = a\u211cb\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s (U \u2227 V) \u211cs \u2111s t (a\u211cb , c\u211cd) = (\u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s U \u211cs \u2111s t a\u211cb , \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s V \u211cs \u2111s t c\u211cd)\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s (U \u21d2 V) \u211cs \u2111s t f\u211cg = \u03bb a\u211cb \u2192 \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s V \u211cs \u2111s t (f\u211cg (\u27e6tsubst\u27e7\u00b2 T \u03b2s U \u211cs \u2111s t a\u211cb))\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s (var \u03c4) \u211cs \u2111s t a\u211cb = \u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T \u03b2s \u03c4 \u211cs \u2111s t a\u211cb\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s (univ \u03b2 U) \u211cs \u2111s t f\u211cg = \u03bb \u2111 \u2192 \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T (\u03b2s , \u03b2) U \u211cs (\u2111s , \u2111) t (f\u211cg \u2111)\n\n-- Variables\n\ndata Var {\u03b1s} (T : Typ \u03b1s) (t : Time) : (\u0393 : Ctxt \u03b1s) \u2192 Set\u2081 where\n zero : \u2200 {\u0393 : Ctxt \u03b1s} \u2192 Var T t (\u0393 , T at t)\n suc : \u2200 {\u0393 : Ctxt \u03b1s} {U : Typ \u03b1s} {u} \u2192 Var T t \u0393 \u2192 Var T t (\u0393 , U at u)\n\nv\u27e6_\u27e7 : \u2200 {\u03b1s} {\u0393 : Ctxt \u03b1s} {T : Typ \u03b1s} {t} \u2192 Var T t \u0393 \u2192 \u2200 As \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7 As \u27e9 \u2192 T\u27e6 T \u27e7 As t\nv\u27e6 zero \u27e7 As (as , a) = a\nv\u27e6 suc v \u27e7 As (as , a) = v\u27e6 v \u27e7 As as\n\nv\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} {\u0393 : Ctxt \u03b1s} {T : Typ \u03b1s} {t} (\u03c4 : Var T t \u0393) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 \n \u2200 {as bs} \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs \u27e9\u00b2 as bs \u2192 T\u27e6 T \u27e7\u00b2 \u211cs t (v\u27e6 \u03c4 \u27e7 As as) (v\u27e6 \u03c4 \u27e7 Bs bs)\nv\u27e6 zero \u27e7\u00b2 \u211cs (as\u211cbs , a\u211cb) = a\u211cb\nv\u27e6 suc v \u27e7\u00b2 \u211cs (as\u211cbs , a\u211cb) = v\u27e6 v \u27e7\u00b2 \u211cs as\u211cbs\n\n-- Expressions\n\ndata Exp : \u2200 {\u03b1s} \u2192 Ctxt \u03b1s \u2192 Typ \u03b1s \u2192 RSet\u2081 where\n const : \u2200 {\u03b1s \u0393 A t} \u2192 (a : A) \u2192 Exp {\u03b1s} \u0393 \u27e8 A \u27e9 t\n pair : \u2200 {\u03b1s \u0393 T U t} \u2192 (e : Exp \u0393 T t) \u2192 (f : Exp \u0393 U t) \u2192 Exp {\u03b1s} \u0393 (T \u2227 U) t\n fst : \u2200 {\u03b1s \u0393 T U t} \u2192 (e : Exp \u0393 (T \u2227 U) t) \u2192 Exp {\u03b1s} \u0393 T t\n snd : \u2200 {\u03b1s \u0393 T U t} \u2192 (e : Exp \u0393 (T \u2227 U) t) \u2192 Exp {\u03b1s} \u0393 U t\n abs : \u2200 {\u03b1s \u0393 T U t} \u2192 (e : Exp (\u0393 , T at t) U t) \u2192 Exp {\u03b1s} \u0393 (T \u21d2 U) t\n app : \u2200 {\u03b1s \u0393 T U t} \u2192 (f : Exp \u0393 (T \u21d2 U) t) \u2192 (e : Exp \u0393 T t) \u2192 Exp {\u03b1s} \u0393 U t\n var : \u2200 {\u03b1s \u0393 T t} \u2192 (v : Var T t \u0393) \u2192 Exp {\u03b1s} \u0393 T t\n tabs : \u2200 {\u03b1s \u0393} \u03b1 {T t} \u2192 (e : Exp (cweaken \u03b1 \u0393) T t) \u2192 Exp {\u03b1s} \u0393 (univ \u03b1 T) t\n tapp : \u2200 {\u03b1s \u0393 t} (T : Typ \u03b1s) {U} \u2192 (e : Exp \u0393 (univ (tlevel T) U) t) \u2192 Exp {\u03b1s} \u0393 (tsubst T \u03b5 U) t\n\ne\u27e6_\u27e7 : \u2200 {\u03b1s} {\u0393 : Ctxt \u03b1s} {T : Typ \u03b1s} {t} \u2192 Exp \u0393 T t \u2192 \u2200 As \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7 As \u27e9 \u2192 T\u27e6 T \u27e7 As t\ne\u27e6 const a \u27e7 As as = a\ne\u27e6 pair e f \u27e7 As as = (e\u27e6 e \u27e7 As as , e\u27e6 f \u27e7 As as)\ne\u27e6 fst e \u27e7 As as = proj\u2081 (e\u27e6 e \u27e7 As as)\ne\u27e6 snd e \u27e7 As as = proj\u2082 (e\u27e6 e \u27e7 As as)\ne\u27e6 abs e \u27e7 As as = \u03bb a \u2192 e\u27e6 e \u27e7 As (as , a)\ne\u27e6 app f e \u27e7 As as = e\u27e6 f \u27e7 As as (e\u27e6 e \u27e7 As as)\ne\u27e6 var v \u27e7 As as = v\u27e6 v \u27e7 As as\ne\u27e6 tabs {\u0393 = \u0393} \u03b1 e \u27e7 As as = \u03bb A \u2192 e\u27e6 e \u27e7 (As , A) (\u27e6cweaken\u27e7 \u03b1 \u0393 As A as)\ne\u27e6 tapp {t = t} T {U = U} e \u27e7 As as = \u27e6tsubst\u27e7 T \u03b5 U As tt t (e\u27e6 e \u27e7 As as (T\u27e6 T \u27e7 As))\n\ne\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} {\u0393 : Ctxt \u03b1s} {T : Typ \u03b1s} {t} (e : Exp \u0393 T t) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 \n \u2200 {as bs} \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs \u27e9\u00b2 as bs \u2192 T\u27e6 T \u27e7\u00b2 \u211cs t (e\u27e6 e \u27e7 As as) (e\u27e6 e \u27e7 Bs bs)\ne\u27e6 const a \u27e7\u00b2 \u211cs as\u211cbs = refl\ne\u27e6 pair e f \u27e7\u00b2 \u211cs as\u211cbs = (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs , e\u27e6 f \u27e7\u00b2 \u211cs as\u211cbs)\ne\u27e6 fst e \u27e7\u00b2 \u211cs as\u211cbs = proj\u2081 (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs)\ne\u27e6 snd e \u27e7\u00b2 \u211cs as\u211cbs = proj\u2082 (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs)\ne\u27e6 abs e \u27e7\u00b2 \u211cs as\u211cbs = \u03bb a\u211cb \u2192 e\u27e6 e \u27e7\u00b2 \u211cs (as\u211cbs , a\u211cb)\ne\u27e6 app f e \u27e7\u00b2 \u211cs as\u211cbs = e\u27e6 f \u27e7\u00b2 \u211cs as\u211cbs (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs)\ne\u27e6 var v \u27e7\u00b2 \u211cs as\u211cbs = v\u27e6 v \u27e7\u00b2 \u211cs as\u211cbs\ne\u27e6 tabs {\u0393 = \u0393} \u03b1 e \u27e7\u00b2 \u211cs as\u211cbs = \u03bb \u211c \u2192 e\u27e6 e \u27e7\u00b2 (\u211cs , \u211c) (\u27e6cweaken\u27e7\u00b2 \u03b1 \u0393 \u211cs \u211c as\u211cbs)\ne\u27e6 tapp {t = t} T {U = U} e \u27e7\u00b2 \u211cs as\u211cbs = \u27e6tsubst\u27e7\u00b2 T \u03b5 U \u211cs tt t (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs (T\u27e6 T \u27e7\u00b2 \u211cs))\n\n-- Surface level types\n\ndata STyp : Set\u2081 where\n \u27e8_\u27e9 : (A : Set) \u2192 STyp\n _\u2227_ _\u21d2_ : (T : STyp) \u2192 (U : STyp) \u2192 STyp\n\n-- Translation of surface level types into types\n\n\u27ea_\u27eb : STyp \u2192 Typ (\u03b5 , o)\n\u27ea \u27e8 A \u27e9 \u27eb = \u27e8 A \u27e9\n\u27ea T \u2227 U \u27eb = \u27ea T \u27eb \u2227 \u27ea U \u27eb\n\u27ea T \u21d2 U \u27eb = \u27ea T \u27eb \u21d2 \u27ea U \u27eb\n\nT\u27ea_\u27eb : STyp \u2192 RSet\u2080\nT\u27ea \u27e8 A \u27e9 \u27eb t = A\nT\u27ea T \u2227 U \u27eb t = T\u27ea T \u27eb t \u00d7 T\u27ea U \u27eb t\nT\u27ea T \u21d2 U \u27eb t = T\u27ea T \u27eb t \u2192 T\u27ea U \u27eb t\n\nWorld : RSet\u2080\nWorld t = \u22a4\n\nmutual\n\n trans : \u2200 T {s} \u2192 T\u27ea T \u27eb s \u2192 T\u27e6 \u27ea T \u27eb \u27e7 (tt , World) s\n trans \u27e8 A \u27e9 a = a\n trans (T \u2227 U) (a , b) = (trans T a , trans U b)\n trans (T \u21d2 U) f = \u03bb a \u2192 trans U (f (trans\u207b\u00b9 T a))\n\n trans\u207b\u00b9 : \u2200 T {s} \u2192 T\u27e6 \u27ea T \u27eb \u27e7 (tt , World) s \u2192 T\u27ea T \u27eb s\n trans\u207b\u00b9 \u27e8 A \u27e9 a = a\n trans\u207b\u00b9 (T \u2227 U) (a , b) = (trans\u207b\u00b9 T a , trans\u207b\u00b9 U b)\n trans\u207b\u00b9 (T \u21d2 U) f = \u03bb a \u2192 trans\u207b\u00b9 U (f (trans T a))\n\n-- Causality\n\n_at_\u220b_\u2248[_\u2235_]_ : \u2200 T s \u2192 T\u27ea T \u27eb s \u2192 \u2200 u \u2192 True (s \u2264 u) \u2192 T\u27ea T \u27eb s \u2192 Set\n\u27e8 A \u27e9 at s \u220b a \u2248[ u \u2235 s\u2264u ] b = a \u2261 b\n(T \u2227 U) at s \u220b (a , b) \u2248[ u \u2235 s\u2264u ] (c , d) = (T at s \u220b a \u2248[ u \u2235 s\u2264u ] c) \u00d7 (U at s \u220b b \u2248[ u \u2235 s\u2264u ] d)\n(T \u21d2 U) at s \u220b f \u2248[ u \u2235 s\u2264u ] g = \u2200 {a b} \u2192 (T at s \u220b a \u2248[ u \u2235 s\u2264u ] b) \u2192 (U at s \u220b f a \u2248[ u \u2235 s\u2264u ] g b)\n\nCausal : \u2200 T U s \u2192 T\u27ea T \u21d2 U \u27eb s \u2192 Set\nCausal T U s f = \u2200 u s\u2264u {a b} \u2192 (T at s \u220b a \u2248[ u \u2235 s\u2264u ] b) \u2192 (U at s \u220b f a \u2248[ u \u2235 s\u2264u ] f b)\n\n-- Parametricity implies causality\n\n\u211c[_] : Time \u2192 (o \u220b World \u21d4 World)\n\u211c[ u ] t tt tt = True (t \u2264 u)\n\nmutual\n\n \u211c-impl-\u2248 : \u2200 T s u s\u2264u {a b} \u2192\n T\u27e6 \u27ea T \u27eb \u27e7\u00b2 (tt , \u211c[ u ]) s a b \u2192\n (T at s \u220b trans\u207b\u00b9 T a \u2248[ u \u2235 s\u2264u ] trans\u207b\u00b9 T b)\n \u211c-impl-\u2248 \u27e8 A \u27e9 s u s\u2264u a\u211cb = a\u211cb\n \u211c-impl-\u2248 (T \u2227 U) s u s\u2264u (a\u211cc , b\u211cd) = (\u211c-impl-\u2248 T s u s\u2264u a\u211cc , \u211c-impl-\u2248 U s u s\u2264u b\u211cd)\n \u211c-impl-\u2248 (T \u21d2 U) s u s\u2264u f\u211cg = \u03bb a\u2248b \u2192 \u211c-impl-\u2248 U s u s\u2264u (f\u211cg (\u2248-impl-\u211c T s u s\u2264u a\u2248b))\n\n \u2248-impl-\u211c : \u2200 T s u s\u2264u {a b} \u2192\n (T at s \u220b a \u2248[ u \u2235 s\u2264u ] b) \u2192\n T\u27e6 \u27ea T \u27eb \u27e7\u00b2 (tt , \u211c[ u ]) s (trans T a) (trans T b)\n \u2248-impl-\u211c \u27e8 A \u27e9 s u s\u2264u a\u2248b = a\u2248b\n \u2248-impl-\u211c (T \u2227 U) s u s\u2264u (a\u2248c , b\u2248d) = (\u2248-impl-\u211c T s u s\u2264u a\u2248c , \u2248-impl-\u211c U s u s\u2264u b\u2248d)\n \u2248-impl-\u211c (T \u21d2 U) s u s\u2264u f\u2248g = \u03bb a\u211cb \u2192 \u2248-impl-\u211c U s u s\u2264u (f\u2248g (\u211c-impl-\u2248 T s u s\u2264u a\u211cb))\n\n-- Every expression is causal\n\ne\u27ea_at_\u220b_\u27eb : \u2200 T t \u2192 Exp \u03b5 \u27ea T \u27eb t \u2192 T\u27ea T \u27eb t\ne\u27ea T at t \u220b e \u27eb = trans\u207b\u00b9 T (e\u27e6 e \u27e7 (tt , World) tt)\n\ncausality : \u2200 T U s f \u2192 Causal T U s e\u27ea (T \u21d2 U) at s \u220b f \u27eb \ncausality T U s f u s\u2264u = \u211c-impl-\u2248 (T \u21d2 U) s u s\u2264u (e\u27e6 f \u27e7\u00b2 (tt , \u211c[ u ]) tt)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"71b3a14edcb512975548e8df67858a30b1f62d68","subject":"Rename TermConstructor to CurriedTermConstructor.","message":"Rename TermConstructor to CurriedTermConstructor.\n\nThis rename prepares for introducing UncurriedTermConstructor which\nwill allow to clarify the role of lift-\u03b7-const-rec.\n\nOld-commit-hash: b9de3c413f5a2a2015d3114bfb8483ab3078cc06\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Term\/Plotkin.agda","new_file":"Syntax\/Term\/Plotkin.agda","new_contents":"import Syntax.Type.Plotkin as Type\nimport Syntax.Context as Context\n\nmodule Syntax.Term.Plotkin\n {B : Set {- of base types -}}\n {C : Context.Context {Type.Type B} \u2192 Type.Type B \u2192 Set {- of constants -}}\n where\n\n-- Terms of languages described in Plotkin style\n\nopen import Function using (_\u2218_)\nopen import Data.Product\n\nopen Type B\nopen Context {Type}\n\nopen import Denotation.Environment Type\nopen import Syntax.Context.Plotkin B\n\n-- Declarations of Term and Terms to enable mutual recursion\ndata Term\n (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set\n\ndata Terms\n (\u0393 : Context) :\n (\u03a3 : Context) \u2192 Set\n\n-- (Term \u0393 \u03c4) represents a term of type \u03c4\n-- with free variables bound in \u0393.\ndata Term \u0393 where\n const : \u2200 {\u03a3 \u03c4} \u2192\n (c : C \u03a3 \u03c4) \u2192\n Terms \u0393 \u03a3 \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\n-- (Terms \u0393 \u03a3) represents a list of terms with types from \u03a3\n-- with free variables bound in \u0393.\ndata Terms \u0393 where\n \u2205 : Terms \u0393 \u2205\n _\u2022_ : \u2200 {\u03c4 \u03a3} \u2192\n Term \u0393 \u03c4 \u2192\n Terms \u0393 \u03a3 \u2192\n Terms \u0393 (\u03c4 \u2022 \u03a3)\n\ninfixr 9 _\u2022_\n\n-- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n-- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\nlift-diff-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4) \u00d7 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-diff-apply diff apply {base \u03b9} = diff , apply\nlift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\nlift-diff : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4)\n\nlift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\nlift-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n-- Weakening\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\n\nweakenAll : \u2200 {\u0393\u2081 \u0393\u2082 \u03a3} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Terms \u0393\u2081 \u03a3 \u2192\n Terms \u0393\u2082 \u03a3\n\nweaken \u0393\u2081\u227c\u0393\u2082 (const c ts) = const c (weakenAll \u0393\u2081\u227c\u0393\u2082 ts)\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (lift \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\nweakenAll \u0393\u2081\u227c\u0393\u2082 \u2205 = \u2205\nweakenAll \u0393\u2081\u227c\u0393\u2082 (t \u2022 ts) = weaken \u0393\u2081\u227c\u0393\u2082 t \u2022 weakenAll \u0393\u2081\u227c\u0393\u2082 ts\n\n-- Specialized weakening\nweaken\u2081 : \u2200 {\u0393 \u03c3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4\nweaken\u2081 t = weaken (drop _ \u2022 \u227c-refl) t\n\nweaken\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u0393) \u03c4\nweaken\u2082 t = weaken (drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\nweaken\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u03b3 \u2022 \u0393) \u03c4\nweaken\u2083 t = weaken (drop _ \u2022 drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n-- Shorthands for nested applications\napp\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3\napp\u2082 f x = app (app f x)\n\napp\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4\napp\u2083 f x = app\u2082 (app f x)\n\napp\u2084 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5\napp\u2084 f x = app\u2083 (app f x)\n\napp\u2085 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6\napp\u2085 f x = app\u2084 (app f x)\n\napp\u2086 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6 \u21d2 \u03b7) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6 \u2192 Term \u0393 \u03b7\napp\u2086 f x = app\u2085 (app f x)\n\nCurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nCurriedTermConstructor \u0393 \u2205 \u03c4\u2032 = Term \u0393 \u03c4\u2032\nCurriedTermConstructor \u0393 (\u03c4 \u2022 \u03a3) \u03c4\u2032 = Term \u0393 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\u2032\n\n-- helper for lift-\u03b7-const, don't try to understand at home\nlift-\u03b7-const-rec : \u2200 {\u03a3 \u0393 \u03c4} \u2192 (Terms \u0393 \u03a3 \u2192 Term \u0393 \u03c4) \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const-rec {\u2205} k = k \u2205\nlift-\u03b7-const-rec {\u03c4 \u2022 \u03a3} k = \u03bb t \u2192 lift-\u03b7-const-rec (\u03bb ts \u2192 k (t \u2022 ts))\n\nlift-\u03b7-const : \u2200 {\u03a3 \u03c4} \u2192 C \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const constant = lift-\u03b7-const-rec (const constant)\n","old_contents":"import Syntax.Type.Plotkin as Type\nimport Syntax.Context as Context\n\nmodule Syntax.Term.Plotkin\n {B : Set {- of base types -}}\n {C : Context.Context {Type.Type B} \u2192 Type.Type B \u2192 Set {- of constants -}}\n where\n\n-- Terms of languages described in Plotkin style\n\nopen import Function using (_\u2218_)\nopen import Data.Product\n\nopen Type B\nopen Context {Type}\n\nopen import Denotation.Environment Type\nopen import Syntax.Context.Plotkin B\n\n-- Declarations of Term and Terms to enable mutual recursion\ndata Term\n (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set\n\ndata Terms\n (\u0393 : Context) :\n (\u03a3 : Context) \u2192 Set\n\n-- (Term \u0393 \u03c4) represents a term of type \u03c4\n-- with free variables bound in \u0393.\ndata Term \u0393 where\n const : \u2200 {\u03a3 \u03c4} \u2192\n (c : C \u03a3 \u03c4) \u2192\n Terms \u0393 \u03a3 \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\n-- (Terms \u0393 \u03a3) represents a list of terms with types from \u03a3\n-- with free variables bound in \u0393.\ndata Terms \u0393 where\n \u2205 : Terms \u0393 \u2205\n _\u2022_ : \u2200 {\u03c4 \u03a3} \u2192\n Term \u0393 \u03c4 \u2192\n Terms \u0393 \u03a3 \u2192\n Terms \u0393 (\u03c4 \u2022 \u03a3)\n\ninfixr 9 _\u2022_\n\n-- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n-- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\nlift-diff-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4) \u00d7 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-diff-apply diff apply {base \u03b9} = diff , apply\nlift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\nlift-diff : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4)\n\nlift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\nlift-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n-- Weakening\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\n\nweakenAll : \u2200 {\u0393\u2081 \u0393\u2082 \u03a3} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Terms \u0393\u2081 \u03a3 \u2192\n Terms \u0393\u2082 \u03a3\n\nweaken \u0393\u2081\u227c\u0393\u2082 (const c ts) = const c (weakenAll \u0393\u2081\u227c\u0393\u2082 ts)\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (lift \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\nweakenAll \u0393\u2081\u227c\u0393\u2082 \u2205 = \u2205\nweakenAll \u0393\u2081\u227c\u0393\u2082 (t \u2022 ts) = weaken \u0393\u2081\u227c\u0393\u2082 t \u2022 weakenAll \u0393\u2081\u227c\u0393\u2082 ts\n\n-- Specialized weakening\nweaken\u2081 : \u2200 {\u0393 \u03c3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4\nweaken\u2081 t = weaken (drop _ \u2022 \u227c-refl) t\n\nweaken\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u0393) \u03c4\nweaken\u2082 t = weaken (drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\nweaken\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u03b3 \u2022 \u0393) \u03c4\nweaken\u2083 t = weaken (drop _ \u2022 drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n-- Shorthands for nested applications\napp\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3\napp\u2082 f x = app (app f x)\n\napp\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4\napp\u2083 f x = app\u2082 (app f x)\n\napp\u2084 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5\napp\u2084 f x = app\u2083 (app f x)\n\napp\u2085 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6\napp\u2085 f x = app\u2084 (app f x)\n\napp\u2086 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6 \u21d2 \u03b7) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6 \u2192 Term \u0393 \u03b7\napp\u2086 f x = app\u2085 (app f x)\n\nTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nTermConstructor \u0393 \u2205 \u03c4\u2032 = Term \u0393 \u03c4\u2032\nTermConstructor \u0393 (\u03c4 \u2022 \u03a3) \u03c4\u2032 = Term \u0393 \u03c4 \u2192 TermConstructor \u0393 \u03a3 \u03c4\u2032\n\n-- helper for lift-\u03b7-const, don't try to understand at home\nlift-\u03b7-const-rec : \u2200 {\u03a3 \u0393 \u03c4} \u2192 (Terms \u0393 \u03a3 \u2192 Term \u0393 \u03c4) \u2192 TermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const-rec {\u2205} k = k \u2205\nlift-\u03b7-const-rec {\u03c4 \u2022 \u03a3} k = \u03bb t \u2192 lift-\u03b7-const-rec (\u03bb ts \u2192 k (t \u2022 ts))\n\nlift-\u03b7-const : \u2200 {\u03a3 \u03c4} \u2192 C \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 TermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const constant = lift-\u03b7-const-rec (const constant)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"58c240b5bb748fbe58ece4cf0646e3c2245c5642","subject":"bijection-syntax: fix","message":"bijection-syntax: fix\n","repos":"crypto-agda\/crypto-agda","old_file":"bijection-syntax\/Bijection-Fin.agda","new_file":"bijection-syntax\/Bijection-Fin.agda","new_contents":"-- NOTE with-K\nmodule bijection-syntax.Bijection-Fin where\n\n open import Type\n\n open import bijection-syntax.Bijection\n open import Function.NP hiding (Cmp)\n open import Relation.Binary.PropositionalEquality.NP\n\n open import Data.Empty\n open import Data.Nat.NP hiding (suc-injective)\n open import Data.Two\n open import Data.Fin.NP\n using ( Fin ; zero ; suc ; \u2115\u25b9Fin ; inject\u2081; suc-injective; _\u2264F_; z\u2264i; s\u2264s\n ; module From-mono-inj )\n open import Data.Nat.BoundedMonoInj-is-Id hiding (module From-mono-inj)\n open import Data.Vec hiding ([_])\n open import Data.Sum\n import Algebra.FunctionProperties.Eq\n open Algebra.FunctionProperties.Eq.Implicits\n\n data `Syn : \u2115 \u2192 Type where\n `id : \u2200 {n} \u2192 `Syn n\n `swap : \u2200 {n} \u2192 `Syn (2 + n)\n `tail : \u2200 {n} \u2192 `Syn n \u2192 `Syn (1 + n)\n _`\u2218_ : \u2200 {n} \u2192 `Syn n \u2192 `Syn n \u2192 `Syn n\n\n `Rep = Fin\n\n `Ix = \u2115\n\n `Tree : Type \u2192 `Ix \u2192 Type\n `Tree X = Vec X\n\n `fromFun : \u2200 {i X} \u2192 (`Rep i \u2192 X) \u2192 `Tree X i\n `fromFun = tabulate\n\n `toFun : \u2200 {i X} \u2192 `Tree X i \u2192 (`Rep i \u2192 X)\n `toFun T zero = head T\n `toFun T (suc i) = `toFun (tail T) i\n\n `toFun\u2218fromFun : \u2200 {i X}(f : `Rep i \u2192 X) \u2192 f \u2257 `toFun (`fromFun f)\n `toFun\u2218fromFun f zero = refl\n `toFun\u2218fromFun f (suc i) = `toFun\u2218fromFun (f \u2218 suc) i\n\n fin-swap : \u2200 {n} \u2192 Endo (Fin (2 + n))\n fin-swap zero = suc zero\n fin-swap (suc zero) = zero\n fin-swap (suc (suc i)) = suc (suc i)\n\n fin-tail : \u2200 {n} \u2192 Endo (Fin n) \u2192 Endo (Fin (1 + n))\n fin-tail f zero = zero\n fin-tail f (suc i) = suc (f i)\n\n `evalArg : \u2200 {i} \u2192 `Syn i \u2192 Endo (`Rep i)\n `evalArg `id = id\n `evalArg `swap = fin-swap\n `evalArg (`tail f) = fin-tail (`evalArg f)\n `evalArg (S `\u2218 S\u2081) = `evalArg S \u2218 `evalArg S\u2081\n\n vec-swap : \u2200 {n}{X : Type} \u2192 Endo (Vec X (2 + n))\n vec-swap xs = head (tail xs) \u2237 head xs \u2237 tail (tail xs)\n\n vec-tail : \u2200 {n}{X : Type} \u2192 Endo (Vec X n) \u2192 Endo (Vec X (1 + n))\n vec-tail f xs = head xs \u2237 f (tail xs)\n\n `evalTree : \u2200 {i X} \u2192 `Syn i \u2192 Endo (`Tree X i)\n `evalTree `id = id\n `evalTree `swap = vec-swap\n `evalTree (`tail f) = vec-tail (`evalTree f)\n `evalTree (S `\u2218 S\u2081) = `evalTree S \u2218 `evalTree S\u2081\n\n `eval-proof : \u2200 {i X} S (T : `Tree X i) \u2192 `toFun T \u2257 `toFun (`evalTree S T) \u2218 `evalArg S\n `eval-proof `id T i = refl\n `eval-proof `swap T zero = refl\n `eval-proof `swap T (suc zero) = refl\n `eval-proof `swap T (suc (suc i)) = refl\n `eval-proof (`tail S) T zero = refl\n `eval-proof (`tail S) T (suc i) = `eval-proof S (tail T) i\n `eval-proof (S `\u2218 S\u2081) T i rewrite\n `eval-proof S\u2081 T i |\n `eval-proof S (`evalTree S\u2081 T) (`evalArg S\u2081 i) = refl\n\n `inv : \u2200 {i} \u2192 Endo (`Syn i)\n `inv `id = `id\n `inv `swap = `swap\n `inv (`tail S) = `tail (`inv S)\n `inv (S `\u2218 S\u2081) = `inv S\u2081 `\u2218 `inv S\n\n `inv-proof : \u2200 {i} \u2192 (S : `Syn i) \u2192 `evalArg S \u2218 `evalArg (`inv S) \u2257 id\n `inv-proof `id x = refl\n `inv-proof `swap zero = refl\n `inv-proof `swap (suc zero) = refl\n `inv-proof `swap (suc (suc x)) = refl\n `inv-proof (`tail S) zero = refl\n `inv-proof (`tail S) (suc x) rewrite `inv-proof S x = refl\n `inv-proof (S `\u2218 S\u2081) x rewrite \n `inv-proof S\u2081 (`evalArg (`inv S) x) |\n `inv-proof S x = refl\n\n `RC : \u2200 {i} \u2192 Cmp (`Rep i)\n `RC zero zero = eq\n `RC zero (suc j) = lt\n `RC (suc i) zero = gt\n `RC (suc i) (suc j) = `RC i j\n\n insert : \u2200 {n X} \u2192 Cmp X \u2192 X \u2192 Vec X n \u2192 Vec X (1 + n)\n insert X-cmp x [] = x \u2237 []\n insert X-cmp x (x\u2081 \u2237 xs) with X-cmp x x\u2081\n insert X-cmp x (x\u2081 \u2237 xs) | lt = x \u2237 x\u2081 \u2237 xs\n insert X-cmp x (x\u2081 \u2237 xs) | eq = x \u2237 x\u2081 \u2237 xs\n insert X-cmp x (x\u2081 \u2237 xs) | gt = x\u2081 \u2237 insert X-cmp x xs\n\n `sort : \u2200 {i X} \u2192 Cmp X \u2192 Endo (`Tree X i)\n `sort X-cmp [] = []\n `sort X-cmp (x \u2237 xs) = insert X-cmp x (`sort X-cmp xs)\n\n insert-syn : \u2200 {n X} \u2192 Cmp X \u2192 X \u2192 Vec X n \u2192 `Syn (1 + n)\n insert-syn X-cmp x [] = `id\n insert-syn X-cmp x (x\u2081 \u2237 xs) with X-cmp x x\u2081\n insert-syn X-cmp x (x\u2081 \u2237 xs) | lt = `id\n insert-syn X-cmp x (x\u2081 \u2237 xs) | eq = `id\n insert-syn X-cmp x (x\u2081 \u2237 xs) | gt = `tail (insert-syn X-cmp x xs) `\u2218 `swap\n\n `sort-syn : \u2200 {i X} \u2192 Cmp X \u2192 `Tree X i \u2192 `Syn i\n `sort-syn X-cmp [] = `id\n `sort-syn X-cmp (x \u2237 xs) = insert-syn X-cmp x (`sort X-cmp xs) `\u2218 `tail (`sort-syn X-cmp xs)\n\n insert-proof : \u2200 {n X}(X-cmp : Cmp X) x (T : Vec X n) \u2192 insert X-cmp x T \u2261 `evalTree (insert-syn X-cmp x T) (x \u2237 T)\n insert-proof X-cmp x [] = refl\n insert-proof X-cmp x (x\u2081 \u2237 T) with X-cmp x x\u2081\n insert-proof X-cmp x (x\u2081 \u2237 T) | lt = refl\n insert-proof X-cmp x (x\u2081 \u2237 T) | eq = refl\n insert-proof X-cmp x (x\u2081 \u2237 T) | gt rewrite insert-proof X-cmp x T = refl\n\n `sort-proof : \u2200 {i X}(X-cmp : Cmp X)(T : `Tree X i) \u2192 `sort X-cmp T \u2261 `evalTree (`sort-syn X-cmp T) T\n `sort-proof X-cmp [] = refl\n `sort-proof X-cmp (x \u2237 T) rewrite \n ! `sort-proof X-cmp T = insert-proof X-cmp x (`sort X-cmp T)\n\n module Alt-Syn where\n\n data ``Syn : \u2115 \u2192 Type where\n `id : \u2200 {n} \u2192 ``Syn n\n _`\u2218_ : \u2200 {n} \u2192 ``Syn n \u2192 ``Syn n \u2192 ``Syn n\n `swap : \u2200 {n} m \u2192 ``Syn (m + 2 + n)\n\n swap-fin : \u2200 {n} m \u2192 Endo (Fin (m + 2 + n))\n swap-fin zero zero = suc zero\n swap-fin zero (suc zero) = zero\n swap-fin zero (suc (suc i)) = suc (suc i)\n swap-fin (suc m) zero = zero\n swap-fin (suc m) (suc i) = suc (swap-fin m i)\n\n ``evalArg : \u2200 {n} \u2192 ``Syn n \u2192 Endo (`Rep n)\n ``evalArg `id = id\n ``evalArg (S `\u2218 S\u2081) = ``evalArg S \u2218 ``evalArg S\u2081\n ``evalArg (`swap m) = swap-fin m\n\n _``\u2218_ : \u2200 {n} \u2192 ``Syn n \u2192 ``Syn n \u2192 ``Syn n\n `id ``\u2218 y = y\n (x `\u2218 x\u2081) ``\u2218 `id = x `\u2218 x\u2081\n (x `\u2218 x\u2081) ``\u2218 (y `\u2218 y\u2081) = x `\u2218 (x\u2081 `\u2218 (y `\u2218 y\u2081))\n (x `\u2218 x\u2081) ``\u2218 `swap m = x `\u2218 (x\u2081 ``\u2218 `swap m)\n `swap m ``\u2218 y = `swap m `\u2218 y\n\n ``tail : \u2200 {n} \u2192 ``Syn n \u2192 ``Syn (suc n)\n ``tail `id = `id\n ``tail (S `\u2218 S\u2081) = ``tail S ``\u2218 ``tail S\u2081\n ``tail (`swap m) = `swap (suc m)\n\n translate : \u2200 {n} \u2192 `Syn n \u2192 ``Syn n\n translate `id = `id\n translate `swap = `swap 0\n translate (`tail S) = ``tail (translate S)\n translate (S `\u2218 S\u2081) = translate S ``\u2218 translate S\u2081\n\n ``\u2218-p : \u2200 {n}(A B : ``Syn n) \u2192 ``evalArg (A ``\u2218 B) \u2257 ``evalArg (A `\u2218 B)\n ``\u2218-p `id B x = refl\n ``\u2218-p (A `\u2218 A\u2081) `id x = refl\n ``\u2218-p (A `\u2218 A\u2081) (B `\u2218 B\u2081) x = refl\n ``\u2218-p (A `\u2218 A\u2081) (`swap m) x rewrite ``\u2218-p A\u2081 (`swap m) x = refl\n ``\u2218-p (`swap m) B x = refl\n\n ``tail-p : \u2200 {n} (S : ``Syn n) \u2192 fin-tail (``evalArg S) \u2257 ``evalArg (``tail S)\n ``tail-p `id zero = refl\n ``tail-p `id (suc x) = refl\n ``tail-p (S `\u2218 S\u2081) zero rewrite ``\u2218-p (``tail S) (``tail S\u2081) zero\n | ! ``tail-p S\u2081 zero = ``tail-p S zero\n ``tail-p (S `\u2218 S\u2081) (suc x) rewrite ``\u2218-p (``tail S) (``tail S\u2081) (suc x)\n | ! ``tail-p S\u2081 (suc x) = ``tail-p S (suc (``evalArg S\u2081 x))\n ``tail-p (`swap m) zero = refl\n ``tail-p (`swap m) (suc x) = refl\n\n `eval`` : \u2200 {n} (S : `Syn n) \u2192 `evalArg S \u2257 ``evalArg (translate S)\n `eval`` `id x = refl\n `eval`` `swap zero = refl\n `eval`` `swap (suc zero) = refl\n `eval`` `swap (suc (suc x)) = refl\n `eval`` (`tail S) zero = ``tail-p (translate S) zero\n `eval`` (`tail S) (suc x) rewrite `eval`` S x = ``tail-p (translate S) (suc x)\n `eval`` (S `\u2218 S\u2081) x rewrite ``\u2218-p (translate S) (translate S\u2081) x | ! `eval`` S\u2081 x | `eval`` S (`evalArg S\u2081 x) = refl\n\n\n data Fin-View : \u2200 {n} \u2192 Fin n \u2192 Type where\n max : \u2200 {n} \u2192 Fin-View (\u2115\u25b9Fin n)\n inject : \u2200 {n} \u2192 (i : Fin n) \u2192 Fin-View (inject\u2081 i)\n\n data Sorted {X}(XC : Cmp X) : \u2200 {l} \u2192 Vec X l \u2192 Type where\n [] : Sorted XC []\n sing : \u2200 x \u2192 Sorted XC (x \u2237 [])\n dbl-lt : \u2200 {l} x y {xs : Vec X l} \u2192 lt \u2261 XC x y \u2192 Sorted XC (y \u2237 xs) \u2192 Sorted XC (x \u2237 y \u2237 xs)\n dbl-eq : \u2200 {l} x {xs : Vec X l} \u2192 Sorted XC (x \u2237 xs) \u2192 Sorted XC (x \u2237 x \u2237 xs)\n\n opposite : Ord \u2192 Ord\n opposite lt = gt\n opposite eq = eq\n opposite gt = lt\n\n flip-RC : \u2200 {n}(x y : Fin n) \u2192 opposite (`RC x y) \u2261 `RC y x\n flip-RC zero zero = refl\n flip-RC zero (suc y) = refl\n flip-RC (suc x) zero = refl\n flip-RC (suc x) (suc y) = flip-RC x y\n\n eq=>\u2261 : \u2200 {i} (x y : Fin i) \u2192 eq \u2261 `RC x y \u2192 x \u2261 y\n eq=>\u2261 zero zero p = refl\n eq=>\u2261 zero (suc y) ()\n eq=>\u2261 (suc x) zero ()\n eq=>\u2261 (suc x) (suc y) p rewrite eq=>\u2261 x y p = refl\n\n insert-Sorted : \u2200 {n l}{V : Vec (Fin n) l}(x : Fin n) \u2192 Sorted {Fin n} `RC V \u2192 Sorted {Fin n} `RC (insert `RC x V)\n insert-Sorted x [] = sing x\n insert-Sorted x (sing x\u2081) with `RC x x\u2081 | dbl-lt {XC = `RC} x x\u2081 {[]} | eq=>\u2261 x x\u2081 | flip-RC x x\u2081\n insert-Sorted x (sing x\u2081) | lt | b | _ | _ = b refl (sing x\u2081)\n insert-Sorted x (sing x\u2081) | eq | _ | p | _ rewrite p refl = dbl-eq x\u2081 (sing x\u2081)\n insert-Sorted x (sing x\u2081) | gt | b | _ | l = dbl-lt x\u2081 x l (sing x)\n insert-Sorted x (dbl-lt y y' {xs} prf xs\u2081) with `RC x y | dbl-lt {XC = `RC} x y {y' \u2237 xs} | eq=>\u2261 x y | flip-RC x y\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | lt | b | p | l\u2081 = b refl (dbl-lt y y' prf xs\u2081)\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | eq | b | p | l\u2081 rewrite p refl = dbl-eq y (dbl-lt y y' prf xs\u2081)\n insert-Sorted x (dbl-lt y y' {xs} prf xs\u2081) | gt | b | p | l\u2081 with `RC x y' | insert-Sorted x xs\u2081\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | gt | b | p | l\u2081 | lt | xs' = dbl-lt y x l\u2081 xs'\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | gt | b | p | l\u2081 | eq | xs' = dbl-lt y x l\u2081 xs'\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | gt | b | p | l\u2081 | gt | xs' = dbl-lt y y' prf xs'\n insert-Sorted x (dbl-eq y {xs} xs\u2081) with `RC x y | inspect (`RC x) y | dbl-lt {XC = `RC} x y {y \u2237 xs} | eq=>\u2261 x y | flip-RC x y | insert-Sorted x xs\u2081\n insert-Sorted x (dbl-eq y xs\u2081) | lt | _ | b | p | l | _ = b refl (dbl-eq y xs\u2081)\n insert-Sorted x (dbl-eq y xs\u2081) | eq | _ | b | p | l | _ rewrite p refl = dbl-eq y (dbl-eq y xs\u2081)\n insert-Sorted x (dbl-eq y xs\u2081) | gt | [ prf ] | b | p | l | ss rewrite prf = dbl-eq y ss \n\n sort-Sorted : \u2200 {n l}(V : Vec (Fin n) l) \u2192 Sorted `RC (`sort `RC V)\n sort-Sorted [] = []\n sort-Sorted (x \u2237 V) = insert-Sorted x (sort-Sorted V)\n\n RC-refl : \u2200 {i}(x : Fin i) \u2192 `RC x x \u2261 eq\n RC-refl zero = refl\n RC-refl (suc x) = RC-refl x\n\n STail : \u2200 {X l}{XC : Cmp X}{xs : Vec X (suc l)} \u2192 Sorted XC xs \u2192 Sorted XC (tail xs)\n STail (sing x) = []\n STail (dbl-lt x y x\u2081 T) = T\n STail (dbl-eq x T) = T\n\n module sproof {X}(XC : Cmp X)(XC-refl : \u2200 x \u2192 XC x x \u2261 eq)\n (eq\u2261 : \u2200 x y \u2192 XC x y \u2261 eq \u2192 x \u2261 y)\n (lt-trans : \u2200 x y z \u2192 XC x y \u2261 lt \u2192 XC y z \u2261 lt \u2192 XC x z \u2261 lt)\n (XC-flip : \u2200 x y \u2192 opposite (XC x y) \u2261 XC y x)\n where\n\n open import Data.Sum\n\n _\u2264X_ : X \u2192 X \u2192 Type\n x \u2264X y = XC x y \u2261 lt \u228e XC x y \u2261 eq\n\n \u2264X-trans : \u2200 {x y z} \u2192 x \u2264X y \u2192 y \u2264X z \u2192 x \u2264X z\n \u2264X-trans (inj\u2081 x\u2081) (inj\u2081 x\u2082) = inj\u2081 (lt-trans _ _ _ x\u2081 x\u2082)\n \u2264X-trans {_}{y}{z}(inj\u2081 x\u2081) (inj\u2082 y\u2081) rewrite eq\u2261 y z y\u2081 = inj\u2081 x\u2081\n \u2264X-trans {x}{y} (inj\u2082 y\u2081) y\u2264z rewrite eq\u2261 x y y\u2081 = y\u2264z\n\n h\u2264t : \u2200 {n}{T : `Tree X (2 + n)} \u2192 Sorted XC T \u2192 head T \u2264X head (tail T)\n h\u2264t (dbl-lt x y x\u2081 ST) = inj\u2081 (! x\u2081)\n h\u2264t (dbl-eq x ST) rewrite XC-refl x = inj\u2082 refl\n\n head-p : \u2200 {n}{T : `Tree X (suc n)} i \u2192 Sorted XC T \u2192 head T \u2264X `toFun T i\n head-p {T = T} zero ST rewrite XC-refl (head T) = inj\u2082 refl\n head-p {zero} (suc ()) ST\n head-p {suc n} (suc i) ST = \u2264X-trans (h\u2264t ST) (head-p i (STail ST))\n\n toFun-p : \u2200 {n}{T : `Tree X n}{i j : Fin n} \u2192 i \u2264F j \u2192 Sorted XC T \u2192 `toFun T i \u2264X `toFun T j\n toFun-p {j = j} z\u2264i ST = head-p j ST\n toFun-p (s\u2264s i\u2264Fj) ST = toFun-p i\u2264Fj (STail ST)\n\n sort-proof : \u2200 {i}{T : `Tree X i} \u2192 Sorted XC T \u2192 Is-Mono `RC XC (`toFun T)\n sort-proof {T = T} T\u2081 zero zero rewrite XC-refl (head T) = _\n sort-proof T\u2081 zero (suc y) with toFun-p (z\u2264i {i = suc y}) T\u2081\n sort-proof T zero (suc y) | inj\u2081 x rewrite x = _\n sort-proof T zero (suc y) | inj\u2082 y\u2081 rewrite y\u2081 = _\n sort-proof {T = T} T\u2081 (suc x) zero with toFun-p (z\u2264i {i = suc x}) T\u2081 | XC-flip (head T) (`toFun (tail T) x)\n sort-proof T (suc x) zero | inj\u2081 x\u2081 | l rewrite x\u2081 | ! l = _\n sort-proof T (suc x) zero | inj\u2082 y | l rewrite y | ! l = _\n sort-proof T\u2081 (suc x) (suc y) = sort-proof (STail T\u2081) x y\n\n lt-trans-RC : \u2200 {i} (x y z : Fin i) \u2192 `RC x y \u2261 lt \u2192 `RC y z \u2261 lt \u2192 `RC x z \u2261 lt\n lt-trans-RC zero zero zero x\u2261 x\u2081 y\u2081 (! x\u2082)) lt-trans-RC flip-RC (sort-Sorted T) x y\n\n move-to-RC : \u2200 {n}{x y : Fin n} \u2192 x \u2264F y \u2192 `RC x y \u2261 lt \u228e `RC x y \u2261 eq\n move-to-RC {y = zero} z\u2264i = inj\u2082 refl\n move-to-RC {y = suc y} z\u2264i = inj\u2081 refl\n move-to-RC (s\u2264s x\u2264Fy) = move-to-RC x\u2264Fy\n\n move-from-RC : \u2200 {n}(x y : Fin n) \u2192 lt \u2261 `RC x y \u228e eq \u2261 `RC x y \u2192 x \u2264F y\n move-from-RC zero zero prf = z\u2264i\n move-from-RC zero (suc y) prf = z\u2264i\n move-from-RC (suc x) zero (inj\u2081 ())\n move-from-RC (suc x) zero (inj\u2082 ())\n move-from-RC (suc x) (suc y) prf = s\u2264s (move-from-RC x y prf)\n\n mono-RC : \u2200 {n}(f : Endo (Fin n))(f-mono : Is-Mono `RC `RC f)\n {x y} \u2192 x \u2264F y \u2192 f x \u2264F f y\n mono-RC {n} f f-mono {x} {y} x\u2264Fy with `RC x y | `RC (f x) (f y) | move-to-RC x\u2264Fy | f-mono x y | move-from-RC (f x) (f y)\n mono-RC f f-mono x\u2264Fy | .lt | lt | inj\u2081 refl | r4 | r5 = r5 (inj\u2081 refl)\n mono-RC f f-mono x\u2264Fy | .lt | eq | inj\u2081 refl | r4 | r5 = r5 (inj\u2082 refl)\n mono-RC f f-mono x\u2264Fy | .lt | gt | inj\u2081 refl | () | r5\n mono-RC f f-mono x\u2264Fy | .eq | lt | inj\u2082 refl | () | r5\n mono-RC f f-mono x\u2264Fy | .eq | eq | inj\u2082 refl | r4 | r5 = r5 (inj\u2082 refl)\n mono-RC f f-mono x\u2264Fy | .eq | gt | inj\u2082 refl | () | r5\n\n\n fin-view : \u2200 {n} \u2192 (i : Fin (suc n)) \u2192 Fin-View i\n fin-view {zero} zero = max\n fin-view {zero} (suc ())\n fin-view {suc n} zero = inject _\n fin-view {suc n} (suc i) with fin-view i\n fin-view {suc n} (suc .(\u2115\u25b9Fin n)) | max = max\n fin-view {suc n} (suc .(inject\u2081 i)) | inject i = inject _\n\n absurd : {X : Type} \u2192 .\u22a5 \u2192 X\n absurd ()\n\n drop\u2081 : \u2200 {n} \u2192 (i : Fin (suc n)) \u2192 .(i \u2262 \u2115\u25b9Fin n) \u2192 Fin n\n drop\u2081 i neq with fin-view i\n drop\u2081 {n} .(\u2115\u25b9Fin n) neq | max = absurd (neq refl)\n drop\u2081 .(inject\u2081 i) neq | inject i = i\n\n drop\u2081\u2192inject\u2081 : \u2200 {n}(i : Fin (suc n))(j : Fin n).(p : i \u2262 \u2115\u25b9Fin n) \u2192 drop\u2081 i p \u2261 j \u2192 i \u2261 inject\u2081 j\n drop\u2081\u2192inject\u2081 i j p q with fin-view i\n drop\u2081\u2192inject\u2081 {n} .(\u2115\u25b9Fin n) j p q | max = absurd (p refl)\n drop\u2081\u2192inject\u2081 .(inject\u2081 i) j p q | inject i = ap inject\u2081 q\n\n\n `mono-inj\u2192id : \u2200{i}(f : Endo (`Rep i)) \u2192 Injective f \u2192 Is-Mono `RC `RC f \u2192 f \u2257 id\n `mono-inj\u2192id f inj mono = From-mono-inj.f\u2257id f inj (mono-RC f mono)\n\n\n interface : Interface\n interface = record \n { Ix = `Ix\n ; Rep = `Rep\n ; Syn = `Syn\n ; Tree = `Tree\n ; fromFun = `fromFun\n ; toFun = `toFun\n ; toFun\u2218fromFun = `toFun\u2218fromFun\n ; evalArg = `evalArg\n ; evalTree = `evalTree\n ; eval-proof = `eval-proof\n ; inv = `inv\n ; inv-proof = `inv-proof\n ; RC = `RC\n ; sort = `sort\n ; sort-syn = `sort-syn\n ; sort-proof = `sort-proof\n ; sort-mono = `sort-mono\n ; mono-inj\u2192id = `mono-inj\u2192id\n }\n\n count : \u2200 {n} \u2192 (Fin n \u2192 \u2115) \u2192 \u2115\n count {n} f = sum (tabulate f)\n\n count-ext : \u2200 {n} \u2192 (f g : Fin n \u2192 \u2115) \u2192 f \u2257 g \u2192 count f \u2261 count g\n count-ext {zero} f g f\u2257g = refl\n count-ext {suc n} f g f\u2257g rewrite f\u2257g zero | count-ext (f \u2218 suc) (g \u2218 suc) (f\u2257g \u2218 suc) = refl\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Fin n \u2192 \ud835\udfda) \u2192 \u2115\n #\u27e8 f \u27e9 = count (\ud835\udfda\u25b9\u2115 \u2218 f)\n\n #-ext : \u2200 {n} \u2192 (f g : Fin n \u2192 \ud835\udfda) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-ext f g f\u2257g = count-ext (\ud835\udfda\u25b9\u2115 \u2218 f) (\ud835\udfda\u25b9\u2115 \u2218 g) (ap \ud835\udfda\u25b9\u2115 \u2218 f\u2257g)\n\n com-assoc : \u2200 x y z \u2192 x + (y + z) \u2261 y + (x + z)\n com-assoc x y z rewrite \n ! \u2115\u00b0.+-assoc x y z |\n \u2115\u00b0.+-comm x y |\n \u2115\u00b0.+-assoc y x z = refl\n \n syn-pres : \u2200 {n}(f : Fin n \u2192 \u2115)(S : `Syn n)\n \u2192 count f \u2261 count (f \u2218 `evalArg S)\n syn-pres f `id = refl\n syn-pres f `swap = com-assoc (f zero) (f (suc zero)) (count (\u03bb i \u2192 f (suc (suc i))))\n syn-pres f (`tail S) rewrite syn-pres (f \u2218 suc) S = refl\n syn-pres f (S `\u2218 S\u2081) rewrite syn-pres f S = syn-pres (f \u2218 `evalArg S) S\u2081\n\n count-perm : \u2200 {n}(f : Fin n \u2192 \u2115)(p : Endo (Fin n)) \u2192 Injective p\n \u2192 count f \u2261 count (f \u2218 p)\n count-perm f p p-inj = trans (syn-pres f (sort-bij p)) (count-ext _ _ f\u2218eval\u2257f\u2218p)\n where\n open abs interface\n f\u2218eval\u2257f\u2218p : f \u2218 `evalArg (sort-bij p) \u2257 f \u2218 p\n f\u2218eval\u2257f\u2218p x rewrite thm p p-inj x = refl\n\n\n #-perm : \u2200 {n}(f : Fin n \u2192 \ud835\udfda)(p : Endo (Fin n)) \u2192 Injective p\n \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 p \u27e9\n #-perm f p p-inj = count-perm (\ud835\udfda\u25b9\u2115 \u2218 f) p p-inj\n\n test : `Syn 8\n test = abs.sort-bij interface (\u03bb x \u2192 `evalArg (`tail `swap) x)\n-- -}\n","old_contents":"-- NOTE with-K\nmodule bijection-syntax.Bijection-Fin where\n\n open import Type\n\n open import bijection-syntax.Bijection\n open import Function.NP hiding (Cmp)\n open import Relation.Binary.PropositionalEquality.NP\n\n open import Data.Empty\n open import Data.Nat.NP hiding (suc-injective)\n open import Data.Two\n open import Data.Fin.NP\n using ( Fin ; zero ; suc ; \u2115\u25b9Fin ; inject\u2081; suc-injective; _\u2264F_; z\u2264i; s\u2264s\n ; module From-mono-inj )\n open import Data.Vec hiding ([_])\n open import Data.Sum\n\n data `Syn : \u2115 \u2192 Type where\n `id : \u2200 {n} \u2192 `Syn n\n `swap : \u2200 {n} \u2192 `Syn (2 + n)\n `tail : \u2200 {n} \u2192 `Syn n \u2192 `Syn (1 + n)\n _`\u2218_ : \u2200 {n} \u2192 `Syn n \u2192 `Syn n \u2192 `Syn n\n\n `Rep = Fin\n\n `Ix = \u2115\n\n `Tree : Type \u2192 `Ix \u2192 Type\n `Tree X = Vec X\n\n `fromFun : \u2200 {i X} \u2192 (`Rep i \u2192 X) \u2192 `Tree X i\n `fromFun = tabulate\n\n `toFun : \u2200 {i X} \u2192 `Tree X i \u2192 (`Rep i \u2192 X)\n `toFun T zero = head T\n `toFun T (suc i) = `toFun (tail T) i\n\n `toFun\u2218fromFun : \u2200 {i X}(f : `Rep i \u2192 X) \u2192 f \u2257 `toFun (`fromFun f)\n `toFun\u2218fromFun f zero = refl\n `toFun\u2218fromFun f (suc i) = `toFun\u2218fromFun (f \u2218 suc) i\n\n fin-swap : \u2200 {n} \u2192 Endo (Fin (2 + n))\n fin-swap zero = suc zero\n fin-swap (suc zero) = zero\n fin-swap (suc (suc i)) = suc (suc i)\n\n fin-tail : \u2200 {n} \u2192 Endo (Fin n) \u2192 Endo (Fin (1 + n))\n fin-tail f zero = zero\n fin-tail f (suc i) = suc (f i)\n\n `evalArg : \u2200 {i} \u2192 `Syn i \u2192 Endo (`Rep i)\n `evalArg `id = id\n `evalArg `swap = fin-swap\n `evalArg (`tail f) = fin-tail (`evalArg f)\n `evalArg (S `\u2218 S\u2081) = `evalArg S \u2218 `evalArg S\u2081\n\n vec-swap : \u2200 {n}{X : Type} \u2192 Endo (Vec X (2 + n))\n vec-swap xs = head (tail xs) \u2237 head xs \u2237 tail (tail xs)\n\n vec-tail : \u2200 {n}{X : Type} \u2192 Endo (Vec X n) \u2192 Endo (Vec X (1 + n))\n vec-tail f xs = head xs \u2237 f (tail xs)\n\n `evalTree : \u2200 {i X} \u2192 `Syn i \u2192 Endo (`Tree X i)\n `evalTree `id = id\n `evalTree `swap = vec-swap\n `evalTree (`tail f) = vec-tail (`evalTree f)\n `evalTree (S `\u2218 S\u2081) = `evalTree S \u2218 `evalTree S\u2081\n\n `eval-proof : \u2200 {i X} S (T : `Tree X i) \u2192 `toFun T \u2257 `toFun (`evalTree S T) \u2218 `evalArg S\n `eval-proof `id T i = refl\n `eval-proof `swap T zero = refl\n `eval-proof `swap T (suc zero) = refl\n `eval-proof `swap T (suc (suc i)) = refl\n `eval-proof (`tail S) T zero = refl\n `eval-proof (`tail S) T (suc i) = `eval-proof S (tail T) i\n `eval-proof (S `\u2218 S\u2081) T i rewrite\n `eval-proof S\u2081 T i |\n `eval-proof S (`evalTree S\u2081 T) (`evalArg S\u2081 i) = refl\n\n `inv : \u2200 {i} \u2192 Endo (`Syn i)\n `inv `id = `id\n `inv `swap = `swap\n `inv (`tail S) = `tail (`inv S)\n `inv (S `\u2218 S\u2081) = `inv S\u2081 `\u2218 `inv S\n\n `inv-proof : \u2200 {i} \u2192 (S : `Syn i) \u2192 `evalArg S \u2218 `evalArg (`inv S) \u2257 id\n `inv-proof `id x = refl\n `inv-proof `swap zero = refl\n `inv-proof `swap (suc zero) = refl\n `inv-proof `swap (suc (suc x)) = refl\n `inv-proof (`tail S) zero = refl\n `inv-proof (`tail S) (suc x) rewrite `inv-proof S x = refl\n `inv-proof (S `\u2218 S\u2081) x rewrite \n `inv-proof S\u2081 (`evalArg (`inv S) x) |\n `inv-proof S x = refl\n\n `RC : \u2200 {i} \u2192 Cmp (`Rep i)\n `RC zero zero = eq\n `RC zero (suc j) = lt\n `RC (suc i) zero = gt\n `RC (suc i) (suc j) = `RC i j\n\n insert : \u2200 {n X} \u2192 Cmp X \u2192 X \u2192 Vec X n \u2192 Vec X (1 + n)\n insert X-cmp x [] = x \u2237 []\n insert X-cmp x (x\u2081 \u2237 xs) with X-cmp x x\u2081\n insert X-cmp x (x\u2081 \u2237 xs) | lt = x \u2237 x\u2081 \u2237 xs\n insert X-cmp x (x\u2081 \u2237 xs) | eq = x \u2237 x\u2081 \u2237 xs\n insert X-cmp x (x\u2081 \u2237 xs) | gt = x\u2081 \u2237 insert X-cmp x xs\n\n `sort : \u2200 {i X} \u2192 Cmp X \u2192 Endo (`Tree X i)\n `sort X-cmp [] = []\n `sort X-cmp (x \u2237 xs) = insert X-cmp x (`sort X-cmp xs)\n\n insert-syn : \u2200 {n X} \u2192 Cmp X \u2192 X \u2192 Vec X n \u2192 `Syn (1 + n)\n insert-syn X-cmp x [] = `id\n insert-syn X-cmp x (x\u2081 \u2237 xs) with X-cmp x x\u2081\n insert-syn X-cmp x (x\u2081 \u2237 xs) | lt = `id\n insert-syn X-cmp x (x\u2081 \u2237 xs) | eq = `id\n insert-syn X-cmp x (x\u2081 \u2237 xs) | gt = `tail (insert-syn X-cmp x xs) `\u2218 `swap\n\n `sort-syn : \u2200 {i X} \u2192 Cmp X \u2192 `Tree X i \u2192 `Syn i\n `sort-syn X-cmp [] = `id\n `sort-syn X-cmp (x \u2237 xs) = insert-syn X-cmp x (`sort X-cmp xs) `\u2218 `tail (`sort-syn X-cmp xs)\n\n insert-proof : \u2200 {n X}(X-cmp : Cmp X) x (T : Vec X n) \u2192 insert X-cmp x T \u2261 `evalTree (insert-syn X-cmp x T) (x \u2237 T)\n insert-proof X-cmp x [] = refl\n insert-proof X-cmp x (x\u2081 \u2237 T) with X-cmp x x\u2081\n insert-proof X-cmp x (x\u2081 \u2237 T) | lt = refl\n insert-proof X-cmp x (x\u2081 \u2237 T) | eq = refl\n insert-proof X-cmp x (x\u2081 \u2237 T) | gt rewrite insert-proof X-cmp x T = refl\n\n `sort-proof : \u2200 {i X}(X-cmp : Cmp X)(T : `Tree X i) \u2192 `sort X-cmp T \u2261 `evalTree (`sort-syn X-cmp T) T\n `sort-proof X-cmp [] = refl\n `sort-proof X-cmp (x \u2237 T) rewrite \n ! `sort-proof X-cmp T = insert-proof X-cmp x (`sort X-cmp T)\n\n module Alt-Syn where\n\n data ``Syn : \u2115 \u2192 Type where\n `id : \u2200 {n} \u2192 ``Syn n\n _`\u2218_ : \u2200 {n} \u2192 ``Syn n \u2192 ``Syn n \u2192 ``Syn n\n `swap : \u2200 {n} m \u2192 ``Syn (m + 2 + n)\n\n swap-fin : \u2200 {n} m \u2192 Endo (Fin (m + 2 + n))\n swap-fin zero zero = suc zero\n swap-fin zero (suc zero) = zero\n swap-fin zero (suc (suc i)) = suc (suc i)\n swap-fin (suc m) zero = zero\n swap-fin (suc m) (suc i) = suc (swap-fin m i)\n\n ``evalArg : \u2200 {n} \u2192 ``Syn n \u2192 Endo (`Rep n)\n ``evalArg `id = id\n ``evalArg (S `\u2218 S\u2081) = ``evalArg S \u2218 ``evalArg S\u2081\n ``evalArg (`swap m) = swap-fin m\n\n _``\u2218_ : \u2200 {n} \u2192 ``Syn n \u2192 ``Syn n \u2192 ``Syn n\n `id ``\u2218 y = y\n (x `\u2218 x\u2081) ``\u2218 `id = x `\u2218 x\u2081\n (x `\u2218 x\u2081) ``\u2218 (y `\u2218 y\u2081) = x `\u2218 (x\u2081 `\u2218 (y `\u2218 y\u2081))\n (x `\u2218 x\u2081) ``\u2218 `swap m = x `\u2218 (x\u2081 ``\u2218 `swap m)\n `swap m ``\u2218 y = `swap m `\u2218 y\n\n ``tail : \u2200 {n} \u2192 ``Syn n \u2192 ``Syn (suc n)\n ``tail `id = `id\n ``tail (S `\u2218 S\u2081) = ``tail S ``\u2218 ``tail S\u2081\n ``tail (`swap m) = `swap (suc m)\n\n translate : \u2200 {n} \u2192 `Syn n \u2192 ``Syn n\n translate `id = `id\n translate `swap = `swap 0\n translate (`tail S) = ``tail (translate S)\n translate (S `\u2218 S\u2081) = translate S ``\u2218 translate S\u2081\n\n ``\u2218-p : \u2200 {n}(A B : ``Syn n) \u2192 ``evalArg (A ``\u2218 B) \u2257 ``evalArg (A `\u2218 B)\n ``\u2218-p `id B x = refl\n ``\u2218-p (A `\u2218 A\u2081) `id x = refl\n ``\u2218-p (A `\u2218 A\u2081) (B `\u2218 B\u2081) x = refl\n ``\u2218-p (A `\u2218 A\u2081) (`swap m) x rewrite ``\u2218-p A\u2081 (`swap m) x = refl\n ``\u2218-p (`swap m) B x = refl\n\n ``tail-p : \u2200 {n} (S : ``Syn n) \u2192 fin-tail (``evalArg S) \u2257 ``evalArg (``tail S)\n ``tail-p `id zero = refl\n ``tail-p `id (suc x) = refl\n ``tail-p (S `\u2218 S\u2081) zero rewrite ``\u2218-p (``tail S) (``tail S\u2081) zero\n | ! ``tail-p S\u2081 zero = ``tail-p S zero\n ``tail-p (S `\u2218 S\u2081) (suc x) rewrite ``\u2218-p (``tail S) (``tail S\u2081) (suc x)\n | ! ``tail-p S\u2081 (suc x) = ``tail-p S (suc (``evalArg S\u2081 x))\n ``tail-p (`swap m) zero = refl\n ``tail-p (`swap m) (suc x) = refl\n\n `eval`` : \u2200 {n} (S : `Syn n) \u2192 `evalArg S \u2257 ``evalArg (translate S)\n `eval`` `id x = refl\n `eval`` `swap zero = refl\n `eval`` `swap (suc zero) = refl\n `eval`` `swap (suc (suc x)) = refl\n `eval`` (`tail S) zero = ``tail-p (translate S) zero\n `eval`` (`tail S) (suc x) rewrite `eval`` S x = ``tail-p (translate S) (suc x)\n `eval`` (S `\u2218 S\u2081) x rewrite ``\u2218-p (translate S) (translate S\u2081) x | ! `eval`` S\u2081 x | `eval`` S (`evalArg S\u2081 x) = refl\n\n\n data Fin-View : \u2200 {n} \u2192 Fin n \u2192 Type where\n max : \u2200 {n} \u2192 Fin-View (\u2115\u25b9Fin n)\n inject : \u2200 {n} \u2192 (i : Fin n) \u2192 Fin-View (inject\u2081 i)\n\n data Sorted {X}(XC : Cmp X) : \u2200 {l} \u2192 Vec X l \u2192 Type where\n [] : Sorted XC []\n sing : \u2200 x \u2192 Sorted XC (x \u2237 [])\n dbl-lt : \u2200 {l} x y {xs : Vec X l} \u2192 lt \u2261 XC x y \u2192 Sorted XC (y \u2237 xs) \u2192 Sorted XC (x \u2237 y \u2237 xs)\n dbl-eq : \u2200 {l} x {xs : Vec X l} \u2192 Sorted XC (x \u2237 xs) \u2192 Sorted XC (x \u2237 x \u2237 xs)\n\n opposite : Ord \u2192 Ord\n opposite lt = gt\n opposite eq = eq\n opposite gt = lt\n\n flip-RC : \u2200 {n}(x y : Fin n) \u2192 opposite (`RC x y) \u2261 `RC y x\n flip-RC zero zero = refl\n flip-RC zero (suc y) = refl\n flip-RC (suc x) zero = refl\n flip-RC (suc x) (suc y) = flip-RC x y\n\n eq=>\u2261 : \u2200 {i} (x y : Fin i) \u2192 eq \u2261 `RC x y \u2192 x \u2261 y\n eq=>\u2261 zero zero p = refl\n eq=>\u2261 zero (suc y) ()\n eq=>\u2261 (suc x) zero ()\n eq=>\u2261 (suc x) (suc y) p rewrite eq=>\u2261 x y p = refl\n\n insert-Sorted : \u2200 {n l}{V : Vec (Fin n) l}(x : Fin n) \u2192 Sorted {Fin n} `RC V \u2192 Sorted {Fin n} `RC (insert `RC x V)\n insert-Sorted x [] = sing x\n insert-Sorted x (sing x\u2081) with `RC x x\u2081 | dbl-lt {XC = `RC} x x\u2081 {[]} | eq=>\u2261 x x\u2081 | flip-RC x x\u2081\n insert-Sorted x (sing x\u2081) | lt | b | _ | _ = b refl (sing x\u2081)\n insert-Sorted x (sing x\u2081) | eq | _ | p | _ rewrite p refl = dbl-eq x\u2081 (sing x\u2081)\n insert-Sorted x (sing x\u2081) | gt | b | _ | l = dbl-lt x\u2081 x l (sing x)\n insert-Sorted x (dbl-lt y y' {xs} prf xs\u2081) with `RC x y | dbl-lt {XC = `RC} x y {y' \u2237 xs} | eq=>\u2261 x y | flip-RC x y\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | lt | b | p | l\u2081 = b refl (dbl-lt y y' prf xs\u2081)\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | eq | b | p | l\u2081 rewrite p refl = dbl-eq y (dbl-lt y y' prf xs\u2081)\n insert-Sorted x (dbl-lt y y' {xs} prf xs\u2081) | gt | b | p | l\u2081 with `RC x y' | insert-Sorted x xs\u2081\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | gt | b | p | l\u2081 | lt | xs' = dbl-lt y x l\u2081 xs'\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | gt | b | p | l\u2081 | eq | xs' = dbl-lt y x l\u2081 xs'\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | gt | b | p | l\u2081 | gt | xs' = dbl-lt y y' prf xs'\n insert-Sorted x (dbl-eq y {xs} xs\u2081) with `RC x y | inspect (`RC x) y | dbl-lt {XC = `RC} x y {y \u2237 xs} | eq=>\u2261 x y | flip-RC x y | insert-Sorted x xs\u2081\n insert-Sorted x (dbl-eq y xs\u2081) | lt | _ | b | p | l | _ = b refl (dbl-eq y xs\u2081)\n insert-Sorted x (dbl-eq y xs\u2081) | eq | _ | b | p | l | _ rewrite p refl = dbl-eq y (dbl-eq y xs\u2081)\n insert-Sorted x (dbl-eq y xs\u2081) | gt | [ prf ] | b | p | l | ss rewrite prf = dbl-eq y ss \n\n sort-Sorted : \u2200 {n l}(V : Vec (Fin n) l) \u2192 Sorted `RC (`sort `RC V)\n sort-Sorted [] = []\n sort-Sorted (x \u2237 V) = insert-Sorted x (sort-Sorted V)\n\n RC-refl : \u2200 {i}(x : Fin i) \u2192 `RC x x \u2261 eq\n RC-refl zero = refl\n RC-refl (suc x) = RC-refl x\n\n STail : \u2200 {X l}{XC : Cmp X}{xs : Vec X (suc l)} \u2192 Sorted XC xs \u2192 Sorted XC (tail xs)\n STail (sing x) = []\n STail (dbl-lt x y x\u2081 T) = T\n STail (dbl-eq x T) = T\n\n module sproof {X}(XC : Cmp X)(XC-refl : \u2200 x \u2192 XC x x \u2261 eq)\n (eq\u2261 : \u2200 x y \u2192 XC x y \u2261 eq \u2192 x \u2261 y)\n (lt-trans : \u2200 x y z \u2192 XC x y \u2261 lt \u2192 XC y z \u2261 lt \u2192 XC x z \u2261 lt)\n (XC-flip : \u2200 x y \u2192 opposite (XC x y) \u2261 XC y x)\n where\n\n open import Data.Sum\n\n _\u2264X_ : X \u2192 X \u2192 Type\n x \u2264X y = XC x y \u2261 lt \u228e XC x y \u2261 eq\n\n \u2264X-trans : \u2200 {x y z} \u2192 x \u2264X y \u2192 y \u2264X z \u2192 x \u2264X z\n \u2264X-trans (inj\u2081 x\u2081) (inj\u2081 x\u2082) = inj\u2081 (lt-trans _ _ _ x\u2081 x\u2082)\n \u2264X-trans {_}{y}{z}(inj\u2081 x\u2081) (inj\u2082 y\u2081) rewrite eq\u2261 y z y\u2081 = inj\u2081 x\u2081\n \u2264X-trans {x}{y} (inj\u2082 y\u2081) y\u2264z rewrite eq\u2261 x y y\u2081 = y\u2264z\n\n h\u2264t : \u2200 {n}{T : `Tree X (2 + n)} \u2192 Sorted XC T \u2192 head T \u2264X head (tail T)\n h\u2264t (dbl-lt x y x\u2081 ST) = inj\u2081 (! x\u2081)\n h\u2264t (dbl-eq x ST) rewrite XC-refl x = inj\u2082 refl\n\n head-p : \u2200 {n}{T : `Tree X (suc n)} i \u2192 Sorted XC T \u2192 head T \u2264X `toFun T i\n head-p {T = T} zero ST rewrite XC-refl (head T) = inj\u2082 refl\n head-p {zero} (suc ()) ST\n head-p {suc n} (suc i) ST = \u2264X-trans (h\u2264t ST) (head-p i (STail ST))\n\n toFun-p : \u2200 {n}{T : `Tree X n}{i j : Fin n} \u2192 i \u2264F j \u2192 Sorted XC T \u2192 `toFun T i \u2264X `toFun T j\n toFun-p {j = j} z\u2264i ST = head-p j ST\n toFun-p (s\u2264s i\u2264Fj) ST = toFun-p i\u2264Fj (STail ST)\n\n sort-proof : \u2200 {i}{T : `Tree X i} \u2192 Sorted XC T \u2192 Is-Mono `RC XC (`toFun T)\n sort-proof {T = T} T\u2081 zero zero rewrite XC-refl (head T) = _\n sort-proof T\u2081 zero (suc y) with toFun-p (z\u2264i {i = suc y}) T\u2081\n sort-proof T zero (suc y) | inj\u2081 x rewrite x = _\n sort-proof T zero (suc y) | inj\u2082 y\u2081 rewrite y\u2081 = _\n sort-proof {T = T} T\u2081 (suc x) zero with toFun-p (z\u2264i {i = suc x}) T\u2081 | XC-flip (head T) (`toFun (tail T) x)\n sort-proof T (suc x) zero | inj\u2081 x\u2081 | l rewrite x\u2081 | ! l = _\n sort-proof T (suc x) zero | inj\u2082 y | l rewrite y | ! l = _\n sort-proof T\u2081 (suc x) (suc y) = sort-proof (STail T\u2081) x y\n\n lt-trans-RC : \u2200 {i} (x y z : Fin i) \u2192 `RC x y \u2261 lt \u2192 `RC y z \u2261 lt \u2192 `RC x z \u2261 lt\n lt-trans-RC zero zero zero x\u2261 x\u2081 y\u2081 (! x\u2082)) lt-trans-RC flip-RC (sort-Sorted T) x y\n\n move-to-RC : \u2200 {n}{x y : Fin n} \u2192 x \u2264F y \u2192 `RC x y \u2261 lt \u228e `RC x y \u2261 eq\n move-to-RC {y = zero} z\u2264i = inj\u2082 refl\n move-to-RC {y = suc y} z\u2264i = inj\u2081 refl\n move-to-RC (s\u2264s x\u2264Fy) = move-to-RC x\u2264Fy\n\n move-from-RC : \u2200 {n}(x y : Fin n) \u2192 lt \u2261 `RC x y \u228e eq \u2261 `RC x y \u2192 x \u2264F y\n move-from-RC zero zero prf = z\u2264i\n move-from-RC zero (suc y) prf = z\u2264i\n move-from-RC (suc x) zero (inj\u2081 ())\n move-from-RC (suc x) zero (inj\u2082 ())\n move-from-RC (suc x) (suc y) prf = s\u2264s (move-from-RC x y prf)\n\n module toNatRC n (f : Endo (Fin (suc n)))(f-inj : Is-Inj f)(f-mono : Is-Mono `RC `RC f) where\n proper-mono : \u2200 {x y} \u2192 x \u2264F y \u2192 f x \u2264F f y\n proper-mono {x} {y} x\u2264Fy with `RC x y | `RC (f x) (f y) | move-to-RC x\u2264Fy | f-mono x y | move-from-RC (f x) (f y)\n proper-mono x\u2264Fy | .lt | lt | inj\u2081 refl | r4 | r5 = r5 (inj\u2081 refl)\n proper-mono x\u2264Fy | .lt | eq | inj\u2081 refl | r4 | r5 = r5 (inj\u2082 refl)\n proper-mono x\u2264Fy | .lt | gt | inj\u2081 refl | () | r5\n proper-mono x\u2264Fy | .eq | lt | inj\u2082 refl | () | r5\n proper-mono x\u2264Fy | .eq | eq | inj\u2082 refl | r4 | r5 = r5 (inj\u2082 refl)\n proper-mono x\u2264Fy | .eq | gt | inj\u2082 refl | () | r5\n open From-mono-inj f f-inj proper-mono public\n\n fin-view : \u2200 {n} \u2192 (i : Fin (suc n)) \u2192 Fin-View i\n fin-view {zero} zero = max\n fin-view {zero} (suc ())\n fin-view {suc n} zero = inject _\n fin-view {suc n} (suc i) with fin-view i\n fin-view {suc n} (suc .(\u2115\u25b9Fin n)) | max = max\n fin-view {suc n} (suc .(inject\u2081 i)) | inject i = inject _\n\n absurd : {X : Type} \u2192 .\u22a5 \u2192 X\n absurd ()\n\n drop\u2081 : \u2200 {n} \u2192 (i : Fin (suc n)) \u2192 .(i \u2262 \u2115\u25b9Fin n) \u2192 Fin n\n drop\u2081 i neq with fin-view i\n drop\u2081 {n} .(\u2115\u25b9Fin n) neq | max = absurd (neq refl)\n drop\u2081 .(inject\u2081 i) neq | inject i = i\n\n drop\u2081\u2192inject\u2081 : \u2200 {n}(i : Fin (suc n))(j : Fin n).(p : i \u2262 \u2115\u25b9Fin n) \u2192 drop\u2081 i p \u2261 j \u2192 i \u2261 inject\u2081 j\n drop\u2081\u2192inject\u2081 i j p q with fin-view i\n drop\u2081\u2192inject\u2081 {n} .(\u2115\u25b9Fin n) j p q | max = absurd (p refl)\n drop\u2081\u2192inject\u2081 .(inject\u2081 i) j p q | inject i = ap inject\u2081 q\n\n\n `mono-inj\u2192id : \u2200{i}(f : Endo (`Rep i)) \u2192 Is-Inj f \u2192 Is-Mono `RC `RC f \u2192 f \u2257 id\n `mono-inj\u2192id {zero} = \u03bb f x x\u2081 ()\n `mono-inj\u2192id {suc i} = toNatRC.f\u2257id i\n\n\n interface : Interface\n interface = record \n { Ix = `Ix\n ; Rep = `Rep\n ; Syn = `Syn\n ; Tree = `Tree\n ; fromFun = `fromFun\n ; toFun = `toFun\n ; toFun\u2218fromFun = `toFun\u2218fromFun\n ; evalArg = `evalArg\n ; evalTree = `evalTree\n ; eval-proof = `eval-proof\n ; inv = `inv\n ; inv-proof = `inv-proof\n ; RC = `RC\n ; sort = `sort\n ; sort-syn = `sort-syn\n ; sort-proof = `sort-proof\n ; sort-mono = `sort-mono\n ; mono-inj\u2192id = `mono-inj\u2192id\n }\n\n count : \u2200 {n} \u2192 (Fin n \u2192 \u2115) \u2192 \u2115\n count {n} f = sum (tabulate f)\n\n count-ext : \u2200 {n} \u2192 (f g : Fin n \u2192 \u2115) \u2192 f \u2257 g \u2192 count f \u2261 count g\n count-ext {zero} f g f\u2257g = refl\n count-ext {suc n} f g f\u2257g rewrite f\u2257g zero | count-ext (f \u2218 suc) (g \u2218 suc) (f\u2257g \u2218 suc) = refl\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Fin n \u2192 \ud835\udfda) \u2192 \u2115\n #\u27e8 f \u27e9 = count (\ud835\udfda\u25b9\u2115 \u2218 f)\n\n #-ext : \u2200 {n} \u2192 (f g : Fin n \u2192 \ud835\udfda) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-ext f g f\u2257g = count-ext (\ud835\udfda\u25b9\u2115 \u2218 f) (\ud835\udfda\u25b9\u2115 \u2218 g) (ap \ud835\udfda\u25b9\u2115 \u2218 f\u2257g)\n\n com-assoc : \u2200 x y z \u2192 x + (y + z) \u2261 y + (x + z)\n com-assoc x y z rewrite \n ! \u2115\u00b0.+-assoc x y z |\n \u2115\u00b0.+-comm x y |\n \u2115\u00b0.+-assoc y x z = refl\n \n syn-pres : \u2200 {n}(f : Fin n \u2192 \u2115)(S : `Syn n)\n \u2192 count f \u2261 count (f \u2218 `evalArg S)\n syn-pres f `id = refl\n syn-pres f `swap = com-assoc (f zero) (f (suc zero)) (count (\u03bb i \u2192 f (suc (suc i))))\n syn-pres f (`tail S) rewrite syn-pres (f \u2218 suc) S = refl\n syn-pres f (S `\u2218 S\u2081) rewrite syn-pres f S = syn-pres (f \u2218 `evalArg S) S\u2081\n\n count-perm : \u2200 {n}(f : Fin n \u2192 \u2115)(p : Endo (Fin n)) \u2192 Is-Inj p\n \u2192 count f \u2261 count (f \u2218 p)\n count-perm f p p-inj = trans (syn-pres f (sort-bij p)) (count-ext _ _ f\u2218eval\u2257f\u2218p)\n where\n open abs interface\n f\u2218eval\u2257f\u2218p : f \u2218 `evalArg (sort-bij p) \u2257 f \u2218 p\n f\u2218eval\u2257f\u2218p x rewrite thm p p-inj x = refl\n\n\n #-perm : \u2200 {n}(f : Fin n \u2192 \ud835\udfda)(p : Endo (Fin n)) \u2192 Is-Inj p\n \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 p \u27e9\n #-perm f p p-inj = count-perm (\ud835\udfda\u25b9\u2115 \u2218 f) p p-inj\n\n test : `Syn 8\n test = abs.sort-bij interface (\u03bb x \u2192 `evalArg (`tail `swap) x)\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"6538c1e3a868c0dc095139fc2243a8683b77c995","subject":"Remove dead code of Data.NatBag.Properties Checkpoint: Recast property proofs using set theoretic methods (#55)","message":"Remove dead code of Data.NatBag.Properties\nCheckpoint: Recast property proofs using set theoretic methods (#55)\n\nOld-commit-hash: 04b32e5d92152cdab089a25c0d8f9f4c39279fb0\n","repos":"inc-lc\/ilc-agda","old_file":"Data\/NatBag\/Properties.agda","new_file":"Data\/NatBag\/Properties.agda","new_contents":"module Data.NatBag.Properties where\n\nimport Data.Nat as \u2115\nopen import Relation.Binary.PropositionalEquality\nopen import Data.NatBag\nopen import Data.Integer\nopen import Data.Sum hiding (map)\nopen import Data.Product hiding (map)\n\n-- This import is too slow.\n-- It causes Agda 2.3.2 to use so much memory that cai's\n-- computer with 4GB RAM begins to thresh.\n--\n-- open import Data.Integer.Properties using (n\u2296n\u22610)\nn\u2296n\u22610 : \u2200 n \u2192 n \u2296 n \u2261 + 0\nn\u2296n\u22610 \u2115.zero = refl\nn\u2296n\u22610 (\u2115.suc n) = n\u2296n\u22610 n\n\n\n----------------\n-- Statements --\n----------------\n\n\nb\\\\b=\u2205 : \u2200 {b : Bag} \u2192 b \\\\ b \u2261 empty\n\n\u2205++b=b : \u2200 {b : Bag} \u2192 empty ++ b \u2261 b\n\nb\\\\\u2205=b : \u2200 {b : Bag} \u2192 b \\\\ empty \u2261 b\n\n\u2205\\\\b=-b : \u2200 {b : Bag} \u2192 empty \\\\ b \u2261 map\u2082 -_ b\n\nb++[d\\\\b]=d : \u2200 {b d : Bag} \u2192 b ++ (d \\\\ b) \u2261 d\n\n\n------------\n-- Proofs --\n------------\n\ni-i=0 : \u2200 {i : \u2124} \u2192 (i - i) \u2261 (+ 0)\ni-i=0 {+ \u2115.zero} = refl\ni-i=0 {+ \u2115.suc n} = n\u2296n\u22610 n\ni-i=0 { -[1+ n ]} = n\u2296n\u22610 n\n\n-- Debug tool\n-- Lets you try out inhabitance of any type anywhere\nabsurd! : \u2200 {B C : Set} \u2192 0 \u2261 1 \u2192 B \u2192 {x : B} \u2192 C\nabsurd! ()\n\n-- Specialized absurdity needed to type check.\n-- \u03bb () hasn't enough information sometimes.\nabsurd : Nonzero (+ 0) \u2192 \u2200 {A : Set} \u2192 A\nabsurd ()\n\n-- Here to please the termination checker.\nneb\\\\neb=\u2205 : \u2200 {neb : NonemptyBag} \u2192 zipNonempty _-_ neb neb \u2261 empty\nneb\\\\neb=\u2205 {singleton i i\u22600} with nonzero? (i - i)\n... | inj\u2081 _ = refl\n... | inj\u2082 0\u22600 rewrite i-i=0 {i} = absurd 0\u22600\nneb\\\\neb=\u2205 {i \u2237 neb} with nonzero? (i - i)\n... | inj\u2081 _ rewrite neb\\\\neb=\u2205 {neb} = refl\n... | inj\u2082 0\u22600 rewrite neb\\\\neb=\u2205 {neb} | i-i=0 {i} = absurd 0\u22600\n\nb\\\\b=\u2205 {inj\u2081 \u2205} = refl\nb\\\\b=\u2205 {inj\u2082 neb} = neb\\\\neb=\u2205 {neb}\n\n\u2205++b=b {b} = {!!}\n\nb\\\\\u2205=b {b} = {!!}\n\n\u2205\\\\b=-b {b} = {!!}\n\nnegate : \u2200 {i} \u2192 Nonzero i \u2192 Nonzero (- i)\nnegate (negative n) = positive n\nnegate (positive n) = negative n\n\nnegate\u2032 : \u2200 {i} \u2192 (i\u22600 : Nonzero i) \u2192 Nonzero (+ 0 - i)\nnegate\u2032 { -[1+ n ]} (negative .n) = positive n\nnegate\u2032 {+ .(\u2115.suc n)} (positive n) = negative n\n\n0-i=-i : \u2200 {i} \u2192 + 0 - i \u2261 - i\n0-i=-i { -[1+ n ]} = refl -- cases are split, for arguments to\n0-i=-i {+ \u2115.zero} = refl -- refl are different.\n0-i=-i {+ \u2115.suc n} = refl\n\nrewrite-singleton :\n \u2200 (i : \u2124) (0-i\u22600 : Nonzero (+ 0 - i)) ( -i\u22600 : Nonzero (- i)) \u2192\n singleton (+ 0 - i) 0-i\u22600 \u2261 singleton (- i) -i\u22600\nrewrite-singleton (+ \u2115.zero) () ()\nrewrite-singleton (+ \u2115.suc n) (negative .n) (negative .n) = refl\nrewrite-singleton ( -[1+ n ]) (positive .n) (positive .n) = refl\n\nnegateSingleton : \u2200 {i i\u22600} \u2192\n mapNonempty\u2082 (\u03bb j \u2192 + 0 - j) (singleton i i\u22600)\n \u2261\n inj\u2082 (singleton (- i) (negate i\u22600))\n\n-- Fun fact:\n-- Pattern-match on implicit parameters in the first two\n-- cases results in rejection by Agda.\nnegateSingleton {i} {i\u22600} with nonzero? i | nonzero? (+ 0 - i)\nnegateSingleton | inj\u2082 (negative n) | inj\u2081 ()\nnegateSingleton | inj\u2082 (positive n) | inj\u2081 ()\nnegateSingleton {_} {i\u22600} | inj\u2081 i=0 | _ rewrite i=0 = absurd i\u22600\nnegateSingleton {i} {i\u22600} | inj\u2082 _ | inj\u2082 0-i\u22600 =\n begin -- Reasoning done in 1 step. Included for clarity only.\n inj\u2082 (singleton (+ 0 + - i) 0-i\u22600)\n \u2261\u27e8 cong inj\u2082 (rewrite-singleton i 0-i\u22600 (negate i\u22600)) \u27e9\n inj\u2082 (singleton (- i) (negate i\u22600))\n \u220e where open \u2261-Reasoning\n\nabsurd[i-i\u22600] : \u2200 {i} \u2192 Nonzero (i - i) \u2192 \u2200 {A : Set} \u2192 A\nabsurd[i-i\u22600] {+ \u2115.zero} = absurd\nabsurd[i-i\u22600] {+ \u2115.suc n} = absurd[i-i\u22600] { -[1+ n ]}\nabsurd[i-i\u22600] { -[1+ \u2115.zero ]} = absurd\nabsurd[i-i\u22600] { -[1+ \u2115.suc n ]} = absurd[i-i\u22600] { -[1+ n ]}\n\nannihilate : \u2200 {i i\u22600} \u2192\n inj\u2082 (singleton i i\u22600) ++ inj\u2082 (singleton (- i) (negate i\u22600)) \u2261 inj\u2081 \u2205\nannihilate {i} with nonzero? (i - i)\n... | inj\u2081 i-i=0 = \u03bb {i\u22600} \u2192 refl\n... | inj\u2082 i-i\u22600 = absurd[i-i\u22600] {i} i-i\u22600\n\nb++[\u2205\\\\b]=\u2205 : \u2200 {b} \u2192 b ++ (empty \\\\ b) \u2261 empty\nb++[\u2205\\\\b]=\u2205 {inj\u2081 \u2205} = refl\nb++[\u2205\\\\b]=\u2205 {inj\u2082 (singleton i i\u22600)} =\n begin\n inj\u2082 (singleton i i\u22600) ++\n mapNonempty\u2082 (\u03bb j \u2192 + 0 - j) (singleton i i\u22600)\n \u2261\u27e8 cong\u2082 _++_ {x = inj\u2082 (singleton i i\u22600)} refl\n (negateSingleton {i} {i\u22600}) \u27e9\n inj\u2082 (singleton i i\u22600) ++ inj\u2082 (singleton (- i) (negate i\u22600))\n \u2261\u27e8 annihilate {i} {i\u22600} \u27e9\n inj\u2081 \u2205\n \u220e where open \u2261-Reasoning\nb++[\u2205\\\\b]=\u2205 {inj\u2082 (i \u2237 y)} = {!!}\n\nb++[d\\\\b]=d {inj\u2081 \u2205} {d} rewrite b\\\\\u2205=b {d} | \u2205++b=b {d} = refl\nb++[d\\\\b]=d {b} {inj\u2081 \u2205} = b++[\u2205\\\\b]=\u2205 {b}\nb++[d\\\\b]=d {inj\u2082 b} {inj\u2082 d} = {!!}\n\n","old_contents":"module Data.NatBag.Properties where\n\nimport Data.Nat as \u2115\nopen import Relation.Binary.PropositionalEquality\nopen import Data.NatBag\nopen import Data.Integer\nopen import Data.Sum hiding (map)\nopen import Data.Product hiding (map)\n\n-- This import is too slow.\n-- It causes Agda 2.3.2 to use so much memory that cai's\n-- computer with 4GB RAM begins to thresh.\n--\n-- open import Data.Integer.Properties using (n\u2296n\u22610)\nn\u2296n\u22610 : \u2200 n \u2192 n \u2296 n \u2261 + 0\nn\u2296n\u22610 \u2115.zero = refl\nn\u2296n\u22610 (\u2115.suc n) = n\u2296n\u22610 n\n\n\n----------------\n-- Statements --\n----------------\n\n\nb\\\\b=\u2205 : \u2200 {b : Bag} \u2192 b \\\\ b \u2261 empty\n\n\u2205++b=b : \u2200 {b : Bag} \u2192 empty ++ b \u2261 b\n\nb\\\\\u2205=b : \u2200 {b : Bag} \u2192 b \\\\ empty \u2261 b\n\n\u2205\\\\b=-b : \u2200 {b : Bag} \u2192 empty \\\\ b \u2261 map\u2082 -_ b\n\n-- ++d=\\\\-d : \u2200 {b d : Bag} \u2192 b ++ d \u2261 b \\\\ map\u2082 -_ d\n\nb++[d\\\\b]=d : \u2200 {b d : Bag} \u2192 b ++ (d \\\\ b) \u2261 d\n\n\n------------\n-- Proofs --\n------------\n\ni-i=0 : \u2200 {i : \u2124} \u2192 (i - i) \u2261 (+ 0)\ni-i=0 {+ \u2115.zero} = refl\ni-i=0 {+ \u2115.suc n} = n\u2296n\u22610 n\ni-i=0 { -[1+ n ]} = n\u2296n\u22610 n\n\n-- Debug tool\n-- Lets you try out inhabitance of any type anywhere\nabsurd! : \u2200 {B C : Set} \u2192 0 \u2261 1 \u2192 B \u2192 {x : B} \u2192 C\nabsurd! ()\n\n-- Specialized absurdity needed to type check.\n-- \u03bb () hasn't enough information sometimes.\nabsurd : Nonzero (+ 0) \u2192 \u2200 {A : Set} \u2192 A\nabsurd ()\n\n-- Here to please the termination checker.\nneb\\\\neb=\u2205 : \u2200 {neb : NonemptyBag} \u2192 zipNonempty _-_ neb neb \u2261 empty\nneb\\\\neb=\u2205 {singleton i i\u22600} with nonzero? (i - i)\n... | inj\u2081 _ = refl\n... | inj\u2082 0\u22600 rewrite i-i=0 {i} = absurd 0\u22600\nneb\\\\neb=\u2205 {i \u2237 neb} with nonzero? (i - i)\n... | inj\u2081 _ rewrite neb\\\\neb=\u2205 {neb} = refl\n... | inj\u2082 0\u22600 rewrite neb\\\\neb=\u2205 {neb} | i-i=0 {i} = absurd 0\u22600\n\n{-\n++d=\\\\-d {inj\u2081 \u2205} {inj\u2081 \u2205} = refl\n++d=\\\\-d {inj\u2081 \u2205} {inj\u2082 (i \u2237 y)} = {!!}\n++d=\\\\-d {inj\u2082 y} {d} = {!!}\n++d=\\\\-d {inj\u2081 \u2205} {inj\u2082 (singleton i i\u22600)}\n rewrite \u2205++b=b {inj\u2082 (singleton i i\u22600)}\n with nonzero? i | nonzero? (+ 0 - i)\n... | inj\u2082 _ | inj\u2082 0-i\u22600 =\n begin\n {!!}\n \u2261\u27e8 {!!} \u27e9\n {!inj\u2082 (singleton (- i) ?)!}\n \u220e where open \u2261-Reasoning\n... | inj\u2081 i=0 | _ rewrite i=0 = absurd i\u22600\n++d=\\\\-d {inj\u2081 \u2205} {inj\u2082 (singleton (+ 0) i\u22600)} | _ | _ = absurd i\u22600\n++d=\\\\-d {inj\u2081 \u2205} {inj\u2082 (singleton (+ (\u2115.suc n)) i\u22600)}\n | inj\u2082 (positive .n) | inj\u2081 ()\n++d=\\\\-d {inj\u2081 \u2205} {inj\u2082 (singleton -[1+ n ] i\u22600)}\n | inj\u2082 (negative .n) | inj\u2081 ()\n-}\n\nb\\\\b=\u2205 {inj\u2081 \u2205} = refl\nb\\\\b=\u2205 {inj\u2082 neb} = neb\\\\neb=\u2205 {neb}\n\n\u2205++b=b {b} = {!!}\n\nb\\\\\u2205=b {b} = {!!}\n\n\u2205\\\\b=-b {b} = {!!}\n\nnegate : \u2200 {i} \u2192 Nonzero i \u2192 Nonzero (- i)\nnegate (negative n) = positive n\nnegate (positive n) = negative n\n\nnegate\u2032 : \u2200 {i} \u2192 (i\u22600 : Nonzero i) \u2192 Nonzero (+ 0 - i)\nnegate\u2032 { -[1+ n ]} (negative .n) = positive n\nnegate\u2032 {+ .(\u2115.suc n)} (positive n) = negative n\n\n0-i=-i : \u2200 {i} \u2192 + 0 - i \u2261 - i\n0-i=-i { -[1+ n ]} = refl -- cases are split, for arguments to\n0-i=-i {+ \u2115.zero} = refl -- refl are different.\n0-i=-i {+ \u2115.suc n} = refl\n\nrewrite-singleton :\n \u2200 (i : \u2124) (0-i\u22600 : Nonzero (+ 0 - i)) ( -i\u22600 : Nonzero (- i)) \u2192\n singleton (+ 0 - i) 0-i\u22600 \u2261 singleton (- i) -i\u22600\nrewrite-singleton (+ \u2115.zero) () ()\nrewrite-singleton (+ \u2115.suc n) (negative .n) (negative .n) = refl\nrewrite-singleton ( -[1+ n ]) (positive .n) (positive .n) = refl\n\nnegateSingleton : \u2200 {i i\u22600} \u2192\n mapNonempty\u2082 (\u03bb j \u2192 + 0 - j) (singleton i i\u22600)\n \u2261\n inj\u2082 (singleton (- i) (negate i\u22600))\n\n-- Fun fact:\n-- Pattern-match on implicit parameters in the first two\n-- cases results in rejection by Agda.\nnegateSingleton {i} {i\u22600} with nonzero? i | nonzero? (+ 0 - i)\nnegateSingleton | inj\u2082 (negative n) | inj\u2081 ()\nnegateSingleton | inj\u2082 (positive n) | inj\u2081 ()\nnegateSingleton {_} {i\u22600} | inj\u2081 i=0 | _ rewrite i=0 = absurd i\u22600\nnegateSingleton {i} {i\u22600} | inj\u2082 _ | inj\u2082 0-i\u22600 =\n begin -- Reasoning done in 1 step. Included for clarity only.\n inj\u2082 (singleton (+ 0 + - i) 0-i\u22600)\n \u2261\u27e8 cong inj\u2082 (rewrite-singleton i 0-i\u22600 (negate i\u22600)) \u27e9\n inj\u2082 (singleton (- i) (negate i\u22600))\n \u220e where open \u2261-Reasoning\n\nabsurd[i-i\u22600] : \u2200 {i} \u2192 Nonzero (i - i) \u2192 \u2200 {A : Set} \u2192 A\nabsurd[i-i\u22600] {+ \u2115.zero} = absurd\nabsurd[i-i\u22600] {+ \u2115.suc n} = absurd[i-i\u22600] { -[1+ n ]}\nabsurd[i-i\u22600] { -[1+ \u2115.zero ]} = absurd\nabsurd[i-i\u22600] { -[1+ \u2115.suc n ]} = absurd[i-i\u22600] { -[1+ n ]}\n\nannihilate : \u2200 {i i\u22600} \u2192\n inj\u2082 (singleton i i\u22600) ++ inj\u2082 (singleton (- i) (negate i\u22600)) \u2261 inj\u2081 \u2205\nannihilate {i} with nonzero? (i - i)\n... | inj\u2081 i-i=0 = \u03bb {i\u22600} \u2192 refl\n... | inj\u2082 i-i\u22600 = absurd[i-i\u22600] {i} i-i\u22600\n\n{-\nleft-is-not-right : \u2200 {A B : Set} {a : A} {b : B} \u2192\n inj\u2081 a \u2261 inj\u2082 b \u2192 \u2200 {X : Set} \u2192 X\nleft-is-not-right = \u03bb {A} {B} {a} {b} \u2192 \u03bb ()\n\nnever-both : \u2200 {A B : Set} {sum : A \u228e B} {a b} \u2192\n sum \u2261 inj\u2081 a \u2192 sum \u2261 inj\u2082 b \u2192 \u2200 {X : Set} \u2192 X\nnever-both s=a s=b = left-is-not-right (trans (sym s=a) s=b)\n\nempty-bag? : \u2200 (b : Bag) \u2192 (b \u2261 inj\u2081 \u2205) \u228e \u03a3 NonemptyBag (\u03bb neb \u2192 b \u2261 inj\u2082 neb)\nempty-bag? (inj\u2081 \u2205) = inj\u2081 refl\nempty-bag? (inj\u2082 neb) = inj\u2082 (neb , refl)\n-}\n\nb++[\u2205\\\\b]=\u2205 : \u2200 {b} \u2192 b ++ (empty \\\\ b) \u2261 empty\nb++[\u2205\\\\b]=\u2205 {inj\u2081 \u2205} = refl\nb++[\u2205\\\\b]=\u2205 {inj\u2082 (singleton i i\u22600)} =\n begin\n inj\u2082 (singleton i i\u22600) ++\n mapNonempty\u2082 (\u03bb j \u2192 + 0 - j) (singleton i i\u22600)\n \u2261\u27e8 cong\u2082 _++_ {x = inj\u2082 (singleton i i\u22600)} refl\n (negateSingleton {i} {i\u22600}) \u27e9\n inj\u2082 (singleton i i\u22600) ++ inj\u2082 (singleton (- i) (negate i\u22600))\n \u2261\u27e8 annihilate {i} {i\u22600} \u27e9\n inj\u2081 \u2205\n \u220e where open \u2261-Reasoning\nb++[\u2205\\\\b]=\u2205 {inj\u2082 (i \u2237 y)} = {!!}\n\nb++[d\\\\b]=d {inj\u2081 \u2205} {d} rewrite b\\\\\u2205=b {d} | \u2205++b=b {d} = refl\nb++[d\\\\b]=d {b} {inj\u2081 \u2205} = b++[\u2205\\\\b]=\u2205 {b}\nb++[d\\\\b]=d {inj\u2082 b} {inj\u2082 d} = {!!}\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"39ff610ece1a0f53d91047ee69dba0f42e646718","subject":"simplify","message":"simplify\n","repos":"crypto-agda\/crypto-agda","old_file":"dist-props.agda","new_file":"dist-props.agda","new_contents":"module dist-props where\n\nopen import Data.Bool.NP\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Product\n\nopen import Function\n\nopen import Search.Type\nopen import Search.Sum\n\nopen import Search.Searchable.Sum\nopen import Search.Searchable.Product\n\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule GameFlipping (R : Set)(sum : Sum R)(sum-ind : SumInd sum)(X Y : R \u2192 Bool) where\n\n R' = Bool \u00d7 R\n sum' : Sum R'\n sum' = searchBit _+_ \u00d7-sum sum\n\n open FromSum sum' renaming (count to count')\n open FromSumInd sum-ind\n\n _==\u1d2e_ : Bool \u2192 Bool \u2192 Bool\n true ==\u1d2e y = y\n false ==\u1d2e y = not y\n\n G : R' \u2192 Bool\n G (b , r) = b == (if b then X r else Y r)\n\n 1\/2 : R' \u2192 Bool\n 1\/2 = proj\u2081\n\n -- TODO use the library\n lemma : \u2200 X \u2192 sum (const 1) \u2261 count (not \u2218 X) + count X\n lemma X = sum-ind P (\u03bb {a}{b} \u2192 part1 {a}{b}) part2\n where\n # = FromSum.count\n\n P = \u03bb s \u2192 s (const 1) \u2261 # s (not \u2218 X) + # s X\n\n part1 : \u2200 {s\u2080 s\u2081} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb f \u2192 s\u2080 f + s\u2081 f)\n part1 {s\u2080} {s\u2081} Ps\u2080 Ps\u2081 rewrite Ps\u2080 | Ps\u2081 = +-interchange (# s\u2080 (not \u2218 X)) (# s\u2080 X) (# s\u2081 (not \u2218 X)) (# s\u2081 X)\n\n part2 : \u2200 x \u2192 P (\u03bb f \u2192 f x)\n part2 x with X x\n part2 x | true = refl\n part2 x | false = refl\n\n\n thm : dist (count' G) (count' 1\/2) \u2261 dist (count X) (count Y)\n thm = dist (count' G) (count' 1\/2)\n \u2261\u27e8 cong (dist (count' G)) helper \u27e9\n dist (count' G) (count (not \u2218 Y) + count Y)\n \u2261\u27e8 refl \u27e9 -- count' definition\n dist (count (_==_ false \u2218 Y) + count (_==_ true \u2218 X)) (count (not \u2218 Y) + count Y)\n \u2261\u27e8 {!refl!} \u27e9 -- count' definition\n dist (count (not \u2218 Y) + count X) (count (not \u2218 Y) + count Y)\n \u2261\u27e8 dist-x+ (count (not \u2218 Y)) (count X) (count Y) \u27e9\n dist (count X) (count Y)\n \u220e\n where\n open \u2261-Reasoning\n\n helper = count' 1\/2\n \u2261\u27e8 refl \u27e9\n sum (const 0) + sum (const 1)\n \u2261\u27e8 cong (\u03bb p \u2192 p + sum (const 1)) sum-zero \u27e9\n sum (const 1)\n \u2261\u27e8 lemma Y \u27e9\n count (not \u2218 Y) + count Y\n \u220e\n","old_contents":"module dist-props where\n\nopen import Data.Bool\nopen import Data.Nat.NP\nopen import Data.Product\n\nopen import Function\n\nopen import Search.Type\nopen import Search.Sum\n\nopen import Search.Searchable.Sum\nopen import Search.Searchable.Product\n\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule GameFlipping (R : Set)(sum : Sum R)(sum-ind : SumInd sum)(X Y : R \u2192 Bool) where\n\n R' = Bool \u00d7 R\n sum' : Sum R'\n sum' = searchBit _+_ \u00d7-sum sum\n\n open FromSum sum' renaming (count to count')\n open FromSumInd sum-ind\n\n _==\u1d2e_ : Bool \u2192 Bool \u2192 Bool\n true ==\u1d2e y = y\n false ==\u1d2e y = not y\n\n G : R' \u2192 Bool\n G (b , r) = b ==\u1d2e (if b then X r else Y r)\n\n 1\/2 : R' \u2192 Bool\n 1\/2 = proj\u2081\n\n lemma : \u2200 X \u2192 sum (const 1) \u2261 count (not \u2218 X) + count X\n lemma X = sum-ind P (\u03bb {a}{b} \u2192 part1 {a}{b}) part2\n where\n # = FromSum.count\n \n P = \u03bb s \u2192 s (const 1) \u2261 # s (not \u2218 X) + # s X\n \n part1 : \u2200 {s\u2080 s\u2081} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb f \u2192 s\u2080 f + s\u2081 f)\n part1 {s\u2080} {s\u2081} Ps\u2080 Ps\u2081 rewrite Ps\u2080 | Ps\u2081 = +-interchange (# s\u2080 (not \u2218 X)) (# s\u2080 X) (# s\u2081 (not \u2218 X)) (# s\u2081 X) \n\n part2 : \u2200 x \u2192 P (\u03bb f \u2192 f x)\n part2 x with X x\n part2 x | true = refl\n part2 x | false = refl\n \n\n thm : dist (count' G) (count' 1\/2) \u2261 dist (count X) (count Y)\n thm = dist (count' G) (count' 1\/2)\n \u2261\u27e8 refl \u27e9\n dist (count (not \u2218 Y) + count X) (sum (const 0) + sum (const 1))\n \u2261\u27e8 cong (\u03bb p \u2192 dist (count (not \u2218 Y) + count X) (p + sum (const 1))) sum-zero \u27e9\n dist (count (not \u2218 Y) + count X) (sum (const 1))\n \u2261\u27e8 cong (dist (count (not \u2218 Y) + count X)) (lemma Y) \u27e9\n dist (count (not \u2218 Y) + count X) (count (not \u2218 Y) + count Y)\n \u2261\u27e8 dist-x+ (count (not \u2218 Y)) (count X) (count Y) \u27e9\n dist (count X) (count Y)\n \u220e\n where\n open \u2261-Reasoning\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"ece000bf8e7a13f345d059fdfc157ac01316ae5b","subject":"Cosmetic changes.","message":"Cosmetic changes.\n\nIgnore-this: 87092ca63c87c904873052fce7315bf4\n\ndarcs-hash:20110301055010-3bd4e-486c9856c3c8f961ed8eb13de29c7833577f9bc4.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Data\/Nat\/Induction\/Acc\/WellFoundedInduction.agda","new_file":"src\/FOTC\/Data\/Nat\/Induction\/Acc\/WellFoundedInduction.agda","new_contents":"----------------------------------------------------------------------------\n-- Well-founded induction on the natural numbers\n----------------------------------------------------------------------------\n\nmodule FOTC.Data.Nat.Induction.Acc.WellFoundedInduction where\n\nopen import FOTC.Base\n\nopen import Common.Function\n\nopen import FOTC.Data.Nat.Induction.Acc.WellFounded\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Type\n\n------------------------------------------------------------------------------\n-- The relation LT is well-founded.\nmodule WF-LT\u2081\n ( x<0\u2192\u22a5 : \u2200 {n} \u2192 N n \u2192 \u00ac (LT n zero)\n ; Sx100 : 100 + 11 > 100\n 100+11>100 = s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n (s\u2264s\n z\u2264n))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))\n\nmodule Primitives where\n\n open import Data.Bool\n open import Data.Nat hiding ( _<_ )\n open import Relation.Binary.PropositionalEquality\n\n primitive primNatLess : \u2115 \u2192 \u2115 \u2192 Bool\n\n _<_ : \u2115 \u2192 \u2115 \u2192 Bool\n _<_ = primNatLess\n\n 100<100+11 : 100 < (100 + 11) \u2261 true\n 100<100+11 = refl\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d9f39e3e94f6a92b25e057a7a737c8c19e8e86d7","subject":"IDesc model: implement lists","message":"IDesc model: implement lists","repos":"brixen\/Epigram,kwangkim\/pigment,kwangkim\/pigment,kwangkim\/pigment","old_file":"models\/IDesc.agda","new_file":"models\/IDesc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (l : Level)(I : Set l) : Set (suc l) where\n var : I -> IDesc l I\n const : Set l -> IDesc l I\n prod : IDesc l I -> IDesc l I -> IDesc l I\n sigma : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n pi : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\ndesc _ I (var i) P = P i\ndesc _ I (const X) P = X\ndesc x I (prod D D') P = desc x I D P * desc x I D' P\ndesc x I (sigma S T) P = Sigma S (\\s -> desc x I (T s) P)\ndesc x I (pi S T) P = (s : S) -> desc x I (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l where\n con : desc l I (R i) (\\j -> IMu l I R j) -> IMu l I R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : (l : Level)(I : Set l)(D : IDesc l I)(P : I -> Set l) -> desc l I D P -> IDesc l (Sigma I P)\nbox _ I (var i) P x = var (i , x)\nbox _ I (const X) P x = const X\nbox x I (prod D D') P (d , d') = prod (box x I D P d) (box x I D' P d')\nbox x I (sigma S T) P (a , b) = box x I (T a) P b\nbox x I (pi S T) P f = pi S (\\s -> box x I (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (l : Level)\n (I : Set l)\n (R : I -> IDesc l I)\n (P : Sigma I (IMu l I R) -> Set l)\n (m : (i : I)\n (xs : desc l I (R i) (IMu l I R))\n (hs : desc l (Sigma I (IMu l I R)) (box l I (R i) (IMu l I R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu l I R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc l I) -> \n (xs : desc l I D (IMu l I R)) -> \n desc l (Sigma I (IMu l I R)) (box l I D (IMu l I R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (l : Level)\n (I : Set l)\n (R : I -> IDesc l I)\n (P : Sigma I (IMu l I R) -> Set l)\n (m : (i : I)\n (xs : desc l I (R i) (IMu l I R))\n (hs : desc l (Sigma I (IMu l I R)) (box l I (R i) (IMu l I R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu l I R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- List\n--********************************************\n\ndata ListDConst (l : Level) : Set l where\n cnil : ListDConst l\n ccons : ListDConst l\n\nlistDChoice : (l : Level) -> Set l -> ListDConst l -> IDesc l Unit\nlistDChoice x X cnil = const Unit\nlistDChoice x X ccons = sigma X (\\_ -> var Void)\n\nlistD : (l : Level) -> Set l -> IDesc l Unit\nlistD x X = sigma (ListDConst x) (listDChoice x X)\n\nlist : (l : Level) -> Set l -> Set l\nlist x X = IMu x Unit (\\_ -> listD x X) Void\n\n\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst (l : Level) : Set l where\n lvar : DescDConst l\n lconst : DescDConst l\n lprod : DescDConst l\n lpi : DescDConst l\n lsigma : DescDConst l\n\ndescDChoice : (l : Level) -> Set l -> DescDConst (suc l) -> IDesc (suc l) Unit\ndescDChoice _ I lvar = const (lift I)\ndescDChoice x _ lconst = const (Set x)\ndescDChoice _ _ lprod = prod (var Void) (var Void)\ndescDChoice x _ lpi = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice x _ lsigma = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\ndescD x I = sigma (DescDConst (suc x)) (descDChoice x I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (suc x) Unit (\\_ -> descD x I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst (suc x)\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst (suc x)\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst (suc x)\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst (suc x)\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst (suc x)\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n ","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (l : Level)(I : Set l) : Set (suc l) where\n var : I -> IDesc l I\n const : Set l -> IDesc l I\n prod : IDesc l I -> IDesc l I -> IDesc l I\n sigma : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n pi : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\ndesc _ I (var i) P = P i\ndesc _ I (const X) P = X\ndesc x I (prod D D') P = desc x I D P * desc x I D' P\ndesc x I (sigma S T) P = Sigma S (\\s -> desc x I (T s) P)\ndesc x I (pi S T) P = (s : S) -> desc x I (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l where\n con : desc l I (R i) (\\j -> IMu l I R j) -> IMu l I R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : (l : Level)(I : Set l)(D : IDesc l I)(P : I -> Set l) -> desc l I D P -> IDesc l (Sigma I P)\nbox _ I (var i) P x = var (i , x)\nbox _ I (const X) P x = const X\nbox x I (prod D D') P (d , d') = prod (box x I D P d) (box x I D' P d')\nbox x I (sigma S T) P (a , b) = box x I (T a) P b\nbox x I (pi S T) P f = pi S (\\s -> box x I (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (l : Level)\n (I : Set l)\n (R : I -> IDesc l I)\n (P : Sigma I (IMu l I R) -> Set l)\n (m : (i : I)\n (xs : desc l I (R i) (IMu l I R))\n (hs : desc l (Sigma I (IMu l I R)) (box l I (R i) (IMu l I R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu l I R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc l I) -> \n (xs : desc l I D (IMu l I R)) -> \n desc l (Sigma I (IMu l I R)) (box l I D (IMu l I R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (l : Level)\n (I : Set l)\n (R : I -> IDesc l I)\n (P : Sigma I (IMu l I R) -> Set l)\n (m : (i : I)\n (xs : desc l I (R i) (IMu l I R))\n (hs : desc l (Sigma I (IMu l I R)) (box l I (R i) (IMu l I R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu l I R i) -> P ( i , x )\ninduction = Elim.induction\n\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst (l : Level) : Set l where\n lvar : DescDConst l\n lconst : DescDConst l\n lprod : DescDConst l\n lpi : DescDConst l\n lsigma : DescDConst l\n\ndescDChoice : (l : Level) -> Set l -> DescDConst (suc l) -> IDesc (suc l) Unit\ndescDChoice _ I lvar = const (lift I)\ndescDChoice x _ lconst = const (Set x)\ndescDChoice _ _ lprod = prod (var Void) (var Void)\ndescDChoice x _ lpi = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice x _ lsigma = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\ndescD x I = sigma (DescDConst (suc x)) (descDChoice x I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (suc x) Unit (\\_ -> descD x I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst (suc x)\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst (suc x)\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst (suc x)\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst (suc x)\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst (suc x)\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n ","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"2f05ffbdd1a1f1b61f4e4d2af1e9628cbe81971f","subject":"Initial commit: Agda bags (work in progress)","message":"Initial commit: Agda bags (work in progress)\n\nOld-commit-hash: 59c52d1fe7d503bcff1b4a9b7849ed02034c61d0\n","repos":"inc-lc\/ilc-agda","old_file":"Data\/NatBag.agda","new_file":"Data\/NatBag.agda","new_contents":"","old_contents":"","returncode":0,"stderr":"unknown","license":"mit","lang":"Agda"} {"commit":"044fe9f6ed008b2e5b99318e97a0383f1d6d3352","subject":"flat-funs: Reworked default definitions and shown the space complexity of foldl","message":"flat-funs: Reworked default definitions and shown the space complexity of foldl\n","repos":"crypto-agda\/crypto-agda","old_file":"flat-funs.agda","new_file":"flat-funs.agda","new_contents":"module flat-funs where\n\nopen import Data.Unit using (\u22a4)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2^_; _\u2294_; module \u2115\u00b0)\nopen import Data.Fin using (Fin) renaming (_+_ to _+\u1da0_)\nopen import Data.Bool using (if_then_else_)\nimport Data.Vec.NP as V\nimport Level as L\nimport Function as F\nimport Data.Product as \u00d7\nimport Relation.Binary.PropositionalEquality as \u2261\nopen F using (const)\nopen V using (Vec; []; _\u2237_; _++_; [_])\nopen \u00d7 using (_\u00d7_; _,_; proj\u2081; proj\u2082; uncurry)\nopen \u2261 using (_\u2261_)\n\nopen import Data.Bits using (Bit; Bits; _\u2192\u1d47_; 0b; 1b)\n\nopen import universe\n\nrecord FlatFuns {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n universe : Universe T\n _`\u2192_ : T \u2192 T \u2192 Set\n infix 0 _`\u2192_\n open Universe universe public\n\nmodule Defaults {t} {T : Set t} (\u266dFuns : FlatFuns T) where\n open FlatFuns \u266dFuns\n\n module CompositionNotations\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)) where\n\n infixr 1 _\u204f_\n infixr 1 _>>>_\n\n _\u204f_ : \u2200 {a b c} \u2192 (a `\u2192 b) \u2192 (b `\u2192 c) \u2192 (a `\u2192 c)\n f \u204f g = g \u2218 f\n\n _>>>_ : \u2200 {a b c} \u2192 (a `\u2192 b) \u2192 (b `\u2192 c) \u2192 (a `\u2192 c)\n f >>> g = f \u204f g\n\n module DefaultsFirstSecond\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (<_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)) where\n\n first-default : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n first-default f = < f \u00d7 id >\n\n second-default : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second-default f = < id \u00d7 f >\n\n module DefaultsGroup2\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4)\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n <_\u00d7_>-default : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g >-default = < f \u2218 fst , g \u2218 snd >\n\n open DefaultsFirstSecond id <_\u00d7_>-default\n\n dup-default : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n dup-default = < id , id >\n\n swap-default : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n swap-default = < snd , fst >\n\n assoc-default : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc-default = < fst \u2218 fst , first-default snd >\n\n -default : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n -default = < tt , id >\n\n \u2032-default : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A\n \u2032-default = snd\n\n module DefaultsGroup1\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4)\n (\u2032 : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A)\n (dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A)\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (<_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)) where\n\n open DefaultsFirstSecond id <_\u00d7_>\n open CompositionNotations _\u2218_\n\n <_,_>-default : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f , g >-default = dup \u204f < f \u00d7 g >\n\n snd-default : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n snd-default = first-default tt \u204f \u2032\n\n fst-default : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n fst-default = swap \u204f snd-default\n\n module <\u00d7>Default\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n where\n open CompositionNotations _\u2218_\n\n <_\u00d7_>-default : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g >-default = first f \u204f swap \u204f first g \u204f swap\n\n module DefaultAssoc\u2032\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C)))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n where\n open CompositionNotations _\u2218_\n\n -- This definition would cost 1 \"space\".\n -- assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n -- assoc\u2032 = < second fst , snd \u204f snd >\n\n assoc\u2032-default : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n assoc\u2032-default = swap \u204f first swap \u204f assoc \u204f swap \u204f first swap\n\n -- In case you wonder... one can also derive cond with fork\n module DefaultCond\n (fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n cond-default : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n cond-default = fork fst snd\n\nrecord FlatFunsOps {t} {T : Set t} (\u266dFuns : FlatFuns T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n\n infixr 9 _\u2218_\n infixr 3 _***_\n infixr 3 _&&&_\n field\n -- Functions\n id : \u2200 {A} \u2192 A `\u2192 A\n _\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)\n\n -- Bit\n <0b> <1b> : \u2200 {_A} \u2192 _A `\u2192 `Bit\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n\n -- Unit\n tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4\n\n -- Products (group 1)\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n\n -- Products (group 2)\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n \u2032 : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A\n\n -- Vectors\n nil : \u2200 {_A B} \u2192 _A `\u2192 `Vec B 0\n cons : \u2200 {n A} \u2192 (A `\u00d7 `Vec A n) `\u2192 `Vec A (1 + n)\n uncons : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 (A `\u00d7 `Vec A n)\n\n open Defaults \u266dFuns\n open CompositionNotations _\u2218_ public\n\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n f &&& g = < f , g >\n\n _***_ : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n f *** g = < f \u00d7 g >\n\n constBit : \u2200 {_A} \u2192 Bit \u2192 _A `\u2192 `Bit\n constBit b = if b then <1b> else <0b>\n\n <,tt> : \u2200 {A} \u2192 A `\u2192 A `\u00d7 `\u22a4\n <,tt> = \u204f swap\n\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n first f = < f \u00d7 id >\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = < id \u00d7 f >\n\n -- This uses <_,_>, hence has space cost 2, while a cost of 1 is expected.\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork f g = second < f , g > \u204f cond\n\n open DefaultAssoc\u2032 _\u2218_ assoc swap first public renaming (assoc\u2032-default to assoc\u2032)\n\n <_`zip`_> : \u2200 {A B C D E F} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 ((D `\u00d7 E) `\u2192 F)\n \u2192 ((A `\u00d7 D) `\u00d7 (B `\u00d7 E)) `\u2192 (C `\u00d7 F)\n < f `zip` g > = < < fst \u00d7 fst > \u204f f ,\n < snd \u00d7 snd > \u204f g >\n\n head : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n head = uncons \u204f fst\n\n tail : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n tail = uncons \u204f snd\n\n <_\u2237_> : \u2200 {m n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A m `\u2192 `Vec B n)\n \u2192 `Vec A (1 + m) `\u2192 `Vec B (1 + n)\n < f \u2237 g > = uncons \u204f < f \u00d7 g > \u204f cons\n\n <_\u2237\u2032_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n < f \u2237\u2032 g > = < f , g > \u204f cons\n\n \u229b : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 `Vec A n `\u2192 `Vec B n\n \u229b [] = nil\n \u229b (f \u2237 fs) = < f \u2237 \u229b fs >\n\n \u229b\u2032 : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 A `\u2192 `Vec B n\n \u229b\u2032 [] = nil\n \u229b\u2032 (f \u2237 fs) = < f \u2237\u2032 \u229b\u2032 fs >\n\n constVec : \u2200 {n b _A} {B : Set b} {C} \u2192 (B \u2192 _A `\u2192 C) \u2192 Vec B n \u2192 _A `\u2192 `Vec C n\n constVec f xs = \u229b\u2032 (V.map f xs)\n\n constBits : \u2200 {n _A} \u2192 Bits n \u2192 _A `\u2192 `Bits n\n constBits = constVec constBit\n\n foldl : \u2200 {n A B} \u2192 (B `\u00d7 A `\u2192 B) \u2192 (B `\u00d7 `Vec A n) `\u2192 B\n foldl {zero} f = fst\n foldl {suc n} f = second uncons\n \u204f assoc\u2032\n \u204f first f\n \u204f foldl f\n\n foldl\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldl\u2081 f = uncons \u204f foldl f\n\n foldr : \u2200 {n A B} \u2192 (A `\u00d7 B `\u2192 B) \u2192 (`Vec A n `\u00d7 B) `\u2192 B\n foldr {zero} f = snd\n foldr {suc n} f = first uncons\n \u204f assoc\n \u204f second (foldr f)\n \u204f f\n\n foldr\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldr\u2081 f = uncons \u204f swap \u204f foldr f\n\n map : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A n `\u2192 `Vec B n)\n map f = \u229b (V.replicate f)\n\n zipWith : \u2200 {n A B C} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec C n\n zipWith {zero} f = nil\n zipWith {suc n} f = < uncons \u00d7 uncons >\n \u204f < f `zip` (zipWith f) >\n \u204f cons\n\n zip : \u2200 {n A B} \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec (A `\u00d7 B) n\n zip = zipWith id\n\n <,nil> : \u2200 {A B} \u2192 A `\u2192 A `\u00d7 `Vec B 0\n <,nil> = <,tt> \u204f second nil\n\n singleton : \u2200 {A} \u2192 A `\u2192 `Vec A 1\n singleton = <,nil> \u204f cons\n\n snoc : \u2200 {n A} \u2192 (`Vec A n `\u00d7 A) `\u2192 `Vec A (1 + n)\n snoc {zero} = snd \u204f singleton\n snoc {suc n} = first uncons \u204f assoc \u204f second snoc \u204f cons\n\n reverse : \u2200 {n A} \u2192 `Vec A n `\u2192 `Vec A n\n reverse {zero} = nil\n reverse {suc n} = uncons \u204f swap \u204f first reverse \u204f snoc\n\n append : \u2200 {m n A} \u2192 (`Vec A m `\u00d7 `Vec A n) `\u2192 `Vec A (m + n)\n append {zero} = snd\n append {suc m} = first uncons\n \u204f assoc\n \u204f second append\n \u204f cons\n\n splitAt : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 (`Vec A m `\u00d7 `Vec A n)\n splitAt zero = < nil , id >\n splitAt (suc m) = uncons\n \u204f second (splitAt m)\n \u204f assoc\u2032\n \u204f first cons\n\n take : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A m\n take zero = nil\n take (suc m) = < id \u2237 take m >\n\n drop : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A n\n drop zero = id\n drop (suc m) = tail \u204f drop m\n\n folda : \u2200 n {A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (2^ n) `\u2192 A\n folda zero f = head\n folda (suc n) f = splitAt (2^ n) \u204f < folda n f \u00d7 folda n f > \u204f f\n\n init : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n init {zero} = nil\n init {suc n} = < id \u2237 init >\n\n last : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n last {zero} = head\n last {suc n} = tail \u204f last\n\n concat : \u2200 {m n A} \u2192 `Vec (`Vec A m) n `\u2192 `Vec A (n * m)\n concat {n = zero} = nil\n concat {n = suc n} = uncons \u204f second concat \u204f append\n\n group : \u2200 {A} n k \u2192 `Vec A (n * k) `\u2192 `Vec (`Vec A k) n\n group zero k = nil\n group (suc n) k = splitAt k \u204f second (group n k) \u204f cons\n\n bind : \u2200 {m n A B} \u2192 (A `\u2192 `Vec B m) \u2192 `Vec A n `\u2192 `Vec B (n * m)\n bind f = map f \u204f concat\n\n replicate : \u2200 {n A} \u2192 A `\u2192 `Vec A n\n replicate {zero} = nil\n replicate {suc n} = < id , replicate > \u204f cons\n\n module WithFin\n (`Fin : \u2115 \u2192 T)\n (fz : \u2200 {n _A} \u2192 _A `\u2192 `Fin (suc n))\n (fs : \u2200 {n} \u2192 `Fin n `\u2192 `Fin (suc n))\n (elim-Fin0 : \u2200 {A} \u2192 `Fin 0 `\u2192 A)\n (elim-Fin1+ : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Fin n `\u00d7 A `\u2192 B) \u2192 `Fin (suc n) `\u00d7 A `\u2192 B) where\n\n tabulate : \u2200 {n A _B} \u2192 (`Fin n `\u2192 A) \u2192 _B `\u2192 `Vec A n\n tabulate {zero} f = nil\n tabulate {suc n} f = < fz \u204f f , tabulate (fs \u204f f) > \u204f cons\n\n lookup : \u2200 {n A} \u2192 `Fin n `\u00d7 `Vec A n `\u2192 A\n lookup {zero} = fst \u204f elim-Fin0\n lookup {suc n} = elim-Fin1+ head (second tail \u204f lookup)\n\n allFin : \u2200 {n _A} \u2192 _A `\u2192 `Vec (`Fin n) n\n allFin = tabulate id\n\n-\u2192- : Set \u2192 Set \u2192 Set\n-\u2192- A B = A \u2192 B\n\n_\u2192\u1da0_ : \u2115 \u2192 \u2115 \u2192 Set\n_\u2192\u1da0_ i o = Fin i \u2192 Fin o\n\nmapArr : \u2200 {s t} {S : Set s} {T : Set t} (F G : T \u2192 S)\n \u2192 (S \u2192 S \u2192 Set) \u2192 (T \u2192 T \u2192 Set)\nmapArr F G _`\u2192_ A B = F A `\u2192 G B\n\nfun\u266dFuns : FlatFuns Set\nfun\u266dFuns = mk Set-U -\u2192-\n\nmodule FunTypes = FlatFuns fun\u266dFuns\n\nbitsFun\u266dFuns : FlatFuns \u2115\nbitsFun\u266dFuns = mk Bits-U _\u2192\u1d47_\n\nmodule BitsFunTypes = FlatFuns bitsFun\u266dFuns\n\nfinFun\u266dFuns : FlatFuns \u2115\nfinFun\u266dFuns = mk Fin-U _\u2192\u1da0_\n\nmodule FinFunTypes = FlatFuns finFun\u266dFuns\n\nfun\u266dOps : FlatFunsOps fun\u266dFuns\nfun\u266dOps = mk F.id F._\u2218\u2032_\n (F.const 0b) (F.const 1b) (\u03bb { (b , x , y) \u2192 if b then x else y }) _\n \u00d7.<_,_> proj\u2081 proj\u2082 (\u03bb x \u2192 x , x) (\u03bb f g \u2192 \u00d7.map f g)\n \u00d7.swap (\u03bb {((x , y) , z) \u2192 x , (y , z) }) (\u03bb x \u2192 _ , x) proj\u2082\n (F.const []) (uncurry _\u2237_) V.uncons\n\nmodule FunOps = FlatFunsOps fun\u266dOps\n\nbitsFun\u266dOps : FlatFunsOps bitsFun\u266dFuns\nbitsFun\u266dOps = mk id _\u2218_\n (const [ 0b ]) (const [ 1b ]) cond\u1d47 (const [])\n <_,_>\u1d47 fst\u1d47 (\u03bb {x} \u2192 snd\u1d47 {x}) dup-default <_\u00d7_>-default\n (\u03bb {x} \u2192 swap-default {x}) (\u03bb {x} \u2192 assoc-default {x}) id id (const []) id id\n where\n open BitsFunTypes\n open FunOps\n fst\u1d47 : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n fst\u1d47 {A} = V.take A\n snd\u1d47 : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n snd\u1d47 {A} = V.drop A\n <_,_>\u1d47 : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n <_,_>\u1d47 f g x = f x ++ g x\n cond\u1d47 : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n cond\u1d47 {A} (b \u2237 xs) = if b then take A xs else drop A xs\n open Defaults bitsFun\u266dFuns\n open DefaultsGroup2 id _\u2218_ (const []) <_,_>\u1d47 fst\u1d47 (\u03bb {x} \u2192 snd\u1d47 {x})\n\nmodule BitsFunOps = FlatFunsOps bitsFun\u266dOps\n\n\u00d7-\u266dFuns : \u2200 {s t} {S : Set s} {T : Set t} \u2192 FlatFuns S \u2192 FlatFuns T \u2192 FlatFuns (S \u00d7 T)\n\u00d7-\u266dFuns funs-S funs-T = mk (\u00d7-U S.universe T.universe)\n (\u03bb { (A\u2080 , A\u2081) (B\u2080 , B\u2081) \u2192 (A\u2080 S.`\u2192 B\u2080) \u00d7 (A\u2081 T.`\u2192 B\u2081) })\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7\u22a4-\u266dFuns : \u2200 {s} {S : Set s} \u2192 FlatFuns S \u2192 FlatFuns \u22a4 \u2192 FlatFuns S\n\u00d7\u22a4-\u266dFuns funs-S funs-T = mk S.universe (\u03bb A B \u2192 (A S.`\u2192 B) \u00d7 (_ T.`\u2192 _))\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7-\u266dOps : \u2200 {s t} {S : Set s} {T : Set t} {funs-S : FlatFuns S} {funs-T : FlatFuns T}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-T\n \u2192 FlatFunsOps (\u00d7-\u266dFuns funs-S funs-T)\n\u00d7-\u266dOps ops-S ops-T\n = mk (S.id , T.id) (\u00d7.zip S._\u2218_ T._\u2218_)\n (S.<0b> , T.<0b>) (S.<1b> , T.<1b>) (S.cond , T.cond) (S.tt , T.tt)\n (\u00d7.zip S.<_,_> T.<_,_>) (S.fst , T.fst) (S.snd , T.snd)\n (S.dup , T.dup) (\u00d7.zip S.<_\u00d7_> T.<_\u00d7_>)\n (S.swap , T.swap) (S.assoc , T.assoc)\n (S. , T.) (S.\u2032 , T.\u2032)\n (S.nil , T.nil) (S.cons , T.cons) (S.uncons , T.uncons)\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-T\n open FunOps\n\n\u00d7\u22a4-\u266dOps : \u2200 {s} {S : Set s} {funs-S : FlatFuns S} {funs-\u22a4 : FlatFuns \u22a4}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-\u22a4\n \u2192 FlatFunsOps (\u00d7\u22a4-\u266dFuns funs-S funs-\u22a4)\n\u00d7\u22a4-\u266dOps ops-S ops-\u22a4\n = mk (S.id , T.id) (\u00d7.zip S._\u2218_ T._\u2218_)\n (S.<0b> , T.<0b>) (S.<1b> , T.<1b>) (S.cond , T.cond) (S.tt , T.tt)\n (\u00d7.zip S.<_,_> T.<_,_>) (S.fst , T.fst) (S.snd , T.snd)\n (S.dup , T.dup) (\u00d7.zip S.<_\u00d7_> T.<_\u00d7_>)\n (S.swap , T.swap) (S.assoc , T.assoc)\n (S. , T.) (S.\u2032 , T.\u2032)\n (S.nil , T.nil) (S.cons , T.id) (S.uncons , T.id)\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-\u22a4\n open FunOps\n\nconstFuns : Set \u2192 FlatFuns \u22a4\nconstFuns A = mk \u22a4-U (\u03bb _ _ \u2192 A)\n\nmodule ConstFunTypes A = FlatFuns (constFuns A)\n\ntimeOps : FlatFunsOps (constFuns \u2115)\ntimeOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 0; <1b> = 0; cond = 1; tt = 0;\n <_,_> = _+_; fst = 0; snd = 0;\n dup = 0; <_\u00d7_> = _\u2294_; swap = 0; assoc = 0;\n = 0; \u2032 = 0;\n nil = 0; cons = 0; uncons = 0 }\n\nmodule TimeOps = FlatFunsOps timeOps\n\nspaceOps : FlatFunsOps (constFuns \u2115)\nspaceOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 0; <1b> = 0; cond = 1; tt = 0;\n <_,_> = \u03bb x y \u2192 1 + (x + y); fst = 0; snd = 0;\n dup = 1; <_\u00d7_> = _+_; swap = 0; assoc = 0;\n = 0; \u2032 = 0;\n nil = 0; cons = 0; uncons = 0 }\n\nmodule SpaceOps where\n Space = \u2115\n open FlatFunsOps spaceOps public\n\n singleton\u22610 : singleton \u2261 0\n singleton\u22610 = \u2261.refl\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2115\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\ntime\u00d7spaceOps : FlatFunsOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-\u266dOps timeOps spaceOps\n\nmodule Time\u00d7SpaceOps = FlatFunsOps time\u00d7spaceOps\n","old_contents":"module flat-funs where\n\nopen import Data.Unit using (\u22a4)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2^_; _\u2294_)\nopen import Data.Fin using (Fin) renaming (_+_ to _+\u1da0_)\nopen import Data.Bool using (if_then_else_)\nimport Data.Vec.NP as V\nimport Level as L\nimport Function as F\nimport Data.Product as \u00d7\nimport Relation.Binary.PropositionalEquality as \u2261\nopen F using (const)\nopen V using (Vec; []; _\u2237_; _++_; [_])\nopen \u00d7 using (_\u00d7_; _,_; proj\u2081; proj\u2082; uncurry)\nopen \u2261 using (_\u2261_)\n\nopen import Data.Bits using (Bit; Bits; _\u2192\u1d47_; 0b; 1b)\n\nopen import universe\n\nrecord FlatFuns {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n universe : Universe T\n _`\u2192_ : T \u2192 T \u2192 Set\n infix 0 _`\u2192_\n open Universe universe public\n\nrecord FlatFunsOps {t} {T : Set t} (\u266dFuns : FlatFuns T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n infixr 1 _\u2218_\n infixr 1 _\u204f_\n infixr 1 _>>>_\n infixr 3 _***_\n infixr 3 _&&&_\n field\n -- Functions\n id : \u2200 {A} \u2192 A `\u2192 A\n _\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)\n\n -- Bit\n <0b> <1b> : \u2200 {_A} \u2192 _A `\u2192 `Bit\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n\n -- Products\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n\n -- Unit\n tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n\n -- Vectors\n nil : \u2200 {_A B} \u2192 _A `\u2192 `Vec B 0\n cons : \u2200 {n A} \u2192 (A `\u00d7 `Vec A n) `\u2192 `Vec A (1 + n)\n uncons : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 (A `\u00d7 `Vec A n)\n\n _\u204f_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (B `\u2192 C) \u2192 (A `\u2192 C)\n f \u204f g = g \u2218 f\n\n _>>>_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (B `\u2192 C) \u2192 (A `\u2192 C)\n f >>> g = f \u204f g\n\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f , g > = dup \u204f < f \u00d7 g >\n\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n f &&& g = < f , g >\n\n <_\u00d7_>-on-top-of-<,> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g >-on-top-of-<,> = < fst \u204f f , snd \u204f g >\n\n _***_ : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n f *** g = < f \u00d7 g >\n\n swap-on-top-<,> : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n swap-on-top-<,> = < snd , fst >\n\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n first f = < f \u00d7 id >\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = < id \u00d7 f >\n\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork f g = second < f , g > \u204f cond\n\n -- In case you wonder... one can also derive cond with fork\n cond-on-top-of-fork : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n cond-on-top-of-fork = fork fst snd\n\n constBit : \u2200 {_A} \u2192 Bit \u2192 _A `\u2192 `Bit\n constBit b = if b then <1b> else <0b>\n\n <,tt> : \u2200 {A} \u2192 A `\u2192 A `\u00d7 `\u22a4\n <,tt> = \u204f swap\n\n assoc-default : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc-default = < fst \u204f fst , first snd >\n\n assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n assoc\u2032 = < second fst , snd \u204f snd >\n\n <_`zip`_> : \u2200 {A B C D E F} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 ((D `\u00d7 E) `\u2192 F)\n \u2192 ((A `\u00d7 D) `\u00d7 (B `\u00d7 E)) `\u2192 (C `\u00d7 F)\n < f `zip` g > = < < fst \u00d7 fst > \u204f f ,\n < snd \u00d7 snd > \u204f g >\n\n head : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n head = uncons \u204f fst\n\n tail : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n tail = uncons \u204f snd\n\n <_\u2237_> : \u2200 {m n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A m `\u2192 `Vec B n)\n \u2192 `Vec A (1 + m) `\u2192 `Vec B (1 + n)\n < f \u2237 g > = uncons \u204f < f \u00d7 g > \u204f cons\n\n <_\u2237\u2032_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n < f \u2237\u2032 g > = < f , g > \u204f cons\n\n \u229b : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 `Vec A n `\u2192 `Vec B n\n \u229b [] = nil\n \u229b (f \u2237 fs) = < f \u2237 \u229b fs >\n\n \u229b\u2032 : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 A `\u2192 `Vec B n\n \u229b\u2032 [] = nil\n \u229b\u2032 (f \u2237 fs) = < f \u2237\u2032 \u229b\u2032 fs >\n\n constVec : \u2200 {n b _A} {B : Set b} {C} \u2192 (B \u2192 _A `\u2192 C) \u2192 Vec B n \u2192 _A `\u2192 `Vec C n\n constVec f xs = \u229b\u2032 (V.map f xs)\n\n constBits : \u2200 {n _A} \u2192 Bits n \u2192 _A `\u2192 `Bits n\n constBits = constVec constBit\n\n foldl : \u2200 {n A B} \u2192 (B `\u00d7 A `\u2192 B) \u2192 (B `\u00d7 `Vec A n) `\u2192 B\n foldl {zero} f = fst\n foldl {suc n} f = second uncons\n \u204f assoc\u2032\n \u204f first f\n \u204f foldl f\n\n foldl\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldl\u2081 f = uncons \u204f foldl f\n\n foldr : \u2200 {n A B} \u2192 (A `\u00d7 B `\u2192 B) \u2192 (`Vec A n `\u00d7 B) `\u2192 B\n foldr {zero} f = snd\n foldr {suc n} f = first uncons\n \u204f assoc\n \u204f second (foldr f)\n \u204f f\n\n foldr\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldr\u2081 f = uncons \u204f swap \u204f foldr f\n\n map : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A n `\u2192 `Vec B n)\n map f = \u229b (V.replicate f)\n\n zipWith : \u2200 {n A B C} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec C n\n zipWith {zero} f = nil\n zipWith {suc n} f = < uncons \u00d7 uncons >\n \u204f < f `zip` (zipWith f) >\n \u204f cons\n\n zip : \u2200 {n A B} \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec (A `\u00d7 B) n\n zip = zipWith id\n\n <,nil> : \u2200 {A B} \u2192 A `\u2192 A `\u00d7 `Vec B 0\n <,nil> = <,tt> \u204f second nil\n\n singleton : \u2200 {A} \u2192 A `\u2192 `Vec A 1\n singleton = <,nil> \u204f cons\n\n snoc : \u2200 {n A} \u2192 (`Vec A n `\u00d7 A) `\u2192 `Vec A (1 + n)\n snoc {zero} = snd \u204f singleton\n snoc {suc n} = first uncons \u204f assoc \u204f second snoc \u204f cons\n\n reverse : \u2200 {n A} \u2192 `Vec A n `\u2192 `Vec A n\n reverse {zero} = nil\n reverse {suc n} = uncons \u204f swap \u204f first reverse \u204f snoc\n\n append : \u2200 {m n A} \u2192 (`Vec A m `\u00d7 `Vec A n) `\u2192 `Vec A (m + n)\n append {zero} = snd\n append {suc m} = first uncons\n \u204f assoc\n \u204f second append\n \u204f cons\n\n splitAt : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 (`Vec A m `\u00d7 `Vec A n)\n splitAt zero = < nil , id >\n splitAt (suc m) = uncons\n \u204f second (splitAt m)\n \u204f assoc\u2032\n \u204f first cons\n\n take : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A m\n take zero = nil\n take (suc m) = < id \u2237 take m >\n\n drop : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A n\n drop zero = id\n drop (suc m) = tail \u204f drop m\n\n folda : \u2200 n {A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (2^ n) `\u2192 A\n folda zero f = head\n folda (suc n) f = splitAt (2^ n) \u204f < folda n f \u00d7 folda n f > \u204f f\n\n init : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n init {zero} = nil\n init {suc n} = < id \u2237 init >\n\n last : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n last {zero} = head\n last {suc n} = tail \u204f last\n\n concat : \u2200 {m n A} \u2192 `Vec (`Vec A m) n `\u2192 `Vec A (n * m)\n concat {n = zero} = nil\n concat {n = suc n} = uncons \u204f second concat \u204f append\n\n group : \u2200 {A} n k \u2192 `Vec A (n * k) `\u2192 `Vec (`Vec A k) n\n group zero k = nil\n group (suc n) k = splitAt k \u204f second (group n k) \u204f cons\n\n bind : \u2200 {m n A B} \u2192 (A `\u2192 `Vec B m) \u2192 `Vec A n `\u2192 `Vec B (n * m)\n bind f = map f \u204f concat\n\n replicate : \u2200 {n A} \u2192 A `\u2192 `Vec A n\n replicate {zero} = nil\n replicate {suc n} = < id , replicate > \u204f cons\n\n module WithFin\n (`Fin : \u2115 \u2192 T)\n (fz : \u2200 {n _A} \u2192 _A `\u2192 `Fin (suc n))\n (fs : \u2200 {n} \u2192 `Fin n `\u2192 `Fin (suc n))\n (elim-Fin0 : \u2200 {A} \u2192 `Fin 0 `\u2192 A)\n (elim-Fin1+ : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Fin n `\u00d7 A `\u2192 B) \u2192 `Fin (suc n) `\u00d7 A `\u2192 B) where\n\n tabulate : \u2200 {n A _B} \u2192 (`Fin n `\u2192 A) \u2192 _B `\u2192 `Vec A n\n tabulate {zero} f = nil\n tabulate {suc n} f = < fz \u204f f , tabulate (fs \u204f f) > \u204f cons\n\n lookup : \u2200 {n A} \u2192 `Fin n `\u00d7 `Vec A n `\u2192 A\n lookup {zero} = fst \u204f elim-Fin0\n lookup {suc n} = elim-Fin1+ head (second tail \u204f lookup)\n\n allFin : \u2200 {n _A} \u2192 _A `\u2192 `Vec (`Fin n) n\n allFin = tabulate id\n\n-\u2192- : Set \u2192 Set \u2192 Set\n-\u2192- A B = A \u2192 B\n\n_\u2192\u1da0_ : \u2115 \u2192 \u2115 \u2192 Set\n_\u2192\u1da0_ i o = Fin i \u2192 Fin o\n\nmapArr : \u2200 {s t} {S : Set s} {T : Set t} (F G : T \u2192 S)\n \u2192 (S \u2192 S \u2192 Set) \u2192 (T \u2192 T \u2192 Set)\nmapArr F G _`\u2192_ A B = F A `\u2192 G B\n\nfun\u266dFuns : FlatFuns Set\nfun\u266dFuns = mk Set-U -\u2192-\n\nmodule FunTypes = FlatFuns fun\u266dFuns\n\nbitsFun\u266dFuns : FlatFuns \u2115\nbitsFun\u266dFuns = mk Bits-U _\u2192\u1d47_\n\nmodule BitsFunTypes = FlatFuns bitsFun\u266dFuns\n\nfinFun\u266dFuns : FlatFuns \u2115\nfinFun\u266dFuns = mk Fin-U _\u2192\u1da0_\n\nmodule FinFunTypes = FlatFuns finFun\u266dFuns\n\nfun\u266dOps : FlatFunsOps fun\u266dFuns\nfun\u266dOps = mk F.id F._\u2218\u2032_\n (F.const 0b) (F.const 1b) (\u03bb { (b , x , y) \u2192 if b then x else y })\n (\u03bb x \u2192 x , x) proj\u2081 proj\u2082 (\u03bb f g \u2192 \u00d7.map f g)\n \u00d7.swap (\u03bb {((x , y) , z) \u2192 x , (y , z) }) _ (\u03bb x \u2192 _ , x) (F.const []) (uncurry _\u2237_) V.uncons\n\nmodule FunOps = FlatFunsOps fun\u266dOps\n\nbitsFun\u266dOps : FlatFunsOps bitsFun\u266dFuns\nbitsFun\u266dOps = mk id _\u2218_\n (const [ 0b ]) (const [ 1b ]) cond\u1d47\n dup\u1d47 fst\u1d47 (\u03bb {x} \u2192 snd\u1d47 {x}) <_\u00d7_>\u1d47\n (\u03bb {x} \u2192 swap\u1d47 {x}) (\u03bb {x} \u2192 assoc\u1d47 {x}) (const []) id (const []) id id\n where\n open BitsFunTypes\n open FunOps\n fst\u1d47 : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n fst\u1d47 {A} = V.take A\n snd\u1d47 : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n snd\u1d47 {A} = V.drop A\n dup\u1d47 : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n dup\u1d47 xs = xs ++ xs\n <_\u00d7_>\u1d47 : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n <_\u00d7_>\u1d47 {A} f g x = f (take A x) ++ g (drop A x)\n cond\u1d47 : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n cond\u1d47 {A} (b \u2237 xs) = if b then take A xs else drop A xs\n swap\u1d47 : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n swap\u1d47 {A} xs = drop A xs ++ take A xs\n assoc\u1d47 : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc\u1d47 {A} {B} xs = take A (take (A + B) xs) ++ (drop A (take (A + B) xs) ++ drop (A + B) xs) -- < fst\u1d47 \u204f fst\u1d47 , first snd\u1d47 >\n\nmodule BitsFunOps = FlatFunsOps bitsFun\u266dOps\n\n\u00d7-\u266dFuns : \u2200 {s t} {S : Set s} {T : Set t} \u2192 FlatFuns S \u2192 FlatFuns T \u2192 FlatFuns (S \u00d7 T)\n\u00d7-\u266dFuns funs-S funs-T = mk (\u00d7-U S.universe T.universe)\n (\u03bb { (A\u2080 , A\u2081) (B\u2080 , B\u2081) \u2192 (A\u2080 S.`\u2192 B\u2080) \u00d7 (A\u2081 T.`\u2192 B\u2081) })\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7\u22a4-\u266dFuns : \u2200 {s} {S : Set s} \u2192 FlatFuns S \u2192 FlatFuns \u22a4 \u2192 FlatFuns S\n\u00d7\u22a4-\u266dFuns funs-S funs-T = mk S.universe (\u03bb A B \u2192 (A S.`\u2192 B) \u00d7 (_ T.`\u2192 _))\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7-\u266dOps : \u2200 {s t} {S : Set s} {T : Set t} {funs-S : FlatFuns S} {funs-T : FlatFuns T}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-T\n \u2192 FlatFunsOps (\u00d7-\u266dFuns funs-S funs-T)\n\u00d7-\u266dOps ops-S ops-T\n = mk (S.id , T.id) (\u00d7.zip S._\u2218_ T._\u2218_)\n (S.<0b> , T.<0b>) (S.<1b> , T.<1b>) (S.cond , T.cond)\n (S.dup , T.dup) (S.fst , T.fst) (S.snd , T.snd) (\u00d7.zip S.<_\u00d7_> T.<_\u00d7_>)\n (S.swap , T.swap) (S.assoc , T.assoc)\n (S.tt , T.tt) (S. , T.)\n (S.nil , T.nil) (S.cons , T.cons) (S.uncons , T.uncons)\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-T\n open FunOps\n\n\u00d7\u22a4-\u266dOps : \u2200 {s} {S : Set s} {funs-S : FlatFuns S} {funs-\u22a4 : FlatFuns \u22a4}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-\u22a4\n \u2192 FlatFunsOps (\u00d7\u22a4-\u266dFuns funs-S funs-\u22a4)\n\u00d7\u22a4-\u266dOps ops-S ops-\u22a4\n = mk (S.id , T.id) (\u00d7.zip S._\u2218_ T._\u2218_)\n (S.<0b> , T.<0b>) (S.<1b> , T.<1b>) (S.cond , T.cond)\n (S.dup , T.dup) (S.fst , T.fst) (S.snd , T.snd) (\u00d7.zip S.<_\u00d7_> T.<_\u00d7_>)\n (S.swap , T.swap) (S.assoc , T.assoc)\n (S.tt , T.tt) (S. , T.)\n (S.nil , T.nil) (S.cons , T.id) (S.uncons , T.id)\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-\u22a4\n open FunOps\n\nconstFuns : Set \u2192 FlatFuns \u22a4\nconstFuns A = mk \u22a4-U (\u03bb _ _ \u2192 A)\n\nmodule ConstFunTypes A = FlatFuns (constFuns A)\n\ntimeOps : FlatFunsOps (constFuns \u2115)\ntimeOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 0; <1b> = 0; cond = 1;\n dup = 0; fst = 0; snd = 0; <_\u00d7_> = _\u2294_; swap = 0; assoc = 0;\n tt = 0; = 0;\n nil = 0; cons = 0; uncons = 0 }\n\nmodule TimeOps = FlatFunsOps timeOps\n\nspaceOps : FlatFunsOps (constFuns \u2115)\nspaceOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 0; <1b> = 0; cond = 1;\n dup = 1; fst = 0; snd = 0; <_\u00d7_> = _+_; swap = 0; assoc = 0;\n tt = 0; = 0;\n nil = 0; cons = 0; uncons = 0 }\n\nmodule SpaceOps where\n Space = \u2115\n open FlatFunsOps spaceOps public\n\n singleton\u22610 : singleton \u2261 0\n singleton\u22610 = \u2261.refl\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\ntime\u00d7spaceOps : FlatFunsOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-\u266dOps timeOps spaceOps\n\nmodule Time\u00d7SpaceOps = FlatFunsOps time\u00d7spaceOps\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"f3ccc448958e9a29f4b3791191c8dcf51d8f46e2","subject":"Added doc.","message":"Added doc.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/PA\/Axiomatic\/Mendelson\/Properties\/UnprovedATP.agda","new_file":"src\/fot\/PA\/Axiomatic\/Mendelson\/Properties\/UnprovedATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Unproven PA properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule PA.Axiomatic.Mendelson.Properties.UnprovedATP where\n\nopen import PA.Axiomatic.Mendelson.Base\nopen import PA.Axiomatic.Mendelson.PropertiesATP\n\n------------------------------------------------------------------------------\n\n+-asocc : \u2200 m n o \u2192 m + n + o \u2248 m + (n + o)\n+-asocc m n o = S\u2089 A A0 is m\n where\n A : \u2115 \u2192 Set\n A i = i + n + o \u2248 i + (n + o)\n {-# ATP definition A #-}\n\n postulate A0 : A zero\n {-# ATP prove A0 +-leftCong #-}\n\n -- 25 November 2013: Vampire 0.6 (revision 903) proves the theorem\n -- using the --mode\u00a0casc option and a time out of 300 sec.\n postulate is : \u2200 i \u2192 A i \u2192 A (succ i)\n {-# ATP prove is +-leftCong #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Unproven PA properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule PA.Axiomatic.Mendelson.Properties.UnprovedATP where\n\nopen import PA.Axiomatic.Mendelson.Base\nopen import PA.Axiomatic.Mendelson.PropertiesATP\n\n------------------------------------------------------------------------------\n\n+-asocc : \u2200 m n o \u2192 m + n + o \u2248 m + (n + o)\n+-asocc m n o = S\u2089 A A0 is m\n where\n A : \u2115 \u2192 Set\n A i = i + n + o \u2248 i + (n + o)\n {-# ATP definition A #-}\n\n postulate A0 : A zero\n {-# ATP prove A0 +-leftCong #-}\n\n -- 25 November 2013: Vampire 0.6 proves the theorem using a time out\n -- of 300 sec.\n postulate is : \u2200 i \u2192 A i \u2192 A (succ i)\n {-# ATP prove is +-leftCong #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"2971c097d16d13188929ef857808539310db0b8e","subject":"Improve printing of resolved overloading.","message":"Improve printing of resolved overloading.\n\nAfter this change, the semantic brackets will contain the syntactic\nthing even if Agda displays explicitly resolved overloaded notation.\n\nOld-commit-hash: c8cbc43ed8e715342e0cc3bccc98e0be2dd23560\n","repos":"inc-lc\/ilc-agda","old_file":"meaning.agda","new_file":"meaning.agda","new_contents":"module meaning where\n\nopen import Level\n\nrecord Meaning (Syntax : Set) {\u2113 : Level} : Set (suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\nopen Meaning public\n using (\u27e8_\u27e9\u27e6_\u27e7)\n","old_contents":"module meaning where\n\nopen import Level\n\nrecord Meaning (Syntax : Set) {\u2113 : Level} : Set (suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b5190556d5bc58a768c39b9fa871dea8b2d6f0f1","subject":"Fix bug in diff-term.","message":"Fix bug in diff-term.\n\nOld-commit-hash: 01ada999b676d2a8a2ab289b3a0c79f1524620e5\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Term.agda","new_file":"Parametric\/Change\/Term.agda","new_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Constant\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\nDiffStructure : Set\nDiffStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))\n\nApplyStructure : Set\nApplyStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\n\nmodule Structure\n (diff-base : DiffStructure)\n (apply-base : ApplyStructure)\n where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n diff-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n apply-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n diff-term {base \u03b9} = diff-base\n diff-term {\u03c3 \u21d2 \u03c4} =\n (let\n _\u229d\u03c4_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff-term {\u03c4} {\u0393}) s t\n _\u2295\u03c3_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply-term {\u03c3} {\u0393}) \u0394t t\n in\n abs\u2084 (\u03bb g f x \u0394x \u2192 app g (x \u2295\u03c3 \u0394x) \u229d\u03c4 app f x))\n\n apply-term {base \u03b9} = apply-base\n apply-term {\u03c3 \u21d2 \u03c4} =\n (let\n _\u229d\u03c3_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff-term {\u03c3} {\u0393}) s t\n _\u2295\u03c4_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply-term {\u03c4} {\u0393}) \u0394t t\n in\n abs\u2083 (\u03bb \u0394h h y \u2192 app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y)))\n\n diff : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 (\u0394Type \u03c4)\n diff = app\u2082 diff-term\n\n apply : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\n apply = app\u2082 apply-term\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Constant\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\nDiffStructure : Set\nDiffStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))\n\nApplyStructure : Set\nApplyStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\n\nmodule Structure\n (diff-base : DiffStructure)\n (apply-base : ApplyStructure)\n where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n diff-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n apply-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n diff-term {base \u03b9} = diff-base\n diff-term {\u03c3 \u21d2 \u03c4} =\n (let\n _\u229d\u03c4_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff-term {\u03c4} {\u0393}) s t\n _\u2295\u03c3_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply-term {\u03c3} {\u0393}) \u0394t t\n in\n abs\u2084 (\u03bb g f x \u0394x \u2192 app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))\n\n apply-term {base \u03b9} = apply-base\n apply-term {\u03c3 \u21d2 \u03c4} =\n (let\n _\u229d\u03c3_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff-term {\u03c3} {\u0393}) s t\n _\u2295\u03c4_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply-term {\u03c4} {\u0393}) \u0394t t\n in\n abs\u2083 (\u03bb \u0394h h y \u2192 app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y)))\n\n diff : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 (\u0394Type \u03c4)\n diff = app\u2082 diff-term\n\n apply : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\n apply = app\u2082 apply-term\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"011116b87218eb84d5010b97f93c495b4f090fae","subject":"Implement nil changes.","message":"Implement nil changes.\n\nOld-commit-hash: da546d60ec48fe5f6f8b261aac1fdbb71b18fa69\n","repos":"inc-lc\/ilc-agda","old_file":"incremental.agda","new_file":"incremental.agda","new_contents":"module incremental where\n\n-- SIMPLE TYPES\n\n-- Syntax\n\ndata Type : Set where\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\n-- Semantics\n\nDom\u27e6_\u27e7 : Type -> Set\nDom\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7 = Dom\u27e6 \u03c4\u2081 \u27e7 \u2192 Dom\u27e6 \u03c4\u2082 \u27e7\n\n-- TYPING CONTEXTS\n\n-- Syntax\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n-- Semantics\n\ndata Empty : Set where\n \u2205 : Empty\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\nEnv\u27e6_\u27e7 : Context \u2192 Set\nEnv\u27e6 \u2205 \u27e7 = Empty\nEnv\u27e6 \u03c4 \u2022 \u0393 \u27e7 = Bind Dom\u27e6 \u03c4 \u27e7 Env\u27e6 \u0393 \u27e7\n\n-- VARIABLES\n\n-- Syntax\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\n-- Semantics\n\nlookup\u27e6_\u27e7 : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Env\u27e6 \u0393 \u27e7 \u2192 Dom\u27e6 \u03c4 \u27e7\nlookup\u27e6 this \u27e7 (v \u2022 \u03c1) = v\nlookup\u27e6 that x \u27e7 (v \u2022 \u03c1) = lookup\u27e6 x \u27e7 \u03c1\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n-- Semantics\n\neval\u27e6_\u27e7 : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Env\u27e6 \u0393 \u27e7 \u2192 Dom\u27e6 \u03c4 \u27e7\neval\u27e6 abs t \u27e7 \u03c1 = \u03bb v \u2192 eval\u27e6 t \u27e7 (v \u2022 \u03c1)\neval\u27e6 app t\u2081 t\u2082 \u27e7 \u03c1 = (eval\u27e6 t\u2081 \u27e7 \u03c1) (eval\u27e6 t\u2082 \u27e7 \u03c1)\neval\u27e6 var x \u27e7 \u03c1 = lookup\u27e6 x \u27e7 \u03c1\n\n\n-- WEAKENING\n\n-- Extend a context to a super context\n\ninfixr 10 _\u22ce_\n\n_\u22ce_ : (\u0393\u2081 \u0393\u2081 : Context) \u2192 Context\n\u2205 \u22ce \u0393\u2082 = \u0393\u2082\n(\u03c4 \u2022 \u0393\u2081) \u22ce \u0393\u2082 = \u03c4 \u2022 \u0393\u2081 \u22ce \u0393\u2082\n\n-- Lift a variable to a super context\n\nlift : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nlift {\u2205} {\u2205} x = x\nlift {\u2205} {\u03c4 \u2022 \u0393\u2082} x = that (lift {\u2205} {\u0393\u2082} x)\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} this = this\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} (that x) = that (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- Weaken a term to a super context\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken {\u0393\u2081} {\u0393\u2082} (abs {\u03c4\u2081 = \u03c4} t) = abs (weaken {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} t)\nweaken {\u0393\u2081} {\u0393\u2082} (app t\u2081 t\u2082) = app (weaken {\u0393\u2081} {\u0393\u2082} t\u2081) (weaken {\u0393\u2081} {\u0393\u2082} t\u2082)\nweaken {\u0393\u2081} {\u0393\u2082} (var x) = var (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\napply : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbf. \u03bbx. apply ( df x ) ( f x )\n\ncompose : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4)\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbdg. \u03bbx. compose ( df x ) ( dg x )\n\nnil : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4)\nnil {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs nil\n -- \u03bbx. nil\n\n-- Hey, apply is \u03b1-equivalent to compose, what's going on?\n-- Oh, and `\u0394-Type` is the identity function?\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u03c4 \u2022 \u0394-Type \u03c4 \u2022 \u0393\n\n-- CHANGING TERMS WHEN THE ENVIRONMENT CHANGES\n\n\u0394-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-term {\u0393} (abs {\u03c4\u2081 = \u03c4} t) = abs (\u0394-term {\u03c4 \u2022 \u0393} t)\n\u0394-term {\u0393} (app t\u2081 t\u2082) = {!!}\n\u0394-term {\u0393} (var x) = {!!}\n","old_contents":"module incremental where\n\n-- SIMPLE TYPES\n\n-- Syntax\n\ndata Type : Set where\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\n-- Semantics\n\nDom\u27e6_\u27e7 : Type -> Set\nDom\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7 = Dom\u27e6 \u03c4\u2081 \u27e7 \u2192 Dom\u27e6 \u03c4\u2082 \u27e7\n\n-- TYPING CONTEXTS\n\n-- Syntax\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n-- Semantics\n\ndata Empty : Set where\n \u2205 : Empty\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\nEnv\u27e6_\u27e7 : Context \u2192 Set\nEnv\u27e6 \u2205 \u27e7 = Empty\nEnv\u27e6 \u03c4 \u2022 \u0393 \u27e7 = Bind Dom\u27e6 \u03c4 \u27e7 Env\u27e6 \u0393 \u27e7\n\n-- VARIABLES\n\n-- Syntax\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\n-- Semantics\n\nlookup\u27e6_\u27e7 : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Env\u27e6 \u0393 \u27e7 \u2192 Dom\u27e6 \u03c4 \u27e7\nlookup\u27e6 this \u27e7 (v \u2022 \u03c1) = v\nlookup\u27e6 that x \u27e7 (v \u2022 \u03c1) = lookup\u27e6 x \u27e7 \u03c1\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n-- Semantics\n\neval\u27e6_\u27e7 : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Env\u27e6 \u0393 \u27e7 \u2192 Dom\u27e6 \u03c4 \u27e7\neval\u27e6 abs t \u27e7 \u03c1 = \u03bb v \u2192 eval\u27e6 t \u27e7 (v \u2022 \u03c1)\neval\u27e6 app t\u2081 t\u2082 \u27e7 \u03c1 = (eval\u27e6 t\u2081 \u27e7 \u03c1) (eval\u27e6 t\u2082 \u27e7 \u03c1)\neval\u27e6 var x \u27e7 \u03c1 = lookup\u27e6 x \u27e7 \u03c1\n\n\n-- WEAKENING\n\n-- Extend a context to a super context\n\ninfixr 10 _\u22ce_\n\n_\u22ce_ : (\u0393\u2081 \u0393\u2081 : Context) \u2192 Context\n\u2205 \u22ce \u0393\u2082 = \u0393\u2082\n(\u03c4 \u2022 \u0393\u2081) \u22ce \u0393\u2082 = \u03c4 \u2022 \u0393\u2081 \u22ce \u0393\u2082\n\n-- Lift a variable to a super context\n\nlift : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nlift {\u2205} {\u2205} x = x\nlift {\u2205} {\u03c4 \u2022 \u0393\u2082} x = that (lift {\u2205} {\u0393\u2082} x)\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} this = this\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} (that x) = that (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- Weaken a term to a super context\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken {\u0393\u2081} {\u0393\u2082} (abs {\u03c4\u2081 = \u03c4} t) = abs (weaken {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} t)\nweaken {\u0393\u2081} {\u0393\u2082} (app t\u2081 t\u2082) = app (weaken {\u0393\u2081} {\u0393\u2082} t\u2081) (weaken {\u0393\u2081} {\u0393\u2082} t\u2082)\nweaken {\u0393\u2081} {\u0393\u2082} (var x) = var (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\napply : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbf. \u03bbx. apply ( df x ) ( f x )\n\ncompose : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4)\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbdg. \u03bbx. compose ( df x ) ( dg x )\n\n-- Hey, apply is \u03b1-equivalent to compose, what's going on?\n-- Oh, and `\u0394-Type` is the identity function?\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u03c4 \u2022 \u0394-Type \u03c4 \u2022 \u0393\n\n-- CHANGING TERMS WHEN THE ENVIRONMENT CHANGES\n\n\u0394-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-term {\u0393} (abs {\u03c4\u2081 = \u03c4} t) = abs (\u0394-term {\u03c4 \u2022 \u0393} t)\n\u0394-term {\u0393} (app t\u2081 t\u2082) = {!!}\n\u0394-term {\u0393} (var x) = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f2fde70dc52c3050e537b787efd3c7efa9778118","subject":"Introduce HOAS-like smart constructors for lambdas","message":"Introduce HOAS-like smart constructors for lambdas\n\nOld-commit-hash: 6bf4939f8ec84c611d98f6f2395332943573dfe5\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Term\/Plotkin.agda","new_file":"Syntax\/Term\/Plotkin.agda","new_contents":"import Syntax.Type.Plotkin as Type\nimport Syntax.Context as Context\n\nmodule Syntax.Term.Plotkin\n {B : Set {- of base types -}}\n {C : Context.Context {Type.Type B} \u2192 Type.Type B \u2192 Set {- of constants -}}\n where\n\n-- Terms of languages described in Plotkin style\n\nopen import Function using (_\u2218_)\nopen import Data.Product\n\nopen Type B\nopen Context {Type}\n\nopen import Syntax.Context.Plotkin B\n\n-- Declarations of Term and Terms to enable mutual recursion\ndata Term\n (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set\n\ndata Terms\n (\u0393 : Context) :\n (\u03a3 : Context) \u2192 Set\n\n-- (Term \u0393 \u03c4) represents a term of type \u03c4\n-- with free variables bound in \u0393.\ndata Term \u0393 where\n const : \u2200 {\u03a3 \u03c4} \u2192\n (c : C \u03a3 \u03c4) \u2192\n Terms \u0393 \u03a3 \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\n-- (Terms \u0393 \u03a3) represents a list of terms with types from \u03a3\n-- with free variables bound in \u0393.\ndata Terms \u0393 where\n \u2205 : Terms \u0393 \u2205\n _\u2022_ : \u2200 {\u03c4 \u03a3} \u2192\n Term \u0393 \u03c4 \u2192\n Terms \u0393 \u03a3 \u2192\n Terms \u0393 (\u03c4 \u2022 \u03a3)\n\ninfixr 9 _\u2022_\n\n-- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n-- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\nlift-diff-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4) \u00d7 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-diff-apply diff apply {base \u03b9} = diff , apply\nlift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\nlift-diff : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4)\n\nlift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\nlift-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n-- Weakening\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\n\nweakenAll : \u2200 {\u0393\u2081 \u0393\u2082 \u03a3} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Terms \u0393\u2081 \u03a3 \u2192\n Terms \u0393\u2082 \u03a3\n\nweaken \u0393\u2081\u227c\u0393\u2082 (const c ts) = const c (weakenAll \u0393\u2081\u227c\u0393\u2082 ts)\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (lift \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\nweakenAll \u0393\u2081\u227c\u0393\u2082 \u2205 = \u2205\nweakenAll \u0393\u2081\u227c\u0393\u2082 (t \u2022 ts) = weaken \u0393\u2081\u227c\u0393\u2082 t \u2022 weakenAll \u0393\u2081\u227c\u0393\u2082 ts\n\n-- Specialized weakening\nweaken\u2081 : \u2200 {\u0393 \u03c3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4\nweaken\u2081 t = weaken (drop _ \u2022 \u227c-refl) t\n\nweaken\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u0393) \u03c4\nweaken\u2082 t = weaken (drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\nweaken\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u03b3 \u2022 \u0393) \u03c4\nweaken\u2083 t = weaken (drop _ \u2022 drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n-- Shorthands for nested applications\napp\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3\napp\u2082 f x = app (app f x)\n\napp\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4\napp\u2083 f x = app\u2082 (app f x)\n\napp\u2084 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5\napp\u2084 f x = app\u2083 (app f x)\n\napp\u2085 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6\napp\u2085 f x = app\u2084 (app f x)\n\napp\u2086 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6 \u21d2 \u03b7) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6 \u2192 Term \u0393 \u03b7\napp\u2086 f x = app\u2085 (app f x)\n\nUncurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nUncurriedTermConstructor \u0393 \u03a3 \u03c4 = Terms \u0393 \u03a3 \u2192 Term \u0393 \u03c4\n\nuncurriedConst : \u2200 {\u03a3 \u03c4} \u2192 C \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 UncurriedTermConstructor \u0393 \u03a3 \u03c4\nuncurriedConst constant = const constant\n\nCurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nCurriedTermConstructor \u0393 \u2205 \u03c4\u2032 = Term \u0393 \u03c4\u2032\nCurriedTermConstructor \u0393 (\u03c4 \u2022 \u03a3) \u03c4\u2032 = Term \u0393 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\u2032\n\ncurryTermConstructor : \u2200 {\u03a3 \u0393 \u03c4} \u2192 UncurriedTermConstructor \u0393 \u03a3 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\ncurryTermConstructor {\u2205} k = k \u2205\ncurryTermConstructor {\u03c4 \u2022 \u03a3} k = \u03bb t \u2192 curryTermConstructor (\u03bb ts \u2192 k (t \u2022 ts))\n\ncurriedConst : \u2200 {\u03a3 \u03c4} \u2192 C \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\ncurriedConst constant = curryTermConstructor (uncurriedConst constant)\n\n\n-- HOAS-like smart constructors for lambdas, for different arities.\n\nabs\u2081 :\n \u2200 {\u0393 \u03c4\u2081 \u03c4} \u2192\n (\u2200 {\u0393\u2032} \u2192 {\u0393\u227c\u0393\u2032 : \u0393 \u227c \u0393\u2032} \u2192 (x : Term \u0393\u2032 \u03c4\u2081) \u2192 Term \u0393\u2032 \u03c4) \u2192\n (Term \u0393 (\u03c4\u2081 \u21d2 \u03c4))\nabs\u2081 {\u0393} {\u03c4\u2081} = \u03bb f \u2192 abs (f {\u0393\u227c\u0393\u2032 = drop \u03c4\u2081 \u2022 \u227c-refl} (var this))\n\nabs\u2082 :\n \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c4} \u2192\n (\u2200 {\u0393\u2032} \u2192 {\u0393\u227c\u0393\u2032 : \u0393 \u227c \u0393\u2032} \u2192 Term \u0393\u2032 \u03c4\u2081 \u2192 Term \u0393\u2032 \u03c4\u2082 \u2192 Term \u0393\u2032 \u03c4) \u2192\n (Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082 \u21d2 \u03c4))\nabs\u2082 f =\n abs\u2081 (\u03bb {_} {\u0393\u227c\u0393\u2032} x\u2081 \u2192\n abs\u2081 (\u03bb {_} {\u0393\u2032\u227c\u0393\u2032\u2081} \u2192\n f {\u0393\u227c\u0393\u2032 = \u227c-trans \u0393\u227c\u0393\u2032 \u0393\u2032\u227c\u0393\u2032\u2081} (weaken \u0393\u2032\u227c\u0393\u2032\u2081 x\u2081)))\n\nabs\u2083 :\n \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c4\u2083 \u03c4} \u2192\n (\u2200 {\u0393\u2032} \u2192 {\u0393\u227c\u0393\u2032 : \u0393 \u227c \u0393\u2032} \u2192 Term \u0393\u2032 \u03c4\u2081 \u2192 Term \u0393\u2032 \u03c4\u2082 \u2192 Term \u0393\u2032 \u03c4\u2083 \u2192 Term \u0393\u2032 \u03c4) \u2192\n (Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082 \u21d2 \u03c4\u2083 \u21d2 \u03c4))\nabs\u2083 f =\n abs\u2081 (\u03bb {_} {\u0393\u227c\u0393\u2032} x\u2081 \u2192\n abs\u2082 (\u03bb {_} {\u0393\u2032\u227c\u0393\u2032\u2081} \u2192\n f {\u0393\u227c\u0393\u2032 = \u227c-trans \u0393\u227c\u0393\u2032 \u0393\u2032\u227c\u0393\u2032\u2081} (weaken \u0393\u2032\u227c\u0393\u2032\u2081 x\u2081)))\n\nabs\u2084 :\n \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c4\u2083 \u03c4\u2084 \u03c4} \u2192\n (\u2200 {\u0393\u2032} \u2192 {\u0393\u227c\u0393\u2032 : \u0393 \u227c \u0393\u2032} \u2192 Term \u0393\u2032 \u03c4\u2081 \u2192 Term \u0393\u2032 \u03c4\u2082 \u2192 Term \u0393\u2032 \u03c4\u2083 \u2192 Term \u0393\u2032 \u03c4\u2084 \u2192 Term \u0393\u2032 \u03c4) \u2192\n (Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082 \u21d2 \u03c4\u2083 \u21d2 \u03c4\u2084 \u21d2 \u03c4))\nabs\u2084 f =\n abs\u2081 (\u03bb {_} {\u0393\u227c\u0393\u2032} x\u2081 \u2192\n abs\u2083 (\u03bb {_} {\u0393\u2032\u227c\u0393\u2032\u2081} \u2192\n f {\u0393\u227c\u0393\u2032 = \u227c-trans \u0393\u227c\u0393\u2032 \u0393\u2032\u227c\u0393\u2032\u2081} (weaken \u0393\u2032\u227c\u0393\u2032\u2081 x\u2081)))\n\nabs\u2085 :\n \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c4\u2083 \u03c4\u2084 \u03c4\u2085 \u03c4} \u2192\n (\u2200 {\u0393\u2032} \u2192 {\u0393\u227c\u0393\u2032 : \u0393 \u227c \u0393\u2032} \u2192 Term \u0393\u2032 \u03c4\u2081 \u2192 Term \u0393\u2032 \u03c4\u2082 \u2192 Term \u0393\u2032 \u03c4\u2083 \u2192 Term \u0393\u2032 \u03c4\u2084 \u2192 Term \u0393\u2032 \u03c4\u2085 \u2192 Term \u0393\u2032 \u03c4) \u2192\n (Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082 \u21d2 \u03c4\u2083 \u21d2 \u03c4\u2084 \u21d2 \u03c4\u2085 \u21d2 \u03c4))\nabs\u2085 f =\n abs\u2081 (\u03bb {_} {\u0393\u227c\u0393\u2032} x\u2081 \u2192\n abs\u2084 (\u03bb {_} {\u0393\u2032\u227c\u0393\u2032\u2081} \u2192\n f {\u0393\u227c\u0393\u2032 = \u227c-trans \u0393\u227c\u0393\u2032 \u0393\u2032\u227c\u0393\u2032\u2081} (weaken \u0393\u2032\u227c\u0393\u2032\u2081 x\u2081)))\n\nabs\u2086 :\n \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c4\u2083 \u03c4\u2084 \u03c4\u2085 \u03c4\u2086 \u03c4} \u2192\n (\u2200 {\u0393\u2032} \u2192 {\u0393\u227c\u0393\u2032 : \u0393 \u227c \u0393\u2032} \u2192 Term \u0393\u2032 \u03c4\u2081 \u2192 Term \u0393\u2032 \u03c4\u2082 \u2192 Term \u0393\u2032 \u03c4\u2083 \u2192 Term \u0393\u2032 \u03c4\u2084 \u2192 Term \u0393\u2032 \u03c4\u2085 \u2192 Term \u0393\u2032 \u03c4\u2086 \u2192 Term \u0393\u2032 \u03c4) \u2192\n (Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082 \u21d2 \u03c4\u2083 \u21d2 \u03c4\u2084 \u21d2 \u03c4\u2085 \u21d2 \u03c4\u2086 \u21d2 \u03c4))\nabs\u2086 f =\n abs\u2081 (\u03bb {_} {\u0393\u227c\u0393\u2032} x\u2081 \u2192\n abs\u2085 (\u03bb {_} {\u0393\u2032\u227c\u0393\u2032\u2081} \u2192\n f {\u0393\u227c\u0393\u2032 = \u227c-trans \u0393\u227c\u0393\u2032 \u0393\u2032\u227c\u0393\u2032\u2081} (weaken \u0393\u2032\u227c\u0393\u2032\u2081 x\u2081)))\n","old_contents":"import Syntax.Type.Plotkin as Type\nimport Syntax.Context as Context\n\nmodule Syntax.Term.Plotkin\n {B : Set {- of base types -}}\n {C : Context.Context {Type.Type B} \u2192 Type.Type B \u2192 Set {- of constants -}}\n where\n\n-- Terms of languages described in Plotkin style\n\nopen import Function using (_\u2218_)\nopen import Data.Product\n\nopen Type B\nopen Context {Type}\n\nopen import Syntax.Context.Plotkin B\n\n-- Declarations of Term and Terms to enable mutual recursion\ndata Term\n (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set\n\ndata Terms\n (\u0393 : Context) :\n (\u03a3 : Context) \u2192 Set\n\n-- (Term \u0393 \u03c4) represents a term of type \u03c4\n-- with free variables bound in \u0393.\ndata Term \u0393 where\n const : \u2200 {\u03a3 \u03c4} \u2192\n (c : C \u03a3 \u03c4) \u2192\n Terms \u0393 \u03a3 \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\n-- (Terms \u0393 \u03a3) represents a list of terms with types from \u03a3\n-- with free variables bound in \u0393.\ndata Terms \u0393 where\n \u2205 : Terms \u0393 \u2205\n _\u2022_ : \u2200 {\u03c4 \u03a3} \u2192\n Term \u0393 \u03c4 \u2192\n Terms \u0393 \u03a3 \u2192\n Terms \u0393 (\u03c4 \u2022 \u03a3)\n\ninfixr 9 _\u2022_\n\n-- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n-- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\nlift-diff-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4) \u00d7 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-diff-apply diff apply {base \u03b9} = diff , apply\nlift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\nlift-diff : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4)\n\nlift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\nlift-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n-- Weakening\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\n\nweakenAll : \u2200 {\u0393\u2081 \u0393\u2082 \u03a3} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Terms \u0393\u2081 \u03a3 \u2192\n Terms \u0393\u2082 \u03a3\n\nweaken \u0393\u2081\u227c\u0393\u2082 (const c ts) = const c (weakenAll \u0393\u2081\u227c\u0393\u2082 ts)\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (lift \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\nweakenAll \u0393\u2081\u227c\u0393\u2082 \u2205 = \u2205\nweakenAll \u0393\u2081\u227c\u0393\u2082 (t \u2022 ts) = weaken \u0393\u2081\u227c\u0393\u2082 t \u2022 weakenAll \u0393\u2081\u227c\u0393\u2082 ts\n\n-- Specialized weakening\nweaken\u2081 : \u2200 {\u0393 \u03c3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4\nweaken\u2081 t = weaken (drop _ \u2022 \u227c-refl) t\n\nweaken\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u0393) \u03c4\nweaken\u2082 t = weaken (drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\nweaken\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u03b3 \u2022 \u0393) \u03c4\nweaken\u2083 t = weaken (drop _ \u2022 drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n-- Shorthands for nested applications\napp\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3\napp\u2082 f x = app (app f x)\n\napp\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4\napp\u2083 f x = app\u2082 (app f x)\n\napp\u2084 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5\napp\u2084 f x = app\u2083 (app f x)\n\napp\u2085 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6\napp\u2085 f x = app\u2084 (app f x)\n\napp\u2086 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6 \u21d2 \u03b7) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6 \u2192 Term \u0393 \u03b7\napp\u2086 f x = app\u2085 (app f x)\n\nUncurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nUncurriedTermConstructor \u0393 \u03a3 \u03c4 = Terms \u0393 \u03a3 \u2192 Term \u0393 \u03c4\n\nuncurriedConst : \u2200 {\u03a3 \u03c4} \u2192 C \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 UncurriedTermConstructor \u0393 \u03a3 \u03c4\nuncurriedConst constant = const constant\n\nCurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nCurriedTermConstructor \u0393 \u2205 \u03c4\u2032 = Term \u0393 \u03c4\u2032\nCurriedTermConstructor \u0393 (\u03c4 \u2022 \u03a3) \u03c4\u2032 = Term \u0393 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\u2032\n\ncurryTermConstructor : \u2200 {\u03a3 \u0393 \u03c4} \u2192 UncurriedTermConstructor \u0393 \u03a3 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\ncurryTermConstructor {\u2205} k = k \u2205\ncurryTermConstructor {\u03c4 \u2022 \u03a3} k = \u03bb t \u2192 curryTermConstructor (\u03bb ts \u2192 k (t \u2022 ts))\n\ncurriedConst : \u2200 {\u03a3 \u03c4} \u2192 C \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\ncurriedConst constant = curryTermConstructor (uncurriedConst constant)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"25e6b7ac020ffceed37754195a08542060325d04","subject":"Missing a cosmetic change.","message":"Missing a cosmetic change.\n\nIgnore-this: 1ff79f43e9cf87ce4ef78941000ea0ce\n\ndarcs-hash:20110313151430-3bd4e-363c8c0166d6c156ba38551e0eca323df0b50f18.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Data\/List\/Type.agda","new_file":"src\/FOTC\/Data\/List\/Type.agda","new_contents":"------------------------------------------------------------------------------\n-- The FOTC list type\n------------------------------------------------------------------------------\n\nmodule FOTC.Data.List.Type where\n\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n-- The FOTC list type.\ndata List : D \u2192 Set where\n nilL : List []\n consL : \u2200 x {xs} \u2192 (Lxs : List xs) \u2192 List (x \u2237 xs)\n\n-- Induction principle for List.\nindList : (P : D \u2192 Set) \u2192\n P [] \u2192\n (\u2200 x {xs} \u2192 List xs \u2192 P xs \u2192 P (x \u2237 xs)) \u2192\n \u2200 {xs} \u2192 List xs \u2192 P xs\nindList P P[] is nilL = P[]\nindList P P[] is (consL x Lxs) = is x Lxs (indList P P[] is Lxs)\n","old_contents":"------------------------------------------------------------------------------\n-- The FOTC list type\n------------------------------------------------------------------------------\n\nmodule FOTC.Data.List.Type where\n\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n-- The FOTC list type.\ndata List : D \u2192 Set where\n nilL : List []\n consL : \u2200 x {xs} \u2192 (Lxs : List xs) \u2192 List (x \u2237 xs)\n\n-- Induction principle for List.\nindList : (P : D \u2192 Set) \u2192\n P [] \u2192\n (\u2200 x {xs} \u2192 List xs \u2192 P xs \u2192 P (x \u2237 xs)) \u2192\n \u2200 {xs} \u2192 List xs \u2192 P xs\nindList P P[] is nilL = P[]\nindList P P[] is (consL x Lxs) = is x Lxs (indList P P[] is Lxs)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"63e76c39ea74fe1aff6cfb352031867948f8b3ea","subject":"Only renaming.","message":"Only renaming.\n\nIgnore-this: 735181c9057b86e703241df8c7d1f7b2\n\ndarcs-hash:20110218162103-3bd4e-34dedb41e328516d8c83a9ef2291477f8f441e55.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/McCarthy91\/ArithmeticATP.agda","new_file":"Draft\/McCarthy91\/ArithmeticATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Arithmetic stuff used by the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.ArithmeticATP where\n\nopen import LTC.Base\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\n\n------------------------------------------------------------------------------\n\npostulate\n 1+x-N : \u2200 {n} \u2192 N n \u2192 N (one + n)\n 11+x-N : \u2200 {n} \u2192 N n \u2192 N (eleven + n)\n x\u223810-N : \u2200 {n} \u2192 N n \u2192 N (n \u2238 ten)\n x+11-N : \u2200 {n} \u2192 N n \u2192 N (n + eleven)\n{-# ATP prove 1+x-N #-}\n{-# ATP prove 11+x-N #-}\n{-# ATP prove x\u223810-N \u2238-N N10 #-}\n{-# ATP prove x+11-N 11+x-N +-comm #-}\n\npostulate\n 91\u2261[100+11\u223810]\u223810 : (one-hundred + eleven \u2238 ten) \u2238 ten \u2261 ninety-one\n{-# ATP prove 91\u2261[100+11\u223810]\u223810 #-}\n\npostulate\n 100\u226189+11 : eighty-nine + eleven \u2261 one-hundred\n 101\u226190+11\u2261101 : ninety + eleven \u2261 hundred-one\n 101\u2261100+11-10 : (one-hundred + eleven) \u2238 ten \u2261 hundred-one\n 102\u226191+11 : ninety-one + eleven \u2261 hundred-two\n 103\u226192+1 : ninety-two + eleven \u2261 hundred-three\n 104\u226193+11 : ninety-three + eleven \u2261 hundred-four\n 105\u226194+11 : ninety-four + eleven \u2261 hundred-five\n 106\u226195+11 : ninety-five + eleven \u2261 hundred-six\n 107\u226196+11 : ninety-six + eleven \u2261 hundred-seven\n 108\u226197+11 : ninety-seven + eleven \u2261 hundred-eight\n 109\u226199+11 : ninety-eight + eleven \u2261 hundred-nine\n 110\u226199+11 : ninety-nine + eleven \u2261 hundred-ten\n 111\u2261100+11 : one-hundred + eleven \u2261 hundred-eleven\n\n{-# ATP prove 100\u226189+11 #-}\n{-# ATP prove 101\u226190+11\u2261101 #-}\n{-# ATP prove 101\u2261100+11-10 #-}\n{-# ATP prove 102\u226191+11 #-}\n{-# ATP prove 103\u226192+1 #-}\n{-# ATP prove 104\u226193+11 #-}\n{-# ATP prove 105\u226194+11 #-}\n{-# ATP prove 106\u226195+11 #-}\n{-# ATP prove 107\u226196+11 #-}\n{-# ATP prove 108\u226197+11 #-}\n{-# ATP prove 109\u226199+11 #-}\n{-# ATP prove 110\u226199+11 #-}\n{-# ATP prove 111\u2261100+11 #-}\n\npostulate\n 101>100' : GT hundred-one one-hundred\n 101>100 : GT ((one-hundred + eleven) \u2238 ten) one-hundred\n 111>100' : GT hundred-eleven one-hundred\n 111>100 : GT (one-hundred + eleven) one-hundred\n 100<102' : LT one-hundred hundred-two\n 100<102 : LT one-hundred (ninety-one + eleven)\n{-# ATP prove 101>100' #-}\n{-# ATP prove 101>100 101>100' 101\u2261100+11-10 #-}\n{-# ATP prove 111>100' #-}\n{-# ATP prove 111>100 111>100' 111\u2261100+11 #-}\n{-# ATP prove 100<102' #-}\n{-# ATP prove 100<102 100<102' 102\u226191+11 #-}\n\npostulate 91>100\u2192\u22a5 : GT ninety-one one-hundred \u2192 \u22a5\n{-# ATP prove 91>100\u2192\u22a5 #-}\n\npostulate x+1\u2264x\u223810+11 : \u2200 {n} \u2192 N n \u2192 LE (n + one) ((n \u2238 ten) + eleven)\n{-# ATP prove x+1\u2264x\u223810+11 x\u2264y+x\u2238y N10 1+x-N 11+x-N x\u223810-N +-comm #-}\n\npostulate x\u226489\u2192x+11>100\u2192\u22a5 : \u2200 {n} \u2192 N n \u2192 LE n eighty-nine \u2192\n GT (n + eleven) one-hundred \u2192 \u22a5\n{-# ATP prove x\u226489\u2192x+11>100\u2192\u22a5 x>y\u2192x\u2264y\u2192\u22a5 x\u2264y\u2192x+k\u2264y+k x+11-N N89 N100 100\u226189+11 #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Arithmetic stuff used by the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.ArithmeticATP where\n\nopen import LTC.Base\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\n\n------------------------------------------------------------------------------\n\npostulate\n 1+x-N : \u2200 {n} \u2192 N n \u2192 N (one + n)\n 11+x-N : \u2200 {n} \u2192 N n \u2192 N (eleven + n)\n x\u223810-N : \u2200 {n} \u2192 N n \u2192 N (n \u2238 ten)\n x+11-N : \u2200 {n} \u2192 N n \u2192 N (n + eleven)\n{-# ATP prove 1+x-N #-}\n{-# ATP prove 11+x-N #-}\n{-# ATP prove x\u223810-N \u2238-N N10 #-}\n{-# ATP prove x+11-N 11+x-N +-comm #-}\n\npostulate\n n111' : eleven + one-hundred \u2261 hundred-eleven\n n111 : one-hundred + eleven \u2261 hundred-eleven\n n101' : hundred-eleven \u2238 ten \u2261 hundred-one\n n101 : (one-hundred + eleven) \u2238 ten \u2261 hundred-one\n n91' : hundred-one \u2238 ten \u2261 ninety-one\n n91 : ((one-hundred + eleven) \u2238 ten) \u2238 ten \u2261 ninety-one\n n102' : eleven + ninety-one \u2261 hundred-two\n n102 : ninety-one + eleven \u2261 hundred-two\n n100' : eleven + eighty-nine \u2261 one-hundred\n n100 : eighty-nine + eleven \u2261 one-hundred\n n110 : ninety-nine + eleven \u2261 hundred-ten\n n109 : ninety-eight + eleven \u2261 hundred-nine\n n108 : ninety-seven + eleven \u2261 hundred-eight\n n107 : ninety-six + eleven \u2261 hundred-seven\n n106 : ninety-five + eleven \u2261 hundred-six\n n105 : ninety-four + eleven \u2261 hundred-five\n n104 : ninety-three + eleven \u2261 hundred-four\n n103 : ninety-two + eleven \u2261 hundred-three\n n101'' : ninety + eleven \u2261 hundred-one\n{-# ATP prove n111' #-}\n{-# ATP prove n111 +-comm n111' #-}\n{-# ATP prove n101' #-}\n{-# ATP prove n101 n111 n101' #-}\n{-# ATP prove n91' #-}\n{-# ATP prove n91 n101 n91' #-}\n{-# ATP prove n102' #-}\n{-# ATP prove n102 n102' +-comm #-}\n{-# ATP prove n100' #-}\n{-# ATP prove n100 n100' +-comm #-}\n{-# ATP prove n110 #-}\n{-# ATP prove n109 #-}\n{-# ATP prove n108 #-}\n{-# ATP prove n107 #-}\n{-# ATP prove n106 #-}\n{-# ATP prove n105 #-}\n{-# ATP prove n104 #-}\n{-# ATP prove n103 #-}\n{-# ATP prove n101'' #-}\n\n\npostulate\n 101>100' : GT hundred-one one-hundred\n 101>100 : GT ((one-hundred + eleven) \u2238 ten) one-hundred\n 111>100' : GT hundred-eleven one-hundred\n 111>100 : GT (one-hundred + eleven) one-hundred\n 100<102' : LT one-hundred hundred-two\n 100<102 : LT one-hundred (ninety-one + eleven)\n{-# ATP prove 101>100' #-}\n{-# ATP prove 101>100 101>100' n101 #-}\n{-# ATP prove 111>100' #-}\n{-# ATP prove 111>100 111>100' n111 #-}\n{-# ATP prove 100<102' #-}\n{-# ATP prove 100<102 100<102' n102 #-}\n\npostulate 91>100\u2192\u22a5 : GT ninety-one one-hundred \u2192 \u22a5\n{-# ATP prove 91>100\u2192\u22a5 #-}\n\npostulate x+1\u2264x\u223810+11 : \u2200 {n} \u2192 N n \u2192 LE (n + one) ((n \u2238 ten) + eleven)\n{-# ATP prove x+1\u2264x\u223810+11 x\u2264y+x\u2238y N10 1+x-N 11+x-N x\u223810-N +-comm #-}\n\npostulate x\u226489\u2192x+11>100\u2192\u22a5 : \u2200 {n} \u2192 N n \u2192 LE n eighty-nine \u2192\n GT (n + eleven) one-hundred \u2192 \u22a5\n{-# ATP prove x\u226489\u2192x+11>100\u2192\u22a5 x>y\u2192x\u2264y\u2192\u22a5 x\u2264y\u2192x+k\u2264y+k x+11-N N89 N100 n100 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"8514aefef6973b813691fb922e6d44e072cfc75d","subject":"update generic ZK","message":"update generic ZK\n","repos":"crypto-agda\/crypto-agda","old_file":"generic-zero-knowledge-interactive.agda","new_file":"generic-zero-knowledge-interactive.agda","new_contents":"open import Type\nopen import Data.Bool.NP as Bool hiding (check)\nopen import Data.Nat\nopen import Data.Maybe\nopen import Data.Product.NP\nopen import Data.Bits\nopen import Function.NP\nopen import Relation.Binary.PropositionalEquality.NP\n\nopen import sum\n\nmodule generic-zero-knowledge-interactive where\n\n-- A random argument, this is only a formal notation to\n-- indicate that the argument is supposed to be picked\n-- at random uniformly. (do not confuse with our randomness\n-- monad).\nrecord \u21ba (A : \u2605) : \u2605 where\n constructor rand\n field get : A\n\nmodule M (Permutation : \u2605)\n (_\u207b\u00b9 : Endo Permutation)\n (\u03bc\u03c0 : SumProp Permutation)\n\n (R\u209a-xtra : \u2605) -- extra prover\/adversary randomness\n (\u03bcR\u209a-xtra : SumProp R\u209a-xtra)\n\n (Problem : \u2605)\n (_==_ : Problem \u2192 Problem \u2192 Bit)\n (==-refl : \u2200 {pb} \u2192 (pb == pb) \u2261 true)\n (_\u2219P_ : Permutation \u2192 Endo Problem)\n (\u207b\u00b9-inverseP : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219P (\u03c0 \u2219P x) \u2261 x)\n\n (Solution : \u2605)\n (_\u2219S_ : Permutation \u2192 Endo Solution)\n (\u207b\u00b9-inverseS : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219S (\u03c0 \u2219S x) \u2261 x)\n\n (check : Problem \u2192 Solution \u2192 Bit)\n (check-\u2219 : \u2200 p s \u03c0 \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 check p s)\n\n (easy-pb : Permutation \u2192 Problem)\n (easy-sol : Permutation \u2192 Solution)\n (check-easy : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P easy-pb \u03c0) (\u03c0 \u2219S easy-sol \u03c0) \u2261 true)\n where\n\n -- prover\/adversary randomness\n R\u209a : \u2605\n R\u209a = Permutation \u00d7 R\u209a-xtra\n\n \u03bcR\u209a : SumProp R\u209a\n \u03bcR\u209a = \u03bc\u03c0 \u00d7\u03bc \u03bcR\u209a-xtra\n\n R = Bit \u00d7 R\u209a\n\n \u03bcR : SumProp R\n \u03bcR = \u03bcBit \u00d7\u03bc \u03bcR\u209a\n\n check-\u03c0 : Problem \u2192 Solution \u2192 R\u209a \u2192 Bit\n check-\u03c0 p s (\u03c0 , _) = check (\u03c0 \u2219P p) (\u03c0 \u2219S s)\n\n otp-\u2219-check : let #_ = count \u03bcR\u209a\n in\n \u2200 p\u2080 s\u2080 p\u2081 s\u2081 \u2192\n check p\u2080 s\u2080 \u2261 check p\u2081 s\u2081 \u2192\n #(check-\u03c0 p\u2080 s\u2080) \u2261 #(check-\u03c0 p\u2081 s\u2081)\n otp-\u2219-check p\u2080 s\u2080 p\u2081 s\u2081 check-pf =\n count-ext \u03bcR\u209a {f = check-\u03c0 p\u2080 s\u2080} {check-\u03c0 p\u2081 s\u2081} (\u03bb \u03c0,r \u2192\n check-\u03c0 p\u2080 s\u2080 \u03c0,r \u2261\u27e8 check-\u2219 p\u2080 s\u2080 (proj\u2081 \u03c0,r) \u27e9\n check p\u2080 s\u2080 \u2261\u27e8 check-pf \u27e9\n check p\u2081 s\u2081 \u2261\u27e8 sym (check-\u2219 p\u2081 s\u2081 (proj\u2081 \u03c0,r)) \u27e9\n check-\u03c0 p\u2081 s\u2081 \u03c0,r \u220e)\n where open \u2261-Reasoning\n\n #_ : (\u21ba (Bit \u00d7 Permutation \u00d7 R\u209a-xtra) \u2192 Bit) \u2192 \u2115\n # f = count \u03bcR (f \u2218 rand)\n\n _\u2261#_ : (f g : \u21ba (Bit \u00d7 R\u209a) \u2192 Bit) \u2192 \u2605\n f \u2261# g = # f \u2261 # g\n\n{-\n otp-\u2219 : let otp = \u03bb O pb s \u2192 count \u03bcR\u209a (\u03bb { (\u03c0 , _) \u2192 O (\u03c0 \u2219P pb) (\u03c0 \u2219S s) }) in\n \u2200 pb\u2080 s\u2080 pb\u2081 s\u2081 \u2192\n check pb\u2080 s\u2080 \u2261 check pb\u2081 s\u2081 \u2192\n (O : _ \u2192 _ \u2192 Bit) \u2192 otp O pb\u2080 s\u2080 \u2261 otp O pb\u2081 s\u2081\n otp-\u2219 pb\u2080 s\u2080 pb\u2081 s\u2081 check-pf O = {!(\u03bc\u03c0 \u00d7Sum-proj\u2082 \u03bcR\u209a-xtra ?!}\n-}\n Answer : Bit \u2192 \u2605\n Answer false{-0b-} = Permutation\n Answer true {-1b-} = Solution\n\n answer : Permutation \u2192 Solution \u2192 \u2200 b \u2192 Answer b\n answer \u03c0 _ false = \u03c0\n answer _ s true = s\n\n -- The prover is the advesary in the generic terminology,\n -- and the verifier is the challenger.\n DepProver : \u2605\n DepProver = Problem \u2192 \u21ba R\u209a \u2192 (b : Bit) \u2192 Problem \u00d7 Answer b\n\n Prover\u2080 : \u2605\n Prover\u2080 = Problem \u2192 \u21ba R\u209a \u2192 Problem \u00d7 Permutation\n\n Prover\u2081 : \u2605\n Prover\u2081 = Problem \u2192 \u21ba R\u209a \u2192 Problem \u00d7 Solution\n\n Prover : \u2605\n Prover = Prover\u2080 \u00d7 Prover\u2081\n\n prover : DepProver \u2192 Prover\n prover dpr = (\u03bb pb r \u2192 dpr pb r 0b) , (\u03bb pb r \u2192 dpr pb r 1b)\n\n depProver : Prover \u2192 DepProver\n depProver (pr\u2080 , pr\u2081) pb r false = pr\u2080 pb r\n depProver (pr\u2080 , pr\u2081) pb r true = pr\u2081 pb r\n\n -- Here we show that the explicit commitment step seems useless given\n -- the formalization. The verifier can \"trust\" the prover on the fact\n -- that any choice is going to be govern only by the problem and the\n -- randomness.\n module WithCommitment (Commitment : \u2605)\n (AnswerWC : Bit \u2192 \u2605)\n (reveal : \u2200 b \u2192 Commitment \u2192 AnswerWC b \u2192 Problem \u00d7 Answer b) where\n ProverWC = (Problem \u2192 R\u209a \u2192 Commitment)\n \u00d7 (Problem \u2192 R\u209a \u2192 (b : Bit) \u2192 AnswerWC b)\n\n depProver' : ProverWC \u2192 DepProver\n depProver' (pr\u2080 , pr\u2081) pb (rand r\u209a) b = reveal b (pr\u2080 pb r\u209a) (pr\u2081 pb r\u209a b)\n\n Verif : Problem \u2192 \u2200 b \u2192 Problem \u00d7 Answer b \u2192 Bit\n Verif pb false{-0b-} (\u03c0\u2219pb , \u03c0) = (\u03c0 \u2219P pb) == \u03c0\u2219pb\n Verif pb true {-1b-} (\u03c0\u2219pb , \u03c0\u2219s) = check \u03c0\u2219pb \u03c0\u2219s\n\n _\u21c4\u2032_ : Problem \u2192 DepProver \u2192 Bit \u2192 \u21ba R\u209a \u2192 Bit\n (pb \u21c4\u2032 pr) b (rand r\u209a) = Verif pb b (pr pb (rand r\u209a) b)\n\n _\u21c4_ : Problem \u2192 DepProver \u2192 \u21ba (Bit \u00d7 R\u209a) \u2192 Bit\n (pb \u21c4 pr) (rand (b , r\u209a)) = (pb \u21c4\u2032 pr) b (rand r\u209a)\n\n _\u21c4''_ : Problem \u2192 Prover \u2192 \u21ba (Bit \u00d7 R\u209a) \u2192 Bit\n pb \u21c4'' pr = pb \u21c4 depProver pr\n\n honest : (Problem \u2192 Maybe Solution) \u2192 DepProver\n honest solve pb (rand (\u03c0 , r\u209a)) b = (\u03c0 \u2219P pb , answer \u03c0 sol b)\n module Honest where\n sol : Solution\n sol with solve pb\n ... | just sol = \u03c0 \u2219S sol\n ... | nothing = \u03c0 \u2219S easy-sol \u03c0\n\n module WithCorrectSolver (pb : Problem)\n (s : Solution)\n (check-s : check pb s \u2261 true)\n where\n\n -- When the honest prover has a solution, he gets accepted\n -- unconditionally by the verifier.\n honest-accepted : \u2200 r \u2192 (pb \u21c4 honest (const (just s))) r \u2261 1b\n honest-accepted (rand (true , \u03c0 , r\u209a)) rewrite check-\u2219 pb s \u03c0 = check-s\n honest-accepted (rand (false , \u03c0 , r\u209a)) = ==-refl\n\n honest-\u2141 = \u03bb pb s \u2192 (pb \u21c4 honest (const (just s)))\n\n module HonestLeakZeroKnowledge (pb\u2080 pb\u2081 : Problem)\n (s\u2080 s\u2081 : Solution)\n (check-pf : check pb\u2080 s\u2080 \u2261 check pb\u2081 s\u2081) where\n\n helper : \u2200 r\u209a \u2192 Bool.to\u2115 ((pb\u2080 \u21c4\u2032 honest (const (just s\u2080))) 0b (rand r\u209a))\n \u2261 Bool.to\u2115 ((pb\u2081 \u21c4\u2032 honest (const (just s\u2081))) 0b (rand r\u209a))\n helper (\u03c0 , r\u209a) rewrite ==-refl {\u03c0 \u2219P pb\u2080} | ==-refl {\u03c0 \u2219P pb\u2081} = refl\n\n honest-leak : honest-\u2141 pb\u2080 s\u2080 \u2261# honest-\u2141 pb\u2081 s\u2081\n honest-leak rewrite otp-\u2219-check pb\u2080 s\u2080 pb\u2081 s\u2081 check-pf | sum-ext \u03bcR\u209a helper = refl\n\n module HonestLeakZeroKnowledge' (pb : Problem)\n (s\u2080 s\u2081 : Solution)\n (check-pf : check pb s\u2080 \u2261 check pb s\u2081) where\n\n honest-leak : honest-\u2141 pb s\u2080 \u2261# honest-\u2141 pb s\u2081\n honest-leak = HonestLeakZeroKnowledge.honest-leak pb pb s\u2080 s\u2081 check-pf\n\n -- Predicts b=b\u2032\n cheater : \u2200 b\u2032 \u2192 DepProver\n cheater b\u2032 pb (rand (\u03c0 , _)) b = \u03c0 \u2219P (case b\u2032 0\u2192 pb 1\u2192 easy-pb \u03c0)\n , answer \u03c0 (\u03c0 \u2219S easy-sol \u03c0) b\n\n -- If cheater predicts correctly, verifer accepts him\n cheater-accepted : \u2200 b pb r\u209a \u2192 (pb \u21c4\u2032 cheater b) b r\u209a \u2261 1b\n cheater-accepted true pb (rand (\u03c0 , r\u209a)) = check-easy \u03c0\n cheater-accepted false pb (rand (\u03c0 , r\u209a)) = ==-refl\n\n -- If cheater predicts incorrecty, verifier rejects him\n module CheaterRejected (pb : Problem)\n (not-easy-sol : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P pb) (\u03c0 \u2219S easy-sol \u03c0) \u2261 false)\n (not-easy-pb : \u2200 \u03c0 \u2192 ((\u03c0 \u2219P pb) == (\u03c0 \u2219P easy-pb \u03c0)) \u2261 false) where\n\n cheater-rejected : \u2200 b r\u209a \u2192 (pb \u21c4\u2032 cheater (not b)) b r\u209a \u2261 0b\n cheater-rejected true (rand (\u03c0 , r\u209a)) = not-easy-sol \u03c0\n cheater-rejected false (rand (\u03c0 , r\u209a)) = not-easy-pb \u03c0\n\nmodule DLog (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u229f_ : \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n (\u229f-\u229e : \u2200 \u03c0 x \u2192 (\u229f \u03c0) \u229e (\u03c0 \u229e x) \u2261 x)\n (^\u229f-\u2219 : \u2200 \u03b1 \u03b2 x \u2192 ((\u03b1 ^ (\u229f x)) \u2219 ((\u03b1 ^ x) \u2219 \u03b2)) \u2261 \u03b2)\n -- (\u2219-assoc : \u2200 \u03b1 \u03b2 \u03b3 \u2192 \u03b1 \u2219 (\u03b2 \u2219 \u03b3) \u2261 (\u03b1 \u2219 \u03b2) \u2219 \u03b3)\n (dist-^-\u229e : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u229e y) \u2261 (\u03b1 ^ x) \u2219 (\u03b1 ^ y))\n (_==_ : G \u2192 G \u2192 Bool)\n (==-refl : \u2200 {\u03b1} \u2192 (\u03b1 == \u03b1) \u2261 true)\n (==-cong-\u2219 : \u2200 {\u03b1 \u03b2 b} \u03b3 \u2192 \u03b1 == \u03b2 \u2261 b \u2192 (\u03b3 \u2219 \u03b1) == (\u03b3 \u2219 \u03b2) \u2261 b)\n (==-true : \u2200 {\u03b1 \u03b2} \u2192 \u03b1 == \u03b2 \u2261 true \u2192 \u03b1 \u2261 \u03b2)\n (\u03bc\u2124q : SumProp \u2124q)\n (R\u209a-xtra : \u2605) -- extra prover\/adversary randomness\n (\u03bcR\u209a-xtra : SumProp R\u209a-xtra)\n (some-\u2124q : \u2124q)\n where\n\n Permutation = \u2124q\n Problem = G\n Solution = \u2124q\n\n _\u207b\u00b9 : Endo Permutation\n \u03c0 \u207b\u00b9 = \u229f \u03c0\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n _\u2219P_ : Permutation \u2192 Endo Problem\n \u03c0 \u2219P p = g^ \u03c0 \u2219 p\n\n \u207b\u00b9-inverseP : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219P (\u03c0 \u2219P x) \u2261 x\n \u207b\u00b9-inverseP \u03c0 x rewrite ^\u229f-\u2219 g x \u03c0 = refl\n\n _\u2219S_ : Permutation \u2192 Endo Solution\n \u03c0 \u2219S s = \u03c0 \u229e s\n\n \u207b\u00b9-inverseS : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219S (\u03c0 \u2219S x) \u2261 x\n \u207b\u00b9-inverseS = \u229f-\u229e\n\n check : Problem \u2192 Solution \u2192 Bit\n check p s = p == g^ s\n\n check-\u2219' : \u2200 p s \u03c0 b \u2192 check p s \u2261 b \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 b\n check-\u2219' p s \u03c0 true check-p-s rewrite dist-^-\u229e g \u03c0 s | ==-true check-p-s = ==-refl\n check-\u2219' p s \u03c0 false check-p-s rewrite dist-^-\u229e g \u03c0 s = ==-cong-\u2219 (g^ \u03c0) check-p-s\n\n check-\u2219 : \u2200 p s \u03c0 \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 check p s\n check-\u2219 p s \u03c0 = check-\u2219' p s \u03c0 (check p s) refl\n\n easy-sol : Permutation \u2192 Solution\n easy-sol \u03c0 = some-\u2124q\n\n easy-pb : Permutation \u2192 Problem\n easy-pb \u03c0 = g^(easy-sol \u03c0)\n\n check-easy : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P easy-pb \u03c0) (\u03c0 \u2219S easy-sol \u03c0) \u2261 true\n check-easy \u03c0 rewrite dist-^-\u229e g \u03c0 (easy-sol \u03c0) = ==-refl\n\n open M Permutation _\u207b\u00b9 \u03bc\u2124q R\u209a-xtra \u03bcR\u209a-xtra\n Problem _==_ ==-refl _\u2219P_ \u207b\u00b9-inverseP Solution _\u2219S_ \u207b\u00b9-inverseS check check-\u2219 easy-pb easy-sol check-easy\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"open import Type\nopen import Data.Bool.NP as Bool hiding (check)\nopen import Data.Nat\nopen import Data.Maybe\nopen import Data.Product.NP\nopen import Data.Bits\nopen import Function.NP\nopen import Relation.Binary.PropositionalEquality.NP\n\nopen import sum\n\nmodule generic-zero-knowledge-interactive where\n\n-- A random argument, this is only a formal notation to\n-- indicate that the argument is supposed to be picked\n-- at random uniformly. (do not confuse with our randomness\n-- monad).\nrecord \u21ba (A : \u2605) : \u2605 where\n constructor rand\n field get : A\n\nmodule M (Permutation : \u2605)\n (_\u207b\u00b9 : Endo Permutation)\n (sum\u03c0 : Sum Permutation)\n (\u03bc\u03c0 : SumProp sum\u03c0)\n\n (R\u209a-xtra : \u2605) -- extra prover\/adversary randomness\n (sumR\u209a-xtra : Sum R\u209a-xtra)\n (\u03bcR\u209a-xtra : SumProp sumR\u209a-xtra)\n\n (Problem : \u2605)\n (_==_ : Problem \u2192 Problem \u2192 Bit)\n (==-refl : \u2200 {pb} \u2192 (pb == pb) \u2261 true)\n (_\u2219P_ : Permutation \u2192 Endo Problem)\n (\u207b\u00b9-inverseP : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219P (\u03c0 \u2219P x) \u2261 x)\n\n (Solution : \u2605)\n (_\u2219S_ : Permutation \u2192 Endo Solution)\n (\u207b\u00b9-inverseS : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219S (\u03c0 \u2219S x) \u2261 x)\n\n (check : Problem \u2192 Solution \u2192 Bit)\n (check-\u2219 : \u2200 p s \u03c0 \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 check p s)\n\n (easy-pb : Permutation \u2192 Problem)\n (easy-sol : Permutation \u2192 Solution)\n (check-easy : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P easy-pb \u03c0) (\u03c0 \u2219S easy-sol \u03c0) \u2261 true)\n where\n\n -- prover\/adversary randomness\n R\u209a : \u2605\n R\u209a = Permutation \u00d7 R\u209a-xtra\n\n sumR\u209a : Sum R\u209a\n sumR\u209a = sum\u03c0 \u00d7Sum sumR\u209a-xtra\n\n \u03bcR\u209a : SumProp sumR\u209a\n \u03bcR\u209a = \u03bc\u03c0 \u00d7\u03bc \u03bcR\u209a-xtra\n\n R = Bit \u00d7 R\u209a\n\n sumR : Sum R\n sumR = sumBit \u00d7Sum sumR\u209a\n\n \u03bcR : SumProp sumR\n \u03bcR = \u03bcBit \u00d7\u03bc \u03bcR\u209a\n\n check-\u03c0 : Problem \u2192 Solution \u2192 R\u209a \u2192 Bit\n check-\u03c0 p s (\u03c0 , _) = check (\u03c0 \u2219P p) (\u03c0 \u2219S s)\n\n otp-\u2219-check : let #_ = count \u03bcR\u209a\n in\n \u2200 p\u2080 s\u2080 p\u2081 s\u2081 \u2192\n check p\u2080 s\u2080 \u2261 check p\u2081 s\u2081 \u2192\n #(check-\u03c0 p\u2080 s\u2080) \u2261 #(check-\u03c0 p\u2081 s\u2081)\n otp-\u2219-check p\u2080 s\u2080 p\u2081 s\u2081 check-pf =\n count-ext \u03bcR\u209a {f = check-\u03c0 p\u2080 s\u2080} {check-\u03c0 p\u2081 s\u2081} (\u03bb \u03c0,r \u2192\n check-\u03c0 p\u2080 s\u2080 \u03c0,r \u2261\u27e8 check-\u2219 p\u2080 s\u2080 (proj\u2081 \u03c0,r) \u27e9\n check p\u2080 s\u2080 \u2261\u27e8 check-pf \u27e9\n check p\u2081 s\u2081 \u2261\u27e8 sym (check-\u2219 p\u2081 s\u2081 (proj\u2081 \u03c0,r)) \u27e9\n check-\u03c0 p\u2081 s\u2081 \u03c0,r \u220e)\n where open \u2261-Reasoning\n\n #_ : (\u21ba (Bit \u00d7 Permutation \u00d7 R\u209a-xtra) \u2192 Bit) \u2192 \u2115\n # f = count \u03bcR (f \u2218 rand)\n\n _\u2261#_ : (f g : \u21ba (Bit \u00d7 R\u209a) \u2192 Bit) \u2192 \u2605\n f \u2261# g = # f \u2261 # g\n\n{-\n otp-\u2219 : let otp = \u03bb O pb s \u2192 count \u03bcR\u209a (\u03bb { (\u03c0 , _) \u2192 O (\u03c0 \u2219P pb) (\u03c0 \u2219S s) }) in\n \u2200 pb\u2080 s\u2080 pb\u2081 s\u2081 \u2192\n check pb\u2080 s\u2080 \u2261 check pb\u2081 s\u2081 \u2192\n (O : _ \u2192 _ \u2192 Bit) \u2192 otp O pb\u2080 s\u2080 \u2261 otp O pb\u2081 s\u2081\n otp-\u2219 pb\u2080 s\u2080 pb\u2081 s\u2081 check-pf O = {!(\u03bc\u03c0 \u00d7Sum-proj\u2082 \u03bcR\u209a-xtra ?!}\n-}\n Answer : Bit \u2192 \u2605\n Answer false{-0b-} = Permutation\n Answer true {-1b-} = Solution\n\n answer : Permutation \u2192 Solution \u2192 \u2200 b \u2192 Answer b\n answer \u03c0 _ false = \u03c0\n answer _ s true = s\n\n -- The prover is the advesary in the generic terminology,\n -- and the verifier is the challenger.\n DepProver : \u2605\n DepProver = Problem \u2192 \u21ba R\u209a \u2192 (b : Bit) \u2192 Problem \u00d7 Answer b\n\n Prover\u2080 : \u2605\n Prover\u2080 = Problem \u2192 \u21ba R\u209a \u2192 Problem \u00d7 Permutation\n\n Prover\u2081 : \u2605\n Prover\u2081 = Problem \u2192 \u21ba R\u209a \u2192 Problem \u00d7 Solution\n\n Prover : \u2605\n Prover = Prover\u2080 \u00d7 Prover\u2081\n\n prover : DepProver \u2192 Prover\n prover dpr = (\u03bb pb r \u2192 dpr pb r 0b) , (\u03bb pb r \u2192 dpr pb r 1b)\n\n depProver : Prover \u2192 DepProver\n depProver (pr\u2080 , pr\u2081) pb r false = pr\u2080 pb r\n depProver (pr\u2080 , pr\u2081) pb r true = pr\u2081 pb r\n\n -- Here we show that the explicit commitment step seems useless given\n -- the formalization. The verifier can \"trust\" the prover on the fact\n -- that any choice is going to be govern only by the problem and the\n -- randomness.\n module WithCommitment (Commitment : \u2605)\n (AnswerWC : Bit \u2192 \u2605)\n (reveal : \u2200 b \u2192 Commitment \u2192 AnswerWC b \u2192 Problem \u00d7 Answer b) where\n ProverWC = (Problem \u2192 R\u209a \u2192 Commitment)\n \u00d7 (Problem \u2192 R\u209a \u2192 (b : Bit) \u2192 AnswerWC b)\n\n depProver' : ProverWC \u2192 DepProver\n depProver' (pr\u2080 , pr\u2081) pb (rand r\u209a) b = reveal b (pr\u2080 pb r\u209a) (pr\u2081 pb r\u209a b)\n\n Verif : Problem \u2192 \u2200 b \u2192 Problem \u00d7 Answer b \u2192 Bit\n Verif pb false{-0b-} (\u03c0\u2219pb , \u03c0) = (\u03c0 \u2219P pb) == \u03c0\u2219pb\n Verif pb true {-1b-} (\u03c0\u2219pb , \u03c0\u2219s) = check \u03c0\u2219pb \u03c0\u2219s\n\n _\u21c4\u2032_ : Problem \u2192 DepProver \u2192 Bit \u2192 \u21ba R\u209a \u2192 Bit\n (pb \u21c4\u2032 pr) b (rand r\u209a) = Verif pb b (pr pb (rand r\u209a) b)\n\n _\u21c4_ : Problem \u2192 DepProver \u2192 \u21ba (Bit \u00d7 R\u209a) \u2192 Bit\n (pb \u21c4 pr) (rand (b , r\u209a)) = (pb \u21c4\u2032 pr) b (rand r\u209a)\n\n _\u21c4''_ : Problem \u2192 Prover \u2192 \u21ba (Bit \u00d7 R\u209a) \u2192 Bit\n pb \u21c4'' pr = pb \u21c4 depProver pr\n\n honest : (Problem \u2192 Maybe Solution) \u2192 DepProver\n honest solve pb (rand (\u03c0 , r\u209a)) b = (\u03c0 \u2219P pb , answer \u03c0 sol b)\n module Honest where\n sol : Solution\n sol with solve pb\n ... | just sol = \u03c0 \u2219S sol\n ... | nothing = \u03c0 \u2219S easy-sol \u03c0\n\n module WithCorrectSolver (pb : Problem)\n (s : Solution)\n (check-s : check pb s \u2261 true)\n where\n\n -- When the honest prover has a solution, he gets accepted\n -- unconditionally by the verifier.\n honest-accepted : \u2200 r \u2192 (pb \u21c4 honest (const (just s))) r \u2261 1b\n honest-accepted (rand (true , \u03c0 , r\u209a)) rewrite check-\u2219 pb s \u03c0 = check-s\n honest-accepted (rand (false , \u03c0 , r\u209a)) = ==-refl\n\n honest-\u2141 = \u03bb pb s \u2192 (pb \u21c4 honest (const (just s)))\n\n module HonestLeakZeroKnowledge (pb\u2080 pb\u2081 : Problem)\n (s\u2080 s\u2081 : Solution)\n (check-pf : check pb\u2080 s\u2080 \u2261 check pb\u2081 s\u2081) where\n\n helper : \u2200 r\u209a \u2192 Bool.to\u2115 ((pb\u2080 \u21c4\u2032 honest (const (just s\u2080))) 0b (rand r\u209a))\n \u2261 Bool.to\u2115 ((pb\u2081 \u21c4\u2032 honest (const (just s\u2081))) 0b (rand r\u209a))\n helper (\u03c0 , r\u209a) rewrite ==-refl {\u03c0 \u2219P pb\u2080} | ==-refl {\u03c0 \u2219P pb\u2081} = refl\n\n honest-leak : honest-\u2141 pb\u2080 s\u2080 \u2261# honest-\u2141 pb\u2081 s\u2081\n honest-leak rewrite otp-\u2219-check pb\u2080 s\u2080 pb\u2081 s\u2081 check-pf | sum-ext \u03bcR\u209a helper = refl\n\n module HonestLeakZeroKnowledge' (pb : Problem)\n (s\u2080 s\u2081 : Solution)\n (check-pf : check pb s\u2080 \u2261 check pb s\u2081) where\n\n honest-leak : honest-\u2141 pb s\u2080 \u2261# honest-\u2141 pb s\u2081\n honest-leak = HonestLeakZeroKnowledge.honest-leak pb pb s\u2080 s\u2081 check-pf\n\n -- Predicts b=b\u2032\n cheater : \u2200 b\u2032 \u2192 DepProver\n cheater b\u2032 pb (rand (\u03c0 , _)) b = \u03c0 \u2219P (case b\u2032 0\u2192 pb 1\u2192 easy-pb \u03c0)\n , answer \u03c0 (\u03c0 \u2219S easy-sol \u03c0) b\n\n -- If cheater predicts correctly, verifer accepts him\n cheater-accepted : \u2200 b pb r\u209a \u2192 (pb \u21c4\u2032 cheater b) b r\u209a \u2261 1b\n cheater-accepted true pb (rand (\u03c0 , r\u209a)) = check-easy \u03c0\n cheater-accepted false pb (rand (\u03c0 , r\u209a)) = ==-refl\n\n -- If cheater predicts incorrecty, verifier rejects him\n module CheaterRejected (pb : Problem)\n (not-easy-sol : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P pb) (\u03c0 \u2219S easy-sol \u03c0) \u2261 false)\n (not-easy-pb : \u2200 \u03c0 \u2192 ((\u03c0 \u2219P pb) == (\u03c0 \u2219P easy-pb \u03c0)) \u2261 false) where\n\n cheater-rejected : \u2200 b r\u209a \u2192 (pb \u21c4\u2032 cheater (not b)) b r\u209a \u2261 0b\n cheater-rejected true (rand (\u03c0 , r\u209a)) = not-easy-sol \u03c0\n cheater-rejected false (rand (\u03c0 , r\u209a)) = not-easy-pb \u03c0\n\nmodule DLog (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u229f_ : \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n (\u229f-\u229e : \u2200 \u03c0 x \u2192 (\u229f \u03c0) \u229e (\u03c0 \u229e x) \u2261 x)\n (^\u229f-\u2219 : \u2200 \u03b1 \u03b2 x \u2192 ((\u03b1 ^ (\u229f x)) \u2219 ((\u03b1 ^ x) \u2219 \u03b2)) \u2261 \u03b2)\n -- (\u2219-assoc : \u2200 \u03b1 \u03b2 \u03b3 \u2192 \u03b1 \u2219 (\u03b2 \u2219 \u03b3) \u2261 (\u03b1 \u2219 \u03b2) \u2219 \u03b3)\n (dist-^-\u229e : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u229e y) \u2261 (\u03b1 ^ x) \u2219 (\u03b1 ^ y))\n (_==_ : G \u2192 G \u2192 Bool)\n (==-refl : \u2200 {\u03b1} \u2192 (\u03b1 == \u03b1) \u2261 true)\n (==-cong-\u2219 : \u2200 {\u03b1 \u03b2 b} \u03b3 \u2192 \u03b1 == \u03b2 \u2261 b \u2192 (\u03b3 \u2219 \u03b1) == (\u03b3 \u2219 \u03b2) \u2261 b)\n (==-true : \u2200 {\u03b1 \u03b2} \u2192 \u03b1 == \u03b2 \u2261 true \u2192 \u03b1 \u2261 \u03b2)\n (sum\u2124q : Sum \u2124q)\n (\u03bc\u2124q : SumProp sum\u2124q)\n (R\u209a-xtra : \u2605) -- extra prover\/adversary randomness\n (sumR\u209a-xtra : Sum R\u209a-xtra)\n (\u03bcR\u209a-xtra : SumProp sumR\u209a-xtra)\n (some-\u2124q : \u2124q)\n where\n\n Permutation = \u2124q\n Problem = G\n Solution = \u2124q\n\n _\u207b\u00b9 : Endo Permutation\n \u03c0 \u207b\u00b9 = \u229f \u03c0\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n _\u2219P_ : Permutation \u2192 Endo Problem\n \u03c0 \u2219P p = g^ \u03c0 \u2219 p\n\n \u207b\u00b9-inverseP : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219P (\u03c0 \u2219P x) \u2261 x\n \u207b\u00b9-inverseP \u03c0 x rewrite ^\u229f-\u2219 g x \u03c0 = refl\n\n _\u2219S_ : Permutation \u2192 Endo Solution\n \u03c0 \u2219S s = \u03c0 \u229e s\n\n \u207b\u00b9-inverseS : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219S (\u03c0 \u2219S x) \u2261 x\n \u207b\u00b9-inverseS = \u229f-\u229e\n\n check : Problem \u2192 Solution \u2192 Bit\n check p s = p == g^ s\n\n check-\u2219' : \u2200 p s \u03c0 b \u2192 check p s \u2261 b \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 b\n check-\u2219' p s \u03c0 true check-p-s rewrite dist-^-\u229e g \u03c0 s | ==-true check-p-s = ==-refl\n check-\u2219' p s \u03c0 false check-p-s rewrite dist-^-\u229e g \u03c0 s = ==-cong-\u2219 (g^ \u03c0) check-p-s\n\n check-\u2219 : \u2200 p s \u03c0 \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 check p s\n check-\u2219 p s \u03c0 = check-\u2219' p s \u03c0 (check p s) refl\n\n easy-sol : Permutation \u2192 Solution\n easy-sol \u03c0 = some-\u2124q\n\n easy-pb : Permutation \u2192 Problem\n easy-pb \u03c0 = g^(easy-sol \u03c0)\n\n check-easy : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P easy-pb \u03c0) (\u03c0 \u2219S easy-sol \u03c0) \u2261 true\n check-easy \u03c0 rewrite dist-^-\u229e g \u03c0 (easy-sol \u03c0) = ==-refl\n\n open M Permutation _\u207b\u00b9 sum\u2124q \u03bc\u2124q R\u209a-xtra sumR\u209a-xtra \u03bcR\u209a-xtra\n Problem _==_ ==-refl _\u2219P_ \u207b\u00b9-inverseP Solution _\u2219S_ \u207b\u00b9-inverseS check check-\u2219 easy-pb easy-sol check-easy\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"8569ad557abf314286b4a14381efb48631f72316","subject":"IDesc model: proof that embedded IDesc and defined IDesc are isomorph (with extensionality axiom).","message":"IDesc model: proof that embedded IDesc and defined IDesc are isomorph (with extensionality axiom).","repos":"brixen\/Epigram,kwangkim\/pigment,kwangkim\/pigment,kwangkim\/pigment","old_file":"models\/IDesc_type_type.agda","new_file":"models\/IDesc_type_type.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : (A : Set)(B : Set)(f : A -> B)(x y : A) -> x == y -> f x == f y\ncong A B f x .x refl = refl\n\ncong2 : (A B C : Set)(f : A -> B -> C)(x y : A)(z t : B) -> \n x == y -> z == t -> f x z == f y t\ncong2 A B C f x .x z .z refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : (A : Set)(B : Set)(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : (I : Set) -> IDesc I -> (I -> Set) -> Set\ndesc I (var i) P = P i\ndesc I (const X) P = X\ndesc I (prod D D') P = desc I D P * desc I D' P\ndesc I (sigma S T) P = Sigma S (\\s -> desc I (T s) P)\ndesc I (pi S T) P = (s : S) -> desc I (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu (I : Set)(R : I -> IDesc I)(i : I) : Set where\n con : desc I (R i) (\\j -> IMu I R j) -> IMu I R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : (I : Set)(D : IDesc I)(P : I -> Set) -> desc I D P -> IDesc (Sigma I P)\nbox I (var i) P x = var (i , x)\nbox I (const X) P x = const X\nbox I (prod D D') P (d , d') = prod (box I D P d) (box I D' P d')\nbox I (sigma S T) P (a , b) = box I (T a) P b\nbox I (pi S T) P f = pi S (\\s -> box I (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu I R) -> Set)\n (m : (i : I)\n (xs : desc I (R i) (IMu I R))\n (hs : desc (Sigma I (IMu I R)) (box I (R i) (IMu I R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu I R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc I D (IMu I R)) -> \n desc (Sigma I (IMu I R)) (box I D (IMu I R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu I R) -> Set)\n (m : (i : I)\n (xs : desc I (R i) (IMu I R))\n (hs : desc (Sigma I (IMu I R)) (box I (R i) (IMu I R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu I R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu Unit (\\_ -> descD I) Void\n\nvarl : (I : Set)(i : I) -> IDescl I\nvarl I i = con (lvar , i)\n\nconstl : (I : Set)(X : Set) -> IDescl I\nconstl I X = con (lconst , X)\n\nprodl : (I : Set)(D D' : IDescl I) -> IDescl I\nprodl I D D' = con (lprod , (D , D'))\n\npil : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\npil I S T = con (lpi , ( S , T))\n\nsigmal : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal I S T = con (lsigma , ( S , T))\n\ncases : (I : Set) \n (xs : desc Unit (descD I) (IMu Unit (\u03bb _ -> descD I)))\n (hs : desc (Sigma Unit (IMu Unit (\u03bb _ -> descD I)))\n (box Unit (descD I) (IMu Unit (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases I ( lvar , i ) hs = var i\ncases I ( lconst , X ) hs = const X\ncases I ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases I ( lpi , ( S , T ) ) hs = pi S hs\ncases I ( lsigma , ( S , T ) ) hs = sigma S hs\n\niso1 : (I : Set) -> IDescl I -> IDesc I\niso1 I d = induction Unit (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases I) Void d\n\niso2 : (I : Set) -> IDesc I -> IDescl I\niso2 I (var i) = varl I i\niso2 I (const X) = constl I X\niso2 I (prod D D') = prodl I (iso2 I D) (iso2 I D')\niso2 I (pi S T) = pil I S (\\s -> iso2 I (T s))\niso2 I (sigma S T) = sigmal I S (\\s -> iso2 I (T s))\n\n\n\nproof-iso1-iso2 : (I : Set) -> (D : IDesc I) -> iso1 I (iso2 I D) == D\nproof-iso1-iso2 I (var i) = refl\nproof-iso1-iso2 I (const x) = refl\nproof-iso1-iso2 I (prod D D') with proof-iso1-iso2 I D | proof-iso1-iso2 I D'\n... | p | q = cong2 (IDesc I) \n (IDesc I)\n (IDesc I) \n prod \n (iso1 I (iso2 I D))\n D \n (iso1 I (iso2 I D'))\n D' p q\nproof-iso1-iso2 I (pi S T) = cong (S \u2192 IDesc I)\n (IDesc I)\n (pi S) \n (\\x -> iso1 I (iso2 I (T x)))\n T \n (reflFun S (IDesc I)\n (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\nproof-iso1-iso2 I (sigma S T) = cong (S \u2192 IDesc I)\n (IDesc I)\n (sigma S) \n (\\x -> iso1 I (iso2 I (T x)))\n T \n (reflFun S \n (IDesc I)\n (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\n\n\n-- induction : (I : Set)\n-- (R : I -> IDesc I)\n-- (P : Sigma I (IMu I R) -> Set)\n-- (m : (i : I)\n-- (xs : desc I (R i) (IMu I R))\n-- (hs : desc (Sigma I (IMu I R)) (box I (R i) (IMu I R) xs) P) ->\n-- P ( i , con xs)) ->\n-- (i : I)(x : IMu I R i) -> P ( i , x )\n\n\nP : (I : Set) -> Sigma Unit (IMu Unit (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = iso2 I (iso1 I D) == D\n\n\nproof-iso2-iso1 : (I : Set) -> (D : IDescl I) -> iso2 I (iso1 I D) == D\nproof-iso2-iso1 I D = induction Unit \n (\u03bb x \u2192 sigma DescDConst (descDChoice I))\n (P I)\n ( proof-iso2-iso1-casesW I) \n Void\n D\n where proof-iso2-iso1-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc Unit (descDChoice I s)\n (IMu Unit (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc (Sigma Unit (IMu Unit (\u03bb x \u2192 sigma DescDConst (descDChoice I))))\n (box Unit (sigma DescDConst (descDChoice I))\n (IMu Unit (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-iso2-iso1-cases I (lvar , i) hs = refl\n proof-iso2-iso1-cases I (lconst , x) hs = refl\n proof-iso2-iso1-cases I (lprod , ( D , D' )) hs with proof-iso2-iso1 I D | proof-iso2-iso1 I D' \n ... | p | q = cong2 (IDescl I) \n (IDescl I) \n (IDescl I)\n (prodl I) \n (iso2 I (iso1 I D))\n D \n (iso2 I (iso1 I D'))\n D' p q\n proof-iso2-iso1-cases I (lpi , ( S , T )) hs = cong (S \u2192 IDescl I)\n (IDescl I)\n (pil I S) \n (\\x -> iso2 I (iso1 I (T x)))\n T \n (reflFun S \n (IDescl I)\n (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n (\\s -> proof-iso2-iso1 I (T s)))\n proof-iso2-iso1-cases I (lsigma , ( S , T )) hs = cong (S \u2192 IDescl I)\n (IDescl I)\n (sigmal I S) \n (\\x -> iso2 I (iso1 I (T x)))\n T \n (reflFun S \n (IDescl I)\n (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n (\\s -> proof-iso2-iso1 I (T s)))\n proof-iso2-iso1-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc Unit (descDChoice I s)\n (IMu Unit (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc (Sigma Unit (IMu Unit (\u03bb x \u2192 sigma DescDConst (descDChoice I))))\n (box Unit (sigma DescDConst (descDChoice I))\n (IMu Unit (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-iso2-iso1-casesW I Void = proof-iso2-iso1-cases I\n \n\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : (I : Set) -> IDesc I -> (I -> Set) -> Set\ndesc I (var i) P = P i\ndesc I (const X) P = X\ndesc I (prod D D') P = desc I D P * desc I D' P\ndesc I (sigma S T) P = Sigma S (\\s -> desc I (T s) P)\ndesc I (pi S T) P = (s : S) -> desc I (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu (I : Set)(R : I -> IDesc I)(i : I) : Set where\n con : desc I (R i) (\\j -> IMu I R j) -> IMu I R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : (I : Set)(D : IDesc I)(P : I -> Set) -> desc I D P -> IDesc (Sigma I P)\nbox I (var i) P x = var (i , x)\nbox I (const X) P x = const X\nbox I (prod D D') P (d , d') = prod (box I D P d) (box I D' P d')\nbox I (sigma S T) P (a , b) = box I (T a) P b\nbox I (pi S T) P f = pi S (\\s -> box I (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu I R) -> Set)\n (m : (i : I)\n (xs : desc I (R i) (IMu I R))\n (hs : desc (Sigma I (IMu I R)) (box I (R i) (IMu I R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu I R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc I D (IMu I R)) -> \n desc (Sigma I (IMu I R)) (box I D (IMu I R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu I R) -> Set)\n (m : (i : I)\n (xs : desc I (R i) (IMu I R))\n (hs : desc (Sigma I (IMu I R)) (box I (R i) (IMu I R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu I R i) -> P ( i , x )\ninduction = Elim.induction\n\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu Unit (\\_ -> descD I) Void\n\nvarl : (I : Set)(i : I) -> IDescl I\nvarl I i = con (lvar , i)\n\nconstl : (I : Set)(X : Set) -> IDescl I\nconstl I X = con (lconst , X)\n\nprodl : (I : Set)(D D' : IDescl I) -> IDescl I\nprodl I D D' = con (lprod , (D , D'))\n\npil : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\npil I S T = con (lpi , ( S , T))\n\nsigmal : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal I S T = con (lsigma , ( S , T))","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"fc4cee02dceacd367ce3a7cbb29f46b050702018","subject":"finished lem2-5-2","message":"finished lem2-5-2\n","repos":"goodlyrottenapple\/lamYcalc","old_file":"Agda\/Reduction.agda","new_file":"Agda\/Reduction.agda","new_contents":"module Reduction where\n\nopen import Data.Empty\nopen import Data.Nat\nopen import Data.List\nopen import Data.List.Any as Any\nopen Any.Membership-\u2261\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Product\n\nopen import Core\nopen import Core-Lemmas\n\n_\u219d_ : Set -> Set -> Set\u2081\nA \u219d B = A -> B -> Set\n\n\ndata _->\u03b2_ : PTerm \u219d PTerm where\n redL : \u2200 {n m m'} -> Term n \u2192 m ->\u03b2 m' -> app m n ->\u03b2 app m' n\n redR : \u2200 {m n n'} -> Term m \u2192 n ->\u03b2 n' -> app m n ->\u03b2 app m n'\n abs : \u2200 L {m m'} -> ( \u2200 {x} -> x \u2209 L -> (m ^' x) ->\u03b2 (m' ^' x) ) ->\n lam m ->\u03b2 lam m'\n beta : \u2200 {m n} -> Term (lam m) -> Term n -> app (lam m) n ->\u03b2 (m ^ n)\n Y : \u2200 {m \u03c3} -> Term m -> app (Y \u03c3) m ->\u03b2 app m (app (Y \u03c3) m)\n\n-- test : \u2200 {x} -> lam (app (lam (bv 0)) (fv x)) ->\u03b2 lam (fv x)\n-- test = abs [] (\u03bb _ -> beta (lam [] (\u03bb x\u2209L -> var)) var)\n\ndata _->||_ : PTerm \u219d PTerm where\n refl : \u2200 {x} -> fv x ->|| fv x\n reflY : \u2200 {\u03c3} -> Y \u03c3 ->|| Y \u03c3\n app : \u2200 {m m' n n'} -> m ->|| m' -> n ->|| n' -> app m n ->|| app m' n'\n abs : \u2200 L {m m'} -> ( \u2200 {x} -> x \u2209 L -> (m ^' x) ->|| (m' ^' x) ) ->\n lam m ->|| lam m'\n beta : \u2200 L {m m' n n'} -> (cf : \u2200 {x} -> x \u2209 L -> (m ^' x) ->|| (m' ^' x) ) -> n ->|| n' ->\n (app (lam m) n) ->|| (m' ^ n')\n Y : \u2200 {m m' \u03c3} -> m ->|| m' -> app (Y \u03c3) m ->|| app m' (app (Y \u03c3) m')\n\n\ndata NotAbsY : PTerm -> Set where\n fv : \u2200 {x} -> NotAbsY (fv x)\n bv : \u2200 {n} -> NotAbsY (bv n)\n app : \u2200 {t1 t2} -> NotAbsY (app t1 t2)\n\n\ndata _>>>_ : PTerm \u219d PTerm where\n refl : \u2200 {x} -> fv x >>> fv x\n reflY : \u2200 {\u03c3} -> Y \u03c3 >>> Y \u03c3\n app : \u2200 {m m' n n'} -> NotAbsY m -> m >>> m' -> n >>> n' -> app m n >>> app m' n'\n abs : \u2200 L {m m'} -> ( \u2200 {x} -> x \u2209 L -> (m ^' x) >>> (m' ^' x) ) ->\n lam m >>> lam m'\n beta : \u2200 L {m m' n n'} -> (cf : \u2200 {x} -> x \u2209 L -> (m ^' x) >>> (m' ^' x) ) -> n >>> n' ->\n app (lam m) n >>> (m' ^ n')\n Y : \u2200 {m m' \u03c3} -> m >>> m' -> app (Y \u03c3) m >>> app m' (app (Y \u03c3) m')\n\n\nsubst-Term : \u2200 {x e u} -> Term e -> Term u -> Term (e [ x ::= u ])\nsubst-Term {x} {_} {u} (var {y}) Term-u with x \u225f y\nsubst-Term var Term-u | yes refl = Term-u\nsubst-Term var Term-u | no p = var\nsubst-Term {x} {_} {u} (lam L {e} cf) Term-u = lam (x \u2237 L) body\n where\n body : {y : \u2115} -> y \u2209 x \u2237 L \u2192 Term ((e [ x ::= u ]) ^' y)\n body {y} y\u2209x\u2237L rewrite subst-open-var y x u e (fv-x\u2260y y x y\u2209x\u2237L) Term-u =\n subst-Term (cf (\u03bb z \u2192 y\u2209x\u2237L (there z))) Term-u\n\nsubst-Term (app Term-e Term-e\u2081) Term-u =\n app (subst-Term Term-e Term-u) (subst-Term Term-e\u2081 Term-u)\nsubst-Term Y Term-u = Y\n\n\n^-Term : \u2200 {m n} -> Term (lam m) -> Term n -> Term (m ^ n)\n^-Term {m} {n} (lam L cf) Term-n = body\n where\n y = \u2203fresh (L ++ FV m)\n\n y\u2209 : y \u2209 (L ++ FV m)\n y\u2209 = \u2203fresh-spec (L ++ FV m)\n\n body : Term (m ^ n)\n body rewrite subst-intro y n m (\u2209-cons-r L (FV m) y\u2209) Term-n =\n subst-Term {y} {m ^' y} {n} (cf (\u2209-cons-l L (FV m) y\u2209)) Term-n\n\n\n->||-Term-l : \u2200 {m m'} -> m ->|| m' -> Term m\n->||-Term-l refl = var\n->||-Term-l reflY = Y\n->||-Term-l (app m->||m' m->||m'') = app (->||-Term-l m->||m') (->||-Term-l m->||m'')\n->||-Term-l (abs L x) = lam L (\u03bb x\u2209L \u2192 ->||-Term-l (x x\u2209L))\n->||-Term-l (beta L x m->||m') =\n app (lam L (\u03bb {x\u2081} x\u2209L \u2192 ->||-Term-l (x x\u2209L)))\n (->||-Term-l m->||m')\n->||-Term-l (Y {m} {m'} m->||m') = app Y (->||-Term-l m->||m')\n\n\n->||-Term-r : \u2200 {m m'} -> m ->|| m' -> Term m'\n->||-Term-r refl = var\n->||-Term-r reflY = Y\n->||-Term-r (app m->||m' m->||m'') = app (->||-Term-r m->||m') (->||-Term-r m->||m'')\n->||-Term-r (abs L x) = lam L (\u03bb x\u2209L \u2192 ->||-Term-r (x x\u2209L))\n->||-Term-r (beta L {m} {m'} {n} {n'} cf m->||m') =\n ^-Term {m'} {n'} (lam L (\u03bb {x\u2081} x\u2209L \u2192 ->||-Term-r (cf x\u2209L))) (->||-Term-r m->||m')\n->||-Term-r (Y m->||m') = app (->||-Term-r m->||m') (app Y (->||-Term-r m->||m'))\n\n\n>>>-Term-l : \u2200 {m m'} -> m >>> m' -> Term m\n>>>-Term-l refl = var\n>>>-Term-l reflY = Y\n>>>-Term-l (app nAbsY m>>>m' m>>>m'') = app (>>>-Term-l m>>>m') (>>>-Term-l m>>>m'')\n>>>-Term-l (abs L x) = lam L (\u03bb x\u2209L \u2192 >>>-Term-l (x x\u2209L))\n>>>-Term-l (beta L x m>>>m') =\n app (lam L (\u03bb {x\u2081} x\u2209L \u2192 >>>-Term-l (x x\u2209L)))\n (>>>-Term-l m>>>m')\n>>>-Term-l (Y {m} {m'} m>>>m') = app Y (>>>-Term-l m>>>m')\n\n\n>>>-Term-r : \u2200 {m m'} -> m >>> m' -> Term m'\n>>>-Term-r refl = var\n>>>-Term-r reflY = Y\n>>>-Term-r (app nAbsY m>>>m' m>>>m'') = app (>>>-Term-r m>>>m') (>>>-Term-r m>>>m'')\n>>>-Term-r (abs L x) = lam L (\u03bb x\u2209L \u2192 >>>-Term-r (x x\u2209L))\n>>>-Term-r (beta L {m} {m'} {n} {n'} cf m>>>m') =\n ^-Term {m'} {n'} (lam L (\u03bb {x\u2081} x\u2209L \u2192 >>>-Term-r (cf x\u2209L))) (>>>-Term-r m>>>m')\n>>>-Term-r (Y m>>>m') = app (>>>-Term-r m>>>m') (app Y (>>>-Term-r m>>>m'))\n\n\nlem2-5-1 : \u2200 s s' t t' (x : \u2115) -> s ->|| s' -> t ->|| t' ->\n (s [ x ::= t ]) ->|| (s' [ x ::= t' ])\nlem2-5-1 _ _ t t' y (refl {x}) t->||t' with x \u225f y\nlem2-5-1 .(fv x) .(fv x) t t' x refl t->||t' | yes refl rewrite\n fv-subst-eq x x t refl | fv-subst-eq x x t' refl = t->||t'\nlem2-5-1 _ _ t t' y (refl {x}) t->||t' | no x\u2260y rewrite\n fv-subst-neq x y t x\u2260y | fv-subst-neq x y t' x\u2260y = refl\nlem2-5-1 _ _ t t' x reflY t->||t' = reflY\nlem2-5-1 _ _ t t' x (app {m} {m'} {n} {n'} ss' ss'') t->||t' = app (lem2-5-1 m m' t t' x ss' t->||t') (lem2-5-1 n n' t t' x ss'' t->||t')\nlem2-5-1 _ _ t t' x (abs L {m} {m'} cf) t->||t' = abs (x \u2237 L) body\n where\n x\u2209FVy : (y : \u2115) -> (y \u2209 x \u2237 L) -> x \u2209 FV (fv y)\n x\u2209FVy y y\u2209x\u2237L x\u2208FVy with fv-x\u2261y x y x\u2208FVy\n x\u2209FVy .x y\u2209x\u2237L x\u2208FVy | refl = y\u2209x\u2237L (here refl)\n\n body : {y : \u2115} -> (y \u2209 x \u2237 L) -> ((m [ x ::= t ]) ^' y) ->|| ((m' [ x ::= t' ]) ^' y)\n body {y} y\u2209x\u2237L rewrite\n subst-fresh2 x (fv y) t (x\u2209FVy y y\u2209x\u2237L) |\n subst-open2 x t 0 (fv y) m (->||-Term-l t->||t') |\n subst-fresh x (fv y) t (x\u2209FVy y y\u2209x\u2237L) |\n subst-fresh2 x (fv y) t' (x\u2209FVy y y\u2209x\u2237L) |\n subst-open2 x t' 0 (fv y) m' (->||-Term-r t->||t') |\n subst-fresh x (fv y) t' (x\u2209FVy y y\u2209x\u2237L) =\n lem2-5-1 (m ^' y) (m' ^' y) t t' x (cf (\u03bb z \u2192 y\u2209x\u2237L (there z))) t->||t'\n\nlem2-5-1 _ _ t t' x (beta L {m} {m'} {n} {n'} cf n->||n') t->||t' rewrite\n subst-open x t' 0 n' m' (->||-Term-r t->||t') =\n beta (x \u2237 L) {_} {m' [ x ::= t' ]} body body\u2082\n where\n body : {y : \u2115} -> y \u2209 x \u2237 L -> ((m [ x ::= t ]) ^' y) ->|| ((m' [ x ::= t' ]) ^' y)\n body {y} y\u2209x\u2237L rewrite\n subst-open-var y x t m (fv-x\u2260y y x y\u2209x\u2237L) (->||-Term-l t->||t') |\n subst-open-var y x t' m' (fv-x\u2260y y x y\u2209x\u2237L) (->||-Term-r t->||t') =\n lem2-5-1 (m ^' y) (m' ^' y) t t' x (cf (\u03bb z \u2192 y\u2209x\u2237L (there z))) t->||t'\n\n body\u2082 : (n [ x ::= t ]) ->|| (n' [ x ::= t' ])\n body\u2082 = lem2-5-1 n n' t t' x n->||n' t->||t'\n\nlem2-5-1 _ _ t t' x (Y {m} {m'} ss') t->||t' = Y (lem2-5-1 m m' t t' x ss' t->||t')\n\n\nlem2-5-1-^ : \u2200 s s' t t' L -> (\u2200 {x} -> x \u2209 L -> (s ^' x) ->|| (s' ^' x)) -> t ->|| t' ->\n (s ^ t) ->|| (s' ^ t')\nlem2-5-1-^ s s' t t' L cf t->||t' = body\n where\n x = \u2203fresh (L ++ FV s ++ FV s')\n\n x\u2209 : x \u2209 (L ++ FV s ++ FV s')\n x\u2209 = \u2203fresh-spec (L ++ FV s ++ FV s')\n\n body : (s ^ t) ->|| (s' ^ t')\n body rewrite\n subst-intro x t s (\u2209-cons-l (FV s) (FV s') (\u2209-cons-r L (FV s ++ FV s') x\u2209)) (->||-Term-l t->||t') |\n subst-intro x t' s' (\u2209-cons-r (FV s) (FV s') (\u2209-cons-r L (FV s ++ FV s') x\u2209)) (->||-Term-r t->||t') =\n lem2-5-1 (s ^' x) (s' ^' x) t t' x (cf (\u2209-cons-l L (FV s ++ FV s') x\u2209)) t->||t'\n\n\nNotAbsY-subst : \u2200 {x y m} -> NotAbsY m -> NotAbsY (m [ x ::= fv y ])\nNotAbsY-subst {x} (fv {y}) with x \u225f y\nNotAbsY-subst {x} {y} fv | yes refl rewrite\n fv-subst-eq x x (fv y) refl = fv\nNotAbsY-subst fv | no _ = fv\nNotAbsY-subst bv = bv\nNotAbsY-subst app = app\n\n\nlem2-5-1>>> : \u2200 s s' (x y : \u2115) -> s >>> s' -> (s [ x ::= fv y ]) >>> (s' [ x ::= fv y ])\nlem2-5-1>>> _ _ x y (refl {z}) with x \u225f z\nlem2-5-1>>> .(fv x) .(fv x) x y refl | yes refl rewrite\n fv-subst-eq x x (fv y) refl = refl\nlem2-5-1>>> _ _ x y (refl {z}) | no z\u2260x = refl\nlem2-5-1>>> _ _ x y reflY = reflY\nlem2-5-1>>> _ _ x y (app {m} {m'} {n} {n'} \u00acabsY ss' ss'') =\n app (NotAbsY-subst \u00acabsY) (lem2-5-1>>> m m' x y ss') (lem2-5-1>>> n n' x y ss'')\nlem2-5-1>>> _ _ x y (abs L {m} {m'} cf) = abs (x \u2237 L) body\n where\n x\u2209FVz : (z : \u2115) -> (z \u2209 x \u2237 L) -> x \u2209 FV (fv z)\n x\u2209FVz z z\u2209x\u2237L x\u2208FVz with fv-x\u2261y x z x\u2208FVz\n x\u2209FVz .x z\u2209x\u2237L x\u2208FVz | refl = z\u2209x\u2237L (here refl)\n\n body : {z : \u2115} -> (z \u2209 x \u2237 L) -> ((m [ x ::= fv y ]) ^' z) >>> ((m' [ x ::= fv y ]) ^' z)\n body {z} z\u2209x\u2237L rewrite\n subst-fresh2 x (fv z) (fv y) (x\u2209FVz z z\u2209x\u2237L) |\n subst-open2 x (fv y) 0 (fv z) m var |\n subst-fresh x (fv z) (fv y) (x\u2209FVz z z\u2209x\u2237L) |\n subst-fresh2 x (fv z) (fv y) (x\u2209FVz z z\u2209x\u2237L) |\n subst-open2 x (fv y) 0 (fv z) m' var |\n subst-fresh x (fv z) (fv y) (x\u2209FVz z z\u2209x\u2237L) =\n lem2-5-1>>> (m ^' z) (m' ^' z) x y (cf (\u03bb z\u2081 \u2192 z\u2209x\u2237L (there z\u2081)))\n\nlem2-5-1>>> _ _ x y (beta L {m} {m'} {n} {n'} cf n>>>n') rewrite\n subst-open x (fv y) 0 n' m' var =\n beta (x \u2237 L) {_} {m' [ x ::= fv y ]} body body\u2082\n where\n body : {z : \u2115} -> z \u2209 x \u2237 L -> ((m [ x ::= fv y ]) ^' z) >>> ((m' [ x ::= fv y ]) ^' z)\n body {z} z\u2209x\u2237L rewrite\n subst-open-var z x (fv y) m (fv-x\u2260y z x z\u2209x\u2237L) var |\n subst-open-var z x (fv y) m' (fv-x\u2260y z x z\u2209x\u2237L) var =\n lem2-5-1>>> (m ^' z) (m' ^' z) x y (cf (\u03bb z\u2081 \u2192 z\u2209x\u2237L (there z\u2081)))\n\n body\u2082 : (n [ x ::= fv y ]) >>> (n' [ x ::= fv y ])\n body\u2082 = lem2-5-1>>> n n' x y n>>>n'\n\nlem2-5-1>>> _ _ x y (Y {m} {m'} ss') = Y (lem2-5-1>>> m m' x y ss')\n\n\n*^-^->>> : \u2200 {x y t d k} -> t >>> d -> y \u2209 x \u2237 (FV t ++ FV d) -> ([ k >> fv y ] ([ k << x ] t)) >>> ([ k >> fv y ] ([ k << x ] d))\n*^-^->>> {x} {y} {t} {d} {k} t>>>d y\u2209 rewrite\n *^-^\u2261subst t x y {k} (>>>-Term-l t>>>d) | *^-^\u2261subst d x y {k} (>>>-Term-r t>>>d) = lem2-5-1>>> t d x y t>>>d\n\n\n\u2203>>> : \u2200 {a} -> Term a -> \u2203(\u03bb d -> a >>> d)\n\u2203>>> (var {x}) = fv x , refl\n\u2203>>> (lam L {t} cf) = body\n where\n x = \u2203fresh (L ++ FV t)\n\n x\u2209 : x \u2209 (L ++ FV t)\n x\u2209 = \u2203fresh-spec (L ++ FV t)\n\n d-spec : \u2203(\u03bb d -> (t ^' x) >>> d)\n d-spec = \u2203>>> (cf (\u2209-cons-l L (FV t) x\u2209))\n\n d : PTerm\n d = proj\u2081 d-spec\n\n subst1 : \u2200 x y t k -> x \u2209 FV t -> (t ^' y) \u2261 (([ k << x ] ([ k >> fv x ] t)) ^' y)\n subst1 x y t k x\u2209FVt rewrite fv-^-*^-refl x t {k} x\u2209FVt = refl\n\n cf' : \u2200 {y} -> y \u2209 x \u2237 (FV d ++ FV t) -> (t ^' y) >>> ((* x ^ d) ^' y)\n cf' {y} y\u2209 rewrite subst1 x y t 0 (\u2209-cons-r L (FV t) x\u2209) =\n *^-^->>> {x} {y} {t ^' x} {d} {0} (proj\u2082 d-spec)\n (\u2209-\u2237 x (FV (t ^' x) ++ FV d) (fv-x\u2260y y x y\u2209)\n (\u2209-cons-intro (FV (t ^' x)) (FV d)\n (fv-^ {0} {y} {x} t (\u2209-cons-r (FV d) _ (\u2209-\u2237-elim _ y\u2209)) (fv-x\u2260y _ _ y\u2209))\n (\u2209-cons-l _ (FV t) (\u2209-\u2237-elim _ y\u2209))))\n\n body : \u2203(\u03bb d -> (lam t) >>> d)\n body = lam (* x ^ d) , (abs (x \u2237 (FV d ++ FV t)) cf')\n\n\u2203>>> (app {t1} {t2} trm-t1 trm-t2) with trm-t1 | \u2203>>> trm-t1 | \u2203>>> trm-t2\n\u2203>>> (app trm-t1 trm-t2) | (var {x}) | d1 , t1>>>d1 | d2 , t2>>>d2 = app (fv x) d2 , app fv refl t2>>>d2\n\u2203>>> (app {_} {t2} trm-t1 trm-t2) | lam L cf | _ , abs L\u2081 {t1} {d1} cf\u2081 | d2 , t2>>>d2 =\n d1 ^ d2 , beta L\u2081 {t1} {d1} {t2} {d2} cf\u2081 t2>>>d2\n\u2203>>> (app {_} {t2} trm-t1 trm-t2) | app {p1} {p2} trm-p1 trm-p2 | d1 , t1>>>d1 | d2 , t2>>>d2 =\n app d1 d2 , app app t1>>>d1 t2>>>d2\n\u2203>>> (app trm-t1 trm-t2) | (Y {t}) | d1 , t1>>>d1 | d2 , t2>>>d2 = app d2 (app (Y t) d2) , Y t2>>>d2\n\u2203>>> (Y {t}) = Y t , reflY\n\n\nY->||Y-\u2261 : \u2200 {t t' : Type} -> (Y t) ->|| (Y t') -> (PTerm.Y t) \u2261 (Y t')\nY->||Y-\u2261 reflY = refl\n\n>>>closes->|| : \u2200 {a b d} -> a >>> d -> a ->|| b -> b ->|| d\n>>>closes->|| refl refl = refl\n>>>closes->|| reflY reflY = reflY\n>>>closes->|| (app \u00acabsY al>>>ald ar>>>ard) (app al->||alb ar->||arb) =\n app (>>>closes->|| al>>>ald al->||alb) (>>>closes->|| ar>>>ard ar->||arb)\n>>>closes->|| (app () al>>>ald ar>>>ard) (beta _ _ _)\n>>>closes->|| (app () al>>>ald ar>>>ard) (Y _)\n>>>closes->|| (abs L {a} {d} cf) (abs L\u2081 {.a} {b} cf\u2081) = abs (L ++ L\u2081) ih\n where\n ih : \u2200 {x : \u2115} \u2192 x \u2209 L ++ L\u2081 \u2192 (b ^' x) ->|| (d ^' x)\n ih {x} x\u2209L++L\u2081 = >>>closes->|| (cf (\u2209-cons-l _ _ x\u2209L++L\u2081)) (cf\u2081 (\u2209-cons-r L _ x\u2209L++L\u2081))\n\n>>>closes->|| (beta _ _ _) (app {m' = bv _} () _)\n>>>closes->|| (beta _ _ _) (app {m' = fv _} () _)\n>>>closes->|| (beta L {_} {ald} {ar} {ard} cf ar>>>ard) (app {m' = lam alb} {n' = arb} (abs L\u2081 cf\u2081) ar->||arb) =\n beta (L ++ L\u2081) {m' = ald} ih arb->||ard\n where\n arb->||ard : arb ->|| ard\n arb->||ard = >>>closes->|| ar>>>ard ar->||arb\n\n ih : \u2200 {x} -> x \u2209 L ++ L\u2081 -> (alb ^' x) ->|| (ald ^' x)\n ih {x} x\u2209L++L\u2081 = >>>closes->|| (cf (\u2209-cons-l _ _ x\u2209L++L\u2081)) (cf\u2081 (\u2209-cons-r L _ x\u2209L++L\u2081))\n\n>>>closes->|| (beta _ _ _) (app {m' = app _ _} () _)\n>>>closes->|| (beta _ _ _) (app {m' = Y _} () _)\n>>>closes->|| (beta L {al} {ald} {ar} {ard} cf ar>>>ard) (beta L\u2081 {m' = alb} {n' = arb} cf\u2081 ar->||arb) =\n lem2-5-1-^ alb ald arb ard (L ++ L\u2081) ih arb->||ard\n where\n arb->||ard : arb ->|| ard\n arb->||ard = >>>closes->|| ar>>>ard ar->||arb\n\n ih : \u2200 {x} -> x \u2209 L ++ L\u2081 -> (alb ^' x) ->|| (ald ^' x)\n ih {x} x\u2209L++L\u2081 = >>>closes->|| (cf (\u2209-cons-l _ _ x\u2209L++L\u2081)) (cf\u2081 (\u2209-cons-r L _ x\u2209L++L\u2081))\n\n>>>closes->|| (Y _) (app {m' = bv _} () _)\n>>>closes->|| (Y _) (app {m' = fv _} () _)\n>>>closes->|| (Y _) (app {m' = lam _} () _)\n>>>closes->|| (Y _) (app {m' = app _ _} () _)\n>>>closes->|| (Y {m} {m'} {t} m>>>m') (app {m' = Y t'} {n' = n''} Yt->||Yt' m->||n'') rewrite\n Y->||Y-\u2261 Yt->||Yt' = Y (>>>closes->|| m>>>m' m->||n'')\n>>>closes->|| (Y {m} {m'} {t} m>>>m') (Y {m' = m''} m->||m'') =\n app (>>>closes->|| m>>>m' m->||m'') (app reflY (>>>closes->|| m>>>m' m->||m''))\n\n\nlem2-5-2 : \u2200 {a b c} -> a ->|| b -> a ->|| c -> \u2203(\u03bb d -> b ->|| d \u00d7 c ->|| d)\nlem2-5-2 {a} a->||b a->||c = d , ((>>>closes->|| a>>>d a->||b) , (>>>closes->|| a>>>d a->||c))\n where\n a>>>d-spec : \u2203(\u03bb d -> a >>> d)\n a>>>d-spec = \u2203>>> (->||-Term-l a->||c)\n\n d : PTerm\n d = proj\u2081 a>>>d-spec\n\n a>>>d : a >>> d\n a>>>d = proj\u2082 a>>>d-spec\n","old_contents":"module Reduction where\n\nopen import Data.Empty\nopen import Data.Nat\nopen import Data.List\nopen import Data.List.Any as Any\nopen Any.Membership-\u2261\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Product\n\nopen import Core\nopen import Core-Lemmas\n\n_\u219d_ : Set -> Set -> Set\u2081\nA \u219d B = A -> B -> Set\n\n\ndata _->\u03b2_ : PTerm \u219d PTerm where\n redL : \u2200 {n m m'} -> Term n \u2192 m ->\u03b2 m' -> app m n ->\u03b2 app m' n\n redR : \u2200 {m n n'} -> Term m \u2192 n ->\u03b2 n' -> app m n ->\u03b2 app m n'\n abs : \u2200 L {m m'} -> ( \u2200 {x} -> x \u2209 L -> (m ^' x) ->\u03b2 (m' ^' x) ) ->\n lam m ->\u03b2 lam m'\n beta : \u2200 {m n} -> Term (lam m) -> Term n -> app (lam m) n ->\u03b2 (m ^ n)\n Y : \u2200 {m \u03c3} -> Term m -> app (Y \u03c3) m ->\u03b2 app m (app (Y \u03c3) m)\n\n-- test : \u2200 {x} -> lam (app (lam (bv 0)) (fv x)) ->\u03b2 lam (fv x)\n-- test = abs [] (\u03bb _ -> beta (lam [] (\u03bb x\u2209L -> var)) var)\n\ndata _->||_ : PTerm \u219d PTerm where\n refl : \u2200 {x} -> fv x ->|| fv x\n reflY : \u2200 {\u03c3} -> Y \u03c3 ->|| Y \u03c3\n app : \u2200 {m m' n n'} -> m ->|| m' -> n ->|| n' -> app m n ->|| app m' n'\n abs : \u2200 L {m m'} -> ( \u2200 {x} -> x \u2209 L -> (m ^' x) ->|| (m' ^' x) ) ->\n lam m ->|| lam m'\n beta : \u2200 L {m m' n n'} -> (cf : \u2200 {x} -> x \u2209 L -> (m ^' x) ->|| (m' ^' x) ) -> n ->|| n' ->\n (app (lam m) n) ->|| (m' ^ n')\n Y : \u2200 {m m' \u03c3} -> m ->|| m' -> app (Y \u03c3) m ->|| app m' (app (Y \u03c3) m')\n\n\ndata NotAbsY : PTerm -> Set where\n fv : \u2200 {x} -> NotAbsY (fv x)\n bv : \u2200 {n} -> NotAbsY (bv n)\n app : \u2200 {t1 t2} -> NotAbsY (app t1 t2)\n\n\ndata _>>>_ : PTerm \u219d PTerm where\n refl : \u2200 {x} -> fv x >>> fv x\n reflY : \u2200 {\u03c3} -> Y \u03c3 >>> Y \u03c3\n app : \u2200 {m m' n n'} -> NotAbsY m -> m >>> m' -> n >>> n' -> app m n >>> app m' n'\n abs : \u2200 L {m m'} -> ( \u2200 {x} -> x \u2209 L -> (m ^' x) >>> (m' ^' x) ) ->\n lam m >>> lam m'\n beta : \u2200 L {m m' n n'} -> (cf : \u2200 {x} -> x \u2209 L -> (m ^' x) >>> (m' ^' x) ) -> n >>> n' ->\n app (lam m) n >>> (m' ^ n')\n Y : \u2200 {m m' \u03c3} -> m >>> m' -> app (Y \u03c3) m >>> app m' (app (Y \u03c3) m')\n\n\nsubst-Term : \u2200 {x e u} -> Term e -> Term u -> Term (e [ x ::= u ])\nsubst-Term {x} {_} {u} (var {y}) Term-u with x \u225f y\nsubst-Term var Term-u | yes refl = Term-u\nsubst-Term var Term-u | no p = var\nsubst-Term {x} {_} {u} (lam L {e} cf) Term-u = lam (x \u2237 L) body\n where\n body : {y : \u2115} -> y \u2209 x \u2237 L \u2192 Term ((e [ x ::= u ]) ^' y)\n body {y} y\u2209x\u2237L rewrite subst-open-var y x u e (fv-x\u2260y y x y\u2209x\u2237L) Term-u =\n subst-Term (cf (\u03bb z \u2192 y\u2209x\u2237L (there z))) Term-u\n\nsubst-Term (app Term-e Term-e\u2081) Term-u =\n app (subst-Term Term-e Term-u) (subst-Term Term-e\u2081 Term-u)\nsubst-Term Y Term-u = Y\n\n\n^-Term : \u2200 {m n} -> Term (lam m) -> Term n -> Term (m ^ n)\n^-Term {m} {n} (lam L cf) Term-n = body\n where\n y = \u2203fresh (L ++ FV m)\n\n y\u2209 : y \u2209 (L ++ FV m)\n y\u2209 = \u2203fresh-spec (L ++ FV m)\n\n body : Term (m ^ n)\n body rewrite subst-intro y n m (\u2209-cons-r L (FV m) y\u2209) Term-n =\n subst-Term {y} {m ^' y} {n} (cf (\u2209-cons-l L (FV m) y\u2209)) Term-n\n\n\n->||-Term-l : \u2200 {m m'} -> m ->|| m' -> Term m\n->||-Term-l refl = var\n->||-Term-l reflY = Y\n->||-Term-l (app m->||m' m->||m'') = app (->||-Term-l m->||m') (->||-Term-l m->||m'')\n->||-Term-l (abs L x) = lam L (\u03bb x\u2209L \u2192 ->||-Term-l (x x\u2209L))\n->||-Term-l (beta L x m->||m') =\n app (lam L (\u03bb {x\u2081} x\u2209L \u2192 ->||-Term-l (x x\u2209L)))\n (->||-Term-l m->||m')\n->||-Term-l (Y {m} {m'} m->||m') = app Y (->||-Term-l m->||m')\n\n\n->||-Term-r : \u2200 {m m'} -> m ->|| m' -> Term m'\n->||-Term-r refl = var\n->||-Term-r reflY = Y\n->||-Term-r (app m->||m' m->||m'') = app (->||-Term-r m->||m') (->||-Term-r m->||m'')\n->||-Term-r (abs L x) = lam L (\u03bb x\u2209L \u2192 ->||-Term-r (x x\u2209L))\n->||-Term-r (beta L {m} {m'} {n} {n'} cf m->||m') =\n ^-Term {m'} {n'} (lam L (\u03bb {x\u2081} x\u2209L \u2192 ->||-Term-r (cf x\u2209L))) (->||-Term-r m->||m')\n->||-Term-r (Y m->||m') = app (->||-Term-r m->||m') (app Y (->||-Term-r m->||m'))\n\n\n>>>-Term-l : \u2200 {m m'} -> m >>> m' -> Term m\n>>>-Term-l refl = var\n>>>-Term-l reflY = Y\n>>>-Term-l (app nAbsY m>>>m' m>>>m'') = app (>>>-Term-l m>>>m') (>>>-Term-l m>>>m'')\n>>>-Term-l (abs L x) = lam L (\u03bb x\u2209L \u2192 >>>-Term-l (x x\u2209L))\n>>>-Term-l (beta L x m>>>m') =\n app (lam L (\u03bb {x\u2081} x\u2209L \u2192 >>>-Term-l (x x\u2209L)))\n (>>>-Term-l m>>>m')\n>>>-Term-l (Y {m} {m'} m>>>m') = app Y (>>>-Term-l m>>>m')\n\n\n>>>-Term-r : \u2200 {m m'} -> m >>> m' -> Term m'\n>>>-Term-r refl = var\n>>>-Term-r reflY = Y\n>>>-Term-r (app nAbsY m>>>m' m>>>m'') = app (>>>-Term-r m>>>m') (>>>-Term-r m>>>m'')\n>>>-Term-r (abs L x) = lam L (\u03bb x\u2209L \u2192 >>>-Term-r (x x\u2209L))\n>>>-Term-r (beta L {m} {m'} {n} {n'} cf m>>>m') =\n ^-Term {m'} {n'} (lam L (\u03bb {x\u2081} x\u2209L \u2192 >>>-Term-r (cf x\u2209L))) (>>>-Term-r m>>>m')\n>>>-Term-r (Y m>>>m') = app (>>>-Term-r m>>>m') (app Y (>>>-Term-r m>>>m'))\n\n\nlem2-5-1 : \u2200 s s' t t' (x : \u2115) -> s ->|| s' -> t ->|| t' ->\n (s [ x ::= t ]) ->|| (s' [ x ::= t' ])\nlem2-5-1 _ _ t t' y (refl {x}) t->||t' with x \u225f y\nlem2-5-1 .(fv x) .(fv x) t t' x refl t->||t' | yes refl rewrite\n fv-subst-eq x x t refl | fv-subst-eq x x t' refl = t->||t'\nlem2-5-1 _ _ t t' y (refl {x}) t->||t' | no x\u2260y rewrite\n fv-subst-neq x y t x\u2260y | fv-subst-neq x y t' x\u2260y = refl\nlem2-5-1 _ _ t t' x reflY t->||t' = reflY\nlem2-5-1 _ _ t t' x (app {m} {m'} {n} {n'} ss' ss'') t->||t' = app (lem2-5-1 m m' t t' x ss' t->||t') (lem2-5-1 n n' t t' x ss'' t->||t')\nlem2-5-1 _ _ t t' x (abs L {m} {m'} cf) t->||t' = abs (x \u2237 L) body\n where\n x\u2209FVy : (y : \u2115) -> (y \u2209 x \u2237 L) -> x \u2209 FV (fv y)\n x\u2209FVy y y\u2209x\u2237L x\u2208FVy with fv-x\u2261y x y x\u2208FVy\n x\u2209FVy .x y\u2209x\u2237L x\u2208FVy | refl = y\u2209x\u2237L (here refl)\n\n body : {y : \u2115} -> (y \u2209 x \u2237 L) -> ((m [ x ::= t ]) ^' y) ->|| ((m' [ x ::= t' ]) ^' y)\n body {y} y\u2209x\u2237L rewrite\n subst-fresh2 x (fv y) t (x\u2209FVy y y\u2209x\u2237L) |\n subst-open2 x t 0 (fv y) m (->||-Term-l t->||t') |\n subst-fresh x (fv y) t (x\u2209FVy y y\u2209x\u2237L) |\n subst-fresh2 x (fv y) t' (x\u2209FVy y y\u2209x\u2237L) |\n subst-open2 x t' 0 (fv y) m' (->||-Term-r t->||t') |\n subst-fresh x (fv y) t' (x\u2209FVy y y\u2209x\u2237L) =\n lem2-5-1 (m ^' y) (m' ^' y) t t' x (cf (\u03bb z \u2192 y\u2209x\u2237L (there z))) t->||t'\n\nlem2-5-1 _ _ t t' x (beta L {m} {m'} {n} {n'} cf n->||n') t->||t' rewrite\n subst-open x t' 0 n' m' (->||-Term-r t->||t') =\n beta (x \u2237 L) {_} {m' [ x ::= t' ]} body body\u2082\n where\n body : {y : \u2115} -> y \u2209 x \u2237 L -> ((m [ x ::= t ]) ^' y) ->|| ((m' [ x ::= t' ]) ^' y)\n body {y} y\u2209x\u2237L rewrite\n subst-open-var y x t m (fv-x\u2260y y x y\u2209x\u2237L) (->||-Term-l t->||t') |\n subst-open-var y x t' m' (fv-x\u2260y y x y\u2209x\u2237L) (->||-Term-r t->||t') =\n lem2-5-1 (m ^' y) (m' ^' y) t t' x (cf (\u03bb z \u2192 y\u2209x\u2237L (there z))) t->||t'\n\n body\u2082 : (n [ x ::= t ]) ->|| (n' [ x ::= t' ])\n body\u2082 = lem2-5-1 n n' t t' x n->||n' t->||t'\n\nlem2-5-1 _ _ t t' x (Y {m} {m'} ss') t->||t' = Y (lem2-5-1 m m' t t' x ss' t->||t')\n\n\nlem2-5-1-^ : \u2200 s s' t t' L -> (\u2200 {x} -> x \u2209 L -> (s ^' x) ->|| (s' ^' x)) -> t ->|| t' ->\n (s ^ t) ->|| (s' ^ t')\nlem2-5-1-^ s s' t t' L cf t->||t' = body\n where\n x = \u2203fresh (L ++ FV s ++ FV s')\n\n x\u2209 : x \u2209 (L ++ FV s ++ FV s')\n x\u2209 = \u2203fresh-spec (L ++ FV s ++ FV s')\n\n body : (s ^ t) ->|| (s' ^ t')\n body rewrite\n subst-intro x t s (\u2209-cons-l (FV s) (FV s') (\u2209-cons-r L (FV s ++ FV s') x\u2209)) (->||-Term-l t->||t') |\n subst-intro x t' s' (\u2209-cons-r (FV s) (FV s') (\u2209-cons-r L (FV s ++ FV s') x\u2209)) (->||-Term-r t->||t') =\n lem2-5-1 (s ^' x) (s' ^' x) t t' x (cf (\u2209-cons-l L (FV s ++ FV s') x\u2209)) t->||t'\n\n\nNotAbsY-subst : \u2200 {x y m} -> NotAbsY m -> NotAbsY (m [ x ::= fv y ])\nNotAbsY-subst {x} (fv {y}) with x \u225f y\nNotAbsY-subst {x} {y} fv | yes refl rewrite\n fv-subst-eq x x (fv y) refl = fv\nNotAbsY-subst fv | no _ = fv\nNotAbsY-subst bv = bv\nNotAbsY-subst app = app\n\n\nlem2-5-1>>> : \u2200 s s' (x y : \u2115) -> s >>> s' -> (s [ x ::= fv y ]) >>> (s' [ x ::= fv y ])\nlem2-5-1>>> _ _ x y (refl {z}) with x \u225f z\nlem2-5-1>>> .(fv x) .(fv x) x y refl | yes refl rewrite\n fv-subst-eq x x (fv y) refl = refl\nlem2-5-1>>> _ _ x y (refl {z}) | no z\u2260x = refl\nlem2-5-1>>> _ _ x y reflY = reflY\nlem2-5-1>>> _ _ x y (app {m} {m'} {n} {n'} \u00acabsY ss' ss'') =\n app (NotAbsY-subst \u00acabsY) (lem2-5-1>>> m m' x y ss') (lem2-5-1>>> n n' x y ss'')\nlem2-5-1>>> _ _ x y (abs L {m} {m'} cf) = abs (x \u2237 L) body\n where\n x\u2209FVz : (z : \u2115) -> (z \u2209 x \u2237 L) -> x \u2209 FV (fv z)\n x\u2209FVz z z\u2209x\u2237L x\u2208FVz with fv-x\u2261y x z x\u2208FVz\n x\u2209FVz .x z\u2209x\u2237L x\u2208FVz | refl = z\u2209x\u2237L (here refl)\n\n body : {z : \u2115} -> (z \u2209 x \u2237 L) -> ((m [ x ::= fv y ]) ^' z) >>> ((m' [ x ::= fv y ]) ^' z)\n body {z} z\u2209x\u2237L rewrite\n subst-fresh2 x (fv z) (fv y) (x\u2209FVz z z\u2209x\u2237L) |\n subst-open2 x (fv y) 0 (fv z) m var |\n subst-fresh x (fv z) (fv y) (x\u2209FVz z z\u2209x\u2237L) |\n subst-fresh2 x (fv z) (fv y) (x\u2209FVz z z\u2209x\u2237L) |\n subst-open2 x (fv y) 0 (fv z) m' var |\n subst-fresh x (fv z) (fv y) (x\u2209FVz z z\u2209x\u2237L) =\n lem2-5-1>>> (m ^' z) (m' ^' z) x y (cf (\u03bb z\u2081 \u2192 z\u2209x\u2237L (there z\u2081)))\n\nlem2-5-1>>> _ _ x y (beta L {m} {m'} {n} {n'} cf n>>>n') rewrite\n subst-open x (fv y) 0 n' m' var =\n beta (x \u2237 L) {_} {m' [ x ::= fv y ]} body body\u2082\n where\n body : {z : \u2115} -> z \u2209 x \u2237 L -> ((m [ x ::= fv y ]) ^' z) >>> ((m' [ x ::= fv y ]) ^' z)\n body {z} z\u2209x\u2237L rewrite\n subst-open-var z x (fv y) m (fv-x\u2260y z x z\u2209x\u2237L) var |\n subst-open-var z x (fv y) m' (fv-x\u2260y z x z\u2209x\u2237L) var =\n lem2-5-1>>> (m ^' z) (m' ^' z) x y (cf (\u03bb z\u2081 \u2192 z\u2209x\u2237L (there z\u2081)))\n\n body\u2082 : (n [ x ::= fv y ]) >>> (n' [ x ::= fv y ])\n body\u2082 = lem2-5-1>>> n n' x y n>>>n'\n\nlem2-5-1>>> _ _ x y (Y {m} {m'} ss') = Y (lem2-5-1>>> m m' x y ss')\n\n\n*^-^->>> : \u2200 {x y t d k} -> t >>> d -> y \u2209 x \u2237 (FV t ++ FV d) -> ([ k >> fv y ] ([ k << x ] t)) >>> ([ k >> fv y ] ([ k << x ] d))\n*^-^->>> {x} {y} {t} {d} {k} t>>>d y\u2209 rewrite\n *^-^\u2261subst t x y {k} (>>>-Term-l t>>>d) | *^-^\u2261subst d x y {k} (>>>-Term-r t>>>d) = lem2-5-1>>> t d x y t>>>d\n\n\n\u2203>>> : \u2200 {a} -> Term a -> \u2203(\u03bb d -> a >>> d)\n\u2203>>> (var {x}) = fv x , refl\n\u2203>>> (lam L {t} cf) = body\n where\n x = \u2203fresh (L ++ FV t)\n\n x\u2209 : x \u2209 (L ++ FV t)\n x\u2209 = \u2203fresh-spec (L ++ FV t)\n\n d-spec : \u2203(\u03bb d -> (t ^' x) >>> d)\n d-spec = \u2203>>> (cf (\u2209-cons-l L (FV t) x\u2209))\n\n d : PTerm\n d = proj\u2081 d-spec\n\n subst1 : \u2200 x y t k -> x \u2209 FV t -> (t ^' y) \u2261 (([ k << x ] ([ k >> fv x ] t)) ^' y)\n subst1 x y t k x\u2209FVt rewrite fv-^-*^-refl x t {k} x\u2209FVt = refl\n\n cf' : \u2200 {y} -> y \u2209 x \u2237 (FV d ++ FV t) -> (t ^' y) >>> ((* x ^ d) ^' y)\n cf' {y} y\u2209 rewrite subst1 x y t 0 (\u2209-cons-r L (FV t) x\u2209) =\n *^-^->>> {x} {y} {t ^' x} {d} {0} (proj\u2082 d-spec)\n (\u2209-\u2237 x (FV (t ^' x) ++ FV d) (fv-x\u2260y y x y\u2209)\n (\u2209-cons-intro (FV (t ^' x)) (FV d)\n (fv-^ {0} {y} {x} t (\u2209-cons-r (FV d) _ (\u2209-\u2237-elim _ y\u2209)) (fv-x\u2260y _ _ y\u2209))\n (\u2209-cons-l _ (FV t) (\u2209-\u2237-elim _ y\u2209))))\n\n body : \u2203(\u03bb d -> (lam t) >>> d)\n body = lam (* x ^ d) , (abs (x \u2237 (FV d ++ FV t)) cf')\n\n\u2203>>> (app {t1} {t2} trm-t1 trm-t2) with trm-t1 | \u2203>>> trm-t1 | \u2203>>> trm-t2\n\u2203>>> (app trm-t1 trm-t2) | (var {x}) | d1 , t1>>>d1 | d2 , t2>>>d2 = app (fv x) d2 , app fv refl t2>>>d2\n\u2203>>> (app {_} {t2} trm-t1 trm-t2) | lam L cf | _ , abs L\u2081 {t1} {d1} cf\u2081 | d2 , t2>>>d2 =\n d1 ^ d2 , beta L\u2081 {t1} {d1} {t2} {d2} cf\u2081 t2>>>d2\n\u2203>>> (app {_} {t2} trm-t1 trm-t2) | app {p1} {p2} trm-p1 trm-p2 | d1 , t1>>>d1 | d2 , t2>>>d2 =\n app d1 d2 , app app t1>>>d1 t2>>>d2\n\u2203>>> (app trm-t1 trm-t2) | (Y {t}) | d1 , t1>>>d1 | d2 , t2>>>d2 = app d2 (app (Y t) d2) , Y t2>>>d2\n\u2203>>> (Y {t}) = Y t , reflY\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"047282f00de5f7bd07a7ab82432a54bc466d77a0","subject":"Prove that \u0394-Context strictly increases the context (see #29).","message":"Prove that \u0394-Context strictly increases the context (see #29).\n\nOld-commit-hash: 528a74e6ca967a4ad12946c97f4f77491928beb1\n","repos":"inc-lc\/ilc-agda","old_file":"ChangeContexts.agda","new_file":"ChangeContexts.agda","new_contents":"module ChangeContexts where\n\n-- CHANGE CONTEXTS\n--\n-- This module defines change contexts, that is, contexts where\n-- for every value assertion x : \u03c4, we also have a change\n-- assertion ds : \u0394-Type \u03c4.\n\n-- CHANGE ENVIRONMENTS\n--\n-- This module describes operations on change environments, that\n-- is, environments where for every value binding x = \u27e6 \u03c4 \u27e7, we\n-- also have a change assertion dx = \u27e6 \u0394-Type \u03c4 \u27e7.\n\nopen import Syntactic.Types\nopen import Changes\nopen import Denotational.Values\nopen import Denotational.Notation\n\n-- TYPING CONTEXTS, VARIABLES and WEAKENING\n\nopen import Syntactic.Contexts Type\nopen import Denotational.Environments Type \u27e6_\u27e7Type\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393\n\n-- Properties\n\nmodule _ where\n open Subcontexts renaming (drop_\u2022_ to drop\u2032_\u2022_)\n\n \u227c-\u0394-Context : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n \u227c-\u0394-Context {\u2205} = \u2205\n \u227c-\u0394-Context {\u03c4 \u2022 \u0393} = drop\u2032 (\u0394-Type \u03c4) \u2022 keep \u03c4 \u2022 \u227c-\u0394-Context\n\n-- OPERATIONS on CHANGE ENVIRONMENTS\n\nupdate : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 \u03c1) = apply dv v \u2022 update \u03c1\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore {\u2205} \u2205 = \u2205\nignore {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 \u03c1) = v \u2022 ignore \u03c1\n\n-- \u0394-Context\u2032: behaves like \u0394-Context, but has an extra argument \u0393\u2032, a\n-- prefix of its first argument which should not be touched.\n\u0394-Context\u2032 : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\n\u0394-Context\u2032 \u0393 \u2205 = \u0394-Context \u0393\n\u0394-Context\u2032 (.\u03c4 \u2022 \u0393) (\u03c4 \u2022 \u0393\u2032) = \u03c4 \u2022 \u0394-Context\u2032 \u0393 \u0393\u2032\n\nupdate\u2032 : \u2200 {\u0393} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nupdate\u2032 \u2205 \u03c1 = update \u03c1\nupdate\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) = v \u2022 update\u2032 \u0393\u2032 \u03c1\n\nignore\u2032 : \u2200 {\u0393} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore\u2032 \u2205 \u03c1 = ignore \u03c1\nignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) = v \u2022 ignore\u2032 \u0393\u2032 \u03c1\n\nopen import Relation.Binary.PropositionalEquality as P\n\n\u0394-Context-\u22ce : \u2200 \u0393\u2081 \u0393\u2082 \u2192\n \u0394-Context (\u0393\u2081 \u22ce \u0393\u2082) P.\u2261 \u0394-Context \u0393\u2081 \u22ce \u0394-Context \u0393\u2082\n\u0394-Context-\u22ce \u2205 \u0393\u2082 = refl\n\u0394-Context-\u22ce (\u03c4 \u2022 \u0393\u2081) \u0393\u2082 rewrite \u0394-Context-\u22ce \u0393\u2081 \u0393\u2082 = refl\n\n\u0394-Context-\u22ce-expanded : \u2200 \u0393\u2081 \u03c4\u2082 \u0393\u2082 \u2192\n \u0394-Context \u0393\u2081 \u22ce (\u0394-Type \u03c4\u2082 \u2022 \u03c4\u2082 \u2022 \u0394-Context \u0393\u2082) P.\u2261 \u0394-Context (\u0393\u2081 \u22ce (\u03c4\u2082 \u2022 \u0393\u2082))\n\u0394-Context-\u22ce-expanded \u0393\u2081 \u03c4\u2082 \u0393\u2082 rewrite \u0394-Context-\u22ce \u0393\u2081 (\u03c4\u2082 \u2022 \u0393\u2082) = refl\n\ntake-\u22ce-\u0394-Context-drop-\u0394-Context\u2032 : \u2200 \u0393 \u0393\u2032 \u2192\n \u0394-Context\u2032 \u0393 \u0393\u2032 P.\u2261 take \u0393 \u0393\u2032 \u22ce \u0394-Context (drop \u0393 \u0393\u2032)\ntake-\u22ce-\u0394-Context-drop-\u0394-Context\u2032 \u2205 \u2205 = refl\ntake-\u22ce-\u0394-Context-drop-\u0394-Context\u2032 (\u03c4 \u2022 \u0393) \u2205 = refl\ntake-\u22ce-\u0394-Context-drop-\u0394-Context\u2032 (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032)\n rewrite take-\u22ce-\u0394-Context-drop-\u0394-Context\u2032 \u0393 \u0393\u2032 = refl\n\n-- WEAKENING\n\n-- Weaken a term to a super context\n\n{-\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken = {!!}\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken {\u0393\u2081} {\u0393\u2082} (abs {\u03c4\u2081 = \u03c4} t) = abs (weaken {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} t)\nweaken {\u0393\u2081} {\u0393\u2082} (app t\u2081 t\u2082) = app (weaken {\u0393\u2081} {\u0393\u2082} t\u2081) (weaken {\u0393\u2081} {\u0393\u2082} t\u2082)\nweaken {\u0393\u2081} {\u0393\u2082} (var x) = var (lift {\u0393\u2081} {\u0393\u2082} x)\nweaken {.(\u0394-Type \u03c4) \u2022 .\u03c4 \u2022 _} {\u0393\u2082} (\u0394 {\u03c4 \u2022 \u0393\u2083} t) = ?\n--weaken {.(\u0394-Type \u03c4) \u2022 .\u03c4 \u2022 _} {\u0393\u2082} (\u0394 {\u03c4 \u2022 \u0393\u2083} t) = ?\n--weaken {(\u0394-Type \u03c4) \u2022 \u03c4 \u2022 _} {\u0393\u2082} (\u0394 {\u03c4 \u2022 \u0393\u2083} t) = ?\n--weaken {(\u0394-Context .\u0393\u2081)} {\u0393\u2082} (\u0394 {\u0393\u2081 \u22ce \u0393\u2083} t) = ?\n-}\n","old_contents":"module ChangeContexts where\n\n-- CHANGE CONTEXTS\n--\n-- This module defines change contexts, that is, contexts where\n-- for every value assertion x : \u03c4, we also have a change\n-- assertion ds : \u0394-Type \u03c4.\n\n-- CHANGE ENVIRONMENTS\n--\n-- This module describes operations on change environments, that\n-- is, environments where for every value binding x = \u27e6 \u03c4 \u27e7, we\n-- also have a change assertion dx = \u27e6 \u0394-Type \u03c4 \u27e7.\n\nopen import Syntactic.Types\nopen import Changes\nopen import Denotational.Values\nopen import Denotational.Notation\n\n-- TYPING CONTEXTS, VARIABLES and WEAKENING\n\nopen import Syntactic.Contexts Type\nopen import Denotational.Environments Type \u27e6_\u27e7Type\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393\n\nupdate : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 \u03c1) = apply dv v \u2022 update \u03c1\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore {\u2205} \u2205 = \u2205\nignore {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 \u03c1) = v \u2022 ignore \u03c1\n\n-- \u0394-Context\u2032: behaves like \u0394-Context, but has an extra argument \u0393\u2032, a\n-- prefix of its first argument which should not be touched.\n\u0394-Context\u2032 : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\n\u0394-Context\u2032 \u0393 \u2205 = \u0394-Context \u0393\n\u0394-Context\u2032 (.\u03c4 \u2022 \u0393) (\u03c4 \u2022 \u0393\u2032) = \u03c4 \u2022 \u0394-Context\u2032 \u0393 \u0393\u2032\n\nupdate\u2032 : \u2200 {\u0393} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nupdate\u2032 \u2205 \u03c1 = update \u03c1\nupdate\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) = v \u2022 update\u2032 \u0393\u2032 \u03c1\n\nignore\u2032 : \u2200 {\u0393} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore\u2032 \u2205 \u03c1 = ignore \u03c1\nignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) = v \u2022 ignore\u2032 \u0393\u2032 \u03c1\n\nopen import Relation.Binary.PropositionalEquality as P\n\n\u0394-Context-\u22ce : \u2200 \u0393\u2081 \u0393\u2082 \u2192\n \u0394-Context (\u0393\u2081 \u22ce \u0393\u2082) P.\u2261 \u0394-Context \u0393\u2081 \u22ce \u0394-Context \u0393\u2082\n\u0394-Context-\u22ce \u2205 \u0393\u2082 = refl\n\u0394-Context-\u22ce (\u03c4 \u2022 \u0393\u2081) \u0393\u2082 rewrite \u0394-Context-\u22ce \u0393\u2081 \u0393\u2082 = refl\n\n\u0394-Context-\u22ce-expanded : \u2200 \u0393\u2081 \u03c4\u2082 \u0393\u2082 \u2192\n \u0394-Context \u0393\u2081 \u22ce (\u0394-Type \u03c4\u2082 \u2022 \u03c4\u2082 \u2022 \u0394-Context \u0393\u2082) P.\u2261 \u0394-Context (\u0393\u2081 \u22ce (\u03c4\u2082 \u2022 \u0393\u2082))\n\u0394-Context-\u22ce-expanded \u0393\u2081 \u03c4\u2082 \u0393\u2082 rewrite \u0394-Context-\u22ce \u0393\u2081 (\u03c4\u2082 \u2022 \u0393\u2082) = refl\n\ntake-\u22ce-\u0394-Context-drop-\u0394-Context\u2032 : \u2200 \u0393 \u0393\u2032 \u2192\n \u0394-Context\u2032 \u0393 \u0393\u2032 P.\u2261 take \u0393 \u0393\u2032 \u22ce \u0394-Context (drop \u0393 \u0393\u2032)\ntake-\u22ce-\u0394-Context-drop-\u0394-Context\u2032 \u2205 \u2205 = refl\ntake-\u22ce-\u0394-Context-drop-\u0394-Context\u2032 (\u03c4 \u2022 \u0393) \u2205 = refl\ntake-\u22ce-\u0394-Context-drop-\u0394-Context\u2032 (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032)\n rewrite take-\u22ce-\u0394-Context-drop-\u0394-Context\u2032 \u0393 \u0393\u2032 = refl\n\n-- WEAKENING\n\n-- Weaken a term to a super context\n\n{-\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken = {!!}\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken {\u0393\u2081} {\u0393\u2082} (abs {\u03c4\u2081 = \u03c4} t) = abs (weaken {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} t)\nweaken {\u0393\u2081} {\u0393\u2082} (app t\u2081 t\u2082) = app (weaken {\u0393\u2081} {\u0393\u2082} t\u2081) (weaken {\u0393\u2081} {\u0393\u2082} t\u2082)\nweaken {\u0393\u2081} {\u0393\u2082} (var x) = var (lift {\u0393\u2081} {\u0393\u2082} x)\nweaken {.(\u0394-Type \u03c4) \u2022 .\u03c4 \u2022 _} {\u0393\u2082} (\u0394 {\u03c4 \u2022 \u0393\u2083} t) = ?\n--weaken {.(\u0394-Type \u03c4) \u2022 .\u03c4 \u2022 _} {\u0393\u2082} (\u0394 {\u03c4 \u2022 \u0393\u2083} t) = ?\n--weaken {(\u0394-Type \u03c4) \u2022 \u03c4 \u2022 _} {\u0393\u2082} (\u0394 {\u03c4 \u2022 \u0393\u2083} t) = ?\n--weaken {(\u0394-Context .\u0393\u2081)} {\u0393\u2082} (\u0394 {\u0393\u2081 \u22ce \u0393\u2083} t) = ?\n-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"241582d7212cda6ed5b4d48058af4392fc61bce7","subject":"Desc model: followed by the generic proof that map (f . g) = map f . map g","message":"Desc model: followed by the generic proof that map (f . g) = map f . map g\n","repos":"mietek\/epigram2,larrytheliquid\/pigit,mietek\/epigram2","old_file":"models\/Desc.agda","new_file":"models\/Desc.agda","new_contents":"\n {-# OPTIONS --type-in-type #-}\n\nmodule Desc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A : Set}{B : A -> Set}(f : (a : A) -> B a)(g : (a : A) -> B a)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata Desc : Set where\n id : Desc\n const : Set -> Desc\n prod : Desc -> Desc -> Desc\n sigma : (S : Set) -> (S -> Desc) -> Desc\n pi : (S : Set) -> (S -> Desc) -> Desc\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|]_ : Desc -> Set -> Set\n[| id |] Z = Z\n[| const X |] Z = X\n[| prod D D' |] Z = [| D |] Z * [| D' |] Z\n[| sigma S T |] Z = Sigma S (\\s -> [| T s |] Z)\n[| pi S T |] Z = (s : S) -> [| T s |] Z\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata Mu (D : Desc) : Set where\n con : [| D |] (Mu D) -> Mu D\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : (D : Desc)(X : Set)(P : X -> Set) -> [| D |] X -> Set\nAll id X P x = P x\nAll (const Z) X P x = Z\nAll (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')\nAll (sigma S T) X P (a , b) = All (T a) X P b\nAll (pi S T) X P f = (s : S) -> All (T s) X P (f s)\n\nall : (D : Desc)(X : Set)(P : X -> Set)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x\nall id X P R x = R x\nall (const Z) X P R z = z\nall (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'\nall (sigma S T) X P R (a , b) = all (T a) X P R b\nall (pi S T) X P R f = \\ s -> all (T s) X P R (f s)\n\n--********************************************\n-- Map\n--********************************************\n\nmap : (D : Desc)(X Y : Set)(f : X -> Y)(v : [| D |] X) -> [| D |] Y\nmap id X Y sig x = sig x\nmap (const Z) X Y sig z = z\nmap (prod D D') X Y sig (d , d') = map D X Y sig d , map D' X Y sig d'\nmap (sigma S T) X Y sig (a , b) = (a , map (T a) X Y sig b)\nmap (pi S T) X Y sig f = \\x -> map (T x) X Y sig (f x)\n\n\nproof-map-id : (D : Desc)(X : Set)(v : [| D |] X) -> map D X X (\\x -> x) v == v\nproof-map-id id X v = refl\nproof-map-id (const Z) X v = refl\nproof-map-id (prod D D') X (v , v') = cong2 (\\x y -> (x , y)) (proof-map-id D X v) (proof-map-id D' X v')\nproof-map-id (sigma S T) X (a , b) = cong (\\x -> (a , x)) (proof-map-id (T a) X b) \nproof-map-id (pi S T) X f = reflFun (\\a -> map (T a) X X (\\x -> x) (f a)) f (\\a -> proof-map-id (T a) X (f a))\n\nproof-map-compos : (D : Desc)(X Y Z : Set)\n (f : X -> Y)(g : Y -> Z)\n (v : [| D |] X) -> \n map D X Z (\\x -> g (f x)) v == map D Y Z g (map D X Y f v)\nproof-map-compos id X Y Z f g v = refl\nproof-map-compos (const K) X Y Z f g v = refl\nproof-map-compos (prod D D') X Y Z f g (v , v') = cong2 (\\x y -> (x , y)) \n (proof-map-compos D X Y Z f g v)\n (proof-map-compos D' X Y Z f g v')\nproof-map-compos (sigma S T) X Y Z f g (a , b) = cong (\\x -> (a , x)) (proof-map-compos (T a) X Y Z f g b)\nproof-map-compos (pi S T) X Y Z f g fc = reflFun (\\a -> map (T a) X Z (\\x -> g (f x)) (fc a))\n (\\a -> map (T a) Y Z g (map (T a) X Y f (fc a)))\n (\\a -> proof-map-compos (T a) X Y Z f g (fc a))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n{-\ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms (con xs) = ms xs (all D (Mu D) P (\\x -> induction D P ms x) xs)\n-}\n\nmodule Elim (D : Desc)\n (P : Mu D -> Set)\n (ms : (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x))\n where\n\n mutual\n induction : (x : Mu D) -> P x\n induction (con xs) = ms xs (hyps D xs)\n \n hyps : (D' : Desc)\n (xs : [| D' |] (Mu D)) ->\n All D' (Mu D) P xs\n hyps id x = induction x\n hyps (const Z) z = z\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (sigma S T) (a , b) = hyps (T a) b\n hyps (pi S T) f = \\s -> hyps (T s) (f s)\n \ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms x = Elim.induction D P ms x\n\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Suc : NatConst\n\nnatCases : NatConst -> Desc\nnatCases Ze = const Unit\nnatCases Suc = id\n\nNatD : Desc\nNatD = sigma NatConst natCases\n\nNat : Set\nNat = Mu NatD\n\nze : Nat\nze = con (Ze , Void)\n\nsuc : Nat -> Nat\nsuc n = con (Suc , n)\n\n--****************\n-- List\n--****************\n\ndata ListConst : Set where\n Nil : ListConst\n Cons : ListConst\n\nlistCases : Set -> ListConst -> Desc\nlistCases X Nil = const Unit\nlistCases X Cons = sigma X (\\_ -> id)\n\nListD : Set -> Desc\nListD X = sigma ListConst (listCases X)\n\nList : Set -> Set\nList X = Mu (ListD X)\n\nnil : {X : Set} -> List X\nnil = con ( Nil , Void )\n\ncons : {X : Set} -> X -> List X -> List X\ncons x t = con ( Cons , ( x , t ))\n\n--****************\n-- Tree\n--****************\n\ndata TreeConst : Set where\n Leaf : TreeConst\n Node : TreeConst\n\ntreeCases : Set -> TreeConst -> Desc\ntreeCases X Leaf = const Unit\ntreeCases X Node = sigma X (\\_ -> prod id id)\n\nTreeD : Set -> Desc\nTreeD X = sigma TreeConst (treeCases X)\n\nTree : Set -> Set\nTree X = Mu (TreeD X)\n\nleaf : {X : Set} -> Tree X\nleaf = con (Leaf , Void)\n\nnode : {X : Set} -> X -> Tree X -> Tree X -> Tree X\nnode x le ri = con (Node , (x , (le , ri)))\n\n--********************************************\n-- Finite sets\n--********************************************\n\nEnumU : Set\nEnumU = Nat\n\nnilE : EnumU\nnilE = ze\n\nconsE : EnumU -> EnumU\nconsE e = suc e\n\n{-\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n-}\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\ncasesSpi : (xs : [| NatD |] Nat) -> \n All NatD Nat (\\e -> (P' : EnumT e -> Set) -> Set) xs -> \n (P' : EnumT (con xs) -> Set) -> Set\ncasesSpi (Ze , Void) hs P' = Unit\ncasesSpi (Suc , n) hs P' = P' EZe * hs (\\e -> P' (ESu e))\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi e P = induction NatD (\\e -> (P : EnumT e -> Set) -> Set) casesSpi e P\n\n{-\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n-}\n\n\ncasesSwitch : (xs : [| NatD |] Nat) ->\n All NatD Nat (\\e -> (P' : EnumT e -> Set)(b' : spi e P')(x' : EnumT e) -> P' x') xs ->\n (P' : EnumT (con xs) -> Set)(b' : spi (con xs) P')(x' : EnumT (con xs)) -> P' x'\ncasesSwitch (Ze , Void) hs P' b' ()\ncasesSwitch (Suc , n) hs P' b' EZe = fst b'\ncasesSwitch (Suc , n) hs P' b' (ESu e') = hs (\\e -> P' (ESu e)) (snd b') e'\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch e P b x = induction NatD (\\e -> (P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x) casesSwitch e P b x\n\n{-\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n-}\n\n--********************************************\n-- Tagged description\n--********************************************\n\nTagDesc : Set\nTagDesc = Sigma EnumU (\\e -> spi e (\\_ -> Desc))\n\ntoDesc : TagDesc -> Desc\ntoDesc (B , F) = sigma (EnumT B) (\\e -> switch B (\\_ -> Desc) F e)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (D : Desc)\n (T : Set) ->\n ([| D |] T -> T) ->\n (Mu D) -> T\ncata D T phi x = induction D (\\_ -> T) (\\x ms -> phi (replace D T x ms)) x\n where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\\_ -> T) xs) -> [| D' |] T\n replace id T x y = y\n replace (const Z) T z z' = z'\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : TagDesc -> (X : Set) -> TagDesc\n(e , D) ** X = consE e , (const X , D)\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : (D : TagDesc)(X Y : Set) ->\n (X -> Mu (toDesc (D ** Y))) ->\n [| toDesc (D ** X) |] (Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\napply (E , B) X Y sig (EZe , x) = sig x\napply (E , B) X Y sig (ESu n , t) = con (ESu n , t)\n\nsubst : (D : TagDesc)(X Y : Set) ->\n Mu (toDesc (D ** X)) ->\n (X -> Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\nsubst D X Y x sig = cata (toDesc (D ** X)) (Mu (toDesc (D ** Y))) (apply D X Y sig) x\n","old_contents":"\n {-# OPTIONS --type-in-type #-}\n\nmodule Desc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A : Set}{B : A -> Set}(f : (a : A) -> B a)(g : (a : A) -> B a)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata Desc : Set where\n id : Desc\n const : Set -> Desc\n prod : Desc -> Desc -> Desc\n sigma : (S : Set) -> (S -> Desc) -> Desc\n pi : (S : Set) -> (S -> Desc) -> Desc\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|]_ : Desc -> Set -> Set\n[| id |] Z = Z\n[| const X |] Z = X\n[| prod D D' |] Z = [| D |] Z * [| D' |] Z\n[| sigma S T |] Z = Sigma S (\\s -> [| T s |] Z)\n[| pi S T |] Z = (s : S) -> [| T s |] Z\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata Mu (D : Desc) : Set where\n con : [| D |] (Mu D) -> Mu D\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : (D : Desc)(X : Set)(P : X -> Set) -> [| D |] X -> Set\nAll id X P x = P x\nAll (const Z) X P x = Z\nAll (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')\nAll (sigma S T) X P (a , b) = All (T a) X P b\nAll (pi S T) X P f = (s : S) -> All (T s) X P (f s)\n\nall : (D : Desc)(X : Set)(P : X -> Set)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x\nall id X P R x = R x\nall (const Z) X P R z = z\nall (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'\nall (sigma S T) X P R (a , b) = all (T a) X P R b\nall (pi S T) X P R f = \\ s -> all (T s) X P R (f s)\n\n--********************************************\n-- Map\n--********************************************\n\nmap : (D : Desc)(X Y : Set)(f : X -> Y)(v : [| D |] X) -> [| D |] Y\nmap id X Y sig x = sig x\nmap (const Z) X Y sig z = z\nmap (prod D D') X Y sig (d , d') = map D X Y sig d , map D' X Y sig d'\nmap (sigma S T) X Y sig (a , b) = (a , map (T a) X Y sig b)\nmap (pi S T) X Y sig f = \\x -> map (T x) X Y sig (f x)\n\n\nproof-map-id : (D : Desc)(X : Set)(v : [| D |] X) -> map D X X (\\x -> x) v == v\nproof-map-id id X v = refl\nproof-map-id (const Z) X v = refl\nproof-map-id (prod D D') X (v , v') = cong2 (\\x y -> (x , y)) (proof-map-id D X v) (proof-map-id D' X v')\nproof-map-id (sigma S T) X (a , b) = cong (\\x -> (a , x)) (proof-map-id (T a) X b) \nproof-map-id (pi S T) X f = reflFun (\\a -> map (T a) X X (\\x -> x) (f a)) f (\\a -> proof-map-id (T a) X (f a))\n\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n{-\ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms (con xs) = ms xs (all D (Mu D) P (\\x -> induction D P ms x) xs)\n-}\n\nmodule Elim (D : Desc)\n (P : Mu D -> Set)\n (ms : (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x))\n where\n\n mutual\n induction : (x : Mu D) -> P x\n induction (con xs) = ms xs (hyps D xs)\n \n hyps : (D' : Desc)\n (xs : [| D' |] (Mu D)) ->\n All D' (Mu D) P xs\n hyps id x = induction x\n hyps (const Z) z = z\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (sigma S T) (a , b) = hyps (T a) b\n hyps (pi S T) f = \\s -> hyps (T s) (f s)\n \ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms x = Elim.induction D P ms x\n\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Suc : NatConst\n\nnatCases : NatConst -> Desc\nnatCases Ze = const Unit\nnatCases Suc = id\n\nNatD : Desc\nNatD = sigma NatConst natCases\n\nNat : Set\nNat = Mu NatD\n\nze : Nat\nze = con (Ze , Void)\n\nsuc : Nat -> Nat\nsuc n = con (Suc , n)\n\n--****************\n-- List\n--****************\n\ndata ListConst : Set where\n Nil : ListConst\n Cons : ListConst\n\nlistCases : Set -> ListConst -> Desc\nlistCases X Nil = const Unit\nlistCases X Cons = sigma X (\\_ -> id)\n\nListD : Set -> Desc\nListD X = sigma ListConst (listCases X)\n\nList : Set -> Set\nList X = Mu (ListD X)\n\nnil : {X : Set} -> List X\nnil = con ( Nil , Void )\n\ncons : {X : Set} -> X -> List X -> List X\ncons x t = con ( Cons , ( x , t ))\n\n--****************\n-- Tree\n--****************\n\ndata TreeConst : Set where\n Leaf : TreeConst\n Node : TreeConst\n\ntreeCases : Set -> TreeConst -> Desc\ntreeCases X Leaf = const Unit\ntreeCases X Node = sigma X (\\_ -> prod id id)\n\nTreeD : Set -> Desc\nTreeD X = sigma TreeConst (treeCases X)\n\nTree : Set -> Set\nTree X = Mu (TreeD X)\n\nleaf : {X : Set} -> Tree X\nleaf = con (Leaf , Void)\n\nnode : {X : Set} -> X -> Tree X -> Tree X -> Tree X\nnode x le ri = con (Node , (x , (le , ri)))\n\n--********************************************\n-- Finite sets\n--********************************************\n\nEnumU : Set\nEnumU = Nat\n\nnilE : EnumU\nnilE = ze\n\nconsE : EnumU -> EnumU\nconsE e = suc e\n\n{-\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n-}\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\ncasesSpi : (xs : [| NatD |] Nat) -> \n All NatD Nat (\\e -> (P' : EnumT e -> Set) -> Set) xs -> \n (P' : EnumT (con xs) -> Set) -> Set\ncasesSpi (Ze , Void) hs P' = Unit\ncasesSpi (Suc , n) hs P' = P' EZe * hs (\\e -> P' (ESu e))\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi e P = induction NatD (\\e -> (P : EnumT e -> Set) -> Set) casesSpi e P\n\n{-\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n-}\n\n\ncasesSwitch : (xs : [| NatD |] Nat) ->\n All NatD Nat (\\e -> (P' : EnumT e -> Set)(b' : spi e P')(x' : EnumT e) -> P' x') xs ->\n (P' : EnumT (con xs) -> Set)(b' : spi (con xs) P')(x' : EnumT (con xs)) -> P' x'\ncasesSwitch (Ze , Void) hs P' b' ()\ncasesSwitch (Suc , n) hs P' b' EZe = fst b'\ncasesSwitch (Suc , n) hs P' b' (ESu e') = hs (\\e -> P' (ESu e)) (snd b') e'\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch e P b x = induction NatD (\\e -> (P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x) casesSwitch e P b x\n\n{-\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n-}\n\n--********************************************\n-- Tagged description\n--********************************************\n\nTagDesc : Set\nTagDesc = Sigma EnumU (\\e -> spi e (\\_ -> Desc))\n\ntoDesc : TagDesc -> Desc\ntoDesc (B , F) = sigma (EnumT B) (\\e -> switch B (\\_ -> Desc) F e)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (D : Desc)\n (T : Set) ->\n ([| D |] T -> T) ->\n (Mu D) -> T\ncata D T phi x = induction D (\\_ -> T) (\\x ms -> phi (replace D T x ms)) x\n where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\\_ -> T) xs) -> [| D' |] T\n replace id T x y = y\n replace (const Z) T z z' = z'\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : TagDesc -> (X : Set) -> TagDesc\n(e , D) ** X = consE e , (const X , D)\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : (D : TagDesc)(X Y : Set) ->\n (X -> Mu (toDesc (D ** Y))) ->\n [| toDesc (D ** X) |] (Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\napply (E , B) X Y sig (EZe , x) = sig x\napply (E , B) X Y sig (ESu n , t) = con (ESu n , t)\n\nsubst : (D : TagDesc)(X Y : Set) ->\n Mu (toDesc (D ** X)) ->\n (X -> Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\nsubst D X Y x sig = cata (toDesc (D ** X)) (Mu (toDesc (D ** Y))) (apply D X Y sig) x\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7c29bc25eaf29f5b062c9326b9ad3562ef87c382","subject":"Bits: more search properties","message":"Bits: more search properties\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (_\u229b_; rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n where open RawMonad L.monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u00b7_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u00b7 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u00b7 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u00b7-comm : \u2200 {m} (x y : A m) \u2192 x \u00b7 y \u2261 y \u00b7 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u00b7-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nmodule SimpleSearch {a} {A : Set a} (_\u00b7_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u00b7_ public\n\n search-\u00b7-\u03b5\u2261\u03b5 : \u2200 \u03b5 (\u03b5\u00b7\u03b5 : \u03b5 \u00b7 \u03b5 \u2261 \u03b5) n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-\u00b7-\u03b5\u2261\u03b5 \u03b5 \u03b5\u00b7\u03b5 = go\n where\n go : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n go zero = refl\n go (suc n) rewrite go n = \u03b5\u00b7\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n module Interchange (\u00b7-interchange : \u2200 x y z t \u2192 (x \u00b7 y) \u00b7 (z \u00b7 t) \u2261 (x \u00b7 z) \u00b7 (y \u00b7 t)) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u00b7 f\u2081 x) \u2261 search f\u2080 \u00b7 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u00b7-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e\n where open \u2261-Reasoning\n\nopen SimpleSearch public\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nsum : \u2200 {n} \u2192 (Bits n \u2192 \u2115) \u2192 \u2115\nsum = search _+_\n\nsum-\u2257 : \u2200 {n} (f g : Bits n \u2192 \u2115) \u2192 f \u2257 g \u2192 sum f \u2261 sum g\nsum-\u2257 = search-\u2257 _+_\n\nsum-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 \u2115) \u2192 sum f \u2261 sum (f \u2218 _\u2295_ pad)\nsum-comm = search\u2032-comm _+_ \u2115\u00b0.+-comm\n\nsum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\nsum-const zero _ = refl\nsum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n#\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n-- #-ext\n#-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n#-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n#-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n#-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n#-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n#-\u2295 = #-comm\n\n#-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n#-const n b = sum-const n (Bool.to\u2115 b)\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n#never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n#never\u22610 = search-\u00b7-\u03b5\u2261\u03b5 _ _ refl\n\n#always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n#always\u22612^ n = sum-const n 1\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\ncount\u1d47 : Bit \u2192 \u2115\ncount\u1d47 b = if b then 1 else 0\n\n#\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n#\u27e8== [] \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n\u2257-cong-search : \u2200 {n a} {A : Set a} op {f g : Bits n \u2192 A} \u2192 f \u2257 g \u2192 search op f \u2261 search op g\n\u2257-cong-search {zero} op f\u2257g = f\u2257g []\n\u2257-cong-search {suc n} op f\u2257g = cong\u2082 op (\u2257-cong-search op (f\u2257g \u2218 0\u2237_))\n (\u2257-cong-search op (f\u2257g \u2218 1\u2237_))\n\n\u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n\u2257-cong-# f g f\u2257g = \u2257-cong-search _+_ (cong count\u1d47 \u2218 f\u2257g)\n\n#-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n#-+ f f0 f1 rewrite f0 | f1 = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\n#-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-take-drop zero n f g with f []\n... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n... | false = #never\u22610 n\n#-take-drop (suc m) n f g = trans (#-+ {a = #\u27e8 f \u2218 0\u2237_ \u27e9 * #\u27e8 g \u27e9} ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)))\n (trans (\u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x)))\n (#-take-drop m n (f \u2218 0\u2237_) g))\n (trans (\u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x)))\n (#-take-drop m n (f \u2218 1\u2237_) g)))\n (sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9))\n\n#-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n#\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n#\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n#\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n... | true | true | _ = s\u2264s z\u2264n\n... | true | false | p = \u22a5-elim (p _)\n... | false | _ | _ = z\u2264n\n#\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n#-\u2227-\u2228\u1d47 : \u2200 x y \u2192 count\u1d47 (x \u2227 y) + count\u1d47 (x \u2228 y) \u2261 count\u1d47 x + count\u1d47 y\n#-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (count\u1d47 y) 1 = refl\n#-\u2227-\u2228\u1d47 false y = refl\n\n#-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n#-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#\u2228' {zero} f g with f []\n... | true = s\u2264s z\u2264n\n... | false = \u2115\u2264.refl\n#\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n#\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n#\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n#\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n#-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n#-bound zero f = Bool.to\u2115\u22641 (f [])\n#-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n#-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n#-\u2218vnot _ f = #-\u2295 1\u207f f\n\n#-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n#-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n#-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n#-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n#-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n#-not\u2218 zero f with f []\n... | true = \u2261.refl\n... | false = \u2261.refl\n#-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n#-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n#-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192 search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = \u2257-cong-search op (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _+_ _*_ f p hom = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom {n} _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n\n\nopen Defs public\n","old_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (_\u229b_; rewire; rewireTbl; sum) renaming (map to vmap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n where open RawMonad L.monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u00b7_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u00b7 search (f \u2218 1\u2237_)\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u00b7-comm : \u2200 {m} (x y : A m) \u2192 x \u00b7 y \u2261 y \u00b7 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u00b7-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nsearch : \u2200 {n a} {A : Set a} \u2192 (A \u2192 A \u2192 A) \u2192 (Bits n \u2192 A) \u2192 A\nsearch {A = A} _\u00b7_ f = search\u2032 {A = const A} _\u00b7_ f\n\nsearch-\u2257 : \u2200 {n a} {A : Set a} (_\u00b7_ : A \u2192 A \u2192 A) (f g : Bits n \u2192 A) \u2192 f \u2257 g \u2192 search _\u00b7_ f \u2261 search _\u00b7_ g\nsearch-\u2257 _\u00b7_ f g f\u2257g = search\u2032-\u2257 _\u00b7_ f g f\u2257g\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nsum : \u2200 {n} \u2192 (Bits n \u2192 \u2115) \u2192 \u2115\nsum = search _+_\n\nsum-\u2257 : \u2200 {n} (f g : Bits n \u2192 \u2115) \u2192 f \u2257 g \u2192 sum f \u2261 sum g\nsum-\u2257 = search-\u2257 _+_\n\nsum-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 \u2115) \u2192 sum f \u2261 sum (f \u2218 _\u2295_ pad)\nsum-comm = search\u2032-comm _+_ \u2115\u00b0.+-comm\n\nsum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\nsum-const zero _ = refl\nsum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n#\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n-- #-ext\n#-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n#-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n#-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n#-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n#-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n#-\u2295 = #-comm\n\n#-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n#-const n b = sum-const n (Bool.to\u2115 b)\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\nsearch-\u00b7-\u03b5\u2261\u03b5 : \u2200 {a} {A : Set a} \u03b5 (_\u00b7_ : A \u2192 A \u2192 A)\n (\u03b5\u00b7\u03b5 : \u03b5 \u00b7 \u03b5 \u2261 \u03b5) n \u2192 search {n} _\u00b7_ (const \u03b5) \u2261 \u03b5\nsearch-\u00b7-\u03b5\u2261\u03b5 \u03b5 _\u00b7_ \u03b5\u00b7\u03b5 = go\n where\n go : \u2200 n \u2192 search {n} _\u00b7_ (const \u03b5) \u2261 \u03b5\n go zero = refl\n go (suc n) rewrite go n = \u03b5\u00b7\u03b5\n\n#never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n#never\u22610 = search-\u00b7-\u03b5\u2261\u03b5 _ _ refl\n\n#always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n#always\u22612^ n = sum-const n 1\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\ncount\u1d47 : Bit \u2192 \u2115\ncount\u1d47 b = if b then 1 else 0\n\n#\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n#\u27e8== [] \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n\u2257-cong-search : \u2200 {n a} {A : Set a} op {f g : Bits n \u2192 A} \u2192 f \u2257 g \u2192 search op f \u2261 search op g\n\u2257-cong-search {zero} op f\u2257g = f\u2257g []\n\u2257-cong-search {suc n} op f\u2257g = cong\u2082 op (\u2257-cong-search op (f\u2257g \u2218 0\u2237_))\n (\u2257-cong-search op (f\u2257g \u2218 1\u2237_))\n\n\u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n\u2257-cong-# f g f\u2257g = \u2257-cong-search _+_ (cong count\u1d47 \u2218 f\u2257g)\n\n#-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n#-+ f f0 f1 rewrite f0 | f1 = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\n#-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-take-drop zero n f g with f []\n... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n... | false = #never\u22610 n\n#-take-drop (suc m) n f g = trans (#-+ {a = #\u27e8 f \u2218 0\u2237_ \u27e9 * #\u27e8 g \u27e9} ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)))\n (trans (\u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x)))\n (#-take-drop m n (f \u2218 0\u2237_) g))\n (trans (\u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x)))\n (#-take-drop m n (f \u2218 1\u2237_) g)))\n (sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9))\n\n#-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n#\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n#\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n#\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n... | true | true | _ = s\u2264s z\u2264n\n... | true | false | p = \u22a5-elim (p _)\n... | false | _ | _ = z\u2264n\n#\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n#-\u2227-\u2228\u1d47 : \u2200 x y \u2192 count\u1d47 (x \u2227 y) + count\u1d47 (x \u2228 y) \u2261 count\u1d47 x + count\u1d47 y\n#-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (count\u1d47 y) 1 = refl\n#-\u2227-\u2228\u1d47 false y = refl\n\n#-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n#-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#\u2228' {zero} f g with f []\n... | true = s\u2264s z\u2264n\n... | false = \u2115\u2264.refl\n#\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n#\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n#\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n#\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n#-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n#-bound zero f = Bool.to\u2115\u22641 (f [])\n#-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n#-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n#-\u2218vnot _ f = #-\u2295 1\u207f f\n\n#-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n#-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n#-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n#-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n#-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n#-not\u2218 zero f with f []\n... | true = \u2261.refl\n... | false = \u2261.refl\n#-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n#-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n#-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192 search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = \u2257-cong-search op (|de-morgan| f g)\n\nsearch-comm :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-comm {zero} _+_ _*_ f p hom = refl\nsearch-comm {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-comm {n} _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-comm _+_ _*_ f (p \u2218 1\u2237_) hom))\n\n\nopen Defs public\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"569e98b5b6d8a84329f54249ad3f83477357f717","subject":"Switch count\u21ba to be based on search","message":"Switch count\u21ba to be based on search\n","repos":"crypto-agda\/crypto-agda","old_file":"flipbased-implem.agda","new_file":"flipbased-implem.agda","new_contents":"module flipbased-implem where\n\nopen import Function\nopen import Data.Bits\nopen import Data.Nat.NP\nopen import Data.Vec\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality as \u2261\nimport Data.Fin as Fin\nopen \u2261 using (_\u2257_; _\u2261_)\nopen Fin using (Fin; suc)\n\nimport flipbased\n\n-- \u201c\u21ba n A\u201d reads like: \u201ctoss n coins and then return a value of type A\u201d\nrecord \u21ba {a} n (A : Set a) : Set a where\n constructor mk\n field\n run\u21ba : Bits n \u2192 A\n\nopen \u21ba public\n\nprivate\n -- If you are not allowed to toss any coin, then you are deterministic.\n Det : \u2200 {a} \u2192 Set a \u2192 Set a\n Det = \u21ba 0\n\nrunDet : \u2200 {a} {A : Set a} \u2192 Det A \u2192 A\nrunDet f = run\u21ba f []\n\ntoss : \u21ba 1 Bit\ntoss = mk head\n\nreturn\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A\nreturn\u21ba = mk \u2218 const\n\nmap\u21ba : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B\nmap\u21ba f x = mk (f \u2218 run\u21ba x)\n-- map\u21ba f x \u2257 x >>=\u2032 (return {0} \u2218 f)\n\njoin\u21ba : \u2200 {n\u2081 n\u2082 a} {A : Set a} \u2192 \u21ba n\u2081 (\u21ba n\u2082 A) \u2192 \u21ba (n\u2081 + n\u2082) A\njoin\u21ba {n\u2081} x = mk (\u03bb bs \u2192 run\u21ba (run\u21ba x (take _ bs)) (drop n\u2081 bs))\n-- join\u21ba x = x >>= id\n\ncomap : \u2200 {m n a} {A : Set a} \u2192 (Bits n \u2192 Bits m) \u2192 \u21ba m A \u2192 \u21ba n A\ncomap f (mk g) = mk (g \u2218 f)\n\nprivate\n take\u2264 : \u2200 {a} {A : Set a} {m n} \u2192 n \u2264 m \u2192 Vec A m \u2192 Vec A n\n take\u2264 z\u2264n _ = []\n take\u2264 (s\u2264s p) (x \u2237 xs) = x \u2237 take\u2264 p xs\n\nweaken\u2264 : \u2200 {m n a} {A : Set a} \u2192 m \u2264 n \u2192 \u21ba m A \u2192 \u21ba n A\nweaken\u2264 p = comap (take\u2264 p)\n\nopen flipbased \u21ba toss weaken\u2264 return\u21ba map\u21ba join\u21ba public\n\n_\u2257\u21ba_ : \u2200 {c a} {A : Set a} (f g : \u21ba c A) \u2192 Set a\nf \u2257\u21ba g = run\u21ba f \u2257 run\u21ba g\n\n\u2141 : \u2115 \u2192 Set\n\u2141 n = \u21ba n Bit\n\n_\u2257\u2141_ : \u2200 {c} (\u2141\u2080 \u2141\u2081 : Bit \u2192 \u2141 c) \u2192 Set\n\u2141\u2080 \u2257\u2141 \u2141\u2081 = \u2200 b \u2192 \u2141\u2080 b \u2257\u21ba \u2141\u2081 b\n\n\u2257\u2141-trans : \u2200 {c} \u2192 Transitive (_\u2257\u2141_ {c})\n\u2257\u2141-trans p q b R = \u2261.trans (p b R) (q b R)\n\ncount\u21ba\u1da0 : \u2200 {c} \u2192 \u2141 c \u2192 Fin (suc (2^ c))\ncount\u21ba\u1da0 f = #\u27e8 run\u21ba f \u27e9\u1da0\n\ncount\u21ba : \u2200 {c} \u2192 \u2141 c \u2192 \u2115\ncount\u21ba f = #\u27e8 run\u21ba f \u27e9\n\n\u2257\u21ba-cong-# : \u2200 {n} {f g : \u2141 n} \u2192 f \u2257\u21ba g \u2192 count\u21ba f \u2261 count\u21ba g\n\u2257\u21ba-cong-# {f = f} {g} = \u2257-cong-# (run\u21ba f) (run\u21ba g)\n\n_\u223c[_]\u2141_ : \u2200 {m n} \u2192 \u2141 m \u2192 (\u2115 \u2192 \u2115 \u2192 Set) \u2192 \u2141 n \u2192 Set\n_\u223c[_]\u2141_ {m} {n} f _\u223c_ g = \u27e82^ n * count\u21ba f \u27e9 \u223c \u27e82^ m * count\u21ba g \u27e9\n\n_\u223c[_]\u2141\u2032_ : \u2200 {n} \u2192 \u2141 n \u2192 (\u2115 \u2192 \u2115 \u2192 Set) \u2192 \u2141 n \u2192 Set\n_\u223c[_]\u2141\u2032_ {n} f _\u223c_ g = count\u21ba f \u223c count\u21ba g\n\n_\u2248\u2141_ : \u2200 {m n} \u2192 \u2141 m \u2192 \u2141 n \u2192 Set\nf \u2248\u2141 g = f \u223c[ _\u2261_ ]\u2141 g\n\n_\u2248\u2141\u2032_ : \u2200 {n} (f g : \u2141 n) \u2192 Set\nf \u2248\u2141\u2032 g = f \u223c[ _\u2261_ ]\u2141\u2032 g\n\n\u2248\u2141-refl : \u2200 {n} {f : \u2141 n} \u2192 f \u2248\u2141 f\n\u2248\u2141-refl = \u2261.refl\n\n\u2248\u2141-sym : \u2200 {n} \u2192 Symmetric {A = \u2141 n} _\u2248\u2141_\n\u2248\u2141-sym = \u2261.sym\n\n\u2248\u2141-trans : \u2200 {n} \u2192 Transitive {A = \u2141 n} _\u2248\u2141_\n\u2248\u2141-trans = \u2261.trans\n\n\u2257\u21d2\u2248\u2141 : \u2200 {c} {f g : \u2141 c} \u2192 f \u2257\u21ba g \u2192 f \u2248\u2141 g\n\u2257\u21d2\u2248\u2141 pf rewrite \u2257\u21ba-cong-# pf = \u2261.refl\n\n\u2248\u2141\u2032\u21d2\u2248\u2141 : \u2200 {n} {f g : \u2141 n} \u2192 f \u2248\u2141\u2032 g \u2192 f \u2248\u2141 g\n\u2248\u2141\u2032\u21d2\u2248\u2141 eq rewrite eq = \u2261.refl\n\n\u2248\u2141\u21d2\u2248\u2141\u2032 : \u2200 {n} {f g : \u2141 n} \u2192 f \u2248\u2141 g \u2192 f \u2248\u2141\u2032 g\n\u2248\u2141\u21d2\u2248\u2141\u2032 {n} = 2^-inj n\n\n\u2248\u2141-cong : \u2200 {c c'} {f g : \u2141 c} {f' g' : \u2141 c'} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f \u2248\u2141 f' \u2192 g \u2248\u2141 g'\n\u2248\u2141-cong f\u2257g f'\u2257g' f\u2248f' rewrite \u2257\u21ba-cong-# f\u2257g | \u2257\u21ba-cong-# f'\u2257g' = f\u2248f'\n\n\u2248\u2141\u2032-cong : \u2200 {c} {f g f' g' : \u2141 c} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f \u2248\u2141\u2032 f' \u2192 g \u2248\u2141\u2032 g'\n\u2248\u2141\u2032-cong f\u2257g f'\u2257g' f\u2248f' rewrite \u2257\u21ba-cong-# f\u2257g | \u2257\u21ba-cong-# f'\u2257g' = f\u2248f'\n\ndata Rat : Set where _\/_ : (num denom : \u2115) \u2192 Rat\n\nPr[_\u22611] : \u2200 {n} (f : \u2141 n) \u2192 Rat\nPr[_\u22611] {n} f = count\u21ba f \/ 2^ n\n","old_contents":"module flipbased-implem where\n\nopen import Function\nopen import Data.Bits\nopen import Data.Nat.NP\nopen import Data.Vec\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality as \u2261\nimport Data.Fin as Fin\nopen \u2261 using (_\u2257_; _\u2261_)\nopen Fin using (Fin; suc)\n\nimport flipbased\n\n-- \u201c\u21ba n A\u201d reads like: \u201ctoss n coins and then return a value of type A\u201d\nrecord \u21ba {a} n (A : Set a) : Set a where\n constructor mk\n field\n run\u21ba : Bits n \u2192 A\n\nopen \u21ba public\n\nprivate\n -- If you are not allowed to toss any coin, then you are deterministic.\n Det : \u2200 {a} \u2192 Set a \u2192 Set a\n Det = \u21ba 0\n\nrunDet : \u2200 {a} {A : Set a} \u2192 Det A \u2192 A\nrunDet f = run\u21ba f []\n\ntoss : \u21ba 1 Bit\ntoss = mk head\n\nreturn\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A\nreturn\u21ba = mk \u2218 const\n\nmap\u21ba : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B\nmap\u21ba f x = mk (f \u2218 run\u21ba x)\n-- map\u21ba f x \u2257 x >>=\u2032 (return {0} \u2218 f)\n\njoin\u21ba : \u2200 {n\u2081 n\u2082 a} {A : Set a} \u2192 \u21ba n\u2081 (\u21ba n\u2082 A) \u2192 \u21ba (n\u2081 + n\u2082) A\njoin\u21ba {n\u2081} x = mk (\u03bb bs \u2192 run\u21ba (run\u21ba x (take _ bs)) (drop n\u2081 bs))\n-- join\u21ba x = x >>= id\n\ncomap : \u2200 {m n a} {A : Set a} \u2192 (Bits n \u2192 Bits m) \u2192 \u21ba m A \u2192 \u21ba n A\ncomap f (mk g) = mk (g \u2218 f)\n\nprivate\n take\u2264 : \u2200 {a} {A : Set a} {m n} \u2192 n \u2264 m \u2192 Vec A m \u2192 Vec A n\n take\u2264 z\u2264n _ = []\n take\u2264 (s\u2264s p) (x \u2237 xs) = x \u2237 take\u2264 p xs\n\nweaken\u2264 : \u2200 {m n a} {A : Set a} \u2192 m \u2264 n \u2192 \u21ba m A \u2192 \u21ba n A\nweaken\u2264 p = comap (take\u2264 p)\n\nopen flipbased \u21ba toss weaken\u2264 return\u21ba map\u21ba join\u21ba public\n\n_\u2257\u21ba_ : \u2200 {c a} {A : Set a} (f g : \u21ba c A) \u2192 Set a\nf \u2257\u21ba g = run\u21ba f \u2257 run\u21ba g\n\n\u2141 : \u2115 \u2192 Set\n\u2141 n = \u21ba n Bit\n\n_\u2257\u2141_ : \u2200 {c} (\u2141\u2080 \u2141\u2081 : Bit \u2192 \u2141 c) \u2192 Set\n\u2141\u2080 \u2257\u2141 \u2141\u2081 = \u2200 b \u2192 \u2141\u2080 b \u2257\u21ba \u2141\u2081 b\n\n\u2257\u2141-trans : \u2200 {c} \u2192 Transitive (_\u2257\u2141_ {c})\n\u2257\u2141-trans p q b R = \u2261.trans (p b R) (q b R)\n\ncount\u21ba\u1da0 : \u2200 {c} \u2192 \u2141 c \u2192 Fin (suc (2^ c))\ncount\u21ba\u1da0 f = #\u27e8 run\u21ba f \u27e9\u1da0\n\ncount\u21ba : \u2200 {c} \u2192 \u2141 c \u2192 \u2115\ncount\u21ba = Fin.to\u2115 \u2218 count\u21ba\u1da0\n\n_\u223c[_]\u2141_ : \u2200 {m n} \u2192 \u2141 m \u2192 (\u2115 \u2192 \u2115 \u2192 Set) \u2192 \u2141 n \u2192 Set\n_\u223c[_]\u2141_ {m} {n} f _\u223c_ g = \u27e82^ n * count\u21ba f \u27e9 \u223c \u27e82^ m * count\u21ba g \u27e9\n\n_\u223c[_]\u2141\u2032_ : \u2200 {n} \u2192 \u2141 n \u2192 (\u2115 \u2192 \u2115 \u2192 Set) \u2192 \u2141 n \u2192 Set\n_\u223c[_]\u2141\u2032_ {n} f _\u223c_ g = count\u21ba f \u223c count\u21ba g\n\n_\u2248\u2141_ : \u2200 {m n} \u2192 \u2141 m \u2192 \u2141 n \u2192 Set\nf \u2248\u2141 g = f \u223c[ _\u2261_ ]\u2141 g\n\n_\u2248\u2141\u2032_ : \u2200 {n} (f g : \u2141 n) \u2192 Set\nf \u2248\u2141\u2032 g = f \u223c[ _\u2261_ ]\u2141\u2032 g\n\n\u2248\u2141-refl : \u2200 {n} {f : \u2141 n} \u2192 f \u2248\u2141 f\n\u2248\u2141-refl = \u2261.refl\n\n\u2248\u2141-sym : \u2200 {n} \u2192 Symmetric {A = \u2141 n} _\u2248\u2141_\n\u2248\u2141-sym = \u2261.sym\n\n\u2248\u2141-trans : \u2200 {n} \u2192 Transitive {A = \u2141 n} _\u2248\u2141_\n\u2248\u2141-trans = \u2261.trans\n\n\u2257\u21d2\u2248\u2141 : \u2200 {c} {f g : \u2141 c} \u2192 f \u2257\u21ba g \u2192 f \u2248\u2141 g\n\u2257\u21d2\u2248\u2141 pf rewrite ext-# pf = \u2261.refl\n\n\u2248\u2141\u2032\u21d2\u2248\u2141 : \u2200 {n} {f g : \u2141 n} \u2192 f \u2248\u2141\u2032 g \u2192 f \u2248\u2141 g\n\u2248\u2141\u2032\u21d2\u2248\u2141 eq rewrite eq = \u2261.refl\n\n\u2248\u2141\u21d2\u2248\u2141\u2032 : \u2200 {n} {f g : \u2141 n} \u2192 f \u2248\u2141 g \u2192 f \u2248\u2141\u2032 g\n\u2248\u2141\u21d2\u2248\u2141\u2032 {n} = 2^-inj n\n\n\u2248\u2141-cong : \u2200 {c c'} {f g : \u2141 c} {f' g' : \u2141 c'} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f \u2248\u2141 f' \u2192 g \u2248\u2141 g'\n\u2248\u2141-cong f\u2257g f'\u2257g' f\u2248f' rewrite ext-# f\u2257g | ext-# f'\u2257g' = f\u2248f'\n\n\u2248\u2141\u2032-cong : \u2200 {c} {f g f' g' : \u2141 c} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f \u2248\u2141\u2032 f' \u2192 g \u2248\u2141\u2032 g'\n\u2248\u2141\u2032-cong f\u2257g f'\u2257g' f\u2248f' rewrite ext-# f\u2257g | ext-# f'\u2257g' = f\u2248f'\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"b590f8a3ed001a65516e5f2211013e1a46683692","subject":"Update binds","message":"Update binds\n","repos":"crypto-agda\/crypto-agda","old_file":"ZK\/PartialHeliosVerifier.agda","new_file":"ZK\/PartialHeliosVerifier.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule ZK.PartialHeliosVerifier where\n\nopen import Type\nopen import Function hiding (case_of_)\nopen import Data.Bool.Base\nopen import Data.Product\nopen import Data.List.Base using (List; []; _\u2237_; and; foldr)\nopen import Data.String.Base using (String)\n\nopen import FFI.JS as JS hiding (_+_; _\/_; _*_; join)\nopen import FFI.JS.BigI as BigI\nimport FFI.JS.Console as Console\nimport FFI.JS.Process as Process\nimport FFI.JS.FS as FS\nopen import FFI.JS.SHA1\nopen import FFI.JS.Proc\nopen import Control.Process.Type\n\njoin : String \u2192 List String \u2192 String\njoin sep [] = \"\"\njoin sep (x \u2237 xs) = x ++ foldr (\u03bb y z \u2192 sep ++ y ++ z) \"\" xs\n\n{-\ninstance\n showBigI = mk \u03bb _ \u2192 showString \u2218 BigI.toString\n-}\n\nG = BigI\n\u2124q = BigI\n\nBigI\u25b9G : BigI \u2192 G\nBigI\u25b9G = id\n\nBigI\u25b9\u2124q : BigI \u2192 \u2124q\nBigI\u25b9\u2124q = id\n\nnon0I : BigI \u2192 BigI\nnon0I x with equals x 0I\n... | true = throw \"Should be non zero!\" 0I\n... | false = x\n\nmodule BigICG (p q : BigI) where\n _^_ : G \u2192 \u2124q \u2192 G\n _\u00b7_ : G \u2192 G \u2192 G\n _\/_ : G \u2192 G \u2192 G\n _+_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n _*_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n _==_ : (x y : G) \u2192 Bool\n\n _^_ = \u03bb x y \u2192 modPow x y p\n _\u00b7_ = \u03bb x y \u2192 mod (multiply x y) p\n _\/_ = \u03bb x y \u2192 mod (multiply x (modInv (non0I y) p)) p\n _+_ = \u03bb x y \u2192 mod (add x y) q\n _*_ = \u03bb x y \u2192 mod (multiply x y) q\n _==_ = equals\n\n sumI : List BigI \u2192 BigI\n sumI = foldr _+_ 0I\n\nbignum : Number \u2192 BigI\nbignum n = bigI (Number\u25b9String n) \"10\"\n\n-- TODO check with undefined\nbigdec : JSValue \u2192 BigI\nbigdec v = bigI (castString v) \"10\"\n\n{-\nprint : {A : Set}{{_ : Show A}} \u2192 A \u2192 Callback0\nprint = Console.log \u2218 show\n-}\n\nPubKey = G\nEncRnd = \u2124q {- randomness used for encryption of ct -}\nMessage = G {- plain text message -}\nChallenge = \u2124q\nResponse = \u2124q\nCommitmentPart = G\nCipherTextPart = G\n\nmodule HeliosVerifyV3 (g : G) p q (y : PubKey) where\n open BigICG p q\n\n verify-chaum-pedersen : (\u03b1 \u03b2 : CipherTextPart)(M : Message)(A B : G)(c : Challenge)(s : Response) \u2192 Bool\n verify-chaum-pedersen \u03b1 \u03b2 M A B c s\n = trace \"\u03b1=\" \u03b1 \u03bb _ \u2192\n trace \"\u03b2=\" \u03b2 \u03bb _ \u2192\n trace \"M=\" M \u03bb _ \u2192\n trace \"A=\" A \u03bb _ \u2192\n trace \"B=\" B \u03bb _ \u2192\n trace \"c=\" c \u03bb _ \u2192\n trace \"s=\" s \u03bb _ \u2192\n (g ^ s) == (A \u00b7 (\u03b1 ^ c))\n \u2227 (y ^ s) == (B \u00b7 ((\u03b2 \/ M) ^ c))\n\n verify-individual-proof : (\u03b1 \u03b2 : CipherTextPart)(ix : Number)(\u03c0 : JSValue) \u2192 Bool\n verify-individual-proof \u03b1 \u03b2 ix \u03c0\n = trace \"m=\" m \u03bb _ \u2192\n trace \"M=\" M \u03bb _ \u2192\n trace \"commitmentA=\" A \u03bb _ \u2192\n trace \"commitmentB=\" B \u03bb _ \u2192\n trace \"challenge=\" c \u03bb _ \u2192\n trace \"response=\" s \u03bb _ \u2192\n res\n where\n m = bignum ix\n M = g ^ m\n A = bigdec (\u03c0 \u00b7\u00ab \"commitment\" \u00bb \u00b7\u00ab \"A\" \u00bb)\n B = bigdec (\u03c0 \u00b7\u00ab \"commitment\" \u00bb \u00b7\u00ab \"B\" \u00bb)\n c = bigdec (\u03c0 \u00b7\u00ab \"challenge\" \u00bb)\n s = bigdec (\u03c0 \u00b7\u00ab \"response\" \u00bb)\n res = verify-chaum-pedersen \u03b1 \u03b2 M A B c s\n\n -- Conform to HeliosV3 but it is too weak.\n -- One should follow a \"Strong Fiat Shamir\" transformation\n -- from interactive ZK proof to non-interactive ZK proofs.\n -- Namely one should hash the statement as well.\n --\n -- SHA1(A0 + \",\" + B0 + \",\" + A1 + \",\" + B1 + ... + \"Amax\" + \",\" + Bmax)\n hash-commitments : JSArray JSValue \u2192 BigI\n hash-commitments \u03c0s =\n fromHex $\n trace-call \"SHA1(commitments)=\" SHA1 $\n trace-call \"commitments=\" (join \",\") $\n decodeJSArray \u03c0s \u03bb _ \u03c0 \u2192\n (castString (\u03c0 \u00b7\u00ab \"commitment\" \u00bb \u00b7\u00ab \"A\" \u00bb))\n ++ \",\" ++\n (castString (\u03c0 \u00b7\u00ab \"commitment\" \u00bb \u00b7\u00ab \"B\" \u00bb))\n\n sum-challenges : JSArray JSValue \u2192 BigI\n sum-challenges \u03c0s =\n sumI $ decodeJSArray \u03c0s \u03bb _ \u03c0 \u2192 bigdec (\u03c0 \u00b7\u00ab \"challenge\" \u00bb)\n\n verify-challenges : JSArray JSValue \u2192 Bool\n verify-challenges \u03c0s =\n trace \"hash(commitments)=\" h \u03bb _ \u2192\n trace \"sum(challenge)=\" c \u03bb _ \u2192\n h == c\n where\n h = hash-commitments \u03c0s\n c = sum-challenges \u03c0s\n\n verify-choice : (v : JSValue)(\u03c0s : JSArray JSValue) \u2192 Bool\n verify-choice v \u03c0s =\n trace \"\u03b1=\" \u03b1 \u03bb _ \u2192\n trace \"\u03b2=\" \u03b2 \u03bb _ \u2192\n verify-challenges \u03c0s\n \u2227\n and (decodeJSArray \u03c0s $ verify-individual-proof \u03b1 \u03b2)\n where\n \u03b1 = bigdec (v \u00b7\u00ab \"alpha\" \u00bb)\n \u03b2 = bigdec (v \u00b7\u00ab \"beta\" \u00bb)\n\n verify-choices : (choices \u03c0s : JSArray JSValue) \u2192 Bool\n verify-choices choices \u03c0s =\n trace \"TODO: check the array size of choices \u03c0s together election data\" \"\" \u03bb _ \u2192\n and $ decodeJSArray choices \u03bb ix choice \u2192\n trace \"verify choice \" ix \u03bb _ \u2192\n verify-choice choice (castJSArray (\u03c0s Array[ ix ]))\n\n verify-answer : (answer : JSValue) \u2192 Bool\n verify-answer answer =\n trace \"TODO: CHECK the overall_proof\" \"\" \u03bb _ \u2192\n verify-choices choices individual-proofs\n where\n choices = answer \u00b7\u00ab \"choices\" \u00bbA\n individual-proofs = answer \u00b7\u00ab \"individual_proofs\" \u00bbA\n overall-proof = answer \u00b7\u00ab \"overall_proof\" \u00bbA\n\n verify-answers : (answers : JSArray JSValue) \u2192 Bool\n verify-answers answers =\n and $ decodeJSArray answers \u03bb ix a \u2192\n trace \"verify answer \" ix \u03bb _ \u2192\n verify-answer a\n\n {-\n verify-election-hash =\n \"\"\n -- notice the whitespaces and the alphabetical order of keys\n -- {\"email\": [\"ben@adida.net\", \"ben@mit.edu\"], \"first_name\": \"Ben\", \"last_name\": \"Adida\"}\n computed_hash = base64.b64encode(hash.new(election.toJSON()).digest())[:-1]\n computed_hash == vote.election_hash:\n \"\"\n -}\n\n -- module _ {-(election : JSValue)-} where\n verify-ballot : (ballot : JSValue) \u2192 Bool\n verify-ballot ballot =\n trace \"TODO: CHECK the election_hash: \" election_hash \u03bb _ \u2192\n trace \"TODO: CHECK the election_uuid: \" election_uuid \u03bb _ \u2192\n verify-answers answers\n where\n answers = ballot \u00b7\u00ab \"answers\" \u00bbA\n election_hash = ballot \u00b7\u00ab \"election_hash\" \u00bb\n election_uuid = ballot \u00b7\u00ab \"election_uuid\" \u00bb\n\n verify-ballots : (ballots : JSArray JSValue) \u2192 Bool\n verify-ballots ballots =\n and $ decodeJSArray ballots \u03bb ix ballot \u2192\n trace \"verify ballot \" ix \u03bb _ \u2192\n verify-ballot ballot\n\n-- Many checks are still missing!\nverify-helios-election : (arg : JSValue) \u2192 Bool\nverify-helios-election arg = trace \"res=\" res id\n where\n ed = arg \u00b7\u00ab \"election_data\" \u00bb\n bs = arg \u00b7\u00ab \"ballots\" \u00bbA\n pk = ed \u00b7\u00ab \"public_key\" \u00bb\n g = bigdec (pk \u00b7\u00ab \"g\" \u00bb)\n p = bigdec (pk \u00b7\u00ab \"p\" \u00bb)\n q = bigdec (pk \u00b7\u00ab \"q\" \u00bb)\n y = bigdec (pk \u00b7\u00ab \"y\" \u00bb)\n res =\n trace \"g=\" g \u03bb _ \u2192\n trace \"p=\" p \u03bb _ \u2192\n trace \"q=\" q \u03bb _ \u2192\n trace \"y=\" y \u03bb _ \u2192\n HeliosVerifyV3.verify-ballots g p q y bs\n\nsrv : URI \u2192 JSProc\nsrv d =\n recv d \u03bb q \u2192\n send d (fromBool (verify-helios-election q))\n end\n\n-- Working around Agda.Primitive.lsuc being undefined\ncase_of_ : {A : Set} {B : Set} \u2192 A \u2192 (A \u2192 B) \u2192 B\ncase x of f = f x\n\nmain : JS!\nmain =\n Process.argv >>= \u03bb args \u2192\n case JSArray\u25b9ListString args of \u03bb {\n (_node \u2237 _run \u2237 _test \u2237 args') \u2192\n case args' of \u03bb {\n [] \u2192\n server \"127.0.0.1\" \"1337\" srv >>= \u03bb uri \u2192\n Console.log (showURI uri)\n ; (arg \u2237 args'') \u2192\n case args'' of \u03bb {\n [] \u2192\n let opts =\n -- fromJSObject (fromObject ((\"encoding\" , fromString \"utf8\") \u2237 []))\n -- nullJS\n JSON-parse \"{\\\"encoding\\\":\\\"utf8\\\"}\"\n in\n Console.log (\"readFile=\" ++ arg) >>\n FS.readFile arg opts >>== \u03bb err dat \u2192\n Console.log (\"readFile: err=\" ++ JS.toString err) >>\n Console.log (Bool\u25b9String (verify-helios-election (JSON-parse (castString dat))))\n ; _ \u2192\n Console.log \"usage\"\n }\n }\n ; _ \u2192\n Console.log \"usage\"\n }\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule ZK.PartialHeliosVerifier where\n\nopen import Type\nopen import Function hiding (case_of_)\nopen import Data.Bool.Base\nopen import Data.Product\nopen import Data.List.Base using (List; []; _\u2237_; and; foldr)\nopen import Data.String.Base using (String)\n\nopen import FFI.JS as JS hiding (_+_; _\/_; _*_; join)\nopen import FFI.JS.BigI as BigI\nimport FFI.JS.Console as Console\nimport FFI.JS.Process as Process\nimport FFI.JS.FS as FS\nopen import FFI.JS.SHA1\nopen import FFI.JS.Proc\nopen import Control.Process.Type\n\njoin : String \u2192 List String \u2192 String\njoin sep [] = \"\"\njoin sep (x \u2237 xs) = x ++ foldr (\u03bb y z \u2192 sep ++ y ++ z) \"\" xs\n\n{-\ninstance\n showBigI = mk \u03bb _ \u2192 showString \u2218 BigI.toString\n-}\n\nG = BigI\n\u2124q = BigI\n\nBigI\u25b9G : BigI \u2192 G\nBigI\u25b9G = id\n\nBigI\u25b9\u2124q : BigI \u2192 \u2124q\nBigI\u25b9\u2124q = id\n\nnon0I : BigI \u2192 BigI\nnon0I x with equals x 0I\n... | true = throw \"Should be non zero!\" 0I\n... | false = x\n\nmodule BigICG (p q : BigI) where\n _^_ : G \u2192 \u2124q \u2192 G\n _\u00b7_ : G \u2192 G \u2192 G\n _\/_ : G \u2192 G \u2192 G\n _+_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n _*_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n _==_ : (x y : G) \u2192 Bool\n\n _^_ = \u03bb x y \u2192 modPow x y p\n _\u00b7_ = \u03bb x y \u2192 mod (multiply x y) p\n _\/_ = \u03bb x y \u2192 mod (multiply x (modInv (non0I y) p)) p\n _+_ = \u03bb x y \u2192 mod (add x y) q\n _*_ = \u03bb x y \u2192 mod (multiply x y) q\n _==_ = equals\n\n sumI : List BigI \u2192 BigI\n sumI = foldr _+_ 0I\n\nbignum : Number \u2192 BigI\nbignum n = bigI (Number\u25b9String n) \"10\"\n\n-- TODO check with undefined\nbigdec : JSValue \u2192 BigI\nbigdec v = bigI (castString v) \"10\"\n\n{-\nprint : {A : Set}{{_ : Show A}} \u2192 A \u2192 Callback0\nprint = Console.log \u2218 show\n-}\n\nPubKey = G\nEncRnd = \u2124q {- randomness used for encryption of ct -}\nMessage = G {- plain text message -}\nChallenge = \u2124q\nResponse = \u2124q\nCommitmentPart = G\nCipherTextPart = G\n\nmodule HeliosVerifyV3 (g : G) p q (y : PubKey) where\n open BigICG p q\n\n verify-chaum-pedersen : (\u03b1 \u03b2 : CipherTextPart)(M : Message)(A B : G)(c : Challenge)(s : Response) \u2192 Bool\n verify-chaum-pedersen \u03b1 \u03b2 M A B c s\n = trace \"\u03b1=\" \u03b1 \u03bb _ \u2192\n trace \"\u03b2=\" \u03b2 \u03bb _ \u2192\n trace \"M=\" M \u03bb _ \u2192\n trace \"A=\" A \u03bb _ \u2192\n trace \"B=\" B \u03bb _ \u2192\n trace \"c=\" c \u03bb _ \u2192\n trace \"s=\" s \u03bb _ \u2192\n (g ^ s) == (A \u00b7 (\u03b1 ^ c))\n \u2227 (y ^ s) == (B \u00b7 ((\u03b2 \/ M) ^ c))\n\n verify-individual-proof : (\u03b1 \u03b2 : CipherTextPart)(ix : Number)(\u03c0 : JSValue) \u2192 Bool\n verify-individual-proof \u03b1 \u03b2 ix \u03c0\n = trace \"m=\" m \u03bb _ \u2192\n trace \"M=\" M \u03bb _ \u2192\n trace \"commitmentA=\" A \u03bb _ \u2192\n trace \"commitmentB=\" B \u03bb _ \u2192\n trace \"challenge=\" c \u03bb _ \u2192\n trace \"response=\" s \u03bb _ \u2192\n res\n where\n m = bignum ix\n M = g ^ m\n A = bigdec (\u03c0 \u00b7\u00ab \"commitment\" \u00bb \u00b7\u00ab \"A\" \u00bb)\n B = bigdec (\u03c0 \u00b7\u00ab \"commitment\" \u00bb \u00b7\u00ab \"B\" \u00bb)\n c = bigdec (\u03c0 \u00b7\u00ab \"challenge\" \u00bb)\n s = bigdec (\u03c0 \u00b7\u00ab \"response\" \u00bb)\n res = verify-chaum-pedersen \u03b1 \u03b2 M A B c s\n\n -- Conform to HeliosV3 but it is too weak.\n -- One should follow a \"Strong Fiat Shamir\" transformation\n -- from interactive ZK proof to non-interactive ZK proofs.\n -- Namely one should hash the statement as well.\n --\n -- SHA1(A0 + \",\" + B0 + \",\" + A1 + \",\" + B1 + ... + \"Amax\" + \",\" + Bmax)\n hash-commitments : JSArray JSValue \u2192 BigI\n hash-commitments \u03c0s =\n fromHex $\n trace-call \"SHA1(commitments)=\" SHA1 $\n trace-call \"commitments=\" (join \",\") $\n decodeJSArray \u03c0s \u03bb _ \u03c0 \u2192\n (castString (\u03c0 \u00b7\u00ab \"commitment\" \u00bb \u00b7\u00ab \"A\" \u00bb))\n ++ \",\" ++\n (castString (\u03c0 \u00b7\u00ab \"commitment\" \u00bb \u00b7\u00ab \"B\" \u00bb))\n\n sum-challenges : JSArray JSValue \u2192 BigI\n sum-challenges \u03c0s =\n sumI $ decodeJSArray \u03c0s \u03bb _ \u03c0 \u2192 bigdec (\u03c0 \u00b7\u00ab \"challenge\" \u00bb)\n\n verify-challenges : JSArray JSValue \u2192 Bool\n verify-challenges \u03c0s =\n trace \"hash(commitments)=\" h \u03bb _ \u2192\n trace \"sum(challenge)=\" c \u03bb _ \u2192\n h == c\n where\n h = hash-commitments \u03c0s\n c = sum-challenges \u03c0s\n\n verify-choice : (v : JSValue)(\u03c0s : JSArray JSValue) \u2192 Bool\n verify-choice v \u03c0s =\n trace \"\u03b1=\" \u03b1 \u03bb _ \u2192\n trace \"\u03b2=\" \u03b2 \u03bb _ \u2192\n verify-challenges \u03c0s\n \u2227\n and (decodeJSArray \u03c0s $ verify-individual-proof \u03b1 \u03b2)\n where\n \u03b1 = bigdec (v \u00b7\u00ab \"alpha\" \u00bb)\n \u03b2 = bigdec (v \u00b7\u00ab \"beta\" \u00bb)\n\n verify-choices : (choices \u03c0s : JSArray JSValue) \u2192 Bool\n verify-choices choices \u03c0s =\n trace \"TODO: check the array size of choices \u03c0s together election data\" \"\" \u03bb _ \u2192\n and $ decodeJSArray choices \u03bb ix choice \u2192\n trace \"verify choice \" ix \u03bb _ \u2192\n verify-choice choice (castJSArray (\u03c0s Array[ ix ]))\n\n verify-answer : (answer : JSValue) \u2192 Bool\n verify-answer answer =\n trace \"TODO: CHECK the overall_proof\" \"\" \u03bb _ \u2192\n verify-choices choices individual-proofs\n where\n choices = answer \u00b7\u00ab \"choices\" \u00bbA\n individual-proofs = answer \u00b7\u00ab \"individual_proofs\" \u00bbA\n overall-proof = answer \u00b7\u00ab \"overall_proof\" \u00bbA\n\n verify-answers : (answers : JSArray JSValue) \u2192 Bool\n verify-answers answers =\n and $ decodeJSArray answers \u03bb ix a \u2192\n trace \"verify answer \" ix \u03bb _ \u2192\n verify-answer a\n\n {-\n verify-election-hash =\n \"\"\n -- notice the whitespaces and the alphabetical order of keys\n -- {\"email\": [\"ben@adida.net\", \"ben@mit.edu\"], \"first_name\": \"Ben\", \"last_name\": \"Adida\"}\n computed_hash = base64.b64encode(hash.new(election.toJSON()).digest())[:-1]\n computed_hash == vote.election_hash:\n \"\"\n -}\n\n -- module _ {-(election : JSValue)-} where\n verify-ballot : (ballot : JSValue) \u2192 Bool\n verify-ballot ballot =\n trace \"TODO: CHECK the election_hash: \" election_hash \u03bb _ \u2192\n trace \"TODO: CHECK the election_uuid: \" election_uuid \u03bb _ \u2192\n verify-answers answers\n where\n answers = ballot \u00b7\u00ab \"answers\" \u00bbA\n election_hash = ballot \u00b7\u00ab \"election_hash\" \u00bb\n election_uuid = ballot \u00b7\u00ab \"election_uuid\" \u00bb\n\n verify-ballots : (ballots : JSArray JSValue) \u2192 Bool\n verify-ballots ballots =\n and $ decodeJSArray ballots \u03bb ix ballot \u2192\n trace \"verify ballot \" ix \u03bb _ \u2192\n verify-ballot ballot\n\n-- Many checks are still missing!\nverify-helios-election : (arg : JSValue) \u2192 Bool\nverify-helios-election arg = trace \"res=\" res id\n where\n ed = arg \u00b7\u00ab \"election_data\" \u00bb\n bs = arg \u00b7\u00ab \"ballots\" \u00bbA\n pk = ed \u00b7\u00ab \"public_key\" \u00bb\n g = bigdec (pk \u00b7\u00ab \"g\" \u00bb)\n p = bigdec (pk \u00b7\u00ab \"p\" \u00bb)\n q = bigdec (pk \u00b7\u00ab \"q\" \u00bb)\n y = bigdec (pk \u00b7\u00ab \"y\" \u00bb)\n res =\n trace \"g=\" g \u03bb _ \u2192\n trace \"p=\" p \u03bb _ \u2192\n trace \"q=\" q \u03bb _ \u2192\n trace \"y=\" y \u03bb _ \u2192\n HeliosVerifyV3.verify-ballots g p q y bs\n\nsrv : URI \u2192 JSProc\nsrv d =\n recv d \u03bb q \u2192\n send d (fromBool (verify-helios-election q))\n end\n\n-- Working around Agda.Primitive.lsuc being undefined\ncase_of_ : {A : Set} {B : Set} \u2192 A \u2192 (A \u2192 B) \u2192 B\ncase x of f = f x\n\nmain : JS!\nmain =\n Process.argv !\u2081 \u03bb args \u2192\n case JSArray\u25b9ListString args of \u03bb {\n (_node \u2237 _run \u2237 _test \u2237 args') \u2192\n case args' of \u03bb {\n [] \u2192\n server \"127.0.0.1\" \"1337\" srv !\u2081 \u03bb uri \u2192\n Console.log (showURI uri)\n ; (arg \u2237 args'') \u2192\n case args'' of \u03bb {\n [] \u2192\n let opts =\n -- fromJSObject (fromObject ((\"encoding\" , fromString \"utf8\") \u2237 []))\n -- nullJS\n JSON-parse \"{\\\"encoding\\\":\\\"utf8\\\"}\"\n in\n Console.log (\"readFile=\" ++ arg) >>\n FS.readFile arg opts !\u2082 \u03bb err dat \u2192\n Console.log (\"readFile: err=\" ++ JS.toString err) >>\n Console.log (Bool\u25b9String (verify-helios-election (JSON-parse (castString dat))))\n ; _ \u2192\n Console.log \"usage\"\n }\n }\n ; _ \u2192\n Console.log \"usage\"\n }\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"d65ed0113d28c841876548e54828896460cd3411","subject":"Cosmetic changes.","message":"Cosmetic changes.\n\nIgnore-this: 280c70464ba8834c70dba89505eaa3e7\n\ndarcs-hash:20110211172355-3bd4e-da68e0330b95249a06ca8fa24c3ba199be3697d1.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/Mirror\/MirrorStdLib.agda","new_file":"Draft\/Mirror\/MirrorStdLib.agda","new_contents":"-- Tested with the darcs version of the standard library on 11 February 2011.\n\nmodule MirrorStdLib where\n\nopen import Algebra\nopen import Data.List as List hiding ( reverse )\nopen import Data.List.Properties\n\nopen import Relation.Binary.PropositionalEquality\nopen \u2261-Reasoning\n\nmodule LM {A : Set} = Monoid (List.monoid A)\n\n------------------------------------------------------------------------------\n\nreverse : {A : Set} \u2192 List A \u2192 List A\nreverse [] = []\nreverse (x \u2237 xs) = reverse xs ++ x \u2237 []\n\n++-rightIdentity : {A : Set}(xs : List A) \u2192 xs ++ [] \u2261 xs\n++-rightIdentity [] = refl\n++-rightIdentity (x \u2237 xs) = cong (_\u2237_ x) (++-rightIdentity xs)\n\nreverse-++ : {A : Set}(xs ys : List A) \u2192\n reverse (xs ++ ys) \u2261 reverse ys ++ reverse xs\nreverse-++ [] ys = sym (++-rightIdentity (reverse ys))\nreverse-++ (x \u2237 xs) [] = cong (\u03bb x' \u2192 reverse x' ++ x \u2237 []) (++-rightIdentity xs)\nreverse-++ (x \u2237 xs) (y \u2237 ys) =\n begin\n reverse (xs ++ y \u2237 ys) ++ x \u2237 []\n \u2261\u27e8 cong (\u03bb x' \u2192 x' ++ x \u2237 []) (reverse-++ xs (y \u2237 ys)) \u27e9\n (reverse (y \u2237 ys) ++ reverse xs) ++ x \u2237 []\n \u2261\u27e8 LM.assoc (reverse (y \u2237 ys)) (reverse xs) (x \u2237 []) \u27e9\n reverse (y \u2237 ys) ++ reverse (x \u2237 xs)\n \u220e\n\ndata Tree (A : Set) : Set where\n treeT : A \u2192 List (Tree A) \u2192 Tree A\n\nmirror : {A : Set} \u2192 Tree A \u2192 Tree A\nmirror (treeT a ts) = treeT a (reverse (map mirror ts))\n\nmutual\n mirror\u00b2 : {A : Set} \u2192 (t : Tree A) \u2192 mirror (mirror t) \u2261 t\n mirror\u00b2 (treeT a []) = refl\n mirror\u00b2 (treeT a (t \u2237 ts)) =\n begin\n treeT a (reverse (map mirror (reverse (map mirror ts) ++ mirror t \u2237 [])))\n \u2261\u27e8 subst (\u03bb x \u2192 treeT a (reverse (map mirror (reverse (map mirror ts) ++\n mirror t \u2237 []))) \u2261\n treeT a x)\n (aux (t \u2237 ts))\n refl\n \u27e9\n treeT a (t \u2237 ts)\n \u220e\n\n aux : {A : Set} \u2192 (ts : List (Tree A)) \u2192\n reverse (map mirror (reverse (map mirror ts))) \u2261 ts\n aux [] = refl\n aux (t \u2237 ts) =\n begin\n reverse (map mirror (reverse (map mirror ts) ++ mirror t \u2237 []))\n \u2261\u27e8 subst (\u03bb x \u2192 (reverse (map mirror (reverse (map mirror ts) ++\n mirror t \u2237 []))) \u2261\n reverse x)\n (map-++-commute mirror (reverse (map mirror ts)) (mirror t \u2237 []))\n refl\n \u27e9\n reverse (map mirror (reverse (map mirror ts)) ++\n (map mirror (mirror t \u2237 [])))\n \u2261\u27e8 subst (\u03bb x \u2192 (reverse (map mirror (reverse (map mirror ts)) ++\n (map mirror (mirror t \u2237 [])))) \u2261\n x)\n (reverse-++ (map mirror (reverse (map mirror ts)))\n (map mirror (mirror t \u2237 [])))\n refl\n \u27e9\n reverse (map mirror (mirror t \u2237 [])) ++\n reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 refl \u27e9\n mirror (mirror t) \u2237 reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 subst (\u03bb x \u2192 (mirror (mirror t) \u2237\n reverse (map mirror (reverse (map mirror ts)))) \u2261\n (x \u2237 reverse (map mirror (reverse (map mirror ts)))))\n (mirror\u00b2 t) -- IH.\n refl\n \u27e9\n t \u2237 reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 subst (\u03bb x \u2192 t \u2237 reverse (map mirror (reverse (map mirror ts))) \u2261\n t \u2237 x)\n (aux ts)\n refl\n \u27e9\n t \u2237 ts\n \u220e\n","old_contents":"module MirrorStdLib where\n\nopen import Algebra\nopen import Data.List as List hiding ( reverse )\nopen import Data.List.Properties\n\nopen import Relation.Binary.PropositionalEquality\nopen \u2261-Reasoning\n\nmodule LM {A : Set} = Monoid (List.monoid A)\n\n------------------------------------------------------------------------------\n\nreverse : {A : Set} \u2192 List A \u2192 List A\nreverse [] = []\nreverse (x \u2237 xs) = reverse xs ++ x \u2237 []\n\n++-rightIdentity : {A : Set}(xs : List A) \u2192 xs ++ [] \u2261 xs\n++-rightIdentity [] = refl\n++-rightIdentity (x \u2237 xs) = cong (_\u2237_ x) (++-rightIdentity xs)\n\nreverse-++ : {A : Set}(xs ys : List A) \u2192\n reverse (xs ++ ys) \u2261 reverse ys ++ reverse xs\nreverse-++ [] ys = sym (++-rightIdentity (reverse ys))\nreverse-++ (x \u2237 xs) [] = cong (\u03bb x' \u2192 reverse x' ++ x \u2237 []) (++-rightIdentity xs)\nreverse-++ (x \u2237 xs) (y \u2237 ys) =\n begin\n reverse (xs ++ y \u2237 ys) ++ x \u2237 []\n \u2261\u27e8 cong (\u03bb x' \u2192 x' ++ x \u2237 []) (reverse-++ xs (y \u2237 ys)) \u27e9\n (reverse (y \u2237 ys) ++ reverse xs) ++ x \u2237 []\n \u2261\u27e8 LM.assoc (reverse (y \u2237 ys)) (reverse xs) (x \u2237 []) \u27e9\n reverse (y \u2237 ys) ++ reverse (x \u2237 xs)\n \u220e\n\ndata Tree (A : Set) : Set where\n treeT : A \u2192 List (Tree A) \u2192 Tree A\n\nmirror : {A : Set} \u2192 Tree A \u2192 Tree A\nmirror (treeT a ts) = treeT a (reverse (map mirror ts))\n\nmutual\n mirror\u00b2 : {A : Set} \u2192 (t : Tree A) \u2192 mirror (mirror t) \u2261 t\n mirror\u00b2 (treeT a []) = refl\n mirror\u00b2 (treeT a (t \u2237 ts)) =\n begin\n treeT a (reverse (map mirror (reverse (map mirror ts) ++ mirror t \u2237 [])))\n \u2261\u27e8 subst (\u03bb x \u2192 treeT a (reverse (map mirror (reverse (map mirror ts) ++\n mirror t \u2237 []))) \u2261\n treeT a x)\n (aux (t \u2237 ts))\n refl\n \u27e9\n treeT a (t \u2237 ts)\n \u220e\n\n aux : {A : Set} \u2192 (ts : List (Tree A)) \u2192\n reverse (map mirror (reverse (map mirror ts))) \u2261 ts\n aux [] = refl\n aux (t \u2237 ts) =\n begin\n reverse (map mirror (reverse (map mirror ts) ++ mirror t \u2237 []))\n \u2261\u27e8 subst (\u03bb x \u2192 (reverse (map mirror (reverse (map mirror ts) ++\n mirror t \u2237 []))) \u2261\n reverse x)\n (map-++-commute mirror (reverse (map mirror ts)) (mirror t \u2237 []))\n refl\n \u27e9\n reverse (map mirror (reverse (map mirror ts)) ++\n (map mirror (mirror t \u2237 [])))\n \u2261\u27e8 subst (\u03bb x \u2192 (reverse (map mirror (reverse (map mirror ts)) ++\n (map mirror (mirror t \u2237 [])))) \u2261\n x)\n (reverse-++ (map mirror (reverse (map mirror ts)))\n (map mirror (mirror t \u2237 [])))\n refl\n \u27e9\n reverse (map mirror (mirror t \u2237 [])) ++\n reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 refl \u27e9\n mirror (mirror t) \u2237 reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 subst (\u03bb x \u2192 (mirror (mirror t) \u2237\n reverse (map mirror (reverse (map mirror ts)))) \u2261\n (x \u2237 reverse (map mirror (reverse (map mirror ts)))))\n (mirror\u00b2 t) -- IH.\n refl\n \u27e9\n t \u2237 reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 subst (\u03bb x \u2192 t \u2237 reverse (map mirror (reverse (map mirror ts))) \u2261\n t \u2237 x)\n (aux ts)\n refl\n \u27e9\n t \u2237 ts\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"15d68ccb1266c0c853b2906c9cef75d1daf94953","subject":"removing some redundant lemmas and moving things around in disjointness","message":"removing some redundant lemmas and moving things around in disjointness\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"disjointness.agda","new_file":"disjointness.agda","new_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import lemmas-disjointness\nopen import exchange\nopen import lemmas-freshness\nopen import weakening\n\nmodule disjointness where\n mutual\n expand-new-disjoint-synth : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4'} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-synth HNConst ESConst = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNAsc hn) (ESAsc x) = expand-new-disjoint-ana hn x\n expand-new-disjoint-synth HNVar (ESVar x\u2081) = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNLam1 hn) ()\n expand-new-disjoint-synth (HNLam2 hn) (ESLam x\u2081 exp) = expand-new-disjoint-synth hn exp\n expand-new-disjoint-synth (HNHole x) ESEHole = disjoint-singles x\n expand-new-disjoint-synth (HNNEHole x hn) (ESNEHole x\u2081 exp) = disjoint-parts (expand-new-disjoint-synth hn exp) (disjoint-singles x)\n expand-new-disjoint-synth (HNAp hn hn\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) =\n disjoint-parts (expand-new-disjoint-ana hn x\u2084)\n (expand-new-disjoint-ana hn\u2081 x\u2085)\n\n expand-new-disjoint-ana : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4' \u03c42} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-ana hn (EASubsume x x\u2081 x\u2082 x\u2083) = expand-new-disjoint-synth hn x\u2082\n expand-new-disjoint-ana (HNLam1 hn) (EALam x\u2081 x\u2082 ex) = expand-new-disjoint-ana hn ex\n expand-new-disjoint-ana (HNHole x) EAEHole = disjoint-singles x\n expand-new-disjoint-ana (HNNEHole x hn) (EANEHole x\u2081 x\u2082) = disjoint-parts (expand-new-disjoint-synth hn x\u2082) (disjoint-singles x)\n\n mutual\n expand-disjoint-new-synth : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c4'} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-synth ESConst disj = HNConst\n expand-disjoint-new-synth (ESVar x\u2081) disj = HNVar\n expand-disjoint-new-synth (ESLam x\u2081 ex) disj = HNLam2 (expand-disjoint-new-synth ex disj)\n expand-disjoint-new-synth (ESAp {\u03941 = \u03941} x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) disj\n with expand-disjoint-new-ana x\u2084 (disjoint-union1 disj) | expand-disjoint-new-ana x\u2085 (disjoint-union2 {\u03931 = \u03941} disj)\n ... | ih1 | ih2 = HNAp ih1 ih2\n expand-disjoint-new-synth {\u0393 = \u0393} ESEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-synth (ESNEHole {\u0394 = \u0394} x ex) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth ex (disjoint-union1 disj))\n expand-disjoint-new-synth (ESAsc x) disj = HNAsc (expand-disjoint-new-ana x disj)\n\n expand-disjoint-new-ana : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c42 \u03c4'} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-ana (EALam x\u2081 x\u2082 ex) disj = HNLam1 (expand-disjoint-new-ana ex disj)\n expand-disjoint-new-ana (EASubsume x x\u2081 x\u2082 x\u2083) disj = expand-disjoint-new-synth x\u2082 disj\n expand-disjoint-new-ana EAEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-ana (EANEHole {\u0394 = \u0394} x x\u2081) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth x\u2081 (disjoint-union1 disj))\n\n -- collect up the hole names of a term as the indices of a trivial contex\n data holes : (e : hexp) (H : \u22a4 ctx) \u2192 Set where\n HConst : holes c \u2205\n HAsc : \u2200{e \u03c4 H} \u2192 holes e H \u2192 holes (e \u00b7: \u03c4) H\n HVar : \u2200{x} \u2192 holes (X x) \u2205\n HLam1 : \u2200{x e H} \u2192 holes e H \u2192 holes (\u00b7\u03bb x e) H\n HLam2 : \u2200{x e \u03c4 H} \u2192 holes e H \u2192 holes (\u00b7\u03bb x [ \u03c4 ] e) H\n HEHole : \u2200{u} \u2192 holes (\u2987\u2988[ u ]) (\u25a0 (u , <>))\n HNEHole : \u2200{e u H} \u2192 holes e H \u2192 holes (\u2987 e \u2988[ u ]) (H ,, (u , <>))\n HAp : \u2200{e1 e2 H1 H2} \u2192 holes e1 H1 \u2192 holes e2 H2 \u2192 holes (e1 \u2218 e2) (H1 \u222a H2)\n\n -- the above judgement has mode (\u2200,\u2203). this doesn't prove uniqueness; any\n -- contex that extends the one computed here will be indistinguishable\n -- but we'll treat this one as canonical\n find-holes : (e : hexp) \u2192 \u03a3[ H \u2208 \u22a4 ctx ](holes e H)\n find-holes c = \u2205 , HConst\n find-holes (e \u00b7: x) with find-holes e\n ... | (h , d)= h , (HAsc d)\n find-holes (X x) = \u2205 , HVar\n find-holes (\u00b7\u03bb x e) with find-holes e\n ... | (h , d) = h , HLam1 d\n find-holes (\u00b7\u03bb x [ x\u2081 ] e) with find-holes e\n ... | (h , d) = h , HLam2 d\n find-holes \u2987\u2988[ x ] = (\u25a0 (x , <>)) , HEHole\n find-holes \u2987 e \u2988[ x ] with find-holes e\n ... | (h , d) = h ,, (x , <>) , HNEHole d\n find-holes (e1 \u2218 e2) with find-holes e1 | find-holes e2\n ... | (h1 , d1) | (h2 , d2) = (h1 \u222a h2 ) , (HAp d1 d2)\n\n -- two contexts that may contain different mappings have the same domain\n dom-eq : {A B : Set} \u2192 A ctx \u2192 B ctx \u2192 Set\n dom-eq {A} {B} C1 C2 = ((n : Nat) \u2192 \u03a3[ x \u2208 A ]( C1 n == Some x) \u2192 (\u03a3[ y \u2208 B ](C2 n == Some y)))\u00d7\n ((n : Nat) \u2192 \u03a3[ y \u2208 B ]( C2 n == Some y) \u2192 (\u03a3[ x \u2208 A ](C1 n == Some x)))\n\n -- the empty context has the same domain as itself\n dom-\u2205 : {A B : Set} \u2192 dom-eq (\u03bb _ \u2192 None {A}) (\u03bb _ \u2192 None {B})\n dom-\u2205 {A} {B} = (\u03bb n x \u2192 abort (somenotnone (! (\u03c02 x)))) , (\u03bb n x \u2192 abort (somenotnone (! (\u03c02 x))))\n\n -- todo: this seems like i would have proven it already? otw move to lemmas\n singleton-eq : {A : Set} {a : A} \u2192 \u2200{x n y} \u2192 (\u25a0 (x , a)) n == Some y \u2192 x == n\n singleton-eq {A} {a} {x} {n} {y} eq with natEQ x n\n singleton-eq eq | Inl x\u2081 = x\u2081\n singleton-eq eq | Inr x\u2081 = abort (somenotnone (! eq))\n\n -- todo: this seems like i would have proven it already? otw move to lemmas\n singleton-lookup-refl : {A : Set} {n : Nat} {\u03b2 : A} \u2192 (\u25a0 (n , \u03b2)) n == Some \u03b2\n singleton-lookup-refl {n = n} with natEQ n n\n singleton-lookup-refl | Inl refl = \u03bb {\u03b2} \u2192 refl\n singleton-lookup-refl | Inr x = abort (x refl)\n\n -- the singleton contexts formed with any contents but the same index has\n -- the same domain\n dom-single : {A B : Set} (x : Nat) (a : A) (b : B) \u2192 dom-eq (\u25a0 (x , a)) (\u25a0 (x , b))\n dom-single {A} {B} x \u03b1 \u03b2 = (\u03bb n x\u2081 \u2192 \u03b2 , (ap1 (\u03bb qq \u2192 (\u25a0 (qq , \u03b2)) n) (singleton-eq (\u03c02 x\u2081)) \u00b7 singleton-lookup-refl)) ,\n (\u03bb n x\u2081 \u2192 \u03b1 , (ap1 (\u03bb qq \u2192 (\u25a0 (qq , \u03b1)) n) (singleton-eq (\u03c02 x\u2081)) \u00b7 singleton-lookup-refl))\n\n -- todo: this seems like i would have proven it already? otw move to lemmas\n lem-dom-union-apt1 : {A : Set} {\u03941 \u03942 : A ctx} {x : Nat} {y : A} \u2192 x # \u03941 \u2192 ((\u03941 \u222a \u03942) x == Some y) \u2192 (\u03942 x == Some y)\n lem-dom-union-apt1 {A} {\u03941} {\u03942} {x} {y} apt xin with \u03941 x\n lem-dom-union-apt1 apt xin | Some x\u2081 = abort (somenotnone apt)\n lem-dom-union-apt1 apt xin | None = xin\n\n -- todo: this seems like i would have proven it already? otw move to lemmas\n lem-dom-union-apt2 : {A : Set} {\u03941 \u03942 : A ctx} {x : Nat} {y : A} \u2192 x # \u03942 \u2192 ((\u03941 \u222a \u03942) x == Some y) \u2192 (\u03941 x == Some y)\n lem-dom-union-apt2 {A} {\u03941} {\u03942} {x} {y} apt xin with \u03941 x\n lem-dom-union-apt2 apt xin | Some x\u2081 = xin\n lem-dom-union-apt2 apt xin | None = abort (somenotnone (! xin \u00b7 apt))\n\n -- if two disjoint sets each share a domain with two other sets, those\n -- are also disjoint.\n dom-eq-disj : {A B : Set} {\u03941 \u03942 : A ctx} {H1 H2 : B ctx} \u2192\n H1 ## H2 \u2192\n dom-eq \u03941 H1 \u2192\n dom-eq \u03942 H2 \u2192\n \u03941 ## \u03942\n dom-eq-disj {A} {B} {\u03941} {\u03942} {H1} {H2} (d1 , d2) (de1 , de2) (de3 , de4) = guts1 , guts2\n where\n guts1 : (n : Nat) \u2192 dom \u03941 n \u2192 n # \u03942\n guts1 n dom1 with ctxindirect H2 n\n guts1 n dom1 | Inl x = abort (somenotnone (! (\u03c02 x) \u00b7 d1 n (de1 n dom1)))\n guts1 n dom1 | Inr x with ctxindirect \u03942 n\n guts1 n dom1 | Inr x\u2081 | Inl x = abort (somenotnone (! (\u03c02 (de3 n x)) \u00b7 x\u2081))\n guts1 n dom1 | Inr x\u2081 | Inr x = x\n\n guts2 : (n : Nat) \u2192 dom \u03942 n \u2192 n # \u03941\n guts2 n dom2 with ctxindirect H1 n\n guts2 n dom2 | Inl x = abort (somenotnone (! (\u03c02 x) \u00b7 d2 n (de3 n dom2)))\n guts2 n dom2 | Inr x with ctxindirect \u03941 n\n guts2 n dom2 | Inr x\u2081 | Inl x = abort (somenotnone (! (\u03c02 (de1 n x)) \u00b7 x\u2081))\n guts2 n dom2 | Inr x\u2081 | Inr x = x\n\n -- if two sets share a domain with disjoint sets, then their union shares\n -- a domain with the union\n dom-union : {A B : Set} {\u03941 \u03942 : A ctx} {H1 H2 : B ctx} \u2192\n H1 ## H2 \u2192\n dom-eq \u03941 H1 \u2192\n dom-eq \u03942 H2 \u2192\n dom-eq (\u03941 \u222a \u03942) (H1 \u222a H2)\n dom-union {A} {B} {\u03941} {\u03942} {H1} {H2} disj (p1 , p2) (p3 , p4) = guts1 , guts2\n where\n guts1 : (n : Nat) \u2192\n \u03a3[ x \u2208 A ] ((\u03941 \u222a \u03942) n == Some x) \u2192\n \u03a3[ y \u2208 B ] ((H1 \u222a H2) n == Some y)\n guts1 n (x , eq) with ctxindirect \u03941 n\n guts1 n (x\u2081 , eq) | Inl x with p1 n x\n ... | q1 , q2 = q1 , x\u2208\u222al H1 H2 n q1 q2\n guts1 n (x\u2081 , eq) | Inr x with p3 n (_ , lem-dom-union-apt1 {\u03941 = \u03941} {\u03942 = \u03942} x eq)\n ... | q1 , q2 = q1 , x\u2208\u222ar H1 H2 n q1 q2 (##-comm disj)\n\n guts2 : (n : Nat) \u2192\n \u03a3[ y \u2208 B ] ((H1 \u222a H2) n == Some y) \u2192\n \u03a3[ x \u2208 A ] ((\u03941 \u222a \u03942) n == Some x)\n guts2 n (x , eq) with ctxindirect H1 n\n guts2 n (x\u2081 , eq) | Inl x with p2 n x\n ... | q1 , q2 = q1 , x\u2208\u222al \u03941 \u03942 n q1 q2\n guts2 n (x\u2081 , eq) | Inr x with p4 n (_ , lem-dom-union-apt2 {\u03941 = H2} {\u03942 = H1} x (tr (\u03bb qq \u2192 qq n == Some x\u2081) (\u222acomm H1 H2 disj) eq))\n ... | q1 , q2 = q1 , x\u2208\u222ar \u03941 \u03942 n q1 q2 (##-comm (dom-eq-disj disj (p1 , p2) (p3 , p4)))\n\n -- if a hole name is new then it's apart from the collection of hole\n -- names\n lem-apart-new : \u2200{e H u} \u2192 holes e H \u2192 hole-name-new e u \u2192 u # H\n lem-apart-new HConst HNConst = refl\n lem-apart-new (HAsc h) (HNAsc hn) = lem-apart-new h hn\n lem-apart-new HVar HNVar = refl\n lem-apart-new (HLam1 h) (HNLam1 hn) = lem-apart-new h hn\n lem-apart-new (HLam2 h) (HNLam2 hn) = lem-apart-new h hn\n lem-apart-new HEHole (HNHole x) = apart-singleton (flip x)\n lem-apart-new (HNEHole {u = u'} {H = H} h) (HNNEHole {u = u} x hn) = apart-parts H (\u25a0 (u' , <>)) u (lem-apart-new h hn) (apart-singleton (flip x))\n lem-apart-new (HAp {H1 = H1} {H2 = H2} h h\u2081) (HNAp hn hn\u2081) = apart-parts H1 H2 _ (lem-apart-new h hn) (lem-apart-new h\u2081 hn\u2081)\n\n -- todo: this seems like i would have proven it already? otw move to lemmas\n lem-dom-apt : {A : Set} {G : A ctx} {x y : Nat} \u2192 x # G \u2192 dom G y \u2192 x \u2260 y\n lem-dom-apt {x = x} {y = y} apt dom with natEQ x y\n lem-dom-apt apt dom | Inl refl = abort (somenotnone (! (\u03c02 dom) \u00b7 apt))\n lem-dom-apt apt dom | Inr x\u2081 = x\u2081\n\n -- if the holes of two expressions are disjoint, so are their collections\n -- of hole names\n holes-disjoint-disjoint : \u2200{ e1 e2 H1 H2} \u2192\n holes e1 H1 \u2192\n holes e2 H2 \u2192\n holes-disjoint e1 e2 \u2192\n H1 ## H2\n holes-disjoint-disjoint HConst he2 HDConst = empty-disj _\n holes-disjoint-disjoint (HAsc he1) he2 (HDAsc hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint HVar he2 HDVar = empty-disj _\n holes-disjoint-disjoint (HLam1 he1) he2 (HDLam1 hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint (HLam2 he1) he2 (HDLam2 hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint HEHole he2 (HDHole x) = lem-apart-sing-disj (lem-apart-new he2 x)\n holes-disjoint-disjoint (HNEHole he1) he2 (HDNEHole x hd) = disjoint-parts (holes-disjoint-disjoint he1 he2 hd) (lem-apart-sing-disj (lem-apart-new he2 x))\n holes-disjoint-disjoint (HAp he1 he2) he3 (HDAp hd hd\u2081) = disjoint-parts (holes-disjoint-disjoint he1 he3 hd) (holes-disjoint-disjoint he2 he3 hd\u2081)\n\n -- the holes of an expression have the same domain as \u0394; that is, we\n -- don't add any extra junk as we expand\n mutual\n holes-delta-ana : \u2200{\u0393 H e \u03c4 d \u03c4' \u0394} \u2192\n holes e H \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n dom-eq \u0394 H\n holes-delta-ana (HLam1 h) (EALam x\u2081 x\u2082 exp) = holes-delta-ana h exp\n holes-delta-ana h (EASubsume x x\u2081 x\u2082 x\u2083) = holes-delta-synth h x\u2082\n holes-delta-ana (HEHole {u = u}) EAEHole = dom-single u _ _\n holes-delta-ana (HNEHole {u = u} h) (EANEHole x x\u2081) with (holes-delta-synth h x\u2081)\n ... | ih = dom-union {!!} ih (dom-single u _ _ )\n\n holes-delta-synth : \u2200{\u0393 H e \u03c4 d \u0394} \u2192\n holes e H \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n dom-eq \u0394 H\n holes-delta-synth HConst ESConst = dom-\u2205\n holes-delta-synth (HAsc h) (ESAsc x) = holes-delta-ana h x\n holes-delta-synth HVar (ESVar x\u2081) = dom-\u2205\n holes-delta-synth (HLam2 h) (ESLam x\u2081 exp) = holes-delta-synth h exp\n holes-delta-synth (HEHole {u = u}) ESEHole = dom-single u _ _\n holes-delta-synth (HNEHole {u = u} h) (ESNEHole x exp) = dom-union {!!} (holes-delta-synth h exp) (dom-single u _ _)\n holes-delta-synth (HAp h h\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) = dom-union (holes-disjoint-disjoint h h\u2081 x) (holes-delta-ana h x\u2084) (holes-delta-ana h\u2081 x\u2085)\n\n -- if you expand two hole-disjoint expressions analytically, the \u0394s\n -- produces are disjoint. note that this is likely true for synthetic\n -- expansions in much the same way, but we only prove \"half\" of the usual\n -- pair here. the proof technique is *not* structurally inductive on the\n -- expansion judgement, because of the missing weakness property, so this\n -- proof is somewhat unusual compared to the rest in this development.\n expand-ana-disjoint : \u2200{ e1 e2 \u03c41 \u03c42 e1' e2' \u03c41' \u03c42' \u0393 \u03941 \u03942 } \u2192\n holes-disjoint e1 e2 \u2192\n \u0393 \u22a2 e1 \u21d0 \u03c41 ~> e1' :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> e2' :: \u03c42' \u22a3 \u03942 \u2192\n \u03941 ## \u03942\n expand-ana-disjoint {e1} {e2} hd ana1 ana2\n with find-holes e1 | find-holes e2\n ... | (_ , he1) | (_ , he2) = dom-eq-disj (holes-disjoint-disjoint he1 he2 hd)\n (holes-delta-ana he1 ana1)\n (holes-delta-ana he2 ana2)\n\n\n -- these lemmas are all structurally recursive and quite\n -- mechanical. morally, they establish the properties about reduction\n -- that would be obvious \/ baked into Agda if holes-disjoint was defined\n -- as a function rather than a judgement (datatype), or if we had defined\n -- all the O(n^2) cases rather than relying on a little indirection to\n -- only have O(n) cases. that work has to go somewhwere, and we prefer\n -- that it goes here.\n ds-lem-asc : \u2200{e1 e2 \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (e1 \u00b7: \u03c4)\n ds-lem-asc HDConst = HDConst\n ds-lem-asc (HDAsc hd) = HDAsc (ds-lem-asc hd)\n ds-lem-asc HDVar = HDVar\n ds-lem-asc (HDLam1 hd) = HDLam1 (ds-lem-asc hd)\n ds-lem-asc (HDLam2 hd) = HDLam2 (ds-lem-asc hd)\n ds-lem-asc (HDHole x) = HDHole (HNAsc x)\n ds-lem-asc (HDNEHole x hd) = HDNEHole (HNAsc x) (ds-lem-asc hd)\n ds-lem-asc (HDAp hd hd\u2081) = HDAp (ds-lem-asc hd) (ds-lem-asc hd\u2081)\n\n ds-lem-lam1 : \u2200{e1 e2 x} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x e1)\n ds-lem-lam1 HDConst = HDConst\n ds-lem-lam1 (HDAsc hd) = HDAsc (ds-lem-lam1 hd)\n ds-lem-lam1 HDVar = HDVar\n ds-lem-lam1 (HDLam1 hd) = HDLam1 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDLam2 hd) = HDLam2 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDHole x\u2081) = HDHole (HNLam1 x\u2081)\n ds-lem-lam1 (HDNEHole x\u2081 hd) = HDNEHole (HNLam1 x\u2081) (ds-lem-lam1 hd)\n ds-lem-lam1 (HDAp hd hd\u2081) = HDAp (ds-lem-lam1 hd) (ds-lem-lam1 hd\u2081)\n\n ds-lem-lam2 : \u2200{e1 e2 x \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x [ \u03c4 ] e1)\n ds-lem-lam2 HDConst = HDConst\n ds-lem-lam2 (HDAsc hd) = HDAsc (ds-lem-lam2 hd)\n ds-lem-lam2 HDVar = HDVar\n ds-lem-lam2 (HDLam1 hd) = HDLam1 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDLam2 hd) = HDLam2 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDHole x\u2081) = HDHole (HNLam2 x\u2081)\n ds-lem-lam2 (HDNEHole x\u2081 hd) = HDNEHole (HNLam2 x\u2081) (ds-lem-lam2 hd)\n ds-lem-lam2 (HDAp hd hd\u2081) = HDAp (ds-lem-lam2 hd) (ds-lem-lam2 hd\u2081)\n\n ds-lem-nehole : \u2200{e e1 u} \u2192 holes-disjoint e e1 \u2192 hole-name-new e u \u2192 holes-disjoint e \u2987 e1 \u2988[ u ]\n ds-lem-nehole HDConst \u03bd = HDConst\n ds-lem-nehole (HDAsc hd) (HNAsc \u03bd) = HDAsc (ds-lem-nehole hd \u03bd)\n ds-lem-nehole HDVar \u03bd = HDVar\n ds-lem-nehole (HDLam1 hd) (HNLam1 \u03bd) = HDLam1 (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDLam2 hd) (HNLam2 \u03bd) = HDLam2 (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDHole x) (HNHole x\u2081) = HDHole (HNNEHole (flip x\u2081) x)\n ds-lem-nehole (HDNEHole x hd) (HNNEHole x\u2081 \u03bd) = HDNEHole (HNNEHole (flip x\u2081) x) (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDAp hd hd\u2081) (HNAp \u03bd \u03bd\u2081) = HDAp (ds-lem-nehole hd \u03bd) (ds-lem-nehole hd\u2081 \u03bd\u2081)\n\n ds-lem-ap : \u2200{e1 e2 e3} \u2192 holes-disjoint e3 e1 \u2192 holes-disjoint e3 e2 \u2192 holes-disjoint e3 (e1 \u2218 e2)\n ds-lem-ap HDConst hd2 = HDConst\n ds-lem-ap (HDAsc hd1) (HDAsc hd2) = HDAsc (ds-lem-ap hd1 hd2)\n ds-lem-ap HDVar hd2 = HDVar\n ds-lem-ap (HDLam1 hd1) (HDLam1 hd2) = HDLam1 (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDLam2 hd1) (HDLam2 hd2) = HDLam2 (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDHole x) (HDHole x\u2081) = HDHole (HNAp x x\u2081)\n ds-lem-ap (HDNEHole x hd1) (HDNEHole x\u2081 hd2) = HDNEHole (HNAp x x\u2081) (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDAp hd1 hd2) (HDAp hd3 hd4) = HDAp (ds-lem-ap hd1 hd3) (ds-lem-ap hd2 hd4)\n\n -- holes-disjoint is symmetric\n disjoint-sym : (e1 e2 : hexp) \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint e2 e1\n disjoint-sym .c c HDConst = HDConst\n disjoint-sym .c (e2 \u00b7: x) HDConst = HDAsc (disjoint-sym _ _ HDConst)\n disjoint-sym .c (X x) HDConst = HDVar\n disjoint-sym .c (\u00b7\u03bb x e2) HDConst = HDLam1 (disjoint-sym c e2 HDConst)\n disjoint-sym .c (\u00b7\u03bb x [ x\u2081 ] e2) HDConst = HDLam2 (disjoint-sym c e2 HDConst)\n disjoint-sym .c \u2987\u2988[ x ] HDConst = HDHole HNConst\n disjoint-sym .c \u2987 e2 \u2988[ x ] HDConst = HDNEHole HNConst (disjoint-sym c e2 HDConst)\n disjoint-sym .c (e2 \u2218 e3) HDConst = HDAp (disjoint-sym c e2 HDConst) (disjoint-sym c e3 HDConst)\n\n disjoint-sym _ c (HDAsc hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) | HDAsc ih = HDAsc (ds-lem-asc ih)\n disjoint-sym _ (X x) (HDAsc hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) | HDLam1 ih = HDLam1 (ds-lem-asc ih)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) | HDLam2 ih = HDLam2 (ds-lem-asc ih)\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) | HDHole x\u2081 = HDHole (HNAsc x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) | HDNEHole x\u2081 ih = HDNEHole (HNAsc x\u2081) (ds-lem-asc ih)\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) | HDAp ih ih\u2081 = HDAp (ds-lem-asc ih) (ds-lem-asc ih\u2081)\n\n disjoint-sym _ c HDVar = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) HDVar = HDAsc (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (X x\u2081) HDVar = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) HDVar = HDLam1 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) HDVar = HDLam2 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] HDVar = HDHole HNVar\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] HDVar = HDNEHole HNVar (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (e2 \u2218 e3) HDVar = HDAp (disjoint-sym _ e2 HDVar) (disjoint-sym _ e3 HDVar)\n\n disjoint-sym _ c (HDLam1 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) | HDAsc ih = HDAsc (ds-lem-lam1 ih)\n disjoint-sym _ (X x\u2081) (HDLam1 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) | HDLam1 ih = HDLam1 (ds-lem-lam1 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) | HDLam2 ih = HDLam2 (ds-lem-lam1 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) | HDHole x = HDHole (HNLam1 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) | HDNEHole x ih = HDNEHole (HNLam1 x) (ds-lem-lam1 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam1 ih) (ds-lem-lam1 ih\u2081)\n\n disjoint-sym _ c (HDLam2 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) | HDAsc ih = HDAsc (ds-lem-lam2 ih)\n disjoint-sym _ (X x\u2081) (HDLam2 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) | HDLam1 ih = HDLam1 (ds-lem-lam2 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) | HDLam2 ih = HDLam2 (ds-lem-lam2 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) | HDHole x = HDHole (HNLam2 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) | HDNEHole x ih = HDNEHole (HNLam2 x) (ds-lem-lam2 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam2 ih) (ds-lem-lam2 ih\u2081)\n\n disjoint-sym _ c (HDHole x) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDHole (HNAsc x\u2081)) = HDAsc (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (X x) (HDHole x\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDHole (HNLam1 x\u2081)) = HDLam1 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDHole (HNLam2 x\u2082)) = HDLam2 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2082))\n disjoint-sym _ \u2987\u2988[ x ] (HDHole (HNHole x\u2081)) = HDHole (HNHole (flip x\u2081))\n disjoint-sym _ \u2987 e2 \u2988[ u' ] (HDHole (HNNEHole x x\u2081)) = HDNEHole (HNHole (flip x)) (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (e2 \u2218 e3) (HDHole (HNAp x x\u2081)) = HDAp (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x))\n (disjoint-sym \u2987\u2988[ _ ] e3 (HDHole x\u2081))\n\n disjoint-sym _ c (HDNEHole x hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e \u00b7: x) (HDNEHole (HNAsc x\u2081) hd) | HDAsc ih = HDAsc (ds-lem-nehole ih x\u2081)\n disjoint-sym _ (X x) (HDNEHole x\u2081 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole (HNLam1 x\u2081) hd) | HDLam1 ih = HDLam1 (ds-lem-nehole ih x\u2081)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole x\u2082 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole (HNLam2 x\u2082) hd) | HDLam2 ih = HDLam2 (ds-lem-nehole ih x\u2082)\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole (HNHole x\u2082) hd) | HDHole x\u2081 = HDHole (HNNEHole (flip x\u2082) x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole (HNNEHole x\u2082 x\u2083) hd) | HDNEHole x\u2081 ih = HDNEHole (HNNEHole (flip x\u2082) x\u2081) (ds-lem-nehole ih x\u2083)\n disjoint-sym _ (e2 \u2218 e3) (HDNEHole x hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e1 \u2218 e3) (HDNEHole (HNAp x x\u2081) hd) | HDAp ih ih\u2081 = HDAp (ds-lem-nehole ih x) (ds-lem-nehole ih\u2081 x\u2081)\n\n disjoint-sym _ c (HDAp hd hd\u2081) = HDConst\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) | HDAsc ih | HDAsc ih1 = HDAsc (ds-lem-ap ih ih1)\n disjoint-sym _ (X x) (HDAp hd hd\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) | HDLam1 ih | HDLam1 ih1 = HDLam1 (ds-lem-ap ih ih1)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) | HDLam2 ih | HDLam2 ih1 = HDLam2 (ds-lem-ap ih ih1)\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) | HDHole x\u2081 | HDHole x\u2082 = HDHole (HNAp x\u2081 x\u2082)\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) | HDNEHole x\u2081 ih | HDNEHole x\u2082 ih1 = HDNEHole (HNAp x\u2081 x\u2082) (ds-lem-ap ih ih1)\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) | HDAp ih ih\u2081 | HDAp ih1 ih2 = HDAp (ds-lem-ap ih ih1) (ds-lem-ap ih\u2081 ih2)\n\n\n -- note that this is false, so holes-disjoint isn't transitive\n -- disjoint-new : \u2200{e1 e2 u} \u2192 holes-disjoint e1 e2 \u2192 hole-name-new e1 u \u2192 hole-name-new e2 u\n\n -- it's also not reflexive, because \u2987\u2988[ u ] isn't hole-disjoint with\n -- itself since refl : u == u; it's also not anti-reflexive, because the\n -- expression c *is* hole-disjoint with itself (albeit vacuously)\n","old_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import lemmas-disjointness\nopen import exchange\nopen import lemmas-freshness\nopen import weakening\n\nmodule disjointness where\n mutual\n expand-new-disjoint-synth : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4'} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-synth HNConst ESConst = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNAsc hn) (ESAsc x) = expand-new-disjoint-ana hn x\n expand-new-disjoint-synth HNVar (ESVar x\u2081) = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNLam1 hn) ()\n expand-new-disjoint-synth (HNLam2 hn) (ESLam x\u2081 exp) = expand-new-disjoint-synth hn exp\n expand-new-disjoint-synth (HNHole x) ESEHole = disjoint-singles x\n expand-new-disjoint-synth (HNNEHole x hn) (ESNEHole x\u2081 exp) = disjoint-parts (expand-new-disjoint-synth hn exp) (disjoint-singles x)\n expand-new-disjoint-synth (HNAp hn hn\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) =\n disjoint-parts (expand-new-disjoint-ana hn x\u2084)\n (expand-new-disjoint-ana hn\u2081 x\u2085)\n\n expand-new-disjoint-ana : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4' \u03c42} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-ana hn (EASubsume x x\u2081 x\u2082 x\u2083) = expand-new-disjoint-synth hn x\u2082\n expand-new-disjoint-ana (HNLam1 hn) (EALam x\u2081 x\u2082 ex) = expand-new-disjoint-ana hn ex\n expand-new-disjoint-ana (HNHole x) EAEHole = disjoint-singles x\n expand-new-disjoint-ana (HNNEHole x hn) (EANEHole x\u2081 x\u2082) = disjoint-parts (expand-new-disjoint-synth hn x\u2082) (disjoint-singles x)\n\n mutual\n expand-disjoint-new-synth : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c4'} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-synth ESConst disj = HNConst\n expand-disjoint-new-synth (ESVar x\u2081) disj = HNVar\n expand-disjoint-new-synth (ESLam x\u2081 ex) disj = HNLam2 (expand-disjoint-new-synth ex disj)\n expand-disjoint-new-synth (ESAp {\u03941 = \u03941} x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) disj\n with expand-disjoint-new-ana x\u2084 (disjoint-union1 disj) | expand-disjoint-new-ana x\u2085 (disjoint-union2 {\u03931 = \u03941} disj)\n ... | ih1 | ih2 = HNAp ih1 ih2\n expand-disjoint-new-synth {\u0393 = \u0393} ESEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-synth (ESNEHole {\u0394 = \u0394} x ex) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth ex (disjoint-union1 disj))\n expand-disjoint-new-synth (ESAsc x) disj = HNAsc (expand-disjoint-new-ana x disj)\n\n expand-disjoint-new-ana : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c42 \u03c4'} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-ana (EALam x\u2081 x\u2082 ex) disj = HNLam1 (expand-disjoint-new-ana ex disj)\n expand-disjoint-new-ana (EASubsume x x\u2081 x\u2082 x\u2083) disj = expand-disjoint-new-synth x\u2082 disj\n expand-disjoint-new-ana EAEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-ana (EANEHole {\u0394 = \u0394} x x\u2081) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth x\u2081 (disjoint-union1 disj))\n\n -- collect up the hole names of a term as the indices of a trivial contex\n data holes : (e : hexp) (H : \u22a4 ctx) \u2192 Set where\n HConst : holes c \u2205\n HAsc : \u2200{e \u03c4 H} \u2192 holes e H \u2192 holes (e \u00b7: \u03c4) H\n HVar : \u2200{x} \u2192 holes (X x) \u2205\n HLam1 : \u2200{x e H} \u2192 holes e H \u2192 holes (\u00b7\u03bb x e) H\n HLam2 : \u2200{x e \u03c4 H} \u2192 holes e H \u2192 holes (\u00b7\u03bb x [ \u03c4 ] e) H\n HEHole : \u2200{u} \u2192 holes (\u2987\u2988[ u ]) (\u25a0 (u , <>))\n HNEHole : \u2200{e u H} \u2192 holes e H \u2192 holes (\u2987 e \u2988[ u ]) (H ,, (u , <>))\n HAp : \u2200{e1 e2 H1 H2} \u2192 holes e1 H1 \u2192 holes e2 H2 \u2192 holes (e1 \u2218 e2) (H1 \u222a H2)\n\n -- the above judgement has mode (\u2200,\u2203). this doesn't prove uniqueness; any\n -- contex that extends the one computed here will be indistinguishable\n -- but we'll treat this one as canonical\n find-holes : (e : hexp) \u2192 \u03a3[ H \u2208 \u22a4 ctx ](holes e H)\n find-holes c = \u2205 , HConst\n find-holes (e \u00b7: x) with find-holes e\n ... | (h , d)= h , (HAsc d)\n find-holes (X x) = \u2205 , HVar\n find-holes (\u00b7\u03bb x e) with find-holes e\n ... | (h , d) = h , HLam1 d\n find-holes (\u00b7\u03bb x [ x\u2081 ] e) with find-holes e\n ... | (h , d) = h , HLam2 d\n find-holes \u2987\u2988[ x ] = (\u25a0 (x , <>)) , HEHole\n find-holes \u2987 e \u2988[ x ] with find-holes e\n ... | (h , d) = h ,, (x , <>) , HNEHole d\n find-holes (e1 \u2218 e2) with find-holes e1 | find-holes e2\n ... | (h1 , d1) | (h2 , d2) = (h1 \u222a h2 ) , (HAp d1 d2)\n\n -- two contexts that may contain different mappings have the same domain\n dom-eq : {A B : Set} \u2192 A ctx \u2192 B ctx \u2192 Set\n dom-eq {A} {B} C1 C2 = ((n : Nat) \u2192 \u03a3[ x \u2208 A ]( C1 n == Some x) \u2192 (\u03a3[ y \u2208 B ](C2 n == Some y)))\u00d7\n ((n : Nat) \u2192 \u03a3[ y \u2208 B ]( C2 n == Some y) \u2192 (\u03a3[ x \u2208 A ](C1 n == Some x)))\n\n -- the empty context has the same domain as itself\n dom-\u2205 : {A B : Set} \u2192 dom-eq (\u03bb _ \u2192 None {A}) (\u03bb _ \u2192 None {B})\n dom-\u2205 {A} {B} = (\u03bb n x \u2192 abort (somenotnone (! (\u03c02 x)))) , (\u03bb n x \u2192 abort (somenotnone (! (\u03c02 x))))\n\n -- todo: this seems like i would have proven it already? otw move to lemmas\n singleton-eq : {A : Set} {a : A} \u2192 \u2200{x n y} \u2192 (\u25a0 (x , a)) n == Some y \u2192 x == n\n singleton-eq {A} {a} {x} {n} {y} eq with natEQ x n\n singleton-eq eq | Inl x\u2081 = x\u2081\n singleton-eq eq | Inr x\u2081 = abort (somenotnone (! eq))\n\n -- todo: this seems like i would have proven it already? otw move to lemmas\n singleton-lookup-refl : {A : Set} {n : Nat} {\u03b2 : A} \u2192 (\u25a0 (n , \u03b2)) n == Some \u03b2\n singleton-lookup-refl {n = n} with natEQ n n\n singleton-lookup-refl | Inl refl = \u03bb {\u03b2} \u2192 refl\n singleton-lookup-refl | Inr x = abort (x refl)\n\n -- the singleton contexts formed with any contents but the same index has\n -- the same domain\n dom-single : {A B : Set} (x : Nat) (a : A) (b : B) \u2192 dom-eq (\u25a0 (x , a)) (\u25a0 (x , b))\n dom-single {A} {B} x \u03b1 \u03b2 = (\u03bb n x\u2081 \u2192 \u03b2 , (ap1 (\u03bb qq \u2192 (\u25a0 (qq , \u03b2)) n) (singleton-eq (\u03c02 x\u2081)) \u00b7 singleton-lookup-refl)) ,\n (\u03bb n x\u2081 \u2192 \u03b1 , (ap1 (\u03bb qq \u2192 (\u25a0 (qq , \u03b1)) n) (singleton-eq (\u03c02 x\u2081)) \u00b7 singleton-lookup-refl))\n\n lem-dom-union-apt1 : {A : Set} {\u03941 \u03942 : A ctx} {x : Nat} {y : A} \u2192 x # \u03941 \u2192 ((\u03941 \u222a \u03942) x == Some y) \u2192 (\u03942 x == Some y)\n lem-dom-union-apt1 {A} {\u03941} {\u03942} {x} {y} apt xin with \u03941 x\n lem-dom-union-apt1 apt xin | Some x\u2081 = abort (somenotnone apt)\n lem-dom-union-apt1 apt xin | None = xin\n\n lem-dom-union-apt2 : {A : Set} {\u03941 \u03942 : A ctx} {x : Nat} {y : A} \u2192 x # \u03942 \u2192 ((\u03941 \u222a \u03942) x == Some y) \u2192 (\u03941 x == Some y)\n lem-dom-union-apt2 {A} {\u03941} {\u03942} {x} {y} apt xin with \u03941 x\n lem-dom-union-apt2 apt xin | Some x\u2081 = xin\n lem-dom-union-apt2 apt xin | None = abort (somenotnone (! xin \u00b7 apt))\n\n -- if two disjoint sets each share a domain with two other sets, those\n -- are also disjoint.\n dom-eq-disj : {A B : Set} {\u03941 \u03942 : A ctx} {H1 H2 : B ctx} \u2192\n H1 ## H2 \u2192\n dom-eq \u03941 H1 \u2192\n dom-eq \u03942 H2 \u2192\n \u03941 ## \u03942\n dom-eq-disj {A} {B} {\u03941} {\u03942} {H1} {H2} (d1 , d2) (de1 , de2) (de3 , de4) = guts1 , guts2\n where\n guts1 : (n : Nat) \u2192 dom \u03941 n \u2192 n # \u03942\n guts1 n dom1 with ctxindirect H2 n\n guts1 n dom1 | Inl x = abort (somenotnone (! (\u03c02 x) \u00b7 d1 n (de1 n dom1)))\n guts1 n dom1 | Inr x with ctxindirect \u03942 n\n guts1 n dom1 | Inr x\u2081 | Inl x = abort (somenotnone (! (\u03c02 (de3 n x)) \u00b7 x\u2081))\n guts1 n dom1 | Inr x\u2081 | Inr x = x\n\n guts2 : (n : Nat) \u2192 dom \u03942 n \u2192 n # \u03941\n guts2 n dom2 with ctxindirect H1 n\n guts2 n dom2 | Inl x = abort (somenotnone (! (\u03c02 x) \u00b7 d2 n (de3 n dom2)))\n guts2 n dom2 | Inr x with ctxindirect \u03941 n\n guts2 n dom2 | Inr x\u2081 | Inl x = abort (somenotnone (! (\u03c02 (de1 n x)) \u00b7 x\u2081))\n guts2 n dom2 | Inr x\u2081 | Inr x = x\n\n dom-union : {A B : Set} {\u03941 \u03942 : A ctx} {H1 H2 : B ctx} \u2192\n H1 ## H2 \u2192\n dom-eq \u03941 H1 \u2192\n dom-eq \u03942 H2 \u2192\n dom-eq (\u03941 \u222a \u03942) (H1 \u222a H2)\n dom-union {A} {B} {\u03941} {\u03942} {H1} {H2} disj (p1 , p2) (p3 , p4) = guts1 , guts2\n where\n guts1 : (n : Nat) \u2192\n \u03a3[ x \u2208 A ] ((\u03941 \u222a \u03942) n == Some x) \u2192\n \u03a3[ y \u2208 B ] ((H1 \u222a H2) n == Some y)\n guts1 n (x , eq) with ctxindirect \u03941 n\n guts1 n (x\u2081 , eq) | Inl x with p1 n x\n ... | q1 , q2 = q1 , x\u2208\u222al H1 H2 n q1 q2\n guts1 n (x\u2081 , eq) | Inr x with p3 n (_ , lem-dom-union-apt1 {\u03941 = \u03941} {\u03942 = \u03942} x eq)\n ... | q1 , q2 = q1 , x\u2208\u222ar H1 H2 n q1 q2 (##-comm disj)\n\n guts2 : (n : Nat) \u2192\n \u03a3[ y \u2208 B ] ((H1 \u222a H2) n == Some y) \u2192\n \u03a3[ x \u2208 A ] ((\u03941 \u222a \u03942) n == Some x)\n guts2 n (x , eq) with ctxindirect H1 n\n guts2 n (x\u2081 , eq) | Inl x with p2 n x\n ... | q1 , q2 = q1 , x\u2208\u222al \u03941 \u03942 n q1 q2\n guts2 n (x\u2081 , eq) | Inr x with p4 n (_ , lem-dom-union-apt2 {\u03941 = H2} {\u03942 = H1} x (tr (\u03bb qq \u2192 qq n == Some x\u2081) (\u222acomm H1 H2 disj) eq))\n ... | q1 , q2 = q1 , x\u2208\u222ar \u03941 \u03942 n q1 q2 (##-comm (dom-eq-disj disj (p1 , p2) (p3 , p4)))\n\n\n\n -- the holes of an expression have the same domain as \u0394; that is, we\n -- don't add any extra junk as we expand\n mutual\n holes-delta-ana : \u2200{\u0393 H e \u03c4 d \u03c4' \u0394} \u2192\n holes e H \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n dom-eq \u0394 H\n holes-delta-ana (HLam1 h) (EALam x\u2081 x\u2082 exp) = holes-delta-ana h exp\n holes-delta-ana h (EASubsume x x\u2081 x\u2082 x\u2083) = holes-delta-synth h x\u2082\n holes-delta-ana (HEHole {u = u}) EAEHole = dom-single u _ _\n holes-delta-ana (HNEHole {u = u} h) (EANEHole x x\u2081) = dom-union {!!} (holes-delta-synth h x\u2081) (dom-single u _ _ )\n\n holes-delta-synth : \u2200{\u0393 H e \u03c4 d \u0394} \u2192\n holes e H \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n dom-eq \u0394 H\n holes-delta-synth HConst ESConst = dom-\u2205\n holes-delta-synth (HAsc h) (ESAsc x) = holes-delta-ana h x\n holes-delta-synth HVar (ESVar x\u2081) = dom-\u2205\n holes-delta-synth (HLam2 h) (ESLam x\u2081 exp) = holes-delta-synth h exp\n holes-delta-synth (HEHole {u = u}) ESEHole = dom-single u _ _\n holes-delta-synth (HNEHole {u = u} h) (ESNEHole x exp) = dom-union {!!} (holes-delta-synth h exp) (dom-single u _ _)\n holes-delta-synth (HAp h h\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) = dom-union {!!} (holes-delta-ana h x\u2084) (holes-delta-ana h\u2081 x\u2085)\n\n -- if a hole name is new then it's apart from the holes\n lem-apart-new : \u2200{e H u} \u2192 holes e H \u2192 hole-name-new e u \u2192 u # H\n lem-apart-new HConst HNConst = refl\n lem-apart-new (HAsc h) (HNAsc hn) = lem-apart-new h hn\n lem-apart-new HVar HNVar = refl\n lem-apart-new (HLam1 h) (HNLam1 hn) = lem-apart-new h hn\n lem-apart-new (HLam2 h) (HNLam2 hn) = lem-apart-new h hn\n lem-apart-new HEHole (HNHole x) = apart-singleton (flip x)\n lem-apart-new (HNEHole {u = u'} {H = H} h) (HNNEHole {u = u} x hn) = apart-parts H (\u25a0 (u' , <>)) u (lem-apart-new h hn) (apart-singleton (flip x))\n lem-apart-new (HAp {H1 = H1} {H2 = H2} h h\u2081) (HNAp hn hn\u2081) = apart-parts H1 H2 _ (lem-apart-new h hn) (lem-apart-new h\u2081 hn\u2081)\n\n -- todo: lemmas file?\n lem-dom-apt : {A : Set} {G : A ctx} {x y : Nat} \u2192 x # G \u2192 dom G y \u2192 x \u2260 y\n lem-dom-apt {x = x} {y = y} apt dom with natEQ x y\n lem-dom-apt apt dom | Inl refl = abort (somenotnone (! (\u03c02 dom) \u00b7 apt))\n lem-dom-apt apt dom | Inr x\u2081 = x\u2081\n\n -- if the holes of two expressions are disjoint, so are their collections\n -- of hole names\n holes-disjoint-disjoint : \u2200{ e1 e2 H1 H2} \u2192\n holes e1 H1 \u2192\n holes e2 H2 \u2192\n holes-disjoint e1 e2 \u2192\n H1 ## H2\n holes-disjoint-disjoint HConst he2 HDConst = empty-disj _\n holes-disjoint-disjoint (HAsc he1) he2 (HDAsc hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint HVar he2 HDVar = empty-disj _\n holes-disjoint-disjoint (HLam1 he1) he2 (HDLam1 hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint (HLam2 he1) he2 (HDLam2 hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint HEHole he2 (HDHole x) = lem-apart-sing-disj (lem-apart-new he2 x)\n holes-disjoint-disjoint (HNEHole he1) he2 (HDNEHole x hd) = disjoint-parts (holes-disjoint-disjoint he1 he2 hd) (lem-apart-sing-disj (lem-apart-new he2 x))\n holes-disjoint-disjoint (HAp he1 he2) he3 (HDAp hd hd\u2081) = disjoint-parts (holes-disjoint-disjoint he1 he3 hd) (holes-disjoint-disjoint he2 he3 hd\u2081)\n\n -- if two contexsts are disjoint and each share a domain with another\n -- context, those other two contexts are also disjoint\n domeq-disj : {A B : Set} {H1 H2 : A ctx} {\u03941 \u03942 : B ctx} \u2192\n H1 ## H2 \u2192\n dom-eq \u03941 H1 \u2192\n dom-eq \u03942 H2 \u2192\n \u03941 ## \u03942\n domeq-disj (\u03c01 , \u03c02) (\u03c03 , \u03c04) (\u03c05 , \u03c06) =\n (\u03bb n x \u2192 {!(\u03c03 n x)!}) ,\n (\u03bb n x \u2192 {!!})\n\n -- if you expand two hole-disjoint expressions analytically, the \u0394s\n -- produces are disjoint\n expand-ana-disjoint : \u2200{ e1 e2 \u03c41 \u03c42 e1' e2' \u03c41' \u03c42' \u0393 \u03941 \u03942 } \u2192\n holes-disjoint e1 e2 \u2192\n \u0393 \u22a2 e1 \u21d0 \u03c41 ~> e1' :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> e2' :: \u03c42' \u22a3 \u03942 \u2192\n \u03941 ## \u03942\n expand-ana-disjoint {e1} {e2} hd ana1 ana2\n with find-holes e1 | find-holes e2\n ... | (_ , he1) | (_ , he2) = domeq-disj (holes-disjoint-disjoint he1 he2 hd)\n (holes-delta-ana he1 ana1)\n (holes-delta-ana he2 ana2)\n\n -- these lemmas are all structurally recursive and quite\n -- mechanical. morally, they establish the properties about reduction\n -- that would be obvious \/ baked into Agda if holes-disjoint was defined\n -- as a function rather than a judgement (datatype), or if we had defined\n -- all the O(n^2) cases rather than relying on a little indirection to\n -- only have O(n) cases. that work has to go somewhwere, and we prefer\n -- that it goes here.\n ds-lem-asc : \u2200{e1 e2 \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (e1 \u00b7: \u03c4)\n ds-lem-asc HDConst = HDConst\n ds-lem-asc (HDAsc hd) = HDAsc (ds-lem-asc hd)\n ds-lem-asc HDVar = HDVar\n ds-lem-asc (HDLam1 hd) = HDLam1 (ds-lem-asc hd)\n ds-lem-asc (HDLam2 hd) = HDLam2 (ds-lem-asc hd)\n ds-lem-asc (HDHole x) = HDHole (HNAsc x)\n ds-lem-asc (HDNEHole x hd) = HDNEHole (HNAsc x) (ds-lem-asc hd)\n ds-lem-asc (HDAp hd hd\u2081) = HDAp (ds-lem-asc hd) (ds-lem-asc hd\u2081)\n\n ds-lem-lam1 : \u2200{e1 e2 x} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x e1)\n ds-lem-lam1 HDConst = HDConst\n ds-lem-lam1 (HDAsc hd) = HDAsc (ds-lem-lam1 hd)\n ds-lem-lam1 HDVar = HDVar\n ds-lem-lam1 (HDLam1 hd) = HDLam1 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDLam2 hd) = HDLam2 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDHole x\u2081) = HDHole (HNLam1 x\u2081)\n ds-lem-lam1 (HDNEHole x\u2081 hd) = HDNEHole (HNLam1 x\u2081) (ds-lem-lam1 hd)\n ds-lem-lam1 (HDAp hd hd\u2081) = HDAp (ds-lem-lam1 hd) (ds-lem-lam1 hd\u2081)\n\n ds-lem-lam2 : \u2200{e1 e2 x \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x [ \u03c4 ] e1)\n ds-lem-lam2 HDConst = HDConst\n ds-lem-lam2 (HDAsc hd) = HDAsc (ds-lem-lam2 hd)\n ds-lem-lam2 HDVar = HDVar\n ds-lem-lam2 (HDLam1 hd) = HDLam1 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDLam2 hd) = HDLam2 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDHole x\u2081) = HDHole (HNLam2 x\u2081)\n ds-lem-lam2 (HDNEHole x\u2081 hd) = HDNEHole (HNLam2 x\u2081) (ds-lem-lam2 hd)\n ds-lem-lam2 (HDAp hd hd\u2081) = HDAp (ds-lem-lam2 hd) (ds-lem-lam2 hd\u2081)\n\n ds-lem-nehole : \u2200{e e1 u} \u2192 holes-disjoint e e1 \u2192 hole-name-new e u \u2192 holes-disjoint e \u2987 e1 \u2988[ u ]\n ds-lem-nehole HDConst \u03bd = HDConst\n ds-lem-nehole (HDAsc hd) (HNAsc \u03bd) = HDAsc (ds-lem-nehole hd \u03bd)\n ds-lem-nehole HDVar \u03bd = HDVar\n ds-lem-nehole (HDLam1 hd) (HNLam1 \u03bd) = HDLam1 (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDLam2 hd) (HNLam2 \u03bd) = HDLam2 (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDHole x) (HNHole x\u2081) = HDHole (HNNEHole (flip x\u2081) x)\n ds-lem-nehole (HDNEHole x hd) (HNNEHole x\u2081 \u03bd) = HDNEHole (HNNEHole (flip x\u2081) x) (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDAp hd hd\u2081) (HNAp \u03bd \u03bd\u2081) = HDAp (ds-lem-nehole hd \u03bd) (ds-lem-nehole hd\u2081 \u03bd\u2081)\n\n ds-lem-ap : \u2200{e1 e2 e3} \u2192 holes-disjoint e3 e1 \u2192 holes-disjoint e3 e2 \u2192 holes-disjoint e3 (e1 \u2218 e2)\n ds-lem-ap HDConst hd2 = HDConst\n ds-lem-ap (HDAsc hd1) (HDAsc hd2) = HDAsc (ds-lem-ap hd1 hd2)\n ds-lem-ap HDVar hd2 = HDVar\n ds-lem-ap (HDLam1 hd1) (HDLam1 hd2) = HDLam1 (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDLam2 hd1) (HDLam2 hd2) = HDLam2 (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDHole x) (HDHole x\u2081) = HDHole (HNAp x x\u2081)\n ds-lem-ap (HDNEHole x hd1) (HDNEHole x\u2081 hd2) = HDNEHole (HNAp x x\u2081) (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDAp hd1 hd2) (HDAp hd3 hd4) = HDAp (ds-lem-ap hd1 hd3) (ds-lem-ap hd2 hd4)\n\n -- holes-disjoint is symmetric\n disjoint-sym : (e1 e2 : hexp) \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint e2 e1\n disjoint-sym .c c HDConst = HDConst\n disjoint-sym .c (e2 \u00b7: x) HDConst = HDAsc (disjoint-sym _ _ HDConst)\n disjoint-sym .c (X x) HDConst = HDVar\n disjoint-sym .c (\u00b7\u03bb x e2) HDConst = HDLam1 (disjoint-sym c e2 HDConst)\n disjoint-sym .c (\u00b7\u03bb x [ x\u2081 ] e2) HDConst = HDLam2 (disjoint-sym c e2 HDConst)\n disjoint-sym .c \u2987\u2988[ x ] HDConst = HDHole HNConst\n disjoint-sym .c \u2987 e2 \u2988[ x ] HDConst = HDNEHole HNConst (disjoint-sym c e2 HDConst)\n disjoint-sym .c (e2 \u2218 e3) HDConst = HDAp (disjoint-sym c e2 HDConst) (disjoint-sym c e3 HDConst)\n\n disjoint-sym _ c (HDAsc hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) | HDAsc ih = HDAsc (ds-lem-asc ih)\n disjoint-sym _ (X x) (HDAsc hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) | HDLam1 ih = HDLam1 (ds-lem-asc ih)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) | HDLam2 ih = HDLam2 (ds-lem-asc ih)\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) | HDHole x\u2081 = HDHole (HNAsc x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) | HDNEHole x\u2081 ih = HDNEHole (HNAsc x\u2081) (ds-lem-asc ih)\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) | HDAp ih ih\u2081 = HDAp (ds-lem-asc ih) (ds-lem-asc ih\u2081)\n\n disjoint-sym _ c HDVar = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) HDVar = HDAsc (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (X x\u2081) HDVar = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) HDVar = HDLam1 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) HDVar = HDLam2 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] HDVar = HDHole HNVar\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] HDVar = HDNEHole HNVar (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (e2 \u2218 e3) HDVar = HDAp (disjoint-sym _ e2 HDVar) (disjoint-sym _ e3 HDVar)\n\n disjoint-sym _ c (HDLam1 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) | HDAsc ih = HDAsc (ds-lem-lam1 ih)\n disjoint-sym _ (X x\u2081) (HDLam1 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) | HDLam1 ih = HDLam1 (ds-lem-lam1 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) | HDLam2 ih = HDLam2 (ds-lem-lam1 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) | HDHole x = HDHole (HNLam1 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) | HDNEHole x ih = HDNEHole (HNLam1 x) (ds-lem-lam1 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam1 ih) (ds-lem-lam1 ih\u2081)\n\n disjoint-sym _ c (HDLam2 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) | HDAsc ih = HDAsc (ds-lem-lam2 ih)\n disjoint-sym _ (X x\u2081) (HDLam2 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) | HDLam1 ih = HDLam1 (ds-lem-lam2 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) | HDLam2 ih = HDLam2 (ds-lem-lam2 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) | HDHole x = HDHole (HNLam2 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) | HDNEHole x ih = HDNEHole (HNLam2 x) (ds-lem-lam2 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam2 ih) (ds-lem-lam2 ih\u2081)\n\n disjoint-sym _ c (HDHole x) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDHole (HNAsc x\u2081)) = HDAsc (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (X x) (HDHole x\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDHole (HNLam1 x\u2081)) = HDLam1 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDHole (HNLam2 x\u2082)) = HDLam2 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2082))\n disjoint-sym _ \u2987\u2988[ x ] (HDHole (HNHole x\u2081)) = HDHole (HNHole (flip x\u2081))\n disjoint-sym _ \u2987 e2 \u2988[ u' ] (HDHole (HNNEHole x x\u2081)) = HDNEHole (HNHole (flip x)) (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (e2 \u2218 e3) (HDHole (HNAp x x\u2081)) = HDAp (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x))\n (disjoint-sym \u2987\u2988[ _ ] e3 (HDHole x\u2081))\n\n disjoint-sym _ c (HDNEHole x hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e \u00b7: x) (HDNEHole (HNAsc x\u2081) hd) | HDAsc ih = HDAsc (ds-lem-nehole ih x\u2081)\n disjoint-sym _ (X x) (HDNEHole x\u2081 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole (HNLam1 x\u2081) hd) | HDLam1 ih = HDLam1 (ds-lem-nehole ih x\u2081)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole x\u2082 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole (HNLam2 x\u2082) hd) | HDLam2 ih = HDLam2 (ds-lem-nehole ih x\u2082)\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole (HNHole x\u2082) hd) | HDHole x\u2081 = HDHole (HNNEHole (flip x\u2082) x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole (HNNEHole x\u2082 x\u2083) hd) | HDNEHole x\u2081 ih = HDNEHole (HNNEHole (flip x\u2082) x\u2081) (ds-lem-nehole ih x\u2083)\n disjoint-sym _ (e2 \u2218 e3) (HDNEHole x hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e1 \u2218 e3) (HDNEHole (HNAp x x\u2081) hd) | HDAp ih ih\u2081 = HDAp (ds-lem-nehole ih x) (ds-lem-nehole ih\u2081 x\u2081)\n\n disjoint-sym _ c (HDAp hd hd\u2081) = HDConst\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) | HDAsc ih | HDAsc ih1 = HDAsc (ds-lem-ap ih ih1)\n disjoint-sym _ (X x) (HDAp hd hd\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) | HDLam1 ih | HDLam1 ih1 = HDLam1 (ds-lem-ap ih ih1)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) | HDLam2 ih | HDLam2 ih1 = HDLam2 (ds-lem-ap ih ih1)\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) | HDHole x\u2081 | HDHole x\u2082 = HDHole (HNAp x\u2081 x\u2082)\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) | HDNEHole x\u2081 ih | HDNEHole x\u2082 ih1 = HDNEHole (HNAp x\u2081 x\u2082) (ds-lem-ap ih ih1)\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) | HDAp ih ih\u2081 | HDAp ih1 ih2 = HDAp (ds-lem-ap ih ih1) (ds-lem-ap ih\u2081 ih2)\n\n\n -- note that this is false, so holes-disjoint isn't transitive\n -- disjoint-new : \u2200{e1 e2 u} \u2192 holes-disjoint e1 e2 \u2192 hole-name-new e1 u \u2192 hole-name-new e2 u\n\n -- it's also not reflexive, because \u2987\u2988[ u ] isn't hole-disjoint with\n -- itself since refl : u == u; it's also not anti-reflexive, because the\n -- expression c *is* hole-disjoint with itself (albeit vacuously)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"1c2120b65288219351b535ddd885c9200ce2e138","subject":"Use shorter name for accent constructors.","message":"Use shorter name for accent constructors.\n","repos":"scott-fleischman\/greek-grammar,scott-fleischman\/greek-grammar","old_file":"agda\/Text\/Greek\/Script.agda","new_file":"agda\/Text\/Greek\/Script.agda","new_contents":"module Text.Greek.Script where\n\nopen import Data.Maybe\nopen import Data.Vec\nopen import Relation.Nullary using (\u00ac_)\nopen import Relation.Binary.PropositionalEquality using (_\u2262_)\n\ndata Case : Set where\n lower upper : Case\n\ndata Letter : Set where\n \u03b1\u2032 \u03b2\u2032 \u03b3\u2032 \u03b4\u2032 \u03b5\u2032 \u03b6\u2032 \u03b7\u2032 \u03b8\u2032 \u03b9\u2032 \u03ba\u2032 \u03bb\u2032 \u03bc\u2032 \u03bd\u2032 \u03be\u2032 \u03bf\u2032 \u03c0\u2032 \u03c1\u2032 \u03c3\u2032 \u03c4\u2032 \u03c5\u2032 \u03c6\u2032 \u03c7\u2032 \u03c8\u2032 \u03c9\u2032 : Letter\n\nvowels : Vec _ _\nvowels = \u03b1\u2032 \u2237 \u03b5\u2032 \u2237 \u03b7\u2032 \u2237 \u03b9\u2032 \u2237 \u03bf\u2032 \u2237 \u03c5\u2032 \u2237 \u03c9\u2032 \u2237 []\n\nalways-short-letters : Vec _ _\nalways-short-letters = \u03b5\u2032 \u2237 \u03bf\u2032 \u2237 []\n\ndiaeresis-letters : Vec _ _\ndiaeresis-letters = \u03b9\u2032 \u2237 \u03c5\u2032 \u2237 []\n\niota-subscript-letters : Vec _ _\niota-subscript-letters = \u03b1\u2032 \u2237 \u03b7\u2032 \u2237 \u03c9\u2032 \u2237 []\n\ndata _vowel : Letter \u2192 Set where\n is-vowel : \u2200 {v} \u2192 v \u2208 vowels \u2192 v vowel\n\ndata _always-short : Letter \u2192 Set where\n is-always-short : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 always-short-letters \u2192 v always-short\n\ndata _diaeresis : Letter \u2192 Set where\n add-diaeresis : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 diaeresis-letters \u2192 v diaeresis\n\ndata _\u27e6_\u27e7-final : Letter \u2192 Case \u2192 Set where\n make-final : \u03c3\u2032 \u27e6 lower \u27e7-final\n\ndata _\u27e6_\u27e7-smooth : Letter \u2192 Case \u2192 Set where\n add-smooth-lower-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u27e6 lower \u27e7-smooth\n add-smooth-\u03c1 : \u03c1\u2032 \u27e6 lower \u27e7-smooth\n add-smooth-upper-vowel-not-\u03a5 : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2262 \u03c5\u2032 \u2192 v \u27e6 upper \u27e7-smooth\n\ndata _rough : Letter \u2192 Set where\n add-rough-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v rough\n add-rough-\u03c1 : \u03c1\u2032 rough\n\ndata _iota-subscript : Letter \u2192 Set where\n add-iota-subscript : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 iota-subscript-letters \u2192 v iota-subscript\n\ndata _\u27e6_\u27e7-breathing : Letter \u2192 Case \u2192 Set where\n add-smooth : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-smooth \u2192 \u2113 \u27e6 c \u27e7-breathing\n add-rough : \u2200 {\u2113 c} \u2192 \u2113 rough \u2192 \u2113 \u27e6 c \u27e7-breathing\n\ndata _long-vowel : Letter \u2192 Set where\n make-long-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 \u00ac v always-short \u2192 v long-vowel\n\ndata _accent : Letter \u2192 Set where\n acute : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n grave : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n add-circumflex : \u2200 {\u2113} \u2192 \u2113 long-vowel \u2192 \u2113 accent\n\ndata Token : Letter \u2192 Case \u2192 Set where\n unmarked : \u2200 {\u2113 c} \u2192 Token \u2113 c\n with-accent : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 Token \u2113 c\n with-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2113 iota-subscript \u2192 Token \u2113 c\n with-diaeresis : \u2200 {\u2113 c} \u2192 \u2113 diaeresis \u2192 Token \u2113 c\n with-accent-diaeresis : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 diaeresis \u2192 Token \u2113 c\n with-accent-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 iota-subscript \u2192 Token \u2113 c\n with-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2113 iota-subscript \u2192 Token \u2113 c\n with-iota : \u2200 {\u2113 c} \u2192 \u2113 iota-subscript \u2192 Token \u2113 c\n final : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-final \u2192 Token \u2113 c\n\n-- Constructions\n\n-- \u0391 \u03b1\ninstance \u03b1-vowel : \u03b1\u2032 vowel\n\u03b1-vowel = is-vowel here\n\n\u00ac\u03b1-always-short : \u00ac \u03b1\u2032 always-short\n\u00ac\u03b1-always-short (is-always-short (there (there ())))\n\n\u03b1-long-vowel : \u03b1\u2032 long-vowel\n\u03b1-long-vowel = make-long-vowel \u00ac\u03b1-always-short\n\n\u03b1-circumflex : \u03b1\u2032 accent\n\u03b1-circumflex = add-circumflex \u03b1-long-vowel\n\n\u03b1-smooth : \u03b1\u2032 \u27e6 lower \u27e7-breathing\n\u03b1-smooth = add-smooth add-smooth-lower-vowel\n\n\u0391-smooth : \u03b1\u2032 \u27e6 upper \u27e7-breathing\n\u0391-smooth = add-smooth (add-smooth-upper-vowel-not-\u03a5 (\u03bb ()))\n\n\u03b1-rough : \u03b1\u2032 \u27e6 lower \u27e7-breathing\n\u03b1-rough = add-rough add-rough-vowel\n\n\u0391-rough : \u03b1\u2032 \u27e6 upper \u27e7-breathing\n\u0391-rough = add-rough add-rough-vowel\n\n\u03b1-iota-subscript : \u03b1\u2032 iota-subscript\n\u03b1-iota-subscript = add-iota-subscript here\n\n-- \u0395 \u03b5\ninstance \u03b5-vowel : \u03b5\u2032 vowel\n\u03b5-vowel = is-vowel (there here)\n\n\u03b5-always-short : \u03b5\u2032 always-short\n\u03b5-always-short = is-always-short here\n\n\u03b5-smooth : \u03b5\u2032 \u27e6 lower \u27e7-breathing\n\u03b5-smooth = add-smooth add-smooth-lower-vowel\n\n\u0395-smooth : \u03b5\u2032 \u27e6 upper \u27e7-breathing\n\u0395-smooth = add-smooth (add-smooth-upper-vowel-not-\u03a5 (\u03bb ()))\n\n\u03b5-rough : \u03b5\u2032 \u27e6 lower \u27e7-breathing\n\u03b5-rough = add-rough add-rough-vowel\n\n\u0395-rough : \u03b5\u2032 \u27e6 upper \u27e7-breathing\n\u0395-rough = add-rough add-rough-vowel\n\n-- \u0397 \u03b7\ninstance \u03b7-vowel : \u03b7\u2032 vowel\n\u03b7-vowel = is-vowel (there (there here))\n\n\u00ac\u03b7-always-short : \u00ac \u03b7\u2032 always-short\n\u00ac\u03b7-always-short (is-always-short (there (there ())))\n\n\u03b7-long-vowel : \u03b7\u2032 long-vowel\n\u03b7-long-vowel = make-long-vowel \u00ac\u03b7-always-short\n\n\u03b7-circumflex : \u03b7\u2032 accent\n\u03b7-circumflex = add-circumflex \u03b7-long-vowel\n\n\u03b7-smooth : \u03b7\u2032 \u27e6 lower \u27e7-breathing\n\u03b7-smooth = add-smooth add-smooth-lower-vowel\n\n\u0397-smooth : \u03b7\u2032 \u27e6 upper \u27e7-breathing\n\u0397-smooth = add-smooth (add-smooth-upper-vowel-not-\u03a5 (\u03bb ()))\n\n\u03b7-rough : \u03b7\u2032 \u27e6 lower \u27e7-breathing\n\u03b7-rough = add-rough add-rough-vowel\n\n\u0397-rough : \u03b7\u2032 \u27e6 upper \u27e7-breathing\n\u0397-rough = add-rough add-rough-vowel\n\n\u03b7-iota-subscript : \u03b7\u2032 iota-subscript\n\u03b7-iota-subscript = add-iota-subscript (there here)\n\n-- \u0399 \u03b9\n\ninstance \u03b9-vowel : \u03b9\u2032 vowel\n\u03b9-vowel = is-vowel (there (there (there here)))\n\n\u00ac\u03b9-always-short : \u00ac \u03b9\u2032 always-short\n\u00ac\u03b9-always-short (is-always-short (there (there ())))\n\n\u03b9-long-vowel : \u03b9\u2032 long-vowel\n\u03b9-long-vowel = make-long-vowel \u00ac\u03b9-always-short\n\n\u03b9-not-\u03c5 : \u03b9\u2032 \u2262 \u03c5\u2032\n\u03b9-not-\u03c5 ()\n\n\u03b9-circumflex : \u03b9\u2032 accent\n\u03b9-circumflex = add-circumflex \u03b9-long-vowel\n\n\u03b9-smooth : \u03b9\u2032 \u27e6 lower \u27e7-breathing\n\u03b9-smooth = add-smooth add-smooth-lower-vowel\n\n\u0399-smooth : \u03b9\u2032 \u27e6 upper \u27e7-breathing\n\u0399-smooth = add-smooth (add-smooth-upper-vowel-not-\u03a5 (\u03bb ()))\n\n\u03b9-rough : \u03b9\u2032 \u27e6 lower \u27e7-breathing\n\u03b9-rough = add-rough add-rough-vowel\n\n-- \u039f \u03bf\n\n-- Unicode\n-- U+0390 - U+03CE\n\u0390 : Token \u03b9\u2032 lower -- U+0390 GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS\n\u0390 = unmarked\n\u0391 : Token \u03b1\u2032 upper -- U+0391 GREEK CAPITAL LETTER ALPHA\n\u0391 = unmarked\n\u0392 : Token \u03b2\u2032 upper -- U+0392 GREEK CAPITAL LETTER BETA\n\u0392 = unmarked\n\u0393 : Token \u03b3\u2032 upper -- U+0393 GREEK CAPITAL LETTER GAMMA\n\u0393 = unmarked\n\u0394 : Token \u03b4\u2032 upper -- U+0394 GREEK CAPITAL LETTER DELTA\n\u0394 = unmarked\n\u0395 : Token \u03b5\u2032 upper -- U+0395 GREEK CAPITAL LETTER EPSILON\n\u0395 = unmarked\n\u0396 : Token \u03b6\u2032 upper -- U+0396 GREEK CAPITAL LETTER ZETA\n\u0396 = unmarked\n\u0397 : Token \u03b7\u2032 upper -- U+0397 GREEK CAPITAL LETTER ETA\n\u0397 = unmarked\n\u0398 : Token \u03b8\u2032 upper -- U+0398 GREEK CAPITAL LETTER THETA\n\u0398 = unmarked\n\u0399 : Token \u03b9\u2032 upper -- U+0399 GREEK CAPITAL LETTER IOTA\n\u0399 = unmarked\n\u039a : Token \u03ba\u2032 upper -- U+039A GREEK CAPITAL LETTER KAPPA\n\u039a = unmarked\n\u039b : Token \u03bb\u2032 upper -- U+039B GREEK CAPITAL LETTER LAMDA\n\u039b = unmarked\n\u039c : Token \u03bc\u2032 upper -- U+039C GREEK CAPITAL LETTER MU\n\u039c = unmarked\n\u039d : Token \u03bd\u2032 upper -- U+039D GREEK CAPITAL LETTER NU\n\u039d = unmarked\n\u039e : Token \u03be\u2032 upper -- U+039E GREEK CAPITAL LETTER XI\n\u039e = unmarked\n\u039f : Token \u03bf\u2032 upper -- U+039F GREEK CAPITAL LETTER OMICRON\n\u039f = unmarked\n\u03a0 : Token \u03c0\u2032 upper -- U+03A0 GREEK CAPITAL LETTER PI\n\u03a0 = unmarked\n\u03a1 : Token \u03c1\u2032 upper -- U+03A1 GREEK CAPITAL LETTER RHO\n\u03a1 = unmarked\n -- U+03A2 \n\u03a3 : Token \u03c3\u2032 upper -- U+03A3 GREEK CAPITAL LETTER SIGMA\n\u03a3 = unmarked\n\u03a4 : Token \u03c4\u2032 upper -- U+03A4 GREEK CAPITAL LETTER TAU\n\u03a4 = unmarked\n\u03a5 : Token \u03c5\u2032 upper -- U+03A5 GREEK CAPITAL LETTER UPSILON\n\u03a5 = unmarked\n\u03a6 : Token \u03c6\u2032 upper -- U+03A6 GREEK CAPITAL LETTER PHI\n\u03a6 = unmarked\n\u03a7 : Token \u03c7\u2032 upper -- U+03A7 GREEK CAPITAL LETTER CHI\n\u03a7 = unmarked\n\u03a8 : Token \u03c8\u2032 upper -- U+03A8 GREEK CAPITAL LETTER PSI\n\u03a8 = unmarked\n\u03a9 : Token \u03c9\u2032 upper -- U+03A9 GREEK CAPITAL LETTER OMEGA\n\u03a9 = unmarked\n\u03aa : Token \u03b9\u2032 upper -- U+03AA GREEK CAPITAL LETTER IOTA WITH DIALYTIKA\n\u03aa = unmarked\n\u03ab : Token \u03c5\u2032 upper -- U+03AB GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA\n\u03ab = unmarked\n\u03ac : Token \u03b1\u2032 lower -- U+03AC GREEK SMALL LETTER ALPHA WITH TONOS\n\u03ac = unmarked\n\u03ad : Token \u03b5\u2032 lower -- U+03AD GREEK SMALL LETTER EPSILON WITH TONOS\n\u03ad = unmarked\n\u03ae : Token \u03b7\u2032 lower -- U+03AE GREEK SMALL LETTER ETA WITH TONOS\n\u03ae = unmarked\n\u03af : Token \u03b9\u2032 lower -- U+03AF GREEK SMALL LETTER IOTA WITH TONOS\n\u03af = unmarked\n\u03b0 : Token \u03c5\u2032 lower -- U+03B0 GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS\n\u03b0 = unmarked\n\u03b1 : Token \u03b1\u2032 lower -- U+03B1 GREEK SMALL LETTER ALPHA\n\u03b1 = unmarked\n\u03b2 : Token \u03b2\u2032 lower -- U+03B2 GREEK SMALL LETTER BETA\n\u03b2 = unmarked\n\u03b3 : Token \u03b3\u2032 lower -- U+03B3 GREEK SMALL LETTER GAMMA\n\u03b3 = unmarked\n\u03b4 : Token \u03b4\u2032 lower -- U+03B4 GREEK SMALL LETTER DELTA\n\u03b4 = unmarked\n\u03b5 : Token \u03b5\u2032 lower -- U+03B5 GREEK SMALL LETTER EPSILON\n\u03b5 = unmarked\n\u03b6 : Token \u03b6\u2032 lower -- U+03B6 GREEK SMALL LETTER ZETA\n\u03b6 = unmarked\n\u03b7 : Token \u03b7\u2032 lower -- U+03B7 GREEK SMALL LETTER ETA\n\u03b7 = unmarked\n\u03b8 : Token \u03b8\u2032 lower -- U+03B8 GREEK SMALL LETTER THETA\n\u03b8 = unmarked\n\u03b9 : Token \u03b9\u2032 lower -- U+03B9 GREEK SMALL LETTER IOTA\n\u03b9 = unmarked\n\u03ba : Token \u03ba\u2032 lower -- U+03BA GREEK SMALL LETTER KAPPA\n\u03ba = unmarked\n\u2219\u03bb : Token \u03bb\u2032 lower -- U+03BB GREEK SMALL LETTER LAMDA\n\u2219\u03bb = unmarked\n\u03bc : Token \u03bc\u2032 lower -- U+03BC GREEK SMALL LETTER MU\n\u03bc = unmarked\n\u03bd : Token \u03bd\u2032 lower -- U+03BD GREEK SMALL LETTER NU\n\u03bd = unmarked\n\u03be : Token \u03be\u2032 lower -- U+03BE GREEK SMALL LETTER XI\n\u03be = unmarked\n\u03bf : Token \u03bf\u2032 lower -- U+03BF GREEK SMALL LETTER OMICRON\n\u03bf = unmarked\n\u03c0 : Token \u03c0\u2032 lower -- U+03C0 GREEK SMALL LETTER PI\n\u03c0 = unmarked\n\u03c1 : Token \u03c1\u2032 lower -- U+03C1 GREEK SMALL LETTER RHO\n\u03c1 = unmarked\n\u03c2 : Token \u03c3\u2032 lower -- U+03C2 GREEK SMALL LETTER FINAL SIGMA\n\u03c2 = unmarked\n\u03c3 : Token \u03c3\u2032 lower -- U+03C3 GREEK SMALL LETTER SIGMA\n\u03c3 = unmarked\n\u03c4 : Token \u03c4\u2032 lower -- U+03C4 GREEK SMALL LETTER TAU\n\u03c4 = unmarked\n\u03c5 : Token \u03c5\u2032 lower -- U+03C5 GREEK SMALL LETTER UPSILON\n\u03c5 = unmarked\n\u03c6 : Token \u03c6\u2032 lower -- U+03C6 GREEK SMALL LETTER PHI\n\u03c6 = unmarked\n\u03c7 : Token \u03c7\u2032 lower -- U+03C7 GREEK SMALL LETTER CHI\n\u03c7 = unmarked\n\u03c8 : Token \u03c8\u2032 lower -- U+03C8 GREEK SMALL LETTER PSI\n\u03c8 = unmarked\n\u03c9 : Token \u03c9\u2032 lower -- U+03C9 GREEK SMALL LETTER OMEGA\n\u03c9 = unmarked\n\u03ca : Token \u03b9\u2032 lower -- U+03CA GREEK SMALL LETTER IOTA WITH DIALYTIKA\n\u03ca = unmarked\n\u03cb : Token \u03c5\u2032 lower -- U+03CB GREEK SMALL LETTER UPSILON WITH DIALYTIKA\n\u03cb = unmarked\n\u03cc : Token \u03bf\u2032 lower -- U+03CC GREEK SMALL LETTER OMICRON WITH TONOS\n\u03cc = unmarked\n\u03cd : Token \u03c5\u2032 lower -- U+03CD GREEK SMALL LETTER UPSILON WITH TONOS\n\u03cd = unmarked\n\u03ce : Token \u03c9\u2032 lower -- U+03CE GREEK SMALL LETTER OMEGA WITH TONOS\n\u03ce = unmarked\n\n\n\u1f00 : Token \u03b1\u2032 lower\n\u1f00 = with-breathing \u03b1-smooth\n\u1f01 : Token \u03b1\u2032 lower\n\u1f01 = with-breathing \u03b1-rough\n\u1f02 : Token \u03b1\u2032 lower\n\u1f02 = with-accent-breathing grave \u03b1-smooth\n\u1f03 : Token \u03b1\u2032 lower\n\u1f03 = with-accent-breathing grave \u03b1-rough\n\u1f04 : Token \u03b1\u2032 lower\n\u1f04 = with-accent-breathing acute \u03b1-smooth\n\u1f05 : Token \u03b1\u2032 lower\n\u1f05 = with-accent-breathing acute \u03b1-rough\n\u1f06 : Token \u03b1\u2032 lower\n\u1f06 = with-accent-breathing \u03b1-circumflex \u03b1-smooth\n\u1f07 : Token \u03b1\u2032 lower\n\u1f07 = with-accent-breathing \u03b1-circumflex \u03b1-rough\n\u1f08 : Token \u03b1\u2032 upper\n\u1f08 = with-breathing \u0391-smooth\n\u1f09 : Token \u03b1\u2032 upper\n\u1f09 = with-breathing \u0391-rough\n\u1f0a : Token \u03b1\u2032 upper\n\u1f0a = with-accent-breathing grave \u0391-smooth\n\u1f0b : Token \u03b1\u2032 upper\n\u1f0b = with-accent-breathing grave \u0391-rough\n\u1f0c : Token \u03b1\u2032 upper\n\u1f0c = with-accent-breathing acute \u0391-smooth\n\u1f0d : Token \u03b1\u2032 upper\n\u1f0d = with-accent-breathing acute \u0391-rough\n\u1f0e : Token \u03b1\u2032 upper\n\u1f0e = with-accent-breathing \u03b1-circumflex \u0391-smooth\n\u1f0f : Token \u03b1\u2032 upper\n\u1f0f = with-accent-breathing \u03b1-circumflex \u0391-rough\n\n-- U+1F7x\n\u1f70 : Token \u03b1\u2032 lower\n\u1f70 = with-accent grave\n\u1f71 : Token \u03b1\u2032 lower\n\u1f71 = with-accent acute\n\n-- U+1F8x\n\u1f80 : Token \u03b1\u2032 lower\n\u1f80 = with-breathing-iota \u03b1-smooth \u03b1-iota-subscript\n\u1f81 : Token \u03b1\u2032 lower\n\u1f81 = with-breathing-iota \u03b1-rough \u03b1-iota-subscript\n\u1f82 : Token \u03b1\u2032 lower\n\u1f82 = with-accent-breathing-iota grave \u03b1-smooth \u03b1-iota-subscript\n\u1f83 : Token \u03b1\u2032 lower\n\u1f83 = with-accent-breathing-iota grave \u03b1-rough \u03b1-iota-subscript\n\u1f84 : Token \u03b1\u2032 lower\n\u1f84 = with-accent-breathing-iota acute \u03b1-smooth \u03b1-iota-subscript\n\u1f85 : Token \u03b1\u2032 lower\n\u1f85 = with-accent-breathing-iota acute \u03b1-rough \u03b1-iota-subscript\n\u1f86 : Token \u03b1\u2032 lower\n\u1f86 = with-accent-breathing-iota \u03b1-circumflex \u03b1-smooth \u03b1-iota-subscript\n\u1f87 : Token \u03b1\u2032 lower\n\u1f87 = with-accent-breathing-iota \u03b1-circumflex \u03b1-rough \u03b1-iota-subscript\n\u1f88 : Token \u03b1\u2032 upper\n\u1f88 = with-breathing-iota \u0391-smooth \u03b1-iota-subscript\n\u1f89 : Token \u03b1\u2032 upper\n\u1f89 = with-breathing-iota \u0391-rough \u03b1-iota-subscript\n\u1f8a : Token \u03b1\u2032 upper\n\u1f8a = with-accent-breathing-iota grave \u0391-smooth \u03b1-iota-subscript\n\u1f8b : Token \u03b1\u2032 upper\n\u1f8b = with-accent-breathing-iota grave \u0391-rough \u03b1-iota-subscript\n\u1f8c : Token \u03b1\u2032 upper\n\u1f8c = with-accent-breathing-iota acute \u0391-smooth \u03b1-iota-subscript\n\u1f8d : Token \u03b1\u2032 upper\n\u1f8d = with-accent-breathing-iota acute \u0391-rough \u03b1-iota-subscript\n\u1f8e : Token \u03b1\u2032 upper\n\u1f8e = with-accent-breathing-iota \u03b1-circumflex \u0391-smooth \u03b1-iota-subscript\n\u1f8f : Token \u03b1\u2032 upper\n\u1f8f = with-accent-breathing-iota \u03b1-circumflex \u0391-rough \u03b1-iota-subscript\n\n-- U+1FBx\n-- \u1fb0\n-- \u1fb1\n\u1fb2 : Token \u03b1\u2032 lower\n\u1fb2 = with-accent-iota grave \u03b1-iota-subscript\n\u1fb3 : Token \u03b1\u2032 lower\n\u1fb3 = with-iota \u03b1-iota-subscript\n\u1fb4 : Token \u03b1\u2032 lower\n\u1fb4 = with-accent-iota acute \u03b1-iota-subscript\n\u1fb6 : Token \u03b1\u2032 lower\n\u1fb6 = with-accent \u03b1-circumflex\n\u1fb7 : Token \u03b1\u2032 lower\n\u1fb7 = with-accent-iota \u03b1-circumflex \u03b1-iota-subscript\n-- \u1fb8\n-- \u1fb9\n\u1fba : Token \u03b1\u2032 upper\n\u1fba = with-accent grave\n\u1fbb : Token \u03b1\u2032 upper\n\u1fbb = with-accent acute\n\u1fbc : Token \u03b1\u2032 upper\n\u1fbc = with-iota \u03b1-iota-subscript\n\n-- Mapping\n\ndata Accent : Set where\n acute-mark grave-mark circumflex-mark : Accent\n\nletter-to-accent : \u2200 {v} \u2192 v accent \u2192 Accent\nletter-to-accent acute = acute-mark\nletter-to-accent grave = grave-mark\nletter-to-accent (add-circumflex x) = circumflex-mark\n\nget-accent : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Accent\nget-accent (with-accent a) = just (letter-to-accent a)\nget-accent (with-accent-breathing a _) = just (letter-to-accent a)\nget-accent (with-accent-breathing-iota a _ _) = just (letter-to-accent a)\nget-accent (with-accent-diaeresis a _) = just (letter-to-accent a)\nget-accent (with-accent-iota a _) = just (letter-to-accent a)\nget-accent _ = nothing\n\ndata Breathing : Set where\n smooth-mark rough-mark : Breathing\n\nletter-to-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Breathing\nletter-to-breathing (add-smooth x) = smooth-mark\nletter-to-breathing (add-rough x) = rough-mark\n\nget-breathing : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Breathing\nget-breathing (with-breathing x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing _ x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing-iota _ x _) = just (letter-to-breathing x)\nget-breathing (with-breathing-iota x _) = just (letter-to-breathing x)\nget-breathing _ = nothing\n\ndata IotaSubscript : Set where\n iota-subscript-mark : IotaSubscript\n\nget-iota-subscript : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe IotaSubscript\nget-iota-subscript (with-accent-breathing-iota _ _ _) = just iota-subscript-mark\nget-iota-subscript (with-accent-iota _ _) = just iota-subscript-mark\nget-iota-subscript (with-breathing-iota _ _) = just iota-subscript-mark\nget-iota-subscript _ = nothing\n\ndata Diaeresis : Set where\n diaeresis-mark : Diaeresis\n\nget-diaeresis : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Diaeresis\nget-diaeresis (with-diaeresis _) = just diaeresis-mark\nget-diaeresis (with-accent-diaeresis _ _) = just diaeresis-mark\nget-diaeresis _ = nothing\n\ndata FinalForm : Set where\n final-form : FinalForm\n\nget-final-form : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe FinalForm\nget-final-form (final _) = just final-form\nget-final-form _ = nothing\n","old_contents":"module Text.Greek.Script where\n\nopen import Data.Maybe\nopen import Data.Vec\nopen import Relation.Nullary using (\u00ac_)\nopen import Relation.Binary.PropositionalEquality using (_\u2262_)\n\ndata Case : Set where\n lower upper : Case\n\ndata Letter : Set where\n \u03b1\u2032 \u03b2\u2032 \u03b3\u2032 \u03b4\u2032 \u03b5\u2032 \u03b6\u2032 \u03b7\u2032 \u03b8\u2032 \u03b9\u2032 \u03ba\u2032 \u03bb\u2032 \u03bc\u2032 \u03bd\u2032 \u03be\u2032 \u03bf\u2032 \u03c0\u2032 \u03c1\u2032 \u03c3\u2032 \u03c4\u2032 \u03c5\u2032 \u03c6\u2032 \u03c7\u2032 \u03c8\u2032 \u03c9\u2032 : Letter\n\nvowels : Vec _ _\nvowels = \u03b1\u2032 \u2237 \u03b5\u2032 \u2237 \u03b7\u2032 \u2237 \u03b9\u2032 \u2237 \u03bf\u2032 \u2237 \u03c5\u2032 \u2237 \u03c9\u2032 \u2237 []\n\nalways-short-letters : Vec _ _\nalways-short-letters = \u03b5\u2032 \u2237 \u03bf\u2032 \u2237 []\n\ndiaeresis-letters : Vec _ _\ndiaeresis-letters = \u03b9\u2032 \u2237 \u03c5\u2032 \u2237 []\n\niota-subscript-letters : Vec _ _\niota-subscript-letters = \u03b1\u2032 \u2237 \u03b7\u2032 \u2237 \u03c9\u2032 \u2237 []\n\ndata _vowel : Letter \u2192 Set where\n is-vowel : \u2200 {v} \u2192 v \u2208 vowels \u2192 v vowel\n\ndata _always-short : Letter \u2192 Set where\n is-always-short : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 always-short-letters \u2192 v always-short\n\ndata _diaeresis : Letter \u2192 Set where\n add-diaeresis : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 diaeresis-letters \u2192 v diaeresis\n\ndata _\u27e6_\u27e7-final : Letter \u2192 Case \u2192 Set where\n make-final : \u03c3\u2032 \u27e6 lower \u27e7-final\n\ndata _\u27e6_\u27e7-smooth : Letter \u2192 Case \u2192 Set where\n add-smooth-lower-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u27e6 lower \u27e7-smooth\n add-smooth-\u03c1 : \u03c1\u2032 \u27e6 lower \u27e7-smooth\n add-smooth-upper-vowel-not-\u03a5 : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2262 \u03c5\u2032 \u2192 v \u27e6 upper \u27e7-smooth\n\ndata _rough : Letter \u2192 Set where\n add-rough-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v rough\n add-rough-\u03c1 : \u03c1\u2032 rough\n\ndata _iota-subscript : Letter \u2192 Set where\n add-iota-subscript : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 iota-subscript-letters \u2192 v iota-subscript\n\ndata _\u27e6_\u27e7-breathing : Letter \u2192 Case \u2192 Set where\n add-smooth : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-smooth \u2192 \u2113 \u27e6 c \u27e7-breathing\n add-rough : \u2200 {\u2113 c} \u2192 \u2113 rough \u2192 \u2113 \u27e6 c \u27e7-breathing\n\ndata _long-vowel : Letter \u2192 Set where\n make-long-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 \u00ac v always-short \u2192 v long-vowel\n\ndata _accent : Letter \u2192 Set where\n add-acute : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n add-grave : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n add-circumflex : \u2200 {\u2113} \u2192 \u2113 long-vowel \u2192 \u2113 accent\n\ndata Token : Letter \u2192 Case \u2192 Set where\n unmarked : \u2200 {\u2113 c} \u2192 Token \u2113 c\n with-accent : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 Token \u2113 c\n with-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2113 iota-subscript \u2192 Token \u2113 c\n with-diaeresis : \u2200 {\u2113 c} \u2192 \u2113 diaeresis \u2192 Token \u2113 c\n with-accent-diaeresis : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 diaeresis \u2192 Token \u2113 c\n with-accent-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 iota-subscript \u2192 Token \u2113 c\n with-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2113 iota-subscript \u2192 Token \u2113 c\n with-iota : \u2200 {\u2113 c} \u2192 \u2113 iota-subscript \u2192 Token \u2113 c\n final : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-final \u2192 Token \u2113 c\n\n-- Constructions\n\n-- \u0391 \u03b1\ninstance \u03b1-vowel : \u03b1\u2032 vowel\n\u03b1-vowel = is-vowel here\n\n\u00ac\u03b1-always-short : \u00ac \u03b1\u2032 always-short\n\u00ac\u03b1-always-short (is-always-short (there (there ())))\n\n\u03b1-long-vowel : \u03b1\u2032 long-vowel\n\u03b1-long-vowel = make-long-vowel \u00ac\u03b1-always-short\n\n\u03b1-acute : \u03b1\u2032 accent\n\u03b1-acute = add-acute\n\n\u03b1-grave : \u03b1\u2032 accent\n\u03b1-grave = add-grave\n\n\u03b1-circumflex : \u03b1\u2032 accent\n\u03b1-circumflex = add-circumflex \u03b1-long-vowel\n\n\u03b1-smooth : \u03b1\u2032 \u27e6 lower \u27e7-breathing\n\u03b1-smooth = add-smooth add-smooth-lower-vowel\n\n\u0391-smooth : \u03b1\u2032 \u27e6 upper \u27e7-breathing\n\u0391-smooth = add-smooth (add-smooth-upper-vowel-not-\u03a5 (\u03bb ()))\n\n\u03b1-rough : \u03b1\u2032 \u27e6 lower \u27e7-breathing\n\u03b1-rough = add-rough add-rough-vowel\n\n\u0391-rough : \u03b1\u2032 \u27e6 upper \u27e7-breathing\n\u0391-rough = add-rough add-rough-vowel\n\n\u03b1-iota-subscript : \u03b1\u2032 iota-subscript\n\u03b1-iota-subscript = add-iota-subscript here\n\n-- \u0395 \u03b5\ninstance \u03b5-vowel : \u03b5\u2032 vowel\n\u03b5-vowel = is-vowel (there here)\n\n\u03b5-always-short : \u03b5\u2032 always-short\n\u03b5-always-short = is-always-short here\n\n\u03b5-acute : \u03b5\u2032 accent\n\u03b5-acute = add-acute\n\n\u03b5-grave : \u03b5\u2032 accent\n\u03b5-grave = add-grave\n\n\u03b5-smooth : \u03b5\u2032 \u27e6 lower \u27e7-breathing\n\u03b5-smooth = add-smooth add-smooth-lower-vowel\n\n\u0395-smooth : \u03b5\u2032 \u27e6 upper \u27e7-breathing\n\u0395-smooth = add-smooth (add-smooth-upper-vowel-not-\u03a5 (\u03bb ()))\n\n\u03b5-rough : \u03b5\u2032 \u27e6 lower \u27e7-breathing\n\u03b5-rough = add-rough add-rough-vowel\n\n\u0395-rough : \u03b5\u2032 \u27e6 upper \u27e7-breathing\n\u0395-rough = add-rough add-rough-vowel\n\n-- \u0397 \u03b7\ninstance \u03b7-vowel : \u03b7\u2032 vowel\n\u03b7-vowel = is-vowel (there (there here))\n\n\u00ac\u03b7-always-short : \u00ac \u03b7\u2032 always-short\n\u00ac\u03b7-always-short (is-always-short (there (there ())))\n\n\u03b7-long-vowel : \u03b7\u2032 long-vowel\n\u03b7-long-vowel = make-long-vowel \u00ac\u03b7-always-short\n\n\u03b7-acute : \u03b7\u2032 accent\n\u03b7-acute = add-acute\n\n\u03b7-grave : \u03b7\u2032 accent\n\u03b7-grave = add-grave\n\n\u03b7-circumflex : \u03b7\u2032 accent\n\u03b7-circumflex = add-circumflex \u03b7-long-vowel\n\n\u03b7-smooth : \u03b7\u2032 \u27e6 lower \u27e7-breathing\n\u03b7-smooth = add-smooth add-smooth-lower-vowel\n\n\u0397-smooth : \u03b7\u2032 \u27e6 upper \u27e7-breathing\n\u0397-smooth = add-smooth (add-smooth-upper-vowel-not-\u03a5 (\u03bb ()))\n\n\u03b7-rough : \u03b7\u2032 \u27e6 lower \u27e7-breathing\n\u03b7-rough = add-rough add-rough-vowel\n\n\u0397-rough : \u03b7\u2032 \u27e6 upper \u27e7-breathing\n\u0397-rough = add-rough add-rough-vowel\n\n\u03b7-iota-subscript : \u03b7\u2032 iota-subscript\n\u03b7-iota-subscript = add-iota-subscript (there here)\n\n-- \u0399 \u03b9\n\ninstance \u03b9-vowel : \u03b9\u2032 vowel\n\u03b9-vowel = is-vowel (there (there (there here)))\n\n\u00ac\u03b9-always-short : \u00ac \u03b9\u2032 always-short\n\u00ac\u03b9-always-short (is-always-short (there (there ())))\n\n\u03b9-long-vowel : \u03b9\u2032 long-vowel\n\u03b9-long-vowel = make-long-vowel \u00ac\u03b9-always-short\n\n\u03b9-not-\u03c5 : \u03b9\u2032 \u2262 \u03c5\u2032\n\u03b9-not-\u03c5 ()\n\n\u03b9-acute : \u03b9\u2032 accent\n\u03b9-acute = add-acute\n\n\u03b9-grave : \u03b9\u2032 accent\n\u03b9-grave = add-grave\n\n\u03b9-circumflex : \u03b9\u2032 accent\n\u03b9-circumflex = add-circumflex \u03b9-long-vowel\n\n\u03b9-smooth : \u03b9\u2032 \u27e6 lower \u27e7-breathing\n\u03b9-smooth = add-smooth add-smooth-lower-vowel\n\n\u0399-smooth : \u03b9\u2032 \u27e6 upper \u27e7-breathing\n\u0399-smooth = add-smooth (add-smooth-upper-vowel-not-\u03a5 (\u03bb ()))\n\n\u03b9-rough : \u03b9\u2032 \u27e6 lower \u27e7-breathing\n\u03b9-rough = add-rough add-rough-vowel\n\n-- \u039f \u03bf\n\n-- Unicode\n-- U+0390 - U+03CE\n\u0390 : Token \u03b9\u2032 lower -- U+0390 GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS\n\u0390 = unmarked\n\u0391 : Token \u03b1\u2032 upper -- U+0391 GREEK CAPITAL LETTER ALPHA\n\u0391 = unmarked\n\u0392 : Token \u03b2\u2032 upper -- U+0392 GREEK CAPITAL LETTER BETA\n\u0392 = unmarked\n\u0393 : Token \u03b3\u2032 upper -- U+0393 GREEK CAPITAL LETTER GAMMA\n\u0393 = unmarked\n\u0394 : Token \u03b4\u2032 upper -- U+0394 GREEK CAPITAL LETTER DELTA\n\u0394 = unmarked\n\u0395 : Token \u03b5\u2032 upper -- U+0395 GREEK CAPITAL LETTER EPSILON\n\u0395 = unmarked\n\u0396 : Token \u03b6\u2032 upper -- U+0396 GREEK CAPITAL LETTER ZETA\n\u0396 = unmarked\n\u0397 : Token \u03b7\u2032 upper -- U+0397 GREEK CAPITAL LETTER ETA\n\u0397 = unmarked\n\u0398 : Token \u03b8\u2032 upper -- U+0398 GREEK CAPITAL LETTER THETA\n\u0398 = unmarked\n\u0399 : Token \u03b9\u2032 upper -- U+0399 GREEK CAPITAL LETTER IOTA\n\u0399 = unmarked\n\u039a : Token \u03ba\u2032 upper -- U+039A GREEK CAPITAL LETTER KAPPA\n\u039a = unmarked\n\u039b : Token \u03bb\u2032 upper -- U+039B GREEK CAPITAL LETTER LAMDA\n\u039b = unmarked\n\u039c : Token \u03bc\u2032 upper -- U+039C GREEK CAPITAL LETTER MU\n\u039c = unmarked\n\u039d : Token \u03bd\u2032 upper -- U+039D GREEK CAPITAL LETTER NU\n\u039d = unmarked\n\u039e : Token \u03be\u2032 upper -- U+039E GREEK CAPITAL LETTER XI\n\u039e = unmarked\n\u039f : Token \u03bf\u2032 upper -- U+039F GREEK CAPITAL LETTER OMICRON\n\u039f = unmarked\n\u03a0 : Token \u03c0\u2032 upper -- U+03A0 GREEK CAPITAL LETTER PI\n\u03a0 = unmarked\n\u03a1 : Token \u03c1\u2032 upper -- U+03A1 GREEK CAPITAL LETTER RHO\n\u03a1 = unmarked\n -- U+03A2 \n\u03a3 : Token \u03c3\u2032 upper -- U+03A3 GREEK CAPITAL LETTER SIGMA\n\u03a3 = unmarked\n\u03a4 : Token \u03c4\u2032 upper -- U+03A4 GREEK CAPITAL LETTER TAU\n\u03a4 = unmarked\n\u03a5 : Token \u03c5\u2032 upper -- U+03A5 GREEK CAPITAL LETTER UPSILON\n\u03a5 = unmarked\n\u03a6 : Token \u03c6\u2032 upper -- U+03A6 GREEK CAPITAL LETTER PHI\n\u03a6 = unmarked\n\u03a7 : Token \u03c7\u2032 upper -- U+03A7 GREEK CAPITAL LETTER CHI\n\u03a7 = unmarked\n\u03a8 : Token \u03c8\u2032 upper -- U+03A8 GREEK CAPITAL LETTER PSI\n\u03a8 = unmarked\n\u03a9 : Token \u03c9\u2032 upper -- U+03A9 GREEK CAPITAL LETTER OMEGA\n\u03a9 = unmarked\n\u03aa : Token \u03b9\u2032 upper -- U+03AA GREEK CAPITAL LETTER IOTA WITH DIALYTIKA\n\u03aa = unmarked\n\u03ab : Token \u03c5\u2032 upper -- U+03AB GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA\n\u03ab = unmarked\n\u03ac : Token \u03b1\u2032 lower -- U+03AC GREEK SMALL LETTER ALPHA WITH TONOS\n\u03ac = unmarked\n\u03ad : Token \u03b5\u2032 lower -- U+03AD GREEK SMALL LETTER EPSILON WITH TONOS\n\u03ad = unmarked\n\u03ae : Token \u03b7\u2032 lower -- U+03AE GREEK SMALL LETTER ETA WITH TONOS\n\u03ae = unmarked\n\u03af : Token \u03b9\u2032 lower -- U+03AF GREEK SMALL LETTER IOTA WITH TONOS\n\u03af = unmarked\n\u03b0 : Token \u03c5\u2032 lower -- U+03B0 GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS\n\u03b0 = unmarked\n\u03b1 : Token \u03b1\u2032 lower -- U+03B1 GREEK SMALL LETTER ALPHA\n\u03b1 = unmarked\n\u03b2 : Token \u03b2\u2032 lower -- U+03B2 GREEK SMALL LETTER BETA\n\u03b2 = unmarked\n\u03b3 : Token \u03b3\u2032 lower -- U+03B3 GREEK SMALL LETTER GAMMA\n\u03b3 = unmarked\n\u03b4 : Token \u03b4\u2032 lower -- U+03B4 GREEK SMALL LETTER DELTA\n\u03b4 = unmarked\n\u03b5 : Token \u03b5\u2032 lower -- U+03B5 GREEK SMALL LETTER EPSILON\n\u03b5 = unmarked\n\u03b6 : Token \u03b6\u2032 lower -- U+03B6 GREEK SMALL LETTER ZETA\n\u03b6 = unmarked\n\u03b7 : Token \u03b7\u2032 lower -- U+03B7 GREEK SMALL LETTER ETA\n\u03b7 = unmarked\n\u03b8 : Token \u03b8\u2032 lower -- U+03B8 GREEK SMALL LETTER THETA\n\u03b8 = unmarked\n\u03b9 : Token \u03b9\u2032 lower -- U+03B9 GREEK SMALL LETTER IOTA\n\u03b9 = unmarked\n\u03ba : Token \u03ba\u2032 lower -- U+03BA GREEK SMALL LETTER KAPPA\n\u03ba = unmarked\n\u2219\u03bb : Token \u03bb\u2032 lower -- U+03BB GREEK SMALL LETTER LAMDA\n\u2219\u03bb = unmarked\n\u03bc : Token \u03bc\u2032 lower -- U+03BC GREEK SMALL LETTER MU\n\u03bc = unmarked\n\u03bd : Token \u03bd\u2032 lower -- U+03BD GREEK SMALL LETTER NU\n\u03bd = unmarked\n\u03be : Token \u03be\u2032 lower -- U+03BE GREEK SMALL LETTER XI\n\u03be = unmarked\n\u03bf : Token \u03bf\u2032 lower -- U+03BF GREEK SMALL LETTER OMICRON\n\u03bf = unmarked\n\u03c0 : Token \u03c0\u2032 lower -- U+03C0 GREEK SMALL LETTER PI\n\u03c0 = unmarked\n\u03c1 : Token \u03c1\u2032 lower -- U+03C1 GREEK SMALL LETTER RHO\n\u03c1 = unmarked\n\u03c2 : Token \u03c3\u2032 lower -- U+03C2 GREEK SMALL LETTER FINAL SIGMA\n\u03c2 = unmarked\n\u03c3 : Token \u03c3\u2032 lower -- U+03C3 GREEK SMALL LETTER SIGMA\n\u03c3 = unmarked\n\u03c4 : Token \u03c4\u2032 lower -- U+03C4 GREEK SMALL LETTER TAU\n\u03c4 = unmarked\n\u03c5 : Token \u03c5\u2032 lower -- U+03C5 GREEK SMALL LETTER UPSILON\n\u03c5 = unmarked\n\u03c6 : Token \u03c6\u2032 lower -- U+03C6 GREEK SMALL LETTER PHI\n\u03c6 = unmarked\n\u03c7 : Token \u03c7\u2032 lower -- U+03C7 GREEK SMALL LETTER CHI\n\u03c7 = unmarked\n\u03c8 : Token \u03c8\u2032 lower -- U+03C8 GREEK SMALL LETTER PSI\n\u03c8 = unmarked\n\u03c9 : Token \u03c9\u2032 lower -- U+03C9 GREEK SMALL LETTER OMEGA\n\u03c9 = unmarked\n\u03ca : Token \u03b9\u2032 lower -- U+03CA GREEK SMALL LETTER IOTA WITH DIALYTIKA\n\u03ca = unmarked\n\u03cb : Token \u03c5\u2032 lower -- U+03CB GREEK SMALL LETTER UPSILON WITH DIALYTIKA\n\u03cb = unmarked\n\u03cc : Token \u03bf\u2032 lower -- U+03CC GREEK SMALL LETTER OMICRON WITH TONOS\n\u03cc = unmarked\n\u03cd : Token \u03c5\u2032 lower -- U+03CD GREEK SMALL LETTER UPSILON WITH TONOS\n\u03cd = unmarked\n\u03ce : Token \u03c9\u2032 lower -- U+03CE GREEK SMALL LETTER OMEGA WITH TONOS\n\u03ce = unmarked\n\n\n\u1f00 : Token \u03b1\u2032 lower\n\u1f00 = with-breathing \u03b1-smooth\n\u1f01 : Token \u03b1\u2032 lower\n\u1f01 = with-breathing \u03b1-rough\n\u1f02 : Token \u03b1\u2032 lower\n\u1f02 = with-accent-breathing \u03b1-grave \u03b1-smooth\n\u1f03 : Token \u03b1\u2032 lower\n\u1f03 = with-accent-breathing \u03b1-grave \u03b1-rough\n\u1f04 : Token \u03b1\u2032 lower\n\u1f04 = with-accent-breathing \u03b1-acute \u03b1-smooth\n\u1f05 : Token \u03b1\u2032 lower\n\u1f05 = with-accent-breathing \u03b1-acute \u03b1-rough\n\u1f06 : Token \u03b1\u2032 lower\n\u1f06 = with-accent-breathing \u03b1-circumflex \u03b1-smooth\n\u1f07 : Token \u03b1\u2032 lower\n\u1f07 = with-accent-breathing \u03b1-circumflex \u03b1-rough\n\u1f08 : Token \u03b1\u2032 upper\n\u1f08 = with-breathing \u0391-smooth\n\u1f09 : Token \u03b1\u2032 upper\n\u1f09 = with-breathing \u0391-rough\n\u1f0a : Token \u03b1\u2032 upper\n\u1f0a = with-accent-breathing \u03b1-grave \u0391-smooth\n\u1f0b : Token \u03b1\u2032 upper\n\u1f0b = with-accent-breathing \u03b1-grave \u0391-rough\n\u1f0c : Token \u03b1\u2032 upper\n\u1f0c = with-accent-breathing \u03b1-acute \u0391-smooth\n\u1f0d : Token \u03b1\u2032 upper\n\u1f0d = with-accent-breathing \u03b1-acute \u0391-rough\n\u1f0e : Token \u03b1\u2032 upper\n\u1f0e = with-accent-breathing \u03b1-circumflex \u0391-smooth\n\u1f0f : Token \u03b1\u2032 upper\n\u1f0f = with-accent-breathing \u03b1-circumflex \u0391-rough\n\n-- U+1F7x\n\u1f70 : Token \u03b1\u2032 lower\n\u1f70 = with-accent \u03b1-grave\n\u1f71 : Token \u03b1\u2032 lower\n\u1f71 = with-accent \u03b1-acute\n\n-- U+1F8x\n\u1f80 : Token \u03b1\u2032 lower\n\u1f80 = with-breathing-iota \u03b1-smooth \u03b1-iota-subscript\n\u1f81 : Token \u03b1\u2032 lower\n\u1f81 = with-breathing-iota \u03b1-rough \u03b1-iota-subscript\n\u1f82 : Token \u03b1\u2032 lower\n\u1f82 = with-accent-breathing-iota \u03b1-grave \u03b1-smooth \u03b1-iota-subscript\n\u1f83 : Token \u03b1\u2032 lower\n\u1f83 = with-accent-breathing-iota \u03b1-grave \u03b1-rough \u03b1-iota-subscript\n\u1f84 : Token \u03b1\u2032 lower\n\u1f84 = with-accent-breathing-iota \u03b1-acute \u03b1-smooth \u03b1-iota-subscript\n\u1f85 : Token \u03b1\u2032 lower\n\u1f85 = with-accent-breathing-iota \u03b1-acute \u03b1-rough \u03b1-iota-subscript\n\u1f86 : Token \u03b1\u2032 lower\n\u1f86 = with-accent-breathing-iota \u03b1-circumflex \u03b1-smooth \u03b1-iota-subscript\n\u1f87 : Token \u03b1\u2032 lower\n\u1f87 = with-accent-breathing-iota \u03b1-circumflex \u03b1-rough \u03b1-iota-subscript\n\u1f88 : Token \u03b1\u2032 upper\n\u1f88 = with-breathing-iota \u0391-smooth \u03b1-iota-subscript\n\u1f89 : Token \u03b1\u2032 upper\n\u1f89 = with-breathing-iota \u0391-rough \u03b1-iota-subscript\n\u1f8a : Token \u03b1\u2032 upper\n\u1f8a = with-accent-breathing-iota \u03b1-grave \u0391-smooth \u03b1-iota-subscript\n\u1f8b : Token \u03b1\u2032 upper\n\u1f8b = with-accent-breathing-iota \u03b1-grave \u0391-rough \u03b1-iota-subscript\n\u1f8c : Token \u03b1\u2032 upper\n\u1f8c = with-accent-breathing-iota \u03b1-acute \u0391-smooth \u03b1-iota-subscript\n\u1f8d : Token \u03b1\u2032 upper\n\u1f8d = with-accent-breathing-iota \u03b1-acute \u0391-rough \u03b1-iota-subscript\n\u1f8e : Token \u03b1\u2032 upper\n\u1f8e = with-accent-breathing-iota \u03b1-circumflex \u0391-smooth \u03b1-iota-subscript\n\u1f8f : Token \u03b1\u2032 upper\n\u1f8f = with-accent-breathing-iota \u03b1-circumflex \u0391-rough \u03b1-iota-subscript\n\n-- U+1FBx\n-- \u1fb0\n-- \u1fb1\n\u1fb2 : Token \u03b1\u2032 lower\n\u1fb2 = with-accent-iota \u03b1-grave \u03b1-iota-subscript\n\u1fb3 : Token \u03b1\u2032 lower\n\u1fb3 = with-iota \u03b1-iota-subscript\n\u1fb4 : Token \u03b1\u2032 lower\n\u1fb4 = with-accent-iota \u03b1-acute \u03b1-iota-subscript\n\u1fb6 : Token \u03b1\u2032 lower\n\u1fb6 = with-accent \u03b1-circumflex\n\u1fb7 : Token \u03b1\u2032 lower\n\u1fb7 = with-accent-iota \u03b1-circumflex \u03b1-iota-subscript\n-- \u1fb8\n-- \u1fb9\n\u1fba : Token \u03b1\u2032 upper\n\u1fba = with-accent \u03b1-grave\n\u1fbb : Token \u03b1\u2032 upper\n\u1fbb = with-accent \u03b1-acute\n\u1fbc : Token \u03b1\u2032 upper\n\u1fbc = with-iota \u03b1-iota-subscript\n\n-- Mapping\n\ndata Accent : Set where\n acute-mark grave-mark circumflex-mark : Accent\n\nletter-to-accent : \u2200 {v} \u2192 v accent \u2192 Accent\nletter-to-accent add-acute = acute-mark\nletter-to-accent add-grave = grave-mark\nletter-to-accent (add-circumflex x) = circumflex-mark\n\nget-accent : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Accent\nget-accent (with-accent a) = just (letter-to-accent a)\nget-accent (with-accent-breathing a _) = just (letter-to-accent a)\nget-accent (with-accent-breathing-iota a _ _) = just (letter-to-accent a)\nget-accent (with-accent-diaeresis a _) = just (letter-to-accent a)\nget-accent (with-accent-iota a _) = just (letter-to-accent a)\nget-accent _ = nothing\n\ndata Breathing : Set where\n smooth-mark rough-mark : Breathing\n\nletter-to-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Breathing\nletter-to-breathing (add-smooth x) = smooth-mark\nletter-to-breathing (add-rough x) = rough-mark\n\nget-breathing : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Breathing\nget-breathing (with-breathing x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing _ x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing-iota _ x _) = just (letter-to-breathing x)\nget-breathing (with-breathing-iota x _) = just (letter-to-breathing x)\nget-breathing _ = nothing\n\ndata IotaSubscript : Set where\n iota-subscript-mark : IotaSubscript\n\nget-iota-subscript : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe IotaSubscript\nget-iota-subscript (with-accent-breathing-iota _ _ _) = just iota-subscript-mark\nget-iota-subscript (with-accent-iota _ _) = just iota-subscript-mark\nget-iota-subscript (with-breathing-iota _ _) = just iota-subscript-mark\nget-iota-subscript _ = nothing\n\ndata Diaeresis : Set where\n diaeresis-mark : Diaeresis\n\nget-diaeresis : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Diaeresis\nget-diaeresis (with-diaeresis _) = just diaeresis-mark\nget-diaeresis (with-accent-diaeresis _ _) = just diaeresis-mark\nget-diaeresis _ = nothing\n\ndata FinalForm : Set where\n final-form : FinalForm\n\nget-final-form : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe FinalForm\nget-final-form (final _) = just final-form\nget-final-form _ = nothing\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"96cc380d8bec495647aa617138f89a24bf3a3266","subject":"Added a simpler way of constructing syntax for the linear solver.","message":"Added a simpler way of constructing syntax for the linear solver.\n","repos":"crypto-agda\/crypto-agda","old_file":"linear-solver.agda","new_file":"linear-solver.agda","new_contents":"module linear-solver where\n\nopen import Data.Nat as \u2115 using (\u2115)\nopen import Data.Fin as F using (Fin)\nimport Data.Fin.Props as FP\n\nmodule Syntax {a} (A : Set a)(_x_ : A \u2192 A \u2192 A)(T : A)\n (R' : A \u2192 A \u2192 Set)\n (id' : \u2200 {A} \u2192 R' A A)\n (_\u223b'_ : \u2200 {A B C} \u2192 R' A B \u2192 R' B C \u2192 R' A C)\n (' : \u2200 {A} \u2192 R' (A x T) A)\n (\u207b\u00b9' : \u2200 {A} \u2192 R' A (A x T))\n (' : \u2200 {A} \u2192 R' (T x A) A)\n (\u207b\u00b9' : \u2200 {A} \u2192 R' A (T x A))\n (\u27e8_\u00d7'_\u27e9 : \u2200 {A B C D} \u2192 R' A C \u2192 R' B D \u2192 R' (A x B) (C x D))\n (first' : \u2200 {A B C} \u2192 R' A B \u2192 R' (A x C) (B x C))\n (second' : \u2200 {A B C} \u2192 R' B C \u2192 R' (A x B) (A x C))\n (assoc' : \u2200 {A B C} \u2192 R' (A x (B x C)) ((A x B) x C))\n (assoc\u207b\u00b9' : \u2200 {A B C} \u2192 R' ((A x B) x C) (A x (B x C)))\n (swap' : \u2200 {A B} \u2192 R' (A x B) (B x A))\n nrVars (!_ : Fin nrVars \u2192 A) where\n\n Var = Fin nrVars\n\n open import Relation.Nullary using (yes ; no)\n open import Relation.Nullary.Decidable\n\n data Syn : Set where\n var : Var \u2192 Syn\n tt : Syn\n _,_ : Syn \u2192 Syn \u2192 Syn\n\n #_ : \u2200 m {m : \u2200 {A} \u2192 R (A , tt) A\n : \u2200 {A} \u2192 R (tt , A) A\n \u207b\u00b9 : \u2200 {A} \u2192 R A (tt , A)\n \u207b\u00b9 : \u2200 {A} \u2192 R A (A , tt)\n \u27e8_\u00d7''_\u27e9 : \u2200 {A B C D} \u2192 R A C \u2192 R B D \u2192 R (A , B) (C , D)\n assoc : \u2200 {A B C} \u2192 R (A , (B , C)) ((A , B) , C)\n assoc\u207b\u00b9 : \u2200 {A B C} \u2192 R ((A , B) , C) (A , (B , C))\n id : \u2200 {A} \u2192 R A A\n swap : \u2200 {A B} \u2192 R (A , B) (B , A)\n\n \u27e8_\u00d7_\u27e9 : \u2200 {A B C D} \u2192 R A C \u2192 R B D \u2192 R (A , B) (C , D)\n \u27e8 id \u00d7 id \u27e9 = id\n \u27e8 r\u2081 \u00d7 r\u2082 \u27e9 = \u27e8 r\u2081 \u00d7'' r\u2082 \u27e9\n\n _\u223b_ : \u2200 {A B C} \u2192 R A B \u2192 R B C \u2192 R A C\n id \u223b r\u2082 = r\u2082\n r\u2081 \u223b id = r\u2081\n \u207b\u00b9 \u223b = id\n \u207b\u00b9 \u223b = id\n \u223b \u207b\u00b9 = id\n \u223b \u207b\u00b9 = id\n swap \u223b = \n swap \u223b = \n \u207b\u00b9 \u223b swap = \u207b\u00b9\n \u207b\u00b9 \u223b swap = \u207b\u00b9\n assoc \u223b assoc\u207b\u00b9 = id\n assoc \u223b (assoc\u207b\u00b9 \u223b'' r) = r\n assoc\u207b\u00b9 \u223b assoc = id\n assoc\u207b\u00b9 \u223b (assoc \u223b'' r) = r\n swap \u223b swap = id\n swap \u223b (swap \u223b'' r) = r\n (r\u2081 \u223b'' r\u2082) \u223b r\u2083 = r\u2081 \u223b (r\u2082 \u223b r\u2083)\n \u27e8 r\u2081 \u00d7'' r\u2082 \u27e9 \u223b \u27e8 r\u2083 \u00d7'' r\u2084 \u27e9 = \u27e8 r\u2081 \u223b r\u2083 \u00d7 r\u2082 \u223b r\u2084 \u27e9\n \u27e8 r\u2081 \u00d7'' r\u2082 \u27e9 \u223b (\u27e8 r\u2083 \u00d7'' r\u2084 \u27e9 \u223b'' r\u2085) with \u27e8 r\u2081 \u223b r\u2083 \u00d7 r\u2082 \u223b r\u2084 \u27e9\n ... |\u00a0id = r\u2085\n ... |\u00a0r\u2086 = r\u2086 \u223b'' r\u2085\n r\u2081 \u223b r\u2082 = r\u2081 \u223b'' r\u2082 \n\n sym : \u2200 {S S'} \u2192 R S S' \u2192 R S' S\n sym (r \u223b'' r\u2081) = sym r\u2081 \u223b sym r\n sym = \u207b\u00b9\n sym = \u207b\u00b9\n sym \u207b\u00b9 = \n sym \u207b\u00b9 = \n sym \u27e8 r \u00d7'' r\u2081 \u27e9 = \u27e8 sym r \u00d7 sym r\u2081 \u27e9\n sym assoc = assoc\u207b\u00b9\n sym assoc\u207b\u00b9 = assoc\n sym id = id\n sym swap = swap\n\n proof\u2081 : \u2200 {S S'} \u2192 R S S' \u2192 R' (eval S) (eval S')\n proof\u2081 (r \u223b'' r\u2081) = proof\u2081 r \u223b' proof\u2081 r\u2081\n proof\u2081 = '\n proof\u2081 = '\n proof\u2081 \u207b\u00b9 = \u207b\u00b9'\n proof\u2081 \u207b\u00b9 = \u207b\u00b9'\n proof\u2081 \u27e8 id \u00d7'' r \u27e9 = second' (proof\u2081 r)\n proof\u2081 \u27e8 r \u00d7'' id \u27e9 = first' (proof\u2081 r)\n proof\u2081 \u27e8 r \u00d7'' r\u2081 \u27e9 = \u27e8 proof\u2081 r \u00d7' proof\u2081 r\u2081 \u27e9\n proof\u2081 assoc = assoc'\n proof\u2081 assoc\u207b\u00b9 = assoc\u207b\u00b9'\n proof\u2081 id = id'\n proof\u2081 swap = swap'\n\n proof\u2082 : \u2200 {S S'} \u2192 R S S' \u2192 R' (eval S') (eval S)\n proof\u2082 r = proof\u2081 (sym r)\n\n data NF : Syn \u2192 Set where\n tt : NF tt\n var : (x : Var) \u2192 NF (var x)\n var_::_ : \u2200 {S}(i : Var) \u2192 NF S \u2192 NF (var i , S)\n\n record NFP S : Set where\n constructor _\u22a2_\n field\n {S'} : Syn\n term : NF S'\n proof : R S' S\n \n \n merge : \u2200 {S S'} \u2192 NF S \u2192 NF S' \u2192 NFP (S , S')\n merge tt n2 = n2 \u22a2 \u207b\u00b9\n merge (var i) n2 = (var i :: n2) \u22a2 id\n merge (var i :: n1) n2 with merge n1 n2\n ... | t \u22a2 p = (var i :: t) \u22a2 (\u27e8 id \u00d7 p \u27e9 \u223b assoc)\n\n norm : (x : Syn) \u2192 NFP x\n norm (var x) = (var x) \u22a2 id\n norm tt = tt \u22a2 id\n norm (x , x\u2081) with norm x | norm x\u2081\n ... | t1 \u22a2 p1 | t2 \u22a2 p2 with merge t1 t2\n ... | t3 \u22a2 p3 = t3 \u22a2 (p3 \u223b \u27e8 p1 \u00d7 p2 \u27e9)\n\n insert : \u2200 {S} \u2192 (x : Var) \u2192 NF S \u2192 NFP (var x , S)\n insert y tt = (var y) \u22a2 \u207b\u00b9\n insert y (var i) with (F.to\u2115 y) \u2115.\u2264? (F.to\u2115 i)\n ... | yes _ = (var y :: var i) \u22a2 id\n ... | no _ = (var i :: var y) \u22a2 swap\n insert y (var i :: n1) with (F.to\u2115 y) \u2115.\u2264? (F.to\u2115 i)\n ... | yes _ = (var y :: (var i :: n1)) \u22a2 id\n ... | no _ with insert y n1\n ... | t \u22a2 p = (var i :: t) \u22a2 (\u27e8 id \u00d7 p \u27e9 \u223b (assoc \u223b (\u27e8 swap \u00d7 id \u27e9 \u223b assoc\u207b\u00b9)))\n\n sort : \u2200 {x : Syn} \u2192 NF x \u2192 NFP x\n sort tt = tt \u22a2 id\n sort (var i) = var i \u22a2 id\n sort (var i :: n1) with sort n1\n ... | t1 \u22a2 p1 with insert i t1\n ... | t2 \u22a2 p2 = t2 \u22a2 (p2 \u223b \u27e8 id \u00d7 p1 \u27e9)\n\n normal : (x : Syn) \u2192 NFP x\n normal x with norm x\n ... | t1 \u22a2 p1 with sort t1\n ... | t2 \u22a2 p2 = t2 \u22a2 (p2 \u223b p1)\n\n open import Relation.Binary.PropositionalEquality using (_\u2261_ ; refl)\n open import Relation.Nullary\n\n import Data.Unit\n import Data.Empty\n\n id\u2261 : \u2200 {S S'} \u2192 S \u2261 S' \u2192 R S S'\n id\u2261 refl = id\n\n _\u2262_ : \u2200 {A : Set} \u2192 A \u2192 A \u2192 Set\n x \u2262 y = x \u2261 y \u2192 Data.Empty.\u22a5\n\n \u2262-cong : \u2200 {A B}{x y : A}(f : A \u2192 B) \u2192 f x \u2262 f y \u2192 x \u2262 y\n \u2262-cong f fr refl = fr refl\n\n var-inj : \u2200 {i j : Fin nrVars} \u2192 i \u2262 j \u2192 Syn.var i \u2262 var j\n var-inj p refl = p refl\n\n ,-inj\u2081 : \u2200 {x y a b} \u2192 x \u2262 y \u2192 (x Syn., a) \u2262 (y , b)\n ,-inj\u2081 p refl = p refl\n\n ,-inj\u2082 : \u2200 {x y a b} \u2192 a \u2262 b \u2192 (x Syn., a) \u2262 (y , b)\n ,-inj\u2082 p refl = p refl\n\n _\u225f_ : (x y : Syn) \u2192 Dec (x \u2261 y)\n var x \u225f var x\u2081 with x FP.\u225f x\u2081\n var .x\u2081 \u225f var x\u2081 | yes refl = yes refl\n ... | no p = no (var-inj p)\n var x \u225f tt = no (\u03bb ())\n var x \u225f (y , y\u2081) = no (\u03bb ())\n tt \u225f var x = no (\u03bb ())\n tt \u225f tt = yes refl\n tt \u225f (y , y\u2081) = no (\u03bb ())\n (x , x\u2081) \u225f var x\u2082 = no (\u03bb ())\n (x , x\u2081) \u225f tt = no (\u03bb ())\n (x , x\u2081) \u225f (y , y\u2081) with x \u225f y | x\u2081 \u225f y\u2081\n (x , x\u2081) \u225f (.x , .x\u2081) | yes refl | yes refl = yes refl\n (x , x\u2081) \u225f (y , y\u2081) | yes p | no \u00acp = no (,-inj\u2082 \u00acp)\n (x , x\u2081) \u225f (y , y\u2081) | no \u00acp | q = no (,-inj\u2081 \u00acp)\n\n CHECK : Syn \u2192 Syn \u2192 Set\n CHECK s1 s2 with s1 \u225f s2\n ... | yes p = Data.Unit.\u22a4\n ... | no p = Data.Empty.\u22a5\n\n rewire : (S\u2081 S\u2082 : Syn) \u2192 CHECK (NFP.S' (normal S\u2081)) (NFP.S' (normal S\u2082)) \u2192 R' (eval S\u2081) (eval S\u2082)\n rewire s\u2081 s\u2082 eq with NFP.S' (normal s\u2081) \u225f NFP.S' (normal s\u2082)\n ... | yes p = proof\u2081\n ((sym (NFP.proof (normal s\u2081)) \u223b id\u2261 p) \u223b NFP.proof (normal s\u2082))\n rewire _ _ () | no _\n -- proof\u2082 (NFP.proof (normal s\u2081)) \u223b' (eq \u223b' proof\u2081 (NFP.proof (normal s\u2082)))\n\n infix 4 _\u21db_\n\n record Eq : Set where\n constructor _\u21db_\n field\n LHS RHS : Syn\n\n open import Data.Vec.N-ary using (N-ary ; _$\u207f_)\n open import Data.Vec using (allFin) renaming (map to vmap)\n\n rewire' : (f : N-ary nrVars Syn Eq) \u2192 let (S\u2081 \u21db S\u2082) = f $\u207f (vmap Syn.var (allFin nrVars))\n in CHECK (NFP.S' (normal S\u2081)) (NFP.S' (normal S\u2082)) \u2192 R' (eval S\u2081) (eval S\u2082)\n rewire' f eq = let S \u21db S' = f $\u207f vmap Syn.var (allFin nrVars)\n in rewire S S' eq\n \nmodule example where\n\n open import Data.Vec\n\n open import Data.Product\n open import Data.Unit\n\n open import Function\n\n -- need to etaexpand this because otherwise we get an error\n module STest n M = Syntax Set _\u00d7_ \u22a4 (\u03bb x x\u2081 \u2192 x \u2192 x\u2081) (\u03bb x \u2192 x) \n (\u03bb x x\u2081 x\u2082 \u2192 x\u2081 (x x\u2082)) (\u03bb x \u2192 proj\u2081 x) (\u03bb x \u2192 x , tt) \n (\u03bb x \u2192 proj\u2082 x) (\u03bb x \u2192 tt , x) (\u03bb x x\u2081 x\u2082 \u2192 (x (proj\u2081 x\u2082)) , (x\u2081 (proj\u2082 x\u2082))) \n (\u03bb x x\u2081 \u2192 (x (proj\u2081 x\u2081)) , (proj\u2082 x\u2081)) (\u03bb x x\u2081 \u2192 (proj\u2081 x\u2081) , (x (proj\u2082 x\u2081))) \n (\u03bb x \u2192 ((proj\u2081 x) , (proj\u2081 (proj\u2082 x))) , (proj\u2082 (proj\u2082 x))) \n (\u03bb x \u2192 (proj\u2081 (proj\u2081 x)) , ((proj\u2082 (proj\u2081 x)) , (proj\u2082 x))) \n (\u03bb x \u2192 (proj\u2082 x) , (proj\u2081 x)) n M\n\n test : (A B C : Set) \u2192 (A \u00d7 B) \u00d7 C \u2192 (B \u00d7 A) \u00d7 C\n test A B C = rewire LHS RHS _ where\n open STest 3 (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n\n LHS = (# 0 , # 1) , # 2\n RHS = (# 1 , # 0) , # 2\n\n test2 : (A B C : Set) \u2192 (A \u00d7 B) \u00d7 C \u2192 (B \u00d7 A) \u00d7 C\n test2 A B C = rewire' (\u03bb a b c \u2192 (a , b) , c \u21db (b , a) , c) _ where\n open STest 3 (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n\n\nmodule example\u2082 where\n\n open import Data.Vec\n\n data Ty : Set where\n _\u00d7_ : Ty \u2192 Ty \u2192 Ty\n \u22a4 : Ty\n\n infix 4 _\u27f6_ \n\n data _\u27f6_ : Ty \u2192 Ty \u2192 Set where\n id' : \u2200 {A} \u2192 A \u27f6 A\n _\u223b'_ : \u2200 {A B C} \u2192 A \u27f6 B \u2192 B \u27f6 C \u2192 A \u27f6 C\n ' : \u2200 {A} \u2192 (A \u00d7 \u22a4) \u27f6 A\n \u207b\u00b9' : \u2200 {A} \u2192 A \u27f6 (A \u00d7 \u22a4)\n ' : \u2200 {A} \u2192 (\u22a4 \u00d7 A) \u27f6 A\n \u207b\u00b9' : \u2200 {A} \u2192 A \u27f6 (\u22a4 \u00d7 A)\n \u27e8_\u00d7'_\u27e9 : \u2200 {A B C D} \u2192 A \u27f6 C \u2192 B \u27f6 D \u2192 (A \u00d7 B) \u27f6 (C \u00d7 D)\n first : \u2200 {A B C} \u2192 A \u27f6 B \u2192 A \u00d7 C \u27f6 B \u00d7 C\n second : \u2200 {A B C} \u2192 B \u27f6 C \u2192 A \u00d7 B \u27f6 A \u00d7 C \n assoc' : \u2200 {A B C} \u2192 (A \u00d7 (B \u00d7 C)) \u27f6 ((A \u00d7 B) \u00d7 C)\n assoc\u207b\u00b9' : \u2200 {A B C} \u2192 ((A \u00d7 B) \u00d7 C) \u27f6 (A \u00d7 (B \u00d7 C))\n swap' : \u2200 {A B} \u2192 (A \u00d7 B) \u27f6 (B \u00d7 A)\n \n\n module STest n M = Syntax Ty _\u00d7_ \u22a4 _\u27f6_ id' _\u223b'_ ' \u207b\u00b9' ' \u207b\u00b9' \u27e8_\u00d7'_\u27e9 first second assoc' assoc\u207b\u00b9' swap' n M\n\n test2 : (A B C : Ty) \u2192 (A \u00d7 B) \u00d7 C \u27f6 (B \u00d7 A) \u00d7 C\n test2 A B C = rewire ((# 0 , # 1) , # 2) ((# 1 , # 0) , # 2) _ where\n open STest 3 (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n","old_contents":"module linear-solver where\n\nopen import Data.Nat as \u2115 using (\u2115)\nopen import Data.Fin as F using (Fin)\nimport Data.Fin.Props as FP\n\nmodule Syntax {a} (A : Set a)(_x_ : A \u2192 A \u2192 A)(T : A)\n (R' : A \u2192 A \u2192 Set)\n (id' : \u2200 {A} \u2192 R' A A)\n (_\u223b'_ : \u2200 {A B C} \u2192 R' A B \u2192 R' B C \u2192 R' A C)\n (' : \u2200 {A} \u2192 R' (A x T) A)\n (\u207b\u00b9' : \u2200 {A} \u2192 R' A (A x T))\n (' : \u2200 {A} \u2192 R' (T x A) A)\n (\u207b\u00b9' : \u2200 {A} \u2192 R' A (T x A))\n (\u27e8_\u00d7'_\u27e9 : \u2200 {A B C D} \u2192 R' A C \u2192 R' B D \u2192 R' (A x B) (C x D))\n (first' : \u2200 {A B C} \u2192 R' A B \u2192 R' (A x C) (B x C))\n (second' : \u2200 {A B C} \u2192 R' B C \u2192 R' (A x B) (A x C))\n (assoc' : \u2200 {A B C} \u2192 R' (A x (B x C)) ((A x B) x C))\n (assoc\u207b\u00b9' : \u2200 {A B C} \u2192 R' ((A x B) x C) (A x (B x C)))\n (swap' : \u2200 {A B} \u2192 R' (A x B) (B x A))\n nrVars (!_ : Fin nrVars \u2192 A) where\n\n Var = Fin nrVars\n\n open import Relation.Nullary using (yes ; no)\n open import Relation.Nullary.Decidable\n\n data Syn : Set where\n var : Var \u2192 Syn\n tt : Syn\n _,_ : Syn \u2192 Syn \u2192 Syn\n\n #_ : \u2200 m {m : \u2200 {A} \u2192 R (A , tt) A\n : \u2200 {A} \u2192 R (tt , A) A\n \u207b\u00b9 : \u2200 {A} \u2192 R A (tt , A)\n \u207b\u00b9 : \u2200 {A} \u2192 R A (A , tt)\n \u27e8_\u00d7''_\u27e9 : \u2200 {A B C D} \u2192 R A C \u2192 R B D \u2192 R (A , B) (C , D)\n assoc : \u2200 {A B C} \u2192 R (A , (B , C)) ((A , B) , C)\n assoc\u207b\u00b9 : \u2200 {A B C} \u2192 R ((A , B) , C) (A , (B , C))\n id : \u2200 {A} \u2192 R A A\n swap : \u2200 {A B} \u2192 R (A , B) (B , A)\n\n \u27e8_\u00d7_\u27e9 : \u2200 {A B C D} \u2192 R A C \u2192 R B D \u2192 R (A , B) (C , D)\n \u27e8 id \u00d7 id \u27e9 = id\n \u27e8 r\u2081 \u00d7 r\u2082 \u27e9 = \u27e8 r\u2081 \u00d7'' r\u2082 \u27e9\n\n _\u223b_ : \u2200 {A B C} \u2192 R A B \u2192 R B C \u2192 R A C\n id \u223b r\u2082 = r\u2082\n r\u2081 \u223b id = r\u2081\n \u207b\u00b9 \u223b = id\n \u207b\u00b9 \u223b = id\n \u223b \u207b\u00b9 = id\n \u223b \u207b\u00b9 = id\n swap \u223b = \n swap \u223b = \n \u207b\u00b9 \u223b swap = \u207b\u00b9\n \u207b\u00b9 \u223b swap = \u207b\u00b9\n assoc \u223b assoc\u207b\u00b9 = id\n assoc \u223b (assoc\u207b\u00b9 \u223b'' r) = r\n assoc\u207b\u00b9 \u223b assoc = id\n assoc\u207b\u00b9 \u223b (assoc \u223b'' r) = r\n swap \u223b swap = id\n swap \u223b (swap \u223b'' r) = r\n (r\u2081 \u223b'' r\u2082) \u223b r\u2083 = r\u2081 \u223b (r\u2082 \u223b r\u2083)\n \u27e8 r\u2081 \u00d7'' r\u2082 \u27e9 \u223b \u27e8 r\u2083 \u00d7'' r\u2084 \u27e9 = \u27e8 r\u2081 \u223b r\u2083 \u00d7 r\u2082 \u223b r\u2084 \u27e9\n \u27e8 r\u2081 \u00d7'' r\u2082 \u27e9 \u223b (\u27e8 r\u2083 \u00d7'' r\u2084 \u27e9 \u223b'' r\u2085) with \u27e8 r\u2081 \u223b r\u2083 \u00d7 r\u2082 \u223b r\u2084 \u27e9\n ... |\u00a0id = r\u2085\n ... |\u00a0r\u2086 = r\u2086 \u223b'' r\u2085\n r\u2081 \u223b r\u2082 = r\u2081 \u223b'' r\u2082 \n\n sym : \u2200 {S S'} \u2192 R S S' \u2192 R S' S\n sym (r \u223b'' r\u2081) = sym r\u2081 \u223b sym r\n sym = \u207b\u00b9\n sym = \u207b\u00b9\n sym \u207b\u00b9 = \n sym \u207b\u00b9 = \n sym \u27e8 r \u00d7'' r\u2081 \u27e9 = \u27e8 sym r \u00d7 sym r\u2081 \u27e9\n sym assoc = assoc\u207b\u00b9\n sym assoc\u207b\u00b9 = assoc\n sym id = id\n sym swap = swap\n\n proof\u2081 : \u2200 {S S'} \u2192 R S S' \u2192 R' (eval S) (eval S')\n proof\u2081 (r \u223b'' r\u2081) = proof\u2081 r \u223b' proof\u2081 r\u2081\n proof\u2081 = '\n proof\u2081 = '\n proof\u2081 \u207b\u00b9 = \u207b\u00b9'\n proof\u2081 \u207b\u00b9 = \u207b\u00b9'\n proof\u2081 \u27e8 id \u00d7'' r \u27e9 = second' (proof\u2081 r)\n proof\u2081 \u27e8 r \u00d7'' id \u27e9 = first' (proof\u2081 r)\n proof\u2081 \u27e8 r \u00d7'' r\u2081 \u27e9 = \u27e8 proof\u2081 r \u00d7' proof\u2081 r\u2081 \u27e9\n proof\u2081 assoc = assoc'\n proof\u2081 assoc\u207b\u00b9 = assoc\u207b\u00b9'\n proof\u2081 id = id'\n proof\u2081 swap = swap'\n\n proof\u2082 : \u2200 {S S'} \u2192 R S S' \u2192 R' (eval S') (eval S)\n proof\u2082 r = proof\u2081 (sym r)\n\n data NF : Syn \u2192 Set where\n tt : NF tt\n var : (x : Var) \u2192 NF (var x)\n var_::_ : \u2200 {S}(i : Var) \u2192 NF S \u2192 NF (var i , S)\n\n record NFP S : Set where\n constructor _\u22a2_\n field\n {S'} : Syn\n term : NF S'\n proof : R S' S\n \n \n merge : \u2200 {S S'} \u2192 NF S \u2192 NF S' \u2192 NFP (S , S')\n merge tt n2 = n2 \u22a2 \u207b\u00b9\n merge (var i) n2 = (var i :: n2) \u22a2 id\n merge (var i :: n1) n2 with merge n1 n2\n ... | t \u22a2 p = (var i :: t) \u22a2 (\u27e8 id \u00d7 p \u27e9 \u223b assoc)\n\n norm : (x : Syn) \u2192 NFP x\n norm (var x) = (var x) \u22a2 id\n norm tt = tt \u22a2 id\n norm (x , x\u2081) with norm x | norm x\u2081\n ... | t1 \u22a2 p1 | t2 \u22a2 p2 with merge t1 t2\n ... | t3 \u22a2 p3 = t3 \u22a2 (p3 \u223b \u27e8 p1 \u00d7 p2 \u27e9)\n\n insert : \u2200 {S} \u2192 (x : Var) \u2192 NF S \u2192 NFP (var x , S)\n insert y tt = (var y) \u22a2 \u207b\u00b9\n insert y (var i) with (F.to\u2115 y) \u2115.\u2264? (F.to\u2115 i)\n ... | yes _ = (var y :: var i) \u22a2 id\n ... | no _ = (var i :: var y) \u22a2 swap\n insert y (var i :: n1) with (F.to\u2115 y) \u2115.\u2264? (F.to\u2115 i)\n ... | yes _ = (var y :: (var i :: n1)) \u22a2 id\n ... | no _ with insert y n1\n ... | t \u22a2 p = (var i :: t) \u22a2 (\u27e8 id \u00d7 p \u27e9 \u223b (assoc \u223b (\u27e8 swap \u00d7 id \u27e9 \u223b assoc\u207b\u00b9)))\n\n sort : \u2200 {x : Syn} \u2192 NF x \u2192 NFP x\n sort tt = tt \u22a2 id\n sort (var i) = var i \u22a2 id\n sort (var i :: n1) with sort n1\n ... | t1 \u22a2 p1 with insert i t1\n ... | t2 \u22a2 p2 = t2 \u22a2 (p2 \u223b \u27e8 id \u00d7 p1 \u27e9)\n\n normal : (x : Syn) \u2192 NFP x\n normal x with norm x\n ... | t1 \u22a2 p1 with sort t1\n ... | t2 \u22a2 p2 = t2 \u22a2 (p2 \u223b p1)\n\n open import Relation.Binary.PropositionalEquality using (_\u2261_ ; refl)\n open import Relation.Nullary\n\n import Data.Unit\n import Data.Empty\n\n id\u2261 : \u2200 {S S'} \u2192 S \u2261 S' \u2192 R S S'\n id\u2261 refl = id\n\n _\u2262_ : \u2200 {A : Set} \u2192 A \u2192 A \u2192 Set\n x \u2262 y = x \u2261 y \u2192 Data.Empty.\u22a5\n\n \u2262-cong : \u2200 {A B}{x y : A}(f : A \u2192 B) \u2192 f x \u2262 f y \u2192 x \u2262 y\n \u2262-cong f fr refl = fr refl\n\n var-inj : \u2200 {i j : Fin nrVars} \u2192 i \u2262 j \u2192 Syn.var i \u2262 var j\n var-inj p refl = p refl\n\n ,-inj\u2081 : \u2200 {x y a b} \u2192 x \u2262 y \u2192 (x Syn., a) \u2262 (y , b)\n ,-inj\u2081 p refl = p refl\n\n ,-inj\u2082 : \u2200 {x y a b} \u2192 a \u2262 b \u2192 (x Syn., a) \u2262 (y , b)\n ,-inj\u2082 p refl = p refl\n\n _\u225f_ : (x y : Syn) \u2192 Dec (x \u2261 y)\n var x \u225f var x\u2081 with x FP.\u225f x\u2081\n var .x\u2081 \u225f var x\u2081 | yes refl = yes refl\n ... | no p = no (var-inj p)\n var x \u225f tt = no (\u03bb ())\n var x \u225f (y , y\u2081) = no (\u03bb ())\n tt \u225f var x = no (\u03bb ())\n tt \u225f tt = yes refl\n tt \u225f (y , y\u2081) = no (\u03bb ())\n (x , x\u2081) \u225f var x\u2082 = no (\u03bb ())\n (x , x\u2081) \u225f tt = no (\u03bb ())\n (x , x\u2081) \u225f (y , y\u2081) with x \u225f y | x\u2081 \u225f y\u2081\n (x , x\u2081) \u225f (.x , .x\u2081) | yes refl | yes refl = yes refl\n (x , x\u2081) \u225f (y , y\u2081) | yes p | no \u00acp = no (,-inj\u2082 \u00acp)\n (x , x\u2081) \u225f (y , y\u2081) | no \u00acp | q = no (,-inj\u2081 \u00acp)\n\n CHECK : Syn \u2192 Syn \u2192 Set\n CHECK s1 s2 with s1 \u225f s2\n ... | yes p = Data.Unit.\u22a4\n ... | no p = Data.Empty.\u22a5\n\n rewire : (S\u2081 S\u2082 : Syn) \u2192 CHECK (NFP.S' (normal S\u2081)) (NFP.S' (normal S\u2082)) \u2192 R' (eval S\u2081) (eval S\u2082)\n rewire s\u2081 s\u2082 eq with NFP.S' (normal s\u2081) \u225f NFP.S' (normal s\u2082)\n ... | yes p = proof\u2081\n ((sym (NFP.proof (normal s\u2081)) \u223b id\u2261 p) \u223b NFP.proof (normal s\u2082))\n rewire _ _ () | no _\n -- proof\u2082 (NFP.proof (normal s\u2081)) \u223b' (eq \u223b' proof\u2081 (NFP.proof (normal s\u2082)))\n\n\nmodule example where\n\n open import Data.Vec\n\n open import Data.Product\n open import Data.Unit\n\n open import Function\n\n -- need to etaexpand this because otherwise we get an error\n module STest n M = Syntax Set _\u00d7_ \u22a4 (\u03bb x x\u2081 \u2192 x \u2192 x\u2081) (\u03bb x \u2192 x) \n (\u03bb x x\u2081 x\u2082 \u2192 x\u2081 (x x\u2082)) (\u03bb x \u2192 proj\u2081 x) (\u03bb x \u2192 x , tt) \n (\u03bb x \u2192 proj\u2082 x) (\u03bb x \u2192 tt , x) (\u03bb x x\u2081 x\u2082 \u2192 (x (proj\u2081 x\u2082)) , (x\u2081 (proj\u2082 x\u2082))) \n (\u03bb x x\u2081 \u2192 (x (proj\u2081 x\u2081)) , (proj\u2082 x\u2081)) (\u03bb x x\u2081 \u2192 (proj\u2081 x\u2081) , (x (proj\u2082 x\u2081))) \n (\u03bb x \u2192 ((proj\u2081 x) , (proj\u2081 (proj\u2082 x))) , (proj\u2082 (proj\u2082 x))) \n (\u03bb x \u2192 (proj\u2081 (proj\u2081 x)) , ((proj\u2082 (proj\u2081 x)) , (proj\u2082 x))) \n (\u03bb x \u2192 (proj\u2082 x) , (proj\u2081 x)) n M\n\n test : (A B C : Set) \u2192 (A \u00d7 B) \u00d7 C \u2192 (B \u00d7 A) \u00d7 C\n test A B C = rewire LHS RHS _ where\n open STest 3 (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n\n LHS = (# 0 , # 1) , # 2\n RHS = (# 1 , # 0) , # 2\n\n\nmodule example\u2082 where\n\n open import Data.Vec\n\n data Ty : Set where\n _\u00d7_ : Ty \u2192 Ty \u2192 Ty\n \u22a4 : Ty\n\n infix 4 _\u27f6_ \n\n data _\u27f6_ : Ty \u2192 Ty \u2192 Set where\n id' : \u2200 {A} \u2192 A \u27f6 A\n _\u223b'_ : \u2200 {A B C} \u2192 A \u27f6 B \u2192 B \u27f6 C \u2192 A \u27f6 C\n ' : \u2200 {A} \u2192 (A \u00d7 \u22a4) \u27f6 A\n \u207b\u00b9' : \u2200 {A} \u2192 A \u27f6 (A \u00d7 \u22a4)\n ' : \u2200 {A} \u2192 (\u22a4 \u00d7 A) \u27f6 A\n \u207b\u00b9' : \u2200 {A} \u2192 A \u27f6 (\u22a4 \u00d7 A)\n \u27e8_\u00d7'_\u27e9 : \u2200 {A B C D} \u2192 A \u27f6 C \u2192 B \u27f6 D \u2192 (A \u00d7 B) \u27f6 (C \u00d7 D)\n first : \u2200 {A B C} \u2192 A \u27f6 B \u2192 A \u00d7 C \u27f6 B \u00d7 C\n second : \u2200 {A B C} \u2192 B \u27f6 C \u2192 A \u00d7 B \u27f6 A \u00d7 C \n assoc' : \u2200 {A B C} \u2192 (A \u00d7 (B \u00d7 C)) \u27f6 ((A \u00d7 B) \u00d7 C)\n assoc\u207b\u00b9' : \u2200 {A B C} \u2192 ((A \u00d7 B) \u00d7 C) \u27f6 (A \u00d7 (B \u00d7 C))\n swap' : \u2200 {A B} \u2192 (A \u00d7 B) \u27f6 (B \u00d7 A)\n \n\n module STest n M = Syntax Ty _\u00d7_ \u22a4 _\u27f6_ id' _\u223b'_ ' \u207b\u00b9' ' \u207b\u00b9' \u27e8_\u00d7'_\u27e9 first second assoc' assoc\u207b\u00b9' swap' n M\n\n test2 : (A B C : Ty) \u2192 (A \u00d7 B) \u00d7 C \u27f6 (B \u00d7 A) \u00d7 C\n test2 A B C = rewire ((# 0 , # 1) , # 2) ((# 1 , # 0) , # 2) _ where\n open STest 3 (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"86469b61012b492b0ff97b1f013785d45df79fac","subject":"Avoiding bug \"undefined\", changing Zq to BigI","message":"Avoiding bug \"undefined\", changing Zq to BigI\n","repos":"crypto-agda\/crypto-agda","old_file":"ZK\/JSChecker.agda","new_file":"ZK\/JSChecker.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule ZK.JSChecker where\n\nopen import Function using (id; _\u2218\u2032_; case_of_)\nopen import Data.Bool.Base using (Bool; true; false; _\u2227_)\nopen import Data.List.Base using (List; []; _\u2237_; and; foldr)\nopen import Data.String.Base using (String)\n\nopen import FFI.JS\n hiding (check)\n renaming (_*_ to _*Number_)\n-- open import FFI.JS.Proc using (URI; JSProc; showURI; server)\n-- open import Control.Process.Type\nimport FFI.JS.Console as Console\nimport FFI.JS.Process as Process\nimport FFI.JS.FS as FS\n\nimport FiniteField.JS as \ud835\udd3d\n\nimport FFI.JS.BigI as BigI\nopen BigI using (BigI; bigI)\n\n-- trace : {A B : Set}(msg : String)(inp : A)(f : A \u2192 B) \u2192 B\n-- trace _ inp f = f inp\n\nbignum : Number \u2192 BigI\nbignum n = bigI (Number\u25b9String n) \"10\"\n\n-- TODO check with undefined\nbigdec : JSValue \u2192 BigI\nbigdec v = bigI (castString v) \"10\"\n\n-- TODO bug (undefined)!\nrecord ZK-chaum-pedersen-pok-elgamal-rnd {--(\u2124q \u2124p\u2605 : Set)--} : Set where\n field\n m c s : BigI {--\u2124q--}\n g p q y \u03b1 \u03b2 A B : BigI --\u2124p\u2605\n\n-- TODO dynamise me\nt : Number\nt = readNumber \"10\"\n\n-- TODO: check if this is large enough\nmin-bits-q : Number\nmin-bits-q = 256N\n\nmin-bits-p : Number\nmin-bits-p = 2048N\n\ncheck : (title : String)\n (pred : Bool)\n (errmsg : \ud835\udfd9 \u2192 String)\n \u2192 JS!\ncheck title true errmsg = Console.log (title ++ \": PASS\")\ncheck title false errmsg = Console.log (title ++ \": FAIL [\" ++ errmsg _ ++ \"]\")\n\ncheck-size : Number \u2192 String \u2192 BigI \u2192 JS!\ncheck-size min-bits name value =\n check (\"check size of \" ++ name)\n (len \u2265Number min-bits)\n (\u03bb _ \u2192 name ++ \" is not a necessarily a safe prime: \"\n ++ BigI.toString value ++ \" has \"\n ++ Number\u25b9String len ++ \" bits which is less than \"\n ++ Number\u25b9String min-bits ++ \" bits\")\n module Check-size where\n len = BigI.byteLength value *Number 8N\n\ncheck-pq-relation : (p q : BigI) \u2192 JS!\ncheck-pq-relation p q =\n check (\"check p and q relation p-1\/q=\" ++ BigI.toString s)\n (equals r 0I)\n (\u03bb _ \u2192 \"Not necessarily a safe group: (p-1) mod q != 0\\np=\"\n ++ BigI.toString p\n ++ \", q=\" ++ BigI.toString q)\n module Check-pq-relation where\n open BigI\n p-1 = subtract p 1I\n r = mod p-1 q\n s = divide p-1 q\n\ncheck-primality : String \u2192 BigI \u2192 JS!\ncheck-primality name value =\n check (\"check primality of \" ++ name)\n (BigI.isProbablePrime value t)\n (\u03bb _ \u2192 \"Not a prime number: \" ++ BigI.toString value)\n\ncheck-generator-group-order : (g q p : BigI) \u2192 JS!\ncheck-generator-group-order g q p =\n check \"check generator & group order\"\n (BigI.equals (BigI.modPow g q p) BigI.1I)\n (\u03bb _ \u2192 \"Not a generator of a group of order q: modPow \"\n ++ BigI.toString g ++ \" \" ++ BigI.toString q ++ \" \"\n ++ BigI.toString p)\n\nmodule [\u2124q]\u2124p\u2605 (qI pI gI : BigI) where\n\n checks : JS!\n checks =\n check-pq-relation pI qI >>\n check-size min-bits-q \"q\" qI >>\n check-size min-bits-p \"p\" pI >>\n check-primality \"q\" qI >>\n check-primality \"p\" pI >>\n check-generator-group-order gI qI pI\n\n module \u2124q = \ud835\udd3d qI\n using (0#; 1#; _+_; _\u2212_; _*_; _\/_)\n renaming (\ud835\udd3d to \u2124q; fromBigI to BigI\u25b9\u2124q; repr to \u2124q-repr)\n module \u2124p\u2605 = \ud835\udd3d pI\n using (_==_)\n renaming ( fromBigI to BigI\u25b9\u2124p\u2605; \ud835\udd3d to \u2124p\u2605; _*_ to _\u00b7_\n ; repr to \u2124p\u2605-repr; _\/_ to _\u00b7\/_)\n\n open \u2124q -- public -- <- BUG\n open \u2124p\u2605 public\n\n g : \u2124p\u2605\n g = BigI\u25b9\u2124p\u2605 gI\n\n _^_ : \u2124p\u2605 \u2192 \u2124q \u2192 \u2124p\u2605\n b ^ e = BigI\u25b9\u2124p\u2605 (BigI.modPow (\u2124p\u2605-repr b) (\u2124q-repr e) pI)\n\nzk-check-chaum-pedersen-pok-elgamal-rnd : ZK-chaum-pedersen-pok-elgamal-rnd {-BigI BigI-} \u2192 JS!\nzk-check-chaum-pedersen-pok-elgamal-rnd pf\n = trace \"g=\" g \u03bb _ \u2192\n trace \"p=\" I.p \u03bb _ \u2192\n trace \"q=\" I.q \u03bb _ \u2192\n trace \"y=\" y \u03bb _ \u2192\n trace \"\u03b1=\" \u03b1 \u03bb _ \u2192\n trace \"\u03b2=\" \u03b2 \u03bb _ \u2192\n trace \"m=\" m \u03bb _ \u2192\n trace \"M=\" M \u03bb _ \u2192\n trace \"A=\" A \u03bb _ \u2192\n trace \"B=\" B \u03bb _ \u2192\n trace \"c=\" c \u03bb _ \u2192\n trace \"s=\" s \u03bb _ \u2192\n checks\n >> check \"g^s==A\u00b7\u03b1^c\" ((g ^ s) == (A \u00b7 (\u03b1 ^ c))) (\u03bb _ \u2192 \"\")\n >> check \"y^s==B\u00b7(\u03b2\/M)^c\" ((y ^ s) == (B \u00b7 ((\u03b2 \u00b7\/ M) ^ c))) (\u03bb _ \u2192 \"\")\n module ZK-check-chaum-pedersen-pok-elgamal-rnd where\n module I = ZK-chaum-pedersen-pok-elgamal-rnd pf\n open module IM = [\u2124q]\u2124p\u2605 I.q I.p I.g\n open \u2124q\n-- open \u2124p\u2605 -- <- BUG\n A = BigI\u25b9\u2124p\u2605 I.A\n B = BigI\u25b9\u2124p\u2605 I.B\n \u03b1 = BigI\u25b9\u2124p\u2605 I.\u03b1\n \u03b2 = BigI\u25b9\u2124p\u2605 I.\u03b2\n y = BigI\u25b9\u2124p\u2605 I.y\n s = BigI\u25b9\u2124q I.s\n c = BigI\u25b9\u2124q I.c\n m = BigI\u25b9\u2124q I.m\n M = g ^ m\n\nzk-check : JSValue \u2192 JS!\nzk-check arg =\n check \"type of statement\" (typ === fromString \"chaum-pedersen-pok-elgamal-rnd\")\n (\u03bb _ \u2192 \"\")\n >> zk-check-chaum-pedersen-pok-elgamal-rnd pok\n module Zk-check where\n stm = arg \u00b7\u00ab \"statement\" \u00bb\n typ = stm \u00b7\u00ab \"type\" \u00bb\n dat = stm \u00b7\u00ab \"data\" \u00bb\n g = bigdec (dat \u00b7\u00ab \"g\" \u00bb)\n p = bigdec (dat \u00b7\u00ab \"p\" \u00bb)\n q = bigdec (dat \u00b7\u00ab \"q\" \u00bb)\n y = bigdec (dat \u00b7\u00ab \"y\" \u00bb)\n m = bigdec (dat \u00b7\u00ab \"plain\" \u00bb)\n enc = dat \u00b7\u00ab \"enc\" \u00bb\n \u03b1 = bigdec (enc \u00b7\u00ab \"alpha\" \u00bb)\n \u03b2 = bigdec (enc \u00b7\u00ab \"beta\" \u00bb)\n prf = arg \u00b7\u00ab \"proof\" \u00bb\n com = prf \u00b7\u00ab \"commitment\" \u00bb\n A = bigdec (com \u00b7\u00ab \"A\" \u00bb)\n B = bigdec (com \u00b7\u00ab \"B\" \u00bb)\n c = bigdec (prf \u00b7\u00ab \"challenge\" \u00bb)\n s = bigdec (prf \u00b7\u00ab \"response\" \u00bb)\n pok = record { g = g; p = p; q = q; y = y; \u03b1 = \u03b1; \u03b2 = \u03b2; A = A; B = B; c = c; s = s; m = m }\n\n{-\nsrv : URI \u2192 JSProc\nsrv d =\n recv d \u03bb q \u2192\n send d (fromBool (zk-check q))\n end\n-}\n\n-- Working around Agda.Primitive.lsuc being undefined\n-- case_of_ : {A : Set} {B : Set} \u2192 A \u2192 (A \u2192 B) \u2192 B\n-- case x of f = f x\n\nmain : JS!\nmain =\n Process.argv !\u2081 \u03bb args \u2192\n case JSArray\u25b9ListString args of \u03bb {\n (_node \u2237 _run \u2237 _test \u2237 args') \u2192\n case args' of \u03bb {\n [] \u2192\n Console.log \"usage: No arguments\"\n {- server \"127.0.0.1\" \"1337\" srv !\u2081 \u03bb uri \u2192\n Console.log (showURI uri)\n -}\n ; (arg \u2237 args'') \u2192\n case args'' of \u03bb {\n [] \u2192\n FS.readFile arg nullJS !\u2082 \u03bb err dat \u2192\n check \"reading input file\" (is-null err)\n (\u03bb _ \u2192 \"readFile error: \" ++ toString err) >>\n zk-check (JSON-parse (castString dat))\n ; _ \u2192\n Console.log \"usage: Too many arguments\"\n }\n }\n ; _ \u2192\n Console.log \"usage\"\n }\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule ZK.JSChecker where\n\nopen import Function using (id; _\u2218\u2032_; case_of_)\nopen import Data.Bool.Base using (Bool; true; false; _\u2227_)\nopen import Data.List.Base using (List; []; _\u2237_; and; foldr)\nopen import Data.String.Base using (String)\n\nopen import FFI.JS\n hiding (check; trace)\n renaming (_*_ to _*Number_)\n-- open import FFI.JS.Proc using (URI; JSProc; showURI; server)\n-- open import Control.Process.Type\nimport FFI.JS.Console as Console\nimport FFI.JS.Process as Process\nimport FFI.JS.FS as FS\n\nimport FiniteField.JS as \ud835\udd3d\n\nimport FFI.JS.BigI as BigI\nopen BigI using (BigI; bigI)\n\ntrace : {A B : Set}(msg : String)(inp : A)(f : A \u2192 B) \u2192 B\ntrace _ inp f = f inp\n\nbignum : Number \u2192 BigI\nbignum n = bigI (Number\u25b9String n) \"10\"\n\n-- TODO check with undefined\nbigdec : JSValue \u2192 BigI\nbigdec v = bigI (castString v) \"10\"\n\nrecord ZK-chaum-pedersen-pok-elgamal-rnd (\u2124q \u2124p\u2605 : Set) : Set where\n field\n m c s : \u2124q\n g p q y \u03b1 \u03b2 A B : \u2124p\u2605\n\n-- TODO dynamise me\nt : Number\nt = readNumber \"10\"\n\n-- TODO: check if this is large enough\nmin-bits-q : Number\nmin-bits-q = 256N\n\nmin-bits-p : Number\nmin-bits-p = 2048N\n\ncheck : (title : String)\n (pred : Bool)\n (errmsg : \ud835\udfd9 \u2192 String)\n \u2192 JS!\ncheck title true errmsg = Console.log (title ++ \": PASS\")\ncheck title false errmsg = Console.log (title ++ \": FAIL [\" ++ errmsg _ ++ \"]\")\n\ncheck-size : Number \u2192 String \u2192 BigI \u2192 JS!\ncheck-size min-bits name value =\n check (\"check size of \" ++ name)\n (len \u2265Number min-bits)\n (\u03bb _ \u2192 name ++ \" is not a necessarily a safe prime: \"\n ++ BigI.toString value ++ \" has \"\n ++ Number\u25b9String len ++ \" bits which is less than \"\n ++ Number\u25b9String min-bits ++ \" bits\")\n module Check-size where\n len = BigI.byteLength value *Number 8N\n\ncheck-pq-relation : (p q : BigI) \u2192 JS!\ncheck-pq-relation p q =\n check (\"check p and q relation p-1\/q=\" ++ BigI.toString s)\n (equals r 0I)\n (\u03bb _ \u2192 \"Not necessarily a safe group: (p-1) mod q != 0\\np=\"\n ++ BigI.toString p\n ++ \", q=\" ++ BigI.toString q)\n module Check-pq-relation where\n open BigI\n p-1 = subtract p 1I\n r = mod p-1 q\n s = divide p-1 q\n\ncheck-primality : String \u2192 BigI \u2192 JS!\ncheck-primality name value =\n check (\"check primality of \" ++ name)\n (BigI.isProbablePrime value t)\n (\u03bb _ \u2192 \"Not a prime number: \" ++ BigI.toString value)\n\ncheck-generator-group-order : (g q p : BigI) \u2192 JS!\ncheck-generator-group-order g q p =\n check \"check generator & group order\"\n (BigI.equals (BigI.modPow g q p) BigI.1I)\n (\u03bb _ \u2192 \"Not a generator of a group of order q: modPow \"\n ++ BigI.toString g ++ \" \" ++ BigI.toString q ++ \" \"\n ++ BigI.toString p)\n\nmodule [\u2124q]\u2124p\u2605 (qI pI gI : BigI) where\n\n checks : JS!\n checks =\n check-pq-relation pI qI >>\n check-size min-bits-q \"q\" qI >>\n check-size min-bits-p \"p\" pI >>\n check-primality \"q\" qI >>\n check-primality \"p\" pI >>\n check-generator-group-order gI qI pI\n\n module \u2124q = \ud835\udd3d qI\n using (0#; 1#; _+_; _\u2212_; _*_; _\/_)\n renaming (\ud835\udd3d to \u2124q; fromBigI to BigI\u25b9\u2124q; repr to \u2124q-repr)\n module \u2124p\u2605 = \ud835\udd3d pI\n using (_==_)\n renaming ( fromBigI to BigI\u25b9\u2124p\u2605; \ud835\udd3d to \u2124p\u2605; _*_ to _\u00b7_\n ; repr to \u2124p\u2605-repr; _\/_ to _\u00b7\/_)\n\n open \u2124q -- public -- <- BUG\n open \u2124p\u2605 public\n\n g : \u2124p\u2605\n g = BigI\u25b9\u2124p\u2605 gI\n\n _^_ : \u2124p\u2605 \u2192 \u2124q \u2192 \u2124p\u2605\n b ^ e = BigI\u25b9\u2124p\u2605 (BigI.modPow (\u2124p\u2605-repr b) (\u2124q-repr e) pI)\n\nzk-check-chaum-pedersen-pok-elgamal-rnd : ZK-chaum-pedersen-pok-elgamal-rnd BigI BigI \u2192 JS!\nzk-check-chaum-pedersen-pok-elgamal-rnd pf\n = trace \"g=\" g \u03bb _ \u2192\n trace \"p=\" I.p \u03bb _ \u2192\n trace \"q=\" I.q \u03bb _ \u2192\n trace \"y=\" y \u03bb _ \u2192\n trace \"\u03b1=\" \u03b1 \u03bb _ \u2192\n trace \"\u03b2=\" \u03b2 \u03bb _ \u2192\n trace \"m=\" m \u03bb _ \u2192\n trace \"M=\" M \u03bb _ \u2192\n trace \"A=\" A \u03bb _ \u2192\n trace \"B=\" B \u03bb _ \u2192\n trace \"c=\" c \u03bb _ \u2192\n trace \"s=\" s \u03bb _ \u2192\n checks\n >> check \"g^s==A\u00b7\u03b1^c\" ((g ^ s) == (A \u00b7 (\u03b1 ^ c))) (\u03bb _ \u2192 \"\")\n >> check \"y^s==B\u00b7(\u03b2\/M)^c\" ((y ^ s) == (B \u00b7 ((\u03b2 \u00b7\/ M) ^ c))) (\u03bb _ \u2192 \"\")\n module Zk-check-chaume-pedersen-pok-elgamal-rnd where\n module I = ZK-chaum-pedersen-pok-elgamal-rnd pf\n open [\u2124q]\u2124p\u2605 I.q I.p I.g\n open \u2124q\n-- open \u2124p\u2605 -- <- BUG\n A = BigI\u25b9\u2124p\u2605 I.A\n B = BigI\u25b9\u2124p\u2605 I.B\n \u03b1 = BigI\u25b9\u2124p\u2605 I.\u03b1\n \u03b2 = BigI\u25b9\u2124p\u2605 I.\u03b2\n y = BigI\u25b9\u2124p\u2605 I.y\n s = BigI\u25b9\u2124q I.s\n c = BigI\u25b9\u2124q I.c\n m = BigI\u25b9\u2124q I.m\n M = g ^ m\n\nzk-check : JSValue \u2192 JS!\nzk-check arg =\n check \"type of statement\" (typ === fromString \"chaum-pedersen-pok-elgamal-rnd\")\n (\u03bb _ \u2192 \"\")\n >> zk-check-chaum-pedersen-pok-elgamal-rnd pok\n module Zk-check where\n stm = arg \u00b7\u00ab \"statement\" \u00bb\n typ = stm \u00b7\u00ab \"type\" \u00bb\n dat = stm \u00b7\u00ab \"data\" \u00bb\n g = bigdec (dat \u00b7\u00ab \"g\" \u00bb)\n p = bigdec (dat \u00b7\u00ab \"p\" \u00bb)\n q = bigdec (dat \u00b7\u00ab \"q\" \u00bb)\n y = bigdec (dat \u00b7\u00ab \"y\" \u00bb)\n m = bigdec (dat \u00b7\u00ab \"plain\" \u00bb)\n enc = dat \u00b7\u00ab \"enc\" \u00bb\n \u03b1 = bigdec (enc \u00b7\u00ab \"alpha\" \u00bb)\n \u03b2 = bigdec (enc \u00b7\u00ab \"beta\" \u00bb)\n prf = arg \u00b7\u00ab \"proof\" \u00bb\n com = prf \u00b7\u00ab \"commitment\" \u00bb\n A = bigdec (com \u00b7\u00ab \"A\" \u00bb)\n B = bigdec (com \u00b7\u00ab \"B\" \u00bb)\n c = bigdec (prf \u00b7\u00ab \"challenge\" \u00bb)\n s = bigdec (prf \u00b7\u00ab \"response\" \u00bb)\n pok = record { g = g; p = p; q = q; y = y; \u03b1 = \u03b1; \u03b2 = \u03b2; A = A; B = B; c = c; s = s; m = m }\n\n{-\nsrv : URI \u2192 JSProc\nsrv d =\n recv d \u03bb q \u2192\n send d (fromBool (zk-check q))\n end\n-}\n\n-- Working around Agda.Primitive.lsuc being undefined\n-- case_of_ : {A : Set} {B : Set} \u2192 A \u2192 (A \u2192 B) \u2192 B\n-- case x of f = f x\n\nmain : JS!\nmain =\n Process.argv !\u2081 \u03bb args \u2192\n case JSArray\u25b9ListString args of \u03bb {\n (_node \u2237 _run \u2237 _test \u2237 args') \u2192\n case args' of \u03bb {\n [] \u2192\n Console.log \"usage: No arguments\"\n {- server \"127.0.0.1\" \"1337\" srv !\u2081 \u03bb uri \u2192\n Console.log (showURI uri)\n -}\n ; (arg \u2237 args'') \u2192\n case args'' of \u03bb {\n [] \u2192\n FS.readFile arg nullJS !\u2082 \u03bb err dat \u2192\n check \"reading input file\" (is-null err)\n (\u03bb _ \u2192 \"readFile error: \" ++ toString err) >>\n zk-check (JSON-parse (castString dat))\n ; _ \u2192\n Console.log \"usage: Too many arguments\"\n }\n }\n ; _ \u2192\n Console.log \"usage\"\n }\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"9025fe6c69d96bf8ff35600a5e95baeaadf809d6","subject":"Updated the mirror draft.","message":"Updated the mirror draft.\n\nIgnore-this: 775599b409b5d3ddd96b6542a5d61e1\n\ndarcs-hash:20110211172236-3bd4e-5593c52c7622802e3890bc8c25540f9493e5dd86.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/Mirror\/PropertiesI.agda","new_file":"Draft\/Mirror\/PropertiesI.agda","new_contents":"------------------------------------------------------------------------------\n-- Properties of the mirror function\n------------------------------------------------------------------------------\n\nmodule Draft.Mirror.PropertiesI where\n\nopen import LTC.Base\n\nopen import Draft.Mirror.Mirror\nopen import Draft.Mirror.ListTree.PropertiesI\n\nopen import LTC.Data.List\nopen import LTC.Data.List.PropertiesI using ( reverse-[x]\u2261[x] )\nopen import Draft.Mirror.ListTree.PropertiesI\n\nopen import LTC.Relation.Binary.EqReasoning\n\n------------------------------------------------------------------------------\n\n-- TODO: To remove the postulate\npostulate\n mirror-Tree : \u2200 {t} \u2192 Tree t \u2192 Tree (mirror \u00b7 t)\n\n-- mirror-Tree : \u2200 {t} \u2192 Tree t \u2192 Tree (mirror \u00b7 t)\n-- mirror-Tree (treeT d nilLT) =\n-- subst Tree (sym (mirror-eq d [])) (treeT d aux\u2082)\n-- where\n-- aux\u2081 : rev (map mirror []) [] \u2261 []\n-- aux\u2081 =\n-- begin\n-- rev (map mirror []) []\n-- \u2261\u27e8 subst (\u03bb x \u2192 rev (map mirror []) [] \u2261 rev x [])\n-- (map-[] mirror)\n-- refl\n-- \u27e9\n-- rev [] []\n-- \u2261\u27e8 rev-[] [] \u27e9\n-- []\n-- \u220e\n\n-- aux\u2082 : ListTree (rev (map mirror []) [])\n-- aux\u2082 = subst ListTree (sym aux\u2081) nilLT\n\n-- mirror-Tree (treeT d (consLT {t} {ts} Tt LTts)) =\n-- subst Tree (sym (mirror-eq d (t \u2237 ts))) (treeT d aux)\n\n-- where\n-- aux : ListTree (reverse (map mirror (t \u2237 ts)))\n-- aux = rev-ListTree (map-ListTree mirror {!!} (consLT Tt LTts)) nilLT\n\nmutual\n mirror\u00b2 : \u2200 {t} \u2192 Tree t \u2192 mirror \u00b7 (mirror \u00b7 t) \u2261 t\n mirror\u00b2 (treeT d nilLT) =\n begin\n mirror \u00b7 (mirror \u00b7 (node d []))\n \u2261\u27e8 subst (\u03bb x \u2192 mirror \u00b7 (mirror \u00b7 (node d [])) \u2261 mirror \u00b7 x )\n (mirror-eq d [])\n refl\n \u27e9\n mirror \u00b7 node d (reverse (map mirror []))\n \u2261\u27e8 subst (\u03bb x \u2192 mirror \u00b7 node d (reverse (map mirror [])) \u2261\n mirror \u00b7 node d (reverse x))\n (map-[] mirror)\n refl\n \u27e9\n mirror \u00b7 node d (reverse [])\n \u2261\u27e8 subst (\u03bb x \u2192 mirror \u00b7 node d (reverse []) \u2261 mirror \u00b7 node d x)\n (rev-[] [])\n refl\n \u27e9\n mirror \u00b7 node d []\n \u2261\u27e8 mirror-eq d [] \u27e9\n node d (reverse (map mirror []))\n \u2261\u27e8 subst (\u03bb x \u2192 node d (reverse (map mirror [])) \u2261\n node d (reverse x))\n (map-[] mirror)\n refl\n \u27e9\n node d (reverse [])\n \u2261\u27e8 subst (\u03bb x \u2192 node d (reverse []) \u2261 node d x)\n (rev-[] [])\n refl\n \u27e9\n node d []\n \u220e\n\n mirror\u00b2 (treeT d (consLT {t} {ts} Tt LTts)) =\n begin\n mirror \u00b7 (mirror \u00b7 node d (t \u2237 ts))\n \u2261\u27e8 subst (\u03bb x \u2192 mirror \u00b7 (mirror \u00b7 node d (t \u2237 ts)) \u2261 mirror \u00b7 x)\n (mirror-eq d (t \u2237 ts))\n refl\n \u27e9\n mirror \u00b7 node d (reverse (map mirror (t \u2237 ts)))\n \u2261\u27e8 subst (\u03bb x \u2192 mirror \u00b7 node d (reverse (map mirror (t \u2237 ts))) \u2261\n x)\n (mirror-eq d (reverse (map mirror (t \u2237 ts))))\n refl\n \u27e9\n node d (reverse (map mirror (reverse (map mirror (t \u2237 ts)))))\n \u2261\u27e8 subst (\u03bb x \u2192 node d (reverse (map mirror (reverse (map mirror (t \u2237 ts))))) \u2261\n node d x)\n (aux (consLT Tt LTts))\n refl\n \u27e9\n node d (t \u2237 ts)\n \u220e\n\n aux : \u2200 {ts} \u2192 ListTree ts \u2192\n reverse (map mirror (reverse (map mirror ts))) \u2261 ts\n aux nilLT =\n begin\n reverse (map mirror (reverse (map mirror [])))\n \u2261\u27e8 subst (\u03bb x \u2192 reverse (map mirror (reverse (map mirror []))) \u2261\n reverse (map mirror (reverse x)))\n (map-[] mirror)\n refl\n \u27e9\n reverse (map mirror (reverse []))\n \u2261\u27e8 subst (\u03bb x \u2192 reverse (map mirror (reverse [])) \u2261\n reverse (map mirror x))\n (rev-[] [])\n refl\n \u27e9\n reverse (map mirror [])\n \u2261\u27e8 subst (\u03bb x \u2192 reverse (map mirror []) \u2261\n reverse x)\n (map-[] mirror)\n refl\n \u27e9\n reverse []\n \u2261\u27e8 rev-[] [] \u27e9\n []\n \u220e\n\n aux (consLT {t} {ts} Tt LTts) =\n begin\n reverse (map mirror (reverse (map mirror (t \u2237 ts))))\n \u2261\u27e8 subst (\u03bb x \u2192 reverse (map mirror (reverse (map mirror (t \u2237 ts)))) \u2261\n reverse (map mirror (reverse x)))\n (map-\u2237 mirror t ts)\n refl\n \u27e9\n reverse (map mirror (reverse (mirror \u00b7 t \u2237 map mirror ts)))\n \u2261\u27e8 subst (\u03bb x \u2192 reverse (map mirror (reverse (mirror \u00b7 t \u2237 map mirror ts))) \u2261\n reverse (map mirror x))\n (reverse-\u2237 (mirror \u00b7 t) (map-ListTree mirror mirror-Tree LTts))\n refl\n \u27e9\n reverse (map mirror (reverse (map mirror ts) ++ (mirror \u00b7 t \u2237 [])))\n \u2261\u27e8 subst (\u03bb x \u2192 (reverse (map mirror (reverse (map mirror ts) ++\n mirror \u00b7 t \u2237 []))) \u2261\n reverse x)\n (map-++ mirror\n mirror-Tree\n (rev-ListTree (map-ListTree mirror mirror-Tree LTts) nilLT)\n (consLT (mirror-Tree Tt) nilLT))\n refl\n \u27e9\n reverse (map mirror (reverse (map mirror ts)) ++\n (map mirror (mirror \u00b7 t \u2237 [])))\n \u2261\u27e8 subst (\u03bb x \u2192 (reverse (map mirror (reverse (map mirror ts)) ++\n (map mirror (mirror \u00b7 t \u2237 [])))) \u2261\n x)\n (reverse-++ (map-ListTree mirror\n mirror-Tree\n (rev-ListTree (map-ListTree mirror\n mirror-Tree\n LTts)\n nilLT))\n (map-ListTree mirror\n mirror-Tree\n (consLT (mirror-Tree Tt) nilLT)))\n refl\n \u27e9\n reverse (map mirror (mirror \u00b7 t \u2237 [])) ++\n reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 subst (\u03bb x \u2192 reverse (map mirror (mirror \u00b7 t \u2237 [])) ++ n\u2081 \u2261\n reverse x ++ n\u2081)\n (map-\u2237 mirror (mirror \u00b7 t) [])\n refl\n \u27e9\n reverse (mirror \u00b7 (mirror \u00b7 t) \u2237 map mirror []) ++ n\u2081\n \u2261\u27e8 subst (\u03bb x \u2192 reverse (mirror \u00b7 (mirror \u00b7 t) \u2237 map mirror []) ++ n\u2081 \u2261\n reverse (mirror \u00b7 (mirror \u00b7 t) \u2237 x) ++ n\u2081)\n (map-[] mirror)\n refl\n \u27e9\n reverse (mirror \u00b7 (mirror \u00b7 t) \u2237 []) ++ n\u2081\n \u2261\u27e8 subst (\u03bb x \u2192 reverse (mirror \u00b7 (mirror \u00b7 t) \u2237 []) ++ n\u2081 \u2261\n x ++ n\u2081)\n (reverse-[x]\u2261[x] (mirror \u00b7 (mirror \u00b7 t)))\n refl\n \u27e9\n (mirror \u00b7 (mirror \u00b7 t) \u2237 []) ++ n\u2081\n \u2261\u27e8 ++-\u2237 (mirror \u00b7 (mirror \u00b7 t)) [] n\u2081 \u27e9\n mirror \u00b7 (mirror \u00b7 t) \u2237 [] ++ n\u2081\n \u2261\u27e8 subst (\u03bb x \u2192 mirror \u00b7 (mirror \u00b7 t) \u2237 [] ++ n\u2081 \u2261\n mirror \u00b7 (mirror \u00b7 t) \u2237 x)\n (++-leftIdentity LTn\u2081)\n refl\n \u27e9\n mirror \u00b7 (mirror \u00b7 t) \u2237 reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 subst (\u03bb x \u2192 (mirror \u00b7 (mirror \u00b7 t) \u2237\n reverse (map mirror (reverse (map mirror ts)))) \u2261\n (x \u2237 reverse (map mirror (reverse (map mirror ts)))))\n (mirror\u00b2 Tt) -- IH.\n refl\n \u27e9\n t \u2237 reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 subst (\u03bb x \u2192 t \u2237 reverse (map mirror (reverse (map mirror ts))) \u2261\n t \u2237 x)\n (aux LTts)\n refl\n \u27e9\n t \u2237 ts\n \u220e\n where\n n\u2081 : D\n n\u2081 = reverse (map mirror (reverse (map mirror ts)))\n\n LTn\u2081 : ListTree n\u2081\n LTn\u2081 = rev-ListTree (map-ListTree mirror\n mirror-Tree\n (rev-ListTree (map-ListTree mirror\n mirror-Tree\n LTts)\n nilLT))\n nilLT\n","old_contents":"------------------------------------------------------------------------------\n-- Properties of the mirror function\n------------------------------------------------------------------------------\n\nmodule Draft.Mirror.PropertiesI where\n\nopen import LTC.Base\n\nopen import Draft.Mirror.Mirror\nopen import Draft.Mirror.ListTree.PropertiesI\n\nopen import LTC.Data.List\nopen import LTC.Data.List.PropertiesI using ( reverse-[x]\u2261[x] )\nopen import Draft.Mirror.ListTree.PropertiesI\n\nopen import LTC.Relation.Binary.EqReasoning\n\n------------------------------------------------------------------------------\n\n-- TODO: To remove\npostulate\n mirror-Tree : \u2200 {t} \u2192 Tree t \u2192 Tree (mirror \u00b7 t)\n\n-- mirror-Tree : \u2200 {t} \u2192 Tree t \u2192 Tree (mirror \u00b7 t)\n-- mirror-Tree (treeT d nilLT) =\n-- subst Tree (sym (mirror-eq d [])) (treeT d aux\u2082)\n-- where\n-- aux\u2081 : rev (map mirror []) [] \u2261 []\n-- aux\u2081 =\n-- begin\n-- rev (map mirror []) []\n-- \u2261\u27e8 subst (\u03bb x \u2192 rev (map mirror []) [] \u2261 rev x [])\n-- (map-[] mirror)\n-- refl\n-- \u27e9\n-- rev [] []\n-- \u2261\u27e8 rev-[] [] \u27e9\n-- []\n-- \u220e\n\n-- aux\u2082 : ListTree (rev (map mirror []) [])\n-- aux\u2082 = subst ListTree (sym aux\u2081) nilLT\n\n-- mirror-Tree (treeT d (consLT {t} {ts} Tt LTts)) =\n-- subst Tree (sym (mirror-eq d (t \u2237 ts))) (treeT d aux)\n\n-- where\n-- aux : ListTree (reverse (map mirror (t \u2237 ts)))\n-- aux = rev-ListTree (map-ListTree mirror {!!} (consLT Tt LTts)) nilLT\n\nmirror\u00b2 : \u2200 {t} \u2192 Tree t \u2192 mirror \u00b7 (mirror \u00b7 t) \u2261 t\nmirror\u00b2 (treeT d nilLT) =\n begin\n mirror \u00b7 (mirror \u00b7 (node d []))\n \u2261\u27e8 subst (\u03bb x \u2192 mirror \u00b7 (mirror \u00b7 (node d [])) \u2261 mirror \u00b7 x )\n (mirror-eq d [])\n refl\n \u27e9\n mirror \u00b7 node d (reverse (map mirror []))\n \u2261\u27e8 subst (\u03bb x \u2192 mirror \u00b7 node d (reverse (map mirror [])) \u2261\n mirror \u00b7 node d (reverse x))\n (map-[] mirror)\n refl\n \u27e9\n mirror \u00b7 node d (reverse [])\n \u2261\u27e8 subst (\u03bb x \u2192 mirror \u00b7 node d (reverse []) \u2261 mirror \u00b7 node d x)\n (rev-[] [])\n refl\n \u27e9\n mirror \u00b7 node d []\n \u2261\u27e8 mirror-eq d [] \u27e9\n node d (reverse (map mirror []))\n \u2261\u27e8 subst (\u03bb x \u2192 node d (reverse (map mirror [])) \u2261\n node d (reverse x))\n (map-[] mirror)\n refl\n \u27e9\n node d (reverse [])\n \u2261\u27e8 subst (\u03bb x \u2192 node d (reverse []) \u2261 node d x)\n (rev-[] [])\n refl\n \u27e9\n node d []\n \u220e\n\nmirror\u00b2 (treeT d (consLT {t} {ts} Tt LTts)) =\n begin\n mirror \u00b7 (mirror \u00b7 node d (t \u2237 ts))\n \u2261\u27e8 subst (\u03bb x \u2192 mirror \u00b7 (mirror \u00b7 node d (t \u2237 ts)) \u2261 mirror \u00b7 x)\n (mirror-eq d (t \u2237 ts))\n refl\n \u27e9\n mirror \u00b7 node d (reverse (map mirror (t \u2237 ts)))\n \u2261\u27e8 subst (\u03bb x \u2192 mirror \u00b7 node d (reverse (map mirror (t \u2237 ts))) \u2261\n mirror \u00b7 node d (reverse x))\n (map-\u2237 mirror t ts)\n refl\n \u27e9\n mirror \u00b7 node d (reverse (mirror \u00b7 t \u2237 map mirror ts))\n \u2261\u27e8 subst (\u03bb x \u2192 mirror \u00b7 node d (reverse (mirror \u00b7 t \u2237 map mirror ts)) \u2261\n mirror \u00b7 node d x)\n (reverse-\u2237 (mirror \u00b7 t) (map-ListTree mirror mirror-Tree LTts))\n refl\n \u27e9\n mirror \u00b7 node d (reverse (map mirror ts) ++ (mirror \u00b7 t \u2237 []))\n \u2261\u27e8 mirror-eq d (reverse (map mirror ts) ++ (mirror \u00b7 t \u2237 [])) \u27e9\n node d (reverse (map mirror (reverse (map mirror ts) ++ (mirror \u00b7 t \u2237 []))))\n \u2261\u27e8 subst (\u03bb x \u2192 node d (reverse (map mirror (reverse (map mirror ts) ++\n (mirror \u00b7 t \u2237 [])))) \u2261\n node d (reverse x))\n (map-++ mirror\n mirror-Tree\n (rev-ListTree (map-ListTree mirror mirror-Tree LTts) nilLT)\n (consLT (mirror-Tree Tt) nilLT))\n refl\n \u27e9\n node d (reverse ((map mirror (reverse (map mirror ts))) ++\n map mirror (mirror \u00b7 t \u2237 [])))\n \u2261\u27e8 subst (\u03bb x \u2192 node d (reverse ((map mirror (reverse (map mirror ts))) ++\n map mirror (mirror \u00b7 t \u2237 []))) \u2261\n node d x)\n (reverse-++ (map-ListTree mirror\n mirror-Tree\n (rev-ListTree (map-ListTree mirror\n mirror-Tree\n LTts)\n nilLT))\n (map-ListTree mirror\n mirror-Tree\n (consLT (mirror-Tree Tt) nilLT)))\n refl\n \u27e9\n node d (reverse (map mirror (mirror \u00b7 t \u2237 [])) ++\n reverse (map mirror (reverse (map mirror ts))))\n \u2261\u27e8 subst (\u03bb x \u2192 node d (reverse (map mirror (mirror \u00b7 t \u2237 [])) ++\n reverse (map mirror (reverse (map mirror ts)))) \u2261\n node d (reverse x ++\n reverse (map mirror (reverse (map mirror ts)))))\n (map-\u2237 mirror (mirror \u00b7 t) [])\n refl\n \u27e9\n node d (reverse (mirror \u00b7 (mirror \u00b7 t) \u2237 map mirror []) ++\n reverse (map mirror (reverse (map mirror ts))))\n \u2261\u27e8 subst (\u03bb x \u2192 node d (reverse (mirror \u00b7 (mirror \u00b7 t) \u2237 map mirror []) ++\n reverse (map mirror (reverse (map mirror ts)))) \u2261\n node d (reverse (x \u2237 map mirror []) ++\n reverse (map mirror (reverse (map mirror ts)))))\n (mirror\u00b2 Tt) -- IH.\n refl\n \u27e9\n node d (reverse (t \u2237 map mirror []) ++\n reverse (map mirror (reverse (map mirror ts))))\n \u2261\u27e8 subst (\u03bb x \u2192 node d (reverse (t \u2237 map mirror []) ++\n reverse (map mirror (reverse (map mirror ts)))) \u2261\n node d (reverse (t \u2237 x) ++\n reverse (map mirror (reverse (map mirror ts)))))\n (map-[] mirror)\n refl\n \u27e9\n node d (reverse (t \u2237 []) ++\n reverse (map mirror (reverse (map mirror ts))))\n \u2261\u27e8 subst (\u03bb x \u2192 node d (reverse (t \u2237 []) ++\n reverse (map mirror (reverse (map mirror ts)))) \u2261\n node d (x ++\n reverse (map mirror (reverse (map mirror ts)))))\n (reverse-[x]\u2261[x] t)\n refl\n \u27e9\n node d ((t \u2237 []) ++\n reverse (map mirror (reverse (map mirror ts))))\n \u2261\u27e8 subst (\u03bb x \u2192 node d ((t \u2237 []) ++\n reverse (map mirror (reverse (map mirror ts)))) \u2261\n node d ((t \u2237 []) ++ x))\n (prf (mirror\u00b2 {!!}))\n refl\n \u27e9\n node d ((t \u2237 []) ++ ts)\n \u2261\u27e8 subst (\u03bb x \u2192 node d ((t \u2237 []) ++ ts) \u2261 node d x)\n (++-\u2237 t [] ts)\n refl\n \u27e9\n node d (t \u2237 [] ++ ts)\n \u2261\u27e8 subst (\u03bb x \u2192 node d (t \u2237 [] ++ ts) \u2261 node d (t \u2237 x))\n (++-leftIdentity LTts)\n refl\n \u27e9\n node d (t \u2237 ts)\n \u220e\n where\n postulate prf : mirror \u00b7 (mirror \u00b7 ts) \u2261 ts \u2192 -- IH\n rev (map mirror (rev (map mirror ts) [])) [] \u2261 ts\n {-# ATP prove prf #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"e9c4182f96dc269533561617c43f24f501a3411a","subject":"finished the sub lemma","message":"finished the sub lemma\n","repos":"goodlyrottenapple\/lamYcalc","old_file":"Agda\/ITyping.agda","new_file":"Agda\/ITyping.agda","new_contents":"module ITyping where\n\nopen import Data.Empty\nopen import Data.List\nopen import Data.Nat\nopen import Data.Product\nopen import Data.Sum\nopen import Data.List.Any as LAny\nopen LAny.Membership-\u2261\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary.Core\n\n\nopen import Core\nopen import Core-Lemmas\nopen import Typing\nopen import Reduction\n\ndata IType : Set\ndata IType\u1d62 : Set\n\ndata IType where\n o : IType\n _~>_ : IType\u1d62 -> IType\u1d62 -> IType\n\ndata IType\u1d62 where\n \u2229 : List IType -> IType\u1d62\n\n\n\u03c9 = \u2229 []\n\n\u2229' : IType -> IType\u1d62\n\u2229' x = \u2229 (x \u2237 [])\n\n~>-inj-l : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2261 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) -> \u03c4\u2081\u2081 \u2261 \u03c4\u2082\u2081\n~>-inj-l refl = refl\n\n~>-inj-r : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2261 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) -> \u03c4\u2081\u2082 \u2261 \u03c4\u2082\u2082\n~>-inj-r refl = refl\n\n\u2229-inj-cons : \u2200 {x y \u03c4\u1d62 \u03c4\u2c7c} -> \u2229 (x \u2237 \u03c4\u1d62) \u2261 \u2229 (y \u2237 \u03c4\u2c7c) -> \u2229 \u03c4\u1d62 \u2261 \u2229 \u03c4\u2c7c\n\u2229-inj-cons refl = refl\n\n\u2229-inj : \u2200 {x y \u03c4\u1d62 \u03c4\u2c7c} -> \u2229 (x \u2237 \u03c4\u1d62) \u2261 \u2229 (y \u2237 \u03c4\u2c7c) -> x \u2261 y\n\u2229-inj refl = refl\n\n\u2229-\u2237-\u2261 : \u2200 {x y : IType} {xs ys} -> x \u2261 y -> \u2229 xs \u2261 \u2229 ys -> \u2229 (x \u2237 xs) \u2261 \u2229 (y \u2237 ys)\n\u2229-\u2237-\u2261 refl refl = refl\n\n\n_\u225fTI_ : Decidable {A = IType} _\u2261_\n_\u225fTI\u1d62_ : Decidable {A = IType\u1d62} _\u2261_\n\no \u225fTI o = yes refl\no \u225fTI (_ ~> _) = no (\u03bb ())\n(_ ~> _) \u225fTI o = no (\u03bb ())\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) with \u03c4\u2081\u2081 \u225fTI\u1d62 \u03c4\u2082\u2081 | \u03c4\u2081\u2082 \u225fTI\u1d62 \u03c4\u2082\u2082\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (.\u03c4\u2081\u2081 ~> .\u03c4\u2081\u2082) | yes refl | yes refl = yes refl\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (.\u03c4\u2081\u2081 ~> \u03c4\u2082\u2082) | yes refl | no \u03c4\u2081\u2082\u2260\u03c4\u2082\u2082 = no (\u03bb eq \u2192 \u03c4\u2081\u2082\u2260\u03c4\u2082\u2082 (~>-inj-r eq))\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) | no \u03c4\u2081\u2081\u2260\u03c4\u2082\u2081 | _ = no (\u03bb eq \u2192 \u03c4\u2081\u2081\u2260\u03c4\u2082\u2081 (~>-inj-l eq))\n\n\n\u2229 [] \u225fTI\u1d62 \u2229 [] = yes refl\n\u2229 [] \u225fTI\u1d62 \u2229 (x \u2237 \u03c4\u2c7c) = no (\u03bb ())\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI\u1d62 \u2229 [] = no (\u03bb ())\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI\u1d62 \u2229 (y \u2237 \u03c4\u2c7c) with x \u225fTI y | (\u2229 \u03c4\u1d62) \u225fTI\u1d62 (\u2229 \u03c4\u2c7c)\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI\u1d62 \u2229 (.x \u2237 .\u03c4\u1d62) | yes refl | yes refl = yes refl\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI\u1d62 \u2229 (.x \u2237 \u03c4\u2c7c) | yes refl | no \u03c4\u1d62\u2260\u03c4\u2c7c = no (\u03bb \u2229x\u2237\u03c4\u1d62\u2261\u2229x\u2237\u03c4\u2c7c \u2192 \u03c4\u1d62\u2260\u03c4\u2c7c (\u2229-inj-cons \u2229x\u2237\u03c4\u1d62\u2261\u2229x\u2237\u03c4\u2c7c))\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI\u1d62 \u2229 (y \u2237 \u03c4\u2c7c) | no x\u2260y | _ = no (\u03bb \u2229x\u2237\u03c4\u1d62\u2261\u2229y\u2237\u03c4\u2c7c \u2192 x\u2260y (\u2229-inj \u2229x\u2237\u03c4\u1d62\u2261\u2229y\u2237\u03c4\u2c7c))\n\n\n\nICtxt = List (Atom \u00d7 (IType\u1d62 \u00d7 Type))\n\n\ndata _\u2237'_ : IType -> Type -> Set\ndata _\u2237'\u1d62_ : IType\u1d62 -> Type -> Set\n\ndata _\u2237'_ where\n base : o \u2237' \u03c3\n arr : \u2200 {\u03b4 \u03c4 A B} -> \u03b4 \u2237'\u1d62 A -> \u03c4 \u2237'\u1d62 B -> (\u03b4 ~> \u03c4) \u2237' (A \u27f6 B)\n\n\ndata Wf-ICtxt : ICtxt -> Set where\n nil : Wf-ICtxt []\n cons : \u2200 {A \u0393 x \u03c4} ->\n (x\u2209 : x \u2209 dom \u0393) -> \u03c4 \u2237'\u1d62 A -> Wf-ICtxt \u0393 ->\n --------------------------------------------\n Wf-ICtxt ((x , (\u03c4 , A)) \u2237 \u0393)\n\ndata _\u2237'\u1d62_ where\n nil : \u2200 {A} -> \u03c9 \u2237'\u1d62 A\n cons : \u2200 {\u03c4\u1d62 \u03c4 A} -> \u03c4 \u2237' A -> \u2229 \u03c4\u1d62 \u2237'\u1d62 A -> \u2229 (\u03c4 \u2237 \u03c4\u1d62) \u2237'\u1d62 A\n\n\ndata _\u2286[_]_ : IType -> Type -> IType -> Set\ndata _\u2286\u1d62[_]_ : IType\u1d62 -> Type -> IType\u1d62 -> Set\n\ndata _\u2286[_]_ where\n base : o \u2286[ \u03c3 ] o\n arr : \u2200 {A B \u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} ->\n \u03c4\u2082\u2081 \u2286\u1d62[ A ] \u03c4\u2081\u2081 -> \u03c4\u2081\u2082 \u2286\u1d62[ B ] \u03c4\u2082\u2082 -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2237' (A \u27f6 B) -> (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) \u2237' (A \u27f6 B) ->\n -------------------------------------------------------------------------------------------\n (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2286[ A \u27f6 B ] (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082)\n -- \u2286-trans : \u2200 {A \u03c4\u2081 \u03c4\u2082 \u03c4\u2083} ->\n -- \u03c4\u2081 \u2286[ A ] \u03c4\u2082 -> \u03c4\u2082 \u2286[ A ] \u03c4\u2083 ->\n -- -------------------------------\n -- \u03c4\u2081 \u2286[ A ] \u03c4\u2083\n\ndata _\u2286\u1d62[_]_ where\n nil : \u2200 {A \u03c4} ->\n \u03c4 \u2237'\u1d62 A ->\n -----------\n \u03c9 \u2286\u1d62[ A ] \u03c4\n -- \u2229-\u2208 : \u2200 {\u03c4 \u03c4\u1d62} -> \u03c4 \u2208 \u03c4\u1d62 -> (\u2229' \u03c4) \u2286[ A ] (\u2229 \u03c4\u1d62)\n cons : \u2200 {A \u03c4\u1d62 \u03c4' \u03c4'\u1d62} ->\n \u2203(\u03bb \u03c4 -> (\u03c4 \u2208 \u03c4\u1d62) \u00d7 (\u03c4' \u2286[ A ] \u03c4)) -> (\u2229 \u03c4'\u1d62) \u2286\u1d62[ A ] (\u2229 \u03c4\u1d62) ->\n -------------------------------------------------------------\n (\u2229 (\u03c4' \u2237 \u03c4'\u1d62)) \u2286\u1d62[ A ] (\u2229 \u03c4\u1d62)\n\n-- data _\u2264\u2229_ : IType -> IType -> Set where\n-- base : o \u2264\u2229 o\n-- arr : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> \u03c4\u2081\u2082 \u2264\u2229 \u03c4\u2082\u2082 -> \u03c4\u2082\u2081 \u2264\u2229 \u03c4\u2081\u2081 -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2264\u2229 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082)\n-- \u2229-\u2208 : \u2200 {\u03c4 \u03c4\u1d62} -> \u03c4 \u2208 \u03c4\u1d62 -> \u2229 \u03c4\u1d62 \u2264\u2229 \u03c4\n-- \u2229-nil : \u2200 {\u03c4} -> \u03c4 \u2264\u2229 \u03c9\n-- \u2229-cons : \u2200 {\u03c4 \u03c4' \u03c4\u1d62} -> \u03c4 \u2264\u2229 \u03c4' -> \u03c4 \u2264\u2229 \u2229 \u03c4\u1d62 -> \u03c4 \u2264\u2229 \u2229 (\u03c4' \u2237 \u03c4\u1d62)\n-- \u2229-trans : \u2200 {\u03c4\u2081 \u03c4\u2082 \u03c4\u2083} -> \u03c4\u2081 \u2264\u2229 \u03c4\u2082 -> \u03c4\u2082 \u2264\u2229 \u03c4\u2083 -> \u03c4\u2081 \u2264\u2229 \u03c4\u2083\n\n\u2237'\u1d62-\u2208 : \u2200 {A \u03c4 \u03c4\u1d62} -> \u03c4 \u2208 \u03c4\u1d62 -> (\u2229 \u03c4\u1d62) \u2237'\u1d62 A -> \u03c4 \u2237' A\n\u2237'\u1d62-\u2208 {\u03c4\u1d62 = []} () _\n\u2237'\u1d62-\u2208 {\u03c4 = \u03c4} {\u03c4' \u2237 \u03c4\u1d62} \u03c4\u2208\u03c4'\u03c4\u1d62 \u03c4'\u03c4\u1d62\u2237A with \u03c4' \u225fTI \u03c4\n\u2237'\u1d62-\u2208 {A} {\u03c4} {.\u03c4 \u2237 \u03c4\u1d62} \u03c4\u2208\u03c4'\u03c4\u1d62 (cons \u03c4\u2237A \u03c4'\u03c4\u1d62\u2237A) | yes refl = \u03c4\u2237A\n\u2237'\u1d62-\u2208 {A} {\u03c4} {\u03c4' \u2237 \u03c4\u1d62} \u03c4\u2208\u03c4'\u03c4\u1d62 (cons \u03c4'\u2237A \u03c4'\u03c4\u1d62\u2237A) | no \u03c4'\u2260\u03c4 = \u2237'\u1d62-\u2208 (\u2208-\u2237-elim \u03c4 \u03c4\u1d62 \u03c4'\u2260\u03c4 \u03c4\u2208\u03c4'\u03c4\u1d62) \u03c4'\u03c4\u1d62\u2237A\n\n\n\u2286-refl : \u2200 {A \u03c4} -> \u03c4 \u2237' A -> \u03c4 \u2286[ A ] \u03c4\n\u2286\u1d62-refl : \u2200 {A \u03c4} -> \u03c4 \u2237'\u1d62 A -> \u03c4 \u2286\u1d62[ A ] \u03c4\n\u2286\u1d62-\u2286 : \u2200 {A \u03c4\u1d62 \u03c4\u2c7c} -> \u03c4\u1d62 \u2286 \u03c4\u2c7c -> (\u2229 \u03c4\u2c7c) \u2237'\u1d62 A -> \u2229 \u03c4\u1d62 \u2286\u1d62[ A ] \u2229 \u03c4\u2c7c\n\n\u2286-refl {\u03c4 = o} base = base\n\u2286-refl {\u03c4 = \u03c4 ~> \u03c4'} (arr \u03c4\u2237\u1d62A \u03c4'\u2237\u1d62B) =\n arr (\u2286\u1d62-refl \u03c4\u2237\u1d62A) (\u2286\u1d62-refl \u03c4'\u2237\u1d62B) (arr \u03c4\u2237\u1d62A \u03c4'\u2237\u1d62B) (arr \u03c4\u2237\u1d62A \u03c4'\u2237\u1d62B)\n\n\u2286\u1d62-refl {\u03c4 = \u2229 []} nil = nil nil\n\u2286\u1d62-refl {A} {\u2229 (\u03c4 \u2237 \u03c4\u1d62)} \u03c4\u03c4\u1d62\u2237A = \u2286\u1d62-\u2286 (\u03bb {x} z \u2192 z) \u03c4\u03c4\u1d62\u2237A\n\n\u2286\u1d62-\u2286 {\u03c4\u1d62 = []} \u03c4\u1d62\u2286\u03c4\u2c7c \u03c4\u2c7c\u2237A = nil \u03c4\u2c7c\u2237A\n\u2286\u1d62-\u2286 {\u03c4\u1d62 = \u03c4 \u2237 \u03c4\u1d62} \u03c4\u1d62\u2286\u03c4\u2c7c \u03c4\u2c7c\u2237A =\n cons (\u03c4 , (\u03c4\u1d62\u2286\u03c4\u2c7c (here refl)) , \u2286-refl (\u2237'\u1d62-\u2208 (\u03c4\u1d62\u2286\u03c4\u2c7c (here refl)) \u03c4\u2c7c\u2237A)) (\u2286\u1d62-\u2286 (\u03bb {x} z \u2192 \u03c4\u1d62\u2286\u03c4\u2c7c (there z)) \u03c4\u2c7c\u2237A)\n\n\n\u2286\u1d62-\u2208-\u2203 : \u2200 {A \u03c4 \u03c4\u2081 \u03c4\u2082} -> \u2229 \u03c4\u2081 \u2286\u1d62[ A ] \u2229 \u03c4\u2082 -> \u03c4 \u2208 \u03c4\u2081 -> \u2203(\u03bb \u03c4' -> (\u03c4' \u2208 \u03c4\u2082) \u00d7 (\u03c4 \u2286[ A ] \u03c4'))\n\u2286\u1d62-\u2208-\u2203 (cons \u2203\u03c4 \u03c4\u2081\u2286\u03c4\u2082) (here refl) = \u2203\u03c4\n\u2286\u1d62-\u2208-\u2203 (cons _ \u03c4\u2081\u2286\u03c4\u2082) (there \u03c4\u2208\u03c4\u2081) = \u2286\u1d62-\u2208-\u2203 \u03c4\u2081\u2286\u03c4\u2082 \u03c4\u2208\u03c4\u2081\n\n\n\u2286\u1d62-\u03c9-\u22a5 : \u2200 {A \u03c4 \u03c4\u1d62} -> (\u2229 (\u03c4 \u2237 \u03c4\u1d62)) \u2286\u1d62[ A ] \u03c9 -> \u22a5\n\u2286\u1d62-\u03c9-\u22a5 (cons (_ , () , _) _)\n\n\u2286-\u2237'-r : \u2200 {A \u03c4 \u03c4'} -> \u03c4 \u2286[ A ] \u03c4' -> \u03c4' \u2237' A\n\u2286-\u2237'-r base = base\n\u2286-\u2237'-r (arr _ _ _ x) = x\n\n\u2286-\u2237'-l : \u2200 {A \u03c4 \u03c4'} -> \u03c4 \u2286[ A ] \u03c4' -> \u03c4 \u2237' A\n\u2286-\u2237'-l base = base\n\u2286-\u2237'-l (arr _ _ x _) = x\n\n\u2286\u1d62-\u2237'\u1d62-r : \u2200 {A \u03c4\u1d62 \u03c4\u2c7c} -> \u03c4\u1d62 \u2286\u1d62[ A ] \u03c4\u2c7c -> \u03c4\u2c7c \u2237'\u1d62 A\n\u2286\u1d62-\u2237'\u1d62-r {\u03c4\u1d62 = \u2229 []} (nil \u03c4\u2c7c\u2237A) = \u03c4\u2c7c\u2237A\n\u2286\u1d62-\u2237'\u1d62-r {\u03c4\u1d62 = \u2229 (\u03c4 \u2237 \u03c4\u1d62)} (cons x \u03c4\u1d62\u2286\u03c4\u2c7c) = \u2286\u1d62-\u2237'\u1d62-r \u03c4\u1d62\u2286\u03c4\u2c7c\n\n\u2286\u1d62-\u2237'\u1d62-l : \u2200 {A \u03c4\u1d62 \u03c4\u2c7c} -> \u03c4\u1d62 \u2286\u1d62[ A ] \u03c4\u2c7c -> \u03c4\u1d62 \u2237'\u1d62 A\n\u2286\u1d62-\u2237'\u1d62-l (nil x) = nil\n\u2286\u1d62-\u2237'\u1d62-l (cons (_ , _ , \u03c4'\u2286\u03c4) \u03c4\u1d62\u2286\u03c4\u2c7c) = cons (\u2286-\u2237'-l \u03c4'\u2286\u03c4) (\u2286\u1d62-\u2237'\u1d62-l \u03c4\u1d62\u2286\u03c4\u2c7c)\n\n\n\u2286\u1d62-trans : \u2200 {A \u03c4\u1d62 \u03c4\u2c7c \u03c4\u2096} ->\n \u03c4\u1d62 \u2286\u1d62[ A ] \u03c4\u2c7c -> \u03c4\u2c7c \u2286\u1d62[ A ] \u03c4\u2096 ->\n ---------------------------------\n \u03c4\u1d62 \u2286\u1d62[ A ] \u03c4\u2096\n\n\u2286-trans : \u2200 {A \u03c4\u2081 \u03c4\u2082 \u03c4\u2083} ->\n \u03c4\u2081 \u2286[ A ] \u03c4\u2082 -> \u03c4\u2082 \u2286[ A ] \u03c4\u2083 ->\n -------------------------------\n \u03c4\u2081 \u2286[ A ] \u03c4\u2083\n\n\u2286\u1d62-trans {\u03c4\u1d62 = \u2229 []} \u03c4\u1d62\u2286\u03c4\u2c7c \u03c4\u2c7c\u2286\u03c4\u2096 = nil (\u2286\u1d62-\u2237'\u1d62-r \u03c4\u2c7c\u2286\u03c4\u2096)\n\u2286\u1d62-trans {\u03c4\u1d62 = \u2229 (\u03c4' \u2237 \u03c4\u1d62)} \u03c4\u1d62\u2286\u03c4\u2c7c (nil x) = \u22a5-elim (\u2286\u1d62-\u03c9-\u22a5 \u03c4\u1d62\u2286\u03c4\u2c7c)\n\u2286\u1d62-trans {\u03c4\u1d62 = \u2229 (\u03c4 \u2237 \u03c4\u1d62)} {\u2229 \u03c4\u2c7c} {\u2229 \u03c4\u2096} (cons (\u03c4' , \u03c4'\u2208\u03c4\u2c7c , \u03c4\u2286\u03c4') \u03c4\u1d62\u2286\u03c4\u2c7c) \u03c4\u2c7c\u2286\u03c4\u2096 =\n cons (\u03c4'' , (\u03c4''\u2208\u03c4\u2096 , (\u2286-trans \u03c4\u2286\u03c4' \u03c4'\u2286\u03c4''))) (\u2286\u1d62-trans \u03c4\u1d62\u2286\u03c4\u2c7c \u03c4\u2c7c\u2286\u03c4\u2096)\n where\n \u03c4'' = proj\u2081 (\u2286\u1d62-\u2208-\u2203 \u03c4\u2c7c\u2286\u03c4\u2096 \u03c4'\u2208\u03c4\u2c7c)\n \u03c4''\u2208\u03c4\u2096 = proj\u2081 (proj\u2082 (\u2286\u1d62-\u2208-\u2203 \u03c4\u2c7c\u2286\u03c4\u2096 \u03c4'\u2208\u03c4\u2c7c))\n \u03c4'\u2286\u03c4'' = proj\u2082 (proj\u2082 (\u2286\u1d62-\u2208-\u2203 \u03c4\u2c7c\u2286\u03c4\u2096 \u03c4'\u2208\u03c4\u2c7c))\n\n\n-- \u2286\u1d62-trans {\u03c4\u1d62 = \u2229 (\u03c4' \u2237 \u03c4\u1d62)} {\u2229 (_ \u2237 \u03c4\u2c7c)} {\u2229 \u03c4\u2096} (cons (\u03c4 , \u03c4\u2208\u03c4\u2c7c , \u03c4'\u2286\u03c4) \u03c4\u1d62\u2286\u03c4\u2c7c) (cons x \u03c4\u2c7c\u2286\u03c4\u2096) = cons (\u03c4 , ({! !} , {! !})) {! !}\n\n\n-- \u2286\u1d62-trans {\u03c4\u1d62 = \u2229 (\u03c4' \u2237 \u03c4\u1d62)} (cons (\u03c4 , \u03c4\u2208\u03c4\u2c7c , \u03c4'\u2286\u03c4) \u03c4\u1d62\u2286\u03c4\u2c7c) \u03c4\u2c7c\u2286\u03c4\u2096 = {! !}\n\n\u2286-trans base base = base\n\u2286-trans (arr \u03c4\u2082\u2081\u2286\u03c4\u2081\u2081 \u03c4\u2081\u2082\u2286\u03c4\u2082\u2082 \u03c4\u2081\u2081~>\u03c4\u2081\u2082\u2237A\u27f6B _) (arr \u03c4\u2082\u2083\u2286\u03c4\u2082\u2081 \u03c4\u2082\u2082\u2286\u03c4\u2082\u2084 \u03c4\u2082\u2081~>\u03c4\u2082\u2082\u2237A\u27f6B \u03c4\u2082\u2083~>\u03c4\u2082\u2084\u2237A\u27f6B) =\n arr (\u2286\u1d62-trans \u03c4\u2082\u2083\u2286\u03c4\u2082\u2081 \u03c4\u2082\u2081\u2286\u03c4\u2081\u2081) (\u2286\u1d62-trans \u03c4\u2081\u2082\u2286\u03c4\u2082\u2082 \u03c4\u2082\u2082\u2286\u03c4\u2082\u2084) \u03c4\u2081\u2081~>\u03c4\u2081\u2082\u2237A\u27f6B \u03c4\u2082\u2083~>\u03c4\u2082\u2084\u2237A\u27f6B\n\n\n\n\u2286->\u2286\u1d62 : \u2200 {A \u03c4 \u03c4'} -> \u03c4 \u2286[ A ] \u03c4' -> (\u2229' \u03c4) \u2286\u1d62[ A ] (\u2229' \u03c4')\n\u2286->\u2286\u1d62 {\u03c4 = \u03c4} {\u03c4'} \u03c4\u2286\u03c4' = cons (\u03c4' , (here refl , \u03c4\u2286\u03c4')) (nil (cons (\u2286-\u2237'-r \u03c4\u2286\u03c4') nil))\n\ndata \u039b : Type -> Set where\n bv : \u2200 {A} -> (i : \u2115) -> \u039b A\n fv : \u2200 {A} -> (x : Atom) -> \u039b A\n lam : \u2200 {B} -> (A : Type) -> (e : \u039b B) -> \u039b (A \u27f6 B)\n app : \u2200 {A B} -> (e\u2081 : \u039b (A \u27f6 B)) -> (e\u2082 : \u039b A) -> \u039b B\n Y : (t : Type) -> \u039b ((t \u27f6 t) \u27f6 t)\n\n\ndata _~_ : \u2200{t} -> \u039b t -> PTerm -> Set where\n bv : \u2200 {t i} -> (bv {t} i) ~ (bv i)\n fv : \u2200 {t x} -> (fv {t} x) ~ (fv x)\n lam : \u2200 {t s m m'} -> m ~ m' -> (lam {s} t m) ~ (lam m')\n app : \u2200 {t s m n m' n'} -> m ~ m' -> n ~ n' -> (app {t} {s} m n) ~ (app m' n')\n Y : \u2200 {t} -> (Y t) ~ (Y t)\n\n\n\u039b[_<<_] : \u2200 {t} -> \u2115 -> Atom -> \u039b t -> \u039b t\n\u039b[ k << x ] (bv i) = bv i\n\u039b[ k << x ] (fv y) with x \u225f y\n... | yes _ = bv k\n... | no _ = fv y\n\u039b[ k << x ] (lam t m) = lam t (\u039b[ (suc k) << x ] m)\n\u039b[ k << x ] (app t1 t2) = app (\u039b[ k << x ] t1) (\u039b[ k << x ] t2)\n\u039b[ k << x ] (Y t) = Y t\n\n\n\n\u22a2->\u039b : \u2200 {\u0393 m t} -> \u0393 \u22a2 m \u2236 t -> \u039b t\n\u22a2->\u039b {m = bv i} ()\n\u22a2->\u039b {m = fv x} {t} \u0393\u22a2m\u2236t = fv {t} x\n\u22a2->\u039b {m = lam m} (abs {_} {\u03c4\u2081} {\u03c4\u2082} L cf) =\n lam \u03c4\u2081 ( \u039b[ 0 << \u2203fresh (L ++ FV m) ] (\u22a2->\u039b (cf (\u2209-cons-l _ _ (\u2203fresh-spec (L ++ FV m)) ))) )\n\u22a2->\u039b {m = app s t} (app \u0393\u22a2s \u0393\u22a2t) = app (\u22a2->\u039b \u0393\u22a2s) (\u22a2->\u039b \u0393\u22a2t)\n\u22a2->\u039b {m = Y \u03c4} (Y _) = Y \u03c4\n\n-- \u22a2->\u039b\u2261 : \u2200 {\u0393 m n t} -> m \u2261 n -> {\u0393\u22a2m : \u0393 \u22a2 m \u2236 t} -> {\u0393\u22a2n : \u0393 \u22a2 n \u2236 t} -> (\u22a2->\u039b \u0393\u22a2m) \u2261 (\u22a2->\u039b \u0393\u22a2m)\n-- \u22a2->\u039b\u2261 refl = \u03bb {\u0393\u22a2m} {\u0393\u22a2n} \u2192 refl\n\n\n\u039b*^-*^~ : \u2200 {\u03c4 x k} t t' -> _~_ {\u03c4} t t' -> \u039b[ k << x ] t ~ ([ k << x ] t')\n\u039b*^-*^~ _ _ bv = bv\n\u039b*^-*^~ {x = x} (fv y) _ fv with x \u225f y\n\u039b*^-*^~ (fv x) .(fv x) fv | yes _ = bv\n\u039b*^-*^~ (fv y) .(fv y) fv | no _ = fv\n\u039b*^-*^~ _ _ (lam {m = m} {m'} t~t') = lam (\u039b*^-*^~ m m' t~t')\n\u039b*^-*^~ _ _ (app {m = m} {n} {m'} {n'} t~t' t~t'') = app (\u039b*^-*^~ m m' t~t') (\u039b*^-*^~ n n' t~t'')\n\u039b*^-*^~ _ _ Y = Y\n\n\n\u22a2->\u039b~ : \u2200 {\u0393 t \u03c4} -> (\u0393\u22a2t : \u0393 \u22a2 t \u2236 \u03c4) -> (\u22a2->\u039b \u0393\u22a2t) ~ t\n\u22a2->\u039b~ {t = bv i} ()\n\u22a2->\u039b~ {t = fv x} (var _ _) = fv\n\u22a2->\u039b~ {t = lam t} (abs {\u03c4\u2081 = \u03c4\u2081} {\u03c4\u2082} L cf) = lam ih\n where\n x = \u2203fresh (L ++ FV t)\n x\u2209 = \u2203fresh-spec (L ++ FV t)\n x\u2237\u0393\u22a2t^'x = cf (\u2209-cons-l _ _ x\u2209)\n\n sub : \u2200 {\u03c4 x m} -> x \u2209 FV t -> _~_ {\u03c4} m t \u2261 m ~ (* x ^ (t ^' x))\n sub {_} {x} x\u2209 rewrite fv-^-*^-refl x t {0} x\u2209 = refl\n\n ih : \u039b[ 0 << x ] (\u22a2->\u039b x\u2237\u0393\u22a2t^'x) ~ t\n ih rewrite sub {_} {x} {\u039b[ 0 << x ] (\u22a2->\u039b x\u2237\u0393\u22a2t^'x)} (\u2209-cons-r L _ x\u2209) =\n \u039b*^-*^~ (\u22a2->\u039b x\u2237\u0393\u22a2t^'x) (t ^' x) (\u22a2->\u039b~ (cf (\u2209-cons-l _ _ x\u2209)))\n\u22a2->\u039b~ {t = app t t\u2081} (app \u0393\u22a2t \u0393\u22a2t\u2081) = app (\u22a2->\u039b~ \u0393\u22a2t) (\u22a2->\u039b~ \u0393\u22a2t\u2081)\n\u22a2->\u039b~ {t = Y t\u2081} (Y x) = Y\n\n\n\n\u039b[_>>_] : \u2200 {\u03c4 \u03c4'} -> \u2115 -> \u039b \u03c4' -> \u039b \u03c4 -> \u039b \u03c4\n\u039b[_>>_] {\u03c4} {\u03c4'} k u (bv i) with k \u225f i | \u03c4 \u225fT \u03c4'\n\u039b[ k >> u ] (bv i) | yes _ | yes refl = u\n... | yes _ | no _ = bv i\n... | no _ | _ = bv i\n\u039b[ k >> u ] (fv x) = fv x\n\u039b[ k >> u ] (lam A t) = lam A (\u039b[ (suc k) >> u ] t)\n\u039b[ k >> u ] (app t1 t2) = app (\u039b[ k >> u ] t1) (\u039b[ k >> u ] t2)\n\u039b[ k >> u ] (Y t) = Y t\n\n\ndata _\u22a9_\u2236_ : \u2200 {A} -> ICtxt -> \u039b A -> IType -> Set\ndata _\u22a9\u1d62_\u2236_ : \u2200 {A} -> ICtxt -> \u039b A -> IType\u1d62 -> Set\n\n\ndata _\u22a9_\u2236_ where\n var : \u2200 {A \u0393 x \u03c4} {\u03c4\u1d62 : IType\u1d62} ->\n (wf-\u0393 : Wf-ICtxt \u0393) -> (\u03c4\u1d62\u2208\u0393 : (x , (\u03c4\u1d62 , A)) \u2208 \u0393) -> (\u03c4\u2286\u03c4\u1d62 : (\u2229' \u03c4) \u2286\u1d62[ A ] \u03c4\u1d62) ->\n -----------------------------------------------------------------------------------\n \u0393 \u22a9 fv {A} x \u2236 \u03c4\n app : \u2200 {A B \u0393 s t \u03c4 \u03c4\u2081 \u03c4\u2082} ->\n \u0393 \u22a9 s \u2236 (\u03c4\u2081 ~> \u03c4\u2082) -> \u0393 \u22a9\u1d62 t \u2236 \u03c4\u2081 -> (\u2229' \u03c4) \u2286\u1d62[ B ] \u03c4\u2082 -> \u03c4\u2081 \u2237'\u1d62 A ->\n ---------------------------------------------------------------------\n \u0393 \u22a9 (app {A} {B} s t) \u2236 \u03c4\n abs : \u2200 {A B \u0393 \u03c4 \u03c4'} (L : FVars) -> \u2200 {t : \u039b B} ->\n ( cf : \u2200 {x} -> (x\u2209L : x \u2209 L) -> ((x , (\u03c4 , A)) \u2237 \u0393) \u22a9\u1d62 \u039b[ 0 >> fv {A} x ] t \u2236 \u03c4' ) -> (\u03c4 ~> \u03c4') \u2237' (A \u27f6 B) ->\n --------------------------------------------------------------------------------------------------------------\n \u0393 \u22a9 lam A t \u2236 (\u03c4 ~> \u03c4')\n Y : \u2200 {\u0393 A \u03c4 \u03c4\u2081 \u03c4\u2082} ->\n (wf-\u0393 : Wf-ICtxt \u0393) -> \u2203(\u03bb \u03c4' -> (\u03c4' \u2208 \u03c4\u2081) \u00d7 ((\u03c4 ~> \u03c4) \u2286[ A \u27f6 A ] \u03c4')) -> (\u2229 \u03c4\u2081) \u2237'\u1d62 (A \u27f6 A) -> \u03c4\u2082 \u2286\u1d62[ A ] \u03c4 -> -- \u03c4\u2081 \u2286[ (A \u27f6 A) \u27f6 A ]((\u2229' (\u03c4 ~> \u03c4)) ~> \u03c4)\n -----------------------------------------------------------------------------------------\n \u0393 \u22a9 Y A \u2236 ((\u2229 \u03c4\u2081) ~> \u03c4\u2082)\n\ndata _\u22a9\u1d62_\u2236_ where\n nil : \u2200 {A \u0393} {m : \u039b A} ->\n (wf-\u0393 : Wf-ICtxt \u0393) ->\n ----------------------\n \u0393 \u22a9\u1d62 m\u2005 \u2236 \u03c9\n cons : \u2200 {A \u0393 \u03c4 \u03c4\u1d62} {m : \u039b A} ->\n \u0393 \u22a9 m\u2005 \u2236 \u03c4 -> \u0393 \u22a9\u1d62 m\u2005 \u2236 (\u2229 \u03c4\u1d62) ->\n --------------------------------\n \u0393 \u22a9\u1d62 m\u2005 \u2236 (\u2229 (\u03c4 \u2237 \u03c4\u1d62))\n\n\ndata \u039bTerm : \u2200 {\u03c4} -> \u039b \u03c4 -> Set where\n var : \u2200 {A x} -> \u039bTerm (fv {A} x)\n lam : \u2200 {A B} (L : FVars) -> \u2200 {e : \u039b B} ->\n (cf : \u2200 {x} -> (x\u2209L : x \u2209 L) -> \u039bTerm (\u039b[ 0 >> fv {A} x ] e)) -> \u039bTerm (lam A e)\n app : \u2200 {A B} {e\u2081 : \u039b (A \u27f6 B)} {e\u2082 : \u039b A} -> \u039bTerm e\u2081 -> \u039bTerm e\u2082 -> \u039bTerm (app e\u2081 e\u2082)\n Y : \u2200 {t} -> \u039bTerm (Y t)\n\n\n\ndata _\u2286\u0393_ : ICtxt -> ICtxt -> Set where\n nil : \u2200 {\u0393} ->\n (wf-\u0393 : Wf-ICtxt \u0393) ->\n ----------------------\n [] \u2286\u0393 \u0393\n cons : \u2200 {A x \u03c4' \u0393 \u0393'} ->\n \u2203(\u03bb \u03c4 -> ((x , (\u03c4 , A)) \u2208 \u0393') \u00d7 (\u03c4' \u2286\u1d62[ A ] \u03c4)) -> \u0393 \u2286\u0393 \u0393' ->\n ------------------------------------------------------------\n ((x , (\u03c4' , A)) \u2237 \u0393) \u2286\u0393 \u0393'\n\n\n\u2286\u0393-wf\u0393' : \u2200 {\u0393 \u0393'} -> \u0393 \u2286\u0393 \u0393' -> Wf-ICtxt \u0393'\n\u2286\u0393-wf\u0393' (nil wf-\u0393') = wf-\u0393'\n\u2286\u0393-wf\u0393' (cons _ \u0393\u2286\u0393') = \u2286\u0393-wf\u0393' \u0393\u2286\u0393'\n\n\n\n\u22a9-\u2237' : \u2200 {A \u0393} {m : \u039b A} {\u03c4} -> \u0393 \u22a9 m \u2236 \u03c4 -> \u03c4 \u2237' A\n\u22a9\u1d62-\u2237'\u1d62 : \u2200 {A \u0393} {m : \u039b A} {\u03c4} -> \u0393 \u22a9\u1d62 m \u2236 \u03c4 -> \u03c4 \u2237'\u1d62 A\n\n\u22a9-\u2237' (var _ _ \u03c4\u2286\u03c4\u1d62) = \u2237'\u1d62-\u2208 (here refl) (\u2286\u1d62-\u2237'\u1d62-l \u03c4\u2286\u03c4\u1d62)\n\u22a9-\u2237' (app _ _ \u03c4\u2286\u03c4\u1d62 _) = \u2237'\u1d62-\u2208 (here refl) (\u2286\u1d62-\u2237'\u1d62-l \u03c4\u2286\u03c4\u1d62)\n\u22a9-\u2237' (abs _ _ x) = x\n\u22a9-\u2237' (Y _ _ \u03c4\u2081\u2237A\u27f6A \u03c4\u2082\u2286\u03c4) = arr \u03c4\u2081\u2237A\u27f6A (\u2286\u1d62-\u2237'\u1d62-l \u03c4\u2082\u2286\u03c4)\n\n\u22a9\u1d62-\u2237'\u1d62 (nil _) = nil\n\u22a9\u1d62-\u2237'\u1d62 (cons \u0393\u22a9m\u2236\u03c4 \u0393\u22a9\u1d62m\u2236\u03c4\u1d62) = cons (\u22a9-\u2237' \u0393\u22a9m\u2236\u03c4) (\u22a9\u1d62-\u2237'\u1d62 \u0393\u22a9\u1d62m\u2236\u03c4\u1d62)\n\n\n\u22a9\u1d62-\u2208-\u22a9 : \u2200 {A \u0393} {m : \u039b A} {\u03c4 \u03c4\u1d62} -> \u0393 \u22a9\u1d62 m \u2236 \u2229 \u03c4\u1d62 -> \u03c4 \u2208 \u03c4\u1d62 -> \u0393 \u22a9 m \u2236 \u03c4\n\u22a9\u1d62-\u2208-\u22a9 (nil _) ()\n\u22a9\u1d62-\u2208-\u22a9 (cons \u0393\u22a9m\u2236\u03c4 x) (here refl) = \u0393\u22a9m\u2236\u03c4\n\u22a9\u1d62-\u2208-\u22a9 (cons _ \u0393\u22a9\u1d62m\u2236\u03c4\u1d62) (there \u03c4\u2208\u03c4\u1d62) = \u22a9\u1d62-\u2208-\u22a9 \u0393\u22a9\u1d62m\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62\n\n\ndata _->\u039b\u03b2_ : \u2200 {\u03c4} -> \u039b \u03c4 \u219d \u039b \u03c4 where\n redL : \u2200 {A B} {n : \u039b A} {m m' : \u039b (A \u27f6 B)} -> \u039bTerm n -> m ->\u039b\u03b2 m' -> app m n ->\u039b\u03b2 app m' n\n redR : \u2200 {A B} {m : \u039b (A \u27f6 B)} {n n' : \u039b A} -> \u039bTerm m -> n ->\u039b\u03b2 n' -> app m n ->\u039b\u03b2 app m n'\n abs : \u2200 L {A B} {m m' : \u039b B} -> ( \u2200 {x} -> x \u2209 L -> \u039b[ 0 >> fv {A} x ] m ->\u039b\u03b2 \u039b[ 0 >> fv {A} x ] m' ) ->\n lam A m ->\u039b\u03b2 lam A m'\n beta : \u2200 {A B} {m : \u039b (A \u27f6 B)} {n : \u039b A} -> \u039bTerm (lam A m) -> \u039bTerm n -> app (lam A m) n ->\u039b\u03b2 (\u039b[ 0 >> n ] m)\n Y : \u2200 {A} {m : \u039b (A \u27f6 A)} -> \u039bTerm m -> app (Y A) m ->\u039b\u03b2 app m (app (Y A) m)\n\n\n\u2208-\u2286\u0393-trans : \u2200 {A x \u03c4\u1d62} {\u0393 \u0393'} -> (x , (\u03c4\u1d62 , A)) \u2208 \u0393 -> \u0393 \u2286\u0393 \u0393' -> \u2203(\u03bb \u03c4\u1d62' -> ((x , (\u03c4\u1d62' , A)) \u2208 \u0393') \u00d7 \u03c4\u1d62 \u2286\u1d62[ A ] \u03c4\u1d62')\n\u2208-\u2286\u0393-trans (here refl) (cons x _) = x\n\u2208-\u2286\u0393-trans (there x\u2208L) (cons _ L\u2286L') = \u2208-\u2286\u0393-trans x\u2208L L\u2286L'\n\n\n\u2286\u0393-\u2237 : \u2200 {A x \u03c4\u1d62 \u0393 \u0393'} -> x \u2209 dom \u0393' -> \u03c4\u1d62 \u2237'\u1d62 A -> \u0393 \u2286\u0393 \u0393' -> \u0393 \u2286\u0393 ((x , \u03c4\u1d62 , A) \u2237 \u0393')\n\u2286\u0393-\u2237 {\u0393 = []} x\u2209\u0393' \u03c4\u1d62\u2237A \u0393\u2286\u0393' = nil (cons x\u2209\u0393' \u03c4\u1d62\u2237A (\u2286\u0393-wf\u0393' \u0393\u2286\u0393'))\n\u2286\u0393-\u2237 {\u0393 = (x , \u03c4\u1d62 , A) \u2237 \u0393} x\u2209\u0393' \u03c4\u1d62\u2237A (cons (proj\u2081 , proj\u2082 , proj\u2083) \u0393\u2286\u0393') =\n cons (proj\u2081 , ((there proj\u2082) , proj\u2083)) (\u2286\u0393-\u2237 x\u2209\u0393' \u03c4\u1d62\u2237A \u0393\u2286\u0393')\n\n\nsub\u0393 : \u2200 {A \u0393 \u0393'} {m : \u039b A} {\u03c4} -> \u0393 \u22a9 m \u2236 \u03c4 -> \u0393 \u2286\u0393 \u0393' -> \u0393' \u22a9 m \u2236 \u03c4\nsub\u1d62\u0393 : \u2200 {A \u0393 \u0393'} {m : \u039b A} {\u03c4} -> \u0393 \u22a9\u1d62 m \u2236 \u03c4 -> \u0393 \u2286\u0393 \u0393' -> \u0393' \u22a9\u1d62 m \u2236 \u03c4\n\nsub\u0393 (var wf-\u0393 \u03c4\u1d62\u2208\u0393 \u03c4\u2286\u03c4\u1d62) \u0393\u2286\u0393' = var (\u2286\u0393-wf\u0393' \u0393\u2286\u0393') \u03c4\u1d62'\u2208 (\u2286\u1d62-trans \u03c4\u2286\u03c4\u1d62 \u03c4\u1d62\u2286\u03c4\u1d62')\n where\n \u03c4\u1d62'\u2208 = proj\u2081 (proj\u2082 (\u2208-\u2286\u0393-trans \u03c4\u1d62\u2208\u0393 \u0393\u2286\u0393'))\n \u03c4\u1d62\u2286\u03c4\u1d62' = proj\u2082 (proj\u2082 (\u2208-\u2286\u0393-trans \u03c4\u1d62\u2208\u0393 \u0393\u2286\u0393'))\n\nsub\u0393 (app \u0393\u22a9m\u2236\u03c4 x x\u2081 x\u2082) \u0393\u2286\u0393' = app (sub\u0393 \u0393\u22a9m\u2236\u03c4 \u0393\u2286\u0393') (sub\u1d62\u0393 x \u0393\u2286\u0393') x\u2081 x\u2082\nsub\u0393 {\u0393' = \u0393'} (abs {\u03c4 = \u03c4} L cf (arr \u03c4\u2237A \u03c4'\u2237B)) \u0393\u2286\u0393' = abs\n (L ++ dom \u0393')\n (\u03bb x\u2209 \u2192 sub\u1d62\u0393\n (cf (\u2209-cons-l _ _ x\u2209))\n (cons\n (\u03c4 , (here refl) , (\u2286\u1d62-refl \u03c4\u2237A))\n (\u2286\u0393-\u2237 (\u2209-cons-r L _ x\u2209) \u03c4\u2237A \u0393\u2286\u0393')))\n (arr \u03c4\u2237A \u03c4'\u2237B)\nsub\u0393 (Y x x\u2081 x\u2082 x\u2083) \u0393\u2286\u0393' = Y (\u2286\u0393-wf\u0393' \u0393\u2286\u0393') x\u2081 x\u2082 x\u2083\n\nsub\u1d62\u0393 (nil wf-\u0393) \u0393\u2286\u0393' = nil (\u2286\u0393-wf\u0393' \u0393\u2286\u0393')\nsub\u1d62\u0393 (cons x \u0393\u22a9\u1d62m\u2236\u03c4) \u0393\u2286\u0393' = cons (sub\u0393 x \u0393\u2286\u0393') (sub\u1d62\u0393 \u0393\u22a9\u1d62m\u2236\u03c4 \u0393\u2286\u0393')\n\n\n\u2208-\u2286\u1d62-trans : \u2200 {A \u03c4 \u03c4\u1d62 \u03c4\u2c7c} -> \u03c4 \u2208 \u03c4\u1d62 -> (\u2229 \u03c4\u1d62) \u2286\u1d62[ A ] (\u2229 \u03c4\u2c7c) -> \u2203(\u03bb \u03c4' -> (\u03c4' \u2208 \u03c4\u2c7c) \u00d7 \u03c4 \u2286[ A ] \u03c4')\n\u2208-\u2286\u1d62-trans (here refl) (cons x _) = x\n\u2208-\u2286\u1d62-trans (there \u03c4\u2208\u03c4\u1d62) (cons _ \u03c4\u1d62\u2286\u03c4\u2c7c) = \u2208-\u2286\u1d62-trans \u03c4\u2208\u03c4\u1d62 \u03c4\u1d62\u2286\u03c4\u2c7c\n\n\nsub : \u2200 {A \u0393 \u0393'} {m : \u039b A} {\u03c4 \u03c4'} -> \u0393 \u22a9 m \u2236 \u03c4 -> \u03c4' \u2286[ A ] \u03c4 -> \u0393 \u2286\u0393 \u0393' -> \u0393' \u22a9 m \u2236 \u03c4'\nsub\u1d62 : \u2200 {A \u0393 \u0393'} {m : \u039b A} {\u03c4 \u03c4'} -> \u0393 \u22a9\u1d62 m \u2236 \u03c4 -> \u03c4' \u2286\u1d62[ A ] \u03c4 -> \u0393 \u2286\u0393 \u0393' -> \u0393' \u22a9\u1d62 m \u2236 \u03c4'\n\nsub (var wf-\u0393 \u03c4\u1d62\u2208\u0393 \u03c4\u2286\u03c4\u1d62) \u03c4'\u2286\u03c4 \u0393\u2286\u0393' =\n var (\u2286\u0393-wf\u0393' \u0393\u2286\u0393') \u03c4\u1d62'\u2208 (\u2286\u1d62-trans (\u2286\u1d62-trans (\u2286->\u2286\u1d62 \u03c4'\u2286\u03c4) \u03c4\u2286\u03c4\u1d62) \u03c4\u1d62\u2286\u03c4\u1d62')\n where\n \u03c4\u1d62'\u2208 = proj\u2081 (proj\u2082 (\u2208-\u2286\u0393-trans \u03c4\u1d62\u2208\u0393 \u0393\u2286\u0393'))\n \u03c4\u1d62\u2286\u03c4\u1d62' = proj\u2082 (proj\u2082 (\u2208-\u2286\u0393-trans \u03c4\u1d62\u2208\u0393 \u0393\u2286\u0393'))\n\nsub (app \u0393\u22a9s\u2236\u03c4\u2081~>\u03c4\u2082 \u0393\u22a9\u1d62t\u2236\u03c4\u2081 \u03c4\u2286\u03c4\u2082 \u03c4\u2237A) \u03c4'\u2286\u03c4 \u0393\u2286\u0393' = app\n (sub\u0393 \u0393\u22a9s\u2236\u03c4\u2081~>\u03c4\u2082 \u0393\u2286\u0393')\n (sub\u1d62\u0393 \u0393\u22a9\u1d62t\u2236\u03c4\u2081 \u0393\u2286\u0393')\n (\u2286\u1d62-trans (\u2286->\u2286\u1d62 \u03c4'\u2286\u03c4) \u03c4\u2286\u03c4\u2082)\n \u03c4\u2237A\nsub {_} {\u0393} {\u0393'} (abs {\u03c4 = \u03c4} {\u03c4'} L {t} cf \u03c4~>\u03c4'\u2237A\u27f6B) (arr {A} {\u03c4\u2081\u2081 = \u03c4\u2081\u2081} \u03c4\u2286\u03c4\u2081\u2081 \u03c4\u2081\u2082\u2286\u03c4' (arr \u03c4\u2081\u2081\u2237A \u03c4\u2081\u2082\u2237B) x\u2083) \u0393\u2286\u0393' = abs\n (L ++ dom \u0393')\n (\u03bb x\u2209 \u2192 sub\u1d62\n (cf (\u2209-cons-l _ _ x\u2209))\n \u03c4\u2081\u2082\u2286\u03c4'\n (cons (\u03c4\u2081\u2081 , (here refl) , \u03c4\u2286\u03c4\u2081\u2081) (\u2286\u0393-\u2237 (\u2209-cons-r L _ x\u2209) \u03c4\u2081\u2081\u2237A \u0393\u2286\u0393')))\n (arr \u03c4\u2081\u2081\u2237A \u03c4\u2081\u2082\u2237B)\nsub (Y wf-\u0393 (\u03c4' , \u03c4'\u2208\u03c4\u2081 , \u03c4~>\u03c4\u2286\u03c4') \u03c4\u2081\u2237A\u27f6A \u03c4\u2082\u2286\u03c4) (arr {\u03c4\u2081\u2081 = \u2229 \u03c4\u2081'} \u03c4\u2081\u2286\u03c4\u2081' \u03c4\u2082'\u2286\u03c4\u2082 (arr \u2229\u03c4\u2081'\u2237A\u27f6A \u03c4\u2082'\u2237A) x\u2084) \u0393\u2286\u0393' =\n Y ((\u2286\u0393-wf\u0393' \u0393\u2286\u0393')) (\u03c4'' , (\u03c4''\u2208\u03c4\u2081' , \u2286-trans \u03c4~>\u03c4\u2286\u03c4' \u03c4'\u2286\u03c4'')) \u2229\u03c4\u2081'\u2237A\u27f6A (\u2286\u1d62-trans \u03c4\u2082'\u2286\u03c4\u2082 \u03c4\u2082\u2286\u03c4)\n where\n \u03c4'' = proj\u2081 (\u2208-\u2286\u1d62-trans \u03c4'\u2208\u03c4\u2081 \u03c4\u2081\u2286\u03c4\u2081')\n \u03c4''\u2208\u03c4\u2081' = proj\u2081 (proj\u2082 (\u2208-\u2286\u1d62-trans \u03c4'\u2208\u03c4\u2081 \u03c4\u2081\u2286\u03c4\u2081'))\n \u03c4'\u2286\u03c4'' = proj\u2082 (proj\u2082 (\u2208-\u2286\u1d62-trans \u03c4'\u2208\u03c4\u2081 \u03c4\u2081\u2286\u03c4\u2081'))\n\nsub\u1d62 \u0393\u22a9\u1d62m\u2236\u03c4 (nil x) \u0393\u2286\u0393' = nil (\u2286\u0393-wf\u0393' \u0393\u2286\u0393')\nsub\u1d62 \u0393\u22a9\u1d62m\u2236\u03c4\u1d62 (cons (\u03c4 , \u03c4\u2208\u03c4\u1d62 , \u03c4'\u2286\u03c4) \u03c4'\u1d62\u2286\u03c4\u1d62) \u0393\u2286\u0393' with \u22a9\u1d62-\u2208-\u22a9 \u0393\u22a9\u1d62m\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62\n... | \u0393\u22a9m\u2236\u03c4 = cons (sub \u0393\u22a9m\u2236\u03c4 \u03c4'\u2286\u03c4 \u0393\u2286\u0393') (sub\u1d62 \u0393\u22a9\u1d62m\u2236\u03c4\u1d62 \u03c4'\u1d62\u2286\u03c4\u1d62 \u0393\u2286\u0393')\n\n\n\n\n-- \u22a9->\u03b2 : \u2200 {A \u0393} {m m' : \u039b A} {\u03c4} -> \u0393 \u22a9 m' \u2236 \u03c4 -> m ->\u039b\u03b2 m' -> \u0393 \u22a9 m \u2236 \u03c4\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redL x m->\u03b2m') = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redR x m->\u03b2m') = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (abs L x) = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (beta x x\u2081) = {! !}\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c9~>\u03c4\u2082 (nil wf-\u0393) \u03c4\u2286\u03c4\u2082 nil) (Y trm-m) = {! !}\n-- \u22a9->\u03b2 (app \u0393\u22a9m\u2236\u03c4\u2083\u03c4\u1d62~>\u03c4\u2085 (cons _ (app (Y _ \u03c4\u2082\u2286\u03c4\u2081 \u03c4\u2081\u2286\u03c4) (cons _ \u0393\u22a9m\u2236\u03c4~>\u03c4\u2081 (nil wf-\u0393)) \u03c4\u2083\u2286\u03c4\u2082 (cons \u03c4~>\u03c4\u2081\u2237A\u27f6A _)) \u0393\u22a9\u1d62Ym\u2236\u2229\u03c4\u1d62) \u03c4\u2084\u2286\u03c4\u2085 \u03c4\u2083\u03c4\u1d62\u2237A) (Y trm-m) = {! !}\n\n\n\n\n\n\n\n\n\n\n\n\n\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redL x m->\u03b2m') = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redR x m->\u03b2m') = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (abs L x) = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (beta x x\u2081) = {! !}\n-- -- \u22a9->\u03b2 (app \u0393\u22a9m\u2236\u03c4\u2081~>\u03c4 (app (Y wf-\u0393 (arr (arr ::' ::'') ::''') \u03c4\u2264\u03c4\u2081 \u03c4\u2082\u2264\u03c4\u2081) \u0393\u22a9m'\u2236\u03c4\u2082 x\u2084) x\u2085) (Y x\u2086) = {! !}\n-- \u22a9->\u03b2 (app {s = m} {\u03c4\u2082 = \u03c4} \u0393\u22a9m\u2236\u03c4\u2081~>\u03c4 (app {\u03c4\u2082 = \u03c4\u2081} (Y {\u03c4 = \u03c4\u2082} {\u03c4\u2083} wf-\u0393 (arr (arr \u03c4\u2082\u2237A \u03c4\u2083\u2237A) _) \u03c4\u2264\u03c4\u2081 \u03c4\u2082\u2264\u03c4\u2081) \u0393\u22a9m\u2236\u03c4\u2082~>\u03c4\u2083 _) (arr {A = A} \u03c4\u2081\u2237A \u03c4\u2237A)) (Y x\u2086) =\n-- app {A = A \u27f6 A}\n-- (Y {_} {A} {\u2229 (\u03c4\u2082 \u2237 \u03c4\u2081 \u2237 [])} {\u2229 (\u03c4\u2083 \u2237 \u03c4 \u2237 [])} {\u03c4}\n-- wf-\u0393\n-- (arr (arr (\u2229-cons \u03c4\u2082\u2237A (\u2229-cons \u03c4\u2081\u2237A \u2229-nil)) (\u2229-cons \u03c4\u2083\u2237A (\u2229-cons \u03c4\u2237A \u2229-nil))) \u03c4\u2237A)\n-- {! !}\n-- (\u2229-\u2208 (there (here refl))))\n-- {! !}\n-- (arr (arr (\u2229-cons \u03c4\u2082\u2237A (\u2229-cons \u03c4\u2081\u2237A \u2229-nil)) (\u2229-cons \u03c4\u2083\u2237A (\u2229-cons \u03c4\u2237A \u2229-nil))) \u03c4\u2237A)\n--\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4 (\u2229-nil \u00acY-shape wf-\u0393) x) (Y x\u2081) = \u22a5-elim (\u00acY-shape intro\u2081)\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4 (\u2229-cons \u00acY-shape wf-\u0393 \u0393\u22a9m'\u2236\u03c4\u2081 \u0393\u22a9m'\u2236\u03c4\u2082) x) (Y x\u2081) = \u22a5-elim (\u00acY-shape intro\u2081)\n-- \u22a9->\u03b2 (\u2229-nil \u00acY-shape wf-\u0393) (Y x) = \u22a5-elim (\u00acY-shape intro\u2082)\n-- \u22a9->\u03b2 (\u2229-cons \u00acY-shape wf-\u0393 \u0393\u22a9m'\u2236\u03c4 \u0393\u22a9m'\u2236\u03c4\u2081) (Y x) = \u22a5-elim (\u00acY-shape intro\u2082)\n\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redL trm-n m->\u03b2m') = \u22a9->\u03b2-redL \u0393\u22a9m'\u2236\u03c4 m->\u03b2m'\n-- where\n-- \u22a9->\u03b2-redL : \u2200 {A B \u0393} {m m' : \u039b (A \u27f6 B)} {n : \u039b A} {\u03c4} -> \u0393 \u22a9 app m' n \u2236 \u03c4 -> m ->\u039b\u03b2 m' -> \u0393 \u22a9 app m n \u2236 \u03c4\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081 x x\u2081) (redL x\u2082 m->\u039b\u03b2m') =\n-- app (\u22a9->\u03b2-redL \u0393\u22a9m'n\u2236\u03c4 m->\u039b\u03b2m') \u0393\u22a9m'n\u2236\u03c4\u2081 x x\u2081\n-- \u22a9->\u03b2-redL (\u2229-nil \u00acY-shape wf-\u0393) (redL x m->\u039b\u03b2m') = {! !}\n-- \u22a9->\u03b2-redL (\u2229-cons \u00acY-shape wf-\u0393 \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (redL x m->\u039b\u03b2m') = {! !}\n-- \u22a9->\u03b2-redL \u0393\u22a9m'n\u2236\u03c4 (redR x m->\u039b\u03b2m') = {! !}\n-- \u22a9->\u03b2-redL \u0393\u22a9m'n\u2236\u03c4 (abs L x) = {! !}\n-- \u22a9->\u03b2-redL \u0393\u22a9m'n\u2236\u03c4 (beta x x\u2081) = {! !}\n-- \u22a9->\u03b2-redL \u0393\u22a9m'n\u2236\u03c4 (Y x) = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redR trm-m n->\u03b2n') = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (abs L x) = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (beta x x\u2081) = {! !}\n-- \u22a9->\u03b2 (app {s = m} {\u03c4\u2082 = \u03c4} \u0393\u22a9m\u2236\u03c4\u2081~>\u03c4 (app (Y {_} {_} {\u03c4\u2082} {\u03c4\u2083} {\u03c4\u2081} wf-\u0393 \u03c4\u2082\u2237A \u03c4\u2083\u2237A \u03c4\u2081\u2237A \u03c4\u2082\u2264\u2229\u03c4\u2083 \u03c4\u2081\u2264\u2229\u03c4\u2083) \u0393\u22a9m\u2236\u03c4\u2082~>\u03c4\u2083 x \u03c4\u2082~>\u03c4\u2083\u2237A) (arr {A = A} _ \u03c4\u2237A) _) (Y trm-m) =\n-- -- app {A = A \u27f6 A} (Y wf-\u0393 \u03c4\u2081\u2237A \u03c4\u2237A \u03c4\u2237A {! !} {! !}) \u0393\u22a9m\u2236\u03c4\u2081~>\u03c4 (arr (arr \u03c4\u2081\u2237A \u03c4\u2237A) \u03c4\u2237A) (arr \u03c4\u2081\u2237A \u03c4\u2237A)\n-- app {A = A \u27f6 A}\n-- (Y {_} {A} {\u2229 (\u03c4\u2081 \u2237 \u03c4\u2082 \u2237 \u03c4\u2083 \u2237 [])} {\u2229 (\u03c4 \u2237 \u03c4\u2083 \u2237 [])} {\u03c4}\n-- wf-\u0393\n-- {! !}\n-- (\u2229-cons \u03c4\u2237A (\u2229-cons \u03c4\u2083\u2237A \u2229-nil))\n-- \u03c4\u2237A\n-- {! !}\n-- {! !})\n-- {! !}\n-- (arr {! !} {! !})\n-- {! !}\n--\n-- \u22a9->\u03b2 (app \u0393\u22a9m\u2236\u03c4~>\u03c4' (\u2229-nil \u00acY-shape wf-\u0393) x \u03c4\u2237A) (Y trm-m) = \u22a5-elim (\u00acY-shape intro\u2081)\n-- \u22a9->\u03b2 (app \u0393\u22a9m\u2236\u03c4~>\u03c4' (\u2229-cons \u00acY-shape wf-\u0393 \u0393\u22a9Ym\u2236\u03c4' \u0393\u22a9Ym\u2236\u03c4'') x \u03c4\u2237A) (Y trm-m) = \u22a5-elim (\u00acY-shape intro\u2081)\n-- \u22a9->\u03b2 (\u2229-nil \u00acY-shape wf-\u0393) (Y x) = \u22a5-elim (\u00acY-shape intro\u2082)\n-- \u22a9->\u03b2 (\u2229-cons \u00acY-shape wf-\u0393 \u0393\u22a9m'\u2236\u03c4 \u0393\u22a9m'\u2236\u03c4\u2081) (Y x) = \u22a5-elim (\u00acY-shape intro\u2082)\n","old_contents":"module ITyping where\n\nopen import Data.Empty\nopen import Data.List\nopen import Data.Nat\nopen import Data.Product\nopen import Data.Sum\nopen import Data.List.Any as LAny\nopen LAny.Membership-\u2261\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary.Core\n\n\nopen import Core\nopen import Core-Lemmas\nopen import Typing\nopen import Reduction\n\ndata IType : Set\ndata IType\u1d62 : Set\n\ndata IType where\n o : IType\n _~>_ : IType\u1d62 -> IType\u1d62 -> IType\n\ndata IType\u1d62 where\n \u2229 : List IType -> IType\u1d62\n\n\n\u03c9 = \u2229 []\n\n\u2229' : IType -> IType\u1d62\n\u2229' x = \u2229 (x \u2237 [])\n\n~>-inj-l : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2261 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) -> \u03c4\u2081\u2081 \u2261 \u03c4\u2082\u2081\n~>-inj-l refl = refl\n\n~>-inj-r : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2261 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) -> \u03c4\u2081\u2082 \u2261 \u03c4\u2082\u2082\n~>-inj-r refl = refl\n\n\u2229-inj-cons : \u2200 {x y \u03c4\u1d62 \u03c4\u2c7c} -> \u2229 (x \u2237 \u03c4\u1d62) \u2261 \u2229 (y \u2237 \u03c4\u2c7c) -> \u2229 \u03c4\u1d62 \u2261 \u2229 \u03c4\u2c7c\n\u2229-inj-cons refl = refl\n\n\u2229-inj : \u2200 {x y \u03c4\u1d62 \u03c4\u2c7c} -> \u2229 (x \u2237 \u03c4\u1d62) \u2261 \u2229 (y \u2237 \u03c4\u2c7c) -> x \u2261 y\n\u2229-inj refl = refl\n\n\u2229-\u2237-\u2261 : \u2200 {x y : IType} {xs ys} -> x \u2261 y -> \u2229 xs \u2261 \u2229 ys -> \u2229 (x \u2237 xs) \u2261 \u2229 (y \u2237 ys)\n\u2229-\u2237-\u2261 refl refl = refl\n\n\n_\u225fTI_ : Decidable {A = IType} _\u2261_\n_\u225fTI\u1d62_ : Decidable {A = IType\u1d62} _\u2261_\n\no \u225fTI o = yes refl\no \u225fTI (_ ~> _) = no (\u03bb ())\n(_ ~> _) \u225fTI o = no (\u03bb ())\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) with \u03c4\u2081\u2081 \u225fTI\u1d62 \u03c4\u2082\u2081 | \u03c4\u2081\u2082 \u225fTI\u1d62 \u03c4\u2082\u2082\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (.\u03c4\u2081\u2081 ~> .\u03c4\u2081\u2082) | yes refl | yes refl = yes refl\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (.\u03c4\u2081\u2081 ~> \u03c4\u2082\u2082) | yes refl | no \u03c4\u2081\u2082\u2260\u03c4\u2082\u2082 = no (\u03bb eq \u2192 \u03c4\u2081\u2082\u2260\u03c4\u2082\u2082 (~>-inj-r eq))\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) | no \u03c4\u2081\u2081\u2260\u03c4\u2082\u2081 | _ = no (\u03bb eq \u2192 \u03c4\u2081\u2081\u2260\u03c4\u2082\u2081 (~>-inj-l eq))\n\n\n\u2229 [] \u225fTI\u1d62 \u2229 [] = yes refl\n\u2229 [] \u225fTI\u1d62 \u2229 (x \u2237 \u03c4\u2c7c) = no (\u03bb ())\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI\u1d62 \u2229 [] = no (\u03bb ())\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI\u1d62 \u2229 (y \u2237 \u03c4\u2c7c) with x \u225fTI y | (\u2229 \u03c4\u1d62) \u225fTI\u1d62 (\u2229 \u03c4\u2c7c)\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI\u1d62 \u2229 (.x \u2237 .\u03c4\u1d62) | yes refl | yes refl = yes refl\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI\u1d62 \u2229 (.x \u2237 \u03c4\u2c7c) | yes refl | no \u03c4\u1d62\u2260\u03c4\u2c7c = no (\u03bb \u2229x\u2237\u03c4\u1d62\u2261\u2229x\u2237\u03c4\u2c7c \u2192 \u03c4\u1d62\u2260\u03c4\u2c7c (\u2229-inj-cons \u2229x\u2237\u03c4\u1d62\u2261\u2229x\u2237\u03c4\u2c7c))\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI\u1d62 \u2229 (y \u2237 \u03c4\u2c7c) | no x\u2260y | _ = no (\u03bb \u2229x\u2237\u03c4\u1d62\u2261\u2229y\u2237\u03c4\u2c7c \u2192 x\u2260y (\u2229-inj \u2229x\u2237\u03c4\u1d62\u2261\u2229y\u2237\u03c4\u2c7c))\n\n\n\nICtxt = List (Atom \u00d7 IType\u1d62)\n\n\ndata Wf-ICtxt : ICtxt -> Set where\n nil : Wf-ICtxt []\n cons : \u2200 {\u0393 x \u03c4} -> (x\u2209 : x \u2209 dom \u0393) -> Wf-ICtxt \u0393 ->\n Wf-ICtxt ((x , \u03c4) \u2237 \u0393)\n\ndata _\u2237'_ : IType -> Type -> Set\ndata _\u2237'\u1d62_ : IType\u1d62 -> Type -> Set\n\ndata _\u2237'_ where\n base : o \u2237' \u03c3\n arr : \u2200 {\u03b4 \u03c4 A B} -> \u03b4 \u2237'\u1d62 A -> \u03c4 \u2237'\u1d62 B -> (\u03b4 ~> \u03c4) \u2237' (A \u27f6 B)\n\ndata _\u2237'\u1d62_ where\n nil : \u2200 {A} -> \u03c9 \u2237'\u1d62 A\n cons : \u2200 {\u03c4\u1d62 \u03c4 A} -> \u03c4 \u2237' A -> \u2229 \u03c4\u1d62 \u2237'\u1d62 A -> \u2229 (\u03c4 \u2237 \u03c4\u1d62) \u2237'\u1d62 A\n\n\ndata _\u2286[_]_ : IType -> Type -> IType -> Set\ndata _\u2286\u1d62[_]_ : IType\u1d62 -> Type -> IType\u1d62 -> Set\n\ndata _\u2286[_]_ where\n base : o \u2286[ \u03c3 ] o\n arr : \u2200 {A B \u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} ->\n \u03c4\u2082\u2081 \u2286\u1d62[ A ] \u03c4\u2081\u2081 -> \u03c4\u2081\u2082 \u2286\u1d62[ B ] \u03c4\u2082\u2082 -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2237' (A \u27f6 B) -> (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) \u2237' (A \u27f6 B) ->\n -------------------------------------------------------------------------------------------\n (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2286[ A \u27f6 B ] (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082)\n -- \u2286-trans : \u2200 {A \u03c4\u2081 \u03c4\u2082 \u03c4\u2083} ->\n -- \u03c4\u2081 \u2286[ A ] \u03c4\u2082 -> \u03c4\u2082 \u2286[ A ] \u03c4\u2083 ->\n -- -------------------------------\n -- \u03c4\u2081 \u2286[ A ] \u03c4\u2083\n\ndata _\u2286\u1d62[_]_ where\n nil : \u2200 {A \u03c4} ->\n \u03c4 \u2237'\u1d62 A ->\n -----------\n \u03c9 \u2286\u1d62[ A ] \u03c4\n -- \u2229-\u2208 : \u2200 {\u03c4 \u03c4\u1d62} -> \u03c4 \u2208 \u03c4\u1d62 -> (\u2229' \u03c4) \u2286[ A ] (\u2229 \u03c4\u1d62)\n cons : \u2200 {A \u03c4\u1d62 \u03c4' \u03c4'\u1d62} ->\n \u2203(\u03bb \u03c4 -> (\u03c4 \u2208 \u03c4\u1d62) \u00d7 (\u03c4' \u2286[ A ] \u03c4)) -> (\u2229 \u03c4'\u1d62) \u2286\u1d62[ A ] (\u2229 \u03c4\u1d62) ->\n -------------------------------------------------------------\n (\u2229 (\u03c4' \u2237 \u03c4'\u1d62)) \u2286\u1d62[ A ] (\u2229 \u03c4\u1d62)\n\n-- data _\u2264\u2229_ : IType -> IType -> Set where\n-- base : o \u2264\u2229 o\n-- arr : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> \u03c4\u2081\u2082 \u2264\u2229 \u03c4\u2082\u2082 -> \u03c4\u2082\u2081 \u2264\u2229 \u03c4\u2081\u2081 -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2264\u2229 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082)\n-- \u2229-\u2208 : \u2200 {\u03c4 \u03c4\u1d62} -> \u03c4 \u2208 \u03c4\u1d62 -> \u2229 \u03c4\u1d62 \u2264\u2229 \u03c4\n-- \u2229-nil : \u2200 {\u03c4} -> \u03c4 \u2264\u2229 \u03c9\n-- \u2229-cons : \u2200 {\u03c4 \u03c4' \u03c4\u1d62} -> \u03c4 \u2264\u2229 \u03c4' -> \u03c4 \u2264\u2229 \u2229 \u03c4\u1d62 -> \u03c4 \u2264\u2229 \u2229 (\u03c4' \u2237 \u03c4\u1d62)\n-- \u2229-trans : \u2200 {\u03c4\u2081 \u03c4\u2082 \u03c4\u2083} -> \u03c4\u2081 \u2264\u2229 \u03c4\u2082 -> \u03c4\u2082 \u2264\u2229 \u03c4\u2083 -> \u03c4\u2081 \u2264\u2229 \u03c4\u2083\n\n\u2237'\u1d62-\u2208 : \u2200 {A \u03c4 \u03c4\u1d62} -> \u03c4 \u2208 \u03c4\u1d62 -> (\u2229 \u03c4\u1d62) \u2237'\u1d62 A -> \u03c4 \u2237' A\n\u2237'\u1d62-\u2208 {\u03c4\u1d62 = []} () _\n\u2237'\u1d62-\u2208 {\u03c4 = \u03c4} {\u03c4' \u2237 \u03c4\u1d62} \u03c4\u2208\u03c4'\u03c4\u1d62 \u03c4'\u03c4\u1d62\u2237A with \u03c4' \u225fTI \u03c4\n\u2237'\u1d62-\u2208 {A} {\u03c4} {.\u03c4 \u2237 \u03c4\u1d62} \u03c4\u2208\u03c4'\u03c4\u1d62 (cons \u03c4\u2237A \u03c4'\u03c4\u1d62\u2237A) | yes refl = \u03c4\u2237A\n\u2237'\u1d62-\u2208 {A} {\u03c4} {\u03c4' \u2237 \u03c4\u1d62} \u03c4\u2208\u03c4'\u03c4\u1d62 (cons \u03c4'\u2237A \u03c4'\u03c4\u1d62\u2237A) | no \u03c4'\u2260\u03c4 = \u2237'\u1d62-\u2208 (\u2208-\u2237-elim \u03c4 \u03c4\u1d62 \u03c4'\u2260\u03c4 \u03c4\u2208\u03c4'\u03c4\u1d62) \u03c4'\u03c4\u1d62\u2237A\n\n\n\u2286-refl : \u2200 {A \u03c4} -> \u03c4 \u2237' A -> \u03c4 \u2286[ A ] \u03c4\n\u2286\u1d62-refl : \u2200 {A \u03c4} -> \u03c4 \u2237'\u1d62 A -> \u03c4 \u2286\u1d62[ A ] \u03c4\n\u2286\u1d62-\u2286 : \u2200 {A \u03c4\u1d62 \u03c4\u2c7c} -> \u03c4\u1d62 \u2286 \u03c4\u2c7c -> (\u2229 \u03c4\u2c7c) \u2237'\u1d62 A -> \u2229 \u03c4\u1d62 \u2286\u1d62[ A ] \u2229 \u03c4\u2c7c\n\n\u2286-refl {\u03c4 = o} base = base\n\u2286-refl {\u03c4 = \u03c4 ~> \u03c4'} (arr \u03c4\u2237\u1d62A \u03c4'\u2237\u1d62B) =\n arr (\u2286\u1d62-refl \u03c4\u2237\u1d62A) (\u2286\u1d62-refl \u03c4'\u2237\u1d62B) (arr \u03c4\u2237\u1d62A \u03c4'\u2237\u1d62B) (arr \u03c4\u2237\u1d62A \u03c4'\u2237\u1d62B)\n\n\u2286\u1d62-refl {\u03c4 = \u2229 []} nil = nil nil\n\u2286\u1d62-refl {A} {\u2229 (\u03c4 \u2237 \u03c4\u1d62)} \u03c4\u03c4\u1d62\u2237A = \u2286\u1d62-\u2286 (\u03bb {x} z \u2192 z) \u03c4\u03c4\u1d62\u2237A\n\n\u2286\u1d62-\u2286 {\u03c4\u1d62 = []} \u03c4\u1d62\u2286\u03c4\u2c7c \u03c4\u2c7c\u2237A = nil \u03c4\u2c7c\u2237A\n\u2286\u1d62-\u2286 {\u03c4\u1d62 = \u03c4 \u2237 \u03c4\u1d62} \u03c4\u1d62\u2286\u03c4\u2c7c \u03c4\u2c7c\u2237A =\n cons (\u03c4 , (\u03c4\u1d62\u2286\u03c4\u2c7c (here refl)) , \u2286-refl (\u2237'\u1d62-\u2208 (\u03c4\u1d62\u2286\u03c4\u2c7c (here refl)) \u03c4\u2c7c\u2237A)) (\u2286\u1d62-\u2286 (\u03bb {x} z \u2192 \u03c4\u1d62\u2286\u03c4\u2c7c (there z)) \u03c4\u2c7c\u2237A)\n\n\n\u2286\u1d62-\u2208-\u2203 : \u2200 {A \u03c4 \u03c4\u2081 \u03c4\u2082} -> \u2229 \u03c4\u2081 \u2286\u1d62[ A ] \u2229 \u03c4\u2082 -> \u03c4 \u2208 \u03c4\u2081 -> \u2203(\u03bb \u03c4' -> (\u03c4' \u2208 \u03c4\u2082) \u00d7 (\u03c4 \u2286[ A ] \u03c4'))\n\u2286\u1d62-\u2208-\u2203 (cons \u2203\u03c4 \u03c4\u2081\u2286\u03c4\u2082) (here refl) = \u2203\u03c4\n\u2286\u1d62-\u2208-\u2203 (cons _ \u03c4\u2081\u2286\u03c4\u2082) (there \u03c4\u2208\u03c4\u2081) = \u2286\u1d62-\u2208-\u2203 \u03c4\u2081\u2286\u03c4\u2082 \u03c4\u2208\u03c4\u2081\n\n\n\u2286\u1d62-\u03c9-\u22a5 : \u2200 {A \u03c4 \u03c4\u1d62} -> (\u2229 (\u03c4 \u2237 \u03c4\u1d62)) \u2286\u1d62[ A ] \u03c9 -> \u22a5\n\u2286\u1d62-\u03c9-\u22a5 (cons (_ , () , _) _)\n\n\u2286-\u2237'-r : \u2200 {A \u03c4 \u03c4'} -> \u03c4 \u2286[ A ] \u03c4' -> \u03c4' \u2237' A\n\u2286-\u2237'-r base = base\n\u2286-\u2237'-r (arr _ _ _ x) = x\n\n\u2286-\u2237'-l : \u2200 {A \u03c4 \u03c4'} -> \u03c4 \u2286[ A ] \u03c4' -> \u03c4 \u2237' A\n\u2286-\u2237'-l base = base\n\u2286-\u2237'-l (arr _ _ x _) = x\n\n\u2286\u1d62-\u2237'\u1d62-r : \u2200 {A \u03c4\u1d62 \u03c4\u2c7c} -> \u03c4\u1d62 \u2286\u1d62[ A ] \u03c4\u2c7c -> \u03c4\u2c7c \u2237'\u1d62 A\n\u2286\u1d62-\u2237'\u1d62-r {\u03c4\u1d62 = \u2229 []} (nil \u03c4\u2c7c\u2237A) = \u03c4\u2c7c\u2237A\n\u2286\u1d62-\u2237'\u1d62-r {\u03c4\u1d62 = \u2229 (\u03c4 \u2237 \u03c4\u1d62)} (cons x \u03c4\u1d62\u2286\u03c4\u2c7c) = \u2286\u1d62-\u2237'\u1d62-r \u03c4\u1d62\u2286\u03c4\u2c7c\n\n\u2286\u1d62-\u2237'\u1d62-l : \u2200 {A \u03c4\u1d62 \u03c4\u2c7c} -> \u03c4\u1d62 \u2286\u1d62[ A ] \u03c4\u2c7c -> \u03c4\u1d62 \u2237'\u1d62 A\n\u2286\u1d62-\u2237'\u1d62-l (nil x) = nil\n\u2286\u1d62-\u2237'\u1d62-l (cons (_ , _ , \u03c4'\u2286\u03c4) \u03c4\u1d62\u2286\u03c4\u2c7c) = cons (\u2286-\u2237'-l \u03c4'\u2286\u03c4) (\u2286\u1d62-\u2237'\u1d62-l \u03c4\u1d62\u2286\u03c4\u2c7c)\n\n\n\u2286\u1d62-trans : \u2200 {A \u03c4\u1d62 \u03c4\u2c7c \u03c4\u2096} ->\n \u03c4\u1d62 \u2286\u1d62[ A ] \u03c4\u2c7c -> \u03c4\u2c7c \u2286\u1d62[ A ] \u03c4\u2096 ->\n ---------------------------------\n \u03c4\u1d62 \u2286\u1d62[ A ] \u03c4\u2096\n\n\u2286-trans : \u2200 {A \u03c4\u2081 \u03c4\u2082 \u03c4\u2083} ->\n \u03c4\u2081 \u2286[ A ] \u03c4\u2082 -> \u03c4\u2082 \u2286[ A ] \u03c4\u2083 ->\n -------------------------------\n \u03c4\u2081 \u2286[ A ] \u03c4\u2083\n\n\u2286\u1d62-trans {\u03c4\u1d62 = \u2229 []} \u03c4\u1d62\u2286\u03c4\u2c7c \u03c4\u2c7c\u2286\u03c4\u2096 = nil (\u2286\u1d62-\u2237'\u1d62-r \u03c4\u2c7c\u2286\u03c4\u2096)\n\u2286\u1d62-trans {\u03c4\u1d62 = \u2229 (\u03c4' \u2237 \u03c4\u1d62)} \u03c4\u1d62\u2286\u03c4\u2c7c (nil x) = \u22a5-elim (\u2286\u1d62-\u03c9-\u22a5 \u03c4\u1d62\u2286\u03c4\u2c7c)\n\u2286\u1d62-trans {\u03c4\u1d62 = \u2229 (\u03c4 \u2237 \u03c4\u1d62)} {\u2229 \u03c4\u2c7c} {\u2229 \u03c4\u2096} (cons (\u03c4' , \u03c4'\u2208\u03c4\u2c7c , \u03c4\u2286\u03c4') \u03c4\u1d62\u2286\u03c4\u2c7c) \u03c4\u2c7c\u2286\u03c4\u2096 =\n cons (\u03c4'' , (\u03c4''\u2208\u03c4\u2096 , (\u2286-trans \u03c4\u2286\u03c4' \u03c4'\u2286\u03c4''))) (\u2286\u1d62-trans \u03c4\u1d62\u2286\u03c4\u2c7c \u03c4\u2c7c\u2286\u03c4\u2096)\n where\n \u03c4'' = proj\u2081 (\u2286\u1d62-\u2208-\u2203 \u03c4\u2c7c\u2286\u03c4\u2096 \u03c4'\u2208\u03c4\u2c7c)\n \u03c4''\u2208\u03c4\u2096 = proj\u2081 (proj\u2082 (\u2286\u1d62-\u2208-\u2203 \u03c4\u2c7c\u2286\u03c4\u2096 \u03c4'\u2208\u03c4\u2c7c))\n \u03c4'\u2286\u03c4'' = proj\u2082 (proj\u2082 (\u2286\u1d62-\u2208-\u2203 \u03c4\u2c7c\u2286\u03c4\u2096 \u03c4'\u2208\u03c4\u2c7c))\n\n\n-- \u2286\u1d62-trans {\u03c4\u1d62 = \u2229 (\u03c4' \u2237 \u03c4\u1d62)} {\u2229 (_ \u2237 \u03c4\u2c7c)} {\u2229 \u03c4\u2096} (cons (\u03c4 , \u03c4\u2208\u03c4\u2c7c , \u03c4'\u2286\u03c4) \u03c4\u1d62\u2286\u03c4\u2c7c) (cons x \u03c4\u2c7c\u2286\u03c4\u2096) = cons (\u03c4 , ({! !} , {! !})) {! !}\n\n\n-- \u2286\u1d62-trans {\u03c4\u1d62 = \u2229 (\u03c4' \u2237 \u03c4\u1d62)} (cons (\u03c4 , \u03c4\u2208\u03c4\u2c7c , \u03c4'\u2286\u03c4) \u03c4\u1d62\u2286\u03c4\u2c7c) \u03c4\u2c7c\u2286\u03c4\u2096 = {! !}\n\n\u2286-trans base base = base\n\u2286-trans (arr \u03c4\u2082\u2081\u2286\u03c4\u2081\u2081 \u03c4\u2081\u2082\u2286\u03c4\u2082\u2082 \u03c4\u2081\u2081~>\u03c4\u2081\u2082\u2237A\u27f6B _) (arr \u03c4\u2082\u2083\u2286\u03c4\u2082\u2081 \u03c4\u2082\u2082\u2286\u03c4\u2082\u2084 \u03c4\u2082\u2081~>\u03c4\u2082\u2082\u2237A\u27f6B \u03c4\u2082\u2083~>\u03c4\u2082\u2084\u2237A\u27f6B) =\n arr (\u2286\u1d62-trans \u03c4\u2082\u2083\u2286\u03c4\u2082\u2081 \u03c4\u2082\u2081\u2286\u03c4\u2081\u2081) (\u2286\u1d62-trans \u03c4\u2081\u2082\u2286\u03c4\u2082\u2082 \u03c4\u2082\u2082\u2286\u03c4\u2082\u2084) \u03c4\u2081\u2081~>\u03c4\u2081\u2082\u2237A\u27f6B \u03c4\u2082\u2083~>\u03c4\u2082\u2084\u2237A\u27f6B\n\n\n\n\u2286->\u2286\u1d62 : \u2200 {A \u03c4 \u03c4'} -> \u03c4 \u2286[ A ] \u03c4' -> (\u2229' \u03c4) \u2286\u1d62[ A ] (\u2229' \u03c4')\n\u2286->\u2286\u1d62 {\u03c4 = \u03c4} {\u03c4'} \u03c4\u2286\u03c4' = cons (\u03c4' , (here refl , \u03c4\u2286\u03c4')) (nil (cons (\u2286-\u2237'-r \u03c4\u2286\u03c4') nil))\n\ndata \u039b : Type -> Set where\n bv : \u2200 {A} -> (i : \u2115) -> \u039b A\n fv : \u2200 {A} -> (x : Atom) -> \u039b A\n lam : \u2200 {B} -> (A : Type) -> (e : \u039b B) -> \u039b (A \u27f6 B)\n app : \u2200 {A B} -> (e\u2081 : \u039b (A \u27f6 B)) -> (e\u2082 : \u039b A) -> \u039b B\n Y : (t : Type) -> \u039b ((t \u27f6 t) \u27f6 t)\n\n\ndata _~_ : \u2200{t} -> \u039b t -> PTerm -> Set where\n bv : \u2200 {t i} -> (bv {t} i) ~ (bv i)\n fv : \u2200 {t x} -> (fv {t} x) ~ (fv x)\n lam : \u2200 {t s m m'} -> m ~ m' -> (lam {s} t m) ~ (lam m')\n app : \u2200 {t s m n m' n'} -> m ~ m' -> n ~ n' -> (app {t} {s} m n) ~ (app m' n')\n Y : \u2200 {t} -> (Y t) ~ (Y t)\n\n\n\u039b[_<<_] : \u2200 {t} -> \u2115 -> Atom -> \u039b t -> \u039b t\n\u039b[ k << x ] (bv i) = bv i\n\u039b[ k << x ] (fv y) with x \u225f y\n... | yes _ = bv k\n... | no _ = fv y\n\u039b[ k << x ] (lam t m) = lam t (\u039b[ (suc k) << x ] m)\n\u039b[ k << x ] (app t1 t2) = app (\u039b[ k << x ] t1) (\u039b[ k << x ] t2)\n\u039b[ k << x ] (Y t) = Y t\n\n\n\n\u22a2->\u039b : \u2200 {\u0393 m t} -> \u0393 \u22a2 m \u2236 t -> \u039b t\n\u22a2->\u039b {m = bv i} ()\n\u22a2->\u039b {m = fv x} {t} \u0393\u22a2m\u2236t = fv {t} x\n\u22a2->\u039b {m = lam m} (abs {_} {\u03c4\u2081} {\u03c4\u2082} L cf) =\n lam \u03c4\u2081 ( \u039b[ 0 << \u2203fresh (L ++ FV m) ] (\u22a2->\u039b (cf (\u2209-cons-l _ _ (\u2203fresh-spec (L ++ FV m)) ))) )\n\u22a2->\u039b {m = app s t} (app \u0393\u22a2s \u0393\u22a2t) = app (\u22a2->\u039b \u0393\u22a2s) (\u22a2->\u039b \u0393\u22a2t)\n\u22a2->\u039b {m = Y \u03c4} (Y _) = Y \u03c4\n\n-- \u22a2->\u039b\u2261 : \u2200 {\u0393 m n t} -> m \u2261 n -> {\u0393\u22a2m : \u0393 \u22a2 m \u2236 t} -> {\u0393\u22a2n : \u0393 \u22a2 n \u2236 t} -> (\u22a2->\u039b \u0393\u22a2m) \u2261 (\u22a2->\u039b \u0393\u22a2m)\n-- \u22a2->\u039b\u2261 refl = \u03bb {\u0393\u22a2m} {\u0393\u22a2n} \u2192 refl\n\n\n\u039b*^-*^~ : \u2200 {\u03c4 x k} t t' -> _~_ {\u03c4} t t' -> \u039b[ k << x ] t ~ ([ k << x ] t')\n\u039b*^-*^~ _ _ bv = bv\n\u039b*^-*^~ {x = x} (fv y) _ fv with x \u225f y\n\u039b*^-*^~ (fv x) .(fv x) fv | yes _ = bv\n\u039b*^-*^~ (fv y) .(fv y) fv | no _ = fv\n\u039b*^-*^~ _ _ (lam {m = m} {m'} t~t') = lam (\u039b*^-*^~ m m' t~t')\n\u039b*^-*^~ _ _ (app {m = m} {n} {m'} {n'} t~t' t~t'') = app (\u039b*^-*^~ m m' t~t') (\u039b*^-*^~ n n' t~t'')\n\u039b*^-*^~ _ _ Y = Y\n\n\n\u22a2->\u039b~ : \u2200 {\u0393 t \u03c4} -> (\u0393\u22a2t : \u0393 \u22a2 t \u2236 \u03c4) -> (\u22a2->\u039b \u0393\u22a2t) ~ t\n\u22a2->\u039b~ {t = bv i} ()\n\u22a2->\u039b~ {t = fv x} (var _ _) = fv\n\u22a2->\u039b~ {t = lam t} (abs {\u03c4\u2081 = \u03c4\u2081} {\u03c4\u2082} L cf) = lam ih\n where\n x = \u2203fresh (L ++ FV t)\n x\u2209 = \u2203fresh-spec (L ++ FV t)\n x\u2237\u0393\u22a2t^'x = cf (\u2209-cons-l _ _ x\u2209)\n\n sub : \u2200 {\u03c4 x m} -> x \u2209 FV t -> _~_ {\u03c4} m t \u2261 m ~ (* x ^ (t ^' x))\n sub {_} {x} x\u2209 rewrite fv-^-*^-refl x t {0} x\u2209 = refl\n\n ih : \u039b[ 0 << x ] (\u22a2->\u039b x\u2237\u0393\u22a2t^'x) ~ t\n ih rewrite sub {_} {x} {\u039b[ 0 << x ] (\u22a2->\u039b x\u2237\u0393\u22a2t^'x)} (\u2209-cons-r L _ x\u2209) =\n \u039b*^-*^~ (\u22a2->\u039b x\u2237\u0393\u22a2t^'x) (t ^' x) (\u22a2->\u039b~ (cf (\u2209-cons-l _ _ x\u2209)))\n\u22a2->\u039b~ {t = app t t\u2081} (app \u0393\u22a2t \u0393\u22a2t\u2081) = app (\u22a2->\u039b~ \u0393\u22a2t) (\u22a2->\u039b~ \u0393\u22a2t\u2081)\n\u22a2->\u039b~ {t = Y t\u2081} (Y x) = Y\n\n\n\n\u039b[_>>_] : \u2200 {\u03c4 \u03c4'} -> \u2115 -> \u039b \u03c4' -> \u039b \u03c4 -> \u039b \u03c4\n\u039b[_>>_] {\u03c4} {\u03c4'} k u (bv i) with k \u225f i | \u03c4 \u225fT \u03c4'\n\u039b[ k >> u ] (bv i) | yes _ | yes refl = u\n... | yes _ | no _ = bv i\n... | no _ | _ = bv i\n\u039b[ k >> u ] (fv x) = fv x\n\u039b[ k >> u ] (lam A t) = lam A (\u039b[ (suc k) >> u ] t)\n\u039b[ k >> u ] (app t1 t2) = app (\u039b[ k >> u ] t1) (\u039b[ k >> u ] t2)\n\u039b[ k >> u ] (Y t) = Y t\n\n\n\n-- ~>' : IType\u1d62 \u00d7 IType\u1d62 -> IType\n-- ~>' (a , b) = a ~> b\n--\n-- \u2237-++-\u2261 : \u2200 {a} {A : Set a} {x : A} {xs ys : List A} -> (x \u2237 xs) ++ ys \u2261 x \u2237 (xs ++ ys)\n-- \u2237-++-\u2261 {a} {A} {x} {xs} {ys} = refl\n--\n--\n-- \u2237\u1d62-++ : \u2200 {A \u03c4\u1d62 \u03c4\u2c7c} -> (\u2229 \u03c4\u1d62) \u2237'\u1d62 A -> (\u2229 \u03c4\u2c7c) \u2237'\u1d62 A -> \u2229 (\u03c4\u1d62 ++ \u03c4\u2c7c) \u2237'\u1d62 A\n-- \u2237\u1d62-++ nil \u03c4\u2c7c\u2237A = \u03c4\u2c7c\u2237A\n-- \u2237\u1d62-++ (cons x \u03c4\u1d62\u2237A) \u03c4\u2c7c\u2237A = cons x (\u2237\u1d62-++ \u03c4\u1d62\u2237A \u03c4\u2c7c\u2237A)\n--\n-- \u2286\u1d62-++ : \u2200 {A \u03c4 \u03c4\u1d62 \u03c4\u2c7c} -> \u03c4 \u2286\u1d62[ A ] (\u2229 \u03c4\u1d62) -> \u03c4 \u2286\u1d62[ A ] (\u2229 \u03c4\u2c7c) -> \u03c4 \u2286\u1d62[ A ] \u2229 (\u03c4\u1d62 ++ \u03c4\u2c7c)\n-- \u2286\u1d62-++ {\u03c4 = \u2229 []} \u03c4\u2286\u03c4\u1d62 \u03c4\u2286\u03c4\u2c7c = nil (\u2237\u1d62-++ (\u2286\u1d62-\u2237'\u1d62-r \u03c4\u2286\u03c4\u1d62) (\u2286\u1d62-\u2237'\u1d62-r \u03c4\u2286\u03c4\u2c7c))\n-- \u2286\u1d62-++ {\u03c4 = \u2229 (x \u2237 x\u1d62)} (cons (\u03c4 , \u03c4\u2208\u03c4\u1d62 , x\u2286\u03c4) x\u2081\u2286\u03c4\u1d62) (cons \u2203\u03c4\u2082 x\u2081\u2286\u03c4\u2c7c) =\n-- cons (\u03c4 , ((\u2208-cons-l _ \u03c4\u2208\u03c4\u1d62) , x\u2286\u03c4)) (\u2286\u1d62-++ {\u03c4 = \u2229 x\u1d62} x\u2081\u2286\u03c4\u1d62 x\u2081\u2286\u03c4\u2c7c)\n--\n-- concat\u1d62 : List (IType\u1d62) -> IType\u1d62\n-- concat\u1d62 [] = \u03c9\n-- concat\u1d62 ((\u2229 x) \u2237 xs) with concat\u1d62 xs\n-- ... | \u2229 xs' = \u2229 (x ++ xs')\n--\n-- \u2229\u2081 : List (IType\u1d62 \u00d7 IType\u1d62) -> IType\u1d62\n-- \u2229\u2081 xs\u00d7ys = concat\u1d62 (Data.List.map proj\u2081 xs\u00d7ys)\n--\n-- \u2229\u2082 : List (IType\u1d62 \u00d7 IType\u1d62) -> IType\u1d62\n-- \u2229\u2082 xs\u00d7ys = concat\u1d62 (Data.List.map proj\u2082 xs\u00d7ys)\n--\n--\n-- inv-\u2286\u1d62-\u2229~>-r : \u2200 {A B \u03c4} -> \u03c4 \u2237'\u1d62 (A \u27f6 B) ->\n-- \u2203(\u03bb (\u03c4\u1d62\u00d7\u03c4\u2c7c : List (IType\u1d62 \u00d7 IType\u1d62)) -> \u03c4 \u2261 \u2229 (Data.List.map ~>' \u03c4\u1d62\u00d7\u03c4\u2c7c))\n-- inv-\u2286\u1d62-\u2229~>-r nil = [] , refl\n-- inv-\u2286\u1d62-\u2229~>-r (cons {\u03c4\u1d62 = \u03c4\u1d62} (arr {\u03b4 = \u03c4} {\u03c4'} _ _) \u03c4\u1d62\u2237A\u27f6B) = ((\u03c4 , \u03c4') \u2237 \u03c4\u1d62\u00d7\u03c4\u2c7c) , \u2229-\u2237-\u2261 refl \u03c4\u1d62\u2261\u2229\u03c4\u1d62\u00d7\u03c4\u2c7c\n-- where\n-- ih : \u2203 (\u03bb \u03c4\u1d62\u00d7\u03c4\u2c7c \u2192 \u2229 \u03c4\u1d62 \u2261 \u2229 (Data.List.map ~>' \u03c4\u1d62\u00d7\u03c4\u2c7c))\n-- ih = inv-\u2286\u1d62-\u2229~>-r \u03c4\u1d62\u2237A\u27f6B\n--\n-- \u03c4\u1d62\u00d7\u03c4\u2c7c = proj\u2081 ih\n-- \u03c4\u1d62\u2261\u2229\u03c4\u1d62\u00d7\u03c4\u2c7c = proj\u2082 ih\n\n\n\ndata _\u22a9_\u2236_ : \u2200 {A} -> ICtxt -> \u039b A -> IType -> Set\ndata _\u22a9\u1d62_\u2236_ : \u2200 {A} -> ICtxt -> \u039b A -> IType\u1d62 -> Set\n\n\ndata _\u22a9_\u2236_ where\n var : \u2200 {A \u0393 x \u03c4} {\u03c4\u1d62 : IType\u1d62} ->\n (wf-\u0393 : Wf-ICtxt \u0393) -> (\u03c4\u1d62\u2208\u0393 : (x , \u03c4\u1d62) \u2208 \u0393) -> (\u03c4\u2286\u03c4\u1d62 : (\u2229' \u03c4) \u2286\u1d62[ A ] \u03c4\u1d62) ->\n -----------------------------------------------------------------------------\n \u0393 \u22a9 fv {A} x \u2236 \u03c4\n app : \u2200 {A B \u0393 s t \u03c4 \u03c4\u2081 \u03c4\u2082} ->\n \u0393 \u22a9 s \u2236 (\u03c4\u2081 ~> \u03c4\u2082) -> \u0393 \u22a9\u1d62 t \u2236 \u03c4\u2081 -> (\u2229' \u03c4) \u2286\u1d62[ B ] \u03c4\u2082 -> \u03c4\u2081 \u2237'\u1d62 A ->\n ---------------------------------------------------------------------\n \u0393 \u22a9 (app {A} {B} s t) \u2236 \u03c4\n abs : \u2200 {A B \u0393 \u03c4 \u03c4'} (L : FVars) -> \u2200 {t : \u039b B} ->\n ( cf : \u2200 {x} -> (x\u2209L : x \u2209 L) -> ((x , \u03c4) \u2237 \u0393) \u22a9\u1d62 \u039b[ 0 >> fv {A} x ] t \u2236 \u03c4' ) -> (\u03c4 ~> \u03c4') \u2237' (A \u27f6 B) ->\n ---------------------------------------------------------------------------------------------------------\n \u0393 \u22a9 lam A t \u2236 (\u03c4 ~> \u03c4')\n -- Y : \u2200 {\u0393 A \u03c4\u2082} {\u03c4\u00d7\u03c4\u2081 : List (IType\u1d62 \u00d7 IType\u1d62)} ->\n -- Wf-ICtxt \u0393 -> \u03c4\u2082 \u2286\u1d62[ A ] (\u2229\u2082 \u03c4\u00d7\u03c4\u2081) -> (\u2229\u2082 \u03c4\u00d7\u03c4\u2081) \u2286\u1d62[ A ] (\u2229\u2081 \u03c4\u00d7\u03c4\u2081) ->\n -- --------------------------------------------------------------------\n -- \u0393 \u22a9 Y A \u2236 ((\u2229 (Data.List.map ~>' \u03c4\u00d7\u03c4\u2081)) ~> \u03c4\u2082)\n\n Y : \u2200 {\u0393 A \u03c4 \u03c4\u2081 \u03c4\u2082 \u03c4\u2083} ->\n Wf-ICtxt \u0393 -> \u03c4\u2082 \u2286\u1d62[ A ] \u03c4\u2081 -> \u03c4 \u2286\u1d62[ A ] \u03c4\u2081 -> \u03c4\u2083 \u2286[ (A \u27f6 A) \u27f6 A ]((\u2229' (\u03c4 ~> \u03c4\u2081)) ~> \u03c4\u2082) ->\n -------------------------------------------------------------------------------------------\n \u0393 \u22a9 Y A \u2236 \u03c4\u2083\n\ndata _\u22a9\u1d62_\u2236_ where\n nil : \u2200 {A \u0393} {m : \u039b A} ->\n (wf-\u0393 : Wf-ICtxt \u0393) ->\n ----------------------\n \u0393 \u22a9\u1d62 m\u2005 \u2236 \u03c9\n\n cons : \u2200 {A \u0393 \u03c4 \u03c4\u1d62} {m : \u039b A} ->\n \u0393 \u22a9 m\u2005 \u2236 \u03c4 -> \u0393 \u22a9\u1d62 m\u2005 \u2236 (\u2229 \u03c4\u1d62) ->\n --------------------------------\n \u0393 \u22a9\u1d62 m\u2005 \u2236 (\u2229 (\u03c4 \u2237 \u03c4\u1d62))\n\n\ndata \u039bTerm : \u2200 {\u03c4} -> \u039b \u03c4 -> Set where\n var : \u2200 {A x} -> \u039bTerm (fv {A} x)\n lam : \u2200 {A B} (L : FVars) -> \u2200 {e : \u039b B} ->\n (cf : \u2200 {x} -> (x\u2209L : x \u2209 L) -> \u039bTerm (\u039b[ 0 >> fv {A} x ] e)) -> \u039bTerm (lam A e)\n app : \u2200 {A B} {e\u2081 : \u039b (A \u27f6 B)} {e\u2082 : \u039b A} -> \u039bTerm e\u2081 -> \u039bTerm e\u2082 -> \u039bTerm (app e\u2081 e\u2082)\n Y : \u2200 {t} -> \u039bTerm (Y t)\n\n\n\ndata _\u2286\u0393_ : ICtxt -> ICtxt -> Set where\n nil : \u2200 {\u0393} ->\n (wf-\u0393 : Wf-ICtxt \u0393) ->\n ----------------------\n [] \u2286\u0393 \u0393\n cons : \u2200 {x \u03c4' \u0393 \u0393'} ->\n \u2203(\u03bb \u03c4A -> ((x , (proj\u2081 \u03c4A)) \u2208 \u0393') \u00d7 (\u03c4' \u2286\u1d62[ (proj\u2082 \u03c4A) ] (proj\u2081 \u03c4A))) -> \u0393 \u2286\u0393 \u0393' ->\n -----------------------------------------------------------------------------------\n ((x , \u03c4') \u2237 \u0393) \u2286\u0393 \u0393'\n\n\n\u2286\u0393-wf\u0393' : \u2200 {\u0393 \u0393'} -> \u0393 \u2286\u0393 \u0393' -> Wf-ICtxt \u0393'\n\u2286\u0393-wf\u0393' (nil wf-\u0393') = wf-\u0393'\n\u2286\u0393-wf\u0393' (cons _ \u0393\u2286\u0393') = \u2286\u0393-wf\u0393' \u0393\u2286\u0393'\n\n\u22a9-\u2237' : \u2200 {A \u0393} {m : \u039b A} {\u03c4} -> \u0393 \u22a9 m \u2236 \u03c4 -> \u03c4 \u2237' A\n\u22a9\u1d62-\u2237'\u1d62 : \u2200 {A \u0393} {m : \u039b A} {\u03c4} -> \u0393 \u22a9\u1d62 m \u2236 \u03c4 -> \u03c4 \u2237'\u1d62 A\n\n\u22a9-\u2237' (var _ _ \u03c4\u2286\u03c4\u1d62) = \u2237'\u1d62-\u2208 (here refl) (\u2286\u1d62-\u2237'\u1d62-l \u03c4\u2286\u03c4\u1d62)\n\u22a9-\u2237' (app _ _ \u03c4\u2286\u03c4\u1d62 _) = \u2237'\u1d62-\u2208 (here refl) (\u2286\u1d62-\u2237'\u1d62-l \u03c4\u2286\u03c4\u1d62)\n\u22a9-\u2237' (abs _ _ x) = x\n\u22a9-\u2237' (Y _ _ _ x) = \u2286-\u2237'-l x\n\n\u22a9\u1d62-\u2237'\u1d62 (nil _) = nil\n\u22a9\u1d62-\u2237'\u1d62 (cons \u0393\u22a9m\u2236\u03c4 \u0393\u22a9\u1d62m\u2236\u03c4\u1d62) = cons (\u22a9-\u2237' \u0393\u22a9m\u2236\u03c4) (\u22a9\u1d62-\u2237'\u1d62 \u0393\u22a9\u1d62m\u2236\u03c4\u1d62)\n\n\n\u22a9\u1d62-\u2208-\u22a9 : \u2200 {A \u0393} {m : \u039b A} {\u03c4 \u03c4\u1d62} -> \u0393 \u22a9\u1d62 m \u2236 \u2229 \u03c4\u1d62 -> \u03c4 \u2208 \u03c4\u1d62 -> \u0393 \u22a9 m \u2236 \u03c4\n\u22a9\u1d62-\u2208-\u22a9 (nil _) ()\n\u22a9\u1d62-\u2208-\u22a9 (cons \u0393\u22a9m\u2236\u03c4 x) (here refl) = \u0393\u22a9m\u2236\u03c4\n\u22a9\u1d62-\u2208-\u22a9 (cons _ \u0393\u22a9\u1d62m\u2236\u03c4\u1d62) (there \u03c4\u2208\u03c4\u1d62) = \u22a9\u1d62-\u2208-\u22a9 \u0393\u22a9\u1d62m\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62\n\n\ndata _->\u039b\u03b2_ : \u2200 {\u03c4} -> \u039b \u03c4 \u219d \u039b \u03c4 where\n redL : \u2200 {A B} {n : \u039b A} {m m' : \u039b (A \u27f6 B)} -> \u039bTerm n -> m ->\u039b\u03b2 m' -> app m n ->\u039b\u03b2 app m' n\n redR : \u2200 {A B} {m : \u039b (A \u27f6 B)} {n n' : \u039b A} -> \u039bTerm m -> n ->\u039b\u03b2 n' -> app m n ->\u039b\u03b2 app m n'\n abs : \u2200 L {A B} {m m' : \u039b B} -> ( \u2200 {x} -> x \u2209 L -> \u039b[ 0 >> fv {A} x ] m ->\u039b\u03b2 \u039b[ 0 >> fv {A} x ] m' ) ->\n lam A m ->\u039b\u03b2 lam A m'\n beta : \u2200 {A B} {m : \u039b (A \u27f6 B)} {n : \u039b A} -> \u039bTerm (lam A m) -> \u039bTerm n -> app (lam A m) n ->\u039b\u03b2 (\u039b[ 0 >> n ] m)\n Y : \u2200 {A} {m : \u039b (A \u27f6 A)} -> \u039bTerm m -> app (Y A) m ->\u039b\u03b2 app m (app (Y A) m)\n\n\nsub\u0393 : \u2200 {A \u0393 \u0393'} {m : \u039b A} {\u03c4} -> \u0393 \u22a9 m \u2236 \u03c4 -> \u0393 \u2286\u0393 \u0393' -> \u0393' \u22a9 m \u2236 \u03c4\nsub\u1d62\u0393 : \u2200 {A \u0393 \u0393'} {m : \u039b A} {\u03c4} -> \u0393 \u22a9\u1d62 m \u2236 \u03c4 -> \u0393 \u2286\u0393 \u0393' -> \u0393' \u22a9\u1d62 m \u2236 \u03c4\n\nsub\u0393 (var wf-\u0393 \u03c4\u1d62\u2208\u0393 \u03c4\u2286\u03c4\u1d62) \u0393\u2286\u0393' = var (\u2286\u0393-wf\u0393' \u0393\u2286\u0393') {! !} \u03c4\u2286\u03c4\u1d62\nsub\u0393 (app \u0393\u22a9m\u2236\u03c4 x x\u2081 x\u2082) \u0393\u2286\u0393' = app (sub\u0393 \u0393\u22a9m\u2236\u03c4 \u0393\u2286\u0393') (sub\u1d62\u0393 x \u0393\u2286\u0393') x\u2081 x\u2082\nsub\u0393 (abs L cf x) \u0393\u2286\u0393' = abs L (\u03bb x\u2209L \u2192 sub\u1d62\u0393 (cf x\u2209L) {! !}) x\nsub\u0393 (Y x x\u2081 x\u2082 x\u2083) \u0393\u2286\u0393' = Y (\u2286\u0393-wf\u0393' \u0393\u2286\u0393') x\u2081 x\u2082 x\u2083\n\nsub\u1d62\u0393 (nil wf-\u0393) \u0393\u2286\u0393' = nil (\u2286\u0393-wf\u0393' \u0393\u2286\u0393')\nsub\u1d62\u0393 (cons x \u0393\u22a9\u1d62m\u2236\u03c4) \u0393\u2286\u0393' = cons (sub\u0393 x \u0393\u2286\u0393') (sub\u1d62\u0393 \u0393\u22a9\u1d62m\u2236\u03c4 \u0393\u2286\u0393')\n\n\nsub : \u2200 {A \u0393 \u0393'} {m : \u039b A} {\u03c4 \u03c4'} -> \u0393 \u22a9 m \u2236 \u03c4 -> \u03c4' \u2286[ A ] \u03c4 -> \u0393 \u2286\u0393 \u0393' -> \u0393' \u22a9 m \u2236 \u03c4'\nsub\u1d62 : \u2200 {A \u0393 \u0393'} {m : \u039b A} {\u03c4 \u03c4'} -> \u0393 \u22a9\u1d62 m \u2236 \u03c4 -> \u03c4' \u2286\u1d62[ A ] \u03c4 -> \u0393 \u2286\u0393 \u0393' -> \u0393' \u22a9\u1d62 m \u2236 \u03c4'\n\nsub (var wf-\u0393 \u03c4\u1d62\u2208\u0393 \u03c4\u2286\u03c4\u1d62) \u03c4'\u2286\u03c4 \u0393\u2286\u0393' =\n var (\u2286\u0393-wf\u0393' \u0393\u2286\u0393') {! !} (\u2286\u1d62-trans (\u2286->\u2286\u1d62 \u03c4'\u2286\u03c4) \u03c4\u2286\u03c4\u1d62)\nsub (app \u0393\u22a9s\u2236\u03c4\u2081~>\u03c4\u2082 \u0393\u22a9\u1d62t\u2236\u03c4\u2081 \u03c4\u2286\u03c4\u2082 \u03c4\u2237A) \u03c4'\u2286\u03c4 \u0393\u2286\u0393' = app\n (sub\u0393 \u0393\u22a9s\u2236\u03c4\u2081~>\u03c4\u2082 \u0393\u2286\u0393')\n (sub\u1d62\u0393 \u0393\u22a9\u1d62t\u2236\u03c4\u2081 \u0393\u2286\u0393')\n (\u2286\u1d62-trans (\u2286->\u2286\u1d62 \u03c4'\u2286\u03c4) \u03c4\u2286\u03c4\u2082)\n \u03c4\u2237A\nsub {_} {\u0393} (abs {\u03c4 = \u03c4} {\u03c4'} L {t} cf \u03c4~>\u03c4'\u2237A\u27f6B) (arr {A} {\u03c4\u2081\u2081 = \u03c4\u2081\u2081} \u03c4\u2286\u03c4\u2081\u2081 \u03c4\u2081\u2082\u2286\u03c4' \u03c4\u2081\u2081~>\u03c4\u2081\u2082\u2237A\u27f6B x\u2083) \u0393\u2286\u0393' =\n abs L (\u03bb x\u2209L \u2192 sub\u1d62 (cf x\u2209L) \u03c4\u2081\u2082\u2286\u03c4' (cons ((\u03c4\u2081\u2081 , A) , (here refl , \u03c4\u2286\u03c4\u2081\u2081)) {! !}) ) \u03c4\u2081\u2081~>\u03c4\u2081\u2082\u2237A\u27f6B\nsub {_} {\u0393} (Y {\u03c4 = \u03c4} {\u03c4\u2081} {\u03c4\u2082} {\u03c4\u2083} wf-\u0393 \u03c4\u2082\u2286\u03c4\u2081 \u03c4\u2286\u03c4\u2081 \u03c4\u2083\u2286) \u03c4'\u2286\u03c4 \u0393\u2286\u0393' =\n Y (\u2286\u0393-wf\u0393' \u0393\u2286\u0393') \u03c4\u2082\u2286\u03c4\u2081 \u03c4\u2286\u03c4\u2081 (\u2286-trans \u03c4'\u2286\u03c4 \u03c4\u2083\u2286)\n\n\nsub\u1d62 \u0393\u22a9\u1d62m\u2236\u03c4 (nil x) \u0393\u2286\u0393' = nil (\u2286\u0393-wf\u0393' \u0393\u2286\u0393')\nsub\u1d62 \u0393\u22a9\u1d62m\u2236\u03c4\u1d62 (cons (\u03c4 , \u03c4\u2208\u03c4\u1d62 , \u03c4'\u2286\u03c4) \u03c4'\u1d62\u2286\u03c4\u1d62) \u0393\u2286\u0393' with \u22a9\u1d62-\u2208-\u22a9 \u0393\u22a9\u1d62m\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62\n... | \u0393\u22a9m\u2236\u03c4 = cons (sub \u0393\u22a9m\u2236\u03c4 \u03c4'\u2286\u03c4 \u0393\u2286\u0393') (sub\u1d62 \u0393\u22a9\u1d62m\u2236\u03c4\u1d62 \u03c4'\u1d62\u2286\u03c4\u1d62 \u0393\u2286\u0393')\n\n\n-- -- weakening-\u03c4 {A} {\u0393} {_} {\u03c4} {\u03c4'} {_} {\u03c4'\u209b\u209b} (var wf-\u0393 \u03c4\u1d62\u2208\u0393 \u03c4\u1d62\u2264\u2229\u03c4 \u03c4\u2237A) \u03c4\u2264\u03c4' \u03c4'\u2237A = var {A} {\u0393} {_} {\u03c4'} {\u03c4'\u209b\u209b} wf-\u0393 \u03c4\u1d62\u2208\u0393 (\u2229-trans \u03c4\u1d62\u2264\u2229\u03c4 \u03c4\u2264\u03c4') \u03c4'\u2237A\n-- -- weakening-\u03c4 (app \u0393\u22a9m\u2236\u03c4\u209b\u209b \u0393\u22a9m\u2236\u03c4\u209b\u209b\u2081 x) \u03c4\u2264\u03c4' \u03c4'\u2237A =\n-- -- app (weakening-\u03c4 \u0393\u22a9m\u2236\u03c4\u209b\u209b (arr \u03c4\u2264\u03c4' \u2264\u2229-refl) (arr {! !} \u03c4'\u2237A)) (weakening-\u03c4 \u0393\u22a9m\u2236\u03c4\u209b\u209b\u2081 \u2264\u2229-refl {! !}) (arr {! !} \u03c4'\u2237A)\n-- -- -- (weakening-\u03c4 \u0393\u22a9m\u2236\u03c4\u209b\u209b (arr \u03c4\u2264\u03c4' \u2264\u2229-refl) ?) (weakening-\u03c4 \u0393\u22a9m\u2236\u03c4\u209b\u209b\u2081 \u2264\u2229-refl ?) ?)\n-- -- weakening-\u03c4 (\u2229-nil \u00acY-shape wf-\u0393) \u03c4\u2264\u03c4' \u03c4'\u2237A = {! !}\n-- -- weakening-\u03c4 (\u2229-cons \u00acY-shape wf-\u0393 \u0393\u22a9m\u2236\u03c4\u209b\u209b \u0393\u22a9m\u2236\u03c4\u209b\u209b\u2081) \u03c4\u2264\u03c4' \u03c4'\u2237A = {! !}\n-- -- weakening-\u03c4 (abs L cf x x\u2081) \u03c4\u2264\u03c4' \u03c4'\u2237A = {! !}\n-- -- weakening-\u03c4 (Y x (arr (arr x\u2081 x\u2082) x\u2083) x\u2084 x\u2085) (arr \u03c4\u2264\u03c4' \u03c4\u2264\u03c4'') \u03c4'\u2237A = {! !}\n-- -- weakening-\u03c4 (Y x (arr (arr x\u2081 x\u2082) x\u2083) x\u2084 x\u2085) \u2229-nil \u03c4'\u2237A = {! !}\n-- -- weakening-\u03c4 (Y x (arr (arr x\u2081 x\u2082) x\u2083) x\u2084 x\u2085) (\u2229-cons \u03c4\u2264\u03c4' \u03c4\u2264\u03c4'') \u03c4'\u2237A = {! !}\n-- -- weakening-\u03c4 (Y x (arr (arr x\u2081 x\u2082) x\u2083) x\u2084 x\u2085) (\u2229-trans \u03c4\u2264\u03c4' \u03c4\u2264\u03c4'') \u03c4'\u2237A = {! !}\n\n\n\n-- \u22a9->\u03b2 : \u2200 {A \u0393} {m m' : \u039b A} {\u03c4} -> \u0393 \u22a9 m' \u2236 \u03c4 -> m ->\u039b\u03b2 m' -> \u0393 \u22a9 m \u2236 \u03c4\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redL x m->\u03b2m') = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redR x m->\u03b2m') = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (abs L x) = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (beta x x\u2081) = {! !}\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c9~>\u03c4\u2082 (nil wf-\u0393) \u03c4\u2286\u03c4\u2082 nil) (Y trm-m) = {! !}\n-- \u22a9->\u03b2 (app \u0393\u22a9m\u2236\u03c4\u2083\u03c4\u1d62~>\u03c4\u2085 (cons _ (app (Y _ \u03c4\u2082\u2286\u03c4\u2081 \u03c4\u2081\u2286\u03c4) (cons _ \u0393\u22a9m\u2236\u03c4~>\u03c4\u2081 (nil wf-\u0393)) \u03c4\u2083\u2286\u03c4\u2082 (cons \u03c4~>\u03c4\u2081\u2237A\u27f6A _)) \u0393\u22a9\u1d62Ym\u2236\u2229\u03c4\u1d62) \u03c4\u2084\u2286\u03c4\u2085 \u03c4\u2083\u03c4\u1d62\u2237A) (Y trm-m) = {! !}\n\n\n\n\n\n\n\n\n\n\n\n\n\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redL x m->\u03b2m') = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redR x m->\u03b2m') = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (abs L x) = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (beta x x\u2081) = {! !}\n-- -- \u22a9->\u03b2 (app \u0393\u22a9m\u2236\u03c4\u2081~>\u03c4 (app (Y wf-\u0393 (arr (arr ::' ::'') ::''') \u03c4\u2264\u03c4\u2081 \u03c4\u2082\u2264\u03c4\u2081) \u0393\u22a9m'\u2236\u03c4\u2082 x\u2084) x\u2085) (Y x\u2086) = {! !}\n-- \u22a9->\u03b2 (app {s = m} {\u03c4\u2082 = \u03c4} \u0393\u22a9m\u2236\u03c4\u2081~>\u03c4 (app {\u03c4\u2082 = \u03c4\u2081} (Y {\u03c4 = \u03c4\u2082} {\u03c4\u2083} wf-\u0393 (arr (arr \u03c4\u2082\u2237A \u03c4\u2083\u2237A) _) \u03c4\u2264\u03c4\u2081 \u03c4\u2082\u2264\u03c4\u2081) \u0393\u22a9m\u2236\u03c4\u2082~>\u03c4\u2083 _) (arr {A = A} \u03c4\u2081\u2237A \u03c4\u2237A)) (Y x\u2086) =\n-- app {A = A \u27f6 A}\n-- (Y {_} {A} {\u2229 (\u03c4\u2082 \u2237 \u03c4\u2081 \u2237 [])} {\u2229 (\u03c4\u2083 \u2237 \u03c4 \u2237 [])} {\u03c4}\n-- wf-\u0393\n-- (arr (arr (\u2229-cons \u03c4\u2082\u2237A (\u2229-cons \u03c4\u2081\u2237A \u2229-nil)) (\u2229-cons \u03c4\u2083\u2237A (\u2229-cons \u03c4\u2237A \u2229-nil))) \u03c4\u2237A)\n-- {! !}\n-- (\u2229-\u2208 (there (here refl))))\n-- {! !}\n-- (arr (arr (\u2229-cons \u03c4\u2082\u2237A (\u2229-cons \u03c4\u2081\u2237A \u2229-nil)) (\u2229-cons \u03c4\u2083\u2237A (\u2229-cons \u03c4\u2237A \u2229-nil))) \u03c4\u2237A)\n--\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4 (\u2229-nil \u00acY-shape wf-\u0393) x) (Y x\u2081) = \u22a5-elim (\u00acY-shape intro\u2081)\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4 (\u2229-cons \u00acY-shape wf-\u0393 \u0393\u22a9m'\u2236\u03c4\u2081 \u0393\u22a9m'\u2236\u03c4\u2082) x) (Y x\u2081) = \u22a5-elim (\u00acY-shape intro\u2081)\n-- \u22a9->\u03b2 (\u2229-nil \u00acY-shape wf-\u0393) (Y x) = \u22a5-elim (\u00acY-shape intro\u2082)\n-- \u22a9->\u03b2 (\u2229-cons \u00acY-shape wf-\u0393 \u0393\u22a9m'\u2236\u03c4 \u0393\u22a9m'\u2236\u03c4\u2081) (Y x) = \u22a5-elim (\u00acY-shape intro\u2082)\n\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redL trm-n m->\u03b2m') = \u22a9->\u03b2-redL \u0393\u22a9m'\u2236\u03c4 m->\u03b2m'\n-- where\n-- \u22a9->\u03b2-redL : \u2200 {A B \u0393} {m m' : \u039b (A \u27f6 B)} {n : \u039b A} {\u03c4} -> \u0393 \u22a9 app m' n \u2236 \u03c4 -> m ->\u039b\u03b2 m' -> \u0393 \u22a9 app m n \u2236 \u03c4\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081 x x\u2081) (redL x\u2082 m->\u039b\u03b2m') =\n-- app (\u22a9->\u03b2-redL \u0393\u22a9m'n\u2236\u03c4 m->\u039b\u03b2m') \u0393\u22a9m'n\u2236\u03c4\u2081 x x\u2081\n-- \u22a9->\u03b2-redL (\u2229-nil \u00acY-shape wf-\u0393) (redL x m->\u039b\u03b2m') = {! !}\n-- \u22a9->\u03b2-redL (\u2229-cons \u00acY-shape wf-\u0393 \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (redL x m->\u039b\u03b2m') = {! !}\n-- \u22a9->\u03b2-redL \u0393\u22a9m'n\u2236\u03c4 (redR x m->\u039b\u03b2m') = {! !}\n-- \u22a9->\u03b2-redL \u0393\u22a9m'n\u2236\u03c4 (abs L x) = {! !}\n-- \u22a9->\u03b2-redL \u0393\u22a9m'n\u2236\u03c4 (beta x x\u2081) = {! !}\n-- \u22a9->\u03b2-redL \u0393\u22a9m'n\u2236\u03c4 (Y x) = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redR trm-m n->\u03b2n') = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (abs L x) = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (beta x x\u2081) = {! !}\n-- \u22a9->\u03b2 (app {s = m} {\u03c4\u2082 = \u03c4} \u0393\u22a9m\u2236\u03c4\u2081~>\u03c4 (app (Y {_} {_} {\u03c4\u2082} {\u03c4\u2083} {\u03c4\u2081} wf-\u0393 \u03c4\u2082\u2237A \u03c4\u2083\u2237A \u03c4\u2081\u2237A \u03c4\u2082\u2264\u2229\u03c4\u2083 \u03c4\u2081\u2264\u2229\u03c4\u2083) \u0393\u22a9m\u2236\u03c4\u2082~>\u03c4\u2083 x \u03c4\u2082~>\u03c4\u2083\u2237A) (arr {A = A} _ \u03c4\u2237A) _) (Y trm-m) =\n-- -- app {A = A \u27f6 A} (Y wf-\u0393 \u03c4\u2081\u2237A \u03c4\u2237A \u03c4\u2237A {! !} {! !}) \u0393\u22a9m\u2236\u03c4\u2081~>\u03c4 (arr (arr \u03c4\u2081\u2237A \u03c4\u2237A) \u03c4\u2237A) (arr \u03c4\u2081\u2237A \u03c4\u2237A)\n-- app {A = A \u27f6 A}\n-- (Y {_} {A} {\u2229 (\u03c4\u2081 \u2237 \u03c4\u2082 \u2237 \u03c4\u2083 \u2237 [])} {\u2229 (\u03c4 \u2237 \u03c4\u2083 \u2237 [])} {\u03c4}\n-- wf-\u0393\n-- {! !}\n-- (\u2229-cons \u03c4\u2237A (\u2229-cons \u03c4\u2083\u2237A \u2229-nil))\n-- \u03c4\u2237A\n-- {! !}\n-- {! !})\n-- {! !}\n-- (arr {! !} {! !})\n-- {! !}\n--\n-- \u22a9->\u03b2 (app \u0393\u22a9m\u2236\u03c4~>\u03c4' (\u2229-nil \u00acY-shape wf-\u0393) x \u03c4\u2237A) (Y trm-m) = \u22a5-elim (\u00acY-shape intro\u2081)\n-- \u22a9->\u03b2 (app \u0393\u22a9m\u2236\u03c4~>\u03c4' (\u2229-cons \u00acY-shape wf-\u0393 \u0393\u22a9Ym\u2236\u03c4' \u0393\u22a9Ym\u2236\u03c4'') x \u03c4\u2237A) (Y trm-m) = \u22a5-elim (\u00acY-shape intro\u2081)\n-- \u22a9->\u03b2 (\u2229-nil \u00acY-shape wf-\u0393) (Y x) = \u22a5-elim (\u00acY-shape intro\u2082)\n-- \u22a9->\u03b2 (\u2229-cons \u00acY-shape wf-\u0393 \u0393\u22a9m'\u2236\u03c4 \u0393\u22a9m'\u2236\u03c4\u2081) (Y x) = \u22a5-elim (\u00acY-shape intro\u2082)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"8a55eb1f41371571c5a4c27630bf3b599688b07e","subject":"IDesc stratified model: silly code indentation","message":"IDesc stratified model: silly code indentation\n","repos":"mietek\/epigram2,larrytheliquid\/pigit,mietek\/epigram2","old_file":"models\/IDesc.agda","new_file":"models\/IDesc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\nunlift : {l : Level}{A : Set l} -> Lifted A -> A\nunlift (lifter a) = a\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\npair : {i j : Level}{A : Set i}{B : A -> Set j} -> \n (x : A) (y : B x) -> Sigma {i = i}{j = j} A B\npair x y = x , y\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l m : Level}{A : Set l}{B : Set m}\n (f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l m n : Level}{A : Set l}{B : Set m}{C : Set n}\n (f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\ntrans : {l : Level}{A : Set l}{x y z : A} -> x == y -> y == z -> x == z\ntrans refl refl = refl\n\nproof-lift-unlift-eq : {l : Level}{A : Set l}(x : Lifted A) -> lifter (unlift x) == x\nproof-lift-unlift-eq (lifter a) = refl\n\n-- Intensionally extensional\npostulate \n reflFun : {l m : Level}{A : Set l}{B : Set m}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set (suc l)) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set (suc l)} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set (suc l)}(R : I -> IDesc {l = l} I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set (suc l)}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set (suc l)}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set (suc l)}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set (suc l) -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\nIDescD : {l : Level}(I : Set (suc l)) -> IDesc {l = suc l} Unit\nIDescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : {l : Level}(I : Set (suc l)) -> Unit -> Set (suc l)\nIDescl0 {x} I = IMu {l = suc x} (\\_ -> IDescD {l = x} I)\n\nIDescl : {l : Level}(I : Set (suc l)) -> Set (suc l)\nIDescl I = IDescl0 I Void\n\nvarl : {l : Level}{I : Set (suc l)}(i : I) -> IDescl I\nvarl {x} i = con (lvar {l = suc x} , i) \n\nconstl : {l : Level}{I : Set (suc l)}(X : Set l) -> IDescl I\nconstl {x} X = con (lconst {l = suc x} , X)\n\nprodl : {l : Level}{I : Set (suc l)}(D D' : IDescl I) -> IDescl I\nprodl {x} D D' = con (lprod {l = suc x} , (D , D'))\n\n\npil : {l : Level}{I : Set (suc l)}(S : Set l)(T : S -> IDescl I) -> IDescl I\npil {x} S T = con (lpi {l = suc x} , pair {i = suc x}{j = suc x} S (\\s -> T (unlift s)))\n\nsigmal : {l : Level}{I : Set (suc l)}(S : Set l)(T : S -> IDescl I) -> IDescl I\nsigmal {x} S T = con (lsigma {l = suc x} , pair {i = suc x}{j = suc x} S (\\s -> T (unlift s)))\n \n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {l : Level}\n {I : Set (suc l)}\n (xs : desc (IDescD I) (IMu (\u03bb _ -> IDescD I)))\n (hs : desc (box (IDescD I) (IMu (\u03bb _ -> IDescD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S (\\s -> hs (lifter s) )\ncases ( lsigma , ( S , T ) ) hs = sigma S (\\s -> hs (lifter s))\n\nphi : {l : Level}{I : Set (suc l)} -> IDescl I -> IDesc I\nphi {x} {I} d = induction (\\_ -> IDescD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {l : Level}{I : Set (suc l)} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {l : Level}{I : Set (suc l)} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi {x} (pi S T) = cong (pi S) \n (reflFun (\\s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S)\n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n-- From embedding to embedding\n\nproof-psi-phi : {l : Level}(I : Set (suc l)) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi {x} I D = induction (\\ _ -> IDescD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> IDescD I)) -> Set (suc x)\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (IDescD I) (IDescl0 I))\n (hs : desc (box (IDescD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (\\T -> con (lpi {l = suc x} , ( S , T ) )) \n (trans (reflFun (\u03bb s \u2192 psi (phi (T (lifter (unlift s)))))\n (\u03bb s \u2192 psi (phi (T (s))))\n (\\s -> cong (\u03bb s \u2192 psi (phi (T (s))))\n (proof-lift-unlift-eq s)))\n (reflFun (\\s -> psi (phi (T s))) \n T \n hs)) \n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (\\T -> con (lsigma {l = suc x} , ( S , T ) )) \n (trans (reflFun (\u03bb s \u2192 psi (phi (T (lifter (unlift s)))))\n (\u03bb s \u2192 psi (phi (T (s))))\n (\\s -> cong (\u03bb s \u2192 psi (phi (T (s))))\n (proof-lift-unlift-eq s)))\n (reflFun (\\s -> psi (phi (T s))) \n T \n hs))","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\nunlift : {l : Level}{A : Set l} -> Lifted A -> A\nunlift (lifter a) = a\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\npair : {i j : Level}{A : Set i}{B : A -> Set j} -> \n (x : A) (y : B x) -> Sigma {i = i}{j = j} A B\npair x y = x , y\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l m : Level}{A : Set l}{B : Set m}\n (f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l m n : Level}{A : Set l}{B : Set m}{C : Set n}\n (f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\ntrans : {l : Level}{A : Set l}{x y z : A} -> x == y -> y == z -> x == z\ntrans refl refl = refl\n\nproof-lift-unlift-eq : {l : Level}{A : Set l}(x : Lifted A) -> lifter (unlift x) == x\nproof-lift-unlift-eq (lifter a) = refl\n\n-- Intensionally extensional\npostulate \n reflFun : {l m : Level}{A : Set l}{B : Set m}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set (suc l)) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set (suc l)} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set (suc l)}(R : I -> IDesc {l = l} I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set (suc l)}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set (suc l)}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set (suc l)}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set (suc l) -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\nIDescD : {l : Level}(I : Set (suc l)) -> IDesc {l = suc l} Unit\nIDescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : {l : Level}(I : Set (suc l)) -> Unit -> Set (suc l)\nIDescl0 {x} I = IMu {l = suc x} (\\_ -> IDescD {l = x} I)\n\nIDescl : {l : Level}(I : Set (suc l)) -> Set (suc l)\nIDescl I = IDescl0 I Void\n\nvarl : {l : Level}{I : Set (suc l)}(i : I) -> IDescl I\nvarl {x} i = con (lvar {l = suc x} , i) \n\nconstl : {l : Level}{I : Set (suc l)}(X : Set l) -> IDescl I\nconstl {x} X = con (lconst {l = suc x} , X)\n\nprodl : {l : Level}{I : Set (suc l)}(D D' : IDescl I) -> IDescl I\nprodl {x} D D' = con (lprod {l = suc x} , (D , D'))\n\n\npil : {l : Level}{I : Set (suc l)}(S : Set l)(T : S -> IDescl I) -> IDescl I\npil {x} S T = con (lpi {l = suc x} , pair {i = suc x}{j = suc x} S (\\s -> T (unlift s)))\n\nsigmal : {l : Level}{I : Set (suc l)}(S : Set l)(T : S -> IDescl I) -> IDescl I\nsigmal {x} S T = con (lsigma {l = suc x} , pair {i = suc x}{j = suc x} S (\\s -> T (unlift s)))\n \n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {l : Level}\n {I : Set (suc l)}\n (xs : desc (IDescD I) (IMu (\u03bb _ -> IDescD I)))\n (hs : desc (box (IDescD I) (IMu (\u03bb _ -> IDescD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S (\\s -> hs (lifter s) )\ncases ( lsigma , ( S , T ) ) hs = sigma S (\\s -> hs (lifter s))\n\nphi : {l : Level}{I : Set (suc l)} -> IDescl I -> IDesc I\nphi {x} {I} d = induction (\\_ -> IDescD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {l : Level}{I : Set (suc l)} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {l : Level}{I : Set (suc l)} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi {x} (pi S T) = cong (pi S) \n (reflFun (\\s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S)\n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n-- From embedding to embedding\n\nproof-psi-phi : {l : Level}(I : Set (suc l)) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi {x} I D = induction (\\ _ -> IDescD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> IDescD I)) -> Set (suc x)\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (IDescD I) (IDescl0 I))\n (hs : desc (box (IDescD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (\\T -> con (lpi {l = suc x} , ( S , T ) )) \n (trans (reflFun (\u03bb s \u2192 psi (phi (T (lifter (unlift s)))))\n (\u03bb s \u2192 psi (phi (T (s))))\n (\\s -> cong (\u03bb s \u2192 psi (phi (T (s))))\n (proof-lift-unlift-eq s)))\n (reflFun (\\s -> psi (phi (T s))) \n T \n hs)) \n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (\\T -> con (lsigma {l = suc x} , ( S , T ) )) \n (trans (reflFun (\u03bb s \u2192 psi (phi (T (lifter (unlift s)))))\n (\u03bb s \u2192 psi (phi (T (s))))\n (\\s -> cong (\u03bb s \u2192 psi (phi (T (s))))\n (proof-lift-unlift-eq s)))\n (reflFun (\\s -> psi (phi (T s))) \n T \n hs))","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"24e0a0189105b8f3f2766a604a1ad5423b51b77e","subject":"Document P.C.Derive.","message":"Document P.C.Derive.\n\nOld-commit-hash: 962425cf9bb190e1b8fb2c400bd9765180081dd0\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Derive.agda","new_file":"Parametric\/Change\/Derive.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Incrementalization as term-to-term transformation (Fig. 4g).\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Derive\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen ChangeType.Structure Base \u0394Base\n\n-- Extension point: Incrementalization of fully applied primitives.\nStructure : Set\nStructure = \u2200 {\u0393 \u03a3 \u03c4} \u2192\n Const \u03a3 \u03c4 \u2192\n Terms (\u0394Context \u0393) \u03a3 \u2192\n Terms (\u0394Context \u0393) (mapContext \u0394Type \u03a3) \u2192\n Term (\u0394Context \u0393) (\u0394Type \u03c4)\n\nmodule Structure (deriveConst : Structure) where\n fit : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394Context \u0393) \u03c4\n fit = weaken \u0393\u227c\u0394\u0393\n\n fit-terms : \u2200 {\u03a3 \u0393} \u2192 Terms \u0393 \u03a3 \u2192 Terms (\u0394Context \u0393) \u03a3\n fit-terms = weaken-terms \u0393\u227c\u0394\u0393\n\n -- In the paper, we transform \"x\" to \"dx\". Here, we work with\n -- de Bruijn indices, so we have to manipulate the indices to\n -- account for a bigger context after transformation.\n deriveVar : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394Context \u0393) (\u0394Type \u03c4)\n deriveVar this = this\n deriveVar (that x) = that (that (deriveVar x))\n\n derive-terms : \u2200 {\u03a3 \u0393} \u2192 Terms \u0393 \u03a3 \u2192 Terms (\u0394Context \u0393) (mapContext \u0394Type \u03a3)\n derive : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394Context \u0393) (\u0394Type \u03c4)\n\n derive-terms {\u2205} \u2205 = \u2205\n derive-terms {\u03c4 \u2022 \u03a3} (t \u2022 ts) = derive t \u2022 derive-terms ts\n\n -- We provide: Incrementalization of arbitrary terms.\n derive (var x) = var (deriveVar x)\n derive (app s t) = app (app (derive s) (fit t)) (derive t)\n derive (abs t) = abs (abs (derive t))\n derive (const c ts) = deriveConst c (fit-terms ts) (derive-terms ts)\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Derive\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen ChangeType.Structure Base \u0394Base\n\nStructure : Set\nStructure = \u2200 {\u0393 \u03a3 \u03c4} \u2192\n Const \u03a3 \u03c4 \u2192\n Terms (\u0394Context \u0393) \u03a3 \u2192\n Terms (\u0394Context \u0393) (mapContext \u0394Type \u03a3) \u2192\n Term (\u0394Context \u0393) (\u0394Type \u03c4)\n\nmodule Structure (deriveConst : Structure) where\n fit : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394Context \u0393) \u03c4\n fit = weaken \u0393\u227c\u0394\u0393\n\n fit-terms : \u2200 {\u03a3 \u0393} \u2192 Terms \u0393 \u03a3 \u2192 Terms (\u0394Context \u0393) \u03a3\n fit-terms = weaken-terms \u0393\u227c\u0394\u0393\n\n deriveVar : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394Context \u0393) (\u0394Type \u03c4)\n deriveVar this = this\n deriveVar (that x) = that (that (deriveVar x))\n\n derive-terms : \u2200 {\u03a3 \u0393} \u2192 Terms \u0393 \u03a3 \u2192 Terms (\u0394Context \u0393) (mapContext \u0394Type \u03a3)\n derive : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394Context \u0393) (\u0394Type \u03c4)\n\n derive-terms {\u2205} \u2205 = \u2205\n derive-terms {\u03c4 \u2022 \u03a3} (t \u2022 ts) = derive t \u2022 derive-terms ts\n\n derive (var x) = var (deriveVar x)\n derive (app s t) = app (app (derive s) (fit t)) (derive t)\n derive (abs t) = abs (abs (derive t))\n derive (const c ts) = deriveConst c (fit-terms ts) (derive-terms ts)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"2785cd34222fed5f734189c03ce46b310d2fd27e","subject":"Desc stratified model: descD implicit.","message":"Desc stratified model: descD implicit.","repos":"kwangkim\/pigment,kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram","old_file":"models\/IDesc.agda","new_file":"models\/IDesc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l : Level}{A B : Set l}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l : Level}{A B C : Set l}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {l : Level}{A B : Set l}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set l) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set l} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set l}(R : I -> IDesc I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set l}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set l -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const (lift I)\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : {l : Level}(I : Set l) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (\\_ -> descD I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst {l = suc x}\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst {l = suc x}\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst {l = suc x}\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst {l = suc x}\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst {l = suc x}\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n \n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\n-- desc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\n-- descD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\n-- IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l\n\n-- cases : (l : Level)\n-- (I : Set l)\n-- (xs : desc (suc l) (lift I) (descD l I) (IMu (suc l) Unit (\\ _ -> descD l I)))\n-- (hs : desc l I (box l I (descD l I) (IMu l Unit (\u03bb _ -> descD l I)) xs) (\u03bb _ -> IDesc (suc l) I)) ->\n-- IDesc (suc l) I\n-- cases x I ( lvar , i ) hs = var i\n-- cases x I ( lconst , X ) hs = const X\n-- cases x I ( lprod , (D , D') ) ( d , d' ) = prod d d'\n-- cases x I ( lpi , ( S , T ) ) hs = pi S hs\n-- cases x I ( lsigma , ( S , T ) ) hs = sigma S hs\n\n-- phi : {I : Set} -> IDescl I -> IDesc I\n-- phi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l : Level}{A B : Set l}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l : Level}{A B C : Set l}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {l : Level}{A B : Set l}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set l) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set l} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set l}(R : I -> IDesc I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set l}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set l -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const (lift I)\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : (l : Level)(I : Set l) -> IDesc Unit\ndescD x I = sigma DescDConst (descDChoice I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (\\_ -> descD x I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst {l = suc x}\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst {l = suc x}\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst {l = suc x}\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst {l = suc x}\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst {l = suc x}\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n \n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\n-- desc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\n-- descD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\n-- IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l\n\n-- cases : (l : Level)\n-- (I : Set l)\n-- (xs : desc (suc l) (lift I) (descD l I) (IMu (suc l) Unit (\\ _ -> descD l I)))\n-- (hs : desc l I (box l I (descD l I) (IMu l Unit (\u03bb _ -> descD l I)) xs) (\u03bb _ -> IDesc (suc l) I)) ->\n-- IDesc (suc l) I\n-- cases x I ( lvar , i ) hs = var i\n-- cases x I ( lconst , X ) hs = const X\n-- cases x I ( lprod , (D , D') ) ( d , d' ) = prod d d'\n-- cases x I ( lpi , ( S , T ) ) hs = pi S hs\n-- cases x I ( lsigma , ( S , T ) ) hs = sigma S hs\n\n-- phi : {I : Set} -> IDescl I -> IDesc I\n-- phi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f215632e880e24517f026a58d55c3637663b1642","subject":"Simplied some proofs.","message":"Simplied some proofs.\n\nIgnore-this: 1aa23e74e5fb6656bef1c63cc2c1e84b\n\ndarcs-hash:20110218170953-3bd4e-ab204ca4c90c40c3fdcd1bd8c40e01b6bb106b5d.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/McCarthy91\/Properties\/AuxiliaryATP.agda","new_file":"Draft\/McCarthy91\/Properties\/AuxiliaryATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Auxiliary properties of the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.Properties.AuxiliaryATP where\n\nopen import LTC.Base\n\nopen import Draft.McCarthy91.ArithmeticATP\nopen import Draft.McCarthy91.McCarthy91\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.Unary.IsN-ATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.Inequalities.PropertiesATP\n\n------------------------------------------------------------------------------\n\n--- Auxiliary properties\n\n---- Case n > 100\npostulate\n Nmc91>100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192 N (mc91 n)\n x100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192\n LT n (mc91 n + eleven)\n{-# ATP prove Nmc91>100 x\u223810-N #-}\n{-# ATP prove x100 x100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810\n Nmc91>100 N111 N101\n#-}\n{-# ATP prove mc91-res-100 111>100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810 #-}\n{-# ATP prove mc91-res-100' mc91-res-100 #-}\n{-# ATP prove Nmc91\u2261100 111>100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810 N91 #-}\n-- {-# ATP prove mc91100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-109 mc91-eq\u2081 98+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-108 mc91-eq\u2081 97+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-107 mc91-eq\u2081 96+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-106 mc91-eq\u2081 95+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-105 mc91-eq\u2081 94+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-104 mc91-eq\u2081 93+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-103 mc91-eq\u2081 92+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-102 mc91-eq\u2081 91+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-101 mc91-eq\u2081 90+11>100 x+11\u223810\u2261Sx #-}\n\npostulate\n mc91-res-99 : mc91 ninety-nine \u2261 ninety-one\n mc91-res-98 : mc91 ninety-eight \u2261 ninety-one\n mc91-res-97 : mc91 ninety-seven \u2261 ninety-one\n mc91-res-96 : mc91 ninety-six \u2261 ninety-one\n mc91-res-95 : mc91 ninety-five \u2261 ninety-one\n mc91-res-94 : mc91 ninety-four \u2261 ninety-one\n mc91-res-93 : mc91 ninety-three \u2261 ninety-one\n mc91-res-92 : mc91 ninety-two \u2261 ninety-one\n mc91-res-91 : mc91 ninety-one \u2261 ninety-one\n mc91-res-90 : mc91 ninety \u2261 ninety-one\n{-# ATP prove mc91-res-99 mc91x-res\u2264100 mc91-res-110 mc91-res-100 #-}\n{-# ATP prove mc91-res-98 mc91x-res\u2264100 mc91-res-109 mc91-res-99 #-}\n{-# ATP prove mc91-res-97 mc91x-res\u2264100 mc91-res-108 mc91-res-98 #-}\n{-# ATP prove mc91-res-96 mc91x-res\u2264100 mc91-res-107 mc91-res-97 #-}\n{-# ATP prove mc91-res-95 mc91x-res\u2264100 mc91-res-106 mc91-res-96 #-}\n{-# ATP prove mc91-res-94 mc91x-res\u2264100 mc91-res-105 mc91-res-95 #-}\n{-# ATP prove mc91-res-93 mc91x-res\u2264100 mc91-res-104 mc91-res-94 #-}\n{-# ATP prove mc91-res-92 mc91x-res\u2264100 mc91-res-103 mc91-res-93 #-}\n{-# ATP prove mc91-res-91 mc91x-res\u2264100 mc91-res-102 mc91-res-92 #-}\n{-# ATP prove mc91-res-90 mc91x-res\u2264100 mc91-res-101 mc91-res-91 #-}\n\nmc91-res-99' : \u2200 {n} \u2192 n \u2261 ninety-nine \u2192 mc91 n \u2261 ninety-one\nmc91-res-99' refl = mc91-res-99\n\nmc91-res-98' : \u2200 {n} \u2192 n \u2261 ninety-eight \u2192 mc91 n \u2261 ninety-one\nmc91-res-98' refl = mc91-res-98\n\nmc91-res-97' : \u2200 {n} \u2192 n \u2261 ninety-seven \u2192 mc91 n \u2261 ninety-one\nmc91-res-97' refl = mc91-res-97\n\nmc91-res-96' : \u2200 {n} \u2192 n \u2261 ninety-six \u2192 mc91 n \u2261 ninety-one\nmc91-res-96' refl = mc91-res-96\n\nmc91-res-95' : \u2200 {n} \u2192 n \u2261 ninety-five \u2192 mc91 n \u2261 ninety-one\nmc91-res-95' refl = mc91-res-95\n\nmc91-res-94' : \u2200 {n} \u2192 n \u2261 ninety-four \u2192 mc91 n \u2261 ninety-one\nmc91-res-94' refl = mc91-res-94\n\nmc91-res-93' : \u2200 {n} \u2192 n \u2261 ninety-three \u2192 mc91 n \u2261 ninety-one\nmc91-res-93' refl = mc91-res-93\n\nmc91-res-92' : \u2200 {n} \u2192 n \u2261 ninety-two \u2192 mc91 n \u2261 ninety-one\nmc91-res-92' refl = mc91-res-92\n\nmc91-res-91' : \u2200 {n} \u2192 n \u2261 ninety-one \u2192 mc91 n \u2261 ninety-one\nmc91-res-91' refl = mc91-res-91\n\nmc91-res-90' : \u2200 {n} \u2192 n \u2261 ninety \u2192 mc91 n \u2261 ninety-one\nmc91-res-90' refl = mc91-res-90\n","old_contents":"------------------------------------------------------------------------------\n-- Auxiliary properties of the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.Properties.AuxiliaryATP where\n\nopen import LTC.Base\n\nopen import Draft.McCarthy91.ArithmeticATP\nopen import Draft.McCarthy91.McCarthy91\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.Unary.IsN-ATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.Inequalities.PropertiesATP\n\n------------------------------------------------------------------------------\n\n--- Auxiliary properties\n\n---- Case n > 100\npostulate\n Nmc91>100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192 N (mc91 n)\n x100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192\n LT n (mc91 n + eleven)\n{-# ATP prove Nmc91>100 x\u223810-N #-}\n{-# ATP prove x100 x100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810\n Nmc91>100 N111 N101\n#-}\n{-# ATP prove mc91-res-100 111>100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810 #-}\n{-# ATP prove mc91-res-100' mc91-res-100 #-}\n{-# ATP prove Nmc91\u2261100 111>100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810 N91 #-}\n-- {-# ATP prove mc91100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-109 mc91-eq\u2081 98+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-108 mc91-eq\u2081 97+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-107 mc91-eq\u2081 96+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-106 mc91-eq\u2081 95+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-105 mc91-eq\u2081 94+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-104 mc91-eq\u2081 93+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-103 mc91-eq\u2081 92+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-102 mc91-eq\u2081 91+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-101 mc91-eq\u2081 90+11>100 x+11\u223810\u2261Sx #-}\n\npostulate\n mc91-res-99 : mc91 ninety-nine \u2261 ninety-one\n mc91-res-98 : mc91 ninety-eight \u2261 ninety-one\n mc91-res-97 : mc91 ninety-seven \u2261 ninety-one\n mc91-res-96 : mc91 ninety-six \u2261 ninety-one\n mc91-res-95 : mc91 ninety-five \u2261 ninety-one\n mc91-res-94 : mc91 ninety-four \u2261 ninety-one\n mc91-res-93 : mc91 ninety-three \u2261 ninety-one\n mc91-res-92 : mc91 ninety-two \u2261 ninety-one\n mc91-res-91 : mc91 ninety-one \u2261 ninety-one\n mc91-res-90 : mc91 ninety \u2261 ninety-one\n{-# ATP prove mc91-res-99 mc91x-res\u2264100 mc91-res-110 mc91-res-100 #-}\n{-# ATP prove mc91-res-98 mc91x-res\u2264100 mc91-res-109 mc91-res-99 #-}\n{-# ATP prove mc91-res-97 mc91x-res\u2264100 mc91-res-108 mc91-res-98 #-}\n{-# ATP prove mc91-res-96 mc91x-res\u2264100 mc91-res-107 mc91-res-97 #-}\n{-# ATP prove mc91-res-95 mc91x-res\u2264100 mc91-res-106 mc91-res-96 #-}\n{-# ATP prove mc91-res-94 mc91x-res\u2264100 mc91-res-105 mc91-res-95 #-}\n{-# ATP prove mc91-res-93 mc91x-res\u2264100 mc91-res-104 mc91-res-94 #-}\n{-# ATP prove mc91-res-92 mc91x-res\u2264100 mc91-res-103 mc91-res-93 #-}\n{-# ATP prove mc91-res-91 mc91x-res\u2264100 mc91-res-102 mc91-res-92 #-}\n{-# ATP prove mc91-res-90 mc91x-res\u2264100 mc91-res-101 mc91-res-91 #-}\n\npostulate\n mc91-res-99' : \u2200 {n} \u2192 n \u2261 ninety-nine \u2192 mc91 n \u2261 ninety-one\n mc91-res-98' : \u2200 {n} \u2192 n \u2261 ninety-eight \u2192 mc91 n \u2261 ninety-one\n mc91-res-97' : \u2200 {n} \u2192 n \u2261 ninety-seven \u2192 mc91 n \u2261 ninety-one\n mc91-res-96' : \u2200 {n} \u2192 n \u2261 ninety-six \u2192 mc91 n \u2261 ninety-one\n mc91-res-95' : \u2200 {n} \u2192 n \u2261 ninety-five \u2192 mc91 n \u2261 ninety-one\n mc91-res-94' : \u2200 {n} \u2192 n \u2261 ninety-four \u2192 mc91 n \u2261 ninety-one\n mc91-res-93' : \u2200 {n} \u2192 n \u2261 ninety-three \u2192 mc91 n \u2261 ninety-one\n mc91-res-92' : \u2200 {n} \u2192 n \u2261 ninety-two \u2192 mc91 n \u2261 ninety-one\n mc91-res-91' : \u2200 {n} \u2192 n \u2261 ninety-one \u2192 mc91 n \u2261 ninety-one\n mc91-res-90' : \u2200 {n} \u2192 n \u2261 ninety \u2192 mc91 n \u2261 ninety-one\n{-# ATP prove mc91-res-99' mc91-res-99 #-}\n{-# ATP prove mc91-res-98' mc91-res-98 #-}\n{-# ATP prove mc91-res-97' mc91-res-97 #-}\n{-# ATP prove mc91-res-96' mc91-res-96 #-}\n{-# ATP prove mc91-res-95' mc91-res-95 #-}\n{-# ATP prove mc91-res-94' mc91-res-94 #-}\n{-# ATP prove mc91-res-93' mc91-res-93 #-}\n{-# ATP prove mc91-res-92' mc91-res-92 #-}\n{-# ATP prove mc91-res-91' mc91-res-91 #-}\n{-# ATP prove mc91-res-90' mc91-res-90 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c2336ec78c0eecde7d41e8928d17e8c38a3f2ed9","subject":"Two: +twist","message":"Two: +twist\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Two.agda","new_file":"lib\/Data\/Two.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule Data.Two where\n\nopen import Data.Bool public hiding (if_then_else_) renaming (Bool to \ud835\udfda; false to 0\u2082; true to 1\u2082; T to \u2713)\nopen import Data.Bool.Properties\n public\n using (isCommutativeSemiring-\u2228-\u2227\n ;commutativeSemiring-\u2228-\u2227\n ;module RingSolver\n ;isCommutativeSemiring-\u2227-\u2228\n ;commutativeSemiring-\u2227-\u2228\n ;isBooleanAlgebra\n ;booleanAlgebra\n ;commutativeRing-xor-\u2227\n ;module XorRingSolver\n ;not-involutive\n ;not-\u00ac\n ;\u00ac-not\n ;\u21d4\u2192\u2261\n ;proof-irrelevance)\n renaming\n (T-\u2261 to \u2713-\u2261\n ;T-\u2227 to \u2713-\u2227\n ;T-\u2228 to \u2713-\u2228)\n\nopen import Type using (\u2605_)\n\nopen import Algebra using (module CommutativeRing; module CommutativeSemiring)\nopen import Algebra.FunctionProperties using (Op\u2081; Op\u2082)\n\nopen import Data.Nat using (\u2115; _\u2264_; z\u2264n; s\u2264s; _\u2293_; _\u2294_; _\u2238_)\nopen import Data.Zero using (\ud835\udfd8-elim)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Product using (proj\u2081; proj\u2082; uncurry; _\u00d7_; _,_)\nopen import Data.Sum using (_\u228e_; inj\u2081; inj\u2082)\n\nopen import Function.Equivalence using (module Equivalence)\nopen import Function.Equality using (_\u27e8$\u27e9_)\nopen import Function.NP using (id; _\u2218_; _\u27e8_\u27e9\u00b0_; flip)\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_; refl; idp; _\u2219_; !_; coe; coe!; J; J-orig)\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\n\nopen import HoTT\nopen Equivalences\n\n\nopen Equivalence using (to; from)\n\nmodule Xor\u00b0 = CommutativeRing commutativeRing-xor-\u2227\nmodule \ud835\udfda\u00b0 = CommutativeSemiring commutativeSemiring-\u2227-\u2228\n\n_\u00b2 : \u2200 {a} \u2192 \u2605 a \u2192 \u2605 a\nA \u00b2 = \ud835\udfda \u2192 A\n\nmodule _ {p} {P : \ud835\udfda \u2192 \u2605 p} where\n\n [0:_1:_] : P 0\u2082 \u2192 P 1\u2082 \u2192 (b : \ud835\udfda) \u2192 P b\n [0: e\u2080 1: e\u2081 ] 0\u2082 = e\u2080\n [0: e\u2080 1: e\u2081 ] 1\u2082 = e\u2081\n\n tabulate\u2082 : ((b : \ud835\udfda) \u2192 P b) \u2192 P 0\u2082 \u00d7 P 1\u2082\n tabulate\u2082 f = f 0\u2082 , f 1\u2082\n\n \u03b7-[0:1:] : \u2200 (f : (b : \ud835\udfda) \u2192 P b) b \u2192 [0: f 0\u2082 1: f 1\u2082 ] b \u2261 f b\n \u03b7-[0:1:] f 0\u2082 = refl\n \u03b7-[0:1:] f 1\u2082 = refl\n\n proj : P 0\u2082 \u00d7 P 1\u2082 \u2192 (b : \ud835\udfda) \u2192 P b\n proj = uncurry [0:_1:_]\n\n proj-tabulate\u2082 : \u2200 (f : (b : \ud835\udfda) \u2192 P b) b \u2192 proj (tabulate\u2082 f) b \u2261 f b\n proj-tabulate\u2082 = \u03b7-[0:1:]\n\nmodule _ {a} {A : \u2605 a} where\n\n [0:_1:_]\u2032 : A \u2192 A \u2192 A \u00b2\n [0:_1:_]\u2032 = [0:_1:_]\n\n case_0:_1:_ : \ud835\udfda \u2192 A \u2192 A \u2192 A\n case b 0: e\u2080 1: e\u2081 = [0: e\u2080\n 1: e\u2081 ] b\n\n proj\u2032 : A \u00d7 A \u2192 A \u00b2\n proj\u2032 = proj\n\n proj[_] : \ud835\udfda \u2192 A \u00d7 A \u2192 A\n proj[_] = [0: proj\u2081 1: proj\u2082 ]\n\n mux : \ud835\udfda \u00d7 (A \u00d7 A) \u2192 A\n mux (s , e\u1d62) = proj e\u1d62 s\n\nnor : (b\u2080 b\u2081 : \ud835\udfda) \u2192 \ud835\udfda\nnor b\u2080 b\u2081 = not (b\u2080 \u2228 b\u2081)\n\nnand : (b\u2080 b\u2081 : \ud835\udfda) \u2192 \ud835\udfda\nnand b\u2080 b\u2081 = not (b\u2080 \u2227 b\u2081)\n\n-- For properties about _==_ see Data.Two.Equality\n_==_ : (b\u2080 b\u2081 : \ud835\udfda) \u2192 \ud835\udfda\nb\u2080 == b\u2081 = (not b\u2080) xor b\u2081\n\ntwist-equiv : \ud835\udfda \u2243 \ud835\udfda\ntwist-equiv = self-inv-equiv not not-involutive\n\nmodule _ {{_ : UA}} where\n twist : \ud835\udfda \u2261 \ud835\udfda\n twist = ua twist-equiv\n\n\u2261\u2192\u2713 : \u2200 {b} \u2192 b \u2261 1\u2082 \u2192 \u2713 b\n\u2261\u2192\u2713 refl = _\n\n\u2261\u2192\u2713not : \u2200 {b} \u2192 b \u2261 0\u2082 \u2192 \u2713 (not b)\n\u2261\u2192\u2713not refl = _\n\n\u2713\u2192\u2261 : \u2200 {b} \u2192 \u2713 b \u2192 b \u2261 1\u2082\n\u2713\u2192\u2261 {1\u2082} _ = refl\n\u2713\u2192\u2261 {0\u2082} ()\n\n\u2713not\u2192\u2261 : \u2200 {b} \u2192 \u2713 (not b) \u2192 b \u2261 0\u2082\n\u2713not\u2192\u2261 {0\u2082} _ = refl\n\u2713not\u2192\u2261 {1\u2082} ()\n\n\u2713\u2227 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 b\u2081 \u2192 \u2713 b\u2082 \u2192 \u2713 (b\u2081 \u2227 b\u2082)\n\u2713\u2227 p q = _\u27e8$\u27e9_ (from \u2713-\u2227) (p , q)\n\n\u2713\u2227\u2081 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 (b\u2081 \u2227 b\u2082) \u2192 \u2713 b\u2081\n\u2713\u2227\u2081 = proj\u2081 \u2218 _\u27e8$\u27e9_ (to \u2713-\u2227)\n\n\u2713\u2227\u2082 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 (b\u2081 \u2227 b\u2082) \u2192 \u2713 b\u2082\n\u2713\u2227\u2082 {b\u2081} = proj\u2082 \u2218 _\u27e8$\u27e9_ (to (\u2713-\u2227 {b\u2081}))\n\n\u2713\u2228-\u228e : \u2200 {b\u2081 b\u2082} \u2192 \u2713 (b\u2081 \u2228 b\u2082) \u2192 \u2713 b\u2081 \u228e \u2713 b\u2082\n\u2713\u2228-\u228e {b\u2081} = _\u27e8$\u27e9_ (to (\u2713-\u2228 {b\u2081}))\n\n\u2713\u2228\u2081 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 b\u2081 \u2192 \u2713 (b\u2081 \u2228 b\u2082)\n\u2713\u2228\u2081 = _\u27e8$\u27e9_ (from \u2713-\u2228) \u2218 inj\u2081\n\n\u2713\u2228\u2082 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 b\u2082 \u2192 \u2713 (b\u2081 \u2228 b\u2082)\n\u2713\u2228\u2082 {b\u2081} = _\u27e8$\u27e9_ (from (\u2713-\u2228 {b\u2081})) \u2218 inj\u2082\n\n\u2713-not-\u00ac : \u2200 {b} \u2192 \u2713 (not b) \u2192 \u00ac (\u2713 b)\n\u2713-not-\u00ac {0\u2082} _ = \u03bb()\n\u2713-not-\u00ac {1\u2082} ()\n\n\u2713-\u00ac-not : \u2200 {b} \u2192 \u00ac (\u2713 b) \u2192 \u2713 (not b)\n\u2713-\u00ac-not {0\u2082} _ = _\n\u2713-\u00ac-not {1\u2082} f = f _\n\n\u2227\u21d2\u2228 : \u2200 x y \u2192 \u2713 (x \u2227 y) \u2192 \u2713 (x \u2228 y)\n\u2227\u21d2\u2228 0\u2082 _ = \u03bb ()\n\u2227\u21d2\u2228 1\u2082 _ = _\n\n\u2713dec : \u2200 b \u2192 Dec (\u2713 b)\n\u2713dec = [0: no (\u03bb())\n 1: yes _ ]\n\nde-morgan : \u2200 x y \u2192 not (x \u2228 y) \u2261 not x \u2227 not y\nde-morgan 0\u2082 _ = refl\nde-morgan 1\u2082 _ = refl\n\n\u22620\u2192\u22611 : \u2200 {x} \u2192 x \u2262 0\u2082 \u2192 x \u2261 1\u2082\n\u22620\u2192\u22611 {1\u2082} p = refl\n\u22620\u2192\u22611 {0\u2082} p = \ud835\udfd8-elim (p refl)\n\n\u22621\u2192\u22610 : \u2200 {x} \u2192 x \u2262 1\u2082 \u2192 x \u2261 0\u2082\n\u22621\u2192\u22610 {0\u2082} p = refl\n\u22621\u2192\u22610 {1\u2082} p = \ud835\udfd8-elim (p refl)\n\n-- 0\u2082 is 0 and 1\u2082 is 1\n\ud835\udfda\u25b9\u2115 : \ud835\udfda \u2192 \u2115\n\ud835\udfda\u25b9\u2115 = [0: 0\n 1: 1 ]\n\n\ud835\udfda\u25b9\u2115\u22641 : \u2200 b \u2192 \ud835\udfda\u25b9\u2115 b \u2264 1\n\ud835\udfda\u25b9\u2115\u22641 = [0: z\u2264n\n 1: s\u2264s z\u2264n ]\n\n\ud835\udfda\u25b9\u2115-\u2293 : \u2200 a b \u2192 \ud835\udfda\u25b9\u2115 a \u2293 \ud835\udfda\u25b9\u2115 b \u2261 \ud835\udfda\u25b9\u2115 (a \u2227 b)\n\ud835\udfda\u25b9\u2115-\u2293 1\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2293 1\u2082 1\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2293 0\u2082 _ = refl\n\n\ud835\udfda\u25b9\u2115-\u2294 : \u2200 a b \u2192 \ud835\udfda\u25b9\u2115 a \u2294 \ud835\udfda\u25b9\u2115 b \u2261 \ud835\udfda\u25b9\u2115 (a \u2228 b)\n\ud835\udfda\u25b9\u2115-\u2294 1\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2294 1\u2082 1\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2294 0\u2082 _ = refl\n\n\ud835\udfda\u25b9\u2115-\u2238 : \u2200 a b \u2192 \ud835\udfda\u25b9\u2115 a \u2238 \ud835\udfda\u25b9\u2115 b \u2261 \ud835\udfda\u25b9\u2115 (a \u2227 not b)\n\ud835\udfda\u25b9\u2115-\u2238 0\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2238 0\u2082 1\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2238 1\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2238 1\u2082 1\u2082 = refl\n\nxor-not-not : \u2200 x y \u2192 (not x) xor (not y) \u2261 x xor y\nxor-not-not 0\u2082 y = not-involutive y\nxor-not-not 1\u2082 _ = refl\n\nnot-inj : \u2200 {x y} \u2192 not x \u2261 not y \u2192 x \u2261 y\nnot-inj {0\u2082} {0\u2082} _ = refl\nnot-inj {1\u2082} {1\u2082} _ = refl\nnot-inj {0\u2082} {1\u2082} ()\nnot-inj {1\u2082} {0\u2082} ()\n\nxor-inj\u2081 : \u2200 x {y z} \u2192 (x xor y) \u2261 (x xor z) \u2192 y \u2261 z\nxor-inj\u2081 0\u2082 = id\nxor-inj\u2081 1\u2082 = not-inj\n\nxor-inj\u2082 : \u2200 x {y z} \u2192 (y xor x) \u2261 (z xor x) \u2192 y \u2261 z\nxor-inj\u2082 x {y} {z} p = xor-inj\u2081 x (Xor\u00b0.+-comm x y \u2219 p \u2219 Xor\u00b0.+-comm z x)\n\ncheck : \u2200 b \u2192 {pf : \u2713 b} \u2192 \ud835\udfd9\ncheck = _\n\nmodule Indexed {a} {A : \u2605 a} where\n _\u2227\u00b0_ : Op\u2082 (A \u2192 \ud835\udfda)\n x \u2227\u00b0 y = x \u27e8 _\u2227_ \u27e9\u00b0 y\n\n _\u2228\u00b0_ : Op\u2082 (A \u2192 \ud835\udfda)\n x \u2228\u00b0 y = x \u27e8 _\u2228_ \u27e9\u00b0 y\n\n _xor\u00b0_ : Op\u2082 (A \u2192 \ud835\udfda)\n x xor\u00b0 y = x \u27e8 _xor_ \u27e9\u00b0 y\n\n _==\u00b0_ : Op\u2082 (A \u2192 \ud835\udfda)\n x ==\u00b0 y = x \u27e8 _==_ \u27e9\u00b0 y\n\n not\u00b0 : Op\u2081 (A \u2192 \ud835\udfda)\n not\u00b0 f = not \u2218 f\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule Data.Two where\n\nopen import Data.Bool public hiding (if_then_else_) renaming (Bool to \ud835\udfda; false to 0\u2082; true to 1\u2082; T to \u2713)\nopen import Data.Bool.Properties\n public\n using (isCommutativeSemiring-\u2228-\u2227\n ;commutativeSemiring-\u2228-\u2227\n ;module RingSolver\n ;isCommutativeSemiring-\u2227-\u2228\n ;commutativeSemiring-\u2227-\u2228\n ;isBooleanAlgebra\n ;booleanAlgebra\n ;commutativeRing-xor-\u2227\n ;module XorRingSolver\n ;not-involutive\n ;not-\u00ac\n ;\u00ac-not\n ;\u21d4\u2192\u2261\n ;proof-irrelevance)\n renaming\n (T-\u2261 to \u2713-\u2261\n ;T-\u2227 to \u2713-\u2227\n ;T-\u2228 to \u2713-\u2228)\n\nopen import Type using (\u2605_)\n\nopen import Algebra using (module CommutativeRing; module CommutativeSemiring)\nopen import Algebra.FunctionProperties using (Op\u2081; Op\u2082)\n\nopen import Data.Nat using (\u2115; _\u2264_; z\u2264n; s\u2264s; _\u2293_; _\u2294_; _\u2238_)\nopen import Data.Zero using (\ud835\udfd8-elim)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Product using (proj\u2081; proj\u2082; uncurry; _\u00d7_; _,_)\nopen import Data.Sum using (_\u228e_; inj\u2081; inj\u2082)\n\nopen import Function.Equivalence using (module Equivalence)\nopen import Function.Equality using (_\u27e8$\u27e9_)\nopen import Function.NP using (id; _\u2218_; _\u27e8_\u27e9\u00b0_)\n\nopen import Relation.Binary.PropositionalEquality.NP using (_\u2261_; _\u2262_; refl; idp; _\u2219_; !_)\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\n\nopen Equivalence using (to; from)\n\nmodule Xor\u00b0 = CommutativeRing commutativeRing-xor-\u2227\nmodule \ud835\udfda\u00b0 = CommutativeSemiring commutativeSemiring-\u2227-\u2228\n\n_\u00b2 : \u2200 {a} \u2192 \u2605 a \u2192 \u2605 a\nA \u00b2 = \ud835\udfda \u2192 A\n\nmodule _ {p} {P : \ud835\udfda \u2192 \u2605 p} where\n\n [0:_1:_] : P 0\u2082 \u2192 P 1\u2082 \u2192 (b : \ud835\udfda) \u2192 P b\n [0: e\u2080 1: e\u2081 ] 0\u2082 = e\u2080\n [0: e\u2080 1: e\u2081 ] 1\u2082 = e\u2081\n\n tabulate\u2082 : ((b : \ud835\udfda) \u2192 P b) \u2192 P 0\u2082 \u00d7 P 1\u2082\n tabulate\u2082 f = f 0\u2082 , f 1\u2082\n\n \u03b7-[0:1:] : \u2200 (f : (b : \ud835\udfda) \u2192 P b) b \u2192 [0: f 0\u2082 1: f 1\u2082 ] b \u2261 f b\n \u03b7-[0:1:] f 0\u2082 = refl\n \u03b7-[0:1:] f 1\u2082 = refl\n\n proj : P 0\u2082 \u00d7 P 1\u2082 \u2192 (b : \ud835\udfda) \u2192 P b\n proj = uncurry [0:_1:_]\n\n proj-tabulate\u2082 : \u2200 (f : (b : \ud835\udfda) \u2192 P b) b \u2192 proj (tabulate\u2082 f) b \u2261 f b\n proj-tabulate\u2082 = \u03b7-[0:1:]\n\nmodule _ {a} {A : \u2605 a} where\n\n [0:_1:_]\u2032 : A \u2192 A \u2192 A \u00b2\n [0:_1:_]\u2032 = [0:_1:_]\n\n case_0:_1:_ : \ud835\udfda \u2192 A \u2192 A \u2192 A\n case b 0: e\u2080 1: e\u2081 = [0: e\u2080\n 1: e\u2081 ] b\n\n proj\u2032 : A \u00d7 A \u2192 A \u00b2\n proj\u2032 = proj\n\n proj[_] : \ud835\udfda \u2192 A \u00d7 A \u2192 A\n proj[_] = [0: proj\u2081 1: proj\u2082 ]\n\n mux : \ud835\udfda \u00d7 (A \u00d7 A) \u2192 A\n mux (s , e\u1d62) = proj e\u1d62 s\n\nnor : (b\u2080 b\u2081 : \ud835\udfda) \u2192 \ud835\udfda\nnor b\u2080 b\u2081 = not (b\u2080 \u2228 b\u2081)\n\nnand : (b\u2080 b\u2081 : \ud835\udfda) \u2192 \ud835\udfda\nnand b\u2080 b\u2081 = not (b\u2080 \u2227 b\u2081)\n\n-- For properties about _==_ see Data.Two.Equality\n_==_ : (b\u2080 b\u2081 : \ud835\udfda) \u2192 \ud835\udfda\nb\u2080 == b\u2081 = (not b\u2080) xor b\u2081\n\n\u2261\u2192\u2713 : \u2200 {b} \u2192 b \u2261 1\u2082 \u2192 \u2713 b\n\u2261\u2192\u2713 refl = _\n\n\u2261\u2192\u2713not : \u2200 {b} \u2192 b \u2261 0\u2082 \u2192 \u2713 (not b)\n\u2261\u2192\u2713not refl = _\n\n\u2713\u2192\u2261 : \u2200 {b} \u2192 \u2713 b \u2192 b \u2261 1\u2082\n\u2713\u2192\u2261 {1\u2082} _ = refl\n\u2713\u2192\u2261 {0\u2082} ()\n\n\u2713not\u2192\u2261 : \u2200 {b} \u2192 \u2713 (not b) \u2192 b \u2261 0\u2082\n\u2713not\u2192\u2261 {0\u2082} _ = refl\n\u2713not\u2192\u2261 {1\u2082} ()\n\n\u2713\u2227 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 b\u2081 \u2192 \u2713 b\u2082 \u2192 \u2713 (b\u2081 \u2227 b\u2082)\n\u2713\u2227 p q = _\u27e8$\u27e9_ (from \u2713-\u2227) (p , q)\n\n\u2713\u2227\u2081 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 (b\u2081 \u2227 b\u2082) \u2192 \u2713 b\u2081\n\u2713\u2227\u2081 = proj\u2081 \u2218 _\u27e8$\u27e9_ (to \u2713-\u2227)\n\n\u2713\u2227\u2082 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 (b\u2081 \u2227 b\u2082) \u2192 \u2713 b\u2082\n\u2713\u2227\u2082 {b\u2081} = proj\u2082 \u2218 _\u27e8$\u27e9_ (to (\u2713-\u2227 {b\u2081}))\n\n\u2713\u2228-\u228e : \u2200 {b\u2081 b\u2082} \u2192 \u2713 (b\u2081 \u2228 b\u2082) \u2192 \u2713 b\u2081 \u228e \u2713 b\u2082\n\u2713\u2228-\u228e {b\u2081} = _\u27e8$\u27e9_ (to (\u2713-\u2228 {b\u2081}))\n\n\u2713\u2228\u2081 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 b\u2081 \u2192 \u2713 (b\u2081 \u2228 b\u2082)\n\u2713\u2228\u2081 = _\u27e8$\u27e9_ (from \u2713-\u2228) \u2218 inj\u2081\n\n\u2713\u2228\u2082 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 b\u2082 \u2192 \u2713 (b\u2081 \u2228 b\u2082)\n\u2713\u2228\u2082 {b\u2081} = _\u27e8$\u27e9_ (from (\u2713-\u2228 {b\u2081})) \u2218 inj\u2082\n\n\u2713-not-\u00ac : \u2200 {b} \u2192 \u2713 (not b) \u2192 \u00ac (\u2713 b)\n\u2713-not-\u00ac {0\u2082} _ = \u03bb()\n\u2713-not-\u00ac {1\u2082} ()\n\n\u2713-\u00ac-not : \u2200 {b} \u2192 \u00ac (\u2713 b) \u2192 \u2713 (not b)\n\u2713-\u00ac-not {0\u2082} _ = _\n\u2713-\u00ac-not {1\u2082} f = f _\n\n\u2227\u21d2\u2228 : \u2200 x y \u2192 \u2713 (x \u2227 y) \u2192 \u2713 (x \u2228 y)\n\u2227\u21d2\u2228 0\u2082 _ = \u03bb ()\n\u2227\u21d2\u2228 1\u2082 _ = _\n\n\u2713dec : \u2200 b \u2192 Dec (\u2713 b)\n\u2713dec = [0: no (\u03bb())\n 1: yes _ ]\n\nde-morgan : \u2200 x y \u2192 not (x \u2228 y) \u2261 not x \u2227 not y\nde-morgan 0\u2082 _ = refl\nde-morgan 1\u2082 _ = refl\n\n\u22620\u2192\u22611 : \u2200 {x} \u2192 x \u2262 0\u2082 \u2192 x \u2261 1\u2082\n\u22620\u2192\u22611 {1\u2082} p = refl\n\u22620\u2192\u22611 {0\u2082} p = \ud835\udfd8-elim (p refl)\n\n\u22621\u2192\u22610 : \u2200 {x} \u2192 x \u2262 1\u2082 \u2192 x \u2261 0\u2082\n\u22621\u2192\u22610 {0\u2082} p = refl\n\u22621\u2192\u22610 {1\u2082} p = \ud835\udfd8-elim (p refl)\n\n-- 0\u2082 is 0 and 1\u2082 is 1\n\ud835\udfda\u25b9\u2115 : \ud835\udfda \u2192 \u2115\n\ud835\udfda\u25b9\u2115 = [0: 0\n 1: 1 ]\n\n\ud835\udfda\u25b9\u2115\u22641 : \u2200 b \u2192 \ud835\udfda\u25b9\u2115 b \u2264 1\n\ud835\udfda\u25b9\u2115\u22641 = [0: z\u2264n\n 1: s\u2264s z\u2264n ]\n\n\ud835\udfda\u25b9\u2115-\u2293 : \u2200 a b \u2192 \ud835\udfda\u25b9\u2115 a \u2293 \ud835\udfda\u25b9\u2115 b \u2261 \ud835\udfda\u25b9\u2115 (a \u2227 b)\n\ud835\udfda\u25b9\u2115-\u2293 1\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2293 1\u2082 1\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2293 0\u2082 _ = refl\n\n\ud835\udfda\u25b9\u2115-\u2294 : \u2200 a b \u2192 \ud835\udfda\u25b9\u2115 a \u2294 \ud835\udfda\u25b9\u2115 b \u2261 \ud835\udfda\u25b9\u2115 (a \u2228 b)\n\ud835\udfda\u25b9\u2115-\u2294 1\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2294 1\u2082 1\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2294 0\u2082 _ = refl\n\n\ud835\udfda\u25b9\u2115-\u2238 : \u2200 a b \u2192 \ud835\udfda\u25b9\u2115 a \u2238 \ud835\udfda\u25b9\u2115 b \u2261 \ud835\udfda\u25b9\u2115 (a \u2227 not b)\n\ud835\udfda\u25b9\u2115-\u2238 0\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2238 0\u2082 1\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2238 1\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2238 1\u2082 1\u2082 = refl\n\nxor-not-not : \u2200 x y \u2192 (not x) xor (not y) \u2261 x xor y\nxor-not-not 0\u2082 y = not-involutive y\nxor-not-not 1\u2082 _ = refl\n\nnot-inj : \u2200 {x y} \u2192 not x \u2261 not y \u2192 x \u2261 y\nnot-inj {0\u2082} {0\u2082} _ = refl\nnot-inj {1\u2082} {1\u2082} _ = refl\nnot-inj {0\u2082} {1\u2082} ()\nnot-inj {1\u2082} {0\u2082} ()\n\nxor-inj\u2081 : \u2200 x {y z} \u2192 (x xor y) \u2261 (x xor z) \u2192 y \u2261 z\nxor-inj\u2081 0\u2082 = id\nxor-inj\u2081 1\u2082 = not-inj\n\nxor-inj\u2082 : \u2200 x {y z} \u2192 (y xor x) \u2261 (z xor x) \u2192 y \u2261 z\nxor-inj\u2082 x {y} {z} p = xor-inj\u2081 x (Xor\u00b0.+-comm x y \u2219 p \u2219 Xor\u00b0.+-comm z x)\n\ncheck : \u2200 b \u2192 {pf : \u2713 b} \u2192 \ud835\udfd9\ncheck = _\n\nmodule Indexed {a} {A : \u2605 a} where\n _\u2227\u00b0_ : Op\u2082 (A \u2192 \ud835\udfda)\n x \u2227\u00b0 y = x \u27e8 _\u2227_ \u27e9\u00b0 y\n\n _\u2228\u00b0_ : Op\u2082 (A \u2192 \ud835\udfda)\n x \u2228\u00b0 y = x \u27e8 _\u2228_ \u27e9\u00b0 y\n\n _xor\u00b0_ : Op\u2082 (A \u2192 \ud835\udfda)\n x xor\u00b0 y = x \u27e8 _xor_ \u27e9\u00b0 y\n\n _==\u00b0_ : Op\u2082 (A \u2192 \ud835\udfda)\n x ==\u00b0 y = x \u27e8 _==_ \u27e9\u00b0 y\n\n not\u00b0 : Op\u2081 (A \u2192 \ud835\udfda)\n not\u00b0 f = not \u2218 f\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"8d4599155dc64db203782bdd39087df1a8014331","subject":"Move (apply|diff)-base to the top of the module.","message":"Move (apply|diff)-base to the top of the module.\n\nThis commit separates the POPL14-specific code from the\nlanguage-independent code in this module. The following commits will\nremove the language-independent code.\n\nOld-commit-hash: 245a7b618b2ac811d28d702089482e9aed8b8425\n","repos":"inc-lc\/ilc-agda","old_file":"Popl14\/Change\/Term.agda","new_file":"Popl14\/Change\/Term.agda","new_contents":"module Popl14.Change.Term where\n\n-- Terms Calculus Popl14\n--\n-- Contents\n-- - Term constructors\n-- - Weakening on terms\n-- - `fit`: weaken a term to its \u0394Context\n-- - diff-term, apply-term and their syntactic sugars\n\nopen import Data.Integer\n\nopen import Popl14.Syntax.Type public\nopen import Popl14.Syntax.Term public\nopen import Popl14.Change.Type public\n\nimport Parametric.Change.Term Const \u0394Base as ChangeTerm\n\ndiff-base : ChangeTerm.DiffStructure\ndiff-base {base-int} = abs\u2082 (\u03bb x y \u2192 add x (minus y))\ndiff-base {base-bag} = abs\u2082 (\u03bb x y \u2192 union x (negate y))\n\napply-base : ChangeTerm.ApplyStructure\napply-base {base-int} = abs\u2082 (\u03bb \u0394x x \u2192 add x \u0394x)\napply-base {base-bag} = abs\u2082 (\u03bb \u0394x x \u2192 union x \u0394x)\n\ndiff-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\napply-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n-- Sugars for diff-term and apply-term\ninfixl 6 _\u2295_ _\u229d_\n_\u2295_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4\n_\u229d_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394Type \u03c4)\nt \u2295 \u0394t = app (app apply-term \u0394t) t\ns \u229d t = app (app diff-term s) t\n\napply-term {base \u03b9} = apply-base {\u03b9}\napply-term {\u03c3 \u21d2 \u03c4} =\n let\n \u0394f = var (that (that this))\n f = var (that this)\n x = var this\n in\n -- \u0394f f x\n abs (abs (abs\n (app f x \u2295 app (app \u0394f x) (x \u229d x))))\n\ndiff-term {base \u03b9} = diff-base {\u03b9}\ndiff-term {\u03c3 \u21d2 \u03c4} =\n let\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n in\n -- g f x \u0394x\n abs (abs (abs (abs\n (app g (x \u2295 \u0394x) \u229d app f x))))\n","old_contents":"module Popl14.Change.Term where\n\n-- Terms Calculus Popl14\n--\n-- Contents\n-- - Term constructors\n-- - Weakening on terms\n-- - `fit`: weaken a term to its \u0394Context\n-- - diff-term, apply-term and their syntactic sugars\n\nopen import Data.Integer\n\nopen import Popl14.Syntax.Type public\nopen import Popl14.Syntax.Term public\nopen import Popl14.Change.Type public\n\nimport Parametric.Change.Term Const \u0394Base as ChangeTerm\n\ndiff-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\napply-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n-- Sugars for diff-term and apply-term\ninfixl 6 _\u2295_ _\u229d_\n_\u2295_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4\n_\u229d_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394Type \u03c4)\nt \u2295 \u0394t = app (app apply-term \u0394t) t\ns \u229d t = app (app diff-term s) t\n\napply-base : ChangeTerm.ApplyStructure\napply-base {base-int} =\n abs\u2082 (\u03bb \u0394x x \u2192 add x \u0394x)\napply-base {base-bag} =\n abs\u2082 (\u03bb \u0394x x \u2192 union x \u0394x)\n\napply-term {base \u03b9} = apply-base {\u03b9}\napply-term {\u03c3 \u21d2 \u03c4} =\n let\n \u0394f = var (that (that this))\n f = var (that this)\n x = var this\n in\n -- \u0394f f x\n abs (abs (abs\n (app f x \u2295 app (app \u0394f x) (x \u229d x))))\n\ndiff-base : ChangeTerm.DiffStructure\ndiff-base {base-int} =\n abs\u2082 (\u03bb x y \u2192 add x (minus y))\ndiff-base {base-bag} =\n abs\u2082 (\u03bb x y \u2192 union x (negate y))\n\ndiff-term {base \u03b9} = diff-base {\u03b9}\ndiff-term {\u03c3 \u21d2 \u03c4} =\n let\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n in\n -- g f x \u0394x\n abs (abs (abs (abs\n (app g (x \u2295 \u0394x) \u229d app f x))))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"cf2254421026fbe05ddc7be9b1c07c8af3c76b19","subject":"whitespace and alpha renaming #3","message":"whitespace and alpha renaming #3\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress.agda","new_file":"progress.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\n\nopen import canonical-boxed-forms\nopen import canonical-value-forms\nopen import canonical-indeterminate-forms\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d boxedval + d indet + \u03a3[ d' \u2208 dhexp ] (d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n V : \u2200{d \u0394} \u2192 d boxedval \u2192 ok d \u0394\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = V (BVVal VConst)\n\n -- variables\n progress (TAVar x\u2081) = abort (somenotnone (! x\u2081))\n\n -- lambdas\n progress (TALam wt) = V (BVVal VLam)\n\n -- applications\n progress (TAAp wt1 wt2)\n with progress wt1 | progress wt2\n -- if the left steps, the whole thing steps\n progress (TAAp wt1 wt2) | S (_ , Step x y z) | _ = S (_ , Step (FHAp1 x) y (FHAp1 z))\n -- if the left is indeterminate, step the right\n progress (TAAp wt1 wt2) | I i | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n -- if they're both indeterminate, step when the cast steps and indet otherwise\n progress (TAAp wt1 wt2) | I x | I x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | I y | CIFACast (_ , _ , _ , _ , _ , refl , _ , _ ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | I y | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n -- if the left is indetermiante but the right is a value\n progress (TAAp wt1 wt2) | I x | V x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | V y | CIFACast (_ , _ , _ , _ , _ , refl , _ , _ ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | V y | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | V y | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | V y | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | V y | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | V y | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n -- if the left is a boxed value, inspect the right\n progress (TAAp wt1 wt2) | V v | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n progress (TAAp wt1 wt2) | V v | I i\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (_ , _ , refl , _) = S (_ , Step FHOuter ITLam FHOuter)\n ... | Inr (_ , _ , _ , refl , _ , _) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | V v | V v\u2082\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (_ , _ , refl , _) = S (_ , Step FHOuter ITLam FHOuter)\n ... | Inr (_ , _ , _ , refl , _ , _) = S (_ , Step FHOuter ITApCast FHOuter)\n\n -- empty holes\n progress (TAEHole x x\u2081) = I IEHole\n\n -- nonempty holes\n progress (TANEHole xin wt x\u2081)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHNEHole x) y (FHNEHole z))\n ... | I x = I (INEHole (FIndet x))\n ... | V x = I (INEHole (FBoxed x))\n\n -- casts\n progress (TACast wt con)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHCast x) y (FHCast z))\n -- indet cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | I x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole1) | I x = I (ICastGroundHole {!!} x) -- cyrus\n progress (TACast wt TCHole2) | I x = I (ICastHoleGround {!!} x {!!}) -- cyrus\n progress (TACast wt (TCArr c1 c2)) | I x = I (ICastArr {!!} x) -- cyrus\n -- boxed value cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | V x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole1) | V x = V (BVHoleCast {!!} x) -- cyrus\n progress (TACast wt TCHole2) | V x = {!!} -- I {!!} -- cyrus: missing rule for boxed values?\n progress (TACast wt (TCArr c1 c2)) | V x = V (BVArrCast {!!} x) -- cyrus\n\n -- failed casts\n progress (TAFailedCast wt y z w)\n with progress wt\n progress (TAFailedCast wt y z w) | S (_ , Step x a q) = S (_ , Step {!!} (ITCastFail y z w) {!!} )\n progress (TAFailedCast wt y z w) | I x = I (IFailedCast (FIndet x) y z w)\n progress (TAFailedCast wt y z w) | V x = I (IFailedCast (FBoxed x) y z w)\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\n\nopen import canonical-boxed-forms\nopen import canonical-value-forms\nopen import canonical-indeterminate-forms\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d boxedval + d indet + \u03a3[ d' \u2208 dhexp ] (d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n V : \u2200{d \u0394} \u2192 d boxedval \u2192 ok d \u0394\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = V (BVVal VConst)\n\n -- variables\n progress (TAVar x\u2081) = abort (somenotnone (! x\u2081))\n\n -- lambdas\n progress (TALam wt) = V (BVVal VLam)\n\n -- applications\n progress (TAAp wt1 wt2)\n with progress wt1 | progress wt2\n -- if the left steps, the whole thing steps\n progress (TAAp wt1 wt2) | S (_ , Step x y z) | _ = S (_ , Step (FHAp1 x) y (FHAp1 z))\n -- if the left is indeterminate, step the right\n progress (TAAp wt1 wt2) | I i | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n -- if they're both indeterminate, step when the cast steps and indet otherwise\n progress (TAAp wt1 wt2) | I x | I x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | I y | CIFACast (_ , _ , _ , _ , _ , refl , _ , _ ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | I y | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n -- if the left is indetermiante but the right is a value\n progress (TAAp wt1 wt2) | I x | V x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | V y | CIFACast (_ , _ , _ , _ , _ , refl , _ , _ ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | V y | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | V y | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | V y | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | V y | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n progress (TAAp wt1 wt2) | I x | V y | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed y))\n -- if the left is a boxed value, inspect the right\n progress (TAAp wt1 wt2) | V v | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n progress (TAAp wt1 wt2) | V v | I i\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (x , d' , refl , qq) = S (_ , Step FHOuter ITLam FHOuter)\n ... | Inr (d' , \u03c41' , \u03c42' , refl , neq , qq) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | V v | V v\u2082\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (x , d' , refl , qq) = S (_ , Step FHOuter ITLam FHOuter)\n ... | Inr (d' , \u03c41' , \u03c42' , refl , neq , qq) = S (_ , Step FHOuter ITApCast FHOuter)\n\n -- empty holes\n progress (TAEHole x x\u2081) = I IEHole\n\n -- nonempty holes\n progress (TANEHole xin wt x\u2081)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHNEHole x) y (FHNEHole z))\n ... | I x = I (INEHole (FIndet x))\n ... | V x = I (INEHole (FBoxed x))\n\n -- casts\n progress (TACast wt con)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHCast x) y (FHCast z))\n -- indet cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | I x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole1) | I x = I (ICastGroundHole {!!} x) -- cyrus\n progress (TACast wt TCHole2) | I x = I (ICastHoleGround {!!} x {!!}) -- cyrus\n progress (TACast wt (TCArr c1 c2)) | I x = I (ICastArr {!!} x) -- cyrus\n -- boxed value cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | V x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole1) | V x = V (BVHoleCast {!!} x) -- cyrus\n progress (TACast wt TCHole2) | V x = {!!} -- I {!!} -- cyrus: missing rule for boxed values?\n progress (TACast wt (TCArr c1 c2)) | V x = V (BVArrCast {!!} x) -- cyrus\n\n -- failed casts\n progress (TAFailedCast wt y z w)\n with progress wt\n progress (TAFailedCast wt y z w) | S (_ , Step x a q) = S (_ , Step {!!} (ITCastFail y z w) {!!} )\n progress (TAFailedCast wt y z w) | I x = I (IFailedCast (FIndet x) y z w)\n progress (TAFailedCast wt y z w) | V x = I (IFailedCast (FBoxed x) y z w)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"5e0522a26266d87d99cd7169d3df306935bdf8f4","subject":"Removed unused code.","message":"Removed unused code.\n\nIgnore-this: bb2cba0cc416e210cbca1ac97da677e5\n\ndarcs-hash:20100505162046-3bd4e-f69bdae78351e9271235c7a8d96d8aea25255f18.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Relation\/Inequalities.agda","new_file":"LTC\/Relation\/Inequalities.agda","new_contents":"------------------------------------------------------------------------------\n-- Inequalities on partial natural numbers\n------------------------------------------------------------------------------\n\nmodule LTC.Relation.Inequalities where\n\nopen import LTC.Minimal\nopen import MyStdLib.Data.Product\n\n------------------------------------------------------------------------------\n\npostulate\n lt : D \u2192 D \u2192 D\n\n lt-00 : lt zero zero \u2261 false\n\n lt-0S : (d : D) \u2192 lt zero (succ d) \u2261 true\n\n lt-S0 : (d : D) \u2192 lt (succ d) zero \u2261 false\n\n lt-SS : (d e : D) \u2192 lt (succ d) (succ e) \u2261 lt d e\n\n{-# ATP axiom lt-00 #-}\n{-# ATP axiom lt-0S #-}\n{-# ATP axiom lt-S0 #-}\n{-# ATP axiom lt-SS #-}\n\ngt : D \u2192 D \u2192 D\ngt d e = lt e d\n{-# ATP definition gt #-}\n\n------------------------------------------------------------------------\n-- The data types\n\n-- infix 4 _\u2264_ _<_ _\u2265_ _>_\n\nGT : D \u2192 D \u2192 Set\nGT d e = gt d e \u2261 true\n{-# ATP definition GT #-}\n\nLT : D \u2192 D \u2192 Set\nLT d e = lt d e \u2261 true\n{-# ATP definition LT #-}\n\nLE : D \u2192 D \u2192 Set\nLE d e = gt d e \u2261 false\n{-# ATP definition LE #-}\n\nGE : D \u2192 D \u2192 Set\nGE d e = lt d e \u2261 false\n{-# ATP definition GE #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Inequalities on partial natural numbers\n------------------------------------------------------------------------------\n\nmodule LTC.Relation.Inequalities where\n\nopen import LTC.Minimal\nopen import MyStdLib.Data.Product\n\n------------------------------------------------------------------------------\n\npostulate\n lt : D \u2192 D \u2192 D\n\n lt-00 : lt zero zero \u2261 false\n\n lt-0S : (d : D) \u2192 lt zero (succ d) \u2261 true\n\n lt-S0 : (d : D) \u2192 lt (succ d) zero \u2261 false\n\n lt-SS : (d e : D) \u2192 lt (succ d) (succ e) \u2261 lt d e\n\n{-# ATP axiom lt-00 #-}\n{-# ATP axiom lt-0S #-}\n{-# ATP axiom lt-S0 #-}\n{-# ATP axiom lt-SS #-}\n\ngt : D \u2192 D \u2192 D\ngt d e = lt e d\n{-# ATP definition gt #-}\n\n------------------------------------------------------------------------\n-- The data types\n\n-- infix 4 _\u2264_ _<_ _\u2265_ _>_\n\nGT : D \u2192 D \u2192 Set\nGT d e = gt d e \u2261 true\n{-# ATP definition GT #-}\n\nLT : D \u2192 D \u2192 Set\nLT d e = lt d e \u2261 true\n{-# ATP definition LT #-}\n\nLE : D \u2192 D \u2192 Set\nLE d e = gt d e \u2261 false\n{-# ATP definition LE #-}\n\nGE : D \u2192 D \u2192 Set\nGE d e = lt d e \u2261 false\n{-# ATP definition GE #-}\n\n-- ------------------------------------------------------------------------\n-- -- Lexicographical order on D\n-- data LT\u2082 : D \u00d7 D \u2192 D \u00d7 D \u2192 Set where\n-- left : {x\u2081 x\u2082 y\u2081 y\u2082 : D} \u2192 LT x\u2081 x\u2082 \u2192 LT\u2082 (x\u2081 , y\u2081) (x\u2082 , y\u2082)\n-- right : {x y\u2081 y\u2082 : D} \u2192 LT y\u2081 y\u2082 \u2192 LT\u2082 (x , y\u2081) (x , y\u2082)\n-- -- {-# ATP hint left #-}\n-- -- {-# ATP hint right #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c19f3333c5cdfd8103fff65fb873090cb0e21f88","subject":"making complete expansion a little bit stronger by also observing that the resultant hole context must be empty","message":"making complete expansion a little bit stronger by also observing that the resultant hole context must be empty\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"complete-expansion.agda","new_file":"complete-expansion.agda","new_contents":"open import Nat\nopen import Prelude\nopen import core\nopen import contexts\nopen import typed-expansion\nopen import lemmas-gcomplete\n\nmodule complete-expansion where\n -- this might be derivable from things below and a fact about => and ::\n -- that we seem to have not proven\n complete-ta : \u2200{\u0393 \u0394 d \u03c4} \u2192 (\u0393 gcomplete) \u2192 (\u0394 , \u0393 \u22a2 d :: \u03c4) \u2192 d dcomplete \u2192 \u03c4 tcomplete\n complete-ta gc TAConst comp = TCBase\n complete-ta gc (TAVar x\u2081) DCVar = gc _ _ x\u2081\n complete-ta gc (TALam a wt) (DCLam comp x\u2081) = TCArr x\u2081 (complete-ta (gcomp-extend gc x\u2081 a ) wt comp)\n complete-ta gc (TAAp wt wt\u2081) (DCAp comp comp\u2081) with complete-ta gc wt comp\n complete-ta gc (TAAp wt wt\u2081) (DCAp comp comp\u2081) | TCArr qq qq\u2081 = qq\u2081\n complete-ta gc (TAEHole x x\u2081) ()\n complete-ta gc (TANEHole x wt x\u2081) ()\n complete-ta gc (TACast wt x) (DCCast comp x\u2081 x\u2082) = x\u2082\n complete-ta gc (TAFailedCast wt x x\u2081 x\u2082) ()\n\n comp-synth : \u2200{\u0393 e \u03c4} \u2192\n \u0393 gcomplete \u2192\n e ecomplete \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u03c4 tcomplete\n comp-synth gc ec SConst = TCBase\n comp-synth gc (ECAsc x ec) (SAsc x\u2081) = x\n comp-synth gc ec (SVar x) = gc _ _ x\n comp-synth gc (ECAp ec ec\u2081) (SAp _ wt MAHole x\u2081) with comp-synth gc ec wt\n ... | ()\n comp-synth gc (ECAp ec ec\u2081) (SAp _ wt MAArr x\u2081) with comp-synth gc ec wt\n comp-synth gc (ECAp ec ec\u2081) (SAp _ wt MAArr x\u2081) | TCArr qq qq\u2081 = qq\u2081\n comp-synth gc () SEHole\n comp-synth gc () (SNEHole _ wt)\n comp-synth gc (ECLam2 ec x\u2081) (SLam x\u2082 wt) = TCArr x\u2081 (comp-synth (gcomp-extend gc x\u2081 x\u2082) ec wt)\n\n mutual\n complete-expansion-synth : \u2200{e \u03c4 \u0393 \u0394 d} \u2192\n \u0393 gcomplete \u2192\n e ecomplete \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n (d dcomplete \u00d7 \u03c4 tcomplete \u00d7 \u0394 == \u2205)\n complete-expansion-synth gc ec ESConst = DCConst , TCBase , refl\n complete-expansion-synth gc ec (ESVar x\u2081) = DCVar , gc _ _ x\u2081 , refl\n complete-expansion-synth gc (ECLam2 ec x\u2081) (ESLam x\u2082 exp) with complete-expansion-synth (gcomp-extend gc x\u2081 x\u2082) ec exp\n ... | ih1 , ih2 , ih3 = DCLam ih1 x\u2081 , TCArr x\u2081 ih2 , ih3\n complete-expansion-synth gc (ECAp ec ec\u2081) (ESAp _ _ x MAHole x\u2082 x\u2083) with comp-synth gc ec x\n ... | ()\n complete-expansion-synth gc (ECAp ec ec\u2081) (ESAp {\u03941 = \u03941} {\u03942 = \u03942} _ _ x MAArr x\u2082 x\u2083)\n with comp-synth gc ec x\n ... | TCArr t1 t2\n with complete-expansion-ana gc ec (TCArr t1 t2) x\u2082 | complete-expansion-ana gc ec\u2081 t1 x\u2083\n ... | ih1 , ih4 | ih2 , ih3 = DCAp (DCCast ih1 (comp-ana gc x\u2082 ih1) (TCArr t1 t2)) (DCCast ih2 (comp-ana gc x\u2083 ih2) t1) ,\n t2 ,\n tr (\u03bb qq \u2192 (qq \u222a \u03942) == \u2205) (! ih4) (tr (\u03bb qq \u2192 (\u2205 \u222a qq) == \u2205) (! ih3) refl)\n complete-expansion-synth gc () ESEHole\n complete-expansion-synth gc () (ESNEHole _ exp)\n complete-expansion-synth gc (ECAsc x ec) (ESAsc x\u2081)\n with complete-expansion-ana gc ec x x\u2081\n ... | ih1 , ih2 = DCCast ih1 (comp-ana gc x\u2081 ih1) x , x , ih2\n\n complete-expansion-ana : \u2200{e \u03c4 \u03c4' \u0393 \u0394 d} \u2192\n \u0393 gcomplete \u2192\n e ecomplete \u2192\n \u03c4 tcomplete \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (d dcomplete \u00d7 \u0394 == \u2205)\n complete-expansion-ana gc (ECLam1 ec) () (EALam x\u2081 MAHole exp)\n complete-expansion-ana gc (ECLam1 ec) (TCArr t1 t2) (EALam x\u2081 MAArr exp)\n with complete-expansion-ana (gcomp-extend gc t1 x\u2081) ec t2 exp\n ... | ih , ih2 = DCLam ih t1 , ih2\n complete-expansion-ana gc ec tc (EASubsume x x\u2081 x\u2082 x\u2083)\n with complete-expansion-synth gc ec x\u2082\n ... | ih1 , ih2 , ih3 = ih1 , ih3\n\n --this is just a convenience since it shows up a few times above\n comp-ana : \u2200{\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 gcomplete \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n d dcomplete \u2192\n \u03c4' tcomplete\n comp-ana gc ex dc = complete-ta gc (\u03c02 (typed-expansion-ana ex)) dc\n","old_contents":"open import Nat\nopen import Prelude\nopen import core\nopen import contexts\nopen import typed-expansion\nopen import lemmas-gcomplete\n\nmodule complete-expansion where\n -- this might be derivable from things below and a fact about => and ::\n -- that we seem to have not proven\n complete-ta : \u2200{\u0393 \u0394 d \u03c4} \u2192 (\u0393 gcomplete) \u2192 (\u0394 , \u0393 \u22a2 d :: \u03c4) \u2192 d dcomplete \u2192 \u03c4 tcomplete\n complete-ta gc TAConst comp = TCBase\n complete-ta gc (TAVar x\u2081) DCVar = gc _ _ x\u2081\n complete-ta gc (TALam a wt) (DCLam comp x\u2081) = TCArr x\u2081 (complete-ta (gcomp-extend gc x\u2081 a ) wt comp)\n complete-ta gc (TAAp wt wt\u2081) (DCAp comp comp\u2081) with complete-ta gc wt comp\n complete-ta gc (TAAp wt wt\u2081) (DCAp comp comp\u2081) | TCArr qq qq\u2081 = qq\u2081\n complete-ta gc (TAEHole x x\u2081) ()\n complete-ta gc (TANEHole x wt x\u2081) ()\n complete-ta gc (TACast wt x) (DCCast comp x\u2081 x\u2082) = x\u2082\n complete-ta gc (TAFailedCast wt x x\u2081 x\u2082) ()\n\n comp-synth : \u2200{\u0393 e \u03c4} \u2192\n \u0393 gcomplete \u2192\n e ecomplete \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u03c4 tcomplete\n comp-synth gc ec SConst = TCBase\n comp-synth gc (ECAsc x ec) (SAsc x\u2081) = x\n comp-synth gc ec (SVar x) = gc _ _ x\n comp-synth gc (ECAp ec ec\u2081) (SAp _ wt MAHole x\u2081) with comp-synth gc ec wt\n ... | ()\n comp-synth gc (ECAp ec ec\u2081) (SAp _ wt MAArr x\u2081) with comp-synth gc ec wt\n comp-synth gc (ECAp ec ec\u2081) (SAp _ wt MAArr x\u2081) | TCArr qq qq\u2081 = qq\u2081\n comp-synth gc () SEHole\n comp-synth gc () (SNEHole _ wt)\n comp-synth gc (ECLam2 ec x\u2081) (SLam x\u2082 wt) = TCArr x\u2081 (comp-synth (gcomp-extend gc x\u2081 x\u2082) ec wt)\n\n mutual\n complete-expansion-synth : \u2200{e \u03c4 \u0393 \u0394 d} \u2192\n \u0393 gcomplete \u2192\n e ecomplete \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n (d dcomplete \u00d7 \u03c4 tcomplete)\n complete-expansion-synth gc ec ESConst = DCConst , TCBase\n complete-expansion-synth gc ec (ESVar x\u2081) = DCVar , gc _ _ x\u2081\n complete-expansion-synth gc (ECLam2 ec x\u2081) (ESLam x\u2082 exp) with complete-expansion-synth (gcomp-extend gc x\u2081 x\u2082) ec exp\n ... | ih1 , ih2 = DCLam ih1 x\u2081 , TCArr x\u2081 ih2\n complete-expansion-synth gc (ECAp ec ec\u2081) (ESAp _ _ x MAHole x\u2082 x\u2083) with comp-synth gc ec x\n ... | ()\n complete-expansion-synth gc (ECAp ec ec\u2081) (ESAp _ _ x MAArr x\u2082 x\u2083)\n with comp-synth gc ec x\n ... | TCArr t1 t2\n with complete-expansion-ana gc ec (TCArr t1 t2) x\u2082 | complete-expansion-ana gc ec\u2081 t1 x\u2083\n ... | ih1 | ih2 = DCAp (DCCast ih1 (comp-ana gc x\u2082 ih1) (TCArr t1 t2)) (DCCast ih2 (comp-ana gc x\u2083 ih2) t1) , t2\n\n complete-expansion-synth gc () ESEHole\n complete-expansion-synth gc () (ESNEHole _ exp)\n complete-expansion-synth gc (ECAsc x ec) (ESAsc x\u2081)\n with complete-expansion-ana gc ec x x\u2081\n ... | ih = DCCast ih (comp-ana gc x\u2081 ih) x , x\n\n complete-expansion-ana : \u2200{e \u03c4 \u03c4' \u0393 \u0394 d} \u2192\n \u0393 gcomplete \u2192\n e ecomplete \u2192\n \u03c4 tcomplete \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n d dcomplete\n complete-expansion-ana gc (ECLam1 ec) () (EALam x\u2081 MAHole exp)\n complete-expansion-ana gc (ECLam1 ec) (TCArr t1 t2) (EALam x\u2081 MAArr exp)\n with complete-expansion-ana (gcomp-extend gc t1 x\u2081) ec t2 exp\n ... | ih = DCLam ih t1\n complete-expansion-ana gc ec tc (EASubsume x x\u2081 x\u2082 x\u2083) = \u03c01(complete-expansion-synth gc ec x\u2082)\n\n --this is just a convenience since it shows up a few times above\n comp-ana : \u2200{\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 gcomplete \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n d dcomplete \u2192\n \u03c4' tcomplete\n comp-ana gc ex dc = complete-ta gc (\u03c02 (typed-expansion-ana ex)) dc\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c68a9b8870e9eaca8c1a746f2ec67e2c84d97fd5","subject":"CyclicGroup: remove useless import","message":"CyclicGroup: remove useless import\n","repos":"crypto-agda\/crypto-agda","old_file":"Crypto\/JS\/BigI\/CyclicGroup.agda","new_file":"Crypto\/JS\/BigI\/CyclicGroup.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Type.Eq\nopen import FFI.JS using (JS[_]; return; _++_; _>>_)\nopen import FFI.JS.Check using (check!)\n\nopen import FFI.JS.BigI\nopen import Data.List.Base using (List; foldr)\nopen import Data.Two hiding (_==_)\nopen import Relation.Binary.PropositionalEquality\nopen import Algebra.Raw\nopen import Algebra.Group\n\n-- TODO carry on a primality proof of p\nmodule Crypto.JS.BigI.CyclicGroup (p : BigI) where\n\nabstract\n \u2124[_]\u2605 : Set\n \u2124[_]\u2605 = BigI\n\n private\n \u2124p\u2605 : Set\n \u2124p\u2605 = BigI\n\n mod-p : BigI \u2192 \u2124p\u2605\n mod-p x = mod x p\n\n -- There are two ways to go from BigI to \u2124p\u2605: check and mod-p\n -- Use check for untrusted input data and mod-p for internal\n -- computation.\n BigI\u25b9\u2124[_]\u2605 : BigI \u2192 JS[ \u2124p\u2605 ]\n BigI\u25b9\u2124[_]\u2605 x =\n -- Console.log \"BigI\u25b9\u2124[_]\u2605\" >>\n check! \"below modulus\" (x >\n check! \"strictcly positive\" (x >I 0I)\n (\u03bb _ \u2192 \"Should be strictly positive: \" ++\n toString x ++\n \" <= 0\") >>\n return x\n\n repr : \u2124p\u2605 \u2192 BigI\n repr x = x\n\n 1# : \u2124p\u2605\n 1# = 1I\n\n 1\/_ : \u2124p\u2605 \u2192 \u2124p\u2605\n 1\/ x = modInv x p\n\n _^_ : \u2124p\u2605 \u2192 BigI \u2192 \u2124p\u2605\n x ^ y = modPow x y p\n\n_*_ _\/_ : \u2124p\u2605 \u2192 \u2124p\u2605 \u2192 \u2124p\u2605\n\nx * y = mod-p (multiply (repr x) (repr y))\nx \/ y = x * 1\/ y\n\ninstance\n \u2124[_]\u2605-Eq? : Eq? \u2124p\u2605\n \u2124[_]\u2605-Eq? = record\n { _==_ = _=='_\n ; \u2261\u21d2== = \u2261\u21d2=='\n ; ==\u21d2\u2261 = ==\u21d2\u2261' }\n where\n _=='_ : \u2124p\u2605 \u2192 \u2124p\u2605 \u2192 \ud835\udfda\n x ==' y = equals (repr x) (repr y)\n postulate\n \u2261\u21d2==' : \u2200 {x y} \u2192 x \u2261 y \u2192 \u2713 (x ==' y)\n ==\u21d2\u2261' : \u2200 {x y} \u2192 \u2713 (x ==' y) \u2192 x \u2261 y\n\nprod : List \u2124p\u2605 \u2192 \u2124p\u2605\nprod = foldr _*_ 1#\n\nmon-ops : Monoid-Ops \u2124p\u2605\nmon-ops = _*_ , 1#\n\ngrp-ops : Group-Ops \u2124p\u2605\ngrp-ops = mon-ops , 1\/_\n\npostulate\n grp-struct : Group-Struct grp-ops\n\ngrp : Group \u2124p\u2605\ngrp = grp-ops , grp-struct\n\nmodule grp = Group grp\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Type.Eq\nopen import FFI.JS using (JS[_]; return; Bool; _++_; _>>_)\nopen import FFI.JS.Check using (check!)\n\nopen import FFI.JS.BigI\nopen import Data.List.Base using (List; foldr)\nopen import Data.Two hiding (_==_)\nopen import Relation.Binary.PropositionalEquality\nopen import Algebra.Raw\nopen import Algebra.Group\n\n-- TODO carry on a primality proof of p\nmodule Crypto.JS.BigI.CyclicGroup (p : BigI) where\n\nabstract\n \u2124[_]\u2605 : Set\n \u2124[_]\u2605 = BigI\n\n private\n \u2124p\u2605 : Set\n \u2124p\u2605 = BigI\n\n mod-p : BigI \u2192 \u2124p\u2605\n mod-p x = mod x p\n\n -- There are two ways to go from BigI to \u2124p\u2605: check and mod-p\n -- Use check for untrusted input data and mod-p for internal\n -- computation.\n BigI\u25b9\u2124[_]\u2605 : BigI \u2192 JS[ \u2124p\u2605 ]\n BigI\u25b9\u2124[_]\u2605 x =\n -- Console.log \"BigI\u25b9\u2124[_]\u2605\" >>\n check! \"below modulus\" (x >\n check! \"strictcly positive\" (x >I 0I)\n (\u03bb _ \u2192 \"Should be strictly positive: \" ++\n toString x ++\n \" <= 0\") >>\n return x\n\n repr : \u2124p\u2605 \u2192 BigI\n repr x = x\n\n 1# : \u2124p\u2605\n 1# = 1I\n\n 1\/_ : \u2124p\u2605 \u2192 \u2124p\u2605\n 1\/ x = modInv x p\n\n _^_ : \u2124p\u2605 \u2192 BigI \u2192 \u2124p\u2605\n x ^ y = modPow x y p\n\n_*_ _\/_ : \u2124p\u2605 \u2192 \u2124p\u2605 \u2192 \u2124p\u2605\n\nx * y = mod-p (multiply (repr x) (repr y))\nx \/ y = x * 1\/ y\n\ninstance\n \u2124[_]\u2605-Eq? : Eq? \u2124p\u2605\n \u2124[_]\u2605-Eq? = record\n { _==_ = _=='_\n ; \u2261\u21d2== = \u2261\u21d2=='\n ; ==\u21d2\u2261 = ==\u21d2\u2261' }\n where\n _=='_ : \u2124p\u2605 \u2192 \u2124p\u2605 \u2192 \ud835\udfda\n x ==' y = equals (repr x) (repr y)\n postulate\n \u2261\u21d2==' : \u2200 {x y} \u2192 x \u2261 y \u2192 \u2713 (x ==' y)\n ==\u21d2\u2261' : \u2200 {x y} \u2192 \u2713 (x ==' y) \u2192 x \u2261 y\n\nprod : List \u2124p\u2605 \u2192 \u2124p\u2605\nprod = foldr _*_ 1#\n\nmon-ops : Monoid-Ops \u2124p\u2605\nmon-ops = _*_ , 1#\n\ngrp-ops : Group-Ops \u2124p\u2605\ngrp-ops = mon-ops , 1\/_\n\npostulate\n grp-struct : Group-Struct grp-ops\n\ngrp : Group \u2124p\u2605\ngrp = grp-ops , grp-struct\n\nmodule grp = Group grp\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"ef672acfb72215f4d1bfad30178223e7de83d7d3","subject":"or-elimiation proved","message":"or-elimiation proved\n","repos":"shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps","old_file":"learyouanagda\/IPL.agda","new_file":"learyouanagda\/IPL.agda","new_contents":"module IPL where\n\n\ndata _\u2227_ (P : Set) (Q : Set) : Set where\n \u2227-intro : P \u2192 Q \u2192 (P \u2227 Q)\n\n\nproof\u2081 : {P Q : Set} \u2192 (P \u2227 Q) \u2192 P\nproof\u2081 (\u2227-intro p q) = p\n\nproof\u2082 : {P Q : Set} \u2192 (P \u2227 Q) \u2192 Q\nproof\u2082 (\u2227-intro p q) = q\n\n\n_\u21d4_ : (P : Set) \u2192 (Q : Set) \u2192 Set\na \u21d4 b = (a \u2192 b) \u2227 (b \u2192 a)\n\n\n\u2227-comm\u2032 : (P Q : Set) \u2192 (P \u2227 Q) \u2192 (Q \u2227 P)\n\u2227-comm\u2032 _ _ (\u2227-intro p q) = \u2227-intro q p\n\n\u2227-comm : (P Q : Set) \u2192 (P \u2227 Q) \u21d4 (Q \u2227 P)\n\u2227-comm P Q = \u2227-intro (\u2227-comm\u2032 P Q) (\u2227-comm\u2032 Q P)\n\n\n\u2227-comm1\u2032 : {P Q : Set} \u2192 (P \u2227 Q) \u2192 (Q \u2227 P)\n\u2227-comm1\u2032 (\u2227-intro p q) = \u2227-intro q p\n\n\n\ndata _\u2228_ (P Q : Set) : Set where\n \u2228-intro\u2081 : P \u2192 P \u2228 Q\n \u2228-intro\u2082 : Q \u2192 P \u2228 Q\n\n\n\u2228-elim : {A B C : Set} \u2192 (A \u2192 C) \u2192 (B \u2192 C) \u2192 (A \u2228 B) \u2192 C\n\u2228-elim ac bc (\u2228-intro\u2081 a) = ac a\n\u2228-elim ac bc (\u2228-intro\u2082 b) = bc b\n","old_contents":"module IPL where\n\n\ndata _\u2227_ (P : Set) (Q : Set) : Set where\n \u2227-intro : P \u2192 Q \u2192 (P \u2227 Q)\n\n\nproof\u2081 : {P Q : Set} \u2192 (P \u2227 Q) \u2192 P\nproof\u2081 (\u2227-intro p q) = p\n\nproof\u2082 : {P Q : Set} \u2192 (P \u2227 Q) \u2192 Q\nproof\u2082 (\u2227-intro p q) = q\n\n\n_\u21d4_ : (P : Set) \u2192 (Q : Set) \u2192 Set\na \u21d4 b = (a \u2192 b) \u2227 (b \u2192 a)\n\n\n\u2227-comm\u2032 : (P Q : Set) \u2192 (P \u2227 Q) \u2192 (Q \u2227 P)\n\u2227-comm\u2032 _ _ (\u2227-intro p q) = \u2227-intro q p\n\n\u2227-comm : (P Q : Set) \u2192 (P \u2227 Q) \u21d4 (Q \u2227 P)\n\u2227-comm P Q = \u2227-intro (\u2227-comm\u2032 P Q) (\u2227-comm\u2032 Q P)\n\n\n\u2227-comm1\u2032 : {P Q : Set} \u2192 (P \u2227 Q) \u2192 (Q \u2227 P)\n\u2227-comm1\u2032 (\u2227-intro p q) = \u2227-intro q p\n\n\u2227-comm1 : {P Q : Set} \u2192 (P \u2227 Q) \u21d4 (Q \u2227 P)\n\u2227-comm1 = \u2227-intro \u2227-comm1\u2032 \u2227-comm1\u2032\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9620d29a7588874f2cd63c3fdb4e0d447c3b59aa","subject":"Cosmetic changes.","message":"Cosmetic changes.\n\nIgnore-this: 22c69a21ac99143a432e5eaf91e798a\n\ndarcs-hash:20110305125148-3bd4e-eaf2fcfcc0c50fbe2e040203577ac1ce04f9e8d4.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Data\/List\/LT-Length\/Induction\/Acc\/WellFoundedInduction.agda","new_file":"src\/FOTC\/Data\/List\/LT-Length\/Induction\/Acc\/WellFoundedInduction.agda","new_contents":"------------------------------------------------------------------------------\n-- Well-founded induction on the relation LTL\n------------------------------------------------------------------------------\n\nopen import FOTC.Base\n\nopen import FOTC.Data.List\nopen import FOTC.Data.List.LT-Length\nopen import FOTC.Data.List.Type\n\nmodule FOTC.Data.List.LT-Length.Induction.Acc.WellFoundedInduction\n (xs<[]\u2192\u22a5 : \u2200 {xs} \u2192 List xs \u2192 \u00ac (LTL xs []))\n (x\u2237xs)\nopen import Data.Nat.NP\nimport Data.Vec.NP as V\nopen V using (Vec; take; drop; drop\u2032; take\u2032; _++_) renaming (swap to vswap)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_; module \u2261-Reasoning)\n\nopen import Data.Bits\nopen import flipbased-implem\nopen import program-distance\n\nK = Bit\nM = Bit\nC = Bit\n\nrecord Adv (S\u2080 S\u2081 S\u2082 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n step\u2081 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2082 : C \u00d7 S\u2081 \u2192 S\u2082\n step\u2083 : S\u2082 \u2192 Bit\n\nrecord Adv\u2081 (S\u2080 S\u2081 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n -- step\u2081 s\u2080 = id , s\u2080\n step\u2082 : C \u00d7 S\u2080 \u2192 S\u2081\n step\u2083 : S\u2081 \u2192 Bit\n\nAdv\u2082 : \u2200 ca \u2192 Set\nAdv\u2082 ca = \u21ba ca (C \u2192 Bit)\n\nmodule Run\u2141\u2032 {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open Adv A\n E : M \u2192 \u21ba 1 C\n E m = toss >>= \u03bb k \u2192 return\u21ba (m xor k)\n run\u2141\u2032 : \u2141? (ca + 1)\n run\u2141\u2032 b = step\u2080 >>= \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n E (m b) >>= \u03bb c \u2192\n return\u21ba (step\u2083 (step\u2082 (c , s\u2081)))}\n\nmodule Run\u2141 {S\u2080 S\u2081 S\u2082 ca} (E : K \u2192 M \u2192 C) (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Adv A\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n let c = E k (m b) in\n step\u2083 (step\u2082 (c , s\u2081))}\n\n run\u2141 : EXP (1 + ca)\n run\u2141 = toss >>= kont\u2080\n\n {- looks wrong\nmodule Run\u2141-Properties {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b k : Bit) where\n open Run\u2141 A\n kont\u2080-not : kont\u2080 b k \u2261 kont\u2080 (not b) (not k)\n kont\u2080-not rewrite xor-not-not b k = {!refl!}\n -}\n\nmodule SymAdv (homPrgDist : HomPrgDist) {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n step\u2081\u2032 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2081\u2032 s\u2080 = case step\u2081 s\u2080 of \u03bb { (m , s\u2081) \u2192 (m \u2218 not , s\u2081) }\n symA : Adv S\u2080 S\u2081 S\u2082 ca\n symA = mk step\u2080 step\u2081\u2032 step\u2082 (not \u2218 step\u2083)\n\n symA\u2032 : Adv S\u2080 S\u2081 S\u2082 ca\n symA\u2032 = mk step\u2080 step\u2081\u2032 step\u2082 step\u2083\n\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n open Run\u2141 _xor_ symA renaming (run\u2141 to runSymA)\n open Run\u2141 _xor_ symA\u2032 renaming (run\u2141 to runSymA\u2032)\n {-\n helper : \u2200 {n} (g\u2080 g\u2081 : EXP n) \u2192 g\u2080 ]-[ g\u2081 \u2192 not\u21ba g\u2080 ]-[ not\u21ba g\u2081\n helper = {!!}\n lem : runA \u21d3 runSymA\n lem A-breaks-E = {!helper (uunSymA\u2032 0b) (runSymA\u2032 1)!}\n where pf : breaks runSymA\n pf = {!!}\n -}\n\nmodule Run\u2141\u2082 {ca} (A : Adv\u2082 ca) (b : Bit) where\n E : Bit \u2192 M \u2192 C\n E k m = m xor k\n\n m : Bit \u2192 Bit\n m = id\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n\n {-\n kont\u2080\u2032 : \u2141? _\n kont\u2080\u2032 k =\n conv-Adv A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n -}\n\n run\u2141\u2082 : EXP (1 + ca)\n run\u2141\u2082 = toss >>= kont\u2080\n\nmodule Run\u2141\u2082-Properties {ca} (A : Adv\u2082 ca) where\n open Run\u2141\u2082 A renaming (run\u2141\u2082 to runA)\n kont\u2080-not : \u2200 b k \u2192 kont\u2080 b k \u2261 kont\u2080 (not b) (not k)\n kont\u2080-not b k rewrite xor-not-not b k = \u2261.refl\n\n open \u2261-Reasoning\n\n lem\u2082 : \u2200 b \u2192 count\u21ba (runA b) \u2261 count\u21ba (runA (not b))\n lem\u2082 b = count\u21ba (runA b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 \u2261.cong\u2082 (_+_ on count\u21ba) (kont\u2080-not b 0b) (kont\u2080-not b 1b) \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (runA (not b)) \u220e\n\n lem\u2083 : Safe\u2141? runA\n lem\u2083 = lem\u2082 0b\n\n -- A specialized version of lem\u2082 (\u2248lem\u2083)\n lem\u2084 : Safe\u2141? (Run\u2141\u2082.run\u2141\u2082 A)\n lem\u2084 = count\u21ba (runA 0b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (kont\u2080 0b 0b) + count\u21ba (kont\u2080 0b 1b)\n \u2261\u27e8 \u2261.cong\u2082 (_+_ on count\u21ba) (kont\u2080-not 0b 0b) (kont\u2080-not 0b 1b) \u27e9\n count\u21ba (kont\u2080 1b 1b) + count\u21ba (kont\u2080 1b 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 1b 1b)) _ \u27e9\n count\u21ba (kont\u2080 1b 0b) + count\u21ba (kont\u2080 1b 1b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (runA 1b) \u220e\n\nconv-Adv : \u2200 {ca S\u2080 S\u2081 S\u2082} \u2192 Adv S\u2080 S\u2081 S\u2082 ca \u2192 Adv\u2082 ca\nconv-Adv A = step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n \u03bb c \u2192 m (step\u2083 (step\u2082 (c , s\u2081)))}\n where open Adv A\n\nmodule Conv-Adv-Props (homPrgDist : HomPrgDist) {ca S\u2080 S\u2081 S\u2082} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n -- open Run\u2141-Properties A\n\n A\u2032 : Adv\u2082 ca\n A\u2032 = conv-Adv A\n open Run\u2141\u2082 A\u2032 using () renaming (kont\u2080 to kont\u2080\u2032; run\u2141\u2082 to runA\u2032)\n\n kont\u2080\u2032\u2032 : \u2200 b k \u2192 EXP ca\n kont\u2080\u2032\u2032 b k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 ((m b) xor k , s\u2081)))}\n\n kont\u2080\u2032\u2032\u2032 : \u2200 b\u2032 \u2192 EXP ca\n kont\u2080\u2032\u2032\u2032 b\u2032 =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 (b\u2032 , s\u2081)))}\n\n {-\n kont\u2032\u2032-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032\u2032 b k)\n kont\u2032\u2032-lem true true = {!!}\n kont\u2032\u2032-lem false true = {!!}\n kont\u2032\u2032-lem true false = {!!}\n kont\u2032\u2032-lem false false = {!!}\n\n kont-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032 b k)\n kont-lem b true = {!!}\n kont-lem b false = {!!}\n\n conv-Adv-lem : runA \u2248\u2141? runA\u2032\n conv-Adv-lem b = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 _+_ (kont-lem b 0b) (kont-lem b 1b) \u27e9\n count\u21ba (kont\u2080\u2032 b 0b) + count\u21ba (kont\u2080\u2032 b 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA\u2032 b) \u220e\n\n conv-Adv-sound : runA \u21d3 runA\u2032\n conv-Adv-sound = ]-[-cong-\u2248\u21ba (conv-Adv-lem 0b) (conv-Adv-lem 1b)\n -}\n-- Cute fact: this is true by computation!\ncount\u21ba-toss->>= : \u2200 {c} (f : \u2141? c) \u2192 count\u21ba (toss >>= f) \u2261 count\u21ba (f 0b) + count\u21ba (f 1b)\ncount\u21ba-toss->>= f = \u2261.refl\n\n{-\nmodule Run\u2141-Properties' {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n lem : count\u21ba (runA b) \u2261 count\u21ba (runA (not b))\n lem = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 (_+_ on count\u21ba) {x = kont\u2080 b 0b} {kont\u2080 (not b) 1b} {kont\u2080 b 1b} {kont\u2080 (not b) 0b} {!!} {!!} \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA (not b)) \u220e\n-}\nopen import program-distance\nopen import Relation.Nullary\n\n\u2047 : \u2200 {n} \u2192 \u21ba n (Bits n)\n-- \u2047 = random\n\u2047 = mk id\n\n\nlem'' : \u2200 {k} (f : Bits k \u2192 Bit) \u2192 #\u27e8 f \u2218 tail \u27e9 \u2261 2* #\u27e8 f \u27e9\nlem'' f = \u2261.refl\n\nlem' : \u2200 {k} (f g : Bits k \u2192 Bit) \u2192 #\u27e8 f \u2218 tail \u27e9 \u2261 #\u27e8 g \u2218 tail \u27e9 \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\nlem' f g pf = 2*-inj (\u2261.trans (lem'' f) (\u2261.trans pf (\u2261.sym (lem'' g))))\n\ndrop-tail : \u2200 k {n a} {A : Set a} \u2192 drop (suc k) {n} \u2257 drop k \u2218 tail {A = A}\ndrop-tail k (x \u2237 xs) = V.drop-\u2237 k x xs\n\nlemdrop\u2032 : \u2200 {k n} (f : Bits n \u2192 Bit) \u2192 #\u27e8 f \u2218 drop\u2032 k \u27e9 \u2261 \u27e82^ k * #\u27e8 f \u27e9 \u27e9\nlemdrop\u2032 {zero} f = \u2261.refl\nlemdrop\u2032 {suc k} f = #\u27e8 f \u2218 drop\u2032 k \u2218 tail \u27e9\n \u2261\u27e8 lem'' (f \u2218 drop\u2032 k) \u27e9\n 2* #\u27e8 f \u2218 drop\u2032 k \u27e9\n \u2261\u27e8 \u2261.cong 2*_ (lemdrop\u2032 {k} f) \u27e9\n 2* \u27e82^ k * #\u27e8 f \u27e9 \u27e9 \u220e\n where open \u2261-Reasoning\n\n\n\n\n\n\n\n-- exchange to independant statements\nlem-flip$-\u2295 : \u2200 {m n a} {A : Set a} (f : \u21ba m (A \u2192 Bit)) (x : \u21ba n A) \u2192\n count\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb \u2261 count\u21ba (f \u229b x)\nlem-flip$-\u2295 {m} {n} f x = \u2261.sym (\n count\u21ba fx\n \u2261\u27e8 #-+ {m} {n} (run\u21ba fx) \u27e9\n sum {m} (\u03bb xs \u2192 #\u27e8_\u27e9 {n} (\u03bb ys \u2192 run\u21ba fx (xs ++ ys)))\n \u2261\u27e8 sum-sum {m} {n} (Bool.to\u2115 \u2218 run\u21ba fx) \u27e9\n sum {n} (\u03bb ys \u2192 #\u27e8_\u27e9 {m} (\u03bb xs \u2192 run\u21ba fx (xs ++ ys)))\n \u2261\u27e8 sum-\u2257\u2082 (\u03bb ys xs \u2192 Bool.to\u2115 (run\u21ba fx (xs ++ ys)))\n (\u03bb ys xs \u2192 Bool.to\u2115 (run\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb (ys ++ xs)))\n (\u03bb ys xs \u2192 \u2261.cong Bool.to\u2115 (lem\u2081 xs ys)) \u27e9\n sum {n} (\u03bb ys \u2192 #\u27e8_\u27e9 {m} (\u03bb xs \u2192 run\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb (ys ++ xs)))\n \u2261\u27e8 \u2261.sym (#-+ {n} {m} (run\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb)) \u27e9\n count\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb\n \u220e )\n where open \u2261-Reasoning\n fx = f \u229b x\n lem\u2081 : \u2200 xs ys \u2192 run\u21ba f (take m (xs ++ ys)) (run\u21ba x (drop m (xs ++ ys)))\n \u2261 run\u21ba f (drop n (ys ++ xs)) (run\u21ba x (take n (ys ++ xs)))\n lem\u2081 xs ys rewrite V.take-++ m xs ys | V.drop-++ m xs ys\n | V.take-++ n ys xs | V.drop-++ n ys xs = \u2261.refl\n\n\u2248\u1d2c\u2032-toss : \u2200 b \u2192 \u27ea b \u27eb\u1d30 \u27e8xor\u27e9 toss \u2248\u1d2c\u2032 toss\n\u2248\u1d2c\u2032-toss true Adv = \u2115\u00b0.+-comm (count\u21ba (Adv true)) _\n\u2248\u1d2c\u2032-toss false Adv = \u2261.refl\n\n\u2248\u1d2c-toss : \u2200 b \u2192 \u27ea b \u27eb\u1d30 \u27e8xor\u27e9 toss \u2248\u1d2c toss\n\u2248\u1d2c-toss b Adv = \u2248\u1d2c\u2032-toss b (return\u1d30 \u2218 Adv)\n\n-- should be equivalent to #-comm if \u27ea m \u27eb\u1d30 \u27e8\u2295\u27e9 x were convertible to \u27ea _\u2295_ m \u00b7 x \u27eb\n\u2248\u1d2c-\u2047 : \u2200 {k} (m : Bits k) \u2192 \u27ea m \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u2047\n\u2248\u1d2c-\u2047 {zero} _ _ = \u2261.refl\n\u2248\u1d2c-\u2047 {suc k} (h \u2237 m) Adv\n rewrite \u2248\u1d2c-\u2047 m (Adv \u2218 _\u2237_ (h xor 0b))\n | \u2248\u1d2c-\u2047 m (Adv \u2218 _\u2237_ (h xor 1b))\n = \u2248\u1d2c\u2032-toss h (\u03bb x \u2192 \u27ea Adv \u2218 _\u2237_ x \u00b7 \u2047 \u27eb)\n\nopen OperationSyntax\n\n_\u27e8\u2219\u27e9_ : Op \u2192 \u2200 {m n} \u2192 Endo (\u21ba m (Bits n))\nf \u27e8\u2219\u27e9 g = \u27ea _\u2219_ f \u00b7 g \u27eb\n\n\u2248\u1d2c-op-\u2047 : \u2200 {k} f \u2192 f \u27e8\u2219\u27e9 \u2047 \u2248\u1d2c \u2047 {k}\n\u2248\u1d2c-op-\u2047 = flip #-op\n\n\u2248\u1d2c-\u2047\u2082 : \u2200 {k} (m\u2080 m\u2081 : Bits k) \u2192 \u27ea m\u2080 \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea m\u2081 \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2082 {k} m\u2080 m\u2081 = \u2248\u1d2c.trans {k} (\u2248\u1d2c-\u2047 m\u2080) (\u2248\u1d2c.sym {k} (\u2248\u1d2c-\u2047 m\u2081))\n\n\u2248\u1d2c-\u2047\u2083 : \u2200 {k} (m : Bit \u2192 Bits k) (b : Bit) \u2192 \u27ea m b \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea m (not b) \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2083 m b = \u2248\u1d2c-\u2047\u2082 (m b) (m (not b))\n\n\u2248\u1d2c-\u2047\u2084 : \u2200 {k} (m : Bits k \u00d7 Bits k) (b : Bit) \u2192 \u27ea proj m b \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea proj m (not b) \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2084 = \u2248\u1d2c-\u2047\u2083 \u2218 proj\n","old_contents":"module single-bit-one-time-pad where\n\nopen import Function\nopen import Data.Bool.NP as Bool\nopen import Data.Product renaming (map to <_\u00d7_>)\nopen import Data.Nat.NP\nimport Data.Vec.NP as V\nopen V using (Vec; take; drop; drop\u2032; take\u2032; _++_) renaming (swap to vswap)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_; module \u2261-Reasoning)\n\nopen import Data.Bits\nopen import flipbased-implem\nopen import program-distance\n\nK = Bit\nM = Bit\nC = Bit\n\nrecord Adv (S\u2080 S\u2081 S\u2082 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n step\u2081 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2082 : C \u00d7 S\u2081 \u2192 S\u2082\n step\u2083 : S\u2082 \u2192 Bit\n\nrecord Adv\u2081 (S\u2080 S\u2081 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n -- step\u2081 s\u2080 = id , s\u2080\n step\u2082 : C \u00d7 S\u2080 \u2192 S\u2081\n step\u2083 : S\u2081 \u2192 Bit\n\nAdv\u2082 : \u2200 ca \u2192 Set\nAdv\u2082 ca = \u21ba ca (C \u2192 Bit)\n\nmodule Run\u2141\u2032 {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open Adv A\n E : M \u2192 \u21ba 1 C\n E m = toss >>= \u03bb k \u2192 return\u21ba (m xor k)\n run\u2141\u2032 : \u2141? (ca + 1)\n run\u2141\u2032 b = step\u2080 >>= \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n E (m b) >>= \u03bb c \u2192\n return\u21ba (step\u2083 (step\u2082 (c , s\u2081)))}\n\nmodule Run\u2141 {S\u2080 S\u2081 S\u2082 ca} (E : K \u2192 M \u2192 C) (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Adv A\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n let c = E k (m b) in\n step\u2083 (step\u2082 (c , s\u2081))}\n\n run\u2141 : EXP (1 + ca)\n run\u2141 = toss >>= kont\u2080\n\n {- looks wrong\nmodule Run\u2141-Properties {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b k : Bit) where\n open Run\u2141 A\n kont\u2080-not : kont\u2080 b k \u2261 kont\u2080 (not b) (not k)\n kont\u2080-not rewrite xor-not-not b k = {!refl!}\n -}\n\nmodule SymAdv (homPrgDist : HomPrgDist) {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n step\u2081\u2032 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2081\u2032 s\u2080 = case step\u2081 s\u2080 of \u03bb { (m , s\u2081) \u2192 (m \u2218 not , s\u2081) }\n symA : Adv S\u2080 S\u2081 S\u2082 ca\n symA = mk step\u2080 step\u2081\u2032 step\u2082 (not \u2218 step\u2083)\n\n symA\u2032 : Adv S\u2080 S\u2081 S\u2082 ca\n symA\u2032 = mk step\u2080 step\u2081\u2032 step\u2082 step\u2083\n\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n open Run\u2141 _xor_ symA renaming (run\u2141 to runSymA)\n open Run\u2141 _xor_ symA\u2032 renaming (run\u2141 to runSymA\u2032)\n {-\n helper : \u2200 {n} (g\u2080 g\u2081 : EXP n) \u2192 g\u2080 ]-[ g\u2081 \u2192 not\u21ba g\u2080 ]-[ not\u21ba g\u2081\n helper = {!!}\n lem : runA \u21d3 runSymA\n lem A-breaks-E = {!helper (uunSymA\u2032 0b) (runSymA\u2032 1)!}\n where pf : breaks runSymA\n pf = {!!}\n -}\n\nmodule Run\u2141\u2082 {ca} (A : Adv\u2082 ca) (b : Bit) where\n E : Bit \u2192 M \u2192 C\n E k m = m xor k\n\n m : Bit \u2192 Bit\n m = id\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n\n {-\n kont\u2080\u2032 : \u2141? _\n kont\u2080\u2032 k =\n conv-Adv A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n -}\n\n run\u2141\u2082 : EXP (1 + ca)\n run\u2141\u2082 = toss >>= kont\u2080\n\nmodule Run\u2141\u2082-Properties {ca} (A : Adv\u2082 ca) where\n open Run\u2141\u2082 A renaming (run\u2141\u2082 to runA)\n kont\u2080-not : \u2200 b k \u2192 kont\u2080 b k \u2261 kont\u2080 (not b) (not k)\n kont\u2080-not b k rewrite xor-not-not b k = \u2261.refl\n\n open \u2261-Reasoning\n\n lem\u2082 : \u2200 b \u2192 count\u21ba (runA b) \u2261 count\u21ba (runA (not b))\n lem\u2082 b = count\u21ba (runA b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 \u2261.cong\u2082 (_+_ on count\u21ba) (kont\u2080-not b 0b) (kont\u2080-not b 1b) \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (runA (not b)) \u220e\n\n lem\u2083 : Safe\u2141? runA\n lem\u2083 = lem\u2082 0b\n\n -- A specialized version of lem\u2082 (\u2248lem\u2083)\n lem\u2084 : Safe\u2141? (Run\u2141\u2082.run\u2141\u2082 A)\n lem\u2084 = count\u21ba (runA 0b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (kont\u2080 0b 0b) + count\u21ba (kont\u2080 0b 1b)\n \u2261\u27e8 \u2261.cong\u2082 (_+_ on count\u21ba) (kont\u2080-not 0b 0b) (kont\u2080-not 0b 1b) \u27e9\n count\u21ba (kont\u2080 1b 1b) + count\u21ba (kont\u2080 1b 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 1b 1b)) _ \u27e9\n count\u21ba (kont\u2080 1b 0b) + count\u21ba (kont\u2080 1b 1b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (runA 1b) \u220e\n\nconv-Adv : \u2200 {ca S\u2080 S\u2081 S\u2082} \u2192 Adv S\u2080 S\u2081 S\u2082 ca \u2192 Adv\u2082 ca\nconv-Adv A = step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n \u03bb c \u2192 m (step\u2083 (step\u2082 (c , s\u2081)))}\n where open Adv A\n\nmodule Conv-Adv-Props (homPrgDist : HomPrgDist) {ca S\u2080 S\u2081 S\u2082} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n -- open Run\u2141-Properties A\n\n A\u2032 : Adv\u2082 ca\n A\u2032 = conv-Adv A\n open Run\u2141\u2082 A\u2032 using () renaming (kont\u2080 to kont\u2080\u2032; run\u2141\u2082 to runA\u2032)\n\n kont\u2080\u2032\u2032 : \u2200 b k \u2192 EXP ca\n kont\u2080\u2032\u2032 b k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 ((m b) xor k , s\u2081)))}\n\n kont\u2080\u2032\u2032\u2032 : \u2200 b\u2032 \u2192 EXP ca\n kont\u2080\u2032\u2032\u2032 b\u2032 =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 (b\u2032 , s\u2081)))}\n\n {-\n kont\u2032\u2032-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032\u2032 b k)\n kont\u2032\u2032-lem true true = {!!}\n kont\u2032\u2032-lem false true = {!!}\n kont\u2032\u2032-lem true false = {!!}\n kont\u2032\u2032-lem false false = {!!}\n\n kont-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032 b k)\n kont-lem b true = {!!}\n kont-lem b false = {!!}\n\n conv-Adv-lem : runA \u2248\u2141? runA\u2032\n conv-Adv-lem b = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 _+_ (kont-lem b 0b) (kont-lem b 1b) \u27e9\n count\u21ba (kont\u2080\u2032 b 0b) + count\u21ba (kont\u2080\u2032 b 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA\u2032 b) \u220e\n\n conv-Adv-sound : runA \u21d3 runA\u2032\n conv-Adv-sound = ]-[-cong-\u2248\u21ba (conv-Adv-lem 0b) (conv-Adv-lem 1b)\n -}\n-- Cute fact: this is true by computation!\ncount\u21ba-toss->>= : \u2200 {c} (f : \u2141? c) \u2192 count\u21ba (toss >>= f) \u2261 count\u21ba (f 0b) + count\u21ba (f 1b)\ncount\u21ba-toss->>= f = \u2261.refl\n\n{-\nmodule Run\u2141-Properties' {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n lem : count\u21ba (runA b) \u2261 count\u21ba (runA (not b))\n lem = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 (_+_ on count\u21ba) {x = kont\u2080 b 0b} {kont\u2080 (not b) 1b} {kont\u2080 b 1b} {kont\u2080 (not b) 0b} {!!} {!!} \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA (not b)) \u220e\n-}\nopen import program-distance\nopen import Relation.Nullary\n\n\u2047 : \u2200 {n} \u2192 \u21ba n (Bits n)\n\u2047 = random\n\n\nlem'' : \u2200 {k} (f : Bits k \u2192 Bit) \u2192 #\u27e8 f \u2218 tail \u27e9 \u2261 2* #\u27e8 f \u27e9\nlem'' f = \u2261.refl\n\nlem' : \u2200 {k} (f g : Bits k \u2192 Bit) \u2192 #\u27e8 f \u2218 tail \u27e9 \u2261 #\u27e8 g \u2218 tail \u27e9 \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\nlem' f g pf = 2*-inj (\u2261.trans (lem'' f) (\u2261.trans pf (\u2261.sym (lem'' g))))\n\ndrop-tail : \u2200 k {n a} {A : Set a} \u2192 drop (suc k) {n} \u2257 drop k \u2218 tail {A = A}\ndrop-tail k (x \u2237 xs) = V.drop-\u2237 k x xs\n\nlemdrop\u2032 : \u2200 {k n} (f : Bits n \u2192 Bit) \u2192 #\u27e8 f \u2218 drop\u2032 k \u27e9 \u2261 \u27e82^ k * #\u27e8 f \u27e9 \u27e9\nlemdrop\u2032 {zero} f = \u2261.refl\nlemdrop\u2032 {suc k} f = #\u27e8 f \u2218 drop\u2032 k \u2218 tail \u27e9\n \u2261\u27e8 lem'' (f \u2218 drop\u2032 k) \u27e9\n 2* #\u27e8 f \u2218 drop\u2032 k \u27e9\n \u2261\u27e8 \u2261.cong 2*_ (lemdrop\u2032 {k} f) \u27e9\n 2* \u27e82^ k * #\u27e8 f \u27e9 \u27e9 \u220e\n where open \u2261-Reasoning\n\n\n\n\n\n\n\n-- exchange to independant statements\nlem-flip$-\u2295 : \u2200 {m n a} {A : Set a} (f : \u21ba m (A \u2192 Bit)) (x : \u21ba n A) \u2192\n count\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb \u2261 count\u21ba (f \u229b x)\nlem-flip$-\u2295 {m} {n} f x = \u2261.sym (\n count\u21ba fx\n \u2261\u27e8 #-+ {m} {n} (run\u21ba fx) \u27e9\n sum {m} (\u03bb xs \u2192 #\u27e8_\u27e9 {n} (\u03bb ys \u2192 run\u21ba fx (xs ++ ys)))\n \u2261\u27e8 sum-sum {m} {n} (Bool.to\u2115 \u2218 run\u21ba fx) \u27e9\n sum {n} (\u03bb ys \u2192 #\u27e8_\u27e9 {m} (\u03bb xs \u2192 run\u21ba fx (xs ++ ys)))\n \u2261\u27e8 sum-\u2257\u2082 (\u03bb ys xs \u2192 Bool.to\u2115 (run\u21ba fx (xs ++ ys)))\n (\u03bb ys xs \u2192 Bool.to\u2115 (run\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb (ys ++ xs)))\n (\u03bb ys xs \u2192 \u2261.cong Bool.to\u2115 (lem\u2081 xs ys)) \u27e9\n sum {n} (\u03bb ys \u2192 #\u27e8_\u27e9 {m} (\u03bb xs \u2192 run\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb (ys ++ xs)))\n \u2261\u27e8 \u2261.sym (#-+ {n} {m} (run\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb)) \u27e9\n count\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb\n \u220e )\n where open \u2261-Reasoning\n fx = f \u229b x\n lem\u2081 : \u2200 xs ys \u2192 run\u21ba f (take m (xs ++ ys)) (run\u21ba x (drop m (xs ++ ys)))\n \u2261 run\u21ba f (drop n (ys ++ xs)) (run\u21ba x (take n (ys ++ xs)))\n lem\u2081 xs ys rewrite V.take-++ m xs ys | V.drop-++ m xs ys\n | V.take-++ n ys xs | V.drop-++ n ys xs = \u2261.refl\n\n\u2248\u1d2c\u2032-toss : \u2200 b \u2192 \u27ea b \u27eb\u1d30 \u27e8xor\u27e9 toss \u2248\u1d2c\u2032 toss\n\u2248\u1d2c\u2032-toss true Adv = \u2115\u00b0.+-comm (count\u21ba (Adv true)) _\n\u2248\u1d2c\u2032-toss false Adv = \u2261.refl\n\n\u2248\u1d2c-toss : \u2200 b \u2192 \u27ea b \u27eb\u1d30 \u27e8xor\u27e9 toss \u2248\u1d2c toss\n\u2248\u1d2c-toss b Adv = \u2248\u1d2c\u2032-toss b (return\u1d30 \u2218 Adv)\n\n-- should be equivalent to #-comm if \u27ea m \u27eb\u1d30 \u27e8\u2295\u27e9 x were convertible to \u27ea _\u2295_ m \u00b7 x \u27eb\n\u2248\u1d2c-\u2047 : \u2200 {k} (m : Bits k) \u2192 \u27ea m \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u2047\n\u2248\u1d2c-\u2047 {zero} _ _ = \u2261.refl\n\u2248\u1d2c-\u2047 {suc k} (h \u2237 m) Adv\n rewrite \u2248\u1d2c-\u2047 m (Adv \u2218 _\u2237_ (h xor 0b))\n | \u2248\u1d2c-\u2047 m (Adv \u2218 _\u2237_ (h xor 1b))\n = \u2248\u1d2c\u2032-toss h (\u03bb x \u2192 \u27ea Adv \u2218 _\u2237_ x \u00b7 \u2047 \u27eb)\n\n\u2248\u1d2c-\u2047\u2082 : \u2200 {k} (m\u2080 m\u2081 : Bits k) \u2192 \u27ea m\u2080 \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea m\u2081 \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2082 {k} m\u2080 m\u2081 = \u2248\u1d2c.trans {k} (\u2248\u1d2c-\u2047 m\u2080) (\u2248\u1d2c.sym {k} (\u2248\u1d2c-\u2047 m\u2081))\n\n\u2248\u1d2c-\u2047\u2083 : \u2200 {k} (m : Bit \u2192 Bits k) (b : Bit) \u2192 \u27ea m b \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea m (not b) \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2083 m b = \u2248\u1d2c-\u2047\u2082 (m b) (m (not b))\n\n\u2248\u1d2c-\u2047\u2084 : \u2200 {k} (m : Bits k \u00d7 Bits k) (b : Bit) \u2192 \u27ea proj m b \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea proj m (not b) \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2084 = \u2248\u1d2c-\u2047\u2083 \u2218 proj\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"c8a0b87298dd1bfc0bfdd36ebc05d9f2b14d82a1","subject":"Typo.","message":"Typo.\n\nIgnore-this: 7ff4279cea3751561b0a45c24a588967\n\ndarcs-hash:20101130074907-3bd4e-2ced29f128dcfb342686615a9d6c60073dd9ec60.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/PA\/Base.agda","new_file":"Draft\/PA\/Base.agda","new_contents":"------------------------------------------------------------------------------\n-- Peano arithmetic base\n------------------------------------------------------------------------------\n\nmodule Draft.PA.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfixl 10 _*_\ninfixl 9 _+_\n\n------------------------------------------------------------------------------\n-- PA universe\n-- N.B. The following module is exported by this module.\nopen import Common.Universe public renaming ( D to PA )\n\n-- Logical constants\n-- N.B. The module is exported by this module.\nopen import Common.LogicalConstants public\n\n-- Non-logical constants\npostulate\n zero : PA\n succ : PA \u2192 PA\n _+_ : PA \u2192 PA \u2192 PA\n _*_ : PA \u2192 PA \u2192 PA\n\n-- The PA equality\n-- The PA equality is the propositional identity on the PA universe.\n-- N.B. The following modules are exported by this module.\n-- N.B. We are not using the refl and sym properties because they are\n-- not stated in the proper axioms (see below).\nopen import Common.Relation.Binary.PropositionalEquality public\n using ( _\u2261_ )\nopen import Common.Relation.Binary.PropositionalEquality.Properties public\n using ( trans )\n\n-- Proper axioms\n-- (From Elliott Mendelson. Introduction to mathematical\n-- logic. Chapman & Hall, 4th edition, 1997, p. 155)\n\n-- S\u2081. x\u2081 = x\u2082 \u2192 x\u2081 = x\u2083 \u2192 x\u2082 = x\u2083\n-- S\u2082. x\u2081 = x\u2082 \u2192 succ x\u2081 = succ x\u2082\n-- S\u2083. 0 \u2260 succ x\n-- S\u2084. succ x\u2081 = succ x\u2082 \u2192 x\u2081 = x\u2082\n-- S\u2085. x\u2081 + 0 = x\u2081\n-- S\u2086. x\u2081 + succ x\u2082 = succ (x\u2081 + x\u2082)\n-- S\u2087. x\u2081 * 0 = 0\n-- S\u2088. x\u2081 * succ x\u2082 = (x\u2081 * x\u2082) + x\u2081\n-- S\u2089. P(0) \u2192 (\u2200x.P(x) \u2192 P(succ x)) \u2192 \u2200x.P(x)\n\npostulate\n -- S\u2081: This axiom is the transitivity property imported from\n -- Common.Relation.Binary.PropositionalEquality.Properties.\n\n -- S\u2082: This axiom is not required by the ATPs.\n\n S\u2083 : \u2200 {x} \u2192 \u00ac (zero \u2261 succ x)\n\n S\u2084 : \u2200 {x\u2081 x\u2082} \u2192 succ x\u2081 \u2261 succ x\u2082 \u2192 x\u2081 \u2261 x\u2082\n\n S\u2085 : \u2200 x \u2192 zero + x \u2261 x\n S\u2086 : \u2200 x\u2081 x\u2082 \u2192 succ x\u2081 + x\u2082 \u2261 succ (x\u2081 + x\u2082)\n\n S\u2087 : \u2200 x \u2192 zero * x \u2261 zero\n S\u2088 : \u2200 x\u2081 x\u2082 \u2192 succ x\u2081 * x\u2082 \u2261 x\u2082 + x\u2081 * x\u2082\n\n -- S\u2089: Instead of the induction schema we will use the inductive predicate N\n -- (see Draft.PA.Type).\n\n{-# ATP axiom S\u2083 #-}\n{-# ATP axiom S\u2084 #-}\n{-# ATP axiom S\u2085 #-}\n{-# ATP axiom S\u2086 #-}\n{-# ATP axiom S\u2087 #-}\n{-# ATP axiom S\u2088 #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Peano arithmetic base\n------------------------------------------------------------------------------\n\nmodule Draft.PA.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfixl 10 _*_\ninfixl 9 _+_\n\n------------------------------------------------------------------------------\n-- PA universe\n-- N.B. The following module is exported by this module.\nopen import Common.Universe public renaming ( D to PA )\n\n-- Logical constants\n-- N.B. The module is exported by this module.\nopen import Common.LogicalConstants public\n\n-- Non-logical constants of PA.\npostulate\n zero : PA\n succ : PA \u2192 PA\n _+_ : PA \u2192 PA \u2192 PA\n _*_ : PA \u2192 PA \u2192 PA\n\n-- The PA equality\n-- The PA equality is the propositional identity on the PA universe.\n-- N.B. The following modules are exported by this module.\n-- N.B. We are not using the refl and sym properties because they are\n-- not stated in the proper axioms (see below).\nopen import Common.Relation.Binary.PropositionalEquality public\n using ( _\u2261_ )\nopen import Common.Relation.Binary.PropositionalEquality.Properties public\n using ( trans )\n\n-- Proper axioms\n-- (From Elliott Mendelson. Introduction to mathematical\n-- logic. Chapman & Hall, 4th edition, 1997, p. 155)\n\n-- S\u2081. x\u2081 = x\u2082 \u2192 x\u2081 = x\u2083 \u2192 x\u2082 = x\u2083\n-- S\u2082. x\u2081 = x\u2082 \u2192 succ x\u2081 = succ x\u2082\n-- S\u2083. 0 \u2260 succ x\n-- S\u2084. succ x\u2081 = succ x\u2082 \u2192 x\u2081 = x\u2082\n-- S\u2085. x\u2081 + 0 = x\u2081\n-- S\u2086. x\u2081 + succ x\u2082 = succ (x\u2081 + x\u2082)\n-- S\u2087. x\u2081 * 0 = 0\n-- S\u2088. x\u2081 * succ x\u2082 = (x\u2081 * x\u2082) + x\u2081\n-- S\u2089. P(0) \u2192 (\u2200x.P(x) \u2192 P(succ x)) \u2192 \u2200x.P(x)\n\npostulate\n -- S\u2081: This axiom is the transitivity property imported from\n -- Common.Relation.Binary.PropositionalEquality.Properties.\n\n -- S\u2082: This axiom is not required by the ATPs.\n\n S\u2083 : \u2200 {x} \u2192 \u00ac (zero \u2261 succ x)\n\n S\u2084 : \u2200 {x\u2081 x\u2082} \u2192 succ x\u2081 \u2261 succ x\u2082 \u2192 x\u2081 \u2261 x\u2082\n\n S\u2085 : \u2200 x \u2192 zero + x \u2261 x\n S\u2086 : \u2200 x\u2081 x\u2082 \u2192 succ x\u2081 + x\u2082 \u2261 succ (x\u2081 + x\u2082)\n\n S\u2087 : \u2200 x \u2192 zero * x \u2261 zero\n S\u2088 : \u2200 x\u2081 x\u2082 \u2192 succ x\u2081 * x\u2082 \u2261 x\u2082 + x\u2081 * x\u2082\n\n -- S\u2089: Instead of the induction schema we will use the inductive predicate N\n -- see (Draft.PA.Type)\n\n{-# ATP axiom S\u2083 #-}\n{-# ATP axiom S\u2084 #-}\n{-# ATP axiom S\u2085 #-}\n{-# ATP axiom S\u2086 #-}\n{-# ATP axiom S\u2087 #-}\n{-# ATP axiom S\u2088 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3a1143b9db99b9ec7e1431f08aab91f27cf9bd7a","subject":"Document Postulate.Extensionality regularly.","message":"Document Postulate.Extensionality regularly.\n\nOld-commit-hash: 35222e92088dbd2313ec7252e50cd68e85b340bf\n","repos":"inc-lc\/ilc-agda","old_file":"Postulate\/Extensionality.agda","new_file":"Postulate\/Extensionality.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Postulate extensionality of functions.\n--\n-- Justification on Agda mailing list:\n-- http:\/\/permalink.gmane.org\/gmane.comp.lang.agda\/2343\n------------------------------------------------------------------------\n\nmodule Postulate.Extensionality where\n\n\nopen import Relation.Binary.PropositionalEquality\n\npostulate ext : \u2200 {a b} \u2192 Extensionality a b\n\n-- Convenience of using extensionality 3 times in a row\n-- (using it twice in a row is moderately tolerable)\next\u00b3 : \u2200\n {A : Set}\n {B : A \u2192 Set}\n {C : (a : A) \u2192 B a \u2192 Set }\n {D : (a : A) \u2192 (b : B a) \u2192 C a b \u2192 Set}\n {f g : (a : A) \u2192 (b : B a) \u2192 (c : C a b) \u2192 D a b c} \u2192\n ((a : A) (b : B a) (c : C a b) \u2192 f a b c \u2261 g a b c) \u2192 f \u2261 g\n\next\u00b3 fabc=gabc = ext (\u03bb a \u2192 ext (\u03bb b \u2192 ext (\u03bb c \u2192 fabc=gabc a b c)))\n","old_contents":"module Postulate.Extensionality where\n\n-- POSTULATE EXTENSIONALITY\n--\n-- Justification on Agda mailing list:\n-- http:\/\/permalink.gmane.org\/gmane.comp.lang.agda\/2343\n\nopen import Relation.Binary.PropositionalEquality\n\npostulate ext : \u2200 {a b} \u2192 Extensionality a b\n\n-- Convenience of using extensionality 3 times in a row\n-- (using it twice in a row is moderately tolerable)\next\u00b3 : \u2200\n {A : Set}\n {B : A \u2192 Set}\n {C : (a : A) \u2192 B a \u2192 Set }\n {D : (a : A) \u2192 (b : B a) \u2192 C a b \u2192 Set}\n {f g : (a : A) \u2192 (b : B a) \u2192 (c : C a b) \u2192 D a b c} \u2192\n ((a : A) (b : B a) (c : C a b) \u2192 f a b c \u2261 g a b c) \u2192 f \u2261 g\n\next\u00b3 fabc=gabc = ext (\u03bb a \u2192 ext (\u03bb b \u2192 ext (\u03bb c \u2192 fabc=gabc a b c)))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"e7d0ffc6976dc3270e58e80326cf78dc7e9dcb2e","subject":"flat-funs: put the type interface in the universe module","message":"flat-funs: put the type interface in the universe module\n","repos":"crypto-agda\/crypto-agda","old_file":"flat-funs.agda","new_file":"flat-funs.agda","new_contents":"module flat-funs where\n\nopen import Data.Nat\nopen import Data.Bits using (Bits)\nimport Level as L\nimport Function as F\nopen import composable\nopen import vcomp\nopen import universe\n\nrecord FlatFuns {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n universe : Universe T\n _`\u2192_ : T \u2192 T \u2192 Set\n infix 0 _`\u2192_\n open Universe universe public\n\nrecord FlatFunsOps {t} {T : Set t} (\u266dFuns : FlatFuns T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n field\n idO : \u2200 {A} \u2192 A `\u2192 A\n\n _>>>_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (B `\u2192 C) \u2192 (A `\u2192 C)\n\n _***_ : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n\n -- Fanout\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n\n constBits : \u2200 {n} \u2192 Bits n \u2192 `\u22a4 `\u2192 `Bits n\n\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f , g > = f &&& g\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = f *** g\n\nopen import Data.Unit using (\u22a4)\nopen import Data.Vec\nopen import Data.Bits\nopen import Data.Fin using (Fin) renaming (_+_ to _+\u1da0_)\nopen import Data.Product renaming (zip to \u00d7-zip; map to \u00d7-map)\nopen import Function\n\n-\u2192- : Set \u2192 Set \u2192 Set\n-\u2192- A B = A \u2192 B\n\n_\u2192\u1da0_ : \u2115 \u2192 \u2115 \u2192 Set\n_\u2192\u1da0_ i o = Fin i \u2192 Fin o\n\nmapArr : \u2200 {s t} {S : Set s} {T : Set t} (F G : T \u2192 S)\n \u2192 (S \u2192 S \u2192 Set) \u2192 (T \u2192 T \u2192 Set)\nmapArr F G _`\u2192_ A B = F A `\u2192 G B\n\nfun\u266dFuns : FlatFuns Set\nfun\u266dFuns = mk Set-U -\u2192-\n\nbitsFun\u266dFuns : FlatFuns \u2115\nbitsFun\u266dFuns = mk Bits-U _\u2192\u1d47_\n\nfinFun\u266dFuns : FlatFuns \u2115\nfinFun\u266dFuns = mk Fin-U _\u2192\u1da0_\n\nfun\u266dOps : FlatFunsOps fun\u266dFuns\nfun\u266dOps = mk id (\u03bb f g x \u2192 g (f x)) (\u03bb f g \u2192 \u00d7-map f g) _&&&_ proj\u2081 proj\u2082 const\n where\n _&&&_ : \u2200 {A B C : Set} \u2192 (A \u2192 B) \u2192 (A \u2192 C) \u2192 A \u2192 B \u00d7 C\n (f &&& g) x = (f x , g x)\n\nbitsFun\u266dOps : FlatFunsOps bitsFun\u266dFuns\nbitsFun\u266dOps = mk id (\u03bb f g \u2192 g \u2218 f) (VComposable._***_ bitsFunVComp) _&&&_ (\u03bb {A} \u2192 take A)\n (\u03bb {A} \u2192 drop A) (\u03bb xs _ \u2192 xs ++ [] {- ugly? -})\n where\n open FlatFuns bitsFun\u266dFuns\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n (f &&& g) x = (f x ++ g x)\n\n\u00d7-\u266dFuns : \u2200 {s t} {S : Set s} {T : Set t} \u2192 FlatFuns S \u2192 FlatFuns T \u2192 FlatFuns (S \u00d7 T)\n\u00d7-\u266dFuns funs-S funs-T = mk (\u00d7-U S.universe T.universe)\n (\u03bb { (A\u2080 , A\u2081) (B\u2080 , B\u2081) \u2192 (A\u2080 S.`\u2192 B\u2080) \u00d7 (A\u2081 T.`\u2192 B\u2081) })\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7\u22a4-\u266dFuns : \u2200 {s} {S : Set s} \u2192 FlatFuns S \u2192 FlatFuns \u22a4 \u2192 FlatFuns S\n\u00d7\u22a4-\u266dFuns funs-S funs-T = mk S.universe\n (\u03bb A B \u2192 (A S.`\u2192 B) \u00d7 (_ T.`\u2192 _))\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7-\u266dOps : \u2200 {s t} {S : Set s} {T : Set t} {funs-S : FlatFuns S} {funs-T : FlatFuns T}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-T\n \u2192 FlatFunsOps (\u00d7-\u266dFuns funs-S funs-T)\n\u00d7-\u266dOps ops-S ops-T = mk (S.idO , T.idO) (\u00d7-zip S._>>>_ T._>>>_) (\u00d7-zip S._***_ T._***_)\n (\u00d7-zip S._&&&_ T._&&&_) (S.fst , T.fst) (S.snd , T.snd)\n (S.constBits &&& T.constBits)\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-T\n open FlatFunsOps fun\u266dOps\n\n\u00d7\u22a4-\u266dOps : \u2200 {s} {S : Set s} {funs-S : FlatFuns S} {funs-\u22a4 : FlatFuns \u22a4}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-\u22a4\n \u2192 FlatFunsOps (\u00d7\u22a4-\u266dFuns funs-S funs-\u22a4)\n\u00d7\u22a4-\u266dOps ops-S ops-\u22a4 = mk (S.idO , T.idO) (\u00d7-zip S._>>>_ T._>>>_) (\u00d7-zip S._***_ T._***_)\n (\u00d7-zip S._&&&_ T._&&&_) (S.fst , T.fst) (S.snd , T.snd)\n (S.constBits &&& T.constBits)\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-\u22a4\n open FlatFunsOps fun\u266dOps\n\n\nconstFuns : Set \u2192 FlatFuns \u22a4\nconstFuns A = mk \u22a4-U (\u03bb _ _ \u2192 A)\n\ntimeOps : FlatFunsOps (constFuns \u2115)\ntimeOps = mk 0 _+_ _\u2294_ _\u2294_ 0 0 (const 0)\n\nspaceOps : FlatFunsOps (constFuns \u2115)\nspaceOps = mk 0 _+_ _+_ _+_ 0 0 (\u03bb {n} _ \u2192 n)\n\ntime\u00d7spaceOps : FlatFunsOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-\u266dOps timeOps spaceOps\n","old_contents":"module flat-funs where\n\nopen import Data.Nat\nimport Level as L\nopen import composable\nopen import vcomp\n\nrecord FlatFuns {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n `\u22a4 : T\n `Bit : T\n _`\u00d7_ : T \u2192 T \u2192 T\n _`^_ : T \u2192 \u2115 \u2192 T\n _`\u2192_ : T \u2192 T \u2192 Set\n\n `Vec : T \u2192 \u2115 \u2192 T\n `Vec A n = A `^ n\n\n `Bits : \u2115 \u2192 T\n `Bits n = `Bit `^ n\n\n infixr 2 _`\u00d7_\n infixl 2 _`^_\n infix 0 _`\u2192_\n\nrecord FlatFunsOps {t} {T : Set t} (\u266dFuns : FlatFuns T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n field\n idO : \u2200 {A} \u2192 A `\u2192 A\n isComposable : Composable _`\u2192_\n isVComposable : VComposable _`\u00d7_ _`\u2192_\n\n -- Fanout\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n\n open Composable isComposable\n open VComposable isVComposable\n\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f , g > = f &&& g\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = f *** g\n\n open Composable isComposable public\n open VComposable isVComposable public\n\nopen import Data.Unit using (\u22a4)\nopen import Data.Vec\nopen import Data.Bits\nopen import Data.Product\nopen import Function\n\nfun\u266dFuns : FlatFuns Set\nfun\u266dFuns = mk \u22a4 Bit _\u00d7_ Vec (\u03bb A B \u2192 A \u2192 B)\n\nbitsFun\u266dFuns : FlatFuns \u2115\nbitsFun\u266dFuns = mk 0 1 _+_ _*_ (\u03bb i o \u2192 Bits i \u2192 Bits o)\n\nfun\u266dOps : FlatFunsOps fun\u266dFuns\nfun\u266dOps = mk id funComp funVComp _&&&_ proj\u2081 proj\u2082\n where\n _&&&_ : \u2200 {A B C : Set} \u2192 (A \u2192 B) \u2192 (A \u2192 C) \u2192 A \u2192 B \u00d7 C\n (f &&& g) x = (f x , g x)\n\nbitsFun\u266dOps : FlatFunsOps bitsFun\u266dFuns\nbitsFun\u266dOps = mk id bitsFunComp bitsFunVComp _&&&_ (\u03bb {A} \u2192 take A) (\u03bb {A} \u2192 drop A)\n where\n open FlatFuns bitsFun\u266dFuns\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n (f &&& g) x = (f x ++ g x)\n\n\u00d7-\u266dFuns : \u2200 {s t} {S : Set s} {T : Set t} \u2192 FlatFuns S \u2192 FlatFuns T \u2192 FlatFuns (S \u00d7 T)\n\u00d7-\u266dFuns funs-S funs-T = ?\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7-\u266dOps : \u2200 {s t} {S : Set s} {T : Set t} {funs-S : FlatFuns S} {funs-T : FlatFuns T}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-T\n \u2192 FlatFunsOps (\u00d7-\u266dFuns funs-S funs-T)\n\u00d7-\u266dOps ops-S ops-T = ?\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-T\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"77d92178ad638a41ae32099024888c6bf121bb0b","subject":"Desc model: generic proof that map id = id.","message":"Desc model: generic proof that map id = id.","repos":"kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram,kwangkim\/pigment","old_file":"models\/Desc.agda","new_file":"models\/Desc.agda","new_contents":"\n {-# OPTIONS --type-in-type #-}\n\nmodule Desc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A : Set}{B : A -> Set}(f : (a : A) -> B a)(g : (a : A) -> B a)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata Desc : Set where\n id : Desc\n const : Set -> Desc\n prod : Desc -> Desc -> Desc\n sigma : (S : Set) -> (S -> Desc) -> Desc\n pi : (S : Set) -> (S -> Desc) -> Desc\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|]_ : Desc -> Set -> Set\n[| id |] Z = Z\n[| const X |] Z = X\n[| prod D D' |] Z = [| D |] Z * [| D' |] Z\n[| sigma S T |] Z = Sigma S (\\s -> [| T s |] Z)\n[| pi S T |] Z = (s : S) -> [| T s |] Z\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata Mu (D : Desc) : Set where\n con : [| D |] (Mu D) -> Mu D\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : (D : Desc)(X : Set)(P : X -> Set) -> [| D |] X -> Set\nAll id X P x = P x\nAll (const Z) X P x = Z\nAll (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')\nAll (sigma S T) X P (a , b) = All (T a) X P b\nAll (pi S T) X P f = (s : S) -> All (T s) X P (f s)\n\nall : (D : Desc)(X : Set)(P : X -> Set)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x\nall id X P R x = R x\nall (const Z) X P R z = z\nall (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'\nall (sigma S T) X P R (a , b) = all (T a) X P R b\nall (pi S T) X P R f = \\ s -> all (T s) X P R (f s)\n\n--********************************************\n-- Map\n--********************************************\n\nmap : (D : Desc)(X Y : Set)(f : X -> Y)(v : [| D |] X) -> [| D |] Y\nmap id X Y sig x = sig x\nmap (const Z) X Y sig z = z\nmap (prod D D') X Y sig (d , d') = map D X Y sig d , map D' X Y sig d'\nmap (sigma S T) X Y sig (a , b) = (a , map (T a) X Y sig b)\nmap (pi S T) X Y sig f = \\x -> map (T x) X Y sig (f x)\n\n\nproof-map-id : (D : Desc)(X : Set)(v : [| D |] X) -> map D X X (\\x -> x) v == v\nproof-map-id id X v = refl\nproof-map-id (const Z) X v = refl\nproof-map-id (prod D D') X (v , v') = cong2 (\\x y -> (x , y)) (proof-map-id D X v) (proof-map-id D' X v')\nproof-map-id (sigma S T) X (a , b) = cong (\\x -> (a , x)) (proof-map-id (T a) X b) \nproof-map-id (pi S T) X f = reflFun (\\a -> map (T a) X X (\\x -> x) (f a)) f (\\a -> proof-map-id (T a) X (f a))\n\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n{-\ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms (con xs) = ms xs (all D (Mu D) P (\\x -> induction D P ms x) xs)\n-}\n\nmodule Elim (D : Desc)\n (P : Mu D -> Set)\n (ms : (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x))\n where\n\n mutual\n induction : (x : Mu D) -> P x\n induction (con xs) = ms xs (hyps D xs)\n \n hyps : (D' : Desc)\n (xs : [| D' |] (Mu D)) ->\n All D' (Mu D) P xs\n hyps id x = induction x\n hyps (const Z) z = z\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (sigma S T) (a , b) = hyps (T a) b\n hyps (pi S T) f = \\s -> hyps (T s) (f s)\n \ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms x = Elim.induction D P ms x\n\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Suc : NatConst\n\nnatCases : NatConst -> Desc\nnatCases Ze = const Unit\nnatCases Suc = id\n\nNatD : Desc\nNatD = sigma NatConst natCases\n\nNat : Set\nNat = Mu NatD\n\nze : Nat\nze = con (Ze , Void)\n\nsuc : Nat -> Nat\nsuc n = con (Suc , n)\n\n--****************\n-- List\n--****************\n\ndata ListConst : Set where\n Nil : ListConst\n Cons : ListConst\n\nlistCases : Set -> ListConst -> Desc\nlistCases X Nil = const Unit\nlistCases X Cons = sigma X (\\_ -> id)\n\nListD : Set -> Desc\nListD X = sigma ListConst (listCases X)\n\nList : Set -> Set\nList X = Mu (ListD X)\n\nnil : {X : Set} -> List X\nnil = con ( Nil , Void )\n\ncons : {X : Set} -> X -> List X -> List X\ncons x t = con ( Cons , ( x , t ))\n\n--****************\n-- Tree\n--****************\n\ndata TreeConst : Set where\n Leaf : TreeConst\n Node : TreeConst\n\ntreeCases : Set -> TreeConst -> Desc\ntreeCases X Leaf = const Unit\ntreeCases X Node = sigma X (\\_ -> prod id id)\n\nTreeD : Set -> Desc\nTreeD X = sigma TreeConst (treeCases X)\n\nTree : Set -> Set\nTree X = Mu (TreeD X)\n\nleaf : {X : Set} -> Tree X\nleaf = con (Leaf , Void)\n\nnode : {X : Set} -> X -> Tree X -> Tree X -> Tree X\nnode x le ri = con (Node , (x , (le , ri)))\n\n--********************************************\n-- Finite sets\n--********************************************\n\nEnumU : Set\nEnumU = Nat\n\nnilE : EnumU\nnilE = ze\n\nconsE : EnumU -> EnumU\nconsE e = suc e\n\n{-\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n-}\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\ncasesSpi : (xs : [| NatD |] Nat) -> \n All NatD Nat (\\e -> (P' : EnumT e -> Set) -> Set) xs -> \n (P' : EnumT (con xs) -> Set) -> Set\ncasesSpi (Ze , Void) hs P' = Unit\ncasesSpi (Suc , n) hs P' = P' EZe * hs (\\e -> P' (ESu e))\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi e P = induction NatD (\\e -> (P : EnumT e -> Set) -> Set) casesSpi e P\n\n{-\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n-}\n\n\ncasesSwitch : (xs : [| NatD |] Nat) ->\n All NatD Nat (\\e -> (P' : EnumT e -> Set)(b' : spi e P')(x' : EnumT e) -> P' x') xs ->\n (P' : EnumT (con xs) -> Set)(b' : spi (con xs) P')(x' : EnumT (con xs)) -> P' x'\ncasesSwitch (Ze , Void) hs P' b' ()\ncasesSwitch (Suc , n) hs P' b' EZe = fst b'\ncasesSwitch (Suc , n) hs P' b' (ESu e') = hs (\\e -> P' (ESu e)) (snd b') e'\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch e P b x = induction NatD (\\e -> (P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x) casesSwitch e P b x\n\n{-\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n-}\n\n--********************************************\n-- Tagged description\n--********************************************\n\nTagDesc : Set\nTagDesc = Sigma EnumU (\\e -> spi e (\\_ -> Desc))\n\ntoDesc : TagDesc -> Desc\ntoDesc (B , F) = sigma (EnumT B) (\\e -> switch B (\\_ -> Desc) F e)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (D : Desc)\n (T : Set) ->\n ([| D |] T -> T) ->\n (Mu D) -> T\ncata D T phi x = induction D (\\_ -> T) (\\x ms -> phi (replace D T x ms)) x\n where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\\_ -> T) xs) -> [| D' |] T\n replace id T x y = y\n replace (const Z) T z z' = z'\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : TagDesc -> (X : Set) -> TagDesc\n(e , D) ** X = consE e , (const X , D)\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : (D : TagDesc)(X Y : Set) ->\n (X -> Mu (toDesc (D ** Y))) ->\n [| toDesc (D ** X) |] (Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\napply (E , B) X Y sig (EZe , x) = sig x\napply (E , B) X Y sig (ESu n , t) = con (ESu n , t)\n\nsubst : (D : TagDesc)(X Y : Set) ->\n Mu (toDesc (D ** X)) ->\n (X -> Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\nsubst D X Y x sig = cata (toDesc (D ** X)) (Mu (toDesc (D ** Y))) (apply D X Y sig) x\n","old_contents":"\n {-# OPTIONS --type-in-type #-}\n\nmodule Desc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata Desc : Set where\n id : Desc\n const : Set -> Desc\n prod : Desc -> Desc -> Desc\n sigma : (S : Set) -> (S -> Desc) -> Desc\n pi : (S : Set) -> (S -> Desc) -> Desc\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|]_ : Desc -> Set -> Set\n[| id |] Z = Z\n[| const X |] Z = X\n[| prod D D' |] Z = [| D |] Z * [| D' |] Z\n[| sigma S T |] Z = Sigma S (\\s -> [| T s |] Z)\n[| pi S T |] Z = (s : S) -> [| T s |] Z\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata Mu (D : Desc) : Set where\n con : [| D |] (Mu D) -> Mu D\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : (D : Desc)(X : Set)(P : X -> Set) -> [| D |] X -> Set\nAll id X P x = P x\nAll (const Z) X P x = Z\nAll (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')\nAll (sigma S T) X P (a , b) = All (T a) X P b\nAll (pi S T) X P f = (s : S) -> All (T s) X P (f s)\n\nall : (D : Desc)(X : Set)(P : X -> Set)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x\nall id X P R x = R x\nall (const Z) X P R z = z\nall (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'\nall (sigma S T) X P R (a , b) = all (T a) X P R b\nall (pi S T) X P R f = \\ s -> all (T s) X P R (f s)\n\n--********************************************\n-- Map\n--********************************************\n\nmap : (D : Desc)(X Y : Set)(f : X -> Y)(v : [| D |] X) -> [| D |] Y\nmap id X Y sig x = sig x\nmap (const Z) X Y sig z = z\nmap (prod D D') X Y sig (d , d') = map D X Y sig d , map D' X Y sig d'\nmap (sigma S T) X Y sig (a , b) = (a , map (T a) X Y sig b)\nmap (pi S T) X Y sig f = \\x -> map (T x) X Y sig (f x)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n{-\ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms (con xs) = ms xs (all D (Mu D) P (\\x -> induction D P ms x) xs)\n-}\n\nmodule Elim (D : Desc)\n (P : Mu D -> Set)\n (ms : (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x))\n where\n\n mutual\n induction : (x : Mu D) -> P x\n induction (con xs) = ms xs (hyps D xs)\n \n hyps : (D' : Desc)\n (xs : [| D' |] (Mu D)) ->\n All D' (Mu D) P xs\n hyps id x = induction x\n hyps (const Z) z = z\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (sigma S T) (a , b) = hyps (T a) b\n hyps (pi S T) f = \\s -> hyps (T s) (f s)\n \ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms x = Elim.induction D P ms x\n\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Suc : NatConst\n\nnatCases : NatConst -> Desc\nnatCases Ze = const Unit\nnatCases Suc = id\n\nNatD : Desc\nNatD = sigma NatConst natCases\n\nNat : Set\nNat = Mu NatD\n\nze : Nat\nze = con (Ze , Void)\n\nsuc : Nat -> Nat\nsuc n = con (Suc , n)\n\n--****************\n-- List\n--****************\n\ndata ListConst : Set where\n Nil : ListConst\n Cons : ListConst\n\nlistCases : Set -> ListConst -> Desc\nlistCases X Nil = const Unit\nlistCases X Cons = sigma X (\\_ -> id)\n\nListD : Set -> Desc\nListD X = sigma ListConst (listCases X)\n\nList : Set -> Set\nList X = Mu (ListD X)\n\nnil : {X : Set} -> List X\nnil = con ( Nil , Void )\n\ncons : {X : Set} -> X -> List X -> List X\ncons x t = con ( Cons , ( x , t ))\n\n--****************\n-- Tree\n--****************\n\ndata TreeConst : Set where\n Leaf : TreeConst\n Node : TreeConst\n\ntreeCases : Set -> TreeConst -> Desc\ntreeCases X Leaf = const Unit\ntreeCases X Node = sigma X (\\_ -> prod id id)\n\nTreeD : Set -> Desc\nTreeD X = sigma TreeConst (treeCases X)\n\nTree : Set -> Set\nTree X = Mu (TreeD X)\n\nleaf : {X : Set} -> Tree X\nleaf = con (Leaf , Void)\n\nnode : {X : Set} -> X -> Tree X -> Tree X -> Tree X\nnode x le ri = con (Node , (x , (le , ri)))\n\n--********************************************\n-- Finite sets\n--********************************************\n\nEnumU : Set\nEnumU = Nat\n\nnilE : EnumU\nnilE = ze\n\nconsE : EnumU -> EnumU\nconsE e = suc e\n\n{-\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n-}\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\ncasesSpi : (xs : [| NatD |] Nat) -> \n All NatD Nat (\\e -> (P' : EnumT e -> Set) -> Set) xs -> \n (P' : EnumT (con xs) -> Set) -> Set\ncasesSpi (Ze , Void) hs P' = Unit\ncasesSpi (Suc , n) hs P' = P' EZe * hs (\\e -> P' (ESu e))\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi e P = induction NatD (\\e -> (P : EnumT e -> Set) -> Set) casesSpi e P\n\n{-\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n-}\n\n\ncasesSwitch : (xs : [| NatD |] Nat) ->\n All NatD Nat (\\e -> (P' : EnumT e -> Set)(b' : spi e P')(x' : EnumT e) -> P' x') xs ->\n (P' : EnumT (con xs) -> Set)(b' : spi (con xs) P')(x' : EnumT (con xs)) -> P' x'\ncasesSwitch (Ze , Void) hs P' b' ()\ncasesSwitch (Suc , n) hs P' b' EZe = fst b'\ncasesSwitch (Suc , n) hs P' b' (ESu e') = hs (\\e -> P' (ESu e)) (snd b') e'\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch e P b x = induction NatD (\\e -> (P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x) casesSwitch e P b x\n\n{-\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n-}\n\n--********************************************\n-- Tagged description\n--********************************************\n\nTagDesc : Set\nTagDesc = Sigma EnumU (\\e -> spi e (\\_ -> Desc))\n\ntoDesc : TagDesc -> Desc\ntoDesc (B , F) = sigma (EnumT B) (\\e -> switch B (\\_ -> Desc) F e)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (D : Desc)\n (T : Set) ->\n ([| D |] T -> T) ->\n (Mu D) -> T\ncata D T phi x = induction D (\\_ -> T) (\\x ms -> phi (replace D T x ms)) x\n where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\\_ -> T) xs) -> [| D' |] T\n replace id T x y = y\n replace (const Z) T z z' = z'\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : TagDesc -> (X : Set) -> TagDesc\n(e , D) ** X = consE e , (const X , D)\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : (D : TagDesc)(X Y : Set) ->\n (X -> Mu (toDesc (D ** Y))) ->\n [| toDesc (D ** X) |] (Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\napply (E , B) X Y sig (EZe , x) = sig x\napply (E , B) X Y sig (ESu n , t) = con (ESu n , t)\n\nsubst : (D : TagDesc)(X Y : Set) ->\n Mu (toDesc (D ** X)) ->\n (X -> Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\nsubst D X Y x sig = cata (toDesc (D ** X)) (Mu (toDesc (D ** Y))) (apply D X Y sig) x\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"785dddce82b3dc294f8d52a3a8a96b64366b1ac4","subject":"Remove more","message":"Remove more\n\nNo Structure in Base.\n\nOld-commit-hash: 81cee315c1ade9b842f0400fbc2bd215a189e8dc\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Equivalence.agda","new_file":"Base\/Change\/Equivalence.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\nmodule Structure where\n\n module _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set a\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n module _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\n-- Extension Point: None (currently). Do we need to allow plugins to customize\n-- this concept?\nStructure : Set\nStructure = Unit\n\nmodule Structure (unused : Structure) where\n\n module _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set a\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n module _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = lemma\n where\n open \u2261-Reasoning\n open import Postulate.Extensionality\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"2aa331ecc397f402c621e7b4b53886eb5ca53e91","subject":"Use HOAS-enabled helpers for nested abstractions.","message":"Use HOAS-enabled helpers for nested abstractions.\n\nOld-commit-hash: f5006b448dcc30a8ff103cf5de501e945e14faa1\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Term.agda","new_file":"Parametric\/Change\/Term.agda","new_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Constant\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\nDiffStructure : Set\nDiffStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))\n\nApplyStructure : Set\nApplyStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\n\nmodule Structure\n (diff-base : DiffStructure)\n (apply-base : ApplyStructure)\n where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n lift-diff-apply :\n \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4) \u00d7 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-diff-apply {base \u03b9} = diff-base , apply-base\n lift-diff-apply {\u03c3 \u21d2 \u03c4} =\n let\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff\u03c3 {\u0393}) s t\n _\u229d\u03c4_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff\u03c4 {\u0393}) s t\n _\u2295\u03c3_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply\u03c3 {\u0393}) \u0394t t\n _\u2295\u03c4_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply\u03c4 {\u0393}) \u0394t t\n in\n abs\u2084 (\u03bb g f x \u0394x \u2192 app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x)\n ,\n abs\u2083 (\u03bb \u0394h h y \u2192 app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))\n\n lift-diff :\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n\n lift-diff = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply {\u03c4} {\u0393})\n\n lift-apply :\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply {\u03c4} {\u0393})\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Constant\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\nDiffStructure : Set\nDiffStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))\n\nApplyStructure : Set\nApplyStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\n\nmodule Structure\n (diff-base : DiffStructure)\n (apply-base : ApplyStructure)\n where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n lift-diff-apply :\n \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4) \u00d7 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-diff-apply {base \u03b9} = diff-base , apply-base\n lift-diff-apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff\u03c3 {\u0393}) s t\n _\u229d\u03c4_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff\u03c4 {\u0393}) s t\n _\u2295\u03c3_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply\u03c3 {\u0393}) \u0394t t\n _\u2295\u03c4_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply\u03c4 {\u0393}) \u0394t t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\n lift-diff :\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n\n lift-diff = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply {\u03c4} {\u0393})\n\n lift-apply :\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply {\u03c4} {\u0393})\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"24ce7b62d29582982f0f0c2694ebd10a473113aa","subject":"IND-CCA2: Hide the Rat code","message":"IND-CCA2: Hide the Rat code\n","repos":"crypto-agda\/crypto-agda","old_file":"Game\/IND-CCA2.agda","new_file":"Game\/IND-CCA2.agda","new_contents":"\n{-# OPTIONS --without-K #-}\n\nopen import Type\nopen import Data.Bit\nopen import Data.Maybe\nopen import Data.Product\nopen import Data.Unit\n\nopen import Data.Nat.NP\n--open import Rat\n\nopen import Explore.Type\nopen import Explore.Explorable\nopen import Explore.Product\nopen Operators\nopen import Relation.Binary.PropositionalEquality\n\nmodule Game.IND-CCA2\n (PubKey : \u2605)\n (SecKey : \u2605)\n (Message : \u2605)\n (CipherText : \u2605)\n\n -- randomness supply for, encryption, key-generation, adversary, adversary state\n (R\u2091 R\u2096 R\u2090 : \u2605)\n (KeyGen : R\u2096 \u2192 PubKey \u00d7 SecKey)\n (Enc : PubKey \u2192 Message \u2192 R\u2091 \u2192 CipherText)\n (Dec : SecKey \u2192 CipherText \u2192 Message)\n\nwhere\nopen import Game.CCA-Common Message CipherText\n \nAdv : \u2605\nAdv = R\u2090 \u2192 PubKey \u2192 Strategy ((Message \u00d7 Message) \u00d7 (CipherText \u2192 Strategy Bit))\n\n{-\nValid-Adv : Adv \u2192 Set\nValid-Adv adv = \u2200 {r\u2090 r\u2093 pk c} \u2192 Valid (\u03bb x \u2192 x \u2262 c) {!!}\n-}\n\nR : \u2605\nR = R\u2090 \u00d7 R\u2096 \u00d7 R\u2091\n\nGame : \u2605\nGame = Adv \u2192 R \u2192 Bit\n\n\n\u2141 : Bit \u2192 Game\n\u2141 b m (r\u2090 , r\u2096 , r\u2091) with KeyGen r\u2096\n... | pk , sk = b\u2032 where\n open Eval Dec sk\n \n ev = eval (m r\u2090 pk)\n mb = proj (proj\u2081 ev) b\n d = proj\u2082 ev\n\n c = Enc pk mb r\u2091\n b\u2032 = eval (d c)\n\n \nmodule Advantage\n (\u03bc\u2091 : Explore\u2080 R\u2091)\n (\u03bc\u2096 : Explore\u2080 R\u2096)\n (\u03bc\u2090 : Explore\u2080 R\u2090)\n where\n \u03bcR : Explore\u2080 R\n \u03bcR = \u03bc\u2090 \u00d7\u1d49 \u03bc\u2096 \u00d7\u1d49 \u03bc\u2091\n \n module \u03bcR = FromExplore\u2080 \u03bcR\n \n run : Bit \u2192 Adv \u2192 \u2115\n run b adv = \u03bcR.count (\u2141 b adv)\n \n Advantage : Adv \u2192 \u2115\n Advantage adv = dist (run 0b adv) (run 1b adv)\n \n --Advantage\u211a : Adv \u2192 \u211a\n --Advantage\u211a adv = Advantage adv \/ \u03bcR.Card\n \n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"\n{-# OPTIONS --without-K #-}\n\nopen import Type\nopen import Data.Bit\nopen import Data.Maybe\nopen import Data.Product\nopen import Data.Unit\n\nopen import Data.Nat.NP\nopen import Rat\n\nopen import Explore.Type\nopen import Explore.Explorable\nopen import Explore.Product\nopen Operators\nopen import Relation.Binary.PropositionalEquality\n\nmodule Game.IND-CCA2\n (PubKey : \u2605)\n (SecKey : \u2605)\n (Message : \u2605)\n (CipherText : \u2605)\n\n -- randomness supply for, encryption, key-generation, adversary, adversary state\n (R\u2091 R\u2096 R\u2090 : \u2605)\n (KeyGen : R\u2096 \u2192 PubKey \u00d7 SecKey)\n (Enc : PubKey \u2192 Message \u2192 R\u2091 \u2192 CipherText)\n (Dec : SecKey \u2192 CipherText \u2192 Message)\n\nwhere\nopen import Game.CCA-Common Message CipherText\n \nAdv : \u2605\nAdv = R\u2090 \u2192 PubKey \u2192 Strategy ((Message \u00d7 Message) \u00d7 (CipherText \u2192 Strategy Bit))\n\n{-\nValid-Adv : Adv \u2192 Set\nValid-Adv adv = \u2200 {r\u2090 r\u2093 pk c} \u2192 Valid (\u03bb x \u2192 x \u2262 c) {!!}\n-}\n\nR : \u2605\nR = R\u2090 \u00d7 R\u2096 \u00d7 R\u2091\n\nGame : \u2605\nGame = Adv \u2192 R \u2192 Bit\n\n\n\u2141 : Bit \u2192 Game\n\u2141 b m (r\u2090 , r\u2096 , r\u2091) with KeyGen r\u2096\n... | pk , sk = b\u2032 where\n open Eval Dec sk\n \n ev = eval (m r\u2090 pk)\n mb = proj (proj\u2081 ev) b\n d = proj\u2082 ev\n\n c = Enc pk mb r\u2091\n b\u2032 = eval (d c)\n\n \nmodule Advantage\n (\u03bc\u2091 : Explore\u2080 R\u2091)\n (\u03bc\u2096 : Explore\u2080 R\u2096)\n (\u03bc\u2090 : Explore\u2080 R\u2090)\n where\n \u03bcR : Explore\u2080 R\n \u03bcR = \u03bc\u2090 \u00d7\u1d49 \u03bc\u2096 \u00d7\u1d49 \u03bc\u2091\n \n module \u03bcR = FromExplore\u2080 \u03bcR\n \n run : Bit \u2192 Adv \u2192 \u2115\n run b adv = \u03bcR.count (\u2141 b adv)\n \n Advantage : Adv \u2192 \u2115\n Advantage adv = dist (run 0b adv) (run 1b adv)\n \n Advantage\u211a : Adv \u2192 \u211a\n Advantage\u211a adv = Advantage adv \/ \u03bcR.Card\n \n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"43f907923a585a467516163bdb20896c50287af0","subject":"removing dep","message":"removing dep\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"cast-inert.agda","new_file":"cast-inert.agda","new_contents":"open import Nat\nopen import Prelude\nopen import core\nopen import contexts\nopen import typed-expansion\nopen import lemmas-gcomplete\nopen import lemmas-complete\nopen import progress-checks\nopen import finality\n\nmodule cast-inert where\n -- if a term is compelete and well typed, then the casts inside are all\n -- identity casts and there are no failed casts\n cast-inert : \u2200{\u0394 \u0393 d \u03c4} \u2192\n \u0393 gcomplete \u2192\n d dcomplete \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n cast-id d -- all casts are id, and there are no failed casts\n cast-inert gc dc TAConst = CIConst\n cast-inert gc dc (TAVar x\u2081) = CIVar\n cast-inert gc (DCLam dc x\u2081) (TALam x\u2082 wt) = CILam (cast-inert (gcomp-extend gc x\u2081 x\u2082) dc wt)\n cast-inert gc (DCAp dc dc\u2081) (TAAp wt wt\u2081) = CIAp (cast-inert gc dc wt)\n (cast-inert gc dc\u2081 wt\u2081)\n cast-inert gc () (TAEHole x x\u2081)\n cast-inert gc () (TANEHole x wt x\u2081)\n cast-inert gc (DCCast dc x x\u2081) (TACast wt x\u2082)\n with eq-complete-consist x x\u2081 x\u2082\n ... | refl = CICast (cast-inert gc dc wt)\n cast-inert gc () (TAFailedCast wt x x\u2081 x\u2082)\n\n -- relates expressions to the same thing with all identity casts removed\n data no-id-casts : dhexp \u2192 dhexp \u2192 Set where\n NICConst : no-id-casts c c\n NICVar : \u2200{x} \u2192 no-id-casts (X x) (X x)\n NICLam : \u2200{x \u03c4 d d'} \u2192 no-id-casts d d' \u2192 no-id-casts (\u00b7\u03bb x [ \u03c4 ] d) (\u00b7\u03bb x [ \u03c4 ] d')\n NICHole : \u2200{u} \u2192 no-id-casts (\u2987\u2988\u27e8 u \u27e9) (\u2987\u2988\u27e8 u \u27e9)\n NICNEHole : \u2200{d d' u} \u2192 no-id-casts d d' \u2192 no-id-casts (\u2987 d \u2988\u27e8 u \u27e9) (\u2987 d' \u2988\u27e8 u \u27e9)\n NICAp : \u2200{d1 d2 d1' d2'} \u2192 no-id-casts d1 d1' \u2192 no-id-casts d2 d2' \u2192 no-id-casts (d1 \u2218 d2) (d1' \u2218 d2')\n NICCast : \u2200{d d' \u03c4} \u2192 no-id-casts d d' \u2192 no-id-casts (d \u27e8 \u03c4 \u21d2 \u03c4 \u27e9) d'\n NICFailed : \u2200{d d' \u03c41 \u03c42} \u2192 no-id-casts d d' \u2192 no-id-casts (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9) (d' \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n\n -- removing identity casts doesn't change the type\n no-id-casts-type : \u2200{\u0393 \u0394 d \u03c4 d' } \u2192 \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n no-id-casts d d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n no-id-casts-type TAConst NICConst = TAConst\n no-id-casts-type (TAVar x\u2081) NICVar = TAVar x\u2081\n no-id-casts-type (TALam x\u2081 wt) (NICLam nic) = TALam x\u2081 (no-id-casts-type wt nic)\n no-id-casts-type (TAAp wt wt\u2081) (NICAp nic nic\u2081) = TAAp (no-id-casts-type wt nic) (no-id-casts-type wt\u2081 nic\u2081)\n no-id-casts-type (TAEHole x x\u2081) NICHole = TAEHole x x\u2081\n no-id-casts-type (TANEHole x wt x\u2081) (NICNEHole nic) = TANEHole x (no-id-casts-type wt nic) x\u2081\n no-id-casts-type (TACast wt x) (NICCast nic) = no-id-casts-type wt nic\n no-id-casts-type (TAFailedCast wt x x\u2081 x\u2082) (NICFailed nic) = TAFailedCast (no-id-casts-type wt nic) x x\u2081 x\u2082\n","old_contents":"open import Nat\nopen import Prelude\nopen import core\nopen import contexts\nopen import typed-expansion\nopen import lemmas-gcomplete\nopen import lemmas-complete\nopen import progress-checks\nopen import finality\nopen import preservation\n\nmodule cast-inert where\n -- if a term is compelete and well typed, then the casts inside are all\n -- identity casts and there are no failed casts\n cast-inert : \u2200{\u0394 \u0393 d \u03c4} \u2192\n \u0393 gcomplete \u2192\n d dcomplete \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n cast-id d -- all casts are id, and there are no failed casts\n cast-inert gc dc TAConst = CIConst\n cast-inert gc dc (TAVar x\u2081) = CIVar\n cast-inert gc (DCLam dc x\u2081) (TALam x\u2082 wt) = CILam (cast-inert (gcomp-extend gc x\u2081 x\u2082) dc wt)\n cast-inert gc (DCAp dc dc\u2081) (TAAp wt wt\u2081) = CIAp (cast-inert gc dc wt)\n (cast-inert gc dc\u2081 wt\u2081)\n cast-inert gc () (TAEHole x x\u2081)\n cast-inert gc () (TANEHole x wt x\u2081)\n cast-inert gc (DCCast dc x x\u2081) (TACast wt x\u2082)\n with eq-complete-consist x x\u2081 x\u2082\n ... | refl = CICast (cast-inert gc dc wt)\n cast-inert gc () (TAFailedCast wt x x\u2081 x\u2082)\n\n -- relates expressions to the same thing with all identity casts removed\n data no-id-casts : dhexp \u2192 dhexp \u2192 Set where\n NICConst : no-id-casts c c\n NICVar : \u2200{x} \u2192 no-id-casts (X x) (X x)\n NICLam : \u2200{x \u03c4 d d'} \u2192 no-id-casts d d' \u2192 no-id-casts (\u00b7\u03bb x [ \u03c4 ] d) (\u00b7\u03bb x [ \u03c4 ] d')\n NICHole : \u2200{u} \u2192 no-id-casts (\u2987\u2988\u27e8 u \u27e9) (\u2987\u2988\u27e8 u \u27e9)\n NICNEHole : \u2200{d d' u} \u2192 no-id-casts d d' \u2192 no-id-casts (\u2987 d \u2988\u27e8 u \u27e9) (\u2987 d' \u2988\u27e8 u \u27e9)\n NICAp : \u2200{d1 d2 d1' d2'} \u2192 no-id-casts d1 d1' \u2192 no-id-casts d2 d2' \u2192 no-id-casts (d1 \u2218 d2) (d1' \u2218 d2')\n NICCast : \u2200{d d' \u03c4} \u2192 no-id-casts d d' \u2192 no-id-casts (d \u27e8 \u03c4 \u21d2 \u03c4 \u27e9) d'\n NICFailed : \u2200{d d' \u03c41 \u03c42} \u2192 no-id-casts d d' \u2192 no-id-casts (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9) (d' \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n\n -- removing identity casts doesn't change the type\n no-id-casts-type : \u2200{\u0393 \u0394 d \u03c4 d' } \u2192 \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n no-id-casts d d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n no-id-casts-type TAConst NICConst = TAConst\n no-id-casts-type (TAVar x\u2081) NICVar = TAVar x\u2081\n no-id-casts-type (TALam x\u2081 wt) (NICLam nic) = TALam x\u2081 (no-id-casts-type wt nic)\n no-id-casts-type (TAAp wt wt\u2081) (NICAp nic nic\u2081) = TAAp (no-id-casts-type wt nic) (no-id-casts-type wt\u2081 nic\u2081)\n no-id-casts-type (TAEHole x x\u2081) NICHole = TAEHole x x\u2081\n no-id-casts-type (TANEHole x wt x\u2081) (NICNEHole nic) = TANEHole x (no-id-casts-type wt nic) x\u2081\n no-id-casts-type (TACast wt x) (NICCast nic) = no-id-casts-type wt nic\n no-id-casts-type (TAFailedCast wt x x\u2081 x\u2082) (NICFailed nic) = TAFailedCast (no-id-casts-type wt nic) x x\u2081 x\u2082\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"01f4ca20dd3c0c79513911d02d3bdc3c76f870d7","subject":"Abstraction and application of stability of terms (i. e., the derivative of a stable term is its nil change in any honest environment)","message":"Abstraction and application of stability of terms\n(i. e., the derivative of a stable term is its nil change in any\nhonest environment)\n\nCaution: Validity assumption is introduced into the close-enough\nrelation. An optimized derivative expecting volatility will NOT be\npropositionally equal to the original derivative. They will only\nact identically on consistent \u0394-environments and valid future\nargument pairs.\n\nOld-commit-hash: b34946276b4d9fed70d1a900dfef8c90df0bb7ad\n","repos":"inc-lc\/ilc-agda","old_file":"experimental\/ExplicitNil.agda","new_file":"experimental\/ExplicitNil.agda","new_contents":"{-\nCommunicate to derivatives that changes to certain arguments\nare always nil (i. e., certain arguments are stable).\n-}\n\nmodule ExplicitNil where\n\nopen import NatBag\n\nopen import Data.Bool\nopen import Data.Product hiding (map)\nopen import Data.Nat using (\u2115)\nopen import Data.Unit using (\u22a4 ; tt)\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\n-- Useful statements about Boolean\n\u2227-proj\u2081 : \u2200 {a b} \u2192 a \u2227 b \u2261 true \u2192 a \u2261 true\n\u2227-proj\u2081 {false} {_} ()\n\u2227-proj\u2081 {true} {false} ()\n\u2227-proj\u2081 {true} {true} eq = refl\n\n\u2227-proj\u2082 : \u2200 {a b} \u2192 a \u2227 b \u2261 true \u2192 b \u2261 true\n\u2227-proj\u2082 {false} {_} ()\n\u2227-proj\u2082 {true} {false} ()\n\u2227-proj\u2082 {true} {true} eq = refl\n\n-- Extending correctness-on-closed-terms\nextended-correctness : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192\n (f : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (x : Term \u0393 \u03c4\u2081)\n {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} {consistency : Consistent-\u0394env \u03c1} \u2192\n \u27e6 weaken \u0393\u227c\u0394\u0393 f \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 x \u27e7 \u03c1)\n \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 x \u27e7 \u03c1) (\u27e6 derive x \u27e7 \u03c1)\n \u2261 (\u27e6 weaken \u0393\u227c\u0394\u0393 f \u27e7 \u03c1 \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u03c1)\n (\u27e6 weaken \u0393\u227c\u0394\u0393 x \u27e7 \u03c1 \u27e6\u2295\u27e7 \u27e6 derive x \u27e7 \u03c1)\n\nextended-correctness {\u03c4\u2081} {\u03c4\u2082} {\u0393} f x {\u03c1} {consistency} = sym (proj\u2082\n (validity-of-derive\u2032 \u03c1 {consistency} f\n (\u27e6 weaken \u0393\u227c\u0394\u0393 x \u27e7 \u03c1) (\u27e6 derive x \u27e7 \u03c1)\n (validity-of-derive\u2032 \u03c1 {consistency} x)))\n\n-- Debug tool\nabsurd! : \u2200 {A B : Set} \u2192 B \u2192 A \u2192 A \u2192 B\nabsurd! b _ _ = b\n\n\ndata Args : (\u03c4 : Type) \u2192 Set where\n \u2205-nat : Args nats\n \u2205-bag : Args bags\n abide : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192 (args : Args \u03c4\u2082) \u2192 Args (\u03c4\u2081 \u21d2 \u03c4\u2082)\n alter : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192 (args : Args \u03c4\u2082) \u2192 Args (\u03c4\u2081 \u21d2 \u03c4\u2082)\n\n-- Totally like subcontext relation _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set\ndata Vars : Context \u2192 Set where\n \u2205 : Vars \u2205\n abide : \u2200 {\u03c4 \u0393} \u2192 Vars \u0393 \u2192 Vars (\u03c4 \u2022 \u0393) -- is in the set\n alter : \u2200 {\u03c4 \u0393} \u2192 Vars \u0393 \u2192 Vars (\u03c4 \u2022 \u0393) -- is out of the set\n\nstableVar : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Bool\nstableVar this (abide _) = true\nstableVar this (alter _) = false\nstableVar (that x) (abide vars) = stableVar x vars\nstableVar (that x) (alter vars) = stableVar x vars\n\n-- A term is stable if all its free variables are unchanging\n-- Alternative definition:\n--\n-- stable t vars = isNil t (derive t)\n--\nstable : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Bool\nstable (nat n) vars = true\nstable (bag b) vars = true\nstable (var x) vars = stableVar x vars\nstable (abs t) vars = stable t (abide vars)\nstable (app f x) vars = stable f vars \u2227 stable x vars\nstable (add m n) vars = stable m vars \u2227 stable n vars\nstable (map f b) vars = stable f vars \u2227 stable b vars\nstable (diff b d) vars = stable b vars \u2227 stable d vars\nstable (union b d) vars = stable b vars \u2227 stable d vars\n\nexpect-volatility : {\u03c4 : Type} \u2192 Args \u03c4\nexpect-volatility {\u03c4\u2081 \u21d2 \u03c4\u2082} = alter expect-volatility\nexpect-volatility {nats} = \u2205-nat\nexpect-volatility {bags} = \u2205-bag\n\n-- Type of `derive'`\nderive' : \u2200 {\u03c4 \u0393} \u2192 Args \u03c4 \u2192 Vars \u0393 \u2192\n Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\nderive' {\u03c4\u2081 \u21d2 \u03c4\u2082} (abide args) vars (abs t) =\n abs (abs (derive' args (abide vars) t))\n\nderive' (alter args) vars (abs t) =\n abs (abs (derive' args (alter vars) t))\n\n-- Assume, for safety, that all arguments that `t` will\n-- eventually receive in `s` or receive curried out of `s`\n-- are volatile.\nderive' args vars (app s t) =\n if stable t vars\n then app (app (derive' (abide args) vars s) (weaken \u0393\u227c\u0394\u0393 t))\n (derive' expect-volatility vars t)\n else app (app (derive' (alter args) vars s) (weaken \u0393\u227c\u0394\u0393 t))\n (derive' expect-volatility vars t)\n\nderive' args vars (map f b) =\n if stable f vars\n then map (weaken \u0393\u227c\u0394\u0393 f) (derive' args vars b)\n else map (weaken \u0393\u227c\u0394\u0393 f \u2295 derive' (abide \u2205-nat) vars f)\n (weaken \u0393\u227c\u0394\u0393 b \u2295 derive' args vars b)\n \u229d weaken \u0393\u227c\u0394\u0393 (map f b)\n\nderive' args vars (diff b d) =\n diff (derive' args vars b) (derive' args vars d)\n\nderive' args vars (union b d) =\n union (derive' args vars b) (derive' args vars d)\n\n-- derive(x + y) = replace (x + y) by (dx snd + dy snd)\n-- = \u03bb f. f (x + y) (dx snd + dy snd)\nderive' args vars (add m n) =\n abs (app (app (var this)\n (add (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) m) (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) n)))\n (add (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive' args vars m)) snd)\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive' args vars n)) snd)))\n\nderive' args vars constant-or-variable = derive constant-or-variable\n\n-- A description of variables is honest w.r.t. a \u0394-environment\n-- if every variable described as stable receives the nil change.\ndata Honest : \u2200 {\u0393} \u2192 Vars \u0393 \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 Set where\n clearly : Honest \u2205 \u2205\n alter : \u2200 {\u0393 \u03c4 v dv} \u2192 {vars : Vars \u0393} \u2192 {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Honest {\u0393} vars \u03c1 \u2192\n Honest {\u03c4 \u2022 \u0393} (alter vars) (dv \u2022 v \u2022 \u03c1)\n abide : \u2200 {\u0393 \u03c4 v dv} \u2192 {vars : Vars \u0393} \u2192 {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n v \u27e6\u2295\u27e7 dv \u2261 v \u2192\n Honest {\u0393} vars \u03c1 \u2192\n Honest {\u03c4 \u2022 \u0393} (abide vars) (dv \u2022 v \u2022 \u03c1)\n\n-- Two \u0394-values are close enough w.r.t. a set of arguments if they\n-- behave the same when fully applied (cf. extensionality) given\n-- that each argument declared stable receives the nil change.\nclose-enough : \u2200 {\u03c4 : Type} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Args \u03c4 \u2192 Set\nclose-enough {nats} df dg args = df \u2261 dg -- extensionally\nclose-enough {bags} df dg args = df \u2261 dg -- literally\nclose-enough {\u03c4\u2081 \u21d2 \u03c4\u2082} df dg (alter args) =\n \u2200 {x dx} {R[x,dx] : valid-\u0394 x dx}\n \u2192 close-enough (df x dx) (dg x dx) args\nclose-enough {\u03c4\u2081 \u21d2 \u03c4\u2082} df dg (abide args) =\n \u2200 {x dx} {validity : valid-\u0394 x dx}\n \u2192 x \u27e6\u2295\u27e7 dx \u2261 x \u2192 close-enough (df x dx) (dg x dx) args\n\nsyntax close-enough df dg args = df \u2248 dg WRT args\n\ndf\u2248df : \u2200 {\u03c4} {df : \u27e6 \u0394-Type \u03c4 \u27e7} {args : Args \u03c4} \u2192 df \u2248 df WRT args\ndf\u2248df {nats} = refl\ndf\u2248df {bags} = refl\ndf\u2248df {\u03c4\u2081 \u21d2 \u03c4\u2082} {df} {abide args} =\n \u03bb {x} {dx} _ \u2192 df\u2248df {\u03c4\u2082} {df x dx} {args}\ndf\u2248df {\u03c4\u2081 \u21d2 \u03c4\u2082} {df} {alter args} =\n \u03bb {x} {dx} \u2192 df\u2248df {\u03c4\u2082} {df x dx} {args}\n\ntoo-close : \u2200 {\u03c4 \u0393} {args : Args \u03c4} \u2192\n {df dg : Term (\u0394-Context \u0393) (\u0394-Type \u03c4)} {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n df \u2261 dg \u2192 \u27e6 df \u27e7 \u03c1 \u2248 \u27e6 dg \u27e7 \u03c1 WRT args\n\ntoo-close {\u03c4}{_} {args} {df} {dg} {\u03c1} df=dg\n rewrite df=dg = df\u2248df {\u03c4} {\u27e6 dg \u27e7 \u03c1} {args}\n\n-- A variable does not change if its value is unchanging.\nstabilityVar : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) (vars : Vars \u0393) \u2192\n stableVar x vars \u2261 true \u2192\n \u2200 {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192 Honest vars \u03c1 \u2192\n \u27e6 weakenVar \u0393\u227c\u0394\u0393 x \u27e7 \u03c1 \u27e6\u2295\u27e7 \u27e6 deriveVar x \u27e7 \u03c1 \u2261 \u27e6 weakenVar \u0393\u227c\u0394\u0393 x \u27e7 \u03c1\n\nstabilityVar this (alter vars) () (alter honesty)\nstabilityVar this (abide vars) refl (abide proof honesty) = proof\n\nstabilityVar {\u03c4} {\u03c4\u2032 \u2022 \u0393 } (that x) (abide vars) truth (abide _ honesty)\n = stabilityVar x vars truth honesty\n\nstabilityVar {\u03c4} {\u03c4\u2032 \u2022 \u0393 } (that x) (alter vars) truth (alter honesty)\n = stabilityVar x vars truth honesty\n\n-- A term does not change if its free variables are unchanging.\nstability : \u2200 {\u03c4 \u0393} \u2192 (t : Term \u0393 \u03c4) (vars : Vars \u0393) \u2192\n stable t vars \u2261 true \u2192\n \u2200 {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} {_ : Consistent-\u0394env \u03c1} \u2192 Honest vars \u03c1 \u2192\n \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 \u03c1 \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 \u03c1 \u2261 \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 \u03c1\n\nstability (nat n) vars truth {\u03c1} _ = refl\n\nstability (bag b) vars truth {\u03c1} _ = b++\u2205=b\n\nstability (var x) vars truth {\u03c1} honesty =\n stabilityVar x vars truth honesty\n\nstability (abs {\u03c4\u2081} {\u03c4\u2082} {\u0393} t) vars truth {\u03c1} {consistency} honesty =\n extensionality (\u03bb v \u2192 let\n dv : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv = \u27e6derive\u27e7 v\n mutual-weakening : \u27e6 weaken (keep \u03c4\u2081 \u2022 \u0393\u227c\u0394\u0393) t \u27e7 (v \u2022 \u03c1)\n \u2261 \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 (dv \u2022 v \u2022 \u03c1)\n mutual-weakening =\n trans (weaken-sound t (v \u2022 \u03c1))\n (trans (cong (\u03bb hole \u2192 \u27e6 t \u27e7 hole)\n {x = \u27e6 keep \u03c4\u2081 \u2022 \u0393\u227c\u0394\u0393 \u27e7 (v \u2022 \u03c1)}\n {y = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 (dv \u2022 v \u2022 \u03c1)}\n refl)\n (sym (weaken-sound t (dv \u2022 v \u2022 \u03c1))))\n in\n begin\n \u27e6 weaken (keep \u03c4\u2081 \u2022 \u0393\u227c\u0394\u0393) t \u27e7 (v \u2022 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u2261\u27e8 cong (\u03bb hole \u2192 hole \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1))\n mutual-weakening \u27e9\n \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 (dv \u2022 v \u2022 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u2261\u27e8 stability t (abide vars) truth\n {dv \u2022 v \u2022 \u03c1}\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] v) consistency}\n (abide (f\u2295\u0394f=f v) honesty) \u27e9\n \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u2261\u27e8 sym mutual-weakening \u27e9\n \u27e6 weaken (keep \u03c4\u2081 \u2022 \u0393\u227c\u0394\u0393) t \u27e7 (v \u2022 \u03c1)\n \u220e\n ) where open \u2261-Reasoning\n\nstability (app s t) vars truth {\u03c1} {consistency} honesty =\n let\n f = \u27e6 weaken \u0393\u227c\u0394\u0393 s \u27e7 \u03c1\n x = \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 \u03c1\n df = \u27e6 derive s \u27e7 \u03c1\n dx = \u27e6 derive t \u27e7 \u03c1\n in\n begin\n f x \u27e6\u2295\u27e7 df x dx\n \u2261\u27e8 extended-correctness s t {\u03c1} {consistency} \u27e9\n (f \u27e6\u2295\u27e7 df) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 stability s vars (\u2227-proj\u2081 truth) {\u03c1} {consistency} honesty \u27e8$\u27e9\n stability t vars\n (\u2227-proj\u2082 {stable s vars} truth)\n {\u03c1} {consistency} honesty \u27e9\n f x\n \u220e where open \u2261-Reasoning\n\nstability (add s t) vars truth {\u03c1} honesty = {!!}\nstability (map s t) vars truth {\u03c1} honesty = {!!}\nstability (diff s t) vars truth {\u03c1} honesty = {!!}\nstability (union s t) vars truth {\u03c1} honesty = {!!}\n\nhonestyVar : {\u03c4 : Type} \u2192 {\u0393 : Context} \u2192\n (args : Args \u03c4) \u2192 (vars : Vars \u0393) \u2192\n (x : Var \u0393 \u03c4) \u2192 derive (var x) \u2261 derive' args vars (var x)\nhonestyVar \u2205-nat vars x = refl\nhonestyVar \u2205-bag vars x = refl\nhonestyVar (abide args) vars x = refl\nhonestyVar (alter args) vars x = refl\n\n-- If both the environment and the future arguments are honest\n-- about nil changes, then the optimized derivation delivers\n-- the same result as the original derivation.\nhonesty-is-the-best-policy : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) \u2192\n (args : Args \u03c4) \u2192 (vars : Vars \u0393) \u2192\n (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env \u03c1} \u2192 Honest vars \u03c1 \u2192\n \u27e6 derive t \u27e7 \u03c1 \u2248 \u27e6 derive' args vars t \u27e7 \u03c1 WRT args\n\nhonesty-is-the-best-policy (app f s) args vars \u03c1 {consistency} honesty\n with stable s vars | inspect (stable s) vars\n... | true | [ truth ] = {!!}\n-- Task after stability\n-- --------------------\n-- To use close-enough on f, must have validity of optimized derivative\n-- of s. Validity should follow from constant expectation of volatility.\n--\n-- absurd! {!!} (stability s vars truth {\u03c1}) {!!}\n\n... | false | [ falsehood ] = {!!}\n\nhonesty-is-the-best-policy {nats} {\u0393} (nat n) args vars \u03c1 honesty =\n begin\n \u27e6 derive (nat n) \u27e7 \u03c1\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 hole \u27e7 \u03c1) (lemma \u0393 args vars) \u27e9\n \u27e6 derive' args vars (nat n) \u27e7 \u03c1\n \u220e where\n open \u2261-Reasoning\n lemma : (\u0393 : Context) (args : Args nats) (vars : Vars \u0393) \u2192\n derive (nat n) \u2261 derive' {nats} {\u0393} args vars (nat n)\n lemma \u2205 \u2205-nat \u2205 = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-nat (abide vars) = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-nat (alter vars) = refl\n\nhonesty-is-the-best-policy {bags} {\u0393} (bag b) args vars \u03c1 honesty =\n begin\n \u27e6 derive (bag b) \u27e7 \u03c1\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 hole \u27e7 \u03c1) (lemma \u0393 args vars) \u27e9\n \u27e6 derive' args vars (bag b) \u27e7 \u03c1\n \u220e where\n open \u2261-Reasoning\n lemma : (\u0393 : Context) (args : Args bags) (vars : Vars \u0393) \u2192\n derive (bag b) \u2261 derive' {bags} {\u0393} args vars (bag b)\n lemma \u2205 \u2205-bag \u2205 = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-bag (abide vars) = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-bag (alter vars) = refl\n\nhonesty-is-the-best-policy {\u03c4} {\u0393} (var x) args vars \u03c1 honesty =\n too-close {\u03c4} {\u0393} (honestyVar args vars x)\n\nhonesty-is-the-best-policy (abs t) (abide args)\n vars \u03c1 {consistency} honesty =\n \u03bb {x} {dx} {R[x,dx]} x\u2295dx=x \u2192 honesty-is-the-best-policy\n t args (abide vars)\n (dx \u2022 x \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[x,dx] consistency}\n (abide x\u2295dx=x honesty)\n\nhonesty-is-the-best-policy (abs t) (alter args)\n vars \u03c1 {consistency} honesty =\n \u03bb {x} {dx} {R[x,dx]} \u2192 honesty-is-the-best-policy\n t args (alter vars)\n (dx \u2022 x \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[x,dx] consistency}\n (alter honesty)\n\nhonesty-is-the-best-policy (add m n) args vars \u03c1 honesty = {!!}\nhonesty-is-the-best-policy (map f b) args vars \u03c1 honesty = {!!}\nhonesty-is-the-best-policy (diff b d) args vars \u03c1 honesty = {!!}\nhonesty-is-the-best-policy (union b d) args vars \u03c1 honesty = {!!}\n\n","old_contents":"{-\nCommunicate to derivatives that changes to certain arguments\nare always nil (i. e., certain arguments are stable).\n-}\n\nmodule ExplicitNil where\n\nopen import NatBag\n\nopen import Data.Bool\nopen import Data.Product hiding (map)\nopen import Data.Nat using (\u2115)\nopen import Data.Unit using (\u22a4 ; tt)\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\n-- Debug tool\nabsurd! : \u2200 {A B : Set} \u2192 B \u2192 A \u2192 A \u2192 B\nabsurd! b _ _ = b\n\n\ndata Args : (\u03c4 : Type) \u2192 Set where\n \u2205-nat : Args nats\n \u2205-bag : Args bags\n abide : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192 (args : Args \u03c4\u2082) \u2192 Args (\u03c4\u2081 \u21d2 \u03c4\u2082)\n alter : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192 (args : Args \u03c4\u2082) \u2192 Args (\u03c4\u2081 \u21d2 \u03c4\u2082)\n\n-- Totally like subcontext relation _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set\ndata Vars : Context \u2192 Set where\n \u2205 : Vars \u2205\n abide : \u2200 {\u03c4 \u0393} \u2192 Vars \u0393 \u2192 Vars (\u03c4 \u2022 \u0393) -- is in the set\n alter : \u2200 {\u03c4 \u0393} \u2192 Vars \u0393 \u2192 Vars (\u03c4 \u2022 \u0393) -- is out of the set\n\nstableVar : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Bool\nstableVar this (abide _) = true\nstableVar this (alter _) = false\nstableVar (that x) (abide vars) = stableVar x vars\nstableVar (that x) (alter vars) = stableVar x vars\n\n-- A term is stable if all its free variables are unchanging\n-- Alternative definition:\n--\n-- stable t vars = isNil t (derive t)\n--\nstable : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Bool\nstable (nat n) vars = true\nstable (bag b) vars = true\nstable (var x) vars = stableVar x vars\nstable (abs t) vars = stable t (abide vars)\nstable (app f x) vars = stable f vars \u2227 stable x vars\nstable (add m n) vars = stable m vars \u2227 stable n vars\nstable (map f b) vars = stable f vars \u2227 stable b vars\nstable (diff b d) vars = stable b vars \u2227 stable d vars\nstable (union b d) vars = stable b vars \u2227 stable d vars\n\n{-\n-- Reformulating stableness as a decidable relation\n-- Not sure if it is necessary or not.\ndata StableVar : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Set where\n abide-this : \u2200 {\u03c4 \u0393} \u2192 {vars : Vars \u0393} \u2192 StableVar this (abide {\u03c4} vars)\n abide-that : \u2200 {\u03c4 \u0393 \u03c4\u2032} \u2192 {x : Var \u0393 \u03c4} \u2192 {vars : Vars \u0393} \u2192\n StableVar x vars \u2192 StableVar (that {\u03c4} {\u03c4\u2032} x) (abide vars)\n alter-that : \u2200 {\u03c4 \u0393 \u03c4\u2032} \u2192 {x : Var \u0393 \u03c4} \u2192 {vars : Vars \u0393} \u2192\n StableVar x vars \u2192 StableVar (that {\u03c4} {\u03c4\u2032} x) (alter vars)\n\ndata Stable : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Set where\n stable-nat : \u2200 {\u0393 n vars} \u2192 Stable {nats} {\u0393} (nat n) vars\n stable-bag : \u2200 {\u0393 b vars} \u2192 Stable {bags} {\u0393} (bag b) vars\n stable-var : \u2200 {\u03c4 \u0393 x vars} \u2192\n StableVar {\u03c4} {\u0393} x vars \u2192 Stable (var x) vars\n stable-abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393 vars} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n Stable t (abide vars) \u2192 Stable (abs t) vars\n -- TODO app add map diff union\n-}\n\nexpect-volatility : {\u03c4 : Type} \u2192 Args \u03c4\nexpect-volatility {\u03c4\u2081 \u21d2 \u03c4\u2082} = alter expect-volatility\nexpect-volatility {nats} = \u2205-nat\nexpect-volatility {bags} = \u2205-bag\n\n-- Type of `derive'`\nderive' : \u2200 {\u03c4 \u0393} \u2192 Args \u03c4 \u2192 Vars \u0393 \u2192\n Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\nderive' {\u03c4\u2081 \u21d2 \u03c4\u2082} (abide args) vars (abs t) =\n abs (abs (derive' args (abide vars) t))\n\nderive' (alter args) vars (abs t) =\n abs (abs (derive' args (alter vars) t))\n\n-- Assume, for safety, that all arguments that `t` will\n-- eventually receive in `s` or receive curried out of `s`\n-- are volatile.\nderive' args vars (app s t) =\n if stable t vars\n then app (app (derive' (abide args) vars s) (weaken \u0393\u227c\u0394\u0393 t))\n (derive' expect-volatility vars t)\n else app (app (derive' (alter args) vars s) (weaken \u0393\u227c\u0394\u0393 t))\n (derive' expect-volatility vars t)\n\nderive' args vars (map f b) =\n if stable f vars\n then map (weaken \u0393\u227c\u0394\u0393 f) (derive' args vars b)\n else map (weaken \u0393\u227c\u0394\u0393 f \u2295 derive' (abide \u2205-nat) vars f)\n (weaken \u0393\u227c\u0394\u0393 b \u2295 derive' args vars b)\n \u229d weaken \u0393\u227c\u0394\u0393 (map f b)\n\nderive' args vars (diff b d) =\n diff (derive' args vars b) (derive' args vars d)\n\nderive' args vars (union b d) =\n union (derive' args vars b) (derive' args vars d)\n\n-- derive(x + y) = replace (x + y) by (dx snd + dy snd)\n-- = \u03bb f. f (x + y) (dx snd + dy snd)\nderive' args vars (add m n) =\n abs (app (app (var this)\n (add (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) m) (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) n)))\n (add (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive' args vars m)) snd)\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive' args vars n)) snd)))\n\nderive' args vars constant-or-variable = derive constant-or-variable\n\n-- A description of variables is honest w.r.t. a \u0394-environment\n-- if every variable described as stable receives the nil change.\ndata Honest : \u2200 {\u0393} \u2192 Vars \u0393 \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 Set where\n clearly : Honest \u2205 \u2205\n alter : \u2200 {\u0393 \u03c4 v dv} \u2192 {vars : Vars \u0393} \u2192 {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Honest {\u0393} vars \u03c1 \u2192\n Honest {\u03c4 \u2022 \u0393} (alter vars) (dv \u2022 v \u2022 \u03c1)\n abide : \u2200 {\u0393 \u03c4 v dv} \u2192 {vars : Vars \u0393} \u2192 {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n v \u27e6\u2295\u27e7 dv \u2261 v \u2192\n Honest {\u0393} vars \u03c1 \u2192\n Honest {\u03c4 \u2022 \u0393} (abide vars) (dv \u2022 v \u2022 \u03c1)\n\n-- Two \u0394-values are close enough w.r.t. a set of arguments if they\n-- behave the same when fully applied (cf. extensionality) given\n-- that each argument declared stable receives the nil change.\nclose-enough : \u2200 {\u03c4 : Type} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Args \u03c4 \u2192 Set\nclose-enough {nats} df dg args = df \u2261 dg -- extensionally\nclose-enough {bags} df dg args = df \u2261 dg -- literally\nclose-enough {\u03c4\u2081 \u21d2 \u03c4\u2082} df dg (alter args) =\n \u2200 {x dx} \u2192 close-enough (df x dx) (dg x dx) args\nclose-enough {\u03c4\u2081 \u21d2 \u03c4\u2082} df dg (abide args) =\n \u2200 {x dx} \u2192 x \u27e6\u2295\u27e7 dx \u2261 x \u2192 close-enough (df x dx) (dg x dx) args\n\nsyntax close-enough df dg args = df \u2248 dg WRT args\n\nvolatility\u21d2identity :\n \u2200 {\u03c4} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n df \u2248 dg WRT (expect-volatility {\u03c4}) \u2192 df \u2261 dg\n\nvolatility\u21d2identity {nats} df\u2248dg = df\u2248dg\nvolatility\u21d2identity {bags} df\u2248dg = df\u2248dg\nvolatility\u21d2identity {\u03c4\u2081 \u21d2 \u03c4\u2082} {df} {dg} df\u2248dg =\n extensionality (\u03bb x \u2192 extensionality (\u03bb dx \u2192\n volatility\u21d2identity {\u03c4\u2082} (df\u2248dg {x} {dx})))\n\ndf\u2248df : \u2200 {\u03c4} {df : \u27e6 \u0394-Type \u03c4 \u27e7} {args : Args \u03c4} \u2192 df \u2248 df WRT args\ndf\u2248df {nats} = refl\ndf\u2248df {bags} = refl\ndf\u2248df {\u03c4\u2081 \u21d2 \u03c4\u2082} {df} {abide args} =\n \u03bb {x} {dx} _ \u2192 df\u2248df {\u03c4\u2082} {df x dx} {args}\ndf\u2248df {\u03c4\u2081 \u21d2 \u03c4\u2082} {df} {alter args} =\n \u03bb {x} {dx} \u2192 df\u2248df {\u03c4\u2082} {df x dx} {args}\n\ntoo-close : \u2200 {\u03c4 \u0393} {args : Args \u03c4} \u2192\n {df dg : Term (\u0394-Context \u0393) (\u0394-Type \u03c4)} {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n df \u2261 dg \u2192 \u27e6 df \u27e7 \u03c1 \u2248 \u27e6 dg \u27e7 \u03c1 WRT args\n\ntoo-close {\u03c4}{_} {args} {df} {dg} {\u03c1} df=dg\n rewrite df=dg = df\u2248df {\u03c4} {\u27e6 dg \u27e7 \u03c1} {args}\n\n-- A variable does not change if its value is unchanging.\nstabilityVar : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) (vars : Vars \u0393) \u2192\n stableVar x vars \u2261 true \u2192\n \u2200 {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192 Honest vars \u03c1 \u2192\n \u27e6 weakenVar \u0393\u227c\u0394\u0393 x \u27e7 \u03c1 \u27e6\u2295\u27e7 \u27e6 deriveVar x \u27e7 \u03c1 \u2261 \u27e6 weakenVar \u0393\u227c\u0394\u0393 x \u27e7 \u03c1\n\nstabilityVar this (alter vars) () (alter honesty)\nstabilityVar this (abide vars) refl (abide proof honesty) = proof\n\nstabilityVar {\u03c4} {\u03c4\u2032 \u2022 \u0393 } (that x) (abide vars) truth (abide _ honesty)\n = stabilityVar x vars truth honesty\n\nstabilityVar {\u03c4} {\u03c4\u2032 \u2022 \u0393 } (that x) (alter vars) truth (alter honesty)\n = stabilityVar x vars truth honesty\n\n-- A term does not change if its free variables are unchanging.\nstability : \u2200 {\u03c4 \u0393} \u2192 (t : Term \u0393 \u03c4) (vars : Vars \u0393) \u2192\n stable t vars \u2261 true \u2192\n \u2200 {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192 Honest vars \u03c1 \u2192\n \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 \u03c1 \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 \u03c1 \u2261 \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 \u03c1\n\nstability (nat n) vars truth {\u03c1} _ = refl\n\nstability (bag b) vars truth {\u03c1} _ = b++\u2205=b\n\nstability (var x) vars truth {\u03c1} honesty =\n stabilityVar x vars truth honesty\n\nstability (abs t) vars truth {\u03c1} honesty = {!!}\nstability (app t t\u2081) vars truth {\u03c1} honesty = {!!}\nstability (add t t\u2081) vars truth {\u03c1} honesty = {!!}\nstability (map t t\u2081) vars truth {\u03c1} honesty = {!!}\nstability (diff t t\u2081) vars truth {\u03c1} honesty = {!!}\nstability (union t t\u2081) vars truth {\u03c1} honesty = {!!}\n\nhonestyVar : {\u03c4 : Type} \u2192 {\u0393 : Context} \u2192\n (args : Args \u03c4) \u2192 (vars : Vars \u0393) \u2192\n (x : Var \u0393 \u03c4) \u2192 derive (var x) \u2261 derive' args vars (var x)\nhonestyVar \u2205-nat vars x = refl\nhonestyVar \u2205-bag vars x = refl\nhonestyVar (abide args) vars x = refl\nhonestyVar (alter args) vars x = refl\n\n-- If both the environment and the future arguments are honest\n-- about nil changes, then the optimized derivation delivers\n-- the same result as the original derivation.\nhonesty-is-the-best-policy : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) \u2192\n (args : Args \u03c4) \u2192 (vars : Vars \u0393) \u2192\n (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Honest vars \u03c1 \u2192\n \u27e6 derive t \u27e7 \u03c1 \u2248 \u27e6 derive' args vars t \u27e7 \u03c1 WRT args\n\nhonesty-is-the-best-policy (app f s) args vars \u03c1 honesty\n with stable s vars | inspect (stable s) vars\n... | true | [ truth ] = {!!}\n-- absurd! {!!} (stability s vars truth {\u03c1}) {!!}\n\n... | false | [ falsehood ] = {!!}\n\nhonesty-is-the-best-policy {nats} {\u0393} (nat n) args vars \u03c1 honesty =\n begin\n \u27e6 derive (nat n) \u27e7 \u03c1\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 hole \u27e7 \u03c1) (lemma \u0393 args vars) \u27e9\n \u27e6 derive' args vars (nat n) \u27e7 \u03c1\n \u220e where\n open \u2261-Reasoning\n lemma : (\u0393 : Context) (args : Args nats) (vars : Vars \u0393) \u2192\n derive (nat n) \u2261 derive' {nats} {\u0393} args vars (nat n)\n lemma \u2205 \u2205-nat \u2205 = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-nat (abide vars) = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-nat (alter vars) = refl\n\nhonesty-is-the-best-policy {bags} {\u0393} (bag b) args vars \u03c1 honesty =\n begin\n \u27e6 derive (bag b) \u27e7 \u03c1\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 hole \u27e7 \u03c1) (lemma \u0393 args vars) \u27e9\n \u27e6 derive' args vars (bag b) \u27e7 \u03c1\n \u220e where\n open \u2261-Reasoning\n lemma : (\u0393 : Context) (args : Args bags) (vars : Vars \u0393) \u2192\n derive (bag b) \u2261 derive' {bags} {\u0393} args vars (bag b)\n lemma \u2205 \u2205-bag \u2205 = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-bag (abide vars) = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-bag (alter vars) = refl\n\nhonesty-is-the-best-policy {\u03c4} {\u0393} (var x) args vars \u03c1 honesty =\n too-close {\u03c4} {\u0393} (honestyVar args vars x)\n\nhonesty-is-the-best-policy (abs t) (abide args) vars \u03c1 honesty =\n \u03bb {x} {dx} x\u2295dx=x \u2192 honesty-is-the-best-policy\n t args (abide vars) (dx \u2022 x \u2022 \u03c1) (abide x\u2295dx=x honesty)\n\nhonesty-is-the-best-policy (abs t) (alter args) vars \u03c1 honesty =\n \u03bb {x} {dx} \u2192 honesty-is-the-best-policy\n t args (alter vars) (dx \u2022 x \u2022 \u03c1) (alter honesty)\n\nhonesty-is-the-best-policy (add m n) args vars \u03c1 honesty = {!!}\nhonesty-is-the-best-policy (map f b) args vars \u03c1 honesty = {!!}\nhonesty-is-the-best-policy (diff b d) args vars \u03c1 honesty = {!!}\nhonesty-is-the-best-policy (union b d) args vars \u03c1 honesty = {!!}\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"5ff2bafd8672cbc1ed595de165ca062a999676f7","subject":"Cleanup","message":"Cleanup\n","repos":"inc-lc\/ilc-agda","old_file":"New\/Correctness.agda","new_file":"New\/Correctness.agda","new_contents":"module New.Correctness where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\nopen import Data.Product\n\nopen import New.Lang\nopen import New.Changes\nopen import New.Derive\nopen import New.LangChanges\n\n-- Lemmas\nalternate : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7Context \u2192 eCh \u0393 \u2192 \u27e6 \u0394\u0393 \u0393 \u27e7Context\nalternate {\u2205} \u2205 \u2205 = \u2205\nalternate {\u03c4 \u2022 \u0393} (v \u2022 \u03c1) (dv \u2022 d\u03c1) = dv \u2022 v \u2022 alternate \u03c1 d\u03c1\n\n\u27e6\u0393\u227c\u0394\u0393\u27e7 : \u2200 {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n \u03c1 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7\u227c (alternate \u03c1 d\u03c1)\n\u27e6\u0393\u227c\u0394\u0393\u27e7 \u2205 \u2205 = refl\n\u27e6\u0393\u227c\u0394\u0393\u27e7 (v \u2022 \u03c1) (dv \u2022 d\u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1)\n\nfit-sound : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n \u27e6 t \u27e7Term \u03c1 \u2261 \u27e6 fit t \u27e7Term (alternate \u03c1 d\u03c1)\nfit-sound t \u03c1 d\u03c1 = trans\n (cong \u27e6 t \u27e7Term (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1))\n (sym (weaken-sound t _))\n\n-- The change semantics is just the semantics composed with derivation!\nchangeSemVar : \u2200 {\u0393 \u03c4} \u2192 (t : Var \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 Cht \u03c4\nchangeSemVar t \u03c1 d\u03c1 = \u27e6 deriveVar t \u27e7Var (alternate \u03c1 d\u03c1)\n\nchangeSem : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 Cht \u03c4\nchangeSem t \u03c1 d\u03c1 = \u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1)\n\n-- XXX Should try to simply relate the semantics to the nil change, and prove\n-- that validity can be carried over, instead of proving separately validity and\n-- correctness; elsewhere this does make things simpler.\n\nvalidDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192\n (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n valid\u0393 \u03c1 d\u03c1 \u2192 valid (\u27e6 x \u27e7Var \u03c1) (\u27e6 deriveVar x \u27e7Var (alternate \u03c1 d\u03c1))\n\nvalidDeriveVar this (v \u2022 \u03c1) (dv \u2022 d\u03c1) (vdv , \u03c1d\u03c1) = vdv\nvalidDeriveVar (that x) (v \u2022 \u03c1) (dv \u2022 d\u03c1) (vdv , \u03c1d\u03c1) = validDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\n\ncorrectDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (v : Var \u0393 \u03c4) \u2192\n IsDerivative \u27e6 v \u27e7Var (changeSemVar v)\ncorrectDeriveVar this (v \u2022 \u03c1) (dv \u2022 d\u03c1) \u03c1d\u03c1 = refl\ncorrectDeriveVar (that x) (v \u2022 \u03c1) (dv \u2022 d\u03c1) (vdv , \u03c1d\u03c1) = correctDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\n\nvalidDerive : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 valid\u0393 \u03c1 d\u03c1 \u2192\n valid (\u27e6 t \u27e7Term \u03c1) (\u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1))\ncorrectDerive : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n IsDerivative \u27e6 t \u27e7Term (changeSem t)\n\nchangeSemConst : \u2200 {\u03c4} (c : Const \u03c4) \u2192 Cht \u03c4\nchangeSemConst c = \u27e6 deriveConst c \u27e7Term \u2205\n\nchangeSemConst-rewrite : \u2200 {\u03c4 \u0393} (c : Const \u03c4) (\u03c1 : \u27e6 \u0393 \u27e7Context) d\u03c1 \u2192 changeSem (const c) \u03c1 d\u03c1 \u2261 changeSemConst c\nchangeSemConst-rewrite c \u03c1 d\u03c1 rewrite weaken-sound {\u0393\u2081\u227c\u0393\u2082 = \u2205\u227c\u0393} (deriveConst c) (alternate \u03c1 d\u03c1) | \u27e6\u2205\u227c\u0393\u27e7-\u2205 (alternate \u03c1 d\u03c1) = refl\n\nvalidDeriveConst : \u2200 {\u03c4} (c : Const \u03c4) \u2192 valid \u27e6 c \u27e7Const (\u27e6 deriveConst c \u27e7Term \u2205)\nvalidDeriveConst ()\n\ncorrectDerive (const c) \u03c1 d\u03c1 \u03c1d\u03c1 rewrite changeSemConst-rewrite c \u03c1 d\u03c1 = correctDeriveConst c\ncorrectDerive (var x) \u03c1 d\u03c1 \u03c1d\u03c1 = correctDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\ncorrectDerive (app s t) \u03c1 d\u03c1 \u03c1d\u03c1 rewrite sym (fit-sound t \u03c1 d\u03c1) =\n let\n open \u2261-Reasoning\n a0 = \u27e6 t \u27e7Term \u03c1\n da0 = \u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1)\n a0da0 = validDerive t \u03c1 d\u03c1 \u03c1d\u03c1\n in\n begin\n \u27e6 s \u27e7Term (\u03c1 \u2295 d\u03c1) (\u27e6 t \u27e7Term (\u03c1 \u2295 d\u03c1))\n \u2261\u27e8 correctDerive s \u03c1 d\u03c1 \u03c1d\u03c1 \u27e8$\u27e9 correctDerive t \u03c1 d\u03c1 \u03c1d\u03c1 \u27e9\n (\u27e6 s \u27e7Term \u03c1 \u2295 changeSem s \u03c1 d\u03c1) (\u27e6 t \u27e7Term \u03c1 \u2295 changeSem t \u03c1 d\u03c1)\n \u2261\u27e8 proj\u2082 (validDerive s \u03c1 d\u03c1 \u03c1d\u03c1 a0 da0 a0da0) \u27e9\n \u27e6 s \u27e7Term \u03c1 (\u27e6 t \u27e7Term \u03c1) \u2295 (changeSem s \u03c1 d\u03c1) (\u27e6 t \u27e7Term \u03c1) (changeSem t \u03c1 d\u03c1)\n \u220e\n where\n open import Theorem.CongApp\ncorrectDerive (abs t) \u03c1 d\u03c1 \u03c1d\u03c1 = ext (\u03bb a \u2192\n let\n open \u2261-Reasoning\n \u03c11 = a \u2022 \u03c1\n d\u03c11 = nil a \u2022 d\u03c1\n \u03c11d\u03c11 : valid \u03c11 d\u03c11\n \u03c11d\u03c11 = nil-valid a , \u03c1d\u03c1\n in\n begin\n \u27e6 t \u27e7Term (a \u2022 \u03c1 \u2295 d\u03c1)\n \u2261\u27e8 cong (\u03bb a\u2032 \u2192 \u27e6 t \u27e7Term (a\u2032 \u2022 \u03c1 \u2295 d\u03c1)) (sym (update-nil a)) \u27e9\n \u27e6 t \u27e7Term (\u03c11 \u2295 d\u03c11)\n \u2261\u27e8 correctDerive t \u03c11 d\u03c11 \u03c11d\u03c11 \u27e9\n \u27e6 t \u27e7Term \u03c11 \u2295 changeSem t \u03c11 d\u03c11\n \u220e)\n\nvalidDerive (app s t) \u03c1 d\u03c1 \u03c1d\u03c1 =\n let\n f = \u27e6 s \u27e7Term \u03c1\n df = \u27e6 derive s \u27e7Term (alternate \u03c1 d\u03c1)\n v = \u27e6 t \u27e7Term \u03c1\n dv = \u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1)\n vdv = validDerive t \u03c1 d\u03c1 \u03c1d\u03c1\n fdf = validDerive s \u03c1 d\u03c1 \u03c1d\u03c1\n fvdfv = proj\u2081 (fdf v dv vdv)\n in subst (\u03bb v\u2032 \u2192 valid (f v) (df v\u2032 dv)) (fit-sound t \u03c1 d\u03c1) fvdfv\nvalidDerive (abs t) \u03c1 d\u03c1 \u03c1d\u03c1 =\n \u03bb a da ada \u2192\n let\n fv = \u27e6 t \u27e7Term (a \u2022 \u03c1)\n dfvdv = \u27e6 derive t \u27e7Term (da \u2022 a \u2022 alternate \u03c1 d\u03c1)\n rdr = validDerive t (a \u2022 \u03c1) (da \u2022 d\u03c1) (ada , \u03c1d\u03c1)\n \u03c11 = a \u2295 da \u2022 \u03c1\n d\u03c11 = nil (a \u2295 da) \u2022 d\u03c1\n \u03c12 = a \u2022 \u03c1\n d\u03c12 = da \u2022 d\u03c1\n \u03c11d\u03c11 : valid \u03c11 d\u03c11\n \u03c11d\u03c11 = nil-valid (a \u2295 da) , \u03c1d\u03c1\n \u03c12d\u03c12 : valid \u03c12 d\u03c12\n \u03c12d\u03c12 = ada , \u03c1d\u03c1\n open \u2261-Reasoning\n in\n rdr ,\n (begin\n \u27e6 t \u27e7Term \u03c11 \u2295\n \u27e6 derive t \u27e7Term (alternate \u03c11 d\u03c11)\n \u2261\u27e8 sym ( correctDerive t \u03c11 d\u03c11 \u03c11d\u03c11) \u27e9\n \u27e6 t \u27e7Term (\u03c11 \u2295 d\u03c11)\n \u2261\u27e8\u27e9\n \u27e6 t \u27e7Term (a \u2295 da \u2295 (nil (a \u2295 da)) \u2022 \u03c1 \u2295 d\u03c1)\n \u2261\u27e8 cong (\u03bb a\u2032 \u2192 \u27e6 t \u27e7Term (a\u2032 \u2022 \u03c1 \u2295 d\u03c1)) (update-nil (a \u2295 da)) \u27e9\n \u27e6 t \u27e7Term (a \u2295 da \u2022 \u03c1 \u2295 d\u03c1)\n \u2261\u27e8 correctDerive t \u03c12 d\u03c12 \u03c12d\u03c12 \u27e9\n \u27e6 t \u27e7Term (a \u2022 \u03c1) \u2295 \u27e6 derive t \u27e7Term (da \u2022 a \u2022 alternate \u03c1 d\u03c1)\n \u220e)\nvalidDerive (var x) \u03c1 d\u03c1 \u03c1d\u03c1 = validDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\nvalidDerive (const c) \u03c1 d\u03c1 \u03c1d\u03c1 rewrite changeSemConst-rewrite c \u03c1 d\u03c1 = validDeriveConst c\n","old_contents":"module New.Correctness where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\nopen import Data.Product\n\nopen import New.Lang\nopen import New.Changes\nopen import New.Derive\nopen import New.LangChanges\n\n-- Lemmas\nalternate : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7Context \u2192 eCh \u0393 \u2192 \u27e6 \u0394\u0393 \u0393 \u27e7Context\nalternate {\u2205} \u2205 \u2205 = \u2205\nalternate {\u03c4 \u2022 \u0393} (v \u2022 \u03c1) (dv \u2022 d\u03c1) = dv \u2022 v \u2022 alternate \u03c1 d\u03c1\n\n\u27e6\u0393\u227c\u0394\u0393\u27e7 : \u2200 {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n \u03c1 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7\u227c (alternate \u03c1 d\u03c1)\n\u27e6\u0393\u227c\u0394\u0393\u27e7 \u2205 \u2205 = refl\n\u27e6\u0393\u227c\u0394\u0393\u27e7 (v \u2022 \u03c1) (dv \u2022 d\u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1)\n\nfit-sound : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n \u27e6 t \u27e7Term \u03c1 \u2261 \u27e6 fit t \u27e7Term (alternate \u03c1 d\u03c1)\nfit-sound t \u03c1 d\u03c1 = trans\n (cong \u27e6 t \u27e7Term (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1))\n (sym (weaken-sound t _))\n\n-- The change semantics is just the semantics composed with derivation!\nchangeSemVar : \u2200 {\u0393 \u03c4} \u2192 (t : Var \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 Cht \u03c4\nchangeSemVar t \u03c1 d\u03c1 = \u27e6 deriveVar t \u27e7Var (alternate \u03c1 d\u03c1)\n\nchangeSem : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 Cht \u03c4\nchangeSem t \u03c1 d\u03c1 = \u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1)\n\n-- XXX Should try to simply relate the semantics to the nil change, and prove\n-- that validity can be carried over, instead of proving separately validity and\n-- correctness; elsewhere this does make things simpler.\n\nvalidDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192\n (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n valid\u0393 \u03c1 d\u03c1 \u2192 valid (\u27e6 x \u27e7Var \u03c1) (\u27e6 deriveVar x \u27e7Var (alternate \u03c1 d\u03c1))\n\nvalidDeriveVar this (v \u2022 \u03c1) (dv \u2022 d\u03c1) (vdv , \u03c1d\u03c1) = vdv\nvalidDeriveVar (that x) (v \u2022 \u03c1) (dv \u2022 d\u03c1) (vdv , \u03c1d\u03c1) = validDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\n\ncorrectDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (v : Var \u0393 \u03c4) \u2192\n IsDerivative \u27e6 v \u27e7Var (changeSemVar v)\ncorrectDeriveVar this (v \u2022 \u03c1) (dv \u2022 d\u03c1) \u03c1d\u03c1 = refl\ncorrectDeriveVar (that x) (v \u2022 \u03c1) (dv \u2022 d\u03c1) (vdv , \u03c1d\u03c1) = correctDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\n\nvalidDerive : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 valid\u0393 \u03c1 d\u03c1 \u2192\n valid (\u27e6 t \u27e7Term \u03c1) (\u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1))\ncorrectDerive : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n IsDerivative \u27e6 t \u27e7Term (changeSem t)\n\nsemConst-rewrite : \u2200 {\u03c4 \u0393} (c : Const \u03c4) (\u03c1 : \u27e6 \u0393 \u27e7Context) d\u03c1 \u2192 changeSem (const c) \u03c1 d\u03c1 \u2261 \u27e6 deriveConst c \u27e7Term \u2205\nsemConst-rewrite c \u03c1 d\u03c1 rewrite weaken-sound {\u0393\u2081\u227c\u0393\u2082 = \u2205\u227c\u0393} (deriveConst c) (alternate \u03c1 d\u03c1) | \u27e6\u2205\u227c\u0393\u27e7-\u2205 (alternate \u03c1 d\u03c1) = refl\n\ncorrectDeriveConst : \u2200 {\u03c4} (c : Const \u03c4) \u2192 \u27e6 c \u27e7Const \u2261 \u27e6 c \u27e7Const \u2295 \u27e6 deriveConst c \u27e7Term \u2205\ncorrectDeriveConst ()\n\nvalidDeriveConst : \u2200 {\u03c4} (c : Const \u03c4) \u2192 valid \u27e6 c \u27e7Const (\u27e6 deriveConst c \u27e7Term \u2205)\nvalidDeriveConst ()\n\ncorrectDerive (const c) \u03c1 d\u03c1 \u03c1d\u03c1 rewrite semConst-rewrite c \u03c1 d\u03c1 = correctDeriveConst c\ncorrectDerive (var x) \u03c1 d\u03c1 \u03c1d\u03c1 = correctDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\ncorrectDerive (app s t) \u03c1 d\u03c1 \u03c1d\u03c1 rewrite sym (fit-sound t \u03c1 d\u03c1) =\n let\n open \u2261-Reasoning\n a0 = \u27e6 t \u27e7Term \u03c1\n da0 = \u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1)\n a0da0 = validDerive t \u03c1 d\u03c1 \u03c1d\u03c1\n in\n begin\n \u27e6 s \u27e7Term (\u03c1 \u2295 d\u03c1) (\u27e6 t \u27e7Term (\u03c1 \u2295 d\u03c1))\n \u2261\u27e8 correctDerive s \u03c1 d\u03c1 \u03c1d\u03c1 \u27e8$\u27e9 correctDerive t \u03c1 d\u03c1 \u03c1d\u03c1 \u27e9\n (\u27e6 s \u27e7Term \u03c1 \u2295 changeSem s \u03c1 d\u03c1) (\u27e6 t \u27e7Term \u03c1 \u2295 changeSem t \u03c1 d\u03c1)\n \u2261\u27e8 proj\u2082 (validDerive s \u03c1 d\u03c1 \u03c1d\u03c1 a0 da0 a0da0) \u27e9\n \u27e6 s \u27e7Term \u03c1 (\u27e6 t \u27e7Term \u03c1) \u2295 (changeSem s \u03c1 d\u03c1) (\u27e6 t \u27e7Term \u03c1) (changeSem t \u03c1 d\u03c1)\n \u220e\n where\n open import Theorem.CongApp\ncorrectDerive (abs t) \u03c1 d\u03c1 \u03c1d\u03c1 = ext (\u03bb a \u2192\n let\n open \u2261-Reasoning\n \u03c11 = a \u2022 \u03c1\n d\u03c11 = nil a \u2022 d\u03c1\n \u03c11d\u03c11 : valid \u03c11 d\u03c11\n \u03c11d\u03c11 = nil-valid a , \u03c1d\u03c1\n in\n begin\n \u27e6 t \u27e7Term (a \u2022 \u03c1 \u2295 d\u03c1)\n \u2261\u27e8 cong (\u03bb a\u2032 \u2192 \u27e6 t \u27e7Term (a\u2032 \u2022 \u03c1 \u2295 d\u03c1)) (sym (update-nil a)) \u27e9\n \u27e6 t \u27e7Term (\u03c11 \u2295 d\u03c11)\n \u2261\u27e8 correctDerive t \u03c11 d\u03c11 \u03c11d\u03c11 \u27e9\n \u27e6 t \u27e7Term \u03c11 \u2295 changeSem t \u03c11 d\u03c11\n \u220e)\n\nvalidDerive (app s t) \u03c1 d\u03c1 \u03c1d\u03c1 =\n let\n f = \u27e6 s \u27e7Term \u03c1\n df = \u27e6 derive s \u27e7Term (alternate \u03c1 d\u03c1)\n v = \u27e6 t \u27e7Term \u03c1\n dv = \u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1)\n vdv = validDerive t \u03c1 d\u03c1 \u03c1d\u03c1\n fdf = validDerive s \u03c1 d\u03c1 \u03c1d\u03c1\n fvdfv = proj\u2081 (fdf v dv vdv)\n in subst (\u03bb v\u2032 \u2192 valid (f v) (df v\u2032 dv)) (fit-sound t \u03c1 d\u03c1) fvdfv\nvalidDerive (abs t) \u03c1 d\u03c1 \u03c1d\u03c1 =\n \u03bb a da ada \u2192\n let\n fv = \u27e6 t \u27e7Term (a \u2022 \u03c1)\n dfvdv = \u27e6 derive t \u27e7Term (da \u2022 a \u2022 alternate \u03c1 d\u03c1)\n rdr = validDerive t (a \u2022 \u03c1) (da \u2022 d\u03c1) (ada , \u03c1d\u03c1)\n \u03c11 = a \u2295 da \u2022 \u03c1\n d\u03c11 = nil (a \u2295 da) \u2022 d\u03c1\n \u03c12 = a \u2022 \u03c1\n d\u03c12 = da \u2022 d\u03c1\n \u03c11d\u03c11 : valid \u03c11 d\u03c11\n \u03c11d\u03c11 = nil-valid (a \u2295 da) , \u03c1d\u03c1\n \u03c12d\u03c12 : valid \u03c12 d\u03c12\n \u03c12d\u03c12 = ada , \u03c1d\u03c1\n open \u2261-Reasoning\n in\n rdr ,\n (begin\n \u27e6 t \u27e7Term \u03c11 \u2295\n \u27e6 derive t \u27e7Term (alternate \u03c11 d\u03c11)\n \u2261\u27e8 sym ( correctDerive t \u03c11 d\u03c11 \u03c11d\u03c11) \u27e9\n \u27e6 t \u27e7Term (\u03c11 \u2295 d\u03c11)\n \u2261\u27e8\u27e9\n \u27e6 t \u27e7Term (a \u2295 da \u2295 (nil (a \u2295 da)) \u2022 \u03c1 \u2295 d\u03c1)\n \u2261\u27e8 cong (\u03bb a\u2032 \u2192 \u27e6 t \u27e7Term (a\u2032 \u2022 \u03c1 \u2295 d\u03c1)) (update-nil (a \u2295 da)) \u27e9\n \u27e6 t \u27e7Term (a \u2295 da \u2022 \u03c1 \u2295 d\u03c1)\n \u2261\u27e8 correctDerive t \u03c12 d\u03c12 \u03c12d\u03c12 \u27e9\n \u27e6 t \u27e7Term (a \u2022 \u03c1) \u2295 \u27e6 derive t \u27e7Term (da \u2022 a \u2022 alternate \u03c1 d\u03c1)\n \u220e)\nvalidDerive (var x) \u03c1 d\u03c1 \u03c1d\u03c1 = validDeriveVar x \u03c1 d\u03c1 \u03c1d\u03c1\nvalidDerive (const c) \u03c1 d\u03c1 \u03c1d\u03c1 rewrite semConst-rewrite c \u03c1 d\u03c1 = validDeriveConst c\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a297da86562d06d588cd10f4dcc0e310624bc4f4","subject":"agda : wouter \/ PT","message":"agda : wouter \/ PT\n","repos":"haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc","old_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/wouter-swierstra-predicate-transformers-6fe8f13\/PredicateTransformers.agda","new_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/wouter-swierstra-predicate-transformers-6fe8f13\/PredicateTransformers.agda","new_contents":"module PredicateTransformers where\n\nopen import Prelude hiding (map; all)\nopen import Level hiding (lift)\n\n------------------------------------------------------------------------------\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\n------------------------------------------------------------------------------\nABSTRACT\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nrefinement type https:\/\/en.wikipedia.org\/wiki\/Refinement_(computing)\n- type with a predicate which must hold for any element of the refined type\n- can express\n - preconditions when used as function args\n - postconditions when used as return types\n\nthe problem statement is centered around \/compositionality\/\n- pure functions enable compositional reasoning\n- effectful programs require reasoning about context\n- paper provides mechanism for reasoning about effectful programs (and for program calculation)\n\nmain idea\n- represent effectful computation as a free monad\n- write an \"interpreter\" that computes a predicate transformer\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to use this refinement relation to\n - show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\nmodule Free where\n -- Free : represents syntax of an effectful computation\n -- C : type of commands\n -- Free C R : returns an 'a' or issues command c : C\n -- For each c : C, there is a set of responses R c\n -- - R : C -> Set : is type family of responses for commands\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\" input to output\n -- input\n -- - pure function 'a\u2192b : a -> b'\n -- - postcondition 'b -> Set' (on output of a\u2192b)\n -- output\n -- - weakest precondition 'a -> Set' on input to a\u2192b that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n module WP0Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data isEven : \u2115 \u2192 Set where\n even-z : isEven Nat.zero\n even-s : {n : \u2115} \u2192 isEven n \u2192 isEven (Nat.suc (Nat.suc n))\n\n isEvenWP : \u2115 -> Set\n isEvenWP = wp0 (_+ 2) isEven\n\n _ : isEvenWP \u2261 isEven \u2218 (_+ 2)\n _ = refl\n\n _ : Set\n _ = isEvenWP 5\n\n _ : isEvenWP 5 \u2261 isEven 7\n _ = refl\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - enable via making dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n -- every element a that satisfies P is contained in the set of things that satifies Q\n --\n -- implication between predicates\n --\n -- P a says a \u2208 P\n -- if the extents of P, Q : A \u2192 Set is taken to be\n -- the subset of A satisfying predicates P, Q\n --\n -- the extent of P : A -> Set is the subset of terms of type A satisfying P.\n -- \"extent\" indicates a kind of black-box view of things,\n -- e.g., the \"extent\" of a function is the set of its input-output pairs.\n --\n -- https:\/\/en.wikipedia.org\/wiki\/Extension_%28semantics%29\n -- the extension of a concept, idea, or sign consists of the things to which it applies\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n module \u2286Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data _\u2264_ : \u2115 \u2192 \u2115 \u2192 Set where\n z\u2264n : \u2200 {n : \u2115} \u2192 0 \u2264 n\n s\u2264s : \u2200 {m n : \u2115} \u2192 m \u2264 n \u2192 Nat.suc m \u2264 Nat.suc n\n\n \u22641\u2286\u22642 : (_\u2264 1) \u2286 (_\u2264 2)\n -- P \u2286 Q\n -- \u2200 a -> P a -> Q a\n -- (a : \u2115) -> (a \u2264 1) -> (a \u2264 2)\n \u22641\u2286\u22642 Zero _ = z\u2264n\n \u22641\u2286\u22642 (Succ Zero) (s\u2264s _) = s\u2264s z\u2264n\n\n {- cannot be proved\n \u22642\u2286\u22641 : (_\u2264 2) \u2286 (_\u2264 1)\n \u22642\u2286\u22641 Zero _ = z\u2264n\n \u22642\u2286\u22641 (Succ a) (s\u2264s p) = {!!}\n -}\n\n -- refinement relation between PTs : pt1 refined by pt2\n -- (same thing as \u2286, but one level up)\n -- for every post condition, pt2 weakens the precondition\n -- \"weakest\" is the right side : pt2 P\n --\n -- pt1 , pt2 : preconditions for satisfying a program\n -- refinement : pt1 maybe be a stronger requirement than pt2\n --\n -- pt2 is a refinement because it weakens what is required by pt1\n -- e.g., pt1 pt2\n -- >= 20 >= 10\n --\n -- weakening the input assumptions gives a stronger result\n --\n -- contract says establish a pre and I will give you a post, i.e.,\n -- \"stronger\" post condition\n -- or \"weaker\" pre condition\n --\n -- refinement of predicate transformers\n --\n -- use case: relate different implementations of the \"same program\"\n -- pt1 \u2291 pt2 : pt1 is refined by pt2\n -- - if pt1 is understood as giving preconditions which satisfy\n -- postcondition P for some given program f1\n -- then his says the precondition can be \/weakened\/ and still guarantee the postcondition\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g\n -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation to\n - relate PT semantics between programs and specifications\n - show a program satisfies its specification\n - show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n - corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n note : for =Free C R b= programs means generating the same AST\n\n instead, define predicate transformers that give \/interpretations\/\n to a particular choice of command type C and response family R\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public using (_+_; _>_; _*_; s\u2264s; z\u2264n)\n renaming (\u2115 to Nat; zero to Zero; suc to Succ)\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n -- one command\n data C : Set where\n Abort : C -- no continuation\n\n -- response code : the program does not execute further\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n -- BIG-STEP SEMANTICS specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n module \u21d3Test where\n _ : Expr\n _ = Val 3\n _ : Expr\n _ = Div (Val 3) (Val 0)\n\n _ : Val 0 \u21d3 0\n _ = \u21d3Base\n\n _ : Val 3 \u21d3 3\n _ = \u21d3Base\n\n _ : Div (Val 3) (Val 3) \u21d3 1\n _ = \u21d3Step \u21d3Base \u21d3Base\n\n _ : Div (Val 9) (Val 3) \u21d3 3\n _ = \u21d3Step \u21d3Base \u21d3Base\n\n _ : Div (Val 9) (Div (Val 3) (Val 1)) \u21d3 3\n _ = \u21d3Step \u21d3Base (\u21d3Step \u21d3Base \u21d3Base)\n\n _ : Div (Div (Val 9) (Val 1)) (Div (Val 3) (Val 1)) \u21d3 3\n _ = \u21d3Step (\u21d3Step \u21d3Base \u21d3Base) (\u21d3Step \u21d3Base \u21d3Base)\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val n \u27e7 = return n\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\nl ->\n \u27e6 er \u27e7 >>= \\nr ->\n nl \u00f7 nr\n\n module InterpTest where\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n _ : evv \u2261 Pure 3\n _ = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n _ : evd \u2261 Pure 1\n _ = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n _ : evd0 \u2261 Step Abort (\u03bb ())\n _ = refl\n\n -- relate big step semantics to monadic interpreter\n\n -- convert post-condition for a pure function 'f : (x : A) -> b x'\n -- to post-condition on partial functions 'f : (x : A) -> Partial (b x)'\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba -- P holds if operation produces a value\n mustPT _ _ (Step Abort _) = \u22a5 -- if operation fails, there is no result to apply P to\n -- note: could give \u22a4 here, but want to rule out failure\n -- (total correctness)\n\n module MustPTTest where\n _ : Expr -> Nat -> Set\n _ = _\u21d3_\n\n _ : Set\n _ = Val 1 \u21d3 1\n\n _ : Expr -> Partial Nat -> Set\n _ = mustPT _\u21d3_\n\n _ : Set\n _ = mustPT _\u21d3_ (Val 1) (Pure 1)\n\n _ : mustPT _\u21d3_ (Val 1) (Pure 1)\n _ = \u21d3Base\n\n _ : Set\n _ = mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n {- a value of this type cannot be constructed\n _ : mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n _ = {!!}\n -}\n _ : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb n -> Pure 3 >>= _\u00f7 n))\n _ = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n _ : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n _ = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n _ : Set\n _ = mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n\n {-\n to relate big step semantics to monadic interpreter\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - _\u21d3_ relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n\n to call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results P' : Partial b -> Set\n - done via 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so mustPT Abort case returns empty type\n -}\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n module WpPartialTest where\n _ : Expr -> Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n _ = refl\n\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n _ = \u21d3Base\n\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n _ = \u21d3Step \u21d3Base \u21d3Base\n _ : Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n\n {- this type cannot be constructed\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n _ = {!!}\n -}\n _ : Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n\n {-\n wpPartial \u27e6_\u27e7 _\u21d3_ : a predicate on expressions\n - a way to express SUCCESSFUL evaluation of an Expr with intended semantics\n\n \u2200 exprs satisfying this predicate\n - monadic interpreter and\n - relational big-step specification\n must agree on result of evaluation\n\n for a given Expr, satisfaction of predicate means\n - Expr evaluates to a value by big-step operational semantics\n - if interpreter returns a value at all\n then that value agrees with big step semantics\n - if interpreter aborts\n then have a proof of \u22a5 (so, it does not abort)\n which will NOT agree with \u21d3 big-step semantics since \u21d3 does not allow div-by-0\n\n What does this say about correctness of interpreter?\n\n does not specify how many Expr that satisfy that characterization also satisfy wpPartial \u27e6_\u27e7 _\u21d3_\n\n to understand wpPartial \u27e6_\u27e7 _\u21d3_ better\n - define SafeDiv : safety criteria that we believe in\n - prove every expression that satisfies SafeDiv satisfies wpPartial \u27e6_\u27e7 _\u21d3_\n - SafeDiv refines wpPartial \u27e6_\u27e7 _\u21d3_\n - if we gave \u22a4 above, then 'correct' would not say that SafeDiv rules out undefined behavior\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val _) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n module SafeDivTest where\n _ : SafeDiv (Val 3) \u2261 \u22a4\n _ = refl\n _ : SafeDiv (Val 0) \u2261 \u22a4\n _ = refl\n _ : Set\n _ = SafeDiv (Val 0)\n\n _ : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 0) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n _ = refl\n _ : Set\n _ = SafeDiv (Div (Val 3) (Val 3))\n\n _ : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val 0 \u21d3 0) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n _ = refl\n _ : Set\n _ = SafeDiv (Div (Val 3) (Val 0))\n\n {-\n expect : any expr : e for which SafeDiv e holds can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n -- (a : Expr) (SafeDiv a) (wpPartial \u27e6_\u27e7 _\u21d3_ a)\n correct (Val _) _ = \u21d3Base\n correct (Div el er) (er\u21d30\u2192\u22a5 , (sdel , sder))\n with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n ... | Pure _ | Pure Zero | _ | er\u21d30 = magic (er\u21d30\u2192\u22a5 er\u21d30)\n ... | Pure _ | Pure (Succ _) | el\u21d3nl | er\u21d3Snr = \u21d3Step el\u21d3nl er\u21d3Snr\n ... | Pure _ | Step Abort _ | _ | ()\n ... | Step Abort _ | _ | () | _\n\n module CorrectTest where\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n _ = \u21d3Base\n\n _ : Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 1))\n _ = \u21d3Step \u21d3Base \u21d3Base\n {-\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 0))\n _ = {!!}\n -}\n {-\n Generalization.\n Instead of manually defining SafeDiv,\n define more general predicate\n characterising the domain of a partial function using wpPartial.\n -}\n\n -- domain is well-defined Exprs (i.e., no div-by-0)\n -- returns \u22a4 on Pure; \u22a5 on Step Abort ...\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n module DomTest where\n _ : dom \u27e6_\u27e7 \u2261 \u03bb z \u2192 mustPT (\u03bb x _ \u2192 \u22a4' zero) z \u27e6 z \u27e7\n _ = refl\n _ : dom \u27e6_\u27e7 (Val 0) \u2261 \u22a4' zero\n _ = refl\n _ : dom \u27e6_\u27e7 (Div (Val 1) (Val 0)) \u2261 \u22a5\n _ = refl\n\n -- can show that the two semantics agree precisely on the domain of the interpreter:\n\n -- everything in domain of interpreter\n -- gets evaluated in agreement with big step semantics\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n -- only things interpreted in agreement with big step semantics\n -- are those that are in domain of interpreter\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on argument expression\n\n sound (Val _) _ = \u21d3Base\n sound (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div _ _) () | Pure _ | Pure Zero | _ | _\n sound (Div _ _) _ | Pure nl | Pure (Succ nr) | \u22a4'zero\u2192el\u21d3nl | \u22a4'zero\u2192er\u21d3Succnr\n = \u21d3Step (\u22a4'zero\u2192el\u21d3nl tt) (\u22a4'zero\u2192er\u21d3Succnr tt)\n sound (Div _ _) () | Pure _ | Step Abort _ | _ | _\n sound (Div _ _) () | Step Abort _ | _ | _ | _\n\n inDom : {n : Nat}\n -> (e : Expr)\n -> \u27e6 e \u27e7 == Pure n\n -> dom \u27e6_\u27e7 e\n inDom (Val _) _ = tt\n inDom (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div _ _) () | Pure _ | Pure Zero\n inDom (Div _ _) _ | Pure _ | Pure (Succ _) = tt\n inDom (Div _ _) () | Pure _ | Step Abort _\n inDom (Div _ _) () | Step Abort _ | _\n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n : (e : Expr) (n : Nat)\n -> \u27e6 e \u27e7 \u2261 Pure n\n -> e \u21d3 n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n e _ eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} Diveler\u21d3_nldivSuccn\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 { _} { _} () | Pure _ | _ | Pure Zero\n wpPartial1 {el} { _} _ | Pure nl | [[[ eqnl ]]] | Pure (Succ _) = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n el nl eqnl\n wpPartial1 { _} { _} () | Pure _ | _ | Step Abort _\n wpPartial1 { _} { _} () | Step Abort _ | _ | _\n\n wpPartial2 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} _ with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 { _} {er} _ | Pure _ | _ | Pure nr | [[[ eqnr ]]] = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n er nr eqnr\n wpPartial2 { _} { _} () | Pure _ | _ | Step Abort _ | _\n wpPartial2 { _} { _} () | Step Abort _ | _ | _ | _\n\n complete (Val _) _ = tt\n complete (Div el er) h\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div _ _) h | Pure nl | [[[ \u27e6el\u27e7\u2261Purenl ]]] | Pure Zero | [[[ \u27e6er\u27e7\u2261Pure0 ]]] | _ | _\n -- Goal\n -- mustPT (\u03bb _ _ \u2192 \u22a4' zero)\n -- (Div el er) (Pure nl >>= (\u03bb nl' \u2192 Pure Zero >>= _\u00f7_ nl'))\n -- \u22a5\n -- Context\n -- h : mustPT _\u21d3_ (Div el er) (\u27e6 el \u27e7 >>= (\u03bb nl' \u2192 \u27e6 er \u27e7 >>= _\u00f7_ nl'))\n rewrite\n \u27e6el\u27e7\u2261Purenl -- h : mustPT _\u21d3_ (Div el er) (\u27e6 er \u27e7 >>= _\u00f7_ nl)\n | \u27e6er\u27e7\u2261Pure0 -- h : \u22a5\n = h -- magic h\n complete (Div _ _) _ | Pure _ | _ | Pure (Succ _) | _ | _ | _ = tt\n complete (Div _ _) _ | Pure _ | _ | Step Abort _ | _ | _ | ()\n complete (Div _ _) _ | Step Abort _ | _ | _ | _ | () | _\n\n module CompleteTest where\n {-\n _ : mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n _ = {!!}\n -}\n _ : Set\n _ = mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) <-> (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement\n - to relate Kleisli morphisms\n - to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n this example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre and post condition\n - to its implementation\n\n add top two elements; fails if stack has too few elements\n\n show how to prove definition meets its specification\n -}\n\n -- define specification in terms of a pre\/post condition\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n -- [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n -- for non-dependent examples (e.g., type b does not depend on x : a)\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function (to discard unused arg of type a)\n\n -- describes desired postcondition for addition function\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n -- spec for addition function\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n -- pre post\n {-\n need to relate spec to an implementation\n 'wpPartial' assigns predicate transformer semantics to functions\n 'wpSpec' assigns predicate transformer semantics to specifications\n -}\n -- given a spec, Spec a b\n -- computes weakest precondition that satisfies an arbitrary postcondition P\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\a -> (pre a) -- i.e., spec\u2019s precondition should hold and\n \u2227 (post a \u2286 P a) -- spec's postcondition must imply P\n\n -- using 'wpSpec' can now find a program 'add' that \"refines\" 'addSpec'\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs0 =\n pop xs0 >>= \\{(x1 , xs1) ->\n pop xs1 >>= \\{(x2 , xs2) ->\n return ((x1 + x2) :: xs2)}}\n\n -- verify correct\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd _ (_ :: Nil) (s\u2264s () , _)\n correctnessAdd P a@(x :: y :: xs ) (s\u2264s (s\u2264s z\u2264n) , post_addSpec_a_\u2286P_a)\n -- wpPartial add P (x :: y :: xs)\n = post_addSpec_a_\u2286P_a (x + y :: xs) AddStep\n -- paper version has \"extra\" 'Nil\" case\n--correctnessAdd P Nil ( () , _)\n--correctnessAdd P (_ :: Nil) (s\u2264s () , _)\n--correctnessAdd P (x :: y :: xs ) ( _ , H) = H (x + y :: xs) AddStep\n\n {-\n repeat: this example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition\n - to its implementation\n\n compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\n{-\n------------------------------------------------------------------------------\n4 Mutable State\n\npredicate transformer semantics for mutable state\ngiving rise to Hoare logic\n\nfollowing assumes a fixed type s : Set (the type of the state)\n-}\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n\n -- smart constructor\n get : State s\n get = Step Get return\n\n -- smart constructor\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n -- map free monad to the state monad\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n -- predicate transformer : for every stateful computation\n -- maps a postcondition on b \u00d7 s\n -- to the required precondition on s:\n statePT : forall {l l'} -> {b : Set l}\n -> (b \u00d7 s -> Set l')\n -> State b\n -> ( s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n -- generalise statePT\n -- Sometimes describe postconditions as a relation between inputs and outputs.\n -- For stateful computations, mean enabling the postcondition to also refer to the initial state:\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n -- weakest precondition semantics for Kleisli morphisms a \u2192 State b\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> ( a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> ( a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n -- Given predicate P relating input, initial state, final state and result,\n -- 'wpState' computes the weakest precondition required of input and initial state\n -- to ensure P holds upon completing the computation.\n -- The definition amounts to composing 'wp' and 'statePT' functions.\n\n -- prove soundness of this semantics with respect to the run function\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\n{-\nExample showing how to reason about stateful programs using weakest precondition semantics.\n\nVerification problem proposed by Hutton and Fulger [2008]\n- input : binary tree\n- relabel tree so each leaf has a unique number associated with it\n\nTypical solution uses state monad to keep track of next unused label.\n\nHutton and Fulger challenge : reason about the program, without expanding definition of monadic operations.\n\nExample shows how properties of refinement relation can be used to reason about arbitrary effects.\n-}\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n -- Specification.\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n -- precondition true regardless of input tree and initial state\n -- v\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n -- ^\n -- postcondition is conjunction\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s')\n = (flatten t' == (seq (s) (size t))) -- flattening result t\u2019 produces sequence of numbers from s to s + size t\n \u2227 (s + size t == s') -- output state 's should be precisely size t larger than input state s\n\n -- increment current state; return value (before incr)\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n -- relabelling function\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n module RelableTest where\n _ : Free C R (Tree Nat)\n _ = relabel (Node (Leaf 10) (Leaf 20))\n _ : relabel (Node (Leaf 10) (Leaf 20))\n \u2261 Step Get (\u03bb n1\n \u2192 Step (Put (Succ n1)) (\u03bb _\n \u2192 Step Get (\u03bb n2\n \u2192 Step (Put (Succ n2)) (\u03bb _\n \u2192 Pure (Node (Leaf n1) (Leaf n2))))))\n _ = refl\n\n _ : Pair (Tree Nat) Nat\n _ = run (relabel (Node (Leaf 10) (Leaf 20))) 1\n _ : run (relabel (Node (Leaf 10) (Leaf 20))) 1 \u2261 (Node (Leaf 1) (Leaf 2) , 3)\n _ = refl\n\n -- Show the definition satisfies the specification,\n -- via using 'wpState' to compute weakest precondition semantics of 'relabel'\n -- and formulating desired correctness property:\n correctnessRelabel : forall {a : Set}\n -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set}\n -> (c : State a)\n (f : a -> State b)\n -> \u2200 i P\n -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n -- Proof by induction on input tree.\n -- Proof of Node constructor case is proving\n -- statePT (relabel l >>= (\u03bb l\u2019 \u2192 relabel r >>= (\u03bb r\u2019 \u2192 Pure (Node l\u2019 r\u2019)))) (P (Node l r , i)) i\n -- Not obvious how apply induction hypothesis (IH states that P holds for l and r)\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification, by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence, specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b}\n ( mx : State a)\n ( f : a -> State b) P i\n -> statePT (wpState f \\_ -> P) mx i\n -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b}\n (mx : State a)\n (f : a -> State b)\n spec\n -> \u2200 P i\n -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i)\n -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P))\n -> statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s}\n -> SpecK {zero} (a \u00d7 s) (b \u00d7 s)\n -> a\n -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a)\n (trans (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr\n -> Pair (fl \u2261 seq s sl) (s + sl \u2261 s')\n -> Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'')\n -> Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s\n -> wpSpec relabelSpec P (t , s)\n -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set}\n -> ((x : a) -> Free C R (b x))\n -> ((x : a) -> b x -> Set)\n -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set}\n -> (c : Free C R a) (f : a -> Free C R b)\n -> \u2200 P\n -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P = cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R}\n -> (a -> Free C R b)\n -> (b -> Free C R c)\n -> a\n -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set}\n -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c)\n -> wpCR f1 \u2291 wpCR f2\n -> wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","old_contents":"module PredicateTransformers where\n\nopen import Prelude hiding (map; all)\nopen import Level hiding (lift)\n\n------------------------------------------------------------------------------\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\n------------------------------------------------------------------------------\nABSTRACT\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nrefinement type https:\/\/en.wikipedia.org\/wiki\/Refinement_(computing)\n- type with a predicate which must hold for any element of the refined type\n- can express\n - preconditions when used as function args\n - postconditions when used as return types\n\nthe problem statement is centered around \/compositionality\/\n- pure functions enable compositional reasoning\n- effectful programs require reasoning about context\n- paper provides mechanism for reasoning about effectful programs (and for program calculation)\n\nmain idea\n- represent effectful computation as a free monad\n- write an \"interpreter\" that computes a predicate transformer\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to use this refinement relation to\n - show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\nmodule Free where\n -- Free : represents syntax of an effectful computation\n -- C : type of commands\n -- Free C R : returns an 'a' or issues command c : C\n -- For each c : C, there is a set of responses R c\n -- - R : C -> Set : is type family of responses for commands\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\" input to output\n -- input\n -- - pure function 'a\u2192b : a -> b'\n -- - postcondition 'b -> Set' (on output of a\u2192b)\n -- output\n -- - weakest precondition 'a -> Set' on input to a\u2192b that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n module WP0Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data isEven : \u2115 \u2192 Set where\n even-z : isEven Nat.zero\n even-s : {n : \u2115} \u2192 isEven n \u2192 isEven (Nat.suc (Nat.suc n))\n\n isEvenWP : \u2115 -> Set\n isEvenWP = wp0 (_+ 2) isEven\n\n _ : isEvenWP \u2261 isEven \u2218 (_+ 2)\n _ = refl\n\n _ : Set\n _ = isEvenWP 5\n\n _ : isEvenWP 5 \u2261 isEven 7\n _ = refl\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - enable via making dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n -- every element a that satisfies P is contained in the set of things that satifies Q\n --\n -- implication between predicates\n --\n -- P a says a \u2208 P\n -- if the extents of P, Q : A \u2192 Set is taken to be\n -- the subset of A satisfying predicates P, Q\n --\n -- the extent of P : A -> Set is the subset of terms of type A satisfying P.\n -- \"extent\" indicates a kind of black-box view of things,\n -- e.g., the \"extent\" of a function is the set of its input-output pairs.\n --\n -- https:\/\/en.wikipedia.org\/wiki\/Extension_%28semantics%29\n -- the extension of a concept, idea, or sign consists of the things to which it applies\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n module \u2286Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data _\u2264_ : \u2115 \u2192 \u2115 \u2192 Set where\n z\u2264n : \u2200 {n : \u2115} \u2192 0 \u2264 n\n s\u2264s : \u2200 {m n : \u2115} \u2192 m \u2264 n \u2192 Nat.suc m \u2264 Nat.suc n\n\n \u22641\u2286\u22642 : (_\u2264 1) \u2286 (_\u2264 2)\n -- P \u2286 Q\n -- \u2200 a -> P a -> Q a\n -- (a : \u2115) -> (a \u2264 1) -> (a \u2264 2)\n \u22641\u2286\u22642 Zero _ = z\u2264n\n \u22641\u2286\u22642 (Succ Zero) (s\u2264s _) = s\u2264s z\u2264n\n\n {- cannot be proved\n \u22642\u2286\u22641 : (_\u2264 2) \u2286 (_\u2264 1)\n \u22642\u2286\u22641 Zero _ = z\u2264n\n \u22642\u2286\u22641 (Succ a) (s\u2264s p) = {!!}\n -}\n\n -- refinement relation between PTs : pt1 refined by pt2\n -- (same thing as \u2286, but one level up)\n -- for every post condition, pt2 weakens the precondition\n -- \"weakest\" is the right side : pt2 P\n --\n -- pt1 , pt2 : preconditions for satisfying a program\n -- refinement : pt1 maybe be a stronger requirement than pt2\n --\n -- pt2 is a refinement because it weakens what is required by pt1\n -- e.g., pt1 pt2\n -- >= 20 >= 10\n --\n -- weakening the input assumptions gives a stronger result\n --\n -- contract says establish a pre and I will give you a post, i.e.,\n -- \"stronger\" post condition\n -- or \"weaker\" pre condition\n --\n -- refinement of predicate transformers\n --\n -- use case: relate different implementations of the \"same program\"\n -- pt1 \u2291 pt2 : pt1 is refined by pt2\n -- - if pt1 is understood as giving preconditions which satisfy\n -- postcondition P for some given program f1\n -- then his says the precondition can be \/weakened\/ and still guarantee the postcondition\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g\n -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation to\n - relate PT semantics between programs and specifications\n - show a program satisfies its specification\n - show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n - corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n note : for =Free C R b= programs means generating the same AST\n\n instead, define predicate transformers that give \/interpretations\/\n to a particular choice of command type C and response family R\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public using (_+_; _>_; _*_; s\u2264s; z\u2264n)\n renaming (\u2115 to Nat; zero to Zero; suc to Succ)\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n -- one command\n data C : Set where\n Abort : C -- no continuation\n\n -- response code : the program does not execute further\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n -- BIG-STEP SEMANTICS specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n module \u21d3Test where\n _ : Expr\n _ = Val 3\n _ : Expr\n _ = Div (Val 3) (Val 0)\n\n _ : Val 0 \u21d3 0\n _ = \u21d3Base\n\n _ : Val 3 \u21d3 3\n _ = \u21d3Base\n\n _ : Div (Val 3) (Val 3) \u21d3 1\n _ = \u21d3Step \u21d3Base \u21d3Base\n\n _ : Div (Val 9) (Val 3) \u21d3 3\n _ = \u21d3Step \u21d3Base \u21d3Base\n\n _ : Div (Val 9) (Div (Val 3) (Val 1)) \u21d3 3\n _ = \u21d3Step \u21d3Base (\u21d3Step \u21d3Base \u21d3Base)\n\n _ : Div (Div (Val 9) (Val 1)) (Div (Val 3) (Val 1)) \u21d3 3\n _ = \u21d3Step (\u21d3Step \u21d3Base \u21d3Base) (\u21d3Step \u21d3Base \u21d3Base)\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val n \u27e7 = return n\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\nl ->\n \u27e6 er \u27e7 >>= \\nr ->\n nl \u00f7 nr\n\n module InterpTest where\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n _ : evv \u2261 Pure 3\n _ = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n _ : evd \u2261 Pure 1\n _ = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n _ : evd0 \u2261 Step Abort (\u03bb ())\n _ = refl\n\n -- relate big step semantics to monadic interpreter\n\n -- convert post-condition for a pure function 'f : (x : A) -> b x'\n -- to post-condition on partial functions 'f : (x : A) -> Partial (b x)'\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba -- P holds if operation produces a value\n mustPT _ _ (Step Abort _) = \u22a5 -- if operation fails, there is no result to apply P to\n -- note: could give \u22a4 here, but want to rule out failure\n -- (total correctness)\n\n module MustPTTest where\n _ : Expr -> Nat -> Set\n _ = _\u21d3_\n\n _ : Set\n _ = Val 1 \u21d3 1\n\n _ : Expr -> Partial Nat -> Set\n _ = mustPT _\u21d3_\n\n _ : Set\n _ = mustPT _\u21d3_ (Val 1) (Pure 1)\n\n _ : mustPT _\u21d3_ (Val 1) (Pure 1)\n _ = \u21d3Base\n\n _ : Set\n _ = mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n {- a value of this type cannot be constructed\n _ : mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n _ = {!!}\n -}\n _ : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb n -> Pure 3 >>= _\u00f7 n))\n _ = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n _ : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n _ = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n _ : Set\n _ = mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n\n {-\n to relate big step semantics to monadic interpreter\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - _\u21d3_ relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n\n to call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results P' : Partial b -> Set\n - done via 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so mustPT Abort case returns empty type\n -}\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n module WpPartialTest where\n _ : Expr -> Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n _ = refl\n\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n _ = \u21d3Base\n\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n _ = \u21d3Step \u21d3Base \u21d3Base\n _ : Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n\n {- this type cannot be constructed\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n _ = {!!}\n -}\n _ : Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n\n {-\n wpPartial \u27e6_\u27e7 _\u21d3_ : a predicate on expressions\n - a way to express SUCCESSFUL evaluation of an Expr with intended semantics\n\n \u2200 exprs satisfying this predicate\n - monadic interpreter and\n - relational big-step specification\n must agree on result of evaluation\n\n for a given Expr, satisfaction of predicate means\n - Expr evaluates to a value by big-step operational semantics\n - if interpreter returns a value at all\n then that value agrees with big step semantics\n - if interpreter aborts\n then have a proof of \u22a5 (so, it does not abort)\n which will NOT agree with \u21d3 big-step semantics since \u21d3 does not allow div-by-0\n\n What does this say about correctness of interpreter?\n\n does not specify how many Expr that satisfy that characterization also satisfy wpPartial \u27e6_\u27e7 _\u21d3_\n\n to understand wpPartial \u27e6_\u27e7 _\u21d3_ better\n - define SafeDiv : safety criteria that we believe in\n - prove every expression that satisfies SafeDiv satisfies wpPartial \u27e6_\u27e7 _\u21d3_\n - SafeDiv refines wpPartial \u27e6_\u27e7 _\u21d3_\n - if we gave \u22a4 above, then 'correct' would not say that SafeDiv rules out undefined behavior\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val _) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n module SafeDivTest where\n _ : SafeDiv (Val 3) \u2261 \u22a4\n _ = refl\n _ : SafeDiv (Val 0) \u2261 \u22a4\n _ = refl\n _ : Set\n _ = SafeDiv (Val 0)\n\n _ : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 0) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n _ = refl\n _ : Set\n _ = SafeDiv (Div (Val 3) (Val 3))\n\n _ : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val 0 \u21d3 0) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n _ = refl\n _ : Set\n _ = SafeDiv (Div (Val 3) (Val 0))\n\n {-\n expect : any expr : e for which SafeDiv e holds can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n -- (a : Expr) (SafeDiv a) (wpPartial \u27e6_\u27e7 _\u21d3_ a)\n correct (Val _) _ = \u21d3Base\n correct (Div el er) (er\u21d30\u2192\u22a5 , (sdel , sder))\n with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n ... | Pure _ | Pure Zero | _ | er\u21d30 = magic (er\u21d30\u2192\u22a5 er\u21d30)\n ... | Pure _ | Pure (Succ _) | el\u21d3nl | er\u21d3Snr = \u21d3Step el\u21d3nl er\u21d3Snr\n ... | Pure _ | Step Abort _ | _ | ()\n ... | Step Abort _ | _ | () | _\n\n module CorrectTest where\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n _ = \u21d3Base\n\n _ : Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 1))\n _ = \u21d3Step \u21d3Base \u21d3Base\n {-\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 0))\n _ = {!!}\n -}\n {-\n Generalization.\n Instead of manually defining SafeDiv,\n define more general predicate\n characterising the domain of a partial function using wpPartial.\n -}\n\n -- domain is well-defined Exprs (i.e., no div-by-0)\n -- returns \u22a4 on Pure; \u22a5 on Step Abort ...\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n module DomTest where\n _ : dom \u27e6_\u27e7 \u2261 \u03bb z \u2192 mustPT (\u03bb x _ \u2192 \u22a4' zero) z \u27e6 z \u27e7\n _ = refl\n _ : dom \u27e6_\u27e7 (Val 0) \u2261 \u22a4' zero\n _ = refl\n _ : dom \u27e6_\u27e7 (Div (Val 1) (Val 0)) \u2261 \u22a5\n _ = refl\n\n -- can show that the two semantics agree precisely on the domain of the interpreter:\n\n -- everything in domain of interpreter\n -- gets evaluated in agreement with big step semantics\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n -- only things interpreted in agreement with big step semantics\n -- are those that are in domain of interpreter\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on argument expression\n\n sound (Val _) _ = \u21d3Base\n sound (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div _ _) () | Pure _ | Pure Zero | _ | _\n sound (Div _ _) _ | Pure nl | Pure (Succ nr) | \u22a4'zero\u2192el\u21d3nl | \u22a4'zero\u2192er\u21d3Succnr\n = \u21d3Step (\u22a4'zero\u2192el\u21d3nl tt) (\u22a4'zero\u2192er\u21d3Succnr tt)\n sound (Div _ _) () | Pure _ | Step Abort _ | _ | _\n sound (Div _ _) () | Step Abort _ | _ | _ | _\n\n inDom : {n : Nat}\n -> (e : Expr)\n -> \u27e6 e \u27e7 == Pure n\n -> dom \u27e6_\u27e7 e\n inDom (Val _) _ = tt\n inDom (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div _ _) () | Pure _ | Pure Zero\n inDom (Div _ _) _ | Pure _ | Pure (Succ _) = tt\n inDom (Div _ _) () | Pure _ | Step Abort _\n inDom (Div _ _) () | Step Abort _ | _\n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n : (e : Expr) (n : Nat)\n -> \u27e6 e \u27e7 \u2261 Pure n\n -> e \u21d3 n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n e _ eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} Diveler\u21d3_nldivSuccn\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 { _} { _} () | Pure _ | _ | Pure Zero\n wpPartial1 {el} { _} _ | Pure nl | [[[ eqnl ]]] | Pure (Succ _) = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n el nl eqnl\n wpPartial1 { _} { _} () | Pure _ | _ | Step Abort _\n wpPartial1 { _} { _} () | Step Abort _ | _ | _\n\n wpPartial2 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} _ with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 { _} {er} _ | Pure _ | _ | Pure nr | [[[ eqnr ]]] = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n er nr eqnr\n wpPartial2 { _} { _} () | Pure _ | _ | Step Abort _ | _\n wpPartial2 { _} { _} () | Step Abort _ | _ | _ | _\n\n complete (Val _) _ = tt\n complete (Div el er) h\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div _ _) h | Pure nl | [[[ \u27e6el\u27e7\u2261Purenl ]]] | Pure Zero | [[[ \u27e6er\u27e7\u2261Pure0 ]]] | _ | _\n -- Goal\n -- mustPT (\u03bb _ _ \u2192 \u22a4' zero)\n -- (Div el er) (Pure nl >>= (\u03bb nl' \u2192 Pure Zero >>= _\u00f7_ nl'))\n -- \u22a5\n -- Context\n -- h : mustPT _\u21d3_ (Div el er) (\u27e6 el \u27e7 >>= (\u03bb nl' \u2192 \u27e6 er \u27e7 >>= _\u00f7_ nl'))\n rewrite\n \u27e6el\u27e7\u2261Purenl -- h : mustPT _\u21d3_ (Div el er) (\u27e6 er \u27e7 >>= _\u00f7_ nl)\n | \u27e6er\u27e7\u2261Pure0 -- h : \u22a5\n = h -- magic h\n complete (Div _ _) _ | Pure _ | _ | Pure (Succ _) | _ | _ | _ = tt\n complete (Div _ _) _ | Pure _ | _ | Step Abort _ | _ | _ | ()\n complete (Div _ _) _ | Step Abort _ | _ | _ | _ | () | _\n\n module CompleteTest where\n {-\n _ : mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n _ = {!!}\n -}\n _ : Set\n _ = mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) <-> (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement\n - to relate Kleisli morphisms\n - to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n this example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre and post condition\n - to its implementation\n\n add top two elements; fails if stack has too few elements\n\n show how to prove definition meets its specification\n -}\n\n -- define specification in terms of a pre\/post condition\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n -- [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n -- for non-dependent examples (e.g., type b does not depend on x : a)\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function (to discard unused arg of type a)\n\n -- describes desired postcondition for addition function\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n -- spec for addition function\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n -- pre post\n {-\n need to relate spec to an implementation\n 'wpPartial' assigns predicate transformer semantics to functions\n 'wpSpec' assigns predicate transformer semantics to specifications\n -}\n -- given a spec, Spec a b\n -- computes weakest precondition that satisfies an arbitrary postcondition P\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\a -> (pre a) -- i.e., spec\u2019s precondition should hold and\n \u2227 (post a \u2286 P a) -- spec's postcondition must imply P\n\n -- using 'wpSpec' can now find a program 'add' that \"refines\" 'addSpec'\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs0 =\n pop xs0 >>= \\{(x1 , xs1) ->\n pop xs1 >>= \\{(x2 , xs2) ->\n return ((x1 + x2) :: xs2)}}\n\n -- verify correct\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd _ (_ :: Nil) (s\u2264s () , _)\n correctnessAdd P a@(x :: y :: xs ) (s\u2264s (s\u2264s z\u2264n) , post_addSpec_a_\u2286P_a)\n -- wpPartial add P (x :: y :: xs)\n = post_addSpec_a_\u2286P_a (x + y :: xs) AddStep\n -- paper version has \"extra\" 'Nil\" case\n--correctnessAdd P Nil ( () , _)\n--correctnessAdd P (_ :: Nil) (s\u2264s () , _)\n--correctnessAdd P (x :: y :: xs ) ( _ , H) = H (x + y :: xs) AddStep\n\n {-\n repeat: this example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition\n - to its implementation\n\n compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n get : State s\n get = Step Get return\n\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n statePT : forall {l l'} -> {b : Set l} -> (b \u00d7 s -> Set l') -> State b -> (s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> ( a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> ( a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s') = (flatten t' == (seq (s) (size t))) \u2227 (s + size t == s')\n\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n correctnessRelabel : forall {a : Set} -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set} -> (c : State a) (f : a -> State b) ->\n \u2200 i P -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification,\n -- by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence,\n -- specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b} (mx : State a) (f : a -> State b) P i ->\n statePT (wpState f \\_ -> P) mx i -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b} (mx : State a) (f : a -> State b) spec ->\n \u2200 P i -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i) ->\n Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P)) ->\n statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s} -> SpecK {zero} (a \u00d7 s) (b \u00d7 s) -> a -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a) (trans\n (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr ->\n Pair (fl \u2261 seq s sl) (s + sl \u2261 s') ->\n Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'') ->\n Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s -> wpSpec relabelSpec P (t , s) -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set} ->\n ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set} -> (c : Free C R a) (f : a -> Free C R b) ->\n \u2200 P -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P =\n cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R} -> (a -> Free C R b) -> (b -> Free C R c) -> a -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set} -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c) ->\n wpCR f1 \u2291 wpCR f2 ->\n wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"dac9f1ba9752874315b02702e10ed439c6530929","subject":"Updated setoids note.","message":"Updated setoids note.\n\nIgnore-this: 207219e84550b3773e01821a2f860d8f\n\ndarcs-hash:20120513172543-3bd4e-799455fba458d48009025fccd8ec6c7a3139f4d2.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/setoids\/FOTC.agda","new_file":"notes\/setoids\/FOTC.agda","new_contents":"------------------------------------------------------------------------------\n-- Using setoids for formalizing the FOTC\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Tested with the development version of Agda on 13 May 2012.\n\n{-\n\nFrom Peter emails:\n\nAt that time I was considering an inductive data type D and an\ninductively defined equality on D. But I think what we are doing now\nis better.\n\nFor =: The reason is that with the propositional identity we have\nidentity elimination which lets us replace equals by equals. We cannot\ndo that in general if we have setoid equality.\n\nFor D: the reason why I prefer to postulate D is that we have no use\nfor the inductive structure of D, and this inductive structure would\nmake e g 0 + 0 different from 0. So in that case we need setoid\nequality.\n\n-}\n\nmodule FOTC where\n\n-- References:\n--\n-- \u2022 Gilles Barthe, Venanzio Capretta, and Olivier Pons. Setoids in\n-- type theory. Journal of Functional Programming, 13(2):261\u2013293,\n-- 2003.\n\n------------------------------------------------------------------------------\n\nmodule Setoid where\n\n -- From Peter's slides\n -- http:\/\/www.cse.chalmers.se\/~peterd\/slides\/Amagasaki.pdf\n\n -- We add 3 to the fixities of the standard library.\n infixl 9 _\u00b7_ -- The symbol is '\\cdot'.\n infix 7 _\u2250_\n\n data D : Set where\n K S : D\n _\u00b7_ : D \u2192 D \u2192 D\n\n -- The setoid equality\n data _\u2250_ : D \u2192 D \u2192 Set where\n refl : \u2200 x \u2192 x \u2250 x\n sym : \u2200 {x y} \u2192 x \u2250 y \u2192 y \u2250 x\n trans : \u2200 {x y z} \u2192 x \u2250 y \u2192 y \u2250 z \u2192 x \u2250 z\n cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2250 x\u2082 \u2192 y\u2081 \u2250 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2250 x\u2082 \u00b7 y\u2082\n Kax : \u2200 x y \u2192 K \u00b7 x \u00b7 y \u2250 x\n Sax : \u2200 x y z \u2192 S \u00b7 x \u00b7 y \u00b7 z \u2250 x \u00b7 z \u00b7 (y \u00b7 z)\n\n -- 13 May 2012. It seems we cannot define the identity elimination\n -- using the setoid equality.\n --\n -- subst : (A : D \u2192 Set) \u2192 \u2200 x y \u2192 x \u2250 y \u2192 A x \u2192 A y\n\n -- The identity type\n data _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n -- 13 May 2012: Using the inductive structure we cannot prove\n --\n -- K \u00b7 x \u00b7 y \u2261 x,\n --\n -- we need the setoid equality.\n -- K-eq : \u2200 {x y} \u2192 (K \u00b7 x \u00b7 y) \u2261 x\n\n------------------------------------------------------------------------------\n\nmodule LeibnizEquality where\n\n data D : Set where\n K S : D\n _\u00b7_ : D \u2192 D \u2192 D\n\n -- (Barthe et al. 2003, p. 262) use the Leibniz equality when\n -- they talk about setoids.\n\n -- Using the Leibniz equality\n -- (Adapted from Agda\/examples\/lib\/Logic\/Leibniz.agda)\n\n infix 7 _\u2261_\n\n _\u2261_ : D \u2192 D \u2192 Set\u2081\n x \u2261 y = (A : D \u2192 Set) \u2192 A x \u2192 A y\n\n -- we can prove the setoids properties\n\n refl : \u2200 x \u2192 x \u2261 x\n refl x A Ax = Ax\n\n sym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\n sym {x} h A Ay = h (\u03bb z \u2192 A z \u2192 A x) (\u03bb Ax \u2192 Ax) Ay\n\n trans : \u2200 x y z \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n trans x y z h\u2081 h\u2082 A Ax = h\u2082 A (h\u2081 A Ax)\n\n -- and the identity elimination\n\n subst : (A : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\n subst A x\u2261y = x\u2261y A\n\n -- and the congruency\n\n cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2261 x\u2082 \u2192 y\u2081 \u2261 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2261 x\u2082 \u00b7 y\u2082\n cong {x\u2081} {x\u2082} {y\u2081} {y\u2082} h\u2081 h\u2082 A Ax\u2081x\u2082 =\n h\u2082 (\u03bb z \u2192 A (x\u2082 \u00b7 z)) (h\u2081 (\u03bb z \u2192 A (z \u00b7 y\u2081)) Ax\u2081x\u2082)\n","old_contents":"------------------------------------------------------------------------------\n-- Using setoids for formalizing the FOTC\n------------------------------------------------------------------------------\n\n-- Tested with the development version of Agda on 23 March 2012.\n\n{-\nFrom Peter emails:\n\nAt that time I was considering an inductive data type D and an\ninductively defined equality on D. But I think what we are doing now\nis better.\n\nFor = : The reason is that with the propositional identity we have\nidentity elimination which lets us replace equals by equals. We cannot\ndo that in general if we have setoid equality.\n\nFor D: the reason why I prefer to postulate D is that we have no use\nfor the inductive structure of D, and this inductive structure would\nmake e g 0 + 0 different from 0. So in that case we need setoid\nequality.\n-}\n\nmodule FOTC where\n\n------------------------------------------------------------------------------\n\nmodule PeterEquality where\n\n -- We add 3 to the fixities of the standard library.\n infixl 9 _\u00b7_ -- The symbol is '\\cdot'.\n\n data D : Set where\n K S : D\n _\u00b7_ : D \u2192 D \u2192 D\n\n -- From Peter's slides\n -- http:\/\/www.cse.chalmers.se\/~peterd\/slides\/Amagasaki.pdf\n\n infix 7 _\u2261_\n\n data _\u2261_ : D \u2192 D \u2192 Set where\n refl : \u2200 x \u2192 x \u2261 x\n sym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\n trans : \u2200 {x y z} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2261 x\u2082 \u2192 y\u2081 \u2261 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2261 x\u2082 \u00b7 y\u2082\n Kax : \u2200 x y \u2192 K \u00b7 x \u00b7 y \u2261 x\n Sax : \u2200 x y z \u2192 S \u00b7 x \u00b7 y \u00b7 z \u2261 x \u00b7 z \u00b7 (y \u00b7 z)\n\n -- It seems we cannot define the identity elimination using the setoid\n -- equality.\n --\n -- subst : \u2200 {x y} (A : D \u2192 Set) \u2192 x \u2261 y \u2192 A x \u2192 A y\n\nmodule PeterD where\n\n -- We add 3 to the fixities of the standard library.\n infixl 9 _\u00b7_ -- The symbol is '\\cdot'.\n\n data D : Set where\n zero succ true false : D\n _\u00b7_ : D \u2192 D \u2192 D\n loop : D\n\n data _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n subst : (A : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\n subst A refl Ax = Ax\n\n data N : D \u2192 Set where\n zN : N zero\n sN : \u2200 {n} \u2192 N n \u2192 N (succ \u00b7 n)\n\n -- 2012-03-23: Why the inductive structure makes 0 + 0 different\n -- from 0? How to define _+_ ?\n\n------------------------------------------------------------------------------\n\nmodule LeibnizEquality where\n\n -- We add 3 to the fixities of the standard library.\n infixl 9 _\u00b7_ -- The symbol is '\\cdot'.\n\n data D : Set where\n K S : D\n _\u00b7_ : D \u2192 D \u2192 D\n\n -- (Barthe et al. 2003, p. 262) use the Leibniz equality when\n -- they talk about setoids.\n\n -- \u2022 Gilles Barthe, Venanzio Capretta, and Olivier Pons. Setoids in\n -- type theory. Journal of Functional Programming, 13(2):261\u2013293,\n -- 2003\n\n -- Using the Leibniz equality\n -- (Adapted from Agda\/examples\/lib\/Logic\/Leibniz.agda)\n\n infix 7 _\u2261_\n\n _\u2261_ : D \u2192 D \u2192 Set\u2081\n x \u2261 y = (A : D \u2192 Set) \u2192 A x \u2192 A y\n\n -- we can prove the setoids properties\n\n refl : \u2200 x \u2192 x \u2261 x\n refl x A Ax = Ax\n\n sym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\n sym {x} x\u2261y A Ay = x\u2261y (\u03bb z \u2192 A z \u2192 A x) (\u03bb Ax \u2192 Ax) Ay\n\n trans : \u2200 x y z \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n trans x y z x\u2261y y\u2261z A Ax = y\u2261z A (x\u2261y A Ax)\n\n -- and the identity elimination\n\n subst : (A : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\n subst A x\u2261y = x\u2261y A\n\n -- but it seems we cannot prove the congruency\n\n -- cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2261 x\u2082 \u2192 y\u2081 \u2261 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2261 x\u2082 \u00b7 y\u2082\n -- cong x\u2081\u2261x\u2082 y\u2081\u2261y\u2082 A Ax\u2081y\u2081 = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"16f8a95227efdacdb93599ca7b8ddfd847c2e47a","subject":"Document P.D.Value.","message":"Document P.D.Value.\n\nOld-commit-hash: bfaab88fbd6365ff0b9046dd999ccd1362c526d6\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Denotation\/Value.agda","new_file":"Parametric\/Denotation\/Value.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Values for standard evaluation (Def. 3.1 and 3.2, Fig. 4c and 4f).\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\n\nmodule Parametric.Denotation.Value\n (Base : Type.Structure)\n where\n\nopen import Base.Denotation.Notation\n\nopen Type.Structure Base\n\n-- Extension point: Values for base types.\nStructure : Set\u2081\nStructure = Base \u2192 Set\n\nmodule Structure (\u27e6_\u27e7Base : Structure) where\n -- We provide: Values for arbitrary types.\n \u27e6_\u27e7Type : Type -> Set\n \u27e6 base \u03b9 \u27e7Type = \u27e6 \u03b9 \u27e7Base\n \u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\n -- This means: Overload \u27e6_\u27e7 to mean \u27e6_\u27e7Type.\n meaningOfType : Meaning Type\n meaningOfType = meaning \u27e6_\u27e7Type\n\n -- We also provide: Environments of such values.\n open import Base.Denotation.Environment Type \u27e6_\u27e7Type public\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Value domains for languages described in Plotkin style\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\n\nmodule Parametric.Denotation.Value\n (Base : Type.Structure)\n where\n\nopen import Base.Denotation.Notation\n\nopen Type.Structure Base\n\nStructure : Set\u2081\nStructure = Base \u2192 Set\n\nmodule Structure (\u27e6_\u27e7Base : Structure) where\n \u27e6_\u27e7Type : Type -> Set\n \u27e6 base \u03b9 \u27e7Type = \u27e6 \u03b9 \u27e7Base\n \u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\n meaningOfType : Meaning Type\n meaningOfType = meaning \u27e6_\u27e7Type\n\n open import Base.Denotation.Environment Type \u27e6_\u27e7Type public\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a2f7958b6a6e7245442d1e150092aacdc7c8707d","subject":"Add a hole in the examples that's strictly unnecessary","message":"Add a hole in the examples that's strictly unnecessary\n\n... but potentially confusing if it isn't there\n","repos":"bch29\/agda-holes","old_file":"examples\/Propositional.agda","new_file":"examples\/Propositional.agda","new_contents":"module Propositional where\n\nopen import Data.Nat\nopen import Relation.Binary.PropositionalEquality\n\nopen import Holes.Term using (\u231e_\u231f)\nopen import Holes.Cong.Propositional\n\nopen \u2261-Reasoning\n\n--------------------------------------------------------------------------------\n-- Some easy lemmas\n--------------------------------------------------------------------------------\n\n+-zero : \u2200 a \u2192 a + zero \u2261 a\n+-zero zero = refl\n+-zero (suc a) rewrite +-zero a = refl\n\n+-suc : \u2200 a b \u2192 a + suc b \u2261 suc (a + b)\n+-suc zero b = refl\n+-suc (suc a) b rewrite +-suc a b = refl\n\n+-assoc : \u2200 a b c \u2192 a + b + c \u2261 a + (b + c)\n+-assoc zero b c = refl\n+-assoc (suc a) b c rewrite +-assoc a b c = refl\n\n--------------------------------------------------------------------------------\n-- Proofs by equational reasoning\n--------------------------------------------------------------------------------\n\n-- commutativity of addition proved traditionally\n\n+-comm\u2081 : \u2200 a b \u2192 a + b \u2261 b + a\n+-comm\u2081 zero b = sym (+-zero b)\n+-comm\u2081 (suc a) b =\n suc \u231e a + b \u231f \u2261\u27e8 cong suc (+-comm\u2081 a b) \u27e9\n suc (b + a) \u2261\u27e8 sym (+-suc b a) \u27e9\n b + suc a \u220e\n\n-- commutativity of addition proved with holes\n\n+-comm : \u2200 a b \u2192 a + b \u2261 b + a\n+-comm zero b = sym (+-zero b)\n+-comm (suc a) b =\n suc \u231e a + b \u231f \u2261\u27e8 cong! (+-comm a b) \u27e9\n suc (b + a) \u2261\u27e8 cong! (+-suc b a) \u27e9\n b + suc a \u220e\n\n-- distributivity of multiplication over addition proved traditionally\n\n*-distrib-+\u2081 : \u2200 a b c \u2192 a * (b + c) \u2261 a * b + a * c\n*-distrib-+\u2081 zero b c = refl\n*-distrib-+\u2081 (suc a) b c =\n b + c + a * (b + c) \u2261\u27e8 cong (\u03bb h \u2192 b + c + h) (*-distrib-+\u2081 a b c) \u27e9\n (b + c) + (a * b + a * c) \u2261\u27e8 sym (+-assoc (b + c) (a * b) (a * c)) \u27e9\n ((b + c) + a * b) + a * c \u2261\u27e8 cong (\u03bb h \u2192 h + a * c) (+-assoc b c (a * b)) \u27e9\n (b + (c + a * b)) + a * c \u2261\u27e8 cong (\u03bb h \u2192 (b + h) + a * c) (+-comm c (a * b)) \u27e9\n (b + (a * b + c)) + a * c \u2261\u27e8 cong (\u03bb h \u2192 h + a * c) (sym (+-assoc b (a * b) c)) \u27e9\n ((b + a * b) + c) + a * c \u2261\u27e8 +-assoc (b + a * b) c (a * c) \u27e9\n (b + a * b) + (c + a * c)\n \u220e\n\n-- distributivity of multiplication over addition proved with holes\n\n*-distrib-+ : \u2200 a b c \u2192 a * (b + c) \u2261 a * b + a * c\n*-distrib-+ zero b c = refl\n*-distrib-+ (suc a) b c =\n b + c + \u231e a * (b + c) \u231f \u2261\u27e8 cong! (*-distrib-+ a b c) \u27e9\n \u231e (b + c) + (a * b + a * c) \u231f \u2261\u27e8 cong! (+-assoc (b + c) (a * b) (a * c)) \u27e9\n \u231e (b + c) + a * b \u231f + a * c \u2261\u27e8 cong! (+-assoc b c (a * b)) \u27e9\n (b + \u231e c + a * b \u231f) + a * c \u2261\u27e8 cong! (+-comm c (a * b)) \u27e9\n \u231e b + (a * b + c) \u231f + a * c \u2261\u27e8 cong! (+-assoc b (a * b) c) \u27e9\n \u231e ((b + a * b) + c) + a * c \u231f \u2261\u27e8 cong! (+-assoc (b + a * b) c (a * c)) \u27e9\n (b + a * b) + (c + a * c)\n \u220e\n","old_contents":"module Propositional where\n\nopen import Data.Nat\nopen import Relation.Binary.PropositionalEquality\n\nopen import Holes.Term using (\u231e_\u231f)\nopen import Holes.Cong.Propositional\n\nopen \u2261-Reasoning\n\n--------------------------------------------------------------------------------\n-- Some easy lemmas\n--------------------------------------------------------------------------------\n\n+-zero : \u2200 a \u2192 a + zero \u2261 a\n+-zero zero = refl\n+-zero (suc a) rewrite +-zero a = refl\n\n+-suc : \u2200 a b \u2192 a + suc b \u2261 suc (a + b)\n+-suc zero b = refl\n+-suc (suc a) b rewrite +-suc a b = refl\n\n+-assoc : \u2200 a b c \u2192 a + b + c \u2261 a + (b + c)\n+-assoc zero b c = refl\n+-assoc (suc a) b c rewrite +-assoc a b c = refl\n\n--------------------------------------------------------------------------------\n-- Proofs by equational reasoning\n--------------------------------------------------------------------------------\n\n-- commutativity of addition proved traditionally\n\n+-comm\u2081 : \u2200 a b \u2192 a + b \u2261 b + a\n+-comm\u2081 zero b = sym (+-zero b)\n+-comm\u2081 (suc a) b =\n suc \u231e a + b \u231f \u2261\u27e8 cong suc (+-comm\u2081 a b) \u27e9\n suc (b + a) \u2261\u27e8 sym (+-suc b a) \u27e9\n b + suc a \u220e\n\n-- commutativity of addition proved with holes\n\n+-comm : \u2200 a b \u2192 a + b \u2261 b + a\n+-comm zero b = sym (+-zero b)\n+-comm (suc a) b =\n suc \u231e a + b \u231f \u2261\u27e8 cong! (+-comm a b) \u27e9\n suc (b + a) \u2261\u27e8 cong! (+-suc b a) \u27e9\n b + suc a \u220e\n\n-- distributivity of multiplication over addition proved traditionally\n\n*-distrib-+\u2081 : \u2200 a b c \u2192 a * (b + c) \u2261 a * b + a * c\n*-distrib-+\u2081 zero b c = refl\n*-distrib-+\u2081 (suc a) b c =\n b + c + a * (b + c) \u2261\u27e8 cong (\u03bb h \u2192 b + c + h) (*-distrib-+\u2081 a b c) \u27e9\n (b + c) + (a * b + a * c) \u2261\u27e8 sym (+-assoc (b + c) (a * b) (a * c)) \u27e9\n ((b + c) + a * b) + a * c \u2261\u27e8 cong (\u03bb h \u2192 h + a * c) (+-assoc b c (a * b)) \u27e9\n (b + (c + a * b)) + a * c \u2261\u27e8 cong (\u03bb h \u2192 (b + h) + a * c) (+-comm c (a * b)) \u27e9\n (b + (a * b + c)) + a * c \u2261\u27e8 cong (\u03bb h \u2192 h + a * c) (sym (+-assoc b (a * b) c)) \u27e9\n ((b + a * b) + c) + a * c \u2261\u27e8 +-assoc (b + a * b) c (a * c) \u27e9\n (b + a * b) + (c + a * c)\n \u220e\n\n-- distributivity of multiplication over addition proved with holes\n\n*-distrib-+ : \u2200 a b c \u2192 a * (b + c) \u2261 a * b + a * c\n*-distrib-+ zero b c = refl\n*-distrib-+ (suc a) b c =\n b + c + \u231e a * (b + c) \u231f \u2261\u27e8 cong! (*-distrib-+ a b c) \u27e9\n (b + c) + (a * b + a * c) \u2261\u27e8 cong! (+-assoc (b + c) (a * b) (a * c)) \u27e9\n \u231e (b + c) + a * b \u231f + a * c \u2261\u27e8 cong! (+-assoc b c (a * b)) \u27e9\n (b + \u231e c + a * b \u231f) + a * c \u2261\u27e8 cong! (+-comm c (a * b)) \u27e9\n \u231e b + (a * b + c) \u231f + a * c \u2261\u27e8 cong! (+-assoc b (a * b) c) \u27e9\n \u231e ((b + a * b) + c) + a * c \u231f \u2261\u27e8 cong! (+-assoc (b + a * b) c (a * c)) \u27e9\n (b + a * b) + (c + a * c)\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3fc6dc8eead442499245bb1f54a5e9a8a2cd012e","subject":"Workaround broken pattern synonyms","message":"Workaround broken pattern synonyms\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_\u2264_; _==_)\nopen import Data.Nat.DivMod\nopen import Data.Bool.NP hiding (_==_)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise)\nopen import Data.Vec.NP hiding (_\u229b_) renaming (map to vmap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_)\nopen import Data.Vec.NP public using ([]; _\u2237_; head; tail; replicate)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x = cong (_\u2237_ x) (vnot\u2218vnot\u2257id xs)\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n\n where open RawMonad L.monad\nallBits : \u2200 n \u2192 Vec (Bits n) (2 ^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) rewrite \u2115\u00b0.+-comm (2 ^ n) 0 = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2 ^ n))\n#\u27e8 pred \u27e9 = count pred (allBits _)\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nopen Defs public\n","old_contents":"module Data.Bits where\n\n-- cleanup\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_\u2264_; _==_)\nopen import Data.Nat.DivMod\nopen import Data.Bool.NP hiding (_==_)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise)\nopen import Data.Vec.NP hiding (_\u229b_) renaming (map to vmap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_)\nopen import Data.Vec.NP public using ([]; _\u2237_; head; tail; replicate)\n\nBit : Set\nBit = Bool\n\npattern 0b = false\npattern 1b = true\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x = cong (_\u2237_ x) (vnot\u2218vnot\u2257id xs)\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n\n where open RawMonad L.monad\nallBits : \u2200 n \u2192 Vec (Bits n) (2 ^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) rewrite \u2115\u00b0.+-comm (2 ^ n) 0 = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2 ^ n))\n#\u27e8 pred \u27e9 = count pred (allBits _)\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"0c9f9226d80652b033e57f60c46d5d84ca504cef","subject":"Added ATP pragma for conversion rules.","message":"Added ATP pragma for conversion rules.\n\nIgnore-this: 23c2214308d27af148d99c602cff2018\n\ndarcs-hash:20100528214442-3bd4e-a2214a104d369a5b78575a474973e4c3968b0683.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Minimal.agda","new_file":"LTC\/Minimal.agda","new_contents":"------------------------------------------------------------------------------\n-- Agda as a logical framework for LTC\n------------------------------------------------------------------------------\n{-\n\nLTC Agda\n* Logical constants * Curry-Howard isomorphism\n* Equality * Identity type\n* Term language * Postulates\n* Inductive predicates * Inductive families\n-}\n\n------------------------------------------------------------------------------\n\nmodule LTC.Minimal where\n\n-- Standard library imports\n-- open import Data.Fin using ( Fin )\n-- open import Data.Nat using ( \u2115 )\n-- open import Data.String\n-- open import Relation.Binary.PropositionalEquality\n-- open \u2261-Reasoning\n\ninfixl 6 _\u2219_\ninfix 5 if_then_else_\ninfix 4 _\u2261_\n\n------------------------------------------------------------------------------\n-- The universal domain\n------------------------------------------------------------------------------\n\n-- N.B. The following module is exported by this module.\nopen import LTC.Minimal.Core public\n\n------------------------------------------------------------------------------\n-- The term language\n------------------------------------------------------------------------------\n\n-- The term language of LTC correspond to the PCF terms\n\n-- t ::= x | t t | \\x -> t\n-- | true | false | if t then t else t\n-- | 0 | succ t | pred t | isZero t\n-- | error\n-- | fix t\n\npostulate\n\n -- LTC partial booleans.\n true : D\n false : D\n if_then_else_ : D \u2192 D \u2192 D \u2192 D\n -- LTC partial natural numbers.\n zero : D\n succ : D \u2192 D\n pred : D \u2192 D\n isZero : D \u2192 D\n -- LTC abstraction.\n lam : (D \u2192 D) \u2192 D\n -- LTC application\n -- Left associative aplication operator\n -- The Agda application has higher precedence level than LTC application\n _\u2219_ : D \u2192 D \u2192 D\n -- LTC error\n error : D\n -- LTC fixed point operator\n fix : (D \u2192 D) \u2192 D\n -- fixFO : D\n\n------------------------------------------------------------------------------\n-- Equality: identity type\n------------------------------------------------------------------------------\n\n-- The LTC's equality is the propositional identity on 'D'.\n\n-- The identity type.\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n------------------------------------------------------------------------------\n-- Logical constants: Curry-Howard isomorphism\n------------------------------------------------------------------------------\n\n-- The LTC's logical constants are the type theory's logical\n-- constants via the Curry-Howard isomorphism.\n-- For the implication and the universal quantifier\n-- we use Agda (dependent) function type.\n\n-- N.B. The following modules are exported by this module.\nopen import LTC.Data.Product public\nopen import MyStdLib.Data.Empty public\nopen import MyStdLib.Data.Product public\nopen import MyStdLib.Data.Sum public\nopen import MyStdLib.Relation.Nullary public\n\n------------------------------------------------------------------------------\n-- Conversion rules\n------------------------------------------------------------------------------\n\npostulate\n -- Conversion rules for booleans.\n cB\u2081 : (d\u2081 : D){d\u2082 : D} \u2192 if true then d\u2081 else d\u2082 \u2261 d\u2081\n cB\u2082 : {d\u2081 : D}(d\u2082 : D) \u2192 if false then d\u2081 else d\u2082 \u2261 d\u2082\n{-# ATP axiom cB\u2081 #-}\n{-# ATP axiom cB\u2082 #-}\n\npostulate\n -- Conversion rules for pred.\n cP\u2081 : pred zero \u2261 zero\n cP\u2082 : (n : D) \u2192 pred (succ n) \u2261 n\n{-# ATP axiom cP\u2081 #-}\n{-# ATP axiom cP\u2082 #-}\n\npostulate\n -- Conversion rules for isZero\n cZ\u2081 : isZero zero \u2261 true\n cZ\u2082 : (n : D) \u2192 isZero (succ n) \u2261 false\n{-# ATP axiom cZ\u2081 #-}\n{-# ATP axiom cZ\u2082 #-}\n\npostulate\n -- Conversion rule for the abstraction and the application.\n cBeta : (f : D \u2192 D) \u2192 (a : D) \u2192 (lam f) \u2219 a \u2261 f a\n{-# ATP axiom cBeta #-}\n\npostulate\n -- Conversion rule for the fixed pointed operator.\n cFix : (f : D \u2192 D) \u2192 fix f \u2261 f (fix f)\n -- cFixFO : (f : D) \u2192 fixFO \u2219 f \u2261 f \u2219 (fixFO \u2219 f)\n{-# ATP axiom cFix #-}\n\n------------------------------------------------------------------------------\n-- Discrimination rules\n------------------------------------------------------------------------------\n\npostulate\n true\u2260false : \u00ac (true \u2261 false)\n 0\u2260S : {d : D} \u2192 \u00ac ( zero \u2261 succ d)\n{-# ATP axiom true\u2260false #-}\n{-# ATP axiom 0\u2260S #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Agda as a logical framework for LTC\n------------------------------------------------------------------------------\n{-\n\nLTC Agda\n* Logical constants * Curry-Howard isomorphism\n* Equality * Identity type\n* Term language * Postulates\n* Inductive predicates * Inductive families\n-}\n\n------------------------------------------------------------------------------\n\nmodule LTC.Minimal where\n\n-- Standard library imports\n-- open import Data.Fin using ( Fin )\n-- open import Data.Nat using ( \u2115 )\n-- open import Data.String\n-- open import Relation.Binary.PropositionalEquality\n-- open \u2261-Reasoning\n\ninfixl 6 _\u2219_\ninfix 5 if_then_else_\ninfix 4 _\u2261_\n\n------------------------------------------------------------------------------\n-- The universal domain\n------------------------------------------------------------------------------\n\n-- N.B. The following module is exported by this module.\nopen import LTC.Minimal.Core public\n\n------------------------------------------------------------------------------\n-- The term language\n------------------------------------------------------------------------------\n\n-- The term language of LTC correspond to the PCF terms\n\n-- t ::= x | t t | \\x -> t\n-- | true | false | if t then t else t\n-- | 0 | succ t | pred t | isZero t\n-- | error\n-- | fix t\n\npostulate\n\n -- LTC partial booleans.\n true : D\n false : D\n if_then_else_ : D \u2192 D \u2192 D \u2192 D\n -- LTC partial natural numbers.\n zero : D\n succ : D \u2192 D\n pred : D \u2192 D\n isZero : D \u2192 D\n -- LTC abstraction.\n lam : (D \u2192 D) \u2192 D\n -- LTC application\n -- Left associative aplication operator\n -- The Agda application has higher precedence level than LTC application\n _\u2219_ : D \u2192 D \u2192 D\n -- LTC error\n error : D\n -- LTC fixed point operator\n fix : (D \u2192 D) \u2192 D\n fixFO : D\n\n------------------------------------------------------------------------------\n-- Equality: identity type\n------------------------------------------------------------------------------\n\n-- The LTC's equality is the propositional identity on 'D'.\n\n-- The identity type.\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n------------------------------------------------------------------------------\n-- Logical constants: Curry-Howard isomorphism\n------------------------------------------------------------------------------\n\n-- The LTC's logical constants are the type theory's logical\n-- constants via the Curry-Howard isomorphism.\n-- For the implication and the universal quantifier\n-- we use Agda (dependent) function type.\n\n-- N.B. The following modules are exported by this module.\nopen import LTC.Data.Product public\nopen import MyStdLib.Data.Empty public\nopen import MyStdLib.Data.Product public\nopen import MyStdLib.Data.Sum public\nopen import MyStdLib.Relation.Nullary public\n\n------------------------------------------------------------------------------\n-- Conversion rules\n------------------------------------------------------------------------------\n\npostulate\n -- Conversion rules for booleans.\n cB\u2081 : (d\u2081 : D){d\u2082 : D} \u2192 if true then d\u2081 else d\u2082 \u2261 d\u2081\n cB\u2082 : {d\u2081 : D}(d\u2082 : D) \u2192 if false then d\u2081 else d\u2082 \u2261 d\u2082\n\npostulate\n -- Conversion rules for pred.\n cP\u2081 : pred zero \u2261 zero\n cP\u2082 : (n : D) \u2192 pred (succ n) \u2261 n\n{-# ATP axiom cP\u2081 #-}\n{-# ATP axiom cP\u2082 #-}\n\npostulate\n -- Conversion rules for isZero\n cZ\u2081 : isZero zero \u2261 true\n cZ\u2082 : (n : D) \u2192 isZero (succ n) \u2261 false\n\npostulate\n -- Conversion rule for the abstraction and the application.\n cBeta : (f : D \u2192 D) \u2192 (a : D) \u2192 (lam f) \u2219 a \u2261 f a\n\npostulate\n -- Conversion rule for the fixed pointed operator.\n cFix : (f : D \u2192 D) \u2192 fix f \u2261 f (fix f)\n cFixFO : (f : D) \u2192 fixFO \u2219 f \u2261 f \u2219 (fixFO \u2219 f)\n\n------------------------------------------------------------------------------\n-- Discrimination rules\n------------------------------------------------------------------------------\n\npostulate\n true\u2260false : \u00ac (true \u2261 false)\n 0\u2260S : {d : D} \u2192 \u00ac ( zero \u2261 succ d)\n{-# ATP axiom true\u2260false #-}\n{-# ATP axiom 0\u2260S #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f20333e283720fccdcb8f0a6ed25587480c9d577","subject":"Start defining ops on dependently-typed changes","message":"Start defining ops on dependently-typed changes\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/Changes.agda","new_file":"Thesis\/Changes.agda","new_contents":"module Thesis.Changes where\n\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Unit\nopen import Relation.Binary.PropositionalEquality\n\nrecord IsChangeStructure (A : Set) (ChA : Set) (ch_from_to_ : (dv : ChA) \u2192 (v1 v2 : A) \u2192 Set) : Set\u2081 where\n infixl 6 _\u2295_ _\u229d_\n field\n _\u2295_ : A \u2192 ChA \u2192 A\n fromto\u2192\u2295 : \u2200 dv v1 v2 \u2192\n ch dv from v1 to v2 \u2192\n v1 \u2295 dv \u2261 v2\n _\u229d_ : A \u2192 A \u2192 ChA\n \u229d-fromto : \u2200 (a b : A) \u2192 ch (b \u229d a) from a to b\n\n update-diff : (b a : A) \u2192 a \u2295 (b \u229d a) \u2261 b\n update-diff b a = fromto\u2192\u2295 (b \u229d a) a b (\u229d-fromto a b)\n nil : A \u2192 ChA\n nil a = a \u229d a\n nil-fromto : (a : A) \u2192 ch (nil a) from a to a\n nil-fromto a = \u229d-fromto a a\n update-nil : (a : A) \u2192 a \u2295 nil a \u2261 a\n update-nil a = update-diff a a\n\n valid : \u2200 (a : A) (da : ChA) \u2192 Set\n valid a da = ch da from a to (a \u2295 da)\n \u0394 : (a : A) \u2192 Set\n \u0394 a = \u03a3[ da \u2208 ChA ] valid a da\n \u0394\u2082 : (a1 : A) (a2 : A) \u2192 Set\n \u0394\u2082 a1 a2 = \u03a3[ da \u2208 ChA ] ch da from a1 to a2\n\n _\u2295'_ : (a1 : A) -> {a2 : A} -> (da : \u0394\u2082 a1 a2) -> A\n a1 \u2295' (da , daa) = a1 \u2295 da\n\nrecord IsCompChangeStructure (A : Set) (ChA : Set) (ch_from_to_ : (dv : ChA) \u2192 (v1 v2 : A) \u2192 Set) : Set\u2081 where\n field\n isChangeStructure : IsChangeStructure A ChA ch_from_to_\n _\u229a[_]_ : ChA \u2192 A \u2192 ChA \u2192 ChA\n \u229a-fromto : \u2200 (a1 a2 a3 : A) (da1 da2 : ChA) \u2192\n ch da1 from a1 to a2 \u2192 ch da2 from a2 to a3 \u2192 ch da1 \u229a[ a1 ] da2 from a1 to a3\n\n open IsChangeStructure isChangeStructure public\n \u229a-correct : \u2200 (a1 a2 a3 : A) (da1 da2 : ChA) \u2192\n ch da1 from a1 to a2 \u2192 ch da2 from a2 to a3 \u2192\n a1 \u2295 (da1 \u229a[ a1 ] da2) \u2261 a3\n \u229a-correct a1 a2 a3 da1 da2 daa1 daa2 = fromto\u2192\u2295 _ _ _ (\u229a-fromto _ _ _ da1 da2 daa1 daa2)\n\nIsChangeStructure\u2192IsCompChangeStructure : \u2200 {A ChA ch_from_to_} \u2192 IsChangeStructure A ChA ch_from_to_ \u2192 IsCompChangeStructure A ChA ch_from_to_\nIsChangeStructure\u2192IsCompChangeStructure {A} {ChA} {ch_from_to_} isCS = record\n { isChangeStructure = isCS\n ; _\u229a[_]_ = \u03bb da1 a da2 \u2192 a \u2295 da1 \u2295 da2 \u229d a\n ; \u229a-fromto = body\n }\n where\n _\u2295_ = IsChangeStructure._\u2295_ isCS\n _\u229d_ = IsChangeStructure._\u229d_ isCS\n fromto\u2192\u2295 = IsChangeStructure.fromto\u2192\u2295 isCS\n \u229d-fromto = IsChangeStructure.\u229d-fromto isCS\n infixl 6 _\u2295_ _\u229d_\n body : \u2200 (a1 a2 a3 : A) da1 da2 \u2192\n ch da1 from a1 to a2 \u2192\n ch da2 from a2 to a3 \u2192 ch a1 \u2295 da1 \u2295 da2 \u229d a1 from a1 to a3\n body a1 a2 a3 da1 da2 daa1 daa2 rewrite fromto\u2192\u2295 _ _ _ daa1 | fromto\u2192\u2295 _ _ _ daa2 =\n \u229d-fromto a1 a3\n\n\nrecord ChangeStructure (A : Set) : Set\u2081 where\n field\n Ch : Set\n ch_from_to_ : (dv : Ch) \u2192 (v1 v2 : A) \u2192 Set\n isCompChangeStructure : IsCompChangeStructure A Ch ch_from_to_\n open IsCompChangeStructure isCompChangeStructure public\n\nopen ChangeStructure {{...}} public hiding (Ch)\nCh : \u2200 (A : Set) \u2192 {{CA : ChangeStructure A}} \u2192 Set\nCh A {{CA}} = ChangeStructure.Ch CA\n\n{-# DISPLAY IsChangeStructure._\u2295_ x = _\u2295_ #-}\n{-# DISPLAY ChangeStructure._\u2295_ x = _\u2295_ #-}\n{-# DISPLAY IsChangeStructure._\u229d_ x = _\u229d_ #-}\n{-# DISPLAY ChangeStructure._\u229d_ x = _\u229d_ #-}\n{-# DISPLAY IsChangeStructure.nil x = nil #-}\n{-# DISPLAY ChangeStructure.nil x = nil #-}\n{-# DISPLAY IsCompChangeStructure._\u229a[_]_ x = _\u229a[_]_ #-}\n{-# DISPLAY ChangeStructure._\u229a[_]_ x = _\u229a[_]_ #-}\n{-# DISPLAY ChangeStructure.ch_from_to_ x = ch_from_to_ #-}\n\nmodule _ {A B : Set} {{CA : ChangeStructure A}} {{CB : ChangeStructure B}} where\n\n -- In this module, given change structures CA and CB for A and B, we define\n -- change structures for A \u2192 B, A \u00d7 B and A \u228e B.\n\n open import Postulate.Extensionality\n\n -- Functions\n instance\n funCS : ChangeStructure (A \u2192 B)\n\n infixl 6 _f\u2295_ _f\u229d_\n private\n fCh = A \u2192 Ch A \u2192 Ch B\n\n fCh_from_to_ : (df : fCh) \u2192 (f1 f2 : A \u2192 B) \u2192 Set\n fCh_from_to_ =\n \u03bb df f1 f2 \u2192 \u2200 da (a1 a2 : A) (daa : ch da from a1 to a2) \u2192\n ch df a1 da from f1 a1 to f2 a2\n\n _f\u2295_ : (A \u2192 B) \u2192 fCh \u2192 A \u2192 B\n _f\u2295_ = \u03bb f df a \u2192 f a \u2295 df a (nil a)\n\n _f\u229d_ : (g f : A \u2192 B) \u2192 fCh\n _f\u229d_ = \u03bb g f a da \u2192 g (a \u2295 da) \u229d f a\n\n f\u229d-fromto : \u2200 (f1 f2 : A \u2192 B) \u2192 fCh (f2 f\u229d f1) from f1 to f2\n f\u229d-fromto f1 f2 da a1 a2 daa\n rewrite sym (fromto\u2192\u2295 da a1 a2 daa) = \u229d-fromto (f1 a1) (f2 (a1 \u2295 da))\n\n _f\u229a[_]_ : fCh \u2192 (A \u2192 B) \u2192 fCh \u2192 fCh\n _f\u229a[_]_ df1 f df2 = \u03bb a da \u2192 (df1 a (nil a)) \u229a[ f a ] (df2 a da)\n\n _f2\u229a[_]_ : fCh \u2192 (A \u2192 B) \u2192 fCh \u2192 fCh\n _f2\u229a[_]_ df1 f df2 = \u03bb a da \u2192 df1 a da \u229a[ f a ] df2 (a \u2295 da) (nil (a \u2295 da))\n\n f\u229a-fromto : \u2200 (f1 f2 f3 : A \u2192 B) (df1 df2 : fCh) \u2192 fCh df1 from f1 to f2 \u2192 fCh df2 from f2 to f3 \u2192\n fCh df1 f\u229a[ f1 ] df2 from f1 to f3\n f\u229a-fromto f1 f2 f3 df1 df2 dff1 dff2 da a1 a2 daa =\n \u229a-fromto (f1 a1) (f2 a1) (f3 a2)\n (df1 a1 (nil a1))\n (df2 a1 da)\n (dff1 (nil a1) a1 a1 (nil-fromto a1))\n (dff2 da a1 a2 daa)\n\n f\u229a2-fromto : \u2200 (f1 f2 f3 : A \u2192 B) (df1 df2 : fCh) \u2192 fCh df1 from f1 to f2 \u2192 fCh df2 from f2 to f3 \u2192\n fCh df1 f2\u229a[ f1 ] df2 from f1 to f3\n f\u229a2-fromto f1 f2 f3 df1 df2 dff1 dff2 da a1 a2 daa rewrite fromto\u2192\u2295 da a1 a2 daa =\n \u229a-fromto (f1 a1) (f2 a2) (f3 a2)\n (df1 a1 da)\n (df2 a2 (nil a2))\n (dff1 da a1 a2 daa)\n (dff2 (nil a2) a2 a2 (nil-fromto a2))\n\n funCS = record\n { Ch = fCh\n ; ch_from_to_ =\n \u03bb df f1 f2 \u2192 \u2200 da (a1 a2 : A) (daa : ch da from a1 to a2) \u2192\n ch df a1 da from f1 a1 to f2 a2\n ; isCompChangeStructure = record\n { isChangeStructure = record\n { _\u2295_ = _f\u2295_\n ; fromto\u2192\u2295 = \u03bb df f1 f2 dff \u2192\n ext (\u03bb v \u2192\n fromto\u2192\u2295 (df v (nil v)) (f1 v) (f2 v) (dff (nil v) v v (nil-fromto v)))\n ; _\u229d_ = _f\u229d_\n ; \u229d-fromto = f\u229d-fromto\n }\n ; _\u229a[_]_ = _f\u229a[_]_\n ; \u229a-fromto = f\u229a-fromto\n }\n }\n\n -- Products\n private\n pCh = Ch A \u00d7 Ch B\n _p\u2295_ : A \u00d7 B \u2192 Ch A \u00d7 Ch B \u2192 A \u00d7 B\n _p\u2295_ (a , b) (da , db) = a \u2295 da , b \u2295 db\n _p\u229d_ : A \u00d7 B \u2192 A \u00d7 B \u2192 pCh\n _p\u229d_ (a2 , b2) (a1 , b1) = a2 \u229d a1 , b2 \u229d b1\n pch_from_to_ : pCh \u2192 A \u00d7 B \u2192 A \u00d7 B \u2192 Set\n pch (da , db) from (a1 , b1) to (a2 , b2) = ch da from a1 to a2 \u00d7 ch db from b1 to b2\n _p\u229a[_]_ : pCh \u2192 A \u00d7 B \u2192 pCh \u2192 pCh\n (da1 , db1) p\u229a[ a , b ] (da2 , db2) = da1 \u229a[ a ] da2 , db1 \u229a[ b ] db2\n pfromto\u2192\u2295 : \u2200 dp p1 p2 \u2192\n pch dp from p1 to p2 \u2192 p1 p\u2295 dp \u2261 p2\n pfromto\u2192\u2295 (da , db) (a1 , b1) (a2 , b2) (daa , dbb) =\n cong\u2082 _,_ (fromto\u2192\u2295 _ _ _ daa) (fromto\u2192\u2295 _ _ _ dbb)\n p\u229d-fromto : \u2200 (p1 p2 : A \u00d7 B) \u2192 pch p2 p\u229d p1 from p1 to p2\n p\u229d-fromto (a1 , b1) (a2 , b2) = \u229d-fromto a1 a2 , \u229d-fromto b1 b2\n p\u229a-fromto : \u2200 p1 p2 p3 dp1 dp2 \u2192\n pch dp1 from p1 to p2 \u2192 (pch dp2 from p2 to p3) \u2192 pch dp1 p\u229a[ p1 ] dp2 from p1 to p3\n p\u229a-fromto (a1 , b1) (a2 , b2) (a3 , b3) (da1 , db1) (da2 , db2)\n (daa1 , dbb1) (daa2 , dbb2) =\n \u229a-fromto a1 a2 a3 da1 da2 daa1 daa2 , \u229a-fromto b1 b2 b3 db1 db2 dbb1 dbb2\n\n instance\n pairCS : ChangeStructure (A \u00d7 B)\n pairCS = record\n { Ch = pCh\n ; ch_from_to_ = pch_from_to_\n ; isCompChangeStructure = record\n { isChangeStructure = record\n { _\u2295_ = _p\u2295_\n ; fromto\u2192\u2295 = pfromto\u2192\u2295\n ; _\u229d_ = _p\u229d_\n ; \u229d-fromto = p\u229d-fromto\n }\n ; _\u229a[_]_ = _p\u229a[_]_\n ; \u229a-fromto = p\u229a-fromto\n }\n }\n\n -- Sums\n private\n SumChange = (Ch A \u228e Ch B) \u228e (A \u228e B)\n\n data SumChange2 : Set where\n ch\u2081 : (da : Ch A) \u2192 SumChange2\n ch\u2082 : (db : Ch B) \u2192 SumChange2\n rp : (s : A \u228e B) \u2192 SumChange2\n\n convert : SumChange \u2192 SumChange2\n convert (inj\u2081 (inj\u2081 da)) = ch\u2081 da\n convert (inj\u2081 (inj\u2082 db)) = ch\u2082 db\n convert (inj\u2082 s) = rp s\n convert\u2081 : SumChange2 \u2192 SumChange\n convert\u2081 (ch\u2081 da) = inj\u2081 (inj\u2081 da)\n convert\u2081 (ch\u2082 db) = inj\u2081 (inj\u2082 db)\n convert\u2081 (rp s) = inj\u2082 s\n\n inv1 : \u2200 ds \u2192 convert\u2081 (convert ds) \u2261 ds\n inv1 (inj\u2081 (inj\u2081 da)) = refl\n inv1 (inj\u2081 (inj\u2082 db)) = refl\n inv1 (inj\u2082 s) = refl\n inv2 : \u2200 ds \u2192 convert (convert\u2081 ds) \u2261 ds\n inv2 (ch\u2081 da) = refl\n inv2 (ch\u2082 db) = refl\n inv2 (rp s) = refl\n\n private\n _s\u22952_ : A \u228e B \u2192 SumChange2 \u2192 A \u228e B\n _s\u22952_ (inj\u2081 a) (ch\u2081 da) = inj\u2081 (a \u2295 da)\n _s\u22952_ (inj\u2082 b) (ch\u2082 db) = inj\u2082 (b \u2295 db)\n _s\u22952_ (inj\u2082 b) (ch\u2081 da) = inj\u2082 b -- invalid\n _s\u22952_ (inj\u2081 a) (ch\u2082 db) = inj\u2081 a -- invalid\n _s\u22952_ s (rp s\u2081) = s\u2081\n\n _s\u2295_ : A \u228e B \u2192 SumChange \u2192 A \u228e B\n s s\u2295 ds = s s\u22952 (convert ds)\n\n _s\u229d2_ : A \u228e B \u2192 A \u228e B \u2192 SumChange2\n _s\u229d2_ (inj\u2081 x2) (inj\u2081 x1) = ch\u2081 (x2 \u229d x1)\n _s\u229d2_ (inj\u2082 y2) (inj\u2082 y1) = ch\u2082 (y2 \u229d y1)\n _s\u229d2_ s2 s1 = rp s2\n\n _s\u229d_ : A \u228e B \u2192 A \u228e B \u2192 SumChange\n s2 s\u229d s1 = convert\u2081 (s2 s\u229d2 s1)\n\n data sch_from_to_ : SumChange \u2192 A \u228e B \u2192 A \u228e B \u2192 Set where\n -- sft = Sum From To\n sft\u2081 : \u2200 {da a1 a2} (daa : ch da from a1 to a2) \u2192 sch (convert\u2081 (ch\u2081 da)) from (inj\u2081 a1) to (inj\u2081 a2)\n sft\u2082 : \u2200 {db b1 b2} (dbb : ch db from b1 to b2) \u2192 sch (convert\u2081 (ch\u2082 db)) from (inj\u2082 b1) to (inj\u2082 b2)\n sftrp\u2081 : \u2200 a1 b2 \u2192 sch (convert\u2081 (rp (inj\u2082 b2))) from (inj\u2081 a1) to (inj\u2082 b2)\n sftrp\u2082 : \u2200 b1 a2 \u2192 sch (convert\u2081 (rp (inj\u2081 a2))) from (inj\u2082 b1) to (inj\u2081 a2)\n\n sfromto\u2192\u22952 : (ds : SumChange2) (s1 s2 : A \u228e B) \u2192\n sch convert\u2081 ds from s1 to s2 \u2192 s1 s\u22952 ds \u2261 s2\n sfromto\u2192\u22952 (ch\u2081 da) (inj\u2081 a1) (inj\u2081 a2) (sft\u2081 daa) = cong inj\u2081 (fromto\u2192\u2295 _ _ _ daa)\n sfromto\u2192\u22952 (ch\u2082 db) (inj\u2082 b1) (inj\u2082 b2) (sft\u2082 dbb) = cong inj\u2082 (fromto\u2192\u2295 _ _ _ dbb)\n sfromto\u2192\u22952 (rp .(inj\u2082 y)) (inj\u2081 x) (inj\u2082 y) (sftrp\u2081 .x .y) = refl\n sfromto\u2192\u22952 (rp .(inj\u2081 x)) (inj\u2082 y) (inj\u2081 x) (sftrp\u2082 .y .x) = refl\n\n sfromto\u2192\u2295 : (ds : SumChange) (s1 s2 : A \u228e B) \u2192\n sch ds from s1 to s2 \u2192 s1 s\u2295 ds \u2261 s2\n sfromto\u2192\u2295 ds s1 s2 dss =\n sfromto\u2192\u22952 (convert ds) s1 s2\n (subst (sch_from s1 to s2) (sym (inv1 ds))\n dss)\n s\u229d-fromto : (s1 s2 : A \u228e B) \u2192 sch s2 s\u229d s1 from s1 to s2\n s\u229d-fromto (inj\u2081 a1) (inj\u2081 a2) = sft\u2081 (\u229d-fromto a1 a2)\n s\u229d-fromto (inj\u2081 a1) (inj\u2082 b2) = sftrp\u2081 a1 b2\n s\u229d-fromto (inj\u2082 b1) (inj\u2081 a2) = sftrp\u2082 b1 a2\n s\u229d-fromto (inj\u2082 b1) (inj\u2082 b2) = sft\u2082 (\u229d-fromto b1 b2)\n instance\n sumCS : ChangeStructure (A \u228e B)\n sumCS = record\n { Ch = SumChange\n ; ch_from_to_ = sch_from_to_\n ; isCompChangeStructure = IsChangeStructure\u2192IsCompChangeStructure (record\n { _\u2295_ = _s\u2295_\n ; fromto\u2192\u2295 = sfromto\u2192\u2295\n ; _\u229d_ = _s\u229d_\n ; \u229d-fromto = s\u229d-fromto\n })\n }\n\ninstance\n unitCS : ChangeStructure \u22a4\n\n unitCS = record\n { Ch = \u22a4\n ; ch_from_to_ = \u03bb dv v1 v2 \u2192 \u22a4\n ; isCompChangeStructure = IsChangeStructure\u2192IsCompChangeStructure (record\n { _\u2295_ = \u03bb _ _ \u2192 tt\n ; fromto\u2192\u2295 = \u03bb { _ _ tt _ \u2192 refl }\n ; _\u229d_ = \u03bb _ _ \u2192 tt\n ; \u229d-fromto = \u03bb a b \u2192 tt\n })\n }\n","old_contents":"module Thesis.Changes where\n\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Unit\nopen import Relation.Binary.PropositionalEquality\n\nrecord IsChangeStructure (A : Set) (ChA : Set) (ch_from_to_ : (dv : ChA) \u2192 (v1 v2 : A) \u2192 Set) : Set\u2081 where\n infixl 6 _\u2295_ _\u229d_\n field\n _\u2295_ : A \u2192 ChA \u2192 A\n fromto\u2192\u2295 : \u2200 dv v1 v2 \u2192\n ch dv from v1 to v2 \u2192\n v1 \u2295 dv \u2261 v2\n _\u229d_ : A \u2192 A \u2192 ChA\n \u229d-fromto : \u2200 (a b : A) \u2192 ch (b \u229d a) from a to b\n\n update-diff : (b a : A) \u2192 a \u2295 (b \u229d a) \u2261 b\n update-diff b a = fromto\u2192\u2295 (b \u229d a) a b (\u229d-fromto a b)\n nil : A \u2192 ChA\n nil a = a \u229d a\n nil-fromto : (a : A) \u2192 ch (nil a) from a to a\n nil-fromto a = \u229d-fromto a a\n update-nil : (a : A) \u2192 a \u2295 nil a \u2261 a\n update-nil a = update-diff a a\n\nrecord IsCompChangeStructure (A : Set) (ChA : Set) (ch_from_to_ : (dv : ChA) \u2192 (v1 v2 : A) \u2192 Set) : Set\u2081 where\n field\n isChangeStructure : IsChangeStructure A ChA ch_from_to_\n _\u229a[_]_ : ChA \u2192 A \u2192 ChA \u2192 ChA\n \u229a-fromto : \u2200 (a1 a2 a3 : A) (da1 da2 : ChA) \u2192\n ch da1 from a1 to a2 \u2192 ch da2 from a2 to a3 \u2192 ch da1 \u229a[ a1 ] da2 from a1 to a3\n\n open IsChangeStructure isChangeStructure public\n \u229a-correct : \u2200 (a1 a2 a3 : A) (da1 da2 : ChA) \u2192\n ch da1 from a1 to a2 \u2192 ch da2 from a2 to a3 \u2192\n a1 \u2295 (da1 \u229a[ a1 ] da2) \u2261 a3\n \u229a-correct a1 a2 a3 da1 da2 daa1 daa2 = fromto\u2192\u2295 _ _ _ (\u229a-fromto _ _ _ da1 da2 daa1 daa2)\n\nIsChangeStructure\u2192IsCompChangeStructure : \u2200 {A ChA ch_from_to_} \u2192 IsChangeStructure A ChA ch_from_to_ \u2192 IsCompChangeStructure A ChA ch_from_to_\nIsChangeStructure\u2192IsCompChangeStructure {A} {ChA} {ch_from_to_} isCS = record\n { isChangeStructure = isCS\n ; _\u229a[_]_ = \u03bb da1 a da2 \u2192 a \u2295 da1 \u2295 da2 \u229d a\n ; \u229a-fromto = body\n }\n where\n _\u2295_ = IsChangeStructure._\u2295_ isCS\n _\u229d_ = IsChangeStructure._\u229d_ isCS\n fromto\u2192\u2295 = IsChangeStructure.fromto\u2192\u2295 isCS\n \u229d-fromto = IsChangeStructure.\u229d-fromto isCS\n infixl 6 _\u2295_ _\u229d_\n body : \u2200 (a1 a2 a3 : A) da1 da2 \u2192\n ch da1 from a1 to a2 \u2192\n ch da2 from a2 to a3 \u2192 ch a1 \u2295 da1 \u2295 da2 \u229d a1 from a1 to a3\n body a1 a2 a3 da1 da2 daa1 daa2 rewrite fromto\u2192\u2295 _ _ _ daa1 | fromto\u2192\u2295 _ _ _ daa2 =\n \u229d-fromto a1 a3\n\n\nrecord ChangeStructure (A : Set) : Set\u2081 where\n field\n Ch : Set\n ch_from_to_ : (dv : Ch) \u2192 (v1 v2 : A) \u2192 Set\n isCompChangeStructure : IsCompChangeStructure A Ch ch_from_to_\n open IsCompChangeStructure isCompChangeStructure public\n\nopen ChangeStructure {{...}} public hiding (Ch)\nCh : \u2200 (A : Set) \u2192 {{CA : ChangeStructure A}} \u2192 Set\nCh A {{CA}} = ChangeStructure.Ch CA\n\n{-# DISPLAY IsChangeStructure._\u2295_ x = _\u2295_ #-}\n{-# DISPLAY ChangeStructure._\u2295_ x = _\u2295_ #-}\n{-# DISPLAY IsChangeStructure._\u229d_ x = _\u229d_ #-}\n{-# DISPLAY ChangeStructure._\u229d_ x = _\u229d_ #-}\n{-# DISPLAY IsChangeStructure.nil x = nil #-}\n{-# DISPLAY ChangeStructure.nil x = nil #-}\n{-# DISPLAY IsCompChangeStructure._\u229a[_]_ x = _\u229a[_]_ #-}\n{-# DISPLAY ChangeStructure._\u229a[_]_ x = _\u229a[_]_ #-}\n{-# DISPLAY ChangeStructure.ch_from_to_ x = ch_from_to_ #-}\n\nmodule _ {A B : Set} {{CA : ChangeStructure A}} {{CB : ChangeStructure B}} where\n\n -- In this module, given change structures CA and CB for A and B, we define\n -- change structures for A \u2192 B, A \u00d7 B and A \u228e B.\n\n open import Postulate.Extensionality\n\n -- Functions\n instance\n funCS : ChangeStructure (A \u2192 B)\n\n infixl 6 _f\u2295_ _f\u229d_\n private\n fCh = A \u2192 Ch A \u2192 Ch B\n\n fCh_from_to_ : (df : fCh) \u2192 (f1 f2 : A \u2192 B) \u2192 Set\n fCh_from_to_ =\n \u03bb df f1 f2 \u2192 \u2200 da (a1 a2 : A) (daa : ch da from a1 to a2) \u2192\n ch df a1 da from f1 a1 to f2 a2\n\n _f\u2295_ : (A \u2192 B) \u2192 fCh \u2192 A \u2192 B\n _f\u2295_ = \u03bb f df a \u2192 f a \u2295 df a (nil a)\n\n _f\u229d_ : (g f : A \u2192 B) \u2192 fCh\n _f\u229d_ = \u03bb g f a da \u2192 g (a \u2295 da) \u229d f a\n\n f\u229d-fromto : \u2200 (f1 f2 : A \u2192 B) \u2192 fCh (f2 f\u229d f1) from f1 to f2\n f\u229d-fromto f1 f2 da a1 a2 daa\n rewrite sym (fromto\u2192\u2295 da a1 a2 daa) = \u229d-fromto (f1 a1) (f2 (a1 \u2295 da))\n\n _f\u229a[_]_ : fCh \u2192 (A \u2192 B) \u2192 fCh \u2192 fCh\n _f\u229a[_]_ df1 f df2 = \u03bb a da \u2192 (df1 a (nil a)) \u229a[ f a ] (df2 a da)\n\n _f2\u229a[_]_ : fCh \u2192 (A \u2192 B) \u2192 fCh \u2192 fCh\n _f2\u229a[_]_ df1 f df2 = \u03bb a da \u2192 df1 a da \u229a[ f a ] df2 (a \u2295 da) (nil (a \u2295 da))\n\n f\u229a-fromto : \u2200 (f1 f2 f3 : A \u2192 B) (df1 df2 : fCh) \u2192 fCh df1 from f1 to f2 \u2192 fCh df2 from f2 to f3 \u2192\n fCh df1 f\u229a[ f1 ] df2 from f1 to f3\n f\u229a-fromto f1 f2 f3 df1 df2 dff1 dff2 da a1 a2 daa =\n \u229a-fromto (f1 a1) (f2 a1) (f3 a2)\n (df1 a1 (nil a1))\n (df2 a1 da)\n (dff1 (nil a1) a1 a1 (nil-fromto a1))\n (dff2 da a1 a2 daa)\n\n f\u229a2-fromto : \u2200 (f1 f2 f3 : A \u2192 B) (df1 df2 : fCh) \u2192 fCh df1 from f1 to f2 \u2192 fCh df2 from f2 to f3 \u2192\n fCh df1 f2\u229a[ f1 ] df2 from f1 to f3\n f\u229a2-fromto f1 f2 f3 df1 df2 dff1 dff2 da a1 a2 daa rewrite fromto\u2192\u2295 da a1 a2 daa =\n \u229a-fromto (f1 a1) (f2 a2) (f3 a2)\n (df1 a1 da)\n (df2 a2 (nil a2))\n (dff1 da a1 a2 daa)\n (dff2 (nil a2) a2 a2 (nil-fromto a2))\n\n funCS = record\n { Ch = fCh\n ; ch_from_to_ =\n \u03bb df f1 f2 \u2192 \u2200 da (a1 a2 : A) (daa : ch da from a1 to a2) \u2192\n ch df a1 da from f1 a1 to f2 a2\n ; isCompChangeStructure = record\n { isChangeStructure = record\n { _\u2295_ = _f\u2295_\n ; fromto\u2192\u2295 = \u03bb df f1 f2 dff \u2192\n ext (\u03bb v \u2192\n fromto\u2192\u2295 (df v (nil v)) (f1 v) (f2 v) (dff (nil v) v v (nil-fromto v)))\n ; _\u229d_ = _f\u229d_\n ; \u229d-fromto = f\u229d-fromto\n }\n ; _\u229a[_]_ = _f\u229a[_]_\n ; \u229a-fromto = f\u229a-fromto\n }\n }\n\n -- Products\n private\n pCh = Ch A \u00d7 Ch B\n _p\u2295_ : A \u00d7 B \u2192 Ch A \u00d7 Ch B \u2192 A \u00d7 B\n _p\u2295_ (a , b) (da , db) = a \u2295 da , b \u2295 db\n _p\u229d_ : A \u00d7 B \u2192 A \u00d7 B \u2192 pCh\n _p\u229d_ (a2 , b2) (a1 , b1) = a2 \u229d a1 , b2 \u229d b1\n pch_from_to_ : pCh \u2192 A \u00d7 B \u2192 A \u00d7 B \u2192 Set\n pch (da , db) from (a1 , b1) to (a2 , b2) = ch da from a1 to a2 \u00d7 ch db from b1 to b2\n _p\u229a[_]_ : pCh \u2192 A \u00d7 B \u2192 pCh \u2192 pCh\n (da1 , db1) p\u229a[ a , b ] (da2 , db2) = da1 \u229a[ a ] da2 , db1 \u229a[ b ] db2\n pfromto\u2192\u2295 : \u2200 dp p1 p2 \u2192\n pch dp from p1 to p2 \u2192 p1 p\u2295 dp \u2261 p2\n pfromto\u2192\u2295 (da , db) (a1 , b1) (a2 , b2) (daa , dbb) =\n cong\u2082 _,_ (fromto\u2192\u2295 _ _ _ daa) (fromto\u2192\u2295 _ _ _ dbb)\n p\u229d-fromto : \u2200 (p1 p2 : A \u00d7 B) \u2192 pch p2 p\u229d p1 from p1 to p2\n p\u229d-fromto (a1 , b1) (a2 , b2) = \u229d-fromto a1 a2 , \u229d-fromto b1 b2\n p\u229a-fromto : \u2200 p1 p2 p3 dp1 dp2 \u2192\n pch dp1 from p1 to p2 \u2192 (pch dp2 from p2 to p3) \u2192 pch dp1 p\u229a[ p1 ] dp2 from p1 to p3\n p\u229a-fromto (a1 , b1) (a2 , b2) (a3 , b3) (da1 , db1) (da2 , db2)\n (daa1 , dbb1) (daa2 , dbb2) =\n \u229a-fromto a1 a2 a3 da1 da2 daa1 daa2 , \u229a-fromto b1 b2 b3 db1 db2 dbb1 dbb2\n\n instance\n pairCS : ChangeStructure (A \u00d7 B)\n pairCS = record\n { Ch = pCh\n ; ch_from_to_ = pch_from_to_\n ; isCompChangeStructure = record\n { isChangeStructure = record\n { _\u2295_ = _p\u2295_\n ; fromto\u2192\u2295 = pfromto\u2192\u2295\n ; _\u229d_ = _p\u229d_\n ; \u229d-fromto = p\u229d-fromto\n }\n ; _\u229a[_]_ = _p\u229a[_]_\n ; \u229a-fromto = p\u229a-fromto\n }\n }\n\n -- Sums\n private\n SumChange = (Ch A \u228e Ch B) \u228e (A \u228e B)\n\n data SumChange2 : Set where\n ch\u2081 : (da : Ch A) \u2192 SumChange2\n ch\u2082 : (db : Ch B) \u2192 SumChange2\n rp : (s : A \u228e B) \u2192 SumChange2\n\n convert : SumChange \u2192 SumChange2\n convert (inj\u2081 (inj\u2081 da)) = ch\u2081 da\n convert (inj\u2081 (inj\u2082 db)) = ch\u2082 db\n convert (inj\u2082 s) = rp s\n convert\u2081 : SumChange2 \u2192 SumChange\n convert\u2081 (ch\u2081 da) = inj\u2081 (inj\u2081 da)\n convert\u2081 (ch\u2082 db) = inj\u2081 (inj\u2082 db)\n convert\u2081 (rp s) = inj\u2082 s\n\n inv1 : \u2200 ds \u2192 convert\u2081 (convert ds) \u2261 ds\n inv1 (inj\u2081 (inj\u2081 da)) = refl\n inv1 (inj\u2081 (inj\u2082 db)) = refl\n inv1 (inj\u2082 s) = refl\n inv2 : \u2200 ds \u2192 convert (convert\u2081 ds) \u2261 ds\n inv2 (ch\u2081 da) = refl\n inv2 (ch\u2082 db) = refl\n inv2 (rp s) = refl\n\n private\n _s\u22952_ : A \u228e B \u2192 SumChange2 \u2192 A \u228e B\n _s\u22952_ (inj\u2081 a) (ch\u2081 da) = inj\u2081 (a \u2295 da)\n _s\u22952_ (inj\u2082 b) (ch\u2082 db) = inj\u2082 (b \u2295 db)\n _s\u22952_ (inj\u2082 b) (ch\u2081 da) = inj\u2082 b -- invalid\n _s\u22952_ (inj\u2081 a) (ch\u2082 db) = inj\u2081 a -- invalid\n _s\u22952_ s (rp s\u2081) = s\u2081\n\n _s\u2295_ : A \u228e B \u2192 SumChange \u2192 A \u228e B\n s s\u2295 ds = s s\u22952 (convert ds)\n\n _s\u229d2_ : A \u228e B \u2192 A \u228e B \u2192 SumChange2\n _s\u229d2_ (inj\u2081 x2) (inj\u2081 x1) = ch\u2081 (x2 \u229d x1)\n _s\u229d2_ (inj\u2082 y2) (inj\u2082 y1) = ch\u2082 (y2 \u229d y1)\n _s\u229d2_ s2 s1 = rp s2\n\n _s\u229d_ : A \u228e B \u2192 A \u228e B \u2192 SumChange\n s2 s\u229d s1 = convert\u2081 (s2 s\u229d2 s1)\n\n data sch_from_to_ : SumChange \u2192 A \u228e B \u2192 A \u228e B \u2192 Set where\n -- sft = Sum From To\n sft\u2081 : \u2200 {da a1 a2} (daa : ch da from a1 to a2) \u2192 sch (convert\u2081 (ch\u2081 da)) from (inj\u2081 a1) to (inj\u2081 a2)\n sft\u2082 : \u2200 {db b1 b2} (dbb : ch db from b1 to b2) \u2192 sch (convert\u2081 (ch\u2082 db)) from (inj\u2082 b1) to (inj\u2082 b2)\n sftrp\u2081 : \u2200 a1 b2 \u2192 sch (convert\u2081 (rp (inj\u2082 b2))) from (inj\u2081 a1) to (inj\u2082 b2)\n sftrp\u2082 : \u2200 b1 a2 \u2192 sch (convert\u2081 (rp (inj\u2081 a2))) from (inj\u2082 b1) to (inj\u2081 a2)\n\n sfromto\u2192\u22952 : (ds : SumChange2) (s1 s2 : A \u228e B) \u2192\n sch convert\u2081 ds from s1 to s2 \u2192 s1 s\u22952 ds \u2261 s2\n sfromto\u2192\u22952 (ch\u2081 da) (inj\u2081 a1) (inj\u2081 a2) (sft\u2081 daa) = cong inj\u2081 (fromto\u2192\u2295 _ _ _ daa)\n sfromto\u2192\u22952 (ch\u2082 db) (inj\u2082 b1) (inj\u2082 b2) (sft\u2082 dbb) = cong inj\u2082 (fromto\u2192\u2295 _ _ _ dbb)\n sfromto\u2192\u22952 (rp .(inj\u2082 y)) (inj\u2081 x) (inj\u2082 y) (sftrp\u2081 .x .y) = refl\n sfromto\u2192\u22952 (rp .(inj\u2081 x)) (inj\u2082 y) (inj\u2081 x) (sftrp\u2082 .y .x) = refl\n\n sfromto\u2192\u2295 : (ds : SumChange) (s1 s2 : A \u228e B) \u2192\n sch ds from s1 to s2 \u2192 s1 s\u2295 ds \u2261 s2\n sfromto\u2192\u2295 ds s1 s2 dss =\n sfromto\u2192\u22952 (convert ds) s1 s2\n (subst (sch_from s1 to s2) (sym (inv1 ds))\n dss)\n s\u229d-fromto : (s1 s2 : A \u228e B) \u2192 sch s2 s\u229d s1 from s1 to s2\n s\u229d-fromto (inj\u2081 a1) (inj\u2081 a2) = sft\u2081 (\u229d-fromto a1 a2)\n s\u229d-fromto (inj\u2081 a1) (inj\u2082 b2) = sftrp\u2081 a1 b2\n s\u229d-fromto (inj\u2082 b1) (inj\u2081 a2) = sftrp\u2082 b1 a2\n s\u229d-fromto (inj\u2082 b1) (inj\u2082 b2) = sft\u2082 (\u229d-fromto b1 b2)\n instance\n sumCS : ChangeStructure (A \u228e B)\n sumCS = record\n { Ch = SumChange\n ; ch_from_to_ = sch_from_to_\n ; isCompChangeStructure = IsChangeStructure\u2192IsCompChangeStructure (record\n { _\u2295_ = _s\u2295_\n ; fromto\u2192\u2295 = sfromto\u2192\u2295\n ; _\u229d_ = _s\u229d_\n ; \u229d-fromto = s\u229d-fromto\n })\n }\n\ninstance\n unitCS : ChangeStructure \u22a4\n\n unitCS = record\n { Ch = \u22a4\n ; ch_from_to_ = \u03bb dv v1 v2 \u2192 \u22a4\n ; isCompChangeStructure = IsChangeStructure\u2192IsCompChangeStructure (record\n { _\u2295_ = \u03bb _ _ \u2192 tt\n ; fromto\u2192\u2295 = \u03bb { _ _ tt _ \u2192 refl }\n ; _\u229d_ = \u03bb _ _ \u2192 tt\n ; \u229d-fromto = \u03bb a b \u2192 tt\n })\n }\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"48d1be2bb4bb61634b15fbd0cff064c02be1f4f9","subject":"Comment on handling of contexts","message":"Comment on handling of contexts\n\nOld-commit-hash: 0bfc0dfdc024921016f8df0d8252aa20f5f0f8d6\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Syntax\/Context.agda","new_file":"Base\/Syntax\/Context.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Variables and contexts\n--\n-- This module defines the syntax of contexts, prefixes of\n-- contexts and variables and properties of these notions.\n--\n-- This module is parametric in the syntax of types, so it\n-- can be reused for different calculi.\n--\n------------------------------------------------------------------------\n\nmodule Base.Syntax.Context\n (Type : Set)\n where\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\n\n-- TYPING CONTEXTS\n\n-- Syntax\n\nimport Data.List as List\nopen List public\n using ()\n renaming\n ( [] to \u2205 ; _\u2237_ to _\u2022_\n ; map to mapContext\n )\n\nContext : Set\nContext = List.List Type\n\n-- VARIABLES\n\n-- Syntax\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c3 \u2022 \u0393) \u03c4\n\n-- WEAKENING\n\n-- CONTEXT PREFIXES\n--\n-- Useful for making lemmas strong enough to prove by induction.\n--\n-- Consider using the Subcontexts module instead.\n\nmodule Prefixes where\n\n-- Prefix of a context\n\n data Prefix : Context \u2192 Set where\n \u2205 : \u2200 {\u0393} \u2192 Prefix \u0393\n _\u2022_ : \u2200 {\u0393} \u2192 (\u03c4 : Type) \u2192 Prefix \u0393 \u2192 Prefix (\u03c4 \u2022 \u0393)\n\n-- take only the prefix of a context\n\n take : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\n take \u0393 \u2205 = \u2205\n take (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) = \u03c4 \u2022 take \u0393 \u0393\u2032\n\n-- drop the prefix of a context\n\n drop : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\n drop \u0393 \u2205 = \u0393\n drop (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) = drop \u0393 \u0393\u2032\n\n-- Extend a context to a super context\n\n infixr 10 _\u22ce_\n\n _\u22ce_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Context\n \u2205 \u22ce \u0393\u2082 = \u0393\u2082\n (\u03c4 \u2022 \u0393\u2081) \u22ce \u0393\u2082 = \u03c4 \u2022 (\u0393\u2081 \u22ce \u0393\u2082)\n\n take-drop : \u2200 \u0393 \u0393\u2032 \u2192 take \u0393 \u0393\u2032 \u22ce drop \u0393 \u0393\u2032 \u2261 \u0393\n take-drop \u2205 \u2205 = refl\n take-drop (\u03c4 \u2022 \u0393) \u2205 = refl\n take-drop (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) rewrite take-drop \u0393 \u0393\u2032 = refl\n\n or-unit : \u2200 \u0393 \u2192 \u0393 \u22ce \u2205 \u2261 \u0393\n or-unit \u2205 = refl\n or-unit (\u03c4 \u2022 \u0393) rewrite or-unit \u0393 = refl\n\n move-prefix : \u2200 \u0393 \u03c4 \u0393\u2032 \u2192\n \u0393 \u22ce (\u03c4 \u2022 \u0393\u2032) \u2261 (\u0393 \u22ce (\u03c4 \u2022 \u2205)) \u22ce \u0393\u2032\n move-prefix \u2205 \u03c4 \u0393\u2032 = refl\n move-prefix (\u03c4 \u2022 \u0393) \u03c4\u2081 \u2205 = sym (or-unit (\u03c4 \u2022 \u0393 \u22ce (\u03c4\u2081 \u2022 \u2205)))\n move-prefix (\u03c4 \u2022 \u0393) \u03c4\u2081 (\u03c4\u2082 \u2022 \u0393\u2032) rewrite move-prefix \u0393 \u03c4\u2081 (\u03c4\u2082 \u2022 \u0393\u2032) = refl\n\n-- Lift a variable to a super context\n\n lift : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\n lift {\u2205} {\u2205} x = x\n lift {\u2205} {\u03c4 \u2022 \u0393\u2082} x = that (lift {\u2205} {\u0393\u2082} x)\n lift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} this = this\n lift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} (that x) = that (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- SUBCONTEXTS\n--\n-- Useful as a reified weakening operation,\n-- and for making theorems strong enough to prove by induction.\n--\n-- The contents of this module are currently exported at the end\n-- of this file.\n\n-- This handling of contexts is recommended by [this\n-- email](https:\/\/lists.chalmers.se\/pipermail\/agda\/2011\/003423.html) and\n-- attributed to Conor McBride.\n--\n-- The associated thread discusses a few alternatives solutions, including one\n-- where beta-reduction can handle associativity of ++.\n\nmodule Subcontexts where\n infix 4 _\u227c_\n\n data _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u03c4 : Type) \u2192\n \u0393\u2081 \u227c \u0393\u2082 \u2192\n \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u03c4 : Type) \u2192\n \u0393\u2081 \u227c \u0393\u2082 \u2192\n \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n -- Properties\n\n \u2205\u227c\u0393 : \u2200 {\u0393} \u2192 \u2205 \u227c \u0393\n \u2205\u227c\u0393 {\u2205} = \u2205\n \u2205\u227c\u0393 {\u03c4 \u2022 \u0393} = drop \u03c4 \u2022 \u2205\u227c\u0393\n\n \u227c-refl : Reflexive _\u227c_\n \u227c-refl {\u2205} = \u2205\n \u227c-refl {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u227c-refl\n\n \u227c-reflexive : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u0393\u2081 \u227c \u0393\u2082\n \u227c-reflexive refl = \u227c-refl\n\n \u227c-trans : Transitive _\u227c_\n \u227c-trans \u227c\u2081 \u2205 = \u227c\u2081\n \u227c-trans (keep .\u03c4 \u2022 \u227c\u2081) (keep \u03c4 \u2022 \u227c\u2082) = keep \u03c4 \u2022 \u227c-trans \u227c\u2081 \u227c\u2082\n \u227c-trans (drop .\u03c4 \u2022 \u227c\u2081) (keep \u03c4 \u2022 \u227c\u2082) = drop \u03c4 \u2022 \u227c-trans \u227c\u2081 \u227c\u2082\n \u227c-trans \u227c\u2081 (drop \u03c4 \u2022 \u227c\u2082) = drop \u03c4 \u2022 \u227c-trans \u227c\u2081 \u227c\u2082\n\n \u227c-isPreorder : IsPreorder _\u2261_ _\u227c_\n \u227c-isPreorder = record\n { isEquivalence = isEquivalence\n ; reflexive = \u227c-reflexive\n ; trans = \u227c-trans\n }\n\n \u227c-preorder : Preorder _ _ _\n \u227c-preorder = record\n { Carrier = Context\n ; _\u2248_ = _\u2261_\n ; _\u223c_ = _\u227c_\n ; isPreorder = \u227c-isPreorder\n }\n\n module \u227c-Reasoning where\n open import Relation.Binary.PreorderReasoning \u227c-preorder public\n renaming\n ( _\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_\n ; _\u223c\u27e8_\u27e9_ to _\u227c\u27e8_\u27e9_\n ; _\u2248\u27e8\u27e9_ to _\u2261\u27e8\u27e9_\n )\n\n -- Lift a variable to a super context\n\n weaken-var : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\n weaken-var (keep \u03c4 \u2022 \u227c\u2081) this = this\n weaken-var (keep \u03c4 \u2022 \u227c\u2081) (that x) = that (weaken-var \u227c\u2081 x)\n weaken-var (drop \u03c4 \u2022 \u227c\u2081) x = that (weaken-var \u227c\u2081 x)\n\n-- Currently, we export the subcontext relation as well as the\n-- definition of _\u22ce_.\n\nopen Subcontexts public\nopen Prefixes public using (_\u22ce_)\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Variables and contexts\n--\n-- This module defines the syntax of contexts, prefixes of\n-- contexts and variables and properties of these notions.\n--\n-- This module is parametric in the syntax of types, so it\n-- can be reused for different calculi.\n--\n------------------------------------------------------------------------\n\nmodule Base.Syntax.Context\n (Type : Set)\n where\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\n\n-- TYPING CONTEXTS\n\n-- Syntax\n\nimport Data.List as List\nopen List public\n using ()\n renaming\n ( [] to \u2205 ; _\u2237_ to _\u2022_\n ; map to mapContext\n )\n\nContext : Set\nContext = List.List Type\n\n-- VARIABLES\n\n-- Syntax\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c3 \u2022 \u0393) \u03c4\n\n-- WEAKENING\n\n-- CONTEXT PREFIXES\n--\n-- Useful for making lemmas strong enough to prove by induction.\n--\n-- Consider using the Subcontexts module instead.\n\nmodule Prefixes where\n\n-- Prefix of a context\n\n data Prefix : Context \u2192 Set where\n \u2205 : \u2200 {\u0393} \u2192 Prefix \u0393\n _\u2022_ : \u2200 {\u0393} \u2192 (\u03c4 : Type) \u2192 Prefix \u0393 \u2192 Prefix (\u03c4 \u2022 \u0393)\n\n-- take only the prefix of a context\n\n take : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\n take \u0393 \u2205 = \u2205\n take (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) = \u03c4 \u2022 take \u0393 \u0393\u2032\n\n-- drop the prefix of a context\n\n drop : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\n drop \u0393 \u2205 = \u0393\n drop (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) = drop \u0393 \u0393\u2032\n\n-- Extend a context to a super context\n\n infixr 10 _\u22ce_\n\n _\u22ce_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Context\n \u2205 \u22ce \u0393\u2082 = \u0393\u2082\n (\u03c4 \u2022 \u0393\u2081) \u22ce \u0393\u2082 = \u03c4 \u2022 (\u0393\u2081 \u22ce \u0393\u2082)\n\n take-drop : \u2200 \u0393 \u0393\u2032 \u2192 take \u0393 \u0393\u2032 \u22ce drop \u0393 \u0393\u2032 \u2261 \u0393\n take-drop \u2205 \u2205 = refl\n take-drop (\u03c4 \u2022 \u0393) \u2205 = refl\n take-drop (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) rewrite take-drop \u0393 \u0393\u2032 = refl\n\n or-unit : \u2200 \u0393 \u2192 \u0393 \u22ce \u2205 \u2261 \u0393\n or-unit \u2205 = refl\n or-unit (\u03c4 \u2022 \u0393) rewrite or-unit \u0393 = refl\n\n move-prefix : \u2200 \u0393 \u03c4 \u0393\u2032 \u2192\n \u0393 \u22ce (\u03c4 \u2022 \u0393\u2032) \u2261 (\u0393 \u22ce (\u03c4 \u2022 \u2205)) \u22ce \u0393\u2032\n move-prefix \u2205 \u03c4 \u0393\u2032 = refl\n move-prefix (\u03c4 \u2022 \u0393) \u03c4\u2081 \u2205 = sym (or-unit (\u03c4 \u2022 \u0393 \u22ce (\u03c4\u2081 \u2022 \u2205)))\n move-prefix (\u03c4 \u2022 \u0393) \u03c4\u2081 (\u03c4\u2082 \u2022 \u0393\u2032) rewrite move-prefix \u0393 \u03c4\u2081 (\u03c4\u2082 \u2022 \u0393\u2032) = refl\n\n-- Lift a variable to a super context\n\n lift : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\n lift {\u2205} {\u2205} x = x\n lift {\u2205} {\u03c4 \u2022 \u0393\u2082} x = that (lift {\u2205} {\u0393\u2082} x)\n lift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} this = this\n lift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} (that x) = that (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- SUBCONTEXTS\n--\n-- Useful as a reified weakening operation,\n-- and for making theorems strong enough to prove by induction.\n--\n-- The contents of this module are currently exported at the end\n-- of this file.\n\nmodule Subcontexts where\n infix 4 _\u227c_\n\n data _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u03c4 : Type) \u2192\n \u0393\u2081 \u227c \u0393\u2082 \u2192\n \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u03c4 : Type) \u2192\n \u0393\u2081 \u227c \u0393\u2082 \u2192\n \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n -- Properties\n\n \u2205\u227c\u0393 : \u2200 {\u0393} \u2192 \u2205 \u227c \u0393\n \u2205\u227c\u0393 {\u2205} = \u2205\n \u2205\u227c\u0393 {\u03c4 \u2022 \u0393} = drop \u03c4 \u2022 \u2205\u227c\u0393\n\n \u227c-refl : Reflexive _\u227c_\n \u227c-refl {\u2205} = \u2205\n \u227c-refl {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u227c-refl\n\n \u227c-reflexive : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u0393\u2081 \u227c \u0393\u2082\n \u227c-reflexive refl = \u227c-refl\n\n \u227c-trans : Transitive _\u227c_\n \u227c-trans \u227c\u2081 \u2205 = \u227c\u2081\n \u227c-trans (keep .\u03c4 \u2022 \u227c\u2081) (keep \u03c4 \u2022 \u227c\u2082) = keep \u03c4 \u2022 \u227c-trans \u227c\u2081 \u227c\u2082\n \u227c-trans (drop .\u03c4 \u2022 \u227c\u2081) (keep \u03c4 \u2022 \u227c\u2082) = drop \u03c4 \u2022 \u227c-trans \u227c\u2081 \u227c\u2082\n \u227c-trans \u227c\u2081 (drop \u03c4 \u2022 \u227c\u2082) = drop \u03c4 \u2022 \u227c-trans \u227c\u2081 \u227c\u2082\n\n \u227c-isPreorder : IsPreorder _\u2261_ _\u227c_\n \u227c-isPreorder = record\n { isEquivalence = isEquivalence\n ; reflexive = \u227c-reflexive\n ; trans = \u227c-trans\n }\n\n \u227c-preorder : Preorder _ _ _\n \u227c-preorder = record\n { Carrier = Context\n ; _\u2248_ = _\u2261_\n ; _\u223c_ = _\u227c_\n ; isPreorder = \u227c-isPreorder\n }\n\n module \u227c-Reasoning where\n open import Relation.Binary.PreorderReasoning \u227c-preorder public\n renaming\n ( _\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_\n ; _\u223c\u27e8_\u27e9_ to _\u227c\u27e8_\u27e9_\n ; _\u2248\u27e8\u27e9_ to _\u2261\u27e8\u27e9_\n )\n\n -- Lift a variable to a super context\n\n weaken-var : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\n weaken-var (keep \u03c4 \u2022 \u227c\u2081) this = this\n weaken-var (keep \u03c4 \u2022 \u227c\u2081) (that x) = that (weaken-var \u227c\u2081 x)\n weaken-var (drop \u03c4 \u2022 \u227c\u2081) x = that (weaken-var \u227c\u2081 x)\n\n-- Currently, we export the subcontext relation as well as the\n-- definition of _\u22ce_.\n\nopen Subcontexts public\nopen Prefixes public using (_\u22ce_)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"675a834b612e0975d9d476442347b95f20a88acc","subject":"Added missing files.","message":"Added missing files.\n\nIgnore-this: c9a956d8948883f8e4a748a6f4128d94\n\ndarcs-hash:20110211225550-3bd4e-789909a82466fe6d270219ddba2fa3b7f95424d0.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"README.agda","new_file":"README.agda","new_contents":"------------------------------------------------------------------------------\n-- FOT (First Order Theories)\n------------------------------------------------------------------------------\n\n-- Code accompanying the paper \"Combining Automatic and Interactive\n-- Proof in First Order Theories of Combinators\" by Ana Bove,\n-- Peter Dybjer, and Andr\u00e9s Sicard-Ram\u00edrez.\n\n------------------------------------------------------------------------------\n\nmodule README where\n\n------------------------------------------------------------------------------\n-- Description\n------------------------------------------------------------------------------\n\n-- Examples of the formalization of first order theories showing the\n-- combination of interactive proofs with automatics proofs carried\n-- out by first order automatic theorem provers (ATPs).\n\n------------------------------------------------------------------------------\n-- Prerequisites\n------------------------------------------------------------------------------\n\n-- You can download agda2atp tool (described in above paper) using darcs\n-- with the following command:\n\n-- $ darcs get http:\/\/patch-tag.com\/r\/asr\/agda2atp\n\n-- The agda2atp tool and the FOT code require a modified version of\n-- Agda. See agda2atp\/README.txt for the related instructions.\n\n------------------------------------------------------------------------------\n-- Use\n------------------------------------------------------------------------------\n\n-- Let's suppose you want to use the Metis ATP to prove the group\n-- theory properties stated in the module\n-- GroupTheory.PropertiesATP. First, you should type-check the module using\n-- Agda\n\n-- $ agda -isrc src\/GroupTheory\/PropertiesATP.agda\n\n-- Second, you call the agda2tool using the Metis ATP\n\n-- $ agda2atp -isrc --atp=metis src\/GroupTheory\/PropertiesATP.agda\n\n------------------------------------------------------------------------------\n-- First order theories\n------------------------------------------------------------------------------\n\n-- Conventions\n\n-- The following modules show the formalization of some first order\n-- theories. If the module's name ends in 'I' the module contains\n-- interactive proofs, if it ends in 'ATP' the module contains\n-- combined proofs, otherwise the module contains interactive proofs\n-- that are used by the combined proofs.\n\n------------------------------------------------------------------------------\n-- Distributive laws on a binary operation\n\n-- We prove the proposition 2 (task B) of [1]. Let _\u00b7_ be a\n-- left-associative binary operation, the task B consist in given the\n-- left and right distributive axioms\n\n-- \u2200 x y z \u2192 x \u2219 (y \u2219 z) \u2261 (x \u2219 y) \u2219 (x \u2219 z)\n-- \u2200 x y z \u2192 (x \u2219 y) \u2219 z \u2261 (x \u2219 z) \u2219 (y \u2219 z)\n\n-- to prove the theorem\n\n-- \u2200 u x y z \u2192 (x \u2219 y \u2219 (z \u2219 u)) \u2219\n-- (( x \u2219 y \u2219 ( z \u2219 u)) \u2219 (x \u2219 z \u2219 (y \u2219 u))) \u2261\n-- x \u2219 z \u2219 (y \u2219 u)\n\n-- [1] David Stanovsk\u00fd. Distributive groupoids are symmetrical-by-medial:\n-- An elementary proof. Commentations Mathematicae Universitatis\n-- Carolinae, 49(4):541\u2013546, 2008.\n\nopen import DistributiveLaws.TaskB-HalvedStepsATP\nopen import DistributiveLaws.TaskB-I\nopen import DistributiveLaws.TaskB-TopDownATP\n\n------------------------------------------------------------------------------\n-- Group theory\n\n-- We formalize the theory of groups using Agda postulates for\n-- the group axioms.\n\n-- Basic properties\nopen import GroupTheory.PropertiesATP\nopen import GroupTheory.PropertiesI\n\n-- Commutator properties\nopen import GroupTheory.Commutator.PropertiesATP\nopen import GroupTheory.Commutator.PropertiesI\n\n-- Abelian groups\nopen import GroupTheory.AbelianGroup.PropertiesATP\n\n------------------------------------------------------------------------------\n-- Logic\n\n-- Propositional logic\nopen import Logic.Propositional.PropertiesATP\nopen import Logic.Predicate.PropertiesI\n\n-- Predicate logic\nopen import Logic.Predicate.PropertiesATP\nopen import Logic.Predicate.PropertiesI\n\n------------------------------------------------------------------------------\n-- LTC\n\n-- Formalization of (a version of) Azcel's Logical Theory of constructions.\n\n-- LTC base\nopen import LTC.Base.Properties\nopen import LTC.Base.PropertiesATP\nopen import LTC.Base.PropertiesI\n\n-- Booleans\nopen import LTC.Data.Bool.PropertiesATP\nopen import LTC.Data.Bool.PropertiesI\n\n-- Lists\nopen import LTC.Data.List.PropertiesATP\nopen import LTC.Data.List.PropertiesI\n\n-- Naturals numbers: Common properties\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.PropertiesI\n\nopen import LTC.Data.Nat.PropertiesByInductionATP\nopen import LTC.Data.Nat.PropertiesByInductionI\n\n-- Naturals numbers: Divisibility relation\nopen import LTC.Data.Nat.Divisibility.PropertiesATP\nopen import LTC.Data.Nat.Divisibility.PropertiesI\n\n-- Naturals numbers: Induction\nopen import LTC.Data.Nat.Induction.LexicographicATP\nopen import LTC.Data.Nat.Induction.LexicographicI\nopen import LTC.Data.Nat.Induction.WellFoundedATP\nopen import LTC.Data.Nat.Induction.WellFoundedI\n\n-- Naturals numbers: Inequalites\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.Inequalities.PropertiesI\n\n-- Naturals numbers: List\nopen import LTC.Data.Nat.List.PropertiesATP\nopen import LTC.Data.Nat.List.PropertiesI\n\n-- Naturals numbers: Unary numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\nopen import LTC.Data.Nat.Unary.Inequalities.PropertiesATP\n\n-- The GCD algorithm\nopen import LTC.Program.GCD.ProofSpecificationATP\nopen import LTC.Program.GCD.ProofSpecificationI\n\n-- Burstall's sort list algorithm\nopen import LTC.Program.SortList.ProofSpecificationATP\nopen import LTC.Program.SortList.ProofSpecificationI\n\n------------------------------------------------------------------------------\n-- LTC-PCF\n\n-- Formalization of a version of Azcel's Logical Theory of constructions.\n\n-- Naturals numbers: Common properties\nopen import LTC-PCF.Data.Nat.PropertiesATP\nopen import LTC-PCF.Data.Nat.PropertiesI\n\n-- Naturals numbers: Divisibility relation\nopen import LTC-PCF.Data.Nat.Divisibility.Properties\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesATP\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesI\n\n-- Naturals numbers: Induction\nopen import LTC-PCF.Data.Nat.Induction.LexicographicATP\nopen import LTC-PCF.Data.Nat.Induction.LexicographicI\nopen import LTC-PCF.Data.Nat.Induction.WellFoundedATP\nopen import LTC-PCF.Data.Nat.Induction.WellFoundedI\n\n-- Naturals numbers: Inequalites\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesATP\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesI\n\n-- Naturals numbers: The recursive operator\nopen import LTC-PCF.Data.Nat.Rec.EquationsATP\nopen import LTC-PCF.Data.Nat.Rec.EquationsI\n\n-- The division algorithm\nopen import LTC-PCF.Program.Division.ProofSpecificationATP\nopen import LTC-PCF.Program.Division.ProofSpecificationI\n\n-- The GCD algorithm\nopen import LTC-PCF.Program.GCD.ProofSpecificationATP\nopen import LTC-PCF.Program.GCD.ProofSpecificationI\n\n------------------------------------------------------------------------------\n-- Peano arithmetic (PA)\n\n-- We write two formalizations of PA. In the axiomatic formalization\n-- we use Agda postulates for Peano's axioms. In the inductive\n-- formalization, we use Agda data types and primitive recursive\n-- functions.\n\n-- Axiomatic PA\nopen import PA.Axiomatic.PropertiesATP\nopen import PA.Axiomatic.PropertiesI\n\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityATP\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityI\n\n-- Inductive PA\nopen import PA.Inductive.Properties\nopen import PA.Inductive.PropertiesATP\nopen import PA.Inductive.PropertiesI\n\nopen import PA.Inductive.PropertiesByInduction\nopen import PA.Inductive.PropertiesByInductionATP\nopen import PA.Inductive.PropertiesByInductionI\n\n------------------------------------------------------------------------------\n-- ATPs failures\n------------------------------------------------------------------------------\n\n-- The ATPs (E, Equinox, Metis and Vampire) could not prove some\n-- theorems.\n\nopen import DistributiveLaws.TaskB-ATP\nopen import LTC-PCF.Program.GCD.EquationsATP\nopen import PA.Axiomatic.PropertiesATP\n\n------------------------------------------------------------------------------\n-- Agsy examples\n------------------------------------------------------------------------------\n\n-- We cannot import the Agsy examples because some modules contain\n-- unsolved metas, so see src\/Agsy\/README.txt\n","old_contents":"------------------------------------------------------------------------------\n-- FOT (First Order Theories)\n------------------------------------------------------------------------------\n\n-- Code accompanying the paper \"Combining Automatic and Interactive\n-- Proof in First Order Theories of Combinators\" by Ana Bove,\n-- Peter Dybjer, and Andr\u00e9s Sicard-Ram\u00edrez.\n\n------------------------------------------------------------------------------\n\nmodule README where\n\n------------------------------------------------------------------------------\n-- Description\n------------------------------------------------------------------------------\n\n-- Examples of the formalization of first order theories showing the\n-- combination of interactive proofs with automatics proofs carried\n-- out by first order automatic theorem provers (ATPs).\n\n------------------------------------------------------------------------------\n-- Prerequisites\n------------------------------------------------------------------------------\n\n-- You can download agda2atp tool (described in above paper) using darcs\n-- with the following command:\n\n-- $ darcs get http:\/\/patch-tag.com\/r\/asr\/agda2atp\n\n-- The agda2atp tool and the FOT code require a modified version of\n-- Agda. See agda2atp\/README.txt for the related instructions.\n\n------------------------------------------------------------------------------\n-- Use\n------------------------------------------------------------------------------\n\n-- Let's suppose you want to use the Metis ATP to prove the group\n-- theory properties stated in the module\n-- GroupTheory.PropertiesATP. First, you should type-check the module using\n-- Agda\n\n-- $ agda -isrc src\/GroupTheory\/PropertiesATP.agda\n\n-- Second, you call the agda2tool using the Metis ATP\n\n-- $ agda2atp -isrc --atp=metis src\/GroupTheory\/PropertiesATP.agda\n\n------------------------------------------------------------------------------\n-- First order theories\n------------------------------------------------------------------------------\n\n-- Conventions\n\n-- The following modules show the formalization of some first order\n-- theories. If the module's name ends in 'I' the module contains\n-- interactive proofs, if it ends in 'ATP' the module contains\n-- combined proofs, otherwise the module contains interactive proofs\n-- that are used by the combined proofs.\n\n------------------------------------------------------------------------------\n-- Distributive laws on a binary operation\n\n-- We prove the proposition 2 (task B) of [1]. Let _\u00b7_ be a\n-- left-associative binary operation, the task B consist in given the\n-- left and right distributive axioms\n\n-- \u2200 x y z \u2192 x \u2219 (y \u2219 z) \u2261 (x \u2219 y) \u2219 (x \u2219 z)\n-- \u2200 x y z \u2192 (x \u2219 y) \u2219 z \u2261 (x \u2219 z) \u2219 (y \u2219 z)\n\n-- to prove the theorem\n\n-- \u2200 u x y z \u2192 (x \u2219 y \u2219 (z \u2219 u)) \u2219\n-- (( x \u2219 y \u2219 ( z \u2219 u)) \u2219 (x \u2219 z \u2219 (y \u2219 u))) \u2261\n-- x \u2219 z \u2219 (y \u2219 u)\n\n-- [1] David Stanovsk\u00fd. Distributive groupoids are symmetrical-by-medial:\n-- An elementary proof. Commentations Mathematicae Universitatis\n-- Carolinae, 49(4):541\u2013546, 2008.\n\nopen import DistributiveLaws.TaskB-HalvedStepsATP\nopen import DistributiveLaws.TaskB-I\nopen import DistributiveLaws.TaskB-TopDownATP\n\n------------------------------------------------------------------------------\n-- Group theory\n\n-- We formalize the theory of groups using Agda postulates for\n-- the group axioms.\n\n-- Basic properties\nopen import GroupTheory.PropertiesATP\nopen import GroupTheory.PropertiesI\n\n-- Commutator properties\nopen import GroupTheory.Commutator.PropertiesATP\nopen import GroupTheory.Commutator.PropertiesI\n\n-- Abelian groups\nopen import GroupTheory.AbelianGroup.PropertiesATP\n\n------------------------------------------------------------------------------\n-- Logic\n\n-- Propositional logic\nopen import Logic.Propositional.PropertiesATP\nopen import Logic.Predicate.PropertiesI\n\n-- Predicate logic\nopen import Logic.Predicate.PropertiesATP\nopen import Logic.Predicate.PropertiesI\n\n------------------------------------------------------------------------------\n-- LTC\n\n-- Formalization of (a version of) Azcel's Logical Theory of constructions.\n\n-- LTC base\nopen import LTC.Base.Properties\nopen import LTC.Base.PropertiesATP\nopen import LTC.Base.PropertiesI\n\n-- Booleans\nopen import LTC.Data.Bool.PropertiesATP\nopen import LTC.Data.Bool.PropertiesI\n\n-- Lists\nopen import LTC.Data.List.PropertiesATP\nopen import LTC.Data.List.PropertiesI\n\n-- Naturals numbers: Common properties\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.PropertiesI\n\nopen import LTC.Data.Nat.PropertiesByInductionATP\nopen import LTC.Data.Nat.PropertiesByInductionI\n\n-- Naturals numbers: Divisibility relation\nopen import LTC.Data.Nat.Divisibility.PropertiesATP\nopen import LTC.Data.Nat.Divisibility.PropertiesI\n\n-- Naturals numbers: Induction\nopen import LTC.Data.Nat.Induction.LexicographicATP\nopen import LTC.Data.Nat.Induction.LexicographicI\nopen import LTC.Data.Nat.Induction.WellFoundedATP\nopen import LTC.Data.Nat.Induction.WellFoundedI\n\n-- Naturals numbers: Inequalites\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.Inequalities.PropertiesI\n\n-- Naturals numbers: List\nopen import LTC.Data.Nat.List.PropertiesATP\nopen import LTC.Data.Nat.List.PropertiesI\n\n-- The GCD algorithm\nopen import LTC.Program.GCD.ProofSpecificationATP\nopen import LTC.Program.GCD.ProofSpecificationI\n\n-- Burstall's sort list algorithm\nopen import LTC.Program.SortList.ProofSpecificationATP\nopen import LTC.Program.SortList.ProofSpecificationI\n\n------------------------------------------------------------------------------\n-- LTC-PCF\n\n-- Formalization of a version of Azcel's Logical Theory of constructions.\n\n-- Naturals numbers: Common properties\nopen import LTC-PCF.Data.Nat.PropertiesATP\nopen import LTC-PCF.Data.Nat.PropertiesI\n\n-- Naturals numbers: Divisibility relation\nopen import LTC-PCF.Data.Nat.Divisibility.Properties\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesATP\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesI\n\n-- Naturals numbers: Induction\nopen import LTC-PCF.Data.Nat.Induction.LexicographicATP\nopen import LTC-PCF.Data.Nat.Induction.LexicographicI\nopen import LTC-PCF.Data.Nat.Induction.WellFoundedATP\nopen import LTC-PCF.Data.Nat.Induction.WellFoundedI\n\n-- Naturals numbers: Inequalites\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesATP\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesI\n\n-- Naturals numbers: The recursive operator\nopen import LTC-PCF.Data.Nat.Rec.EquationsATP\nopen import LTC-PCF.Data.Nat.Rec.EquationsI\n\n-- The division algorithm\nopen import LTC-PCF.Program.Division.ProofSpecificationATP\nopen import LTC-PCF.Program.Division.ProofSpecificationI\n\n-- The GCD algorithm\nopen import LTC-PCF.Program.GCD.ProofSpecificationATP\nopen import LTC-PCF.Program.GCD.ProofSpecificationI\n\n------------------------------------------------------------------------------\n-- Peano arithmetic (PA)\n\n-- We write two formalizations of PA. In the axiomatic formalization\n-- we use Agda postulates for Peano's axioms. In the inductive\n-- formalization, we use Agda data types and primitive recursive\n-- functions.\n\n-- Axiomatic PA\nopen import PA.Axiomatic.PropertiesATP\nopen import PA.Axiomatic.PropertiesI\n\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityATP\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityI\n\n-- Inductive PA\nopen import PA.Inductive.Properties\nopen import PA.Inductive.PropertiesATP\nopen import PA.Inductive.PropertiesI\n\nopen import PA.Inductive.PropertiesByInduction\nopen import PA.Inductive.PropertiesByInductionATP\nopen import PA.Inductive.PropertiesByInductionI\n\n------------------------------------------------------------------------------\n-- ATPs failures\n------------------------------------------------------------------------------\n\n-- The ATPs (E, Equinox, Metis and Vampire) could not prove some\n-- theorems.\n\nopen import DistributiveLaws.TaskB-ATP\nopen import LTC-PCF.Program.GCD.EquationsATP\nopen import PA.Axiomatic.PropertiesATP\n\n------------------------------------------------------------------------------\n-- Agsy examples\n------------------------------------------------------------------------------\n\n-- We cannot import the Agsy examples because some modules contain\n-- unsolved metas, so see src\/Agsy\/README.txt\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"5e711a8f559b2a518b2b9fa467bd3c15c34b1dc3","subject":"Add \u22a2\u02e2 the server, its \u0394 is \u2297-like","message":"Add \u22a2\u02e2 the server, its \u0394 is \u2297-like\n","repos":"crypto-agda\/protocols","old_file":"js-experiment\/Terms.agda","new_file":"js-experiment\/Terms.agda","new_contents":"open import proto\nopen import Types\nopen import prelude\nopen import uri\n\nmodule Terms where\n\ninfix 2 \u22a2\u02e2_ \u22a2_ \u22a2\u1d9c\u1da0_\n\ndata \u22a2_ : (\u0394 : Env) \u2192 Set\u2081 where\n end : \u2200{\u0394}{e : EndedEnv \u0394}\n ------------------\n \u2192 \u22a2 \u0394\n\n output : \u2200 {\u0394 d M P}{{_ : SER M}}\n (l : d \u21a6 send P \u2208 \u0394)(m : M)\n (p : \u22a2 \u0394 [ l \u2254 m ])\n -------------------\n \u2192 \u22a2 \u0394\n\n input : \u2200 {\u0394 d M}{P : M \u2192 _}{{_ : SER M}}\n (l : d \u21a6 recv P \u2208 \u0394)\n (p : \u2200 m \u2192 \u22a2 \u0394 [ l \u2254 m ])\n ----------------------\n \u2192 \u22a2 \u0394\n\n mix : \u2200 {\u0394 \u0394\u2080 \u0394\u2081} (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ])\n (p : \u22a2 \u0394\u2080) (q : \u22a2 \u0394\u2081)\n --------------------\n \u2192 \u22a2 \u0394\n\n cut : \u2200 {\u0394 \u0394\u2080 \u0394\u2081} (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ]) {P d}\n (p : \u22a2 (\u0394\u2080 , d \u21a6 dual P))\n (q : \u22a2 (\u0394\u2081 , d \u21a6 P))\n ---------------------\n \u2192 \u22a2 \u0394\n\n fwd : \u2200 c d {P} \u2192 \u22a2 (\u03b5 , c \u21a6 P , d \u21a6 dual P)\n\n exch-last :\n \u2200 {\u0394 c d P Q} \u2192\n (p : \u22a2 \u0394 , c \u21a6 P , d \u21a6 Q)\n \u2192 \u22a2 \u0394 , d \u21a6 Q , c \u21a6 P\n\n wk-last : \u2200 {\u0394 d}\n (p : \u22a2 \u0394)\n -----------------------\n \u2192 \u22a2 (\u0394 , d \u21a6 end)\n\n end-last : \u2200 {\u0394 d}\n (p : \u22a2 (\u0394 , d \u21a6 end))\n ----------------------\n \u2192 \u22a2 \u0394\n\ndata \u22a2\u1d9c\u1da0_ (\u0394 : Env) : Set\u2081 where\n end : {e : EndedEnv \u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394\n\n output : \u2200 {d M P}{{_ : SER M}}\n (l : d \u21a6 send P \u2208 \u0394) \u2192 (m : M)\n (p : \u22a2\u1d9c\u1da0 \u0394 [ l \u2254 m ])\n --------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\n\n input : \u2200 {d M}{P : M \u2192 _}{{_ : SER M}}\n (l : d \u21a6 recv P \u2208 \u0394)\n (p : \u2200 m \u2192 \u22a2\u1d9c\u1da0 \u0394 [ l \u2254 m ])\n ----------------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\n\n-- The \u0394 for the server contains the view point of the clients\n-- The point is that the meaning of _,_ in \u0394 is \u2297 while it\n-- is \u214b in \u22a2\u1d9c\u1da0\nrecord \u22a2\u02e2_ (\u0394 : Env) : Set\u2081 where\n coinductive\n field\n server-output :\n \u2200 {d M}{{_ : SER M}}{P : M \u2192 Proto}\n (l : d \u21a6 recv P \u2208 \u0394) \u2192\n \u03a3 M \u03bb m \u2192 \u22a2\u02e2 \u0394 [ l \u2254 m ]\n server-input :\n \u2200 {d M}{{_ : SER M}}{P : M \u2192 Proto}\n (l : d \u21a6 send P \u2208 \u0394)\n (m : M) \u2192 \u22a2\u02e2 \u0394 [ l \u2254 m ]\nopen \u22a2\u02e2_ public\n\n-- This is just to confirm that we have enough cases\ntelecom' : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u22a2\u02e2 \u0394 \u2192 \ud835\udfd9\ntelecom' end q = _\ntelecom' (output l m p) q\n = telecom' p (server-input q l m)\ntelecom' (input l p) q\n = case server-output q l of \u03bb { (m , s) \u2192\n telecom' (p m) s }\n\nembed\u1d9c\u1da0 : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u22a2 \u0394\nembed\u1d9c\u1da0 (end {e = e}) = end {e = e}\nembed\u1d9c\u1da0 (output l m p) = output l m (embed\u1d9c\u1da0 p)\nembed\u1d9c\u1da0 (input l p) = input l \u03bb m \u2192 embed\u1d9c\u1da0 (p m)\n\nmix\u1d9c\u1da0 : \u2200 {\u0394 \u0394\u2080 \u0394\u2081} (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ])\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080)\n (q : \u22a2\u1d9c\u1da0 \u0394\u2081)\n -------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\nmix\u1d9c\u1da0 \u0394\u209b end q = tr \u22a2\u1d9c\u1da0_ (Zip-identity \u0394\u209b) q\nmix\u1d9c\u1da0 \u0394\u209b (output l m p) q\n = output (Zip-com\u2208\u2080 \u0394\u209b l) m (mix\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) p q)\nmix\u1d9c\u1da0 \u0394\u209b (input l p) q\n = input (Zip-com\u2208\u2080 \u0394\u209b l) \u03bb m \u2192 mix\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) (p m) q\n\ncut\u1d9c\u1da0 : \u2200 {\u0394 \u0394\u2080 \u0394\u2081}\n (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ]) d P\n (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 dual P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P))\n ---------------------------\n \u2192 \u22a2\u1d9c\u1da0 (\u0394 , d \u21a6 end)\ncut\u1d9c\u1da0 \u0394\u209b d end p q = mix\u1d9c\u1da0 (\u0394\u209b , d \u21a6\u2080 end) p q\n\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) (output here m p) (input here q) = cut\u1d9c\u1da0 \u0394\u209b d (P m) p (q m)\ncut\u1d9c\u1da0 \u0394\u209b d (send P) (input here p) (output here m q) = cut\u1d9c\u1da0 \u0394\u209b d (P m) (p m) q\n\ncut\u1d9c\u1da0 \u0394\u209b d (send P) (output (there l) m p) q\n = output (there (Zip-com\u2208\u2080 \u0394\u209b l)) m (cut\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) d _ p q)\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) (output (there l) m p) q\n = output (there (Zip-com\u2208\u2080 \u0394\u209b l)) m (cut\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) d _ p q)\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) (input (there l) p) q\n = input (there (Zip-com\u2208\u2080 \u0394\u209b l)) \u03bb m \u2192 cut\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) d _ (p m) q\ncut\u1d9c\u1da0 \u0394\u209b d (send P) (input (there l) p) q\n = input (there (Zip-com\u2208\u2080 \u0394\u209b l)) \u03bb m \u2192 cut\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) d _ (p m) q\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) p (output (there l) m q)\n = output (there (Zip-com\u2208\u2081 \u0394\u209b l)) m (cut\u1d9c\u1da0 (Zip-\u2254\u2081 l \u0394\u209b) d _ p q)\ncut\u1d9c\u1da0 \u0394\u209b d (send P) p (output (there l) m q)\n = output (there (Zip-com\u2208\u2081 \u0394\u209b l)) m (cut\u1d9c\u1da0 (Zip-\u2254\u2081 l \u0394\u209b) d _ p q)\ncut\u1d9c\u1da0 \u0394\u209b d (send P) p (input (there l) q)\n = input (there (Zip-com\u2208\u2081 \u0394\u209b l)) \u03bb m \u2192 cut\u1d9c\u1da0 (Zip-\u2254\u2081 l \u0394\u209b) d _ p (q m)\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) p (input (there l) q)\n = input (there (Zip-com\u2208\u2081 \u0394\u209b l)) \u03bb m \u2192 cut\u1d9c\u1da0 (Zip-\u2254\u2081 l \u0394\u209b) d _ p (q m)\n\ncut\u1d9c\u1da0 _ _ (com _ _) (end {e = _ , ()}) _\ncut\u1d9c\u1da0 _ _ (com _ _) _ (end {e = _ , ()})\n\nend-last\u1d9c\u1da0 : \u2200 {\u0394 d}\n (p : \u22a2\u1d9c\u1da0 (\u0394 , d \u21a6 end))\n -----------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\nend-last\u1d9c\u1da0 (end {e = e , _}) = end {e = e}\nend-last\u1d9c\u1da0 (output (there l) m p) = output l m (end-last\u1d9c\u1da0 p)\nend-last\u1d9c\u1da0 (input (there l) p) = input l \u03bb m \u2192 end-last\u1d9c\u1da0 (p m)\n\nwk-last\u1d9c\u1da0 : \u2200 {\u0394 d}\n (p : \u22a2\u1d9c\u1da0 \u0394)\n -----------------------\n \u2192 \u22a2\u1d9c\u1da0 (\u0394 , d \u21a6 end)\nwk-last\u1d9c\u1da0 end = end {e = \u2026 , _}\nwk-last\u1d9c\u1da0 (output l m p) = output (there l) m (wk-last\u1d9c\u1da0 p)\nwk-last\u1d9c\u1da0 (input l p) = input (there l) \u03bb m \u2192 wk-last\u1d9c\u1da0 (p m)\n\nwk-,,\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 \u2192 EndedEnv \u0394\u2081 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\nwk-,,\u1d9c\u1da0 {\u0394\u2081 = \u03b5} p E = p\nwk-,,\u1d9c\u1da0 {\u0394\u2081 = \u0394\u2081 , d \u21a6 P} p (E , e) rewrite Ended-\u2261end e\n = wk-last\u1d9c\u1da0 (wk-,,\u1d9c\u1da0 p E)\n\nmodule _ {d P \u0394\u2080} where\n pre-wk-\u2208 : \u2200 {\u0394\u2081} \u2192 d \u21a6 P \u2208 \u0394\u2081 \u2192 d \u21a6 P \u2208 (\u0394\u2080 ,, \u0394\u2081)\n pre-wk-\u2208 here = here\n pre-wk-\u2208 (there l) = there (pre-wk-\u2208 l)\n\n{-\npre-wk\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 EndedEnv \u0394\u2080 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\npre-wk\u1d9c\u1da0 E\u0394\u2080 end = end {e = {!!}}\npre-wk\u1d9c\u1da0 {\u0394\u2080} {\u0394\u2081} E\u0394\u2080 (output l m p) =\n output (pre-wk-\u2208 l) m (pre-wk\u1d9c\u1da0 {\u0394\u2080} {{!\u0394\u2081!}} E\u0394\u2080 {!!})\npre-wk\u1d9c\u1da0 E\u0394\u2080 (input l p) = {!!}\n-}\n\nfwd-mix\u1d9c\u1da0 : \u2200 {\u0394 c d} P \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u22a2\u1d9c\u1da0 (\u0394 , c \u21a6 P , d \u21a6 dual P)\nfwd-mix\u1d9c\u1da0 end p = wk-last\u1d9c\u1da0 (wk-last\u1d9c\u1da0 p)\nfwd-mix\u1d9c\u1da0 (recv P) p = input (there here) \u03bb m \u2192 output here m (fwd-mix\u1d9c\u1da0 (P m) p)\nfwd-mix\u1d9c\u1da0 (send P) p = input here \u03bb m \u2192 output (there here) m (fwd-mix\u1d9c\u1da0 (P m) p)\n\nfwd\u1d9c\u1da0 : \u2200 c d {P} \u2192 \u22a2\u1d9c\u1da0 (\u03b5 , c \u21a6 P , d \u21a6 dual P)\nfwd\u1d9c\u1da0 _ _ {P} = fwd-mix\u1d9c\u1da0 {\u03b5} P end\n\n\u03b5,, : \u2200 \u0394 \u2192 \u03b5 ,, \u0394 \u2261 \u0394\n\u03b5,, \u03b5 = refl\n\u03b5,, (\u0394 , d \u21a6 P) rewrite \u03b5,, \u0394 = refl\n\npostulate\n exch\u1d9c\u1da0 :\n \u2200 \u0394\u2080 \u0394\u2081 \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394\u2081 ,, \u0394\u2080)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\n {-\nexch\u1d9c\u1da0 \u03b5 \u0394\u2081 p rewrite \u03b5,, \u0394\u2081 = p\nexch\u1d9c\u1da0 \u0394\u2080 \u03b5 p rewrite \u03b5,, \u0394\u2080 = p\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) end = {!!}\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 ._) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) (output here m p) = {!exch\u1d9c\u1da0!}\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) (output (there l) m p) = {!!}\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) (input l p) = {!!}\n-}\n\npre-wk\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 EndedEnv \u0394\u2080 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\npre-wk\u1d9c\u1da0 {\u0394\u2080} {\u0394\u2081} E p = exch\u1d9c\u1da0 \u0394\u2080 \u0394\u2081 (wk-,,\u1d9c\u1da0 p E)\n\nend-of : Env \u2192 Env\nend-of \u03b5 = \u03b5\nend-of (\u0394 , d \u21a6 P) = end-of \u0394 , d \u21a6 end\n\nend-of-Ended : \u2200 \u0394 \u2192 EndedEnv (end-of \u0394)\nend-of-Ended \u03b5 = _\nend-of-Ended (\u0394 , d \u21a6 P) = end-of-Ended \u0394 , _\n\nend-of-\u22ce : \u2200 \u0394 \u2192 [ \u0394 is \u0394 \u22ce end-of \u0394 ]\nend-of-\u22ce \u03b5 = \u03b5\nend-of-\u22ce (\u0394 , d \u21a6 P) = end-of-\u22ce \u0394 , d \u21a6\u2080 P\n\nend-of-,,-\u22ce : \u2200 \u0394\u2080 \u0394\u2081 \u2192 [ \u0394\u2080 ,, \u0394\u2081 is \u0394\u2080 ,, end-of \u0394\u2081 \u22ce end-of \u0394\u2080 ,, \u0394\u2081 ]\nend-of-,,-\u22ce \u0394\u2080 \u03b5 = end-of-\u22ce \u0394\u2080\nend-of-,,-\u22ce \u0394\u2080 (\u0394\u2081 , d \u21a6 P) = end-of-,,-\u22ce \u0394\u2080 \u0394\u2081 , d \u21a6\u2081 P\n\n,,-assoc : \u2200 {\u0394\u2080 \u0394\u2081 \u0394\u2082} \u2192 \u0394\u2080 ,, (\u0394\u2081 ,, \u0394\u2082) \u2261 (\u0394\u2080 ,, \u0394\u2081) ,, \u0394\u2082\n,,-assoc {\u0394\u2082 = \u03b5} = refl\n,,-assoc {\u0394\u2080} {\u0394\u2081} {\u0394\u2082 , d \u21a6 P} rewrite ,,-assoc {\u0394\u2080} {\u0394\u2081} {\u0394\u2082} = refl\n\ncut,,\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} d P\n (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 dual P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P))\n ---------------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\ncut,,\u1d9c\u1da0 {\u0394\u2080}{\u0394\u2081} d P p q =\n end-last\u1d9c\u1da0\n (cut\u1d9c\u1da0 \u0394\u209b d P\n (exch\u1d9c\u1da0 (\u0394\u2080 ,, end-of \u0394\u2081) (\u03b5 , d \u21a6 dual P)\n (tr \u22a2\u1d9c\u1da0_ (! (,,-assoc {\u03b5 , d \u21a6 dual P} {\u0394\u2080} {end-of \u0394\u2081}))\n (wk-,,\u1d9c\u1da0\n (exch\u1d9c\u1da0 (\u03b5 , d \u21a6 dual P) _ p) (end-of-Ended _))))\n (pre-wk\u1d9c\u1da0 (end-of-Ended _) q))\n where \u0394\u209b = end-of-,,-\u22ce \u0394\u2080 \u0394\u2081\n\npostulate\n !cut,,\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} d P\n (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 dual P))\n ---------------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\n-- !cut,,\u1d9c\u1da0 {\u0394\u2080}{\u0394\u2081} d P p q = {!!}\n\n-- only the last two are exchanged, some more has to be done\nexch-last\u1d9c\u1da0 :\n \u2200 {\u0394 c d P Q} \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394 , c \u21a6 P , d \u21a6 Q)\n \u2192 \u22a2\u1d9c\u1da0 \u0394 , d \u21a6 Q , c \u21a6 P\nexch-last\u1d9c\u1da0 (end {e = (a , b) , c}) = end {e = (a , c) , b}\nexch-last\u1d9c\u1da0 (output here m p) = output (there here) m (exch-last\u1d9c\u1da0 p)\nexch-last\u1d9c\u1da0 (output (there here) m p) = output here m (exch-last\u1d9c\u1da0 p)\nexch-last\u1d9c\u1da0 (output (there (there l)) m p) = output (there (there l)) m (exch-last\u1d9c\u1da0 p)\nexch-last\u1d9c\u1da0 (input here p) = input (there here) \u03bb m \u2192 exch-last\u1d9c\u1da0 (p m)\nexch-last\u1d9c\u1da0 (input (there here) p) = input here \u03bb m \u2192 exch-last\u1d9c\u1da0 (p m)\nexch-last\u1d9c\u1da0 (input (there (there l)) p) = input (there (there l)) \u03bb m \u2192 exch-last\u1d9c\u1da0 (p m)\n\n{-\ndata Relabel : Env \u2192 Env \u2192 Set where\n \u03b5 : Relabel \u03b5 \u03b5\n _,_\u21a6_ : \u2200 {\u0394\u2080 \u0394\u2081 c d P} \u2192 Relabel \u0394\u2080 \u0394\u2081 \u2192 Relabel (\u0394\u2080 , c \u21a6 P) (\u0394\u2081 , d \u21a6 P)\n\nmodule _ where\n rebalel-\u2208 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 Relabel \u0394\u2080 \u0394\u2081\n (l : d \u21a6 P \u2208 \u0394\u2080)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081\n relabel : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 Relabel \u0394\u2080 \u0394\u2081\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081\n relabel end = {!end!}\n relabel (output l m p) = output {!l!} {!!} {!!}\n relabel (input l p) = {!!}\n\npar\u1d9c\u1da0 : \u2200 {\u0394 c} P Q\n (p : \u22a2\u1d9c\u1da0 (\u0394 , c \u21a6 P , c \u21a6 Q))\n \u2192 \u22a2\u1d9c\u1da0 (\u0394 , c \u21a6 (P \u214b' Q))\n-- TODO only one channel name!!!\n-- TODO empty context\n-- TODO try to match on 'p' first\nbroken-par\u1d9c\u1da0 : \u2200 {c d e} P Q\n (p : \u22a2\u1d9c\u1da0 (\u03b5 , c \u21a6 P , d \u21a6 Q))\n \u2192 \u22a2\u1d9c\u1da0 (\u03b5 , e \u21a6 (P \u214b' Q))\nbroken-par\u1d9c\u1da0 end Q p = {!end-last\u1d9c\u1da0 (exch-last\u1d9c\u1da0 p)!}\nbroken-par\u1d9c\u1da0 (com x P) end p = end-last\u1d9c\u1da0 {!p!}\nbroken-par\u1d9c\u1da0 (com x P) (com y Q) (end {e = _ , ()})\n\nbroken-par\u1d9c\u1da0 (com x P) (com .OUT Q) (output here m p)\n = output here R (output here m (broken-par\u1d9c\u1da0 _ _ p))\nbroken-par\u1d9c\u1da0 (com x P) (com .IN Q) (input here p)\n = output here R (input here \u03bb m \u2192 broken-par\u1d9c\u1da0 _ _ (p m))\n\nbroken-par\u1d9c\u1da0 (com .OUT P) (com y Q) (output (there here) m p)\n = output here L (output here m (broken-par\u1d9c\u1da0 _ _ p))\nbroken-par\u1d9c\u1da0 (com .IN P) (com y Q) (input (there here) p)\n = output here L (input here \u03bb m \u2192 broken-par\u1d9c\u1da0 _ _ (p m))\n\nbroken-par\u1d9c\u1da0 (com x P) (com y Q) (output (there (there ())) m p)\nbroken-par\u1d9c\u1da0 (com x P) (com y Q) (input (there (there ())) p)\n-}\n\nmodule _ {c d cd} where\n bi-fwd : \u2200 P Q \u2192 \u22a2\u1d9c\u1da0 (\u03b5 , cd \u21a6 P \u2297 Q , c \u21a6 dual P , d \u21a6 dual Q)\n\n private\n module _ {M} {{_ : SER M}} {P : M \u2192 Proto} {Q} where\n goL : \u2200 x \u2192 \u22a2\u1d9c\u1da0 \u03b5 , cd \u21a6 com x (\u03bb m \u2192 P m \u2297 Q)\n , c \u21a6 dual (com x P)\n , d \u21a6 dual Q\n\n goL IN = input (there (there here)) \u03bb m \u2192 output (there here) m (bi-fwd _ _)\n goL OUT = input (there here) \u03bb m \u2192 output (there (there here)) m (bi-fwd _ _)\n\n goR : \u2200 x \u2192 \u22a2\u1d9c\u1da0 \u03b5 , cd \u21a6 com x (\u03bb m \u2192 Q \u2297 P m)\n , c \u21a6 dual Q\n , d \u21a6 dual (com x P)\n goR IN = input (there (there here)) \u03bb m \u2192 output here m (bi-fwd _ _)\n goR OUT = input here \u03bb m \u2192 output (there (there here)) m (bi-fwd _ _)\n\n bi-fwd end Q = exch-last\u1d9c\u1da0 (wk-last\u1d9c\u1da0 (fwd\u1d9c\u1da0 _ _))\n bi-fwd (com x P) end = wk-last\u1d9c\u1da0 (fwd\u1d9c\u1da0 _ _)\n bi-fwd (com x P) (com y Q) = input (there (there here)) [L: goL x ,R: goR y ]\n\n module _ {\u0394\u2080 \u0394\u2081 P Q} where\n \u2297\u1d9c\u1da0 : (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , c \u21a6 P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 Q))\n ----------------------------------\n \u2192 \u22a2\u1d9c\u1da0 (\u0394\u2080 ,, \u0394\u2081 , cd \u21a6 (P \u2297 Q))\n \u2297\u1d9c\u1da0 p q = !cut,,\u1d9c\u1da0 _ _ p (!cut,,\u1d9c\u1da0 _ _ q (bi-fwd P Q))\n\n {-\nexch\u1d9c\u1da0 :\n \u2200 {\u0394 c d P Q} \u2192\n (l : c \u21a6 P \u2208 \u0394)\n (p : \u22a2\u1d9c\u1da0 \u0394 , d \u21a6 Q)\n \u2192 \u22a2\u1d9c\u1da0 \u0394 [ l \u2254 d \u21a6 Q ] , c \u21a6 P\nexch\u1d9c\u1da0 here p = exch-last\u1d9c\u1da0 p\nexch\u1d9c\u1da0 (there l) p = {!!}\n-}\n\n{-\nrot\u1d9c\u1da0 : \u2200 \u0394 {c P} \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394 , c \u21a6 P)\n \u2192 \u22a2\u1d9c\u1da0 \u03b5 , c \u21a6 P ,, \u0394\nrot\u1d9c\u1da0 \u03b5 p = p\nrot\u1d9c\u1da0 (\u0394 , d \u21a6 P) p = {!rot\u1d9c\u1da0 \u0394 p!}\n\nexch\u1d9c\u1da0 :\n \u2200 {\u0394\u2080} \u0394\u2081 {c d P Q} \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080 , c \u21a6 P , d \u21a6 Q ,, \u0394\u2081)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 , d \u21a6 Q , c \u21a6 P ,, \u0394\u2081\nexch\u1d9c\u1da0 \u03b5 p = exch-last\u1d9c\u1da0 p\nexch\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P) (end e) = end {!!}\nexch\u1d9c\u1da0 (\u0394\u2081 , d\u2081 \u21a6 ._) (output here m p) = output here m ({!exch\u1d9c\u1da0 \u0394\u2081 p!})\nexch\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P) (output (there l) m p) = {!!}\nexch\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P) (input l p) = {!!}\n-}\n\n_\u2286_ : (\u0394\u2080 \u0394\u2081 : Env) \u2192 Set\u2081\n_\u2287_ : (\u0394\u2080 \u0394\u2081 : Env) \u2192 Set\u2081\n\n\u0394\u2080 \u2286 \u0394\u2081 = \u2200 {d P} \u2192 d \u21a6 P \u2208 \u0394\u2080 \u2192 d \u21a6 P \u2208 \u0394\u2081\n\u0394\u2080 \u2287 \u0394\u2081 = \u0394\u2081 \u2286 \u0394\u2080\n\nget-put : \u2200 {d P \u0394 c Q} \u2192\n (l : d \u21a6 P \u2208 \u0394) \u2192 c \u21a6 Q \u2208 (\u0394 [ l \u2254 c \u21a6 Q ])\nget-put here = here\nget-put (there l) = there (get-put l)\n\n{-\n\u2286_[_\u2254_\u21a6_] : \u2200 {\u0394\u2080 \u0394\u2081} (f : \u0394\u2080 \u2286 \u0394\u2081)\n {d P} (l : d \u21a6 P \u2208 \u0394\u2080) (c : URI) (Q : Proto)\n \u2192 (\u0394\u2080 [ l \u2254 c \u21a6 Q ]) \u2286 (\u0394\u2081 [ f l \u2254 c \u21a6 Q ])\n\u2286 f [ l \u2254 c \u21a6 Q ] {d'} {P'} l' = {!!}\n\n(l : d \u21a6 P \u2208 \u0394)\n\u2192 \u0394 [ l \u2254 ]\n\nrecord _\u2248_ (\u0394\u2080 \u0394\u2081 : Env) : Set\u2081 where\n constructor _,_\n field\n \u2248\u2286 : \u0394\u2080 \u2286 \u0394\u2081\n \u2248\u2287 : \u0394\u2080 \u2287 \u0394\u2081\nopen _\u2248_ public\n\n\u2248_[_\u2254_\u21a6_] : \u2200 {\u0394\u2080 \u0394\u2081} (\u0394\u2091 : \u0394\u2080 \u2248 \u0394\u2081)\n {d P} (l : d \u21a6 P \u2208 \u0394\u2080) (c : URI) (Q : Proto)\n \u2192 (\u0394\u2080 [ l \u2254 c \u21a6 Q ]) \u2248 (\u0394\u2081 [ \u2248\u2286 \u0394\u2091 l \u2254 m ])\n\u2248 \u0394\u2091 [ here \u2254 m ] = {!!}\n\u2248 \u0394\u2091 [ there l \u2254 m ] = {!!}\n\n{-(\u03bb l' \u2192 {!\u2248\u2286 \u0394\u2091!}) , from\n where\n from : \u2200 {\u0394\u2080 \u0394\u2081 d io M} {P : M \u2192 Proto} {ser : SER M}\n {\u0394\u2091 : \u0394\u2080 \u2248 \u0394\u2081} {l : d \u21a6 com io P \u2208 \u0394\u2080} {m : M} {d\u2081} {P\u2081} \u2192\n d\u2081 \u21a6 P\u2081 \u2208 (\u0394\u2081 [ \u2248\u2286 \u0394\u2091 l \u2254 m ]) \u2192 d\u2081 \u21a6 P\u2081 \u2208 (\u0394\u2080 [ l \u2254 m ])\n from = {!!}\n\n\u2248\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081}\n (\u0394\u2091 : \u0394\u2080 \u2248 \u0394\u2081)\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080)\n -----------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081\n\u2248\u1d9c\u1da0 \u0394\u2091 (end {e = e}) = end {e = {!!}}\n\u2248\u1d9c\u1da0 \u0394\u2091 (output l m p) = output (\u2248\u2286 \u0394\u2091 l) m (\u2248\u1d9c\u1da0 (\u2248 \u0394\u2091 [ l \u2254 m ]) p)\n\u2248\u1d9c\u1da0 \u0394\u2091 (input l p) = {!!}\n-}\n-}\n\ncut-elim : \u2200 {\u0394} (p : \u22a2 \u0394)\n ------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\ncut-elim (end {e = e}) = end {e = e}\ncut-elim (output l m p) = output l m (cut-elim p)\ncut-elim (input l p) = input l (\u03bb m \u2192 cut-elim (p m))\ncut-elim (mix \u0394\u209b p q) = mix\u1d9c\u1da0 \u0394\u209b (cut-elim p) (cut-elim q)\ncut-elim (cut \u0394\u209b {P} {d} p q) = end-last\u1d9c\u1da0 (cut\u1d9c\u1da0 \u0394\u209b d P (cut-elim p) (cut-elim q))\ncut-elim (end-last p) = end-last\u1d9c\u1da0 (cut-elim p)\ncut-elim (wk-last p) = wk-last\u1d9c\u1da0 (cut-elim p)\ncut-elim (fwd c d) = fwd\u1d9c\u1da0 c d\ncut-elim (exch-last p) = exch-last\u1d9c\u1da0 (cut-elim p)\n\n{-\n\nstart : \u2200 {\u0394} P\n \u2192 \u22a2 [ clientURI \u21a6 dual P ]\n \u2192 (\u2200 d \u2192 \u22a2 (\u0394 , d \u21a6 P))\n \u2192 \u22a2 \u0394\nstart P p q = cut {!!} (... p) (q {!!})\n-}\n\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 : \u2200 {P d} \u2192 \u22a2\u1d9c\u1da0 [ d \u21a6 P ] \u2192 \u27e6 P \u27e7\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 {end} end = _\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 {com x P} (end {e = _ , ()})\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (output here m der) = m , \u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 der\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (output (there ()) m der)\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (input here x\u2081) m = \u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (x\u2081 m)\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (input (there ()) x\u2081)\n\nSatisfy : \u2200 {p d} P \u2192 (R : \u27e6 log P \u27e7 \u2192 Set p) \u2192 \u22a2 [ d \u21a6 P ] \u2192 Set p\nSatisfy P Rel d = (d' : \u27e6 dual P \u27e7) \u2192 Rel (telecom P (\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (cut-elim d)) d')\n\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 : \u2200 {P d} \u2192 \u27e6 P \u27e7 \u2192 \u22a2\u1d9c\u1da0 [ d \u21a6 P ]\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 {end} p = end\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 {recv P} p = input here (\u03bb m \u2192 \u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 (p m))\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 {send P} (m , p) = output here m (\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 p)\n\n\u27e6\u27e7\u2192\u22a2 : \u2200 {P d} \u2192 \u27e6 P \u27e7 \u2192 \u22a2 [ d \u21a6 P ]\n\u27e6\u27e7\u2192\u22a2 {end} p = end\n\u27e6\u27e7\u2192\u22a2 {recv P} p = input here (\u03bb m \u2192 \u27e6\u27e7\u2192\u22a2 (p m))\n\u27e6\u27e7\u2192\u22a2 {send P} (m , p) = output here m (\u27e6\u27e7\u2192\u22a2 p)\n\n{-\n\u22a2toProc : \u2200 {\u0394} \u2192 \u22a2 \u0394 \u2192 JSProc\n\u22a2toProc end = end\n\u22a2toProc (output {d = d} l m prg) = output d (serialize m) (\u22a2toProc prg)\n\u22a2toProc (input {d = d} l prg) = input d ([succeed: (\u03bb m \u2192 \u22a2toProc (prg m)) ,fail: error ] \u2218 parse)\n\u22a2toProc (start P prg x) = start (\u22a2toProc prg) (\u03bb d \u2192 \u22a2toProc (x d))\n\n\n\u22a2toProc-WT : \u2200 {\u0394} (der : \u22a2 \u0394) \u2192 \u0394 \u22a2 \u22a2toProc der\n\u22a2toProc-WT (end {x}) = end {_} {x}\n\u22a2toProc-WT (output {{x}} l m der) = output l (sym (rinv m)) (\u22a2toProc-WT der)\n\u22a2toProc-WT (input {{x}} l x\u2081) = input l \u03bb s m x \u2192\n subst (\u03bb X \u2192 _ [ l \u2254 m ] \u22a2 [succeed: (\u22a2toProc \u2218 x\u2081) ,fail: error ] X) x (\u22a2toProc-WT (x\u2081 m))\n\u22a2toProc-WT (start P der x) = start P (\u22a2toProc-WT der) (\u03bb d \u2192 \u22a2toProc-WT (x d))\n-}\n\n\u27e6_\u27e7E : Env \u2192 Set\n\u27e6 \u03b5 \u27e7E = \ud835\udfd9\n\u27e6 \u0394 , d \u21a6 P \u27e7E = \u27e6 \u0394 \u27e7E \u00d7 \u27e6 P \u27e7\n\nmapEnv : (Proto \u2192 Proto) \u2192 Env \u2192 Env\nmapEnv f \u03b5 = \u03b5\nmapEnv f (\u0394 , d \u21a6 P) = mapEnv f \u0394 , d \u21a6 f P\n\nmapEnv-all : \u2200 {P Q : URI \u2192 Proto \u2192 Set}{\u0394}{f : Proto \u2192 Proto}\n \u2192 (\u2200 d x \u2192 P d x \u2192 Q d (f x))\n \u2192 AllEnv P \u0394 \u2192 AllEnv Q (mapEnv f \u0394)\nmapEnv-all {\u0394 = \u03b5} f\u2081 \u2200\u0394 = \u2200\u0394\nmapEnv-all {\u0394 = \u0394 , d \u21a6 P\u2081} f\u2081 (\u2200\u0394 , P) = (mapEnv-all f\u2081 \u2200\u0394) , f\u2081 d P\u2081 P\n\nmapEnv-Ended : \u2200 {f : Proto \u2192 Proto}{\u0394} \u2192 Ended (f end)\n \u2192 AllEnv (\u03bb _ \u2192 Ended) \u0394 \u2192 AllEnv (\u03bb _ \u2192 Ended) (mapEnv f \u0394)\nmapEnv-Ended eq = mapEnv-all (\u03bb { d end _ \u2192 eq ; d (send P) () ; d (recv P) () })\n\nmapEnv-\u2208 : \u2200 {d P f \u0394} \u2192 d \u21a6 P \u2208 \u0394 \u2192 d \u21a6 f P \u2208 mapEnv f \u0394\nmapEnv-\u2208 here = here\nmapEnv-\u2208 (there der) = there (mapEnv-\u2208 der)\n\nmodule _ {d c M cf}{m : M}{{_ : M \u2243? SERIAL}}{p} where\n subst-lemma-one-point-four : \u2200 {\u0394}( l : d \u21a6 com c p \u2208 \u0394 ) \u2192\n let f = mapProto cf\n in (mapEnv f (\u0394 [ l \u2254 m ])) \u2261 (_[_\u2254_]{c = cf c} (mapEnv f \u0394) (mapEnv-\u2208 l) m)\n subst-lemma-one-point-four here = refl\n subst-lemma-one-point-four (there {d' = d'}{P'} l) = ap (\u03bb X \u2192 X , d' \u21a6 mapProto cf P') (subst-lemma-one-point-four l)\n\nmodule _ {d P} where\n project : \u2200 {\u0394} \u2192 d \u21a6 P \u2208 \u0394 \u2192 \u27e6 \u0394 \u27e7E \u2192 \u27e6 P \u27e7\n project here env = snd env\n project (there mem) env = project mem (fst env)\n\nempty : \u2200 {\u0394} \u2192 AllEnv (\u03bb _ \u2192 Ended) \u0394 \u2192 \u27e6 \u0394 \u27e7E\nempty {\u03b5} <> = _\nempty {\u0394 , d \u21a6 end} (fst , <>) = empty fst , _\nempty {\u0394 , d \u21a6 com x P} (fst , ())\n\nnoRecvInLog : \u2200 {d M}{{_ : M \u2243? SERIAL}}{P : M \u2192 _}{\u0394} \u2192 d \u21a6 recv P \u2208 mapEnv log \u0394 \u2192 \ud835\udfd8\nnoRecvInLog {\u0394 = \u03b5} ()\nnoRecvInLog {\u0394 = \u0394 , d\u2081 \u21a6 end} (there l) = noRecvInLog l\nnoRecvInLog {\u0394 = \u0394 , d\u2081 \u21a6 com x\u2081 P\u2081} (there l) = noRecvInLog l\n\nmodule _ {d M P}{{_ : M \u2243? SERIAL}} where\n lookup : \u2200 {\u0394}(l : d \u21a6 send P \u2208 \u0394) \u2192 \u27e6 \u0394 \u27e7E \u2192 \u03a3 M \u03bb m \u2192 \u27e6 \u0394 [ l \u2254 m ] \u27e7E\n lookup here (env , (m , p)) = m , (env , p)\n lookup (there l) (env , P') = let (m , env') = lookup l env in m , (env' , P')\n\n set : \u2200 {\u0394}(l : d \u21a6 recv P \u2208 \u0394) \u2192 \u27e6 \u0394 \u27e7E \u2192 (m : M) \u2192 \u27e6 \u0394 [ l \u2254 m ] \u27e7E\n set here (env , f) m = env , f m\n set (there l) (env , P') m = set l env m , P'\n\n set\u03a3 : \u2200 {\u0394}(l : d \u21a6 send P \u2208 \u0394) \u2192 (m : M) \u2192 \u27e6 \u0394 [ l \u2254 m ] \u27e7E \u2192 \u27e6 \u0394 \u27e7E\n set\u03a3 here m env = fst env , (m , snd env)\n set\u03a3 (there l) m env = set\u03a3 l m (fst env) , snd env\n\n {-\nforgetConc : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 mapEnv log \u0394 \u2192 \u27e6 mapEnv log \u0394 \u27e7E\nforgetConc (end e) = empty \u2026\nforgetConc {\u0394} (output l m der) = set\u03a3 l m (forgetConc {{!set\u03a3 l m!}} der) -- (forgetConc der)\nforgetConc (input l x\u2081) with noRecvInLog l\n... | ()\n-}\n\n\u22a2telecom : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u27e6 mapEnv dual \u0394 \u27e7E \u2192 \u22a2 mapEnv log \u0394\n\u22a2telecom end env = end {e = mapEnv-Ended _ \u2026}\n\u22a2telecom (output l m der) env = output (mapEnv-\u2208 l) m (subst (\u22a2_)\n (subst-lemma-one-point-four l) (\u22a2telecom der (subst \u27e6_\u27e7E (sym (subst-lemma-one-point-four l)) (set (mapEnv-\u2208 l) env m))))\n\u22a2telecom (input l x\u2081) env = let (m , env') = lookup (mapEnv-\u2208 l) env\n hyp = \u22a2telecom (x\u2081 m) (subst (\u27e6_\u27e7E) (sym (subst-lemma-one-point-four l)) env')\n in output (mapEnv-\u2208 l) m\n (subst (\u22a2_) (subst-lemma-one-point-four l) hyp)\n\n-- old version\n{-\ncut\u1d9c\u1da0 : \u2200 {\u0394 d P} \u2192 \u27e6 dual P \u27e7 \u2192 \u22a2\u1d9c\u1da0 \u0394 , d \u21a6 P \u2192 \u22a2\u1d9c\u1da0 \u0394\ncut\u1d9c\u1da0 D (end {allEnded = \u0394E , PE }) = end {allEnded = \u0394E}\ncut\u1d9c\u1da0 D (output here m E) = cut\u1d9c\u1da0 (D m) E\ncut\u1d9c\u1da0 D (output (there l) m E) = output l m (cut\u1d9c\u1da0 D E)\ncut\u1d9c\u1da0 (m , D) (input here x\u2081) = cut\u1d9c\u1da0 D (x\u2081 m)\ncut\u1d9c\u1da0 D (input (there l) x\u2081) = input l (\u03bb m \u2192 cut\u1d9c\u1da0 D (x\u2081 m))\n\ncut : \u2200 {\u0394 \u0394' \u0393 \u0393' d P} \u2192 \u22a2 \u0394 , clientURI \u21a6 dual P +++ \u0394' \u2192 \u22a2 \u0393 , d \u21a6 P +++ \u0393' \u2192 \u22a2 (\u0394 +++ \u0394') +++ (\u0393 +++ \u0393')\ncut D E = {!!}\n-}\n\n\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"open import proto\nopen import Types\nopen import prelude\nopen import uri\n\nmodule Terms where\n\ninfix 2 \u22a2_ \u22a2\u1d9c\u1da0_\n\ndata \u22a2_ : (\u0394 : Env) \u2192 Set\u2081 where\n end : \u2200{\u0394}{e : EndedEnv \u0394}\n ------------------\n \u2192 \u22a2 \u0394\n\n output : \u2200 {\u0394 d M P}{{_ : SER M}}\n (l : d \u21a6 send P \u2208 \u0394)(m : M)\n (p : \u22a2 \u0394 [ l \u2254 m ])\n -------------------\n \u2192 \u22a2 \u0394\n\n input : \u2200 {\u0394 d M}{P : M \u2192 _}{{_ : SER M}}\n (l : d \u21a6 recv P \u2208 \u0394)\n (p : \u2200 m \u2192 \u22a2 \u0394 [ l \u2254 m ])\n ----------------------\n \u2192 \u22a2 \u0394\n\n mix : \u2200 {\u0394 \u0394\u2080 \u0394\u2081} (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ])\n (p : \u22a2 \u0394\u2080) (q : \u22a2 \u0394\u2081)\n --------------------\n \u2192 \u22a2 \u0394\n\n cut : \u2200 {\u0394 \u0394\u2080 \u0394\u2081} (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ]) {P d}\n (p : \u22a2 (\u0394\u2080 , d \u21a6 dual P))\n (q : \u22a2 (\u0394\u2081 , d \u21a6 P))\n ---------------------\n \u2192 \u22a2 \u0394\n\n fwd : \u2200 c d {P} \u2192 \u22a2 (\u03b5 , c \u21a6 P , d \u21a6 dual P)\n\n exch-last :\n \u2200 {\u0394 c d P Q} \u2192\n (p : \u22a2 \u0394 , c \u21a6 P , d \u21a6 Q)\n \u2192 \u22a2 \u0394 , d \u21a6 Q , c \u21a6 P\n\n wk-last : \u2200 {\u0394 d}\n (p : \u22a2 \u0394)\n -----------------------\n \u2192 \u22a2 (\u0394 , d \u21a6 end)\n\n end-last : \u2200 {\u0394 d}\n (p : \u22a2 (\u0394 , d \u21a6 end))\n ----------------------\n \u2192 \u22a2 \u0394\n\ndata \u22a2\u1d9c\u1da0_ (\u0394 : Env) : Set\u2081 where\n end : {e : EndedEnv \u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394\n\n output : \u2200 {d M P}{{_ : SER M}}\n (l : d \u21a6 send P \u2208 \u0394) \u2192 (m : M)\n (p : \u22a2\u1d9c\u1da0 \u0394 [ l \u2254 m ])\n --------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\n\n input : \u2200 {d M}{P : M \u2192 _}{{_ : SER M}}\n (l : d \u21a6 recv P \u2208 \u0394)\n (p : \u2200 m \u2192 \u22a2\u1d9c\u1da0 \u0394 [ l \u2254 m ])\n ----------------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\n\nembed\u1d9c\u1da0 : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u22a2 \u0394\nembed\u1d9c\u1da0 (end {e = e}) = end {e = e}\nembed\u1d9c\u1da0 (output l m p) = output l m (embed\u1d9c\u1da0 p)\nembed\u1d9c\u1da0 (input l p) = input l \u03bb m \u2192 embed\u1d9c\u1da0 (p m)\n\nmix\u1d9c\u1da0 : \u2200 {\u0394 \u0394\u2080 \u0394\u2081} (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ])\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080)\n (q : \u22a2\u1d9c\u1da0 \u0394\u2081)\n -------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\nmix\u1d9c\u1da0 \u0394\u209b end q = tr \u22a2\u1d9c\u1da0_ (Zip-identity \u0394\u209b) q\nmix\u1d9c\u1da0 \u0394\u209b (output l m p) q\n = output (Zip-com\u2208\u2080 \u0394\u209b l) m (mix\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) p q)\nmix\u1d9c\u1da0 \u0394\u209b (input l p) q\n = input (Zip-com\u2208\u2080 \u0394\u209b l) \u03bb m \u2192 mix\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) (p m) q\n\ncut\u1d9c\u1da0 : \u2200 {\u0394 \u0394\u2080 \u0394\u2081}\n (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ]) d P\n (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 dual P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P))\n ---------------------------\n \u2192 \u22a2\u1d9c\u1da0 (\u0394 , d \u21a6 end)\ncut\u1d9c\u1da0 \u0394\u209b d end p q = mix\u1d9c\u1da0 (\u0394\u209b , d \u21a6\u2080 end) p q\n\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) (output here m p) (input here q) = cut\u1d9c\u1da0 \u0394\u209b d (P m) p (q m)\ncut\u1d9c\u1da0 \u0394\u209b d (send P) (input here p) (output here m q) = cut\u1d9c\u1da0 \u0394\u209b d (P m) (p m) q\n\ncut\u1d9c\u1da0 \u0394\u209b d (send P) (output (there l) m p) q\n = output (there (Zip-com\u2208\u2080 \u0394\u209b l)) m (cut\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) d _ p q)\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) (output (there l) m p) q\n = output (there (Zip-com\u2208\u2080 \u0394\u209b l)) m (cut\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) d _ p q)\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) (input (there l) p) q\n = input (there (Zip-com\u2208\u2080 \u0394\u209b l)) \u03bb m \u2192 cut\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) d _ (p m) q\ncut\u1d9c\u1da0 \u0394\u209b d (send P) (input (there l) p) q\n = input (there (Zip-com\u2208\u2080 \u0394\u209b l)) \u03bb m \u2192 cut\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) d _ (p m) q\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) p (output (there l) m q)\n = output (there (Zip-com\u2208\u2081 \u0394\u209b l)) m (cut\u1d9c\u1da0 (Zip-\u2254\u2081 l \u0394\u209b) d _ p q)\ncut\u1d9c\u1da0 \u0394\u209b d (send P) p (output (there l) m q)\n = output (there (Zip-com\u2208\u2081 \u0394\u209b l)) m (cut\u1d9c\u1da0 (Zip-\u2254\u2081 l \u0394\u209b) d _ p q)\ncut\u1d9c\u1da0 \u0394\u209b d (send P) p (input (there l) q)\n = input (there (Zip-com\u2208\u2081 \u0394\u209b l)) \u03bb m \u2192 cut\u1d9c\u1da0 (Zip-\u2254\u2081 l \u0394\u209b) d _ p (q m)\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) p (input (there l) q)\n = input (there (Zip-com\u2208\u2081 \u0394\u209b l)) \u03bb m \u2192 cut\u1d9c\u1da0 (Zip-\u2254\u2081 l \u0394\u209b) d _ p (q m)\n\ncut\u1d9c\u1da0 _ _ (com _ _) (end {e = _ , ()}) _\ncut\u1d9c\u1da0 _ _ (com _ _) _ (end {e = _ , ()})\n\nend-last\u1d9c\u1da0 : \u2200 {\u0394 d}\n (p : \u22a2\u1d9c\u1da0 (\u0394 , d \u21a6 end))\n -----------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\nend-last\u1d9c\u1da0 (end {e = e , _}) = end {e = e}\nend-last\u1d9c\u1da0 (output (there l) m p) = output l m (end-last\u1d9c\u1da0 p)\nend-last\u1d9c\u1da0 (input (there l) p) = input l \u03bb m \u2192 end-last\u1d9c\u1da0 (p m)\n\nwk-last\u1d9c\u1da0 : \u2200 {\u0394 d}\n (p : \u22a2\u1d9c\u1da0 \u0394)\n -----------------------\n \u2192 \u22a2\u1d9c\u1da0 (\u0394 , d \u21a6 end)\nwk-last\u1d9c\u1da0 end = end {e = \u2026 , _}\nwk-last\u1d9c\u1da0 (output l m p) = output (there l) m (wk-last\u1d9c\u1da0 p)\nwk-last\u1d9c\u1da0 (input l p) = input (there l) \u03bb m \u2192 wk-last\u1d9c\u1da0 (p m)\n\nwk-,,\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 \u2192 EndedEnv \u0394\u2081 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\nwk-,,\u1d9c\u1da0 {\u0394\u2081 = \u03b5} p E = p\nwk-,,\u1d9c\u1da0 {\u0394\u2081 = \u0394\u2081 , d \u21a6 P} p (E , e) rewrite Ended-\u2261end e\n = wk-last\u1d9c\u1da0 (wk-,,\u1d9c\u1da0 p E)\n\nmodule _ {d P \u0394\u2080} where\n pre-wk-\u2208 : \u2200 {\u0394\u2081} \u2192 d \u21a6 P \u2208 \u0394\u2081 \u2192 d \u21a6 P \u2208 (\u0394\u2080 ,, \u0394\u2081)\n pre-wk-\u2208 here = here\n pre-wk-\u2208 (there l) = there (pre-wk-\u2208 l)\n\n{-\npre-wk\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 EndedEnv \u0394\u2080 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\npre-wk\u1d9c\u1da0 E\u0394\u2080 end = end {e = {!!}}\npre-wk\u1d9c\u1da0 {\u0394\u2080} {\u0394\u2081} E\u0394\u2080 (output l m p) =\n output (pre-wk-\u2208 l) m (pre-wk\u1d9c\u1da0 {\u0394\u2080} {{!\u0394\u2081!}} E\u0394\u2080 {!!})\npre-wk\u1d9c\u1da0 E\u0394\u2080 (input l p) = {!!}\n-}\n\nfwd-mix\u1d9c\u1da0 : \u2200 {\u0394 c d} P \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u22a2\u1d9c\u1da0 (\u0394 , c \u21a6 P , d \u21a6 dual P)\nfwd-mix\u1d9c\u1da0 end p = wk-last\u1d9c\u1da0 (wk-last\u1d9c\u1da0 p)\nfwd-mix\u1d9c\u1da0 (recv P) p = input (there here) \u03bb m \u2192 output here m (fwd-mix\u1d9c\u1da0 (P m) p)\nfwd-mix\u1d9c\u1da0 (send P) p = input here \u03bb m \u2192 output (there here) m (fwd-mix\u1d9c\u1da0 (P m) p)\n\nfwd\u1d9c\u1da0 : \u2200 c d {P} \u2192 \u22a2\u1d9c\u1da0 (\u03b5 , c \u21a6 P , d \u21a6 dual P)\nfwd\u1d9c\u1da0 _ _ {P} = fwd-mix\u1d9c\u1da0 {\u03b5} P end\n\n\u03b5,, : \u2200 \u0394 \u2192 \u03b5 ,, \u0394 \u2261 \u0394\n\u03b5,, \u03b5 = refl\n\u03b5,, (\u0394 , d \u21a6 P) rewrite \u03b5,, \u0394 = refl\n\npostulate\n exch\u1d9c\u1da0 :\n \u2200 \u0394\u2080 \u0394\u2081 \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394\u2081 ,, \u0394\u2080)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\n {-\nexch\u1d9c\u1da0 \u03b5 \u0394\u2081 p rewrite \u03b5,, \u0394\u2081 = p\nexch\u1d9c\u1da0 \u0394\u2080 \u03b5 p rewrite \u03b5,, \u0394\u2080 = p\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) end = {!!}\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 ._) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) (output here m p) = {!exch\u1d9c\u1da0!}\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) (output (there l) m p) = {!!}\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) (input l p) = {!!}\n-}\n\npre-wk\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 EndedEnv \u0394\u2080 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\npre-wk\u1d9c\u1da0 {\u0394\u2080} {\u0394\u2081} E p = exch\u1d9c\u1da0 \u0394\u2080 \u0394\u2081 (wk-,,\u1d9c\u1da0 p E)\n\nend-of : Env \u2192 Env\nend-of \u03b5 = \u03b5\nend-of (\u0394 , d \u21a6 P) = end-of \u0394 , d \u21a6 end\n\nend-of-Ended : \u2200 \u0394 \u2192 EndedEnv (end-of \u0394)\nend-of-Ended \u03b5 = _\nend-of-Ended (\u0394 , d \u21a6 P) = end-of-Ended \u0394 , _\n\nend-of-\u22ce : \u2200 \u0394 \u2192 [ \u0394 is \u0394 \u22ce end-of \u0394 ]\nend-of-\u22ce \u03b5 = \u03b5\nend-of-\u22ce (\u0394 , d \u21a6 P) = end-of-\u22ce \u0394 , d \u21a6\u2080 P\n\nend-of-,,-\u22ce : \u2200 \u0394\u2080 \u0394\u2081 \u2192 [ \u0394\u2080 ,, \u0394\u2081 is \u0394\u2080 ,, end-of \u0394\u2081 \u22ce end-of \u0394\u2080 ,, \u0394\u2081 ]\nend-of-,,-\u22ce \u0394\u2080 \u03b5 = end-of-\u22ce \u0394\u2080\nend-of-,,-\u22ce \u0394\u2080 (\u0394\u2081 , d \u21a6 P) = end-of-,,-\u22ce \u0394\u2080 \u0394\u2081 , d \u21a6\u2081 P\n\n,,-assoc : \u2200 {\u0394\u2080 \u0394\u2081 \u0394\u2082} \u2192 \u0394\u2080 ,, (\u0394\u2081 ,, \u0394\u2082) \u2261 (\u0394\u2080 ,, \u0394\u2081) ,, \u0394\u2082\n,,-assoc {\u0394\u2082 = \u03b5} = refl\n,,-assoc {\u0394\u2080} {\u0394\u2081} {\u0394\u2082 , d \u21a6 P} rewrite ,,-assoc {\u0394\u2080} {\u0394\u2081} {\u0394\u2082} = refl\n\ncut,,\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} d P\n (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 dual P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P))\n ---------------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\ncut,,\u1d9c\u1da0 {\u0394\u2080}{\u0394\u2081} d P p q =\n end-last\u1d9c\u1da0\n (cut\u1d9c\u1da0 \u0394\u209b d P\n (exch\u1d9c\u1da0 (\u0394\u2080 ,, end-of \u0394\u2081) (\u03b5 , d \u21a6 dual P)\n (tr \u22a2\u1d9c\u1da0_ (! (,,-assoc {\u03b5 , d \u21a6 dual P} {\u0394\u2080} {end-of \u0394\u2081}))\n (wk-,,\u1d9c\u1da0\n (exch\u1d9c\u1da0 (\u03b5 , d \u21a6 dual P) _ p) (end-of-Ended _))))\n (pre-wk\u1d9c\u1da0 (end-of-Ended _) q))\n where \u0394\u209b = end-of-,,-\u22ce \u0394\u2080 \u0394\u2081\n\npostulate\n !cut,,\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} d P\n (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 dual P))\n ---------------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\n-- !cut,,\u1d9c\u1da0 {\u0394\u2080}{\u0394\u2081} d P p q = {!!}\n\n-- only the last two are exchanged, some more has to be done\nexch-last\u1d9c\u1da0 :\n \u2200 {\u0394 c d P Q} \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394 , c \u21a6 P , d \u21a6 Q)\n \u2192 \u22a2\u1d9c\u1da0 \u0394 , d \u21a6 Q , c \u21a6 P\nexch-last\u1d9c\u1da0 (end {e = (a , b) , c}) = end {e = (a , c) , b}\nexch-last\u1d9c\u1da0 (output here m p) = output (there here) m (exch-last\u1d9c\u1da0 p)\nexch-last\u1d9c\u1da0 (output (there here) m p) = output here m (exch-last\u1d9c\u1da0 p)\nexch-last\u1d9c\u1da0 (output (there (there l)) m p) = output (there (there l)) m (exch-last\u1d9c\u1da0 p)\nexch-last\u1d9c\u1da0 (input here p) = input (there here) \u03bb m \u2192 exch-last\u1d9c\u1da0 (p m)\nexch-last\u1d9c\u1da0 (input (there here) p) = input here \u03bb m \u2192 exch-last\u1d9c\u1da0 (p m)\nexch-last\u1d9c\u1da0 (input (there (there l)) p) = input (there (there l)) \u03bb m \u2192 exch-last\u1d9c\u1da0 (p m)\n\n{-\ndata Relabel : Env \u2192 Env \u2192 Set where\n \u03b5 : Relabel \u03b5 \u03b5\n _,_\u21a6_ : \u2200 {\u0394\u2080 \u0394\u2081 c d P} \u2192 Relabel \u0394\u2080 \u0394\u2081 \u2192 Relabel (\u0394\u2080 , c \u21a6 P) (\u0394\u2081 , d \u21a6 P)\n\nmodule _ where\n rebalel-\u2208 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 Relabel \u0394\u2080 \u0394\u2081\n (l : d \u21a6 P \u2208 \u0394\u2080)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081\n relabel : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 Relabel \u0394\u2080 \u0394\u2081\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081\n relabel end = {!end!}\n relabel (output l m p) = output {!l!} {!!} {!!}\n relabel (input l p) = {!!}\n\npar\u1d9c\u1da0 : \u2200 {\u0394 c} P Q\n (p : \u22a2\u1d9c\u1da0 (\u0394 , c \u21a6 P , c \u21a6 Q))\n \u2192 \u22a2\u1d9c\u1da0 (\u0394 , c \u21a6 (P \u214b' Q))\n-- TODO only one channel name!!!\n-- TODO empty context\n-- TODO try to match on 'p' first\nbroken-par\u1d9c\u1da0 : \u2200 {c d e} P Q\n (p : \u22a2\u1d9c\u1da0 (\u03b5 , c \u21a6 P , d \u21a6 Q))\n \u2192 \u22a2\u1d9c\u1da0 (\u03b5 , e \u21a6 (P \u214b' Q))\nbroken-par\u1d9c\u1da0 end Q p = {!end-last\u1d9c\u1da0 (exch-last\u1d9c\u1da0 p)!}\nbroken-par\u1d9c\u1da0 (com x P) end p = end-last\u1d9c\u1da0 {!p!}\nbroken-par\u1d9c\u1da0 (com x P) (com y Q) (end {e = _ , ()})\n\nbroken-par\u1d9c\u1da0 (com x P) (com .OUT Q) (output here m p)\n = output here R (output here m (broken-par\u1d9c\u1da0 _ _ p))\nbroken-par\u1d9c\u1da0 (com x P) (com .IN Q) (input here p)\n = output here R (input here \u03bb m \u2192 broken-par\u1d9c\u1da0 _ _ (p m))\n\nbroken-par\u1d9c\u1da0 (com .OUT P) (com y Q) (output (there here) m p)\n = output here L (output here m (broken-par\u1d9c\u1da0 _ _ p))\nbroken-par\u1d9c\u1da0 (com .IN P) (com y Q) (input (there here) p)\n = output here L (input here \u03bb m \u2192 broken-par\u1d9c\u1da0 _ _ (p m))\n\nbroken-par\u1d9c\u1da0 (com x P) (com y Q) (output (there (there ())) m p)\nbroken-par\u1d9c\u1da0 (com x P) (com y Q) (input (there (there ())) p)\n-}\n\nmodule _ {c d cd} where\n bi-fwd : \u2200 P Q \u2192 \u22a2\u1d9c\u1da0 (\u03b5 , cd \u21a6 P \u2297 Q , c \u21a6 dual P , d \u21a6 dual Q)\n\n private\n module _ {M} {{_ : SER M}} {P : M \u2192 Proto} {Q} where\n goL : \u2200 x \u2192 \u22a2\u1d9c\u1da0 \u03b5 , cd \u21a6 com x (\u03bb m \u2192 P m \u2297 Q)\n , c \u21a6 dual (com x P)\n , d \u21a6 dual Q\n\n goL IN = input (there (there here)) \u03bb m \u2192 output (there here) m (bi-fwd _ _)\n goL OUT = input (there here) \u03bb m \u2192 output (there (there here)) m (bi-fwd _ _)\n\n goR : \u2200 x \u2192 \u22a2\u1d9c\u1da0 \u03b5 , cd \u21a6 com x (\u03bb m \u2192 Q \u2297 P m)\n , c \u21a6 dual Q\n , d \u21a6 dual (com x P)\n goR IN = input (there (there here)) \u03bb m \u2192 output here m (bi-fwd _ _)\n goR OUT = input here \u03bb m \u2192 output (there (there here)) m (bi-fwd _ _)\n\n bi-fwd end Q = exch-last\u1d9c\u1da0 (wk-last\u1d9c\u1da0 (fwd\u1d9c\u1da0 _ _))\n bi-fwd (com x P) end = wk-last\u1d9c\u1da0 (fwd\u1d9c\u1da0 _ _)\n bi-fwd (com x P) (com y Q) = input (there (there here)) [L: goL x ,R: goR y ]\n\n module _ {\u0394\u2080 \u0394\u2081 P Q} where\n \u2297\u1d9c\u1da0 : (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , c \u21a6 P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 Q))\n ----------------------------------\n \u2192 \u22a2\u1d9c\u1da0 (\u0394\u2080 ,, \u0394\u2081 , cd \u21a6 (P \u2297 Q))\n \u2297\u1d9c\u1da0 p q = !cut,,\u1d9c\u1da0 _ _ p (!cut,,\u1d9c\u1da0 _ _ q (bi-fwd P Q))\n\n {-\nexch\u1d9c\u1da0 :\n \u2200 {\u0394 c d P Q} \u2192\n (l : c \u21a6 P \u2208 \u0394)\n (p : \u22a2\u1d9c\u1da0 \u0394 , d \u21a6 Q)\n \u2192 \u22a2\u1d9c\u1da0 \u0394 [ l \u2254 d \u21a6 Q ] , c \u21a6 P\nexch\u1d9c\u1da0 here p = exch-last\u1d9c\u1da0 p\nexch\u1d9c\u1da0 (there l) p = {!!}\n-}\n\n{-\nrot\u1d9c\u1da0 : \u2200 \u0394 {c P} \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394 , c \u21a6 P)\n \u2192 \u22a2\u1d9c\u1da0 \u03b5 , c \u21a6 P ,, \u0394\nrot\u1d9c\u1da0 \u03b5 p = p\nrot\u1d9c\u1da0 (\u0394 , d \u21a6 P) p = {!rot\u1d9c\u1da0 \u0394 p!}\n\nexch\u1d9c\u1da0 :\n \u2200 {\u0394\u2080} \u0394\u2081 {c d P Q} \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080 , c \u21a6 P , d \u21a6 Q ,, \u0394\u2081)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 , d \u21a6 Q , c \u21a6 P ,, \u0394\u2081\nexch\u1d9c\u1da0 \u03b5 p = exch-last\u1d9c\u1da0 p\nexch\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P) (end e) = end {!!}\nexch\u1d9c\u1da0 (\u0394\u2081 , d\u2081 \u21a6 ._) (output here m p) = output here m ({!exch\u1d9c\u1da0 \u0394\u2081 p!})\nexch\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P) (output (there l) m p) = {!!}\nexch\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P) (input l p) = {!!}\n-}\n\n_\u2286_ : (\u0394\u2080 \u0394\u2081 : Env) \u2192 Set\u2081\n_\u2287_ : (\u0394\u2080 \u0394\u2081 : Env) \u2192 Set\u2081\n\n\u0394\u2080 \u2286 \u0394\u2081 = \u2200 {d P} \u2192 d \u21a6 P \u2208 \u0394\u2080 \u2192 d \u21a6 P \u2208 \u0394\u2081\n\u0394\u2080 \u2287 \u0394\u2081 = \u0394\u2081 \u2286 \u0394\u2080\n\nget-put : \u2200 {d P \u0394 c Q} \u2192\n (l : d \u21a6 P \u2208 \u0394) \u2192 c \u21a6 Q \u2208 (\u0394 [ l \u2254 c \u21a6 Q ])\nget-put here = here\nget-put (there l) = there (get-put l)\n\n{-\n\u2286_[_\u2254_\u21a6_] : \u2200 {\u0394\u2080 \u0394\u2081} (f : \u0394\u2080 \u2286 \u0394\u2081)\n {d P} (l : d \u21a6 P \u2208 \u0394\u2080) (c : URI) (Q : Proto)\n \u2192 (\u0394\u2080 [ l \u2254 c \u21a6 Q ]) \u2286 (\u0394\u2081 [ f l \u2254 c \u21a6 Q ])\n\u2286 f [ l \u2254 c \u21a6 Q ] {d'} {P'} l' = {!!}\n\n(l : d \u21a6 P \u2208 \u0394)\n\u2192 \u0394 [ l \u2254 ]\n\nrecord _\u2248_ (\u0394\u2080 \u0394\u2081 : Env) : Set\u2081 where\n constructor _,_\n field\n \u2248\u2286 : \u0394\u2080 \u2286 \u0394\u2081\n \u2248\u2287 : \u0394\u2080 \u2287 \u0394\u2081\nopen _\u2248_ public\n\n\u2248_[_\u2254_\u21a6_] : \u2200 {\u0394\u2080 \u0394\u2081} (\u0394\u2091 : \u0394\u2080 \u2248 \u0394\u2081)\n {d P} (l : d \u21a6 P \u2208 \u0394\u2080) (c : URI) (Q : Proto)\n \u2192 (\u0394\u2080 [ l \u2254 c \u21a6 Q ]) \u2248 (\u0394\u2081 [ \u2248\u2286 \u0394\u2091 l \u2254 m ])\n\u2248 \u0394\u2091 [ here \u2254 m ] = {!!}\n\u2248 \u0394\u2091 [ there l \u2254 m ] = {!!}\n\n{-(\u03bb l' \u2192 {!\u2248\u2286 \u0394\u2091!}) , from\n where\n from : \u2200 {\u0394\u2080 \u0394\u2081 d io M} {P : M \u2192 Proto} {ser : SER M}\n {\u0394\u2091 : \u0394\u2080 \u2248 \u0394\u2081} {l : d \u21a6 com io P \u2208 \u0394\u2080} {m : M} {d\u2081} {P\u2081} \u2192\n d\u2081 \u21a6 P\u2081 \u2208 (\u0394\u2081 [ \u2248\u2286 \u0394\u2091 l \u2254 m ]) \u2192 d\u2081 \u21a6 P\u2081 \u2208 (\u0394\u2080 [ l \u2254 m ])\n from = {!!}\n\n\u2248\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081}\n (\u0394\u2091 : \u0394\u2080 \u2248 \u0394\u2081)\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080)\n -----------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081\n\u2248\u1d9c\u1da0 \u0394\u2091 (end {e = e}) = end {e = {!!}}\n\u2248\u1d9c\u1da0 \u0394\u2091 (output l m p) = output (\u2248\u2286 \u0394\u2091 l) m (\u2248\u1d9c\u1da0 (\u2248 \u0394\u2091 [ l \u2254 m ]) p)\n\u2248\u1d9c\u1da0 \u0394\u2091 (input l p) = {!!}\n-}\n-}\n\ncut-elim : \u2200 {\u0394} (p : \u22a2 \u0394)\n ------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\ncut-elim (end {e = e}) = end {e = e}\ncut-elim (output l m p) = output l m (cut-elim p)\ncut-elim (input l p) = input l (\u03bb m \u2192 cut-elim (p m))\ncut-elim (mix \u0394\u209b p q) = mix\u1d9c\u1da0 \u0394\u209b (cut-elim p) (cut-elim q)\ncut-elim (cut \u0394\u209b {P} {d} p q) = end-last\u1d9c\u1da0 (cut\u1d9c\u1da0 \u0394\u209b d P (cut-elim p) (cut-elim q))\ncut-elim (end-last p) = end-last\u1d9c\u1da0 (cut-elim p)\ncut-elim (wk-last p) = wk-last\u1d9c\u1da0 (cut-elim p)\ncut-elim (fwd c d) = fwd\u1d9c\u1da0 c d\ncut-elim (exch-last p) = exch-last\u1d9c\u1da0 (cut-elim p)\n\n{-\n\nstart : \u2200 {\u0394} P\n \u2192 \u22a2 [ clientURI \u21a6 dual P ]\n \u2192 (\u2200 d \u2192 \u22a2 (\u0394 , d \u21a6 P))\n \u2192 \u22a2 \u0394\nstart P p q = cut {!!} (... p) (q {!!})\n-}\n\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 : \u2200 {P d} \u2192 \u22a2\u1d9c\u1da0 [ d \u21a6 P ] \u2192 \u27e6 P \u27e7\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 {end} end = _\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 {com x P} (end {e = _ , ()})\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (output here m der) = m , \u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 der\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (output (there ()) m der)\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (input here x\u2081) m = \u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (x\u2081 m)\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (input (there ()) x\u2081)\n\nSatisfy : \u2200 {p d} P \u2192 (R : \u27e6 log P \u27e7 \u2192 Set p) \u2192 \u22a2 [ d \u21a6 P ] \u2192 Set p\nSatisfy P Rel d = (d' : \u27e6 dual P \u27e7) \u2192 Rel (telecom P (\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (cut-elim d)) d')\n\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 : \u2200 {P d} \u2192 \u27e6 P \u27e7 \u2192 \u22a2\u1d9c\u1da0 [ d \u21a6 P ]\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 {end} p = end\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 {recv P} p = input here (\u03bb m \u2192 \u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 (p m))\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 {send P} (m , p) = output here m (\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 p)\n\n\u27e6\u27e7\u2192\u22a2 : \u2200 {P d} \u2192 \u27e6 P \u27e7 \u2192 \u22a2 [ d \u21a6 P ]\n\u27e6\u27e7\u2192\u22a2 {end} p = end\n\u27e6\u27e7\u2192\u22a2 {recv P} p = input here (\u03bb m \u2192 \u27e6\u27e7\u2192\u22a2 (p m))\n\u27e6\u27e7\u2192\u22a2 {send P} (m , p) = output here m (\u27e6\u27e7\u2192\u22a2 p)\n\n{-\n\u22a2toProc : \u2200 {\u0394} \u2192 \u22a2 \u0394 \u2192 JSProc\n\u22a2toProc end = end\n\u22a2toProc (output {d = d} l m prg) = output d (serialize m) (\u22a2toProc prg)\n\u22a2toProc (input {d = d} l prg) = input d ([succeed: (\u03bb m \u2192 \u22a2toProc (prg m)) ,fail: error ] \u2218 parse)\n\u22a2toProc (start P prg x) = start (\u22a2toProc prg) (\u03bb d \u2192 \u22a2toProc (x d))\n\n\n\u22a2toProc-WT : \u2200 {\u0394} (der : \u22a2 \u0394) \u2192 \u0394 \u22a2 \u22a2toProc der\n\u22a2toProc-WT (end {x}) = end {_} {x}\n\u22a2toProc-WT (output {{x}} l m der) = output l (sym (rinv m)) (\u22a2toProc-WT der)\n\u22a2toProc-WT (input {{x}} l x\u2081) = input l \u03bb s m x \u2192\n subst (\u03bb X \u2192 _ [ l \u2254 m ] \u22a2 [succeed: (\u22a2toProc \u2218 x\u2081) ,fail: error ] X) x (\u22a2toProc-WT (x\u2081 m))\n\u22a2toProc-WT (start P der x) = start P (\u22a2toProc-WT der) (\u03bb d \u2192 \u22a2toProc-WT (x d))\n-}\n\n\u27e6_\u27e7E : Env \u2192 Set\n\u27e6 \u03b5 \u27e7E = \ud835\udfd9\n\u27e6 \u0394 , d \u21a6 P \u27e7E = \u27e6 \u0394 \u27e7E \u00d7 \u27e6 P \u27e7\n\nmapEnv : (Proto \u2192 Proto) \u2192 Env \u2192 Env\nmapEnv f \u03b5 = \u03b5\nmapEnv f (\u0394 , d \u21a6 P) = mapEnv f \u0394 , d \u21a6 f P\n\nmapEnv-all : \u2200 {P Q : URI \u2192 Proto \u2192 Set}{\u0394}{f : Proto \u2192 Proto}\n \u2192 (\u2200 d x \u2192 P d x \u2192 Q d (f x))\n \u2192 AllEnv P \u0394 \u2192 AllEnv Q (mapEnv f \u0394)\nmapEnv-all {\u0394 = \u03b5} f\u2081 \u2200\u0394 = \u2200\u0394\nmapEnv-all {\u0394 = \u0394 , d \u21a6 P\u2081} f\u2081 (\u2200\u0394 , P) = (mapEnv-all f\u2081 \u2200\u0394) , f\u2081 d P\u2081 P\n\nmapEnv-Ended : \u2200 {f : Proto \u2192 Proto}{\u0394} \u2192 Ended (f end)\n \u2192 AllEnv (\u03bb _ \u2192 Ended) \u0394 \u2192 AllEnv (\u03bb _ \u2192 Ended) (mapEnv f \u0394)\nmapEnv-Ended eq = mapEnv-all (\u03bb { d end _ \u2192 eq ; d (send P) () ; d (recv P) () })\n\nmapEnv-\u2208 : \u2200 {d P f \u0394} \u2192 d \u21a6 P \u2208 \u0394 \u2192 d \u21a6 f P \u2208 mapEnv f \u0394\nmapEnv-\u2208 here = here\nmapEnv-\u2208 (there der) = there (mapEnv-\u2208 der)\n\nmodule _ {d c M cf}{m : M}{{_ : M \u2243? SERIAL}}{p} where\n subst-lemma-one-point-four : \u2200 {\u0394}( l : d \u21a6 com c p \u2208 \u0394 ) \u2192\n let f = mapProto cf\n in (mapEnv f (\u0394 [ l \u2254 m ])) \u2261 (_[_\u2254_]{c = cf c} (mapEnv f \u0394) (mapEnv-\u2208 l) m)\n subst-lemma-one-point-four here = refl\n subst-lemma-one-point-four (there {d' = d'}{P'} l) = ap (\u03bb X \u2192 X , d' \u21a6 mapProto cf P') (subst-lemma-one-point-four l)\n\nmodule _ {d P} where\n project : \u2200 {\u0394} \u2192 d \u21a6 P \u2208 \u0394 \u2192 \u27e6 \u0394 \u27e7E \u2192 \u27e6 P \u27e7\n project here env = snd env\n project (there mem) env = project mem (fst env)\n\nempty : \u2200 {\u0394} \u2192 AllEnv (\u03bb _ \u2192 Ended) \u0394 \u2192 \u27e6 \u0394 \u27e7E\nempty {\u03b5} <> = _\nempty {\u0394 , d \u21a6 end} (fst , <>) = empty fst , _\nempty {\u0394 , d \u21a6 com x P} (fst , ())\n\nnoRecvInLog : \u2200 {d M}{{_ : M \u2243? SERIAL}}{P : M \u2192 _}{\u0394} \u2192 d \u21a6 recv P \u2208 mapEnv log \u0394 \u2192 \ud835\udfd8\nnoRecvInLog {\u0394 = \u03b5} ()\nnoRecvInLog {\u0394 = \u0394 , d\u2081 \u21a6 end} (there l) = noRecvInLog l\nnoRecvInLog {\u0394 = \u0394 , d\u2081 \u21a6 com x\u2081 P\u2081} (there l) = noRecvInLog l\n\nmodule _ {d M P}{{_ : M \u2243? SERIAL}} where\n lookup : \u2200 {\u0394}(l : d \u21a6 send P \u2208 \u0394) \u2192 \u27e6 \u0394 \u27e7E \u2192 \u03a3 M \u03bb m \u2192 \u27e6 \u0394 [ l \u2254 m ] \u27e7E\n lookup here (env , (m , p)) = m , (env , p)\n lookup (there l) (env , P') = let (m , env') = lookup l env in m , (env' , P')\n\n set : \u2200 {\u0394}(l : d \u21a6 recv P \u2208 \u0394) \u2192 \u27e6 \u0394 \u27e7E \u2192 (m : M) \u2192 \u27e6 \u0394 [ l \u2254 m ] \u27e7E\n set here (env , f) m = env , f m\n set (there l) (env , P') m = set l env m , P'\n\n set\u03a3 : \u2200 {\u0394}(l : d \u21a6 send P \u2208 \u0394) \u2192 (m : M) \u2192 \u27e6 \u0394 [ l \u2254 m ] \u27e7E \u2192 \u27e6 \u0394 \u27e7E\n set\u03a3 here m env = fst env , (m , snd env)\n set\u03a3 (there l) m env = set\u03a3 l m (fst env) , snd env\n\n {-\nforgetConc : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 mapEnv log \u0394 \u2192 \u27e6 mapEnv log \u0394 \u27e7E\nforgetConc (end e) = empty \u2026\nforgetConc {\u0394} (output l m der) = set\u03a3 l m (forgetConc {{!set\u03a3 l m!}} der) -- (forgetConc der)\nforgetConc (input l x\u2081) with noRecvInLog l\n... | ()\n-}\n\n\u22a2telecom : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u27e6 mapEnv dual \u0394 \u27e7E \u2192 \u22a2 mapEnv log \u0394\n\u22a2telecom end env = end {e = mapEnv-Ended _ \u2026}\n\u22a2telecom (output l m der) env = output (mapEnv-\u2208 l) m (subst (\u22a2_)\n (subst-lemma-one-point-four l) (\u22a2telecom der (subst \u27e6_\u27e7E (sym (subst-lemma-one-point-four l)) (set (mapEnv-\u2208 l) env m))))\n\u22a2telecom (input l x\u2081) env = let (m , env') = lookup (mapEnv-\u2208 l) env\n hyp = \u22a2telecom (x\u2081 m) (subst (\u27e6_\u27e7E) (sym (subst-lemma-one-point-four l)) env')\n in output (mapEnv-\u2208 l) m\n (subst (\u22a2_) (subst-lemma-one-point-four l) hyp)\n\n-- old version\n{-\ncut\u1d9c\u1da0 : \u2200 {\u0394 d P} \u2192 \u27e6 dual P \u27e7 \u2192 \u22a2\u1d9c\u1da0 \u0394 , d \u21a6 P \u2192 \u22a2\u1d9c\u1da0 \u0394\ncut\u1d9c\u1da0 D (end {allEnded = \u0394E , PE }) = end {allEnded = \u0394E}\ncut\u1d9c\u1da0 D (output here m E) = cut\u1d9c\u1da0 (D m) E\ncut\u1d9c\u1da0 D (output (there l) m E) = output l m (cut\u1d9c\u1da0 D E)\ncut\u1d9c\u1da0 (m , D) (input here x\u2081) = cut\u1d9c\u1da0 D (x\u2081 m)\ncut\u1d9c\u1da0 D (input (there l) x\u2081) = input l (\u03bb m \u2192 cut\u1d9c\u1da0 D (x\u2081 m))\n\ncut : \u2200 {\u0394 \u0394' \u0393 \u0393' d P} \u2192 \u22a2 \u0394 , clientURI \u21a6 dual P +++ \u0394' \u2192 \u22a2 \u0393 , d \u21a6 P +++ \u0393' \u2192 \u22a2 (\u0394 +++ \u0394') +++ (\u0393 +++ \u0393')\ncut D E = {!!}\n-}\n\n\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"c797d7eb5e42bb8c41377fd0cda132082dd7438a","subject":"Desc stratified model: IDescl implicit.","message":"Desc stratified model: IDescl implicit.","repos":"kwangkim\/pigment,brixen\/Epigram,kwangkim\/pigment,kwangkim\/pigment","old_file":"models\/IDesc.agda","new_file":"models\/IDesc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l : Level}{A B : Set l}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l : Level}{A B C : Set l}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {l : Level}{A B : Set l}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set l) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set l} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set l}(R : I -> IDesc I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set l}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set l -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const (lift I)\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : {l : Level}(I : Set l) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : {l : Level}(I : Set l) -> Set (suc l)\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst {l = suc x}\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl I\nconstl x I X = con (lc , X)\n where lc : DescDConst {l = suc x}\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl I) -> IDescl I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst {l = suc x}\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl I) -> IDescl I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst {l = suc x}\n lp = lpi\n Tl : Lifted S -> IDescl I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl I) -> IDescl I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst {l = suc x}\n ls = lsigma\n Tl : Lifted S -> IDescl I\n Tl (lifter s) = T s\n \n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\n-- desc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\n-- descD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\n-- IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l\n\n-- cases : (l : Level)\n-- (I : Set l)\n-- (xs : desc (suc l) (lift I) (descD l I) (IMu (suc l) Unit (\\ _ -> descD l I)))\n-- (hs : desc l I (box l I (descD l I) (IMu l Unit (\u03bb _ -> descD l I)) xs) (\u03bb _ -> IDesc (suc l) I)) ->\n-- IDesc (suc l) I\n-- cases x I ( lvar , i ) hs = var i\n-- cases x I ( lconst , X ) hs = const X\n-- cases x I ( lprod , (D , D') ) ( d , d' ) = prod d d'\n-- cases x I ( lpi , ( S , T ) ) hs = pi S hs\n-- cases x I ( lsigma , ( S , T ) ) hs = sigma S hs\n\n-- phi : {I : Set} -> IDescl I -> IDesc I\n-- phi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l : Level}{A B : Set l}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l : Level}{A B C : Set l}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {l : Level}{A B : Set l}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set l) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set l} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set l}(R : I -> IDesc I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set l}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set l -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const (lift I)\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : {l : Level}(I : Set l) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (\\_ -> descD I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst {l = suc x}\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst {l = suc x}\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst {l = suc x}\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst {l = suc x}\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst {l = suc x}\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n \n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\n-- desc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\n-- descD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\n-- IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l\n\n-- cases : (l : Level)\n-- (I : Set l)\n-- (xs : desc (suc l) (lift I) (descD l I) (IMu (suc l) Unit (\\ _ -> descD l I)))\n-- (hs : desc l I (box l I (descD l I) (IMu l Unit (\u03bb _ -> descD l I)) xs) (\u03bb _ -> IDesc (suc l) I)) ->\n-- IDesc (suc l) I\n-- cases x I ( lvar , i ) hs = var i\n-- cases x I ( lconst , X ) hs = const X\n-- cases x I ( lprod , (D , D') ) ( d , d' ) = prod d d'\n-- cases x I ( lpi , ( S , T ) ) hs = pi S hs\n-- cases x I ( lsigma , ( S , T ) ) hs = sigma S hs\n\n-- phi : {I : Set} -> IDescl I -> IDesc I\n-- phi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7ec8bed818d248b395b4b0b2c2d2f21361f940bc","subject":"Add lemmas I present in thesis","message":"Add lemmas I present in thesis\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/LangOps.agda","new_file":"Thesis\/LangOps.agda","new_contents":"module Thesis.LangOps where\n\nopen import Thesis.Lang\nopen import Thesis.Changes\nopen import Thesis.LangChanges\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\n\noplus\u03c4o : \u2200 {\u0393} \u03c4 \u2192 Term \u0393 (\u03c4 \u21d2 \u0394t \u03c4 \u21d2 \u03c4)\nominus\u03c4o : \u2200 {\u0393} \u03c4 \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394t \u03c4)\n\nonil\u03c4o : \u2200 {\u0393} \u03c4 \u2192 Term \u0393 (\u03c4 \u21d2 \u0394t \u03c4)\nonil\u03c4o \u03c4 = abs (app\u2082 (ominus\u03c4o \u03c4) (var this) (var this))\n\n-- Do NOT try to read this, such terms are write-only. But the behavior is\n-- specified to be oplus\u03c4-equiv and ominus\u03c4-equiv.\noplus\u03c4o (\u03c3 \u21d2 \u03c4) = abs (abs (abs\n (app\u2082 (oplus\u03c4o \u03c4)\n (app (var (that (that this))) (var this))\n (app\u2082 (var (that this)) (var this) (app (onil\u03c4o \u03c3) (var this))))))\noplus\u03c4o unit = abs (abs (const unit))\noplus\u03c4o int = const plus\noplus\u03c4o (pair \u03c3 \u03c4) = abs (abs (app\u2082 (const cons)\n (app\u2082 (oplus\u03c4o \u03c3) (app (const fst) (var (that this))) (app (const fst) (var this)))\n (app\u2082 (oplus\u03c4o \u03c4) (app (const snd) (var (that this))) (app (const snd) (var this)))))\noplus\u03c4o (sum \u03c3 \u03c4) = abs (abs (app\u2083 (const match) (var (that this))\n (abs (app\u2083 (const match) (var (that this))\n (abs (app\u2083 (const match) (var this)\n (abs (app (const linj) (app\u2082 (oplus\u03c4o \u03c3) (var (that (that this))) (var this))))\n (abs (app (const linj) (var (that (that this)))))))\n (abs (var this))))\n (abs (app\u2083 (const match) (var (that this))\n (abs (app\u2083 (const match) (var this)\n (abs (app (const rinj) (var (that (that this)))))\n (abs (app (const rinj) (app\u2082 (oplus\u03c4o \u03c4) (var (that (that this))) (var this))))))\n (abs (var this))))))\n\nominus\u03c4o (\u03c3 \u21d2 \u03c4) = abs (abs (abs (abs (app\u2082 (ominus\u03c4o \u03c4)\n (app (var (that (that (that this)))) (app\u2082 (oplus\u03c4o \u03c3) (var (that this)) (var this)))\n (app (var (that (that this))) (var (that this)))))))\nominus\u03c4o unit = abs (abs (const unit))\nominus\u03c4o int = const minus\nominus\u03c4o (pair \u03c3 \u03c4) = abs (abs (app\u2082 (const cons)\n (app\u2082 (ominus\u03c4o \u03c3) (app (const fst) (var (that this))) (app (const fst) (var this)))\n (app\u2082 (ominus\u03c4o \u03c4) (app (const snd) (var (that this))) (app (const snd) (var this)))))\nominus\u03c4o (sum \u03c3 \u03c4) = abs (abs (app\u2083 (const match) (var (that this))\n (abs (app\u2083 (const match) (var (that this))\n (abs (app (const linj) (app (const linj) (app\u2082 (ominus\u03c4o \u03c3) (var (that this)) (var this)))))\n (abs (app (const rinj) (var (that (that (that this))))))))\n (abs (app\u2083 (const match) (var (that this))\n (abs (app (const rinj) (var (that (that (that this))))))\n (abs (app (const linj) (app (const rinj) (app\u2082 (ominus\u03c4o \u03c4) (var (that this)) (var this)))))))))\n\noplus\u03c4-equiv : \u2200 \u0393 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u03c4 a da \u2192 \u27e6 oplus\u03c4o \u03c4 \u27e7Term \u03c1 a da \u2261 a \u2295 da\nominus\u03c4-equiv : \u2200 \u0393 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u03c4 b a \u2192 \u27e6 ominus\u03c4o \u03c4 \u27e7Term \u03c1 b a \u2261 b \u229d a\n\noplus\u03c4-equiv-ext : \u2200 \u03c4 \u0393 \u2192 \u27e6 oplus\u03c4o {\u0393} \u03c4 \u27e7Term \u2261 \u03bb \u03c1 \u2192 _\u2295_\noplus\u03c4-equiv-ext \u03c4 _ = ext\u00b3 (\u03bb \u03c1 a da \u2192 oplus\u03c4-equiv _ \u03c1 \u03c4 a da)\nominus\u03c4-equiv-ext : \u2200 \u03c4 \u0393 \u2192 \u27e6 ominus\u03c4o {\u0393} \u03c4 \u27e7Term \u2261 \u03bb \u03c1 \u2192 _\u229d_\nominus\u03c4-equiv-ext \u03c4 _ = ext\u00b3 (\u03bb \u03c1 a da \u2192 ominus\u03c4-equiv _ \u03c1 \u03c4 a da)\n\noplus\u03c4-equiv \u0393 \u03c1 (\u03c3 \u21d2 \u03c4) f df = ext (\u03bb a \u2192 lemma a)\n where\n module _ (a : \u27e6 \u03c3 \u27e7Type) where\n \u03c1\u2032 = a \u2022 df \u2022 f \u2022 \u03c1\n \u03c1\u2032\u2032 = a \u2022 \u03c1\u2032\n lemma : \u27e6 oplus\u03c4o \u03c4 \u27e7Term \u03c1\u2032 (f a)\n (df a (\u27e6 ominus\u03c4o \u03c3 \u27e7Term \u03c1\u2032\u2032 a a))\n \u2261 f a \u2295 df a (nil a)\n lemma\n rewrite ominus\u03c4-equiv _ \u03c1\u2032\u2032 \u03c3 a a\n | oplus\u03c4-equiv _ \u03c1\u2032 \u03c4 (f a) (df a (nil a))\n = refl\noplus\u03c4-equiv \u0393 \u03c1 unit tt tt = refl\noplus\u03c4-equiv \u0393 \u03c1 int a da = refl\noplus\u03c4-equiv \u0393 \u03c1 (pair \u03c3 \u03c4) (a , b) (da , db)\n rewrite oplus\u03c4-equiv _ ((da , db) \u2022 (a , b) \u2022 \u03c1) \u03c3 a da\n | oplus\u03c4-equiv _ ((da , db) \u2022 (a , b) \u2022 \u03c1) \u03c4 b db\n = refl\noplus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2081 x) (inj\u2081 (inj\u2081 dx))\n rewrite oplus\u03c4-equiv-ext \u03c3 (\u0394t \u03c3 \u2022 sum (\u0394t \u03c3) (\u0394t \u03c4) \u2022 \u03c3 \u2022 \u0394t (sum \u03c3 \u03c4) \u2022 sum \u03c3 \u03c4 \u2022 \u0393)\n = refl\noplus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2081 x) (inj\u2081 (inj\u2082 dy)) = refl\noplus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2081 x) (inj\u2082 y) = refl\noplus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2082 y) (inj\u2081 (inj\u2081 dx)) = refl\noplus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2082 y) (inj\u2081 (inj\u2082 dy))\n rewrite oplus\u03c4-equiv-ext \u03c4 (\u0394t \u03c4 \u2022 sum (\u0394t \u03c3) (\u0394t \u03c4) \u2022 \u03c4 \u2022 \u0394t (sum \u03c3 \u03c4) \u2022 sum \u03c3 \u03c4 \u2022 \u0393)\n = refl\noplus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2082 y) (inj\u2082 y\u2081) = refl\n\nominus\u03c4-equiv \u0393 \u03c1 (\u03c3 \u21d2 \u03c4) g f = ext (\u03bb a \u2192 ext (lemma a))\n where\n module _ (a : \u27e6 \u03c3 \u27e7Type) (da : Ch\u03c4 \u03c3) where\n \u03c1\u2032 = da \u2022 a \u2022 f \u2022 g \u2022 \u03c1\n lemma : \u27e6 ominus\u03c4o \u03c4 \u27e7Term (da \u2022 a \u2022 f \u2022 g \u2022 \u03c1)\n (g (\u27e6 oplus\u03c4o \u03c3 \u27e7Term (da \u2022 a \u2022 f \u2022 g \u2022 \u03c1) a da)) (f a)\n \u2261 g (a \u2295 da) \u229d f a\n lemma\n rewrite oplus\u03c4-equiv _ \u03c1\u2032 \u03c3 a da\n | ominus\u03c4-equiv _ \u03c1\u2032 \u03c4 (g (a \u2295 da)) (f a) = refl\nominus\u03c4-equiv \u0393 \u03c1 unit tt tt = refl\nominus\u03c4-equiv \u0393 \u03c1 int b a = refl\nominus\u03c4-equiv \u0393 \u03c1 (pair \u03c3 \u03c4) (a2 , b2) (a1 , b1)\n rewrite ominus\u03c4-equiv _ ((a1 , b1) \u2022 (a2 , b2) \u2022 \u03c1) \u03c3 a2 a1\n | ominus\u03c4-equiv _ ((a1 , b1) \u2022 (a2 , b2) \u2022 \u03c1) \u03c4 b2 b1\n = refl\nominus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2081 x) (inj\u2081 x\u2081)\n rewrite ominus\u03c4-equiv-ext \u03c3 (\u03c3 \u2022 \u03c3 \u2022 sum \u03c3 \u03c4 \u2022 sum \u03c3 \u03c4 \u2022 \u0393)\n = refl\nominus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2081 x) (inj\u2082 y) = refl\nominus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2082 y) (inj\u2081 x) = refl\nominus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2082 y) (inj\u2082 y\u2081)\n rewrite ominus\u03c4-equiv-ext \u03c4 (\u03c4 \u2022 \u03c4 \u2022 sum \u03c3 \u03c4 \u2022 sum \u03c3 \u03c4 \u2022 \u0393)\n = refl\n\n-- Proved these lemmas to show them in thesis.\noplus\u03c4-equiv-term : \u2200 d\u0393 (d\u03c1 : \u27e6 d\u0393 \u27e7Context) \u03c4 t dt \u2192\n \u27e6 app\u2082 (oplus\u03c4o \u03c4) t dt \u27e7Term d\u03c1 \u2261 \u27e6 t \u27e7Term d\u03c1 \u2295 \u27e6 dt \u27e7Term d\u03c1\noplus\u03c4-equiv-term d\u0393 d\u03c1 \u03c4 t dt = oplus\u03c4-equiv d\u0393 d\u03c1 \u03c4 (\u27e6 t \u27e7Term d\u03c1) (\u27e6 dt \u27e7Term d\u03c1)\nominus\u03c4-equiv-term : \u2200 \u0393 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u03c4 t2 t1 \u2192\n \u27e6 app\u2082 (ominus\u03c4o \u03c4) t2 t1 \u27e7Term \u03c1 \u2261 \u27e6 t2 \u27e7Term \u03c1 \u229d \u27e6 t1 \u27e7Term \u03c1\nominus\u03c4-equiv-term \u0393 \u03c1 \u03c4 t2 t1 = ominus\u03c4-equiv \u0393 \u03c1 \u03c4 (\u27e6 t2 \u27e7Term \u03c1) (\u27e6 t1 \u27e7Term \u03c1)\n","old_contents":"module Thesis.LangOps where\n\nopen import Thesis.Lang\nopen import Thesis.Changes\nopen import Thesis.LangChanges\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\n\noplus\u03c4o : \u2200 {\u0393} \u03c4 \u2192 Term \u0393 (\u03c4 \u21d2 \u0394t \u03c4 \u21d2 \u03c4)\nominus\u03c4o : \u2200 {\u0393} \u03c4 \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394t \u03c4)\n\nonil\u03c4o : \u2200 {\u0393} \u03c4 \u2192 Term \u0393 (\u03c4 \u21d2 \u0394t \u03c4)\nonil\u03c4o \u03c4 = abs (app\u2082 (ominus\u03c4o \u03c4) (var this) (var this))\n\n-- Do NOT try to read this, such terms are write-only. But the behavior is\n-- specified to be oplus\u03c4-equiv and ominus\u03c4-equiv.\noplus\u03c4o (\u03c3 \u21d2 \u03c4) = abs (abs (abs\n (app\u2082 (oplus\u03c4o \u03c4)\n (app (var (that (that this))) (var this))\n (app\u2082 (var (that this)) (var this) (app (onil\u03c4o \u03c3) (var this))))))\noplus\u03c4o unit = abs (abs (const unit))\noplus\u03c4o int = const plus\noplus\u03c4o (pair \u03c3 \u03c4) = abs (abs (app\u2082 (const cons)\n (app\u2082 (oplus\u03c4o \u03c3) (app (const fst) (var (that this))) (app (const fst) (var this)))\n (app\u2082 (oplus\u03c4o \u03c4) (app (const snd) (var (that this))) (app (const snd) (var this)))))\noplus\u03c4o (sum \u03c3 \u03c4) = abs (abs (app\u2083 (const match) (var (that this))\n (abs (app\u2083 (const match) (var (that this))\n (abs (app\u2083 (const match) (var this)\n (abs (app (const linj) (app\u2082 (oplus\u03c4o \u03c3) (var (that (that this))) (var this))))\n (abs (app (const linj) (var (that (that this)))))))\n (abs (var this))))\n (abs (app\u2083 (const match) (var (that this))\n (abs (app\u2083 (const match) (var this)\n (abs (app (const rinj) (var (that (that this)))))\n (abs (app (const rinj) (app\u2082 (oplus\u03c4o \u03c4) (var (that (that this))) (var this))))))\n (abs (var this))))))\n\nominus\u03c4o (\u03c3 \u21d2 \u03c4) = abs (abs (abs (abs (app\u2082 (ominus\u03c4o \u03c4)\n (app (var (that (that (that this)))) (app\u2082 (oplus\u03c4o \u03c3) (var (that this)) (var this)))\n (app (var (that (that this))) (var (that this)))))))\nominus\u03c4o unit = abs (abs (const unit))\nominus\u03c4o int = const minus\nominus\u03c4o (pair \u03c3 \u03c4) = abs (abs (app\u2082 (const cons)\n (app\u2082 (ominus\u03c4o \u03c3) (app (const fst) (var (that this))) (app (const fst) (var this)))\n (app\u2082 (ominus\u03c4o \u03c4) (app (const snd) (var (that this))) (app (const snd) (var this)))))\nominus\u03c4o (sum \u03c3 \u03c4) = abs (abs (app\u2083 (const match) (var (that this))\n (abs (app\u2083 (const match) (var (that this))\n (abs (app (const linj) (app (const linj) (app\u2082 (ominus\u03c4o \u03c3) (var (that this)) (var this)))))\n (abs (app (const rinj) (var (that (that (that this))))))))\n (abs (app\u2083 (const match) (var (that this))\n (abs (app (const rinj) (var (that (that (that this))))))\n (abs (app (const linj) (app (const rinj) (app\u2082 (ominus\u03c4o \u03c4) (var (that this)) (var this)))))))))\n\noplus\u03c4-equiv : \u2200 \u0393 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u03c4 a da \u2192 \u27e6 oplus\u03c4o \u03c4 \u27e7Term \u03c1 a da \u2261 a \u2295 da\nominus\u03c4-equiv : \u2200 \u0393 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u03c4 b a \u2192 \u27e6 ominus\u03c4o \u03c4 \u27e7Term \u03c1 b a \u2261 b \u229d a\n\noplus\u03c4-equiv-ext : \u2200 \u03c4 \u0393 \u2192 \u27e6 oplus\u03c4o {\u0393} \u03c4 \u27e7Term \u2261 \u03bb \u03c1 \u2192 _\u2295_\noplus\u03c4-equiv-ext \u03c4 _ = ext\u00b3 (\u03bb \u03c1 a da \u2192 oplus\u03c4-equiv _ \u03c1 \u03c4 a da)\nominus\u03c4-equiv-ext : \u2200 \u03c4 \u0393 \u2192 \u27e6 ominus\u03c4o {\u0393} \u03c4 \u27e7Term \u2261 \u03bb \u03c1 \u2192 _\u229d_\nominus\u03c4-equiv-ext \u03c4 _ = ext\u00b3 (\u03bb \u03c1 a da \u2192 ominus\u03c4-equiv _ \u03c1 \u03c4 a da)\n\noplus\u03c4-equiv \u0393 \u03c1 (\u03c3 \u21d2 \u03c4) f df = ext (\u03bb a \u2192 lemma a)\n where\n module _ (a : \u27e6 \u03c3 \u27e7Type) where\n \u03c1\u2032 = a \u2022 df \u2022 f \u2022 \u03c1\n \u03c1\u2032\u2032 = a \u2022 \u03c1\u2032\n lemma : \u27e6 oplus\u03c4o \u03c4 \u27e7Term \u03c1\u2032 (f a)\n (df a (\u27e6 ominus\u03c4o \u03c3 \u27e7Term \u03c1\u2032\u2032 a a))\n \u2261 f a \u2295 df a (nil a)\n lemma\n rewrite ominus\u03c4-equiv _ \u03c1\u2032\u2032 \u03c3 a a\n | oplus\u03c4-equiv _ \u03c1\u2032 \u03c4 (f a) (df a (nil a))\n = refl\noplus\u03c4-equiv \u0393 \u03c1 unit tt tt = refl\noplus\u03c4-equiv \u0393 \u03c1 int a da = refl\noplus\u03c4-equiv \u0393 \u03c1 (pair \u03c3 \u03c4) (a , b) (da , db)\n rewrite oplus\u03c4-equiv _ ((da , db) \u2022 (a , b) \u2022 \u03c1) \u03c3 a da\n | oplus\u03c4-equiv _ ((da , db) \u2022 (a , b) \u2022 \u03c1) \u03c4 b db\n = refl\noplus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2081 x) (inj\u2081 (inj\u2081 dx))\n rewrite oplus\u03c4-equiv-ext \u03c3 (\u0394t \u03c3 \u2022 sum (\u0394t \u03c3) (\u0394t \u03c4) \u2022 \u03c3 \u2022 \u0394t (sum \u03c3 \u03c4) \u2022 sum \u03c3 \u03c4 \u2022 \u0393)\n = refl\noplus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2081 x) (inj\u2081 (inj\u2082 dy)) = refl\noplus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2081 x) (inj\u2082 y) = refl\noplus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2082 y) (inj\u2081 (inj\u2081 dx)) = refl\noplus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2082 y) (inj\u2081 (inj\u2082 dy))\n rewrite oplus\u03c4-equiv-ext \u03c4 (\u0394t \u03c4 \u2022 sum (\u0394t \u03c3) (\u0394t \u03c4) \u2022 \u03c4 \u2022 \u0394t (sum \u03c3 \u03c4) \u2022 sum \u03c3 \u03c4 \u2022 \u0393)\n = refl\noplus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2082 y) (inj\u2082 y\u2081) = refl\n\nominus\u03c4-equiv \u0393 \u03c1 (\u03c3 \u21d2 \u03c4) g f = ext (\u03bb a \u2192 ext (lemma a))\n where\n module _ (a : \u27e6 \u03c3 \u27e7Type) (da : Ch\u03c4 \u03c3) where\n \u03c1\u2032 = da \u2022 a \u2022 f \u2022 g \u2022 \u03c1\n lemma : \u27e6 ominus\u03c4o \u03c4 \u27e7Term (da \u2022 a \u2022 f \u2022 g \u2022 \u03c1)\n (g (\u27e6 oplus\u03c4o \u03c3 \u27e7Term (da \u2022 a \u2022 f \u2022 g \u2022 \u03c1) a da)) (f a)\n \u2261 g (a \u2295 da) \u229d f a\n lemma\n rewrite oplus\u03c4-equiv _ \u03c1\u2032 \u03c3 a da\n | ominus\u03c4-equiv _ \u03c1\u2032 \u03c4 (g (a \u2295 da)) (f a) = refl\nominus\u03c4-equiv \u0393 \u03c1 unit tt tt = refl\nominus\u03c4-equiv \u0393 \u03c1 int b a = refl\nominus\u03c4-equiv \u0393 \u03c1 (pair \u03c3 \u03c4) (a2 , b2) (a1 , b1)\n rewrite ominus\u03c4-equiv _ ((a1 , b1) \u2022 (a2 , b2) \u2022 \u03c1) \u03c3 a2 a1\n | ominus\u03c4-equiv _ ((a1 , b1) \u2022 (a2 , b2) \u2022 \u03c1) \u03c4 b2 b1\n = refl\nominus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2081 x) (inj\u2081 x\u2081)\n rewrite ominus\u03c4-equiv-ext \u03c3 (\u03c3 \u2022 \u03c3 \u2022 sum \u03c3 \u03c4 \u2022 sum \u03c3 \u03c4 \u2022 \u0393)\n = refl\nominus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2081 x) (inj\u2082 y) = refl\nominus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2082 y) (inj\u2081 x) = refl\nominus\u03c4-equiv \u0393 \u03c1 (sum \u03c3 \u03c4) (inj\u2082 y) (inj\u2082 y\u2081)\n rewrite ominus\u03c4-equiv-ext \u03c4 (\u03c4 \u2022 \u03c4 \u2022 sum \u03c3 \u03c4 \u2022 sum \u03c3 \u03c4 \u2022 \u0393)\n = refl\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"965133f11d3863769cb8e636eff326a49bc7321b","subject":"CyclicGroup: rename \ud835\udd3e as \u2124[ p ]\u2605","message":"CyclicGroup: rename \ud835\udd3e as \u2124[ p ]\u2605\n","repos":"crypto-agda\/crypto-agda","old_file":"Crypto\/JS\/BigI\/CyclicGroup.agda","new_file":"Crypto\/JS\/BigI\/CyclicGroup.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Type.Eq\nopen import FFI.JS using (Bool; trace-call; _++_)\nopen import FFI.JS.Check\n renaming (check to check?)\n--renaming (warn-check to check?)\n\nopen import FFI.JS.BigI\nopen import Data.List.Base using (List; foldr)\nopen import Data.Two hiding (_==_)\nopen import Relation.Binary.PropositionalEquality\nopen import Algebra.Raw\nopen import Algebra.Group\n\n-- TODO carry on a primality proof of p\nmodule Crypto.JS.BigI.CyclicGroup (p : BigI) where\n\nabstract\n \u2124[_]\u2605 : Set\n \u2124[_]\u2605 = BigI\n\n private\n \u2124p\u2605 : Set\n \u2124p\u2605 = BigI\n\n mod-p : BigI \u2192 \u2124p\u2605\n mod-p x = mod x p\n\n -- There are two ways to go from BigI to \u2124p\u2605: check and mod-p\n -- Use check for untrusted input data and mod-p for internal\n -- computation.\n BigI\u25b9\u2124[_]\u2605 : BigI \u2192 \u2124p\u2605\n BigI\u25b9\u2124[_]\u2605 = -- trace-call \"BigI\u25b9\u2124[_]\u2605 \"\n \u03bb x \u2192\n (check? (x I 0I)\n (\u03bb _ \u2192 \"Should be strictly positive: \" ++ toString x ++ \" <= 0\") x))\n\n check-non-zero : \u2124p\u2605 \u2192 BigI\n check-non-zero = -- trace-call \"check-non-zero \"\n \u03bb x \u2192 check? (x >I 0I) (\u03bb _ \u2192 \"Should be non zero\") x\n\n repr : \u2124p\u2605 \u2192 BigI\n repr x = x\n\n 1# : \u2124p\u2605\n 1# = 1I\n\n 1\/_ : \u2124p\u2605 \u2192 \u2124p\u2605\n 1\/ x = modInv (check-non-zero x) p\n\n _^_ : \u2124p\u2605 \u2192 BigI \u2192 \u2124p\u2605\n x ^ y = modPow x y p\n\n_*_ _\/_ : \u2124p\u2605 \u2192 \u2124p\u2605 \u2192 \u2124p\u2605\n\nx * y = mod-p (multiply (repr x) (repr y))\nx \/ y = x * 1\/ y\n\ninstance\n \u2124[_]\u2605-Eq? : Eq? \u2124p\u2605\n \u2124[_]\u2605-Eq? = record\n { _==_ = _=='_\n ; \u2261\u21d2== = \u2261\u21d2=='\n ; ==\u21d2\u2261 = ==\u21d2\u2261' }\n where\n _=='_ : \u2124p\u2605 \u2192 \u2124p\u2605 \u2192 \ud835\udfda\n x ==' y = equals (repr x) (repr y)\n postulate\n \u2261\u21d2==' : \u2200 {x y} \u2192 x \u2261 y \u2192 \u2713 (x ==' y)\n ==\u21d2\u2261' : \u2200 {x y} \u2192 \u2713 (x ==' y) \u2192 x \u2261 y\n\nprod : List \u2124p\u2605 \u2192 \u2124p\u2605\nprod = foldr _*_ 1#\n\nmon-ops : Monoid-Ops \u2124p\u2605\nmon-ops = _*_ , 1#\n\ngrp-ops : Group-Ops \u2124p\u2605\ngrp-ops = mon-ops , 1\/_\n\npostulate\n grp-struct : Group-Struct grp-ops\n\ngrp : Group \u2124p\u2605\ngrp = grp-ops , grp-struct\n\nmodule grp = Group grp\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Type.Eq\nopen import FFI.JS using (Bool; trace-call; _++_)\nopen import FFI.JS.Check\n renaming (check to check?)\n--renaming (warn-check to check?)\n\nopen import FFI.JS.BigI\nopen import Data.List.Base using (List; foldr)\nopen import Data.Two hiding (_==_)\nopen import Relation.Binary.PropositionalEquality\nopen import Algebra.Raw\nopen import Algebra.Group\n\n-- TODO carry on a primality proof of p\nmodule Crypto.JS.BigI.CyclicGroup (p : BigI) where\n\nabstract\n -- \u2124p*\n \ud835\udd3e : Set\n \ud835\udd3e = BigI\n\n private\n mod-p : BigI \u2192 \ud835\udd3e\n mod-p x = mod x p\n\n -- There are two ways to go from BigI to \ud835\udd3e: check and mod-p\n -- Use check for untrusted input data and mod-p for internal\n -- computation.\n BigI\u25b9\ud835\udd3e : BigI \u2192 \ud835\udd3e\n BigI\u25b9\ud835\udd3e = -- trace-call \"BigI\u25b9\ud835\udd3e \"\n \u03bb x \u2192\n (check? (x I 0I)\n (\u03bb _ \u2192 \"Should be strictly positive: \" ++ toString x ++ \" <= 0\") x))\n\n check-non-zero : \ud835\udd3e \u2192 BigI\n check-non-zero = -- trace-call \"check-non-zero \"\n \u03bb x \u2192 check? (x >I 0I) (\u03bb _ \u2192 \"Should be non zero\") x\n\n repr : \ud835\udd3e \u2192 BigI\n repr x = x\n\n 1# : \ud835\udd3e\n 1# = 1I\n\n 1\/_ : \ud835\udd3e \u2192 \ud835\udd3e\n 1\/ x = modInv (check-non-zero x) p\n\n _^_ : \ud835\udd3e \u2192 BigI \u2192 \ud835\udd3e\n x ^ y = modPow x y p\n\n_*_ _\/_ : \ud835\udd3e \u2192 \ud835\udd3e \u2192 \ud835\udd3e\n\nx * y = mod-p (multiply (repr x) (repr y))\nx \/ y = x * 1\/ y\n\ninstance\n \ud835\udd3e-Eq? : Eq? \ud835\udd3e\n \ud835\udd3e-Eq? = record\n { _==_ = _=='_\n ; \u2261\u21d2== = \u2261\u21d2=='\n ; ==\u21d2\u2261 = ==\u21d2\u2261' }\n where\n _=='_ : \ud835\udd3e \u2192 \ud835\udd3e \u2192 \ud835\udfda\n x ==' y = equals (repr x) (repr y)\n postulate\n \u2261\u21d2==' : \u2200 {x y} \u2192 x \u2261 y \u2192 \u2713 (x ==' y)\n ==\u21d2\u2261' : \u2200 {x y} \u2192 \u2713 (x ==' y) \u2192 x \u2261 y\n\nprod : List \ud835\udd3e \u2192 \ud835\udd3e\nprod = foldr _*_ 1#\n\nmon-ops : Monoid-Ops \ud835\udd3e\nmon-ops = _*_ , 1#\n\ngrp-ops : Group-Ops \ud835\udd3e\ngrp-ops = mon-ops , 1\/_\n\npostulate\n grp-struct : Group-Struct grp-ops\n\ngrp : Group \ud835\udd3e\ngrp = grp-ops , grp-struct\n\nmodule grp = Group grp\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"fcda492aa46e222289a8d537bd357e0a62b63c45","subject":"Working in a draft about the existential elimination.","message":"Working in a draft about the existential elimination.\n\nIgnore-this: 1d4d9bccdab653e3734d917852116238\n\ndarcs-hash:20120220031726-3bd4e-400bf695e96bc4680bfccab55da676c918628e5d.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/Common\/Data\/Existential.agda","new_file":"Draft\/Common\/Data\/Existential.agda","new_contents":"-----------------------------------------------------------------------------\n-- Existential elimination\n-----------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Tested with the development version of Agda on 19 February 2012.\n\nmodule Existential where\n\n-----------------------------------------------------------------------------\n\npostulate\n D : Set\n\nmodule \u2203\u2081 where\n -- Type theoretical version\n\n -- We add 3 to the fixities of the standard library.\n infixr 7 _,_\n\n -- The existential quantifier type on D.\n data \u2203 (P : D \u2192 Set) : Set where\n _,_ : (x : D) \u2192 P x \u2192 \u2203 P\n\n -- Sugar syntax for the existential quantifier.\n syntax \u2203 (\u03bb x \u2192 e) = \u2203[ x ] e\n\n -- The existential elimination.\n \u2203-proj\u2081 : \u2200 {P} \u2192 \u2203 P \u2192 D\n \u2203-proj\u2081 (x , _) = x\n\n \u2203-proj\u2082 : \u2200 {P} \u2192 (p : \u2203 P) \u2192 P (\u2203-proj\u2081 p)\n \u2203-proj\u2082 (_ , Px) = Px\n\n -- Some examples\n\n -- The order of quantifiers of the same sort is irrelevant.\n \u2203-ord : {P\u00b2 : D \u2192 D \u2192 Set} \u2192 (\u2203[ x ] \u2203[ y ] P\u00b2 x y) \u2192 (\u2203[ y ] \u2203[ x ] P\u00b2 x y)\n \u2203-ord (x , y , h) = y , x , h\n\n-----------------------------------------------------------------------------\n\nmodule \u2203\u2082 where\n -- FOL version\n\n -- We add 3 to the fixities of the standard library.\n infixr 7 _,_\n\n -- The existential quantifier type on D.\n data \u2203 (P : D \u2192 Set) : Set where\n _,_ : (x : D) \u2192 P x \u2192 \u2203 P\n\n -- Sugar syntax for the existential quantifier.\n syntax \u2203 (\u03bb x \u2192 e) = \u2203[ x ] e\n\n -- FOL existential elimination\n -- \u2203x.P(x) P(x) \u2192 Q\n -- ------------------------\n -- Q\n \u2203-elim : {P : D \u2192 Set}{Q : Set} \u2192 \u2203 P \u2192 ((x : D) \u2192 P x \u2192 Q) \u2192 Q\n \u2203-elim (x , p) h = h x p\n\n -- Some examples\n\n -- The order of quantifiers of the same sort is irrelevant.\n \u2203-ord : {P\u00b2 : D \u2192 D \u2192 Set} \u2192 (\u2203[ x ] \u2203[ y ] P\u00b2 x y) \u2192 (\u2203[ y ] \u2203[ x ] P\u00b2 x y)\n \u2203-ord h = \u2203-elim h (\u03bb x h\u2081 \u2192 \u2203-elim h\u2081 (\u03bb y prf \u2192 y , x , prf))\n\n -- A proof non-FOL valid\n non-FOL : {P : D \u2192 Set} \u2192 \u2203 P \u2192 D\n non-FOL h = \u2203-elim h (\u03bb x _ \u2192 x)\n\n-----------------------------------------------------------------------------\n\nmodule \u2203\u2083 where\n -- A different version from the FOL existential introduction\n -- P(x)\n -- ------------\n -- \u2203x.P(x)\n\n -- The existential quantifier type on D.\n data \u2203 (P : D \u2192 Set) : Set where\n \u2203-intro : ((x : D) \u2192 P x) \u2192 \u2203 P\n\n -- Sugar syntax for the existential quantifier.\n syntax \u2203 (\u03bb x \u2192 e) = \u2203[ x ] e\n\n postulate d : D\n\n -- FOL existential elimination.\n -- NB. It is neccesary that D \u2260 \u2205.\n \u2203-elim : {P : D \u2192 Set}{Q : Set} \u2192 \u2203 P \u2192 ((x : D) \u2192 P x \u2192 Q) \u2192 Q\n \u2203-elim (\u2203-intro h\u2081) h\u2082 = h\u2082 d (h\u2081 d)\n\n -- Some examples\n\n -- Impossible\n -- thm : {P : D \u2192 Set} \u2192 \u2203[ x ] P x \u2192 \u2203[ y ] P y\n -- thm h = \u2203-elim h (\u03bb x prf \u2192 {!!})\n","old_contents":"-----------------------------------------------------------------------------\n-- Existential elimination\n-----------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Tested with the development version of Agda on 16 February 2012.\n\nmodule Existential where\n\n-----------------------------------------------------------------------------\n\npostulate\n D : Set\n\nmodule \u2203\u2081 where\n\n -- We add 3 to the fixities of the standard library.\n infixr 7 _,_\n\n -- The existential quantifier type on D.\n data \u2203 (P : D \u2192 Set) : Set where\n _,_ : (x : D) \u2192 P x \u2192 \u2203 P\n\n -- FOL existential elimination\n -- \u2203x.P(x) P(x) \u2192 Q\n -- ------------------------\n -- Q\n \u2203-elim : {P : D \u2192 Set}{Q : Set} \u2192 \u2203 P \u2192 ((x : D) \u2192 P x \u2192 Q) \u2192 Q\n \u2203-elim (x , p) h = h x p\n\n -- Type theory existential elimination.\n \u2203-proj\u2081 : \u2200 {P} \u2192 \u2203 P \u2192 D\n \u2203-proj\u2081 (x , _) = x\n\n \u2203-proj\u2082 : \u2200 {P} \u2192 (p : \u2203 P) \u2192 P (\u2203-proj\u2081 p)\n \u2203-proj\u2082 (_ , Px) = Px\n\n-----------------------------------------------------------------------------\n\nmodule \u2203\u2082 where\n -- A different version from the FOL existential introduction\n -- P(x)\n -- ------------\n -- \u2203x.P(x)\n\n -- The existential quantifier type on D.\n data \u2203 (P : D \u2192 Set) : Set where\n \u2203-intro : ((x : D) \u2192 P x) \u2192 \u2203 P\n\n postulate d : D\n\n -- FOL existential elimination.\n -- NB. It is neccesary that D \u2260 \u2205.\n \u2203-elim : {P : D \u2192 Set}{Q : Set} \u2192 \u2203 P \u2192 ((x : D) \u2192 P x \u2192 Q) \u2192 Q\n \u2203-elim (\u2203-intro h\u2081) h\u2082 = h\u2082 d (h\u2081 d)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"6448a4f7d90d0941e9df46bfb68a3553176b9ee2","subject":"Desc@ICFP: update model, adding just Variable from the thing containing Values.","message":"Desc@ICFP: update model, adding just Variable from the thing containing Values.","repos":"kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram,kwangkim\/pigment","old_file":"models\/IDescTT.agda","new_file":"models\/IDescTT.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n pair : Type -> Type -> Type\n\n--********************************\n-- Typed expressions\n--********************************\n\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\nVal (pair x y) = (Val x) * (Val y)\n\n\n-- Fix menu:\nexprFixMenu : FixMenu Type\nexprFixMenu = ( consE (consE nilE) , \n \\ty -> (const (Val ty), -- Val t\n (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void)))\n\n-- Indexed menu:\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\nchoiceMenu (pair x y) = nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\nchoiceDessert (pair x y) = Void\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\n\n-- Expression:\nexpr : TagIDesc Type\nexpr = exprFixMenu , exprSensitiveMenu\n\nexprIDesc : TagIDesc Type -> (Type -> IDesc Type)\nexprIDesc D = toIDesc Type D\n\n\n--********************************\n-- Closed terms\n--********************************\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : {ty : Type} -> IMu closeTerm ty -> Val ty\neval {ty} term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nEmpty : Type -> Set\nEmpty _ = Zero\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (expr ** Empty)\n\n--********************************\n-- Closed term' evaluation\n--********************************\n\neval' : {ty : Type} -> IMu closeTerm' ty -> Val ty\neval' {ty} term = cata Type closeTerm' Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm' ty |] Val -> Val ty\n evalOneStep _ (EZe , ())\n evalOneStep _ (ESu EZe , t) = t\n evalOneStep _ ((ESu (ESu EZe)) , (true , ( x , _))) = x\n evalOneStep _ ((ESu (ESu EZe)) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu (ESu EZe))) , (x , y)) = plus x y\n evalOneStep nat (((ESu (ESu (ESu (ESu ()))))) , t) \n evalOneStep bool ((ESu (ESu (ESu EZe))) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu (ESu ())))) , _) \n evalOneStep (pair x y) (ESu (ESu (ESu ())) , _)\n\n\n--********************************\n-- Open terms\n--********************************\n\ndata Vec (A : Set) : Nat -> Set where\n vnil : Vec A ze\n vcons : {n : Nat} -> A -> Vec A n -> Vec A (su n)\n\ndata Fin : Nat -> Set where\n fze : {n : Nat} -> Fin (su n)\n fsu : {n : Nat} -> Fin n -> Fin (su n)\n\nContext : Nat -> Set\nContext n = Vec (Sigma Type (\\ty -> IMu closeTerm' ty)) n\n\ntypeAt : {n : Nat}(c : Context n) -> Fin n -> Type\ntypeAt {ze} c ()\ntypeAt {.(su n)} (vcons x xs) (fze {n}) = fst x\ntypeAt {.(su n)} (vcons x xs) (fsu {n} y) = typeAt xs y\n\nlookup : {n : Nat}(c : Context n)(i : Fin n) -> IMu closeTerm' (typeAt c i)\nlookup {ze} c ()\nlookup {su _} (vcons x _) fze = snd x\nlookup {su _} (vcons _ xs) (fsu y) = lookup xs y\n\n\nVar : {n : Nat} -> Context n -> Type -> Set\nVar {n} c ty = Sigma (Fin n) (\\i -> typeAt c i == ty)\n\nopenTerm : {n : Nat} -> Context n -> Type -> IDesc Type\nopenTerm c = toIDesc Type (expr ** (Var c))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : {n : Nat}\n {context : Context n}\n (ty : Type) ->\n Var context ty ->\n IMu closeTerm' ty\ndischarge {n} {c} ty variable = subst (cong (IMu closeTerm') (snd variable)) (lookup c (fst variable))\n\nsubstExpr : {n : Nat}\n {ty : Type}\n (context : Context n)\n (sigma : (ty : Type) ->\n Var context ty ->\n IMu closeTerm' ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm' ty\nsubstExpr {n} {ty} c sig term = \n substI (Var c) Empty expr sig ty term\n\nevalOpen : {n : Nat}{ty : Type}\n (context : Context n) ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen context tm = eval' (substExpr context discharge tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- V 0 :-> true, V 1 :-> 2, V 2 :-> ( false , 1 )\ntestContext : Context _\ntestContext = vcons (bool , con ((ESu EZe) , true )) \n (vcons (nat , con ((ESu EZe) , su (su ze)) ) \n (vcons (pair bool nat , con ((ESu EZe) , ( false , su ze )))\n vnil))\n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , ( fsu fze , refl ) )\n\ntestSubst1 : IMu closeTerm' nat\ntestSubst1 = substExpr testContext \n discharge \n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con ((ESu (ESu (ESu EZe))) , (con ((ESu EZe) , (su ze)) , con ( EZe , (fsu fze , refl) )) )\n\ntestSubst2 : IMu closeTerm' nat\ntestSubst2 = substExpr testContext \n discharge \n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext test2\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu (ESu EZe) , (con (EZe , (fze , refl)) ,\n (con (EZe , (fsu fze , refl)) ,\n con (ESu EZe , ze))))\n\ntestSubst3 : IMu closeTerm' nat\ntestSubst3 = substExpr testContext \n discharge \n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext test3\n\n-- V 2\ntest4 : IMu (openTerm testContext) (pair bool nat)\ntest4 = con (EZe , ( fsu (fsu fze) , refl ) )\n\ntestSubst4 : IMu closeTerm' (pair bool nat)\ntestSubst4 = substExpr testContext \n discharge \n test4\n-- = (false , 1)\ntestEval4 : Val (pair bool nat)\ntestEval4 = evalOpen testContext test4\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n pair : Type -> Type -> Type\n\n--********************************\n-- Typed expressions\n--********************************\n\n-- Fix menu:\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty), -- Val t\n (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void)))\n\n-- Indexed menu:\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\nchoiceMenu (pair x y) = nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\nchoiceDessert (pair x y) = Void\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\n\n-- Expression:\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\nVal (pair x y) = (Val x) * (Val y)\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), \n Void))\n\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\nchoiceFreeMenu (pair x y) = nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void)\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void )\nchoiceFreeDessert (pair x y) = Void\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (exprFree ** Val)\n\n--********************************\n-- Closed term' evaluation\n--********************************\n\neval' : {ty : Type} -> IMu closeTerm' ty -> Val ty\neval' {ty} term = cata Type closeTerm' Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm' ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n\n--********************************\n-- Open terms\n--********************************\n\ndata Vec (A : Set) : Nat -> Set where\n vnil : Vec A ze\n vcons : {n : Nat} -> A -> Vec A n -> Vec A (su n)\n\ndata Fin : Nat -> Set where\n fze : {n : Nat} -> Fin (su n)\n fsu : {n : Nat} -> Fin n -> Fin (su n)\n\nContext : Nat -> Set\nContext n = Vec (Sigma Type (\\ty -> IMu closeTerm' ty)) n\n\ntypeAt : {n : Nat}(c : Context n) -> Fin n -> Type\ntypeAt {ze} c ()\ntypeAt {.(su n)} (vcons x xs) (fze {n}) = fst x\ntypeAt {.(su n)} (vcons x xs) (fsu {n} y) = typeAt xs y\n\nlookup : {n : Nat}(c : Context n)(i : Fin n) -> IMu closeTerm' (typeAt c i)\nlookup {ze} c ()\nlookup {su _} (vcons x _) fze = snd x\nlookup {su _} (vcons _ xs) (fsu y) = lookup xs y\n\n\nVar : {n : Nat} -> Context n -> Type -> Set\nVar {n} c ty = Sigma (Fin n) (\\i -> typeAt c i == ty)\n\nopenTerm : {n : Nat} -> Context n -> Type -> IDesc Type\nopenTerm c = toIDesc Type (exprFree ** (\\ty -> Val ty + Var c ty))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : {n : Nat}\n {context : Context n}\n (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm' ty\ndischarge ty (l value) = con (EZe , value)\ndischarge {n} {c} ty (r variable) = subst (cong (IMu closeTerm') (snd variable)) (lookup c (fst variable))\n\nsubstExpr : {n : Nat}\n {ty : Type}\n (context : Context n)\n (sigma : (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm' ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm' ty\nsubstExpr {n} {ty} c sig term = \n substI (\\ty -> Val ty + Var c ty) Val exprFree sig ty term\n\nevalOpen : {n : Nat}{ty : Type}\n (context : Context n) ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen context tm = eval' (substExpr context discharge tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- V 0 :-> true, V 1 :-> 2, V 2 :-> ( false , 1 )\ntestContext : Context _\ntestContext = vcons (bool , con (EZe , true )) \n (vcons (nat , con (EZe , su (su ze)) ) \n (vcons (pair bool nat , con (EZe , ( false , su ze )))\n vnil))\n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , r ( fsu fze , refl ) )\n\ntestSubst1 : IMu closeTerm' nat\ntestSubst1 = substExpr testContext \n discharge \n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con (ESu (ESu EZe) , (con (EZe , l (su ze)) , con ( EZe , r (fsu fze , refl) )) )\n\ntestSubst2 : IMu closeTerm' nat\ntestSubst2 = substExpr testContext \n discharge \n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext test2\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu EZe , (con (EZe , r (fze , refl)) ,\n (con (EZe , r (fsu fze , refl)) ,\n con (EZe , l ze))))\n\ntestSubst3 : IMu closeTerm' nat\ntestSubst3 = substExpr testContext \n discharge \n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext test3\n\n-- V 2\ntest4 : IMu (openTerm testContext) (pair bool nat)\ntest4 = con (EZe , r ( fsu (fsu fze) , refl ) )\n\ntestSubst4 : IMu closeTerm' (pair bool nat)\ntestSubst4 = substExpr testContext \n discharge \n test4\n-- = (false , 1)\ntestEval4 : Val (pair bool nat)\ntestEval4 = evalOpen testContext test4\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"da7ed128a0220380c3109913af5f58760ad241d3","subject":"Improve commentary in Base.Denotation.Notation.","message":"Improve commentary in Base.Denotation.Notation.\n\nOld-commit-hash: f51d5958c73259a9120fc8a2ad20854bb12f8870\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Denotation\/Notation.agda","new_file":"Base\/Denotation\/Notation.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Overloading \u27e6_\u27e7 notation\n--\n-- This module defines a general mechanism for overloading the\n-- \u27e6_\u27e7 notation, using Agda\u2019s instance arguments.\n------------------------------------------------------------------------\n\nmodule Base.Denotation.Notation where\n\nopen import Level\n\nrecord Meaning (Syntax : Set) {\u2113 : Level} : Set (suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\nopen Meaning public\n using (\u27e8_\u27e9\u27e6_\u27e7)\n","old_contents":"module Base.Denotation.Notation where\n\n-- OVERLOADING \u27e6_\u27e7 NOTATION\n--\n-- This module defines a general mechanism for overloading the\n-- \u27e6_\u27e7 notation, using Agda\u2019s instance arguments.\n\nopen import Level\n\nrecord Meaning (Syntax : Set) {\u2113 : Level} : Set (suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\nopen Meaning public\n using (\u27e8_\u27e9\u27e6_\u27e7)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"66a643a5678be6f131477803e19ed0cb52159556","subject":"use the standard terminology refl.","message":"use the standard terminology refl.\n","repos":"piyush-kurur\/sample-code","old_file":"agda\/Equality.agda","new_file":"agda\/Equality.agda","new_contents":"-- This is an implementation of the equality type for Sets. Agda's\n-- standard equality is more powerful. The main idea here is to\n-- illustrate the equality type.\nmodule Equality where\n\nopen import Level\n-- The equality of two elements of type A. The type a \u2261 b is a family\n-- of types which captures the statement of equality in A. Not all of\n-- the types are inhabited as in general not all elements are equal\n-- here. The only type that are inhabited are a \u2261 a by the element\n-- that we call definition. This is called refl (for reflection in\n-- agda).\ndata _\u2261_ {\u2113} {A : Set \u2113} : (a b : A) \u2192 Set \u2113 where\n refl : {x : A} \u2192 x \u2261 x\n\n-- \u2261 is a symmetric relation.\nsym : \u2200{\u2113} {A : Set \u2113} {a b : A} \u2192 a \u2261 b \u2192 b \u2261 a\nsym refl = refl\n\n-- \u2261 is a transitive relation.\ntrans : \u2200 {\u2113} {A : Set \u2113} {a b c : A} \u2192 a \u2261 b \u2192 b \u2261 c \u2192 a \u2261 c\ntrans pAB refl = pAB\n-- trans refl pBC = pBC -- alternate proof of transitivity.\n\n-- Congruence. If we apply f to equals the result are also equal.\ncong : \u2200{\u2113\u2080 \u2113\u2081} {A : Set \u2113\u2080} {B : Set \u2113\u2081}\n {a\u2080 a\u2081 : A} \u2192 (f : A \u2192 B) \u2192 a\u2080 \u2261 a\u2081 \u2192 f a\u2080 \u2261 f a\u2081\ncong f refl = refl\n\n-- Pretty way of doing equational reasoning. If we want to prove a\u2080 \u2261\n-- b through an intermediate set of equations use this. The general\n-- form will look like.\n--\n-- begin a \u2248 a\u2080 by p\u2080\n-- \u2248 a\u2081 by p\u2081\n-- ...\n-- \u2248 b by p\n-- \u220e\n\nbegin : \u2200{\u2113} {A : Set \u2113} (a : A) \u2192 a \u2261 a\n_\u2248_by_ : \u2200{\u2113} {A : Set \u2113} {a b : A} \u2192 a \u2261 b \u2192 (c : A) \u2192 b \u2261 c \u2192 a \u2261 c\n_\u220e : \u2200{\u2113} {A : Set \u2113} (a : A) \u2192 A\n\nbegin a = refl\naEb \u2248 c by bEc = trans aEb bEc\nx \u220e = x\n\ninfixl 1 _\u2248_by_\ninfixl 1 _\u2261_\n","old_contents":"-- This is an implementation of the equality type for Sets. Agda's\n-- standard equality is more powerful. The main idea here is to\n-- illustrate the equality type.\nmodule Equality where\n\nopen import Level\n-- The equality of two elements of type A. The type a \u2261 b is a family\n-- of types which captures the statement of equality in A. Not all of\n-- the types are inhabited as in general not all elements are equal\n-- here. The only type that are inhabited are a \u2261 a by the element\n-- that we call definition. This is called refl (for reflection in\n-- agda).\ndata _\u2261_ {\u2113} {A : Set \u2113} : (a b : A) \u2192 Set \u2113 where\n definition : {x : A} \u2192 x \u2261 x\n\n-- \u2261 is a symmetric relation.\nsym : \u2200{\u2113} {A : Set \u2113} {a b : A} \u2192 a \u2261 b \u2192 b \u2261 a\nsym definition = definition\n\n-- \u2261 is a transitive relation.\ntrans : \u2200 {\u2113} {A : Set \u2113} {a b c : A} \u2192 a \u2261 b \u2192 b \u2261 c \u2192 a \u2261 c\ntrans pAB definition = pAB\n-- trans definition pBC = pBC -- alternate proof of transitivity.\n\n-- Congruence. If we apply f to equals the result are also equal.\ncong : \u2200{\u2113\u2080 \u2113\u2081} {A : Set \u2113\u2080} {B : Set \u2113\u2081}\n {a\u2080 a\u2081 : A} \u2192 (f : A \u2192 B) \u2192 a\u2080 \u2261 a\u2081 \u2192 f a\u2080 \u2261 f a\u2081\ncong f definition = definition\n\n-- Pretty way of doing equational reasoning. If we want to prove a\u2080 \u2261\n-- b through an intermediate set of equations use this. The general\n-- form will look like.\n--\n-- begin a \u2248 a\u2080 by p\u2080\n-- \u2248 a\u2081 by p\u2081\n-- ...\n-- \u2248 b by p\n-- \u220e\n\nbegin : \u2200{\u2113} {A : Set \u2113} (a : A) \u2192 a \u2261 a\n_\u2248_by_ : \u2200{\u2113} {A : Set \u2113} {a b : A} \u2192 a \u2261 b \u2192 (c : A) \u2192 b \u2261 c \u2192 a \u2261 c\n_\u220e : \u2200{\u2113} {A : Set \u2113} (a : A) \u2192 A\n\nbegin a = definition\naEb \u2248 c by bEc = trans aEb bEc\nx \u220e = x\n\ninfixl 1 _\u2248_by_\ninfixl 1 _\u2261_\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"c16588fa8c31fbc05fa4fccc81543dfcc150709d","subject":"Desc model: iso2 implicit.","message":"Desc model: iso2 implicit.\n","repos":"mietek\/epigram2,larrytheliquid\/pigit,mietek\/epigram2","old_file":"models\/IDesc_type_type.agda","new_file":"models\/IDesc_type_type.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\niso1 : (I : Set) -> IDescl I -> IDesc I\niso1 I d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\niso2 : {I : Set} -> IDesc I -> IDescl I\niso2 (var i) = varl i\niso2 (const X) = constl X\niso2 (prod D D') = prodl (iso2 D) (iso2 D')\niso2 (pi S T) = pil S (\\s -> iso2 (T s))\niso2 (sigma S T) = sigmal S (\\s -> iso2 (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-iso1-iso2 : (I : Set) -> (D : IDesc I) -> iso1 I (iso2 D) == D\nproof-iso1-iso2 I (var i) = refl\nproof-iso1-iso2 I (const x) = refl\nproof-iso1-iso2 I (prod D D') with proof-iso1-iso2 I D | proof-iso1-iso2 I D'\n... | p | q = cong2 prod p q\nproof-iso1-iso2 I (pi S T) = cong (pi S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\nproof-iso1-iso2 I (sigma S T) = cong (sigma S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\n\n\n-- From embedding to embedding\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = iso2 (iso1 I D) == D\n\nproof-iso2-iso1 : (I : Set) -> (D : IDescl I) -> iso2 (iso1 I D) == D\nproof-iso2-iso1 I D = induction (\u03bb _ \u2192 descD I)\n (P I)\n (proof-iso2-iso1-casesW I) \n Void\n D\n where proof-iso2-iso1-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-iso2-iso1-cases I (lvar , i) hs = refl\n proof-iso2-iso1-cases I (lconst , x) hs = refl\n proof-iso2-iso1-cases I (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-iso2-iso1-cases I (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\u03bb s \u2192 iso2 (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-cases I (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s \u2192 iso2 (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-iso2-iso1-casesW I Void = proof-iso2-iso1-cases I\n \n\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\niso1 : (I : Set) -> IDescl I -> IDesc I\niso1 I d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\niso2 : (I : Set) -> IDesc I -> IDescl I\niso2 I (var i) = varl i\niso2 I (const X) = constl X\niso2 I (prod D D') = prodl (iso2 I D) (iso2 I D')\niso2 I (pi S T) = pil S (\\s -> iso2 I (T s))\niso2 I (sigma S T) = sigmal S (\\s -> iso2 I (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-iso1-iso2 : (I : Set) -> (D : IDesc I) -> iso1 I (iso2 I D) == D\nproof-iso1-iso2 I (var i) = refl\nproof-iso1-iso2 I (const x) = refl\nproof-iso1-iso2 I (prod D D') with proof-iso1-iso2 I D | proof-iso1-iso2 I D'\n... | p | q = cong2 prod p q\nproof-iso1-iso2 I (pi S T) = cong (pi S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\nproof-iso1-iso2 I (sigma S T) = cong (sigma S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\n\n\n-- From embedding to embedding\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = iso2 I (iso1 I D) == D\n\nproof-iso2-iso1 : (I : Set) -> (D : IDescl I) -> iso2 I (iso1 I D) == D\nproof-iso2-iso1 I D = induction (\u03bb _ \u2192 descD I)\n (P I)\n (proof-iso2-iso1-casesW I) \n Void\n D\n where proof-iso2-iso1-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-iso2-iso1-cases I (lvar , i) hs = refl\n proof-iso2-iso1-cases I (lconst , x) hs = refl\n proof-iso2-iso1-cases I (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-iso2-iso1-cases I (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-cases I (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-iso2-iso1-casesW I Void = proof-iso2-iso1-cases I\n \n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c3bd14c398dacc413acb6e18cf83eca8483f5e2c","subject":"Defined the internal hom.","message":"Defined the internal hom.\n","repos":"heades\/AUGL","old_file":"dialectica-cats\/DC2Sets.agda","new_file":"dialectica-cats\/DC2Sets.agda","new_contents":"-----------------------------------------------------------------------\n-- This file defines DC\u2082(Sets) and its SMC structure. --\n-----------------------------------------------------------------------\nmodule DC2Sets where\n\nopen import prelude\n\n-- The objects:\nObj : Set\u2081\nObj = \u03a3[ U \u2208 Set ] (\u03a3[ X \u2208 Set ] (U \u2192 X \u2192 Set))\n\n-- The morphisms:\nHom : Obj \u2192 Obj \u2192 Set\nHom (U , X , \u03b1) (V , Y , \u03b2) =\n \u03a3[ f \u2208 (U \u2192 V) ]\n (\u03a3[ F \u2208 (U \u2192 Y \u2192 X) ] (\u2200{u : U}{y : Y} \u2192 \u03b1 u (F u y) \u2192 \u03b2 (f u) y))\n\n-- Composition:\ncomp : {A B C : Obj} \u2192 Hom A B \u2192 Hom B C \u2192 Hom A C\ncomp {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)} (f , F , p\u2081) (g , G , p\u2082) =\n (g \u2218 f , (\u03bb u z \u2192 F u (G (f u) z)), (\u03bb {u} {y} p\u2083 \u2192 p\u2082 (p\u2081 p\u2083)))\n\ninfixl 5 _\u25cb_\n\n_\u25cb_ = comp\n\n-- The contravariant hom-functor:\nHom\u2090 : {A' A B B' : Obj} \u2192 Hom A' A \u2192 Hom B B' \u2192 Hom A B \u2192 Hom A' B'\nHom\u2090 f h g = comp f (comp g h)\n\n-- The identity function:\nid : {A : Obj} \u2192 Hom A A \nid {(U , V , \u03b1)} = (id-set , curry snd , id-set)\n\n-- In this formalization we will only worry about proving that the\n-- data of morphisms are equivalent, and not worry about the morphism\n-- conditions. This will make proofs shorter and faster.\n--\n-- If we have parallel morphisms (f,F) and (g,G) in which we know that\n-- f = g and F = G, then the condition for (f,F) will imply the\n-- condition of (g,G) and vice versa. Thus, we can safely ignore it.\ninfix 4 _\u2261h_\n\n_\u2261h_ : {A B : Obj} \u2192 (f g : Hom A B) \u2192 Set\n_\u2261h_ {(U , X , \u03b1)}{(V , Y , \u03b2)} (f , F , p\u2081) (g , G , p\u2082) = f \u2261 g \u00d7 F \u2261 G\n\n\u2261h-refl : {A B : Obj}{f : Hom A B} \u2192 f \u2261h f\n\u2261h-refl {U , X , \u03b1}{V , Y , \u03b2}{f , F , _} = refl , refl\n\n\u2261h-trans : \u2200{A B}{f g h : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h h \u2192 f \u2261h h\n\u2261h-trans {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _}{h , H , _} (p\u2081 , p\u2082) (p\u2083 , p\u2084) rewrite p\u2081 | p\u2082 | p\u2083 | p\u2084 = refl , refl\n\n\u2261h-sym : \u2200{A B}{f g : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h f\n\u2261h-sym {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\n\u2261h-subst-\u25cb : \u2200{A B C}{f\u2081 f\u2082 : Hom A B}{g\u2081 g\u2082 : Hom B C}{j : Hom A C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 g\u2081 \u2261h g\u2082\n \u2192 f\u2082 \u25cb g\u2082 \u2261h j\n \u2192 f\u2081 \u25cb g\u2081 \u2261h j\n\u2261h-subst-\u25cb {U , X , \u03b1}\n {V , Y , \u03b2}\n {W , Z , \u03b3}\n {f\u2081 , F\u2081 , _}\n {f\u2082 , F\u2082 , _}\n {g\u2081 , G\u2081 , _}\n {g\u2082 , G\u2082 , _}\n {j , J , _}\n (p\u2085 , p\u2086) (p\u2087 , p\u2088) (p\u2089 , p\u2081\u2080) rewrite p\u2085 | p\u2086 | p\u2087 | p\u2088 | p\u2089 | p\u2081\u2080 = refl , refl\n\n\u25cb-assoc : \u2200{A B C D}{f : Hom A B}{g : Hom B C}{h : Hom C D}\n \u2192 f \u25cb (g \u25cb h) \u2261h (f \u25cb g) \u25cb h\n\u25cb-assoc {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{S , T , \u03b9}\n {f , F , _}{g , G , _}{h , H , _} = refl , refl\n\n\u25cb-idl : \u2200{A B}{f : Hom A B} \u2192 id \u25cb f \u2261h f\n\u25cb-idl {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\u25cb-idr : \u2200{A B}{f : Hom A B} \u2192 f \u25cb id \u2261h f\n\u25cb-idr {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\n-----------------------------------------------------------------------\n-- DC\u2082(Sets) is a SMC --\n-----------------------------------------------------------------------\n\n-- The tensor functor: \u2297\n_\u2297\u1d63_ : \u2200{U X V Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 ((U \u00d7 V) \u2192 (X \u00d7 Y) \u2192 Set)\n_\u2297\u1d63_ \u03b1 \u03b2 (u , v) (x , y) = (\u03b1 u x) \u00d7 (\u03b2 v y)\n\n_\u2297\u2092_ : (A B : Obj) \u2192 Obj\n(U , X , \u03b1) \u2297\u2092 (V , Y , \u03b2) = ((U \u00d7 V) , (X \u00d7 Y) , \u03b1 \u2297\u1d63 \u03b2)\n\nF\u2297 : \u2200{Z T V X U Y : Set}{F : U \u2192 Z \u2192 X}{G : V \u2192 T \u2192 Y} \u2192 (U \u00d7 V) \u2192 (Z \u00d7 T) \u2192 (X \u00d7 Y)\nF\u2297 {F = F}{G} (u , v) (z , t) = F u z , G v t\n \n_\u2297\u2090_ : {A B C D : Obj} \u2192 Hom A C \u2192 Hom B D \u2192 Hom (A \u2297\u2092 B) (C \u2297\u2092 D)\n_\u2297\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) = \u27e8 f , g \u27e9 , F\u2297 {F = F}{G} , p\u2297\n where\n p\u2297 : {u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Z (\u03bb x \u2192 T)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (F\u2297 {F = F}{G} u y) \u2192 (\u03b3 \u2297\u1d63 \u03b4) (\u27e8 f , g \u27e9 u) y\n p\u2297 {u , v}{z , t} (p\u2083 , p\u2084) = p\u2081 p\u2083 , p\u2082 p\u2084\n\n\n-- The unit for tensor:\n\u03b9 : \u22a4 \u2192 \u22a4 \u2192 Set\n\u03b9 triv triv = \u22a4\n\nI : Obj\nI = (\u22a4 , \u22a4 , \u03b9)\n\nJ : Obj\nJ = (\u22a4 , \u22a4 , (\u03bb x y \u2192 \u22a5))\n\n\n-- The left-unitor:\n\u03bb\u2297-p : \u2200{U X \u03b1}{u : \u03a3 \u22a4 (\u03bb x \u2192 U)} {y : X} \u2192 (\u03b9 \u2297\u1d63 \u03b1) u (triv , y) \u2192 \u03b1 (snd u) y\n\u03bb\u2297-p {U}{X}{\u03b1}{(triv , u)}{x} = snd\n \n\u03bb\u2297 : \u2200{A : Obj} \u2192 Hom (I \u2297\u2092 A) A\n\u03bb\u2297 {(U , X , \u03b1)} = snd , (\u03bb _ x \u2192 triv , x) , \u03bb\u2297-p\n\n\u03bb\u2297-inv : \u2200{A : Obj} \u2192 Hom A (I \u2297\u2092 A)\n\u03bb\u2297-inv {(U , X , \u03b1)} = (\u03bb u \u2192 triv , u) , (\u03bb _ r \u2192 snd r) , \u03bb\u2297-inv-p\n where\n \u03bb\u2297-inv-p : \u2200{U X \u03b1}{u : U} {y : \u03a3 \u22a4 (\u03bb x \u2192 X)} \u2192 \u03b1 u (snd y) \u2192 (\u03b9 \u2297\u1d63 \u03b1) (triv , u) y\n \u03bb\u2297-inv-p {U}{X}{\u03b1}{u}{triv , x} p = triv , p\n\n-- The right-unitor:\n\u03c1\u2297 : \u2200{A : Obj} \u2192 Hom (A \u2297\u2092 I) A\n\u03c1\u2297 {(U , X , \u03b1)} = fst , (\u03bb r x \u2192 x , triv) , \u03c1\u2297-p\n where\n \u03c1\u2297-p : \u2200{U X \u03b1}{u : \u03a3 U (\u03bb x \u2192 \u22a4)} {y : X} \u2192 (\u03b1 \u2297\u1d63 \u03b9) u (y , triv) \u2192 \u03b1 (fst u) y\n \u03c1\u2297-p {U}{X}{\u03b1}{(u , _)}{x} (p , _) = p\n\n\n\u03c1\u2297-inv : \u2200{A : Obj} \u2192 Hom A (A \u2297\u2092 I)\n\u03c1\u2297-inv {(U , X , \u03b1)} = (\u03bb u \u2192 u , triv) , (\u03bb u r \u2192 fst r) , \u03c1\u2297-p-inv\n where\n \u03c1\u2297-p-inv : \u2200{U X \u03b1}{u : U} {y : \u03a3 X (\u03bb x \u2192 \u22a4)} \u2192 \u03b1 u (fst y) \u2192 (\u03b1 \u2297\u1d63 \u03b9) (u , triv) y\n \u03c1\u2297-p-inv {U}{X}{\u03b1}{u}{x , triv} p = p , triv\n\n-- Symmetry:\n\u03b2\u2297 : \u2200{A B : Obj} \u2192 Hom (A \u2297\u2092 B) (B \u2297\u2092 A)\n\u03b2\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)} = twist-\u00d7 , (\u03bb r\u2081 r\u2082 \u2192 twist-\u00d7 r\u2082) , \u03b2\u2297-p\n where\n \u03b2\u2297-p : \u2200{U V Y X \u03b1 \u03b2}{u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Y (\u03bb x \u2192 X)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (twist-\u00d7 y) \u2192 (\u03b2 \u2297\u1d63 \u03b1) (twist-\u00d7 u) y\n \u03b2\u2297-p {U}{V}{Y}{X}{\u03b1}{\u03b2}{u , v}{y , x} = twist-\u00d7\n\n\n-- The associator:\nF\u03b1-inv : \u2200{\u2113}{U V W X Y Z : Set \u2113} \u2192 (U \u00d7 (V \u00d7 W)) \u2192 ((X \u00d7 Y) \u00d7 Z) \u2192 (X \u00d7 (Y \u00d7 Z))\nF\u03b1-inv (u , (v , w)) ((x , y) , z) = x , y , z\n \n\u03b1\u2297-inv : \u2200{A B C : Obj} \u2192 Hom (A \u2297\u2092 (B \u2297\u2092 C)) ((A \u2297\u2092 B) \u2297\u2092 C)\n\u03b1\u2297-inv {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = rl-assoc-\u00d7 , F\u03b1-inv , \u03b1-inv-cond\n where\n \u03b1-inv-cond : {u : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))}{y : \u03a3 (\u03a3 X (\u03bb x \u2192 Y)) (\u03bb x \u2192 Z)}\n \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) u (F\u03b1-inv u y)\n \u2192 ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) (rl-assoc-\u00d7 u) y\n \u03b1-inv-cond {u , (v , w)}{(x , y) , z} (p\u2081 , (p\u2082 , p\u2083)) = (p\u2081 , p\u2082) , p\u2083\n\n\nF\u03b1 : \u2200{V W X Y U Z : Set} \u2192 ((U \u00d7 V) \u00d7 W) \u2192 (X \u00d7 (Y \u00d7 Z)) \u2192 ((X \u00d7 Y) \u00d7 Z)\nF\u03b1 {V}{W}{X}{Y}{U}{Z} ((u , v) , w) (x , (y , z)) = (x , y) , z\n\n\u03b1\u2297 : \u2200{A B C : Obj} \u2192 Hom ((A \u2297\u2092 B) \u2297\u2092 C) (A \u2297\u2092 (B \u2297\u2092 C)) \n\u03b1\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = (lr-assoc-\u00d7 , F\u03b1 , \u03b1-cond)\n where\n \u03b1-cond : {u : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)}\n {y : \u03a3 X (\u03bb x \u2192 \u03a3 Y (\u03bb x\u2081 \u2192 Z))} \u2192\n ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) u (F\u03b1 u y) \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) (lr-assoc-\u00d7 u) y\n \u03b1-cond {(u , v) , w}{x , (y , z)} ((p\u2081 , p\u2082) , p\u2083) = p\u2081 , p\u2082 , p\u2083\n\n\u03b1\u2297-id\u2081 : \u2200{A B C} \u2192 (\u03b1\u2297 {A}{B}{C}) \u25cb \u03b1\u2297-inv \u2261h id\n\u03b1\u2297-id\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)} \u2192 rl-assoc-\u00d7 (lr-assoc-\u00d7 a) \u2261 a\n aux {(u , v) , w} = refl\n\n aux' : {a : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)} \u2192 (\u03bb z \u2192 F\u03b1 {V}{W}{X}{Y}{U}{Z} a (F\u03b1-inv (lr-assoc-\u00d7 a) z)) \u2261 (\u03bb y \u2192 y)\n aux' {(u , v), w} = ext-set aux''\n where\n aux'' : {a : \u03a3 (\u03a3 X (\u03bb x \u2192 Y)) (\u03bb x \u2192 Z)} \u2192 F\u03b1 ((u , v) , w) (F\u03b1-inv (u , v , w) a) \u2261 a\n aux'' {(x , y) , z} = refl\n\n\u03b1\u2297-id\u2082 : \u2200{A B C} \u2192 (\u03b1\u2297-inv {A}{B}{C}) \u25cb \u03b1\u2297 \u2261h id\n\u03b1\u2297-id\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))} \u2192 lr-assoc-\u00d7 (rl-assoc-\u00d7 a) \u2261 a\n aux {u , (v , w)} = refl\n aux' : {a : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))} \u2192 (\u03bb z \u2192 F\u03b1-inv {_}{U}{V}{W}{X}{Y}{Z} a (F\u03b1 (rl-assoc-\u00d7 a) z)) \u2261 (\u03bb y \u2192 y)\n aux' {u , (v , w)} = ext-set aux''\n where\n aux'' : {a : \u03a3 X (\u03bb x \u2192 \u03a3 Y (\u03bb x\u2081 \u2192 Z))} \u2192 F\u03b1-inv (u , v , w) (F\u03b1 ((u , v) , w) a) \u2261 a\n aux'' {x , (y , z)} = refl\n \n-- Internal hom:\n\u22b8-cond : \u2200{U V X Y : Set}{\u03b1 : U \u2192 X \u2192 Set}{\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 \u03a3 (U \u2192 V) (\u03bb x \u2192 U \u2192 Y \u2192 X)\n \u2192 \u03a3 U (\u03bb x \u2192 Y)\n \u2192 Set\n\u22b8-cond {\u03b1 = \u03b1}{\u03b2} (f , F) (u , y) = \u03b1 u (F u y) \u2192 \u03b2 (f u) y\n\n_\u22b8\u2092_ : Obj \u2192 Obj \u2192 Obj\n(U , X , \u03b1) \u22b8\u2092 (V , Y , \u03b2) = ((U \u2192 V) \u00d7 (U \u2192 Y \u2192 X)) , ((U \u00d7 Y) , \u22b8-cond {\u03b1 = \u03b1}{\u03b2})\n\n\n_\u22b8\u2090_ : {A B C D : Obj} \u2192 Hom C A \u2192 Hom B D \u2192 Hom (A \u22b8\u2092 B) (C \u22b8\u2092 D)\n_\u22b8\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) =\n h , H , cond\n where\n h : \u03a3 (U \u2192 V) (\u03bb x \u2192 U \u2192 Y \u2192 X) \u2192 \u03a3 (W \u2192 S) (\u03bb x \u2192 W \u2192 T \u2192 Z)\n h (i , I) = (\u03bb w \u2192 g (i (f w))) , (\u03bb w t \u2192 F w (I (f w) (G (i (f w)) t)))\n H : \u03a3 (U \u2192 V) (\u03bb x \u2192 U \u2192 Y \u2192 X) \u2192 \u03a3 W (\u03bb x \u2192 T) \u2192 \u03a3 U (\u03bb x \u2192 Y)\n H (i , I) (w , t) = f w , G (i (f w)) t\n cond : {u : \u03a3 (U \u2192 V) (\u03bb x \u2192 U \u2192 Y \u2192 X)} {y : \u03a3 W (\u03bb x \u2192 T)} \u2192 \u22b8-cond {\u03b1 = \u03b1}{\u03b2} u (H u y) \u2192 \u22b8-cond {\u03b1 = \u03b3}{\u03b4} (h u) y\n cond {i , I}{w , y} p\u2083 p\u2084 = p\u2082 (p\u2083 (p\u2081 p\u2084))\n\n{-\ncur : {A B C : Obj}\n \u2192 Hom (A \u2297\u2092 B) C\n \u2192 Hom A (B \u22b8\u2092 C)\ncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb u \u2192 (\u03bb v \u2192 f (u , v)) , (\u03bb z \u2192 snd (F z) u)) , (\u03bb p \u2192 fst (F (snd p)) (fst p)) , cur-cond\n where\n cur-cond : \u2200{u : U}{y : \u03a3 V (\u03bb x \u2192 Z)}\n \u2192 \u03b1 u (fst (F (snd y)) (fst y))\n \u2192 \u22b8-cond \u03b2 \u03b3 ((\u03bb v \u2192 f (u , v)) , (\u03bb z \u2192 snd (F z) u)) y\n cur-cond {u}{v , z} p\u2082 p\u2083 with p\u2081 {u , v}{z} \n ... | p\u2081' with F z\n ... | (j\u2081 , j\u2082) = p\u2081' (p\u2082 , p\u2083)\n\ncur-\u2261h : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-\u2261h {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}\n {f\u2081 , F\u2081 , p\u2081}{f\u2082 , F\u2082 , p\u2082} (p\u2083 , p\u2084)\n rewrite p\u2083 | p\u2084 = refl , refl\n\ncur-cong : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C} \u2192 f\u2081 \u2261h f\u2082 \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-cong {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)}{f\u2081 , F\u2081 , _}{f\u2082 , F\u2082 , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\nuncur : {A B C : Obj}\n \u2192 Hom A (B \u22b8\u2092 C)\n \u2192 Hom (A \u2297\u2092 B) C\nuncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb p \u2192 fst (f (fst p)) (snd p)) , (\u03bb z \u2192 (\u03bb v \u2192 F (v , z)) , (\u03bb u \u2192 snd (f u) z)) , uncur-cond\n where\n uncur-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : Z}\n \u2192 (\u03b1 \u2297\u1d63 \u03b2) u ((\u03bb v \u2192 F (v , y)) , (\u03bb u\u2081 \u2192 snd (f u\u2081) y))\n \u2192 \u03b3 (fst (f (fst u)) (snd u)) y\n uncur-cond {u , v}{z} (p\u2082 , p\u2083) with p\u2081 {u}{v , z} p\u2082\n ... | p\u2081' with f u\n ... | (j\u2081 , j\u2082) = p\u2081' p\u2083\n\ncur-uncur-bij\u2081 : \u2200{A B C}{f : Hom (A \u2297\u2092 B) C}\n \u2192 uncur (cur f) \u2261h f\ncur-uncur-bij\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{f , F , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : \u03a3 U (\u03bb x \u2192 V)} \u2192 f (fst a , snd a) \u2261 f a\n aux\u2081 {u , v} = refl\n \n aux\u2082 : {a : Z} \u2192 ((\u03bb v \u2192 fst (F a) v) , (\u03bb u \u2192 snd (F a) u)) \u2261 F a\n aux\u2082 {z} with F z\n ... | j\u2081 , j\u2082 = refl\n\ncur-uncur-bij\u2082 : \u2200{A B C}{g : Hom A (B \u22b8\u2092 C)}\n \u2192 cur (uncur g) \u2261h g\ncur-uncur-bij\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{g , G , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : U} \u2192 ((\u03bb v \u2192 fst (g a) v) , (\u03bb z \u2192 snd (g a) z)) \u2261 g a\n aux\u2081 {u} with g u\n ... | (j\u2081 , j\u2082) = refl\n\n aux\u2082 : {a : \u03a3 V (\u03bb x \u2192 Z)} \u2192 G (fst a , snd a) \u2261 G a\n aux\u2082 {v , z} = refl\n \n-- The of-course exponential:\n!\u2092-cond : \u2200{U X : Set}\n \u2192 (U \u2192 X \u2192 Set)\n \u2192 U\n \u2192 (U \u2192 X *)\n \u2192 Set\n!\u2092-cond \u03b1 u f = all-pred (\u03b1 u) (f u)\n \n!\u2092 : Obj \u2192 Obj\n!\u2092 (U , X , \u03b1) = U , (U \u2192 X *) , !\u2092-cond \u03b1\n\n!-cta : {V Y U X : Set}\n \u2192 (Y \u2192 X)\n \u2192 (U \u2192 V)\n \u2192 (V \u2192 Y *)\n \u2192 (U \u2192 X *)\n!-cta F f g = \u03bb u \u2192 list-funct F (g (f u))\n\n!\u2090-cond : \u2200{U V Y X : Set}{F : Y \u2192 X}{f : U \u2192 V}\n \u2192 (\u03b1 : U \u2192 X \u2192 Set)\n \u2192 (\u03b2 : V \u2192 Y \u2192 Set)\n \u2192 (p : {u : U} {y : Y} \u2192 \u03b1 u (F y) \u2192 \u03b2 (f u) y)\n {u : U}{l : Y *}\n \u2192 all-pred (\u03b1 u) (list-funct F l)\n \u2192 all-pred (\u03b2 (f u)) l\n!\u2090-cond _ _ _ {l = []} _ = triv\n!\u2090-cond \u03b1 \u03b2 p {u}{x :: xs} (p' , p'') = p p' , !\u2090-cond \u03b1 \u03b2 p p'' \n \n!\u2090 : {A B : Obj} \u2192 Hom A B \u2192 Hom (!\u2092 A) (!\u2092 B)\n!\u2090 {U , X , \u03b1}{V , Y , \u03b2} (f , F , p) = f , !-cta F f , !\u2090-cond \u03b1 \u03b2 p\n\n-- Of-course is a comonad:\n\u03b5 : \u2200{A} \u2192 Hom (!\u2092 A) A\n\u03b5 {U , X , \u03b1} = id-set , (\u03bb x y \u2192 [ x ]) , fst\n\n\u03b4-cta : {U X : Set} \u2192 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X)) \u2192 U \u2192 \ud835\udd43 X\n\u03b4-cta g u = foldr (\u03bb f rest \u2192 (f u) ++ rest) [] (g u)\n \n\u03b4 : \u2200{A} \u2192 Hom (!\u2092 A) (!\u2092 (!\u2092 A))\n\u03b4 {U , X , \u03b1} = id-set , \u03b4-cta , \u03b4-cond\n where\n \u03b4-cond : {u : U} {l : \ud835\udd43 (U \u2192 \ud835\udd43 X)}\n \u2192 all-pred (\u03b1 u) (foldr (\u03bb f \u2192 _++_ (f u)) [] l)\n \u2192 all-pred (\u03bb f\n \u2192 all-pred (\u03b1 u) (f u)) l\n \u03b4-cond {l = []} _ = triv\n \u03b4-cond {u}{l = x :: l'} p with\n all-pred-append {X}{\u03b1 u}\n {x u}\n {foldr (\u03bb f \u2192 _++_ (f u)) [] l'}\n \u2227-unit \u2227-assoc\n ... | p' rewrite p' = fst p , \u03b4-cond {u} {l'} (snd p)\n\n-- These diagrams can be found on page 22 of the report.\ncomonand-diag\u2081 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (!\u2090 (\u03b4 {A})) \u2261h (\u03b4 {A}) \u25cb (\u03b4 { !\u2092 A})\ncomonand-diag\u2081 {U , X , \u03b1} =\n refl , ext-set (\u03bb {a} \u2192 ext-set (\u03bb {a\u2081} \u2192 aux {a\u2081}{a a\u2081}))\n where\n aux : \u2200{a\u2081 : U}{l : \ud835\udd43 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X))} \u2192\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) []\n (map (\u03bb g u \u2192 foldr (\u03bb f \u2192 _++_ (f u)) [] (g u)) l)\n \u2261\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] (foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] l)\n aux {a}{[]} = refl \n aux {a}{x :: l} rewrite\n sym (foldr-append {l\u2081 = x a}{foldr (\u03bb f \u2192 _++_ (f a)) [] l}{a})\n = cong2 {a = foldr (\u03bb f \u2192 _++_ (f a)) [] (x a)}\n _++_\n refl\n (aux {a}{l})\n\ncomonand-diag\u2082 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (\u03b5 { !\u2092 A}) \u2261h (\u03b4 {A}) \u25cb (!\u2090 (\u03b5 {A}))\ncomonand-diag\u2082 {U , X , \u03b1} =\n refl , ext-set (\u03bb {f} \u2192 ext-set (\u03bb {a} \u2192 aux {a}{f a}))\n where\n aux : \u2200{a : U}{l : X *}\n \u2192 l ++ [] \u2261 foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x y \u2192 x :: []) l)\n aux {a}{[]} = refl\n aux {a}{x :: l} with aux {a}{l}\n ... | IH rewrite ++[] l =\n cong2 {a = x} {x} {l}\n {foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x\u2081 y \u2192 x\u2081 :: []) l)} _::_ refl\n IH\n \nmodule Cartesian where\n \u03c0\u2081 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (U , X , \u03b1))\n \u03c0\u2081 {U}{X}{V}{Y}{\u03b1}{\u03b2} = fst , (\u03bb f \u2192 (\u03bb v u \u2192 f u) , (\u03bb u v \u2192 [])) , \u03c0\u2081-cond\n where\n \u03c0\u2081-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : U \u2192 \ud835\udd43 X} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 y u\u2081) , (\u03bb u\u2081 v \u2192 [])) \u2192\n all-pred (\u03b1 (fst u)) (y (fst u))\n \u03c0\u2081-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2081\n\n \u03c0\u2082 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (V , Y , \u03b2))\n \u03c0\u2082 {U}{X}{V}{Y}{\u03b1}{\u03b2} = snd , (\u03bb f \u2192 (\u03bb v u \u2192 []) , (\u03bb u v \u2192 f v)) , \u03c0\u2082-cond\n where\n \u03c0\u2082-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : V \u2192 \ud835\udd43 Y} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 []) , (\u03bb u\u2081 v \u2192 y v)) \u2192\n all-pred (\u03b2 (snd u)) (y (snd u))\n \u03c0\u2082-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2082\n\n cart-ar-crt : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y) \u2192 W \u2192 \ud835\udd43 Z\n cart-ar-crt (f , F , p\u2081) (g , G , p\u2082) (j\u2081 , j\u2082) w = F (j\u2081 (g w)) w ++ G (j\u2082 (f w)) w\n\n cart-ar : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))\n cart-ar {U}{X}{V}{Y}{W}{Z}{\u03b1}{\u03b2}{\u03b3} (f , F , p\u2081) (g , G , p\u2082)\n = (\u03bb w \u2192 f w , g w) , cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) , cart-ar-cond\n where\n cart-ar-cond : \u2200{u : W} {y : \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y)} \u2192\n all-pred (\u03b3 u) (cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) y u) \u2192\n ((\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b1 u\u2081) (f\u2081 u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b2 u\u2081) (f\u2081 u\u2081)))\n (f u , g u) y\n cart-ar-cond {w}{j\u2081 , j\u2082} p\n rewrite\n all-pred-append {f = \u03b3 w}{F (j\u2081 (g w)) w}{G (j\u2082 (f w)) w} \u2227-unit \u2227-assoc with p\n ... | (a , b) = p\u2081 a , p\u2082 b\n\n cart-diag\u2081 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (U , X , \u03b1)}\n (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (U , X , \u03b1)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2081)\n cart-diag\u2081 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 sym (++[] (map F (j (f w))))))\n\n cart-diag\u2082 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (V , Y , \u03b2)}\n (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (V , Y , \u03b2)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2082)\n cart-diag\u2082 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 refl))\n-}\n","old_contents":"-----------------------------------------------------------------------\n-- This file defines DC\u2082(Sets) and its SMC structure. --\n-----------------------------------------------------------------------\nmodule DC2Sets where\n\nopen import prelude\n\n-- The objects:\nObj : Set\u2081\nObj = \u03a3[ U \u2208 Set ] (\u03a3[ X \u2208 Set ] (U \u2192 X \u2192 Set))\n\n-- The morphisms:\nHom : Obj \u2192 Obj \u2192 Set\nHom (U , X , \u03b1) (V , Y , \u03b2) =\n \u03a3[ f \u2208 (U \u2192 V) ]\n (\u03a3[ F \u2208 (U \u2192 Y \u2192 X) ] (\u2200{u : U}{y : Y} \u2192 \u03b1 u (F u y) \u2192 \u03b2 (f u) y))\n\n-- Composition:\ncomp : {A B C : Obj} \u2192 Hom A B \u2192 Hom B C \u2192 Hom A C\ncomp {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)} (f , F , p\u2081) (g , G , p\u2082) =\n (g \u2218 f , (\u03bb u z \u2192 F u (G (f u) z)), (\u03bb {u} {y} p\u2083 \u2192 p\u2082 (p\u2081 p\u2083)))\n\ninfixl 5 _\u25cb_\n\n_\u25cb_ = comp\n\n-- The contravariant hom-functor:\nHom\u2090 : {A' A B B' : Obj} \u2192 Hom A' A \u2192 Hom B B' \u2192 Hom A B \u2192 Hom A' B'\nHom\u2090 f h g = comp f (comp g h)\n\n-- The identity function:\nid : {A : Obj} \u2192 Hom A A \nid {(U , V , \u03b1)} = (id-set , curry snd , id-set)\n\n-- In this formalization we will only worry about proving that the\n-- data of morphisms are equivalent, and not worry about the morphism\n-- conditions. This will make proofs shorter and faster.\n--\n-- If we have parallel morphisms (f,F) and (g,G) in which we know that\n-- f = g and F = G, then the condition for (f,F) will imply the\n-- condition of (g,G) and vice versa. Thus, we can safely ignore it.\ninfix 4 _\u2261h_\n\n_\u2261h_ : {A B : Obj} \u2192 (f g : Hom A B) \u2192 Set\n_\u2261h_ {(U , X , \u03b1)}{(V , Y , \u03b2)} (f , F , p\u2081) (g , G , p\u2082) = f \u2261 g \u00d7 F \u2261 G\n\n\u2261h-refl : {A B : Obj}{f : Hom A B} \u2192 f \u2261h f\n\u2261h-refl {U , X , \u03b1}{V , Y , \u03b2}{f , F , _} = refl , refl\n\n\u2261h-trans : \u2200{A B}{f g h : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h h \u2192 f \u2261h h\n\u2261h-trans {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _}{h , H , _} (p\u2081 , p\u2082) (p\u2083 , p\u2084) rewrite p\u2081 | p\u2082 | p\u2083 | p\u2084 = refl , refl\n\n\u2261h-sym : \u2200{A B}{f g : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h f\n\u2261h-sym {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\n\u2261h-subst-\u25cb : \u2200{A B C}{f\u2081 f\u2082 : Hom A B}{g\u2081 g\u2082 : Hom B C}{j : Hom A C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 g\u2081 \u2261h g\u2082\n \u2192 f\u2082 \u25cb g\u2082 \u2261h j\n \u2192 f\u2081 \u25cb g\u2081 \u2261h j\n\u2261h-subst-\u25cb {U , X , \u03b1}\n {V , Y , \u03b2}\n {W , Z , \u03b3}\n {f\u2081 , F\u2081 , _}\n {f\u2082 , F\u2082 , _}\n {g\u2081 , G\u2081 , _}\n {g\u2082 , G\u2082 , _}\n {j , J , _}\n (p\u2085 , p\u2086) (p\u2087 , p\u2088) (p\u2089 , p\u2081\u2080) rewrite p\u2085 | p\u2086 | p\u2087 | p\u2088 | p\u2089 | p\u2081\u2080 = refl , refl\n\n\u25cb-assoc : \u2200{A B C D}{f : Hom A B}{g : Hom B C}{h : Hom C D}\n \u2192 f \u25cb (g \u25cb h) \u2261h (f \u25cb g) \u25cb h\n\u25cb-assoc {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{S , T , \u03b9}\n {f , F , _}{g , G , _}{h , H , _} = refl , refl\n\n\u25cb-idl : \u2200{A B}{f : Hom A B} \u2192 id \u25cb f \u2261h f\n\u25cb-idl {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\u25cb-idr : \u2200{A B}{f : Hom A B} \u2192 f \u25cb id \u2261h f\n\u25cb-idr {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\n-----------------------------------------------------------------------\n-- DC\u2082(Sets) is a SMC --\n-----------------------------------------------------------------------\n\n-- The tensor functor: \u2297\n_\u2297\u1d63_ : \u2200{U X V Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 ((U \u00d7 V) \u2192 (X \u00d7 Y) \u2192 Set)\n_\u2297\u1d63_ \u03b1 \u03b2 (u , v) (x , y) = (\u03b1 u x) \u00d7 (\u03b2 v y)\n\n_\u2297\u2092_ : (A B : Obj) \u2192 Obj\n(U , X , \u03b1) \u2297\u2092 (V , Y , \u03b2) = ((U \u00d7 V) , (X \u00d7 Y) , \u03b1 \u2297\u1d63 \u03b2)\n\nF\u2297 : \u2200{Z T V X U Y : Set}{F : U \u2192 Z \u2192 X}{G : V \u2192 T \u2192 Y} \u2192 (U \u00d7 V) \u2192 (Z \u00d7 T) \u2192 (X \u00d7 Y)\nF\u2297 {F = F}{G} (u , v) (z , t) = F u z , G v t\n \n_\u2297\u2090_ : {A B C D : Obj} \u2192 Hom A C \u2192 Hom B D \u2192 Hom (A \u2297\u2092 B) (C \u2297\u2092 D)\n_\u2297\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) = \u27e8 f , g \u27e9 , F\u2297 {F = F}{G} , p\u2297\n where\n p\u2297 : {u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Z (\u03bb x \u2192 T)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (F\u2297 {F = F}{G} u y) \u2192 (\u03b3 \u2297\u1d63 \u03b4) (\u27e8 f , g \u27e9 u) y\n p\u2297 {u , v}{z , t} (p\u2083 , p\u2084) = p\u2081 p\u2083 , p\u2082 p\u2084\n\n\n-- The unit for tensor:\n\u03b9 : \u22a4 \u2192 \u22a4 \u2192 Set\n\u03b9 triv triv = \u22a4\n\nI : Obj\nI = (\u22a4 , \u22a4 , \u03b9)\n\nJ : Obj\nJ = (\u22a4 , \u22a4 , (\u03bb x y \u2192 \u22a5))\n\n\n-- The left-unitor:\n\u03bb\u2297-p : \u2200{U X \u03b1}{u : \u03a3 \u22a4 (\u03bb x \u2192 U)} {y : X} \u2192 (\u03b9 \u2297\u1d63 \u03b1) u (triv , y) \u2192 \u03b1 (snd u) y\n\u03bb\u2297-p {U}{X}{\u03b1}{(triv , u)}{x} = snd\n \n\u03bb\u2297 : \u2200{A : Obj} \u2192 Hom (I \u2297\u2092 A) A\n\u03bb\u2297 {(U , X , \u03b1)} = snd , (\u03bb _ x \u2192 triv , x) , \u03bb\u2297-p\n\n\u03bb\u2297-inv : \u2200{A : Obj} \u2192 Hom A (I \u2297\u2092 A)\n\u03bb\u2297-inv {(U , X , \u03b1)} = (\u03bb u \u2192 triv , u) , (\u03bb _ r \u2192 snd r) , \u03bb\u2297-inv-p\n where\n \u03bb\u2297-inv-p : \u2200{U X \u03b1}{u : U} {y : \u03a3 \u22a4 (\u03bb x \u2192 X)} \u2192 \u03b1 u (snd y) \u2192 (\u03b9 \u2297\u1d63 \u03b1) (triv , u) y\n \u03bb\u2297-inv-p {U}{X}{\u03b1}{u}{triv , x} p = triv , p\n\n-- The right-unitor:\n\u03c1\u2297 : \u2200{A : Obj} \u2192 Hom (A \u2297\u2092 I) A\n\u03c1\u2297 {(U , X , \u03b1)} = fst , (\u03bb r x \u2192 x , triv) , \u03c1\u2297-p\n where\n \u03c1\u2297-p : \u2200{U X \u03b1}{u : \u03a3 U (\u03bb x \u2192 \u22a4)} {y : X} \u2192 (\u03b1 \u2297\u1d63 \u03b9) u (y , triv) \u2192 \u03b1 (fst u) y\n \u03c1\u2297-p {U}{X}{\u03b1}{(u , _)}{x} (p , _) = p\n\n\n\u03c1\u2297-inv : \u2200{A : Obj} \u2192 Hom A (A \u2297\u2092 I)\n\u03c1\u2297-inv {(U , X , \u03b1)} = (\u03bb u \u2192 u , triv) , (\u03bb u r \u2192 fst r) , \u03c1\u2297-p-inv\n where\n \u03c1\u2297-p-inv : \u2200{U X \u03b1}{u : U} {y : \u03a3 X (\u03bb x \u2192 \u22a4)} \u2192 \u03b1 u (fst y) \u2192 (\u03b1 \u2297\u1d63 \u03b9) (u , triv) y\n \u03c1\u2297-p-inv {U}{X}{\u03b1}{u}{x , triv} p = p , triv\n\n-- Symmetry:\n\u03b2\u2297 : \u2200{A B : Obj} \u2192 Hom (A \u2297\u2092 B) (B \u2297\u2092 A)\n\u03b2\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)} = twist-\u00d7 , (\u03bb r\u2081 r\u2082 \u2192 twist-\u00d7 r\u2082) , \u03b2\u2297-p\n where\n \u03b2\u2297-p : \u2200{U V Y X \u03b1 \u03b2}{u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Y (\u03bb x \u2192 X)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (twist-\u00d7 y) \u2192 (\u03b2 \u2297\u1d63 \u03b1) (twist-\u00d7 u) y\n \u03b2\u2297-p {U}{V}{Y}{X}{\u03b1}{\u03b2}{u , v}{y , x} = twist-\u00d7\n\n\n-- The associator:\nF\u03b1-inv : \u2200{\u2113}{U V W X Y Z : Set \u2113} \u2192 (U \u00d7 (V \u00d7 W)) \u2192 ((X \u00d7 Y) \u00d7 Z) \u2192 (X \u00d7 (Y \u00d7 Z))\nF\u03b1-inv (u , (v , w)) ((x , y) , z) = x , y , z\n \n\u03b1\u2297-inv : \u2200{A B C : Obj} \u2192 Hom (A \u2297\u2092 (B \u2297\u2092 C)) ((A \u2297\u2092 B) \u2297\u2092 C)\n\u03b1\u2297-inv {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = rl-assoc-\u00d7 , F\u03b1-inv , \u03b1-inv-cond\n where\n \u03b1-inv-cond : {u : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))}{y : \u03a3 (\u03a3 X (\u03bb x \u2192 Y)) (\u03bb x \u2192 Z)}\n \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) u (F\u03b1-inv u y)\n \u2192 ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) (rl-assoc-\u00d7 u) y\n \u03b1-inv-cond {u , (v , w)}{(x , y) , z} (p\u2081 , (p\u2082 , p\u2083)) = (p\u2081 , p\u2082) , p\u2083\n\n\nF\u03b1 : \u2200{V W X Y U Z : Set} \u2192 ((U \u00d7 V) \u00d7 W) \u2192 (X \u00d7 (Y \u00d7 Z)) \u2192 ((X \u00d7 Y) \u00d7 Z)\nF\u03b1 {V}{W}{X}{Y}{U}{Z} ((u , v) , w) (x , (y , z)) = (x , y) , z\n\n\u03b1\u2297 : \u2200{A B C : Obj} \u2192 Hom ((A \u2297\u2092 B) \u2297\u2092 C) (A \u2297\u2092 (B \u2297\u2092 C)) \n\u03b1\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = (lr-assoc-\u00d7 , F\u03b1 , \u03b1-cond)\n where\n \u03b1-cond : {u : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)}\n {y : \u03a3 X (\u03bb x \u2192 \u03a3 Y (\u03bb x\u2081 \u2192 Z))} \u2192\n ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) u (F\u03b1 u y) \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) (lr-assoc-\u00d7 u) y\n \u03b1-cond {(u , v) , w}{x , (y , z)} ((p\u2081 , p\u2082) , p\u2083) = p\u2081 , p\u2082 , p\u2083\n\n\u03b1\u2297-id\u2081 : \u2200{A B C} \u2192 (\u03b1\u2297 {A}{B}{C}) \u25cb \u03b1\u2297-inv \u2261h id\n\u03b1\u2297-id\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)} \u2192 rl-assoc-\u00d7 (lr-assoc-\u00d7 a) \u2261 a\n aux {(u , v) , w} = refl\n\n aux' : {a : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)} \u2192 (\u03bb z \u2192 F\u03b1 {V}{W}{X}{Y}{U}{Z} a (F\u03b1-inv (lr-assoc-\u00d7 a) z)) \u2261 (\u03bb y \u2192 y)\n aux' {(u , v), w} = ext-set aux''\n where\n aux'' : {a : \u03a3 (\u03a3 X (\u03bb x \u2192 Y)) (\u03bb x \u2192 Z)} \u2192 F\u03b1 ((u , v) , w) (F\u03b1-inv (u , v , w) a) \u2261 a\n aux'' {(x , y) , z} = refl\n\n\u03b1\u2297-id\u2082 : \u2200{A B C} \u2192 (\u03b1\u2297-inv {A}{B}{C}) \u25cb \u03b1\u2297 \u2261h id\n\u03b1\u2297-id\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))} \u2192 lr-assoc-\u00d7 (rl-assoc-\u00d7 a) \u2261 a\n aux {u , (v , w)} = refl\n aux' : {a : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))} \u2192 (\u03bb z \u2192 F\u03b1-inv {_}{U}{V}{W}{X}{Y}{Z} a (F\u03b1 (rl-assoc-\u00d7 a) z)) \u2261 (\u03bb y \u2192 y)\n aux' {u , (v , w)} = ext-set aux''\n where\n aux'' : {a : \u03a3 X (\u03bb x \u2192 \u03a3 Y (\u03bb x\u2081 \u2192 Z))} \u2192 F\u03b1-inv (u , v , w) (F\u03b1 ((u , v) , w) a) \u2261 a\n aux'' {x , (y , z)} = refl\n{- \n-- Internal hom:\n\u22b8-cond : \u2200{U V X Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 (U \u2192 V) \u00d7 (Y \u2192 X) \u2192 U \u00d7 Y \u2192 Set\n\u22b8-cond \u03b1 \u03b2 (f , g) (u , y) = \u03b1 u (g y) \u2192 \u03b2 (f u) y\n\n_\u22b8\u2092_ : Obj \u2192 Obj \u2192 Obj\n(U , X , \u03b1) \u22b8\u2092 (V , Y , \u03b2) = ((U \u2192 V) \u00d7 (Y \u2192 X)) , (U \u00d7 Y) , \u22b8-cond \u03b1 \u03b2\n\n_\u22b8\u2090_ : {A B C D : Obj} \u2192 Hom C A \u2192 Hom B D \u2192 Hom (A \u22b8\u2092 B) (C \u22b8\u2092 D)\n_\u22b8\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) =\n h , H , p\u2083\n where\n h : \u03a3 (U \u2192 V) (\u03bb x \u2192 Y \u2192 X) \u2192 \u03a3 (W \u2192 S) (\u03bb x \u2192 T \u2192 Z)\n h (h\u2081 , h\u2082) = (\u03bb w \u2192 g (h\u2081 (f w))) , (\u03bb t \u2192 F (h\u2082 (G t)))\n H : \u03a3 W (\u03bb x \u2192 T) \u2192 \u03a3 U (\u03bb x \u2192 Y)\n H (w , t) = f w , G t\n p\u2083 : {u : \u03a3 (U \u2192 V) (\u03bb x \u2192 Y \u2192 X)} {y : \u03a3 W (\u03bb x \u2192 T)} \u2192 \u22b8-cond \u03b1 \u03b2 u (H y) \u2192 \u22b8-cond \u03b3 \u03b4 (h u) y\n p\u2083 {h\u2081 , h\u2082}{w , t} c c' = p\u2082 (c (p\u2081 c'))\n\ncur : {A B C : Obj}\n \u2192 Hom (A \u2297\u2092 B) C\n \u2192 Hom A (B \u22b8\u2092 C)\ncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb u \u2192 (\u03bb v \u2192 f (u , v)) , (\u03bb z \u2192 snd (F z) u)) , (\u03bb p \u2192 fst (F (snd p)) (fst p)) , cur-cond\n where\n cur-cond : \u2200{u : U}{y : \u03a3 V (\u03bb x \u2192 Z)}\n \u2192 \u03b1 u (fst (F (snd y)) (fst y))\n \u2192 \u22b8-cond \u03b2 \u03b3 ((\u03bb v \u2192 f (u , v)) , (\u03bb z \u2192 snd (F z) u)) y\n cur-cond {u}{v , z} p\u2082 p\u2083 with p\u2081 {u , v}{z} \n ... | p\u2081' with F z\n ... | (j\u2081 , j\u2082) = p\u2081' (p\u2082 , p\u2083)\n\ncur-\u2261h : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-\u2261h {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}\n {f\u2081 , F\u2081 , p\u2081}{f\u2082 , F\u2082 , p\u2082} (p\u2083 , p\u2084)\n rewrite p\u2083 | p\u2084 = refl , refl\n\ncur-cong : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C} \u2192 f\u2081 \u2261h f\u2082 \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-cong {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)}{f\u2081 , F\u2081 , _}{f\u2082 , F\u2082 , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\nuncur : {A B C : Obj}\n \u2192 Hom A (B \u22b8\u2092 C)\n \u2192 Hom (A \u2297\u2092 B) C\nuncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb p \u2192 fst (f (fst p)) (snd p)) , (\u03bb z \u2192 (\u03bb v \u2192 F (v , z)) , (\u03bb u \u2192 snd (f u) z)) , uncur-cond\n where\n uncur-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : Z}\n \u2192 (\u03b1 \u2297\u1d63 \u03b2) u ((\u03bb v \u2192 F (v , y)) , (\u03bb u\u2081 \u2192 snd (f u\u2081) y))\n \u2192 \u03b3 (fst (f (fst u)) (snd u)) y\n uncur-cond {u , v}{z} (p\u2082 , p\u2083) with p\u2081 {u}{v , z} p\u2082\n ... | p\u2081' with f u\n ... | (j\u2081 , j\u2082) = p\u2081' p\u2083\n\ncur-uncur-bij\u2081 : \u2200{A B C}{f : Hom (A \u2297\u2092 B) C}\n \u2192 uncur (cur f) \u2261h f\ncur-uncur-bij\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{f , F , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : \u03a3 U (\u03bb x \u2192 V)} \u2192 f (fst a , snd a) \u2261 f a\n aux\u2081 {u , v} = refl\n \n aux\u2082 : {a : Z} \u2192 ((\u03bb v \u2192 fst (F a) v) , (\u03bb u \u2192 snd (F a) u)) \u2261 F a\n aux\u2082 {z} with F z\n ... | j\u2081 , j\u2082 = refl\n\ncur-uncur-bij\u2082 : \u2200{A B C}{g : Hom A (B \u22b8\u2092 C)}\n \u2192 cur (uncur g) \u2261h g\ncur-uncur-bij\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{g , G , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : U} \u2192 ((\u03bb v \u2192 fst (g a) v) , (\u03bb z \u2192 snd (g a) z)) \u2261 g a\n aux\u2081 {u} with g u\n ... | (j\u2081 , j\u2082) = refl\n\n aux\u2082 : {a : \u03a3 V (\u03bb x \u2192 Z)} \u2192 G (fst a , snd a) \u2261 G a\n aux\u2082 {v , z} = refl\n \n-- The of-course exponential:\n!\u2092-cond : \u2200{U X : Set}\n \u2192 (U \u2192 X \u2192 Set)\n \u2192 U\n \u2192 (U \u2192 X *)\n \u2192 Set\n!\u2092-cond \u03b1 u f = all-pred (\u03b1 u) (f u)\n \n!\u2092 : Obj \u2192 Obj\n!\u2092 (U , X , \u03b1) = U , (U \u2192 X *) , !\u2092-cond \u03b1\n\n!-cta : {V Y U X : Set}\n \u2192 (Y \u2192 X)\n \u2192 (U \u2192 V)\n \u2192 (V \u2192 Y *)\n \u2192 (U \u2192 X *)\n!-cta F f g = \u03bb u \u2192 list-funct F (g (f u))\n\n!\u2090-cond : \u2200{U V Y X : Set}{F : Y \u2192 X}{f : U \u2192 V}\n \u2192 (\u03b1 : U \u2192 X \u2192 Set)\n \u2192 (\u03b2 : V \u2192 Y \u2192 Set)\n \u2192 (p : {u : U} {y : Y} \u2192 \u03b1 u (F y) \u2192 \u03b2 (f u) y)\n {u : U}{l : Y *}\n \u2192 all-pred (\u03b1 u) (list-funct F l)\n \u2192 all-pred (\u03b2 (f u)) l\n!\u2090-cond _ _ _ {l = []} _ = triv\n!\u2090-cond \u03b1 \u03b2 p {u}{x :: xs} (p' , p'') = p p' , !\u2090-cond \u03b1 \u03b2 p p'' \n \n!\u2090 : {A B : Obj} \u2192 Hom A B \u2192 Hom (!\u2092 A) (!\u2092 B)\n!\u2090 {U , X , \u03b1}{V , Y , \u03b2} (f , F , p) = f , !-cta F f , !\u2090-cond \u03b1 \u03b2 p\n\n-- Of-course is a comonad:\n\u03b5 : \u2200{A} \u2192 Hom (!\u2092 A) A\n\u03b5 {U , X , \u03b1} = id-set , (\u03bb x y \u2192 [ x ]) , fst\n\n\u03b4-cta : {U X : Set} \u2192 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X)) \u2192 U \u2192 \ud835\udd43 X\n\u03b4-cta g u = foldr (\u03bb f rest \u2192 (f u) ++ rest) [] (g u)\n \n\u03b4 : \u2200{A} \u2192 Hom (!\u2092 A) (!\u2092 (!\u2092 A))\n\u03b4 {U , X , \u03b1} = id-set , \u03b4-cta , \u03b4-cond\n where\n \u03b4-cond : {u : U} {l : \ud835\udd43 (U \u2192 \ud835\udd43 X)}\n \u2192 all-pred (\u03b1 u) (foldr (\u03bb f \u2192 _++_ (f u)) [] l)\n \u2192 all-pred (\u03bb f\n \u2192 all-pred (\u03b1 u) (f u)) l\n \u03b4-cond {l = []} _ = triv\n \u03b4-cond {u}{l = x :: l'} p with\n all-pred-append {X}{\u03b1 u}\n {x u}\n {foldr (\u03bb f \u2192 _++_ (f u)) [] l'}\n \u2227-unit \u2227-assoc\n ... | p' rewrite p' = fst p , \u03b4-cond {u} {l'} (snd p)\n\n-- These diagrams can be found on page 22 of the report.\ncomonand-diag\u2081 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (!\u2090 (\u03b4 {A})) \u2261h (\u03b4 {A}) \u25cb (\u03b4 { !\u2092 A})\ncomonand-diag\u2081 {U , X , \u03b1} =\n refl , ext-set (\u03bb {a} \u2192 ext-set (\u03bb {a\u2081} \u2192 aux {a\u2081}{a a\u2081}))\n where\n aux : \u2200{a\u2081 : U}{l : \ud835\udd43 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X))} \u2192\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) []\n (map (\u03bb g u \u2192 foldr (\u03bb f \u2192 _++_ (f u)) [] (g u)) l)\n \u2261\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] (foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] l)\n aux {a}{[]} = refl \n aux {a}{x :: l} rewrite\n sym (foldr-append {l\u2081 = x a}{foldr (\u03bb f \u2192 _++_ (f a)) [] l}{a})\n = cong2 {a = foldr (\u03bb f \u2192 _++_ (f a)) [] (x a)}\n _++_\n refl\n (aux {a}{l})\n\ncomonand-diag\u2082 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (\u03b5 { !\u2092 A}) \u2261h (\u03b4 {A}) \u25cb (!\u2090 (\u03b5 {A}))\ncomonand-diag\u2082 {U , X , \u03b1} =\n refl , ext-set (\u03bb {f} \u2192 ext-set (\u03bb {a} \u2192 aux {a}{f a}))\n where\n aux : \u2200{a : U}{l : X *}\n \u2192 l ++ [] \u2261 foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x y \u2192 x :: []) l)\n aux {a}{[]} = refl\n aux {a}{x :: l} with aux {a}{l}\n ... | IH rewrite ++[] l =\n cong2 {a = x} {x} {l}\n {foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x\u2081 y \u2192 x\u2081 :: []) l)} _::_ refl\n IH\n \nmodule Cartesian where\n \u03c0\u2081 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (U , X , \u03b1))\n \u03c0\u2081 {U}{X}{V}{Y}{\u03b1}{\u03b2} = fst , (\u03bb f \u2192 (\u03bb v u \u2192 f u) , (\u03bb u v \u2192 [])) , \u03c0\u2081-cond\n where\n \u03c0\u2081-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : U \u2192 \ud835\udd43 X} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 y u\u2081) , (\u03bb u\u2081 v \u2192 [])) \u2192\n all-pred (\u03b1 (fst u)) (y (fst u))\n \u03c0\u2081-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2081\n\n \u03c0\u2082 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (V , Y , \u03b2))\n \u03c0\u2082 {U}{X}{V}{Y}{\u03b1}{\u03b2} = snd , (\u03bb f \u2192 (\u03bb v u \u2192 []) , (\u03bb u v \u2192 f v)) , \u03c0\u2082-cond\n where\n \u03c0\u2082-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : V \u2192 \ud835\udd43 Y} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 []) , (\u03bb u\u2081 v \u2192 y v)) \u2192\n all-pred (\u03b2 (snd u)) (y (snd u))\n \u03c0\u2082-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2082\n\n cart-ar-crt : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y) \u2192 W \u2192 \ud835\udd43 Z\n cart-ar-crt (f , F , p\u2081) (g , G , p\u2082) (j\u2081 , j\u2082) w = F (j\u2081 (g w)) w ++ G (j\u2082 (f w)) w\n\n cart-ar : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))\n cart-ar {U}{X}{V}{Y}{W}{Z}{\u03b1}{\u03b2}{\u03b3} (f , F , p\u2081) (g , G , p\u2082)\n = (\u03bb w \u2192 f w , g w) , cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) , cart-ar-cond\n where\n cart-ar-cond : \u2200{u : W} {y : \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y)} \u2192\n all-pred (\u03b3 u) (cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) y u) \u2192\n ((\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b1 u\u2081) (f\u2081 u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b2 u\u2081) (f\u2081 u\u2081)))\n (f u , g u) y\n cart-ar-cond {w}{j\u2081 , j\u2082} p\n rewrite\n all-pred-append {f = \u03b3 w}{F (j\u2081 (g w)) w}{G (j\u2082 (f w)) w} \u2227-unit \u2227-assoc with p\n ... | (a , b) = p\u2081 a , p\u2082 b\n\n cart-diag\u2081 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (U , X , \u03b1)}\n (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (U , X , \u03b1)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2081)\n cart-diag\u2081 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 sym (++[] (map F (j (f w))))))\n\n cart-diag\u2082 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (V , Y , \u03b2)}\n (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (V , Y , \u03b2)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2082)\n cart-diag\u2082 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 refl))\n-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d2a91e9f9db12e5bb647ea91f7f5cb8b8d3dc42d","subject":"Still in progress of getting Finable to work under +","message":"Still in progress of getting Finable to work under +\n","repos":"crypto-agda\/crypto-agda","old_file":"sum-properties.agda","new_file":"sum-properties.agda","new_contents":"module sum-properties where\n\nopen import Type\n\nimport Level as L\n\nopen import Data.Bool.NP\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\n\nopen import Function.NP\n\nopen import sum\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_ ; _\u2257_ ; _\u2257\u2082_)\n\nsum-lem : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA f \u2261 sum \u03bcA (\u03bb x \u2192 f x \u2293 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2238 g x)\nsum-lem \u03bcA f g = \u2261.trans (sum-ext \u03bcA f\u2257f\u2293g+f\u2238g) (sum-hom \u03bcA (\u03bb x \u2192 f x \u2293 g x) (\u03bb x \u2192 f x \u2238 g x))\n where\n f\u2257f\u2293g+f\u2238g : f \u2257 (\u03bb x \u2192 f x \u2293 g x + (f x \u2238 g x))\n f\u2257f\u2293g+f\u2238g x = a\u2261a\u2293b+a\u2238b (f x) (g x)\n\nsum-lem\u2082 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA f + sum \u03bcA g \u2261 sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2293 g x)\nsum-lem\u2082 \u03bcA f g =\n sum \u03bcA f + sum \u03bcA g \u2261\u27e8 \u2261.sym (sum-hom \u03bcA f g) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x + g x) \u2261\u27e8 sum-ext \u03bcA (\u03bb x \u2192 lemma (f x) (g x)) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x \u2294 g x + f x \u2293 g x) \u2261\u27e8 sum-hom \u03bcA (\u03bb x \u2192 f x \u2294 g x) (\u03bb x \u2192 f x \u2293 g x) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2293 g x) \u220e\n where\n open \u2261.\u2261-Reasoning\n lemma : \u2200 a b \u2192 a + b \u2261 a \u2294 b + a \u2293 b\n lemma zero b rewrite \u2115\u00b0.+-comm b 0 = \u2261.refl\n lemma (suc a) zero = \u2261.refl\n lemma (suc a) (suc b) rewrite +-assoc-comm a 1 b\n | +-assoc-comm (a \u2294 b) 1 (a \u2293 b) = \u2261.cong (suc \u2218 suc) (lemma a b)\n\nto\u2115-\u2293 : \u2200 a b \u2192 to\u2115 a \u2293 to\u2115 b \u2261 to\u2115 (a \u2227 b)\nto\u2115-\u2293 true true = \u2261.refl\nto\u2115-\u2293 true false = \u2261.refl\nto\u2115-\u2293 false b = \u2261.refl\n\nto\u2115-\u2294 : \u2200 a b \u2192 to\u2115 a \u2294 to\u2115 b \u2261 to\u2115 (a \u2228 b)\nto\u2115-\u2294 true true = \u2261.refl\nto\u2115-\u2294 true false = \u2261.refl\nto\u2115-\u2294 false b = \u2261.refl\n\nto\u2115-\u2238 : \u2200 a b \u2192 to\u2115 a \u2238 to\u2115 b \u2261 to\u2115 (a \u2227 not b)\nto\u2115-\u2238 true true = \u2261.refl\nto\u2115-\u2238 true false = \u2261.refl\nto\u2115-\u2238 false true = \u2261.refl\nto\u2115-\u2238 false false = \u2261.refl\n\ncount-lem : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool)\n \u2192 count \u03bcA f \u2261 count \u03bcA (\u03bb x \u2192 f x \u2227 g x) + count \u03bcA (\u03bb x \u2192 f x \u2227 not (g x))\ncount-lem \u03bcA f g rewrite sum-lem \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g)\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2293 (f x) (g x)) \n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2238 (f x) (g x)) = \u2261.refl\n\ncount-lem\u2082 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool) \u2192 count \u03bcA f + count \u03bcA g \u2261 count \u03bcA (\u03bb x \u2192 f x \u2228 g x) + count \u03bcA (\u03bb x \u2192 f x \u2227 g x)\ncount-lem\u2082 \u03bcA f g rewrite sum-lem\u2082 \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g)\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2294 (f x) (g x))\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2293 (f x) (g x)) = \u2261.refl\n\n\nsum-\u2294 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) \u2264 sum \u03bcA f + sum \u03bcA g\nsum-\u2294 \u03bcA f g = \u2115\u2264.trans\n (sum-mono \u03bcA (\u03bb x \u2192 \u2294\u2264+ (f x) (g x)))\n (\u2115\u2264.reflexive (sum-hom \u03bcA f g))\n where\n \u2294\u2264+ : \u2200 a b \u2192 a \u2294 b \u2264 a + b\n \u2294\u2264+ zero b = \u2115\u2264.refl\n \u2294\u2264+ (suc a) zero = \u2115\u2264.reflexive (\u2261.cong suc (\u2115\u00b0.+-comm 0 a))\n \u2294\u2264+ (suc a) (suc b) = s\u2264s (\u2115\u2264.trans (\u2294\u2264+ a b) (\u2115\u2264.trans (\u2264-step \u2115\u2264.refl) (\u2115\u2264.reflexive (+-assoc-comm 1 a b))))\n\ncount-\u2228 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool) \u2192 count \u03bcA (\u03bb x \u2192 f x \u2228 g x) \u2264 count \u03bcA f + count \u03bcA g\ncount-\u2228 \u03bcA f g = \u2115\u2264.trans (\u2115\u2264.reflexive (sum-ext \u03bcA (\u03bb x \u2192 \u2261.sym (to\u2115-\u2294 (f x) (g x))))) \n (sum-\u2294 \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g))\n\n\nsum-ext\u2082 : \u2200 {A B}{f g : A \u2192 B \u2192 \u2115}(\u03bcA : SumProp A)(\u03bcB : SumProp B) \u2192 f \u2257\u2082 g \u2192 sum \u03bcA (sum \u03bcB \u2218 f) \u2261 sum \u03bcA (sum \u03bcB \u2218 g)\nsum-ext\u2082 \u03bcA \u03bcB f\u2257g = sum-ext \u03bcA (sum-ext \u03bcB \u2218 f\u2257g)\n\nInjective : \u2200 {a b}{A : Set a}{B : Set b}(f : A \u2192 B) \u2192 Set (a L.\u2294 b)\nInjective f = \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y\n\nStableUnderInjection : \u2200 {A} \u2192 SumProp A \u2192 Set\nStableUnderInjection \u03bc = \u2200 f p \u2192 Injective p \u2192 sum \u03bc f \u2261 sum \u03bc (f \u2218 p)\n\n#-StableUnderInjection : \u2200 {A}{\u03bc : SumProp A} \u2192 StableUnderInjection \u03bc\n \u2192 \u2200 f p \u2192 Injective p \u2192 count \u03bc f \u2261 count \u03bc (f \u2218 p)\n#-StableUnderInjection sui f p p-inj = sui (to\u2115 \u2218 f) p p-inj\n\nopen import Data.Fin using (Fin ; zero ; suc)\n\nrecord Finable {A : Set}(\u03bcA : SumProp A) : Set where\n constructor mk\n field\n FinCard : \u2115\n toFin : A \u2192 Fin FinCard\n fromFin : Fin FinCard \u2192 A\n to-inj : Injective toFin\n from-inj : Injective fromFin\n iso : fromFin \u2218 toFin \u2257 id\n sums-ok : \u2200 f \u2192 sum \u03bcA f \u2261 sumFin FinCard (f \u2218 fromFin)\n\npostulate\n Fin-Stab : \u2200 n \u2192 StableUnderInjection (\u03bcFin n)\n\n\u22a4-Finable : Finable \u03bc\u22a4\n\u22a4-Finable = mk 1 (\u03bb x \u2192 zero) (\u03bb x \u2192 _) (\u03bb x\u2081 \u2192 \u2261.refl) (\u03bb x\u2081 \u2192 help) (\u03bb x \u2192 \u2261.refl) (\u03bb f \u2192 ?)\n where help : {x y : Fin 1} \u2192 x \u2261 y\n help {zero} {zero} = \u2261.refl\n help {zero} {suc ()}\n help {suc ()}\n\n+-Finable : \u2200 {A B}(\u03bcA : SumProp A)(\u03bcB : SumProp B) \u2192 Finable \u03bcA \u2192 Finable \u03bcB \u2192 Finable (\u03bcA +\u03bc \u03bcB)\n+-Finable {A}{B} \u03bcA \u03bcB finA finB = mk FinCard toFin fromFin to-inj from-inj iso sums-ok where\n open import Data.Sum\n open import Data.Empty\n\n |A| = Finable.FinCard finA\n |B| = Finable.FinCard finB\n\n Fsuc-injective : \u2200 {n} {i j : Fin n} \u2192 Fin.suc i \u2261 suc j \u2192 i \u2261 j\n Fsuc-injective \u2261.refl = \u2261.refl\n \n inj\u2081-inj : \u2200 {A B : Set} {x y : A} \u2192 inj\u2081 {B = B} x \u2261 inj\u2081 y \u2192 x \u2261 y\n inj\u2081-inj \u2261.refl = \u2261.refl\n\n inj\u2082-inj : \u2200 {A B : Set} {x y : B} \u2192 inj\u2082 {A = A} x \u2261 inj\u2082 y \u2192 x \u2261 y\n inj\u2082-inj \u2261.refl = \u2261.refl\n\n fin\u2081 : \u2200 n {m} \u2192 Fin n \u2192 Fin (n + m)\n fin\u2081 zero ()\n fin\u2081 (suc n) zero = zero\n fin\u2081 (suc n) (suc i) = suc (fin\u2081 n i)\n\n fin\u2082 : \u2200 n {m} \u2192 Fin m \u2192 Fin (n + m)\n fin\u2082 zero i = i\n fin\u2082 (suc n) i = suc (fin\u2082 n i)\n\n toFin' : \u2200 {n} {m} \u2192 Fin n \u228e Fin m \u2192 Fin (n + m)\n toFin' {n} (inj\u2081 x) = fin\u2081 n x\n toFin' {n} (inj\u2082 y) = fin\u2082 n y\n\n fin\u2081-inj : \u2200 n {m} \u2192 Injective (fin\u2081 n {m})\n fin\u2081-inj .(suc n) {m} {zero {n}} {zero} x\u2261y = \u2261.refl\n fin\u2081-inj .(suc n) {m} {zero {n}} {suc y} ()\n fin\u2081-inj .(suc n) {m} {suc {n} x} {zero} ()\n fin\u2081-inj .(suc n) {m} {suc {n} x} {suc y} x\u2261y = \u2261.cong suc (fin\u2081-inj n (Fsuc-injective x\u2261y))\n\n fin\u2082-inj : \u2200 n {m} \u2192 Injective (fin\u2082 n {m})\n fin\u2082-inj zero eq = eq\n fin\u2082-inj (suc n) eq = fin\u2082-inj n (Fsuc-injective eq)\n\n fin\u2081\u2260fin\u2082 : \u2200 n {m} x y \u2192 (fin\u2081 n {m} x \u2261 fin\u2082 n y) \u2192 \u22a5\n fin\u2081\u2260fin\u2082 .(suc n) (zero {n}) zero ()\n fin\u2081\u2260fin\u2082 .(suc n) (zero {n}) (suc y) ()\n fin\u2081\u2260fin\u2082 .(suc n) (suc {n} x) zero eq = fin\u2081\u2260fin\u2082 n x zero (Fsuc-injective eq)\n fin\u2081\u2260fin\u2082 .(suc n) (suc {n} x) (suc y) eq = fin\u2081\u2260fin\u2082 n x (suc y) (Fsuc-injective eq)\n\n fromFin' : \u2200 n {m} \u2192 Fin (n + m) \u2192 Fin n \u228e Fin m\n fromFin' zero k = inj\u2082 k\n fromFin' (suc n) zero = inj\u2081 zero\n fromFin' (suc n) (suc k) with fromFin' n k\n ... | inj\u2081 x = inj\u2081 (suc x)\n ... | inj\u2082 y = inj\u2082 y\n\n fromFin'-inj : \u2200 n {m} x y \u2192 fromFin' n {m} x \u2261 fromFin' n y \u2192 x \u2261 y\n fromFin'-inj zero x y eq = inj\u2082-inj eq\n fromFin'-inj (suc n) zero zero eq = \u2261.refl\n fromFin'-inj (suc n) zero (suc y) eq = {!!}\n fromFin'-inj (suc n) (suc x) zero eq = {!!}\n fromFin'-inj (suc n) (suc x) (suc y) eq = {!fromFin'-inj n x y!}\n\n fromFin'-inj\u2081 : \u2200 n {m} x \u2192 fromFin' n {m} (fin\u2081 n x) \u2261 inj\u2081 x\n fromFin'-inj\u2081 zero ()\n fromFin'-inj\u2081 (suc n) zero = \u2261.refl\n fromFin'-inj\u2081 (suc n) {m} (suc x) rewrite fromFin'-inj\u2081 n {m} x = \u2261.refl\n\n fromFin'-inj\u2082 : \u2200 n {m} x \u2192 fromFin' n {m} (fin\u2082 n x) \u2261 inj\u2082 x\n fromFin'-inj\u2082 zero x = \u2261.refl\n fromFin'-inj\u2082 (suc n) x rewrite fromFin'-inj\u2082 n x = \u2261.refl\n\n FinCard : \u2115\n FinCard = Finable.FinCard finA + Finable.FinCard finB\n\n toFin : A \u228e B \u2192 Fin FinCard\n toFin (inj\u2081 x) = toFin' (inj\u2081 (Finable.toFin finA x))\n toFin (inj\u2082 y) = toFin' {n = |A|} (inj\u2082 (Finable.toFin finB y))\n\n fromFin : Fin FinCard \u2192 A \u228e B\n fromFin x+y with fromFin' |A| x+y\n ... | inj\u2081 x = inj\u2081 (Finable.fromFin finA x)\n ... | inj\u2082 y = inj\u2082 (Finable.fromFin finB y)\n\n to-inj : Injective toFin\n to-inj {inj\u2081 x} {inj\u2081 x\u2081} tx\u2261ty = \u2261.cong inj\u2081 (Finable.to-inj finA (fin\u2081-inj |A| tx\u2261ty))\n to-inj {inj\u2081 x} {inj\u2082 y} tx\u2261ty = \u22a5-elim (fin\u2081\u2260fin\u2082 |A| _ _ tx\u2261ty)\n to-inj {inj\u2082 y} {inj\u2081 x} tx\u2261ty = \u22a5-elim (fin\u2081\u2260fin\u2082 |A| _ _ (\u2261.sym tx\u2261ty))\n to-inj {inj\u2082 y} {inj\u2082 y\u2081} tx\u2261ty = \u2261.cong inj\u2082 (Finable.to-inj finB (fin\u2082-inj |A| tx\u2261ty))\n\n from-inj : Injective fromFin\n from-inj {x} {y} fx\u2261fy = {!!}\n\n iso : fromFin \u2218 toFin \u2257 id\n iso (inj\u2081 x) rewrite fromFin'-inj\u2081 |A| {|B|} (Finable.toFin finA x) | Finable.iso finA x = \u2261.refl\n iso (inj\u2082 y) rewrite fromFin'-inj\u2082 |A| {|B|} (Finable.toFin finB y) | Finable.iso finB y = \u2261.refl\n\n fin-proof : \u2200 n m (f : Fin n \u228e Fin m \u2192 \u2115) \u2192 sumFin n (f \u2218 inj\u2081) + sumFin m (f \u2218 inj\u2082) \u2261 sumFin (n + m) (f \u2218 fromFin' n)\n fin-proof zero m f = \u2261.refl\n fin-proof (suc n) m f = sumFin (suc n) (f \u2218 inj\u2081) + sumFin m (f \u2218 inj\u2082)\n \u2261\u27e8 \u2115\u00b0.+-assoc (f (inj\u2081 zero)) (sumFin n (f' f \u2218 inj\u2081))\n (sumFin m (f \u2218 inj\u2082)) \u27e9\n f (inj\u2081 zero) + (sumFin n (f' f \u2218 inj\u2081) + sumFin m (f \u2218 inj\u2082))\n \u2261\u27e8 \u2261.cong (_+_ (f (inj\u2081 zero))) (fin-proof n m (f' f)) \u27e9\n f (inj\u2081 zero) + (sumFin (n + m) (f' f \u2218 fromFin' n))\n \u2261\u27e8 \u2261.cong (\u03bb p \u2192 f (inj\u2081 zero) + vsum p)\n (tabulate\u2257 (\u03bb x \u2192 \u2261.sym (f'-proof n x f))) \u27e9\n sumFin (suc n + m) (f \u2218 fromFin' (suc n)) \n \u220e\n where open \u2261.\u2261-Reasoning\n open import Data.Vec renaming (sum to vsum)\n\n tabulate\u2257 : \u2200 {n} {A : Set}{f g : Fin n \u2192 A} \u2192 f \u2257 g \u2192 tabulate f \u2261 tabulate g\n tabulate\u2257 {zero} eq = \u2261.refl\n tabulate\u2257 {suc n\u2081} eq rewrite eq zero | tabulate\u2257 (eq \u2218 suc) = \u2261.refl\n\n f' : \u2200 {n m} \u2192 (Fin (suc n) \u228e Fin m \u2192 \u2115) \u2192 Fin n \u228e Fin m \u2192 \u2115\n f' f (inj\u2081 x) = f (inj\u2081 (suc x))\n f' f (inj\u2082 x) = f (inj\u2082 x)\n\n f'-proof : \u2200 n {m} x f \u2192 f (fromFin' (suc n) (suc x)) \u2261 f' {m = m} f (fromFin' n x)\n f'-proof n x f with fromFin' n x\n ... | inj\u2081 p = \u2261.refl\n ... | inj\u2082 p = \u2261.refl\n\n sums-ok : \u2200 f \u2192 sum (\u03bcA +\u03bc \u03bcB) f \u2261 sumFin FinCard (f \u2218 fromFin)\n sums-ok f =\n sum (\u03bcA +\u03bc \u03bcB) f\n \u2261\u27e8 \u2261.cong\u2082 _+_ (Finable.sums-ok finA (f \u2218 inj\u2081))\n (Finable.sums-ok finB (f \u2218 inj\u2082)) \u27e9\n sumFin (Finable.FinCard finA) (f \u2218 inj\u2081 \u2218 Finable.fromFin finA)\n + sumFin (Finable.FinCard finB) (f \u2218 inj\u2082 \u2218 Finable.fromFin finB)\n \u2261\u27e8 {!!} \u27e9\n sumFin FinCard (f \u2218 fromFin)\n \u220e\n where open \u2261.\u2261-Reasoning\n\nStableIfFinable : \u2200 {A} (\u03bcA : SumProp A) \u2192 Finable \u03bcA \u2192 StableUnderInjection \u03bcA\nStableIfFinable \u03bcA fin f p p-inj\n = sum \u03bcA f\n \u2261\u27e8 sums-ok f \u27e9\n sumFin FinCard (f \u2218 fromFin)\n \u2261\u27e8 Fin-Stab FinCard (f \u2218 fromFin) (toFin \u2218 p \u2218 fromFin) (from-inj \u2218 p-inj \u2218 to-inj) \u27e9\n sumFin FinCard (f \u2218 fromFin \u2218 toFin \u2218 p \u2218 fromFin)\n \u2261\u27e8 sum-ext (\u03bcFin FinCard) (\u03bb x \u2192 \u2261.cong f (iso (p (fromFin x)))) \u27e9\n sumFin FinCard (f \u2218 p \u2218 fromFin)\n \u2261\u27e8 \u2261.sym (sums-ok (f \u2218 p)) \u27e9\n sum \u03bcA (f \u2218 p)\n \u220e\n where open \u2261.\u2261-Reasoning\n open Finable fin\n\n\nmodule _ where\n open import bijection-fin\n open import Data.Vec.NP\n\n sumFinSUI : \u2200 n f p \u2192 Injective p \u2192 sumFin n f \u2261 sumFin n (f \u2218 p)\n sumFinSUI n f p p-inj = count-perm f p (\u03bb x y \u2192 p-inj)\n\n \u03bcFinSUI : \u2200 {n} \u2192 StableUnderInjection (\u03bcFin n)\n \u03bcFinSUI {n} f p p-inj rewrite \u2261.sym (sumFin-spec n f)\n | \u2261.sym (sumFin-spec n (f \u2218 p))\n = {!sumFin-spec n (f \u2218 p)!} -- {!sumFinSUI (suc n) f p p-inj!}\n\n","old_contents":"module sum-properties where\n\nopen import Type\n\nimport Level as L\n\nopen import Data.Bool.NP\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\n\nopen import Function.NP\n\nopen import sum\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_ ; _\u2257_ ; _\u2257\u2082_)\n\nsum-lem : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA f \u2261 sum \u03bcA (\u03bb x \u2192 f x \u2293 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2238 g x)\nsum-lem \u03bcA f g = \u2261.trans (sum-ext \u03bcA f\u2257f\u2293g+f\u2238g) (sum-hom \u03bcA (\u03bb x \u2192 f x \u2293 g x) (\u03bb x \u2192 f x \u2238 g x))\n where\n f\u2257f\u2293g+f\u2238g : f \u2257 (\u03bb x \u2192 f x \u2293 g x + (f x \u2238 g x))\n f\u2257f\u2293g+f\u2238g x = a\u2261a\u2293b+a\u2238b (f x) (g x)\n\nsum-lem\u2082 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA f + sum \u03bcA g \u2261 sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2293 g x)\nsum-lem\u2082 \u03bcA f g =\n sum \u03bcA f + sum \u03bcA g \u2261\u27e8 \u2261.sym (sum-hom \u03bcA f g) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x + g x) \u2261\u27e8 sum-ext \u03bcA (\u03bb x \u2192 lemma (f x) (g x)) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x \u2294 g x + f x \u2293 g x) \u2261\u27e8 sum-hom \u03bcA (\u03bb x \u2192 f x \u2294 g x) (\u03bb x \u2192 f x \u2293 g x) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2293 g x) \u220e\n where\n open \u2261.\u2261-Reasoning\n lemma : \u2200 a b \u2192 a + b \u2261 a \u2294 b + a \u2293 b\n lemma zero b rewrite \u2115\u00b0.+-comm b 0 = \u2261.refl\n lemma (suc a) zero = \u2261.refl\n lemma (suc a) (suc b) rewrite +-assoc-comm a 1 b\n | +-assoc-comm (a \u2294 b) 1 (a \u2293 b) = \u2261.cong (suc \u2218 suc) (lemma a b)\n\nto\u2115-\u2293 : \u2200 a b \u2192 to\u2115 a \u2293 to\u2115 b \u2261 to\u2115 (a \u2227 b)\nto\u2115-\u2293 true true = \u2261.refl\nto\u2115-\u2293 true false = \u2261.refl\nto\u2115-\u2293 false b = \u2261.refl\n\nto\u2115-\u2294 : \u2200 a b \u2192 to\u2115 a \u2294 to\u2115 b \u2261 to\u2115 (a \u2228 b)\nto\u2115-\u2294 true true = \u2261.refl\nto\u2115-\u2294 true false = \u2261.refl\nto\u2115-\u2294 false b = \u2261.refl\n\nto\u2115-\u2238 : \u2200 a b \u2192 to\u2115 a \u2238 to\u2115 b \u2261 to\u2115 (a \u2227 not b)\nto\u2115-\u2238 true true = \u2261.refl\nto\u2115-\u2238 true false = \u2261.refl\nto\u2115-\u2238 false true = \u2261.refl\nto\u2115-\u2238 false false = \u2261.refl\n\ncount-lem : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool)\n \u2192 count \u03bcA f \u2261 count \u03bcA (\u03bb x \u2192 f x \u2227 g x) + count \u03bcA (\u03bb x \u2192 f x \u2227 not (g x))\ncount-lem \u03bcA f g rewrite sum-lem \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g)\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2293 (f x) (g x)) \n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2238 (f x) (g x)) = \u2261.refl\n\ncount-lem\u2082 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool) \u2192 count \u03bcA f + count \u03bcA g \u2261 count \u03bcA (\u03bb x \u2192 f x \u2228 g x) + count \u03bcA (\u03bb x \u2192 f x \u2227 g x)\ncount-lem\u2082 \u03bcA f g rewrite sum-lem\u2082 \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g)\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2294 (f x) (g x))\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2293 (f x) (g x)) = \u2261.refl\n\n\nsum-\u2294 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) \u2264 sum \u03bcA f + sum \u03bcA g\nsum-\u2294 \u03bcA f g = \u2115\u2264.trans\n (sum-mono \u03bcA (\u03bb x \u2192 \u2294\u2264+ (f x) (g x)))\n (\u2115\u2264.reflexive (sum-hom \u03bcA f g))\n where\n \u2294\u2264+ : \u2200 a b \u2192 a \u2294 b \u2264 a + b\n \u2294\u2264+ zero b = \u2115\u2264.refl\n \u2294\u2264+ (suc a) zero = \u2115\u2264.reflexive (\u2261.cong suc (\u2115\u00b0.+-comm 0 a))\n \u2294\u2264+ (suc a) (suc b) = s\u2264s (\u2115\u2264.trans (\u2294\u2264+ a b) (\u2115\u2264.trans (\u2264-step \u2115\u2264.refl) (\u2115\u2264.reflexive (+-assoc-comm 1 a b))))\n\ncount-\u2228 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool) \u2192 count \u03bcA (\u03bb x \u2192 f x \u2228 g x) \u2264 count \u03bcA f + count \u03bcA g\ncount-\u2228 \u03bcA f g = \u2115\u2264.trans (\u2115\u2264.reflexive (sum-ext \u03bcA (\u03bb x \u2192 \u2261.sym (to\u2115-\u2294 (f x) (g x))))) \n (sum-\u2294 \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g))\n\n\nsum-ext\u2082 : \u2200 {A B}{f g : A \u2192 B \u2192 \u2115}(\u03bcA : SumProp A)(\u03bcB : SumProp B) \u2192 f \u2257\u2082 g \u2192 sum \u03bcA (sum \u03bcB \u2218 f) \u2261 sum \u03bcA (sum \u03bcB \u2218 g)\nsum-ext\u2082 \u03bcA \u03bcB f\u2257g = sum-ext \u03bcA (sum-ext \u03bcB \u2218 f\u2257g)\n\nInjective : \u2200 {a b}{A : Set a}{B : Set b}(f : A \u2192 B) \u2192 Set (a L.\u2294 b)\nInjective f = \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y\n\nStableUnderInjection : \u2200 {A} \u2192 SumProp A \u2192 Set\nStableUnderInjection \u03bc = \u2200 f p \u2192 Injective p \u2192 sum \u03bc f \u2261 sum \u03bc (f \u2218 p)\n\n#-StableUnderInjection : \u2200 {A}{\u03bc : SumProp A} \u2192 StableUnderInjection \u03bc\n \u2192 \u2200 f p \u2192 Injective p \u2192 count \u03bc f \u2261 count \u03bc (f \u2218 p)\n#-StableUnderInjection sui f p p-inj = sui (to\u2115 \u2218 f) p p-inj\n\nopen import Data.Fin using (Fin ; zero ; suc)\n\nrecord Finable {A : Set}(\u03bcA : SumProp A) : Set where\n constructor mk\n field\n FinCard : \u2115\n toFin : A \u2192 Fin (suc FinCard)\n fromFin : Fin (suc FinCard) \u2192 A\n to-inj : Injective toFin\n from-inj : Injective fromFin\n iso : fromFin \u2218 toFin \u2257 id\n sums-ok : \u2200 f \u2192 sum \u03bcA f \u2261 sum (\u03bcFin FinCard) (f \u2218 fromFin)\n\npostulate\n Fin-Stab : \u2200 n \u2192 StableUnderInjection (\u03bcFin n)\n\n\u22a4-Finable : Finable \u03bc\u22a4\n\u22a4-Finable = mk 0 (\u03bb x \u2192 zero) (\u03bb x \u2192 _) (\u03bb x\u2081 \u2192 \u2261.refl) (\u03bb x\u2081 \u2192 help) (\u03bb x \u2192 \u2261.refl) (\u03bb f \u2192 \u2261.refl) \n where help : {x y : Fin 1} \u2192 x \u2261 y\n help {zero} {zero} = \u2261.refl\n help {zero} {suc ()}\n help {suc ()}\n\n+-Finable : \u2200 {A B}(\u03bcA : SumProp A)(\u03bcB : SumProp B) \u2192 Finable \u03bcA \u2192 Finable \u03bcB \u2192 Finable (\u03bcA +\u03bc \u03bcB)\n+-Finable {A}{B} \u03bcA \u03bcB finA finB = mk FinCard toFin fromFin to-inj from-inj iso sums-ok where\n open import Data.Sum\n open import Data.Empty\n\n |A| = suc (Finable.FinCard finA)\n |B| = suc (Finable.FinCard finB)\n\n Fsuc-injective : \u2200 {n} {i j : Fin n} \u2192 Fin.suc i \u2261 suc j \u2192 i \u2261 j\n Fsuc-injective \u2261.refl = \u2261.refl\n \n inj\u2081-inj : \u2200 {A B : Set} {x y : A} \u2192 inj\u2081 {B = B} x \u2261 inj\u2081 y \u2192 x \u2261 y\n inj\u2081-inj \u2261.refl = \u2261.refl\n\n inj\u2082-inj : \u2200 {A B : Set} {x y : B} \u2192 inj\u2082 {A = A} x \u2261 inj\u2082 y \u2192 x \u2261 y\n inj\u2082-inj \u2261.refl = \u2261.refl\n\n fin\u2081 : \u2200 n {m} \u2192 Fin n \u2192 Fin (n + m)\n fin\u2081 zero ()\n fin\u2081 (suc n) zero = zero\n fin\u2081 (suc n) (suc i) = suc (fin\u2081 n i)\n\n fin\u2082 : \u2200 n {m} \u2192 Fin m \u2192 Fin (n + m)\n fin\u2082 zero i = i\n fin\u2082 (suc n) i = suc (fin\u2082 n i)\n\n toFin' : \u2200 {n} {m} \u2192 Fin n \u228e Fin m \u2192 Fin (n + m)\n toFin' {n} (inj\u2081 x) = fin\u2081 n x\n toFin' {n} (inj\u2082 y) = fin\u2082 n y\n\n fin\u2081-inj : \u2200 n {m} \u2192 Injective (fin\u2081 n {m})\n fin\u2081-inj .(suc n) {m} {zero {n}} {zero} x\u2261y = \u2261.refl\n fin\u2081-inj .(suc n) {m} {zero {n}} {suc y} ()\n fin\u2081-inj .(suc n) {m} {suc {n} x} {zero} ()\n fin\u2081-inj .(suc n) {m} {suc {n} x} {suc y} x\u2261y = \u2261.cong suc (fin\u2081-inj n (Fsuc-injective x\u2261y))\n\n fin\u2082-inj : \u2200 n {m} \u2192 Injective (fin\u2082 n {m})\n fin\u2082-inj zero eq = eq\n fin\u2082-inj (suc n) eq = fin\u2082-inj n (Fsuc-injective eq)\n\n fin\u2081\u2260fin\u2082 : \u2200 n {m} x y \u2192 (fin\u2081 n {m} x \u2261 fin\u2082 n y) \u2192 \u22a5\n fin\u2081\u2260fin\u2082 .(suc n) (zero {n}) zero ()\n fin\u2081\u2260fin\u2082 .(suc n) (zero {n}) (suc y) ()\n fin\u2081\u2260fin\u2082 .(suc n) (suc {n} x) zero eq = fin\u2081\u2260fin\u2082 n x zero (Fsuc-injective eq)\n fin\u2081\u2260fin\u2082 .(suc n) (suc {n} x) (suc y) eq = fin\u2081\u2260fin\u2082 n x (suc y) (Fsuc-injective eq)\n\n fromFin' : \u2200 n {m} \u2192 Fin (n + m) \u2192 Fin n \u228e Fin m\n fromFin' zero k = inj\u2082 k\n fromFin' (suc n) zero = inj\u2081 zero\n fromFin' (suc n) (suc k) with fromFin' n k\n ... | inj\u2081 x = inj\u2081 (suc x)\n ... | inj\u2082 y = inj\u2082 y\n\n fromFin'-inj : \u2200 n {m} x y \u2192 fromFin' n {m} x \u2261 fromFin' n y \u2192 x \u2261 y\n fromFin'-inj zero x y eq = inj\u2082-inj eq\n fromFin'-inj (suc n) zero zero eq = \u2261.refl\n fromFin'-inj (suc n) zero (suc y) eq = {!!}\n fromFin'-inj (suc n) (suc x) zero eq = {!!}\n fromFin'-inj (suc n) (suc x) (suc y) eq = {!fromFin'-inj n x y!}\n\n fromFin'-inj\u2081 : \u2200 n {m} x \u2192 fromFin' n {m} (fin\u2081 n x) \u2261 inj\u2081 x\n fromFin'-inj\u2081 zero ()\n fromFin'-inj\u2081 (suc n) zero = \u2261.refl\n fromFin'-inj\u2081 (suc n) {m} (suc x) rewrite fromFin'-inj\u2081 n {m} x = \u2261.refl\n\n fromFin'-inj\u2082 : \u2200 n {m} x \u2192 fromFin' n {m} (fin\u2082 n x) \u2261 inj\u2082 x\n fromFin'-inj\u2082 zero x = \u2261.refl\n fromFin'-inj\u2082 (suc n) x rewrite fromFin'-inj\u2082 n x = \u2261.refl\n\n FinCard : \u2115\n FinCard = Finable.FinCard finA + suc (Finable.FinCard finB)\n\n toFin : A \u228e B \u2192 Fin (suc FinCard)\n toFin (inj\u2081 x) = toFin' (inj\u2081 (Finable.toFin finA x))\n toFin (inj\u2082 y) = toFin' {n = |A|} (inj\u2082 (Finable.toFin finB y))\n\n fromFin : Fin (suc FinCard) \u2192 A \u228e B\n fromFin x+y with fromFin' |A| x+y\n ... | inj\u2081 x = inj\u2081 (Finable.fromFin finA x)\n ... | inj\u2082 y = inj\u2082 (Finable.fromFin finB y)\n\n to-inj : Injective toFin\n to-inj {inj\u2081 x} {inj\u2081 x\u2081} tx\u2261ty = \u2261.cong inj\u2081 (Finable.to-inj finA (fin\u2081-inj |A| tx\u2261ty))\n to-inj {inj\u2081 x} {inj\u2082 y} tx\u2261ty = \u22a5-elim (fin\u2081\u2260fin\u2082 |A| _ _ tx\u2261ty)\n to-inj {inj\u2082 y} {inj\u2081 x} tx\u2261ty = \u22a5-elim (fin\u2081\u2260fin\u2082 |A| _ _ (\u2261.sym tx\u2261ty))\n to-inj {inj\u2082 y} {inj\u2082 y\u2081} tx\u2261ty = \u2261.cong inj\u2082 (Finable.to-inj finB (fin\u2082-inj |A| tx\u2261ty))\n\n from-inj : Injective fromFin\n from-inj {x} {y} fx\u2261fy = {!!}\n\n iso : fromFin \u2218 toFin \u2257 id\n iso (inj\u2081 x) rewrite fromFin'-inj\u2081 |A| {|B|} (Finable.toFin finA x) | Finable.iso finA x = \u2261.refl\n iso (inj\u2082 y) rewrite fromFin'-inj\u2082 |A| {|B|} (Finable.toFin finB y) | Finable.iso finB y = \u2261.refl\n\n sums-ok : \u2200 f \u2192 sum (\u03bcA +\u03bc \u03bcB) f \u2261 sum (\u03bcFin FinCard) (f \u2218 fromFin)\n sums-ok f =\n sum (\u03bcA +\u03bc \u03bcB) f\n \u2261\u27e8 \u2261.cong\u2082 _+_ (Finable.sums-ok finA (f \u2218 inj\u2081))\n (Finable.sums-ok finB (f \u2218 inj\u2082)) \u27e9\n sum (\u03bcFin (Finable.FinCard finA)) (f \u2218 inj\u2081 \u2218 Finable.fromFin finA)\n + sum (\u03bcFin (Finable.FinCard finB)) (f \u2218 inj\u2082 \u2218 Finable.fromFin finB)\n \u2261\u27e8 {!!} \u27e9\n sum (\u03bcFin FinCard) (f \u2218 fromFin)\n \u220e\n where open \u2261.\u2261-Reasoning\n\nStableIfFinable : \u2200 {A} (\u03bcA : SumProp A) \u2192 Finable \u03bcA \u2192 StableUnderInjection \u03bcA\nStableIfFinable \u03bcA fin f p p-inj\n = sum \u03bcA f\n \u2261\u27e8 sums-ok f \u27e9\n sum (\u03bcFin FinCard) (f \u2218 fromFin)\n \u2261\u27e8 Fin-Stab FinCard (f \u2218 fromFin) (toFin \u2218 p \u2218 fromFin) (from-inj \u2218 p-inj \u2218 to-inj) \u27e9\n sum (\u03bcFin FinCard) (f \u2218 fromFin \u2218 toFin \u2218 p \u2218 fromFin)\n \u2261\u27e8 sum-ext (\u03bcFin FinCard) (\u03bb x \u2192 \u2261.cong f (iso (p (fromFin x)))) \u27e9\n sum (\u03bcFin FinCard) (f \u2218 p \u2218 fromFin)\n \u2261\u27e8 \u2261.sym (sums-ok (f \u2218 p)) \u27e9\n sum \u03bcA (f \u2218 p)\n \u220e\n where open \u2261.\u2261-Reasoning\n open Finable fin\n\n\nmodule _ where\n open import bijection-fin\n open import Data.Vec.NP\n\n sumFinSUI : \u2200 n f p \u2192 Injective p \u2192 sumFin n f \u2261 sumFin n (f \u2218 p)\n sumFinSUI n f p p-inj = count-perm f p (\u03bb x y \u2192 p-inj)\n\n \u03bcFinSUI : \u2200 {n} \u2192 StableUnderInjection (\u03bcFin n)\n \u03bcFinSUI {n} f p p-inj rewrite \u2261.sym (sumFin-spec n f)\n | \u2261.sym (sumFin-spec n (f \u2218 p))\n = sumFinSUI (suc n) f p p-inj\n\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"5b2b91ed59added92ff1db3f16b2ed90b51a8fbf","subject":"Desc stratified model: painful implicitification of IDescl constructors","message":"Desc stratified model: painful implicitification of IDescl constructors","repos":"kwangkim\/pigment,brixen\/Epigram,kwangkim\/pigment,kwangkim\/pigment","old_file":"models\/IDesc.agda","new_file":"models\/IDesc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l : Level}{A B : Set l}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l : Level}{A B C : Set l}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {l : Level}{A B : Set l}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set l) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set l} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set l}(R : I -> IDesc I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set l}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set l -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const (lift I)\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : {l : Level}(I : Set l) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : {l : Level}(I : Set l) -> Set (suc l)\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : {l : Level}{I : Set l}(i : I) -> IDescl I\nvarl {x} i = con (lv , lifter i) \n where lv : DescDConst {l = suc x}\n lv = lvar\n\nconstl : {l : Level}{I : Set l}(X : Set l) -> IDescl I\nconstl {x} X = con (lc , X)\n where lc : DescDConst {l = suc x}\n lc = lconst\n\nprodl : {l : Level}{I : Set l}(D D' : IDescl I) -> IDescl I\nprodl {x} D D' = con (lp , (D , D'))\n where lp : DescDConst {l = suc x}\n lp = lprod\n\n\npil : {l : Level}{I : Set l}(S : Set l)(T : S -> IDescl I) -> IDescl I\npil {x} S T = con (lp , ( S , Tl))\n where lp : DescDConst {l = suc x}\n lp = lpi\n Tl : Lifted S -> IDescl _\n Tl (lifter s) = T s\n\nsigmal : {l : Level}{I : Set l}(S : Set l)(T : S -> IDescl I) -> IDescl I\nsigmal {x} S T = con (ls , ( S , Tl))\n where ls : DescDConst {l = suc x}\n ls = lsigma\n Tl : Lifted S -> IDescl _\n Tl (lifter s) = T s\n \n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\n-- desc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\n-- descD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\n-- IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l\n\n-- cases : (l : Level)\n-- (I : Set l)\n-- (xs : desc (suc l) (lift I) (descD l I) (IMu (suc l) Unit (\\ _ -> descD l I)))\n-- (hs : desc l I (box l I (descD l I) (IMu l Unit (\u03bb _ -> descD l I)) xs) (\u03bb _ -> IDesc (suc l) I)) ->\n-- IDesc (suc l) I\n-- cases x I ( lvar , i ) hs = var i\n-- cases x I ( lconst , X ) hs = const X\n-- cases x I ( lprod , (D , D') ) ( d , d' ) = prod d d'\n-- cases x I ( lpi , ( S , T ) ) hs = pi S hs\n-- cases x I ( lsigma , ( S , T ) ) hs = sigma S hs\n\n-- phi : {I : Set} -> IDescl I -> IDesc I\n-- phi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l : Level}{A B : Set l}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l : Level}{A B C : Set l}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {l : Level}{A B : Set l}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set l) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set l} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set l}(R : I -> IDesc I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set l}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set l -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const (lift I)\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : {l : Level}(I : Set l) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : {l : Level}(I : Set l) -> Set (suc l)\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst {l = suc x}\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl I\nconstl x I X = con (lc , X)\n where lc : DescDConst {l = suc x}\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl I) -> IDescl I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst {l = suc x}\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl I) -> IDescl I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst {l = suc x}\n lp = lpi\n Tl : Lifted S -> IDescl I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl I) -> IDescl I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst {l = suc x}\n ls = lsigma\n Tl : Lifted S -> IDescl I\n Tl (lifter s) = T s\n \n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\n-- desc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\n-- descD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\n-- IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l\n\n-- cases : (l : Level)\n-- (I : Set l)\n-- (xs : desc (suc l) (lift I) (descD l I) (IMu (suc l) Unit (\\ _ -> descD l I)))\n-- (hs : desc l I (box l I (descD l I) (IMu l Unit (\u03bb _ -> descD l I)) xs) (\u03bb _ -> IDesc (suc l) I)) ->\n-- IDesc (suc l) I\n-- cases x I ( lvar , i ) hs = var i\n-- cases x I ( lconst , X ) hs = const X\n-- cases x I ( lprod , (D , D') ) ( d , d' ) = prod d d'\n-- cases x I ( lpi , ( S , T ) ) hs = pi S hs\n-- cases x I ( lsigma , ( S , T ) ) hs = sigma S hs\n\n-- phi : {I : Set} -> IDescl I -> IDesc I\n-- phi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"6b984ce04f570d4e3d656e2a27f2ebd3ff9e3d3f","subject":"Added doc about the conversion rules.","message":"Added doc about the conversion rules.\n\nIgnore-this: c2bffa7d9902f27b0a6892bded45551a\n\ndarcs-hash:20111005211320-3bd4e-56b7013f3a6d7a01bf747a11474b16d00af46d6d.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Base.agda","new_file":"src\/FOTC\/Base.agda","new_contents":"------------------------------------------------------------------------------\n-- The first order theory of combinators (FOTC) base\n------------------------------------------------------------------------------\n\n{-\nFOTC Agda\n* Logical constants * Curry-Howard isomorphism\n* Equality * Identity type\n* Term language and conversion rules * Postulates\n* Inductive predicates * Inductive families\n-}\n\nmodule FOTC.Base where\n\ninfixl 9 _\u00b7_ -- The symbol is '\\cdot'.\ninfixr 8 _\u2237_ -- We add 3 to the fixities of the standard library.\ninfix 8 if_then_else_\n\n------------------------------------------------------------------------------\n-- The universal domain.\nopen import Common.Universe public\n\n-- The FOTC equality\n-- The FOTC equality is the propositional identity on the universal domain.\nimport Common.Relation.Binary.PropositionalEquality\nopen module Eq =\n Common.Relation.Binary.PropositionalEquality.NonInductive public\n\n-- Logical constants\nopen import Common.LogicalConstants public\n\n------------------------------------------------------------------------------\n-- The term language of FOTC correspond to the PCF terms.\n\n-- t ::= x | t t |\n-- | true | false | if t then t else t\n-- | 0 | succ t | pred t | isZero t\n-- | [] | _\u2237_ | null | head | tail\n-- | loop\n\npostulate\n\n -- FOTC partial booleans.\n true : D\n false : D\n if_then_else_ : D \u2192 D \u2192 D \u2192 D\n\n -- FOTC partial natural numbers.\n zero : D\n succ : D \u2192 D\n pred : D \u2192 D\n isZero : D \u2192 D\n\n -- FOTC application.\n _\u00b7_ : D \u2192 D \u2192 D\n\n -- FOTC lists.\n [] : D\n _\u2237_ : D \u2192 D \u2192 D\n null : D \u2192 D\n head : D \u2192 D\n tail : D \u2192 D\n\n -- FOTC looping programs.\n loop : D\n\n------------------------------------------------------------------------------\n-- Conversion rules\n\n-- Note: The conversion relation _conv_ satifies (Aczel 1977. The\n-- strength of Martin-L\u00f6f's intuitionistic type theory with one\n-- universe, p. 8).\n--\n-- x conv y <=> FOTC \u22a2 x \u2261 y,\n--\n-- therefore, we introduce the conversion rules as FOL non-logical\n-- axioms.\n\n-- Conversion rules for booleans.\npostulate\n if-true : \u2200 d\u2081 {d\u2082} \u2192 if true then d\u2081 else d\u2082 \u2261 d\u2081\n if-false : \u2200 {d\u2081} d\u2082 \u2192 if false then d\u2081 else d\u2082 \u2261 d\u2082\n{-# ATP axiom if-true #-}\n{-# ATP axiom if-false #-}\n\n-- Conversion rules for pred.\npostulate\n -- N.B. We don't need this equation.\n -- pred-0 : pred zero \u2261 zero\n pred-S : \u2200 d \u2192 pred (succ d) \u2261 d\n{-# ATP axiom pred-S #-}\n\n-- Conversion rules for isZero.\npostulate\n isZero-0 : isZero zero \u2261 true\n isZero-S : \u2200 d \u2192 isZero (succ d) \u2261 false\n{-# ATP axiom isZero-0 #-}\n{-# ATP axiom isZero-S #-}\n\n-- Conversion rules for null.\npostulate\n null-[] : null [] \u2261 true\n null-\u2237 : \u2200 x xs \u2192 null (x \u2237 xs) \u2261 false\n\n-- Conversion rule for head.\npostulate head-\u2237 : \u2200 x xs \u2192 head (x \u2237 xs) \u2261 x\n{-# ATP axiom head-\u2237 #-}\n\n-- Conversion rule for tail.\npostulate tail-\u2237 : \u2200 x xs \u2192 tail (x \u2237 xs) \u2261 xs\n{-# ATP axiom tail-\u2237 #-}\n\n-- Conversion rule for loop.\n-- The equation loop-eq adds anything to the logic (because\n-- reflexivity is already an axiom of equality), therefore we won't\n-- add this equation as a FOL axiom.\npostulate loop-eq : loop \u2261 loop\n\n------------------------------------------------------------------------------\n-- Discrimination rules\n\npostulate\n true\u2260false : \u00ac (true \u2261 false)\n 0\u2260S : \u2200 {d} \u2192 \u00ac (zero \u2261 succ d)\n{-# ATP axiom true\u2260false #-}\n{-# ATP axiom 0\u2260S #-}\n","old_contents":"------------------------------------------------------------------------------\n-- The first order theory of combinators (FOTC) base\n------------------------------------------------------------------------------\n\n{-\nFOTC Agda\n* Logical constants * Curry-Howard isomorphism\n* Equality * Identity type\n* Term language and conversion rules * Postulates\n* Inductive predicates * Inductive families\n-}\n\nmodule FOTC.Base where\n\ninfixl 9 _\u00b7_ -- The symbol is '\\cdot'.\ninfixr 8 _\u2237_ -- We add 3 to the fixities of the standard library.\ninfix 8 if_then_else_\n\n------------------------------------------------------------------------------\n-- The universal domain.\nopen import Common.Universe public\n\n-- The FOTC equality\n-- The FOTC equality is the propositional identity on the universal domain.\nimport Common.Relation.Binary.PropositionalEquality\nopen module Eq =\n Common.Relation.Binary.PropositionalEquality.NonInductive public\n\n-- Logical constants\nopen import Common.LogicalConstants public\n\n------------------------------------------------------------------------------\n-- The term language of FOTC correspond to the PCF terms.\n\n-- t ::= x | t t |\n-- | true | false | if t then t else t\n-- | 0 | succ t | pred t | isZero t\n-- | [] | _\u2237_ | null | head | tail\n-- | loop\n\npostulate\n\n -- FOTC partial booleans.\n true : D\n false : D\n if_then_else_ : D \u2192 D \u2192 D \u2192 D\n\n -- FOTC partial natural numbers.\n zero : D\n succ : D \u2192 D\n pred : D \u2192 D\n isZero : D \u2192 D\n\n -- FOTC application.\n _\u00b7_ : D \u2192 D \u2192 D\n\n -- FOTC lists.\n [] : D\n _\u2237_ : D \u2192 D \u2192 D\n null : D \u2192 D\n head : D \u2192 D\n tail : D \u2192 D\n\n -- FOTC looping programs.\n loop : D\n\n------------------------------------------------------------------------------\n-- Conversion rules\n\n-- Conversion rules for booleans.\npostulate\n if-true : \u2200 d\u2081 {d\u2082} \u2192 if true then d\u2081 else d\u2082 \u2261 d\u2081\n if-false : \u2200 {d\u2081} d\u2082 \u2192 if false then d\u2081 else d\u2082 \u2261 d\u2082\n{-# ATP axiom if-true #-}\n{-# ATP axiom if-false #-}\n\n-- Conversion rules for pred.\npostulate\n -- N.B. We don't need this equation.\n -- pred-0 : pred zero \u2261 zero\n pred-S : \u2200 d \u2192 pred (succ d) \u2261 d\n{-# ATP axiom pred-S #-}\n\n-- Conversion rules for isZero.\npostulate\n isZero-0 : isZero zero \u2261 true\n isZero-S : \u2200 d \u2192 isZero (succ d) \u2261 false\n{-# ATP axiom isZero-0 #-}\n{-# ATP axiom isZero-S #-}\n\n-- Conversion rules for null.\npostulate\n null-[] : null [] \u2261 true\n null-\u2237 : \u2200 x xs \u2192 null (x \u2237 xs) \u2261 false\n\n-- Conversion rule for head.\npostulate head-\u2237 : \u2200 x xs \u2192 head (x \u2237 xs) \u2261 x\n{-# ATP axiom head-\u2237 #-}\n\n-- Conversion rule for tail.\npostulate tail-\u2237 : \u2200 x xs \u2192 tail (x \u2237 xs) \u2261 xs\n{-# ATP axiom tail-\u2237 #-}\n\n-- Conversion rule for loop.\n-- The equation loop-eq adds anything to the logic (because\n-- reflexivity is already an axiom of equality), therefore we won't\n-- add this equation as a FOL axiom.\npostulate loop-eq : loop \u2261 loop\n\n------------------------------------------------------------------------------\n-- Discrimination rules\n\npostulate\n true\u2260false : \u00ac (true \u2261 false)\n 0\u2260S : \u2200 {d} \u2192 \u00ac (zero \u2261 succ d)\n{-# ATP axiom true\u2260false #-}\n{-# ATP axiom 0\u2260S #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"22450b7d49eb76b754ec41272ab5f116d5c52d5c","subject":"A variable does not change in an honest environment that says so.","message":"A variable does not change in an honest environment that says so.\n\nOld-commit-hash: 58a997c1f5014000ba469de18ca3018c0d55d9fd\n","repos":"inc-lc\/ilc-agda","old_file":"experimental\/ExplicitNil.agda","new_file":"experimental\/ExplicitNil.agda","new_contents":"{-\nCommunicate to derivatives that changes to certain arguments\nare always nil (i. e., certain arguments are stable).\n-}\n\nmodule ExplicitNil where\n\nopen import NatBag\n\nopen import Data.Bool\nopen import Data.Product hiding (map)\nopen import Data.Nat using (\u2115)\nopen import Data.Unit using (\u22a4 ; tt)\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\n-- Debug tool\nabsurd! : \u2200 {A B : Set} \u2192 B \u2192 A \u2192 A \u2192 B\nabsurd! b _ _ = b\n\n\ndata Args : (\u03c4 : Type) \u2192 Set where\n \u2205-nat : Args nats\n \u2205-bag : Args bags\n abide : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192 (args : Args \u03c4\u2082) \u2192 Args (\u03c4\u2081 \u21d2 \u03c4\u2082)\n alter : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192 (args : Args \u03c4\u2082) \u2192 Args (\u03c4\u2081 \u21d2 \u03c4\u2082)\n\n-- Totally like subcontext relation _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set\ndata Vars : Context \u2192 Set where\n \u2205 : Vars \u2205\n abide : \u2200 {\u03c4 \u0393} \u2192 Vars \u0393 \u2192 Vars (\u03c4 \u2022 \u0393) -- is in the set\n alter : \u2200 {\u03c4 \u0393} \u2192 Vars \u0393 \u2192 Vars (\u03c4 \u2022 \u0393) -- is out of the set\n\nstableVar : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Bool\nstableVar this (abide _) = true\nstableVar this (alter _) = false\nstableVar (that x) (abide vars) = stableVar x vars\nstableVar (that x) (alter vars) = stableVar x vars\n\n-- A term is stable if all its free variables are unchanging\n-- Alternative definition:\n--\n-- stable t vars = isNil t (derive t)\n--\nstable : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Bool\nstable (nat n) vars = true\nstable (bag b) vars = true\nstable (var x) vars = stableVar x vars\nstable (abs t) vars = stable t (abide vars)\nstable (app f x) vars = stable f vars \u2227 stable x vars\nstable (add m n) vars = stable m vars \u2227 stable n vars\nstable (map f b) vars = stable f vars \u2227 stable b vars\nstable (diff b d) vars = stable b vars \u2227 stable d vars\nstable (union b d) vars = stable b vars \u2227 stable d vars\n\n{-\n-- Reformulating stableness as a decidable relation\n-- Not sure if it is necessary or not.\ndata StableVar : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Set where\n abide-this : \u2200 {\u03c4 \u0393} \u2192 {vars : Vars \u0393} \u2192 StableVar this (abide {\u03c4} vars)\n abide-that : \u2200 {\u03c4 \u0393 \u03c4\u2032} \u2192 {x : Var \u0393 \u03c4} \u2192 {vars : Vars \u0393} \u2192\n StableVar x vars \u2192 StableVar (that {\u03c4} {\u03c4\u2032} x) (abide vars)\n alter-that : \u2200 {\u03c4 \u0393 \u03c4\u2032} \u2192 {x : Var \u0393 \u03c4} \u2192 {vars : Vars \u0393} \u2192\n StableVar x vars \u2192 StableVar (that {\u03c4} {\u03c4\u2032} x) (alter vars)\n\ndata Stable : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Set where\n stable-nat : \u2200 {\u0393 n vars} \u2192 Stable {nats} {\u0393} (nat n) vars\n stable-bag : \u2200 {\u0393 b vars} \u2192 Stable {bags} {\u0393} (bag b) vars\n stable-var : \u2200 {\u03c4 \u0393 x vars} \u2192\n StableVar {\u03c4} {\u0393} x vars \u2192 Stable (var x) vars\n stable-abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393 vars} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n Stable t (abide vars) \u2192 Stable (abs t) vars\n -- TODO app add map diff union\n-}\n\nexpect-volatility : {\u03c4 : Type} \u2192 Args \u03c4\nexpect-volatility {\u03c4\u2081 \u21d2 \u03c4\u2082} = alter expect-volatility\nexpect-volatility {nats} = \u2205-nat\nexpect-volatility {bags} = \u2205-bag\n\n-- Type of `derive'`\nderive' : \u2200 {\u03c4 \u0393} \u2192 Args \u03c4 \u2192 Vars \u0393 \u2192\n Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\nderive' {\u03c4\u2081 \u21d2 \u03c4\u2082} (abide args) vars (abs t) =\n abs (abs (derive' args (abide vars) t))\n\nderive' (alter args) vars (abs t) =\n abs (abs (derive' args (alter vars) t))\n\n-- Assume, for safety, that all arguments that `t` will\n-- eventually receive in `s` or receive curried out of `s`\n-- are volatile.\nderive' args vars (app s t) =\n if stable t vars\n then app (app (derive' (abide args) vars s) (weaken \u0393\u227c\u0394\u0393 t))\n (derive' expect-volatility vars t)\n else app (app (derive' (alter args) vars s) (weaken \u0393\u227c\u0394\u0393 t))\n (derive' expect-volatility vars t)\n\nderive' args vars (map f b) =\n if stable f vars\n then map (weaken \u0393\u227c\u0394\u0393 f) (derive' args vars b)\n else map (weaken \u0393\u227c\u0394\u0393 f \u2295 derive' (abide \u2205-nat) vars f)\n (weaken \u0393\u227c\u0394\u0393 b \u2295 derive' args vars b)\n \u229d weaken \u0393\u227c\u0394\u0393 (map f b)\n\nderive' args vars (diff b d) =\n diff (derive' args vars b) (derive' args vars d)\n\nderive' args vars (union b d) =\n union (derive' args vars b) (derive' args vars d)\n\n-- derive(x + y) = replace (x + y) by (dx snd + dy snd)\n-- = \u03bb f. f (x + y) (dx snd + dy snd)\nderive' args vars (add m n) =\n abs (app (app (var this)\n (add (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) m) (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) n)))\n (add (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive' args vars m)) snd)\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive' args vars n)) snd)))\n\nderive' args vars constant-or-variable = derive constant-or-variable\n\n-- A description of variables is honest w.r.t. a \u0394-environment\n-- if every variable described as stable receives the nil change.\ndata Honest : \u2200 {\u0393} \u2192 Vars \u0393 \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 Set where\n clearly : Honest \u2205 \u2205\n alter : \u2200 {\u0393 \u03c4 v dv} \u2192 {vars : Vars \u0393} \u2192 {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Honest {\u0393} vars \u03c1 \u2192\n Honest {\u03c4 \u2022 \u0393} (alter vars) (dv \u2022 v \u2022 \u03c1)\n abide : \u2200 {\u0393 \u03c4 v dv} \u2192 {vars : Vars \u0393} \u2192 {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n v \u27e6\u2295\u27e7 dv \u2261 v \u2192\n Honest {\u0393} vars \u03c1 \u2192\n Honest {\u03c4 \u2022 \u0393} (abide vars) (dv \u2022 v \u2022 \u03c1)\n\n-- Two \u0394-values are close enough w.r.t. a set of arguments if they\n-- behave the same when fully applied (cf. extensionality) given\n-- that each argument declared stable receives the nil change.\nclose-enough : \u2200 {\u03c4 : Type} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Args \u03c4 \u2192 Set\nclose-enough {nats} df dg args = df \u2261 dg -- extensionally\nclose-enough {bags} df dg args = df \u2261 dg -- literally\nclose-enough {\u03c4\u2081 \u21d2 \u03c4\u2082} df dg (alter args) =\n \u2200 {x dx} \u2192 close-enough (df x dx) (dg x dx) args\nclose-enough {\u03c4\u2081 \u21d2 \u03c4\u2082} df dg (abide args) =\n \u2200 {x dx} \u2192 x \u27e6\u2295\u27e7 dx \u2261 x \u2192 close-enough (df x dx) (dg x dx) args\n\nsyntax close-enough df dg args = df \u2248 dg WRT args\n\nvolatility\u21d2identity :\n \u2200 {\u03c4} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n df \u2248 dg WRT (expect-volatility {\u03c4}) \u2192 df \u2261 dg\n\nvolatility\u21d2identity {nats} df\u2248dg = df\u2248dg\nvolatility\u21d2identity {bags} df\u2248dg = df\u2248dg\nvolatility\u21d2identity {\u03c4\u2081 \u21d2 \u03c4\u2082} {df} {dg} df\u2248dg =\n extensionality (\u03bb x \u2192 extensionality (\u03bb dx \u2192\n volatility\u21d2identity {\u03c4\u2082} (df\u2248dg {x} {dx})))\n\ndf\u2248df : \u2200 {\u03c4} {df : \u27e6 \u0394-Type \u03c4 \u27e7} {args : Args \u03c4} \u2192 df \u2248 df WRT args\ndf\u2248df {nats} = refl\ndf\u2248df {bags} = refl\ndf\u2248df {\u03c4\u2081 \u21d2 \u03c4\u2082} {df} {abide args} =\n \u03bb {x} {dx} _ \u2192 df\u2248df {\u03c4\u2082} {df x dx} {args}\ndf\u2248df {\u03c4\u2081 \u21d2 \u03c4\u2082} {df} {alter args} =\n \u03bb {x} {dx} \u2192 df\u2248df {\u03c4\u2082} {df x dx} {args}\n\ntoo-close : \u2200 {\u03c4 \u0393} {args : Args \u03c4} \u2192\n {df dg : Term (\u0394-Context \u0393) (\u0394-Type \u03c4)} {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n df \u2261 dg \u2192 \u27e6 df \u27e7 \u03c1 \u2248 \u27e6 dg \u27e7 \u03c1 WRT args\n\ntoo-close {\u03c4}{_} {args} {df} {dg} {\u03c1} df=dg\n rewrite df=dg = df\u2248df {\u03c4} {\u27e6 dg \u27e7 \u03c1} {args}\n\n-- A variable does not change if its value is unchanging.\nstabilityVar : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) (vars : Vars \u0393) \u2192\n stableVar x vars \u2261 true \u2192\n \u2200 {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192 Honest vars \u03c1 \u2192\n \u27e6 weakenVar \u0393\u227c\u0394\u0393 x \u27e7 \u03c1 \u27e6\u2295\u27e7 \u27e6 deriveVar x \u27e7 \u03c1 \u2261 \u27e6 weakenVar \u0393\u227c\u0394\u0393 x \u27e7 \u03c1\n\nstabilityVar this (alter vars) () (alter honesty)\nstabilityVar this (abide vars) refl (abide proof honesty) = proof\n\nstabilityVar {\u03c4} {\u03c4\u2032 \u2022 \u0393 } (that x) (abide vars) truth (abide _ honesty) =\n stabilityVar x vars (trans eq2 truth) honesty\n where\n eq2 : stableVar x vars \u2261 stableVar (that {\u03c4} {\u03c4\u2032} {\u0393} x) (abide vars)\n eq2 = refl\n\nstabilityVar (that x) (alter vars) truth honesty = {!ditto!}\n\n-- A term does not change if its free variables are unchanging.\nstability : \u2200 {\u03c4 \u0393} \u2192 (t : Term \u0393 \u03c4) (vars : Vars \u0393) \u2192\n stable t vars \u2261 true \u2192\n \u2200 {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192 Honest vars \u03c1 \u2192\n \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 \u03c1 \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 \u03c1 \u2261 \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 \u03c1\n\nstability (nat n) vars truth {\u03c1} _ = refl\n\nstability (bag b) vars truth {\u03c1} _ = b++\u2205=b\n\nstability (var x) vars truth {\u03c1} honesty =\n {!!}\n where\n eq1 : stableVar x vars \u2261 stable (var x) vars\n eq1 = refl\n tVar : stableVar x vars \u2261 true\n tVar = trans eq1 truth\n\nstability (abs t) vars truth {\u03c1} honesty = {!!}\nstability (app t t\u2081) vars truth {\u03c1} honesty = {!!}\nstability (add t t\u2081) vars truth {\u03c1} honesty = {!!}\nstability (map t t\u2081) vars truth {\u03c1} honesty = {!!}\nstability (diff t t\u2081) vars truth {\u03c1} honesty = {!!}\nstability (union t t\u2081) vars truth {\u03c1} honesty = {!!}\n\nhonestyVar : {\u03c4 : Type} \u2192 {\u0393 : Context} \u2192\n (args : Args \u03c4) \u2192 (vars : Vars \u0393) \u2192\n (x : Var \u0393 \u03c4) \u2192 derive (var x) \u2261 derive' args vars (var x)\nhonestyVar \u2205-nat vars x = refl\nhonestyVar \u2205-bag vars x = refl\nhonestyVar (abide args) vars x = refl\nhonestyVar (alter args) vars x = refl\n\n-- If both the environment and the future arguments are honest\n-- about nil changes, then the optimized derivation delivers\n-- the same result as the original derivation.\nhonesty-is-the-best-policy : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) \u2192\n (args : Args \u03c4) \u2192 (vars : Vars \u0393) \u2192\n (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Honest vars \u03c1 \u2192\n \u27e6 derive t \u27e7 \u03c1 \u2248 \u27e6 derive' args vars t \u27e7 \u03c1 WRT args\n\nhonesty-is-the-best-policy (app f s) args vars \u03c1 honesty\n with stable s vars | inspect (stable s) vars\n... | true | [ truth ] = {!!}\n-- absurd! {!!} (stability s vars truth {\u03c1}) {!!}\n\n... | false | [ falsehood ] = {!!}\n\nhonesty-is-the-best-policy {nats} {\u0393} (nat n) args vars \u03c1 honesty =\n begin\n \u27e6 derive (nat n) \u27e7 \u03c1\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 hole \u27e7 \u03c1) (lemma \u0393 args vars) \u27e9\n \u27e6 derive' args vars (nat n) \u27e7 \u03c1\n \u220e where\n open \u2261-Reasoning\n lemma : (\u0393 : Context) (args : Args nats) (vars : Vars \u0393) \u2192\n derive (nat n) \u2261 derive' {nats} {\u0393} args vars (nat n)\n lemma \u2205 \u2205-nat \u2205 = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-nat (abide vars) = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-nat (alter vars) = refl\n\nhonesty-is-the-best-policy {bags} {\u0393} (bag b) args vars \u03c1 honesty =\n begin\n \u27e6 derive (bag b) \u27e7 \u03c1\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 hole \u27e7 \u03c1) (lemma \u0393 args vars) \u27e9\n \u27e6 derive' args vars (bag b) \u27e7 \u03c1\n \u220e where\n open \u2261-Reasoning\n lemma : (\u0393 : Context) (args : Args bags) (vars : Vars \u0393) \u2192\n derive (bag b) \u2261 derive' {bags} {\u0393} args vars (bag b)\n lemma \u2205 \u2205-bag \u2205 = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-bag (abide vars) = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-bag (alter vars) = refl\n\nhonesty-is-the-best-policy {\u03c4} {\u0393} (var x) args vars \u03c1 honesty =\n too-close {\u03c4} {\u0393} (honestyVar args vars x)\n\nhonesty-is-the-best-policy (abs t) (abide args) vars \u03c1 honesty =\n \u03bb {x} {dx} x\u2295dx=x \u2192 honesty-is-the-best-policy\n t args (abide vars) (dx \u2022 x \u2022 \u03c1) (abide x\u2295dx=x honesty)\n\nhonesty-is-the-best-policy (abs t) (alter args) vars \u03c1 honesty =\n \u03bb {x} {dx} \u2192 honesty-is-the-best-policy\n t args (alter vars) (dx \u2022 x \u2022 \u03c1) (alter honesty)\n\nhonesty-is-the-best-policy (add m n) args vars \u03c1 honesty = {!!}\nhonesty-is-the-best-policy (map f b) args vars \u03c1 honesty = {!!}\nhonesty-is-the-best-policy (diff b d) args vars \u03c1 honesty = {!!}\nhonesty-is-the-best-policy (union b d) args vars \u03c1 honesty = {!!}\n\n","old_contents":"{-\nCommunicate to derivatives that changes to certain arguments\nare always nil (i. e., certain arguments are stable).\n-}\n\nmodule ExplicitNil where\n\nopen import NatBag\n\nopen import Data.Bool\nopen import Data.Product hiding (map)\nopen import Data.Nat using (\u2115)\nopen import Data.Unit using (\u22a4 ; tt)\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\ndata Args : (\u03c4 : Type) \u2192 Set where\n \u2205-nat : Args nats\n \u2205-bag : Args bags\n abide : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192 (args : Args \u03c4\u2082) \u2192 Args (\u03c4\u2081 \u21d2 \u03c4\u2082)\n alter : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192 (args : Args \u03c4\u2082) \u2192 Args (\u03c4\u2081 \u21d2 \u03c4\u2082)\n\n-- Totally like subcontext relation _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set\ndata Vars : Context \u2192 Set where\n \u2205 : Vars \u2205\n abide : \u2200 {\u03c4 \u0393} \u2192 Vars \u0393 \u2192 Vars (\u03c4 \u2022 \u0393) -- is in the set\n alter : \u2200 {\u03c4 \u0393} \u2192 Vars \u0393 \u2192 Vars (\u03c4 \u2022 \u0393) -- is out of the set\n\nstableVar : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Bool\nstableVar this (abide _) = true\nstableVar this (alter _) = false\nstableVar (that x) (abide vars) = stableVar x vars\nstableVar (that x) (alter vars) = stableVar x vars\n\n-- A term is stable if all its free variables are unchanging\n-- Alternative definition:\n--\n-- stable t vars = isNil t (derive t)\n--\nstable : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Bool\nstable (nat n) vars = true\nstable (bag b) vars = true\nstable (var x) vars = stableVar x vars\nstable (abs t) vars = stable t (abide vars)\nstable (app f x) vars = stable f vars \u2227 stable x vars\nstable (add m n) vars = stable m vars \u2227 stable n vars\nstable (map f b) vars = stable f vars \u2227 stable b vars\nstable (diff b d) vars = stable b vars \u2227 stable d vars\nstable (union b d) vars = stable b vars \u2227 stable d vars\n\n{-\n-- Reformulating stableness as a decidable relation\n-- Not sure if it is necessary or not.\ndata StableVar : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Set where\n abide-this : \u2200 {\u03c4 \u0393} \u2192 {vars : Vars \u0393} \u2192 StableVar this (abide {\u03c4} vars)\n abide-that : \u2200 {\u03c4 \u0393 \u03c4\u2032} \u2192 {x : Var \u0393 \u03c4} \u2192 {vars : Vars \u0393} \u2192\n StableVar x vars \u2192 StableVar (that {\u03c4} {\u03c4\u2032} x) (abide vars)\n alter-that : \u2200 {\u03c4 \u0393 \u03c4\u2032} \u2192 {x : Var \u0393 \u03c4} \u2192 {vars : Vars \u0393} \u2192\n StableVar x vars \u2192 StableVar (that {\u03c4} {\u03c4\u2032} x) (alter vars)\n\ndata Stable : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Set where\n stable-nat : \u2200 {\u0393 n vars} \u2192 Stable {nats} {\u0393} (nat n) vars\n stable-bag : \u2200 {\u0393 b vars} \u2192 Stable {bags} {\u0393} (bag b) vars\n stable-var : \u2200 {\u03c4 \u0393 x vars} \u2192\n StableVar {\u03c4} {\u0393} x vars \u2192 Stable (var x) vars\n stable-abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393 vars} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n Stable t (abide vars) \u2192 Stable (abs t) vars\n -- TODO app add map diff union\n-}\n\nexpect-volatility : {\u03c4 : Type} \u2192 Args \u03c4\nexpect-volatility {\u03c4\u2081 \u21d2 \u03c4\u2082} = alter expect-volatility\nexpect-volatility {nats} = \u2205-nat\nexpect-volatility {bags} = \u2205-bag\n\n-- Type of `derive'`\nderive' : \u2200 {\u03c4 \u0393} \u2192 Args \u03c4 \u2192 Vars \u0393 \u2192\n Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\nderive' {\u03c4\u2081 \u21d2 \u03c4\u2082} (abide args) vars (abs t) =\n abs (abs (derive' args (abide vars) t))\n\nderive' (alter args) vars (abs t) =\n abs (abs (derive' args (alter vars) t))\n\n-- Assume, for safety, that all arguments that `t` will\n-- eventually receive in `s` or receive curried out of `s`\n-- are volatile.\nderive' args vars (app s t) =\n if stable t vars\n then app (app (derive' (abide args) vars s) (weaken \u0393\u227c\u0394\u0393 t))\n (derive' expect-volatility vars t)\n else app (app (derive' (alter args) vars s) (weaken \u0393\u227c\u0394\u0393 t))\n (derive' expect-volatility vars t)\n\nderive' args vars (map f b) =\n if stable f vars\n then map (weaken \u0393\u227c\u0394\u0393 f) (derive' args vars b)\n else map (weaken \u0393\u227c\u0394\u0393 f \u2295 derive' (abide \u2205-nat) vars f)\n (weaken \u0393\u227c\u0394\u0393 b \u2295 derive' args vars b)\n \u229d weaken \u0393\u227c\u0394\u0393 (map f b)\n\nderive' args vars (diff b d) =\n diff (derive' args vars b) (derive' args vars d)\n\nderive' args vars (union b d) =\n union (derive' args vars b) (derive' args vars d)\n\n-- derive(x + y) = replace (x + y) by (dx snd + dy snd)\n-- = \u03bb f. f (x + y) (dx snd + dy snd)\nderive' args vars (add m n) =\n abs (app (app (var this)\n (add (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) m) (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) n)))\n (add (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive' args vars m)) snd)\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive' args vars n)) snd)))\n\nderive' args vars constant-or-variable = derive constant-or-variable\n\n-- A description of variables is honest w.r.t. a \u0394-environment\n-- if every variable described as stable receives the nil change.\ndata Honest : \u2200 {\u0393} \u2192 Vars \u0393 \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 Set where\n clearly : Honest \u2205 \u2205\n alter : \u2200 {\u0393 \u03c4 v dv} \u2192 {vars : Vars \u0393} \u2192 {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Honest {\u0393} vars \u03c1 \u2192\n Honest {\u03c4 \u2022 \u0393} (alter vars) (dv \u2022 v \u2022 \u03c1)\n abide : \u2200 {\u0393 \u03c4 v dv} \u2192 {vars : Vars \u0393} \u2192 {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n v \u27e6\u2295\u27e7 dv \u2261 v \u2192\n Honest {\u0393} vars \u03c1 \u2192\n Honest {\u03c4 \u2022 \u0393} (abide vars) (dv \u2022 v \u2022 \u03c1)\n\n-- Two \u0394-values are close enough w.r.t. a set of arguments if they\n-- behave the same when fully applied (cf. extensionality) given\n-- that each argument declared stable receives the nil change.\nclose-enough : \u2200 {\u03c4 : Type} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Args \u03c4 \u2192 Set\nclose-enough {nats} df dg args = df \u2261 dg -- extensionally\nclose-enough {bags} df dg args = df \u2261 dg -- literally\nclose-enough {\u03c4\u2081 \u21d2 \u03c4\u2082} df dg (alter args) =\n \u2200 {x dx} \u2192 close-enough (df x dx) (dg x dx) args\nclose-enough {\u03c4\u2081 \u21d2 \u03c4\u2082} df dg (abide args) =\n \u2200 {x dx} \u2192 x \u27e6\u2295\u27e7 dx \u2261 x \u2192 close-enough (df x dx) (dg x dx) args\n\nsyntax close-enough df dg args = df \u2248 dg WRT args\n\ndf\u2248df : \u2200 {\u03c4} {df : \u27e6 \u0394-Type \u03c4 \u27e7} {args : Args \u03c4} \u2192 df \u2248 df WRT args\ndf\u2248df {nats} = refl\ndf\u2248df {bags} = refl\ndf\u2248df {\u03c4\u2081 \u21d2 \u03c4\u2082} {df} {abide args} =\n \u03bb {x} {dx} _ \u2192 df\u2248df {\u03c4\u2082} {df x dx} {args}\ndf\u2248df {\u03c4\u2081 \u21d2 \u03c4\u2082} {df} {alter args} =\n \u03bb {x} {dx} \u2192 df\u2248df {\u03c4\u2082} {df x dx} {args}\n\ntoo-close : \u2200 {\u03c4 \u0393} {args : Args \u03c4} \u2192\n {df dg : Term (\u0394-Context \u0393) (\u0394-Type \u03c4)} {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n df \u2261 dg \u2192 \u27e6 df \u27e7 \u03c1 \u2248 \u27e6 dg \u27e7 \u03c1 WRT args\n\ntoo-close {nats}{_}{_} {df} {dg} {\u03c1} df=dg =\n cong (\u03bb hole \u2192 \u27e6 hole \u27e7 \u03c1) df=dg\ntoo-close {bags}{_}{_} {df} {dg} {\u03c1} df=dg =\n cong (\u03bb hole \u2192 \u27e6 hole \u27e7 \u03c1) df=dg\ntoo-close {\u03c4\u2081 \u21d2 \u03c4\u2082}{_} {args} {df} {dg} {\u03c1} df=dg\n rewrite df=dg = df\u2248df {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u27e6 dg \u27e7 \u03c1} {args}\n\nhonestyVar : {\u03c4 : Type} \u2192 {\u0393 : Context} \u2192\n (args : Args \u03c4) \u2192 (vars : Vars \u0393) \u2192\n (x : Var \u0393 \u03c4) \u2192 derive (var x) \u2261 derive' args vars (var x)\nhonestyVar \u2205-nat vars x = refl\nhonestyVar \u2205-bag vars x = refl\nhonestyVar (abide args) vars x = refl\nhonestyVar (alter args) vars x = refl\n\nhonesty-is-the-best-policy : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) \u2192\n (args : Args \u03c4) \u2192 (vars : Vars \u0393) \u2192\n (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Honest vars \u03c1 \u2192\n \u27e6 derive t \u27e7 \u03c1 \u2248 \u27e6 derive' args vars t \u27e7 \u03c1 WRT args\n\n-- A term does not change if its free variables are unchanging.\nstability : \u2200 {\u03c4 \u0393} \u2192 (t : Term \u0393 \u03c4) (vars : Vars \u0393) \u2192\n T (stable t vars) \u2192\n \u2200 {A : Set} \u2192 A\n-- Conclusion is absurdity for now\n-- to test usability inside case t-app.\n-- Using T (stable t vars) as a condition is not enough.\nstability t vars truth = {!!}\n\nhonesty-is-the-best-policy (app f s) args vars \u03c1 honesty\n with stable s vars\n... | true = stability s vars {!tt!}\n... | false = {!!}\n\nhonesty-is-the-best-policy {nats} {\u0393} (nat n) args vars \u03c1 honesty =\n begin\n \u27e6 derive (nat n) \u27e7 \u03c1\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 hole \u27e7 \u03c1) (lemma \u0393 args vars) \u27e9\n \u27e6 derive' args vars (nat n) \u27e7 \u03c1\n \u220e where\n open \u2261-Reasoning\n lemma : (\u0393 : Context) (args : Args nats) (vars : Vars \u0393) \u2192\n derive (nat n) \u2261 derive' {nats} {\u0393} args vars (nat n)\n lemma \u2205 \u2205-nat \u2205 = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-nat (abide vars) = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-nat (alter vars) = refl\n\nhonesty-is-the-best-policy {bags} {\u0393} (bag b) args vars \u03c1 honesty =\n begin\n \u27e6 derive (bag b) \u27e7 \u03c1\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 hole \u27e7 \u03c1) (lemma \u0393 args vars) \u27e9\n \u27e6 derive' args vars (bag b) \u27e7 \u03c1\n \u220e where\n open \u2261-Reasoning\n lemma : (\u0393 : Context) (args : Args bags) (vars : Vars \u0393) \u2192\n derive (bag b) \u2261 derive' {bags} {\u0393} args vars (bag b)\n lemma \u2205 \u2205-bag \u2205 = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-bag (abide vars) = refl\n lemma (\u03c4 \u2022 \u0393) \u2205-bag (alter vars) = refl\n\nhonesty-is-the-best-policy {\u03c4} {\u0393} (var x) args vars \u03c1 honesty =\n too-close {\u03c4} {\u0393} (honestyVar args vars x)\n\nhonesty-is-the-best-policy (abs t) (abide args) vars \u03c1 honesty =\n \u03bb {x} {dx} x\u2295dx=x \u2192 honesty-is-the-best-policy\n t args (abide vars) (dx \u2022 x \u2022 \u03c1) (abide x\u2295dx=x honesty)\n\nhonesty-is-the-best-policy (abs t) (alter args) vars \u03c1 honesty =\n \u03bb {x} {dx} \u2192 honesty-is-the-best-policy\n t args (alter vars) (dx \u2022 x \u2022 \u03c1) (alter honesty)\n\nhonesty-is-the-best-policy (add m n) args vars \u03c1 honesty = {!!}\nhonesty-is-the-best-policy (map f b) args vars \u03c1 honesty = {!!}\nhonesty-is-the-best-policy (diff b d) args vars \u03c1 honesty = {!!}\nhonesty-is-the-best-policy (union b d) args vars \u03c1 honesty = {!!}\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"5303d7901af55944be2ec4695ee17bbd01624291","subject":"Document Theorem.CongApp regularly.","message":"Document Theorem.CongApp regularly.\n\nOld-commit-hash: f16f3e3d0b8e59230bd86a0c223af263a8db75e8\n","repos":"inc-lc\/ilc-agda","old_file":"Theorem\/CongApp.agda","new_file":"Theorem\/CongApp.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Congruence of application.\n--\n-- If f \u2261 g and x \u2261 y, then (f x) \u2261 (g y).\n------------------------------------------------------------------------\n\nmodule Theorem.CongApp where\n\nopen import Relation.Binary.PropositionalEquality public\n\ninfixl 0 _\u27e8$\u27e9_\n\n_\u27e8$\u27e9_ : \u2200 {a b} {A : Set a} {B : Set b}\n {f g : A \u2192 B} {x y : A} \u2192\n f \u2261 g \u2192 x \u2261 y \u2192 f x \u2261 g y\n\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\n","old_contents":"module Theorem.CongApp where\n\n-- Theorem CongApp.\n-- If f \u2261 g and x \u2261 y, then (f x) \u2261 (g y).\n\nopen import Relation.Binary.PropositionalEquality public\n\ninfixl 0 _\u27e8$\u27e9_\n\n_\u27e8$\u27e9_ : \u2200 {a b} {A : Set a} {B : Set b}\n {f g : A \u2192 B} {x y : A} \u2192\n f \u2261 g \u2192 x \u2261 y \u2192 f x \u2261 g y\n\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"45091fb39397b33a08cf731e29b25b603d1c525b","subject":"Removed unnecessary implicit argument.","message":"Removed unnecessary implicit argument.\n\nIgnore-this: d00e8c938cb9a7fa50eccab6b862327b\n\ndarcs-hash:20120314030126-3bd4e-92953574fef902c48980a129990a5a2edd427c8e.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/PA\/Inductive\/Properties.agda","new_file":"src\/PA\/Inductive\/Properties.agda","new_contents":"------------------------------------------------------------------------------\n-- Inductive PA properties commons to the interactive and automatic proofs\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule PA.Inductive.Properties where\n\nopen import PA.Inductive.Base\n\n------------------------------------------------------------------------------\n-- Congruence properties\n\nsucc-cong : \u2200 {m n} \u2192 m \u2261 n \u2192 succ m \u2261 succ n\nsucc-cong refl = refl\n\n+-leftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 m + o \u2261 n + o\n+-leftCong refl = refl\n\n+-rightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 m + n \u2261 m + o\n+-rightCong refl = refl\n\n------------------------------------------------------------------------------\n-- Peano's third axiom.\nP\u2083 : \u2200 {m n} \u2192 succ m \u2261 succ n \u2192 m \u2261 n\nP\u2083 refl = refl\n\n-- Peano's fourth axiom.\nP\u2084 : \u2200 {n} \u2192 \u00ac (zero \u2261 succ n)\nP\u2084 ()\n\nx\u2262Sx : \u2200 {n} \u2192 \u00ac (n \u2261 succ n)\nx\u2262Sx {zero} ()\nx\u2262Sx {succ n} h = x\u2262Sx (P\u2083 h)\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity n = refl\n\n-- Adapted from the Agda standard library v0.6 (see\n-- Data.Nat.Properties.n+0\u2261n).\n+-rightIdentity : \u2200 n \u2192 n + zero \u2261 n\n+-rightIdentity zero = refl\n+-rightIdentity (succ n) = succ-cong (+-rightIdentity n)\n\n-- Adapted from the Agda standard library v0.6 (see\n-- Data.Nat.Properties.+-assoc).\n+-assoc : \u2200 m n o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc zero _ _ = refl\n+-assoc (succ m) n o = succ-cong (+-assoc m n o)\n\n-- Adapted from the Agda standard library v0.6 (see\n-- Data.Nat.Properties.m+1+n\u22611+m+n).\nx+Sy\u2261S[x+y] : \u2200 m n \u2192 m + succ n \u2261 succ (m + n)\nx+Sy\u2261S[x+y] zero _ = refl\nx+Sy\u2261S[x+y] (succ m) n = succ-cong (x+Sy\u2261S[x+y] m n)\n","old_contents":"------------------------------------------------------------------------------\n-- Inductive PA properties commons to the interactive and automatic proofs\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule PA.Inductive.Properties where\n\nopen import PA.Inductive.Base\n\n------------------------------------------------------------------------------\n-- Congruence properties\n\nsucc-cong : \u2200 {m n} \u2192 m \u2261 n \u2192 succ m \u2261 succ n\nsucc-cong = cong succ\n\n+-leftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 m + o \u2261 n + o\n+-leftCong h = cong\u2082 _+_ h refl\n\n-- TODO: It is strictly necessary the implicit argument m?\n+-rightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 m + n \u2261 m + o\n+-rightCong {m} h = cong\u2082 _+_ {m} refl h\n\n------------------------------------------------------------------------------\n-- Peano's third axiom.\nP\u2083 : \u2200 {m n} \u2192 succ m \u2261 succ n \u2192 m \u2261 n\nP\u2083 refl = refl\n\n-- Peano's fourth axiom.\nP\u2084 : \u2200 {n} \u2192 \u00ac (zero \u2261 succ n)\nP\u2084 ()\n\nx\u2262Sx : \u2200 {n} \u2192 \u00ac (n \u2261 succ n)\nx\u2262Sx {zero} ()\nx\u2262Sx {succ n} h = x\u2262Sx (P\u2083 h)\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity n = refl\n\n-- Adapted from the Agda standard library v0.6 (see\n-- Data.Nat.Properties.n+0\u2261n).\n+-rightIdentity : \u2200 n \u2192 n + zero \u2261 n\n+-rightIdentity zero = refl\n+-rightIdentity (succ n) = succ-cong (+-rightIdentity n)\n\n-- Adapted from the Agda standard library v0.6 (see\n-- Data.Nat.Properties.+-assoc).\n+-assoc : \u2200 m n o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc zero _ _ = refl\n+-assoc (succ m) n o = succ-cong (+-assoc m n o)\n\n-- Adapted from the Agda standard library v0.6 (see\n-- Data.Nat.Properties.m+1+n\u22611+m+n).\nx+Sy\u2261S[x+y] : \u2200 m n \u2192 m + succ n \u2261 succ (m + n)\nx+Sy\u2261S[x+y] zero _ = refl\nx+Sy\u2261S[x+y] (succ m) n = succ-cong (x+Sy\u2261S[x+y] m n)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"dfc3d8fd4f8a36bc9eac388bded7b5a7635e7af8","subject":"IDescTT model: implement the Vec and Fin examples","message":"IDescTT model: implement the Vec and Fin examples","repos":"kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram,kwangkim\/pigment","old_file":"models\/IDescTT.agda","new_file":"models\/IDescTT.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n\n\n--********************************\n-- Typed expressions\n--********************************\n\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty),\n (prod (var bool) (prod (var ty) (var ty)), \n Void)))\n\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n\n--********************************\n-- Open terms\n--********************************\n\nVar : EnumU -> Type -> Set\nVar dom _ = EnumT dom\n\nopenTerm : EnumU -> Type -> IDesc Type\nopenTerm dom = exprIDesc (\\ty -> Val ty + Var dom ty) expr\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm ty\ndischarge ty vars context (l value) = con ( EZe , value )\ndischarge ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm ty) context variable \n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), \n Void))\n\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void)\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void )\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (exprFree ** Val)\n\nopenTerm' : EnumU -> Type -> IDesc Type\nopenTerm' dom = toIDesc Type (exprFree ** (\\ty -> Val ty + Var dom ty))\n\n--********************************\n-- Evaluation of open terms'\n--********************************\n\ndischarge' : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm' ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm' ty\ndischarge' ty vars context (l value) = con (EZe , value) \ndischarge' ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm' ty) context variable\n\nchooseDom : (ty : Type)(domNat domBool : EnumU) -> EnumU\nchooseDom nat domNat _ = domNat\nchooseDom bool _ domBool = domBool\n\nchooseGamma : (ty : Type)\n (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool)) ->\n spi dom (\\_ -> IMu closeTerm' ty)\nchooseGamma nat dom gammaNat gammaBool = gammaNat\nchooseGamma bool dom gammaNat gammaBool = gammaBool\n\nsubstExpr : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (sigma : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var dom ty) ->\n IMu closeTerm' ty)\n (ty : Type) ->\n IMu (openTerm' dom) ty ->\n IMu closeTerm' ty\nsubstExpr dom gammaNat gammaBool sig ty term = \n substI (\\ty -> Val ty + Var dom ty)\n Val\n exprFree\n (sig dom gammaNat gammaBool)\n ty\n term\n\n\nsigmaExpr : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var dom ty) ->\n IMu closeTerm' ty\nsigmaExpr dom gammaNat gammaBool ty v = \n discharge' ty\n dom\n (chooseGamma ty dom gammaNat gammaBool)\n v\n\ntest : IMu (openTerm' (consE (consE nilE))) nat ->\n IMu closeTerm' nat\ntest term = substExpr (consE (consE nilE)) \n ( con ( EZe , ze ) , ( con (EZe , su ze ) , Void )) \n ( con ( EZe , true ) , ( con ( EZe , false ) , Void ))\n sigmaExpr nat term\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Meta-language\n--********************************\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n\n\n--********************************\n-- Typed expressions\n--********************************\n\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty),\n (prod (var bool) (prod (var ty) (var ty)), \n Void)))\n\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n\n--********************************\n-- Open terms\n--********************************\n\nVar : EnumU -> Type -> Set\nVar dom _ = EnumT dom\n\nopenTerm : EnumU -> Type -> IDesc Type\nopenTerm dom = exprIDesc (\\ty -> Val ty + Var dom ty) expr\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm ty\ndischarge ty vars context (l value) = con ( EZe , value )\ndischarge ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm ty) context variable \n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), \n Void))\n\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void)\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void )\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (exprFree ** Val)\n\nopenTerm' : EnumU -> Type -> IDesc Type\nopenTerm' dom = toIDesc Type (exprFree ** (\\ty -> Val ty + Var dom ty))\n\n--********************************\n-- Evaluation of open terms'\n--********************************\n\ndischarge' : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm' ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm' ty\ndischarge' ty vars context (l value) = con (EZe , value) \ndischarge' ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm' ty) context variable\n\nchooseDom : (ty : Type)(domNat domBool : EnumU) -> EnumU\nchooseDom nat domNat _ = domNat\nchooseDom bool _ domBool = domBool\n\nchooseGamma : (ty : Type)\n (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool)) ->\n spi dom (\\_ -> IMu closeTerm' ty)\nchooseGamma nat dom gammaNat gammaBool = gammaNat\nchooseGamma bool dom gammaNat gammaBool = gammaBool\n\nsubstExpr : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (sigma : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var dom ty) ->\n IMu closeTerm' ty)\n (ty : Type) ->\n IMu (openTerm' dom) ty ->\n IMu closeTerm' ty\nsubstExpr dom gammaNat gammaBool sig ty term = \n substI (\\ty -> Val ty + Var dom ty)\n Val\n exprFree\n (sig dom gammaNat gammaBool)\n ty\n term\n\n\nsigmaExpr : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var dom ty) ->\n IMu closeTerm' ty\nsigmaExpr dom gammaNat gammaBool ty v = \n discharge' ty\n dom\n (chooseGamma ty dom gammaNat gammaBool)\n v\n\ntest : IMu (openTerm' (consE (consE nilE))) nat ->\n IMu closeTerm' nat\ntest term = substExpr (consE (consE nilE)) \n ( con ( EZe , ze ) , ( con (EZe , su ze ) , Void )) \n ( con ( EZe , true ) , ( con ( EZe , false ) , Void ))\n sigmaExpr nat term\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f184f39c2cc18ca063517afb7b4501762acaf24e","subject":"various","message":"various\n","repos":"crypto-agda\/crypto-agda","old_file":"Control\/Protocol\/Choreography.agda","new_file":"Control\/Protocol\/Choreography.agda","new_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map; proj\u2081 to fst; proj\u2082 to snd)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_]) hiding ([_,_]\u2032)\nopen import Data.One hiding (_\u225f_)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP as \u2261\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\n {-\ndata S<_> {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n\npattern com' q M P = com (mk q M P)\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\n\nmodule ProtoRelImplicit {_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605} = ProtoRel _\u2248\u1d35\u1d3c_\nopen ProtoRelImplicit hiding (_\u2248\u1d3e_)\nopen ProtoRel _\u2261_ public renaming (_\u2248\u1d3e_ to _\u2261\u1d3e_) using ()\n\ndata View-\u2261\u1d3e : (P Q : Proto) \u2192 P \u2261\u1d3e Q \u2192 \u2605\u2081 where\n end : View-\u2261\u1d3e end end end\n \u2261-\u03a3 : \u2200 {M P Q} (p\u2261q : \u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 View-\u2261\u1d3e (\u03a3\u1d3e M P) (\u03a3\u1d3e M Q) (com refl M p\u2261q)\n \u2261-\u03a0 : \u2200 {M P Q} (p\u2261q : \u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 View-\u2261\u1d3e (\u03a0\u1d3e M P) (\u03a0\u1d3e M Q) (com refl M p\u2261q)\n\nview-\u2261\u1d3e : \u2200 {P Q} (p\u2261q : P \u2261\u1d3e Q) \u2192 View-\u2261\u1d3e P Q p\u2261q\nview-\u2261\u1d3e end = end\nview-\u2261\u1d3e (com {In} refl _ _) = \u2261-\u03a0 _\nview-\u2261\u1d3e (com {Out} refl _ _) = \u2261-\u03a3 _\n\n{-\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n -}\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n source-of : Proto \u2192 Proto\n source-of end = end\n source-of (com c) = com (source-of\u1d9c c)\n\n source-of\u1d9c : Com \u2192 Com\n source-of\u1d9c c = \u03a3\u1d9c (Com.M c) \u03bb m \u2192 source-of (Com.P c m)\n\n {-\ndual : Proto \u2192 Proto\ndual end = end\ndual (\u03a3\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 dual (P m)\ndual (\u03a0\u1d3e M P) = \u03a3\u1d3e M \u03bb m \u2192 dual (P m)\n-}\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c c = mk (dual\u1d35\u1d3c (Com.io c)) (Com.M c) \u03bb m \u2192 dual (Com.P c m)\n\ndata IsSource : Proto \u2192 \u2605\u2081 where\n end : IsSource end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSource (P m)) \u2192 IsSource (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n\u27e6_\u27e7\u27e8_\u2248_\u27e9 : (P : Proto) (p q : \u27e6 P \u27e7) \u2192 \u2605\n\u27e6 end \u27e7\u27e8 p \u2248 q \u27e9 = \ud835\udfd9\n\u27e6 \u03a0\u1d3e M P \u27e7\u27e8 p \u2248 q \u27e9 = (m : M) \u2192 \u27e6 P m \u27e7\u27e8 p m \u2248 q m \u27e9\n\u27e6 \u03a3\u1d3e M P \u27e7\u27e8 p \u2248 q \u27e9 = \u03a3 (fst p \u2261 fst q) \u03bb e \u2192 \u27e6 P (fst q) \u27e7\u27e8 subst (\u27e6_\u27e7 \u2218 P) e (snd p) \u2248 snd q \u27e9\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com refl M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\n\u2261\u1d3e-reflexive : \u2200 {P Q} \u2192 P \u2261 Q \u2192 P \u2261\u1d3e Q\n\u2261\u1d3e-reflexive refl = \u2261\u1d3e-refl _\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com refl M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n \u2261\u1d3e-cong : \u2200 {P Q} (f : Proto \u2192 Proto) \u2192 P \u2261\u1d3e Q \u2192 f P \u2261\u1d3e f Q\n \u2261\u1d3e-cong f P\u2261Q = \u2261\u1d3e-reflexive (cong f (\u2261\u1d3e-sound P\u2261Q))\n\ndual\u1d35\u1d3c-involutive : \u2200 io \u2192 dual\u1d35\u1d3c (dual\u1d35\u1d3c io) \u2261 io\ndual\u1d35\u1d3c-involutive In = refl\ndual\u1d35\u1d3c-involutive Out = refl\n\ndual-involutive : \u2200 P \u2192 dual (dual P) \u2261\u1d3e P\ndual-involutive end = end\ndual-involutive (com' q M P) = com (dual\u1d35\u1d3c-involutive q) M (\u03bb m \u2192 dual-involutive (P m))\n\ndual-inj : \u2200 P Q \u2192 dual P \u2261\u1d3e dual Q \u2192 P \u2261\u1d3e Q\ndual-inj end end end = end\ndual-inj end (com x) ()\ndual-inj (com x) end ()\ndual-inj (com (mk In M P\u2081)) (com (mk In .M P)) (ProtoRel.com q .M x) = ProtoRel.com refl M (\u03bb m \u2192 dual-inj (P\u2081 m) (P m) (x m))\ndual-inj (com (mk In M P\u2081)) (com (mk Out .M P)) (ProtoRel.com () .M x)\ndual-inj (com (mk Out M P)) (com (mk In .M Q)) (ProtoRel.com () .M x)\ndual-inj (com (mk Out M P)) (com (mk Out .M Q)) (ProtoRel.com refl .M x) = ProtoRel.com refl M (\u03bb m \u2192 dual-inj (P m) (Q m) (x m))\n\nsource-of-idempotent : \u2200 P \u2192 source-of (source-of P) \u2261\u1d3e source-of P\nsource-of-idempotent end = end\nsource-of-idempotent (com' _ M P) = com refl M \u03bb m \u2192 source-of-idempotent (P m)\n\nsource-of-dual-oblivious : \u2200 P \u2192 source-of (dual P) \u2261\u1d3e source-of P\nsource-of-dual-oblivious end = end\nsource-of-dual-oblivious (com' _ M P) = com refl M \u03bb m \u2192 source-of-dual-oblivious (P m)\n\nsink-of : Proto \u2192 Proto\nsink-of = dual \u2218 source-of\n\nLog : Proto \u2192 \u2605\nLog P = \u27e6 source-of P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Log P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n++Tele : \u2200 (P : Proto){Q : Log P \u2192 Proto} (xs : Log P) \u2192 Log (Q xs) \u2192 Log (P >>= Q)\n++Tele end _ ys = ys\n++Tele (com' q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com refl M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Log P \u2192 Proto)(R : Log (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com refl M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [inl: (\u03bb m \u2192 add\u03a3\u1d3e (P m)) ,inr: A\u1d3e ]\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e (M \u228e A) [inl: (\u03bb m \u2192 add\u03a0\u1d3e (P m)) ,inr: A\u1d3e ]\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n dual-add\u03a3\u1d3e : \u2200 P \u2192 dual (add\u03a3\u1d3e A\u1d3e P) \u2261\u1d3e add\u03a0\u1d3e (dual \u2218 A\u1d3e) (dual P)\n dual-add\u03a3\u1d3e end = end\n dual-add\u03a3\u1d3e (\u03a0\u1d3e M P) = com refl M (\u03bb m \u2192 dual-add\u03a3\u1d3e (P m))\n dual-add\u03a3\u1d3e (\u03a3\u1d3e M P) = com refl (M \u228e A) [inl: (\u03bb m \u2192 dual-add\u03a3\u1d3e (P m))\n ,inr: (\u03bb x \u2192 \u2261\u1d3e-refl (dual (A\u1d3e x))) ]\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n{-\n\nmutual\n data Dual\u1d9c : Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a0\u1d9c M P) (\u03a3\u1d9c M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a3\u1d9c M P) (\u03a0\u1d9c M Q)\n\n data Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n {-\n \u03a0\u2610\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P [ x ]) (Q x)) \u2192 Dual (\u03a0\u1d3e (\u2610 M) P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0\u2610 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q [ x ])) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e (\u2610 M) Q)\n -}\n\n {-\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Log : Choreo I \u2192 Proto\n \u2102Log \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Log-IsSource : \u2200 \u2102 \u2192 IsSource (\u2102Log \u2102)\n \u2102Log-IsSource (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Log-IsSource (\u2102 m)\n \u2102Log-IsSource (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Log-IsSource (\u2102 m)\n \u2102Log-IsSource end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n-}\n\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n{-\nDual-sym (\u03a0\u2610\u00b7\u03a3 f) = {!\u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))!}\nDual-sym (\u03a3\u00b7\u03a0\u2610 f) = {!\u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))!}\n-}\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n-}\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Log (dual P) \u2261 Log P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Log P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n El->>= : (P : Proto){Q : Log P \u2192 Proto}{X : Log (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Log P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n-}\n\n>>=-com : (P : Proto){Q : Log P \u2192 Proto}{R : Log P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Log P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Log P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\nmodule ClientServerV1 (Query : \u2605\u2080) (Resp : Query \u2192 \u2605\u2080) (P : Proto) where\n Client : \u2115 \u2192 Proto\n Client zero = P\n Client (suc n) = \u03a3\u1d3e Query \u03bb q \u2192 \u03a0\u1d3e (Resp q) \u03bb r \u2192 Client n\n\n Server : \u2115 \u2192 Proto\n Server zero = P\n Server (suc n) = \u03a0\u1d3e Query \u03bb q \u2192 \u03a3\u1d3e (Resp q) \u03bb r \u2192 Server n\n\nmodule ClientServerV2 (Query : \u2605\u2080) (Resp : Query \u2192 \u2605\u2080) where\n Client : \u2115 \u2192 Proto\n Client zero = end\n Client (suc n) = \u03a3\u1d3e Query \u03bb q \u2192 \u03a0\u1d3e (Resp q) \u03bb r \u2192 Client n\n\n Server : \u2115 \u2192 Proto\n Server = dual \u2218 Client\n\n Server' : \u2115 \u2192 Proto\n Server' zero = end\n Server' (suc n) = \u03a0\u1d3e Query \u03bb q \u2192 \u03a3\u1d3e (Resp q) \u03bb r \u2192 Server' n\n\n Server\u2261Server' : \u2200 n \u2192 Server n \u2261\u1d3e Server' n\n Server\u2261Server' zero = end\n Server\u2261Server' (suc n) = com refl Query (\u03bb m \u2192 com refl (Resp m) (\u03bb m' \u2192 Server\u2261Server' n))\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : \u2605_ a} (l r : A) \u2192 LR \u2192 A \n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a0\u1d3e LR [L: l R: r ]\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP >>\u1d9c S = record P { P = \u03bb m \u2192 S (Com.P P m) }\n\nmodule Equivalences\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n record Equiv {A B : \u2605}(f : A \u2192 B) : \u2605 where\n field\n linv : B \u2192 A\n is-linv : \u2200 x \u2192 linv (f x) \u2261 x\n rinv : B \u2192 A\n is-rinv : \u2200 x \u2192 f (rinv x) \u2261 x\n\n id\u1d31 : \u2200 {A} \u2192 Equiv {A} id\n id\u1d31 = record\n { linv = id\n ; is-linv = \u03bb _ \u2192 refl\n ; rinv = id\n ; is-rinv = \u03bb _ \u2192 refl\n }\n\n module _ {A B C}{g : B \u2192 C}{f : A \u2192 B} where\n _\u2218\u1d31_ : Equiv g \u2192 Equiv f \u2192 Equiv (g \u2218 f)\n g\u1d31 \u2218\u1d31 f\u1d31 = record { linv = F.linv \u2218 G.linv ; is-linv = \u03bb x \u2192 cong F.linv (G.is-linv (f x)) \u2219 F.is-linv x\n ; rinv = F.rinv \u2218 G.rinv ; is-rinv = \u03bb x \u2192 cong g (F.is-rinv _) \u2219 G.is-rinv x }\n where\n module G = Equiv g\u1d31\n module F = Equiv f\u1d31\n\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = \u03a3 (A \u2192 B) Equiv\n\n module _ {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b} where\n \u03a3-ext : \u2200 {x y : \u03a3 A B} \u2192 (p : fst x \u2261 fst y) \u2192 subst B p (snd x) \u2261 snd y \u2192 x \u2261 y\n \u03a3-ext refl = cong (_,_ _)\n\ndata ViewProc : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200 M(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewProc (\u03a3\u1d3e M P) (m , p)\n recv : \u2200 M(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewProc (\u03a0\u1d3e M P) p\n end : ViewProc end _\n\nview-proc : \u2200 P (p : \u27e6 P \u27e7) \u2192 ViewProc P p\nview-proc end _ = end\nview-proc (\u03a0\u1d3e M P) p = recv _ _ p\nview-proc (\u03a3\u1d3e M P) (m , p) = send _ _ m p\n\ndata ViewCom : \u2200 P \u2192 \u27e6 com P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200{M}(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewCom (\u03a3\u1d9c M P) (m , p)\n recv : \u2200{M}(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewCom (\u03a0\u1d9c M P) p\n\nview-com : \u2200 P (p : \u27e6 com P \u27e7) \u2192 ViewCom P p\nview-com (\u03a0\u1d9c M P) p = recv _ p\nview-com (\u03a3\u1d9c M P) (m , p) = send _ m p\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open Equivalences funExt\n\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n \u03a0\u1d3e M P \u214b\u1d3e Q = \u03a0\u1d3e M \u03bb m \u2192 P m \u214b\u1d3e Q\n P \u214b\u1d3e end = P\n P \u214b\u1d3e \u03a0\u1d3e M Q = \u03a0\u1d3e M \u03bb m \u2192 P \u214b\u1d3e Q m\n \u03a3\u1d3e M P \u214b\u1d3e \u03a3\u1d3e M' Q = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 P m \u214b\u1d3e \u03a3\u1d3e M' Q)\n ,inr: (\u03bb m' \u2192 \u03a3\u1d3e M P \u214b\u1d3e Q m') ]\n\n data View-\u214b : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n sendL' : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto)(m : M )(p : \u27e6 P m \u214b\u1d3e \u03a3\u1d3e M' Q \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p)\n sendR' : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto)(m' : M')(p : \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q m' \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m' , p)\n recvL' : \u2200 {M} (P : M \u2192 Proto) Q (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7)) \u2192 View-\u214b (\u03a0\u1d3e M P) Q p\n recvR' : \u2200 {M M'} (P : M \u2192 Proto) (Q : M' \u2192 Proto)(p : (m' : M') \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q m' \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p\n endL : \u2200 Q (p : \u27e6 Q \u27e7) \u2192 View-\u214b end Q p\n endR : \u2200 {M} (P : M \u2192 Proto) (p : \u27e6 \u03a3\u1d3e M P \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) end p\n\n view-\u214b : \u2200 P Q (p : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 View-\u214b P Q p\n view-\u214b end Q p = endL Q p\n view-\u214b (\u03a0\u1d3e M P) Q p = recvL' P Q p\n view-\u214b (\u03a3\u1d3e M P) end p = endR P p\n view-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = recvR' P Q p\n view-\u214b (com (mk Out M P)) (com (mk Out M' Q)) (inl x , p) = sendL' P Q x p\n view-\u214b (com (mk Out M P)) (com (mk Out M' Q)) (inr y , p) = sendR' P Q y p\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7 \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7\n \u214b\u1d3e-assoc end Q R s = s\n \u214b\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = \u214b\u1d3e-assoc (P m) _ _ (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , \u214b\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end p = p\n \u214b\u1d3e-rend (\u03a0\u1d3e _ P) p = \u03bb m \u2192 \u214b\u1d3e-rend (P m) (p m)\n \u214b\u1d3e-rend (\u03a3\u1d3e _ _) p = p\n\n \u214b\u1d3e-rend' : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u214b\u1d3e end \u27e7\n \u214b\u1d3e-rend' end p = p\n \u214b\u1d3e-rend' (\u03a0\u1d3e _ P) p = \u03bb m \u2192 \u214b\u1d3e-rend' (P m) (p m)\n \u214b\u1d3e-rend' (\u03a3\u1d3e _ _) p = p\n\n {-\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n -}\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (\u03a3\u1d3e M P) m p = inr m , p\n \u214b\u1d3e-sendR (\u03a0\u1d3e M P) m p = \u03bb x \u2192 \u214b\u1d3e-sendR (P x) m (p x)\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL (\u03a0\u1d3e M Q) m p = \u03bb m' \u2192 \u214b\u1d3e-sendL (Q m') m {!p!}\n \u214b\u1d3e-sendL (\u03a3\u1d3e M Q) m p = {!!} , p\n\n {-\n \u214b\u1d3e-sendR : \u2200 {M} P Q \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e M Q \u27e7 \u2192 (m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7\n \u214b\u1d3e-sendR P Q s m = {!!}\n -}\n\n \u214b\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e M Q \u27e7\n \u214b\u1d3e-recvR end Q s = s\n \u214b\u1d3e-recvR (\u03a0\u1d3e M P) Q s = \u03bb x \u2192 \u214b\u1d3e-recvR (P x) Q (\u03bb m \u2192 s m x)\n \u214b\u1d3e-recvR (\u03a3\u1d3e M P) Q s = s\n\n mutual\n \u214b\u1d3e-! : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-! P Q p = {!!}\n\n \u214b\u1d3e-!-view : \u2200 {P Q} {pq : \u27e6 P \u214b\u1d3e Q \u27e7} \u2192 View-\u214b P Q pq \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-!-view (sendL' P Q m p) = \u214b\u1d3e-sendR (\u03a3\u1d3e _ Q) m (\u214b\u1d3e-! (P m) (com (mk Out _ Q)) p)\n \u214b\u1d3e-!-view (sendR' P Q m' p) = {!\u214b\u1d3e-sendL (\u03a3\u1d3e _ P) m'!}\n \u214b\u1d3e-!-view (recvL' P Q pq) = {!!}\n \u214b\u1d3e-!-view (recvR' P Q pq) = {!!}\n \u214b\u1d3e-!-view (endL Q pq) = {!!}\n \u214b\u1d3e-!-view (endR P pq) = {!!}\n\n {-\n end Q p = \u214b\u1d3e-rend' Q p\n {-\n \u214b\u1d3e-! end Q p = \u214b\u1d3e-rend' Q p\n \u214b\u1d3e-! (\u03a0\u1d3e M P) end p x = \u214b\u1d3e-! (P x) end (p x)\n \u214b\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 {!\u214b\u1d3e-! (\u03a0 !}\n \u214b\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 \u214b\u1d3e-! (P m) (com (mk Out M' Q)) (p m)\n \u214b\u1d3e-! (\u03a3\u1d3e M P) end p = p\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 \u214b\u1d3e-! (com (mk Out M P)) (Q m') (p m')\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (\u214b\u1d3e-! (P m) (com (mk Out M' Q)) p)\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (\u214b\u1d3e-! (com (mk Out M P)) (Q m) p)\n\n {-\n \u214b\u1d3e-apply : \u2200 P Q \u2192 \u27e6 \u214b\u1d3e P Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply end Q s p = s\n \u214b\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = \u214b\u1d3e-apply (P m) Q (s m) p\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = \u214b\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = \u214b\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , \u214b\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n -}\n\n {-\nmodule V4\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n P \u214b\u1d3e end = P\n com P \u214b\u1d3e com Q = \u03a3\u1d3e LR (P \u214b\u1d9c Q)\n\n _\u214b\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 \u214b\u1d9c P\u1d3f) `L = P\u1d38 \u214b\u1d9cL P\u1d3f\n (P\u1d38 \u214b\u1d9c P\u1d3f) `R = P\u1d38 \u214b\u1d9cR P\u1d3f\n\n _\u214b\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) \u214b\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m \u214b\u1d3e com Q)\n\n _\u214b\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P \u214b\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P \u214b\u1d3e P\u1d3f m)\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end ox\u1d3e Q = Q\n P ox\u1d3e end = P\n com P\u1d38 ox\u1d3e com P\u1d3f = \u03a0\u1d3e LR (P\u1d38 ox\u1d9c P\u1d3f)\n\n _ox\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 ox\u1d9c P\u1d3f) `L = P\u1d38 ox\u1d9cL P\u1d3f\n (P\u1d38 ox\u1d9c P\u1d3f) `R = P\u1d38 ox\u1d9cR P\u1d3f\n\n _ox\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) ox\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m ox\u1d3e com Q)\n\n _ox\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P ox\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P ox\u1d3e P\u1d3f m)\n\n data Viewox : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n com : \u2200{P Q}(p : \u27e6 \u03a0\u1d3e LR (P ox\u1d9c Q) \u27e7) \u2192 Viewox (com P) (com Q) p\n endL : \u2200{Q}(p : \u27e6 Q \u27e7) \u2192 Viewox end Q p\n endR : \u2200{P}(p : \u27e6 com P \u27e7) \u2192 Viewox (com P) end p\n\n viewox : \u2200 P Q (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 Viewox P Q p\n viewox end Q p = endL p\n viewox (com P) end p = endR p\n viewox (com P) (com Q) p = com p\n\n data View-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u2605\u2081 where\n sendLL : \u2200 {M} (P : M \u2192 Proto) Q R (m : M)(p : \u27e6 P m \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e M P) (com Q) R (`L , m , p) q\n recvLL : \u2200 {M} (P : M \u2192 Proto) Q R\n (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e com Q \u27e7))(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a0\u1d3e M P) (com Q) R (`L , p) q\n sendRR : \u2200 {M} P Q (R : M \u2192 Proto)(m : M)(p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a3\u1d3e M R) p (`R , m , q)\n recvRR : \u2200 {M} P Q (R : M \u2192 Proto)\n (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : (m : M) \u2192 \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a0\u1d3e M R) p (`R , q)\n\n sendR-recvL : \u2200 {M} P (Q : M \u2192 Proto) R (m : M)(p : \u27e6 com P \u214b\u1d3e Q m \u27e7)(q : (m : M) \u2192 \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a3\u1d3e M Q) (com R) (`R , m , p) (`L , q)\n recvR-sendL : \u2200 {M} P (Q : M \u2192 Proto) R (p : (m : M) \u2192 \u27e6 com P \u214b\u1d3e Q m \u27e7)(m : M)(q : \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a0\u1d3e M Q) (com R) (`R , p) (`L , m , q)\n\n endL : \u2200 Q R\n \u2192 (q : \u27e6 Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 end Q R q qr\n\n endM : \u2200 P R\n \u2192 (p : \u27e6 com P \u27e7)(r : \u27e6 R \u27e7)\n \u2192 View-\u2218 (com P) end R p r\n endR : \u2200 P Q\n \u2192 (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u27e7)\n \u2192 View-\u2218 (com P) (com Q) end p q\n\n view-\u2218 : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7) \u2192 View-\u2218 P Q R pq qr\n view-\u2218 P Q R pq qr = view-\u2218-view (view-\u214b P Q pq) (view-\u214b (dual Q) R qr)\n where\n view-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u214b P Q pq \u2192 View-\u214b (dual Q) R qr \u2192 View-\u2218 P Q R pq qr\n view-\u2218-view (sendL' _ _ _ _) _ = sendLL _ _ _ _ _ _\n view-\u2218-view (recvL' _ _ _) _ = recvLL _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvL' ._ _ _) = sendR-recvL _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (endR ._ _) = endR _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendL' ._ _ _ _) = recvR-sendL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (endR ._ ._) = endR _ _ _ _\n view-\u2218-view (endL _ _) _ = endL _ _ _ _\n view-\u2218-view (endR _ _) _ = endM _ _ _ _\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end = id\n \u214b\u1d3e-rend (com _) = id\n\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (com P) m p = `R , (m , p)\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL (com _) m p = `L , (m , p)\n\n \u214b\u1d3e-recvR : \u2200 {M}P{Q : M \u2192 Proto} \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e com' In M Q \u27e7\n \u214b\u1d3e-recvR end f = f\n \u214b\u1d3e-recvR (com _) f = `R , f\n\n \u214b\u1d3e-recvL : \u2200 {M}{P : M \u2192 Proto}Q \u2192 ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7) \u2192 \u27e6 \u03a0\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-recvL {P = P} end f x = \u214b\u1d3e-rend (P x) (f x)\n \u214b\u1d3e-recvL (com _) f = `L , f\n\n ox\u1d3e-rend : \u2200 P \u2192 \u27e6 P ox\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-rend end = id\n ox\u1d3e-rend (com x) = id\n\n ox\u1d3e-sendR : \u2200 {M P}{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n ox\u1d3e-sendR {P = end} m p = m , p\n ox\u1d3e-sendR {P = com x} m p = `R , (m , p)\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id end = _\n \u214b\u1d3e-id (com (mk In M P)) = `R , \u03bb m \u2192 \u214b\u1d3e-sendL (P m) m (\u214b\u1d3e-id (P m))\n \u214b\u1d3e-id (com (mk Out M P)) = `L , \u03bb m \u2192 \u214b\u1d3e-sendR (dual (P m)) m (\u214b\u1d3e-id (P m))\n\n module _ where\n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm = \u03bb P Q \u2192 to P Q , equiv P Q\n where\n to : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n to end end pq = pq\n to end (com x) pq = pq\n to (com x) end pq = pq\n to (com (mk In M P)) (com x\u2081) (`L , pq) = `R , (\u03bb m \u2192 to (P m) (com x\u2081) (pq m))\n to (com (mk Out M P)) (com x\u2081) (`L , m , pq) = `R , m , to (P m) (com x\u2081) pq\n to (com x) (com (mk In M P)) (`R , pq) = `L , (\u03bb m \u2192 to (com x) (P m) (pq m))\n to (com x) (com (mk Out M P)) (`R , m , pq) = `L , m , to (com x) (P m) pq\n\n toto : \u2200 P Q (x : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 to Q P (to P Q x) \u2261 x\n toto end end x = refl\n toto end (com (mk io M P)) x\u2081 = refl\n toto (com (mk io M P)) end x\u2081 = refl\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' In M\u2081 P\u2081) (pq x))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' Out M\u2081 P\u2081) (pq x))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' In M\u2081 P\u2081) pq))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' Out M\u2081 P\u2081) pq))\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' In M P) (P\u2081 x) (pq x)))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' In M P) (P\u2081 m) pq))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' Out M P) (P\u2081 x) (pq x)))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' Out M P) (P\u2081 m) pq))\n\n equiv : \u2200 P Q \u2192 Equiv (to P Q)\n equiv P Q = record { linv = to Q P ; is-linv = toto P Q ; rinv = to Q P ; is-rinv = toto Q P }\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e end Q p q = q\n comma\u1d3e (com _) end p q = p\n comma\u1d3e (\u03a3\u1d3e M P) (com Q) (m , p) q `L = m , comma\u1d3e (P m) (com Q) p q\n comma\u1d3e (\u03a0\u1d3e M P) (com Q) p q `L = \u03bb m \u2192 comma\u1d3e (P m) (com Q) (p m) q\n comma\u1d3e (com P) (\u03a3\u1d3e M Q) p (m , q) `R = m , comma\u1d3e (com P) (Q m) p q\n comma\u1d3e (com P) (\u03a0\u1d3e M Q) p q `R = \u03bb m \u2192 comma\u1d3e (com P) (Q m) p (q m)\n\n \u00d7\u2192ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n \u00d7\u2192ox\u1d3e P Q (p , q) = comma\u1d3e P Q p q\n\n ox\u1d3e-fst : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-fst end Q pq = _\n ox\u1d3e-fst (com _) end pq = pq\n ox\u1d3e-fst (\u03a0\u1d3e M P) (com Q) pq = \u03bb m \u2192 ox\u1d3e-fst (P m) (com Q) (pq `L m)\n ox\u1d3e-fst (\u03a3\u1d3e M P) (com Q) pq = \u00d7-map id (\u03bb {x} \u2192 ox\u1d3e-fst (P x) (com Q)) (pq `L)\n\n ox\u1d3e-snd : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 Q \u27e7\n ox\u1d3e-snd end Q pq = pq\n ox\u1d3e-snd (com _) end pq = _\n ox\u1d3e-snd (com P) (\u03a0\u1d3e M' Q) pq = \u03bb m' \u2192 ox\u1d3e-snd (com P) (Q m') (pq `R m')\n ox\u1d3e-snd (com P) (\u03a3\u1d3e M' Q) pq = \u00d7-map id (\u03bb {m'} \u2192 ox\u1d3e-snd (com P) (Q m')) (pq `R)\n\n ox\u1d3e\u2192\u00d7 : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n ox\u1d3e\u2192\u00d7 P Q p = ox\u1d3e-fst P Q p , ox\u1d3e-snd P Q p\n\n ox\u1d3e-comma-fst : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-fst P Q (comma\u1d3e P Q p q) \u2261 p\n ox\u1d3e-comma-fst end Q p q = refl\n ox\u1d3e-comma-fst (com P) end p q = refl\n ox\u1d3e-comma-fst (com P) (com Q) p q with view-com P p\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | send P m p = cong (_,_ m) (ox\u1d3e-comma-fst (P m) (com Q) p q)\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | recv P p = funExt \u03bb m \u2192 ox\u1d3e-comma-fst (P m) (com Q) (p m) q\n\n ox\u1d3e-comma-snd : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-snd P Q (comma\u1d3e P Q p q) \u2261 q\n ox\u1d3e-comma-snd end Q p q = refl\n ox\u1d3e-comma-snd (com P) end p q = refl\n ox\u1d3e-comma-snd (com P) (com Q) p q with view-com Q q\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p (q m)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p (q m)\n\n {-\n ox\u1d3e\u2192\u00d7-rinv : \u2200 {P Q} (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 \u00d7\u2192ox\u1d3e P Q (ox\u1d3e\u2192\u00d7 P Q p) \u2261 p\n ox\u1d3e\u2192\u00d7-rinv {P} {Q} p with viewox P Q p\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk In M\u2081 P\u2081} .p = funExt \u03bb { `L \u2192 funExt \u03bb m \u2192 {!ox\u1d3e\u2192\u00d7-rinv {P m} {\u03a0\u1d3e _ P\u2081} ?!} ; `R \u2192 {!!} }\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk In M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | endL .p = refl\n ox\u1d3e\u2192\u00d7-rinv p | endR {P} .p = refl\n\n \u00d7\u2192ox\u1d3e-equiv : \u2200 P Q \u2192 Equiv (\u00d7\u2192ox\u1d3e P Q)\n \u00d7\u2192ox\u1d3e-equiv P Q = record { linv = ox\u1d3e\u2192\u00d7 P Q\n ; is-linv = \u03bb { (x , y) \u2192 cong\u2082 _,_ (ox\u1d3e-comma-fst P Q x y) (ox\u1d3e-comma-snd P Q x y) }\n ; rinv = ox\u1d3e\u2192\u00d7 P Q\n ; is-rinv = ox\u1d3e\u2192\u00d7-rinv {P} {Q} }\n -}\n\n -- left-biased strategy\n par : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par P Q p q = par-view (view-proc P p)\n where par-view : \u2200 {P} {p : \u27e6 P \u27e7} \u2192 ViewProc P p \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par-view (send M P m p) = \u214b\u1d3e-sendL Q m (par (P m) Q p q)\n par-view (recv M P p) = \u214b\u1d3e-recvL Q \u03bb m \u2192 par (P m) Q (p m) q\n par-view end = q\n\n \u214b\u1d3e-apply : \u2200 {P Q} \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply {P} {Q} pq p with view-\u214b P Q pq\n \u214b\u1d3e-apply ._ p | sendL' P Q m pq = \u214b\u1d3e-apply {P m} pq (p m)\n \u214b\u1d3e-apply ._ p | sendR' P Q m pq = m , \u214b\u1d3e-apply {com P} {Q m} pq p\n \u214b\u1d3e-apply ._ (m , p) | recvL' P Q pq = \u214b\u1d3e-apply {P m} (pq m) p\n \u214b\u1d3e-apply ._ p | recvR' P Q pq = \u03bb m \u2192 \u214b\u1d3e-apply {com P} {Q m} (pq m) p\n \u214b\u1d3e-apply pq p | endL Q .pq = pq\n \u214b\u1d3e-apply pq p | endR P .pq = _\n\n \u214b\u1d3e-apply' : \u2200 {P Q} \u2192 \u27e6 dual P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply' {P} {Q} pq p = \u214b\u1d3e-apply {dual P} {Q} pq (subst \u27e6_\u27e7 (\u2261.sym (\u2261\u1d3e-sound funExt (dual-involutive P))) p)\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = \u214b\u1d3e-\u2218-view (view-\u2218 P Q R pq qr)\n where\n \u214b\u1d3e-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u2218 P Q R pq qr \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218-view (sendLL P Q R m p qr) = \u214b\u1d3e-sendL R m (\u214b\u1d3e-\u2218 (P m) (com Q) R p qr)\n \u214b\u1d3e-\u2218-view (recvLL P Q R p qr) = \u214b\u1d3e-recvL R (\u03bb m \u2192 \u214b\u1d3e-\u2218 (P m) (com Q) R (p m) qr)\n \u214b\u1d3e-\u2218-view (sendRR P Q R m pq q) = \u214b\u1d3e-sendR (com P) m (\u214b\u1d3e-\u2218 (com P) (com Q) (R m) pq q)\n \u214b\u1d3e-\u2218-view (recvRR P Q R pq q) = \u214b\u1d3e-recvR (com P) (\u03bb m\u2081 \u2192 \u214b\u1d3e-\u2218 (com P) (com Q) (R m\u2081) pq (q m\u2081))\n \u214b\u1d3e-\u2218-view (sendR-recvL P Q R m p q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) p (q m)\n \u214b\u1d3e-\u2218-view (recvR-sendL P Q R p m q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) (p m) q\n \u214b\u1d3e-\u2218-view (endL Q R pq qr) = \u214b\u1d3e-apply' {Q} {R} qr pq\n \u214b\u1d3e-\u2218-view (endM P R pq qr) = par (com P) R pq qr\n \u214b\u1d3e-\u2218-view (endR P Q pq qr) = \u214b\u1d3e-apply {com Q} {com P} (fst (\u214b\u1d3e-comm (com P) (com Q)) pq) qr\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map P Q R S f g p = comma\u1d3e Q S (f (ox\u1d3e-fst P R p)) (g (ox\u1d3e-snd P R p))\n\n {-\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = switchL' P Q R (ox\u1d3e-fst (P \u214b\u1d3e Q) R pqr) (ox\u1d3e-snd (P \u214b\u1d3e Q) R pqr)\n -}\n\n {-\n switchL' : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7) (r : \u27e6 R \u27e7) \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL' P Q R pq r = {!switchL-view {!!}!}\n where\n switchL-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{r : \u27e6 R \u27e7} \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL-view {P} {Q} {R} {p\u214bq} {r} with view-\u214b P Q p\u214bq | view-proc R r\n switchL-view | sendL' P\u2081 Q\u2081 m p | vr = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | send M\u2081 P\u2082 m\u2081 p\u2081 = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | end = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | send M\u2081 P\u2082 m p\u2081 = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | end = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | send M\u2081 P\u2082 m p\u2081 = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | end = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | send M P\u2081 m p = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | recv M P\u2081 r\u2081 = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | end = {!!}\n switchL-view | endR P\u2081 pq\u2081 | send M P\u2082 m p = {!!}\n switchL-view | endR P\u2081 pq\u2081 | recv M P\u2082 r\u2081 = {!!}\n switchL-view | endR P\u2081 pq\u2081 | end = {!!}\n {-\n switchL-view {R = R}{r = r} | sendL' P Q m p = \u214b\u1d3e-sendL {!!} m (switchL' {!!} {!!} {!!} p r)\n switchL-view {R = R}{r = r} | sendR' P Q m p with view-proc R r\n switchL-view | sendR' {M\u2081} P\u2081 Q m\u2081 p\u2081 | send M P m p = {!!}\n switchL-view | sendR' {M\u2081} P\u2081 Q m p | recv M P r = {!!}\n switchL-view | sendR' {M} P Q m p | end = {!!}\n -- {!\u214b\u1d3e-map (com P) (com P) (Q m ox\u1d3e R) (\u03a3\u1d3e M Q ox\u1d3e R) id (ox\u1d3e-map (Q m) (\u03a3\u1d3e M Q) R R (_,_ m) id) (switchL-view (com P) (Q m) R p r)!}\n switchL-view {R = R}{r = r} | recvL' P Q p = \u214b\u1d3e-recvL (com Q ox\u1d3e R) \u03bb m \u2192 switchL' (P m) (com Q) R (p m) r\n switchL-view {R = R}{r = r} | recvR' (\u03a0\u1d9c M' P) Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL-view {R = R}{r = r} | recvR' (\u03a3\u1d9c M' P) Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL-view {R = R}{r = r} | endL Q p\u214bq = comma\u1d3e Q R p\u214bq r\n switchL-view {R = R}{r = r} | endR P p\u214bq = par (com P) R p\u214bq r\n -}\n -}\n\n \u2295\u1d3e-map : \u2200 {P Q R S} \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map f g (`L , pr) = `L , f pr\n \u2295\u1d3e-map f g (`R , pr) = `R , g pr\n\n &\u1d3e-map : \u2200 {P Q R S} \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map f g p `L = {!!}\n &\u1d3e-map f g p `R = {!!}\n -}\n\n {-\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Log P \u00d7 Log Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Log B \u00d7 Log E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map; proj\u2081 to fst; proj\u2082 to snd)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\nopen import Data.One hiding (_\u225f_)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP as \u2261\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\n {-\ndata S<_> {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n\npattern com' q M P = com (mk q M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\nopen ProtoRel _\u2261_ public renaming (_\u2248\u1d3e_ to _\u2261\u1d3e_)\n\n{-\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n -}\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n Trace : Proto \u2192 Proto\n Trace end = end\n Trace (com c) = com (Trace\u1d9c c)\n\n Trace\u1d9c : Com \u2192 Com\n Trace\u1d9c c = \u03a3\u1d9c (Com.M c) \u03bb m \u2192 Trace (Com.P c m)\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c c = mk (dual\u1d35\u1d3c (Com.io c)) (Com.M c) \u03bb m \u2192 dual (Com.P c m)\n\ndata IsTrace : Proto \u2192 \u2605\u2081 where\n end : IsTrace end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsTrace (P m)) \u2192 IsTrace (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n\u27e6_\u27e7\u27e8_\u2248_\u27e9 : (P : Proto) (p q : \u27e6 P \u27e7) \u2192 \u2605\n\u27e6 end \u27e7\u27e8 p \u2248 q \u27e9 = \ud835\udfd9\n\u27e6 \u03a0\u1d3e M P \u27e7\u27e8 p \u2248 q \u27e9 = (m : M) \u2192 \u27e6 P m \u27e7\u27e8 p m \u2248 q m \u27e9\n\u27e6 \u03a3\u1d3e M P \u27e7\u27e8 p \u2248 q \u27e9 = \u03a3 (fst p \u2261 fst q) \u03bb e \u2192 \u27e6 P (fst q) \u27e7\u27e8 subst (\u27e6_\u27e7 \u2218 P) e (snd p) \u2248 snd q \u27e9\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\nmutual\n data Dual\u1d9c : Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a0\u1d9c M P) (\u03a3\u1d9c M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a3\u1d9c M P) (\u03a0\u1d9c M Q)\n\n data Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n {-\n \u03a0\u2610\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P [ x ]) (Q x)) \u2192 Dual (\u03a0\u1d3e (\u2610 M) P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0\u2610 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q [ x ])) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e (\u2610 M) Q)\n -}\n\n {-\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Trace : Choreo I \u2192 Proto\n \u2102Trace \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Trace-IsTrace : \u2200 \u2102 \u2192 IsTrace (\u2102Trace \u2102)\n \u2102Trace-IsTrace (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n-}\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com refl M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com refl M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\ndual\u1d35\u1d3c-involutive : \u2200 io \u2192 dual\u1d35\u1d3c (dual\u1d35\u1d3c io) \u2261 io\ndual\u1d35\u1d3c-involutive In = refl\ndual\u1d35\u1d3c-involutive Out = refl\n\ndual-involutive : \u2200 P \u2192 dual (dual P) \u2261\u1d3e P\ndual-involutive end = end\ndual-involutive (com' q M P) = com (dual\u1d35\u1d3c-involutive q) M (\u03bb m \u2192 dual-involutive (P m))\n\nTrace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261\u1d3e Trace P\nTrace-idempotent end = end\nTrace-idempotent (com' _ M P) = com refl M \u03bb m \u2192 Trace-idempotent (P m)\n\nTrace-dual-oblivious : \u2200 P \u2192 Trace (dual P) \u2261\u1d3e Trace P\nTrace-dual-oblivious end = end\nTrace-dual-oblivious (com' _ M P) = com refl M \u03bb m \u2192 Trace-dual-oblivious (P m)\n\nSink : Proto \u2192 Proto\nSink = dual \u2218 Trace\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n{-\n++Tele : \u2200 (P : Proto){Q : Tele P \u2192 Proto} (xs : Tele P) \u2192 Tele (Q xs) \u2192 Tele (P >>= Q)\n++Tele end _ ys = ys\n++Tele (com' q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com q M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com q M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n{-\nDual-sym (\u03a0\u2610\u00b7\u03a3 f) = {!\u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))!}\nDual-sym (\u03a3\u00b7\u03a0\u2610 f) = {!\u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))!}\n-}\n-}\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele (dual P) \u2261 Tele P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n El->>= : (P : Proto){Q : Tele P \u2192 Proto}{X : Tele (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Tele P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n-}\n\n>>=-com : (P : Proto){Q : Tele P \u2192 Proto}{R : Tele P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Tele P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Tele P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : \u2605_ a} (l r : A) \u2192 LR \u2192 A \n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a0\u1d3e LR [L: l R: r ]\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP >>\u1d9c S = record P { P = \u03bb m \u2192 S (Com.P P m) }\n\nmodule Equivalences\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n record Equiv {A B : \u2605}(f : A \u2192 B) : \u2605 where\n field\n linv : B \u2192 A\n is-linv : \u2200 x \u2192 linv (f x) \u2261 x\n rinv : B \u2192 A\n is-rinv : \u2200 x \u2192 f (rinv x) \u2261 x\n\n id\u1d31 : \u2200 {A} \u2192 Equiv {A} id\n id\u1d31 = record\n { linv = id\n ; is-linv = \u03bb _ \u2192 refl\n ; rinv = id\n ; is-rinv = \u03bb _ \u2192 refl\n }\n\n module _ {A B C}{g : B \u2192 C}{f : A \u2192 B} where\n _\u2218\u1d31_ : Equiv g \u2192 Equiv f \u2192 Equiv (g \u2218 f)\n g\u1d31 \u2218\u1d31 f\u1d31 = record { linv = F.linv \u2218 G.linv ; is-linv = \u03bb x \u2192 cong F.linv (G.is-linv (f x)) \u2219 F.is-linv x\n ; rinv = F.rinv \u2218 G.rinv ; is-rinv = \u03bb x \u2192 cong g (F.is-rinv _) \u2219 G.is-rinv x }\n where\n module G = Equiv g\u1d31\n module F = Equiv f\u1d31\n\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = \u03a3 (A \u2192 B) Equiv\n\n module _ {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b} where\n \u03a3-ext : \u2200 {x y : \u03a3 A B} \u2192 (p : fst x \u2261 fst y) \u2192 subst B p (snd x) \u2261 snd y \u2192 x \u2261 y\n \u03a3-ext refl = cong (_,_ _)\n\ndata ViewProc : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200 M(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewProc (\u03a3\u1d3e M P) (m , p)\n recv : \u2200 M(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewProc (\u03a0\u1d3e M P) p\n end : ViewProc end _\n\nview-proc : \u2200 P (p : \u27e6 P \u27e7) \u2192 ViewProc P p\nview-proc end _ = end\nview-proc (\u03a0\u1d3e M P) p = recv _ _ p\nview-proc (\u03a3\u1d3e M P) (m , p) = send _ _ m p\n\ndata ViewCom : \u2200 P \u2192 \u27e6 com P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200{M}(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewCom (\u03a3\u1d9c M P) (m , p)\n recv : \u2200{M}(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewCom (\u03a0\u1d9c M P) p\n\nview-com : \u2200 P (p : \u27e6 com P \u27e7) \u2192 ViewCom P p\nview-com (\u03a0\u1d9c M P) p = recv _ p\nview-com (\u03a3\u1d9c M P) (m , p) = send _ m p\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open Equivalences funExt\n\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n \u03a0\u1d3e M P \u214b\u1d3e Q = \u03a0\u1d3e M \u03bb m \u2192 P m \u214b\u1d3e Q\n P \u214b\u1d3e end = P\n P \u214b\u1d3e \u03a0\u1d3e M Q = \u03a0\u1d3e M \u03bb m \u2192 P \u214b\u1d3e Q m\n \u03a3\u1d3e M P \u214b\u1d3e \u03a3\u1d3e M' Q = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 P m \u214b\u1d3e \u03a3\u1d3e M' Q)\n ,inr: (\u03bb m' \u2192 \u03a3\u1d3e M P \u214b\u1d3e Q m') ]\n\n data View-\u214b : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n sendL' : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto)(m : M )(p : \u27e6 P m \u214b\u1d3e \u03a3\u1d3e M' Q \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p)\n sendR' : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto)(m' : M')(p : \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q m' \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m' , p)\n recvL' : \u2200 {M} (P : M \u2192 Proto) Q (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7)) \u2192 View-\u214b (\u03a0\u1d3e M P) Q p\n recvR' : \u2200 {M M'} (P : M \u2192 Proto) (Q : M' \u2192 Proto)(p : (m' : M') \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q m' \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p\n endL : \u2200 Q (p : \u27e6 Q \u27e7) \u2192 View-\u214b end Q p\n endR : \u2200 {M} (P : M \u2192 Proto) (p : \u27e6 \u03a3\u1d3e M P \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) end p\n\n view-\u214b : \u2200 P Q (p : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 View-\u214b P Q p\n view-\u214b end Q p = endL Q p\n view-\u214b (\u03a0\u1d3e M P) Q p = recvL' P Q p\n view-\u214b (\u03a3\u1d3e M P) end p = endR P p\n view-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = recvR' P Q p\n view-\u214b (com (mk Out M P)) (com (mk Out M' Q)) (inl x , p) = sendL' P Q x p\n view-\u214b (com (mk Out M P)) (com (mk Out M' Q)) (inr y , p) = sendR' P Q y p\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7 \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7\n \u214b\u1d3e-assoc end Q R s = s\n \u214b\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = \u214b\u1d3e-assoc (P m) _ _ (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , \u214b\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end p = p\n \u214b\u1d3e-rend (\u03a0\u1d3e _ P) p = \u03bb m \u2192 \u214b\u1d3e-rend (P m) (p m)\n \u214b\u1d3e-rend (\u03a3\u1d3e _ _) p = p\n\n \u214b\u1d3e-rend' : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u214b\u1d3e end \u27e7\n \u214b\u1d3e-rend' end p = p\n \u214b\u1d3e-rend' (\u03a0\u1d3e _ P) p = \u03bb m \u2192 \u214b\u1d3e-rend' (P m) (p m)\n \u214b\u1d3e-rend' (\u03a3\u1d3e _ _) p = p\n\n {-\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n -}\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (\u03a3\u1d3e M P) m p = inr m , p\n \u214b\u1d3e-sendR (\u03a0\u1d3e M P) m p = \u03bb x \u2192 \u214b\u1d3e-sendR (P x) m (p x)\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL (\u03a0\u1d3e M Q) m p = \u03bb m' \u2192 \u214b\u1d3e-sendL (Q m') m {!p!}\n \u214b\u1d3e-sendL (\u03a3\u1d3e M Q) m p = {!!} , p\n\n {-\n \u214b\u1d3e-sendR : \u2200 {M} P Q \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e M Q \u27e7 \u2192 (m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7\n \u214b\u1d3e-sendR P Q s m = {!!}\n -}\n\n \u214b\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e M Q \u27e7\n \u214b\u1d3e-recvR end Q s = s\n \u214b\u1d3e-recvR (\u03a0\u1d3e M P) Q s = \u03bb x \u2192 \u214b\u1d3e-recvR (P x) Q (\u03bb m \u2192 s m x)\n \u214b\u1d3e-recvR (\u03a3\u1d3e M P) Q s = s\n\n mutual\n \u214b\u1d3e-! : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-! P Q p = {!!}\n\n \u214b\u1d3e-!-view : \u2200 {P Q} {pq : \u27e6 P \u214b\u1d3e Q \u27e7} \u2192 View-\u214b P Q pq \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-!-view (sendL' P Q m p) = \u214b\u1d3e-sendR (\u03a3\u1d3e _ Q) m (\u214b\u1d3e-! (P m) (com (mk Out _ Q)) p)\n \u214b\u1d3e-!-view (sendR' P Q m' p) = {!\u214b\u1d3e-sendL (\u03a3\u1d3e _ P) m'!}\n \u214b\u1d3e-!-view (recvL' P Q pq) = {!!}\n \u214b\u1d3e-!-view (recvR' P Q pq) = {!!}\n \u214b\u1d3e-!-view (endL Q pq) = {!!}\n \u214b\u1d3e-!-view (endR P pq) = {!!}\n\n {-\n end Q p = \u214b\u1d3e-rend' Q p\n {-\n \u214b\u1d3e-! end Q p = \u214b\u1d3e-rend' Q p\n \u214b\u1d3e-! (\u03a0\u1d3e M P) end p x = \u214b\u1d3e-! (P x) end (p x)\n \u214b\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 {!\u214b\u1d3e-! (\u03a0 !}\n \u214b\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 \u214b\u1d3e-! (P m) (com (mk Out M' Q)) (p m)\n \u214b\u1d3e-! (\u03a3\u1d3e M P) end p = p\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 \u214b\u1d3e-! (com (mk Out M P)) (Q m') (p m')\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (\u214b\u1d3e-! (P m) (com (mk Out M' Q)) p)\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (\u214b\u1d3e-! (com (mk Out M P)) (Q m) p)\n\n {-\n \u214b\u1d3e-apply : \u2200 P Q \u2192 \u27e6 \u214b\u1d3e P Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply end Q s p = s\n \u214b\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = \u214b\u1d3e-apply (P m) Q (s m) p\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = \u214b\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = \u214b\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , \u214b\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n -}\n\n {-\nmodule V4\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n P \u214b\u1d3e end = P\n com P \u214b\u1d3e com Q = \u03a3\u1d3e LR (P \u214b\u1d9c Q)\n\n _\u214b\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 \u214b\u1d9c P\u1d3f) `L = P\u1d38 \u214b\u1d9cL P\u1d3f\n (P\u1d38 \u214b\u1d9c P\u1d3f) `R = P\u1d38 \u214b\u1d9cR P\u1d3f\n\n _\u214b\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) \u214b\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m \u214b\u1d3e com Q)\n\n _\u214b\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P \u214b\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P \u214b\u1d3e P\u1d3f m)\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end ox\u1d3e Q = Q\n P ox\u1d3e end = P\n com P\u1d38 ox\u1d3e com P\u1d3f = \u03a0\u1d3e LR (P\u1d38 ox\u1d9c P\u1d3f)\n\n _ox\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 ox\u1d9c P\u1d3f) `L = P\u1d38 ox\u1d9cL P\u1d3f\n (P\u1d38 ox\u1d9c P\u1d3f) `R = P\u1d38 ox\u1d9cR P\u1d3f\n\n _ox\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) ox\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m ox\u1d3e com Q)\n\n _ox\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P ox\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P ox\u1d3e P\u1d3f m)\n\n data Viewox : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n com : \u2200{P Q}(p : \u27e6 \u03a0\u1d3e LR (P ox\u1d9c Q) \u27e7) \u2192 Viewox (com P) (com Q) p\n endL : \u2200{Q}(p : \u27e6 Q \u27e7) \u2192 Viewox end Q p\n endR : \u2200{P}(p : \u27e6 com P \u27e7) \u2192 Viewox (com P) end p\n\n viewox : \u2200 P Q (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 Viewox P Q p\n viewox end Q p = endL p\n viewox (com P) end p = endR p\n viewox (com P) (com Q) p = com p\n\n data View-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u2605\u2081 where\n sendLL : \u2200 {M} (P : M \u2192 Proto) Q R (m : M)(p : \u27e6 P m \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e M P) (com Q) R (`L , m , p) q\n recvLL : \u2200 {M} (P : M \u2192 Proto) Q R\n (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e com Q \u27e7))(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a0\u1d3e M P) (com Q) R (`L , p) q\n sendRR : \u2200 {M} P Q (R : M \u2192 Proto)(m : M)(p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a3\u1d3e M R) p (`R , m , q)\n recvRR : \u2200 {M} P Q (R : M \u2192 Proto)\n (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : (m : M) \u2192 \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a0\u1d3e M R) p (`R , q)\n\n sendR-recvL : \u2200 {M} P (Q : M \u2192 Proto) R (m : M)(p : \u27e6 com P \u214b\u1d3e Q m \u27e7)(q : (m : M) \u2192 \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a3\u1d3e M Q) (com R) (`R , m , p) (`L , q)\n recvR-sendL : \u2200 {M} P (Q : M \u2192 Proto) R (p : (m : M) \u2192 \u27e6 com P \u214b\u1d3e Q m \u27e7)(m : M)(q : \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a0\u1d3e M Q) (com R) (`R , p) (`L , m , q)\n\n endL : \u2200 Q R\n \u2192 (q : \u27e6 Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 end Q R q qr\n\n endM : \u2200 P R\n \u2192 (p : \u27e6 com P \u27e7)(r : \u27e6 R \u27e7)\n \u2192 View-\u2218 (com P) end R p r\n endR : \u2200 P Q\n \u2192 (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u27e7)\n \u2192 View-\u2218 (com P) (com Q) end p q\n\n view-\u2218 : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7) \u2192 View-\u2218 P Q R pq qr\n view-\u2218 P Q R pq qr = view-\u2218-view (view-\u214b P Q pq) (view-\u214b (dual Q) R qr)\n where\n view-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u214b P Q pq \u2192 View-\u214b (dual Q) R qr \u2192 View-\u2218 P Q R pq qr\n view-\u2218-view (sendL' _ _ _ _) _ = sendLL _ _ _ _ _ _\n view-\u2218-view (recvL' _ _ _) _ = recvLL _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvL' ._ _ _) = sendR-recvL _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (endR ._ _) = endR _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendL' ._ _ _ _) = recvR-sendL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (endR ._ ._) = endR _ _ _ _\n view-\u2218-view (endL _ _) _ = endL _ _ _ _\n view-\u2218-view (endR _ _) _ = endM _ _ _ _\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end = id\n \u214b\u1d3e-rend (com _) = id\n\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (com P) m p = `R , (m , p)\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL (com _) m p = `L , (m , p)\n\n \u214b\u1d3e-recvR : \u2200 {M}P{Q : M \u2192 Proto} \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e com' In M Q \u27e7\n \u214b\u1d3e-recvR end f = f\n \u214b\u1d3e-recvR (com _) f = `R , f\n\n \u214b\u1d3e-recvL : \u2200 {M}{P : M \u2192 Proto}Q \u2192 ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7) \u2192 \u27e6 \u03a0\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-recvL {P = P} end f x = \u214b\u1d3e-rend (P x) (f x)\n \u214b\u1d3e-recvL (com _) f = `L , f\n\n ox\u1d3e-rend : \u2200 P \u2192 \u27e6 P ox\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-rend end = id\n ox\u1d3e-rend (com x) = id\n\n ox\u1d3e-sendR : \u2200 {M P}{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n ox\u1d3e-sendR {P = end} m p = m , p\n ox\u1d3e-sendR {P = com x} m p = `R , (m , p)\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id end = _\n \u214b\u1d3e-id (com (mk In M P)) = `R , \u03bb m \u2192 \u214b\u1d3e-sendL (P m) m (\u214b\u1d3e-id (P m))\n \u214b\u1d3e-id (com (mk Out M P)) = `L , \u03bb m \u2192 \u214b\u1d3e-sendR (dual (P m)) m (\u214b\u1d3e-id (P m))\n\n module _ where\n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm = \u03bb P Q \u2192 to P Q , equiv P Q\n where\n to : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n to end end pq = pq\n to end (com x) pq = pq\n to (com x) end pq = pq\n to (com (mk In M P)) (com x\u2081) (`L , pq) = `R , (\u03bb m \u2192 to (P m) (com x\u2081) (pq m))\n to (com (mk Out M P)) (com x\u2081) (`L , m , pq) = `R , m , to (P m) (com x\u2081) pq\n to (com x) (com (mk In M P)) (`R , pq) = `L , (\u03bb m \u2192 to (com x) (P m) (pq m))\n to (com x) (com (mk Out M P)) (`R , m , pq) = `L , m , to (com x) (P m) pq\n\n toto : \u2200 P Q (x : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 to Q P (to P Q x) \u2261 x\n toto end end x = refl\n toto end (com (mk io M P)) x\u2081 = refl\n toto (com (mk io M P)) end x\u2081 = refl\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' In M\u2081 P\u2081) (pq x))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' Out M\u2081 P\u2081) (pq x))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' In M\u2081 P\u2081) pq))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' Out M\u2081 P\u2081) pq))\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' In M P) (P\u2081 x) (pq x)))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' In M P) (P\u2081 m) pq))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' Out M P) (P\u2081 x) (pq x)))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' Out M P) (P\u2081 m) pq))\n\n equiv : \u2200 P Q \u2192 Equiv (to P Q)\n equiv P Q = record { linv = to Q P ; is-linv = toto P Q ; rinv = to Q P ; is-rinv = toto Q P }\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e end Q p q = q\n comma\u1d3e (com _) end p q = p\n comma\u1d3e (\u03a3\u1d3e M P) (com Q) (m , p) q `L = m , comma\u1d3e (P m) (com Q) p q\n comma\u1d3e (\u03a0\u1d3e M P) (com Q) p q `L = \u03bb m \u2192 comma\u1d3e (P m) (com Q) (p m) q\n comma\u1d3e (com P) (\u03a3\u1d3e M Q) p (m , q) `R = m , comma\u1d3e (com P) (Q m) p q\n comma\u1d3e (com P) (\u03a0\u1d3e M Q) p q `R = \u03bb m \u2192 comma\u1d3e (com P) (Q m) p (q m)\n\n \u00d7\u2192ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n \u00d7\u2192ox\u1d3e P Q (p , q) = comma\u1d3e P Q p q\n\n ox\u1d3e-fst : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-fst end Q pq = _\n ox\u1d3e-fst (com _) end pq = pq\n ox\u1d3e-fst (\u03a0\u1d3e M P) (com Q) pq = \u03bb m \u2192 ox\u1d3e-fst (P m) (com Q) (pq `L m)\n ox\u1d3e-fst (\u03a3\u1d3e M P) (com Q) pq = \u00d7-map id (\u03bb {x} \u2192 ox\u1d3e-fst (P x) (com Q)) (pq `L)\n\n ox\u1d3e-snd : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 Q \u27e7\n ox\u1d3e-snd end Q pq = pq\n ox\u1d3e-snd (com _) end pq = _\n ox\u1d3e-snd (com P) (\u03a0\u1d3e M' Q) pq = \u03bb m' \u2192 ox\u1d3e-snd (com P) (Q m') (pq `R m')\n ox\u1d3e-snd (com P) (\u03a3\u1d3e M' Q) pq = \u00d7-map id (\u03bb {m'} \u2192 ox\u1d3e-snd (com P) (Q m')) (pq `R)\n\n ox\u1d3e\u2192\u00d7 : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n ox\u1d3e\u2192\u00d7 P Q p = ox\u1d3e-fst P Q p , ox\u1d3e-snd P Q p\n\n ox\u1d3e-comma-fst : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-fst P Q (comma\u1d3e P Q p q) \u2261 p\n ox\u1d3e-comma-fst end Q p q = refl\n ox\u1d3e-comma-fst (com P) end p q = refl\n ox\u1d3e-comma-fst (com P) (com Q) p q with view-com P p\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | send P m p = cong (_,_ m) (ox\u1d3e-comma-fst (P m) (com Q) p q)\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | recv P p = funExt \u03bb m \u2192 ox\u1d3e-comma-fst (P m) (com Q) (p m) q\n\n ox\u1d3e-comma-snd : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-snd P Q (comma\u1d3e P Q p q) \u2261 q\n ox\u1d3e-comma-snd end Q p q = refl\n ox\u1d3e-comma-snd (com P) end p q = refl\n ox\u1d3e-comma-snd (com P) (com Q) p q with view-com Q q\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p (q m)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p (q m)\n\n {-\n ox\u1d3e\u2192\u00d7-rinv : \u2200 {P Q} (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 \u00d7\u2192ox\u1d3e P Q (ox\u1d3e\u2192\u00d7 P Q p) \u2261 p\n ox\u1d3e\u2192\u00d7-rinv {P} {Q} p with viewox P Q p\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk In M\u2081 P\u2081} .p = funExt \u03bb { `L \u2192 funExt \u03bb m \u2192 {!ox\u1d3e\u2192\u00d7-rinv {P m} {\u03a0\u1d3e _ P\u2081} ?!} ; `R \u2192 {!!} }\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk In M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | endL .p = refl\n ox\u1d3e\u2192\u00d7-rinv p | endR {P} .p = refl\n\n \u00d7\u2192ox\u1d3e-equiv : \u2200 P Q \u2192 Equiv (\u00d7\u2192ox\u1d3e P Q)\n \u00d7\u2192ox\u1d3e-equiv P Q = record { linv = ox\u1d3e\u2192\u00d7 P Q\n ; is-linv = \u03bb { (x , y) \u2192 cong\u2082 _,_ (ox\u1d3e-comma-fst P Q x y) (ox\u1d3e-comma-snd P Q x y) }\n ; rinv = ox\u1d3e\u2192\u00d7 P Q\n ; is-rinv = ox\u1d3e\u2192\u00d7-rinv {P} {Q} }\n -}\n\n -- left-biased strategy\n par : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par P Q p q = par-view (view-proc P p)\n where par-view : \u2200 {P} {p : \u27e6 P \u27e7} \u2192 ViewProc P p \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par-view (send M P m p) = \u214b\u1d3e-sendL Q m (par (P m) Q p q)\n par-view (recv M P p) = \u214b\u1d3e-recvL Q \u03bb m \u2192 par (P m) Q (p m) q\n par-view end = q\n\n \u214b\u1d3e-apply : \u2200 {P Q} \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply {P} {Q} pq p with view-\u214b P Q pq\n \u214b\u1d3e-apply ._ p | sendL' P Q m pq = \u214b\u1d3e-apply {P m} pq (p m)\n \u214b\u1d3e-apply ._ p | sendR' P Q m pq = m , \u214b\u1d3e-apply {com P} {Q m} pq p\n \u214b\u1d3e-apply ._ (m , p) | recvL' P Q pq = \u214b\u1d3e-apply {P m} (pq m) p\n \u214b\u1d3e-apply ._ p | recvR' P Q pq = \u03bb m \u2192 \u214b\u1d3e-apply {com P} {Q m} (pq m) p\n \u214b\u1d3e-apply pq p | endL Q .pq = pq\n \u214b\u1d3e-apply pq p | endR P .pq = _\n\n \u214b\u1d3e-apply' : \u2200 {P Q} \u2192 \u27e6 dual P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply' {P} {Q} pq p = \u214b\u1d3e-apply {dual P} {Q} pq (subst \u27e6_\u27e7 (\u2261.sym (\u2261\u1d3e-sound funExt (dual-involutive P))) p)\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = \u214b\u1d3e-\u2218-view (view-\u2218 P Q R pq qr)\n where\n \u214b\u1d3e-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u2218 P Q R pq qr \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218-view (sendLL P Q R m p qr) = \u214b\u1d3e-sendL R m (\u214b\u1d3e-\u2218 (P m) (com Q) R p qr)\n \u214b\u1d3e-\u2218-view (recvLL P Q R p qr) = \u214b\u1d3e-recvL R (\u03bb m \u2192 \u214b\u1d3e-\u2218 (P m) (com Q) R (p m) qr)\n \u214b\u1d3e-\u2218-view (sendRR P Q R m pq q) = \u214b\u1d3e-sendR (com P) m (\u214b\u1d3e-\u2218 (com P) (com Q) (R m) pq q)\n \u214b\u1d3e-\u2218-view (recvRR P Q R pq q) = \u214b\u1d3e-recvR (com P) (\u03bb m\u2081 \u2192 \u214b\u1d3e-\u2218 (com P) (com Q) (R m\u2081) pq (q m\u2081))\n \u214b\u1d3e-\u2218-view (sendR-recvL P Q R m p q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) p (q m)\n \u214b\u1d3e-\u2218-view (recvR-sendL P Q R p m q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) (p m) q\n \u214b\u1d3e-\u2218-view (endL Q R pq qr) = \u214b\u1d3e-apply' {Q} {R} qr pq\n \u214b\u1d3e-\u2218-view (endM P R pq qr) = par (com P) R pq qr\n \u214b\u1d3e-\u2218-view (endR P Q pq qr) = \u214b\u1d3e-apply {com Q} {com P} (fst (\u214b\u1d3e-comm (com P) (com Q)) pq) qr\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map P Q R S f g p = comma\u1d3e Q S (f (ox\u1d3e-fst P R p)) (g (ox\u1d3e-snd P R p))\n\n {-\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = switchL' P Q R (ox\u1d3e-fst (P \u214b\u1d3e Q) R pqr) (ox\u1d3e-snd (P \u214b\u1d3e Q) R pqr)\n -}\n\n {-\n switchL' : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7) (r : \u27e6 R \u27e7) \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL' P Q R pq r = {!switchL-view {!!}!}\n where\n switchL-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{r : \u27e6 R \u27e7} \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL-view {P} {Q} {R} {p\u214bq} {r} with view-\u214b P Q p\u214bq | view-proc R r\n switchL-view | sendL' P\u2081 Q\u2081 m p | vr = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | send M\u2081 P\u2082 m\u2081 p\u2081 = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | end = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | send M\u2081 P\u2082 m p\u2081 = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | end = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | send M\u2081 P\u2082 m p\u2081 = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | end = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | send M P\u2081 m p = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | recv M P\u2081 r\u2081 = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | end = {!!}\n switchL-view | endR P\u2081 pq\u2081 | send M P\u2082 m p = {!!}\n switchL-view | endR P\u2081 pq\u2081 | recv M P\u2082 r\u2081 = {!!}\n switchL-view | endR P\u2081 pq\u2081 | end = {!!}\n {-\n switchL-view {R = R}{r = r} | sendL' P Q m p = \u214b\u1d3e-sendL {!!} m (switchL' {!!} {!!} {!!} p r)\n switchL-view {R = R}{r = r} | sendR' P Q m p with view-proc R r\n switchL-view | sendR' {M\u2081} P\u2081 Q m\u2081 p\u2081 | send M P m p = {!!}\n switchL-view | sendR' {M\u2081} P\u2081 Q m p | recv M P r = {!!}\n switchL-view | sendR' {M} P Q m p | end = {!!}\n -- {!\u214b\u1d3e-map (com P) (com P) (Q m ox\u1d3e R) (\u03a3\u1d3e M Q ox\u1d3e R) id (ox\u1d3e-map (Q m) (\u03a3\u1d3e M Q) R R (_,_ m) id) (switchL-view (com P) (Q m) R p r)!}\n switchL-view {R = R}{r = r} | recvL' P Q p = \u214b\u1d3e-recvL (com Q ox\u1d3e R) \u03bb m \u2192 switchL' (P m) (com Q) R (p m) r\n switchL-view {R = R}{r = r} | recvR' (\u03a0\u1d9c M' P) Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL-view {R = R}{r = r} | recvR' (\u03a3\u1d9c M' P) Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL-view {R = R}{r = r} | endL Q p\u214bq = comma\u1d3e Q R p\u214bq r\n switchL-view {R = R}{r = r} | endR P p\u214bq = par (com P) R p\u214bq r\n -}\n -}\n\n \u2295\u1d3e-map : \u2200 {P Q R S} \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map f g (`L , pr) = `L , f pr\n \u2295\u1d3e-map f g (`R , pr) = `R , g pr\n\n &\u1d3e-map : \u2200 {P Q R S} \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map f g p `L = {!!}\n &\u1d3e-map f g p `R = {!!}\n -}\n\n {-\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Tele B \u00d7 Tele E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a3\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a0\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"6a83206fb6f2245d31f9953821a63d85003599a9","subject":"progress towards lemmas after restructuring TA hole rules","message":"progress towards lemmas after restructuring TA hole rules\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"typed-expansion.agda","new_file":"typed-expansion.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import correspondence\nopen import contexts\n\nmodule typed-expansion where\n lem-idsub : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 id \u0393 :s: \u0393\n lem-idsub {\u0393 = \u0393} x d xin with \u0393 x\n lem-idsub {\u0393 = \u0393} x .(X x) refl | Some \u03c4 = \u03c4 , refl , TAVar {!!}\n lem-idsub x d () | None\n\n lem-weaken\u03941 : \u2200{\u03941 \u03942 \u0393 d \u03c4} \u2192 \u03941 , \u0393 \u22a2 d :: \u03c4 \u2192 (\u03941 \u222a \u03942) , \u0393 \u22a2 d :: \u03c4\n lem-weaken\u03941 TAConst = TAConst\n lem-weaken\u03941 (TAVar x\u2081) = TAVar x\u2081\n lem-weaken\u03941 (TALam D) = TALam (lem-weaken\u03941 D)\n lem-weaken\u03941 (TAAp D x D\u2081) = TAAp (lem-weaken\u03941 D) x (lem-weaken\u03941 D\u2081)\n lem-weaken\u03941 (TAEHole {\u0394 = \u0394} x y) = TAEHole {!!} {!!}\n lem-weaken\u03941 (TANEHole D x y) = TANEHole {!!} (lem-weaken\u03941 x) {!!}\n lem-weaken\u03941 (TACast D x) = TACast (lem-weaken\u03941 D) x\n\n lem-weaken\u03942 : \u2200{\u03941 \u03942 \u0393 d \u03c4} \u2192 \u03942 , \u0393 \u22a2 d :: \u03c4 \u2192 (\u03941 \u222a \u03942) , \u0393 \u22a2 d :: \u03c4\n lem-weaken\u03942 {\u03941} {\u03942} {\u0393} {d} {\u03c4} D = tr (\u03bb x \u2192 x , \u0393 \u22a2 d :: \u03c4) (funext (\u03bb x \u2192 {!!})) D\n\n lem-weaken\u0394single : \u2200{\u0394 \u0393 d \u03c4 u \u0393' \u03c4'} \u2192 \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192 (\u0394 ,, u ::[ \u0393' ] \u03c4') , \u0393 \u22a2 d :: \u03c4\n lem-weaken\u0394single = {!!}\n\n mutual\n typed-expansion-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4\n typed-expansion-synth ESConst = TAConst\n typed-expansion-synth (ESVar x\u2081) = TAVar x\u2081\n typed-expansion-synth (ESLam x\u2081 ex) = TALam (typed-expansion-synth ex)\n typed-expansion-synth (ESAp1 {\u03941 = \u03941} x\u2081 x\u2082 x\u2083)\n with typed-expansion-ana x\u2082 | typed-expansion-ana x\u2083\n ... | con1 , ih1 | con2 , ih2 = TAAp (TACast (lem-weaken\u03941 ih1) con1) MAArr (lem-weaken\u03942 {\u03941 = \u03941} ih2)\n typed-expansion-synth (ESAp2 {\u03941 = \u03941} ex x\u2081 x\u2082)\n with typed-expansion-synth ex | typed-expansion-ana x\u2081\n ... | ih1 | con2 , ih2 = TAAp (lem-weaken\u03941 ih1) MAArr (TACast (lem-weaken\u03942 {\u03941 = \u03941} ih2) con2)\n typed-expansion-synth (ESAp3 {\u03941 = \u03941} ex x\u2081)\n with typed-expansion-synth ex | typed-expansion-ana x\u2081\n ... | ih1 | con2 , ih2 = TAAp (lem-weaken\u03941 ih1) MAArr (lem-weaken\u03942 {\u03941 = \u03941} ih2)\n typed-expansion-synth ESEHole = TAEHole {!!} lem-idsub\n typed-expansion-synth (ESNEHole ex)\n with typed-expansion-synth ex\n ... | ih1 = TANEHole {!!} (lem-weaken\u0394single ih1) lem-idsub\n typed-expansion-synth (ESAsc1 x x\u2081)\n with typed-expansion-ana x\n ... | con , ih = TACast ih con\n typed-expansion-synth (ESAsc2 x)\n with typed-expansion-ana x\n ... | con , ih = ih\n\n typed-expansion-ana : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u03c4 ~ \u03c4') \u00d7 (\u0394 , \u0393 \u22a2 d :: \u03c4')\n typed-expansion-ana (EALam x\u2081 ex)\n with typed-expansion-ana ex\n ... | con , D = (TCArr TCRefl con) , TALam D\n typed-expansion-ana (EASubsume x x\u2081 x\u2082 x\u2083) = x\u2083 , typed-expansion-synth x\u2082\n typed-expansion-ana EAEHole = TCRefl , TAEHole {!!} lem-idsub\n typed-expansion-ana (EANEHole x)\n with typed-expansion-synth x\n ... | ih1 = TCRefl , TANEHole {!!} (lem-weaken\u0394single ih1) lem-idsub\n typed-expansion-ana (EALamHole x y)\n with typed-expansion-ana y\n ... | _ , ih = TCHole2 , TALam ih\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import correspondence\nopen import contexts\n\nmodule typed-expansion where\n lem-idsub : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 id \u0393 :s: \u0393\n lem-idsub {\u0393 = \u0393} x d xin with \u0393 x\n lem-idsub {\u0393 = \u0393} x .(X x) refl | Some \u03c4 = \u03c4 , refl , TAVar {!!}\n lem-idsub x d () | None\n\n lem-weaken\u03941 : \u2200{\u03941 \u03942 \u0393 d \u03c4} \u2192 \u03941 , \u0393 \u22a2 d :: \u03c4 \u2192 (\u03941 \u222a \u03942) , \u0393 \u22a2 d :: \u03c4\n lem-weaken\u03941 TAConst = TAConst\n lem-weaken\u03941 (TAVar x\u2081) = TAVar x\u2081\n lem-weaken\u03941 (TALam D) = TALam (lem-weaken\u03941 D)\n lem-weaken\u03941 (TAAp D x D\u2081) = TAAp (lem-weaken\u03941 D) x (lem-weaken\u03941 D\u2081)\n lem-weaken\u03941 (TAEHole {\u0394 = \u0394} x) = {!TAEHole!}\n lem-weaken\u03941 (TANEHole D x) = {!!}\n lem-weaken\u03941 (TACast D x) = TACast (lem-weaken\u03941 D) x\n\n lem-##eq : {\u03941 \u03942 : hctx} (n : Nat) \u2192 \u03941 ## \u03942 \u2192 \u03941 n == (\u03941 \u222a \u03942) n\n lem-##eq {\u03941} {\u03942} n apart with \u03941 n\n lem-##eq n apart | Some x = refl\n lem-##eq {\u03941} {\u03942} n apart | None with \u03942 n\n lem-##eq n (\u03c01 , \u03c02) | None | Some x = {!!}\n lem-##eq n apart | None | None = refl\n\n lem-weaken\u03942 : \u2200{\u03941 \u03942 \u0393 d \u03c4} \u2192 \u03942 , \u0393 \u22a2 d :: \u03c4 \u2192 (\u03941 \u222a \u03942) , \u0393 \u22a2 d :: \u03c4\n lem-weaken\u03942 {\u03941} {\u03942} {\u0393} {d} {\u03c4} D = tr (\u03bb x \u2192 x , \u0393 \u22a2 d :: \u03c4) (funext (\u03bb x \u2192 {!!})) D\n\n mutual\n typed-expansion-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4\n typed-expansion-synth ESConst = TAConst\n typed-expansion-synth (ESVar x\u2081) = TAVar x\u2081\n typed-expansion-synth (ESLam x\u2081 ex) = TALam (typed-expansion-synth ex)\n typed-expansion-synth (ESAp1 {\u03941 = \u03941} x\u2081 x\u2082 x\u2083)\n with typed-expansion-ana x\u2082 | typed-expansion-ana x\u2083\n ... | con1 , ih1 | con2 , ih2 = TAAp (TACast (lem-weaken\u03941 ih1) con1) MAArr (lem-weaken\u03942 {\u03941 = \u03941} ih2)\n typed-expansion-synth (ESAp2 {\u03941 = \u03941} ex x\u2081 x\u2082)\n with typed-expansion-synth ex | typed-expansion-ana x\u2081\n ... | ih1 | con2 , ih2 = TAAp (lem-weaken\u03941 ih1) MAArr (TACast (lem-weaken\u03942 {\u03941 = \u03941} ih2) con2)\n typed-expansion-synth (ESAp3 {\u03941 = \u03941} ex x\u2081)\n with typed-expansion-synth ex | typed-expansion-ana x\u2081\n ... | ih1 | con2 , ih2 = TAAp (lem-weaken\u03941 ih1) MAArr (lem-weaken\u03942 {\u03941 = \u03941} ih2)\n typed-expansion-synth ESEHole = TAEHole lem-idsub\n typed-expansion-synth (ESNEHole ex) = TANEHole (typed-expansion-synth ex) lem-idsub\n typed-expansion-synth (ESAsc1 x x\u2081)\n with typed-expansion-ana x\n ... | con , ih = TACast ih con\n typed-expansion-synth (ESAsc2 x)\n with typed-expansion-ana x\n ... | con , ih = ih\n\n typed-expansion-ana : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u03c4 ~ \u03c4') \u00d7 (\u0394 , \u0393 \u22a2 d :: \u03c4')\n typed-expansion-ana (EALam x\u2081 ex)\n with typed-expansion-ana ex\n ... | con , D = (TCArr TCRefl con) , TALam D\n typed-expansion-ana (EASubsume x x\u2081 x\u2082 x\u2083) = x\u2083 , typed-expansion-synth x\u2082\n typed-expansion-ana EAEHole = TCRefl , TAEHole lem-idsub\n typed-expansion-ana (EANEHole x) = TCRefl , TANEHole (typed-expansion-synth x) lem-idsub\n typed-expansion-ana (EALamHole x y)\n with typed-expansion-ana y\n ... | _ , ih = TCHole2 , TALam ih\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"53305597f0881c1001550f6c966e1f87162dcd74","subject":"pisearch: search _\u228e_ is \u03a3","message":"pisearch: search _\u228e_ is \u03a3\n","repos":"crypto-agda\/crypto-agda","old_file":"pisearch.agda","new_file":"pisearch.agda","new_contents":"{-# OPTIONS --type-in-type #-}\nmodule pisearch where\nopen import Type hiding (\u2605_)\nopen import Function.NP\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Bool.NP\nopen import Search.Type\nopen import Search.Searchable.Product\nopen import Search.Searchable\nopen import sum\n\nData : \u2200 {A} \u2192 Search A \u2192 (A \u2192 \u2605) \u2192 \u2605\nData sA B = sA _\u00d7_ B\n\nToFun : \u2200 {A} (sA : Search A) \u2192 \u2605\nToFun {A} sA = \u2200 {B} \u2192 Data sA B \u2192 \u03a0 A B\n\nFromFun : \u2200 {A} (sA : Search A) \u2192 \u2605\nFromFun {A} sA = \u2200 {B} \u2192 \u03a0 A B \u2192 Data sA B\n\nfromFun-searchInd : \u2200 {A} {sA : Search A} \u2192 SearchInd sA \u2192 FromFun sA\nfromFun-searchInd indA = indA (\u03bb s \u2192 Data s _) _,_\n\ntoFun-\u03a3 : \u2200 {A} {B : A \u2192 \u2605} (sA : Search A) (sB : \u2200 {x} \u2192 Search (B x))\n \u2192 ToFun sA\n \u2192 (\u2200 {x} \u2192 ToFun (sB {x}))\n \u2192 ToFun (\u03a3Search sA (\u03bb {x} \u2192 sB {x}))\ntoFun-\u03a3 _ _ toFunA toFunB d = uncurry (toFunB \u2218 toFunA d)\n\nfromFun-\u03a3 : \u2200 {A} {B : A \u2192 \u2605} (sA : Search A) (sB : \u2200 {x} \u2192 Search (B x))\n \u2192 FromFun sA\n \u2192 (\u2200 {x} \u2192 FromFun (sB {x}))\n \u2192 FromFun (\u03a3Search sA (\u03bb {x} \u2192 sB {x}))\nfromFun-\u03a3 _ _ fromFunA fromFunB f = fromFunA (fromFunB \u2218 curry f)\n\nopen import Relation.Binary.PropositionalEquality hiding ([_])\nToFrom : \u2200 {A} (sA : Search A)\n (toFunA : ToFun sA)\n (fromFunA : FromFun sA)\n \u2192 \u2605\nToFrom {A} sA toFunA fromFunA = \u2200 {B} (f : \u03a0 A B) x \u2192 toFunA (fromFunA f) x \u2261 f x\n\nFromTo : \u2200 {A} (sA : Search A)\n (toFunA : ToFun sA)\n (fromFunA : FromFun sA)\n \u2192 \u2605\nFromTo sA toFunA fromFunA = \u2200 {B} (d : Data sA B) \u2192 fromFunA (toFunA d) \u2261 d\n\nmodule \u03a3-props {A} {B : A \u2192 \u2605}\n (\u03bcA : Searchable A) (\u03bcB : \u2200 {x} \u2192 Searchable (B x)) where\n sA = search \u03bcA\n sB : \u2200 {x} \u2192 Search (B x)\n sB {x} = search (\u03bcB {x})\n fromFunA : FromFun sA\n fromFunA = fromFun-searchInd (search-ind \u03bcA)\n fromFunB : \u2200 {x} \u2192 FromFun (sB {x})\n fromFunB {x} = fromFun-searchInd (search-ind (\u03bcB {x}))\n module ToFrom\n (toFunA : ToFun sA)\n (toFunB : \u2200 {x} \u2192 ToFun (sB {x}))\n (toFromA : ToFrom sA toFunA fromFunA)\n (toFromB : \u2200 {x} \u2192 ToFrom (sB {x}) toFunB fromFunB) where\n toFrom-\u03a3 : ToFrom (\u03a3Search sA (\u03bb {x} \u2192 sB {x})) (toFun-\u03a3 sA sB toFunA toFunB) (fromFun-\u03a3 sA sB fromFunA fromFunB)\n toFrom-\u03a3 f (x , y) rewrite toFromA (fromFunB \u2218 curry f) x = toFromB (curry f x) y\n\n {- we need a search-ind-ext...\n module FromTo\n (toFunA : ToFun sA)\n (toFunB : \u2200 {x} \u2192 ToFun (sB {x}))\n (toFromA : FromTo sA toFunA fromFunA)\n (toFromB : \u2200 {x} \u2192 FromTo (sB {x}) toFunB fromFunB) where\n toFrom-\u03a3 : FromTo (\u03a3Search sA (\u03bb {x} \u2192 sB {x})) (toFun-\u03a3 sA sB toFunA toFunB) (fromFun-\u03a3 sA sB fromFunA fromFunB)\n toFrom-\u03a3 t = {!toFromA!} -- {!(\u03bb x \u2192 toFromB (toFunA t x))!}\n -}\n\ntoFun-\u00d7 : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 ToFun sA \u2192 ToFun sB \u2192 ToFun (sA \u00d7Search sB)\ntoFun-\u00d7 sA sB toFunA toFunB = toFun-\u03a3 sA sB toFunA toFunB\n\nfromFun-\u00d7 : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 FromFun sA \u2192 FromFun sB \u2192 FromFun (sA \u00d7Search sB)\nfromFun-\u00d7 sA sB fromFunA fromFunB = fromFun-\u03a3 sA sB fromFunA fromFunB\n\ntoFun-Bit : ToFun (search \u03bcBit)\ntoFun-Bit (f , t) false = f\ntoFun-Bit (f , t) true = t\n\nfromFun-Bit : FromFun (search \u03bcBit)\nfromFun-Bit f = f false , f true\n\ntoFun-\u228e : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 ToFun sA \u2192 ToFun sB \u2192 ToFun (sA +Search sB)\ntoFun-\u228e sA sB toFunA toFunB (x , y) = [ toFunA x , toFunB y ]\n\nfromFun-\u228e : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 FromFun sA \u2192 FromFun sB \u2192 FromFun (sA +Search sB)\nfromFun-\u228e sA sB fromFunA fromFunB f = fromFunA (f \u2218 inj\u2081) , fromFunB (f \u2218 inj\u2082)\n\nPoint : \u2200 {A} \u2192 Search A \u2192 (A \u2192 \u2605) \u2192 \u2605\nPoint sA B = sA _\u228e_ B\n\nToPair : \u2200 {A} (sA : Search A) \u2192 \u2605\nToPair {A} sA = \u2200 {B} \u2192 Point sA B \u2192 \u03a3 A B\n\nFromPair : \u2200 {A} (sA : Search A) \u2192 \u2605\nFromPair {A} sA = \u2200 {B} \u2192 \u03a3 A B \u2192 Point sA B\n\ntoPair-searchInd : \u2200 {A} {sA : Search A} \u2192 SearchInd sA \u2192 ToPair sA\ntoPair-searchInd indA = indA ToPair (\u03bb P0 P1 \u2192 [ P0 , P1 ]) (\u03bb \u03b7 \u2192 _,_ \u03b7)\n\n-- toFun-searchInd : \u2200 {A} {sA : Search A} \u2192 SearchInd sA \u2192 ToFun sA\n-- toFun-searchInd {A} {sA} indA {B} t = ?\n","old_contents":"{-# OPTIONS --type-in-type #-}\nmodule pisearch where\nopen import Type hiding (\u2605_)\nopen import Function.NP\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Bool.NP\nopen import Search.Type\nopen import Search.Searchable.Product\nopen import Search.Searchable\nopen import sum\n\nTree : \u2200 {A} \u2192 Search A \u2192 (A \u2192 \u2605) \u2192 \u2605\nTree sA B = sA _\u00d7_ B\n\nToFun : \u2200 {A} (sA : Search A) \u2192 \u2605\nToFun {A} sA = \u2200 {B} \u2192 Tree sA B \u2192 \u03a0 A B\n\nFromFun : \u2200 {A} (sA : Search A) \u2192 \u2605\nFromFun {A} sA = \u2200 {B} \u2192 \u03a0 A B \u2192 Tree sA B\n\nfromFun-searchInd : \u2200 {A} {sA : Search A} \u2192 SearchInd sA \u2192 FromFun sA\nfromFun-searchInd indA = indA (\u03bb s \u2192 Tree s _) _,_\n\ntoFun-\u03a3 : \u2200 {A} {B : A \u2192 \u2605} (sA : Search A) (sB : \u2200 {x} \u2192 Search (B x))\n \u2192 ToFun sA\n \u2192 (\u2200 {x} \u2192 ToFun (sB {x}))\n \u2192 ToFun (\u03a3Search sA (\u03bb {x} \u2192 sB {x}))\ntoFun-\u03a3 _ _ toFunA toFunB t = uncurry (toFunB \u2218 toFunA t)\n\nfromFun-\u03a3 : \u2200 {A} {B : A \u2192 \u2605} (sA : Search A) (sB : \u2200 {x} \u2192 Search (B x))\n \u2192 FromFun sA\n \u2192 (\u2200 {x} \u2192 FromFun (sB {x}))\n \u2192 FromFun (\u03a3Search sA (\u03bb {x} \u2192 sB {x}))\nfromFun-\u03a3 _ _ fromFunA fromFunB f = fromFunA (fromFunB \u2218 curry f)\n\nopen import Relation.Binary.PropositionalEquality\nToFrom : \u2200 {A} (sA : Search A)\n (toFunA : ToFun sA)\n (fromFunA : FromFun sA)\n \u2192 \u2605\nToFrom {A} sA toFunA fromFunA = \u2200 {B} (f : \u03a0 A B) x \u2192 toFunA (fromFunA f) x \u2261 f x\n\nFromTo : \u2200 {A} (sA : Search A)\n (toFunA : ToFun sA)\n (fromFunA : FromFun sA)\n \u2192 \u2605\nFromTo sA toFunA fromFunA = \u2200 {B} (t : Tree sA B) \u2192 fromFunA (toFunA t) \u2261 t\n\nmodule \u03a3-props {A} {B : A \u2192 \u2605}\n (\u03bcA : Searchable A) (\u03bcB : \u2200 {x} \u2192 Searchable (B x)) where\n sA = search \u03bcA\n sB : \u2200 {x} \u2192 Search (B x)\n sB {x} = search (\u03bcB {x})\n fromFunA : FromFun sA\n fromFunA = fromFun-searchInd (search-ind \u03bcA)\n fromFunB : \u2200 {x} \u2192 FromFun (sB {x})\n fromFunB {x} = fromFun-searchInd (search-ind (\u03bcB {x}))\n module ToFrom\n (toFunA : ToFun sA)\n (toFunB : \u2200 {x} \u2192 ToFun (sB {x}))\n (toFromA : ToFrom sA toFunA fromFunA)\n (toFromB : \u2200 {x} \u2192 ToFrom (sB {x}) toFunB fromFunB) where\n toFrom-\u03a3 : ToFrom (\u03a3Search sA (\u03bb {x} \u2192 sB {x})) (toFun-\u03a3 sA sB toFunA toFunB) (fromFun-\u03a3 sA sB fromFunA fromFunB)\n toFrom-\u03a3 f (x , y) rewrite toFromA (fromFunB \u2218 curry f) x = toFromB (curry f x) y\n\n {- we need a search-ind-ext...\n module FromTo\n (toFunA : ToFun sA)\n (toFunB : \u2200 {x} \u2192 ToFun (sB {x}))\n (toFromA : FromTo sA toFunA fromFunA)\n (toFromB : \u2200 {x} \u2192 FromTo (sB {x}) toFunB fromFunB) where\n toFrom-\u03a3 : FromTo (\u03a3Search sA (\u03bb {x} \u2192 sB {x})) (toFun-\u03a3 sA sB toFunA toFunB) (fromFun-\u03a3 sA sB fromFunA fromFunB)\n toFrom-\u03a3 t = {!toFromA!} -- {!(\u03bb x \u2192 toFromB (toFunA t x))!}\n -}\n\ntoFun-\u00d7 : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 ToFun sA \u2192 ToFun sB \u2192 ToFun (sA \u00d7Search sB)\ntoFun-\u00d7 sA sB toFunA toFunB = toFun-\u03a3 sA sB toFunA toFunB\n\nfromFun-\u00d7 : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 FromFun sA \u2192 FromFun sB \u2192 FromFun (sA \u00d7Search sB)\nfromFun-\u00d7 sA sB fromFunA fromFunB = fromFun-\u03a3 sA sB fromFunA fromFunB\n\ntoFun-Bit : ToFun (search \u03bcBit)\ntoFun-Bit (f , t) false = f\ntoFun-Bit (f , t) true = t\n\nfromFun-Bit : FromFun (search \u03bcBit)\nfromFun-Bit f = f false , f true\n\ntoFun-\u228e : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 ToFun sA \u2192 ToFun sB \u2192 ToFun (sA +Search sB)\ntoFun-\u228e sA sB toFunA toFunB (t , u) (inj\u2081 x) = toFunA t x\ntoFun-\u228e sA sB toFunA toFunB (t , u) (inj\u2082 x) = toFunB u x\n\nfromFun-\u228e : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 FromFun sA \u2192 FromFun sB \u2192 FromFun (sA +Search sB)\nfromFun-\u228e sA sB fromFunA fromFunB f = fromFunA (f \u2218 inj\u2081) , fromFunB (f \u2218 inj\u2082)\n\n-- toFun-searchInd : \u2200 {A} {sA : Search A} \u2192 SearchInd sA \u2192 ToFun sA\n-- toFun-searchInd {A} {sA} indA {B} t = ?\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"5cc39d986b6ffa05d7fd642b7b5dae4e8d18c155","subject":"this proves expandability, except for three premises about the disjointness of Deltas which might not be relevant","message":"this proves expandability, except for three premises about the disjointness of Deltas which might not be relevant\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"expandability.agda","new_file":"expandability.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import htype-decidable\n\nmodule expandability where\n mutual\n expandability-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ]\n (\u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394)\n expandability-synth SConst = _ , _ , ESConst\n expandability-synth (SAsc {\u03c4 = \u03c4} wt)\n with expandability-ana wt\n ... | _ , _ , \u03c4' , D with htype-dec \u03c4 \u03c4'\n ... | Inl _ = _ , _ , ESAsc2 D\n ... | Inr x = _ , _ , ESAsc1 D x\n expandability-synth (SVar {n = n} x) = _ , _ , ESVar x\n expandability-synth (SAp wt1 MAHole wt2)\n with expandability-ana wt2\n ... | d2 , \u03942 , \u03c42 , D2 with expandability-ana (ASubsume wt1 TCHole1)\n ... | d1 , \u03941 , \u03c41 , D1 = _ , _ , ESAp1 {!!} wt1 D1 D2\n expandability-synth (SAp wt1 (MAArr {\u03c41 = \u03c42}) wt2)\n with expandability-synth wt1 | expandability-ana wt2\n ... | d1 , \u03941 , D1\n | d2 , \u03942 , \u03c42' , D2\n with htype-dec \u03c42 \u03c42'\n expandability-synth (SAp wt1 MAArr wt2) | d1 , \u03941 , D1 | d2 , \u03942 , \u03c42' , D2 | Inr neq = _ , _ , ESAp2 {!!} D1 D2 neq\n expandability-synth (SAp wt1 MAArr wt2) | d1 , \u03941 , D1 | d2 , \u03942 , \u03c41 , D2 | Inl refl = _ , _ , ESAp3 {!!} D1 D2\n expandability-synth SEHole = _ , _ , ESEHole\n expandability-synth (SNEHole wt)\n with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESNEHole wt'\n expandability-synth (SLam x\u2081 wt)\n with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESLam x\u2081 wt'\n\n expandability-ana : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ] \u03a3[ \u03c4' \u2208 htyp ]\n (\u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394)\n expandability-ana {e = e} (ASubsume D x\u2081)\n with expandability-synth D\n -- these cases just pass through, but we need to pattern match so we can prove things aren't holes\n expandability-ana {e = c} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = e \u00b7: x} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = X x} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = \u00b7\u03bb x e} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = \u00b7\u03bb x [ x\u2081 ] e} (ASubsume D x\u2082) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2082\n expandability-ana {e = e1 \u2218 e2} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n -- the two holes are special-cased\n expandability-ana {e = \u2987\u2988[ x ]} (ASubsume _ _ ) | _ , _ , _ = _ , _ , _ , EAEHole\n expandability-ana {e = \u2987 e \u2988[ x ]} (ASubsume (SNEHole wt) _) | _ , _ , _ = _ , _ , _ , EANEHole (\u03c02( \u03c02 (expandability-synth wt)))\n -- the lambda cases\n expandability-ana (ALam x\u2081 MAHole wt)\n with expandability-ana wt\n ... | _ , _ , _ , D' = _ , _ , _ , EALamHole x\u2081 D'\n expandability-ana (ALam x\u2081 MAArr wt)\n with expandability-ana wt\n ... | _ , _\u202f, _ , D' = _ , _ , _ , EALam x\u2081 D'\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import htype-decidable\n\nmodule expandability where\n -- lemma : \u2200 { \u0393 e \u03c4} \u2192\n -- \u0393 \u22a2 e => \u2987\u2988 \u2192\n -- \u0393 \u22a2 e <= \u03c4 ==> \u2987\u2988\n -- lemma wt = ASubsume wt TCHole1\n\n mutual\n expandability-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ]\n (\u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394)\n expandability-synth SConst = _ , _ , ESConst\n expandability-synth (SAsc {\u03c4 = \u03c4} wt)\n with expandability-ana wt\n ... | _ , _ , \u03c4' , D with htype-dec \u03c4 \u03c4'\n ... | Inl _ = _ , _ , ESAsc2 D\n ... | Inr x = _ , _ , ESAsc1 D x\n expandability-synth (SVar {n = n} x) = _ , _ , ESVar x\n expandability-synth (SAp wt1 MAHole wt2)\n with expandability-ana wt2\n ... | d2 , \u03942 , \u03c42 , D2 with expandability-ana (ASubsume wt1 TCHole1)\n ... | d1 , \u03941 , \u03c41 , D1 = _ , _ , ESAp1 {!!} wt1 D1 D2\n expandability-synth (SAp wt1 (MAArr {\u03c42 = \u03c42}) wt2)\n with expandability-synth wt1 | expandability-ana wt2\n ... | d1 , \u03941 , D1\n | d2 , \u03942 , \u03c42' , D2\n with htype-dec \u03c42 \u03c42'\n expandability-synth (SAp wt1 MAArr wt2) | d1 , \u03941 , D1 | d2 , \u03942 , \u03c42' , D2 | Inr neq = _ , _ , ESAp2 {!!} {!D1!} {!!} neq\n expandability-synth (SAp wt1 MAArr wt2) | d1 , \u03941 , D1 | d2 , \u03942 , \u03c42 , D2 | Inl refl = _ , _ , ESAp3 {!!} {!!} {!!}\n expandability-synth SEHole = _ , _ , ESEHole\n expandability-synth (SNEHole wt)\n with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESNEHole wt'\n expandability-synth (SLam x\u2081 wt)\n with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESLam x\u2081 wt'\n\n expandability-ana : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ] \u03a3[ \u03c4' \u2208 htyp ]\n (\u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394)\n expandability-ana {e = e} (ASubsume D x\u2081)\n with expandability-synth D\n -- these cases just pass through, but we need to pattern match so we can prove things aren't holes\n expandability-ana {e = c} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = e \u00b7: x} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = X x} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = \u00b7\u03bb x e} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = \u00b7\u03bb x [ x\u2081 ] e} (ASubsume D x\u2082) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2082\n expandability-ana {e = e1 \u2218 e2} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n -- the two holes are special-cased\n expandability-ana {e = \u2987\u2988[ x ]} (ASubsume _ _ ) | _ , _ , _ = _ , _ , _ , EAEHole\n expandability-ana {e = \u2987 e \u2988[ x ]} (ASubsume (SNEHole wt) _) | _ , _ , _ = _ , _ , _ , EANEHole (\u03c02( \u03c02 (expandability-synth wt)))\n -- the lambda cases\n expandability-ana (ALam x\u2081 MAHole wt)\n with expandability-ana wt\n ... | _ , _ , _ , D' = _ , _ , _ , EALamHole x\u2081 D'\n expandability-ana (ALam x\u2081 MAArr wt)\n with expandability-ana wt\n ... | _ , _\u202f, _ , D' = _ , _ , _ , EALam x\u2081 D'\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"47c8540189b6bea5f1a13bfc28f05bc9a52bf47c","subject":"[ FOL ] Added documentation.","message":"[ FOL ] Added documentation.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/Common\/FOL\/FOL.agda","new_file":"src\/fot\/Common\/FOL\/FOL.agda","new_contents":"------------------------------------------------------------------------------\n-- First-order logic (without equality)\n------------------------------------------------------------------------------\n\n{-# OPTIONS --exact-split #-}\n{-# OPTIONS --no-sized-types #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module is re-exported by the \"base\" modules whose theories are\n-- defined on first-order logic (without equality).\n\n-- The logical connectives are hard-coded in our translation, i.e. the\n-- symbols \u22a5, \u22a4, \u00ac, \u2227, \u2228, \u2192 and \u2194 must be used.\n--\n-- N.B. For the implication we use the Agda function type.\n--\n-- N.B For the universal quantifier we use the Agda (dependent)\n-- function type.\n\nmodule Common.FOL.FOL where\n\ninfixr 4 _,_\ninfix 3 \u00ac_\ninfixr 2 _\u2227_\ninfix 2 \u2203\ninfixr 1 _\u2228_\ninfixr 0 _\u2194_\n\n----------------------------------------------------------------------------\n-- The universe of discourse\/universal domain.\npostulate D : Set\n\n------------------------------------------------------------------------------\n-- The conjunction data type.\n\n-- It is not necessary to add the data constructor _,_ as an\n-- axiom because the ATPs implement it.\ndata _\u2227_ (A B : Set) : Set where\n _,_ : A \u2192 B \u2192 A \u2227 B\n\n-- It is not strictly necessary define the projections \u2227-proj\u2081 and\n-- \u2227-proj\u2082 because the ATPs implement them. For the same reason, it is\n-- not necessary to add them as (general\/local) hints.\n\u2227-proj\u2081 : \u2200 {A B} \u2192 A \u2227 B \u2192 A\n\u2227-proj\u2081 (a , _) = a\n\n\u2227-proj\u2082 : \u2200 {A B} \u2192 A \u2227 B \u2192 B\n\u2227-proj\u2082 (_ , b) = b\n\n-----------------------------------------------------------------------------\n-- The disjunction data type.\n\n-- It is not necessary to add the data constructors inj\u2081 and inj\u2082 as\n-- axioms because the ATPs implement them.\ndata _\u2228_ (A B : Set) : Set where\n inj\u2081 : A \u2192 A \u2228 B\n inj\u2082 : B \u2192 A \u2228 B\n\n-- It is not strictly necessary define the eliminator `case` because\n-- the ATPs implement it. For the same reason, it is not necessary to\n-- add it as a (general\/local) hint.\ncase : \u2200 {A B} \u2192 {C : Set} \u2192 (A \u2192 C) \u2192 (B \u2192 C) \u2192 A \u2228 B \u2192 C\ncase f g (inj\u2081 a) = f a\ncase f g (inj\u2082 b) = g b\n\n------------------------------------------------------------------------------\n-- The empty type.\ndata \u22a5 : Set where\n\n\u22a5-elim : {A : Set} \u2192 \u22a5 \u2192 A\n\u22a5-elim ()\n\n------------------------------------------------------------------------------\n-- The unit type.\n-- N.B. The name of this type is \"\\top\", not T.\ndata \u22a4 : Set where\n tt : \u22a4\n\n------------------------------------------------------------------------------\n-- Negation.\n\n-- The underscore allows to write for example '\u00ac \u00ac A' instead of '\u00ac (\u00ac A)'.\n\n-- We do not add a definition because: i) the definition of negation\n-- is not a FOL-definition and ii) the translation of the neagation is\n-- hard-coded in Apia.\n\n\u00ac_ : Set \u2192 Set\n\u00ac A = A \u2192 \u22a5\n\n------------------------------------------------------------------------------\n-- Biconditional.\n\n-- We do not add a definition because: i) the definition of the\n-- biconditional is not a FOL-definition, ii) the translation of the\n-- biconditional is hard-coded in Apia.\n\n_\u2194_ : Set \u2192 Set \u2192 Set\nA \u2194 B = (A \u2192 B) \u2227 (B \u2192 A)\n\n------------------------------------------------------------------------------\n-- The existential quantifier type on D.\ndata \u2203 (A : D \u2192 Set) : Set where\n _,_ : (t : D) \u2192 A t \u2192 \u2203 A\n\n-- Sugar syntax for the existential quantifier.\nsyntax \u2203 (\u03bb x \u2192 e) = \u2203[ x ] e\n\n-- 2012-03-05: We avoid to use the existential elimination or the\n-- existential projections because we use pattern matching (and the\n-- Agda's with constructor).\n\n-- The existential elimination.\n--\n-- NB. We do not use the usual type theory elimination with two\n-- projections because we are working in first-order logic where we do\n-- not need extract a witness from an existence proof.\n-- \u2203-elim : {A : D \u2192 Set}{B : Set} \u2192 \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B) \u2192 B\n-- \u2203-elim (_ , Ax) h = h Ax\n\n-- The existential proyections.\n-- \u2203-proj\u2081 : \u2200 {A} \u2192 \u2203 A \u2192 D\n-- \u2203-proj\u2081 (x , _) = x\n\n-- \u2203-proj\u2082 : \u2200 {A} \u2192 (h : \u2203 A) \u2192 A (\u2203-proj\u2081 h)\n-- \u2203-proj\u2082 (_ , Ax) = Ax\n\n------------------------------------------------------------------------------\n-- Properties\n\n\u2192-trans : {A B C : Set} \u2192 (A \u2192 B) \u2192 (B \u2192 C) \u2192 A \u2192 C\n\u2192-trans f g a = g (f a)\n","old_contents":"------------------------------------------------------------------------------\n-- First-order logic (without equality)\n------------------------------------------------------------------------------\n\n{-# OPTIONS --exact-split #-}\n{-# OPTIONS --no-sized-types #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module is re-exported by the \"base\" modules whose theories are\n-- defined on first-order logic (without equality).\n\n-- The logical connectives are hard-coded in our translation, i.e. the\n-- symbols \u22a5, \u22a4, \u00ac, \u2227, \u2228, \u2192 and \u2194 must be used.\n--\n-- N.B. For the implication we use the Agda function type.\n--\n-- N.B For the universal quantifier we use the Agda (dependent)\n-- function type.\n\nmodule Common.FOL.FOL where\n\ninfixr 4 _,_\ninfix 3 \u00ac_\ninfixr 2 _\u2227_\ninfix 2 \u2203\ninfixr 1 _\u2228_\ninfixr 0 _\u2194_\n\n----------------------------------------------------------------------------\n-- The universe of discourse\/universal domain.\npostulate D : Set\n\n------------------------------------------------------------------------------\n-- The conjunction data type.\n\n-- It is not necessary to add the data constructor _,_ as an\n-- axiom because the ATPs implement it.\ndata _\u2227_ (A B : Set) : Set where\n _,_ : A \u2192 B \u2192 A \u2227 B\n\n-- It is not strictly necessary define the projections \u2227-proj\u2081 and\n-- \u2227-proj\u2082 because the ATPs implement them. For the same reason, it is\n-- not necessary to add them as (general\/local) hints.\n\u2227-proj\u2081 : \u2200 {A B} \u2192 A \u2227 B \u2192 A\n\u2227-proj\u2081 (a , _) = a\n\n\u2227-proj\u2082 : \u2200 {A B} \u2192 A \u2227 B \u2192 B\n\u2227-proj\u2082 (_ , b) = b\n\n-----------------------------------------------------------------------------\n-- The disjunction data type.\n\n-- It is not necessary to add the data constructors inj\u2081 and inj\u2082 as\n-- axioms because the ATPs implement them.\ndata _\u2228_ (A B : Set) : Set where\n inj\u2081 : A \u2192 A \u2228 B\n inj\u2082 : B \u2192 A \u2228 B\n\n-- It is not strictly necessary define the eliminator `case` because\n-- the ATPs implement it. For the same reason, it is not necessary to\n-- add it as a (general\/local) hint.\ncase : \u2200 {A B} \u2192 {C : Set} \u2192 (A \u2192 C) \u2192 (B \u2192 C) \u2192 A \u2228 B \u2192 C\ncase f g (inj\u2081 a) = f a\ncase f g (inj\u2082 b) = g b\n\n------------------------------------------------------------------------------\n-- The empty type.\ndata \u22a5 : Set where\n\n\u22a5-elim : {A : Set} \u2192 \u22a5 \u2192 A\n\u22a5-elim ()\n\n------------------------------------------------------------------------------\n-- The unit type.\n-- N.B. The name of this type is \"\\top\", not T.\ndata \u22a4 : Set where\n tt : \u22a4\n\n------------------------------------------------------------------------------\n-- Negation.\n-- The underscore allows to write for example '\u00ac \u00ac A' instead of '\u00ac (\u00ac A)'.\n\u00ac_ : Set \u2192 Set\n\u00ac A = A \u2192 \u22a5\n\n------------------------------------------------------------------------------\n-- Biconditional.\n_\u2194_ : Set \u2192 Set \u2192 Set\nA \u2194 B = (A \u2192 B) \u2227 (B \u2192 A)\n\n------------------------------------------------------------------------------\n-- The existential quantifier type on D.\ndata \u2203 (A : D \u2192 Set) : Set where\n _,_ : (t : D) \u2192 A t \u2192 \u2203 A\n\n-- Sugar syntax for the existential quantifier.\nsyntax \u2203 (\u03bb x \u2192 e) = \u2203[ x ] e\n\n-- 2012-03-05: We avoid to use the existential elimination or the\n-- existential projections because we use pattern matching (and the\n-- Agda's with constructor).\n\n-- The existential elimination.\n--\n-- NB. We do not use the usual type theory elimination with two\n-- projections because we are working in first-order logic where we do\n-- not need extract a witness from an existence proof.\n-- \u2203-elim : {A : D \u2192 Set}{B : Set} \u2192 \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B) \u2192 B\n-- \u2203-elim (_ , Ax) h = h Ax\n\n-- The existential proyections.\n-- \u2203-proj\u2081 : \u2200 {A} \u2192 \u2203 A \u2192 D\n-- \u2203-proj\u2081 (x , _) = x\n\n-- \u2203-proj\u2082 : \u2200 {A} \u2192 (h : \u2203 A) \u2192 A (\u2203-proj\u2081 h)\n-- \u2203-proj\u2082 (_ , Ax) = Ax\n\n------------------------------------------------------------------------------\n-- Properties\n\n\u2192-trans : {A B C : Set} \u2192 (A \u2192 B) \u2192 (B \u2192 C) \u2192 A \u2192 C\n\u2192-trans f g a = g (f a)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"bf33562cdea58a03e94ec0693c42335249daaf5a","subject":"Lift change algebras to Data.All.","message":"Lift change algebras to Data.All.\n\nOld-commit-hash: 2d6c57c5ca13b3f3e97bf60d2d09f4b1f0aaf56b\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Algebra.agda","new_file":"Base\/Change\/Algebra.agda","new_contents":"module Base.Change.Algebra where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Change Algebras\n\nrecord IsChangeAlgebra\n {c} {d}\n {Carrier : Set c}\n (Change : Carrier \u2192 Set d)\n (update : (v : Carrier) (dv : Change v) \u2192 Carrier)\n (diff : (u v : Carrier) \u2192 Change v) : Set (c \u2294 d) where\n field\n update-diff : \u2200 u v \u2192 update v (diff u v) \u2261 u\n\n nil : \u2200 v \u2192 Change v\n nil v = diff v v\n\n update-nil : \u2200 v \u2192 update v (nil v) \u2261 v\n update-nil v = update-diff v v\n\nrecord ChangeAlgebra {c} \u2113\n (Carrier : Set c) : Set (c \u2294 suc \u2113) where\n field\n Change : Carrier \u2192 Set \u2113\n update : (v : Carrier) (dv : Change v) \u2192 Carrier\n diff : (u v : Carrier) \u2192 Change v\n\n isChangeAlgebra : IsChangeAlgebra Change update diff\n\n\n infixl 6 update diff\n\n open IsChangeAlgebra isChangeAlgebra public\n\nopen ChangeAlgebra {{...}} public\n using\n ( update-diff\n ; update-nil\n ; nil\n )\n renaming\n ( Change to \u0394\n ; update to _\u229e_\n ; diff to _\u229f_\n )\n\n-- Families of change algebras\n\nrecord ChangeAlgebraFamily {a p} \u2113 {A : Set a} (P : A \u2192 Set p): Set (suc \u2113 \u2294 p \u2294 a) where\n constructor\n family\n field\n change-algebra : \u2200 x \u2192 ChangeAlgebra \u2113 (P x)\n\n module _ x where\n open ChangeAlgebra (change-algebra x) public\n\nmodule Family = ChangeAlgebraFamily {{...}}\n\nopen Family public\n using\n (\n )\n renaming\n ( Change to \u0394\u208d_\u208e\n ; nil to nil\u208d_\u208e\n ; update-diff to update-diff\u208d_\u208e\n ; update-nil to update-nil\u208d_\u208e\n ; change-algebra to change-algebra\u208d_\u208e\n )\n\ninfixl 6 update\u2032 diff\u2032\n\nupdate\u2032 = Family.update\nsyntax update\u2032 x v dv = v \u229e\u208d x \u208e dv\n\ndiff\u2032 = Family.diff\nsyntax diff\u2032 x u v = u \u229f\u208d x \u208e v\n\n-- Derivatives\n\nDerivative : \u2200 {a b c d} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra c A}} \u2192\n {{CB : ChangeAlgebra d B}} \u2192\n (f : A \u2192 B) \u2192\n (df : (a : A) (da : \u0394 a) \u2192 \u0394 (f a)) \u2192\n Set (a \u2294 b \u2294 c)\nDerivative f df = \u2200 a da \u2192 f a \u229e df a da \u2261 f (a \u229e da)\n\n-- Abelian groups induce change algebras\n\nopen import Algebra.Structures\nopen import Data.Product\nopen import Function\n\nmodule GroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isAbelianGroup : IsAbelianGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsAbelianGroup isAbelianGroup\n hiding\n ( refl\n )\n renaming\n ( _-_ to _\u229d_\n ; \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n changeAlgebra : ChangeAlgebra a A\n changeAlgebra = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8 comm _ _ \u27e9\n (u \u229d v ) \u2295 v\n \u2261\u27e8\u27e9\n (u \u2295 (v \u207b\u00b9)) \u2295 v\n \u2261\u27e8 assoc _ _ _ \u27e9\n u \u2295 ((v \u207b\u00b9) \u2295 v)\n \u2261\u27e8 refl \u27e8\u2295\u27e9 proj\u2081 inverse v \u27e9\n u \u2295 \u03b5\n \u2261\u27e8 proj\u2082 identity u \u27e9\n u\n \u220e\n }\n }\n\n-- Function changes\n\nmodule FunctionChanges\n {a} {b} {c} {d} (A : Set a) (B : Set b) {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}}\n where\n record FunctionChange (f : A \u2192 B) : Set (a \u2294 b \u2294 c \u2294 d) where\n constructor\n cons\n field\n apply : (a : A) (da : \u0394 a) \u2192\n \u0394 (f a)\n correct : (a : A) (da : \u0394 a) \u2192\n f (a \u229e da) \u229e apply (a \u229e da) (nil (a \u229e da)) \u2261 f a \u229e apply a da\n\n open FunctionChange\n open \u2261-Reasoning\n open import Postulate.Extensionality\n\n changeAlgebra : ChangeAlgebra (a \u2294 b \u2294 c \u2294 d) (A \u2192 B)\n changeAlgebra = record\n { Change = FunctionChange\n ; update = \u03bb f df a \u2192 f a \u229e apply df a (nil a)\n ; diff = \u03bb g f \u2192 record\n { apply = \u03bb a da \u2192 g (a \u229e da) \u229f f a\n ; correct = \u03bb a da \u2192\n begin\n f (a \u229e da) \u229e (g ((a \u229e da) \u229e nil (a \u229e da)) \u229f f (a \u229e da))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f (a \u229e da) \u229e (g \u25a1 \u229f f (a \u229e da)))\n (update-nil (a \u229e da)) \u27e9\n f (a \u229e da) \u229e (g (a \u229e da) \u229f f (a \u229e da))\n \u2261\u27e8 update-diff (g (a \u229e da)) (f (a \u229e da)) \u27e9\n g (a \u229e da)\n \u2261\u27e8 sym (update-diff (g (a \u229e da)) (f a)) \u27e9\n f a \u229e (g (a \u229e da) \u229f f a)\n \u220e\n }\n ; isChangeAlgebra = record\n { update-diff = \u03bb g f \u2192 ext (\u03bb a \u2192\n begin\n f a \u229e (g (a \u229e nil a) \u229f f a)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f a \u229e (g \u25a1 \u229f f a)) (update-nil a) \u27e9\n f a \u229e (g a \u229f f a)\n \u2261\u27e8 update-diff (g a) (f a) \u27e9\n g a\n \u220e)\n }\n }\n\n incrementalization : \u2200 (f : A \u2192 B) df a da \u2192\n (f \u229e df) (a \u229e da) \u2261 f a \u229e apply df a da\n incrementalization f df a da = correct df a da\n\n nil-is-derivative : \u2200 (f : A \u2192 B) \u2192\n Derivative f (apply (nil f))\n nil-is-derivative f a da =\n begin\n f a \u229e apply (nil f) a da\n \u2261\u27e8 sym (incrementalization f (nil f) a da) \u27e9\n (f \u229e nil f) (a \u229e da)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (a \u229e da))\n (update-nil f) \u27e9\n f (a \u229e da)\n \u220e\n\n-- List changes\n\nopen import Data.List\nopen import Data.List.All\n\ndata All\u2032 {a p q} {A : Set a}\n {P : A \u2192 Set p}\n (Q : {x : A} \u2192 P x \u2192 Set q)\n : {xs : List A} (pxs : All P xs) \u2192 Set (p \u2294 q \u2294 a) where\n [] : All\u2032 Q []\n _\u2237_ : \u2200 {x xs} {px : P x} {pxs : All P xs} (qx : Q px) (qxs : All\u2032 Q pxs) \u2192 All\u2032 Q (px \u2237 pxs)\n\nmodule ListChanges\n {a} {c} {A : Set a} (P : A \u2192 Set) {{C : ChangeAlgebraFamily c P}}\n where\n update-all : \u2200 {xs} \u2192 (pxs : All P xs) \u2192 All\u2032 (\u0394\u208d _ \u208e) pxs \u2192 All P xs\n update-all {[]} [] [] = []\n update-all {x \u2237 xs} (px \u2237 pxs) (dpx \u2237 dpxs) = (px \u229e\u208d x \u208e dpx) \u2237 update-all pxs dpxs\n\n diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 All\u2032 (\u0394\u208d _ \u208e) pxs\n diff-all [] [] = []\n diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = (px\u2032 \u229f\u208d _ \u208e px) \u2237 diff-all pxs\u2032 pxs\n\n update-diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 update-all pxs (diff-all pxs\u2032 pxs) \u2261 pxs\u2032\n update-diff-all [] [] = refl\n update-diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = cong\u2082 _\u2237_ (update-diff\u208d _ \u208e px\u2032 px) (update-diff-all pxs\u2032 pxs)\n\n changeAlgebra : ChangeAlgebraFamily (c \u2294 a) (All P)\n changeAlgebra = record\n { change-algebra = \u03bb xs \u2192 record\n { Change = All\u2032 \u0394\u208d _ \u208e\n ; update = update-all\n ; diff = diff-all\n ; isChangeAlgebra = record\n { update-diff = update-diff-all\n }\n }\n }\n","old_contents":"module Base.Change.Algebra where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Change Algebras\n\nrecord IsChangeAlgebra\n {c} {d}\n {Carrier : Set c}\n (Change : Carrier \u2192 Set d)\n (update : (v : Carrier) (dv : Change v) \u2192 Carrier)\n (diff : (u v : Carrier) \u2192 Change v) : Set (c \u2294 d) where\n field\n update-diff : \u2200 u v \u2192 update v (diff u v) \u2261 u\n\n nil : \u2200 v \u2192 Change v\n nil v = diff v v\n\n update-nil : \u2200 v \u2192 update v (nil v) \u2261 v\n update-nil v = update-diff v v\n\nrecord ChangeAlgebra {c} \u2113\n (Carrier : Set c) : Set (c \u2294 suc \u2113) where\n field\n Change : Carrier \u2192 Set \u2113\n update : (v : Carrier) (dv : Change v) \u2192 Carrier\n diff : (u v : Carrier) \u2192 Change v\n\n isChangeAlgebra : IsChangeAlgebra Change update diff\n\n\n infixl 6 update diff\n\n open IsChangeAlgebra isChangeAlgebra public\n\nopen ChangeAlgebra {{...}} public\n using\n ( update-diff\n ; update-nil\n ; nil\n )\n renaming\n ( Change to \u0394\n ; update to _\u229e_\n ; diff to _\u229f_\n )\n\n-- Families of change algebras\n\nrecord ChangeAlgebraFamily {a p} \u2113 {A : Set a} (P : A \u2192 Set p): Set (suc \u2113 \u2294 p \u2294 a) where\n constructor\n family\n field\n change-algebra : \u2200 x \u2192 ChangeAlgebra \u2113 (P x)\n\n module _ x where\n open ChangeAlgebra (change-algebra x) public\n\nmodule Family = ChangeAlgebraFamily {{...}}\n\nopen Family public\n using\n (\n )\n renaming\n ( Change to \u0394\u208d_\u208e\n ; nil to nil\u208d_\u208e\n ; update-diff to update-diff\u208d_\u208e\n ; update-nil to update-nil\u208d_\u208e\n ; change-algebra to change-algebra\u208d_\u208e\n )\n\ninfixl 6 update\u2032 diff\u2032\n\nupdate\u2032 = Family.update\nsyntax update\u2032 x v dv = v \u229e\u208d x \u208e dv\n\ndiff\u2032 = Family.diff\nsyntax diff\u2032 x u v = u \u229f\u208d x \u208e v\n\n-- Derivatives\n\nDerivative : \u2200 {a b c d} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra c A}} \u2192\n {{CB : ChangeAlgebra d B}} \u2192\n (f : A \u2192 B) \u2192\n (df : (a : A) (da : \u0394 a) \u2192 \u0394 (f a)) \u2192\n Set (a \u2294 b \u2294 c)\nDerivative f df = \u2200 a da \u2192 f a \u229e df a da \u2261 f (a \u229e da)\n\n-- Abelian groups induce change algebras\n\nopen import Algebra.Structures\nopen import Data.Product\nopen import Function\n\nmodule GroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isAbelianGroup : IsAbelianGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsAbelianGroup isAbelianGroup\n hiding\n ( refl\n )\n renaming\n ( _-_ to _\u229d_\n ; \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n changeAlgebra : ChangeAlgebra a A\n changeAlgebra = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8 comm _ _ \u27e9\n (u \u229d v ) \u2295 v\n \u2261\u27e8\u27e9\n (u \u2295 (v \u207b\u00b9)) \u2295 v\n \u2261\u27e8 assoc _ _ _ \u27e9\n u \u2295 ((v \u207b\u00b9) \u2295 v)\n \u2261\u27e8 refl \u27e8\u2295\u27e9 proj\u2081 inverse v \u27e9\n u \u2295 \u03b5\n \u2261\u27e8 proj\u2082 identity u \u27e9\n u\n \u220e\n }\n }\n\n-- Function changes\n\nmodule FunctionChanges\n {a} {b} {c} {d} (A : Set a) (B : Set b) {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}}\n where\n record FunctionChange (f : A \u2192 B) : Set (a \u2294 b \u2294 c \u2294 d) where\n constructor\n cons\n field\n apply : (a : A) (da : \u0394 a) \u2192\n \u0394 (f a)\n correct : (a : A) (da : \u0394 a) \u2192\n f (a \u229e da) \u229e apply (a \u229e da) (nil (a \u229e da)) \u2261 f a \u229e apply a da\n\n open FunctionChange\n open \u2261-Reasoning\n open import Postulate.Extensionality\n\n changeAlgebra : ChangeAlgebra (a \u2294 b \u2294 c \u2294 d) (A \u2192 B)\n changeAlgebra = record\n { Change = FunctionChange\n ; update = \u03bb f df a \u2192 f a \u229e apply df a (nil a)\n ; diff = \u03bb g f \u2192 record\n { apply = \u03bb a da \u2192 g (a \u229e da) \u229f f a\n ; correct = \u03bb a da \u2192\n begin\n f (a \u229e da) \u229e (g ((a \u229e da) \u229e nil (a \u229e da)) \u229f f (a \u229e da))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f (a \u229e da) \u229e (g \u25a1 \u229f f (a \u229e da)))\n (update-nil (a \u229e da)) \u27e9\n f (a \u229e da) \u229e (g (a \u229e da) \u229f f (a \u229e da))\n \u2261\u27e8 update-diff (g (a \u229e da)) (f (a \u229e da)) \u27e9\n g (a \u229e da)\n \u2261\u27e8 sym (update-diff (g (a \u229e da)) (f a)) \u27e9\n f a \u229e (g (a \u229e da) \u229f f a)\n \u220e\n }\n ; isChangeAlgebra = record\n { update-diff = \u03bb g f \u2192 ext (\u03bb a \u2192\n begin\n f a \u229e (g (a \u229e nil a) \u229f f a)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f a \u229e (g \u25a1 \u229f f a)) (update-nil a) \u27e9\n f a \u229e (g a \u229f f a)\n \u2261\u27e8 update-diff (g a) (f a) \u27e9\n g a\n \u220e)\n }\n }\n\n incrementalization : \u2200 (f : A \u2192 B) df a da \u2192\n (f \u229e df) (a \u229e da) \u2261 f a \u229e apply df a da\n incrementalization f df a da = correct df a da\n\n nil-is-derivative : \u2200 (f : A \u2192 B) \u2192\n Derivative f (apply (nil f))\n nil-is-derivative f a da =\n begin\n f a \u229e apply (nil f) a da\n \u2261\u27e8 sym (incrementalization f (nil f) a da) \u27e9\n (f \u229e nil f) (a \u229e da)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (a \u229e da))\n (update-nil f) \u27e9\n f (a \u229e da)\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b87eb75ad51669de831d389ef1a78b415b31a18d","subject":"Dist: add the `symIter` from Conor and some lemmas","message":"Dist: add the `symIter` from Conor and some lemmas\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Nat\/Distance.agda","new_file":"lib\/Data\/Nat\/Distance.agda","new_contents":"open import Function.NP\nopen import Data.Nat.NP\nopen import Data.Nat.Properties as Props\nopen import Data.Nat.Properties.Simple\nopen import Data.Product.NP\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Relation.Binary\nopen import Relation.Nullary\n\nmodule Data.Nat.Distance where\n\n{-\ndist : \u2115 \u2192 \u2115 \u2192 \u2115\ndist zero y = y\ndist x zero = x\ndist (suc x) (suc y) = dist x y\n-}\n\n-- `symIter` from Conor McBride\n-- Since symIter is essentially `dist` with callbacks, I\n-- include it here.\nmodule Generic {a}{A : Set a}(z : \u2115 \u2192 A)(s : A \u2192 A) where\n dist : \u2115 \u2192 \u2115 \u2192 A\n dist zero y = z y\n dist x zero = z x\n dist (suc x) (suc y) = s (dist x y)\n\n dist-comm : \u2200 x y \u2192 dist x y \u2261 dist y x\n dist-comm zero zero = idp\n dist-comm zero (suc y) = idp\n dist-comm (suc x) zero = idp\n dist-comm (suc x) (suc y) = ap s (dist-comm x y)\n\n dist-0\u2261id : \u2200 x \u2192 dist 0 x \u2261 z x\n dist-0\u2261id _ = idp\n\n dist-0\u2261id\u2032 : \u2200 x \u2192 dist x 0 \u2261 z x\n dist-0\u2261id\u2032 zero = idp\n dist-0\u2261id\u2032 (suc _) = idp\n\nopen Generic id id public\n\nmodule Add where\n open Generic id (suc \u2218 suc)\n public\n renaming ( dist to _+'_\n ; dist-0\u2261id to +'0-identity\n ; dist-0\u2261id\u2032 to 0+'-identity\n ; dist-comm to +'-comm\n )\n\n +-spec : \u2200 x y \u2192 x +' y \u2261 x + y\n +-spec zero _ = idp\n +-spec (suc x) zero = ap suc (\u2115\u00b0.+-comm 0 x)\n +-spec (suc x) (suc y) = ap suc (ap suc (+-spec x y) \u2219 ! +-suc x y)\n\nmodule AddMult where\n open Generic (\u03bb x \u2192 x , 0)\n (\u03bb { (s , p) \u2192 (2 + s , 1 + s + p) })\n public\n renaming ( dist to _+*_\n ; dist-0\u2261id to +*0-identity\n ; dist-0\u2261id\u2032 to 0+*-identity\n ; dist-comm to +*-comm\n )\n\n _+'_ : \u2115 \u2192 \u2115 \u2192 \u2115\n x +' y = fst (x +* y)\n\n _*'_ : \u2115 \u2192 \u2115 \u2192 \u2115\n x *' y = snd (x +* y)\n\n +*-spec : \u2200 x y \u2192 (x +' y \u2261 x + y) \u00d7 (x *' y \u2261 x * y)\n +*-spec zero _ = idp , idp\n +*-spec (suc x) zero = ap suc (\u2115\u00b0.+-comm 0 x) , \u2115\u00b0.*-comm 0 x\n +*-spec (suc x) (suc y) = ap suc p+ , ap suc p*\n where p = +*-spec x y\n p+ = ap suc (fst p) \u2219 ! +-suc x y\n p* = ap\u2082 _+_ (fst p) (snd p)\n \u2219 +-assoc x y (x * y)\n \u2219 ap (\u03bb z \u2192 x + (y + z)) (\u2115\u00b0.*-comm x y)\n \u2219 ! +-assoc-comm y x (y * x)\n \u2219 ap (_+_ y) (\u2115\u00b0.*-comm (suc y) x)\n\nmodule Min where\n open Generic (const zero) suc\n public\n renaming ( dist to _\u2293'_\n ; dist-0\u2261id to \u2293'0-zero\n ; dist-0\u2261id\u2032 to 0\u2293'-zero\n ; dist-comm to \u2293'-comm\n )\n\n \u2293'-spec : \u2200 x y \u2192 x \u2293' y \u2261 x \u2293 y\n \u2293'-spec zero zero = idp\n \u2293'-spec zero (suc y) = idp\n \u2293'-spec (suc x) zero = idp\n \u2293'-spec (suc x) (suc y) = ap suc (\u2293'-spec x y)\n\nmodule Max where\n open Generic id suc\n public\n renaming ( dist to _\u2294'_\n ; dist-0\u2261id to \u2294'0-identity\n ; dist-0\u2261id\u2032 to 0\u2294'-identity\n ; dist-comm to \u2294'-comm\n )\n\n \u2294'-spec : \u2200 x y \u2192 x \u2294' y \u2261 x \u2294 y\n \u2294'-spec zero zero = idp\n \u2294'-spec zero (suc y) = idp\n \u2294'-spec (suc x) zero = idp\n \u2294'-spec (suc x) (suc y) = ap suc (\u2294'-spec x y)\n\ndist-refl : \u2200 x \u2192 dist x x \u2261 0\ndist-refl zero = idp\ndist-refl (suc x) rewrite dist-refl x = idp\n\ndist-x-x+y\u2261y : \u2200 x y \u2192 dist x (x + y) \u2261 y\ndist-x-x+y\u2261y zero y = idp\ndist-x-x+y\u2261y (suc x) y = dist-x-x+y\u2261y x y\n\ndist-x+ : \u2200 x y z \u2192 dist (x + y) (x + z) \u2261 dist y z\ndist-x+ zero y z = idp\ndist-x+ (suc x) y z = dist-x+ x y z\n\ndist-2* : \u2200 x y \u2192 dist (2* x) (2* y) \u2261 2* dist x y\ndist-2* zero y = idp\ndist-2* (suc x) zero = idp\ndist-2* (suc x) (suc y) rewrite +-assoc-comm x 1 x | +-assoc-comm y 1 y = dist-2* x y\n\ndist-asym-def : \u2200 {x y} \u2192 x \u2264 y \u2192 x + dist x y \u2261 y\ndist-asym-def z\u2264n = idp\ndist-asym-def (s\u2264s pf) = ap suc (dist-asym-def pf)\n\ndist-sym-wlog : \u2200 (f : \u2115 \u2192 \u2115) \u2192 (\u2200 x k \u2192 dist (f x) (f (x + k)) \u2261 f k) \u2192 \u2200 x y \u2192 dist (f x) (f y) \u2261 f (dist x y)\ndist-sym-wlog f pf x y with compare x y\ndist-sym-wlog f pf x .(suc (x + k)) | less .x k with pf x (suc k)\n... | q rewrite +-assoc-comm x 1 k | q | ! +-assoc-comm x 1 k | dist-x-x+y\u2261y x (suc k) = idp\ndist-sym-wlog f pf .y y | equal .y with pf y 0\n... | q rewrite \u2115\u00b0.+-comm y 0 | dist-refl y = q\ndist-sym-wlog f pf .(suc (y + k)) y | greater .y k with pf y (suc k)\n... | q rewrite +-assoc-comm 1 y k | dist-comm (y + suc k) y | dist-x-x+y\u2261y y (suc k) | dist-comm (f (y + suc k)) (f y) = q\n\ndist-x* : \u2200 x y z \u2192 dist (x * y) (x * z) \u2261 x * dist y z\ndist-x* x = dist-sym-wlog (_*_ x) pf\n where pf : \u2200 a k \u2192 dist (x * a) (x * (a + k)) \u2261 x * k\n pf a k rewrite fst \u2115\u00b0.distrib x a k = dist-x-x+y\u2261y (x * a) _\n\ndist-2^* : \u2200 x y z \u2192 dist \u27e82^ x * y \u27e9 \u27e82^ x * z \u27e9 \u2261 \u27e82^ x * dist y z \u27e9\ndist-2^* x = dist-sym-wlog (2^\u27e8 x \u27e9*) pf\n where pf : \u2200 a k \u2192 dist \u27e82^ x * a \u27e9 \u27e82^ x * (a + k) \u27e9 \u2261 \u27e82^ x * k \u27e9\n pf a k rewrite 2^*-distrib x a k = dist-x-x+y\u2261y \u27e82^ x * a \u27e9 \u27e82^ x * k \u27e9\n\ndist-bounded : \u2200 {x y f} \u2192 x \u2264 f \u2192 y \u2264 f \u2192 dist x y \u2264 f\ndist-bounded z\u2264n y\u2264f = y\u2264f\ndist-bounded (s\u2264s x\u2264f) z\u2264n = s\u2264s x\u2264f\ndist-bounded (s\u2264s x\u2264f) (s\u2264s y\u2264f) = \u2264-step (dist-bounded x\u2264f y\u2264f)\n\n-- Triangular inequality\ndist-sum : \u2200 x y z \u2192 dist x z \u2264 dist x y + dist y z\ndist-sum zero zero z = \u2115\u2264.refl\ndist-sum zero (suc y) zero = z\u2264n\ndist-sum zero (suc y) (suc z) = s\u2264s (dist-sum zero y z)\ndist-sum (suc x) zero zero = s\u2264s (\u2115\u2264.reflexive (\u2115\u00b0.+-comm 0 x))\ndist-sum (suc x) (suc y) zero\n rewrite \u2115\u00b0.+-comm (dist x y) (suc y)\n | dist-comm x y = s\u2264s (dist-sum zero y x)\ndist-sum (suc x) zero (suc z) = dist-sum x zero z\n \u2219\u2264 \u2115\u2264.reflexive (ap\u2082 _+_ (dist-comm x 0) idp)\n \u2219\u2264 \u2264-step (\u2115\u2264.refl {x} +-mono \u2264-step \u2115\u2264.refl)\ndist-sum (suc x) (suc y) (suc z) = dist-sum x y z\n\ndist\u22610 : \u2200 x y \u2192 dist x y \u2261 0 \u2192 x \u2261 y\ndist\u22610 zero zero d\u22610 = idp\ndist\u22610 zero (suc y) ()\ndist\u22610 (suc x) zero ()\ndist\u22610 (suc x) (suc y) d\u22610 = ap suc (dist\u22610 x y d\u22610)\n\n\u2238-\u2264-dist : \u2200 x y \u2192 x \u2238 y \u2264 dist x y\n\u2238-\u2264-dist zero zero = z\u2264n\n\u2238-\u2264-dist zero (suc y) = z\u2264n\n\u2238-\u2264-dist (suc x) zero = \u2115\u2264.refl\n\u2238-\u2264-dist (suc x) (suc y) = \u2238-\u2264-dist x y\n\n\u2238-dist : \u2200 x y \u2192 y \u2264 x \u2192 x \u2238 y \u2261 dist x y\n\u2238-dist x .0 z\u2264n = ! dist-comm x 0\n\u2238-dist ._ ._ (s\u2264s y\u2264x) = \u2238-dist _ _ y\u2264x\n\ndist-min-max : \u2200 x y \u2192 dist x y \u2261 (x \u2294 y) \u2238 (x \u2293 y)\ndist-min-max zero zero = idp\ndist-min-max zero (suc y) = idp\ndist-min-max (suc x) zero = idp\ndist-min-max (suc x) (suc y) = dist-min-max x y\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"open import Data.Nat.NP\nopen import Data.Nat.Properties as Props\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Data.Nat.Distance where\n\ndist : \u2115 \u2192 \u2115 \u2192 \u2115\ndist zero y = y\ndist x zero = x\ndist (suc x) (suc y) = dist x y\n\ndist-refl : \u2200 x \u2192 dist x x \u2261 0\ndist-refl zero = idp\ndist-refl (suc x) rewrite dist-refl x = idp\n\ndist-0\u2261id : \u2200 x \u2192 dist 0 x \u2261 x\ndist-0\u2261id _ = idp\n\ndist-x-x+y\u2261y : \u2200 x y \u2192 dist x (x + y) \u2261 y\ndist-x-x+y\u2261y zero y = idp\ndist-x-x+y\u2261y (suc x) y = dist-x-x+y\u2261y x y\n\ndist-comm : \u2200 x y \u2192 dist x y \u2261 dist y x\ndist-comm zero zero = idp\ndist-comm zero (suc y) = idp\ndist-comm (suc x) zero = idp\ndist-comm (suc x) (suc y) = dist-comm x y\n\ndist-x+ : \u2200 x y z \u2192 dist (x + y) (x + z) \u2261 dist y z\ndist-x+ zero y z = idp\ndist-x+ (suc x) y z = dist-x+ x y z\n\ndist-2* : \u2200 x y \u2192 dist (2* x) (2* y) \u2261 2* dist x y\ndist-2* zero y = idp\ndist-2* (suc x) zero = idp\ndist-2* (suc x) (suc y) rewrite +-assoc-comm x 1 x | +-assoc-comm y 1 y = dist-2* x y\n\ndist-asym-def : \u2200 {x y} \u2192 x \u2264 y \u2192 x + dist x y \u2261 y\ndist-asym-def z\u2264n = idp\ndist-asym-def (s\u2264s pf) = ap suc (dist-asym-def pf)\n\ndist-sym-wlog : \u2200 (f : \u2115 \u2192 \u2115) \u2192 (\u2200 x k \u2192 dist (f x) (f (x + k)) \u2261 f k) \u2192 \u2200 x y \u2192 dist (f x) (f y) \u2261 f (dist x y)\ndist-sym-wlog f pf x y with compare x y\ndist-sym-wlog f pf x .(suc (x + k)) | less .x k with pf x (suc k)\n... | q rewrite +-assoc-comm x 1 k | q | ! +-assoc-comm x 1 k | dist-x-x+y\u2261y x (suc k) = idp\ndist-sym-wlog f pf .y y | equal .y with pf y 0\n... | q rewrite \u2115\u00b0.+-comm y 0 | dist-refl y = q\ndist-sym-wlog f pf .(suc (y + k)) y | greater .y k with pf y (suc k)\n... | q rewrite +-assoc-comm 1 y k | dist-comm (y + suc k) y | dist-x-x+y\u2261y y (suc k) | dist-comm (f (y + suc k)) (f y) = q\n\ndist-x* : \u2200 x y z \u2192 dist (x * y) (x * z) \u2261 x * dist y z\ndist-x* x = dist-sym-wlog (_*_ x) pf\n where pf : \u2200 a k \u2192 dist (x * a) (x * (a + k)) \u2261 x * k\n pf a k rewrite proj\u2081 \u2115\u00b0.distrib x a k = dist-x-x+y\u2261y (x * a) _\n\ndist-2^* : \u2200 x y z \u2192 dist \u27e82^ x * y \u27e9 \u27e82^ x * z \u27e9 \u2261 \u27e82^ x * dist y z \u27e9\ndist-2^* x = dist-sym-wlog (2^\u27e8 x \u27e9*) pf\n where pf : \u2200 a k \u2192 dist \u27e82^ x * a \u27e9 \u27e82^ x * (a + k) \u27e9 \u2261 \u27e82^ x * k \u27e9\n pf a k rewrite 2^*-distrib x a k = dist-x-x+y\u2261y \u27e82^ x * a \u27e9 \u27e82^ x * k \u27e9\n\ndist-bounded : \u2200 {x y f} \u2192 x \u2264 f \u2192 y \u2264 f \u2192 dist x y \u2264 f\ndist-bounded z\u2264n y\u2264f = y\u2264f\ndist-bounded (s\u2264s x\u2264f) z\u2264n = s\u2264s x\u2264f\ndist-bounded (s\u2264s x\u2264f) (s\u2264s y\u2264f) = \u2264-step (dist-bounded x\u2264f y\u2264f)\n\n-- Triangular inequality\ndist-sum : \u2200 x y z \u2192 dist x z \u2264 dist x y + dist y z\ndist-sum zero zero z = \u2115\u2264.refl\ndist-sum zero (suc y) zero = z\u2264n\ndist-sum zero (suc y) (suc z) = s\u2264s (dist-sum zero y z)\ndist-sum (suc x) zero zero = s\u2264s (\u2115\u2264.reflexive (\u2115\u00b0.+-comm 0 x))\ndist-sum (suc x) (suc y) zero\n rewrite \u2115\u00b0.+-comm (dist x y) (suc y)\n | dist-comm x y = s\u2264s (dist-sum zero y x)\ndist-sum (suc x) zero (suc z) = dist-sum x zero z\n \u2219\u2264 \u2115\u2264.reflexive (ap\u2082 _+_ (dist-comm x 0) idp)\n \u2219\u2264 \u2264-step (\u2115\u2264.refl {x} +-mono \u2264-step \u2115\u2264.refl)\ndist-sum (suc x) (suc y) (suc z) = dist-sum x y z\n\ndist\u22610 : \u2200 x y \u2192 dist x y \u2261 0 \u2192 x \u2261 y\ndist\u22610 zero zero d\u22610 = refl\ndist\u22610 zero (suc y) ()\ndist\u22610 (suc x) zero ()\ndist\u22610 (suc x) (suc y) d\u22610 = ap suc (dist\u22610 x y d\u22610)\n\n\u2238-dist : \u2200 x y \u2192 y \u2264 x \u2192 x \u2238 y \u2261 dist x y\n\u2238-dist x .0 z\u2264n = ! dist-comm x 0\n\u2238-dist ._ ._ (s\u2264s y\u2264x) = \u2238-dist _ _ y\u2264x\n\n{-\npost--ulate\n dist-\u2264 : \u2200 x y \u2192 dist x y \u2264 x\n dist-mono\u2081 : \u2200 x y z t \u2192 x \u2264 y \u2192 dist z t \u2264 dist (x + z) (y + t)\n-}\n\n-- Haskell\n-- let dist x y = abs (x - y)\n-- quickCheck (forAll (replicateM 3 (choose (0,100))) (\\ [x,y,z] -> dist (x * y) (x * z) == x * dist y z))\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"1dd4fe7a8a920498ea83e46ad2707edca7f6f4bf","subject":"Agda: move import to top of file in Syntax.Type.Plotkin","message":"Agda: move import to top of file in Syntax.Type.Plotkin\n\nOld-commit-hash: 0d789d2318a5215ac4ba2f68286e7db5826593ec\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Type\/Plotkin.agda","new_file":"Syntax\/Type\/Plotkin.agda","new_contents":"module Syntax.Type.Plotkin where\n\n-- Types for language description \u00e0 la Plotkin (LCF as PL)\n--\n-- Given base types, we build function types.\n\nopen import Function\n\ninfixr 5 _\u21d2_\n\ndata Type (B : Set {- of base types -}) : Set where\n base : (\u03b9 : B) \u2192 Type B\n _\u21d2_ : (\u03c3 : Type B) \u2192 (\u03c4 : Type B) \u2192 Type B\n\n-- Lift (\u0394 : B \u2192 Type B) to (\u0394type : Type B \u2192 Type B)\n-- according to \u0394 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394 \u03c3 \u21d2 \u0394 \u03c4\nlift-\u0394type : \u2200 {B} \u2192 (B \u2192 Type B) \u2192 (Type B \u2192 Type B)\nlift-\u0394type f (base \u03b9) = f \u03b9\nlift-\u0394type f (\u03c3 \u21d2 \u03c4) = let \u0394 = lift-\u0394type f in \u03c3 \u21d2 \u0394 \u03c3 \u21d2 \u0394 \u03c4\n\n-- Note: the above is *not* a monadic bind.\n--\n-- Proof. base` is the most straightforward `return` of the\n-- functor `Type`.\n--\n-- return : B \u2192 Type B\n-- return = base\n--\n-- Let\n--\n-- m : Type B\n-- m = base \u03ba \u21d2 base \u03b9\n--\n-- then\n--\n-- m >>= return = lift-\u0394type return m\n-- = base \u03ba \u21d2 base \u03ba \u21d2 base \u03b9\n--\n-- violating the second monadic law, m >>= return \u2261 m. \u220e\n\n-- Variant of lift-\u0394type for (\u0394 : B \u2192 B).\nlift-\u0394type\u2080 : \u2200 {B} \u2192 (B \u2192 B) \u2192 (Type B \u2192 Type B)\nlift-\u0394type\u2080 f = lift-\u0394type $ base \u2218 f\n-- This has a similar type to the type of `fmap`,\n-- and `base` has a similar type to the type of `return`.\n--\n-- Similarly, for collections map can be defined from flatMap,\n-- like lift-\u0394type\u2080 can be defined in terms of lift-\u0394type.\n","old_contents":"module Syntax.Type.Plotkin where\n\n-- Types for language description \u00e0 la Plotkin (LCF as PL)\n--\n-- Given base types, we build function types.\n\ninfixr 5 _\u21d2_\n\ndata Type (B : Set {- of base types -}) : Set where\n base : (\u03b9 : B) \u2192 Type B\n _\u21d2_ : (\u03c3 : Type B) \u2192 (\u03c4 : Type B) \u2192 Type B\n\n-- Lift (\u0394 : B \u2192 Type B) to (\u0394type : Type B \u2192 Type B)\n-- according to \u0394 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394 \u03c3 \u21d2 \u0394 \u03c4\nlift-\u0394type : \u2200 {B} \u2192 (B \u2192 Type B) \u2192 (Type B \u2192 Type B)\nlift-\u0394type f (base \u03b9) = f \u03b9\nlift-\u0394type f (\u03c3 \u21d2 \u03c4) = let \u0394 = lift-\u0394type f in \u03c3 \u21d2 \u0394 \u03c3 \u21d2 \u0394 \u03c4\n\n-- Note: the above is *not* a monadic bind.\n--\n-- Proof. base` is the most straightforward `return` of the\n-- functor `Type`.\n--\n-- return : B \u2192 Type B\n-- return = base\n--\n-- Let\n--\n-- m : Type B\n-- m = base \u03ba \u21d2 base \u03b9\n--\n-- then\n--\n-- m >>= return = lift-\u0394type return m\n-- = base \u03ba \u21d2 base \u03ba \u21d2 base \u03b9\n--\n-- violating the second monadic law, m >>= return \u2261 m. \u220e\n\nopen import Function\n\n-- Variant of lift-\u0394type for (\u0394 : B \u2192 B).\nlift-\u0394type\u2080 : \u2200 {B} \u2192 (B \u2192 B) \u2192 (Type B \u2192 Type B)\nlift-\u0394type\u2080 f = lift-\u0394type $ base \u2218 f\n-- This has a similar type to the type of `fmap`,\n-- and `base` has a similar type to the type of `return`.\n--\n-- Similarly, for collections map can be defined from flatMap,\n-- like lift-\u0394type\u2080 can be defined in terms of lift-\u0394type.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"fa1e5e6ba14f3f7038a0a46052c8f81d12ed2e9d","subject":"Clarify Agda comment","message":"Clarify Agda comment\n\nOld-commit-hash: e00f9f0bb80cb86372669d674900a9abd43b7ef4\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Equivalence\/Base.agda","new_file":"Base\/Change\/Equivalence\/Base.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence - base definitions\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence.Base where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\nopen import Function\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n\n -- To avoid unification problems, use a one-field record (a Haskell \"newtype\")\n -- instead of a \"type synonym\".\n record _\u2259_ dx dy : Set a where\n -- doe = Delta-Observational Equivalence.\n constructor doe\n field\n proof : x \u229e dx \u2261 x \u229e dy\n\n open _\u2259_ public\n\n -- Same priority as \u2261\n infix 4 _\u2259_\n\n open import Relation.Binary\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = doe refl\n\n \u2259-sym : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-sym \u2259 = doe $ sym $ proof \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = doe $ trans (proof \u2259\u2081) (proof \u2259\u2082)\n\n -- That's standard congruence applied to \u2259\n \u2259-cong : \u2200 {b} {B : Set b}\n (f : A \u2192 B) {dx dy} \u2192 dx \u2259 dy \u2192 f (x \u229e dx) \u2261 f (x \u229e dy)\n \u2259-cong f da\u2259db = cong f $ proof da\u2259db\n\n \u2259-isEquivalence : IsEquivalence (_\u2259_)\n \u2259-isEquivalence = record\n { refl = \u2259-refl\n ; sym = \u2259-sym\n ; trans = \u2259-trans\n }\n\n \u2259-setoid : Setoid \u2113 a\n \u2259-setoid = record\n { Carrier = \u0394 x\n ; _\u2248_ = _\u2259_\n ; isEquivalence = \u2259-isEquivalence\n }\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence - base definitions\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence.Base where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\nopen import Function\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n\n -- To avoid unification problems, use a one-field record.\n record _\u2259_ dx dy : Set a where\n -- doe = Delta-Observational Equivalence.\n constructor doe\n field\n proof : x \u229e dx \u2261 x \u229e dy\n\n open _\u2259_ public\n\n -- Same priority as \u2261\n infix 4 _\u2259_\n\n open import Relation.Binary\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = doe refl\n\n \u2259-sym : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-sym \u2259 = doe $ sym $ proof \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = doe $ trans (proof \u2259\u2081) (proof \u2259\u2082)\n\n -- That's standard congruence applied to \u2259\n \u2259-cong : \u2200 {b} {B : Set b}\n (f : A \u2192 B) {dx dy} \u2192 dx \u2259 dy \u2192 f (x \u229e dx) \u2261 f (x \u229e dy)\n \u2259-cong f da\u2259db = cong f $ proof da\u2259db\n\n \u2259-isEquivalence : IsEquivalence (_\u2259_)\n \u2259-isEquivalence = record\n { refl = \u2259-refl\n ; sym = \u2259-sym\n ; trans = \u2259-trans\n }\n\n \u2259-setoid : Setoid \u2113 a\n \u2259-setoid = record\n { Carrier = \u0394 x\n ; _\u2248_ = _\u2259_\n ; isEquivalence = \u2259-isEquivalence\n }\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"279231bbe708c42f5ac74a904e54cdc43421d2e0","subject":"Fixed indentation.","message":"Fixed indentation.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Data\/Nat\/Type.agda","new_file":"src\/fot\/FOTC\/Data\/Nat\/Type.agda","new_contents":"------------------------------------------------------------------------------\n-- The FOTC natural numbers type\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- N.B. This module is re-exported by FOTC.Data.Nat.\n\nmodule FOTC.Data.Nat.Type where\n\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n-- The FOTC natural numbers type (inductive predicate for the total\n-- natural numbers).\ndata N : D \u2192 Set where\n nzero : N zero\n nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n{-# ATP axiom nzero nsucc #-}\n\n-- Induction principle.\nN-ind : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\nN-ind A A0 h nzero = A0\nN-ind A A0 h (nsucc Nn) = h (N-ind A A0 h Nn)\n","old_contents":"------------------------------------------------------------------------------\n-- The FOTC natural numbers type\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- N.B. This module is re-exported by FOTC.Data.Nat.\n\nmodule FOTC.Data.Nat.Type where\n\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n-- The FOTC natural numbers type (inductive predicate for the total\n-- natural numbers).\ndata N : D \u2192 Set where\n nzero : N zero\n nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n{-# ATP axiom nzero nsucc #-}\n\n-- Induction principle.\nN-ind : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\nN-ind A A0 h nzero = A0\nN-ind A A0 h (nsucc Nn) = h (N-ind A A0 h Nn)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"697c75829b2c797ef4e527479a655a98ef5fc934","subject":"Added _\u00ab_.","message":"Added _\u00ab_.\n\nIgnore-this: ca495454edf50adffc0277133e7c4df3\n\ndarcs-hash:20110215150831-3bd4e-6b7a74d6063f5dd4275e974f1b96a807a6688f76.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/McCarthy91\/McCarthy91.agda","new_file":"Draft\/McCarthy91\/McCarthy91.agda","new_contents":"------------------------------------------------------------------------------\n-- The McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.McCarthy91 where\n\nopen import LTC.Base\n\nopen import Draft.McCarthy91.ArithmeticATP\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Unary.Numbers\n\n------------------------------------------------------------------------------\n\n-- The McCarthy 91 function\npostulate\n mc91 : D \u2192 D\n mc91-eq\u2081 : \u2200 n \u2192 GT n one-hundred \u2192 mc91 n \u2261 n \u2238 ten\n mc91-eq\u2082 : \u2200 n \u2192 LE n one-hundred \u2192 mc91 n \u2261 mc91 (mc91 (n + eleven))\n{-# ATP axiom mc91-eq\u2081 #-}\n{-# ATP axiom mc91-eq\u2082 #-}\n\n-- Relation use by the properties of the McCarthy 91 function\n_\u00ab_ : D \u2192 D \u2192 D\nm \u00ab n = (hundred-one \u2238 m) < (hundred-one \u2238 n)\n{-# ATP definition _\u00ab_ #-}\n\nMCR : D \u2192 D \u2192 Set\nMCR m n = m \u00ab n \u2261 true\n{-# ATP definition MCR #-}\n","old_contents":"------------------------------------------------------------------------------\n-- The McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.McCarthy91 where\n\nopen import LTC.Base\n\nopen import Draft.McCarthy91.ArithmeticATP\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Unary.Numbers\n\n------------------------------------------------------------------------------\n\n-- The McCarthy 91 function\npostulate\n mc91 : D \u2192 D\n mc91-eq\u2081 : \u2200 n \u2192 GT n one-hundred \u2192 mc91 n \u2261 n \u2238 ten\n mc91-eq\u2082 : \u2200 n \u2192 LE n one-hundred \u2192 mc91 n \u2261 mc91 (mc91 (n + eleven))\n{-# ATP axiom mc91-eq\u2081 #-}\n{-# ATP axiom mc91-eq\u2082 #-}\n\n-- Relation use by the properties of the McCarthy 91 function\nMCR : D \u2192 D \u2192 Set\nMCR m n = LT (hundred-one \u2238 m) (hundred-one \u2238 n)\n-- NB. The ATP pragma is not necessary at the moment.\n-- {-# ATP definition MCR #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ee3e58322cada11dac8048f080e1f4b548cf4bbf","subject":"FunUniverse.Core: adder","message":"FunUniverse.Core: adder\n","repos":"crypto-agda\/crypto-agda","old_file":"FunUniverse\/Core.agda","new_file":"FunUniverse\/Core.agda","new_contents":"module FunUniverse.Core where\n\nopen import Type\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2^_)\nimport Data.Bool.NP as B\nopen B using (if_then_else_; true; false)\nopen import Data.Unit using (\u22a4)\nopen import Data.Fin using (Fin)\nopen import Function using (_\u2218\u2032_; flip)\nimport Data.Vec.NP as V\nimport Level as L\nopen V using (Vec; []; _\u2237_)\n\nopen import Data.Bit using (Bit; 0b; 1b)\nopen import Data.Bits using (Bits; _\u2192\u1d47_; RewireTbl; 0\u207f; 1\u207f)\n\nimport FunUniverse.BinTree as Tree\nopen Tree using (Tree)\nopen import FunUniverse.Data\n\nrecord FunUniverse {t} (T : Set t) : Set (L.suc t) where\n constructor _,_\n field\n universe : Universe T\n _`\u2192_ : T \u2192 T \u2192 \u2605\n\n infix 0 _`\u2192_\n open Universe universe public\n\n _`\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 \u2605\n i `\u2192\u1d47 o = `Bits i `\u2192 `Bits o\n\n `Endo : T \u2192 \u2605\n `Endo A = A `\u2192 A\n\nmodule OpFunU {t} {T : Set t} (funU : FunUniverse T) where\n open FunUniverse funU\n opFunU : FunUniverse T\n opFunU = universe , flip _`\u2192_\n\nmodule Defaults\u27e8first-part\u27e9 {t} {T : Set t} (funU : FunUniverse T) where\n open FunUniverse funU\n\n module CompositionNotations\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)) where\n\n infixr 1 _\u204f_\n infixr 1 _>>>_\n\n _\u204f_ : \u2200 {a b c} \u2192 (a `\u2192 b) \u2192 (b `\u2192 c) \u2192 (a `\u2192 c)\n f \u204f g = g \u2218 f\n\n _>>>_ : \u2200 {a b c} \u2192 (a `\u2192 b) \u2192 (b `\u2192 c) \u2192 (a `\u2192 c)\n f >>> g = f \u204f g\n\n module DefaultsFirstSecond\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (<_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)) where\n\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n first f = < f \u00d7 id >\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = < id \u00d7 f >\n\n module DefaultsGroup2\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (tt : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `\u22a4)\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = < f \u2218 fst , g \u2218 snd >\n\n open DefaultsFirstSecond id <_\u00d7_> public\n\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n dup = < id , id >\n\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n swap = < snd , fst >\n\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc = < fst \u2218 fst , first snd >\n\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n = < tt , id >\n\n snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A\n snd = snd\n\n module DefaultSecondFromFirstSwap\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)) where\n\n open CompositionNotations _\u2218_\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = swap \u204f first f \u204f swap\n\n module Default<\u00d7>FromFirstSecond\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n (second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)) where\n\n open CompositionNotations _\u2218_\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = first f \u204f second g\n\n module DefaultFstFromSndSwap\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B)\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)) where\n\n open CompositionNotations _\u2218_\n\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n fst = swap \u204f snd\n\n module DefaultsGroup1\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (tt : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `\u22a4)\n (snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A)\n (dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A)\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)) where\n\n open CompositionNotations _\u2218_\n open DefaultSecondFromFirstSwap _\u2218_ first swap public\n open Default<\u00d7>FromFirstSecond _\u2218_ first second public\n\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f , g > = dup \u204f < f \u00d7 g >\n\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n snd = first tt \u204f snd\n\n open DefaultFstFromSndSwap _\u2218_ snd swap public\n\n module <\u00d7>Default\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)) where\n\n open CompositionNotations _\u2218_\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = first f \u204f swap \u204f first g \u204f swap\n\n module DefaultAssoc\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n open CompositionNotations _\u2218_\n\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc = < fst \u204f fst , < fst \u204f snd , snd > >\n\n module DefaultAssoc\u2032\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C)))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n where\n open CompositionNotations _\u2218_\n\n -- This definition would cost 1 unit of space instead of 0.\n -- assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n -- assoc\u2032 = < second fst , snd \u204f snd >\n\n assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n assoc\u2032 = swap \u204f first swap \u204f assoc \u204f swap \u204f first swap\n\n module DefaultCondFromFork\n (fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n cond = fork fst snd\n\n -- This definition cost 2 units of space instead of 1.\n module DefaultForkFromCond\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C))\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A) where\n\n open CompositionNotations _\u2218_\n\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork f g = second < f , g > \u204f cond\n\n module DefaultForkFromBijFork\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (bijFork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 `Bit `\u00d7 B)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n open CompositionNotations _\u2218_\n\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork f g = bijFork f g \u204f snd\n\n module DefaultBijForkFromCond\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C))\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A) where\n\n open CompositionNotations _\u2218_\n\n bijFork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 `Bit `\u00d7 B\n bijFork f g = second < f , g > \u204f < fst , cond >\n\n module DefaultBijForkFromFork\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B) where\n\n bijFork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 `Bit `\u00d7 B\n bijFork f g = < fst , fork f g >\n\n module DefaultXor\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (not : `Bit `\u2192 `Bit)\n (<_\u229b> : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 `Vec A n `\u2192 `Vec B n) where\n\n xor : \u2200 {n} \u2192 Bits n \u2192 `Endo (`Bits n)\n xor xs = < V.map (B.cond not id) xs \u229b>\n\n module DefaultRewire\n (rewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i `\u2192\u1d47 o) where\n\n rewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i `\u2192\u1d47 o\n rewire fun = rewireTbl (V.tabulate fun)\n\n module DefaultRewireTbl\n (rewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i `\u2192\u1d47 o) where\n\n rewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i `\u2192\u1d47 o\n rewireTbl tbl = rewire (flip V.lookup tbl)\n\n module LinDefaults\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))) where\n\n open CompositionNotations _\u2218_ public\n open <\u00d7>Default _\u2218_ first swap public\n open DefaultSecondFromFirstSwap _\u2218_ first swap public\n open DefaultAssoc\u2032 _\u2218_ assoc swap first public\n\nrecord LinRewiring {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n\n infixr 9 _\u2218_\n field\n -- Functions\n id : \u2200 {A} \u2192 A `\u2192 A\n _\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)\n\n -- Products (group 2 primitive functions or derived from group 1)\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A\n\n -- Products (derived from group 1 or 2)\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n\n -- Vectors\n tt\u2192[] : \u2200 {A} \u2192 `\u22a4 `\u2192 `Vec A 0\n []\u2192tt : \u2200 {A} \u2192 `Vec A 0 `\u2192 `\u22a4\n <\u2237> : \u2200 {n A} \u2192 (A `\u00d7 `Vec A n) `\u2192 `Vec A (1 + n)\n uncons : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 (A `\u00d7 `Vec A n)\n\n open Defaults\u27e8first-part\u27e9 funU\n open CompositionNotations _\u2218_ public\n open DefaultAssoc\u2032 _\u2218_ assoc swap first public\n\n infixr 3 _***_\n _***_ : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n f *** g = < f \u00d7 g >\n\n : \u2200 {A} \u2192 A `\u2192 A `\u00d7 `\u22a4\n = \u204f swap\n\n : \u2200 {A B C} \u2192 (`\u22a4 `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n = \u204f < f \u00d7 g >\n\n <_,tt\u204f_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (`\u22a4 `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f ,tt\u204f g > = \u204f swap\n\n fst<,tt> : \u2200 {A} \u2192 A `\u00d7 `\u22a4 `\u2192 A\n fst<,tt> = swap \u204f snd\n\n fst<,[]> : \u2200 {A B} \u2192 A `\u00d7 `Vec B 0 `\u2192 A\n fst<,[]> = second []\u2192tt \u204f fst<,tt>\n\n snd<[],> : \u2200 {A B} \u2192 `Vec A 0 `\u00d7 B `\u2192 B\n snd<[],> = first []\u2192tt \u204f snd\n\n -- Like first, but applies on a triple associated the other way\n assoc-first : \u2200 {A B C D E} \u2192 (A `\u00d7 B `\u2192 D `\u00d7 E) \u2192 A `\u00d7 B `\u00d7 C `\u2192 D `\u00d7 E `\u00d7 C\n assoc-first f = assoc\u2032 \u204f first f \u204f assoc\n\n swap-top : \u2200 {A B C} \u2192 A `\u00d7 B `\u00d7 C `\u2192 B `\u00d7 A `\u00d7 C\n swap-top = assoc-first swap\n\n -- Like assoc-first but for second\n assoc-second : \u2200 {A B C D E} \u2192 (B `\u00d7 C `\u2192 E `\u00d7 D) \u2192 (A `\u00d7 B) `\u00d7 C `\u2192 (A `\u00d7 E) `\u00d7 D\n assoc-second f = assoc \u204f second f \u204f assoc\u2032\n\n <_\u00d7\u2081_> : \u2200 {A B C D E F} \u2192 (A `\u00d7 B `\u2192 D `\u00d7 E) \u2192 (C `\u2192 F) \u2192 A `\u00d7 B `\u00d7 C `\u2192 D `\u00d7 E `\u00d7 F\n < f \u00d7\u2081 g > = assoc\u2032 \u204f < f \u00d7 g > \u204f assoc\n\n <_\u00d7\u2082_> : \u2200 {A B C D E F} \u2192 (A `\u2192 D) \u2192 (B `\u00d7 C `\u2192 E `\u00d7 F) \u2192 (A `\u00d7 B) `\u00d7 C `\u2192 (D `\u00d7 E) `\u00d7 F\n < f \u00d7\u2082 g > = assoc \u204f < f \u00d7 g > \u204f assoc\u2032\n\n <_`zip`_> : \u2200 {A B C D E F} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 ((D `\u00d7 E) `\u2192 F)\n \u2192 ((A `\u00d7 D) `\u00d7 (B `\u00d7 E)) `\u2192 (C `\u00d7 F)\n < f `zip` g > = assoc-first (assoc-second swap) \u204f < f \u00d7 g >\n\n{- This one use one unit of space\n < f `zip` g > = < < fst \u00d7 fst > \u204f f ,\n < snd \u00d7 snd > \u204f g >\n-}\n\n <_\u2237\u2032_> : \u2200 {n A B C} \u2192 (A `\u2192 C) \u2192 (B `\u2192 `Vec C n)\n \u2192 A `\u00d7 B `\u2192 `Vec C (1 + n)\n < f \u2237\u2032 g > = < f \u00d7 g > \u204f <\u2237>\n\n <_\u2237_> : \u2200 {m n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A m `\u2192 `Vec B n)\n \u2192 `Vec A (1 + m) `\u2192 `Vec B (1 + n)\n < f \u2237 g > = uncons \u204f < f \u2237\u2032 g >\n\n : \u2200 {n A B} \u2192 (`\u22a4 `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n = \u204f <\u2237>\n\n <_\u2237\u2032tt\u204f_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`\u22a4 `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n < f \u2237\u2032tt\u204f g > = < f ,tt\u204f g > \u204f <\u2237>\n\n <_\u2237[]> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Vec B 1\n < f \u2237[]> = < f \u2237\u2032tt\u204f tt\u2192[] >\n\n <\u2237[]> : \u2200 {A} \u2192 A `\u2192 `Vec A 1\n <\u2237[]> = < id \u2237[]>\n\n <[],_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Vec C 0 `\u00d7 B\n <[], f > = \n\n <_,[]> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 A `\u2192 B `\u00d7 `Vec C 0\n < f ,[]> = < f ,tt\u204f tt\u2192[] >\n\n head<\u2237> : \u2200 {A} \u2192 `Vec A 1 `\u2192 A\n head<\u2237> = uncons \u204f fst<,[]>\n\n constVec\u22a4 : \u2200 {n a} {A : Set a} {B} \u2192 (A \u2192 `\u22a4 `\u2192 B) \u2192 Vec A n \u2192 `\u22a4 `\u2192 `Vec B n\n constVec\u22a4 f [] = tt\u2192[]\n constVec\u22a4 f (x \u2237 xs) = \n\n []\u2192[] : \u2200 {A B} \u2192 `Vec A 0 `\u2192 `Vec B 0\n []\u2192[] = []\u2192tt \u204f tt\u2192[]\n\n <[],[]>\u2192[] : \u2200 {A B C} \u2192 (`Vec A 0 `\u00d7 `Vec B 0) `\u2192 `Vec C 0\n <[],[]>\u2192[] = fst<,[]> \u204f []\u2192[]\n\n <_\u229b> : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 `Vec A n `\u2192 `Vec B n\n <_\u229b> [] = []\u2192[]\n <_\u229b> (f \u2237 fs) = < f \u2237 < fs \u229b> >\n\n foldl : \u2200 {n A B} \u2192 (B `\u00d7 A `\u2192 B) \u2192 (B `\u00d7 `Vec A n) `\u2192 B\n foldl {zero} f = fst<,[]>\n foldl {suc n} f = second uncons\n \u204f assoc\u2032\n \u204f first f\n \u204f foldl f\n\n foldl\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldl\u2081 f = uncons \u204f foldl f\n\n foldr : \u2200 {n A B} \u2192 (A `\u00d7 B `\u2192 B) \u2192 (`Vec A n `\u00d7 B) `\u2192 B\n foldr {zero} f = snd<[],>\n foldr {suc n} f = first uncons\n \u204f assoc\n \u204f second (foldr f)\n \u204f f\n\n foldr\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldr\u2081 f = uncons \u204f swap \u204f foldr f\n\n map : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A n `\u2192 `Vec B n)\n map f = < V.replicate f \u229b>\n\n zipWith : \u2200 {n A B C} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec C n\n zipWith {zero} f = <[],[]>\u2192[]\n zipWith {suc n} f = < uncons \u00d7 uncons >\n \u204f < f `zip` (zipWith f) >\n \u204f <\u2237>\n\n zip : \u2200 {n A B} \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec (A `\u00d7 B) n\n zip = zipWith id\n\n snoc : \u2200 {n A} \u2192 (`Vec A n `\u00d7 A) `\u2192 `Vec A (1 + n)\n snoc {zero} = < snd<[],> \u2237[]>\n snoc {suc n} = first uncons \u204f assoc \u204f second snoc \u204f <\u2237>\n\n reverse : \u2200 {n A} \u2192 `Vec A n `\u2192 `Vec A n\n reverse {zero} = id\n reverse {suc n} = uncons \u204f swap \u204f first reverse \u204f snoc\n\n append : \u2200 {m n A} \u2192 (`Vec A m `\u00d7 `Vec A n) `\u2192 `Vec A (m + n)\n append {zero} = snd<[],>\n append {suc m} = first uncons\n \u204f assoc\n \u204f second append\n \u204f <\u2237>\n\n <_++_> : \u2200 {m n A} \u2192 (`\u22a4 `\u2192 `Vec A m) \u2192 (`\u22a4 `\u2192 `Vec A n) \u2192\n `\u22a4 `\u2192 `Vec A (m + n)\n < f ++ g > = \u204f append\n\n splitAt : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 (`Vec A m `\u00d7 `Vec A n)\n splitAt zero = <[], id >\n splitAt (suc m) = uncons\n \u204f second (splitAt m)\n \u204f assoc\u2032\n \u204f first <\u2237>\n\n rot-left : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A (n + m)\n rot-left m = splitAt m \u204f swap \u204f append\n\n rot-right : \u2200 {m} n {A} \u2192 `Vec A (m + n) `\u2192 `Vec A (n + m)\n rot-right {m} _ = rot-left m\n\n folda : \u2200 n {A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (2^ n) `\u2192 A\n folda zero f = head<\u2237>\n folda (suc n) f = splitAt (2^ n) \u204f < folda n f \u00d7 folda n f > \u204f f\n\n concat : \u2200 {m n A} \u2192 `Vec (`Vec A m) n `\u2192 `Vec A (n * m)\n concat {n = zero} = []\u2192[]\n concat {n = suc n} = uncons \u204f second concat \u204f append\n\n group : \u2200 {A} n k \u2192 `Vec A (n * k) `\u2192 `Vec (`Vec A k) n\n group zero k = []\u2192[]\n group (suc n) k = splitAt k \u204f second (group n k) \u204f <\u2237>\n\n bind : \u2200 {m n A B} \u2192 (A `\u2192 `Vec B m) \u2192 `Vec A n `\u2192 `Vec B (n * m)\n bind f = map f \u204f concat\n\n replicate\u22a4 : \u2200 n \u2192 `\u22a4 `\u2192 `Vec `\u22a4 n\n replicate\u22a4 _ = constVec\u22a4 (\u03bb _ \u2192 id) (V.replicate {A = \u22a4} _)\n\n loop : \u2200 {A} \u2192 \u2115 \u2192 (A `\u2192 A) \u2192 (A `\u2192 A)\n loop zero _ = id\n loop (suc n) f = f \u204f loop n f\n -- or based on fold:\n -- loop n f = < id ,tt\u204f replicate\u22a4 n > \u204f foldl (fst<,tt> \u204f f)\n\nrecord HasBijFork {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n field\n bijFork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 `Bit `\u00d7 B\n\n bijFork\u2032 : \u2200 {A B} \u2192 (Bit \u2192 A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 `Bit `\u00d7 B\n bijFork\u2032 f = bijFork (f 0b) (f 1b)\n\nrecord HasFork {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor _,_\n open FunUniverse funU\n field\n -- See Defaults.DefaultCond\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n\n -- See Defaults.DefaultFork\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n\n fork\u2032 : \u2200 {A B} \u2192 (Bit \u2192 A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork\u2032 f = fork (f 0b) (f 1b)\n\nrecord HasXor {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n field\n xor : \u2200 {n} \u2192 Bits n \u2192 `Endo (`Bits n)\n\n vnot : \u2200 {n} \u2192 `Endo (`Bits n)\n vnot = xor 1\u207f\n\n \u27e8\u2295_\u27e9 : \u2200 {n} \u2192 Bits n \u2192 `Endo (`Bits n)\n \u27e8\u2295 xs \u27e9 = xor xs\n\nrecord Bijective {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n\n field\n linRewiring : LinRewiring funU\n hasBijFork : HasBijFork funU\n hasXor : HasXor funU\n\nrecord Rewiring {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n\n field\n linRewiring : LinRewiring funU\n\n -- Unit (ignoring its argument)\n tt : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `\u22a4\n\n -- Products (all that comes from LinRewiring)\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n\n -- Vectors\n <[]> : \u2200 {_\u22a4 A} \u2192 _\u22a4 `\u2192 `Vec A 0\n -- * <\u2237> and uncons come from LinRewiring\n\n -- Products (group 1 primitive functions or derived from group 2)\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n\n rewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i `\u2192\u1d47 o\n rewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i `\u2192\u1d47 o\n\n open LinRewiring linRewiring public\n\n proj : \u2200 {A} \u2192 Bit \u2192 (A `\u00d7 A) `\u2192 A\n proj true = fst\n proj false = snd\n\n head : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n head = uncons \u204f fst\n\n tail : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n tail = uncons \u204f snd\n\n constVec : \u2200 {n a _\u22a4} {A : Set a} {B} \u2192 (A \u2192 `\u22a4 `\u2192 B) \u2192 Vec A n \u2192 _\u22a4 `\u2192 `Vec B n\n constVec f vec = tt \u204f constVec\u22a4 f vec\n\n take : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A m\n take zero = <[]>\n take (suc m) = < id \u2237 take m >\n\n drop : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A n\n drop zero = id\n drop (suc m) = tail \u204f drop m\n\n msb : \u2200 m {n} \u2192 (m + n) `\u2192\u1d47 m\n msb m = take m\n\n lsb : \u2200 {n} k \u2192 (n + k) `\u2192\u1d47 k\n lsb {n} _ = drop n\n\n init : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n init {zero} = <[]>\n init {suc n} = < id \u2237 init >\n\n last : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n last {zero} = head\n last {suc n} = tail \u204f last\n\n replicate : \u2200 {n A} \u2192 A `\u2192 `Vec A n\n replicate {zero} = <[]>\n replicate {suc n} = < id , replicate > \u204f <\u2237>\n\n constBits\u2032 : \u2200 {n A} \u2192 Bits n \u2192 (A `\u00d7 A) `\u2192 `Vec A n\n constBits\u2032 [] = <[]>\n constBits\u2032 (b \u2237 xs) = dup \u204f < proj b \u2237\u2032 constBits\u2032 xs >\n\nrecord FunOps {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n\n field\n rewiring : Rewiring funU\n hasFork : HasFork funU\n\n -- Bit\n <0b> <1b> : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `Bit\n\n -- Products\n -- * <_\u00d7_>; first; second; swap; assoc; ; snd come from LinRewiring\n -- * dup; <_,_>; fst; snd come from Rewiring\n\n -- Vectors\n -- <[]>; <\u2237>; uncons come from Rewiring\n\n open Defaults\u27e8first-part\u27e9 funU\n open Rewiring rewiring public\n open HasFork hasFork public\n\n : \u2200 {A B} (b : A `\u2192 `Bit) (f g : A `\u2192 B) \u2192 A `\u2192 B\n = < b , id > \u204f fork if-0 if-1\n\n not : `Bit `\u2192 `Bit\n not = \u204f fork <0b> <1b>\n\n -- We might want it to be part of the interface\n hasXor : HasXor funU\n hasXor = mk (DefaultXor.xor id not <_\u229b>)\n\n hasBijFork : HasBijFork funU\n hasBijFork = mk (DefaultBijForkFromFork.bijFork <_,_> fst fork)\n\n bijective : Bijective funU\n bijective = mk linRewiring hasBijFork hasXor\n\n open HasXor hasXor public\n open HasBijFork hasBijFork public\n\n infixr 3 _&&&_\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n f &&& g = < f , g >\n\n constBit : \u2200 {_\u22a4} \u2192 Bit \u2192 _\u22a4 `\u2192 `Bit\n constBit b = if b then <1b> else <0b>\n\n -- Notice that this one costs 1 unit of space.\n dup\u204f<_\u2237\u2032_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n dup\u204f< f \u2237\u2032 g > = dup \u204f < f \u2237\u2032 g >\n\n <0,_> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Bit `\u00d7 B\n <0, f > = , f >\n\n <1,_> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Bit `\u00d7 B\n <1, f > = , f >\n\n <0,> : \u2200 {A} \u2192 A `\u2192 `Bit `\u00d7 A\n <0,> = <0, id >\n\n <1,> : \u2200 {A} \u2192 A `\u2192 `Bit `\u00d7 A\n <1,> = <1, id >\n\n <0,1> : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `Bit `\u00d7 `Bit\n <0,1> = <0, <1b> >\n\n <0\u2237_> : \u2200 {n A} \u2192 (A `\u2192 `Bits n) \u2192 A `\u2192 `Bits (1 + n)\n <0\u2237 f > = \u2237\u2032 f >\n\n <1\u2237_> : \u2200 {n A} \u2192 (A `\u2192 `Bits n) \u2192 A `\u2192 `Bits (1 + n)\n <1\u2237 f > = \u2237\u2032 f >\n\n <0\u2237> : \u2200 {n} \u2192 n `\u2192\u1d47 (1 + n)\n <0\u2237> = <0\u2237 id >\n\n <1\u2237> : \u2200 {n} \u2192 n `\u2192\u1d47 (1 + n)\n <1\u2237> = <1\u2237 id >\n\n constBits : \u2200 {n _\u22a4} \u2192 Bits n \u2192 _\u22a4 `\u2192 `Bits n\n constBits = constVec constBit\n\n <0\u207f> : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Bits n\n <0\u207f> = constBits 0\u207f\n\n <1\u207f> : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Bits n\n <1\u207f> = constBits 1\u207f\n\n constBits\u2032\u2032 : \u2200 {n _\u22a4} \u2192 Bits n \u2192 _\u22a4 `\u2192 `Bits n\n constBits\u2032\u2032 bs = <0,1> \u204f constBits\u2032 bs\n\n `Maybe : T \u2192 T\n `Maybe A = `Bit `\u00d7 A\n\n : \u2200 {A} \u2192 A `\u2192 `Maybe A\n = <0,>\n\n : \u2200 {A} \u2192 A `\u2192 `Maybe A\n = <1,>\n\n : \u2200 {A B C} \u2192 (f : A `\u00d7 B `\u2192 C) (g : B `\u2192 C) \u2192 `Maybe A `\u00d7 B `\u2192 C\n = \n\n _\u2223?_ : \u2200 {A} \u2192 `Maybe A `\u00d7 `Maybe A `\u2192 `Maybe A\n _\u2223?_ = \u2236 id >\n\n _`\u2192?_ : T \u2192 T \u2192 Set\n A `\u2192? B = A `\u2192 `Maybe B\n\n search : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 (`Bits n `\u2192 A) \u2192 `\u22a4 `\u2192 A\n search {zero} _ f = <[]> \u204f f\n search {suc n} op f = ) , search op (f \u2218 <1\u2237>) > \u204f op\n\n find? : \u2200 {n A} \u2192 (`Bits n `\u2192? A) \u2192 `\u22a4 `\u2192? A\n find? = search _\u2223?_\n\n findB : \u2200 {n} \u2192 (`Bits n `\u2192 `Bit) \u2192 `\u22a4 `\u2192? `Bits n\n findB pred = find? else >\n\n fromTree : \u2200 {n A} \u2192 Tree (`\u22a4 `\u2192 A) n \u2192 `Bits n `\u2192 A\n fromTree (Tree.leaf x) = tt \u204f x\n fromTree (Tree.fork t\u2080 t\u2081) = uncons \u204f fork (fromTree t\u2080) (fromTree t\u2081)\n\n fromFun : \u2200 {n A} \u2192 (Bits n \u2192 `\u22a4 `\u2192 A) \u2192 `Bits n `\u2192 A\n fromFun = fromTree \u2218\u2032 Tree.fromFun\n\n fromBitsFun : \u2200 {i o} \u2192 (i \u2192\u1d47 o) \u2192 i `\u2192\u1d47 o\n fromBitsFun f = fromFun (constBits \u2218\u2032 f)\n\n : `Bit `\u00d7 `Bit `\u2192 `Bit\n = fork id not\n\n : `Bit `\u00d7 `Bit `\u2192 `Bit\n = fork id <1b>\n\n : `Bit `\u00d7 `Bit `\u2192 `Bit\n = fork <0b> id\n\n <==\u1d47> : `Bit `\u00d7 `Bit `\u2192 `Bit\n <==\u1d47> = \u204f not\n\n <==> : \u2200 {n} \u2192 `Bits n `\u00d7 `Bits n `\u2192 `Bit\n <==> {zero} = <1b>\n <==> {suc n} = < uncons \u00d7 uncons > \u204f < <==\u1d47> `zip` <==> {n} > \u204f \n\n <\u2295> : \u2200 {n} \u2192 `Bits n `\u00d7 `Bits n `\u2192 `Bits n\n <\u2295> = zipWith \n\n -- vnot : \u2200 {n} \u2192 `Endo (`Bits n)\n -- vnot = map not\n\n allBits : \u2200 n \u2192 `\u22a4 `\u2192 `Vec (`Bits n) (2^ n)\n allBits zero = < <[]> \u2237[]>\n allBits (suc n) = < bs \u204f map <0\u2237> ++ bs \u204f map <1\u2237> >\n where bs = allBits n\n\n sucBCarry : \u2200 {n} \u2192 `Bits n `\u2192 `Bits (1 + n)\n sucBCarry {zero} = < <0b> \u2237[]>\n sucBCarry {suc n} = uncons\n \u204f fork <0\u2237 sucBCarry >\n (sucBCarry \u204f uncons \u204f fork <0\u2237 <1\u2237> > <1\u2237 <0\u2237> >)\n\n sucB : \u2200 {n} \u2192 `Bits n `\u2192 `Bits n\n sucB = sucBCarry \u204f tail\n\n half-adder : `Bit `\u00d7 `Bit `\u2192 `Bit `\u00d7 `Bit\n half-adder = < , >\n\n full-adder : `Bit `\u00d7 `Bit `\u00d7 `Bit `\u2192 `Bit `\u00d7 `Bit\n full-adder = < (sc\u2082 \u204f fst) , < (sc\u2081 \u204f snd) , (sc\u2082 \u204f snd) > \u204f >\n where a = snd \u204f fst\n b = snd \u204f snd\n c\u1d62\u2099 = fst\n sc\u2081 = < a , b > \u204f half-adder\n sc\u2082 = < (sc\u2081 \u204f fst) , c\u1d62\u2099 > \u204f half-adder\n\n lookupTbl : \u2200 {n A} \u2192 `Bits n `\u00d7 `Vec A (2^ n) `\u2192 A\n lookupTbl {zero} = snd \u204f head\n lookupTbl {suc n}\n = first uncons\n \u204f assoc\n \u204f fork (second (take (2^ n)) \u204f lookupTbl)\n (second (drop (2^ n)) \u204f lookupTbl)\n\n funFromTbl : \u2200 {n A} \u2192 Vec (`\u22a4 `\u2192 A) (2^ n) \u2192 (`Bits n `\u2192 A)\n funFromTbl {zero} (x \u2237 []) = tt \u204f x\n funFromTbl {suc n} tbl\n = uncons \u204f fork (funFromTbl (V.take (2^ n) tbl))\n (funFromTbl (V.drop (2^ n) tbl))\n\n tblFromFun : \u2200 {n A} \u2192 (`Bits n `\u2192 A) \u2192 `\u22a4 `\u2192 `Vec A (2^ n)\n tblFromFun {zero} f = < <[]> \u204f f \u2237[]>\n tblFromFun {suc n} f = < tblFromFun (<0\u2237> \u204f f) ++\n tblFromFun (<1\u2237> \u204f f) >\n\n module WithFin\n (`Fin : \u2115 \u2192 T)\n (fz : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Fin (suc n))\n (fs : \u2200 {n} \u2192 `Fin n `\u2192 `Fin (suc n))\n (elim-Fin0 : \u2200 {A} \u2192 `Fin 0 `\u2192 A)\n (elim-Fin1+ : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Fin n `\u00d7 A `\u2192 B) \u2192 `Fin (suc n) `\u00d7 A `\u2192 B) where\n\n tabulate : \u2200 {n A _B} \u2192 (`Fin n `\u2192 A) \u2192 _B `\u2192 `Vec A n\n tabulate {zero} f = <[]>\n tabulate {suc n} f = \n\n lookup : \u2200 {n A} \u2192 `Fin n `\u00d7 `Vec A n `\u2192 A\n lookup {zero} = fst \u204f elim-Fin0\n lookup {suc n} = elim-Fin1+ head (second tail \u204f lookup)\n\n allFin : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Vec (`Fin n) n\n allFin = tabulate id\n\nmodule Defaults {t} {T : Set t} (funU : FunUniverse T) where\n open FunUniverse funU\n open Defaults\u27e8first-part\u27e9 funU public\n\n module RewiringDefaults\n (linRewiring : LinRewiring funU)\n (tt : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `\u22a4)\n (dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A)\n (rewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i `\u2192\u1d47 o) where\n\n open LinRewiring linRewiring\n open DefaultsGroup1 _\u2218_ tt snd dup swap first public\n <[]> : \u2200 {_\u22a4 A} \u2192 _\u22a4 `\u2192 `Vec A 0\n <[]> = tt \u204f tt\u2192[]\n open DefaultRewireTbl rewire public\n-- -}\n","old_contents":"module FunUniverse.Core where\n\nopen import Type\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2^_)\nimport Data.Bool.NP as B\nopen B using (if_then_else_; true; false)\nopen import Data.Unit using (\u22a4)\nopen import Data.Fin using (Fin)\nopen import Function using (_\u2218\u2032_; flip)\nimport Data.Vec.NP as V\nimport Level as L\nopen V using (Vec; []; _\u2237_)\n\nopen import Data.Bit using (Bit; 0b; 1b)\nopen import Data.Bits using (Bits; _\u2192\u1d47_; RewireTbl; 0\u207f; 1\u207f)\n\nimport FunUniverse.BinTree as Tree\nopen Tree using (Tree)\nopen import FunUniverse.Data\n\nrecord FunUniverse {t} (T : Set t) : Set (L.suc t) where\n constructor _,_\n field\n universe : Universe T\n _`\u2192_ : T \u2192 T \u2192 \u2605\n\n infix 0 _`\u2192_\n open Universe universe public\n\n _`\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 \u2605\n i `\u2192\u1d47 o = `Bits i `\u2192 `Bits o\n\n `Endo : T \u2192 \u2605\n `Endo A = A `\u2192 A\n\nmodule OpFunU {t} {T : Set t} (funU : FunUniverse T) where\n open FunUniverse funU\n opFunU : FunUniverse T\n opFunU = universe , flip _`\u2192_\n\nmodule Defaults\u27e8first-part\u27e9 {t} {T : Set t} (funU : FunUniverse T) where\n open FunUniverse funU\n\n module CompositionNotations\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)) where\n\n infixr 1 _\u204f_\n infixr 1 _>>>_\n\n _\u204f_ : \u2200 {a b c} \u2192 (a `\u2192 b) \u2192 (b `\u2192 c) \u2192 (a `\u2192 c)\n f \u204f g = g \u2218 f\n\n _>>>_ : \u2200 {a b c} \u2192 (a `\u2192 b) \u2192 (b `\u2192 c) \u2192 (a `\u2192 c)\n f >>> g = f \u204f g\n\n module DefaultsFirstSecond\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (<_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)) where\n\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n first f = < f \u00d7 id >\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = < id \u00d7 f >\n\n module DefaultsGroup2\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (tt : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `\u22a4)\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = < f \u2218 fst , g \u2218 snd >\n\n open DefaultsFirstSecond id <_\u00d7_> public\n\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n dup = < id , id >\n\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n swap = < snd , fst >\n\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc = < fst \u2218 fst , first snd >\n\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n = < tt , id >\n\n snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A\n snd = snd\n\n module DefaultSecondFromFirstSwap\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)) where\n\n open CompositionNotations _\u2218_\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = swap \u204f first f \u204f swap\n\n module Default<\u00d7>FromFirstSecond\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n (second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)) where\n\n open CompositionNotations _\u2218_\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = first f \u204f second g\n\n module DefaultFstFromSndSwap\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B)\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)) where\n\n open CompositionNotations _\u2218_\n\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n fst = swap \u204f snd\n\n module DefaultsGroup1\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (tt : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `\u22a4)\n (snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A)\n (dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A)\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)) where\n\n open CompositionNotations _\u2218_\n open DefaultSecondFromFirstSwap _\u2218_ first swap public\n open Default<\u00d7>FromFirstSecond _\u2218_ first second public\n\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f , g > = dup \u204f < f \u00d7 g >\n\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n snd = first tt \u204f snd\n\n open DefaultFstFromSndSwap _\u2218_ snd swap public\n\n module <\u00d7>Default\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)) where\n\n open CompositionNotations _\u2218_\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = first f \u204f swap \u204f first g \u204f swap\n\n module DefaultAssoc\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n open CompositionNotations _\u2218_\n\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc = < fst \u204f fst , < fst \u204f snd , snd > >\n\n module DefaultAssoc\u2032\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C)))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n where\n open CompositionNotations _\u2218_\n\n -- This definition would cost 1 unit of space instead of 0.\n -- assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n -- assoc\u2032 = < second fst , snd \u204f snd >\n\n assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n assoc\u2032 = swap \u204f first swap \u204f assoc \u204f swap \u204f first swap\n\n module DefaultCondFromFork\n (fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n cond = fork fst snd\n\n -- This definition cost 2 units of space instead of 1.\n module DefaultForkFromCond\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C))\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A) where\n\n open CompositionNotations _\u2218_\n\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork f g = second < f , g > \u204f cond\n\n module DefaultForkFromBijFork\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (bijFork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 `Bit `\u00d7 B)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n open CompositionNotations _\u2218_\n\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork f g = bijFork f g \u204f snd\n\n module DefaultBijForkFromCond\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C))\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A) where\n\n open CompositionNotations _\u2218_\n\n bijFork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 `Bit `\u00d7 B\n bijFork f g = second < f , g > \u204f < fst , cond >\n\n module DefaultBijForkFromFork\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B) where\n\n bijFork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 `Bit `\u00d7 B\n bijFork f g = < fst , fork f g >\n\n module DefaultXor\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (not : `Bit `\u2192 `Bit)\n (<_\u229b> : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 `Vec A n `\u2192 `Vec B n) where\n\n xor : \u2200 {n} \u2192 Bits n \u2192 `Endo (`Bits n)\n xor xs = < V.map (B.cond not id) xs \u229b>\n\n module DefaultRewire\n (rewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i `\u2192\u1d47 o) where\n\n rewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i `\u2192\u1d47 o\n rewire fun = rewireTbl (V.tabulate fun)\n\n module DefaultRewireTbl\n (rewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i `\u2192\u1d47 o) where\n\n rewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i `\u2192\u1d47 o\n rewireTbl tbl = rewire (flip V.lookup tbl)\n\n module LinDefaults\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))) where\n\n open CompositionNotations _\u2218_ public\n open <\u00d7>Default _\u2218_ first swap public\n open DefaultSecondFromFirstSwap _\u2218_ first swap public\n open DefaultAssoc\u2032 _\u2218_ assoc swap first public\n\nrecord LinRewiring {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n\n infixr 9 _\u2218_\n field\n -- Functions\n id : \u2200 {A} \u2192 A `\u2192 A\n _\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)\n\n -- Products (group 2 primitive functions or derived from group 1)\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A\n\n -- Products (derived from group 1 or 2)\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n\n -- Vectors\n tt\u2192[] : \u2200 {A} \u2192 `\u22a4 `\u2192 `Vec A 0\n []\u2192tt : \u2200 {A} \u2192 `Vec A 0 `\u2192 `\u22a4\n <\u2237> : \u2200 {n A} \u2192 (A `\u00d7 `Vec A n) `\u2192 `Vec A (1 + n)\n uncons : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 (A `\u00d7 `Vec A n)\n\n open Defaults\u27e8first-part\u27e9 funU\n open CompositionNotations _\u2218_ public\n open DefaultAssoc\u2032 _\u2218_ assoc swap first public\n\n infixr 3 _***_\n _***_ : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n f *** g = < f \u00d7 g >\n\n : \u2200 {A} \u2192 A `\u2192 A `\u00d7 `\u22a4\n = \u204f swap\n\n : \u2200 {A B C} \u2192 (`\u22a4 `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n = \u204f < f \u00d7 g >\n\n <_,tt\u204f_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (`\u22a4 `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f ,tt\u204f g > = \u204f swap\n\n fst<,tt> : \u2200 {A} \u2192 A `\u00d7 `\u22a4 `\u2192 A\n fst<,tt> = swap \u204f snd\n\n fst<,[]> : \u2200 {A B} \u2192 A `\u00d7 `Vec B 0 `\u2192 A\n fst<,[]> = second []\u2192tt \u204f fst<,tt>\n\n snd<[],> : \u2200 {A B} \u2192 `Vec A 0 `\u00d7 B `\u2192 B\n snd<[],> = first []\u2192tt \u204f snd\n\n -- Like first, but applies on a triple associated the other way\n assoc-first : \u2200 {A B C D E} \u2192 (A `\u00d7 B `\u2192 D `\u00d7 E) \u2192 A `\u00d7 B `\u00d7 C `\u2192 D `\u00d7 E `\u00d7 C\n assoc-first f = assoc\u2032 \u204f first f \u204f assoc\n\n swap-top : \u2200 {A B C} \u2192 A `\u00d7 B `\u00d7 C `\u2192 B `\u00d7 A `\u00d7 C\n swap-top = assoc-first swap\n\n -- Like assoc-first but for second\n assoc-second : \u2200 {A B C D E} \u2192 (B `\u00d7 C `\u2192 E `\u00d7 D) \u2192 (A `\u00d7 B) `\u00d7 C `\u2192 (A `\u00d7 E) `\u00d7 D\n assoc-second f = assoc \u204f second f \u204f assoc\u2032\n\n <_\u00d7\u2081_> : \u2200 {A B C D E F} \u2192 (A `\u00d7 B `\u2192 D `\u00d7 E) \u2192 (C `\u2192 F) \u2192 A `\u00d7 B `\u00d7 C `\u2192 D `\u00d7 E `\u00d7 F\n < f \u00d7\u2081 g > = assoc\u2032 \u204f < f \u00d7 g > \u204f assoc\n\n <_\u00d7\u2082_> : \u2200 {A B C D E F} \u2192 (A `\u2192 D) \u2192 (B `\u00d7 C `\u2192 E `\u00d7 F) \u2192 (A `\u00d7 B) `\u00d7 C `\u2192 (D `\u00d7 E) `\u00d7 F\n < f \u00d7\u2082 g > = assoc \u204f < f \u00d7 g > \u204f assoc\u2032\n\n <_`zip`_> : \u2200 {A B C D E F} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 ((D `\u00d7 E) `\u2192 F)\n \u2192 ((A `\u00d7 D) `\u00d7 (B `\u00d7 E)) `\u2192 (C `\u00d7 F)\n < f `zip` g > = assoc-first (assoc-second swap) \u204f < f \u00d7 g >\n\n{- This one use one unit of space\n < f `zip` g > = < < fst \u00d7 fst > \u204f f ,\n < snd \u00d7 snd > \u204f g >\n-}\n\n <_\u2237\u2032_> : \u2200 {n A B C} \u2192 (A `\u2192 C) \u2192 (B `\u2192 `Vec C n)\n \u2192 A `\u00d7 B `\u2192 `Vec C (1 + n)\n < f \u2237\u2032 g > = < f \u00d7 g > \u204f <\u2237>\n\n <_\u2237_> : \u2200 {m n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A m `\u2192 `Vec B n)\n \u2192 `Vec A (1 + m) `\u2192 `Vec B (1 + n)\n < f \u2237 g > = uncons \u204f < f \u2237\u2032 g >\n\n : \u2200 {n A B} \u2192 (`\u22a4 `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n = \u204f <\u2237>\n\n <_\u2237\u2032tt\u204f_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`\u22a4 `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n < f \u2237\u2032tt\u204f g > = < f ,tt\u204f g > \u204f <\u2237>\n\n <_\u2237[]> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Vec B 1\n < f \u2237[]> = < f \u2237\u2032tt\u204f tt\u2192[] >\n\n <\u2237[]> : \u2200 {A} \u2192 A `\u2192 `Vec A 1\n <\u2237[]> = < id \u2237[]>\n\n <[],_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Vec C 0 `\u00d7 B\n <[], f > = \n\n <_,[]> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 A `\u2192 B `\u00d7 `Vec C 0\n < f ,[]> = < f ,tt\u204f tt\u2192[] >\n\n head<\u2237> : \u2200 {A} \u2192 `Vec A 1 `\u2192 A\n head<\u2237> = uncons \u204f fst<,[]>\n\n constVec\u22a4 : \u2200 {n a} {A : Set a} {B} \u2192 (A \u2192 `\u22a4 `\u2192 B) \u2192 Vec A n \u2192 `\u22a4 `\u2192 `Vec B n\n constVec\u22a4 f [] = tt\u2192[]\n constVec\u22a4 f (x \u2237 xs) = \n\n []\u2192[] : \u2200 {A B} \u2192 `Vec A 0 `\u2192 `Vec B 0\n []\u2192[] = []\u2192tt \u204f tt\u2192[]\n\n <[],[]>\u2192[] : \u2200 {A B C} \u2192 (`Vec A 0 `\u00d7 `Vec B 0) `\u2192 `Vec C 0\n <[],[]>\u2192[] = fst<,[]> \u204f []\u2192[]\n\n <_\u229b> : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 `Vec A n `\u2192 `Vec B n\n <_\u229b> [] = []\u2192[]\n <_\u229b> (f \u2237 fs) = < f \u2237 < fs \u229b> >\n\n foldl : \u2200 {n A B} \u2192 (B `\u00d7 A `\u2192 B) \u2192 (B `\u00d7 `Vec A n) `\u2192 B\n foldl {zero} f = fst<,[]>\n foldl {suc n} f = second uncons\n \u204f assoc\u2032\n \u204f first f\n \u204f foldl f\n\n foldl\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldl\u2081 f = uncons \u204f foldl f\n\n foldr : \u2200 {n A B} \u2192 (A `\u00d7 B `\u2192 B) \u2192 (`Vec A n `\u00d7 B) `\u2192 B\n foldr {zero} f = snd<[],>\n foldr {suc n} f = first uncons\n \u204f assoc\n \u204f second (foldr f)\n \u204f f\n\n foldr\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldr\u2081 f = uncons \u204f swap \u204f foldr f\n\n map : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A n `\u2192 `Vec B n)\n map f = < V.replicate f \u229b>\n\n zipWith : \u2200 {n A B C} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec C n\n zipWith {zero} f = <[],[]>\u2192[]\n zipWith {suc n} f = < uncons \u00d7 uncons >\n \u204f < f `zip` (zipWith f) >\n \u204f <\u2237>\n\n zip : \u2200 {n A B} \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec (A `\u00d7 B) n\n zip = zipWith id\n\n snoc : \u2200 {n A} \u2192 (`Vec A n `\u00d7 A) `\u2192 `Vec A (1 + n)\n snoc {zero} = < snd<[],> \u2237[]>\n snoc {suc n} = first uncons \u204f assoc \u204f second snoc \u204f <\u2237>\n\n reverse : \u2200 {n A} \u2192 `Vec A n `\u2192 `Vec A n\n reverse {zero} = id\n reverse {suc n} = uncons \u204f swap \u204f first reverse \u204f snoc\n\n append : \u2200 {m n A} \u2192 (`Vec A m `\u00d7 `Vec A n) `\u2192 `Vec A (m + n)\n append {zero} = snd<[],>\n append {suc m} = first uncons\n \u204f assoc\n \u204f second append\n \u204f <\u2237>\n\n <_++_> : \u2200 {m n A} \u2192 (`\u22a4 `\u2192 `Vec A m) \u2192 (`\u22a4 `\u2192 `Vec A n) \u2192\n `\u22a4 `\u2192 `Vec A (m + n)\n < f ++ g > = \u204f append\n\n splitAt : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 (`Vec A m `\u00d7 `Vec A n)\n splitAt zero = <[], id >\n splitAt (suc m) = uncons\n \u204f second (splitAt m)\n \u204f assoc\u2032\n \u204f first <\u2237>\n\n folda : \u2200 n {A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (2^ n) `\u2192 A\n folda zero f = head<\u2237>\n folda (suc n) f = splitAt (2^ n) \u204f < folda n f \u00d7 folda n f > \u204f f\n\n concat : \u2200 {m n A} \u2192 `Vec (`Vec A m) n `\u2192 `Vec A (n * m)\n concat {n = zero} = []\u2192[]\n concat {n = suc n} = uncons \u204f second concat \u204f append\n\n group : \u2200 {A} n k \u2192 `Vec A (n * k) `\u2192 `Vec (`Vec A k) n\n group zero k = []\u2192[]\n group (suc n) k = splitAt k \u204f second (group n k) \u204f <\u2237>\n\n bind : \u2200 {m n A B} \u2192 (A `\u2192 `Vec B m) \u2192 `Vec A n `\u2192 `Vec B (n * m)\n bind f = map f \u204f concat\n\n replicate\u22a4 : \u2200 n \u2192 `\u22a4 `\u2192 `Vec `\u22a4 n\n replicate\u22a4 _ = constVec\u22a4 (\u03bb _ \u2192 id) (V.replicate {A = \u22a4} _)\n\n loop : \u2200 {A} \u2192 \u2115 \u2192 (A `\u2192 A) \u2192 (A `\u2192 A)\n loop zero _ = id\n loop (suc n) f = f \u204f loop n f\n -- or based on fold:\n -- loop n f = < id ,tt\u204f replicate\u22a4 n > \u204f foldl (fst<,tt> \u204f f)\n\nrecord HasBijFork {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n field\n bijFork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 `Bit `\u00d7 B\n\n bijFork\u2032 : \u2200 {A B} \u2192 (Bit \u2192 A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 `Bit `\u00d7 B\n bijFork\u2032 f = bijFork (f 0b) (f 1b)\n\nrecord HasFork {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor _,_\n open FunUniverse funU\n field\n -- See Defaults.DefaultCond\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n\n -- See Defaults.DefaultFork\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n\n fork\u2032 : \u2200 {A B} \u2192 (Bit \u2192 A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork\u2032 f = fork (f 0b) (f 1b)\n\nrecord HasXor {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n field\n xor : \u2200 {n} \u2192 Bits n \u2192 `Endo (`Bits n)\n\n vnot : \u2200 {n} \u2192 `Endo (`Bits n)\n vnot = xor 1\u207f\n\n \u27e8\u2295_\u27e9 : \u2200 {n} \u2192 Bits n \u2192 `Endo (`Bits n)\n \u27e8\u2295 xs \u27e9 = xor xs\n\nrecord Bijective {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n\n field\n linRewiring : LinRewiring funU\n hasBijFork : HasBijFork funU\n hasXor : HasXor funU\n\nrecord Rewiring {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n\n field\n linRewiring : LinRewiring funU\n\n -- Unit\n tt : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `\u22a4\n\n -- Products (all that comes from LinRewiring)\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n\n -- Vectors\n <[]> : \u2200 {_\u22a4 A} \u2192 _\u22a4 `\u2192 `Vec A 0\n -- * <\u2237> and uncons come from LinRewiring\n\n -- Products (group 1 primitive functions or derived from group 2)\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n\n rewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i `\u2192\u1d47 o\n rewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i `\u2192\u1d47 o\n\n open LinRewiring linRewiring public\n\n proj : \u2200 {A} \u2192 Bit \u2192 (A `\u00d7 A) `\u2192 A\n proj true = fst\n proj false = snd\n\n head : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n head = uncons \u204f fst\n\n tail : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n tail = uncons \u204f snd\n\n constVec : \u2200 {n a _\u22a4} {A : Set a} {B} \u2192 (A \u2192 `\u22a4 `\u2192 B) \u2192 Vec A n \u2192 _\u22a4 `\u2192 `Vec B n\n constVec f vec = tt \u204f constVec\u22a4 f vec\n\n take : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A m\n take zero = <[]>\n take (suc m) = < id \u2237 take m >\n\n drop : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A n\n drop zero = id\n drop (suc m) = tail \u204f drop m\n\n msb : \u2200 m {n} \u2192 (m + n) `\u2192\u1d47 m\n msb m = take m\n\n lsb : \u2200 {n} k \u2192 (n + k) `\u2192\u1d47 k\n lsb {n} _ = drop n\n\n init : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n init {zero} = <[]>\n init {suc n} = < id \u2237 init >\n\n last : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n last {zero} = head\n last {suc n} = tail \u204f last\n\n replicate : \u2200 {n A} \u2192 A `\u2192 `Vec A n\n replicate {zero} = <[]>\n replicate {suc n} = < id , replicate > \u204f <\u2237>\n\n constBits\u2032 : \u2200 {n A} \u2192 Bits n \u2192 (A `\u00d7 A) `\u2192 `Vec A n\n constBits\u2032 [] = <[]>\n constBits\u2032 (b \u2237 xs) = dup \u204f < proj b \u2237\u2032 constBits\u2032 xs >\n\nrecord FunOps {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n\n field\n rewiring : Rewiring funU\n hasFork : HasFork funU\n\n -- Bit\n <0b> <1b> : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `Bit\n\n -- Products\n -- * <_\u00d7_>; first; second; swap; assoc; ; snd come from LinRewiring\n -- * dup; <_,_>; fst; snd come from Rewiring\n\n -- Vectors\n -- <[]>; <\u2237>; uncons come from Rewiring\n\n open Defaults\u27e8first-part\u27e9 funU\n open Rewiring rewiring public\n open HasFork hasFork public\n\n : \u2200 {A B} (b : A `\u2192 `Bit) (f g : A `\u2192 B) \u2192 A `\u2192 B\n = < b , id > \u204f fork if-0 if-1\n\n not : `Bit `\u2192 `Bit\n not = \u204f fork <0b> <1b>\n\n -- We might want it to be part of the interface\n hasXor : HasXor funU\n hasXor = mk (DefaultXor.xor id not <_\u229b>)\n\n hasBijFork : HasBijFork funU\n hasBijFork = mk (DefaultBijForkFromFork.bijFork <_,_> fst fork)\n\n bijective : Bijective funU\n bijective = mk linRewiring hasBijFork hasXor\n\n open HasXor hasXor public\n open HasBijFork hasBijFork public\n\n infixr 3 _&&&_\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n f &&& g = < f , g >\n\n constBit : \u2200 {_\u22a4} \u2192 Bit \u2192 _\u22a4 `\u2192 `Bit\n constBit b = if b then <1b> else <0b>\n\n -- Notice that this one costs 1 unit of space.\n dup\u204f<_\u2237\u2032_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n dup\u204f< f \u2237\u2032 g > = dup \u204f < f \u2237\u2032 g >\n\n <0,_> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Bit `\u00d7 B\n <0, f > = , f >\n\n <1,_> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Bit `\u00d7 B\n <1, f > = , f >\n\n <0,> : \u2200 {A} \u2192 A `\u2192 `Bit `\u00d7 A\n <0,> = <0, id >\n\n <1,> : \u2200 {A} \u2192 A `\u2192 `Bit `\u00d7 A\n <1,> = <1, id >\n\n <0,1> : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `Bit `\u00d7 `Bit\n <0,1> = <0, <1b> >\n\n <0\u2237_> : \u2200 {n A} \u2192 (A `\u2192 `Bits n) \u2192 A `\u2192 `Bits (1 + n)\n <0\u2237 f > = \u2237\u2032 f >\n\n <1\u2237_> : \u2200 {n A} \u2192 (A `\u2192 `Bits n) \u2192 A `\u2192 `Bits (1 + n)\n <1\u2237 f > = \u2237\u2032 f >\n\n <0\u2237> : \u2200 {n} \u2192 n `\u2192\u1d47 (1 + n)\n <0\u2237> = <0\u2237 id >\n\n <1\u2237> : \u2200 {n} \u2192 n `\u2192\u1d47 (1 + n)\n <1\u2237> = <1\u2237 id >\n\n constBits : \u2200 {n _\u22a4} \u2192 Bits n \u2192 _\u22a4 `\u2192 `Bits n\n constBits = constVec constBit\n\n <0\u207f> : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Bits n\n <0\u207f> = constBits 0\u207f\n\n <1\u207f> : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Bits n\n <1\u207f> = constBits 1\u207f\n\n constBits\u2032\u2032 : \u2200 {n _\u22a4} \u2192 Bits n \u2192 _\u22a4 `\u2192 `Bits n\n constBits\u2032\u2032 bs = <0,1> \u204f constBits\u2032 bs\n\n `Maybe : T \u2192 T\n `Maybe A = `Bit `\u00d7 A\n\n : \u2200 {A} \u2192 A `\u2192 `Maybe A\n = <0,>\n\n : \u2200 {A} \u2192 A `\u2192 `Maybe A\n = <1,>\n\n : \u2200 {A B C} \u2192 (f : A `\u00d7 B `\u2192 C) (g : B `\u2192 C) \u2192 `Maybe A `\u00d7 B `\u2192 C\n = \n\n _\u2223?_ : \u2200 {A} \u2192 `Maybe A `\u00d7 `Maybe A `\u2192 `Maybe A\n _\u2223?_ = \u2236 id >\n\n _`\u2192?_ : T \u2192 T \u2192 Set\n A `\u2192? B = A `\u2192 `Maybe B\n\n search : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 (`Bits n `\u2192 A) \u2192 `\u22a4 `\u2192 A\n search {zero} _ f = <[]> \u204f f\n search {suc n} op f = ) , search op (f \u2218 <1\u2237>) > \u204f op\n\n find? : \u2200 {n A} \u2192 (`Bits n `\u2192? A) \u2192 `\u22a4 `\u2192? A\n find? = search _\u2223?_\n\n findB : \u2200 {n} \u2192 (`Bits n `\u2192 `Bit) \u2192 `\u22a4 `\u2192? `Bits n\n findB pred = find? else >\n\n fromTree : \u2200 {n A} \u2192 Tree (`\u22a4 `\u2192 A) n \u2192 `Bits n `\u2192 A\n fromTree (Tree.leaf x) = tt \u204f x\n fromTree (Tree.fork t\u2080 t\u2081) = uncons \u204f fork (fromTree t\u2080) (fromTree t\u2081)\n\n fromFun : \u2200 {n A} \u2192 (Bits n \u2192 `\u22a4 `\u2192 A) \u2192 `Bits n `\u2192 A\n fromFun = fromTree \u2218\u2032 Tree.fromFun\n\n fromBitsFun : \u2200 {i o} \u2192 (i \u2192\u1d47 o) \u2192 i `\u2192\u1d47 o\n fromBitsFun f = fromFun (constBits \u2218\u2032 f)\n\n : `Bit `\u00d7 `Bit `\u2192 `Bit\n = fork id not\n\n : `Bit `\u00d7 `Bit `\u2192 `Bit\n = fork id <1b>\n\n : `Bit `\u00d7 `Bit `\u2192 `Bit\n = fork <0b> id\n\n <==\u1d47> : `Bit `\u00d7 `Bit `\u2192 `Bit\n <==\u1d47> = \u204f not\n\n <==> : \u2200 {n} \u2192 `Bits n `\u00d7 `Bits n `\u2192 `Bit\n <==> {zero} = <1b>\n <==> {suc n} = < uncons \u00d7 uncons > \u204f < <==\u1d47> `zip` <==> {n} > \u204f \n\n <\u2295> : \u2200 {n} \u2192 `Bits n `\u00d7 `Bits n `\u2192 `Bits n\n <\u2295> = zipWith \n\n -- vnot : \u2200 {n} \u2192 `Endo (`Bits n)\n -- vnot = map not\n\n allBits : \u2200 n \u2192 `\u22a4 `\u2192 `Vec (`Bits n) (2^ n)\n allBits zero = < <[]> \u2237[]>\n allBits (suc n) = < bs \u204f map <0\u2237> ++ bs \u204f map <1\u2237> >\n where bs = allBits n\n\n sucBCarry : \u2200 {n} \u2192 `Bits n `\u2192 `Bits (1 + n)\n sucBCarry {zero} = < <0b> \u2237[]>\n sucBCarry {suc n} = uncons\n \u204f fork <0\u2237 sucBCarry >\n (sucBCarry \u204f uncons \u204f fork <0\u2237 <1\u2237> > <1\u2237 <0\u2237> >)\n\n sucB : \u2200 {n} \u2192 `Bits n `\u2192 `Bits n\n sucB = sucBCarry \u204f tail\n\n lookupTbl : \u2200 {n A} \u2192 `Bits n `\u00d7 `Vec A (2^ n) `\u2192 A\n lookupTbl {zero} = snd \u204f head\n lookupTbl {suc n}\n = first uncons\n \u204f assoc\n \u204f fork (second (take (2^ n)) \u204f lookupTbl)\n (second (drop (2^ n)) \u204f lookupTbl)\n\n funFromTbl : \u2200 {n A} \u2192 Vec (`\u22a4 `\u2192 A) (2^ n) \u2192 (`Bits n `\u2192 A)\n funFromTbl {zero} (x \u2237 []) = tt \u204f x\n funFromTbl {suc n} tbl\n = uncons \u204f fork (funFromTbl (V.take (2^ n) tbl))\n (funFromTbl (V.drop (2^ n) tbl))\n\n tblFromFun : \u2200 {n A} \u2192 (`Bits n `\u2192 A) \u2192 `\u22a4 `\u2192 `Vec A (2^ n)\n tblFromFun {zero} f = < <[]> \u204f f \u2237[]>\n tblFromFun {suc n} f = < tblFromFun (<0\u2237> \u204f f) ++\n tblFromFun (<1\u2237> \u204f f) >\n\n module WithFin\n (`Fin : \u2115 \u2192 T)\n (fz : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Fin (suc n))\n (fs : \u2200 {n} \u2192 `Fin n `\u2192 `Fin (suc n))\n (elim-Fin0 : \u2200 {A} \u2192 `Fin 0 `\u2192 A)\n (elim-Fin1+ : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Fin n `\u00d7 A `\u2192 B) \u2192 `Fin (suc n) `\u00d7 A `\u2192 B) where\n\n tabulate : \u2200 {n A _B} \u2192 (`Fin n `\u2192 A) \u2192 _B `\u2192 `Vec A n\n tabulate {zero} f = <[]>\n tabulate {suc n} f = \n\n lookup : \u2200 {n A} \u2192 `Fin n `\u00d7 `Vec A n `\u2192 A\n lookup {zero} = fst \u204f elim-Fin0\n lookup {suc n} = elim-Fin1+ head (second tail \u204f lookup)\n\n allFin : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Vec (`Fin n) n\n allFin = tabulate id\n\nmodule Defaults {t} {T : Set t} (funU : FunUniverse T) where\n open FunUniverse funU\n open Defaults\u27e8first-part\u27e9 funU public\n\n module RewiringDefaults\n (linRewiring : LinRewiring funU)\n (tt : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `\u22a4)\n (dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A)\n (rewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i `\u2192\u1d47 o) where\n\n open LinRewiring linRewiring\n open DefaultsGroup1 _\u2218_ tt snd dup swap first public\n <[]> : \u2200 {_\u22a4 A} \u2192 _\u22a4 `\u2192 `Vec A 0\n <[]> = tt \u204f tt\u2192[]\n open DefaultRewireTbl rewire public\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"06c0a79346fc5f1d818b819a394fd296aac8ce52","subject":"Refine logical relation for integers","message":"Refine logical relation for integers\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/FunBigStepSILR2.agda","new_file":"Thesis\/FunBigStepSILR2.agda","new_contents":"-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\" (ISAC below), POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\n-- CHEATS:\n-- \"Fuctional big-step semantics\" requires an external termination proof for the\n-- semantics. There it is also mechanized, here it isn't. Worse, the same\n-- termination problem affects some lemmas about the semantics.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen import Data.Nat.Properties.Simple\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n\n-- WARNING: ISAC's big-step semantics produces a step count as \"output\". But\n-- that would not help Agda establish termination. That's only a problem for a\n-- functional big-step semantics, not for a relational semantics.\n--\n-- So, instead, I tried to use a sort of writer monad: the interpreter gets fuel\n-- and returns the remaining fuel. That's the same trick as in \"functional\n-- big-step semantics\". That *makes* the function terminating, even though Agda\n-- cannot see this because it does not know that the returned fuel is no bigger.\n\n-- Since we focus for now on STLC, unlike that\n-- paper, we could avoid error values because we keep types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) n = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\neval (app s t) \u03c1 (suc n) = (eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)) n\n\neval-const-dec : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-const-dec (lit v) n0 .n0 refl = \u2264-refl\n\n{-# TERMINATING #-}\neval-dec : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-dec (const c) \u03c1 v n0 n1 eq = eval-const-dec c n0 n1 eq\neval-dec (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (app s t) \u03c1 v zero n1 ()\neval-dec (app s t) \u03c1 v (suc n0) n3 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv sn1 | [ seq ] with eval t \u03c1 sn1 | inspect (eval t \u03c1) sn1\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv@(closure st s\u03c1) sn1 | [ seq ] | (Done tv tn2) | [ teq ] = \u2264-step (\u2264-trans (\u2264-trans (eval-dec st _ _ _ _ eq) (eval-dec t _ _ _ _ teq)) (eval-dec s _ _ _ _ seq))\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | Error | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | TimeOut | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Error | [ seq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | TimeOut | [ seq ]\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1)\neval-const-mono (lit v) n0 .n0 refl = refl\n\n-- ARGH\n{-# TERMINATING #-}\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1)\neval-mono (const c) \u03c1 v n0 n1 eq = eval-const-mono c n0 n1 eq\neval-mono (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\neval-mono (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\neval-mono (app s t) \u03c1 v zero n1 ()\neval-mono (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-mono t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n\neval-adjust-plus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1)\neval-adjust-plus zero t \u03c1 v n0 n1 eq = eq\neval-adjust-plus (suc d) t \u03c1 v n0 n1 eq = eval-mono t \u03c1 v (d + n0) (d + n1) (eval-adjust-plus d t \u03c1 v n0 n1 eq)\n\neval-const-strengthen : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1) \u2192 evalConst c n0 \u2261 Done v n1\neval-const-strengthen (lit v) n0 .n0 refl = refl\n\n-- {-# TERMINATING #-}\n-- eval-strengthen : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-strengthen (const c) \u03c1 v n0 n1 eq = eval-const-strengthen c n0 n1 eq\n-- eval-strengthen (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq = {!!}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 (suc n0) | inspect (eval s \u03c1) (suc n0)\n-- -- eval-strengthen (app s t) \u03c1 v\u2081 (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 n0 = {!eval-strengthen s \u03c1 v n0 n1 seq !}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-strengthen t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | Error | [ seq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | TimeOut | [ seq ] = {!!}\n\n-- eval-adjust-minus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 {\u03c1 v} n0 n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-adjust-minus zero t n0 n1 eq = eq\n-- eval-adjust-minus (suc d) t n0 n1 eq = eval-adjust-minus d t n0 n1 (eval-strengthen t _ _ (d + n0) (d + n1) eq)\n\nmutual\n -- This is not the same definition of relT, but it is equivalent.\n relT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\n -- This equation is a lemma in the original definition.\n relT {\u03c4} t1 t2 \u03c11 \u03c12 zero = \u22a4\n -- To compare this definition, note that the original k is suc n here.\n relT {\u03c4} t1 t2 \u03c11 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n -- Originally we have 0 \u2264 j < k, so j < suc n, so k - j = suc n - j.\n -- It follows that 0 < k - j \u2264 k, hence suc n - j \u2264 suc n, or n - j \u2264 n.\n -- Here, instead of binding j we bind n-j = n - j, require n - j \u2264 n, and\n -- use suc n-j instead of k - j.\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n -- The next assumption is important. This still says that evaluation consumes j steps.\n -- Since j \u2264 n, it is OK to start evaluation with n steps.\n -- Starting with (suc n) and getting suc n-j is equivalent, per eval-mono\n -- and eval-strengthen. But in practice this version is easier to use.\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 0 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n -- Here, computing t2 is allowed to take an unbounded number of steps. Having to write a number at all is annoying.\n\n relV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n relV nat (intV v1) (intV v2) n = v1 \u2261 v2\n relV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) n =\n \u2200 (k : \u2115) (k\u2264n : k < n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\n -- Here, in the conclusion, I'm not relating app (closure t1 \u03c11) v1 with app\n -- (closure t2 \u03c12) v2 (or some encoding of that that actually works), but the\n -- result of taking a step from that configuration. That is important, because\n -- both Pitts' \"Step-Indexed Biorthogonality: a Tutorial Example\" and\n -- \"Imperative Self-Adjusting Computation\" do the same thing (and point out it's\n -- important).\n\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono m n m\u2264n nat (intV v1) (intV v2) vv = vv\nrelV-mono m n m\u2264n (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) ff k k\u2264m = ff k (\u2264-trans k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 .n n-j\u2264n refl = v2 , zero , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\nlt1 : \u2200 {k n} \u2192 k < n \u2192 k \u2264 n\nlt1 (s\u2264s p) = \u2264-step p\n\n-- relV-apply : \u2200 {\u03c3 \u03c4 \u0393} (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t v1 \u03c12 n-j\n-- n1 sv1 sv2\n-- (svv : relV (\u03c3 \u21d2 \u03c4) sv1 sv2 n1)\n-- n2 tv1 tv2\n-- (tvv : relV \u03c3 tv1 tv2 (suc (suc n2)))\n-- (eq : apply sv1 tv1 n2 \u2261 Done v1 n-j) \u2192\n-- -- (tvv)\n-- -- (eqv1)\n-- \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval (app s t) \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n-- relV-apply s t v1 \u03c1 n-j n1 (closure st \u03c11) (closure st2 \u03c12) svv n2 tv1 tv2 tvv eq = {! !}\n-- -- relV-apply s t v1 \u03c12 n-j _ _ sv2 svv _ tv1 tv2 tvv eq\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\nfundamental (const (lit v)) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(intV v) .n n-j\u2264n refl = intV v , zero , refl , refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(closure t \u03c11) .n n-j\u2264n refl = closure t \u03c12 , zero , refl , (\u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k (suc n) (lt1 k\u2264n) _ _ _ \u03c1\u03c1))\nfundamental (app s t) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq with eval s \u03c11 n | inspect (eval s \u03c11) n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done sv1 n1 | [ s1eq ] with eval-dec s _ _ n n1 s1eq | eval t \u03c11 n1 | inspect (eval t \u03c11) n1\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done (closure st1 s\u03c11) n1 | [ s1eq ] | n1\u2264n | Done tv1 n2 | [ t1eq ] with eval-dec t _ _ n1 n2 t1eq\n... | n2\u2264n1 with fundamental s (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) (suc n1) (s\u2264s n1\u2264n) (eval-mono s \u03c11 (closure st1 s\u03c11) n n1 s1eq)\n | fundamental t (suc (suc n1)) \u03c11 \u03c12 (rel\u03c1-mono (suc (suc n1)) (suc (suc n)) (s\u2264s (s\u2264s n1\u2264n)) _ _ _ \u03c1\u03c1) tv1 (suc n2) (s\u2264s n2\u2264n1) (eval-mono t \u03c11 tv1 n1 n2 t1eq)\n... | sv2@(closure st2 s\u03c12) , sn3 , s2eq , svv | tv2 , tn3 , t2eq , tvv with svv (suc n2) (s\u2264s (s\u2264s n2\u2264n1)) tv1 tv2 (relV-mono _ _ (s\u2264s (n\u22641+n n2)) _ _ _ tvv ) v1 n-j (eval-dec st1 _ _ _ _ eq) eq\n... | v2 , n3 , eq2 , vv = v2 , suc (sn3 + (tn3 + n3)) , comp , vv\n where\n s2eq-adj : eval s \u03c12 (sn3 + (tn3 + n3)) \u2261 Done (closure st2 s\u03c12) (tn3 + n3)\n s2eq-adj rewrite +-comm sn3 (tn3 + n3)| cong (Done (closure st2 s\u03c12)) (sym (+-right-identity (tn3 + n3))) = eval-adjust-plus (tn3 + n3) s _ sv2 _ _ s2eq\n t2eq-adj : eval t \u03c12 (tn3 + n3) \u2261 Done tv2 n3\n t2eq-adj rewrite +-comm tn3 n3 | cong (Done tv2) (sym (+-right-identity n3)) = eval-adjust-plus n3 t _ tv2 _ _ t2eq\n\n comp : (eval s \u03c12 >>= (\u03bb sv \u2192 eval t \u03c12 >>= apply sv))\n (sn3 + (tn3 + n3))\n \u2261 Done v2 0\n comp rewrite s2eq-adj | t2eq-adj = eq2\n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | Error | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | TimeOut | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Error | [ s1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | TimeOut | [ s1eq ]\n","old_contents":"-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\" (ISAC below), POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\n-- CHEATS:\n-- \"Fuctional big-step semantics\" requires an external termination proof for the\n-- semantics. There it is also mechanized, here it isn't. Worse, the same\n-- termination problem affects some lemmas about the semantics.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen import Data.Nat.Properties.Simple\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n\n-- WARNING: ISAC's big-step semantics produces a step count as \"output\". But\n-- that would not help Agda establish termination. That's only a problem for a\n-- functional big-step semantics, not for a relational semantics.\n--\n-- So, instead, I tried to use a sort of writer monad: the interpreter gets fuel\n-- and returns the remaining fuel. That's the same trick as in \"functional\n-- big-step semantics\". That *makes* the function terminating, even though Agda\n-- cannot see this because it does not know that the returned fuel is no bigger.\n\n-- Since we focus for now on STLC, unlike that\n-- paper, we could avoid error values because we keep types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) n = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\neval (app s t) \u03c1 (suc n) = (eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)) n\n\neval-const-dec : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-const-dec (lit v) n0 .n0 refl = \u2264-refl\n\n{-# TERMINATING #-}\neval-dec : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-dec (const c) \u03c1 v n0 n1 eq = eval-const-dec c n0 n1 eq\neval-dec (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (app s t) \u03c1 v zero n1 ()\neval-dec (app s t) \u03c1 v (suc n0) n3 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv sn1 | [ seq ] with eval t \u03c1 sn1 | inspect (eval t \u03c1) sn1\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv@(closure st s\u03c1) sn1 | [ seq ] | (Done tv tn2) | [ teq ] = \u2264-step (\u2264-trans (\u2264-trans (eval-dec st _ _ _ _ eq) (eval-dec t _ _ _ _ teq)) (eval-dec s _ _ _ _ seq))\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | Error | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | TimeOut | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Error | [ seq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | TimeOut | [ seq ]\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1)\neval-const-mono (lit v) n0 .n0 refl = refl\n\n-- ARGH\n{-# TERMINATING #-}\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1)\neval-mono (const c) \u03c1 v n0 n1 eq = eval-const-mono c n0 n1 eq\neval-mono (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\neval-mono (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\neval-mono (app s t) \u03c1 v zero n1 ()\neval-mono (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-mono t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n\neval-adjust-plus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1)\neval-adjust-plus zero t \u03c1 v n0 n1 eq = eq\neval-adjust-plus (suc d) t \u03c1 v n0 n1 eq = eval-mono t \u03c1 v (d + n0) (d + n1) (eval-adjust-plus d t \u03c1 v n0 n1 eq)\n\neval-const-strengthen : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1) \u2192 evalConst c n0 \u2261 Done v n1\neval-const-strengthen (lit v) n0 .n0 refl = refl\n\n-- {-# TERMINATING #-}\n-- eval-strengthen : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-strengthen (const c) \u03c1 v n0 n1 eq = eval-const-strengthen c n0 n1 eq\n-- eval-strengthen (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq = {!!}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 (suc n0) | inspect (eval s \u03c1) (suc n0)\n-- -- eval-strengthen (app s t) \u03c1 v\u2081 (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 n0 = {!eval-strengthen s \u03c1 v n0 n1 seq !}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-strengthen t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | Error | [ seq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | TimeOut | [ seq ] = {!!}\n\n-- eval-adjust-minus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 {\u03c1 v} n0 n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-adjust-minus zero t n0 n1 eq = eq\n-- eval-adjust-minus (suc d) t n0 n1 eq = eval-adjust-minus d t n0 n1 (eval-strengthen t _ _ (d + n0) (d + n1) eq)\n\nmutual\n -- This is not the same definition of relT, but it is equivalent.\n relT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\n -- This equation is a lemma in the original definition.\n relT {\u03c4} t1 t2 \u03c11 \u03c12 zero = \u22a4\n -- To compare this definition, note that the original k is suc n here.\n relT {\u03c4} t1 t2 \u03c11 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n -- Originally we have 0 \u2264 j < k, so j < suc n, so k - j = suc n - j.\n -- It follows that 0 < k - j \u2264 k, hence suc n - j \u2264 suc n, or n - j \u2264 n.\n -- Here, instead of binding j we bind n-j = n - j, require n - j \u2264 n, and\n -- use suc n-j instead of k - j.\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n -- The next assumption is important. This still says that evaluation consumes j steps.\n -- Since j \u2264 n, it is OK to start evaluation with n steps.\n -- Starting with (suc n) and getting suc n-j is equivalent, per eval-mono\n -- and eval-strengthen. But in practice this version is easier to use.\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 0 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n -- Here, computing t2 is allowed to take an unbounded number of steps. Having to write a number at all is annoying.\n\n relV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n relV nat v1 v2 n = v1 \u2261 v2\n relV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) n =\n \u2200 (k : \u2115) (k\u2264n : k < n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\n -- Here, in the conclusion, I'm not relating app (closure t1 \u03c11) v1 with app\n -- (closure t2 \u03c12) v2 (or some encoding of that that actually works), but the\n -- result of taking a step from that configuration. That is important, because\n -- both Pitts' \"Step-Indexed Biorthogonality: a Tutorial Example\" and\n -- \"Imperative Self-Adjusting Computation\" do the same thing (and point out it's\n -- important).\n\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono m n m\u2264n nat v1 v2 vv = vv\nrelV-mono m n m\u2264n (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) ff k k\u2264m = ff k (\u2264-trans k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 .n n-j\u2264n refl = v2 , zero , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\nlt1 : \u2200 {k n} \u2192 k < n \u2192 k \u2264 n\nlt1 (s\u2264s p) = \u2264-step p\n\n-- relV-apply : \u2200 {\u03c3 \u03c4 \u0393} (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t v1 \u03c12 n-j\n-- n1 sv1 sv2\n-- (svv : relV (\u03c3 \u21d2 \u03c4) sv1 sv2 n1)\n-- n2 tv1 tv2\n-- (tvv : relV \u03c3 tv1 tv2 (suc (suc n2)))\n-- (eq : apply sv1 tv1 n2 \u2261 Done v1 n-j) \u2192\n-- -- (tvv)\n-- -- (eqv1)\n-- \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval (app s t) \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n-- relV-apply s t v1 \u03c1 n-j n1 (closure st \u03c11) (closure st2 \u03c12) svv n2 tv1 tv2 tvv eq = {! !}\n-- -- relV-apply s t v1 \u03c12 n-j _ _ sv2 svv _ tv1 tv2 tvv eq\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\nfundamental (const (lit v)) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(intV v) .n n-j\u2264n refl = intV v , zero , refl , refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(closure t \u03c11) .n n-j\u2264n refl = closure t \u03c12 , zero , refl , (\u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k (suc n) (lt1 k\u2264n) _ _ _ \u03c1\u03c1))\nfundamental (app s t) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq with eval s \u03c11 n | inspect (eval s \u03c11) n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done sv1 n1 | [ s1eq ] with eval-dec s _ _ n n1 s1eq | eval t \u03c11 n1 | inspect (eval t \u03c11) n1\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done (closure st1 s\u03c11) n1 | [ s1eq ] | n1\u2264n | Done tv1 n2 | [ t1eq ] with eval-dec t _ _ n1 n2 t1eq\n... | n2\u2264n1 with fundamental s (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) (suc n1) (s\u2264s n1\u2264n) (eval-mono s \u03c11 (closure st1 s\u03c11) n n1 s1eq)\n | fundamental t (suc (suc n1)) \u03c11 \u03c12 (rel\u03c1-mono (suc (suc n1)) (suc (suc n)) (s\u2264s (s\u2264s n1\u2264n)) _ _ _ \u03c1\u03c1) tv1 (suc n2) (s\u2264s n2\u2264n1) (eval-mono t \u03c11 tv1 n1 n2 t1eq)\n... | sv2@(closure st2 s\u03c12) , sn3 , s2eq , svv | tv2 , tn3 , t2eq , tvv with svv (suc n2) (s\u2264s (s\u2264s n2\u2264n1)) tv1 tv2 (relV-mono _ _ (s\u2264s (n\u22641+n n2)) _ _ _ tvv ) v1 n-j (eval-dec st1 _ _ _ _ eq) eq\n... | v2 , n3 , eq2 , vv = v2 , suc (sn3 + (tn3 + n3)) , comp , vv\n where\n s2eq-adj : eval s \u03c12 (sn3 + (tn3 + n3)) \u2261 Done (closure st2 s\u03c12) (tn3 + n3)\n s2eq-adj rewrite +-comm sn3 (tn3 + n3)| cong (Done (closure st2 s\u03c12)) (sym (+-right-identity (tn3 + n3))) = eval-adjust-plus (tn3 + n3) s _ sv2 _ _ s2eq\n t2eq-adj : eval t \u03c12 (tn3 + n3) \u2261 Done tv2 n3\n t2eq-adj rewrite +-comm tn3 n3 | cong (Done tv2) (sym (+-right-identity n3)) = eval-adjust-plus n3 t _ tv2 _ _ t2eq\n\n comp : (eval s \u03c12 >>= (\u03bb sv \u2192 eval t \u03c12 >>= apply sv))\n (sn3 + (tn3 + n3))\n \u2261 Done v2 0\n comp rewrite s2eq-adj | t2eq-adj = eq2\n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | Error | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | TimeOut | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Error | [ s1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | TimeOut | [ s1eq ]\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"8d00761c933d85cbaf7c91e2a9f2d3a00108afb9","subject":"game.agda","message":"game.agda\n","repos":"crypto-agda\/crypto-agda","old_file":"game.agda","new_file":"game.agda","new_contents":"module game where\n\nopen import Function.NP\nopen import Data.Nat.NP\nopen import Data.Product\nopen import Data.Bool -- hiding (_\u225f_)\n\n{-\n-- Game\ndata \u2141 ...\n-}\n\ndata Strategy (\u2610_ : Set \u2192 Set) (R : Set) : Set\u2081 where\n say : R \u2192 Strategy \u2610_ R\n ask : {A : Set} \u2192 \u2610 A \u2192 (A \u2192 Strategy \u2610_ R) \u2192 Strategy \u2610_ R\n\nrun : \u2200 {R} \u2192 Strategy id R \u2192 R\nrun (say r) = r\nrun (ask a f) = run (f a)\n\nopen import Data.Bits\n\npattern looks-random = 0b\npattern not-random = 1b\n\nmodule CryptoGames where\n\n module CipherGames (M C : Set) where\n SemSecAdv : Set\n SemSecAdv = C \u2192 Bit\n\n CPAAdv : Set\u2081\n CPAAdv = \u2200 {\u2610_} (enc : M \u2192 \u2610 C) \u2192 Strategy \u2610_ Bit\n\n data CPAWinner (enc : M \u2192 C) b : Strategy id Bit \u2192 Set\u2081 where\n win : CPAWinner enc b (say b)\n ask : \u2200 {m f} \u2192 CPAWinner enc b (f (enc m)) \u2192 CPAWinner enc b (ask (enc m) f)\n\n module MACGames (M T : Set) where\n MacAdv : Set\u2081\n MacAdv = \u2200 {\u2610_} (mac : M \u2192 \u2610 T) \u2192 Strategy \u2610_ (M \u00d7 T)\n\n data MACWinner (mac : M \u2192 T) : Strategy id (M \u00d7 T) \u2192 Set\u2081 where\n win : \u2200 {m} \u2192 MACWinner mac (say (m , mac m))\n ask : \u2200 {m f} \u2192 MACWinner mac (f (mac m)) \u2192 MACWinner mac (ask (mac m) f)\n","old_contents":"module game where\n\nopen import Function.NP\nopen import Data.Nat.NP\nopen import Data.Product\nopen import Data.Bool -- hiding (_\u225f_)\n\n{-\n-- Game\ndata \u2141 ...\n-}\n\ndata Strategy (\u2610_ : Set \u2192 Set) (R : Set) : Set\u2081 where\n say : R \u2192 Strategy \u2610_ R\n ask : {A : Set} \u2192 \u2610 A \u2192 (A \u2192 Strategy \u2610_ R) \u2192 Strategy \u2610_ R\n\nrun : \u2200 {R} \u2192 Strategy id R \u2192 R\nrun (say r) = r\nrun (ask a f) = run (f a)\n\nopen import Data.Bits\n\npattern looks-random = 0b\npattern not-random = 1b\n\nmodule CryptoGames where\n\n module CipherGames (M C : Set) where\n SemSecAdv : Set\n SemSecAdv = C \u2192 Bit\n\n CPAAdv : Set\n CPAAdv = \u2200 {\u2610_} (enc : M \u2192 \u2610 C) \u2192 Strategy \u2610_ Bit\n\n data CPAWinner b : Strategy \u2610_ Bit \u2192 Set where\n win : CPAWinner b (say b)\n ask : CPAWinner b s \u2192 CPAWinner b (ask ? (\u03bb ? \u2192 ))\n\n module MACGames (M T : Set) where\n MacAdv : Set\n MacAdv = \u2200 {\u2610_} (mac : M \u2192 \u2610 T) \u2192 Strategy \u2610_ (M \u00d7 T)\n\n data MACWinner (mac : M \u2192 T) : Strategy id (M \u00d7 T) \u2192 Set where\n win : \u2200 {m} \u2192 MACWinner mac (say (m , mac m))\n ask : \u2200 {m f} \u2192 MACWinner mac (f (mac m)) \u2192 MACWinner mac (ask (mac m) f)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"e2d49c81f464ab9449818f964e0078660f4315da","subject":"Agda: add lemma about syntactic contexts","message":"Agda: add lemma about syntactic contexts\n\nOld-commit-hash: 3613bba9a32778eaaa8f9d22419c23a8505be346\n","repos":"inc-lc\/ilc-agda","old_file":"Syntactic\/Contexts.agda","new_file":"Syntactic\/Contexts.agda","new_contents":"module Syntactic.Contexts\n (Type : Set)\n where\n\n-- CONTEXTS\n--\n-- This module defines the syntax of contexts, prefixes of\n-- contexts and variables and properties of these notions.\n--\n-- This module is parametric in the syntax of types, so it\n-- can be reused for different calculi.\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\n\n-- TYPING CONTEXTS\n\n-- Syntax\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n-- Specialized congruence rules\n\n\u27e8\u2205\u27e9 : \u2205 \u2261 \u2205\n\u27e8\u2205\u27e9 = refl\n\n_\u27e8\u2022\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} \u2192 \u03c4\u2081 \u2261 \u03c4\u2082 \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u03c4\u2081 \u2022 \u0393\u2081 \u2261 \u03c4\u2082 \u2022 \u0393\u2082\n_\u27e8\u2022\u27e9_ = cong\u2082 _\u2022_\n\n-- VARIABLES\n\n-- Syntax\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\n-- WEAKENING\n\n-- CONTEXT PREFIXES\n--\n-- Useful for making lemmas strong enough to prove by induction.\n--\n-- Consider using the Subcontexts module instead.\n\nmodule Prefixes where\n\n-- Prefix of a context\n\n data Prefix : Context \u2192 Set where\n \u2205 : \u2200 {\u0393} \u2192 Prefix \u0393\n _\u2022_ : \u2200 {\u0393} \u2192 (\u03c4 : Type) \u2192 Prefix \u0393 \u2192 Prefix (\u03c4 \u2022 \u0393)\n\n-- take only the prefix of a context\n\n take : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\n take \u0393 \u2205 = \u2205\n take (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) = \u03c4 \u2022 take \u0393 \u0393\u2032\n\n-- drop the prefix of a context\n\n drop : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\n drop \u0393 \u2205 = \u0393\n drop (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) = drop \u0393 \u0393\u2032\n\n-- Extend a context to a super context\n\n infixr 10 _\u22ce_\n\n _\u22ce_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Context\n \u2205 \u22ce \u0393\u2082 = \u0393\u2082\n (\u03c4 \u2022 \u0393\u2081) \u22ce \u0393\u2082 = \u03c4 \u2022 (\u0393\u2081 \u22ce \u0393\u2082)\n\n take-drop : \u2200 \u0393 \u0393\u2032 \u2192 take \u0393 \u0393\u2032 \u22ce drop \u0393 \u0393\u2032 \u2261 \u0393\n take-drop \u2205 \u2205 = refl\n take-drop (\u03c4 \u2022 \u0393) \u2205 = refl\n take-drop (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) rewrite take-drop \u0393 \u0393\u2032 = refl\n\n or-unit : \u2200 \u0393 \u2192 \u0393 \u22ce \u2205 \u2261 \u0393\n or-unit \u2205 = refl\n or-unit (\u03c4 \u2022 \u0393) rewrite or-unit \u0393 = refl\n\n move-prefix : \u2200 \u0393 \u03c4 \u0393\u2032 \u2192\n \u0393 \u22ce (\u03c4 \u2022 \u0393\u2032) \u2261 (\u0393 \u22ce (\u03c4 \u2022 \u2205)) \u22ce \u0393\u2032\n move-prefix \u2205 \u03c4 \u0393\u2032 = refl\n move-prefix (\u03c4 \u2022 \u0393) \u03c4\u2081 \u2205 = sym (or-unit (\u03c4 \u2022 \u0393 \u22ce (\u03c4\u2081 \u2022 \u2205)))\n move-prefix (\u03c4 \u2022 \u0393) \u03c4\u2081 (\u03c4\u2082 \u2022 \u0393\u2032) rewrite move-prefix \u0393 \u03c4\u2081 (\u03c4\u2082 \u2022 \u0393\u2032) = refl\n\n-- Lift a variable to a super context\n\n lift : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\n lift {\u2205} {\u2205} x = x\n lift {\u2205} {\u03c4 \u2022 \u0393\u2082} x = that (lift {\u2205} {\u0393\u2082} x)\n lift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} this = this\n lift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} (that x) = that (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- SUBCONTEXTS\n--\n-- Useful as a reified weakening operation,\n-- and for making theorems strong enough to prove by induction.\n--\n-- The contents of this module are currently exported at the end\n-- of this file.\n\nmodule Subcontexts where\n infix 8 _\u227c_\n\n data _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u03c4 : Type) \u2192\n \u0393\u2081 \u227c \u0393\u2082 \u2192\n \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u03c4 : Type) \u2192\n \u0393\u2081 \u227c \u0393\u2082 \u2192\n \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n -- Properties\n\n \u2205\u227c\u0393 : \u2200 {\u0393} \u2192 \u2205 \u227c \u0393\n \u2205\u227c\u0393 {\u2205} = \u2205\n \u2205\u227c\u0393 {\u03c4 \u2022 \u0393} = drop \u03c4 \u2022 \u2205\u227c\u0393\n\n \u227c-refl : Reflexive _\u227c_\n \u227c-refl {\u2205} = \u2205\n \u227c-refl {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u227c-refl\n\n \u227c-reflexive : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u0393\u2081 \u227c \u0393\u2082\n \u227c-reflexive refl = \u227c-refl\n\n \u227c-trans : Transitive _\u227c_\n \u227c-trans \u227c\u2081 \u2205 = \u227c\u2081\n \u227c-trans (keep .\u03c4 \u2022 \u227c\u2081) (keep \u03c4 \u2022 \u227c\u2082) = keep \u03c4 \u2022 \u227c-trans \u227c\u2081 \u227c\u2082\n \u227c-trans (drop .\u03c4 \u2022 \u227c\u2081) (keep \u03c4 \u2022 \u227c\u2082) = drop \u03c4 \u2022 \u227c-trans \u227c\u2081 \u227c\u2082\n \u227c-trans \u227c\u2081 (drop \u03c4 \u2022 \u227c\u2082) = drop \u03c4 \u2022 \u227c-trans \u227c\u2081 \u227c\u2082\n\n \u227c-isPreorder : IsPreorder _\u2261_ _\u227c_\n \u227c-isPreorder = record\n { isEquivalence = isEquivalence\n ; reflexive = \u227c-reflexive\n ; trans = \u227c-trans\n }\n\n \u227c-preorder : Preorder _ _ _\n \u227c-preorder = record\n { Carrier = Context\n ; _\u2248_ = _\u2261_\n ; _\u223c_ = _\u227c_\n ; isPreorder = \u227c-isPreorder\n }\n\n module \u227c-Reasoning where\n open import Relation.Binary.PreorderReasoning \u227c-preorder public\n renaming\n ( _\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_\n ; _\u223c\u27e8_\u27e9_ to _\u227c\u27e8_\u27e9_\n ; _\u2248\u27e8\u27e9_ to _\u2261\u27e8\u27e9_\n )\n\n -- Lift a variable to a super context\n\n lift : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\n lift (keep \u03c4 \u2022 \u227c\u2081) this = this\n lift (keep \u03c4 \u2022 \u227c\u2081) (that x) = that (lift \u227c\u2081 x)\n lift (drop \u03c4 \u2022 \u227c\u2081) x = that (lift \u227c\u2081 x)\n\n-- Currently, we export the subcontext relation as well as the\n-- definition of _\u22ce_.\n\nopen Subcontexts public\nopen Prefixes public using (_\u22ce_)\n","old_contents":"module Syntactic.Contexts\n (Type : Set)\n where\n\n-- CONTEXTS\n--\n-- This module defines the syntax of contexts, prefixes of\n-- contexts and variables and properties of these notions.\n--\n-- This module is parametric in the syntax of types, so it\n-- can be reused for different calculi.\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\n\n-- TYPING CONTEXTS\n\n-- Syntax\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n-- Specialized congruence rules\n\n\u27e8\u2205\u27e9 : \u2205 \u2261 \u2205\n\u27e8\u2205\u27e9 = refl\n\n_\u27e8\u2022\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} \u2192 \u03c4\u2081 \u2261 \u03c4\u2082 \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u03c4\u2081 \u2022 \u0393\u2081 \u2261 \u03c4\u2082 \u2022 \u0393\u2082\n_\u27e8\u2022\u27e9_ = cong\u2082 _\u2022_\n\n-- VARIABLES\n\n-- Syntax\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\n-- WEAKENING\n\n-- CONTEXT PREFIXES\n--\n-- Useful for making lemmas strong enough to prove by induction.\n--\n-- Consider using the Subcontexts module instead.\n\nmodule Prefixes where\n\n-- Prefix of a context\n\n data Prefix : Context \u2192 Set where\n \u2205 : \u2200 {\u0393} \u2192 Prefix \u0393\n _\u2022_ : \u2200 {\u0393} \u2192 (\u03c4 : Type) \u2192 Prefix \u0393 \u2192 Prefix (\u03c4 \u2022 \u0393)\n\n-- take only the prefix of a context\n\n take : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\n take \u0393 \u2205 = \u2205\n take (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) = \u03c4 \u2022 take \u0393 \u0393\u2032\n\n-- drop the prefix of a context\n\n drop : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\n drop \u0393 \u2205 = \u0393\n drop (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) = drop \u0393 \u0393\u2032\n\n-- Extend a context to a super context\n\n infixr 10 _\u22ce_\n\n _\u22ce_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Context\n \u2205 \u22ce \u0393\u2082 = \u0393\u2082\n (\u03c4 \u2022 \u0393\u2081) \u22ce \u0393\u2082 = \u03c4 \u2022 (\u0393\u2081 \u22ce \u0393\u2082)\n\n take-drop : \u2200 \u0393 \u0393\u2032 \u2192 take \u0393 \u0393\u2032 \u22ce drop \u0393 \u0393\u2032 \u2261 \u0393\n take-drop \u2205 \u2205 = refl\n take-drop (\u03c4 \u2022 \u0393) \u2205 = refl\n take-drop (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) rewrite take-drop \u0393 \u0393\u2032 = refl\n\n or-unit : \u2200 \u0393 \u2192 \u0393 \u22ce \u2205 \u2261 \u0393\n or-unit \u2205 = refl\n or-unit (\u03c4 \u2022 \u0393) rewrite or-unit \u0393 = refl\n\n move-prefix : \u2200 \u0393 \u03c4 \u0393\u2032 \u2192\n \u0393 \u22ce (\u03c4 \u2022 \u0393\u2032) \u2261 (\u0393 \u22ce (\u03c4 \u2022 \u2205)) \u22ce \u0393\u2032\n move-prefix \u2205 \u03c4 \u0393\u2032 = refl\n move-prefix (\u03c4 \u2022 \u0393) \u03c4\u2081 \u2205 = sym (or-unit (\u03c4 \u2022 \u0393 \u22ce (\u03c4\u2081 \u2022 \u2205)))\n move-prefix (\u03c4 \u2022 \u0393) \u03c4\u2081 (\u03c4\u2082 \u2022 \u0393\u2032) rewrite move-prefix \u0393 \u03c4\u2081 (\u03c4\u2082 \u2022 \u0393\u2032) = refl\n\n-- Lift a variable to a super context\n\n lift : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\n lift {\u2205} {\u2205} x = x\n lift {\u2205} {\u03c4 \u2022 \u0393\u2082} x = that (lift {\u2205} {\u0393\u2082} x)\n lift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} this = this\n lift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} (that x) = that (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- SUBCONTEXTS\n--\n-- Useful as a reified weakening operation,\n-- and for making theorems strong enough to prove by induction.\n--\n-- The contents of this module are currently exported at the end\n-- of this file.\n\nmodule Subcontexts where\n infix 8 _\u227c_\n\n data _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u03c4 : Type) \u2192\n \u0393\u2081 \u227c \u0393\u2082 \u2192\n \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u03c4 : Type) \u2192\n \u0393\u2081 \u227c \u0393\u2082 \u2192\n \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n -- Properties\n\n \u227c-refl : Reflexive _\u227c_\n \u227c-refl {\u2205} = \u2205\n \u227c-refl {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u227c-refl\n\n \u227c-reflexive : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u0393\u2081 \u227c \u0393\u2082\n \u227c-reflexive refl = \u227c-refl\n\n \u227c-trans : Transitive _\u227c_\n \u227c-trans \u227c\u2081 \u2205 = \u227c\u2081\n \u227c-trans (keep .\u03c4 \u2022 \u227c\u2081) (keep \u03c4 \u2022 \u227c\u2082) = keep \u03c4 \u2022 \u227c-trans \u227c\u2081 \u227c\u2082\n \u227c-trans (drop .\u03c4 \u2022 \u227c\u2081) (keep \u03c4 \u2022 \u227c\u2082) = drop \u03c4 \u2022 \u227c-trans \u227c\u2081 \u227c\u2082\n \u227c-trans \u227c\u2081 (drop \u03c4 \u2022 \u227c\u2082) = drop \u03c4 \u2022 \u227c-trans \u227c\u2081 \u227c\u2082\n\n \u227c-isPreorder : IsPreorder _\u2261_ _\u227c_\n \u227c-isPreorder = record\n { isEquivalence = isEquivalence\n ; reflexive = \u227c-reflexive\n ; trans = \u227c-trans\n }\n\n \u227c-preorder : Preorder _ _ _\n \u227c-preorder = record\n { Carrier = Context\n ; _\u2248_ = _\u2261_\n ; _\u223c_ = _\u227c_\n ; isPreorder = \u227c-isPreorder\n }\n\n module \u227c-Reasoning where\n open import Relation.Binary.PreorderReasoning \u227c-preorder public\n renaming\n ( _\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_\n ; _\u223c\u27e8_\u27e9_ to _\u227c\u27e8_\u27e9_\n ; _\u2248\u27e8\u27e9_ to _\u2261\u27e8\u27e9_\n )\n\n -- Lift a variable to a super context\n\n lift : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\n lift (keep \u03c4 \u2022 \u227c\u2081) this = this\n lift (keep \u03c4 \u2022 \u227c\u2081) (that x) = that (lift \u227c\u2081 x)\n lift (drop \u03c4 \u2022 \u227c\u2081) x = that (lift \u227c\u2081 x)\n\n-- Currently, we export the subcontext relation as well as the\n-- definition of _\u22ce_.\n\nopen Subcontexts public\nopen Prefixes public using (_\u22ce_)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"2c88234563182184ea1c4db69a4b3fc0742b0371","subject":"PropEq: reexport \"reflexive\"","message":"PropEq: reexport \"reflexive\"\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Relation\/Binary\/PropositionalEquality\/NP.agda","new_file":"lib\/Relation\/Binary\/PropositionalEquality\/NP.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n-- move this to propeq\nmodule Relation.Binary.PropositionalEquality.NP where\n\nopen import Relation.Binary.PropositionalEquality public hiding (module \u2261-Reasoning)\nopen import Relation.Binary.NP\nopen import Relation.Binary.Bijection\nopen import Relation.Binary.Logical\nopen import Relation.Nullary\n\nprivate\n module Dummy {a} {A : Set a} where\n open IsEquivalence (isEquivalence {a} {A}) public hiding (refl; sym; trans)\nopen Dummy public\n\ncong\u2083 : \u2200 {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d}\n (f : A \u2192 B \u2192 C \u2192 D) {a\u2081 a\u2082 b\u2081 b\u2082 c\u2081 c\u2082}\n \u2192 a\u2081 \u2261 a\u2082 \u2192 b\u2081 \u2261 b\u2082 \u2192 c\u2081 \u2261 c\u2082 \u2192 f a\u2081 b\u2081 c\u2081 \u2261 f a\u2082 b\u2082 c\u2082\ncong\u2083 f refl refl refl = refl\n\n_\u2261\u2261_ : \u2200 {a} {A : Set a} {i j : A}\n (i\u2261j\u2081 : i \u2261 j) (i\u2261j\u2082 : i \u2261 j) \u2192 i\u2261j\u2081 \u2261 i\u2261j\u2082\n_\u2261\u2261_ refl refl = refl\n\n_\u225f\u2261_ : \u2200 {a} {A : Set a} {i j : A} \u2192 Decidable {A = i \u2261 j} _\u2261_\n_\u225f\u2261_ refl refl = yes refl\n\n_\u2257\u2082_ : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c} (f g : A \u2192 B \u2192 C) \u2192 Set _\nf \u2257\u2082 g = \u2200 x y \u2192 f x y \u2261 g x y\n\ninjective : \u2200 {a} {A : Set a} \u2192 InjectiveRel A _\u2261_\ninjective refl refl = refl\n\nsurjective : \u2200 {a} {A : Set a} \u2192 SurjectiveRel A _\u2261_\nsurjective refl refl = refl\n\nbijective : \u2200 {a} {A : Set a} \u2192 BijectiveRel A _\u2261_\nbijective = record { injectiveREL = injective; surjectiveREL = surjective }\n\nmodule \u2261-Reasoning {a} {A : Set a} where\n open Setoid-Reasoning (setoid A) public renaming (_\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_)\n\nmodule \u2257-Reasoning {a b} {A : Set a} {B : Set b} where\n open Setoid-Reasoning (A \u2192-setoid B) public renaming (_\u2248\u27e8_\u27e9_ to _\u2257\u27e8_\u27e9_)\n\ndata \u27e6\u2261\u27e7 {a\u2081 a\u2082 a\u1d63}\n {A\u2081 A\u2082} (A\u1d63 : \u27e6\u2605\u27e7 {a\u2081} {a\u2082} a\u1d63 A\u2081 A\u2082)\n {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082)\n : (A\u1d63 \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 a\u1d63) (_\u2261_ x\u2081) (_\u2261_ x\u2082) where\n -- : \u2200 {y\u2081 y\u2082} (y\u1d63 : A\u1d63 y\u2081 y\u2082) \u2192 x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192 \u2605\n \u27e6refl\u27e7 : \u27e6\u2261\u27e7 A\u1d63 x\u1d63 x\u1d63 refl refl\n\n-- Double checking level 0 with a direct \u27e6_\u27e7 encoding\nprivate\n \u27e6\u2261\u27e7\u2032 : (\u2200\u27e8 A\u1d63 \u2236 \u27e6\u2605\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 \u27e6\u2605\u2080\u27e7) _\u2261_ _\u2261_\n \u27e6\u2261\u27e7\u2032 = \u27e6\u2261\u27e7\n\n \u27e6refl\u27e7\u2032 : (\u2200\u27e8 A\u1d63 \u2236 \u27e6\u2605\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 x\u1d63 \u2236 A\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6\u2261\u27e7 A\u1d63 x\u1d63 x\u1d63) refl refl\n \u27e6refl\u27e7\u2032 _ _ = \u27e6refl\u27e7\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n-- move this to propeq\nmodule Relation.Binary.PropositionalEquality.NP where\n\nopen import Relation.Binary.PropositionalEquality public hiding (module \u2261-Reasoning)\nopen import Relation.Binary.NP\nopen import Relation.Binary.Bijection\nopen import Relation.Binary.Logical\nopen import Relation.Nullary\n\ncong\u2083 : \u2200 {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d}\n (f : A \u2192 B \u2192 C \u2192 D) {a\u2081 a\u2082 b\u2081 b\u2082 c\u2081 c\u2082}\n \u2192 a\u2081 \u2261 a\u2082 \u2192 b\u2081 \u2261 b\u2082 \u2192 c\u2081 \u2261 c\u2082 \u2192 f a\u2081 b\u2081 c\u2081 \u2261 f a\u2082 b\u2082 c\u2082\ncong\u2083 f refl refl refl = refl\n\n_\u2261\u2261_ : \u2200 {a} {A : Set a} {i j : A}\n (i\u2261j\u2081 : i \u2261 j) (i\u2261j\u2082 : i \u2261 j) \u2192 i\u2261j\u2081 \u2261 i\u2261j\u2082\n_\u2261\u2261_ refl refl = refl\n\n_\u225f\u2261_ : \u2200 {a} {A : Set a} {i j : A} \u2192 Decidable {A = i \u2261 j} _\u2261_\n_\u225f\u2261_ refl refl = yes refl\n\n_\u2257\u2082_ : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c} (f g : A \u2192 B \u2192 C) \u2192 Set _\nf \u2257\u2082 g = \u2200 x y \u2192 f x y \u2261 g x y\n\ninjective : \u2200 {a} {A : Set a} \u2192 InjectiveRel A _\u2261_\ninjective refl refl = refl\n\nsurjective : \u2200 {a} {A : Set a} \u2192 SurjectiveRel A _\u2261_\nsurjective refl refl = refl\n\nbijective : \u2200 {a} {A : Set a} \u2192 BijectiveRel A _\u2261_\nbijective = record { injectiveREL = injective; surjectiveREL = surjective }\n\nmodule \u2261-Reasoning {a} {A : Set a} where\n open Setoid-Reasoning (setoid A) public renaming (_\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_)\n\nmodule \u2257-Reasoning {a b} {A : Set a} {B : Set b} where\n open Setoid-Reasoning (A \u2192-setoid B) public renaming (_\u2248\u27e8_\u27e9_ to _\u2257\u27e8_\u27e9_)\n\ndata \u27e6\u2261\u27e7 {a\u2081 a\u2082 a\u1d63}\n {A\u2081 A\u2082} (A\u1d63 : \u27e6\u2605\u27e7 {a\u2081} {a\u2082} a\u1d63 A\u2081 A\u2082)\n {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082)\n : (A\u1d63 \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 a\u1d63) (_\u2261_ x\u2081) (_\u2261_ x\u2082) where\n -- : \u2200 {y\u2081 y\u2082} (y\u1d63 : A\u1d63 y\u2081 y\u2082) \u2192 x\u2081 \u2261 y\u2081 \u2192 x\u2082 \u2261 y\u2082 \u2192 \u2605\n \u27e6refl\u27e7 : \u27e6\u2261\u27e7 A\u1d63 x\u1d63 x\u1d63 refl refl\n\n-- Double checking level 0 with a direct \u27e6_\u27e7 encoding\nprivate\n \u27e6\u2261\u27e7\u2032 : (\u2200\u27e8 A\u1d63 \u2236 \u27e6\u2605\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 \u27e6\u2605\u2080\u27e7) _\u2261_ _\u2261_\n \u27e6\u2261\u27e7\u2032 = \u27e6\u2261\u27e7\n\n \u27e6refl\u27e7\u2032 : (\u2200\u27e8 A\u1d63 \u2236 \u27e6\u2605\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 x\u1d63 \u2236 A\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6\u2261\u27e7 A\u1d63 x\u1d63 x\u1d63) refl refl\n \u27e6refl\u27e7\u2032 _ _ = \u27e6refl\u27e7\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"9b9ca55078054bf601d204c004b44563204271a9","subject":"few more cases of #14, including an elided exercise in loop unrolling","message":"few more cases of #14, including an elided exercise in loop unrolling\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress-checks.agda","new_file":"progress-checks.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import type-assignment-unicity\nopen import lemmas-progress-checks\n\n-- taken together, the theorems in this file argue that for any expression\n-- d, at most one summand of the labeled sum that results from progress may\n-- be true at any time: that boxed values, indeterminates, cast errors, and\n-- expressions that step are pairwise disjoint.\n--\n-- note that as a consequence of currying and comutativity of products,\n-- this means that there are six theorems to prove. in addition to those,\n-- we also prove several convenince forms that combine theorems about\n-- indeterminate and boxed value forms into the same statement about final\n-- forms, which mirrors the mutual definition of indeterminate and final\n-- and saves some redundant argumentation.\nmodule progress-checks where\n -- boxed values are not indeterminates\n boxedval-not-indet : \u2200{d} \u2192 d boxedval \u2192 d indet \u2192 \u22a5\n boxedval-not-indet (BVVal VConst) ()\n boxedval-not-indet (BVVal VLam) ()\n boxedval-not-indet (BVArrCast x bv) (ICastArr x\u2081 ind) = boxedval-not-indet bv ind\n boxedval-not-indet (BVHoleCast x bv) (ICastGroundHole x\u2081 ind) = boxedval-not-indet bv ind\n boxedval-not-indet (BVHoleCast x bv) (ICastHoleGround x\u2081 ind x\u2082) = boxedval-not-indet bv ind\n\n -- boxed values are not errors\n boxedval-not-err : \u2200{d} \u2192 d boxedval \u2192 d casterr \u2192 \u22a5\n boxedval-not-err (BVVal ()) (CECastFail x\u2081 x\u2082 x\u2083 x\u2084)\n boxedval-not-err (BVHoleCast x bv) (CECastFail x\u2081 x\u2082 () x\u2084)\n boxedval-not-err (BVArrCast x bv) (CECong FHOuter er) = boxedval-not-err bv (ce-castarr er)\n boxedval-not-err (BVArrCast x bv) (CECong (FHCast x\u2081) er) = boxedval-not-err bv (CECong x\u2081 er)\n boxedval-not-err (BVHoleCast x bv) (CECong FHOuter er) = boxedval-not-err bv (ce-castth er)\n boxedval-not-err (BVHoleCast x bv) (CECong (FHCast x\u2081) er) = boxedval-not-err bv (CECong x\u2081 er)\n boxedval-not-err (BVVal x) (CECong FHOuter er) = boxedval-not-err (BVVal x) er\n boxedval-not-err (BVVal ()) (CECong (FHAp1 x\u2081) er)\n boxedval-not-err (BVVal ()) (CECong (FHAp2 x\u2081 x\u2082) er)\n boxedval-not-err (BVVal ()) (CECong (FHNEHole x\u2081) er)\n boxedval-not-err (BVVal ()) (CECong (FHCast x\u2081) er)\n\n -- boxed values don't step\n boxedval-not-step : \u2200{d} \u2192 d boxedval \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n boxedval-not-step (BVVal VConst) (d' , Step FHOuter () x\u2083)\n boxedval-not-step (BVVal VLam) (d' , Step FHOuter () x\u2083)\n boxedval-not-step (BVArrCast x bv) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n boxedval-not-step (BVArrCast x bv) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = boxedval-not-step bv (_ , Step x\u2081 x\u2082 x\u2083)\n boxedval-not-step (BVHoleCast () bv) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n boxedval-not-step (BVHoleCast x bv) (d' , Step FHOuter (ITCastSucceed x\u2081 ()) FHOuter)\n boxedval-not-step (BVHoleCast GHole bv) (_ , Step FHOuter (ITGround x\u2081 x\u2082) FHOuter) = x\u2082 refl\n boxedval-not-step (BVHoleCast x bv) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = boxedval-not-step bv (_ , Step x\u2081 x\u2082 x\u2083)\n\n -- todo: what class of P is this true for?\n -- lem-something : \u2200{ d \u03b5 d'} \u2192 d == \u03b5 \u27e6 d' \u27e7 \u2192 P d' \u2192 P d\n\n mutual\n -- indeterminates are not errors\n indet-not-err : \u2200{d} \u2192 d indet \u2192 d casterr \u2192 \u22a5\n indet-not-err IEHole (CECong FHOuter err) = indet-not-err IEHole err\n indet-not-err (INEHole x) (CECong FHOuter err) = final-not-err x (ce-nehole err)\n indet-not-err (INEHole x) (CECong (FHNEHole x\u2081) err) = final-not-err x (CECong x\u2081 err)\n indet-not-err (IAp x indet x\u2081) (CECong FHOuter err)\n with ce-ap err\n ... | Inl d1err = indet-not-err indet d1err\n ... | Inr d2err = final-not-err x\u2081 d2err\n indet-not-err (IAp x indet x\u2081) (CECong (FHAp1 x\u2082) err) = indet-not-err indet (CECong x\u2082 err)\n indet-not-err (IAp x indet x\u2081) (CECong (FHAp2 x\u2082 x\u2083) err) = final-not-err x\u2081 (CECong x\u2083 err)\n indet-not-err (ICastArr x indet) (CECong FHOuter err) = indet-not-err indet (ce-castarr err)\n indet-not-err (ICastArr x indet) (CECong (FHCast x\u2081) err) = indet-not-err indet (CECong x\u2081 err)\n indet-not-err (ICastGroundHole x indet) (CECastFail x\u2081 x\u2082 () x\u2084)\n indet-not-err (ICastGroundHole x indet) (CECong FHOuter err) = indet-not-err indet (ce-castth err)\n indet-not-err (ICastGroundHole x indet) (CECong (FHCast x\u2081) err) = indet-not-err indet (CECong x\u2081 err)\n indet-not-err (ICastHoleGround x indet x\u2081) (CECastFail x\u2082 x\u2083 x\u2084 x\u2085) = x _ _ refl\n indet-not-err (ICastHoleGround x indet x\u2081) (CECong FHOuter err) = indet-not-err indet (ce-castht err x)\n indet-not-err (ICastHoleGround x indet x\u2081) (CECong (FHCast x\u2082) err) = indet-not-err indet (CECong x\u2082 err)\n\n -- final expressions are not errors\n final-not-err : \u2200{d} \u2192 d final \u2192 d casterr \u2192 \u22a5\n final-not-err (FBoxed x) err = boxedval-not-err x err\n final-not-err (FIndet x) err = indet-not-err x err\n\n mutual\n -- indeterminates don't step\n indet-not-step : \u2200{d} \u2192 d indet \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n indet-not-step IEHole (d' , Step FHOuter () FHOuter)\n indet-not-step (INEHole x) (d' , Step FHOuter () FHOuter)\n indet-not-step (INEHole x) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = final-sub-not-trans x x\u2081 x\u2082\n indet-not-step (IAp x\u2081 () x\u2082) (_ , Step FHOuter (ITLam x\u2083) FHOuter)\n indet-not-step (IAp x (ICastArr x\u2081 ind) x\u2082) (_ , Step FHOuter (ITApCast x\u2083 x\u2084) FHOuter) = x _ _ _ _ _ refl\n indet-not-step (IAp x ind _) (_ , Step (FHAp1 x\u2082) x\u2083 (FHAp1 x\u2084)) = indet-not-step ind (_ , Step x\u2082 x\u2083 x\u2084)\n indet-not-step (IAp x ind f) (_ , Step (FHAp2 x\u2082 x\u2083) x\u2084 (FHAp2 x\u2085 x\u2086)) = final-not-step f (_ , Step x\u2083 x\u2084 x\u2086)\n indet-not-step (ICastArr x ind) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n indet-not-step (ICastArr x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n indet-not-step (ICastGroundHole () ind) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n indet-not-step (ICastGroundHole x ind) (d' , Step FHOuter (ITCastSucceed x\u2081 ()) FHOuter)\n indet-not-step (ICastGroundHole GHole ind) (_ , Step FHOuter (ITGround _ y) FHOuter) = y refl\n indet-not-step (ICastGroundHole x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n indet-not-step (ICastHoleGround x ind ()) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n indet-not-step (ICastHoleGround x ind g) (d' , Step FHOuter (ITCastSucceed x\u2081 x\u2082) FHOuter) = x _ _ refl\n indet-not-step (ICastHoleGround x ind GHole) (_ , Step FHOuter (ITExpand x\u2081 x\u2082) FHOuter) = x\u2082 refl\n indet-not-step (ICastHoleGround x ind g) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n\n -- final expressions don't step\n final-not-step : \u2200{d} \u2192 d final \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 \u22a5\n final-not-step (FBoxed x) stp = boxedval-not-step x stp\n final-not-step (FIndet x) stp = indet-not-step x stp\n\n -- errors don't step\n err-not-step : \u2200{d} \u2192 d casterr \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n -- cast fail cases\n err-not-step (CECastFail x x\u2081 () x\u2083) (_ , Step FHOuter (ITCastID x\u2084) FHOuter)\n err-not-step (CECastFail x x\u2081 x\u2082 x\u2083) (_ , Step FHOuter (ITCastSucceed x\u2084 x\u2085) FHOuter) = x\u2083 refl\n err-not-step (CECastFail x x\u2081 GHole x\u2083) (_ , Step FHOuter (ITExpand x\u2084 x\u2085) FHOuter) = x\u2085 refl\n err-not-step (CECastFail x () x\u2082 x\u2083) (_ , Step (FHCast FHOuter) (ITCastID x\u2084) (FHCast FHOuter))\n err-not-step (CECastFail x () x\u2082 x\u2083) (_ , Step (FHCast FHOuter) (ITCastSucceed x\u2084 x\u2085) (FHCast FHOuter))\n err-not-step (CECastFail x GHole x\u2082 x\u2083) (_ , Step (FHCast FHOuter) (ITGround x\u2084 x\u2085) (FHCast FHOuter)) = x\u2085 refl\n err-not-step (CECastFail x x\u2081 x\u2082 x\u2083) (_ , Step (FHCast (FHCast x\u2084)) x\u2085 (FHCast (FHCast x\u2086))) = final-not-step x (_ , Step x\u2084 x\u2085 x\u2086)\n\n -- congruence cases\n err-not-step (CECong FHOuter ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = err-not-step ce (\u03c01 , Step FHOuter x\u2082 FHOuter)\n err-not-step (CECong (FHAp1 FHOuter) (CECong FHOuter ce)) (_ , Step FHOuter (ITLam x\u2082) FHOuter) = boxedval-not-err (BVVal VLam) ce\n err-not-step (CECong (FHAp1 x) ce) (_ , Step FHOuter (ITApCast x\u2081 x\u2082) FHOuter) = {!!} -- final-not-err x\u2081 (CECong {!!} ce) -- proably case on x, but get incomplete pattern garbage\n err-not-step (CECong (FHAp2 x x\u2081) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!!} -- cyrus: possibly another counter example\n err-not-step (CECong (FHNEHole x) ce) (\u03c01 , Step FHOuter () FHOuter)\n err-not-step (CECong (FHCast x) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!!} -- cyrus: only obvious thing to do is case on x\u2082, doesn't seem to get anywhere\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHAp1 x\u2081) x\u2082 (FHAp1 x\u2083))\n with ce-ap ce\n ... | Inl d1err = err-not-step d1err (_ , Step x\u2081 x\u2082 x\u2083)\n ... | Inr d2err = {!Step x\u2081 x\u2082 x\u2083!} -- cyrus: this is a counter example, d2 is a casterror but d1 isn't yet a value so the whole thing steps\n err-not-step (CECong (FHAp1 x) ce) (_ , Step (FHAp1 x\u2081) x\u2082 (FHAp1 x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n err-not-step (CECong (FHAp2 x x\u2081) ce) (_ , Step (FHAp1 x\u2082) x\u2083 (FHAp1 x\u2084)) = final-not-step x (_ , Step x\u2082 x\u2083 x\u2084)\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHAp2 x\u2081 x\u2082) x\u2083 (FHAp2 x\u2084 x\u2085))\n with ce-ap ce\n ... | Inl d1err = final-not-err x\u2084 d1err\n ... | Inr d2err = err-not-step d2err (_ , Step x\u2082 x\u2083 x\u2085)\n err-not-step (CECong (FHAp1 x) ce) (_ , Step (FHAp2 x\u2081 x\u2082) x\u2083 (FHAp2 x\u2084 x\u2085)) = final-not-err x\u2081 (ce-out-cast ce x)\n err-not-step (CECong (FHAp2 x x\u2081) ce) (_ , Step (FHAp2 x\u2082 x\u2083) x\u2084 (FHAp2 x\u2085 x\u2086)) = err-not-step (ce-out-cast ce x\u2081) (_ , Step x\u2083 x\u2084 x\u2086)\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = err-not-step (ce-nehole ce) (_ , Step x\u2081 x\u2082 x\u2083)\n err-not-step (CECong (FHNEHole x) ce) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = {!!} -- err-not-step {!!} (_ , Step x\u2081 x\u2082 x\u2083)\n err-not-step (CECong (FHCast x) ce) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import type-assignment-unicity\nopen import lemmas-progress-checks\n\n-- taken together, the theorems in this file argue that for any expression\n-- d, at most one summand of the labeled sum that results from progress may\n-- be true at any time, i.e. that boxed values, indeterminates, cast\n-- errors, and expressions that step are pairwise disjoint. (note that as a\n-- consequence of currying and comutativity of products, this means that\n-- there are six theorems to prove)\nmodule progress-checks where\n -- boxed values and indeterminates are disjoint\n boxedval-not-indet : \u2200{d} \u2192 d boxedval \u2192 d indet \u2192 \u22a5\n boxedval-not-indet (BVVal VConst) ()\n boxedval-not-indet (BVVal VLam) ()\n boxedval-not-indet (BVArrCast x bv) (ICastArr x\u2081 ind) = boxedval-not-indet bv ind\n boxedval-not-indet (BVHoleCast x bv) (ICastGroundHole x\u2081 ind) = boxedval-not-indet bv ind\n boxedval-not-indet (BVHoleCast x bv) (ICastHoleGround x\u2081 ind x\u2082) = boxedval-not-indet bv ind\n\n -- boxed values and errors are disjoint\n boxedval-not-err : \u2200{d} \u2192 d boxedval \u2192 d casterr \u2192 \u22a5\n boxedval-not-err (BVVal ()) (CECastFail x\u2081 x\u2082 x\u2083 x\u2084)\n boxedval-not-err (BVHoleCast x bv) (CECastFail x\u2081 x\u2082 () x\u2084)\n boxedval-not-err (BVArrCast x bv) (CECong FHOuter er) = boxedval-not-err bv (ce-castarr er)\n boxedval-not-err (BVArrCast x bv) (CECong (FHCast x\u2081) er) = boxedval-not-err bv (CECong x\u2081 er)\n boxedval-not-err (BVHoleCast x bv) (CECong FHOuter er) = boxedval-not-err bv (ce-castth er)\n boxedval-not-err (BVHoleCast x bv) (CECong (FHCast x\u2081) er) = boxedval-not-err bv (CECong x\u2081 er)\n boxedval-not-err (BVVal x) (CECong FHOuter er) = boxedval-not-err (BVVal x) er\n boxedval-not-err (BVVal ()) (CECong (FHAp1 x\u2081) er)\n boxedval-not-err (BVVal ()) (CECong (FHAp2 x\u2081 x\u2082) er)\n boxedval-not-err (BVVal ()) (CECong (FHNEHole x\u2081) er)\n boxedval-not-err (BVVal ()) (CECong (FHCast x\u2081) er)\n\n -- boxed values and expressions that step are disjoint\n boxedval-not-step : \u2200{d} \u2192 d boxedval \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n boxedval-not-step (BVVal VConst) (d' , Step FHOuter () x\u2083)\n boxedval-not-step (BVVal VLam) (d' , Step FHOuter () x\u2083)\n boxedval-not-step (BVArrCast x bv) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n boxedval-not-step (BVArrCast x bv) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = boxedval-not-step bv (_ , Step x\u2081 x\u2082 x\u2083)\n boxedval-not-step (BVHoleCast () bv) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n boxedval-not-step (BVHoleCast x bv) (d' , Step FHOuter (ITCastSucceed x\u2081 ()) FHOuter)\n boxedval-not-step (BVHoleCast GHole bv) (_ , Step FHOuter (ITGround x\u2081 x\u2082) FHOuter) = x\u2082 refl\n boxedval-not-step (BVHoleCast x bv) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = boxedval-not-step bv (_ , Step x\u2081 x\u2082 x\u2083)\n\n -- todo: what class of P is this true for?\n -- lem-something : \u2200{ d \u03b5 d'} \u2192 d == \u03b5 \u27e6 d' \u27e7 \u2192 P d' \u2192 P d\n\n mutual\n -- indeterminates and errors are disjoint\n indet-not-err : \u2200{d} \u2192 d indet \u2192 d casterr \u2192 \u22a5\n indet-not-err IEHole (CECong FHOuter err) = indet-not-err IEHole err\n indet-not-err (INEHole x) (CECong FHOuter err) = final-not-err x (ce-nehole err)\n indet-not-err (INEHole x) (CECong (FHNEHole x\u2081) err) = final-not-err x (CECong x\u2081 err)\n indet-not-err (IAp x indet x\u2081) (CECong FHOuter err)\n with ce-ap err\n ... | Inl d1err = indet-not-err indet d1err\n ... | Inr d2err = final-not-err x\u2081 d2err\n indet-not-err (IAp x indet x\u2081) (CECong (FHAp1 x\u2082) err) = indet-not-err indet (CECong x\u2082 err)\n indet-not-err (IAp x indet x\u2081) (CECong (FHAp2 x\u2082 x\u2083) err) = final-not-err x\u2081 (CECong x\u2083 err)\n indet-not-err (ICastArr x indet) (CECong FHOuter err) = indet-not-err indet (ce-castarr err)\n indet-not-err (ICastArr x indet) (CECong (FHCast x\u2081) err) = indet-not-err indet (CECong x\u2081 err)\n indet-not-err (ICastGroundHole x indet) (CECastFail x\u2081 x\u2082 () x\u2084)\n indet-not-err (ICastGroundHole x indet) (CECong FHOuter err) = indet-not-err indet (ce-castth err)\n indet-not-err (ICastGroundHole x indet) (CECong (FHCast x\u2081) err) = indet-not-err indet (CECong x\u2081 err)\n indet-not-err (ICastHoleGround x indet x\u2081) (CECastFail x\u2082 x\u2083 x\u2084 x\u2085) = x _ _ refl\n indet-not-err (ICastHoleGround x indet x\u2081) (CECong FHOuter err) = indet-not-err indet (ce-castht err x)\n indet-not-err (ICastHoleGround x indet x\u2081) (CECong (FHCast x\u2082) err) = indet-not-err indet (CECong x\u2082 err)\n\n -- final expressions are not errors\n final-not-err : \u2200{d} \u2192 d final \u2192 d casterr \u2192 \u22a5\n final-not-err (FBoxed x) err = boxedval-not-err x err\n final-not-err (FIndet x) err = indet-not-err x err\n\n mutual\n -- indeterminates and expressions that step are disjoint\n indet-not-step : \u2200{d} \u2192 d indet \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n indet-not-step IEHole (d' , Step FHOuter () FHOuter)\n indet-not-step (INEHole x) (d' , Step FHOuter () FHOuter)\n indet-not-step (INEHole x) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = final-sub-not-trans x x\u2081 x\u2082\n indet-not-step (IAp x\u2081 () x\u2082) (_ , Step FHOuter (ITLam x\u2083) FHOuter)\n indet-not-step (IAp x (ICastArr x\u2081 ind) x\u2082) (_ , Step FHOuter (ITApCast x\u2083 x\u2084) FHOuter) = x _ _ _ _ _ refl\n indet-not-step (IAp x ind _) (_ , Step (FHAp1 x\u2082) x\u2083 (FHAp1 x\u2084)) = indet-not-step ind (_ , Step x\u2082 x\u2083 x\u2084)\n indet-not-step (IAp x ind f) (_ , Step (FHAp2 x\u2082 x\u2083) x\u2084 (FHAp2 x\u2085 x\u2086)) = final-not-step f (_ , Step x\u2083 x\u2084 x\u2086)\n indet-not-step (ICastArr x ind) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n indet-not-step (ICastArr x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n indet-not-step (ICastGroundHole () ind) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n indet-not-step (ICastGroundHole x ind) (d' , Step FHOuter (ITCastSucceed x\u2081 ()) FHOuter)\n indet-not-step (ICastGroundHole GHole ind) (_ , Step FHOuter (ITGround _ y) FHOuter) = y refl\n indet-not-step (ICastGroundHole x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n indet-not-step (ICastHoleGround x ind ()) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n indet-not-step (ICastHoleGround x ind g) (d' , Step FHOuter (ITCastSucceed x\u2081 x\u2082) FHOuter) = x _ _ refl\n indet-not-step (ICastHoleGround x ind GHole) (_ , Step FHOuter (ITExpand x\u2081 x\u2082) FHOuter) = x\u2082 refl\n indet-not-step (ICastHoleGround x ind g) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n\n -- final expressions don't step\n final-not-step : \u2200{d} \u2192 d final \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 \u22a5\n final-not-step (FBoxed x) stp = boxedval-not-step x stp\n final-not-step (FIndet x) stp = indet-not-step x stp\n\n -- errors and expressions that step are disjoint\n err-not-step : \u2200{d} \u2192 d casterr \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n -- cast fail cases\n err-not-step (CECastFail x x\u2081 () x\u2083) (_ , Step FHOuter (ITCastID x\u2084) FHOuter)\n err-not-step (CECastFail x x\u2081 x\u2082 x\u2083) (_ , Step FHOuter (ITCastSucceed x\u2084 x\u2085) FHOuter) = x\u2083 refl\n err-not-step (CECastFail x x\u2081 GHole x\u2083) (_ , Step FHOuter (ITExpand x\u2084 x\u2085) FHOuter) = x\u2085 refl\n err-not-step (CECastFail x () x\u2082 x\u2083) (_ , Step (FHCast FHOuter) (ITCastID x\u2084) (FHCast FHOuter))\n err-not-step (CECastFail x () x\u2082 x\u2083) (_ , Step (FHCast FHOuter) (ITCastSucceed x\u2084 x\u2085) (FHCast FHOuter))\n err-not-step (CECastFail x GHole x\u2082 x\u2083) (_ , Step (FHCast FHOuter) (ITGround x\u2084 x\u2085) (FHCast FHOuter)) = x\u2085 refl\n err-not-step (CECastFail x x\u2081 x\u2082 x\u2083) (_ , Step (FHCast (FHCast x\u2084)) x\u2085 (FHCast (FHCast x\u2086))) = final-not-step x (_ , Step x\u2084 x\u2085 x\u2086)\n\n -- congruence cases\n err-not-step (CECong FHOuter ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = err-not-step ce (\u03c01 , Step FHOuter x\u2082 FHOuter)\n err-not-step (CECong (FHAp1 FHOuter) (CECong FHOuter ce)) (_ , Step FHOuter (ITLam x\u2082) FHOuter) = boxedval-not-err (BVVal VLam) ce\n err-not-step (CECong (FHAp1 x) ce) (_ , Step FHOuter (ITApCast x\u2081 x\u2082) FHOuter) = {!!} -- fe x\u2081 (CECong {!ce-out-cast ce!} ce)\n err-not-step (CECong (FHAp2 x x\u2081) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!ce-out-cast ce x\u2081!}\n err-not-step (CECong (FHNEHole x) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!!}\n err-not-step (CECong (FHCast x) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!!}\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHAp1 x\u2081) x\u2082 (FHAp1 x\u2083))\n with ce-ap ce\n ... | Inl d1err = err-not-step d1err (_ , Step x\u2081 x\u2082 x\u2083)\n ... | Inr d2err = {!Step x\u2081 x\u2082 x\u2083!} -- cyrus this is a counter example: d2 is a casterror but d1 isn't yet a value so the whole thing steps\n err-not-step (CECong (FHAp1 x) ce) (_ , Step (FHAp1 x\u2081) x\u2082 (FHAp1 x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n err-not-step (CECong (FHAp2 x x\u2081) ce) (_ , Step (FHAp1 x\u2082) x\u2083 (FHAp1 x\u2084)) = final-not-step x (_ , Step x\u2082 x\u2083 x\u2084)\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHAp2 x\u2081 x\u2082) x\u2083 (FHAp2 x\u2084 x\u2085))\n with ce-ap ce\n ... | Inl d1err = final-not-err x\u2084 d1err\n ... | Inr d2err = err-not-step d2err (_ , Step x\u2082 x\u2083 x\u2085)\n err-not-step (CECong (FHAp1 x) ce) (_ , Step (FHAp2 x\u2081 x\u2082) x\u2083 (FHAp2 x\u2084 x\u2085)) = {!!}\n err-not-step (CECong (FHAp2 x x\u2081) ce) (_ , Step (FHAp2 x\u2082 x\u2083) x\u2084 (FHAp2 x\u2085 x\u2086)) = {!!}\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = err-not-step (ce-nehole ce) (_ , Step x\u2081 x\u2082 x\u2083)\n err-not-step (CECong (FHNEHole x) ce) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = {!!} -- err-not-step {!!} (_ , Step x\u2081 x\u2082 x\u2083) -- this might not work\n err-not-step (CECong (FHCast x) ce) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"dabd8bc14552ef33e1791e95dba210bfae26d7a0","subject":"Typo.","message":"Typo.\n\nIgnore-this: 59c6c0863d6e6aaabad15e16de6dd83\n\ndarcs-hash:20120223161854-3bd4e-069e05e6ab9b3c8e18f2242c558fee7bc0105f2b.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/PA\/Axiomatic\/Standard\/Base.agda","new_file":"src\/PA\/Axiomatic\/Standard\/Base.agda","new_contents":"------------------------------------------------------------------------------\n-- Axiomatic Peano arithmetic base\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule PA.Axiomatic.Standard.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfixl 10 _*_\ninfixl 9 _+_\n\n------------------------------------------------------------------------------\n-- PA universe.\n-- We chose the symbol M because there are non-standard models of\n-- Peano Arithmetic, where the domain is not the set of natural\n-- numbers.\nopen import FOL.Universe public renaming ( D to M )\n\n-- FOL with equality.\nopen import FOL.FOL-Eq public\n\n-- Non-logical constants\npostulate\n zero : M\n succ : M \u2192 M\n _+_ _*_ : M \u2192 M \u2192 M\n\n-- Proper axioms\n-- From [Machover, 1977, p. 263] and [H\u00e1jeck and Pudl\u00e1k, 1998, p. 28]\n--\n-- * Mosh\u00e9 Machover. Set theory, logic and their\n-- limitations. Cambridge University Press, 1996.\n--\n-- * Petr H\u00e1jeck and Pavel Pudl\u00e1k. Metamathematics of First-Order\n-- Arithmetic. Springer, 1998. 2nd printing.\n-- )\n\n-- A\u2081. 0 \u2260 succ n\n-- A\u2082. succ m = succ n \u2192 m = n\n-- A\u2083. 0 + n = n\n-- A\u2084. succ m + n = succ (m + n)\n-- A\u2085. 0 * n = 0\n-- A\u2086. succ m * n = (m * n) + n\n-- A\u2087. P(0) \u2192 (\u2200n.P(n) \u2192 P(succ n)) \u2192 \u2200n.P(n), for any wff P(n) of PA.\n\npostulate\n A\u2081 : \u2200 {n} \u2192 \u00ac (zero \u2261 succ n)\n A\u2082 : \u2200 {m n} \u2192 succ m \u2261 succ n \u2192 m \u2261 n\n A\u2083 : \u2200 n \u2192 zero + n \u2261 n\n A\u2084 : \u2200 m n \u2192 succ m + n \u2261 succ (m + n)\n A\u2085 : \u2200 n \u2192 zero * n \u2261 zero\n A\u2086 : \u2200 m n \u2192 succ m * n \u2261 n + m * n\n{-# ATP axiom A\u2081 A\u2082 A\u2083 A\u2084 A\u2085 A\u2086 #-}\n\n-- A\u2087 is an axiom schema, therefore we do not translate it to TPTP.\npostulate A\u2087 : (P : M \u2192 Set) \u2192 P zero \u2192 (\u2200 n \u2192 P n \u2192 P (succ n)) \u2192 \u2200 n \u2192 P n\n","old_contents":"------------------------------------------------------------------------------\n-- Axiomatic Peano arithmetic base\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule PA.Axiomatic.Standard.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfixl 10 _*_\ninfixl 9 _+_\n\n------------------------------------------------------------------------------\n-- PA universe.\n-- We chose the symbol M because there are non-standard models of\n-- Peano Arithmetic, where the domain is not the set of natural\n-- numbers.\nopen import FOL.Universe public renaming ( D to M )\n\n-- FOL with equality.\nopen import FOL.FOL-Eq public\n\n-- Non-logical constants\npostulate\n zero : M\n succ : M \u2192 M\n _+_ _*_ : M \u2192 M \u2192 M\n\n-- Proper axioms (see [Machover, 1977, p. 263], [H\u00e1jeck and Pudl\u00e1k,\n 1998, p. 28])\n--\n-- * Mosh\u00e9 Machover. Set theory, logic and their\n-- limitations. Cambridge University Press, 1996.\n--\n-- * Petr H\u00e1jeck and Pavel Pudl\u00e1k. Metamathematics of First-Order\n-- Arithmetic. Springer, 1998. 2nd printing.\n\n-- A\u2081. 0 \u2260 succ n\n-- A\u2082. succ m = succ n \u2192 m = n\n-- A\u2083. 0 + n = n\n-- A\u2084. succ m + n = succ (m + n)\n-- A\u2085. 0 * n = 0\n-- A\u2086. succ m * n = (m * n) + n\n-- A\u2087. P(0) \u2192 (\u2200n.P(n) \u2192 P(succ n)) \u2192 \u2200n.P(n), for any wff P(n) of PA.\n\npostulate\n A\u2081 : \u2200 {n} \u2192 \u00ac (zero \u2261 succ n)\n A\u2082 : \u2200 {m n} \u2192 succ m \u2261 succ n \u2192 m \u2261 n\n A\u2083 : \u2200 n \u2192 zero + n \u2261 n\n A\u2084 : \u2200 m n \u2192 succ m + n \u2261 succ (m + n)\n A\u2085 : \u2200 n \u2192 zero * n \u2261 zero\n A\u2086 : \u2200 m n \u2192 succ m * n \u2261 n + m * n\n{-# ATP axiom A\u2081 A\u2082 A\u2083 A\u2084 A\u2085 A\u2086 #-}\n\n-- A\u2087 is an axiom schema, therefore we do not translate it to TPTP.\npostulate A\u2087 : (P : M \u2192 Set) \u2192 P zero \u2192 (\u2200 n \u2192 P n \u2192 P (succ n)) \u2192 \u2200 n \u2192 P n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"082fca7202356a2254bdefad74bb4680042edbdb","subject":"Made a module private.","message":"Made a module private.\n\nIgnore-this: c8dd74d9a93a0ee413fd3a59fffc49f4\n\ndarcs-hash:20120305232020-3bd4e-b6b668cdb5a1c4ff19d99a001ea15a451288921a.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Data\/Stream\/Equality.agda","new_file":"src\/FOTC\/Data\/Stream\/Equality.agda","new_contents":"------------------------------------------------------------------------------\n-- Equality on streams\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Stream.Equality where\n\nopen import FOTC.Base\n\n-- We add 3 to the fixities of the standard library.\ninfix 7 _\u2248_\n\n------------------------------------------------------------------------------\n-- The equality on streams.\npostulate\n _\u2248_ : D \u2192 D \u2192 Set\n\n-- The relation _\u2248_ is the greatest post-fixed point (by \u2248-gfp\u2081 and\n-- \u2248-gfp\u2082) of the bisimulation functional (see below).\n\n-- The relation _\u2248_ is a post-fixed point of the bisimulation functional\n-- (see below).\npostulate\n \u2248-gfp\u2081 : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n{-# ATP axiom \u2248-gfp\u2081 #-}\n\n-- N.B. This is an axiom schema. Because in the automatic proofs we\n-- *must* use an instance, we do not add this postulate as an ATP\n-- axiom.\npostulate\n \u2248-gfp\u2082 : (_R_ : D \u2192 D \u2192 Set) \u2192\n -- R is a post-fixed point of the bisimulation functional.\n (\u2200 {xs ys} \u2192 xs R ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys') \u2192\n -- _\u2248_ is greater than R.\n \u2200 {xs ys} \u2192 xs R ys \u2192 xs \u2248 ys\n\n-- Because a greatest post-fixed point is a fixed point, the relation\n-- _\u2248_ is also a pre-fixed point of the bisimulation functional (see\n-- below).\n\u2248-gfp\u2083 : \u2200 {xs ys} \u2192\n (\u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys') \u2192\n xs \u2248 ys\n\u2248-gfp\u2083 h = \u2248-gfp\u2082 _R_ helper h\n where\n _R_ : D \u2192 D \u2192 Set\n _R_ xs ys = \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n\n helper : \u2200 {xs ys} \u2192 xs R ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n helper (_ , _ , _ , xs'\u2248ys' , prf) = _ , _ , _ , \u2248-gfp\u2081 xs'\u2248ys' , prf\n\nprivate\n module Bisimulation where\n -- In FOTC we won't use the bisimulation functional. This module is\n -- only for illustrative purposes.\n\n -- Adapted from [1]. In this paper the authors use the name\n\n -- as (R :: R') bs' (p. 310)\n\n -- for the bisimulation functional.\n\n -- [1] Peter Dybjer and Herbert Sander. A functional programming\n -- approach to the specification and verification of concurrent\n -- systems. Formal Aspects of Computing, 1:303\u2013319, 1989.\n\n -- The bisimulation functional (Bart Jacobs and Jan\n -- Rutten. (Co)algebras and (co)induction. EATCS Bulletin,\n -- 62:222\u2013259, 1997).\n Bisimulation : (D \u2192 D \u2192 Set) \u2192 D \u2192 D \u2192 Set\n Bisimulation _R_ xs ys =\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n\n -- The relation _\u2248_ is the greatest post-fixed point of\n -- Bisimulation (by post-fp and gpfp).\n\n -- The relation _\u2248_ is a post-fixed point of Bisimulation.\n post-fp : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Bisimulation _\u2248_ xs ys\n post-fp = \u2248-gfp\u2081\n\n gpfp : (_R_ : D \u2192 D \u2192 Set) \u2192\n -- R is a post-fixed point of Bisimulation.\n (\u2200 {xs ys} \u2192 xs R ys \u2192 Bisimulation _R_ xs ys) \u2192\n -- _\u2248_ is greater than R.\n \u2200 {xs ys} \u2192 xs R ys \u2192 xs \u2248 ys\n gpfp = \u2248-gfp\u2082\n\n -- The relation _\u2248_ is a pre-fixed point of Bisimulation.\n pre-fp : \u2200 {xs ys} \u2192 Bisimulation _\u2248_ xs ys \u2192 xs \u2248 ys\n pre-fp = \u2248-gfp\u2083\n","old_contents":"------------------------------------------------------------------------------\n-- Equality on streams\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Stream.Equality where\n\nopen import FOTC.Base\n\n-- We add 3 to the fixities of the standard library.\ninfix 7 _\u2248_\n\n------------------------------------------------------------------------------\n-- The equality on streams.\npostulate\n _\u2248_ : D \u2192 D \u2192 Set\n\n-- The relation _\u2248_ is the greatest post-fixed point (by \u2248-gfp\u2081 and\n-- \u2248-gfp\u2082) of the bisimulation functional (see below).\n\n-- The relation _\u2248_ is a post-fixed point of the bisimulation functional\n-- (see below).\npostulate\n \u2248-gfp\u2081 : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n{-# ATP axiom \u2248-gfp\u2081 #-}\n\n-- N.B. This is an axiom schema. Because in the automatic proofs we\n-- *must* use an instance, we do not add this postulate as an ATP\n-- axiom.\npostulate\n \u2248-gfp\u2082 : (_R_ : D \u2192 D \u2192 Set) \u2192\n -- R is a post-fixed point of the bisimulation functional.\n (\u2200 {xs ys} \u2192 xs R ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys') \u2192\n -- _\u2248_ is greater than R.\n \u2200 {xs ys} \u2192 xs R ys \u2192 xs \u2248 ys\n\n-- Because a greatest post-fixed point is a fixed point, the relation\n-- _\u2248_ is also a pre-fixed point of the bisimulation functional (see\n-- below).\n\u2248-gfp\u2083 : \u2200 {xs ys} \u2192\n (\u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys') \u2192\n xs \u2248 ys\n\u2248-gfp\u2083 h = \u2248-gfp\u2082 _R_ helper h\n where\n _R_ : D \u2192 D \u2192 Set\n _R_ xs ys = \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n\n helper : \u2200 {xs ys} \u2192 xs R ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n helper (_ , _ , _ , xs'\u2248ys' , prf) = _ , _ , _ , \u2248-gfp\u2081 xs'\u2248ys' , prf\n\nmodule Bisimulation where\n -- In FOTC we won't use the bisimulation functional. This module is\n -- only for illustrative purposes.\n\n -- Adapted from [1]. In this paper the authors use the name\n\n -- as (R :: R') bs' (p. 310)\n\n -- for the bisimulation functional.\n\n -- [1] Peter Dybjer and Herbert Sander. A functional programming\n -- approach to the specification and verification of concurrent\n -- systems. Formal Aspects of Computing, 1:303\u2013319, 1989.\n\n -- The bisimulation functional (Bart Jacobs and Jan\n -- Rutten. (Co)algebras and (co)induction. EATCS Bulletin,\n -- 62:222\u2013259, 1997).\n Bisimulation : (D \u2192 D \u2192 Set) \u2192 D \u2192 D \u2192 Set\n Bisimulation _R_ xs ys =\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n\n -- The relation _\u2248_ is the greatest post-fixed point of\n -- Bisimulation (by post-fp and gpfp).\n\n -- The relation _\u2248_ is a post-fixed point of Bisimulation.\n post-fp : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Bisimulation _\u2248_ xs ys\n post-fp = \u2248-gfp\u2081\n\n gpfp : (_R_ : D \u2192 D \u2192 Set) \u2192\n -- R is a post-fixed point of Bisimulation.\n (\u2200 {xs ys} \u2192 xs R ys \u2192 Bisimulation _R_ xs ys) \u2192\n -- _\u2248_ is greater than R.\n \u2200 {xs ys} \u2192 xs R ys \u2192 xs \u2248 ys\n gpfp = \u2248-gfp\u2082\n\n -- The relation _\u2248_ is a pre-fixed point of Bisimulation.\n pre-fp : \u2200 {xs ys} \u2192 Bisimulation _\u2248_ xs ys \u2192 xs \u2248 ys\n pre-fp = \u2248-gfp\u2083\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"93eab697da9ba8131347a9dab0758cc9925c3605","subject":"Control\/Protocol\/Choreography.agda","message":"Control\/Protocol\/Choreography.agda\n","repos":"crypto-agda\/crypto-agda","old_file":"Control\/Protocol\/Choreography.agda","new_file":"Control\/Protocol\/Choreography.agda","new_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product\nopen import Data.One\n\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndualInOut : InOut \u2192 InOut\ndualInOut In = Out\ndualInOut Out = In\n\ndata Proto : \u2605\u2081 where\n end : Proto\n com : (q : InOut)(M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com q M P \u2261\u1d3e com q M Q\n\npattern \u03a0' M P = com In M P\npattern \u03a3' M P = com Out M P\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0S' : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0S' M P = \u03a0' (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3S' : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3S' M P = \u03a3' (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u27e6_\u27e7\u03a0\u03a3 : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u03a0\u03a3 In = \u03a0\n\u27e6_\u27e7\u03a0\u03a3 Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com q M P \u27e7 = \u27e6 q \u27e7\u03a0\u03a3 M \u03bb x \u2192 \u27e6 P x \u27e7\n\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\n_-[_]\u2192\u00f8\u204f_ : \u2200 {I}(A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\nA -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\ndata [_\/_\u2261_] {I} : Choreo I \u2192 I \u2192 Proto \u2192 \u2605\u2081 where\n I\u03a3D : \u2200 {A B M \u2102 \u2102A} \u2192 (\u2200 m \u2192 [ \u2102 m \/ A \u2261 \u2102A m ]) \u2192 [ (A -[ M ]\u2192 B \u204f \u2102) \/ A \u2261 \u03a3' M \u2102A ]\n I\u03a0D : \u2200 {A B M \u2102 \u2102B} \u2192 (\u2200 m \u2192 [ \u2102 m \/ B \u2261 \u2102B m ]) \u2192 [ (A -[ M ]\u2192 B \u204f \u2102) \/ B \u2261 \u03a0' M \u2102B ]\n-- I\u03a0S : \u2200 {A B C M \u2102 \u2102C} \u2192 (\u2200 ..m \u2192 [ \u2102 m \/ C \u2261 \u2102C m ]) \u2192 [ (A -[ M ]\u2192 B \u204f \u2102) \/ C \u2261 \u03a0' (\u2610 M) \u2102C ]\n \u2605\u03a3D : \u2200 {A M \u2102 \u2102A} \u2192 (\u2200 m \u2192 [ \u2102 m \/ A \u2261 \u2102A m ]) \u2192 [ (A -[ M ]\u2192\u2605\u204f \u2102) \/ A \u2261 \u03a3' M \u2102A ]\n \u2605\u03a0D : \u2200 {A B M \u2102 \u2102B} \u2192 (\u2200 m \u2192 [ \u2102 m \/ B \u2261 \u2102B m ]) \u2192 [ (A -[ M ]\u2192\u2605\u204f \u2102) \/ B \u2261 \u03a0' M \u2102B ]\n --\u00f8\u03a3D : \u2200 {A M \u2102 \u2102A} \u2192 (\u2200 ..m \u2192 [ \u2102 m \/ A \u2261 \u2102A m ]) \u2192 [ (A -[ M ]\u2192\u00f8\u204f \u2102) \/ A \u2261 \u03a3' S M \u2102A ]\n --\u00f8\u03a0D : \u2200 {A B M \u2102 \u2102B} \u2192 (\u2200 ..m \u2192 [ \u2102 m \/ B \u2261 \u2102B m ]) \u2192 [ (A -[ M ]\u2192\u00f8\u204f \u2102) \/ B \u2261 \u03a0' S M \u2102B ]\n end : \u2200 {A} \u2192 [ end \/ A \u2261 end ]\n\nTrace : Proto \u2192 Proto\nTrace end = end\nTrace (com _ A B) = \u03a3' A \u03bb m \u2192 Trace (B m)\n\ndual : Proto \u2192 Proto\ndual end = end\ndual (com q A B) = com (dualInOut q) A (\u03bb x \u2192 dual (B x))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com q M P\u2261Q) = cong (com q M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com q M P) = com q M (\u03bb m \u2192 \u2261\u1d3e-refl (P m))\n\nTrace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261\u1d3e Trace P\nTrace-idempotent end = end\nTrace-idempotent (com q M P) = \u03a3' M \u03bb m \u2192 Trace-idempotent (P m)\n\nTrace-dual-oblivious : \u2200 P \u2192 Trace (dual P) \u2261\u1d3e Trace P\nTrace-dual-oblivious end = end\nTrace-dual-oblivious (com q M P) = \u03a3' M \u03bb m \u2192 Trace-dual-oblivious (P m)\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>\u2261_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>\u2261 Q = Q _\ncom \u03c0 A B >>\u2261 Q = com \u03c0 A (\u03bb x \u2192 B x >>\u2261 (\u03bb xs \u2192 Q (x , xs)))\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>\u2261 \u03bb _ \u2192 Q\n\n++Tele : \u2200 (P : Proto){Q : Tele P \u2192 Proto} (xs : Tele P) \u2192 Tele (Q xs) \u2192 Tele (P >>\u2261 Q)\n++Tele end _ ys = ys\n++Tele (com q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\nright-unit : \u2200 (P : Proto) \u2192 (P >>\u2261 \u03bb x \u2192 end) \u2261\u1d3e P\nright-unit end = end\nright-unit (com q M P) = com q M \u03bb m \u2192 right-unit (P m)\n\nassoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>\u2261 Q) \u2192 Proto)\n \u2192 P >>\u2261 (\u03bb x \u2192 Q x >>\u2261 (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>\u2261 Q) >>\u2261 R)\nassoc end Q R = \u2261\u1d3e-refl (Q _ >>\u2261 R)\nassoc (com q M P) Q R = com q M \u03bb m \u2192 assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\n_\u00d7'_ : Set \u2192 Proto \u2192 Proto\nA \u00d7' B = \u03a3' A \u03bb _ \u2192 B\n\n_\u2192'_ : Set \u2192 Proto \u2192 Proto\nA \u2192' B = \u03a0' A \u03bb _ \u2192 B\n\ndata DualInOut : InOut \u2192 InOut \u2192 \u2605 where\n DInOut : DualInOut In Out\n DOutIn : DualInOut Out In\n\ndata Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {A B B'} \u2192 (\u2200 x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a0' A B) (\u03a3' A B')\n \u03a3\u00b7\u03a0 : \u2200 {A B B'} \u2192 (\u2200 x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a3' A B) (\u03a0' A B')\n\ndata Sing {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n sing : \u2200 x \u2192 Sing x\n\n-- Commit = \u03bb M R \u2192 \u03a3' D (\u2610 M) \u03bb { [ m ] \u2192 \u03a0' D R \u03bb r \u2192 \u03a3' D (Sing m) (\u03bb _ \u2192 end) }\nCommit = \u03bb M R \u2192 \u03a3S' M \u03bb m \u2192 \u03a0' R \u03bb r \u2192 \u03a3' (Sing m) (\u03bb _ \u2192 end)\n\ncommit : \u2200 M R (m : M) \u2192 \u27e6 Commit M R \u27e7\ncommit M R m = [ m ] , (\u03bb x \u2192 (sing m) , _)\n\ndecommit : \u2200 M R (r : R) \u2192 \u27e6 dual (Commit M R) \u27e7\ndecommit M R r = \u03bb { [ m ] \u2192 r , (\u03bb x \u2192 0\u2081) }\n\ndata [_&_\u2261_]InOut : InOut \u2192 InOut \u2192 InOut \u2192 \u2605\u2081 where\n \u03a0XX : \u2200 {X} \u2192 [ In & X \u2261 X ]InOut\n X\u03a0X : \u2200 {X} \u2192 [ X & In \u2261 X ]InOut\n\n&InOut-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]InOut \u2192 [ Q & P \u2261 R ]InOut\n&InOut-comm \u03a0XX = X\u03a0X\n&InOut-comm X\u03a0X = \u03a0XX\n\ndata [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n end& : \u2200 {P} \u2192 [ end & P \u2261 P ]\n &end : \u2200 {P} \u2192 [ P & end \u2261 P ]\n D&D : \u2200 {qP qQ qR M P Q R}(q : [ qP & qQ \u2261 qR ]InOut)(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ com qP M P & com qQ M Q \u2261 com qR M R ]\n S&D : \u2200 {qP qQ qR M P Q R}(q : [ qP & qQ \u2261 qR ]InOut)(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ com qP (\u2610 M) P & com qQ M Q \u2261 com qR M R ]\n D&S : \u2200 {qP qQ qR M P Q R}(q : [ qP & qQ \u2261 qR ]InOut)(P& : \u2200 m \u2192 [ P m & Q [ m ] \u2261 R m ]) \u2192 [ com qP M P & com qQ (\u2610 M) Q \u2261 com qR M R ]\n\n&-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n&-comm end& = &end\n&-comm &end = end&\n&-comm (D&D q P&) = D&D (&InOut-comm q) (\u03bb m \u2192 &-comm (P& m))\n&-comm (S&D q P&) = D&S (&InOut-comm q) (\u03bb m \u2192 &-comm (P& m))\n&-comm (D&S q P&) = S&D (&InOut-comm q) (\u03bb m \u2192 &-comm (P& m))\n\nDualInOut-sym : \u2200 {P Q} \u2192 DualInOut P Q \u2192 DualInOut Q P\nDualInOut-sym DInOut = DOutIn\nDualInOut-sym DOutIn = DInOut\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n\nDualInOut-spec : \u2200 P \u2192 DualInOut P (dualInOut P)\nDualInOut-spec In = DInOut\nDualInOut-spec Out = DOutIn\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (com In M P) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-spec (P x))\nDual-spec (com Out M P) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-spec (P x))\n\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele (dual P) \u2261 Tele P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com q M P) X = \u27e6 q \u27e7\u03a0\u03a3 M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n bind-spec : (P : Proto){Q : Tele P \u2192 Proto}{X : Tele (P >>\u2261 Q) \u2192 \u2605} \u2192 El (P >>\u2261 Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n bind-spec end = refl\n bind-spec (com q M P) = cong (\u27e6 q \u27e7\u03a0\u03a3 M) (funExt \u03bb m \u2192 bind-spec (P m))\n\n\nmodule _ {A B} where\n run-com : (P : Proto) \u2192 El P (const A) \u2192 El (dual P) (const B) \u2192 A \u00d7 B\n run-com end a b = a , b\n run-com (\u03a0' M P) p (m , q) = run-com (P m) (p m) q\n run-com (\u03a3' M P) (m , p) q = run-com (P m) p (q m)\n\ncom-cont : (P : Proto){A : Tele P \u2192 \u2605}{B : Tele (dual P) \u2192 \u2605} \u2192 El P A \u2192 El (dual P) B \u2192 \u03a3 (Tele P) A \u00d7 \u03a3 (Tele (dual P)) B\ncom-cont end p q = (_ , p) , (_ , q)\ncom-cont (\u03a0' A B) p (m , q) with com-cont (B m) (p m) q\n... | (X , a) , (Y , b) = ((m , X) , a) , (m , Y) , b\ncom-cont (\u03a3' A B) (m , p) q with com-cont (B m) p (q m)\n... | (X , a) , (Y , b) = ((m , X) , a) , (m , Y) , b\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Proto \u2192 \u2605\u2081 where\n recv : \u2200 {M P} \u2192 ((m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0' M P)\n send : \u2200 {M P} (m : M) \u2192 this (P m) \u2192 ProcessF this (\u03a3' M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0' (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3' (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process (A : \u2605) : Proto \u2192 \u2605\u2081 where\n do : \u2200 {P} \u2192 ProcessF (Process A) P \u2192 Process A P\n end : A \u2192 Process A end\n\nmutual\n SimL : Proto \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Proto \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n left : \u2200 {q M P Q} \u2192 SimL (com q M P) Q \u2192 Sim (com q M P) Q\n right : \u2200 {P q M Q} \u2192 SimR P (com q M Q) \u2192 Sim P (com q M Q)\n end : Sim end end\n\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = \u2200 {A} \u2192 Process A (dual P) \u2192 Process A Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& P& (right \u00b7PA) (right \u00b7PB) = {! (sim&R P& \u00b7PA \u00b7PB)!}\nsim& end& PA PB = PB\nsim& &end PA PB = PA\n\nsim&R (\u03a0D\u03a3D\u03a3S P&) (recv PQA) (send m PQB) = sendS m (sim& (P& m) (PQA m) PQB)\nsim&R (\u03a0D\u03a0D\u03a0D P&) (recv PQA) (recv PQB) = recv \u03bb m \u2192 sim& (P& m) (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) (recvS PQA) (recvS PQB) = recvS \u03bb m \u2192 sim& (P& m) (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) (recvS PQA) (sendS m PQB) = sendS m (sim& (P& m) (PQA m) PQB)\nsim&R (\u03a0S-D-D P&) (recvS PQA) (recv PQB) = recv \u03bb m \u2192 sim& (P& m) (PQA m) (PQB m)\nsim&R (\u03a0S-D-D P&) (recvS PQA) (send m PQB) = send m (sim& (P& m) (PQA m) PQB)\nsim&R (\u03a3D\u03a0D\u03a3S P&) (send m PQA) (recv PQB) = sendS m (sim& (P& m) PQA (PQB m))\nsim&R &end _ ()\n-}\n\n{-\nsim&R (\u03a3D\u03a0D\u03a3S x) (send x\u2081 x\u2082) (recv x\u2083) = {!!}\n-}\n\n{-\nsim& : \u2200 {PA PB PAB QA QB QAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 [ QA & QB \u2261 QAB ] \u2192 Sim PA QA \u2192 Sim PB QB \u2192 Sim PAB QAB\nsim&L : \u2200 {PA PB PAB QA QB QAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 [ QA & QB \u2261 QAB ] \u2192 SimL PA QA \u2192 SimL PB QB \u2192 SimL PAB QAB\nsim&R : \u2200 {PA PB PAB QA QB QAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 [ QA & QB \u2261 QAB ] \u2192 SimR PA QA \u2192 SimR PB QB \u2192 SimR PAB QAB\n\nsim& P& Q& (left PQA) (left PQB) = left (sim&L P& Q& PQA PQB)\nsim& P& Q& (left PQA) (right PQB) = {!!}\nsim& P& Q& (left PQA) end = {!!}\nsim& P& Q& (right x) PQB = {!!}\nsim& P& Q& end PQB = {!!}\n\nsim&L (\u03a0D\u03a3D\u03a3S P&) Q& (recv PQA) (send m PQB) = sendS m (sim& (P& m) Q& (PQA m) PQB)\nsim&L (\u03a0D\u03a0D\u03a0D P&) Q& (recv PQA) (recv PQB) = recv \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&L (\u03a0S-S-S P&) Q& (recvS PQA) (recvS PQB) = recvS \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&L (\u03a0S-S-S P&) Q& (recvS PQA) (sendS m PQB) = sendS m (sim& (P& m) Q& (PQA m) PQB)\nsim&L (\u03a0S-D-D P&) Q& (recvS PQA) (recv PQB) = recv \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&L (\u03a0S-D-D P&) Q& (recvS PQA) (send m PQB) = send m (sim& (P& m) Q& (PQA m) PQB)\nsim&L (\u03a3D\u03a0D\u03a3S P&) Q& (send m PQA) (recv PQB) = sendS m (sim& (P& m) Q& PQA (PQB m))\nsim&L &end Q& PQA ()\n\n{-\nsim&R (\u03a0D\u03a3D\u03a3S P&) Q& (recv PQA) (send m PQB) = ?\nsim&R (\u03a0D\u03a0D\u03a0D P&) Q& (recv PQA) (recv PQB) = ? -- recv \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) Q& (recvS PQA) (recvS PQB) = ? -- recvS \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) Q& (recvS PQA) (sendS m PQB) = ? -- sendS m (sim& (P& m) Q& (PQA m) PQB)\nsim&R (\u03a0S-D-D P&) Q& (recvS PQA) (recv PQB) = ? -- recv \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&R (\u03a0S-D-D P&) Q& (recvS PQA) (send m PQB) = ? -- send m (sim& (P& m) Q& (PQA m) PQB)\nsim&R (\u03a3D\u03a0D\u03a3S P&) Q& (send m PQA) (recv PQB) = ? -- sendS m (sim& (P& m) Q& PQA (PQB m))\nsim&R end& Q& PQA PQB = ?\n-}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0' A B) = right (recv (\u03bb x \u2192 left (send x (sim-id (B x)))))\nsim-id (\u03a3' A B) = left (recv (\u03bb x \u2192 right (send x (sim-id (B x)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (left x) QR = left (sim-compL Q-Q' x QR)\nsim-comp Q-Q' (right x) (left x\u2081) = sim-compRL Q-Q' x x\u2081\nsim-comp Q-Q' (right x) (right x\u2081) = right (sim-compR Q-Q' (right x) x\u2081)\nsim-comp () (right x) end\nsim-comp end end QR = QR\n\nsim-compRL end () QR\nsim-compRL (\u03a0\u00b7\u03a3 x\u2081) (recv x) (send x\u2082 x\u2083) = sim-comp (x\u2081 x\u2082) (x x\u2082) x\u2083\nsim-compRL (\u03a3\u00b7\u03a0 x) (send x\u2081 x\u2082) (recv x\u2083) = sim-comp (x x\u2081) x\u2082 (x\u2083 x\u2081)\n\nsim-compL Q-Q' (recv PQ) QR = recv (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR)\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv (\u03bb m \u2192 sim-comp Q-Q' PQ (QR m))\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (left x) = right (sim-symL x)\n!\u02e2 (right x) = left (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process \ud835\udfd9 P\nsim-unit (right (recv P)) = do (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (right (send m P)) = do (send m (sim-unit P))\nsim-unit end = end 0\u2081\n\nmodule _ where\n\n mod\u2081 : \u2200 {A A' B : \u2605} \u2192 (A \u2192 A') \u2192 A \u00d7 B \u2192 A' \u00d7 B\n mod\u2081 = \u03bb f \u2192 Data.Product.map f id\n\n mod\u2082 : \u2200 {A B B' : \u2605} \u2192 (B \u2192 B') \u2192 A \u00d7 B \u2192 A \u00d7 B'\n mod\u2082 = \u03bb f \u2192 Data.Product.map id f\n\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (right (send x x\u2082)) (left (recv x\u2083)) Q\u00b7 = mod\u2081 (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (right (recv x)) (left (send x\u2082 x\u2083)) Q\u00b7 = mod\u2081 (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (right (recv x)) (left (send x\u2082 x\u2083)) = mod\u2082 (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (right (send x x\u2082)) (left (recv x\u2083)) = mod\u2082 (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n sim-comp-assoc-end : \u2200 {P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (\u00f8P : Sim end P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' \u00f8P PQ) QR\n \u2261 sim-comp P-P' \u00f8P (sim-comp Q-Q' PQ QR)\n sim-comp-assoc-end (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (right (recv x)) (left (send x\u2082 x\u2083)) QR\n = sim-comp-assoc-end (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 QR\n sim-comp-assoc-end (\u03a3\u00b7\u03a0 x) Q-Q' (right (send x\u2081 x\u2082)) (left (recv x\u2083)) QR\n = sim-comp-assoc-end (x x\u2081) Q-Q' x\u2082 (x\u2083 x\u2081) QR\n sim-comp-assoc-end P-P' (\u03a0\u00b7\u03a3 x\u2081) (right \u00f8P) (right (recv x)) (left (send x\u2082 x\u2083))\n = sim-comp-assoc-end P-P' (x\u2081 x\u2082) (right \u00f8P) (x x\u2082) x\u2083\n sim-comp-assoc-end P-P' (\u03a3\u00b7\u03a0 x) (right \u00f8P) (right (send x\u2081 x\u2082)) (left (recv x\u2083))\n = sim-comp-assoc-end P-P' (x x\u2081) (right \u00f8P) x\u2082 (x\u2083 x\u2081)\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (recv x\u2081))\n = cong (right \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (x\u2081 m)))\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (send x\u2081 x\u2082))\n = cong (right \u2218 send x\u2081) (sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) x\u2082)\n sim-comp-assoc-end end Q-Q' end PQ QR = refl\n\n \u2666-assoc-end : \u2200 {P Q R}(\u00f8P : Sim end P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (\u00f8P \u2666 PQ) \u2666 QR \u2261 \u00f8P \u2666 (PQ \u2666 QR)\n \u2666-assoc-end = sim-comp-assoc-end (Dual-spec _) (Dual-spec _)\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u223c sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' WP PQ QR {W'} W'-W \u00f8W'\n = sim-comp W'-W \u00f8W' (sim-comp Q-Q' (sim-comp P-P' WP PQ) QR)\n \u2261\u27e8 sym (sim-comp-assoc-end W'-W Q-Q' \u00f8W' (sim-comp P-P' WP PQ) QR) \u27e9\n sim-comp Q-Q' (sim-comp W'-W \u00f8W' (sim-comp P-P' WP PQ)) QR\n \u2261\u27e8 cong (\u03bb X \u2192 sim-comp Q-Q' X QR) (sym (sim-comp-assoc-end W'-W P-P' \u00f8W' WP PQ)) \u27e9\n sim-comp Q-Q' (sim-comp P-P' (sim-comp W'-W \u00f8W' WP) PQ) QR\n \u2261\u27e8 sim-comp-assoc-end P-P' Q-Q' (sim-comp W'-W \u00f8W' WP) PQ QR \u27e9\n sim-comp P-P' (sim-comp W'-W \u00f8W' WP) (sim-comp Q-Q' PQ QR)\n \u2261\u27e8 sim-comp-assoc-end W'-W P-P' \u00f8W' WP (sim-comp Q-Q' PQ QR) \u27e9\n sim-comp W'-W \u00f8W' (sim-comp P-P' WP (sim-comp Q-Q' PQ QR))\n \u220e\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u223c WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n\n\u223c-\u00f8 : \u2200 {P}{s s' : Sim end P} \u2192 s \u223c s' \u2192 s \u2261 s'\n\u223c-\u00f8 s\u223cs' = s\u223cs' end end\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (left (recv x)) = cong (left \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (left (send x x\u2081)) = cong (left \u2218 send x) (sim-!! x\u2081)\n sim-!! (right (recv x)) = cong (right \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (right (send x x\u2081)) = cong (right \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (right (recv x)) (left (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (right (send x\u2081 x\u2082)) (left (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (right x) (right (recv x\u2081))\n = cong (left \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (right x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (right x) (right (send x\u2081 x\u2082))\n = cong (left \u2218 send x\u2081) (sim-comp-!-end Q-Q' (right x) x\u2082)\n sim-comp-!-end end end QR = {!!}\n\n open \u2261-Reasoning\n module _ {P Q}{s s' : Sim P Q} where\n !\u02e2-cong : s \u223c s' \u2192 !\u02e2 s \u223c !\u02e2 s'\n !\u02e2-cong s\u223cs' Q'-Q \u00f8Q'\n = sim-comp Q'-Q \u00f8Q' (!\u02e2 s)\n \u2261\u27e8 {!!} \u27e9\n sim-comp Q'-Q \u00f8Q' (!\u02e2 (sim-comp (Dual-spec Q) s (sim-id _)))\n \u2261\u27e8 {!!} \u27e9\n sim-comp Q'-Q \u00f8Q' (!\u02e2 s')\n \u220e\n\n postulate\n sim-comp-assoc-end' : \u2200 {P Q Q' R R'}(Q-Q' : Dual Q Q')(R-R' : Dual R R')\n (PQ : Sim P Q)(QR : Sim Q' R )(R\u00f8 : Sim R' end)\n \u2192 sim-comp R-R' (sim-comp Q-Q' PQ QR) R\u00f8\n \u2261 sim-comp Q-Q' PQ (sim-comp R-R' QR R\u00f8)\n\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc-end funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n -- \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc-end' Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n \u2261\u27e8 \u223c-\u00f8 {!!}\u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n{-\n\nunit-sim : \u2200 {P} \u2192 Process \ud835\udfd9 P \u2192 Sim end P\nunit-sim (do (send m x)) = right (send m (unit-sim x))\nunit-sim (do (recv x)) = right (recv (\u03bb m \u2192 unit-sim (x m)))\nunit-sim (end x) = end\n\n{-\ntoEl : \u2200 {P A} \u2192 Process A P \u2192 El P (const A)\ntoEl (end x) = x\ntoEl (do (recv f)) = \u03bb x \u2192 toEl (f x)\ntoEl (do (send m x)) = m , toEl x\n-}\n\nidP : \u2200 {A} \u2192 Process A (\u03a0' A (const end))\nidP = do (recv end)\n\ndual\u00b2 : \u2200 {P A} \u2192 Process A P \u2192 Process A (dual (dual P))\ndual\u00b2 (end x) = end x\ndual\u00b2 (do (recv x)) = do (recv (\u03bb m \u2192 dual\u00b2 (x m)))\ndual\u00b2 (do (send m x)) = do (send m (dual\u00b2 x))\n\napply-sim : \u2200 {P Q} \u2192 Sim P Q \u2192 P \u27f9\u1d3e Q\napply-sim (left (send m x)) (do (recv x\u2081)) = apply-sim x (x\u2081 m)\napply-sim (left (recv x)) (do (send m x\u2081)) = apply-sim (x m) x\u2081\napply-sim (right (send m x)) P\u2082 = do (send m (apply-sim x P\u2082))\napply-sim (right (recv x)) P\u2082 = do (recv (\u03bb m \u2192 apply-sim (x m) P\u2082))\napply-sim end P = P\n\napply-sim' : \u2200 {P Q} \u2192 Sim P Q \u2192 Q \u27f9\u1d3e P -- \u2200 {A} \u2192 Process A Q \u2192 Process A (dual P)\napply-sim' PQ P = apply-sim (sim-sym PQ) P\n\napply : \u2200 {P Q A} \u2192 P \u27f9 Q \u2192 Process A P \u2192 Process A Q\napply PQ P = apply-sim PQ (dual\u00b2 P)\n\nmodule _ (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n apply-comp : \u2200 {P Q R A}(PQ : Sim P Q)(QR : Sim (dual Q) R)(p : Process A (dual P)) \u2192 apply-sim (sim-comp PQ QR) p \u2261 apply QR (apply-sim PQ p)\n apply-comp (left (send m x)) QR (do (recv x\u2081)) = apply-comp x QR (x\u2081 m)\n apply-comp (left (recv x)) QR (do (send m x\u2081)) = apply-comp (x m) QR x\u2081\n apply-comp (right (send m x)) (left (recv x\u2081)) p = apply-comp x (x\u2081 m) p\n apply-comp (right (send m x)) (right (recv x\u2081)) p = cong (\u03bb X \u2192 do (recv X))\n (funExt (\u03bb m' \u2192 apply-comp (right (send m x)) (x\u2081 m') p))\n apply-comp (right (send m x)) (right (send m\u2081 x\u2081)) p\n rewrite apply-comp (right (send m x)) x\u2081 p = refl\n apply-comp (right (recv x)) (left (send m x\u2081)) p = apply-comp (x m) x\u2081 p\n apply-comp (right (recv x)) (right (send m x\u2081)) p\n rewrite apply-comp (right (recv x)) x\u2081 p = refl\n apply-comp (right (recv x)) (right (recv x\u2081)) p = cong (\u03bb X \u2192 do (recv X))\n (funExt (\u03bb m \u2192 apply-comp (right (recv x)) (x\u2081 m) p))\n apply-comp end QR (do ())\n apply-comp end QR (end x) = refl\n\n{-\n_>>=P_ : \u2200 {A B P}{Q : Tele P \u2192 Proto} \u2192 Process A P \u2192 ((p : Tele P) \u2192 A \u2192 Process B (Q p)) \u2192 Process B (P >>\u2261 Q)\nsend m p >>=P k = send m (p >>=P (\u03bb p\u2081 \u2192 k (m , p\u2081)))\nrecv x >>=P k = recv (\u03bb m \u2192 x m >>=P (\u03bb p \u2192 k (m , p)))\nend x >>=P k = k 0\u2081 x\n\n\n {-\nmodule _ where\n bind-com : (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (dual P) \u2192 Proto)\n (X : Tele (P >>\u2261 Q) \u2192 \u2605)(Y : Tele (dual P >>\u2261 R) \u2192 \u2605)\n \u2192 El (P >>\u2261 Q) X \u2192 El (dual P >>\u2261 R) Y \u2192 ? \u00d7 ?\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product\nopen import Data.One\n\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\ndata InOut : \u2605 where\n -- \u03a0' \u03a3' : InOut\n In Out : InOut\n\ndata Proto : \u2605\u2081 where\n end : Proto\n com : (q : InOut)(M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\npattern \u03a0' M P = com In M P\npattern \u03a3' M P = com Out M P\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0S' : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0S' M P = \u03a0' (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3S' : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3S' M P = \u03a3' (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 \u03a0' M P \u27e7 = \u03a0 M \u03bb x \u2192 \u27e6 P x \u27e7\n\u27e6 \u03a3' M P \u27e7 = \u03a3 M \u03bb x \u2192 \u27e6 P x \u27e7\n\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\n_-[_]\u2192\u00f8\u204f_ : \u2200 {I}(A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\nA -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\ndata [_\/_\u2261_] {I} : Choreo I \u2192 I \u2192 Proto \u2192 \u2605\u2081 where\n I\u03a3D : \u2200 {A B M \u2102 \u2102A} \u2192 (\u2200 m \u2192 [ \u2102 m \/ A \u2261 \u2102A m ]) \u2192 [ (A -[ M ]\u2192 B \u204f \u2102) \/ A \u2261 \u03a3' M \u2102A ]\n I\u03a0D : \u2200 {A B M \u2102 \u2102B} \u2192 (\u2200 m \u2192 [ \u2102 m \/ B \u2261 \u2102B m ]) \u2192 [ (A -[ M ]\u2192 B \u204f \u2102) \/ B \u2261 \u03a0' M \u2102B ]\n-- I\u03a0S : \u2200 {A B C M \u2102 \u2102C} \u2192 (\u2200 ..m \u2192 [ \u2102 m \/ C \u2261 \u2102C m ]) \u2192 [ (A -[ M ]\u2192 B \u204f \u2102) \/ C \u2261 \u03a0' (\u2610 M) \u2102C ]\n \u2605\u03a3D : \u2200 {A M \u2102 \u2102A} \u2192 (\u2200 m \u2192 [ \u2102 m \/ A \u2261 \u2102A m ]) \u2192 [ (A -[ M ]\u2192\u2605\u204f \u2102) \/ A \u2261 \u03a3' M \u2102A ]\n \u2605\u03a0D : \u2200 {A B M \u2102 \u2102B} \u2192 (\u2200 m \u2192 [ \u2102 m \/ B \u2261 \u2102B m ]) \u2192 [ (A -[ M ]\u2192\u2605\u204f \u2102) \/ B \u2261 \u03a0' M \u2102B ]\n --\u00f8\u03a3D : \u2200 {A M \u2102 \u2102A} \u2192 (\u2200 ..m \u2192 [ \u2102 m \/ A \u2261 \u2102A m ]) \u2192 [ (A -[ M ]\u2192\u00f8\u204f \u2102) \/ A \u2261 \u03a3' S M \u2102A ]\n --\u00f8\u03a0D : \u2200 {A B M \u2102 \u2102B} \u2192 (\u2200 ..m \u2192 [ \u2102 m \/ B \u2261 \u2102B m ]) \u2192 [ (A -[ M ]\u2192\u00f8\u204f \u2102) \/ B \u2261 \u03a0' S M \u2102B ]\n end : \u2200 {A} \u2192 [ end \/ A \u2261 end ]\n\nTrace : Proto \u2192 Proto\nTrace end = end\nTrace (com _ A B) = \u03a3' A \u03bb m \u2192 Trace (B m)\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n Trace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261 Trace P\n Trace-idempotent end = refl\n Trace-idempotent (com q M P) = cong (\u03a3' M) (funExt \u03bb m \u2192 Trace-idempotent (P m))\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>\u2261_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>\u2261 Q = Q _\ncom \u03c0 A B >>\u2261 Q = com \u03c0 A (\u03bb x \u2192 B x >>\u2261 (\u03bb xs \u2192 Q (x , xs)))\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>\u2261 \u03bb _ \u2192 Q\n\n++Tele : \u2200 (P : Proto)(Q : Tele P \u2192 Proto) \u2192 (x : Tele P) \u2192 Tele (Q x) \u2192 Tele (P >>\u2261 Q)\n++Tele end Q x y = y\n++Tele (\u03a0' M C) Q (m , x) y = m , ++Tele (C m) (\u03bb x\u2081 \u2192 Q (m , x\u2081)) x y\n++Tele (\u03a3' M C) Q (m , x) y = m , ++Tele (C m) _ x y\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n right-unit : \u2200 (P : Proto) \u2192 (P >>\u2261 \u03bb x \u2192 end) \u2261 P\n right-unit end = refl\n right-unit (\u03a0' M C) = let p = funExt (\u03bb x \u2192 right-unit (C x)) in cong (\u03a0' M) p\n right-unit (\u03a3' M C) = cong (\u03a3' M) (funExt (\u03bb x \u2192 right-unit (C x)))\n\n assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>\u2261 Q) \u2192 Proto)\n \u2192 P >>\u2261 (\u03bb x \u2192 Q x >>\u2261 (\u03bb y \u2192 R (++Tele P Q x y))) \u2261 ((P >>\u2261 Q) >>\u2261 R)\n assoc end Q R = refl\n assoc (\u03a0' M C\u2081) Q R = cong (\u03a0' M) (funExt (\u03bb x \u2192 assoc (C\u2081 x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb xs \u2192 R (x , xs))))\n assoc (\u03a3' M C\u2081) Q R = cong (\u03a3' M) (funExt (\u03bb x \u2192 assoc (C\u2081 x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb xs \u2192 R (x , xs))))\n\n_\u00d7'_ : Set \u2192 Proto \u2192 Proto\nA \u00d7' B = \u03a3' A \u03bb _ \u2192 B\n\n_\u2192'_ : Set \u2192 Proto \u2192 Proto\nA \u2192' B = \u03a0' A \u03bb _ \u2192 B\n\ndualInOut : InOut \u2192 InOut\ndualInOut In = Out\ndualInOut Out = In\n\ndual : Proto \u2192 Proto\ndual end = end\ndual (com q A B) = com (dualInOut q) A (\u03bb x \u2192 dual (B x))\n\ndata DualInOut : InOut \u2192 InOut \u2192 \u2605 where\n DInOut : DualInOut In Out\n DOutIn : DualInOut Out In\n\ndata Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {A B B'} \u2192 (\u2200 x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a0' A B) (\u03a3' A B')\n \u03a3\u00b7\u03a0 : \u2200 {A B B'} \u2192 (\u2200 x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a3' A B) (\u03a0' A B')\n\ndata Sing {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n sing : \u2200 x \u2192 Sing x\n\n-- Commit = \u03bb M R \u2192 \u03a3' D (\u2610 M) \u03bb { [ m ] \u2192 \u03a0' D R \u03bb r \u2192 \u03a3' D (Sing m) (\u03bb _ \u2192 end) }\nCommit = \u03bb M R \u2192 \u03a3S' M \u03bb m \u2192 \u03a0' R \u03bb r \u2192 \u03a3' (Sing m) (\u03bb _ \u2192 end)\n\ncommit : \u2200 M R (m : M) \u2192 \u27e6 Commit M R \u27e7\ncommit M R m = [ m ] , (\u03bb x \u2192 (sing m) , _)\n\ndecommit : \u2200 M R (r : R) \u2192 \u27e6 dual (Commit M R) \u27e7\ndecommit M R r = \u03bb { [ m ] \u2192 r , (\u03bb x \u2192 {!!}) }\n\ndata [_&_\u2261_]InOut : InOut \u2192 InOut \u2192 InOut \u2192 \u2605\u2081 where\n \u03a0XX : \u2200 {X} \u2192 [ In & X \u2261 X ]InOut\n X\u03a0X : \u2200 {X} \u2192 [ X & In \u2261 X ]InOut\n\n&InOut-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]InOut \u2192 [ Q & P \u2261 R ]InOut\n&InOut-comm \u03a0XX = X\u03a0X\n&InOut-comm X\u03a0X = \u03a0XX\n\n {-\ndata [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n end& : \u2200 {P} \u2192 [ end & P \u2261 P ]\n &end : \u2200 {P} \u2192 [ P & end \u2261 P ]\n XXX : \u2200 {qP qQ qR M P Q R}(q : [ qP & qQ \u2261 qR ]InOut)(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ com qP M P & com qQ M Q \u2261 com qR M R ]\n -- SDD : \u2200 {qP qQ qR M P Q R}(q : [ qP & qQ \u2261 qR ]InOut)(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ com qP (\u2610 M) P & com qQ M Q \u2261 com qR M R ]\n DSD : \u2200 {qP qQ qR M P Q R}(q : [ qP & qQ \u2261 qR ]InOut)(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ com qP M P & com qQ (\u2610 M) Q \u2261 com qR M R ]\n\n&-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n&-comm end& = &end\n&-comm &end = end&\n&-comm (XXX q P&) = XXX (&InOut-comm q) (\u03bb m \u2192 &-comm (P& m))\n&-comm (SDD q P&) = DSD (&InOut-comm q) (\u03bb m \u2192 &-comm (P& m))\n&-comm (DSD q P&) = SDD (&InOut-comm q) (\u03bb m \u2192 &-comm (P& m))\n-}\n\nDualInOut-sym : \u2200 {P Q} \u2192 DualInOut P Q \u2192 DualInOut Q P\nDualInOut-sym DInOut = DOutIn\nDualInOut-sym DOutIn = DInOut\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n\nDualInOut-spec : \u2200 P \u2192 DualInOut P (dualInOut P)\nDualInOut-spec In = DInOut\nDualInOut-spec Out = DOutIn\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (com In M P) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-spec (P x))\nDual-spec (com Out M P) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-spec (P x))\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele P \u2261 Tele (dual P)\n dual-Tele end = refl\n dual-Tele (\u03a0' A B) = cong (\u03a3 A) (funExt (\u03bb x \u2192 dual-Tele (B x)))\n dual-Tele (\u03a3' A B) = cong (\u03a3 A) (funExt (\u03bb x \u2192 dual-Tele (B x)))\n dual-Tele (later i P) = ?\n-}{-\nmodule _ where\n El : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\n El end X = X _\n El (\u03a0' A B) X = \u03a0 A \u03bb x \u2192 El (B x) (\u03bb y \u2192 X (x , y))\n El (\u03a3' A B) X = \u03a3 A \u03bb x \u2192 El (B x) (\u03bb y \u2192 X (x , y))\n El (later i P) = ?\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n bind-spec : (P : Proto)(Q : Tele P \u2192 Proto)(X : Tele (P >>\u2261 Q) \u2192 \u2605) \u2192 El (P >>\u2261 Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P Q x y)))\n bind-spec end Q X = refl\n bind-spec (\u03a0' A B) Q X = cong (\u03a0 A) (funExt (\u03bb x \u2192 bind-spec (B x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb y \u2192 X (x , y))))\n bind-spec (\u03a3' A B) Q X = cong (\u03a3 A) (funExt (\u03bb x \u2192 bind-spec (B x) _ _))\n bind-spec (later i p) Q X = ?\n\n\nmodule _ {A B} where\n com : (P : Proto) \u2192 El P (const A) \u2192 El (dual P) (const B) \u2192 A \u00d7 B\n com end a b = a , b\n com (\u03a0' A B) f (x , y) = com (B x) (f x) y\n com (\u03a3' A B) (x , y) f = com (B x) y (f x)\n com (later i P) x y = ?\n\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n com-cont : (P : Proto){A : Tele P \u2192 \u2605}{B : Tele (dual P) \u2192 \u2605} \u2192 El P A \u2192 El (dual P) B \u2192 \u03a3 (Tele P) A \u00d7 \u03a3 (Tele (dual P)) B\n com-cont end p q = (_ , p) , (_ , q)\n com-cont (\u03a0' A B) p (m , q) with com-cont (B m) (p m) q\n ... | (X , a) , (Y , b) = ((m , X) , a) , (m , Y) , b\n com-cont (\u03a3' A B) (m , p) q with com-cont (B m) p (q m)\n ... | (X , a) , (Y , b) = ((m , X) , a) , (m , Y) , b\n com-cont (later i P) p q = ?\n-}\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Proto \u2192 \u2605\u2081 where\n recv : \u2200 {M P} \u2192 ((m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0' M P)\n send : \u2200 {M P} (m : M) \u2192 this (P m) \u2192 ProcessF this (\u03a3' M P)\n\n{-\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M P} \u2192 (..(m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0' (\u2610 M) P)\nrecvS = ?\nsendS : \u2200 {M P} \u2192 \u03a0M S M (\u03bb m \u2192 this (P m) \u2192 ProcessF this (\u03a3' S M P))\nsendS = ?\n-}\n\ndata Process (A : \u2605) : Proto \u2192 \u2605\u2081 where\n do : \u2200 {P} \u2192 ProcessF (Process A) P \u2192 Process A P\n end : A \u2192 Process A end\n\nmutual\n SimL : Proto \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Proto \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n left : \u2200 {q M P Q} \u2192 SimL (com q M P) Q \u2192 Sim (com q M P) Q\n right : \u2200 {P q M Q} \u2192 SimR P (com q M Q) \u2192 Sim P (com q M Q)\n end : Sim end end\n\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = \u2200 {A} \u2192 Process A (dual P) \u2192 Process A Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& P& (right \u00b7PA) (right \u00b7PB) = {! (sim&R P& \u00b7PA \u00b7PB)!}\nsim& end& PA PB = PB\nsim& &end PA PB = PA\n\nsim&R (\u03a0D\u03a3D\u03a3S P&) (recv PQA) (send m PQB) = sendS m (sim& (P& m) (PQA m) PQB)\nsim&R (\u03a0D\u03a0D\u03a0D P&) (recv PQA) (recv PQB) = recv \u03bb m \u2192 sim& (P& m) (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) (recvS PQA) (recvS PQB) = recvS \u03bb m \u2192 sim& (P& m) (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) (recvS PQA) (sendS m PQB) = sendS m (sim& (P& m) (PQA m) PQB)\nsim&R (\u03a0S-D-D P&) (recvS PQA) (recv PQB) = recv \u03bb m \u2192 sim& (P& m) (PQA m) (PQB m)\nsim&R (\u03a0S-D-D P&) (recvS PQA) (send m PQB) = send m (sim& (P& m) (PQA m) PQB)\nsim&R (\u03a3D\u03a0D\u03a3S P&) (send m PQA) (recv PQB) = sendS m (sim& (P& m) PQA (PQB m))\nsim&R &end _ ()\n-}\n\n{-\nsim&R (\u03a3D\u03a0D\u03a3S x) (send x\u2081 x\u2082) (recv x\u2083) = {!!}\n-}\n\n{-\nsim& : \u2200 {PA PB PAB QA QB QAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 [ QA & QB \u2261 QAB ] \u2192 Sim PA QA \u2192 Sim PB QB \u2192 Sim PAB QAB\nsim&L : \u2200 {PA PB PAB QA QB QAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 [ QA & QB \u2261 QAB ] \u2192 SimL PA QA \u2192 SimL PB QB \u2192 SimL PAB QAB\nsim&R : \u2200 {PA PB PAB QA QB QAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 [ QA & QB \u2261 QAB ] \u2192 SimR PA QA \u2192 SimR PB QB \u2192 SimR PAB QAB\n\nsim& P& Q& (left PQA) (left PQB) = left (sim&L P& Q& PQA PQB)\nsim& P& Q& (left PQA) (right PQB) = {!!}\nsim& P& Q& (left PQA) end = {!!}\nsim& P& Q& (right x) PQB = {!!}\nsim& P& Q& end PQB = {!!}\n\nsim&L (\u03a0D\u03a3D\u03a3S P&) Q& (recv PQA) (send m PQB) = sendS m (sim& (P& m) Q& (PQA m) PQB)\nsim&L (\u03a0D\u03a0D\u03a0D P&) Q& (recv PQA) (recv PQB) = recv \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&L (\u03a0S-S-S P&) Q& (recvS PQA) (recvS PQB) = recvS \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&L (\u03a0S-S-S P&) Q& (recvS PQA) (sendS m PQB) = sendS m (sim& (P& m) Q& (PQA m) PQB)\nsim&L (\u03a0S-D-D P&) Q& (recvS PQA) (recv PQB) = recv \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&L (\u03a0S-D-D P&) Q& (recvS PQA) (send m PQB) = send m (sim& (P& m) Q& (PQA m) PQB)\nsim&L (\u03a3D\u03a0D\u03a3S P&) Q& (send m PQA) (recv PQB) = sendS m (sim& (P& m) Q& PQA (PQB m))\nsim&L &end Q& PQA ()\n\n{-\nsim&R (\u03a0D\u03a3D\u03a3S P&) Q& (recv PQA) (send m PQB) = ?\nsim&R (\u03a0D\u03a0D\u03a0D P&) Q& (recv PQA) (recv PQB) = ? -- recv \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) Q& (recvS PQA) (recvS PQB) = ? -- recvS \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) Q& (recvS PQA) (sendS m PQB) = ? -- sendS m (sim& (P& m) Q& (PQA m) PQB)\nsim&R (\u03a0S-D-D P&) Q& (recvS PQA) (recv PQB) = ? -- recv \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&R (\u03a0S-D-D P&) Q& (recvS PQA) (send m PQB) = ? -- send m (sim& (P& m) Q& (PQA m) PQB)\nsim&R (\u03a3D\u03a0D\u03a3S P&) Q& (send m PQA) (recv PQB) = ? -- sendS m (sim& (P& m) Q& PQA (PQB m))\nsim&R end& Q& PQA PQB = ?\n-}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0' A B) = right (recv (\u03bb x \u2192 left (send x (sim-id (B x)))))\nsim-id (\u03a3' A B) = left (recv (\u03bb x \u2192 right (send x (sim-id (B x)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (left x) QR = left (sim-compL Q-Q' x QR)\nsim-comp Q-Q' (right x) (left x\u2081) = sim-compRL Q-Q' x x\u2081\nsim-comp Q-Q' (right x) (right x\u2081) = right (sim-compR Q-Q' (right x) x\u2081)\nsim-comp () (right x) end\nsim-comp end end QR = QR\n\nsim-compRL end () QR\nsim-compRL (\u03a0\u00b7\u03a3 x\u2081) (recv x) (send x\u2082 x\u2083) = sim-comp (x\u2081 x\u2082) (x x\u2082) x\u2083\nsim-compRL (\u03a3\u00b7\u03a0 x) (send x\u2081 x\u2082) (recv x\u2083) = sim-comp (x x\u2081) x\u2082 (x\u2083 x\u2081)\n\nsim-compL Q-Q' (recv PQ) QR = recv (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR)\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv (\u03bb m \u2192 sim-comp Q-Q' PQ (QR m))\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (left x) = right (sim-symL x)\n!\u02e2 (right x) = left (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process \ud835\udfd9 P\nsim-unit (right (recv P)) = do (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (right (send m P)) = do (send m (sim-unit P))\nsim-unit end = end 0\u2081\n\nmodule _ where\n\n mod\u2081 : \u2200 {A A' B : \u2605} \u2192 (A \u2192 A') \u2192 A \u00d7 B \u2192 A' \u00d7 B\n mod\u2081 = \u03bb f \u2192 Data.Product.map f id\n\n mod\u2082 : \u2200 {A B B' : \u2605} \u2192 (B \u2192 B') \u2192 A \u00d7 B \u2192 A \u00d7 B'\n mod\u2082 = \u03bb f \u2192 Data.Product.map id f\n\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (right (send x x\u2082)) (left (recv x\u2083)) Q\u00b7 = mod\u2081 (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (right (recv x)) (left (send x\u2082 x\u2083)) Q\u00b7 = mod\u2081 (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (right (recv x)) (left (send x\u2082 x\u2083)) = mod\u2082 (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (right (send x x\u2082)) (left (recv x\u2083)) = mod\u2082 (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n sim-comp-assoc-end : \u2200 {P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (\u00f8P : Sim end P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' \u00f8P PQ) QR\n \u2261 sim-comp P-P' \u00f8P (sim-comp Q-Q' PQ QR)\n sim-comp-assoc-end (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (right (recv x)) (left (send x\u2082 x\u2083)) QR\n = sim-comp-assoc-end (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 QR\n sim-comp-assoc-end (\u03a3\u00b7\u03a0 x) Q-Q' (right (send x\u2081 x\u2082)) (left (recv x\u2083)) QR\n = sim-comp-assoc-end (x x\u2081) Q-Q' x\u2082 (x\u2083 x\u2081) QR\n sim-comp-assoc-end P-P' (\u03a0\u00b7\u03a3 x\u2081) (right \u00f8P) (right (recv x)) (left (send x\u2082 x\u2083))\n = sim-comp-assoc-end P-P' (x\u2081 x\u2082) (right \u00f8P) (x x\u2082) x\u2083\n sim-comp-assoc-end P-P' (\u03a3\u00b7\u03a0 x) (right \u00f8P) (right (send x\u2081 x\u2082)) (left (recv x\u2083))\n = sim-comp-assoc-end P-P' (x x\u2081) (right \u00f8P) x\u2082 (x\u2083 x\u2081)\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (recv x\u2081))\n = cong (right \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (x\u2081 m)))\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (send x\u2081 x\u2082))\n = cong (right \u2218 send x\u2081) (sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) x\u2082)\n sim-comp-assoc-end end Q-Q' end PQ QR = refl\n\n \u2666-assoc-end : \u2200 {P Q R}(\u00f8P : Sim end P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (\u00f8P \u2666 PQ) \u2666 QR \u2261 \u00f8P \u2666 (PQ \u2666 QR)\n \u2666-assoc-end = sim-comp-assoc-end (Dual-spec _) (Dual-spec _)\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u223c sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' WP PQ QR {W'} W'-W \u00f8W'\n = sim-comp W'-W \u00f8W' (sim-comp Q-Q' (sim-comp P-P' WP PQ) QR)\n \u2261\u27e8 sym (sim-comp-assoc-end W'-W Q-Q' \u00f8W' (sim-comp P-P' WP PQ) QR) \u27e9\n sim-comp Q-Q' (sim-comp W'-W \u00f8W' (sim-comp P-P' WP PQ)) QR\n \u2261\u27e8 cong (\u03bb X \u2192 sim-comp Q-Q' X QR) (sym (sim-comp-assoc-end W'-W P-P' \u00f8W' WP PQ)) \u27e9\n sim-comp Q-Q' (sim-comp P-P' (sim-comp W'-W \u00f8W' WP) PQ) QR\n \u2261\u27e8 sim-comp-assoc-end P-P' Q-Q' (sim-comp W'-W \u00f8W' WP) PQ QR \u27e9\n sim-comp P-P' (sim-comp W'-W \u00f8W' WP) (sim-comp Q-Q' PQ QR)\n \u2261\u27e8 sim-comp-assoc-end W'-W P-P' \u00f8W' WP (sim-comp Q-Q' PQ QR) \u27e9\n sim-comp W'-W \u00f8W' (sim-comp P-P' WP (sim-comp Q-Q' PQ QR))\n \u220e\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u223c WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n\n\u223c-\u00f8 : \u2200 {P}{s s' : Sim end P} \u2192 s \u223c s' \u2192 s \u2261 s'\n\u223c-\u00f8 s\u223cs' = s\u223cs' end end\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (left (recv x)) = cong (left \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (left (send x x\u2081)) = cong (left \u2218 send x) (sim-!! x\u2081)\n sim-!! (right (recv x)) = cong (right \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (right (send x x\u2081)) = cong (right \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (right (recv x)) (left (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (right (send x\u2081 x\u2082)) (left (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (right x) (right (recv x\u2081))\n = cong (left \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (right x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (right x) (right (send x\u2081 x\u2082))\n = cong (left \u2218 send x\u2081) (sim-comp-!-end Q-Q' (right x) x\u2082)\n sim-comp-!-end end end QR = {!!}\n\n open \u2261-Reasoning\n module _ {P Q}{s s' : Sim P Q} where\n !\u02e2-cong : s \u223c s' \u2192 !\u02e2 s \u223c !\u02e2 s'\n !\u02e2-cong s\u223cs' Q'-Q \u00f8Q'\n = sim-comp Q'-Q \u00f8Q' (!\u02e2 s)\n \u2261\u27e8 {!!} \u27e9\n sim-comp Q'-Q \u00f8Q' (!\u02e2 (sim-comp (Dual-spec Q) s (sim-id _)))\n \u2261\u27e8 {!!} \u27e9\n sim-comp Q'-Q \u00f8Q' (!\u02e2 s')\n \u220e\n\n postulate\n sim-comp-assoc-end' : \u2200 {P Q Q' R R'}(Q-Q' : Dual Q Q')(R-R' : Dual R R')\n (PQ : Sim P Q)(QR : Sim Q' R )(R\u00f8 : Sim R' end)\n \u2192 sim-comp R-R' (sim-comp Q-Q' PQ QR) R\u00f8\n \u2261 sim-comp Q-Q' PQ (sim-comp R-R' QR R\u00f8)\n\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc-end funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n -- \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc-end' Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n \u2261\u27e8 \u223c-\u00f8 {!!}\u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n{-\n\nunit-sim : \u2200 {P} \u2192 Process \ud835\udfd9 P \u2192 Sim end P\nunit-sim (do (send m x)) = right (send m (unit-sim x))\nunit-sim (do (recv x)) = right (recv (\u03bb m \u2192 unit-sim (x m)))\nunit-sim (end x) = end\n\n{-\ntoEl : \u2200 {P A} \u2192 Process A P \u2192 El P (const A)\ntoEl (end x) = x\ntoEl (do (recv f)) = \u03bb x \u2192 toEl (f x)\ntoEl (do (send m x)) = m , toEl x\n-}\n\nidP : \u2200 {A} \u2192 Process A (\u03a0' A (const end))\nidP = do (recv end)\n\ndual\u00b2 : \u2200 {P A} \u2192 Process A P \u2192 Process A (dual (dual P))\ndual\u00b2 (end x) = end x\ndual\u00b2 (do (recv x)) = do (recv (\u03bb m \u2192 dual\u00b2 (x m)))\ndual\u00b2 (do (send m x)) = do (send m (dual\u00b2 x))\n\napply-sim : \u2200 {P Q} \u2192 Sim P Q \u2192 P \u27f9\u1d3e Q\napply-sim (left (send m x)) (do (recv x\u2081)) = apply-sim x (x\u2081 m)\napply-sim (left (recv x)) (do (send m x\u2081)) = apply-sim (x m) x\u2081\napply-sim (right (send m x)) P\u2082 = do (send m (apply-sim x P\u2082))\napply-sim (right (recv x)) P\u2082 = do (recv (\u03bb m \u2192 apply-sim (x m) P\u2082))\napply-sim end P = P\n\napply-sim' : \u2200 {P Q} \u2192 Sim P Q \u2192 Q \u27f9\u1d3e P -- \u2200 {A} \u2192 Process A Q \u2192 Process A (dual P)\napply-sim' PQ P = apply-sim (sim-sym PQ) P\n\napply : \u2200 {P Q A} \u2192 P \u27f9 Q \u2192 Process A P \u2192 Process A Q\napply PQ P = apply-sim PQ (dual\u00b2 P)\n\nmodule _ (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n apply-comp : \u2200 {P Q R A}(PQ : Sim P Q)(QR : Sim (dual Q) R)(p : Process A (dual P)) \u2192 apply-sim (sim-comp PQ QR) p \u2261 apply QR (apply-sim PQ p)\n apply-comp (left (send m x)) QR (do (recv x\u2081)) = apply-comp x QR (x\u2081 m)\n apply-comp (left (recv x)) QR (do (send m x\u2081)) = apply-comp (x m) QR x\u2081\n apply-comp (right (send m x)) (left (recv x\u2081)) p = apply-comp x (x\u2081 m) p\n apply-comp (right (send m x)) (right (recv x\u2081)) p = cong (\u03bb X \u2192 do (recv X))\n (funExt (\u03bb m' \u2192 apply-comp (right (send m x)) (x\u2081 m') p))\n apply-comp (right (send m x)) (right (send m\u2081 x\u2081)) p\n rewrite apply-comp (right (send m x)) x\u2081 p = refl\n apply-comp (right (recv x)) (left (send m x\u2081)) p = apply-comp (x m) x\u2081 p\n apply-comp (right (recv x)) (right (send m x\u2081)) p\n rewrite apply-comp (right (recv x)) x\u2081 p = refl\n apply-comp (right (recv x)) (right (recv x\u2081)) p = cong (\u03bb X \u2192 do (recv X))\n (funExt (\u03bb m \u2192 apply-comp (right (recv x)) (x\u2081 m) p))\n apply-comp end QR (do ())\n apply-comp end QR (end x) = refl\n\n{-\n_>>=P_ : \u2200 {A B P}{Q : Tele P \u2192 Proto} \u2192 Process A P \u2192 ((p : Tele P) \u2192 A \u2192 Process B (Q p)) \u2192 Process B (P >>\u2261 Q)\nsend m p >>=P k = send m (p >>=P (\u03bb p\u2081 \u2192 k (m , p\u2081)))\nrecv x >>=P k = recv (\u03bb m \u2192 x m >>=P (\u03bb p \u2192 k (m , p)))\nend x >>=P k = k 0\u2081 x\n\n\n {-\nmodule _ where\n bind-com : (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (dual P) \u2192 Proto)\n (X : Tele (P >>\u2261 Q) \u2192 \u2605)(Y : Tele (dual P >>\u2261 R) \u2192 \u2605)\n \u2192 El (P >>\u2261 Q) X \u2192 El (dual P >>\u2261 R) Y \u2192 ? \u00d7 ?\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"18227b35dbe96a254f64b4a6adfb56b2f345d4ff","subject":"Cosmetic changes.","message":"Cosmetic changes.\n\nIgnore-this: b556b22f21ca044a6c7ddf7e1b7f84c5\n\ndarcs-hash:20120105125316-3bd4e-49c60ec209442b07dc5649ba80697225c37662ac.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Program\/McCarthy91\/Properties\/MainATP.agda","new_file":"src\/FOTC\/Program\/McCarthy91\/Properties\/MainATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Main properties of the McCarthy 91 function\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- The main properties proved of the McCarthy 91 function (called\n-- mc91) are\n\n-- 1. The function always terminates.\n-- 2. For all n, n < mc91 n + 11.\n-- 3. For all n > 100, then mc91 n = n - 10.\n-- 4. For all n <= 100, then mc91 n = 91.\n\n-- N.B This module does not contain combined proofs, but it imports\n-- modules which contain combined proofs.\n\nmodule FOTC.Program.McCarthy91.Properties.MainATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers.TotalityATP\nopen import FOTC.Program.McCarthy91.ArithmeticATP\nopen import FOTC.Program.McCarthy91.McCarthy91\nopen import FOTC.Program.McCarthy91.MCR\nopen import FOTC.Program.McCarthy91.MCR.LT2MCR-ATP\nopen import FOTC.Program.McCarthy91.MCR.Induction.Acc.WellFoundedInductionATP\nopen import FOTC.Program.McCarthy91.Properties.AuxiliaryATP\n\n------------------------------------------------------------------------------\n\nmc91-N-ineq : \u2200 {n} \u2192 N n \u2192 N (mc91 n) \u2227 LT n (mc91 n + eleven)\nmc91-N-ineq = wfInd-MCR P mc91-N-ineq-aux\n where\n P : D \u2192 Set\n P d = N (mc91 d) \u2227 LT d (mc91 d + eleven)\n\n mc91-N-ineq-aux : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 MCR k m \u2192 P k) \u2192 P m\n mc91-N-ineq-aux {m} Nm f with x>y\u2228x\u2264y Nm 100-N\n ... | inj\u2081 m>100 = ( Nmc91>100 Nm m>100 , x100 Nm m>100 )\n ... | inj\u2082 m\u2264100 =\n let Nm+11 : N (m + eleven)\n Nm+11 = x+11-N Nm\n\n ih1 : P (m + eleven)\n ih1 = f Nm+11 (LT2MCR (x+11-N Nm) Nm m\u2264100 (xy\u2228x\u226fy Nm 100-N\n ... | inj\u2081 m>100 = inj\u2081 ( m>100 , mc91-eq-aux m m>100 )\n ... | inj\u2082 m\u226f100 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 99-N m\u226f100\n ... | inj\u2082 m\u2261100 = inj\u2082 ( m\u226f100 , mc91-res-100' m\u2261100 )\n ... | inj\u2081 m\u226f99 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 98-N m\u226f99\n ... | inj\u2082 m\u226199 = inj\u2082 ( m\u226f100 , mc91-res-99' m\u226199 )\n ... | inj\u2081 m\u226f98 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 97-N m\u226f98\n ... | inj\u2082 m\u226198 = inj\u2082 ( m\u226f100 , mc91-res-98' m\u226198 )\n ... | inj\u2081 m\u226f97 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 96-N m\u226f97\n ... | inj\u2082 m\u226197 = inj\u2082 ( m\u226f100 , mc91-res-97' m\u226197 )\n ... | inj\u2081 m\u226f96 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 95-N m\u226f96\n ... | inj\u2082 m\u226196 = inj\u2082 ( m\u226f100 , mc91-res-96' m\u226196 )\n ... | inj\u2081 m\u226f95 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 94-N m\u226f95\n ... | inj\u2082 m\u226195 = inj\u2082 ( m\u226f100 , mc91-res-95' m\u226195 )\n ... | inj\u2081 m\u226f94 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 93-N m\u226f94\n ... | inj\u2082 m\u226194 = inj\u2082 ( m\u226f100 , mc91-res-94' m\u226194 )\n ... | inj\u2081 m\u226f93 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 92-N m\u226f93\n ... | inj\u2082 m\u226193 = inj\u2082 ( m\u226f100 , mc91-res-93' m\u226193 )\n ... | inj\u2081 m\u226f92 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 91-N m\u226f92\n ... | inj\u2082 m\u226192 = inj\u2082 ( m\u226f100 , mc91-res-92' m\u226192 )\n ... | inj\u2081 m\u226f91 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 90-N m\u226f91\n ... | inj\u2082 m\u226191 = inj\u2082 ( m\u226f100 , mc91-res-91' m\u226191 )\n ... | inj\u2081 m\u226f90 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 89-N m\u226f90\n ... | inj\u2082 m\u226190 = inj\u2082 ( m\u226f100 , mc91-res-90' m\u226190 )\n ... | inj\u2081 m\u226f89 = inj\u2082 ( m\u226f100 , mc91-res-m\u226f89 )\n where\n m\u2264100 : LE m one-hundred\n m\u2264100 = x\u226fy\u2192x\u2264y Nm 100-N m\u226f100\n\n m\u226489 : LE m eighty-nine\n m\u226489 = x\u226fy\u2192x\u2264y Nm 89-N m\u226f89\n\n mc91-res-m+11 : mc91 (m + eleven) \u2261 ninety-one\n mc91-res-m+11 with f (x+11-N Nm) (LT2MCR (x+11-N Nm) Nm m\u2264100 (x100 , _ ) = \u22a5-elim (x\u226489\u2192x+11>100\u2192\u22a5 Nm m\u226489 m+11>100)\n ... | inj\u2082 ( _ , res ) = res\n\n mc91-res-m\u226f89 : mc91 m \u2261 ninety-one\n mc91-res-m\u226f89 = mc91x-res\u226f100 m ninety-one m\u226f100 mc91-res-m+11 mc91-res-91\n\n------------------------------------------------------------------------------\n-- Main properties\n\n-- The function always terminates.\nmc91-N : \u2200 {n} \u2192 N n \u2192 N (mc91 n)\nmc91-N Nn = \u2227-proj\u2081 (mc91-N-ineq Nn)\n\n-- For all n, n < mc91 n + 11.\nmc91-ineq : \u2200 {n} \u2192 N n \u2192 LT n (mc91 n + eleven)\nmc91-ineq Nn = \u2227-proj\u2082 (mc91-N-ineq Nn)\n\n-- For all n > 100, then mc91 n = n - 10.\nmc91-res>100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192 mc91 n \u2261 n \u2238 ten\nmc91-res>100 Nn n>100 with mc91-res Nn\n... | inj\u2081 ( _ , res ) = res\n... | inj\u2082 ( n\u226f100 , _ ) = \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nn 100-N n>100\n (x\u226fy\u2192x\u2264y Nn 100-N n\u226f100))\n-- For all n <= 100, then mc91 n = 91.\nmc91-res\u226f100 : \u2200 {n} \u2192 N n \u2192 NGT n one-hundred \u2192 mc91 n \u2261 ninety-one\nmc91-res\u226f100 Nn n\u226f100 with mc91-res Nn\n... | inj\u2081 ( n>100 , _ ) = \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nn 100-N n>100\n (x\u226fy\u2192x\u2264y Nn 100-N n\u226f100))\n... | inj\u2082 ( _ , res ) = res\n","old_contents":"------------------------------------------------------------------------------\n-- Main properties of the McCarthy 91 function\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- The main properties proved of the McCarthy 91 function (called\n-- mc91) are\n\n-- 1. The function always terminates.\n-- 2. For all n, n < mc91 n + 11.\n-- 3. For all n > 100, then mc91 n = n - 10.\n-- 4. For all n <= 100, then mc91 n = 91.\n\n-- N.B This module does not contain combined proofs, but it imports\n-- modules which contain combined proofs.\n\nmodule FOTC.Program.McCarthy91.Properties.MainATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers.TotalityATP\nopen import FOTC.Program.McCarthy91.ArithmeticATP\nopen import FOTC.Program.McCarthy91.McCarthy91\nopen import FOTC.Program.McCarthy91.MCR\nopen import FOTC.Program.McCarthy91.MCR.LT2MCR-ATP\nopen import FOTC.Program.McCarthy91.MCR.Induction.Acc.WellFoundedInductionATP\nopen import FOTC.Program.McCarthy91.Properties.AuxiliaryATP\n\n------------------------------------------------------------------------------\n\nmc91-N-ineq : \u2200 {n} \u2192 N n \u2192 N (mc91 n) \u2227 LT n (mc91 n + eleven)\nmc91-N-ineq = wfInd-MCR P mc91-N-ineq-aux\n where\n P : D \u2192 Set\n P d = N (mc91 d) \u2227 LT d (mc91 d + eleven)\n\n mc91-N-ineq-aux : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 MCR k m \u2192 P k) \u2192 P m\n mc91-N-ineq-aux {m} Nm f with x>y\u2228x\u2264y Nm 100-N\n ... | inj\u2081 m>100 = ( Nmc91>100 Nm m>100 , x100 Nm m>100 )\n ... | inj\u2082 m\u2264100 =\n let Nm+11 : N (m + eleven)\n Nm+11 = x+11-N Nm\n\n ih1 : P (m + eleven)\n ih1 = f Nm+11 (LT2MCR (x+11-N Nm) Nm m\u2264100 (xy\u2228x\u226fy Nm 100-N\n ... | inj\u2081 m>100 = inj\u2081 ( m>100 , mc91-eq-aux m m>100 )\n ... | inj\u2082 m\u226f100 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 99-N m\u226f100\n ... | inj\u2082 m\u2261100 = inj\u2082 ( m\u226f100 , mc91-res-100' m\u2261100 )\n ... | inj\u2081 m\u226f99 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 98-N m\u226f99\n ... | inj\u2082 m\u226199 = inj\u2082 ( m\u226f100 , mc91-res-99' m\u226199 )\n ... | inj\u2081 m\u226f98 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 97-N m\u226f98\n ... | inj\u2082 m\u226198 = inj\u2082 ( m\u226f100 , mc91-res-98' m\u226198 )\n ... | inj\u2081 m\u226f97 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 96-N m\u226f97\n ... | inj\u2082 m\u226197 = inj\u2082 ( m\u226f100 , mc91-res-97' m\u226197 )\n ... | inj\u2081 m\u226f96 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 95-N m\u226f96\n ... | inj\u2082 m\u226196 = inj\u2082 ( m\u226f100 , mc91-res-96' m\u226196 )\n ... | inj\u2081 m\u226f95 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 94-N m\u226f95\n ... | inj\u2082 m\u226195 = inj\u2082 ( m\u226f100 , mc91-res-95' m\u226195 )\n ... | inj\u2081 m\u226f94 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 93-N m\u226f94\n ... | inj\u2082 m\u226194 = inj\u2082 ( m\u226f100 , mc91-res-94' m\u226194 )\n ... | inj\u2081 m\u226f93 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 92-N m\u226f93\n ... | inj\u2082 m\u226193 = inj\u2082 ( m\u226f100 , mc91-res-93' m\u226193 )\n ... | inj\u2081 m\u226f92 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 91-N m\u226f92\n ... | inj\u2082 m\u226192 = inj\u2082 ( m\u226f100 , mc91-res-92' m\u226192 )\n ... | inj\u2081 m\u226f91 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 90-N m\u226f91\n ... | inj\u2082 m\u226191 = inj\u2082 ( m\u226f100 , mc91-res-91' m\u226191 )\n ... | inj\u2081 m\u226f90 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 89-N m\u226f90\n ... | inj\u2082 m\u226190 = inj\u2082 ( m\u226f100 , mc91-res-90' m\u226190 )\n ... | inj\u2081 m\u226f89 = inj\u2082 ( m\u226f100 , mc91-res-m\u226f89 )\n\n where\n m\u2264100 : LE m one-hundred\n m\u2264100 = x\u226fy\u2192x\u2264y Nm 100-N m\u226f100\n\n m\u226489 : LE m eighty-nine\n m\u226489 = x\u226fy\u2192x\u2264y Nm 89-N m\u226f89\n\n mc91-res-m+11 : mc91 (m + eleven) \u2261 ninety-one\n mc91-res-m+11 with f (x+11-N Nm) (LT2MCR (x+11-N Nm) Nm m\u2264100 (x100 , _ ) = \u22a5-elim (x\u226489\u2192x+11>100\u2192\u22a5 Nm m\u226489 m+11>100)\n ... | inj\u2082 ( _ , res ) = res\n\n mc91-res-m\u226f89 : mc91 m \u2261 ninety-one\n mc91-res-m\u226f89 = mc91x-res\u226f100 m ninety-one m\u226f100 mc91-res-m+11 mc91-res-91\n\n------------------------------------------------------------------------------\n-- Main properties\n\n-- The function always terminates.\nmc91-N : \u2200 {n} \u2192 N n \u2192 N (mc91 n)\nmc91-N Nn = \u2227-proj\u2081 (mc91-N-ineq Nn)\n\n-- For all n, n < mc91 n + 11.\nmc91-ineq : \u2200 {n} \u2192 N n \u2192 LT n (mc91 n + eleven)\nmc91-ineq Nn = \u2227-proj\u2082 (mc91-N-ineq Nn)\n\n-- For all n > 100, then mc91 n = n - 10.\nmc91-res>100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192 mc91 n \u2261 n \u2238 ten\nmc91-res>100 Nn n>100 with mc91-res Nn\n... | inj\u2081 ( _ , res ) = res\n... | inj\u2082 ( n\u226f100 , _ ) = \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nn 100-N n>100\n (x\u226fy\u2192x\u2264y Nn 100-N n\u226f100))\n\n-- For all n <= 100, then mc91 n = 91.\nmc91-res\u226f100 : \u2200 {n} \u2192 N n \u2192 NGT n one-hundred \u2192 mc91 n \u2261 ninety-one\nmc91-res\u226f100 Nn n\u226f100 with mc91-res Nn\n... | inj\u2081 ( n>100 , _ ) = \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nn 100-N n>100\n (x\u226fy\u2192x\u2264y Nn 100-N n\u226f100))\n... | inj\u2082 ( _ , res ) = res\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"db67836522d8449804ce9b344f24877546eafb80","subject":"Only white space.","message":"Only white space.\n\nIgnore-this: e576b6173e37484c807272422296be50\n\ndarcs-hash:20100505225855-3bd4e-3eb9725e590920b79243fbc6d8a973b84fcfe044.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Relation\/Inequalities\/PropertiesER.agda","new_file":"LTC\/Relation\/Inequalities\/PropertiesER.agda","new_contents":"------------------------------------------------------------------------------\n-- Properties of the inequalities (using equational reasoning)\n------------------------------------------------------------------------------\n\nmodule LTC.Relation.Inequalities.PropertiesER where\n\nopen import LTC.Minimal\nopen import LTC.MinimalER\n\nopen import LTC.Function.Arithmetic\nopen import LTC.Function.Arithmetic.Properties using ( +-comm )\nopen import LTC.Function.Arithmetic.PropertiesER\nopen import LTC.Relation.Inequalities\nopen import LTC.Data.N\n\nopen import MyStdLib.Data.Sum\nopen import MyStdLib.Function\nimport MyStdLib.Relation.Binary.EqReasoning\nopen module IPER = MyStdLib.Relation.Binary.EqReasoning.StdLib _\u2261_ refl trans\n\n------------------------------------------------------------------------------\n\nx\u22650 : {n : D} \u2192 N n \u2192 GE n zero\nx\u22650 zN = lt-00\nx\u22650 (sN {n} Nn) = lt-S0 n\n\n\u00ac0>x : {n : D} \u2192 N n \u2192 \u00ac (GT zero n)\n\u00ac0>x Nn 0>n = true\u2260false $ trans (sym 0>n ) $ x\u22650 Nn\n\n\u00acS\u22640 : {d : D} \u2192 \u00ac (LE (succ d) zero)\n\u00acS\u22640 {d} Sx\u22640 = true\u2260false $ trans (sym $ lt-0S d ) Sx\u22640\n\nx>y\u2228x\u2264y : {m n : D} \u2192 N m \u2192 N n \u2192 GT m n \u2228 LE m n\nx>y\u2228x\u2264y zN Nn = inj\u2082 $ x\u22650 Nn\nx>y\u2228x\u2264y (sN {m} Nm) zN = inj\u2081 $ lt-0S m\nx>y\u2228x\u2264y (sN {m} Nm) (sN {n} Nn) =\n subst (\u03bb a \u2192 a \u2261 true \u2228 a \u2261 false)\n (sym $ lt-SS n m)\n (x>y\u2228x\u2264y Nm Nn )\n\n\u00acxy\u2192x-y+y\u2261x : {m n : D} \u2192 N m \u2192 N n \u2192 GT m n \u2192 (m - n) + n \u2261 m\nx>y\u2192x-y+y\u2261x zN Nn 0>n = \u22a5-elim (\u00ac0>x Nn 0>n)\nx>y\u2192x-y+y\u2261x (sN {m} Nm) zN Sm>0 = trans (+-rightIdentity (minus-N (sN Nm) zN))\n (minus-x0 (succ m))\nx>y\u2192x-y+y\u2261x (sN {m} Nm) (sN {n} Nn) Sm>Sn =\n begin\n (succ m - succ n) + succ n \u2261\u27e8 subst (\u03bb t \u2192 (succ m - succ n) + succ n \u2261\n t + succ n)\n (minus-SS m n)\n refl\n \u27e9\n (m - n) + succ n \u2261\u27e8 +-comm (minus-N Nm Nn) (sN Nn) \u27e9\n succ n + (m - n) \u2261\u27e8 +-Sx n (m - n) \u27e9\n succ (n + (m - n)) \u2261\u27e8 subst (\u03bb t \u2192 succ (n + (m - n)) \u2261 succ t )\n (+-comm Nn (minus-N Nm Nn))\n refl\n \u27e9\n succ ((m - n) + n) \u2261\u27e8 subst (\u03bb t \u2192 succ ((m - n) + n) \u2261 succ t )\n (x>y\u2192x-y+y\u2261x Nm Nn\n (trans (sym (lt-SS n m)) Sm>Sn) )\n refl\n \u27e9\n succ m\n \u220e\n","old_contents":"------------------------------------------------------------------------------\n-- Properties of the inequalities (using equational reasoning)\n------------------------------------------------------------------------------\n\nmodule LTC.Relation.Inequalities.PropertiesER where\n\nopen import LTC.Minimal\nopen import LTC.MinimalER\n\nopen import LTC.Function.Arithmetic\nopen import LTC.Function.Arithmetic.Properties using ( +-comm )\nopen import LTC.Function.Arithmetic.PropertiesER\nopen import LTC.Relation.Inequalities\nopen import LTC.Data.N\n\nopen import MyStdLib.Data.Sum\nopen import MyStdLib.Function\nimport MyStdLib.Relation.Binary.EqReasoning\nopen module IPER = MyStdLib.Relation.Binary.EqReasoning.StdLib _\u2261_ refl trans\n\n------------------------------------------------------------------------------\n\nx\u22650 : {n : D} \u2192 N n \u2192 GE n zero\nx\u22650 zN = lt-00\nx\u22650 (sN {n} Nn) = lt-S0 n\n\n\u00ac0>x : {n : D} \u2192 N n \u2192 \u00ac (GT zero n)\n\u00ac0>x Nn 0>n = true\u2260false $ trans (sym 0>n ) $ x\u22650 Nn\n\n\u00acS\u22640 : {d : D} \u2192 \u00ac (LE (succ d) zero)\n\u00acS\u22640 {d} Sx\u22640 = true\u2260false $ trans (sym $ lt-0S d ) Sx\u22640\n\nx>y\u2228x\u2264y : {m n : D} \u2192 N m \u2192 N n \u2192 GT m n \u2228 LE m n\nx>y\u2228x\u2264y zN Nn = inj\u2082 $ x\u22650 Nn\nx>y\u2228x\u2264y (sN {m} Nm) zN = inj\u2081 $ lt-0S m\nx>y\u2228x\u2264y (sN {m} Nm) (sN {n} Nn) =\n subst (\u03bb a \u2192 a \u2261 true \u2228 a \u2261 false)\n (sym $ lt-SS n m)\n (x>y\u2228x\u2264y Nm Nn )\n\n\u00acxy\u2192x-y+y\u2261x : {m n : D} \u2192 N m \u2192 N n \u2192 GT m n \u2192 (m - n) + n \u2261 m\nx>y\u2192x-y+y\u2261x zN Nn 0>n = \u22a5-elim (\u00ac0>x Nn 0>n)\nx>y\u2192x-y+y\u2261x (sN {m} Nm) zN Sm>0 =\n trans (+-rightIdentity (minus-N (sN Nm) zN)) (minus-x0 (succ m))\nx>y\u2192x-y+y\u2261x (sN {m} Nm) (sN {n} Nn) Sm>Sn =\n begin\n (succ m - succ n) + succ n \u2261\u27e8 subst (\u03bb t \u2192 (succ m - succ n) + succ n \u2261\n t + succ n)\n (minus-SS m n)\n refl\n \u27e9\n (m - n) + succ n \u2261\u27e8 +-comm (minus-N Nm Nn) (sN Nn) \u27e9\n succ n + (m - n) \u2261\u27e8 +-Sx n (m - n) \u27e9\n succ (n + (m - n)) \u2261\u27e8 subst (\u03bb t \u2192 succ (n + (m - n)) \u2261 succ t )\n (+-comm Nn (minus-N Nm Nn))\n refl\n \u27e9\n succ ((m - n) + n) \u2261\u27e8 subst (\u03bb t \u2192 succ ((m - n) + n) \u2261 succ t )\n (x>y\u2192x-y+y\u2261x Nm Nn\n (trans (sym (lt-SS n m)) Sm>Sn) )\n refl\n \u27e9\n succ m\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"886648677722c9cb94f5a4a0191634f396112553","subject":"[ Agda ] Agda's issue 1342 was fixed","message":"[ Agda ] Agda's issue 1342 was fixed\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/GFPs\/Conat.agda","new_file":"notes\/fixed-points\/GFPs\/Conat.agda","new_contents":"------------------------------------------------------------------------------\n-- Co-inductive natural numbers\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-sized-types #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule GFPs.Conat where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- Conat is a greatest fixed-point of a functor\n\n-- The functor.\nNatF : (D \u2192 Set) \u2192 D \u2192 Set\nNatF A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n\n-- The co-natural numbers are the greatest fixed-point of NatF.\npostulate\n Conat : D \u2192 Set\n\n -- Conat is a post-fixed point of NatF, i.e.\n --\n -- Conat \u2264 NatF Conat.\n Conat-out : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\n\n -- The higher-order version.\n Conat-out-ho : \u2200 {n} \u2192 Conat n \u2192 NatF Conat n\n\n -- Conat is the greatest post-fixed point of NatF, i.e.\n --\n -- \u2200 A. A \u2264 NatF A \u21d2 A \u2264 Conat.\n Conat-coind :\n (A : D \u2192 Set) \u2192\n -- A is post-fixed point of ConatF.\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n -- Conat is greater than A.\n \u2200 {n} \u2192 A n \u2192 Conat n\n\n -- The higher-order version.\n Conat-coind-ho :\n (A : D \u2192 Set) \u2192 (\u2200 {n} \u2192 A n \u2192 NatF A n) \u2192 \u2200 {n} \u2192 A n \u2192 Conat n\n\n -- 22 December 2013. This is a stronger induction principle. If we\n -- use it, we can use the trivial predicate A = \u03bb x \u2192 x \u2261 x in the\n -- proofs. Unfortunately, we don't have a justification\/proof for\n -- this principle.\n Conat-stronger-coind\u2081 :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n A n \u2192 Conat n\n\n -- Other stronger co-induction principle\n --\n -- Adapted from (Paulson, 1997. p. 16).\n Conat-stronger-coind\u2082 :\n (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2228 Conat n) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\n\n------------------------------------------------------------------------------\n-- Conat-out and Conat-out-ho are equivalents\n\nConat-out-fo : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\nConat-out-fo = Conat-out-ho\n\nConat-out-ho' : \u2200 {n} \u2192 Conat n \u2192 NatF Conat n\nConat-out-ho' = Conat-out\n\n------------------------------------------------------------------------------\n-- Conat-coind and Conat-coind-ho are equivalents\n\nConat-coind-fo :\n (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind-fo = Conat-coind-ho\n\nConat-coind-ho' :\n (A : D \u2192 Set) \u2192 (\u2200 {n} \u2192 A n \u2192 NatF A n) \u2192 \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind-ho' = Conat-coind\n\n------------------------------------------------------------------------------\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Conat predicate is also a pre-fixed point of the functional NatF,\n-- i.e.\n--\n-- NatF Conat \u2264 Conat.\nConat-in : \u2200 {n} \u2192\n n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n') \u2192\n Conat n\nConat-in h = Conat-coind A h' h\n where\n A : D \u2192 Set\n A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\n\n h' : \u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n h' (inj\u2081 n\u22610) = inj\u2081 n\u22610\n h' (inj\u2082 (n' , prf , Cn')) = inj\u2082 (n' , prf , Conat-out Cn')\n\n-- The higher-order version.\nConat-in-ho : \u2200 {n} \u2192 NatF Conat n \u2192 Conat n\nConat-in-ho = Conat-in\n\n-- A different definition.\nConat-in' : (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')) \u2192\n \u2200 {n} \u2192 Conat n\nConat-in' h = Conat-coind (\u03bb m \u2192 m \u2261 m) (h' h) refl\n where\n h' : (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')) \u2192\n \u2200 {m} \u2192 m \u2261 m \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 m' \u2261 m')\n h' h'' {m} _ with (h'' {m})\n ... | inj\u2081 m\u22610 = inj\u2081 m\u22610\n ... | inj\u2082 (m' , prf , _) = inj\u2082 (m' , prf , refl)\n\nConat-in-ho' : (\u2200 {n} \u2192 NatF Conat n) \u2192 \u2200 {n} \u2192 Conat n\nConat-in-ho' = Conat-in'\n\n------------------------------------------------------------------------------\n-- From Conat-coind\/Conat-stronger-coind\u2081 to Conat-stronger-coind\u2081\/Conat-coind\n\nConat-coind'' :\n (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind'' A h An = Conat-stronger-coind\u2081 A h An\n\n-- 22 December 2013: We couln't prove Conat-stronger-coind\u2081 using\n-- Conat-coind.\nConat-stronger-coind\u2081' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n A n \u2192 Conat n\nConat-stronger-coind\u2081' A {n} h An = Conat-in (case prf\u2081 prf\u2082 (h An))\n where\n prf\u2081 : n \u2261 zero \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\n prf\u2081 n\u22610 = inj\u2081 n\u22610\n\n prf\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n' \u2192\n n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\n prf\u2082 (n' , prf , An') = inj\u2082 (n' , prf , {!!})\n\n------------------------------------------------------------------------------\n-- From Conat-stronger-coind\u2082 to Conat-stronger-coind\u2081\n\n-- 13 January 2014: We couln't prove Conat-stronger-coind\u2081 using\n-- Conat-stronger-coind\u2082.\nConat-stronger-coind\u2081'' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n A n \u2192 Conat n\nConat-stronger-coind\u2081'' A h An = Conat-stronger-coind\u2082 A {!!} An\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Paulson, L. C. (1997). Mechanizing Coinduction and Corecursion in\n-- Higher-order Logic. Journal of Logic and Computation 7.2,\n-- pp. 175\u2013204.\n","old_contents":"------------------------------------------------------------------------------\n-- Co-inductive natural numbers\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-sized-types #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule GFPs.Conat where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- Conat is a greatest fixed-point of a functor\n\n-- The functor.\nNatF : (D \u2192 Set) \u2192 D \u2192 Set\nNatF A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n\n-- The co-natural numbers are the greatest fixed-point of NatF.\npostulate\n Conat : D \u2192 Set\n\n -- Conat is a post-fixed point of NatF, i.e.\n --\n -- Conat \u2264 NatF Conat.\n Conat-out : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\n\n -- The higher-order version.\n Conat-out-ho : \u2200 {n} \u2192 Conat n \u2192 NatF Conat n\n\n -- Conat is the greatest post-fixed point of NatF, i.e.\n --\n -- \u2200 A. A \u2264 NatF A \u21d2 A \u2264 Conat.\n Conat-coind :\n (A : D \u2192 Set) \u2192\n -- A is post-fixed point of ConatF.\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n -- Conat is greater than A.\n \u2200 {n} \u2192 A n \u2192 Conat n\n\n -- The higher-order version.\n Conat-coind-ho :\n (A : D \u2192 Set) \u2192 (\u2200 {n} \u2192 A n \u2192 NatF A n) \u2192 \u2200 {n} \u2192 A n \u2192 Conat n\n\n -- 22 December 2013. This is a stronger induction principle. If we\n -- use it, we can use the trivial predicate A = \u03bb x \u2192 x \u2261 x in the\n -- proofs. Unfortunately, we don't have a justification\/proof for\n -- this principle.\n Conat-stronger-coind\u2081 :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n A n \u2192 Conat n\n\n -- Other stronger co-induction principle\n --\n -- Adapted from (Paulson, 1997. p. 16).\n Conat-stronger-coind\u2082 :\n (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2228 Conat n) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\n\n------------------------------------------------------------------------------\n-- Conat-out and Conat-out-ho are equivalents\n\nConat-out-fo : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\nConat-out-fo = Conat-out-ho\n\nConat-out-ho' : \u2200 {n} \u2192 Conat n \u2192 NatF Conat n\nConat-out-ho' = Conat-out\n\n------------------------------------------------------------------------------\n-- Conat-coind and Conat-coind-ho are equivalents\n\nConat-coind-fo :\n (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind-fo = Conat-coind-ho\n\nConat-coind-ho' :\n (A : D \u2192 Set) \u2192 (\u2200 {n} \u2192 A n \u2192 NatF A n) \u2192 \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind-ho' = Conat-coind\n\n------------------------------------------------------------------------------\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Conat predicate is also a pre-fixed point of the functional NatF,\n-- i.e.\n--\n-- NatF Conat \u2264 Conat.\nConat-in : \u2200 {n} \u2192\n n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n') \u2192\n Conat n\nConat-in h = Conat-coind A h' h\n where\n A : D \u2192 Set\n A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\n\n h' : \u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n h' (inj\u2081 n\u22610) = inj\u2081 n\u22610\n h' (inj\u2082 (n' , prf , Cn')) = inj\u2082 (n' , prf , Conat-out Cn')\n\n-- The higher-order version.\nConat-in-ho : \u2200 {n} \u2192 NatF Conat n \u2192 Conat n\nConat-in-ho = Conat-in\n\n-- A different definition.\nConat-in' : (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')) \u2192\n \u2200 {n} \u2192 Conat n\nConat-in' h = Conat-coind (\u03bb m \u2192 m \u2261 m) h' refl\n where\n h' : \u2200 {m} \u2192 m \u2261 m \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 m' \u2261 m')\n h' _ with h\n ... | inj\u2081 m\u22610 = inj\u2081 m\u22610\n ... | inj\u2082 (m' , prf , _) = inj\u2082 (m' , prf , refl)\n\nConat-in-ho' : (\u2200 {n} \u2192 NatF Conat n) \u2192 \u2200 {n} \u2192 Conat n\nConat-in-ho' = Conat-in'\n\n------------------------------------------------------------------------------\n-- From Conat-coind\/Conat-stronger-coind\u2081 to Conat-stronger-coind\u2081\/Conat-coind\n\nConat-coind'' :\n (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind'' A h An = Conat-stronger-coind\u2081 A h An\n\n-- 22 December 2013: We couln't prove Conat-stronger-coind\u2081 using\n-- Conat-coind.\nConat-stronger-coind\u2081' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n A n \u2192 Conat n\nConat-stronger-coind\u2081' A {n} h An = Conat-in (case prf\u2081 prf\u2082 (h An))\n where\n prf\u2081 : n \u2261 zero \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\n prf\u2081 n\u22610 = inj\u2081 n\u22610\n\n prf\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n' \u2192\n n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\n prf\u2082 (n' , prf , An') = inj\u2082 (n' , prf , {!!})\n\n------------------------------------------------------------------------------\n-- From Conat-stronger-coind\u2082 to Conat-stronger-coind\u2081\n\n-- 13 January 2014: We couln't prove Conat-stronger-coind\u2081 using\n-- Conat-stronger-coind\u2082.\nConat-stronger-coind\u2081'' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n A n \u2192 Conat n\nConat-stronger-coind\u2081'' A h An = Conat-stronger-coind\u2082 A {!!} An\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Paulson, L. C. (1997). Mechanizing Coinduction and Corecursion in\n-- Higher-order Logic. Journal of Logic and Computation 7.2,\n-- pp. 175\u2013204.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b65f846ed1d44f640b5958eabb65b2f70f230ac3","subject":"Prove correctness of apply-term and diff-term (see #25).","message":"Prove correctness of apply-term and diff-term (see #25).\n\nOld-commit-hash: 30f988d1ecc240c3f9e95082b6b7506d53d58724\n","repos":"inc-lc\/ilc-agda","old_file":"Denotational\/Evaluation\/Total.agda","new_file":"Denotational\/Evaluation\/Total.agda","new_contents":"module Denotational.Evaluation.Total where\n\n-- EVALUATION with a primitive for TOTAL DERIVATIVES\n--\n-- This module defines the semantics of terms that support a\n-- primitive (\u0394 e) for computing the total derivative according\n-- to all free variables in e and all future arguments of e if e\n-- is a function.\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Changes\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\n\nopen import Changes\nopen import ChangeContexts\n\n-- TERMS\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 true \u27e7Term \u03c1 = true\n\u27e6 false \u27e7Term \u03c1 = false\n\u27e6 if t\u2081 t\u2082 t\u2083 \u27e7Term \u03c1 = if \u27e6 t\u2081 \u27e7Term \u03c1 then \u27e6 t\u2082 \u27e7Term \u03c1 else \u27e6 t\u2083 \u27e7Term \u03c1\n\u27e6 \u0394 {{\u0393\u2032}} t \u27e7Term \u03c1 = diff (\u27e6 t \u27e7Term (update (\u27e6 \u0393\u2032 \u27e7 \u03c1))) (\u27e6 t \u27e7Term (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n\n{-\n\nHere is an example to understand the semantics of \u0394. I will use a\nnamed variable representation for the task.\n\nConsider the typing judgment:\n\n x: T |- x: T\n\nThus, we have that:\n\n dx : \u0394 T, x: T |- \u0394 x : \u0394 T\n\nThanks to weakening, we also have:\n\n y : S, dx : \u0394 T, x: T |- \u0394 x : \u0394 T\n\nIn the formalization, we need a proof \u0393\u2032 that the context \u0393\u2081 = dx : \u0394\nT, x: T is a subcontext of \u0393\u2082 = y : S, dx : \u0394 T, x: T. Thus, \u0393\u2032 has\ntype \u0393\u2081 \u227c \u0393\u2082.\n\nNow take the environment:\n\n \u03c1 = y \u21a6 w, dx \u21a6 dv, x \u21a6 v\n\nSince the semantics of \u0393\u2032 : \u0393\u2081 \u227c \u0393\u2082 is a function from environments\nfor \u0393\u2082 to environments for \u0393\u2081, we have that:\n\n \u27e6 \u0393\u2032 \u27e7 \u03c1 = dx \u21a6 dv, x \u21a6 v\n\nFrom the definitions of update and ignore, it follows that:\n\n update (\u27e6 \u0393\u2032 \u27e7 \u03c1) = x \u21a6 dv \u2295 v\n ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1) = x \u21a6 v\n\nHence, finally, we have that:\n\n diff (\u27e6 t \u27e7Term (update (\u27e6 \u0393\u2032 \u27e7 \u03c1))) (\u27e6 t \u27e7Term (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n\nis simply diff (dv \u2295 v) v (or (dv \u2295 v) \u229d v). If dv is a valid change,\nthat's just dv, that is \u27e6 dx \u27e7 \u03c1. In other words, if dv is a valid\nchange then \u27e6 \u0394 {{\u0393\u2032}} x \u27e7 \u03c1 \u2261 \u27e6 dx \u27e7 \u03c1 \u2261 \u27e6 derive-term x \u27e7 \u03c1. This\nfact, generalized for arbitrary terms, is proven formally by\nderive-term-correct.\n\n-}\n\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n-- PROPERTIES of WEAKENING\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (t : Term \u0393\u2081 \u03c4) {\u0393\u2032 : \u0393\u2081 \u227c \u0393\u2082} \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken \u0393\u2032 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 \u0393\u2032 \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = ext (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = \u2261-app (weaken-sound t\u2081 \u03c1) (weaken-sound t\u2082 \u03c1)\nweaken-sound (var x) \u03c1 = lift-sound _ x \u03c1\nweaken-sound true \u03c1 = refl\nweaken-sound false \u03c1 = refl\nweaken-sound (if t\u2081 t\u2082 t\u2083) {\u0393\u2032} \u03c1 with weaken-sound t\u2081 {\u0393\u2032} \u03c1\n... | H with \u27e6 weaken \u0393\u2032 t\u2081 \u27e7 \u03c1 | \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u2032 \u27e7 \u03c1)\nweaken-sound (if t\u2081 t\u2082 t\u2083) {\u0393\u2032} \u03c1 | refl | true | true = weaken-sound t\u2082 {\u0393\u2032} \u03c1\nweaken-sound (if t\u2081 t\u2082 t\u2083) {\u0393\u2032} \u03c1 | refl | false | false = weaken-sound t\u2083 {\u0393\u2032} \u03c1\nweaken-sound (\u0394 {{\u0393\u2032}} t) {\u0393\u2033} \u03c1 =\n cong (\u03bb x \u2192 diff (\u27e6 t \u27e7 (update x)) (\u27e6 t \u27e7 (ignore x))) (\u27e6\u27e7-\u227c-trans \u0393\u2032 \u0393\u2033 \u03c1)\n\n-- Simplification rules for weakening\n\n\u2261-weaken\u2070 : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393 \u27e7) \u2192 \u27e6 weaken\u2070 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 \u03c1\n\u2261-weaken\u2070 t \u03c1 =\n begin\n \u27e6 weaken\u2070 t \u27e7 \u03c1\n \u2261\u27e8 weaken-sound t \u03c1 \u27e9\n \u27e6 t \u27e7 (\u27e6 \u227c-refl \u27e7 \u03c1)\n \u2261\u27e8 cong \u27e6 t \u27e7 (\u27e6\u27e7-\u227c-refl \u03c1) \u27e9\n \u27e6 t \u27e7 \u03c1\n \u220e where open \u2261-Reasoning\n\n\u2261-weaken\u00b9 : \u2200 {\u0393 \u03c4} {\u03c4\u2081 : Type} (t : Term \u0393 \u03c4) \u2192\n \u2200 (v\u2081 : \u27e6 \u03c4\u2081 \u27e7) (\u03c1 : \u27e6 \u0393 \u27e7) \u2192 \u27e6 weaken\u00b9 t \u27e7 (v\u2081 \u2022 \u03c1) \u2261 \u27e6 t \u27e7 \u03c1\n\u2261-weaken\u00b9 t v\u2081 \u03c1 =\n begin\n \u27e6 weaken\u00b9 t \u27e7 (v\u2081 \u2022 \u03c1)\n \u2261\u27e8 weaken-sound t (v\u2081 \u2022 \u03c1) \u27e9\n \u27e6 t \u27e7 (\u27e6 \u227c-refl \u27e7 \u03c1)\n \u2261\u27e8 cong \u27e6 t \u27e7 (\u27e6\u27e7-\u227c-refl \u03c1) \u27e9\n \u27e6 t \u27e7 \u03c1\n \u220e where open \u2261-Reasoning\n\n\u2261-weaken\u00b2 : \u2200 {\u0393 \u03c4} {\u03c4\u2081 \u03c4\u2082 : Type} (t : Term \u0393 \u03c4) \u2192\n \u2200 (v\u2081 : \u27e6 \u03c4\u2081 \u27e7) (v\u2082 : \u27e6 \u03c4\u2082 \u27e7) (\u03c1 : \u27e6 \u0393 \u27e7) \u2192 \u27e6 weaken\u00b2 t \u27e7 (v\u2081 \u2022 v\u2082 \u2022 \u03c1) \u2261 \u27e6 t \u27e7 \u03c1\n\u2261-weaken\u00b2 t v\u2081 v\u2082 \u03c1 =\n begin\n \u27e6 weaken\u00b2 t \u27e7 (v\u2081 \u2022 v\u2082 \u2022 \u03c1)\n \u2261\u27e8 weaken-sound t (v\u2081 \u2022 v\u2082 \u2022 \u03c1) \u27e9\n \u27e6 t \u27e7 (\u27e6 \u227c-refl \u27e7 \u03c1)\n \u2261\u27e8 cong \u27e6 t \u27e7 (\u27e6\u27e7-\u227c-refl \u03c1) \u27e9\n \u27e6 t \u27e7 \u03c1\n \u220e where open \u2261-Reasoning\n\n-- CORRECTNESS of NAMED TERMS\n\nxor-term-correct : \u2200 {\u0393} (t\u2081 t\u2082 : Term \u0393 bool) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393 \u27e7) \u2192 \u27e6 t\u2081 xor-term t\u2082 \u27e7 \u03c1 \u2261 \u27e6 t\u2081 \u27e7 \u03c1 xor \u27e6 t\u2082 \u27e7 \u03c1\nxor-term-correct t\u2081 t\u2082 \u03c1\n with \u27e6 t\u2081 \u27e7 \u03c1 | \u27e6 t\u2082 \u27e7 \u03c1\n... | true | true = refl\n... | true | false = refl\n... | false | true = refl\n... | false | false = refl\n\ndiff-term-correct : \u2200 {\u03c4 \u0393} {t\u2081 t\u2082 : Term \u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0393 \u27e7) \u2192 \u27e6 diff-term t\u2081 t\u2082 \u27e7 \u03c1 \u2261 diff (\u27e6 t\u2081 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 \u03c1)\n\napply-term-correct : \u2200 {\u03c4 \u0393} {t\u2081 : Term \u0393 (\u0394-Type \u03c4)} {t\u2082 : Term \u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0393 \u27e7) \u2192 \u27e6 apply-term t\u2081 t\u2082 \u27e7 \u03c1 \u2261 apply (\u27e6 t\u2081 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 \u03c1)\n\ndiff-term-correct {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u0393} {f\u2081} {f\u2082} \u03c1 = ext (\u03bb dv \u2192 ext (\u03bb v \u2192\n begin\n (\u27e6 diff-term f\u2081 f\u2082 \u27e7 \u03c1) dv v\n \u2261\u27e8\u27e9\n \u27e6 diff-term\n (app (weaken\u00b2 f\u2081) (apply-term (var this) (var (that this))))\n (app (weaken\u00b2 f\u2082) (var (that this))) \u27e7 (v \u2022 dv \u2022 \u03c1)\n \u2261\u27e8 diff-term-correct (v \u2022 dv \u2022 \u03c1) \u27e9\n diff\n (\u27e6 app (weaken\u00b2 f\u2081) (apply-term (var this) (var (that this))) \u27e7 (v \u2022 dv \u2022 \u03c1))\n (\u27e6 app (weaken\u00b2 f\u2082) (var (that this)) \u27e7 (v \u2022 dv \u2022 \u03c1))\n \u2261\u27e8\u27e9\n diff\n (\u27e6 weaken\u00b2 f\u2081 \u27e7 (v \u2022 dv \u2022 \u03c1) (\u27e6 apply-term (var this) (var (that this)) \u27e7 (v \u2022 dv \u2022 \u03c1)))\n (\u27e6 weaken\u00b2 f\u2082 \u27e7 (v \u2022 dv \u2022 \u03c1) dv)\n \u2261\u27e8 \u2261-diff\n (\u2261-app (\u2261-weaken\u00b2 f\u2081 v dv \u03c1) (apply-term-correct (v \u2022 dv \u2022 \u03c1)))\n (\u2261-app (\u2261-weaken\u00b2 f\u2082 v dv \u03c1) \u2261-refl) \u27e9\n diff\n (\u27e6 f\u2081 \u27e7 \u03c1 (apply (\u27e6 var {_ \u2022 _ \u2022 _} this \u27e7 (v \u2022 dv \u2022 \u03c1)) (\u27e6 var (that this) \u27e7 (v \u2022 dv \u2022 \u03c1))))\n (\u27e6 f\u2082 \u27e7 \u03c1 dv)\n \u2261\u27e8\u27e9\n diff (\u27e6 f\u2081 \u27e7 \u03c1 (apply v dv)) (\u27e6 f\u2082 \u27e7Term \u03c1 dv)\n \u2261\u27e8\u27e9\n diff (\u27e6 f\u2081 \u27e7 \u03c1) (\u27e6 f\u2082 \u27e7 \u03c1) dv v\n \u220e)) where open \u2261-Reasoning\n\ndiff-term-correct {bool} {\u0393} {b\u2081} {b\u2082} \u03c1 =\n begin\n \u27e6 diff-term b\u2081 b\u2082 \u27e7 \u03c1\n \u2261\u27e8\u27e9\n \u27e6 b\u2081 xor-term b\u2082 \u27e7 \u03c1\n \u2261\u27e8 xor-term-correct b\u2081 b\u2082 \u03c1 \u27e9\n \u27e6 b\u2081 \u27e7 \u03c1 xor \u27e6 b\u2082 \u27e7 \u03c1\n \u2261\u27e8\u27e9\n diff (\u27e6 b\u2081 \u27e7 \u03c1) (\u27e6 b\u2082 \u27e7 \u03c1)\n \u220e where open \u2261-Reasoning\n\napply-term-correct {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u0393} {df} {f} \u03c1 = ext (\u03bb v \u2192\n begin\n \u27e6 apply-term df f \u27e7 \u03c1 v\n \u2261\u27e8\u27e9\n \u27e6 abs (apply-term\n (app (app (weaken\u00b9 df) (var this))\n (diff-term (var this) (var this)))\n (app (weaken\u00b9 f) (var this))) \u27e7 \u03c1 v\n \u2261\u27e8\u27e9\n \u27e6 apply-term\n (app (app (weaken\u00b9 df) (var this))\n (diff-term (var this) (var this)))\n (app (weaken\u00b9 f) (var this)) \u27e7 (v \u2022 \u03c1)\n \u2261\u27e8 apply-term-correct (v \u2022 \u03c1) \u27e9\n apply\n (\u27e6 app (app (weaken\u00b9 df) (var this))\n (diff-term (var this) (var this)) \u27e7 (v \u2022 \u03c1))\n (\u27e6 app (weaken\u00b9 f) (var this) \u27e7 (v \u2022 \u03c1))\n \u2261\u27e8\u27e9\n apply\n (\u27e6 weaken\u00b9 df \u27e7 (v \u2022 \u03c1) v\n (\u27e6 diff-term (var this) (var this) \u27e7 (v \u2022 \u03c1)))\n (\u27e6 weaken\u00b9 f \u27e7 (v \u2022 \u03c1) v)\n \u2261\u27e8 \u2261-apply\n (\u2261-app (\u2261-app (\u2261-weaken\u00b9 df v \u03c1) \u2261-refl)\n (diff-term-correct (v \u2022 \u03c1)))\n (\u2261-app (\u2261-weaken\u00b9 f v \u03c1) \u2261-refl) \u27e9\n apply\n (\u27e6 df \u27e7 \u03c1 v\n (diff (\u27e6 var this \u27e7 (v \u2022 \u03c1)) (\u27e6 var this \u27e7 (v \u2022 \u03c1))))\n (\u27e6 f \u27e7 \u03c1 v)\n \u2261\u27e8\u27e9\n apply (\u27e6 df \u27e7 \u03c1 v (diff v v)) (\u27e6 f \u27e7 \u03c1 v)\n \u2261\u27e8 cong (\u03bb X \u2192 apply (\u27e6 df \u27e7 \u03c1 v X) (\u27e6 f \u27e7 \u03c1 v)) (diff-derive v) \u27e9\n apply (\u27e6 df \u27e7 \u03c1 v (derive v)) (\u27e6 f \u27e7 \u03c1 v)\n \u2261\u27e8\u27e9\n apply (\u27e6 df \u27e7 \u03c1) (\u27e6 f \u27e7 \u03c1) v\n \u220e) where open \u2261-Reasoning\n\napply-term-correct {bool} {\u0393} {db} {b} \u03c1 =\n begin\n \u27e6 apply-term db b \u27e7 \u03c1\n \u2261\u27e8\u27e9\n \u27e6 db xor-term b \u27e7 \u03c1\n \u2261\u27e8 xor-term-correct db b \u03c1 \u27e9\n \u27e6 db \u27e7 \u03c1 xor \u27e6 b \u27e7 \u03c1\n \u2261\u27e8\u27e9\n apply (\u27e6 db \u27e7 \u03c1) (\u27e6 b \u27e7 \u03c1)\n \u220e where open \u2261-Reasoning\n","old_contents":"module Denotational.Evaluation.Total where\n\n-- EVALUATION with a primitive for TOTAL DERIVATIVES\n--\n-- This module defines the semantics of terms that support a\n-- primitive (\u0394 e) for computing the total derivative according\n-- to all free variables in e and all future arguments of e if e\n-- is a function.\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\n\nopen import Changes\nopen import ChangeContexts\n\n-- TERMS\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 true \u27e7Term \u03c1 = true\n\u27e6 false \u27e7Term \u03c1 = false\n\u27e6 if t\u2081 t\u2082 t\u2083 \u27e7Term \u03c1 = if \u27e6 t\u2081 \u27e7Term \u03c1 then \u27e6 t\u2082 \u27e7Term \u03c1 else \u27e6 t\u2083 \u27e7Term \u03c1\n\u27e6 \u0394 {{\u0393\u2032}} t \u27e7Term \u03c1 = diff (\u27e6 t \u27e7Term (update (\u27e6 \u0393\u2032 \u27e7 \u03c1))) (\u27e6 t \u27e7Term (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n\n{-\n\nHere is an example to understand the semantics of \u0394. I will use a\nnamed variable representation for the task.\n\nConsider the typing judgment:\n\n x: T |- x: T\n\nThus, we have that:\n\n dx : \u0394 T, x: T |- \u0394 x : \u0394 T\n\nThanks to weakening, we also have:\n\n y : S, dx : \u0394 T, x: T |- \u0394 x : \u0394 T\n\nIn the formalization, we need a proof \u0393\u2032 that the context \u0393\u2081 = dx : \u0394\nT, x: T is a subcontext of \u0393\u2082 = y : S, dx : \u0394 T, x: T. Thus, \u0393\u2032 has\ntype \u0393\u2081 \u227c \u0393\u2082.\n\nNow take the environment:\n\n \u03c1 = y \u21a6 w, dx \u21a6 dv, x \u21a6 v\n\nSince the semantics of \u0393\u2032 : \u0393\u2081 \u227c \u0393\u2082 is a function from environments\nfor \u0393\u2082 to environments for \u0393\u2081, we have that:\n\n \u27e6 \u0393\u2032 \u27e7 \u03c1 = dx \u21a6 dv, x \u21a6 v\n\nFrom the definitions of update and ignore, it follows that:\n\n update (\u27e6 \u0393\u2032 \u27e7 \u03c1) = x \u21a6 dv \u2295 v\n ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1) = x \u21a6 v\n\nHence, finally, we have that:\n\n diff (\u27e6 t \u27e7Term (update (\u27e6 \u0393\u2032 \u27e7 \u03c1))) (\u27e6 t \u27e7Term (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n\nis simply diff (dv \u2295 v) v (or (dv \u2295 v) \u229d v). If dv is a valid change,\nthat's just dv, that is \u27e6 dx \u27e7 \u03c1. In other words, if dv is a valid\nchange then \u27e6 \u0394 {{\u0393\u2032}} x \u27e7 \u03c1 \u2261 \u27e6 dx \u27e7 \u03c1 \u2261 \u27e6 derive-term x \u27e7 \u03c1. This\nfact, generalized for arbitrary terms, is proven formally by\nderive-term-correct.\n\n-}\n\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n-- PROPERTIES of WEAKENING\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (t : Term \u0393\u2081 \u03c4) {\u0393\u2032 : \u0393\u2081 \u227c \u0393\u2082} \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken \u0393\u2032 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 \u0393\u2032 \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = ext (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = \u2261-app (weaken-sound t\u2081 \u03c1) (weaken-sound t\u2082 \u03c1)\nweaken-sound (var x) \u03c1 = lift-sound _ x \u03c1\nweaken-sound true \u03c1 = refl\nweaken-sound false \u03c1 = refl\nweaken-sound (if t\u2081 t\u2082 t\u2083) {\u0393\u2032} \u03c1 with weaken-sound t\u2081 {\u0393\u2032} \u03c1\n... | H with \u27e6 weaken \u0393\u2032 t\u2081 \u27e7 \u03c1 | \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u2032 \u27e7 \u03c1)\nweaken-sound (if t\u2081 t\u2082 t\u2083) {\u0393\u2032} \u03c1 | refl | true | true = weaken-sound t\u2082 {\u0393\u2032} \u03c1\nweaken-sound (if t\u2081 t\u2082 t\u2083) {\u0393\u2032} \u03c1 | refl | false | false = weaken-sound t\u2083 {\u0393\u2032} \u03c1\nweaken-sound (\u0394 {{\u0393\u2032}} t) {\u0393\u2033} \u03c1 =\n cong (\u03bb x \u2192 diff (\u27e6 t \u27e7 (update x)) (\u27e6 t \u27e7 (ignore x))) (\u27e6\u27e7-\u227c-trans \u0393\u2032 \u0393\u2033 \u03c1)\n\n-- Simplification rules for weakening\n\n\u2261-weaken\u2070 : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393 \u27e7) \u2192 \u27e6 weaken\u2070 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 \u03c1\n\u2261-weaken\u2070 t \u03c1 =\n begin\n \u27e6 weaken\u2070 t \u27e7 \u03c1\n \u2261\u27e8 weaken-sound t \u03c1 \u27e9\n \u27e6 t \u27e7 (\u27e6 \u227c-refl \u27e7 \u03c1)\n \u2261\u27e8 cong \u27e6 t \u27e7 (\u27e6\u27e7-\u227c-refl \u03c1) \u27e9\n \u27e6 t \u27e7 \u03c1\n \u220e where open \u2261-Reasoning\n\n\u2261-weaken\u00b9 : \u2200 {\u0393 \u03c4} {\u03c4\u2081 : Type} (t : Term \u0393 \u03c4) \u2192\n \u2200 (v\u2081 : \u27e6 \u03c4\u2081 \u27e7) (\u03c1 : \u27e6 \u0393 \u27e7) \u2192 \u27e6 weaken\u00b9 t \u27e7 (v\u2081 \u2022 \u03c1) \u2261 \u27e6 t \u27e7 \u03c1\n\u2261-weaken\u00b9 t v\u2081 \u03c1 =\n begin\n \u27e6 weaken\u00b9 t \u27e7 (v\u2081 \u2022 \u03c1)\n \u2261\u27e8 weaken-sound t (v\u2081 \u2022 \u03c1) \u27e9\n \u27e6 t \u27e7 (\u27e6 \u227c-refl \u27e7 \u03c1)\n \u2261\u27e8 cong \u27e6 t \u27e7 (\u27e6\u27e7-\u227c-refl \u03c1) \u27e9\n \u27e6 t \u27e7 \u03c1\n \u220e where open \u2261-Reasoning\n\n\u2261-weaken\u00b2 : \u2200 {\u0393 \u03c4} {\u03c4\u2081 \u03c4\u2082 : Type} (t : Term \u0393 \u03c4) \u2192\n \u2200 (v\u2081 : \u27e6 \u03c4\u2081 \u27e7) (v\u2082 : \u27e6 \u03c4\u2082 \u27e7) (\u03c1 : \u27e6 \u0393 \u27e7) \u2192 \u27e6 weaken\u00b2 t \u27e7 (v\u2081 \u2022 v\u2082 \u2022 \u03c1) \u2261 \u27e6 t \u27e7 \u03c1\n\u2261-weaken\u00b2 t v\u2081 v\u2082 \u03c1 =\n begin\n \u27e6 weaken\u00b2 t \u27e7 (v\u2081 \u2022 v\u2082 \u2022 \u03c1)\n \u2261\u27e8 weaken-sound t (v\u2081 \u2022 v\u2082 \u2022 \u03c1) \u27e9\n \u27e6 t \u27e7 (\u27e6 \u227c-refl \u27e7 \u03c1)\n \u2261\u27e8 cong \u27e6 t \u27e7 (\u27e6\u27e7-\u227c-refl \u03c1) \u27e9\n \u27e6 t \u27e7 \u03c1\n \u220e where open \u2261-Reasoning\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"6942f545a655646a6bfe7b3fd9b3dce0fbbb8e64","subject":"massaging notation","message":"massaging notation\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"core.agda","new_file":"core.agda","new_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n mutual\n subst : Set\n subst = dhexp ctx\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 subst) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 subst) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n _\u27e8_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 dhexp\n _\u27e8_\u21d2\u2987\u2988\u21d2\u0338_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 dhexp\n\n -- notation for chaining together agreeable casts\n _\u27e8_\u21d2_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 htyp \u2192 dhexp\n d \u27e8 t1 \u21d2 t2 \u21d2 t3 \u27e9 = d \u27e8 t1 \u21d2 t2 \u27e9 \u27e8 t2 \u21d2 t3 \u27e9\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = htyp ctx\n\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n -- todo: this probably belongs in contexts, but need to abstract it.\n id : tctx \u2192 subst\n id ctx x with ctx x\n id ctx x | Some \u03c4 = Some (X x)\n id ctx x | None = None\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 (tctx \u00d7 htyp))\n u ::[ \u0393 ] \u03c4 = u , (\u0393 , \u03c4)\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {n : Nat} \u2192\n (n , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- those types without holes anywhere\n data _tcomplete : htyp \u2192 Set where\n TCBase : b tcomplete\n TCArr : \u2200{\u03c41 \u03c42} \u2192 \u03c41 tcomplete \u2192 \u03c42 tcomplete \u2192 (\u03c41 ==> \u03c42) tcomplete\n\n -- those expressions without holes anywhere\n data _ecomplete : hexp \u2192 Set where\n ECConst : c ecomplete\n ECAsc : \u2200{\u03c4 e} \u2192 \u03c4 tcomplete \u2192 e ecomplete \u2192 (e \u00b7: \u03c4) ecomplete\n ECVar : \u2200{x} \u2192 (X x) ecomplete\n ECLam1 : \u2200{x e} \u2192 e ecomplete \u2192 (\u00b7\u03bb x e) ecomplete\n ECLam2 : \u2200{x e \u03c4} \u2192 e ecomplete \u2192 \u03c4 tcomplete \u2192 (\u00b7\u03bb x [ \u03c4 ] e) ecomplete\n ECAp : \u2200{e1 e2} \u2192 e1 ecomplete \u2192 e2 ecomplete \u2192 (e1 \u2218 e2) ecomplete\n\n data _dcomplete : dhexp \u2192 Set where\n DCVar : \u2200{x} \u2192 (X x) dcomplete\n DCConst : c dcomplete\n DCLam : \u2200{x \u03c4 d} \u2192 d dcomplete \u2192 \u03c4 tcomplete \u2192 (\u00b7\u03bb x [ \u03c4 ] d) dcomplete\n DCAp : \u2200{d1 d2} \u2192 d1 dcomplete \u2192 d2 dcomplete \u2192 (d1 \u2218 d2) dcomplete\n DCCast : \u2200{d \u03c41 \u03c42} \u2192 d dcomplete \u2192 \u03c41 tcomplete \u2192 \u03c42 tcomplete \u2192 (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) dcomplete\n\n -- contexts that only know about complete types\n _gcomplete : tctx \u2192 Set\n \u0393 gcomplete = (x : Nat) (\u03c4 : htyp) \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u03c4 tcomplete\n\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp : \u2200{\u0393 e1 \u03c4 \u03c41 \u03c41' \u03c42 \u03c42' d1 \u03941 e2 d2 \u03942 } \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: need to think about disjointness and context rep\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u03c4) ~> d1 :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> (d1 \u27e8 \u03c41' \u21d2 \u03c42 ==> \u03c4 \u27e9) \u2218 (d2 \u27e8 \u03c42' \u21d2 \u03c42 \u27e9) \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u27e8 \u03c4' \u21d2 \u03c4 \u27e9 \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c4 \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c4 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 e \u2260 \u2987\u2988[ u ]) \u2192\n ((e' : hexp) (u : Nat) \u2192 e \u2260 \u2987 e' \u2988[ u ]) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n -- ground\n data _ground : (\u03c4 : htyp) \u2192 Set where\n GBase : b ground\n GHole : \u2987\u2988 ==> \u2987\u2988 ground\n\n mutual\n -- substitition type assignment\n _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 subst \u2192 tctx \u2192 Set\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' =\n (x : Nat) (d : dhexp) (xd\u2208\u03c3 : (x , d) \u2208 \u03c3) \u2192\n \u03a3[ \u03c4 \u2208 htyp ] ((\u0393' x == Some \u03c4 \u00d7 (\u0394 , \u0393 \u22a2 d :: \u03c4)))\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 ==> \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 } \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c41 \u03c42} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u03c41 ~ \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 :: \u03c42\n TAFailedCast : \u2200{\u0394 \u0393 d \u03c41 \u03c42} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 :: \u03c42\n\n -- substitution;; todo: maybe get a premise that it's final; analagous to \"value substitution\"\n [_\/_]_ : dhexp \u2192 Nat \u2192 dhexp \u2192 dhexp\n [ d \/ y ] c = c\n [ d \/ y ] X x\n with natEQ x y\n [ d \/ y ] X .y | Inl refl = d\n [ d \/ y ] X x | Inr neq = X x\n [ d \/ y ] (\u00b7\u03bb x [ x\u2081 ] d') = \u00b7\u03bb x [ x\u2081 ] ( [ d \/ y ] d') -- TODO: i *think* barendrecht's saves us here, or at least i want it to. may need to reformulat this as a relation --> set\n [ d \/ y ] \u2987\u2988\u27e8 u , \u03c3 \u27e9 = \u2987\u2988\u27e8 u , \u03c3 \u27e9\n [ d \/ y ] \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9 = \u2987 [ d \/ y ] d' \u2988\u27e8 u , \u03c3 \u27e9\n [ d \/ y ] (d1 \u2218 d2) = ([ d \/ y ] d1) \u2218 ([ d \/ y ] d2)\n [ d \/ y ] (d' \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 ) = ([ d \/ y ] d') \u27e8 \u03c41 \u21d2 \u03c42 \u27e9\n [ d \/ y ] (d' \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 ) = ([ d \/ y ] d') \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9\n\n -- value\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n data _boxedval : (d : dhexp) \u2192 Set where\n BVVal : \u2200{d} \u2192 d val \u2192 d boxedval\n BVArrCast : \u2200{ d \u03c41 \u03c42 \u03c43 \u03c44 } \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d boxedval \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 boxedval\n BVHoleCast : \u2200{ \u03c4 d } \u2192 \u03c4 ground \u2192 d boxedval \u2192 d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 boxedval\n\n mutual\n -- indeterminate\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192\n d1 \u2260 (d1' \u27e8(\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44)\u27e9)) \u2192\n d1 indet \u2192\n d2 final \u2192\n (d1 \u2218 d2) indet -- todo: should there be two ap rules?\n ICastArr : \u2200{d \u03c41 \u03c42 \u03c43 \u03c44} \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d indet \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 indet\n ICastGroundHole : \u2200{ \u03c4 d } \u2192\n \u03c4 ground \u2192\n d indet \u2192\n d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 indet\n ICastHoleGround : \u2200 { d \u03c4 } \u2192\n ((d' : dhexp) (\u03c4' : htyp) \u2192 d \u2260 (d' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9)) \u2192\n d indet \u2192\n \u03c4 ground \u2192\n d \u27e8 \u2987\u2988 \u21d2 \u03c4 \u27e9 indet\n IFailedCast : \u2200{ d \u03c41 \u03c42 } \u2192\n d final \u2192\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 indet\n\n -- final\n data _final : (d : dhexp) \u2192 Set where\n FBoxed : \u2200{d} \u2192 d boxedval \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n\n -- -- contextual dynamics\n\n -- evaluation contexts\n data ectx : Set where\n \u2299 : ectx\n _\u2218\u2081_ : ectx \u2192 dhexp \u2192 ectx\n _\u2218\u2082_ : dhexp \u2192 ectx \u2192 ectx\n \u2987_\u2988\u27e8_\u27e9 : ectx \u2192 (Nat \u00d7 subst ) \u2192 ectx\n _\u27e8_\u21d2_\u27e9 : ectx \u2192 htyp \u2192 htyp \u2192 ectx\n _\u27e8_\u21d2\u2987\u2988\u21d2\u0338_\u27e9 : ectx \u2192 htyp \u2192 htyp \u2192 ectx\n\n -- todo\/ note: this judgement is redundant now; in the absence of the\n -- premises in the red brackets in the notes PDF, all syntactically well\n -- formed ectxs are valid; with finality premises, that's not true. so it\n -- might make sense to remove this judgement entirely, but need to make\n -- sure to describe why we don't have the redbox things and how we'd patch\n -- it up if we wanted to force a particular evaluation order in some\n -- document somewhere (probably a README for this repo, or a sentence in\n -- the paper text or both)\n\n --\u03b5 is an evaluation context\n data _evalctx : (\u03b5 : ectx) \u2192 Set where\n ECDot : \u2299 evalctx\n ECAp1 : \u2200{d \u03b5} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u2218\u2081 d) evalctx\n ECAp2 : \u2200{d \u03b5} \u2192\n -- d final \u2192 -- red box\n \u03b5 evalctx \u2192\n (d \u2218\u2082 \u03b5) evalctx\n ECNEHole : \u2200{\u03b5 u \u03c3} \u2192\n \u03b5 evalctx \u2192\n \u2987 \u03b5 \u2988\u27e8 u , \u03c3 \u27e9 evalctx\n ECCast : \u2200{ \u03b5 \u03c41 \u03c42} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) evalctx\n ECFailedCast : \u2200{ \u03b5 \u03c41 \u03c42 } \u2192\n \u03b5 evalctx \u2192\n \u03b5 \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 evalctx\n\n -- d is the result of filling the hole in \u03b5 with d'\n data _==_\u27e6_\u27e7 : (d : dhexp) (\u03b5 : ectx) (d' : dhexp) \u2192 Set where\n FHOuter : \u2200{d} \u2192 d == \u2299 \u27e6 d \u27e7\n FHAp1 : \u2200{d1 d1' d2 \u03b5} \u2192\n d1 == \u03b5 \u27e6 d1' \u27e7 \u2192\n (d1 \u2218 d2) == (\u03b5 \u2218\u2081 d2) \u27e6 d1' \u27e7\n FHAp2 : \u2200{d1 d2 d2' \u03b5} \u2192\n -- d1 final \u2192 -- red box\n d2 == \u03b5 \u27e6 d2' \u27e7 \u2192\n (d1 \u2218 d2) == (d1 \u2218\u2082 \u03b5) \u27e6 d2' \u27e7\n FHNEHole : \u2200{ d d' \u03b5 u \u03c3} \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u2987 d \u2988\u27e8 (u , \u03c3 ) \u27e9 == \u2987 \u03b5 \u2988\u27e8 (u , \u03c3 ) \u27e9 \u27e6 d' \u27e7\n FHCast : \u2200{ d d' \u03b5 \u03c41 \u03c42 } \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 == \u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 \u27e6 d' \u27e7\n FHFailedCast : \u2200{ d d' \u03b5 \u03c41 \u03c42} \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9) == (\u03b5 \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9) \u27e6 d' \u27e7\n\n data _\u25b8gnd_ : htyp \u2192 htyp \u2192 Set where\n MGArr : \u2200{\u03c41 \u03c42} \u2192\n (\u03c41 ==> \u03c42) \u2260 (\u2987\u2988 ==> \u2987\u2988) \u2192\n (\u03c41 ==> \u03c42) \u25b8gnd (\u2987\u2988 ==> \u2987\u2988)\n\n -- instruction transition judgement\n data _\u2192>_ : (d d' : dhexp) \u2192 Set where\n ITLam : \u2200{ x \u03c4 d1 d2 } \u2192\n -- d2 final \u2192 -- red box\n ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u2192> ([ d2 \/ x ] d1) -- todo: this is very unlikely to work long term\n ITCastID : \u2200{d \u03c4 } \u2192\n -- d final \u2192 -- red box\n (d \u27e8 \u03c4 \u21d2 \u03c4 \u27e9) \u2192> d\n ITCastSucceed : \u2200{d \u03c4 } \u2192\n -- d final \u2192 -- red box\n \u03c4 ground \u2192\n (d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u21d2 \u03c4 \u27e9) \u2192> d\n ITCastFail : \u2200{ d \u03c41 \u03c42} \u2192\n -- d final \u2192 -- red box\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n (d \u27e8 \u03c41 \u21d2 \u2987\u2988 \u21d2 \u03c42 \u27e9) \u2192> (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n ITApCast : \u2200{d1 d2 \u03c41 \u03c42 \u03c41' \u03c42' } \u2192\n -- d1 final \u2192 -- red box\n -- d2 final \u2192 -- red box\n ((d1 \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c41' ==> \u03c42')\u27e9) \u2218 d2) \u2192> ((d1 \u2218 (d2 \u27e8 \u03c41' \u21d2 \u03c41 \u27e9)) \u27e8 \u03c42 \u21d2 \u03c42' \u27e9)\n ITGround : \u2200{ d \u03c4 \u03c4'} \u2192\n -- d final \u2192 -- red box\n \u03c4 \u25b8gnd \u03c4' \u2192\n (d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9) \u2192> (d \u27e8 \u03c4 \u21d2 \u03c4' \u21d2 \u2987\u2988 \u27e9)\n ITExpand : \u2200{d \u03c4 \u03c4' } \u2192\n -- d final \u2192 -- red box\n \u03c4 \u25b8gnd \u03c4' \u2192\n (d \u27e8 \u2987\u2988 \u21d2 \u03c4 \u27e9) \u2192> (d \u27e8 \u2987\u2988 \u21d2 \u03c4' \u21d2 \u03c4 \u27e9)\n\n data _\u21a6_ : (d d' : dhexp) \u2192 Set where\n Step : \u2200{ d d0 d' d0' \u03b5} \u2192\n d == \u03b5 \u27e6 d0 \u27e7 \u2192\n d0 \u2192> d0' \u2192\n d' == \u03b5 \u27e6 d0' \u27e7 \u2192\n d \u21a6 d'\n\n data _\u21a6*_ : (d d' : dhexp) \u2192 Set where\n MSRefl : \u2200{d} \u2192 d \u21a6* d\n MSStep : \u2200{d d' d''} \u2192\n d \u21a6 d' \u2192\n d' \u21a6* d'' \u2192\n d \u21a6* d''\n\n -- application of a substution to a term\n postulate\n apply : subst \u2192 dhexp \u2192 dhexp\n -- apply \u03c3 c = c\n -- apply \u03c3 (X x) with \u03c3 x\n -- apply \u03c3 (X x) | Some d' = d'\n -- apply \u03c3 (X x) | None = X x\n -- apply \u03c3 (\u00b7\u03bb x [ \u03c4 ] d) = (\u00b7\u03bb x [ \u03c4 ] (apply \u03c3 d))\n -- apply \u03c3 \u2987\u2988\u27e8 u , \u03c3' \u27e9 = \u2987\u2988\u27e8 u , ((\u03bb x \u2192 (lift (apply \u03c3)) (\u03c3' x))) \u27e9 -- (\u03bb x \u2192 (lift (apply \u03c3)) (\u03c3' x))\n -- apply \u03c3 \u2987 d \u2988\u27e8 u , \u03c3' \u27e9 = \u2987 apply \u03c3 d \u2988\u27e8 u ,{!!} \u27e9\n -- apply \u03c3 (d1 \u2218 d2) = ((apply \u03c3 d1) \u2218 (apply \u03c3 d2))\n -- apply \u03c3 (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) = ((apply \u03c3 d) \u27e8 \u03c41 \u21d2 \u03c42 \u27e9)\n -- apply \u03c3 (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9) = ((apply \u03c3 d) \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n\n --hole instantiation; todo: judgemental or functional?\n \u27e6_\/_\u27e7_ : dhexp \u2192 Nat \u2192 dhexp \u2192 dhexp\n \u27e6 d \/ u \u27e7 c = c\n \u27e6 d \/ u \u27e7 X x = X x\n \u27e6 d \/ u \u27e7 (\u00b7\u03bb x [ \u03c4 ] d') = \u00b7\u03bb x [ \u03c4 ] (\u27e6 d \/ u \u27e7 d')\n \u27e6 d \/ u \u27e7 \u2987\u2988\u27e8 n , \u03c3 \u27e9\n with natEQ n u\n \u27e6 d \/ u \u27e7 \u2987\u2988\u27e8 .u , \u03c3 \u27e9 | Inl refl = apply \u03c3 d\n \u27e6 d \/ u \u27e7 \u2987\u2988\u27e8 n , \u03c3 \u27e9 | Inr x = \u2987\u2988\u27e8 n , \u03c3 \u27e9\n \u27e6 d \/ u \u27e7 \u2987 d' \u2988\u27e8 n , \u03c3 \u27e9\n with natEQ n u\n \u27e6 d \/ u \u27e7 \u2987 d' \u2988\u27e8 .u , \u03c3 \u27e9 | Inl refl = apply \u03c3 d\n \u27e6 d \/ u \u27e7 \u2987 d' \u2988\u27e8 n , \u03c3 \u27e9 | Inr x = \u2987 d' \u2988\u27e8 n , \u03c3 \u27e9\n \u27e6 d \/ u \u27e7 (d1 \u2218 d2) = (\u27e6 d \/ u \u27e7 d1) \u2218 (\u27e6 d \/ u \u27e7 d2)\n \u27e6 d \/ u \u27e7 (d' \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) = (\u27e6 d \/ u \u27e7 d') \u27e8 \u03c41 \u21d2 \u03c42 \u27e9\n \u27e6 d \/ u \u27e7 (d' \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9) = (\u27e6 d \/ u \u27e7 d') \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9\n","old_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n mutual\n subst : Set\n subst = dhexp ctx\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 subst) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 subst) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n _\u27e8_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 dhexp\n _\u27e8_\u21d2\u2987\u2988\u21d2\u0338_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 dhexp\n\n -- notation for chaining together agreeable casts\n _\u27e8_\u21d2_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 htyp \u2192 dhexp\n d \u27e8 t1 \u21d2 t2 \u21d2 t3 \u27e9 = d \u27e8 t1 \u21d2 t2 \u27e9 \u27e8 t2 \u21d2 t3 \u27e9\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = htyp ctx\n\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n -- todo: this probably belongs in contexts, but need to abstract it.\n id : tctx \u2192 subst\n id ctx x with ctx x\n id ctx x | Some \u03c4 = Some (X x)\n id ctx x | None = None\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 (tctx \u00d7 htyp))\n u ::[ \u0393 ] \u03c4 = u , (\u0393 , \u03c4)\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {n : Nat} \u2192\n (n , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- those types without holes anywhere\n data _tcomplete : htyp \u2192 Set where\n TCBase : b tcomplete\n TCArr : \u2200{\u03c41 \u03c42} \u2192 \u03c41 tcomplete \u2192 \u03c42 tcomplete \u2192 (\u03c41 ==> \u03c42) tcomplete\n\n -- those expressions without holes anywhere\n data _ecomplete : hexp \u2192 Set where\n ECConst : c ecomplete\n ECAsc : \u2200{\u03c4 e} \u2192 \u03c4 tcomplete \u2192 e ecomplete \u2192 (e \u00b7: \u03c4) ecomplete\n ECVar : \u2200{x} \u2192 (X x) ecomplete\n ECLam1 : \u2200{x e} \u2192 e ecomplete \u2192 (\u00b7\u03bb x e) ecomplete\n ECLam2 : \u2200{x e \u03c4} \u2192 e ecomplete \u2192 \u03c4 tcomplete \u2192 (\u00b7\u03bb x [ \u03c4 ] e) ecomplete\n ECAp : \u2200{e1 e2} \u2192 e1 ecomplete \u2192 e2 ecomplete \u2192 (e1 \u2218 e2) ecomplete\n\n data _dcomplete : dhexp \u2192 Set where\n DCVar : \u2200{x} \u2192 (X x) dcomplete\n DCConst : c dcomplete\n DCLam : \u2200{x \u03c4 d} \u2192 d dcomplete \u2192 \u03c4 tcomplete \u2192 (\u00b7\u03bb x [ \u03c4 ] d) dcomplete\n DCAp : \u2200{d1 d2} \u2192 d1 dcomplete \u2192 d2 dcomplete \u2192 (d1 \u2218 d2) dcomplete\n DCCast : \u2200{d \u03c41 \u03c42} \u2192 d dcomplete \u2192 \u03c41 tcomplete \u2192 \u03c42 tcomplete \u2192 (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) dcomplete\n\n -- contexts that only know about complete types\n _gcomplete : tctx \u2192 Set\n \u0393 gcomplete = (x : Nat) (t : htyp) \u2192 (\u0393 x) == Some t \u2192 t tcomplete\n\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp : \u2200{\u0393 e1 \u03c4 \u03c41 \u03c41' \u03c42 \u03c42' d1 \u03941 e2 d2 \u03942 } \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: need to think about disjointness and context rep\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u03c4) ~> d1 :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> (d1 \u27e8 \u03c41' \u21d2 \u03c42 ==> \u03c4 \u27e9) \u2218 (d2 \u27e8 \u03c42' \u21d2 \u03c42 \u27e9) \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u27e8 \u03c4' \u21d2 \u03c4 \u27e9 \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c4 \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c4 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 e \u2260 \u2987\u2988[ u ]) \u2192\n ((e' : hexp) (u : Nat) \u2192 e \u2260 \u2987 e' \u2988[ u ]) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n -- ground\n data _ground : (\u03c4 : htyp) \u2192 Set where\n GBase : b ground\n GHole : \u2987\u2988 ==> \u2987\u2988 ground\n\n mutual\n -- substitition type assignment\n _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 subst \u2192 tctx \u2192 Set\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' =\n (x : Nat) (d : dhexp) (xd\u2208\u03c3 : (x , d) \u2208 \u03c3) \u2192\n \u03a3[ \u03c4 \u2208 htyp ] ((\u0393' x == Some \u03c4 \u00d7 (\u0394 , \u0393 \u22a2 d :: \u03c4)))\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 ==> \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 } \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c41 \u03c42} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u03c41 ~ \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 :: \u03c42\n TAFailedCast : \u2200{\u0394 \u0393 d \u03c41 \u03c42} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 :: \u03c42\n\n -- substitution;; todo: maybe get a premise that it's final; analagous to \"value substitution\"\n [_\/_]_ : dhexp \u2192 Nat \u2192 dhexp \u2192 dhexp\n [ d \/ y ] c = c\n [ d \/ y ] X x\n with natEQ x y\n [ d \/ y ] X .y | Inl refl = d\n [ d \/ y ] X x | Inr neq = X x\n [ d \/ y ] (\u00b7\u03bb x [ x\u2081 ] d') = \u00b7\u03bb x [ x\u2081 ] ( [ d \/ y ] d') -- TODO: i *think* barendrecht's saves us here, or at least i want it to. may need to reformulat this as a relation --> set\n [ d \/ y ] \u2987\u2988\u27e8 u , \u03c3 \u27e9 = \u2987\u2988\u27e8 u , \u03c3 \u27e9\n [ d \/ y ] \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9 = \u2987 [ d \/ y ] d' \u2988\u27e8 u , \u03c3 \u27e9\n [ d \/ y ] (d1 \u2218 d2) = ([ d \/ y ] d1) \u2218 ([ d \/ y ] d2)\n [ d \/ y ] (d' \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 ) = ([ d \/ y ] d') \u27e8 \u03c41 \u21d2 \u03c42 \u27e9\n [ d \/ y ] (d' \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 ) = ([ d \/ y ] d') \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9\n\n -- value\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n data _boxedval : (d : dhexp) \u2192 Set where\n BVVal : \u2200{d} \u2192 d val \u2192 d boxedval\n BVArrCast : \u2200{ d \u03c41 \u03c42 \u03c43 \u03c44 } \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d boxedval \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 boxedval\n BVHoleCast : \u2200{ \u03c4 d } \u2192 \u03c4 ground \u2192 d boxedval \u2192 d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 boxedval\n\n mutual\n -- indeterminate\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192\n d1 \u2260 (d1' \u27e8(\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44)\u27e9)) \u2192\n d1 indet \u2192\n d2 final \u2192\n (d1 \u2218 d2) indet -- todo: should there be two ap rules?\n ICastArr : \u2200{d \u03c41 \u03c42 \u03c43 \u03c44} \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d indet \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 indet\n ICastGroundHole : \u2200{ \u03c4 d } \u2192\n \u03c4 ground \u2192\n d indet \u2192\n d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 indet\n ICastHoleGround : \u2200 { d \u03c4 } \u2192\n ((d' : dhexp) (\u03c4' : htyp) \u2192 d \u2260 (d' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9)) \u2192\n d indet \u2192\n \u03c4 ground \u2192\n d \u27e8 \u2987\u2988 \u21d2 \u03c4 \u27e9 indet\n IFailedCast : \u2200{ d \u03c41 \u03c42 } \u2192\n d final \u2192\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 indet\n\n -- final\n data _final : (d : dhexp) \u2192 Set where\n FBoxed : \u2200{d} \u2192 d boxedval \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n\n -- -- contextual dynamics\n\n -- evaluation contexts\n data ectx : Set where\n \u2299 : ectx\n _\u2218\u2081_ : ectx \u2192 dhexp \u2192 ectx\n _\u2218\u2082_ : dhexp \u2192 ectx \u2192 ectx\n \u2987_\u2988\u27e8_\u27e9 : ectx \u2192 (Nat \u00d7 subst ) \u2192 ectx\n _\u27e8_\u21d2_\u27e9 : ectx \u2192 htyp \u2192 htyp \u2192 ectx\n _\u27e8_\u21d2\u2987\u2988\u21d2\u0338_\u27e9 : ectx \u2192 htyp \u2192 htyp \u2192 ectx\n\n -- todo\/ note: this judgement is redundant now; in the absence of the\n -- premises in the red brackets in the notes PDF, all syntactically well\n -- formed ectxs are valid; with finality premises, that's not true. so it\n -- might make sense to remove this judgement entirely, but need to make\n -- sure to describe why we don't have the redbox things and how we'd patch\n -- it up if we wanted to force a particular evaluation order in some\n -- document somewhere (probably a README for this repo, or a sentence in\n -- the paper text or both)\n\n --\u03b5 is an evaluation context\n data _evalctx : (\u03b5 : ectx) \u2192 Set where\n ECDot : \u2299 evalctx\n ECAp1 : \u2200{d \u03b5} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u2218\u2081 d) evalctx\n ECAp2 : \u2200{d \u03b5} \u2192\n -- d final \u2192 -- red box\n \u03b5 evalctx \u2192\n (d \u2218\u2082 \u03b5) evalctx\n ECNEHole : \u2200{\u03b5 u \u03c3} \u2192\n \u03b5 evalctx \u2192\n \u2987 \u03b5 \u2988\u27e8 u , \u03c3 \u27e9 evalctx\n ECCast : \u2200{ \u03b5 \u03c41 \u03c42} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) evalctx\n ECFailedCast : \u2200{ \u03b5 \u03c41 \u03c42 } \u2192\n \u03b5 evalctx \u2192\n \u03b5 \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 evalctx\n\n -- d is the result of filling the hole in \u03b5 with d'\n data _==_\u27e6_\u27e7 : (d : dhexp) (\u03b5 : ectx) (d' : dhexp) \u2192 Set where\n FHOuter : \u2200{d} \u2192 d == \u2299 \u27e6 d \u27e7\n FHAp1 : \u2200{d1 d1' d2 \u03b5} \u2192\n d1 == \u03b5 \u27e6 d1' \u27e7 \u2192\n (d1 \u2218 d2) == (\u03b5 \u2218\u2081 d2) \u27e6 d1' \u27e7\n FHAp2 : \u2200{d1 d2 d2' \u03b5} \u2192\n -- d1 final \u2192 -- red box\n d2 == \u03b5 \u27e6 d2' \u27e7 \u2192\n (d1 \u2218 d2) == (d1 \u2218\u2082 \u03b5) \u27e6 d2' \u27e7\n FHNEHole : \u2200{ d d' \u03b5 u \u03c3} \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u2987 d \u2988\u27e8 (u , \u03c3 ) \u27e9 == \u2987 \u03b5 \u2988\u27e8 (u , \u03c3 ) \u27e9 \u27e6 d' \u27e7\n FHCast : \u2200{ d d' \u03b5 \u03c41 \u03c42 } \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 == \u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 \u27e6 d' \u27e7\n FHFailedCast : \u2200{ d d' \u03b5 \u03c41 \u03c42} \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9) == (\u03b5 \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9) \u27e6 d' \u27e7\n\n data _\u25b8gnd_ : htyp \u2192 htyp \u2192 Set where\n MGArr : \u2200{\u03c41 \u03c42} \u2192\n (\u03c41 ==> \u03c42) \u2260 (\u2987\u2988 ==> \u2987\u2988) \u2192\n (\u03c41 ==> \u03c42) \u25b8gnd (\u2987\u2988 ==> \u2987\u2988)\n\n -- instruction transition judgement\n data _\u2192>_ : (d d' : dhexp) \u2192 Set where\n ITLam : \u2200{ x \u03c4 d1 d2 } \u2192\n -- d2 final \u2192 -- red box\n ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u2192> ([ d2 \/ x ] d1) -- todo: this is very unlikely to work long term\n ITCastID : \u2200{d \u03c4 } \u2192\n -- d final \u2192 -- red box\n (d \u27e8 \u03c4 \u21d2 \u03c4 \u27e9) \u2192> d\n ITCastSucceed : \u2200{d \u03c4 } \u2192\n -- d final \u2192 -- red box\n \u03c4 ground \u2192\n (d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u21d2 \u03c4 \u27e9) \u2192> d\n ITCastFail : \u2200{ d \u03c41 \u03c42} \u2192\n -- d final \u2192 -- red box\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n (d \u27e8 \u03c41 \u21d2 \u2987\u2988 \u21d2 \u03c42 \u27e9) \u2192> (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n ITApCast : \u2200{d1 d2 \u03c41 \u03c42 \u03c41' \u03c42' } \u2192\n -- d1 final \u2192 -- red box\n -- d2 final \u2192 -- red box\n ((d1 \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c41' ==> \u03c42')\u27e9) \u2218 d2) \u2192> ((d1 \u2218 (d2 \u27e8 \u03c41' \u21d2 \u03c41 \u27e9)) \u27e8 \u03c42 \u21d2 \u03c42' \u27e9)\n ITGround : \u2200{ d \u03c4 \u03c4'} \u2192\n -- d final \u2192 -- red box\n \u03c4 \u25b8gnd \u03c4' \u2192\n (d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9) \u2192> (d \u27e8 \u03c4 \u21d2 \u03c4' \u21d2 \u2987\u2988 \u27e9)\n ITExpand : \u2200{d \u03c4 \u03c4' } \u2192\n -- d final \u2192 -- red box\n \u03c4 \u25b8gnd \u03c4' \u2192\n (d \u27e8 \u2987\u2988 \u21d2 \u03c4 \u27e9) \u2192> (d \u27e8 \u2987\u2988 \u21d2 \u03c4' \u21d2 \u03c4 \u27e9)\n\n data _\u21a6_ : (d d' : dhexp) \u2192 Set where\n Step : \u2200{ d d0 d' d0' \u03b5} \u2192\n d == \u03b5 \u27e6 d0 \u27e7 \u2192\n d0 \u2192> d0' \u2192\n d' == \u03b5 \u27e6 d0' \u27e7 \u2192\n d \u21a6 d'\n\n data _\u21a6*_ : (d d' : dhexp) \u2192 Set where\n MSRefl : \u2200{d} \u2192 d \u21a6* d\n MSStep : \u2200{d d' d''} \u2192\n d \u21a6 d' \u2192\n d' \u21a6* d'' \u2192\n d \u21a6* d''\n\n -- application of a substution to a term\n postulate\n apply : subst \u2192 dhexp \u2192 dhexp\n -- apply \u03c3 c = c\n -- apply \u03c3 (X x) with \u03c3 x\n -- apply \u03c3 (X x) | Some d' = d'\n -- apply \u03c3 (X x) | None = X x\n -- apply \u03c3 (\u00b7\u03bb x [ \u03c4 ] d) = (\u00b7\u03bb x [ \u03c4 ] (apply \u03c3 d))\n -- apply \u03c3 \u2987\u2988\u27e8 u , \u03c3' \u27e9 = \u2987\u2988\u27e8 u , ((\u03bb x \u2192 (lift (apply \u03c3)) (\u03c3' x))) \u27e9 -- (\u03bb x \u2192 (lift (apply \u03c3)) (\u03c3' x))\n -- apply \u03c3 \u2987 d \u2988\u27e8 u , \u03c3' \u27e9 = \u2987 apply \u03c3 d \u2988\u27e8 u ,{!!} \u27e9\n -- apply \u03c3 (d1 \u2218 d2) = ((apply \u03c3 d1) \u2218 (apply \u03c3 d2))\n -- apply \u03c3 (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) = ((apply \u03c3 d) \u27e8 \u03c41 \u21d2 \u03c42 \u27e9)\n -- apply \u03c3 (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9) = ((apply \u03c3 d) \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n\n --hole instantiation; todo: judgemental or functional?\n \u27e6_\/_\u27e7_ : dhexp \u2192 Nat \u2192 dhexp \u2192 dhexp\n \u27e6 d \/ u \u27e7 c = c\n \u27e6 d \/ u \u27e7 X x = X x\n \u27e6 d \/ u \u27e7 (\u00b7\u03bb x [ \u03c4 ] d') = \u00b7\u03bb x [ \u03c4 ] (\u27e6 d \/ u \u27e7 d')\n \u27e6 d \/ u \u27e7 \u2987\u2988\u27e8 n , \u03c3 \u27e9\n with natEQ n u\n \u27e6 d \/ u \u27e7 \u2987\u2988\u27e8 .u , \u03c3 \u27e9 | Inl refl = apply \u03c3 d\n \u27e6 d \/ u \u27e7 \u2987\u2988\u27e8 n , \u03c3 \u27e9 | Inr x = \u2987\u2988\u27e8 n , \u03c3 \u27e9\n \u27e6 d \/ u \u27e7 \u2987 d' \u2988\u27e8 n , \u03c3 \u27e9\n with natEQ n u\n \u27e6 d \/ u \u27e7 \u2987 d' \u2988\u27e8 .u , \u03c3 \u27e9 | Inl refl = apply \u03c3 d\n \u27e6 d \/ u \u27e7 \u2987 d' \u2988\u27e8 n , \u03c3 \u27e9 | Inr x = \u2987 d' \u2988\u27e8 n , \u03c3 \u27e9\n \u27e6 d \/ u \u27e7 (d1 \u2218 d2) = (\u27e6 d \/ u \u27e7 d1) \u2218 (\u27e6 d \/ u \u27e7 d2)\n \u27e6 d \/ u \u27e7 (d' \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) = (\u27e6 d \/ u \u27e7 d') \u27e8 \u03c41 \u21d2 \u03c42 \u27e9\n \u27e6 d \/ u \u27e7 (d' \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9) = (\u27e6 d \/ u \u27e7 d') \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"0ecf1536814a880e8cdbe60b6eef1ff1106504bf","subject":"make main theorem correspond to the one stated in the paper","message":"make main theorem correspond to the one stated in the paper\n\nOld-commit-hash: 84979b2067c54a685110f1bad744ba106a962a0f\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Correctness.agda","new_file":"Parametric\/Change\/Correctness.agda","new_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Change.Validity as Validity\nimport Parametric.Change.Specification as Specification\nimport Parametric.Change.Type as ChangeType\nimport Parametric.Change.Term as ChangeTerm\nimport Parametric.Change.Value as ChangeValue\nimport Parametric.Change.Evaluation as ChangeEvaluation\nimport Parametric.Change.Derive as Derive\nimport Parametric.Change.Implementation as Implementation\n\nmodule Parametric.Change.Correctness\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (\u0394Base : ChangeType.Structure Base)\n (diff-base : ChangeTerm.DiffStructure Const \u0394Base)\n (apply-base : ChangeTerm.ApplyStructure Const \u0394Base)\n (\u27e6apply-base\u27e7 : ChangeValue.ApplyStructure Const \u27e6_\u27e7Base \u0394Base)\n (\u27e6diff-base\u27e7 : ChangeValue.DiffStructure Const \u27e6_\u27e7Base \u0394Base)\n (meaning-\u2295-base : ChangeEvaluation.ApplyStructure\n \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base apply-base diff-base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7)\n (meaning-\u229d-base : ChangeEvaluation.DiffStructure\n \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base apply-base diff-base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7)\n (validity-structure : Validity.Structure \u27e6_\u27e7Base)\n (specification-structure : Specification.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const validity-structure)\n (derive-const : Derive.Structure Const \u0394Base)\n (implementation-structure : Implementation.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base\n validity-structure specification-structure\n \u27e6apply-base\u27e7 \u27e6diff-base\u27e7 derive-const)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen Validity.Structure \u27e6_\u27e7Base validity-structure\nopen Specification.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const validity-structure specification-structure\nopen ChangeType.Structure Base \u0394Base\nopen ChangeTerm.Structure Const \u0394Base diff-base apply-base\nopen ChangeValue.Structure Const \u27e6_\u27e7Base \u0394Base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7\nopen ChangeEvaluation.Structure\n \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base\n apply-base diff-base\n \u27e6apply-base\u27e7 \u27e6diff-base\u27e7\n meaning-\u2295-base meaning-\u229d-base\nopen Derive.Structure Const \u0394Base derive-const\nopen Implementation.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base validity-structure specification-structure\n \u27e6apply-base\u27e7 \u27e6diff-base\u27e7 derive-const implementation-structure\n\n-- The denotational properties of the `derive` transformation.\n-- In particular, the main theorem about it producing the correct\n-- incremental behavior.\n\nopen import Base.Denotation.Notation\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\n\nStructure : Set\nStructure = \u2200 {\u03a3 \u0393 \u03c4} (c : Const \u03a3 \u03c4) (ts : Terms \u0393 \u03a3)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n (ts-correct : implements-env \u03a3 (\u27e6 ts \u27e7\u0394Terms \u03c1 d\u03c1) (\u27e6 derive-terms ts \u27e7Terms (alternate \u03c1 \u03c1\u2032))) \u2192\n \u27e6 c \u27e7\u0394Const (\u27e6 ts \u27e7Terms \u03c1) (\u27e6 ts \u27e7\u0394Terms \u03c1 d\u03c1) \u2248\u208d \u03c4 \u208e \u27e6 derive-const c (fit-terms ts) (derive-terms ts) \u27e7 (alternate \u03c1 \u03c1\u2032)\n\nmodule Structure (derive-const-correct : Structure) where\n deriveVar-correct : \u2200 {\u03c4 \u0393} (x : Var \u0393 \u03c4)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n \u27e6 x \u27e7\u0394Var \u03c1 d\u03c1 \u2248\u208d \u03c4 \u208e \u27e6 deriveVar x \u27e7 (alternate \u03c1 \u03c1\u2032)\n deriveVar-correct this (v \u2022 \u03c1) (dv \u2022 d\u03c1) (dv\u2032 \u2022 d\u03c1\u2032) (dv\u2248dv\u2032 \u2022 d\u03c1\u2248d\u03c1\u2032) = dv\u2248dv\u2032\n deriveVar-correct (that x) (v \u2022 \u03c1) (dv \u2022 d\u03c1) (dv\u2032 \u2022 d\u03c1\u2032) (dv\u2248dv\u2032 \u2022 d\u03c1\u2248d\u03c1\u2032) = deriveVar-correct x \u03c1 d\u03c1 d\u03c1\u2032 d\u03c1\u2248d\u03c1\u2032\n\n -- That `derive t` implements \u27e6 t \u27e7\u0394\n derive-correct : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n \u27e6 t \u27e7\u0394 \u03c1 d\u03c1 \u2248\u208d \u03c4 \u208e \u27e6 derive t \u27e7 (alternate \u03c1 \u03c1\u2032)\n\n derive-terms-correct : \u2200 {\u03a3 \u0393} (ts : Terms \u0393 \u03a3)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n implements-env \u03a3 (\u27e6 ts \u27e7\u0394Terms \u03c1 d\u03c1) (\u27e6 derive-terms ts \u27e7Terms (alternate \u03c1 \u03c1\u2032))\n\n derive-terms-correct \u2205 \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 = \u2205\n derive-terms-correct (t \u2022 ts) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n derive-correct t \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 \u2022 derive-terms-correct ts \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n\n derive-correct (const c ts) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n derive-const-correct c ts \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n (derive-terms-correct ts \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032)\n derive-correct (var x) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n deriveVar-correct x \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n derive-correct (app {\u03c3} {\u03c4} s t) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n = subst (\u03bb \u27e6t\u27e7 \u2192 \u27e6 app s t \u27e7\u0394 \u03c1 d\u03c1 \u2248\u208d \u03c4 \u208e (\u27e6 derive s \u27e7Term (alternate \u03c1 \u03c1\u2032)) \u27e6t\u27e7 (\u27e6 derive t \u27e7Term (alternate \u03c1 \u03c1\u2032))) (\u27e6fit\u27e7 t \u03c1 \u03c1\u2032)\n (derive-correct {\u03c3 \u21d2 \u03c4} s \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n (\u27e6 t \u27e7 \u03c1) (\u27e6 t \u27e7\u0394 \u03c1 d\u03c1) (\u27e6 derive t \u27e7 (alternate \u03c1 \u03c1\u2032)) (derive-correct {\u03c3} t \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032))\n\n derive-correct (abs {\u03c3} {\u03c4} t) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n \u03bb w dw w\u2032 dw\u2248w\u2032 \u2192\n derive-correct t (w \u2022 \u03c1) (dw \u2022 d\u03c1) (w\u2032 \u2022 \u03c1\u2032) (dw\u2248w\u2032 \u2022 d\u03c1\u2248\u03c1\u2032)\n\n derive-correct-closed : \u2200 {\u03c4} (t : Term \u2205 \u03c4) \u2192\n \u27e6 t \u27e7\u0394 \u2205 \u2205 \u2248\u208d \u03c4 \u208e \u27e6 derive t \u27e7 \u2205\n\n derive-correct-closed t = derive-correct t \u2205 \u2205 \u2205 \u2205\n\n main-theorem : \u2200 {\u03c3 \u03c4}\n {f : Term \u2205 (\u03c3 \u21d2 \u03c4)} {s : Term \u2205 \u03c3} {ds : Term \u2205 (\u0394Type \u03c3)} \u2192\n {dv : \u0394\u208d \u03c3 \u208e (\u27e6 s \u27e7 \u2205)} {erasure : dv \u2248\u208d \u03c3 \u208e (\u27e6 ds \u27e7 \u2205)} \u2192\n\n \u27e6 app f (s \u2295\u208d \u03c3 \u208e ds) \u27e7 \u2261 \u27e6 app f s \u2295\u208d \u03c4 \u208e app (app (derive f) s) ds \u27e7\n\n main-theorem {\u03c3} {\u03c4} {f} {s} {ds} {dv} {erasure} =\n let\n g = \u27e6 f \u27e7 \u2205\n \u0394g = \u27e6 f \u27e7\u0394 \u2205 \u2205\n \u0394g\u2032 = \u27e6 derive f \u27e7 \u2205\n v = \u27e6 s \u27e7 \u2205\n dv\u2032 = \u27e6 ds \u27e7 \u2205\n u = \u27e6 s \u2295\u208d \u03c3 \u208e ds \u27e7 \u2205\n -- \u0394output-term = app (app (derive f) x) (y \u229d x)\n in\n ext {A = \u27e6 \u2205 \u27e7Context} (\u03bb { \u2205 \u2192\n begin\n g u\n \u2261\u27e8 cong g (sym (meaning-\u2295 {t = s} {\u0394t = ds})) \u27e9\n g (v \u27e6\u2295\u208d \u03c3 \u208e\u27e7 dv\u2032)\n \u2261\u27e8 cong g (sym (carry-over {\u03c3} dv erasure)) \u27e9\n g (v \u229e\u208d \u03c3 \u208e dv)\n \u2261\u27e8 corollary-closed {\u03c3} {\u03c4} f v dv \u27e9\n g v \u229e\u208d \u03c4 \u208e call-change {\u03c3} {\u03c4} \u0394g v dv\n \u2261\u27e8 carry-over {\u03c4} (call-change {\u03c3} {\u03c4} \u0394g v dv)\n (derive-correct f \u2205 \u2205 \u2205 \u2205 v dv dv\u2032 erasure) \u27e9\n g v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u0394g\u2032 v dv\u2032\n \u2261\u27e8 meaning-\u2295 {t = app f s} {\u0394t = app (app (derive f) s) ds} \u27e9\n \u27e6 app f s \u2295\u208d \u03c4 \u208e app (app (derive f) s) ds \u27e7 \u2205\n \u220e}) where open \u2261-Reasoning\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Change.Validity as Validity\nimport Parametric.Change.Specification as Specification\nimport Parametric.Change.Type as ChangeType\nimport Parametric.Change.Term as ChangeTerm\nimport Parametric.Change.Value as ChangeValue\nimport Parametric.Change.Evaluation as ChangeEvaluation\nimport Parametric.Change.Derive as Derive\nimport Parametric.Change.Implementation as Implementation\n\nmodule Parametric.Change.Correctness\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (\u0394Base : ChangeType.Structure Base)\n (diff-base : ChangeTerm.DiffStructure Const \u0394Base)\n (apply-base : ChangeTerm.ApplyStructure Const \u0394Base)\n (\u27e6apply-base\u27e7 : ChangeValue.ApplyStructure Const \u27e6_\u27e7Base \u0394Base)\n (\u27e6diff-base\u27e7 : ChangeValue.DiffStructure Const \u27e6_\u27e7Base \u0394Base)\n (meaning-\u2295-base : ChangeEvaluation.ApplyStructure\n \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base apply-base diff-base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7)\n (meaning-\u229d-base : ChangeEvaluation.DiffStructure\n \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base apply-base diff-base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7)\n (validity-structure : Validity.Structure \u27e6_\u27e7Base)\n (specification-structure : Specification.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const validity-structure)\n (derive-const : Derive.Structure Const \u0394Base)\n (implementation-structure : Implementation.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base\n validity-structure specification-structure\n \u27e6apply-base\u27e7 \u27e6diff-base\u27e7 derive-const)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen Validity.Structure \u27e6_\u27e7Base validity-structure\nopen Specification.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const validity-structure specification-structure\nopen ChangeType.Structure Base \u0394Base\nopen ChangeTerm.Structure Const \u0394Base diff-base apply-base\nopen ChangeValue.Structure Const \u27e6_\u27e7Base \u0394Base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7\nopen ChangeEvaluation.Structure\n \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base\n apply-base diff-base\n \u27e6apply-base\u27e7 \u27e6diff-base\u27e7\n meaning-\u2295-base meaning-\u229d-base\nopen Derive.Structure Const \u0394Base derive-const\nopen Implementation.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base validity-structure specification-structure\n \u27e6apply-base\u27e7 \u27e6diff-base\u27e7 derive-const implementation-structure\n\n-- The denotational properties of the `derive` transformation.\n-- In particular, the main theorem about it producing the correct\n-- incremental behavior.\n\nopen import Base.Denotation.Notation\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\n\nStructure : Set\nStructure = \u2200 {\u03a3 \u0393 \u03c4} (c : Const \u03a3 \u03c4) (ts : Terms \u0393 \u03a3)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n (ts-correct : implements-env \u03a3 (\u27e6 ts \u27e7\u0394Terms \u03c1 d\u03c1) (\u27e6 derive-terms ts \u27e7Terms (alternate \u03c1 \u03c1\u2032))) \u2192\n \u27e6 c \u27e7\u0394Const (\u27e6 ts \u27e7Terms \u03c1) (\u27e6 ts \u27e7\u0394Terms \u03c1 d\u03c1) \u2248\u208d \u03c4 \u208e \u27e6 derive-const c (fit-terms ts) (derive-terms ts) \u27e7 (alternate \u03c1 \u03c1\u2032)\n\nmodule Structure (derive-const-correct : Structure) where\n deriveVar-correct : \u2200 {\u03c4 \u0393} (x : Var \u0393 \u03c4)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n \u27e6 x \u27e7\u0394Var \u03c1 d\u03c1 \u2248\u208d \u03c4 \u208e \u27e6 deriveVar x \u27e7 (alternate \u03c1 \u03c1\u2032)\n deriveVar-correct this (v \u2022 \u03c1) (dv \u2022 d\u03c1) (dv\u2032 \u2022 d\u03c1\u2032) (dv\u2248dv\u2032 \u2022 d\u03c1\u2248d\u03c1\u2032) = dv\u2248dv\u2032\n deriveVar-correct (that x) (v \u2022 \u03c1) (dv \u2022 d\u03c1) (dv\u2032 \u2022 d\u03c1\u2032) (dv\u2248dv\u2032 \u2022 d\u03c1\u2248d\u03c1\u2032) = deriveVar-correct x \u03c1 d\u03c1 d\u03c1\u2032 d\u03c1\u2248d\u03c1\u2032\n\n -- That `derive t` implements \u27e6 t \u27e7\u0394\n derive-correct : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n \u27e6 t \u27e7\u0394 \u03c1 d\u03c1 \u2248\u208d \u03c4 \u208e \u27e6 derive t \u27e7 (alternate \u03c1 \u03c1\u2032)\n\n derive-terms-correct : \u2200 {\u03a3 \u0393} (ts : Terms \u0393 \u03a3)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n implements-env \u03a3 (\u27e6 ts \u27e7\u0394Terms \u03c1 d\u03c1) (\u27e6 derive-terms ts \u27e7Terms (alternate \u03c1 \u03c1\u2032))\n\n derive-terms-correct \u2205 \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 = \u2205\n derive-terms-correct (t \u2022 ts) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n derive-correct t \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 \u2022 derive-terms-correct ts \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n\n derive-correct (const c ts) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n derive-const-correct c ts \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n (derive-terms-correct ts \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032)\n derive-correct (var x) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n deriveVar-correct x \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n derive-correct (app {\u03c3} {\u03c4} s t) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n = subst (\u03bb \u27e6t\u27e7 \u2192 \u27e6 app s t \u27e7\u0394 \u03c1 d\u03c1 \u2248\u208d \u03c4 \u208e (\u27e6 derive s \u27e7Term (alternate \u03c1 \u03c1\u2032)) \u27e6t\u27e7 (\u27e6 derive t \u27e7Term (alternate \u03c1 \u03c1\u2032))) (\u27e6fit\u27e7 t \u03c1 \u03c1\u2032)\n (derive-correct {\u03c3 \u21d2 \u03c4} s \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n (\u27e6 t \u27e7 \u03c1) (\u27e6 t \u27e7\u0394 \u03c1 d\u03c1) (\u27e6 derive t \u27e7 (alternate \u03c1 \u03c1\u2032)) (derive-correct {\u03c3} t \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032))\n\n derive-correct (abs {\u03c3} {\u03c4} t) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n \u03bb w dw w\u2032 dw\u2248w\u2032 \u2192\n derive-correct t (w \u2022 \u03c1) (dw \u2022 d\u03c1) (w\u2032 \u2022 \u03c1\u2032) (dw\u2248w\u2032 \u2022 d\u03c1\u2248\u03c1\u2032)\n\n -- Our main theorem, as we used to state it in the paper.\n main-theorem : \u2200 {\u03c3 \u03c4}\n {f : Term \u2205 (\u03c3 \u21d2 \u03c4)} {x : Term \u2205 \u03c3} {y : Term \u2205 \u03c3}\n \u2192 \u27e6 app f y \u27e7\n \u2261 \u27e6 app f x \u2295\u208d \u03c4 \u208e app (app (derive f) x) (y \u229d x) \u27e7\n\n main-theorem {\u03c3} {\u03c4} {f} {x} {y} =\n let\n h = \u27e6 f \u27e7 \u2205\n \u0394h = \u27e6 f \u27e7\u0394 \u2205 \u2205\n \u0394h\u2032 = \u27e6 derive f \u27e7 \u2205\n v = \u27e6 x \u27e7 \u2205\n u = \u27e6 y \u27e7 \u2205\n \u0394output-term = app (app (derive f) x) (y \u229d x)\n in\n ext {A = \u27e6 \u2205 \u27e7Context} (\u03bb { \u2205 \u2192\n begin\n h u\n \u2261\u27e8 cong h (sym (update-diff\u208d \u03c3 \u208e u v)) \u27e9\n h (v \u229e\u208d \u03c3 \u208e (u \u229f\u208d \u03c3 \u208e v))\n \u2261\u27e8 corollary-closed {\u03c3} {\u03c4} f v (u \u229f\u208d \u03c3 \u208e v) \u27e9\n h v \u229e\u208d \u03c4 \u208e call-change {\u03c3} {\u03c4} \u0394h v (u \u229f\u208d \u03c3 \u208e v)\n \u2261\u27e8 carry-over {\u03c4}\n (call-change {\u03c3} {\u03c4} \u0394h v (u \u229f\u208d \u03c3 \u208e v))\n (derive-correct f\n \u2205 \u2205 \u2205 \u2205 v (u \u229f\u208d \u03c3 \u208e v) (u \u27e6\u229d\u208d \u03c3 \u208e\u27e7 v) (u\u229fv\u2248u\u229dv {\u03c3} {u} {v})) \u27e9\n h v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u0394h\u2032 v (u \u27e6\u229d\u208d \u03c3 \u208e\u27e7 v)\n \u2261\u27e8 trans\n (cong (\u03bb hole \u2192 h v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u0394h\u2032 v hole) (meaning-\u229d {\u03c3} {s = y} {x}))\n (meaning-\u2295 {t = app f x} {\u0394t = \u0394output-term}) \u27e9\n \u27e6 app f x \u2295\u208d \u03c4 \u208e app (app (derive f) x) (y \u229d x) \u27e7 \u2205\n \u220e}) where open \u2261-Reasoning\n\n -- A corollary, closer to what we state in the paper.\n main-theorem-coroll : \u2200 {\u03c3 \u03c4}\n {f : Term \u2205 (\u03c3 \u21d2 \u03c4)} {x : Term \u2205 \u03c3} {dx : Term \u2205 (\u0394Type \u03c3)}\n \u2192 \u27e6 app f (x \u2295\u208d \u03c3 \u208e dx) \u27e7\n \u2261 \u27e6 app f x \u2295\u208d \u03c4 \u208e app (app (derive f) x) ((x \u2295\u208d \u03c3 \u208e dx) \u229d x) \u27e7\n main-theorem-coroll {\u03c3} {\u03c4} {f} {x} {dx} = main-theorem {\u03c3} {\u03c4} {f} {x} {x \u2295\u208d \u03c3 \u208e dx}\n\n -- For the statement in the paper, we'd need to talk about valid changes in\n -- the lambda calculus. In fact we can, thanks to the `implements` relation;\n -- but I guess the required proof must be done directly from derive-correct,\n -- not from main-theorem.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"56284e892af435d1f8e9968c5f9e7aaa7fa61706","subject":"Agda: start reflecting back changes to the main theorem in the paper","message":"Agda: start reflecting back changes to the main theorem in the paper\n\nWe simplified the statement in the paper by talking about valid changes.\nThough this is a minor change, this should be reflected in Agda, also to\nget an exact statement for the paper.\n\nThis commit discusses what to do; I didn't try writing the real\nstatement.\n\nOld-commit-hash: 0d78ce6f1fab80aa61430ee3241ca2c7080028ba\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Correctness.agda","new_file":"Parametric\/Change\/Correctness.agda","new_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Change.Validity as Validity\nimport Parametric.Change.Specification as Specification\nimport Parametric.Change.Type as ChangeType\nimport Parametric.Change.Term as ChangeTerm\nimport Parametric.Change.Value as ChangeValue\nimport Parametric.Change.Evaluation as ChangeEvaluation\nimport Parametric.Change.Derive as Derive\nimport Parametric.Change.Implementation as Implementation\n\nmodule Parametric.Change.Correctness\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (\u0394Base : ChangeType.Structure Base)\n (diff-base : ChangeTerm.DiffStructure Const \u0394Base)\n (apply-base : ChangeTerm.ApplyStructure Const \u0394Base)\n (\u27e6apply-base\u27e7 : ChangeValue.ApplyStructure Const \u27e6_\u27e7Base \u0394Base)\n (\u27e6diff-base\u27e7 : ChangeValue.DiffStructure Const \u27e6_\u27e7Base \u0394Base)\n (meaning-\u2295-base : ChangeEvaluation.ApplyStructure\n \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base apply-base diff-base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7)\n (meaning-\u229d-base : ChangeEvaluation.DiffStructure\n \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base apply-base diff-base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7)\n (validity-structure : Validity.Structure \u27e6_\u27e7Base)\n (specification-structure : Specification.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const validity-structure)\n (derive-const : Derive.Structure Const \u0394Base)\n (implementation-structure : Implementation.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base\n validity-structure specification-structure\n \u27e6apply-base\u27e7 \u27e6diff-base\u27e7 derive-const)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen Validity.Structure \u27e6_\u27e7Base validity-structure\nopen Specification.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const validity-structure specification-structure\nopen ChangeType.Structure Base \u0394Base\nopen ChangeTerm.Structure Const \u0394Base diff-base apply-base\nopen ChangeValue.Structure Const \u27e6_\u27e7Base \u0394Base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7\nopen ChangeEvaluation.Structure\n \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base\n apply-base diff-base\n \u27e6apply-base\u27e7 \u27e6diff-base\u27e7\n meaning-\u2295-base meaning-\u229d-base\nopen Derive.Structure Const \u0394Base derive-const\nopen Implementation.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base validity-structure specification-structure\n \u27e6apply-base\u27e7 \u27e6diff-base\u27e7 derive-const implementation-structure\n\n-- The denotational properties of the `derive` transformation.\n-- In particular, the main theorem about it producing the correct\n-- incremental behavior.\n\nopen import Base.Denotation.Notation\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\n\nStructure : Set\nStructure = \u2200 {\u03a3 \u0393 \u03c4} (c : Const \u03a3 \u03c4) (ts : Terms \u0393 \u03a3)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n (ts-correct : implements-env \u03a3 (\u27e6 ts \u27e7\u0394Terms \u03c1 d\u03c1) (\u27e6 derive-terms ts \u27e7Terms (alternate \u03c1 \u03c1\u2032))) \u2192\n \u27e6 c \u27e7\u0394Const (\u27e6 ts \u27e7Terms \u03c1) (\u27e6 ts \u27e7\u0394Terms \u03c1 d\u03c1) \u2248\u208d \u03c4 \u208e \u27e6 derive-const c (fit-terms ts) (derive-terms ts) \u27e7 (alternate \u03c1 \u03c1\u2032)\n\nmodule Structure (derive-const-correct : Structure) where\n deriveVar-correct : \u2200 {\u03c4 \u0393} (x : Var \u0393 \u03c4)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n \u27e6 x \u27e7\u0394Var \u03c1 d\u03c1 \u2248\u208d \u03c4 \u208e \u27e6 deriveVar x \u27e7 (alternate \u03c1 \u03c1\u2032)\n deriveVar-correct this (v \u2022 \u03c1) (dv \u2022 d\u03c1) (dv\u2032 \u2022 d\u03c1\u2032) (dv\u2248dv\u2032 \u2022 d\u03c1\u2248d\u03c1\u2032) = dv\u2248dv\u2032\n deriveVar-correct (that x) (v \u2022 \u03c1) (dv \u2022 d\u03c1) (dv\u2032 \u2022 d\u03c1\u2032) (dv\u2248dv\u2032 \u2022 d\u03c1\u2248d\u03c1\u2032) = deriveVar-correct x \u03c1 d\u03c1 d\u03c1\u2032 d\u03c1\u2248d\u03c1\u2032\n\n -- That `derive t` implements \u27e6 t \u27e7\u0394\n derive-correct : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n \u27e6 t \u27e7\u0394 \u03c1 d\u03c1 \u2248\u208d \u03c4 \u208e \u27e6 derive t \u27e7 (alternate \u03c1 \u03c1\u2032)\n\n derive-terms-correct : \u2200 {\u03a3 \u0393} (ts : Terms \u0393 \u03a3)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n implements-env \u03a3 (\u27e6 ts \u27e7\u0394Terms \u03c1 d\u03c1) (\u27e6 derive-terms ts \u27e7Terms (alternate \u03c1 \u03c1\u2032))\n\n derive-terms-correct \u2205 \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 = \u2205\n derive-terms-correct (t \u2022 ts) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n derive-correct t \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 \u2022 derive-terms-correct ts \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n\n derive-correct (const c ts) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n derive-const-correct c ts \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n (derive-terms-correct ts \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032)\n derive-correct (var x) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n deriveVar-correct x \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n derive-correct (app {\u03c3} {\u03c4} s t) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n = subst (\u03bb \u27e6t\u27e7 \u2192 \u27e6 app s t \u27e7\u0394 \u03c1 d\u03c1 \u2248\u208d \u03c4 \u208e (\u27e6 derive s \u27e7Term (alternate \u03c1 \u03c1\u2032)) \u27e6t\u27e7 (\u27e6 derive t \u27e7Term (alternate \u03c1 \u03c1\u2032))) (\u27e6fit\u27e7 t \u03c1 \u03c1\u2032)\n (derive-correct {\u03c3 \u21d2 \u03c4} s \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n (\u27e6 t \u27e7 \u03c1) (\u27e6 t \u27e7\u0394 \u03c1 d\u03c1) (\u27e6 derive t \u27e7 (alternate \u03c1 \u03c1\u2032)) (derive-correct {\u03c3} t \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032))\n\n derive-correct (abs {\u03c3} {\u03c4} t) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n \u03bb w dw w\u2032 dw\u2248w\u2032 \u2192\n derive-correct t (w \u2022 \u03c1) (dw \u2022 d\u03c1) (w\u2032 \u2022 \u03c1\u2032) (dw\u2248w\u2032 \u2022 d\u03c1\u2248\u03c1\u2032)\n\n -- Our main theorem, as we used to state it in the paper.\n main-theorem : \u2200 {\u03c3 \u03c4}\n {f : Term \u2205 (\u03c3 \u21d2 \u03c4)} {x : Term \u2205 \u03c3} {y : Term \u2205 \u03c3}\n \u2192 \u27e6 app f y \u27e7\n \u2261 \u27e6 app f x \u2295\u208d \u03c4 \u208e app (app (derive f) x) (y \u229d x) \u27e7\n\n main-theorem {\u03c3} {\u03c4} {f} {x} {y} =\n let\n h = \u27e6 f \u27e7 \u2205\n \u0394h = \u27e6 f \u27e7\u0394 \u2205 \u2205\n \u0394h\u2032 = \u27e6 derive f \u27e7 \u2205\n v = \u27e6 x \u27e7 \u2205\n u = \u27e6 y \u27e7 \u2205\n \u0394output-term = app (app (derive f) x) (y \u229d x)\n in\n ext {A = \u27e6 \u2205 \u27e7Context} (\u03bb { \u2205 \u2192\n begin\n h u\n \u2261\u27e8 cong h (sym (update-diff\u208d \u03c3 \u208e u v)) \u27e9\n h (v \u229e\u208d \u03c3 \u208e (u \u229f\u208d \u03c3 \u208e v))\n \u2261\u27e8 corollary-closed {\u03c3} {\u03c4} f v (u \u229f\u208d \u03c3 \u208e v) \u27e9\n h v \u229e\u208d \u03c4 \u208e call-change {\u03c3} {\u03c4} \u0394h v (u \u229f\u208d \u03c3 \u208e v)\n \u2261\u27e8 carry-over {\u03c4}\n (call-change {\u03c3} {\u03c4} \u0394h v (u \u229f\u208d \u03c3 \u208e v))\n (derive-correct f\n \u2205 \u2205 \u2205 \u2205 v (u \u229f\u208d \u03c3 \u208e v) (u \u27e6\u229d\u208d \u03c3 \u208e\u27e7 v) (u\u229fv\u2248u\u229dv {\u03c3} {u} {v})) \u27e9\n h v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u0394h\u2032 v (u \u27e6\u229d\u208d \u03c3 \u208e\u27e7 v)\n \u2261\u27e8 trans\n (cong (\u03bb hole \u2192 h v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u0394h\u2032 v hole) (meaning-\u229d {\u03c3} {s = y} {x}))\n (meaning-\u2295 {t = app f x} {\u0394t = \u0394output-term}) \u27e9\n \u27e6 app f x \u2295\u208d \u03c4 \u208e app (app (derive f) x) (y \u229d x) \u27e7 \u2205\n \u220e}) where open \u2261-Reasoning\n\n -- A corollary, closer to what we state in the paper.\n main-theorem-coroll : \u2200 {\u03c3 \u03c4}\n {f : Term \u2205 (\u03c3 \u21d2 \u03c4)} {x : Term \u2205 \u03c3} {dx : Term \u2205 (\u0394Type \u03c3)}\n \u2192 \u27e6 app f (x \u2295\u208d \u03c3 \u208e dx) \u27e7\n \u2261 \u27e6 app f x \u2295\u208d \u03c4 \u208e app (app (derive f) x) ((x \u2295\u208d \u03c3 \u208e dx) \u229d x) \u27e7\n main-theorem-coroll {\u03c3} {\u03c4} {f} {x} {dx} = main-theorem {\u03c3} {\u03c4} {f} {x} {x \u2295\u208d \u03c3 \u208e dx}\n\n -- For the statement in the paper, we'd need to talk about valid changes in\n -- the lambda calculus. In fact we can, thanks to the `implements` relation;\n -- but I guess the required proof must be done directly from derive-correct,\n -- not from main-theorem.\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Change.Validity as Validity\nimport Parametric.Change.Specification as Specification\nimport Parametric.Change.Type as ChangeType\nimport Parametric.Change.Term as ChangeTerm\nimport Parametric.Change.Value as ChangeValue\nimport Parametric.Change.Evaluation as ChangeEvaluation\nimport Parametric.Change.Derive as Derive\nimport Parametric.Change.Implementation as Implementation\n\nmodule Parametric.Change.Correctness\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (\u0394Base : ChangeType.Structure Base)\n (diff-base : ChangeTerm.DiffStructure Const \u0394Base)\n (apply-base : ChangeTerm.ApplyStructure Const \u0394Base)\n (\u27e6apply-base\u27e7 : ChangeValue.ApplyStructure Const \u27e6_\u27e7Base \u0394Base)\n (\u27e6diff-base\u27e7 : ChangeValue.DiffStructure Const \u27e6_\u27e7Base \u0394Base)\n (meaning-\u2295-base : ChangeEvaluation.ApplyStructure\n \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base apply-base diff-base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7)\n (meaning-\u229d-base : ChangeEvaluation.DiffStructure\n \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base apply-base diff-base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7)\n (validity-structure : Validity.Structure \u27e6_\u27e7Base)\n (specification-structure : Specification.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const validity-structure)\n (derive-const : Derive.Structure Const \u0394Base)\n (implementation-structure : Implementation.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base\n validity-structure specification-structure\n \u27e6apply-base\u27e7 \u27e6diff-base\u27e7 derive-const)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen Validity.Structure \u27e6_\u27e7Base validity-structure\nopen Specification.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const validity-structure specification-structure\nopen ChangeType.Structure Base \u0394Base\nopen ChangeTerm.Structure Const \u0394Base diff-base apply-base\nopen ChangeValue.Structure Const \u27e6_\u27e7Base \u0394Base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7\nopen ChangeEvaluation.Structure\n \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base\n apply-base diff-base\n \u27e6apply-base\u27e7 \u27e6diff-base\u27e7\n meaning-\u2295-base meaning-\u229d-base\nopen Derive.Structure Const \u0394Base derive-const\nopen Implementation.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const \u0394Base validity-structure specification-structure\n \u27e6apply-base\u27e7 \u27e6diff-base\u27e7 derive-const implementation-structure\n\n-- The denotational properties of the `derive` transformation.\n-- In particular, the main theorem about it producing the correct\n-- incremental behavior.\n\nopen import Base.Denotation.Notation\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\n\nStructure : Set\nStructure = \u2200 {\u03a3 \u0393 \u03c4} (c : Const \u03a3 \u03c4) (ts : Terms \u0393 \u03a3)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n (ts-correct : implements-env \u03a3 (\u27e6 ts \u27e7\u0394Terms \u03c1 d\u03c1) (\u27e6 derive-terms ts \u27e7Terms (alternate \u03c1 \u03c1\u2032))) \u2192\n \u27e6 c \u27e7\u0394Const (\u27e6 ts \u27e7Terms \u03c1) (\u27e6 ts \u27e7\u0394Terms \u03c1 d\u03c1) \u2248\u208d \u03c4 \u208e \u27e6 derive-const c (fit-terms ts) (derive-terms ts) \u27e7 (alternate \u03c1 \u03c1\u2032)\n\nmodule Structure (derive-const-correct : Structure) where\n deriveVar-correct : \u2200 {\u03c4 \u0393} (x : Var \u0393 \u03c4)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n \u27e6 x \u27e7\u0394Var \u03c1 d\u03c1 \u2248\u208d \u03c4 \u208e \u27e6 deriveVar x \u27e7 (alternate \u03c1 \u03c1\u2032)\n deriveVar-correct this (v \u2022 \u03c1) (dv \u2022 d\u03c1) (dv\u2032 \u2022 d\u03c1\u2032) (dv\u2248dv\u2032 \u2022 d\u03c1\u2248d\u03c1\u2032) = dv\u2248dv\u2032\n deriveVar-correct (that x) (v \u2022 \u03c1) (dv \u2022 d\u03c1) (dv\u2032 \u2022 d\u03c1\u2032) (dv\u2248dv\u2032 \u2022 d\u03c1\u2248d\u03c1\u2032) = deriveVar-correct x \u03c1 d\u03c1 d\u03c1\u2032 d\u03c1\u2248d\u03c1\u2032\n\n -- That `derive t` implements \u27e6 t \u27e7\u0394\n derive-correct : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n \u27e6 t \u27e7\u0394 \u03c1 d\u03c1 \u2248\u208d \u03c4 \u208e \u27e6 derive t \u27e7 (alternate \u03c1 \u03c1\u2032)\n\n derive-terms-correct : \u2200 {\u03a3 \u0393} (ts : Terms \u0393 \u03a3)\n (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) (\u03c1\u2032 : \u27e6 mapContext \u0394Type \u0393 \u27e7) (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 d\u03c1 \u03c1\u2032) \u2192\n implements-env \u03a3 (\u27e6 ts \u27e7\u0394Terms \u03c1 d\u03c1) (\u27e6 derive-terms ts \u27e7Terms (alternate \u03c1 \u03c1\u2032))\n\n derive-terms-correct \u2205 \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 = \u2205\n derive-terms-correct (t \u2022 ts) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n derive-correct t \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 \u2022 derive-terms-correct ts \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n\n derive-correct (const c ts) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n derive-const-correct c ts \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n (derive-terms-correct ts \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032)\n derive-correct (var x) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n deriveVar-correct x \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n derive-correct (app {\u03c3} {\u03c4} s t) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n = subst (\u03bb \u27e6t\u27e7 \u2192 \u27e6 app s t \u27e7\u0394 \u03c1 d\u03c1 \u2248\u208d \u03c4 \u208e (\u27e6 derive s \u27e7Term (alternate \u03c1 \u03c1\u2032)) \u27e6t\u27e7 (\u27e6 derive t \u27e7Term (alternate \u03c1 \u03c1\u2032))) (\u27e6fit\u27e7 t \u03c1 \u03c1\u2032)\n (derive-correct {\u03c3 \u21d2 \u03c4} s \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032\n (\u27e6 t \u27e7 \u03c1) (\u27e6 t \u27e7\u0394 \u03c1 d\u03c1) (\u27e6 derive t \u27e7 (alternate \u03c1 \u03c1\u2032)) (derive-correct {\u03c3} t \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032))\n\n derive-correct (abs {\u03c3} {\u03c4} t) \u03c1 d\u03c1 \u03c1\u2032 d\u03c1\u2248\u03c1\u2032 =\n \u03bb w dw w\u2032 dw\u2248w\u2032 \u2192\n derive-correct t (w \u2022 \u03c1) (dw \u2022 d\u03c1) (w\u2032 \u2022 \u03c1\u2032) (dw\u2248w\u2032 \u2022 d\u03c1\u2248\u03c1\u2032)\n\n main-theorem : \u2200 {\u03c3 \u03c4}\n {f : Term \u2205 (\u03c3 \u21d2 \u03c4)} {x : Term \u2205 \u03c3} {y : Term \u2205 \u03c3}\n \u2192 \u27e6 app f y \u27e7\n \u2261 \u27e6 app f x \u2295\u208d \u03c4 \u208e app (app (derive f) x) (y \u229d x) \u27e7\n\n main-theorem {\u03c3} {\u03c4} {f} {x} {y} =\n let\n h = \u27e6 f \u27e7 \u2205\n \u0394h = \u27e6 f \u27e7\u0394 \u2205 \u2205\n \u0394h\u2032 = \u27e6 derive f \u27e7 \u2205\n v = \u27e6 x \u27e7 \u2205\n u = \u27e6 y \u27e7 \u2205\n \u0394output-term = app (app (derive f) x) (y \u229d x)\n in\n ext {A = \u27e6 \u2205 \u27e7Context} (\u03bb { \u2205 \u2192\n begin\n h u\n \u2261\u27e8 cong h (sym (update-diff\u208d \u03c3 \u208e u v)) \u27e9\n h (v \u229e\u208d \u03c3 \u208e (u \u229f\u208d \u03c3 \u208e v))\n \u2261\u27e8 corollary-closed {\u03c3} {\u03c4} f v (u \u229f\u208d \u03c3 \u208e v) \u27e9\n h v \u229e\u208d \u03c4 \u208e call-change {\u03c3} {\u03c4} \u0394h v (u \u229f\u208d \u03c3 \u208e v)\n \u2261\u27e8 carry-over {\u03c4}\n (call-change {\u03c3} {\u03c4} \u0394h v (u \u229f\u208d \u03c3 \u208e v))\n (derive-correct f\n \u2205 \u2205 \u2205 \u2205 v (u \u229f\u208d \u03c3 \u208e v) (u \u27e6\u229d\u208d \u03c3 \u208e\u27e7 v) (u\u229fv\u2248u\u229dv {\u03c3} {u} {v})) \u27e9\n h v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u0394h\u2032 v (u \u27e6\u229d\u208d \u03c3 \u208e\u27e7 v)\n \u2261\u27e8 trans\n (cong (\u03bb hole \u2192 h v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u0394h\u2032 v hole) (meaning-\u229d {\u03c3} {s = y} {x}))\n (meaning-\u2295 {t = app f x} {\u0394t = \u0394output-term}) \u27e9\n \u27e6 app f x \u2295\u208d \u03c4 \u208e app (app (derive f) x) (y \u229d x) \u27e7 \u2205\n \u220e}) where open \u2261-Reasoning\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9de1d2be11ad3758770500baab5298c68874ca56","subject":"uploading all","message":"uploading all\n","repos":"xekoukou\/sparrow,xekoukou\/sparrow,xekoukou\/sparrow,xekoukou\/sparrow","old_file":"agda\/WellFormedLF.agda","new_file":"agda\/WellFormedLF.agda","new_contents":"module WellFormedLF where\n\nopen import Common\nopen import LinLogic\nopen import SetLL\n\n--data IndexLF : \u2200{u} \u2192 {i : Size} \u2192 {j : Size< \u2191 i} \u2192 {rll : LinLogic j {u}} \u2192 {ll : LinLogic i {u}} \u2192 LFun {u} {i} {j} {rll} {ll} \u2192 Set where\n-- \u2193 : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 \u2200{u rll ll} \u2192 (lf : LFun {u} {i} {j} {rll} {ll}) \u2192 IndexLF lf\n-- _\u2190\u2282_ : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 {k : Size< \u2191 j} \u2192 \u2200{u rll pll ell ll ind elf prf lf}\n-- \u2192 IndexLF elf\n-- \u2192 IndexLF (_\u2282_ {u} {i} {j} {k} {pll} {ll} {ell} {rll} {ind} elf {{prf}} lf)\n-- _\u2282\u2192_ : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 {k : Size< \u2191 j} \u2192 \u2200{u rll pll ell ll ind elf prf lf}\n-- \u2192 IndexLF lf\n-- \u2192 IndexLF (_\u2282_ {u} {i} {j} {k} {pll} {ll} {ell} {rll} {ind} elf {{prf}} lf)\n-- tr : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 \u2200{u ll orll rll} \u2192 {{ltr : LLTr orll ll}} \u2192 {lf : LFun {u} {i} {j} {rll} {orll}}\n-- \u2192 IndexLF lf \u2192 IndexLF (tr {{ltr = ltr}} lf) \n--\n\n-- _\u2190\u2282_ : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 {rll : LinLogic j {u}} \u2192 {ll : LinLogic i {u}} \u2192 (lf : LFun {u} {i} {j} {rll} {ll}) \u2192 IndexLF lf\n\n-- \u2193 probably the subtrees that contain all the inputs. \n-- We need to keep truck of all the latest subtrees that are outputs of coms. We can then check whether a transformation permutates them. If so , the tr is acceptable.\n-- transformations inside a subtree are acceptable.\n-- tranformations between two subtrees are only acceptable if the next com depends on both of them.\n-- If more than one subtree depends on a specific subtree, but the two do not depend on each other, we first separate the elements for the first subtree and then for the other.\n-- Since these coms can be independently executed, there can be tranformations of one subtree that can not be done while the others are ready. --> We remove the separation transformation and consider that\n-- the unexecuted transformations are done on those separated elements.\n\n-- > The input tree contains subtrees that represent exactly the input of the coms it can execute.\n\n-- 2 TWO\n-- We need three structures.\n-- THe first is used to identify inputs from the same com. When a tranformation splits these coms, we also split the set into two sets. Next we track all the inputs that are part of the tranformation, be it sets of inputs of a specific com or individual inputs. From all these sets, at least one item from each set needs to be the input of the next com.\n-- The set of coms that are used to allow for commutation of inputs.\n\nmodule _ where\n\n open import Data.Vec\n \n data Ancestor : Set where\n orig : Ancestor\n anc : \u2115 \u2192 Ancestor \u2192 Ancestor\n manc : \u2115 \u2192 \u2200{n} \u2192 Vec Ancestor n \u2192 Ancestor \u2192 Ancestor\n\n\n \n data SetLLD {i : Size} {u} : LinLogic i {u} \u2192 Set (lsuc u) where\n \u2193 : \u2200{ll} \u2192 Ancestor \u2192 SetLLD ll\n _\u2190\u2227 : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD (ls \u2227 rs)\n \u2227\u2192_ : \u2200{rs ls} \u2192 SetLLD rs \u2192 SetLLD (ls \u2227 rs)\n _\u2190\u2227\u2192_ : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD rs \u2192 SetLLD (ls \u2227 rs)\n _\u2190\u2228 : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD (ls \u2228 rs)\n \u2228\u2192_ : \u2200{rs ls} \u2192 SetLLD rs \u2192 SetLLD (ls \u2228 rs)\n _\u2190\u2228\u2192_ : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD rs \u2192 SetLLD (ls \u2228 rs)\n _\u2190\u2202 : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD (ls \u2202 rs)\n \u2202\u2192_ : \u2200{rs ls} \u2192 SetLLD rs \u2192 SetLLD (ls \u2202 rs)\n _\u2190\u2202\u2192_ : \u2200{rs ls} \u2192 SetLLD ls \u2192 SetLLD rs \u2192 SetLLD (ls \u2202 rs)\n \n \n \n data MSetLLD {i : Size} {u} : LinLogic i {u} \u2192 Set (lsuc u) where\n \u2205 : \u2200{ll} \u2192 MSetLLD ll\n \u00ac\u2205 : \u2200{ll} \u2192 SetLLD ll \u2192 MSetLLD ll\n \n -- TODO We shouldn't need this. When issue agda #2409 is resolved, remove this.\n drsize : \u2200{i u ll} \u2192 {j : Size< \u2191 i} \u2192 SetLLD {i} {u} ll \u2192 SetLLD {j} ll\n drsize (\u2193 mm) = (\u2193 mm)\n drsize (x \u2190\u2227) = (drsize x) \u2190\u2227\n drsize (\u2227\u2192 x) = \u2227\u2192 (drsize x)\n drsize (x \u2190\u2227\u2192 x\u2081) = (drsize x \u2190\u2227\u2192 drsize x\u2081)\n drsize (x \u2190\u2228) = (drsize x) \u2190\u2228\n drsize (\u2228\u2192 x) = \u2228\u2192 (drsize x)\n drsize (x \u2190\u2228\u2192 x\u2081) = (drsize x \u2190\u2228\u2192 drsize x\u2081)\n drsize (x \u2190\u2202) = (drsize x) \u2190\u2202\n drsize (\u2202\u2192 x) = \u2202\u2192 (drsize x)\n drsize (x \u2190\u2202\u2192 x\u2081) = (drsize x \u2190\u2202\u2192 drsize x\u2081)\n \n fillAllLowerD : \u2200{i u} \u2192 \u2200 ll \u2192 SetLLD {i} {u} ll\n fillAllLowerD ll = {!!}\n \n \n \n","old_contents":"module WellFormedLF where\n\nopen import Common\nopen import LinLogic\nimport SetLL\n\n--data IndexLF : \u2200{u} \u2192 {i : Size} \u2192 {j : Size< \u2191 i} \u2192 {rll : LinLogic j {u}} \u2192 {ll : LinLogic i {u}} \u2192 LFun {u} {i} {j} {rll} {ll} \u2192 Set where\n-- \u2193 : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 \u2200{u rll ll} \u2192 (lf : LFun {u} {i} {j} {rll} {ll}) \u2192 IndexLF lf\n-- _\u2190\u2282_ : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 {k : Size< \u2191 j} \u2192 \u2200{u rll pll ell ll ind elf prf lf}\n-- \u2192 IndexLF elf\n-- \u2192 IndexLF (_\u2282_ {u} {i} {j} {k} {pll} {ll} {ell} {rll} {ind} elf {{prf}} lf)\n-- _\u2282\u2192_ : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 {k : Size< \u2191 j} \u2192 \u2200{u rll pll ell ll ind elf prf lf}\n-- \u2192 IndexLF lf\n-- \u2192 IndexLF (_\u2282_ {u} {i} {j} {k} {pll} {ll} {ell} {rll} {ind} elf {{prf}} lf)\n-- tr : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 \u2200{u ll orll rll} \u2192 {{ltr : LLTr orll ll}} \u2192 {lf : LFun {u} {i} {j} {rll} {orll}}\n-- \u2192 IndexLF lf \u2192 IndexLF (tr {{ltr = ltr}} lf) \n--\n\n-- _\u2190\u2282_ : {i : Size} \u2192 {j : Size< \u2191 i} \u2192 {rll : LinLogic j {u}} \u2192 {ll : LinLogic i {u}} \u2192 (lf : LFun {u} {i} {j} {rll} {ll}) \u2192 IndexLF lf\n\n-- \u2193 probably the subtrees that contain all the inputs. \n-- We need to keep truck of all the latest subtrees that are outputs of coms. We can then check whether a transformation permutates them. If so , the tr is acceptable.\n-- transformations inside a subtree are acceptable.\n-- tranformations between two subtrees are only acceptable if the next com depends on both of them.\n-- If more than one subtree depends on a specific subtree, but the two do not depend on each other, we first separate the elements for the first subtree and then for the other.\n-- Since these coms can be independently executed, there can be tranformations of one subtree that can not be done while the others are ready. --> We remove the separation transformation and consider that\n-- the unexecuted transformations are done on those separated elements.\n\n-- > The input tree contains subtrees that represent exactly the input of the coms it can execute.\n\n-- 2 TWO\n-- We need three structures.\n-- THe first is used to identify inputs from the same com. When a tranformation splits these coms, we also split the set into two sets. Next we track all the inputs that are part of the tranformation, be it sets of inputs of a specific com or individual inputs. From all these sets, at least one item from each set needs to be the input of the next com.\n-- The set of coms that are used to allow for commutation of inputs.\n\n\n\n-- A non-empty set of nodes in a Linear Logic tree that also can also tag intermediary nodes.\ndata SetLLInter {i : Size} {u} : LinLogic i {u} \u2192 Set where\n \u2193 : \u2200{ll} \u2192 SetLLInter ll\n \u2193+ : \u2200{ll} \u2192 SetLLInter ll \u2192 SetLLInter ll\n _\u2190\u2227 : \u2200{rs ls} \u2192 SetLLInter ls \u2192 SetLLInter (ls \u2227 rs)\n \u2227\u2192_ : \u2200{rs ls} \u2192 SetLLInter rs \u2192 SetLLInter (ls \u2227 rs)\n _\u2190\u2227\u2192_ : \u2200{rs ls} \u2192 SetLLInter ls \u2192 SetLLInter rs \u2192 SetLLInter (ls \u2227 rs)\n _\u2190\u2228 : \u2200{rs ls} \u2192 SetLLInter ls \u2192 SetLLInter (ls \u2228 rs)\n \u2228\u2192_ : \u2200{rs ls} \u2192 SetLLInter rs \u2192 SetLLInter (ls \u2228 rs)\n _\u2190\u2228\u2192_ : \u2200{rs ls} \u2192 SetLLInter ls \u2192 SetLLInter rs \u2192 SetLLInter (ls \u2228 rs)\n _\u2190\u2202 : \u2200{rs ls} \u2192 SetLLInter ls \u2192 SetLLInter (ls \u2202 rs)\n \u2202\u2192_ : \u2200{rs ls} \u2192 SetLLInter rs \u2192 SetLLInter (ls \u2202 rs)\n _\u2190\u2202\u2192_ : \u2200{rs ls} \u2192 SetLLInter ls \u2192 SetLLInter rs \u2192 SetLLInter (ls \u2202 rs)\n\n\ndata MSetLLInter {i : Size} {u} : LinLogic i {u} \u2192 Set where\n \u2205 : \u2200{ll} \u2192 MSetLLInter ll\n \u00ac\u2205 : \u2200{ll} \u2192 SetLLInter ll \u2192 MSetLLInter ll\n\n\n\u2205-add : \u2200{i u ll rll} \u2192 {j : Size< \u2191 i} \u2192 (ind : IndexLL {i} {u} rll ll) \u2192 (nrll : LinLogic j )\n \u2192 SetLLInter (replLL ll ind nrll)\n\u2205-add \u2193 nrll = \u2193\n\u2205-add (ind \u2190\u2227) nrll = (\u2205-add ind nrll) \u2190\u2227\n\u2205-add (\u2227\u2192 ind) nrll = \u2227\u2192 (\u2205-add ind nrll)\n\u2205-add (ind \u2190\u2228) nrll = (\u2205-add ind nrll) \u2190\u2228\n\u2205-add (\u2228\u2192 ind) nrll = \u2228\u2192 (\u2205-add ind nrll)\n\u2205-add (ind \u2190\u2202) nrll = (\u2205-add ind nrll) \u2190\u2202\n\u2205-add (\u2202\u2192 ind) nrll = \u2202\u2192 (\u2205-add ind nrll)\n\n-- TODO We shouldn't need this. When issue agda #2409 is resolved, remove this.\ndsize : \u2200{i u ll} \u2192 {j : Size< \u2191 i} \u2192 SetLLInter {i} {u} ll \u2192 SetLLInter {j} ll\ndsize \u2193 = \u2193\ndsize (\u2193+ s) = (\u2193+ $ dsize s)\ndsize (x \u2190\u2227) = (dsize x) \u2190\u2227\ndsize (\u2227\u2192 x) = \u2227\u2192 (dsize x)\ndsize (x \u2190\u2227\u2192 x\u2081) = (dsize x \u2190\u2227\u2192 dsize x\u2081)\ndsize (x \u2190\u2228) = (dsize x) \u2190\u2228\ndsize (\u2228\u2192 x) = \u2228\u2192 (dsize x)\ndsize (x \u2190\u2228\u2192 x\u2081) = (dsize x \u2190\u2228\u2192 dsize x\u2081)\ndsize (x \u2190\u2202) = (dsize x) \u2190\u2202\ndsize (\u2202\u2192 x) = \u2202\u2192 (dsize x)\ndsize (x \u2190\u2202\u2192 x\u2081) = (dsize x \u2190\u2202\u2192 dsize x\u2081)\n\n-- Lower level intermediary are removed when adding a new index.\nadd : \u2200{i u ll q} \u2192 {j : Size< \u2191 i} \u2192 SetLLInter ll \u2192 (ind : IndexLL {i} {u} q ll) \u2192 (rll : LinLogic j)\n \u2192 SetLLInter (replLL ll ind rll)\nadd \u2193 \u2193 rll = \u2193\nadd \u2193 ind rll = \u2193+ (\u2205-add ind rll)\nadd (\u2193+ s) ind rll = \u2193+ (add s ind rll)\nadd (s \u2190\u2227) \u2193 rll = \u2193\nadd (s \u2190\u2227) (ind \u2190\u2227) rll = (add s ind rll) \u2190\u2227\nadd (s \u2190\u2227) (\u2227\u2192 ind) rll = dsize s \u2190\u2227\u2192 (\u2205-add ind rll)\nadd (\u2227\u2192 s) \u2193 rll = \u2193\nadd (\u2227\u2192 s) (ind \u2190\u2227) rll = (\u2205-add ind rll) \u2190\u2227\u2192 dsize s\nadd (\u2227\u2192 s) (\u2227\u2192 ind) rll = \u2227\u2192 add s ind rll\nadd (s \u2190\u2227\u2192 s\u2081) \u2193 rll = \u2193\nadd (s \u2190\u2227\u2192 s\u2081) (ind \u2190\u2227) rll = (add s ind rll) \u2190\u2227\u2192 dsize s\u2081\nadd (s \u2190\u2227\u2192 s\u2081) (\u2227\u2192 ind) rll = dsize s \u2190\u2227\u2192 (add s\u2081 ind rll)\nadd (s \u2190\u2228) \u2193 rll = \u2193\nadd (s \u2190\u2228) (ind \u2190\u2228) rll = (add s ind rll) \u2190\u2228\nadd (s \u2190\u2228) (\u2228\u2192 ind) rll = dsize s \u2190\u2228\u2192 (\u2205-add ind rll)\nadd (\u2228\u2192 s) \u2193 rll = \u2193\nadd (\u2228\u2192 s) (ind \u2190\u2228) rll = (\u2205-add ind rll) \u2190\u2228\u2192 dsize s\nadd (\u2228\u2192 s) (\u2228\u2192 ind) rll = \u2228\u2192 add s ind rll\nadd (s \u2190\u2228\u2192 s\u2081) \u2193 rll = \u2193\nadd (s \u2190\u2228\u2192 s\u2081) (ind \u2190\u2228) rll = (add s ind rll) \u2190\u2228\u2192 dsize s\u2081\nadd (s \u2190\u2228\u2192 s\u2081) (\u2228\u2192 ind) rll = dsize s \u2190\u2228\u2192 (add s\u2081 ind rll)\nadd (s \u2190\u2202) \u2193 rll = \u2193\nadd (s \u2190\u2202) (ind \u2190\u2202) rll = (add s ind rll) \u2190\u2202\nadd (s \u2190\u2202) (\u2202\u2192 ind) rll = dsize s \u2190\u2202\u2192 (\u2205-add ind rll)\nadd (\u2202\u2192 s) \u2193 rll = \u2193\nadd (\u2202\u2192 s) (ind \u2190\u2202) rll = (\u2205-add ind rll) \u2190\u2202\u2192 dsize s\nadd (\u2202\u2192 s) (\u2202\u2192 ind) rll = \u2202\u2192 add s ind rll\nadd (s \u2190\u2202\u2192 s\u2081) \u2193 rll = \u2193\nadd (s \u2190\u2202\u2192 s\u2081) (ind \u2190\u2202) rll = (add s ind rll) \u2190\u2202\u2192 dsize s\u2081\nadd (s \u2190\u2202\u2192 s\u2081) (\u2202\u2192 ind) rll = dsize s \u2190\u2202\u2192 (add s\u2081 ind rll)\n\n\nmadd : \u2200{i u ll q} \u2192 {j : Size< \u2191 i} \u2192 MSetLLInter ll \u2192 (ind : IndexLL {i} {u} q ll) \u2192 (rll : LinLogic j)\n \u2192 MSetLLInter (replLL ll ind rll)\nmadd \u2205 ind rll = \u00ac\u2205 (\u2205-add ind rll)\nmadd (\u00ac\u2205 x) ind rll = \u00ac\u2205 (add x ind rll)\n\ndel : \u2200{i u ll q} \u2192 {j : Size< \u2191 i} \u2192 SetLLInter ll \u2192 (ind : IndexLL {i} {u} q ll) \u2192 (rll : LinLogic j)\n \u2192 MSetLLInter (replLL ll ind rll)\ndel \u2193 \u2193 rll = \u2205\ndel (\u2193+ s) \u2193 rll = {!\u00ac\u2205 s!}\ndel (s \u2190\u2227) \u2193 rll = {!!}\ndel (\u2227\u2192 s) \u2193 rll = {!!}\ndel (s \u2190\u2227\u2192 s\u2081) \u2193 rll = {!!}\ndel (s \u2190\u2228) \u2193 rll = {!!}\ndel (\u2228\u2192 s) \u2193 rll = {!!}\ndel (s \u2190\u2228\u2192 s\u2081) \u2193 rll = {!!}\ndel (s \u2190\u2202) \u2193 rll = {!!}\ndel (\u2202\u2192 s) \u2193 rll = {!!}\ndel (s \u2190\u2202\u2192 s\u2081) \u2193 rll = {!!}\ndel \u2193 (ind \u2190\u2227) rll with (del \u2193 ind rll)\ndel \u2193 (ind \u2190\u2227) rll | \u2205 = \u00ac\u2205 (\u2227\u2192 \u2193)\ndel \u2193 (ind \u2190\u2227) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2227\u2192 \u2193)\ndel (s \u2190\u2227) (ind \u2190\u2227) rll with (del s ind rll)\ndel (s \u2190\u2227) (ind \u2190\u2227) rll | \u2205 = \u2205\ndel (s \u2190\u2227) (ind \u2190\u2227) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2227)\ndel (\u2227\u2192 s) (ind \u2190\u2227) rll = \u00ac\u2205 (\u2227\u2192 (dsize s))\ndel (s \u2190\u2227\u2192 s\u2081) (ind \u2190\u2227) rll with (del s ind rll)\ndel (s \u2190\u2227\u2192 s\u2081) (ind \u2190\u2227) rll | \u2205 = \u00ac\u2205 (\u2227\u2192 (dsize s\u2081))\ndel (s \u2190\u2227\u2192 s\u2081) (ind \u2190\u2227) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2227\u2192 (dsize s\u2081))\ndel \u2193 (\u2227\u2192 ind) rll with (del \u2193 ind rll)\ndel \u2193 (\u2227\u2192 ind) rll | \u2205 = \u00ac\u2205 (\u2193 \u2190\u2227)\ndel \u2193 (\u2227\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 (\u2193 \u2190\u2227\u2192 x)\ndel (s \u2190\u2227) (\u2227\u2192 ind) rll = \u00ac\u2205 ((dsize s) \u2190\u2227)\ndel (\u2227\u2192 s) (\u2227\u2192 ind) rll with (del s ind rll)\ndel (\u2227\u2192 s) (\u2227\u2192 ind) rll | \u2205 = \u2205\ndel (\u2227\u2192 s) (\u2227\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 (\u2227\u2192 x)\ndel (s \u2190\u2227\u2192 s\u2081) (\u2227\u2192 ind) rll with (del s\u2081 ind rll)\ndel (s \u2190\u2227\u2192 s\u2081) (\u2227\u2192 ind) rll | \u2205 = \u00ac\u2205 ((dsize s) \u2190\u2227)\ndel (s \u2190\u2227\u2192 s\u2081) (\u2227\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 ((dsize s) \u2190\u2227\u2192 x)\ndel \u2193 (ind \u2190\u2228) rll with (del \u2193 ind rll)\ndel \u2193 (ind \u2190\u2228) rll | \u2205 = \u00ac\u2205 (\u2228\u2192 \u2193)\ndel \u2193 (ind \u2190\u2228) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2228\u2192 \u2193)\ndel (s \u2190\u2228) (ind \u2190\u2228) rll with (del s ind rll)\ndel (s \u2190\u2228) (ind \u2190\u2228) rll | \u2205 = \u2205\ndel (s \u2190\u2228) (ind \u2190\u2228) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2228)\ndel (\u2228\u2192 s) (ind \u2190\u2228) rll = \u00ac\u2205 (\u2228\u2192 (dsize s))\ndel (s \u2190\u2228\u2192 s\u2081) (ind \u2190\u2228) rll with (del s ind rll)\ndel (s \u2190\u2228\u2192 s\u2081) (ind \u2190\u2228) rll | \u2205 = \u00ac\u2205 (\u2228\u2192 (dsize s\u2081))\ndel (s \u2190\u2228\u2192 s\u2081) (ind \u2190\u2228) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2228\u2192 (dsize s\u2081))\ndel \u2193 (\u2228\u2192 ind) rll with (del \u2193 ind rll)\ndel \u2193 (\u2228\u2192 ind) rll | \u2205 = \u00ac\u2205 (\u2193 \u2190\u2228)\ndel \u2193 (\u2228\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 (\u2193 \u2190\u2228\u2192 x)\ndel (s \u2190\u2228) (\u2228\u2192 ind) rll = \u00ac\u2205 ((dsize s) \u2190\u2228)\ndel (\u2228\u2192 s) (\u2228\u2192 ind) rll with (del s ind rll)\ndel (\u2228\u2192 s) (\u2228\u2192 ind) rll | \u2205 = \u2205\ndel (\u2228\u2192 s) (\u2228\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 (\u2228\u2192 x)\ndel (s \u2190\u2228\u2192 s\u2081) (\u2228\u2192 ind) rll with (del s\u2081 ind rll)\ndel (s \u2190\u2228\u2192 s\u2081) (\u2228\u2192 ind) rll | \u2205 = \u00ac\u2205 ((dsize s) \u2190\u2228)\ndel (s \u2190\u2228\u2192 s\u2081) (\u2228\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 ((dsize s) \u2190\u2228\u2192 x)\ndel \u2193 (ind \u2190\u2202) rll with (del \u2193 ind rll)\ndel \u2193 (ind \u2190\u2202) rll | \u2205 = \u00ac\u2205 (\u2202\u2192 \u2193)\ndel \u2193 (ind \u2190\u2202) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2202\u2192 \u2193)\ndel (s \u2190\u2202) (ind \u2190\u2202) rll with (del s ind rll)\ndel (s \u2190\u2202) (ind \u2190\u2202) rll | \u2205 = \u2205\ndel (s \u2190\u2202) (ind \u2190\u2202) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2202)\ndel (\u2202\u2192 s) (ind \u2190\u2202) rll = \u00ac\u2205 (\u2202\u2192 (dsize s))\ndel (s \u2190\u2202\u2192 s\u2081) (ind \u2190\u2202) rll with (del s ind rll)\ndel (s \u2190\u2202\u2192 s\u2081) (ind \u2190\u2202) rll | \u2205 = \u00ac\u2205 (\u2202\u2192 (dsize s\u2081))\ndel (s \u2190\u2202\u2192 s\u2081) (ind \u2190\u2202) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2202\u2192 (dsize s\u2081))\ndel \u2193 (\u2202\u2192 ind) rll with (del \u2193 ind rll)\ndel \u2193 (\u2202\u2192 ind) rll | \u2205 = \u00ac\u2205 (\u2193 \u2190\u2202)\ndel \u2193 (\u2202\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 (\u2193 \u2190\u2202\u2192 x)\ndel (s \u2190\u2202) (\u2202\u2192 ind) rll = \u00ac\u2205 ((dsize s) \u2190\u2202)\ndel (\u2202\u2192 s) (\u2202\u2192 ind) rll with (del s ind rll)\ndel (\u2202\u2192 s) (\u2202\u2192 ind) rll | \u2205 = \u2205\ndel (\u2202\u2192 s) (\u2202\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 (\u2202\u2192 x)\ndel (s \u2190\u2202\u2192 s\u2081) (\u2202\u2192 ind) rll with (del s\u2081 ind rll)\ndel (s \u2190\u2202\u2192 s\u2081) (\u2202\u2192 ind) rll | \u2205 = \u00ac\u2205 ((dsize s) \u2190\u2202)\ndel (s \u2190\u2202\u2192 s\u2081) (\u2202\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 ((dsize s) \u2190\u2202\u2192 x)\n\n\n","returncode":0,"stderr":"","license":"mpl-2.0","lang":"Agda"} {"commit":"ec77d4b02f6afb3d807d66f7e51f3b56de0a84e0","subject":"GroupIsomorphism: is now indexed over the function","message":"GroupIsomorphism: is now indexed over the function\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Algebra\/Group\/Isomorphism.agda","new_file":"lib\/Algebra\/Group\/Isomorphism.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Type using (Type_)\nopen import Level.NP\nopen import Function.NP\nopen import Data.Product using (_,_)\nopen import Relation.Binary.PropositionalEquality.NP\nopen \u2261-Reasoning\n\nmodule Algebra.Group.Isomorphism where\n\nopen import Algebra.Group\nopen import Algebra.Group.Homomorphism\n hiding (module Stability; module Stability-Minimal)\n\nrecord GroupIsomorphism\n {a}{A : Type a}\n {b}{B : Type b}\n (G+ : Group A)\n (G* : Group B)\n (\u03c6 : A \u2192 B) : Set(a \u2294 b) where\n open Additive-Group G+\n open Multiplicative-Group G*\n\n field\n \u03c6-+-* : \u2200 {x y} \u2192 \u03c6 (x + y) \u2261 \u03c6 x * \u03c6 y\n \u03c6\u207b\u00b9 : B \u2192 A\n \u03c6\u207b\u00b9-\u03c6 : \u2200 {x} \u2192 \u03c6\u207b\u00b9 (\u03c6 x) \u2261 x\n \u03c6-\u03c6\u207b\u00b9 : \u2200 {x} \u2192 \u03c6 (\u03c6\u207b\u00b9 x) \u2261 x\n\n \u03c6-hom : GroupHomomorphism G+ G* \u03c6\n \u03c6-hom = mk \u03c6-+-*\n\n open GroupHomomorphism \u03c6-hom public\n\n{- How this proof can be used for crypto, in particular ElGamal to DDH\n\n the Group A is \u2124q with modular addition as operation\n the Group B is the cyclic group with order q\n\n \u03c6 is g^, the proof only need that it is a group homomorphism\n and that it has a right inverse\n\n F is the summation operator and is required to be stable\n under addition of A.\n\n F should respects extensionality\n\n This proof adds \u03c6\u207b\u00b9 k, because adding a constant is stable under\n the big op F, this addition can then be pulled homomorphically\n through f, to become a, multiplication by k.\n-}\nmodule Stability-Minimal\n {a}{A : Type a}\n {b}{B : Type b}\n (\u03c6 : A \u2192 B)\n (\u03c6\u207b\u00b9 : B \u2192 A)\n (\u03c6-\u03c6\u207b\u00b9 : \u2200 {x} \u2192 \u03c6 (\u03c6\u207b\u00b9 x) \u2261 x)\n (_+_ : Op\u2082 A)\n (_*_ : Op\u2082 B)\n (\u03c6-+-* : \u2200 {x y} \u2192 \u03c6 (x + y) \u2261 \u03c6 x * \u03c6 y)\n\n {c}{C : Type c}\n (F : (A \u2192 B) \u2192 C)\n (F= : \u2200 {f g : A \u2192 B} \u2192 f \u2257 g \u2192 F f \u2261 F g)\n where\n\n module _ (F\u03c6+ : \u2200 {k} \u2192 F \u03c6 \u2261 F (\u03c6 \u2218 _+_ k)) where\n\n *-stable : \u2200 {k} \u2192 F \u03c6 \u2261 F (_*_ k \u2218 \u03c6)\n *-stable {k} =\n F \u03c6 \u2261\u27e8 F\u03c6+ \u27e9\n F (\u03c6 \u2218 _+_ (\u03c6\u207b\u00b9 k)) \u2261\u27e8 F= (\u03bb x \u2192 \u03c6 (\u03c6\u207b\u00b9 k + x) \u2261\u27e8 \u03c6-+-* \u27e9\n \u03c6 (\u03c6\u207b\u00b9 k) * \u03c6 x \u2261\u27e8 ap\u2082 _*_ \u03c6-\u03c6\u207b\u00b9 idp \u27e9\n k * \u03c6 x \u220e) \u27e9\n F (_*_ k \u2218 \u03c6) \u220e\n\n {- The reverse direction comes from the homomorphism -}\n open Algebra.Group.Homomorphism.Stability-Minimal\n \u03c6 _+_ _*_ \u03c6-+-* F F=\n\n stability : (\u2200 {k} \u2192 F \u03c6 \u2261 F (\u03c6 \u2218 _+_ k)) \u2194 (\u2200 {k} \u2192 F \u03c6 \u2261 F (_*_ k \u2218 \u03c6))\n stability = *-stable , +-stable\n\nmodule Stability\n {a}{A : Type a}\n {b}{B : Type b}\n (G+ : Group A)\n (G* : Group B)\n (\u03c6 : A \u2192 B)\n (\u03c6-iso : GroupIsomorphism G+ G* \u03c6)\n where\n open Additive-Group G+\n open Multiplicative-Group G*\n open GroupIsomorphism \u03c6-iso\n\n open Stability-Minimal \u03c6 \u03c6\u207b\u00b9 \u03c6-\u03c6\u207b\u00b9 _+_ _*_ \u03c6-+-* public\n\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Type using (Type_)\nopen import Level.NP\nopen import Function.NP\nopen import Data.Product using (_,_)\nopen import Relation.Binary.PropositionalEquality.NP\nopen \u2261-Reasoning\n\nmodule Algebra.Group.Isomorphism where\n\nopen import Algebra.Group\nopen import Algebra.Group.Homomorphism\n hiding (module Stability; module Stability-Minimal)\n\nrecord GroupIsomorphism\n {a}{A : Type a}\n {b}{B : Type b}\n (G+ : Group A)\n (G* : Group B) : Set(a \u2294 b) where\n open Additive-Group G+\n open Multiplicative-Group G*\n\n field\n \u03c6 : A \u2192 B -- TODO\n \u03c6-+-* : \u2200 {x y} \u2192 \u03c6 (x + y) \u2261 \u03c6 x * \u03c6 y\n \u03c6\u207b\u00b9 : B \u2192 A\n \u03c6\u207b\u00b9-\u03c6 : \u2200 {x} \u2192 \u03c6\u207b\u00b9 (\u03c6 x) \u2261 x\n \u03c6-\u03c6\u207b\u00b9 : \u2200 {x} \u2192 \u03c6 (\u03c6\u207b\u00b9 x) \u2261 x\n\n \u03c6-hom : GroupHomomorphism G+ G* \u03c6\n \u03c6-hom = mk \u03c6-+-*\n\n open GroupHomomorphism \u03c6-hom public\n\n{- How this proof can be used for crypto, in particular ElGamal to DDH\n\n the Group A is \u2124q with modular addition as operation\n the Group B is the cyclic group with order q\n\n \u03c6 is g^, the proof only need that it is a group homomorphism\n and that it has a right inverse\n\n F is the summation operator and is required to be stable\n under addition of A.\n\n F should respects extensionality\n\n This proof adds \u03c6\u207b\u00b9 k, because adding a constant is stable under\n the big op F, this addition can then be pulled homomorphically\n through f, to become a, multiplication by k.\n-}\nmodule Stability-Minimal\n {a}{A : Type a}\n {b}{B : Type b}\n (\u03c6 : A \u2192 B)\n (\u03c6\u207b\u00b9 : B \u2192 A)\n (\u03c6-\u03c6\u207b\u00b9 : \u2200 {x} \u2192 \u03c6 (\u03c6\u207b\u00b9 x) \u2261 x)\n (_+_ : Op\u2082 A)\n (_*_ : Op\u2082 B)\n (\u03c6-+-* : \u2200 {x y} \u2192 \u03c6 (x + y) \u2261 \u03c6 x * \u03c6 y)\n {c}{C : Type c}\n (F : (A \u2192 B) \u2192 C)\n (F= : \u2200 {f g : A \u2192 B} \u2192 f \u2257 g \u2192 F f \u2261 F g)\n where\n\n module _ (F\u03c6+ : \u2200 {k} \u2192 F \u03c6 \u2261 F (\u03c6 \u2218 _+_ k)) where\n\n *-stable : \u2200 {k} \u2192 F \u03c6 \u2261 F (_*_ k \u2218 \u03c6)\n *-stable {k} =\n F \u03c6 \u2261\u27e8 F\u03c6+ \u27e9\n F (\u03c6 \u2218 _+_ (\u03c6\u207b\u00b9 k)) \u2261\u27e8 F= (\u03bb x \u2192 \u03c6 (\u03c6\u207b\u00b9 k + x) \u2261\u27e8 \u03c6-+-* \u27e9\n \u03c6 (\u03c6\u207b\u00b9 k) * \u03c6 x \u2261\u27e8 ap\u2082 _*_ \u03c6-\u03c6\u207b\u00b9 idp \u27e9\n k * \u03c6 x \u220e) \u27e9\n F (_*_ k \u2218 \u03c6) \u220e\n\n {- The reverse direction comes from the homomorphism -}\n open Algebra.Group.Homomorphism.Stability-Minimal\n \u03c6 _+_ _*_ \u03c6-+-* F F=\n\n stability : (\u2200 {k} \u2192 F \u03c6 \u2261 F (\u03c6 \u2218 _+_ k)) \u2194 (\u2200 {k} \u2192 F \u03c6 \u2261 F (_*_ k \u2218 \u03c6))\n stability = *-stable , +-stable\n\nmodule Stability\n {a}{A : Type a}\n {b}{B : Type b}\n (G+ : Group A)\n (G* : Group B)\n (\u03c6-iso : GroupIsomorphism G+ G*)\n where\n open Additive-Group G+\n open Multiplicative-Group G*\n open GroupIsomorphism \u03c6-iso\n\n open Stability-Minimal \u03c6 \u03c6\u207b\u00b9 \u03c6-\u03c6\u207b\u00b9 _+_ _*_ \u03c6-+-* public\n\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"272abae5605c8fd8c5385376ffe4f4d4c26d2a9d","subject":"Add and use a congruence lemma","message":"Add and use a congruence lemma\n\nTo reduce explicit wrapping\/unwrapping of proofs of \u2259.\n\nOld-commit-hash: 2bc64b5b9f3b074a70918e11f66512232fb6796b\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Equivalence.agda","new_file":"Base\/Change\/Equivalence.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\nopen import Function\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n\n -- To avoid unification problems, use a one-field record.\n record _\u2259_ dx dy : Set a where\n -- doe = Delta-Observational Equivalence.\n constructor doe\n field\n proof : x \u229e dx \u2261 x \u229e dy\n\n open _\u2259_ public\n\n -- Same priority as \u2261\n infix 4 _\u2259_\n\n open import Relation.Binary\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = doe refl\n\n \u2259-sym : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-sym \u2259 = doe $ sym $ proof \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = doe $ trans (proof \u2259\u2081) (proof \u2259\u2082)\n\n -- That's standard congruence applied to \u2259\n \u2259-cong : \u2200 {b} {B : Set b}\n (f : A \u2192 B) {dx dy} \u2192 dx \u2259 dy \u2192 f (x \u229e dx) \u2261 f (x \u229e dy)\n \u2259-cong f da\u2259db = cong f $ proof da\u2259db\n\n \u2259-isEquivalence : IsEquivalence (_\u2259_)\n \u2259-isEquivalence = record\n { refl = \u2259-refl\n ; sym = \u2259-sym\n ; trans = \u2259-trans\n }\n\n \u2259-setoid : Setoid \u2113 a\n \u2259-setoid = record\n { Carrier = \u0394 x\n ; _\u2248_ = _\u2259_\n ; isEquivalence = \u2259-isEquivalence\n }\n\n\n ------------------------------------------------------------------------\n -- Convenient syntax for equational reasoning\n\n import Relation.Binary.EqReasoning as EqR\n\n module \u2259-Reasoning where\n open EqR \u2259-setoid public\n renaming (_\u2248\u27e8_\u27e9_ to _\u2259\u27e8_\u27e9_)\n\n -- By update-nil, if dx = nil x, then x \u229e dx \u2261 x.\n -- As a consequence, if dx \u2259 nil x, then x \u229e dx \u2261 x\n nil-is-\u229e-unit : \u2200 dx \u2192 dx \u2259 nil x \u2192 x \u229e dx \u2261 x\n nil-is-\u229e-unit dx dx\u2259nil-x =\n begin\n x \u229e dx\n \u2261\u27e8 proof dx\u2259nil-x \u27e9\n x \u229e (nil x)\n \u2261\u27e8 update-nil x \u27e9\n x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Here we prove the inverse:\n \u229e-unit-is-nil : \u2200 dx \u2192 x \u229e dx \u2261 x \u2192 dx \u2259 nil x\n \u229e-unit-is-nil dx x\u229edx\u2261x = doe $\n begin\n x \u229e dx\n \u2261\u27e8 x\u229edx\u2261x \u27e9\n x\n \u2261\u27e8 sym (update-nil x) \u27e9\n x \u229e nil x\n \u220e\n where\n open \u2261-Reasoning\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n -- This results pairs with update-diff.\n diff-update : \u2200 {dx} \u2192 (x \u229e dx) \u229f x \u2259 dx\n diff-update {dx} = doe lemma\n where\n lemma : x \u229e (x \u229e dx \u229f x) \u2261 x \u229e dx\n lemma = update-diff (x \u229e dx) x\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = doe lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 \u2259-cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 \u2259-cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n open import Postulate.Extensionality\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} df-x-dx\u2259dg-x-dx = doe lemma\u2082\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 x dx = proof $ df-x-dx\u2259dg-x-dx x dx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n\n -- We know that Derivative f (apply (nil f)) (by nil-is-derivative).\n -- That is, df = nil f -> Derivative f (apply df).\n -- Now, we try to prove that if Derivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 Derivative f (apply df) \u2192 Derivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 \u2259-sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n -- Unused, but just to test that inference works.\n lemma : nil f \u2259 dg\n lemma = \u2259-sym (derivative-is-nil dg fdg)\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\nopen import Function\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n\n -- To avoid unification problems, use a one-field record.\n record _\u2259_ dx dy : Set a where\n -- doe = Delta-Observational Equivalence.\n constructor doe\n field\n proof : x \u229e dx \u2261 x \u229e dy\n\n open _\u2259_ public\n\n -- Same priority as \u2261\n infix 4 _\u2259_\n\n open import Relation.Binary\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = doe refl\n\n \u2259-sym : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-sym \u2259 = doe $ sym $ proof \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = doe $ trans (proof \u2259\u2081) (proof \u2259\u2082)\n\n \u2259-isEquivalence : IsEquivalence (_\u2259_)\n \u2259-isEquivalence = record\n { refl = \u2259-refl\n ; sym = \u2259-sym\n ; trans = \u2259-trans\n }\n\n \u2259-setoid : Setoid \u2113 a\n \u2259-setoid = record\n { Carrier = \u0394 x\n ; _\u2248_ = _\u2259_\n ; isEquivalence = \u2259-isEquivalence\n }\n\n\n ------------------------------------------------------------------------\n -- Convenient syntax for equational reasoning\n\n import Relation.Binary.EqReasoning as EqR\n\n module \u2259-Reasoning where\n open EqR \u2259-setoid public\n renaming (_\u2248\u27e8_\u27e9_ to _\u2259\u27e8_\u27e9_)\n\n -- By update-nil, if dx = nil x, then x \u229e dx \u2261 x.\n -- As a consequence, if dx \u2259 nil x, then x \u229e dx \u2261 x\n nil-is-\u229e-unit : \u2200 dx \u2192 dx \u2259 nil x \u2192 x \u229e dx \u2261 x\n nil-is-\u229e-unit dx dx\u2259nil-x =\n begin\n x \u229e dx\n \u2261\u27e8 proof dx\u2259nil-x \u27e9\n x \u229e (nil x)\n \u2261\u27e8 update-nil x \u27e9\n x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Here we prove the inverse:\n \u229e-unit-is-nil : \u2200 dx \u2192 x \u229e dx \u2261 x \u2192 dx \u2259 nil x\n \u229e-unit-is-nil dx x\u229edx\u2261x = doe $\n begin\n x \u229e dx\n \u2261\u27e8 x\u229edx\u2261x \u27e9\n x\n \u2261\u27e8 sym (update-nil x) \u27e9\n x \u229e nil x\n \u220e\n where\n open \u2261-Reasoning\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n -- This results pairs with update-diff.\n diff-update : \u2200 {dx} \u2192 (x \u229e dx) \u229f x \u2259 dx\n diff-update {dx} = doe lemma\n where\n lemma : x \u229e (x \u229e dx \u229f x) \u2261 x \u229e dx\n lemma = update-diff (x \u229e dx) x\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = doe lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 cong (f \u229e df\u2081) $ proof dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 cong (\u03bb f \u2192 f (x \u229e dx\u2082)) $ proof df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n open import Postulate.Extensionality\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} df-x-dx\u2259dg-x-dx = doe lemma\u2082\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 x dx = proof $ df-x-dx\u2259dg-x-dx x dx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n\n -- We know that Derivative f (apply (nil f)) (by nil-is-derivative).\n -- That is, df = nil f -> Derivative f (apply df).\n -- Now, we try to prove that if Derivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 Derivative f (apply df) \u2192 Derivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 \u2259-sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n -- Unused, but just to test that inference works.\n lemma : nil f \u2259 dg\n lemma = \u2259-sym (derivative-is-nil dg fdg)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"aa599ff81b5977aba6526f15272f7c5cd110b5ca","subject":"Minor changes to a note.","message":"Minor changes to a note.\n\nIgnore-this: e9399eaae7068336c59aed864a0878a0\n\ndarcs-hash:20120615190941-3bd4e-d47bd9ffb7bbc540837cedc9e4f06e1fba047622.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/Predicates.agda","new_file":"notes\/fixed-points\/Predicates.agda","new_contents":"------------------------------------------------------------------------------\n-- The FOTC types without use data, i.e. using Agda as a logical framework\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Predicates where\n\nopen import FOTC.Base\n\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- The existential proyections.\n\u2203-proj\u2081 : \u2200 {A} \u2192 \u2203 A \u2192 D\n\u2203-proj\u2081 (x , _) = x\n\n\u2203-proj\u2082 : \u2200 {A} \u2192 (h : \u2203 A) \u2192 A (\u2203-proj\u2081 h)\n\u2203-proj\u2082 (_ , Ax) = Ax\n\n------------------------------------------------------------------------------\n-- The inductive FOTC types using postulates.\n\nmodule Pure where\n\n -- The FOTC natural numbers type.\n postulate\n N : D \u2192 Set\n zN : N zero\n sN : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n\n -- Example.\n one : D\n one = succ\u2081 zero\n\n oneN : N one\n oneN = sN zN\n\n -- The FOTC lists type.\n postulate\n List : D \u2192 Set\n nilL : List []\n consL : \u2200 x {xs} \u2192 List xs \u2192 List (x \u2237 xs)\n\n -- Example.\n l : List (zero \u2237 true \u2237 [])\n l = consL zero (consL true nilL)\n\n -- The FOTC list of natural numbers type.\n postulate\n ListN : D \u2192 Set\n nilLN : ListN []\n consLN : \u2200 {n ns} \u2192 N n \u2192 ListN ns \u2192 ListN (n \u2237 ns)\n\n -- Example.\n ln : ListN (zero \u2237 one \u2237 [])\n ln = consLN zN (consLN oneN nilLN)\n\n------------------------------------------------------------------------------\n-- The inductive FOTC types using data.\n\nmodule Inductive where\n\n -- The FOTC natural numbers type.\n data N : D \u2192 Set where\n zN : N zero\n sN : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n\n -- Example.\n one : D\n one = succ\u2081 zero\n\n oneN : N one\n oneN = sN zN\n\n -- The FOTC lists type.\n data List : D \u2192 Set where\n nilL : List []\n consL : \u2200 x {xs} \u2192 List xs \u2192 List (x \u2237 xs)\n\n -- Example.\n l : List (zero \u2237 true \u2237 [])\n l = consL zero (consL true nilL)\n\n -- The FOTC list of natural numbers type.\n data ListN : D \u2192 Set where\n nilLN : ListN []\n consLN : \u2200 {n ns} \u2192 N n \u2192 ListN ns \u2192 ListN (n \u2237 ns)\n\n -- Example.\n ln : ListN (zero \u2237 one \u2237 [])\n ln = consLN zN (consLN oneN nilLN)\n\n------------------------------------------------------------------------------\n-- The least fixed-point operator.\n\nmodule LFP where\n\n postulate\n -- Least fixed-points correspond to inductively defined types.\n --\n -- N.B. We cannot write LFP in first-order logic. We should use an\n -- instance instead.\n LFP : ((D \u2192 Set) \u2192 D \u2192 Set) \u2192 D \u2192 Set\n\n -- In FOTC, we cannot use the equality on predicates, i.e. we\n -- cannot write\n --\n -- (f : (D \u2192 Set) \u2192 D \u2192 Set) \u2192 f (LFP f) \u2261 LFP f (1)\n --\n -- because the type of the equality is\n --\n -- _\u2261_ : D \u2192 D \u2192 Set,\n --\n -- therefore we postulate both directions of the conversion rule (1).\n\n LFP\u2081 : (f : (D \u2192 Set) \u2192 D \u2192 Set)(d : D) \u2192 LFP f d \u2192 f (LFP f) d\n LFP\u2082 : (f : (D \u2192 Set) \u2192 D \u2192 Set)(d : D) \u2192 f (LFP f) d \u2192 LFP f d\n\n------------------------------------------------------------------------------\n-- The greatest fixed-point operator.\n\n-- N.B. At the moment, the definitions of LFP and GFP are the same.\n\nmodule GFP where\n\n postulate\n -- Greatest fixed-points correspond to coinductively defined types.\n\n GFP : ((D \u2192 Set) \u2192 D \u2192 Set) \u2192 D \u2192 Set\n\n GFP\u2081 : (f : (D \u2192 Set) \u2192 D \u2192 Set)(d : D) \u2192 GFP f d \u2192 f (GFP f) d\n GFP\u2082 : (f : (D \u2192 Set) \u2192 D \u2192 Set)(d : D) \u2192 f (GFP f) d \u2192 GFP f d\n\n------------------------------------------------------------------------------\n-- The FOTC natural numbers type as the least fixed-point of a\n-- functor.\n\nmodule NLFP where\n\n open LFP\n\n -- Functor for the FOTC natural numbers type.\n\n -- From Peter: NatF if D was an inductive type\n -- NatF : (D \u2192 Set) \u2192 D \u2192 Set\n -- NatF X zero = \u22a4\n -- NatF X (succ\u2081 n) = X n\n\n -- From Peter: NatF in pure predicate logic.\n NatF : (D \u2192 Set) \u2192 D \u2192 Set\n NatF X n = n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ\u2081 m \u2227 X m)\n\n -- The FOTC natural numbers type using LFP.\n N : D \u2192 Set\n N = LFP NatF\n\n -- The data constructors of N.\n zN : N zero\n zN = LFP\u2082 NatF zero (inj\u2081 refl)\n\n sN : {n : D} \u2192 N n \u2192 N (succ\u2081 n)\n sN {n} Nn = LFP\u2082 NatF (succ\u2081 n) (inj\u2082 (n , (refl , Nn)))\n\n -- Example.\n one : D\n one = succ\u2081 zero\n\n oneN : N one\n oneN = sN zN\n\n------------------------------------------------------------------------------\n-- The FOTC list type as the least fixed-point of a functor.\n\nmodule ListLFT where\n\n open LFP\n\n -- Functor for the FOTC lists type.\n ListF : (D \u2192 Set) \u2192 D \u2192 Set\n ListF X ds = ds \u2261 [] \u2228 (\u2203 \u03bb e \u2192 \u2203 \u03bb es \u2192 ds \u2261 e \u2237 es \u2227 X es)\n\n -- The FOTC list type using LFP.\n List : D \u2192 Set\n List = LFP ListF\n\n -- The data constructors of List.\n nilL : List []\n nilL = LFP\u2082 ListF [] (inj\u2081 refl)\n\n consL : \u2200 x {xs} \u2192 List xs \u2192 List (x \u2237 xs)\n consL x {xs} Lxs = LFP\u2082 ListF (x \u2237 xs) (inj\u2082 (x , xs , refl , Lxs))\n\n -- Example.\n l : List (zero \u2237 true \u2237 [])\n l = consL zero (consL true nilL)\n\n------------------------------------------------------------------------------\n-- The FOTC list of natural numbers type as the least fixed-point of a\n-- functor.\n\nmodule ListNLFT where\n\n open LFP\n open NLFP\n\n -- Functor for the FOTC list of natural numbers type.\n ListNF : (D \u2192 Set) \u2192 D \u2192 Set\n ListNF X ds = ds \u2261 [] \u2228 (\u2203 \u03bb e \u2192 \u2203 \u03bb es \u2192 ds \u2261 e \u2237 es \u2227 N e \u2227 X es)\n\n -- The FOTC list type using LFP.\n ListN : D \u2192 Set\n ListN = LFP ListNF\n\n -- The data constructors of ListN.\n nilLN : ListN []\n nilLN = LFP\u2082 ListNF [] (inj\u2081 refl)\n\n consLN : \u2200 {n ns} \u2192 N n \u2192 ListN ns \u2192 ListN (n \u2237 ns)\n consLN {n} {ns} Nn LNns =\n LFP\u2082 ListNF (n \u2237 ns) (inj\u2082 (n , ns , refl , Nn , LNns))\n\n -- Example.\n ln : ListN (zero \u2237 one \u2237 [])\n ln = consLN zN (consLN oneN nilLN)\n\n------------------------------------------------------------------------------\n-- The FOTC Colist type as the greatest fixed-point of a functor.\n\nmodule CoList where\n\n open GFP\n\n -- Functor for the FOTC Colists type (the same functor that for the\n -- List type).\n ColistF : (D \u2192 Set) \u2192 D \u2192 Set\n ColistF X ds = ds \u2261 [] \u2228 (\u2203 \u03bb e \u2192 \u2203 \u03bb es \u2192 ds \u2261 e \u2237 es \u2227 X es)\n\n -- The FOTC Colist type using GFP.\n Colist : D \u2192 Set\n Colist = GFP ColistF\n\n -- The data constructors of Colist.\n nilCL : Colist []\n nilCL = GFP\u2082 ColistF [] (inj\u2081 refl)\n\n consCL : \u2200 x xs \u2192 Colist xs \u2192 Colist (x \u2237 xs)\n consCL x xs CLxs = GFP\u2082 ColistF (x \u2237 xs) (inj\u2082 (x , xs , refl , CLxs))\n\n -- Example (finite colist).\n l : Colist (zero \u2237 true \u2237 [])\n l = consCL zero (true \u2237 []) (consCL true [] nilCL)\n\n -- TODO: Example (infinite colist).\n -- zerosCL : Colist {!!}\n -- zerosCL = consCL zero {!!} zerosCL\n\n------------------------------------------------------------------------------\n-- The FOTC Stream type as the greatest fixed-point of a functor.\n\nmodule Stream\u2081 where\n\n open GFP\n\n -- Functor for the FOTC Stream type.\n StreamF : (D \u2192 Set) \u2192 D \u2192 Set\n StreamF X ds = \u2203 \u03bb e \u2192 X ds\n\n -- The FOTC Stream type using GFP.\n Stream : D \u2192 Set\n Stream = GFP StreamF\n\n -- The data constructor of Stream.\n -- Using StreamF we cannot define this data constructor\n -- consS : \u2200 x xs \u2192 Stream xs \u2192 Stream (x \u2237 xs)\n -- consS x xs Sxs = GFP\u2082 StreamF (x \u2237 xs) {!!}\n\n------------------------------------------------------------------------------\n-- The FOTC Stream type as the greatest fixed-point of a functor.\n\nmodule Stream\u2082 where\n\n open GFP\n\n -- Functor for the FOTC Stream type.\n StreamF : (D \u2192 Set) \u2192 D \u2192 Set\n StreamF X ds = \u2203 \u03bb e \u2192 \u2203 \u03bb es \u2192 X es\n\n -- The FOTC Stream type using GFP.\n Stream : D \u2192 Set\n Stream = GFP StreamF\n\n -- The data constructor of Stream.\n -- TODO: To use implicit arguments.\n consS : \u2200 x xs \u2192 Stream xs \u2192 Stream (x \u2237 xs)\n consS x xs Sxs = GFP\u2082 StreamF (x \u2237 xs) (x , xs , Sxs)\n\n -- TODO: Example\n -- zerosS : Stream {!!}\n -- zerosS = consS zero {!!} zerosS\n\n headS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 D\n headS {x} _ = x\n\n tailS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\n tailS {x} {xs} S = GFP\u2082 StreamF xs (x , x \u2237 xs , S)\n\n -- The functor StreamF does not link together the parts of the\n -- stream, so I can get a stream from any stream.\n bad : \u2200 {xs ys} \u2192 Stream xs \u2192 Stream ys\n bad {xs} {ys} S = GFP\u2082 StreamF ys (ys , xs , S)\n\n------------------------------------------------------------------------------\n-- The FOTC Stream type as the greatest fixed-point of a functor.\n\nmodule Stream\u2083 where\n\n open GFP\n\n -- Functor for the FOTC Stream type.\n StreamF : (D \u2192 Set) \u2192 D \u2192 Set\n StreamF X ds = \u2203 \u03bb e \u2192 \u2203 \u03bb es \u2192 ds \u2261 e \u2237 es \u2227 X es\n\n -- The FOTC Stream type using GFP.\n Stream : D \u2192 Set\n Stream = GFP StreamF\n\n -- The data constructor of Stream.\n -- TODO: To use implicit arguments.\n consS : \u2200 x xs \u2192 Stream xs \u2192 Stream (x \u2237 xs)\n consS x xs Sxs = GFP\u2082 StreamF (x \u2237 xs) (x , xs , refl , Sxs)\n\n -- TODO: Example\n -- zerosS : Stream {!!}\n -- zerosS = consS zero {!!} zerosS\n\n headS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 D\n headS {x} _ = x\n\n tailS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\n tailS {x} {xs} Sx\u2237xs = Sxs\n where\n unfoldS : StreamF (GFP StreamF) (x \u2237 xs)\n unfoldS = GFP\u2081 StreamF (x \u2237 xs) Sx\u2237xs\n\n e : D\n e = \u2203-proj\u2081 unfoldS\n\n Pe : \u2203 \u03bb es \u2192 x \u2237 xs \u2261 e \u2237 es \u2227 GFP StreamF es\n Pe = \u2203-proj\u2082 unfoldS\n\n es : D\n es = \u2203-proj\u2081 Pe\n\n Pes : x \u2237 xs \u2261 e \u2237 es \u2227 GFP StreamF es\n Pes = \u2203-proj\u2082 Pe\n\n xs\u2261es : xs \u2261 es\n xs\u2261es = \u2227-proj\u2082 (\u2237-injective (\u2227-proj\u2081 Pes))\n\n Sxs : GFP StreamF xs\n Sxs = subst (GFP StreamF) (sym xs\u2261es) (\u2227-proj\u2082 Pes)\n","old_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Predicates where\n\nopen import FOTC.Base\n\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- The existential proyections.\n\u2203-proj\u2081 : \u2200 {A} \u2192 \u2203 A \u2192 D\n\u2203-proj\u2081 (x , _) = x\n\n\u2203-proj\u2082 : \u2200 {A} \u2192 (h : \u2203 A) \u2192 A (\u2203-proj\u2081 h)\n\u2203-proj\u2082 (_ , Ax) = Ax\n\n-- The FOTC types without use data, i.e. using Agda as a logical framework.\n\nmodule Pure where\n\n -- The FOTC natural numbers type.\n postulate\n N : D \u2192 Set\n zN : N zero\n sN : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n\n -- Example.\n one : D\n one = succ\u2081 zero\n\n oneN : N one\n oneN = sN zN\n\n -- The FOTC lists type.\n postulate\n List : D \u2192 Set\n nilL : List []\n consL : \u2200 x {xs} \u2192 List xs \u2192 List (x \u2237 xs)\n\n -- Example\n l : List (zero \u2237 true \u2237 [])\n l = consL zero (consL true nilL)\n\n -- The FOTC list of natural numbers type.\n postulate\n ListN : D \u2192 Set\n nilLN : ListN []\n consLN : \u2200 {n ns} \u2192 N n \u2192 ListN ns \u2192 ListN (n \u2237 ns)\n\n -- Example.\n ln : ListN (zero \u2237 one \u2237 [])\n ln = consLN zN (consLN oneN nilLN)\n\n------------------------------------------------------------------------------\n\n-- The inductive FOTC types using data.\n\nmodule Inductive where\n\n -- The FOTC natural numbers type.\n data N : D \u2192 Set where\n zN : N zero\n sN : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n\n -- Example.\n one : D\n one = succ\u2081 zero\n\n oneN : N one\n oneN = sN zN\n\n -- The FOTC lists type.\n data List : D \u2192 Set where\n nilL : List []\n consL : \u2200 x {xs} \u2192 List xs \u2192 List (x \u2237 xs)\n\n -- Example\n l : List (zero \u2237 true \u2237 [])\n l = consL zero (consL true nilL)\n\n -- The FOTC list of natural numbers type.\n data ListN : D \u2192 Set where\n nilLN : ListN []\n consLN : \u2200 {n ns} \u2192 N n \u2192 ListN ns \u2192 ListN (n \u2237 ns)\n\n -- Example.\n ln : ListN (zero \u2237 one \u2237 [])\n ln = consLN zN (consLN oneN nilLN)\n\n------------------------------------------------------------------------------\n\n-- The least fixed-point operator.\n\nmodule LFP where\n\n postulate\n -- Least fixed-points correspond to inductively defined types.\n --\n -- N.B. We cannot write LFP in first-order logic. We should use an\n -- instance instead.\n LFP : ((D \u2192 Set) \u2192 D \u2192 Set) \u2192 D \u2192 Set\n\n -- In FOTC, we cannot use the equality on predicates, i.e. we\n -- cannot write\n --\n -- (f : (D \u2192 Set) \u2192 D \u2192 Set) \u2192 f (LFP f) \u2261 LFP f (1)\n --\n -- because the type of the equality is\n --\n -- _\u2261_ : D \u2192 D \u2192 Set,\n --\n -- therefore we postulate both directions of the conversion rule (1).\n\n LFP\u2081 : (f : (D \u2192 Set) \u2192 D \u2192 Set)(d : D) \u2192 LFP f d \u2192 f (LFP f) d\n LFP\u2082 : (f : (D \u2192 Set) \u2192 D \u2192 Set)(d : D) \u2192 f (LFP f) d \u2192 LFP f d\n\n------------------------------------------------------------------------------\n\n-- The greatest fixed-point operator.\n\n-- N.B. At the moment, the definitions of LFP and GFP are the same.\n\nmodule GFP where\n\n postulate\n -- Greatest fixed-points correspond to coinductively defined types.\n\n GFP : ((D \u2192 Set) \u2192 D \u2192 Set) \u2192 D \u2192 Set\n\n GFP\u2081 : (f : (D \u2192 Set) \u2192 D \u2192 Set)(d : D) \u2192 GFP f d \u2192 f (GFP f) d\n GFP\u2082 : (f : (D \u2192 Set) \u2192 D \u2192 Set)(d : D) \u2192 f (GFP f) d \u2192 GFP f d\n\n------------------------------------------------------------------------------\n\n-- The FOTC natural numbers type as the least fixed-point of a\n-- functor.\n\nmodule NLFP where\n\n open LFP\n\n -- Functor for the FOTC natural numbers type.\n\n -- From Peter: NatF if D was an inductive type\n -- NatF : (D \u2192 Set) \u2192 D \u2192 Set\n -- NatF X zero = \u22a4\n -- NatF X (succ\u2081 n) = X n\n\n -- From Peter: NatF in pure predicate logic.\n NatF : (D \u2192 Set) \u2192 D \u2192 Set\n NatF X n = n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ\u2081 m \u2227 X m)\n\n -- The FOTC natural numbers type using LFP.\n N : D \u2192 Set\n N = LFP NatF\n\n -- The data constructors of N.\n zN : N zero\n zN = LFP\u2082 NatF zero (inj\u2081 refl)\n\n sN : {n : D} \u2192 N n \u2192 N (succ\u2081 n)\n sN {n} Nn = LFP\u2082 NatF (succ\u2081 n) (inj\u2082 (n , (refl , Nn)))\n\n -- Example.\n one : D\n one = succ\u2081 zero\n\n oneN : N one\n oneN = sN zN\n\n------------------------------------------------------------------------------\n\n-- The FOTC list type as the least fixed-point of a functor.\n\nmodule ListLFT where\n\n open LFP\n\n -- Functor for the FOTC lists type.\n ListF : (D \u2192 Set) \u2192 D \u2192 Set\n ListF X ds = ds \u2261 [] \u2228 (\u2203 \u03bb e \u2192 \u2203 \u03bb es \u2192 ds \u2261 e \u2237 es \u2227 X es)\n\n -- The FOTC list type using LFP.\n List : D \u2192 Set\n List = LFP ListF\n\n -- The data constructors of List.\n nilL : List []\n nilL = LFP\u2082 ListF [] (inj\u2081 refl)\n\n consL : \u2200 x {xs} \u2192 List xs \u2192 List (x \u2237 xs)\n consL x {xs} Lxs = LFP\u2082 ListF (x \u2237 xs) (inj\u2082 (x , xs , refl , Lxs))\n\n -- Example.\n l : List (zero \u2237 true \u2237 [])\n l = consL zero (consL true nilL)\n\n------------------------------------------------------------------------------\n\n-- The FOTC list of natural numbers type as the least fixed-point of a\n-- functor.\n\nmodule ListNLFT where\n\n open LFP\n open NLFP\n\n -- Functor for the FOTC list of natural numbers type.\n ListNF : (D \u2192 Set) \u2192 D \u2192 Set\n ListNF X ds = ds \u2261 [] \u2228 (\u2203 \u03bb e \u2192 \u2203 \u03bb es \u2192 ds \u2261 e \u2237 es \u2227 N e \u2227 X es)\n\n -- The FOTC list type using LFP.\n ListN : D \u2192 Set\n ListN = LFP ListNF\n\n -- The data constructors of ListN.\n nilLN : ListN []\n nilLN = LFP\u2082 ListNF [] (inj\u2081 refl)\n\n consLN : \u2200 {n ns} \u2192 N n \u2192 ListN ns \u2192 ListN (n \u2237 ns)\n consLN {n} {ns} Nn LNns =\n LFP\u2082 ListNF (n \u2237 ns) (inj\u2082 (n , ns , refl , Nn , LNns))\n\n -- Example.\n ln : ListN (zero \u2237 one \u2237 [])\n ln = consLN zN (consLN oneN nilLN)\n\n------------------------------------------------------------------------------\n\n-- The FOTC Colist type as the greatest fixed-point of a functor.\n\nmodule CoList where\n\n open GFP\n\n -- Functor for the FOTC Colists type (the same functor that for the\n -- List type).\n ColistF : (D \u2192 Set) \u2192 D \u2192 Set\n ColistF X ds = ds \u2261 [] \u2228 (\u2203 \u03bb e \u2192 \u2203 \u03bb es \u2192 ds \u2261 e \u2237 es \u2227 X es)\n\n -- The FOTC Colist type using GFP.\n Colist : D \u2192 Set\n Colist = GFP ColistF\n\n -- The data constructors of Colist.\n nilCL : Colist []\n nilCL = GFP\u2082 ColistF [] (inj\u2081 refl)\n\n consCL : \u2200 x xs \u2192 Colist xs \u2192 Colist (x \u2237 xs)\n consCL x xs CLxs = GFP\u2082 ColistF (x \u2237 xs) (inj\u2082 (x , xs , refl , CLxs))\n\n -- Example (finite colist).\n l : Colist (zero \u2237 true \u2237 [])\n l = consCL zero (true \u2237 []) (consCL true [] nilCL)\n\n -- TODO: Example (infinite colist).\n -- zerosCL : Colist {!!}\n -- zerosCL = consCL zero {!!} zerosCL\n\n------------------------------------------------------------------------------\n\n-- The FOTC Stream type as the greatest fixed-point of a functor.\n\nmodule Stream\u2081 where\n\n open GFP\n\n -- Functor for the FOTC Stream type.\n StreamF : (D \u2192 Set) \u2192 D \u2192 Set\n StreamF X ds = \u2203 \u03bb e \u2192 X ds\n\n -- The FOTC Stream type using GFP.\n Stream : D \u2192 Set\n Stream = GFP StreamF\n\n -- The data constructor of Stream.\n -- Using StreamF we cannot define this data constructor\n -- consS : \u2200 x xs \u2192 Stream xs \u2192 Stream (x \u2237 xs)\n -- consS x xs Sxs = GFP\u2082 StreamF (x \u2237 xs) {!!}\n\n------------------------------------------------------------------------------\n\n-- The FOTC Stream type as the greatest fixed-point of a functor.\n\nmodule Stream\u2082 where\n\n open GFP\n\n -- Functor for the FOTC Stream type.\n StreamF : (D \u2192 Set) \u2192 D \u2192 Set\n StreamF X ds = \u2203 \u03bb e \u2192 \u2203 \u03bb es \u2192 X es\n\n -- The FOTC Stream type using GFP.\n Stream : D \u2192 Set\n Stream = GFP StreamF\n\n -- The data constructor of Stream.\n -- TODO: To use implicit arguments.\n consS : \u2200 x xs \u2192 Stream xs \u2192 Stream (x \u2237 xs)\n consS x xs Sxs = GFP\u2082 StreamF (x \u2237 xs) (x , xs , Sxs)\n\n -- TODO: Example\n -- zerosS : Stream {!!}\n -- zerosS = consS zero {!!} zerosS\n\n headS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 D\n headS {x} _ = x\n\n tailS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\n tailS {x} {xs} S = GFP\u2082 StreamF xs (x , x \u2237 xs , S)\n\n -- The functor StreamF does not link together the parts of the\n -- stream, so I can get a stream from any stream.\n bad : \u2200 {xs ys} \u2192 Stream xs \u2192 Stream ys\n bad {xs} {ys} S = GFP\u2082 StreamF ys (ys , xs , S)\n\n------------------------------------------------------------------------------\n\n-- The FOTC Stream type as the greatest fixed-point of a functor.\n\nmodule Stream\u2083 where\n\n open GFP\n\n -- Functor for the FOTC Stream type.\n StreamF : (D \u2192 Set) \u2192 D \u2192 Set\n StreamF X ds = \u2203 \u03bb e \u2192 \u2203 \u03bb es \u2192 ds \u2261 e \u2237 es \u2227 X es\n\n -- The FOTC Stream type using GFP.\n Stream : D \u2192 Set\n Stream = GFP StreamF\n\n -- The data constructor of Stream.\n -- TODO: To use implicit arguments.\n consS : \u2200 x xs \u2192 Stream xs \u2192 Stream (x \u2237 xs)\n consS x xs Sxs = GFP\u2082 StreamF (x \u2237 xs) (x , xs , refl , Sxs)\n\n -- TODO: Example\n -- zerosS : Stream {!!}\n -- zerosS = consS zero {!!} zerosS\n\n headS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 D\n headS {x} _ = x\n\n tailS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\n tailS {x} {xs} Sx\u2237xs = Sxs\n where\n unfoldS : StreamF (GFP StreamF) (x \u2237 xs)\n unfoldS = GFP\u2081 StreamF (x \u2237 xs) Sx\u2237xs\n\n e : D\n e = \u2203-proj\u2081 unfoldS\n\n Pe : \u2203 \u03bb es \u2192 x \u2237 xs \u2261 e \u2237 es \u2227 GFP StreamF es\n Pe = \u2203-proj\u2082 unfoldS\n\n es : D\n es = \u2203-proj\u2081 Pe\n\n Pes : x \u2237 xs \u2261 e \u2237 es \u2227 GFP StreamF es\n Pes = \u2203-proj\u2082 Pe\n\n xs\u2261es : xs \u2261 es\n xs\u2261es = \u2227-proj\u2082 (\u2237-injective (\u2227-proj\u2081 Pes))\n\n Sxs : GFP StreamF xs\n Sxs = subst (GFP StreamF) (sym xs\u2261es) (\u2227-proj\u2082 Pes)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"30c4508e8343a2319a8a365ad65b0c8f13a6c8c5","subject":"More bintree fun","message":"More bintree fun\n","repos":"crypto-agda\/crypto-agda","old_file":"bintree.agda","new_file":"bintree.agda","new_contents":"module bintree where\n\nopen import Function\nopen import Data.Nat\nopen import Data.Bool\nopen import Data.Bits\n\ndata Tree {a} (A : Set a) : \u2115 \u2192 Set a where\n leaf : \u2200 {n} \u2192 A \u2192 Tree A n\n fork : \u2200 {n} \u2192 (left right : Tree A n) \u2192 Tree A (suc n)\n\nfromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Tree A n\nfromFun {zero} f = leaf (f [])\nfromFun {suc n} f = fork (fromFun (f \u2218 0\u2237_)) (fromFun (f \u2218 1\u2237_))\n\ntoFun : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Bits n \u2192 A\ntoFun (leaf x) _ = x\ntoFun (fork left right) (b \u2237 bs) = toFun (if b then left else right) bs\n\nlookup : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Tree A n \u2192 A\nlookup = flip toFun\n\nmap : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 Tree A n \u2192 Tree B n\nmap f (leaf x) = leaf (f x)\nmap f (fork t\u2080 t\u2081) = fork (map f t\u2080) (map f t\u2081)\n\ndata Rot {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n leaf : \u2200 {n} x \u2192 Rot {n = n} (leaf x) (leaf x)\n fork : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 left\u2081 \u2192\n Rot right\u2080 right\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n krof : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 right\u2081 \u2192\n Rot right\u2080 left\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n\nopen import Relation.Binary\n\nRot-refl : \u2200 {n a} {A : Set a} \u2192 Reflexive (Rot {A = A} {n})\nRot-refl {x = leaf x} = leaf x\nRot-refl {x = fork left right} = fork Rot-refl Rot-refl\n\nRot-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Rot {A = A} {n})\nRot-sym (leaf x) = leaf x\nRot-sym (fork p\u2080 p\u2081) = fork (Rot-sym p\u2080) (Rot-sym p\u2081)\nRot-sym (krof p\u2080 p\u2081) = krof (Rot-sym p\u2081) (Rot-sym p\u2080)\n\nRot-trans : \u2200 {n a} {A : Set a} \u2192 Transitive (Rot {A = A} {n})\nRot-trans (leaf x) q = q\nRot-trans (fork p\u2080 p\u2081) (fork q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (fork p\u2080 p\u2081) (krof q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (krof p\u2080 p\u2081) (fork q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\nRot-trans (krof p\u2080 p\u2081) (krof q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\n","old_contents":"module bintree\n ([0,1] : Set)\nwhere\n\nopen import Data.Nat\nopen import Data.Bits\n\ndata T : \u2115 \u2192 Set where\n leaf : \u2200 {n} \u2192 Bits n \u2192 T n\n fork : \u2200 {n} (proba : [0,1]) (left right : T n) \u2192 T (1 + n)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"85b48bf78ebbcaed626a162fe5616b7890cc7e8a","subject":"validity-of-derive on bags (toward #51)","message":"validity-of-derive on bags (toward #51)\n\nOld-commit-hash: 02f7fd17338ba8df5698d20442c1956021f53f0a\n","repos":"inc-lc\/ilc-agda","old_file":"experimental\/NatBag.agda","new_file":"experimental\/NatBag.agda","new_contents":"{-\nThe goal of this file is to make the 3rd example\ndescribed in \/examples.md, \"Map.mapValues\", fast:\n\n inc :: Bag Integer -> Bag Integer\n inc = map (+1)\n\n old = fromList [1, 2 .. n - 1, n]\n res = inc old = [2, 3 .. n, n + 1]\n\nTODO\n1. Make sure this file has no hole\n X. Replace \u2115 by \u2124\n 0. Replace \u2124 by \u2115 -- our bags are bags of nats now.\n 0. Introduce addition\n 0. Add MapBags and map\n 0. Figure out a way to communicate to a derivative that\n certain changes are always nil (in this case, `+1`).\n2. Finish ExplicitNils\n3. Consider appending ExplicitNils\n\n\nChecklist: Adding syntactic constructs\n\n- weaken\n- \u27e6_\u27e7Term\n- weaken-sound\n- derive (symbolic derivation)\n- validity-of-derive\n- correctness-of-derive\n\nChecklist: Adding types\n\n- \u27e6_\u27e7Type\n- \u0394-Type\n- \u27e6derive\u27e7\n- _\u27e6\u229d\u27e7_\n- _\u27e6\u2295\u27e7_\n- f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f\n- f\u2295[g\u229df]=g\n- f\u2295\u0394f=f\n- valid-\u0394\n- R[f,g\u229df]\n- df=f\u2295df\u229df\n- R (inside validity-of-derive)\n\n-}\n\nmodule NatBag where\n\nopen import Data.NatBag renaming\n (map to mapBag ; empty to emptyBag ; update to updateBag)\nopen import Relation.Binary.PropositionalEquality\n\n-- This module has holes and can't be imported.\n-- We postulate necessary properties now to be able\n-- to work on derivation and remove them later.\n--\n-- open import Data.NatBag.Properties\npostulate b\\\\b=\u2205 : \u2200 {{b : Bag}} \u2192 b \\\\ b \u2261 emptyBag\npostulate b++\u2205=b : \u2200 {{b : Bag}} \u2192 b ++ emptyBag \u2261 b\npostulate b++[d\\\\b]=d : \u2200 {b d} \u2192 b ++ (d \\\\ b) \u2261 d\npostulate [b++d]\\\\b=d : \u2200 {b d} \u2192 (b ++ d) \\\\ b \u2261 d\n\n\nopen import Data.Nat\n\nopen import Data.Unit using\n (\u22a4 ; tt)\n\nimport Data.Integer as \u2124\n\nopen import Data.Product using\n (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\nimport Level\nimport Data.Product as Product\n\npostulate extensionality : Extensionality Level.zero Level.zero\n\n\ndata Type : Set where\n nats : Type\n bags : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u03c4 \u0393} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u03c4 \u03c4\u2032 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n bag : \u2200 {\u0393} \u2192 (b : Bag) \u2192 Term \u0393 bags\n add : \u2200 {\u0393} \u2192 (t\u2081 : Term \u0393 nats) \u2192 (t\u2082 : Term \u0393 nats) \u2192 Term \u0393 nats\n map : \u2200 {\u0393} \u2192 (f : Term \u0393 (nats \u21d2 nats)) \u2192 (b : Term \u0393 bags) \u2192 Term \u0393 bags\n union : \u2200 {\u0393} \u2192 (b\u2081 : Term \u0393 bags) \u2192 (b\u2082 : Term \u0393 bags) \u2192 Term \u0393 bags\n diff : \u2200 {\u0393} \u2192 (b\u2081 : Term \u0393 bags) \u2192 (b\u2082 : Term \u0393 bags) \u2192 Term \u0393 bags\n\n var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n -- Change to nats = replacement Church pairs\n -- 3 -> 5 ::= \u03bbf. f 3 5\n\n -- Change to bags = a summand\n -- b\u2081 -> b\u2082 ::= b\u2082 \\\\ b\u2081\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u0393\u227c\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0393\n\u0393\u227c\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0393 {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u0393\u227c\u0393 {\u0393}\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\nweaken subctx (bag b) = bag b\nweaken subctx (add t\u2081 t\u2082) = add (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (map f b) = map (weaken subctx f) (weaken subctx b)\nweaken subctx (union b\u2081 b\u2082) = union (weaken subctx b\u2081) (weaken subctx b\u2082)\nweaken subctx (diff b\u2081 b\u2082) = diff (weaken subctx b\u2081) (weaken subctx b\u2082)\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 nats \u27e7Type = \u2115\n\u27e6 bags \u27e7Type = Bag\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\u27e6 bag b \u27e7Term \u03c1 = b\n\u27e6 add m n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 map f b \u27e7Term \u03c1 = mapBag (\u27e6 f \u27e7Term \u03c1) (\u27e6 b \u27e7Term \u03c1)\n\u27e6 union b d \u27e7Term \u03c1 = \u27e6 b \u27e7Term \u03c1 ++ \u27e6 d \u27e7Term \u03c1\n\u27e6 diff b d \u27e7Term \u03c1 = \u27e6 b \u27e7Term \u03c1 \\\\ \u27e6 d \u27e7Term \u03c1\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n_\u27e8$\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\ninfix 0 _\u27e8$\u27e9_ -- infix 0 $ in Haskell\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = weaken-sound t\u2081 \u03c1 \u27e8$\u27e9 weaken-sound t\u2082 \u03c1\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (bag b) \u03c1 = refl\nweaken-sound (add m n) \u03c1 = cong\u2082 _+_ (weaken-sound m \u03c1) (weaken-sound n \u03c1)\nweaken-sound (map f b) \u03c1 =\n cong\u2082 mapBag (weaken-sound f \u03c1) (weaken-sound b \u03c1)\nweaken-sound (union b d) \u03c1 =\n cong\u2082 _++_ (weaken-sound b \u03c1) (weaken-sound d \u03c1)\nweaken-sound (diff b d) \u03c1 =\n cong\u2082 _\\\\_ (weaken-sound b \u03c1) (weaken-sound d \u03c1)\n\n-- Changes to \u2115 are replacement Church pairs. The only arguments\n-- of conern are `fst` and `snd`, so the Church pairs don't have\n-- to be polymorphic.\n--\n-- Changes on bags are bags. They allow negative multiplicities\n-- to begin with.\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type nats = (nats \u21d2 nats \u21d2 nats) \u21d2 nats\n\u0394-Type bags = bags\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393 -- push \u03c4, then push \u0394\u03c4.\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 (keep \u03c4 \u2022 \u0393\u227c\u0394\u0393)\n\n-- It is clear that \u27e6\u229d\u27e7 exists on the semantic level:\n-- there exists an Agda value to describe the change between any\n-- two Agda values denoted by terms. If we have (not dependently-\n-- typed) arrays, no term denotes the change between two arrays\n-- of different lengths. Thus no full abstraction.\n\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n_\u229d_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4)\n_\u2295_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4) \u2192 Term \u0393 \u03c4\n\ninfixl 6 _\u27e6\u2295\u27e7_\ninfixl 6 _\u27e6\u229d\u27e7_\ninfixl 6 _\u229d_\ninfixl 6 _\u2295_\n\n\u27e6fst\u27e7 \u27e6snd\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e6fst\u27e7 m n = m\n\u27e6snd\u27e7 m n = n\n\nfst snd : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\nfst = abs (abs (var (that this)))\nsnd = abs (abs (var this))\n\n\u27e6derive\u27e7 : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\u27e6derive\u27e7 {nats} n = \u03bb f \u2192 f n n\n\u27e6derive\u27e7 {bags} b = emptyBag\n\u27e6derive\u27e7 {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 f (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f v\n\n_\u27e6\u229d\u27e7_ {nats} m n = \u03bb f \u2192 f n m\n_\u27e6\u229d\u27e7_ {bags} b\u2081 b\u2082 = b\u2081 \\\\ b\u2082\n_\u27e6\u229d\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 f\u2081 (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f\u2082 v\n-- m \u27e6\u229d\u27e7 n ::= replace n by m\n\n-- m \u229d n = \u03bb f. f n m\n_\u229d_ {nats} m n =\n abs (app (app (var this)\n (weaken (drop _ \u2022 \u0393\u227c\u0393) n)) (weaken (drop _ \u2022 \u0393\u227c\u0393) m))\n-- d \u229d b = d \\\\ b\n_\u229d_ {bags} d b = weaken \u0393\u227c\u0393 (diff d b)\n-- g \u229d f = \u03bb x. \u03bb dx. g (x \u2295 dx) \u229d f x -- Incurs recomputation!\n_\u229d_ {\u03c4 \u21d2 \u03c4\u2081} g f =\n abs (abs ((app (weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) g)\n (var (that this) \u2295 var this))\n \u229d app (weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) f) (var (that this))))\n\n_\u2295_ {nats} n dn = app dn snd\n_\u2295_ {bags} b db = union b db\n-- f \u2295 df = \u03bb x. f x \u2295 df x (x \u229d x) -- Incurs recomputation!\n_\u2295_ {\u03c4 \u21d2 \u03c4\u2081} f df =\n abs (app (weaken (drop _ \u2022 \u0393\u227c\u0393) f) (var this)\n \u2295 app (app (weaken (drop _ \u2022 \u0393\u227c\u0393) df)\n (var this)) ((var this) \u229d (var this)))\n\n\n_\u27e6\u2295\u27e7_ {nats} n dn = dn \u27e6snd\u27e7\n_\u27e6\u2295\u27e7_ {bags} b db = b ++ db\n_\u27e6\u2295\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n\n-- Cool lemmas\n\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u229d\u27e7 f \u2261 \u27e6derive\u27e7 f\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {nats} f = refl\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {bags} b = b\\\\b=\u2205\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = refl\n\nf\u2295[g\u229df]=g : \u2200 {\u03c4 : Type} (f g : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) \u2261 g\n\nf\u2295\u0394f=f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (\u27e6derive\u27e7 f) \u2261 f\n\nf\u2295[g\u229df]=g {nats} m n = refl\nf\u2295[g\u229df]=g {bags} b\u2081 b\u2082 = b++[d\\\\b]=d {b\u2081} {b\u2082}\nf\u2295[g\u229df]=g {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = f x} {y = f x} refl\n (cong\u2082 _\u27e6\u229d\u27e7_ (cong g (f\u2295\u0394f=f x)) refl) \u27e9\n f x \u27e6\u2295\u27e7 (g x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (g x) \u27e9\n g x\n \u220e\n ) where open \u2261-Reasoning\n\nf\u2295\u0394f=f {nats} n = refl\nf\u2295\u0394f=f {bags} b = b++\u2205=b\nf\u2295\u0394f=f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 \u27e6derive\u27e7 f) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (f (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong (\u03bb hole \u2192 f x \u27e6\u2295\u27e7 (f hole \u27e6\u229d\u27e7 f x)) (f\u2295\u0394f=f x) \u27e9\n f x \u27e6\u2295\u27e7 (f x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (f x) \u27e9\n f x\n \u220e\n ) where open \u2261-Reasoning\n\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {nats} n dn = n \u2261 dn \u27e6fst\u27e7\nvalid-\u0394 {bags} b db = \u22a4 -- all bags are vald for all other bags\nvalid-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (s : \u27e6 \u03c4\u2081 \u27e7) (ds : \u27e6 \u0394-Type \u03c4\u2081 \u27e7) (R[s,ds] : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7 -- (valid-\u0394:1)\n (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 f s \u27e6\u2295\u27e7 df s ds -- (valid-\u0394:2)\n\nR[f,g\u229df] : \u2200 {\u03c4} (f g : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (g \u27e6\u229d\u27e7 f)\nR[f,g\u229df] {nats} m n = refl\nR[f,g\u229df] {bags} b\u2081 b\u2082 = tt\nR[f,g\u229df] {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb x dx R[x,dx] \u2192\n R[f,g\u229df] {\u03c4\u2082} (f x) (g (x \u27e6\u2295\u27e7 dx)) -- (valid-\u0394:1)\n , -- tuple constructor\n (begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 refl \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7\n (g ((x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 \u27e6derive\u27e7 (x \u27e6\u2295\u27e7 dx)) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 cong (\u03bb hole \u2192 f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g hole \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx)))\n (f\u2295\u0394f=f (x \u27e6\u2295\u27e7 dx)) \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 f\u2295[g\u229df]=g (f (x \u27e6\u2295\u27e7 dx)) (g (x \u27e6\u2295\u27e7 dx)) \u27e9\n g (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (f x) (g (x \u27e6\u2295\u27e7 dx))) \u27e9\n f x \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) x dx\n \u220e)\n where open \u2261-Reasoning\n\nR[f,\u0394f] : \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (\u27e6derive\u27e7 f)\nR[f,\u0394f] f rewrite sym (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f f) = R[f,g\u229df] f f\n\n-- Data type to hold proofs that a \u0394-env is consistent with self\ndata Consistent-\u0394env : {\u0393 : Context} (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is consistent with itself\n d\u03c1=\u2205 : Consistent-\u0394env {\u2205} \u2205\n -- Induction case: the change introduced at top level\n -- should be valid.\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Consistent-\u0394env d\u03c1\u2080 \u2192\n Consistent-\u0394env {\u03c4 \u2022 \u0393\u2080} (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- finally, update and ignore\n\nupdate : \u2200 {\u0393} \u2192 (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env d\u03c1} \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 {d\u03c1=\u2205} = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 d\u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] consistent[d\u03c1\u2080]} =\n (v \u27e6\u2295\u27e7 dv) \u2022 update d\u03c1 {consistent[d\u03c1\u2080]}\n\n-- Ignorance is bliss (don't have to pass consistency proofs around :D)\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 -- Using a proof to describe computation\n\n-- Naming scheme follows weakenVar\/weaken\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- derive(n) = \u03bbf. f n n\nderive (nat n) = abs (app (app (var this) (nat n)) (nat n))\n\n-- derive(x) = dx\nderive (var x) = var (deriveVar x)\n\n-- derive(\u03bbx. t) = \u03bbx. \u03bbdx. derive(t)\nderive (abs t) = abs (abs (derive t))\n\n-- derive(f s) = derive(f) s derive(s)\nderive (app f s) = app (app (derive f) (weaken \u0393\u227c\u0394\u0393 s)) (derive s)\n\n-- derive(x + y) = replace (x + y) by (dx snd + dy snd)\n-- = \u03bb f. f (x + y) (dx snd + dy snd)\nderive (add x y) =\n abs (app (app (var this)\n (add (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) x) (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) y)))\n (add (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive x)) snd)\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive y)) snd)))\n\n-- derive(bag) = \u2205\nderive (bag b) = bag emptyBag\n\n-- derive(map f b) = map (f \u2295 df) (b \u2295 db) \u229d map f b\nderive (map f b) = map ((weaken \u0393\u227c\u0394\u0393 f) \u2295 derive f)\n ((weaken \u0393\u227c\u0394\u0393 b) \u2295 derive b)\n \u229d weaken \u0393\u227c\u0394\u0393 (map f b)\n\n-- derive(b ++ d) = derive(b) ++ derive(d)\nderive (union b d) = union (derive b) (derive d)\n\n-- derive(b \\\\ d) = derive(b) \\\\ derive(d)\nderive (diff b d) = diff (derive b) (derive d)\n\n\n-- Extensional equivalence for changes\ndata Extensionally-equivalent-as-changes :\n \u2200 (\u03c4 : Type) \u2192 (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 (dg : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n ext-\u0394 : \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n (\u2200 (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192\n (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)) \u2192\n Extensionally-equivalent-as-changes \u03c4 df dg\n\nsyntax Extensionally-equivalent-as-changes \u03c4 df dg = df \u2248 dg :\u0394 \u03c4\n\n-- Question: How to declare fixity for infix syntax?\n-- infix 4 _\u2248_:\u0394_ -- same as \u2261\n\n-- Extractor for extensional-equivalence-as-changes:\n-- Given a value of the data type holding the proof,\n-- returns the proof in applicable form.\n--\n-- It would not be necessary if such\n-- proof-holding types were defined as a function in\n-- the first place, say in the manner of `valid-\u0394`.\n--\nextract-\u0394equiv :\n \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n df \u2248 dg :\u0394 \u03c4 \u2192\n (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192 (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)\n\nextract-\u0394equiv (ext-\u0394 proof-method) = proof-method\n\n-- Distribution lemmas of validity over \u27e6\u229d\u27e7 and \u27e6\u2295\u27e7\n\napplication-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v) \u2261 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\napplication-over-\u2295-and-\u229d f g df v =\n begin\n f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (cong\u2082 _\u27e6\u2295\u27e7_\n (cong f ((sym (f\u2295\u0394f=f v))))\n (cong\u2082 df\n (sym (f\u2295\u0394f=f v))\n (cong \u27e6derive\u27e7 (sym (f\u2295\u0394f=f v)))))\n refl \u27e9\n f (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v)\n \u27e6\u2295\u27e7 df (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v) (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v))\n \u27e6\u229d\u27e7 f v\n \u2261\u27e8 refl \u27e9\n (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e where open \u2261-Reasoning\n\nvalidity-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n valid-\u0394 g (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) \u2192\n valid-\u0394 (g v) (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\nvalidity-over-\u2295-and-\u229d f g df v R[g,f\u2295df]\n rewrite application-over-\u2295-and-\u229d f g df v =\n proj\u2081 (R[g,f\u2295df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))\n\n-- diff-apply\ndf=f\u2295df\u229df :\n \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n valid-\u0394 f df \u2192 df \u2248 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) :\u0394 \u03c4\n\n-- Case nat: this REFL is more obvious to Agda than to a human.\ndf=f\u2295df\u229df {nats} n dn valid-n-dn =\n ext-\u0394 (\u03bb m valid-m-dn valid-rhs \u2192 refl)\n\n-- Case bags: rely on set-theoretic interpretation of bags.\ndf=f\u2295df\u229df {bags} b db valid-b-db = ext-\u0394 (\u03bb d _ _ \u2192\n begin -- Reasoning done in 1 step. Here for clarity only.\n d \u27e6\u2295\u27e7 db\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_ {x = d} refl (sym ([b++d]\\\\b=d {b} {db})) \u27e9\n d \u27e6\u2295\u27e7 (b \u27e6\u2295\u27e7 db \u27e6\u229d\u27e7 b)\n \u220e) where open \u2261-Reasoning\n\ndf=f\u2295df\u229df {\u03c4\u2081 \u21d2 \u03c4\u2082} f df R[f,df] = ext-\u0394 (\n \u03bb g R[g,df] R[g,f\u2295df\u229df] \u2192\n extensionality (\u03bb v \u2192\n begin\n g v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n \u2261\u27e8 extract-\u0394equiv\n (df=f\u2295df\u229df\n (f v) (df v (\u27e6derive\u27e7 v))\n (proj\u2081 (R[f,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))))\n (g v)\n (proj\u2081 (R[g,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v)))\n (validity-over-\u2295-and-\u229d f g df v R[g,f\u2295df\u229df]) \u27e9\n g v \u27e6\u2295\u27e7 (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = g v} {y = g v} refl\n (application-over-\u2295-and-\u229d f g df v) \u27e9\n g v \u27e6\u2295\u27e7 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e\n )\n )\n where open \u2261-Reasoning\n\ncorrectness-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n (\u27e6 deriveVar x \u27e7 \u03c1)\n \u2248\n (\u27e6 x \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 x \u27e7 (ignore \u03c1)) :\u0394 \u03c4\n\ncorrectness-of-deriveVar {\u03c4 \u2022 \u0393\u2080} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n this = df=f\u2295df\u229df {\u03c4} v dv R[v,dv]\n\ncorrectness-of-deriveVar {\u03c4\u2080 \u2022 \u0393\u2080} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4\u2080} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n (that x) = correctness-of-deriveVar \u03c1 x\n\n\n\ncorrectness-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n\n (\u27e6 derive t \u27e7 \u03c1)\n \u2248 (\u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 t \u27e7 (ignore \u03c1)) :\u0394 \u03c4\n\ncorrectness-of-derive\u2032 : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n\n (\u27e6 derive t \u27e7 \u03c1)\n \u2248 (\u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 \u03c1) :\u0394 \u03c4\n\ncorrectness-of-derive\u2032 \u03c1 {consistency} t\n rewrite weaken-sound {subctx = \u0393\u227c\u0394\u0393} t \u03c1\n = correctness-of-derive \u03c1 {consistency} t\n\n\n-- Mutually recursive lemma: derivatives are valid\n\nvalidity-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 deriveVar x \u27e7 \u03c1)\n\nvalidity-of-deriveVar {\u03c4 \u2022 \u0393} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n this = R[v,dv]\n\nvalidity-of-deriveVar {\u03c4\u2080 \u2022 \u0393} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n (that x) = validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 {consistency} (var x) =\n validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs t) = \u03bb v dv R[v,dv] \u2192\n validity-of-derive (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency} t\n , -- tuple constructor\n (begin\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n ((R[f,g\u229df] (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 f\u2295[g\u229df]=g (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u2261\u27e8 cong \u27e6 t \u27e7 (cong\u2082 _\u2022_ (f\u2295\u0394f=f (v \u27e6\u2295\u27e7 dv)) refl) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency}))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 sym (extract-\u0394equiv\n (correctness-of-derive\n ((dv \u2022 v \u2022 \u03c1))\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (R[f,g\u229df] (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u220e)\n where open \u2261-Reasoning\n\nvalidity-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} t\u2081 t\u2082)\n = R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7]\n where\n open \u2261-Reasoning\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 : \u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1) \u2261 v\u2081 v\u2082\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 rewrite (sym v\u2082=old-v\u2082) = refl\n\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 : \u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1 \u2261 dv\u2081 v\u2082 dv\u2082\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = refl\n\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] : valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (dv\u2081 v\u2082 dv\u2082)\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] rewrite \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 = R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n -- What I want to write:\n {-\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] rewrite \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n -}\n\n -- What I have to write:\n\n R : {\u03c4 : Type} \u2192 {v : \u27e6 \u03c4 \u27e7} \u2192 {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 valid-\u0394 v dv\u2081 \u2192 valid-\u0394 v dv\u2082\n\n R {nats} dv\u2081=dv\u2082 refl = cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl\n\n R {bags} dv\u2081=dv\u2082 _ = tt\n\n --R {\u03c4\u2081 \u21d2 \u03c4\u2082} dv\u2081=dv\u2082 valid-dv\u2081 rewrite dv\u2081=dv\u2082 = {!valid-dv\u2081!}\n R {\u03c4\u2081 \u21d2 \u03c4\u2082} {v} {dv\u2081} {dv\u2082} dv\u2081=dv\u2082 valid-dv\u2081 =\n \u03bb s ds R[s,ds] \u2192\n R {\u03c4\u2082} {v s} {dv\u2081 s ds} {dv\u2082 s ds}\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl)\n (proj\u2081 (valid-dv\u2081 s ds R[s,ds]))\n ,\n (begin\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2082 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v (s \u27e6\u2295\u27e7 ds)} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) (sym dv\u2081=dv\u2082) refl) refl) \u27e9\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2081 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 sym (proj\u2082 (valid-dv\u2081\n (s \u27e6\u2295\u27e7 ds)\n (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n (R[f,\u0394f] (s \u27e6\u2295\u27e7 ds)))) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds \u27e6\u2295\u27e7 \u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong (v \u27e6\u2295\u27e7 dv\u2081) (f\u2295\u0394f=f (s \u27e6\u2295\u27e7 ds)) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds)\n \u2261\u27e8 proj\u2082 (valid-dv\u2081 s ds R[s,ds]) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2081 s ds\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v s} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2082 s ds\n \u220e) where open \u2261-Reasoning\n\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] = R \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n\n-- Validity of deriving bag-typed terms is trivial.\n\nvalidity-of-derive \u03c1 (bag b) = tt\n\nvalidity-of-derive \u03c1 (map f b) = tt\n\nvalidity-of-derive \u03c1 (union b d) = tt\n\nvalidity-of-derive \u03c1 (diff d b) = tt\n\nvalidity-of-derive \u03c1 (nat n) = refl\n\nvalidity-of-derive \u03c1 {consistency} (add m n) =\n begin\n \u27e6 m \u27e7 (ignore \u03c1) + \u27e6 n \u27e7 (ignore \u03c1)\n \u2261\u27e8 cong\u2082 _+_\n (sym (weaken-sound m (\u27e6fst\u27e7 \u2022 \u03c1)))\n (sym (weaken-sound n (\u27e6fst\u27e7 \u2022 \u03c1))) \u27e9\n (\u27e6 weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) m \u27e7 (\u27e6fst\u27e7 \u2022 \u03c1) +\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) n \u27e7 (\u27e6fst\u27e7 \u2022 \u03c1))\n \u2261\u27e8 refl \u27e9\n \u27e6 abs (app (app (var this)\n (add (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) m) (weaken (drop _ \u2022 \u0393\u227c\u0394\u0393) n)))\n (add (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive m)) snd)\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive n)) snd)))\n \u27e7 \u03c1 \u27e6fst\u27e7\n \u2261\u27e8 refl \u27e9\n \u27e6 derive (add m n) \u27e7 \u03c1 \u27e6fst\u27e7\n \u220e where\n open \u2261-Reasoning\n blah : \u2115\n blah =\n \u27e6 add (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive m)) snd)\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) (derive n)) snd) \u27e7\n (\u27e6fst\u27e7 \u2022 \u03c1)\n\ncorrectness-of-derive \u03c1 (var x) = correctness-of-deriveVar \u03c1 x\n\ncorrectness-of-derive \u03c1 (nat n) = ext-\u0394 (\u03bb _ _ _ \u2192 refl)\n\ncorrectness-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs {.\u03c4\u2081} {.\u03c4\u2082} t) =\n ext-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n extensionality {\u27e6 \u03c4\u2081 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4\u2082 \u27e7} (\u03bb x \u2192\n begin\n f x \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive {\u03c4\u2081 \u2022 \u0393} {\u03c4\u2082}\n (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency}\n t)\n (f x)\n (proj\u2081 (R[f,\u0394t] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x)))\n (proj\u2081 (R[f,t\u2032\u229dt] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x))) \u27e9\n f x\n \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 (update (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency})\n \u27e6\u229d\u27e7 \u27e6 t \u27e7 (x \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u220e\n )) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} {\u0393} t\u2081 t\u2082) =\n ext-\u0394 {\u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n begin\n f \u27e6\u2295\u27e7 \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n \u2261\u27e8 extract-\u0394equiv ext-\u0394[t\u2081t\u2082] f R[f,\u0394t] R[f,t\u2032\u229dt] \u27e9\n f \u27e6\u2295\u27e7\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n \u220e)\n where\n open \u2261-Reasoning\n\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n v\u2081\u2295dv\u2081=v\u2081\u2032 : v\u2081 \u27e6\u2295\u27e7 dv\u2081 \u2261 v\u2081\u2032\n v\u2081\u2295dv\u2081=v\u2081\u2032 =\n begin\n v\u2081 \u27e6\u2295\u27e7 dv\u2081\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2081)\n v\u2081 valid-dv\u2081 (R[f,g\u229df] v\u2081 v\u2081\u2032) \u27e9\n v\u2081 \u27e6\u2295\u27e7 (v\u2081\u2032 \u27e6\u229d\u27e7 v\u2081)\n \u2261\u27e8 f\u2295[g\u229df]=g v\u2081 v\u2081\u2032 \u27e9\n v\u2081\u2032\n \u220e\n\n -- TODO: remove code duplication.\n v\u2082\u2295dv\u2082=v\u2082\u2032 : v\u2082 \u27e6\u2295\u27e7 dv\u2082 \u2261 v\u2082\u2032\n v\u2082\u2295dv\u2082=v\u2082\u2032 rewrite v\u2082=old-v\u2082 =\n begin\n old-v\u2082 \u27e6\u2295\u27e7 dv\u2082\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2082)\n old-v\u2082\n (validity-of-derive \u03c1 {consistency} t\u2082)\n (R[f,g\u229df] old-v\u2082 v\u2082\u2032) \u27e9\n old-v\u2082 \u27e6\u2295\u27e7 (v\u2082\u2032 \u27e6\u229d\u27e7 old-v\u2082)\n \u2261\u27e8 f\u2295[g\u229df]=g old-v\u2082 v\u2082\u2032 \u27e9\n v\u2082\u2032\n \u220e\n where old-v\u2082 = \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] : v\u2081\u2032 v\u2082\u2032 \u2261 (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082)\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] = sym (cong\u2082 (\u03bb f x \u2192 f x) v\u2081\u2295dv\u2081=v\u2081\u2032 v\u2082\u2295dv\u2082=v\u2082\u2032)\n\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 :\n (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082) \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 = proj\u2082 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082 \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n cong\u2082 _\u27e6\u229d\u27e7_\n (trans v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082)\n refl\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n (dv\u2081 v\u2082 dv\u2082) \u2248 (v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082) :\u0394 \u03c4\u2082\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n df=f\u2295df\u229df (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082) R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 : (dv\u2081 v\u2082 dv\u2082) \u2248 (v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082) :\u0394 \u03c4\u2082\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 rewrite v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082\n\n ext-\u0394[t\u2081t\u2082] :\n (\u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1))\n \u2248\n (\u27e6 t\u2081 \u27e7 (update \u03c1 {consistency}) (\u27e6 t\u2082 \u27e7 (update \u03c1 {consistency}))\n \u27e6\u229d\u27e7 \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n :\u0394 \u03c4\u2082\n ext-\u0394[t\u2081t\u2082] rewrite sym v\u2082=old-v\u2082 = dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082\n\ncorrectness-of-derive \u03c1 {consistency} _ = {!!}\n\ncorrectness-on-closed-terms : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192\n \u2200 (f : Term \u2205 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192\n \u2200 (s : Term \u2205 \u03c4\u2081) (ds : Term \u2205 (\u0394-Type \u03c4\u2081))\n {R[v,dv] : valid-\u0394 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)} \u2192\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205 \u27e6\u2295\u27e7 \u27e6 ds \u27e7 \u2205)\n \u2261\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)\n\ncorrectness-on-closed-terms {\u03c4\u2081} {\u03c4\u2082} f s ds {R[v,dv]} =\n begin\n h (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (f\u2295[g\u229df]=g h h))\n refl \u27e9\n (h \u27e6\u2295\u27e7 (h \u27e6\u229d\u27e7 h)) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (extract-\u0394equiv\n (correctness-of-derive \u2205 f)\n h\n (validity-of-derive \u2205 {d\u03c1=\u2205} f)\n (R[f,g\u229df] h h)))\n refl \u27e9\n (h \u27e6\u2295\u27e7 \u0394h) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 proj\u2082 (validity-of-derive \u2205 {d\u03c1=\u2205} f v dv R[v,dv]) \u27e9\n h v \u27e6\u2295\u27e7 \u0394h v dv\n \u220e\n where\n open \u2261-Reasoning\n h : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n h = \u27e6 f \u27e7 \u2205\n \u0394h : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n \u0394h = \u27e6 derive f \u27e7 \u2205\n v : \u27e6 \u03c4\u2081 \u27e7\n v = \u27e6 s \u27e7 \u2205\n dv : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv = \u27e6 ds \u27e7 \u2205\n\n","old_contents":"{-\nThe goal of this file is to make the 3rd example\ndescribed in \/examples.md, \"Map.mapValues\", fast:\n\n inc :: Bag Integer -> Bag Integer\n inc = map (+1)\n\n old = fromList [1, 2 .. n - 1, n]\n res = inc old = [2, 3 .. n, n + 1]\n\nTODO\n1. Make sure this file has no hole\n X. Replace \u2115 by \u2124\n 0. Replace \u2124 by \u2115 -- our bags are bags of nats now.\n 0. Introduce addition\n 0. Add MapBags and map\n 0. Figure out a way to communicate to a derivative that\n certain changes are always nil (in this case, `+1`).\n2. Finish ExplicitNils\n3. Consider appending ExplicitNils\n\n\nChecklist: Adding syntactic constructs\n\n- weaken\n- \u27e6_\u27e7Term\n- weaken-sound\n- derive (symbolic derivation)\n- validity-of-derive\n- correctness-of-derive\n\nChecklist: Adding types\n\n- \u27e6_\u27e7Type\n- \u0394-Type\n- \u27e6derive\u27e7\n- _\u27e6\u229d\u27e7_\n- _\u27e6\u2295\u27e7_\n- f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f\n- f\u2295[g\u229df]=g\n- f\u2295\u0394f=f\n- valid-\u0394\n- R[f,g\u229df]\n- df=f\u2295df\u229df\n- R (inside validity-of-derive)\n\n-}\n\nmodule NatBag where\n\nopen import Data.NatBag renaming\n (map to mapBag ; empty to emptyBag)\n\nopen import Data.NatBag.Properties using\n (b\\\\b=\u2205)\n\nopen import Data.Nat\n\nopen import Data.Unit using\n (\u22a4)\n\nimport Data.Integer as \u2124\n\nopen import Data.Product using\n (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\nopen import Relation.Binary.PropositionalEquality\n\nimport Level\nimport Data.Product as Product\n\npostulate extensionality : Extensionality Level.zero Level.zero\n\n\ndata Type : Set where\n nats : Type\n bags : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n bag : \u2200 {\u0393} \u2192 (b : Bag) \u2192 Term \u0393 bags\n add : \u2200 {\u0393} \u2192 (t\u2081 : Term \u0393 nats) \u2192 (t\u2082 : Term \u0393 nats) \u2192 Term \u0393 nats\n map : \u2200 {\u0393} \u2192 (f : Term \u0393 (nats \u21d2 nats)) \u2192 (b : Term \u0393 bags) \u2192 Term \u0393 bags\n\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n -- Change to nats = replacement Church pairs\n -- 3 -> 5 ::= \u03bbf. f 3 5\n\n -- Change to bags = a summand\n -- b\u2081 -> b\u2082 ::= b\u2082 \\\\ b\u2081\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\nweaken subctx (bag b) = bag b\nweaken subctx (add t\u2081 t\u2082) = add (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (map f b) = map (weaken subctx f) (weaken subctx b)\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 nats \u27e7Type = \u2115\n\u27e6 bags \u27e7Type = Bag\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\u27e6 bag b \u27e7Term \u03c1 = b\n\u27e6 add m n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 map f b \u27e7Term \u03c1 = mapBag (\u27e6 f \u27e7Term \u03c1) (\u27e6 b \u27e7Term \u03c1)\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n_\u27e8$\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\ninfix 0 _\u27e8$\u27e9_ -- infix 0 $ in Haskell\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = weaken-sound t\u2081 \u03c1 \u27e8$\u27e9 weaken-sound t\u2082 \u03c1\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (bag b) \u03c1 = refl\nweaken-sound (add m n) \u03c1 = cong\u2082 _+_ (weaken-sound m \u03c1) (weaken-sound n \u03c1)\nweaken-sound (map f b) \u03c1 = cong\u2082 mapBag (weaken-sound f \u03c1)\n (weaken-sound b \u03c1)\n\n-- Changes to \u2115 are replacement Church pairs. The only arguments\n-- of conern are `fst` and `snd`, so the Church pairs don't have\n-- to be polymorphic.\n--\n-- Changes on bags are bags. They allow negative multiplicities\n-- to begin with.\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type nats = (nats \u21d2 nats \u21d2 nats) \u21d2 nats\n\u0394-Type bags = bags\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\n-- It is clear that \u27e6\u229d\u27e7 exists on the semantic level:\n-- there exists an Agda value to describe the change between any\n-- two Agda values denoted by terms. If we have (not dependently-\n-- typed) arrays, no term denotes the change between two arrays\n-- of different lengths. Thus no full abstraction.\n\n\u27e6derive\u27e7 : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ninfixl 6 _\u27e6\u2295\u27e7_\ninfixl 6 _\u27e6\u229d\u27e7_\n\n\u27e6fst\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e6snd\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\n\u27e6derive\u27e7 {nats} n = \u03bb f \u2192 f n n\n\u27e6derive\u27e7 {bags} b = emptyBag\n\u27e6derive\u27e7 {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 f (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f v\n\n_\u27e6\u229d\u27e7_ {nats} m n = \u03bb f \u2192 f n m\n_\u27e6\u229d\u27e7_ {bags} b\u2081 b\u2082 = b\u2081 \\\\ b\u2082\n_\u27e6\u229d\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 f\u2081 (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f\u2082 v\n-- m \u27e6\u229d\u27e7 n ::= replace n by m\n\n_\u27e6\u2295\u27e7_ {nats} n dn = dn \u27e6snd\u27e7\n_\u27e6\u2295\u27e7_ {bags} b db = b ++ db\n_\u27e6\u2295\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n\n\u27e6fst\u27e7 m n = m\n\u27e6snd\u27e7 m n = n\n\n-- Cool lemmas\n\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u229d\u27e7 f \u2261 \u27e6derive\u27e7 f\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {nats} f = refl\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {bags} b = b\\\\b=\u2205\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = refl\n\nf\u2295[g\u229df]=g : \u2200 {\u03c4 : Type} (f g : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) \u2261 g\n\nf\u2295\u0394f=f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (\u27e6derive\u27e7 f) \u2261 f\n\nf\u2295[g\u229df]=g {nats} m n = refl\nf\u2295[g\u229df]=g {bags} b\u2081 b\u2082 = {!!}\nf\u2295[g\u229df]=g {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = f x} {y = f x} refl\n (cong\u2082 _\u27e6\u229d\u27e7_ (cong g (f\u2295\u0394f=f x)) refl) \u27e9\n f x \u27e6\u2295\u27e7 (g x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (g x) \u27e9\n g x\n \u220e\n ) where open \u2261-Reasoning\n\nf\u2295\u0394f=f {nats} n = refl\nf\u2295\u0394f=f {bags} b = {!!}\nf\u2295\u0394f=f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 \u27e6derive\u27e7 f) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (f (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong (\u03bb hole \u2192 f x \u27e6\u2295\u27e7 (f hole \u27e6\u229d\u27e7 f x)) (f\u2295\u0394f=f x) \u27e9\n f x \u27e6\u2295\u27e7 (f x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (f x) \u27e9\n f x\n \u220e\n ) where open \u2261-Reasoning\n{-\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {nats} n dn = n \u2261 dn \u27e6fst\u27e7\nvalid-\u0394 {bags} b db = {!!}\nvalid-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (s : \u27e6 \u03c4\u2081 \u27e7) (ds : \u27e6 \u0394-Type \u03c4\u2081 \u27e7) (R[s,ds] : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7 -- (valid-\u0394:1)\n (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 f s \u27e6\u2295\u27e7 df s ds -- (valid-\u0394:2)\n\nR[f,g\u229df] : \u2200 {\u03c4} (f g : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (g \u27e6\u229d\u27e7 f)\nR[f,g\u229df] {nats} m n = refl\nR[f,g\u229df] {bags} b\u2081 b\u2082 = {!!}\nR[f,g\u229df] {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb x dx R[x,dx] \u2192\n R[f,g\u229df] {\u03c4\u2082} (f x) (g (x \u27e6\u2295\u27e7 dx)) -- (valid-\u0394:1)\n , -- tuple constructor\n (begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 refl \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7\n (g ((x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 \u27e6derive\u27e7 (x \u27e6\u2295\u27e7 dx)) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 cong (\u03bb hole \u2192 f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g hole \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx)))\n (f\u2295\u0394f=f (x \u27e6\u2295\u27e7 dx)) \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 f\u2295[g\u229df]=g (f (x \u27e6\u2295\u27e7 dx)) (g (x \u27e6\u2295\u27e7 dx)) \u27e9\n g (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (f x) (g (x \u27e6\u2295\u27e7 dx))) \u27e9\n f x \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) x dx\n \u220e)\n where open \u2261-Reasoning\n\nR[f,\u0394f] : \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (\u27e6derive\u27e7 f)\nR[f,\u0394f] f rewrite sym (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f f) = R[f,g\u229df] f f\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393 -- push \u03c4, then push \u0394\u03c4.\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 (keep \u03c4 \u2022 \u0393\u227c\u0394\u0393)\n\n-- Data type to hold proofs that environments are valid\ndata Valid-\u0394env : {\u0393 : Context} (\u03c1 : \u27e6 \u0393 \u27e7) (\u0394\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is its own valid \u0394-environment.\n \u03c1=\u2205 : Valid-\u0394env {\u2205} \u2205 \u2205\n -- Induction case: the change introduced therein should be valid,\n -- and the smaller \u0394-environment should be valid as well.\n \u03c1=v\u2022\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {\u03c1\u2080 : \u27e6 \u0393\u2080 \u27e7} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Valid-\u0394env \u03c1\u2080 d\u03c1\u2080 \u2192\n Valid-\u0394env {\u03c4 \u2022 \u0393\u2080} (v \u2022 \u03c1\u2080) (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- Data type to hold proofs that a \u0394-env is consistent with self\ndata Consistent-\u0394env : {\u0393 : Context} (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is consistent with itself\n d\u03c1=\u2205 : Consistent-\u0394env {\u2205} \u2205\n -- Induction case: the change introduced at top level\n -- should be valid.\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Consistent-\u0394env d\u03c1\u2080 \u2192\n Consistent-\u0394env {\u03c4 \u2022 \u0393\u2080} (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- If a \u0394-environment is valid for some other environment,\n-- then it is also consistent with itself.\n\nvalid-\u0394env-is-consistent :\n \u2200 {\u0393 : Context} {\u03c1 : \u27e6 \u0393 \u27e7} {d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Valid-\u0394env \u03c1 d\u03c1 \u2192 Consistent-\u0394env d\u03c1\n\nvalid-\u0394env-is-consistent \u03c1=\u2205 = d\u03c1=\u2205\nvalid-\u0394env-is-consistent (\u03c1=v\u2022\u03c1\u2080 valid[v,dv] valid[\u03c1\u2080,d\u03c1\u2080]) =\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] (valid-\u0394env-is-consistent valid[\u03c1\u2080,d\u03c1\u2080])\n\n-- finally, update and ignore\n\nupdate : \u2200 {\u0393} \u2192 (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env d\u03c1} \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 {d\u03c1=\u2205} = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 d\u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] consistent[d\u03c1\u2080]} =\n (v \u27e6\u2295\u27e7 dv) \u2022 update d\u03c1 {consistent[d\u03c1\u2080]}\n\n-- Ignorance is bliss (don't have to pass consistency proofs around :D)\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 -- Using a proof to describe computation\n\n-- Naming scheme follows weakenVar\/weaken\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- derive(n) = \u03bbf. f n n\nderive (nat n) = abs (app (app (var this) (nat n)) (nat n))\n\n-- derive(x) = dx\nderive (var x) = var (deriveVar x)\n\n-- derive(\u03bbx. t) = \u03bbx. \u03bbdx. derive(t)\nderive (abs t) = abs (abs (derive t))\n\n-- derive(f s) = derive(f) s derive(s)\nderive (app f s) = app (app (derive f) (weaken \u0393\u227c\u0394\u0393 s)) (derive s)\n\nderive _ = {!!}\n\n-- Extensional equivalence for changes\ndata Extensionally-equivalent-as-changes :\n \u2200 (\u03c4 : Type) \u2192 (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 (dg : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n ext-\u0394 : \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n (\u2200 (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192\n (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)) \u2192\n Extensionally-equivalent-as-changes \u03c4 df dg\n\nsyntax Extensionally-equivalent-as-changes \u03c4 df dg = df \u2248 dg :\u0394 \u03c4\n\n-- Question: How to declare fixity for infix syntax?\n-- infix 4 _\u2248_:\u0394_ -- same as \u2261\n\n-- Extractor for extensional-equivalence-as-changes:\n-- Given a value of the data type holding the proof,\n-- returns the proof in applicable form.\n--\n-- It would not be necessary if such\n-- proof-holding types were defined as a function in\n-- the first place, say in the manner of `valid-\u0394`.\n--\nextract-\u0394equiv :\n \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n df \u2248 dg :\u0394 \u03c4 \u2192\n (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192 (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)\n\nextract-\u0394equiv (ext-\u0394 proof-method) = proof-method\n\n-- Distribution lemmas of validity over \u27e6\u229d\u27e7 and \u27e6\u2295\u27e7\n\napplication-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v) \u2261 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\napplication-over-\u2295-and-\u229d f g df v =\n begin\n f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (cong\u2082 _\u27e6\u2295\u27e7_\n (cong f ((sym (f\u2295\u0394f=f v))))\n (cong\u2082 df\n (sym (f\u2295\u0394f=f v))\n (cong \u27e6derive\u27e7 (sym (f\u2295\u0394f=f v)))))\n refl \u27e9\n f (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v)\n \u27e6\u2295\u27e7 df (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v) (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v))\n \u27e6\u229d\u27e7 f v\n \u2261\u27e8 refl \u27e9\n (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e where open \u2261-Reasoning\n\nvalidity-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n valid-\u0394 g (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) \u2192\n valid-\u0394 (g v) (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\nvalidity-over-\u2295-and-\u229d f g df v R[g,f\u2295df]\n rewrite application-over-\u2295-and-\u229d f g df v =\n proj\u2081 (R[g,f\u2295df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))\n\n-- diff-apply\ndf=f\u2295df\u229df :\n \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n valid-\u0394 f df \u2192 df \u2248 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) :\u0394 \u03c4\n\n-- Case nat: this REFL is more obvious to Agda than to a human.\ndf=f\u2295df\u229df {nats} n dn valid-n-dn =\n ext-\u0394 (\u03bb m valid-m-dn valid-rhs \u2192 refl)\n\ndf=f\u2295df\u229df {bags} b db valid-b-db = {!!}\n\ndf=f\u2295df\u229df {\u03c4\u2081 \u21d2 \u03c4\u2082} f df R[f,df] = ext-\u0394 (\n \u03bb g R[g,df] R[g,f\u2295df\u229df] \u2192\n extensionality (\u03bb v \u2192\n begin\n g v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n \u2261\u27e8 extract-\u0394equiv\n (df=f\u2295df\u229df\n (f v) (df v (\u27e6derive\u27e7 v))\n (proj\u2081 (R[f,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))))\n (g v)\n (proj\u2081 (R[g,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v)))\n (validity-over-\u2295-and-\u229d f g df v R[g,f\u2295df\u229df]) \u27e9\n g v \u27e6\u2295\u27e7 (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = g v} {y = g v} refl\n (application-over-\u2295-and-\u229d f g df v) \u27e9\n g v \u27e6\u2295\u27e7 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e\n )\n )\n where open \u2261-Reasoning\n\n\n\ncorrectness-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n (\u27e6 deriveVar x \u27e7 \u03c1)\n \u2248\n (\u27e6 x \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 x \u27e7 (ignore \u03c1)) :\u0394 \u03c4\n\ncorrectness-of-deriveVar {\u03c4 \u2022 \u0393\u2080} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n this = df=f\u2295df\u229df {\u03c4} v dv R[v,dv]\n\ncorrectness-of-deriveVar {\u03c4\u2080 \u2022 \u0393\u2080} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4\u2080} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n (that x) = correctness-of-deriveVar \u03c1 x\n\n\n\ncorrectness-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n\n (\u27e6 derive t \u27e7 \u03c1)\n \u2248 (\u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 t \u27e7 (ignore \u03c1)) :\u0394 \u03c4\n\n-- Mutually recursive lemma: derivatives are valid\nvalidity-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 deriveVar x \u27e7 \u03c1)\n\nvalidity-of-deriveVar {\u03c4 \u2022 \u0393} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n this = R[v,dv]\n\nvalidity-of-deriveVar {\u03c4\u2080 \u2022 \u0393} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n (that x) = validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 {consistency} (var x) =\n validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 (nat n) = refl\n\nvalidity-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs t) = \u03bb v dv R[v,dv] \u2192\n validity-of-derive (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency} t\n , -- tuple constructor\n (begin\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n ((R[f,g\u229df] (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 f\u2295[g\u229df]=g (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u2261\u27e8 cong \u27e6 t \u27e7 (cong\u2082 _\u2022_ (f\u2295\u0394f=f (v \u27e6\u2295\u27e7 dv)) refl) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency}))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 sym (extract-\u0394equiv\n (correctness-of-derive\n ((dv \u2022 v \u2022 \u03c1))\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (R[f,g\u229df] (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u220e)\n where open \u2261-Reasoning\n\nvalidity-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} t\u2081 t\u2082)\n = R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7]\n where\n open \u2261-Reasoning\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 : \u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1) \u2261 v\u2081 v\u2082\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 rewrite (sym v\u2082=old-v\u2082) = refl\n\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 : \u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1 \u2261 dv\u2081 v\u2082 dv\u2082\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = refl\n\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] : valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (dv\u2081 v\u2082 dv\u2082)\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] rewrite \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 = R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n -- What I want to write:\n {-\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] rewrite \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n -}\n\n -- What I have to write:\n\n R : {\u03c4 : Type} \u2192 {v : \u27e6 \u03c4 \u27e7} \u2192 {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 valid-\u0394 v dv\u2081 \u2192 valid-\u0394 v dv\u2082\n\n R {nats} dv\u2081=dv\u2082 refl = cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl\n\n R {bags} dv\u2081=dv\u2082 _ = {!!}\n\n --R {\u03c4\u2081 \u21d2 \u03c4\u2082} dv\u2081=dv\u2082 valid-dv\u2081 rewrite dv\u2081=dv\u2082 = {!valid-dv\u2081!}\n R {\u03c4\u2081 \u21d2 \u03c4\u2082} {v} {dv\u2081} {dv\u2082} dv\u2081=dv\u2082 valid-dv\u2081 =\n \u03bb s ds R[s,ds] \u2192\n R {\u03c4\u2082} {v s} {dv\u2081 s ds} {dv\u2082 s ds}\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl)\n (proj\u2081 (valid-dv\u2081 s ds R[s,ds]))\n ,\n (begin\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2082 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v (s \u27e6\u2295\u27e7 ds)} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) (sym dv\u2081=dv\u2082) refl) refl) \u27e9\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2081 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 sym (proj\u2082 (valid-dv\u2081\n (s \u27e6\u2295\u27e7 ds)\n (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n (R[f,\u0394f] (s \u27e6\u2295\u27e7 ds)))) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds \u27e6\u2295\u27e7 \u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong (v \u27e6\u2295\u27e7 dv\u2081) (f\u2295\u0394f=f (s \u27e6\u2295\u27e7 ds)) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds)\n \u2261\u27e8 proj\u2082 (valid-dv\u2081 s ds R[s,ds]) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2081 s ds\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v s} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2082 s ds\n \u220e) where open \u2261-Reasoning\n\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] = R \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n\nvalidity-of-derive \u03c1 {consistency} _ = {!!}\n\ncorrectness-of-derive \u03c1 (var x) = correctness-of-deriveVar \u03c1 x\n\ncorrectness-of-derive \u03c1 (nat n) = ext-\u0394 (\u03bb _ _ _ \u2192 refl)\n\ncorrectness-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs {.\u03c4\u2081} {.\u03c4\u2082} t) =\n ext-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n extensionality {\u27e6 \u03c4\u2081 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4\u2082 \u27e7} (\u03bb x \u2192\n begin\n f x \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive {\u03c4\u2081 \u2022 \u0393} {\u03c4\u2082}\n (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency}\n t)\n (f x)\n (proj\u2081 (R[f,\u0394t] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x)))\n (proj\u2081 (R[f,t\u2032\u229dt] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x))) \u27e9\n f x\n \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 (update (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency})\n \u27e6\u229d\u27e7 \u27e6 t \u27e7 (x \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u220e\n )) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} {\u0393} t\u2081 t\u2082) =\n ext-\u0394 {\u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n begin\n f \u27e6\u2295\u27e7 \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n \u2261\u27e8 extract-\u0394equiv ext-\u0394[t\u2081t\u2082] f R[f,\u0394t] R[f,t\u2032\u229dt] \u27e9\n f \u27e6\u2295\u27e7\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n \u220e)\n where\n open \u2261-Reasoning\n\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n v\u2081\u2295dv\u2081=v\u2081\u2032 : v\u2081 \u27e6\u2295\u27e7 dv\u2081 \u2261 v\u2081\u2032\n v\u2081\u2295dv\u2081=v\u2081\u2032 =\n begin\n v\u2081 \u27e6\u2295\u27e7 dv\u2081\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2081)\n v\u2081 valid-dv\u2081 (R[f,g\u229df] v\u2081 v\u2081\u2032) \u27e9\n v\u2081 \u27e6\u2295\u27e7 (v\u2081\u2032 \u27e6\u229d\u27e7 v\u2081)\n \u2261\u27e8 f\u2295[g\u229df]=g v\u2081 v\u2081\u2032 \u27e9\n v\u2081\u2032\n \u220e\n\n -- TODO: remove code duplication.\n v\u2082\u2295dv\u2082=v\u2082\u2032 : v\u2082 \u27e6\u2295\u27e7 dv\u2082 \u2261 v\u2082\u2032\n v\u2082\u2295dv\u2082=v\u2082\u2032 rewrite v\u2082=old-v\u2082 =\n begin\n old-v\u2082 \u27e6\u2295\u27e7 dv\u2082\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2082)\n old-v\u2082\n (validity-of-derive \u03c1 {consistency} t\u2082)\n (R[f,g\u229df] old-v\u2082 v\u2082\u2032) \u27e9\n old-v\u2082 \u27e6\u2295\u27e7 (v\u2082\u2032 \u27e6\u229d\u27e7 old-v\u2082)\n \u2261\u27e8 f\u2295[g\u229df]=g old-v\u2082 v\u2082\u2032 \u27e9\n v\u2082\u2032\n \u220e\n where old-v\u2082 = \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] : v\u2081\u2032 v\u2082\u2032 \u2261 (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082)\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] = sym (cong\u2082 (\u03bb f x \u2192 f x) v\u2081\u2295dv\u2081=v\u2081\u2032 v\u2082\u2295dv\u2082=v\u2082\u2032)\n\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 :\n (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082) \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 = proj\u2082 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082 \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n cong\u2082 _\u27e6\u229d\u27e7_\n (trans v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082)\n refl\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n (dv\u2081 v\u2082 dv\u2082) \u2248 (v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082) :\u0394 \u03c4\u2082\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n df=f\u2295df\u229df (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082) R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 : (dv\u2081 v\u2082 dv\u2082) \u2248 (v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082) :\u0394 \u03c4\u2082\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 rewrite v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082\n\n ext-\u0394[t\u2081t\u2082] :\n (\u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1))\n \u2248\n (\u27e6 t\u2081 \u27e7 (update \u03c1 {consistency}) (\u27e6 t\u2082 \u27e7 (update \u03c1 {consistency}))\n \u27e6\u229d\u27e7 \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n :\u0394 \u03c4\u2082\n ext-\u0394[t\u2081t\u2082] rewrite sym v\u2082=old-v\u2082 = dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082\n\ncorrectness-of-derive \u03c1 {consistency} _ = {!!}\n\ncorrectness-on-closed-terms : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192\n \u2200 (f : Term \u2205 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192\n \u2200 (s : Term \u2205 \u03c4\u2081) (ds : Term \u2205 (\u0394-Type \u03c4\u2081))\n {R[v,dv] : valid-\u0394 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)} \u2192\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205 \u27e6\u2295\u27e7 \u27e6 ds \u27e7 \u2205)\n \u2261\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)\n\ncorrectness-on-closed-terms {\u03c4\u2081} {\u03c4\u2082} f s ds {R[v,dv]} =\n begin\n h (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (f\u2295[g\u229df]=g h h))\n refl \u27e9\n (h \u27e6\u2295\u27e7 (h \u27e6\u229d\u27e7 h)) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (extract-\u0394equiv\n (correctness-of-derive \u2205 f)\n h\n (validity-of-derive \u2205 {d\u03c1=\u2205} f)\n (R[f,g\u229df] h h)))\n refl \u27e9\n (h \u27e6\u2295\u27e7 \u0394h) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 proj\u2082 (validity-of-derive \u2205 {d\u03c1=\u2205} f v dv R[v,dv]) \u27e9\n h v \u27e6\u2295\u27e7 \u0394h v dv\n \u220e\n where\n open \u2261-Reasoning\n h : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n h = \u27e6 f \u27e7 \u2205\n \u0394h : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n \u0394h = \u27e6 derive f \u27e7 \u2205\n v : \u27e6 \u03c4\u2081 \u27e7\n v = \u27e6 s \u27e7 \u2205\n dv : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv = \u27e6 ds \u27e7 \u2205\n\n-}\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"bc8f73c86b96f119aff939fd38aa12d023ebc02e","subject":"Checkpoint: To merge alter\/abide to one constructor with Bool tag. Reason: Otherwise impossible to express \"nonempty \u0394-context\" as a type.","message":"Checkpoint: To merge alter\/abide to one constructor with Bool tag.\nReason: Otherwise impossible to express \"nonempty \u0394-context\" as\na type.\n\nOld-commit-hash: 67e1406b2769044d22b7ac779f8da7e8eea1b5f2\n","repos":"inc-lc\/ilc-agda","old_file":"experimental\/TaggedDeltaTypes.agda","new_file":"experimental\/TaggedDeltaTypes.agda","new_contents":"{-\nThe goal of this file is to make the domain of \u0394 types smaller\nso as to be nearer to full abstraction and hopefully close\nenough for the purpose of having explicit nil changes.\n-}\n\nmodule TaggedDeltaTypes where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Nat\nopen import Data.Bool\nopen import Data.Unit using (\u22a4 ; tt)\nimport Data.Integer as \u2124\nimport Data.Product as Product\nopen import Data.Product using (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\nimport Level\n\n-- Postulates: Extensionality and bag properties (#55)\npostulate extensionality : Extensionality Level.zero Level.zero\n--\n-- open import Data.NatBag renaming\n-- (map to mapBag ; empty to emptyBag ; update to updateBag)\n-- open import Data.NatBag.Properties\npostulate Bag : Set\npostulate emptyBag : Bag\npostulate mapBag : (\u2115 \u2192 \u2115) \u2192 Bag \u2192 Bag\npostulate _++_ : Bag \u2192 Bag \u2192 Bag\npostulate _\\\\_ : Bag \u2192 Bag \u2192 Bag\ninfixr 5 _++_\ninfixl 9 _\\\\_\npostulate b\\\\b=\u2205 : \u2200 {b : Bag} \u2192 b \\\\ b \u2261 emptyBag\npostulate b++\u2205=b : \u2200 {b : Bag} \u2192 b ++ emptyBag \u2261 b\npostulate \u2205++b=b : \u2200 {b : Bag} \u2192 emptyBag ++ b \u2261 b\npostulate b++[d\\\\b]=d : \u2200 {b d} \u2192 b ++ (d \\\\ b) \u2261 d\npostulate [b++d]\\\\b=d : \u2200 {b d} \u2192 (b ++ d) \\\\ b \u2261 d\npostulate\n [a++b]\\\\[c++d]=[a\\\\c]++[b\\\\d] : \u2200 {a b c d} \u2192\n (a ++ b) \\\\ (c ++ d) \u2261 (a \\\\ c) ++ (b \\\\ d)\npostulate\n [a\\\\b]\\\\[c\\\\d]=[a\\\\c]\\\\[b\\\\d] : \u2200 {a b c d} \u2192\n (a \\\\ b) \\\\ (c \\\\ d) \u2261 (a \\\\ c) \\\\ (b \\\\ d)\n\n------------------------\n-- Syntax of programs --\n------------------------\n\ndata Type : Set where\n nats : Type\n bags : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u03c4 \u0393} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u03c4 \u03c4\u2032 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n bag : \u2200 {\u0393} \u2192 (b : Bag) \u2192 Term \u0393 bags\n\n var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (s : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n add : \u2200 {\u0393} \u2192 (s : Term \u0393 nats) \u2192 (t : Term \u0393 nats) \u2192 Term \u0393 nats\n map : \u2200 {\u0393} \u2192 (f : Term \u0393 (nats \u21d2 nats)) \u2192 (b : Term \u0393 bags) \u2192 Term \u0393 bags\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u0393\u227c\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0393\n\u0393\u227c\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0393 {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u0393\u227c\u0393 {\u0393}\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\nweaken subctx (bag b) = bag b\nweaken subctx (add t\u2081 t\u2082) = add (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (map f b) = map (weaken subctx f) (weaken subctx b)\n\n---------------------------\n-- Semantics of programs --\n---------------------------\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 nats \u27e7Type = \u2115\n\u27e6 bags \u27e7Type = Bag\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nidentity-weakening : \u2200 {\u0393} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192 \u27e6 \u0393\u227c\u0393 {\u0393} \u27e7 \u03c1 \u2261 \u03c1\nidentity-weakening {\u2205} {\u2205} = refl\nidentity-weakening {\u03c4 \u2022 \u0393} {v \u2022 \u03c1} =\n cong\u2082 _\u2022_ {x = v} refl (identity-weakening {\u0393} {\u03c1})\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\u27e6 bag b \u27e7Term \u03c1 = b\n\u27e6 add m n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 map f b \u27e7Term \u03c1 = mapBag (\u27e6 f \u27e7Term \u03c1) (\u27e6 b \u27e7Term \u03c1)\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n_\u27e8$\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\n\n-- infix 0 $ in Haskell\ninfixl 0 _\u27e8$\u27e9_\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = weaken-sound t\u2081 \u03c1 \u27e8$\u27e9 weaken-sound t\u2082 \u03c1\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (bag b) \u03c1 = refl\nweaken-sound (add m n) \u03c1 = cong\u2082 _+_ (weaken-sound m \u03c1) (weaken-sound n \u03c1)\nweaken-sound (map f b) \u03c1 =\n cong\u2082 mapBag (weaken-sound f \u03c1) (weaken-sound b \u03c1)\n\n----------------------------------------------------------\n-- Syntax and semantics of changes (they are entangled) --\n----------------------------------------------------------\n\ndata \u0394-Type : Set where\n nats : \u0394-Type\n bags : \u0394-Type\n alter_\u21d2_ : \u0394-Type \u2192 \u0394-Type \u2192 \u0394-Type\n abide_\u21d2_ : \u0394-Type \u2192 \u0394-Type \u2192 \u0394-Type\n\ndata \u0394-Context : Set where\n \u2205 : \u0394-Context\n alter_\u2022_ : \u0394-Type \u2192 \u0394-Context \u2192 \u0394-Context\n abide_\u2022_ : \u0394-Type \u2192 \u0394-Context \u2192 \u0394-Context\n\nerase : \u0394-Type \u2192 Type\nerase nats = nats\nerase bags = bags\nerase (alter \u03c4\u2081 \u21d2 \u03c4\u2082) = erase \u03c4\u2081 \u21d2 erase \u03c4\u2082\nerase (abide \u03c4\u2081 \u21d2 \u03c4\u2082) = erase \u03c4\u2081 \u21d2 erase \u03c4\u2082\n\nforget : \u0394-Context \u2192 Context\nforget \u2205 = \u2205\nforget (alter \u03c4 \u2022 \u03c1) = erase \u03c4 \u2022 forget \u03c1\nforget (abide \u03c4 \u2022 \u03c1) = erase \u03c4 \u2022 forget \u03c1\n\n-- Convert a type\/context to a \u0394-type\/\u0394-context without any\n-- assumption about arguments\n\n\u0394-type : Type \u2192 \u0394-Type\n\u0394-type nats = nats\n\u0394-type bags = bags\n\u0394-type (\u03c4\u2081 \u21d2 \u03c4\u2082) = alter \u0394-type \u03c4\u2081 \u21d2 \u0394-type \u03c4\u2082\n\n\u0394-context : Context \u2192 \u0394-Context\n\u0394-context \u2205 = \u2205\n\u0394-context (\u03c4 \u2022 \u0393) = alter \u0394-type \u03c4 \u2022 \u0394-context \u0393\n\ndata \u0394-Term : \u0394-Context \u2192 \u0394-Type \u2192 Set\ndata \u27e6_\u27e7\u0394Context : \u0394-Context \u2192 Set\n\n\u27e6_\u27e7\u0394Type : \u0394-Type \u2192 Set\n\u27e6_\u27e7\u0394Term : \u2200 {\u03c4 \u0393} \u2192 \u0394-Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7\u0394Context \u2192 \u27e6 \u03c4 \u27e7\u0394Type\n_\u2295_ : \u2200 {\u03c4} \u2192 \u27e6 erase \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\u0394Type \u2192 \u27e6 erase \u03c4 \u27e7\n_\u229d_ : \u2200 {\u03c4} \u2192 \u27e6 erase \u03c4 \u27e7 \u2192 \u27e6 erase \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\u0394Type\nvalid : \u2200 {\u03c4} \u2192 \u27e6 erase \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\u0394Type \u2192 Set\nR[v,u\u229dv] : \u2200 {\u03c4} {u v : \u27e6 erase \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229d v)\nv\u2295[u\u229dv]=u : \u2200 {\u03c4} {u v : \u27e6 erase \u03c4 \u27e7} \u2192 v \u2295 (_\u229d_ {\u03c4} u v) \u2261 u\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7\u0394Context \u2192 \u27e6 forget \u0393 \u27e7\nupdate : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7\u0394Context \u2192 \u27e6 forget \u0393 \u27e7\ninfixl 6 _\u2295_ _\u229d_ -- as with + - in GHC.Num\n\ndata \u0394-Term where\n -- changes to numbers are replacement pairs\n \u0394nat : \u2200 {\u0393} \u2192 (old : \u2115) \u2192 (new : \u2115) \u2192 \u0394-Term \u0393 nats\n -- changes to bags are bags\n \u0394bag : \u2200 {\u0393} \u2192 (db : Bag) \u2192 \u0394-Term \u0393 bags\n -- changes to variables are variables\n \u0394var : \u2200 {\u03c4 \u0393} \u2192 (x : Var (forget \u0393) (erase \u03c4)) \u2192 \u0394-Term \u0393 \u03c4\n -- changes to abstractions are binders of x and dx\n -- There are two kinds of those: One who expects the argument\n -- to change, and one who does not.\n \u0394abs\u2080 : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393}\n (t : \u0394-Term (abide \u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192\n \u0394-Term \u0393 (abide \u03c4\u2081 \u21d2 \u03c4\u2082)\n \u0394abs\u2081 : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393}\n (t : \u0394-Term (alter \u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192\n \u0394-Term \u0393 (alter \u03c4\u2081 \u21d2 \u03c4\u2082)\n{-\n \u0394app\u2080 : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393}\n (ds : \u0394-Term \u0393 (abide \u03c4\u2081 \u21d2 \u03c4\u2082))\n (t : Term (forget \u0393) (erase \u03c4\u2081))\n (dt : \u0394-Term \u0393 \u03c4\u2081)\n {R[t,dt] : \u2200 {\u03c1 : \u27e6\n-}\n \n{-\n -- changes to applications are applications of a value and a change\n \u0394app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393 vars}\n (ds : \u0394-Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082) {vars})\n ( t : Term \u0393 \u03c4\u2081)\n (dt : \u0394-Term \u0393 \u03c4\u2081 {vars})\n (R[t,dt] : {\u03c1 : \u0394-Env \u0393 {-vars-}} \u2192\n valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394Term \u03c1)) \u2192\n \u0394-Term \u0393 \u03c4\u2082 {vars}\n -- changes to addition are changes to their components\n \u0394add : \u2200 {\u0393 vars}\n (ds : \u0394-Term \u0393 nats {vars})\n (dt : \u0394-Term \u0393 nats {vars}) \u2192\n \u0394-Term \u0393 nats {vars}\n -- There are two kinds of changes to maps:\n -- 0. recomputation,\n -- 1. mapping over changes,\n -- the latter used only with some form of isNil available.\n \u0394map\u2080 : \u2200 {\u0393 vars} \u2192\n ( f : Term \u0393 (nats \u21d2 nats))\n (df : \u0394-Term \u0393 (nats \u21d2 nats) {vars})\n ( b : Term \u0393 bags)\n (db : \u0394-Term \u0393 bags {vars}) \u2192\n \u0394-Term \u0393 bags {vars}\n \u0394map\u2081 : \u2200 {\u0393 vars} \u2192\n ( f : Term \u0393 (nats \u21d2 nats)) (db : \u0394-Term \u0393 bags {vars}) \u2192\n \u0394-Term \u0393 bags {vars}\n-}\n\n-- \u27e6_\u27e7\u0394Type : \u0394-Type \u2192 Set\n\u27e6 nats \u27e7\u0394Type = \u2115 \u00d7 \u2115\n\u27e6 bags \u27e7\u0394Type = Bag\n\u27e6 alter \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\u0394Type =\n (v : \u27e6 erase \u03c4\u2081 \u27e7) \u2192 (dv : \u27e6 \u03c4\u2081 \u27e7\u0394Type) \u2192\n valid {\u03c4\u2081} v dv \u2192\n \u27e6 \u03c4\u2082 \u27e7\u0394Type\n\u27e6 abide \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\u0394Type =\n (v : \u27e6 erase \u03c4\u2081 \u27e7) \u2192 (dv : \u27e6 \u03c4\u2081 \u27e7\u0394Type) \u2192\n valid {\u03c4\u2081} v dv \u2192 _\u2295_ {\u03c4\u2081} v dv \u2261 v \u2192\n \u27e6 \u03c4\u2082 \u27e7\u0394Type\n\nmeaning-\u0394Type : Meaning \u0394-Type\nmeaning-\u0394Type = meaning \u27e6_\u27e7\u0394Type\n\n\n-- _\u2295_ : \u2200 {\u03c4} \u2192 \u27e6 erase \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\u0394Type \u2192 \u27e6 erase \u03c4 \u27e7\n_\u2295_ = {!!}\n{-\n_\u2295_ {nats} n dn = proj\u2082 dn\n_\u2295_ {bags} b db = b ++ db\n_\u2295_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u2295 df v (v \u229d v) R[v,u\u229dv]\n-}\n\n-- _\u229d_ : \u2200 {\u03c4} \u2192 \u27e6 erase \u03c4 \u27e7 \u2192 \u27e6 erase \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\u0394Type\n_\u229d_ = {!!}\n{-\n_\u229d_ {nats} m n = (n , m)\n_\u229d_ {bags} b d = b \\\\ d\n_\u229d_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb v dv R[v,dv] \u2192 f (v \u2295 dv) \u229d g v\n-}\n\n-- valid : \u2200 {\u03c4} \u2192 \u27e6 erase \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\u0394Type \u2192 Set\nvalid _ _ = \u22a4\n{-\nvalid {nats} n dn = n \u2261 proj\u2081 dn\nvalid {bags} b db = \u22a4\nvalid {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (v : \u27e6 \u03c4\u2081 \u27e7) (dv : \u27e6 \u03c4\u2081 \u27e7\u0394Type) (R[v,dv] : valid v dv)\n \u2192 valid (f v) (df v dv R[v,dv])\n \u00d7 (f \u2295 df) (v \u2295 dv) \u2261 f v \u2295 df v dv R[v,dv]\n-}\n\n-- v\u2295[u\u229dv]=u : \u2200 {\u03c4} {u v : \u27e6 erase \u03c4 \u27e7} \u2192 v \u2295 (_\u229d_ {\u03c4} u v) \u2261 u\nv\u2295[u\u229dv]=u = {!!}\n{-\nv\u2295[u\u229dv]=u {nats} {u} {v} = refl\nv\u2295[u\u229dv]=u {bags} {u} {v} = b++[d\\\\b]=d {v} {u}\nv\u2295[u\u229dv]=u {\u03c4\u2081 \u21d2 \u03c4\u2082} {u} {v} = extensionality (\u03bb w \u2192\n begin\n (v \u2295 (u \u229d v)) w\n \u2261\u27e8 refl \u27e9 -- for clarity\n v w \u2295 (u (w \u2295 (w \u229d w)) \u229d v w)\n \u2261\u27e8 cong (\u03bb hole \u2192 v w \u2295 (u hole \u229d v w)) v\u2295[u\u229dv]=u \u27e9\n v w \u2295 (u w \u229d v w)\n \u2261\u27e8 v\u2295[u\u229dv]=u \u27e9\n u w\n \u220e) where open \u2261-Reasoning\n-}\n\n-- R[v,u\u229dv] : \u2200 {\u03c4} {u v : \u27e6 erase \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229d v)\nR[v,u\u229dv] = tt\n{-\nR[v,u\u229dv] {nats} {u} {v} = refl\nR[v,u\u229dv] {bags} {u} {v} = tt\nR[v,u\u229dv] {\u03c4\u2081 \u21d2 \u03c4\u2082} {u} {v} = \u03bb w dw R[w,dw] \u2192\n let\n w\u2032 = w \u2295 dw\n in\n R[v,u\u229dv] -- NOT a self recursion: implicit arguments are different.\n ,\n (begin\n (v \u2295 (u \u229d v)) w\u2032\n \u2261\u27e8 cong (\u03bb hole \u2192 hole w\u2032) (v\u2295[u\u229dv]=u {u = u} {v}) \u27e9\n u w\u2032\n \u2261\u27e8 sym (v\u2295[u\u229dv]=u {u = u w\u2032} {v w}) \u27e9\n v w \u2295 (u \u229d v) w dw R[w,dw]\n \u220e) where open \u2261-Reasoning\n-}\n\n\n-- The type of environments ensures their consistency and honesty.\n-- data \u27e6_\u27e7\u0394Context : \u0394-Context \u2192 Set\ndata \u27e6_\u27e7\u0394Context where\n \u2205 : \u27e6 \u2205 \u27e7\u0394Context\n quad : \u2200 {\u03c4 \u0393}\n (v : \u27e6 erase \u03c4 \u27e7)\n (dv : \u27e6 \u03c4 \u27e7\u0394Type)\n (R[v,dv] : valid {\u03c4} v dv)\n (\u03c1 : \u27e6 \u0393 \u27e7\u0394Context) \u2192\n \u27e6 alter \u03c4 \u2022 \u0393 \u27e7\u0394Context\n quint : \u2200 {\u03c4 \u0393}\n (v : \u27e6 erase \u03c4 \u27e7)\n (dv : \u27e6 \u03c4 \u27e7\u0394Type)\n (R[v,dv] : valid {\u03c4} v dv)\n (v\u2295dv=v : _\u2295_ {\u03c4} v dv \u2261 v)\n (\u03c1 : \u27e6 \u0393 \u27e7\u0394Context) \u2192\n \u27e6 abide \u03c4 \u2022 \u0393 \u27e7\u0394Context\n\nmeaning-\u0394Context : Meaning \u0394-Context\nmeaning-\u0394Context = meaning \u27e6_\u27e7\u0394Context\n\n\n-- ignore : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7\u0394Context \u2192 \u27e6 forget \u0393 \u27e7\nignore \u2205 = \u2205\nignore (quad v dv R[v,dv] \u03c1) = v \u2022 ignore \u03c1\nignore (quint v dv R[v,dv] v\u2295dv=v \u03c1) = v \u2022 ignore \u03c1\n\n-- update : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7\u0394Context \u2192 \u27e6 forget \u0393 \u27e7\nupdate \u2205 = \u2205\nupdate (quad {\u03c4} v dv R[v,dv] \u03c1) = (_\u2295_ {\u03c4} v dv) \u2022 update \u03c1\nupdate (quint {\u03c4} v dv R[v,dv] v\u2295dv=v \u03c1) = (_\u2295_ {\u03c4} v dv) \u2022 update \u03c1\n\n--\u27e6_\u27e7\u0394Var : \u2200 {\u03c4} {\u0393} \u2192 Var (forget \u0393) (erase \u03c4) \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\n{-\n\u27e6_\u27e7\u0394Var {alter \u03c4 \u2022 _} {.\u03c4} this (quad _ dv _ _) = dv\n\u27e6_\u27e7\u0394Var {abide \u03c4 \u2022 _} {.\u03c4} this (quint _ dv _ _ _) = dv\n\u27e6_\u27e7\u0394Var {alter _ \u2022 _} (that x) (quad _ _ _ \u03c1) = \u27e6 x \u27e7\u0394Var \u03c1\n\u27e6_\u27e7\u0394Var {abide _ \u2022 _} (that x) (quint _ _ _ _ \u03c1) = \u27e6 x \u27e7\u0394Var \u03c1\n-}\n\n\n-- \u27e6_\u27e7\u0394Term : \u2200 {\u03c4 \u0393} \u2192 \u0394-Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7\u0394Context \u2192 \u27e6 \u03c4 \u27e7\u0394Type\n\u27e6_\u27e7\u0394Term _ _ = {!!}\n{-\n\u27e6 \u0394nat old new \u27e7\u0394Term \u03c1 = (old , new)\n\u27e6 \u0394bag db \u27e7\u0394Term \u03c1 = db\n\u27e6 \u0394var x \u27e7\u0394Term \u03c1 = \u27e6 x \u27e7\u0394Var \u03c1\n\u27e6 \u0394abs t \u27e7\u0394Term \u03c1 =\n \u03bb v dv R[v,dv] \u2192 \u27e6 t \u27e7\u0394Term (alter v dv R[v,dv] \u03c1)\n\u27e6 \u0394abs\u2080 t \u27e7\u0394Term \u03c1 =\n \u03bb v dv R[v,dv] {-v\u2295dv=v-} \u2192 \u27e6 t \u27e7\u0394Term (abide v dv R[v,dv] {!v\u2295dv=v!} \u03c1)\n\u27e6 \u0394app ds t dt R[dt,t] \u27e7\u0394Term \u03c1 =\n \u27e6 ds \u27e7\u0394Term \u03c1 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394Term \u03c1) R[dt,t]\n --(R[dt,t] {\u03c1} {honesty = {!!}})\n\u27e6 \u0394add ds dt \u27e7\u0394Term \u03c1 =\n let\n (old-s , new-s) = \u27e6 ds \u27e7\u0394Term \u03c1\n (old-t , new-t) = \u27e6 dt \u27e7\u0394Term \u03c1\n in\n (old-s + old-t , new-s + new-t)\n\u27e6 \u0394map\u2080 f df b db \u27e7\u0394Term \u03c1 =\n let\n v = \u27e6 b \u27e7 (ignore \u03c1)\n h = \u27e6 f \u27e7 (ignore \u03c1)\n dv = \u27e6 db \u27e7\u0394Term \u03c1\n dh = \u27e6 df \u27e7\u0394Term \u03c1\n in\n mapBag (h \u2295 dh) (v \u2295 dv) \\\\ mapBag h v\n\u27e6 \u0394map\u2081 f db \u27e7\u0394Term \u03c1 = mapBag (\u27e6 f \u27e7 (ignore \u03c1)) (\u27e6 db \u27e7\u0394Term \u03c1)\n-}\n\nmeaning-\u0394Term : \u2200 {\u03c4 \u0393} \u2192 Meaning (\u0394-Term \u0393 \u03c4)\nmeaning-\u0394Term = meaning \u27e6_\u27e7\u0394Term\n\n\n\n{-\n--------------------------------------------------------\n-- Program transformation and correctness (entangled) --\n--------------------------------------------------------\n\nderive : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394-Term \u0393 \u03c4\n\nvalidity : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394-Env \u0393} \u2192\n valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\ncorrectness : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394-Env \u0393} \u2192\n \u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (update \u03c1)\n\n-- derive : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394-Term \u0393 \u03c4\nderive (nat n) = \u0394nat n n\nderive (bag b) = \u0394bag emptyBag\nderive (var x) = \u0394var x\nderive (abs t) = \u0394abs (derive t)\nderive (app s t) = \u0394app (derive s) t (derive t) validity\nderive (add s t) = \u0394add (derive s) (derive t)\nderive (map f b) = \u0394map\u2080 f (derive f) b (derive b)\n\nvalidity-var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192\n \u2200 {\u03c1 : \u0394-Env \u0393} \u2192 valid (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 x \u27e7\u0394Var \u03c1)\n\nvalidity-var this {cons v dv R[v,dv] \u03c1} = R[v,dv]\nvalidity-var (that x) {cons v dv R[v,dv] \u03c1} = validity-var x\n\nvalidity {t = nat n} = refl\nvalidity {t = bag b} = tt\nvalidity {t = var x} = validity-var x\nvalidity {t = map f b} = tt\nvalidity {t = add s t} = cong\u2082 _+_ (validity {t = s}) (validity {t = t})\n\nvalidity {t = app s t} {\u03c1} =\n let\n v = \u27e6 t \u27e7 (ignore \u03c1)\n dv = \u27e6 derive t \u27e7 \u03c1\n in\n proj\u2081 (validity {t = s} {\u03c1} v dv (validity {t = t} {\u03c1}))\n\nvalidity {t = abs t} {\u03c1} = \u03bb v dv R[v,dv] \u2192\n let\n v\u2032 = v \u2295 dv\n dv\u2032 = v\u2032 \u229d v\u2032\n \u03c1\u2081 = cons v dv R[v,dv] \u03c1\n \u03c1\u2082 = cons v\u2032 dv\u2032 R[v,u\u229dv] \u03c1\n in\n validity {t = t} {\u03c1\u2081}\n ,\n (begin\n \u27e6 t \u27e7 (ignore \u03c1\u2082) \u2295 \u27e6 derive t \u27e7 \u03c1\u2082\n \u2261\u27e8 correctness {t = t} {\u03c1\u2082} \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2082)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 (hole \u2022 update \u03c1)) v\u2295[u\u229dv]=u \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2081)\n \u2261\u27e8 sym (correctness {t = t} {\u03c1\u2081}) \u27e9\n \u27e6 t \u27e7 (ignore \u03c1\u2081) \u2295 \u27e6 derive t \u27e7 \u03c1\u2081\n \u220e) where open \u2261-Reasoning\n\ncorrectVar : \u2200 {\u03c4 \u0393} {x : Var \u0393 \u03c4} {\u03c1 : \u0394-Env \u0393} \u2192\n \u27e6 x \u27e7 (ignore \u03c1) \u2295 \u27e6 x \u27e7\u0394Var \u03c1 \u2261 \u27e6 x \u27e7 (update \u03c1)\n\ncorrectVar {x = this } {cons v dv R[v,dv] \u03c1} = refl\ncorrectVar {x = that y} {cons v dv R[v,dv] \u03c1} = correctVar {x = y} {\u03c1}\n\ncorrectness {t = nat n} = refl\ncorrectness {t = bag b} = b++\u2205=b\ncorrectness {t = var x} = correctVar {x = x}\n\ncorrectness {t = add s t} =\n cong\u2082 _+_ (correctness {t = s}) (correctness {t = t})\n\ncorrectness {t = map s t} {\u03c1} =\n trans (b++[d\\\\b]=d {mapBag f b} {mapBag (f \u2295 df) (b \u2295 db)})\n (cong\u2082 mapBag (correctness {t = s}) (correctness {t = t}))\n where\n f = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7 \u03c1\n db = \u27e6 derive t \u27e7 \u03c1\n\ncorrectness {t = app s t} {\u03c1} =\n let\n v = \u27e6 t \u27e7 (ignore \u03c1)\n dv = \u27e6 derive t \u27e7 \u03c1\n in trans\n (sym (proj\u2082 (validity {t = s} {\u03c1} v dv (validity {t = t} {\u03c1}))))\n (correctness {t = s} \u27e8$\u27e9 correctness {t = t})\n\ncorrectness {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u0393} {abs t} {\u03c1} = extensionality (\u03bb v \u2192\n let\n \u03c1\u2032 : \u0394-Env (\u03c4\u2081 \u2022 \u0393)\n \u03c1\u2032 = cons v (v \u229d v) R[v,u\u229dv] \u03c1\n in\n begin\n \u27e6 t \u27e7 (ignore \u03c1\u2032) \u2295 \u27e6 derive t \u27e7 \u03c1\u2032\n \u2261\u27e8 correctness {t = t} {\u03c1\u2032} \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2032)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 (hole \u2022 update \u03c1)) v\u2295[u\u229dv]=u \u27e9\n \u27e6 t \u27e7 (v \u2022 update \u03c1)\n \u220e\n ) where open \u2261-Reasoning\n-}\n\n","old_contents":"{-\nThe goal of this file is to make the domain of \u0394 types smaller\nso as to be nearer to full abstraction and hopefully close\nenough for the purpose of having explicit nil changes.\n-}\n\nmodule TaggedDeltaTypes where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Nat\nopen import Data.Unit using (\u22a4 ; tt)\nimport Data.Integer as \u2124\nimport Data.Product as Product\nopen import Data.Product using (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\nimport Level\n\n-- Postulates: Extensionality and bag properties (#55)\npostulate extensionality : Extensionality Level.zero Level.zero\n--\n-- open import Data.NatBag renaming\n-- (map to mapBag ; empty to emptyBag ; update to updateBag)\n-- open import Data.NatBag.Properties\npostulate Bag : Set\npostulate emptyBag : Bag\npostulate mapBag : (\u2115 \u2192 \u2115) \u2192 Bag \u2192 Bag\npostulate _++_ : Bag \u2192 Bag \u2192 Bag\npostulate _\\\\_ : Bag \u2192 Bag \u2192 Bag\ninfixr 5 _++_\ninfixl 9 _\\\\_\npostulate b\\\\b=\u2205 : \u2200 {b : Bag} \u2192 b \\\\ b \u2261 emptyBag\npostulate b++\u2205=b : \u2200 {b : Bag} \u2192 b ++ emptyBag \u2261 b\npostulate \u2205++b=b : \u2200 {b : Bag} \u2192 emptyBag ++ b \u2261 b\npostulate b++[d\\\\b]=d : \u2200 {b d} \u2192 b ++ (d \\\\ b) \u2261 d\npostulate [b++d]\\\\b=d : \u2200 {b d} \u2192 (b ++ d) \\\\ b \u2261 d\npostulate\n [a++b]\\\\[c++d]=[a\\\\c]++[b\\\\d] : \u2200 {a b c d} \u2192\n (a ++ b) \\\\ (c ++ d) \u2261 (a \\\\ c) ++ (b \\\\ d)\npostulate\n [a\\\\b]\\\\[c\\\\d]=[a\\\\c]\\\\[b\\\\d] : \u2200 {a b c d} \u2192\n (a \\\\ b) \\\\ (c \\\\ d) \u2261 (a \\\\ c) \\\\ (b \\\\ d)\n\n------------------------\n-- Syntax of programs --\n------------------------\n\ndata Type : Set where\n nats : Type\n bags : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u03c4 \u0393} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u03c4 \u03c4\u2032 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n bag : \u2200 {\u0393} \u2192 (b : Bag) \u2192 Term \u0393 bags\n\n var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (s : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n add : \u2200 {\u0393} \u2192 (s : Term \u0393 nats) \u2192 (t : Term \u0393 nats) \u2192 Term \u0393 nats\n map : \u2200 {\u0393} \u2192 (f : Term \u0393 (nats \u21d2 nats)) \u2192 (b : Term \u0393 bags) \u2192 Term \u0393 bags\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u0393\u227c\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0393\n\u0393\u227c\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0393 {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u0393\u227c\u0393 {\u0393}\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\nweaken subctx (bag b) = bag b\nweaken subctx (add t\u2081 t\u2082) = add (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (map f b) = map (weaken subctx f) (weaken subctx b)\n\n---------------------------\n-- Semantics of programs --\n---------------------------\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 nats \u27e7Type = \u2115\n\u27e6 bags \u27e7Type = Bag\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nidentity-weakening : \u2200 {\u0393} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192 \u27e6 \u0393\u227c\u0393 {\u0393} \u27e7 \u03c1 \u2261 \u03c1\nidentity-weakening {\u2205} {\u2205} = refl\nidentity-weakening {\u03c4 \u2022 \u0393} {v \u2022 \u03c1} =\n cong\u2082 _\u2022_ {x = v} refl (identity-weakening {\u0393} {\u03c1})\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\u27e6 bag b \u27e7Term \u03c1 = b\n\u27e6 add m n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 map f b \u27e7Term \u03c1 = mapBag (\u27e6 f \u27e7Term \u03c1) (\u27e6 b \u27e7Term \u03c1)\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n_\u27e8$\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\n\n-- infix 0 $ in Haskell\ninfixl 0 _\u27e8$\u27e9_\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = weaken-sound t\u2081 \u03c1 \u27e8$\u27e9 weaken-sound t\u2082 \u03c1\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (bag b) \u03c1 = refl\nweaken-sound (add m n) \u03c1 = cong\u2082 _+_ (weaken-sound m \u03c1) (weaken-sound n \u03c1)\nweaken-sound (map f b) \u03c1 =\n cong\u2082 mapBag (weaken-sound f \u03c1) (weaken-sound b \u03c1)\n\n----------------------------------------------------------\n-- Syntax and semantics of changes (they are entangled) --\n----------------------------------------------------------\n\ndata \u0394-Type : Set where\n nats : \u0394-Type\n bags : \u0394-Type\n alter_\u21d2_ : \u0394-Type \u2192 \u0394-Type \u2192 \u0394-Type\n abide_\u21d2_ : \u0394-Type \u2192 \u0394-Type \u2192 \u0394-Type\n\ndata \u0394-Context : Set where\n \u2205 : \u0394-Context\n alter_\u2022_ : \u0394-Type \u2192 \u0394-Context \u2192 \u0394-Context\n abide_\u2022_ : \u0394-Type \u2192 \u0394-Context \u2192 \u0394-Context\n\n-- Convert a type\/context to a \u0394-type\/\u0394-context without any\n-- assumption about arguments\n\n\u0394-type : Type \u2192 \u0394-Type\n\u0394-type nats = nats\n\u0394-type bags = bags\n\u0394-type (\u03c4\u2081 \u21d2 \u03c4\u2082) = alter \u0394-type \u03c4\u2081 \u21d2 \u0394-type \u03c4\u2082\n\n\u0394-context : Context \u2192 \u0394-Context\n\u0394-context \u2205 = \u2205\n\u0394-context (\u03c4 \u2022 \u0393) = alter \u0394-type \u03c4 \u2022 \u0394-context \u0393\n\n{-\n\n\u27e6_\u27e7\u0394Type : \u2200 {\u03c4 args} \u2192 \u0394-Type \u03c4 {args} \u2192 Set\n\ndata \u0394-Env : (\u0393 : Context) \u2192 {vars : Vars \u0393} \u2192 Set\ndata \u0394-Term : (\u0393 : Context) \u2192 Type \u2192 {vars : Vars \u0393} \u2192 Set\n\n\u27e6_\u27e7\u0394Term : \u2200 {\u03c4 : Type} {\u0393 : Context} {vars}\n \u2192 \u0394-Term \u0393 \u03c4 {vars} \u2192 \u0394-Env \u0393 {vars} \u2192 \u27e6 \u03c4 \u27e7\u0394Type\n_\u2295_ : \u2200 {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\u0394Type \u2192 \u27e6 \u03c4 \u27e7\n_\u229d_ : \u2200 {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\u0394Type\nvalid : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\u0394Type \u2192 Set\nR[v,u\u229dv] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229d v)\nv\u2295[u\u229dv]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 v \u2295 (u \u229d v) \u2261 u\nignore : \u2200 {\u0393 : Context} {vars} \u2192 (\u03c1 : \u0394-Env \u0393 {vars}) \u2192 \u27e6 \u0393 \u27e7\nupdate : \u2200 {\u0393 : Context} {vars} \u2192 (\u03c1 : \u0394-Env \u0393 {vars}) \u2192 \u27e6 \u0393 \u27e7\n\ninfixl 6 _\u2295_ _\u229d_ -- as with + - in GHC.Num\n\ndata \u0394-Term where\n -- changes to numbers are replacement pairs\n \u0394nat : \u2200 {\u0393 vars} \u2192\n (old : \u2115) \u2192 (new : \u2115) \u2192 \u0394-Term \u0393 nats {vars}\n -- changes to bags are bags\n \u0394bag : \u2200 {\u0393 vars} \u2192\n (db : Bag) \u2192 \u0394-Term \u0393 bags {vars}\n -- changes to variables are variables\n \u0394var : \u2200 {\u03c4 \u0393 vars} \u2192\n (x : Var \u0393 \u03c4) \u2192 \u0394-Term \u0393 \u03c4 {vars}\n -- changes to abstractions are binders of x and dx\n -- There are two kinds of those: One who expects the argument\n -- to change, and one who does not.\n \u0394abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393 vars} \u2192 (t : \u0394-Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082 {alter vars}) \u2192\n \u0394-Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082) {vars}\n \u0394abs\u2080 : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393 vars} \u2192 (t : \u0394-Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082 {abide vars}) \u2192\n \u0394-Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082) {vars}\n -- changes to applications are applications of a value and a change\n \u0394app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393 vars}\n (ds : \u0394-Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082) {vars})\n ( t : Term \u0393 \u03c4\u2081)\n (dt : \u0394-Term \u0393 \u03c4\u2081 {vars})\n (R[t,dt] : {\u03c1 : \u0394-Env \u0393 {-vars-}} \u2192\n valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394Term \u03c1)) \u2192\n \u0394-Term \u0393 \u03c4\u2082 {vars}\n -- changes to addition are changes to their components\n \u0394add : \u2200 {\u0393 vars}\n (ds : \u0394-Term \u0393 nats {vars})\n (dt : \u0394-Term \u0393 nats {vars}) \u2192\n \u0394-Term \u0393 nats {vars}\n -- There are two kinds of changes to maps:\n -- 0. recomputation,\n -- 1. mapping over changes,\n -- the latter used only with some form of isNil available.\n \u0394map\u2080 : \u2200 {\u0393 vars} \u2192\n ( f : Term \u0393 (nats \u21d2 nats))\n (df : \u0394-Term \u0393 (nats \u21d2 nats) {vars})\n ( b : Term \u0393 bags)\n (db : \u0394-Term \u0393 bags {vars}) \u2192\n \u0394-Term \u0393 bags {vars}\n \u0394map\u2081 : \u2200 {\u0393 vars} \u2192\n ( f : Term \u0393 (nats \u21d2 nats)) (db : \u0394-Term \u0393 bags {vars}) \u2192\n \u0394-Term \u0393 bags {vars}\n\n\u27e6 \u0394 nats \u27e7\u0394Type = \u2115 \u00d7 \u2115\n\u27e6 \u0394 bags \u27e7\u0394Type = Bag\n\u27e6 \u0394 (\u03c4\u2081 \u21d2 \u03c4\u2082) {alter args} \u27e7\u0394Type =\n \u2200 {args\u2081} \u2192\n (v : \u27e6 \u03c4\u2081 \u27e7) \u2192 (dv : \u27e6 \u0394 \u03c4\u2081 {args\u2081} \u27e7\u0394Type) \u2192 valid v dv \u2192\n \u27e6 \u0394 \u03c4\u2082 {args} \u27e7\u0394Type\n\nmeaning-\u0394Type : Meaning\u0394 Type\nmeaning-\u0394Type = meaning\u0394 \u27e6_\u27e7\u0394Type\n\nrecord Meaning\u0394\n (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\u0394\n field\n {\u0394-Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7\u0394 : Syntax \u2192 \u0394-Semantics\n\nopen Meaning\u0394 {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7\u0394 to \u27e6_\u27e7\u0394)\n\n_\u2295_ {nats} n dn = proj\u2082 dn\n_\u2295_ {bags} b db = b ++ db\n_\u2295_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u2295 df v (v \u229d v) R[v,u\u229dv]\n\n_\u229d_ {nats} m n = (n , m)\n_\u229d_ {bags} b d = b \\\\ d\n_\u229d_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb v dv R[v,dv] \u2192 f (v \u2295 dv) \u229d g v\n\n-- valid : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\u0394Type \u2192 Set\nvalid {nats} n dn = n \u2261 proj\u2081 dn\nvalid {bags} b db = \u22a4\nvalid {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (v : \u27e6 \u03c4\u2081 \u27e7) (dv : \u27e6 \u03c4\u2081 \u27e7\u0394Type) (R[v,dv] : valid v dv)\n \u2192 valid (f v) (df v dv R[v,dv])\n \u00d7 (f \u2295 df) (v \u2295 dv) \u2261 f v \u2295 df v dv R[v,dv]\n\nv\u2295[u\u229dv]=u {nats} {u} {v} = refl\nv\u2295[u\u229dv]=u {bags} {u} {v} = b++[d\\\\b]=d {v} {u}\nv\u2295[u\u229dv]=u {\u03c4\u2081 \u21d2 \u03c4\u2082} {u} {v} = extensionality (\u03bb w \u2192\n begin\n (v \u2295 (u \u229d v)) w\n \u2261\u27e8 refl \u27e9 -- for clarity\n v w \u2295 (u (w \u2295 (w \u229d w)) \u229d v w)\n \u2261\u27e8 cong (\u03bb hole \u2192 v w \u2295 (u hole \u229d v w)) v\u2295[u\u229dv]=u \u27e9\n v w \u2295 (u w \u229d v w)\n \u2261\u27e8 v\u2295[u\u229dv]=u \u27e9\n u w\n \u220e) where open \u2261-Reasoning\n\nR[v,u\u229dv] {nats} {u} {v} = refl\nR[v,u\u229dv] {bags} {u} {v} = tt\nR[v,u\u229dv] {\u03c4\u2081 \u21d2 \u03c4\u2082} {u} {v} = \u03bb w dw R[w,dw] \u2192\n let\n w\u2032 = w \u2295 dw\n in\n R[v,u\u229dv] -- NOT a self recursion: implicit arguments are different.\n ,\n (begin\n (v \u2295 (u \u229d v)) w\u2032\n \u2261\u27e8 cong (\u03bb hole \u2192 hole w\u2032) (v\u2295[u\u229dv]=u {u = u} {v}) \u27e9\n u w\u2032\n \u2261\u27e8 sym (v\u2295[u\u229dv]=u {u = u w\u2032} {v w}) \u27e9\n v w \u2295 (u \u229d v) w dw R[w,dw]\n \u220e) where open \u2261-Reasoning\n\nrecord Quadruple\n (A : Set) (B : A \u2192 Set) (C : (a : A) \u2192 B a \u2192 Set)\n (D : (a : A) \u2192 (b : B a) \u2192 (c : C a b) \u2192 Set): Set where\n constructor cons\n field\n car : A\n cadr : B car\n caddr : C car cadr\n cdddr : D car cadr caddr\n\nopen Quadruple public\n\n-- The type of environments ensures their consistency and honesty.\n-- \u0394-Env : (\u0393 : Context) \u2192 {vars : Vars \u0393} \u2192 Set\ndata \u0394-Env where\n \u2205 : \u0394-Env \u2205 {\u2205}\n abide : \u2200 {\u03c4 \u0393 vars} \u2192\n (v : \u27e6 \u03c4 \u27e7) \u2192 (dv : \u27e6 \u03c4 \u27e7\u0394) \u2192 valid v dv \u2192 v \u2295 dv \u2261 v \u2192\n \u0394-Env \u0393 {vars} \u2192 \u0394-Env (\u03c4 \u2022 \u0393) {abide vars}\n alter : \u2200 {\u03c4 \u0393 vars} \u2192\n (v : \u27e6 \u03c4 \u27e7) \u2192 (dv : \u27e6 \u03c4 \u27e7\u0394) \u2192 valid v dv \u2192\n \u0394-Env \u0393 {vars} \u2192 \u0394-Env (\u03c4 \u2022 \u0393) {alter vars}\n\n\u2016_\u2016 : \u2200 {\u03c4 \u0393 vars} \u2192 \u0394-Env (\u03c4 \u2022 \u0393) {vars} \u2192\n Quadruple \u27e6 \u03c4 \u27e7 (\u03bb _ \u2192 \u27e6 \u03c4 \u27e7\u0394) (\u03bb v dv \u2192 valid v dv)\n (\u03bb _ _ _ \u2192 \u0394-Env \u0393 {cdr vars})\n\n\u2016 abide v dv R[v,dv] _ \u03c1 \u2016 = cons v dv R[v,dv] \u03c1\n\u2016 alter v dv R[v,dv] \u03c1 \u2016 = cons v dv R[v,dv] \u03c1\n\n-- ignore : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394-Env \u0393) \u2192 Env \u0393\nignore {\u2205} \u03c1 = \u2205\nignore {\u03c4 \u2022 \u0393} \u03c1\u2032 with \u2016 \u03c1\u2032 \u2016\n... | (cons v dv R[v,dv] \u03c1) = v \u2022 ignore \u03c1\n\n-- update : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394-Env \u0393) \u2192 Env \u0393\nupdate {\u2205} \u03c1 = \u2205\nupdate {\u03c4 \u2022 \u0393} \u03c1\u2032 with \u2016 \u03c1\u2032 \u2016\n... | (cons v dv R[v,dv] \u03c1) = (v \u2295 dv) \u2022 update \u03c1\n\n\u27e6_\u27e7\u0394Var : \u2200 {\u03c4 \u0393 vars} \u2192 Var \u0393 \u03c4 \u2192 \u0394-Env \u0393 {vars} \u2192 \u27e6 \u03c4 \u27e7\u0394Type\n\u27e6 this \u27e7\u0394Var \u03c1\u2032 with \u2016 \u03c1\u2032 \u2016\n... | (cons v dv R[v,dv] \u03c1) = dv\n\u27e6 that x \u27e7\u0394Var \u03c1\u2032 with \u2016 \u03c1\u2032 \u2016\n... | (cons v dv R[v,dv] \u03c1) = \u27e6 x \u27e7\u0394Var \u03c1\n\nmeaning-\u0394Var : \u2200 {\u03c4 \u0393} {vars : Vars \u0393} \u2192 Meaning\u0394 (Var \u0393 \u03c4)\nmeaning-\u0394Var {\u03c4} {\u0393} {vars} = meaning\u0394 (\u27e6_\u27e7\u0394Var {\u03c4} {\u0393} {vars})\n\n-- \u27e6_\u27e7\u0394Term : \u2200 {\u03c4 \u0393 vars} \u2192\n-- \u0394-Term \u0393 \u03c4 {vars} \u2192 \u0394-Env \u0393 {vars} \u2192 \u27e6 \u03c4 \u27e7\u0394Type\n\u27e6 \u0394nat old new \u27e7\u0394Term \u03c1 = (old , new)\n\u27e6 \u0394bag db \u27e7\u0394Term \u03c1 = db\n\u27e6 \u0394var x \u27e7\u0394Term \u03c1 = \u27e6 x \u27e7\u0394Var \u03c1\n\u27e6 \u0394abs t \u27e7\u0394Term \u03c1 =\n \u03bb v dv R[v,dv] \u2192 \u27e6 t \u27e7\u0394Term (alter v dv R[v,dv] \u03c1)\n\u27e6 \u0394abs\u2080 t \u27e7\u0394Term \u03c1 =\n \u03bb v dv R[v,dv] {-v\u2295dv=v-} \u2192 \u27e6 t \u27e7\u0394Term (abide v dv R[v,dv] {!v\u2295dv=v!} \u03c1)\n\u27e6 \u0394app ds t dt R[dt,t] \u27e7\u0394Term \u03c1 =\n \u27e6 ds \u27e7\u0394Term \u03c1 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394Term \u03c1) R[dt,t]\n --(R[dt,t] {\u03c1} {honesty = {!!}})\n\u27e6 \u0394add ds dt \u27e7\u0394Term \u03c1 =\n let\n (old-s , new-s) = \u27e6 ds \u27e7\u0394Term \u03c1\n (old-t , new-t) = \u27e6 dt \u27e7\u0394Term \u03c1\n in\n (old-s + old-t , new-s + new-t)\n\u27e6 \u0394map\u2080 f df b db \u27e7\u0394Term \u03c1 =\n let\n v = \u27e6 b \u27e7 (ignore \u03c1)\n h = \u27e6 f \u27e7 (ignore \u03c1)\n dv = \u27e6 db \u27e7\u0394Term \u03c1\n dh = \u27e6 df \u27e7\u0394Term \u03c1\n in\n mapBag (h \u2295 dh) (v \u2295 dv) \\\\ mapBag h v\n\u27e6 \u0394map\u2081 f db \u27e7\u0394Term \u03c1 = mapBag (\u27e6 f \u27e7 (ignore \u03c1)) (\u27e6 db \u27e7\u0394Term \u03c1)\n\nmeaning-\u0394Term-0 : \u2200 {\u03c4 \u0393 vars} \u2192 Meaning (\u0394-Term \u0393 \u03c4 {vars})\nmeaning-\u0394Term-0 = meaning \u27e6_\u27e7\u0394Term\n\nmeaning-\u0394Term-1 : \u2200 {\u03c4 \u0393 vars} \u2192 Meaning\u0394 (\u0394-Term \u0393 \u03c4 {vars})\nmeaning-\u0394Term-1 = meaning\u0394 \u27e6_\u27e7\u0394Term\n\n{-\n--------------------------------------------------------\n-- Program transformation and correctness (entangled) --\n--------------------------------------------------------\n\nderive : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394-Term \u0393 \u03c4\n\nvalidity : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394-Env \u0393} \u2192\n valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\ncorrectness : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394-Env \u0393} \u2192\n \u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (update \u03c1)\n\n-- derive : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394-Term \u0393 \u03c4\nderive (nat n) = \u0394nat n n\nderive (bag b) = \u0394bag emptyBag\nderive (var x) = \u0394var x\nderive (abs t) = \u0394abs (derive t)\nderive (app s t) = \u0394app (derive s) t (derive t) validity\nderive (add s t) = \u0394add (derive s) (derive t)\nderive (map f b) = \u0394map\u2080 f (derive f) b (derive b)\n\nvalidity-var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192\n \u2200 {\u03c1 : \u0394-Env \u0393} \u2192 valid (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 x \u27e7\u0394Var \u03c1)\n\nvalidity-var this {cons v dv R[v,dv] \u03c1} = R[v,dv]\nvalidity-var (that x) {cons v dv R[v,dv] \u03c1} = validity-var x\n\nvalidity {t = nat n} = refl\nvalidity {t = bag b} = tt\nvalidity {t = var x} = validity-var x\nvalidity {t = map f b} = tt\nvalidity {t = add s t} = cong\u2082 _+_ (validity {t = s}) (validity {t = t})\n\nvalidity {t = app s t} {\u03c1} =\n let\n v = \u27e6 t \u27e7 (ignore \u03c1)\n dv = \u27e6 derive t \u27e7 \u03c1\n in\n proj\u2081 (validity {t = s} {\u03c1} v dv (validity {t = t} {\u03c1}))\n\nvalidity {t = abs t} {\u03c1} = \u03bb v dv R[v,dv] \u2192\n let\n v\u2032 = v \u2295 dv\n dv\u2032 = v\u2032 \u229d v\u2032\n \u03c1\u2081 = cons v dv R[v,dv] \u03c1\n \u03c1\u2082 = cons v\u2032 dv\u2032 R[v,u\u229dv] \u03c1\n in\n validity {t = t} {\u03c1\u2081}\n ,\n (begin\n \u27e6 t \u27e7 (ignore \u03c1\u2082) \u2295 \u27e6 derive t \u27e7 \u03c1\u2082\n \u2261\u27e8 correctness {t = t} {\u03c1\u2082} \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2082)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 (hole \u2022 update \u03c1)) v\u2295[u\u229dv]=u \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2081)\n \u2261\u27e8 sym (correctness {t = t} {\u03c1\u2081}) \u27e9\n \u27e6 t \u27e7 (ignore \u03c1\u2081) \u2295 \u27e6 derive t \u27e7 \u03c1\u2081\n \u220e) where open \u2261-Reasoning\n\ncorrectVar : \u2200 {\u03c4 \u0393} {x : Var \u0393 \u03c4} {\u03c1 : \u0394-Env \u0393} \u2192\n \u27e6 x \u27e7 (ignore \u03c1) \u2295 \u27e6 x \u27e7\u0394Var \u03c1 \u2261 \u27e6 x \u27e7 (update \u03c1)\n\ncorrectVar {x = this } {cons v dv R[v,dv] \u03c1} = refl\ncorrectVar {x = that y} {cons v dv R[v,dv] \u03c1} = correctVar {x = y} {\u03c1}\n\ncorrectness {t = nat n} = refl\ncorrectness {t = bag b} = b++\u2205=b\ncorrectness {t = var x} = correctVar {x = x}\n\ncorrectness {t = add s t} =\n cong\u2082 _+_ (correctness {t = s}) (correctness {t = t})\n\ncorrectness {t = map s t} {\u03c1} =\n trans (b++[d\\\\b]=d {mapBag f b} {mapBag (f \u2295 df) (b \u2295 db)})\n (cong\u2082 mapBag (correctness {t = s}) (correctness {t = t}))\n where\n f = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7 \u03c1\n db = \u27e6 derive t \u27e7 \u03c1\n\ncorrectness {t = app s t} {\u03c1} =\n let\n v = \u27e6 t \u27e7 (ignore \u03c1)\n dv = \u27e6 derive t \u27e7 \u03c1\n in trans\n (sym (proj\u2082 (validity {t = s} {\u03c1} v dv (validity {t = t} {\u03c1}))))\n (correctness {t = s} \u27e8$\u27e9 correctness {t = t})\n\ncorrectness {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u0393} {abs t} {\u03c1} = extensionality (\u03bb v \u2192\n let\n \u03c1\u2032 : \u0394-Env (\u03c4\u2081 \u2022 \u0393)\n \u03c1\u2032 = cons v (v \u229d v) R[v,u\u229dv] \u03c1\n in\n begin\n \u27e6 t \u27e7 (ignore \u03c1\u2032) \u2295 \u27e6 derive t \u27e7 \u03c1\u2032\n \u2261\u27e8 correctness {t = t} {\u03c1\u2032} \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2032)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 (hole \u2022 update \u03c1)) v\u2295[u\u229dv]=u \u27e9\n \u27e6 t \u27e7 (v \u2022 update \u03c1)\n \u220e\n ) where open \u2261-Reasoning\n-}\n-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"28811f00933ec13ba9e4a8c9ad81998ae70b7eb0","subject":"Alternative definition of function composition","message":"Alternative definition of function composition\n\nThis is the second definition taken from New\/Changes.agda. As usual, its\ncorrectness proofs are much simpler (after we've rephrased the\nstatements cleverly).\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/Changes.agda","new_file":"Thesis\/Changes.agda","new_contents":"module Thesis.Changes where\n\nopen import Data.Product\nopen import Data.Sum\nopen import Relation.Binary.PropositionalEquality\n\nrecord IsChangeStructure (A : Set) (ChA : Set) (ch_from_to_ : (dv : ChA) \u2192 (v1 v2 : A) \u2192 Set) : Set\u2081 where\n infixl 6 _\u2295_ _\u229d_\n field\n _\u2295_ : A \u2192 ChA \u2192 A\n fromto\u2192\u2295 : \u2200 dv v1 v2 \u2192\n ch dv from v1 to v2 \u2192\n v1 \u2295 dv \u2261 v2\n _\u229d_ : A \u2192 A \u2192 ChA\n \u229d-fromto : \u2200 (a b : A) \u2192 ch (b \u229d a) from a to b\n\n update-diff : (b a : A) \u2192 a \u2295 (b \u229d a) \u2261 b\n update-diff b a = fromto\u2192\u2295 (b \u229d a) a b (\u229d-fromto a b)\n nil : A \u2192 ChA\n nil a = a \u229d a\n nil-fromto : (a : A) \u2192 ch (nil a) from a to a\n nil-fromto a = \u229d-fromto a a\n update-nil : (a : A) \u2192 a \u2295 nil a \u2261 a\n update-nil a = update-diff a a\n\nrecord IsCompChangeStructure (A : Set) (ChA : Set) (ch_from_to_ : (dv : ChA) \u2192 (v1 v2 : A) \u2192 Set) : Set\u2081 where\n field\n isChangeStructure : IsChangeStructure A ChA ch_from_to_\n _\u229a[_]_ : ChA \u2192 A \u2192 ChA \u2192 ChA\n \u229a-fromto : \u2200 (a1 a2 a3 : A) (da1 da2 : ChA) \u2192\n ch da1 from a1 to a2 \u2192 ch da2 from a2 to a3 \u2192 ch da1 \u229a[ a1 ] da2 from a1 to a3\n\n open IsChangeStructure isChangeStructure public\n \u229a-correct : \u2200 (a1 a2 a3 : A) (da1 da2 : ChA) \u2192\n ch da1 from a1 to a2 \u2192 ch da2 from a2 to a3 \u2192\n a1 \u2295 (da1 \u229a[ a1 ] da2) \u2261 a3\n \u229a-correct a1 a2 a3 da1 da2 daa1 daa2 = fromto\u2192\u2295 _ _ _ (\u229a-fromto _ _ _ da1 da2 daa1 daa2)\n\nIsChangeStructure\u2192IsCompChangeStructure : \u2200 {A ChA ch_from_to_} \u2192 IsChangeStructure A ChA ch_from_to_ \u2192 IsCompChangeStructure A ChA ch_from_to_\nIsChangeStructure\u2192IsCompChangeStructure {A} {ChA} {ch_from_to_} isCS = record\n { isChangeStructure = isCS\n ; _\u229a[_]_ = \u03bb da1 a da2 \u2192 a \u2295 da1 \u2295 da2 \u229d a\n ; \u229a-fromto = body\n }\n where\n _\u2295_ = IsChangeStructure._\u2295_ isCS\n _\u229d_ = IsChangeStructure._\u229d_ isCS\n fromto\u2192\u2295 = IsChangeStructure.fromto\u2192\u2295 isCS\n \u229d-fromto = IsChangeStructure.\u229d-fromto isCS\n infixl 6 _\u2295_ _\u229d_\n body : \u2200 (a1 a2 a3 : A) da1 da2 \u2192\n ch da1 from a1 to a2 \u2192\n ch da2 from a2 to a3 \u2192 ch a1 \u2295 da1 \u2295 da2 \u229d a1 from a1 to a3\n body a1 a2 a3 da1 da2 daa1 daa2 rewrite fromto\u2192\u2295 _ _ _ daa1 | fromto\u2192\u2295 _ _ _ daa2 =\n \u229d-fromto a1 a3\n\n\nrecord ChangeStructure (A : Set) : Set\u2081 where\n field\n Ch : Set\n ch_from_to_ : (dv : Ch) \u2192 (v1 v2 : A) \u2192 Set\n isCompChangeStructure : IsCompChangeStructure A Ch ch_from_to_\n open IsCompChangeStructure isCompChangeStructure public\n\nopen ChangeStructure {{...}} public hiding (Ch)\nCh : \u2200 (A : Set) \u2192 {{CA : ChangeStructure A}} \u2192 Set\nCh A {{CA}} = ChangeStructure.Ch CA\n\n{-# DISPLAY IsChangeStructure._\u2295_ x = _\u2295_ #-}\n{-# DISPLAY ChangeStructure._\u2295_ x = _\u2295_ #-}\n{-# DISPLAY IsChangeStructure._\u229d_ x = _\u229d_ #-}\n{-# DISPLAY ChangeStructure._\u229d_ x = _\u229d_ #-}\n{-# DISPLAY IsChangeStructure.nil x = nil #-}\n{-# DISPLAY ChangeStructure.nil x = nil #-}\n{-# DISPLAY IsCompChangeStructure._\u229a[_]_ x = _\u229a[_]_ #-}\n{-# DISPLAY ChangeStructure._\u229a[_]_ x = _\u229a[_]_ #-}\n{-# DISPLAY ChangeStructure.ch_from_to_ x = ch_from_to_ #-}\n\nmodule _ {A B : Set} {{CA : ChangeStructure A}} {{CB : ChangeStructure B}} where\n\n -- In this module, given change structures CA and CB for A and B, we define\n -- change structures for A \u2192 B, A \u00d7 B and A \u228e B.\n\n open import Postulate.Extensionality\n\n -- Functions\n instance\n funCS : ChangeStructure (A \u2192 B)\n\n infixl 6 _f\u2295_ _f\u229d_\n private\n fCh = A \u2192 Ch A \u2192 Ch B\n\n fCh_from_to_ : (df : fCh) \u2192 (f1 f2 : A \u2192 B) \u2192 Set\n fCh_from_to_ =\n \u03bb df f1 f2 \u2192 \u2200 da (a1 a2 : A) (daa : ch da from a1 to a2) \u2192\n ch df a1 da from f1 a1 to f2 a2\n\n _f\u2295_ : (A \u2192 B) \u2192 fCh \u2192 A \u2192 B\n _f\u2295_ = \u03bb f df a \u2192 f a \u2295 df a (nil a)\n\n _f\u229d_ : (g f : A \u2192 B) \u2192 fCh\n _f\u229d_ = \u03bb g f a da \u2192 g (a \u2295 da) \u229d f a\n\n f\u229d-fromto : \u2200 (f1 f2 : A \u2192 B) \u2192 fCh (f2 f\u229d f1) from f1 to f2\n f\u229d-fromto f1 f2 da a1 a2 daa\n rewrite sym (fromto\u2192\u2295 da a1 a2 daa) = \u229d-fromto (f1 a1) (f2 (a1 \u2295 da))\n\n _f\u229a[_]_ : fCh \u2192 (A \u2192 B) \u2192 fCh \u2192 fCh\n _f\u229a[_]_ df1 f df2 = \u03bb a da \u2192 (df1 a (nil a)) \u229a[ f a ] (df2 a da)\n\n _f2\u229a[_]_ : fCh \u2192 (A \u2192 B) \u2192 fCh \u2192 fCh\n _f2\u229a[_]_ df1 f df2 = \u03bb a da \u2192 df1 a da \u229a[ f a ] df2 (a \u2295 da) (nil (a \u2295 da))\n\n f\u229a-fromto : \u2200 (f1 f2 f3 : A \u2192 B) (df1 df2 : fCh) \u2192 fCh df1 from f1 to f2 \u2192 fCh df2 from f2 to f3 \u2192\n fCh df1 f\u229a[ f1 ] df2 from f1 to f3\n f\u229a-fromto f1 f2 f3 df1 df2 dff1 dff2 da a1 a2 daa =\n \u229a-fromto (f1 a1) (f2 a1) (f3 a2)\n (df1 a1 (nil a1))\n (df2 a1 da)\n (dff1 (nil a1) a1 a1 (nil-fromto a1))\n (dff2 da a1 a2 daa)\n\n f\u229a2-fromto : \u2200 (f1 f2 f3 : A \u2192 B) (df1 df2 : fCh) \u2192 fCh df1 from f1 to f2 \u2192 fCh df2 from f2 to f3 \u2192\n fCh df1 f2\u229a[ f1 ] df2 from f1 to f3\n f\u229a2-fromto f1 f2 f3 df1 df2 dff1 dff2 da a1 a2 daa rewrite fromto\u2192\u2295 da a1 a2 daa =\n \u229a-fromto (f1 a1) (f2 a2) (f3 a2)\n (df1 a1 da)\n (df2 a2 (nil a2))\n (dff1 da a1 a2 daa)\n (dff2 (nil a2) a2 a2 (nil-fromto a2))\n\n funCS = record\n { Ch = fCh\n ; ch_from_to_ =\n \u03bb df f1 f2 \u2192 \u2200 da (a1 a2 : A) (daa : ch da from a1 to a2) \u2192\n ch df a1 da from f1 a1 to f2 a2\n ; isCompChangeStructure = record\n { isChangeStructure = record\n { _\u2295_ = _f\u2295_\n ; fromto\u2192\u2295 = \u03bb df f1 f2 dff \u2192\n ext (\u03bb v \u2192\n fromto\u2192\u2295 (df v (nil v)) (f1 v) (f2 v) (dff (nil v) v v (nil-fromto v)))\n ; _\u229d_ = _f\u229d_\n ; \u229d-fromto = f\u229d-fromto\n }\n ; _\u229a[_]_ = _f\u229a[_]_\n ; \u229a-fromto = f\u229a-fromto\n }\n }\n\n -- Products\n private\n pCh = Ch A \u00d7 Ch B\n _p\u2295_ : A \u00d7 B \u2192 Ch A \u00d7 Ch B \u2192 A \u00d7 B\n _p\u2295_ (a , b) (da , db) = a \u2295 da , b \u2295 db\n _p\u229d_ : A \u00d7 B \u2192 A \u00d7 B \u2192 pCh\n _p\u229d_ (a2 , b2) (a1 , b1) = a2 \u229d a1 , b2 \u229d b1\n pch_from_to_ : pCh \u2192 A \u00d7 B \u2192 A \u00d7 B \u2192 Set\n pch (da , db) from (a1 , b1) to (a2 , b2) = ch da from a1 to a2 \u00d7 ch db from b1 to b2\n _p\u229a[_]_ : pCh \u2192 A \u00d7 B \u2192 pCh \u2192 pCh\n (da1 , db1) p\u229a[ a , b ] (da2 , db2) = da1 \u229a[ a ] da2 , db1 \u229a[ b ] db2\n pfromto\u2192\u2295 : \u2200 dp p1 p2 \u2192\n pch dp from p1 to p2 \u2192 p1 p\u2295 dp \u2261 p2\n pfromto\u2192\u2295 (da , db) (a1 , b1) (a2 , b2) (daa , dbb) =\n cong\u2082 _,_ (fromto\u2192\u2295 _ _ _ daa) (fromto\u2192\u2295 _ _ _ dbb)\n p\u229d-fromto : \u2200 (p1 p2 : A \u00d7 B) \u2192 pch p2 p\u229d p1 from p1 to p2\n p\u229d-fromto (a1 , b1) (a2 , b2) = \u229d-fromto a1 a2 , \u229d-fromto b1 b2\n p\u229a-fromto : \u2200 p1 p2 p3 dp1 dp2 \u2192\n pch dp1 from p1 to p2 \u2192 (pch dp2 from p2 to p3) \u2192 pch dp1 p\u229a[ p1 ] dp2 from p1 to p3\n p\u229a-fromto (a1 , b1) (a2 , b2) (a3 , b3) (da1 , db1) (da2 , db2)\n (daa1 , dbb1) (daa2 , dbb2) =\n \u229a-fromto a1 a2 a3 da1 da2 daa1 daa2 , \u229a-fromto b1 b2 b3 db1 db2 dbb1 dbb2\n\n instance\n pairCS : ChangeStructure (A \u00d7 B)\n pairCS = record\n { Ch = pCh\n ; ch_from_to_ = pch_from_to_\n ; isCompChangeStructure = record\n { isChangeStructure = record\n { _\u2295_ = _p\u2295_\n ; fromto\u2192\u2295 = pfromto\u2192\u2295\n ; _\u229d_ = _p\u229d_\n ; \u229d-fromto = p\u229d-fromto\n }\n ; _\u229a[_]_ = _p\u229a[_]_\n ; \u229a-fromto = p\u229a-fromto\n }\n }\n\n -- Sums\n private\n SumChange = (Ch A \u228e Ch B) \u228e (A \u228e B)\n\n data SumChange2 : Set where\n ch\u2081 : (da : Ch A) \u2192 SumChange2\n ch\u2082 : (db : Ch B) \u2192 SumChange2\n rp : (s : A \u228e B) \u2192 SumChange2\n\n convert : SumChange \u2192 SumChange2\n convert (inj\u2081 (inj\u2081 da)) = ch\u2081 da\n convert (inj\u2081 (inj\u2082 db)) = ch\u2082 db\n convert (inj\u2082 s) = rp s\n convert\u2081 : SumChange2 \u2192 SumChange\n convert\u2081 (ch\u2081 da) = inj\u2081 (inj\u2081 da)\n convert\u2081 (ch\u2082 db) = inj\u2081 (inj\u2082 db)\n convert\u2081 (rp s) = inj\u2082 s\n\n inv1 : \u2200 ds \u2192 convert\u2081 (convert ds) \u2261 ds\n inv1 (inj\u2081 (inj\u2081 da)) = refl\n inv1 (inj\u2081 (inj\u2082 db)) = refl\n inv1 (inj\u2082 s) = refl\n inv2 : \u2200 ds \u2192 convert (convert\u2081 ds) \u2261 ds\n inv2 (ch\u2081 da) = refl\n inv2 (ch\u2082 db) = refl\n inv2 (rp s) = refl\n\n private\n _s\u22952_ : A \u228e B \u2192 SumChange2 \u2192 A \u228e B\n _s\u22952_ (inj\u2081 a) (ch\u2081 da) = inj\u2081 (a \u2295 da)\n _s\u22952_ (inj\u2082 b) (ch\u2082 db) = inj\u2082 (b \u2295 db)\n _s\u22952_ (inj\u2082 b) (ch\u2081 da) = inj\u2082 b -- invalid\n _s\u22952_ (inj\u2081 a) (ch\u2082 db) = inj\u2081 a -- invalid\n _s\u22952_ s (rp s\u2081) = s\u2081\n\n _s\u2295_ : A \u228e B \u2192 SumChange \u2192 A \u228e B\n s s\u2295 ds = s s\u22952 (convert ds)\n\n _s\u229d2_ : A \u228e B \u2192 A \u228e B \u2192 SumChange2\n _s\u229d2_ (inj\u2081 x2) (inj\u2081 x1) = ch\u2081 (x2 \u229d x1)\n _s\u229d2_ (inj\u2082 y2) (inj\u2082 y1) = ch\u2082 (y2 \u229d y1)\n _s\u229d2_ s2 s1 = rp s2\n\n _s\u229d_ : A \u228e B \u2192 A \u228e B \u2192 SumChange\n s2 s\u229d s1 = convert\u2081 (s2 s\u229d2 s1)\n\n data sch_from_to_ : SumChange \u2192 A \u228e B \u2192 A \u228e B \u2192 Set where\n -- sft = Sum From To\n sft\u2081 : \u2200 {da a1 a2} (daa : ch da from a1 to a2) \u2192 sch (convert\u2081 (ch\u2081 da)) from (inj\u2081 a1) to (inj\u2081 a2)\n sft\u2082 : \u2200 {db b1 b2} (dbb : ch db from b1 to b2) \u2192 sch (convert\u2081 (ch\u2082 db)) from (inj\u2082 b1) to (inj\u2082 b2)\n sftrp\u2081 : \u2200 a1 b2 \u2192 sch (convert\u2081 (rp (inj\u2082 b2))) from (inj\u2081 a1) to (inj\u2082 b2)\n sftrp\u2082 : \u2200 b1 a2 \u2192 sch (convert\u2081 (rp (inj\u2081 a2))) from (inj\u2082 b1) to (inj\u2081 a2)\n\n sfromto\u2192\u22952 : (ds : SumChange2) (s1 s2 : A \u228e B) \u2192\n sch convert\u2081 ds from s1 to s2 \u2192 s1 s\u22952 ds \u2261 s2\n sfromto\u2192\u22952 (ch\u2081 da) (inj\u2081 a1) (inj\u2081 a2) (sft\u2081 daa) = cong inj\u2081 (fromto\u2192\u2295 _ _ _ daa)\n sfromto\u2192\u22952 (ch\u2082 db) (inj\u2082 b1) (inj\u2082 b2) (sft\u2082 dbb) = cong inj\u2082 (fromto\u2192\u2295 _ _ _ dbb)\n sfromto\u2192\u22952 (rp .(inj\u2082 y)) (inj\u2081 x) (inj\u2082 y) (sftrp\u2081 .x .y) = refl\n sfromto\u2192\u22952 (rp .(inj\u2081 x)) (inj\u2082 y) (inj\u2081 x) (sftrp\u2082 .y .x) = refl\n\n sfromto\u2192\u2295 : (ds : SumChange) (s1 s2 : A \u228e B) \u2192\n sch ds from s1 to s2 \u2192 s1 s\u2295 ds \u2261 s2\n sfromto\u2192\u2295 ds s1 s2 dss =\n sfromto\u2192\u22952 (convert ds) s1 s2\n (subst (sch_from s1 to s2) (sym (inv1 ds))\n dss)\n s\u229d-fromto : (s1 s2 : A \u228e B) \u2192 sch s2 s\u229d s1 from s1 to s2\n s\u229d-fromto (inj\u2081 a1) (inj\u2081 a2) = sft\u2081 (\u229d-fromto a1 a2)\n s\u229d-fromto (inj\u2081 a1) (inj\u2082 b2) = sftrp\u2081 a1 b2\n s\u229d-fromto (inj\u2082 b1) (inj\u2081 a2) = sftrp\u2082 b1 a2\n s\u229d-fromto (inj\u2082 b1) (inj\u2082 b2) = sft\u2082 (\u229d-fromto b1 b2)\n instance\n sumCS : ChangeStructure (A \u228e B)\n sumCS = record\n { Ch = SumChange\n ; ch_from_to_ = sch_from_to_\n ; isCompChangeStructure = IsChangeStructure\u2192IsCompChangeStructure (record\n { _\u2295_ = _s\u2295_\n ; fromto\u2192\u2295 = sfromto\u2192\u2295\n ; _\u229d_ = _s\u229d_\n ; \u229d-fromto = s\u229d-fromto\n })\n }\n\nopen import Data.Integer\nopen import Data.Unit\nopen import Theorem.Groups-Nehemiah\n\nprivate\n intCh = \u2124\ninstance\n intCS : ChangeStructure \u2124\nintCS = record\n { Ch = \u2124\n ; ch_from_to_ = \u03bb dv v1 v2 \u2192 v1 + dv \u2261 v2\n ; isCompChangeStructure = record\n { isChangeStructure = record\n { _\u2295_ = _+_\n ; fromto\u2192\u2295 = \u03bb dv v1 v2 v2\u2261v1+dv \u2192 v2\u2261v1+dv\n ; _\u229d_ = _-_\n ; \u229d-fromto = \u03bb a b \u2192 n+[m-n]=m {a} {b}\n }\n ; _\u229a[_]_ = \u03bb da1 a da2 \u2192 da1 + da2\n ; \u229a-fromto = i\u229a-fromto\n }\n }\n where\n i\u229a-fromto : (a1 a2 a3 : \u2124) (da1 da2 : intCh) \u2192\n a1 + da1 \u2261 a2 \u2192 a2 + da2 \u2261 a3 \u2192 a1 + (da1 + da2) \u2261 a3\n i\u229a-fromto a1 a2 a3 da1 da2 a1+da1\u2261a2 a2+da2\u2261a3\n rewrite sym (associative-int a1 da1 da2) | a1+da1\u2261a2 = a2+da2\u2261a3\n","old_contents":"module Thesis.Changes where\n\nopen import Data.Product\nopen import Data.Sum\nopen import Relation.Binary.PropositionalEquality\n\nrecord IsChangeStructure (A : Set) (ChA : Set) (ch_from_to_ : (dv : ChA) \u2192 (v1 v2 : A) \u2192 Set) : Set\u2081 where\n infixl 6 _\u2295_ _\u229d_\n field\n _\u2295_ : A \u2192 ChA \u2192 A\n fromto\u2192\u2295 : \u2200 dv v1 v2 \u2192\n ch dv from v1 to v2 \u2192\n v1 \u2295 dv \u2261 v2\n _\u229d_ : A \u2192 A \u2192 ChA\n \u229d-fromto : \u2200 (a b : A) \u2192 ch (b \u229d a) from a to b\n\n update-diff : (b a : A) \u2192 a \u2295 (b \u229d a) \u2261 b\n update-diff b a = fromto\u2192\u2295 (b \u229d a) a b (\u229d-fromto a b)\n nil : A \u2192 ChA\n nil a = a \u229d a\n nil-fromto : (a : A) \u2192 ch (nil a) from a to a\n nil-fromto a = \u229d-fromto a a\n update-nil : (a : A) \u2192 a \u2295 nil a \u2261 a\n update-nil a = update-diff a a\n\nrecord IsCompChangeStructure (A : Set) (ChA : Set) (ch_from_to_ : (dv : ChA) \u2192 (v1 v2 : A) \u2192 Set) : Set\u2081 where\n field\n isChangeStructure : IsChangeStructure A ChA ch_from_to_\n _\u229a[_]_ : ChA \u2192 A \u2192 ChA \u2192 ChA\n \u229a-fromto : \u2200 (a1 a2 a3 : A) (da1 da2 : ChA) \u2192\n ch da1 from a1 to a2 \u2192 ch da2 from a2 to a3 \u2192 ch da1 \u229a[ a1 ] da2 from a1 to a3\n\n open IsChangeStructure isChangeStructure public\n \u229a-correct : \u2200 (a1 a2 a3 : A) (da1 da2 : ChA) \u2192\n ch da1 from a1 to a2 \u2192 ch da2 from a2 to a3 \u2192\n a1 \u2295 (da1 \u229a[ a1 ] da2) \u2261 a3\n \u229a-correct a1 a2 a3 da1 da2 daa1 daa2 = fromto\u2192\u2295 _ _ _ (\u229a-fromto _ _ _ da1 da2 daa1 daa2)\n\nIsChangeStructure\u2192IsCompChangeStructure : \u2200 {A ChA ch_from_to_} \u2192 IsChangeStructure A ChA ch_from_to_ \u2192 IsCompChangeStructure A ChA ch_from_to_\nIsChangeStructure\u2192IsCompChangeStructure {A} {ChA} {ch_from_to_} isCS = record\n { isChangeStructure = isCS\n ; _\u229a[_]_ = \u03bb da1 a da2 \u2192 a \u2295 da1 \u2295 da2 \u229d a\n ; \u229a-fromto = body\n }\n where\n _\u2295_ = IsChangeStructure._\u2295_ isCS\n _\u229d_ = IsChangeStructure._\u229d_ isCS\n fromto\u2192\u2295 = IsChangeStructure.fromto\u2192\u2295 isCS\n \u229d-fromto = IsChangeStructure.\u229d-fromto isCS\n infixl 6 _\u2295_ _\u229d_\n body : \u2200 (a1 a2 a3 : A) da1 da2 \u2192\n ch da1 from a1 to a2 \u2192\n ch da2 from a2 to a3 \u2192 ch a1 \u2295 da1 \u2295 da2 \u229d a1 from a1 to a3\n body a1 a2 a3 da1 da2 daa1 daa2 rewrite fromto\u2192\u2295 _ _ _ daa1 | fromto\u2192\u2295 _ _ _ daa2 =\n \u229d-fromto a1 a3\n\n\nrecord ChangeStructure (A : Set) : Set\u2081 where\n field\n Ch : Set\n ch_from_to_ : (dv : Ch) \u2192 (v1 v2 : A) \u2192 Set\n isCompChangeStructure : IsCompChangeStructure A Ch ch_from_to_\n open IsCompChangeStructure isCompChangeStructure public\n\nopen ChangeStructure {{...}} public hiding (Ch)\nCh : \u2200 (A : Set) \u2192 {{CA : ChangeStructure A}} \u2192 Set\nCh A {{CA}} = ChangeStructure.Ch CA\n\n{-# DISPLAY IsChangeStructure._\u2295_ x = _\u2295_ #-}\n{-# DISPLAY ChangeStructure._\u2295_ x = _\u2295_ #-}\n{-# DISPLAY IsChangeStructure._\u229d_ x = _\u229d_ #-}\n{-# DISPLAY ChangeStructure._\u229d_ x = _\u229d_ #-}\n{-# DISPLAY IsChangeStructure.nil x = nil #-}\n{-# DISPLAY ChangeStructure.nil x = nil #-}\n{-# DISPLAY IsCompChangeStructure._\u229a[_]_ x = _\u229a[_]_ #-}\n{-# DISPLAY ChangeStructure._\u229a[_]_ x = _\u229a[_]_ #-}\n{-# DISPLAY ChangeStructure.ch_from_to_ x = ch_from_to_ #-}\n\nmodule _ {A B : Set} {{CA : ChangeStructure A}} {{CB : ChangeStructure B}} where\n\n -- In this module, given change structures CA and CB for A and B, we define\n -- change structures for A \u2192 B, A \u00d7 B and A \u228e B.\n\n open import Postulate.Extensionality\n\n -- Functions\n instance\n funCS : ChangeStructure (A \u2192 B)\n\n infixl 6 _f\u2295_ _f\u229d_\n private\n fCh = A \u2192 Ch A \u2192 Ch B\n\n fCh_from_to_ : (df : fCh) \u2192 (f1 f2 : A \u2192 B) \u2192 Set\n fCh_from_to_ =\n \u03bb df f1 f2 \u2192 \u2200 da (a1 a2 : A) (daa : ch da from a1 to a2) \u2192\n ch df a1 da from f1 a1 to f2 a2\n\n _f\u2295_ : (A \u2192 B) \u2192 fCh \u2192 A \u2192 B\n _f\u2295_ = \u03bb f df a \u2192 f a \u2295 df a (nil a)\n\n _f\u229d_ : (g f : A \u2192 B) \u2192 fCh\n _f\u229d_ = \u03bb g f a da \u2192 g (a \u2295 da) \u229d f a\n\n f\u229d-fromto : \u2200 (f1 f2 : A \u2192 B) \u2192 fCh (f2 f\u229d f1) from f1 to f2\n f\u229d-fromto f1 f2 da a1 a2 daa\n rewrite sym (fromto\u2192\u2295 da a1 a2 daa) = \u229d-fromto (f1 a1) (f2 (a1 \u2295 da))\n\n _f\u229a[_]_ : fCh \u2192 (A \u2192 B) \u2192 fCh \u2192 fCh\n _f\u229a[_]_ df1 f df2 = \u03bb a da \u2192 (df1 a (nil a)) \u229a[ f a ] (df2 a da)\n\n f\u229a-fromto : \u2200 (f1 f2 f3 : A \u2192 B) (df1 df2 : fCh) \u2192 fCh df1 from f1 to f2 \u2192 fCh df2 from f2 to f3 \u2192\n fCh df1 f\u229a[ f1 ] df2 from f1 to f3\n f\u229a-fromto f1 f2 f3 df1 df2 dff1 dff2 da a1 a2 daa = \u229a-fromto (f1 a1) (f2 a1) (f3 a2) (df1 a1 (nil a1)) (df2 a1 da) (dff1 (nil a1) a1 a1 (nil-fromto a1)) (dff2 da a1 a2 daa)\n\n funCS = record\n { Ch = fCh\n ; ch_from_to_ =\n \u03bb df f1 f2 \u2192 \u2200 da (a1 a2 : A) (daa : ch da from a1 to a2) \u2192\n ch df a1 da from f1 a1 to f2 a2\n ; isCompChangeStructure = record\n { isChangeStructure = record\n { _\u2295_ = _f\u2295_\n ; fromto\u2192\u2295 = \u03bb df f1 f2 dff \u2192\n ext (\u03bb v \u2192\n fromto\u2192\u2295 (df v (nil v)) (f1 v) (f2 v) (dff (nil v) v v (nil-fromto v)))\n ; _\u229d_ = _f\u229d_\n ; \u229d-fromto = f\u229d-fromto\n }\n ; _\u229a[_]_ = _f\u229a[_]_\n ; \u229a-fromto = f\u229a-fromto\n }\n }\n\n -- Products\n private\n pCh = Ch A \u00d7 Ch B\n _p\u2295_ : A \u00d7 B \u2192 Ch A \u00d7 Ch B \u2192 A \u00d7 B\n _p\u2295_ (a , b) (da , db) = a \u2295 da , b \u2295 db\n _p\u229d_ : A \u00d7 B \u2192 A \u00d7 B \u2192 pCh\n _p\u229d_ (a2 , b2) (a1 , b1) = a2 \u229d a1 , b2 \u229d b1\n pch_from_to_ : pCh \u2192 A \u00d7 B \u2192 A \u00d7 B \u2192 Set\n pch (da , db) from (a1 , b1) to (a2 , b2) = ch da from a1 to a2 \u00d7 ch db from b1 to b2\n _p\u229a[_]_ : pCh \u2192 A \u00d7 B \u2192 pCh \u2192 pCh\n (da1 , db1) p\u229a[ a , b ] (da2 , db2) = da1 \u229a[ a ] da2 , db1 \u229a[ b ] db2\n pfromto\u2192\u2295 : \u2200 dp p1 p2 \u2192\n pch dp from p1 to p2 \u2192 p1 p\u2295 dp \u2261 p2\n pfromto\u2192\u2295 (da , db) (a1 , b1) (a2 , b2) (daa , dbb) =\n cong\u2082 _,_ (fromto\u2192\u2295 _ _ _ daa) (fromto\u2192\u2295 _ _ _ dbb)\n p\u229d-fromto : \u2200 (p1 p2 : A \u00d7 B) \u2192 pch p2 p\u229d p1 from p1 to p2\n p\u229d-fromto (a1 , b1) (a2 , b2) = \u229d-fromto a1 a2 , \u229d-fromto b1 b2\n p\u229a-fromto : \u2200 p1 p2 p3 dp1 dp2 \u2192\n pch dp1 from p1 to p2 \u2192 (pch dp2 from p2 to p3) \u2192 pch dp1 p\u229a[ p1 ] dp2 from p1 to p3\n p\u229a-fromto (a1 , b1) (a2 , b2) (a3 , b3) (da1 , db1) (da2 , db2)\n (daa1 , dbb1) (daa2 , dbb2) =\n \u229a-fromto a1 a2 a3 da1 da2 daa1 daa2 , \u229a-fromto b1 b2 b3 db1 db2 dbb1 dbb2\n\n instance\n pairCS : ChangeStructure (A \u00d7 B)\n pairCS = record\n { Ch = pCh\n ; ch_from_to_ = pch_from_to_\n ; isCompChangeStructure = record\n { isChangeStructure = record\n { _\u2295_ = _p\u2295_\n ; fromto\u2192\u2295 = pfromto\u2192\u2295\n ; _\u229d_ = _p\u229d_\n ; \u229d-fromto = p\u229d-fromto\n }\n ; _\u229a[_]_ = _p\u229a[_]_\n ; \u229a-fromto = p\u229a-fromto\n }\n }\n\n -- Sums\n private\n SumChange = (Ch A \u228e Ch B) \u228e (A \u228e B)\n\n data SumChange2 : Set where\n ch\u2081 : (da : Ch A) \u2192 SumChange2\n ch\u2082 : (db : Ch B) \u2192 SumChange2\n rp : (s : A \u228e B) \u2192 SumChange2\n\n convert : SumChange \u2192 SumChange2\n convert (inj\u2081 (inj\u2081 da)) = ch\u2081 da\n convert (inj\u2081 (inj\u2082 db)) = ch\u2082 db\n convert (inj\u2082 s) = rp s\n convert\u2081 : SumChange2 \u2192 SumChange\n convert\u2081 (ch\u2081 da) = inj\u2081 (inj\u2081 da)\n convert\u2081 (ch\u2082 db) = inj\u2081 (inj\u2082 db)\n convert\u2081 (rp s) = inj\u2082 s\n\n inv1 : \u2200 ds \u2192 convert\u2081 (convert ds) \u2261 ds\n inv1 (inj\u2081 (inj\u2081 da)) = refl\n inv1 (inj\u2081 (inj\u2082 db)) = refl\n inv1 (inj\u2082 s) = refl\n inv2 : \u2200 ds \u2192 convert (convert\u2081 ds) \u2261 ds\n inv2 (ch\u2081 da) = refl\n inv2 (ch\u2082 db) = refl\n inv2 (rp s) = refl\n\n private\n _s\u22952_ : A \u228e B \u2192 SumChange2 \u2192 A \u228e B\n _s\u22952_ (inj\u2081 a) (ch\u2081 da) = inj\u2081 (a \u2295 da)\n _s\u22952_ (inj\u2082 b) (ch\u2082 db) = inj\u2082 (b \u2295 db)\n _s\u22952_ (inj\u2082 b) (ch\u2081 da) = inj\u2082 b -- invalid\n _s\u22952_ (inj\u2081 a) (ch\u2082 db) = inj\u2081 a -- invalid\n _s\u22952_ s (rp s\u2081) = s\u2081\n\n _s\u2295_ : A \u228e B \u2192 SumChange \u2192 A \u228e B\n s s\u2295 ds = s s\u22952 (convert ds)\n\n _s\u229d2_ : A \u228e B \u2192 A \u228e B \u2192 SumChange2\n _s\u229d2_ (inj\u2081 x2) (inj\u2081 x1) = ch\u2081 (x2 \u229d x1)\n _s\u229d2_ (inj\u2082 y2) (inj\u2082 y1) = ch\u2082 (y2 \u229d y1)\n _s\u229d2_ s2 s1 = rp s2\n\n _s\u229d_ : A \u228e B \u2192 A \u228e B \u2192 SumChange\n s2 s\u229d s1 = convert\u2081 (s2 s\u229d2 s1)\n\n data sch_from_to_ : SumChange \u2192 A \u228e B \u2192 A \u228e B \u2192 Set where\n -- sft = Sum From To\n sft\u2081 : \u2200 {da a1 a2} (daa : ch da from a1 to a2) \u2192 sch (convert\u2081 (ch\u2081 da)) from (inj\u2081 a1) to (inj\u2081 a2)\n sft\u2082 : \u2200 {db b1 b2} (dbb : ch db from b1 to b2) \u2192 sch (convert\u2081 (ch\u2082 db)) from (inj\u2082 b1) to (inj\u2082 b2)\n sftrp\u2081 : \u2200 a1 b2 \u2192 sch (convert\u2081 (rp (inj\u2082 b2))) from (inj\u2081 a1) to (inj\u2082 b2)\n sftrp\u2082 : \u2200 b1 a2 \u2192 sch (convert\u2081 (rp (inj\u2081 a2))) from (inj\u2082 b1) to (inj\u2081 a2)\n\n sfromto\u2192\u22952 : (ds : SumChange2) (s1 s2 : A \u228e B) \u2192\n sch convert\u2081 ds from s1 to s2 \u2192 s1 s\u22952 ds \u2261 s2\n sfromto\u2192\u22952 (ch\u2081 da) (inj\u2081 a1) (inj\u2081 a2) (sft\u2081 daa) = cong inj\u2081 (fromto\u2192\u2295 _ _ _ daa)\n sfromto\u2192\u22952 (ch\u2082 db) (inj\u2082 b1) (inj\u2082 b2) (sft\u2082 dbb) = cong inj\u2082 (fromto\u2192\u2295 _ _ _ dbb)\n sfromto\u2192\u22952 (rp .(inj\u2082 y)) (inj\u2081 x) (inj\u2082 y) (sftrp\u2081 .x .y) = refl\n sfromto\u2192\u22952 (rp .(inj\u2081 x)) (inj\u2082 y) (inj\u2081 x) (sftrp\u2082 .y .x) = refl\n\n sfromto\u2192\u2295 : (ds : SumChange) (s1 s2 : A \u228e B) \u2192\n sch ds from s1 to s2 \u2192 s1 s\u2295 ds \u2261 s2\n sfromto\u2192\u2295 ds s1 s2 dss =\n sfromto\u2192\u22952 (convert ds) s1 s2\n (subst (sch_from s1 to s2) (sym (inv1 ds))\n dss)\n s\u229d-fromto : (s1 s2 : A \u228e B) \u2192 sch s2 s\u229d s1 from s1 to s2\n s\u229d-fromto (inj\u2081 a1) (inj\u2081 a2) = sft\u2081 (\u229d-fromto a1 a2)\n s\u229d-fromto (inj\u2081 a1) (inj\u2082 b2) = sftrp\u2081 a1 b2\n s\u229d-fromto (inj\u2082 b1) (inj\u2081 a2) = sftrp\u2082 b1 a2\n s\u229d-fromto (inj\u2082 b1) (inj\u2082 b2) = sft\u2082 (\u229d-fromto b1 b2)\n instance\n sumCS : ChangeStructure (A \u228e B)\n sumCS = record\n { Ch = SumChange\n ; ch_from_to_ = sch_from_to_\n ; isCompChangeStructure = IsChangeStructure\u2192IsCompChangeStructure (record\n { _\u2295_ = _s\u2295_\n ; fromto\u2192\u2295 = sfromto\u2192\u2295\n ; _\u229d_ = _s\u229d_\n ; \u229d-fromto = s\u229d-fromto\n })\n }\n\nopen import Data.Integer\nopen import Data.Unit\nopen import Theorem.Groups-Nehemiah\n\nprivate\n intCh = \u2124\ninstance\n intCS : ChangeStructure \u2124\nintCS = record\n { Ch = \u2124\n ; ch_from_to_ = \u03bb dv v1 v2 \u2192 v1 + dv \u2261 v2\n ; isCompChangeStructure = record\n { isChangeStructure = record\n { _\u2295_ = _+_\n ; fromto\u2192\u2295 = \u03bb dv v1 v2 v2\u2261v1+dv \u2192 v2\u2261v1+dv\n ; _\u229d_ = _-_\n ; \u229d-fromto = \u03bb a b \u2192 n+[m-n]=m {a} {b}\n }\n ; _\u229a[_]_ = \u03bb da1 a da2 \u2192 da1 + da2\n ; \u229a-fromto = i\u229a-fromto\n }\n }\n where\n i\u229a-fromto : (a1 a2 a3 : \u2124) (da1 da2 : intCh) \u2192\n a1 + da1 \u2261 a2 \u2192 a2 + da2 \u2261 a3 \u2192 a1 + (da1 + da2) \u2261 a3\n i\u229a-fromto a1 a2 a3 da1 da2 a1+da1\u2261a2 a2+da2\u2261a3\n rewrite sym (associative-int a1 da1 da2) | a1+da1\u2261a2 = a2+da2\u2261a3\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"57dd579828aa8a7c7ff8b0546dd51520584eb18e","subject":"agda: add extended comment on semantics of \u0394","message":"agda: add extended comment on semantics of \u0394\n\nDuring my walk-throughs on the code, explaining this line was a bit\nhard and required a small example. After writing it here during the\nwalkthrough, I decided to extend it and save it.\n\nOld-commit-hash: 279b6047512b8c934cae49bda5977fa104077e12\n","repos":"inc-lc\/ilc-agda","old_file":"Denotational\/Evaluation\/Total.agda","new_file":"Denotational\/Evaluation\/Total.agda","new_contents":"module Denotational.Evaluation.Total where\n\n-- EVALUATION with a primitive for TOTAL DERIVATIVES\n--\n-- This module defines the semantics of terms that support a\n-- primitive (\u0394 e) for computing the total derivative according\n-- to all free variables in e and all future arguments of e if e\n-- is a function.\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\n\nopen import Changes\nopen import ChangeContexts\n\n-- TERMS\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 true \u27e7Term \u03c1 = true\n\u27e6 false \u27e7Term \u03c1 = false\n\u27e6 if t\u2081 t\u2082 t\u2083 \u27e7Term \u03c1 = if \u27e6 t\u2081 \u27e7Term \u03c1 then \u27e6 t\u2082 \u27e7Term \u03c1 else \u27e6 t\u2083 \u27e7Term \u03c1\n\u27e6 \u0394 {{\u0393\u2032}} t \u27e7Term \u03c1 = diff (\u27e6 t \u27e7Term (update (\u27e6 \u0393\u2032 \u27e7 \u03c1))) (\u27e6 t \u27e7Term (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n\n{-\n\nHere is an example to understand the semantics of \u0394. I will use a\nnamed variable representation for the task.\n\nConsider the typing judgment:\n\n x: T |- x: T\n\nThus, we have that:\n\n dx : \u0394 T, x: T |- \u0394 x : \u0394 T\n\nThanks to weakening, we also have:\n\n y : S, dx : \u0394 T, x: T |- \u0394 x : \u0394 T\n\nIn the formalization, we need a proof \u0393\u2032 that the context \u0393\u2081 = dx : \u0394\nT, x: T is a subcontext of \u0393\u2082 = y : S, dx : \u0394 T, x: T. Thus, \u0393\u2032 has\ntype \u0393\u2081 \u227c \u0393\u2082.\n\nNow take the environment:\n\n \u03c1 = y \u21a6 w, dx \u21a6 dv, x \u21a6 v\n\nSince the semantics of \u0393\u2032 : \u0393\u2081 \u227c \u0393\u2082 is a function from environments\nfor \u0393\u2082 to environments for \u0393\u2081, we have that:\n\n \u27e6 \u0393\u2032 \u27e7 \u03c1 = dx \u21a6 dv, x \u21a6 v\n\nFrom the definitions of update and ignore, it follows that:\n\n update (\u27e6 \u0393\u2032 \u27e7 \u03c1) = x \u21a6 dv \u2295 v\n ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1) = x \u21a6 v\n\nHence, finally, we have that:\n\n diff (\u27e6 t \u27e7Term (update (\u27e6 \u0393\u2032 \u27e7 \u03c1))) (\u27e6 t \u27e7Term (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n\nis simply diff (dv \u2295 v) v (or (dv \u2295 v) \u229d v). If dv is a valid change,\nthat's just dv, that is \u27e6 dx \u27e7 \u03c1. In other words\n\n-}\n\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n-- PROPERTIES of WEAKENING\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (t : Term \u0393\u2081 \u03c4) {\u0393\u2032 : \u0393\u2081 \u227c \u0393\u2082} \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken \u0393\u2032 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 \u0393\u2032 \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = ext (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = \u2261-app (weaken-sound t\u2081 \u03c1) (weaken-sound t\u2082 \u03c1)\nweaken-sound (var x) \u03c1 = lift-sound _ x \u03c1\nweaken-sound true \u03c1 = refl\nweaken-sound false \u03c1 = refl\nweaken-sound (if t\u2081 t\u2082 t\u2083) {\u0393\u2032} \u03c1 with weaken-sound t\u2081 {\u0393\u2032} \u03c1\n... | H with \u27e6 weaken \u0393\u2032 t\u2081 \u27e7 \u03c1 | \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u2032 \u27e7 \u03c1)\nweaken-sound (if t\u2081 t\u2082 t\u2083) {\u0393\u2032} \u03c1 | refl | true | true = weaken-sound t\u2082 {\u0393\u2032} \u03c1\nweaken-sound (if t\u2081 t\u2082 t\u2083) {\u0393\u2032} \u03c1 | refl | false | false = weaken-sound t\u2083 {\u0393\u2032} \u03c1\nweaken-sound (\u0394 {{\u0393\u2032}} t) {\u0393\u2033} \u03c1 =\n cong (\u03bb x \u2192 diff (\u27e6 t \u27e7 (update x)) (\u27e6 t \u27e7 (ignore x))) (\u27e6\u27e7-\u227c-trans \u0393\u2032 \u0393\u2033 \u03c1)\n","old_contents":"module Denotational.Evaluation.Total where\n\n-- EVALUATION with a primitive for TOTAL DERIVATIVES\n--\n-- This module defines the semantics of terms that support a\n-- primitive (\u0394 e) for computing the total derivative according\n-- to all free variables in e and all future arguments of e if e\n-- is a function.\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\n\nopen import Changes\nopen import ChangeContexts\n\n-- TERMS\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 true \u27e7Term \u03c1 = true\n\u27e6 false \u27e7Term \u03c1 = false\n\u27e6 if t\u2081 t\u2082 t\u2083 \u27e7Term \u03c1 = if \u27e6 t\u2081 \u27e7Term \u03c1 then \u27e6 t\u2082 \u27e7Term \u03c1 else \u27e6 t\u2083 \u27e7Term \u03c1\n\u27e6 \u0394 {{\u0393\u2032}} t \u27e7Term \u03c1 = diff (\u27e6 t \u27e7Term (update (\u27e6 \u0393\u2032 \u27e7 \u03c1))) (\u27e6 t \u27e7Term (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n-- PROPERTIES of WEAKENING\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (t : Term \u0393\u2081 \u03c4) {\u0393\u2032 : \u0393\u2081 \u227c \u0393\u2082} \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken \u0393\u2032 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 \u0393\u2032 \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = ext (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = \u2261-app (weaken-sound t\u2081 \u03c1) (weaken-sound t\u2082 \u03c1)\nweaken-sound (var x) \u03c1 = lift-sound _ x \u03c1\nweaken-sound true \u03c1 = refl\nweaken-sound false \u03c1 = refl\nweaken-sound (if t\u2081 t\u2082 t\u2083) {\u0393\u2032} \u03c1 with weaken-sound t\u2081 {\u0393\u2032} \u03c1\n... | H with \u27e6 weaken \u0393\u2032 t\u2081 \u27e7 \u03c1 | \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u2032 \u27e7 \u03c1)\nweaken-sound (if t\u2081 t\u2082 t\u2083) {\u0393\u2032} \u03c1 | refl | true | true = weaken-sound t\u2082 {\u0393\u2032} \u03c1\nweaken-sound (if t\u2081 t\u2082 t\u2083) {\u0393\u2032} \u03c1 | refl | false | false = weaken-sound t\u2083 {\u0393\u2032} \u03c1\nweaken-sound (\u0394 {{\u0393\u2032}} t) {\u0393\u2033} \u03c1 =\n cong (\u03bb x \u2192 diff (\u27e6 t \u27e7 (update x)) (\u27e6 t \u27e7 (ignore x))) (\u27e6\u27e7-\u227c-trans \u0393\u2032 \u0393\u2033 \u03c1)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"bd2e108c643b5ec0da84a5746e9e2a4410b98970","subject":"elgamal cleanups and improvments","message":"elgamal cleanups and improvments\n","repos":"crypto-agda\/crypto-agda","old_file":"elgamal.agda","new_file":"elgamal.agda","new_contents":"{-# OPTIONS --copatterns #-}\nopen import Function\nopen import Data.Product\nopen import Data.Bool.NP as Bool\nopen import Data.Unit\nopen import Data.Maybe.NP\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc) renaming (#_ to ##_)\nopen import Data.Nat.NP hiding (_^_)\nopen import Data.Bits\nimport Data.Vec.NP as Vec\nopen Vec using (Vec; []; _\u2237_; _\u2237\u02b3_; allFin; lookup) renaming (map to vmap)\nopen import Algebra.FunctionProperties\nopen import Relation.Binary.PropositionalEquality as \u2261\nimport cont as cont\nopen cont using (Cont; ContA)\n\nmodule elgamal where\n\nprivate\n \u2605\u2081 : Set\u2082\n \u2605\u2081 = Set\u2081\n\n \u2605 : \u2605\u2081\n \u2605 = Set\n\n[0\u2192_,1\u2192_] : \u2200 {a} {A : Set a} \u2192 A \u2192 A \u2192 Bit \u2192 A\n[0\u2192 e\u2080 ,1\u2192 e\u2081 ] b = if b then e\u2081 else e\u2080\n\ncase_0\u2192_1\u2192_ : \u2200 {a} {A : Set a} \u2192 Bit \u2192 A \u2192 A \u2192 A\ncase b 0\u2192 e\u2080 1\u2192 e\u2081 = if b then e\u2081 else e\u2080\n\nmodule Sum where\n Sum : \u2605 \u2192 \u2605\n Sum A = (A \u2192 \u2115) \u2192 \u2115 \n\n Count : \u2605 \u2192 \u2605\n Count A = (A \u2192 Bit) \u2192 \u2115 \n\n SumExt : \u2200 {A} \u2192 Sum A \u2192 \u2605\n SumExt sumA = \u2200 {f g} \u2192 f \u2257 g \u2192 sumA f \u2261 sumA g\n\n sumToCount : \u2200 {A} \u2192 Sum A \u2192 Count A\n sumToCount sumA f = sumA (Bool.to\u2115 \u2218 f)\n\n sum\u22a4 : Sum \u22a4\n sum\u22a4 f = f _\n\n sum\u22a4-ext : SumExt sum\u22a4\n sum\u22a4-ext f\u2257g = f\u2257g _\n\n sumBit : Sum Bit\n sumBit f = f 0b + f 1b\n\n sumBit-ext : SumExt sumBit\n sumBit-ext f\u2257g rewrite f\u2257g 0b | f\u2257g 1b = refl\n\n -- liftM2 _,_ in the continuation monad\n sumProd : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u00d7 B)\n sumProd sumA sumB f = sumA (\u03bb x\u2080 \u2192\n sumB (\u03bb x\u2081 \u2192\n f (x\u2080 , x\u2081)))\n\n sumProd-ext : \u2200 {A B} {sumA : Sum A} {sumB : Sum B} \u2192\n SumExt sumA \u2192 SumExt sumB \u2192 SumExt (sumProd sumA sumB)\n sumProd-ext sumA-ext sumB-ext f\u2257g = sumA-ext (\u03bb x \u2192 sumB-ext (\u03bb y \u2192 f\u2257g (x , y)))\n\ndata `\u2605 : \u2605 where\n `\u22a4 : `\u2605\n `X : `\u2605\n _`\u00d7_ : `\u2605 \u2192 `\u2605 \u2192 `\u2605\ninfixr 2 _`\u00d7_\n\nmodule Univ (X : \u2605) where\n El : `\u2605 \u2192 \u2605\n El `\u22a4 = \u22a4\n El `X = X\n El (u\u2080 `\u00d7 u\u2081) = El u\u2080 \u00d7 El u\u2081\n\n record \u21ba (R : `\u2605) (A : \u2605) : \u2605 where\n constructor mk\n field\n run\u21ba : El R \u2192 A\n open \u21ba public\n\n EXP : (R : `\u2605) \u2192 \u2605\n EXP R = \u21ba R Bit\n\n Det : \u2605 \u2192 \u2605\n Det = \u21ba `\u22a4\n\nmodule EntropySmoothing\n (M : \u2605) -- Message\n (Hash : \u2605)\n (\u210b : M \u2192 Hash) -- Hashing function\n (R\u2090 : \u2605) -- Adversary randomness\n where\n\n -- Entropy smoothing adversary\n ESAdv : \u2605\n ESAdv = R\u2090 \u2192 Hash \u2192 Bit\n\n -- The randomness universe needed for the following games\n R : \u2605\n R = M \u00d7 Hash \u00d7 R\u2090\n\n -- In this game we always use \u210b on a random message\n ES\u2141\u2080 : ESAdv \u2192 R \u2192 Bit\n ES\u2141\u2080 A (m , _ , r\u2090) = A r\u2090 (\u210b m)\n\n -- In this game we just retrun a random Hash value\n ES\u2141\u2081 : ESAdv \u2192 R \u2192 Bit\n ES\u2141\u2081 A (_ , h , r\u2090) = A r\u2090 h\n\n ES\u2141 : ESAdv \u2192 Bit \u2192 R \u2192 Bit\n ES\u2141 A b r = (case b 0\u2192 ES\u2141\u2080 1\u2192 ES\u2141\u2081) A r\n\nmodule EntropySmoothingWithKey\n (M : \u2605)\n (Key : \u2605)\n (Hash : \u2605)\n (\u210b : Key \u2192 M \u2192 Hash) -- Hashing function\n (R\u2090 : \u2605) -- Adversary randomness\n where\n\n -- Entropy smoothing adversary\n ESAdv : \u2605\n ESAdv = R\u2090 \u2192 Key \u2192 Hash \u2192 Bit\n\n -- The randomness universe needed for the following games\n R : \u2605\n R = Key \u00d7 M \u00d7 Hash \u00d7 R\u2090\n\n -- In this game we always use \u210b on a random message\n ES\u2141\u2080 : ESAdv \u2192 R \u2192 Bit\n ES\u2141\u2080 A (k , m , _ , r\u2090) = A r\u2090 k (\u210b k m)\n\n -- In this game we just retrun a random Hash value\n ES\u2141\u2081 : ESAdv \u2192 R \u2192 Bit\n ES\u2141\u2081 A (k , _ , h , r\u2090) = A r\u2090 k h\n\n ES\u2141 : ESAdv \u2192 Bit \u2192 R \u2192 Bit\n ES\u2141 A b r = (case b 0\u2192 ES\u2141\u2080 1\u2192 ES\u2141\u2081) A r\n\nmodule \u2124q-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (sum\u2124q : Sum.Sum \u2124q)\n (sum\u2124q-ext : Sum.SumExt sum\u2124q)\n (sum\u2124q-\u229e-lem : \u2200 x f \u2192 sum\u2124q (f \u2218 _\u229e_ x) \u2261 sum\u2124q f)\n where\n\n open Sum\n open Univ \u2124q public\n open `\u2605 public renaming (`X to `\u2124q)\n\n sum : \u2200 u \u2192 Sum (El u)\n sum `\u22a4 = sum\u22a4\n sum `X = sum\u2124q\n sum (u\u2080 `\u00d7 u\u2081) = sumProd (sum u\u2080) (sum u\u2081)\n\n sum-ext : \u2200 u \u2192 SumExt (sum u)\n sum-ext `\u22a4 = sum\u22a4-ext\n sum-ext `X = sum\u2124q-ext\n sum-ext (u\u2080 `\u00d7 u\u2081) = sumProd-ext (sum-ext u\u2080) (sum-ext u\u2081)\n\n #_ : \u2200 {u} \u2192 \u21ba u Bit \u2192 \u2115\n #_ {u} f = sum u (Bool.to\u2115 \u2218 run\u21ba f)\n\n #q_ : Count \u2124q\n #q_ = sumToCount sum\u2124q\n\n \u2047 : \u2200 R \u2192 \u21ba R (El R)\n run\u21ba (\u2047 _) = id\n\n pure\u21ba : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n run\u21ba (pure\u21ba x) r = x -- turning r to _ produce an error\n\n \u27ea_\u27eb : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n \u27ea_\u27eb = pure\u21ba\n\n {-\n \u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n \u27ea_\u27eb\u1d30 = pure\u1d30\n -}\n\n map\u21ba : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n run\u21ba (map\u21ba f x) r = f (run\u21ba x r)\n\n infixl 4 _\u229b_\n _\u229b_ : \u2200 {R S A B} \u2192 \u21ba R (A \u2192 B) \u2192 \u21ba S A \u2192 \u21ba (R `\u00d7 S) B\n run\u21ba (af \u229b ax) rs = run\u21ba af (proj\u2081 rs) (run\u21ba ax (proj\u2082 rs))\n\n \u27ea_\u00b7_\u27eb : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n \u27ea f \u00b7 x \u27eb = map\u21ba f x\n\n \u27ea_\u00b7_\u00b7_\u27eb : \u2200 {A B C} {R S} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba R A \u2192 \u21ba S B \u2192 \u21ba (R `\u00d7 S) C\n \u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n _\u27e8\u229e\u27e9_ : \u2200 {R S} \u2192 \u21ba R \u2124q \u2192 \u21ba S \u2124q \u2192 \u21ba (R `\u00d7 S) \u2124q\n x \u27e8\u229e\u27e9 y = \u27ea _\u229e_ \u00b7 x \u00b7 y \u27eb\n\n \u27e8_\u229e\u27e9_ : \u2200 {R} \u2192 \u2124q \u2192 \u21ba R \u2124q \u2192 \u21ba R \u2124q\n \u27e8 x \u229e\u27e9 y = \u27ea _\u229e_ x \u00b7 y \u27eb\n\n infix 4 _\u2248\u21ba_ _\u2248\u1d2c_\n _\u2248\u21ba_ : \u2200 {R : `\u2605} (f g : EXP R) \u2192 \u2605\n _\u2248\u21ba_ = _\u2261_ on #_\n\n _\u2248\u1d2c_ : \u2200 {A R} (f g : \u21ba R A) \u2192 Set _\n _\u2248\u1d2c_ {A} f g = \u2200 (Adv : A \u2192 Bit) \u2192 \u27ea Adv \u00b7 f \u27eb \u2248\u21ba \u27ea Adv \u00b7 g \u27eb\n\n lem : \u2200 x \u2192 \u27e8 x \u229e\u27e9 (\u2047 `\u2124q) \u2248\u1d2c \u2047 _ \n lem x Adv = sum\u2124q-\u229e-lem x (Bool.to\u2115 \u2218 Adv)\n\n -- \u2200 (A : \u2124q \u2192 Bit) \u2192 # (A \u2047) \n\nopen Fin.Modulo renaming (sucmod to [suc])\n\n{-\nmodule \u2124q-implem (q-2 : \u2115) where\n q : \u2115\n q = 2 + q-2\n\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = zero\n\n [1] : \u2124q\n [1] = suc zero\n-}\nmodule \u2124q-implem (q : \u2115) ([0]' [1]' : Fin q) where\n open Sum\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = [0]'\n\n [1] : \u2124q\n [1] = [1]'\n\n _\u2115\u229e_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = m \u2115\u229e ([suc] n)\n {-\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = [suc] (m \u2115\u229e n)\n -}\n\n _\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u229e n = Fin.to\u2115 m \u2115\u229e n\n\n _\u2115\u22a0_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u22a0 n = [0]\n suc m \u2115\u22a0 n = n \u229e (m \u2115\u22a0 n)\n\n _\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u22a0 n = Fin.to\u2115 m \u2115\u22a0 n\n\n _[^]\u2115_ : \u2124q \u2192 \u2115 \u2192 \u2124q\n m [^]\u2115 zero = [1]\n m [^]\u2115 suc n = m \u22a0 (m [^]\u2115 n)\n\n _[^]_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m [^] n = m [^]\u2115 (Fin.to\u2115 n)\n\n all\u2124q : Vec \u2124q q\n all\u2124q = allFin q\n\n sum\u2124q : Sum \u2124q\n sum\u2124q f = Vec.sum (vmap f all\u2124q)\n\n sum\u2124q-[suc]-lem : \u2200 f \u2192 sum\u2124q (f \u2218 [suc]) \u2261 sum\u2124q f\n sum\u2124q-[suc]-lem f rewrite \u2261.sym (Vec.sum-map-rot\u2081 f all\u2124q)\n | Vec.map-\u2218 f [suc] all\u2124q\n | rot\u2081-map-sucmod q\n = refl\n\n -- comm-[suc]-\u2115\u229e : \u2200 m n \u2192 [suc] (m \u2115\u229e n) \u2261 m \u2115\u229e ([suc] n)\n\n sum\u2124q-\u2115\u229e-lem : \u2200 m f \u2192 sum\u2124q (f \u2218 _\u2115\u229e_ m) \u2261 sum\u2124q f\n sum\u2124q-\u2115\u229e-lem zero f = refl\n sum\u2124q-\u2115\u229e-lem (suc m) f rewrite sum\u2124q-[suc]-lem (f \u2218 _\u2115\u229e_ m)\n | sum\u2124q-\u2115\u229e-lem m f = refl\n\n sum\u2124q-\u229e-lem : \u2200 m f \u2192 sum\u2124q (f \u2218 _\u229e_ m) \u2261 sum\u2124q f\n sum\u2124q-\u229e-lem = sum\u2124q-\u2115\u229e-lem \u2218 Fin.to\u2115\n\n vmap-ext : \u2200 {A B : \u2605} {f g : A \u2192 B} {n} \u2192 f \u2257 g \u2192 vmap f \u2257 vmap {n = n} g\n vmap-ext f\u2257g [] = refl\n vmap-ext f\u2257g (x \u2237 xs) rewrite f\u2257g x | vmap-ext f\u2257g xs = refl\n\n sum\u2124q-ext : SumExt sum\u2124q\n sum\u2124q-ext f\u2257g rewrite vmap-ext f\u2257g all\u2124q = refl\n\nmodule G-implem (p q : \u2115) (g' : Fin p) (0[p] 1[p] : Fin p) (0[q] 1[q] : Fin q) where\n open \u2124q-implem q 0[q] 1[q] public\n open \u2124q-implem p 0[p] 1[p] public using () renaming (\u2124q to G; _\u22a0_ to _\u2219_; _[^]\u2115_ to _^[p]_)\n\n g : G\n g = g'\n\n _^_ : G \u2192 \u2124q \u2192 G\n x ^ n = x ^[p] Fin.to\u2115 n\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n {-\n g^-inj : \u2200 m n \u2192 g^ m \u2261 g^ n \u2192 m \u2261 n\n g^-inj = {!!}\n -}\n\nmodule G-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (sum\u2124q : Sum.Sum \u2124q)\n (sum\u2124q-ext : Sum.SumExt sum\u2124q)\n (sum\u2124q-\u229e-lem : \u2200 x f \u2192 sum\u2124q (f \u2218 _\u229e_ x) \u2261 sum\u2124q f)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n where\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n open Sum\n open \u2124q-count \u2124q _\u229e_ sum\u2124q sum\u2124q-ext sum\u2124q-\u229e-lem\n\n \u2047G : \u21ba `\u2124q G\n run\u21ba \u2047G x = g^ x\n\n #G : Count G\n #G f = #q (f \u2218 g^_)\n\n {-\n #G-\u2219 : \u2200 f m \u2192 #G (f \u2218 _\u2219_ m) \u2261 #G f\n #G-\u2219 f m = {!!}\n -}\n\nmodule DDH\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g^_ : \u2124q \u2192 G)\n where\n DDHAdv : \u2605 \u2192 \u2605\n DDHAdv R = R \u2192 G \u2192 G \u2192 G \u2192 Bit\n\n DDH\u2141\u2080 : \u2200 {R} {_I : \u2605} \u2192 DDHAdv R \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n DDH\u2141\u2080 D (r , x , y , _) = D r (g^ x) (g^ y) (g^ (x \u22a0 y))\n\n DDH\u2141\u2081 : \u2200 {R} \u2192 DDHAdv R \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n DDH\u2141\u2081 D (r , x , y , z) = D r (g^ x) (g^ y) (g^ z)\n\n DDH\u2141 : \u2200 {R} \u2192 DDHAdv R \u2192 Bit \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n DDH\u2141 D b = (case b 0\u2192 DDH\u2141\u2080 1\u2192 DDH\u2141\u2081) D\n\n -- DDH\u2141\u2032 : \u2200 {R} \u2192 DDHAdv R \u2192 (Bit \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q \u00d7 R) \u2192 Bit\n -- DDH\u2141\u2032 D (b , x , y , z , r) = DDH\u2141 D b (x , y , z , r)\n\n module With\u21ba where\n open Univ \u2124q\n open `\u2605 public renaming (`X to `\u2124q)\n DDHAdv\u21ba : `\u2605 \u2192 \u2605\n DDHAdv\u21ba R = G \u2192 G \u2192 G \u2192 \u21ba R Bit\n DDH\u2141\u2080\u21ba : \u2200 {R _I} \u2192 DDHAdv\u21ba R \u2192 \u21ba (R `\u00d7 `\u2124q `\u00d7 `\u2124q `\u00d7 _I) Bit\n run\u21ba (DDH\u2141\u2080\u21ba D) = DDH\u2141\u2080 (\u03bb a b c d \u2192 run\u21ba (D b c d) a)\n\nmodule El-Gamal-Generic\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (Message : \u2605)\n (_\u2295_ : G \u2192 Message \u2192 Message)\n (_\u2295\u207b\u00b9_ : G \u2192 Message \u2192 Message)\n where\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n -- g\u02e3 is the pk\n -- x is the sk\n\n PubKey = G\n SecKey = \u2124q\n KeyPair = PubKey \u00d7 SecKey\n CipherText = G \u00d7 Message\n\n M = Message\n C = CipherText\n\n KeyGen : \u2124q \u2192 KeyPair\n KeyGen x = (g^ x , x)\n\n -- KeyGen\u21ba : \u21ba \u2124q KeyPair\n -- KeyGen\u21ba = mk KeyGen\n\n Enc : PubKey \u2192 Message \u2192 \u2124q \u2192 CipherText\n Enc g\u02e3 m y = g\u02b8 , \u03b6 where\n g\u02b8 = g^ y\n \u03b4 = g\u02e3 ^ y\n \u03b6 = \u03b4 \u2295 m\n\n -- Enc\u21ba : PubKey \u2192 Message \u2192 \u21ba \u2124q CipherText\n -- Enc\u21ba g\u02e3 m = mk (Enc g\u02e3 m)\n\n Dec : SecKey \u2192 CipherText \u2192 Message\n Dec x (g\u02b8 , \u03b6) = (g\u02b8 ^ x) \u2295\u207b\u00b9 \u03b6\n\n EncAdv : \u2605 \u2192 \u2605\n EncAdv R = (R \u2192 PubKey \u2192 Bit \u2192 M) \u00d7 (R \u2192 PubKey \u2192 C \u2192 Bit)\n\n SS\u2141 : \u2200 {R _I : \u2605} \u2192 EncAdv R \u2192 Bit \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n SS\u2141 (m , D) b (r , x , y , z) =\n let pk = proj\u2081 (KeyGen x) in\n D r pk (Enc pk (m r pk b) y)\n\n Game : (i : Bit) \u2192 \u2200 {R} \u2192 EncAdv R \u2192 (Bit \u00d7 R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n Game i (m , D) (b , r , x , y , z) =\n let g\u02e3 = g^ x\n g\u02b8 = g^ y\n \u03b4 = g\u02e3 ^ case i 0\u2192 y 1\u2192 z\n \u03b6 = \u03b4 \u2295 m r g\u02e3 b\n in b ==\u1d47 D r g\u02e3 (g\u02b8 , \u03b6)\n\n {-\n Game-0b\u2261Game0 : \u2200 {R} \u2192 Game 0b \u2261 Game0 {R}\n Game-0b\u2261Game0 = refl\n -}\n\n open DDH \u2124q _\u22a0_ G g^_ public\n\n -- Game0 \u2248 Game 0b\n -- Game1 = Game 1b\n\n -- Game0 \u2264 \u03b5\n -- Game1 \u2261 0\n\n OTP\u2141 : \u2200 {R : \u2605} \u2192 (R \u2192 G \u2192 Message) \u2192 (R \u2192 G \u2192 G \u2192 Message \u2192 Bit)\n \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n OTP\u2141 M D (r , x , y , z) = D r g\u02e3 g\u02b8 (g\u1dbb \u2295 M r g\u02e3)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n g\u1dbb = g^ z\n\n TrA : \u2200 {R} \u2192 Bit \u2192 EncAdv R \u2192 DDHAdv R\n TrA b (m , D) r g\u02e3 g\u02b8 g\u02e3\u02b8 = D r g\u02e3 (g\u02b8 , g\u02e3\u02b8 \u2295 m r g\u02e3 b)\n\nmodule El-Gamal-Base\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u207b\u00b9 : G \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n where\n\n _\/_ : G \u2192 G \u2192 G\n x \/ y = x \u2219 (y \u207b\u00b9)\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ G _\u2219_ (flip _\/_) public\n\n like-SS\u2141 : \u2200 {R _I : \u2605} \u2192 EncAdv R \u2192 Bit \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n like-SS\u2141 (m , D) b (r , x , y , _z) =\n D r g\u02e3 (g\u02b8 , (g\u02e3 ^ y) \u2219 m r g\u02e3 b)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n\n SS\u2141\u2261like-SS\u2141 : \u2200 {R _I} \u2192 SS\u2141 {R} {_I} \u2261 like-SS\u2141\n SS\u2141\u2261like-SS\u2141 = refl\n\n open Sum\n module WithSumRAnd\u2124qProps\n (R\u2090 : \u2605)\n (sumR\u2090 : Sum R\u2090)\n (sumR\u2090-ext : SumExt sumR\u2090)\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (sum\u2124q : Sum \u2124q)\n (sum\u2124q-ext : SumExt sum\u2124q)\n where\n\n module SumR where\n R = R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q\n sumR : Sum R\n sumR = sumProd sumR\u2090 (sumProd sum\u2124q (sumProd sum\u2124q sum\u2124q))\n #R_ : Count R\n #R_ = sumToCount sumR\n sumR-ext : SumExt sumR\n sumR-ext = sumProd-ext sumR\u2090-ext (sumProd-ext sum\u2124q-ext (sumProd-ext sum\u2124q-ext sum\u2124q-ext))\n private\n #q_ : Count \u2124q\n #q_ = sumToCount sum\u2124q\n\n open SumR\n\n _\u2248q_ : (f g : \u2124q \u2192 Bit) \u2192 \u2605\n f \u2248q g = #q f \u2261 #q g\n\n _\u2248R_ : (f g : R \u2192 Bit) \u2192 \u2605\n f \u2248R g = #R f \u2261 #R g\n\n module EvenMoreProof\n (ddh-hyp : (A : DDHAdv R\u2090) \u2192 DDH\u2141 A 0b \u2248R DDH\u2141 A 1b)\n (otp-lem : \u2200 (A : G \u2192 Bit) m \u2192 (\u03bb x \u2192 A (g^ x \u2219 m)) \u2248q (\u03bb x \u2192 A (g^ x)))\n where\n\n otp-lem' : \u2200 D M\u2080 M\u2081 \u2192 OTP\u2141 M\u2080 D \u2248R OTP\u2141 M\u2081 D\n otp-lem' D M\u2080 M\u2081 = sumR\u2090-ext (\u03bb r \u2192\n sum\u2124q-ext (\u03bb x \u2192\n sum\u2124q-ext (\u03bb y \u2192\n pf r x y)))\n where\n f0 = \u03bb M r x y z \u2192 OTP\u2141 M D (r , x , y , z)\n f1 = \u03bb M r x y \u2192 sum\u2124q (Bool.to\u2115 \u2218 f0 M r x y)\n f2 = \u03bb M r x \u2192 sum\u2124q (f1 M r x)\n f3 = \u03bb M r \u2192 sum\u2124q (f2 M r)\n pf : \u2200 r x y \u2192 f1 M\u2080 r x y \u2261 f1 M\u2081 r x y\n pf r x y = pf'\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n m\u2080 = M\u2080 r g\u02e3\n m\u2081 = M\u2081 r g\u02e3\n f5 = \u03bb M z \u2192 D r g\u02e3 g\u02b8 (g^ z \u2219 M)\n pf' : f5 m\u2080 \u2248q f5 m\u2081\n pf' rewrite otp-lem (D r g\u02e3 g\u02b8) m\u2080\n | otp-lem (D r g\u02e3 g\u02b8) m\u2081 = refl\n\n projM : EncAdv R\u2090 \u2192 Bit \u2192 R\u2090 \u2192 G \u2192 G\n projM (m , _) b r g\u02e3 = m r g\u02e3 b\n\n projD : EncAdv R\u2090 \u2192 R\u2090 \u2192 G \u2192 G \u2192 G \u2192 Bit\n projD (_ , D) r g\u02e3 g\u02b8 g\u1dbb\u2219M = D r g\u02e3 (g\u02b8 , g\u1dbb\u2219M)\n\n module WithAdversary (A : EncAdv R\u2090) b where\n A\u1d47 = TrA b A\n A\u00ac\u1d47 = TrA (not b) A\n\n pf0,5 : SS\u2141 A b \u2257 DDH\u2141 A\u1d47 0b\n pf0,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n pf1 : SS\u2141 A b \u2248R DDH\u2141 A\u1d47 0b\n pf1 = sumR-ext (cong Bool.to\u2115 \u2218 pf0,5)\n\n pf2 : DDH\u2141 A\u1d47 0b \u2248R DDH\u2141 A\u1d47 1b\n pf2 = ddh-hyp A\u1d47\n\n pf2,5 : DDH\u2141 A\u1d47 1b \u2261 OTP\u2141 (projM A b) (projD A)\n pf2,5 = refl\n\n pf3 : DDH\u2141 A\u1d47 1b \u2248R DDH\u2141 A\u00ac\u1d47 1b\n pf3 = otp-lem' (projD A) (projM A b) (projM A (not b))\n\n pf4 : DDH\u2141 A\u00ac\u1d47 1b \u2248R DDH\u2141 A\u00ac\u1d47 0b\n pf4 = \u2261.sym (ddh-hyp A\u00ac\u1d47)\n\n pf4,5 : SS\u2141 A (not b) \u2257 DDH\u2141 A\u00ac\u1d47 0b\n pf4,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n pf5 : SS\u2141 A (not b) \u2248R DDH\u2141 A\u00ac\u1d47 0b\n pf5 = sumR-ext (cong Bool.to\u2115 \u2218 pf4,5)\n\n final : SS\u2141 A b \u2248R SS\u2141 A (not b)\n final rewrite pf1 | pf2 | pf3 | pf4 | pf5 = refl\n\nmodule El-Gamal-Hashed\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n -- (HKey : \u2605)\n (|M| : \u2115)\n (\u210b : {-HKey \u2192-} G \u2192 Bits |M|) where\n\n Message = Bits |M|\n\n \u210b\u27e8_\u27e9\u2295_ : G \u2192 Message \u2192 Message\n \u210b\u27e8 \u03b4 \u27e9\u2295 m = \u210b \u03b4 \u2295 m\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ Message \u210b\u27e8_\u27e9\u2295_\n\nmodule \u27e8\u2124p\u27e9\u2605 p-3 {- p is prime -} (`R\u2090 : `\u2605) where\n p : \u2115\n p = 3 + p-3\n\n q : \u2115\n q = p \u2238 1\n\n module G = G-implem p q (## 2) (## 0) (## 1) (## 0) (## 1)\n open G\n\n postulate\n _\u207b\u00b9 : G \u2192 G\n dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y\n\n module EB = El-Gamal-Base _ _\u22a0_ G g _^_ _\u207b\u00b9 _\u2219_\n open EB hiding (g^_)\n\n open \u2124q-count \u2124q _\u229e_ sum\u2124q sum\u2124q-ext sum\u2124q-\u229e-lem\n\n R\u2090 = El `R\u2090\n sumR\u2090 = sum `R\u2090\n sumR\u2090-ext = sum-ext `R\u2090\n\n open WithSumRAnd\u2124qProps R\u2090 sumR\u2090 sumR\u2090-ext dist-^-\u22a0 sum\u2124q sum\u2124q-ext\n\n open SumR\n postulate\n ddh-hyp : (A : DDHAdv R\u2090) \u2192 DDH\u2141 A 0b \u2248R DDH\u2141 A 1b\n otp-lem : \u2200 (A : G \u2192 Bit) m \u2192 (\u03bb x \u2192 A (g^ x \u2219 m)) \u2248q (\u03bb x \u2192 A (g^ x))\n\n open EvenMoreProof ddh-hyp otp-lem\n\n final : \u2200 A \u2192 SS\u2141 A 0b \u2248R SS\u2141 A 1b\n final A = WithAdversary.final A 0b\n\nmodule \u27e8\u212411\u27e9\u2605 = \u27e8\u2124p\u27e9\u2605 (11 \u2238 3)\n `X -- the amount of adversary randomness\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","old_contents":"{-# OPTIONS --copatterns #-}\nopen import Function\nopen import Data.Product\nopen import Data.Bool.NP as Bool\nopen import Data.Unit\nopen import Data.Maybe.NP\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc) renaming (#_ to ##_)\nopen import Data.Nat.NP hiding (_^_)\nopen import Data.Bits\nimport Data.Vec.NP as Vec\nopen Vec using (Vec; []; _\u2237_; _\u2237\u02b3_; allFin; lookup) renaming (map to vmap)\n--import Data.Vec.Properties as Vec\nopen import Algebra.FunctionProperties\nopen import Relation.Binary.PropositionalEquality as \u2261\n\nmodule elgamal where\n\n\u2605\u2081 : Set\u2082\n\u2605\u2081 = Set\u2081\n\n\u2605 : Set\u2081\n\u2605 = Set\n\n[0\u2192_,1\u2192_] : \u2200 {a} {A : Set a} \u2192 A \u2192 A \u2192 Bit \u2192 A\n[0\u2192 e\u2080 ,1\u2192 e\u2081 ] b = if b then e\u2081 else e\u2080\n\ncase_0\u2192_1\u2192_ : \u2200 {a} {A : Set a} \u2192 Bit \u2192 A \u2192 A \u2192 A\ncase b 0\u2192 e\u2080 1\u2192 e\u2081 = if b then e\u2081 else e\u2080\n\nSum : \u2605 \u2192 \u2605\nSum A = (A \u2192 \u2115) \u2192 \u2115\n\nCount : \u2605 \u2192 \u2605\nCount A = (A \u2192 Bit) \u2192 \u2115\n\nSumExt : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumExt sumA = \u2200 f g \u2192 f \u2257 g \u2192 sumA f \u2261 sumA g\n\nsumToCount : \u2200 {A} \u2192 Sum A \u2192 Count A\nsumToCount sumA f = sumA (Bool.to\u2115 \u2218 f)\n\nsumBit : Sum Bit\nsumBit f = f 0b + f 1b\n\n-- liftM2 _,_ in the continuation monad\nsumProd : \u2200 {A B : Set} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u00d7 B)\nsumProd sumA sumB f = sumA (\u03bb x\u2080 \u2192\n sumB (\u03bb x\u2081 \u2192\n f (x\u2080 , x\u2081)))\n\nsumProd-ext : \u2200 {A B : Set} {sumA : Sum A} {sumB : Sum B} \u2192\n SumExt sumA \u2192 SumExt sumB \u2192 SumExt (sumProd sumA sumB)\nsumProd-ext sumA-ext sumB-ext f g f\u2257g = sumA-ext _ _ (\u03bb x \u2192 sumB-ext _ _ (\u03bb y \u2192 f\u2257g (x , y)))\n\ndata `\u2605 : \u2605 where\n `\u22a4 : `\u2605\n `X : `\u2605\n _`\u00d7_ : `\u2605 \u2192 `\u2605 \u2192 `\u2605\ninfixr 2 _`\u00d7_\n\nmodule Univ (X : \u2605) where\n El : `\u2605 \u2192 \u2605\n El `\u22a4 = \u22a4\n El `X = X\n El (u\u2080 `\u00d7 u\u2081) = El u\u2080 \u00d7 El u\u2081\n\n record \u21ba (R : `\u2605) (A : \u2605) : \u2605 where\n constructor mk\n field\n run\u21ba : El R \u2192 A\n open \u21ba public\n\n EXP : (R : `\u2605) \u2192 \u2605\n EXP R = \u21ba R Bit\n\n Det : \u2605 \u2192 \u2605\n Det = \u21ba `\u22a4\n\nmodule \u2124q-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (sum\u2124q : Sum \u2124q)\n (sum\u2124q-lem : \u2200 f x \u2192 sum\u2124q (f \u2218 _\u229e_ x) \u2261 sum\u2124q f)\n where\n\n open Univ \u2124q\n open `\u2605 public renaming (`X to `\u2124q)\n\n sum : (u : `\u2605) \u2192 Sum (El u)\n sum `\u22a4 f = f _\n sum `X f = sum\u2124q f\n sum (u\u2080 `\u00d7 u\u2081) f = sum u\u2080 (\u03bb x\u2080 \u2192\n sum u\u2081 (\u03bb x\u2081 \u2192\n f (x\u2080 , x\u2081)))\n\n #_ : \u2200 {u} \u2192 \u21ba u Bit \u2192 \u2115\n #_ {u} f = sum u (Bool.to\u2115 \u2218 run\u21ba f)\n\n \u2047 : \u2200 R \u2192 \u21ba R (El R)\n run\u21ba (\u2047 _) = id\n\n pure\u21ba : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n run\u21ba (pure\u21ba x) r = x -- turning r to _ produce an error\n\n \u27ea_\u27eb : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n \u27ea_\u27eb = pure\u21ba\n\n {-\n \u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n \u27ea_\u27eb\u1d30 = pure\u1d30\n -}\n\n map\u21ba : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n run\u21ba (map\u21ba f x) r = f (run\u21ba x r)\n\n infixl 4 _\u229b_\n _\u229b_ : \u2200 {R S A B} \u2192 \u21ba R (A \u2192 B) \u2192 \u21ba S A \u2192 \u21ba (R `\u00d7 S) B\n run\u21ba (af \u229b ax) rs = run\u21ba af (proj\u2081 rs) (run\u21ba ax (proj\u2082 rs))\n\n \u27ea_\u00b7_\u27eb : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n \u27ea f \u00b7 x \u27eb = map\u21ba f x\n\n \u27ea_\u00b7_\u00b7_\u27eb : \u2200 {A B C} {R S} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba R A \u2192 \u21ba S B \u2192 \u21ba (R `\u00d7 S) C\n \u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n _\u27e8\u229e\u27e9_ : \u2200 {R S} \u2192 \u21ba R \u2124q \u2192 \u21ba S \u2124q \u2192 \u21ba (R `\u00d7 S) \u2124q\n x \u27e8\u229e\u27e9 y = \u27ea _\u229e_ \u00b7 x \u00b7 y \u27eb\n\n \u27e8_\u229e\u27e9_ : \u2200 {R} \u2192 \u2124q \u2192 \u21ba R \u2124q \u2192 \u21ba R \u2124q\n \u27e8 x \u229e\u27e9 y = \u27ea _\u229e_ x \u00b7 y \u27eb\n\n infix 4 _\u2248\u21ba_ _\u2248\u1d2c_\n _\u2248\u21ba_ : \u2200 {R : `\u2605} (f g : EXP R) \u2192 \u2605\n _\u2248\u21ba_ = _\u2261_ on #_\n\n _\u2248\u1d2c_ : \u2200 {A R} (f g : \u21ba R A) \u2192 Set _\n _\u2248\u1d2c_ {A} f g = \u2200 (Adv : A \u2192 Bit) \u2192 \u27ea Adv \u00b7 f \u27eb \u2248\u21ba \u27ea Adv \u00b7 g \u27eb\n\n lem : \u2200 x \u2192 \u27e8 x \u229e\u27e9 (\u2047 `\u2124q) \u2248\u1d2c \u2047 _ \n lem x Adv = sum\u2124q-lem (Bool.to\u2115 \u2218 Adv) x\n\n -- \u2200 (A : \u2124q \u2192 Bit) \u2192 # (A \u2047) \n\nopen Fin.Modulo renaming (sucmod to [suc])\n\n{-\nmodule \u2124q-implem (q-2 : \u2115) where\n q : \u2115\n q = 2 + q-2\n\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = zero\n\n [1] : \u2124q\n [1] = suc zero\n-}\nmodule \u2124q-implem (q : \u2115) ([0]' [1]' : Fin q) where\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = [0]'\n\n [1] : \u2124q\n [1] = [1]'\n\n _\u2115\u229e_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = m \u2115\u229e ([suc] n)\n {-\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = [suc] (m \u2115\u229e n)\n -}\n\n _\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u229e n = Fin.to\u2115 m \u2115\u229e n\n\n _\u2115\u22a0_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u22a0 n = [0]\n suc m \u2115\u22a0 n = n \u229e (m \u2115\u22a0 n)\n\n _\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u22a0 n = Fin.to\u2115 m \u2115\u22a0 n\n\n _[^]\u2115_ : \u2124q \u2192 \u2115 \u2192 \u2124q\n m [^]\u2115 zero = [1]\n m [^]\u2115 suc n = m \u22a0 (m [^]\u2115 n)\n\n _[^]_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m [^] n = m [^]\u2115 (Fin.to\u2115 n)\n\n all\u2124q : Vec \u2124q q\n all\u2124q = allFin q\n\n sum\u2124q : Sum \u2124q\n sum\u2124q f = Vec.sum (vmap f all\u2124q)\n\n sum\u2124q-[suc]-lem : \u2200 f \u2192 sum\u2124q (f \u2218 [suc]) \u2261 sum\u2124q f\n sum\u2124q-[suc]-lem f rewrite \u2261.sym (Vec.sum-map-rot\u2081 f all\u2124q)\n | Vec.map-\u2218 f [suc] all\u2124q\n | rot\u2081-map-sucmod q\n = refl\n\n -- comm-[suc]-\u2115\u229e : \u2200 m n \u2192 [suc] (m \u2115\u229e n) \u2261 m \u2115\u229e ([suc] n)\n\n sum\u2124q-\u2115\u229e-lem : \u2200 m f \u2192 sum\u2124q (f \u2218 _\u2115\u229e_ m) \u2261 sum\u2124q f\n sum\u2124q-\u2115\u229e-lem zero f = refl\n sum\u2124q-\u2115\u229e-lem (suc m) f rewrite sum\u2124q-[suc]-lem (f \u2218 _\u2115\u229e_ m)\n | sum\u2124q-\u2115\u229e-lem m f = refl\n\n sum\u2124q-\u229e-lem : \u2200 m f \u2192 sum\u2124q (f \u2218 _\u229e_ m) \u2261 sum\u2124q f\n sum\u2124q-\u229e-lem = sum\u2124q-\u2115\u229e-lem \u2218 Fin.to\u2115\n\nmodule G-implem (p q : \u2115) (g' : Fin p) (0[p] 1[p] : Fin p) (0[q] 1[q] : Fin q) where\n open \u2124q-implem q 0[q] 1[q] public\n open \u2124q-implem p 0[p] 1[p] public using () renaming (\u2124q to G; _\u22a0_ to _\u2219_; _[^]\u2115_ to _^[p]_)\n\n g : G\n g = g'\n\n _^_ : G \u2192 \u2124q \u2192 G\n x ^ n = x ^[p] Fin.to\u2115 n\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n postulate\n g^-inj : \u2200 m n \u2192 g^ m \u2261 g^ n \u2192 m \u2261 n\n\nmodule G-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (sum\u2124q : Sum \u2124q)\n (sum\u2124q-lem : \u2200 f x \u2192 sum\u2124q (f \u2218 _\u229e_ x) \u2261 sum\u2124q f)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n where\n\n open Univ \u2124q\n open \u2124q-count \u2124q _\u229e_ sum\u2124q sum\u2124q-lem\n\n \u2047G : \u21ba `\u2124q G\n run\u21ba \u2047G x = g ^ x\n\nmodule DDH\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g^_ : \u2124q \u2192 G)\n where\n DDHAdv : \u2605 \u2192 \u2605\n DDHAdv R = R \u2192 G \u2192 G \u2192 G \u2192 Bit\n\n DDH\u2141\u2080 : \u2200 {R} {_I : \u2605} \u2192 DDHAdv R \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n DDH\u2141\u2080 D (r , x , y , _) = D r (g^ x) (g^ y) (g^ (x \u22a0 y))\n\n DDH\u2141\u2081 : \u2200 {R} \u2192 DDHAdv R \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n DDH\u2141\u2081 D (r , x , y , z) = D r (g^ x) (g^ y) (g^ z)\n\n DDH\u2141 : \u2200 {R} \u2192 DDHAdv R \u2192 Bit \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n DDH\u2141 D b = (case b 0\u2192 DDH\u2141\u2080 1\u2192 DDH\u2141\u2081) D\n\n -- \u2141\u2032 : \u2200 {R} \u2192 DDHAdv R \u2192 (Bit \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q \u00d7 R) \u2192 Bit\n -- \u2141\u2032 D (b , x , y , z , r) = DDH\u2141 D b (x , y , z , r)\n\n module With\u21ba where\n open Univ \u2124q\n open `\u2605 public renaming (`X to `\u2124q)\n DDHAdv\u21ba : `\u2605 \u2192 \u2605\n DDHAdv\u21ba R = G \u2192 G \u2192 G \u2192 \u21ba R Bit\n DDH\u2141\u2080\u21ba : \u2200 {R _I} \u2192 DDHAdv\u21ba R \u2192 \u21ba (R `\u00d7 `\u2124q `\u00d7 `\u2124q `\u00d7 _I) Bit\n run\u21ba (DDH\u2141\u2080\u21ba D) = DDH\u2141\u2080 (\u03bb a b c d \u2192 run\u21ba (D b c d) a)\n\nmodule El-Gamal-Generic\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (Message : \u2605)\n (_\u2295_ : G \u2192 Message \u2192 Message)\n (_\u2295\u207b\u00b9_ : G \u2192 Message \u2192 Message)\n where\n\n -- \u03b1 is the pk\n -- \u03b1 = g ^ x\n -- x is the sk\n\n PubKey = G\n SecKey = \u2124q\n KeyPair = PubKey \u00d7 SecKey\n CipherText = G \u00d7 Message\n\n M = Message\n C = CipherText\n\n KeyGen : \u2124q \u2192 KeyPair\n KeyGen x = (g ^ x , x)\n\n -- KeyGen\u21ba : \u21ba \u2124q KeyPair\n -- KeyGen\u21ba = mk KeyGen\n\n Enc : PubKey \u2192 Message \u2192 \u2124q \u2192 CipherText\n Enc \u03b1 m y = \u03b2 , \u03b6 where\n \u03b2 = g ^ y\n \u03b4 = \u03b1 ^ y\n \u03b6 = \u03b4 \u2295 m\n\n -- Enc\u21ba : PubKey \u2192 Message \u2192 \u21ba \u2124q CipherText\n -- Enc\u21ba \u03b1 m = mk (Enc \u03b1 m)\n\n Dec : SecKey \u2192 CipherText \u2192 Message\n Dec x (\u03b2 , \u03b6) = (\u03b2 ^ x) \u2295\u207b\u00b9 \u03b6\n\n EncAdv : \u2605 \u2192 \u2605\n EncAdv R = PubKey \u2192 R \u2192 (Bit \u2192 M) \u00d7 (C \u2192 Bit)\n\n {-\n Game0 : \u2200 {R _I : Set} \u2192 EncAdv R \u2192 (Bit \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I \u00d7 R) \u2192 Bit\n Game0 A (b , x , y , z , r) =\n let (pk , sk) = KeyGen x\n (m , D) = A pk r in\n D (Enc pk (m b) y)\n\n Game : (i : Bit) \u2192 \u2200 {R} \u2192 EncAdv R \u2192 (Bit \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q \u00d7 R) \u2192 Bit\n Game i A (b , x , y , z , r) =\n let (\u03b1 , sk) = KeyGen x\n (m , D) = A \u03b1 r\n \u03b2 = g ^ y\n \u03b4 = \u03b1 ^ case i 0\u2192 y 1\u2192 z\n \u03b6 = \u03b4 \u2295 m b\n in {-b ==\u1d47-} D (\u03b2 , \u03b6)\n\n Game-0b\u2261Game0 : \u2200 {R} \u2192 Game 0b \u2261 Game0 {R}\n Game-0b\u2261Game0 = refl\n -}\n\n SS\u2141 : \u2200 {R _I : Set} \u2192 EncAdv R \u2192 Bit \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n SS\u2141 A b (r , x , y , z) =\n let -- (pk , sk) = KeyGen x\n (m , D) = A pk r in\n D (Enc pk (m b) y)\n where pk = g ^ x\n\n open DDH \u2124q _\u22a0_ G (_^_ g) public\n\n -- Game0 \u2248 Game 0b\n -- Game1 = Game 1b\n\n -- Game0 \u2264 \u03b5\n -- Game1 \u2261 0\n\n -- \u2047 \u2295 x \u2248 \u2047\n\n -- g ^ \u2047 \u2219 x \u2248 g ^ \u2047\n\nmodule El-Gamal-Base\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u207b\u00b9 : G \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n where\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n _\/_ : G \u2192 G \u2192 G\n x \/ y = x \u2219 (y \u207b\u00b9)\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ G _\u2219_ (flip _\/_) public\n\n TrA : \u2200 {R} \u2192 Bit \u2192 EncAdv R \u2192 DDHAdv R\n TrA b A r g\u02e3 g\u02b8 g\u02e3\u02b8 = d (g\u02b8 , g\u02e3\u02b8 \u2219 m b)\n where m,d = A g\u02e3 r\n m = proj\u2081 m,d\n d = proj\u2082 m,d\n\n like-SS\u2141 : \u2200 {R _I : Set} \u2192 EncAdv R \u2192 Bit \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n like-SS\u2141 A b (r , x , y , _z) =\n let -- g\u02e3 = g ^ x\n -- g\u02b8 = g ^ y\n (m , D) = A g\u02e3 r in\n D (g\u02b8 , (g\u02e3 ^ y) \u2219 m b)\n where g\u02e3 = g ^ x\n g\u02b8 = g ^ y\n\n SS\u2141\u2261like-SS\u2141 : \u2200 {R _I} \u2192 SS\u2141 {R} {_I} \u2261 like-SS\u2141\n SS\u2141\u2261like-SS\u2141 = refl\n\n OTP\u2141 : \u2200 {R : Set} \u2192 (R \u2192 G \u2192 G) \u2192 (R \u2192 G \u2192 G \u2192 G \u2192 Bit) \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n OTP\u2141 M D (r , x , y , z) = D r g\u02e3 g\u02b8 (g\u1dbb \u2219 M r g\u02e3)\n where g\u02e3 = g ^ x\n g\u02b8 = g ^ y\n g\u1dbb = g ^ z\n\n module With\u2124qProps\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n -- (\u22a0-comm : Commutative _\u2261_ _\u22a0_)\n -- (^-comm : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x) ^ y \u2261 (\u03b1 ^ y) ^ x)\n (sum\u2124q : Sum \u2124q)\n (sum\u2124q-ext : SumExt sum\u2124q) where\n #q_ : Count \u2124q\n #q_ = sumToCount sum\u2124q\n\n module SumU\n (R : Set)\n (sumR : Sum R)\n (sumR-ext : SumExt sumR)\n where\n U = R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q\n sumU : Sum U\n sumU = sumProd sumR (sumProd sum\u2124q (sumProd sum\u2124q sum\u2124q))\n #U_ : Count U\n #U_ = sumToCount sumU\n sumU-ext : SumExt sumU\n sumU-ext = sumProd-ext sumR-ext (sumProd-ext sum\u2124q-ext (sumProd-ext sum\u2124q-ext sum\u2124q-ext))\n\n module WithSumR\n (R : Set)\n (sumR : Sum R)\n (sumR-ext : SumExt sumR)\n where\n open SumU R sumR sumR-ext\n\n module EvenMoreProof\n (ddh-hyp : (A : DDHAdv R) \u2192 #U (DDH\u2141 A 0b) \u2261 #U (DDH\u2141 A 1b))\n\n (otp-lem : \u2200 (A : G \u2192 Bit) m \u2192 #q(\u03bb x \u2192 A (g^ x \u2219 m)) \u2261 #q(\u03bb x \u2192 A (g^ x)))\n where\n\n _\u2248U_ : (f g : U \u2192 Bit) \u2192 Set\n f \u2248U g = #U f \u2261 #U g\n\n otp-lem' : \u2200 D M\u2080 M\u2081 \u2192 OTP\u2141 M\u2080 D \u2248U OTP\u2141 M\u2081 D\n otp-lem' D M\u2080 M\u2081 = sumR-ext (f3 M\u2080) (f3 M\u2081) (\u03bb r \u2192\n sum\u2124q-ext (f2 M\u2080 r) (f2 M\u2081 r) (\u03bb x \u2192\n sum\u2124q-ext (f1 M\u2080 r x) (f1 M\u2081 r x) (\u03bb y \u2192\n pf r x y)))\n where\n f0 = \u03bb M r x y z \u2192 OTP\u2141 M D (r , x , y , z)\n f1 = \u03bb M r x y \u2192 sum\u2124q (Bool.to\u2115 \u2218 f0 M r x y)\n f2 = \u03bb M r x \u2192 sum\u2124q (f1 M r x)\n f3 = \u03bb M r \u2192 sum\u2124q (f2 M r)\n pf : \u2200 r x y \u2192 f1 M\u2080 r x y \u2261 f1 M\u2081 r x y\n pf r x y = pf'\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n m0 = M\u2080 r g\u02e3\n m1 = M\u2081 r g\u02e3\n f5 = \u03bb M z \u2192 D r g\u02e3 g\u02b8 (g^ z \u2219 M)\n pf' : #q(f5 m0) \u2261 #q(f5 m1)\n pf' rewrite otp-lem (D r g\u02e3 g\u02b8) m0\n | otp-lem (D r g\u02e3 g\u02b8) m1 = refl\n\n {-\n #-proj\u2081 : \u2200 {A B : Set} {f g : A \u2192 Bit} \u2192 # f \u2261 # g\n \u2192 #_ {A \u00d7 B} (f \u2218 proj\u2081) \u2261 #_ {A \u00d7 B} (g \u2218 proj\u2081)\n #-proj\u2081 = {!!}\n\n otp-lem'' : \u2200 (A : G \u2192 Bit) m\n \u2192 #(\u03bb { (x , y) \u2192 A (g^ x \u2219 m) }) \u2261 #(\u03bb { (x , y) \u2192 A (g ^ x) })\n otp-lem'' = {!!}\n -}\n projM : EncAdv R \u2192 Bit \u2192 R \u2192 G \u2192 G\n projM A b r g\u02e3 = proj\u2081 (A g\u02e3 r) b\n\n projD : EncAdv R \u2192 R \u2192 G \u2192 G \u2192 G \u2192 Bit\n projD A r g\u02e3 g\u02b8 g\u1dbb\u2219M = proj\u2082 (A g\u02e3 r) (g\u02b8 , g\u1dbb\u2219M)\n\n\n module WithAdversary (A : EncAdv R) b where\n A\u1d47 = TrA b A\n A\u00ac\u1d47 = TrA (not b) A\n\n pf0,5 : SS\u2141 A b \u2257 DDH\u2141 A\u1d47 0b\n pf0,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n pf1 : #U(SS\u2141 A b) \u2261 #U(DDH\u2141 A\u1d47 0b)\n pf1 = sumU-ext _ _ (cong Bool.to\u2115 \u2218 pf0,5)\n\n pf2 : DDH\u2141 A\u1d47 0b \u2248U DDH\u2141 A\u1d47 1b\n pf2 = ddh-hyp A\u1d47\n\n pf2,5 : DDH\u2141 A\u1d47 1b \u2261 OTP\u2141 (projM A b) (projD A)\n pf2,5 = refl\n\n pf3 : DDH\u2141 A\u1d47 1b \u2248U DDH\u2141 A\u00ac\u1d47 1b\n pf3 = otp-lem' (projD A) (projM A b) (projM A (not b))\n\n pf4 : DDH\u2141 A\u00ac\u1d47 1b \u2248U DDH\u2141 A\u00ac\u1d47 0b\n pf4 = \u2261.sym (ddh-hyp A\u00ac\u1d47)\n\n pf4,5 : SS\u2141 A (not b) \u2257 DDH\u2141 A\u00ac\u1d47 0b\n pf4,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n pf5 : #U(SS\u2141 A (not b)) \u2261 #U(DDH\u2141 A\u00ac\u1d47 0b)\n pf5 = sumU-ext _ _ (cong Bool.to\u2115 \u2218 pf4,5)\n\n final : #U(SS\u2141 A b) \u2261 #U(SS\u2141 A (not b))\n final rewrite pf1 | pf2 | pf3 | pf4 | pf5 = refl\n\n\n {-\n pf1 : \u2200 {R} A r \u2192 Game 0b {R} A r \u2261 \u2141\u2032 (TrA 1b A) r\n pf1 A (true , x , y , z , r) rewrite dist-^-\u22a0 g x y = refl\n pf1 A (false , x , y , z , r) = {!!}\n\n pf2 : \u2200 {R} A r \u2192 Game 1b {R} A r \u2261 \u2141\u2032 (TrA 0b A) r\n pf2 A (true , x , y , z , r) = {!!}\n pf2 A (false , x , y , z , r) = {!!}\n-}\nmodule El-Gamal-Hashed\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n -- (HKey : \u2605)\n (|M| : \u2115)\n (\u210b : {-HKey \u2192-} G \u2192 Bits |M|) where\n\n Message = Bits |M|\n\n \u210b\u27e8_\u27e9\u2295_ : G \u2192 Message \u2192 Message\n \u210b\u27e8 \u03b4 \u27e9\u2295 m = \u210b \u03b4 \u2295 m\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ Message \u210b\u27e8_\u27e9\u2295_\n\n module Proof\n -- (\u22a0-comm : Commutative _\u2261_ _\u22a0_)\n -- (^-lem : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (^-comm : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x) ^ y \u2261 (\u03b1 ^ y) ^ x)\n -- (x \u2219 x \u207b\u00b9 \u2261 0)\n (\u211a : \u2605)\n (dist : \u211a \u2192 \u211a \u2192 \u211a)\n (1\/2 : \u211a)\n (_\u2264_ : \u211a \u2192 \u211a \u2192 \u2605)\n (\u03b5-DDH : \u211a)\n where\n\n Pr[S_] : Bool \u2192 \u211a\n Pr[S b ] = {!!}\n\n -- SS\n -- SS = dist Pr[\n\n Pr[S\u2080] = Pr[S 0b ]\n Pr[S\u2081] = Pr[S 1b ]\n\n pf1 : Pr[S\u2081] \u2261 1\/2\n pf1 = {!!}\n\n pf2 : dist Pr[S\u2080] 1\/2 \u2261 dist Pr[S\u2080] Pr[S\u2081]\n pf2 = {!!}\n\n pf3 : dist Pr[S\u2080] Pr[S\u2081] \u2264 \u03b5-DDH\n pf3 = {!!}\n\n pf4 : dist Pr[S\u2080] 1\/2 \u2264 \u03b5-DDH\n pf4 = {!!}\n\nmodule G11 = G-implem 11 10 (## 2) (## 0) (## 1) (## 0) (## 1)\nopen G11\nmodule E11 = El-Gamal-Base _ _\u22a0_ G g _^_ {!!} _\u2219_\nopen E11\nopen With\u2124qProps ?\n-- open Proof {!!}\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"c11e644e4393f792d6cc2dc45eb85a1e4d25a878","subject":" demand way","message":" demand way\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"canonical-indeterminate-forms.agda","new_file":"canonical-indeterminate-forms.agda","new_contents":"open import Nat\nopen import Prelude\nopen import contexts\nopen import core\nopen import type-assignment-unicity\n\nmodule canonical-indeterminate-forms where\n data cif-base : (\u0394 : hctx) (d : dhexp) \u2192 Set where\n CIFBEHole : \u2200 { \u0394 d} \u2192\n \u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] b) \u2208 \u0394))\n \u2192 cif-base \u0394 d\n CIFBNEHole : \u2200 { \u0394 d} \u2192\n \u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] b) \u2208 \u0394))\n \u2192 cif-base \u0394 d\n CIFBAp : \u2200 { \u0394 d} \u2192\n \u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c42 \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: \u03c42 ==> b) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c42) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))\n \u2192 cif-base \u0394 d\n CIFBCast : \u2200 { \u0394 d} \u2192\n \u03a3[ d' \u2208 dhexp ]\n ((d == d' \u27e8 \u2987\u2988 \u21d2 b \u27e9) \u00d7\n (d' indet) \u00d7\n ((d'' : dhexp) (\u03c4' : htyp) \u2192 d' \u2260 (d'' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9)))\n \u2192 cif-base \u0394 d\n CIFBFailedCast : \u2200 { \u0394 d} \u2192\n \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ]\n ((d == d' \u27e8 \u03c4' \u21d2\u2987\u2988\u21d2\u0338 b \u27e9) \u00d7\n (\u03c4' ground) \u00d7\n (\u03c4' \u2260 b) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4'))\n \u2192 cif-base \u0394 d\n\n canonical-indeterminate-forms-base : \u2200{\u0394 d} \u2192\n \u0394 , \u2205 \u22a2 d :: b \u2192\n d indet \u2192 cif-base \u0394 d\n canonical-indeterminate-forms-base TAConst ()\n canonical-indeterminate-forms-base (TAVar x\u2081) ()\n canonical-indeterminate-forms-base (TAAp wt wt\u2081) (IAp x ind x\u2081) = CIFBAp (_ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)\n canonical-indeterminate-forms-base (TAEHole x x\u2081) IEHole = CIFBEHole (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-base (TANEHole x wt x\u2081) (INEHole x\u2082) = CIFBNEHole (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x)\n canonical-indeterminate-forms-base (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) = CIFBCast (_ , refl , ind , x\u2081)\n canonical-indeterminate-forms-base (TAFailedCast x x\u2081 x\u2082 x\u2083) (IFailedCast x\u2084 x\u2085 x\u2086 x\u2087) = CIFBFailedCast (_ , _ , refl , x\u2085 , x\u2087 , x)\n\n data cif-arr : (\u0394 : hctx) (d : dhexp) (\u03c41 \u03c42 : htyp) \u2192 Set where\n CIFAEHole : \u2200{d \u0394 \u03c41 \u03c42} \u2192\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] (\u03c41 ==> \u03c42)) \u2208 \u0394)))\n \u2192 cif-arr \u0394 d \u03c41 \u03c42\n CIFANEHole : \u2200{d \u0394 \u03c41 \u03c42} \u2192\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] (\u03c41 ==> \u03c42)) \u2208 \u0394)))\n \u2192 cif-arr \u0394 d \u03c41 \u03c42\n CIFAAp : \u2200{d \u0394 \u03c41 \u03c42} \u2192\n (\u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c4 \u2208 htyp ] \u03a3[ \u03c41' \u2208 htyp ] \u03a3[ \u03c42' \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: \u03c4 ==> (\u03c41' ==> \u03c42')) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c4) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9))))\n \u2192 cif-arr \u0394 d \u03c41 \u03c42\n CIFACast : \u2200{d \u0394 \u03c41 \u03c42} \u2192\n (\u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c41 \u2208 htyp ] \u03a3[ \u03c42 \u2208 htyp ] \u03a3[ \u03c43 \u2208 htyp ] \u03a3[ \u03c44 \u2208 htyp ]\n ((d == d' \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9) \u00d7\n (d' indet) \u00d7\n ((\u03c41 ==> \u03c42) \u2260 (\u03c43 ==> \u03c44))))\n \u2192 cif-arr \u0394 d \u03c41 \u03c42\n CIFACastHole : \u2200{d \u0394 \u03c41 \u03c42} \u2192\n (\u03a3[ d' \u2208 dhexp ]\n ((\u03c41 == \u2987\u2988) \u00d7\n (\u03c42 == \u2987\u2988) \u00d7\n (d == (d' \u27e8 \u2987\u2988 \u21d2 \u2987\u2988 ==> \u2987\u2988 \u27e9)) \u00d7\n (d' indet) \u00d7\n ((d'' : dhexp) (\u03c4' : htyp) \u2192 d' \u2260 (d'' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9))))\n \u2192 cif-arr \u0394 d \u03c41 \u03c42\n CIFAFailedCast : \u2200{d \u0394 \u03c41 \u03c42} \u2192\n (\u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4 \u2208 htyp ]\n ((d == (d' \u27e8 \u03c4 \u21d2\u2987\u2988\u21d2\u0338 \u2987\u2988 ==> \u2987\u2988 \u27e9) ) \u00d7\n (\u03c4 ground) \u00d7\n (\u03c4 \u2260 (\u2987\u2988 ==> \u2987\u2988)) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4)))\n \u2192 cif-arr \u0394 d \u03c41 \u03c42\n\n canonical-indeterminate-forms-arr : \u2200{\u0394 d \u03c41 \u03c42 } \u2192\n \u0394 , \u2205 \u22a2 d :: (\u03c41 ==> \u03c42) \u2192\n d indet \u2192\n cif-arr \u0394 d \u03c41 \u03c42\n canonical-indeterminate-forms-arr (TAVar x\u2081) ()\n canonical-indeterminate-forms-arr (TALam wt) ()\n canonical-indeterminate-forms-arr (TAAp wt wt\u2081) (IAp x ind x\u2081) = CIFAAp (_ , _ , _ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)\n canonical-indeterminate-forms-arr (TAEHole x x\u2081) IEHole = CIFAEHole (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-arr (TANEHole x wt x\u2081) (INEHole x\u2082) = CIFANEHole (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x)\n canonical-indeterminate-forms-arr (TACast wt x) (ICastArr x\u2081 ind) = CIFACast (_ , _ , _ , _ , _ , refl , ind , x\u2081)\n canonical-indeterminate-forms-arr (TACast wt TCHole2) (ICastHoleGround x\u2081 ind GHole) = CIFACastHole (_ , refl , refl , refl , ind , x\u2081)\n canonical-indeterminate-forms-arr (TAFailedCast x x\u2081 GHole x\u2083) (IFailedCast x\u2084 x\u2085 GHole x\u2087) = CIFAFailedCast (_ , _ , refl , x\u2085 , x\u2087 , x)\n\n canonical-indeterminate-forms-hole : \u2200{\u0394 d} \u2192\n \u0394 , \u2205 \u22a2 d :: \u2987\u2988 \u2192\n d indet \u2192\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] \u2987\u2988) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] \u2987\u2988) \u2208 \u0394))) +\n (\u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c42 \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: (\u03c42 ==> \u2987\u2988)) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c42) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))) +\n (\u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ]\n ((d == d' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9) \u00d7\n (\u03c4' ground) \u00d7\n (d' indet)))\n canonical-indeterminate-forms-hole (TAVar x\u2081) ()\n canonical-indeterminate-forms-hole (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-hole (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-hole (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x ))\n canonical-indeterminate-forms-hole (TACast wt x) (ICastGroundHole x\u2081 ind) = Inr (Inr (Inr (_ , _ , refl , x\u2081 , ind)))\n canonical-indeterminate-forms-hole (TACast wt x) (ICastHoleGround x\u2081 ind ())\n canonical-indeterminate-forms-hole (TAFailedCast x x\u2081 () x\u2083) (IFailedCast x\u2084 x\u2085 x\u2086 x\u2087)\n\n canonical-indeterminate-forms-coverage : \u2200{\u0394 d \u03c4} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d indet \u2192\n \u03c4 \u2260 b \u2192\n ((\u03c41 : htyp) (\u03c42 : htyp) \u2192 \u03c4 \u2260 (\u03c41 ==> \u03c42)) \u2192\n \u03c4 \u2260 \u2987\u2988 \u2192\n \u22a5\n canonical-indeterminate-forms-coverage TAConst () nb na nh\n canonical-indeterminate-forms-coverage (TAVar x\u2081) () nb na nh\n canonical-indeterminate-forms-coverage (TALam wt) () nb na nh\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TAAp wt wt\u2081) (IAp x ind x\u2081) nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TAAp wt wt\u2081) (IAp x ind x\u2081) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TAAp wt wt\u2081) (IAp x ind x\u2081) nb na nh = na \u03c4 \u03c4\u2081 refl\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TAEHole x x\u2081) IEHole nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TAEHole x x\u2081) IEHole nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TAEHole x x\u2081) IEHole nb na nh = na \u03c4 \u03c4\u2081 refl\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TANEHole x wt x\u2081) (INEHole x\u2082) nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TANEHole x wt x\u2081) (INEHole x\u2082) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TANEHole x wt x\u2081) (INEHole x\u2082) nb na nh = na \u03c4 \u03c4\u2081 refl\n canonical-indeterminate-forms-coverage (TACast wt x) (ICastArr x\u2081 ind) nb na nh = na _ _ refl\n canonical-indeterminate-forms-coverage (TACast wt x) (ICastGroundHole x\u2081 ind) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) nb na nh = na \u03c4 \u03c4\u2081 refl\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TAFailedCast x x\u2081 x\u2082 x\u2083) (IFailedCast x\u2084 x\u2085 x\u2086 x\u2087) = \u03bb z _ _\u2081 \u2192 z refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TAFailedCast x x\u2081 x\u2082 x\u2083) (IFailedCast x\u2084 x\u2085 x\u2086 x\u2087) = \u03bb _ _\u2081 z \u2192 z refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TAFailedCast x x\u2081 x\u2082 x\u2083) (IFailedCast x\u2084 x\u2085 x\u2086 x\u2087) = \u03bb _ z _\u2081 \u2192 z \u03c4 \u03c4\u2081 refl\n","old_contents":"open import Nat\nopen import Prelude\nopen import contexts\nopen import core\nopen import type-assignment-unicity\n\nmodule canonical-indeterminate-forms where\n canonical-indeterminate-forms-base : \u2200{\u0394 d} \u2192\n \u0394 , \u2205 \u22a2 d :: b \u2192\n d indet \u2192\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] b) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] b) \u2208 \u0394))) +\n \u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c42 \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: \u03c42 ==> b) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c42) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))\n +\n (\u03a3[ d' \u2208 dhexp ]\n ((d == d' \u27e8 \u2987\u2988 \u21d2 b \u27e9) \u00d7\n (d' indet) \u00d7\n ((d'' : dhexp) (\u03c4' : htyp) \u2192 d' \u2260 (d'' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9)))) +\n (\u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ]\n ((d == d' \u27e8 \u03c4' \u21d2\u2987\u2988\u21d2\u0338 b \u27e9) \u00d7\n (\u03c4' ground) \u00d7\n (\u03c4' \u2260 b) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4')))\n canonical-indeterminate-forms-base TAConst ()\n canonical-indeterminate-forms-base (TAVar x\u2081) ()\n canonical-indeterminate-forms-base (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-base (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-base (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x))\n canonical-indeterminate-forms-base (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) = Inr (Inr (Inr (Inl (_ , refl , ind , x\u2081))))\n canonical-indeterminate-forms-base (TAFailedCast x x\u2081 x\u2082 x\u2083) (IFailedCast x\u2084 x\u2085 x\u2086 x\u2087) = Inr (Inr (Inr (Inr (_ , _ , refl , x\u2085 , x\u2087 , x))))\n\n canonical-indeterminate-forms-arr : \u2200{\u0394 d \u03c41 \u03c42 } \u2192\n \u0394 , \u2205 \u22a2 d :: (\u03c41 ==> \u03c42) \u2192\n d indet \u2192\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] (\u03c41 ==> \u03c42)) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] (\u03c41 ==> \u03c42)) \u2208 \u0394))) +\n (\u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c4 \u2208 htyp ] \u03a3[ \u03c41' \u2208 htyp ] \u03a3[ \u03c42' \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: \u03c4 ==> (\u03c41' ==> \u03c42')) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c4) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))) +\n (\u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c41 \u2208 htyp ] \u03a3[ \u03c42 \u2208 htyp ] \u03a3[ \u03c43 \u2208 htyp ] \u03a3[ \u03c44 \u2208 htyp ]\n ((d == d' \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9) \u00d7\n (d' indet) \u00d7\n ((\u03c41 ==> \u03c42) \u2260 (\u03c43 ==> \u03c44)))) +\n (\u03a3[ d' \u2208 dhexp ]\n ((\u03c41 == \u2987\u2988) \u00d7\n (\u03c42 == \u2987\u2988) \u00d7\n (d == (d' \u27e8 \u2987\u2988 \u21d2 \u2987\u2988 ==> \u2987\u2988 \u27e9)) \u00d7\n (d' indet) \u00d7\n ((d'' : dhexp) (\u03c4' : htyp) \u2192 d' \u2260 (d'' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9)))) +\n (\u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4 \u2208 htyp ]\n ((d == (d' \u27e8 \u03c4 \u21d2\u2987\u2988\u21d2\u0338 \u2987\u2988 ==> \u2987\u2988 \u27e9) ) \u00d7\n (\u03c4 ground) \u00d7\n (\u03c4 \u2260 (\u2987\u2988 ==> \u2987\u2988)) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4)))\n canonical-indeterminate-forms-arr (TAVar x\u2081) ()\n canonical-indeterminate-forms-arr (TALam wt) ()\n canonical-indeterminate-forms-arr (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-arr (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-arr (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x ))\n canonical-indeterminate-forms-arr (TACast wt x) (ICastArr x\u2081 ind) = Inr (Inr (Inr (Inl (_ , _ , _ , _ , _ , refl , ind , x\u2081))))\n canonical-indeterminate-forms-arr (TACast wt TCHole2) (ICastHoleGround x\u2081 ind GHole) = Inr (Inr (Inr (Inr (Inl (_ , refl , refl , refl , ind , x\u2081)))))\n canonical-indeterminate-forms-arr (TAFailedCast x x\u2081 GHole x\u2083) (IFailedCast x\u2084 x\u2085 GHole x\u2087) = Inr (Inr (Inr (Inr (Inr (_ , _ , refl , x\u2085 , x\u2083 , x)))))\n\n canonical-indeterminate-forms-hole : \u2200{\u0394 d} \u2192\n \u0394 , \u2205 \u22a2 d :: \u2987\u2988 \u2192\n d indet \u2192\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] \u2987\u2988) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] \u2987\u2988) \u2208 \u0394))) +\n (\u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c42 \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: (\u03c42 ==> \u2987\u2988)) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c42) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))) +\n (\u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ]\n ((d == d' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9) \u00d7\n (\u03c4' ground) \u00d7\n (d' indet)))\n canonical-indeterminate-forms-hole (TAVar x\u2081) ()\n canonical-indeterminate-forms-hole (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-hole (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-hole (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x ))\n canonical-indeterminate-forms-hole (TACast wt x) (ICastGroundHole x\u2081 ind) = Inr (Inr (Inr (_ , _ , refl , x\u2081 , ind)))\n canonical-indeterminate-forms-hole (TACast wt x) (ICastHoleGround x\u2081 ind ())\n canonical-indeterminate-forms-hole (TAFailedCast x x\u2081 () x\u2083) (IFailedCast x\u2084 x\u2085 x\u2086 x\u2087)\n\n canonical-indeterminate-forms-coverage : \u2200{\u0394 d \u03c4} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d indet \u2192\n \u03c4 \u2260 b \u2192\n ((\u03c41 : htyp) (\u03c42 : htyp) \u2192 \u03c4 \u2260 (\u03c41 ==> \u03c42)) \u2192\n \u03c4 \u2260 \u2987\u2988 \u2192\n \u22a5\n canonical-indeterminate-forms-coverage TAConst () nb na nh\n canonical-indeterminate-forms-coverage (TAVar x\u2081) () nb na nh\n canonical-indeterminate-forms-coverage (TALam wt) () nb na nh\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TAAp wt wt\u2081) (IAp x ind x\u2081) nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TAAp wt wt\u2081) (IAp x ind x\u2081) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TAAp wt wt\u2081) (IAp x ind x\u2081) nb na nh = na \u03c4 \u03c4\u2081 refl\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TAEHole x x\u2081) IEHole nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TAEHole x x\u2081) IEHole nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TAEHole x x\u2081) IEHole nb na nh = na \u03c4 \u03c4\u2081 refl\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TANEHole x wt x\u2081) (INEHole x\u2082) nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TANEHole x wt x\u2081) (INEHole x\u2082) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TANEHole x wt x\u2081) (INEHole x\u2082) nb na nh = na \u03c4 \u03c4\u2081 refl\n canonical-indeterminate-forms-coverage (TACast wt x) (ICastArr x\u2081 ind) nb na nh = na _ _ refl\n canonical-indeterminate-forms-coverage (TACast wt x) (ICastGroundHole x\u2081 ind) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) nb na nh = na \u03c4 \u03c4\u2081 refl\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TAFailedCast x x\u2081 x\u2082 x\u2083) (IFailedCast x\u2084 x\u2085 x\u2086 x\u2087) = \u03bb z _ _\u2081 \u2192 z refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TAFailedCast x x\u2081 x\u2082 x\u2083) (IFailedCast x\u2084 x\u2085 x\u2086 x\u2087) = \u03bb _ _\u2081 z \u2192 z refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TAFailedCast x x\u2081 x\u2082 x\u2083) (IFailedCast x\u2084 x\u2085 x\u2086 x\u2087) = \u03bb _ z _\u2081 \u2192 z \u03c4 \u03c4\u2081 refl\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"64cba9367b5f3fea04faf0eb4ea064124283c935","subject":"Cosmetic changes.","message":"Cosmetic changes.\n\nIgnore-this: 5c709c40f6f324f1a0056bad771ebef4\n\ndarcs-hash:20110324133307-3bd4e-465681788611ef6934d13ab3f7bebf4d0ae71f41.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/FOTC\/Data\/Nat\/AddTotality.agda","new_file":"Draft\/FOTC\/Data\/Nat\/AddTotality.agda","new_contents":"module Draft.FOTC.Data.Nat.AddTotality where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n\n-- Interactive proof using the induction principle for natural numbers.\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = indN P P0 ih Nm\n where\n P : D \u2192 Set\n P = \u03bb i \u2192 N (i + n)\n\n P0 : P zero\n P0 = subst N (sym (+-0x n)) Nn\n\n ih : \u2200 {i} \u2192 N i \u2192 P i \u2192 P (succ i)\n ih {i} Ni Pi = subst N (sym (+-Sx i n)) (sN Pi)\n\n-- Combined proof using an instance of the induction principle.\nindN-instance : (x : D) \u2192\n N (zero + x) \u2192\n (\u2200 {n} \u2192 N n \u2192 N (n + x) \u2192 N (succ n + x)) \u2192\n \u2200 {n} \u2192 N n \u2192 N (n + x)\nindN-instance x = indN (\u03bb i \u2192 N (i + x))\n\npostulate\n +-N\u2081 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n{-# ATP prove +-N\u2081 indN-instance #-}\n","old_contents":"module Draft.FOTC.Data.Nat.AddTotality where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n\n-- Interactive proof using the induction principle for natural numbers.\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = indN P P0 ih Nm\n where\n P : D \u2192 Set\n P = \u03bb i \u2192 N (i + n)\n\n P0 : P zero\n P0 = subst N (sym (+-0x n)) Nn\n\n ih : \u2200 {i} \u2192 N i \u2192 P i \u2192 P (succ i)\n ih {i} Ni Pi = subst N (sym (+-Sx i n)) (sN Pi)\n\n-- Combined proof using an instance of the induction principle.\nindN-instance : (x : D) \u2192\n N (zero + x) \u2192\n (\u2200 {n} \u2192 N n \u2192 N (n + x) \u2192\n N (succ n + x) ) \u2192\n \u2200 {n} \u2192 N n \u2192 N (n + x)\nindN-instance x = indN (\u03bb i \u2192 N (i + x))\n\npostulate\n +-N\u2081 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n{-# ATP prove +-N\u2081 indN-instance #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"1ade0ad7a853c7c488eb2b25e66563058db1a3d6","subject":"Bool: +cond","message":"Bool: +cond\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bool\/NP.agda","new_file":"lib\/Data\/Bool\/NP.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Bool.NP where\n\nopen import Data.Bool using (Bool; true; false; T; if_then_else_; not)\nimport Algebra\nimport Data.Bool.Properties as B\nopen import Data.Unit using (\u22a4)\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Nat using (\u2115; _\u2264_; z\u2264n; s\u2264s)\nopen import Function\nopen import Relation.Binary.NP\nopen import Relation.Binary.Logical\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\nimport Relation.Binary.PropositionalEquality as \u2261\nimport Function.Equivalence as E\nopen E.Equivalence using (to; from)\nopen import Function.Equality using (_\u27e8$\u27e9_)\nopen \u2261 using (_\u2261_)\n\ncond : \u2200 {a} {A : Set a} \u2192 A \u2192 A \u2192 Bool \u2192 A\ncond x y b = if b then x else y\n\nmodule Xor\u00b0 = Algebra.CommutativeRing B.commutativeRing-xor-\u2227\nmodule Bool\u00b0 = Algebra.CommutativeSemiring B.commutativeSemiring-\u2227-\u2228\n\ncheck : \u2200 b \u2192 {pf : T b} \u2192 \u22a4\ncheck = _\n\nIf_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 (T b \u2192 A) \u2192 (T (not b) \u2192 B) \u2192 if b then A else B\nIf_then_else_ true x _ = x _\nIf_then_else_ false _ x = x _\n\nIf\u2032_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 A \u2192 B \u2192 if b then A else B\nIf\u2032_then_else_ true x _ = x\nIf\u2032_then_else_ false _ x = x\n\nIf-map : \u2200 {A B C D : Set} b (f : T b \u2192 A \u2192 C) (g : T (not b) \u2192 B \u2192 D) \u2192\n if b then A else B \u2192 if b then C else D\nIf-map true f _ = f _\nIf-map false _ f = f _\n\nIf-elim : \u2200 {A B : Set} {P : Bool \u2192 Set}\n b (f : T b \u2192 A \u2192 P true) (g : T (not b) \u2192 B \u2192 P false) \u2192 if b then A else B \u2192 P b\nIf-elim true f _ = f _\nIf-elim false _ f = f _\n\nIf-true : \u2200 {A B : Set} {b} \u2192 T b \u2192 (if b then A else B) \u2261 A\nIf-true {b = true} _ = \u2261.refl\nIf-true {b = false} ()\n\nIf-false : \u2200 {A B : Set} {b} \u2192 T (not b) \u2192 (if b then A else B) \u2261 B\nIf-false {b = true} ()\nIf-false {b = false} _ = \u2261.refl\n\ncong-if : \u2200 {A B : Set} b {t\u2080 t\u2081} (f : A \u2192 B) \u2192 (if b then f t\u2080 else f t\u2081) \u2261 f (if b then t\u2080 else t\u2081)\ncong-if true _ = \u2261.refl\ncong-if false _ = \u2261.refl\n\nif-not : \u2200 {a} {A : Set a} b {t\u2080 t\u2081 : A} \u2192 (if b then t\u2080 else t\u2081) \u2261 (if not b then t\u2081 else t\u2080)\nif-not true = \u2261.refl\nif-not false = \u2261.refl\n\ndata \u27e6Bool\u27e7 : (b\u2081 b\u2082 : Bool) \u2192 Set where\n \u27e6true\u27e7 : \u27e6Bool\u27e7 true true\n \u27e6false\u27e7 : \u27e6Bool\u27e7 false false\n\nprivate\n module \u27e6Bool\u27e7-Internals where\n refl : Reflexive \u27e6Bool\u27e7\n refl {true} = \u27e6true\u27e7\n refl {false} = \u27e6false\u27e7\n\n sym : Symmetric \u27e6Bool\u27e7\n sym \u27e6true\u27e7 = \u27e6true\u27e7\n sym \u27e6false\u27e7 = \u27e6false\u27e7\n\n trans : Transitive \u27e6Bool\u27e7\n trans \u27e6true\u27e7 = id\n trans \u27e6false\u27e7 = id\n\n subst : \u2200 {\u2113} \u2192 Substitutive \u27e6Bool\u27e7 \u2113\n subst _ \u27e6true\u27e7 = id\n subst _ \u27e6false\u27e7 = id\n\n _\u225f_ : Decidable \u27e6Bool\u27e7\n true \u225f true = yes \u27e6true\u27e7\n false \u225f false = yes \u27e6false\u27e7\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence \u27e6Bool\u27e7\n isEquivalence = record { refl = refl; sym = sym; trans = trans }\n\n isDecEquivalence : IsDecEquivalence \u27e6Bool\u27e7\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isDecEquivalence = isDecEquivalence }\n\n equality : Equality \u27e6Bool\u27e7\n equality = record { isEquivalence = isEquivalence; subst = subst }\n\nmodule \u27e6Bool\u27e7-Props where\n open \u27e6Bool\u27e7-Internals public using (subst; decSetoid; equality)\n open DecSetoid decSetoid public\n open Equality equality public hiding (subst; isEquivalence; refl; reflexive; sym; trans)\n\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6Bool\u27e7 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63) if_then_else_ if_then_else_\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 : \u2200 {\u2113\u2081 \u2113\u2082 \u2113\u1d63} \u2192\n (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 B\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7\n \u27e8 b\u1d63 \u2236 \u27e6Bool\u27e7 \u27e9\u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 \u27e6if\u27e8 \u27e6Set\u27e7 _ \u27e9 b\u1d63 then A\u1d63 else B\u1d63 \u27e7)\n If\u2032_then_else_ If\u2032_then_else_\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n_==_ : (x y : Bool) \u2192 Bool\ntrue == true = true\ntrue == false = false\nfalse == true = false\nfalse == false = true\n\nmodule == where\n _\u2248_ : (x y : Bool) \u2192 Set\n x \u2248 y = T (x == y)\n\n refl : Reflexive _\u2248_\n refl {true} = _\n refl {false} = _\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {true} {true} _ = id\n subst _ {false} {false} _ = id\n subst _ {true} {false} ()\n subst _ {false} {true} ()\n\n sym : Symmetric _\u2248_\n sym {x} {y} eq = subst (\u03bb y \u2192 y \u2248 x) {x} {y} eq (refl {x})\n\n trans : Transitive _\u2248_\n trans {x} {y} {z} x\u2248y y\u2248z = subst (_\u2248_ x) {y} {z} y\u2248z x\u2248y\n\n _\u225f_ : Decidable _\u2248_\n true \u225f true = yes _\n false \u225f false = yes _\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence _\u2248_\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n\n isDecEquivalence : IsDecEquivalence _\u2248_\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = _\u2248_ ; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = _\u2248_; isDecEquivalence = isDecEquivalence }\n\nmodule \u27e6Bool\u27e7-Reasoning = Setoid-Reasoning \u27e6Bool\u27e7-Props.setoid\n\nopen Data.Bool public\n\n\u27e6true\u27e7\u2032 : \u2200 {b} \u2192 T b \u2192 \u27e6Bool\u27e7 true b\n\u27e6true\u27e7\u2032 {true} _ = \u27e6true\u27e7\n\u27e6true\u27e7\u2032 {false} ()\n\n\u27e6false\u27e7\u2032 : \u2200 {b} \u2192 T (not b) \u2192 \u27e6Bool\u27e7 false b\n\u27e6false\u27e7\u2032 {true} ()\n\u27e6false\u27e7\u2032 {false} _ = \u27e6false\u27e7\n\nT\u2227 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T b\u2082 \u2192 T (b\u2081 \u2227 b\u2082)\nT\u2227 p q = _\u27e8$\u27e9_ (from B.T-\u2227) (p , q)\n\nT\u2227\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2081\nT\u2227\u2081 = proj\u2081 \u2218 _\u27e8$\u27e9_ (to B.T-\u2227)\n\nT\u2227\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2082\nT\u2227\u2082 {b\u2081} = proj\u2082 \u2218 _\u27e8$\u27e9_ (to (B.T-\u2227 {b\u2081}))\n\nT\u2228'\u228e : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2228 b\u2082) \u2192 T b\u2081 \u228e T b\u2082\nT\u2228'\u228e {b\u2081} = _\u27e8$\u27e9_ (to (B.T-\u2228 {b\u2081}))\n\nT\u2228\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2081 = _\u27e8$\u27e9_ (from B.T-\u2228) \u2218 inj\u2081\n\nT\u2228\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2082 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2082 {b\u2081} = _\u27e8$\u27e9_ (from (B.T-\u2228 {b\u2081})) \u2218 inj\u2082\n\nT'not'\u00ac : \u2200 {b} \u2192 T (not b) \u2192 \u00ac (T b)\nT'not'\u00ac {false} _ = \u03bb()\nT'not'\u00ac {true} ()\n\nT'\u00ac'not : \u2200 {b} \u2192 \u00ac (T b) \u2192 T (not b)\nT'\u00ac'not {true} f = f _\nT'\u00ac'not {false} _ = _\n\n\u2227\u21d2\u2228 : \u2200 x y \u2192 T (x \u2227 y) \u2192 T (x \u2228 y)\n\u2227\u21d2\u2228 true y = _\n\u2227\u21d2\u2228 false y = \u03bb ()\n\nTdec : \u2200 b \u2192 Dec (T b)\nTdec true = yes _\nTdec false = no \u03bb()\n\nde-morgan : \u2200 x y \u2192 not (x \u2228 y) \u2261 not x \u2227 not y\nde-morgan true _ = \u2261.refl\nde-morgan false _ = \u2261.refl\n\n-- false is 0 and true is 1\nto\u2115 : Bool \u2192 \u2115\nto\u2115 b = if b then 1 else 0\n\nto\u2115\u22641 : \u2200 b \u2192 to\u2115 b \u2264 1\nto\u2115\u22641 true = s\u2264s z\u2264n\nto\u2115\u22641 false = z\u2264n\n\nxor-not-not : \u2200 x y \u2192 (not x) xor (not y) \u2261 x xor y\nxor-not-not true y = \u2261.refl\nxor-not-not false y = B.not-involutive y\n\nnot-inj : \u2200 {x y} \u2192 not x \u2261 not y \u2192 x \u2261 y\nnot-inj {true} {true} _ = \u2261.refl\nnot-inj {true} {false} ()\nnot-inj {false} {true} ()\nnot-inj {false} {false} _ = \u2261.refl\n\nxor-inj\u2081 : \u2200 x {y z} \u2192 x xor y \u2261 x xor z \u2192 y \u2261 z\nxor-inj\u2081 true = not-inj\nxor-inj\u2081 false = id\n\nxor-inj\u2082 : \u2200 x {y z} \u2192 y xor x \u2261 z xor x \u2192 y \u2261 z\nxor-inj\u2082 x {y} {z} rewrite Xor\u00b0.+-comm y x | Xor\u00b0.+-comm z x = xor-inj\u2081 x\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Bool.NP where\n\nopen import Data.Bool using (Bool; true; false; T; if_then_else_; not)\nimport Algebra\nimport Data.Bool.Properties as B\nopen import Data.Unit using (\u22a4)\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Nat using (\u2115; _\u2264_; z\u2264n; s\u2264s)\nopen import Function\nopen import Relation.Binary.NP\nopen import Relation.Binary.Logical\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\nimport Relation.Binary.PropositionalEquality as \u2261\nimport Function.Equivalence as E\nopen E.Equivalence using (to; from)\nopen import Function.Equality using (_\u27e8$\u27e9_)\nopen \u2261 using (_\u2261_)\n\nmodule Xor\u00b0 = Algebra.CommutativeRing B.commutativeRing-xor-\u2227\nmodule Bool\u00b0 = Algebra.CommutativeSemiring B.commutativeSemiring-\u2227-\u2228\n\ncheck : \u2200 b \u2192 {pf : T b} \u2192 \u22a4\ncheck = _\n\nIf_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 (T b \u2192 A) \u2192 (T (not b) \u2192 B) \u2192 if b then A else B\nIf_then_else_ true x _ = x _\nIf_then_else_ false _ x = x _\n\nIf\u2032_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 A \u2192 B \u2192 if b then A else B\nIf\u2032_then_else_ true x _ = x\nIf\u2032_then_else_ false _ x = x\n\nIf-map : \u2200 {A B C D : Set} b (f : T b \u2192 A \u2192 C) (g : T (not b) \u2192 B \u2192 D) \u2192\n if b then A else B \u2192 if b then C else D\nIf-map true f _ = f _\nIf-map false _ f = f _\n\nIf-elim : \u2200 {A B : Set} {P : Bool \u2192 Set}\n b (f : T b \u2192 A \u2192 P true) (g : T (not b) \u2192 B \u2192 P false) \u2192 if b then A else B \u2192 P b\nIf-elim true f _ = f _\nIf-elim false _ f = f _\n\nIf-true : \u2200 {A B : Set} {b} \u2192 T b \u2192 (if b then A else B) \u2261 A\nIf-true {b = true} _ = \u2261.refl\nIf-true {b = false} ()\n\nIf-false : \u2200 {A B : Set} {b} \u2192 T (not b) \u2192 (if b then A else B) \u2261 B\nIf-false {b = true} ()\nIf-false {b = false} _ = \u2261.refl\n\ncong-if : \u2200 {A B : Set} b {t\u2080 t\u2081} (f : A \u2192 B) \u2192 (if b then f t\u2080 else f t\u2081) \u2261 f (if b then t\u2080 else t\u2081)\ncong-if true _ = \u2261.refl\ncong-if false _ = \u2261.refl\n\nif-not : \u2200 {a} {A : Set a} b {t\u2080 t\u2081 : A} \u2192 (if b then t\u2080 else t\u2081) \u2261 (if not b then t\u2081 else t\u2080)\nif-not true = \u2261.refl\nif-not false = \u2261.refl\n\ndata \u27e6Bool\u27e7 : (b\u2081 b\u2082 : Bool) \u2192 Set where\n \u27e6true\u27e7 : \u27e6Bool\u27e7 true true\n \u27e6false\u27e7 : \u27e6Bool\u27e7 false false\n\nprivate\n module \u27e6Bool\u27e7-Internals where\n refl : Reflexive \u27e6Bool\u27e7\n refl {true} = \u27e6true\u27e7\n refl {false} = \u27e6false\u27e7\n\n sym : Symmetric \u27e6Bool\u27e7\n sym \u27e6true\u27e7 = \u27e6true\u27e7\n sym \u27e6false\u27e7 = \u27e6false\u27e7\n\n trans : Transitive \u27e6Bool\u27e7\n trans \u27e6true\u27e7 = id\n trans \u27e6false\u27e7 = id\n\n subst : \u2200 {\u2113} \u2192 Substitutive \u27e6Bool\u27e7 \u2113\n subst _ \u27e6true\u27e7 = id\n subst _ \u27e6false\u27e7 = id\n\n _\u225f_ : Decidable \u27e6Bool\u27e7\n true \u225f true = yes \u27e6true\u27e7\n false \u225f false = yes \u27e6false\u27e7\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence \u27e6Bool\u27e7\n isEquivalence = record { refl = refl; sym = sym; trans = trans }\n\n isDecEquivalence : IsDecEquivalence \u27e6Bool\u27e7\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isDecEquivalence = isDecEquivalence }\n\n equality : Equality \u27e6Bool\u27e7\n equality = record { isEquivalence = isEquivalence; subst = subst }\n\nmodule \u27e6Bool\u27e7-Props where\n open \u27e6Bool\u27e7-Internals public using (subst; decSetoid; equality)\n open DecSetoid decSetoid public\n open Equality equality public hiding (subst; isEquivalence; refl; reflexive; sym; trans)\n\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6Bool\u27e7 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63) if_then_else_ if_then_else_\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 : \u2200 {\u2113\u2081 \u2113\u2082 \u2113\u1d63} \u2192\n (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 B\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7\n \u27e8 b\u1d63 \u2236 \u27e6Bool\u27e7 \u27e9\u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 \u27e6if\u27e8 \u27e6Set\u27e7 _ \u27e9 b\u1d63 then A\u1d63 else B\u1d63 \u27e7)\n If\u2032_then_else_ If\u2032_then_else_\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n_==_ : (x y : Bool) \u2192 Bool\ntrue == true = true\ntrue == false = false\nfalse == true = false\nfalse == false = true\n\nmodule == where\n _\u2248_ : (x y : Bool) \u2192 Set\n x \u2248 y = T (x == y)\n\n refl : Reflexive _\u2248_\n refl {true} = _\n refl {false} = _\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {true} {true} _ = id\n subst _ {false} {false} _ = id\n subst _ {true} {false} ()\n subst _ {false} {true} ()\n\n sym : Symmetric _\u2248_\n sym {x} {y} eq = subst (\u03bb y \u2192 y \u2248 x) {x} {y} eq (refl {x})\n\n trans : Transitive _\u2248_\n trans {x} {y} {z} x\u2248y y\u2248z = subst (_\u2248_ x) {y} {z} y\u2248z x\u2248y\n\n _\u225f_ : Decidable _\u2248_\n true \u225f true = yes _\n false \u225f false = yes _\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence _\u2248_\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n\n isDecEquivalence : IsDecEquivalence _\u2248_\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = _\u2248_ ; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = _\u2248_; isDecEquivalence = isDecEquivalence }\n\nmodule \u27e6Bool\u27e7-Reasoning = Setoid-Reasoning \u27e6Bool\u27e7-Props.setoid\n\nopen Data.Bool public\n\n\u27e6true\u27e7\u2032 : \u2200 {b} \u2192 T b \u2192 \u27e6Bool\u27e7 true b\n\u27e6true\u27e7\u2032 {true} _ = \u27e6true\u27e7\n\u27e6true\u27e7\u2032 {false} ()\n\n\u27e6false\u27e7\u2032 : \u2200 {b} \u2192 T (not b) \u2192 \u27e6Bool\u27e7 false b\n\u27e6false\u27e7\u2032 {true} ()\n\u27e6false\u27e7\u2032 {false} _ = \u27e6false\u27e7\n\nT\u2227 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T b\u2082 \u2192 T (b\u2081 \u2227 b\u2082)\nT\u2227 p q = _\u27e8$\u27e9_ (from B.T-\u2227) (p , q)\n\nT\u2227\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2081\nT\u2227\u2081 = proj\u2081 \u2218 _\u27e8$\u27e9_ (to B.T-\u2227)\n\nT\u2227\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2082\nT\u2227\u2082 {b\u2081} = proj\u2082 \u2218 _\u27e8$\u27e9_ (to (B.T-\u2227 {b\u2081}))\n\nT\u2228'\u228e : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2228 b\u2082) \u2192 T b\u2081 \u228e T b\u2082\nT\u2228'\u228e {b\u2081} = _\u27e8$\u27e9_ (to (B.T-\u2228 {b\u2081}))\n\nT\u2228\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2081 = _\u27e8$\u27e9_ (from B.T-\u2228) \u2218 inj\u2081\n\nT\u2228\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2082 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2082 {b\u2081} = _\u27e8$\u27e9_ (from (B.T-\u2228 {b\u2081})) \u2218 inj\u2082\n\nT'not'\u00ac : \u2200 {b} \u2192 T (not b) \u2192 \u00ac (T b)\nT'not'\u00ac {false} _ = \u03bb()\nT'not'\u00ac {true} ()\n\nT'\u00ac'not : \u2200 {b} \u2192 \u00ac (T b) \u2192 T (not b)\nT'\u00ac'not {true} f = f _\nT'\u00ac'not {false} _ = _\n\n\u2227\u21d2\u2228 : \u2200 x y \u2192 T (x \u2227 y) \u2192 T (x \u2228 y)\n\u2227\u21d2\u2228 true y = _\n\u2227\u21d2\u2228 false y = \u03bb ()\n\nTdec : \u2200 b \u2192 Dec (T b)\nTdec true = yes _\nTdec false = no \u03bb()\n\nde-morgan : \u2200 x y \u2192 not (x \u2228 y) \u2261 not x \u2227 not y\nde-morgan true _ = \u2261.refl\nde-morgan false _ = \u2261.refl\n\n-- false is 0 and true is 1\nto\u2115 : Bool \u2192 \u2115\nto\u2115 b = if b then 1 else 0\n\nto\u2115\u22641 : \u2200 b \u2192 to\u2115 b \u2264 1\nto\u2115\u22641 true = s\u2264s z\u2264n\nto\u2115\u22641 false = z\u2264n\n\nxor-not-not : \u2200 x y \u2192 (not x) xor (not y) \u2261 x xor y\nxor-not-not true y = \u2261.refl\nxor-not-not false y = B.not-involutive y\n\nnot-inj : \u2200 {x y} \u2192 not x \u2261 not y \u2192 x \u2261 y\nnot-inj {true} {true} _ = \u2261.refl\nnot-inj {true} {false} ()\nnot-inj {false} {true} ()\nnot-inj {false} {false} _ = \u2261.refl\n\nxor-inj\u2081 : \u2200 x {y z} \u2192 x xor y \u2261 x xor z \u2192 y \u2261 z\nxor-inj\u2081 true = not-inj\nxor-inj\u2081 false = id\n\nxor-inj\u2082 : \u2200 x {y z} \u2192 y xor x \u2261 z xor x \u2192 y \u2261 z\nxor-inj\u2082 x {y} {z} rewrite Xor\u00b0.+-comm y x | Xor\u00b0.+-comm z x = xor-inj\u2081 x\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"beadc4c6bf5a0654b270878f150131cb18ada078","subject":"Data.Bool.NP: add ring modules Xor\u00b0, Bool\u00b0","message":"Data.Bool.NP: add ring modules Xor\u00b0, Bool\u00b0\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bool\/NP.agda","new_file":"lib\/Data\/Bool\/NP.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Bool.NP where\n\nopen import Data.Bool using (Bool; true; false; T; if_then_else_; not)\nimport Algebra\nimport Data.Bool.Properties\nopen import Data.Unit using (\u22a4)\nopen import Data.Sum\nopen import Function\nopen import Relation.Binary.NP\nopen import Relation.Binary.Logical\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_)\n\nmodule Xor\u00b0 = Algebra.CommutativeRing Data.Bool.Properties.commutativeRing-xor-\u2227\nmodule Bool\u00b0 = Algebra.CommutativeSemiring Data.Bool.Properties.commutativeSemiring-\u2227-\u2228\n\ncheck : \u2200 b \u2192 {pf : T b} \u2192 \u22a4\ncheck = _\n\nIf_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 (T b \u2192 A) \u2192 (T (not b) \u2192 B) \u2192 if b then A else B\nIf_then_else_ true x _ = x _\nIf_then_else_ false _ x = x _\n\nIf\u2032_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 A \u2192 B \u2192 if b then A else B\nIf\u2032_then_else_ true x _ = x\nIf\u2032_then_else_ false _ x = x\n\nIf-map : \u2200 {A B C D : Set} b (f : T b \u2192 A \u2192 C) (g : T (not b) \u2192 B \u2192 D) \u2192\n if b then A else B \u2192 if b then C else D\nIf-map true f _ = f _\nIf-map false _ f = f _\n\nIf-elim : \u2200 {A B : Set} {P : Bool \u2192 Set}\n b (f : T b \u2192 A \u2192 P true) (g : T (not b) \u2192 B \u2192 P false) \u2192 if b then A else B \u2192 P b\nIf-elim true f _ = f _\nIf-elim false _ f = f _\n\nIf-true : \u2200 {A B : Set} {b} \u2192 T b \u2192 (if b then A else B) \u2261 A\nIf-true {b = true} _ = \u2261.refl\nIf-true {b = false} ()\n\nIf-false : \u2200 {A B : Set} {b} \u2192 T (not b) \u2192 (if b then A else B) \u2261 B\nIf-false {b = true} ()\nIf-false {b = false} _ = \u2261.refl\n\ncong-if : \u2200 {A B : Set} b {t\u2080 t\u2081} (f : A \u2192 B) \u2192 (if b then f t\u2080 else f t\u2081) \u2261 f (if b then t\u2080 else t\u2081)\ncong-if true _ = \u2261.refl\ncong-if false _ = \u2261.refl\n\ndata \u27e6Bool\u27e7 : (b\u2081 b\u2082 : Bool) \u2192 Set where\n \u27e6true\u27e7 : \u27e6Bool\u27e7 true true\n \u27e6false\u27e7 : \u27e6Bool\u27e7 false false\n\nprivate\n module \u27e6Bool\u27e7-Internals where\n refl : Reflexive \u27e6Bool\u27e7\n refl {true} = \u27e6true\u27e7\n refl {false} = \u27e6false\u27e7\n\n sym : Symmetric \u27e6Bool\u27e7\n sym \u27e6true\u27e7 = \u27e6true\u27e7\n sym \u27e6false\u27e7 = \u27e6false\u27e7\n\n trans : Transitive \u27e6Bool\u27e7\n trans \u27e6true\u27e7 = id\n trans \u27e6false\u27e7 = id\n\n subst : \u2200 {\u2113} \u2192 Substitutive \u27e6Bool\u27e7 \u2113\n subst _ \u27e6true\u27e7 = id\n subst _ \u27e6false\u27e7 = id\n\n _\u225f_ : Decidable \u27e6Bool\u27e7\n true \u225f true = yes \u27e6true\u27e7\n false \u225f false = yes \u27e6false\u27e7\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence \u27e6Bool\u27e7\n isEquivalence = record { refl = refl; sym = sym; trans = trans }\n\n isDecEquivalence : IsDecEquivalence \u27e6Bool\u27e7\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isDecEquivalence = isDecEquivalence }\n\n equality : Equality \u27e6Bool\u27e7\n equality = record { isEquivalence = isEquivalence; subst = subst }\n\nmodule \u27e6Bool\u27e7-Props where\n open \u27e6Bool\u27e7-Internals public using (subst; decSetoid; equality)\n open DecSetoid decSetoid public\n open Equality equality public hiding (subst; isEquivalence; refl; reflexive; sym; trans)\n\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6Bool\u27e7 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63) if_then_else_ if_then_else_\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 : \u2200 {\u2113\u2081 \u2113\u2082 \u2113\u1d63} \u2192\n (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 B\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7\n \u27e8 b\u1d63 \u2236 \u27e6Bool\u27e7 \u27e9\u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 \u27e6if\u27e8 \u27e6Set\u27e7 _ \u27e9 b\u1d63 then A\u1d63 else B\u1d63 \u27e7)\n If\u2032_then_else_ If\u2032_then_else_\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n_==_ : (x y : Bool) \u2192 Bool\ntrue == true = true\ntrue == false = false\nfalse == true = false\nfalse == false = true\n\nmodule == where\n _\u2248_ : (x y : Bool) \u2192 Set\n x \u2248 y = T (x == y)\n\n refl : Reflexive _\u2248_\n refl {true} = _\n refl {false} = _\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {true} {true} _ = id\n subst _ {false} {false} _ = id\n subst _ {true} {false} ()\n subst _ {false} {true} ()\n\n sym : Symmetric _\u2248_\n sym {x} {y} eq = subst (\u03bb y \u2192 y \u2248 x) {x} {y} eq (refl {x})\n\n trans : Transitive _\u2248_\n trans {x} {y} {z} x\u2248y y\u2248z = subst (_\u2248_ x) {y} {z} y\u2248z x\u2248y\n\n _\u225f_ : Decidable _\u2248_\n true \u225f true = yes _\n false \u225f false = yes _\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence _\u2248_\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n\n isDecEquivalence : IsDecEquivalence _\u2248_\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = _\u2248_ ; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = _\u2248_; isDecEquivalence = isDecEquivalence }\n\nmodule \u27e6Bool\u27e7-Reasoning = Setoid-Reasoning \u27e6Bool\u27e7-Props.setoid\n\nopen Data.Bool public\n\n\u27e6true\u27e7\u2032 : \u2200 {b} \u2192 T b \u2192 \u27e6Bool\u27e7 true b\n\u27e6true\u27e7\u2032 {true} _ = \u27e6true\u27e7\n\u27e6true\u27e7\u2032 {false} ()\n\n\u27e6false\u27e7\u2032 : \u2200 {b} \u2192 T (not b) \u2192 \u27e6Bool\u27e7 false b\n\u27e6false\u27e7\u2032 {true} ()\n\u27e6false\u27e7\u2032 {false} _ = \u27e6false\u27e7\n\nT\u2227 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T b\u2082 \u2192 T (b\u2081 \u2227 b\u2082)\nT\u2227 {true} {true} _ _ = _\nT\u2227 {false} {_} () _\nT\u2227 {true} {false} _ ()\n\nT\u2227\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2081\nT\u2227\u2081 {true} {true} _ = _\nT\u2227\u2081 {false} {_} ()\nT\u2227\u2081 {true} {false} ()\n\nT\u2227\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2082\nT\u2227\u2082 {true} {true} _ = _\nT\u2227\u2082 {false} {_} ()\nT\u2227\u2082 {true} {false} ()\n\nT\u2228'\u228e : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2228 b\u2082) \u2192 T b\u2081 \u228e T b\u2082\nT\u2228'\u228e {true} _ = inj\u2081 _\nT\u2228'\u228e {false} {true} _ = inj\u2082 _\nT\u2228'\u228e {false} {false} ()\n\nT\u2228\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2081 {true} _ = _\nT\u2228\u2081 {false} {true} _ = _\nT\u2228\u2081 {false} {false} ()\n\nT\u2228\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2082 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2082 {true} _ = _\nT\u2228\u2082 {false} {true} _ = _\nT\u2228\u2082 {false} {false} ()\n\nT'not'\u00ac : \u2200 {b} \u2192 T (not b) \u2192 \u00ac (T b)\nT'not'\u00ac {false} _ = \u03bb()\nT'not'\u00ac {true} ()\n\nT'\u00ac'not : \u2200 {b} \u2192 \u00ac (T b) \u2192 T (not b)\nT'\u00ac'not {true} f = f _\nT'\u00ac'not {false} _ = _\n\nTdec : \u2200 b \u2192 Dec (T b)\nTdec true = yes _\nTdec false = no \u03bb()\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Bool.NP where\n\nopen import Data.Bool using (Bool; true; false; T; if_then_else_; not)\nopen import Data.Unit using (\u22a4)\nopen import Data.Sum\nopen import Function\nopen import Relation.Binary.NP\nopen import Relation.Binary.Logical\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_)\n\ncheck : \u2200 b \u2192 {pf : T b} \u2192 \u22a4\ncheck = _\n\nIf_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 (T b \u2192 A) \u2192 (T (not b) \u2192 B) \u2192 if b then A else B\nIf_then_else_ true x _ = x _\nIf_then_else_ false _ x = x _\n\nIf\u2032_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 A \u2192 B \u2192 if b then A else B\nIf\u2032_then_else_ true x _ = x\nIf\u2032_then_else_ false _ x = x\n\nIf-map : \u2200 {A B C D : Set} b (f : T b \u2192 A \u2192 C) (g : T (not b) \u2192 B \u2192 D) \u2192\n if b then A else B \u2192 if b then C else D\nIf-map true f _ = f _\nIf-map false _ f = f _\n\nIf-elim : \u2200 {A B : Set} {P : Bool \u2192 Set}\n b (f : T b \u2192 A \u2192 P true) (g : T (not b) \u2192 B \u2192 P false) \u2192 if b then A else B \u2192 P b\nIf-elim true f _ = f _\nIf-elim false _ f = f _\n\nIf-true : \u2200 {A B : Set} {b} \u2192 T b \u2192 (if b then A else B) \u2261 A\nIf-true {b = true} _ = \u2261.refl\nIf-true {b = false} ()\n\nIf-false : \u2200 {A B : Set} {b} \u2192 T (not b) \u2192 (if b then A else B) \u2261 B\nIf-false {b = true} ()\nIf-false {b = false} _ = \u2261.refl\n\ncong-if : \u2200 {A B : Set} b {t\u2080 t\u2081} (f : A \u2192 B) \u2192 (if b then f t\u2080 else f t\u2081) \u2261 f (if b then t\u2080 else t\u2081)\ncong-if true _ = \u2261.refl\ncong-if false _ = \u2261.refl\n\ndata \u27e6Bool\u27e7 : (b\u2081 b\u2082 : Bool) \u2192 Set where\n \u27e6true\u27e7 : \u27e6Bool\u27e7 true true\n \u27e6false\u27e7 : \u27e6Bool\u27e7 false false\n\nprivate\n module \u27e6Bool\u27e7-Internals where\n refl : Reflexive \u27e6Bool\u27e7\n refl {true} = \u27e6true\u27e7\n refl {false} = \u27e6false\u27e7\n\n sym : Symmetric \u27e6Bool\u27e7\n sym \u27e6true\u27e7 = \u27e6true\u27e7\n sym \u27e6false\u27e7 = \u27e6false\u27e7\n\n trans : Transitive \u27e6Bool\u27e7\n trans \u27e6true\u27e7 = id\n trans \u27e6false\u27e7 = id\n\n subst : \u2200 {\u2113} \u2192 Substitutive \u27e6Bool\u27e7 \u2113\n subst _ \u27e6true\u27e7 = id\n subst _ \u27e6false\u27e7 = id\n\n _\u225f_ : Decidable \u27e6Bool\u27e7\n true \u225f true = yes \u27e6true\u27e7\n false \u225f false = yes \u27e6false\u27e7\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence \u27e6Bool\u27e7\n isEquivalence = record { refl = refl; sym = sym; trans = trans }\n\n isDecEquivalence : IsDecEquivalence \u27e6Bool\u27e7\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isDecEquivalence = isDecEquivalence }\n\n equality : Equality \u27e6Bool\u27e7\n equality = record { isEquivalence = isEquivalence; subst = subst }\n\nmodule \u27e6Bool\u27e7-Props where\n open \u27e6Bool\u27e7-Internals public using (subst; decSetoid; equality)\n open DecSetoid decSetoid public\n open Equality equality public hiding (subst; isEquivalence; refl; reflexive; sym; trans)\n\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6Bool\u27e7 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63) if_then_else_ if_then_else_\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 : \u2200 {\u2113\u2081 \u2113\u2082 \u2113\u1d63} \u2192\n (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 B\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7\n \u27e8 b\u1d63 \u2236 \u27e6Bool\u27e7 \u27e9\u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 \u27e6if\u27e8 \u27e6Set\u27e7 _ \u27e9 b\u1d63 then A\u1d63 else B\u1d63 \u27e7)\n If\u2032_then_else_ If\u2032_then_else_\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n_==_ : (x y : Bool) \u2192 Bool\ntrue == true = true\ntrue == false = false\nfalse == true = false\nfalse == false = true\n\nmodule == where\n _\u2248_ : (x y : Bool) \u2192 Set\n x \u2248 y = T (x == y)\n\n refl : Reflexive _\u2248_\n refl {true} = _\n refl {false} = _\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {true} {true} _ = id\n subst _ {false} {false} _ = id\n subst _ {true} {false} ()\n subst _ {false} {true} ()\n\n sym : Symmetric _\u2248_\n sym {x} {y} eq = subst (\u03bb y \u2192 y \u2248 x) {x} {y} eq (refl {x})\n\n trans : Transitive _\u2248_\n trans {x} {y} {z} x\u2248y y\u2248z = subst (_\u2248_ x) {y} {z} y\u2248z x\u2248y\n\n _\u225f_ : Decidable _\u2248_\n true \u225f true = yes _\n false \u225f false = yes _\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence _\u2248_\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n\n isDecEquivalence : IsDecEquivalence _\u2248_\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = _\u2248_ ; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = _\u2248_; isDecEquivalence = isDecEquivalence }\n\nmodule \u27e6Bool\u27e7-Reasoning = Setoid-Reasoning \u27e6Bool\u27e7-Props.setoid\n\nopen Data.Bool public\n\n\u27e6true\u27e7\u2032 : \u2200 {b} \u2192 T b \u2192 \u27e6Bool\u27e7 true b\n\u27e6true\u27e7\u2032 {true} _ = \u27e6true\u27e7\n\u27e6true\u27e7\u2032 {false} ()\n\n\u27e6false\u27e7\u2032 : \u2200 {b} \u2192 T (not b) \u2192 \u27e6Bool\u27e7 false b\n\u27e6false\u27e7\u2032 {true} ()\n\u27e6false\u27e7\u2032 {false} _ = \u27e6false\u27e7\n\nT\u2227 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T b\u2082 \u2192 T (b\u2081 \u2227 b\u2082)\nT\u2227 {true} {true} _ _ = _\nT\u2227 {false} {_} () _\nT\u2227 {true} {false} _ ()\n\nT\u2227\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2081\nT\u2227\u2081 {true} {true} _ = _\nT\u2227\u2081 {false} {_} ()\nT\u2227\u2081 {true} {false} ()\n\nT\u2227\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2082\nT\u2227\u2082 {true} {true} _ = _\nT\u2227\u2082 {false} {_} ()\nT\u2227\u2082 {true} {false} ()\n\nT\u2228'\u228e : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2228 b\u2082) \u2192 T b\u2081 \u228e T b\u2082\nT\u2228'\u228e {true} _ = inj\u2081 _\nT\u2228'\u228e {false} {true} _ = inj\u2082 _\nT\u2228'\u228e {false} {false} ()\n\nT\u2228\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2081 {true} _ = _\nT\u2228\u2081 {false} {true} _ = _\nT\u2228\u2081 {false} {false} ()\n\nT\u2228\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2082 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2082 {true} _ = _\nT\u2228\u2082 {false} {true} _ = _\nT\u2228\u2082 {false} {false} ()\n\nT'not'\u00ac : \u2200 {b} \u2192 T (not b) \u2192 \u00ac (T b)\nT'not'\u00ac {false} _ = \u03bb()\nT'not'\u00ac {true} ()\n\nT'\u00ac'not : \u2200 {b} \u2192 \u00ac (T b) \u2192 T (not b)\nT'\u00ac'not {true} f = f _\nT'\u00ac'not {false} _ = _\n\nTdec : \u2200 b \u2192 Dec (T b)\nTdec true = yes _\nTdec false = no \u03bb()\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"09aa7263bcfccc0537afdaba6875c6d31a8fa0e1","subject":"update Group to include left and right inverse","message":"update Group to include left and right inverse\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Algebra\/Group.agda","new_file":"lib\/Algebra\/Group.agda","new_contents":"{-# OPTIONS --without-K #-}\n -- TODO\n -- If you are looking for a proof of:\n -- f (\u03a3(x\u1d62\u2208A) g(x\u2081)) \u2261 \u03a0(x\u1d62\u2208A) (f(g(x\u1d62)))\n -- Have a look to:\n -- https:\/\/github.com\/crypto-agda\/explore\/blob\/master\/lib\/Explore\/GroupHomomorphism.agda\nopen import Type using (Type_)\nopen import Data.Product.NP using (_,_;fst;snd)\nimport Algebra.FunctionProperties.Eq\nopen Algebra.FunctionProperties.Eq.Implicits\nopen import Algebra.Monoid\n\nmodule Algebra.Group where\n\nrecord Group-Ops {\u2113} (G : Type \u2113) : Type \u2113 where\n constructor _,_\n\n field\n mon-ops : Monoid-Ops G\n _\u207b\u00b9 : G \u2192 G\n\n open Monoid-Ops mon-ops public\n open From-Group-Ops \u03b5 _\u2219_ _\u207b\u00b9 public\n\nrecord Group-Struct {\u2113} {G : Type \u2113} (grp-ops : Group-Ops G) : Type \u2113 where\n constructor _,_\n open Group-Ops grp-ops\n\n -- laws\n field\n mon-struct : Monoid-Struct mon-ops\n inverse : Inverse \u03b5 _\u207b\u00b9 _\u2219_\n\n mon : Monoid G\n mon = mon-ops , mon-struct\n\n \u207b\u00b9\u2219-inverse : LeftInverse \u03b5 _\u207b\u00b9 _\u2219_\n \u207b\u00b9\u2219-inverse = fst inverse\n\n \u2219\u207b\u00b9-inverse : RightInverse \u03b5 _\u207b\u00b9 _\u2219_\n \u2219\u207b\u00b9-inverse = snd inverse\n\n open Monoid-Struct mon-struct public\n open From-Assoc-Identities-Inverse assoc identity inverse public\n\n-- TODO Monoid+LeftInverse \u2192 Group\n\nrecord Group {\u2113}(G : Type \u2113) : Type \u2113 where\n constructor _,_\n field\n grp-ops : Group-Ops G\n grp-struct : Group-Struct grp-ops\n open Group-Ops grp-ops public\n open Group-Struct grp-struct public\n\n-- A renaming of Group-Ops with additive notation\nmodule Additive-Group-Ops {\u2113}{G : Type \u2113} (grp : Group-Ops G) where\n private\n module M = Group-Ops grp\n using ()\n renaming ( _\u207b\u00b9 to 0\u2212_\n ; _\/_ to _\u2212_\n ; _^\u207b_ to _\u2297\u207b_\n ; _^_ to _\u2297_\n ; mon-ops to +-mon-ops\n ; \/= to \u2212=)\n open M public using (0\u2212_; +-mon-ops; \u2212=)\n open Additive-Monoid-Ops +-mon-ops public\n infixl 6 _\u2212_\n infixl 7 _\u2297\u207b_ _\u2297_\n _\u2212_ = M._\u2212_\n _\u2297\u207b_ = M._\u2297\u207b_\n _\u2297_ = M._\u2297_\n\n-- A renaming of Group-Struct with additive notation\nmodule Additive-Group-Struct {\u2113}{G : Type \u2113}{grp-ops : Group-Ops G}\n (grp-struct : Group-Struct grp-ops)\n = Group-Struct grp-struct\n using ()\n renaming ( mon-struct to +-mon-struct\n ; mon to +-mon\n ; assoc to +-assoc\n ; identity to +-identity\n ; \u03b5\u2219-identity to 0+-identity\n ; \u2219\u03b5-identity to +0-identity\n ; assoc= to +-assoc=\n ; !assoc= to +-!assoc=\n ; inner= to +-inner=\n ; inverse to 0\u2212-inverse\n ; \u2219-\/ to +-\u2212; \/-\u2219 to \u2212-+\n ; unique-\u03b5-left to unique-0-left\n ; unique-\u03b5-right to unique-0-right\n ; x\/y\u2261\u03b5\u2192x\u2261y to x\u2212y\u22610\u2192x\u2261y\n ; x\/y\u2262\u03b5 to x\u2212y\u22620\n ; is-\u03b5-left to is-0-left\n ; is-\u03b5-right to is-0-right\n ; unique-\u207b\u00b9 to unique-0\u2212\n ; cancels-\u2219-left to cancels-+-left\n ; cancels-\u2219-right to cancels-+-right\n ; elim-\u2219-right-\/ to elim-+-right-\u2212\n ; elim-assoc= to elim-+-assoc=\n ; elim-!assoc= to elim-+-!assoc=\n ; elim-inner= to elim-+-inner=\n ; \u207b\u00b9-hom\u2032 to 0\u2212-hom\u2032\n ; \u207b\u00b9-inj to 0\u2212-inj\n ; \u207b\u00b9-involutive to 0\u2212-involutive\n ; \u03b5\u207b\u00b9\u2261\u03b5 to 0\u22120\u22610\n )\n\n-- A renaming of Group with additive notation\nmodule Additive-Group {\u2113}{G : Type \u2113}(mon : Group G) where\n open Additive-Group-Ops (Group.grp-ops mon) public\n open Additive-Group-Struct (Group.grp-struct mon) public\n\n-- A renaming of Group-Ops with multiplicative notation\nmodule Multiplicative-Group-Ops {\u2113}{G : Type \u2113} (grp : Group-Ops G) = Group-Ops grp\n using ( _\u207b\u00b9; _\/_; \/=; _^\u207a_ ; _^\u207b_; _^_; _\u00b2; _\u00b3; _\u2074 )\n renaming ( _\u2219_ to _*_; \u03b5 to 1#; mon-ops to *-mon-ops; \u2219= to *= )\n\n-- A renaming of Group-Struct with multiplicative notation\nmodule Multiplicative-Group-Struct {\u2113}{G : Type \u2113}{grp-ops : Group-Ops G}\n (grp-struct : Group-Struct grp-ops)\n = Group-Struct grp-struct\n using ( unique-\u207b\u00b9\n ; \u207b\u00b9-hom\u2032\n ; \u207b\u00b9-inj\n ; \u207b\u00b9-involutive\n )\n renaming ( assoc to *-assoc\n ; identity to *-identity\n ; \u03b5\u2219-identity to 1*-identity\n ; \u2219\u03b5-identity to *1-identity\n ; inverse to \u207b\u00b9-inverse\n ; \u2219-\/ to *-\/; \/-\u2219 to \/-*\n ; mon-struct to *-mon-struct\n ; mon to *-mon\n ; unique-\u03b5-left to unique-1-left\n ; unique-\u03b5-right to unique-1-right\n ; x\/y\u2261\u03b5\u2192x\u2261y to x\/y\u22611\u2192x\u2261y\n ; x\/y\u2262\u03b5 to x\/y\u22621\n ; is-\u03b5-left to is-1-left\n ; is-\u03b5-right to is-1-right\n ; cancels-\u2219-left to cancels-*-left\n ; cancels-\u2219-right to cancels-*-right\n ; assoc= to *-assoc=\n ; !assoc= to *-!assoc=\n ; inner= to *-inner=\n ; elim-\u2219-right-\/ to elim-*-right-\/\n ; elim-assoc= to elim-*-assoc=\n ; elim-!assoc= to elim-*-!assoc=\n ; elim-inner= to elim-*-inner=\n ; \u03b5\u207b\u00b9\u2261\u03b5 to 1\u207b\u00b9\u22611\n )\n\n-- A renaming of Group with multiplicative notation\nmodule Multiplicative-Group {\u2113}{G : Type \u2113}(mon : Group G) where\n open Multiplicative-Group-Ops (Group.grp-ops mon) public\n open Multiplicative-Group-Struct (Group.grp-struct mon) public\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\n -- TODO\n -- If you are looking for a proof of:\n -- f (\u03a3(x\u1d62\u2208A) g(x\u2081)) \u2261 \u03a0(x\u1d62\u2208A) (f(g(x\u1d62)))\n -- Have a look to:\n -- https:\/\/github.com\/crypto-agda\/explore\/blob\/master\/lib\/Explore\/GroupHomomorphism.agda\nopen import Type using (Type_)\nopen import Data.Product.NP using (_,_)\nimport Algebra.FunctionProperties.Eq\nopen Algebra.FunctionProperties.Eq.Implicits\nopen import Algebra.Monoid\n\nmodule Algebra.Group where\n\nrecord Group-Ops {\u2113} (G : Type \u2113) : Type \u2113 where\n constructor _,_\n\n field\n mon-ops : Monoid-Ops G\n _\u207b\u00b9 : G \u2192 G\n\n open Monoid-Ops mon-ops public\n open From-Group-Ops \u03b5 _\u2219_ _\u207b\u00b9 public\n\nrecord Group-Struct {\u2113} {G : Type \u2113} (grp-ops : Group-Ops G) : Type \u2113 where\n constructor _,_\n open Group-Ops grp-ops\n\n -- laws\n field\n mon-struct : Monoid-Struct mon-ops\n inverse : Inverse \u03b5 _\u207b\u00b9 _\u2219_\n\n mon : Monoid G\n mon = mon-ops , mon-struct\n\n open Monoid-Struct mon-struct public\n open From-Assoc-Identities-Inverse assoc identity inverse public\n\n-- TODO Monoid+LeftInverse \u2192 Group\n\nrecord Group {\u2113}(G : Type \u2113) : Type \u2113 where\n constructor _,_\n field\n grp-ops : Group-Ops G\n grp-struct : Group-Struct grp-ops\n open Group-Ops grp-ops public\n open Group-Struct grp-struct public\n\n-- A renaming of Group-Ops with additive notation\nmodule Additive-Group-Ops {\u2113}{G : Type \u2113} (grp : Group-Ops G) where\n private\n module M = Group-Ops grp\n using ()\n renaming ( _\u207b\u00b9 to 0\u2212_\n ; _\/_ to _\u2212_\n ; _^\u207b_ to _\u2297\u207b_\n ; _^_ to _\u2297_\n ; mon-ops to +-mon-ops\n ; \/= to \u2212=)\n open M public using (0\u2212_; +-mon-ops; \u2212=)\n open Additive-Monoid-Ops +-mon-ops public\n infixl 6 _\u2212_\n infixl 7 _\u2297\u207b_ _\u2297_\n _\u2212_ = M._\u2212_\n _\u2297\u207b_ = M._\u2297\u207b_\n _\u2297_ = M._\u2297_\n\n-- A renaming of Group-Struct with additive notation\nmodule Additive-Group-Struct {\u2113}{G : Type \u2113}{grp-ops : Group-Ops G}\n (grp-struct : Group-Struct grp-ops)\n = Group-Struct grp-struct\n using ()\n renaming ( mon-struct to +-mon-struct\n ; mon to +-mon\n ; assoc to +-assoc\n ; identity to +-identity\n ; \u03b5\u2219-identity to 0+-identity\n ; \u2219\u03b5-identity to +0-identity\n ; assoc= to +-assoc=\n ; !assoc= to +-!assoc=\n ; inner= to +-inner=\n ; inverse to 0\u2212-inverse\n ; \u2219-\/ to +-\u2212; \/-\u2219 to \u2212-+\n ; unique-\u03b5-left to unique-0-left\n ; unique-\u03b5-right to unique-0-right\n ; x\/y\u2261\u03b5\u2192x\u2261y to x\u2212y\u22610\u2192x\u2261y\n ; x\/y\u2262\u03b5 to x\u2212y\u22620\n ; is-\u03b5-left to is-0-left\n ; is-\u03b5-right to is-0-right\n ; unique-\u207b\u00b9 to unique-0\u2212\n ; cancels-\u2219-left to cancels-+-left\n ; cancels-\u2219-right to cancels-+-right\n ; elim-\u2219-right-\/ to elim-+-right-\u2212\n ; elim-assoc= to elim-+-assoc=\n ; elim-!assoc= to elim-+-!assoc=\n ; elim-inner= to elim-+-inner=\n ; \u207b\u00b9-hom\u2032 to 0\u2212-hom\u2032\n ; \u207b\u00b9-inj to 0\u2212-inj\n ; \u207b\u00b9-involutive to 0\u2212-involutive\n ; \u03b5\u207b\u00b9\u2261\u03b5 to 0\u22120\u22610\n )\n\n-- A renaming of Group with additive notation\nmodule Additive-Group {\u2113}{G : Type \u2113}(mon : Group G) where\n open Additive-Group-Ops (Group.grp-ops mon) public\n open Additive-Group-Struct (Group.grp-struct mon) public\n\n-- A renaming of Group-Ops with multiplicative notation\nmodule Multiplicative-Group-Ops {\u2113}{G : Type \u2113} (grp : Group-Ops G) = Group-Ops grp\n using ( _\u207b\u00b9; _\/_; \/=; _^\u207a_ ; _^\u207b_; _^_; _\u00b2; _\u00b3; _\u2074 )\n renaming ( _\u2219_ to _*_; \u03b5 to 1#; mon-ops to *-mon-ops; \u2219= to *= )\n\n-- A renaming of Group-Struct with multiplicative notation\nmodule Multiplicative-Group-Struct {\u2113}{G : Type \u2113}{grp-ops : Group-Ops G}\n (grp-struct : Group-Struct grp-ops)\n = Group-Struct grp-struct\n using ( unique-\u207b\u00b9\n ; \u207b\u00b9-hom\u2032\n ; \u207b\u00b9-inj\n ; \u207b\u00b9-involutive\n )\n renaming ( assoc to *-assoc\n ; identity to *-identity\n ; \u03b5\u2219-identity to 1*-identity\n ; \u2219\u03b5-identity to *1-identity\n ; inverse to \u207b\u00b9-inverse\n ; \u2219-\/ to *-\/; \/-\u2219 to \/-*\n ; mon-struct to *-mon-struct\n ; mon to *-mon\n ; unique-\u03b5-left to unique-1-left\n ; unique-\u03b5-right to unique-1-right\n ; x\/y\u2261\u03b5\u2192x\u2261y to x\/y\u22611\u2192x\u2261y\n ; x\/y\u2262\u03b5 to x\/y\u22621\n ; is-\u03b5-left to is-1-left\n ; is-\u03b5-right to is-1-right\n ; cancels-\u2219-left to cancels-*-left\n ; cancels-\u2219-right to cancels-*-right\n ; assoc= to *-assoc=\n ; !assoc= to *-!assoc=\n ; inner= to *-inner=\n ; elim-\u2219-right-\/ to elim-*-right-\/\n ; elim-assoc= to elim-*-assoc=\n ; elim-!assoc= to elim-*-!assoc=\n ; elim-inner= to elim-*-inner=\n ; \u03b5\u207b\u00b9\u2261\u03b5 to 1\u207b\u00b9\u22611\n )\n\n-- A renaming of Group with multiplicative notation\nmodule Multiplicative-Group {\u2113}{G : Type \u2113}(mon : Group G) where\n open Multiplicative-Group-Ops (Group.grp-ops mon) public\n open Multiplicative-Group-Struct (Group.grp-struct mon) public\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"ef4cad43e7a3e2241f5561276449a1d159f04d4d","subject":"Maybe: just^, Maybe^-\u2218-+","message":"Maybe: just^, Maybe^-\u2218-+\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Maybe\/NP.agda","new_file":"lib\/Data\/Maybe\/NP.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Maybe.NP where\n\nopen import Function\nimport Level as L\nopen L using (_\u2294_; lift)\nopen import Data.Maybe public\nopen import Category.Applicative\nimport Category.Monad as Cat\nopen import Relation.Binary.PropositionalEquality as \u2261 using (_\u2261_;_\u2257_)\nopen import Relation.Binary.Logical\nopen import Function using (type-signature;_$_;flip;id)\nopen import Data.Empty using (\u22a5)\nopen import Data.Unit using (\u22a4)\nopen import Data.Nat using (\u2115; zero; suc; _+_)\n\nmodule M? \u2113 where\n open Cat.RawMonadPlus (monadPlus {\u2113}) public\n\n\u03a0? : \u2200 {a b} (A : Set a) (B : A \u2192 Set b) \u2192 Set _\n\u03a0? A B = (x : A) \u2192 Maybe (B x)\n\n_\u2192?_ : \u2200 {a b} \u2192 Set a \u2192 Set b \u2192 Set _\nA \u2192? B = A \u2192 Maybe B\n\nmap? : \u2200 {a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 Maybe A \u2192 Maybe B\nmap? f = maybe (just \u2218\u2032 f) nothing\n-- map? = M?._<$>_ _ <= not universe-polymorphic enough\n\n-- more universe-polymorphic than M?.join\njoin? : \u2200 {a} {A : Set a} \u2192 Maybe (Maybe A) \u2192 Maybe A\njoin? nothing = nothing\njoin? (just x) = x\n\nMaybe^ : \u2115 \u2192 Set \u2192 Set\nMaybe^ zero = id\nMaybe^ (suc n) = Maybe \u2218 Maybe^ n\n\njust^ : \u2200 {A} n \u2192 A \u2192 Maybe^ n A\njust^ zero x = x\njust^ (suc n) x = just (just^ n x)\n\nMaybe^-\u2218-+ : \u2200 m n A \u2192 Maybe^ m (Maybe^ n A) \u2261 Maybe^ (m + n) A\nMaybe^-\u2218-+ zero _ _ = \u2261.refl\nMaybe^-\u2218-+ (suc m) _ _ = \u2261.cong Maybe (Maybe^-\u2218-+ m _ _)\n\njust-injective : \u2200 {a} {A : Set a} {x y : A}\n \u2192 (just x \u2236 Maybe A) \u2261 just y \u2192 x \u2261 y\njust-injective \u2261.refl = \u2261.refl\n\nmaybe-just-nothing : \u2200 {a} {A : Set a} \u2192 maybe {A = A} just nothing \u2257 id\nmaybe-just-nothing (just _) = \u2261.refl\nmaybe-just-nothing nothing = \u2261.refl\n\napplicative : \u2200 {f} \u2192 RawApplicative {f} Maybe\napplicative = record { pure = just ; _\u229b_ = _\u229b_ }\n where\n _\u229b_ : \u2200 {a b}{A : Set a}{B : Set b} \u2192 Maybe (A \u2192 B) \u2192 Maybe A \u2192 Maybe B\n just f \u229b just x = just (f x)\n _ \u229b _ = nothing\n\njust? : \u2200 {a} {A : Set a} \u2192 Maybe A \u2192 Set\njust? nothing = \u22a5\njust? (just _) = \u22a4\n\ndata \u27e6Maybe\u27e7 {a b r} {A : Set a} {B : Set b} (_\u223c_ : A \u2192 B \u2192 Set r) : Maybe A \u2192 Maybe B \u2192 Set (a \u2294 b \u2294 r) where\n \u27e6just\u27e7 : \u2200 {x\u2081 x\u2082} \u2192 (x\u1d63 : x\u2081 \u223c x\u2082) \u2192 \u27e6Maybe\u27e7 _\u223c_ (just x\u2081) (just x\u2082)\n \u27e6nothing\u27e7 : \u27e6Maybe\u27e7 _\u223c_ nothing nothing\n\n\u27e6maybe\u27e7 : \u2200 {a b} \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 a \u27e9\u27e6\u2192\u27e7 (\u2200\u27e8 B\u1d63 \u2236 \u27e6Set\u27e7 b \u27e9\u27e6\u2192\u27e7 ((A\u1d63 \u27e6\u2192\u27e7 B\u1d63) \u27e6\u2192\u27e7 (B\u1d63 \u27e6\u2192\u27e7 (\u27e6Maybe\u27e7 A\u1d63 \u27e6\u2192\u27e7 B\u1d63)))))\n (maybe {a} {b}) (maybe {a} {b})\n\u27e6maybe\u27e7 _ _ just\u1d63 nothing\u1d63 (\u27e6just\u27e7 x\u1d63) = just\u1d63 x\u1d63\n\u27e6maybe\u27e7 _ _ just\u1d63 nothing\u1d63 \u27e6nothing\u27e7 = nothing\u1d63\n\n_\u27e6\u2192?\u27e7_ : \u2200 {a b c d} \u2192 (\u27e6Set\u27e7 {a} {b} c \u27e6\u2192\u27e7 \u27e6Set\u27e7 {a} {b} d \u27e6\u2192\u27e7 \u27e6Set\u27e7 _) _\u2192?_ _\u2192?_\nA\u1d63 \u27e6\u2192?\u27e7 B\u1d63 = A\u1d63 \u27e6\u2192\u27e7 \u27e6Maybe\u27e7 B\u1d63\n\nmodule \u27e6Maybe\u27e7-Properties where\n\n refl : \u2200 {a} {A : Set a} {_\u223c_ : A \u2192 A \u2192 Set} (refl-A : \u2200 x \u2192 x \u223c x) (mx : Maybe A) \u2192 \u27e6Maybe\u27e7 _\u223c_ mx mx\n refl refl-A (just x) = \u27e6just\u27e7 (refl-A x)\n refl refl-A nothing = \u27e6nothing\u27e7\n\n sym : \u2200 {a b} {A : Set a} {B : Set b} {_\u223c\u2081_ : A \u2192 B \u2192 Set} {_\u223c\u2082_ : B \u2192 A \u2192 Set}\n (sym-AB : \u2200 {x y} \u2192 x \u223c\u2081 y \u2192 y \u223c\u2082 x) {mx : Maybe A} {my : Maybe B}\n \u2192 \u27e6Maybe\u27e7 _\u223c\u2081_ mx my \u2192 \u27e6Maybe\u27e7 _\u223c\u2082_ my mx\n sym sym-A (\u27e6just\u27e7 x\u223c\u2081y) = \u27e6just\u27e7 (sym-A x\u223c\u2081y)\n sym sym-A \u27e6nothing\u27e7 = \u27e6nothing\u27e7\n\n trans : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c}\n {_\u27e6AB\u27e7_ : A \u2192 B \u2192 Set}\n {_\u27e6BC\u27e7_ : B \u2192 C \u2192 Set}\n {_\u27e6AC\u27e7_ : A \u2192 C \u2192 Set}\n (trans : \u2200 {x y z} \u2192 x \u27e6AB\u27e7 y \u2192 y \u27e6BC\u27e7 z \u2192 x \u27e6AC\u27e7 z)\n {mx : Maybe A} {my : Maybe B} {mz : Maybe C}\n \u2192 \u27e6Maybe\u27e7 _\u27e6AB\u27e7_ mx my \u2192 \u27e6Maybe\u27e7 _\u27e6BC\u27e7_ my mz\n \u2192 \u27e6Maybe\u27e7 _\u27e6AC\u27e7_ mx mz\n trans trans' (\u27e6just\u27e7 x\u223cy) (\u27e6just\u27e7 y\u223cz) = \u27e6just\u27e7 (trans' x\u223cy y\u223cz)\n trans trans' \u27e6nothing\u27e7 \u27e6nothing\u27e7 = \u27e6nothing\u27e7\n\n subst-\u27e6AB\u27e7 : \u2200 {a b} {A : Set a} {B : Set b}\n (P : Maybe A \u2192 Set)\n (Q : Maybe B \u2192 Set)\n (\u27e6AB\u27e7 : A \u2192 B \u2192 Set)\n (subst-\u27e6AB\u27e7 : \u2200 {x y} \u2192 \u27e6AB\u27e7 x y \u2192 P (just x) \u2192 Q (just y))\n (Pnothing\u2192Qnothing : P nothing \u2192 Q nothing)\n {mx : Maybe A} {my : Maybe B}\n \u2192 (\u27e6Maybe\u27e7 \u27e6AB\u27e7 mx my) \u2192 P mx \u2192 Q my\n subst-\u27e6AB\u27e7 _ _ _ subst-\u27e6AB\u27e7 _ (\u27e6just\u27e7 x\u223cy) Pmx = subst-\u27e6AB\u27e7 x\u223cy Pmx\n subst-\u27e6AB\u27e7 _ _ _ _ f \u27e6nothing\u27e7 Pnothing = f Pnothing\n\n subst : \u2200 {a} {A : Set a}\n (P : Maybe A \u2192 Set)\n (A\u1d63 : A \u2192 A \u2192 Set)\n (subst-A\u1d63 : \u2200 {x y} \u2192 A\u1d63 x y \u2192 P (just x) \u2192 P (just y))\n {mx my}\n \u2192 (\u27e6Maybe\u27e7 A\u1d63 mx my) \u2192 P mx \u2192 P my\n subst P A\u1d63 subst-A\u1d63 = subst-\u27e6AB\u27e7 P P A\u1d63 subst-A\u1d63 id\n\nIsNothing'\u2261nothing : \u2200 {a} {A : Set a} {x : Maybe A} \u2192 IsNothing x \u2192 x \u2261 nothing\nIsNothing'\u2261nothing nothing = \u2261.refl\nIsNothing'\u2261nothing (just (lift ()))\n\n\u2261nothing'IsNothing : \u2200 {a} {A : Set a} {x : Maybe A} \u2192 x \u2261 nothing \u2192 IsNothing x\n\u2261nothing'IsNothing \u2261.refl = nothing\n\n_\u2261JAll_ : \u2200 {a} {A : Set a} (x y : Maybe A) \u2192 Set a\nx \u2261JAll y = All (\u03bb y' \u2192 All (_\u2261_ y') y) x\n\n_\u2261JAny_ : \u2200 {a} {A : Set a} (x y : Maybe A) \u2192 Set a\nx \u2261JAny y = Any (\u03bb y' \u2192 Any (_\u2261_ y') y) x\n\nmodule MonadLemmas where\n\n open M? L.zero public\n -- open RawApplicative applicative public\n cong-Maybe : \u2200 {A B : Set}\n -- cong-Maybe : \u2200 {a b} {A : Set a} {B : Set b}\n (f : A \u2192 B) {x y} \u2192 x \u2261 pure y \u2192 f <$> x \u2261 pure (f y)\n cong-Maybe f \u2261.refl = \u2261.refl\n\n cong\u2082-Maybe : \u2200 {A B C : Set}\n (f : A \u2192 B \u2192 C) {x y u v} \u2192 x \u2261 pure y \u2192 u \u2261 pure v \u2192 pure f \u229b x \u229b u \u2261 pure (f y v)\n cong\u2082-Maybe f \u2261.refl \u2261.refl = \u2261.refl\n\n Maybe-comm-monad :\n \u2200 {A B C} {x y} {f : A \u2192 B \u2192 Maybe C} \u2192\n (x >>= \u03bb x' \u2192 y >>= \u03bb y' \u2192 f x' y')\n \u2261 (y >>= \u03bb y' \u2192 x >>= \u03bb x' \u2192 f x' y')\n Maybe-comm-monad {x = nothing} {nothing} = \u2261.refl\n Maybe-comm-monad {x = nothing} {just _} = \u2261.refl\n Maybe-comm-monad {x = just _} {nothing} = \u2261.refl\n Maybe-comm-monad {x = just _} {just _} = \u2261.refl\n\n Maybe-comm-appl : \u2200 {A B} {f : Maybe (A \u2192 B)} {x} \u2192 f \u229b x \u2261 (flip _$_) <$> x \u229b f\n Maybe-comm-appl {f = nothing} {nothing} = \u2261.refl\n Maybe-comm-appl {f = nothing} {just _} = \u2261.refl\n Maybe-comm-appl {f = just _} {nothing} = \u2261.refl\n Maybe-comm-appl {f = just _} {just _} = \u2261.refl\n\n Maybe-comm-appl\u2082 : \u2200 {A B C} {f : A \u2192 B \u2192 C} {x y} \u2192 f <$> x \u229b y \u2261 flip f <$> y \u229b x\n Maybe-comm-appl\u2082 {x = nothing} {nothing} = \u2261.refl\n Maybe-comm-appl\u2082 {x = nothing} {just _} = \u2261.refl\n Maybe-comm-appl\u2082 {x = just _} {nothing} = \u2261.refl\n Maybe-comm-appl\u2082 {x = just _} {just _} = \u2261.refl\n\nmodule FunctorLemmas where\n open M? L.zero\n\n <$>-injective\u2081 : \u2200 {A B : Set}\n {f : A \u2192 B} {x y : Maybe A}\n (f-inj : \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y)\n \u2192 f <$> x \u2261 f <$> y \u2192 x \u2261 y\n <$>-injective\u2081 {x = just _} {just _} f-inj eq = \u2261.cong just (f-inj (just-injective eq))\n <$>-injective\u2081 {x = nothing} {nothing} _ _ = \u2261.refl\n <$>-injective\u2081 {x = just _} {nothing} _ ()\n <$>-injective\u2081 {x = nothing} {just _} _ ()\n\n <$>-assoc : \u2200 {A B C : Set} {f : A \u2192 B} {g : C \u2192 A} (x : Maybe C) \u2192 f \u2218 g <$> x \u2261 f <$> (g <$> x)\n <$>-assoc (just _) = \u2261.refl\n <$>-assoc nothing = \u2261.refl\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Maybe.NP where\n\nopen import Function\nimport Level as L\nopen L using (_\u2294_; lift)\nopen import Data.Maybe public\nopen import Category.Applicative\nimport Category.Monad as Cat\nopen import Relation.Binary.PropositionalEquality as \u2261 using (_\u2261_;_\u2257_)\nopen import Relation.Binary.Logical\nopen import Function using (type-signature;_$_;flip;id)\nopen import Data.Empty using (\u22a5)\nopen import Data.Unit using (\u22a4)\nopen import Data.Nat using (\u2115; zero; suc)\n\nmodule M? \u2113 where\n open Cat.RawMonadPlus (monadPlus {\u2113}) public\n\n\u03a0? : \u2200 {a b} (A : Set a) (B : A \u2192 Set b) \u2192 Set _\n\u03a0? A B = (x : A) \u2192 Maybe (B x)\n\n_\u2192?_ : \u2200 {a b} \u2192 Set a \u2192 Set b \u2192 Set _\nA \u2192? B = A \u2192 Maybe B\n\nmap? : \u2200 {a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 Maybe A \u2192 Maybe B\nmap? f = maybe (just \u2218\u2032 f) nothing\n-- map? = M?._<$>_ _ <= not universe-polymorphic enough\n\n-- more universe-polymorphic than M?.join\njoin? : \u2200 {a} {A : Set a} \u2192 Maybe (Maybe A) \u2192 Maybe A\njoin? nothing = nothing\njoin? (just x) = x\n\nMaybe^ : \u2115 \u2192 Set \u2192 Set\nMaybe^ zero = id\nMaybe^ (suc n) = Maybe \u2218 Maybe^ n\n\njust-injective : \u2200 {a} {A : Set a} {x y : A}\n \u2192 (just x \u2236 Maybe A) \u2261 just y \u2192 x \u2261 y\njust-injective \u2261.refl = \u2261.refl\n\nmaybe-just-nothing : \u2200 {a} {A : Set a} \u2192 maybe {A = A} just nothing \u2257 id\nmaybe-just-nothing (just _) = \u2261.refl\nmaybe-just-nothing nothing = \u2261.refl\n\napplicative : \u2200 {f} \u2192 RawApplicative {f} Maybe\napplicative = record { pure = just ; _\u229b_ = _\u229b_ }\n where\n _\u229b_ : \u2200 {a b}{A : Set a}{B : Set b} \u2192 Maybe (A \u2192 B) \u2192 Maybe A \u2192 Maybe B\n just f \u229b just x = just (f x)\n _ \u229b _ = nothing\n\njust? : \u2200 {a} {A : Set a} \u2192 Maybe A \u2192 Set\njust? nothing = \u22a5\njust? (just _) = \u22a4\n\ndata \u27e6Maybe\u27e7 {a b r} {A : Set a} {B : Set b} (_\u223c_ : A \u2192 B \u2192 Set r) : Maybe A \u2192 Maybe B \u2192 Set (a \u2294 b \u2294 r) where\n \u27e6just\u27e7 : \u2200 {x\u2081 x\u2082} \u2192 (x\u1d63 : x\u2081 \u223c x\u2082) \u2192 \u27e6Maybe\u27e7 _\u223c_ (just x\u2081) (just x\u2082)\n \u27e6nothing\u27e7 : \u27e6Maybe\u27e7 _\u223c_ nothing nothing\n\n\u27e6maybe\u27e7 : \u2200 {a b} \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 a \u27e9\u27e6\u2192\u27e7 (\u2200\u27e8 B\u1d63 \u2236 \u27e6Set\u27e7 b \u27e9\u27e6\u2192\u27e7 ((A\u1d63 \u27e6\u2192\u27e7 B\u1d63) \u27e6\u2192\u27e7 (B\u1d63 \u27e6\u2192\u27e7 (\u27e6Maybe\u27e7 A\u1d63 \u27e6\u2192\u27e7 B\u1d63)))))\n (maybe {a} {b}) (maybe {a} {b})\n\u27e6maybe\u27e7 _ _ just\u1d63 nothing\u1d63 (\u27e6just\u27e7 x\u1d63) = just\u1d63 x\u1d63\n\u27e6maybe\u27e7 _ _ just\u1d63 nothing\u1d63 \u27e6nothing\u27e7 = nothing\u1d63\n\n_\u27e6\u2192?\u27e7_ : \u2200 {a b c d} \u2192 (\u27e6Set\u27e7 {a} {b} c \u27e6\u2192\u27e7 \u27e6Set\u27e7 {a} {b} d \u27e6\u2192\u27e7 \u27e6Set\u27e7 _) _\u2192?_ _\u2192?_\nA\u1d63 \u27e6\u2192?\u27e7 B\u1d63 = A\u1d63 \u27e6\u2192\u27e7 \u27e6Maybe\u27e7 B\u1d63\n\nmodule \u27e6Maybe\u27e7-Properties where\n\n refl : \u2200 {a} {A : Set a} {_\u223c_ : A \u2192 A \u2192 Set} (refl-A : \u2200 x \u2192 x \u223c x) (mx : Maybe A) \u2192 \u27e6Maybe\u27e7 _\u223c_ mx mx\n refl refl-A (just x) = \u27e6just\u27e7 (refl-A x)\n refl refl-A nothing = \u27e6nothing\u27e7\n\n sym : \u2200 {a b} {A : Set a} {B : Set b} {_\u223c\u2081_ : A \u2192 B \u2192 Set} {_\u223c\u2082_ : B \u2192 A \u2192 Set}\n (sym-AB : \u2200 {x y} \u2192 x \u223c\u2081 y \u2192 y \u223c\u2082 x) {mx : Maybe A} {my : Maybe B}\n \u2192 \u27e6Maybe\u27e7 _\u223c\u2081_ mx my \u2192 \u27e6Maybe\u27e7 _\u223c\u2082_ my mx\n sym sym-A (\u27e6just\u27e7 x\u223c\u2081y) = \u27e6just\u27e7 (sym-A x\u223c\u2081y)\n sym sym-A \u27e6nothing\u27e7 = \u27e6nothing\u27e7\n\n trans : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c}\n {_\u27e6AB\u27e7_ : A \u2192 B \u2192 Set}\n {_\u27e6BC\u27e7_ : B \u2192 C \u2192 Set}\n {_\u27e6AC\u27e7_ : A \u2192 C \u2192 Set}\n (trans : \u2200 {x y z} \u2192 x \u27e6AB\u27e7 y \u2192 y \u27e6BC\u27e7 z \u2192 x \u27e6AC\u27e7 z)\n {mx : Maybe A} {my : Maybe B} {mz : Maybe C}\n \u2192 \u27e6Maybe\u27e7 _\u27e6AB\u27e7_ mx my \u2192 \u27e6Maybe\u27e7 _\u27e6BC\u27e7_ my mz\n \u2192 \u27e6Maybe\u27e7 _\u27e6AC\u27e7_ mx mz\n trans trans' (\u27e6just\u27e7 x\u223cy) (\u27e6just\u27e7 y\u223cz) = \u27e6just\u27e7 (trans' x\u223cy y\u223cz)\n trans trans' \u27e6nothing\u27e7 \u27e6nothing\u27e7 = \u27e6nothing\u27e7\n\n subst-\u27e6AB\u27e7 : \u2200 {a b} {A : Set a} {B : Set b}\n (P : Maybe A \u2192 Set)\n (Q : Maybe B \u2192 Set)\n (\u27e6AB\u27e7 : A \u2192 B \u2192 Set)\n (subst-\u27e6AB\u27e7 : \u2200 {x y} \u2192 \u27e6AB\u27e7 x y \u2192 P (just x) \u2192 Q (just y))\n (Pnothing\u2192Qnothing : P nothing \u2192 Q nothing)\n {mx : Maybe A} {my : Maybe B}\n \u2192 (\u27e6Maybe\u27e7 \u27e6AB\u27e7 mx my) \u2192 P mx \u2192 Q my\n subst-\u27e6AB\u27e7 _ _ _ subst-\u27e6AB\u27e7 _ (\u27e6just\u27e7 x\u223cy) Pmx = subst-\u27e6AB\u27e7 x\u223cy Pmx\n subst-\u27e6AB\u27e7 _ _ _ _ f \u27e6nothing\u27e7 Pnothing = f Pnothing\n\n subst : \u2200 {a} {A : Set a}\n (P : Maybe A \u2192 Set)\n (A\u1d63 : A \u2192 A \u2192 Set)\n (subst-A\u1d63 : \u2200 {x y} \u2192 A\u1d63 x y \u2192 P (just x) \u2192 P (just y))\n {mx my}\n \u2192 (\u27e6Maybe\u27e7 A\u1d63 mx my) \u2192 P mx \u2192 P my\n subst P A\u1d63 subst-A\u1d63 = subst-\u27e6AB\u27e7 P P A\u1d63 subst-A\u1d63 id\n\nIsNothing'\u2261nothing : \u2200 {a} {A : Set a} {x : Maybe A} \u2192 IsNothing x \u2192 x \u2261 nothing\nIsNothing'\u2261nothing nothing = \u2261.refl\nIsNothing'\u2261nothing (just (lift ()))\n\n\u2261nothing'IsNothing : \u2200 {a} {A : Set a} {x : Maybe A} \u2192 x \u2261 nothing \u2192 IsNothing x\n\u2261nothing'IsNothing \u2261.refl = nothing\n\n_\u2261JAll_ : \u2200 {a} {A : Set a} (x y : Maybe A) \u2192 Set a\nx \u2261JAll y = All (\u03bb y' \u2192 All (_\u2261_ y') y) x\n\n_\u2261JAny_ : \u2200 {a} {A : Set a} (x y : Maybe A) \u2192 Set a\nx \u2261JAny y = Any (\u03bb y' \u2192 Any (_\u2261_ y') y) x\n\nmodule MonadLemmas where\n\n open M? L.zero public\n -- open RawApplicative applicative public\n cong-Maybe : \u2200 {A B : Set}\n -- cong-Maybe : \u2200 {a b} {A : Set a} {B : Set b}\n (f : A \u2192 B) {x y} \u2192 x \u2261 pure y \u2192 f <$> x \u2261 pure (f y)\n cong-Maybe f \u2261.refl = \u2261.refl\n\n cong\u2082-Maybe : \u2200 {A B C : Set}\n (f : A \u2192 B \u2192 C) {x y u v} \u2192 x \u2261 pure y \u2192 u \u2261 pure v \u2192 pure f \u229b x \u229b u \u2261 pure (f y v)\n cong\u2082-Maybe f \u2261.refl \u2261.refl = \u2261.refl\n\n Maybe-comm-monad :\n \u2200 {A B C} {x y} {f : A \u2192 B \u2192 Maybe C} \u2192\n (x >>= \u03bb x' \u2192 y >>= \u03bb y' \u2192 f x' y')\n \u2261 (y >>= \u03bb y' \u2192 x >>= \u03bb x' \u2192 f x' y')\n Maybe-comm-monad {x = nothing} {nothing} = \u2261.refl\n Maybe-comm-monad {x = nothing} {just _} = \u2261.refl\n Maybe-comm-monad {x = just _} {nothing} = \u2261.refl\n Maybe-comm-monad {x = just _} {just _} = \u2261.refl\n\n Maybe-comm-appl : \u2200 {A B} {f : Maybe (A \u2192 B)} {x} \u2192 f \u229b x \u2261 (flip _$_) <$> x \u229b f\n Maybe-comm-appl {f = nothing} {nothing} = \u2261.refl\n Maybe-comm-appl {f = nothing} {just _} = \u2261.refl\n Maybe-comm-appl {f = just _} {nothing} = \u2261.refl\n Maybe-comm-appl {f = just _} {just _} = \u2261.refl\n\n Maybe-comm-appl\u2082 : \u2200 {A B C} {f : A \u2192 B \u2192 C} {x y} \u2192 f <$> x \u229b y \u2261 flip f <$> y \u229b x\n Maybe-comm-appl\u2082 {x = nothing} {nothing} = \u2261.refl\n Maybe-comm-appl\u2082 {x = nothing} {just _} = \u2261.refl\n Maybe-comm-appl\u2082 {x = just _} {nothing} = \u2261.refl\n Maybe-comm-appl\u2082 {x = just _} {just _} = \u2261.refl\n\nmodule FunctorLemmas where\n open M? L.zero\n\n <$>-injective\u2081 : \u2200 {A B : Set}\n {f : A \u2192 B} {x y : Maybe A}\n (f-inj : \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y)\n \u2192 f <$> x \u2261 f <$> y \u2192 x \u2261 y\n <$>-injective\u2081 {x = just _} {just _} f-inj eq = \u2261.cong just (f-inj (just-injective eq))\n <$>-injective\u2081 {x = nothing} {nothing} _ _ = \u2261.refl\n <$>-injective\u2081 {x = just _} {nothing} _ ()\n <$>-injective\u2081 {x = nothing} {just _} _ ()\n\n <$>-assoc : \u2200 {A B C : Set} {f : A \u2192 B} {g : C \u2192 A} (x : Maybe C) \u2192 f \u2218 g <$> x \u2261 f <$> (g <$> x)\n <$>-assoc (just _) = \u2261.refl\n <$>-assoc nothing = \u2261.refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"84190aeeefe0df40d05f1276e5972c27c14ee9db","subject":"HoTTify grouphomomorphism","message":"HoTTify grouphomomorphism\n","repos":"crypto-agda\/explore","old_file":"lib\/Explore\/GroupHomomorphism.agda","new_file":"lib\/Explore\/GroupHomomorphism.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule Explore.GroupHomomorphism where\n\nopen import Level\nopen import Algebra.FunctionProperties\nopen import Data.Product\nopen import Function using (_\u2218_ ; flip)\nopen import Function.Inverse as Inv using (_\u2194_; module Inverse)\nopen import Relation.Binary.PropositionalEquality.NP hiding (_\u2219_)\n\nopen import Explore.Core\nopen import Explore.Properties\nopen import Explore.Sum\n\n{-\n I had some problems with using the standard library definiton of Groups\n so I rolled my own, therefor I need some boring proofs first\n\n-}\nrecord Group (G : Set) : Set where\n field\n _\u2219_ : G \u2192 G \u2192 G\n \u03b5 : G\n -_ : G \u2192 G\n\n -- laws\n field\n assoc : Associative _\u2261_ _\u2219_\n identity : Identity _\u2261_ \u03b5 _\u2219_\n inverse : Inverse _\u2261_ \u03b5 -_ _\u2219_\n\n -- derived property\n help : \u2200 x y \u2192 x \u2261 (x \u2219 y) \u2219 - y\n help x y = x\n \u2261\u27e8 ! proj\u2082 identity x \u27e9\n x \u2219 \u03b5\n \u2261\u27e8 ap (_\u2219_ x) (! proj\u2082 inverse y) \u27e9\n x \u2219 (y \u2219 - y)\n \u2261\u27e8 ! assoc x y (- y) \u27e9\n (x \u2219 y) \u2219 (- y)\n \u220e\n where open \u2261-Reasoning\n\n unique-1g : \u2200 x y \u2192 x \u2219 y \u2261 y \u2192 x \u2261 \u03b5\n unique-1g x y eq = x\n \u2261\u27e8 help x y \u27e9\n (x \u2219 y) \u2219 - y\n \u2261\u27e8 ap (flip _\u2219_ (- y)) eq \u27e9\n y \u2219 - y\n \u2261\u27e8 proj\u2082 inverse y \u27e9\n \u03b5\n \u220e\n where open \u2261-Reasoning\n\n unique-\/ : \u2200 x y \u2192 x \u2219 y \u2261 \u03b5 \u2192 x \u2261 - y\n unique-\/ x y eq = x\n \u2261\u27e8 help x y \u27e9\n (x \u2219 y) \u2219 - y\n \u2261\u27e8 ap (flip _\u2219_ (- y)) eq \u27e9\n \u03b5 \u2219 - y\n \u2261\u27e8 proj\u2081 identity (- y) \u27e9\n - y\n \u220e\n where open \u2261-Reasoning\n\nmodule _ {A B : Set}(GA : Group A)(GB : Group B) where\n open Group GA using (-_;inverse;identity) renaming (_\u2219_ to _+_; \u03b5 to 0g)\n open Group GB using (unique-1g ; unique-\/) renaming (_\u2219_ to _*_; \u03b5 to 1g; -_ to 1\/_)\n\n GroupHomomorphism : (A \u2192 B) \u2192 Set\n GroupHomomorphism f = \u2200 x y \u2192 f (x + y) \u2261 f x * f y\n\n module GroupHomomorphismProp {f}(f-homo : GroupHomomorphism f) where\n f-pres-\u03b5 : f 0g \u2261 1g\n f-pres-\u03b5 = unique-1g (f 0g) (f 0g) part\n where open \u2261-Reasoning\n part = f 0g * f 0g\n \u2261\u27e8 ! f-homo 0g 0g \u27e9\n f (0g + 0g)\n \u2261\u27e8 ap f (proj\u2081 identity 0g) \u27e9\n f 0g\n \u220e\n\n f-pres-inv : \u2200 x \u2192 f (- x) \u2261 1\/ f x\n f-pres-inv x = unique-\/ (f (- x)) (f x) part\n where open \u2261-Reasoning\n part = f (- x) * f x\n \u2261\u27e8 ! f-homo (- x) x \u27e9\n f (- x + x)\n \u2261\u27e8 ap f (proj\u2081 inverse x) \u27e9\n f 0g\n \u2261\u27e8 f-pres-\u03b5 \u27e9\n 1g\n \u220e\n\nmodule _ {A B}(GA : Group A)(GB : Group B)\n (f : A \u2192 B)\n (exploreA : Explore zero A)(f-homo : GroupHomomorphism GA GB f)\n ([f] : B \u2192 A)(f-sur : \u2200 b \u2192 f ([f] b) \u2261 b)\n (explore-ext : ExploreExt exploreA)\n where\n open Group GA using (-_) renaming (_\u2219_ to _+_ ; \u03b5 to 0g)\n open Group GB using () renaming (_\u2219_ to _*_ ; \u03b5 to 1g ; -_ to 1\/_)\n open GroupHomomorphismProp GA GB f-homo\n\n {- How all this is related to elgamal\n\n the Group GA is \u2124q with modular addition as operation\n the Group GB is the cyclic group with order q\n\n f is g^, the proof only need that it is a group homomorphism\n and that it has a right inverse\n\n we require that the explore (for type A) function (should work with only summation)\n is Stable under addition of GA (notice that we have flip in there that is so that\n we don't need commutativity\n\n finally we require that the explore function respects extensionality\n -}\n\n {-\n While this proof looks complicated it basically just adds inverse of m\u2080 and then adds m\u2081 (from image of f)\n we need the homomorphic property to pull out the values.\n\n -}\n\n module _ {X}(z : X)(op : X \u2192 X \u2192 X)\n (sui : \u2200 k \u2192 StableUnder' exploreA z op (flip (Group._\u2219_ GA) k))\n where\n -- this proof isn't actually any hard..\n thm : \u2200 (O : B \u2192 X) m\u2080 m\u2081 \u2192 exploreA z op (\u03bb x \u2192 O (f x * m\u2080)) \u2261 exploreA z op (\u03bb x \u2192 O (f x * m\u2081))\n thm O m\u2080 m\u2081 = explore (\u03bb x \u2192 O (f x * m\u2080))\n \u2261\u27e8 sui (- [f] m\u2080) (\u03bb x \u2192 O (f x * m\u2080)) \u27e9\n explore (\u03bb x \u2192 O (f (x + - [f] m\u2080) * m\u2080))\n \u2261\u27e8 explore-ext z op (\u03bb x \u2192 ap O (lemma1 x)) \u27e9\n explore (\u03bb x \u2192 O (f x ))\n \u2261\u27e8 sui ([f] m\u2081) (O \u2218 f) \u27e9\n explore (\u03bb x \u2192 O (f (x + [f] m\u2081)))\n \u2261\u27e8 explore-ext z op (\u03bb x \u2192 ap O (lemma2 x)) \u27e9\n explore (\u03bb x \u2192 O (f x * m\u2081))\n \u220e\n where\n open \u2261-Reasoning\n explore = exploreA z op\n\n lemma1 : \u2200 x \u2192 f (x + - [f] m\u2080) * m\u2080 \u2261 f x\n lemma1 x rewrite f-homo x (- [f] m\u2080)\n | f-pres-inv ([f] m\u2080)\n | f-sur m\u2080\n | Group.assoc GB (f x) (1\/ m\u2080) m\u2080\n | proj\u2081 (Group.inverse GB) m\u2080\n | proj\u2082 (Group.identity GB) (f x) = refl\n\n lemma2 : \u2200 x \u2192 f (x + [f] m\u2081) \u2261 f x * m\u2081\n lemma2 x rewrite f-homo x ([f] m\u2081)\n | f-sur m\u2081 = refl\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule Explore.GroupHomomorphism where\n\nopen import Level\nopen import Algebra.FunctionProperties\nopen import Data.Product\nopen import Function using (_\u2218_ ; flip)\nopen import Function.Inverse as Inv using (_\u2194_; module Inverse)\nopen import Relation.Binary.PropositionalEquality.NP hiding (_\u2219_)\n\nopen import Explore.Core\nopen import Explore.Properties\nopen import Explore.Sum\n\n{-\n I had some problems with using the standard library definiton of Groups\n so I rolled my own, therefor I need some boring proofs first\n\n-}\nrecord Group (G : Set) : Set where\n field\n _\u2219_ : G \u2192 G \u2192 G\n \u03b5 : G\n -_ : G \u2192 G\n\n -- laws\n field\n assoc : Associative _\u2261_ _\u2219_\n identity : Identity _\u2261_ \u03b5 _\u2219_\n inverse : Inverse _\u2261_ \u03b5 -_ _\u2219_\n\n -- derived property\n help : \u2200 x y \u2192 x \u2261 (x \u2219 y) \u2219 - y\n help x y = x\n \u2261\u27e8 sym (proj\u2082 identity x) \u27e9\n x \u2219 \u03b5\n \u2261\u27e8 cong (_\u2219_ x) (sym (proj\u2082 inverse y)) \u27e9\n x \u2219 (y \u2219 - y)\n \u2261\u27e8 sym (assoc x y (- y)) \u27e9\n (x \u2219 y) \u2219 (- y)\n \u220e\n where open \u2261-Reasoning\n\n unique-1g : \u2200 x y \u2192 x \u2219 y \u2261 y \u2192 x \u2261 \u03b5\n unique-1g x y eq = x\n \u2261\u27e8 help x y \u27e9\n (x \u2219 y) \u2219 - y\n \u2261\u27e8 cong (flip _\u2219_ (- y)) eq \u27e9\n y \u2219 - y\n \u2261\u27e8 proj\u2082 inverse y \u27e9\n \u03b5\n \u220e\n where open \u2261-Reasoning\n\n unique-\/ : \u2200 x y \u2192 x \u2219 y \u2261 \u03b5 \u2192 x \u2261 - y\n unique-\/ x y eq = x\n \u2261\u27e8 help x y \u27e9\n (x \u2219 y) \u2219 - y\n \u2261\u27e8 cong (flip _\u2219_ (- y)) eq \u27e9\n \u03b5 \u2219 - y\n \u2261\u27e8 proj\u2081 identity (- y) \u27e9\n - y\n \u220e\n where open \u2261-Reasoning\n\nmodule _ {A B : Set}(GA : Group A)(GB : Group B) where\n open Group GA using (-_) renaming (_\u2219_ to _+_; \u03b5 to 0g)\n open Group GB using (unique-1g ; unique-\/) renaming (_\u2219_ to _*_; \u03b5 to 1g; -_ to 1\/_)\n\n GroupHomomorphism : (A \u2192 B) \u2192 Set\n GroupHomomorphism f = \u2200 x y \u2192 f (x + y) \u2261 f x * f y\n\n module GroupHomomorphismProp {f}(f-homo : GroupHomomorphism f) where\n f-pres-\u03b5 : f 0g \u2261 1g\n f-pres-\u03b5 = unique-1g (f 0g) (f 0g) part\n where open \u2261-Reasoning\n open Group GA using (identity)\n part = f 0g * f 0g\n \u2261\u27e8 sym (f-homo 0g 0g) \u27e9\n f (0g + 0g)\n \u2261\u27e8 cong f (proj\u2081 identity 0g) \u27e9\n f 0g\n \u220e\n\n f-pres-inv : \u2200 x \u2192 f (- x) \u2261 1\/ f x\n f-pres-inv x = unique-\/ (f (- x)) (f x) part\n where open \u2261-Reasoning\n open Group GA using (inverse)\n part = f (- x) * f x\n \u2261\u27e8 sym (f-homo (- x) x) \u27e9\n f (- x + x)\n \u2261\u27e8 cong f (proj\u2081 inverse x) \u27e9\n f 0g\n \u2261\u27e8 f-pres-\u03b5 \u27e9\n 1g\n \u220e\n\nmodule _ {A B}(GA : Group A)(GB : Group B)\n (f : A \u2192 B)\n (exploreA : Explore zero A)(f-homo : GroupHomomorphism GA GB f)\n ([f] : B \u2192 A)(f-sur : \u2200 b \u2192 f ([f] b) \u2261 b)\n (explore-ext : ExploreExt exploreA)\n where\n open Group GA using (-_) renaming (_\u2219_ to _+_ ; \u03b5 to 0g)\n open Group GB using () renaming (_\u2219_ to _*_ ; \u03b5 to 1g ; -_ to 1\/_)\n open GroupHomomorphismProp GA GB f-homo\n\n {- How all this is related to elgamal\n\n the Group GA is \u2124q with modular addition as operation\n the Group GB is the cyclic group with order q\n\n f is g^, the proof only need that it is a group homomorphism\n and that it has a right inverse\n\n we require that the explore (for type A) function (should work with only summation)\n is Stable under addition of GA (notice that we have flip in there that is so that\n we don't need commutativity\n\n finally we require that the explore function respects extensionality\n -}\n\n {-\n While this proof looks complicated it basically just adds inverse of m\u2080 and then adds m\u2081 (from image of f)\n we need the homomorphic property to pull out the values.\n\n -}\n\n module _ {X}(z : X)(op : X \u2192 X \u2192 X)\n (sui : \u2200 k \u2192 StableUnder' exploreA z op (flip (Group._\u2219_ GA) k))\n where\n -- this proof isn't actually any hard..\n thm : \u2200 (O : B \u2192 X) m\u2080 m\u2081 \u2192 exploreA z op (\u03bb x \u2192 O (f x * m\u2080)) \u2261 exploreA z op (\u03bb x \u2192 O (f x * m\u2081))\n thm O m\u2080 m\u2081 = explore (\u03bb x \u2192 O (f x * m\u2080))\n \u2261\u27e8 sui (- [f] m\u2080) (\u03bb x \u2192 O (f x * m\u2080)) \u27e9\n explore (\u03bb x \u2192 O (f (x + - [f] m\u2080) * m\u2080))\n \u2261\u27e8 explore-ext z op (\u03bb x \u2192 cong O (lemma1 x)) \u27e9\n explore (\u03bb x \u2192 O (f x ))\n \u2261\u27e8 sui ([f] m\u2081) (O \u2218 f) \u27e9\n explore (\u03bb x \u2192 O (f (x + [f] m\u2081)))\n \u2261\u27e8 explore-ext z op (\u03bb x \u2192 cong O (lemma2 x)) \u27e9\n explore (\u03bb x \u2192 O (f x * m\u2081))\n \u220e\n where\n open \u2261-Reasoning\n explore = exploreA z op\n\n lemma1 : \u2200 x \u2192 f (x + - [f] m\u2080) * m\u2080 \u2261 f x\n lemma1 x rewrite f-homo x (- [f] m\u2080)\n | f-pres-inv ([f] m\u2080)\n | f-sur m\u2080\n | Group.assoc GB (f x) (1\/ m\u2080) m\u2080\n | proj\u2081 (Group.inverse GB) m\u2080\n | proj\u2082 (Group.identity GB) (f x) = refl\n\n lemma2 : \u2200 x \u2192 f (x + [f] m\u2081) \u2261 f x * m\u2081\n lemma2 x rewrite f-homo x ([f] m\u2081)\n | f-sur m\u2081 = refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"1ed3194fa9dfc175ebb7232172842e35a201ecc1","subject":"Updated a note on the existential elimination.","message":"Updated a note on the existential elimination.\n\nIgnore-this: fc0913e5a6f5eb20a7f038ae272d00fe\n\ndarcs-hash:20120226141843-3bd4e-3ceaba139b8cf1afb4fe19775c5d1e65df77d8ec.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/Existential.agda","new_file":"notes\/Existential.agda","new_contents":"-- Tested with the development version of Agda on 26 February 2012.\n\nmodule Existential where\n\ninfixr 7 _,_\n\npostulate\n D : Set\n P Q : D \u2192 Set\n P\u2192Q : \u2200 {x} \u2192 P x \u2192 Q x\n\ndata \u2203 (P : D \u2192 Set) : Set where\n _,_ : (x : D) \u2192 P x \u2192 \u2203 P\n\n\u2203-elim : {P : D \u2192 Set}{Q : Set} \u2192 \u2203 P \u2192 (\u2200 x \u2192 P x \u2192 Q) \u2192 Q\n\u2203-elim (x , p) h = h x p\n\n-- A proof using the existential elimination.\nprf\u2081 : \u2203 P \u2192 \u2203 Q\nprf\u2081 h = \u2203-elim h (\u03bb x Px \u2192 x , P\u2192Q Px)\n\n-- A proof using the existential elimination with a helper function.\nprf\u2082 : \u2203 P \u2192 \u2203 Q\nprf\u2082 h = \u2203-elim h helper\n where\n helper : \u2200 x \u2192 P x \u2192 \u2203 Q\n helper x Px = x , P\u2192Q Px\n\n-- A proof using pattern matching.\nprf\u2083 : \u2203 P \u2192 \u2203 Q\nprf\u2083 (x , Px) = x , P\u2192Q Px\n","old_contents":"-- Tested with the development version of Agda on 25 February 2012.\n\nmodule Existential where\n\ninfixr 7 _,_\n\npostulate\n D : Set\n P Q : D \u2192 Set\n P\u2192Q : \u2200 {x} \u2192 P x \u2192 Q x\n\ndata \u2203 (P : D \u2192 Set) : Set where\n _,_ : (x : D) \u2192 P x \u2192 \u2203 P\n\n\u2203-elim : {P : D \u2192 Set}{Q : Set} \u2192 \u2203 P \u2192 (\u2200 x \u2192 P x \u2192 Q) \u2192 Q\n\u2203-elim (x , p) h = h x p\n\nthm\u2081 : \u2203 P \u2192 \u2203 Q\nthm\u2081 h = \u2203-elim h (\u03bb x Px \u2192 x , P\u2192Q Px)\n\nthm\u2082 : \u2203 P \u2192 \u2203 Q\nthm\u2082 h = \u2203-elim h helper\n where\n helper : \u2200 x \u2192 P x \u2192 \u2203 Q\n helper x Px = x , P\u2192Q Px\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"039ad45a017c28bce761d79c39ee6ce685fde2a5","subject":"Added Nx+11.","message":"Added Nx+11.\n\nIgnore-this: 70697fba4fa0352e7230a83d70878b15\n\ndarcs-hash:20110211212344-3bd4e-9c970f14fcec789dcb7e01b02ea6f379dae2dc5a.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/McCarthy91\/ArithmeticATP.agda","new_file":"Draft\/McCarthy91\/ArithmeticATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Arithmetic stuff used by the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.ArithmeticATP where\n\nopen import LTC.Base\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\n\n------------------------------------------------------------------------------\n\npostulate\n N1+n : \u2200 {n} \u2192 N n \u2192 N (one + n)\n N11+n : \u2200 {n} \u2192 N n \u2192 N (eleven + n)\n Nn-10 : \u2200 {n} \u2192 N n \u2192 N (n \u2238 ten)\n Nx+11 : \u2200 {n} \u2192 N n \u2192 N (n + eleven)\n{-# ATP prove N1+n #-}\n{-# ATP prove N11+n #-}\n{-# ATP prove Nn-10 \u2238-N N10 #-}\n{-# ATP prove Nx+11 N11+n +-comm #-}\n\npostulate\n n111' : eleven + one-hundred \u2261 hundred-eleven\n n111 : one-hundred + eleven \u2261 hundred-eleven\n n101' : hundred-eleven \u2238 ten \u2261 hundred-one\n n101 : (one-hundred + eleven) \u2238 ten \u2261 hundred-one\n n91' : hundred-one \u2238 ten \u2261 ninety-one\n n91 : ((one-hundred + eleven) \u2238 ten) \u2238 ten \u2261 ninety-one\n n102' : eleven + ninety-one \u2261 hundred-two\n n102 : ninety-one + eleven \u2261 hundred-two\n{-# ATP prove n111' #-}\n{-# ATP prove n111 +-comm n111' #-}\n{-# ATP prove n101' #-}\n{-# ATP prove n101 n111 n101' #-}\n{-# ATP prove n91' #-}\n{-# ATP prove n91 n101 n91' #-}\n{-# ATP prove n102' #-}\n{-# ATP prove n102 n102' +-comm #-}\n\n\npostulate\n 101>100' : GT hundred-one one-hundred\n 101>100 : GT ((one-hundred + eleven) \u2238 ten) one-hundred\n 111>100' : GT hundred-eleven one-hundred\n 111>100 : GT (one-hundred + eleven) one-hundred\n 100<102' : LT one-hundred hundred-two\n 100<102 : LT one-hundred (ninety-one + eleven)\n{-# ATP prove 101>100' #-}\n{-# ATP prove 101>100 101>100' n101 #-}\n{-# ATP prove 111>100' #-}\n{-# ATP prove 111>100 111>100' n111 #-}\n{-# ATP prove 100<102' #-}\n{-# ATP prove 100<102 100<102' n102 #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Arithmetic stuff used by the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.ArithmeticATP where\n\nopen import LTC.Base\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\n\n------------------------------------------------------------------------------\n\npostulate\n N1+n : \u2200 {n} \u2192 N n \u2192 N (one + n)\n N11+n : \u2200 {n} \u2192 N n \u2192 N (eleven + n)\n Nn-10 : \u2200 {n} \u2192 N n \u2192 N (n \u2238 ten)\n{-# ATP prove N1+n #-}\n{-# ATP prove N11+n #-}\n{-# ATP prove Nn-10 \u2238-N N10 #-}\n\n\npostulate\n n111' : eleven + one-hundred \u2261 hundred-eleven\n n111 : one-hundred + eleven \u2261 hundred-eleven\n n101' : hundred-eleven \u2238 ten \u2261 hundred-one\n n101 : (one-hundred + eleven) \u2238 ten \u2261 hundred-one\n n91' : hundred-one \u2238 ten \u2261 ninety-one\n n91 : ((one-hundred + eleven) \u2238 ten) \u2238 ten \u2261 ninety-one\n n102' : eleven + ninety-one \u2261 hundred-two\n n102 : ninety-one + eleven \u2261 hundred-two\n{-# ATP prove n111' #-}\n{-# ATP prove n111 +-comm n111' #-}\n{-# ATP prove n101' #-}\n{-# ATP prove n101 n111 n101' #-}\n{-# ATP prove n91' #-}\n{-# ATP prove n91 n101 n91' #-}\n{-# ATP prove n102' #-}\n{-# ATP prove n102 n102' +-comm #-}\n\n\npostulate\n 101>100' : GT hundred-one one-hundred\n 101>100 : GT ((one-hundred + eleven) \u2238 ten) one-hundred\n 111>100' : GT hundred-eleven one-hundred\n 111>100 : GT (one-hundred + eleven) one-hundred\n 100<102' : LT one-hundred hundred-two\n 100<102 : LT one-hundred (ninety-one + eleven)\n{-# ATP prove 101>100' #-}\n{-# ATP prove 101>100 101>100' n101 #-}\n{-# ATP prove 111>100' #-}\n{-# ATP prove 111>100 111>100' n111 #-}\n{-# ATP prove 100<102' #-}\n{-# ATP prove 100<102 100<102' n102 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7d9fac76bdbab958ba63ed54cada87a0186888a3","subject":"Added doc.","message":"Added doc.\n\nIgnore-this: 1e957aefcbd418b95c65022fab6e539f\n\ndarcs-hash:20110401125940-3bd4e-6fedb07830363ae4a9d8f466812a51604b999298.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/FOTC\/Data\/Nat\/AddTotality.agda","new_file":"Draft\/FOTC\/Data\/Nat\/AddTotality.agda","new_contents":"module Draft.FOTC.Data.Nat.AddTotality where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n\n-- Interactive proof using the induction principle for natural numbers.\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = indN P P0 ih Nm\n where\n P : D \u2192 Set\n P = \u03bb i \u2192 N (i + n)\n\n P0 : P zero\n P0 = subst N (sym (+-0x n)) Nn\n\n ih : \u2200 {i} \u2192 N i \u2192 P i \u2192 P (succ i)\n ih {i} Ni Pi = subst N (sym (+-Sx i n)) (sN Pi)\n\n-- Combined proof using an instance of the induction principle.\nindN-instance : (x : D) \u2192\n N (zero + x) \u2192\n (\u2200 {n} \u2192 N n \u2192 N (n + x) \u2192 N (succ n + x)) \u2192\n \u2200 {n} \u2192 N n \u2192 N (n + x)\nindN-instance x = indN (\u03bb i \u2192 N (i + x))\n\npostulate\n +-N\u2081 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n{-# ATP prove +-N\u2081 indN-instance #-}\n\n-- Combined proof using the induction principle.\n\n-- The translation is\n-- \u2200 P. app\u2081(P,zero) \u2192\n-- (\u2200 x. app1(N,x) \u2192 app\u2081(P,x) \u2192 app\u2081(P,succ x)) \u2192 -- indN\n-- (\u2200 x. app\u2081(N,x) \u2192 app\u2081(P,x))\n------------------------------------------------------------------\n-- \u2200 x y. app\u2081(N,x) \u2192 app\u2081(N,y) \u2192 app\u2081(N, x + y) -- +-N\u2082\n\npostulate\n +-N\u2082 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n-- The ATPs could not prove this postulate.\n{-# ATP prove +-N\u2082 indN #-}\n","old_contents":"module Draft.FOTC.Data.Nat.AddTotality where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n\n-- Interactive proof using the induction principle for natural numbers.\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = indN P P0 ih Nm\n where\n P : D \u2192 Set\n P = \u03bb i \u2192 N (i + n)\n\n P0 : P zero\n P0 = subst N (sym (+-0x n)) Nn\n\n ih : \u2200 {i} \u2192 N i \u2192 P i \u2192 P (succ i)\n ih {i} Ni Pi = subst N (sym (+-Sx i n)) (sN Pi)\n\n-- Combined proof using an instance of the induction principle.\nindN-instance : (x : D) \u2192\n N (zero + x) \u2192\n (\u2200 {n} \u2192 N n \u2192 N (n + x) \u2192 N (succ n + x)) \u2192\n \u2200 {n} \u2192 N n \u2192 N (n + x)\nindN-instance x = indN (\u03bb i \u2192 N (i + x))\n\npostulate\n +-N\u2081 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n{-# ATP prove +-N\u2081 indN-instance #-}\n\n-- Combined proof using the induction principle.\npostulate\n +-N\u2082 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n-- The ATPs could not prove this postulate.\n{-# ATP prove +-N\u2082 indN #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"77ca7303f67051396173f312a7b72369abb053c0","subject":"Added x\u00abSy\u2192x\u00aby.","message":"Added x\u00abSy\u2192x\u00aby.\n\nIgnore-this: 3603ab0adb279ea9e60ffaf28464b82c\n\ndarcs-hash:20110216152234-3bd4e-c26e56e210fb50398beb7c90652dc1f185a328f3.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/McCarthy91\/RelationATP.agda","new_file":"Draft\/McCarthy91\/RelationATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Properties for some relations\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.RelationATP where\n\nopen import LTC.Base\n\nopen import Draft.McCarthy91.McCarthy91\nopen import Draft.McCarthy91.ArithmeticATP\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\n\n------------------------------------------------------------------------------\n\n0\u00abx\u2192\u22a5 : \u2200 {n} \u2192 N n \u2192 \u00ac (MCR zero n)\n0\u00abx\u2192\u22a5 zN 0\u00abn = prf\n where\n postulate prf : \u22a5\n {-# ATP prove prf #-}\n\n0\u00abx\u2192\u22a5 (sN Nn) 0\u00abSn = prf\n where\n postulate prf : \u22a5\n {-# ATP prove prf \u2238-N x\u2238y>>\u1d56_\n infixr 3 _***\u1d56_\n field\n id\u1d56 : Power\n _>>>\u1d56_ _***\u1d56_ : Power \u2192 Power \u2192 Power\n\nconstPowerOps : PowerOps \u22a4\nconstPowerOps = _\n\nrecord PoweredFlatFuns (Power : Set) {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n `Bits : \u2115 \u2192 T\n `Bit : T\n _`\u00d7_ : T \u2192 T \u2192 T\n \u27e8_\u27e9_\u219d_ : Power \u2192 T \u2192 T \u2192 Set\n infixr 2 _`\u00d7_\n infix 0 \u27e8_\u27e9_\u219d_\n\n\u22a4-poweredFlatFuns : \u2200 {t} {T : Set t} \u2192 FlatFuns T \u2192 PoweredFlatFuns \u22a4 T\n\u22a4-poweredFlatFuns ff = mk `Bits `Bit _`\u00d7_ (\u03bb _ A B \u2192 A `\u2192 B) where open FlatFuns ff\n\nrecord PoweredFlatFunsOps {P t} {T : Set t} (powerOps : PowerOps P) (\u266dFuns : PoweredFlatFuns P T) : Set t where\n constructor mk\n open PoweredFlatFuns \u266dFuns\n open PowerOps powerOps\n field\n idO : \u2200 {A p} \u2192 \u27e8 p \u27e9 A \u219d A\n isIComposable : IComposable {I = \u22a4} _>>>\u1d56_ \u27e8_\u27e9_\u219d_\n isVIComposable : VIComposable {I = \u22a4} _***\u1d56_ _`\u00d7_ \u27e8_\u27e9_\u219d_\n open PoweredFlatFuns \u266dFuns public\n open PowerOps powerOps public\n open IComposable isIComposable public\n open VIComposable isVIComposable public\n\n\u22a4-poweredFlatFunsOps : \u2200 {t} {T : Set t} {\u266dFuns : FlatFuns T}\n \u2192 FlatFunsOps \u266dFuns \u2192 PoweredFlatFunsOps _ (\u22a4-poweredFlatFuns \u266dFuns)\n\u22a4-poweredFlatFunsOps ops = mk idO isComposable isVComposable where open FlatFunsOps ops\n\npoweredFlatFuns : \u2200 {t} {T : Set t} {P}\n \u2192 FlatFuns T \u2192 PoweredFlatFuns P T\npoweredFlatFuns ff = mk `Bits `Bit _`\u00d7_ (\u03bb p A B \u2192 Ann p (A `\u2192 B)) where open FlatFuns ff\n\npoweredFlatFunsOps : \u2200 {t} {T : Set t} {P} (funPowerOps : PowerOps P)\n {\u266dFuns : FlatFuns T}\n \u2192 FlatFunsOps \u266dFuns \u2192 PoweredFlatFunsOps funPowerOps (poweredFlatFuns \u266dFuns)\npoweredFlatFunsOps funPowerOps {\u266dFuns} (mk idO (mk _>>>_) (mk _***_) _)\n = mk (mk idO) (mk (\u03bb x y \u2192 mk (get x >>> get y))) (mk (\u03bb x y \u2192 mk (get x *** get y)))\n where open Ann\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"b2aaa4d843f2cd1f54bf4cd24fadc6827fbbb5b4","subject":"agda: prove most of diff-apply (see #33)","message":"agda: prove most of diff-apply (see #33)\n\nIssue #33 discusses the remaining holes.\n\nOld-commit-hash: a9aeee0162bb9af15ba0d4a4c8b85e53211bbb36\n","repos":"inc-lc\/ilc-agda","old_file":"total.agda","new_file":"total.agda","new_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Note that \u0394 is *not* the same as the \u2202 operator in\n-- definition\/intro.tex. See discussion at:\n--\n-- https:\/\/github.com\/ps-mr\/ilc\/pull\/34#discussion_r4290325\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nopen import Data.Product\nopen import Data.Unit\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Changes\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\n\nopen import Changes\nopen import ChangeContexts\n\n-- DEFINITION of valid changes via a logical relation\n\n{-\nWhat I wanted to write:\n\ndata Valid\u0394 : {T : Type} \u2192 (v : \u27e6 T \u27e7) \u2192 (dv : \u27e6 \u0394-Type T \u27e7) \u2192 Set where\n base : (v : \u27e6 bool \u27e7) \u2192 (dv : \u27e6 \u0394-Type bool \u27e7) \u2192 Valid\u0394 v dv\n fun : \u2200 {S T} \u2192 (f : \u27e6 S \u21d2 T \u27e7) \u2192 (df : \u27e6 \u0394-Type (S \u21d2 T) \u27e7) \u2192\n (\u2200 (s : \u27e6 S \u27e7) ds (valid : Valid\u0394 s ds) \u2192 (Valid\u0394 (f s) (df s ds)) \u00d7 ((apply df f) (apply ds s) \u2261 apply (df s ds) (f s))) \u2192 \n Valid\u0394 f df\n-}\n-- What I had to write:\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {bool} v dv = \u22a4\nvalid-\u0394 {S \u21d2 T} f df =\n \u2200 (s : \u27e6 S \u27e7) ds (valid-w : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7\n (apply df f) (apply ds s) \u2261 apply (df s ds) (f s)\n\nderive-is-valid : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} v (derive v)\nderive-is-valid {bool} v = tt\nderive-is-valid {\u03c4\u2081 \u21d2 \u03c4\u2082} v =\n \u03bb s ds valid-\u0394-s-ds \u2192 {!!} , (\n begin\n (apply (derive v) v) (apply ds s)\n \u2261\u27e8 \u2261-cong (\u03bb x \u2192 x (apply ds s)) (apply-derive v) \u27e9\n v (apply ds s)\n \u2261\u27e8 sym (apply-diff (v s) (v (apply ds s))) \u27e9\n apply (derive v s ds) (v s)\n \u220e) where open \u2261-Reasoning\n\n-- This is a postulate elsewhere, but here I want to start developing a proper proof.\n\ndiff-apply-proof : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n (valid-\u0394 v dv) \u2192 diff (apply dv v) v \u2261 dv\n\ndiff-apply-proof {\u03c4\u2081 \u21d2 \u03c4\u2082} df f df-valid = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n begin\n diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (sym (proj\u2082 (df-valid (apply dv v) (derive (apply dv v)) (derive-is-valid (apply dv v))))) \u27e9\n diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff (apply df f x) (f v))\n (apply-derive (apply dv v)) \u27e9\n diff ((apply df f) (apply dv v)) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (proj\u2082 (df-valid v dv {!!})) \u27e9\n diff (apply (df v dv) (f v)) (f v)\n \u2261\u27e8 diff-apply-proof {\u03c4\u2082} (df v dv) (f v) (proj\u2081 (df-valid v dv {!!})) \u27e9\n df v dv\n \u220e)) where open \u2261-Reasoning\n\ndiff-apply-proof {bool} db b _ = xor-cancellative b db\n\n-- LIFTING terms into \u0394-Contexts\n\nlift-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nlift-term {\u0393\u2081} {\u0393\u2082} {{\u0393\u2032}} = weaken (\u227c-trans \u227c-\u0394-Context \u0393\u2032)\n\n-- PROPERTIES of lift-term\n\nlift-term-ignore : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} {\u03c1 : \u27e6 \u0393\u2082 \u27e7} (t : Term \u0393\u2081 \u03c4) \u2192\n \u27e6 lift-term {{\u0393\u2032}} t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1))\nlift-term-ignore {{\u0393\u2032}} {\u03c1} t = let \u0393\u2033 = \u227c-trans \u227c-\u0394-Context \u0393\u2032 in\n begin\n \u27e6 lift-term {{\u0393\u2032}} t \u27e7 \u03c1\n \u2261\u27e8\u27e9\n \u27e6 weaken \u0393\u2033 t \u27e7 \u03c1\n \u2261\u27e8 weaken-sound t \u03c1 \u27e9\n \u27e6 t \u27e7 (\u27e6 \u227c-trans \u227c-\u0394-Context \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 cong (\u03bb x \u2192 \u27e6 t \u27e7 x) (\u27e6\u27e7-\u227c-trans \u227c-\u0394-Context \u0393\u2032 \u03c1) \u27e9\n \u27e6 t \u27e7Term (\u27e6 \u227c-\u0394-Context \u27e7\u227c (\u27e6 \u0393\u2032 \u27e7\u227c \u03c1))\n \u2261\u27e8\u27e9\n \u27e6 t \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1))\n \u220e where open \u2261-Reasoning\n\n-- PROPERTIES of \u0394\n\n\u0394-abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} (t : Term (\u03c4\u2081 \u2022 \u0393\u2081) \u03c4\u2082) \u2192\n let \u0393\u2033 = keep \u0394-Type \u03c4\u2081 \u2022 keep \u03c4\u2081 \u2022 \u0393\u2032 in\n \u0394 {{\u0393\u2032}} (abs t) \u2248 abs (abs (\u0394 {\u03c4\u2081 \u2022 \u0393\u2081} t))\n\u0394-abs t = ext-t (\u03bb \u03c1 \u2192 refl)\n\n\u0394-app : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4\u2081 \u03c4\u2082} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} (t\u2081 : Term \u0393\u2081 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393\u2081 \u03c4\u2081) \u2192\n \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082) \u2248 app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n\u0394-app {{\u0393\u2032}} t\u2081 t\u2082 = \u2248-sym (ext-t (\u03bb \u03c1\u2032 \u2192 let \u03c1 = \u27e6 \u0393\u2032 \u27e7 \u03c1\u2032 in\n begin\n \u27e6 app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082) \u27e7 \u03c1\u2032\n \u2261\u27e8\u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 lift-term {{\u0393\u2032}} t\u2082 \u27e7 \u03c1\u2032)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 lift-term {{\u0393\u2032}} t\u2082 \u27e7 \u03c1\u2032))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))) x))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) x))\n (lift-term-ignore {{\u0393\u2032}} t\u2082) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff (\u27e6 t\u2081 \u27e7 (update \u03c1) x) (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (apply-diff (\u27e6 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 t\u2082 \u27e7 (update \u03c1))) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8\u27e9\n \u27e6 \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082) \u27e7 \u03c1\u2032\n \u220e)) where open \u2261-Reasoning\n\n-- SYMBOLIC DERIVATION\n\nderive-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-var {\u03c4 \u2022 \u0393} this = this\nderive-var {\u03c4 \u2022 \u0393} (that x) = that (that (derive-var x))\n\nderive-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 (\u0394-Type \u03c4)\nderive-term {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) = abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t))\n where \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term {{\u0393\u2032}} (app t\u2081 t\u2082) = app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\nderive-term {{\u0393\u2032}} (var x) = var (lift \u0393\u2032 (derive-var x))\nderive-term {{\u0393\u2032}} true = false\nderive-term {{\u0393\u2032}} false = false\nderive-term {{\u0393\u2032}} (if c t e) =\n if ((derive-term {{\u0393\u2032}} c) and (lift-term {{\u0393\u2032}} c))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} e) (lift-term {{\u0393\u2032}} e)) (lift-term {{\u0393\u2032}} t))\n (if ((derive-term {{\u0393\u2032}} c) and (lift-term {{\u0393\u2032}} (! c)))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} t) (lift-term {{\u0393\u2032}} t)) (lift-term {{\u0393\u2032}} e))\n (if (lift-term {{\u0393\u2032}} c)\n (derive-term {{\u0393\u2032}} t)\n (derive-term {{\u0393\u2032}} e)))\nderive-term {{\u0393\u2032}} (\u0394 {{\u0393\u2033}} t) = \u0394 {{\u0393\u2032}} (derive-term {{\u0393\u2033}} t)\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 (t : Term \u0393\u2081 \u03c4) \u2192\n \u0394 {{\u0393\u2032}} t \u2248 derive-term {{\u0393\u2032}} t\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 {\u03c4 \u2022 \u0393\u2081} t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct {\u03c4 \u2022 \u0393\u2081} t)) \u27e9\n abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} t))\n \u2261\u27e8\u27e9\n derive-term (abs t)\n \u220e where\n open \u2248-Reasoning\n \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct {{\u0393\u2032}} t\u2081) \u2248-refl) (derive-term-correct {{\u0393\u2032}} t\u2082) \u27e9\n app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (var x) = ext-t (\u03bb \u03c1 \u2192\n begin\n \u27e6 \u0394 {{\u0393\u2032}} (var x) \u27e7 \u03c1\n \u2261\u27e8\u27e9\n diff\n (\u27e6 x \u27e7 (update (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n (\u27e6 x \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n \u2261\u27e8 derive-var-correct {\u0393\u2081} (\u27e6 \u0393\u2032 \u27e7 \u03c1) x \u27e9\n \u27e6 derive-var x \u27e7Var (\u27e6 \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 sym (lift-sound \u0393\u2032 (derive-var x) \u03c1) \u27e9\n \u27e6 lift \u0393\u2032 (derive-var x) \u27e7Var \u03c1\n \u220e) where open \u2261-Reasoning\nderive-term-correct true = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\n","old_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Note that \u0394 is *not* the same as the \u2202 operator in\n-- definition\/intro.tex. See discussion at:\n--\n-- https:\/\/github.com\/ps-mr\/ilc\/pull\/34#discussion_r4290325\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nopen import Data.Product\nopen import Data.Unit\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Changes\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\n\nopen import Changes\nopen import ChangeContexts\n\n-- DEFINITION of valid changes via a logical relation\n\n{-\nWhat I wanted to write:\n\ndata Valid\u0394 : {T : Type} \u2192 (v : \u27e6 T \u27e7) \u2192 (dv : \u27e6 \u0394-Type T \u27e7) \u2192 Set where\n base : (v : \u27e6 bool \u27e7) \u2192 (dv : \u27e6 \u0394-Type bool \u27e7) \u2192 Valid\u0394 v dv\n fun : \u2200 {S T} \u2192 (f : \u27e6 S \u21d2 T \u27e7) \u2192 (df : \u27e6 \u0394-Type (S \u21d2 T) \u27e7) \u2192\n (\u2200 (s : \u27e6 S \u27e7) ds (valid : Valid\u0394 s ds) \u2192 (Valid\u0394 (f s) (df s ds)) \u00d7 ((apply df f) (apply ds s) \u2261 apply (df s ds) (f s))) \u2192 \n Valid\u0394 f df\n-}\n-- What I had to write:\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {bool} v dv = \u22a4\nvalid-\u0394 {S \u21d2 T} f df =\n \u2200 (s : \u27e6 S \u27e7) ds (valid-w : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7\n (apply df f) (apply ds s) \u2261 apply (df s ds) (f s)\n\n-- LIFTING terms into \u0394-Contexts\n\nlift-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nlift-term {\u0393\u2081} {\u0393\u2082} {{\u0393\u2032}} = weaken (\u227c-trans \u227c-\u0394-Context \u0393\u2032)\n\n-- PROPERTIES of lift-term\n\nlift-term-ignore : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} {\u03c1 : \u27e6 \u0393\u2082 \u27e7} (t : Term \u0393\u2081 \u03c4) \u2192\n \u27e6 lift-term {{\u0393\u2032}} t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1))\nlift-term-ignore {{\u0393\u2032}} {\u03c1} t = let \u0393\u2033 = \u227c-trans \u227c-\u0394-Context \u0393\u2032 in\n begin\n \u27e6 lift-term {{\u0393\u2032}} t \u27e7 \u03c1\n \u2261\u27e8\u27e9\n \u27e6 weaken \u0393\u2033 t \u27e7 \u03c1\n \u2261\u27e8 weaken-sound t \u03c1 \u27e9\n \u27e6 t \u27e7 (\u27e6 \u227c-trans \u227c-\u0394-Context \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 cong (\u03bb x \u2192 \u27e6 t \u27e7 x) (\u27e6\u27e7-\u227c-trans \u227c-\u0394-Context \u0393\u2032 \u03c1) \u27e9\n \u27e6 t \u27e7Term (\u27e6 \u227c-\u0394-Context \u27e7\u227c (\u27e6 \u0393\u2032 \u27e7\u227c \u03c1))\n \u2261\u27e8\u27e9\n \u27e6 t \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1))\n \u220e where open \u2261-Reasoning\n\n-- PROPERTIES of \u0394\n\n\u0394-abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} (t : Term (\u03c4\u2081 \u2022 \u0393\u2081) \u03c4\u2082) \u2192\n let \u0393\u2033 = keep \u0394-Type \u03c4\u2081 \u2022 keep \u03c4\u2081 \u2022 \u0393\u2032 in\n \u0394 {{\u0393\u2032}} (abs t) \u2248 abs (abs (\u0394 {\u03c4\u2081 \u2022 \u0393\u2081} t))\n\u0394-abs t = ext-t (\u03bb \u03c1 \u2192 refl)\n\n\u0394-app : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4\u2081 \u03c4\u2082} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} (t\u2081 : Term \u0393\u2081 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393\u2081 \u03c4\u2081) \u2192\n \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082) \u2248 app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n\u0394-app {{\u0393\u2032}} t\u2081 t\u2082 = \u2248-sym (ext-t (\u03bb \u03c1\u2032 \u2192 let \u03c1 = \u27e6 \u0393\u2032 \u27e7 \u03c1\u2032 in\n begin\n \u27e6 app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082) \u27e7 \u03c1\u2032\n \u2261\u27e8\u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 lift-term {{\u0393\u2032}} t\u2082 \u27e7 \u03c1\u2032)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 lift-term {{\u0393\u2032}} t\u2082 \u27e7 \u03c1\u2032))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))) x))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) x))\n (lift-term-ignore {{\u0393\u2032}} t\u2082) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff (\u27e6 t\u2081 \u27e7 (update \u03c1) x) (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (apply-diff (\u27e6 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 t\u2082 \u27e7 (update \u03c1))) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8\u27e9\n \u27e6 \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082) \u27e7 \u03c1\u2032\n \u220e)) where open \u2261-Reasoning\n\n-- SYMBOLIC DERIVATION\n\nderive-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-var {\u03c4 \u2022 \u0393} this = this\nderive-var {\u03c4 \u2022 \u0393} (that x) = that (that (derive-var x))\n\nderive-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 (\u0394-Type \u03c4)\nderive-term {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) = abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t))\n where \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term {{\u0393\u2032}} (app t\u2081 t\u2082) = app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\nderive-term {{\u0393\u2032}} (var x) = var (lift \u0393\u2032 (derive-var x))\nderive-term {{\u0393\u2032}} true = false\nderive-term {{\u0393\u2032}} false = false\nderive-term {{\u0393\u2032}} (if c t e) =\n if ((derive-term {{\u0393\u2032}} c) and (lift-term {{\u0393\u2032}} c))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} e) (lift-term {{\u0393\u2032}} e)) (lift-term {{\u0393\u2032}} t))\n (if ((derive-term {{\u0393\u2032}} c) and (lift-term {{\u0393\u2032}} (! c)))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} t) (lift-term {{\u0393\u2032}} t)) (lift-term {{\u0393\u2032}} e))\n (if (lift-term {{\u0393\u2032}} c)\n (derive-term {{\u0393\u2032}} t)\n (derive-term {{\u0393\u2032}} e)))\nderive-term {{\u0393\u2032}} (\u0394 {{\u0393\u2033}} t) = \u0394 {{\u0393\u2032}} (derive-term {{\u0393\u2033}} t)\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 (t : Term \u0393\u2081 \u03c4) \u2192\n \u0394 {{\u0393\u2032}} t \u2248 derive-term {{\u0393\u2032}} t\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 {\u03c4 \u2022 \u0393\u2081} t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct {\u03c4 \u2022 \u0393\u2081} t)) \u27e9\n abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} t))\n \u2261\u27e8\u27e9\n derive-term (abs t)\n \u220e where\n open \u2248-Reasoning\n \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct {{\u0393\u2032}} t\u2081) \u2248-refl) (derive-term-correct {{\u0393\u2032}} t\u2082) \u27e9\n app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (var x) = ext-t (\u03bb \u03c1 \u2192\n begin\n \u27e6 \u0394 {{\u0393\u2032}} (var x) \u27e7 \u03c1\n \u2261\u27e8\u27e9\n diff\n (\u27e6 x \u27e7 (update (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n (\u27e6 x \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n \u2261\u27e8 derive-var-correct {\u0393\u2081} (\u27e6 \u0393\u2032 \u27e7 \u03c1) x \u27e9\n \u27e6 derive-var x \u27e7Var (\u27e6 \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 sym (lift-sound \u0393\u2032 (derive-var x) \u03c1) \u27e9\n \u27e6 lift \u0393\u2032 (derive-var x) \u27e7Var \u03c1\n \u220e) where open \u2261-Reasoning\nderive-term-correct true = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"21ea7cbcf3428ce9d579b019c1be987a27c706e9","subject":"Step-indexed evaluation: simplify back","message":"Step-indexed evaluation: simplify back\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/FunBigStepSILR2.agda","new_file":"Thesis\/FunBigStepSILR2.agda","new_contents":"{-# OPTIONS --allow-unsolved-metas #-}\n-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\", POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n-- Since we focus for now on STLC, unlike that\n-- paper, we can avoid error values by keeping types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) zero = TimeOut\nevalConst (lit v) (suc n) = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a (suc n) = eval t (a \u2022 \u03c1) n\napply (closure t \u03c1) a zero = TimeOut\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\n-- eval (app s t) \u03c1 = eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)\neval (app s t) \u03c1 n0 with eval s \u03c1 n0\n... | Error = Error\n... | TimeOut = TimeOut\n... | Done sv n1 with eval t \u03c1 n1\n... | Done tv n2 = apply sv tv n2\n... | Error = Error\n... | TimeOut = TimeOut\n","old_contents":"{-# OPTIONS --allow-unsolved-metas #-}\n-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\", POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n-- Since we focus for now on STLC, unlike that\n-- paper, we can avoid error values by keeping types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) (n0 : \u2115) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 (n\u2264 : n1 \u2264 n0) \u2192 ErrVal \u03c4 n0\n Error : ErrVal \u03c4 n0\n TimeOut : ErrVal \u03c4 n0\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4 n\n\nup : \u2200 {\u03c4 n0 n1} \u2192 ErrVal \u03c4 n1 \u2192 (n1 \u2264 n0) \u2192 ErrVal \u03c4 n0\nup (Done v n2 n2\u2264n1) n1\u2264n0 = Done v n2 (\u2264-trans n2\u2264n1 n1\u2264n0)\nup Error n\u2264 = Error\nup TimeOut n\u2264 = TimeOut\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 n\u2264 = up (t v n1) n\u2264\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) zero = TimeOut\nevalConst (lit v) (suc n) = Done (intV v) n (\u2264-step \u2264-refl)\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a (suc n) = up (eval t (a \u2022 \u03c1) n) (\u2264-step \u2264-refl)\napply (closure t \u03c1) a zero = TimeOut\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n \u2264-refl\neval (abs t) \u03c1 n = Done (closure t \u03c1) n \u2264-refl\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\n-- eval (app s t) \u03c1 = eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)\neval (app s t) \u03c1 n0 with eval s \u03c1 n0\n... | Error = Error\n... | TimeOut = TimeOut\n... | Done sv n1 n1\u2264n0 with eval t \u03c1 n1\n... | Done tv n2 n2\u2264n1 = up (apply sv tv n2) (\u2264-trans n2\u2264n1 n1\u2264n0)\n... | Error = Error\n... | TimeOut = TimeOut\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"4886cd0de64e23f77fd7608edbc35ba22f9ea2f2","subject":"tried to analyze a slightly larger example, but it is too big already","message":"tried to analyze a slightly larger example, but it is too big already\n\nOld-commit-hash: 3e1af00454de058acf5ca90a5d63198a5b3aca11\n","repos":"inc-lc\/ilc-agda","old_file":"Examples\/Examples1.agda","new_file":"Examples\/Examples1.agda","new_contents":"module Examples.Examples1 where\n\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Changes\nopen import Syntactic.Closures\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\nopen import Denotational.ValidChanges\n\nopen import Changes\nopen import ChangeContexts\nopen import ChangeContextLifting\nopen import PropsDelta\nopen import SymbolicDerivation\nopen import Natural.Evaluation\nopen import Relation.Binary.PropositionalEquality\n\n-- only some finger exercises to get used to the definitions\n \nbool-identity : Term \u2205 (bool \u21d2 bool)\nbool-identity = abs (var this)\n\nterm1 : Term \u2205 bool\nterm1 = app bool-identity true\n\nres : \u27e6 bool \u27e7\nres = \u27e6 term1 \u27e7 \u2205\n\n-- test the denotational semantics\n\n-- is this a good way to write unit tests in Agda?\ntest1 : res \u2261 true\ntest1 = refl\n\n-- test the natural semantics\nopen import Natural.Lookup\ntest2 : \u2205 \u22a2 term1 \u2193 vtrue\ntest2 = app abs e-true (var this)\n\nterm2 : Term (bool \u2022 bool \u2022 bool \u2022 \u2205) bool\nterm2 = if (var this) (if (var (that this)) false true) (if (var (that (that this))) false true)\n\nterm3 : Term \u2205 (bool \u21d2 bool \u21d2 bool \u21d2 bool)\nterm3 = abs (abs (abs term2))\n\nopen import SymbolicDerivation\n-- presumably there is some shorter way to do this?\n-- I don't understand instance arguments sufficiently\nterm4 = derive-term {\u2205} {\u2205} {bool \u21d2 bool \u21d2 bool \u21d2 bool} {{\u2205 }} term3\n\n-- it turns out that term4 is already way too complicated to be analyzed by hand\n","old_contents":"module Examples.Examples1 where\n\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Changes\nopen import Syntactic.Closures\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\nopen import Denotational.ValidChanges\n\nopen import Changes\nopen import ChangeContexts\nopen import ChangeContextLifting\nopen import PropsDelta\nopen import SymbolicDerivation\nopen import Natural.Evaluation\nopen import Relation.Binary.PropositionalEquality\n\n-- only some finger exercises to get used to the definitions\n \nbool-identity : Term \u2205 (bool \u21d2 bool)\nbool-identity = abs (var this)\n\nterm1 : Term \u2205 bool\nterm1 = app bool-identity true\n\nres : \u27e6 bool \u27e7\nres = \u27e6 term1 \u27e7 \u2205\n\n-- test the denotational semantics\n\n-- is this a good way to write unit tests in Agda?\ntest1 : res \u2261 true\ntest1 = refl\n\n-- test the natural semantics\nopen import Natural.Lookup\ntest2 : \u2205 \u22a2 term1 \u2193 vtrue\ntest2 = app abs e-true (var this)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"bd4721cbfeecd02bd82f2eb2bb19570e836dac3a","subject":"Change argument order of carry-over.","message":"Change argument order of carry-over.\n\nThis commit prepares for uncurrying cary-over.\n\nOld-commit-hash: e1a21bdcbe7ee2b217917bd6ac9dfc576487102a\n","repos":"inc-lc\/ilc-agda","old_file":"Denotation\/Implementation\/Popl14.agda","new_file":"Denotation\/Implementation\/Popl14.agda","new_contents":"module Denotation.Implementation.Popl14 where\n\n-- Notions of programs being implementations of specifications\n-- for Calculus Popl14\n\nopen import Denotation.Specification.Canon-Popl14 public\n\nopen import Popl14.Syntax.Type\nopen import Popl14.Syntax.Term\nopen import Popl14.Denotation.Value\nopen import Popl14.Change.Derive\nopen import Popl14.Change.Value\nopen import Popl14.Change.Validity\n\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Unit\nopen import Data.Product\nopen import Data.Integer\nopen import Structure.Tuples\nopen import Structure.Bag.Popl14\nopen import Postulate.Extensionality\n\ninfix 4 implements\nsyntax implements \u03c4 u v = u \u2248\u208d \u03c4 \u208e v\nimplements : \u2200 (\u03c4 : Type) \u2192 Change \u03c4 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 Set\n\nu \u2248\u208d int \u208e v = u \u2261 v\nu \u2248\u208d bag \u208e v = u \u2261 v\nu \u2248\u208d \u03c3 \u21d2 \u03c4 \u208e v =\n (w : \u27e6 \u03c3 \u27e7) (\u0394w : Change \u03c3) (R[w,\u0394w] : valid {\u03c3} w \u0394w)\n (\u0394w\u2032 : \u27e6 \u0394Type \u03c3 \u27e7) (\u0394w\u2248\u0394w\u2032 : implements \u03c3 \u0394w \u0394w\u2032) \u2192\n implements \u03c4 (u (cons w \u0394w R[w,\u0394w])) (v w \u0394w\u2032)\n\ninfix 4 _\u2248_\n_\u2248_ : \u2200 {\u03c4} \u2192 Change \u03c4 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 Set\n_\u2248_ {\u03c4} = implements \u03c4\n\nmodule Disambiguation (\u03c4 : Type) where\n _\u271a_ : \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n _\u271a_ = _\u27e6\u2295\u27e7_ {\u03c4}\n _\u2212_ : \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7\n _\u2212_ = _\u27e6\u229d\u27e7_ {\u03c4}\n infixl 6 _\u271a_ _\u2212_\n _\u2243_ : Change \u03c4 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 Set\n _\u2243_ = _\u2248_ {\u03c4}\n infix 4 _\u2243_\n\nmodule FunctionDisambiguation (\u03c3 : Type) (\u03c4 : Type) where\n open Disambiguation (\u03c3 \u21d2 \u03c4) public\n _\u271a\u2081_ : \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n _\u271a\u2081_ = _\u27e6\u2295\u27e7_ {\u03c4}\n _\u2212\u2081_ : \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7\n _\u2212\u2081_ = _\u27e6\u229d\u27e7_ {\u03c4}\n infixl 6 _\u271a\u2081_ _\u2212\u2081_\n _\u2243\u2081_ : Change \u03c4 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 Set\n _\u2243\u2081_ = _\u2248_ {\u03c4}\n infix 4 _\u2243\u2081_\n _\u271a\u2080_ : \u27e6 \u03c3 \u27e7 \u2192 \u27e6 \u0394Type \u03c3 \u27e7 \u2192 \u27e6 \u03c3 \u27e7\n _\u271a\u2080_ = _\u27e6\u2295\u27e7_ {\u03c3}\n _\u2212\u2080_ : \u27e6 \u03c3 \u27e7 \u2192 \u27e6 \u03c3 \u27e7 \u2192 \u27e6 \u0394Type \u03c3 \u27e7\n _\u2212\u2080_ = _\u27e6\u229d\u27e7_ {\u03c3}\n infixl 6 _\u271a\u2080_ _\u2212\u2080_\n _\u2243\u2080_ : Change \u03c3 \u2192 \u27e6 \u0394Type \u03c3 \u27e7 \u2192 Set\n _\u2243\u2080_ = _\u2248_ {\u03c3}\n infix 4 _\u2243\u2080_\n\ncompatible : \u2200 {\u0393} \u2192 \u0394Env \u0393 \u2192 \u27e6 \u0394Context \u0393 \u27e7 \u2192 Set\ncompatible {\u2205} \u2205 \u2205 = \u22a4\ncompatible {\u03c4 \u2022 \u0393} (cons v \u0394v _ \u2022 \u03c1) (\u0394v\u2032 \u2022 v\u2032 \u2022 \u03c1\u2032) =\n Triple (v \u2261 v\u2032) (\u03bb _ \u2192 \u0394v \u2248\u208d \u03c4 \u208e \u0394v\u2032) (\u03bb _ _ \u2192 compatible \u03c1 \u03c1\u2032)\n\n-- If a program implements a specification, then certain things\n-- proven about the specification carry over to the programs.\ncarry-over : \u2200 {\u03c4}\n {v : \u27e6 \u03c4 \u27e7} {\u0394v : Change \u03c4} (R[v,\u0394v] : valid {\u03c4} v \u0394v)\n {\u0394v\u2032 : \u27e6 \u0394Type \u03c4 \u27e7} (\u0394v\u2248\u0394v\u2032 : \u0394v \u2248\u208d \u03c4 \u208e \u0394v\u2032) \u2192\n let open Disambiguation \u03c4 in\n v \u229e\u208d \u03c4 \u208e \u0394v \u2261 v \u271a \u0394v\u2032\n\nu\u229fv\u2248u\u229dv : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192\n let open Disambiguation \u03c4 in\n u \u229f\u208d \u03c4 \u208e v \u2243 u \u2212 v\nu\u229fv\u2248u\u229dv {base base-int} = refl\nu\u229fv\u2248u\u229dv {base base-bag} = refl\nu\u229fv\u2248u\u229dv {\u03c3 \u21d2 \u03c4} {g} {f} = result where\n open FunctionDisambiguation \u03c3 \u03c4\n result : (w : \u27e6 \u03c3 \u27e7) (\u0394w : Change \u03c3) \u2192 valid {\u03c3} w \u0394w \u2192\n (\u0394w\u2032 : \u27e6 \u0394Type \u03c3 \u27e7) \u2192 \u0394w \u2248\u208d \u03c3 \u208e \u0394w\u2032 \u2192\n g (w \u229e\u208d \u03c3 \u208e \u0394w) \u229f\u208d \u03c4 \u208e f w \u2243\u2081 g (w \u271a\u2080 \u0394w\u2032) \u2212\u2081 f w\n result w \u0394w R[w,\u0394w] \u0394w\u2032 \u0394w\u2248\u0394w\u2032\n rewrite carry-over {\u03c3} {w} R[w,\u0394w] \u0394w\u2248\u0394w\u2032 =\n u\u229fv\u2248u\u229dv {\u03c4} {g (w \u271a\u2080 \u0394w\u2032)} {f w}\ncarry-over {base base-int} {v} _ \u0394v\u2248\u0394v\u2032 = cong (_+_ v) \u0394v\u2248\u0394v\u2032\ncarry-over {base base-bag} {v} _ \u0394v\u2248\u0394v\u2032 = cong (_++_ v) \u0394v\u2248\u0394v\u2032\ncarry-over {\u03c3 \u21d2 \u03c4} {f} {\u0394f} R[f,\u0394f] {\u0394f\u2032} \u0394f\u2248\u0394f\u2032 =\n ext (\u03bb v \u2192\n let\n open FunctionDisambiguation \u03c3 \u03c4\n V = R[v,u-v] {\u03c3} {v} {v}\n S = u\u229fv\u2248u\u229dv {\u03c3} {v} {v}\n in\n carry-over {\u03c4} {f v}\n {\u0394f (nil-valid-change \u03c3 v)} (proj\u2081 (R[f,\u0394f] (nil-valid-change \u03c3 v)))\n {\u0394f\u2032 v (v \u2212\u2080 v)}\n (\u0394f\u2248\u0394f\u2032 v (v \u229f\u208d \u03c3 \u208e v) V (v \u2212\u2080 v) S))\n\n-- A property relating `ignore` and the subcontext relation \u0393\u227c\u0394\u0393\n\u27e6\u0393\u227c\u0394\u0393\u27e7 : \u2200 {\u0393} {\u03c1 : \u0394Env \u0393} {\u03c1\u2032 : \u27e6 \u0394Context \u0393 \u27e7}\n (C : compatible \u03c1 \u03c1\u2032) \u2192 ignore \u03c1 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1\u2032\n\u27e6\u0393\u227c\u0394\u0393\u27e7 {\u2205} {\u2205} {\u2205} _ = refl\n\u27e6\u0393\u227c\u0394\u0393\u27e7 {\u03c4 \u2022 \u0393} {cons v dv _ \u2022 \u03c1} {dv\u2032 \u2022 v\u2032 \u2022 \u03c1\u2032}\n (cons v\u2261v\u2032 _ C) = cong\u2082 _\u2022_ v\u2261v\u2032 (\u27e6\u0393\u227c\u0394\u0393\u27e7 C)\n\n-- A specialization of the soundness of weakening\n\u27e6fit\u27e7 : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4)\n {\u03c1 : \u0394Env \u0393} {\u03c1\u2032 : \u27e6 \u0394Context \u0393 \u27e7} (C : compatible \u03c1 \u03c1\u2032) \u2192\n \u27e6 t \u27e7 (ignore \u03c1) \u2261 \u27e6 fit t \u27e7 \u03c1\u2032\n\u27e6fit\u27e7 t {\u03c1} {\u03c1\u2032} C =\n trans (cong \u27e6 t \u27e7 (\u27e6\u0393\u227c\u0394\u0393\u27e7 C)) (sym (weaken-sound t \u03c1\u2032))\n","old_contents":"module Denotation.Implementation.Popl14 where\n\n-- Notions of programs being implementations of specifications\n-- for Calculus Popl14\n\nopen import Denotation.Specification.Canon-Popl14 public\n\nopen import Popl14.Syntax.Type\nopen import Popl14.Syntax.Term\nopen import Popl14.Denotation.Value\nopen import Popl14.Change.Derive\nopen import Popl14.Change.Value\nopen import Popl14.Change.Validity\n\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Unit\nopen import Data.Product\nopen import Data.Integer\nopen import Structure.Tuples\nopen import Structure.Bag.Popl14\nopen import Postulate.Extensionality\n\ninfix 4 implements\nsyntax implements \u03c4 u v = u \u2248\u208d \u03c4 \u208e v\nimplements : \u2200 (\u03c4 : Type) \u2192 Change \u03c4 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 Set\n\nu \u2248\u208d int \u208e v = u \u2261 v\nu \u2248\u208d bag \u208e v = u \u2261 v\nu \u2248\u208d \u03c3 \u21d2 \u03c4 \u208e v =\n (w : \u27e6 \u03c3 \u27e7) (\u0394w : Change \u03c3) (R[w,\u0394w] : valid {\u03c3} w \u0394w)\n (\u0394w\u2032 : \u27e6 \u0394Type \u03c3 \u27e7) (\u0394w\u2248\u0394w\u2032 : implements \u03c3 \u0394w \u0394w\u2032) \u2192\n implements \u03c4 (u (cons w \u0394w R[w,\u0394w])) (v w \u0394w\u2032)\n\ninfix 4 _\u2248_\n_\u2248_ : \u2200 {\u03c4} \u2192 Change \u03c4 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 Set\n_\u2248_ {\u03c4} = implements \u03c4\n\nmodule Disambiguation (\u03c4 : Type) where\n _\u271a_ : \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n _\u271a_ = _\u27e6\u2295\u27e7_ {\u03c4}\n _\u2212_ : \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7\n _\u2212_ = _\u27e6\u229d\u27e7_ {\u03c4}\n infixl 6 _\u271a_ _\u2212_\n _\u2243_ : Change \u03c4 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 Set\n _\u2243_ = _\u2248_ {\u03c4}\n infix 4 _\u2243_\n\nmodule FunctionDisambiguation (\u03c3 : Type) (\u03c4 : Type) where\n open Disambiguation (\u03c3 \u21d2 \u03c4) public\n _\u271a\u2081_ : \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n _\u271a\u2081_ = _\u27e6\u2295\u27e7_ {\u03c4}\n _\u2212\u2081_ : \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7\n _\u2212\u2081_ = _\u27e6\u229d\u27e7_ {\u03c4}\n infixl 6 _\u271a\u2081_ _\u2212\u2081_\n _\u2243\u2081_ : Change \u03c4 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 Set\n _\u2243\u2081_ = _\u2248_ {\u03c4}\n infix 4 _\u2243\u2081_\n _\u271a\u2080_ : \u27e6 \u03c3 \u27e7 \u2192 \u27e6 \u0394Type \u03c3 \u27e7 \u2192 \u27e6 \u03c3 \u27e7\n _\u271a\u2080_ = _\u27e6\u2295\u27e7_ {\u03c3}\n _\u2212\u2080_ : \u27e6 \u03c3 \u27e7 \u2192 \u27e6 \u03c3 \u27e7 \u2192 \u27e6 \u0394Type \u03c3 \u27e7\n _\u2212\u2080_ = _\u27e6\u229d\u27e7_ {\u03c3}\n infixl 6 _\u271a\u2080_ _\u2212\u2080_\n _\u2243\u2080_ : Change \u03c3 \u2192 \u27e6 \u0394Type \u03c3 \u27e7 \u2192 Set\n _\u2243\u2080_ = _\u2248_ {\u03c3}\n infix 4 _\u2243\u2080_\n\ncompatible : \u2200 {\u0393} \u2192 \u0394Env \u0393 \u2192 \u27e6 \u0394Context \u0393 \u27e7 \u2192 Set\ncompatible {\u2205} \u2205 \u2205 = \u22a4\ncompatible {\u03c4 \u2022 \u0393} (cons v \u0394v _ \u2022 \u03c1) (\u0394v\u2032 \u2022 v\u2032 \u2022 \u03c1\u2032) =\n Triple (v \u2261 v\u2032) (\u03bb _ \u2192 \u0394v \u2248\u208d \u03c4 \u208e \u0394v\u2032) (\u03bb _ _ \u2192 compatible \u03c1 \u03c1\u2032)\n\n-- If a program implements a specification, then certain things\n-- proven about the specification carry over to the programs.\ncarry-over : \u2200 {\u03c4}\n {v : \u27e6 \u03c4 \u27e7} {\u0394v : Change \u03c4} {\u0394v\u2032 : \u27e6 \u0394Type \u03c4 \u27e7}\n (R[v,\u0394v] : valid {\u03c4} v \u0394v) (\u0394v\u2248\u0394v\u2032 : \u0394v \u2248\u208d \u03c4 \u208e \u0394v\u2032) \u2192\n let open Disambiguation \u03c4 in\n v \u229e\u208d \u03c4 \u208e \u0394v \u2261 v \u271a \u0394v\u2032\n\nu\u229fv\u2248u\u229dv : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192\n let open Disambiguation \u03c4 in\n u \u229f\u208d \u03c4 \u208e v \u2243 u \u2212 v\nu\u229fv\u2248u\u229dv {base base-int} = refl\nu\u229fv\u2248u\u229dv {base base-bag} = refl\nu\u229fv\u2248u\u229dv {\u03c3 \u21d2 \u03c4} {g} {f} = result where\n open FunctionDisambiguation \u03c3 \u03c4\n result : (w : \u27e6 \u03c3 \u27e7) (\u0394w : Change \u03c3) \u2192 valid {\u03c3} w \u0394w \u2192\n (\u0394w\u2032 : \u27e6 \u0394Type \u03c3 \u27e7) \u2192 \u0394w \u2248\u208d \u03c3 \u208e \u0394w\u2032 \u2192\n g (w \u229e\u208d \u03c3 \u208e \u0394w) \u229f\u208d \u03c4 \u208e f w \u2243\u2081 g (w \u271a\u2080 \u0394w\u2032) \u2212\u2081 f w\n result w \u0394w R[w,\u0394w] \u0394w\u2032 \u0394w\u2248\u0394w\u2032\n rewrite carry-over {\u03c3} {w} R[w,\u0394w] \u0394w\u2248\u0394w\u2032 =\n u\u229fv\u2248u\u229dv {\u03c4} {g (w \u271a\u2080 \u0394w\u2032)} {f w}\ncarry-over {base base-int} {v} _ \u0394v\u2248\u0394v\u2032 = cong (_+_ v) \u0394v\u2248\u0394v\u2032\ncarry-over {base base-bag} {v} _ \u0394v\u2248\u0394v\u2032 = cong (_++_ v) \u0394v\u2248\u0394v\u2032\ncarry-over {\u03c3 \u21d2 \u03c4} {f} {\u0394f} {\u0394f\u2032} R[f,\u0394f] \u0394f\u2248\u0394f\u2032 =\n ext (\u03bb v \u2192\n let\n open FunctionDisambiguation \u03c3 \u03c4\n V = R[v,u-v] {\u03c3} {v} {v}\n S = u\u229fv\u2248u\u229dv {\u03c3} {v} {v}\n in\n carry-over {\u03c4} {f v}\n {\u0394f (nil-valid-change \u03c3 v)} {\u0394f\u2032 v (v \u2212\u2080 v)}\n (proj\u2081 (R[f,\u0394f] (nil-valid-change \u03c3 v)))\n (\u0394f\u2248\u0394f\u2032 v (v \u229f\u208d \u03c3 \u208e v) V (v \u2212\u2080 v) S))\n\n-- A property relating `ignore` and the subcontext relation \u0393\u227c\u0394\u0393\n\u27e6\u0393\u227c\u0394\u0393\u27e7 : \u2200 {\u0393} {\u03c1 : \u0394Env \u0393} {\u03c1\u2032 : \u27e6 \u0394Context \u0393 \u27e7}\n (C : compatible \u03c1 \u03c1\u2032) \u2192 ignore \u03c1 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1\u2032\n\u27e6\u0393\u227c\u0394\u0393\u27e7 {\u2205} {\u2205} {\u2205} _ = refl\n\u27e6\u0393\u227c\u0394\u0393\u27e7 {\u03c4 \u2022 \u0393} {cons v dv _ \u2022 \u03c1} {dv\u2032 \u2022 v\u2032 \u2022 \u03c1\u2032}\n (cons v\u2261v\u2032 _ C) = cong\u2082 _\u2022_ v\u2261v\u2032 (\u27e6\u0393\u227c\u0394\u0393\u27e7 C)\n\n-- A specialization of the soundness of weakening\n\u27e6fit\u27e7 : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4)\n {\u03c1 : \u0394Env \u0393} {\u03c1\u2032 : \u27e6 \u0394Context \u0393 \u27e7} (C : compatible \u03c1 \u03c1\u2032) \u2192\n \u27e6 t \u27e7 (ignore \u03c1) \u2261 \u27e6 fit t \u27e7 \u03c1\u2032\n\u27e6fit\u27e7 t {\u03c1} {\u03c1\u2032} C =\n trans (cong \u27e6 t \u27e7 (\u27e6\u0393\u227c\u0394\u0393\u27e7 C)) (sym (weaken-sound t \u03c1\u2032))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9028d1ebd71caa01d4b8a57cbb884b32e787ebe1","subject":"Revert \"agda\/...: misc (to revert)\"","message":"Revert \"agda\/...: misc (to revert)\"\n\nThis reverts commit ae3a9db6fb87ddc01573ae236f8850b322bec8db.\n\nOld-commit-hash: fa2ae9742c4282275b8f242a197b35ef1acf5932\n","repos":"inc-lc\/ilc-agda","old_file":"total.agda","new_file":"total.agda","new_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nimport Relation.Binary as B\n\nopen import Relation.Binary using\n (IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)\nimport Relation.Binary.EqReasoning as EqR\n\nopen import Relation.Nullary using (\u00ac_)\n\nopen import meaning\nopen import IlcModel\nopen import Changes\nopen import ChangeContexts\nopen import binding Type \u27e6_\u27e7Type\nopen import TotalTerms\n\n-- LIFTING terms into \u0394-Contexts\n\nlift-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) \u03c4\nlift-var this = that this\nlift-var (that x) = that (that (lift-var x))\n\nlift-var\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-var\u2032 \u2205 x = lift-var x\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) this = this\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) (that x) = that (lift-var\u2032 \u0393\u2032 x)\n\nlift-term\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-term\u2032 \u0393\u2032 (abs t) = abs (lift-term\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term\u2032 \u0393\u2032 (app t\u2081 t\u2082) = app (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082)\nlift-term\u2032 \u0393\u2032 (var x) = var (lift-var\u2032 \u0393\u2032 x)\nlift-term\u2032 \u0393\u2032 true = true\nlift-term\u2032 \u0393\u2032 false = false\nlift-term\u2032 \u0393\u2032 (if t\u2081 t\u2082 t\u2083) = if (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082) (lift-term\u2032 \u0393\u2032 t\u2083)\nlift-term\u2032 {.(\u0394-Context \u0393)} {.(\u0394-Type \u03c4)} \u0393\u2032 (\u0394 {\u0393} {\u03c4} t) = weakenMore t\n where\n open import Relation.Binary.PropositionalEquality using (sym)\n\n doWeakenMore : \u2200 \u0393prefix \u0393rest {\u03c4} \u2192\n Term (\u0393prefix \u22ce \u0393rest) (\u0394-Type \u03c4) \u2192\n Term (\u0393prefix \u22ce \u0394-Context \u0393rest) (\u0394-Type \u03c4)\n\n doWeakenMore \u0393prefix \u2205 t\u2081 = t\u2081\n doWeakenMore \u2205 (\u03c4\u2082 \u2022 \u0393rest) t\u2081 =\n weakenOne \u2205 (\u0394-Type \u03c4\u2082) (doWeakenMore (\u03c4\u2082 \u2022 \u2205) \u0393rest t\u2081)\n doWeakenMore \u0393prefix (\u03c4\u2082 \u2022 \u0393rest) {\u03c4} t\u2081 =\n weakenOne \u0393prefix (\u0394-Type \u03c4\u2082)\n (substTerm (sym (move-prefix \u0393prefix \u03c4\u2082 (\u0394-Context \u0393rest)))\n (doWeakenMore (\u0393prefix \u22ce (\u03c4\u2082 \u2022 \u2205)) \u0393rest\n (substTerm (move-prefix \u0393prefix \u03c4\u2082 \u0393rest) t\u2081)))\n\n prefix : \u2200 \u0393 \u2192 (\u0393\u2032 : Prefix (\u0394-Context \u0393)) \u2192 Context\n prefix \u0393 \u0393\u2032 = take (\u0394-Context \u0393) \u0393\u2032\n\n rest : \u2200 \u0393 \u2192 (\u0393\u2032 : Prefix (\u0394-Context \u0393)) \u2192 Context\n rest \u0393 \u0393\u2032 = drop (\u0394-Context \u0393) \u0393\u2032\n\n weakenMore2 : \u2200 \u0393 \u0393\u2032 {\u03c4} \u2192\n Term \u0393 \u03c4 \u2192\n Term (prefix \u0393 \u0393\u2032 \u22ce \u0394-Context (rest \u0393 \u0393\u2032)) (\u0394-Type \u03c4)\n weakenMore2 \u0393 \u0393\u2032 t =\n doWeakenMore (prefix \u0393 \u0393\u2032) (rest \u0393 \u0393\u2032) (\n substTerm (sym (take-drop (\u0394-Context \u0393) \u0393\u2032)) (\u0394 t))\n\n weakenMore : --\u2200 {\u0393 \u03c4} \u0393\u2032 \u2192\n Term \u0393 \u03c4 \u2192 Term (\u0394-Context\u2032 (\u0394-Context \u0393) \u0393\u2032) (\u0394-Type \u03c4)\n weakenMore t =\n substTerm\n (sym (take-\u22ce-\u0394-Context-drop-\u0394-Context\u2032 (\u0394-Context \u0393) \u0393\u2032))\n (weakenMore2 \u0393 \u0393\u2032 t)\n\nlift-term\u2032 {._} {_} _ (weakenOne _ _ {_} {._} _) = {!!}\n\nlift-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) \u03c4\nlift-term = lift-term\u2032 \u2205\n\n-- PROPERTIES of lift-term\n\nlift-var-ignore : \u2200 {\u0393 \u03c4} (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore \u03c1)\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) (that x) = lift-var-ignore \u03c1 x\n\nlift-var-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) (\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var\u2032 \u0393\u2032 x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-var-ignore\u2032 \u2205 \u03c1 x = lift-var-ignore \u03c1 x\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) (that x) = lift-var-ignore\u2032 \u0393\u2032 \u03c1 x\n\nlift-term-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) {\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term\u2032 \u0393\u2032 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-term-ignore\u2032 \u0393\u2032 (abs t) =\n ext (\u03bb v \u2192 lift-term-ignore\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term-ignore\u2032 \u0393\u2032 (app t\u2081 t\u2082) =\n \u2261-app (lift-term-ignore\u2032 \u0393\u2032 t\u2081) (lift-term-ignore\u2032 \u0393\u2032 t\u2082)\nlift-term-ignore\u2032 \u0393\u2032 (var x) = lift-var-ignore\u2032 \u0393\u2032 _ x\nlift-term-ignore\u2032 \u0393\u2032 true = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 false = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 {\u03c1} (if t\u2081 t\u2082 t\u2083)\n with \u27e6 lift-term\u2032 \u0393\u2032 t\u2081 \u27e7 \u03c1\n | \u27e6 t\u2081 \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\n | lift-term-ignore\u2032 \u0393\u2032 {\u03c1} t\u2081\n... | true | true | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2082\n... | false | false | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2083\nlift-term-ignore\u2032 \u0393\u2032 (\u0394 t) = {!!}\nlift-term-ignore\u2032 _ (weakenOne _ _ {_} {._} _) = {!!}\n\nlift-term-ignore : \u2200 {\u0393 \u03c4} {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore \u03c1)\nlift-term-ignore = lift-term-ignore\u2032 \u2205\n\n\n-- PROPERTIES of \u0394\n\n\u0394-abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192\n \u0394 (abs t) \u2248 abs (abs (\u0394 t))\n\u0394-abs t = ext (\u03bb \u03c1 \u2192 \u2261-refl)\n\n\u0394-app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192\n \u0394 (app t\u2081 t\u2082) \u2248 app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n\u0394-app t\u2081 t\u2082 = \u2248-sym (ext (\u03bb \u03c1 \u2192\n begin\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 lift-term t\u2082 \u27e7 \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 lift-term t\u2082 \u27e7 \u03c1))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))) x))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) x))\n (lift-term-ignore {\u03c1 = \u03c1} t\u2082) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff (\u27e6 t\u2081 \u27e7 (update \u03c1) x) (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (apply-diff (\u27e6 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 t\u2082 \u27e7 (update \u03c1))) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u220e)) where open \u2261-Reasoning\n\n-- SYMBOLIC DERIVATION\n\nderive-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-var this = this\nderive-var (that x) = that (that (derive-var x))\n\ndiff-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4)\ndiff-term = {!!}\n\napply-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 (\u0394-Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4\napply-term = {!!}\n\n_and_ : \u2200 {\u0393} \u2192 Term \u0393 bool \u2192 Term \u0393 bool \u2192 Term \u0393 bool\na and b = {!!}\n\n!_ : \u2200 {\u0393} \u2192 Term \u0393 bool \u2192 Term \u0393 bool\n! x = {!!}\n\nderive-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-term (abs t) = abs (abs (derive-term t))\nderive-term (app t\u2081 t\u2082) = app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\nderive-term (var x) = var (derive-var x)\nderive-term true = false\nderive-term false = false\nderive-term (if c t e) =\n if ((derive-term c) and (lift-term c))\n (diff-term (apply-term (derive-term e) (lift-term e)) (lift-term t))\n (if ((derive-term c) and (lift-term (! c)))\n (diff-term (apply-term (derive-term t) (lift-term t)) (lift-term e))\n (if (lift-term c)\n (derive-term t)\n (derive-term e)))\n\nderive-term (\u0394 t) = \u0394 (derive-term t)\nderive-term (weakenOne \u0393\u2081 \u03c4\u2082 {\u0393\u2083} t) =\n substTerm (\u0394-Context-\u22ce-expanded \u0393\u2081 \u03c4\u2082 \u0393\u2083)\n (weakenOne (\u0394-Context \u0393\u2081) (\u0394-Type \u03c4\u2082)\n (weakenOne (\u0394-Context \u0393\u2081) \u03c4\u2082\n (substTerm (\u0394-Context-\u22ce \u0393\u2081 \u0393\u2083)\n (derive-term t))))\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n \u0394 t \u2248 derive-term t\nderive-term-correct {\u0393} (abs t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct t)) \u27e9\n abs (abs (derive-term t))\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (abs t)\n \u220e where open \u2248-Reasoning\nderive-term-correct (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct t\u2081) \u2248-refl) (derive-term-correct t\u2082) \u27e9\n app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct (var x) = ext (\u03bb \u03c1 \u2192 derive-var-correct \u03c1 x)\nderive-term-correct true = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\nderive-term-correct (weakenOne _ _ t) = {!!}\n","old_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nimport Relation.Binary as B\n\nopen import Relation.Binary using\n (IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)\nimport Relation.Binary.EqReasoning as EqR\n\nopen import Relation.Nullary using (\u00ac_)\n\nopen import meaning\nopen import IlcModel\nopen import Changes\nopen import ChangeContexts\nopen import binding Type \u27e6_\u27e7Type\nopen import TotalTerms\n\n-- LIFTING terms into \u0394-Contexts\n\nlift-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) \u03c4\nlift-var this = that this\nlift-var (that x) = that (that (lift-var x))\n\nlift-var\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-var\u2032 \u2205 x = lift-var x\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) this = this\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) (that x) = that (lift-var\u2032 \u0393\u2032 x)\n\nlift-var-3 : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context (\u0394-Context \u0393)) \u03c4\nlift-var-3 this = that (that (that this))\nlift-var-3 (that x) = that (that (that (that (lift-var-3 x))))\n\nlift-term\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-term\u2032 \u0393\u2032 (abs t) = abs (lift-term\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term\u2032 \u0393\u2032 (app t\u2081 t\u2082) = app (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082)\nlift-term\u2032 \u0393\u2032 (var x) = var (lift-var\u2032 \u0393\u2032 x)\nlift-term\u2032 \u0393\u2032 true = true\nlift-term\u2032 \u0393\u2032 false = false\nlift-term\u2032 \u0393\u2032 (if t\u2081 t\u2082 t\u2083) = if (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082) (lift-term\u2032 \u0393\u2032 t\u2083)\nlift-term\u2032 {.(\u0394-Context \u0393)} {.(\u0394-Type \u03c4)} \u0393\u2032 (\u0394 {\u0393} {\u03c4} t) = weakenMore t\n where\n open import Relation.Binary.PropositionalEquality using (sym)\n\n doWeakenMore : \u2200 \u0393prefix \u0393rest {\u03c4} \u2192\n Term (\u0393prefix \u22ce \u0393rest) (\u0394-Type \u03c4) \u2192\n Term (\u0393prefix \u22ce \u0394-Context \u0393rest) (\u0394-Type \u03c4)\n\n doWeakenMore \u0393prefix \u2205 t\u2081 = t\u2081\n doWeakenMore \u2205 (\u03c4\u2082 \u2022 \u0393rest) t\u2081 =\n weakenOne \u2205 (\u0394-Type \u03c4\u2082) (doWeakenMore (\u03c4\u2082 \u2022 \u2205) \u0393rest t\u2081)\n doWeakenMore \u0393prefix (\u03c4\u2082 \u2022 \u0393rest) {\u03c4} t\u2081 =\n weakenOne \u0393prefix (\u0394-Type \u03c4\u2082)\n (substTerm (sym (move-prefix \u0393prefix \u03c4\u2082 (\u0394-Context \u0393rest)))\n (doWeakenMore (\u0393prefix \u22ce (\u03c4\u2082 \u2022 \u2205)) \u0393rest\n (substTerm (move-prefix \u0393prefix \u03c4\u2082 \u0393rest) t\u2081)))\n\n prefix : \u2200 \u0393 \u2192 (\u0393\u2032 : Prefix (\u0394-Context \u0393)) \u2192 Context\n prefix \u0393 \u0393\u2032 = take (\u0394-Context \u0393) \u0393\u2032\n\n rest : \u2200 \u0393 \u2192 (\u0393\u2032 : Prefix (\u0394-Context \u0393)) \u2192 Context\n rest \u0393 \u0393\u2032 = drop (\u0394-Context \u0393) \u0393\u2032\n\n weakenMore2 : \u2200 \u0393 \u0393\u2032 {\u03c4} \u2192\n Term \u0393 \u03c4 \u2192\n Term (prefix \u0393 \u0393\u2032 \u22ce \u0394-Context (rest \u0393 \u0393\u2032)) (\u0394-Type \u03c4)\n weakenMore2 \u0393 \u0393\u2032 t =\n doWeakenMore (prefix \u0393 \u0393\u2032) (rest \u0393 \u0393\u2032) (\n substTerm (sym (take-drop (\u0394-Context \u0393) \u0393\u2032)) (\u0394 t))\n\n weakenMore : --\u2200 {\u0393 \u03c4} \u0393\u2032 \u2192\n Term \u0393 \u03c4 \u2192 Term (\u0394-Context\u2032 (\u0394-Context \u0393) \u0393\u2032) (\u0394-Type \u03c4)\n weakenMore t =\n substTerm\n (sym (take-\u22ce-\u0394-Context-drop-\u0394-Context\u2032 (\u0394-Context \u0393) \u0393\u2032))\n (weakenMore2 \u0393 \u0393\u2032 t)\n\n{-\n weakenMore2 : \u2200 \u0393 \u0393\u2032 {\u03c4} \u2192\n Term \u0393 \u03c4 \u2192\n Term (prefix \u0393 \u0393\u2032 \u22ce \u0394-Context (rest \u0393 \u0393\u2032)) (\u0394-Type \u03c4)\n weakenMore2 \u2205 \u2205 t\u2081 = \u0394 t\u2081\n weakenMore2 (\u03c4\u2081 \u2022 \u0393\u2081) \u2205 t\u2081 =\n weakenOne \u2205 (\u0394-Type (\u0394-Type \u03c4\u2081))\n (weakenOne (\u0394-Type \u03c4\u2081 \u2022 \u2205) (\u0394-Type \u03c4\u2081) {!\n weakenMore2\n (\u0394 {\u03c4\u2081 \u2022 \u0393\u2081} {\u03c4} t\u2081)!})\n --(weakenMore2 {\u03c4\u2081 \u2022 \u0393\u2081} (\u0394-Type \u03c4\u2081 \u2022 \u03c4\u2081 \u2022 \u2205) t\u2081 ))\n\n-- (\u0394 {\u03c4\u2081 \u2022 \u0393\u2081} {\u03c4} t\u2081)))\n\n weakenMore2 (\u03c4\u2081 \u2022 \u0393\u2081) (.(\u0394-Type \u03c4\u2081) \u2022 \u0393\u2032\u2081) t\u2081 = {!!}\n-}\n\nlift-term\u2032 {._} {_} _ (weakenOne _ _ {_} {._} _) = {!!}\n\nlift-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) \u03c4\nlift-term = lift-term\u2032 \u2205\n\n-- PROPERTIES of lift-term\n\nlift-var-ignore : \u2200 {\u0393 \u03c4} (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore \u03c1)\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) (that x) = lift-var-ignore \u03c1 x\n\nlift-var-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) (\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var\u2032 \u0393\u2032 x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-var-ignore\u2032 \u2205 \u03c1 x = lift-var-ignore \u03c1 x\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) (that x) = lift-var-ignore\u2032 \u0393\u2032 \u03c1 x\n\nlift-term-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) {\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term\u2032 \u0393\u2032 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-term-ignore\u2032 \u0393\u2032 (abs t) =\n ext (\u03bb v \u2192 lift-term-ignore\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term-ignore\u2032 \u0393\u2032 (app t\u2081 t\u2082) =\n \u2261-app (lift-term-ignore\u2032 \u0393\u2032 t\u2081) (lift-term-ignore\u2032 \u0393\u2032 t\u2082)\nlift-term-ignore\u2032 \u0393\u2032 (var x) = lift-var-ignore\u2032 \u0393\u2032 _ x\nlift-term-ignore\u2032 \u0393\u2032 true = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 false = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 {\u03c1} (if t\u2081 t\u2082 t\u2083)\n with \u27e6 lift-term\u2032 \u0393\u2032 t\u2081 \u27e7 \u03c1\n | \u27e6 t\u2081 \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\n | lift-term-ignore\u2032 \u0393\u2032 {\u03c1} t\u2081\n... | true | true | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2082\n... | false | false | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2083\nlift-term-ignore\u2032 \u0393\u2032 (\u0394 t) = {!!}\nlift-term-ignore\u2032 _ (weakenOne _ _ {_} {._} _) = {!!}\n\nlift-term-ignore : \u2200 {\u0393 \u03c4} {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore \u03c1)\nlift-term-ignore = lift-term-ignore\u2032 \u2205\n\n\n-- PROPERTIES of \u0394\n\n\u0394-abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192\n \u0394 (abs t) \u2248 abs (abs (\u0394 t))\n\u0394-abs t = ext (\u03bb \u03c1 \u2192 \u2261-refl)\n\n\u0394-app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192\n \u0394 (app t\u2081 t\u2082) \u2248 app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n\u0394-app t\u2081 t\u2082 = \u2248-sym (ext (\u03bb \u03c1 \u2192\n begin\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 lift-term t\u2082 \u27e7 \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 lift-term t\u2082 \u27e7 \u03c1))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))) x))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) x))\n (lift-term-ignore {\u03c1 = \u03c1} t\u2082) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff (\u27e6 t\u2081 \u27e7 (update \u03c1) x) (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (apply-diff (\u27e6 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 t\u2082 \u27e7 (update \u03c1))) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u220e)) where open \u2261-Reasoning\n\n-- SYMBOLIC DERIVATION\n\nderive-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-var this = this\nderive-var (that x) = that (that (derive-var x))\n\ndiff-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4)\ndiff-term = {!!}\n\napply-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 (\u0394-Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4\napply-term = {!!}\n\n_and_ : \u2200 {\u0393} \u2192 Term \u0393 bool \u2192 Term \u0393 bool \u2192 Term \u0393 bool\na and b = {!!}\n\n!_ : \u2200 {\u0393} \u2192 Term \u0393 bool \u2192 Term \u0393 bool\n! x = {!!}\n\nderive-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-term (abs t) = abs (abs (derive-term t))\nderive-term (app t\u2081 t\u2082) = app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\nderive-term (var x) = var (derive-var x)\nderive-term true = false\nderive-term false = false\nderive-term (if c t e) =\n if ((derive-term c) and (lift-term c))\n (diff-term (apply-term (derive-term e) (lift-term e)) (lift-term t))\n (if ((derive-term c) and (lift-term (! c)))\n (diff-term (apply-term (derive-term t) (lift-term t)) (lift-term e))\n (if (lift-term c)\n (derive-term t)\n (derive-term e)))\n\nderive-term (\u0394 t) = \u0394 (derive-term t)\nderive-term (weakenOne \u0393\u2081 \u03c4\u2082 {\u0393\u2083} t) =\n substTerm (\u0394-Context-\u22ce-expanded \u0393\u2081 \u03c4\u2082 \u0393\u2083)\n (weakenOne (\u0394-Context \u0393\u2081) (\u0394-Type \u03c4\u2082)\n (weakenOne (\u0394-Context \u0393\u2081) \u03c4\u2082\n (substTerm (\u0394-Context-\u22ce \u0393\u2081 \u0393\u2083)\n (derive-term t))))\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n \u0394 t \u2248 derive-term t\nderive-term-correct {\u0393} (abs t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct t)) \u27e9\n abs (abs (derive-term t))\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (abs t)\n \u220e where open \u2248-Reasoning\nderive-term-correct (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct t\u2081) \u2248-refl) (derive-term-correct t\u2082) \u27e9\n app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct (var x) = ext (\u03bb \u03c1 \u2192 derive-var-correct \u03c1 x)\nderive-term-correct true = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\nderive-term-correct (weakenOne _ _ t) = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"322bfa9ea5da1ad8271fefe5c6188aaa3736ee9d","subject":"Desc model: proof-phi-psi implicit.","message":"Desc model: proof-phi-psi implicit.\n","repos":"larrytheliquid\/pigit,mietek\/epigram2,mietek\/epigram2","old_file":"models\/IDesc_type_type.agda","new_file":"models\/IDesc_type_type.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {I : Set} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi (pi S T) = cong (pi S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n\n-- From embedding to embedding\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = psi (phi D) == D\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\u03bb _ \u2192 descD I)\n (P I)\n (proof-psi-phi-casesW I) \n Void\n D\n where proof-psi-phi-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-psi-phi-cases I (lvar , i) hs = refl\n proof-psi-phi-cases I (lconst , x) hs = refl\n proof-psi-phi-cases I (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases I (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\u03bb s \u2192 psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases I (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s \u2192 psi (phi (T s)))\n T\n hs)\n proof-psi-phi-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-psi-phi-casesW I Void = proof-psi-phi-cases I\n \n\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : (I : Set) -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi I (var i) = refl\nproof-phi-psi I (const x) = refl\nproof-phi-psi I (prod D D') with proof-phi-psi I D | proof-phi-psi I D'\n... | p | q = cong2 prod p q\nproof-phi-psi I (pi S T) = cong (pi S) \n (reflFun (\u03bb s \u2192 phi (psi (T s)))\n T\n (\\s -> proof-phi-psi I (T s)))\nproof-phi-psi I (sigma S T) = cong (sigma S) \n (reflFun (\u03bb s \u2192 phi (psi (T s)))\n T\n (\\s -> proof-phi-psi I (T s)))\n\n\n-- From embedding to embedding\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = psi (phi D) == D\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\u03bb _ \u2192 descD I)\n (P I)\n (proof-psi-phi-casesW I) \n Void\n D\n where proof-psi-phi-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-psi-phi-cases I (lvar , i) hs = refl\n proof-psi-phi-cases I (lconst , x) hs = refl\n proof-psi-phi-cases I (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases I (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\u03bb s \u2192 psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases I (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s \u2192 psi (phi (T s)))\n T\n hs)\n proof-psi-phi-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-psi-phi-casesW I Void = proof-psi-phi-cases I\n \n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a6016a23a99d566ccb1cf3fc64f026e867f0dc5a","subject":"Universe: \u03a0\u2192\u03a0\u1d41","message":"Universe: \u03a0\u2192\u03a0\u1d41\n","repos":"crypto-agda\/explore","old_file":"lib\/Explore\/Universe.agda","new_file":"lib\/Explore\/Universe.agda","new_contents":"open import Level.NP\nopen import Type\nopen import Type.Identities\nopen import Function.NP\nopen import Function.Extensionality\nopen import Data.Zero\nopen import Data.One\nopen import Data.Two\nopen import Data.Product.NP\nopen import Data.Sum.NP\nopen import Data.Sum.Logical\nopen import Data.Nat\nopen import Data.Fin\nopen import Relation.Nullary.NP\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Relation.Binary.Logical\n\nopen import HoTT\nopen Equivalences\n\nopen import Explore.Core\nopen import Explore.Properties\nopen import Explore.Zero\nopen import Explore.One\nopen import Explore.Two\nopen import Explore.Product\nopen import Explore.Sum\nopen import Explore.Explorable\nimport Explore.Isomorphism\n\nmodule Explore.Universe where\n\nopen Operators\n\ninfixr 2 _\u00d7\u1d41_\n\ndata U : \u2605\nEl : U \u2192 \u2605\n\ndata U where\n \ud835\udfd8\u1d41 \ud835\udfd9\u1d41 \ud835\udfda\u1d41 : U\n _\u00d7\u1d41_ _\u228e\u1d41_ : U \u2192 U \u2192 U\n \u03a3\u1d41 : (t : U) (f : El t \u2192 U) \u2192 U\n\nEl \ud835\udfd8\u1d41 = \ud835\udfd8\nEl \ud835\udfd9\u1d41 = \ud835\udfd9\nEl \ud835\udfda\u1d41 = \ud835\udfda\nEl (s \u00d7\u1d41 t) = El s \u00d7 El t\nEl (s \u228e\u1d41 t) = El s \u228e El t\nEl (\u03a3\u1d41 t f) = \u03a3 (El t) \u03bb x \u2192 El (f x)\n\ndata \u27e6U\u27e7 : \u27e6\u2605\u2080\u27e7 U U\n\u27e6El\u27e7 : (\u27e6U\u27e7 \u27e6\u2192\u27e7 \u27e6\u2605\u2080\u27e7) El El\n\ndata \u27e6U\u27e7 where\n \u27e6\ud835\udfd8\u1d41\u27e7 : \u27e6U\u27e7 \ud835\udfd8\u1d41 \ud835\udfd8\u1d41\n \u27e6\ud835\udfd9\u1d41\u27e7 : \u27e6U\u27e7 \ud835\udfd9\u1d41 \ud835\udfd9\u1d41\n \u27e6\ud835\udfda\u1d41\u27e7 : \u27e6U\u27e7 \ud835\udfda\u1d41 \ud835\udfda\u1d41\n _\u27e6\u00d7\u1d41\u27e7_ : \u27e6Op\u2082\u27e7 \u27e6U\u27e7 _\u00d7\u1d41_ _\u00d7\u1d41_\n _\u27e6\u228e\u1d41\u27e7_ : \u27e6Op\u2082\u27e7 \u27e6U\u27e7 _\u228e\u1d41_ _\u228e\u1d41_\n \u27e6\u03a3\u1d41\u27e7 : (\u27e8 t \u2236 \u27e6U\u27e7 \u27e9\u27e6\u2192\u27e7 (\u27e6El\u27e7 t \u27e6\u2192\u27e7 \u27e6U\u27e7) \u27e6\u2192\u27e7 \u27e6U\u27e7) \u03a3\u1d41 \u03a3\u1d41\n\n\u27e6El\u27e7 \u27e6\ud835\udfd8\u1d41\u27e7 = _\u2261_\n\u27e6El\u27e7 \u27e6\ud835\udfd9\u1d41\u27e7 = _\u2261_\n\u27e6El\u27e7 \u27e6\ud835\udfda\u1d41\u27e7 = _\u2261_\n\u27e6El\u27e7 (s \u27e6\u00d7\u1d41\u27e7 t) = \u27e6El\u27e7 s \u27e6\u00d7\u27e7 \u27e6El\u27e7 t\n\u27e6El\u27e7 (s \u27e6\u228e\u1d41\u27e7 t) = \u27e6El\u27e7 s \u27e6\u228e\u27e7 \u27e6El\u27e7 t\n\u27e6El\u27e7 (\u27e6\u03a3\u1d41\u27e7 t f) = \u27e6\u03a3\u27e7 (\u27e6El\u27e7 t) \u03bb x \u2192 \u27e6El\u27e7 (f x)\n\nmodule _ {k} {K : \u2605_ k} {a} {A : \u2605_ a} {x y : A} (p : x \u2261 y) where\n tr-const : tr (const K) p \u2261 id\n tr-const = J (\u03bb x\u2081 p\u2081 \u2192 tr (const K) p\u2081 \u2261 id) refl p\n\n {-\n\u27e6U\u27e7-sound : \u2200 {{_ : FunExt}} {x y} \u2192 \u27e6U\u27e7 x y \u2192 x \u2261 y\n\u27e6U\u27e7-refl : \u2200 x \u2192 \u27e6U\u27e7 x x\n\n{-\n\u27e6El\u27e7-refl : \u2200 x \u2192 {!\u27e6El\u27e7 x x!}\n\u27e6El\u27e7-refl = {!!}\n-}\n\n\u27e6U\u27e7-sound \u27e6\ud835\udfd8\u1d41\u27e7 = refl\n\u27e6U\u27e7-sound \u27e6\ud835\udfd9\u1d41\u27e7 = refl\n\u27e6U\u27e7-sound \u27e6\ud835\udfda\u1d41\u27e7 = refl\n\u27e6U\u27e7-sound (u \u27e6\u00d7\u1d41\u27e7 u\u2081) = ap\u2082 _\u00d7\u1d41_ (\u27e6U\u27e7-sound u) (\u27e6U\u27e7-sound u\u2081)\n\u27e6U\u27e7-sound (u \u27e6\u228e\u1d41\u27e7 u\u2081) = ap\u2082 _\u228e\u1d41_ (\u27e6U\u27e7-sound u) (\u27e6U\u27e7-sound u\u2081)\n\u27e6U\u27e7-sound (\u27e6\u03a3\u1d41\u27e7 {u\u2080} {u\u2081} u {f\u2080} {f\u2081} f\u1d63) = apd\u2082 \u03a3\u1d41 (\u27e6U\u27e7-sound u) (tr-\u2192 El (const U) (\u27e6U\u27e7-sound u) f\u2080 \u2219 \u03bb= (\u03bb A \u2192 ap (\u03bb z \u2192 z (f\u2080 (tr El (! \u27e6U\u27e7-sound u) A))) (tr-const (\u27e6U\u27e7-sound u)) \u2219 \u27e6U\u27e7-sound (f\u1d63 {!!}))) -- (\u03bb= (\u03bb y \u2192 let foo = x\u1d63 {{!!}} {y} {!x\u1d63!} in {!tr-\u2192 El (const U) (\u27e6U\u27e7-sound u)!}))\n\n\u27e6U\u27e7-refl \ud835\udfd8\u1d41 = \u27e6\ud835\udfd8\u1d41\u27e7\n\u27e6U\u27e7-refl \ud835\udfd9\u1d41 = \u27e6\ud835\udfd9\u1d41\u27e7\n\u27e6U\u27e7-refl \ud835\udfda\u1d41 = \u27e6\ud835\udfda\u1d41\u27e7\n\u27e6U\u27e7-refl (x \u00d7\u1d41 x\u2081) = \u27e6U\u27e7-refl x \u27e6\u00d7\u1d41\u27e7 \u27e6U\u27e7-refl x\u2081\n\u27e6U\u27e7-refl (x \u228e\u1d41 x\u2081) = \u27e6U\u27e7-refl x \u27e6\u228e\u1d41\u27e7 \u27e6U\u27e7-refl x\u2081\n\u27e6U\u27e7-refl (\u03a3\u1d41 x f) = \u27e6\u03a3\u1d41\u27e7 (\u27e6U\u27e7-refl x) (\u03bb y \u2192 {!\u27e6U\u27e7-refl ?!})\n-}\n\ninfix 8 _^\u1d41_\n_^\u1d41_ : U \u2192 \u2115 \u2192 U\nt ^\u1d41 zero = t\nt ^\u1d41 suc n = t \u00d7\u1d41 t ^\u1d41 n\n\nmodule _ {\u2113} where\n\n explore : \u2200 t \u2192 Explore \u2113 (El t)\n explore \ud835\udfd8\u1d41 = \ud835\udfd8\u1d49\n explore \ud835\udfd9\u1d41 = \ud835\udfd9\u1d49\n explore \ud835\udfda\u1d41 = \ud835\udfda\u1d49\n explore (s \u00d7\u1d41 t) = explore s \u00d7\u1d49 explore t\n explore (s \u228e\u1d41 t) = explore s \u228e\u1d49 explore t\n explore (\u03a3\u1d41 t f) = explore\u03a3 (explore t) \u03bb {x} \u2192 explore (f x)\n\n exploreU-ind : \u2200 {p} t \u2192 ExploreInd p (explore t)\n exploreU-ind \ud835\udfd8\u1d41 = \ud835\udfd8\u2071\n exploreU-ind \ud835\udfd9\u1d41 = \ud835\udfd9\u2071\n exploreU-ind \ud835\udfda\u1d41 = \ud835\udfda\u2071\n exploreU-ind (s \u00d7\u1d41 t) = exploreU-ind s \u00d7\u2071 exploreU-ind t\n exploreU-ind (s \u228e\u1d41 t) = exploreU-ind s \u228e\u2071 exploreU-ind t\n exploreU-ind (\u03a3\u1d41 t f) = explore\u03a3-ind (exploreU-ind t) \u03bb {x} \u2192 exploreU-ind (f x)\n\nmodule _ {\u2113\u2080 \u2113\u2081 \u2113\u1d63} where\n \u27e6explore\u27e7 : \u2200 {t\u2080 t\u2081} (t : \u27e6U\u27e7 t\u2080 t\u2081) \u2192 \u27e6Explore\u27e7 {\u2113\u2080} {\u2113\u2081} \u2113\u1d63 (\u27e6El\u27e7 t) (explore t\u2080) (explore t\u2081)\n \u27e6explore\u27e7 \u27e6\ud835\udfd8\u1d41\u27e7 = \u27e6\ud835\udfd8\u1d49\u27e7 {\u2113\u2080} {\u2113\u2081} {\u2113\u1d63}\n \u27e6explore\u27e7 \u27e6\ud835\udfd9\u1d41\u27e7 = \u27e6\ud835\udfd9\u1d49\u27e7 {\u2113\u2080} {\u2113\u2081} {\u2113\u1d63} {_\u2261_} {refl}\n \u27e6explore\u27e7 \u27e6\ud835\udfda\u1d41\u27e7 = \u27e6\ud835\udfda\u1d49\u27e7 {\u2113\u2080} {\u2113\u2081} {\u2113\u1d63} {_\u2261_} {refl} {refl}\n \u27e6explore\u27e7 (t \u27e6\u00d7\u1d41\u27e7 t\u2081) = \u27e6explore\u00d7\u27e7 {\u2113\u2080} {\u2113\u2081} {\u2113\u1d63} (\u27e6explore\u27e7 t) (\u27e6explore\u27e7 t\u2081)\n \u27e6explore\u27e7 (t \u27e6\u228e\u1d41\u27e7 t\u2081) = \u27e6explore\u228e\u27e7 {\u2113\u2080} {\u2113\u2081} {\u2113\u1d63} (\u27e6explore\u27e7 t) (\u27e6explore\u27e7 t\u2081)\n \u27e6explore\u27e7 (\u27e6\u03a3\u1d41\u27e7 t f) = \u27e6explore\u03a3\u27e7 {\u2113\u2080} {\u2113\u2081} {\u2113\u1d63} (\u27e6explore\u27e7 t) (\u27e6explore\u27e7 \u2218 f)\n\nmodule _ (t : U) where\n private\n t\u1d49 : \u2200 {\u2113} \u2192 Explore \u2113 (El t)\n t\u1d49 = explore t\n t\u2071 : \u2200 {\u2113 p} \u2192 ExploreInd {\u2113} p t\u1d49\n t\u2071 = exploreU-ind t\n\n open FromExploreInd t\u2071 public hiding (\u27e6explore\u27e7)\n {-\n open From\u27e6Explore\u27e7 (\u03bb {\u2113\u2081} {\u2113\u2082} {\u2113\u1d63} \u2192 \u27e6explore\u27e7' {\u2113\u2081} {\u2113\u2082} {\u2113\u1d63} t) public\n -}\n\nadequate-sumU : \u2200 {{_ : UA}}{{_ : FunExt}} t \u2192 Adequate-sum (sum t)\nadequate-sumU \ud835\udfd8\u1d41 = \ud835\udfd8\u02e2-ok\nadequate-sumU \ud835\udfd9\u1d41 = \ud835\udfd9\u02e2-ok\nadequate-sumU \ud835\udfda\u1d41 = \ud835\udfda\u02e2-ok\nadequate-sumU (s \u00d7\u1d41 t) = adequate-sum\u03a3 (adequate-sumU s) (adequate-sumU t)\nadequate-sumU (s \u228e\u1d41 t) = adequate-sum\u228e (adequate-sumU s) (adequate-sumU t)\nadequate-sumU (\u03a3\u1d41 t f) = adequate-sum\u03a3 (adequate-sumU t) (\u03bb {x} \u2192 adequate-sumU (f x))\n\nmodule _ {\u2113} where\n lookupU : \u2200 t \u2192 Lookup {\u2113} (explore t)\n lookupU \ud835\udfd8\u1d41 = \ud835\udfd8\u02e1\n lookupU \ud835\udfd9\u1d41 = \ud835\udfd9\u02e1\n lookupU \ud835\udfda\u1d41 = \ud835\udfda\u02e1\n lookupU (s \u00d7\u1d41 t) = lookup\u00d7 {e\u1d2c = explore s} {e\u1d2e = explore t} (lookupU s) (lookupU t)\n lookupU (s \u228e\u1d41 t) = lookup\u228e {e\u1d2c = explore s} {e\u1d2e = explore t} (lookupU s) (lookupU t)\n lookupU (\u03a3\u1d41 t f) = lookup\u03a3 {e\u1d2c = explore t} {e\u1d2e = \u03bb {x} \u2192 explore (f x)} (lookupU t) (\u03bb {x} \u2192 lookupU (f x))\n\n focusU : \u2200 t \u2192 Focus {\u2113} (explore t)\n focusU \ud835\udfd8\u1d41 = \ud835\udfd8\u1da0\n focusU \ud835\udfd9\u1d41 = \ud835\udfd9\u1da0\n focusU \ud835\udfda\u1d41 = \ud835\udfda\u1da0\n focusU (s \u00d7\u1d41 t) = focus\u00d7 {e\u1d2c = explore s} {e\u1d2e = explore t} (focusU s) (focusU t)\n focusU (s \u228e\u1d41 t) = focus\u228e {e\u1d2c = explore s} {e\u1d2e = explore t} (focusU s) (focusU t)\n focusU (\u03a3\u1d41 t f) = focus\u03a3 {e\u1d2c = explore t} {e\u1d2e = \u03bb {x} \u2192 explore (f x)} (focusU t) (\u03bb {x} \u2192 focusU (f x))\n\n \u03a3\u1d49U : \u2200 A {\u2113} \u2192 (El A \u2192 \u2605_ \u2113) \u2192 \u2605_ \u2113\n \u03a3\u1d49U = \u03bb A \u2192 \u03a3\u1d49 (explore A)\n \u03a0\u1d49U : \u2200 A {\u2113} \u2192 (El A \u2192 \u2605_ \u2113) \u2192 \u2605_ \u2113\n \u03a0\u1d49U = \u03bb A \u2192 \u03a0\u1d49 (explore A)\n\n module _ {{_ : UA}}{{_ : FunExt}} where\n \u03a3\u1d49U-ok : \u2200 t \u2192 Adequate-\u03a3 {\u2113} (\u03a3\u1d49U t)\n \u03a3\u1d49U-ok \ud835\udfd8\u1d41 = \u03a3\u1d49\ud835\udfd8-ok\n \u03a3\u1d49U-ok \ud835\udfd9\u1d41 = \u03a3\u1d49\ud835\udfd9-ok\n \u03a3\u1d49U-ok \ud835\udfda\u1d41 = \u03a3\u1d49\ud835\udfda-ok\n \u03a3\u1d49U-ok (t \u00d7\u1d41 u) = \u03a3\u1d49\u00d7-ok {e\u1d2c = explore t} {e\u1d2e = explore u} (\u03a3\u1d49U-ok t) (\u03a3\u1d49U-ok u)\n \u03a3\u1d49U-ok (t \u228e\u1d41 u) = \u03a3\u1d49\u228e-ok {e\u1d2c = explore t} {e\u1d2e = explore u} (\u03a3\u1d49U-ok t) (\u03a3\u1d49U-ok u)\n \u03a3\u1d49U-ok (\u03a3\u1d41 t u) = \u03a3\u1d49\u03a3-ok {e\u1d2c = explore t} {e\u1d2e = \u03bb {x} \u2192 explore (u x)} (\u03a3\u1d49U-ok t) (\u03bb {x} \u2192 \u03a3\u1d49U-ok (u x))\n\n \u03a0\u1d49U-ok : \u2200 t \u2192 Adequate-\u03a0 {\u2113} (\u03a0\u1d49U t)\n \u03a0\u1d49U-ok \ud835\udfd8\u1d41 = \u03a0\u1d49\ud835\udfd8-ok\n \u03a0\u1d49U-ok \ud835\udfd9\u1d41 = \u03a0\u1d49\ud835\udfd9-ok\n \u03a0\u1d49U-ok \ud835\udfda\u1d41 = \u03a0\u1d49\ud835\udfda-ok\n \u03a0\u1d49U-ok (t \u00d7\u1d41 u) = \u03a0\u1d49\u00d7-ok {e\u1d2c = explore t} {e\u1d2e = explore u} (\u03a0\u1d49U-ok t) (\u03a0\u1d49U-ok u)\n \u03a0\u1d49U-ok (t \u228e\u1d41 u) = \u03a0\u1d49\u228e-ok {e\u1d2c = explore t} {e\u1d2e = explore u} (\u03a0\u1d49U-ok t) (\u03a0\u1d49U-ok u)\n \u03a0\u1d49U-ok (\u03a3\u1d41 t u) = \u03a0\u1d49\u03a3-ok {e\u1d2c = explore t} {e\u1d2e = \u03bb {x} \u2192 explore (u x)} (\u03a0\u1d49U-ok t) (\u03bb {x} \u2192 \u03a0\u1d49U-ok (u x))\n\n{-\nmodule _ (t : U) {{_ : UA}} {{_ : FunExt}} where\n open FromAdequate-\u03a3\u1d49 t (\u03a3\u1d49U-ok t) public\n open FromAdequate-\u03a0\u1d49 t (\u03a0\u1d49U-ok t) public\n-}\n\nmodule _ (A : U) (P : El A \u2192 \u2605\u2080) where\n Dec-\u03a3 : \u03a0 (El A) (Dec \u2218 P) \u2192 Dec (\u03a3 (El A) P)\n Dec-\u03a3 = map-Dec (unfocus A) (focusU A) \u2218 lift-Dec A P \u2218 reify A\n\n-- See Explore.Fin for an example of the use of this module\nmodule Isomorphism {A : \u2605\u2080} u (e : El u \u2243 A) where\n open Explore.Isomorphism e\n\n module _ {\u2113} where\n iso\u1d49 : Explore \u2113 A\n iso\u1d49 = explore-iso (explore u)\n\n module _ {p} where\n iso\u2071 : ExploreInd p iso\u1d49\n iso\u2071 = explore-iso-ind (exploreU-ind u)\n\n module _ {\u2113} where\n {-\n iso\u02e1 : Lookup {\u2113} iso\u1d49\n iso\u02e1 = lookup-iso {\u2113} {exploreU u} (lookupU u)\n\n iso\u1da0 : Focus {\u2113} iso\u1d49\n iso\u1da0 = focus-iso {\u2113} {exploreU u} (focusU u)\n -}\n\n iso\u02b3 : Reify {\u2113} iso\u1d49\n iso\u02b3 = FromExploreInd-iso.reify (exploreU-ind u)\n\n iso\u1d58 : Unfocus {\u2113} iso\u1d49\n iso\u1d58 = FromExploreInd-iso.unfocus (exploreU-ind u)\n\n iso\u02e2 : Sum A\n iso\u02e2 = sum-iso (sum u)\n\n iso\u1d56 : Product A\n iso\u1d56 = product-iso (sum u)\n\n module _ {{_ : UA}}{{_ : FunExt}} where\n iso\u02e2-ok : Adequate-sum iso\u02e2\n iso\u02e2-ok = sum-iso-ok (adequate-sumU u)\n\n open Adequate-sum\u2080 iso\u02e2-ok iso\u02e2-ok public renaming (sumStableUnder to iso\u02e2-stableUnder)\n\nFin\u1d41 : \u2115 \u2192 U\nFin\u1d41 zero = \ud835\udfd8\u1d41\nFin\u1d41 (suc n) = \ud835\udfd9\u1d41 \u228e\u1d41 Fin\u1d41 n\n\nFin\u1d41' : \u2115 \u2192 U\nFin\u1d41' zero = \ud835\udfd8\u1d41\nFin\u1d41' (suc zero) = \ud835\udfd9\u1d41\nFin\u1d41' (suc (suc n)) = \ud835\udfd9\u1d41 \u228e\u1d41 Fin\u1d41' (suc n)\n\nFin\u1d41-Fin : \u2200 n \u2192 El (Fin\u1d41 n) \u2243 Fin n\nFin\u1d41-Fin zero = \u2243-! Fin0\u2243\ud835\udfd8\nFin\u1d41-Fin (suc n) = \u228e\u2243 \u2243-refl (Fin\u1d41-Fin n) \u2243-\u2219 \u2243-! Fin\u2218suc\u2243\ud835\udfd9\u228eFin\n\nFin\u1d41'-Fin : \u2200 n \u2192 El (Fin\u1d41' n) \u2243 Fin n\nFin\u1d41'-Fin zero = \u2243-! Fin0\u2243\ud835\udfd8\nFin\u1d41'-Fin (suc zero) = \u2243-! Fin1\u2243\ud835\udfd9\nFin\u1d41'-Fin (suc (suc n)) = \u228e\u2243 \u2243-refl (Fin\u1d41'-Fin (suc n)) \u2243-\u2219 \u2243-! Fin\u2218suc\u2243\ud835\udfd9\u228eFin\n\n\u03a0\u1d41 : (u : U) (v : El u \u2192 U) \u2192 U\n\u03a0\u1d41 \ud835\udfd8\u1d41 v = \ud835\udfd9\u1d41\n\u03a0\u1d41 \ud835\udfd9\u1d41 v = v _\n\u03a0\u1d41 \ud835\udfda\u1d41 v = v 0\u2082 \u00d7\u1d41 v 1\u2082\n\u03a0\u1d41 (u \u00d7\u1d41 u\u2081) v = \u03a0\u1d41 u \u03bb x \u2192 \u03a0\u1d41 u\u2081 \u03bb y \u2192 v (x , y)\n\u03a0\u1d41 (u \u228e\u1d41 u\u2081) v = (\u03a0\u1d41 u (v \u2218 inl)) \u00d7\u1d41 (\u03a0\u1d41 u\u2081 (v \u2218 inr))\n\u03a0\u1d41 (\u03a3\u1d41 u f) v = \u03a0\u1d41 u \u03bb x \u2192 \u03a0\u1d41 (f x) (v \u2218 _,_ x)\n\n_\u2192\u1d41_ : (u : U) (v : U) \u2192 U\nu \u2192\u1d41 v = \u03a0\u1d41 u (const v)\n\n{-\n\ud835\udfdb\u1d41 : U\n\ud835\udfdb\u1d41 = \ud835\udfd9\u1d41 \u228e\u1d41 \ud835\udfda\u1d41\n\nlist22 = list (\ud835\udfda\u1d41 \u2192\u1d41 \ud835\udfda\u1d41)\nlist33 = list (\ud835\udfdb\u1d41 \u2192\u1d41 \ud835\udfdb\u1d41)\n-}\n\nmodule _ {{_ : UA}}{{_ : FunExt}} where\n \u03a0\u1d41-\u03a0 : \u2200 u v \u2192 El (\u03a0\u1d41 u v) \u2261 \u03a0 (El u) (El \u2218 v)\n \u03a0\u1d41-\u03a0 \ud835\udfd8\u1d41 v = ! \u03a0\ud835\udfd8-uniq\u2080 _\n \u03a0\u1d41-\u03a0 \ud835\udfd9\u1d41 v = ! \u03a0\ud835\udfd9-uniq _\n \u03a0\u1d41-\u03a0 \ud835\udfda\u1d41 v = ! \u03a0\ud835\udfda-\u00d7\n \u03a0\u1d41-\u03a0 (u \u00d7\u1d41 u\u2081) v = \u03a0\u1d41-\u03a0 u (\u03bb x \u2192 \u03a0\u1d41 u\u2081 (v \u2218 _,_ x)) \u2219 \u03a0=\u2032 _ (\u03bb _ \u2192 \u03a0\u1d41-\u03a0 u\u2081 _) \u2219 ! \u03a0\u03a3-curry\n \u03a0\u1d41-\u03a0 (u \u228e\u1d41 u\u2081) v = \u00d7= (\u03a0\u1d41-\u03a0 u (v \u2218 inl)) (\u03a0\u1d41-\u03a0 u\u2081 (v \u2218 inr)) \u2219 ! dist-\u00d7-\u03a0\n \u03a0\u1d41-\u03a0 (\u03a3\u1d41 u f) v = \u03a0\u1d41-\u03a0 u (\u03bb x \u2192 \u03a0\u1d41 (f _) (v \u2218 _,_ x)) \u2219 \u03a0=\u2032 _ (\u03bb _ \u2192 \u03a0\u1d41-\u03a0 (f _) _) \u2219 ! \u03a0\u03a3-curry\n\n \u2192\u1d41-\u2192 : \u2200 u v \u2192 El (u \u2192\u1d41 v) \u2261 (El u \u2192 El v)\n \u2192\u1d41-\u2192 u v = \u03a0\u1d41-\u03a0 u (const v)\n\n \u03a0\u1d41\u2192\u03a0 : \u2200 u v \u2192 El (\u03a0\u1d41 u v) \u2192 \u03a0 (El u) (El \u2218 v)\n \u03a0\u1d41\u2192\u03a0 \ud835\udfd8\u1d41 v x\u2082 ()\n \u03a0\u1d41\u2192\u03a0 \ud835\udfd9\u1d41 v x\u2082 x\u2083 = x\u2082\n \u03a0\u1d41\u2192\u03a0 \ud835\udfda\u1d41 v (x , y) 0\u2082 = x\n \u03a0\u1d41\u2192\u03a0 \ud835\udfda\u1d41 v (x , y) 1\u2082 = y\n \u03a0\u1d41\u2192\u03a0 (u \u00d7\u1d41 u\u2081) v x (z , t) = \u03a0\u1d41\u2192\u03a0 u\u2081 (v \u2218 _,_ z) (\u03a0\u1d41\u2192\u03a0 u (\u03bb x \u2192 \u03a0\u1d41 u\u2081 (v \u2218 _,_ x)) x z) t\n \u03a0\u1d41\u2192\u03a0 (u \u228e\u1d41 _) v (x , _) (inl y) = \u03a0\u1d41\u2192\u03a0 u (v \u2218 inl) x y\n \u03a0\u1d41\u2192\u03a0 (_ \u228e\u1d41 u) v (_ , x) (inr y) = \u03a0\u1d41\u2192\u03a0 u (v \u2218 inr) x y\n \u03a0\u1d41\u2192\u03a0 (\u03a3\u1d41 u f) v x (y , z) = \u03a0\u1d41\u2192\u03a0 (f _) (v \u2218 _,_ y) (\u03a0\u1d41\u2192\u03a0 u (\u03bb x \u2192 \u03a0\u1d41 (f _) (v \u2218 _,_ x)) x y) z\n\n \u03a0\u2192\u03a0\u1d41 : \u2200 u v \u2192 \u03a0 (El u) (El \u2218 v) \u2192 El (\u03a0\u1d41 u v)\n \u03a0\u2192\u03a0\u1d41 \ud835\udfd8\u1d41 v f = 0\u2081\n \u03a0\u2192\u03a0\u1d41 \ud835\udfd9\u1d41 v f = f 0\u2081\n \u03a0\u2192\u03a0\u1d41 \ud835\udfda\u1d41 v f = f 0\u2082 , f 1\u2082\n \u03a0\u2192\u03a0\u1d41 (u \u00d7\u1d41 u\u2081) v f = \u03a0\u2192\u03a0\u1d41 u (\u03bb x \u2192 \u03a0\u1d41 u\u2081 (v \u2218 _,_ x))\n (\u03bb x \u2192 \u03a0\u2192\u03a0\u1d41 u\u2081 (v \u2218 _,_ x) (f \u2218 _,_ x))\n \u03a0\u2192\u03a0\u1d41 (u \u228e\u1d41 u\u2081) v f = \u03a0\u2192\u03a0\u1d41 u (v \u2218 inl) (f \u2218 inl) ,\n \u03a0\u2192\u03a0\u1d41 u\u2081 (v \u2218 inr) (f \u2218 inr)\n \u03a0\u2192\u03a0\u1d41 (\u03a3\u1d41 u F) v f = \u03a0\u2192\u03a0\u1d41 u (\u03bb x \u2192 \u03a0\u1d41 (F x) (v \u2218 _,_ x))\n (\u03bb x \u2192 \u03a0\u2192\u03a0\u1d41 (F x) (v \u2218 _,_ x) (f \u2218 _,_ x))\n\n \u2192\u1d41\u2192\u2192 : \u2200 u v \u2192 El (u \u2192\u1d41 v) \u2192 (El u \u2192 El v)\n \u2192\u1d41\u2192\u2192 u v = \u03a0\u1d41\u2192\u03a0 u (const v)\n\n \u2192\u2192\u2192\u1d41 : \u2200 u v \u2192 (El u \u2192 El v) \u2192 El (u \u2192\u1d41 v)\n \u2192\u2192\u2192\u1d41 u v = \u03a0\u2192\u03a0\u1d41 u (const v)\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"open import Level.NP\nopen import Type\nopen import Type.Identities\nopen import Function.NP\nopen import Function.Extensionality\nopen import Data.Zero\nopen import Data.One\nopen import Data.Two\nopen import Data.Product.NP\nopen import Data.Sum.NP\nopen import Data.Sum.Logical\nopen import Data.Nat\nopen import Data.Fin\nopen import Relation.Nullary.NP\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Relation.Binary.Logical\n\nopen import HoTT\nopen Equivalences\n\nopen import Explore.Core\nopen import Explore.Properties\nopen import Explore.Zero\nopen import Explore.One\nopen import Explore.Two\nopen import Explore.Product\nopen import Explore.Sum\nopen import Explore.Explorable\nimport Explore.Isomorphism\n\nmodule Explore.Universe where\n\nopen Operators\n\ninfixr 2 _\u00d7\u1d41_\n\ndata U : \u2605\nEl : U \u2192 \u2605\n\ndata U where\n \ud835\udfd8\u1d41 \ud835\udfd9\u1d41 \ud835\udfda\u1d41 : U\n _\u00d7\u1d41_ _\u228e\u1d41_ : U \u2192 U \u2192 U\n \u03a3\u1d41 : (t : U) (f : El t \u2192 U) \u2192 U\n\nEl \ud835\udfd8\u1d41 = \ud835\udfd8\nEl \ud835\udfd9\u1d41 = \ud835\udfd9\nEl \ud835\udfda\u1d41 = \ud835\udfda\nEl (s \u00d7\u1d41 t) = El s \u00d7 El t\nEl (s \u228e\u1d41 t) = El s \u228e El t\nEl (\u03a3\u1d41 t f) = \u03a3 (El t) \u03bb x \u2192 El (f x)\n\ndata \u27e6U\u27e7 : \u27e6\u2605\u2080\u27e7 U U\n\u27e6El\u27e7 : (\u27e6U\u27e7 \u27e6\u2192\u27e7 \u27e6\u2605\u2080\u27e7) El El\n\ndata \u27e6U\u27e7 where\n \u27e6\ud835\udfd8\u1d41\u27e7 : \u27e6U\u27e7 \ud835\udfd8\u1d41 \ud835\udfd8\u1d41\n \u27e6\ud835\udfd9\u1d41\u27e7 : \u27e6U\u27e7 \ud835\udfd9\u1d41 \ud835\udfd9\u1d41\n \u27e6\ud835\udfda\u1d41\u27e7 : \u27e6U\u27e7 \ud835\udfda\u1d41 \ud835\udfda\u1d41\n _\u27e6\u00d7\u1d41\u27e7_ : \u27e6Op\u2082\u27e7 \u27e6U\u27e7 _\u00d7\u1d41_ _\u00d7\u1d41_\n _\u27e6\u228e\u1d41\u27e7_ : \u27e6Op\u2082\u27e7 \u27e6U\u27e7 _\u228e\u1d41_ _\u228e\u1d41_\n \u27e6\u03a3\u1d41\u27e7 : (\u27e8 t \u2236 \u27e6U\u27e7 \u27e9\u27e6\u2192\u27e7 (\u27e6El\u27e7 t \u27e6\u2192\u27e7 \u27e6U\u27e7) \u27e6\u2192\u27e7 \u27e6U\u27e7) \u03a3\u1d41 \u03a3\u1d41\n\n\u27e6El\u27e7 \u27e6\ud835\udfd8\u1d41\u27e7 = _\u2261_\n\u27e6El\u27e7 \u27e6\ud835\udfd9\u1d41\u27e7 = _\u2261_\n\u27e6El\u27e7 \u27e6\ud835\udfda\u1d41\u27e7 = _\u2261_\n\u27e6El\u27e7 (s \u27e6\u00d7\u1d41\u27e7 t) = \u27e6El\u27e7 s \u27e6\u00d7\u27e7 \u27e6El\u27e7 t\n\u27e6El\u27e7 (s \u27e6\u228e\u1d41\u27e7 t) = \u27e6El\u27e7 s \u27e6\u228e\u27e7 \u27e6El\u27e7 t\n\u27e6El\u27e7 (\u27e6\u03a3\u1d41\u27e7 t f) = \u27e6\u03a3\u27e7 (\u27e6El\u27e7 t) \u03bb x \u2192 \u27e6El\u27e7 (f x)\n\nmodule _ {k} {K : \u2605_ k} {a} {A : \u2605_ a} {x y : A} (p : x \u2261 y) where\n tr-const : tr (const K) p \u2261 id\n tr-const = J (\u03bb x\u2081 p\u2081 \u2192 tr (const K) p\u2081 \u2261 id) refl p\n\n {-\n\u27e6U\u27e7-sound : \u2200 {{_ : FunExt}} {x y} \u2192 \u27e6U\u27e7 x y \u2192 x \u2261 y\n\u27e6U\u27e7-refl : \u2200 x \u2192 \u27e6U\u27e7 x x\n\n{-\n\u27e6El\u27e7-refl : \u2200 x \u2192 {!\u27e6El\u27e7 x x!}\n\u27e6El\u27e7-refl = {!!}\n-}\n\n\u27e6U\u27e7-sound \u27e6\ud835\udfd8\u1d41\u27e7 = refl\n\u27e6U\u27e7-sound \u27e6\ud835\udfd9\u1d41\u27e7 = refl\n\u27e6U\u27e7-sound \u27e6\ud835\udfda\u1d41\u27e7 = refl\n\u27e6U\u27e7-sound (u \u27e6\u00d7\u1d41\u27e7 u\u2081) = ap\u2082 _\u00d7\u1d41_ (\u27e6U\u27e7-sound u) (\u27e6U\u27e7-sound u\u2081)\n\u27e6U\u27e7-sound (u \u27e6\u228e\u1d41\u27e7 u\u2081) = ap\u2082 _\u228e\u1d41_ (\u27e6U\u27e7-sound u) (\u27e6U\u27e7-sound u\u2081)\n\u27e6U\u27e7-sound (\u27e6\u03a3\u1d41\u27e7 {u\u2080} {u\u2081} u {f\u2080} {f\u2081} f\u1d63) = apd\u2082 \u03a3\u1d41 (\u27e6U\u27e7-sound u) (tr-\u2192 El (const U) (\u27e6U\u27e7-sound u) f\u2080 \u2219 \u03bb= (\u03bb A \u2192 ap (\u03bb z \u2192 z (f\u2080 (tr El (! \u27e6U\u27e7-sound u) A))) (tr-const (\u27e6U\u27e7-sound u)) \u2219 \u27e6U\u27e7-sound (f\u1d63 {!!}))) -- (\u03bb= (\u03bb y \u2192 let foo = x\u1d63 {{!!}} {y} {!x\u1d63!} in {!tr-\u2192 El (const U) (\u27e6U\u27e7-sound u)!}))\n\n\u27e6U\u27e7-refl \ud835\udfd8\u1d41 = \u27e6\ud835\udfd8\u1d41\u27e7\n\u27e6U\u27e7-refl \ud835\udfd9\u1d41 = \u27e6\ud835\udfd9\u1d41\u27e7\n\u27e6U\u27e7-refl \ud835\udfda\u1d41 = \u27e6\ud835\udfda\u1d41\u27e7\n\u27e6U\u27e7-refl (x \u00d7\u1d41 x\u2081) = \u27e6U\u27e7-refl x \u27e6\u00d7\u1d41\u27e7 \u27e6U\u27e7-refl x\u2081\n\u27e6U\u27e7-refl (x \u228e\u1d41 x\u2081) = \u27e6U\u27e7-refl x \u27e6\u228e\u1d41\u27e7 \u27e6U\u27e7-refl x\u2081\n\u27e6U\u27e7-refl (\u03a3\u1d41 x f) = \u27e6\u03a3\u1d41\u27e7 (\u27e6U\u27e7-refl x) (\u03bb y \u2192 {!\u27e6U\u27e7-refl ?!})\n-}\n\ninfix 8 _^\u1d41_\n_^\u1d41_ : U \u2192 \u2115 \u2192 U\nt ^\u1d41 zero = t\nt ^\u1d41 suc n = t \u00d7\u1d41 t ^\u1d41 n\n\nmodule _ {\u2113} where\n\n explore : \u2200 t \u2192 Explore \u2113 (El t)\n explore \ud835\udfd8\u1d41 = \ud835\udfd8\u1d49\n explore \ud835\udfd9\u1d41 = \ud835\udfd9\u1d49\n explore \ud835\udfda\u1d41 = \ud835\udfda\u1d49\n explore (s \u00d7\u1d41 t) = explore s \u00d7\u1d49 explore t\n explore (s \u228e\u1d41 t) = explore s \u228e\u1d49 explore t\n explore (\u03a3\u1d41 t f) = explore\u03a3 (explore t) \u03bb {x} \u2192 explore (f x)\n\n exploreU-ind : \u2200 {p} t \u2192 ExploreInd p (explore t)\n exploreU-ind \ud835\udfd8\u1d41 = \ud835\udfd8\u2071\n exploreU-ind \ud835\udfd9\u1d41 = \ud835\udfd9\u2071\n exploreU-ind \ud835\udfda\u1d41 = \ud835\udfda\u2071\n exploreU-ind (s \u00d7\u1d41 t) = exploreU-ind s \u00d7\u2071 exploreU-ind t\n exploreU-ind (s \u228e\u1d41 t) = exploreU-ind s \u228e\u2071 exploreU-ind t\n exploreU-ind (\u03a3\u1d41 t f) = explore\u03a3-ind (exploreU-ind t) \u03bb {x} \u2192 exploreU-ind (f x)\n\nmodule _ {\u2113\u2080 \u2113\u2081 \u2113\u1d63} where\n \u27e6explore\u27e7 : \u2200 {t\u2080 t\u2081} (t : \u27e6U\u27e7 t\u2080 t\u2081) \u2192 \u27e6Explore\u27e7 {\u2113\u2080} {\u2113\u2081} \u2113\u1d63 (\u27e6El\u27e7 t) (explore t\u2080) (explore t\u2081)\n \u27e6explore\u27e7 \u27e6\ud835\udfd8\u1d41\u27e7 = \u27e6\ud835\udfd8\u1d49\u27e7 {\u2113\u2080} {\u2113\u2081} {\u2113\u1d63}\n \u27e6explore\u27e7 \u27e6\ud835\udfd9\u1d41\u27e7 = \u27e6\ud835\udfd9\u1d49\u27e7 {\u2113\u2080} {\u2113\u2081} {\u2113\u1d63} {_\u2261_} {refl}\n \u27e6explore\u27e7 \u27e6\ud835\udfda\u1d41\u27e7 = \u27e6\ud835\udfda\u1d49\u27e7 {\u2113\u2080} {\u2113\u2081} {\u2113\u1d63} {_\u2261_} {refl} {refl}\n \u27e6explore\u27e7 (t \u27e6\u00d7\u1d41\u27e7 t\u2081) = \u27e6explore\u00d7\u27e7 {\u2113\u2080} {\u2113\u2081} {\u2113\u1d63} (\u27e6explore\u27e7 t) (\u27e6explore\u27e7 t\u2081)\n \u27e6explore\u27e7 (t \u27e6\u228e\u1d41\u27e7 t\u2081) = \u27e6explore\u228e\u27e7 {\u2113\u2080} {\u2113\u2081} {\u2113\u1d63} (\u27e6explore\u27e7 t) (\u27e6explore\u27e7 t\u2081)\n \u27e6explore\u27e7 (\u27e6\u03a3\u1d41\u27e7 t f) = \u27e6explore\u03a3\u27e7 {\u2113\u2080} {\u2113\u2081} {\u2113\u1d63} (\u27e6explore\u27e7 t) (\u27e6explore\u27e7 \u2218 f)\n\nmodule _ (t : U) where\n private\n t\u1d49 : \u2200 {\u2113} \u2192 Explore \u2113 (El t)\n t\u1d49 = explore t\n t\u2071 : \u2200 {\u2113 p} \u2192 ExploreInd {\u2113} p t\u1d49\n t\u2071 = exploreU-ind t\n\n open FromExploreInd t\u2071 public hiding (\u27e6explore\u27e7)\n {-\n open From\u27e6Explore\u27e7 (\u03bb {\u2113\u2081} {\u2113\u2082} {\u2113\u1d63} \u2192 \u27e6explore\u27e7' {\u2113\u2081} {\u2113\u2082} {\u2113\u1d63} t) public\n -}\n\nadequate-sumU : \u2200 {{_ : UA}}{{_ : FunExt}} t \u2192 Adequate-sum (sum t)\nadequate-sumU \ud835\udfd8\u1d41 = \ud835\udfd8\u02e2-ok\nadequate-sumU \ud835\udfd9\u1d41 = \ud835\udfd9\u02e2-ok\nadequate-sumU \ud835\udfda\u1d41 = \ud835\udfda\u02e2-ok\nadequate-sumU (s \u00d7\u1d41 t) = adequate-sum\u03a3 (adequate-sumU s) (adequate-sumU t)\nadequate-sumU (s \u228e\u1d41 t) = adequate-sum\u228e (adequate-sumU s) (adequate-sumU t)\nadequate-sumU (\u03a3\u1d41 t f) = adequate-sum\u03a3 (adequate-sumU t) (\u03bb {x} \u2192 adequate-sumU (f x))\n\nmodule _ {\u2113} where\n lookupU : \u2200 t \u2192 Lookup {\u2113} (explore t)\n lookupU \ud835\udfd8\u1d41 = \ud835\udfd8\u02e1\n lookupU \ud835\udfd9\u1d41 = \ud835\udfd9\u02e1\n lookupU \ud835\udfda\u1d41 = \ud835\udfda\u02e1\n lookupU (s \u00d7\u1d41 t) = lookup\u00d7 {e\u1d2c = explore s} {e\u1d2e = explore t} (lookupU s) (lookupU t)\n lookupU (s \u228e\u1d41 t) = lookup\u228e {e\u1d2c = explore s} {e\u1d2e = explore t} (lookupU s) (lookupU t)\n lookupU (\u03a3\u1d41 t f) = lookup\u03a3 {e\u1d2c = explore t} {e\u1d2e = \u03bb {x} \u2192 explore (f x)} (lookupU t) (\u03bb {x} \u2192 lookupU (f x))\n\n focusU : \u2200 t \u2192 Focus {\u2113} (explore t)\n focusU \ud835\udfd8\u1d41 = \ud835\udfd8\u1da0\n focusU \ud835\udfd9\u1d41 = \ud835\udfd9\u1da0\n focusU \ud835\udfda\u1d41 = \ud835\udfda\u1da0\n focusU (s \u00d7\u1d41 t) = focus\u00d7 {e\u1d2c = explore s} {e\u1d2e = explore t} (focusU s) (focusU t)\n focusU (s \u228e\u1d41 t) = focus\u228e {e\u1d2c = explore s} {e\u1d2e = explore t} (focusU s) (focusU t)\n focusU (\u03a3\u1d41 t f) = focus\u03a3 {e\u1d2c = explore t} {e\u1d2e = \u03bb {x} \u2192 explore (f x)} (focusU t) (\u03bb {x} \u2192 focusU (f x))\n\n \u03a3\u1d49U : \u2200 A {\u2113} \u2192 (El A \u2192 \u2605_ \u2113) \u2192 \u2605_ \u2113\n \u03a3\u1d49U = \u03bb A \u2192 \u03a3\u1d49 (explore A)\n \u03a0\u1d49U : \u2200 A {\u2113} \u2192 (El A \u2192 \u2605_ \u2113) \u2192 \u2605_ \u2113\n \u03a0\u1d49U = \u03bb A \u2192 \u03a0\u1d49 (explore A)\n\n module _ {{_ : UA}}{{_ : FunExt}} where\n \u03a3\u1d49U-ok : \u2200 t \u2192 Adequate-\u03a3 {\u2113} (\u03a3\u1d49U t)\n \u03a3\u1d49U-ok \ud835\udfd8\u1d41 = \u03a3\u1d49\ud835\udfd8-ok\n \u03a3\u1d49U-ok \ud835\udfd9\u1d41 = \u03a3\u1d49\ud835\udfd9-ok\n \u03a3\u1d49U-ok \ud835\udfda\u1d41 = \u03a3\u1d49\ud835\udfda-ok\n \u03a3\u1d49U-ok (t \u00d7\u1d41 u) = \u03a3\u1d49\u00d7-ok {e\u1d2c = explore t} {e\u1d2e = explore u} (\u03a3\u1d49U-ok t) (\u03a3\u1d49U-ok u)\n \u03a3\u1d49U-ok (t \u228e\u1d41 u) = \u03a3\u1d49\u228e-ok {e\u1d2c = explore t} {e\u1d2e = explore u} (\u03a3\u1d49U-ok t) (\u03a3\u1d49U-ok u)\n \u03a3\u1d49U-ok (\u03a3\u1d41 t u) = \u03a3\u1d49\u03a3-ok {e\u1d2c = explore t} {e\u1d2e = \u03bb {x} \u2192 explore (u x)} (\u03a3\u1d49U-ok t) (\u03bb {x} \u2192 \u03a3\u1d49U-ok (u x))\n\n \u03a0\u1d49U-ok : \u2200 t \u2192 Adequate-\u03a0 {\u2113} (\u03a0\u1d49U t)\n \u03a0\u1d49U-ok \ud835\udfd8\u1d41 = \u03a0\u1d49\ud835\udfd8-ok\n \u03a0\u1d49U-ok \ud835\udfd9\u1d41 = \u03a0\u1d49\ud835\udfd9-ok\n \u03a0\u1d49U-ok \ud835\udfda\u1d41 = \u03a0\u1d49\ud835\udfda-ok\n \u03a0\u1d49U-ok (t \u00d7\u1d41 u) = \u03a0\u1d49\u00d7-ok {e\u1d2c = explore t} {e\u1d2e = explore u} (\u03a0\u1d49U-ok t) (\u03a0\u1d49U-ok u)\n \u03a0\u1d49U-ok (t \u228e\u1d41 u) = \u03a0\u1d49\u228e-ok {e\u1d2c = explore t} {e\u1d2e = explore u} (\u03a0\u1d49U-ok t) (\u03a0\u1d49U-ok u)\n \u03a0\u1d49U-ok (\u03a3\u1d41 t u) = \u03a0\u1d49\u03a3-ok {e\u1d2c = explore t} {e\u1d2e = \u03bb {x} \u2192 explore (u x)} (\u03a0\u1d49U-ok t) (\u03bb {x} \u2192 \u03a0\u1d49U-ok (u x))\n\n{-\nmodule _ (t : U) {{_ : UA}} {{_ : FunExt}} where\n open FromAdequate-\u03a3\u1d49 t (\u03a3\u1d49U-ok t) public\n open FromAdequate-\u03a0\u1d49 t (\u03a0\u1d49U-ok t) public\n-}\n\nmodule _ (A : U) (P : El A \u2192 \u2605\u2080) where\n Dec-\u03a3 : \u03a0 (El A) (Dec \u2218 P) \u2192 Dec (\u03a3 (El A) P)\n Dec-\u03a3 = map-Dec (unfocus A) (focusU A) \u2218 lift-Dec A P \u2218 reify A\n\n-- See Explore.Fin for an example of the use of this module\nmodule Isomorphism {A : \u2605\u2080} u (e : El u \u2243 A) where\n open Explore.Isomorphism e\n\n module _ {\u2113} where\n iso\u1d49 : Explore \u2113 A\n iso\u1d49 = explore-iso (explore u)\n\n module _ {p} where\n iso\u2071 : ExploreInd p iso\u1d49\n iso\u2071 = explore-iso-ind (exploreU-ind u)\n\n module _ {\u2113} where\n {-\n iso\u02e1 : Lookup {\u2113} iso\u1d49\n iso\u02e1 = lookup-iso {\u2113} {exploreU u} (lookupU u)\n\n iso\u1da0 : Focus {\u2113} iso\u1d49\n iso\u1da0 = focus-iso {\u2113} {exploreU u} (focusU u)\n -}\n\n iso\u02b3 : Reify {\u2113} iso\u1d49\n iso\u02b3 = FromExploreInd-iso.reify (exploreU-ind u)\n\n iso\u1d58 : Unfocus {\u2113} iso\u1d49\n iso\u1d58 = FromExploreInd-iso.unfocus (exploreU-ind u)\n\n iso\u02e2 : Sum A\n iso\u02e2 = sum-iso (sum u)\n\n iso\u1d56 : Product A\n iso\u1d56 = product-iso (sum u)\n\n module _ {{_ : UA}}{{_ : FunExt}} where\n iso\u02e2-ok : Adequate-sum iso\u02e2\n iso\u02e2-ok = sum-iso-ok (adequate-sumU u)\n\n open Adequate-sum\u2080 iso\u02e2-ok iso\u02e2-ok public renaming (sumStableUnder to iso\u02e2-stableUnder)\n\nFin\u1d41 : \u2115 \u2192 U\nFin\u1d41 zero = \ud835\udfd8\u1d41\nFin\u1d41 (suc n) = \ud835\udfd9\u1d41 \u228e\u1d41 Fin\u1d41 n\n\nFin\u1d41' : \u2115 \u2192 U\nFin\u1d41' zero = \ud835\udfd8\u1d41\nFin\u1d41' (suc zero) = \ud835\udfd9\u1d41\nFin\u1d41' (suc (suc n)) = \ud835\udfd9\u1d41 \u228e\u1d41 Fin\u1d41' (suc n)\n\nFin\u1d41-Fin : \u2200 n \u2192 El (Fin\u1d41 n) \u2243 Fin n\nFin\u1d41-Fin zero = \u2243-! Fin0\u2243\ud835\udfd8\nFin\u1d41-Fin (suc n) = \u228e\u2243 \u2243-refl (Fin\u1d41-Fin n) \u2243-\u2219 \u2243-! Fin\u2218suc\u2243\ud835\udfd9\u228eFin\n\nFin\u1d41'-Fin : \u2200 n \u2192 El (Fin\u1d41' n) \u2243 Fin n\nFin\u1d41'-Fin zero = \u2243-! Fin0\u2243\ud835\udfd8\nFin\u1d41'-Fin (suc zero) = \u2243-! Fin1\u2243\ud835\udfd9\nFin\u1d41'-Fin (suc (suc n)) = \u228e\u2243 \u2243-refl (Fin\u1d41'-Fin (suc n)) \u2243-\u2219 \u2243-! Fin\u2218suc\u2243\ud835\udfd9\u228eFin\n\n\u03a0\u1d41 : (u : U) (v : El u \u2192 U) \u2192 U\n\u03a0\u1d41 \ud835\udfd8\u1d41 v = \ud835\udfd9\u1d41\n\u03a0\u1d41 \ud835\udfd9\u1d41 v = v _\n\u03a0\u1d41 \ud835\udfda\u1d41 v = v 0\u2082 \u00d7\u1d41 v 1\u2082\n\u03a0\u1d41 (u \u00d7\u1d41 u\u2081) v = \u03a0\u1d41 u \u03bb x \u2192 \u03a0\u1d41 u\u2081 \u03bb y \u2192 v (x , y)\n\u03a0\u1d41 (u \u228e\u1d41 u\u2081) v = (\u03a0\u1d41 u (v \u2218 inl)) \u00d7\u1d41 (\u03a0\u1d41 u\u2081 (v \u2218 inr))\n\u03a0\u1d41 (\u03a3\u1d41 u f) v = \u03a0\u1d41 u \u03bb x \u2192 \u03a0\u1d41 (f x) (v \u2218 _,_ x)\n\n_\u2192\u1d41_ : (u : U) (v : U) \u2192 U\nu \u2192\u1d41 v = \u03a0\u1d41 u (const v)\n\n{-\n\ud835\udfdb\u1d41 : U\n\ud835\udfdb\u1d41 = \ud835\udfd9\u1d41 \u228e\u1d41 \ud835\udfda\u1d41\n\nlist22 = list (\ud835\udfda\u1d41 \u2192\u1d41 \ud835\udfda\u1d41)\nlist33 = list (\ud835\udfdb\u1d41 \u2192\u1d41 \ud835\udfdb\u1d41)\n-}\n\nmodule _ {{_ : UA}}{{_ : FunExt}} where\n \u03a0\u1d41-\u03a0 : \u2200 u v \u2192 El (\u03a0\u1d41 u v) \u2261 \u03a0 (El u) (El \u2218 v)\n \u03a0\u1d41-\u03a0 \ud835\udfd8\u1d41 v = ! \u03a0\ud835\udfd8-uniq\u2080 _\n \u03a0\u1d41-\u03a0 \ud835\udfd9\u1d41 v = ! \u03a0\ud835\udfd9-uniq _\n \u03a0\u1d41-\u03a0 \ud835\udfda\u1d41 v = ! \u03a0\ud835\udfda-\u00d7\n \u03a0\u1d41-\u03a0 (u \u00d7\u1d41 u\u2081) v = \u03a0\u1d41-\u03a0 u (\u03bb x \u2192 \u03a0\u1d41 u\u2081 (v \u2218 _,_ x)) \u2219 \u03a0=\u2032 _ (\u03bb _ \u2192 \u03a0\u1d41-\u03a0 u\u2081 _) \u2219 ! \u03a0\u03a3-curry\n \u03a0\u1d41-\u03a0 (u \u228e\u1d41 u\u2081) v = \u00d7= (\u03a0\u1d41-\u03a0 u (v \u2218 inl)) (\u03a0\u1d41-\u03a0 u\u2081 (v \u2218 inr)) \u2219 ! dist-\u00d7-\u03a0\n \u03a0\u1d41-\u03a0 (\u03a3\u1d41 u f) v = \u03a0\u1d41-\u03a0 u (\u03bb x \u2192 \u03a0\u1d41 (f _) (v \u2218 _,_ x)) \u2219 \u03a0=\u2032 _ (\u03bb _ \u2192 \u03a0\u1d41-\u03a0 (f _) _) \u2219 ! \u03a0\u03a3-curry\n\n \u2192\u1d41-\u2192 : \u2200 u v \u2192 El (u \u2192\u1d41 v) \u2261 (El u \u2192 El v)\n \u2192\u1d41-\u2192 u v = \u03a0\u1d41-\u03a0 u (const v)\n\n \u03a0\u1d41\u2192\u03a0 : \u2200 u v \u2192 El (\u03a0\u1d41 u v) \u2192 \u03a0 (El u) (El \u2218 v)\n \u03a0\u1d41\u2192\u03a0 \ud835\udfd8\u1d41 v x\u2082 ()\n \u03a0\u1d41\u2192\u03a0 \ud835\udfd9\u1d41 v x\u2082 x\u2083 = x\u2082\n \u03a0\u1d41\u2192\u03a0 \ud835\udfda\u1d41 v (x , y) 0\u2082 = x\n \u03a0\u1d41\u2192\u03a0 \ud835\udfda\u1d41 v (x , y) 1\u2082 = y\n \u03a0\u1d41\u2192\u03a0 (u \u00d7\u1d41 u\u2081) v x (z , t) = \u03a0\u1d41\u2192\u03a0 u\u2081 (v \u2218 _,_ z) (\u03a0\u1d41\u2192\u03a0 u (\u03bb x \u2192 \u03a0\u1d41 u\u2081 (v \u2218 _,_ x)) x z) t\n \u03a0\u1d41\u2192\u03a0 (u \u228e\u1d41 _) v (x , _) (inl y) = \u03a0\u1d41\u2192\u03a0 u (v \u2218 inl) x y\n \u03a0\u1d41\u2192\u03a0 (_ \u228e\u1d41 u) v (_ , x) (inr y) = \u03a0\u1d41\u2192\u03a0 u (v \u2218 inr) x y\n \u03a0\u1d41\u2192\u03a0 (\u03a3\u1d41 u f) v x (y , z) = \u03a0\u1d41\u2192\u03a0 (f _) (v \u2218 _,_ y) (\u03a0\u1d41\u2192\u03a0 u (\u03bb x \u2192 \u03a0\u1d41 (f _) (v \u2218 _,_ x)) x y) z\n\n \u2192\u1d41\u2192\u2192 : \u2200 u v \u2192 El (u \u2192\u1d41 v) \u2192 (El u \u2192 El v)\n \u2192\u1d41\u2192\u2192 u v = \u03a0\u1d41\u2192\u03a0 u (const v)\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"2e98871b50826699f6f16a2718f8f2361a559c4d","subject":"Write down real step-indexed logical relation for untyped terms","message":"Write down real step-indexed logical relation for untyped terms\n\nSadly, Agda does not recognize this is terminating, because recursing on k with\nk < n is not a structurally recursive call on a subterm. SAD!\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/ANormalUntyped.agda","new_file":"Thesis\/ANormalUntyped.agda","new_contents":"module Thesis.ANormalUntyped where\n\nopen import Data.Empty\nopen import Data.Product\nopen import Data.Nat.Base\nimport Data.Integer.Base as I\nopen I using (\u2124)\nopen import Data.Integer.Base using (\u2124)\nopen import Relation.Binary.PropositionalEquality\n\n{- Typed deBruijn indexes for untyped languages. -}\n\n-- Using a record gives an eta rule saying that all types are equal.\nrecord Type : Set where\n constructor Uni\n\nrecord DType : Set where\n constructor DUni\n\nopen import Base.Syntax.Context Type public\nimport Base.Syntax.Context DType as DC\n\ndata Term (\u0393 : Context) : Set where\n var : (x : Var \u0393 Uni) \u2192\n Term \u0393\n lett : (f : Var \u0393 Uni) \u2192 (x : Var \u0393 Uni) \u2192 Term (Uni \u2022 \u0393) \u2192 Term \u0393\n\n\u0394\u0394 : Context \u2192 DC.Context\n\u0394\u0394 \u2205 = \u2205\n\u0394\u0394 (\u03c4 \u2022 \u0393) = DUni \u2022 \u0394\u0394 \u0393\n\nderive-dvar : \u2200 {\u0394} \u2192 (x : Var \u0394 Uni) \u2192 DC.Var (\u0394\u0394 \u0394) DUni\nderive-dvar this = DC.this\nderive-dvar (that x) = DC.that (derive-dvar x)\n\ndata DTerm : (\u0394 : Context) \u2192 Set where\n dvar : \u2200 {\u0394} (x : DC.Var (\u0394\u0394 \u0394) DUni) \u2192\n DTerm \u0394\n dlett : \u2200 {\u0394} \u2192\n (f : Var \u0394 Uni) \u2192\n (x : Var \u0394 Uni) \u2192\n (t : Term (Uni \u2022 \u0394)) \u2192\n (df : DC.Var (\u0394\u0394 \u0394) DUni) \u2192\n (dx : DC.Var (\u0394\u0394 \u0394) DUni) \u2192\n (dt : DTerm (Uni \u2022 \u0394)) \u2192\n DTerm \u0394\n\nderive-dterm : \u2200 {\u0394} \u2192 (t : Term \u0394) \u2192 DTerm \u0394\nderive-dterm (var x) = dvar (derive-dvar x)\nderive-dterm (lett f x t) =\n dlett f x t (derive-dvar f) (derive-dvar x) (derive-dterm t)\n\n{-\nderiveC \u0394 (lett f x t) = dlett df x dx\n-}\n\n-- data \u0394CTerm (\u0393 : Context) (\u03c4 : Type) (\u0394 : Context) : Set where\n-- data \u0394CTerm (\u0393 : Context) (\u03c4 : Type) (\u0394 : Context) : Set where\n-- cvar : (x : Var \u0393 \u03c4) \u0394 \u2192\n-- \u0394CTerm \u0393 \u03c4 \u0394\n-- clett : \u2200 {\u03c3 \u03c4\u2081 \u03ba} \u2192 (f : Var \u0393 (\u03c3 \u21d2 \u03c4\u2081)) \u2192 (x : Var \u0393 \u03c3) \u2192\n-- \u0394CTerm (\u03c4\u2081 \u2022 \u0393) \u03c4 (? \u2022 \u0394) \u2192\n-- \u0394CTerm \u0393 \u03c4 \u0394\n\nweaken-term : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u2192\n Term \u0393\u2082\nweaken-term \u0393\u2081\u227c\u0393\u2082 (var x) = var (weaken-var \u0393\u2081\u227c\u0393\u2082 x)\nweaken-term \u0393\u2081\u227c\u0393\u2082 (lett f x t) = lett (weaken-var \u0393\u2081\u227c\u0393\u2082 f) (weaken-var \u0393\u2081\u227c\u0393\u2082 x) (weaken-term (keep _ \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\n-- I don't necessarily recommend having a separate syntactic category for\n-- functions, but we should prove a fundamental lemma for them too, somehow.\n-- I'll probably end up with some ANF allowing lambdas to do the semantics.\ndata Fun (\u0393 : Context) : Set where\n term : Term \u0393 \u2192 Fun \u0393\n abs : \u2200 {\u03c3} \u2192 Fun (\u03c3 \u2022 \u0393) \u2192 Fun \u0393\n\ndata DFun (\u0394 : Context) : Set where\n dterm : DTerm \u0394 \u2192 DFun \u0394\n dabs : DFun (Uni \u2022 \u0394) \u2192 DFun \u0394\n\nderive-dfun : \u2200 {\u0394} \u2192 (t : Fun \u0394) \u2192 DFun \u0394\nderive-dfun (term t) = dterm (derive-dterm t)\nderive-dfun (abs f) = dabs (derive-dfun f)\n\nweaken-fun : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Fun \u0393\u2081 \u2192\n Fun \u0393\u2082\nweaken-fun \u0393\u2081\u227c\u0393\u2082 (term x) = term (weaken-term \u0393\u2081\u227c\u0393\u2082 x)\nweaken-fun \u0393\u2081\u227c\u0393\u2082 (abs f) = abs (weaken-fun (keep _ \u2022 \u0393\u2081\u227c\u0393\u2082) f)\n\ndata Val : Type \u2192 Set\ndata DVal : DType \u2192 Set\n-- data Val (\u03c4 : Type) : Set\n\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\nimport Base.Denotation.Environment DType DVal as D\n\n-- data Val (\u03c4 : Type) where\ndata Val where\n closure : \u2200 {\u0393} \u2192 (t : Fun (Uni \u2022 \u0393)) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val Uni\n intV : \u2200 (n : \u2115) \u2192 Val Uni\n\ndata DVal where\n dclosure : \u2200 {\u0393} \u2192 (dt : DFun (Uni \u2022 \u0393)) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 (d\u03c1 : D.\u27e6 \u0394\u0394 \u0393 \u27e7Context) \u2192 DVal DUni\n dintV : \u2200 (n : \u2124) \u2192 DVal DUni\n\nCh\u0394 : \u2200 (\u0394 : Context) \u2192 Set\nCh\u0394 \u0394 = D.\u27e6 \u0394\u0394 \u0394 \u27e7Context\n\n-- \u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 \u27e6 \u03c4 \u27e7Type\n-- \u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7Var \u03c1\n-- \u27e6 lett f x t \u27e7Term \u03c1 = \u27e6 t \u27e7Term (\u27e6 f \u27e7Var \u03c1 (\u27e6 x \u27e7Var \u03c1) \u2022 \u03c1)\n\n-- XXX separate syntax is a bit dangerous. Also, do I want to be so accurate relative to the original model?\ndata _F\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) : Fun \u0393 \u2192 \u2115 \u2192 Val Uni \u2192 Set where\n abs : \u2200 {t : Fun (Uni \u2022 \u0393)} \u2192\n \u03c1 F\u22a2 abs t \u2193[ 0 ] closure t \u03c1\n\ndata _\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) : Term \u0393 \u2192 \u2115 \u2192 Val Uni \u2192 Set where\n var : \u2200 (x : Var \u0393 Uni) \u2192\n \u03c1 \u22a2 var x \u2193[ 0 ] (\u27e6 x \u27e7Var \u03c1)\n lett : \u2200 n1 n2 {\u0393' \u03c1\u2032 v1 v2 v3} {f x t t'} \u2192\n \u03c1 \u22a2 var f \u2193[ 0 ] closure {\u0393'} t' \u03c1\u2032 \u2192\n \u03c1 \u22a2 var x \u2193[ 0 ] v1 \u2192\n (v1 \u2022 \u03c1\u2032) F\u22a2 t' \u2193[ n1 ] v2 \u2192\n (v2 \u2022 \u03c1) \u22a2 t \u2193[ n2 ] v3 \u2192\n \u03c1 \u22a2 lett f x t \u2193[ suc (suc (n1 + n2)) ] v3\n -- lit : \u2200 n \u2192\n -- \u03c1 \u22a2 const (lit n) \u2193[ 0 ] intV n\n\n-- data _D_\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : D.\u27e6 \u0394\u0394 \u0393 \u27e7Context) : DTerm \u0393 \u2192 \u2115 \u2192 DVal DUni \u2192 Set where\n-- dvar : \u2200 (x : DC.Var (\u0394\u0394 \u0393) DUni) \u2192\n-- \u03c1 D d\u03c1 \u22a2 dvar x \u2193[ 0 ] (D.\u27e6 x \u27e7Var d\u03c1)\n-- dlett : \u2200 n1 n2 n3 n4 {\u0393' \u03c1\u2032 \u03c1'' d\u03c1' v1 v2 v3 dv1 dv2 dv3} {f x t df dx dt t' dt'} \u2192\n-- \u03c1 \u22a2 var f \u2193[ 0 ] closure {\u0393'} t' \u03c1\u2032 \u2192\n-- \u03c1 \u22a2 var x \u2193[ 0 ] v1 \u2192\n-- (v1 \u2022 \u03c1\u2032) F\u22a2 t' \u2193[ n1 ] v2 \u2192\n-- (v2 \u2022 \u03c1) \u22a2 t \u2193[ n2 ] v3 \u2192\n-- -- With a valid input \u03c1' and \u03c1'' coincide? Varies among plausible validity\n-- -- definitions.\n-- \u03c1 D d\u03c1 \u22a2 dvar df \u2193[ 0 ] dclosure {\u0393'} dt' \u03c1'' d\u03c1' \u2192\n-- \u03c1 D d\u03c1 \u22a2 dvar dx \u2193[ 0 ] dv1 \u2192\n-- (v1 \u2022 \u03c1'') D (dv1 \u2022 d\u03c1') F\u22a2 dt' \u2193[ n3 ] dv2 \u2192\n-- (v2 \u2022 \u03c1) D (dv2 \u2022 d\u03c1) \u22a2 dt \u2193[ n4 ] dv3 \u2192\n-- \u03c1 D d\u03c1 \u22a2 dlett f x t df dx dt \u2193[ suc (suc (n1 + n2)) ] dv3\n\n\n-- Do I need to damn count steps here? No.\n\ndata _D_F\u22a2_\u2193_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) : DFun \u0393 \u2192 DVal DUni \u2192 Set where\n dabs : \u2200 {t : DFun (Uni \u2022 \u0393)} \u2192\n \u03c1 D d\u03c1 F\u22a2 dabs t \u2193 dclosure t \u03c1 d\u03c1\n\ndata _D_\u22a2_\u2193_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) : DTerm \u0393 \u2192 DVal DUni \u2192 Set where\n dvar : \u2200 (x : DC.Var (\u0394\u0394 \u0393) DUni) \u2192\n \u03c1 D d\u03c1 \u22a2 dvar x \u2193 (D.\u27e6 x \u27e7Var d\u03c1)\n dlett : \u2200 n1 n2 {\u0393' \u03c1\u2032 \u03c1'' d\u03c1' v1 v2 v3 dv1 dv2 dv3} {f x t df dx dt t' dt'} \u2192\n \u03c1 \u22a2 var f \u2193[ 0 ] closure {\u0393'} t' \u03c1\u2032 \u2192\n \u03c1 \u22a2 var x \u2193[ 0 ] v1 \u2192\n (v1 \u2022 \u03c1\u2032) F\u22a2 t' \u2193[ n1 ] v2 \u2192\n (v2 \u2022 \u03c1) \u22a2 t \u2193[ n2 ] v3 \u2192\n -- With a valid input \u03c1' and \u03c1'' coincide? Varies among plausible validity\n -- definitions.\n \u03c1 D d\u03c1 \u22a2 dvar df \u2193 dclosure {\u0393'} dt' \u03c1'' d\u03c1' \u2192\n \u03c1 D d\u03c1 \u22a2 dvar dx \u2193 dv1 \u2192\n (v1 \u2022 \u03c1'') D (dv1 \u2022 d\u03c1') F\u22a2 dt' \u2193 dv2 \u2192\n (v2 \u2022 \u03c1) D (dv2 \u2022 d\u03c1) \u22a2 dt \u2193 dv3 \u2192\n \u03c1 D d\u03c1 \u22a2 dlett f x t df dx dt \u2193 dv3\n\n{-# TERMINATING #-} -- Dear lord. Why on Earth.\nmutual\n -- Single context? Yeah, good, that's what we want in the end...\n -- Though we might want more flexibility till when we have replacement\n -- changes.\n rrelT3 : \u2200 {\u0393} (t1 : Fun \u0393) (dt : DFun \u0393) (t2 : Fun \u0393) (\u03c11 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) (\u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\n rrelT3 t1 dt t2 \u03c11 d\u03c1 \u03c12 k =\n (v1 v2 : Val Uni) \u2192\n \u2200 j n2 (j>=_ : ErrVal \u2192 (Val Uni \u2192 ErrVal) \u2192 ErrVal\n-- Done v >>= f = f v\n-- Error >>= f = Error\n-- TimeOut >>= f = TimeOut\n\n-- Res : Set\n-- Res = \u2115 \u2192 ErrVal\n\n-- -- eval-fun : \u2200 {\u0393} \u2192 Fun \u0393 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res\n-- -- eval-term : \u2200 {\u0393} \u2192 Term \u0393 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res\n\n-- -- apply : Val Uni \u2192 Val Uni \u2192 Res\n-- -- apply (closure f \u03c1) a n = eval-fun f (a \u2022 \u03c1) n\n-- -- apply (intV _) a n = Error\n\n-- -- eval-term t \u03c1 zero = TimeOut\n-- -- eval-term (var x) \u03c1 (suc n) = Done (\u27e6 x \u27e7Var \u03c1)\n-- -- eval-term (lett f x t) \u03c1 (suc n) = apply (\u27e6 f \u27e7Var \u03c1) (\u27e6 x \u27e7Var \u03c1) n >>= (\u03bb v \u2192 eval-term t (v \u2022 \u03c1) n)\n\n-- -- eval-fun (term t) \u03c1 n = eval-term t \u03c1 n\n-- -- eval-fun (abs f) \u03c1 n = Done (closure f \u03c1)\n\n-- -- -- Erasure from typed to untyped values.\n-- -- import Thesis.ANormalBigStep as T\n\n-- -- erase-type : T.Type \u2192 Type\n-- -- erase-type _ = Uni\n\n-- -- erase-val : \u2200 {\u03c4} \u2192 T.Val \u03c4 \u2192 Val (erase-type \u03c4)\n\n-- -- erase-errVal : \u2200 {\u03c4} \u2192 T.ErrVal \u03c4 \u2192 ErrVal\n-- -- erase-errVal (T.Done v) = Done (erase-val v)\n-- -- erase-errVal T.Error = Error\n-- -- erase-errVal T.TimeOut = TimeOut\n\n-- -- erase-res : \u2200 {\u03c4} \u2192 T.Res \u03c4 \u2192 Res\n-- -- erase-res r n = erase-errVal (r n)\n\n-- -- erase-ctx : T.Context \u2192 Context\n-- -- erase-ctx \u2205 = \u2205\n-- -- erase-ctx (\u03c4 \u2022 \u0393) = erase-type \u03c4 \u2022 (erase-ctx \u0393)\n\n-- -- erase-env : \u2200 {\u0393} \u2192 T.Op.\u27e6 \u0393 \u27e7Context \u2192 \u27e6 erase-ctx \u0393 \u27e7Context\n-- -- erase-env \u2205 = \u2205\n-- -- erase-env (v \u2022 \u03c1) = erase-val v \u2022 erase-env \u03c1\n\n-- -- erase-var : \u2200 {\u0393 \u03c4} \u2192 T.Var \u0393 \u03c4 \u2192 Var (erase-ctx \u0393) (erase-type \u03c4)\n-- -- erase-var T.this = this\n-- -- erase-var (T.that x) = that (erase-var x)\n\n-- -- erase-term : \u2200 {\u0393 \u03c4} \u2192 T.Term \u0393 \u03c4 \u2192 Term (erase-ctx \u0393)\n-- -- erase-term (T.var x) = var (erase-var x)\n-- -- erase-term (T.lett f x t) = lett (erase-var f) (erase-var x) (erase-term t)\n\n-- -- erase-fun : \u2200 {\u0393 \u03c4} \u2192 T.Fun \u0393 \u03c4 \u2192 Fun (erase-ctx \u0393)\n-- -- erase-fun (T.term x) = term (erase-term x)\n-- -- erase-fun (T.abs f) = abs (erase-fun f)\n\n-- -- erase-val (T.closure t \u03c1) = closure (erase-fun t) (erase-env \u03c1)\n-- -- erase-val (T.intV n) = intV n\n\n-- -- -- Different erasures commute.\n-- -- erasure-commute-var : \u2200 {\u0393 \u03c4} (x : T.Var \u0393 \u03c4) \u03c1 \u2192\n-- -- erase-val (T.Op.\u27e6 x \u27e7Var \u03c1) \u2261 \u27e6 erase-var x \u27e7Var (erase-env \u03c1)\n-- -- erasure-commute-var T.this (v \u2022 \u03c1) = refl\n-- -- erasure-commute-var (T.that x) (v \u2022 \u03c1) = erasure-commute-var x \u03c1\n\n-- -- erase-bind : \u2200 {\u03c3 \u03c4 \u0393} a (t : T.Term (\u03c3 \u2022 \u0393) \u03c4) \u03c1 n \u2192 erase-errVal (a T.>>= (\u03bb v \u2192 T.eval-term t (v \u2022 \u03c1) n)) \u2261 erase-errVal a >>= (\u03bb v \u2192 eval-term (erase-term t) (v \u2022 erase-env \u03c1) n)\n\n-- -- erasure-commute-fun : \u2200 {\u0393 \u03c4} (t : T.Fun \u0393 \u03c4) \u03c1 n \u2192\n-- -- erase-errVal (T.eval-fun t \u03c1 n) \u2261 eval-fun (erase-fun t) (erase-env \u03c1) n\n\n-- -- erasure-commute-apply : \u2200 {\u03c3 \u03c4} (f : T.Val (\u03c3 T.\u21d2 \u03c4)) a n \u2192 erase-errVal (T.apply f a n) \u2261 apply (erase-val f) (erase-val a) n\n-- -- erasure-commute-apply {\u03c3} (T.closure t \u03c1) a n = erasure-commute-fun t (a \u2022 \u03c1) n\n\n-- -- erasure-commute-term : \u2200 {\u0393 \u03c4} (t : T.Term \u0393 \u03c4) \u03c1 n \u2192\n-- -- erase-errVal (T.eval-term t \u03c1 n) \u2261 eval-term (erase-term t) (erase-env \u03c1) n\n\n-- -- erasure-commute-fun (T.term t) \u03c1 n = erasure-commute-term t \u03c1 n\n-- -- erasure-commute-fun (T.abs t) \u03c1 n = refl\n\n-- -- erasure-commute-term t \u03c1 zero = refl\n-- -- erasure-commute-term (T.var x) \u03c1 (\u2115.suc n) = cong Done (erasure-commute-var x \u03c1)\n-- -- erasure-commute-term (T.lett f x t) \u03c1 (\u2115.suc n) rewrite erase-bind (T.apply (T.Op.\u27e6 f \u27e7Var \u03c1) (T.Op.\u27e6 x \u27e7Var \u03c1) n) t \u03c1 n | erasure-commute-apply (T.Op.\u27e6 f \u27e7Var \u03c1) (T.Op.\u27e6 x \u27e7Var \u03c1) n | erasure-commute-var f \u03c1 | erasure-commute-var x \u03c1 = refl\n\n-- -- erase-bind (T.Done v) t \u03c1 n = erasure-commute-term t (v \u2022 \u03c1) n\n-- -- erase-bind T.Error t \u03c1 n = refl\n-- -- erase-bind T.TimeOut t \u03c1 n = refl\n","old_contents":"module Thesis.ANormalUntyped where\n\nopen import Data.Nat.Base\nopen import Data.Integer.Base\nopen import Relation.Binary.PropositionalEquality\n\n{- Typed deBruijn indexes for untyped languages. -}\n\n-- Using a record gives an eta rule saying that all types are equal.\nrecord Type : Set where\n constructor Uni\n\nopen import Base.Syntax.Context Type public\ndata Term (\u0393 : Context) : Set where\n var : (x : Var \u0393 Uni) \u2192\n Term \u0393\n lett : (f : Var \u0393 Uni) \u2192 (x : Var \u0393 Uni) \u2192 Term (Uni \u2022 \u0393) \u2192 Term \u0393\n\n{-\nderiveC \u0394 (lett f x t) = dlett df x dx\n-}\n\n-- data \u0394CTerm (\u0393 : Context) (\u03c4 : Type) (\u0394 : Context) : Set where\n-- data \u0394CTerm (\u0393 : Context) (\u03c4 : Type) (\u0394 : Context) : Set where\n-- cvar : (x : Var \u0393 \u03c4) \u0394 \u2192\n-- \u0394CTerm \u0393 \u03c4 \u0394\n-- clett : \u2200 {\u03c3 \u03c4\u2081 \u03ba} \u2192 (f : Var \u0393 (\u03c3 \u21d2 \u03c4\u2081)) \u2192 (x : Var \u0393 \u03c3) \u2192\n-- \u0394CTerm (\u03c4\u2081 \u2022 \u0393) \u03c4 (? \u2022 \u0394) \u2192\n-- \u0394CTerm \u0393 \u03c4 \u0394\n\nweaken-term : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u2192\n Term \u0393\u2082\nweaken-term \u0393\u2081\u227c\u0393\u2082 (var x) = var (weaken-var \u0393\u2081\u227c\u0393\u2082 x)\nweaken-term \u0393\u2081\u227c\u0393\u2082 (lett f x t) = lett (weaken-var \u0393\u2081\u227c\u0393\u2082 f) (weaken-var \u0393\u2081\u227c\u0393\u2082 x) (weaken-term (keep _ \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\ndata Fun (\u0393 : Context) : Set where\n term : Term \u0393 \u2192 Fun \u0393\n abs : \u2200 {\u03c3} \u2192 Fun (\u03c3 \u2022 \u0393) \u2192 Fun \u0393\n\nweaken-fun : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Fun \u0393\u2081 \u2192\n Fun \u0393\u2082\nweaken-fun \u0393\u2081\u227c\u0393\u2082 (term x) = term (weaken-term \u0393\u2081\u227c\u0393\u2082 x)\nweaken-fun \u0393\u2081\u227c\u0393\u2082 (abs f) = abs (weaken-fun (keep _ \u2022 \u0393\u2081\u227c\u0393\u2082) f)\n\ndata Val : Type \u2192 Set\n-- data Val (\u03c4 : Type) : Set\n\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\n-- data Val (\u03c4 : Type) where\ndata Val where\n closure : \u2200 {\u0393} \u2192 (t : Fun (Uni \u2022 \u0393)) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val Uni\n intV : \u2200 (n : \u2124) \u2192 Val Uni\n-- \u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 \u27e6 \u03c4 \u27e7Type\n-- \u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7Var \u03c1\n-- \u27e6 lett f x t \u27e7Term \u03c1 = \u27e6 t \u27e7Term (\u27e6 f \u27e7Var \u03c1 (\u27e6 x \u27e7Var \u03c1) \u2022 \u03c1)\n\ndata ErrVal : Set where\n Done : (v : Val Uni) \u2192 ErrVal\n Error : ErrVal\n TimeOut : ErrVal\n\n_>>=_ : ErrVal \u2192 (Val Uni \u2192 ErrVal) \u2192 ErrVal\nDone v >>= f = f v\nError >>= f = Error\nTimeOut >>= f = TimeOut\n\nRes : Set\nRes = \u2115 \u2192 ErrVal\n\neval-fun : \u2200 {\u0393} \u2192 Fun \u0393 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res\neval-term : \u2200 {\u0393} \u2192 Term \u0393 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res\n\napply : Val Uni \u2192 Val Uni \u2192 Res\napply (closure f \u03c1) a n = eval-fun f (a \u2022 \u03c1) n\napply (intV _) a n = Error\n\neval-term t \u03c1 zero = TimeOut\neval-term (var x) \u03c1 (suc n) = Done (\u27e6 x \u27e7Var \u03c1)\neval-term (lett f x t) \u03c1 (suc n) = apply (\u27e6 f \u27e7Var \u03c1) (\u27e6 x \u27e7Var \u03c1) n >>= (\u03bb v \u2192 eval-term t (v \u2022 \u03c1) n)\n\neval-fun (term t) \u03c1 n = eval-term t \u03c1 n\neval-fun (abs f) \u03c1 n = Done (closure f \u03c1)\n\n-- Erasure from typed to untyped values.\nimport Thesis.ANormalBigStep as T\n\nerase-type : T.Type \u2192 Type\nerase-type _ = Uni\n\nerase-val : \u2200 {\u03c4} \u2192 T.Val \u03c4 \u2192 Val (erase-type \u03c4)\n\nerase-errVal : \u2200 {\u03c4} \u2192 T.ErrVal \u03c4 \u2192 ErrVal\nerase-errVal (T.Done v) = Done (erase-val v)\nerase-errVal T.Error = Error\nerase-errVal T.TimeOut = TimeOut\n\nerase-res : \u2200 {\u03c4} \u2192 T.Res \u03c4 \u2192 Res\nerase-res r n = erase-errVal (r n)\n\nerase-ctx : T.Context \u2192 Context\nerase-ctx \u2205 = \u2205\nerase-ctx (\u03c4 \u2022 \u0393) = erase-type \u03c4 \u2022 (erase-ctx \u0393)\n\nerase-env : \u2200 {\u0393} \u2192 T.Op.\u27e6 \u0393 \u27e7Context \u2192 \u27e6 erase-ctx \u0393 \u27e7Context\nerase-env \u2205 = \u2205\nerase-env (v \u2022 \u03c1) = erase-val v \u2022 erase-env \u03c1\n\nerase-var : \u2200 {\u0393 \u03c4} \u2192 T.Var \u0393 \u03c4 \u2192 Var (erase-ctx \u0393) (erase-type \u03c4)\nerase-var T.this = this\nerase-var (T.that x) = that (erase-var x)\n\nerase-term : \u2200 {\u0393 \u03c4} \u2192 T.Term \u0393 \u03c4 \u2192 Term (erase-ctx \u0393)\nerase-term (T.var x) = var (erase-var x)\nerase-term (T.lett f x t) = lett (erase-var f) (erase-var x) (erase-term t)\n\nerase-fun : \u2200 {\u0393 \u03c4} \u2192 T.Fun \u0393 \u03c4 \u2192 Fun (erase-ctx \u0393)\nerase-fun (T.term x) = term (erase-term x)\nerase-fun (T.abs f) = abs (erase-fun f)\n\nerase-val (T.closure t \u03c1) = closure (erase-fun t) (erase-env \u03c1)\nerase-val (T.intV n) = intV n\n\n-- Different erasures commute.\nerasure-commute-var : \u2200 {\u0393 \u03c4} (x : T.Var \u0393 \u03c4) \u03c1 \u2192\n erase-val (T.Op.\u27e6 x \u27e7Var \u03c1) \u2261 \u27e6 erase-var x \u27e7Var (erase-env \u03c1)\nerasure-commute-var T.this (v \u2022 \u03c1) = refl\nerasure-commute-var (T.that x) (v \u2022 \u03c1) = erasure-commute-var x \u03c1\n\nerase-bind : \u2200 {\u03c3 \u03c4 \u0393} a (t : T.Term (\u03c3 \u2022 \u0393) \u03c4) \u03c1 n \u2192 erase-errVal (a T.>>= (\u03bb v \u2192 T.eval-term t (v \u2022 \u03c1) n)) \u2261 erase-errVal a >>= (\u03bb v \u2192 eval-term (erase-term t) (v \u2022 erase-env \u03c1) n)\n\nerasure-commute-fun : \u2200 {\u0393 \u03c4} (t : T.Fun \u0393 \u03c4) \u03c1 n \u2192\n erase-errVal (T.eval-fun t \u03c1 n) \u2261 eval-fun (erase-fun t) (erase-env \u03c1) n\n\nerasure-commute-apply : \u2200 {\u03c3 \u03c4} (f : T.Val (\u03c3 T.\u21d2 \u03c4)) a n \u2192 erase-errVal (T.apply f a n) \u2261 apply (erase-val f) (erase-val a) n\nerasure-commute-apply {\u03c3} (T.closure t \u03c1) a n = erasure-commute-fun t (a \u2022 \u03c1) n\n\nerasure-commute-term : \u2200 {\u0393 \u03c4} (t : T.Term \u0393 \u03c4) \u03c1 n \u2192\n erase-errVal (T.eval-term t \u03c1 n) \u2261 eval-term (erase-term t) (erase-env \u03c1) n\n\nerasure-commute-fun (T.term t) \u03c1 n = erasure-commute-term t \u03c1 n\nerasure-commute-fun (T.abs t) \u03c1 n = refl\n\nerasure-commute-term t \u03c1 zero = refl\nerasure-commute-term (T.var x) \u03c1 (\u2115.suc n) = cong Done (erasure-commute-var x \u03c1)\nerasure-commute-term (T.lett f x t) \u03c1 (\u2115.suc n) rewrite erase-bind (T.apply (T.Op.\u27e6 f \u27e7Var \u03c1) (T.Op.\u27e6 x \u27e7Var \u03c1) n) t \u03c1 n | erasure-commute-apply (T.Op.\u27e6 f \u27e7Var \u03c1) (T.Op.\u27e6 x \u27e7Var \u03c1) n | erasure-commute-var f \u03c1 | erasure-commute-var x \u03c1 = refl\n\nerase-bind (T.Done v) t \u03c1 n = erasure-commute-term t (v \u2022 \u03c1) n\nerase-bind T.Error t \u03c1 n = refl\nerase-bind T.TimeOut t \u03c1 n = refl\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a66b08fcfbf8ac4b747cc3153338fe176a361f01","subject":"scraps","message":"scraps\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"expansion-unicity.agda","new_file":"expansion-unicity.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nmodule expansion-unicity where\n mutual\n expansion-unicity-synth : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {d1 d2 : dhexp} {\u03941 \u03942 : hctx} \u2192\n \u0393 \u22a2 e \u21d2 \u03c41 ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e \u21d2 \u03c42 ~> d2 \u22a3 \u03942 \u2192\n \u03c41 == \u03c42 \u00d7 d1 == d2 \u00d7 \u03941 == \u03942\n expansion-unicity-synth ESConst ESConst = refl , refl , refl\n expansion-unicity-synth (ESVar {\u0393 = \u0393} x\u2081) (ESVar x\u2082) = ctxunicity {\u0393 = \u0393} x\u2081 x\u2082 , refl , refl\n expansion-unicity-synth (ESLam apt1 d1) (ESLam apt2 d2) with expansion-unicity-synth d1 d2\n ... | ih1 , ih2 , ih3 = ap1 _ ih1 , ap1 _ ih2 , {!!}\n expansion-unicity-synth (ESAp1 x x\u2081 x\u2082 x\u2083) (ESAp1 x\u2084 x\u2085 x\u2086 x\u2087) = {!!}\n expansion-unicity-synth (ESAp1 x x\u2081 x\u2082 x\u2083) (ESAp2 x\u2084 d5 x\u2085 x\u2086) = {!!}\n expansion-unicity-synth (ESAp1 x x\u2081 x\u2082 x\u2083) (ESAp3 x\u2084 d5 x\u2085) = {!!}\n expansion-unicity-synth (ESAp2 x d5 x\u2081 x\u2082) (ESAp1 x\u2083 x\u2084 x\u2085 x\u2086) = {!!}\n expansion-unicity-synth (ESAp2 x d5 x\u2081 x\u2082) (ESAp2 x\u2083 d6 x\u2084 x\u2085) = {!!}\n expansion-unicity-synth (ESAp2 x d5 x\u2081 x\u2082) (ESAp3 x\u2083 d6 x\u2084) = {!!}\n expansion-unicity-synth (ESAp3 x d5 x\u2081) (ESAp1 x\u2082 x\u2083 x\u2084 x\u2085) = {!!}\n expansion-unicity-synth (ESAp3 x d5 x\u2081) (ESAp2 x\u2082 d6 x\u2083 x\u2084) = {!!}\n expansion-unicity-synth (ESAp3 x d5 x\u2081) (ESAp3 x\u2082 d6 x\u2083) = {!!}\n expansion-unicity-synth ESEHole ESEHole = refl , refl , refl\n expansion-unicity-synth (ESNEHole d1) (ESNEHole d2) with expansion-unicity-synth d1 d2\n ... | ih1 , ih2 , ih3 = refl , ap1 _ ih2 , ap1 _ ih3\n expansion-unicity-synth (ESAsc1 x x\u2081) (ESAsc1 x\u2082 x\u2083) = {!!}\n expansion-unicity-synth (ESAsc1 x x\u2081) (ESAsc2 x\u2082) = {!!}\n expansion-unicity-synth (ESAsc2 x) (ESAsc1 x\u2081 x\u2082) = {!!}\n expansion-unicity-synth (ESAsc2 x) (ESAsc2 x\u2081) = {!!}\n\n expansion-unicity-ana : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c41' \u03c42 \u03c42' : htyp} {d1 d2 : dhexp} {\u03941 \u03942 : hctx} \u2192\n \u0393 \u22a2 e \u21d0 \u03c41 ~> d1 :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n \u03c41 == \u03c42 \u00d7 d1 == d2 \u00d7 \u03c41' == \u03c42' \u00d7 \u03941 == \u03942\n expansion-unicity-ana (EALam apt1 d1) (EALam apt2 d2) = {!!}\n expansion-unicity-ana (EALam apt1 d1) (EASubsume x\u2081 x\u2082 x\u2083 x\u2084) = {!!}\n expansion-unicity-ana (EASubsume x\u2081 x\u2082 x\u2083 x\u2084) (EALam apt2 d2) = {!!}\n expansion-unicity-ana (EASubsume x x\u2081 x\u2082 x\u2083) (EASubsume x\u2084 x\u2085 x\u2086 x\u2087) = {!!}\n expansion-unicity-ana (EASubsume x x\u2081 x\u2082 x\u2083) EAEHole = {!!}\n expansion-unicity-ana (EASubsume x x\u2081 x\u2082 x\u2083) (EANEHole x\u2084 x\u2085) = {!!}\n expansion-unicity-ana EAEHole (EASubsume x x\u2081 x\u2082 x\u2083) = {!!}\n expansion-unicity-ana EAEHole EAEHole = {!!}\n expansion-unicity-ana (EANEHole x x\u2081) (EASubsume x\u2082 x\u2083 x\u2084 x\u2085) = {!!}\n expansion-unicity-ana (EANEHole x x\u2081) (EANEHole x\u2082 x\u2083) = {!!}\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nmodule expansion-unicity where\n mutual\n expansion-unicity-synth : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {d1 d2 : dhexp} {\u03941 \u03942 : hctx} \u2192\n \u0393 \u22a2 e \u21d2 \u03c41 ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e \u21d2 \u03c42 ~> d2 \u22a3 \u03942 \u2192\n \u03c41 == \u03c42 \u00d7 d1 == d2 \u00d7 \u03941 == \u03942\n expansion-unicity-synth ESConst ESConst = refl , refl , refl\n expansion-unicity-synth (ESVar {\u0393 = \u0393} x\u2081) (ESVar x\u2082) = ctxunicity {\u0393 = \u0393} x\u2081 x\u2082 , refl , refl\n expansion-unicity-synth (ESLam d1) (ESLam d2) with expansion-unicity-synth d1 d2\n ... | ih1 , ih2 , ih3 = ap1 _ ih1 , ap1 _ ih2 , refl\n expansion-unicity-synth (ESAp1 x x\u2081 x\u2082 x\u2083) (ESAp1 x\u2084 x\u2085 x\u2086 x\u2087) = {!!}\n expansion-unicity-synth (ESAp1 x x\u2081 x\u2082 x\u2083) (ESAp2 x\u2084 d5 x\u2085 x\u2086) = {!!}\n expansion-unicity-synth (ESAp1 x x\u2081 x\u2082 x\u2083) (ESAp3 x\u2084 d5 x\u2085) = {!!}\n expansion-unicity-synth (ESAp2 x d5 x\u2081 x\u2082) (ESAp1 x\u2083 x\u2084 x\u2085 x\u2086) = {!!}\n expansion-unicity-synth (ESAp2 x d5 x\u2081 x\u2082) (ESAp2 x\u2083 d6 x\u2084 x\u2085) = {!!}\n expansion-unicity-synth (ESAp2 x d5 x\u2081 x\u2082) (ESAp3 x\u2083 d6 x\u2084) = {!!}\n expansion-unicity-synth (ESAp3 x d5 x\u2081) (ESAp1 x\u2082 x\u2083 x\u2084 x\u2085) = {!!}\n expansion-unicity-synth (ESAp3 x d5 x\u2081) (ESAp2 x\u2082 d6 x\u2083 x\u2084) = {!!}\n expansion-unicity-synth (ESAp3 x d5 x\u2081) (ESAp3 x\u2082 d6 x\u2083) = {!!}\n expansion-unicity-synth ESEHole ESEHole = refl , refl , refl\n expansion-unicity-synth (ESNEHole d1) (ESNEHole d2) with expansion-unicity-synth d1 d2\n ... | ih1 , ih2 , ih3 = refl , ap1 _ ih2 , ap1 _ ih3\n expansion-unicity-synth (ESAsc1 x x\u2081) (ESAsc1 x\u2082 x\u2083) = {!!}\n expansion-unicity-synth (ESAsc1 x x\u2081) (ESAsc2 x\u2082) = {!!}\n expansion-unicity-synth (ESAsc2 x) (ESAsc1 x\u2081 x\u2082) = {!!}\n expansion-unicity-synth (ESAsc2 x) (ESAsc2 x\u2081) = {!!}\n\n expansion-unicity-ana : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c41' \u03c42 \u03c42' : htyp} {d1 d2 : dhexp} {\u03941 \u03942 : hctx} \u2192\n \u0393 \u22a2 e \u21d0 \u03c41 ~> d1 :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n \u03c41 == \u03c42 \u00d7 d1 == d2 \u00d7 \u03c41' == \u03c42' \u00d7 \u03941 == \u03942\n expansion-unicity-ana (EALam d1) (EALam d2) = {!!}\n expansion-unicity-ana (EALam d1) (EASubsume x\u2081 x\u2082 x\u2083 x\u2084) = {!!}\n expansion-unicity-ana (EASubsume x\u2081 x\u2082 x\u2083 x\u2084) (EALam d2) = {!!}\n expansion-unicity-ana (EASubsume x x\u2081 x\u2082 x\u2083) (EASubsume x\u2084 x\u2085 x\u2086 x\u2087) = {!!}\n expansion-unicity-ana (EASubsume x x\u2081 x\u2082 x\u2083) EAEHole = {!!}\n expansion-unicity-ana (EASubsume x x\u2081 x\u2082 x\u2083) (EANEHole x\u2084 x\u2085) = {!!}\n expansion-unicity-ana EAEHole (EASubsume x x\u2081 x\u2082 x\u2083) = {!!}\n expansion-unicity-ana EAEHole EAEHole = {!!}\n expansion-unicity-ana (EANEHole x x\u2081) (EASubsume x\u2082 x\u2083 x\u2084 x\u2085) = {!!}\n expansion-unicity-ana (EANEHole x x\u2081) (EANEHole x\u2082 x\u2083) = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"5423d9e6f476f8d4fea29421fe3397bfa9cfa786","subject":"Avoid local function (fix #1).","message":"Avoid local function (fix #1).\n\nThis fixes an error about hidden vs. visible types in the type of a\nlocal function. The error was introduced by switching to Agda 2.4.0. I\ndon't understand the error. It seems that the local function was\nintroduced to be able to use rewrite, so I rewrote the proof with subst\ninstead. This avoids the local function, and thus the error.\n\nThe ilc-agda project now type checks with current Agda. Tested versions:\n\nagda\/agda-stdlib@517d842ed43d0684e31b2e3e1f45ec9dda703761\n\nagda\/agda@b21f32a35f1a1b584538e3aaec3f62c9b4463aff (2_4_0)\nagda\/agda@70c8a575c46f6a568c7518150a1a64fcd03aa437\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Implementation.agda","new_file":"Parametric\/Change\/Implementation.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Logical relation for erasure (Def. 3.8 and Lemma 3.9)\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Change.Validity as Validity\nimport Parametric.Change.Specification as Specification\nimport Parametric.Change.Type as ChangeType\nimport Parametric.Change.Value as ChangeValue\nimport Parametric.Change.Derive as Derive\n\nmodule Parametric.Change.Implementation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (\u0394Base : ChangeType.Structure Base)\n (validity-structure : Validity.Structure \u27e6_\u27e7Base)\n (specification-structure : Specification.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const validity-structure)\n (\u27e6apply-base\u27e7 : ChangeValue.ApplyStructure Const \u27e6_\u27e7Base \u0394Base)\n (\u27e6diff-base\u27e7 : ChangeValue.DiffStructure Const \u27e6_\u27e7Base \u0394Base)\n (derive-const : Derive.Structure Const \u0394Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen Validity.Structure \u27e6_\u27e7Base validity-structure\nopen Specification.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const validity-structure specification-structure\nopen ChangeType.Structure Base \u0394Base\nopen ChangeValue.Structure Const \u27e6_\u27e7Base \u0394Base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7\nopen Derive.Structure Const \u0394Base derive-const\n\nopen import Base.Denotation.Notation\n\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\n\nrecord Structure : Set\u2081 where\n\n ----------------\n -- Parameters --\n ----------------\n\n field\n -- Extension point 1: Logical relation on base types.\n --\n -- In the paper, we assume that the logical relation is equality on base types\n -- (see Def. 3.8a). Here, we only require that plugins define what the logical\n -- relation is on base types, and provide proofs for the two extension points\n -- below.\n implements-base : \u2200 \u03b9 {v : \u27e6 \u03b9 \u27e7Base} \u2192 \u0394\u208d \u03b9 \u208e v \u2192 \u27e6 \u0394Base \u03b9 \u27e7Base \u2192 Set\n\n -- Extension point 2: Differences on base types are logically related.\n u\u229fv\u2248u\u229dv-base : \u2200 \u03b9 {u v : \u27e6 \u03b9 \u27e7Base} \u2192\n implements-base \u03b9 (u \u229f\u208d \u03b9 \u208e v) (\u27e6diff-base\u27e7 \u03b9 u v)\n\n -- Extension point 3: Lemma 3.1 for base types.\n carry-over-base : \u2200 {\u03b9}\n {v : \u27e6 \u03b9 \u27e7Base}\n (\u0394v : \u0394\u208d \u03b9 \u208e v)\n {\u0394v\u2032 : \u27e6 \u0394Base \u03b9 \u27e7Base} (\u0394v\u2248\u0394v\u2032 : implements-base \u03b9 \u0394v \u0394v\u2032) \u2192\n v \u229e\u208d base \u03b9 \u208e \u0394v \u2261 v \u27e6\u2295\u208d base \u03b9 \u208e\u27e7 \u0394v\u2032\n\n ------------------------\n -- Logical relation \u2248 --\n ------------------------\n\n infix 4 implements\n syntax implements \u03c4 u v = u \u2248\u208d \u03c4 \u208e v\n implements : \u2200 \u03c4 {v} \u2192 \u0394\u208d \u03c4 \u208e v \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 Set\n\n implements (base \u03b9) \u0394f \u0394f\u2032 = implements-base \u03b9 \u0394f \u0394f\u2032\n implements (\u03c3 \u21d2 \u03c4) {f} \u0394f \u0394f\u2032 =\n (w : \u27e6 \u03c3 \u27e7) (\u0394w : \u0394\u208d \u03c3 \u208e w)\n (\u0394w\u2032 : \u27e6 \u0394Type \u03c3 \u27e7) (\u0394w\u2248\u0394w\u2032 : implements \u03c3 {w} \u0394w \u0394w\u2032) \u2192\n implements \u03c4 {f w} (call-change {\u03c3} {\u03c4} \u0394f w \u0394w) (\u0394f\u2032 w \u0394w\u2032)\n\n infix 4 _\u2248_\n _\u2248_ : \u2200 {\u03c4 v} \u2192 \u0394\u208d \u03c4 \u208e v \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 Set\n _\u2248_ {\u03c4} {v} = implements \u03c4 {v}\n\n data implements-env : \u2200 \u0393 \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) \u2192 \u27e6 mapContext \u0394Type \u0393 \u27e7 \u2192 Set where\n \u2205 : implements-env \u2205 {\u2205} \u2205 \u2205\n _\u2022_ : \u2200 {\u03c4 \u0393 v \u03c1 dv d\u03c1 v\u2032 \u03c1\u2032} \u2192\n (dv\u2248v\u2032 : implements \u03c4 {v} dv v\u2032) \u2192\n (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 {\u03c1} d\u03c1 \u03c1\u2032) \u2192\n implements-env (\u03c4 \u2022 \u0393) {v \u2022 \u03c1} (dv \u2022 d\u03c1) (v\u2032 \u2022 \u03c1\u2032)\n\n ----------------\n -- carry-over --\n ----------------\n\n -- This is lemma 3.10.\n carry-over : \u2200 {\u03c4}\n {v : \u27e6 \u03c4 \u27e7}\n (\u0394v : \u0394\u208d \u03c4 \u208e v)\n {\u0394v\u2032 : \u27e6 \u0394Type \u03c4 \u27e7} (\u0394v\u2248\u0394v\u2032 : \u0394v \u2248\u208d \u03c4 \u208e \u0394v\u2032) \u2192\n after\u208d \u03c4 \u208e \u0394v \u2261 before\u208d \u03c4 \u208e \u0394v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u0394v\u2032\n\n u\u229fv\u2248u\u229dv : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192\n u \u229f\u208d \u03c4 \u208e v \u2248\u208d \u03c4 \u208e u \u27e6\u229d\u208d \u03c4 \u208e\u27e7 v\n\n u\u229fv\u2248u\u229dv {base \u03b9} {u} {v} = u\u229fv\u2248u\u229dv-base \u03b9 {u} {v}\n u\u229fv\u2248u\u229dv {\u03c3 \u21d2 \u03c4} {g} {f} w \u0394w \u0394w\u2032 \u0394w\u2248\u0394w\u2032 =\n subst\n (\u03bb \u25a1 \u2192 (g \u25a1 \u229f\u208d \u03c4 \u208e f (before\u208d \u03c3 \u208e \u0394w)) \u2248\u208d \u03c4 \u208e g (before\u208d \u03c3 \u208e \u0394w \u27e6\u2295\u208d \u03c3 \u208e\u27e7 \u0394w\u2032) \u27e6\u229d\u208d \u03c4 \u208e\u27e7 f (before\u208d \u03c3 \u208e \u0394w))\n (sym (carry-over {\u03c3} \u0394w \u0394w\u2248\u0394w\u2032))\n (u\u229fv\u2248u\u229dv {\u03c4} {g (before\u208d \u03c3 \u208e \u0394w \u27e6\u2295\u208d \u03c3 \u208e\u27e7 \u0394w\u2032)} {f (before\u208d \u03c3 \u208e \u0394w)})\n\n carry-over {base \u03b9} \u0394v \u0394v\u2248\u0394v\u2032 = carry-over-base \u0394v \u0394v\u2248\u0394v\u2032\n carry-over {\u03c3 \u21d2 \u03c4} {f} \u0394f {\u0394f\u2032} \u0394f\u2248\u0394f\u2032 =\n ext (\u03bb v \u2192\n carry-over {\u03c4} {f v} (call-change {\u03c3} {\u03c4} \u0394f v (nil\u208d \u03c3 \u208e v))\n {\u0394f\u2032 v (v \u27e6\u229d\u208d \u03c3 \u208e\u27e7 v)}\n (\u0394f\u2248\u0394f\u2032 v (nil\u208d \u03c3 \u208e v) (v \u27e6\u229d\u208d \u03c3 \u208e\u27e7 v) ( u\u229fv\u2248u\u229dv {\u03c3} {v} {v})))\n\n -- A property relating `alternate` and the subcontext relation \u0393\u227c\u0394\u0393\n \u27e6\u0393\u227c\u0394\u0393\u27e7 : \u2200 {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u27e6 mapContext \u0394Type \u0393 \u27e7) \u2192\n \u03c1 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 (alternate \u03c1 d\u03c1)\n \u27e6\u0393\u227c\u0394\u0393\u27e7 \u2205 \u2205 = refl\n \u27e6\u0393\u227c\u0394\u0393\u27e7 (v \u2022 \u03c1) (dv \u2022 d\u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1)\n\n -- A specialization of the soundness of weakening\n \u27e6fit\u27e7 : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u27e6 mapContext \u0394Type \u0393 \u27e7) \u2192\n \u27e6 t \u27e7 \u03c1 \u2261 \u27e6 fit t \u27e7 (alternate \u03c1 d\u03c1)\n \u27e6fit\u27e7 t \u03c1 d\u03c1 =\n trans (cong \u27e6 t \u27e7 (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1)) (sym (weaken-sound t _))\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Logical relation for erasure (Def. 3.8 and Lemma 3.9)\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Change.Validity as Validity\nimport Parametric.Change.Specification as Specification\nimport Parametric.Change.Type as ChangeType\nimport Parametric.Change.Value as ChangeValue\nimport Parametric.Change.Derive as Derive\n\nmodule Parametric.Change.Implementation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (\u0394Base : ChangeType.Structure Base)\n (validity-structure : Validity.Structure \u27e6_\u27e7Base)\n (specification-structure : Specification.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const validity-structure)\n (\u27e6apply-base\u27e7 : ChangeValue.ApplyStructure Const \u27e6_\u27e7Base \u0394Base)\n (\u27e6diff-base\u27e7 : ChangeValue.DiffStructure Const \u27e6_\u27e7Base \u0394Base)\n (derive-const : Derive.Structure Const \u0394Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen Validity.Structure \u27e6_\u27e7Base validity-structure\nopen Specification.Structure\n Const \u27e6_\u27e7Base \u27e6_\u27e7Const validity-structure specification-structure\nopen ChangeType.Structure Base \u0394Base\nopen ChangeValue.Structure Const \u27e6_\u27e7Base \u0394Base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7\nopen Derive.Structure Const \u0394Base derive-const\n\nopen import Base.Denotation.Notation\n\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\n\nrecord Structure : Set\u2081 where\n\n ----------------\n -- Parameters --\n ----------------\n\n field\n -- Extension point 1: Logical relation on base types.\n --\n -- In the paper, we assume that the logical relation is equality on base types\n -- (see Def. 3.8a). Here, we only require that plugins define what the logical\n -- relation is on base types, and provide proofs for the two extension points\n -- below.\n implements-base : \u2200 \u03b9 {v : \u27e6 \u03b9 \u27e7Base} \u2192 \u0394\u208d \u03b9 \u208e v \u2192 \u27e6 \u0394Base \u03b9 \u27e7Base \u2192 Set\n\n -- Extension point 2: Differences on base types are logically related.\n u\u229fv\u2248u\u229dv-base : \u2200 \u03b9 {u v : \u27e6 \u03b9 \u27e7Base} \u2192\n implements-base \u03b9 (u \u229f\u208d \u03b9 \u208e v) (\u27e6diff-base\u27e7 \u03b9 u v)\n\n -- Extension point 3: Lemma 3.1 for base types.\n carry-over-base : \u2200 {\u03b9}\n {v : \u27e6 \u03b9 \u27e7Base}\n (\u0394v : \u0394\u208d \u03b9 \u208e v)\n {\u0394v\u2032 : \u27e6 \u0394Base \u03b9 \u27e7Base} (\u0394v\u2248\u0394v\u2032 : implements-base \u03b9 \u0394v \u0394v\u2032) \u2192\n v \u229e\u208d base \u03b9 \u208e \u0394v \u2261 v \u27e6\u2295\u208d base \u03b9 \u208e\u27e7 \u0394v\u2032\n\n ------------------------\n -- Logical relation \u2248 --\n ------------------------\n\n infix 4 implements\n syntax implements \u03c4 u v = u \u2248\u208d \u03c4 \u208e v\n implements : \u2200 \u03c4 {v} \u2192 \u0394\u208d \u03c4 \u208e v \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 Set\n\n implements (base \u03b9) \u0394f \u0394f\u2032 = implements-base \u03b9 \u0394f \u0394f\u2032\n implements (\u03c3 \u21d2 \u03c4) {f} \u0394f \u0394f\u2032 =\n (w : \u27e6 \u03c3 \u27e7) (\u0394w : \u0394\u208d \u03c3 \u208e w)\n (\u0394w\u2032 : \u27e6 \u0394Type \u03c3 \u27e7) (\u0394w\u2248\u0394w\u2032 : implements \u03c3 {w} \u0394w \u0394w\u2032) \u2192\n implements \u03c4 {f w} (call-change {\u03c3} {\u03c4} \u0394f w \u0394w) (\u0394f\u2032 w \u0394w\u2032)\n\n infix 4 _\u2248_\n _\u2248_ : \u2200 {\u03c4 v} \u2192 \u0394\u208d \u03c4 \u208e v \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 Set\n _\u2248_ {\u03c4} {v} = implements \u03c4 {v}\n\n data implements-env : \u2200 \u0393 \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) \u2192 \u27e6 mapContext \u0394Type \u0393 \u27e7 \u2192 Set where\n \u2205 : implements-env \u2205 {\u2205} \u2205 \u2205\n _\u2022_ : \u2200 {\u03c4 \u0393 v \u03c1 dv d\u03c1 v\u2032 \u03c1\u2032} \u2192\n (dv\u2248v\u2032 : implements \u03c4 {v} dv v\u2032) \u2192\n (d\u03c1\u2248\u03c1\u2032 : implements-env \u0393 {\u03c1} d\u03c1 \u03c1\u2032) \u2192\n implements-env (\u03c4 \u2022 \u0393) {v \u2022 \u03c1} (dv \u2022 d\u03c1) (v\u2032 \u2022 \u03c1\u2032)\n\n ----------------\n -- carry-over --\n ----------------\n\n -- This is lemma 3.10.\n carry-over : \u2200 {\u03c4}\n {v : \u27e6 \u03c4 \u27e7}\n (\u0394v : \u0394\u208d \u03c4 \u208e v)\n {\u0394v\u2032 : \u27e6 \u0394Type \u03c4 \u27e7} (\u0394v\u2248\u0394v\u2032 : \u0394v \u2248\u208d \u03c4 \u208e \u0394v\u2032) \u2192\n after\u208d \u03c4 \u208e \u0394v \u2261 before\u208d \u03c4 \u208e \u0394v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u0394v\u2032\n\n u\u229fv\u2248u\u229dv : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192\n u \u229f\u208d \u03c4 \u208e v \u2248\u208d \u03c4 \u208e u \u27e6\u229d\u208d \u03c4 \u208e\u27e7 v\n\n u\u229fv\u2248u\u229dv {base \u03b9} {u} {v} = u\u229fv\u2248u\u229dv-base \u03b9 {u} {v}\n u\u229fv\u2248u\u229dv {\u03c3 \u21d2 \u03c4} {g} {f} = result where\n result : (w : \u27e6 \u03c3 \u27e7) (\u0394w : \u0394\u208d \u03c3 \u208e w) \u2192\n (\u0394w\u2032 : \u27e6 \u0394Type \u03c3 \u27e7) \u2192 \u0394w \u2248\u208d \u03c3 \u208e \u0394w\u2032 \u2192\n (g (after\u208d \u03c3 \u208e \u0394w) \u229f\u208d \u03c4 \u208e f (before\u208d \u03c3 \u208e \u0394w)) \u2248\u208d \u03c4 \u208e g (before\u208d \u03c3 \u208e \u0394w \u27e6\u2295\u208d \u03c3 \u208e\u27e7 \u0394w\u2032) \u27e6\u229d\u208d \u03c4 \u208e\u27e7 f (before\u208d \u03c3 \u208e \u0394w)\n result w \u0394w \u0394w\u2032 \u0394w\u2248\u0394w\u2032\n rewrite carry-over {\u03c3} \u0394w \u0394w\u2248\u0394w\u2032 =\n u\u229fv\u2248u\u229dv {\u03c4} {g (before\u208d \u03c3 \u208e \u0394w \u27e6\u2295\u208d \u03c3 \u208e\u27e7 \u0394w\u2032)} {f (before\u208d \u03c3 \u208e \u0394w)}\n\n carry-over {base \u03b9} \u0394v \u0394v\u2248\u0394v\u2032 = carry-over-base \u0394v \u0394v\u2248\u0394v\u2032\n carry-over {\u03c3 \u21d2 \u03c4} {f} \u0394f {\u0394f\u2032} \u0394f\u2248\u0394f\u2032 =\n ext (\u03bb v \u2192\n carry-over {\u03c4} {f v} (call-change {\u03c3} {\u03c4} \u0394f v (nil\u208d \u03c3 \u208e v))\n {\u0394f\u2032 v (v \u27e6\u229d\u208d \u03c3 \u208e\u27e7 v)}\n (\u0394f\u2248\u0394f\u2032 v (nil\u208d \u03c3 \u208e v) (v \u27e6\u229d\u208d \u03c3 \u208e\u27e7 v) ( u\u229fv\u2248u\u229dv {\u03c3} {v} {v})))\n\n -- A property relating `alternate` and the subcontext relation \u0393\u227c\u0394\u0393\n \u27e6\u0393\u227c\u0394\u0393\u27e7 : \u2200 {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u27e6 mapContext \u0394Type \u0393 \u27e7) \u2192\n \u03c1 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 (alternate \u03c1 d\u03c1)\n \u27e6\u0393\u227c\u0394\u0393\u27e7 \u2205 \u2205 = refl\n \u27e6\u0393\u227c\u0394\u0393\u27e7 (v \u2022 \u03c1) (dv \u2022 d\u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1)\n\n -- A specialization of the soundness of weakening\n \u27e6fit\u27e7 : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) (\u03c1 : \u27e6 \u0393 \u27e7) (d\u03c1 : \u27e6 mapContext \u0394Type \u0393 \u27e7) \u2192\n \u27e6 t \u27e7 \u03c1 \u2261 \u27e6 fit t \u27e7 (alternate \u03c1 d\u03c1)\n \u27e6fit\u27e7 t \u03c1 d\u03c1 =\n trans (cong \u27e6 t \u27e7 (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1)) (sym (weaken-sound t _))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"e4292c039f3f26444126c66ebb060a83ab1e914d","subject":"Added tailS.","message":"Added tailS.\n\nIgnore-this: f8c3a1c756aff5692726c70d02433824\n\ndarcs-hash:20110730172619-3bd4e-8ac339efc4e78dfcdd270f2cd26b2497b39c4bf3.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Data\/Stream\/PropertiesATP.agda","new_file":"src\/FOTC\/Data\/Stream\/PropertiesATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Streams properties\n------------------------------------------------------------------------------\n\nmodule FOTC.Data.Stream.PropertiesATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Base.PropertiesATP\n\nopen import FOTC.Data.Stream.Type\n\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n\npostulate tailS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\n{-# ATP prove tailS #-}\n\n\u2248\u2192Stream : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Stream xs \u2227 Stream ys\n\u2248\u2192Stream {xs} {ys} xs\u2248ys = Stream-gfp\u2082 P\u2081 helper\u2081 (ys , xs\u2248ys)\n , Stream-gfp\u2082 P\u2082 helper\u2082 (xs , xs\u2248ys)\n where\n P\u2081 : D \u2192 Set\n P\u2081 ws = \u2203 \u03bb zs \u2192 ws \u2248 zs\n {-# ATP definition P\u2081 #-}\n\n P\u2082 : D \u2192 Set\n P\u2082 zs = \u2203 \u03bb ws \u2192 ws \u2248 zs\n {-# ATP definition P\u2082 #-}\n\n postulate\n helper\u2081 : \u2200 {ws} \u2192 P\u2081 ws \u2192\n \u2203 (\u03bb w' \u2192 \u2203 (\u03bb ws' \u2192 ws \u2261 w' \u2237 ws' \u2227 P\u2081 ws'))\n {-# ATP prove helper\u2081 #-}\n\n postulate\n helper\u2082 : \u2200 {zs} \u2192 P\u2082 zs \u2192\n \u2203 (\u03bb z' \u2192 \u2203 (\u03bb zs' \u2192 zs \u2261 z' \u2237 zs' \u2227 P\u2082 zs'))\n {-# ATP prove helper\u2082 #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Streams properties\n------------------------------------------------------------------------------\n\nmodule FOTC.Data.Stream.PropertiesATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Base.PropertiesATP\n\nopen import FOTC.Data.Stream.Type\n\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n\n\u2248\u2192Stream : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Stream xs \u2227 Stream ys\n\u2248\u2192Stream {xs} {ys} xs\u2248ys = Stream-gfp\u2082 P\u2081 helper\u2081 (ys , xs\u2248ys)\n , Stream-gfp\u2082 P\u2082 helper\u2082 (xs , xs\u2248ys)\n where\n P\u2081 : D \u2192 Set\n P\u2081 ws = \u2203 \u03bb zs \u2192 ws \u2248 zs\n {-# ATP definition P\u2081 #-}\n\n P\u2082 : D \u2192 Set\n P\u2082 zs = \u2203 \u03bb ws \u2192 ws \u2248 zs\n {-# ATP definition P\u2082 #-}\n\n postulate\n helper\u2081 : \u2200 {ws} \u2192 P\u2081 ws \u2192\n \u2203 (\u03bb w' \u2192 \u2203 (\u03bb ws' \u2192 ws \u2261 w' \u2237 ws' \u2227 P\u2081 ws'))\n {-# ATP prove helper\u2081 #-}\n\n postulate\n helper\u2082 : \u2200 {zs} \u2192 P\u2082 zs \u2192\n \u2203 (\u03bb z' \u2192 \u2203 (\u03bb zs' \u2192 zs \u2261 z' \u2237 zs' \u2227 P\u2082 zs'))\n {-# ATP prove helper\u2082 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"826c3786890b50ee2d861e13539405b2aac40bbf","subject":"Fixed doc.","message":"Fixed doc.\n\nIgnore-this: 50b6461ccb5bd7b956e03cae0ed535da\n\ndarcs-hash:20110402162139-3bd4e-30f7fb20d91dca52965210f6fa7687e4370efcb5.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/FOTC\/Data\/Nat\/AddTotality.agda","new_file":"Draft\/FOTC\/Data\/Nat\/AddTotality.agda","new_contents":"module Draft.FOTC.Data.Nat.AddTotality where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n\n-- Interactive proof using the induction principle for natural numbers.\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = indN P P0 ih Nm\n where\n P : D \u2192 Set\n P = \u03bb i \u2192 N (i + n)\n\n P0 : P zero\n P0 = subst N (sym (+-0x n)) Nn\n\n ih : \u2200 {i} \u2192 N i \u2192 P i \u2192 P (succ i)\n ih {i} Ni Pi = subst N (sym (+-Sx i n)) (sN Pi)\n\n-- Combined proof using an instance of the induction principle.\nindN-instance : (x : D) \u2192\n N (zero + x) \u2192\n (\u2200 {n} \u2192 N n \u2192 N (n + x) \u2192 N (succ n + x)) \u2192\n \u2200 {n} \u2192 N n \u2192 N (n + x)\nindN-instance x = indN (\u03bb i \u2192 N (i + x))\n\npostulate\n +-N\u2081 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n{-# ATP prove +-N\u2081 indN-instance #-}\n\n-- Combined proof using the induction principle.\n\n-- The translation is\n-- \u2200 p. app\u2081(p,zero) \u2192\n-- (\u2200 x. app\u2081(n,x) \u2192 app\u2081(p,x) \u2192 app\u2081(p,appFn(succ,x))) \u2192 -- indN\n-- (\u2200 x. app\u2081(n,x) \u2192 app\u2081(p,x))\n----------------------------------------------------------------\n-- \u2200 x y. app\u2081(n,x) \u2192 app\u2081(n,y) \u2192 app\u2081(n,appFn(appFn(+,x),y)) -- +-N\u2082\n\npostulate\n +-N\u2082 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n-- The ATPs could not prove this postulate.\n{-# ATP prove +-N\u2082 indN #-}\n","old_contents":"module Draft.FOTC.Data.Nat.AddTotality where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n\n-- Interactive proof using the induction principle for natural numbers.\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = indN P P0 ih Nm\n where\n P : D \u2192 Set\n P = \u03bb i \u2192 N (i + n)\n\n P0 : P zero\n P0 = subst N (sym (+-0x n)) Nn\n\n ih : \u2200 {i} \u2192 N i \u2192 P i \u2192 P (succ i)\n ih {i} Ni Pi = subst N (sym (+-Sx i n)) (sN Pi)\n\n-- Combined proof using an instance of the induction principle.\nindN-instance : (x : D) \u2192\n N (zero + x) \u2192\n (\u2200 {n} \u2192 N n \u2192 N (n + x) \u2192 N (succ n + x)) \u2192\n \u2200 {n} \u2192 N n \u2192 N (n + x)\nindN-instance x = indN (\u03bb i \u2192 N (i + x))\n\npostulate\n +-N\u2081 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n{-# ATP prove +-N\u2081 indN-instance #-}\n\n-- Combined proof using the induction principle.\n\n-- The translation is\n-- \u2200 p. app\u2081(p,zero) \u2192\n-- (\u2200 x. app\u2081(n,x) \u2192 app\u2081(n,x) \u2192 app\u2081(n,appFn(succ,x))) \u2192 -- indN\n-- (\u2200 x. app\u2081(n,x) \u2192 app\u2081(p,x))\n----------------------------------------------------------------\n-- \u2200 x y. app\u2081(n,x) \u2192 app\u2081(n,y) \u2192 app\u2081(n,appFn(appFn(+,x),y)) -- +-N\u2082\n\npostulate\n +-N\u2082 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n-- The ATPs could not prove this postulate.\n{-# ATP prove +-N\u2082 indN #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"55886cdd77fb42b777e571c6b0625cc4e67c7c4e","subject":"Cosmetic change.","message":"Cosmetic change.\n\nIgnore-this: 4cb7c429d7aabe9ac4be5cc0acb31552\n\ndarcs-hash:20120519135448-3bd4e-301a535b796fb0130b2cc51ef323c1879532647f.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/LTC-PCF\/Data\/Nat\/PropertiesI.agda","new_file":"src\/LTC-PCF\/Data\/Nat\/PropertiesI.agda","new_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule LTC-PCF.Data.Nat.PropertiesI where\n\nopen import Common.FOL.Relation.Binary.EqReasoning\nopen import Common.Function\n\nopen import LTC-PCF.Base\nopen import LTC-PCF.Base.Properties\nopen import LTC-PCF.Data.Nat\nopen import LTC-PCF.Data.Nat.Rec\nopen import LTC-PCF.Data.Nat.Rec.EquationsI\n\n------------------------------------------------------------------------------\n-- Congruence properties\n\n+-leftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 m + o \u2261 n + o\n+-leftCong refl = refl\n\n+-rightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 m + n \u2261 m + o\n+-rightCong refl = refl\n\n\u2238-leftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 m \u2238 o \u2261 n \u2238 o\n\u2238-leftCong refl = refl\n\n\u2238-rightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 m \u2238 n \u2261 m \u2238 o\n\u2238-rightCong refl = refl\n\n*-leftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 m * o \u2261 n * o\n*-leftCong refl = refl\n\n*-rightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 m * n \u2261 m * o\n*-rightCong refl = refl\n\n------------------------------------------------------------------------------\n\n+-0x : \u2200 n \u2192 zero + n \u2261 n\n+-0x n = rec zero n _ \u2261\u27e8 rec-0 n \u27e9\n n \u220e\n\n+-Sx : \u2200 m n \u2192 succ\u2081 m + n \u2261 succ\u2081 (m + n)\n+-Sx m n =\n rec (succ\u2081 m) n (lam (\u03bb x \u2192 lam (\u03bb y \u2192 succ\u2081 y)))\n \u2261\u27e8 rec-S m n (lam (\u03bb x \u2192 lam (\u03bb y \u2192 succ\u2081 y))) \u27e9\n (lam (\u03bb x \u2192 lam (\u03bb y \u2192 succ\u2081 y))) \u00b7 m \u00b7 (m + n)\n \u2261\u27e8 \u00b7-leftCong (beta (\u03bb x \u2192 lam (\u03bb y \u2192 succ\u2081 y)) m) \u27e9\n (\u03bb x \u2192 lam (\u03bb y \u2192 succ\u2081 y)) m \u00b7 (m + n)\n \u2261\u27e8 refl \u27e9\n lam succ\u2081 \u00b7 (m + n)\n \u2261\u27e8 beta succ\u2081 (m + n) \u27e9\n succ\u2081 (m + n) \u220e\n\n\u2238-x0 : \u2200 n \u2192 n \u2238 zero \u2261 n\n\u2238-x0 n = rec zero n _ \u2261\u27e8 rec-0 n \u27e9\n n \u220e\n\n\u2238-0S : \u2200 {n} \u2192 N n \u2192 zero \u2238 succ\u2081 n \u2261 zero\n\u2238-0S zN =\n rec (succ\u2081 zero) zero (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)))\n \u2261\u27e8 rec-S zero zero (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y))) \u27e9\n lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) \u00b7 zero \u00b7 (zero \u2238 zero)\n \u2261\u27e8 \u00b7-leftCong (beta (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) zero) \u27e9\n (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) zero \u00b7 (zero \u2238 zero)\n \u2261\u27e8 refl \u27e9\n lam pred\u2081 \u00b7 (zero \u2238 zero)\n \u2261\u27e8 beta pred\u2081 (zero \u2238 zero) \u27e9\n pred\u2081 (zero \u2238 zero)\n \u2261\u27e8 cong pred\u2081 (\u2238-x0 zero) \u27e9\n pred\u2081 zero\n \u2261\u27e8 pred-0 \u27e9\n zero \u220e\n\n\u2238-0S (sN {n} Nn) =\n rec (succ\u2081 (succ\u2081 n)) zero (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)))\n \u2261\u27e8 rec-S (succ\u2081 n) zero (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y))) \u27e9\n lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) \u00b7 (succ\u2081 n) \u00b7 (zero \u2238 (succ\u2081 n))\n \u2261\u27e8 \u00b7-leftCong (beta (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) (succ\u2081 n)) \u27e9\n (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) (succ\u2081 n) \u00b7 (zero \u2238 (succ\u2081 n))\n \u2261\u27e8 refl \u27e9\n lam pred\u2081 \u00b7 (zero \u2238 (succ\u2081 n))\n \u2261\u27e8 beta pred\u2081 (zero \u2238 (succ\u2081 n)) \u27e9\n pred\u2081 (zero \u2238 (succ\u2081 n))\n \u2261\u27e8 cong pred\u2081 (\u2238-0S Nn) \u27e9\n pred\u2081 zero\n \u2261\u27e8 pred-0 \u27e9\n zero \u220e\n\n\u2238-0x : \u2200 {n} \u2192 N n \u2192 zero \u2238 n \u2261 zero\n\u2238-0x zN = \u2238-x0 zero\n\u2238-0x (sN Nn) = \u2238-0S Nn\n\n\u2238-SS : \u2200 {m n} \u2192 N m \u2192 N n \u2192 succ\u2081 m \u2238 succ\u2081 n \u2261 m \u2238 n\n\u2238-SS {m} _ zN =\n rec (succ\u2081 zero) (succ\u2081 m) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)))\n \u2261\u27e8 rec-S zero (succ\u2081 m) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y))) \u27e9\n lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) \u00b7 zero \u00b7 (succ\u2081 m \u2238 zero)\n \u2261\u27e8 \u00b7-leftCong (beta (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) zero) \u27e9\n (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) zero \u00b7 (succ\u2081 m \u2238 zero)\n \u2261\u27e8 refl \u27e9\n lam pred\u2081 \u00b7 (succ\u2081 m \u2238 zero)\n \u2261\u27e8 beta pred\u2081 (succ\u2081 m \u2238 zero) \u27e9\n pred\u2081 (succ\u2081 m \u2238 zero)\n \u2261\u27e8 cong pred\u2081 (\u2238-x0 (succ\u2081 m)) \u27e9\n pred\u2081 (succ\u2081 m)\n \u2261\u27e8 pred-S m \u27e9\n m\n \u2261\u27e8 sym $ \u2238-x0 m \u27e9\n m \u2238 zero \u220e\n\n\u2238-SS zN (sN {n} Nn) =\n rec (succ\u2081 (succ\u2081 n)) (succ\u2081 zero) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)))\n \u2261\u27e8 rec-S (succ\u2081 n) (succ\u2081 zero) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y))) \u27e9\n lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) \u00b7 (succ\u2081 n) \u00b7 (succ\u2081 zero \u2238 succ\u2081 n)\n \u2261\u27e8 \u00b7-leftCong (beta (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) (succ\u2081 n)) \u27e9\n (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) (succ\u2081 n) \u00b7 (succ\u2081 zero \u2238 succ\u2081 n)\n \u2261\u27e8 refl \u27e9\n lam pred\u2081 \u00b7 (succ\u2081 zero \u2238 succ\u2081 n)\n \u2261\u27e8 beta pred\u2081 (succ\u2081 zero \u2238 succ\u2081 n) \u27e9\n pred\u2081 (succ\u2081 zero \u2238 succ\u2081 n)\n \u2261\u27e8 cong pred\u2081 (\u2238-SS zN Nn) \u27e9\n pred\u2081 (zero \u2238 n)\n \u2261\u27e8 cong pred\u2081 (\u2238-0x Nn) \u27e9\n pred\u2081 zero\n \u2261\u27e8 pred-0 \u27e9\n zero\n \u2261\u27e8 sym $ \u2238-0S Nn \u27e9\n zero \u2238 succ\u2081 n \u220e\n\n\u2238-SS (sN {m} Nm) (sN {n} Nn) =\n rec (succ\u2081 (succ\u2081 n)) (succ\u2081 (succ\u2081 m)) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)))\n \u2261\u27e8 rec-S (succ\u2081 n) (succ\u2081 (succ\u2081 m)) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y))) \u27e9\n lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) \u00b7 (succ\u2081 n) \u00b7 (succ\u2081 (succ\u2081 m) \u2238 succ\u2081 n)\n \u2261\u27e8 \u00b7-leftCong (beta (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) (succ\u2081 n)) \u27e9\n (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) (succ\u2081 n) \u00b7 (succ\u2081 (succ\u2081 m) \u2238 succ\u2081 n)\n \u2261\u27e8 refl \u27e9\n lam pred\u2081 \u00b7 (succ\u2081 (succ\u2081 m) \u2238 succ\u2081 n)\n \u2261\u27e8 beta pred\u2081 (succ\u2081 (succ\u2081 m) \u2238 succ\u2081 n) \u27e9\n pred\u2081 (succ\u2081 (succ\u2081 m) \u2238 succ\u2081 n)\n \u2261\u27e8 cong pred\u2081 (\u2238-SS (sN Nm) Nn) \u27e9\n pred\u2081 (succ\u2081 m \u2238 n)\n \u2261\u27e8 sym $ beta pred\u2081 (succ\u2081 m \u2238 n) \u27e9\n lam pred\u2081 \u00b7 (succ\u2081 m \u2238 n)\n \u2261\u27e8 refl \u27e9\n (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) n \u00b7 (succ\u2081 m \u2238 n)\n \u2261\u27e8 \u00b7-leftCong (sym $ beta (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) n) \u27e9\n (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y))) \u00b7 n \u00b7 (succ\u2081 m \u2238 n)\n \u2261\u27e8 sym $ rec-S n (succ\u2081 m) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y))) \u27e9\n rec (succ\u2081 n) (succ\u2081 m) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)))\n \u2261\u27e8 refl \u27e9\n succ\u2081 m \u2238 succ\u2081 n \u220e\n\n*-0x : \u2200 n \u2192 zero * n \u2261 zero\n*-0x n = rec zero zero (lam (\u03bb _ \u2192 lam (\u03bb y \u2192 n + y))) \u2261\u27e8 rec-0 zero \u27e9\n zero \u220e\n\n*-Sx : \u2200 m n \u2192 succ\u2081 m * n \u2261 n + m * n\n*-Sx m n =\n rec (succ\u2081 m) zero (lam (\u03bb _ \u2192 lam (\u03bb y \u2192 n + y)))\n \u2261\u27e8 rec-S m zero (lam (\u03bb _ \u2192 lam (\u03bb y \u2192 n + y))) \u27e9\n (lam (\u03bb _ \u2192 lam (\u03bb y \u2192 n + y))) \u00b7 m \u00b7 (m * n)\n \u2261\u27e8 \u00b7-leftCong (beta (\u03bb _ \u2192 lam (\u03bb y \u2192 n + y)) m) \u27e9\n (\u03bb _ \u2192 lam (\u03bb y \u2192 n + y)) m \u00b7 (m * n)\n \u2261\u27e8 refl \u27e9\n lam (\u03bb y \u2192 n + y) \u00b7 (m * n)\n \u2261\u27e8 beta (\u03bb y \u2192 n + y) (m * n) \u27e9\n (\u03bb y \u2192 n + y) (m * n)\n \u2261\u27e8 refl \u27e9\n n + (m * n) \u220e\n\n\u2238-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m \u2238 n)\n\u2238-N {m} Nm zN = subst N (sym $ \u2238-x0 m) Nm\n\u2238-N zN (sN Nn) = subst N (sym $ \u2238-0S Nn) zN\n\u2238-N (sN Nm) (sN Nn) = subst N (sym $ \u2238-SS Nm Nn) (\u2238-N Nm Nn)\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity = +-0x\n\n+-rightIdentity : \u2200 {n} \u2192 N n \u2192 n + zero \u2261 n\n+-rightIdentity zN = +-leftIdentity zero\n+-rightIdentity (sN {n} Nn) =\n trans (+-Sx n zero)\n (subst (\u03bb t \u2192 succ\u2081 (n + zero) \u2261 succ\u2081 t) (+-rightIdentity Nn) refl)\n\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {n = n} zN Nn = subst N (sym $ +-leftIdentity n) Nn\n+-N {n = n} (sN {m} Nm) Nn = subst N (sym $ +-Sx m n) (sN (+-N Nm Nn))\n\n+-assoc : \u2200 {m} \u2192 N m \u2192 \u2200 n o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc zN n o =\n zero + n + o \u2261\u27e8 subst (\u03bb t \u2192 zero + n + o \u2261 t + o) (+-leftIdentity n) refl \u27e9\n n + o \u2261\u27e8 sym $ +-leftIdentity (n + o) \u27e9\n zero + (n + o) \u220e\n\n+-assoc (sN {m} Nm) n o =\n succ\u2081 m + n + o\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 m + n + o \u2261 t + o) (+-Sx m n) refl \u27e9\n succ\u2081 (m + n) + o\n \u2261\u27e8 +-Sx (m + n) o \u27e9\n succ\u2081 (m + n + o)\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 (m + n + o) \u2261 succ\u2081 t) (+-assoc Nm n o) refl \u27e9\n succ\u2081 (m + (n + o))\n \u2261\u27e8 sym $ +-Sx m (n + o) \u27e9\n succ\u2081 m + (n + o) \u220e\n\nx+Sy\u2261S[x+y] : \u2200 {m} \u2192 N m \u2192 \u2200 n \u2192 m + succ\u2081 n \u2261 succ\u2081 (m + n)\nx+Sy\u2261S[x+y] zN n =\n zero + succ\u2081 n \u2261\u27e8 +-0x (succ\u2081 n) \u27e9\n succ\u2081 n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 n \u2261 succ\u2081 t) (sym $ +-leftIdentity n) refl \u27e9\n succ\u2081 (zero + n) \u220e\n\nx+Sy\u2261S[x+y] (sN {m} Nm) n =\n succ\u2081 m + succ\u2081 n\n \u2261\u27e8 +-Sx m (succ\u2081 n) \u27e9\n succ\u2081 (m + succ\u2081 n)\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 (m + succ\u2081 n) \u2261 succ\u2081 t) (x+Sy\u2261S[x+y] Nm n) refl \u27e9\n succ\u2081 (succ\u2081 (m + n))\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 (succ\u2081 (m + n)) \u2261 succ\u2081 t) (sym $ +-Sx m n) refl \u27e9\n succ\u2081 (succ\u2081 m + n) \u220e\n\n[x+y]\u2238[x+z]\u2261y\u2238z : \u2200 {m n o} \u2192 N m \u2192 N n \u2192 N o \u2192 (m + n) \u2238 (m + o) \u2261 n \u2238 o\n[x+y]\u2238[x+z]\u2261y\u2238z {n = n} {o} zN _ _ =\n (zero + n) \u2238 (zero + o)\n \u2261\u27e8 subst (\u03bb t \u2192 (zero + n) \u2238 (zero + o) \u2261 t \u2238 (zero + o)) (+-0x n) refl \u27e9\n n \u2238 (zero + o)\n \u2261\u27e8 subst (\u03bb t \u2192 n \u2238 (zero + o) \u2261 n \u2238 t) (+-0x o) refl \u27e9\n n \u2238 o \u220e\n\n[x+y]\u2238[x+z]\u2261y\u2238z {n = n} {o} (sN {m} Nm) Nn No =\n (succ\u2081 m + n) \u2238 (succ\u2081 m + o)\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 m + n \u2238 (succ\u2081 m + o) \u2261 t \u2238 (succ\u2081 m + o))\n (+-Sx m n)\n refl\n \u27e9\n succ\u2081 (m + n) \u2238 (succ\u2081 m + o)\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 (m + n) \u2238 (succ\u2081 m + o) \u2261 succ\u2081 (m + n) \u2238 t)\n (+-Sx m o)\n refl\n \u27e9\n succ\u2081 (m + n) \u2238 succ\u2081 (m + o)\n \u2261\u27e8 \u2238-SS (+-N Nm Nn) (+-N Nm No) \u27e9\n (m + n) \u2238 (m + o)\n \u2261\u27e8 [x+y]\u2238[x+z]\u2261y\u2238z Nm Nn No \u27e9\n n \u2238 o \u220e\n\n+-comm : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m + n \u2261 n + m\n+-comm {n = n} zN Nn =\n zero + n \u2261\u27e8 +-leftIdentity n \u27e9\n n \u2261\u27e8 sym $ +-rightIdentity Nn \u27e9\n n + zero \u220e\n\n+-comm {n = n} (sN {m} Nm) Nn =\n succ\u2081 m + n \u2261\u27e8 +-Sx m n \u27e9\n succ\u2081 (m + n) \u2261\u27e8 cong succ\u2081 (+-comm Nm Nn) \u27e9\n succ\u2081 (n + m) \u2261\u27e8 sym $ x+Sy\u2261S[x+y] Nn m \u27e9\n n + succ\u2081 m \u220e\n\n*-leftZero : \u2200 n \u2192 zero * n \u2261 zero\n*-leftZero = *-0x\n\n*-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m * n)\n*-N {n = n} zN _ = subst N (sym $ *-leftZero n) zN\n*-N {n = n} (sN {m} Nm) Nn = subst N (sym $ *-Sx m n) (+-N Nn (*-N Nm Nn))\n\n*-rightZero : \u2200 {n} \u2192 N n \u2192 n * zero \u2261 zero\n*-rightZero zN = *-leftZero zero\n*-rightZero (sN {n} Nn) =\n trans (*-Sx n zero)\n (trans (+-leftIdentity (n * zero)) (*-rightZero Nn))\n\n*-leftIdentity : \u2200 {n} \u2192 N n \u2192 succ\u2081 zero * n \u2261 n\n*-leftIdentity {n} Nn =\n succ\u2081 zero * n \u2261\u27e8 *-Sx zero n \u27e9\n n + zero * n \u2261\u27e8 subst (\u03bb t \u2192 n + zero * n \u2261 n + t) (*-leftZero n) refl \u27e9\n n + zero \u2261\u27e8 +-rightIdentity Nn \u27e9\n n \u220e\n\nx*Sy\u2261x+xy : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m * succ\u2081 n \u2261 m + m * n\nx*Sy\u2261x+xy {n = n} zN _ = sym\n ( zero + zero * n\n \u2261\u27e8 subst (\u03bb t \u2192 zero + zero * n \u2261 zero + t) (*-leftZero n) refl \u27e9\n zero + zero\n \u2261\u27e8 +-leftIdentity zero \u27e9\n zero\n \u2261\u27e8 sym $ *-leftZero (succ\u2081 n) \u27e9\n zero * succ\u2081 n \u220e\n )\n\nx*Sy\u2261x+xy {n = n} (sN {m} Nm) Nn =\n succ\u2081 m * succ\u2081 n\n \u2261\u27e8 *-Sx m (succ\u2081 n) \u27e9\n succ\u2081 n + m * succ\u2081 n\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 n + m * succ\u2081 n \u2261 succ\u2081 n + t)\n (x*Sy\u2261x+xy Nm Nn)\n refl\n \u27e9\n succ\u2081 n + (m + m * n)\n \u2261\u27e8 +-Sx n (m + m * n) \u27e9\n succ\u2081 (n + (m + m * n))\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 (n + (m + m * n)) \u2261 succ\u2081 t)\n (sym $ +-assoc Nn m (m * n))\n refl\n \u27e9\n succ\u2081 (n + m + m * n)\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 (n + m + m * n) \u2261 succ\u2081 (t + m * n))\n (+-comm Nn Nm)\n refl\n \u27e9\n succ\u2081 (m + n + m * n)\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 (m + n + m * n) \u2261 succ\u2081 t)\n (+-assoc Nm n (m * n))\n refl\n \u27e9\n succ\u2081 (m + (n + m * n))\n \u2261\u27e8 sym $ +-Sx m (n + m * n) \u27e9\n succ\u2081 m + (n + m * n)\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 m + (n + m * n) \u2261 succ\u2081 m + t)\n (sym $ *-Sx m n)\n refl\n \u27e9\n succ\u2081 m + succ\u2081 m * n \u220e\n\n*-comm : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m * n \u2261 n * m\n*-comm {n = n} zN Nn = trans (*-leftZero n) (sym $ *-rightZero Nn)\n*-comm {n = n} (sN {m} Nm) Nn =\n succ\u2081 m * n \u2261\u27e8 *-Sx m n \u27e9\n n + m * n \u2261\u27e8 subst (\u03bb t \u2192 n + m * n \u2261 n + t) (*-comm Nm Nn) refl \u27e9\n n + n * m \u2261\u27e8 sym $ x*Sy\u2261x+xy Nn Nm \u27e9\n n * succ\u2081 m \u220e\n\n*\u2238-leftDistributive : \u2200 {m n o} \u2192 N m \u2192 N n \u2192 N o \u2192 (m \u2238 n) * o \u2261 m * o \u2238 n * o\n*\u2238-leftDistributive {m} {o = o} _ zN _ =\n (m \u2238 zero) * o \u2261\u27e8 subst (\u03bb t \u2192 (m \u2238 zero) * o \u2261 t * o) (\u2238-x0 m) refl \u27e9\n m * o \u2261\u27e8 sym $ \u2238-x0 (m * o) \u27e9\n m * o \u2238 zero \u2261\u27e8 subst (\u03bb t \u2192 m * o \u2238 zero \u2261 m * o \u2238 t) (sym $ *-0x o) refl \u27e9\n m * o \u2238 zero * o \u220e\n\n*\u2238-leftDistributive {o = o} zN (sN {n} Nn) No =\n (zero \u2238 succ\u2081 n) * o\n \u2261\u27e8 subst (\u03bb t \u2192 (zero \u2238 succ\u2081 n) * o \u2261 t * o) (\u2238-0S Nn) refl \u27e9\n zero * o\n \u2261\u27e8 *-0x o \u27e9\n zero\n \u2261\u27e8 sym $ \u2238-0x (*-N (sN Nn) No) \u27e9\n zero \u2238 succ\u2081 n * o\n \u2261\u27e8 subst (\u03bb t \u2192 zero \u2238 succ\u2081 n * o \u2261 t \u2238 succ\u2081 n * o)\n (sym $ *-0x o)\n refl\n \u27e9\n zero * o \u2238 succ\u2081 n * o \u220e\n\n*\u2238-leftDistributive (sN {m} Nm) (sN {n} Nn) zN =\n (succ\u2081 m \u2238 succ\u2081 n) * zero\n \u2261\u27e8 *-comm (\u2238-N (sN Nm) (sN Nn)) zN \u27e9\n zero * (succ\u2081 m \u2238 succ\u2081 n)\n \u2261\u27e8 *-0x (succ\u2081 m \u2238 succ\u2081 n) \u27e9\n zero\n \u2261\u27e8 sym $ \u2238-0x (*-N (sN Nn) zN) \u27e9\n zero \u2238 succ\u2081 n * zero\n \u2261\u27e8 subst (\u03bb t \u2192 zero \u2238 succ\u2081 n * zero \u2261 t \u2238 succ\u2081 n * zero)\n (sym $ *-0x (succ\u2081 m))\n refl\n \u27e9\n zero * succ\u2081 m \u2238 succ\u2081 n * zero\n \u2261\u27e8 subst (\u03bb t \u2192 zero * succ\u2081 m \u2238 succ\u2081 n * zero \u2261 t \u2238 succ\u2081 n * zero)\n (*-comm zN (sN Nm))\n refl\n \u27e9\n succ\u2081 m * zero \u2238 succ\u2081 n * zero \u220e\n\n*\u2238-leftDistributive (sN {m} Nm) (sN {n} Nn) (sN {o} No) =\n (succ\u2081 m \u2238 succ\u2081 n) * succ\u2081 o\n \u2261\u27e8 subst (\u03bb t \u2192 (succ\u2081 m \u2238 succ\u2081 n) * succ\u2081 o \u2261 t * succ\u2081 o)\n (\u2238-SS Nm Nn)\n refl\n \u27e9\n (m \u2238 n) * succ\u2081 o\n \u2261\u27e8 *\u2238-leftDistributive Nm Nn (sN No) \u27e9\n m * succ\u2081 o \u2238 n * succ\u2081 o\n \u2261\u27e8 sym $ [x+y]\u2238[x+z]\u2261y\u2238z (sN No) (*-N Nm (sN No)) (*-N Nn (sN No)) \u27e9\n (succ\u2081 o + m * succ\u2081 o) \u2238 (succ\u2081 o + n * succ\u2081 o)\n \u2261\u27e8 subst (\u03bb t \u2192 (succ\u2081 o + m * succ\u2081 o) \u2238 (succ\u2081 o + n * succ\u2081 o) \u2261\n t \u2238 (succ\u2081 o + n * succ\u2081 o))\n (sym $ *-Sx m (succ\u2081 o))\n refl\n \u27e9\n (succ\u2081 m * succ\u2081 o) \u2238 (succ\u2081 o + n * succ\u2081 o)\n \u2261\u27e8 subst (\u03bb t \u2192 (succ\u2081 m * succ\u2081 o) \u2238 (succ\u2081 o + n * succ\u2081 o) \u2261\n (succ\u2081 m * succ\u2081 o) \u2238 t)\n (sym $ *-Sx n (succ\u2081 o))\n refl\n \u27e9\n (succ\u2081 m * succ\u2081 o) \u2238 (succ\u2081 n * succ\u2081 o) \u220e\n\n*+-leftDistributive : \u2200 {m n o} \u2192 N m \u2192 N n \u2192 N o \u2192 (m + n) * o \u2261 m * o + n * o\n*+-leftDistributive {m} {n} Nm Nn zN =\n (m + n) * zero \u2261\u27e8 *-comm (+-N Nm Nn) zN \u27e9\n zero * (m + n) \u2261\u27e8 *-0x (m + n) \u27e9\n zero \u2261\u27e8 sym $ *-0x m \u27e9\n zero * m \u2261\u27e8 *-comm zN Nm \u27e9\n m * zero \u2261\u27e8 sym $ +-rightIdentity (*-N Nm zN) \u27e9\n m * zero + zero \u2261\u27e8 subst (\u03bb t \u2192 m * zero + zero \u2261 m * zero + t)\n (trans (sym $ *-0x n) (*-comm zN Nn))\n refl\n \u27e9\n m * zero + n * zero \u220e\n\n*+-leftDistributive {n = n} zN Nn (sN {o} No) =\n (zero + n) * succ\u2081 o\n \u2261\u27e8 subst (\u03bb t \u2192 (zero + n) * succ\u2081 o \u2261 t * succ\u2081 o)\n (+-leftIdentity n)\n refl\n \u27e9\n n * succ\u2081 o\n \u2261\u27e8 sym $ +-leftIdentity (n * succ\u2081 o) \u27e9\n zero + n * succ\u2081 o\n \u2261\u27e8 subst (\u03bb t \u2192 zero + n * succ\u2081 o \u2261 t + n * succ\u2081 o)\n (sym $ *-0x (succ\u2081 o))\n refl\n \u27e9\n zero * succ\u2081 o + n * succ\u2081 o \u220e\n\n*+-leftDistributive (sN {m} Nm) zN (sN {o} No) =\n (succ\u2081 m + zero) * succ\u2081 o\n \u2261\u27e8 subst (\u03bb t \u2192 (succ\u2081 m + zero) * succ\u2081 o \u2261 t * succ\u2081 o)\n (+-rightIdentity (sN Nm))\n refl\n \u27e9\n succ\u2081 m * succ\u2081 o\n \u2261\u27e8 sym $ +-rightIdentity (*-N (sN Nm) (sN No)) \u27e9\n succ\u2081 m * succ\u2081 o + zero\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 m * succ\u2081 o + zero \u2261 succ\u2081 m * succ\u2081 o + t)\n (sym $ *-leftZero (succ\u2081 o))\n refl\n \u27e9\n succ\u2081 m * succ\u2081 o + zero * succ\u2081 o \u220e\n\n*+-leftDistributive (sN {m} Nm) (sN {n} Nn) (sN {o} No) =\n (succ\u2081 m + succ\u2081 n) * succ\u2081 o\n \u2261\u27e8 subst (\u03bb t \u2192 (succ\u2081 m + succ\u2081 n) * succ\u2081 o \u2261 t * succ\u2081 o)\n (+-Sx m (succ\u2081 n))\n refl\n \u27e9\n succ\u2081 (m + succ\u2081 n) * succ\u2081 o\n \u2261\u27e8 *-Sx (m + succ\u2081 n) (succ\u2081 o) \u27e9\n succ\u2081 o + (m + succ\u2081 n) * succ\u2081 o\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 o + (m + succ\u2081 n) * succ\u2081 o \u2261 succ\u2081 o + t)\n (*+-leftDistributive Nm (sN Nn) (sN No))\n refl\n \u27e9\n succ\u2081 o + (m * succ\u2081 o + succ\u2081 n * succ\u2081 o)\n \u2261\u27e8 sym $ +-assoc (sN No) (m * succ\u2081 o) (succ\u2081 n * succ\u2081 o) \u27e9\n succ\u2081 o + m * succ\u2081 o + succ\u2081 n * succ\u2081 o\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 o + m * succ\u2081 o + succ\u2081 n * succ\u2081 o \u2261\n t + succ\u2081 n * succ\u2081 o)\n (sym $ *-Sx m (succ\u2081 o))\n refl\n \u27e9\n succ\u2081 m * succ\u2081 o + succ\u2081 n * succ\u2081 o \u220e\n","old_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule LTC-PCF.Data.Nat.PropertiesI where\n\nopen import Common.FOL.Relation.Binary.EqReasoning\nopen import Common.Function\n\nopen import LTC-PCF.Base\nopen import LTC-PCF.Base.Properties\nopen import LTC-PCF.Data.Nat\nopen import LTC-PCF.Data.Nat.Rec\nopen import LTC-PCF.Data.Nat.Rec.EquationsI\n\n------------------------------------------------------------------------------\n-- Congruence properties\n\n+-leftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 m + o \u2261 n + o\n+-leftCong refl = refl\n\n+-rightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 m + n \u2261 m + o\n+-rightCong refl = refl\n\n\u2238-leftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 m \u2238 o \u2261 n \u2238 o\n\u2238-leftCong refl = refl\n\n\u2238-rightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 m \u2238 n \u2261 m \u2238 o\n\u2238-rightCong refl = refl\n\n*-leftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 m * o \u2261 n * o\n*-leftCong refl = refl\n\n*-rightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 m * n \u2261 m * o\n*-rightCong refl = refl\n\n------------------------------------------------------------------------------\n\n+-0x : \u2200 n \u2192 zero + n \u2261 n\n+-0x n = rec zero n _ \u2261\u27e8 rec-0 n \u27e9\n n \u220e\n\n+-Sx : \u2200 m n \u2192 succ\u2081 m + n \u2261 succ\u2081 (m + n)\n+-Sx m n =\n rec (succ\u2081 m) n (lam (\u03bb x \u2192 lam (\u03bb y \u2192 succ\u2081 y)))\n \u2261\u27e8 rec-S m n (lam (\u03bb x \u2192 lam (\u03bb y \u2192 succ\u2081 y))) \u27e9\n (lam (\u03bb x \u2192 lam (\u03bb y \u2192 succ\u2081 y))) \u00b7 m \u00b7 (m + n)\n \u2261\u27e8 \u00b7-leftCong (beta (\u03bb x \u2192 lam (\u03bb y \u2192 succ\u2081 y)) m) \u27e9\n (\u03bb x \u2192 lam (\u03bb y \u2192 succ\u2081 y)) m \u00b7 (m + n)\n \u2261\u27e8 refl \u27e9\n lam succ\u2081 \u00b7 (m + n)\n \u2261\u27e8 beta succ\u2081 (m + n) \u27e9\n succ\u2081 (m + n) \u220e\n\n\u2238-x0 : \u2200 n \u2192 n \u2238 zero \u2261 n\n\u2238-x0 n = rec zero n _ \u2261\u27e8 rec-0 n \u27e9\n n \u220e\n\n\u2238-0S : \u2200 {n} \u2192 N n \u2192 zero \u2238 succ\u2081 n \u2261 zero\n\u2238-0S zN =\n rec (succ\u2081 zero) zero (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)))\n \u2261\u27e8 rec-S zero zero (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y))) \u27e9\n lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) \u00b7 zero \u00b7 (zero \u2238 zero)\n \u2261\u27e8 \u00b7-leftCong (beta (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) zero) \u27e9\n (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) zero \u00b7 (zero \u2238 zero)\n \u2261\u27e8 refl \u27e9\n lam pred\u2081 \u00b7 (zero \u2238 zero)\n \u2261\u27e8 beta pred\u2081 (zero \u2238 zero) \u27e9\n pred\u2081 (zero \u2238 zero)\n \u2261\u27e8 cong pred\u2081 (\u2238-x0 zero) \u27e9\n pred\u2081 zero\n \u2261\u27e8 pred-0 \u27e9\n zero \u220e\n\n\u2238-0S (sN {n} Nn) =\n rec (succ\u2081 (succ\u2081 n)) zero (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)))\n \u2261\u27e8 rec-S (succ\u2081 n) zero (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y))) \u27e9\n lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) \u00b7 (succ\u2081 n) \u00b7 (zero \u2238 (succ\u2081 n))\n \u2261\u27e8 \u00b7-leftCong (beta (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) (succ\u2081 n)) \u27e9\n (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) (succ\u2081 n) \u00b7 (zero \u2238 (succ\u2081 n))\n \u2261\u27e8 refl \u27e9\n lam pred\u2081 \u00b7 (zero \u2238 (succ\u2081 n))\n \u2261\u27e8 beta pred\u2081 (zero \u2238 (succ\u2081 n)) \u27e9\n pred\u2081 (zero \u2238 (succ\u2081 n))\n \u2261\u27e8 cong pred\u2081 (\u2238-0S Nn) \u27e9\n pred\u2081 zero\n \u2261\u27e8 pred-0 \u27e9\n zero \u220e\n\n\u2238-0x : \u2200 {n} \u2192 N n \u2192 zero \u2238 n \u2261 zero\n\u2238-0x zN = \u2238-x0 zero\n\u2238-0x (sN Nn) = \u2238-0S Nn\n\n\u2238-SS : \u2200 {m n} \u2192 N m \u2192 N n \u2192 succ\u2081 m \u2238 succ\u2081 n \u2261 m \u2238 n\n\u2238-SS {m} _ zN =\n rec (succ\u2081 zero) (succ\u2081 m) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)))\n \u2261\u27e8 rec-S zero (succ\u2081 m) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y))) \u27e9\n lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) \u00b7 zero \u00b7 (succ\u2081 m \u2238 zero)\n \u2261\u27e8 \u00b7-leftCong (beta (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) zero) \u27e9\n (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) zero \u00b7 (succ\u2081 m \u2238 zero)\n \u2261\u27e8 refl \u27e9\n lam pred\u2081 \u00b7 (succ\u2081 m \u2238 zero)\n \u2261\u27e8 beta pred\u2081 (succ\u2081 m \u2238 zero) \u27e9\n pred\u2081 (succ\u2081 m \u2238 zero)\n \u2261\u27e8 cong pred\u2081 (\u2238-x0 (succ\u2081 m)) \u27e9\n pred\u2081 (succ\u2081 m)\n \u2261\u27e8 pred-S m \u27e9\n m\n \u2261\u27e8 sym $ \u2238-x0 m \u27e9\n m \u2238 zero \u220e\n\n\u2238-SS zN (sN {n} Nn) =\n rec (succ\u2081 (succ\u2081 n)) (succ\u2081 zero) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)))\n \u2261\u27e8 rec-S (succ\u2081 n) (succ\u2081 zero) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y))) \u27e9\n lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) \u00b7 (succ\u2081 n) \u00b7 (succ\u2081 zero \u2238 succ\u2081 n)\n \u2261\u27e8 \u00b7-leftCong (beta (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) (succ\u2081 n)) \u27e9\n (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) (succ\u2081 n) \u00b7 (succ\u2081 zero \u2238 succ\u2081 n)\n \u2261\u27e8 refl \u27e9\n lam pred\u2081 \u00b7 (succ\u2081 zero \u2238 succ\u2081 n)\n \u2261\u27e8 beta pred\u2081 (succ\u2081 zero \u2238 succ\u2081 n) \u27e9\n pred\u2081 (succ\u2081 zero \u2238 succ\u2081 n)\n \u2261\u27e8 cong pred\u2081 (\u2238-SS zN Nn) \u27e9\n pred\u2081 (zero \u2238 n)\n \u2261\u27e8 cong pred\u2081 (\u2238-0x Nn) \u27e9\n pred\u2081 zero\n \u2261\u27e8 pred-0 \u27e9\n zero\n \u2261\u27e8 sym $ \u2238-0S Nn \u27e9\n zero \u2238 succ\u2081 n \u220e\n\n\u2238-SS (sN {m} Nm) (sN {n} Nn) =\n rec (succ\u2081 (succ\u2081 n)) (succ\u2081 (succ\u2081 m)) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)))\n \u2261\u27e8 rec-S (succ\u2081 n) (succ\u2081 (succ\u2081 m)) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y))) \u27e9\n lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) \u00b7 (succ\u2081 n) \u00b7 (succ\u2081 (succ\u2081 m) \u2238 succ\u2081 n)\n \u2261\u27e8 \u00b7-leftCong (beta (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) (succ\u2081 n)) \u27e9\n (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) (succ\u2081 n) \u00b7 (succ\u2081 (succ\u2081 m) \u2238 succ\u2081 n)\n \u2261\u27e8 refl \u27e9\n lam pred\u2081 \u00b7 (succ\u2081 (succ\u2081 m) \u2238 succ\u2081 n)\n \u2261\u27e8 beta pred\u2081 (succ\u2081 (succ\u2081 m) \u2238 succ\u2081 n) \u27e9\n pred\u2081 (succ\u2081 (succ\u2081 m) \u2238 succ\u2081 n)\n \u2261\u27e8 cong pred\u2081 (\u2238-SS (sN Nm) Nn) \u27e9\n pred\u2081 (succ\u2081 m \u2238 n)\n \u2261\u27e8 sym $ beta pred\u2081 (succ\u2081 m \u2238 n) \u27e9\n lam pred\u2081 \u00b7 (succ\u2081 m \u2238 n)\n \u2261\u27e8 refl \u27e9\n (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) n \u00b7 (succ\u2081 m \u2238 n)\n \u2261\u27e8 \u00b7-leftCong (sym $ beta (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)) n) \u27e9\n (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y))) \u00b7 n \u00b7 (succ\u2081 m \u2238 n)\n \u2261\u27e8 sym $ rec-S n (succ\u2081 m) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y))) \u27e9\n rec (succ\u2081 n) (succ\u2081 m) (lam (\u03bb x \u2192 lam (\u03bb y \u2192 pred\u2081 y)))\n \u2261\u27e8 refl \u27e9\n succ\u2081 m \u2238 succ\u2081 n \u220e\n\n*-0x : \u2200 n \u2192 zero * n \u2261 zero\n*-0x n = rec zero zero (lam (\u03bb _ \u2192 lam (\u03bb y \u2192 n + y))) \u2261\u27e8 rec-0 zero \u27e9\n zero \u220e\n\n*-Sx : \u2200 m n \u2192 succ\u2081 m * n \u2261 n + m * n\n*-Sx m n =\n rec (succ\u2081 m) zero (lam (\u03bb _ \u2192 lam (\u03bb y \u2192 n + y)))\n \u2261\u27e8 rec-S m zero (lam (\u03bb _ \u2192 lam (\u03bb y \u2192 n + y))) \u27e9\n (lam (\u03bb _ \u2192 lam (\u03bb y \u2192 n + y))) \u00b7 m \u00b7 (m * n)\n \u2261\u27e8 \u00b7-leftCong (beta (\u03bb _ \u2192 lam (\u03bb y \u2192 n + y)) m) \u27e9\n (\u03bb _ \u2192 lam (\u03bb y \u2192 n + y)) m \u00b7 (m * n)\n \u2261\u27e8 refl \u27e9\n lam (\u03bb y \u2192 n + y) \u00b7 (m * n)\n \u2261\u27e8 beta (\u03bb y \u2192 n + y) (m * n) \u27e9\n (\u03bb y \u2192 n + y) (m * n)\n \u2261\u27e8 refl \u27e9\n n + (m * n) \u220e\n\n\u2238-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m \u2238 n)\n\u2238-N {m} Nm zN = subst N (sym $ \u2238-x0 m) Nm\n\u2238-N zN (sN Nn) = subst N (sym $ \u2238-0S Nn) zN\n\u2238-N (sN Nm) (sN Nn) = subst N (sym $ \u2238-SS Nm Nn) (\u2238-N Nm Nn)\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity = +-0x\n\n+-rightIdentity : \u2200 {n} \u2192 N n \u2192 n + zero \u2261 n\n+-rightIdentity zN = +-leftIdentity zero\n+-rightIdentity (sN {n} Nn) =\n trans (+-Sx n zero)\n (subst (\u03bb t \u2192 succ\u2081 (n + zero) \u2261 succ\u2081 t) (+-rightIdentity Nn) refl)\n\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {n = n} zN Nn = subst N (sym $ +-leftIdentity n) Nn\n+-N {n = n} (sN {m} Nm) Nn = subst N (sym $ +-Sx m n) (sN (+-N Nm Nn))\n\n+-assoc : \u2200 {m} \u2192 N m \u2192 \u2200 n o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc zN n o =\n zero + n + o \u2261\u27e8 subst (\u03bb t \u2192 zero + n + o \u2261 t + o) (+-leftIdentity n) refl \u27e9\n n + o \u2261\u27e8 sym $ +-leftIdentity (n + o) \u27e9\n zero + (n + o) \u220e\n\n+-assoc (sN {m} Nm) n o =\n succ\u2081 m + n + o\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 m + n + o \u2261 t + o) (+-Sx m n) refl \u27e9\n succ\u2081 (m + n) + o\n \u2261\u27e8 +-Sx (m + n) o \u27e9\n succ\u2081 (m + n + o)\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 (m + n + o) \u2261 succ\u2081 t) (+-assoc Nm n o) refl \u27e9\n succ\u2081 (m + (n + o))\n \u2261\u27e8 sym $ +-Sx m (n + o) \u27e9\n succ\u2081 m + (n + o) \u220e\n\nx+Sy\u2261S[x+y] : \u2200 {m} \u2192 N m \u2192 \u2200 n \u2192 m + succ\u2081 n \u2261 succ\u2081 (m + n)\nx+Sy\u2261S[x+y] zN n =\n zero + succ\u2081 n \u2261\u27e8 +-0x (succ\u2081 n) \u27e9\n succ\u2081 n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 n \u2261 succ\u2081 t) (sym $ +-leftIdentity n) refl \u27e9\n succ\u2081 (zero + n) \u220e\n\nx+Sy\u2261S[x+y] (sN {m} Nm) n =\n succ\u2081 m + succ\u2081 n\n \u2261\u27e8 +-Sx m (succ\u2081 n) \u27e9\n succ\u2081 (m + succ\u2081 n)\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 (m + succ\u2081 n) \u2261 succ\u2081 t) (x+Sy\u2261S[x+y] Nm n) refl \u27e9\n succ\u2081 (succ\u2081 (m + n))\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 (succ\u2081 (m + n)) \u2261 succ\u2081 t) (sym $ +-Sx m n) refl \u27e9\n succ\u2081 (succ\u2081 m + n) \u220e\n\n[x+y]\u2238[x+z]\u2261y\u2238z : \u2200 {m n o} \u2192 N m \u2192 N n \u2192 N o \u2192 (m + n) \u2238 (m + o) \u2261 n \u2238 o\n[x+y]\u2238[x+z]\u2261y\u2238z {n = n} {o} zN _ _ =\n (zero + n) \u2238 (zero + o)\n \u2261\u27e8 subst (\u03bb t \u2192 (zero + n) \u2238 (zero + o) \u2261 t \u2238 (zero + o)) (+-0x n) refl \u27e9\n n \u2238 (zero + o)\n \u2261\u27e8 subst (\u03bb t \u2192 n \u2238 (zero + o) \u2261 n \u2238 t) (+-0x o) refl \u27e9\n n \u2238 o \u220e\n\n[x+y]\u2238[x+z]\u2261y\u2238z {n = n} {o} (sN {m} Nm) Nn No =\n (succ\u2081 m + n) \u2238 (succ\u2081 m + o)\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 m + n \u2238 (succ\u2081 m + o) \u2261 t \u2238 (succ\u2081 m + o))\n (+-Sx m n)\n refl\n \u27e9\n succ\u2081 (m + n) \u2238 (succ\u2081 m + o)\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 (m + n) \u2238 (succ\u2081 m + o) \u2261 succ\u2081 (m + n) \u2238 t)\n (+-Sx m o)\n refl\n \u27e9\n succ\u2081 (m + n) \u2238 succ\u2081 (m + o)\n \u2261\u27e8 \u2238-SS (+-N Nm Nn) (+-N Nm No) \u27e9\n (m + n) \u2238 (m + o)\n \u2261\u27e8 [x+y]\u2238[x+z]\u2261y\u2238z Nm Nn No \u27e9\n n \u2238 o \u220e\n\n+-comm : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m + n \u2261 n + m\n+-comm {n = n} zN Nn =\n zero + n \u2261\u27e8 +-leftIdentity n \u27e9\n n \u2261\u27e8 sym $ +-rightIdentity Nn \u27e9\n n + zero \u220e\n\n+-comm {n = n} (sN {m} Nm) Nn =\n succ\u2081 m + n \u2261\u27e8 +-Sx m n \u27e9\n succ\u2081 (m + n) \u2261\u27e8 cong succ\u2081 (+-comm Nm Nn) \u27e9\n succ\u2081 (n + m) \u2261\u27e8 sym $ x+Sy\u2261S[x+y] Nn m \u27e9\n n + succ\u2081 m \u220e\n\n*-leftZero : \u2200 n \u2192 zero * n \u2261 zero\n*-leftZero = *-0x\n\n*-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m * n)\n*-N {n = n} zN _ = subst N (sym $ *-leftZero n) zN\n*-N {n = n} (sN {m} Nm) Nn = subst N (sym $ *-Sx m n) (+-N Nn (*-N Nm Nn))\n\n*-rightZero : \u2200 {n} \u2192 N n \u2192 n * zero \u2261 zero\n*-rightZero zN = *-leftZero zero\n*-rightZero (sN {n} Nn) =\n trans (*-Sx n zero)\n (trans (+-leftIdentity (n * zero)) (*-rightZero Nn))\n\n*-leftIdentity : \u2200 {n} \u2192 N n \u2192 succ\u2081 zero * n \u2261 n\n*-leftIdentity {n} Nn =\n succ\u2081 zero * n \u2261\u27e8 *-Sx zero n \u27e9\n n + zero * n \u2261\u27e8 subst (\u03bb t \u2192 n + zero * n \u2261 n + t) (*-leftZero n) refl \u27e9\n n + zero \u2261\u27e8 +-rightIdentity Nn \u27e9\n n \u220e\n\nx*Sy\u2261x+xy : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m * succ\u2081 n \u2261 m + m * n\nx*Sy\u2261x+xy {n = n} zN _ = sym\n ( zero + zero * n\n \u2261\u27e8 subst (\u03bb t \u2192 zero + zero * n \u2261 zero + t) (*-leftZero n) refl \u27e9\n zero + zero\n \u2261\u27e8 +-leftIdentity zero \u27e9\n zero\n \u2261\u27e8 sym $ *-leftZero (succ\u2081 n) \u27e9\n zero * succ\u2081 n \u220e\n )\n\nx*Sy\u2261x+xy {n = n} (sN {m} Nm) Nn =\n succ\u2081 m * succ\u2081 n\n \u2261\u27e8 *-Sx m (succ\u2081 n) \u27e9\n succ\u2081 n + m * succ\u2081 n\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 n + m * succ\u2081 n \u2261 succ\u2081 n + t)\n (x*Sy\u2261x+xy Nm Nn)\n refl\n \u27e9\n succ\u2081 n + (m + m * n)\n \u2261\u27e8 +-Sx n (m + m * n) \u27e9\n succ\u2081 (n + (m + m * n))\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 (n + (m + m * n)) \u2261 succ\u2081 t)\n (sym $ +-assoc Nn m (m * n))\n refl\n \u27e9\n succ\u2081 (n + m + m * n)\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 (n + m + m * n) \u2261 succ\u2081 (t + m * n))\n (+-comm Nn Nm)\n refl\n \u27e9\n succ\u2081 (m + n + m * n)\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 (m + n + m * n) \u2261 succ\u2081 t)\n (+-assoc Nm n (m * n))\n refl\n \u27e9\n succ\u2081 (m + (n + m * n))\n \u2261\u27e8 sym $ +-Sx m (n + m * n) \u27e9\n succ\u2081 m + (n + m * n)\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 m + (n + m * n) \u2261 succ\u2081 m + t)\n (sym $ *-Sx m n)\n refl\n \u27e9\n succ\u2081 m + succ\u2081 m * n \u220e\n\n*-comm : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m * n \u2261 n * m\n*-comm {n = n} zN Nn = trans (*-leftZero n) (sym $ *-rightZero Nn)\n*-comm {n = n} (sN {m} Nm) Nn =\n succ\u2081 m * n \u2261\u27e8 *-Sx m n \u27e9\n n + m * n \u2261\u27e8 subst (\u03bb t \u2192 n + m * n \u2261 n + t) (*-comm Nm Nn) refl \u27e9\n n + n * m \u2261\u27e8 sym $ x*Sy\u2261x+xy Nn Nm \u27e9\n n * succ\u2081 m \u220e\n\n*\u2238-leftDistributive : \u2200 {m n o} \u2192 N m \u2192 N n \u2192 N o \u2192 (m \u2238 n) * o \u2261 m * o \u2238 n * o\n*\u2238-leftDistributive {m} {o = o} _ zN _ =\n (m \u2238 zero) * o \u2261\u27e8 subst (\u03bb t \u2192 (m \u2238 zero) * o \u2261 t * o) (\u2238-x0 m) refl \u27e9\n m * o \u2261\u27e8 sym $ \u2238-x0 (m * o) \u27e9\n m * o \u2238 zero \u2261\u27e8 subst (\u03bb t \u2192 m * o \u2238 zero \u2261 m * o \u2238 t) (sym $ *-0x o) refl \u27e9\n m * o \u2238 zero * o \u220e\n\n*\u2238-leftDistributive {o = o} zN (sN {n} Nn) No =\n (zero \u2238 succ\u2081 n) * o\n \u2261\u27e8 subst (\u03bb t \u2192 (zero \u2238 succ\u2081 n) * o \u2261 t * o) (\u2238-0S Nn) refl \u27e9\n zero * o\n \u2261\u27e8 *-0x o \u27e9\n zero\n \u2261\u27e8 sym $ \u2238-0x (*-N (sN Nn) No) \u27e9\n zero \u2238 succ\u2081 n * o\n \u2261\u27e8 subst (\u03bb t \u2192 zero \u2238 succ\u2081 n * o \u2261 t \u2238 succ\u2081 n * o)\n (sym $ *-0x o)\n refl\n \u27e9\n zero * o \u2238 succ\u2081 n * o \u220e\n\n*\u2238-leftDistributive (sN {m} Nm) (sN {n} Nn) zN =\n (succ\u2081 m \u2238 succ\u2081 n) * zero\n \u2261\u27e8 *-comm (\u2238-N (sN Nm) (sN Nn)) zN \u27e9\n zero * (succ\u2081 m \u2238 succ\u2081 n)\n \u2261\u27e8 *-0x (succ\u2081 m \u2238 succ\u2081 n) \u27e9\n zero\n \u2261\u27e8 sym $ \u2238-0x (*-N (sN Nn) zN) \u27e9\n zero \u2238 succ\u2081 n * zero\n \u2261\u27e8 subst (\u03bb t \u2192 zero \u2238 succ\u2081 n * zero \u2261 t \u2238 succ\u2081 n * zero)\n (sym $ *-0x (succ\u2081 m))\n refl\n \u27e9\n zero * succ\u2081 m \u2238 succ\u2081 n * zero\n \u2261\u27e8 subst (\u03bb t \u2192 zero * succ\u2081 m \u2238 succ\u2081 n * zero \u2261 t \u2238 succ\u2081 n * zero)\n (*-comm zN (sN Nm))\n refl\n \u27e9\n succ\u2081 m * zero \u2238 succ\u2081 n * zero \u220e\n\n*\u2238-leftDistributive (sN {m} Nm) (sN {n} Nn) (sN {o} No) =\n (succ\u2081 m \u2238 succ\u2081 n) * succ\u2081 o\n \u2261\u27e8 subst (\u03bb t \u2192 (succ\u2081 m \u2238 succ\u2081 n) * succ\u2081 o \u2261 t * succ\u2081 o)\n (\u2238-SS Nm Nn)\n refl\n \u27e9\n (m \u2238 n) * succ\u2081 o\n \u2261\u27e8 *\u2238-leftDistributive Nm Nn (sN No) \u27e9\n m * succ\u2081 o \u2238 n * succ\u2081 o\n \u2261\u27e8 sym $ [x+y]\u2238[x+z]\u2261y\u2238z (sN No) (*-N Nm (sN No)) (*-N Nn (sN No)) \u27e9\n (succ\u2081 o + m * succ\u2081 o) \u2238 (succ\u2081 o + n * succ\u2081 o)\n \u2261\u27e8 subst (\u03bb t \u2192 (succ\u2081 o + m * succ\u2081 o) \u2238 (succ\u2081 o + n * succ\u2081 o) \u2261\n t \u2238 (succ\u2081 o + n * succ\u2081 o))\n (sym $ *-Sx m (succ\u2081 o))\n refl\n \u27e9\n (succ\u2081 m * succ\u2081 o) \u2238 (succ\u2081 o + n * succ\u2081 o)\n \u2261\u27e8 subst (\u03bb t \u2192 (succ\u2081 m * succ\u2081 o) \u2238 (succ\u2081 o + n * succ\u2081 o) \u2261\n (succ\u2081 m * succ\u2081 o) \u2238 t)\n (sym $ *-Sx n (succ\u2081 o))\n refl\n \u27e9\n (succ\u2081 m * succ\u2081 o) \u2238 (succ\u2081 n * succ\u2081 o) \u220e\n\n*+-leftDistributive : \u2200 {m n o} \u2192 N m \u2192 N n \u2192 N o \u2192 (m + n) * o \u2261 m * o + n * o\n*+-leftDistributive {m} {n} Nm Nn zN =\n (m + n) * zero \u2261\u27e8 *-comm (+-N Nm Nn) zN \u27e9\n zero * (m + n) \u2261\u27e8 *-0x (m + n) \u27e9\n zero \u2261\u27e8 sym $ *-0x m \u27e9\n zero * m \u2261\u27e8 *-comm zN Nm \u27e9\n m * zero \u2261\u27e8 sym $ +-rightIdentity (*-N Nm zN) \u27e9\n m * zero + zero \u2261\u27e8 subst (\u03bb t \u2192 m * zero + zero \u2261 m * zero + t)\n (trans (sym $ *-0x n) (*-comm zN Nn))\n refl\n \u27e9\n m * zero + n * zero \u220e\n\n*+-leftDistributive {n = n} zN Nn (sN {o} No) =\n (zero + n) * succ\u2081 o\n \u2261\u27e8 subst (\u03bb t \u2192 (zero + n) * succ\u2081 o \u2261 t * succ\u2081 o)\n (+-leftIdentity n)\n refl\n \u27e9\n n * succ\u2081 o\n \u2261\u27e8 sym $ +-leftIdentity (n * succ\u2081 o) \u27e9\n zero + n * succ\u2081 o\n \u2261\u27e8 subst (\u03bb t \u2192 zero + n * succ\u2081 o \u2261 t + n * succ\u2081 o)\n (sym $ *-0x (succ\u2081 o))\n refl\n \u27e9\n zero * succ\u2081 o + n * succ\u2081 o \u220e\n\n*+-leftDistributive (sN {m} Nm) zN (sN {o} No) =\n (succ\u2081 m + zero) * succ\u2081 o\n \u2261\u27e8 subst (\u03bb t \u2192 (succ\u2081 m + zero) * succ\u2081 o \u2261 t * succ\u2081 o)\n (+-rightIdentity (sN Nm))\n refl\n \u27e9\n succ\u2081 m * succ\u2081 o\n \u2261\u27e8 sym $ +-rightIdentity (*-N (sN Nm) (sN No)) \u27e9\n succ\u2081 m * succ\u2081 o + zero\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 m * succ\u2081 o + zero \u2261 succ\u2081 m * succ\u2081 o + t)\n (sym $ *-leftZero (succ\u2081 o))\n refl\n \u27e9\n succ\u2081 m * succ\u2081 o + zero * succ\u2081 o \u220e\n\n*+-leftDistributive (sN {m} Nm) (sN {n} Nn) (sN {o} No) =\n (succ\u2081 m + succ\u2081 n) * succ\u2081 o\n \u2261\u27e8 subst (\u03bb t \u2192 (succ\u2081 m + succ\u2081 n) * succ\u2081 o \u2261 t * succ\u2081 o)\n (+-Sx m (succ\u2081 n))\n refl\n \u27e9\n succ\u2081 (m + succ\u2081 n) * succ\u2081 o\n \u2261\u27e8 *-Sx (m + succ\u2081 n) (succ\u2081 o) \u27e9\n succ\u2081 o + (m + succ\u2081 n) * succ\u2081 o\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 o + (m + succ\u2081 n) * succ\u2081 o \u2261 succ\u2081 o + t)\n (*+-leftDistributive Nm (sN Nn) (sN No))\n refl\n \u27e9\n succ\u2081 o + (m * succ\u2081 o + succ\u2081 n * succ\u2081 o)\n \u2261\u27e8 sym $ +-assoc (sN No) (m * succ\u2081 o) (succ\u2081 n * succ\u2081 o) \u27e9\n succ\u2081 o + m * succ\u2081 o + succ\u2081 n * succ\u2081 o\n \u2261\u27e8 subst (\u03bb t \u2192 succ\u2081 o + m * succ\u2081 o + succ\u2081 n * succ\u2081 o \u2261\n t + succ\u2081 n * succ\u2081 o)\n (sym $ *-Sx m (succ\u2081 o))\n refl\n \u27e9\n succ\u2081 m * succ\u2081 o + succ\u2081 n * succ\u2081 o \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"25e15c0f23c0b4a2589c85d5401ff2f55c2d7735","subject":"Defined uncurry.","message":"Defined uncurry.\n","repos":"heades\/AUGL","old_file":"dialectica-cats\/DC2Sets.agda","new_file":"dialectica-cats\/DC2Sets.agda","new_contents":"-----------------------------------------------------------------------\n-- This file defines DC\u2082(Sets) and its SMC structure. --\n-----------------------------------------------------------------------\nmodule DC2Sets where\n\nopen import prelude\n\n-- The objects:\nObj : Set\u2081\nObj = \u03a3[ U \u2208 Set ] (\u03a3[ X \u2208 Set ] (U \u2192 X \u2192 Set))\n\n-- The morphisms:\nHom : Obj \u2192 Obj \u2192 Set\nHom (U , X , \u03b1) (V , Y , \u03b2) =\n \u03a3[ f \u2208 (U \u2192 V) ]\n (\u03a3[ F \u2208 (U \u2192 Y \u2192 X) ] (\u2200{u : U}{y : Y} \u2192 \u03b1 u (F u y) \u2192 \u03b2 (f u) y))\n\n-- Composition:\ncomp : {A B C : Obj} \u2192 Hom A B \u2192 Hom B C \u2192 Hom A C\ncomp {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)} (f , F , p\u2081) (g , G , p\u2082) =\n (g \u2218 f , (\u03bb u z \u2192 F u (G (f u) z)), (\u03bb {u} {y} p\u2083 \u2192 p\u2082 (p\u2081 p\u2083)))\n\ninfixl 5 _\u25cb_\n\n_\u25cb_ = comp\n\n-- The contravariant hom-functor:\nHom\u2090 : {A' A B B' : Obj} \u2192 Hom A' A \u2192 Hom B B' \u2192 Hom A B \u2192 Hom A' B'\nHom\u2090 f h g = comp f (comp g h)\n\n-- The identity function:\nid : {A : Obj} \u2192 Hom A A \nid {(U , V , \u03b1)} = (id-set , curry snd , id-set)\n\n-- In this formalization we will only worry about proving that the\n-- data of morphisms are equivalent, and not worry about the morphism\n-- conditions. This will make proofs shorter and faster.\n--\n-- If we have parallel morphisms (f,F) and (g,G) in which we know that\n-- f = g and F = G, then the condition for (f,F) will imply the\n-- condition of (g,G) and vice versa. Thus, we can safely ignore it.\ninfix 4 _\u2261h_\n\n_\u2261h_ : {A B : Obj} \u2192 (f g : Hom A B) \u2192 Set\n_\u2261h_ {(U , X , \u03b1)}{(V , Y , \u03b2)} (f , F , p\u2081) (g , G , p\u2082) = f \u2261 g \u00d7 F \u2261 G\n\n\u2261h-refl : {A B : Obj}{f : Hom A B} \u2192 f \u2261h f\n\u2261h-refl {U , X , \u03b1}{V , Y , \u03b2}{f , F , _} = refl , refl\n\n\u2261h-trans : \u2200{A B}{f g h : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h h \u2192 f \u2261h h\n\u2261h-trans {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _}{h , H , _} (p\u2081 , p\u2082) (p\u2083 , p\u2084) rewrite p\u2081 | p\u2082 | p\u2083 | p\u2084 = refl , refl\n\n\u2261h-sym : \u2200{A B}{f g : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h f\n\u2261h-sym {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\n\u2261h-subst-\u25cb : \u2200{A B C}{f\u2081 f\u2082 : Hom A B}{g\u2081 g\u2082 : Hom B C}{j : Hom A C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 g\u2081 \u2261h g\u2082\n \u2192 f\u2082 \u25cb g\u2082 \u2261h j\n \u2192 f\u2081 \u25cb g\u2081 \u2261h j\n\u2261h-subst-\u25cb {U , X , \u03b1}\n {V , Y , \u03b2}\n {W , Z , \u03b3}\n {f\u2081 , F\u2081 , _}\n {f\u2082 , F\u2082 , _}\n {g\u2081 , G\u2081 , _}\n {g\u2082 , G\u2082 , _}\n {j , J , _}\n (p\u2085 , p\u2086) (p\u2087 , p\u2088) (p\u2089 , p\u2081\u2080) rewrite p\u2085 | p\u2086 | p\u2087 | p\u2088 | p\u2089 | p\u2081\u2080 = refl , refl\n\n\u25cb-assoc : \u2200{A B C D}{f : Hom A B}{g : Hom B C}{h : Hom C D}\n \u2192 f \u25cb (g \u25cb h) \u2261h (f \u25cb g) \u25cb h\n\u25cb-assoc {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{S , T , \u03b9}\n {f , F , _}{g , G , _}{h , H , _} = refl , refl\n\n\u25cb-idl : \u2200{A B}{f : Hom A B} \u2192 id \u25cb f \u2261h f\n\u25cb-idl {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\u25cb-idr : \u2200{A B}{f : Hom A B} \u2192 f \u25cb id \u2261h f\n\u25cb-idr {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\n-----------------------------------------------------------------------\n-- DC\u2082(Sets) is a SMC --\n-----------------------------------------------------------------------\n\n-- The tensor functor: \u2297\n_\u2297\u1d63_ : \u2200{U X V Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 ((U \u00d7 V) \u2192 (X \u00d7 Y) \u2192 Set)\n_\u2297\u1d63_ \u03b1 \u03b2 (u , v) (x , y) = (\u03b1 u x) \u00d7 (\u03b2 v y)\n\n_\u2297\u2092_ : (A B : Obj) \u2192 Obj\n(U , X , \u03b1) \u2297\u2092 (V , Y , \u03b2) = ((U \u00d7 V) , (X \u00d7 Y) , \u03b1 \u2297\u1d63 \u03b2)\n\nF\u2297 : \u2200{Z T V X U Y : Set}{F : U \u2192 Z \u2192 X}{G : V \u2192 T \u2192 Y} \u2192 (U \u00d7 V) \u2192 (Z \u00d7 T) \u2192 (X \u00d7 Y)\nF\u2297 {F = F}{G} (u , v) (z , t) = F u z , G v t\n \n_\u2297\u2090_ : {A B C D : Obj} \u2192 Hom A C \u2192 Hom B D \u2192 Hom (A \u2297\u2092 B) (C \u2297\u2092 D)\n_\u2297\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) = \u27e8 f , g \u27e9 , F\u2297 {F = F}{G} , p\u2297\n where\n p\u2297 : {u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Z (\u03bb x \u2192 T)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (F\u2297 {F = F}{G} u y) \u2192 (\u03b3 \u2297\u1d63 \u03b4) (\u27e8 f , g \u27e9 u) y\n p\u2297 {u , v}{z , t} (p\u2083 , p\u2084) = p\u2081 p\u2083 , p\u2082 p\u2084\n\n\n-- The unit for tensor:\n\u03b9 : \u22a4 \u2192 \u22a4 \u2192 Set\n\u03b9 triv triv = \u22a4\n\nI : Obj\nI = (\u22a4 , \u22a4 , \u03b9)\n\nJ : Obj\nJ = (\u22a4 , \u22a4 , (\u03bb x y \u2192 \u22a5))\n\n\n-- The left-unitor:\n\u03bb\u2297-p : \u2200{U X \u03b1}{u : \u03a3 \u22a4 (\u03bb x \u2192 U)} {y : X} \u2192 (\u03b9 \u2297\u1d63 \u03b1) u (triv , y) \u2192 \u03b1 (snd u) y\n\u03bb\u2297-p {U}{X}{\u03b1}{(triv , u)}{x} = snd\n \n\u03bb\u2297 : \u2200{A : Obj} \u2192 Hom (I \u2297\u2092 A) A\n\u03bb\u2297 {(U , X , \u03b1)} = snd , (\u03bb _ x \u2192 triv , x) , \u03bb\u2297-p\n\n\u03bb\u2297-inv : \u2200{A : Obj} \u2192 Hom A (I \u2297\u2092 A)\n\u03bb\u2297-inv {(U , X , \u03b1)} = (\u03bb u \u2192 triv , u) , (\u03bb _ r \u2192 snd r) , \u03bb\u2297-inv-p\n where\n \u03bb\u2297-inv-p : \u2200{U X \u03b1}{u : U} {y : \u03a3 \u22a4 (\u03bb x \u2192 X)} \u2192 \u03b1 u (snd y) \u2192 (\u03b9 \u2297\u1d63 \u03b1) (triv , u) y\n \u03bb\u2297-inv-p {U}{X}{\u03b1}{u}{triv , x} p = triv , p\n\n-- The right-unitor:\n\u03c1\u2297 : \u2200{A : Obj} \u2192 Hom (A \u2297\u2092 I) A\n\u03c1\u2297 {(U , X , \u03b1)} = fst , (\u03bb r x \u2192 x , triv) , \u03c1\u2297-p\n where\n \u03c1\u2297-p : \u2200{U X \u03b1}{u : \u03a3 U (\u03bb x \u2192 \u22a4)} {y : X} \u2192 (\u03b1 \u2297\u1d63 \u03b9) u (y , triv) \u2192 \u03b1 (fst u) y\n \u03c1\u2297-p {U}{X}{\u03b1}{(u , _)}{x} (p , _) = p\n\n\n\u03c1\u2297-inv : \u2200{A : Obj} \u2192 Hom A (A \u2297\u2092 I)\n\u03c1\u2297-inv {(U , X , \u03b1)} = (\u03bb u \u2192 u , triv) , (\u03bb u r \u2192 fst r) , \u03c1\u2297-p-inv\n where\n \u03c1\u2297-p-inv : \u2200{U X \u03b1}{u : U} {y : \u03a3 X (\u03bb x \u2192 \u22a4)} \u2192 \u03b1 u (fst y) \u2192 (\u03b1 \u2297\u1d63 \u03b9) (u , triv) y\n \u03c1\u2297-p-inv {U}{X}{\u03b1}{u}{x , triv} p = p , triv\n\n-- Symmetry:\n\u03b2\u2297 : \u2200{A B : Obj} \u2192 Hom (A \u2297\u2092 B) (B \u2297\u2092 A)\n\u03b2\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)} = twist-\u00d7 , (\u03bb r\u2081 r\u2082 \u2192 twist-\u00d7 r\u2082) , \u03b2\u2297-p\n where\n \u03b2\u2297-p : \u2200{U V Y X \u03b1 \u03b2}{u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Y (\u03bb x \u2192 X)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (twist-\u00d7 y) \u2192 (\u03b2 \u2297\u1d63 \u03b1) (twist-\u00d7 u) y\n \u03b2\u2297-p {U}{V}{Y}{X}{\u03b1}{\u03b2}{u , v}{y , x} = twist-\u00d7\n\n\n-- The associator:\nF\u03b1-inv : \u2200{\u2113}{U V W X Y Z : Set \u2113} \u2192 (U \u00d7 (V \u00d7 W)) \u2192 ((X \u00d7 Y) \u00d7 Z) \u2192 (X \u00d7 (Y \u00d7 Z))\nF\u03b1-inv (u , (v , w)) ((x , y) , z) = x , y , z\n \n\u03b1\u2297-inv : \u2200{A B C : Obj} \u2192 Hom (A \u2297\u2092 (B \u2297\u2092 C)) ((A \u2297\u2092 B) \u2297\u2092 C)\n\u03b1\u2297-inv {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = rl-assoc-\u00d7 , F\u03b1-inv , \u03b1-inv-cond\n where\n \u03b1-inv-cond : {u : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))}{y : \u03a3 (\u03a3 X (\u03bb x \u2192 Y)) (\u03bb x \u2192 Z)}\n \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) u (F\u03b1-inv u y)\n \u2192 ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) (rl-assoc-\u00d7 u) y\n \u03b1-inv-cond {u , (v , w)}{(x , y) , z} (p\u2081 , (p\u2082 , p\u2083)) = (p\u2081 , p\u2082) , p\u2083\n\n\nF\u03b1 : \u2200{V W X Y U Z : Set} \u2192 ((U \u00d7 V) \u00d7 W) \u2192 (X \u00d7 (Y \u00d7 Z)) \u2192 ((X \u00d7 Y) \u00d7 Z)\nF\u03b1 {V}{W}{X}{Y}{U}{Z} ((u , v) , w) (x , (y , z)) = (x , y) , z\n\n\u03b1\u2297 : \u2200{A B C : Obj} \u2192 Hom ((A \u2297\u2092 B) \u2297\u2092 C) (A \u2297\u2092 (B \u2297\u2092 C)) \n\u03b1\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = (lr-assoc-\u00d7 , F\u03b1 , \u03b1-cond)\n where\n \u03b1-cond : {u : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)}\n {y : \u03a3 X (\u03bb x \u2192 \u03a3 Y (\u03bb x\u2081 \u2192 Z))} \u2192\n ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) u (F\u03b1 u y) \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) (lr-assoc-\u00d7 u) y\n \u03b1-cond {(u , v) , w}{x , (y , z)} ((p\u2081 , p\u2082) , p\u2083) = p\u2081 , p\u2082 , p\u2083\n\n\u03b1\u2297-id\u2081 : \u2200{A B C} \u2192 (\u03b1\u2297 {A}{B}{C}) \u25cb \u03b1\u2297-inv \u2261h id\n\u03b1\u2297-id\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)} \u2192 rl-assoc-\u00d7 (lr-assoc-\u00d7 a) \u2261 a\n aux {(u , v) , w} = refl\n\n aux' : {a : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)} \u2192 (\u03bb z \u2192 F\u03b1 {V}{W}{X}{Y}{U}{Z} a (F\u03b1-inv (lr-assoc-\u00d7 a) z)) \u2261 (\u03bb y \u2192 y)\n aux' {(u , v), w} = ext-set aux''\n where\n aux'' : {a : \u03a3 (\u03a3 X (\u03bb x \u2192 Y)) (\u03bb x \u2192 Z)} \u2192 F\u03b1 ((u , v) , w) (F\u03b1-inv (u , v , w) a) \u2261 a\n aux'' {(x , y) , z} = refl\n\n\u03b1\u2297-id\u2082 : \u2200{A B C} \u2192 (\u03b1\u2297-inv {A}{B}{C}) \u25cb \u03b1\u2297 \u2261h id\n\u03b1\u2297-id\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))} \u2192 lr-assoc-\u00d7 (rl-assoc-\u00d7 a) \u2261 a\n aux {u , (v , w)} = refl\n aux' : {a : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))} \u2192 (\u03bb z \u2192 F\u03b1-inv {_}{U}{V}{W}{X}{Y}{Z} a (F\u03b1 (rl-assoc-\u00d7 a) z)) \u2261 (\u03bb y \u2192 y)\n aux' {u , (v , w)} = ext-set aux''\n where\n aux'' : {a : \u03a3 X (\u03bb x \u2192 \u03a3 Y (\u03bb x\u2081 \u2192 Z))} \u2192 F\u03b1-inv (u , v , w) (F\u03b1 ((u , v) , w) a) \u2261 a\n aux'' {x , (y , z)} = refl\n \n-- Internal hom:\n\u22b8-cond : \u2200{U V X Y : Set}{\u03b1 : U \u2192 X \u2192 Set}{\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 \u03a3 (U \u2192 V) (\u03bb x \u2192 U \u2192 Y \u2192 X)\n \u2192 \u03a3 U (\u03bb x \u2192 Y)\n \u2192 Set\n\u22b8-cond {\u03b1 = \u03b1}{\u03b2} (f , F) (u , y) = \u03b1 u (F u y) \u2192 \u03b2 (f u) y\n\n_\u22b8\u2092_ : Obj \u2192 Obj \u2192 Obj\n(U , X , \u03b1) \u22b8\u2092 (V , Y , \u03b2) = ((U \u2192 V) \u00d7 (U \u2192 Y \u2192 X)) , ((U \u00d7 Y) , \u22b8-cond {\u03b1 = \u03b1}{\u03b2})\n\n\n_\u22b8\u2090_ : {A B C D : Obj} \u2192 Hom C A \u2192 Hom B D \u2192 Hom (A \u22b8\u2092 B) (C \u22b8\u2092 D)\n_\u22b8\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) =\n h , H , cond\n where\n h : \u03a3 (U \u2192 V) (\u03bb x \u2192 U \u2192 Y \u2192 X) \u2192 \u03a3 (W \u2192 S) (\u03bb x \u2192 W \u2192 T \u2192 Z)\n h (i , I) = (\u03bb w \u2192 g (i (f w))) , (\u03bb w t \u2192 F w (I (f w) (G (i (f w)) t)))\n H : \u03a3 (U \u2192 V) (\u03bb x \u2192 U \u2192 Y \u2192 X) \u2192 \u03a3 W (\u03bb x \u2192 T) \u2192 \u03a3 U (\u03bb x \u2192 Y)\n H (i , I) (w , t) = f w , G (i (f w)) t\n cond : {u : \u03a3 (U \u2192 V) (\u03bb x \u2192 U \u2192 Y \u2192 X)} {y : \u03a3 W (\u03bb x \u2192 T)} \u2192 \u22b8-cond {\u03b1 = \u03b1}{\u03b2} u (H u y) \u2192 \u22b8-cond {\u03b1 = \u03b3}{\u03b4} (h u) y\n cond {i , I}{w , y} p\u2083 p\u2084 = p\u2082 (p\u2083 (p\u2081 p\u2084))\n\ncur : {A B C : Obj}\n \u2192 Hom (A \u2297\u2092 B) C\n \u2192 Hom A (B \u22b8\u2092 C)\ncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb u \u2192 (\u03bb v \u2192 f (u , v)) , (\u03bb v z \u2192 snd (F (u , v) z))) , (\u03bb u r \u2192 fst (F (u , (fst r)) (snd r))) , cond \n where\n cond : {u : U} {y : \u03a3 V (\u03bb x \u2192 Z)}\n \u2192 \u03b1 u (fst (F (u , fst y) (snd y)))\n \u2192 \u22b8-cond {\u03b1 = \u03b2}{\u03b3} ((\u03bb v \u2192 f (u , v)) , (\u03bb v z \u2192 snd (F (u , v) z))) y \n cond {u}{v , z} p\u2082 p\u2083 with (p\u2081 {u , v}{z})\n ... | p\u2084 with F (u , v) z\n ... | (x , y) = p\u2084 (p\u2082 , p\u2083)\n \n\ncur-\u2261h : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-\u2261h {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}\n {f\u2081 , F\u2081 , p\u2081}{f\u2082 , F\u2082 , p\u2082} (p\u2083 , p\u2084)\n rewrite p\u2083 | p\u2084 = refl , refl\n\ncur-cong : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C} \u2192 f\u2081 \u2261h f\u2082 \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-cong {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)}{f\u2081 , F\u2081 , _}{f\u2082 , F\u2082 , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\n\nuncur : {A B C : Obj}\n \u2192 Hom A (B \u22b8\u2092 C)\n \u2192 Hom (A \u2297\u2092 B) C\nuncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = h , (H , cond)\n where\n h : \u03a3 U (\u03bb x \u2192 V) \u2192 W\n h (u , v) with f u\n ... | i , I = i v\n H : \u03a3 U (\u03bb x \u2192 V) \u2192 Z \u2192 \u03a3 X (\u03bb x \u2192 Y)\n H (u , v) z with f u\n ... | i , I = F u (v , z) , I v z\n cond : {u : \u03a3 U (\u03bb x \u2192 V)} {z : Z} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (H u z) \u2192 \u03b3 (h u) z\n cond {u , v}{z} r with (p\u2081 {u}{v , z})\n ... | p\u2084 with f u\n ... | i , I with r\n ... | p\u2082 , p\u2083 = p\u2084 p\u2082 p\u2083\n \n{-\ncur-uncur-bij\u2081 : \u2200{A B C}{f : Hom (A \u2297\u2092 B) C}\n \u2192 uncur (cur f) \u2261h f\ncur-uncur-bij\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{f , F , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : \u03a3 U (\u03bb x \u2192 V)} \u2192 f (fst a , snd a) \u2261 f a\n aux\u2081 {u , v} = refl\n \n aux\u2082 : {a : Z} \u2192 ((\u03bb v \u2192 fst (F a) v) , (\u03bb u \u2192 snd (F a) u)) \u2261 F a\n aux\u2082 {z} with F z\n ... | j\u2081 , j\u2082 = refl\n\ncur-uncur-bij\u2082 : \u2200{A B C}{g : Hom A (B \u22b8\u2092 C)}\n \u2192 cur (uncur g) \u2261h g\ncur-uncur-bij\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{g , G , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : U} \u2192 ((\u03bb v \u2192 fst (g a) v) , (\u03bb z \u2192 snd (g a) z)) \u2261 g a\n aux\u2081 {u} with g u\n ... | (j\u2081 , j\u2082) = refl\n\n aux\u2082 : {a : \u03a3 V (\u03bb x \u2192 Z)} \u2192 G (fst a , snd a) \u2261 G a\n aux\u2082 {v , z} = refl\n \n-- The of-course exponential:\n!\u2092-cond : \u2200{U X : Set}\n \u2192 (U \u2192 X \u2192 Set)\n \u2192 U\n \u2192 (U \u2192 X *)\n \u2192 Set\n!\u2092-cond \u03b1 u f = all-pred (\u03b1 u) (f u)\n \n!\u2092 : Obj \u2192 Obj\n!\u2092 (U , X , \u03b1) = U , (U \u2192 X *) , !\u2092-cond \u03b1\n\n!-cta : {V Y U X : Set}\n \u2192 (Y \u2192 X)\n \u2192 (U \u2192 V)\n \u2192 (V \u2192 Y *)\n \u2192 (U \u2192 X *)\n!-cta F f g = \u03bb u \u2192 list-funct F (g (f u))\n\n!\u2090-cond : \u2200{U V Y X : Set}{F : Y \u2192 X}{f : U \u2192 V}\n \u2192 (\u03b1 : U \u2192 X \u2192 Set)\n \u2192 (\u03b2 : V \u2192 Y \u2192 Set)\n \u2192 (p : {u : U} {y : Y} \u2192 \u03b1 u (F y) \u2192 \u03b2 (f u) y)\n {u : U}{l : Y *}\n \u2192 all-pred (\u03b1 u) (list-funct F l)\n \u2192 all-pred (\u03b2 (f u)) l\n!\u2090-cond _ _ _ {l = []} _ = triv\n!\u2090-cond \u03b1 \u03b2 p {u}{x :: xs} (p' , p'') = p p' , !\u2090-cond \u03b1 \u03b2 p p'' \n \n!\u2090 : {A B : Obj} \u2192 Hom A B \u2192 Hom (!\u2092 A) (!\u2092 B)\n!\u2090 {U , X , \u03b1}{V , Y , \u03b2} (f , F , p) = f , !-cta F f , !\u2090-cond \u03b1 \u03b2 p\n\n-- Of-course is a comonad:\n\u03b5 : \u2200{A} \u2192 Hom (!\u2092 A) A\n\u03b5 {U , X , \u03b1} = id-set , (\u03bb x y \u2192 [ x ]) , fst\n\n\u03b4-cta : {U X : Set} \u2192 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X)) \u2192 U \u2192 \ud835\udd43 X\n\u03b4-cta g u = foldr (\u03bb f rest \u2192 (f u) ++ rest) [] (g u)\n \n\u03b4 : \u2200{A} \u2192 Hom (!\u2092 A) (!\u2092 (!\u2092 A))\n\u03b4 {U , X , \u03b1} = id-set , \u03b4-cta , \u03b4-cond\n where\n \u03b4-cond : {u : U} {l : \ud835\udd43 (U \u2192 \ud835\udd43 X)}\n \u2192 all-pred (\u03b1 u) (foldr (\u03bb f \u2192 _++_ (f u)) [] l)\n \u2192 all-pred (\u03bb f\n \u2192 all-pred (\u03b1 u) (f u)) l\n \u03b4-cond {l = []} _ = triv\n \u03b4-cond {u}{l = x :: l'} p with\n all-pred-append {X}{\u03b1 u}\n {x u}\n {foldr (\u03bb f \u2192 _++_ (f u)) [] l'}\n \u2227-unit \u2227-assoc\n ... | p' rewrite p' = fst p , \u03b4-cond {u} {l'} (snd p)\n\n-- These diagrams can be found on page 22 of the report.\ncomonand-diag\u2081 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (!\u2090 (\u03b4 {A})) \u2261h (\u03b4 {A}) \u25cb (\u03b4 { !\u2092 A})\ncomonand-diag\u2081 {U , X , \u03b1} =\n refl , ext-set (\u03bb {a} \u2192 ext-set (\u03bb {a\u2081} \u2192 aux {a\u2081}{a a\u2081}))\n where\n aux : \u2200{a\u2081 : U}{l : \ud835\udd43 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X))} \u2192\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) []\n (map (\u03bb g u \u2192 foldr (\u03bb f \u2192 _++_ (f u)) [] (g u)) l)\n \u2261\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] (foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] l)\n aux {a}{[]} = refl \n aux {a}{x :: l} rewrite\n sym (foldr-append {l\u2081 = x a}{foldr (\u03bb f \u2192 _++_ (f a)) [] l}{a})\n = cong2 {a = foldr (\u03bb f \u2192 _++_ (f a)) [] (x a)}\n _++_\n refl\n (aux {a}{l})\n\ncomonand-diag\u2082 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (\u03b5 { !\u2092 A}) \u2261h (\u03b4 {A}) \u25cb (!\u2090 (\u03b5 {A}))\ncomonand-diag\u2082 {U , X , \u03b1} =\n refl , ext-set (\u03bb {f} \u2192 ext-set (\u03bb {a} \u2192 aux {a}{f a}))\n where\n aux : \u2200{a : U}{l : X *}\n \u2192 l ++ [] \u2261 foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x y \u2192 x :: []) l)\n aux {a}{[]} = refl\n aux {a}{x :: l} with aux {a}{l}\n ... | IH rewrite ++[] l =\n cong2 {a = x} {x} {l}\n {foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x\u2081 y \u2192 x\u2081 :: []) l)} _::_ refl\n IH\n \nmodule Cartesian where\n \u03c0\u2081 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (U , X , \u03b1))\n \u03c0\u2081 {U}{X}{V}{Y}{\u03b1}{\u03b2} = fst , (\u03bb f \u2192 (\u03bb v u \u2192 f u) , (\u03bb u v \u2192 [])) , \u03c0\u2081-cond\n where\n \u03c0\u2081-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : U \u2192 \ud835\udd43 X} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 y u\u2081) , (\u03bb u\u2081 v \u2192 [])) \u2192\n all-pred (\u03b1 (fst u)) (y (fst u))\n \u03c0\u2081-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2081\n\n \u03c0\u2082 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (V , Y , \u03b2))\n \u03c0\u2082 {U}{X}{V}{Y}{\u03b1}{\u03b2} = snd , (\u03bb f \u2192 (\u03bb v u \u2192 []) , (\u03bb u v \u2192 f v)) , \u03c0\u2082-cond\n where\n \u03c0\u2082-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : V \u2192 \ud835\udd43 Y} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 []) , (\u03bb u\u2081 v \u2192 y v)) \u2192\n all-pred (\u03b2 (snd u)) (y (snd u))\n \u03c0\u2082-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2082\n\n cart-ar-crt : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y) \u2192 W \u2192 \ud835\udd43 Z\n cart-ar-crt (f , F , p\u2081) (g , G , p\u2082) (j\u2081 , j\u2082) w = F (j\u2081 (g w)) w ++ G (j\u2082 (f w)) w\n\n cart-ar : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))\n cart-ar {U}{X}{V}{Y}{W}{Z}{\u03b1}{\u03b2}{\u03b3} (f , F , p\u2081) (g , G , p\u2082)\n = (\u03bb w \u2192 f w , g w) , cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) , cart-ar-cond\n where\n cart-ar-cond : \u2200{u : W} {y : \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y)} \u2192\n all-pred (\u03b3 u) (cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) y u) \u2192\n ((\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b1 u\u2081) (f\u2081 u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b2 u\u2081) (f\u2081 u\u2081)))\n (f u , g u) y\n cart-ar-cond {w}{j\u2081 , j\u2082} p\n rewrite\n all-pred-append {f = \u03b3 w}{F (j\u2081 (g w)) w}{G (j\u2082 (f w)) w} \u2227-unit \u2227-assoc with p\n ... | (a , b) = p\u2081 a , p\u2082 b\n\n cart-diag\u2081 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (U , X , \u03b1)}\n (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (U , X , \u03b1)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2081)\n cart-diag\u2081 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 sym (++[] (map F (j (f w))))))\n\n cart-diag\u2082 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (V , Y , \u03b2)}\n (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (V , Y , \u03b2)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2082)\n cart-diag\u2082 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 refl))\n-}\n","old_contents":"-----------------------------------------------------------------------\n-- This file defines DC\u2082(Sets) and its SMC structure. --\n-----------------------------------------------------------------------\nmodule DC2Sets where\n\nopen import prelude\n\n-- The objects:\nObj : Set\u2081\nObj = \u03a3[ U \u2208 Set ] (\u03a3[ X \u2208 Set ] (U \u2192 X \u2192 Set))\n\n-- The morphisms:\nHom : Obj \u2192 Obj \u2192 Set\nHom (U , X , \u03b1) (V , Y , \u03b2) =\n \u03a3[ f \u2208 (U \u2192 V) ]\n (\u03a3[ F \u2208 (U \u2192 Y \u2192 X) ] (\u2200{u : U}{y : Y} \u2192 \u03b1 u (F u y) \u2192 \u03b2 (f u) y))\n\n-- Composition:\ncomp : {A B C : Obj} \u2192 Hom A B \u2192 Hom B C \u2192 Hom A C\ncomp {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)} (f , F , p\u2081) (g , G , p\u2082) =\n (g \u2218 f , (\u03bb u z \u2192 F u (G (f u) z)), (\u03bb {u} {y} p\u2083 \u2192 p\u2082 (p\u2081 p\u2083)))\n\ninfixl 5 _\u25cb_\n\n_\u25cb_ = comp\n\n-- The contravariant hom-functor:\nHom\u2090 : {A' A B B' : Obj} \u2192 Hom A' A \u2192 Hom B B' \u2192 Hom A B \u2192 Hom A' B'\nHom\u2090 f h g = comp f (comp g h)\n\n-- The identity function:\nid : {A : Obj} \u2192 Hom A A \nid {(U , V , \u03b1)} = (id-set , curry snd , id-set)\n\n-- In this formalization we will only worry about proving that the\n-- data of morphisms are equivalent, and not worry about the morphism\n-- conditions. This will make proofs shorter and faster.\n--\n-- If we have parallel morphisms (f,F) and (g,G) in which we know that\n-- f = g and F = G, then the condition for (f,F) will imply the\n-- condition of (g,G) and vice versa. Thus, we can safely ignore it.\ninfix 4 _\u2261h_\n\n_\u2261h_ : {A B : Obj} \u2192 (f g : Hom A B) \u2192 Set\n_\u2261h_ {(U , X , \u03b1)}{(V , Y , \u03b2)} (f , F , p\u2081) (g , G , p\u2082) = f \u2261 g \u00d7 F \u2261 G\n\n\u2261h-refl : {A B : Obj}{f : Hom A B} \u2192 f \u2261h f\n\u2261h-refl {U , X , \u03b1}{V , Y , \u03b2}{f , F , _} = refl , refl\n\n\u2261h-trans : \u2200{A B}{f g h : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h h \u2192 f \u2261h h\n\u2261h-trans {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _}{h , H , _} (p\u2081 , p\u2082) (p\u2083 , p\u2084) rewrite p\u2081 | p\u2082 | p\u2083 | p\u2084 = refl , refl\n\n\u2261h-sym : \u2200{A B}{f g : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h f\n\u2261h-sym {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\n\u2261h-subst-\u25cb : \u2200{A B C}{f\u2081 f\u2082 : Hom A B}{g\u2081 g\u2082 : Hom B C}{j : Hom A C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 g\u2081 \u2261h g\u2082\n \u2192 f\u2082 \u25cb g\u2082 \u2261h j\n \u2192 f\u2081 \u25cb g\u2081 \u2261h j\n\u2261h-subst-\u25cb {U , X , \u03b1}\n {V , Y , \u03b2}\n {W , Z , \u03b3}\n {f\u2081 , F\u2081 , _}\n {f\u2082 , F\u2082 , _}\n {g\u2081 , G\u2081 , _}\n {g\u2082 , G\u2082 , _}\n {j , J , _}\n (p\u2085 , p\u2086) (p\u2087 , p\u2088) (p\u2089 , p\u2081\u2080) rewrite p\u2085 | p\u2086 | p\u2087 | p\u2088 | p\u2089 | p\u2081\u2080 = refl , refl\n\n\u25cb-assoc : \u2200{A B C D}{f : Hom A B}{g : Hom B C}{h : Hom C D}\n \u2192 f \u25cb (g \u25cb h) \u2261h (f \u25cb g) \u25cb h\n\u25cb-assoc {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{S , T , \u03b9}\n {f , F , _}{g , G , _}{h , H , _} = refl , refl\n\n\u25cb-idl : \u2200{A B}{f : Hom A B} \u2192 id \u25cb f \u2261h f\n\u25cb-idl {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\u25cb-idr : \u2200{A B}{f : Hom A B} \u2192 f \u25cb id \u2261h f\n\u25cb-idr {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\n-----------------------------------------------------------------------\n-- DC\u2082(Sets) is a SMC --\n-----------------------------------------------------------------------\n\n-- The tensor functor: \u2297\n_\u2297\u1d63_ : \u2200{U X V Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 ((U \u00d7 V) \u2192 (X \u00d7 Y) \u2192 Set)\n_\u2297\u1d63_ \u03b1 \u03b2 (u , v) (x , y) = (\u03b1 u x) \u00d7 (\u03b2 v y)\n\n_\u2297\u2092_ : (A B : Obj) \u2192 Obj\n(U , X , \u03b1) \u2297\u2092 (V , Y , \u03b2) = ((U \u00d7 V) , (X \u00d7 Y) , \u03b1 \u2297\u1d63 \u03b2)\n\nF\u2297 : \u2200{Z T V X U Y : Set}{F : U \u2192 Z \u2192 X}{G : V \u2192 T \u2192 Y} \u2192 (U \u00d7 V) \u2192 (Z \u00d7 T) \u2192 (X \u00d7 Y)\nF\u2297 {F = F}{G} (u , v) (z , t) = F u z , G v t\n \n_\u2297\u2090_ : {A B C D : Obj} \u2192 Hom A C \u2192 Hom B D \u2192 Hom (A \u2297\u2092 B) (C \u2297\u2092 D)\n_\u2297\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) = \u27e8 f , g \u27e9 , F\u2297 {F = F}{G} , p\u2297\n where\n p\u2297 : {u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Z (\u03bb x \u2192 T)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (F\u2297 {F = F}{G} u y) \u2192 (\u03b3 \u2297\u1d63 \u03b4) (\u27e8 f , g \u27e9 u) y\n p\u2297 {u , v}{z , t} (p\u2083 , p\u2084) = p\u2081 p\u2083 , p\u2082 p\u2084\n\n\n-- The unit for tensor:\n\u03b9 : \u22a4 \u2192 \u22a4 \u2192 Set\n\u03b9 triv triv = \u22a4\n\nI : Obj\nI = (\u22a4 , \u22a4 , \u03b9)\n\nJ : Obj\nJ = (\u22a4 , \u22a4 , (\u03bb x y \u2192 \u22a5))\n\n\n-- The left-unitor:\n\u03bb\u2297-p : \u2200{U X \u03b1}{u : \u03a3 \u22a4 (\u03bb x \u2192 U)} {y : X} \u2192 (\u03b9 \u2297\u1d63 \u03b1) u (triv , y) \u2192 \u03b1 (snd u) y\n\u03bb\u2297-p {U}{X}{\u03b1}{(triv , u)}{x} = snd\n \n\u03bb\u2297 : \u2200{A : Obj} \u2192 Hom (I \u2297\u2092 A) A\n\u03bb\u2297 {(U , X , \u03b1)} = snd , (\u03bb _ x \u2192 triv , x) , \u03bb\u2297-p\n\n\u03bb\u2297-inv : \u2200{A : Obj} \u2192 Hom A (I \u2297\u2092 A)\n\u03bb\u2297-inv {(U , X , \u03b1)} = (\u03bb u \u2192 triv , u) , (\u03bb _ r \u2192 snd r) , \u03bb\u2297-inv-p\n where\n \u03bb\u2297-inv-p : \u2200{U X \u03b1}{u : U} {y : \u03a3 \u22a4 (\u03bb x \u2192 X)} \u2192 \u03b1 u (snd y) \u2192 (\u03b9 \u2297\u1d63 \u03b1) (triv , u) y\n \u03bb\u2297-inv-p {U}{X}{\u03b1}{u}{triv , x} p = triv , p\n\n-- The right-unitor:\n\u03c1\u2297 : \u2200{A : Obj} \u2192 Hom (A \u2297\u2092 I) A\n\u03c1\u2297 {(U , X , \u03b1)} = fst , (\u03bb r x \u2192 x , triv) , \u03c1\u2297-p\n where\n \u03c1\u2297-p : \u2200{U X \u03b1}{u : \u03a3 U (\u03bb x \u2192 \u22a4)} {y : X} \u2192 (\u03b1 \u2297\u1d63 \u03b9) u (y , triv) \u2192 \u03b1 (fst u) y\n \u03c1\u2297-p {U}{X}{\u03b1}{(u , _)}{x} (p , _) = p\n\n\n\u03c1\u2297-inv : \u2200{A : Obj} \u2192 Hom A (A \u2297\u2092 I)\n\u03c1\u2297-inv {(U , X , \u03b1)} = (\u03bb u \u2192 u , triv) , (\u03bb u r \u2192 fst r) , \u03c1\u2297-p-inv\n where\n \u03c1\u2297-p-inv : \u2200{U X \u03b1}{u : U} {y : \u03a3 X (\u03bb x \u2192 \u22a4)} \u2192 \u03b1 u (fst y) \u2192 (\u03b1 \u2297\u1d63 \u03b9) (u , triv) y\n \u03c1\u2297-p-inv {U}{X}{\u03b1}{u}{x , triv} p = p , triv\n\n-- Symmetry:\n\u03b2\u2297 : \u2200{A B : Obj} \u2192 Hom (A \u2297\u2092 B) (B \u2297\u2092 A)\n\u03b2\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)} = twist-\u00d7 , (\u03bb r\u2081 r\u2082 \u2192 twist-\u00d7 r\u2082) , \u03b2\u2297-p\n where\n \u03b2\u2297-p : \u2200{U V Y X \u03b1 \u03b2}{u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Y (\u03bb x \u2192 X)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (twist-\u00d7 y) \u2192 (\u03b2 \u2297\u1d63 \u03b1) (twist-\u00d7 u) y\n \u03b2\u2297-p {U}{V}{Y}{X}{\u03b1}{\u03b2}{u , v}{y , x} = twist-\u00d7\n\n\n-- The associator:\nF\u03b1-inv : \u2200{\u2113}{U V W X Y Z : Set \u2113} \u2192 (U \u00d7 (V \u00d7 W)) \u2192 ((X \u00d7 Y) \u00d7 Z) \u2192 (X \u00d7 (Y \u00d7 Z))\nF\u03b1-inv (u , (v , w)) ((x , y) , z) = x , y , z\n \n\u03b1\u2297-inv : \u2200{A B C : Obj} \u2192 Hom (A \u2297\u2092 (B \u2297\u2092 C)) ((A \u2297\u2092 B) \u2297\u2092 C)\n\u03b1\u2297-inv {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = rl-assoc-\u00d7 , F\u03b1-inv , \u03b1-inv-cond\n where\n \u03b1-inv-cond : {u : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))}{y : \u03a3 (\u03a3 X (\u03bb x \u2192 Y)) (\u03bb x \u2192 Z)}\n \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) u (F\u03b1-inv u y)\n \u2192 ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) (rl-assoc-\u00d7 u) y\n \u03b1-inv-cond {u , (v , w)}{(x , y) , z} (p\u2081 , (p\u2082 , p\u2083)) = (p\u2081 , p\u2082) , p\u2083\n\n\nF\u03b1 : \u2200{V W X Y U Z : Set} \u2192 ((U \u00d7 V) \u00d7 W) \u2192 (X \u00d7 (Y \u00d7 Z)) \u2192 ((X \u00d7 Y) \u00d7 Z)\nF\u03b1 {V}{W}{X}{Y}{U}{Z} ((u , v) , w) (x , (y , z)) = (x , y) , z\n\n\u03b1\u2297 : \u2200{A B C : Obj} \u2192 Hom ((A \u2297\u2092 B) \u2297\u2092 C) (A \u2297\u2092 (B \u2297\u2092 C)) \n\u03b1\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = (lr-assoc-\u00d7 , F\u03b1 , \u03b1-cond)\n where\n \u03b1-cond : {u : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)}\n {y : \u03a3 X (\u03bb x \u2192 \u03a3 Y (\u03bb x\u2081 \u2192 Z))} \u2192\n ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) u (F\u03b1 u y) \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) (lr-assoc-\u00d7 u) y\n \u03b1-cond {(u , v) , w}{x , (y , z)} ((p\u2081 , p\u2082) , p\u2083) = p\u2081 , p\u2082 , p\u2083\n\n\u03b1\u2297-id\u2081 : \u2200{A B C} \u2192 (\u03b1\u2297 {A}{B}{C}) \u25cb \u03b1\u2297-inv \u2261h id\n\u03b1\u2297-id\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)} \u2192 rl-assoc-\u00d7 (lr-assoc-\u00d7 a) \u2261 a\n aux {(u , v) , w} = refl\n\n aux' : {a : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)} \u2192 (\u03bb z \u2192 F\u03b1 {V}{W}{X}{Y}{U}{Z} a (F\u03b1-inv (lr-assoc-\u00d7 a) z)) \u2261 (\u03bb y \u2192 y)\n aux' {(u , v), w} = ext-set aux''\n where\n aux'' : {a : \u03a3 (\u03a3 X (\u03bb x \u2192 Y)) (\u03bb x \u2192 Z)} \u2192 F\u03b1 ((u , v) , w) (F\u03b1-inv (u , v , w) a) \u2261 a\n aux'' {(x , y) , z} = refl\n\n\u03b1\u2297-id\u2082 : \u2200{A B C} \u2192 (\u03b1\u2297-inv {A}{B}{C}) \u25cb \u03b1\u2297 \u2261h id\n\u03b1\u2297-id\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))} \u2192 lr-assoc-\u00d7 (rl-assoc-\u00d7 a) \u2261 a\n aux {u , (v , w)} = refl\n aux' : {a : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))} \u2192 (\u03bb z \u2192 F\u03b1-inv {_}{U}{V}{W}{X}{Y}{Z} a (F\u03b1 (rl-assoc-\u00d7 a) z)) \u2261 (\u03bb y \u2192 y)\n aux' {u , (v , w)} = ext-set aux''\n where\n aux'' : {a : \u03a3 X (\u03bb x \u2192 \u03a3 Y (\u03bb x\u2081 \u2192 Z))} \u2192 F\u03b1-inv (u , v , w) (F\u03b1 ((u , v) , w) a) \u2261 a\n aux'' {x , (y , z)} = refl\n \n-- Internal hom:\n\u22b8-cond : \u2200{U V X Y : Set}{\u03b1 : U \u2192 X \u2192 Set}{\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 \u03a3 (U \u2192 V) (\u03bb x \u2192 U \u2192 Y \u2192 X)\n \u2192 \u03a3 U (\u03bb x \u2192 Y)\n \u2192 Set\n\u22b8-cond {\u03b1 = \u03b1}{\u03b2} (f , F) (u , y) = \u03b1 u (F u y) \u2192 \u03b2 (f u) y\n\n_\u22b8\u2092_ : Obj \u2192 Obj \u2192 Obj\n(U , X , \u03b1) \u22b8\u2092 (V , Y , \u03b2) = ((U \u2192 V) \u00d7 (U \u2192 Y \u2192 X)) , ((U \u00d7 Y) , \u22b8-cond {\u03b1 = \u03b1}{\u03b2})\n\n\n_\u22b8\u2090_ : {A B C D : Obj} \u2192 Hom C A \u2192 Hom B D \u2192 Hom (A \u22b8\u2092 B) (C \u22b8\u2092 D)\n_\u22b8\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) =\n h , H , cond\n where\n h : \u03a3 (U \u2192 V) (\u03bb x \u2192 U \u2192 Y \u2192 X) \u2192 \u03a3 (W \u2192 S) (\u03bb x \u2192 W \u2192 T \u2192 Z)\n h (i , I) = (\u03bb w \u2192 g (i (f w))) , (\u03bb w t \u2192 F w (I (f w) (G (i (f w)) t)))\n H : \u03a3 (U \u2192 V) (\u03bb x \u2192 U \u2192 Y \u2192 X) \u2192 \u03a3 W (\u03bb x \u2192 T) \u2192 \u03a3 U (\u03bb x \u2192 Y)\n H (i , I) (w , t) = f w , G (i (f w)) t\n cond : {u : \u03a3 (U \u2192 V) (\u03bb x \u2192 U \u2192 Y \u2192 X)} {y : \u03a3 W (\u03bb x \u2192 T)} \u2192 \u22b8-cond {\u03b1 = \u03b1}{\u03b2} u (H u y) \u2192 \u22b8-cond {\u03b1 = \u03b3}{\u03b4} (h u) y\n cond {i , I}{w , y} p\u2083 p\u2084 = p\u2082 (p\u2083 (p\u2081 p\u2084))\n\ncur : {A B C : Obj}\n \u2192 Hom (A \u2297\u2092 B) C\n \u2192 Hom A (B \u22b8\u2092 C)\ncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb u \u2192 (\u03bb v \u2192 f (u , v)) , (\u03bb v z \u2192 snd (F (u , v) z))) , (\u03bb u r \u2192 fst (F (u , (fst r)) (snd r))) , cond \n where\n cond : {u : U} {y : \u03a3 V (\u03bb x \u2192 Z)}\n \u2192 \u03b1 u (fst (F (u , fst y) (snd y)))\n \u2192 \u22b8-cond {\u03b1 = \u03b2}{\u03b3} ((\u03bb v \u2192 f (u , v)) , (\u03bb v z \u2192 snd (F (u , v) z))) y \n cond {u}{v , z} p\u2082 p\u2083 with (p\u2081 {u , v}{z})\n ... | p\u2084 with F (u , v) z\n ... | (x , y) = p\u2084 (p\u2082 , p\u2083)\n \n\ncur-\u2261h : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-\u2261h {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}\n {f\u2081 , F\u2081 , p\u2081}{f\u2082 , F\u2082 , p\u2082} (p\u2083 , p\u2084)\n rewrite p\u2083 | p\u2084 = refl , refl\n\ncur-cong : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C} \u2192 f\u2081 \u2261h f\u2082 \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-cong {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)}{f\u2081 , F\u2081 , _}{f\u2082 , F\u2082 , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\n{-\nuncur : {A B C : Obj}\n \u2192 Hom A (B \u22b8\u2092 C)\n \u2192 Hom (A \u2297\u2092 B) C\nuncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb p \u2192 fst (f (fst p)) (snd p)) , (\u03bb z \u2192 (\u03bb v \u2192 F (v , z)) , (\u03bb u \u2192 snd (f u) z)) , uncur-cond\n where\n uncur-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : Z}\n \u2192 (\u03b1 \u2297\u1d63 \u03b2) u ((\u03bb v \u2192 F (v , y)) , (\u03bb u\u2081 \u2192 snd (f u\u2081) y))\n \u2192 \u03b3 (fst (f (fst u)) (snd u)) y\n uncur-cond {u , v}{z} (p\u2082 , p\u2083) with p\u2081 {u}{v , z} p\u2082\n ... | p\u2081' with f u\n ... | (j\u2081 , j\u2082) = p\u2081' p\u2083\n\ncur-uncur-bij\u2081 : \u2200{A B C}{f : Hom (A \u2297\u2092 B) C}\n \u2192 uncur (cur f) \u2261h f\ncur-uncur-bij\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{f , F , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : \u03a3 U (\u03bb x \u2192 V)} \u2192 f (fst a , snd a) \u2261 f a\n aux\u2081 {u , v} = refl\n \n aux\u2082 : {a : Z} \u2192 ((\u03bb v \u2192 fst (F a) v) , (\u03bb u \u2192 snd (F a) u)) \u2261 F a\n aux\u2082 {z} with F z\n ... | j\u2081 , j\u2082 = refl\n\ncur-uncur-bij\u2082 : \u2200{A B C}{g : Hom A (B \u22b8\u2092 C)}\n \u2192 cur (uncur g) \u2261h g\ncur-uncur-bij\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{g , G , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : U} \u2192 ((\u03bb v \u2192 fst (g a) v) , (\u03bb z \u2192 snd (g a) z)) \u2261 g a\n aux\u2081 {u} with g u\n ... | (j\u2081 , j\u2082) = refl\n\n aux\u2082 : {a : \u03a3 V (\u03bb x \u2192 Z)} \u2192 G (fst a , snd a) \u2261 G a\n aux\u2082 {v , z} = refl\n \n-- The of-course exponential:\n!\u2092-cond : \u2200{U X : Set}\n \u2192 (U \u2192 X \u2192 Set)\n \u2192 U\n \u2192 (U \u2192 X *)\n \u2192 Set\n!\u2092-cond \u03b1 u f = all-pred (\u03b1 u) (f u)\n \n!\u2092 : Obj \u2192 Obj\n!\u2092 (U , X , \u03b1) = U , (U \u2192 X *) , !\u2092-cond \u03b1\n\n!-cta : {V Y U X : Set}\n \u2192 (Y \u2192 X)\n \u2192 (U \u2192 V)\n \u2192 (V \u2192 Y *)\n \u2192 (U \u2192 X *)\n!-cta F f g = \u03bb u \u2192 list-funct F (g (f u))\n\n!\u2090-cond : \u2200{U V Y X : Set}{F : Y \u2192 X}{f : U \u2192 V}\n \u2192 (\u03b1 : U \u2192 X \u2192 Set)\n \u2192 (\u03b2 : V \u2192 Y \u2192 Set)\n \u2192 (p : {u : U} {y : Y} \u2192 \u03b1 u (F y) \u2192 \u03b2 (f u) y)\n {u : U}{l : Y *}\n \u2192 all-pred (\u03b1 u) (list-funct F l)\n \u2192 all-pred (\u03b2 (f u)) l\n!\u2090-cond _ _ _ {l = []} _ = triv\n!\u2090-cond \u03b1 \u03b2 p {u}{x :: xs} (p' , p'') = p p' , !\u2090-cond \u03b1 \u03b2 p p'' \n \n!\u2090 : {A B : Obj} \u2192 Hom A B \u2192 Hom (!\u2092 A) (!\u2092 B)\n!\u2090 {U , X , \u03b1}{V , Y , \u03b2} (f , F , p) = f , !-cta F f , !\u2090-cond \u03b1 \u03b2 p\n\n-- Of-course is a comonad:\n\u03b5 : \u2200{A} \u2192 Hom (!\u2092 A) A\n\u03b5 {U , X , \u03b1} = id-set , (\u03bb x y \u2192 [ x ]) , fst\n\n\u03b4-cta : {U X : Set} \u2192 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X)) \u2192 U \u2192 \ud835\udd43 X\n\u03b4-cta g u = foldr (\u03bb f rest \u2192 (f u) ++ rest) [] (g u)\n \n\u03b4 : \u2200{A} \u2192 Hom (!\u2092 A) (!\u2092 (!\u2092 A))\n\u03b4 {U , X , \u03b1} = id-set , \u03b4-cta , \u03b4-cond\n where\n \u03b4-cond : {u : U} {l : \ud835\udd43 (U \u2192 \ud835\udd43 X)}\n \u2192 all-pred (\u03b1 u) (foldr (\u03bb f \u2192 _++_ (f u)) [] l)\n \u2192 all-pred (\u03bb f\n \u2192 all-pred (\u03b1 u) (f u)) l\n \u03b4-cond {l = []} _ = triv\n \u03b4-cond {u}{l = x :: l'} p with\n all-pred-append {X}{\u03b1 u}\n {x u}\n {foldr (\u03bb f \u2192 _++_ (f u)) [] l'}\n \u2227-unit \u2227-assoc\n ... | p' rewrite p' = fst p , \u03b4-cond {u} {l'} (snd p)\n\n-- These diagrams can be found on page 22 of the report.\ncomonand-diag\u2081 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (!\u2090 (\u03b4 {A})) \u2261h (\u03b4 {A}) \u25cb (\u03b4 { !\u2092 A})\ncomonand-diag\u2081 {U , X , \u03b1} =\n refl , ext-set (\u03bb {a} \u2192 ext-set (\u03bb {a\u2081} \u2192 aux {a\u2081}{a a\u2081}))\n where\n aux : \u2200{a\u2081 : U}{l : \ud835\udd43 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X))} \u2192\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) []\n (map (\u03bb g u \u2192 foldr (\u03bb f \u2192 _++_ (f u)) [] (g u)) l)\n \u2261\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] (foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] l)\n aux {a}{[]} = refl \n aux {a}{x :: l} rewrite\n sym (foldr-append {l\u2081 = x a}{foldr (\u03bb f \u2192 _++_ (f a)) [] l}{a})\n = cong2 {a = foldr (\u03bb f \u2192 _++_ (f a)) [] (x a)}\n _++_\n refl\n (aux {a}{l})\n\ncomonand-diag\u2082 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (\u03b5 { !\u2092 A}) \u2261h (\u03b4 {A}) \u25cb (!\u2090 (\u03b5 {A}))\ncomonand-diag\u2082 {U , X , \u03b1} =\n refl , ext-set (\u03bb {f} \u2192 ext-set (\u03bb {a} \u2192 aux {a}{f a}))\n where\n aux : \u2200{a : U}{l : X *}\n \u2192 l ++ [] \u2261 foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x y \u2192 x :: []) l)\n aux {a}{[]} = refl\n aux {a}{x :: l} with aux {a}{l}\n ... | IH rewrite ++[] l =\n cong2 {a = x} {x} {l}\n {foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x\u2081 y \u2192 x\u2081 :: []) l)} _::_ refl\n IH\n \nmodule Cartesian where\n \u03c0\u2081 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (U , X , \u03b1))\n \u03c0\u2081 {U}{X}{V}{Y}{\u03b1}{\u03b2} = fst , (\u03bb f \u2192 (\u03bb v u \u2192 f u) , (\u03bb u v \u2192 [])) , \u03c0\u2081-cond\n where\n \u03c0\u2081-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : U \u2192 \ud835\udd43 X} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 y u\u2081) , (\u03bb u\u2081 v \u2192 [])) \u2192\n all-pred (\u03b1 (fst u)) (y (fst u))\n \u03c0\u2081-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2081\n\n \u03c0\u2082 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (V , Y , \u03b2))\n \u03c0\u2082 {U}{X}{V}{Y}{\u03b1}{\u03b2} = snd , (\u03bb f \u2192 (\u03bb v u \u2192 []) , (\u03bb u v \u2192 f v)) , \u03c0\u2082-cond\n where\n \u03c0\u2082-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : V \u2192 \ud835\udd43 Y} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 []) , (\u03bb u\u2081 v \u2192 y v)) \u2192\n all-pred (\u03b2 (snd u)) (y (snd u))\n \u03c0\u2082-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2082\n\n cart-ar-crt : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y) \u2192 W \u2192 \ud835\udd43 Z\n cart-ar-crt (f , F , p\u2081) (g , G , p\u2082) (j\u2081 , j\u2082) w = F (j\u2081 (g w)) w ++ G (j\u2082 (f w)) w\n\n cart-ar : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))\n cart-ar {U}{X}{V}{Y}{W}{Z}{\u03b1}{\u03b2}{\u03b3} (f , F , p\u2081) (g , G , p\u2082)\n = (\u03bb w \u2192 f w , g w) , cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) , cart-ar-cond\n where\n cart-ar-cond : \u2200{u : W} {y : \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y)} \u2192\n all-pred (\u03b3 u) (cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) y u) \u2192\n ((\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b1 u\u2081) (f\u2081 u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b2 u\u2081) (f\u2081 u\u2081)))\n (f u , g u) y\n cart-ar-cond {w}{j\u2081 , j\u2082} p\n rewrite\n all-pred-append {f = \u03b3 w}{F (j\u2081 (g w)) w}{G (j\u2082 (f w)) w} \u2227-unit \u2227-assoc with p\n ... | (a , b) = p\u2081 a , p\u2082 b\n\n cart-diag\u2081 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (U , X , \u03b1)}\n (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (U , X , \u03b1)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2081)\n cart-diag\u2081 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 sym (++[] (map F (j (f w))))))\n\n cart-diag\u2082 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (V , Y , \u03b2)}\n (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (V , Y , \u03b2)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2082)\n cart-diag\u2082 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 refl))\n-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7849a93dd727b32c2606d958173b08212260367d","subject":"update the validity for the simulator","message":"update the validity for the simulator\n","repos":"crypto-agda\/crypto-agda","old_file":"Game\/Transformation\/ReceiptFreeness-CCA2d\/Valid.agda","new_file":"Game\/Transformation\/ReceiptFreeness-CCA2d\/Valid.agda","new_contents":"{-# OPTIONS --copatterns #-}\nopen import Type\nopen import Function\nopen import Data.One\nopen import Data.Two\nopen import Data.Maybe\nopen import Data.Product\nopen import Data.Nat\nopen import Data.Vec hiding (_>>=_ ; _\u2208_)\nopen import Data.List as L\nopen import Data.Fin as Fin using (Fin)\nopen import Relation.Binary.PropositionalEquality.NP as \u2261\nopen import Control.Strategy renaming (map to mapS)\nopen import Control.Strategy.Utils\nopen import Game.Challenge\nimport Game.ReceiptFreeness\nimport Game.IND-CCA2-dagger\nimport Game.IND-CCA2-dagger.Valid\nimport Game.IND-CPA-utils\n\nimport Data.List.Any\nopen Data.List.Any using (here; there)\nopen Data.List.Any.Membership-\u2261 using (_\u2208_ ; _\u2209_)\n\nimport Game.ReceiptFreeness.Adversary\nimport Game.ReceiptFreeness.Valid\nimport Game.Transformation.ReceiptFreeness-CCA2d.Simulator\n\nmodule Game.Transformation.ReceiptFreeness-CCA2d.Valid\n (PubKey : \u2605)\n (CipherText : \u2605)\n\n (SerialNumber : \u2605)\n (Receipt : \u2605)\n (MarkedReceipt? : \u2605)\n (Ballot : \u2605)\n (Tally : \u2605)\n-- (BB : \u2605)\n-- ([] : BB)\n-- (_\u2237_ : Receipt \u2192 BB \u2192 BB)\n (Rgb : \u2605)\n (genBallot : PubKey \u2192 Rgb \u2192 Ballot)\n (tallyMarkedReceipt? : let CO = \ud835\udfda in CO \u2192 MarkedReceipt? \u2192 Tally)\n (0,0 : Tally)\n (1,1 : Tally)\n (_+,+_ : Tally \u2192 Tally \u2192 Tally)\n (receipts : SerialNumber \u00b2 \u2192 CipherText \u00b2 \u2192 Receipt \u00b2)\n (enc-co : Receipt \u2192 CipherText)\n (r-sn : Receipt \u2192 SerialNumber)\n (m? : Receipt \u2192 MarkedReceipt?)\n (b-sn : Ballot \u2192 SerialNumber)\n -- randomness supply for, encryption, key-generation, adversary, adversary state\n (R\u2090 : \u2605)\n (#q : \u2115) (max#q : Fin #q)\n (Check : let BB = List Receipt in BB \u2192 Receipt \u2192 \ud835\udfda)\n where\n\n_\u00b2' : \u2605 \u2192 \u2605\nX \u00b2' = X \u00d7 X\n\nCO = \ud835\udfda\nBB = List Receipt\nall-sn : BB \u2192 List SerialNumber\nall-sn = L.map r-sn\n\nmodule RF = Game.ReceiptFreeness.Adversary PubKey (SerialNumber \u00b2) R\u2090 Receipt Ballot Tally CO BB\nmodule RFV = Game.ReceiptFreeness.Valid PubKey SerialNumber R\u2090 Receipt Ballot Tally CO BB\n CipherText enc-co r-sn b-sn\n\nopen Game.Transformation.ReceiptFreeness-CCA2d.Simulator\n PubKey CipherText (SerialNumber \u00b2) Receipt MarkedReceipt? Ballot Tally BB [] _\u2237_ Rgb genBallot\n tallyMarkedReceipt? 0,0 1,1 _+,+_ receipts enc-co m? R\u2090 #q max#q Check\n\nmodule CCA2\u2020V = Game.IND-CCA2-dagger.Valid PubKey Message CipherText R\u2090\u2020\n\n\nmodule Simulator-Valid (RFA : RF.Adversary)(RFA-Valid : RFV.Valid-Adversary RFA)\n (WRONG-HYP : \u2200 r r' \u2192 enc-co r \u2261 enc-co r' \u2192 r-sn r \u2261 r-sn r')\n (CheckMem : \u2200 bb r \u2192 \u2713 (Check bb r) \u2192 r-sn r \u2209 all-sn bb)\n where\n valid : CCA2\u2020V.Valid-Adversary (Simulator.A\u2020 RFA)\n valid (r\u2090 , rgb) pk = Phase1 _ (RFA-Valid r\u2090 pk) where\n open CCA2\u2020V.Valid-Adversary (r\u2090 , rgb) pk\n module RFVA = RFV.Valid-Adversary r\u2090 pk\n open RF\n open Simulator RFA\n open AdversaryParts rgb pk r\u2090\n\n -- could refine r more\n -- {-\n Phase2 : \u2200 RF {bb i ta r} \u2192 r-sn (r 0\u2082) \u2208 all-sn bb \u2192 r-sn (r 1\u2082) \u2208 all-sn bb \u2192 RFVA.Phase2-Valid r RF\n \u2192 Phase2-Valid (enc-co \u2218 r) (mapS proj\u2081 (mitm-to-client-trans (MITM-phase 1\u2082 i bb ta) RF))\n Phase2 (ask REB cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)\n Phase2 (ask RBB cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)\n Phase2 (ask RTally cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)\n Phase2 (ask (RCO x) cont) r0 r1 ((r\u2080 , r\u2081) , RF-valid) = r\u2080 , r\u2081 , (\u03bb r \u2192 Phase2 (cont _) r0 r1 (RF-valid _))\n Phase2 (ask (Vote x) cont) {bb} r0 r1 RF-valid with Check bb x | CheckMem bb x\n ... | 0\u2082 | _ = Phase2 (cont _) r0 r1 (RF-valid _)\n ... | 1\u2082 | not-in-bb = (\u03bb eq \u2192 not-in-bb _ (subst (\u03bb x\u2081 \u2192 x\u2081 \u2208 all-sn bb) (WRONG-HYP _ _ eq) r0))\n , (\u03bb eq \u2192 not-in-bb _ (subst (\u03bb x\u2081 \u2192 x\u2081 \u2208 all-sn bb) (WRONG-HYP _ _ eq) r1))\n , \u03bb r \u2192 Phase2 (cont _) (there r0) (there r1) (RF-valid _)\n --Phase2 (cont _) (there r0) (there r1) (RF-valid _)\n Phase2 (done x) r0 r1 RF-valid = RF-valid\n\n Phase1 : \u2200 RF {sn i bb ta} \u2192 RFVA.Phase1-Valid sn RF\n \u2192 Phase1-Valid (mapS A\u20202 (mitm-to-client-trans (MITM-phase 0\u2082 i bb ta) RF))\n Phase1 (ask REB cont) RF-valid = Phase1 _ (RF-valid _)\n Phase1 (ask RBB cont) RF-valid = Phase1 _ (RF-valid _)\n Phase1 (ask RTally cont) RF-valid = Phase1 _ (RF-valid _)\n Phase1 (ask (RCO x) cont) RF-valid r = Phase1 _ (RF-valid _)\n Phase1 (ask (Vote x) cont) {bb = bb} RF-valid with Check bb x\n Phase1 (ask (Vote x) cont) RF-valid | 1\u2082 = \u03bb r \u2192 Phase1 _ (RF-valid _)\n Phase1 (ask (Vote x) cont) RF-valid | 0\u2082 = Phase1 _ (RF-valid _)\n Phase1 (done x) {bb = bb'}{ta} (sn\u2080\u2209sn , sn\u2081\u2209sn , RF-valid) cs\n = {!Phase2 (put-resp x (proj\u2082 (put-resp (hack-challenge x) cs))) (here refl) (there (here refl)) (RF-valid _)!}\n -- Phase2 (put-resp x (proj\u2082 (put-resp (hack-challenge x) cs) ))\n -- ? ? (RF-valid _)\n -- -}\n\n-- -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --copatterns #-}\nopen import Type\nopen import Function\nopen import Data.One\nopen import Data.Two\nopen import Data.Maybe\nopen import Data.Product\nopen import Data.Nat\nopen import Data.Vec hiding (_>>=_ ; _\u2208_)\nopen import Data.List as L\nopen import Data.Fin as Fin using (Fin)\nopen import Relation.Binary.PropositionalEquality.NP as \u2261\nopen import Control.Strategy renaming (map to mapS)\nopen import Game.Challenge\nimport Game.ReceiptFreeness\nimport Game.IND-CCA2-dagger\nimport Game.IND-CPA-utils\n\nimport Data.List.Any\nopen Data.List.Any using (here; there)\nopen Data.List.Any.Membership-\u2261 using (_\u2208_ ; _\u2209_)\n\nmodule Game.Transformation.ReceiptFreeness-CCA2d.Valid\n (PubKey : \u2605)\n (SecKey : \u2605)\n -- Message = \ud835\udfda\n (CipherText : \u2605)\n\n (SerialNumber : \u2605)\n\n -- randomness supply for, encryption, key-generation, adversary, adversary state\n (R\u2091 R\u2096 R\u2090 : \u2605)\n (#q : \u2115) (max#q : Fin #q)\n (KeyGen : R\u2096 \u2192 PubKey \u00d7 SecKey)\n (Enc : let Message = \ud835\udfda in\n PubKey \u2192 Message \u2192 R\u2091 \u2192 CipherText)\n (Dec : let Message = \ud835\udfda in\n SecKey \u2192 CipherText \u2192 Message)\n (Check : let open Game.ReceiptFreeness PubKey SecKey CipherText SerialNumber R\u2091 R\u2096 R\u2090 #q max#q KeyGen Enc Dec\n in BB \u2192 Receipt \u2192 \ud835\udfda)\n (CheckMem : \u2200 bb r \u2192 \u2713 (Check bb r) \u2192 proj\u2081 (proj\u2082 r) \u2209 L.map (proj\u2081 \u2218 proj\u2082) bb)\n -- (CheckEnc : \u2200 pk m r\u2091 \u2192 Check (Enc pk m r\u2091) \u2261 1\u2082)\n where\n\n_\u00b2' : \u2605 \u2192 \u2605\nX \u00b2' = X \u00d7 X\n\n\nr-sn : Receipt \u2192 SerialNumber\nr-sn (_ , sn , _) = sn\n\nmodule Simulator-Valid (RFA : RF.Adversary)(RFA-Valid : RF.Valid-Adversary RFA)\n (WRONG-HYP : \u2200 r r' \u2192 r-sn r \u2261 r-sn r' \u2192 enc-co r \u2261 enc-co r')\n where\n valid : CCA2\u2020.Valid-Adversary (Simulator.A\u2020 RFA)\n valid (r\u2090 , rgb) pk = Phase1 _ (RFA-Valid r\u2090 pk) where\n open CCA2\u2020.Valid-Adversary (r\u2090 , rgb) pk\n module RFA = RF.Valid-Adversary r\u2090 pk\n open Simulator RFA\n open AdversaryParts rgb pk r\u2090\n\n -- could refine r more\n -- {-\n Phase2 : \u2200 RF {bb i taA taB r} \u2192 r-sn (r 0\u2082) \u2208 L.map r-sn bb \u2192 r-sn (r 1\u2082) \u2208 L.map r-sn bb \u2192 RFA.Phase2-Valid r RF\n \u2192 Phase2-Valid (proj\u2082 \u2218 proj\u2082 \u2218 r) (mapS proj\u2081 (mitm-to-client-trans (MITM-phase 1\u2082 i bb (taA , taB)) RF))\n Phase2 (ask REB cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)\n Phase2 (ask RBB cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)\n Phase2 (ask RTally cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)\n Phase2 (ask (RCO x) cont) r0 r1 ((r\u2080 , r\u2081) , RF-valid) = r\u2080 , r\u2081 , (\u03bb r \u2192 Phase2 (cont _) r0 r1 (RF-valid _))\n Phase2 (ask (Vote x) cont) {bb} r0 r1 RF-valid with Check bb x | CheckMem bb x\n ... | 0\u2082 | _ = Phase2 (cont _) r0 r1 (RF-valid _)\n ... | 1\u2082 | not-in-bb = (\u03bb eq \u2192 not-in-bb _ {!subst (\u03bb x \u2192 x \u2208 L.map r-sn bb) eq!})\n , {!!},\n , \u03bb r \u2192 Phase2 (cont _) (there r0) (there r1) (RF-valid _)\n Phase2 (done x) r0 r1 RF-valid = RF-valid\n\n Phase1 : \u2200 RF {sn i bb taA taB} \u2192 RFA.Phase1-Valid sn RF\n \u2192 Phase1-Valid (mapS A\u20202 (mitm-to-client-trans (MITM-phase 0\u2082 i bb (taA , taB)) RF))\n Phase1 (ask REB cont) RF-valid = Phase1 _ (RF-valid _)\n Phase1 (ask RBB cont) RF-valid = Phase1 _ (RF-valid _)\n Phase1 (ask RTally cont) RF-valid = Phase1 _ (RF-valid _)\n Phase1 (ask (RCO x) cont) RF-valid r = Phase1 _ (RF-valid _)\n Phase1 (ask (Vote x) cont) {bb = bb} RF-valid with Check bb x\n Phase1 (ask (Vote x) cont) RF-valid | 1\u2082 = \u03bb r \u2192 Phase1 _ (RF-valid _)\n Phase1 (ask (Vote x) cont) RF-valid | 0\u2082 = Phase1 _ (RF-valid _)\n Phase1 (done x) (sn\u2080\u2209sn , sn\u2081\u2209sn , RF-valid) cs = Phase2 (put-resp x (proj\u2082 (put-resp (hack-challenge x) cs) ))\n (here refl) (there (here refl)) (RF-valid _)\n -- -}\n\n-- -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"2685982a354df08144518cd09a76727e9556b673","subject":"Hutton's razor: shave it with Conor(tm)","message":"Hutton's razor: shave it with Conor(tm)\n","repos":"larrytheliquid\/pigit,mietek\/epigram2,mietek\/epigram2","old_file":"models\/RevisedHutton.agda","new_file":"models\/RevisedHutton.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule RevisedHutton where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\ndata Vec (A : Set) : Nat -> Set where\n vnil : Vec A ze\n vcons : {n : Nat} -> A -> Vec A n -> Vec A (su n)\n\ndata Fin : Nat -> Set where\n fze : {n : Nat} -> Fin (su n)\n fsu : {n : Nat} -> Fin n -> Fin (su n)\n\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n pair : Type -> Type -> Type\n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\n-- Fix menu:\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void))\n\n-- Index-dependent menu:\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\nchoiceFreeMenu (pair x y) = nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void) -- plus x y\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void ) -- le x y\nchoiceFreeDessert (pair x y) = Void\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\n-- Tagged description of expressions\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\n-- Free monadic expressions\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\nVal (pair x y) = (Val x) * (Val y)\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = toIDesc Type (exprFree ** Val)\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : {ty : Type} -> IMu closeTerm ty -> Val ty\neval {ty} term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n\n--********************************\n-- [Open terms]\n--********************************\n\n-- A context is a snoc-list of types\n-- put otherwise, a context is a type telescope\ndata Context : Set where\n [] : Context\n _,_ : Context -> Type -> Context\n\n-- The environment realizes the context, having a value for each type\nEnv : Context -> Set\nEnv [] = Unit\nEnv (G , S) = Env G * Val S\n\n-- A typed-variable indexes into the context, obtaining a proof that\n-- what we get is what you want (WWGIWYW)\nVar : Context -> Type -> Set\nVar [] T = Zero\nVar (G , S) T = Var G T + (S == T)\n\n-- The lookup gets into the context to extract the value\nlookup : (G : Context) -> Env G -> (T : Type) -> Var G T -> Val T\nlookup [] _ T ()\nlookup (G , .T) (g , t) T (r refl) = t\nlookup (G , S) (g , t) T (l x) = lookup G g T x \n\n-- Open term: holes are either values or variables in a context\nopenTerm : Context -> Type -> IDesc Type\nopenTerm c = toIDesc Type (exprFree ** (\\ty -> Val ty + Var c ty))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\n-- |discharge| is the local substitution expected by |substI|. It is\n-- just sugar around context lookup\ndischarge : (context : Context) ->\n Env context ->\n (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm ty\ndischarge ctxt env ty (l value) = con (EZe , value)\ndischarge ctxt env ty (r variable) = con (EZe , lookup ctxt env ty variable ) \n\n-- |substExpr| is the specialized |substI| to expressions. We get it\n-- generically from the free monad construction.\nsubstExpr : {ty : Type}\n (context : Context)\n (sigma : (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm ty\nsubstExpr {ty} c sig term = \n substI (\\ty -> Val ty + Var c ty) Val exprFree sig ty term\n\n-- By first doing substitution to close the term, we can use\n-- evaluation of closed terms, obtaining evaluation of open terms\n-- under a valid context.\nevalOpen : {ty : Type}(context : Context) ->\n Env context ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen ctxt env tm = eval (substExpr ctxt (discharge ctxt env) tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- Test context:\n-- V 0 :-> true, V 1 :-> 2, V 2 :-> ( false , 1 )\ntestContext : Context\ntestContext = (([] , bool) , nat) , pair bool nat\ntestEnv : Env testContext\ntestEnv = ((Void , true ) , su (su ze)) , (false , su ze) \n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , r ( l (r refl) ) )\n\ntestSubst1 : IMu closeTerm nat\ntestSubst1 = substExpr testContext \n (discharge testContext testEnv)\n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext testEnv test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con (ESu (ESu EZe) , (con (EZe , l (su ze)) , con ( EZe , r (l (r refl)) )) )\n\ntestSubst2 : IMu closeTerm nat\ntestSubst2 = substExpr testContext \n (discharge testContext testEnv)\n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext testEnv test2\n\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu EZe , (con (EZe , r (l (l (r refl)))) ,\n (con (EZe , r (l (r refl))) ,\n con (EZe , l ze))))\n\ntestSubst3 : IMu closeTerm nat\ntestSubst3 = substExpr testContext \n (discharge testContext testEnv)\n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext testEnv test3\n\n-- V 2\ntest4 : IMu (openTerm testContext) (pair bool nat)\ntest4 = con (EZe , r ( r refl ) )\n\ntestSubst4 : IMu closeTerm (pair bool nat)\ntestSubst4 = substExpr testContext \n (discharge testContext testEnv)\n test4\n-- = (false , 1)\ntestEval4 : Val (pair bool nat)\ntestEval4 = evalOpen testContext testEnv test4","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule RevisedHutton where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\ndata Vec (A : Set) : Nat -> Set where\n vnil : Vec A ze\n vcons : {n : Nat} -> A -> Vec A n -> Vec A (su n)\n\ndata Fin : Nat -> Set where\n fze : {n : Nat} -> Fin (su n)\n fsu : {n : Nat} -> Fin n -> Fin (su n)\n\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n pair : Type -> Type -> Type\n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\n-- Fix menu:\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void))\n\n-- Index-dependent menu:\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\nchoiceFreeMenu (pair x y) = nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void) -- plus x y\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void ) -- le x y\nchoiceFreeDessert (pair x y) = Void\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\n-- Tagged description of expressions\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\n-- Free monadic expressions\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\nVal (pair x y) = (Val x) * (Val y)\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = toIDesc Type (exprFree ** Val)\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : {ty : Type} -> IMu closeTerm ty -> Val ty\neval {ty} term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n\n--********************************\n-- [Open terms]\n--********************************\n\n-- Context and context lookup \nContext : Nat -> Set\nContext n = Vec (Sigma Type (\\ty -> IMu closeTerm ty)) n\n\ntypeAt : {n : Nat}(c : Context n) -> Fin n -> Type\ntypeAt {ze} c ()\ntypeAt {su _} (vcons x _) fze = fst x\ntypeAt {su _} (vcons _ xs) (fsu y) = typeAt xs y\n\nlookup : {n : Nat}(c : Context n)(i : Fin n) -> IMu closeTerm (typeAt c i)\nlookup {ze} c ()\nlookup {su _} (vcons x _) fze = snd x\nlookup {su _} (vcons _ xs) (fsu y) = lookup xs y\n\n-- A variable is an index into the context *and* a proof that the\n-- context contains the expected stuff\nVar : {n : Nat} -> Context n -> Type -> Set\nVar {n} c ty = Sigma (Fin n) (\\i -> typeAt c i == ty)\n\n-- Open term: holes are either values or variables in a context\nopenTerm : {n : Nat} -> Context n -> Type -> IDesc Type\nopenTerm c = toIDesc Type (exprFree ** (\\ty -> Val ty + Var c ty))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\n-- |discharge| is the local substitution expected by |substI|. It is\n-- just sugar around context lookup, taking care of transmitting the\n-- proof\ndischarge : {n : Nat}\n {context : Context n}\n (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm ty\ndischarge ty (l value) = con (EZe , value)\ndischarge {n} {c} ty (r variable) = \n subst (cong (IMu closeTerm) (snd variable)) \n (lookup c (fst variable))\n\n-- |substExpr| is the specialized |substI| to expressions. We get it\n-- generically from the free monad construction.\nsubstExpr : {n : Nat}\n {ty : Type}\n (context : Context n)\n (sigma : (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm ty\nsubstExpr {n} {ty} c sig term = \n substI (\\ty -> Val ty + Var c ty) Val exprFree sig ty term\n\n-- By first doing substitution to close the term, we can use\n-- evaluation of closed terms, obtaining evaluation of open terms\n-- under a valid context.\nevalOpen : {n : Nat}{ty : Type}\n (context : Context n) ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen context tm = eval (substExpr context discharge tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- V 0 :-> true, V 1 :-> 2, V 2 :-> ( false , 1 )\ntestContext : Context _\ntestContext = vcons (bool , con (EZe , true )) \n (vcons (nat , con (EZe , su (su ze)) ) \n (vcons (pair bool nat , con (EZe , ( false , su ze )))\n vnil))\n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , r ( fsu fze , refl ) )\n\ntestSubst1 : IMu closeTerm nat\ntestSubst1 = substExpr testContext \n discharge \n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con (ESu (ESu EZe) , (con (EZe , l (su ze)) , con ( EZe , r (fsu fze , refl) )) )\n\ntestSubst2 : IMu closeTerm nat\ntestSubst2 = substExpr testContext \n discharge \n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext test2\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu EZe , (con (EZe , r (fze , refl)) ,\n (con (EZe , r (fsu fze , refl)) ,\n con (EZe , l ze))))\n\ntestSubst3 : IMu closeTerm nat\ntestSubst3 = substExpr testContext \n discharge \n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext test3\n\n-- V 2\ntest4 : IMu (openTerm testContext) (pair bool nat)\ntest4 = con (EZe , r ( fsu (fsu fze) , refl ) )\n\ntestSubst4 : IMu closeTerm (pair bool nat)\ntestSubst4 = substExpr testContext \n discharge \n test4\n-- = (false , 1)\ntestEval4 : Val (pair bool nat)\ntestEval4 = evalOpen testContext test4\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"de8917e4d69350f1eb3c42d6c86c64eeb18e3786","subject":"Cosmetic change.","message":"Cosmetic change.\n\nIgnore-this: ecda4978e184c6c467a610bfa8971fb6\n\ndarcs-hash:20120306162449-3bd4e-ca9ba6a7b8b6177d3150a5d3ab44428f7a3496c8.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Data\/Stream\/Type.agda","new_file":"src\/FOTC\/Data\/Stream\/Type.agda","new_contents":"------------------------------------------------------------------------------\n-- The FOTC stream type\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Stream.Type where\n\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n-- Functional for the FOTC Stream type.\n-- StreamF : (D \u2192 Set) \u2192 D \u2192 Set\n-- StreamF P xs = \u2203[ x' ] \u2203[ xs' ] P xs' \u2227 xs \u2261 x' \u2237 xs'\n\n-- Stream is the greatest fixed-point of StreamF (by Stream-gfp\u2081 and\n-- Stream-gfp\u2082).\n\npostulate\n Stream : D \u2192 Set\n\npostulate\n-- Stream is a post-fixed point of StreamF, i.e.\n--\n-- Stream \u2264 StreamF Stream.\n Stream-gfp\u2081 : \u2200 {xs} \u2192 Stream xs \u2192\n \u2203[ x' ] \u2203[ xs' ] Stream xs' \u2227 xs \u2261 x' \u2237 xs'\n{-# ATP axiom Stream-gfp\u2081 #-}\n\n-- Stream is the greatest post-fixed point of StreamF, i.e\n--\n-- \u2200 P. P \u2264 StreamF P \u21d2 P \u2264 Stream.\n--\n-- N.B. This is an axiom schema. Because in the automatic proofs we\n-- *must* use an instance, we do not add this postulate as an ATP\n-- axiom.\npostulate\n Stream-gfp\u2082 : (P : D \u2192 Set) \u2192\n -- P is post-fixed point of StreamF.\n (\u2200 {xs} \u2192 P xs \u2192 \u2203[ x' ] \u2203[ xs' ] P xs' \u2227 xs \u2261 x' \u2237 xs') \u2192\n -- Stream is greater than P.\n \u2200 {xs} \u2192 P xs \u2192 Stream xs\n\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Stream predicate is also a pre-fixed point of the functor StreamF, i.e.\n--\n-- StreamF Stream \u2264 Stream.\nStream-gfp\u2083 : \u2200 {xs} \u2192\n (\u2203[ x' ] \u2203[ xs' ] Stream xs' \u2227 xs \u2261 x' \u2237 xs') \u2192\n Stream xs\nStream-gfp\u2083 h = Stream-gfp\u2082 P helper h\n where\n P : D \u2192 Set\n P ws = \u2203[ w' ] \u2203[ ws' ] Stream ws' \u2227 ws \u2261 w' \u2237 ws'\n\n helper : \u2200 {xs} \u2192 P xs \u2192 \u2203[ x' ] \u2203[ xs' ] P xs' \u2227 xs \u2261 x' \u2237 xs'\n helper (_ , _ , Sxs' , xs\u2261x'\u2237xs') = _ , _ , Stream-gfp\u2081 Sxs' , xs\u2261x'\u2237xs'\n","old_contents":"------------------------------------------------------------------------------\n-- The FOTC stream type\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Stream.Type where\n\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n-- Functional for the FOTC Stream type.\n-- StreamF : (D \u2192 Set) \u2192 D \u2192 Set\n-- StreamF P ds = \u2203[ e ] \u2203[ es ] P es \u2227 ds \u2261 e \u2237 es\n\n-- Stream is the greatest fixed-point of StreamF (by Stream-gfp\u2081 and\n-- Stream-gfp\u2082).\n\npostulate\n Stream : D \u2192 Set\n\npostulate\n-- Stream is a post-fixed point of StreamF, i.e.\n--\n-- Stream \u2264 StreamF Stream.\n Stream-gfp\u2081 : \u2200 {xs} \u2192 Stream xs \u2192\n \u2203[ x' ] \u2203[ xs' ] Stream xs' \u2227 xs \u2261 x' \u2237 xs'\n{-# ATP axiom Stream-gfp\u2081 #-}\n\n-- Stream is the greatest post-fixed point of StreamF, i.e\n--\n-- \u2200 P. P \u2264 StreamF P \u21d2 P \u2264 Stream.\n--\n-- N.B. This is an axiom schema. Because in the automatic proofs we\n-- *must* use an instance, we do not add this postulate as an ATP\n-- axiom.\npostulate\n Stream-gfp\u2082 : (P : D \u2192 Set) \u2192\n -- P is post-fixed point of StreamF.\n (\u2200 {xs} \u2192 P xs \u2192 \u2203[ x' ] \u2203[ xs' ] P xs' \u2227 xs \u2261 x' \u2237 xs') \u2192\n -- Stream is greater than P.\n \u2200 {xs} \u2192 P xs \u2192 Stream xs\n\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Stream predicate is also a pre-fixed point of the functor StreamF, i.e.\n--\n-- StreamF Stream \u2264 Stream.\nStream-gfp\u2083 : \u2200 {xs} \u2192\n (\u2203[ x' ] \u2203[ xs' ] Stream xs' \u2227 xs \u2261 x' \u2237 xs') \u2192\n Stream xs\nStream-gfp\u2083 h = Stream-gfp\u2082 P helper h\n where\n P : D \u2192 Set\n P ws = \u2203[ w' ] \u2203[ ws' ] Stream ws' \u2227 ws \u2261 w' \u2237 ws'\n\n helper : \u2200 {xs} \u2192 P xs \u2192 \u2203[ x' ] \u2203[ xs' ] P xs' \u2227 xs \u2261 x' \u2237 xs'\n helper (_ , _ , Sxs' , xs\u2261x'\u2237xs') = _ , _ , Stream-gfp\u2081 Sxs' , xs\u2261x'\u2237xs'\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"41898751fdcc62758217e161ad60e93a20db6f51","subject":"Monoid: use nest and not fold","message":"Monoid: use nest and not fold\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Algebra\/Monoid.agda","new_file":"lib\/Algebra\/Monoid.agda","new_contents":"open import Function.NP\nopen import Data.Product.NP\nopen import Data.Nat\n using (\u2115; zero)\n renaming (_+_ to _+\u2115_; _*_ to _*\u2115_; suc to 1+_)\nopen import Data.Integer\n using (\u2124; +_; -[1+_]; _\u2296_)\n renaming (_+_ to _+\u2124_; _*_ to _*\u2124_)\nopen import Relation.Binary.PropositionalEquality.NP renaming (_\u2219_ to _\u2666_)\nopen import Algebra.FunctionProperties.Eq\nopen \u2261-Reasoning\n\nmodule Algebra.Monoid where\n\nrecord Monoid-Ops {\u2113} (M : Set \u2113) : Set \u2113 where\n constructor mk\n infixl 7 _\u2219_\n\n field\n _\u2219_ : M \u2192 M \u2192 M\n \u03b5 : M\n\n _^\u207a_ : M \u2192 \u2115 \u2192 M\n x ^\u207a n = nest n (_\u2219_ x) \u03b5\n\n module FromInverseOp\n (_\u207b\u00b9 : Op\u2081 M)\n where\n infixl 7 _\/_\n\n _\/_ : M \u2192 M \u2192 M\n x \/ y = x \u2219 y \u207b\u00b9\n\n open FromOp\u2082 _\/_ public renaming (op= to \/=)\n\n _^\u207b_ _^\u207b\u2032_ : M \u2192 \u2115 \u2192 M\n x ^\u207b n = (x ^\u207a n)\u207b\u00b9\n x ^\u207b\u2032 n = (x \u207b\u00b9)^\u207a n\n\n _^_ : M \u2192 \u2124 \u2192 M\n x ^ -[1+ n ] = x ^\u207b(1+ n)\n x ^ (+ n) = x ^\u207a n\n\nrecord Monoid-Struct {\u2113} {M : Set \u2113} (mon-ops : Monoid-Ops M) : Set \u2113 where\n open Monoid-Ops mon-ops\n\n -- laws\n field\n assoc : Associative _\u2219_\n identity : Identity \u03b5 _\u2219_\n\n open FromOp\u2082 _\u2219_ public renaming (op= to \u2219=)\n open FromAssoc _\u2219_ assoc public\n\n module _ {b} where\n ^\u207a0-\u03b5 : b ^\u207a 0 \u2261 \u03b5\n ^\u207a0-\u03b5 = idp\n\n ^\u207a1-id : b ^\u207a 1 \u2261 b\n ^\u207a1-id = snd identity\n\n ^\u207a2-\u2219 : b ^\u207a 2 \u2261 b \u2219 b\n ^\u207a2-\u2219 = ap (_\u2219_ _) ^\u207a1-id\n\n ^\u207a-+ : \u2200 m {n} \u2192 b ^\u207a (m +\u2115 n) \u2261 b ^\u207a m \u2219 b ^\u207a n\n ^\u207a-+ 0 = ! fst identity\n ^\u207a-+ (1+ m) = ap (_\u2219_ b) (^\u207a-+ m) \u2666 ! assoc\n\n ^\u207a-* : \u2200 m {n} \u2192 b ^\u207a(m *\u2115 n) \u2261 (b ^\u207a n)^\u207a m\n ^\u207a-* 0 = idp\n ^\u207a-* (1+ m) {n}\n = b ^\u207a (n +\u2115 m *\u2115 n) \u2261\u27e8 ^\u207a-+ n \u27e9\n b ^\u207a n \u2219 b ^\u207a(m *\u2115 n) \u2261\u27e8 ap (_\u2219_ (b ^\u207a n)) (^\u207a-* m) \u27e9\n b ^\u207a n \u2219 (b ^\u207a n)^\u207a m \u220e\n\n comm-\u03b5 : \u2200 {x} \u2192 x \u2219 \u03b5 \u2261 \u03b5 \u2219 x\n comm-\u03b5 = snd identity \u2666 ! fst identity\n\n module _ {c x y} (e : (x \u2219 y) \u2261 \u03b5) where\n elim-assoc= : (c \u2219 x) \u2219 y \u2261 c\n elim-assoc= = assoc \u2666 \u2219= idp e \u2666 snd identity\n\n elim-!assoc= : x \u2219 (y \u2219 c) \u2261 c\n elim-!assoc= = ! assoc \u2666 \u2219= e idp \u2666 fst identity\n\n module _ {c d x y} (e : (x \u2219 y) \u2261 \u03b5) where\n elim-inner= : (c \u2219 x) \u2219 (y \u2219 d) \u2261 c \u2219 d\n elim-inner= = assoc \u2666 ap (_\u2219_ c) (elim-!assoc= e)\n\n module FromLeftInverse\n (_\u207b\u00b9 : Op\u2081 M)\n (inv-l : LeftInverse \u03b5 _\u207b\u00b9 _\u2219_)\n where\n open FromInverseOp _\u207b\u00b9\n\n cancels-\u2219-left : LeftCancel _\u2219_\n cancels-\u2219-left {c} {x} {y} e\n = x \u2261\u27e8 ! fst identity \u27e9\n \u03b5 \u2219 x \u2261\u27e8 \u2219= (! inv-l) idp \u27e9\n c \u207b\u00b9 \u2219 c \u2219 x \u2261\u27e8 !assoc= e \u27e9\n c \u207b\u00b9 \u2219 c \u2219 y \u2261\u27e8 \u2219= inv-l idp \u27e9\n \u03b5 \u2219 y \u2261\u27e8 fst identity \u27e9\n y \u220e\n\n inv-r : RightInverse \u03b5 _\u207b\u00b9 _\u2219_\n inv-r = cancels-\u2219-left (! assoc \u2666 \u2219= inv-l idp \u2666 ! comm-\u03b5)\n\n \/-\u2219 : \u2200 {x y} \u2192 x \u2261 (x \/ y) \u2219 y\n \/-\u2219 {x} {y}\n = x \u2261\u27e8 ! snd identity \u27e9\n x \u2219 \u03b5 \u2261\u27e8 ap (_\u2219_ x) (! inv-l) \u27e9\n x \u2219 (y \u207b\u00b9 \u2219 y) \u2261\u27e8 ! assoc \u27e9\n (x \/ y) \u2219 y \u220e\n\n module FromRightInverse\n (_\u207b\u00b9 : Op\u2081 M)\n (inv-r : RightInverse \u03b5 _\u207b\u00b9 _\u2219_)\n where\n open FromInverseOp _\u207b\u00b9\n\n cancels-\u2219-right : RightCancel _\u2219_\n cancels-\u2219-right {c} {x} {y} e\n = x \u2261\u27e8 ! snd identity \u27e9\n x \u2219 \u03b5 \u2261\u27e8 \u2219= idp (! inv-r) \u27e9\n x \u2219 (c \u2219 c \u207b\u00b9) \u2261\u27e8 assoc= e \u27e9\n y \u2219 (c \u2219 c \u207b\u00b9) \u2261\u27e8 \u2219= idp inv-r \u27e9\n y \u2219 \u03b5 \u2261\u27e8 snd identity \u27e9\n y \u220e\n\n inv-l : LeftInverse \u03b5 _\u207b\u00b9 _\u2219_\n inv-l = cancels-\u2219-right (assoc \u2666 \u2219= idp inv-r \u2666 comm-\u03b5)\n\n module _ {x y} where\n is-\u03b5-left : x \u2261 \u03b5 \u2192 x \u2219 y \u2261 y\n is-\u03b5-left e = ap (\u03bb z \u2192 z \u2219 _) e \u2666 fst identity\n\n is-\u03b5-right : y \u2261 \u03b5 \u2192 x \u2219 y \u2261 x\n is-\u03b5-right e = ap (\u03bb z \u2192 _ \u2219 z) e \u2666 snd identity\n\n \u2219-\/ : x \u2261 (x \u2219 y) \/ y\n \u2219-\/\n = x \u2261\u27e8 ! snd identity \u27e9\n x \u2219 \u03b5 \u2261\u27e8 ap (_\u2219_ x) (! inv-r) \u27e9\n x \u2219 (y \/ y) \u2261\u27e8 ! assoc \u27e9\n (x \u2219 y) \/ y \u220e\n\n module _ {x y} where\n unique-\u03b5-left : x \u2219 y \u2261 y \u2192 x \u2261 \u03b5\n unique-\u03b5-left eq\n = x \u2261\u27e8 \u2219-\/ \u27e9\n (x \u2219 y) \/ y \u2261\u27e8 \/= eq idp \u27e9\n y \/ y \u2261\u27e8 inv-r \u27e9\n \u03b5 \u220e\n\n unique-\u03b5-right : x \u2219 y \u2261 x \u2192 y \u2261 \u03b5\n unique-\u03b5-right eq\n = y \u2261\u27e8 ! is-\u03b5-left inv-l \u27e9\n x \u207b\u00b9 \u2219 x \u2219 y \u2261\u27e8 assoc \u27e9\n x \u207b\u00b9 \u2219 (x \u2219 y) \u2261\u27e8 \u2219= idp eq \u27e9\n x \u207b\u00b9 \u2219 x \u2261\u27e8 inv-l \u27e9\n \u03b5 \u220e\n\n unique-\u207b\u00b9 : x \u2219 y \u2261 \u03b5 \u2192 x \u2261 y \u207b\u00b9\n unique-\u207b\u00b9 eq\n = x \u2261\u27e8 \u2219-\/ \u27e9\n (x \u2219 y) \/ y \u2261\u27e8 \/= eq idp \u27e9\n \u03b5 \/ y \u2261\u27e8 fst identity \u27e9\n y \u207b\u00b9 \u220e\n\n open FromLeftInverse _\u207b\u00b9 inv-l hiding (inv-r)\n\n \u03b5\u207b\u00b9-\u03b5 : \u03b5 \u207b\u00b9 \u2261 \u03b5\n \u03b5\u207b\u00b9-\u03b5 = unique-\u03b5-left inv-l\n\n involutive : Involutive _\u207b\u00b9\n involutive {x}\n = cancels-\u2219-right\n (x \u207b\u00b9 \u207b\u00b9 \u2219 x \u207b\u00b9 \u2261\u27e8 inv-l \u27e9\n \u03b5 \u2261\u27e8 ! inv-r \u27e9\n x \u2219 x \u207b\u00b9 \u220e)\n\n \u207b\u00b9-hom\u2032 : \u2200 {x y} \u2192 (x \u2219 y)\u207b\u00b9 \u2261 y \u207b\u00b9 \u2219 x \u207b\u00b9\n \u207b\u00b9-hom\u2032 {x} {y} = cancels-\u2219-left {x \u2219 y}\n ((x \u2219 y) \u2219 (x \u2219 y)\u207b\u00b9 \u2261\u27e8 inv-r \u27e9\n \u03b5 \u2261\u27e8 ! inv-r \u27e9\n x \u2219 x \u207b\u00b9 \u2261\u27e8 ap (_\u2219_ x) (! fst identity) \u27e9\n x \u2219 (\u03b5 \u2219 x \u207b\u00b9) \u2261\u27e8 \u2219= idp (\u2219= (! inv-r) idp) \u27e9\n x \u2219 ((y \u2219 y \u207b\u00b9) \u2219 x \u207b\u00b9) \u2261\u27e8 ap (_\u2219_ x) assoc \u27e9\n x \u2219 (y \u2219 (y \u207b\u00b9 \u2219 x \u207b\u00b9)) \u2261\u27e8 ! assoc \u27e9\n (x \u2219 y) \u2219 (y \u207b\u00b9 \u2219 x \u207b\u00b9) \u220e)\n\n elim-\u2219-left-\u207b\u00b9\u2219 : \u2200 {c x y} \u2192 (c \u2219 x)\u207b\u00b9 \u2219 (c \u2219 y) \u2261 x \u207b\u00b9 \u2219 y\n elim-\u2219-left-\u207b\u00b9\u2219 {c} {x} {y}\n = (c \u2219 x)\u207b\u00b9 \u2219 (c \u2219 y) \u2261\u27e8 \u2219= \u207b\u00b9-hom\u2032 idp \u27e9\n x \u207b\u00b9 \u2219 c \u207b\u00b9 \u2219 (c \u2219 y) \u2261\u27e8 elim-inner= inv-l \u27e9\n x \u207b\u00b9 \u2219 y \u220e\n\n elim-\u2219-right-\/ : \u2200 {c x y} \u2192 (x \u2219 c) \/ (y \u2219 c) \u2261 x \/ y\n elim-\u2219-right-\/ {c} {x} {y}\n = x \u2219 c \u2219 (y \u2219 c)\u207b\u00b9 \u2261\u27e8 ap (_\u2219_ _) \u207b\u00b9-hom\u2032 \u27e9\n x \u2219 c \u2219 (c \u207b\u00b9 \/ y) \u2261\u27e8 elim-inner= inv-r \u27e9\n x \/ y \u220e \n\n module _ {b} where\n ^\u207asuc : \u2200 n \u2192 b ^\u207a(1+ n) \u2261 b ^\u207a n \u2219 b\n ^\u207asuc 0 = comm-\u03b5\n ^\u207asuc (1+ n) = ap (_\u2219_ b) (^\u207asuc n) \u2666 ! assoc\n\n ^\u207a-comm : \u2200 n \u2192 b \u2219 b ^\u207a n \u2261 b ^\u207a n \u2219 b\n ^\u207a-comm = ^\u207asuc\n\n ^\u207bsuc : \u2200 n \u2192 b ^\u207b(1+ n) \u2261 b \u207b\u00b9 \u2219 b ^\u207b n\n ^\u207bsuc n = ap _\u207b\u00b9 (^\u207asuc n) \u2666 \u207b\u00b9-hom\u2032\n\n ^\u207b\u2032-spec : \u2200 n \u2192 b ^\u207b\u2032 n \u2261 b ^\u207b n\n ^\u207b\u2032-spec 0 = ! \u03b5\u207b\u00b9-\u03b5\n ^\u207b\u2032-spec (1+ n) = ap (_\u2219_ (b \u207b\u00b9)) (^\u207b\u2032-spec n)\n \u2666 ! \u207b\u00b9-hom\u2032\n \u2666 ap _\u207b\u00b9 (! ^\u207asuc n)\n\n ^\u207b\u20321-id : b ^\u207b\u2032 1 \u2261 b \u207b\u00b9\n ^\u207b\u20321-id = snd identity\n\n ^\u207b1-id : b ^\u207b 1 \u2261 b \u207b\u00b9\n ^\u207b1-id = ! ^\u207b\u2032-spec 1 \u2666 ^\u207b\u20321-id\n\n ^\u207b\u20322-\u2219 : b ^\u207b\u2032 2 \u2261 b \u207b\u00b9 \u2219 b \u207b\u00b9\n ^\u207b\u20322-\u2219 = ap (_\u2219_ _) ^\u207b\u20321-id\n\n ^\u207b2-\u2219 : b ^\u207b 2 \u2261 b \u207b\u00b9 \u2219 b \u207b\u00b9\n ^\u207b2-\u2219 = ! ^\u207b\u2032-spec 2 \u2666 ^\u207b\u20322-\u2219\n\nrecord Monoid (M : Set) : Set where\n field\n mon-ops : Monoid-Ops M\n mon-struct : Monoid-Struct mon-ops\n open Monoid-Ops mon-ops public\n open Monoid-Struct mon-struct public\n\nrecord Commutative-Monoid-Struct {\u2113} {M : Set \u2113} (mon-ops : Monoid-Ops M) : Set \u2113 where\n open Monoid-Ops mon-ops\n field\n mon-struct : Monoid-Struct mon-ops\n comm : Commutative _\u2219_\n open Monoid-Struct mon-struct public\n open FromAssocComm _\u2219_ assoc comm public\n hiding (!assoc=; assoc=; inner=)\n\nrecord Commutative-Monoid (M : Set) : Set where\n field\n mon-ops : Monoid-Ops M\n mon-comm : Commutative-Monoid-Struct mon-ops\n open Monoid-Ops mon-ops public\n open Commutative-Monoid-Struct mon-comm public\n mon : Monoid M\n mon = record { mon-struct = mon-struct }\n\n-- A renaming of Monoid with additive notation\nmodule Additive-Monoid {M} (mon : Monoid M) = Monoid mon\n renaming ( _\u2219_ to _+_; \u03b5 to 0\u1d50\n ; assoc to +-assoc; identity to +-identity\n ; \u2219= to +=\n )\n\n-- A renaming of Monoid with multiplicative notation\nmodule Multiplicative-Monoid {M} (mon : Monoid M) = Monoid mon\n renaming ( _\u2219_ to _*_; \u03b5 to 1\u1d50\n ; assoc to *-assoc; identity to *-identity\n ; \u2219= to *=\n )\n\nmodule Additive-Commutative-Monoid {M} (mon-comm : Commutative-Monoid M)\n = Commutative-Monoid mon-comm\n renaming ( _\u2219_ to _+_; \u03b5 to 0\u1d50\n ; assoc to +-assoc; identity to +-identity\n ; \u2219= to +=\n ; assoc= to +-assoc=\n ; !assoc= to +-!assoc=\n ; inner= to +-inner=\n ; assoc-comm to +-assoc-comm\n ; interchange to +-interchange\n ; outer= to +-outer=\n )\n\nmodule Multiplicative-Commutative-Monoid {M} (mon : Commutative-Monoid M) = Commutative-Monoid mon\n renaming ( _\u2219_ to _*_; \u03b5 to 1\u1d50\n ; assoc to *-assoc; identity to *-identity\n ; \u2219= to *=\n ; assoc= to *-assoc=\n ; !assoc= to *-!assoc=\n ; inner= to *-inner=\n ; assoc-comm to *-assoc-comm\n ; interchange to *-interchange\n ; outer= to *-outer=\n )\n\nrecord MonoidHomomorphism {A B : Set}\n (monA0+ : Monoid A)\n (monB1* : Monoid B)\n (f : A \u2192 B) : Set where\n open Additive-Monoid monA0+\n open Multiplicative-Monoid monB1*\n field\n 0-hom-1 : f 0\u1d50 \u2261 1\u1d50\n +-hom-* : \u2200 {x y} \u2192 f (x + y) \u2261 f x * f y\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"open import Function.NP\nopen import Data.Product.NP\nopen import Data.Nat\n using (\u2115; fold; zero)\n renaming (_+_ to _+\u2115_; _*_ to _*\u2115_; suc to 1+_)\nopen import Data.Integer\n using (\u2124; +_; -[1+_]; _\u2296_)\n renaming (_+_ to _+\u2124_; _*_ to _*\u2124_)\nopen import Relation.Binary.PropositionalEquality.NP renaming (_\u2219_ to _\u2666_)\nopen import Algebra.FunctionProperties.Eq\nopen \u2261-Reasoning\n\nmodule Algebra.Monoid where\n\nrecord Monoid-Ops {\u2113} (M : Set \u2113) : Set \u2113 where\n constructor mk\n infixl 7 _\u2219_\n\n field\n _\u2219_ : M \u2192 M \u2192 M\n \u03b5 : M\n\n _^\u207a_ : M \u2192 \u2115 \u2192 M\n x ^\u207a n = fold \u03b5 (_\u2219_ x) n\n\n module FromInverseOp\n (_\u207b\u00b9 : Op\u2081 M)\n where\n infixl 7 _\/_\n\n _\/_ : M \u2192 M \u2192 M\n x \/ y = x \u2219 y \u207b\u00b9\n\n open FromOp\u2082 _\/_ public renaming (op= to \/=)\n\n _^\u207b_ _^\u207b\u2032_ : M \u2192 \u2115 \u2192 M\n x ^\u207b n = (x ^\u207a n)\u207b\u00b9\n x ^\u207b\u2032 n = (x \u207b\u00b9)^\u207a n\n\n _^_ : M \u2192 \u2124 \u2192 M\n x ^ -[1+ n ] = x ^\u207b(1+ n)\n x ^ (+ n) = x ^\u207a n\n\nrecord Monoid-Struct {\u2113} {M : Set \u2113} (mon-ops : Monoid-Ops M) : Set \u2113 where\n open Monoid-Ops mon-ops\n\n -- laws\n field\n assoc : Associative _\u2219_\n identity : Identity \u03b5 _\u2219_\n\n open FromOp\u2082 _\u2219_ public renaming (op= to \u2219=)\n open FromAssoc _\u2219_ assoc public\n\n module _ {b} where\n ^\u207a0-\u03b5 : b ^\u207a 0 \u2261 \u03b5\n ^\u207a0-\u03b5 = idp\n\n ^\u207a1-id : b ^\u207a 1 \u2261 b\n ^\u207a1-id = snd identity\n\n ^\u207a2-\u2219 : b ^\u207a 2 \u2261 b \u2219 b\n ^\u207a2-\u2219 = ap (_\u2219_ _) ^\u207a1-id\n\n ^\u207a-+ : \u2200 m {n} \u2192 b ^\u207a (m +\u2115 n) \u2261 b ^\u207a m \u2219 b ^\u207a n\n ^\u207a-+ 0 = ! fst identity\n ^\u207a-+ (1+ m) = ap (_\u2219_ b) (^\u207a-+ m) \u2666 ! assoc\n\n ^\u207a-* : \u2200 m {n} \u2192 b ^\u207a(m *\u2115 n) \u2261 (b ^\u207a n)^\u207a m\n ^\u207a-* 0 = idp\n ^\u207a-* (1+ m) {n}\n = b ^\u207a (n +\u2115 m *\u2115 n) \u2261\u27e8 ^\u207a-+ n \u27e9\n b ^\u207a n \u2219 b ^\u207a(m *\u2115 n) \u2261\u27e8 ap (_\u2219_ (b ^\u207a n)) (^\u207a-* m) \u27e9\n b ^\u207a n \u2219 (b ^\u207a n)^\u207a m \u220e\n\n comm-\u03b5 : \u2200 {x} \u2192 x \u2219 \u03b5 \u2261 \u03b5 \u2219 x\n comm-\u03b5 = snd identity \u2666 ! fst identity\n\n module _ {c x y} (e : (x \u2219 y) \u2261 \u03b5) where\n elim-assoc= : (c \u2219 x) \u2219 y \u2261 c\n elim-assoc= = assoc \u2666 \u2219= idp e \u2666 snd identity\n\n elim-!assoc= : x \u2219 (y \u2219 c) \u2261 c\n elim-!assoc= = ! assoc \u2666 \u2219= e idp \u2666 fst identity\n\n module _ {c d x y} (e : (x \u2219 y) \u2261 \u03b5) where\n elim-inner= : (c \u2219 x) \u2219 (y \u2219 d) \u2261 c \u2219 d\n elim-inner= = assoc \u2666 ap (_\u2219_ c) (elim-!assoc= e)\n\n module FromLeftInverse\n (_\u207b\u00b9 : Op\u2081 M)\n (inv-l : LeftInverse \u03b5 _\u207b\u00b9 _\u2219_)\n where\n open FromInverseOp _\u207b\u00b9\n\n cancels-\u2219-left : LeftCancel _\u2219_\n cancels-\u2219-left {c} {x} {y} e\n = x \u2261\u27e8 ! fst identity \u27e9\n \u03b5 \u2219 x \u2261\u27e8 \u2219= (! inv-l) idp \u27e9\n c \u207b\u00b9 \u2219 c \u2219 x \u2261\u27e8 !assoc= e \u27e9\n c \u207b\u00b9 \u2219 c \u2219 y \u2261\u27e8 \u2219= inv-l idp \u27e9\n \u03b5 \u2219 y \u2261\u27e8 fst identity \u27e9\n y \u220e\n\n inv-r : RightInverse \u03b5 _\u207b\u00b9 _\u2219_\n inv-r = cancels-\u2219-left (! assoc \u2666 \u2219= inv-l idp \u2666 ! comm-\u03b5)\n\n \/-\u2219 : \u2200 {x y} \u2192 x \u2261 (x \/ y) \u2219 y\n \/-\u2219 {x} {y}\n = x \u2261\u27e8 ! snd identity \u27e9\n x \u2219 \u03b5 \u2261\u27e8 ap (_\u2219_ x) (! inv-l) \u27e9\n x \u2219 (y \u207b\u00b9 \u2219 y) \u2261\u27e8 ! assoc \u27e9\n (x \/ y) \u2219 y \u220e\n\n module FromRightInverse\n (_\u207b\u00b9 : Op\u2081 M)\n (inv-r : RightInverse \u03b5 _\u207b\u00b9 _\u2219_)\n where\n open FromInverseOp _\u207b\u00b9\n\n cancels-\u2219-right : RightCancel _\u2219_\n cancels-\u2219-right {c} {x} {y} e\n = x \u2261\u27e8 ! snd identity \u27e9\n x \u2219 \u03b5 \u2261\u27e8 \u2219= idp (! inv-r) \u27e9\n x \u2219 (c \u2219 c \u207b\u00b9) \u2261\u27e8 assoc= e \u27e9\n y \u2219 (c \u2219 c \u207b\u00b9) \u2261\u27e8 \u2219= idp inv-r \u27e9\n y \u2219 \u03b5 \u2261\u27e8 snd identity \u27e9\n y \u220e\n\n inv-l : LeftInverse \u03b5 _\u207b\u00b9 _\u2219_\n inv-l = cancels-\u2219-right (assoc \u2666 \u2219= idp inv-r \u2666 comm-\u03b5)\n\n module _ {x y} where\n is-\u03b5-left : x \u2261 \u03b5 \u2192 x \u2219 y \u2261 y\n is-\u03b5-left e = ap (\u03bb z \u2192 z \u2219 _) e \u2666 fst identity\n\n is-\u03b5-right : y \u2261 \u03b5 \u2192 x \u2219 y \u2261 x\n is-\u03b5-right e = ap (\u03bb z \u2192 _ \u2219 z) e \u2666 snd identity\n\n \u2219-\/ : x \u2261 (x \u2219 y) \/ y\n \u2219-\/\n = x \u2261\u27e8 ! snd identity \u27e9\n x \u2219 \u03b5 \u2261\u27e8 ap (_\u2219_ x) (! inv-r) \u27e9\n x \u2219 (y \/ y) \u2261\u27e8 ! assoc \u27e9\n (x \u2219 y) \/ y \u220e\n\n module _ {x y} where\n unique-\u03b5-left : x \u2219 y \u2261 y \u2192 x \u2261 \u03b5\n unique-\u03b5-left eq\n = x \u2261\u27e8 \u2219-\/ \u27e9\n (x \u2219 y) \/ y \u2261\u27e8 \/= eq idp \u27e9\n y \/ y \u2261\u27e8 inv-r \u27e9\n \u03b5 \u220e\n\n unique-\u03b5-right : x \u2219 y \u2261 x \u2192 y \u2261 \u03b5\n unique-\u03b5-right eq\n = y \u2261\u27e8 ! is-\u03b5-left inv-l \u27e9\n x \u207b\u00b9 \u2219 x \u2219 y \u2261\u27e8 assoc \u27e9\n x \u207b\u00b9 \u2219 (x \u2219 y) \u2261\u27e8 \u2219= idp eq \u27e9\n x \u207b\u00b9 \u2219 x \u2261\u27e8 inv-l \u27e9\n \u03b5 \u220e\n\n unique-\u207b\u00b9 : x \u2219 y \u2261 \u03b5 \u2192 x \u2261 y \u207b\u00b9\n unique-\u207b\u00b9 eq\n = x \u2261\u27e8 \u2219-\/ \u27e9\n (x \u2219 y) \/ y \u2261\u27e8 \/= eq idp \u27e9\n \u03b5 \/ y \u2261\u27e8 fst identity \u27e9\n y \u207b\u00b9 \u220e\n\n open FromLeftInverse _\u207b\u00b9 inv-l hiding (inv-r)\n\n \u03b5\u207b\u00b9-\u03b5 : \u03b5 \u207b\u00b9 \u2261 \u03b5\n \u03b5\u207b\u00b9-\u03b5 = unique-\u03b5-left inv-l\n\n involutive : Involutive _\u207b\u00b9\n involutive {x}\n = cancels-\u2219-right\n (x \u207b\u00b9 \u207b\u00b9 \u2219 x \u207b\u00b9 \u2261\u27e8 inv-l \u27e9\n \u03b5 \u2261\u27e8 ! inv-r \u27e9\n x \u2219 x \u207b\u00b9 \u220e)\n\n \u207b\u00b9-hom\u2032 : \u2200 {x y} \u2192 (x \u2219 y)\u207b\u00b9 \u2261 y \u207b\u00b9 \u2219 x \u207b\u00b9\n \u207b\u00b9-hom\u2032 {x} {y} = cancels-\u2219-left {x \u2219 y}\n ((x \u2219 y) \u2219 (x \u2219 y)\u207b\u00b9 \u2261\u27e8 inv-r \u27e9\n \u03b5 \u2261\u27e8 ! inv-r \u27e9\n x \u2219 x \u207b\u00b9 \u2261\u27e8 ap (_\u2219_ x) (! fst identity) \u27e9\n x \u2219 (\u03b5 \u2219 x \u207b\u00b9) \u2261\u27e8 \u2219= idp (\u2219= (! inv-r) idp) \u27e9\n x \u2219 ((y \u2219 y \u207b\u00b9) \u2219 x \u207b\u00b9) \u2261\u27e8 ap (_\u2219_ x) assoc \u27e9\n x \u2219 (y \u2219 (y \u207b\u00b9 \u2219 x \u207b\u00b9)) \u2261\u27e8 ! assoc \u27e9\n (x \u2219 y) \u2219 (y \u207b\u00b9 \u2219 x \u207b\u00b9) \u220e)\n\n elim-\u2219-left-\u207b\u00b9\u2219 : \u2200 {c x y} \u2192 (c \u2219 x)\u207b\u00b9 \u2219 (c \u2219 y) \u2261 x \u207b\u00b9 \u2219 y\n elim-\u2219-left-\u207b\u00b9\u2219 {c} {x} {y}\n = (c \u2219 x)\u207b\u00b9 \u2219 (c \u2219 y) \u2261\u27e8 \u2219= \u207b\u00b9-hom\u2032 idp \u27e9\n x \u207b\u00b9 \u2219 c \u207b\u00b9 \u2219 (c \u2219 y) \u2261\u27e8 elim-inner= inv-l \u27e9\n x \u207b\u00b9 \u2219 y \u220e\n\n elim-\u2219-right-\/ : \u2200 {c x y} \u2192 (x \u2219 c) \/ (y \u2219 c) \u2261 x \/ y\n elim-\u2219-right-\/ {c} {x} {y}\n = x \u2219 c \u2219 (y \u2219 c)\u207b\u00b9 \u2261\u27e8 ap (_\u2219_ _) \u207b\u00b9-hom\u2032 \u27e9\n x \u2219 c \u2219 (c \u207b\u00b9 \/ y) \u2261\u27e8 elim-inner= inv-r \u27e9\n x \/ y \u220e \n\n module _ {b} where\n ^\u207asuc : \u2200 n \u2192 b ^\u207a(1+ n) \u2261 b ^\u207a n \u2219 b\n ^\u207asuc 0 = comm-\u03b5\n ^\u207asuc (1+ n) = ap (_\u2219_ b) (^\u207asuc n) \u2666 ! assoc\n\n ^\u207a-comm : \u2200 n \u2192 b \u2219 b ^\u207a n \u2261 b ^\u207a n \u2219 b\n ^\u207a-comm = ^\u207asuc\n\n ^\u207bsuc : \u2200 n \u2192 b ^\u207b(1+ n) \u2261 b \u207b\u00b9 \u2219 b ^\u207b n\n ^\u207bsuc n = ap _\u207b\u00b9 (^\u207asuc n) \u2666 \u207b\u00b9-hom\u2032\n\n ^\u207b\u2032-spec : \u2200 n \u2192 b ^\u207b\u2032 n \u2261 b ^\u207b n\n ^\u207b\u2032-spec 0 = ! \u03b5\u207b\u00b9-\u03b5\n ^\u207b\u2032-spec (1+ n) = ap (_\u2219_ (b \u207b\u00b9)) (^\u207b\u2032-spec n)\n \u2666 ! \u207b\u00b9-hom\u2032\n \u2666 ap _\u207b\u00b9 (! ^\u207asuc n)\n\n ^\u207b\u20321-id : b ^\u207b\u2032 1 \u2261 b \u207b\u00b9\n ^\u207b\u20321-id = snd identity\n\n ^\u207b1-id : b ^\u207b 1 \u2261 b \u207b\u00b9\n ^\u207b1-id = ! ^\u207b\u2032-spec 1 \u2666 ^\u207b\u20321-id\n\n ^\u207b\u20322-\u2219 : b ^\u207b\u2032 2 \u2261 b \u207b\u00b9 \u2219 b \u207b\u00b9\n ^\u207b\u20322-\u2219 = ap (_\u2219_ _) ^\u207b\u20321-id\n\n ^\u207b2-\u2219 : b ^\u207b 2 \u2261 b \u207b\u00b9 \u2219 b \u207b\u00b9\n ^\u207b2-\u2219 = ! ^\u207b\u2032-spec 2 \u2666 ^\u207b\u20322-\u2219\n\nrecord Monoid (M : Set) : Set where\n field\n mon-ops : Monoid-Ops M\n mon-struct : Monoid-Struct mon-ops\n open Monoid-Ops mon-ops public\n open Monoid-Struct mon-struct public\n\nrecord Commutative-Monoid-Struct {\u2113} {M : Set \u2113} (mon-ops : Monoid-Ops M) : Set \u2113 where\n open Monoid-Ops mon-ops\n field\n mon-struct : Monoid-Struct mon-ops\n comm : Commutative _\u2219_\n open Monoid-Struct mon-struct public\n open FromAssocComm _\u2219_ assoc comm public\n hiding (!assoc=; assoc=; inner=)\n\nrecord Commutative-Monoid (M : Set) : Set where\n field\n mon-ops : Monoid-Ops M\n mon-comm : Commutative-Monoid-Struct mon-ops\n open Monoid-Ops mon-ops public\n open Commutative-Monoid-Struct mon-comm public\n mon : Monoid M\n mon = record { mon-struct = mon-struct }\n\n-- A renaming of Monoid with additive notation\nmodule Additive-Monoid {M} (mon : Monoid M) = Monoid mon\n renaming ( _\u2219_ to _+_; \u03b5 to 0\u1d50\n ; assoc to +-assoc; identity to +-identity\n ; \u2219= to +=\n )\n\n-- A renaming of Monoid with multiplicative notation\nmodule Multiplicative-Monoid {M} (mon : Monoid M) = Monoid mon\n renaming ( _\u2219_ to _*_; \u03b5 to 1\u1d50\n ; assoc to *-assoc; identity to *-identity\n ; \u2219= to *=\n )\n\nmodule Additive-Commutative-Monoid {M} (mon-comm : Commutative-Monoid M)\n = Commutative-Monoid mon-comm\n renaming ( _\u2219_ to _+_; \u03b5 to 0\u1d50\n ; assoc to +-assoc; identity to +-identity\n ; \u2219= to +=\n ; assoc= to +-assoc=\n ; !assoc= to +-!assoc=\n ; inner= to +-inner=\n ; assoc-comm to +-assoc-comm\n ; interchange to +-interchange\n ; outer= to +-outer=\n )\n\nmodule Multiplicative-Commutative-Monoid {M} (mon : Commutative-Monoid M) = Commutative-Monoid mon\n renaming ( _\u2219_ to _*_; \u03b5 to 1\u1d50\n ; assoc to *-assoc; identity to *-identity\n ; \u2219= to *=\n ; assoc= to *-assoc=\n ; !assoc= to *-!assoc=\n ; inner= to *-inner=\n ; assoc-comm to *-assoc-comm\n ; interchange to *-interchange\n ; outer= to *-outer=\n )\n\nrecord MonoidHomomorphism {A B : Set}\n (monA0+ : Monoid A)\n (monB1* : Monoid B)\n (f : A \u2192 B) : Set where\n open Additive-Monoid monA0+\n open Multiplicative-Monoid monB1*\n field\n 0-hom-1 : f 0\u1d50 \u2261 1\u1d50\n +-hom-* : \u2200 {x y} \u2192 f (x + y) \u2261 f x * f y\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"66e4306a6d9260a51adfb53ab8e82a87b0e8ff9c","subject":"Prove that nil changes are derivatives.","message":"Prove that nil changes are derivatives.\n\nThe point of these simple definitions is to rephrase correct in more\nhigh-level terms. In the definition of correct, we inline update and\ndiff for function changes to avoid mutual recursion.\n\nOld-commit-hash: a2aff54dae7f67a166745a2df2acd581140bc7b9\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Algebra.agda","new_file":"Base\/Change\/Algebra.agda","new_contents":"module Base.Change.Algebra where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Change Algebras\n\nrecord IsChangeAlgebra\n {c} {d}\n {Carrier : Set c}\n (Change : Carrier \u2192 Set d)\n (update : (v : Carrier) (dv : Change v) \u2192 Carrier)\n (diff : (u v : Carrier) \u2192 Change v) : Set (c \u2294 d) where\n field\n update-diff : \u2200 u v \u2192 update v (diff u v) \u2261 u\n\n nil : \u2200 v \u2192 Change v\n nil v = diff v v\n\n update-nil : \u2200 v \u2192 update v (nil v) \u2261 v\n update-nil v = update-diff v v\n\nrecord ChangeAlgebra {c} \u2113\n (Carrier : Set c) : Set (c \u2294 suc \u2113) where\n field\n Change : Carrier \u2192 Set \u2113\n update : (v : Carrier) (dv : Change v) \u2192 Carrier\n diff : (u v : Carrier) \u2192 Change v\n\n isChangeAlgebra : IsChangeAlgebra Change update diff\n\n\n infixl 6 update diff\n\n open IsChangeAlgebra isChangeAlgebra public\n\nopen ChangeAlgebra {{...}} public\n using\n ( update-diff\n ; update-nil\n ; nil\n )\n renaming\n ( Change to \u0394\n ; update to _\u229e_\n ; diff to _\u229f_\n )\n\n-- Families of change algebras\n\nrecord ChangeAlgebraFamily {a p} \u2113 {A : Set a} (P : A \u2192 Set p): Set (suc \u2113 \u2294 p \u2294 a) where\n constructor\n family\n field\n change-algebra : \u2200 x \u2192 ChangeAlgebra \u2113 (P x)\n\n module _ x where\n open ChangeAlgebra (change-algebra x) public\n\nmodule Family = ChangeAlgebraFamily {{...}}\n\nopen Family public\n using\n (\n )\n renaming\n ( Change to \u0394\u208d_\u208e\n ; nil to nil\u208d_\u208e\n ; update-diff to update-diff\u208d_\u208e\n ; update-nil to update-nil\u208d_\u208e\n ; change-algebra to change-algebra\u208d_\u208e\n )\n\ninfixl 6 update\u2032 diff\u2032\n\nupdate\u2032 = Family.update\nsyntax update\u2032 x v dv = v \u229e\u208d x \u208e dv\n\ndiff\u2032 = Family.diff\nsyntax diff\u2032 x u v = u \u229f\u208d x \u208e v\n\n-- Derivatives\n\nDerivative : \u2200 {a b c d} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra c A}} \u2192\n {{CB : ChangeAlgebra d B}} \u2192\n (f : A \u2192 B) \u2192\n (df : (a : A) (da : \u0394 a) \u2192 \u0394 (f a)) \u2192\n Set (a \u2294 b \u2294 c)\nDerivative f df = \u2200 a da \u2192 f a \u229e df a da \u2261 f (a \u229e da)\n\n-- Abelian groups induce change algebras\n\nopen import Algebra.Structures\nopen import Data.Product\nopen import Function\n\nmodule GroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isAbelianGroup : IsAbelianGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsAbelianGroup isAbelianGroup\n hiding\n ( refl\n )\n renaming\n ( _-_ to _\u229d_\n ; \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n changeAlgebra : ChangeAlgebra a A\n changeAlgebra = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8 comm _ _ \u27e9\n (u \u229d v ) \u2295 v\n \u2261\u27e8\u27e9\n (u \u2295 (v \u207b\u00b9)) \u2295 v\n \u2261\u27e8 assoc _ _ _ \u27e9\n u \u2295 ((v \u207b\u00b9) \u2295 v)\n \u2261\u27e8 refl \u27e8\u2295\u27e9 proj\u2081 inverse v \u27e9\n u \u2295 \u03b5\n \u2261\u27e8 proj\u2082 identity u \u27e9\n u\n \u220e\n }\n }\n\n-- Function changes\n\nmodule FunctionChanges\n {a} {b} {c} {d} (A : Set a) (B : Set b) {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}}\n where\n record FunctionChange (f : A \u2192 B) : Set (a \u2294 b \u2294 c \u2294 d) where\n constructor\n cons\n field\n apply : (a : A) (da : \u0394 a) \u2192\n \u0394 (f a)\n correct : (a : A) (da : \u0394 a) \u2192\n f (a \u229e da) \u229e apply (a \u229e da) (nil (a \u229e da)) \u2261 f a \u229e apply a da\n\n open FunctionChange\n open \u2261-Reasoning\n open import Postulate.Extensionality\n\n changeAlgebra : ChangeAlgebra (a \u2294 b \u2294 c \u2294 d) (A \u2192 B)\n changeAlgebra = record\n { Change = FunctionChange\n ; update = \u03bb f df a \u2192 f a \u229e apply df a (nil a)\n ; diff = \u03bb g f \u2192 record\n { apply = \u03bb a da \u2192 g (a \u229e da) \u229f f a\n ; correct = \u03bb a da \u2192\n begin\n f (a \u229e da) \u229e (g ((a \u229e da) \u229e nil (a \u229e da)) \u229f f (a \u229e da))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f (a \u229e da) \u229e (g \u25a1 \u229f f (a \u229e da)))\n (update-nil (a \u229e da)) \u27e9\n f (a \u229e da) \u229e (g (a \u229e da) \u229f f (a \u229e da))\n \u2261\u27e8 update-diff (g (a \u229e da)) (f (a \u229e da)) \u27e9\n g (a \u229e da)\n \u2261\u27e8 sym (update-diff (g (a \u229e da)) (f a)) \u27e9\n f a \u229e (g (a \u229e da) \u229f f a)\n \u220e\n }\n ; isChangeAlgebra = record\n { update-diff = \u03bb g f \u2192 ext (\u03bb a \u2192\n begin\n f a \u229e (g (a \u229e nil a) \u229f f a)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f a \u229e (g \u25a1 \u229f f a)) (update-nil a) \u27e9\n f a \u229e (g a \u229f f a)\n \u2261\u27e8 update-diff (g a) (f a) \u27e9\n g a\n \u220e)\n }\n }\n\n incrementalization : \u2200 (f : A \u2192 B) df a da \u2192\n (f \u229e df) (a \u229e da) \u2261 f a \u229e apply df a da\n incrementalization f df a da = correct df a da\n\n nil-is-derivative : \u2200 (f : A \u2192 B) \u2192\n Derivative f (apply (nil f))\n nil-is-derivative f a da =\n begin\n f a \u229e apply (nil f) a da\n \u2261\u27e8 sym (incrementalization f (nil f) a da) \u27e9\n (f \u229e nil f) (a \u229e da)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (a \u229e da))\n (update-nil f) \u27e9\n f (a \u229e da)\n \u220e\n","old_contents":"module Base.Change.Algebra where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Change Algebras\n\nrecord IsChangeAlgebra\n {c} {d}\n {Carrier : Set c}\n (Change : Carrier \u2192 Set d)\n (update : (v : Carrier) (dv : Change v) \u2192 Carrier)\n (diff : (u v : Carrier) \u2192 Change v) : Set (c \u2294 d) where\n field\n update-diff : \u2200 u v \u2192 update v (diff u v) \u2261 u\n\n nil : \u2200 v \u2192 Change v\n nil v = diff v v\n\n update-nil : \u2200 v \u2192 update v (nil v) \u2261 v\n update-nil v = update-diff v v\n\nrecord ChangeAlgebra {c} \u2113\n (Carrier : Set c) : Set (c \u2294 suc \u2113) where\n field\n Change : Carrier \u2192 Set \u2113\n update : (v : Carrier) (dv : Change v) \u2192 Carrier\n diff : (u v : Carrier) \u2192 Change v\n\n isChangeAlgebra : IsChangeAlgebra Change update diff\n\n\n infixl 6 update diff\n\n open IsChangeAlgebra isChangeAlgebra public\n\nopen ChangeAlgebra {{...}} public\n using\n ( update-diff\n ; update-nil\n ; nil\n )\n renaming\n ( Change to \u0394\n ; update to _\u229e_\n ; diff to _\u229f_\n )\n\n-- Families of change algebras\n\nrecord ChangeAlgebraFamily {a p} \u2113 {A : Set a} (P : A \u2192 Set p): Set (suc \u2113 \u2294 p \u2294 a) where\n constructor\n family\n field\n change-algebra : \u2200 x \u2192 ChangeAlgebra \u2113 (P x)\n\n module _ x where\n open ChangeAlgebra (change-algebra x) public\n\nmodule Family = ChangeAlgebraFamily {{...}}\n\nopen Family public\n using\n (\n )\n renaming\n ( Change to \u0394\u208d_\u208e\n ; nil to nil\u208d_\u208e\n ; update-diff to update-diff\u208d_\u208e\n ; update-nil to update-nil\u208d_\u208e\n ; change-algebra to change-algebra\u208d_\u208e\n )\n\ninfixl 6 update\u2032 diff\u2032\n\nupdate\u2032 = Family.update\nsyntax update\u2032 x v dv = v \u229e\u208d x \u208e dv\n\ndiff\u2032 = Family.diff\nsyntax diff\u2032 x u v = u \u229f\u208d x \u208e v\n\n-- Abelian groups induce change algebras\n\nopen import Algebra.Structures\nopen import Data.Product\nopen import Function\n\nmodule GroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isAbelianGroup : IsAbelianGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsAbelianGroup isAbelianGroup\n hiding\n ( refl\n )\n renaming\n ( _-_ to _\u229d_\n ; \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n changeAlgebra : ChangeAlgebra a A\n changeAlgebra = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8 comm _ _ \u27e9\n (u \u229d v ) \u2295 v\n \u2261\u27e8\u27e9\n (u \u2295 (v \u207b\u00b9)) \u2295 v\n \u2261\u27e8 assoc _ _ _ \u27e9\n u \u2295 ((v \u207b\u00b9) \u2295 v)\n \u2261\u27e8 refl \u27e8\u2295\u27e9 proj\u2081 inverse v \u27e9\n u \u2295 \u03b5\n \u2261\u27e8 proj\u2082 identity u \u27e9\n u\n \u220e\n }\n }\n\n-- Function changes\n\nmodule FunctionChanges\n {a} {b} {c} {d} (A : Set a) (B : Set b) {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}}\n where\n record FunctionChange (f : A \u2192 B) : Set (a \u2294 b \u2294 c \u2294 d) where\n constructor\n cons\n field\n apply : (a : A) (da : \u0394 a) \u2192\n \u0394 (f a)\n correct : (a : A) (da : \u0394 a) \u2192\n f (a \u229e da) \u229e apply (a \u229e da) (nil (a \u229e da)) \u2261 f a \u229e apply a da\n\n open FunctionChange\n open \u2261-Reasoning\n open import Postulate.Extensionality\n\n changeAlgebra : ChangeAlgebra (a \u2294 b \u2294 c \u2294 d) (A \u2192 B)\n changeAlgebra = record\n { Change = FunctionChange\n ; update = \u03bb f df a \u2192 f a \u229e apply df a (nil a)\n ; diff = \u03bb g f \u2192 record\n { apply = \u03bb a da \u2192 g (a \u229e da) \u229f f a\n ; correct = \u03bb a da \u2192\n begin\n f (a \u229e da) \u229e (g ((a \u229e da) \u229e nil (a \u229e da)) \u229f f (a \u229e da))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f (a \u229e da) \u229e (g \u25a1 \u229f f (a \u229e da)))\n (update-nil (a \u229e da)) \u27e9\n f (a \u229e da) \u229e (g (a \u229e da) \u229f f (a \u229e da))\n \u2261\u27e8 update-diff (g (a \u229e da)) (f (a \u229e da)) \u27e9\n g (a \u229e da)\n \u2261\u27e8 sym (update-diff (g (a \u229e da)) (f a)) \u27e9\n f a \u229e (g (a \u229e da) \u229f f a)\n \u220e\n }\n ; isChangeAlgebra = record\n { update-diff = \u03bb g f \u2192 ext (\u03bb a \u2192\n begin\n f a \u229e (g (a \u229e nil a) \u229f f a)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f a \u229e (g \u25a1 \u229f f a)) (update-nil a) \u27e9\n f a \u229e (g a \u229f f a)\n \u2261\u27e8 update-diff (g a) (f a) \u27e9\n g a\n \u220e)\n }\n }\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"058d6bf14a3aa315b9ba2d681bb621847c3aa089","subject":"Simplified some proofs.","message":"Simplified some proofs.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/LTC-PCF\/Data\/Nat\/Inequalities\/Properties.agda","new_file":"src\/fot\/LTC-PCF\/Data\/Nat\/Inequalities\/Properties.agda","new_contents":"------------------------------------------------------------------------------\n-- Properties of the inequalities\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule LTC-PCF.Data.Nat.Inequalities.Properties where\n\nopen import Common.FOL.Relation.Binary.EqReasoning\n\nopen import LTC-PCF.Base\nopen import LTC-PCF.Base.Properties\nopen import LTC-PCF.Data.Nat\nopen import LTC-PCF.Data.Nat.Inequalities\nopen import LTC-PCF.Data.Nat.Inequalities.EliminationProperties\nopen import LTC-PCF.Data.Nat.Inequalities.ConversionRules public\nopen import LTC-PCF.Data.Nat.Properties\nopen import LTC-PCF.Data.Nat.UnaryNumbers\n\n------------------------------------------------------------------------------\n-- N.B. The elimination properties are in the module\n-- LTC.Data.Nat.Inequalities.EliminationProperties.\n\n------------------------------------------------------------------------------\n-- Congruence properties\n\nltLeftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 lt m o \u2261 lt n o\nltLeftCong refl = refl\n\nltRightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 lt m n \u2261 lt m o\nltRightCong refl = refl\n\n------------------------------------------------------------------------------\n\n0\u226fx : \u2200 {n} \u2192 N n \u2192 zero \u226f n\n0\u226fx nzero = lt-00\n0\u226fx (nsucc {n} Nn) = lt-S0 n\n\nx\u226ex : \u2200 {n} \u2192 N n \u2192 n \u226e n\nx\u226ex nzero = lt-00\nx\u226ex (nsucc {n} Nn) = trans (lt-SS n n) (x\u226ex Nn)\n\nSx\u22700 : \u2200 {n} \u2192 N n \u2192 succ\u2081 n \u2270 zero\nSx\u22700 nzero = x\u226ex (nsucc nzero)\nSx\u22700 (nsucc {n} Nn) = trans (lt-SS (succ\u2081 n) zero) (lt-S0 n)\n\nxy\u2228x\u2264y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2228 m \u2264 n\nx>y\u2228x\u2264y {n = n} nzero Nn = inj\u2082 (lt-0S n)\nx>y\u2228x\u2264y (nsucc {m} Nm) nzero = inj\u2081 (lt-0S m)\nx>y\u2228x\u2264y (nsucc {m} Nm) (nsucc {n} Nn) =\n case (\u03bb m>n \u2192 inj\u2081 (trans (lt-SS n m) m>n))\n (\u03bb m\u2264n \u2192 inj\u2082 (trans (lt-SS m (succ\u2081 n)) m\u2264n))\n (x>y\u2228x\u2264y Nm Nn)\n\nx\u2264y\u2192x\u226fy : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m \u2264 n \u2192 m \u226f n\nx\u2264y\u2192x\u226fy nzero Nn _ = 0\u226fx Nn\nx\u2264y\u2192x\u226fy (nsucc Nm) nzero Sm\u22640 = \u22a5-elim (S\u22640\u2192\u22a5 Nm Sm\u22640)\nx\u2264y\u2192x\u226fy (nsucc {m} Nm) (nsucc {n} Nn) Sm\u2264Sn =\n trans (lt-SS n m) (x\u2264y\u2192x\u226fy Nm Nn (trans (sym (lt-SS m (succ\u2081 n))) Sm\u2264Sn))\n\nx\u226fy\u2192x\u2264y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m \u226f n \u2192 m \u2264 n\nx\u226fy\u2192x\u2264y {n = n} nzero Nn _ = lt-0S n\nx\u226fy\u2192x\u2264y (nsucc {m} Nm) nzero Sm\u226f0 = \u22a5-elim (t\u2262f (trans (sym (lt-0S m)) Sm\u226f0))\nx\u226fy\u2192x\u2264y (nsucc {m} Nm) (nsucc {n} Nn) Sm\u226fSn =\n trans (lt-SS m (succ\u2081 n)) (x\u226fy\u2192x\u2264y Nm Nn (trans (sym (lt-SS n m)) Sm\u226fSn))\n\nx>y\u2228x\u226fy : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2228 m \u226f n\nx>y\u2228x\u226fy nzero Nn = inj\u2082 (0\u226fx Nn)\nx>y\u2228x\u226fy (nsucc {m} Nm) nzero = inj\u2081 (lt-0S m)\nx>y\u2228x\u226fy (nsucc {m} Nm) (nsucc {n} Nn) =\n case (\u03bb h \u2192 inj\u2081 (trans (lt-SS n m) h))\n (\u03bb h \u2192 inj\u2082 (trans (lt-SS n m) h))\n (x>y\u2228x\u226fy Nm Nn)\n\nxy\u2228x\u2264y Nn Nm\n\nxy\u2192x\u2238y+y\u2261x : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2192 (m \u2238 n) + n \u2261 m\nx>y\u2192x\u2238y+y\u2261x nzero Nn 0>n = \u22a5-elim (0>x\u2192\u22a5 Nn 0>n)\nx>y\u2192x\u2238y+y\u2261x (nsucc {m} Nm) nzero Sm>0 =\n trans (+-rightIdentity (\u2238-N (nsucc Nm) nzero)) (\u2238-x0 (succ\u2081 m))\nx>y\u2192x\u2238y+y\u2261x (nsucc {m} Nm) (nsucc {n} Nn) Sm>Sn =\n (succ\u2081 m \u2238 succ\u2081 n) + succ\u2081 n\n \u2261\u27e8 +-leftCong (S\u2238S Nm Nn) \u27e9\n (m \u2238 n) + succ\u2081 n\n \u2261\u27e8 +-comm (\u2238-N Nm Nn) (nsucc Nn) \u27e9\n succ\u2081 n + (m \u2238 n)\n \u2261\u27e8 +-Sx n (m \u2238 n) \u27e9\n succ\u2081 (n + (m \u2238 n))\n \u2261\u27e8 succCong (+-comm Nn (\u2238-N Nm Nn)) \u27e9\n succ\u2081 ((m \u2238 n) + n)\n \u2261\u27e8 succCong (x>y\u2192x\u2238y+y\u2261x Nm Nn (trans (sym (lt-SS n m)) Sm>Sn)) \u27e9\n succ\u2081 m \u220e\n\nx\u2264y\u2192y\u2238x+x\u2261y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m \u2264 n \u2192 (n \u2238 m) + m \u2261 n\nx\u2264y\u2192y\u2238x+x\u2261y {n = n} nzero Nn 0\u2264n =\n trans (+-rightIdentity (\u2238-N Nn nzero)) (\u2238-x0 n)\nx\u2264y\u2192y\u2238x+x\u2261y (nsucc Nm) nzero Sm\u22640 = \u22a5-elim (S\u22640\u2192\u22a5 Nm Sm\u22640)\nx\u2264y\u2192y\u2238x+x\u2261y (nsucc {m} Nm) (nsucc {n} Nn) Sm\u2264Sn =\n (succ\u2081 n \u2238 succ\u2081 m) + succ\u2081 m\n \u2261\u27e8 +-leftCong (S\u2238S Nn Nm) \u27e9\n (n \u2238 m) + succ\u2081 m\n \u2261\u27e8 +-comm (\u2238-N Nn Nm) (nsucc Nm) \u27e9\n succ\u2081 m + (n \u2238 m)\n \u2261\u27e8 +-Sx m (n \u2238 m) \u27e9\n succ\u2081 (m + (n \u2238 m))\n \u2261\u27e8 succCong (+-comm Nm (\u2238-N Nn Nm)) \u27e9\n succ\u2081 ((n \u2238 m) + m)\n \u2261\u27e8 succCong (x\u2264y\u2192y\u2238x+x\u2261y Nm Nn (trans (sym (lt-SS m (succ\u2081 n))) Sm\u2264Sn)) \u27e9\n succ\u2081 n \u220e\n\nx0\u2192x\u2238y zero \u2192 m \u2238 n < m\nx\u2265y\u2192y>0\u2192x\u2238y0 = \u22a5-elim (x>x\u2192\u22a5 nzero 0>0)\nx\u2265y\u2192y>0\u2192x\u2238y0\u2192x\u2238y0 =\n lt (succ\u2081 m \u2238 succ\u2081 n) (succ\u2081 m)\n \u2261\u27e8 ltLeftCong (S\u2238S Nm Nn) \u27e9\n lt (m \u2238 n) (succ\u2081 m)\n \u2261\u27e8 x\u2238yy\u2228x\u2264y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2228 m \u2264 n\nx>y\u2228x\u2264y {n = n} nzero Nn = inj\u2082 (lt-0S n)\nx>y\u2228x\u2264y (nsucc {m} Nm) nzero = inj\u2081 (lt-0S m)\nx>y\u2228x\u2264y (nsucc {m} Nm) (nsucc {n} Nn) =\n case (\u03bb m>n \u2192 inj\u2081 (trans (lt-SS n m) m>n))\n (\u03bb m\u2264n \u2192 inj\u2082 (trans (lt-SS m (succ\u2081 n)) m\u2264n))\n (x>y\u2228x\u2264y Nm Nn)\n\nx\u2264y\u2192x\u226fy : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m \u2264 n \u2192 m \u226f n\nx\u2264y\u2192x\u226fy nzero Nn _ = 0\u226fx Nn\nx\u2264y\u2192x\u226fy (nsucc Nm) nzero Sm\u22640 = \u22a5-elim (S\u22640\u2192\u22a5 Nm Sm\u22640)\nx\u2264y\u2192x\u226fy (nsucc {m} Nm) (nsucc {n} Nn) Sm\u2264Sn =\n trans (lt-SS n m) (x\u2264y\u2192x\u226fy Nm Nn (trans (sym (lt-SS m (succ\u2081 n))) Sm\u2264Sn))\n\nx\u226fy\u2192x\u2264y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m \u226f n \u2192 m \u2264 n\nx\u226fy\u2192x\u2264y {n = n} nzero Nn _ = lt-0S n\nx\u226fy\u2192x\u2264y (nsucc {m} Nm) nzero Sm\u226f0 = \u22a5-elim (t\u2262f (trans (sym (lt-0S m)) Sm\u226f0))\nx\u226fy\u2192x\u2264y (nsucc {m} Nm) (nsucc {n} Nn) Sm\u226fSn =\n trans (lt-SS m (succ\u2081 n)) (x\u226fy\u2192x\u2264y Nm Nn (trans (sym (lt-SS n m)) Sm\u226fSn))\n\nx>y\u2228x\u226fy : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2228 m \u226f n\nx>y\u2228x\u226fy nzero Nn = inj\u2082 (0\u226fx Nn)\nx>y\u2228x\u226fy (nsucc {m} Nm) nzero = inj\u2081 (lt-0S m)\nx>y\u2228x\u226fy (nsucc {m} Nm) (nsucc {n} Nn) =\n case (\u03bb h \u2192 inj\u2081 (trans (lt-SS n m) h))\n (\u03bb h \u2192 inj\u2082 (trans (lt-SS n m) h))\n (x>y\u2228x\u226fy Nm Nn)\n\nxy\u2228x\u2264y Nn Nm\n\nxy\u2192x\u2238y+y\u2261x : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2192 (m \u2238 n) + n \u2261 m\nx>y\u2192x\u2238y+y\u2261x nzero Nn 0>n = \u22a5-elim (0>x\u2192\u22a5 Nn 0>n)\nx>y\u2192x\u2238y+y\u2261x (nsucc {m} Nm) nzero Sm>0 =\n trans (+-rightIdentity (\u2238-N (nsucc Nm) nzero)) (\u2238-x0 (succ\u2081 m))\nx>y\u2192x\u2238y+y\u2261x (nsucc {m} Nm) (nsucc {n} Nn) Sm>Sn =\n (succ\u2081 m \u2238 succ\u2081 n) + succ\u2081 n\n \u2261\u27e8 +-leftCong (S\u2238S Nm Nn) \u27e9\n (m \u2238 n) + succ\u2081 n\n \u2261\u27e8 +-comm (\u2238-N Nm Nn) (nsucc Nn) \u27e9\n succ\u2081 n + (m \u2238 n)\n \u2261\u27e8 +-Sx n (m \u2238 n) \u27e9\n succ\u2081 (n + (m \u2238 n))\n \u2261\u27e8 succCong (+-comm Nn (\u2238-N Nm Nn)) \u27e9\n succ\u2081 ((m \u2238 n) + n)\n \u2261\u27e8 succCong (x>y\u2192x\u2238y+y\u2261x Nm Nn (trans (sym (lt-SS n m)) Sm>Sn)) \u27e9\n succ\u2081 m \u220e\n\nx\u2264y\u2192y\u2238x+x\u2261y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m \u2264 n \u2192 (n \u2238 m) + m \u2261 n\nx\u2264y\u2192y\u2238x+x\u2261y {n = n} nzero Nn 0\u2264n =\n trans (+-rightIdentity (\u2238-N Nn nzero)) (\u2238-x0 n)\nx\u2264y\u2192y\u2238x+x\u2261y (nsucc Nm) nzero Sm\u22640 = \u22a5-elim (S\u22640\u2192\u22a5 Nm Sm\u22640)\nx\u2264y\u2192y\u2238x+x\u2261y (nsucc {m} Nm) (nsucc {n} Nn) Sm\u2264Sn =\n (succ\u2081 n \u2238 succ\u2081 m) + succ\u2081 m\n \u2261\u27e8 +-leftCong (S\u2238S Nn Nm) \u27e9\n (n \u2238 m) + succ\u2081 m\n \u2261\u27e8 +-comm (\u2238-N Nn Nm) (nsucc Nm) \u27e9\n succ\u2081 m + (n \u2238 m)\n \u2261\u27e8 +-Sx m (n \u2238 m) \u27e9\n succ\u2081 (m + (n \u2238 m))\n \u2261\u27e8 succCong (+-comm Nm (\u2238-N Nn Nm)) \u27e9\n succ\u2081 ((n \u2238 m) + m)\n \u2261\u27e8 succCong (x\u2264y\u2192y\u2238x+x\u2261y Nm Nn (trans (sym (lt-SS m (succ\u2081 n))) Sm\u2264Sn)) \u27e9\n succ\u2081 n \u220e\n\nx0\u2192x\u2238y zero \u2192 m \u2238 n < m\nx\u2265y\u2192y>0\u2192x\u2238y0 = \u22a5-elim (x>x\u2192\u22a5 nzero 0>0)\nx\u2265y\u2192y>0\u2192x\u2238y0\u2192x\u2238y0 =\n lt (succ\u2081 m \u2238 succ\u2081 n) (succ\u2081 m)\n \u2261\u27e8 ltLeftCong (S\u2238S Nm Nn) \u27e9\n lt (m \u2238 n) (succ\u2081 m)\n \u2261\u27e8 x\u2238y = 0#; <1b> = 0#; cond = 1#; fork = 1+_+_D; tt = 0#;\n <_,_> = _\u2218\u2032_; fst = 0#; snd = 0#;\n dup = 0#; first = F.id; swap = 0#; assoc = 0#;\n = 0#; snd = 0#;\n <_\u00d7_> = _\u2218\u2032_; second = F.id;\n <[]> = 0#; <\u2237> = 0#; uncons = 0# }\n where open D\n\nmodule SeqTimeOpsD where\n Time = Diff\u2115\n open D\n open FunOps seqTimeOpsD public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2257 0#\n snoc\u22610 zero x = \u2261.refl\n snoc\u22610 (suc n) x rewrite snoc\u22610 n x = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2257 0#\n reverse\u22610 zero x = \u2261.refl\n reverse\u22610 (suc n) x rewrite reverse\u22610 n x | snoc\u22610 n x = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2257 0#\n replicate\u22610 zero = \u03bb _ \u2192 \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2257 0#\n constBit\u22610 true {-1b-} _ = \u2261.refl\n constBit\u22610 false {-0b-} _ = \u2261.refl\n\n sum\u2032 : \u2200 {n} \u2192 Vec Diff\u2115 n \u2192 Diff\u2115\n sum\u2032 = V.foldr (const Diff\u2115) _\u2218\u2032_ id\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec {n} f xs \u2257 sum\u2032 (V.map f xs)\n constVec\u2261sum f [] x = \u2261.refl\n constVec\u2261sum {suc n} f (b \u2237 bs) x rewrite \u2115\u00b0.+-comm (f b x \u2294 constVec f bs x) 0\n | constVec\u2261sum f bs x = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2257 0#\n constBits\u22610 [] x = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) x rewrite constBits\u22610 bs x\n | constBit\u22610 b x = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2257 m *# n\n foldl\u2261* zero n x = \u2261.refl\n foldl\u2261* (suc m) n x rewrite foldl\u2261* m n (n x) = \u2261.refl\n\n foldr\u2261* : \u2200 m n \u2192 foldr {m} n \u2257 m *#\u2032 n\n foldr\u2261* zero n x = \u2261.refl\n foldr\u2261* (suc m) n x rewrite foldr\u2261* m n x = \u2261.refl\n\n #nodes : \u2200 i \u2192 Diff\u2115\n #nodes zero = 0#\n #nodes (suc i) = 1# +# #nodes i +# #nodes i\n\n fromBitsFun\u2261 : \u2200 {i o} (f : i \u2192\u1d47 o) \u2192 fromBitsFun f \u2257 #nodes i\n fromBitsFun\u2261 {zero} f x = constBits\u22610 (f []) x\n fromBitsFun\u2261 {suc i} f x rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_) x\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_) (#nodes i x) = \u2261.refl\n-}\n\nTime = \u2115\nTimeCost = constFuns Time\nSpace = \u2115\nSpaceCost = constFuns Space\n\nseqTimeCat : Category {\u2080} {\u2080} {\ud835\udfd9} (\u03bb _ _ \u2192 Time)\nseqTimeCat = 0 , _+_\n\nseqTimeLin : LinRewiring TimeCost\nseqTimeLin =\n record {\n cat = seqTimeCat;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _+_;\n second = F.id;\n tt\u2192[] = 0;\n []\u2192tt = 0;\n <\u2237> = 0;\n uncons = 0 }\n\nseqTimeRewiring : Rewiring TimeCost\nseqTimeRewiring =\n record {\n linRewiring = seqTimeLin;\n tt = 0;\n dup = 0;\n <[]> = 0;\n <_,_> = _+_;\n fst = 0;\n snd = 0;\n rewire = const 0;\n rewireTbl = const 0 }\n\nseqTimeFork : HasFork TimeCost\nseqTimeFork = record { cond = 1; fork = 1+_+_ }\n\nseqTimeOps : FunOps TimeCost\nseqTimeOps = record { rewiring = seqTimeRewiring; hasFork = seqTimeFork;\n <0b> = 0; <1b> = 0 }\n\nseqTimeBij : Bijective TimeCost\nseqTimeBij = FunOps.bijective seqTimeOps\n\ntimeCat : Category (\u03bb _ _ \u2192 Time)\ntimeCat = seqTimeCat\n\ntimeLin : LinRewiring TimeCost\ntimeLin =\n record {\n cat = timeCat;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _\u2294_;\n second = F.id;\n tt\u2192[] = 0;\n []\u2192tt = 0;\n <\u2237> = 0;\n uncons = 0 }\n\ntimeRewiring : Rewiring TimeCost\ntimeRewiring =\n record {\n linRewiring = timeLin;\n tt = 0;\n dup = 0;\n <[]> = 0;\n <_,_> = _\u2294_;\n fst = 0;\n snd = 0;\n rewire = const 0;\n rewireTbl = const 0 }\n\ntimeFork : HasFork TimeCost\ntimeFork = record { cond = 1; fork = 1+_\u2294_ }\n\ntimeOps : FunOps TimeCost\ntimeOps = record { rewiring = timeRewiring; hasFork = timeFork;\n <0b> = 0; <1b> = 0 }\n\n{-\ntimeOps\u2261seqTimeOps : timeOps \u2261 record seqTimeOps {\n rewiring = record seqTimeRewiring {\n linRewiring = record seqTimeLin { <_\u00d7_> = _\u2294_ };\n <_,_> = _\u2294_};\n fork = 1+_\u2294_ }\n {-;\n <\u2237> = 0; uncons = 0 } -- Without <\u2237> = 0... this definition makes\n -- the FunOps record def yellow\n -}\ntimeOps\u2261seqTimeOps = \u2261.refl\n-}\n\n{-\nopen import maxsemiring\nopen \u2294-+-SemiringSolver\ntimeOps' : \u2200 n \u2192 FunOps (constFuns (Polynomial n))\ntimeOps' n = record {\n id = con 0; _\u2218_ = _:*_;\n <0b> = con 0; <1b> = con 0; cond = con 1; fork = 1+_+_; tt = con 0;\n <_,_> = _:*_; fst = con 0; snd = con 0;\n dup = con 0; <_\u00d7_> = _:+_; swap = con 0; assoc = con 0;\n = con 0; snd = con 0;\n <[]> = con 0; <\u2237> = con 0; uncons = con 0 }\n where 1+_+_ : Polynomial n \u2192 Polynomial n \u2192 Polynomial n\n 1+ x + y = con 1 :* (x :* y)\n\nModule TimeOps' where\n Time = \u2115\n open FunOps (timeOps' 1) public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} := con 0\n snoc\u22610 zero = ?\n snoc\u22610 (suc n) = ?\n-}\n\nmodule TimeOps where\n open FunOps timeOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2294\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2261 0\n replicate\u22610 zero = \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2261 0\n constBit\u22610 true = \u2261.refl\n constBit\u22610 false = \u2261.refl\n\n maximum : \u2200 {n} \u2192 Vec \u2115 n \u2192 \u2115\n maximum = V.foldr (const \u2115) (\u03bb {_} x y \u2192 x \u2294 y) 0\n\n constVec\u22a4\u2261maximum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec\u22a4 {n} f xs \u2261 maximum (V.map f xs)\n constVec\u22a4\u2261maximum f [] = \u2261.refl\n constVec\u22a4\u2261maximum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b \u2294 constVec\u22a4 f bs) 0\n | constVec\u22a4\u2261maximum f bs = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2261 0\n constBits\u22610 [] = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) rewrite constBit\u22610 b\n | constBits\u22610 bs = \u2261.refl\n\n fromBitsFun\u2261i : \u2200 {i o} (f : i \u2192\u1d47 o) \u2192 fromBitsFun f \u2261 i\n fromBitsFun\u2261i {zero} f rewrite constBits\u22610 (f []) = \u2261.refl\n fromBitsFun\u2261i {suc i} f rewrite fromBitsFun\u2261i {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261i {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (i \u2294 i) 0\n = \u2261.cong suc (i\u2294i\u2261i i)\n\n {-\nspaceLin : LinRewiring SpaceCost\nspaceLin =\n record {\n id = 0;\n _\u2218_ = _+_;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _+_;\n second = F.id;\n tt\u2192[] = 0;\n []\u2192tt = 0;\n <\u2237> = 0;\n uncons = 0 }\n\nspaceLin\u2261seqTimeLin : spaceLin \u2261 seqTimeLin\nspaceLin\u2261seqTimeLin = \u2261.refl\n\nspaceRewiring : Rewiring TimeCost\nspaceRewiring =\n record {\n linRewiring = spaceLin;\n tt = 0;\n dup = 1;\n <[]> = 0;\n <_,_> = 1+_+_;\n fst = 0;\n snd = 0;\n rewire = \u03bb {_} {o} _ \u2192 o;\n rewireTbl = \u03bb {_} {o} _ \u2192 o }\n\nspaceFork : HasFork SpaceCost\nspaceFork = record { cond = 1; fork = 1+_+_ }\n\nspaceOps : FunOps SpaceCost\nspaceOps = record { rewiring = spaceRewiring; hasFork = spaceFork;\n <0b> = 1; <1b> = 1 }\n\n {-\n-- So far the space cost model is like the sequential time cost model but makes <0b>,<1b>,dup\n-- cost one unit of space.\nspaceOps\u2261seqTimeOps : spaceOps \u2261 record seqTimeOps { <0b> = 1; <1b> = 1; dup = 1; <_,_> = 1+_+_\n ; <\u2237> = 0; uncons = 0 } -- same bug here\nspaceOps\u2261seqTimeOps = \u2261.refl\n-}\n\nmodule SpaceOps where\n open FunOps spaceOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2115\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u2261n : \u2200 n \u2192 replicate {n} \u2261 n\n replicate\u2261n zero = \u2261.refl\n replicate\u2261n (suc n) rewrite replicate\u2261n n = \u2261.refl\n\n constBit\u22611 : \u2200 b \u2192 constBit b \u2261 1\n constBit\u22611 true = \u2261.refl\n constBit\u22611 false = \u2261.refl\n\n constVec\u22a4\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Space) xs \u2192 constVec\u22a4 {n} f xs \u2261 V.sum (V.map f xs)\n constVec\u22a4\u2261sum f [] = \u2261.refl\n constVec\u22a4\u2261sum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b + constVec\u22a4 f bs) 0\n | constVec\u22a4\u2261sum f bs = \u2261.refl\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Space) xs \u2192 constVec {n} f xs \u2261 V.sum (V.map f xs)\n constVec\u2261sum f xs rewrite constVec\u22a4\u2261sum f xs | \u2115\u00b0.+-comm (V.sum (V.map f xs)) 0 = \u2261.refl\n\n constBits\u2261n : \u2200 {n} xs \u2192 constBits {n} xs \u2261 n\n constBits\u2261n [] = \u2261.refl\n constBits\u2261n {suc n} (b \u2237 bs) rewrite constBit\u22611 b\n | constBits\u2261n bs\n | \u2115\u00b0.+-comm n 0 = \u2261.refl\n\n fromBitsFun-cost : \u2200 (i o : \u2115) \u2192 \u2115\n fromBitsFun-cost zero o = o\n fromBitsFun-cost (suc i) o = 1 + 2*(fromBitsFun-cost i o)\n\n fromBitsFun\u2261 : \u2200 {i o} (f : i \u2192\u1d47 o) \u2192 fromBitsFun f \u2261 fromBitsFun-cost i o\n fromBitsFun\u2261 {zero} {o} f rewrite constBits\u2261n (f []) | \u2115\u00b0.+-comm o 0 = \u2261.refl\n fromBitsFun\u2261 {suc i} {o} f rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (2* (fromBitsFun-cost i o)) 0\n = \u2261.refl\n\n{-\ntime\u00d7spaceOps : FunOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-Ops timeOps spaceOps\n\nmodule Time\u00d7SpaceOps = FunOps time\u00d7spaceOps\n-}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"module FunUniverse.Cost where\n\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2*_; 2^_; _^_; _\u2294_; module \u2115\u00b0; module \u2294\u00b0; 2*\u2032_)\nopen import Data.Bool using (true; false)\nimport Data.DifferenceNat\nimport Data.Vec.NP as V\nimport Function as F\nimport Data.Product as \u00d7\nimport Relation.Binary.PropositionalEquality as \u2261\nopen F using (const; _\u2218\u2032_)\nopen V using (Vec; []; _\u2237_; _++_; [_])\nopen \u00d7 using (_\u00d7_)\nopen \u2261 using (_\u2261_; _\u2257_)\n\nopen import Data.Bits using (Bits; 0\u2237_; 1\u2237_; _\u2192\u1d47_)\n\nopen import FunUniverse.Core\nopen import FunUniverse.Const\n\nmodule D where\n open Data.DifferenceNat public renaming (suc to suc#; _+_ to _+#_)\n _*#_ : \u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n zero *# d = 0#\n suc n *# d = (n *# d) +# d\n _*#\u2032_ : \u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n zero *#\u2032 d = 0#\n suc n *#\u2032 d = d +# (n *#\u2032 d)\n 2*#_ : Diff\u2115 \u2192 Diff\u2115\n 2*# n = n +# n\n 2^#_ : \u2115 \u2192 Diff\u2115\n 2^# zero = 1#\n 2^# suc n = 2*# (2^# n)\n 1+_+_D : Diff\u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n 1+ x + y D = 1# \u2218\u2032 (x \u2218\u2032 y)\nopen D using (Diff\u2115)\n\nprivate\n 1+_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n 1+ x + y = 1 + (x + y)\n 1+_\u2294_ : \u2115 \u2192 \u2115 \u2192 \u2115\n 1+ x \u2294 y = 1 + (x \u2294 y)\n i\u2294i\u2261i : \u2200 i \u2192 i \u2294 i \u2261 i\n i\u2294i\u2261i zero = \u2261.refl\n i\u2294i\u2261i (suc i) = \u2261.cong suc (i\u2294i\u2261i i)\n\n{-\nseqTimeOpsD : FunOps (constFuns Diff\u2115)\nseqTimeOpsD = record {\n id = 0#; _\u2218_ = _\u2218\u2032_;\n <0b> = 0#; <1b> = 0#; cond = 1#; fork = 1+_+_D; tt = 0#;\n <_,_> = _\u2218\u2032_; fst = 0#; snd = 0#;\n dup = 0#; first = F.id; swap = 0#; assoc = 0#;\n = 0#; snd = 0#;\n <_\u00d7_> = _\u2218\u2032_; second = F.id;\n <[]> = 0#; <\u2237> = 0#; uncons = 0# }\n where open D\n\nmodule SeqTimeOpsD where\n Time = Diff\u2115\n open D\n open FunOps seqTimeOpsD public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2257 0#\n snoc\u22610 zero x = \u2261.refl\n snoc\u22610 (suc n) x rewrite snoc\u22610 n x = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2257 0#\n reverse\u22610 zero x = \u2261.refl\n reverse\u22610 (suc n) x rewrite reverse\u22610 n x | snoc\u22610 n x = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2257 0#\n replicate\u22610 zero = \u03bb _ \u2192 \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2257 0#\n constBit\u22610 true {-1b-} _ = \u2261.refl\n constBit\u22610 false {-0b-} _ = \u2261.refl\n\n sum\u2032 : \u2200 {n} \u2192 Vec Diff\u2115 n \u2192 Diff\u2115\n sum\u2032 = V.foldr (const Diff\u2115) _\u2218\u2032_ id\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec {n} f xs \u2257 sum\u2032 (V.map f xs)\n constVec\u2261sum f [] x = \u2261.refl\n constVec\u2261sum {suc n} f (b \u2237 bs) x rewrite \u2115\u00b0.+-comm (f b x \u2294 constVec f bs x) 0\n | constVec\u2261sum f bs x = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2257 0#\n constBits\u22610 [] x = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) x rewrite constBits\u22610 bs x\n | constBit\u22610 b x = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2257 m *# n\n foldl\u2261* zero n x = \u2261.refl\n foldl\u2261* (suc m) n x rewrite foldl\u2261* m n (n x) = \u2261.refl\n\n foldr\u2261* : \u2200 m n \u2192 foldr {m} n \u2257 m *#\u2032 n\n foldr\u2261* zero n x = \u2261.refl\n foldr\u2261* (suc m) n x rewrite foldr\u2261* m n x = \u2261.refl\n\n #nodes : \u2200 i \u2192 Diff\u2115\n #nodes zero = 0#\n #nodes (suc i) = 1# +# #nodes i +# #nodes i\n\n fromBitsFun\u2261 : \u2200 {i o} (f : i \u2192\u1d47 o) \u2192 fromBitsFun f \u2257 #nodes i\n fromBitsFun\u2261 {zero} f x = constBits\u22610 (f []) x\n fromBitsFun\u2261 {suc i} f x rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_) x\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_) (#nodes i x) = \u2261.refl\n-}\n\nTime = \u2115\nTimeCost = constFuns Time\nSpace = \u2115\nSpaceCost = constFuns Space\n\nseqTimeLin : LinRewiring TimeCost\nseqTimeLin =\n record {\n id = 0;\n _\u2218_ = _+_;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _+_;\n second = F.id;\n tt\u2192[] = 0;\n []\u2192tt = 0;\n <\u2237> = 0;\n uncons = 0 }\n\nseqTimeRewiring : Rewiring TimeCost\nseqTimeRewiring =\n record {\n linRewiring = seqTimeLin;\n tt = 0;\n dup = 0;\n <[]> = 0;\n <_,_> = _+_;\n fst = 0;\n snd = 0;\n rewire = const 0;\n rewireTbl = const 0 }\n\nseqTimeFork : HasFork TimeCost\nseqTimeFork = record { cond = 1; fork = 1+_+_ }\n\nseqTimeOps : FunOps TimeCost\nseqTimeOps = record { rewiring = seqTimeRewiring; hasFork = seqTimeFork;\n <0b> = 0; <1b> = 0 }\n\nseqTimeBij : Bijective TimeCost\nseqTimeBij = FunOps.bijective seqTimeOps\n\ntimeLin : LinRewiring TimeCost\ntimeLin =\n record {\n id = 0;\n _\u2218_ = _+_;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _\u2294_;\n second = F.id;\n tt\u2192[] = 0;\n []\u2192tt = 0;\n <\u2237> = 0;\n uncons = 0 }\n\ntimeRewiring : Rewiring TimeCost\ntimeRewiring =\n record {\n linRewiring = timeLin;\n tt = 0;\n dup = 0;\n <[]> = 0;\n <_,_> = _\u2294_;\n fst = 0;\n snd = 0;\n rewire = const 0;\n rewireTbl = const 0 }\n\ntimeFork : HasFork TimeCost\ntimeFork = record { cond = 1; fork = 1+_\u2294_ }\n\ntimeOps : FunOps TimeCost\ntimeOps = record { rewiring = timeRewiring; hasFork = timeFork;\n <0b> = 0; <1b> = 0 }\n\n{-\ntimeOps\u2261seqTimeOps : timeOps \u2261 record seqTimeOps {\n rewiring = record seqTimeRewiring {\n linRewiring = record seqTimeLin { <_\u00d7_> = _\u2294_ };\n <_,_> = _\u2294_};\n fork = 1+_\u2294_ }\n {-;\n <\u2237> = 0; uncons = 0 } -- Without <\u2237> = 0... this definition makes\n -- the FunOps record def yellow\n -}\ntimeOps\u2261seqTimeOps = \u2261.refl\n-}\n\n{-\nopen import maxsemiring\nopen \u2294-+-SemiringSolver\ntimeOps' : \u2200 n \u2192 FunOps (constFuns (Polynomial n))\ntimeOps' n = record {\n id = con 0; _\u2218_ = _:*_;\n <0b> = con 0; <1b> = con 0; cond = con 1; fork = 1+_+_; tt = con 0;\n <_,_> = _:*_; fst = con 0; snd = con 0;\n dup = con 0; <_\u00d7_> = _:+_; swap = con 0; assoc = con 0;\n = con 0; snd = con 0;\n <[]> = con 0; <\u2237> = con 0; uncons = con 0 }\n where 1+_+_ : Polynomial n \u2192 Polynomial n \u2192 Polynomial n\n 1+ x + y = con 1 :* (x :* y)\n\nModule TimeOps' where\n Time = \u2115\n open FunOps (timeOps' 1) public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} := con 0\n snoc\u22610 zero = ?\n snoc\u22610 (suc n) = ?\n-}\n\nmodule TimeOps where\n open FunOps timeOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2294\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2261 0\n replicate\u22610 zero = \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2261 0\n constBit\u22610 true = \u2261.refl\n constBit\u22610 false = \u2261.refl\n\n maximum : \u2200 {n} \u2192 Vec \u2115 n \u2192 \u2115\n maximum = V.foldr (const \u2115) (\u03bb {_} x y \u2192 x \u2294 y) 0\n\n constVec\u22a4\u2261maximum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec\u22a4 {n} f xs \u2261 maximum (V.map f xs)\n constVec\u22a4\u2261maximum f [] = \u2261.refl\n constVec\u22a4\u2261maximum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b \u2294 constVec\u22a4 f bs) 0\n | constVec\u22a4\u2261maximum f bs = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2261 0\n constBits\u22610 [] = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) rewrite constBit\u22610 b\n | constBits\u22610 bs = \u2261.refl\n\n fromBitsFun\u2261i : \u2200 {i o} (f : i \u2192\u1d47 o) \u2192 fromBitsFun f \u2261 i\n fromBitsFun\u2261i {zero} f rewrite constBits\u22610 (f []) = \u2261.refl\n fromBitsFun\u2261i {suc i} f rewrite fromBitsFun\u2261i {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261i {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (i \u2294 i) 0\n = \u2261.cong suc (i\u2294i\u2261i i)\n\nspaceLin : LinRewiring SpaceCost\nspaceLin =\n record {\n id = 0;\n _\u2218_ = _+_;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _+_;\n second = F.id;\n tt\u2192[] = 0;\n []\u2192tt = 0;\n <\u2237> = 0;\n uncons = 0 }\n\nspaceLin\u2261seqTimeLin : spaceLin \u2261 seqTimeLin\nspaceLin\u2261seqTimeLin = \u2261.refl\n\nspaceRewiring : Rewiring TimeCost\nspaceRewiring =\n record {\n linRewiring = spaceLin;\n tt = 0;\n dup = 1;\n <[]> = 0;\n <_,_> = 1+_+_;\n fst = 0;\n snd = 0;\n rewire = \u03bb {_} {o} _ \u2192 o;\n rewireTbl = \u03bb {_} {o} _ \u2192 o }\n\nspaceFork : HasFork SpaceCost\nspaceFork = record { cond = 1; fork = 1+_+_ }\n\nspaceOps : FunOps SpaceCost\nspaceOps = record { rewiring = spaceRewiring; hasFork = spaceFork;\n <0b> = 1; <1b> = 1 }\n\n {-\n-- So far the space cost model is like the sequential time cost model but makes <0b>,<1b>,dup\n-- cost one unit of space.\nspaceOps\u2261seqTimeOps : spaceOps \u2261 record seqTimeOps { <0b> = 1; <1b> = 1; dup = 1; <_,_> = 1+_+_\n ; <\u2237> = 0; uncons = 0 } -- same bug here\nspaceOps\u2261seqTimeOps = \u2261.refl\n-}\n\nmodule SpaceOps where\n open FunOps spaceOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2115\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u2261n : \u2200 n \u2192 replicate {n} \u2261 n\n replicate\u2261n zero = \u2261.refl\n replicate\u2261n (suc n) rewrite replicate\u2261n n = \u2261.refl\n\n constBit\u22611 : \u2200 b \u2192 constBit b \u2261 1\n constBit\u22611 true = \u2261.refl\n constBit\u22611 false = \u2261.refl\n\n constVec\u22a4\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Space) xs \u2192 constVec\u22a4 {n} f xs \u2261 V.sum (V.map f xs)\n constVec\u22a4\u2261sum f [] = \u2261.refl\n constVec\u22a4\u2261sum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b + constVec\u22a4 f bs) 0\n | constVec\u22a4\u2261sum f bs = \u2261.refl\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Space) xs \u2192 constVec {n} f xs \u2261 V.sum (V.map f xs)\n constVec\u2261sum f xs rewrite constVec\u22a4\u2261sum f xs | \u2115\u00b0.+-comm (V.sum (V.map f xs)) 0 = \u2261.refl\n\n constBits\u2261n : \u2200 {n} xs \u2192 constBits {n} xs \u2261 n\n constBits\u2261n [] = \u2261.refl\n constBits\u2261n {suc n} (b \u2237 bs) rewrite constBit\u22611 b\n | constBits\u2261n bs\n | \u2115\u00b0.+-comm n 0 = \u2261.refl\n\n fromBitsFun-cost : \u2200 (i o : \u2115) \u2192 \u2115\n fromBitsFun-cost zero o = o\n fromBitsFun-cost (suc i) o = 1 + 2*(fromBitsFun-cost i o)\n\n fromBitsFun\u2261 : \u2200 {i o} (f : i \u2192\u1d47 o) \u2192 fromBitsFun f \u2261 fromBitsFun-cost i o\n fromBitsFun\u2261 {zero} {o} f rewrite constBits\u2261n (f []) | \u2115\u00b0.+-comm o 0 = \u2261.refl\n fromBitsFun\u2261 {suc i} {o} f rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (2* (fromBitsFun-cost i o)) 0\n = \u2261.refl\n\n{-\ntime\u00d7spaceOps : FunOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-Ops timeOps spaceOps\n\nmodule Time\u00d7SpaceOps = FunOps time\u00d7spaceOps\n-}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"a0c0e331a5839a6f7cd2921fe50b2acfdf977244","subject":"Resolve implicit arguments.","message":"Resolve implicit arguments.\n\nWhen I filled the last hole in total.agda in the previous commit,\nAgda told me to 'resolve implicit argument', so I added explicit\nimplicit arguments until Agda was satisfied. I don't know whether\nor why it matters to do this.\n\nOld-commit-hash: ed138bd06659ab2caac47a670b7d3d60c4d97f3a\n","repos":"inc-lc\/ilc-agda","old_file":"total.agda","new_file":"total.agda","new_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Note that \u0394 is *not* the same as the \u2202 operator in\n-- definition\/intro.tex. See discussion at:\n--\n-- https:\/\/github.com\/ps-mr\/ilc\/pull\/34#discussion_r4290325\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Changes\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\nopen import Denotational.ValidChanges\n\nopen import Changes\nopen import ChangeContexts\nopen import ChangeContextLifting\nopen import PropsDelta\nopen import SymbolicDerivation\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 (t : Term \u0393\u2081 \u03c4) \u2192\n \u0394 {{\u0393\u2032}} t \u2248 derive-term {{\u0393\u2032}} t\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) =\n begin\n \u0394 {{\u0393\u2032}} (abs t)\n \u2248\u27e8 \u0394-abs {{\u0393\u2032}} t \u27e9\n abs (abs (\u0394 {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t)) \u27e9\n abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t))\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (abs t)\n \u220e where\n open \u2248-Reasoning\n \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (app t\u2081 t\u2082) =\n begin\n \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app {{\u0393\u2032}} t\u2081 t\u2082 \u27e9\n app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct {{\u0393\u2032}} t\u2081) \u2248-refl) (derive-term-correct {{\u0393\u2032}} t\u2082) \u27e9\n app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (var x) = ext-t (\u03bb \u03c1 \u2192\n begin\n \u27e6 \u0394 {{\u0393\u2032}} (var x) \u27e7 \u03c1\n \u2261\u27e8\u27e9\n diff\n (\u27e6 x \u27e7 (update (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n (\u27e6 x \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n \u2261\u27e8 derive-var-correct {\u0393\u2081} (\u27e6 \u0393\u2032 \u27e7 \u03c1) x \u27e9\n \u27e6 derive-var x \u27e7Var (\u27e6 \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 sym (lift-sound \u0393\u2032 (derive-var x) \u03c1) \u27e9\n \u27e6 lift \u0393\u2032 (derive-var x) \u27e7Var \u03c1\n \u220e) where open \u2261-Reasoning\nderive-term-correct true = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct {{\u0393\u2032}} (if t\u2081 t\u2082 t\u2083) =\n begin\n \u0394 (if t\u2081 t\u2082 t\u2083)\n \u2248\u27e8 \u0394-if {{\u0393\u2032}} t\u2081 t\u2082 t\u2083 \u27e9\n if (\u0394 t\u2081)\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (diff-term (apply-term (\u0394 {{\u0393\u2032}} t\u2083) (lift-term {{\u0393\u2032}} t\u2083)) (lift-term {{\u0393\u2032}} t\u2082))\n (diff-term (apply-term (\u0394 {{\u0393\u2032}} t\u2082) (lift-term {{\u0393\u2032}} t\u2082)) (lift-term {{\u0393\u2032}} t\u2083)))\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (\u0394 {{\u0393\u2032}} t\u2082)\n (\u0394 {{\u0393\u2032}} t\u2083))\n \u2248\u27e8 \u2248-if (derive-term-correct {{\u0393\u2032}} t\u2081)\n (\u2248-if (\u2248-refl)\n (\u2248-diff-term (\u2248-apply-term (derive-term-correct {{\u0393\u2032}} t\u2083) \u2248-refl) \u2248-refl)\n (\u2248-diff-term (\u2248-apply-term (derive-term-correct {{\u0393\u2032}} t\u2082) \u2248-refl) \u2248-refl))\n (\u2248-if (\u2248-refl)\n (derive-term-correct {{\u0393\u2032}} t\u2082)\n (derive-term-correct {{\u0393\u2032}} t\u2083)) \u27e9\n if (derive-term {{\u0393\u2032}} t\u2081)\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (diff-term (apply-term (derive-term {{\u0393\u2032}} t\u2083) (lift-term {{\u0393\u2032}} t\u2083)) (lift-term {{\u0393\u2032}} t\u2082))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} t\u2082) (lift-term {{\u0393\u2032}} t\u2082)) (lift-term {{\u0393\u2032}} t\u2083)))\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (derive-term {{\u0393\u2032}} t\u2082)\n (derive-term {{\u0393\u2032}} t\u2083))\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (if t\u2081 t\u2082 t\u2083)\n \u220e where open \u2248-Reasoning\n\nderive-term-correct {{\u0393\u2032}} (\u0394 {{\u0393\u2033}} t) = \u2248-\u0394 {{\u0393\u2032}} (derive-term-correct {{\u0393\u2033}} t)\n","old_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Note that \u0394 is *not* the same as the \u2202 operator in\n-- definition\/intro.tex. See discussion at:\n--\n-- https:\/\/github.com\/ps-mr\/ilc\/pull\/34#discussion_r4290325\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Changes\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\nopen import Denotational.ValidChanges\n\nopen import Changes\nopen import ChangeContexts\nopen import ChangeContextLifting\nopen import PropsDelta\nopen import SymbolicDerivation\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 (t : Term \u0393\u2081 \u03c4) \u2192\n \u0394 {{\u0393\u2032}} t \u2248 derive-term {{\u0393\u2032}} t\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 {\u03c4 \u2022 \u0393\u2081} t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct {\u03c4 \u2022 \u0393\u2081} t)) \u27e9\n abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} t))\n \u2261\u27e8\u27e9\n derive-term (abs t)\n \u220e where\n open \u2248-Reasoning\n \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct {{\u0393\u2032}} t\u2081) \u2248-refl) (derive-term-correct {{\u0393\u2032}} t\u2082) \u27e9\n app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (var x) = ext-t (\u03bb \u03c1 \u2192\n begin\n \u27e6 \u0394 {{\u0393\u2032}} (var x) \u27e7 \u03c1\n \u2261\u27e8\u27e9\n diff\n (\u27e6 x \u27e7 (update (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n (\u27e6 x \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n \u2261\u27e8 derive-var-correct {\u0393\u2081} (\u27e6 \u0393\u2032 \u27e7 \u03c1) x \u27e9\n \u27e6 derive-var x \u27e7Var (\u27e6 \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 sym (lift-sound \u0393\u2032 (derive-var x) \u03c1) \u27e9\n \u27e6 lift \u0393\u2032 (derive-var x) \u27e7Var \u03c1\n \u220e) where open \u2261-Reasoning\nderive-term-correct true = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct {{\u0393\u2032}} (if t\u2081 t\u2082 t\u2083) =\n begin\n \u0394 (if t\u2081 t\u2082 t\u2083)\n \u2248\u27e8 \u0394-if {{\u0393\u2032}} t\u2081 t\u2082 t\u2083 \u27e9\n if (\u0394 t\u2081)\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (diff-term (apply-term (\u0394 t\u2083) (lift-term {{\u0393\u2032}} t\u2083)) (lift-term {{\u0393\u2032}} t\u2082))\n (diff-term (apply-term (\u0394 t\u2082) (lift-term {{\u0393\u2032}} t\u2082)) (lift-term {{\u0393\u2032}} t\u2083)))\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (\u0394 t\u2082)\n (\u0394 t\u2083))\n \u2248\u27e8 \u2248-if (derive-term-correct t\u2081)\n (\u2248-if (\u2248-refl)\n (\u2248-diff-term (\u2248-apply-term (derive-term-correct t\u2083) \u2248-refl) \u2248-refl)\n (\u2248-diff-term (\u2248-apply-term (derive-term-correct t\u2082) \u2248-refl) \u2248-refl))\n (\u2248-if (\u2248-refl)\n (derive-term-correct t\u2082)\n (derive-term-correct t\u2083)) \u27e9\n if (derive-term t\u2081)\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (diff-term (apply-term (derive-term t\u2083) (lift-term {{\u0393\u2032}} t\u2083)) (lift-term {{\u0393\u2032}} t\u2082))\n (diff-term (apply-term (derive-term t\u2082) (lift-term {{\u0393\u2032}} t\u2082)) (lift-term {{\u0393\u2032}} t\u2083)))\n (if (lift-term {{\u0393\u2032}} t\u2081)\n (derive-term t\u2082)\n (derive-term t\u2083))\n \u2261\u27e8\u27e9\n derive-term (if t\u2081 t\u2082 t\u2083)\n \u220e where open \u2248-Reasoning\n\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"37dbe4e367fef7b52ae7dff6fe572cc48ada1378","subject":"Define step-indexed logical relation we want (almost)","message":"Define step-indexed logical relation we want (almost)\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/BigStepSILR2.agda","new_file":"Thesis\/BigStepSILR2.agda","new_contents":"module Thesis.BigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen import Data.Nat.Properties.Simple\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\nopen import Thesis.FunBigStepSILR2\n\n-- Standard relational big-step semantics, with step-indexes matching a small-step semantics.\n-- Protip: doing this on ANF terms would be much easier.\ndata _\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) : \u2200 {\u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u2115 \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n \u03c1 \u22a2 abs t \u2193[ 0 ] closure t \u03c1\n app : \u2200 n1 n2 n3 {\u0393\u2032 \u03c4\u2081 \u03c4\u2082 \u03c1\u2032 v\u2082 v\u2032} {t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2082 : Term \u0393 \u03c4\u2081} {t\u2032 : Term (\u03c4\u2081 \u2022 \u0393\u2032) \u03c4\u2082} \u2192\n \u03c1 \u22a2 t\u2081 \u2193[ n1 ] closure t\u2032 \u03c1\u2032 \u2192\n \u03c1 \u22a2 t\u2082 \u2193[ n2 ] v\u2082 \u2192\n (v\u2082 \u2022 \u03c1\u2032) \u22a2 t\u2032 \u2193[ n3 ] v\u2032 \u2192\n \u03c1 \u22a2 app t\u2081 t\u2082 \u2193[ suc n1 + n2 + n3 ] v\u2032\n var : \u2200 {\u03c4} (x : Var \u0393 \u03c4) \u2192\n \u03c1 \u22a2 var x \u2193[ 0 ] (\u27e6 x \u27e7Var \u03c1)\n lit : \u2200 n \u2192\n \u03c1 \u22a2 const (lit n) \u2193[ 0 ] intV n\n\n-- Silly lemmas for eval-dec-sound\nmodule _ where\n Done-inj : \u2200 {\u03c4} m n {v1 v2 : Val \u03c4} \u2192 Done v1 m \u2261 Done v2 n \u2192 m \u2261 n\n Done-inj _ _ refl = refl\n\n lem1 : \u2200 n d \u2192 d \u2262 suc (n + d)\n lem1 n d eq rewrite +-comm n d = m\u22621+m+n d eq\n\n subd : \u2200 n d \u2192 n + d \u2261 d \u2192 n \u2261 0\n subd zero d eq = refl\n subd (suc n) d eq = \u22a5-elim (lem1 n d (sym eq))\n\n comp\u2238 : \u2200 a b \u2192 b \u2264 a \u2192 suc a \u2238 b \u2261 suc (a \u2238 b)\n comp\u2238 a zero le = refl\n comp\u2238 zero (suc b) ()\n comp\u2238 (suc a) (suc b) (s\u2264s le) = comp\u2238 a b le\n\n rearr\u2238 : \u2200 a b c \u2192 c \u2264 b \u2192 a + (b \u2238 c) \u2261 (a + b) \u2238 c\n rearr\u2238 a b zero c\u2264b = refl\n rearr\u2238 a zero (suc c) ()\n rearr\u2238 a (suc b) (suc c) (s\u2264s c\u2264b) rewrite +-suc a b = rearr\u2238 a b c c\u2264b\n\n cancel\u2238 : \u2200 a b \u2192 b \u2264 a \u2192 a \u2238 b + b \u2261 a\n cancel\u2238 a zero b\u2264a = +-right-identity a\n cancel\u2238 zero (suc b) ()\n cancel\u2238 (suc a) (suc b) (s\u2264s b\u2264a) rewrite +-suc (a \u2238 b) b = cong suc (cancel\u2238 a b b\u2264a)\n\n lemR : \u2200 {\u03c4} n m {v1 v2 : Val \u03c4} \u2192 Done v1 m \u2261 Done v2 n \u2192 m \u2261 n\n lemR n .n refl = refl\n\n lem2 : \u2200 n n3 r \u2192 n3 \u2264 n \u2192 n + n3 \u2261 suc r \u2192 \u2203 \u03bb s \u2192 (n \u2261 suc s)\n lem2 zero .0 r z\u2264n ()\n lem2 (suc n) n3 .(n + n3) le refl = n , refl\n\n{-# TERMINATING #-}\neval-dec-sound : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v m n \u2192 eval t \u03c1 m \u2261 Done v n \u2192 \u03c1 \u22a2 t \u2193[ m \u2238 n ] v\neval-dec-sound (const (lit x)) \u03c1 (intV v) m n eq with lemR n m eq\neval-dec-sound (const (lit x)) \u03c1 (intV .x) m .m refl | refl rewrite n\u2238n\u22610 m = lit x\neval-dec-sound (var x) \u03c1 v m n eq with lemR n m eq\neval-dec-sound (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) m .m refl | refl rewrite n\u2238n\u22610 m = var x\neval-dec-sound (abs t) \u03c1 v m n eq with lemR n m eq\neval-dec-sound (abs t) \u03c1 .(closure t \u03c1) m .m refl | refl rewrite n\u2238n\u22610 m = abs\neval-dec-sound (app s t) \u03c1 v zero n ()\neval-dec-sound (app s t) \u03c1 v (suc r) n eq with eval s \u03c1 r | inspect (eval s \u03c1) r\neval-dec-sound (app s t) \u03c1 v (suc r) n eq | (Done sv n1) | [ seq ] with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\neval-dec-sound (app s t) \u03c1 v (suc r) n eq | (Done (closure st s\u03c1) n1) | [ seq ] | (Done tv n2) | [ teq ] with eval st (tv \u2022 s\u03c1) n2 | inspect (eval st (tv \u2022 s\u03c1)) n2\neval-dec-sound (app s t) \u03c1 .v (suc r) .n3 refl | (Done sv@(closure st s\u03c1) n1) | [ seq ] | (Done tv n2) | [ teq ] | (Done v n3) | [ veq ] = body\n where\n n1\u2264r : n1 \u2264 r\n n1\u2264r = eval-dec s \u03c1 sv r n1 seq\n n2\u2264n1 : n2 \u2264 n1\n n2\u2264n1 = eval-dec t \u03c1 tv n1 n2 teq\n n3\u2264n2 : n3 \u2264 n2\n n3\u2264n2 = eval-dec st (tv \u2022 s\u03c1) v n2 n3 veq\n eval1 = eval-dec-sound s \u03c1 sv r n1 seq\n eval2 = eval-dec-sound t \u03c1 tv n1 n2 teq\n eval3 = eval-dec-sound st (tv \u2022 s\u03c1) v n2 n3 veq\n l1 : suc r \u2238 n3 \u2261 suc (r \u2238 n3)\n l1 = comp\u2238 r n3 (\u2264-trans n3\u2264n2 (\u2264-trans n2\u2264n1 n1\u2264r))\n l2 : suc r \u2238 n3 \u2261 suc (((r \u2238 n1) + (n1 \u2238 n2)) + (n2 \u2238 n3))\n l2\n rewrite rearr\u2238 (r \u2238 n1) n1 n2 n2\u2264n1 | cancel\u2238 r n1 n1\u2264r\n | rearr\u2238 (r \u2238 n2) n2 n3 n3\u2264n2 | cancel\u2238 r n2 (\u2264-trans n2\u2264n1 n1\u2264r) = l1\n foo : \u03c1 \u22a2 app s t \u2193[ suc (r \u2238 n1 + (n1 \u2238 n2) + (n2 \u2238 n3)) ] v\n foo = app (r \u2238 n1) (n1 \u2238 n2) (n2 \u2238 n3) {t\u2081 = s} {t\u2082 = t} {t\u2032 = st} eval1 eval2 eval3\n body : \u03c1 \u22a2 app s t \u2193[ suc r \u2238 n3 ] v\n body rewrite l2 = foo\neval-dec-sound (app s t) \u03c1 v (suc r) n () | (Done (closure st s\u03c1) n1) | [ seq ] | (Done tv n2) | [ teq ] | Error | [ veq ]\neval-dec-sound (app s t) \u03c1 v (suc r) n () | (Done (closure st s\u03c1) n1) | [ seq ] | (Done tv n2) | [ teq ] | TimeOut | [ veq ]\neval-dec-sound (app s t) \u03c1 v (suc r) n () | (Done sv n1) | [ seq ] | Error | [ teq ]\neval-dec-sound (app s t) \u03c1 v (suc r) n () | (Done sv n1) | [ seq ] | TimeOut | [ teq ]\neval-dec-sound (app s t) \u03c1 v (suc r) n () | Error | [ seq ]\neval-dec-sound (app s t) \u03c1 v (suc r) n () | TimeOut | [ seq ]\n-- \u21a6-sound : \u2200 {\u0393 \u03c4} \u03c1 (x : Var \u0393 \u03c4) \u2192\n-- ((Den.\u27e6 x \u27e7Var) (\u27e6 \u03c1 \u27e7Context)) \u2261 (\u27e6 (\u27e6 x \u27e7Var) \u03c1 \u27e7Val)\n-- \u21a6-sound (px \u2022 \u03c1) this = refl\n-- \u21a6-sound (px \u2022 \u03c1) (that x) = \u21a6-sound \u03c1 x\n\n-- \u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n-- \u03c1 \u22a2 t \u2193 v \u2192\n-- \u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 v \u27e7Val\n-- \u2193-sound abs = refl\n-- \u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) rewrite \u2193-sound \u2193\u2081 | \u2193-sound \u2193\u2082 | \u2193-sound \u2193\u2032 = refl\n-- \u2193-sound (var x) = \u21a6-sound _ x\n-- \u2193-sound (lit n) = refl\n\nimport Data.Integer as I\nopen I using (\u2124)\n\nmutual\n rrelT3 : \u2200 {\u03c4 \u0393 \u0394\u0393} (t1 : Term \u0393 \u03c4) (dt : Term \u0394\u0393 (\u0394\u03c4 \u03c4)) (t2 : Term \u0393 \u03c4) (\u03c11 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : \u27e6 \u0394\u0393 \u27e7Context) (\u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\n rrelT3 {\u03c4} t1 dt t2 \u03c11 d\u03c1 \u03c12 k =\n (v1 v2 : Val \u03c4) \u2192\n \u2200 j n2 (j {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n\npattern com' q M P = com (mk q M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\nopen ProtoRel _\u2261_ public renaming (_\u2248\u1d3e_ to _\u2261\u1d3e_)\n\n{-\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n -}\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n Trace : Proto \u2192 Proto\n Trace end = end\n Trace (com c) = com (Trace\u1d9c c)\n\n Trace\u1d9c : Com \u2192 Com\n Trace\u1d9c c = \u03a3\u1d9c (Com.M c) \u03bb m \u2192 Trace (Com.P c m)\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c c = mk (dual\u1d35\u1d3c (Com.io c)) (Com.M c) \u03bb m \u2192 dual (Com.P c m)\n\ndata IsTrace : Proto \u2192 \u2605\u2081 where\n end : IsTrace end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsTrace (P m)) \u2192 IsTrace (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\nmutual\n data Dual\u1d9c : Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a0\u1d9c M P) (\u03a3\u1d9c M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a3\u1d9c M P) (\u03a0\u1d9c M Q)\n\n data Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n {-\n \u03a0\u2610\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P [ x ]) (Q x)) \u2192 Dual (\u03a0\u1d3e (\u2610 M) P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0\u2610 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q [ x ])) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e (\u2610 M) Q)\n -}\n\n {-\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Trace : Choreo I \u2192 Proto\n \u2102Trace \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Trace-IsTrace : \u2200 \u2102 \u2192 IsTrace (\u2102Trace \u2102)\n \u2102Trace-IsTrace (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n-}\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com refl M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com refl M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\ndual\u1d35\u1d3c-involutive : \u2200 io \u2192 dual\u1d35\u1d3c (dual\u1d35\u1d3c io) \u2261 io\ndual\u1d35\u1d3c-involutive In = refl\ndual\u1d35\u1d3c-involutive Out = refl\n\ndual-involutive : \u2200 P \u2192 dual (dual P) \u2261\u1d3e P\ndual-involutive end = end\ndual-involutive (com' q M P) = com (dual\u1d35\u1d3c-involutive q) M (\u03bb m \u2192 dual-involutive (P m))\n\nTrace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261\u1d3e Trace P\nTrace-idempotent end = end\nTrace-idempotent (com' _ M P) = com refl M \u03bb m \u2192 Trace-idempotent (P m)\n\nTrace-dual-oblivious : \u2200 P \u2192 Trace (dual P) \u2261\u1d3e Trace P\nTrace-dual-oblivious end = end\nTrace-dual-oblivious (com' _ M P) = com refl M \u03bb m \u2192 Trace-dual-oblivious (P m)\n\nSink : Proto \u2192 Proto\nSink = dual \u2218 Trace\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n{-\n++Tele : \u2200 (P : Proto){Q : Tele P \u2192 Proto} (xs : Tele P) \u2192 Tele (Q xs) \u2192 Tele (P >>= Q)\n++Tele end _ ys = ys\n++Tele (com' q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com q M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com q M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n{-\nDual-sym (\u03a0\u2610\u00b7\u03a3 f) = {!\u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))!}\nDual-sym (\u03a3\u00b7\u03a0\u2610 f) = {!\u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))!}\n-}\n-}\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele (dual P) \u2261 Tele P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n El->>= : (P : Proto){Q : Tele P \u2192 Proto}{X : Tele (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Tele P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n-}\n\n>>=-com : (P : Proto){Q : Tele P \u2192 Proto}{R : Tele P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Tele P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Tele P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : \u2605_ a} (l r : A) \u2192 LR \u2192 A \n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a0\u1d3e LR [L: l R: r ]\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP >>\u1d9c S = record P { P = \u03bb m \u2192 S (Com.P P m) }\n\nmodule Equivalences\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n record Equiv {A B : \u2605}(f : A \u2192 B) : \u2605 where\n field\n linv : B \u2192 A\n is-linv : \u2200 x \u2192 linv (f x) \u2261 x\n rinv : B \u2192 A\n is-rinv : \u2200 x \u2192 f (rinv x) \u2261 x\n\n id\u1d31 : \u2200 {A} \u2192 Equiv {A} id\n id\u1d31 = record\n { linv = id\n ; is-linv = \u03bb _ \u2192 refl\n ; rinv = id\n ; is-rinv = \u03bb _ \u2192 refl\n }\n\n module _ {A B C}{g : B \u2192 C}{f : A \u2192 B} where\n _\u2218\u1d31_ : Equiv g \u2192 Equiv f \u2192 Equiv (g \u2218 f)\n g\u1d31 \u2218\u1d31 f\u1d31 = record { linv = F.linv \u2218 G.linv ; is-linv = \u03bb x \u2192 cong F.linv (G.is-linv (f x)) \u2219 F.is-linv x\n ; rinv = F.rinv \u2218 G.rinv ; is-rinv = \u03bb x \u2192 cong g (F.is-rinv _) \u2219 G.is-rinv x }\n where\n module G = Equiv g\u1d31\n module F = Equiv f\u1d31\n\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = \u03a3 (A \u2192 B) Equiv\n\n module _ {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b} where\n \u03a3-ext : \u2200 {x y : \u03a3 A B} \u2192 (p : proj\u2081 x \u2261 proj\u2081 y) \u2192 subst B p (proj\u2082 x) \u2261 proj\u2082 y \u2192 x \u2261 y\n \u03a3-ext refl = cong (_,_ _)\n\ndata ViewProc : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200 M(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewProc (\u03a3\u1d3e M P) (m , p)\n recv : \u2200 M(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewProc (\u03a0\u1d3e M P) p\n end : ViewProc end _\n\nview-proc : \u2200 P (p : \u27e6 P \u27e7) \u2192 ViewProc P p\nview-proc end _ = end\nview-proc (\u03a0\u1d3e M P) p = recv _ _ p\nview-proc (\u03a3\u1d3e M P) (m , p) = send _ _ m p\n\ndata ViewCom : \u2200 P \u2192 \u27e6 com P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200{M}(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewCom (\u03a3\u1d9c M P) (m , p)\n recv : \u2200{M}(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewCom (\u03a0\u1d9c M P) p\n\nview-com : \u2200 P (p : \u27e6 com P \u27e7) \u2192 ViewCom P p\nview-com (\u03a0\u1d9c M P) p = recv _ p\nview-com (\u03a3\u1d9c M P) (m , p) = send _ m p\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open Equivalences funExt\n\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n \u03a0\u1d3e M P \u214b\u1d3e Q = \u03a0\u1d3e M \u03bb m \u2192 P m \u214b\u1d3e Q\n P \u214b\u1d3e end = P\n P \u214b\u1d3e \u03a0\u1d3e M Q = \u03a0\u1d3e M \u03bb m \u2192 P \u214b\u1d3e Q m\n \u03a3\u1d3e M P \u214b\u1d3e \u03a3\u1d3e M' Q = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 P m \u214b\u1d3e \u03a3\u1d3e M' Q)\n ,inr: (\u03bb m' \u2192 \u03a3\u1d3e M P \u214b\u1d3e Q m') ]\n\n data View-\u214b : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n sendL' : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto)(m : M )(p : \u27e6 P m \u214b\u1d3e \u03a3\u1d3e M' Q \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p)\n sendR' : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto)(m' : M')(p : \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q m' \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m' , p)\n recvL' : \u2200 {M} (P : M \u2192 Proto) Q (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7)) \u2192 View-\u214b (\u03a0\u1d3e M P) Q p\n recvR' : \u2200 {M M'} (P : M \u2192 Proto) (Q : M' \u2192 Proto)(p : (m' : M') \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q m' \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p\n endL : \u2200 Q (p : \u27e6 Q \u27e7) \u2192 View-\u214b end Q p\n endR : \u2200 {M} (P : M \u2192 Proto) (p : \u27e6 \u03a3\u1d3e M P \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) end p\n\n view-\u214b : \u2200 P Q (p : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 View-\u214b P Q p\n view-\u214b end Q p = endL Q p\n view-\u214b (\u03a0\u1d3e M P) Q p = recvL' P Q p\n view-\u214b (\u03a3\u1d3e M P) end p = endR P p\n view-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = recvR' P Q p\n view-\u214b (com (mk Out M P)) (com (mk Out M' Q)) (inl x , p) = sendL' P Q x p\n view-\u214b (com (mk Out M P)) (com (mk Out M' Q)) (inr y , p) = sendR' P Q y p\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7 \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7\n \u214b\u1d3e-assoc end Q R s = s\n \u214b\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = \u214b\u1d3e-assoc (P m) _ _ (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , \u214b\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end p = p\n \u214b\u1d3e-rend (\u03a0\u1d3e _ P) p = \u03bb m \u2192 \u214b\u1d3e-rend (P m) (p m)\n \u214b\u1d3e-rend (\u03a3\u1d3e _ _) p = p\n\n \u214b\u1d3e-rend' : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u214b\u1d3e end \u27e7\n \u214b\u1d3e-rend' end p = p\n \u214b\u1d3e-rend' (\u03a0\u1d3e _ P) p = \u03bb m \u2192 \u214b\u1d3e-rend' (P m) (p m)\n \u214b\u1d3e-rend' (\u03a3\u1d3e _ _) p = p\n\n {-\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n -}\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (\u03a3\u1d3e M P) m p = inr m , p\n \u214b\u1d3e-sendR (\u03a0\u1d3e M P) m p = \u03bb x \u2192 \u214b\u1d3e-sendR (P x) m (p x)\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL (\u03a0\u1d3e M Q) m p = \u03bb m' \u2192 \u214b\u1d3e-sendL (Q m') m {!p!}\n \u214b\u1d3e-sendL (\u03a3\u1d3e M Q) m p = {!!} , p\n\n {-\n \u214b\u1d3e-sendR : \u2200 {M} P Q \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e M Q \u27e7 \u2192 (m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7\n \u214b\u1d3e-sendR P Q s m = {!!}\n -}\n\n \u214b\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e M Q \u27e7\n \u214b\u1d3e-recvR end Q s = s\n \u214b\u1d3e-recvR (\u03a0\u1d3e M P) Q s = \u03bb x \u2192 \u214b\u1d3e-recvR (P x) Q (\u03bb m \u2192 s m x)\n \u214b\u1d3e-recvR (\u03a3\u1d3e M P) Q s = s\n\n mutual\n \u214b\u1d3e-! : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-! P Q p = {!!}\n\n \u214b\u1d3e-!-view : \u2200 {P Q} {pq : \u27e6 P \u214b\u1d3e Q \u27e7} \u2192 View-\u214b P Q pq \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-!-view (sendL' P Q m p) = \u214b\u1d3e-sendR (\u03a3\u1d3e _ Q) m (\u214b\u1d3e-! (P m) (com (mk Out _ Q)) p)\n \u214b\u1d3e-!-view (sendR' P Q m' p) = {!\u214b\u1d3e-sendL (\u03a3\u1d3e _ P) m'!}\n \u214b\u1d3e-!-view (recvL' P Q pq) = {!!}\n \u214b\u1d3e-!-view (recvR' P Q pq) = {!!}\n \u214b\u1d3e-!-view (endL Q pq) = {!!}\n \u214b\u1d3e-!-view (endR P pq) = {!!}\n\n {-\n end Q p = \u214b\u1d3e-rend' Q p\n {-\n \u214b\u1d3e-! end Q p = \u214b\u1d3e-rend' Q p\n \u214b\u1d3e-! (\u03a0\u1d3e M P) end p x = \u214b\u1d3e-! (P x) end (p x)\n \u214b\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 {!\u214b\u1d3e-! (\u03a0 !}\n \u214b\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 \u214b\u1d3e-! (P m) (com (mk Out M' Q)) (p m)\n \u214b\u1d3e-! (\u03a3\u1d3e M P) end p = p\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 \u214b\u1d3e-! (com (mk Out M P)) (Q m') (p m')\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (\u214b\u1d3e-! (P m) (com (mk Out M' Q)) p)\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (\u214b\u1d3e-! (com (mk Out M P)) (Q m) p)\n\n {-\n \u214b\u1d3e-apply : \u2200 P Q \u2192 \u27e6 \u214b\u1d3e P Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply end Q s p = s\n \u214b\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = \u214b\u1d3e-apply (P m) Q (s m) p\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = \u214b\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = \u214b\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , \u214b\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n -}\n\n {-\nmodule V4\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n P \u214b\u1d3e end = P\n com P \u214b\u1d3e com Q = \u03a3\u1d3e LR (P \u214b\u1d9c Q)\n\n _\u214b\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 \u214b\u1d9c P\u1d3f) `L = P\u1d38 \u214b\u1d9cL P\u1d3f\n (P\u1d38 \u214b\u1d9c P\u1d3f) `R = P\u1d38 \u214b\u1d9cR P\u1d3f\n\n _\u214b\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) \u214b\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m \u214b\u1d3e com Q)\n\n _\u214b\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P \u214b\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P \u214b\u1d3e P\u1d3f m)\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end ox\u1d3e Q = Q\n P ox\u1d3e end = P\n com P\u1d38 ox\u1d3e com P\u1d3f = \u03a0\u1d3e LR (P\u1d38 ox\u1d9c P\u1d3f)\n\n _ox\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 ox\u1d9c P\u1d3f) `L = P\u1d38 ox\u1d9cL P\u1d3f\n (P\u1d38 ox\u1d9c P\u1d3f) `R = P\u1d38 ox\u1d9cR P\u1d3f\n\n _ox\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) ox\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m ox\u1d3e com Q)\n\n _ox\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P ox\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P ox\u1d3e P\u1d3f m)\n\n data Viewox : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n com : \u2200{P Q}(p : \u27e6 \u03a0\u1d3e LR (P ox\u1d9c Q) \u27e7) \u2192 Viewox (com P) (com Q) p\n endL : \u2200{Q}(p : \u27e6 Q \u27e7) \u2192 Viewox end Q p\n endR : \u2200{P}(p : \u27e6 com P \u27e7) \u2192 Viewox (com P) end p\n\n viewox : \u2200 P Q (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 Viewox P Q p\n viewox end Q p = endL p\n viewox (com P) end p = endR p\n viewox (com P) (com Q) p = com p\n\n data View-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u2605\u2081 where\n sendLL : \u2200 {M} (P : M \u2192 Proto) Q R (m : M)(p : \u27e6 P m \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e M P) (com Q) R (`L , m , p) q\n recvLL : \u2200 {M} (P : M \u2192 Proto) Q R\n (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e com Q \u27e7))(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a0\u1d3e M P) (com Q) R (`L , p) q\n sendRR : \u2200 {M} P Q (R : M \u2192 Proto)(m : M)(p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a3\u1d3e M R) p (`R , m , q)\n recvRR : \u2200 {M} P Q (R : M \u2192 Proto)\n (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : (m : M) \u2192 \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a0\u1d3e M R) p (`R , q)\n\n sendR-recvL : \u2200 {M} P (Q : M \u2192 Proto) R (m : M)(p : \u27e6 com P \u214b\u1d3e Q m \u27e7)(q : (m : M) \u2192 \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a3\u1d3e M Q) (com R) (`R , m , p) (`L , q)\n recvR-sendL : \u2200 {M} P (Q : M \u2192 Proto) R (p : (m : M) \u2192 \u27e6 com P \u214b\u1d3e Q m \u27e7)(m : M)(q : \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a0\u1d3e M Q) (com R) (`R , p) (`L , m , q)\n\n endL : \u2200 Q R\n \u2192 (q : \u27e6 Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 end Q R q qr\n\n endM : \u2200 P R\n \u2192 (p : \u27e6 com P \u27e7)(r : \u27e6 R \u27e7)\n \u2192 View-\u2218 (com P) end R p r\n endR : \u2200 P Q\n \u2192 (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u27e7)\n \u2192 View-\u2218 (com P) (com Q) end p q\n\n view-\u2218 : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7) \u2192 View-\u2218 P Q R pq qr\n view-\u2218 P Q R pq qr = view-\u2218-view (view-\u214b P Q pq) (view-\u214b (dual Q) R qr)\n where\n view-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u214b P Q pq \u2192 View-\u214b (dual Q) R qr \u2192 View-\u2218 P Q R pq qr\n view-\u2218-view (sendL' _ _ _ _) _ = sendLL _ _ _ _ _ _\n view-\u2218-view (recvL' _ _ _) _ = recvLL _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvL' ._ _ _) = sendR-recvL _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (endR ._ _) = endR _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendL' ._ _ _ _) = recvR-sendL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (endR ._ ._) = endR _ _ _ _\n view-\u2218-view (endL _ _) _ = endL _ _ _ _\n view-\u2218-view (endR _ _) _ = endM _ _ _ _\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end = id\n \u214b\u1d3e-rend (com _) = id\n\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (com P) m p = `R , (m , p)\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL (com _) m p = `L , (m , p)\n\n \u214b\u1d3e-recvR : \u2200 {M}P{Q : M \u2192 Proto} \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e com' In M Q \u27e7\n \u214b\u1d3e-recvR end f = f\n \u214b\u1d3e-recvR (com _) f = `R , f\n\n \u214b\u1d3e-recvL : \u2200 {M}{P : M \u2192 Proto}Q \u2192 ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7) \u2192 \u27e6 \u03a0\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-recvL {P = P} end f x = \u214b\u1d3e-rend (P x) (f x)\n \u214b\u1d3e-recvL (com _) f = `L , f\n\n ox\u1d3e-rend : \u2200 P \u2192 \u27e6 P ox\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-rend end = id\n ox\u1d3e-rend (com x) = id\n\n ox\u1d3e-sendR : \u2200 {M P}{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n ox\u1d3e-sendR {P = end} m p = m , p\n ox\u1d3e-sendR {P = com x} m p = `R , (m , p)\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id end = _\n \u214b\u1d3e-id (com (mk In M P)) = `R , \u03bb m \u2192 \u214b\u1d3e-sendL (P m) m (\u214b\u1d3e-id (P m))\n \u214b\u1d3e-id (com (mk Out M P)) = `L , \u03bb m \u2192 \u214b\u1d3e-sendR (dual (P m)) m (\u214b\u1d3e-id (P m))\n\n module _ where\n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm = \u03bb P Q \u2192 to P Q , equiv P Q\n where\n to : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n to end end pq = pq\n to end (com x) pq = pq\n to (com x) end pq = pq\n to (com (mk In M P)) (com x\u2081) (`L , pq) = `R , (\u03bb m \u2192 to (P m) (com x\u2081) (pq m))\n to (com (mk Out M P)) (com x\u2081) (`L , m , pq) = `R , m , to (P m) (com x\u2081) pq\n to (com x) (com (mk In M P)) (`R , pq) = `L , (\u03bb m \u2192 to (com x) (P m) (pq m))\n to (com x) (com (mk Out M P)) (`R , m , pq) = `L , m , to (com x) (P m) pq\n\n toto : \u2200 P Q (x : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 to Q P (to P Q x) \u2261 x\n toto end end x = refl\n toto end (com (mk io M P)) x\u2081 = refl\n toto (com (mk io M P)) end x\u2081 = refl\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' In M\u2081 P\u2081) (pq x))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' Out M\u2081 P\u2081) (pq x))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' In M\u2081 P\u2081) pq))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' Out M\u2081 P\u2081) pq))\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' In M P) (P\u2081 x) (pq x)))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' In M P) (P\u2081 m) pq))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' Out M P) (P\u2081 x) (pq x)))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' Out M P) (P\u2081 m) pq))\n\n equiv : \u2200 P Q \u2192 Equiv (to P Q)\n equiv P Q = record { linv = to Q P ; is-linv = toto P Q ; rinv = to Q P ; is-rinv = toto Q P }\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e end Q p q = q\n comma\u1d3e (com _) end p q = p\n comma\u1d3e (\u03a3\u1d3e M P) (com Q) (m , p) q `L = m , comma\u1d3e (P m) (com Q) p q\n comma\u1d3e (\u03a0\u1d3e M P) (com Q) p q `L = \u03bb m \u2192 comma\u1d3e (P m) (com Q) (p m) q\n comma\u1d3e (com P) (\u03a3\u1d3e M Q) p (m , q) `R = m , comma\u1d3e (com P) (Q m) p q\n comma\u1d3e (com P) (\u03a0\u1d3e M Q) p q `R = \u03bb m \u2192 comma\u1d3e (com P) (Q m) p (q m)\n\n \u00d7\u2192ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n \u00d7\u2192ox\u1d3e P Q (p , q) = comma\u1d3e P Q p q\n\n ox\u1d3e-fst : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-fst end Q pq = _\n ox\u1d3e-fst (com _) end pq = pq\n ox\u1d3e-fst (\u03a0\u1d3e M P) (com Q) pq = \u03bb m \u2192 ox\u1d3e-fst (P m) (com Q) (pq `L m)\n ox\u1d3e-fst (\u03a3\u1d3e M P) (com Q) pq = \u00d7-map id (\u03bb {x} \u2192 ox\u1d3e-fst (P x) (com Q)) (pq `L)\n\n ox\u1d3e-snd : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 Q \u27e7\n ox\u1d3e-snd end Q pq = pq\n ox\u1d3e-snd (com _) end pq = _\n ox\u1d3e-snd (com P) (\u03a0\u1d3e M' Q) pq = \u03bb m' \u2192 ox\u1d3e-snd (com P) (Q m') (pq `R m')\n ox\u1d3e-snd (com P) (\u03a3\u1d3e M' Q) pq = \u00d7-map id (\u03bb {m'} \u2192 ox\u1d3e-snd (com P) (Q m')) (pq `R)\n\n ox\u1d3e\u2192\u00d7 : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n ox\u1d3e\u2192\u00d7 P Q p = ox\u1d3e-fst P Q p , ox\u1d3e-snd P Q p\n\n ox\u1d3e-comma-fst : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-fst P Q (comma\u1d3e P Q p q) \u2261 p\n ox\u1d3e-comma-fst end Q p q = refl\n ox\u1d3e-comma-fst (com P) end p q = refl\n ox\u1d3e-comma-fst (com P) (com Q) p q with view-com P p\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | send P m p = cong (_,_ m) (ox\u1d3e-comma-fst (P m) (com Q) p q)\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | recv P p = funExt \u03bb m \u2192 ox\u1d3e-comma-fst (P m) (com Q) (p m) q\n\n ox\u1d3e-comma-snd : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-snd P Q (comma\u1d3e P Q p q) \u2261 q\n ox\u1d3e-comma-snd end Q p q = refl\n ox\u1d3e-comma-snd (com P) end p q = refl\n ox\u1d3e-comma-snd (com P) (com Q) p q with view-com Q q\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p (q m)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p (q m)\n\n {-\n ox\u1d3e\u2192\u00d7-rinv : \u2200 {P Q} (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 \u00d7\u2192ox\u1d3e P Q (ox\u1d3e\u2192\u00d7 P Q p) \u2261 p\n ox\u1d3e\u2192\u00d7-rinv {P} {Q} p with viewox P Q p\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk In M\u2081 P\u2081} .p = funExt \u03bb { `L \u2192 funExt \u03bb m \u2192 {!ox\u1d3e\u2192\u00d7-rinv {P m} {\u03a0\u1d3e _ P\u2081} ?!} ; `R \u2192 {!!} }\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk In M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | endL .p = refl\n ox\u1d3e\u2192\u00d7-rinv p | endR {P} .p = refl\n\n \u00d7\u2192ox\u1d3e-equiv : \u2200 P Q \u2192 Equiv (\u00d7\u2192ox\u1d3e P Q)\n \u00d7\u2192ox\u1d3e-equiv P Q = record { linv = ox\u1d3e\u2192\u00d7 P Q\n ; is-linv = \u03bb { (x , y) \u2192 cong\u2082 _,_ (ox\u1d3e-comma-fst P Q x y) (ox\u1d3e-comma-snd P Q x y) }\n ; rinv = ox\u1d3e\u2192\u00d7 P Q\n ; is-rinv = ox\u1d3e\u2192\u00d7-rinv {P} {Q} }\n -}\n\n -- left-biased strategy\n par : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par P Q p q = par-view (view-proc P p)\n where par-view : \u2200 {P} {p : \u27e6 P \u27e7} \u2192 ViewProc P p \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par-view (send M P m p) = \u214b\u1d3e-sendL Q m (par (P m) Q p q)\n par-view (recv M P p) = \u214b\u1d3e-recvL Q \u03bb m \u2192 par (P m) Q (p m) q\n par-view end = q\n\n \u214b\u1d3e-apply : \u2200 {P Q} \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply {P} {Q} pq p with view-\u214b P Q pq\n \u214b\u1d3e-apply ._ p | sendL' P Q m pq = \u214b\u1d3e-apply {P m} pq (p m)\n \u214b\u1d3e-apply ._ p | sendR' P Q m pq = m , \u214b\u1d3e-apply {com P} {Q m} pq p\n \u214b\u1d3e-apply ._ (m , p) | recvL' P Q pq = \u214b\u1d3e-apply {P m} (pq m) p\n \u214b\u1d3e-apply ._ p | recvR' P Q pq = \u03bb m \u2192 \u214b\u1d3e-apply {com P} {Q m} (pq m) p\n \u214b\u1d3e-apply pq p | endL Q .pq = pq\n \u214b\u1d3e-apply pq p | endR P .pq = _\n\n \u214b\u1d3e-apply' : \u2200 {P Q} \u2192 \u27e6 dual P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply' {P} {Q} pq p = \u214b\u1d3e-apply {dual P} {Q} pq (subst \u27e6_\u27e7 (\u2261.sym (\u2261\u1d3e-sound funExt (dual-involutive P))) p)\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = \u214b\u1d3e-\u2218-view (view-\u2218 P Q R pq qr)\n where\n \u214b\u1d3e-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u2218 P Q R pq qr \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218-view (sendLL P Q R m p qr) = \u214b\u1d3e-sendL R m (\u214b\u1d3e-\u2218 (P m) (com Q) R p qr)\n \u214b\u1d3e-\u2218-view (recvLL P Q R p qr) = \u214b\u1d3e-recvL R (\u03bb m \u2192 \u214b\u1d3e-\u2218 (P m) (com Q) R (p m) qr)\n \u214b\u1d3e-\u2218-view (sendRR P Q R m pq q) = \u214b\u1d3e-sendR (com P) m (\u214b\u1d3e-\u2218 (com P) (com Q) (R m) pq q)\n \u214b\u1d3e-\u2218-view (recvRR P Q R pq q) = \u214b\u1d3e-recvR (com P) (\u03bb m\u2081 \u2192 \u214b\u1d3e-\u2218 (com P) (com Q) (R m\u2081) pq (q m\u2081))\n \u214b\u1d3e-\u2218-view (sendR-recvL P Q R m p q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) p (q m)\n \u214b\u1d3e-\u2218-view (recvR-sendL P Q R p m q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) (p m) q\n \u214b\u1d3e-\u2218-view (endL Q R pq qr) = \u214b\u1d3e-apply' {Q} {R} qr pq\n \u214b\u1d3e-\u2218-view (endM P R pq qr) = par (com P) R pq qr\n \u214b\u1d3e-\u2218-view (endR P Q pq qr) = \u214b\u1d3e-apply {com Q} {com P} (proj\u2081 (\u214b\u1d3e-comm (com P) (com Q)) pq) qr\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map P Q R S f g p = comma\u1d3e Q S (f (ox\u1d3e-fst P R p)) (g (ox\u1d3e-snd P R p))\n\n {-\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = switchL' P Q R (ox\u1d3e-fst (P \u214b\u1d3e Q) R pqr) (ox\u1d3e-snd (P \u214b\u1d3e Q) R pqr)\n -}\n\n {-\n switchL' : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7) (r : \u27e6 R \u27e7) \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL' P Q R pq r = {!switchL-view {!!}!}\n where\n switchL-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{r : \u27e6 R \u27e7} \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL-view {P} {Q} {R} {p\u214bq} {r} with view-\u214b P Q p\u214bq | view-proc R r\n switchL-view | sendL' P\u2081 Q\u2081 m p | vr = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | send M\u2081 P\u2082 m\u2081 p\u2081 = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | end = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | send M\u2081 P\u2082 m p\u2081 = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | end = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | send M\u2081 P\u2082 m p\u2081 = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | end = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | send M P\u2081 m p = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | recv M P\u2081 r\u2081 = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | end = {!!}\n switchL-view | endR P\u2081 pq\u2081 | send M P\u2082 m p = {!!}\n switchL-view | endR P\u2081 pq\u2081 | recv M P\u2082 r\u2081 = {!!}\n switchL-view | endR P\u2081 pq\u2081 | end = {!!}\n {-\n switchL-view {R = R}{r = r} | sendL' P Q m p = \u214b\u1d3e-sendL {!!} m (switchL' {!!} {!!} {!!} p r)\n switchL-view {R = R}{r = r} | sendR' P Q m p with view-proc R r\n switchL-view | sendR' {M\u2081} P\u2081 Q m\u2081 p\u2081 | send M P m p = {!!}\n switchL-view | sendR' {M\u2081} P\u2081 Q m p | recv M P r = {!!}\n switchL-view | sendR' {M} P Q m p | end = {!!}\n -- {!\u214b\u1d3e-map (com P) (com P) (Q m ox\u1d3e R) (\u03a3\u1d3e M Q ox\u1d3e R) id (ox\u1d3e-map (Q m) (\u03a3\u1d3e M Q) R R (_,_ m) id) (switchL-view (com P) (Q m) R p r)!}\n switchL-view {R = R}{r = r} | recvL' P Q p = \u214b\u1d3e-recvL (com Q ox\u1d3e R) \u03bb m \u2192 switchL' (P m) (com Q) R (p m) r\n switchL-view {R = R}{r = r} | recvR' (\u03a0\u1d9c M' P) Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL-view {R = R}{r = r} | recvR' (\u03a3\u1d9c M' P) Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL-view {R = R}{r = r} | endL Q p\u214bq = comma\u1d3e Q R p\u214bq r\n switchL-view {R = R}{r = r} | endR P p\u214bq = par (com P) R p\u214bq r\n -}\n -}\n\n \u2295\u1d3e-map : \u2200 {P Q R S} \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map f g (`L , pr) = `L , f pr\n \u2295\u1d3e-map f g (`R , pr) = `R , g pr\n\n &\u1d3e-map : \u2200 {P Q R S} \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map f g p `L = {!!}\n &\u1d3e-map f g p `R = {!!}\n -}\n\n {-\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Tele B \u00d7 Tele E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a3\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a0\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\nopen import Data.One hiding (_\u225f_)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP as \u2261\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\n {-\ndata S<_> {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n\npattern com' q M P = com (mk q M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\nopen ProtoRel _\u2261_ public renaming (_\u2248\u1d3e_ to _\u2261\u1d3e_)\n\n{-\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n -}\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n Trace : Proto \u2192 Proto\n Trace end = end\n Trace (com c) = com (Trace\u1d9c c)\n\n Trace\u1d9c : Com \u2192 Com\n Trace\u1d9c c = \u03a3\u1d9c (Com.M c) \u03bb m \u2192 Trace (Com.P c m)\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c c = mk (dual\u1d35\u1d3c (Com.io c)) (Com.M c) \u03bb m \u2192 dual (Com.P c m)\n\ndata IsTrace : Proto \u2192 \u2605\u2081 where\n end : IsTrace end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsTrace (P m)) \u2192 IsTrace (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\nmutual\n data Dual\u1d9c : Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a0\u1d9c M P) (\u03a3\u1d9c M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a3\u1d9c M P) (\u03a0\u1d9c M Q)\n\n data Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n {-\n \u03a0\u2610\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P [ x ]) (Q x)) \u2192 Dual (\u03a0\u1d3e (\u2610 M) P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0\u2610 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q [ x ])) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e (\u2610 M) Q)\n -}\n\n {-\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Trace : Choreo I \u2192 Proto\n \u2102Trace \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Trace-IsTrace : \u2200 \u2102 \u2192 IsTrace (\u2102Trace \u2102)\n \u2102Trace-IsTrace (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n-}\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com refl M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com refl M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\ndual\u1d35\u1d3c-involutive : \u2200 io \u2192 dual\u1d35\u1d3c (dual\u1d35\u1d3c io) \u2261 io\ndual\u1d35\u1d3c-involutive In = refl\ndual\u1d35\u1d3c-involutive Out = refl\n\ndual-involutive : \u2200 P \u2192 dual (dual P) \u2261\u1d3e P\ndual-involutive end = end\ndual-involutive (com' q M P) = com (dual\u1d35\u1d3c-involutive q) M (\u03bb m \u2192 dual-involutive (P m))\n\nTrace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261\u1d3e Trace P\nTrace-idempotent end = end\nTrace-idempotent (com' _ M P) = com refl M \u03bb m \u2192 Trace-idempotent (P m)\n\nTrace-dual-oblivious : \u2200 P \u2192 Trace (dual P) \u2261\u1d3e Trace P\nTrace-dual-oblivious end = end\nTrace-dual-oblivious (com' _ M P) = com refl M \u03bb m \u2192 Trace-dual-oblivious (P m)\n\nSink : Proto \u2192 Proto\nSink = dual \u2218 Trace\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n{-\n++Tele : \u2200 (P : Proto){Q : Tele P \u2192 Proto} (xs : Tele P) \u2192 Tele (Q xs) \u2192 Tele (P >>= Q)\n++Tele end _ ys = ys\n++Tele (com' q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com q M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com q M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n{-\nDual-sym (\u03a0\u2610\u00b7\u03a3 f) = {!\u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))!}\nDual-sym (\u03a3\u00b7\u03a0\u2610 f) = {!\u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))!}\n-}\n-}\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele (dual P) \u2261 Tele P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n El->>= : (P : Proto){Q : Tele P \u2192 Proto}{X : Tele (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Tele P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n-}\n\n>>=-com : (P : Proto){Q : Tele P \u2192 Proto}{R : Tele P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Tele P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Tele P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : \u2605_ a} (l r : A) \u2192 LR \u2192 A \n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP >>\u1d9c S = record P { P = \u03bb m \u2192 S (Com.P P m) }\n\n{-\nmodule V2 where\n mutual\n Sim\u1d3e : Proto \u2192 Proto \u2192 Proto\n Sim\u1d3e end Q = Q\n Sim\u1d3e (\u03a0\u1d3e M P) Q = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e (P m) Q\n Sim\u1d3e P end = P\n Sim\u1d3e P (\u03a0\u1d3e M Q) = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e P (Q m)\n Sim\u1d3e (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 Sim\u1d3e (P m) (\u03a3\u1d3e M' Q)) ,inr: (\u03bb m' \u2192 Sim\u1d3e (\u03a3\u1d3e M P) (Q m')) ]\n\n Sim\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\n Sim\u1d3e-assoc end Q R s = s\n Sim\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = Sim\u1d3e-assoc (P m) _ _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , Sim\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n Sim\u1d3e-sendR : \u2200 {M} P Q \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7 \u2192 (m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7\n Sim\u1d3e-sendR P Q s m = {!!}\n\n Sim\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7) \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7\n Sim\u1d3e-recvR P Q s = {!!}\n\n Sim\u1d3e-! : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 Sim\u1d3e Q P \u27e7\n Sim\u1d3e-! end Q p = {!!}\n Sim\u1d3e-! (\u03a0\u1d3e M P) end p x = Sim\u1d3e-! (P x) end (p x)\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-recvR (Q m') P \u03bb m \u2192 Sim\u1d3e-sendR {!!} {!!} (p m) m'\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 Sim\u1d3e-! (P m) (com (mk Out M' Q)) (p m) \n Sim\u1d3e-! (\u03a3\u1d3e M P) end p = p \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-! (com (mk Out M P)) (Q m') (p m') \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (Sim\u1d3e-! (P m) (com (mk Out M' Q)) p) \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (Sim\u1d3e-! (com (mk Out M P)) (Q m) p) \n\n Sim\u1d3e-apply : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n Sim\u1d3e-apply end Q s p = s\n Sim\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = Sim\u1d3e-apply (P m) Q (s m) p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = Sim\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n P \u214b\u1d3e end = P\n com P \u214b\u1d3e com Q = \u03a3\u1d3e LR (P \u214b\u1d9c Q)\n\n _\u214b\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 \u214b\u1d9c P\u1d3f) `L = P\u1d38 \u214b\u1d9cL P\u1d3f\n (P\u1d38 \u214b\u1d9c P\u1d3f) `R = P\u1d38 \u214b\u1d9cR P\u1d3f\n\n _\u214b\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) \u214b\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m \u214b\u1d3e com Q)\n\n _\u214b\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P \u214b\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P \u214b\u1d3e P\u1d3f m)\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end ox\u1d3e Q = Q\n P ox\u1d3e end = P\n com P\u1d38 ox\u1d3e com P\u1d3f = \u03a0\u1d3e LR (P\u1d38 ox\u1d9c P\u1d3f)\n\n _ox\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 ox\u1d9c P\u1d3f) `L = P\u1d38 ox\u1d9cL P\u1d3f\n (P\u1d38 ox\u1d9c P\u1d3f) `R = P\u1d38 ox\u1d9cR P\u1d3f\n\n _ox\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) ox\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m ox\u1d3e com Q)\n\n _ox\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P ox\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P ox\u1d3e P\u1d3f m)\n\n data ViewProc : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200 M(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewProc (\u03a3\u1d3e M P) (m , p)\n recv : \u2200 M(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewProc (\u03a0\u1d3e M P) p\n end : ViewProc end _\n\n view-proc : \u2200 P (p : \u27e6 P \u27e7) \u2192 ViewProc P p\n view-proc end _ = end\n view-proc (\u03a0\u1d3e M P) p = recv _ _ p\n view-proc (\u03a3\u1d3e M P) (m , p) = send _ _ m p\n\n data ViewCom : \u2200 P \u2192 \u27e6 com P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200{M}(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewCom (\u03a3\u1d9c M P) (m , p)\n recv : \u2200{M}(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewCom (\u03a0\u1d9c M P) p\n\n view-com : \u2200 P (p : \u27e6 com P \u27e7) \u2192 ViewCom P p\n view-com (\u03a0\u1d9c M P) p = recv _ p\n view-com (\u03a3\u1d9c M P) (m , p) = send _ m p\n\n data View-\u214b : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n sendL' : \u2200 {M} (P : M \u2192 Proto) Q (m : M)(p : \u27e6 P m \u214b\u1d3e com Q \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (com Q) (`L , m , p)\n sendR' : \u2200 {M} P (Q : M \u2192 Proto)(m : M)(p : \u27e6 com P \u214b\u1d3e Q m \u27e7) \u2192 View-\u214b (com P) (\u03a3\u1d3e M Q) (`R , m , p)\n recvL' : \u2200 {M} (P : M \u2192 Proto) Q (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e com Q \u27e7)) \u2192 View-\u214b (\u03a0\u1d3e M P) (com Q) (`L , p)\n recvR' : \u2200 {M} P (Q : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 com P \u214b\u1d3e Q m \u27e7)) \u2192 View-\u214b (com P) (\u03a0\u1d3e M Q) (`R , p)\n endL : \u2200 Q (p : \u27e6 Q \u27e7) \u2192 View-\u214b end Q p\n endR : \u2200 P (p : \u27e6 com P \u27e7) \u2192 View-\u214b (com P) end p\n\n view-\u214b : \u2200 P Q (p : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 View-\u214b P Q p\n view-\u214b end Q p = endL _ _\n view-\u214b (com x) end p = endR _ _\n view-\u214b (\u03a0\u1d3e M P) (com Q) (`L , p) = recvL' _ _ _\n view-\u214b (\u03a3\u1d3e M P) (com Q) (`L , (m , p)) = sendL' _ _ _ _\n view-\u214b (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) (`R , p) = recvR' _ _ _\n view-\u214b (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) (`R , (m , p)) = sendR' _ _ _ _\n view-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) (`R , p) = recvR' _ _ _\n view-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (`R , (m , p)) = sendR' _ _ _ _\n\n data Viewox : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n com : \u2200{P Q}(p : \u27e6 \u03a0\u1d3e LR (P ox\u1d9c Q) \u27e7) \u2192 Viewox (com P) (com Q) p\n endL : \u2200{Q}(p : \u27e6 Q \u27e7) \u2192 Viewox end Q p\n endR : \u2200{P}(p : \u27e6 com P \u27e7) \u2192 Viewox (com P) end p\n\n viewox : \u2200 P Q (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 Viewox P Q p\n viewox end Q p = endL p\n viewox (com P) end p = endR p\n viewox (com P) (com Q) p = com p\n\n data View-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u2605\u2081 where\n sendLL : \u2200 {M} (P : M \u2192 Proto) Q R (m : M)(p : \u27e6 P m \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e M P) (com Q) R (`L , m , p) q\n recvLL : \u2200 {M} (P : M \u2192 Proto) Q R\n (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e com Q \u27e7))(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a0\u1d3e M P) (com Q) R (`L , p) q\n sendRR : \u2200 {M} P Q (R : M \u2192 Proto)(m : M)(p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a3\u1d3e M R) p (`R , m , q)\n recvRR : \u2200 {M} P Q (R : M \u2192 Proto)\n (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : (m : M) \u2192 \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a0\u1d3e M R) p (`R , q)\n\n sendR-recvL : \u2200 {M} P (Q : M \u2192 Proto) R (m : M)(p : \u27e6 com P \u214b\u1d3e Q m \u27e7)(q : (m : M) \u2192 \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a3\u1d3e M Q) (com R) (`R , m , p) (`L , q)\n recvR-sendL : \u2200 {M} P (Q : M \u2192 Proto) R (p : (m : M) \u2192 \u27e6 com P \u214b\u1d3e Q m \u27e7)(m : M)(q : \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a0\u1d3e M Q) (com R) (`R , p) (`L , m , q)\n\n endL : \u2200 Q R\n \u2192 (q : \u27e6 Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 end Q R q qr\n\n endM : \u2200 P R\n \u2192 (p : \u27e6 com P \u27e7)(r : \u27e6 R \u27e7)\n \u2192 View-\u2218 (com P) end R p r\n endR : \u2200 P Q\n \u2192 (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u27e7)\n \u2192 View-\u2218 (com P) (com Q) end p q\n\n view-\u2218 : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7) \u2192 View-\u2218 P Q R pq qr\n view-\u2218 P Q R pq qr = view-\u2218-view (view-\u214b P Q pq) (view-\u214b (dual Q) R qr)\n where\n view-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u214b P Q pq \u2192 View-\u214b (dual Q) R qr \u2192 View-\u2218 P Q R pq qr\n view-\u2218-view (sendL' _ _ _ _) _ = sendLL _ _ _ _ _ _\n view-\u2218-view (recvL' _ _ _) _ = recvLL _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvL' ._ _ _) = sendR-recvL _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (endR ._ _) = endR _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendL' ._ _ _ _) = recvR-sendL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (endR ._ ._) = endR _ _ _ _\n view-\u2218-view (endL _ _) _ = endL _ _ _ _\n view-\u2218-view (endR _ _) _ = endM _ _ _ _\n\n record Equiv {A B : \u2605}(f : A \u2192 B) : \u2605 where\n field\n linv : B \u2192 A\n is-linv : \u2200 x \u2192 linv (f x) \u2261 x\n rinv : B \u2192 A\n is-rinv : \u2200 x \u2192 f (rinv x) \u2261 x\n\n id\u1d31 : \u2200 {A} \u2192 Equiv {A} id\n id\u1d31 = record\n { linv = id\n ; is-linv = \u03bb _ \u2192 refl\n ; rinv = id\n ; is-rinv = \u03bb _ \u2192 refl\n }\n\n module _ {A B C}{g : B \u2192 C}{f : A \u2192 B} where\n _\u2218\u1d31_ : Equiv g \u2192 Equiv f \u2192 Equiv (g \u2218 f)\n g\u1d31 \u2218\u1d31 f\u1d31 = record { linv = F.linv \u2218 G.linv ; is-linv = \u03bb x \u2192 cong F.linv (G.is-linv (f x)) \u2219 F.is-linv x\n ; rinv = F.rinv \u2218 G.rinv ; is-rinv = \u03bb x \u2192 cong g (F.is-rinv _) \u2219 G.is-rinv x }\n where\n module G = Equiv g\u1d31\n module F = Equiv f\u1d31\n\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = \u03a3 (A \u2192 B) Equiv\n\n module _ {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b} where\n \u03a3-ext : \u2200 {x y : \u03a3 A B} \u2192 (p : proj\u2081 x \u2261 proj\u2081 y) \u2192 subst B p (proj\u2082 x) \u2261 proj\u2082 y \u2192 x \u2261 y\n \u03a3-ext refl = cong (_,_ _)\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end = id\n \u214b\u1d3e-rend (com _) = id\n\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (com P) m p = `R , (m , p)\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL (com _) m p = `L , (m , p)\n\n \u214b\u1d3e-recvR : \u2200 {M}P{Q : M \u2192 Proto} \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e com' In M Q \u27e7\n \u214b\u1d3e-recvR end f = f\n \u214b\u1d3e-recvR (com _) f = `R , f\n\n \u214b\u1d3e-recvL : \u2200 {M}{P : M \u2192 Proto}Q \u2192 ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7) \u2192 \u27e6 \u03a0\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-recvL {P = P} end f x = \u214b\u1d3e-rend (P x) (f x)\n \u214b\u1d3e-recvL (com _) f = `L , f\n\n ox\u1d3e-rend : \u2200 P \u2192 \u27e6 P ox\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-rend end = id\n ox\u1d3e-rend (com x) = id\n\n ox\u1d3e-sendR : \u2200 {M P}{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n ox\u1d3e-sendR {P = end} m p = m , p\n ox\u1d3e-sendR {P = com x} m p = `R , (m , p)\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id end = _\n \u214b\u1d3e-id (com (mk In M P)) = `R , \u03bb m \u2192 \u214b\u1d3e-sendL (P m) m (\u214b\u1d3e-id (P m))\n \u214b\u1d3e-id (com (mk Out M P)) = `L , \u03bb m \u2192 \u214b\u1d3e-sendR (dual (P m)) m (\u214b\u1d3e-id (P m))\n\n module _ (\u03a0-ext : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : \u2200 x \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm = \u03bb P Q \u2192 to P Q , equiv P Q\n where\n to : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n to end end pq = pq\n to end (com x) pq = pq\n to (com x) end pq = pq\n to (com (mk In M P)) (com x\u2081) (`L , pq) = `R , (\u03bb m \u2192 to (P m) (com x\u2081) (pq m))\n to (com (mk Out M P)) (com x\u2081) (`L , m , pq) = `R , m , to (P m) (com x\u2081) pq\n to (com x) (com (mk In M P)) (`R , pq) = `L , (\u03bb m \u2192 to (com x) (P m) (pq m))\n to (com x) (com (mk Out M P)) (`R , m , pq) = `L , m , to (com x) (P m) pq\n\n toto : \u2200 P Q (x : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 to Q P (to P Q x) \u2261 x\n toto end end x = refl\n toto end (com (mk io M P)) x\u2081 = refl\n toto (com (mk io M P)) end x\u2081 = refl\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (\u03a0-ext \u03bb x \u2192 toto (P x) (com' In M\u2081 P\u2081) (pq x))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (\u03a0-ext \u03bb x \u2192 toto (P x) (com' Out M\u2081 P\u2081) (pq x))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' In M\u2081 P\u2081) pq))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' Out M\u2081 P\u2081) pq))\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (\u03a0-ext (\u03bb x \u2192 toto (com' In M P) (P\u2081 x) (pq x)))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' In M P) (P\u2081 m) pq))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (\u03a0-ext (\u03bb x \u2192 toto (com' Out M P) (P\u2081 x) (pq x)))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' Out M P) (P\u2081 m) pq))\n\n equiv : \u2200 P Q \u2192 Equiv (to P Q)\n equiv P Q = record { linv = to Q P ; is-linv = toto P Q ; rinv = to Q P ; is-rinv = toto Q P }\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e end Q p q = q\n comma\u1d3e (com _) end p q = p\n comma\u1d3e (\u03a3\u1d3e M P) (com Q) (m , p) q `L = m , comma\u1d3e (P m) (com Q) p q\n comma\u1d3e (\u03a0\u1d3e M P) (com Q) p q `L = \u03bb m \u2192 comma\u1d3e (P m) (com Q) (p m) q\n comma\u1d3e (com P) (\u03a3\u1d3e M Q) p (m , q) `R = m , comma\u1d3e (com P) (Q m) p q\n comma\u1d3e (com P) (\u03a0\u1d3e M Q) p q `R = \u03bb m \u2192 comma\u1d3e (com P) (Q m) p (q m)\n\n \u00d7\u2192ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n \u00d7\u2192ox\u1d3e P Q (p , q) = comma\u1d3e P Q p q\n\n ox\u1d3e-fst : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-fst end Q pq = _\n ox\u1d3e-fst (com _) end pq = pq\n ox\u1d3e-fst (\u03a0\u1d3e M P) (com Q) pq = \u03bb m \u2192 ox\u1d3e-fst (P m) (com Q) (pq `L m)\n ox\u1d3e-fst (\u03a3\u1d3e M P) (com Q) pq = \u00d7-map id (\u03bb {x} \u2192 ox\u1d3e-fst (P x) (com Q)) (pq `L)\n\n ox\u1d3e-snd : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 Q \u27e7\n ox\u1d3e-snd end Q pq = pq\n ox\u1d3e-snd (com _) end pq = _\n ox\u1d3e-snd (com P) (\u03a0\u1d3e M' Q) pq = \u03bb m' \u2192 ox\u1d3e-snd (com P) (Q m') (pq `R m')\n ox\u1d3e-snd (com P) (\u03a3\u1d3e M' Q) pq = \u00d7-map id (\u03bb {m'} \u2192 ox\u1d3e-snd (com P) (Q m')) (pq `R)\n\n ox\u1d3e\u2192\u00d7 : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n ox\u1d3e\u2192\u00d7 P Q p = ox\u1d3e-fst P Q p , ox\u1d3e-snd P Q p\n\n ox\u1d3e-comma-fst : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-fst P Q (comma\u1d3e P Q p q) \u2261 p\n ox\u1d3e-comma-fst end Q p q = refl\n ox\u1d3e-comma-fst (com P) end p q = refl\n ox\u1d3e-comma-fst (com P) (com Q) p q with view-com P p\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | send P m p = cong (_,_ m) (ox\u1d3e-comma-fst (P m) (com Q) p q)\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | recv P p = funExt \u03bb m \u2192 ox\u1d3e-comma-fst (P m) (com Q) (p m) q\n\n ox\u1d3e-comma-snd : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-snd P Q (comma\u1d3e P Q p q) \u2261 q\n ox\u1d3e-comma-snd end Q p q = refl\n ox\u1d3e-comma-snd (com P) end p q = refl\n ox\u1d3e-comma-snd (com P) (com Q) p q with view-com Q q\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p (q m)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p (q m)\n\n {-\n ox\u1d3e\u2192\u00d7-rinv : \u2200 {P Q} (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 \u00d7\u2192ox\u1d3e P Q (ox\u1d3e\u2192\u00d7 P Q p) \u2261 p\n ox\u1d3e\u2192\u00d7-rinv {P} {Q} p with viewox P Q p\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk In M\u2081 P\u2081} .p = funExt \u03bb { `L \u2192 funExt \u03bb m \u2192 {!ox\u1d3e\u2192\u00d7-rinv {P m} {\u03a0\u1d3e _ P\u2081} ?!} ; `R \u2192 {!!} }\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk In M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | endL .p = refl\n ox\u1d3e\u2192\u00d7-rinv p | endR {P} .p = refl\n\n \u00d7\u2192ox\u1d3e-equiv : \u2200 P Q \u2192 Equiv (\u00d7\u2192ox\u1d3e P Q)\n \u00d7\u2192ox\u1d3e-equiv P Q = record { linv = ox\u1d3e\u2192\u00d7 P Q\n ; is-linv = \u03bb { (x , y) \u2192 cong\u2082 _,_ (ox\u1d3e-comma-fst P Q x y) (ox\u1d3e-comma-snd P Q x y) }\n ; rinv = ox\u1d3e\u2192\u00d7 P Q\n ; is-rinv = ox\u1d3e\u2192\u00d7-rinv {P} {Q} }\n -}\n\n -- left-biased strategy\n par : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par P Q p q = par-view (view-proc P p)\n where par-view : \u2200 {P} {p : \u27e6 P \u27e7} \u2192 ViewProc P p \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par-view (send M P m p) = \u214b\u1d3e-sendL Q m (par (P m) Q p q)\n par-view (recv M P p) = \u214b\u1d3e-recvL Q \u03bb m \u2192 par (P m) Q (p m) q\n par-view end = q\n\n \u214b\u1d3e-apply : \u2200 {P Q} \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply {P} {Q} pq p with view-\u214b P Q pq\n \u214b\u1d3e-apply ._ p | sendL' P Q m pq = \u214b\u1d3e-apply {P m} pq (p m)\n \u214b\u1d3e-apply ._ p | sendR' P Q m pq = m , \u214b\u1d3e-apply {com P} {Q m} pq p\n \u214b\u1d3e-apply ._ (m , p) | recvL' P Q pq = \u214b\u1d3e-apply {P m} (pq m) p\n \u214b\u1d3e-apply ._ p | recvR' P Q pq = \u03bb m \u2192 \u214b\u1d3e-apply {com P} {Q m} (pq m) p\n \u214b\u1d3e-apply pq p | endL Q .pq = pq\n \u214b\u1d3e-apply pq p | endR P .pq = _\n\n \u214b\u1d3e-apply' : \u2200 {P Q} \u2192 \u27e6 dual P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply' {P} {Q} pq p = \u214b\u1d3e-apply {dual P} {Q} pq (subst \u27e6_\u27e7 (\u2261.sym (\u2261\u1d3e-sound funExt (dual-involutive P))) p)\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = \u214b\u1d3e-\u2218-view (view-\u2218 P Q R pq qr)\n where\n \u214b\u1d3e-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u2218 P Q R pq qr \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218-view (sendLL P Q R m p qr) = \u214b\u1d3e-sendL R m (\u214b\u1d3e-\u2218 (P m) (com Q) R p qr)\n \u214b\u1d3e-\u2218-view (recvLL P Q R p qr) = \u214b\u1d3e-recvL R (\u03bb m \u2192 \u214b\u1d3e-\u2218 (P m) (com Q) R (p m) qr)\n \u214b\u1d3e-\u2218-view (sendRR P Q R m pq q) = \u214b\u1d3e-sendR (com P) m (\u214b\u1d3e-\u2218 (com P) (com Q) (R m) pq q)\n \u214b\u1d3e-\u2218-view (recvRR P Q R pq q) = \u214b\u1d3e-recvR (com P) (\u03bb m\u2081 \u2192 \u214b\u1d3e-\u2218 (com P) (com Q) (R m\u2081) pq (q m\u2081))\n \u214b\u1d3e-\u2218-view (sendR-recvL P Q R m p q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) p (q m)\n \u214b\u1d3e-\u2218-view (recvR-sendL P Q R p m q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) (p m) q\n \u214b\u1d3e-\u2218-view (endL Q R pq qr) = \u214b\u1d3e-apply' {Q} {R} qr pq\n \u214b\u1d3e-\u2218-view (endM P R pq qr) = par (com P) R pq qr\n \u214b\u1d3e-\u2218-view (endR P Q pq qr) = {!\u214b\u1d3e-apply {?} {?} pq qr!}\n\n {-\n \u214b\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u214b\u1d3e R \u27e7 \u2192 \u27e6 Q \u214b\u1d3e S \u27e7\n \u214b\u1d3e-map P Q R S f g p with view-\u214b P R p\n \u214b\u1d3e-map .(com (mk Out M P)) Q\u2081 .(com Q) S f g .(`L , m , p) | sendL' M P Q m p = {!!}\n \u214b\u1d3e-map .(com P) Q\u2081 .(com (mk Out M Q)) S f g .(`R , m , p) | sendR' P M Q m p = {!!}\n \u214b\u1d3e-map .(com (mk In M P)) Q\u2081 .(com Q) S f g .(`L , p) | recvL' M P Q p = {!!}\n \u214b\u1d3e-map .(com P) Q\u2081 .(com (mk In M Q)) S f g .(`R , p) | recvR' P M Q p = {!!}\n \u214b\u1d3e-map .end Q R S f g p | endL .R .p = {!!}\n \u214b\u1d3e-map .(com P) Q .end S f g p | endR P .p = {!!}\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map P Q R S f g p = comma\u1d3e Q S (f (ox\u1d3e-fst P R p)) (g (ox\u1d3e-snd P R p))\n\n switchL' : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL' P Q R p\u214bq r with view-\u214b P Q p\u214bq\n switchL' ._ ._ R .(`L , m , p) r | sendL' M P Q m p = \u214b\u1d3e-sendL (com Q ox\u1d3e R) m (switchL' (P m) (com Q) R p r)\n switchL' ._ ._ R .(`R , m , p) r | sendR' P M Q m p = \u214b\u1d3e-map (com P) (com P) (Q m ox\u1d3e R) (\u03a3\u1d3e M Q ox\u1d3e R) id\n (ox\u1d3e-map (Q m) (\u03a3\u1d3e M Q) R R (_,_ m) id)\n (switchL' (com P) (Q m) R p r)\n switchL' ._ ._ R .(`L , p) r | recvL' M P Q p = \u214b\u1d3e-recvL (com Q ox\u1d3e R) \u03bb m \u2192 switchL' (P m) (com Q) R (p m) r\n switchL' ._ ._ R .(`R , p) r | recvR' (\u03a0\u1d9c M' P) M Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL' ._ ._ R .(`R , p) r | recvR' (\u03a3\u1d9c M' P) M Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL' ._ ._ R p\u214bq r | endL Q .p\u214bq = comma\u1d3e Q R p\u214bq r\n switchL' .(com P) .end R p\u214bq r | endR P .p\u214bq = par (com P) R p\u214bq r\n\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = switchL' P Q R (ox\u1d3e-fst (P \u214b\u1d3e Q) R pqr) (ox\u1d3e-snd (P \u214b\u1d3e Q) R pqr)\n -}\n\n {-\n \u2295\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map = {!!}\n\n &\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map = {!!}\n\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Tele B \u00d7 Tele E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a3\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a0\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"45831718134fddb82017042f777ad924faa14f70","subject":"par id defined, and send{L,R} and recv{L,R} for par","message":"par id defined, and send{L,R} and recv{L,R} for par\n","repos":"crypto-agda\/crypto-agda","old_file":"Control\/Protocol\/Choreography.agda","new_file":"Control\/Protocol\/Choreography.agda","new_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\nopen import Data.One hiding (_\u225f_)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\n {-\ndata S<_> {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n\npattern com' q M P = com (mk q M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\n\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n Trace : Proto \u2192 Proto\n Trace end = end\n Trace (com c) = com (Trace\u1d9c c)\n\n Trace\u1d9c : Com \u2192 Com\n Trace\u1d9c (mk io M P) = \u03a3\u1d9c M \u03bb m \u2192 Trace (P m)\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c (mk q M P) = mk (dual\u1d35\u1d3c q) M \u03bb m \u2192 dual (P m)\n\ndata IsTrace : Proto \u2192 \u2605\u2081 where\n end : IsTrace end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsTrace (P m)) \u2192 IsTrace (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\nmutual\n data Dual\u1d9c : Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a0\u1d9c M P) (\u03a3\u1d9c M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a3\u1d9c M P) (\u03a0\u1d9c M Q)\n\n data Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n {-\n \u03a0\u2610\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P [ x ]) (Q x)) \u2192 Dual (\u03a0\u1d3e (\u2610 M) P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0\u2610 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q [ x ])) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e (\u2610 M) Q)\n -}\n\n {-\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Trace : Choreo I \u2192 Proto\n \u2102Trace \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Trace-IsTrace : \u2200 \u2102 \u2192 IsTrace (\u2102Trace \u2102)\n \u2102Trace-IsTrace (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com q M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com q M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\nTrace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261\u1d3e Trace P\nTrace-idempotent end = end\nTrace-idempotent (com' _ M P) = com Out M \u03bb m \u2192 Trace-idempotent (P m)\n\nTrace-dual-oblivious : \u2200 P \u2192 Trace (dual P) \u2261\u1d3e Trace P\nTrace-dual-oblivious end = end\nTrace-dual-oblivious (com' _ M P) = com Out M \u03bb m \u2192 Trace-dual-oblivious (P m)\n\nSink : Proto \u2192 Proto\nSink = dual \u2218 Trace\n-}\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n{-\n++Tele : \u2200 (P : Proto){Q : Tele P \u2192 Proto} (xs : Tele P) \u2192 Tele (Q xs) \u2192 Tele (P >>= Q)\n++Tele end _ ys = ys\n++Tele (com' q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com q M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com q M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n{-\nDual-sym (\u03a0\u2610\u00b7\u03a3 f) = {!\u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))!}\nDual-sym (\u03a3\u00b7\u03a0\u2610 f) = {!\u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))!}\n-}\n-}\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele (dual P) \u2261 Tele P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n El->>= : (P : Proto){Q : Tele P \u2192 Proto}{X : Tele (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Tele P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n-}\n\n>>=-com : (P : Proto){Q : Tele P \u2192 Proto}{R : Tele P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Tele P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Tele P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : \u2605_ a} (l r : A) \u2192 LR \u2192 A \n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP >>\u1d9c S = record P { P = \u03bb m \u2192 S (Com.P P m) }\n\n{-\nmodule V2 where\n mutual\n Sim\u1d3e : Proto \u2192 Proto \u2192 Proto\n Sim\u1d3e end Q = Q\n Sim\u1d3e (\u03a0\u1d3e M P) Q = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e (P m) Q\n Sim\u1d3e P end = P\n Sim\u1d3e P (\u03a0\u1d3e M Q) = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e P (Q m)\n Sim\u1d3e (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 Sim\u1d3e (P m) (\u03a3\u1d3e M' Q)) ,inr: (\u03bb m' \u2192 Sim\u1d3e (\u03a3\u1d3e M P) (Q m')) ]\n\n Sim\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\n Sim\u1d3e-assoc end Q R s = s\n Sim\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = Sim\u1d3e-assoc (P m) _ _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , Sim\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n Sim\u1d3e-sendR : \u2200 {M} P Q \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7 \u2192 (m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7\n Sim\u1d3e-sendR P Q s m = {!!}\n\n Sim\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7) \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7\n Sim\u1d3e-recvR P Q s = {!!}\n\n Sim\u1d3e-! : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 Sim\u1d3e Q P \u27e7\n Sim\u1d3e-! end Q p = {!!}\n Sim\u1d3e-! (\u03a0\u1d3e M P) end p x = Sim\u1d3e-! (P x) end (p x)\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-recvR (Q m') P \u03bb m \u2192 Sim\u1d3e-sendR {!!} {!!} (p m) m'\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 Sim\u1d3e-! (P m) (com (mk Out M' Q)) (p m) \n Sim\u1d3e-! (\u03a3\u1d3e M P) end p = p \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-! (com (mk Out M P)) (Q m') (p m') \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (Sim\u1d3e-! (P m) (com (mk Out M' Q)) p) \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (Sim\u1d3e-! (com (mk Out M P)) (Q m) p) \n\n Sim\u1d3e-apply : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n Sim\u1d3e-apply end Q s p = s\n Sim\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = Sim\u1d3e-apply (P m) Q (s m) p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = Sim\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n -}\n\nmodule _ where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n P \u214b\u1d3e end = P\n com P\u1d38 \u214b\u1d3e com P\u1d3f = \u03a3\u1d3e LR (P\u1d38 \u214b\u1d9c P\u1d3f)\n\n _\u214b\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 \u214b\u1d9c P\u1d3f) `L = P\u1d38 \u214b\u1d9cL P\u1d3f\n (P\u1d38 \u214b\u1d9c P\u1d3f) `R = P\u1d38 \u214b\u1d9cR P\u1d3f\n\n _\u214b\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) \u214b\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m \u214b\u1d3e com Q)\n\n _\u214b\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P \u214b\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P \u214b\u1d3e P\u1d3f m)\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end ox\u1d3e Q = Q\n P ox\u1d3e end = P\n com P\u1d38 ox\u1d3e com P\u1d3f = \u03a0\u1d3e LR (P\u1d38 ox\u1d9c P\u1d3f)\n\n _ox\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 ox\u1d9c P\u1d3f) `L = P\u1d38 ox\u1d9cL P\u1d3f\n (P\u1d38 ox\u1d9c P\u1d3f) `R = P\u1d38 ox\u1d9cR P\u1d3f\n\n _ox\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) ox\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m ox\u1d3e com Q)\n\n _ox\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P ox\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P ox\u1d3e P\u1d3f m)\n\n Equiv : \u2200 {A B : \u2605} \u2192 (A \u2192 B) \u2192 \u2605\n Equiv {A} {B} f = \u2200 y \u2192 \u03a3 A (_\u2261_ y \u2218 f)\n\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = \u03a3 (A \u2192 B) Equiv\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end = id\n \u214b\u1d3e-rend (com x) = id\n\n \u214b\u1d3e-sendR : \u2200 {M P}{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR {P = end} m p = m , p\n \u214b\u1d3e-sendR {P = com x} m p = `R , (m , p)\n\n \u214b\u1d3e-sendL : \u2200 {M P}{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 Q m \u214b\u1d3e P \u27e7 \u2192 \u27e6 com' Out M Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-sendL {P = end}{Q} m p = m , \u214b\u1d3e-rend (Q m) p\n \u214b\u1d3e-sendL {P = com x} m p = `L , (m , p)\n\n \u214b\u1d3e-recvR : \u2200 {M P}{Q : M \u2192 Proto} \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e com' In M Q \u27e7\n \u214b\u1d3e-recvR {P = end} f = f\n \u214b\u1d3e-recvR {P = com x} f = `R , f\n\n \u214b\u1d3e-recvL : \u2200 {M P}{Q : M \u2192 Proto} \u2192 ((m : M) \u2192 \u27e6 Q m \u214b\u1d3e P \u27e7) \u2192 \u27e6 com' In M Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-recvL {P = end}{Q} f x = \u214b\u1d3e-rend (Q x) (f x)\n \u214b\u1d3e-recvL {P = com x} f = `L , f\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id end = _\n \u214b\u1d3e-id (com (mk In M P)) = `R , \u03bb m \u2192 \u214b\u1d3e-sendL {P = P m} m (\u214b\u1d3e-id (P m))\n \u214b\u1d3e-id (com (mk Out M P)) = `L , \u03bb m \u2192 \u214b\u1d3e-sendR {P = dual (P m)} m (\u214b\u1d3e-id (P m))\n\n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm P Q = {!!}\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e end Q p q = q\n comma\u1d3e (com x) end p q = p\n comma\u1d3e (\u03a3\u1d3e M P) (com Q) (m , p) q `L = m , comma\u1d3e (P m) (com Q) p q\n comma\u1d3e (\u03a0\u1d3e M P) (com Q) p q `L = \u03bb m \u2192 comma\u1d3e (P m) (com Q) (p m) q\n comma\u1d3e (com P) (\u03a3\u1d3e M Q) p (m , q) `R = m , comma\u1d3e (com P) (Q m) p q\n comma\u1d3e (com P) (\u03a0\u1d3e M Q) p q `R = \u03bb m \u2192 comma\u1d3e (com P) (Q m) p (q m)\n\n comma\u1d3e-equiv : \u2200 P Q \u2192 Equiv (comma\u1d3e P Q)\n comma\u1d3e-equiv P Q = {!!}\n\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = {!!}\n\n \u2295\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map = {!!}\n\n &\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map = {!!}\n\n \u214b\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u214b\u1d3e R \u27e7 \u2192 \u27e6 Q \u214b\u1d3e S \u27e7\n \u214b\u1d3e-map = {!!}\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map = {!!}\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = let z = switchL _ _ _ (comma\u1d3e (P \u214b\u1d3e Q) (dual Q \u214b\u1d3e R) pq qr) in {!z!}\n\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Tele B \u00d7 Tele E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a3\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a0\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\nopen import Data.One hiding (_\u225f_)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\n {-\ndata S<_> {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n\npattern com' q M P = com (mk q M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\n\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n Trace : Proto \u2192 Proto\n Trace end = end\n Trace (com c) = com (Trace\u1d9c c)\n\n Trace\u1d9c : Com \u2192 Com\n Trace\u1d9c (mk io M P) = \u03a3\u1d9c M \u03bb m \u2192 Trace (P m)\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c (mk q M P) = mk (dual\u1d35\u1d3c q) M \u03bb m \u2192 dual (P m)\n\ndata IsTrace : Proto \u2192 \u2605\u2081 where\n end : IsTrace end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsTrace (P m)) \u2192 IsTrace (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\nmutual\n data Dual\u1d9c : Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a0\u1d9c M P) (\u03a3\u1d9c M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a3\u1d9c M P) (\u03a0\u1d9c M Q)\n\n data Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n {-\n \u03a0\u2610\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P [ x ]) (Q x)) \u2192 Dual (\u03a0\u1d3e (\u2610 M) P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0\u2610 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q [ x ])) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e (\u2610 M) Q)\n -}\n\n {-\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Trace : Choreo I \u2192 Proto\n \u2102Trace \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Trace-IsTrace : \u2200 \u2102 \u2192 IsTrace (\u2102Trace \u2102)\n \u2102Trace-IsTrace (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com q M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com q M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\nTrace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261\u1d3e Trace P\nTrace-idempotent end = end\nTrace-idempotent (com' _ M P) = com Out M \u03bb m \u2192 Trace-idempotent (P m)\n\nTrace-dual-oblivious : \u2200 P \u2192 Trace (dual P) \u2261\u1d3e Trace P\nTrace-dual-oblivious end = end\nTrace-dual-oblivious (com' _ M P) = com Out M \u03bb m \u2192 Trace-dual-oblivious (P m)\n\nSink : Proto \u2192 Proto\nSink = dual \u2218 Trace\n-}\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n{-\n++Tele : \u2200 (P : Proto){Q : Tele P \u2192 Proto} (xs : Tele P) \u2192 Tele (Q xs) \u2192 Tele (P >>= Q)\n++Tele end _ ys = ys\n++Tele (com' q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com q M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com q M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n{-\nDual-sym (\u03a0\u2610\u00b7\u03a3 f) = {!\u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))!}\nDual-sym (\u03a3\u00b7\u03a0\u2610 f) = {!\u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))!}\n-}\n-}\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele (dual P) \u2261 Tele P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n El->>= : (P : Proto){Q : Tele P \u2192 Proto}{X : Tele (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Tele P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n-}\n\n>>=-com : (P : Proto){Q : Tele P \u2192 Proto}{R : Tele P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Tele P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Tele P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : \u2605_ a} (l r : A) \u2192 LR \u2192 A \n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP >>\u1d9c S = record P { P = \u03bb m \u2192 S (Com.P P m) }\n\n{-\nmodule V2 where\n mutual\n Sim\u1d3e : Proto \u2192 Proto \u2192 Proto\n Sim\u1d3e end Q = Q\n Sim\u1d3e (\u03a0\u1d3e M P) Q = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e (P m) Q\n Sim\u1d3e P end = P\n Sim\u1d3e P (\u03a0\u1d3e M Q) = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e P (Q m)\n Sim\u1d3e (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 Sim\u1d3e (P m) (\u03a3\u1d3e M' Q)) ,inr: (\u03bb m' \u2192 Sim\u1d3e (\u03a3\u1d3e M P) (Q m')) ]\n\n Sim\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\n Sim\u1d3e-assoc end Q R s = s\n Sim\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = Sim\u1d3e-assoc (P m) _ _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , Sim\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n Sim\u1d3e-sendR : \u2200 {M} P Q \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7 \u2192 (m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7\n Sim\u1d3e-sendR P Q s m = {!!}\n\n Sim\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7) \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7\n Sim\u1d3e-recvR P Q s = {!!}\n\n Sim\u1d3e-! : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 Sim\u1d3e Q P \u27e7\n Sim\u1d3e-! end Q p = {!!}\n Sim\u1d3e-! (\u03a0\u1d3e M P) end p x = Sim\u1d3e-! (P x) end (p x)\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-recvR (Q m') P \u03bb m \u2192 Sim\u1d3e-sendR {!!} {!!} (p m) m'\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 Sim\u1d3e-! (P m) (com (mk Out M' Q)) (p m) \n Sim\u1d3e-! (\u03a3\u1d3e M P) end p = p \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-! (com (mk Out M P)) (Q m') (p m') \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (Sim\u1d3e-! (P m) (com (mk Out M' Q)) p) \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (Sim\u1d3e-! (com (mk Out M P)) (Q m) p) \n\n Sim\u1d3e-apply : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n Sim\u1d3e-apply end Q s p = s\n Sim\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = Sim\u1d3e-apply (P m) Q (s m) p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = Sim\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n -}\n\nmodule _ where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n P \u214b\u1d3e end = P\n com P\u1d38 \u214b\u1d3e com P\u1d3f = \u03a3\u1d3e LR (P\u1d38 \u214b\u1d9c P\u1d3f)\n\n _\u214b\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 \u214b\u1d9c P\u1d3f) `L = P\u1d38 \u214b\u1d9cL P\u1d3f\n (P\u1d38 \u214b\u1d9c P\u1d3f) `R = P\u1d38 \u214b\u1d9cR P\u1d3f\n\n _\u214b\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) \u214b\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m \u214b\u1d3e com Q)\n\n _\u214b\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P \u214b\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P \u214b\u1d3e P\u1d3f m)\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end ox\u1d3e Q = Q\n P ox\u1d3e end = P\n com P\u1d38 ox\u1d3e com P\u1d3f = \u03a0\u1d3e LR (P\u1d38 ox\u1d9c P\u1d3f)\n\n _ox\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 ox\u1d9c P\u1d3f) `L = P\u1d38 ox\u1d9cL P\u1d3f\n (P\u1d38 ox\u1d9c P\u1d3f) `R = P\u1d38 ox\u1d9cR P\u1d3f\n\n _ox\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) ox\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m ox\u1d3e com Q)\n\n _ox\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P ox\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P ox\u1d3e P\u1d3f m)\n\n Equiv : \u2200 {A B : \u2605} \u2192 (A \u2192 B) \u2192 \u2605\n Equiv {A} {B} f = \u2200 y \u2192 \u03a3 A (_\u2261_ y \u2218 f)\n\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = \u03a3 (A \u2192 B) Equiv\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id = {!!}\n \n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm P Q = {!!}\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e end Q p q = q\n comma\u1d3e (com x) end p q = p\n comma\u1d3e (\u03a3\u1d3e M P) (com Q) (m , p) q `L = m , comma\u1d3e (P m) (com Q) p q\n comma\u1d3e (\u03a0\u1d3e M P) (com Q) p q `L = \u03bb m \u2192 comma\u1d3e (P m) (com Q) (p m) q\n comma\u1d3e (com P) (\u03a3\u1d3e M Q) p (m , q) `R = m , comma\u1d3e (com P) (Q m) p q\n comma\u1d3e (com P) (\u03a0\u1d3e M Q) p q `R = \u03bb m \u2192 comma\u1d3e (com P) (Q m) p (q m)\n\n comma\u1d3e-equiv : \u2200 P Q \u2192 Equiv (comma\u1d3e P Q)\n comma\u1d3e-equiv P Q = {!!}\n\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = {!!}\n\n \u2295\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map = {!!}\n\n &\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map = {!!}\n\n \u214b\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u214b\u1d3e R \u27e7 \u2192 \u27e6 Q \u214b\u1d3e S \u27e7\n \u214b\u1d3e-map = {!!}\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map = {!!}\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = let z = switchL _ _ _ (comma\u1d3e (P \u214b\u1d3e Q) (dual Q \u214b\u1d3e R) pq qr) in {!z!}\n\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Tele B \u00d7 Tele E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a3\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a0\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"db5307718bfbf262aa80ac0a0dd4b8da3799363b","subject":"Change where-clause to let-expr so that agda 2.3.2 typechecks README","message":"Change where-clause to let-expr so that agda 2.3.2 typechecks README\n\nOld-commit-hash: 6ffa74c100a7ee9d51e15d960db14c801f06e27c\n","repos":"inc-lc\/ilc-agda","old_file":"Denotational\/WeakValidChanges.agda","new_file":"Denotational\/WeakValidChanges.agda","new_contents":"module Denotational.WeakValidChanges where\n\nopen import Data.Product\nopen import Data.Unit\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.ChangeTypes.ChangesAreDerivatives\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Changes\nopen import Denotational.EqualityLemmas\n\n-- DEFINITION of weakly valid changes via a logical relation\n\n-- Strong validity:\nopen import Denotational.ValidChanges\n\n-- Weak validity, defined through a function producing a type.\n--\n-- Since this is a logical relation, this couldn't be defined as a\n-- datatype, since it is not strictly positive.\n\nWeak-Valid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nWeak-Valid-\u0394 {bool} v dv = \u22a4\nWeak-Valid-\u0394 {S \u21d2 T} f df =\n \u2200 (s : \u27e6 S \u27e7) ds (valid-w : Weak-Valid-\u0394 s ds) \u2192\n Weak-Valid-\u0394 (f s) (df s ds) \u00d7\n df is-correct-for f on s and ds\n\nstrong-to-weak-validity : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv} \u2192 Valid-\u0394 v dv \u2192 Weak-Valid-\u0394 v dv\nstrong-to-weak-validity {bool} _ = tt\nstrong-to-weak-validity {\u03c4 \u21d2 \u03c4\u2081} {v} {dv} s-valid-v-dv = \u03bb s ds _ \u2192 \n let\n proofs = s-valid-v-dv s ds\n dv-s-ds-valid-on-v-s = proj\u2081 proofs\n dv-is-correct-for-v-on-s-and-ds = proj\u2082 proofs\n in\n strong-to-weak-validity dv-s-ds-valid-on-v-s , dv-is-correct-for-v-on-s-and-ds\n\n{-\n-- This proof doesn't go through: the desired equivalence is too\n-- strong, and we can't fill the holes because dv is arbitrary.\n\ndiff-apply-proof-weak : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n (Weak-Valid-\u0394 v dv) \u2192 diff (apply dv v) v \u2261 dv\n\ndiff-apply-proof-weak {\u03c4\u2081 \u21d2 \u03c4\u2082} df f df-valid = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n begin\n diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (sym (proj\u2082 (df-valid (apply dv v) (derive (apply dv v)) (strong-to-weak-validity (derive-is-valid (apply dv v)))))) \u27e9\n diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff (apply df f x) (f v))\n (apply-derive (apply dv v)) \u27e9\n diff ((apply df f) (apply dv v)) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (proj\u2082 (df-valid v dv {!!})) \u27e9\n diff (apply (df v dv) (f v)) (f v)\n \u2261\u27e8 diff-apply-proof-weak {\u03c4\u2082} (df v dv) (f v) (proj\u2081 (df-valid v dv {!!})) \u27e9\n df v dv\n \u220e)) where open \u2261-Reasoning\n\ndiff-apply-proof-weak {bool} db b _ = xor-cancellative b db\n\n-- That'd be the core of the proof of diff-apply-weak, if we relax the\n-- equivalence in the goal to something more correct. We need a proof\n-- of validity only for the first argument, somehow: maybe because\n-- diff itself produces strongly valid changes?\n--\n-- Ahah! Not really: below we use the unprovable\n-- diff-apply-proof-weak. To be able to use induction, we need to use\n-- this weaker lemma again. Hence, we'll only be able to prove the\n-- same equivalence, which will needs another proof of validity to\n-- unfold further until the base case.\n\ndiff-apply-proof-weak-f : \u2200 {\u03c4\u2081 \u03c4\u2082} (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (f : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) \u2192\n (Weak-Valid-\u0394 f df) \u2192 \u2200 dv v \u2192 (Weak-Valid-\u0394 v dv) \u2192 (diff (apply df f) f) v dv \u2261 df v dv\n\ndiff-apply-proof-weak-f {\u03c4\u2081} {\u03c4\u2082} df f df-valid dv v dv-valid =\n begin\n diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (sym (proj\u2082 (df-valid (apply dv v) (derive (apply dv v)) (strong-to-weak-validity (derive-is-valid (apply dv v)))))) \u27e9\n diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff (apply df f x) (f v))\n (apply-derive (apply dv v)) \u27e9\n diff ((apply df f) (apply dv v)) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (proj\u2082 (df-valid v dv dv-valid)) \u27e9\n diff (apply (df v dv) (f v)) (f v)\n \u2261\u27e8 diff-apply-proof-weak {\u03c4\u2082} (df v dv) (f v) (proj\u2081 (df-valid v dv dv-valid)) \u27e9\n df v dv\n \u220e where open \u2261-Reasoning\n-}\n\n-- A stronger delta equivalence. Note this isn't a congruence; it\n-- should be possible to define some congruence rules, but those will\n-- be more complex.\n\ndata _\u2248_ : {\u03c4 : Type} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set where\n --base : \u2200 (v : \u27e6 bool \u27e7) \u2192 v \u2248 v\n base : \u2200 (v1 v2 : \u27e6 bool \u27e7) \u2192 (\u2261 : v1 \u2261 v2) \u2192 v1 \u2248 v2\n fun : \u2200 {\u03c3 \u03c4} (f1 f2 : \u27e6 \u0394-Type (\u03c3 \u21d2 \u03c4) \u27e7) \u2192\n (t\u2248 : \u2200 s ds (valid : Weak-Valid-\u0394 s ds) \u2192 f1 s ds \u2248 f2 s ds) \u2192\n f1 \u2248 f2\n\n\u2261\u2192\u2248 : \u2200 {\u03c4} {v1 v2 : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192 v1 \u2261 v2 \u2192 v1 \u2248 v2\n\u2261\u2192\u2248 {bool} {v1} {.v1} refl = base v1 v1 refl\n\u2261\u2192\u2248 {\u03c4\u2081 \u21d2 \u03c4\u2082} {v1} {.v1} refl = fun v1 v1 (\u03bb s ds _ \u2192 \u2261\u2192\u2248 refl)\n\nopen import Relation.Binary hiding (_\u21d2_)\n\n\u2248-refl : \u2200 {\u03c4} \u2192 Reflexive (_\u2248_ {\u03c4})\n\u2248-refl = \u2261\u2192\u2248 refl\n\n\u2248-sym : \u2200 {\u03c4} \u2192 Symmetric (_\u2248_ {\u03c4})\n\u2248-sym (base v1 v2 \u2261) = base _ _ (sym \u2261)\n\u2248-sym {(\u03c3 \u21d2 \u03c4)} (fun f1 f2 t\u2248) = fun _ _ (\u03bb s ds valid \u2192 \u2248-sym (t\u2248 s ds valid))\n\n\u2248-trans : \u2200 {\u03c4} \u2192 Transitive (_\u2248_ {\u03c4})\n\u2248-trans {.bool} {.k} {.k} {k} (base .k .k refl) (base .k .k refl) = base k k refl\n\u2248-trans {\u03c3 \u21d2 \u03c4} {.f1} {.f2} {k} (fun f1 .f2 \u2248\u2081) (fun f2 .k \u2248\u2082) = fun f1 k (\u03bb s ds valid \u2192 \u2248-trans (\u2248\u2081 _ _ valid) (\u2248\u2082 _ _ valid))\n\n\u2248-isEquivalence : \u2200 {\u03c4} \u2192 IsEquivalence (_\u2248_ {\u03c4})\n\u2248-isEquivalence = record\n { refl = \u2248-refl\n ; sym = \u2248-sym\n ; trans = \u2248-trans\n }\n\n\u2248-setoid : Type \u2192 Setoid _ _\n\u2248-setoid \u03c4 = record\n { Carrier = \u27e6 \u0394-Type \u03c4 \u27e7\n ; _\u2248_ = _\u2248_\n ; isEquivalence = \u2248-isEquivalence\n }\n\nmodule \u2248-Reasoning where\n module _ {\u03c4 : Type} where\n open import Relation.Binary.EqReasoning (\u2248-setoid \u03c4) public\n hiding (_\u2261\u27e8_\u27e9_)\n\n-- Correct proof, to refactor using \u2248-Reasoning\n\ndiff-apply-proof-weak-real : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n (Weak-Valid-\u0394 v dv) \u2192 diff (apply dv v) v \u2248 dv\ndiff-apply-proof-weak-real {\u03c4\u2081 \u21d2 \u03c4\u2082} df f df-valid = fun _ _ (\u03bb v dv dv-valid \u2192 \u2248-trans (\u2261\u2192\u2248 (\n begin\n diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (sym (proj\u2082 (df-valid (apply dv v) (derive (apply dv v)) (strong-to-weak-validity (derive-is-valid (apply dv v)))))) \u27e9\n diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff (apply df f x) (f v))\n (apply-derive (apply dv v)) \u27e9\n diff ((apply df f) (apply dv v)) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (proj\u2082 (df-valid v dv dv-valid)) \u27e9\n diff (apply (df v dv) (f v)) (f v)\n \u220e)) (diff-apply-proof-weak-real {\u03c4\u2082} (df v dv) (f v) (proj\u2081 (df-valid v dv dv-valid))))\n --\u2248\u27e8 fill in the above proof \u27e9\n --df v dv\n\n where open \u2261-Reasoning\n\ndiff-apply-proof-weak-real {bool} db b _ = \u2261\u2192\u2248 (xor-cancellative b db)\n","old_contents":"module Denotational.WeakValidChanges where\n\nopen import Data.Product\nopen import Data.Unit\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.ChangeTypes.ChangesAreDerivatives\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Changes\nopen import Denotational.EqualityLemmas\n\n-- DEFINITION of weakly valid changes via a logical relation\n\n-- Strong validity:\nopen import Denotational.ValidChanges\n\n-- Weak validity, defined through a function producing a type.\n--\n-- Since this is a logical relation, this couldn't be defined as a\n-- datatype, since it is not strictly positive.\n\nWeak-Valid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nWeak-Valid-\u0394 {bool} v dv = \u22a4\nWeak-Valid-\u0394 {S \u21d2 T} f df =\n \u2200 (s : \u27e6 S \u27e7) ds (valid-w : Weak-Valid-\u0394 s ds) \u2192\n Weak-Valid-\u0394 (f s) (df s ds) \u00d7\n df is-correct-for f on s and ds\n\nstrong-to-weak-validity : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv} \u2192 Valid-\u0394 v dv \u2192 Weak-Valid-\u0394 v dv\nstrong-to-weak-validity {bool} _ = tt\nstrong-to-weak-validity {\u03c4 \u21d2 \u03c4\u2081} {v} {dv} s-valid-v-dv s ds _ = strong-to-weak-validity dv-s-ds-valid-on-v-s , dv-is-correct-for-v-on-s-and-ds\n where\n proofs = s-valid-v-dv s ds\n dv-s-ds-valid-on-v-s = proj\u2081 proofs\n dv-is-correct-for-v-on-s-and-ds = proj\u2082 proofs\n\n{-\n-- This proof doesn't go through: the desired equivalence is too\n-- strong, and we can't fill the holes because dv is arbitrary.\n\ndiff-apply-proof-weak : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n (Weak-Valid-\u0394 v dv) \u2192 diff (apply dv v) v \u2261 dv\n\ndiff-apply-proof-weak {\u03c4\u2081 \u21d2 \u03c4\u2082} df f df-valid = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n begin\n diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (sym (proj\u2082 (df-valid (apply dv v) (derive (apply dv v)) (strong-to-weak-validity (derive-is-valid (apply dv v)))))) \u27e9\n diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff (apply df f x) (f v))\n (apply-derive (apply dv v)) \u27e9\n diff ((apply df f) (apply dv v)) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (proj\u2082 (df-valid v dv {!!})) \u27e9\n diff (apply (df v dv) (f v)) (f v)\n \u2261\u27e8 diff-apply-proof-weak {\u03c4\u2082} (df v dv) (f v) (proj\u2081 (df-valid v dv {!!})) \u27e9\n df v dv\n \u220e)) where open \u2261-Reasoning\n\ndiff-apply-proof-weak {bool} db b _ = xor-cancellative b db\n\n-- That'd be the core of the proof of diff-apply-weak, if we relax the\n-- equivalence in the goal to something more correct. We need a proof\n-- of validity only for the first argument, somehow: maybe because\n-- diff itself produces strongly valid changes?\n--\n-- Ahah! Not really: below we use the unprovable\n-- diff-apply-proof-weak. To be able to use induction, we need to use\n-- this weaker lemma again. Hence, we'll only be able to prove the\n-- same equivalence, which will needs another proof of validity to\n-- unfold further until the base case.\n\ndiff-apply-proof-weak-f : \u2200 {\u03c4\u2081 \u03c4\u2082} (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (f : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) \u2192\n (Weak-Valid-\u0394 f df) \u2192 \u2200 dv v \u2192 (Weak-Valid-\u0394 v dv) \u2192 (diff (apply df f) f) v dv \u2261 df v dv\n\ndiff-apply-proof-weak-f {\u03c4\u2081} {\u03c4\u2082} df f df-valid dv v dv-valid =\n begin\n diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (sym (proj\u2082 (df-valid (apply dv v) (derive (apply dv v)) (strong-to-weak-validity (derive-is-valid (apply dv v)))))) \u27e9\n diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff (apply df f x) (f v))\n (apply-derive (apply dv v)) \u27e9\n diff ((apply df f) (apply dv v)) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (proj\u2082 (df-valid v dv dv-valid)) \u27e9\n diff (apply (df v dv) (f v)) (f v)\n \u2261\u27e8 diff-apply-proof-weak {\u03c4\u2082} (df v dv) (f v) (proj\u2081 (df-valid v dv dv-valid)) \u27e9\n df v dv\n \u220e where open \u2261-Reasoning\n-}\n\n-- A stronger delta equivalence. Note this isn't a congruence; it\n-- should be possible to define some congruence rules, but those will\n-- be more complex.\n\ndata _\u2248_ : {\u03c4 : Type} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set where\n --base : \u2200 (v : \u27e6 bool \u27e7) \u2192 v \u2248 v\n base : \u2200 (v1 v2 : \u27e6 bool \u27e7) \u2192 (\u2261 : v1 \u2261 v2) \u2192 v1 \u2248 v2\n fun : \u2200 {\u03c3 \u03c4} (f1 f2 : \u27e6 \u0394-Type (\u03c3 \u21d2 \u03c4) \u27e7) \u2192\n (t\u2248 : \u2200 s ds (valid : Weak-Valid-\u0394 s ds) \u2192 f1 s ds \u2248 f2 s ds) \u2192\n f1 \u2248 f2\n\n\u2261\u2192\u2248 : \u2200 {\u03c4} {v1 v2 : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192 v1 \u2261 v2 \u2192 v1 \u2248 v2\n\u2261\u2192\u2248 {bool} {v1} {.v1} refl = base v1 v1 refl\n\u2261\u2192\u2248 {\u03c4\u2081 \u21d2 \u03c4\u2082} {v1} {.v1} refl = fun v1 v1 (\u03bb s ds _ \u2192 \u2261\u2192\u2248 refl)\n\nopen import Relation.Binary hiding (_\u21d2_)\n\n\u2248-refl : \u2200 {\u03c4} \u2192 Reflexive (_\u2248_ {\u03c4})\n\u2248-refl = \u2261\u2192\u2248 refl\n\n\u2248-sym : \u2200 {\u03c4} \u2192 Symmetric (_\u2248_ {\u03c4})\n\u2248-sym (base v1 v2 \u2261) = base _ _ (sym \u2261)\n\u2248-sym {(\u03c3 \u21d2 \u03c4)} (fun f1 f2 t\u2248) = fun _ _ (\u03bb s ds valid \u2192 \u2248-sym (t\u2248 s ds valid))\n\n\u2248-trans : \u2200 {\u03c4} \u2192 Transitive (_\u2248_ {\u03c4})\n\u2248-trans {.bool} {.k} {.k} {k} (base .k .k refl) (base .k .k refl) = base k k refl\n\u2248-trans {\u03c3 \u21d2 \u03c4} {.f1} {.f2} {k} (fun f1 .f2 \u2248\u2081) (fun f2 .k \u2248\u2082) = fun f1 k (\u03bb s ds valid \u2192 \u2248-trans (\u2248\u2081 _ _ valid) (\u2248\u2082 _ _ valid))\n\n\u2248-isEquivalence : \u2200 {\u03c4} \u2192 IsEquivalence (_\u2248_ {\u03c4})\n\u2248-isEquivalence = record\n { refl = \u2248-refl\n ; sym = \u2248-sym\n ; trans = \u2248-trans\n }\n\n\u2248-setoid : Type \u2192 Setoid _ _\n\u2248-setoid \u03c4 = record\n { Carrier = \u27e6 \u0394-Type \u03c4 \u27e7\n ; _\u2248_ = _\u2248_\n ; isEquivalence = \u2248-isEquivalence\n }\n\nmodule \u2248-Reasoning where\n module _ {\u03c4 : Type} where\n open import Relation.Binary.EqReasoning (\u2248-setoid \u03c4) public\n hiding (_\u2261\u27e8_\u27e9_)\n\n-- Correct proof, to refactor using \u2248-Reasoning\n\ndiff-apply-proof-weak-real : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n (Weak-Valid-\u0394 v dv) \u2192 diff (apply dv v) v \u2248 dv\ndiff-apply-proof-weak-real {\u03c4\u2081 \u21d2 \u03c4\u2082} df f df-valid = fun _ _ (\u03bb v dv dv-valid \u2192 \u2248-trans (\u2261\u2192\u2248 (\n begin\n diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (sym (proj\u2082 (df-valid (apply dv v) (derive (apply dv v)) (strong-to-weak-validity (derive-is-valid (apply dv v)))))) \u27e9\n diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff (apply df f x) (f v))\n (apply-derive (apply dv v)) \u27e9\n diff ((apply df f) (apply dv v)) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (proj\u2082 (df-valid v dv dv-valid)) \u27e9\n diff (apply (df v dv) (f v)) (f v)\n \u220e)) (diff-apply-proof-weak-real {\u03c4\u2082} (df v dv) (f v) (proj\u2081 (df-valid v dv dv-valid))))\n --\u2248\u27e8 fill in the above proof \u27e9\n --df v dv\n\n where open \u2261-Reasoning\n\ndiff-apply-proof-weak-real {bool} db b _ = \u2261\u2192\u2248 (xor-cancellative b db)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c358fe1ba64f44c5f57f72267b2aa9d0ffc61be4","subject":"Pseudocode for","message":"Pseudocode for\n\nOld-commit-hash: 3d42c268469649df0f436aa927eb3587fa8d09db\n","repos":"inc-lc\/ilc-agda","old_file":"nats\/Extension-00.agda","new_file":"nats\/Extension-00.agda","new_contents":"{-\n\nChecklist of stuff to add when adding syntactic constructs\n\n- weaken\n- \u27e6_\u27e7Term\n- weaken-sound\n- derive (symbolic derivation)\n- validity-of-derive\n- correctness-of-derive\n\n\nThis file explores introducing folds over natural numbers\ninto the object language.\n\n-}\n\nmodule Extension-00 where\n\nopen import Data.Nat using\n ( \u2115\n ; suc\n ; fold\n ; _+_\n ; _\u2238_ -- x \u2238 y = max(0, x - y)\n )\n\nopen import Data.Product\nopen import Data.Unit using (\u22a4)\n\nopen import Relation.Binary.PropositionalEquality\n\nimport Level using (zero ; Level ; suc)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\npostulate extensionality : Extensionality Level.zero Level.zero\n\ndata Type : Set where\n nats : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n\u27e8\u2205\u27e9 : \u2205 \u2261 \u2205\n\u27e8\u2205\u27e9 = refl\n\n_\u27e8\u2022\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} \u2192 \u03c4\u2081 \u2261 \u03c4\u2082 \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u03c4\u2081 \u2022 \u0393\u2081 \u2261 \u03c4\u2082 \u2022 \u0393\u2082\n_\u27e8\u2022\u27e9_ = cong\u2082 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n foldNat : \u2200 {\u0393 \u03c4} \u2192 (z : Term \u0393 \u03c4) \u2192 (f : Term \u0393 (\u03c4 \u21d2 \u03c4))\n \u2192 (n : Term \u0393 nats)\n \u2192 Term \u0393 \u03c4\n _plus_ : \u2200 {\u0393} \u2192 (m : Term \u0393 nats) \u2192 (n : Term \u0393 nats) \u2192 Term \u0393 nats\n _minus_ : \u2200 {\u0393} \u2192 (m : Term \u0393 nats) \u2192 (n : Term \u0393 nats) \u2192 Term \u0393 nats\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u227c-reflexivity : Reflexive _\u227c_\n\u227c-reflexivity {\u2205} = \u2205\u227c\u2205\n\u227c-reflexivity {\u03c4 \u2022 x} = keep \u03c4 \u2022 \u227c-reflexivity\n\n\u227c-reflexive : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u0393\u2081 \u227c \u0393\u2082\n\u227c-reflexive refl = \u227c-reflexivity\n\n\u0393\u227c\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0393\n\u0393\u227c\u0393 = \u227c-reflexive refl\n\n\u227c-transitive : Transitive _\u227c_\n\u227c-transitive \u2205\u227c\u2205 rel1 = rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n keep \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (keep \u03c4 \u2022 rel0) rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n drop \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (drop \u03c4 \u2022 rel0) rel1\n\n\u227c-isPreorder : IsPreorder _\u2261_ _\u227c_\n\u227c-isPreorder = record\n { isEquivalence = isEquivalence\n ; reflexive = \u227c-reflexive\n ; trans = \u227c-transitive\n }\n\n\u227c-preorder : Preorder _ _ _\n\u227c-preorder = record\n { Carrier = Context\n ; _\u2248_ = _\u2261_\n ; isPreorder = \u227c-isPreorder\n }\n\nmodule \u227c-reasoning where\n open import Relation.Binary.PreorderReasoning \u227c-preorder public\n renaming\n ( _\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_\n ; _\u223c\u27e8_\u27e9_ to _\u227c\u27e8_\u27e9_\n ; _\u2248\u27e8\u27e9_ to _\u2261\u27e8\u27e9_\n )\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\n\nweaken subctx (foldNat z f n) = \n foldNat (weaken subctx z) (weaken subctx f) (weaken subctx n)\nweaken subctx (m plus n) = (weaken subctx m) plus (weaken subctx n)\nweaken subctx (m minus n) = (weaken subctx m) minus (weaken subctx n)\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 nats \u27e7Type = \u2115\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\n\u27e6 foldNat z f n \u27e7Term \u03c1 with \u27e6 n \u27e7Term \u03c1\n... | i = fold (\u27e6 z \u27e7Term \u03c1) (\u27e6 f \u27e7Term \u03c1) i\n\u27e6 m plus n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 m minus n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 \u2238 \u27e6 n \u27e7Term \u03c1\n\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = cong\u2082 (\u03bb x y \u2192 x y)\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = \u2261-app (weaken-sound t\u2081 \u03c1) (weaken-sound t\u2082 \u03c1)\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (foldNat n f z) \u03c1 = {!!}\nweaken-sound (m plus n) \u03c1 = {!!}\nweaken-sound (m minus n) \u03c1 = {!!}\n\n-- Changes to \u2115 are replacement Church pairs. The only arguments\n-- of conern are `fst` and `snd`, so the Church pairs don't have\n-- to be polymorphic.\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type nats = (nats \u21d2 nats \u21d2 nats) \u21d2 nats\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\nfst : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\nsnd : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\nfst = abs (abs (var (that this)))\nsnd = abs (abs (var this))\n\n-- Syntactic sugars\n-- * if0-then-else\n-- * change application\n-- * term difference\n-- * replacement pairs\n\ninfix 4 if0_then_else_\n\nif0_then_else_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 nats \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4\nif0 condition then then-branch else else-branch =\n foldNat then-branch (abs (weaken (drop _ \u2022 \u0393\u227c\u0393) else-branch)) condition\n\n_\u2295_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4) \u2192 Term \u0393 \u03c4\n_\u229d_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4)\n\nreplace_by_ : \u2200 {\u03c4 \u0393} \u2192 (old : Term \u0393 \u03c4) (new : Term \u0393 \u03c4)\n \u2192 Term \u0393 (\u0394-Type \u03c4)\n\n-- Term difference is replacement because symbolic\n-- derivation isn't avaliable at object level yet\ns \u229d t = replace s by t\n\n-- n \u2295 dn = dn snd\n-- f \u2295 df = \u03bb x . f x \u2295 df x (x \u229d x)\n--\n-- Replacing same by same is used to get the nil change,\n-- because symbolic derivation is not part of the object\n-- language.\n_\u2295_ {nats} t dt = app dt snd\n_\u2295_ {\u03c4\u2081 \u21d2 \u03c4\u2082} t dt = abs ((app t\u2032 x) \u2295 (app (app dt\u2032 x) (x \u229d x)))\n where\n drop-it = drop \u03c4\u2081 \u2022 \u0393\u227c\u0393\n x = var this\n t\u2032 = weaken drop-it t\n dt\u2032 = weaken drop-it dt\n\n-- replace n by m = \u03bb f . f n m\n-- replace f by g = \u03bb x . \u03bb dx . replace (f x) by (g (x \u2295 dx))\n--\n-- Amazingly, weakening is identical to arbitrary\n-- legal adjustment of de-Bruijn indices.\nreplace_by_ {nats} {\u0393} old new =\n abs (app (app (var this) (weaken drop-f old)) (weaken drop-f new))\n where drop-f = drop _ \u2022 \u0393\u227c\u0393\n-- Think: the new must compute upon changes...\nreplace_by_ {\u03c4\u2081 \u21d2 \u03c4\u2082} old new =\n abs (abs (replace (app (weaken drop! old) x)\n by (app (weaken drop! new) (x \u2295 dx))))\n where\n drop! = drop (\u0394-Type \u03c4\u2081) \u2022 drop \u03c4\u2081 \u2022 \u0393\u227c\u0393\n dx = var this\n x = var (that this)\n\n\n-- It is clear that \u27e6\u229d\u27e7 exists on the semantic level:\n-- there exists an Agda value to describe the change between any\n-- two Agda values denoted by terms. If we have (not dependently-\n-- typed) arrays, no term denotes the change between two arrays\n-- of different lengths. Thus no full abstraction.\n\n\u27e6derive\u27e7 : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ninfixl 6 _\u27e6\u2295\u27e7_\ninfixl 6 _\u27e6\u229d\u27e7_\n\n\u27e6fst\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e6snd\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\n\u27e6derive\u27e7 {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 f (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f v\n\u27e6derive\u27e7 {nats} n = \u03bb f \u2192 f n n\n\n_\u27e6\u229d\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 f\u2081 (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f\u2082 v\n_\u27e6\u229d\u27e7_ {nats} m n = \u03bb f \u2192 f n m\n-- m \u27e6\u229d\u27e7 n ::= replace n by m\n\n_\u27e6\u2295\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n_\u27e6\u2295\u27e7_ {nats} n dn = dn \u27e6snd\u27e7\n\n\u27e6fst\u27e7 m n = m\n\u27e6snd\u27e7 m n = n\n\n-- Strong validity!\ndata \u27e6Valid\u0394\u27e7 : {\u03c4 : Type} \u2192 (v : \u27e6 \u03c4 \u27e7) \u2192 (dv : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n -- Following Pierce's case names `T-Var`, `T-Abs`, `T-App`\n -- with Agda's capitalization convention\n -- generalized to the semantic (value) domain\n v-nat : (n : \u2115) \u2192 (dn : (\u2115 \u2192 \u2115 \u2192 \u2115) \u2192 \u2115) \u2192\n n \u2261 dn \u27e6fst\u27e7 \u2192\n \u27e6Valid\u0394\u27e7 n dn\n\n v-fun : {\u03c4\u2081 \u03c4\u2082 : Type} \u2192\n (f : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) \u2192 (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) \u2192\n -- A strong antecedent: f and df map invalid changes to\n -- valid changes! So long as invalid changes exist,\n -- this is NOT satisfied by\n --\n -- f = \u27e6 \u03bb x. x \u27e7 = identity\n -- df = \u27e6 \u03bb x dx. dx \u27e7 = \u27e6snd\u27e7,\n --\n -- negating any hope of \u27e6Valid\u0394\u27e7 \u27e6 t \u27e7 \u27e6 derive t \u27e7.\n (\u2200 {s ds} \u2192 {-Valid\u0394 s ds \u2192-} \u27e6Valid\u0394\u27e7 (f s) (df s ds)) \u2192\n (\u2200 {s ds} \u2192 (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 (f s \u27e6\u2295\u27e7 df s ds)) \u2192\n \u27e6Valid\u0394\u27e7 f df\n\nabsurdity-of-0=1 : 0 \u2261 1 \u2192 (\u2200 {A : Set} \u2192 A)\nabsurdity-of-0=1 ()\n\nit-is-absurd-that : \u27e6Valid\u0394\u27e7 {nats} 0 (\u03bb f \u2192 f 1 1) \u2192 (\u2200 {A : Set} \u2192 A)\nit-is-absurd-that (v-nat .0 .(\u03bb f \u2192 f 1 1) 0=1) = absurdity-of-0=1 0=1\n\n-- If (\u03bb x dx \u2192 dx) is a \u27e6Valid\u0394\u27e7 to (\u03bb x \u2192 x),\n-- then impossible is nothing.\nno-way : \u27e6Valid\u0394\u27e7 {nats \u21d2 nats} (\u03bb x \u2192 x) (\u03bb x dx \u2192 dx) \u2192\n (\u2200 {A : Set} \u2192 A)\nno-way (v-fun .(\u03bb x \u2192 x) .(\u03bb x dx \u2192 dx) validity correctness) =\n it-is-absurd-that (validity {0} {\u03bb f \u2192 f 1 1})\n\n-- Cool lemmas\n\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u229d\u27e7 f \u2261 \u27e6derive\u27e7 f\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {nats} f = refl\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = refl\n\nf\u2295[g\u229df]=g : \u2200 {\u03c4 : Type} (f g : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) \u2261 g\n\nf\u2295\u0394f=f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (\u27e6derive\u27e7 f) \u2261 f\n\nf\u2295[g\u229df]=g {nats} m n = refl\nf\u2295[g\u229df]=g {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = f x} {y = f x} refl\n (cong\u2082 _\u27e6\u229d\u27e7_ (cong g (f\u2295\u0394f=f x)) refl) \u27e9\n f x \u27e6\u2295\u27e7 (g x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (g x) \u27e9\n g x\n \u220e\n ) where open \u2261-Reasoning\n\nf\u2295\u0394f=f {nats} n = refl\nf\u2295\u0394f=f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 \u27e6derive\u27e7 f) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (f (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong (\u03bb hole \u2192 f x \u27e6\u2295\u27e7 (f hole \u27e6\u229d\u27e7 f x)) (f\u2295\u0394f=f x) \u27e9\n f x \u27e6\u2295\u27e7 (f x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (f x) \u27e9\n f x\n \u220e\n ) where open \u2261-Reasoning\n\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {nats} n dn = n \u2261 dn \u27e6fst\u27e7\nvalid-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (s : \u27e6 \u03c4\u2081 \u27e7) (ds : \u27e6 \u0394-Type \u03c4\u2081 \u27e7) (R[s,ds] : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7 -- (valid-\u0394:1)\n (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 f s \u27e6\u2295\u27e7 df s ds -- (valid-\u0394:2)\n\nR[f,g\u229df] : \u2200 {\u03c4} (f g : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (g \u27e6\u229d\u27e7 f)\nR[f,g\u229df] {nats} m n = refl\nR[f,g\u229df] {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb x dx R[x,dx] \u2192\n R[f,g\u229df] {\u03c4\u2082} (f x) (g (x \u27e6\u2295\u27e7 dx)) -- (valid-\u0394:1)\n , -- tuple constructor\n (begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 refl \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7\n (g ((x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 \u27e6derive\u27e7 (x \u27e6\u2295\u27e7 dx)) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 cong (\u03bb hole \u2192 f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g hole \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx)))\n (f\u2295\u0394f=f (x \u27e6\u2295\u27e7 dx)) \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 f\u2295[g\u229df]=g (f (x \u27e6\u2295\u27e7 dx)) (g (x \u27e6\u2295\u27e7 dx)) \u27e9\n g (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (f x) (g (x \u27e6\u2295\u27e7 dx))) \u27e9\n f x \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) x dx\n \u220e)\n where open \u2261-Reasoning\n\nR[f,\u0394f] : \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (\u27e6derive\u27e7 f)\nR[f,\u0394f] f rewrite sym (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f f) = R[f,g\u229df] f f\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393 -- push \u03c4, then push \u0394\u03c4.\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 (keep \u03c4 \u2022 \u0393\u227c\u0394\u0393)\n\n-- Data type to hold proofs that environments are valid\ndata Valid-\u0394env : {\u0393 : Context} (\u03c1 : \u27e6 \u0393 \u27e7) (\u0394\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is its own valid \u0394-environment.\n \u03c1=\u2205 : Valid-\u0394env {\u2205} \u2205 \u2205\n -- Induction case: the change introduced therein should be valid,\n -- and the smaller \u0394-environment should be valid as well.\n \u03c1=v\u2022\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {\u03c1\u2080 : \u27e6 \u0393\u2080 \u27e7} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Valid-\u0394env \u03c1\u2080 d\u03c1\u2080 \u2192\n Valid-\u0394env {\u03c4 \u2022 \u0393\u2080} (v \u2022 \u03c1\u2080) (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- Data type to hold proofs that a \u0394-env is consistent with self\ndata Consistent-\u0394env : {\u0393 : Context} (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is consistent with itself\n d\u03c1=\u2205 : Consistent-\u0394env {\u2205} \u2205\n -- Induction case: the change introduced at top level\n -- should be valid.\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Consistent-\u0394env d\u03c1\u2080 \u2192\n Consistent-\u0394env {\u03c4 \u2022 \u0393\u2080} (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- If a \u0394-environment is valid for some other environment,\n-- then it is also consistent with itself.\n\nvalid-\u0394env-is-consistent :\n \u2200 {\u0393 : Context} {\u03c1 : \u27e6 \u0393 \u27e7} {d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Valid-\u0394env \u03c1 d\u03c1 \u2192 Consistent-\u0394env d\u03c1\n\nvalid-\u0394env-is-consistent \u03c1=\u2205 = d\u03c1=\u2205\nvalid-\u0394env-is-consistent (\u03c1=v\u2022\u03c1\u2080 valid[v,dv] valid[\u03c1\u2080,d\u03c1\u2080]) =\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] (valid-\u0394env-is-consistent valid[\u03c1\u2080,d\u03c1\u2080])\n\n-- finally, update and ignore\n\nupdate : \u2200 {\u0393} \u2192 (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env d\u03c1} \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 {d\u03c1=\u2205} = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 d\u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] consistent[d\u03c1\u2080]} =\n (v \u27e6\u2295\u27e7 dv) \u2022 update d\u03c1 {consistent[d\u03c1\u2080]}\n\n-- Ignorance is bliss (don't have to pass consistency proofs around :D)\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 -- Using a proof to describe computation\n\n-- Naming scheme follows weakenVar\/weaken\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- derive(n) = \u03bbf. f n n\nderive (nat n) = abs (app (app (var this) (nat n)) (nat n))\n\n-- derive(x) = dx\nderive (var x) = var (deriveVar x)\n\n-- derive(\u03bbx. t) = \u03bbx. \u03bbdx. derive(t)\nderive (abs t) = abs (abs (derive t))\n\n-- derive(f s) = derive(f) s derive(s)\nderive (app f s) = app (app (derive f) (weaken \u0393\u227c\u0394\u0393 s)) (derive s)\n\n-- derive (foldNat z f n) =\n-- let\n-- dz = derive z\n-- df = derive f\n-- dn = derive n\n-- n\u2032 = dn snd\n-- in\n-- if (n = n\u2032) -- if0 (n\u2032 \u2238 n) + (n \u2238 n\u2032)\n-- core\u2082\n-- if (n < n\u2032) -- else if0 (n - n\u2032)\n-- (replace (\u03bb x . x) by (\u03bb x . foldNat x f (n\u2032 \u2238 n))) core\u2081 core\u2082\n-- if (n > n\u2032) -- else\n-- (replace (\u03bb x . foldNat x f (n \u2238 n\u2032)) by (\u03bb x . x)) core\u2081 core\u2082\n-- where\n-- min[n,n\u2032] = if (n < n\u2032) then n else n\u2032\n-- -- core is a Church pair (core\u2081, core\u2082)\n-- -- where core\u2081 is the result of applying f to z min[n,n\u2032] times\n-- -- and core\u2082 is the change to core\u2081 caused by changes to f and z.\n-- core = foldNat (\u03bb g . g z \u0394z)\n-- (\u03bb p . \u03bb g . g (f (p fst)) (df (p fst) (p snd)))\n-- min[n,n\u2032]\n-- core\u2081 = core fst\n-- core\u2082 = core snd\nderive (foldNat z f n) = {!!}\n\n-- derive (m plus n) = ?\nderive (m plus n) = {!!}\n\n-- derive (m minus n) = ?\nderive (m minus n) = {!!}\n\n-- Extensional equivalence for changes\ndata as-\u0394_is_ext-equiv-to_ :\n \u2200 (\u03c4 : Type) \u2192 (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 (dg : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n ext-\u0394 : \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n (\u2200 (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192\n (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)) \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg\n\ninfix 3 as-\u0394_is_ext-equiv-to_\n\n-- Extractor for extensional-equivalence-as-changes:\n-- Given a value of the data type holding the proof,\n-- returns the proof in applicable form.\n--\n-- It would not be necessary if such\n-- proof-holding types were defined as a function in\n-- the first place, say in the manner of `valid-\u0394`.\n--\nextract-\u0394equiv :\n \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg \u2192\n (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192 (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)\n\nextract-\u0394equiv (ext-\u0394 proof-method) = proof-method\n\n-- Distribution lemmas of validity over \u27e6\u229d\u27e7 and \u27e6\u2295\u27e7\n\napplication-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v) \u2261 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\napplication-over-\u2295-and-\u229d f g df v =\n begin\n f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (cong\u2082 _\u27e6\u2295\u27e7_\n (cong f ((sym (f\u2295\u0394f=f v))))\n (cong\u2082 df\n (sym (f\u2295\u0394f=f v))\n (cong \u27e6derive\u27e7 (sym (f\u2295\u0394f=f v)))))\n refl \u27e9\n f (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v)\n \u27e6\u2295\u27e7 df (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v) (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v))\n \u27e6\u229d\u27e7 f v\n \u2261\u27e8 refl \u27e9\n (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e where open \u2261-Reasoning\n\nvalidity-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n valid-\u0394 g (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) \u2192\n valid-\u0394 (g v) (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\nvalidity-over-\u2295-and-\u229d f g df v R[g,f\u2295df]\n rewrite application-over-\u2295-and-\u229d f g df v =\n proj\u2081 (R[g,f\u2295df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))\n\n-- diff-apply\ndf=f\u2295df\u229df :\n \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n valid-\u0394 f df \u2192 as-\u0394 \u03c4 is df ext-equiv-to f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f\n\n-- Case nat: this REFL is more obvious to Agda than to a human.\ndf=f\u2295df\u229df {nats} n dn valid-f-df =\n ext-\u0394 (\u03bb m valid-m-dn valid-rhs \u2192 refl)\n\ndf=f\u2295df\u229df {\u03c4\u2081 \u21d2 \u03c4\u2082} f df R[f,df] = ext-\u0394 (\n \u03bb g R[g,df] R[g,f\u2295df\u229df] \u2192\n extensionality (\u03bb v \u2192\n begin\n g v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n \u2261\u27e8 extract-\u0394equiv\n (df=f\u2295df\u229df\n (f v) (df v (\u27e6derive\u27e7 v))\n (proj\u2081 (R[f,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))))\n (g v)\n (proj\u2081 (R[g,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v)))\n (validity-over-\u2295-and-\u229d f g df v R[g,f\u2295df\u229df]) \u27e9\n g v \u27e6\u2295\u27e7 (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = g v} {y = g v} refl\n (application-over-\u2295-and-\u229d f g df v) \u27e9\n g v \u27e6\u2295\u27e7 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e\n )\n )\n where open \u2261-Reasoning\n\n\n\ncorrectness-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 deriveVar x \u27e7 \u03c1\n ext-equiv-to\n \u27e6 x \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 x \u27e7 (ignore \u03c1)\n\ncorrectness-of-deriveVar {\u03c4 \u2022 \u0393\u2080} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n this = df=f\u2295df\u229df {\u03c4} v dv R[v,dv]\n\ncorrectness-of-deriveVar {\u03c4\u2080 \u2022 \u0393\u2080} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4\u2080} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n (that x) = correctness-of-deriveVar \u03c1 x\n\n\n\ncorrectness-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 derive t \u27e7 \u03c1\n ext-equiv-to\n \u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 t \u27e7 (ignore \u03c1)\n\n-- Mutually recursive lemma: derivatives are valid\nvalidity-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 deriveVar x \u27e7 \u03c1)\n\nvalidity-of-deriveVar {\u03c4 \u2022 \u0393} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n this = R[v,dv]\n\nvalidity-of-deriveVar {\u03c4\u2080 \u2022 \u0393} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n (that x) = validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 {consistency} (var x) =\n validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 (nat n) = refl\n\nvalidity-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs t) = \u03bb v dv R[v,dv] \u2192\n validity-of-derive (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency} t\n , -- tuple constructor\n (begin\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n ((R[f,g\u229df] (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 f\u2295[g\u229df]=g (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u2261\u27e8 cong \u27e6 t \u27e7 (cong\u2082 _\u2022_ (f\u2295\u0394f=f (v \u27e6\u2295\u27e7 dv)) refl) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency}))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 sym (extract-\u0394equiv\n (correctness-of-derive\n ((dv \u2022 v \u2022 \u03c1))\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (R[f,g\u229df] (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u220e)\n where open \u2261-Reasoning\n\nvalidity-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} t\u2081 t\u2082)\n = R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7]\n where\n open \u2261-Reasoning\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 : \u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1) \u2261 v\u2081 v\u2082\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 rewrite (sym v\u2082=old-v\u2082) = refl\n\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 : \u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1 \u2261 dv\u2081 v\u2082 dv\u2082\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = refl\n\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] : valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (dv\u2081 v\u2082 dv\u2082)\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] rewrite \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 = R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n -- What I want to write:\n {-\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] rewrite \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n -}\n\n -- What I have to write:\n\n R : {\u03c4 : Type} \u2192 {v : \u27e6 \u03c4 \u27e7} \u2192 {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 valid-\u0394 v dv\u2081 \u2192 valid-\u0394 v dv\u2082\n\n R {nats} dv\u2081=dv\u2082 refl = cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl\n\n --R {\u03c4\u2081 \u21d2 \u03c4\u2082} dv\u2081=dv\u2082 valid-dv\u2081 rewrite dv\u2081=dv\u2082 = {!valid-dv\u2081!}\n R {\u03c4\u2081 \u21d2 \u03c4\u2082} {v} {dv\u2081} {dv\u2082} dv\u2081=dv\u2082 valid-dv\u2081 =\n \u03bb s ds R[s,ds] \u2192\n R {\u03c4\u2082} {v s} {dv\u2081 s ds} {dv\u2082 s ds}\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl)\n (proj\u2081 (valid-dv\u2081 s ds R[s,ds]))\n ,\n (begin\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2082 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v (s \u27e6\u2295\u27e7 ds)} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) (sym dv\u2081=dv\u2082) refl) refl) \u27e9\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2081 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 sym (proj\u2082 (valid-dv\u2081\n (s \u27e6\u2295\u27e7 ds)\n (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n (R[f,\u0394f] (s \u27e6\u2295\u27e7 ds)))) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds \u27e6\u2295\u27e7 \u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong (v \u27e6\u2295\u27e7 dv\u2081) (f\u2295\u0394f=f (s \u27e6\u2295\u27e7 ds)) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds)\n \u2261\u27e8 proj\u2082 (valid-dv\u2081 s ds R[s,ds]) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2081 s ds\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v s} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2082 s ds\n \u220e) where open \u2261-Reasoning\n\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] = R \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n\nvalidity-of-derive \u03c1 {consistency} (foldNat z f n) = {!!}\nvalidity-of-derive \u03c1 {consistency} (m plus n) = {!!}\nvalidity-of-derive \u03c1 {consistency} (m minus n) = {!!}\n\n\ncorrectness-of-derive \u03c1 (var x) = correctness-of-deriveVar \u03c1 x\n\ncorrectness-of-derive \u03c1 (nat n) = ext-\u0394 (\u03bb _ _ _ \u2192 refl)\n\ncorrectness-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs {.\u03c4\u2081} {.\u03c4\u2082} t) =\n ext-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n extensionality {\u27e6 \u03c4\u2081 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4\u2082 \u27e7} (\u03bb x \u2192\n begin\n f x \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive {\u03c4\u2081 \u2022 \u0393} {\u03c4\u2082}\n (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency}\n t)\n (f x)\n (proj\u2081 (R[f,\u0394t] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x)))\n (proj\u2081 (R[f,t\u2032\u229dt] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x))) \u27e9\n f x\n \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 (update (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency})\n \u27e6\u229d\u27e7 \u27e6 t \u27e7 (x \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u220e\n )) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} {\u0393} t\u2081 t\u2082) =\n ext-\u0394 {\u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n begin\n f \u27e6\u2295\u27e7 \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n \u2261\u27e8 extract-\u0394equiv ext-\u0394[t\u2081t\u2082] f R[f,\u0394t] R[f,t\u2032\u229dt] \u27e9\n f \u27e6\u2295\u27e7\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n \u220e)\n where\n open \u2261-Reasoning\n\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n v\u2081\u2295dv\u2081=v\u2081\u2032 : v\u2081 \u27e6\u2295\u27e7 dv\u2081 \u2261 v\u2081\u2032\n v\u2081\u2295dv\u2081=v\u2081\u2032 =\n begin\n v\u2081 \u27e6\u2295\u27e7 dv\u2081\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2081)\n v\u2081 valid-dv\u2081 (R[f,g\u229df] v\u2081 v\u2081\u2032) \u27e9\n v\u2081 \u27e6\u2295\u27e7 (v\u2081\u2032 \u27e6\u229d\u27e7 v\u2081)\n \u2261\u27e8 f\u2295[g\u229df]=g v\u2081 v\u2081\u2032 \u27e9\n v\u2081\u2032\n \u220e\n\n -- TODO: remove code duplication.\n v\u2082\u2295dv\u2082=v\u2082\u2032 : v\u2082 \u27e6\u2295\u27e7 dv\u2082 \u2261 v\u2082\u2032\n v\u2082\u2295dv\u2082=v\u2082\u2032 rewrite v\u2082=old-v\u2082 =\n begin\n old-v\u2082 \u27e6\u2295\u27e7 dv\u2082\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2082)\n old-v\u2082\n (validity-of-derive \u03c1 {consistency} t\u2082)\n (R[f,g\u229df] old-v\u2082 v\u2082\u2032) \u27e9\n old-v\u2082 \u27e6\u2295\u27e7 (v\u2082\u2032 \u27e6\u229d\u27e7 old-v\u2082)\n \u2261\u27e8 f\u2295[g\u229df]=g old-v\u2082 v\u2082\u2032 \u27e9\n v\u2082\u2032\n \u220e\n where old-v\u2082 = \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] : v\u2081\u2032 v\u2082\u2032 \u2261 (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082)\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] = sym (cong\u2082 (\u03bb f x \u2192 f x) v\u2081\u2295dv\u2081=v\u2081\u2032 v\u2082\u2295dv\u2082=v\u2082\u2032)\n\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 :\n (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082) \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 = proj\u2082 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082 \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n cong\u2082 _\u27e6\u229d\u27e7_\n (trans v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082)\n refl\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n as-\u0394 \u03c4\u2082 is dv\u2081 v\u2082 dv\u2082 ext-equiv-to v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n df=f\u2295df\u229df (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082) R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 : as-\u0394 \u03c4\u2082 is dv\u2081 v\u2082 dv\u2082 ext-equiv-to v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 rewrite v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082\n\n ext-\u0394[t\u2081t\u2082] :\n as-\u0394 \u03c4\u2082 is\n \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n ext-equiv-to\n \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency}) (\u27e6 t\u2082 \u27e7 (update \u03c1 {consistency}))\n \u27e6\u229d\u27e7 \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n ext-\u0394[t\u2081t\u2082] rewrite sym v\u2082=old-v\u2082 = dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082\n\ncorrectness-of-derive {\u0393} {\u03c4}\n \u03c1 {consistency} (foldNat z f n) = {!!}\n\ncorrectness-of-derive {\u0393} {nats}\n \u03c1 {consistency} (m plus n) = {!!}\n\ncorrectness-of-derive {\u0393} {nats}\n \u03c1 {consistency} (m minus n) = {!!}\n\ncorrectness-on-closed-terms : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192\n \u2200 (f : Term \u2205 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192\n \u2200 (s : Term \u2205 \u03c4\u2081) (ds : Term \u2205 (\u0394-Type \u03c4\u2081))\n {R[v,dv] : valid-\u0394 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)} \u2192\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205 \u27e6\u2295\u27e7 \u27e6 ds \u27e7 \u2205)\n \u2261\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)\n\ncorrectness-on-closed-terms {\u03c4\u2081} {\u03c4\u2082} f s ds {R[v,dv]} =\n begin\n h (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (f\u2295[g\u229df]=g h h))\n refl \u27e9\n (h \u27e6\u2295\u27e7 (h \u27e6\u229d\u27e7 h)) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (extract-\u0394equiv\n (correctness-of-derive \u2205 f)\n h\n (validity-of-derive \u2205 {d\u03c1=\u2205} f)\n (R[f,g\u229df] h h)))\n refl \u27e9\n (h \u27e6\u2295\u27e7 \u0394h) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 proj\u2082 (validity-of-derive \u2205 {d\u03c1=\u2205} f v dv R[v,dv]) \u27e9\n h v \u27e6\u2295\u27e7 \u0394h v dv\n \u220e\n where\n open \u2261-Reasoning\n h : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n h = \u27e6 f \u27e7 \u2205\n \u0394h : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n \u0394h = \u27e6 derive f \u27e7 \u2205\n v : \u27e6 \u03c4\u2081 \u27e7\n v = \u27e6 s \u27e7 \u2205\n dv : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv = \u27e6 ds \u27e7 \u2205\n\n","old_contents":"{-\n\nChecklist of stuff to add when adding syntactic constructs\n\n- derive (symbolic derivation; most important!)\n- weaken\n- \u27e6_\u27e7Term\n- weaken-sound\n- validity-of-derive\n- correctness-of-derive\n\n-}\n\nmodule Extension-00 where\n\nopen import Data.Product\nopen import Data.Nat using (\u2115)\nopen import Data.Unit using (\u22a4)\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Level using\n (zero ; Level ; suc)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\npostulate extensionality : Extensionality zero zero\n\ndata Type : Set where\n nats : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n\u27e8\u2205\u27e9 : \u2205 \u2261 \u2205\n\u27e8\u2205\u27e9 = refl\n\n_\u27e8\u2022\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} \u2192 \u03c4\u2081 \u2261 \u03c4\u2082 \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u03c4\u2081 \u2022 \u0393\u2081 \u2261 \u03c4\u2082 \u2022 \u0393\u2082\n_\u27e8\u2022\u27e9_ = cong\u2082 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n -- ad-hoc extensions\n foldNat : \u2200 {\u0393 \u03c4} \u2192 (n : Term \u0393 nats) \u2192\n (f : Term \u0393 (\u03c4 \u21d2 \u03c4)) \u2192 (z : Term \u0393 \u03c4)\n \u2192 Term \u0393 \u03c4\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u227c-reflexivity : Reflexive _\u227c_\n\u227c-reflexivity {\u2205} = \u2205\u227c\u2205\n\u227c-reflexivity {\u03c4 \u2022 x} = keep \u03c4 \u2022 \u227c-reflexivity\n\n\u227c-reflexive : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u0393\u2081 \u227c \u0393\u2082\n\u227c-reflexive refl = \u227c-reflexivity\n\n\u0393\u227c\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0393\n\u0393\u227c\u0393 = \u227c-reflexive refl\n\n\u227c-transitive : Transitive _\u227c_\n\u227c-transitive \u2205\u227c\u2205 rel1 = rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n keep \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (keep \u03c4 \u2022 rel0) rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n drop \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (drop \u03c4 \u2022 rel0) rel1\n\n\u227c-isPreorder : IsPreorder _\u2261_ _\u227c_\n\u227c-isPreorder = record\n { isEquivalence = isEquivalence\n ; reflexive = \u227c-reflexive\n ; trans = \u227c-transitive\n }\n\n\u227c-preorder : Preorder _ _ _\n\u227c-preorder = record\n { Carrier = Context\n ; _\u2248_ = _\u2261_\n ; isPreorder = \u227c-isPreorder\n }\n\nmodule \u227c-reasoning where\n open import Relation.Binary.PreorderReasoning \u227c-preorder public\n renaming\n ( _\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_\n ; _\u223c\u27e8_\u27e9_ to _\u227c\u27e8_\u27e9_\n ; _\u2248\u27e8\u27e9_ to _\u2261\u27e8\u27e9_\n )\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\n\nweaken subctx (foldNat n f z) = {!!}\n\nrecord Meaning (Syntax : Set) {\u2113 : Level} : Set (suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 nats \u27e7Type = \u2115\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\n\u27e6 foldNat n f z \u27e7Term \u03c1 = {!!}\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = cong\u2082 (\u03bb x y \u2192 x y)\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = \u2261-app (weaken-sound t\u2081 \u03c1) (weaken-sound t\u2082 \u03c1)\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (foldNat n f z) \u03c1 = {!!}\n\n-- Changes to \u2115 are replacement Church pairs. The only arguments\n-- of conern are `fst` and `snd`, so the Church pairs don't have\n-- to be polymorphic.\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type nats = (nats \u21d2 nats \u21d2 nats) \u21d2 nats\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\n-- Combination as type-indexed family of terms\n-- _\u2295_ : \u2200 {\u03c4 \u0393} \u2192 TODO: IMPLEMENT ME!! {!!}\n\n-- Replacement-pairs on all types as syntactic sugar\n\nreplace_by_ : \u2200 {\u03c4 \u0393} \u2192 (old : Term \u0393 \u03c4) (new : Term \u0393 \u03c4)\n \u2192 Term \u0393 (\u0394-Type \u03c4)\n\n-- replace n by m = \u03bb f . f n m\n-- replace f by g = \u03bb x . \u03bb dx . replace (f x) by (g (x \u2295 dx))\n--\n-- Remark. Amazingly, weakening is identical to arbitrary\n-- legal adjustment of de-Bruijn indices.\nreplace_by_ {nats} {\u0393} old new =\n abs (app (app (var this) (weaken drop-f old)) (weaken drop-f new))\n where drop-f = drop _ \u2022 \u0393\u227c\u0393\n-- Think: the new must compute upon changes...\nreplace_by_ {\u03c4\u2081 \u21d2 \u03c4\u2082} old new =\n abs (abs (replace (app (weaken drop! old) (var (that this)))\n by (app (weaken drop! new) {!!})))\n where drop! = drop (\u0394-Type \u03c4\u2081) \u2022 drop \u03c4\u2081 \u2022 \u0393\u227c\u0393\n\n\n-- It is clear that \u27e6\u229d\u27e7 exists on the semantic level:\n-- there exists an Agda value to describe the change between any\n-- two Agda values denoted by terms. If we have (not dependently-\n-- typed) arrays, no term denotes the change between two arrays\n-- of different lengths. Thus no full abstraction.\n\n\u27e6derive\u27e7 : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ninfixl 6 _\u27e6\u2295\u27e7_\ninfixl 6 _\u27e6\u229d\u27e7_\n\n\u27e6fst\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e6snd\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\n\u27e6derive\u27e7 {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 f (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f v\n\u27e6derive\u27e7 {nats} n = \u03bb f \u2192 f n n\n\n_\u27e6\u229d\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 f\u2081 (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f\u2082 v\n_\u27e6\u229d\u27e7_ {nats} m n = \u03bb f \u2192 f n m\n-- m \u27e6\u229d\u27e7 n ::= replace n by m\n\n_\u27e6\u2295\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n_\u27e6\u2295\u27e7_ {nats} n dn = dn \u27e6snd\u27e7\n\n\u27e6fst\u27e7 m n = m\n\u27e6snd\u27e7 m n = n\n\n-- Strong validity!\ndata \u27e6Valid\u0394\u27e7 : {\u03c4 : Type} \u2192 (v : \u27e6 \u03c4 \u27e7) \u2192 (dv : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n -- Following Pierce's case names `T-Var`, `T-Abs`, `T-App`\n -- with Agda's capitalization convention\n -- generalized to the semantic (value) domain\n v-nat : (n : \u2115) \u2192 (dn : (\u2115 \u2192 \u2115 \u2192 \u2115) \u2192 \u2115) \u2192\n n \u2261 dn \u27e6fst\u27e7 \u2192\n \u27e6Valid\u0394\u27e7 n dn\n\n v-fun : {\u03c4\u2081 \u03c4\u2082 : Type} \u2192\n (f : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) \u2192 (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) \u2192\n -- A strong antecedent: f and df map invalid changes to\n -- valid changes! So long as invalid changes exist,\n -- this is NOT satisfied by\n --\n -- f = \u27e6 \u03bb x. x \u27e7 = identity\n -- df = \u27e6 \u03bb x dx. dx \u27e7 = \u27e6snd\u27e7,\n --\n -- negating any hope of \u27e6Valid\u0394\u27e7 \u27e6 t \u27e7 \u27e6 derive t \u27e7.\n (\u2200 {s ds} \u2192 {-Valid\u0394 s ds \u2192-} \u27e6Valid\u0394\u27e7 (f s) (df s ds)) \u2192\n (\u2200 {s ds} \u2192 (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 (f s \u27e6\u2295\u27e7 df s ds)) \u2192\n \u27e6Valid\u0394\u27e7 f df\n\nabsurdity-of-0=1 : 0 \u2261 1 \u2192 (\u2200 {A : Set} \u2192 A)\nabsurdity-of-0=1 ()\n\nit-is-absurd-that : \u27e6Valid\u0394\u27e7 {nats} 0 (\u03bb f \u2192 f 1 1) \u2192 (\u2200 {A : Set} \u2192 A)\nit-is-absurd-that (v-nat .0 .(\u03bb f \u2192 f 1 1) 0=1) = absurdity-of-0=1 0=1\n\n-- If (\u03bb x dx \u2192 dx) is a \u27e6Valid\u0394\u27e7 to (\u03bb x \u2192 x),\n-- then impossible is nothing.\nno-way : \u27e6Valid\u0394\u27e7 {nats \u21d2 nats} (\u03bb x \u2192 x) (\u03bb x dx \u2192 dx) \u2192\n (\u2200 {A : Set} \u2192 A)\nno-way (v-fun .(\u03bb x \u2192 x) .(\u03bb x dx \u2192 dx) validity correctness) =\n it-is-absurd-that (validity {0} {\u03bb f \u2192 f 1 1})\n\n-- Cool lemmas\n\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u229d\u27e7 f \u2261 \u27e6derive\u27e7 f\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {nats} f = refl\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = refl\n\nf\u2295[g\u229df]=g : \u2200 {\u03c4 : Type} (f g : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) \u2261 g\n\nf\u2295\u0394f=f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (\u27e6derive\u27e7 f) \u2261 f\n\nf\u2295[g\u229df]=g {nats} m n = refl\nf\u2295[g\u229df]=g {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = f x} {y = f x} refl\n (cong\u2082 _\u27e6\u229d\u27e7_ (cong g (f\u2295\u0394f=f x)) refl) \u27e9\n f x \u27e6\u2295\u27e7 (g x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (g x) \u27e9\n g x\n \u220e\n ) where open \u2261-Reasoning\n\nf\u2295\u0394f=f {nats} n = refl\nf\u2295\u0394f=f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 \u27e6derive\u27e7 f) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (f (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong (\u03bb hole \u2192 f x \u27e6\u2295\u27e7 (f hole \u27e6\u229d\u27e7 f x)) (f\u2295\u0394f=f x) \u27e9\n f x \u27e6\u2295\u27e7 (f x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (f x) \u27e9\n f x\n \u220e\n ) where open \u2261-Reasoning\n\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {nats} n dn = n \u2261 dn \u27e6fst\u27e7\nvalid-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (s : \u27e6 \u03c4\u2081 \u27e7) (ds : \u27e6 \u0394-Type \u03c4\u2081 \u27e7) (R[s,ds] : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7 -- (valid-\u0394:1)\n (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 f s \u27e6\u2295\u27e7 df s ds -- (valid-\u0394:2)\n\nR[f,g\u229df] : \u2200 {\u03c4} (f g : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (g \u27e6\u229d\u27e7 f)\nR[f,g\u229df] {nats} m n = refl\nR[f,g\u229df] {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb x dx R[x,dx] \u2192\n R[f,g\u229df] {\u03c4\u2082} (f x) (g (x \u27e6\u2295\u27e7 dx)) -- (valid-\u0394:1)\n , -- tuple constructor\n (begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 refl \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7\n (g ((x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 \u27e6derive\u27e7 (x \u27e6\u2295\u27e7 dx)) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 cong (\u03bb hole \u2192 f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g hole \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx)))\n (f\u2295\u0394f=f (x \u27e6\u2295\u27e7 dx)) \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 f\u2295[g\u229df]=g (f (x \u27e6\u2295\u27e7 dx)) (g (x \u27e6\u2295\u27e7 dx)) \u27e9\n g (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (f x) (g (x \u27e6\u2295\u27e7 dx))) \u27e9\n f x \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) x dx\n \u220e)\n where open \u2261-Reasoning\n\nR[f,\u0394f] : \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (\u27e6derive\u27e7 f)\nR[f,\u0394f] f rewrite sym (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f f) = R[f,g\u229df] f f\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393 -- push \u03c4, then push \u0394\u03c4.\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 (keep \u03c4 \u2022 \u0393\u227c\u0394\u0393)\n\n-- Data type to hold proofs that environments are valid\ndata Valid-\u0394env : {\u0393 : Context} (\u03c1 : \u27e6 \u0393 \u27e7) (\u0394\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is its own valid \u0394-environment.\n \u03c1=\u2205 : Valid-\u0394env {\u2205} \u2205 \u2205\n -- Induction case: the change introduced therein should be valid,\n -- and the smaller \u0394-environment should be valid as well.\n \u03c1=v\u2022\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {\u03c1\u2080 : \u27e6 \u0393\u2080 \u27e7} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Valid-\u0394env \u03c1\u2080 d\u03c1\u2080 \u2192\n Valid-\u0394env {\u03c4 \u2022 \u0393\u2080} (v \u2022 \u03c1\u2080) (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- Data type to hold proofs that a \u0394-env is consistent with self\ndata Consistent-\u0394env : {\u0393 : Context} (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is consistent with itself\n d\u03c1=\u2205 : Consistent-\u0394env {\u2205} \u2205\n -- Induction case: the change introduced at top level\n -- should be valid.\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Consistent-\u0394env d\u03c1\u2080 \u2192\n Consistent-\u0394env {\u03c4 \u2022 \u0393\u2080} (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- If a \u0394-environment is valid for some other environment,\n-- then it is also consistent with itself.\n\nvalid-\u0394env-is-consistent :\n \u2200 {\u0393 : Context} {\u03c1 : \u27e6 \u0393 \u27e7} {d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Valid-\u0394env \u03c1 d\u03c1 \u2192 Consistent-\u0394env d\u03c1\n\nvalid-\u0394env-is-consistent \u03c1=\u2205 = d\u03c1=\u2205\nvalid-\u0394env-is-consistent (\u03c1=v\u2022\u03c1\u2080 valid[v,dv] valid[\u03c1\u2080,d\u03c1\u2080]) =\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] (valid-\u0394env-is-consistent valid[\u03c1\u2080,d\u03c1\u2080])\n\n-- finally, update and ignore\n\nupdate : \u2200 {\u0393} \u2192 (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env d\u03c1} \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 {d\u03c1=\u2205} = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 d\u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] consistent[d\u03c1\u2080]} =\n (v \u27e6\u2295\u27e7 dv) \u2022 update d\u03c1 {consistent[d\u03c1\u2080]}\n\n-- Ignorance is bliss (don't have to pass consistency proofs around :D)\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 -- Using a proof to describe computation\n\n-- Naming scheme follows weakenVar\/weaken\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- derive(n) = \u03bbf. f n n\nderive (nat n) = abs (app (app (var this) (nat n)) (nat n))\n\n-- derive(x) = dx\nderive (var x) = var (deriveVar x)\n\n-- derive(\u03bbx. t) = \u03bbx. \u03bbdx. derive(t)\nderive (abs t) = abs (abs (derive t))\n\n-- derive(f s) = derive(f) s derive(s)\nderive (app f s) = app (app (derive f) (weaken \u0393\u227c\u0394\u0393 s)) (derive s)\n\n-- derive (foldNat n f z) = ?\nderive (foldNat n f z) = {!!}\n\n-- Extensional equivalence for changes\ndata as-\u0394_is_ext-equiv-to_ :\n \u2200 (\u03c4 : Type) \u2192 (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 (dg : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n ext-\u0394 : \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n (\u2200 (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192\n (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)) \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg\n\ninfix 3 as-\u0394_is_ext-equiv-to_\n\n-- Extractor for extensional-equivalence-as-changes:\n-- Given a value of the data type holding the proof,\n-- returns the proof in applicable form.\n--\n-- It would not be necessary if such\n-- proof-holding types were defined as a function in\n-- the first place, say in the manner of `valid-\u0394`.\n--\nextract-\u0394equiv :\n \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg \u2192\n (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192 (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)\n\nextract-\u0394equiv (ext-\u0394 proof-method) = proof-method\n\n-- Distribution lemmas of validity over \u27e6\u229d\u27e7 and \u27e6\u2295\u27e7\n\napplication-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v) \u2261 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\napplication-over-\u2295-and-\u229d f g df v =\n begin\n f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (cong\u2082 _\u27e6\u2295\u27e7_\n (cong f ((sym (f\u2295\u0394f=f v))))\n (cong\u2082 df\n (sym (f\u2295\u0394f=f v))\n (cong \u27e6derive\u27e7 (sym (f\u2295\u0394f=f v)))))\n refl \u27e9\n f (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v)\n \u27e6\u2295\u27e7 df (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v) (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v))\n \u27e6\u229d\u27e7 f v\n \u2261\u27e8 refl \u27e9\n (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e where open \u2261-Reasoning\n\nvalidity-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n valid-\u0394 g (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) \u2192\n valid-\u0394 (g v) (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\nvalidity-over-\u2295-and-\u229d f g df v R[g,f\u2295df]\n rewrite application-over-\u2295-and-\u229d f g df v =\n proj\u2081 (R[g,f\u2295df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))\n\n-- diff-apply\ndf=f\u2295df\u229df :\n \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n valid-\u0394 f df \u2192 as-\u0394 \u03c4 is df ext-equiv-to f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f\n\n-- Case nat: this REFL is more obvious to Agda than to a human.\ndf=f\u2295df\u229df {nats} n dn valid-f-df =\n ext-\u0394 (\u03bb m valid-m-dn valid-rhs \u2192 refl)\n\ndf=f\u2295df\u229df {\u03c4\u2081 \u21d2 \u03c4\u2082} f df R[f,df] = ext-\u0394 (\n \u03bb g R[g,df] R[g,f\u2295df\u229df] \u2192\n extensionality (\u03bb v \u2192\n begin\n g v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n \u2261\u27e8 extract-\u0394equiv\n (df=f\u2295df\u229df\n (f v) (df v (\u27e6derive\u27e7 v))\n (proj\u2081 (R[f,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))))\n (g v)\n (proj\u2081 (R[g,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v)))\n (validity-over-\u2295-and-\u229d f g df v R[g,f\u2295df\u229df]) \u27e9\n g v \u27e6\u2295\u27e7 (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = g v} {y = g v} refl\n (application-over-\u2295-and-\u229d f g df v) \u27e9\n g v \u27e6\u2295\u27e7 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e\n )\n )\n where open \u2261-Reasoning\n\n\n\ncorrectness-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 deriveVar x \u27e7 \u03c1\n ext-equiv-to\n \u27e6 x \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 x \u27e7 (ignore \u03c1)\n\ncorrectness-of-deriveVar {\u03c4 \u2022 \u0393\u2080} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n this = df=f\u2295df\u229df {\u03c4} v dv R[v,dv]\n\ncorrectness-of-deriveVar {\u03c4\u2080 \u2022 \u0393\u2080} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4\u2080} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n (that x) = correctness-of-deriveVar \u03c1 x\n\n\n\ncorrectness-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 derive t \u27e7 \u03c1\n ext-equiv-to\n \u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 t \u27e7 (ignore \u03c1)\n\n-- Mutually recursive lemma: derivatives are valid\nvalidity-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 deriveVar x \u27e7 \u03c1)\n\nvalidity-of-deriveVar {\u03c4 \u2022 \u0393} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n this = R[v,dv]\n\nvalidity-of-deriveVar {\u03c4\u2080 \u2022 \u0393} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n (that x) = validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 {consistency} (var x) =\n validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 (nat n) = refl\n\nvalidity-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs t) = \u03bb v dv R[v,dv] \u2192\n validity-of-derive (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency} t\n , -- tuple constructor\n (begin\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n ((R[f,g\u229df] (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 f\u2295[g\u229df]=g (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u2261\u27e8 cong \u27e6 t \u27e7 (cong\u2082 _\u2022_ (f\u2295\u0394f=f (v \u27e6\u2295\u27e7 dv)) refl) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency}))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 sym (extract-\u0394equiv\n (correctness-of-derive\n ((dv \u2022 v \u2022 \u03c1))\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (R[f,g\u229df] (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u220e)\n where open \u2261-Reasoning\n\nvalidity-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} t\u2081 t\u2082)\n = R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7]\n where\n open \u2261-Reasoning\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 : \u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1) \u2261 v\u2081 v\u2082\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 rewrite (sym v\u2082=old-v\u2082) = refl\n\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 : \u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1 \u2261 dv\u2081 v\u2082 dv\u2082\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = refl\n\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] : valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (dv\u2081 v\u2082 dv\u2082)\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] rewrite \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 = R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n -- What I want to write:\n {-\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] rewrite \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n -}\n\n -- What I have to write:\n\n R : {\u03c4 : Type} \u2192 {v : \u27e6 \u03c4 \u27e7} \u2192 {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 valid-\u0394 v dv\u2081 \u2192 valid-\u0394 v dv\u2082\n\n R {nats} dv\u2081=dv\u2082 refl = cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl\n\n --R {\u03c4\u2081 \u21d2 \u03c4\u2082} dv\u2081=dv\u2082 valid-dv\u2081 rewrite dv\u2081=dv\u2082 = {!valid-dv\u2081!}\n R {\u03c4\u2081 \u21d2 \u03c4\u2082} {v} {dv\u2081} {dv\u2082} dv\u2081=dv\u2082 valid-dv\u2081 =\n \u03bb s ds R[s,ds] \u2192\n R {\u03c4\u2082} {v s} {dv\u2081 s ds} {dv\u2082 s ds}\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl)\n (proj\u2081 (valid-dv\u2081 s ds R[s,ds]))\n ,\n (begin\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2082 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v (s \u27e6\u2295\u27e7 ds)} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) (sym dv\u2081=dv\u2082) refl) refl) \u27e9\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2081 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 sym (proj\u2082 (valid-dv\u2081\n (s \u27e6\u2295\u27e7 ds)\n (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n (R[f,\u0394f] (s \u27e6\u2295\u27e7 ds)))) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds \u27e6\u2295\u27e7 \u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong (v \u27e6\u2295\u27e7 dv\u2081) (f\u2295\u0394f=f (s \u27e6\u2295\u27e7 ds)) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds)\n \u2261\u27e8 proj\u2082 (valid-dv\u2081 s ds R[s,ds]) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2081 s ds\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v s} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2082 s ds\n \u220e) where open \u2261-Reasoning\n\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] = R \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n\nvalidity-of-derive \u03c1 {consistency} (foldNat n f z) = {!!}\n\n\ncorrectness-of-derive \u03c1 (var x) = correctness-of-deriveVar \u03c1 x\n\ncorrectness-of-derive \u03c1 (nat n) = ext-\u0394 (\u03bb _ _ _ \u2192 refl)\n\ncorrectness-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs {.\u03c4\u2081} {.\u03c4\u2082} t) =\n ext-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n extensionality {\u27e6 \u03c4\u2081 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4\u2082 \u27e7} (\u03bb x \u2192\n begin\n f x \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive {\u03c4\u2081 \u2022 \u0393} {\u03c4\u2082}\n (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency}\n t)\n (f x)\n (proj\u2081 (R[f,\u0394t] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x)))\n (proj\u2081 (R[f,t\u2032\u229dt] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x))) \u27e9\n f x\n \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 (update (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency})\n \u27e6\u229d\u27e7 \u27e6 t \u27e7 (x \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u220e\n )) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} {\u0393} t\u2081 t\u2082) =\n ext-\u0394 {\u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n begin\n f \u27e6\u2295\u27e7 \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n \u2261\u27e8 extract-\u0394equiv ext-\u0394[t\u2081t\u2082] f R[f,\u0394t] R[f,t\u2032\u229dt] \u27e9\n f \u27e6\u2295\u27e7\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n \u220e)\n where\n open \u2261-Reasoning\n\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n v\u2081\u2295dv\u2081=v\u2081\u2032 : v\u2081 \u27e6\u2295\u27e7 dv\u2081 \u2261 v\u2081\u2032\n v\u2081\u2295dv\u2081=v\u2081\u2032 =\n begin\n v\u2081 \u27e6\u2295\u27e7 dv\u2081\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2081)\n v\u2081 valid-dv\u2081 (R[f,g\u229df] v\u2081 v\u2081\u2032) \u27e9\n v\u2081 \u27e6\u2295\u27e7 (v\u2081\u2032 \u27e6\u229d\u27e7 v\u2081)\n \u2261\u27e8 f\u2295[g\u229df]=g v\u2081 v\u2081\u2032 \u27e9\n v\u2081\u2032\n \u220e\n\n -- TODO: remove code duplication.\n v\u2082\u2295dv\u2082=v\u2082\u2032 : v\u2082 \u27e6\u2295\u27e7 dv\u2082 \u2261 v\u2082\u2032\n v\u2082\u2295dv\u2082=v\u2082\u2032 rewrite v\u2082=old-v\u2082 =\n begin\n old-v\u2082 \u27e6\u2295\u27e7 dv\u2082\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2082)\n old-v\u2082\n (validity-of-derive \u03c1 {consistency} t\u2082)\n (R[f,g\u229df] old-v\u2082 v\u2082\u2032) \u27e9\n old-v\u2082 \u27e6\u2295\u27e7 (v\u2082\u2032 \u27e6\u229d\u27e7 old-v\u2082)\n \u2261\u27e8 f\u2295[g\u229df]=g old-v\u2082 v\u2082\u2032 \u27e9\n v\u2082\u2032\n \u220e\n where old-v\u2082 = \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] : v\u2081\u2032 v\u2082\u2032 \u2261 (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082)\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] = sym (cong\u2082 (\u03bb f x \u2192 f x) v\u2081\u2295dv\u2081=v\u2081\u2032 v\u2082\u2295dv\u2082=v\u2082\u2032)\n\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 :\n (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082) \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 = proj\u2082 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082 \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n cong\u2082 _\u27e6\u229d\u27e7_\n (trans v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082)\n refl\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n as-\u0394 \u03c4\u2082 is dv\u2081 v\u2082 dv\u2082 ext-equiv-to v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n df=f\u2295df\u229df (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082) R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 : as-\u0394 \u03c4\u2082 is dv\u2081 v\u2082 dv\u2082 ext-equiv-to v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 rewrite v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082\n\n ext-\u0394[t\u2081t\u2082] :\n as-\u0394 \u03c4\u2082 is\n \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n ext-equiv-to\n \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency}) (\u27e6 t\u2082 \u27e7 (update \u03c1 {consistency}))\n \u27e6\u229d\u27e7 \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n ext-\u0394[t\u2081t\u2082] rewrite sym v\u2082=old-v\u2082 = dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082\n\ncorrectness-of-derive {\u0393} {\u03c4}\n \u03c1 {consistency} (foldNat n f z) = {!!}\n\ncorrectness-on-closed-terms : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192\n \u2200 (f : Term \u2205 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192\n \u2200 (s : Term \u2205 \u03c4\u2081) (ds : Term \u2205 (\u0394-Type \u03c4\u2081))\n {R[v,dv] : valid-\u0394 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)} \u2192\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205 \u27e6\u2295\u27e7 \u27e6 ds \u27e7 \u2205)\n \u2261\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)\n\ncorrectness-on-closed-terms {\u03c4\u2081} {\u03c4\u2082} f s ds {R[v,dv]} =\n begin\n h (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (f\u2295[g\u229df]=g h h))\n refl \u27e9\n (h \u27e6\u2295\u27e7 (h \u27e6\u229d\u27e7 h)) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (extract-\u0394equiv\n (correctness-of-derive \u2205 f)\n h\n (validity-of-derive \u2205 {d\u03c1=\u2205} f)\n (R[f,g\u229df] h h)))\n refl \u27e9\n (h \u27e6\u2295\u27e7 \u0394h) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 proj\u2082 (validity-of-derive \u2205 {d\u03c1=\u2205} f v dv R[v,dv]) \u27e9\n h v \u27e6\u2295\u27e7 \u0394h v dv\n \u220e\n where\n open \u2261-Reasoning\n h : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n h = \u27e6 f \u27e7 \u2205\n \u0394h : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n \u0394h = \u27e6 derive f \u27e7 \u2205\n v : \u27e6 \u03c4\u2081 \u27e7\n v = \u27e6 s \u27e7 \u2205\n dv : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv = \u27e6 ds \u27e7 \u2205\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"4b53dd9e6c407b8e8ee939aee74a311a390cb3b6","subject":"Added doc.","message":"Added doc.\n","repos":"asr\/fotc,asr\/fotc","old_file":"README.agda","new_file":"README.agda","new_contents":"------------------------------------------------------------------------------\n-- FOT (First-Order Theories)\n------------------------------------------------------------------------------\n\n-- Code accompanying the paper Combining Interactive and Automatic\n-- Reasoning in First Order Theories of Functional Programs by Ana\n-- Bove, Peter Dybjer, and Andr\u00e9s Sicard-Ram\u00edrez (FoSSaCS 2012).\n\n-- The code presented here does not match the paper exactly.\n\nmodule README where\n\n------------------------------------------------------------------------------\n-- Description\n\n-- Examples of the formalization of first-order theories showing the\n-- combination of interactive proofs with automatics proofs carried\n-- out by first-order automatic theorem provers (ATPs).\n\n------------------------------------------------------------------------------\n-- Paper, prerequisites, tested versions of the ATPS, and use\n\n-- See http:\/\/www1.eafit.edu.co\/asicard\/code\/fossacs-2012\/.\n\n------------------------------------------------------------------------------\n-- Conventions\n\n-- In the modules with the formalization of the first-order theories,\n-- if the module's name ends in 'I' the module contains interactive\n-- proofs, if it ends in 'ATP' the module contains combined proofs,\n-- otherwise the module contains definitions and\/or interactive proofs\n-- that are used by the interactive and combined proofs.\n\n------------------------------------------------------------------------------\n-- First-order theories\n------------------------------------------------------------------------------\n\n-- \u2022 First-order logic with equality\n\n-- First-order logic (FOL)\nopen import FOL.README\n\n-- Propositional equality\nopen import Common.FOL.Relation.Binary.PropositionalEquality\n\n-- Equality reasoning\nopen import Common.FOL.Relation.Binary.EqReasoning\n\n-- \u2022 Group theory\n\nopen import GroupTheory.README\n\n-- \u2022 Distributive laws on a binary operation (Stanovsk\u00fd example)\n\nopen import DistributiveLaws.README\n\n-- \u2022 First-order Peano arithmetic (PA)\n\nopen import PA.README\n\n-- \u2022 First-Order Theory of Combinators (FOTC)\n\nopen import FOTC.README\n\n-- \u2022 Logical Theory of Constructions for PCF (LTC-PCF)\n\nopen import LTC-PCF.README\n\n------------------------------------------------------------------------------\n-- ATPs failures\n------------------------------------------------------------------------------\n\n-- The ATPs (E, Equinox, Metis, SPASS and Vampire) could not prove some\n-- theorems.\n\nopen import DistributiveLaws.TaskB-ATP\nopen import FOTC.Program.ABP.MayorPremiseATP\n\n------------------------------------------------------------------------------\n-- Agsy examples\n------------------------------------------------------------------------------\n\n-- We cannot import the Agsy examples because some modules contain\n-- unsolved metas, therefore see src\/Agsy\/README.txt\n","old_contents":"------------------------------------------------------------------------------\n-- FOT (First-Order Theories)\n------------------------------------------------------------------------------\n\n-- Code accompanying the paper Combining Interactive and Automatic\n-- Reasoning in First Order Theories of Functional Programs by Ana\n-- Bove, Peter Dybjer, and Andr\u00e9s Sicard-Ram\u00edrez (FoSSaCS 2012).\n\n-- The code presented here does not match the paper exactly.\n\nmodule README where\n\n------------------------------------------------------------------------------\n-- Description\n\n-- Examples of the formalization of first-order theories showing the\n-- combination of interactive proofs with automatics proofs carried\n-- out by first-order automatic theorem provers (ATPs).\n\n------------------------------------------------------------------------------\n-- Prerequisites, tested versions of the ATPS, and use\n\n-- See http:\/\/www1.eafit.edu.co\/asicard\/code\/fossacs-2012\/.\n\n------------------------------------------------------------------------------\n-- Conventions\n\n-- In the modules with the formalization of the first-order theories,\n-- if the module's name ends in 'I' the module contains interactive\n-- proofs, if it ends in 'ATP' the module contains combined proofs,\n-- otherwise the module contains definitions and\/or interactive proofs\n-- that are used by the interactive and combined proofs.\n\n------------------------------------------------------------------------------\n-- First-order theories\n------------------------------------------------------------------------------\n\n-- \u2022 First-order logic with equality\n\n-- First-order logic (FOL)\nopen import FOL.README\n\n-- Propositional equality\nopen import Common.FOL.Relation.Binary.PropositionalEquality\n\n-- Equality reasoning\nopen import Common.FOL.Relation.Binary.EqReasoning\n\n-- \u2022 Group theory\n\nopen import GroupTheory.README\n\n-- \u2022 Distributive laws on a binary operation (Stanovsk\u00fd example)\n\nopen import DistributiveLaws.README\n\n-- \u2022 First-order Peano arithmetic (PA)\n\nopen import PA.README\n\n-- \u2022 First-Order Theory of Combinators (FOTC)\n\nopen import FOTC.README\n\n-- \u2022 Logical Theory of Constructions for PCF (LTC-PCF)\n\nopen import LTC-PCF.README\n\n------------------------------------------------------------------------------\n-- ATPs failures\n------------------------------------------------------------------------------\n\n-- The ATPs (E, Equinox, Metis, SPASS and Vampire) could not prove some\n-- theorems.\n\nopen import DistributiveLaws.TaskB-ATP\nopen import FOTC.Program.ABP.MayorPremiseATP\n\n------------------------------------------------------------------------------\n-- Agsy examples\n------------------------------------------------------------------------------\n\n-- We cannot import the Agsy examples because some modules contain\n-- unsolved metas, therefore see src\/Agsy\/README.txt\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"cad090cf1face3e9ee7c8e4575440a427c74e66a","subject":"Add more exploration functions about binary trees","message":"Add more exploration functions about binary trees\n","repos":"crypto-agda\/explore","old_file":"lib\/Explore\/BinTree.agda","new_file":"lib\/Explore\/BinTree.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule Explore.BinTree where\n\nopen import Level.NP\nopen import Type hiding (\u2605)\nopen import Data.Tree.Binary\nopen import Data.Zero\nopen import Data.Product\n\nopen import Relation.Binary.PropositionalEquality.NP\nopen import HoTT\nopen Equivalences\nopen import Type.Identities\nopen import Function.Extensionality\n\nopen import Explore.Core\nopen import Explore.Properties\nopen import Explore.Zero\nopen import Explore.Sum\nopen import Explore.Isomorphism\n\nfromBinTree : \u2200 {m} {A} \u2192 BinTree A \u2192 Explore m A\nfromBinTree empty = empty-explore\nfromBinTree (leaf x) = point-explore x\nfromBinTree (fork \u2113 r) = merge-explore (fromBinTree \u2113) (fromBinTree r)\n\nfromBinTree-ind : \u2200 {m p A} (t : BinTree A) \u2192 ExploreInd p (fromBinTree {m} t)\nfromBinTree-ind empty = empty-explore-ind\nfromBinTree-ind (leaf x) = point-explore-ind x\nfromBinTree-ind (fork \u2113 r) = merge-explore-ind (fromBinTree-ind \u2113)\n (fromBinTree-ind r)\n{-\nfromBinTree : \u2200 {m A} \u2192 BinTree A \u2192 Explore m A\nfromBinTree (leaf x) _ _ f = f x\nfromBinTree (fork \u2113 r) \u03b5 _\u2219_ f = fromBinTree \u2113 \u03b5 _\u2219_ f \u2219 fromBinTree r \u03b5 _\u2219_ f\n\nfromBinTree-ind : \u2200 {m p A} (t : BinTree A) \u2192 ExploreInd p (fromBinTree {m} t)\nfromBinTree-ind (leaf x) P _ P\u2219 Pf = Pf x\nfromBinTree-ind (fork \u2113 r) P P\u03b5 P\u2219 Pf = P\u2219 (fromBinTree-ind \u2113 P P\u03b5 P\u2219 Pf)\n (fromBinTree-ind r P P\u03b5 P\u2219 Pf)\n-}\n\nAnyP\u2261\u03a3fromBinTree : \u2200 {p}{A : \u2605 _}{P : A \u2192 \u2605 p}(xs : BinTree A) \u2192 Any P xs \u2261 \u03a3\u1d49 (fromBinTree xs) P\nAnyP\u2261\u03a3fromBinTree empty = idp\nAnyP\u2261\u03a3fromBinTree (leaf x) = idp\nAnyP\u2261\u03a3fromBinTree (fork xs xs\u2081) = \u228e= (AnyP\u2261\u03a3fromBinTree xs) (AnyP\u2261\u03a3fromBinTree xs\u2081)\n\n\nmodule _ {{_ : UA}}{{_ : FunExt}}{A : \u2605 \u2080} where\n\n\n explore\u03a3\u2208 : \u2200 {m} xs \u2192 Explore m (\u03a3 A \u03bb x \u2192 Any (_\u2261_ x) xs)\n explore\u03a3\u2208 empty = explore-iso (coe-equiv (Lift\u2261id \u2219 ! \u00d7\ud835\udfd8-snd \u2219 \u00d7= idp (! Lift\u2261id))) Lift\ud835\udfd8\u1d49\n explore\u03a3\u2208 (leaf x) = point-explore (x , idp)\n explore\u03a3\u2208 (fork xs xs\u2081) = explore-iso (coe-equiv (! \u03a3\u228e-split)) (explore\u03a3\u2208 xs \u228e\u1d49 explore\u03a3\u2208 xs\u2081)\n\n \u03a3\u1d49-adq-explore\u03a3\u2208 : \u2200 {m} xs \u2192 Adequate-\u03a3\u1d49 {m} (explore\u03a3\u2208 xs)\n \u03a3\u1d49-adq-explore\u03a3\u2208 empty = \u03a3-iso-ok (coe-equiv (Lift\u2261id \u2219 ! \u00d7\ud835\udfd8-snd \u2219 \u00d7= idp (! Lift\u2261id)))\n {A\u1d49 = Lift\ud835\udfd8\u1d49} \u03a3\u1d49Lift\ud835\udfd8-ok\n \u03a3\u1d49-adq-explore\u03a3\u2208 (leaf x\u2081) F = ! \u03a3\ud835\udfd9-snd \u2219 \u03a3-fst\u2243 (\u2243-sym (\u03a3x\u2261\u2243\ud835\udfd9 x\u2081)) F\n \u03a3\u1d49-adq-explore\u03a3\u2208 (fork xs xs\u2081) = \u03a3-iso-ok (coe-equiv (! \u03a3\u228e-split)) {A\u1d49 = explore\u03a3\u2208 xs \u228e\u1d49 explore\u03a3\u2208 xs\u2081}\n (\u03a3\u1d49\u228e-ok {e\u1d2c = explore\u03a3\u2208 xs}{e\u1d2e = explore\u03a3\u2208 xs\u2081} (\u03a3\u1d49-adq-explore\u03a3\u2208 xs) (\u03a3\u1d49-adq-explore\u03a3\u2208 xs\u2081))\n\nmodule _ {{_ : UA}}{{_ : FunExt}}{A : \u2605 \u2080}{P : A \u2192 \u2605 _}(explore-P : \u2200 {m} x \u2192 Explore m (P x)) where\n open import Explore.Zero\n open import Explore.Sum\n open import Explore.Isomorphism\n\n exploreAny : \u2200 {m} xs \u2192 Explore m (Any P xs)\n exploreAny empty = Lift\ud835\udfd8\u1d49\n exploreAny (leaf x) = explore-P x\n exploreAny (fork xs xs\u2081) = exploreAny xs \u228e\u1d49 exploreAny xs\u2081\n\n module _ (\u03a3\u1d49-adq-explore-P : \u2200 {m} x \u2192 Adequate-\u03a3\u1d49 {m} (explore-P x)) where\n \u03a3\u1d49-adq-exploreAny : \u2200 {m} xs \u2192 Adequate-\u03a3\u1d49 {m} (exploreAny xs)\n \u03a3\u1d49-adq-exploreAny empty F = ! \u03a3\ud835\udfd8-lift\u2218fst \u2219 \u03a3-fst= (! Lift\u2261id) _\n \u03a3\u1d49-adq-exploreAny (leaf x\u2081) F = \u03a3\u1d49-adq-explore-P x\u2081 F\n \u03a3\u1d49-adq-exploreAny (fork xs xs\u2081) F = \u228e= (\u03a3\u1d49-adq-exploreAny xs _) (\u03a3\u1d49-adq-exploreAny xs\u2081 _) \u2219 ! dist-\u228e-\u03a3\n\n explore\u03a3\u1d49 : \u2200 {m} xs \u2192 Explore m (\u03a3\u1d49 (fromBinTree xs) P)\n explore\u03a3\u1d49 {m} xs = fromBinTree-ind xs (\u03bb e \u2192 Explore m (\u03a3\u1d49 e P)) Lift\ud835\udfd8\u1d49 _\u228e\u1d49_ explore-P\n\n module _ (\u03a3\u1d49-adq-explore-P : \u2200 {m} x \u2192 Adequate-\u03a3\u1d49 {m} (explore-P x)) where\n \u03a3\u1d49-adq-explore\u03a3\u1d49 : \u2200 {m} xs \u2192 Adequate-\u03a3\u1d49 {m} (explore\u03a3\u1d49 xs)\n \u03a3\u1d49-adq-explore\u03a3\u1d49 empty F = ! \u03a3\ud835\udfd8-lift\u2218fst \u2219 \u03a3-fst= (! Lift\u2261id) _\n \u03a3\u1d49-adq-explore\u03a3\u1d49 (leaf x\u2081) F = \u03a3\u1d49-adq-explore-P x\u2081 F\n \u03a3\u1d49-adq-explore\u03a3\u1d49 (fork xs xs\u2081) F = \u228e= (\u03a3\u1d49-adq-explore\u03a3\u1d49 xs _) (\u03a3\u1d49-adq-explore\u03a3\u1d49 xs\u2081 _) \u2219 ! dist-\u228e-\u03a3\n\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule Explore.BinTree where\n\nopen import Data.Tree.Binary\n\nopen import Explore.Core\nopen import Explore.Properties\n\nfromBinTree : \u2200 {m} {A} \u2192 BinTree A \u2192 Explore m A\nfromBinTree empty = empty-explore\nfromBinTree (leaf x) = point-explore x\nfromBinTree (fork \u2113 r) = merge-explore (fromBinTree \u2113) (fromBinTree r)\n\nfromBinTree-ind : \u2200 {m p A} (t : BinTree A) \u2192 ExploreInd p (fromBinTree {m} t)\nfromBinTree-ind empty = empty-explore-ind\nfromBinTree-ind (leaf x) = point-explore-ind x\nfromBinTree-ind (fork \u2113 r) = merge-explore-ind (fromBinTree-ind \u2113)\n (fromBinTree-ind r)\n{-\nfromBinTree : \u2200 {m A} \u2192 BinTree A \u2192 Explore m A\nfromBinTree (leaf x) _ _ f = f x\nfromBinTree (fork \u2113 r) \u03b5 _\u2219_ f = fromBinTree \u2113 \u03b5 _\u2219_ f \u2219 fromBinTree r \u03b5 _\u2219_ f\n\nfromBinTree-ind : \u2200 {m p A} (t : BinTree A) \u2192 ExploreInd p (fromBinTree {m} t)\nfromBinTree-ind (leaf x) P _ P\u2219 Pf = Pf x\nfromBinTree-ind (fork \u2113 r) P P\u03b5 P\u2219 Pf = P\u2219 (fromBinTree-ind \u2113 P P\u03b5 P\u2219 Pf)\n (fromBinTree-ind r P P\u03b5 P\u2219 Pf)\n-}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"a51b1493ba6e471baa5091f2b629e860cc75ca83","subject":"Bits: propertis about search and count","message":"Bits: propertis about search and count\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nopen import Data.Bool.NP hiding (_==_)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (_\u229b_; rewire; rewireTbl) renaming (map to vmap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x = cong (_\u2237_ x) (vnot\u2218vnot\u2257id xs)\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n\n where open RawMonad L.monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nsearch : \u2200 {n a} {A : Set a} \u2192 (A \u2192 A \u2192 A) \u2192 (Bits n \u2192 A) \u2192 A\nsearch {zero} _ f = f []\nsearch {suc n} _\u00b7_ f = search _\u00b7_ (f \u2218 0\u2237_) \u00b7 search _\u00b7_ (f \u2218 1\u2237_)\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n#\u27e8 pred \u27e9 = search _+_ (\u03bb x \u2192 if pred x then 1 else 0)\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\nsearch-\u00b7-\u03b5\u2261\u03b5 : \u2200 {a} {A : Set a} \u03b5 (_\u00b7_ : A \u2192 A \u2192 A)\n (\u03b5\u00b7\u03b5 : \u03b5 \u00b7 \u03b5 \u2261 \u03b5) n \u2192 search {n} _\u00b7_ (const \u03b5) \u2261 \u03b5\nsearch-\u00b7-\u03b5\u2261\u03b5 \u03b5 _\u00b7_ \u03b5\u00b7\u03b5 = go\n where\n go : \u2200 n \u2192 search {n} _\u00b7_ (const \u03b5) \u2261 \u03b5\n go zero = refl\n go (suc n) rewrite go n = \u03b5\u00b7\u03b5\n\n#never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n#never\u22610 = search-\u00b7-\u03b5\u2261\u03b5 _ _ refl\n\n#always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n#always\u22612^_ zero = refl\n#always\u22612^_ (suc n) = cong\u2082 _+_ pf pf where pf = #always\u22612^ n\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\ncount\u1d47 : Bit \u2192 \u2115\ncount\u1d47 b = if b then 1 else 0\n\n#\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n#\u27e8== [] \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n\u2257-cong-search : \u2200 {n a} {A : Set a} op {f g : Bits n \u2192 A} \u2192 f \u2257 g \u2192 search op f \u2261 search op g\n\u2257-cong-search {zero} op f\u2257g = f\u2257g []\n\u2257-cong-search {suc n} op f\u2257g = cong\u2082 op (\u2257-cong-search op (f\u2257g \u2218 0\u2237_))\n (\u2257-cong-search op (f\u2257g \u2218 1\u2237_))\n\n\u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n\u2257-cong-# f g f\u2257g = \u2257-cong-search _+_ (cong count\u1d47 \u2218 f\u2257g)\n\n#-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n#-+ f f0 f1 rewrite f0 | f1 = refl\n\ntake-\u2237 : \u2200 {m a} {A : Set a} n x (xs : Vec A (n + m)) \u2192 take (suc n) (x \u2237 xs) \u2261 x \u2237 take n xs\ntake-\u2237 n x xs with splitAt n xs\ntake-\u2237 _ _ ._ | _ , _ , refl = refl\n\ndrop-\u2237 : \u2200 {m a} {A : Set a} n x (xs : Vec A (n + m)) \u2192 drop (suc n) (x \u2237 xs) \u2261 drop n xs\ndrop-\u2237 n x xs with splitAt n xs\ndrop-\u2237 _ _ ._ | _ , _ , refl = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\n#-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-take-drop zero n f g with f []\n... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n... | false = #never\u22610 n\n#-take-drop (suc m) n f g = trans (#-+ {a = #\u27e8 f \u2218 0\u2237_ \u27e9 * #\u27e8 g \u27e9} ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)))\n (trans (\u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x)))\n (#-take-drop m n (f \u2218 0\u2237_) g))\n (trans (\u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x)))\n (#-take-drop m n (f \u2218 1\u2237_) g)))\n (sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9))\n\n#-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n#\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n#\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n#\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n... | true | true | _ = s\u2264s z\u2264n\n... | true | false | p = \u22a5-elim (p _)\n... | false | _ | _ = z\u2264n\n#\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n#-\u2227-\u2228\u1d47 : \u2200 x y \u2192 count\u1d47 (x \u2227 y) + count\u1d47 (x \u2228 y) \u2261 count\u1d47 x + count\u1d47 y\n#-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (count\u1d47 y) 1 = refl\n#-\u2227-\u2228\u1d47 false y = refl\n\n#-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n#-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#\u2228' {zero} f g with f []\n... | true = s\u2264s z\u2264n\n... | false = \u2115\u2264.refl\n#\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n#\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n\u2227\u21d2\u2228 : \u2200 x y \u2192 T (x \u2227 y) \u2192 T (x \u2228 y)\n\u2227\u21d2\u2228 true y = _\n\u2227\u21d2\u2228 false y = \u03bb ()\n\n#\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n#\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192 search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = \u2257-cong-search op {-(f |\u2228| g) (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))-} (|de-morgan| f g)\n\nsearch-comm :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-comm {zero} _+_ _*_ f p hom = refl\nsearch-comm {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-comm {n} _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-comm _+_ _*_ f (p \u2218 1\u2237_) hom))\n\n[0\u2194_] : \u2200 {n} \u2192 Fin n \u2192 Bits n \u2192 Bits n\n[0\u2194_] {zero} i xs = xs\n[0\u2194_] {suc n} i xs = lookup i xs \u2237 tail (xs [ i ]\u2254 head xs)\n\n[0\u21941] : Bits 2 \u2192 Bits 2\n[0\u21941] = [0\u2194 suc zero ]\n\n[0\u21941]-spec : [0\u21941] \u2257 (\u03bb { (x \u2237 y \u2237 []) \u2192 y \u2237 x \u2237 [] })\n[0\u21941]-spec (x \u2237 y \u2237 []) = refl\n\nopen Defs public\n","old_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_\u2264_; _==_)\nopen import Data.Nat.DivMod\nopen import Data.Bool.NP hiding (_==_)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (_\u229b_; rewire; rewireTbl) renaming (map to vmap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x = cong (_\u2237_ x) (vnot\u2218vnot\u2257id xs)\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n\n where open RawMonad L.monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nsearch : \u2200 {n a} {A : Set a} \u2192 (A \u2192 A \u2192 A) \u2192 (Bits n \u2192 A) \u2192 A\nsearch {zero} _ f = f []\nsearch {suc n} _\u00b7_ f = search _\u00b7_ (f \u2218 0\u2237_) \u00b7 search _\u00b7_ (f \u2218 1\u2237_)\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n#\u27e8 pred \u27e9 = search _+_ (\u03bb x \u2192 if pred x then 1 else 0)\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nopen Defs public\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"15cd0eafd613a991286818c276496c93db0ada5d","subject":"Drop \u27e6_\u27e7*TypeHidCacheWrong","message":"Drop \u27e6_\u27e7*TypeHidCacheWrong\n\nThis wrong definition should not be counted among the failures before\nCBPV, since it was created after and it is due to a separate limitation.\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Denotation\/CachingMValue.agda","new_file":"Parametric\/Denotation\/CachingMValue.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Values for caching evaluation of MTerm\n------------------------------------------------------------------------\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.MType as MType\n\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.MValue as MValue\n\nmodule Parametric.Denotation.CachingMValue\n (Base : Type.Structure)\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen import Base.Denotation.Notation\n\nopen Type.Structure Base\nopen MType.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\nopen MValue.Structure Base \u27e6_\u27e7Base\n\nopen import Data.Product hiding (map)\nopen import Data.Sum hiding (map)\nopen import Data.Unit\nopen import Level\nopen import Function hiding (const)\n\nmodule Structure where\n {-# NO_TERMINATION_CHECK #-}\n \u27e6_\u27e7ValTypeHidCache : (\u03c4 : ValType) \u2192 Set\n \u27e6_\u27e7CompTypeHidCache : (\u03c4 : CompType) \u2192 Set\n\n \u27e6 U c \u27e7ValTypeHidCache = \u27e6 c \u27e7CompTypeHidCache\n \u27e6 B \u03b9 \u27e7ValTypeHidCache = \u27e6 base \u03b9 \u27e7\n \u27e6 vUnit \u27e7ValTypeHidCache = \u22a4\n \u27e6 \u03c4\u2081 v\u00d7 \u03c4\u2082 \u27e7ValTypeHidCache = \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache \u00d7 \u27e6 \u03c4\u2082 \u27e7ValTypeHidCache\n \u27e6 \u03c4\u2081 v+ \u03c4\u2082 \u27e7ValTypeHidCache = \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache \u228e \u27e6 \u03c4\u2082 \u27e7ValTypeHidCache\n\n --\n -- XXX The termination checker isn't happy with it, and it may be right \u2500 if\n -- you keep substituting \u03c4\u2081 = U (F \u03c4), you can make the cache arbitrarily big.\n -- I think we don't do that unless we are caching a non-terminating\n -- computation to begin with, but I'm not entirely sure.\n --\n -- However, the termination checker can't prove that the function is\n -- terminating because it's not structurally recursive, but one call of the\n -- function will produce another call of the function stuck on a neutral term:\n -- So the computation will have terminated, just in an unusual way!\n --\n -- Anyway, I need not mechanize this part of the proof for my goals.\n --\n -- XXX: This line is the only change, up to now, for the caching semantics,\n -- the rest is copied. Inheritance would handle this precisely; without\n -- inheritance, we might want to use one of the standard encodings of related\n -- features (delegation?).\n \u27e6 F \u03c4 \u27e7CompTypeHidCache = (\u03a3[ \u03c4\u2081 \u2208 ValType ] \u27e6 \u03c4 \u27e7ValTypeHidCache \u00d7 \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache )\n \u27e6 \u03c3 \u21db \u03c4 \u27e7CompTypeHidCache = \u27e6 \u03c3 \u27e7ValTypeHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n\n \u27e6_\u27e7ValCtxHidCache : (\u0393 : ValContext) \u2192 Set\n \u27e6_\u27e7ValCtxHidCache = DependentList \u27e6_\u27e7ValTypeHidCache\n\n {-\n \u27e6_\u27e7CompCtxHidCache : (\u0393 : CompContext) \u2192 Set\u2081\n \u27e6_\u27e7CompCtxHidCache = DependentList \u27e6_\u27e7CompTypeHidCache\n -}\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Values for caching evaluation of MTerm\n------------------------------------------------------------------------\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.MType as MType\n\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.MValue as MValue\n\nmodule Parametric.Denotation.CachingMValue\n (Base : Type.Structure)\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen import Base.Denotation.Notation\n\nopen Type.Structure Base\nopen MType.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\nopen MValue.Structure Base \u27e6_\u27e7Base\n\nopen import Data.Product hiding (map)\nopen import Data.Sum hiding (map)\nopen import Data.Unit\nopen import Level\nopen import Function hiding (const)\n\nmodule Structure where\n -- XXX: below we need to override just a few cases. Inheritance would handle\n -- this precisely; without inheritance, we might want to use one of the\n -- standard encodings of related features (delegation?).\n\n \u27e6_\u27e7ValTypeHidCacheWrong : (\u03c4 : ValType) \u2192 Set\u2081\n \u27e6_\u27e7CompTypeHidCacheWrong : (\u03c4 : CompType) \u2192 Set\u2081\n\n -- This line is the only change, up to now, for the caching semantics starting from CBPV.\n \u27e6 F \u03c4 \u27e7CompTypeHidCacheWrong = (\u03a3[ \u03c4\u2081 \u2208 ValType ] \u27e6 \u03c4 \u27e7ValTypeHidCacheWrong \u00d7 \u27e6 \u03c4\u2081 \u27e7ValTypeHidCacheWrong )\n -- Delegation upward.\n \u27e6 \u03c4 \u27e7CompTypeHidCacheWrong = Lift \u27e6 \u03c4 \u27e7CompType\n \u27e6_\u27e7ValTypeHidCacheWrong = Lift \u2218 \u27e6_\u27e7ValType\n -- The above does not override what happens in recursive occurrences.\n\n\n {-# NO_TERMINATION_CHECK #-}\n \u27e6_\u27e7ValTypeHidCache : (\u03c4 : ValType) \u2192 Set\n \u27e6_\u27e7CompTypeHidCache : (\u03c4 : CompType) \u2192 Set\n\n \u27e6 U c \u27e7ValTypeHidCache = \u27e6 c \u27e7CompTypeHidCache\n \u27e6 B \u03b9 \u27e7ValTypeHidCache = \u27e6 base \u03b9 \u27e7\n \u27e6 vUnit \u27e7ValTypeHidCache = \u22a4\n \u27e6 \u03c4\u2081 v\u00d7 \u03c4\u2082 \u27e7ValTypeHidCache = \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache \u00d7 \u27e6 \u03c4\u2082 \u27e7ValTypeHidCache\n \u27e6 \u03c4\u2081 v+ \u03c4\u2082 \u27e7ValTypeHidCache = \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache \u228e \u27e6 \u03c4\u2082 \u27e7ValTypeHidCache\n\n -- This line is the only change, up to now, for the caching semantics.\n --\n -- XXX The termination checker isn't happy with it, and it may be right \u2500 if\n -- you keep substituting \u03c4\u2081 = U (F \u03c4), you can make the cache arbitrarily big.\n -- I think we don't do that unless we are caching a non-terminating\n -- computation to begin with, but I'm not entirely sure.\n --\n -- However, the termination checker can't prove that the function is\n -- terminating because it's not structurally recursive, but one call of the\n -- function will produce another call of the function stuck on a neutral term:\n -- So the computation will have terminated, just in an unusual way!\n --\n -- Anyway, I need not mechanize this part of the proof for my goals.\n \u27e6 F \u03c4 \u27e7CompTypeHidCache = (\u03a3[ \u03c4\u2081 \u2208 ValType ] \u27e6 \u03c4 \u27e7ValTypeHidCache \u00d7 \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache )\n \u27e6 \u03c3 \u21db \u03c4 \u27e7CompTypeHidCache = \u27e6 \u03c3 \u27e7ValTypeHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n\n \u27e6_\u27e7ValCtxHidCache : (\u0393 : ValContext) \u2192 Set\n \u27e6_\u27e7ValCtxHidCache = DependentList \u27e6_\u27e7ValTypeHidCache\n\n {-\n \u27e6_\u27e7CompCtxHidCache : (\u0393 : CompContext) \u2192 Set\u2081\n \u27e6_\u27e7CompCtxHidCache = DependentList \u27e6_\u27e7CompTypeHidCache\n -}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a5ce35c64b2c723b065d127b6ae2c8a7e2fba61c","subject":"Embed62.agda:410: main theorem as stated in slides for Hessen Workshop (fix #68)","message":"Embed62.agda:410: main theorem as stated in slides for Hessen Workshop\n(fix #68)\n\nOld-commit-hash: 7254a11f4c494091307a31436b4d31d567d52b12\n","repos":"inc-lc\/ilc-agda","old_file":"experimental\/Embed62.agda","new_file":"experimental\/Embed62.agda","new_contents":"module Embed62 where\n\nopen import TaggedDeltaTypes\nopen import ExplicitNil using (ext\u00b3)\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Product using (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Data.Unit using (\u22a4 ; tt)\nopen import Data.Nat\n\nnatPairVisitor : Type\nnatPairVisitor = nats \u21d2 nats \u21d2 nats\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type nats = natPairVisitor \u21d2 nats -- Church pairs\n\u0394-Type bags = bags\n\u0394-Type (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394-Type \u03c3 \u21d2 \u0394-Type \u03c4\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u227c\u0394\u0393\n\nweak : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) \u03c4\nweak = weaken \u0393\u227c\u0394\u0393\n\nderiveVar : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nabsurd! : \u2200 {A B : Set} \u2192 A \u2192 A \u2192 B \u2192 B\nabsurd! _ _ b = b\n\nfst : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\nfst = abs (abs (var (that this)))\nsnd : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\nsnd = abs (abs (var this))\n\noldFrom newFrom : \u2200 {\u0393} \u2192 Term \u0393 (\u0394-Type nats) \u2192 Term \u0393 nats\noldFrom d = app d fst\nnewFrom d = app d snd\n\ndifff : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4)\napply : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4\n\napply {nats} dt t = app dt snd\napply {bags} dt t = union t dt\napply {\u03c3 \u21d2 \u03c4} dt t = abs (apply\n (app (app (weaken (drop _ \u2022 \u0393\u227c\u0393) dt)\n (var this)) (difff (var this) (var this)))\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) t) (var this)))\n\ndifff {nats} s t = abs (app (app (var this)\n (weaken (drop _ \u2022 \u0393\u227c\u0393) t)) (weaken (drop _ \u2022 \u0393\u227c\u0393) s))\ndifff {bags} s t = diff s t\ndifff {\u03c3 \u21d2 \u03c4} s t = abs (abs (difff\n (app (weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) s)\n (apply (var this) (var (that this))))\n (app (weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) t) (var (that this)))))\n\nnatPair : \u2200 {\u0393} \u2192 (old new : Term (natPairVisitor \u2022 \u0393) nats) \u2192 Term \u0393 (\u0394-Type nats)\nnatPair old new = abs (app (app (var this) old) new)\n\nembed : \u2200 {\u03c4 \u0393} \u2192 \u0394Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\nembed (\u0394nat old new) = natPair (nat old) (nat new)\nembed (\u0394bag db) = bag db\nembed (\u0394var x) = var (deriveVar x)\nembed (\u0394abs dt) = abs (abs (embed dt))\nembed (\u0394app ds t dt) = app (app (embed ds) (weak t)) (embed dt)\nembed (\u0394add ds dt) = natPair\n (add (oldFrom (embedWeaken ds))\n (oldFrom (embedWeaken dt)))\n (add (newFrom (embedWeaken ds))\n (newFrom (embedWeaken dt)))\n where\n embedWeaken : \u2200 {\u0393 \u03c4} \u2192 (d : \u0394Term \u0393 nats) \u2192\n Term (\u03c4 \u2022 \u0394-Context \u0393) (natPairVisitor \u21d2 nats)\n embedWeaken d = (weaken (drop _ \u2022 \u0393\u227c\u0393) (embed d))\nembed (\u0394map\u2080 s ds t dt) = diff\n (map (apply (embed ds) (weak s)) (apply (embed dt) (weak t)))\n (weak (map s t))\nembed (\u0394map\u2081 s dt) = map (weak s) (embed dt)\nembed (\u0394union ds dt) = union (embed ds) (embed dt)\nembed (\u0394diff ds dt) = diff (embed ds) (embed dt)\n\n\u27e6fst\u27e7 : \u2200 {A : Set} \u2192 A \u2192 A \u2192 A\n\u27e6fst\u27e7 a b = a\n\u27e6snd\u27e7 : \u2200 {A : Set} \u2192 A \u2192 A \u2192 A\n\u27e6snd\u27e7 a b = b\n\n_\u2248_ : \u2200 {\u03c4} \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\n_\u2248_ {nats} (old , new) v = old \u2261 v \u27e6fst\u27e7 \u00d7 new \u2261 v \u27e6snd\u27e7\n_\u2248_ {bags} u v = u \u2261 v\n_\u2248_ {\u03c3 \u21d2 \u03c4} u v = \n (w : \u27e6 \u03c3 \u27e7) (dw : \u0394Val \u03c3) (dw\u2032 : \u27e6 \u0394-Type \u03c3 \u27e7)\n (R[w,dw] : valid w dw) (eq : dw \u2248 dw\u2032) \u2192\n u w dw R[w,dw] \u2248 v w dw\u2032\n\ninfix 4 _\u2248_\n\ncompatible : \u2200 {\u0393} \u2192 \u0394Env \u0393 \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 Set\ncompatible {\u2205} \u2205 \u2205 = EmptySet\ncompatible {\u03c4 \u2022 \u0393} (cons v dv _ \u03c1) (dv\u2032 \u2022 v\u2032 \u2022 \u03c1\u2032) =\n triple (v \u2261 v\u2032) (dv \u2248 dv\u2032) (compatible \u03c1 \u03c1\u2032)\n\nderiveVar-correct : \u2200 {\u03c4 \u0393} {x : Var \u0393 \u03c4}\n {\u03c1 : \u0394Env \u0393} {\u03c1\u2032 : \u27e6 \u0394-Context \u0393 \u27e7} {C : compatible \u03c1 \u03c1\u2032} \u2192\n \u27e6 x \u27e7\u0394Var \u03c1 \u2248 \u27e6 deriveVar x \u27e7 \u03c1\u2032\n\nderiveVar-correct {x = this} -- pattern-matching on \u03c1, \u03c1\u2032 NOT optional\n {cons _ _ _ _} {_ \u2022 _ \u2022 _} {cons _ dv\u2248dv\u2032 _ _} = dv\u2248dv\u2032\nderiveVar-correct {x = that y}\n {cons _ _ _ \u03c1} {_ \u2022 _ \u2022 \u03c1\u2032} {cons _ _ C _} =\n deriveVar-correct {x = y} {\u03c1} {\u03c1\u2032} {C}\n\nweak-id : \u2200 {\u0393} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192 \u27e6 \u0393\u227c\u0393 {\u0393} \u27e7 \u03c1 \u2261 \u03c1\nweak-id {\u2205} {\u2205} = refl\nweak-id {\u03c4 \u2022 \u0393} {v \u2022 \u03c1} = cong\u2082 _\u2022_ {x = v} refl (weak-id {\u0393} {\u03c1})\n\nweak-eq : \u2200 {\u0393} {\u03c1 : \u0394Env \u0393} {\u03c1\u2032 : \u27e6 \u0394-Context \u0393 \u27e7}\n (C : compatible \u03c1 \u03c1\u2032) \u2192 ignore \u03c1 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1\u2032\n\nweak-eq {\u2205} {\u2205} {\u2205} _ = refl\nweak-eq {\u03c4 \u2022 \u0393} {cons v dv _ \u03c1} {dv\u2032 \u2022 v\u2032 \u2022 \u03c1\u2032} (cons v\u2261v\u2032 _ C _) =\n cong\u2082 _\u2022_ v\u2261v\u2032 (weak-eq C)\n\nweak-eq-term : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4)\n {\u03c1 : \u0394Env \u0393} {\u03c1\u2032 : \u27e6 \u0394-Context \u0393 \u27e7} (C : compatible \u03c1 \u03c1\u2032) \u2192\n \u27e6 t \u27e7 (ignore \u03c1) \u2261 \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 \u03c1\u2032\n\nweak-eq-term t {\u03c1} {\u03c1\u2032} C =\n trans (cong \u27e6 t \u27e7 (weak-eq C)) (sym (weaken-sound t \u03c1\u2032))\n\nweaken-once : \u2200 {\u03c4 \u0393 \u03c3} {t : Term \u0393 \u03c4} {v : \u27e6 \u03c3 \u27e7} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 t \u27e7 \u03c1 \u2261 \u27e6 weaken (drop \u03c3 \u2022 \u0393\u227c\u0393) t \u27e7 (v \u2022 \u03c1)\n\nweaken-once {t = t} {v} {\u03c1} = trans\n (cong \u27e6 t \u27e7 (sym weak-id))\n (sym (weaken-sound t (v \u2022 \u03c1)))\n\nweaken-twice : \u2200 {\u03c4 \u0393 \u03c3\u2080 \u03c3\u2081} {t : Term \u0393 \u03c4}\n {u : \u27e6 \u03c3\u2080 \u27e7} {v : \u27e6 \u03c3\u2081 \u27e7} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 t \u27e7 \u03c1 \u2261 \u27e6 weaken (drop \u03c3\u2080 \u2022 drop \u03c3\u2081 \u2022 \u0393\u227c\u0393) t \u27e7 (u \u2022 v \u2022 \u03c1)\n\nweaken-twice {t = t} {u} {v} {\u03c1} = trans\n (cong \u27e6 t \u27e7 (sym weak-id))\n (sym (weaken-sound t (u \u2022 v \u2022 \u03c1)))\n\nembed-correct : \u2200 {\u03c4 \u0393}\n {dt : \u0394Term \u0393 \u03c4}\n {\u03c1 : \u0394Env \u0393} {V : dt is-valid-for \u03c1}\n {\u03c1\u2032 : \u27e6 \u0394-Context \u0393 \u27e7} {C : compatible \u03c1 \u03c1\u2032} \u2192\n \u27e6 dt \u27e7\u0394 \u03c1 V \u2248 \u27e6 embed dt \u27e7 \u03c1\u2032\n\nembed-correct {dt = \u0394nat old new} = refl , refl\n\nembed-correct {dt = \u0394bag db} = refl\n\nembed-correct {dt = \u0394var x} {\u03c1} {\u03c1\u2032 = \u03c1\u2032} {C} =\n deriveVar-correct {x = x} {\u03c1} {\u03c1\u2032} {C}\n\nembed-correct {dt = \u0394add ds dt} {\u03c1} {V} {\u03c1\u2032} {C} =\n let\n s00 , s01 = \u27e6 ds \u27e7\u0394 \u03c1 (car V)\n t00 , t01 = \u27e6 dt \u27e7\u0394 \u03c1 (cadr V)\n s10 = \u27e6 embed ds \u27e7 \u03c1\u2032 \u27e6fst\u27e7\n s11 = \u27e6 embed ds \u27e7 \u03c1\u2032 \u27e6snd\u27e7\n t10 = \u27e6 embed dt \u27e7 \u03c1\u2032 \u27e6fst\u27e7\n t11 = \u27e6 embed dt \u27e7 \u03c1\u2032 \u27e6snd\u27e7\n rec-s = embed-correct {dt = ds} {\u03c1} {car V} {\u03c1\u2032} {C}\n rec-t = embed-correct {dt = dt} {\u03c1} {cadr V} {\u03c1\u2032} {C}\n in\n \n (begin\n s00 + t00\n \u2261\u27e8 cong\u2082 _+_ (proj\u2081 rec-s) (proj\u2081 rec-t) \u27e9\n s10 + t10\n \u2261\u27e8 cong\u2082 _+_\n (cong (\u03bb x \u2192 \u27e6 embed ds \u27e7 x \u27e6fst\u27e7) (sym weak-id))\n (cong (\u03bb x \u2192 \u27e6 embed dt \u27e7 x \u27e6fst\u27e7) (sym weak-id)) \u27e9\n \u27e6 embed ds \u27e7 (\u27e6 drop _ \u2022 \u0393\u227c\u0393 \u27e7 (\u27e6fst\u27e7 {\u2115} \u2022 \u03c1\u2032)) \u27e6fst\u27e7 +\n \u27e6 embed dt \u27e7 (\u27e6 drop _ \u2022 \u0393\u227c\u0393 \u27e7 (\u27e6fst\u27e7 {\u2115} \u2022 \u03c1\u2032)) \u27e6fst\u27e7\n \u2261\u27e8 cong\u2082 _+_\n (cong (\u03bb f \u2192 f \u27e6fst\u27e7) (sym (weaken-sound (embed ds) (\u27e6fst\u27e7 \u2022 \u03c1\u2032))))\n (cong (\u03bb f \u2192 f \u27e6fst\u27e7) (sym (weaken-sound (embed dt) (\u27e6fst\u27e7 \u2022 \u03c1\u2032)))) \u27e9\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (embed ds) \u27e7 (\u27e6fst\u27e7 \u2022 \u03c1\u2032) \u27e6fst\u27e7 +\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (embed dt) \u27e7 (\u27e6fst\u27e7 \u2022 \u03c1\u2032) \u27e6fst\u27e7\n \u220e)\n ,\n (begin\n s01 + t01\n \u2261\u27e8 cong\u2082 _+_ (proj\u2082 rec-s) (proj\u2082 rec-t) \u27e9\n s11 + t11\n \u2261\u27e8 cong\u2082 _+_\n (cong (\u03bb x \u2192 \u27e6 embed ds \u27e7 x \u27e6snd\u27e7) (sym weak-id))\n (cong (\u03bb x \u2192 \u27e6 embed dt \u27e7 x \u27e6snd\u27e7) (sym weak-id)) \u27e9\n \u27e6 embed ds \u27e7 (\u27e6 drop _ \u2022 \u0393\u227c\u0393 \u27e7 (\u27e6snd\u27e7 {\u2115} \u2022 \u03c1\u2032)) \u27e6snd\u27e7 +\n \u27e6 embed dt \u27e7 (\u27e6 drop _ \u2022 \u0393\u227c\u0393 \u27e7 (\u27e6snd\u27e7 {\u2115} \u2022 \u03c1\u2032)) \u27e6snd\u27e7\n \u2261\u27e8 cong\u2082 _+_\n (cong (\u03bb f \u2192 f \u27e6snd\u27e7) (sym (weaken-sound (embed ds) (\u27e6snd\u27e7 \u2022 \u03c1\u2032))))\n (cong (\u03bb f \u2192 f \u27e6snd\u27e7) (sym (weaken-sound (embed dt) (\u27e6snd\u27e7 \u2022 \u03c1\u2032)))) \u27e9\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (embed ds) \u27e7 (\u27e6snd\u27e7 \u2022 \u03c1\u2032) \u27e6snd\u27e7 +\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (embed dt) \u27e7 (\u27e6snd\u27e7 \u2022 \u03c1\u2032) \u27e6snd\u27e7\n \u220e)\n where open \u2261-Reasoning\n\nembed-correct {dt = \u0394union ds dt} {\u03c1} {V} {\u03c1\u2032} {C} = cong\u2082 _++_\n (embed-correct {dt = ds} {\u03c1} {car V} {\u03c1\u2032} {C})\n (embed-correct {dt = dt} {\u03c1} {cadr V} {\u03c1\u2032} {C})\n\nembed-correct {dt = \u0394diff ds dt} {\u03c1} {V} {\u03c1\u2032} {C} = cong\u2082 _\\\\_\n (embed-correct {dt = ds} {\u03c1} {car V} {\u03c1\u2032} {C})\n (embed-correct {dt = dt} {\u03c1} {cadr V} {\u03c1\u2032} {C})\n\nembed-correct {dt = \u0394map\u2081 s dt} {\u03c1} {V} {\u03c1\u2032} {C} = cong\u2082 mapBag\n (weak-eq-term s C)\n (embed-correct {dt = dt} {\u03c1} {car V} {\u03c1\u2032} {C})\n\nembed-correct {dt = \u0394map\u2080 s ds t dt} {\u03c1} {V} {\u03c1\u2032} {C} = cong\u2082 _\\\\_\n (cong\u2082 mapBag\n (extensionality (\u03bb v \u2192\n begin\n proj\u2082 (\u27e6 ds \u27e7\u0394 \u03c1 (car V) v (v , v) refl)\n \u2261\u27e8 proj\u2082 (embed-correct {dt = ds} {\u03c1} {car V} {\u03c1\u2032} {C}\n v (v , v) (\u03bb f \u2192 f v v) refl (refl , refl)) \u27e9\n \u27e6 embed ds \u27e7 \u03c1\u2032 v (\u03bb f \u2192 f v v) \u27e6snd\u27e7\n \u2261\u27e8 cong (\u03bb hole \u2192 hole v (\u03bb f \u2192 f v v) \u27e6snd\u27e7)\n (weaken-once {t = embed ds} {v} {\u03c1\u2032}) \u27e9\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (embed ds) \u27e7 (v \u2022 \u03c1\u2032) v (\u03bb f \u2192 f v v) \u27e6snd\u27e7\n \u220e))\n (cong\u2082 _++_\n (weak-eq-term t C)\n (embed-correct {dt = dt} {\u03c1} {cadr V} {\u03c1\u2032} {C})))\n (cong\u2082 mapBag (weak-eq-term s C) (weak-eq-term t C))\n where open \u2261-Reasoning\n\nembed-correct {dt = \u0394app ds t dt} {\u03c1} {cons vds vdt R[t,dt] _} {\u03c1\u2032} {C}\n rewrite sym (weak-eq-term t C) =\n embed-correct {dt = ds} {\u03c1} {vds} {\u03c1\u2032} {C}\n (\u27e6 t \u27e7Term (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1 vdt) (\u27e6 embed dt \u27e7Term \u03c1\u2032)\n R[t,dt] (embed-correct {dt = dt} {\u03c1} {vdt} {\u03c1\u2032} {C})\n\nembed-correct {dt = \u0394abs dt} {\u03c1} {V} {\u03c1\u2032} {C} = \u03bb w dw dw\u2032 R[w,dw] dw\u2248dw\u2032 \u2192\n embed-correct {dt = dt}\n {cons w dw R[w,dw] \u03c1} {V w dw R[w,dw]}\n {dw\u2032 \u2022 w \u2022 \u03c1\u2032} {cons refl dw\u2248dw\u2032 C tt}\n\n_\u229e_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u229f_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\ninfixl 6 _\u229e_ _\u229f_ -- as with + - in GHC.Num\n\n_\u229e_ {nats} n dn = dn \u27e6snd\u27e7\n_\u229e_ {bags} b db = b ++ db\n_\u229e_ {\u03c3 \u21d2 \u03c4} f df = \u03bb v \u2192 f v \u229e df v (v \u229f v)\n\n_\u229f_ {nats} m n = \u03bb f \u2192 f n m\n_\u229f_ {bags} d b = d \\\\ b\n_\u229f_ {\u03c3 \u21d2 \u03c4} g f = \u03bb v dv \u2192 g (v \u229e dv) \u229f f v\n\nu\u229dv\u2248u\u229fv : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 u \u229d v \u2248 u \u229f v\n\ncarry-over : \u2200 {\u03c4}\n {dv : \u0394Val \u03c4} {dv\u2032 : \u27e6 \u0394-Type \u03c4 \u27e7} (dv\u2248dv\u2032 : dv \u2248 dv\u2032)\n {v : \u27e6 \u03c4 \u27e7} (R[v,dv] : valid v dv) \u2192\n v \u2295 dv \u2261 v \u229e dv\u2032\n\nu\u229dv\u2248u\u229fv {nats} = refl , refl\nu\u229dv\u2248u\u229fv {bags} = refl\nu\u229dv\u2248u\u229fv {\u03c3 \u21d2 \u03c4} {g} {f} = result\n where\n result : \u2200 (w : \u27e6 \u03c3 \u27e7) (dw : \u0394Val \u03c3) (dw\u2032 : \u27e6 \u0394-Type \u03c3 \u27e7)\n (R[w,dw] : valid w dw) (dw\u2248dw\u2032 : dw \u2248 dw\u2032) \u2192\n g (w \u2295 dw) \u229d f w \u2248 g (w \u229e dw\u2032) \u229f f w\n result w dw dw\u2032 R[w,dw] dw\u2248dw\u2032\n rewrite carry-over dw\u2248dw\u2032 R[w,dw] = u\u229dv\u2248u\u229fv\n -- Question: is there anyway not to write the signature\n -- of u\u229dv\u2248u\u229fv twice just so as to carry out the rewrite?\n\ncarry-over {nats} dv\u2248dv\u2032 R[v,dv] = proj\u2082 dv\u2248dv\u2032\ncarry-over {bags} dv\u2248dv\u2032 {v} R[v,dv] = cong (_++_ v) dv\u2248dv\u2032\ncarry-over {\u03c3 \u21d2 \u03c4} {df} {df\u2032} df\u2248df\u2032 {f} R[f,df] =\n extensionality (\u03bb v \u2192 carry-over {\u03c4}\n {df v (v \u229d v) R[v,u\u229dv]} {df\u2032 v (v \u229f v)}\n (df\u2248df\u2032 v (v \u229d v) (v \u229f v) R[v,u\u229dv] u\u229dv\u2248u\u229fv)\n {f v}\n (proj\u2081 (R[f,df] v (v \u229d v) R[v,u\u229dv])))\n\nignore\u2032 : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore\u2032 {\u2205} \u2205 = \u2205\nignore\u2032 {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 \u03c1) = v \u2022 ignore\u2032 \u03c1\n\nupdate\u2032 : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nupdate\u2032 {\u2205} \u2205 = \u2205\nupdate\u2032 {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 \u03c1) = (v \u229e dv) \u2022 update\u2032 \u03c1\n\nignorance : \u2200 {\u0393}\n {\u03c1 : \u0394Env \u0393} {\u03c1\u2032 : \u27e6 \u0394-Context \u0393 \u27e7} (C : compatible \u03c1 \u03c1\u2032) \u2192\n (ignore \u03c1) \u2261 (ignore\u2032 \u03c1\u2032)\n\nignorance {\u2205} {\u2205} {\u2205} _ = refl\nignorance {\u03c4 \u2022 \u0393} {cons v dv _ \u03c1} {dv\u2032 \u2022 v\u2032 \u2022 \u03c1\u2032}\n (cons v\u2261v\u2032 _ C _) = cong\u2082 _\u2022_ v\u2261v\u2032 (ignorance C)\n\n-- ... because \"updatedness\" sounds horrible\nactualit\u00e9 : \u2200 {\u0393}\n {\u03c1 : \u0394Env \u0393} {\u03c1\u2032 : \u27e6 \u0394-Context \u0393 \u27e7} (C : compatible \u03c1 \u03c1\u2032) \u2192\n (update \u03c1) \u2261 (update\u2032 \u03c1\u2032)\n\nactualit\u00e9 {\u2205} {\u2205} {\u2205} _ = refl\nactualit\u00e9 {\u03c4 \u2022 \u0393} {cons v dv R[v,dv] \u03c1} {dv\u2032 \u2022 v\u2032 \u2022 \u03c1\u2032}\n (cons v\u2261v\u2032 dv\u2248dv\u2032 C _) rewrite v\u2261v\u2032 =\n cong\u2082 _\u2022_ (carry-over dv\u2248dv\u2032 R[v,dv]) (actualit\u00e9 C)\n\nignore-both : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4)\n {\u03c1 : \u0394Env \u0393} {\u03c1\u2032 : \u27e6 \u0394-Context \u0393 \u27e7} (C : compatible \u03c1 \u03c1\u2032) \u2192\n \u27e6 t \u27e7 (ignore \u03c1) \u2261 \u27e6 t \u27e7 (ignore\u2032 \u03c1\u2032)\n\nignore-both t C rewrite ignorance C = refl\n\nupdate-both : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4)\n {\u03c1 : \u0394Env \u0393} {\u03c1\u2032 : \u27e6 \u0394-Context \u0393 \u27e7} (C : compatible \u03c1 \u03c1\u2032) \u2192\n \u27e6 t \u27e7 (update \u03c1) \u2261 \u27e6 t \u27e7 (update\u2032 \u03c1\u2032)\n\nupdate-both t C rewrite actualit\u00e9 C = refl\n\ncorollary-closed : \u2200 {\u03c3 \u03c4} {t : Term \u2205 (\u03c3 \u21d2 \u03c4)}\n {v : \u27e6 \u03c3 \u27e7} {dv : \u0394Val \u03c3} {R[v,dv] : valid v dv}\n \u2192 \u27e6 t \u27e7 \u2205 (v \u2295 dv)\n \u2261 \u27e6 t \u27e7 \u2205 v \u2295 \u27e6 derive t \u27e7\u0394 \u2205 (unrestricted t) v dv R[v,dv]\n\ncorollary-closed {t = t} {v} {dv} {R[v,dv]} =\n let\n f = \u27e6 t \u27e7 \u2205\n df = \u27e6 derive t \u27e7\u0394 \u2205 (unrestricted t)\n in\n begin\n f (v \u2295 dv)\n \u2261\u27e8 cong (\u03bb hole \u2192 hole (v \u2295 dv)) (sym (correctness {t = t})) \u27e9\n (f \u2295 df) (v \u2295 dv)\n \u2261\u27e8 proj\u2082 (validity {t = t} v dv R[v,dv]) \u27e9\n f v \u2295 df v dv R[v,dv]\n \u220e where open \u2261-Reasoning\n\n\u27e6apply\u27e7 : \u2200 {\u03c4 \u0393}\n {t : Term \u0393 \u03c4} {dt : Term \u0393 (\u0394-Type \u03c4)} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 t \u27e7 \u03c1 \u229e \u27e6 dt \u27e7 \u03c1 \u2261 \u27e6 apply dt t \u27e7 \u03c1\n\n\u27e6difff\u27e7 : \u2200 {\u03c4 \u0393}\n {s : Term \u0393 \u03c4} {t : Term \u0393 \u03c4} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 s \u27e7 \u03c1 \u229f \u27e6 t \u27e7 \u03c1 \u2261 \u27e6 difff s t \u27e7 \u03c1\n\n\u27e6apply\u27e7 {nats} {\u0393} {t} {dt} {\u03c1} = refl\n\u27e6apply\u27e7 {bags} {\u0393} {t} {dt} {\u03c1} = refl\n\u27e6apply\u27e7 {\u03c3 \u21d2 \u03c4} {\u0393} {t} {dt} {\u03c1} = extensionality (\u03bb v \u2192\n let\n \u03c1\u2032 : \u27e6 \u03c3 \u2022 \u0393 \u27e7Context\n \u03c1\u2032 = v \u2022 \u03c1\n dv = \u27e6 difff (var this) (var this) \u27e7 \u03c1\u2032\n t-v = app (weaken (drop \u03c3 \u2022 \u0393\u227c\u0393) t) (var this)\n dt-v-dv = app (app\n (weaken (drop \u03c3 \u2022 \u0393\u227c\u0393) dt) (var this)) (difff (var this) (var this))\n in\n begin\n \u27e6 t \u27e7 \u03c1 v \u229e \u27e6 dt \u27e7 \u03c1 v (v \u229f v)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 \u03c1 v \u229e \u27e6 dt \u27e7 \u03c1 v hole)\n (\u27e6difff\u27e7 {s = var this} {var this} {\u03c1\u2032}) \u27e9\n \u27e6 t \u27e7 \u03c1 v \u229e \u27e6 dt \u27e7 \u03c1 v dv\n \u2261\u27e8 cong\u2082 _\u229e_\n (cong (\u03bb hole \u2192 hole v ) (weaken-once {t = t}))\n (cong (\u03bb hole \u2192 hole v dv) (weaken-once {t = dt})) \u27e9\n \u27e6 t-v \u27e7 \u03c1\u2032 \u229e \u27e6 dt-v-dv \u27e7 \u03c1\u2032\n \u2261\u27e8 \u27e6apply\u27e7 \u27e9\n \u27e6 apply dt-v-dv t-v \u27e7 \u03c1\u2032\n \u220e) where\n open \u2261-Reasoning\n\n\u27e6difff\u27e7 {nats} {\u0393} {s} {t} {\u03c1} = extensionality result\n where\n result : (f : \u27e6 nats \u21d2 nats \u21d2 nats \u27e7)\n \u2192 f (\u27e6 t \u27e7 \u03c1) (\u27e6 s \u27e7 \u03c1)\n \u2261 f (\u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) t \u27e7 (f \u2022 \u03c1))\n (\u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) s \u27e7 (f \u2022 \u03c1))\n result f rewrite weaken-once {t = s} {f} {\u03c1}\n | weaken-once {t = t} {f} {\u03c1} = refl\n\u27e6difff\u27e7 {bags} {\u0393} {s} {t} {\u03c1} = refl\n\u27e6difff\u27e7 {\u03c3 \u21d2 \u03c4} {\u0393} {s} {t} {\u03c1} = \n extensionality (\u03bb v \u2192 extensionality (\u03bb dv \u2192\n let\n \u03c1\u2032 : \u27e6 (\u0394-Type \u03c3) \u2022 \u03c3 \u2022 \u0393 \u27e7Context\n \u03c1\u2032 = dv \u2022 v \u2022 \u03c1\n f = weaken (drop \u0394-Type \u03c3 \u2022 (drop \u03c3 \u2022 \u0393\u227c\u0393)) s\n t-x = app (weaken (drop \u0394-Type \u03c3 \u2022 (drop \u03c3 \u2022 \u0393\u227c\u0393)) t)\n (var (that this))\n x\u229edx = apply (var this) (var (that this))\n in\n begin\n \u27e6 s \u27e7 \u03c1 (v \u229e dv) \u229f \u27e6 t \u27e7 \u03c1 v\n \u2261\u27e8 cong\u2082 _\u229f_\n (cong\u2082 (\u03bb f x \u2192 f x)\n (weaken-twice {t = s} {dv} {v} {\u03c1}) \u27e6apply\u27e7)\n (cong (\u03bb hole \u2192 hole v)\n (weaken-twice {t = t} {dv} {v} {\u03c1})) \u27e9\n \u27e6 f \u27e7 \u03c1\u2032 (\u27e6 x\u229edx \u27e7 \u03c1\u2032) \u229f \u27e6 t-x \u27e7 \u03c1\u2032\n \u2261\u27e8 \u27e6difff\u27e7 {s = app f x\u229edx} {t-x} \u27e9\n \u27e6 difff (app f x\u229edx) t-x \u27e7 \u03c1\u2032\n \u220e)) where open \u2261-Reasoning\n\nmain-theorem : \u2200 {\u03c3 \u03c4}\n {s : Term \u2205 (\u03c3 \u21d2 \u03c4)} {t\u2080 : Term \u2205 \u03c3} {t\u2081 : Term \u2205 \u03c3}\n \u2192 \u27e6 app s t\u2081 \u27e7 \u2205\n \u2261 \u27e6 apply (app (app (embed (derive s)) t\u2080) (difff t\u2081 t\u2080)) (app s t\u2080) \u27e7 \u2205\n\nmain-theorem {s = s} {t\u2080} {t\u2081} =\n let\n f = \u27e6 s \u27e7 \u2205\n df = \u27e6 derive s \u27e7\u0394 \u2205 (unrestricted s)\n df\u2032 = \u27e6 embed (derive s) \u27e7 \u2205\n v = \u27e6 t\u2080 \u27e7 \u2205\n u = \u27e6 t\u2081 \u27e7 \u2205\n in\n begin\n f u\n \u2261\u27e8 cong (\u03bb hole \u2192 f hole) (sym v\u2295[u\u229dv]=u) \u27e9\n f (v \u2295 (u \u229d v))\n \u2261\u27e8 corollary-closed {t = s} {v} {u \u229d v} {R[v,u\u229dv]} \u27e9\n f v \u2295 df v (u \u229d v) R[v,u\u229dv]\n \u2261\u27e8 carry-over\n (embed-correct {\u0393 = \u2205} {dt = derive s}\n {\u2205} {unrestricted s} {\u2205} {\u2205}\n v (u \u229d v) (u \u229f v) R[v,u\u229dv] u\u229dv\u2248u\u229fv)\n (proj\u2081 (validity {\u0393 = \u2205} {s} v (u \u229d v) R[v,u\u229dv])) \u27e9\n f v \u229e df\u2032 v (u \u229f v)\n \u2261\u27e8 trans (cong (\u03bb hole \u2192 f v \u229e df\u2032 v hole)\n (\u27e6difff\u27e7 {s = t\u2081} {t\u2080})) \u27e6apply\u27e7 \u27e9\n \u27e6 apply (app (app (embed (derive s)) t\u2080) (difff t\u2081 t\u2080))\n (app s t\u2080) \u27e7 \u2205\n \u220e where open \u2261-Reasoning\n","old_contents":"module Embed62 where\n\nopen import TaggedDeltaTypes\nopen import ExplicitNil using (ext\u00b3)\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Product using (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Data.Unit using (\u22a4 ; tt)\nopen import Data.Nat\n\nnatPairVisitor : Type\nnatPairVisitor = nats \u21d2 nats \u21d2 nats\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type nats = natPairVisitor \u21d2 nats -- Church pairs\n\u0394-Type bags = bags\n\u0394-Type (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394-Type \u03c3 \u21d2 \u0394-Type \u03c4\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u227c\u0394\u0393\n\nweak : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) \u03c4\nweak = weaken \u0393\u227c\u0394\u0393\n\nderiveVar : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nabsurd! : \u2200 {A B : Set} \u2192 A \u2192 A \u2192 B \u2192 B\nabsurd! _ _ b = b\n\nfst : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\nfst = abs (abs (var (that this)))\nsnd : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\nsnd = abs (abs (var this))\n\noldFrom newFrom : \u2200 {\u0393} \u2192 Term \u0393 (\u0394-Type nats) \u2192 Term \u0393 nats\noldFrom d = app d fst\nnewFrom d = app d snd\n\ndifff : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4)\napply : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4\n\napply {nats} dt t = app dt snd\napply {bags} dt t = union t dt\napply {\u03c3 \u21d2 \u03c4} dt t = abs (apply\n (app (app (weaken (drop _ \u2022 \u0393\u227c\u0393) dt)\n (var this)) (difff (var this) (var this)))\n (app (weaken (drop _ \u2022 \u0393\u227c\u0393) t) (var this)))\n\ndifff {nats} s t = abs (app (app (var this)\n (weaken (drop _ \u2022 \u0393\u227c\u0393) s)) (weaken (drop _ \u2022 \u0393\u227c\u0393) t))\ndifff {bags} s t = diff s t\ndifff {\u03c3 \u21d2 \u03c4} s t = abs (abs (difff\n (app (weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) s)\n (apply (var this) (var (that this))))\n (app (weaken (drop _ \u2022 drop _ \u2022 \u0393\u227c\u0393) t) (var (that this)))))\n\nnatPair : \u2200 {\u0393} \u2192 (old new : Term (natPairVisitor \u2022 \u0393) nats) \u2192 Term \u0393 (\u0394-Type nats)\nnatPair old new = abs (app (app (var this) old) new)\n\nembed : \u2200 {\u03c4 \u0393} \u2192 \u0394Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\nembed (\u0394nat old new) = natPair (nat old) (nat new)\nembed (\u0394bag db) = bag db\nembed (\u0394var x) = var (deriveVar x)\nembed (\u0394abs dt) = abs (abs (embed dt))\nembed (\u0394app ds t dt) = app (app (embed ds) (weak t)) (embed dt)\nembed (\u0394add ds dt) = natPair\n (add (oldFrom (embedWeaken ds))\n (oldFrom (embedWeaken dt)))\n (add (newFrom (embedWeaken ds))\n (newFrom (embedWeaken dt)))\n where\n embedWeaken : \u2200 {\u0393 \u03c4} \u2192 (d : \u0394Term \u0393 nats) \u2192\n Term (\u03c4 \u2022 \u0394-Context \u0393) (natPairVisitor \u21d2 nats)\n embedWeaken d = (weaken (drop _ \u2022 \u0393\u227c\u0393) (embed d))\nembed (\u0394map\u2080 s ds t dt) = diff\n (map (apply (embed ds) (weak s)) (apply (embed dt) (weak t)))\n (weak (map s t))\nembed (\u0394map\u2081 s dt) = map (weak s) (embed dt)\nembed (\u0394union ds dt) = union (embed ds) (embed dt)\nembed (\u0394diff ds dt) = diff (embed ds) (embed dt)\n\n\u27e6fst\u27e7 : \u2200 {A : Set} \u2192 A \u2192 A \u2192 A\n\u27e6fst\u27e7 a b = a\n\u27e6snd\u27e7 : \u2200 {A : Set} \u2192 A \u2192 A \u2192 A\n\u27e6snd\u27e7 a b = b\n\n_\u2248_ : \u2200 {\u03c4} \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\n_\u2248_ {nats} (old , new) v = old \u2261 v \u27e6fst\u27e7 \u00d7 new \u2261 v \u27e6snd\u27e7\n_\u2248_ {bags} u v = u \u2261 v\n_\u2248_ {\u03c3 \u21d2 \u03c4} u v = \n (w : \u27e6 \u03c3 \u27e7) (dw : \u0394Val \u03c3) (dw\u2032 : \u27e6 \u0394-Type \u03c3 \u27e7)\n (R[w,dw] : valid w dw) (eq : dw \u2248 dw\u2032) \u2192\n u w dw R[w,dw] \u2248 v w dw\u2032\n\ninfix 4 _\u2248_\n\ncompatible : \u2200 {\u0393} \u2192 \u0394Env \u0393 \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 Set\ncompatible {\u2205} \u2205 \u2205 = EmptySet\ncompatible {\u03c4 \u2022 \u0393} (cons v dv _ \u03c1) (dv\u2032 \u2022 v\u2032 \u2022 \u03c1\u2032) =\n triple (v \u2261 v\u2032) (dv \u2248 dv\u2032) (compatible \u03c1 \u03c1\u2032)\n\nderiveVar-correct : \u2200 {\u03c4 \u0393} {x : Var \u0393 \u03c4}\n {\u03c1 : \u0394Env \u0393} {\u03c1\u2032 : \u27e6 \u0394-Context \u0393 \u27e7} {C : compatible \u03c1 \u03c1\u2032} \u2192\n \u27e6 x \u27e7\u0394Var \u03c1 \u2248 \u27e6 deriveVar x \u27e7 \u03c1\u2032\n\nderiveVar-correct {x = this} -- pattern-matching on \u03c1, \u03c1\u2032 NOT optional\n {cons _ _ _ _} {_ \u2022 _ \u2022 _} {cons _ dv\u2248dv\u2032 _ _} = dv\u2248dv\u2032\nderiveVar-correct {x = that y}\n {cons _ _ _ \u03c1} {_ \u2022 _ \u2022 \u03c1\u2032} {cons _ _ C _} =\n deriveVar-correct {x = y} {\u03c1} {\u03c1\u2032} {C}\n\nweak-id : \u2200 {\u0393} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192 \u27e6 \u0393\u227c\u0393 {\u0393} \u27e7 \u03c1 \u2261 \u03c1\nweak-id {\u2205} {\u2205} = refl\nweak-id {\u03c4 \u2022 \u0393} {v \u2022 \u03c1} = cong\u2082 _\u2022_ {x = v} refl (weak-id {\u0393} {\u03c1})\n\nweak-eq : \u2200 {\u0393} {\u03c1 : \u0394Env \u0393} {\u03c1\u2032 : \u27e6 \u0394-Context \u0393 \u27e7}\n (C : compatible \u03c1 \u03c1\u2032) \u2192 ignore \u03c1 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1\u2032\n\nweak-eq {\u2205} {\u2205} {\u2205} _ = refl\nweak-eq {\u03c4 \u2022 \u0393} {cons v dv _ \u03c1} {dv\u2032 \u2022 v\u2032 \u2022 \u03c1\u2032} (cons v\u2261v\u2032 _ C _) =\n cong\u2082 _\u2022_ v\u2261v\u2032 (weak-eq C)\n\nweak-eq-term : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4)\n {\u03c1 : \u0394Env \u0393} {\u03c1\u2032 : \u27e6 \u0394-Context \u0393 \u27e7} (C : compatible \u03c1 \u03c1\u2032) \u2192\n \u27e6 t \u27e7 (ignore \u03c1) \u2261 \u27e6 weaken \u0393\u227c\u0394\u0393 t \u27e7 \u03c1\u2032\n\nweak-eq-term t {\u03c1} {\u03c1\u2032} C =\n trans (cong \u27e6 t \u27e7 (weak-eq C)) (sym (weaken-sound t \u03c1\u2032))\n\nweaken-once : \u2200 {\u03c4 \u0393 \u03c3} {t : Term \u0393 \u03c4} {v : \u27e6 \u03c3 \u27e7} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 t \u27e7 \u03c1 \u2261 \u27e6 weaken (drop \u03c3 \u2022 \u0393\u227c\u0393) t \u27e7 (v \u2022 \u03c1)\n\nweaken-once {t = t} {v} {\u03c1} = trans\n (cong \u27e6 t \u27e7 (sym weak-id))\n (sym (weaken-sound t (v \u2022 \u03c1)))\n\nembed-correct : \u2200 {\u03c4 \u0393}\n {dt : \u0394Term \u0393 \u03c4}\n {\u03c1 : \u0394Env \u0393} {V : dt is-valid-for \u03c1}\n {\u03c1\u2032 : \u27e6 \u0394-Context \u0393 \u27e7} {C : compatible \u03c1 \u03c1\u2032} \u2192\n \u27e6 dt \u27e7\u0394 \u03c1 V \u2248 \u27e6 embed dt \u27e7 \u03c1\u2032\n\nembed-correct {dt = \u0394nat old new} = refl , refl\n\nembed-correct {dt = \u0394bag db} = refl\n\nembed-correct {dt = \u0394var x} {\u03c1} {\u03c1\u2032 = \u03c1\u2032} {C} =\n deriveVar-correct {x = x} {\u03c1} {\u03c1\u2032} {C}\n\nembed-correct {dt = \u0394add ds dt} {\u03c1} {V} {\u03c1\u2032} {C} =\n let\n s00 , s01 = \u27e6 ds \u27e7\u0394 \u03c1 (car V)\n t00 , t01 = \u27e6 dt \u27e7\u0394 \u03c1 (cadr V)\n s10 = \u27e6 embed ds \u27e7 \u03c1\u2032 \u27e6fst\u27e7\n s11 = \u27e6 embed ds \u27e7 \u03c1\u2032 \u27e6snd\u27e7\n t10 = \u27e6 embed dt \u27e7 \u03c1\u2032 \u27e6fst\u27e7\n t11 = \u27e6 embed dt \u27e7 \u03c1\u2032 \u27e6snd\u27e7\n rec-s = embed-correct {dt = ds} {\u03c1} {car V} {\u03c1\u2032} {C}\n rec-t = embed-correct {dt = dt} {\u03c1} {cadr V} {\u03c1\u2032} {C}\n in\n \n (begin\n s00 + t00\n \u2261\u27e8 cong\u2082 _+_ (proj\u2081 rec-s) (proj\u2081 rec-t) \u27e9\n s10 + t10\n \u2261\u27e8 cong\u2082 _+_\n (cong (\u03bb x \u2192 \u27e6 embed ds \u27e7 x \u27e6fst\u27e7) (sym weak-id))\n (cong (\u03bb x \u2192 \u27e6 embed dt \u27e7 x \u27e6fst\u27e7) (sym weak-id)) \u27e9\n \u27e6 embed ds \u27e7 (\u27e6 drop _ \u2022 \u0393\u227c\u0393 \u27e7 (\u27e6fst\u27e7 {\u2115} \u2022 \u03c1\u2032)) \u27e6fst\u27e7 +\n \u27e6 embed dt \u27e7 (\u27e6 drop _ \u2022 \u0393\u227c\u0393 \u27e7 (\u27e6fst\u27e7 {\u2115} \u2022 \u03c1\u2032)) \u27e6fst\u27e7\n \u2261\u27e8 cong\u2082 _+_\n (cong (\u03bb f \u2192 f \u27e6fst\u27e7) (sym (weaken-sound (embed ds) (\u27e6fst\u27e7 \u2022 \u03c1\u2032))))\n (cong (\u03bb f \u2192 f \u27e6fst\u27e7) (sym (weaken-sound (embed dt) (\u27e6fst\u27e7 \u2022 \u03c1\u2032)))) \u27e9\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (embed ds) \u27e7 (\u27e6fst\u27e7 \u2022 \u03c1\u2032) \u27e6fst\u27e7 +\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (embed dt) \u27e7 (\u27e6fst\u27e7 \u2022 \u03c1\u2032) \u27e6fst\u27e7\n \u220e)\n ,\n (begin\n s01 + t01\n \u2261\u27e8 cong\u2082 _+_ (proj\u2082 rec-s) (proj\u2082 rec-t) \u27e9\n s11 + t11\n \u2261\u27e8 cong\u2082 _+_\n (cong (\u03bb x \u2192 \u27e6 embed ds \u27e7 x \u27e6snd\u27e7) (sym weak-id))\n (cong (\u03bb x \u2192 \u27e6 embed dt \u27e7 x \u27e6snd\u27e7) (sym weak-id)) \u27e9\n \u27e6 embed ds \u27e7 (\u27e6 drop _ \u2022 \u0393\u227c\u0393 \u27e7 (\u27e6snd\u27e7 {\u2115} \u2022 \u03c1\u2032)) \u27e6snd\u27e7 +\n \u27e6 embed dt \u27e7 (\u27e6 drop _ \u2022 \u0393\u227c\u0393 \u27e7 (\u27e6snd\u27e7 {\u2115} \u2022 \u03c1\u2032)) \u27e6snd\u27e7\n \u2261\u27e8 cong\u2082 _+_\n (cong (\u03bb f \u2192 f \u27e6snd\u27e7) (sym (weaken-sound (embed ds) (\u27e6snd\u27e7 \u2022 \u03c1\u2032))))\n (cong (\u03bb f \u2192 f \u27e6snd\u27e7) (sym (weaken-sound (embed dt) (\u27e6snd\u27e7 \u2022 \u03c1\u2032)))) \u27e9\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (embed ds) \u27e7 (\u27e6snd\u27e7 \u2022 \u03c1\u2032) \u27e6snd\u27e7 +\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (embed dt) \u27e7 (\u27e6snd\u27e7 \u2022 \u03c1\u2032) \u27e6snd\u27e7\n \u220e)\n where open \u2261-Reasoning\n\nembed-correct {dt = \u0394union ds dt} {\u03c1} {V} {\u03c1\u2032} {C} = cong\u2082 _++_\n (embed-correct {dt = ds} {\u03c1} {car V} {\u03c1\u2032} {C})\n (embed-correct {dt = dt} {\u03c1} {cadr V} {\u03c1\u2032} {C})\n\nembed-correct {dt = \u0394diff ds dt} {\u03c1} {V} {\u03c1\u2032} {C} = cong\u2082 _\\\\_\n (embed-correct {dt = ds} {\u03c1} {car V} {\u03c1\u2032} {C})\n (embed-correct {dt = dt} {\u03c1} {cadr V} {\u03c1\u2032} {C})\n\nembed-correct {dt = \u0394map\u2081 s dt} {\u03c1} {V} {\u03c1\u2032} {C} = cong\u2082 mapBag\n (weak-eq-term s C)\n (embed-correct {dt = dt} {\u03c1} {car V} {\u03c1\u2032} {C})\n\nembed-correct {dt = \u0394map\u2080 s ds t dt} {\u03c1} {V} {\u03c1\u2032} {C} = cong\u2082 _\\\\_\n (cong\u2082 mapBag\n (extensionality (\u03bb v \u2192\n begin\n proj\u2082 (\u27e6 ds \u27e7\u0394 \u03c1 (car V) v (v , v) refl)\n \u2261\u27e8 proj\u2082 (embed-correct {dt = ds} {\u03c1} {car V} {\u03c1\u2032} {C}\n v (v , v) (\u03bb f \u2192 f v v) refl (refl , refl)) \u27e9\n \u27e6 embed ds \u27e7 \u03c1\u2032 v (\u03bb f \u2192 f v v) \u27e6snd\u27e7\n \u2261\u27e8 cong (\u03bb hole \u2192 hole v (\u03bb f \u2192 f v v) \u27e6snd\u27e7)\n (weaken-once {t = embed ds} {v} {\u03c1\u2032}) \u27e9\n \u27e6 weaken (drop _ \u2022 \u0393\u227c\u0393) (embed ds) \u27e7 (v \u2022 \u03c1\u2032) v (\u03bb f \u2192 f v v) \u27e6snd\u27e7\n \u220e))\n (cong\u2082 _++_\n (weak-eq-term t C)\n (embed-correct {dt = dt} {\u03c1} {cadr V} {\u03c1\u2032} {C})))\n (cong\u2082 mapBag (weak-eq-term s C) (weak-eq-term t C))\n where open \u2261-Reasoning\n\nembed-correct {dt = \u0394app ds t dt} {\u03c1} {cons vds vdt R[t,dt] _} {\u03c1\u2032} {C}\n rewrite sym (weak-eq-term t C) =\n embed-correct {dt = ds} {\u03c1} {vds} {\u03c1\u2032} {C}\n (\u27e6 t \u27e7Term (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1 vdt) (\u27e6 embed dt \u27e7Term \u03c1\u2032)\n R[t,dt] (embed-correct {dt = dt} {\u03c1} {vdt} {\u03c1\u2032} {C})\n\nembed-correct {dt = \u0394abs dt} {\u03c1} {V} {\u03c1\u2032} {C} = \u03bb w dw dw\u2032 R[w,dw] dw\u2248dw\u2032 \u2192\n embed-correct {dt = dt}\n {cons w dw R[w,dw] \u03c1} {V w dw R[w,dw]}\n {dw\u2032 \u2022 w \u2022 \u03c1\u2032} {cons refl dw\u2248dw\u2032 C tt}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"e7b75259e34cf9de11a2696f75d1384a4dc62f8d","subject":"allow the use of more natural notation in Nat","message":"allow the use of more natural notation in Nat\n","repos":"piyush-kurur\/sample-code","old_file":"agda\/Nat.agda","new_file":"agda\/Nat.agda","new_contents":"module Nat where\n\ndata \u2115 : Set where\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n\n{-# BUILTIN NATURAL \u2115 #-}\n{-# BUILTIN ZERO zero #-}\n{-# BUILTIN SUC succ #-}\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero + b = b\nsucc a + b = succ (a + b)\n\n_\u00d7_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero \u00d7 b = zero\nsucc a \u00d7 b = (a \u00d7 b) + b\n\n\nopen import Relation.Binary.PropositionalEquality\n\n0-is-right-identity-of-+ : \u2200 (n : \u2115) \u2192 n + zero \u2261 n\n0-is-right-identity-of-+ zero = refl\n0-is-right-identity-of-+ (succ n) = cong succ (0-is-right-identity-of-+ n)\n\n\n+-is-associative : \u2200 (a b c : \u2115) \u2192 a + (b + c) \u2261 (a + b) + c\n+-is-associative zero b c = refl\n+-is-associative (succ a) b c = cong succ (+-is-associative a b c)\n\n\nlemma : \u2200 (a b : \u2115) \u2192 a + succ b \u2261 succ (a + b)\nlemma zero b = refl\nlemma (succ a) b = cong succ (lemma a b)\n\nimport Relation.Binary.EqReasoning as EqR\nopen module EqNat = EqR (setoid \u2115)\n\n+-is-commutative : \u2200 (a b : \u2115) \u2192 a + b \u2261 b + a\n+-is-commutative a zero = 0-is-right-identity-of-+ a\n+-is-commutative a (succ b) =\n begin\n a + succ b\n \u2248\u27e8 lemma a b \u27e9\n succ (a + b)\n \u2248\u27e8 cong succ (+-is-commutative a b) \u27e9\n succ (b + a)\n \u2248\u27e8 refl \u27e9\n succ b + a\n \u220e\n","old_contents":"module Nat where\n\ndata \u2115 : Set where\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero + b = b\nsucc a + b = succ (a + b)\n\n_\u00d7_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero \u00d7 b = zero\nsucc a \u00d7 b = (a \u00d7 b) + b\n\n\nopen import Relation.Binary.PropositionalEquality\n\n0-is-right-identity-of-+ : \u2200 (n : \u2115) \u2192 n + zero \u2261 n\n0-is-right-identity-of-+ zero = refl\n0-is-right-identity-of-+ (succ n) = cong succ (0-is-right-identity-of-+ n)\n\n\n+-is-associative : \u2200 (a b c : \u2115) \u2192 a + (b + c) \u2261 (a + b) + c\n+-is-associative zero b c = refl\n+-is-associative (succ a) b c = cong succ (+-is-associative a b c)\n\n\nlemma : \u2200 (a b : \u2115) \u2192 a + succ b \u2261 succ (a + b)\nlemma zero b = refl\nlemma (succ a) b = cong succ (lemma a b)\n\nimport Relation.Binary.EqReasoning as EqR\nopen module EqNat = EqR (setoid \u2115)\n\n+-is-commutative : \u2200 (a b : \u2115) \u2192 a + b \u2261 b + a\n+-is-commutative a zero = 0-is-right-identity-of-+ a\n+-is-commutative a (succ b) =\n begin\n a + succ b\n \u2248\u27e8 lemma a b \u27e9\n succ (a + b)\n \u2248\u27e8 cong succ (+-is-commutative a b) \u27e9\n succ (b + a)\n \u2248\u27e8 refl \u27e9\n succ b + a\n \u220e\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"ff2b28f9855d9d8d0f36b8784508cda5ca3bb006","subject":"FLABloM: more lifting of seminearring","message":"FLABloM: more lifting of seminearring\n","repos":"DSLsofMath\/DSLsofMath","old_file":"FLABloM\/SquareMatrix.agda","new_file":"FLABloM\/SquareMatrix.agda","new_contents":"-- Matrices indexed by shape\nmodule SquareMatrix where\n\nopen import Shape\nopen import Matrix\n\n-- Square matrices have the same shape in both dimensions\nSq : Set \u2192 Shape \u2192 Set\nSq a shape = M a shape shape\n\nopen import SemiNearRingRecord\nopen import Algebra.Structures\nopen import Relation.Binary\nopen import Data.Product\n\n-- Lifting a SNR to a to a Square matrix of some shape\nSquare : SemiNearRing \u2192 Shape \u2192 SemiNearRing\nSquare snr shape = SNR\n where\n open SemiNearRing snr\n\n S = Sq s shape\n\n open Operations s _\u2219\u209b_ _+\u209b_\n renaming (_+_ to _+S_; _*_ to _\u2219S_)\n\n 0S : (r c : Shape) \u2192 M s r c\n 0S L L = One 0\u209b\n 0S L (B s s\u2081) = Row (0S L s) (0S L s\u2081)\n 0S (B r r\u2081) L = Col (0S r L) (0S r\u2081 L)\n 0S (B r r\u2081) (B s s\u2081) =\n Q (0S r s) (0S r s\u2081)\n (0S r\u2081 s) (0S r\u2081 s\u2081)\n\n \u2243S : (r c : Shape) \u2192\n M s r c \u2192 M s r c \u2192 Set\n \u2243S L L (One x) (One x\u2081) = x \u2243\u209b x\u2081\n \u2243S L (B c\u2081 c\u2082) (Row m m\u2081) (Row n n\u2081) = \u2243S L c\u2081 m n \u00d7 \u2243S L c\u2082 m\u2081 n\u2081\n \u2243S (B r\u2081 r\u2082) L (Col m m\u2081) (Col n n\u2081) = \u2243S r\u2081 L m n \u00d7 \u2243S r\u2082 L m\u2081 n\u2081\n \u2243S (B r\u2081 r\u2082) (B c\u2081 c\u2082) (Q m00 m01 m10 m11) (Q n00 n01 n10 n11) =\n \u2243S r\u2081 c\u2081 m00 n00 \u00d7 \u2243S r\u2081 c\u2082 m01 n01 \u00d7\n \u2243S r\u2082 c\u2081 m10 n10 \u00d7 \u2243S r\u2082 c\u2082 m11 n11\n\n\n _\u2243S'_ : {r c : Shape} \u2192 M s r c \u2192 M s r c \u2192 Set\n _\u2243S'_ {r} {c} m n = \u2243S r c m n\n\n\n reflS : (r c : Shape) \u2192\n {X : M s r c} \u2192 X \u2243S' X\n reflS L L {X = One x} = refl\u209b {x}\n reflS L (B c\u2081 c\u2082) {X = Row X Y} = reflS L c\u2081 , reflS L c\u2082\n reflS (B r\u2081 r\u2082) L {X = Col X Y} = reflS r\u2081 L , reflS r\u2082 L\n reflS (B r\u2081 r\u2082) (B c\u2081 c\u2082) {X = Q X Y Z W} =\n reflS r\u2081 c\u2081 , reflS r\u2081 c\u2082 ,\n reflS r\u2082 c\u2081 , reflS r\u2082 c\u2082\n\n symS : (r c : Shape) \u2192\n {i j : M s r c} \u2192 i \u2243S' j \u2192 j \u2243S' i\n symS L L {One x} {One x\u2081} p = sym\u209b p\n symS L (B c\u2081 c\u2082) {Row i\u2081 i\u2082} {Row j\u2081 j\u2082} (p , q) = symS L c\u2081 p , symS L c\u2082 q\n symS (B r\u2081 r\u2082) L {Col i\u2081 i} {Col j j\u2081} (p , q) = symS r\u2081 L p , symS r\u2082 L q\n symS (B r r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j j\u2081 j\u2082 j\u2083} (p , q , x , y) =\n symS r c\u2081 p , symS r c\u2082 q ,\n symS r\u2082 c\u2081 x , symS r\u2082 c\u2082 y\n\n transS : (r c : Shape) \u2192\n {i j k : M s r c} \u2192 i \u2243S' j \u2192 j \u2243S' k \u2192 i \u2243S' k\n transS L L {One x} {One x\u2081} {One x\u2082} p q = trans p q\n transS L (B c\u2081 c\u2082) {Row i i\u2081} {Row j j\u2081} {Row k k\u2081} (p , q) (p' , q') =\n transS L c\u2081 p p' , transS L c\u2082 q q'\n transS (B r\u2081 r\u2082) L {Col i i\u2081} {Col j j\u2081} {Col k k\u2081} (p , q) (p' , q') =\n transS r\u2081 L p p' , transS r\u2082 L q q'\n transS (B r\u2081 r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j\u2082 j j\u2083 j\u2081} {Q k k\u2081 k\u2082 k\u2083}\n (p , q , x , y) (p' , q' , x' , y') =\n transS r\u2081 c\u2081 p p' , transS r\u2081 c\u2082 q q' ,\n transS r\u2082 c\u2081 x x' , transS r\u2082 c\u2082 y y'\n\n isEquivS =\n record\n { refl = reflS shape shape\n ; sym = symS shape shape\n ; trans = transS shape shape }\n\n liftAssoc : (r c : Shape) (x y z : M s r c) \u2192 ((x +S y) +S z) \u2243S' (x +S (y +S z))\n liftAssoc L L (One x) (One y) (One z) = assoc\u209b x y z\n liftAssoc L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) (Row z z\u2081) =\n liftAssoc L c x y z , liftAssoc L c\u2081 x\u2081 y\u2081 z\u2081\n liftAssoc (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) (Col z z\u2081) =\n liftAssoc r L x y z , liftAssoc r\u2081 L x\u2081 y\u2081 z\u2081\n liftAssoc (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n (liftAssoc r c x y z) , (liftAssoc r c\u2081 x\u2081 y\u2081 z\u2081) ,\n (liftAssoc r\u2081 c x\u2082 y\u2082 z\u2082) , (liftAssoc r\u2081 c\u2081 x\u2083 y\u2083 z\u2083)\n\n lift<+> : (r c : Shape) {x y u v : M s r c} \u2192\n x \u2243S' y \u2192 u \u2243S' v \u2192 (x +S u) \u2243S' (y +S v)\n lift<+> L L {One x} {One x\u2081} {One x\u2082} {One x\u2083} p q = p <+> q\n lift<+> L (B c c\u2081) {Row x x\u2081} {Row y y\u2081} {Row u u\u2081} {Row v v\u2081} (p , p\u2081) (q , q\u2081) =\n lift<+> L c p q , lift<+> L c\u2081 p\u2081 q\u2081\n lift<+> (B r r\u2081) L {Col x x\u2081} {Col y y\u2081} {Col u u\u2081} {Col v v\u2081} (p , p\u2081) (q , q\u2081) =\n lift<+> r L p q , lift<+> r\u2081 L p\u2081 q\u2081\n lift<+> (B r r\u2081) (B c c\u2081) {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083}\n (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081 , q\u2082 , q\u2083) =\n lift<+> r c p q , lift<+> r c\u2081 p\u2081 q\u2081 ,\n lift<+> r\u2081 c p\u2082 q\u2082 , lift<+> r\u2081 c\u2081 p\u2083 q\u2083\n\n isSemgroupS =\n record\n { isEquivalence = isEquivS\n ; assoc = liftAssoc shape shape\n ; \u2219-cong = lift<+> shape shape }\n\n\n liftIdent\u02e1 : (r c : Shape) (x : M s r c) \u2192\n 0S r c +S x \u2243S' x\n liftIdent\u02e1 L L (One x) = identity\u02e1\u209b x\n liftIdent\u02e1 L (B c c\u2081) (Row x x\u2081) = liftIdent\u02e1 L c x , liftIdent\u02e1 L c\u2081 x\u2081\n liftIdent\u02e1 (B r r\u2081) L (Col x x\u2081) = liftIdent\u02e1 r L x , liftIdent\u02e1 r\u2081 L x\u2081\n liftIdent\u02e1 (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n liftIdent\u02e1 r c x , liftIdent\u02e1 r c\u2081 x\u2081 ,\n liftIdent\u02e1 r\u2081 c x\u2082 , liftIdent\u02e1 r\u2081 c\u2081 x\u2083\n\n liftComm : (r c : Shape) \u2192 (x y : M s r c) \u2192\n (x +S y) \u2243S' (y +S x)\n liftComm L L (One x) (One x\u2081) = comm\u209b x x\u2081\n liftComm L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) = (liftComm L c x y) , (liftComm L c\u2081 x\u2081 y\u2081)\n liftComm (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) = (liftComm r L x y) , (liftComm r\u2081 L x\u2081 y\u2081)\n liftComm (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) =\n liftComm r c x y , liftComm r c\u2081 x\u2081 y\u2081 ,\n liftComm r\u2081 c x\u2082 y\u2082 , liftComm r\u2081 c\u2081 x\u2083 y\u2083\n\n isCommMonS =\n record\n { isSemigroup = isSemgroupS\n ; identity\u02e1 = liftIdent\u02e1 shape shape\n ; comm = liftComm shape shape }\n\n\n -- TODO: can I use \u2243S to 'rewrite' types?\n liftZero\u02e1 : (a b c : Shape) (x : M s b c) \u2192\n let 0\u02e1 = 0S a b\n 0\u02b3 = 0S a c\n in (0\u02e1 \u2219S x) \u2243S' 0\u02b3\n liftZero\u02e1 L L L (One x) = zero\u02e1 x\n liftZero\u02e1 L L (B c c\u2081) (Row x x\u2081) = (liftZero\u02e1 L L c x) , (liftZero\u02e1 L L c\u2081 x\u2081)\n liftZero\u02e1 L (B b b\u2081) L (Col x x\u2081) = {!!}\n liftZero\u02e1 L (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) = {!!} , {!!}\n liftZero\u02e1 (B a a\u2081) L L (One x) = liftZero\u02e1 a L L (One x) , liftZero\u02e1 a\u2081 L L (One x)\n liftZero\u02e1 (B a a\u2081) L (B c c\u2081) (Row x x\u2081) =\n liftZero\u02e1 a L c x , liftZero\u02e1 a L c\u2081 x\u2081 ,\n liftZero\u02e1 a\u2081 L c x , liftZero\u02e1 a\u2081 L c\u2081 x\u2081\n liftZero\u02e1 (B a a\u2081) (B b b\u2081) L (Col x x\u2081) = {!!} , {!!}\n liftZero\u02e1 (B a a\u2081) (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n {!liftZero\u02e1 a b c x!} , ({!!} , ({!!} , {!!}))\n\n\n SNR : SemiNearRing\n SNR =\n record\n { s = S\n ; _\u2243\u209b_ = \u2243S shape shape\n ; 0\u209b = 0S shape shape\n ; _+\u209b_ = _+S_\n ; _\u2219\u209b_ = _\u2219S_\n ; isCommMon = isCommMonS\n ; zero\u02e1 = liftZero\u02e1 shape shape shape\n ; zero\u02b3 = {!!}\n ; _<\u2219>_ = {!!}\n }\n","old_contents":"-- Matrices indexed by shape\nmodule SquareMatrix where\n\nopen import Shape\nopen import Matrix\n\n-- Square matrices have the same shape in both dimensions\nSq : Set \u2192 Shape \u2192 Set\nSq a shape = M a shape shape\n\nopen import SemiNearRingRecord\nopen import Algebra.Structures\nopen import Relation.Binary\nopen import Data.Product\n\n-- Lifting a SNR to a to a Square matrix of some shape\nSquare : SemiNearRing \u2192 Shape \u2192 SemiNearRing\nSquare snr shape = SNR\n where\n open SemiNearRing snr\n\n S = Sq s shape\n\n open Operations s _\u2219\u209b_ _+\u209b_\n renaming (_+_ to _+S_; _*_ to _\u2219S_)\n\n lift0 : (r c : Shape) \u2192 M s r c\n lift0 L L = One 0\u209b\n lift0 L (B s s\u2081) = Row (lift0 L s) (lift0 L s\u2081)\n lift0 (B r r\u2081) L = Col (lift0 r L) (lift0 r\u2081 L)\n lift0 (B r r\u2081) (B s s\u2081) =\n Q (lift0 r s) (lift0 r s\u2081)\n (lift0 r\u2081 s) (lift0 r\u2081 s\u2081)\n\n 0S = lift0 shape shape\n\n lift\u2243 : (r c : Shape) \u2192\n M s r c \u2192 M s r c \u2192 Set\n lift\u2243 L L (One x) (One x\u2081) = x \u2243\u209b x\u2081\n lift\u2243 L (B c\u2081 c\u2082) (Row m m\u2081) (Row n n\u2081) = lift\u2243 L c\u2081 m n \u00d7 lift\u2243 L c\u2082 m\u2081 n\u2081\n lift\u2243 (B r\u2081 r\u2082) L (Col m m\u2081) (Col n n\u2081) = lift\u2243 r\u2081 L m n \u00d7 lift\u2243 r\u2082 L m\u2081 n\u2081\n lift\u2243 (B r\u2081 r\u2082) (B c\u2081 c\u2082) (Q m00 m01 m10 m11) (Q n00 n01 n10 n11) =\n lift\u2243 r\u2081 c\u2081 m00 n00 \u00d7 lift\u2243 r\u2081 c\u2082 m01 n01 \u00d7\n lift\u2243 r\u2082 c\u2081 m10 n10 \u00d7 lift\u2243 r\u2082 c\u2082 m11 n11\n\n _\u2243S_ = lift\u2243 shape shape\n\n liftRefl : (r c : Shape) \u2192\n {X : M s r c} \u2192 lift\u2243 r c X X\n liftRefl L L {X = One x} = refl\u209b {x}\n liftRefl L (B c\u2081 c\u2082) {X = Row X Y} = liftRefl L c\u2081 , liftRefl L c\u2082\n liftRefl (B r\u2081 r\u2082) L {X = Col X Y} = liftRefl r\u2081 L , liftRefl r\u2082 L\n liftRefl (B r\u2081 r\u2082) (B c\u2081 c\u2082) {X = Q X Y Z W} =\n liftRefl r\u2081 c\u2081 , liftRefl r\u2081 c\u2082 ,\n liftRefl r\u2082 c\u2081 , liftRefl r\u2082 c\u2082\n\n reflS = liftRefl shape shape\n\n liftSym : (r c : Shape) \u2192\n let R' = lift\u2243 r c\n in {i j : M s r c} \u2192 R' i j \u2192 R' j i\n liftSym L L {One x} {One x\u2081} p = sym\u209b p\n liftSym L (B c\u2081 c\u2082) {Row i\u2081 i\u2082} {Row j\u2081 j\u2082} (p , q) = liftSym L c\u2081 p , liftSym L c\u2082 q\n liftSym (B r\u2081 r\u2082) L {Col i\u2081 i} {Col j j\u2081} (p , q) = liftSym r\u2081 L p , liftSym r\u2082 L q\n liftSym (B r r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j j\u2081 j\u2082 j\u2083} (p , q , x , y) =\n liftSym r c\u2081 p , liftSym r c\u2082 q ,\n liftSym r\u2082 c\u2081 x , liftSym r\u2082 c\u2082 y\n\n symS : {i j : M s shape shape} \u2192 i \u2243S j \u2192 j \u2243S i\n symS p = liftSym shape shape p\n\n liftTrans : (r c : Shape) \u2192\n let R' = lift\u2243 r c\n in {i j k : M s r c} \u2192 R' i j \u2192 R' j k \u2192 R' i k\n liftTrans L L {One x} {One x\u2081} {One x\u2082} p q = trans p q\n liftTrans L (B c\u2081 c\u2082) {Row i i\u2081} {Row j j\u2081} {Row k k\u2081} (p , q) (p' , q') =\n liftTrans L c\u2081 p p' , liftTrans L c\u2082 q q'\n liftTrans (B r\u2081 r\u2082) L {Col i i\u2081} {Col j j\u2081} {Col k k\u2081} (p , q) (p' , q') =\n liftTrans r\u2081 L p p' , liftTrans r\u2082 L q q'\n liftTrans (B r\u2081 r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j\u2082 j j\u2083 j\u2081} {Q k k\u2081 k\u2082 k\u2083}\n (p , q , x , y) (p' , q' , x' , y') =\n liftTrans r\u2081 c\u2081 p p' , liftTrans r\u2081 c\u2082 q q' ,\n liftTrans r\u2082 c\u2081 x x' , liftTrans r\u2082 c\u2082 y y'\n\n transS : {i j k : M s shape shape} \u2192 i \u2243S j \u2192 j \u2243S k \u2192 i \u2243S k\n transS p q = liftTrans shape shape p q\n\n liftAssoc : (r c : Shape) (x y z : M s r c) \u2192 lift\u2243 r c ((x +S y) +S z) (x +S (y +S z))\n liftAssoc L L (One x) (One y) (One z) = assoc\u209b x y z\n liftAssoc L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) (Row z z\u2081) =\n liftAssoc L c x y z , liftAssoc L c\u2081 x\u2081 y\u2081 z\u2081\n liftAssoc (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) (Col z z\u2081) =\n liftAssoc r L x y z , liftAssoc r\u2081 L x\u2081 y\u2081 z\u2081\n liftAssoc (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n (liftAssoc r c x y z) , (liftAssoc r c\u2081 x\u2081 y\u2081 z\u2081) ,\n (liftAssoc r\u2081 c x\u2082 y\u2082 z\u2082) , (liftAssoc r\u2081 c\u2081 x\u2083 y\u2083 z\u2083)\n\n assocS : (x y z : M s shape shape) \u2192 ((x +S y) +S z) \u2243S (x +S (y +S z))\n assocS x y z = liftAssoc shape shape x y z\n\n isEquivS =\n record\n { refl = reflS\n ; sym = symS\n ; trans = transS }\n isSemgroupS =\n record\n { isEquivalence = isEquivS\n ; assoc = assocS\n ; \u2219-cong = {!!} }\n isCommMonS =\n record\n { isSemigroup = isSemgroupS\n ; identity\u02e1 = {!!}\n ; comm = {!!} }\n\n\n SNR : SemiNearRing\n SNR =\n record\n { s = S\n ; _\u2243\u209b_ = _\u2243S_\n ; 0\u209b = 0S\n ; _+\u209b_ = _+S_\n ; _\u2219\u209b_ = _\u2219S_\n ; isCommMon = isCommMonS\n ; zero\u02e1 = {!!}\n ; zero\u02b3 = {!!}\n ; _<\u2219>_ = {!!}\n }\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"710f1568c8d76da54a538508f3b3ed6a43af57f4","subject":"README","message":"README\n","repos":"crypto-agda\/crypto-agda","old_file":"README.agda","new_file":"README.agda","new_contents":"module README where\n\n-- A module for bit-vectors which is used almost\n-- everywhere in this development.\nopen import Data.Bits\n\nopen import elgamal\nopen import schnorr\nopen import generic-zero-knowledge-interactive\nopen import sum-properties\n\n-- Randomized programs\nopen import flipbased\nopen import flipbased-counting\nopen import flipbased-running\nopen import flipbased-implem\nopen import flipbased-tree\n\n-- A distance between randomized programs\nopen import program-distance\n\n-- Pure guessing game, a game in which no strategy can\n-- consistently win or consistently lose.\nopen import bit-guessing-game\n\n-- Cryptographic pseudo-random generator game.\nopen import prg\n\n-- \u201cOne time Semantic Security\u201d of the \u201cOne time pad\u201d cipher\n-- on one bit messages. In other words, \u201cxor\u201ding any bit with\n-- a random bit will look random as well.\nopen import single-bit-one-time-pad\n\n-- Ciphers, the \u201cone time Semantic Security\u201d game.\nopen import one-time-semantic-security\n\n-- A simple reduction on ciphers.\nopen import composition-sem-sec-reduction\n\n-- Tracking space and time through a restricted universe\n-- of functions.\nopen import data-universe\nopen import fun-universe\nopen import agda-fun-universe\nopen import bits-fun-universe\nopen import fin-fun-universe\nopen import const-fun-universe\nopen import cost-fun-universe\nopen import inverse-fun-universe\nopen import circuit-fun-universe\nopen import syntax-fun-universe\n\n-- TODO: Fix & restore the product of universes and ops\n\n-- Draft modules of previous attempts.\n-- There is still some code to rescue.\nopen import circuit\nopen import flipbased-tree-probas\nopen import flipbased-no-split\nopen import flipbased-product-implem\n","old_contents":"module README where\n\n-- A module for bit-vectors which is used almost\n-- everywhere in this development.\nopen import Data.Bits\n\n-- Randomized programs\nopen import flipbased\nopen import flipbased-counting\nopen import flipbased-running\nopen import flipbased-implem\nopen import flipbased-tree\n\n-- A distance between randomized programs\nopen import program-distance\n\n-- Pure guessing game, a game in which no strategy can\n-- consistently win or consistently lose.\nopen import bit-guessing-game\n\n-- Cryptographic pseudo-random generator game.\nopen import prg\n\n-- \u201cOne time Semantic Security\u201d of the \u201cOne time pad\u201d cipher\n-- on one bit messages. In other words, \u201cxor\u201ding any bit with\n-- a random bit will look random as well.\nopen import single-bit-one-time-pad\n\n-- Ciphers, the \u201cone time Semantic Security\u201d game.\nopen import one-time-semantic-security\n\n-- A simple reduction on ciphers.\nopen import composition-sem-sec-reduction\n\n-- Tracking space and time through a restricted universe\n-- of functions.\nopen import data-universe\nopen import fun-universe\nopen import agda-fun-universe\nopen import bits-fun-universe\nopen import fin-fun-universe\nopen import const-fun-universe\nopen import cost-fun-universe\nopen import inverse-fun-universe\nopen import circuit-fun-universe\nopen import syntax-fun-universe\n\n-- TODO: Fix & restore the product of universes and ops\n\n-- Draft modules of previous attempts.\n-- There is still some code to rescue.\nopen import circuit\nopen import flipbased-tree-probas\nopen import flipbased-no-split\nopen import flipbased-product-implem\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"8e1932027422fbfd5254b377ea26f80df138163c","subject":"Move parameter to module level.","message":"Move parameter to module level.\n\nAll definitions in this module take the same parameter, so moving that\nparameter to the module level makes it easier to instantiate all of the\ndefinitions at once.\n\nOld-commit-hash: c9a73b24cd02aed1ee75b46416c1893c8c37b130\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Term.agda","new_file":"Parametric\/Change\/Term.agda","new_contents":"import Parametric.Syntax.Type as Type\nimport Base.Syntax.Context as Context\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Context.Context (Type.Type Base) \u2192 Type.Type Base \u2192 Set {- of constants -})\n {\u0394base : Base \u2192 Type.Type Base}\n where\n\n-- Terms that operate on changes\n\nopen Type Base\nopen Context Type\n\nopen import Parametric.Change.Type \u0394base\nopen import Parametric.Syntax.Term Constant\n\nopen import Data.Product\n\n-- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n-- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\nlift-diff-apply :\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4) \u00d7 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-diff-apply diff apply {base \u03b9} = diff , apply\nlift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\nlift-diff :\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4)\n\nlift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\nlift-apply :\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Base.Syntax.Context as Context\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Context.Context (Type.Type Base) \u2192 Type.Type Base \u2192 Set {- of constants -})\n where\n\n-- Terms that operate on changes\n\nopen Type Base\nopen Context Type\n\nopen import Parametric.Change.Type \u0394base\nopen import Parametric.Syntax.Term Constant\n\nopen import Data.Product\n\n-- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n-- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\nlift-diff-apply : \u2200 {\u0394base : Base \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4) \u00d7 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-diff-apply diff apply {base \u03b9} = diff , apply\nlift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\nlift-diff : \u2200 {\u0394base : Base \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4)\n\nlift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\nlift-apply : \u2200 {\u0394base : Base \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"917d3d21d2860202db684184e85baa981642c3dc","subject":"Type.Identities: some more identities","message":"Type.Identities: some more identities\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Type\/Identities.agda","new_file":"lib\/Type\/Identities.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Type\n\nopen import Level.NP\nopen import HoTT\nopen import Function.NP\nopen import Function.Extensionality\n\nopen import Data.Maybe.NP using (Maybe ; just ; nothing ; maybe ; maybe\u2032 ; just-injective; Maybe^)\nopen import Data.Zero using (\ud835\udfd8 ; \ud835\udfd8-elim)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Two\nopen import Data.Fin.NP as Fin using (Fin; suc; zero; [zero:_,suc:_])\nopen import Data.Vec as Vec using (Vec; []; _\u2237_)\nopen import Data.Nat.NP as \u2115 using (\u2115 ; suc ; zero; _+_)\nopen import Data.Product.NP renaming (map to map\u00d7)\nopen import Data.Sum using (_\u228e_) renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_]; map to map\u228e)\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_ ; ap; coe; coe!; !_; _\u2219_; J ; inspect ; [_]; tr; ap\u2082; apd) renaming (refl to idp; _\u2257_ to _\u223c_; J-orig to J')\n\nmodule Type.Identities where\n\nopen Equivalences\n\n\n\nmodule _ {a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 B\u2081 : \u2605_ b}(A= : A\u2080 \u2261 A\u2081)(B= : B\u2080 \u2261 B\u2081) where\n \u00d7= : (A\u2080 \u00d7 B\u2080) \u2261 (A\u2081 \u00d7 B\u2081)\n \u00d7= = ap\u2082 _\u00d7_ A= B=\n\n \u228e= : (A\u2080 \u228e B\u2080) \u2261 (A\u2081 \u228e B\u2081)\n \u228e= = ap\u2082 _\u228e_ A= B=\n\n \u2192= : (A\u2080 \u2192 B\u2080) \u2261 (A\u2081 \u2192 B\u2081)\n \u2192= = ap\u2082 -\u2192- A= B=\n\n\ncoe\u00d7= : \u2200 {a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 B\u2081 : \u2605_ b}(A= : A\u2080 \u2261 A\u2081)(B= : B\u2080 \u2261 B\u2081){x y}\n \u2192 coe (\u00d7= A= B=) (x , y) \u2261 (coe A= x , coe B= y)\ncoe\u00d7= idp idp = idp\n\n\nmodule _ {a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 B\u2081 : \u2605_ b}(A\u2243 : A\u2080 \u2243 A\u2081)(B\u2243 : B\u2080 \u2243 B\u2081) where\n private\n module A\u2243 = Equiv A\u2243\n A\u2192 = A\u2243.\u00b7\u2192\n A\u2190 = A\u2243.\u00b7\u2190\n module B\u2243 = Equiv B\u2243\n B\u2192 = B\u2243.\u00b7\u2192\n B\u2190 = B\u2243.\u00b7\u2190\n\n {-\n \u00d7\u2243 : (A\u2080 \u00d7 B\u2080) \u2243 (A\u2081 \u00d7 B\u2081)\n \u00d7\u2243 = equiv (map\u00d7 A\u2192 B\u2192) (map\u00d7 A\u2190 B\u2190)\n (\u03bb { (x , y) \u2192 pair= (A\u2243.\u00b7\u2190-inv-r x) ({!coe-\u03b2!} \u2219 B\u2243.\u00b7\u2190-inv-r y) })\n (\u03bb { (x , y) \u2192 pair= (A\u2243.\u00b7\u2190-inv-l x) {!!} })\n -}\n\n \u228e\u2243 : (A\u2080 \u228e B\u2080) \u2243 (A\u2081 \u228e B\u2081)\n \u228e\u2243 = equiv (map\u228e A\u2192 B\u2192) (map\u228e A\u2190 B\u2190)\n [inl: (\u03bb x \u2192 ap inl (A\u2243.\u00b7\u2190-inv-r x)) ,inr: ap inr \u2218 B\u2243.\u00b7\u2190-inv-r ]\n [inl: (\u03bb x \u2192 ap inl (A\u2243.\u00b7\u2190-inv-l x)) ,inr: ap inr \u2218 B\u2243.\u00b7\u2190-inv-l ]\n\n \u2192\u2243 : {{_ : FunExt}} \u2192 (A\u2080 \u2192 B\u2080) \u2243 (A\u2081 \u2192 B\u2081)\n \u2192\u2243 = equiv (\u03bb f \u2192 B\u2192 \u2218 f \u2218 A\u2190)\n (\u03bb f \u2192 B\u2190 \u2218 f \u2218 A\u2192)\n (\u03bb f \u2192 \u03bb= (\u03bb x \u2192 B\u2243.\u00b7\u2190-inv-r _ \u2219 ap f (A\u2243.\u00b7\u2190-inv-r x))) \n (\u03bb f \u2192 \u03bb= (\u03bb x \u2192 B\u2243.\u00b7\u2190-inv-l _ \u2219 ap f (A\u2243.\u00b7\u2190-inv-l x)))\n\nmodule _ {{_ : FunExt}}{a}(A : \u2605_ a){b}{B\u2080 B\u2081 : A \u2192 \u2605_ b}(B : (x : A) \u2192 B\u2080 x \u2261 B\u2081 x) where\n \u03a3=\u2032 : \u03a3 A B\u2080 \u2261 \u03a3 A B\u2081\n \u03a3=\u2032 = ap (\u03a3 A) (\u03bb= B)\n\n \u03a0=\u2032 : \u03a0 A B\u2080 \u2261 \u03a0 A B\u2081\n \u03a0=\u2032 = ap (\u03a0 A) (\u03bb= B)\n\nmodule _ {a b}{A\u2080 : \u2605_ a}{B\u2080 : A\u2080 \u2192 \u2605_ b}{{_ : FunExt}} where\n \u03a3= : {A\u2081 : \u2605_ a}(A= : A\u2080 \u2261 A\u2081)\n {B\u2081 : A\u2081 \u2192 \u2605_ b}(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (coe A= x))\n \u2192 \u03a3 A\u2080 B\u2080 \u2261 \u03a3 A\u2081 B\u2081\n \u03a3= = J (\u03bb A\u2081 A= \u2192 {B\u2081 : A\u2081 \u2192 \u2605_ b}(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (coe A= x))\n \u2192 \u03a3 A\u2080 B\u2080 \u2261 \u03a3 A\u2081 B\u2081) (\u03a3=\u2032 _)\n -- \u03a3= idp B= = \u03a3=\u2032 _ B=\n\n \u03a0= : \u2200 {A\u2081 : \u2605_ a}(A= : A\u2080 \u2261 A\u2081)\n {B\u2081 : A\u2081 \u2192 \u2605_ b}(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (coe A= x))\n \u2192 \u03a0 A\u2080 B\u2080 \u2261 \u03a0 A\u2081 B\u2081\n \u03a0= idp B= = \u03a0=\u2032 _ B=\n\nmodule _ {a}(A : \u2605_ a){b}{B\u2080 B\u2081 : A \u2192 \u2605_ b}(B : (x : A) \u2192 B\u2080 x \u2261 B\u2081 x){{_ : FunExt}} where\n !\u03a3=\u2032 : ! (\u03a3=\u2032 A B) \u2261 \u03a3=\u2032 A (!_ \u2218 B)\n !\u03a3=\u2032 = !-ap _ (\u03bb= B) \u2219 ap (ap (\u03a3 A)) (!-\u03bb= B)\n\ncoe\u03a3=\u2032-aux : \u2200{{_ : FunExt}}{a}(A : \u2605_ a){b}{B\u2080 B\u2081 : A \u2192 \u2605_ b}(B : (x : A) \u2192 B\u2080 x \u2261 B\u2081 x){x y}\n \u2192 coe (\u03a3=\u2032 A B) (x , y) \u2261 (x , coe (ap (\u03bb f \u2192 f x) (\u03bb= B)) y)\ncoe\u03a3=\u2032-aux A B with \u03bb= B\ncoe\u03a3=\u2032-aux A B | idp = idp\n\ncoe\u03a3=\u2032 : \u2200{{_ : FunExt}}{a}(A : \u2605_ a){b}{B\u2080 B\u2081 : A \u2192 \u2605_ b}(B : (x : A) \u2192 B\u2080 x \u2261 B\u2081 x){x y}\n \u2192 coe (\u03a3=\u2032 A B) (x , y) \u2261 (x , coe (B x) y)\ncoe\u03a3=\u2032 A B = coe\u03a3=\u2032-aux A B \u2219 ap (_,_ _) (coe-same (happly (happly-\u03bb= B) _) _)\n\ncoe!\u03a3=\u2032 : \u2200{{_ : FunExt}}{a}(A : \u2605_ a){b}{B\u2080 B\u2081 : A \u2192 \u2605_ b}(B : (x : A) \u2192 B\u2080 x \u2261 B\u2081 x){x y}\n \u2192 coe! (\u03a3=\u2032 A B) (x , y) \u2261 (x , coe! (B x) y)\ncoe!\u03a3=\u2032 A B {x}{y} = coe-same (!\u03a3=\u2032 A B) _ \u2219 coe\u03a3=\u2032 A (!_ \u2218 B)\n\nmodule _ {{_ : UA}}{{_ : FunExt}}{a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 : A\u2080 \u2192 \u2605_ b}{B\u2081 : A\u2081 \u2192 \u2605_ b} where\n \u03a3\u2243 : (A\u2243 : A\u2080 \u2243 A\u2081)(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (\u2013> A\u2243 x))\n \u2192 \u03a3 A\u2080 B\u2080 \u2261 \u03a3 A\u2081 B\u2081\n \u03a3\u2243 A\u2243 B= = \u03a3= (ua A\u2243) \u03bb x \u2192 B= x \u2219 ap B\u2081 (! coe-\u03b2 A\u2243 x)\n\n \u03a0\u2243 : (A : A\u2080 \u2243 A\u2081)(B : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (\u2013> A x))\n \u2192 \u03a0 A\u2080 B\u2080 \u2261 \u03a0 A\u2081 B\u2081\n \u03a0\u2243 A B = \u03a0= (ua A) \u03bb x \u2192 B x \u2219 ap B\u2081 (! coe-\u03b2 A x)\n\n {-\nmodule _ {{_ : FunExt}}{a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 : A\u2080 \u2192 \u2605_ b}{B\u2081 : A\u2081 \u2192 \u2605_ b}(A : A\u2080 \u2243 A\u2081)(B : (x : A\u2081) \u2192 B\u2080 (<\u2013 A x) \u2243 B\u2081 x) where\n \u03a0\u2243' : (\u03a0 A\u2080 B\u2080) \u2243 (\u03a0 A\u2081 B\u2081)\n \u03a0\u2243' = equiv (\u03bb f x \u2192 \u2013> (B x) (f (<\u2013 A x)))\n (\u03bb f x \u2192 tr B\u2080 (<\u2013-inv-l A x) (<\u2013 (B (\u2013> A x)) (f (\u2013> A x))))\n (\u03bb f \u2192 \u03bb= (\u03bb x \u2192 {!apd (<\u2013-inv-l A (<\u2013 A x))!}))\n {!\u03bb f \u2192 \u03bb= (\u03bb x \u2192 <\u2013-inv-l B _ \u2219 ap f (<\u2013-inv-l A x))!}\n -}\n\nmodule _ {{_ : UA}}{{_ : FunExt}}{a}{A\u2080 A\u2081 : \u2605_ a}{b} where\n \u03a3-fst\u2243 : \u2200 (A : A\u2080 \u2243 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2080 (B \u2218 \u2013> A) \u2261 \u03a3 A\u2081 B\n \u03a3-fst\u2243 A B = \u03a3\u2243 A (\u03bb x \u2192 idp)\n\n \u03a3-fst= : \u2200 (A : A\u2080 \u2261 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2080 (B \u2218 coe A) \u2261 \u03a3 A\u2081 B\n \u03a3-fst= A = \u03a3-fst\u2243 (coe-equiv A)\n\n \u03a0-dom\u2243 : \u2200 (A : A\u2080 \u2243 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2080 (B \u2218 \u2013> A) \u2261 \u03a0 A\u2081 B\n \u03a0-dom\u2243 A B = \u03a0\u2243 A (\u03bb x \u2192 idp)\n\n \u03a0-dom= : \u2200 (A : A\u2080 \u2261 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2080 (B \u2218 coe A) \u2261 \u03a0 A\u2081 B\n \u03a0-dom= A = \u03a0-dom\u2243 (coe-equiv A)\n\n -- variations where the equiv is transported backward on the right side\n\n \u03a3-fst\u2243\u2032 : (A : A\u2081 \u2243 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2081 B \u2261 \u03a3 A\u2080 (B \u2218 <\u2013 A)\n \u03a3-fst\u2243\u2032 A B = ! \u03a3-fst\u2243 (\u2243-sym A) B\n\n \u03a3-fst=\u2032 : (A : A\u2081 \u2261 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2081 B \u2261 \u03a3 A\u2080 (B \u2218 coe! A)\n \u03a3-fst=\u2032 A = \u03a3-fst\u2243\u2032 (coe-equiv A)\n\n \u03a0-dom\u2243\u2032 : (A : A\u2081 \u2243 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2081 B \u2261 \u03a0 A\u2080 (B \u2218 <\u2013 A)\n \u03a0-dom\u2243\u2032 A B = ! \u03a0-dom\u2243 (\u2243-sym A) B\n\n \u03a0-dom=\u2032 : (A : A\u2081 \u2261 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2081 B \u2261 \u03a0 A\u2080 (B \u2218 coe! A)\n \u03a0-dom=\u2032 A = \u03a0-dom\u2243\u2032 (coe-equiv A)\n\nmodule _ {a b c} {A : \u2605_ a} {B : A \u2192 \u2605_ b} {C : \u03a3 A B \u2192 \u2605_ c} where\n \u03a0\u03a3-curry-equiv : \u03a0 (\u03a3 A B) C \u2243 ((x : A) (y : B x) \u2192 C (x , y))\n \u03a0\u03a3-curry-equiv = equiv curry uncurry (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a0\u03a3-curry : {{_ : UA}} \u2192 \u03a0 (\u03a3 A B) C \u2261 ((x : A) (y : B x) \u2192 C (x , y))\n \u03a0\u03a3-curry = ua \u03a0\u03a3-curry-equiv\n\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 : \ud835\udfda \u2243 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 = equiv [0: inl _ 1: inr _ ]\n [inl: const 0\u2082 ,inr: const 1\u2082 ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n [0: idp 1: idp ]\n\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 : {{_ : UA}} \u2192 \ud835\udfda \u2261 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 = ua \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9\n\nmodule _ {{_ : UA}}{A : \u2605}{B C : A \u2192 \u2605} where\n \u03a3\u228e-split : (\u03a3 A (\u03bb x \u2192 B x \u228e C x)) \u2261 (\u03a3 A B \u228e \u03a3 A C)\n \u03a3\u228e-split = ua (equiv (\u03bb { (x , inl y) \u2192 inl (x , y)\n ; (x , inr y) \u2192 inr (x , y) })\n (\u03bb { (inl (x , y)) \u2192 x , inl y\n ; (inr (x , y)) \u2192 x , inr y })\n (\u03bb { (inl (x , y)) \u2192 idp\n ; (inr (x , y)) \u2192 idp })\n (\u03bb { (x , inl y) \u2192 idp\n ; (x , inr y) \u2192 idp }))\n\nmodule _ {{_ : UA}}{a b c}{A : \u2605_ a}{B : \u2605_ b}{C : A \u228e B \u2192 \u2605_ c} where\n dist-\u228e-\u03a3-equiv : \u03a3 (A \u228e B) C \u2243 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u03a3 : \u03a3 (A \u228e B) C \u2261 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3 = ua dist-\u228e-\u03a3-equiv\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0-equiv : \u03a0 (A \u228e B) C \u2243 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u03a0 : \u03a0 (A \u228e B) C \u2261 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0 = ua dist-\u00d7-\u03a0-equiv\n\nmodule _ {{_ : UA}}{A B : \u2605}{C : A \u2192 \u2605}{D : B \u2192 \u2605} where\n dist-\u228e-\u03a3[] : (\u03a3 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a3 A C \u228e \u03a3 B D)\n dist-\u228e-\u03a3[] = dist-\u228e-\u03a3\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0[] : (\u03a0 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a0 A C \u00d7 \u03a0 B D)\n dist-\u00d7-\u03a0[] = dist-\u00d7-\u03a0\n\nmodule _ {{_ : UA}}{A B C : \u2605} where\n dist-\u228e-\u00d7-equiv : ((A \u228e B) \u00d7 C) \u2243 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u00d7 : ((A \u228e B) \u00d7 C) \u2261 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7 = ua dist-\u228e-\u00d7-equiv\n\n module _ {{_ : FunExt}} where\n dist-\u00d7-\u2192-equiv : ((A \u228e B) \u2192 C ) \u2243 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u2192 : ((A \u228e B) \u2192 C) \u2261 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192 = ua dist-\u00d7-\u2192-equiv\n\nmodule _ {a b c}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{C : (x : A) \u2192 B x \u2192 \u2605_ c} where\n \u03a3-assoc-equiv : (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2243 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc-equiv = equiv (\u03bb x \u2192 (fst x , fst (snd x)) , snd (snd x))\n (\u03bb x \u2192 fst (fst x) , snd (fst x) , snd x)\n (\u03bb y \u2192 idp)\n (\u03bb y \u2192 idp)\n\n \u03a3-assoc : {{_ : UA}} \u2192 (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2261 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc = ua \u03a3-assoc-equiv\n\nmodule _ {a b}{A : \u2605_ a} {B : \u2605_ b} where\n \u00d7-comm-equiv : (A \u00d7 B) \u2243 (B \u00d7 A)\n \u00d7-comm-equiv = equiv swap swap (\u03bb y \u2192 idp) (\u03bb x \u2192 idp)\n\n \u00d7-comm : {{_ : UA}} \u2192 (A \u00d7 B) \u2261 (B \u00d7 A)\n \u00d7-comm = ua \u00d7-comm-equiv\n\n \u228e-comm-equiv : (A \u228e B) \u2243 (B \u228e A)\n \u228e-comm-equiv = equiv [inl: inr ,inr: inl ]\n [inl: inr ,inr: inl ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n\n \u228e-comm : {{_ : UA}} \u2192 (A \u228e B) \u2261 (B \u228e A)\n \u228e-comm = ua \u228e-comm-equiv\n\nmodule _ {A B : \u2605}{C : A \u2192 B \u2192 \u2605} where\n \u03a0\u03a0-comm-equiv : ((x : A)(y : B) \u2192 C x y) \u2243 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm-equiv = equiv flip flip (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a0\u03a0-comm : {{_ : UA}} \u2192 ((x : A)(y : B) \u2192 C x y) \u2261 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm = ua \u03a0\u03a0-comm-equiv\n\n \u03a3\u03a3-comm-equiv : (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2243 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm-equiv = equiv (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb _ \u2192 idp)\n (\u03bb _ \u2192 idp)\n\n \u03a3\u03a3-comm : {{_ : UA}} \u2192 (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2261 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm = ua \u03a3\u03a3-comm-equiv\n\nmodule _ {a b c} {A : \u2605_ a} {B : \u2605_ b} {C : \u2605_ c} where\n \u00d7-assoc : {{_ : UA}} \u2192 (A \u00d7 (B \u00d7 C)) \u2261 ((A \u00d7 B) \u00d7 C)\n \u00d7-assoc = \u03a3-assoc\n\n \u228e-assoc-equiv : (A \u228e (B \u228e C)) \u2243 ((A \u228e B) \u228e C)\n \u228e-assoc-equiv = equiv [inl: inl \u2218 inl ,inr: [inl: inl \u2218 inr ,inr: inr ] ]\n [inl: [inl: inl ,inr: inr \u2218 inl ] ,inr: inr \u2218 inr ]\n [inl: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ]\n\n \u228e-assoc : {{_ : UA}} \u2192 (A \u228e (B \u228e C)) \u2261 ((A \u228e B) \u228e C)\n \u228e-assoc = ua \u228e-assoc-equiv\n\nmodule _ {{_ : UA}}{{_ : FunExt}}{a}(A : \ud835\udfd8 \u2192 \u2605_ a) where\n \u03a0\ud835\udfd8-uniq : \u03a0 \ud835\udfd8 A \u2261 Lift \ud835\udfd9\n \u03a0\ud835\udfd8-uniq = ua (equiv _ (\u03bb _ ()) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= (\u03bb())))\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \ud835\udfd8 \u2192 \u2605\u2080) where\n \u03a0\ud835\udfd8-uniq\u2080 : \u03a0 \ud835\udfd8 A \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq\u2080 = ua (equiv _ (\u03bb _ ()) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= (\u03bb())))\n\nmodule _ {{_ : UA}}{a}(A : \ud835\udfd9 \u2192 \u2605_ a) where\n \u03a0\ud835\udfd9-uniq : \u03a0 \ud835\udfd9 A \u2261 A _\n \u03a0\ud835\udfd9-uniq = ua (equiv (\u03bb f \u2192 f _) (\u03bb x _ \u2192 x) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\nmodule _ {{_ : UA}}{a}(A : \u2605_ a) where\n \u03a0\ud835\udfd9-uniq\u2032 : (\ud835\udfd9 \u2192 A) \u2261 A\n \u03a0\ud835\udfd9-uniq\u2032 = \u03a0\ud835\udfd9-uniq (\u03bb _ \u2192 A)\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \u2605\u2080) where\n \u03a0\ud835\udfd8-uniq\u2032 : (\ud835\udfd8 \u2192 A) \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq\u2032 = \u03a0\ud835\udfd8-uniq\u2080 (\u03bb _ \u2192 A)\n\n \ud835\udfd8\u2192A\u2261\ud835\udfd9 = \u03a0\ud835\udfd8-uniq\u2032\n\nmodule _ {{_ : FunExt}}{\u2113}(F G : \ud835\udfd8 \u2192 \u2605_ \u2113) where\n -- also by \u03a0\ud835\udfd8-uniq twice\n \u03a0\ud835\udfd8-uniq' : \u03a0 \ud835\udfd8 F \u2261 \u03a0 \ud835\udfd8 G\n \u03a0\ud835\udfd8-uniq' = \u03a0=\u2032 \ud835\udfd8 (\u03bb())\n\nmodule _ {a \u2113} {A : \ud835\udfd8 \u2192 \u2605_ a} where\n \u03a3\ud835\udfd8-lift\u2218fst-equiv : \u03a3 \ud835\udfd8 A \u2243 Lift {\u2113 = \u2113} \ud835\udfd8\n \u03a3\ud835\udfd8-lift\u2218fst-equiv = equiv (lift \u2218 fst) (\u03bb { (lift ()) }) (\u03bb { (lift ()) }) (\u03bb { (() , _) })\n\nmodule _ {a} {A : \ud835\udfd8 \u2192 \u2605_ a} {{_ : UA}} where\n \u03a3\ud835\udfd8-lift\u2218fst : \u03a3 \ud835\udfd8 A \u2261 Lift {\u2113 = a} \ud835\udfd8\n \u03a3\ud835\udfd8-lift\u2218fst = ua \u03a3\ud835\udfd8-lift\u2218fst-equiv\n\nmodule _ {A : \ud835\udfd8 \u2192 \u2605} where\n \u03a3\ud835\udfd8-fst-equiv : \u03a3 \ud835\udfd8 A \u2243 \ud835\udfd8\n \u03a3\ud835\udfd8-fst-equiv = equiv fst (\u03bb()) (\u03bb()) (\u03bb { (() , _) })\n\n \u03a3\ud835\udfd8-fst : {{_ : UA}} \u2192 \u03a3 \ud835\udfd8 A \u2261 \ud835\udfd8\n \u03a3\ud835\udfd8-fst = ua \u03a3\ud835\udfd8-fst-equiv\n\nmodule _ {a}{A : \ud835\udfd9 \u2192 \u2605_ a} where\n \u03a3\ud835\udfd9-snd-equiv : \u03a3 \ud835\udfd9 A \u2243 A _\n \u03a3\ud835\udfd9-snd-equiv = equiv snd (_,_ _) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a3\ud835\udfd9-snd : {{_ : UA}} \u2192 \u03a3 \ud835\udfd9 A \u2261 A _\n \u03a3\ud835\udfd9-snd = ua \u03a3\ud835\udfd9-snd-equiv\n\nmodule _ {A : \u2605} where\n \u228e\ud835\udfd8-inl-equiv : A \u2243 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl-equiv = equiv inl [inl: id ,inr: (\u03bb()) ] [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb()) ] (\u03bb _ \u2192 idp)\n\n module _ {{_ : UA}} where\n \u228e\ud835\udfd8-inl : A \u2261 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl = ua \u228e\ud835\udfd8-inl-equiv\n\n \ud835\udfd8\u228e-inr : A \u2261 (\ud835\udfd8 \u228e A)\n \ud835\udfd8\u228e-inr = \u228e\ud835\udfd8-inl \u2219 \u228e-comm\n\n \ud835\udfd9\u00d7-snd : (\ud835\udfd9 \u00d7 A) \u2261 A\n \ud835\udfd9\u00d7-snd = \u03a3\ud835\udfd9-snd\n\n \u00d7\ud835\udfd9-fst : (A \u00d7 \ud835\udfd9) \u2261 A\n \u00d7\ud835\udfd9-fst = \u00d7-comm \u2219 \ud835\udfd9\u00d7-snd\n\n \ud835\udfd8\u00d7-fst : (\ud835\udfd8 \u00d7 A) \u2261 \ud835\udfd8\n \ud835\udfd8\u00d7-fst = \u03a3\ud835\udfd8-fst\n\n \u00d7\ud835\udfd8-snd : (A \u00d7 \ud835\udfd8) \u2261 \ud835\udfd8\n \u00d7\ud835\udfd8-snd = \u00d7-comm \u2219 \u03a3\ud835\udfd8-fst\n\n -- old names\n A\u00d7\ud835\udfd9\u2261A = \u00d7\ud835\udfd9-fst\n \ud835\udfd9\u00d7A\u2261A = \ud835\udfd9\u00d7-snd\n \ud835\udfd8\u228eA\u2261A = \ud835\udfd8\u228e-inr\n A\u228e\ud835\udfd8\u2261A = \u228e\ud835\udfd8-inl\n \ud835\udfd8\u00d7A\u2261\ud835\udfd8 = \ud835\udfd8\u00d7-fst\n A\u00d7\ud835\udfd8\u2261\ud835\udfd8 = \u00d7\ud835\udfd8-snd\n\nmodule _ {A : \u2605}{B : A \u2192 \u2605}{C : \u03a3 A B \u2192 \u2605} where\n -- AC: Dependent axiom of choice\n -- In Type Theory it happens to be neither an axiom nor to be choosing anything.\n \u03a0\u03a3-comm-equiv : (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2243 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm-equiv = equiv (\u03bb H \u2192 fst \u2218 H , snd \u2218 H) (\u03bb H \u2192 < fst H , snd H >) (\u03bb H \u2192 idp) (\u03bb H \u2192 idp)\n\n \u03a0\u03a3-comm : {{_ : UA}}\n \u2192 (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2261 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm = ua \u03a0\u03a3-comm-equiv\n\nmodule _ {\u2113}{A : \ud835\udfda \u2192 \u2605_ \u2113}{{_ : UA}}{{_ : FunExt}} where\n \u03a3\ud835\udfda-\u228e : \u03a3 \ud835\udfda A \u2261 (A 0\u2082 \u228e A 1\u2082)\n \u03a3\ud835\udfda-\u228e = \u03a3-fst\u2243\u2032 \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 _ \u2219 dist-\u228e-\u03a3 \u2219 \u228e= \u03a3\ud835\udfd9-snd \u03a3\ud835\udfd9-snd\n\n \u03a0\ud835\udfda-\u00d7 : \u03a0 \ud835\udfda A \u2261 (A 0\u2082 \u00d7 A 1\u2082)\n \u03a0\ud835\udfda-\u00d7 = \u03a0-dom\u2243\u2032 \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 _ \u2219 dist-\u00d7-\u03a0 \u2219 \u00d7= (\u03a0\ud835\udfd9-uniq _) (\u03a0\ud835\udfd9-uniq _)\n\n \u03a0\ud835\udfdaF\u2261F\u2080\u00d7F\u2081 = \u03a0\ud835\udfda-\u00d7\n\nmodule _ {A : \u2605}{{_ : UA}}{{_ : FunExt}} where\n \u03a3\ud835\udfda-\u228e\u2032 : (\ud835\udfda \u00d7 A) \u2261 (A \u228e A)\n \u03a3\ud835\udfda-\u228e\u2032 = \u03a3\ud835\udfda-\u228e\n\n \u03a0\ud835\udfda\u2192\u00d7\u2032 : (\ud835\udfda \u2192 A) \u2261 (A \u00d7 A)\n \u03a0\ud835\udfda\u2192\u00d7\u2032 = \u03a0\ud835\udfda-\u00d7\n\nmodule _ {a}{A : \u2605_ a} where\n\n \u03a3\u2261x\u2243\ud835\udfd9 : \u2200 x \u2192 (\u03a3 A (_\u2261_ x)) \u2243 \ud835\udfd9\n \u03a3\u2261x\u2243\ud835\udfd9 x = equiv (\u03bb _ \u2192 _) (\u03bb _ \u2192 x , idp) (\u03bb _ \u2192 idp) (\u03bb p \u2192 pair= (snd p) (tr-r\u2261 (snd p) idp))\n\n \u03a3x\u2261\u2243\ud835\udfd9 : \u2200 x \u2192 (\u03a3 A (flip _\u2261_ x)) \u2243 \ud835\udfd9\n \u03a3x\u2261\u2243\ud835\udfd9 x = equiv (\u03bb _ \u2192 _) (\u03bb _ \u2192 x , idp) (\u03bb _ \u2192 idp) (\u03bb p \u2192 pair= (! snd p) ( tr-l\u2261 (! snd p) idp \u2219\n \u2219-refl (! (! (snd p))) \u2219 !-inv (snd p)))\n\nmodule _ {ab c}{A B : \u2605_ ab}{C : A \u2192 B \u2192 \u2605_ c}{{_ : UA}}{{_ : FunExt}} where\n\n \u03a0\u228e-equiv : (\u03a0 (A \u228e B) [inl: (\u03bb x \u2192 \u2200 y \u2192 C x y) ,inr: (\u03bb y \u2192 \u2200 x \u2192 C x y) ]) \u2243 ((t : \ud835\udfda)(x : A)(y : B) \u2192 C x y)\n \u03a0\u228e-equiv = equiv (\u03bb f \u2192 [0: (\u03bb x y \u2192 f (inl x) y) 1: ((\u03bb x y \u2192 f (inr y) x)) ])\n (\u03bb f \u2192 [inl: f 0\u2082 ,inr: flip (f 1\u2082) ])\n (\u03bb f \u2192 \u03bb= [0: idp 1: idp ])\n (\u03bb f \u2192 \u03bb= [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ])\n\n \u03a0\u228e : (\u03a0 (A \u228e B) [inl: (\u03bb x \u2192 \u2200 y \u2192 C x y) ,inr: (\u03bb y \u2192 \u2200 x \u2192 C x y) ]) \u2261 ((t : \ud835\udfda)(x : A)(y : B) \u2192 C x y)\n \u03a0\u228e = ua \u03a0\u228e-equiv\n\nmodule _ {ab c}{A B : \u2605_ ab}{C : \u2605_ c}{{_ : UA}}{{_ : FunExt}} where\n \u03a0\u228e\u2032 : (\u03a0 (A \u228e B) [inl: const (B \u2192 C) ,inr: const (A \u2192 C) ]) \u2261 (\ud835\udfda \u2192 A \u2192 B \u2192 C)\n \u03a0\u228e\u2032 = \u03a0\u228e\n\nmodule _ where\n\n private\n maybe-elim : {X : Set}(m : Maybe X) \u2192 m \u2262 nothing \u2192 X\n maybe-elim {X} m = maybe {B = \u03bb m' \u2192 m' \u2262 _ \u2192 _} (\u03bb x _ \u2192 x) (\u03bb e \u2192 \ud835\udfd8-elim (e idp)) m\n\n maybe-elim-just : {X : Set}(m : Maybe X)(p : m \u2262 nothing)\n \u2192 just (maybe-elim m p) \u2261 m\n maybe-elim-just (just x) p = idp\n maybe-elim-just nothing p = \ud835\udfd8-elim (p idp)\n\n maybe-elim-cong : \u2200 {X : Set}{m m' : Maybe X}{p p'} \u2192 m \u2261 m'\n \u2192 maybe-elim m p \u2261 maybe-elim m' p'\n maybe-elim-cong {m = just x} idp = idp\n maybe-elim-cong {m = nothing} {p = p} idp = \ud835\udfd8-elim (p idp)\n\n not-just : {X : Set}{x : X} \u2192 _\u2262_ {A = Maybe X} (just x) nothing\n not-just ()\n\n !!-p : \u2200 {x}{X : Set x}{x y : X}(p : x \u2261 y) \u2192 ! (! p) \u2261 p\n !!-p idp = idp\n\n !\u2219 : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 ! p \u2219 p \u2261 idp\n !\u2219 idp = idp\n\n \u2219! : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 p \u2219 ! p \u2261 idp\n \u2219! idp = idp\n\n\n record PreservePoint {A B : Set}(eq : Maybe A \u2261 Maybe B) : Set where\n field\n coe-p : \u2200 x \u2192 coe eq (just x) \u2262 nothing\n coe!-p : \u2200 x \u2192 coe! eq (just x) \u2262 nothing\n open PreservePoint\n\n nothing-to-nothing : {A B : Set}(eq : Maybe A \u2261 Maybe B)\n \u2192 coe eq nothing \u2261 nothing \u2192 PreservePoint eq\n nothing-to-nothing eq n2n = record { coe-p = \u03bb x p \u2192 not-just (coe-inj eq (p \u2219 ! n2n))\n ; coe!-p = \u03bb x p \u2192 not-just (coe-same (! !\u2219 eq) (just x)\n \u2219 ! (coe\u2218coe eq (! eq) (just x)) \u2219 ap (coe eq) p \u2219 n2n) }\n\n !-PP : \u2200 {A B : Set} {e : Maybe A \u2261 Maybe B} \u2192 PreservePoint e \u2192 PreservePoint (! e)\n !-PP {e = e} e-pp = record { coe-p = coe!-p e-pp\n ; coe!-p = \u03bb x p \u2192 coe-p e-pp x ( ap (\u03bb X \u2192 coe X (just x)) (! !!-p e ) \u2219 p) }\n\n to : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e \u2192 A \u2192 B\n to e e-pp x = maybe-elim (coe e (just x)) (coe-p e-pp x)\n\n module _{A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}{ep ep'}(e'\u2219e=id : e' \u2219 e \u2261 idp)(x : B) where\n to-to : to e ep (to e' ep' x) \u2261 x\n to-to = just-injective (ap just (maybe-elim-cong\n {m = coe e (just (maybe-elim (coe e' (just x)) _))}\n {m' = coe e (coe e' (just x))}\n {p' = \u03bb q \u2192 not-just (! p \u2219 q)} (ap (coe e) (maybe-elim-just _ _)))\n \u2219 maybe-elim-just (coe e (coe e' (just x))) (\u03bb q \u2192 not-just (! p \u2219 q))\n \u2219 p)\n where\n p : coe e (coe e' (just x)) \u2261 just x\n p = coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n\n cto : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 Maybe A \u2192 Maybe B\n cto e = maybe (maybe\u2032 just (coe e nothing) \u2218 coe e \u2218 just) nothing\n\n module _ {A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}(e'\u2219e=id : e' \u2219 e \u2261 idp) where\n cto-cto : (x : Maybe B) \u2192 cto e (cto e' x) \u2261 x\n cto-cto nothing = idp\n cto-cto (just x) with coe e' (just x) | coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n cto-cto (just x) | just x\u2081 | p = ap (maybe just (coe e nothing)) p\n cto-cto (just x) | nothing | p with coe e' nothing\n | coe\u2218coe e e' nothing \u2219 coe-same e'\u2219e=id nothing\n cto-cto (just x) | nothing | p | just y | q = ap (maybe just (coe e nothing)) q \u2219 p\n cto-cto (just x) | nothing | p | nothing | q = ! q \u2219 p\n\n module _ {A B : Set} where\n create-PP-\u2243 : Maybe A \u2261 Maybe B \u2192 Maybe A \u2243 Maybe B\n create-PP-\u2243 e = equiv (cto e)\n (cto (! e))\n (cto-cto {e = e} {e' = ! e} (!\u2219 e))\n (cto-cto {e = ! e}{e' = e} (\u2219! e))\n\n create-PP : {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 \u03a3 (Maybe A \u2261 Maybe B) PreservePoint\n create-PP eq = ua (create-PP-\u2243 eq) , nothing-to-nothing _ (coe-\u03b2 (create-PP-\u2243 eq) nothing)\n\n Maybe-injective-PP : {{_ : UA}} \u2192 (e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e\n \u2192 A \u2261 B\n Maybe-injective-PP e e-pp = ua\n (equiv (to e e-pp)\n (to (! e) (!-PP e-pp))\n (to-to {e = e} { ! e} {e-pp}{ !-PP e-pp} (!\u2219 e))\n (to-to {e = ! e} {e} { !-PP e-pp} {e-pp} (\u2219! e)))\n\n Maybe-injective : \u2200 {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 A \u2261 B\n Maybe-injective e = let (e' , e'-pp) = create-PP e in Maybe-injective-PP e' e'-pp\n\n\nmodule _ {a}{A : \u2605_ a} where\n Maybe\u2243\ud835\udfd9\u228e : Maybe A \u2243 (\ud835\udfd9 \u228e A)\n Maybe\u2243\ud835\udfd9\u228e = equiv (maybe inr (inl _)) [inl: const nothing ,inr: just ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (maybe (\u03bb x \u2192 idp) idp)\n\n Maybe\u2243Lift\ud835\udfd9\u228e : Maybe A \u2243 (Lift {\u2113 = a} \ud835\udfd9 \u228e A)\n Maybe\u2243Lift\ud835\udfd9\u228e = equiv (maybe inr (inl _))\n [inl: const nothing ,inr: just ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n (maybe (\u03bb _ \u2192 idp) idp)\n\n Vec0\u2243\ud835\udfd9 : Vec A 0 \u2243 \ud835\udfd9\n Vec0\u2243\ud835\udfd9 = equiv _ (const []) (\u03bb _ \u2192 idp) (\u03bb { [] \u2192 idp })\n\n Vec0\u2243Lift\ud835\udfd9 : Vec A 0 \u2243 Lift {\u2113 = a} \ud835\udfd9\n Vec0\u2243Lift\ud835\udfd9 = equiv _ (const []) (\u03bb _ \u2192 idp) (\u03bb { [] \u2192 idp })\n\n Vec\u2218suc\u2243\u00d7 : \u2200 {n} \u2192 Vec A (suc n) \u2243 (A \u00d7 Vec A n)\n Vec\u2218suc\u2243\u00d7 = equiv (\u03bb { (x \u2237 xs) \u2192 x , xs }) (\u03bb { (x , xs) \u2192 x \u2237 xs })\n (\u03bb { (x , xs) \u2192 idp }) (\u03bb { (x \u2237 xs) \u2192 idp })\n\n module _ {{_ : UA}} where\n\n Maybe\u2261\ud835\udfd9\u228e : Maybe A \u2261 (\ud835\udfd9 \u228e A)\n Maybe\u2261\ud835\udfd9\u228e = ua Maybe\u2243\ud835\udfd9\u228e\n\n Maybe\u2261Lift\ud835\udfd9\u228e : Maybe A \u2261 (Lift {\u2113 = a} \ud835\udfd9 \u228e A)\n Maybe\u2261Lift\ud835\udfd9\u228e = ua Maybe\u2243Lift\ud835\udfd9\u228e\n\n Vec0\u2261Lift\ud835\udfd9 : Vec A 0 \u2261 Lift {\u2113 = a} \ud835\udfd9\n Vec0\u2261Lift\ud835\udfd9 = ua Vec0\u2243Lift\ud835\udfd9\n\n Vec\u2218suc\u2261\u00d7 : \u2200 {n} \u2192 Vec A (suc n) \u2261 (A \u00d7 Vec A n)\n Vec\u2218suc\u2261\u00d7 = ua Vec\u2218suc\u2243\u00d7\n\nmodule _ {A : \u2605}{{_ : UA}} where\n Vec0\u2261\ud835\udfd9 : Vec A 0 \u2261 \ud835\udfd9\n Vec0\u2261\ud835\udfd9 = ua Vec0\u2243\ud835\udfd9\n\nFin0\u2243\ud835\udfd8 : Fin 0 \u2243 \ud835\udfd8\nFin0\u2243\ud835\udfd8 = equiv (\u03bb ()) (\u03bb ()) (\u03bb ()) (\u03bb ())\n\nFin1\u2243\ud835\udfd9 : Fin 1 \u2243 \ud835\udfd9\nFin1\u2243\ud835\udfd9 = equiv _ (\u03bb _ \u2192 zero) (\u03bb _ \u2192 idp) \u03b2\n where \u03b2 : (_ : Fin 1) \u2192 _\n \u03b2 zero = idp\n \u03b2 (suc ())\n\nmodule _ {{_ : UA}} where\n Fin0\u2261\ud835\udfd8 : Fin 0 \u2261 \ud835\udfd8\n Fin0\u2261\ud835\udfd8 = ua Fin0\u2243\ud835\udfd8\n\n Fin1\u2261\ud835\udfd9 : Fin 1 \u2261 \ud835\udfd9\n Fin1\u2261\ud835\udfd9 = ua Fin1\u2243\ud835\udfd9\n\nFin\u2218suc\u2243\ud835\udfd9\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2243 (\ud835\udfd9 \u228e Fin n)\nFin\u2218suc\u2243\ud835\udfd9\u228eFin = equiv [zero: inl _ ,suc: inr ] [inl: (\u03bb _ \u2192 zero) ,inr: suc ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n [zero: idp ,suc: (\u03bb _ \u2192 idp) ]\n\nFin\u2218suc\u2261\ud835\udfd9\u228eFin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 (\ud835\udfd9 \u228e Fin n)\nFin\u2218suc\u2261\ud835\udfd9\u228eFin = ua Fin\u2218suc\u2243\ud835\udfd9\u228eFin\n\nFin-\u228e-+ : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u228e Fin n) \u2261 Fin (m \u2115.+ n)\nFin-\u228e-+ {zero} = \u228e= Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm \u2219 ! \u228e\ud835\udfd8-inl\nFin-\u228e-+ {suc m} = \u228e= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ap (_\u228e_ \ud835\udfd9) Fin-\u228e-+ \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\n\nFin-\u00d7-* : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u00d7 Fin n) \u2261 Fin (m \u2115.* n)\nFin-\u00d7-* {zero} = \u00d7= Fin0\u2261\ud835\udfd8 idp \u2219 \u03a3\ud835\udfd8-fst \u2219 ! Fin0\u2261\ud835\udfd8\nFin-\u00d7-* {suc m} = \u00d7= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u228e-\u00d7 \u2219 \u228e= \u03a3\ud835\udfd9-snd Fin-\u00d7-* \u2219 Fin-\u228e-+\n\nFin-\u2192-^ : \u2200 {{_ : UA}}{{_ : FunExt}}{m n} \u2192 (Fin m \u2192 Fin n) \u2261 Fin (n \u2115.^ m)\nFin-\u2192-^ {zero} = \u2192= Fin0\u2261\ud835\udfd8 idp \u2219 \u03a0\ud835\udfd8-uniq\u2032 _ \u2219 \u228e\ud835\udfd8-inl \u2219 ap (_\u228e_ \ud835\udfd9) (! Fin0\u2261\ud835\udfd8)\n \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\nFin-\u2192-^ {suc m} = \u2192= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u00d7-\u2192 \u2219 \u00d7= (\u03a0\ud835\udfd9-uniq _) Fin-\u2192-^ \u2219 Fin-\u00d7-*\n\nFin\u2218suc\u2261Maybe\u2218Fin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 Maybe (Fin n)\nFin\u2218suc\u2261Maybe\u2218Fin = Fin\u2218suc\u2261\ud835\udfd9\u228eFin \u2219 ! Maybe\u2261\ud835\udfd9\u228e\n\nFin-injective : \u2200 {{_ : UA}}{m n} \u2192 Fin m \u2261 Fin n \u2192 m \u2261 n\nFin-injective {zero} {zero} Finm\u2261Finn = idp\nFin-injective {zero} {suc n} Finm\u2261Finn = \ud835\udfd8-elim (coe (! Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {zero} Finm\u2261Finn = \ud835\udfd8-elim (coe (Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {suc n} Finm\u2261Finn\n = ap suc (Fin-injective (Maybe-injective\n (! Fin\u2218suc\u2261Maybe\u2218Fin \u2219 Finm\u2261Finn \u2219 Fin\u2218suc\u2261Maybe\u2218Fin)))\n\nFin\u228e-injective : \u2200 {{_ : UA}}{A B : Set} n \u2192 (Fin n \u228e A) \u2261 (Fin n \u228e B) \u2192 A \u2261 B\nFin\u228e-injective zero f = \u228e\ud835\udfd8-inl \u2219 \u228e-comm \u2219 \u228e= (! Fin0\u2261\ud835\udfd8) idp\n \u2219 f \u2219 (\u228e= Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm) \u2219 ! \u228e\ud835\udfd8-inl\nFin\u228e-injective (suc n) f = Fin\u228e-injective n (Maybe-injective\n (Maybe\u2261\ud835\udfd9\u228e \u2219 \u228e-assoc \u2219 \u228e= (! Fin\u2218suc\u2261\ud835\udfd9\u228eFin) idp \u2219 f\n \u2219 \u228e= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ! Maybe\u2261\ud835\udfd9\u228e))\n\nLift\u2243id : \u2200 {a} {A : \u2605_ a} \u2192 Lift {a} {a} A \u2243 A\nLift\u2243id = equiv lower lift (\u03bb _ \u2192 idp) (\u03bb { (lift x) \u2192 idp })\n\nmodule _ {{_ : UA}} where\n Fin-\u2261-\u22611\u2082 : \u2200 b \u2192 Fin (\ud835\udfda\u25b9\u2115 b) \u2261 (b \u2261 1\u2082)\n Fin-\u2261-\u22611\u2082 1\u2082 = Fin1\u2261\ud835\udfd9 \u2219 ua (Is-contr-to-Is-equiv.\ud835\udfd9\u2243 (\u03a9\u2081-set-to-contr \ud835\udfda-is-set 1\u2082))\n Fin-\u2261-\u22611\u2082 0\u2082 = ua (equiv (\u03bb ()) (\u03bb ()) (\u03bb ()) (\u03bb ()))\n\n Fin-\u2261-\u22610\u2082 : \u2200 b \u2192 Fin (\ud835\udfda\u25b9\u2115 (not b)) \u2261 (b \u2261 0\u2082)\n Fin-\u2261-\u22610\u2082 b = Fin-\u2261-\u22611\u2082 (not b) \u2219 ! \u2013>-paths-equiv twist-equiv\n\n \u2713-\u2227-\u00d7 : \u2200 x y \u2192 \u2713 (x \u2227 y) \u2261 (\u2713 x \u00d7 \u2713 y)\n \u2713-\u2227-\u00d7 1\u2082 y = ! \ud835\udfd9\u00d7-snd\n \u2713-\u2227-\u00d7 0\u2082 y = ! \ud835\udfd8\u00d7-fst\n\n count-\u2261 : \u2200 {a} {A : \u2605_ a} (p : A \u2192 \ud835\udfda) x \u2192 Fin (\ud835\udfda\u25b9\u2115 (p x)) \u2261 (p x \u2261 1\u2082)\n count-\u2261 p x = Fin-\u2261-\u22611\u2082 (p x)\n\n Lift\u2261id : \u2200 {a} {A : \u2605_ a} \u2192 Lift {a} {a} A \u2261 A\n Lift\u2261id = ua Lift\u2243id\n\n \u03a0\ud835\udfd9F\u2261F : \u2200 {\u2113} {F : \ud835\udfd9 \u2192 \u2605_ \u2113} \u2192 \u03a0 \ud835\udfd9 F \u2261 F _\n \u03a0\ud835\udfd9F\u2261F = ua (equiv (\u03bb x \u2192 x _) const (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\n \ud835\udfd9\u2192A\u2261A : \u2200 {\u2113} {A : \u2605_ \u2113} \u2192 (\ud835\udfd9 \u2192 A) \u2261 A\n \ud835\udfd9\u2192A\u2261A = \u03a0\ud835\udfd9F\u2261F\n\n not-\ud835\udfda\u2261\ud835\udfda : \ud835\udfda \u2261 \ud835\udfda\n not-\ud835\udfda\u2261\ud835\udfda = twist\n\n Maybe\ud835\udfd8\u2261\ud835\udfd9 : Maybe \ud835\udfd8 \u2261 \ud835\udfd9\n Maybe\ud835\udfd8\u2261\ud835\udfd9 = Maybe\u2261\ud835\udfd9\u228e \u2219 ! \u228e\ud835\udfd8-inl\n\n Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe : \u2200 {a} {A : \u2605_ a} n \u2192 Maybe (Maybe^ n A) \u2261 Maybe^ n (Maybe A)\n Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe zero = idp\n Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe (suc n) = ap Maybe (Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe n)\n\n Maybe^\ud835\udfd8\u2261Fin : \u2200 n \u2192 Maybe^ n \ud835\udfd8 \u2261 Fin n\n Maybe^\ud835\udfd8\u2261Fin zero = ! Fin0\u2261\ud835\udfd8\n Maybe^\ud835\udfd8\u2261Fin (suc n) = ap Maybe (Maybe^\ud835\udfd8\u2261Fin n) \u2219 ! Fin\u2218suc\u2261Maybe\u2218Fin\n\n Maybe^\ud835\udfd9\u2261Fin1+ : \u2200 n \u2192 Maybe^ n \ud835\udfd9 \u2261 Fin (suc n)\n Maybe^\ud835\udfd9\u2261Fin1+ n = ap (Maybe^ n) (! Maybe\ud835\udfd8\u2261\ud835\udfd9) \u2219 ! Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe n \u2219 Maybe^\ud835\udfd8\u2261Fin (suc n)\n\n Maybe-\u228e : \u2200 {a} {A B : \u2605_ a} \u2192 (Maybe A \u228e B) \u2261 Maybe (A \u228e B)\n Maybe-\u228e {a} = \u228e= Maybe\u2261Lift\ud835\udfd9\u228e idp \u2219 ! \u228e-assoc \u2219 ! Maybe\u2261Lift\ud835\udfd9\u228e\n\n Maybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \ud835\udfd8 \u228e Maybe^ n A) \u2261 Maybe^ (m + n) A\n Maybe^-\u228e-+ zero n = ! \ud835\udfd8\u228e-inr\n Maybe^-\u228e-+ (suc m) n = Maybe-\u228e \u2219 ap Maybe (Maybe^-\u228e-+ m n)\n\nmodule _ {A : Set} {p q : A \u2192 \ud835\udfda} where\n \u03a3AP\u00acQ : Set\n \u03a3AP\u00acQ = \u03a3 A (\u03bb x \u2192 p x \u2261 1\u2082 \u00d7 q x \u2261 0\u2082)\n\n \u03a3A\u00acPQ : Set\n \u03a3A\u00acPQ = \u03a3 A (\u03bb x \u2192 p x \u2261 0\u2082 \u00d7 q x \u2261 1\u2082)\n\n module EquivalentSubsets (e : \u03a3AP\u00acQ \u2261 \u03a3A\u00acPQ) where\n\n f' : \u03a3AP\u00acQ \u2192 \u03a3A\u00acPQ\n f' = coe e\n\n f-1' : \u03a3A\u00acPQ \u2192 \u03a3AP\u00acQ\n f-1' = coe! e\n\n f-1f' : \u2200 x \u2192 f-1' (f' x) \u2261 x\n f-1f' = coe!-inv-l e\n\n ff-1' : \u2200 x \u2192 f' (f-1' x) \u2261 x\n ff-1' = coe!-inv-r e\n\n f : (x : A) \u2192 p x \u2261 1\u2082 \u2192 q x \u2261 0\u2082 \u2192 \u03a3A\u00acPQ\n f x px qx = f' (x , (px , qx))\n\n f-1 : (x : A) \u2192 p x \u2261 0\u2082 \u2192 q x \u2261 1\u2082 \u2192 \u03a3AP\u00acQ\n f-1 x px qx = f-1' (x , (px , qx))\n\n f-1f : \u2200 x px nqx \u2192\n let y = snd (f x px nqx) in fst (f-1 (fst (f x px nqx)) (fst y) (snd y)) \u2261 x\n f-1f x px nqx = \u2261.cong fst (f-1f' (x , (px , nqx)))\n\n ff-1 : \u2200 x px nqx \u2192\n let y = snd (f-1 x px nqx) in fst (f (fst (f-1 x px nqx)) (fst y) (snd y)) \u2261 x\n ff-1 x px nqx = \u2261.cong fst (ff-1' (x , (px , nqx)))\n\n \u03c0' : (x : A) (px qx : \ud835\udfda) \u2192 p x \u2261 px \u2192 q x \u2261 qx \u2192 A\n \u03c0' x 1\u2082 1\u2082 px qx = x\n \u03c0' x 1\u2082 0\u2082 px qx = fst (f x px qx)\n \u03c0' x 0\u2082 1\u2082 px qx = fst (f-1 x px qx)\n \u03c0' x 0\u2082 0\u2082 px qx = x\n\n \u03c0 : A \u2192 A\n \u03c0 x = \u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n 0\u22621 : 0\u2082 \u2262 1\u2082\n 0\u22621 ()\n\n \u03c001 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px0 : p x \u2261 0\u2082) (qx1 : q x \u2261 1\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 0\u2082 1\u2082 px0 qx1\n \u03c001 x 1\u2082 _ ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym px0) ppx))\n \u03c001 x 0\u2082 1\u2082 ppx qqx px0 qx1 = \u2261.ap\u2082 (\u03bb z1 z2 \u2192 fst (f-1 x z1 z2)) (UIP-set \ud835\udfda-is-set ppx px0) (UIP-set \ud835\udfda-is-set qqx qx1)\n \u03c001 x 0\u2082 0\u2082 ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qqx) qx1))\n\n \u03c010 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px1 : p x \u2261 1\u2082) (qx0 : q x \u2261 0\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 1\u2082 0\u2082 px1 qx0\n \u03c010 x 0\u2082 _ ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym ppx) px1))\n \u03c010 x 1\u2082 0\u2082 ppx qqx px1 qx0 = \u2261.ap\u2082 (\u03bb z1 z2 \u2192 fst (f x z1 z2)) (UIP-set \ud835\udfda-is-set ppx px1) (UIP-set \ud835\udfda-is-set qqx qx0)\n \u03c010 x 1\u2082 1\u2082 ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qx0) qqx))\n\n \u03c0'bb : \u2200 {b} x (px : p x \u2261 b) (qx : q x \u2261 b) ppx qqx ([ppx] : p x \u2261 ppx) ([qqx] : q x \u2261 qqx) \u2192 \u03c0' x ppx qqx [ppx] [qqx] \u2261 x\n \u03c0'bb x px qx 1\u2082 1\u2082 [ppx] [qqx] = \u2261.refl\n \u03c0'bb x px qx 1\u2082 0\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [qqx]) (\u2261.trans qx (\u2261.trans (\u2261.sym px) [ppx]))))\n \u03c0'bb x px qx 0\u2082 1\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [ppx]) (\u2261.trans px (\u2261.trans (\u2261.sym qx) [qqx]))))\n \u03c0'bb x px qx 0\u2082 0\u2082 [ppx] [qqx] = \u2261.refl\n\n \u03c0\u03c0' : \u2200 x px qx [px] [qx] \u2192 let y = (\u03c0' x px qx [px] [qx]) in \u03c0' y (p y) (q y) \u2261.refl \u2261.refl \u2261 x\n \u03c0\u03c0' x 1\u2082 1\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n \u03c0\u03c0' x 1\u2082 0\u2082 px qx = let fx = f x px qx in let pfx = fst (snd fx) in let qfx = snd (snd fx) in \u2261.trans (\u03c001 (fst fx) (p (fst fx)) (q (fst fx)) \u2261.refl \u2261.refl pfx qfx) (f-1f x px qx)\n \u03c0\u03c0' x 0\u2082 1\u2082 px qx = let fx = f-1 x px qx in let pfx = fst (snd fx) in let qfx = snd (snd fx) in \u2261.trans (\u03c010 (fst fx) (p (fst fx)) (q (fst fx)) \u2261.refl \u2261.refl pfx qfx) (ff-1 x px qx)\n \u03c0\u03c0' x 0\u2082 0\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n\n \u03c0\u03c0 : \u2200 x \u2192 \u03c0 (\u03c0 x) \u2261 x\n \u03c0\u03c0 x = \u03c0\u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n prop' : \u2200 px qx x ([px] : p x \u2261 px) ([qx] : q x \u2261 qx) \u2192 q (\u03c0' x px qx [px] [qx]) \u2261 px\n prop' 1\u2082 1\u2082 x px qx = qx\n prop' 1\u2082 0\u2082 x px qx = snd (snd (f x px qx))\n prop' 0\u2082 1\u2082 x px qx = snd (snd (f-1 x px qx))\n prop' 0\u2082 0\u2082 x px qx = qx\n\n prop'' : \u2200 x \u2192 p x \u2261 q (\u03c0 x)\n prop'' x = \u2261.sym (prop' (p x) (q x) x \u2261.refl \u2261.refl)\n\n prop : {{_ : FunExt}} \u2192 p \u2261 q \u2218 \u03c0\n prop = \u03bb= prop''\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Type\n\nopen import Level.NP\nopen import HoTT\nopen import Function.NP\nopen import Function.Extensionality\n\nopen import Data.Maybe.NP using (Maybe ; just ; nothing ; maybe ; maybe\u2032 ; just-injective; Maybe^)\nopen import Data.Zero using (\ud835\udfd8 ; \ud835\udfd8-elim)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Two\nopen import Data.Fin.NP as Fin using (Fin; suc; zero; [zero:_,suc:_])\nopen import Data.Nat.NP as \u2115 using (\u2115 ; suc ; zero; _+_)\nopen import Data.Product.NP renaming (map to map\u00d7)\nopen import Data.Sum using (_\u228e_) renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_]; map to map\u228e)\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_ ; ap; coe; coe!; !_; _\u2219_; J ; inspect ; [_]; tr; ap\u2082; apd) renaming (refl to idp; _\u2257_ to _\u223c_; J-orig to J')\n\nmodule Type.Identities where\n\nopen Equivalences\n\n\n\nmodule _ {a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 B\u2081 : \u2605_ b}(A= : A\u2080 \u2261 A\u2081)(B= : B\u2080 \u2261 B\u2081) where\n \u00d7= : (A\u2080 \u00d7 B\u2080) \u2261 (A\u2081 \u00d7 B\u2081)\n \u00d7= = ap\u2082 _\u00d7_ A= B=\n\n \u228e= : (A\u2080 \u228e B\u2080) \u2261 (A\u2081 \u228e B\u2081)\n \u228e= = ap\u2082 _\u228e_ A= B=\n\n \u2192= : (A\u2080 \u2192 B\u2080) \u2261 (A\u2081 \u2192 B\u2081)\n \u2192= = ap\u2082 -\u2192- A= B=\n\n\ncoe\u00d7= : \u2200 {a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 B\u2081 : \u2605_ b}(A= : A\u2080 \u2261 A\u2081)(B= : B\u2080 \u2261 B\u2081){x y}\n \u2192 coe (\u00d7= A= B=) (x , y) \u2261 (coe A= x , coe B= y)\ncoe\u00d7= idp idp = idp\n\n\nmodule _ {a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 B\u2081 : \u2605_ b}(A\u2243 : A\u2080 \u2243 A\u2081)(B\u2243 : B\u2080 \u2243 B\u2081) where\n{-\n \u00d7\u2243 : (A\u2080 \u00d7 B\u2080) \u2243 (A\u2081 \u00d7 B\u2081)\n \u00d7\u2243 = equiv (map\u00d7 (\u2013> A\u2243) (\u2013> B\u2243)) (map\u00d7 (<\u2013 A\u2243) (<\u2013 B\u2243))\n (\u03bb y \u2192 pair= (<\u2013-inv-r A\u2243 (fst y)) ({!!} \u2219 <\u2013-inv-r B\u2243 (snd y)))\n {!!}\n -}\n\n \u228e\u2243 : (A\u2080 \u228e B\u2080) \u2243 (A\u2081 \u228e B\u2081)\n \u228e\u2243 = equiv (map\u228e (\u2013> A\u2243) (\u2013> B\u2243)) (map\u228e (<\u2013 A\u2243) (<\u2013 B\u2243))\n [inl: (\u03bb x \u2192 ap inl (<\u2013-inv-r A\u2243 x)) ,inr: ap inr \u2218 <\u2013-inv-r B\u2243 ]\n [inl: (\u03bb x \u2192 ap inl (<\u2013-inv-l A\u2243 x)) ,inr: ap inr \u2218 <\u2013-inv-l B\u2243 ]\n\n \u2192\u2243 : {{_ : FunExt}} \u2192 (A\u2080 \u2192 B\u2080) \u2243 (A\u2081 \u2192 B\u2081)\n \u2192\u2243 = equiv (\u03bb f \u2192 \u2013> B\u2243 \u2218 f \u2218 <\u2013 A\u2243)\n (\u03bb f \u2192 <\u2013 B\u2243 \u2218 f \u2218 \u2013> A\u2243)\n (\u03bb f \u2192 \u03bb= (\u03bb x \u2192 <\u2013-inv-r B\u2243 _ \u2219 ap f (<\u2013-inv-r A\u2243 x)))\n (\u03bb f \u2192 \u03bb= (\u03bb x \u2192 <\u2013-inv-l B\u2243 _ \u2219 ap f (<\u2013-inv-l A\u2243 x)))\n\nmodule _ {{_ : FunExt}}{a}(A : \u2605_ a){b}{B\u2080 B\u2081 : A \u2192 \u2605_ b}(B : (x : A) \u2192 B\u2080 x \u2261 B\u2081 x) where\n \u03a3=\u2032 : \u03a3 A B\u2080 \u2261 \u03a3 A B\u2081\n \u03a3=\u2032 = ap (\u03a3 A) (\u03bb= B)\n\n \u03a0=\u2032 : \u03a0 A B\u2080 \u2261 \u03a0 A B\u2081\n \u03a0=\u2032 = ap (\u03a0 A) (\u03bb= B)\n\nmodule _ {a b}{A\u2080 : \u2605_ a}{B\u2080 : A\u2080 \u2192 \u2605_ b}{{_ : FunExt}} where\n \u03a3= : {A\u2081 : \u2605_ a}(A= : A\u2080 \u2261 A\u2081)\n {B\u2081 : A\u2081 \u2192 \u2605_ b}(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (coe A= x))\n \u2192 \u03a3 A\u2080 B\u2080 \u2261 \u03a3 A\u2081 B\u2081\n \u03a3= = J (\u03bb A\u2081 A= \u2192 {B\u2081 : A\u2081 \u2192 \u2605_ b}(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (coe A= x))\n \u2192 \u03a3 A\u2080 B\u2080 \u2261 \u03a3 A\u2081 B\u2081) (\u03a3=\u2032 _)\n -- \u03a3= idp B= = \u03a3=\u2032 _ B=\n\n \u03a0= : \u2200 {A\u2081 : \u2605_ a}(A= : A\u2080 \u2261 A\u2081)\n {B\u2081 : A\u2081 \u2192 \u2605_ b}(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (coe A= x))\n \u2192 \u03a0 A\u2080 B\u2080 \u2261 \u03a0 A\u2081 B\u2081\n \u03a0= idp B= = \u03a0=\u2032 _ B=\n\nmodule _ {a}(A : \u2605_ a){b}{B\u2080 B\u2081 : A \u2192 \u2605_ b}(B : (x : A) \u2192 B\u2080 x \u2261 B\u2081 x){{_ : FunExt}} where\n !\u03a3=\u2032 : ! (\u03a3=\u2032 A B) \u2261 \u03a3=\u2032 A (!_ \u2218 B)\n !\u03a3=\u2032 = !-ap _ (\u03bb= B) \u2219 ap (ap (\u03a3 A)) (!-\u03bb= B)\n\ncoe\u03a3=\u2032-aux : \u2200{{_ : FunExt}}{a}(A : \u2605_ a){b}{B\u2080 B\u2081 : A \u2192 \u2605_ b}(B : (x : A) \u2192 B\u2080 x \u2261 B\u2081 x){x y}\n \u2192 coe (\u03a3=\u2032 A B) (x , y) \u2261 (x , coe (ap (\u03bb f \u2192 f x) (\u03bb= B)) y)\ncoe\u03a3=\u2032-aux A B with \u03bb= B\ncoe\u03a3=\u2032-aux A B | idp = idp\n\ncoe\u03a3=\u2032 : \u2200{{_ : FunExt}}{a}(A : \u2605_ a){b}{B\u2080 B\u2081 : A \u2192 \u2605_ b}(B : (x : A) \u2192 B\u2080 x \u2261 B\u2081 x){x y}\n \u2192 coe (\u03a3=\u2032 A B) (x , y) \u2261 (x , coe (B x) y)\ncoe\u03a3=\u2032 A B = coe\u03a3=\u2032-aux A B \u2219 ap (_,_ _) (coe-same (happly (happly-\u03bb= B) _) _)\n\ncoe!\u03a3=\u2032 : \u2200{{_ : FunExt}}{a}(A : \u2605_ a){b}{B\u2080 B\u2081 : A \u2192 \u2605_ b}(B : (x : A) \u2192 B\u2080 x \u2261 B\u2081 x){x y}\n \u2192 coe! (\u03a3=\u2032 A B) (x , y) \u2261 (x , coe! (B x) y)\ncoe!\u03a3=\u2032 A B {x}{y} = coe-same (!\u03a3=\u2032 A B) _ \u2219 coe\u03a3=\u2032 A (!_ \u2218 B)\n\nmodule _ {{_ : UA}}{{_ : FunExt}}{a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 : A\u2080 \u2192 \u2605_ b}{B\u2081 : A\u2081 \u2192 \u2605_ b} where\n \u03a3\u2243 : (A\u2243 : A\u2080 \u2243 A\u2081)(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (\u2013> A\u2243 x))\n \u2192 \u03a3 A\u2080 B\u2080 \u2261 \u03a3 A\u2081 B\u2081\n \u03a3\u2243 A\u2243 B= = \u03a3= (ua A\u2243) \u03bb x \u2192 B= x \u2219 ap B\u2081 (! coe-\u03b2 A\u2243 x)\n\n \u03a0\u2243 : (A : A\u2080 \u2243 A\u2081)(B : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (\u2013> A x))\n \u2192 \u03a0 A\u2080 B\u2080 \u2261 \u03a0 A\u2081 B\u2081\n \u03a0\u2243 A B = \u03a0= (ua A) \u03bb x \u2192 B x \u2219 ap B\u2081 (! coe-\u03b2 A x)\n\n {-\nmodule _ {{_ : FunExt}}{a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 : A\u2080 \u2192 \u2605_ b}{B\u2081 : A\u2081 \u2192 \u2605_ b}(A : A\u2080 \u2243 A\u2081)(B : (x : A\u2081) \u2192 B\u2080 (<\u2013 A x) \u2243 B\u2081 x) where\n \u03a0\u2243' : (\u03a0 A\u2080 B\u2080) \u2243 (\u03a0 A\u2081 B\u2081)\n \u03a0\u2243' = equiv (\u03bb f x \u2192 \u2013> (B x) (f (<\u2013 A x)))\n (\u03bb f x \u2192 tr B\u2080 (<\u2013-inv-l A x) (<\u2013 (B (\u2013> A x)) (f (\u2013> A x))))\n (\u03bb f \u2192 \u03bb= (\u03bb x \u2192 {!apd (<\u2013-inv-l A (<\u2013 A x))!}))\n {!\u03bb f \u2192 \u03bb= (\u03bb x \u2192 <\u2013-inv-l B _ \u2219 ap f (<\u2013-inv-l A x))!}\n -}\n\nmodule _ {{_ : UA}}{{_ : FunExt}}{a}{A\u2080 A\u2081 : \u2605_ a}{b} where\n \u03a3-fst\u2243 : \u2200 (A : A\u2080 \u2243 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2080 (B \u2218 \u2013> A) \u2261 \u03a3 A\u2081 B\n \u03a3-fst\u2243 A B = \u03a3\u2243 A (\u03bb x \u2192 idp)\n\n \u03a3-fst= : \u2200 (A : A\u2080 \u2261 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2080 (B \u2218 coe A) \u2261 \u03a3 A\u2081 B\n \u03a3-fst= A = \u03a3-fst\u2243 (coe-equiv A)\n\n \u03a0-dom\u2243 : \u2200 (A : A\u2080 \u2243 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2080 (B \u2218 \u2013> A) \u2261 \u03a0 A\u2081 B\n \u03a0-dom\u2243 A B = \u03a0\u2243 A (\u03bb x \u2192 idp)\n\n \u03a0-dom= : \u2200 (A : A\u2080 \u2261 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2080 (B \u2218 coe A) \u2261 \u03a0 A\u2081 B\n \u03a0-dom= A = \u03a0-dom\u2243 (coe-equiv A)\n\n -- variations where the equiv is transported backward on the right side\n\n \u03a3-fst\u2243\u2032 : (A : A\u2081 \u2243 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2081 B \u2261 \u03a3 A\u2080 (B \u2218 <\u2013 A)\n \u03a3-fst\u2243\u2032 A B = ! \u03a3-fst\u2243 (\u2243-sym A) B\n\n \u03a3-fst=\u2032 : (A : A\u2081 \u2261 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2081 B \u2261 \u03a3 A\u2080 (B \u2218 coe! A)\n \u03a3-fst=\u2032 A = \u03a3-fst\u2243\u2032 (coe-equiv A)\n\n \u03a0-dom\u2243\u2032 : (A : A\u2081 \u2243 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2081 B \u2261 \u03a0 A\u2080 (B \u2218 <\u2013 A)\n \u03a0-dom\u2243\u2032 A B = ! \u03a0-dom\u2243 (\u2243-sym A) B\n\n \u03a0-dom=\u2032 : (A : A\u2081 \u2261 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2081 B \u2261 \u03a0 A\u2080 (B \u2218 coe! A)\n \u03a0-dom=\u2032 A = \u03a0-dom\u2243\u2032 (coe-equiv A)\n\nmodule _ {a b c} {A : \u2605_ a} {B : A \u2192 \u2605_ b} {C : \u03a3 A B \u2192 \u2605_ c} where\n \u03a0\u03a3-curry-equiv : \u03a0 (\u03a3 A B) C \u2243 ((x : A) (y : B x) \u2192 C (x , y))\n \u03a0\u03a3-curry-equiv = equiv curry uncurry (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a0\u03a3-curry : {{_ : UA}} \u2192 \u03a0 (\u03a3 A B) C \u2261 ((x : A) (y : B x) \u2192 C (x , y))\n \u03a0\u03a3-curry = ua \u03a0\u03a3-curry-equiv\n\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 : \ud835\udfda \u2243 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 = equiv [0: inl _ 1: inr _ ]\n [inl: const 0\u2082 ,inr: const 1\u2082 ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n [0: idp 1: idp ]\n\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 : {{_ : UA}} \u2192 \ud835\udfda \u2261 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 = ua \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9\n\nmodule _ {{_ : UA}}{A : \u2605}{B C : A \u2192 \u2605} where\n \u03a3\u228e-split : (\u03a3 A (\u03bb x \u2192 B x \u228e C x)) \u2261 (\u03a3 A B \u228e \u03a3 A C)\n \u03a3\u228e-split = ua (equiv (\u03bb { (x , inl y) \u2192 inl (x , y)\n ; (x , inr y) \u2192 inr (x , y) })\n (\u03bb { (inl (x , y)) \u2192 x , inl y\n ; (inr (x , y)) \u2192 x , inr y })\n (\u03bb { (inl (x , y)) \u2192 idp\n ; (inr (x , y)) \u2192 idp })\n (\u03bb { (x , inl y) \u2192 idp\n ; (x , inr y) \u2192 idp }))\n\nmodule _ {{_ : UA}}{a b c}{A : \u2605_ a}{B : \u2605_ b}{C : A \u228e B \u2192 \u2605_ c} where\n dist-\u228e-\u03a3-equiv : \u03a3 (A \u228e B) C \u2243 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u03a3 : \u03a3 (A \u228e B) C \u2261 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3 = ua dist-\u228e-\u03a3-equiv\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0-equiv : \u03a0 (A \u228e B) C \u2243 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u03a0 : \u03a0 (A \u228e B) C \u2261 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0 = ua dist-\u00d7-\u03a0-equiv\n\nmodule _ {{_ : UA}}{A B : \u2605}{C : A \u2192 \u2605}{D : B \u2192 \u2605} where\n dist-\u228e-\u03a3[] : (\u03a3 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a3 A C \u228e \u03a3 B D)\n dist-\u228e-\u03a3[] = dist-\u228e-\u03a3\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0[] : (\u03a0 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a0 A C \u00d7 \u03a0 B D)\n dist-\u00d7-\u03a0[] = dist-\u00d7-\u03a0\n\nmodule _ {{_ : UA}}{A B C : \u2605} where\n dist-\u228e-\u00d7-equiv : ((A \u228e B) \u00d7 C) \u2243 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u00d7 : ((A \u228e B) \u00d7 C) \u2261 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7 = ua dist-\u228e-\u00d7-equiv\n\n module _ {{_ : FunExt}} where\n dist-\u00d7-\u2192-equiv : ((A \u228e B) \u2192 C ) \u2243 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u2192 : ((A \u228e B) \u2192 C) \u2261 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192 = ua dist-\u00d7-\u2192-equiv\n\nmodule _ {a b c}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{C : (x : A) \u2192 B x \u2192 \u2605_ c} where\n \u03a3-assoc-equiv : (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2243 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc-equiv = equiv (\u03bb x \u2192 (fst x , fst (snd x)) , snd (snd x))\n (\u03bb x \u2192 fst (fst x) , snd (fst x) , snd x)\n (\u03bb y \u2192 idp)\n (\u03bb y \u2192 idp)\n\n \u03a3-assoc : {{_ : UA}} \u2192 (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2261 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc = ua \u03a3-assoc-equiv\n\nmodule _ {a b}{A : \u2605_ a} {B : \u2605_ b} where\n \u00d7-comm-equiv : (A \u00d7 B) \u2243 (B \u00d7 A)\n \u00d7-comm-equiv = equiv swap swap (\u03bb y \u2192 idp) (\u03bb x \u2192 idp)\n\n \u00d7-comm : {{_ : UA}} \u2192 (A \u00d7 B) \u2261 (B \u00d7 A)\n \u00d7-comm = ua \u00d7-comm-equiv\n\n \u228e-comm-equiv : (A \u228e B) \u2243 (B \u228e A)\n \u228e-comm-equiv = equiv [inl: inr ,inr: inl ]\n [inl: inr ,inr: inl ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n\n \u228e-comm : {{_ : UA}} \u2192 (A \u228e B) \u2261 (B \u228e A)\n \u228e-comm = ua \u228e-comm-equiv\n\nmodule _ {A B : \u2605}{C : A \u2192 B \u2192 \u2605} where\n \u03a0\u03a0-comm-equiv : ((x : A)(y : B) \u2192 C x y) \u2243 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm-equiv = equiv flip flip (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a0\u03a0-comm : {{_ : UA}} \u2192 ((x : A)(y : B) \u2192 C x y) \u2261 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm = ua \u03a0\u03a0-comm-equiv\n\n \u03a3\u03a3-comm-equiv : (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2243 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm-equiv = equiv (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb _ \u2192 idp)\n (\u03bb _ \u2192 idp)\n\n \u03a3\u03a3-comm : {{_ : UA}} \u2192 (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2261 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm = ua \u03a3\u03a3-comm-equiv\n\nmodule _ {a b c} {A : \u2605_ a} {B : \u2605_ b} {C : \u2605_ c} where\n \u00d7-assoc : {{_ : UA}} \u2192 (A \u00d7 (B \u00d7 C)) \u2261 ((A \u00d7 B) \u00d7 C)\n \u00d7-assoc = \u03a3-assoc\n\n \u228e-assoc-equiv : (A \u228e (B \u228e C)) \u2243 ((A \u228e B) \u228e C)\n \u228e-assoc-equiv = equiv [inl: inl \u2218 inl ,inr: [inl: inl \u2218 inr ,inr: inr ] ]\n [inl: [inl: inl ,inr: inr \u2218 inl ] ,inr: inr \u2218 inr ]\n [inl: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ]\n\n \u228e-assoc : {{_ : UA}} \u2192 (A \u228e (B \u228e C)) \u2261 ((A \u228e B) \u228e C)\n \u228e-assoc = ua \u228e-assoc-equiv\n\nmodule _ {{_ : UA}}{{_ : FunExt}}{a}(A : \ud835\udfd8 \u2192 \u2605_ a) where\n \u03a0\ud835\udfd8-uniq : \u03a0 \ud835\udfd8 A \u2261 Lift \ud835\udfd9\n \u03a0\ud835\udfd8-uniq = ua (equiv _ (\u03bb _ ()) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= (\u03bb())))\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \ud835\udfd8 \u2192 \u2605\u2080) where\n \u03a0\ud835\udfd8-uniq\u2080 : \u03a0 \ud835\udfd8 A \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq\u2080 = ua (equiv _ (\u03bb _ ()) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= (\u03bb())))\n\nmodule _ {{_ : UA}}{a}(A : \ud835\udfd9 \u2192 \u2605_ a) where\n \u03a0\ud835\udfd9-uniq : \u03a0 \ud835\udfd9 A \u2261 A _\n \u03a0\ud835\udfd9-uniq = ua (equiv (\u03bb f \u2192 f _) (\u03bb x _ \u2192 x) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\nmodule _ {{_ : UA}}{a}(A : \u2605_ a) where\n \u03a0\ud835\udfd9-uniq\u2032 : (\ud835\udfd9 \u2192 A) \u2261 A\n \u03a0\ud835\udfd9-uniq\u2032 = \u03a0\ud835\udfd9-uniq (\u03bb _ \u2192 A)\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \u2605\u2080) where\n \u03a0\ud835\udfd8-uniq\u2032 : (\ud835\udfd8 \u2192 A) \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq\u2032 = \u03a0\ud835\udfd8-uniq\u2080 (\u03bb _ \u2192 A)\n\n \ud835\udfd8\u2192A\u2261\ud835\udfd9 = \u03a0\ud835\udfd8-uniq\u2032\n\nmodule _ {{_ : FunExt}}{\u2113}(F G : \ud835\udfd8 \u2192 \u2605_ \u2113) where\n -- also by \u03a0\ud835\udfd8-uniq twice\n \u03a0\ud835\udfd8-uniq' : \u03a0 \ud835\udfd8 F \u2261 \u03a0 \ud835\udfd8 G\n \u03a0\ud835\udfd8-uniq' = \u03a0=\u2032 \ud835\udfd8 (\u03bb())\n\nmodule _ {a \u2113} {A : \ud835\udfd8 \u2192 \u2605_ a} where\n \u03a3\ud835\udfd8-lift\u2218fst-equiv : \u03a3 \ud835\udfd8 A \u2243 Lift {\u2113 = \u2113} \ud835\udfd8\n \u03a3\ud835\udfd8-lift\u2218fst-equiv = equiv (lift \u2218 fst) (\u03bb { (lift ()) }) (\u03bb { (lift ()) }) (\u03bb { (() , _) })\n\nmodule _ {a} {A : \ud835\udfd8 \u2192 \u2605_ a} {{_ : UA}} where\n \u03a3\ud835\udfd8-lift\u2218fst : \u03a3 \ud835\udfd8 A \u2261 Lift {\u2113 = a} \ud835\udfd8\n \u03a3\ud835\udfd8-lift\u2218fst = ua \u03a3\ud835\udfd8-lift\u2218fst-equiv\n\nmodule _ {A : \ud835\udfd8 \u2192 \u2605} where\n \u03a3\ud835\udfd8-fst-equiv : \u03a3 \ud835\udfd8 A \u2243 \ud835\udfd8\n \u03a3\ud835\udfd8-fst-equiv = equiv fst (\u03bb()) (\u03bb()) (\u03bb { (() , _) })\n\n \u03a3\ud835\udfd8-fst : {{_ : UA}} \u2192 \u03a3 \ud835\udfd8 A \u2261 \ud835\udfd8\n \u03a3\ud835\udfd8-fst = ua \u03a3\ud835\udfd8-fst-equiv\n\nmodule _ {a}{A : \ud835\udfd9 \u2192 \u2605_ a} where\n \u03a3\ud835\udfd9-snd-equiv : \u03a3 \ud835\udfd9 A \u2243 A _\n \u03a3\ud835\udfd9-snd-equiv = equiv snd (_,_ _) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a3\ud835\udfd9-snd : {{_ : UA}} \u2192 \u03a3 \ud835\udfd9 A \u2261 A _\n \u03a3\ud835\udfd9-snd = ua \u03a3\ud835\udfd9-snd-equiv\n\nmodule _ {A : \u2605} where\n \u228e\ud835\udfd8-inl-equiv : A \u2243 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl-equiv = equiv inl [inl: id ,inr: (\u03bb()) ] [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb()) ] (\u03bb _ \u2192 idp)\n\n module _ {{_ : UA}} where\n \u228e\ud835\udfd8-inl : A \u2261 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl = ua \u228e\ud835\udfd8-inl-equiv\n\n \ud835\udfd8\u228e-inr : A \u2261 (\ud835\udfd8 \u228e A)\n \ud835\udfd8\u228e-inr = \u228e\ud835\udfd8-inl \u2219 \u228e-comm\n\n \ud835\udfd9\u00d7-snd : (\ud835\udfd9 \u00d7 A) \u2261 A\n \ud835\udfd9\u00d7-snd = \u03a3\ud835\udfd9-snd\n\n \u00d7\ud835\udfd9-fst : (A \u00d7 \ud835\udfd9) \u2261 A\n \u00d7\ud835\udfd9-fst = \u00d7-comm \u2219 \ud835\udfd9\u00d7-snd\n\n \ud835\udfd8\u00d7-fst : (\ud835\udfd8 \u00d7 A) \u2261 \ud835\udfd8\n \ud835\udfd8\u00d7-fst = \u03a3\ud835\udfd8-fst\n\n \u00d7\ud835\udfd8-snd : (A \u00d7 \ud835\udfd8) \u2261 \ud835\udfd8\n \u00d7\ud835\udfd8-snd = \u00d7-comm \u2219 \u03a3\ud835\udfd8-fst\n\n -- old names\n A\u00d7\ud835\udfd9\u2261A = \u00d7\ud835\udfd9-fst\n \ud835\udfd9\u00d7A\u2261A = \ud835\udfd9\u00d7-snd\n \ud835\udfd8\u228eA\u2261A = \ud835\udfd8\u228e-inr\n A\u228e\ud835\udfd8\u2261A = \u228e\ud835\udfd8-inl\n \ud835\udfd8\u00d7A\u2261\ud835\udfd8 = \ud835\udfd8\u00d7-fst\n A\u00d7\ud835\udfd8\u2261\ud835\udfd8 = \u00d7\ud835\udfd8-snd\n\nmodule _ {A : \u2605}{B : A \u2192 \u2605}{C : \u03a3 A B \u2192 \u2605} where\n -- AC: Dependent axiom of choice\n -- In Type Theory it happens to be neither an axiom nor to be choosing anything.\n \u03a0\u03a3-comm-equiv : (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2243 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm-equiv = equiv (\u03bb H \u2192 fst \u2218 H , snd \u2218 H) (\u03bb H \u2192 < fst H , snd H >) (\u03bb H \u2192 idp) (\u03bb H \u2192 idp)\n\n \u03a0\u03a3-comm : {{_ : UA}}\n \u2192 (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2261 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm = ua \u03a0\u03a3-comm-equiv\n\nmodule _ {\u2113}{A : \ud835\udfda \u2192 \u2605_ \u2113}{{_ : UA}}{{_ : FunExt}} where\n \u03a3\ud835\udfda-\u228e : \u03a3 \ud835\udfda A \u2261 (A 0\u2082 \u228e A 1\u2082)\n \u03a3\ud835\udfda-\u228e = \u03a3-fst\u2243\u2032 \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 _ \u2219 dist-\u228e-\u03a3 \u2219 \u228e= \u03a3\ud835\udfd9-snd \u03a3\ud835\udfd9-snd\n\n \u03a0\ud835\udfda-\u00d7 : \u03a0 \ud835\udfda A \u2261 (A 0\u2082 \u00d7 A 1\u2082)\n \u03a0\ud835\udfda-\u00d7 = \u03a0-dom\u2243\u2032 \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 _ \u2219 dist-\u00d7-\u03a0 \u2219 \u00d7= (\u03a0\ud835\udfd9-uniq _) (\u03a0\ud835\udfd9-uniq _)\n\n \u03a0\ud835\udfdaF\u2261F\u2080\u00d7F\u2081 = \u03a0\ud835\udfda-\u00d7\n\nmodule _ {A : \u2605}{{_ : UA}}{{_ : FunExt}} where\n \u03a3\ud835\udfda-\u228e\u2032 : (\ud835\udfda \u00d7 A) \u2261 (A \u228e A)\n \u03a3\ud835\udfda-\u228e\u2032 = \u03a3\ud835\udfda-\u228e\n\n \u03a0\ud835\udfda\u2192\u00d7\u2032 : (\ud835\udfda \u2192 A) \u2261 (A \u00d7 A)\n \u03a0\ud835\udfda\u2192\u00d7\u2032 = \u03a0\ud835\udfda-\u00d7\n\nmodule _ {a}{A : \u2605_ a} where\n\n \u03a3\u2261x\u2243\ud835\udfd9 : \u2200 x \u2192 (\u03a3 A (_\u2261_ x)) \u2243 \ud835\udfd9\n \u03a3\u2261x\u2243\ud835\udfd9 x = equiv (\u03bb _ \u2192 _) (\u03bb _ \u2192 x , idp) (\u03bb _ \u2192 idp) (\u03bb p \u2192 pair= (snd p) (tr-r\u2261 (snd p) idp))\n\n \u03a3x\u2261\u2243\ud835\udfd9 : \u2200 x \u2192 (\u03a3 A (flip _\u2261_ x)) \u2243 \ud835\udfd9\n \u03a3x\u2261\u2243\ud835\udfd9 x = equiv (\u03bb _ \u2192 _) (\u03bb _ \u2192 x , idp) (\u03bb _ \u2192 idp) (\u03bb p \u2192 pair= (! snd p) ( tr-l\u2261 (! snd p) idp \u2219\n \u2219-refl (! (! (snd p))) \u2219 !-inv (snd p)))\n\nmodule _ {ab c}{A B : \u2605_ ab}{C : A \u2192 B \u2192 \u2605_ c}{{_ : UA}}{{_ : FunExt}} where\n\n \u03a0\u228e-equiv : (\u03a0 (A \u228e B) [inl: (\u03bb x \u2192 \u2200 y \u2192 C x y) ,inr: (\u03bb y \u2192 \u2200 x \u2192 C x y) ]) \u2243 ((t : \ud835\udfda)(x : A)(y : B) \u2192 C x y)\n \u03a0\u228e-equiv = equiv (\u03bb f \u2192 [0: (\u03bb x y \u2192 f (inl x) y) 1: ((\u03bb x y \u2192 f (inr y) x)) ])\n (\u03bb f \u2192 [inl: f 0\u2082 ,inr: flip (f 1\u2082) ])\n (\u03bb f \u2192 \u03bb= [0: idp 1: idp ])\n (\u03bb f \u2192 \u03bb= [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ])\n\n \u03a0\u228e : (\u03a0 (A \u228e B) [inl: (\u03bb x \u2192 \u2200 y \u2192 C x y) ,inr: (\u03bb y \u2192 \u2200 x \u2192 C x y) ]) \u2261 ((t : \ud835\udfda)(x : A)(y : B) \u2192 C x y)\n \u03a0\u228e = ua \u03a0\u228e-equiv\n\nmodule _ {ab c}{A B : \u2605_ ab}{C : \u2605_ c}{{_ : UA}}{{_ : FunExt}} where\n \u03a0\u228e\u2032 : (\u03a0 (A \u228e B) [inl: const (B \u2192 C) ,inr: const (A \u2192 C) ]) \u2261 (\ud835\udfda \u2192 A \u2192 B \u2192 C)\n \u03a0\u228e\u2032 = \u03a0\u228e\n\nmodule _ where\n\n private\n maybe-elim : {X : Set}(m : Maybe X) \u2192 m \u2262 nothing \u2192 X\n maybe-elim {X} m = maybe {B = \u03bb m' \u2192 m' \u2262 _ \u2192 _} (\u03bb x _ \u2192 x) (\u03bb e \u2192 \ud835\udfd8-elim (e idp)) m\n\n maybe-elim-just : {X : Set}(m : Maybe X)(p : m \u2262 nothing)\n \u2192 just (maybe-elim m p) \u2261 m\n maybe-elim-just (just x) p = idp\n maybe-elim-just nothing p = \ud835\udfd8-elim (p idp)\n\n maybe-elim-cong : \u2200 {X : Set}{m m' : Maybe X}{p p'} \u2192 m \u2261 m'\n \u2192 maybe-elim m p \u2261 maybe-elim m' p'\n maybe-elim-cong {m = just x} idp = idp\n maybe-elim-cong {m = nothing} {p = p} idp = \ud835\udfd8-elim (p idp)\n\n not-just : {X : Set}{x : X} \u2192 _\u2262_ {A = Maybe X} (just x) nothing\n not-just ()\n\n !!-p : \u2200 {x}{X : Set x}{x y : X}(p : x \u2261 y) \u2192 ! (! p) \u2261 p\n !!-p idp = idp\n\n !\u2219 : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 ! p \u2219 p \u2261 idp\n !\u2219 idp = idp\n\n \u2219! : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 p \u2219 ! p \u2261 idp\n \u2219! idp = idp\n\n\n record PreservePoint {A B : Set}(eq : Maybe A \u2261 Maybe B) : Set where\n field\n coe-p : \u2200 x \u2192 coe eq (just x) \u2262 nothing\n coe!-p : \u2200 x \u2192 coe! eq (just x) \u2262 nothing\n open PreservePoint\n\n nothing-to-nothing : {A B : Set}(eq : Maybe A \u2261 Maybe B)\n \u2192 coe eq nothing \u2261 nothing \u2192 PreservePoint eq\n nothing-to-nothing eq n2n = record { coe-p = \u03bb x p \u2192 not-just (coe-inj eq (p \u2219 ! n2n))\n ; coe!-p = \u03bb x p \u2192 not-just (coe-same (! !\u2219 eq) (just x)\n \u2219 ! (coe\u2218coe eq (! eq) (just x)) \u2219 ap (coe eq) p \u2219 n2n) }\n\n !-PP : \u2200 {A B : Set} {e : Maybe A \u2261 Maybe B} \u2192 PreservePoint e \u2192 PreservePoint (! e)\n !-PP {e = e} e-pp = record { coe-p = coe!-p e-pp\n ; coe!-p = \u03bb x p \u2192 coe-p e-pp x ( ap (\u03bb X \u2192 coe X (just x)) (! !!-p e ) \u2219 p) }\n\n to : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e \u2192 A \u2192 B\n to e e-pp x = maybe-elim (coe e (just x)) (coe-p e-pp x)\n\n module _{A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}{ep ep'}(e'\u2219e=id : e' \u2219 e \u2261 idp)(x : B) where\n to-to : to e ep (to e' ep' x) \u2261 x\n to-to = just-injective (ap just (maybe-elim-cong\n {m = coe e (just (maybe-elim (coe e' (just x)) _))}\n {m' = coe e (coe e' (just x))}\n {p' = \u03bb q \u2192 not-just (! p \u2219 q)} (ap (coe e) (maybe-elim-just _ _)))\n \u2219 maybe-elim-just (coe e (coe e' (just x))) (\u03bb q \u2192 not-just (! p \u2219 q))\n \u2219 p)\n where\n p : coe e (coe e' (just x)) \u2261 just x\n p = coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n\n cto : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 Maybe A \u2192 Maybe B\n cto e = maybe (maybe\u2032 just (coe e nothing) \u2218 coe e \u2218 just) nothing\n\n module _ {A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}(e'\u2219e=id : e' \u2219 e \u2261 idp) where\n cto-cto : (x : Maybe B) \u2192 cto e (cto e' x) \u2261 x\n cto-cto nothing = idp\n cto-cto (just x) with coe e' (just x) | coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n cto-cto (just x) | just x\u2081 | p = ap (maybe just (coe e nothing)) p\n cto-cto (just x) | nothing | p with coe e' nothing\n | coe\u2218coe e e' nothing \u2219 coe-same e'\u2219e=id nothing\n cto-cto (just x) | nothing | p | just y | q = ap (maybe just (coe e nothing)) q \u2219 p\n cto-cto (just x) | nothing | p | nothing | q = ! q \u2219 p\n\n module _ {A B : Set} where\n create-PP-\u2243 : Maybe A \u2261 Maybe B \u2192 Maybe A \u2243 Maybe B\n create-PP-\u2243 e = equiv (cto e)\n (cto (! e))\n (cto-cto {e = e} {e' = ! e} (!\u2219 e))\n (cto-cto {e = ! e}{e' = e} (\u2219! e))\n\n create-PP : {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 \u03a3 (Maybe A \u2261 Maybe B) PreservePoint\n create-PP eq = ua (create-PP-\u2243 eq) , nothing-to-nothing _ (coe-\u03b2 (create-PP-\u2243 eq) nothing)\n\n Maybe-injective-PP : {{_ : UA}} \u2192 (e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e\n \u2192 A \u2261 B\n Maybe-injective-PP e e-pp = ua\n (equiv (to e e-pp)\n (to (! e) (!-PP e-pp))\n (to-to {e = e} { ! e} {e-pp}{ !-PP e-pp} (!\u2219 e))\n (to-to {e = ! e} {e} { !-PP e-pp} {e-pp} (\u2219! e)))\n\n Maybe-injective : \u2200 {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 A \u2261 B\n Maybe-injective e = let (e' , e'-pp) = create-PP e in Maybe-injective-PP e' e'-pp\n\n\nmodule _ {a}{A : \u2605_ a} where\n Maybe\u2243\ud835\udfd9\u228e : Maybe A \u2243 (\ud835\udfd9 \u228e A)\n Maybe\u2243\ud835\udfd9\u228e = equiv (maybe inr (inl _)) [inl: const nothing ,inr: just ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (maybe (\u03bb x \u2192 idp) idp)\n\n module _ {{_ : UA}} where\n\n Maybe\u2261\ud835\udfd9\u228e : Maybe A \u2261 (\ud835\udfd9 \u228e A)\n Maybe\u2261\ud835\udfd9\u228e = ua Maybe\u2243\ud835\udfd9\u228e\n\n Maybe\u2261Lift\ud835\udfd9\u228e : Maybe A \u2261 (Lift {\u2113 = a} \ud835\udfd9 \u228e A)\n Maybe\u2261Lift\ud835\udfd9\u228e = ua (equiv (maybe inr (inl _))\n [inl: const nothing ,inr: just ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n (maybe (\u03bb _ \u2192 idp) idp))\n\nFin0\u2243\ud835\udfd8 : Fin 0 \u2243 \ud835\udfd8\nFin0\u2243\ud835\udfd8 = equiv (\u03bb ()) (\u03bb ()) (\u03bb ()) (\u03bb ())\n\nFin1\u2243\ud835\udfd9 : Fin 1 \u2243 \ud835\udfd9\nFin1\u2243\ud835\udfd9 = equiv _ (\u03bb _ \u2192 zero) (\u03bb _ \u2192 idp) \u03b2\n where \u03b2 : (_ : Fin 1) \u2192 _\n \u03b2 zero = idp\n \u03b2 (suc ())\n\nmodule _ {{_ : UA}} where\n Fin0\u2261\ud835\udfd8 : Fin 0 \u2261 \ud835\udfd8\n Fin0\u2261\ud835\udfd8 = ua Fin0\u2243\ud835\udfd8\n\n Fin1\u2261\ud835\udfd9 : Fin 1 \u2261 \ud835\udfd9\n Fin1\u2261\ud835\udfd9 = ua Fin1\u2243\ud835\udfd9\n\nFin\u2218suc\u2243\ud835\udfd9\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2243 (\ud835\udfd9 \u228e Fin n)\nFin\u2218suc\u2243\ud835\udfd9\u228eFin = equiv [zero: inl _ ,suc: inr ] [inl: (\u03bb _ \u2192 zero) ,inr: suc ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n [zero: idp ,suc: (\u03bb _ \u2192 idp) ]\n\nFin\u2218suc\u2261\ud835\udfd9\u228eFin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 (\ud835\udfd9 \u228e Fin n)\nFin\u2218suc\u2261\ud835\udfd9\u228eFin = ua Fin\u2218suc\u2243\ud835\udfd9\u228eFin\n\nFin-\u228e-+ : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u228e Fin n) \u2261 Fin (m \u2115.+ n)\nFin-\u228e-+ {zero} = \u228e= Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm \u2219 ! \u228e\ud835\udfd8-inl\nFin-\u228e-+ {suc m} = \u228e= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ap (_\u228e_ \ud835\udfd9) Fin-\u228e-+ \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\n\nFin-\u00d7-* : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u00d7 Fin n) \u2261 Fin (m \u2115.* n)\nFin-\u00d7-* {zero} = \u00d7= Fin0\u2261\ud835\udfd8 idp \u2219 \u03a3\ud835\udfd8-fst \u2219 ! Fin0\u2261\ud835\udfd8\nFin-\u00d7-* {suc m} = \u00d7= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u228e-\u00d7 \u2219 \u228e= \u03a3\ud835\udfd9-snd Fin-\u00d7-* \u2219 Fin-\u228e-+\n\nFin-\u2192-^ : \u2200 {{_ : UA}}{{_ : FunExt}}{m n} \u2192 (Fin m \u2192 Fin n) \u2261 Fin (n \u2115.^ m)\nFin-\u2192-^ {zero} = \u2192= Fin0\u2261\ud835\udfd8 idp \u2219 \u03a0\ud835\udfd8-uniq\u2032 _ \u2219 \u228e\ud835\udfd8-inl \u2219 ap (_\u228e_ \ud835\udfd9) (! Fin0\u2261\ud835\udfd8)\n \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\nFin-\u2192-^ {suc m} = \u2192= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u00d7-\u2192 \u2219 \u00d7= (\u03a0\ud835\udfd9-uniq _) Fin-\u2192-^ \u2219 Fin-\u00d7-*\n\nFin\u2218suc\u2261Maybe\u2218Fin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 Maybe (Fin n)\nFin\u2218suc\u2261Maybe\u2218Fin = Fin\u2218suc\u2261\ud835\udfd9\u228eFin \u2219 ! Maybe\u2261\ud835\udfd9\u228e\n\nFin-injective : \u2200 {{_ : UA}}{m n} \u2192 Fin m \u2261 Fin n \u2192 m \u2261 n\nFin-injective {zero} {zero} Finm\u2261Finn = idp\nFin-injective {zero} {suc n} Finm\u2261Finn = \ud835\udfd8-elim (coe (! Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {zero} Finm\u2261Finn = \ud835\udfd8-elim (coe (Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {suc n} Finm\u2261Finn\n = ap suc (Fin-injective (Maybe-injective\n (! Fin\u2218suc\u2261Maybe\u2218Fin \u2219 Finm\u2261Finn \u2219 Fin\u2218suc\u2261Maybe\u2218Fin)))\n\nFin\u228e-injective : \u2200 {{_ : UA}}{A B : Set} n \u2192 (Fin n \u228e A) \u2261 (Fin n \u228e B) \u2192 A \u2261 B\nFin\u228e-injective zero f = \u228e\ud835\udfd8-inl \u2219 \u228e-comm \u2219 \u228e= (! Fin0\u2261\ud835\udfd8) idp\n \u2219 f \u2219 (\u228e= Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm) \u2219 ! \u228e\ud835\udfd8-inl\nFin\u228e-injective (suc n) f = Fin\u228e-injective n (Maybe-injective\n (Maybe\u2261\ud835\udfd9\u228e \u2219 \u228e-assoc \u2219 \u228e= (! Fin\u2218suc\u2261\ud835\udfd9\u228eFin) idp \u2219 f\n \u2219 \u228e= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ! Maybe\u2261\ud835\udfd9\u228e))\n\nLift\u2243id : \u2200 {a} {A : \u2605_ a} \u2192 Lift {a} {a} A \u2243 A\nLift\u2243id = equiv lower lift (\u03bb _ \u2192 idp) (\u03bb { (lift x) \u2192 idp })\n\nmodule _ {{_ : UA}} where\n Fin-\u2261-\u22611\u2082 : \u2200 b \u2192 Fin (\ud835\udfda\u25b9\u2115 b) \u2261 (b \u2261 1\u2082)\n Fin-\u2261-\u22611\u2082 1\u2082 = Fin1\u2261\ud835\udfd9 \u2219 ua (Is-contr-to-Is-equiv.\ud835\udfd9\u2243 (\u03a9\u2081-set-to-contr \ud835\udfda-is-set 1\u2082))\n Fin-\u2261-\u22611\u2082 0\u2082 = ua (equiv (\u03bb ()) (\u03bb ()) (\u03bb ()) (\u03bb ()))\n\n Fin-\u2261-\u22610\u2082 : \u2200 b \u2192 Fin (\ud835\udfda\u25b9\u2115 (not b)) \u2261 (b \u2261 0\u2082)\n Fin-\u2261-\u22610\u2082 b = Fin-\u2261-\u22611\u2082 (not b) \u2219 ! \u2013>-paths-equiv twist-equiv\n\n \u2713-\u2227-\u00d7 : \u2200 x y \u2192 \u2713 (x \u2227 y) \u2261 (\u2713 x \u00d7 \u2713 y)\n \u2713-\u2227-\u00d7 1\u2082 y = ! \ud835\udfd9\u00d7-snd\n \u2713-\u2227-\u00d7 0\u2082 y = ! \ud835\udfd8\u00d7-fst\n\n count-\u2261 : \u2200 {a} {A : \u2605_ a} (p : A \u2192 \ud835\udfda) x \u2192 Fin (\ud835\udfda\u25b9\u2115 (p x)) \u2261 (p x \u2261 1\u2082)\n count-\u2261 p x = Fin-\u2261-\u22611\u2082 (p x)\n\n Lift\u2261id : \u2200 {a} {A : \u2605_ a} \u2192 Lift {a} {a} A \u2261 A\n Lift\u2261id = ua Lift\u2243id\n\n \u03a0\ud835\udfd9F\u2261F : \u2200 {\u2113} {F : \ud835\udfd9 \u2192 \u2605_ \u2113} \u2192 \u03a0 \ud835\udfd9 F \u2261 F _\n \u03a0\ud835\udfd9F\u2261F = ua (equiv (\u03bb x \u2192 x _) const (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\n \ud835\udfd9\u2192A\u2261A : \u2200 {\u2113} {A : \u2605_ \u2113} \u2192 (\ud835\udfd9 \u2192 A) \u2261 A\n \ud835\udfd9\u2192A\u2261A = \u03a0\ud835\udfd9F\u2261F\n\n not-\ud835\udfda\u2261\ud835\udfda : \ud835\udfda \u2261 \ud835\udfda\n not-\ud835\udfda\u2261\ud835\udfda = twist\n\n Maybe\ud835\udfd8\u2261\ud835\udfd9 : Maybe \ud835\udfd8 \u2261 \ud835\udfd9\n Maybe\ud835\udfd8\u2261\ud835\udfd9 = Maybe\u2261\ud835\udfd9\u228e \u2219 ! \u228e\ud835\udfd8-inl\n\n Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe : \u2200 {a} {A : \u2605_ a} n \u2192 Maybe (Maybe^ n A) \u2261 Maybe^ n (Maybe A)\n Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe zero = idp\n Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe (suc n) = ap Maybe (Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe n)\n\n Maybe^\ud835\udfd8\u2261Fin : \u2200 n \u2192 Maybe^ n \ud835\udfd8 \u2261 Fin n\n Maybe^\ud835\udfd8\u2261Fin zero = ! Fin0\u2261\ud835\udfd8\n Maybe^\ud835\udfd8\u2261Fin (suc n) = ap Maybe (Maybe^\ud835\udfd8\u2261Fin n) \u2219 ! Fin\u2218suc\u2261Maybe\u2218Fin\n\n Maybe^\ud835\udfd9\u2261Fin1+ : \u2200 n \u2192 Maybe^ n \ud835\udfd9 \u2261 Fin (suc n)\n Maybe^\ud835\udfd9\u2261Fin1+ n = ap (Maybe^ n) (! Maybe\ud835\udfd8\u2261\ud835\udfd9) \u2219 ! Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe n \u2219 Maybe^\ud835\udfd8\u2261Fin (suc n)\n\n Maybe-\u228e : \u2200 {a} {A B : \u2605_ a} \u2192 (Maybe A \u228e B) \u2261 Maybe (A \u228e B)\n Maybe-\u228e {a} = \u228e= Maybe\u2261Lift\ud835\udfd9\u228e idp \u2219 ! \u228e-assoc \u2219 ! Maybe\u2261Lift\ud835\udfd9\u228e\n\n Maybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \ud835\udfd8 \u228e Maybe^ n A) \u2261 Maybe^ (m + n) A\n Maybe^-\u228e-+ zero n = ! \ud835\udfd8\u228e-inr\n Maybe^-\u228e-+ (suc m) n = Maybe-\u228e \u2219 ap Maybe (Maybe^-\u228e-+ m n)\n\nmodule _ {A : Set} {p q : A \u2192 \ud835\udfda} where\n \u03a3AP\u00acQ : Set\n \u03a3AP\u00acQ = \u03a3 A (\u03bb x \u2192 p x \u2261 1\u2082 \u00d7 q x \u2261 0\u2082)\n\n \u03a3A\u00acPQ : Set\n \u03a3A\u00acPQ = \u03a3 A (\u03bb x \u2192 p x \u2261 0\u2082 \u00d7 q x \u2261 1\u2082)\n\n module EquivalentSubsets (e : \u03a3AP\u00acQ \u2261 \u03a3A\u00acPQ) where\n\n f' : \u03a3AP\u00acQ \u2192 \u03a3A\u00acPQ\n f' = coe e\n\n f-1' : \u03a3A\u00acPQ \u2192 \u03a3AP\u00acQ\n f-1' = coe! e\n\n f-1f' : \u2200 x \u2192 f-1' (f' x) \u2261 x\n f-1f' = coe!-inv-l e\n\n ff-1' : \u2200 x \u2192 f' (f-1' x) \u2261 x\n ff-1' = coe!-inv-r e\n\n f : (x : A) \u2192 p x \u2261 1\u2082 \u2192 q x \u2261 0\u2082 \u2192 \u03a3A\u00acPQ\n f x px qx = f' (x , (px , qx))\n\n f-1 : (x : A) \u2192 p x \u2261 0\u2082 \u2192 q x \u2261 1\u2082 \u2192 \u03a3AP\u00acQ\n f-1 x px qx = f-1' (x , (px , qx))\n\n f-1f : \u2200 x px nqx \u2192\n let y = snd (f x px nqx) in fst (f-1 (fst (f x px nqx)) (fst y) (snd y)) \u2261 x\n f-1f x px nqx = \u2261.cong fst (f-1f' (x , (px , nqx)))\n\n ff-1 : \u2200 x px nqx \u2192\n let y = snd (f-1 x px nqx) in fst (f (fst (f-1 x px nqx)) (fst y) (snd y)) \u2261 x\n ff-1 x px nqx = \u2261.cong fst (ff-1' (x , (px , nqx)))\n\n \u03c0' : (x : A) (px qx : \ud835\udfda) \u2192 p x \u2261 px \u2192 q x \u2261 qx \u2192 A\n \u03c0' x 1\u2082 1\u2082 px qx = x\n \u03c0' x 1\u2082 0\u2082 px qx = fst (f x px qx)\n \u03c0' x 0\u2082 1\u2082 px qx = fst (f-1 x px qx)\n \u03c0' x 0\u2082 0\u2082 px qx = x\n\n \u03c0 : A \u2192 A\n \u03c0 x = \u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n 0\u22621 : 0\u2082 \u2262 1\u2082\n 0\u22621 ()\n\n \u03c001 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px0 : p x \u2261 0\u2082) (qx1 : q x \u2261 1\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 0\u2082 1\u2082 px0 qx1\n \u03c001 x 1\u2082 _ ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym px0) ppx))\n \u03c001 x 0\u2082 1\u2082 ppx qqx px0 qx1 = \u2261.ap\u2082 (\u03bb z1 z2 \u2192 fst (f-1 x z1 z2)) (UIP-set \ud835\udfda-is-set ppx px0) (UIP-set \ud835\udfda-is-set qqx qx1)\n \u03c001 x 0\u2082 0\u2082 ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qqx) qx1))\n\n \u03c010 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px1 : p x \u2261 1\u2082) (qx0 : q x \u2261 0\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 1\u2082 0\u2082 px1 qx0\n \u03c010 x 0\u2082 _ ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym ppx) px1))\n \u03c010 x 1\u2082 0\u2082 ppx qqx px1 qx0 = \u2261.ap\u2082 (\u03bb z1 z2 \u2192 fst (f x z1 z2)) (UIP-set \ud835\udfda-is-set ppx px1) (UIP-set \ud835\udfda-is-set qqx qx0)\n \u03c010 x 1\u2082 1\u2082 ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qx0) qqx))\n\n \u03c0'bb : \u2200 {b} x (px : p x \u2261 b) (qx : q x \u2261 b) ppx qqx ([ppx] : p x \u2261 ppx) ([qqx] : q x \u2261 qqx) \u2192 \u03c0' x ppx qqx [ppx] [qqx] \u2261 x\n \u03c0'bb x px qx 1\u2082 1\u2082 [ppx] [qqx] = \u2261.refl\n \u03c0'bb x px qx 1\u2082 0\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [qqx]) (\u2261.trans qx (\u2261.trans (\u2261.sym px) [ppx]))))\n \u03c0'bb x px qx 0\u2082 1\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [ppx]) (\u2261.trans px (\u2261.trans (\u2261.sym qx) [qqx]))))\n \u03c0'bb x px qx 0\u2082 0\u2082 [ppx] [qqx] = \u2261.refl\n\n \u03c0\u03c0' : \u2200 x px qx [px] [qx] \u2192 let y = (\u03c0' x px qx [px] [qx]) in \u03c0' y (p y) (q y) \u2261.refl \u2261.refl \u2261 x\n \u03c0\u03c0' x 1\u2082 1\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n \u03c0\u03c0' x 1\u2082 0\u2082 px qx = let fx = f x px qx in let pfx = fst (snd fx) in let qfx = snd (snd fx) in \u2261.trans (\u03c001 (fst fx) (p (fst fx)) (q (fst fx)) \u2261.refl \u2261.refl pfx qfx) (f-1f x px qx)\n \u03c0\u03c0' x 0\u2082 1\u2082 px qx = let fx = f-1 x px qx in let pfx = fst (snd fx) in let qfx = snd (snd fx) in \u2261.trans (\u03c010 (fst fx) (p (fst fx)) (q (fst fx)) \u2261.refl \u2261.refl pfx qfx) (ff-1 x px qx)\n \u03c0\u03c0' x 0\u2082 0\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n\n \u03c0\u03c0 : \u2200 x \u2192 \u03c0 (\u03c0 x) \u2261 x\n \u03c0\u03c0 x = \u03c0\u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n prop' : \u2200 px qx x ([px] : p x \u2261 px) ([qx] : q x \u2261 qx) \u2192 q (\u03c0' x px qx [px] [qx]) \u2261 px\n prop' 1\u2082 1\u2082 x px qx = qx\n prop' 1\u2082 0\u2082 x px qx = snd (snd (f x px qx))\n prop' 0\u2082 1\u2082 x px qx = snd (snd (f-1 x px qx))\n prop' 0\u2082 0\u2082 x px qx = qx\n\n prop'' : \u2200 x \u2192 p x \u2261 q (\u03c0 x)\n prop'' x = \u2261.sym (prop' (p x) (q x) x \u2261.refl \u2261.refl)\n\n prop : {{_ : FunExt}} \u2192 p \u2261 q \u2218 \u03c0\n prop = \u03bb= prop''\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"887d992f22f6e65e6ca4201f28f73ab449fdfb41","subject":"progress progress #3","message":"progress progress #3\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress.agda","new_file":"progress.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\n\nopen import canonical-boxed-forms\nopen import canonical-indeterminate-forms\n\nopen import Agda.Primitive using (Level; lzero; lsuc) renaming (_\u2294_ to lmax)\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d boxedval + d indet + d casterr[ \u0394 ] + \u03a3[ d' \u2208 dhexp ] (d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 ok d \u0394\n E : \u2200{d \u0394} \u2192 d casterr \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n V : \u2200{d \u0394} \u2192 d boxedval \u2192 ok d \u0394\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = V (BVVal VConst)\n\n -- variables\n progress (TAVar x\u2081) = abort (somenotnone (! x\u2081))\n\n -- lambdas\n progress (TALam wt) = V (BVVal VLam)\n\n -- applications\n progress (TAAp wt1 wt2)\n with progress wt1 | progress wt2\n -- if the left steps, the whole thing steps\n progress (TAAp wt1 wt2) | S (_ , Step x y z) | _ = S (_ , Step (FHAp1 x) y (FHAp1 z))\n -- if the left is an error, the whole thing is an error\n progress (TAAp wt1 wt2) | E x | _ = E (CECong (FHAp1 FHOuter) x)\n -- if the left is indeterminate, inspect the right\n progress (TAAp wt1 wt2) | I i | S (_ , Step x y z) = S (_ , Step (FHAp2 (FIndet i) x) y (FHAp2 (FIndet i) z))\n progress (TAAp wt1 wt2) | I x | E x\u2081 = E (CECong (FHAp2 (FIndet x) FHOuter) x\u2081)\n progress (TAAp wt1 wt2) | I x | I x\u2081 = I (IAp {!!} x (FIndet x\u2081)) -- cyrus (had an idea for a lemma here but it's false; see bottom)\n progress (TAAp wt1 wt2) | I x | V x\u2081 = I (IAp {!!} x (FBoxed x\u2081)) -- cyrus (see bottom)\n -- if the left is a boxed value, inspect the right\n progress (TAAp wt1 wt2) | V v | S (_ , Step x y z) = S (_ , Step (FHAp2 (FBoxed v) x) y (FHAp2 (FBoxed v) z))\n progress (TAAp wt1 wt2) | V v | E e = E (CECong (FHAp2 (FBoxed v) FHOuter) e)\n progress (TAAp wt1 wt2) | V v | I i\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (x , d' , refl , qq) = S (_ , Step FHOuter (ITLam (FIndet i)) FHOuter)\n ... | Inr (d' , \u03c41' , \u03c42' , refl , neq , qq) = I (IAp {!!} (ICastArr neq {!!}) (FIndet i)) --cyrus, as below\n progress (TAAp wt1 wt2) | V v | V v\u2082\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (x , d' , refl , qq) = S (_ , Step FHOuter (ITLam (FBoxed v\u2082)) FHOuter)\n ... | Inr (d' , \u03c41' , \u03c42' , refl , neq , qq) = I (IAp {!!} (ICastArr neq {!!}) (FBoxed v\u2082)) --cyrus\n where\n -- empty holes\n progress (TAEHole x x\u2081) = I IEHole\n\n -- nonempty holes\n progress (TANEHole xin wt x\u2081)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHNEHole x) y (FHNEHole z))\n ... | E x = E (CECong (FHNEHole FHOuter) x)\n ... | I x = I (INEHole (FIndet x))\n ... | V x = I (INEHole (FBoxed x))\n\n -- casts\n progress (TACast wt con)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHCast x) y (FHCast z))\n ... | E x = E (CECong (FHCast FHOuter) x)\n -- indet cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | I x = S (_ , Step FHOuter (ITCastID (FIndet x)) FHOuter)\n progress (TACast wt TCHole1) | I x = I (ICastGroundHole {!!} x) -- cyrus\n progress (TACast wt TCHole2) | I x = I (ICastHoleGround {!!} x {!!}) -- cyrus\n progress (TACast wt (TCArr c1 c2)) | I x = I (ICastArr {!!} x) -- cyrus\n -- boxed value cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | V x = S (_ , Step FHOuter (ITCastID (FBoxed x)) FHOuter)\n progress (TACast wt TCHole1) | V x = V (BVHoleCast {!!} x) -- cyrus\n progress (TACast wt TCHole2) | V x = V {!!} -- cyrus: missing rule for boxed values?\n progress (TACast wt (TCArr c1 c2)) | V x = V (BVArrCast {!!} x) -- cyrus\n\n\n -- this would fill two above, but it's false: \u2987\u2988 indet but its type, \u2987\u2988, is not ground\n -- lem-groundindet : \u2200{ \u0394 d \u03c4} \u2192 \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192 d indet \u2192 \u03c4 ground\n\n\n counter : \u03a3[ d \u2208 dhexp ] \u03a3[ \u03c4 \u2208 htyp ] \u03a3[ \u03c4' \u2208 htyp ] \u03a3[ \u0394 \u2208 hctx ]\n ((d indet) \u00d7 (\u0394 , \u2205 \u22a2 d :: \u03c4 ==> \u03c4'))\n counter = (\u2987\u2988\u27e8 Z , \u2205 \u27e9 \u27e8 b ==> b \u21d2 b ==> \u2987\u2988 \u27e9) ,\n b , \u2987\u2988 , \u25a0 (Z , \u2205 , b ==> b ) ,\n ICastArr (\u03bb ()) IEHole , TACast (TAEHole refl (\u03bb x d \u2192 \u03bb ())) (TCArr TCRefl TCHole1)\n\n postulate\n lem : \u2200{ \u0394 d1 \u03c4 \u03c4'} \u2192 \u0394 , \u2205 \u22a2 d1 :: (\u03c4 ==> \u03c4') \u2192\n d1 indet \u2192\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9))\n\n problem : \u22a5\n problem = lem (\u03c02 (\u03c02 (\u03c02 (\u03c02 (\u03c02 counter)))))\n (\u03c01 (\u03c02 (\u03c02 (\u03c02 (\u03c02 counter))))) b b b \u2987\u2988 \u2987\u2988\u27e8 Z , \u2205 \u27e9 refl\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import canonical-boxed-forms\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d boxedval + d indet + d casterr[ \u0394 ] + \u03a3[ d' \u2208 dhexp ] (d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 ok d \u0394\n E : \u2200{d \u0394} \u2192 d casterr \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n V : \u2200{d \u0394} \u2192 d boxedval \u2192 ok d \u0394\n\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = V (BVVal VConst)\n\n -- variables\n progress (TAVar x\u2081) = abort (somenotnone (! x\u2081))\n\n -- lambdas\n progress (TALam wt) = V (BVVal VLam)\n\n -- applications\n progress (TAAp wt1 wt2)\n with progress wt1 | progress wt2\n -- if the left steps, the whole thing steps\n progress (TAAp wt1 wt2) | S (_ , Step x y z) | _ = S (_ , Step (FHAp1 x) y (FHAp1 z))\n -- if the left is an error, the whole thing is an error\n progress (TAAp wt1 wt2) | E x | _ = E (CECong (FHAp1 FHOuter) x)\n -- if the left is indeterminate, inspect the right\n progress (TAAp wt1 wt2) | I i | S (_ , Step x y z) = S (_ , Step (FHAp2 (FIndet i) x) y (FHAp2 (FIndet i) z))\n progress (TAAp wt1 wt2) | I x | E x\u2081 = E (CECong (FHAp2 (FIndet x) FHOuter) x\u2081)\n progress (TAAp wt1 wt2) | I x | I x\u2081 = I (IAp {!!} x (FIndet x\u2081)) -- cyrus\n where\n lem : \u2200{ \u0394 d1 \u03c4 \u03c4'} \u2192 \u0394 , \u2205 \u22a2 d1 :: (\u03c4 ==> \u03c4') \u2192\n d1 indet \u2192\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9))\n lem (TACast wt TCRefl) (ICastArr x\u2083 ind) \u03c41 \u03c42 .\u03c41 .\u03c42 d refl = x\u2083 refl\n lem (TACast wt (TCArr x\u2082 x\u2083)) (ICastArr x\u2084 ind) \u03c41 \u03c42 \u03c43 \u03c4' d refl = {!!}\n progress (TAAp wt1 wt2) | I x | V x\u2081 = I (IAp {!!} x (FBoxed x\u2081)) -- cyrus\n -- if the left is a boxed value, inspect the right\n progress (TAAp wt1 wt2) | V v | S (_ , Step x y z) = S (_ , Step (FHAp2 (FBoxed v) x) y (FHAp2 (FBoxed v) z))\n progress (TAAp wt1 wt2) | V v | E e = E (CECong (FHAp2 (FBoxed v) FHOuter) e)\n progress (TAAp wt1 wt2) | V v | I i = I (IAp {!!} {!!} (FIndet i)) -- cyrus (issue from above, and also i think missing a rule for indet applications)\n progress (TAAp wt1 wt2) | V v | V v\u2082 = {!!} -- {!canonical-boxed-forms-arr wt1 x !}\n\n -- empty holes\n progress (TAEHole x x\u2081) = I IEHole\n\n -- nonempty holes\n progress (TANEHole xin wt x\u2081)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHNEHole x) y (FHNEHole z))\n ... | E x = E (CECong (FHNEHole FHOuter) x)\n ... | I x = I (INEHole (FIndet x))\n ... | V x = I (INEHole (FBoxed x))\n\n -- casts\n progress (TACast wt con)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHCast x) y (FHCast z))\n ... | E x = E (CECong (FHCast FHOuter) x)\n -- indet cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | I x = S (_ , Step FHOuter (ITCastID (FIndet x)) FHOuter)\n progress (TACast wt TCHole1) | I x = I (ICastGroundHole {!!} x) -- cyrus\n progress (TACast wt TCHole2) | I x = I (ICastHoleGround {!!} x {!!}) -- cyrus\n progress (TACast wt (TCArr c1 c2)) | I x = I (ICastArr {!!} x) -- cyrus\n -- boxed value cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | V x = S (_ , Step FHOuter (ITCastID (FBoxed x)) FHOuter)\n progress (TACast wt TCHole1) | V x = V (BVHoleCast {!!} x) -- cyrus\n progress (TACast wt TCHole2) | V x = V {!!} -- cyrus: missing rule for boxed values?\n progress (TACast wt (TCArr c1 c2)) | V x = V (BVArrCast {!!} x) -- cyrus\n\n\n -- this would fill two above, but it's false: \u2987\u2988 indet but its type, \u2987\u2988, is not ground\n -- lem-groundindet : \u2200{ \u0394 d \u03c4} \u2192 \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192 d indet \u2192 \u03c4 ground\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"6936c62b46d40ca7430caebbc9225fd24549ad9a","subject":"P{\u22a4,0} do not need FunExt,UA","message":"P{\u22a4,0} do not need FunExt,UA\n","repos":"crypto-agda\/protocols","old_file":"Control\/Protocol\/Additive.agda","new_file":"Control\/Protocol\/Additive.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Function.NP\nopen import Type\nopen import Data.Product.NP renaming (proj\u2081 to fst; proj\u2082 to snd) using (_\u00d7_; _,_)\nopen import Data.Zero using (\ud835\udfd8)\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_]) hiding ([_,_]\u2032)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.LR\nopen import Relation.Binary.PropositionalEquality.NP using (_\u2261_; !_; _\u2219_; refl; ap)\nopen import Function.Extensionality\nopen import HoTT\nopen Equivalences\n\nopen import Control.Protocol\n\nmodule Control.Protocol.Additive where\n\nmodule send\/recv-\ud835\udfd8 (P : \ud835\udfd8 \u2192 Proto) where\n P\u22a4 : Proto\n P\u22a4 = recvE \ud835\udfd8 P\n\n P0 : Proto\n P0 = sendE \ud835\udfd8 P\n\n module _ {{_ : FunExt}}{{_ : UA}} where\n P0-empty : \u27e6 P0 \u27e7 \u2261 \ud835\udfd8\n P0-empty = ua (equiv fst (\u03bb()) (\u03bb()) (\u03bb { (() , _) }))\n\n P\u22a4-uniq : \u27e6 P\u22a4 \u27e7 \u2261 \ud835\udfd9\n P\u22a4-uniq = \u03a0\ud835\udfd8-uniq _\n\nopen send\/recv-\ud835\udfd8 (\u03bb _ \u2192 end) public\n\n_\u2295_ : (l r : Proto) \u2192 Proto\nl \u2295 r = send [L: l R: r ]\n\n_&_ : (l r : Proto) \u2192 Proto\nl & r = recv [L: l R: r ]\n\nmodule _ {{_ : FunExt}} {P Q} where\n dual-\u2295 : dual (P \u2295 Q) \u2261 dual P & dual Q\n dual-\u2295 = recv=\u2032 [L: refl R: refl ]\n\n dual-& : dual (P & Q) \u2261 dual P \u2295 dual Q\n dual-& = send=\u2032 [L: refl R: refl ]\n\nmodule _ {{_ : FunExt}}{{_ : UA}} P Q where\n &-comm : P & Q \u2261 Q & P\n &-comm = recv\u2243 LR!-equiv [L: refl R: refl ]\n\n \u2295-comm : P \u2295 Q \u2261 Q \u2295 P\n \u2295-comm = send\u2243 LR!-equiv [L: refl R: refl ]\n\n -- additive mix (left-biased)\n amixL : \u27e6 P & Q \u27e7 \u2192 \u27e6 P \u2295 Q \u27e7\n amixL pq = (`L , pq `L)\n\n amixR : \u27e6 P & Q \u27e7 \u2192 \u27e6 P \u2295 Q \u27e7\n amixR pq = (`R , pq `R)\n\nmodule _ {P Q R S}(f : \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7)(g : \u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) where\n \u2295-map : \u27e6 P \u2295 R \u27e7 \u2192 \u27e6 Q \u2295 S \u27e7\n \u2295-map (`L , pr) = `L , f pr\n \u2295-map (`R , pr) = `R , g pr\n\n &-map : \u27e6 P & R \u27e7 \u2192 \u27e6 Q & S \u27e7\n &-map p `L = f (p `L)\n &-map p `R = g (p `R)\n\nmodule _ {P Q} where\n \u2295\u2192\u228e : \u27e6 P \u2295 Q \u27e7 \u2192 \u27e6 P \u27e7 \u228e \u27e6 Q \u27e7\n \u2295\u2192\u228e (`L , p) = inl p\n \u2295\u2192\u228e (`R , q) = inr q\n\n \u228e\u2192\u2295 : \u27e6 P \u27e7 \u228e \u27e6 Q \u27e7 \u2192 \u27e6 P \u2295 Q \u27e7\n \u228e\u2192\u2295 = [inl: _,_ `L ,inr: _,_ `R ]\n\n \u2295\u2243\u228e : \u27e6 P \u2295 Q \u27e7 \u2243 (\u27e6 P \u27e7 \u228e \u27e6 Q \u27e7)\n \u2295\u2243\u228e = equiv \u2295\u2192\u228e \u228e\u2192\u2295 [inl: (\u03bb _ \u2192 refl) ,inr: (\u03bb _ \u2192 refl) ] \u228e\u2192\u2295\u2192\u228e\n where\n \u228e\u2192\u2295\u2192\u228e : \u2200 x \u2192 \u228e\u2192\u2295 (\u2295\u2192\u228e x) \u2261 x\n \u228e\u2192\u2295\u2192\u228e (`L , _) = refl\n \u228e\u2192\u2295\u2192\u228e (`R , _) = refl\n\n \u2295\u2261\u228e : {{_ : UA}} \u2192 \u27e6 P \u2295 Q \u27e7 \u2261 (\u27e6 P \u27e7 \u228e \u27e6 Q \u27e7)\n \u2295\u2261\u228e = ua \u2295\u2243\u228e\n\n &\u2192\u00d7 : \u27e6 P & Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n &\u2192\u00d7 p = p `L , p `R\n\n \u00d7\u2192& : \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P & Q \u27e7\n \u00d7\u2192& (p , q) `L = p\n \u00d7\u2192& (p , q) `R = q\n\n &\u2192\u00d7\u2192& : \u2200 x \u2192 &\u2192\u00d7 (\u00d7\u2192& x) \u2261 x\n &\u2192\u00d7\u2192& (p , q) = refl\n\n module _ {{_ : FunExt}} where\n \u00d7\u2192&\u2192\u00d7 : \u2200 x \u2192 \u00d7\u2192& (&\u2192\u00d7 x) \u2261 x\n \u00d7\u2192&\u2192\u00d7 p = \u03bb= \u03bb { `L \u2192 refl ; `R \u2192 refl }\n\n &\u2243\u00d7 : \u27e6 P & Q \u27e7 \u2243 (\u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7)\n &\u2243\u00d7 = &\u2192\u00d7 , record { linv = \u00d7\u2192& ; is-linv = \u00d7\u2192&\u2192\u00d7 ; rinv = \u00d7\u2192& ; is-rinv = &\u2192\u00d7\u2192& }\n\n &\u2261\u00d7 : {{_ : UA}} \u2192 \u27e6 P & Q \u27e7 \u2261 (\u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7)\n &\u2261\u00d7 = ua &\u2243\u00d7\n\nmodule _ P {{_ : FunExt}}{{_ : UA}} where\n P\u22a4-& : \u27e6 P\u22a4 & P \u27e7 \u2261 \u27e6 P \u27e7\n P\u22a4-& = &\u2261\u00d7 \u2219 ap (flip _\u00d7_ \u27e6 P \u27e7) P\u22a4-uniq \u2219 \u03a3\ud835\udfd9-snd\n\n P0-\u2295 : \u27e6 P0 \u2295 P \u27e7 \u2261 \u27e6 P \u27e7\n P0-\u2295 = \u2295\u2261\u228e \u2219 ap (flip _\u228e_ \u27e6 P \u27e7) \u03a3\ud835\udfd8-fst \u2219 \u228e-comm \u2219 ! \u228e\ud835\udfd8-inl\n\nmodule _ P Q R {{_ : FunExt}}{{_ : UA}} where\n &-assoc : \u27e6 P & (Q & R) \u27e7 \u2261 \u27e6 (P & Q) & R \u27e7\n &-assoc = &\u2261\u00d7 \u2219 (ap (_\u00d7_ \u27e6 P \u27e7) &\u2261\u00d7 \u2219 \u00d7-assoc \u2219 ap (flip _\u00d7_ \u27e6 R \u27e7) (! &\u2261\u00d7)) \u2219 ! &\u2261\u00d7\n\n \u2295-assoc : \u27e6 P \u2295 (Q \u2295 R) \u27e7 \u2261 \u27e6 (P \u2295 Q) \u2295 R \u27e7\n \u2295-assoc = \u2295\u2261\u228e \u2219 (ap (_\u228e_ \u27e6 P \u27e7) \u2295\u2261\u228e \u2219 \u228e-assoc \u2219 ap (flip _\u228e_ \u27e6 R \u27e7) (! \u2295\u2261\u228e)) \u2219 ! \u2295\u2261\u228e\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Function.NP\nopen import Type\nopen import Data.Product.NP renaming (proj\u2081 to fst; proj\u2082 to snd) using (_\u00d7_; _,_)\nopen import Data.Zero using (\ud835\udfd8)\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_]) hiding ([_,_]\u2032)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.LR\nopen import Relation.Binary.PropositionalEquality.NP using (_\u2261_; !_; _\u2219_; refl; ap)\nopen import Function.Extensionality\nopen import HoTT\nopen Equivalences\n\nopen import Control.Protocol\n\nmodule Control.Protocol.Additive where\n\nmodule send\/recv-\ud835\udfd8 (P : \ud835\udfd8 \u2192 Proto){{_ : FunExt}}{{_ : UA}} where\n P\u22a4 : Proto\n P\u22a4 = recvE \ud835\udfd8 P\n\n P0 : Proto\n P0 = sendE \ud835\udfd8 P\n\n P0-empty : \u27e6 P0 \u27e7 \u2261 \ud835\udfd8\n P0-empty = ua (equiv fst (\u03bb()) (\u03bb()) (\u03bb { (() , _) }))\n\n P\u22a4-uniq : \u27e6 P\u22a4 \u27e7 \u2261 \ud835\udfd9\n P\u22a4-uniq = \u03a0\ud835\udfd8-uniq _\n\nopen send\/recv-\ud835\udfd8 (\u03bb _ \u2192 end) public\n\n_\u2295_ : (l r : Proto) \u2192 Proto\nl \u2295 r = send [L: l R: r ]\n\n_&_ : (l r : Proto) \u2192 Proto\nl & r = recv [L: l R: r ]\n\nmodule _ {{_ : FunExt}} {P Q} where\n dual-\u2295 : dual (P \u2295 Q) \u2261 dual P & dual Q\n dual-\u2295 = recv=\u2032 [L: refl R: refl ]\n\n dual-& : dual (P & Q) \u2261 dual P \u2295 dual Q\n dual-& = send=\u2032 [L: refl R: refl ]\n\nmodule _ {{_ : FunExt}}{{_ : UA}} P Q where\n &-comm : P & Q \u2261 Q & P\n &-comm = recv\u2243 LR!-equiv [L: refl R: refl ]\n\n \u2295-comm : P \u2295 Q \u2261 Q \u2295 P\n \u2295-comm = send\u2243 LR!-equiv [L: refl R: refl ]\n\n -- additive mix (left-biased)\n amixL : \u27e6 P & Q \u27e7 \u2192 \u27e6 P \u2295 Q \u27e7\n amixL pq = (`L , pq `L)\n\n amixR : \u27e6 P & Q \u27e7 \u2192 \u27e6 P \u2295 Q \u27e7\n amixR pq = (`R , pq `R)\n\nmodule _ {P Q R S}(f : \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7)(g : \u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) where\n \u2295-map : \u27e6 P \u2295 R \u27e7 \u2192 \u27e6 Q \u2295 S \u27e7\n \u2295-map (`L , pr) = `L , f pr\n \u2295-map (`R , pr) = `R , g pr\n\n &-map : \u27e6 P & R \u27e7 \u2192 \u27e6 Q & S \u27e7\n &-map p `L = f (p `L)\n &-map p `R = g (p `R)\n\nmodule _ {P Q} where\n \u2295\u2192\u228e : \u27e6 P \u2295 Q \u27e7 \u2192 \u27e6 P \u27e7 \u228e \u27e6 Q \u27e7\n \u2295\u2192\u228e (`L , p) = inl p\n \u2295\u2192\u228e (`R , q) = inr q\n\n \u228e\u2192\u2295 : \u27e6 P \u27e7 \u228e \u27e6 Q \u27e7 \u2192 \u27e6 P \u2295 Q \u27e7\n \u228e\u2192\u2295 = [inl: _,_ `L ,inr: _,_ `R ]\n\n \u2295\u2243\u228e : \u27e6 P \u2295 Q \u27e7 \u2243 (\u27e6 P \u27e7 \u228e \u27e6 Q \u27e7)\n \u2295\u2243\u228e = equiv \u2295\u2192\u228e \u228e\u2192\u2295 [inl: (\u03bb _ \u2192 refl) ,inr: (\u03bb _ \u2192 refl) ] \u228e\u2192\u2295\u2192\u228e\n where\n \u228e\u2192\u2295\u2192\u228e : \u2200 x \u2192 \u228e\u2192\u2295 (\u2295\u2192\u228e x) \u2261 x\n \u228e\u2192\u2295\u2192\u228e (`L , _) = refl\n \u228e\u2192\u2295\u2192\u228e (`R , _) = refl\n\n \u2295\u2261\u228e : {{_ : UA}} \u2192 \u27e6 P \u2295 Q \u27e7 \u2261 (\u27e6 P \u27e7 \u228e \u27e6 Q \u27e7)\n \u2295\u2261\u228e = ua \u2295\u2243\u228e\n\n &\u2192\u00d7 : \u27e6 P & Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n &\u2192\u00d7 p = p `L , p `R\n\n \u00d7\u2192& : \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P & Q \u27e7\n \u00d7\u2192& (p , q) `L = p\n \u00d7\u2192& (p , q) `R = q\n\n &\u2192\u00d7\u2192& : \u2200 x \u2192 &\u2192\u00d7 (\u00d7\u2192& x) \u2261 x\n &\u2192\u00d7\u2192& (p , q) = refl\n\n module _ {{_ : FunExt}} where\n \u00d7\u2192&\u2192\u00d7 : \u2200 x \u2192 \u00d7\u2192& (&\u2192\u00d7 x) \u2261 x\n \u00d7\u2192&\u2192\u00d7 p = \u03bb= \u03bb { `L \u2192 refl ; `R \u2192 refl }\n\n &\u2243\u00d7 : \u27e6 P & Q \u27e7 \u2243 (\u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7)\n &\u2243\u00d7 = &\u2192\u00d7 , record { linv = \u00d7\u2192& ; is-linv = \u00d7\u2192&\u2192\u00d7 ; rinv = \u00d7\u2192& ; is-rinv = &\u2192\u00d7\u2192& }\n\n &\u2261\u00d7 : {{_ : UA}} \u2192 \u27e6 P & Q \u27e7 \u2261 (\u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7)\n &\u2261\u00d7 = ua &\u2243\u00d7\n\nmodule _ P {{_ : FunExt}}{{_ : UA}} where\n P\u22a4-& : \u27e6 P\u22a4 & P \u27e7 \u2261 \u27e6 P \u27e7\n P\u22a4-& = &\u2261\u00d7 \u2219 ap (flip _\u00d7_ \u27e6 P \u27e7) P\u22a4-uniq \u2219 \u03a3\ud835\udfd9-snd\n\n P0-\u2295 : \u27e6 P0 \u2295 P \u27e7 \u2261 \u27e6 P \u27e7\n P0-\u2295 = \u2295\u2261\u228e \u2219 ap (flip _\u228e_ \u27e6 P \u27e7) \u03a3\ud835\udfd8-fst \u2219 \u228e-comm \u2219 ! \u228e\ud835\udfd8-inl\n\nmodule _ P Q R {{_ : FunExt}}{{_ : UA}} where\n &-assoc : \u27e6 P & (Q & R) \u27e7 \u2261 \u27e6 (P & Q) & R \u27e7\n &-assoc = &\u2261\u00d7 \u2219 (ap (_\u00d7_ \u27e6 P \u27e7) &\u2261\u00d7 \u2219 \u00d7-assoc \u2219 ap (flip _\u00d7_ \u27e6 R \u27e7) (! &\u2261\u00d7)) \u2219 ! &\u2261\u00d7\n\n \u2295-assoc : \u27e6 P \u2295 (Q \u2295 R) \u27e7 \u2261 \u27e6 (P \u2295 Q) \u2295 R \u27e7\n \u2295-assoc = \u2295\u2261\u228e \u2219 (ap (_\u228e_ \u27e6 P \u27e7) \u2295\u2261\u228e \u2219 \u228e-assoc \u2219 ap (flip _\u228e_ \u27e6 R \u27e7) (! \u2295\u2261\u228e)) \u2219 ! \u2295\u2261\u228e\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"4f7fa7c66950d9794f12eff0ea347f1fd2e7c52f","subject":"program-disantce: upgrade","message":"program-disantce: upgrade\n","repos":"crypto-agda\/crypto-agda","old_file":"program-distance.agda","new_file":"program-distance.agda","new_contents":"module program-distance where\n\nopen import flipbased-implem\nopen import Data.Bits\nopen import Data.Bool\nopen import Data.Vec.NP using (Vec; count; count\u1da0)\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\nopen import Relation.Nullary\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\nimport Data.Fin as Fin\n\nrecord PrgDist : Set\u2081 where\n constructor mk\n field\n _]-[_ : \u2200 {m n} \u2192 \u2141 m \u2192 \u2141 n \u2192 Set\n ]-[-antisym : \u2200 {n} (f : \u2141 n) \u2192 \u00ac (f ]-[ f)\n ]-[-sym : \u2200 {m n} {f : \u2141 m} {g : \u2141 n} \u2192 f ]-[ g \u2192 g ]-[ f\n ]-[-cong-left-\u2248\u21ba : \u2200 {m n o} {f : \u2141 m} {g : \u2141 n} {h : \u2141 o} \u2192 f \u2248\u2141 g \u2192 g ]-[ h \u2192 f ]-[ h\n\n ]-[-cong-right-\u2248\u21ba : \u2200 {m n o} {f : \u2141 m} {g : \u2141 n} {h : \u2141 o} \u2192 f ]-[ g \u2192 g \u2248\u2141 h \u2192 f ]-[ h\n ]-[-cong-right-\u2248\u21ba pf pf' = ]-[-sym (]-[-cong-left-\u2248\u21ba (sym pf') (]-[-sym pf))\n\n ]-[-cong-\u2257\u21ba : \u2200 {c c'} {f g : \u2141 c} {f' g' : \u2141 c'} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f ]-[ f' \u2192 g ]-[ g'\n ]-[-cong-\u2257\u21ba {c} {c'} {f} {g} {f'} {g'} f\u2257g f'\u2257g' pf\n = ]-[-cong-left-\u2248\u21ba {f = g} {g = f} {h = g'}\n ((\u2257\u21d2\u2248\u2141 (\u03bb x \u2192 sym (f\u2257g x)))) (]-[-cong-right-\u2248\u21ba pf (\u2257\u21d2\u2248\u2141 f'\u2257g'))\n\n breaks : \u2200 {c} (EXP : Bit \u2192 \u2141 c) \u2192 Set\n breaks \u2141 = \u2141 0b ]-[ \u2141 1b\n\n -- An wining adversary for game \u2141\u2080 reduces to a wining adversary for game \u2141\u2081\n _\u21d3_ : \u2200 {c\u2080 c\u2081} (\u2141\u2080 : Bit \u2192 \u2141 c\u2080) (\u2141\u2081 : Bit \u2192 \u2141 c\u2081) \u2192 Set\n \u2141\u2080 \u21d3 \u2141\u2081 = breaks \u2141\u2080 \u2192 breaks \u2141\u2081\n\n extensional-reduction : \u2200 {c} {\u2141\u2080 \u2141\u2081 : Bit \u2192 \u2141 c}\n \u2192 \u2141\u2080 \u2257\u2141 \u2141\u2081 \u2192 \u2141\u2080 \u21d3 \u2141\u2081\n extensional-reduction same-games = ]-[-cong-\u2257\u21ba (same-games 0b) (same-games 1b)\n\nmodule HomImplem k where\n -- | Pr[ f \u2261 1 ] - Pr[ g \u2261 1 ] | \u2265 \u03b5 [ on reals ]\n -- dist Pr[ f \u2261 1 ] Pr[ g \u2261 1 ] \u2265 \u03b5 [ on reals ]\n -- dist (#1 f \/ 2^ c) (#1 g \/ 2^ c) \u2265 \u03b5 [ on reals ]\n -- dist (#1 f) (#1 g) \u2265 \u03b5 * 2^ c where \u03b5 = 2^ -k [ on rationals ]\n -- dist (#1 f) (#1 g) \u2265 2^(-k) * 2^ c [ on rationals ]\n -- dist (#1 f) (#1 g) \u2265 2^(c - k) [ on rationals ]\n -- dist (#1 f) (#1 g) \u2265 2^(c \u2238 k) [ on natural ]\n _]-[_ : \u2200 {n} (f g : \u2141 n) \u2192 Set\n _]-[_ {n} f g = dist (count\u21ba f) (count\u21ba g) \u2265 2^(n \u2238 k)\n\n ]-[-antisym : \u2200 {n} (f : \u2141 n) \u2192 \u00ac (f ]-[ f)\n ]-[-antisym {n} f f]-[g rewrite dist-refl (count\u21ba f) with \u2115\u2264.trans (1\u22642^ (n \u2238 k)) f]-[g\n ... | ()\n\n ]-[-sym : \u2200 {n} {f g : \u2141 n} \u2192 f ]-[ g \u2192 g ]-[ f\n ]-[-sym {n} {f} {g} f]-[g rewrite dist-sym (count\u21ba f) (count\u21ba g) = f]-[g\n\n ]-[-cong-left-\u2248\u21ba : \u2200 {n} {f g h : \u2141 n} \u2192 f \u2248\u2141 g \u2192 g ]-[ h \u2192 f ]-[ h\n ]-[-cong-left-\u2248\u21ba {n} {f} {g} f\u2248g g]-[h rewrite \u2248\u2141\u21d2\u2248\u2141\u2032 {n} {f} {g} f\u2248g = g]-[h\n -- dist #g #h \u2265 2^(n \u2238 k)\n -- dist #f #h \u2265 2^(n \u2238 k)\n\nmodule Implem k where\n _]-[_ : \u2200 {m n} \u2192 \u2141 m \u2192 \u2141 n \u2192 Set\n _]-[_ {m} {n} f g = dist \u27e82^ n * count\u21ba f \u27e9 \u27e82^ m * count\u21ba g \u27e9 \u2265 2^((m + n) \u2238 k)\n\n ]-[-antisym : \u2200 {n} (f : \u2141 n) \u2192 \u00ac (f ]-[ f)\n ]-[-antisym {n} f f]-[g rewrite dist-refl \u27e82^ n * count\u21ba f \u27e9 with \u2115\u2264.trans (1\u22642^ (n + n \u2238 k)) f]-[g\n ... | ()\n\n ]-[-sym : \u2200 {m n} {f : \u2141 m} {g : \u2141 n} \u2192 f ]-[ g \u2192 g ]-[ f\n ]-[-sym {m} {n} {f} {g} f]-[g rewrite dist-sym \u27e82^ n * count\u21ba f \u27e9 \u27e82^ m * count\u21ba g \u27e9 | \u2115\u00b0.+-comm m n = f]-[g\n\n postulate\n helper : \u2200 m n o k \u2192 m + ((n + o) \u2238 k) \u2261 n + ((m + o) \u2238 k)\n helper\u2032 : \u2200 m n o k \u2192 \u27e82^ m * (2^((n + o) \u2238 k))\u27e9 \u2261 \u27e82^ n * (2^((m + o) \u2238 k))\u27e9\n\n ]-[-cong-left-\u2248\u21ba : \u2200 {m n o} {f : \u2141 m} {g : \u2141 n} {h : \u2141 o} \u2192 f \u2248\u2141 g \u2192 g ]-[ h \u2192 f ]-[ h\n ]-[-cong-left-\u2248\u21ba {m} {n} {o} {f} {g} {h} f\u2248g g]-[h\n with 2^*-mono m g]-[h\n -- 2\u1d50(dist 2\u1d52#g 2\u207f#h) \u2264 2\u1d502\u207f\u207a\u1d52\u207b\u1d4f\n ... | q rewrite sym (dist-2^* m \u27e82^ o * count\u21ba g \u27e9 \u27e82^ n * count\u21ba h \u27e9)\n -- dist 2\u1d502\u1d52#g 2\u1d502\u207f#h \u2264 2\u1d502\u207f\u207a\u1d52\u207b\u1d4f\n | 2^-comm m o (count\u21ba g)\n -- dist 2\u1d522\u1d50#g 2\u1d502\u207f#h \u2264 2\u1d502\u207f\u207a\u1d52\u207b\u1d4f\n | sym f\u2248g\n -- dist 2\u1d522\u207f#f 2\u1d502\u207f#h \u2264 2\u1d502\u207f\u207a\u1d52\u207b\u1d4f\n | 2^-comm o n (count\u21ba f)\n -- dist 2\u207f2\u1d52#f 2\u1d502\u207f#h \u2264 2\u1d502\u207f\u207a\u1d52\u207b\u1d4f\n | 2^-comm m n (count\u21ba h)\n -- dist 2\u207f2\u1d52#f 2\u207f2\u1d50#h \u2264 2\u1d502\u207f\u207a\u1d52\u207b\u1d4f\n | dist-2^* n \u27e82^ o * count\u21ba f \u27e9 \u27e82^ m * count\u21ba h \u27e9\n -- 2\u207f(dist 2\u1d52#f 2\u1d50#h) \u2264 2\u1d502\u207f\u207a\u1d52\u207b\u1d4f\n | 2^-+ m (n + o \u2238 k) 1\n -- 2\u207f(dist 2\u1d52#f 2\u1d50#h) \u2264 2\u1d50\u207a\u207f\u207a\u1d52\u207b\u1d4f\n | helper m n o k\n -- 2\u207f(dist 2\u1d52#f 2\u1d50#h) \u2264 2\u207f\u207a\u1d50\u207a\u1d52\u207b\u1d4f\n | sym (2^-+ n (m + o \u2238 k) 1)\n -- 2\u207f(dist 2\u1d52#f 2\u1d50#h) \u2264 2\u207f2\u1d50\u207a\u1d52\u207b\u1d4f\n = 2^*-mono\u2032 n q\n -- dist 2\u1d52#f 2\u1d50#h \u2264 2\u1d50\u207a\u1d52\u207b\u1d4f\n\n prgDist : PrgDist\n prgDist = mk _]-[_\n ]-[-antisym\n (\u03bb {m n f g} \u2192 ]-[-sym {f = f} {g})\n (\u03bb {m n o f g h} \u2192 ]-[-cong-left-\u2248\u21ba {f = f} {g} {h})\n","old_contents":"module program-distance where\n\nopen import flipbased-implem\nopen import Data.Bits\nopen import Data.Bool\nopen import Data.Vec.NP using (Vec; count; count\u1da0)\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\nopen import Relation.Nullary\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\nimport Data.Fin as Fin\n\nrecord PrgDist : Set\u2081 where\n constructor mk\n field\n _]-[_ : \u2200 {m n} \u2192 \u2141 m \u2192 \u2141 n \u2192 Set\n ]-[-antisym : \u2200 {n} (f : \u2141 n) \u2192 \u00ac (f ]-[ f)\n ]-[-sym : \u2200 {m n} {f : \u2141 m} {g : \u2141 n} \u2192 f ]-[ g \u2192 g ]-[ f\n ]-[-cong-left-\u2248\u21ba : \u2200 {m n o} {f : \u2141 m} {g : \u2141 n} {h : \u2141 o} \u2192 f \u2248\u2141 g \u2192 g ]-[ h \u2192 f ]-[ h\n\n ]-[-cong-right-\u2248\u21ba : \u2200 {m n o} {f : \u2141 m} {g : \u2141 n} {h : \u2141 o} \u2192 f ]-[ g \u2192 g \u2248\u2141 h \u2192 f ]-[ h\n ]-[-cong-right-\u2248\u21ba pf pf' = ]-[-sym (]-[-cong-left-\u2248\u21ba (sym pf') (]-[-sym pf))\n\n ]-[-cong-\u2257\u21ba : \u2200 {c c'} {f g : \u2141 c} {f' g' : \u2141 c'} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f ]-[ f' \u2192 g ]-[ g'\n ]-[-cong-\u2257\u21ba {c} {c'} {f} {g} {f'} {g'} f\u2257g f'\u2257g' pf\n = ]-[-cong-left-\u2248\u21ba {f = g} {g = f} {h = g'}\n ((\u2257\u21d2\u2248\u2141 (\u03bb x \u2192 sym (f\u2257g x)))) (]-[-cong-right-\u2248\u21ba pf (\u2257\u21d2\u2248\u2141 f'\u2257g'))\n\n breaks : \u2200 {c} (EXP : Bit \u2192 \u2141 c) \u2192 Set\n breaks \u2141 = \u2141 0b ]-[ \u2141 1b\n\n -- An wining adversary for game \u2141\u2080 reduces to a wining adversary for game \u2141\u2081\n _\u21d3_ : \u2200 {c\u2080 c\u2081} (\u2141\u2080 : Bit \u2192 \u2141 c\u2080) (\u2141\u2081 : Bit \u2192 \u2141 c\u2081) \u2192 Set\n \u2141\u2080 \u21d3 \u2141\u2081 = breaks \u2141\u2080 \u2192 breaks \u2141\u2081\n\n extensional-reduction : \u2200 {c} {\u2141\u2080 \u2141\u2081 : Bit \u2192 \u2141 c}\n \u2192 \u2141\u2080 \u2257\u2141 \u2141\u2081 \u2192 \u2141\u2080 \u21d3 \u2141\u2081\n extensional-reduction same-games = ]-[-cong-\u2257\u21ba (same-games 0b) (same-games 1b)\n\nprivate\n 2^_\u22651 : \u2200 n \u2192 2^ n \u2265 1\n 2^ zero \u22651 = s\u2264s z\u2264n\n 2^ suc n \u22651 = z\u2264n {2^ n} +-mono 2^ n \u22651\n\nmodule HomImplem k where\n -- | Pr[ f \u2261 1 ] - Pr[ g \u2261 1 ] | \u2265 \u03b5 [ on reals ]\n -- dist Pr[ f \u2261 1 ] Pr[ g \u2261 1 ] \u2265 \u03b5 [ on reals ]\n -- dist (#1 f \/ 2^ c) (#1 g \/ 2^ c) \u2265 \u03b5 [ on reals ]\n -- dist (#1 f) (#1 g) \u2265 \u03b5 * 2^ c where \u03b5 = 2^ -k [ on rationals ]\n -- dist (#1 f) (#1 g) \u2265 2^(-k) * 2^ c [ on rationals ]\n -- dist (#1 f) (#1 g) \u2265 2^(c - k) [ on rationals ]\n -- dist (#1 f) (#1 g) \u2265 2^(c \u2238 k) [ on natural ]\n _]-[_ : \u2200 {n} (f g : \u2141 n) \u2192 Set\n _]-[_ {n} f g = dist (count\u21ba f) (count\u21ba g) \u2265 2^(n \u2238 k)\n\n ]-[-antisym : \u2200 {n} (f : \u2141 n) \u2192 \u00ac (f ]-[ f)\n ]-[-antisym {n} f f]-[g rewrite dist-refl (count\u21ba f) with \u2115\u2264.trans (2^ (n \u2238 k) \u22651) f]-[g\n ... | ()\n\n ]-[-sym : \u2200 {n} {f g : \u2141 n} \u2192 f ]-[ g \u2192 g ]-[ f\n ]-[-sym {n} {f} {g} f]-[g rewrite dist-sym (count\u21ba f) (count\u21ba g) = f]-[g\n\n ]-[-cong-left-\u2248\u21ba : \u2200 {n} {f g h : \u2141 n} \u2192 f \u2248\u2141 g \u2192 g ]-[ h \u2192 f ]-[ h\n ]-[-cong-left-\u2248\u21ba {n} {f} {g} f\u2248g g]-[h rewrite \u2248\u2141\u21d2\u2248\u2141\u2032 {n} {f} {g} f\u2248g = g]-[h\n -- dist #g #h \u2265 2^(n \u2238 k)\n -- dist #f #h \u2265 2^(n \u2238 k)\n\nmodule Implem k where\n _]-[_ : \u2200 {m n} \u2192 \u2141 m \u2192 \u2141 n \u2192 Set\n _]-[_ {m} {n} f g = dist \u27e82^ n * count\u21ba f \u27e9 \u27e82^ m * count\u21ba g \u27e9 \u2265 2^((m + n) \u2238 k)\n\n ]-[-antisym : \u2200 {n} (f : \u2141 n) \u2192 \u00ac (f ]-[ f)\n ]-[-antisym {n} f f]-[g rewrite dist-refl \u27e82^ n * count\u21ba f \u27e9 with \u2115\u2264.trans (2^ (n + n \u2238 k) \u22651) f]-[g\n ... | ()\n\n ]-[-sym : \u2200 {m n} {f : \u2141 m} {g : \u2141 n} \u2192 f ]-[ g \u2192 g ]-[ f\n ]-[-sym {m} {n} {f} {g} f]-[g rewrite dist-sym \u27e82^ n * count\u21ba f \u27e9 \u27e82^ m * count\u21ba g \u27e9 | \u2115\u00b0.+-comm m n = f]-[g\n\n postulate\n helper : \u2200 m n o k \u2192 m + ((n + o) \u2238 k) \u2261 n + ((m + o) \u2238 k)\n helper\u2032 : \u2200 m n o k \u2192 \u27e82^ m * (2^((n + o) \u2238 k))\u27e9 \u2261 \u27e82^ n * (2^((m + o) \u2238 k))\u27e9\n\n ]-[-cong-left-\u2248\u21ba : \u2200 {m n o} {f : \u2141 m} {g : \u2141 n} {h : \u2141 o} \u2192 f \u2248\u2141 g \u2192 g ]-[ h \u2192 f ]-[ h\n ]-[-cong-left-\u2248\u21ba {m} {n} {o} {f} {g} {h} f\u2248g g]-[h\n with 2^*-mono m g]-[h\n -- 2\u1d50(dist 2\u1d52#g 2\u207f#h) \u2264 2\u1d502\u207f\u207a\u1d52\u207b\u1d4f\n ... | q rewrite sym (dist-2^* m \u27e82^ o * count\u21ba g \u27e9 \u27e82^ n * count\u21ba h \u27e9)\n -- dist 2\u1d502\u1d52#g 2\u1d502\u207f#h \u2264 2\u1d502\u207f\u207a\u1d52\u207b\u1d4f\n | 2^-comm m o (count\u21ba g)\n -- dist 2\u1d522\u1d50#g 2\u1d502\u207f#h \u2264 2\u1d502\u207f\u207a\u1d52\u207b\u1d4f\n | sym f\u2248g\n -- dist 2\u1d522\u207f#f 2\u1d502\u207f#h \u2264 2\u1d502\u207f\u207a\u1d52\u207b\u1d4f\n | 2^-comm o n (count\u21ba f)\n -- dist 2\u207f2\u1d52#f 2\u1d502\u207f#h \u2264 2\u1d502\u207f\u207a\u1d52\u207b\u1d4f\n | 2^-comm m n (count\u21ba h)\n -- dist 2\u207f2\u1d52#f 2\u207f2\u1d50#h \u2264 2\u1d502\u207f\u207a\u1d52\u207b\u1d4f\n | dist-2^* n \u27e82^ o * count\u21ba f \u27e9 \u27e82^ m * count\u21ba h \u27e9\n -- 2\u207f(dist 2\u1d52#f 2\u1d50#h) \u2264 2\u1d502\u207f\u207a\u1d52\u207b\u1d4f\n | 2^-+ m (n + o \u2238 k) 1\n -- 2\u207f(dist 2\u1d52#f 2\u1d50#h) \u2264 2\u1d50\u207a\u207f\u207a\u1d52\u207b\u1d4f\n | helper m n o k\n -- 2\u207f(dist 2\u1d52#f 2\u1d50#h) \u2264 2\u207f\u207a\u1d50\u207a\u1d52\u207b\u1d4f\n | sym (2^-+ n (m + o \u2238 k) 1)\n -- 2\u207f(dist 2\u1d52#f 2\u1d50#h) \u2264 2\u207f2\u1d50\u207a\u1d52\u207b\u1d4f\n = 2^*-mono\u2032 n q\n -- dist 2\u1d52#f 2\u1d50#h \u2264 2\u1d50\u207a\u1d52\u207b\u1d4f\n\n prgDist : PrgDist\n prgDist = mk _]-[_\n ]-[-antisym\n (\u03bb {m n f g} \u2192 ]-[-sym {f = f} {g})\n (\u03bb {m n o f g h} \u2192 ]-[-cong-left-\u2248\u21ba {f = f} {g} {h})\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"87661ddb46c3c4fe87733c8324a74ae28bd65966","subject":"Describe the purpose of binding.adga (see #28).","message":"Describe the purpose of binding.adga (see #28).\n\nThere are two comments because I intend to split this file.\n\nOld-commit-hash: 4156d12adc58a4ac0856145a0353e54b14c9a865\n","repos":"inc-lc\/ilc-agda","old_file":"binding.agda","new_file":"binding.agda","new_contents":"module binding\n (Type : Set)\n (\u27e6_\u27e7Type : Type \u2192 Set)\n where\n\n-- CONTEXTS\n--\n-- This module defines the syntax of contexts, prefixes of\n-- contexts and variables and properties of these notions.\n--\n-- This module is parametric in the syntax of types, so it\n-- can be reused for different calculi.\n\n-- ENVIRONMENTS\n--\n-- This module defines the meaning of contexts, that is,\n-- the type of environments that fit a context, together\n-- with operations and properties of these operations.\n--\n-- This module is parametric in the syntax and semantics\n-- of types, so it can be reused for different calculi\n-- and models.\n\nopen import meaning\n\nprivate\n meaningOfType : Meaning Type\n meaningOfType = meaning \u27e6_\u27e7Type\n\n-- TYPING CONTEXTS\n\n-- Syntax\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n-- Denotational Semantics : Contexts Represent Environments\n\ndata Empty : Set where\n \u2205 : Empty\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = Empty\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n-- VARIABLES\n\n-- Syntax\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n-- WEAKENING\n\n-- Prefix of a context\n\ndata Prefix : Context \u2192 Set where\n \u2205 : \u2200 {\u0393} \u2192 Prefix \u0393\n _\u2022_ : \u2200 {\u0393} \u2192 (\u03c4 : Type) \u2192 Prefix \u0393 \u2192 Prefix (\u03c4 \u2022 \u0393)\n\n-- take only the prefix of a context\n\ntake : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\ntake \u0393 \u2205 = \u2205\ntake (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) = \u03c4 \u2022 take \u0393 \u0393\u2032\n\n-- drop the prefix of a context\n\ndrop : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\ndrop \u0393 \u2205 = \u0393\ndrop (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) = drop \u0393 \u0393\u2032\n\n-- Extend a context to a super context\n\ninfixr 10 _\u22ce_\n\n_\u22ce_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Context\n\u2205 \u22ce \u0393\u2082 = \u0393\u2082\n(\u03c4 \u2022 \u0393\u2081) \u22ce \u0393\u2082 = \u03c4 \u2022 \u0393\u2081 \u22ce \u0393\u2082\n\n-- Remove a variable from an environment\n\nweakenEnv : \u2200 \u0393\u2081 \u03c4\u2082 {\u0393\u2083} \u2192 \u27e6 \u0393\u2081 \u22ce (\u03c4\u2082 \u2022 \u0393\u2083) \u27e7 \u2192 \u27e6 \u0393\u2081 \u22ce \u0393\u2083 \u27e7\nweakenEnv \u2205 \u03c4\u2082 (v \u2022 \u03c1) = \u03c1\nweakenEnv (\u03c4 \u2022 \u0393\u2081) \u03c4\u2082 (v \u2022 \u03c1) = v \u2022 weakenEnv \u0393\u2081 \u03c4\u2082 \u03c1\n\nopen import Relation.Binary.PropositionalEquality\n\ntake-drop : \u2200 \u0393 \u0393\u2032 \u2192 take \u0393 \u0393\u2032 \u22ce drop \u0393 \u0393\u2032 \u2261 \u0393\ntake-drop \u2205 \u2205 = refl\ntake-drop (\u03c4 \u2022 \u0393) \u2205 = refl\ntake-drop (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) rewrite take-drop \u0393 \u0393\u2032 = refl\n\nor-unit : \u2200 \u0393 \u2192 \u0393 \u22ce \u2205 \u2261 \u0393\nor-unit \u2205 = refl\nor-unit (\u03c4 \u2022 \u0393) rewrite or-unit \u0393 = refl\n\nmove-prefix : \u2200 \u0393 \u03c4 \u0393\u2032 \u2192\n \u0393 \u22ce (\u03c4 \u2022 \u0393\u2032) \u2261 (\u0393 \u22ce (\u03c4 \u2022 \u2205)) \u22ce \u0393\u2032\nmove-prefix \u2205 \u03c4 \u0393\u2032 = refl\nmove-prefix (\u03c4 \u2022 \u0393) \u03c4\u2081 \u2205 = sym (or-unit (\u03c4 \u2022 \u0393 \u22ce (\u03c4\u2081 \u2022 \u2205)))\nmove-prefix (\u03c4 \u2022 \u0393) \u03c4\u2081 (\u03c4\u2082 \u2022 \u0393\u2032) rewrite move-prefix \u0393 \u03c4\u2081 (\u03c4\u2082 \u2022 \u0393\u2032) = refl\n\n-- Lift a variable to a super context\n\nlift : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nlift {\u2205} {\u2205} x = x\nlift {\u2205} {\u03c4 \u2022 \u0393\u2082} x = that (lift {\u2205} {\u0393\u2082} x)\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} this = this\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} (that x) = that (lift {\u0393\u2081} {\u0393\u2082} x)\n","old_contents":"module binding\n (Type : Set)\n (\u27e6_\u27e7Type : Type \u2192 Set)\n where\n\nopen import meaning\n\nprivate\n meaningOfType : Meaning Type\n meaningOfType = meaning \u27e6_\u27e7Type\n\n-- TYPING CONTEXTS\n\n-- Syntax\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n-- Denotational Semantics : Contexts Represent Environments\n\ndata Empty : Set where\n \u2205 : Empty\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = Empty\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n-- VARIABLES\n\n-- Syntax\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n-- WEAKENING\n\n-- Prefix of a context\n\ndata Prefix : Context \u2192 Set where\n \u2205 : \u2200 {\u0393} \u2192 Prefix \u0393\n _\u2022_ : \u2200 {\u0393} \u2192 (\u03c4 : Type) \u2192 Prefix \u0393 \u2192 Prefix (\u03c4 \u2022 \u0393)\n\n-- take only the prefix of a context\n\ntake : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\ntake \u0393 \u2205 = \u2205\ntake (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) = \u03c4 \u2022 take \u0393 \u0393\u2032\n\n-- drop the prefix of a context\n\ndrop : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\ndrop \u0393 \u2205 = \u0393\ndrop (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) = drop \u0393 \u0393\u2032\n\n-- Extend a context to a super context\n\ninfixr 10 _\u22ce_\n\n_\u22ce_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Context\n\u2205 \u22ce \u0393\u2082 = \u0393\u2082\n(\u03c4 \u2022 \u0393\u2081) \u22ce \u0393\u2082 = \u03c4 \u2022 \u0393\u2081 \u22ce \u0393\u2082\n\n-- Remove a variable from an environment\n\nweakenEnv : \u2200 \u0393\u2081 \u03c4\u2082 {\u0393\u2083} \u2192 \u27e6 \u0393\u2081 \u22ce (\u03c4\u2082 \u2022 \u0393\u2083) \u27e7 \u2192 \u27e6 \u0393\u2081 \u22ce \u0393\u2083 \u27e7\nweakenEnv \u2205 \u03c4\u2082 (v \u2022 \u03c1) = \u03c1\nweakenEnv (\u03c4 \u2022 \u0393\u2081) \u03c4\u2082 (v \u2022 \u03c1) = v \u2022 weakenEnv \u0393\u2081 \u03c4\u2082 \u03c1\n\nopen import Relation.Binary.PropositionalEquality\n\ntake-drop : \u2200 \u0393 \u0393\u2032 \u2192 take \u0393 \u0393\u2032 \u22ce drop \u0393 \u0393\u2032 \u2261 \u0393\ntake-drop \u2205 \u2205 = refl\ntake-drop (\u03c4 \u2022 \u0393) \u2205 = refl\ntake-drop (\u03c4 \u2022 \u0393) (.\u03c4 \u2022 \u0393\u2032) rewrite take-drop \u0393 \u0393\u2032 = refl\n\nor-unit : \u2200 \u0393 \u2192 \u0393 \u22ce \u2205 \u2261 \u0393\nor-unit \u2205 = refl\nor-unit (\u03c4 \u2022 \u0393) rewrite or-unit \u0393 = refl\n\nmove-prefix : \u2200 \u0393 \u03c4 \u0393\u2032 \u2192\n \u0393 \u22ce (\u03c4 \u2022 \u0393\u2032) \u2261 (\u0393 \u22ce (\u03c4 \u2022 \u2205)) \u22ce \u0393\u2032\nmove-prefix \u2205 \u03c4 \u0393\u2032 = refl\nmove-prefix (\u03c4 \u2022 \u0393) \u03c4\u2081 \u2205 = sym (or-unit (\u03c4 \u2022 \u0393 \u22ce (\u03c4\u2081 \u2022 \u2205)))\nmove-prefix (\u03c4 \u2022 \u0393) \u03c4\u2081 (\u03c4\u2082 \u2022 \u0393\u2032) rewrite move-prefix \u0393 \u03c4\u2081 (\u03c4\u2082 \u2022 \u0393\u2032) = refl\n\n-- Lift a variable to a super context\n\nlift : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nlift {\u2205} {\u2205} x = x\nlift {\u2205} {\u03c4 \u2022 \u0393\u2082} x = that (lift {\u2205} {\u0393\u2082} x)\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} this = this\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} (that x) = that (lift {\u0393\u2081} {\u0393\u2082} x)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"568f209dbe43ea336be8078951c585c924fb4d9d","subject":"bintree: toFun\/fromFun","message":"bintree: toFun\/fromFun\n","repos":"crypto-agda\/crypto-agda","old_file":"bintree.agda","new_file":"bintree.agda","new_contents":"module bintree where\n\nopen import Function\nopen import Data.Nat\nopen import Data.Bool\nopen import Data.Bits\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\n\ndata Tree {a} (A : Set a) : \u2115 \u2192 Set a where\n leaf : \u2200 {n} \u2192 A \u2192 Tree A n\n fork : \u2200 {n} (left right : Tree A n) \u2192 Tree A (suc n)\n\nfromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Tree A n\nfromFun {zero} f = leaf (f [])\nfromFun {suc n} f = fork (fromFun (f \u2218 0\u2237_)) (fromFun (f \u2218 1\u2237_))\n\ntoFun : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Bits n \u2192 A\ntoFun (leaf x) _ = x\ntoFun (fork left right) (b \u2237 bs) = toFun (if b then right else left) bs\n\ntoFun\u2218fromFun : \u2200 {n a} {A : Set a} (f : Bits n \u2192 A) \u2192 toFun (fromFun f) \u2257 f\ntoFun\u2218fromFun {zero} f [] = refl\ntoFun\u2218fromFun {suc n} f (0b \u2237 bs) = toFun\u2218fromFun {n} (f \u2218 0\u2237_) bs\ntoFun\u2218fromFun {suc n} f (1b \u2237 bs) = toFun\u2218fromFun {n} (f \u2218 1\u2237_) bs\n\nleaf\u207f : \u2200 {n a} {A : Set a} \u2192 A \u2192 Tree A n\nleaf\u207f {zero} x = leaf x\nleaf\u207f {suc n} x = fork t t where t = leaf\u207f x\n\nexpand : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Tree A n\nexpand (leaf x) = leaf\u207f x\nexpand (fork t\u2080 t\u2081) = fork (expand t\u2080) (expand t\u2081)\n\nfromConst\u2261leaf\u207f : \u2200 {n a} {A : Set a} (x : A) \u2192 fromFun (const x) \u2261 leaf\u207f {n} x\nfromConst\u2261leaf\u207f {zero} _ = refl\nfromConst\u2261leaf\u207f {suc n} x rewrite fromConst\u2261leaf\u207f {n} x = refl\n\nfromFun\u2218toFun : \u2200 {n a} {A : Set a} (t : Tree A n) \u2192 fromFun (toFun t) \u2261 expand t\nfromFun\u2218toFun (leaf x) = fromConst\u2261leaf\u207f x\nfromFun\u2218toFun (fork t\u2080 t\u2081) = cong\u2082 fork (fromFun\u2218toFun t\u2080) (fromFun\u2218toFun t\u2081)\n\nlookup : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Tree A n \u2192 A\nlookup = flip toFun\n\nmap : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 Tree A n \u2192 Tree B n\nmap f (leaf x) = leaf (f x)\nmap f (fork t\u2080 t\u2081) = fork (map f t\u2080) (map f t\u2081)\n\ndata Rot {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n leaf : \u2200 {n} x \u2192 Rot {n = n} (leaf x) (leaf x)\n fork : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 left\u2081 \u2192\n Rot right\u2080 right\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n krof : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 right\u2081 \u2192\n Rot right\u2080 left\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n\nRot-refl : \u2200 {n a} {A : Set a} \u2192 Reflexive (Rot {A = A} {n})\nRot-refl {x = leaf x} = leaf x\nRot-refl {x = fork left right} = fork Rot-refl Rot-refl\n\nRot-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Rot {A = A} {n})\nRot-sym (leaf x) = leaf x\nRot-sym (fork p\u2080 p\u2081) = fork (Rot-sym p\u2080) (Rot-sym p\u2081)\nRot-sym (krof p\u2080 p\u2081) = krof (Rot-sym p\u2081) (Rot-sym p\u2080)\n\nRot-trans : \u2200 {n a} {A : Set a} \u2192 Transitive (Rot {A = A} {n})\nRot-trans (leaf x) q = q\nRot-trans (fork p\u2080 p\u2081) (fork q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (fork p\u2080 p\u2081) (krof q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (krof p\u2080 p\u2081) (fork q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\nRot-trans (krof p\u2080 p\u2081) (krof q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\n","old_contents":"module bintree where\n\nopen import Function\nopen import Data.Nat\nopen import Data.Bool\nopen import Data.Bits\n\ndata Tree {a} (A : Set a) : \u2115 \u2192 Set a where\n leaf : \u2200 {n} \u2192 A \u2192 Tree A n\n fork : \u2200 {n} \u2192 (left right : Tree A n) \u2192 Tree A (suc n)\n\nfromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Tree A n\nfromFun {zero} f = leaf (f [])\nfromFun {suc n} f = fork (fromFun (f \u2218 0\u2237_)) (fromFun (f \u2218 1\u2237_))\n\ntoFun : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Bits n \u2192 A\ntoFun (leaf x) _ = x\ntoFun (fork left right) (b \u2237 bs) = toFun (if b then left else right) bs\n\nlookup : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Tree A n \u2192 A\nlookup = flip toFun\n\nmap : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 Tree A n \u2192 Tree B n\nmap f (leaf x) = leaf (f x)\nmap f (fork t\u2080 t\u2081) = fork (map f t\u2080) (map f t\u2081)\n\ndata Rot {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n leaf : \u2200 {n} x \u2192 Rot {n = n} (leaf x) (leaf x)\n fork : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 left\u2081 \u2192\n Rot right\u2080 right\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n krof : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 right\u2081 \u2192\n Rot right\u2080 left\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n\nopen import Relation.Binary\n\nRot-refl : \u2200 {n a} {A : Set a} \u2192 Reflexive (Rot {A = A} {n})\nRot-refl {x = leaf x} = leaf x\nRot-refl {x = fork left right} = fork Rot-refl Rot-refl\n\nRot-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Rot {A = A} {n})\nRot-sym (leaf x) = leaf x\nRot-sym (fork p\u2080 p\u2081) = fork (Rot-sym p\u2080) (Rot-sym p\u2081)\nRot-sym (krof p\u2080 p\u2081) = krof (Rot-sym p\u2081) (Rot-sym p\u2080)\n\nRot-trans : \u2200 {n a} {A : Set a} \u2192 Transitive (Rot {A = A} {n})\nRot-trans (leaf x) q = q\nRot-trans (fork p\u2080 p\u2081) (fork q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (fork p\u2080 p\u2081) (krof q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (krof p\u2080 p\u2081) (fork q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\nRot-trans (krof p\u2080 p\u2081) (krof q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"24ca737e8a2713939592b800f904f7abb2cd7816","subject":"Added minus-0x.","message":"Added minus-0x.\n\nIgnore-this: 184c72f3970e6a8e48a91ed79f94646c\n\ndarcs-hash:20100429154806-3bd4e-a8caa80a418cb33160bf13afca11eb6ff2a25c1f.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Function\/Arithmetic\/Properties.agda","new_file":"LTC\/Function\/Arithmetic\/Properties.agda","new_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties on total natural numbers\n------------------------------------------------------------------------------\n\nmodule LTC.Function.Arithmetic.Properties where\n\nopen import LTC.Minimal\n\nopen import LTC.Data.N\nopen import LTC.Function.Arithmetic\n\nopen import MyStdLib.Function\n\n------------------------------------------------------------------------------\n-- Closure properties\n\npred-N : {n : D} \u2192 N n \u2192 N (pred n)\npred-N zN = prf\n where\n postulate prf : N (pred zero)\n {-# ATP prove prf zN #-}\n\npred-N (sN {n} Nn) = prf\n where\n -- TODO: The postulate N (pred $ succ n) is not proved by the ATP.\n postulate prf : N (pred (succ n))\n {-# ATP prove prf #-}\n-- {-# ATP hint pred-N #-}\n\nminus-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m - n)\nminus-N {m} Nm zN = prf\n where\n postulate prf : N (m - zero)\n {-# ATP prove prf #-}\n\nminus-N zN (sN {n} Nn) = prf\n where\n postulate prf : N (zero - succ n)\n {-# ATP prove prf zN #-}\n\nminus-N (sN {m} Nm) (sN {n} Nn) = prf $ minus-N Nm Nn\n where\n postulate prf : N (m - n) \u2192 N (succ m - succ n)\n {-# ATP prove prf #-}\n\n+-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {n = n} zN Nn = prf\n where\n postulate prf : N (zero + n)\n {-# ATP prove prf #-}\n+-N {n = n} (sN {m} Nm ) Nn = prf (+-N Nm Nn)\n where\n postulate prf : N (m + n) \u2192 N (succ m + n)\n {-# ATP prove prf sN #-}\n\n*-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m * n)\n*-N {n = n} zN Nn = prf\n where\n postulate prf : N (zero * n)\n {-# ATP prove prf zN #-}\n*-N {n = n} (sN {m} Nm) Nn = prf (*-N Nm Nn)\n where\n postulate prf : N (m * n) \u2192 N (succ m * n)\n {-# ATP prove prf +-N #-}\n\n------------------------------------------------------------------------------\n\n-- Some proofs are based on the proofs in the standard library\n\n+-leftIdentity : {n : D} \u2192 N n \u2192 zero + n \u2261 n\n+-leftIdentity {n} _ = +-0x n\n\n+-rightIdentity : {n : D} \u2192 N n \u2192 n + zero \u2261 n\n+-rightIdentity zN = +-leftIdentity zN\n+-rightIdentity (sN {n} Nn) = prf $ +-rightIdentity Nn\n where\n postulate prf : n + zero \u2261 n \u2192 succ n + zero \u2261 succ n\n {-# ATP prove prf #-}\n\n+-assoc : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 (m + n) + o \u2261 m + (n + o)\n+-assoc {n = n} {o = o} zN Nn No = prf\n where\n postulate prf : zero + n + o \u2261 zero + (n + o)\n {-# ATP prove prf #-}\n+-assoc {n = n} {o = o} (sN {m} Nm) Nn No = prf $ +-assoc Nm Nn No\n where\n postulate prf : m + n + o \u2261 m + (n + o) \u2192\n succ m + n + o \u2261 succ m + (n + o)\n {-# ATP prove prf #-}\n\nx+1+y\u22611+x+y : {m n : D} \u2192 N m \u2192 N n \u2192 m + succ n \u2261 succ (m + n)\nx+1+y\u22611+x+y {n = n} zN Nn = prf\n where\n postulate prf : zero + succ n \u2261 succ (zero + n)\n {-# ATP prove prf #-}\nx+1+y\u22611+x+y {n = n} (sN {m} Nm) Nn = prf (x+1+y\u22611+x+y Nm Nn)\n where\n postulate prf : m + succ n \u2261 succ (m + n) \u2192\n succ m + succ n \u2261 succ (succ m + n)\n {-# ATP prove prf #-}\n\n+-comm : {m n : D} \u2192 N m \u2192 N n \u2192 m + n \u2261 n + m\n+-comm {n = n} zN Nn = prf\n where\n postulate prf : zero + n \u2261 n + zero\n {-# ATP prove prf +-rightIdentity #-}\n+-comm {n = n} (sN {m} Nm) Nn = prf (+-comm Nm Nn)\n where\n postulate prf : m + n \u2261 n + m \u2192 succ m + n \u2261 n + succ m\n {-# ATP prove prf x+1+y\u22611+x+y #-}\n\nminus-0x : {n : D} \u2192 N n \u2192 zero - n \u2261 zero\nminus-0x zN = minus-x0 zero\nminus-0x (sN {n} Nn) = minus-0S n\n\n*-leftZero : {n : D} \u2192 N n \u2192 zero * n \u2261 zero\n*-leftZero {n} _ = *-0x n\n\n*-rightZero : {n : D} \u2192 N n \u2192 n * zero \u2261 zero\n*-rightZero zN = *-leftZero zN\n*-rightZero (sN {n} Nn) = prf (*-rightZero Nn)\n where\n postulate prf : n * zero \u2261 zero \u2192 succ n * zero \u2261 zero\n {-# ATP prove prf #-}\n\npostulate *-leftIdentity : {n : D} \u2192 N n \u2192 succ zero * n \u2261 n\n{-# ATP prove *-leftIdentity +-rightIdentity #-}\n\nx*1+y\u2261x+xy : {m n : D} \u2192 N m \u2192 N n \u2192 m * succ n \u2261 m + m * n\nx*1+y\u2261x+xy {n = n} zN Nn = prf\n where\n postulate prf : zero * succ n \u2261 zero + zero * n\n {-# ATP prove prf #-}\nx*1+y\u2261x+xy {n = n} (sN {m} Nm) Nn = prf (x*1+y\u2261x+xy Nm Nn)\n (+-assoc Nn Nm (*-N Nm Nn))\n (+-assoc Nm Nn (*-N Nm Nn))\n where\n -- N.B. We had to feed the ATP with the instances of the associate law\n postulate prf : m * succ n \u2261 m + m * n \u2192 -- IH\n (n + m) + (m * n) \u2261 n + (m + (m * n)) \u2192 -- Associative law\n (m + n) + (m * n) \u2261 m + (n + (m * n)) \u2192 -- Associateve law\n succ m * succ n \u2261 succ m + succ m * n\n {-# ATP prove prf +-comm #-}\n\n*-comm : {m n : D} \u2192 N m \u2192 N n \u2192 m * n \u2261 n * m\n*-comm {n = n} zN Nn = prf\n where\n postulate prf : zero * n \u2261 n * zero\n {-# ATP prove prf *-rightZero #-}\n*-comm {n = n} (sN {m} Nm) Nn = prf (*-comm Nm Nn)\n where\n postulate prf : m * n \u2261 n * m \u2192\n succ m * n \u2261 n * succ m\n {-# ATP prove prf x*1+y\u2261x+xy #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties on total natural numbers\n------------------------------------------------------------------------------\n\nmodule LTC.Function.Arithmetic.Properties where\n\nopen import LTC.Minimal\n\nopen import LTC.Data.N\nopen import LTC.Function.Arithmetic\n\nopen import MyStdLib.Function\n\n------------------------------------------------------------------------------\n-- Closure properties\n\npred-N : {n : D} \u2192 N n \u2192 N (pred n)\npred-N zN = prf\n where\n postulate prf : N (pred zero)\n {-# ATP prove prf zN #-}\n\npred-N (sN {n} Nn) = prf\n where\n -- TODO: The postulate N (pred $ succ n) is not proved by the ATP.\n postulate prf : N (pred (succ n))\n {-# ATP prove prf #-}\n-- {-# ATP hint pred-N #-}\n\nminus-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m - n)\nminus-N {m} Nm zN = prf\n where\n postulate prf : N (m - zero)\n {-# ATP prove prf #-}\n\nminus-N zN (sN {n} Nn) = prf\n where\n postulate prf : N (zero - succ n)\n {-# ATP prove prf zN #-}\n\nminus-N (sN {m} Nm) (sN {n} Nn) = prf $ minus-N Nm Nn\n where\n postulate prf : N (m - n) \u2192 N (succ m - succ n)\n {-# ATP prove prf #-}\n\n------------------------------------------------------------------------------\n\n+-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {n = n} zN Nn = prf\n where\n postulate prf : N (zero + n)\n {-# ATP prove prf #-}\n+-N {n = n} (sN {m} Nm ) Nn = prf (+-N Nm Nn)\n where\n postulate prf : N (m + n) \u2192 N (succ m + n)\n {-# ATP prove prf sN #-}\n\n*-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m * n)\n*-N {n = n} zN Nn = prf\n where\n postulate prf : N (zero * n)\n {-# ATP prove prf zN #-}\n*-N {n = n} (sN {m} Nm) Nn = prf (*-N Nm Nn)\n where\n postulate prf : N (m * n) \u2192 N (succ m * n)\n {-# ATP prove prf +-N #-}\n\n------------------------------------------------------------------------------\n\n-- The proofs are based on the proofs in the standard library\n\n+-leftIdentity : {n : D} \u2192 N n \u2192 zero + n \u2261 n\n+-leftIdentity {n} _ = +-0x n\n\n+-rightIdentity : {n : D} \u2192 N n \u2192 n + zero \u2261 n\n+-rightIdentity zN = +-leftIdentity zN\n+-rightIdentity (sN {n} Nn) = prf $ +-rightIdentity Nn\n where\n postulate prf : n + zero \u2261 n \u2192 succ n + zero \u2261 succ n\n {-# ATP prove prf #-}\n\n+-assoc : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 (m + n) + o \u2261 m + (n + o)\n+-assoc {n = n} {o = o} zN Nn No = prf\n where\n postulate prf : zero + n + o \u2261 zero + (n + o)\n {-# ATP prove prf #-}\n+-assoc {n = n} {o = o} (sN {m} Nm) Nn No = prf $ +-assoc Nm Nn No\n where\n postulate prf : m + n + o \u2261 m + (n + o) \u2192\n succ m + n + o \u2261 succ m + (n + o)\n {-# ATP prove prf #-}\n\nx+1+y\u22611+x+y : {m n : D} \u2192 N m \u2192 N n \u2192 m + succ n \u2261 succ (m + n)\nx+1+y\u22611+x+y {n = n} zN Nn = prf\n where\n postulate prf : zero + succ n \u2261 succ (zero + n)\n {-# ATP prove prf #-}\nx+1+y\u22611+x+y {n = n} (sN {m} Nm) Nn = prf (x+1+y\u22611+x+y Nm Nn)\n where\n postulate prf : m + succ n \u2261 succ (m + n) \u2192\n succ m + succ n \u2261 succ (succ m + n)\n {-# ATP prove prf #-}\n\n+-comm : {m n : D} \u2192 N m \u2192 N n \u2192 m + n \u2261 n + m\n+-comm {n = n} zN Nn = prf\n where\n postulate prf : zero + n \u2261 n + zero\n {-# ATP prove prf +-rightIdentity #-}\n+-comm {n = n} (sN {m} Nm) Nn = prf (+-comm Nm Nn)\n where\n postulate prf : m + n \u2261 n + m \u2192 succ m + n \u2261 n + succ m\n {-# ATP prove prf x+1+y\u22611+x+y #-}\n\n*-leftZero : {n : D} \u2192 N n \u2192 zero * n \u2261 zero\n*-leftZero {n} _ = *-0x n\n\n*-rightZero : {n : D} \u2192 N n \u2192 n * zero \u2261 zero\n*-rightZero zN = *-leftZero zN\n*-rightZero (sN {n} Nn) = prf (*-rightZero Nn)\n where\n postulate prf : n * zero \u2261 zero \u2192 succ n * zero \u2261 zero\n {-# ATP prove prf #-}\n\npostulate *-leftIdentity : {n : D} \u2192 N n \u2192 succ zero * n \u2261 n\n{-# ATP prove *-leftIdentity +-rightIdentity #-}\n\nx*1+y\u2261x+xy : {m n : D} \u2192 N m \u2192 N n \u2192 m * succ n \u2261 m + m * n\nx*1+y\u2261x+xy {n = n} zN Nn = prf\n where\n postulate prf : zero * succ n \u2261 zero + zero * n\n {-# ATP prove prf #-}\nx*1+y\u2261x+xy {n = n} (sN {m} Nm) Nn = prf (x*1+y\u2261x+xy Nm Nn)\n (+-assoc Nn Nm (*-N Nm Nn))\n (+-assoc Nm Nn (*-N Nm Nn))\n where\n -- N.B. We had to feed the ATP with the instances of the associate law\n postulate prf : m * succ n \u2261 m + m * n \u2192 -- IH\n (n + m) + (m * n) \u2261 n + (m + (m * n)) \u2192 -- Associative law\n (m + n) + (m * n) \u2261 m + (n + (m * n)) \u2192 -- Associateve law\n succ m * succ n \u2261 succ m + succ m * n\n {-# ATP prove prf +-comm #-}\n\n*-comm : {m n : D} \u2192 N m \u2192 N n \u2192 m * n \u2261 n * m\n*-comm {n = n} zN Nn = prf\n where\n postulate prf : zero * n \u2261 n * zero\n {-# ATP prove prf *-rightZero #-}\n*-comm {n = n} (sN {m} Nm) Nn = prf (*-comm Nm Nn)\n where\n postulate prf : m * n \u2261 n * m \u2192\n succ m * n \u2261 n * succ m\n {-# ATP prove prf x*1+y\u2261x+xy #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"e1d9192fe2f0abe2c2a369db5b7083d63331c9a5","subject":"Describe the purpose of Changes.agda (see #28).","message":"Describe the purpose of Changes.agda (see #28).\n\nOld-commit-hash: cbd11ef58e011e2b5d8af09524e6a0c07ee91098\n","repos":"inc-lc\/ilc-agda","old_file":"Changes.agda","new_file":"Changes.agda","new_contents":"module Changes where\n\n-- CHANGES\n--\n-- This module defines the representation of changes, the\n-- available operations on changes, and properties of these\n-- operations.\n\nopen import meaning\nopen import Model\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\u0394-Type (bool) = bool -- true means negate, false means nil change\n\nderive : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\napply : \u2200 {\u03c4} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\ndiff : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ndiff {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 diff (f\u2081 (apply dv v)) (f\u2082 v)\ndiff {bool} b c = b xor c\n\nderive {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 diff (f (apply dv v)) (f v)\nderive {bool} b = false\n\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} df f = \u03bb v \u2192 apply (df v (derive v)) (f v)\napply {bool} b c = b xor c\n\ncompose : \u2200 {\u03c4} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} df\u2081 df\u2082 = \u03bb v dv \u2192 compose (df\u2081 v dv) (df\u2082 v dv)\ncompose {bool} b c = b xor c\n\n-- CONGRUENCE rules for change operations\n\nopen import Relation.Binary.PropositionalEquality\n\n\u2261-diff : \u2200 {\u03c4 : Type} {v\u2081 v\u2082 v\u2083 v\u2084 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 diff v\u2081 v\u2083 \u2261 diff v\u2082 v\u2084\n\u2261-diff = \u2261-cong\u2082 diff\n\n\u2261-apply : \u2200 {\u03c4 : Type} {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} {v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 v\u2081 \u2261 v\u2082 \u2192 apply dv\u2081 v\u2081 \u2261 apply dv\u2082 v\u2082\n\u2261-apply = \u2261-cong\u2082 apply\n\n-- PROPERTIES of changes\n\ndiff-derive : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192\n diff v v \u2261 derive v\ndiff-derive {\u03c4\u2081 \u21d2 \u03c4\u2082} v = \u2261-refl\n\ndiff-derive {bool} b = a-xor-a-false b\n\n-- XXX: as given, this is false!\npostulate\n diff-apply : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n diff (apply dv v) v \u2261 dv\n\n{-\ndiff-apply {\u03c4\u2081 \u21d2 \u03c4\u2082} df f = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n {!!}))\ndiff-apply {bool} true true = bool\ndiff-apply {bool} true false = bool\ndiff-apply {bool} false true = bool\ndiff-apply {bool} false false = bool\n-}\n\napply-diff : \u2200 {\u03c4} (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192\n apply (diff v\u2082 v\u2081) v\u2081 \u2261 v\u2082\n\napply-derive : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192\n apply (derive v) v \u2261 v\n\napply-diff {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = ext (\u03bb v \u2192\n begin\n apply (diff f\u2082 f\u2081) f\u2081 v\n \u2261\u27e8 \u2261-refl \u27e9\n apply (diff (f\u2082 (apply (derive v) v)) (f\u2081 v)) (f\u2081 v)\n \u2261\u27e8 \u2261-apply (\u2261-diff (\u2261-cong f\u2082 (apply-derive v)) \u2261-refl) \u2261-refl \u27e9\n apply (diff (f\u2082 v) (f\u2081 v)) (f\u2081 v)\n \u2261\u27e8 apply-diff (f\u2081 v) (f\u2082 v) \u27e9\n f\u2082 v\n \u220e) where open \u2261-Reasoning\napply-diff {bool} a b = xor-cancellative a b\n\napply-derive {\u03c4\u2081 \u21d2 \u03c4\u2082} f = ext (\u03bb v \u2192\n begin\n apply (derive f) f v\n \u2261\u27e8 \u2261-refl \u27e9\n apply (diff (f (apply (derive v) v)) (f v)) (f v)\n \u2261\u27e8 \u2261-cong (\u03bb x \u2192 apply (diff (f x) (f v)) (f v)) (apply-derive v) \u27e9\n apply (diff (f v) (f v)) (f v)\n \u2261\u27e8 apply-diff (f v) (f v)\u27e9\n f v\n \u220e) where open \u2261-Reasoning\napply-derive {bool} a = a-xor-false-a a\n\napply-compose : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) (dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n apply (compose dv\u2081 dv\u2082) v \u2261 apply dv\u2081 (apply dv\u2082 v)\napply-compose {\u03c4\u2081 \u21d2 \u03c4\u2082} f df\u2081 df\u2082 = ext (\u03bb v \u2192\n apply-compose (f v) (df\u2081 v (derive v)) (df\u2082 v (derive v)))\napply-compose {bool} a b c = xor-associative a b c\n\ncompose-assoc : \u2200 {\u03c4} (dv\u2081 dv\u2082 dv\u2083 : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n compose dv\u2081 (compose dv\u2082 dv\u2083) \u2261 compose (compose dv\u2081 dv\u2082) dv\u2083\ncompose-assoc {\u03c4\u2081 \u21d2 \u03c4\u2082} df\u2081 df\u2082 df\u2083 = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n compose-assoc (df\u2081 v dv) (df\u2082 v dv) (df\u2083 v dv)))\ncompose-assoc {bool} a b c = \u2261-sym (xor-associative c a b)\n","old_contents":"module Changes where\n\nopen import meaning\nopen import Model\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\u0394-Type (bool) = bool -- true means negate, false means nil change\n\nderive : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\napply : \u2200 {\u03c4} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\ndiff : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ndiff {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 diff (f\u2081 (apply dv v)) (f\u2082 v)\ndiff {bool} b c = b xor c\n\nderive {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 diff (f (apply dv v)) (f v)\nderive {bool} b = false\n\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} df f = \u03bb v \u2192 apply (df v (derive v)) (f v)\napply {bool} b c = b xor c\n\ncompose : \u2200 {\u03c4} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} df\u2081 df\u2082 = \u03bb v dv \u2192 compose (df\u2081 v dv) (df\u2082 v dv)\ncompose {bool} b c = b xor c\n\n-- CONGRUENCE rules for change operations\n\nopen import Relation.Binary.PropositionalEquality\n\n\u2261-diff : \u2200 {\u03c4 : Type} {v\u2081 v\u2082 v\u2083 v\u2084 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 diff v\u2081 v\u2083 \u2261 diff v\u2082 v\u2084\n\u2261-diff = \u2261-cong\u2082 diff\n\n\u2261-apply : \u2200 {\u03c4 : Type} {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} {v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 v\u2081 \u2261 v\u2082 \u2192 apply dv\u2081 v\u2081 \u2261 apply dv\u2082 v\u2082\n\u2261-apply = \u2261-cong\u2082 apply\n\n-- PROPERTIES of changes\n\ndiff-derive : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192\n diff v v \u2261 derive v\ndiff-derive {\u03c4\u2081 \u21d2 \u03c4\u2082} v = \u2261-refl\n\ndiff-derive {bool} b = a-xor-a-false b\n\n-- XXX: as given, this is false!\npostulate\n diff-apply : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n diff (apply dv v) v \u2261 dv\n\n{-\ndiff-apply {\u03c4\u2081 \u21d2 \u03c4\u2082} df f = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n {!!}))\ndiff-apply {bool} true true = bool\ndiff-apply {bool} true false = bool\ndiff-apply {bool} false true = bool\ndiff-apply {bool} false false = bool\n-}\n\napply-diff : \u2200 {\u03c4} (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192\n apply (diff v\u2082 v\u2081) v\u2081 \u2261 v\u2082\n\napply-derive : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192\n apply (derive v) v \u2261 v\n\napply-diff {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = ext (\u03bb v \u2192\n begin\n apply (diff f\u2082 f\u2081) f\u2081 v\n \u2261\u27e8 \u2261-refl \u27e9\n apply (diff (f\u2082 (apply (derive v) v)) (f\u2081 v)) (f\u2081 v)\n \u2261\u27e8 \u2261-apply (\u2261-diff (\u2261-cong f\u2082 (apply-derive v)) \u2261-refl) \u2261-refl \u27e9\n apply (diff (f\u2082 v) (f\u2081 v)) (f\u2081 v)\n \u2261\u27e8 apply-diff (f\u2081 v) (f\u2082 v) \u27e9\n f\u2082 v\n \u220e) where open \u2261-Reasoning\napply-diff {bool} a b = xor-cancellative a b\n\napply-derive {\u03c4\u2081 \u21d2 \u03c4\u2082} f = ext (\u03bb v \u2192\n begin\n apply (derive f) f v\n \u2261\u27e8 \u2261-refl \u27e9\n apply (diff (f (apply (derive v) v)) (f v)) (f v)\n \u2261\u27e8 \u2261-cong (\u03bb x \u2192 apply (diff (f x) (f v)) (f v)) (apply-derive v) \u27e9\n apply (diff (f v) (f v)) (f v)\n \u2261\u27e8 apply-diff (f v) (f v)\u27e9\n f v\n \u220e) where open \u2261-Reasoning\napply-derive {bool} a = a-xor-false-a a\n\napply-compose : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) (dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n apply (compose dv\u2081 dv\u2082) v \u2261 apply dv\u2081 (apply dv\u2082 v)\napply-compose {\u03c4\u2081 \u21d2 \u03c4\u2082} f df\u2081 df\u2082 = ext (\u03bb v \u2192\n apply-compose (f v) (df\u2081 v (derive v)) (df\u2082 v (derive v)))\napply-compose {bool} a b c = xor-associative a b c\n\ncompose-assoc : \u2200 {\u03c4} (dv\u2081 dv\u2082 dv\u2083 : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n compose dv\u2081 (compose dv\u2082 dv\u2083) \u2261 compose (compose dv\u2081 dv\u2082) dv\u2083\ncompose-assoc {\u03c4\u2081 \u21d2 \u03c4\u2082} df\u2081 df\u2082 df\u2083 = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n compose-assoc (df\u2081 v dv) (df\u2082 v dv) (df\u2083 v dv)))\ncompose-assoc {bool} a b c = \u2261-sym (xor-associative c a b)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"2374278020e2b6ffe37d215e7ea4fffd7a448576","subject":"extended natural semantics proof with ifthenelse but failed to finish it","message":"extended natural semantics proof with ifthenelse but failed to finish it\n\nOld-commit-hash: 542159c766c7879ae32fd9c8be27fc977bf99b16\n","repos":"inc-lc\/ilc-agda","old_file":"Natural\/Soundness.agda","new_file":"Natural\/Soundness.agda","new_contents":"module Natural.Soundness where\n\n-- SOUNDNESS of NATURAL SEMANTICS\n--\n-- This module proves consistency of the natural semantics with\n-- respect to the denotational semantics.\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Closures\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\nopen import Denotational.Closures\n\nopen import Natural.Lookup\nopen import Natural.Evaluation\n\n-- Syntactic lookup is consistent with semantic lookup.\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = \u2261-refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n-- Syntactic evaluation is consistent with semantic evaluation.\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = \u2261-refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) =\n \u2261-trans\n (\u2261-cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082))\n (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n\u2193-sound e-true = \u2261-refl \n\u2193-sound e-false = \u2261-refl \n-- Agda does not recognize automagically that\n-- \u27e6if c t e\u27e7 \u2261 \u27e6t\u27e7, which I would need to finish \n-- the proof. Maybe I need some \"with\" magic in\n-- the pattern match here? Help!\n\u2193-sound (if-true \u2193\u2081 \u2193\u2082) = \u2261-trans {!!} (\u2193-sound \u2193\u2082)\n\u2193-sound (if-false \u2193\u2081 \u2193\u2082) = {!!}\n","old_contents":"module Natural.Soundness where\n\n-- SOUNDNESS of NATURAL SEMANTICS\n--\n-- This module proves consistency of the natural semantics with\n-- respect to the denotational semantics.\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Closures\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\nopen import Denotational.Closures\n\nopen import Natural.Lookup\nopen import Natural.Evaluation\n\n-- Syntactic lookup is consistent with semantic lookup.\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = \u2261-refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n-- Syntactic evaluation is consistent with semantic evaluation.\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = \u2261-refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) =\n \u2261-trans\n (\u2261-cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082))\n (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n\u2193-sound e-true = \u2261-refl \n\u2193-sound e-false = \u2261-refl \n\u2193-sound (if-true \u2193\u2081 \u2193\u2082) \u2261-trans {!!} (\u2193-sound \u2193\u2082)\n\u2193-sound (if-false \u2193\u2081 \u2193\u2082) = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"8ca23b07a65ea0c63d74b53511d03868a5e1ccbf","subject":"better circuits","message":"better circuits\n","repos":"crypto-agda\/crypto-agda","old_file":"circuit.agda","new_file":"circuit.agda","new_contents":"module circuit where\n\nopen import Function\nopen import Data.Nat.NP hiding (_\u225f_; compare)\nopen import Data.Bits\nopen import Data.Bits.Bits2\nopen import Data.Bool hiding (_\u225f_)\nopen import Data.Product hiding (swap; map)\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc; inject+; raise; #_)\nopen import Data.List using (List; []; _\u2237_)\nopen import Data.Vec using (Vec; []; _\u2237_; foldr; _[_]\u2254_; lookup; _++_; splitAt; tabulate; allFin; concat) renaming (map to vmap)\nopen import Data.Vec.Properties\nopen import Relation.Nullary.Decidable hiding (map)\nopen import Relation.Binary.PropositionalEquality\n\nCircuitType : Set\u2081\nCircuitType = (i o : \u2115) \u2192 Set\n\nRunCircuit : CircuitType \u2192 Set\nRunCircuit C = \u2200 {i o} \u2192 C i o \u2192 Bits i \u2192 Bits o\n\nmodule Rewire where\n RewireFun : CircuitType\n RewireFun i o = Fin o \u2192 Fin i\n\n RewireTbl : CircuitType\n RewireTbl i o = Vec (Fin i) o\n\n rewire : \u2200 {a i o} {A : Set a} \u2192 (Fin o \u2192 Fin i) \u2192 Vec A i \u2192 Vec A o\n rewire f v = tabulate (flip lookup v \u2218 f)\n\n rewireTbl : \u2200 {a i o} {A : Set a} \u2192 RewireTbl i o \u2192 Vec A i \u2192 Vec A o\n rewireTbl tbl v = vmap (flip lookup v) tbl\n\n runRewireFun : RunCircuit RewireFun\n runRewireFun = rewire\n\n runRewireTbl : RunCircuit RewireTbl\n runRewireTbl = rewireTbl\n\nopen Rewire using (RewireTbl; RewireFun)\n\nrecord RewiringBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n\n infixr 1 _>>>_\n infixr 3 _***_\n\n field\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n\n rewireWithTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 C i o\n rewireWithTbl = rewire \u2218 flip lookup\n\n idCDefault : \u2200 {i} \u2192 C i i\n idCDefault = rewire id\n\n field\n idC : \u2200 {i} \u2192 C i i\n\n field\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) is \u2192 is =[ rewire r ]= Rewire.rewire r is\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idC ]= bs\n\n idCDefault-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idCDefault ]= bs\n idCDefault-spec bs\n = subst (\u03bb bs' \u2192 bs =[ idCDefault ]= bs') (tabulate\u2218lookup bs) (rewire-spec id bs)\n\n sink : \u2200 i \u2192 C i 0\n sink _ = rewire (\u03bb())\n-- sink-spec : bs =[ sink i ]= []\n\n dup\u2081 : \u2200 o \u2192 C 1 o\n dup\u2081 _ = rewire (const zero)\n-- dup\u2081-spec : (b \u2237 []) =[ dup o ]= replicate o\n\n dup\u2081\u00b2 : C 1 2\n dup\u2081\u00b2 = dup\u2081 2\n-- dup\u2081\u00b2-spec : (b \u2237 []) =[ dup\u2081\u00b2 ]= (b \u2237 b \u2237 [])\n\n vcat : \u2200 {i o n} \u2192 Vec (C i o) n \u2192 C (n * i) (n * o)\n vcat [] = idC\n vcat (x \u2237 xs) = x *** vcat xs\n\n coerce : \u2200 {i o} \u2192 i \u2261 o \u2192 C i o\n coerce refl = idC\n\n{-\n coerce-spec : \u2200 {i o} {i\u2261o : i \u2261 o} {is} \u2192\n is =[ coerce i\u2261o ]= subst Bits i\u2261o is\n coerce-spec = {!!}\n-}\n\n {-\n Sends inputs 0,1,2,...,n to outputs 0,1,2,...,n,0,1,2,...,n,0,1,2,...,n,...\n -}\n dup\u207f : \u2200 {i} k \u2192 C i (k * i)\n dup\u207f {i} k = rewireWithTbl (concat (replicate {n = k} (allFin i)))\n\n{-\n dup\u207f-spec : \u2200 {i} {is : Bits i} k \u2192 is =[ dup\u207f k ]= concat (replicate {n = k} is)\n dup\u207f-spec {i} {is} k = {!rewireWithTbl-spec (concat (replicate {n = k} (allFin _))) ?!}\n-}\n\n {-\n Sends inputs 0,1,2,...,n to outputs 0,0,0,...,1,1,1,...,2,2,2,...,n,n,n,...\n -}\n dup\u207f\u2032 : \u2200 {i} k \u2192 C i (i * k)\n dup\u207f\u2032 {i} k = rewireWithTbl (concat (vmap replicate (allFin i)))\n{-\n dup\u207f\u2032 {i} k = coerce (proj\u2082 \u2115\u00b0.*-identity i) >>>\n (vcat {n = i} (replicate (dup\u2081 _)))\n-}\n\n dup\u00b2 : \u2200 {i} \u2192 C i (i + i)\n dup\u00b2 {i} = dup\u207f 2 >>> coerce (cong (_+_ i) (sym (\u2115\u00b0.+-comm 0 i)))\n\n{-\n dup\u00b2-spec : \u2200 {n} {is : Bits n} \u2192 is =[ dup\u00b2 ]= (is ++ is)\n dup\u00b2-spec = coerce-spec (rewireWithTbl-spec {!!} {!!})\n-}\n\n _&&&_ : \u2200 {i o\u2080 o\u2081} \u2192 C i o\u2080 \u2192 C i o\u2081 \u2192 C i (o\u2080 + o\u2081)\n c\u2080 &&& c\u2081 = dup\u00b2 >>> c\u2080 *** c\u2081\n\n{-\n _&&&-spec_ : \u2200 {i o\u2080 o\u2081} {c\u2080 : C i o\u2080} {c\u2081 : C i o\u2081} {is os\u2080 os\u2081}\n \u2192 is =[ c\u2080 ]= os\u2080 \u2192 is =[ c\u2081 ]= os\u2081 \u2192 is =[ c\u2080 &&& c\u2081 ]= (os\u2080 ++ os\u2081)\n pf\u2080 &&&-spec pf\u2081 = dup\u00b2-spec >>>-spec (pf\u2080 ***-spec pf\u2081)\n-}\n\n ext-before : \u2200 {k i o} \u2192 C i o \u2192 C (k + i) (k + o)\n ext-before {k} c = idC {k} *** c\n\n ext-after : \u2200 {k i o} \u2192 C i o \u2192 C (i + k) (o + k)\n ext-after c = c *** idC\n\n commC : \u2200 m n \u2192 C (m + n) (n + m)\n commC m n = rewireWithTbl (vmap (raise m) (allFin n) ++ vmap (inject+ n) (allFin m))\n\n dropC : \u2200 {i} k \u2192 C (k + i) i\n dropC k = sink k *** idC\n\n takeC : \u2200 {i} k \u2192 C (k + i) k\n takeC {i} k = commC k _ >>> dropC i\n\n swap : \u2200 {i} (x y : Fin i) \u2192 C i i\n -- swap x y = arr (\u03bb xs \u2192 (xs [ x ]\u2254 (lookup y xs)) [ y ]\u2254 (lookup x xs))\n swap x y = rewire (Fin.swap x y)\n\n rev : \u2200 {i} \u2192 C i i\n rev = rewire Fin.reverse\n\n swap\u2082 : C 2 2\n swap\u2082 = rev\n -- swap\u2082 = swap (# 0) (# 1)\n\n Perm : \u2115 \u2192 Set\n Perm n = List (Fin n \u00d7 Fin n)\n\n perm : \u2200 {i} \u2192 Perm i \u2192 C i i\n perm [] = idC\n perm ((x , y) \u2237 \u03c0) = swap x y >>> perm \u03c0\n\n headC : \u2200 {i} \u2192 C (1 + i) 1\n headC = takeC 1\n\n tailC : \u2200 {i} \u2192 C (1 + i) i\n tailC = dropC 1\n\nrecord CircuitBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n field\n isRewiringBuilder : RewiringBuilder C\n arr : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n leafC : \u2200 {o} \u2192 Bits o \u2192 C 0 o\n forkC : \u2200 {i o} (c\u2080 c\u2081 : C i o) \u2192 C (1 + i) o\n\n open RewiringBuilder isRewiringBuilder\n\n bit : Bit \u2192 C 0 1\n bit b = leafC (b \u2237 [])\n\n 0\u02b7 : C 0 1\n 0\u02b7 = bit 0b\n\n 0\u02b7\u207f : \u2200 {o} \u2192 C 0 o\n 0\u02b7\u207f = leafC 0\u207f\n\n 1\u02b7 : C 0 1\n 1\u02b7 = bit 1b\n\n 1\u02b7\u207f : \u2200 {o} \u2192 C 0 o\n 1\u02b7\u207f = leafC 1\u207f\n\n padL : \u2200 {i} k \u2192 C i (k + i)\n padL k = 0\u02b7\u207f {k} *** idC\n\n padR : \u2200 {i} k \u2192 C i (i + k)\n padR k = padL k >>> commC k _\n\n arr' : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n arr' {zero} f = leafC (f [])\n arr' {suc i} f = forkC (arr' {i} (f \u2218 0\u2237_)) (arr' (f \u2218 1\u2237_))\n\n unOp : (Bit \u2192 Bit) \u2192 C 1 1\n unOp op = arr (\u03bb { (x \u2237 []) \u2192 (op x) \u2237 [] })\n\n notC : C 1 1\n notC = unOp not\n\n binOp : (Bit \u2192 Bit \u2192 Bit) \u2192 C 2 1\n binOp op = arr (\u03bb { (x \u2237 y \u2237 []) \u2192 (op x y) \u2237 [] })\n\n terOp : (Bit \u2192 Bit \u2192 Bit \u2192 Bit) \u2192 C 3 1\n terOp op = arr (\u03bb { (x \u2237 y \u2237 z \u2237 []) \u2192 (op x y z) \u2237 [] })\n\n xorC : C 2 1\n xorC = binOp _xor_\n\n eqC : C 2 1\n eqC = binOp _==\u1d47_\n\n orC : C 2 1\n orC = binOp _\u2228_\n\n andC : C 2 1\n andC = binOp _\u2227_\n\n norC : C 2 1\n norC = binOp (\u03bb x y \u2192 not (x \u2228 y))\n\n nandC : C 2 1\n nandC = binOp (\u03bb x y \u2192 not (x \u2227 y))\n\n if\u27e8head=0\u27e9then_else_ : \u2200 {i o} (c\u2080 c\u2081 : C i o) \u2192 C (1 + i) o\n if\u27e8head=0\u27e9then_else_ = forkC\n\n -- Base addition with carry:\n -- * Any input can be used as the carry\n -- * First output is the carry\n --\n -- This can also be seen as the addition of\n -- three bits which result is between 0 and 3\n -- and thus fits in two bit word in binary\n -- representation.\n --\n -- Alternatively you can see it as counting the\n -- number of 1s in a three bits vector.\n add\u2082 : C 3 2\n add\u2082 = carry &&& result\n where carry : C 3 1\n carry = terOp (\u03bb x y z \u2192 x xor (y \u2227 z))\n result : C 3 1\n result = terOp (\u03bb x y z \u2192 x xor (y xor z))\n\n open RewiringBuilder isRewiringBuilder public\n\n_\u2192\u1d47_ : CircuitType\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n_\u2192\u1da0_ : CircuitType\ni \u2192\u1da0 o = Fin o \u2192 Fin i\n\nfinFunRewiringBuilder : RewiringBuilder _\u2192\u1da0_\nfinFunRewiringBuilder = mk id _\u2218\u2032_ _***_ id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1da0_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = Rewire.rewire f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec = tabulate\u2218lookup\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n _***_ {o\u2080 = o\u2080} f g x with Fin.cmp o\u2080 _ x\n _***_ f _ ._ | Fin.bound x = inject+ _ (f x)\n _***_ {i\u2080} _ g ._ | Fin.free x = raise i\u2080 (g x)\n\ntblRewiringBuilder : RewiringBuilder RewireTbl\ntblRewiringBuilder = mk tabulate _>>>_ _***_ (allFin _) _=[_]=_ rewire-spec idC-spec\n where\n open Rewire\n C = RewireTbl\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tbl ]= output = Rewire.rewireTbl tbl input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ tabulate r ]= Rewire.rewire r bs\n rewire-spec r bs = sym (tabulate-\u2218 (flip lookup bs) r)\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ allFin _ ]= bs\n -- idC-spec = map-lookup-allFin\n idC-spec bs rewrite rewire-spec id bs = tabulate\u2218lookup bs\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n c\u2080 >>> c\u2081 = rewireTbl c\u2081 c\u2080\n -- c\u2080 >>> c\u2081 = tabulate (flip lookup c\u2080 \u2218 flip lookup c\u2081)\n -- c\u2080 >>> c\u2081 = vmap (flip lookup c\u2080) c\u2081\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n _***_ {i\u2080} c\u2080 c\u2081 = vmap (inject+ _) c\u2080 ++ vmap (raise i\u2080) c\u2081\n\nbitsFunRewiringBuilder : RewiringBuilder _\u2192\u1d47_\nbitsFunRewiringBuilder = mk Rewire.rewire _>>>_ _***_ id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1d47_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ Rewire.rewire r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec bs = refl\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n f >>> g = g \u2218 f\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n (f *** g) xs with splitAt _ xs\n ... | ys , zs , _ = f ys ++ g zs\n\nbitsFunCircuitBuilder : CircuitBuilder _\u2192\u1d47_\nbitsFunCircuitBuilder = mk bitsFunRewiringBuilder id (\u03bb { bs [] \u2192 bs }) (\u03bb { f g (b \u2237 bs) \u2192 (if b then f else g) bs })\n\nopen import bintree\nopen import flipbased-tree\n\nTreeBits : CircuitType\nTreeBits i o = Tree (Bits o) i\n\nmodule moretree where\n _>>>_ : \u2200 {m n a} {A : Set a} \u2192 Tree (Bits m) n \u2192 Tree A m \u2192 Tree A n\n f >>> g = map (flip bintree.lookup g) f\n\ntreeBitsRewiringBuilder : RewiringBuilder TreeBits\ntreeBitsRewiringBuilder = mk rewire moretree._>>>_ _***_ (rewire id) _=[_]=_ rewire-spec idC-spec\n where\n C = TreeBits\n\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n rewire f = fromFun (Rewire.rewire f)\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tree ]= output = toFun tree input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ rewire r ]= Rewire.rewire r bs\n rewire-spec r = toFun\u2218fromFun (tabulate \u2218 flip (Data.Vec.lookup \u2218 r))\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ rewire id ]= bs\n idC-spec bs rewrite toFun\u2218fromFun (tabulate \u2218 flip Data.Vec.lookup) bs | tabulate\u2218lookup bs = refl\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n (f *** g) = f >>= \u03bb xs \u2192 map (_++_ xs) g\n\ntreeBitsCircuitBuilder : CircuitBuilder TreeBits\ntreeBitsCircuitBuilder = mk treeBitsRewiringBuilder arr leaf fork\n where\n C = TreeBits\n\n arr : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n arr = fromFun\n\nRewiringTree : CircuitType\nRewiringTree i o = Tree (Fin i) o\n\nmodule RewiringWith2^Outputs where\n C_\u27e82^_\u27e9 = RewiringTree\n\n rewire : \u2200 {i o} \u2192 RewireFun i (2 ^ o) \u2192 C i \u27e82^ o \u27e9\n rewire f = fromFun (f \u2218 toFin)\n\n lookupFin : \u2200 {i o} \u2192 C i \u27e82^ o \u27e9 \u2192 Fin (2 ^ o) \u2192 Fin i\n lookupFin c x = bintree.lookup (fromFin x) c\n\n _>>>_ : \u2200 {i m o} \u2192 C i \u27e82^ m \u27e9 \u2192 C (2 ^ m) \u27e82^ o \u27e9 \u2192 C i \u27e82^ o \u27e9\n f >>> g = rewire (lookupFin f \u2218 lookupFin g)\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 \u27e82^ o\u2080 \u27e9 \u2192 C i\u2081 \u27e82^ o\u2081 \u27e9 \u2192 C (i\u2080 + i\u2081) \u27e82^ (o\u2080 + o\u2081) \u27e9\n f *** g = f >>= \u03bb x \u2192 map (Fin._+\u2032_ x) g\n\nmodule Test where\n open import Data.Bits.Bits4\n\n tbl : RewireTbl 4 4\n tbl = # 1 \u2237 # 0 \u2237 # 2 \u2237 # 2 \u2237 []\n\n fun : RewireFun 4 4\n fun zero = # 1\n fun (suc zero) = # 0\n fun (suc (suc zero)) = # 2\n fun (suc (suc (suc x))) = # 2\n\n-- swap x y \u2248 swap y x\n-- reverse \u2218 reverse \u2248 id\n\n abs : \u2200 {C} \u2192 RewiringBuilder C \u2192 C 4 4\n abs builder = swap\u2082 *** dup\u2081\u00b2 *** sink 1\n where open RewiringBuilder builder\n\n tinytree : Tree (Fin 4) 2\n tinytree = fork (fork (leaf (# 1)) (leaf (# 0))) (fork (leaf (# 2)) (leaf (# 2)))\n\n bigtree : Tree (Bits 4) 4\n bigtree = fork (fork (fork (same 0000b) (same 0011b)) (fork (same 1000b) (same 1011b)))\n (fork (fork (same 0100b) (same 0111b)) (fork (same 1100b) (same 1111b)))\n where same : \u2200 {n} {A : Set} \u2192 A \u2192 Tree A (suc n)\n same x = fork (leaf x) (leaf x)\n\n test\u2081 : tbl \u2261 tabulate fun\n test\u2081 = refl\n\n test\u2082 : tbl \u2261 abs tblRewiringBuilder\n test\u2082 = refl\n\n test\u2083 : tabulate fun \u2261 tabulate (abs finFunRewiringBuilder)\n test\u2083 = refl\n\n test\u2084 : bigtree \u2261 abs treeBitsRewiringBuilder\n test\u2084 = refl\n\n open RewiringWith2^Outputs\n test\u2085 : tabulate (lookupFin tinytree) \u2261 tbl\n test\u2085 = refl\n","old_contents":"module circuit where\n\nopen import Function\nopen import Data.Nat.NP hiding (_\u225f_; compare)\nopen import Data.Bits\nopen import Data.Bool hiding (_\u225f_)\nopen import Data.Product hiding (swap; map)\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc; inject+; raise; #_)\nopen import Data.List using (List; []; _\u2237_)\nopen import Data.Vec using (Vec; []; _\u2237_; foldr; _[_]\u2254_; lookup; _++_; splitAt; tabulate; allFin) renaming (map to vmap)\nopen import Data.Vec.Properties\nopen import Relation.Nullary.Decidable hiding (map)\nopen import Relation.Binary.PropositionalEquality\n\nCircuitType : Set\u2081\nCircuitType = (i o : \u2115) \u2192 Set\n\nRunCircuit : CircuitType \u2192 Set\nRunCircuit C = \u2200 {i o} \u2192 C i o \u2192 Bits i \u2192 Bits o\n\nmodule Rewire where\n RewireFun : CircuitType\n RewireFun i o = Fin o \u2192 Fin i\n\n RewireTbl : CircuitType\n RewireTbl i o = Vec (Fin i) o\n\n rewire : \u2200 {a i o} {A : Set a} \u2192 (Fin o \u2192 Fin i) \u2192 Vec A i \u2192 Vec A o\n rewire f v = tabulate (flip lookup v \u2218 f)\n\n rewireTbl : \u2200 {a i o} {A : Set a} \u2192 RewireTbl i o \u2192 Vec A i \u2192 Vec A o\n rewireTbl tbl v = vmap (flip lookup v) tbl\n\n runRewireFun : RunCircuit RewireFun\n runRewireFun = rewire\n\n runRewireTbl : RunCircuit RewireTbl\n runRewireTbl = rewireTbl\n\nopen Rewire using (RewireTbl; RewireFun)\n\nrecord RewiringBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n\n infixr 1 _>>>_\n infixr 3 _***_\n\n field\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n\n rewireWithTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 C i o\n rewireWithTbl = rewire \u2218 flip lookup\n\n idCDefault : \u2200 {i} \u2192 C i i\n idCDefault = rewire id\n\n field\n idC : \u2200 {i} \u2192 C i i\n\n field\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ rewire r ]= Rewire.rewire r bs\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idC ]= bs\n\n idCDefault-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idCDefault ]= bs\n idCDefault-spec bs\n = subst (\u03bb bs' \u2192 bs =[ idCDefault ]= bs') (tabulate\u2218lookup bs) (rewire-spec id bs)\n\n sink : \u2200 i \u2192 C i 0\n sink _ = rewire (\u03bb())\n-- sink-spec : bs =[ sink i ]= []\n\n dup : \u2200 o \u2192 C 1 o\n dup _ = rewire (const zero)\n-- dup-spec : (b \u2237 []) =[ dup o ]= replicate o\n\n dup\u2082 : C 1 2\n dup\u2082 = dup 2\n-- dup\u2082-spec : (b \u2237 []) =[ dup\u2082 ]= (b \u2237 b \u2237 [])\n\n vcat : \u2200 {i o n} \u2192 Vec (C i o) n \u2192 C (n * i) (n * o)\n vcat [] = idC\n vcat (x \u2237 xs) = x *** vcat xs\n\n coerce : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 i\u2080 \u2261 i\u2081 \u2192 o\u2080 \u2261 o\u2081 \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081\n coerce refl refl = id\n\n dup\u207f : \u2200 {n} k \u2192 C n (k * n)\n dup\u207f {n} k = coerce (proj\u2082 \u2115\u00b0.*-identity n) (\u2115\u00b0.*-comm n k) (vcat {n = n} (replicate (dup _)))\n\n ext-before : \u2200 {k i o} \u2192 C i o \u2192 C (k + i) (k + o)\n ext-before {k} c = idC {k} *** c\n\n ext-after : \u2200 {k i o} \u2192 C i o \u2192 C (i + k) (o + k)\n ext-after c = c *** idC\n\n takeC : \u2200 {k i o} \u2192 C i o \u2192 C (i + k) o\n takeC {k} {_} {o} c = coerce refl (\u2115\u00b0.+-comm o 0) (c *** sink k)\n\n takeC' : \u2200 {i} k \u2192 C (i + k) i\n takeC' k = takeC {k} idC\n\n dropC : \u2200 {k i o} \u2192 C i o \u2192 C (k + i) o\n dropC {k} c = sink k *** c\n\n dropC' : \u2200 {i} k \u2192 C (k + i) i\n dropC' k = dropC {k} idC\n\n swap : \u2200 {i} (x y : Fin i) \u2192 C i i\n -- swap x y = arr (\u03bb xs \u2192 (xs [ x ]\u2254 (lookup y xs)) [ y ]\u2254 (lookup x xs))\n swap x y = rewire (Fin.swap x y)\n\n rev : \u2200 {i} \u2192 C i i\n rev = rewire Fin.reverse\n\n swap\u2082 : C 2 2\n swap\u2082 = rev\n -- swap\u2082 = swap (# 0) (# 1)\n\n Perm : \u2115 \u2192 Set\n Perm n = List (Fin n \u00d7 Fin n)\n\n perm : \u2200 {i} \u2192 Perm i \u2192 C i i\n perm [] = idC\n perm ((x , y) \u2237 \u03c0) = swap x y >>> perm \u03c0\n\n headC : \u2200 {i} \u2192 C (1 + i) 1\n headC = idC {1} *** sink _\n\n tailC : \u2200 {i} \u2192 C (1 + i) i\n tailC = dropC' 1\n\nrecord CircuitBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n field\n isRewiringBuilder : RewiringBuilder C\n arr : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n leafC : \u2200 {o} \u2192 Bits o \u2192 C 0 o\n forkC : \u2200 {i o} (c\u2080 c\u2081 : C i o) \u2192 C (1 + i) o\n\n open RewiringBuilder isRewiringBuilder public\n\n bit : Bit \u2192 C 0 1\n bit b = arr (const (b \u2237 []))\n\n 0\u02b7 : C 0 1\n 0\u02b7 = bit 0b\n\n 1\u02b7 : C 0 1\n 1\u02b7 = bit 1b\n\n arr' : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n arr' {zero} f = leafC (f [])\n arr' {suc i} f = forkC (arr' {i} (f \u2218 0\u2237_)) (arr' (f \u2218 1\u2237_))\n\n unOp : (Bit \u2192 Bit) \u2192 C 1 1\n unOp op = arr (\u03bb { (x \u2237 []) \u2192 (op x) \u2237 [] })\n\n notC : C 1 1\n notC = unOp not\n\n binOp : (Bit \u2192 Bit \u2192 Bit) \u2192 C 2 1\n binOp op = arr (\u03bb { (x \u2237 y \u2237 []) \u2192 (op x y) \u2237 [] })\n\n xorC : C 2 1\n xorC = binOp _xor_\n\n eqC : C 2 1\n eqC = binOp _==\u1d47_\n\n orC : C 2 1\n orC = binOp _\u2228_\n\n andC : C 2 1\n andC = binOp _\u2227_\n\n norC : C 2 1\n norC = binOp (\u03bb x y \u2192 not (x \u2228 y))\n\n nandC : C 2 1\n nandC = binOp (\u03bb x y \u2192 not (x \u2227 y))\n\n if\u27e8head=0\u27e9then_else_ : \u2200 {i o} (c\u2080 c\u2081 : C i o) \u2192 C (1 + i) o\n if\u27e8head=0\u27e9then_else_ = forkC\n\n_\u2192\u1d47_ : CircuitType\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n_\u2192\u1da0_ : CircuitType\ni \u2192\u1da0 o = Fin o \u2192 Fin i\n\nfinFunRewiringBuilder : RewiringBuilder _\u2192\u1da0_\nfinFunRewiringBuilder = mk id _\u2218\u2032_ _***_ id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1da0_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = Rewire.rewire f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec = tabulate\u2218lookup\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n _***_ {o\u2080 = o\u2080} f g x with Fin.cmp o\u2080 _ x\n _***_ f _ ._ | Fin.bound x = inject+ _ (f x)\n _***_ {i\u2080} _ g ._ | Fin.free x = raise i\u2080 (g x)\n\ntblRewiringBuilder : RewiringBuilder RewireTbl\ntblRewiringBuilder = mk tabulate _>>>_ _***_ (allFin _) _=[_]=_ rewire-spec idC-spec\n where\n open Rewire\n C = RewireTbl\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tbl ]= output = Rewire.rewireTbl tbl input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ tabulate r ]= Rewire.rewire r bs\n rewire-spec r bs = sym (tabulate-\u2218 (flip lookup bs) r)\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ allFin _ ]= bs\n -- idC-spec = map-lookup-allFin\n idC-spec bs rewrite rewire-spec id bs = tabulate\u2218lookup bs\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n c\u2080 >>> c\u2081 = rewireTbl c\u2081 c\u2080\n -- c\u2080 >>> c\u2081 = tabulate (flip lookup c\u2080 \u2218 flip lookup c\u2081)\n -- c\u2080 >>> c\u2081 = vmap (flip lookup c\u2080) c\u2081\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n _***_ {i\u2080} c\u2080 c\u2081 = vmap (inject+ _) c\u2080 ++ vmap (raise i\u2080) c\u2081\n\nbitsFunRewiringBuilder : RewiringBuilder _\u2192\u1d47_\nbitsFunRewiringBuilder = mk Rewire.rewire _>>>_ _***_ id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1d47_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ Rewire.rewire r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec bs = refl\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n f >>> g = g \u2218 f\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n (f *** g) xs with splitAt _ xs\n ... | ys , zs , _ = f ys ++ g zs\n\nbitsFunCircuitBuilder : CircuitBuilder _\u2192\u1d47_\nbitsFunCircuitBuilder = mk bitsFunRewiringBuilder id (\u03bb { bs [] \u2192 bs }) (\u03bb { f g (b \u2237 bs) \u2192 (if b then f else g) bs })\n\nopen import bintree\nopen import flipbased-tree\n\nTreeBits : CircuitType\nTreeBits i o = Tree (Bits o) i\n\nmodule moretree where\n _>>>_ : \u2200 {m n a} {A : Set a} \u2192 Tree (Bits m) n \u2192 Tree A m \u2192 Tree A n\n f >>> g = map (flip bintree.lookup g) f\n\ntreeBitsRewiringBuilder : RewiringBuilder TreeBits\ntreeBitsRewiringBuilder = mk rewire moretree._>>>_ _***_ (rewire id) _=[_]=_ rewire-spec idC-spec\n where\n C = TreeBits\n\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n rewire f = fromFun (Rewire.rewire f)\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tree ]= output = toFun tree input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ rewire r ]= Rewire.rewire r bs\n rewire-spec r = toFun\u2218fromFun (tabulate \u2218 flip (Data.Vec.lookup \u2218 r))\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ rewire id ]= bs\n idC-spec bs rewrite toFun\u2218fromFun (tabulate \u2218 flip Data.Vec.lookup) bs | tabulate\u2218lookup bs = refl\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n (f *** g) = f >>= \u03bb xs \u2192 map (_++_ xs) g\n\ntreeBitsCircuitBuilder : CircuitBuilder TreeBits\ntreeBitsCircuitBuilder = mk treeBitsRewiringBuilder arr leaf fork\n where\n C = TreeBits\n\n arr : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n arr = fromFun\n\nRewiringTree : CircuitType\nRewiringTree i o = Tree (Fin i) o\n\nmodule RewiringWith2^Outputs where\n C_\u27e82^_\u27e9 = RewiringTree\n\n rewire : \u2200 {i o} \u2192 RewireFun i (2 ^ o) \u2192 C i \u27e82^ o \u27e9\n rewire f = fromFun (f \u2218 toFin)\n\n lookupFin : \u2200 {i o} \u2192 C i \u27e82^ o \u27e9 \u2192 Fin (2 ^ o) \u2192 Fin i\n lookupFin c x = bintree.lookup (fromFin x) c\n\n _>>>_ : \u2200 {i m o} \u2192 C i \u27e82^ m \u27e9 \u2192 C (2 ^ m) \u27e82^ o \u27e9 \u2192 C i \u27e82^ o \u27e9\n f >>> g = rewire (lookupFin f \u2218 lookupFin g)\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 \u27e82^ o\u2080 \u27e9 \u2192 C i\u2081 \u27e82^ o\u2081 \u27e9 \u2192 C (i\u2080 + i\u2081) \u27e82^ (o\u2080 + o\u2081) \u27e9\n f *** g = f >>= \u03bb x \u2192 map (Fin._+\u2032_ x) g\n\nmodule Test where\n open import Data.Bits.Bits4\n\n tbl : RewireTbl 4 4\n tbl = # 1 \u2237 # 0 \u2237 # 2 \u2237 # 2 \u2237 []\n\n fun : RewireFun 4 4\n fun zero = # 1\n fun (suc zero) = # 0\n fun (suc (suc zero)) = # 2\n fun (suc (suc (suc x))) = # 2\n\n-- swap x y \u2248 swap y x\n-- reverse \u2218 reverse \u2248 id\n\n abs : \u2200 {C} \u2192 RewiringBuilder C \u2192 C 4 4\n abs builder = swap\u2082 *** dup\u2082 *** sink 1\n where open RewiringBuilder builder\n\n tinytree : Tree (Fin 4) 2\n tinytree = fork (fork (leaf (# 1)) (leaf (# 0))) (fork (leaf (# 2)) (leaf (# 2)))\n\n bigtree : Tree (Bits 4) 4\n bigtree = fork (fork (fork (same 0000b) (same 0011b)) (fork (same 1000b) (same 1011b)))\n (fork (fork (same 0100b) (same 0111b)) (fork (same 1100b) (same 1111b)))\n where same : \u2200 {n} {A : Set} \u2192 A \u2192 Tree A (suc n)\n same x = fork (leaf x) (leaf x)\n\n test\u2081 : tbl \u2261 tabulate fun\n test\u2081 = refl\n\n test\u2082 : tbl \u2261 abs tblRewiringBuilder\n test\u2082 = refl\n\n test\u2083 : tabulate fun \u2261 tabulate (abs finFunRewiringBuilder)\n test\u2083 = refl\n\n test\u2084 : bigtree \u2261 abs treeBitsRewiringBuilder\n test\u2084 = refl\n\n open RewiringWith2^Outputs\n test\u2085 : tabulate (lookupFin tinytree) \u2261 tbl\n test\u2085 = refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"5c05f76f8895526eefdbaa88f665fd4534bd4a1f","subject":"Fixed indentation.","message":"Fixed indentation.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Data\/Conat\/PropertiesI.agda","new_file":"src\/fot\/FOTC\/Data\/Conat\/PropertiesI.agda","new_contents":"------------------------------------------------------------------------------\n-- Conat properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Conat.PropertiesI where\n\nopen import FOTC.Base\nopen import FOTC.Data.Conat\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Conat predicate is also a pre-fixed point of the functional NatF,\n-- i.e,\n--\n-- NatF Conat \u2264 Conat (see FOTC.Data.Conat.Type).\nConat-pre-fixed : (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')) \u2192\n \u2200 {n} \u2192 Conat n\nConat-pre-fixed h = Conat-coind (\u03bb m \u2192 m \u2261 m) h' refl\n where\n h' : \u2200 {m} \u2192 m \u2261 m \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 m' \u2261 m')\n h' _ with h\n ... | inj\u2081 m\u22610 = inj\u2081 m\u22610\n ... | inj\u2082 (m' , prf , _) = inj\u2082 (m' , prf , refl)\n\n0-Conat : Conat zero\n0-Conat = Conat-coind A h refl\n where\n A : D \u2192 Set\n A n = n \u2261 zero\n\n h : \u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n h An = inj\u2081 An\n\n-- Adapted from (Sander 1992, p. 57).\n\u221e-Conat : Conat \u221e\n\u221e-Conat = Conat-coind A h refl\n where\n A : D \u2192 Set\n A n = n \u2261 \u221e\n\n h : \u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n h An = inj\u2082 (\u221e , trans An \u221e-eq , refl)\n\nN\u2192Conat : \u2200 {n} \u2192 N n \u2192 Conat n\nN\u2192Conat Nn = Conat-coind N h Nn\n where\n h : \u2200 {m} \u2192 N m \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 N m')\n h nzero = inj\u2081 refl\n h (nsucc {m} Nm) = inj\u2082 (m , refl , Nm)\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Sander, Herbert P. (1992). A Logic of Functional Programs with an\n-- Application to Concurrency. PhD thesis. Department of Computer\n-- Sciences: Chalmers University of Technology and University of\n-- Gothenburg.\n","old_contents":"------------------------------------------------------------------------------\n-- Conat properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Conat.PropertiesI where\n\nopen import FOTC.Base\nopen import FOTC.Data.Conat\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Conat predicate is also a pre-fixed point of the functional NatF,\n-- i.e,\n--\n-- NatF Conat \u2264 Conat (see FOTC.Data.Conat.Type).\nConat-pre-fixed : (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')) \u2192\n \u2200 {n} \u2192 Conat n\nConat-pre-fixed h = Conat-coind (\u03bb m \u2192 m \u2261 m) h' refl\n where\n h' : \u2200 {m} \u2192 m \u2261 m \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 m' \u2261 m')\n h' _ with h\n ... | inj\u2081 m\u22610 = inj\u2081 m\u22610\n ... | inj\u2082 (m' , prf , _) = inj\u2082 (m' , prf , refl)\n\n0-Conat : Conat zero\n0-Conat = Conat-coind A h refl\n where\n A : D \u2192 Set\n A n = n \u2261 zero\n\n h : \u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n h An = inj\u2081 An\n\n-- Adapted from (Sander 1992, p. 57).\n\u221e-Conat : Conat \u221e\n\u221e-Conat = Conat-coind A h refl\n where\n A : D \u2192 Set\n A n = n \u2261 \u221e\n\n h : \u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n h An = inj\u2082 (\u221e , trans An \u221e-eq , refl)\n\nN\u2192Conat : \u2200 {n} \u2192 N n \u2192 Conat n\nN\u2192Conat Nn = Conat-coind N h Nn\n where\n h : \u2200 {m} \u2192 N m \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 N m')\n h nzero = inj\u2081 refl\n h (nsucc {m} Nm) = inj\u2082 (m , refl , Nm)\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Sander, Herbert P. (1992). A Logic of Functional Programs with an\n-- Application to Concurrency. PhD thesis. Department of Computer\n-- Sciences: Chalmers University of Technology and University of\n-- Gothenburg.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"0d92eb4d13b0cf810bec100dd2fe9b96ff861d7e","subject":"[ agda ] The --no-coverage-check option was removed.","message":"[ agda ] The --no-coverage-check option was removed.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/thesis\/report\/AgdaIntroduction.agda","new_file":"notes\/thesis\/report\/AgdaIntroduction.agda","new_contents":"{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --exact-split #-}\n{-# OPTIONS --no-sized-types #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule AgdaIntroduction where\n\n-- We add 3 to the fixities of the Agda standard library\u00a00.8.1 (see\n-- Data\/Nat.agda).\ninfixl 10 _*_\ninfixl 9 _+_\ninfixr 5 _\u2237_ _++_\ninfix 4 _\u2261_\n\n-- Dependent function types\nid : (A : Set) \u2192 A \u2192 A\nid A x = x\n\n-- \u03bb-notation\nid\u2082 : (A : Set) \u2192 A \u2192 A\nid\u2082 = \u03bb A \u2192 \u03bb x \u2192 x\n\nid\u2083 : (A : Set) \u2192 A \u2192 A\nid\u2083 = \u03bb A x \u2192 x\n\n-- Implicit arguments\nid\u2084 : {A : Set} \u2192 A \u2192 A\nid\u2084 x = x\n\nid\u2085 : {A : Set} \u2192 A \u2192 A\nid\u2085 = \u03bb x \u2192 x\n\n-- Inductively defined sets and families\ndata Bool : Set where\n false true : Bool\n\ndata \u2115 : Set where\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n\ndata List (A : Set) : Set where\n [] : List A\n _\u2237_ : A \u2192 List A \u2192 List A\n\ndata Vec (A : Set) : \u2115 \u2192 Set where\n [] : Vec A zero\n _\u2237_ : {n : \u2115} \u2192 A \u2192 Vec A n \u2192 Vec A (succ n)\n\ndata Fin : \u2115 \u2192 Set where\n fzero : {n : \u2115} \u2192 Fin (succ n)\n fsucc : {n : \u2115} \u2192 Fin n \u2192 Fin (succ n)\n\n-- Structurally recursive functions and pattern matching\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero + n = n\nsucc m + n = succ (m + n)\n\nmap : {A B : Set} \u2192 (A \u2192 B) \u2192 List A \u2192 List B\nmap f [] = []\nmap f (x \u2237 xs) = f x \u2237 map f xs\n\nf : \u2115 \u2192 \u2115\nf zero = zero\n{-# CATCHALL #-}\nf _ = succ zero\n\n-- The absurd pattern\nmagic : {A : Set} \u2192 Fin zero \u2192 A\nmagic ()\n\n-- The with constructor\n\nfilter : {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 List A\nfilter p [] = []\nfilter p (x \u2237 xs) with p x\n... | true = x \u2237 filter p xs\n... | false = filter p xs\n\nfilter' : {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 List A\nfilter' p [] = []\nfilter' p (x \u2237 xs) with p x\nfilter' p (x \u2237 xs) | true = x \u2237 filter' p xs\nfilter' p (x \u2237 xs) | false = filter' p xs\n\n-- Mutual definitions\neven : \u2115 \u2192 Bool\nodd : \u2115 \u2192 Bool\n\neven zero = true\neven (succ n) = odd n\n\nodd zero = false\nodd (succ n) = even n\n\ndata EvenList : Set\ndata OddList : Set\n\ndata EvenList where\n [] : EvenList\n _\u2237_ : \u2115 \u2192 OddList \u2192 EvenList\n\ndata OddList where\n _\u2237_ : \u2115 \u2192 EvenList \u2192 OddList\n\n-- Normalisation\n\ndata _\u2261_ {A : Set} : A \u2192 A \u2192 Set where\n refl : {a : A} \u2192 a \u2261 a\n\nlength : {A : Set} \u2192 List A \u2192 \u2115\nlength [] = zero\nlength (x \u2237 xs) = succ zero + length xs\n\n_++_ : {A : Set} \u2192 List A \u2192 List A \u2192 List A\n[] ++ ys = ys\n(x \u2237 xs) ++ ys = x \u2237 xs ++ ys\n\nsuccCong : {m n : \u2115} \u2192 m \u2261 n \u2192 succ m \u2261 succ n\nsuccCong refl = refl\n\nlength-++ : {A : Set}(xs ys : List A) \u2192\n length (xs ++ ys) \u2261 length xs + length ys\nlength-++ [] ys = refl\nlength-++ (x \u2237 xs) ys = succCong (length-++ xs ys)\n\n-- Coverage and termination checkers\nhead : {A : Set} \u2192 List A \u2192 A\nhead [] = ?\nhead (x \u2237 xs) = x\n\nack : \u2115 \u2192 \u2115 \u2192 \u2115\nack zero n = succ n\nack (succ m) zero = ack m (succ zero)\nack (succ m) (succ n) = ack m (ack (succ m) n)\n\n-- Combinators for equational reasoning\n\npostulate\n sym : {A : Set}{a b : A} \u2192 a \u2261 b \u2192 b \u2261 a\n trans : {A : Set}{a b c : A} \u2192 a \u2261 b \u2192 b \u2261 c \u2192 a \u2261 c\n subst : {A : Set}(P : A \u2192 Set){a b : A} \u2192 a \u2261 b \u2192 P a \u2192 P b\n\npostulate\n _*_ : \u2115 \u2192 \u2115 \u2192 \u2115\n *-comm : \u2200 m n \u2192 m * n \u2261 n * m\n *-rightIdentity : \u2200 n \u2192 n * succ zero \u2261 n\n\n*-leftIdentity : \u2200 n \u2192 succ zero * n \u2261 n\n*-leftIdentity n =\n trans {\u2115} {succ zero * n} {n * succ zero} {n} (*-comm (succ zero) n) (*-rightIdentity n)\n\nmodule ER\n {A : Set}\n (_\u223c_ : A \u2192 A \u2192 Set)\n (\u223c-refl : \u2200 {x} \u2192 x \u223c x)\n (\u223c-trans : \u2200 {x y z} \u2192 x \u223c y \u2192 y \u223c z \u2192 x \u223c z)\n where\n\n infixr 5 _\u223c\u27e8_\u27e9_\n infix 6 _\u220e\n\n _\u223c\u27e8_\u27e9_ : \u2200 x {y z} \u2192 x \u223c y \u2192 y \u223c z \u2192 x \u223c z\n _ \u223c\u27e8 x\u223cy \u27e9 y\u223cz = \u223c-trans x\u223cy y\u223cz\n\n _\u220e : \u2200 x \u2192 x \u223c x\n _\u220e _ = \u223c-refl\n\nopen module \u2261-Reasoning = ER _\u2261_ (refl {\u2115}) (trans {\u2115})\n renaming ( _\u223c\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_ )\n\n*-leftIdentity' : \u2200 n \u2192 succ zero * n \u2261 n\n*-leftIdentity' n =\n succ zero * n \u2261\u27e8 *-comm (succ zero) n \u27e9\n n * succ zero \u2261\u27e8 *-rightIdentity n \u27e9\n n \u220e\n","old_contents":"{-# OPTIONS --exact-split #-}\n{-# OPTIONS --no-coverage-check #-}\n{-# OPTIONS --no-sized-types #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule AgdaIntroduction where\n\n-- We add 3 to the fixities of the Agda standard library\u00a00.8.1 (see\n-- Data\/Nat.agda).\ninfixl 10 _*_\ninfixl 9 _+_\ninfixr 5 _\u2237_ _++_\ninfix 4 _\u2261_\n\n-- Dependent function types\nid : (A : Set) \u2192 A \u2192 A\nid A x = x\n\n-- \u03bb-notation\nid\u2082 : (A : Set) \u2192 A \u2192 A\nid\u2082 = \u03bb A \u2192 \u03bb x \u2192 x\n\nid\u2083 : (A : Set) \u2192 A \u2192 A\nid\u2083 = \u03bb A x \u2192 x\n\n-- Implicit arguments\nid\u2084 : {A : Set} \u2192 A \u2192 A\nid\u2084 x = x\n\nid\u2085 : {A : Set} \u2192 A \u2192 A\nid\u2085 = \u03bb x \u2192 x\n\n-- Inductively defined sets and families\ndata Bool : Set where\n false true : Bool\n\ndata \u2115 : Set where\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n\ndata List (A : Set) : Set where\n [] : List A\n _\u2237_ : A \u2192 List A \u2192 List A\n\ndata Vec (A : Set) : \u2115 \u2192 Set where\n [] : Vec A zero\n _\u2237_ : {n : \u2115} \u2192 A \u2192 Vec A n \u2192 Vec A (succ n)\n\ndata Fin : \u2115 \u2192 Set where\n fzero : {n : \u2115} \u2192 Fin (succ n)\n fsucc : {n : \u2115} \u2192 Fin n \u2192 Fin (succ n)\n\n-- Structurally recursive functions and pattern matching\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero + n = n\nsucc m + n = succ (m + n)\n\nmap : {A B : Set} \u2192 (A \u2192 B) \u2192 List A \u2192 List B\nmap f [] = []\nmap f (x \u2237 xs) = f x \u2237 map f xs\n\nf : \u2115 \u2192 \u2115\nf zero = zero\n{-# CATCHALL #-}\nf _ = succ zero\n\n-- The absurd pattern\nmagic : {A : Set} \u2192 Fin zero \u2192 A\nmagic ()\n\n-- The with constructor\n\nfilter : {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 List A\nfilter p [] = []\nfilter p (x \u2237 xs) with p x\n... | true = x \u2237 filter p xs\n... | false = filter p xs\n\nfilter' : {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 List A\nfilter' p [] = []\nfilter' p (x \u2237 xs) with p x\nfilter' p (x \u2237 xs) | true = x \u2237 filter' p xs\nfilter' p (x \u2237 xs) | false = filter' p xs\n\n-- Mutual definitions\neven : \u2115 \u2192 Bool\nodd : \u2115 \u2192 Bool\n\neven zero = true\neven (succ n) = odd n\n\nodd zero = false\nodd (succ n) = even n\n\ndata EvenList : Set\ndata OddList : Set\n\ndata EvenList where\n [] : EvenList\n _\u2237_ : \u2115 \u2192 OddList \u2192 EvenList\n\ndata OddList where\n _\u2237_ : \u2115 \u2192 EvenList \u2192 OddList\n\n-- Normalisation\n\ndata _\u2261_ {A : Set} : A \u2192 A \u2192 Set where\n refl : {a : A} \u2192 a \u2261 a\n\nlength : {A : Set} \u2192 List A \u2192 \u2115\nlength [] = zero\nlength (x \u2237 xs) = succ zero + length xs\n\n_++_ : {A : Set} \u2192 List A \u2192 List A \u2192 List A\n[] ++ ys = ys\n(x \u2237 xs) ++ ys = x \u2237 xs ++ ys\n\nsuccCong : {m n : \u2115} \u2192 m \u2261 n \u2192 succ m \u2261 succ n\nsuccCong refl = refl\n\nlength-++ : {A : Set}(xs ys : List A) \u2192\n length (xs ++ ys) \u2261 length xs + length ys\nlength-++ [] ys = refl\nlength-++ (x \u2237 xs) ys = succCong (length-++ xs ys)\n\n-- Coverage and termination checkers\nhead : {A : Set} \u2192 List A \u2192 A\nhead (x \u2237 xs) = x\n\nack : \u2115 \u2192 \u2115 \u2192 \u2115\nack zero n = succ n\nack (succ m) zero = ack m (succ zero)\nack (succ m) (succ n) = ack m (ack (succ m) n)\n\n-- Combinators for equational reasoning\n\npostulate\n sym : {A : Set}{a b : A} \u2192 a \u2261 b \u2192 b \u2261 a\n trans : {A : Set}{a b c : A} \u2192 a \u2261 b \u2192 b \u2261 c \u2192 a \u2261 c\n subst : {A : Set}(P : A \u2192 Set){a b : A} \u2192 a \u2261 b \u2192 P a \u2192 P b\n\npostulate\n _*_ : \u2115 \u2192 \u2115 \u2192 \u2115\n *-comm : \u2200 m n \u2192 m * n \u2261 n * m\n *-rightIdentity : \u2200 n \u2192 n * succ zero \u2261 n\n\n*-leftIdentity : \u2200 n \u2192 succ zero * n \u2261 n\n*-leftIdentity n =\n trans {\u2115} {succ zero * n} {n * succ zero} {n} (*-comm (succ zero) n) (*-rightIdentity n)\n\nmodule ER\n {A : Set}\n (_\u223c_ : A \u2192 A \u2192 Set)\n (\u223c-refl : \u2200 {x} \u2192 x \u223c x)\n (\u223c-trans : \u2200 {x y z} \u2192 x \u223c y \u2192 y \u223c z \u2192 x \u223c z)\n where\n\n infixr 5 _\u223c\u27e8_\u27e9_\n infix 6 _\u220e\n\n _\u223c\u27e8_\u27e9_ : \u2200 x {y z} \u2192 x \u223c y \u2192 y \u223c z \u2192 x \u223c z\n _ \u223c\u27e8 x\u223cy \u27e9 y\u223cz = \u223c-trans x\u223cy y\u223cz\n\n _\u220e : \u2200 x \u2192 x \u223c x\n _\u220e _ = \u223c-refl\n\nopen module \u2261-Reasoning = ER _\u2261_ (refl {\u2115}) (trans {\u2115})\n renaming ( _\u223c\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_ )\n\n*-leftIdentity' : \u2200 n \u2192 succ zero * n \u2261 n\n*-leftIdentity' n =\n succ zero * n \u2261\u27e8 *-comm (succ zero) n \u27e9\n n * succ zero \u2261\u27e8 *-rightIdentity n \u27e9\n n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"83736713e89c34dbd05375de260cf1ac934c06ab","subject":"Try more","message":"Try more\n","repos":"louisswarren\/hieretikz","old_file":"hierarchy.agda","new_file":"hierarchy.agda","new_contents":"module hierarchy where\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\n_and_ : Bool \u2192 Bool \u2192 Bool\ntrue and true = true\ntrue and false = false\nfalse and true = false\nfalse and false = false\n\n_or_ : Bool \u2192 Bool \u2192 Bool\ntrue or true = true\ntrue or false = true\nfalse or true = true\nfalse or false = false\n\n\ndata \u2115 : Set where\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n\n_==_ : \u2115 \u2192 \u2115 \u2192 Bool\nzero == zero = true\nsuc n == suc m = n == m\n_ == _ = false\n\n\n\n\ndata List (A : Set) : Set where\n [] : List A\n _::_ : A \u2192 List A \u2192 List A\n\n_\u2208_ : \u2115 \u2192 List \u2115 \u2192 Bool\nx \u2208 [] = false\nx \u2208 (y :: ys) with x == y\n... | true = true\n... | false = x \u2208 ys\n\n_\u2282_ : List \u2115 \u2192 List \u2115 \u2192 Bool\n[] \u2282 ys = true\n(x :: xs) \u2282 ys with x \u2208 ys\n... | true = xs \u2282 ys\n... | false = false\n\n\n_++_ : {A : Set} \u2192 List A \u2192 List A \u2192 List A\n[] ++ ys = ys\n(x :: xs) ++ ys = x :: (xs ++ ys)\n\nmap : {A : Set} \u2192 (A \u2192 A) \u2192 List A \u2192 List A\nmap f [] = []\nmap f (x :: xs) = (f x) :: (map f xs)\n\n\nany : {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 Bool\nany _ [] = false\nany f (x :: xs) = (f x) or (any f xs)\n\nall : {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 Bool\nall _ [] = true\nall f (x :: xs) = (f x) and (all f xs)\n\n\ndata Arrow : Set where\n _\u21d2_ : List \u2115 \u2192 \u2115 \u2192 Arrow\n\n_\u21d4_ : Arrow \u2192 Arrow \u2192 Bool\n(at \u21d2 ah) \u21d4 (bt \u21d2 bh) = ((at \u2282 bt) and (bt \u2282 at)) and (ah == bh)\n\n_\u2208\u2208_ : Arrow \u2192 List Arrow \u2192 Bool\nx \u2208\u2208 [] = false\nx \u2208\u2208 (y :: ys) with x \u21d4 y\n... | true = true\n... | false = x \u2208\u2208 ys\n\n_\u2282\u2282_ : List Arrow \u2192 List Arrow \u2192 Bool\n[] \u2282\u2282 ys = true\n(x :: xs) \u2282\u2282 ys with x \u2208\u2208 ys\n... | true = xs \u2282\u2282 ys\n... | false = false\n\n\nArrowSearch : List Arrow \u2192 List Arrow \u2192 \u2115 \u2192 Bool\nArrowSearch [] ds g = false\nArrowSearch (([] \u21d2 h) :: cs) ds g with (h == g)\n... | true = true\n... | false = ArrowSearch cs (([] \u21d2 h) :: ds) g\nArrowSearch ((ts \u21d2 h) :: cs) ds g with (h == g)\n... | true = (all (ArrowSearch (cs ++ ds) []) ts)\n or (ArrowSearch cs ((ts \u21d2 h) :: ds) g)\n... | false = ArrowSearch cs ((ts \u21d2 h) :: ds) g\n\n\n_\u22a2_ : List Arrow \u2192 Arrow \u2192 Bool\ncs \u22a2 ((t :: ts) \u21d2 h) = (([] \u21d2 t) :: cs) \u22a2 (ts \u21d2 h)\ncs \u22a2 ([] \u21d2 h) = ArrowSearch cs [] h\n","old_contents":"module hierarchy where\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\n_and_ : Bool \u2192 Bool \u2192 Bool\ntrue and true = true\ntrue and false = false\nfalse and true = false\nfalse and false = false\n\n\ndata \u2115 : Set where\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n\n_==_ : \u2115 \u2192 \u2115 \u2192 Bool\nzero == zero = true\nsuc n == suc m = n == m\n_ == _ = false\n\n\n\n\ndata List (A : Set) : Set where\n [] : List A\n _::_ : A \u2192 List A \u2192 List A\n\n_\u2208_ : \u2115 \u2192 List \u2115 \u2192 Bool\nx \u2208 [] = false\nx \u2208 (y :: ys) with x == y\n... | true = true\n... | false = x \u2208 ys\n\n_\u2282_ : List \u2115 \u2192 List \u2115 \u2192 Bool\n[] \u2282 ys = true\n(x :: xs) \u2282 ys with x \u2208 ys\n... | true = xs \u2282 ys\n... | false = false\n\n\n_++_ : List \u2115 \u2192 List \u2115 \u2192 List \u2115\n[] ++ ys = ys\n(x :: xs) ++ ys with (x \u2208 ys)\n... | true = xs ++ ys\n... | false = x :: (xs ++ ys)\n\nmap : {A : Set} \u2192 (A \u2192 A) \u2192 List A \u2192 List A\nmap f [] = []\nmap f (x :: xs) = (f x) :: (map f xs)\n\n\ndata Arrow : Set where\n _\u21d2_ : List \u2115 \u2192 \u2115 \u2192 Arrow\n\n_\u21d4_ : Arrow \u2192 Arrow \u2192 Bool\n(at \u21d2 ah) \u21d4 (bt \u21d2 bh) = ((at \u2282 bt) and (bt \u2282 at)) and (ah == bh)\n\n_\u2208\u2208_ : Arrow \u2192 List Arrow \u2192 Bool\nx \u2208\u2208 [] = false\nx \u2208\u2208 (y :: ys) with x \u21d4 y\n... | true = true\n... | false = x \u2208\u2208 ys\n\n_\u2282\u2282_ : List Arrow \u2192 List Arrow \u2192 Bool\n[] \u2282\u2282 ys = true\n(x :: xs) \u2282\u2282 ys with x \u2208\u2208 ys\n... | true = xs \u2282\u2282 ys\n... | false = false\n\n\n-- SingleClosure : Arrow \u2192 List \u2115 \u2192 List \u2115\n-- SingleClosure (ts \u21d2 h) ps with ts \u2282 ps\n-- ... | true = h :: ps\n-- ... | false = ps\n--\n-- Closure' : List Arrow \u2192 List \u2115 \u2192 List \u2115\n-- Closure' [] roots = roots\n-- Closure' (c :: cs) roots = (SingleClosure c roots) ++ (Closure' cs roots)\n--\n-- Closure : List Arrow \u2192 List \u2115 \u2192 List \u2115\n-- Closure arrows roots with (Closure' arrows roots) \u2282 roots\n-- ... | true = roots\n-- ... | false = Closure arrows (Closure' arrows roots)\n--\n--\n-- _,_\u22a2_ : List Arrow \u2192 List \u2115 \u2192 \u2115 \u2192 Bool\n-- _ , [] \u22a2 _ = false\n-- thms , p :: premises \u22a2 conc = true\n--\n\nArrowIntro : \u2115 \u2192 Arrow \u2192 Arrow\nArrowIntro n (ts \u21d2 h) = (n :: ts) \u21d2 h\n\nArrowElim : \u2115 \u2192 Arrow \u2192 Arrow\nArrowElim _ ([] \u21d2 h) = [] \u21d2 h\nArrowElim n ((t :: ts) \u21d2 h) with (t == n)\n... | true = ArrowElim n (ts \u21d2 h)\n... | false = ArrowIntro t (ArrowElim n (ts \u21d2 h))\n\nReduce : List Arrow \u2192 List \u2115 \u2192 List Arrow\nReduce cs [] = cs\nReduce cs (n :: ns) with (map (ArrowElim n) cs) \u2282\u2282 cs\n... | true = cs\n... | false = Reduce (map (ArrowElim n) cs) (n :: ns)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"666809a94d7b89e1d7e137009b28f6c4db550b60","subject":"CachingEvaluation: add TODO","message":"CachingEvaluation: add TODO\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Denotation\/CachingEvaluation.agda","new_file":"Parametric\/Denotation\/CachingEvaluation.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Caching evaluation\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\n\nmodule Parametric.Denotation.CachingEvaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\n\nopen import Base.Denotation.Notation\n\nopen import Relation.Binary.PropositionalEquality\n\nmodule Structure where\n {-\n -- Prototype here the type-correctness of a simple non-standard semantics.\n -- This describes a simplified version of the transformation by Liu and\n -- Tanenbaum, PEPM 1995 - but instead of producing object language terms, we\n -- produce host language terms to take advantage of the richer type system of\n -- the host language (in particular, here we need the unit type, product types\n -- and *existentials*).\n -}\n open import Data.Product hiding (map)\n open import Data.Sum hiding (map)\n open import Data.Unit\n\n -- A semantics for empty caching semantics\n \u27e6_\u27e7Type2 : (\u03c4 : Type) \u2192 Set\n \u27e6 base \u03b9 \u27e7Type2 = \u27e6 base \u03b9 \u27e7\n \u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type2 = \u27e6 \u03c3 \u27e7Type \u2192 (\u27e6 \u03c4 \u27e7Type \u00d7 \u22a4)\n\n open import Level\n\n {-\n -- Hidden cache semantics, try 1.\n --\n -- Wrong: even the inputs should be extended.\n -- It's just that extension on base values does nothing interesting.\n\n \u27e6_\u27e7TypeHidCache : (\u03c4 : Type) \u2192 Set\u2081\n \u27e6 base \u03b9 \u27e7TypeHidCache = Lift \u27e6 base \u03b9 \u27e7\n \u27e6 \u03c3 \u21d2 \u03c4 \u27e7TypeHidCache = \u27e6 \u03c3 \u27e7Type \u2192 (\u03a3[ \u03c4\u2081 \u2208 Set ] \u27e6 \u03c4 \u27e7TypeHidCache \u00d7 \u03c4\u2081 )\n -- This type would be cooler, but it also places \"excessive\" requirements on\n -- the object language compared to what we formalize - e.g., unit types.\n --\n --\u27e6 \u03c3 \u21d2 \u03c4 \u27e7TypeHidCache = \u27e6 \u03c3 \u27e7Type \u2192 (\u03a3[ \u03c4\u2081 \u2208 Type ] \u27e6 \u03c4 \u27e7Type \u00d7 \u27e6 \u03c4\u2081 \u27e7Type )\n\n extend : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n extend {base \u03b9} v = lift v\n extend {\u03c3 \u21d2 \u03c4} v = \u03bb x \u2192 \u22a4 , (extend (v x) , tt)\n\n dropCache : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7TypeHidCache \u2192 \u27e6 \u03c4 \u27e7\n dropCache {base \u03b9} v = lower v\n dropCache {\u03c3 \u21d2 \u03c4} v x = dropCache (proj\u2081 (proj\u2082 (v x)))\n\n -- Wrong type signature:\n \u27e6_\u27e7TermCache : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n \u27e6_\u27e7TermCache (const c args) \u03c1 = {!!}\n -- Oh. We don't even know what to return without matching on the type. That's\n -- also true in our term transformation: we need to eta-expand also\n -- non-primitives (higher-order arguments) for everything to possibly work.\n -- Hm... or we need to have extended values in the environment - where the extension is again just for functions.\n \u27e6_\u27e7TermCache (var x) \u03c1 = extend (\u27e6 var x \u27e7 \u03c1)\n -- XXX: this delegates to standard evaluation, I'm not sure whether we should do that.\n -- In fact, we should use our evaluation and select from the result.\n \u27e6_\u27e7TermCache (app s t) \u03c1 = proj\u2081 (proj\u2082 ((\u27e6 s \u27e7TermCache \u03c1) (\u27e6 t \u27e7Term \u03c1)))\n -- Provide an empty cache :-)!\n \u27e6_\u27e7TermCache (abs t) \u03c1 x = \u22a4 , (\u27e6 t \u27e7TermCache (x \u2022 \u03c1) , tt)\n\n\n -- Wrong type definitions: functions get argument of transformed types.\n \u27e6_\u27e7CtxHidCache : (\u0393 : Context) \u2192 Set\u2081\n \u27e6_\u27e7CtxHidCache = DependentList \u27e6_\u27e7TypeHidCache\n\n \u27e6_\u27e7VarHidCache : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7CtxHidCache \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n \u27e6 this \u27e7VarHidCache (v \u2022 \u03c1) = v\n \u27e6 that x \u27e7VarHidCache (v \u2022 \u03c1) = \u27e6 x \u27e7VarHidCache \u03c1\n\n \u27e6_\u27e7TermCache2 : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7CtxHidCache \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n \u27e6_\u27e7TermCache2 (const c args) \u03c1 = {!!}\n \u27e6_\u27e7TermCache2 (var x) \u03c1 = \u27e6 x \u27e7VarHidCache \u03c1\n \u27e6_\u27e7TermCache2 (app s t) \u03c1 = proj\u2081 (proj\u2082 ((\u27e6 s \u27e7TermCache2 \u03c1) (dropCache (\u27e6 t \u27e7TermCache2 \u03c1))))\n -- Provide an empty cache :-)!\n \u27e6_\u27e7TermCache2 (abs t) \u03c1 x = \u22a4 , (\u27e6 t \u27e7TermCache2 ((extend x) \u2022 \u03c1) , tt)\n -}\n\n -- Type semantics for this scenario.\n \u27e6_\u27e7TypeHidCache : (\u03c4 : Type) \u2192 Set\u2081\n \u27e6 base \u03b9 \u27e7TypeHidCache = Lift \u27e6 base \u03b9 \u27e7\n \u27e6 \u03c3 \u21d2 \u03c4 \u27e7TypeHidCache = \u27e6 \u03c3 \u27e7TypeHidCache \u2192 (\u03a3[ \u03c4\u2081 \u2208 Set ] \u27e6 \u03c4 \u27e7TypeHidCache \u00d7 \u03c4\u2081 )\n\n open import Parametric.Syntax.MType as MType\n open MType.Structure Base\n open import Parametric.Syntax.MTerm as MTerm\n open MTerm.Structure Const\n\n open import Function hiding (const)\n\n \u27e6_\u27e7ValType : (\u03c4 : ValType) \u2192 Set\n \u27e6_\u27e7CompType : (\u03c4 : CompType) \u2192 Set\n\n \u27e6 U c \u27e7ValType = \u27e6 c \u27e7CompType\n \u27e6 B \u03b9 \u27e7ValType = \u27e6 base \u03b9 \u27e7\n \u27e6 vUnit \u27e7ValType = Lift Unit\n \u27e6 \u03c4\u2081 v\u00d7 \u03c4\u2082 \u27e7ValType = \u27e6 \u03c4\u2081 \u27e7ValType \u00d7 \u27e6 \u03c4\u2082 \u27e7ValType\n \u27e6 \u03c4\u2081 v+ \u03c4\u2082 \u27e7ValType = \u27e6 \u03c4\u2081 \u27e7ValType \u228e \u27e6 \u03c4\u2082 \u27e7ValType\n\n \u27e6 F \u03c4 \u27e7CompType = \u27e6 \u03c4 \u27e7ValType\n \u27e6 \u03c3 \u21db \u03c4 \u27e7CompType = \u27e6 \u03c3 \u27e7ValType \u2192 \u27e6 \u03c4 \u27e7CompType\n\n -- XXX: below we need to override just a few cases. Inheritance would handle\n -- this precisely; without inheritance, we might want to use one of the\n -- standard encodings of related features (delegation?).\n\n \u27e6_\u27e7ValTypeHidCacheWrong : (\u03c4 : ValType) \u2192 Set\u2081\n \u27e6_\u27e7CompTypeHidCacheWrong : (\u03c4 : CompType) \u2192 Set\u2081\n\n -- This line is the only change, up to now, for the caching semantics starting from CBPV.\n \u27e6 F \u03c4 \u27e7CompTypeHidCacheWrong = (\u03a3[ \u03c4\u2081 \u2208 Set ] \u27e6 \u03c4 \u27e7ValTypeHidCacheWrong \u00d7 \u03c4\u2081 )\n -- Delegation upward.\n \u27e6 \u03c4 \u27e7CompTypeHidCacheWrong = Lift \u27e6 \u03c4 \u27e7CompType\n \u27e6_\u27e7ValTypeHidCacheWrong = Lift \u2218 \u27e6_\u27e7ValType\n -- The above does not override what happens in recursive occurrences.\n\n \u27e6_\u27e7ValTypeHidCache : (\u03c4 : ValType) \u2192 Set\u2081\n \u27e6_\u27e7CompTypeHidCache : (\u03c4 : CompType) \u2192 Set\u2081\n\n \u27e6 U c \u27e7ValTypeHidCache = \u27e6 c \u27e7CompTypeHidCache\n \u27e6 B \u03b9 \u27e7ValTypeHidCache = Lift \u27e6 base \u03b9 \u27e7\n \u27e6 vUnit \u27e7ValTypeHidCache = Lift Unit\n \u27e6 \u03c4\u2081 v\u00d7 \u03c4\u2082 \u27e7ValTypeHidCache = \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache \u00d7 \u27e6 \u03c4\u2082 \u27e7ValTypeHidCache\n \u27e6 \u03c4\u2081 v+ \u03c4\u2082 \u27e7ValTypeHidCache = \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache \u228e \u27e6 \u03c4\u2082 \u27e7ValTypeHidCache\n\n -- This line is the only change, up to now, for the caching semantics.\n \u27e6 F \u03c4 \u27e7CompTypeHidCache = (\u03a3[ \u03c4\u2081 \u2208 Set ] \u27e6 \u03c4 \u27e7ValTypeHidCache \u00d7 \u03c4\u2081 )\n \u27e6 \u03c3 \u21db \u03c4 \u27e7CompTypeHidCache = \u27e6 \u03c3 \u27e7ValTypeHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n\n \u27e6_\u27e7CtxHidCache : (\u0393 : Context) \u2192 Set\u2081\n \u27e6_\u27e7CtxHidCache = DependentList \u27e6_\u27e7TypeHidCache\n\n \u27e6_\u27e7ValCtxHidCache : (\u0393 : ValContext) \u2192 Set\u2081\n \u27e6_\u27e7ValCtxHidCache = DependentList \u27e6_\u27e7ValTypeHidCache\n\n {-\n \u27e6_\u27e7CompCtxHidCache : (\u0393 : CompContext) \u2192 Set\u2081\n \u27e6_\u27e7CompCtxHidCache = DependentList \u27e6_\u27e7CompTypeHidCache\n -}\n\n -- It's questionable that this is not polymorphic enough.\n \u27e6_\u27e7VarHidCache : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7CtxHidCache \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n \u27e6 this \u27e7VarHidCache (v \u2022 \u03c1) = v\n \u27e6 that x \u27e7VarHidCache (v \u2022 \u03c1) = \u27e6 x \u27e7VarHidCache \u03c1\n\n -- Now, let us define a caching semantics for terms.\n\n -- This proves to be hard, because we need to insert and remove caches where\n -- we apply constants.\n\n -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.\n\n --\n -- Inserting and removing caches --\n --\n\n -- Implementation note: The mutual recursion looks like a fold on exponentials, where you need to define the function and the inverse at the same time.\n -- Indeed, both functions seem structurally recursive on \u03c4.\n dropCache : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7TypeHidCache \u2192 \u27e6 \u03c4 \u27e7\n extend : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n\n dropCache {base \u03b9} v = lower v\n dropCache {\u03c3 \u21d2 \u03c4} v x = dropCache (proj\u2081 (proj\u2082 (v (extend x))))\n\n extend {base \u03b9} v = lift v\n extend {\u03c3 \u21d2 \u03c4} v = \u03bb x \u2192 , (extend (v (dropCache x)) , tt)\n\n -- OK, this version is syntax-directed, luckily enough, except on primitives\n -- (as expected). This reveals a bug of ours on higher-order primitives.\n --\n -- Moreover, we can somewhat safely assume that each call to extend and to\n -- dropCache is bad: then we see that the handling of constants is bad. That's\n -- correct, because constants will not return intermediate results in this\n -- schema :-(.\n \u27e6_\u27e7TermCache2 : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7CtxHidCache \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n \u27e6_\u27e7TermCache2 (const c args) \u03c1 = extend (\u27e6 const c args \u27e7 (map dropCache \u03c1))\n \u27e6_\u27e7TermCache2 (var x) \u03c1 = \u27e6 x \u27e7VarHidCache \u03c1\n\n -- It seems odd (a probable bug?) that the result of t needn't be stripped of\n -- its cache.\n \u27e6_\u27e7TermCache2 (app s t) \u03c1 = proj\u2081 (proj\u2082 ((\u27e6 s \u27e7TermCache2 \u03c1) (\u27e6 t \u27e7TermCache2 \u03c1)))\n\n -- Provide an empty cache!\n \u27e6_\u27e7TermCache2 (abs t) \u03c1 x = , (\u27e6 t \u27e7TermCache2 (x \u2022 \u03c1) , tt)\n\n -- The solution is to distinguish among different kinds of constants. Some are\n -- value constructors (and thus do not return caches), while others are\n -- computation constructors (and thus should return caches). For products, I\n -- believe we will only use the products which are values, not computations\n -- (XXX check CBPV paper for the name).\n \u27e6_\u27e7CompTermCache : \u2200 {\u03c4 \u0393} \u2192 Comp \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n \u27e6_\u27e7ValTermCache : \u2200 {\u03c4 \u0393} \u2192 Val \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n\n open import Base.Denotation.Environment ValType \u27e6_\u27e7ValTypeHidCache public\n using ()\n renaming (\u27e6_\u27e7Var to \u27e6_\u27e7ValVar)\n\n -- This says that the environment does not contain caches... sounds wrong!\n -- Either we add extra variables for the caches, or we store computations in\n -- the environment (but that does not make sense), or we store caches in\n -- values, by acting not on F but on something else (U?).\n\n -- I suspect the plan was to use extra variables; that's annoying to model in\n -- Agda but easier in implementations.\n\n \u27e6 vVar x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7ValVar \u03c1\n \u27e6 vThunk x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7CompTermCache \u03c1\n\n -- The real deal, finally.\n open import UNDEFINED\n -- XXX constants are still a slight mess because I'm abusing CBPV...\n -- (Actually, I just forgot the difference, and believe I had too little clue\n -- when I wrote these constructors... but some of them did make sense).\n \u27e6_\u27e7CompTermCache (cConst c args) \u03c1 = reveal UNDEFINED\n \u27e6_\u27e7CompTermCache (cConstV c args) \u03c1 = reveal UNDEFINED\n \u27e6_\u27e7CompTermCache (cConstV2 c args) \u03c1 = reveal UNDEFINED\n\n -- Also, where are introduction forms for pairs and sums among values? With\n -- them, we should see that we can interpret them without adding a cache.\n\n -- Thunks keep seeming noops.\n \u27e6_\u27e7CompTermCache (cForce x) \u03c1 = \u27e6 x \u27e7ValTermCache \u03c1\n\n -- Here, in an actual implementation, we would return the actual cache with\n -- all variables.\n --\n -- The effect of F is a writer monad of cached values (where the monoid is\n -- (isomorphic to) the free monoid over (\u2203 \u03c4 . \u03c4), but we push the\n -- existentials up when pairing things)!\n\n -- That's what we're interpreting computations in. XXX maybe consider using\n -- monads directly. But that doesn't deal with arity.\n \u27e6_\u27e7CompTermCache (cReturn v) \u03c1 = , (\u27e6 v \u27e7ValTermCache \u03c1 , tt)\n\n -- For this to be enough, a lambda needs to return a produce, not to forward\n -- the underlying one (unless there are no intermediate results). The correct\n -- requirements can maybe be enforced through a linear typing discipline.\n\n {-\n -- Here we'd have a problem with the original into from CBPV, because it does\n -- not require converting expressions to the \"CBPV A-normal form\".\n\n \u27e6_\u27e7CompTermCache (v\u2081 into v\u2082) \u03c1 =\n -- Sequence commands and combine their caches.\n {-\n let (_ , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (_ , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in , (r\u2082 , (c\u2081 ,\u2032 c\u2082))\n -}\n\n -- The code above does not work, because we only guarantee that v\u2082 is\n -- a computation, not that it's an F-computation - v\u2082 could also be function\n -- type or a computation product.\n\n -- We need a restricted CBPV, where these two possibilities are forbidden. If\n -- you want to save something between different lambdas, you need to add an F\n -- U to reflect that. (Double-check the papers which show how to encode arity\n -- using CBPV, it seems that they should find the same problem --- XXX they\n -- don't).\n\n -- Instead, just drop the first cache (XXX WRONG).\n \u27e6 v\u2082 \u27e7CompTermCache (proj\u2081 (proj\u2082 (\u27e6 v\u2081 \u27e7CompTermCache \u03c1)) \u2022 \u03c1)\n -}\n\n -- But if we alter _into_ as described above, composing the caches works!\n\n -- XXX: here we do have intermediate results, so we should save them.\n \u27e6_\u27e7CompTermCache (v\u2081 into v\u2082) \u03c1 =\n let (\u03c4\u2081 , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (\u03c4\u2082 , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in , (r\u2082 , (c\u2081 ,\u2032 c\u2082))\n\n -- Note the compositionality and luck: we don't need to do anything at the\n -- cReturn time, we just need the nested into to do their job, because as I\n -- said intermediate results are a writer monad.\n --\n -- Q: But then, do we still need to do all the other stuff? IOW, do we still\n -- need to forbid (\u03bb x . y <- f args; g args') and replace it with (\u03bb x . y <-\n -- f args; z <- g args'; z)?\n --\n -- A: One thing we still need is using the monadic version of into for the\n -- same reasons - which makes sense, since it has the type of monadic bind.\n --\n -- Maybe not: if we use a monad, which respects left and right identity, the\n -- two above forms are equivalent. But what about associativity? We don't have\n -- associativity with nested tuples in the middle. That's why the monad uses\n -- lists! We can also use nested tuple, as long as in the into case we don't\n -- do (a, b) but append a b (ahem, where?), which decomposes the first list\n -- and prepends it to the second. To this end, we need to know the type of the\n -- first element, or to ensure it's always a pair. Maybe we just want to reuse\n -- an HList.\n\n -- In abstractions, we should start collecting all variables...\n\n -- Here, unlike in \u27e6_\u27e7TermCache2, we don't need to invent an empty cache,\n -- that's moved into the handling of cReturn. This makes *the* difference for\n -- nested lambdas, where we don't need to create caches multiple times!\n\n \u27e6_\u27e7CompTermCache (cAbs v) \u03c1 x = \u27e6 v \u27e7CompTermCache (x \u2022 \u03c1)\n\n -- Here we see that we are in a sort of A-normal form, because the argument is\n -- a value (not quite ANF though, since values can be thunks - that is,\n -- computations which haven't been run yet, I guess. Do we have an use for\n -- that? That allows passing lambdas as arguments directly - which is fine,\n -- because producing a closure indeed does not have intermediate results!).\n \u27e6_\u27e7CompTermCache (cApp t v) \u03c1 = \u27e6 t \u27e7CompTermCache \u03c1 (\u27e6 v \u27e7ValTermCache \u03c1)\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Caching evaluation\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\n\nmodule Parametric.Denotation.CachingEvaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\n\nopen import Base.Denotation.Notation\n\nopen import Relation.Binary.PropositionalEquality\n\nmodule Structure where\n {-\n -- Prototype here the type-correctness of a simple non-standard semantics.\n -- This describes a simplified version of the transformation by Liu and\n -- Tanenbaum, PEPM 1995 - but instead of producing object language terms, we\n -- produce host language terms to take advantage of the richer type system of\n -- the host language (in particular, here we need the unit type, product types\n -- and *existentials*).\n -}\n open import Data.Product hiding (map)\n open import Data.Sum hiding (map)\n open import Data.Unit\n\n -- A semantics for empty caching semantics\n \u27e6_\u27e7Type2 : (\u03c4 : Type) \u2192 Set\n \u27e6 base \u03b9 \u27e7Type2 = \u27e6 base \u03b9 \u27e7\n \u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type2 = \u27e6 \u03c3 \u27e7Type \u2192 (\u27e6 \u03c4 \u27e7Type \u00d7 \u22a4)\n\n open import Level\n\n {-\n -- Hidden cache semantics, try 1.\n --\n -- Wrong: even the inputs should be extended.\n -- It's just that extension on base values does nothing interesting.\n\n \u27e6_\u27e7TypeHidCache : (\u03c4 : Type) \u2192 Set\u2081\n \u27e6 base \u03b9 \u27e7TypeHidCache = Lift \u27e6 base \u03b9 \u27e7\n \u27e6 \u03c3 \u21d2 \u03c4 \u27e7TypeHidCache = \u27e6 \u03c3 \u27e7Type \u2192 (\u03a3[ \u03c4\u2081 \u2208 Set ] \u27e6 \u03c4 \u27e7TypeHidCache \u00d7 \u03c4\u2081 )\n -- This type would be cooler, but it also places \"excessive\" requirements on\n -- the object language compared to what we formalize - e.g., unit types.\n --\n --\u27e6 \u03c3 \u21d2 \u03c4 \u27e7TypeHidCache = \u27e6 \u03c3 \u27e7Type \u2192 (\u03a3[ \u03c4\u2081 \u2208 Type ] \u27e6 \u03c4 \u27e7Type \u00d7 \u27e6 \u03c4\u2081 \u27e7Type )\n\n extend : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n extend {base \u03b9} v = lift v\n extend {\u03c3 \u21d2 \u03c4} v = \u03bb x \u2192 \u22a4 , (extend (v x) , tt)\n\n dropCache : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7TypeHidCache \u2192 \u27e6 \u03c4 \u27e7\n dropCache {base \u03b9} v = lower v\n dropCache {\u03c3 \u21d2 \u03c4} v x = dropCache (proj\u2081 (proj\u2082 (v x)))\n\n -- Wrong type signature:\n \u27e6_\u27e7TermCache : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n \u27e6_\u27e7TermCache (const c args) \u03c1 = {!!}\n -- Oh. We don't even know what to return without matching on the type. That's\n -- also true in our term transformation: we need to eta-expand also\n -- non-primitives (higher-order arguments) for everything to possibly work.\n -- Hm... or we need to have extended values in the environment - where the extension is again just for functions.\n \u27e6_\u27e7TermCache (var x) \u03c1 = extend (\u27e6 var x \u27e7 \u03c1)\n -- XXX: this delegates to standard evaluation, I'm not sure whether we should do that.\n -- In fact, we should use our evaluation and select from the result.\n \u27e6_\u27e7TermCache (app s t) \u03c1 = proj\u2081 (proj\u2082 ((\u27e6 s \u27e7TermCache \u03c1) (\u27e6 t \u27e7Term \u03c1)))\n -- Provide an empty cache :-)!\n \u27e6_\u27e7TermCache (abs t) \u03c1 x = \u22a4 , (\u27e6 t \u27e7TermCache (x \u2022 \u03c1) , tt)\n\n\n -- Wrong type definitions: functions get argument of transformed types.\n \u27e6_\u27e7CtxHidCache : (\u0393 : Context) \u2192 Set\u2081\n \u27e6_\u27e7CtxHidCache = DependentList \u27e6_\u27e7TypeHidCache\n\n \u27e6_\u27e7VarHidCache : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7CtxHidCache \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n \u27e6 this \u27e7VarHidCache (v \u2022 \u03c1) = v\n \u27e6 that x \u27e7VarHidCache (v \u2022 \u03c1) = \u27e6 x \u27e7VarHidCache \u03c1\n\n \u27e6_\u27e7TermCache2 : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7CtxHidCache \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n \u27e6_\u27e7TermCache2 (const c args) \u03c1 = {!!}\n \u27e6_\u27e7TermCache2 (var x) \u03c1 = \u27e6 x \u27e7VarHidCache \u03c1\n \u27e6_\u27e7TermCache2 (app s t) \u03c1 = proj\u2081 (proj\u2082 ((\u27e6 s \u27e7TermCache2 \u03c1) (dropCache (\u27e6 t \u27e7TermCache2 \u03c1))))\n -- Provide an empty cache :-)!\n \u27e6_\u27e7TermCache2 (abs t) \u03c1 x = \u22a4 , (\u27e6 t \u27e7TermCache2 ((extend x) \u2022 \u03c1) , tt)\n -}\n\n -- Type semantics for this scenario.\n \u27e6_\u27e7TypeHidCache : (\u03c4 : Type) \u2192 Set\u2081\n \u27e6 base \u03b9 \u27e7TypeHidCache = Lift \u27e6 base \u03b9 \u27e7\n \u27e6 \u03c3 \u21d2 \u03c4 \u27e7TypeHidCache = \u27e6 \u03c3 \u27e7TypeHidCache \u2192 (\u03a3[ \u03c4\u2081 \u2208 Set ] \u27e6 \u03c4 \u27e7TypeHidCache \u00d7 \u03c4\u2081 )\n\n open import Parametric.Syntax.MType as MType\n open MType.Structure Base\n open import Parametric.Syntax.MTerm as MTerm\n open MTerm.Structure Const\n\n open import Function hiding (const)\n\n \u27e6_\u27e7ValType : (\u03c4 : ValType) \u2192 Set\n \u27e6_\u27e7CompType : (\u03c4 : CompType) \u2192 Set\n\n \u27e6 U c \u27e7ValType = \u27e6 c \u27e7CompType\n \u27e6 B \u03b9 \u27e7ValType = \u27e6 base \u03b9 \u27e7\n \u27e6 vUnit \u27e7ValType = Lift Unit\n \u27e6 \u03c4\u2081 v\u00d7 \u03c4\u2082 \u27e7ValType = \u27e6 \u03c4\u2081 \u27e7ValType \u00d7 \u27e6 \u03c4\u2082 \u27e7ValType\n \u27e6 \u03c4\u2081 v+ \u03c4\u2082 \u27e7ValType = \u27e6 \u03c4\u2081 \u27e7ValType \u228e \u27e6 \u03c4\u2082 \u27e7ValType\n\n \u27e6 F \u03c4 \u27e7CompType = \u27e6 \u03c4 \u27e7ValType\n \u27e6 \u03c3 \u21db \u03c4 \u27e7CompType = \u27e6 \u03c3 \u27e7ValType \u2192 \u27e6 \u03c4 \u27e7CompType\n\n -- XXX: below we need to override just a few cases. Inheritance would handle\n -- this precisely; without inheritance, we might want to use one of the\n -- standard encodings of related features (delegation?).\n\n \u27e6_\u27e7ValTypeHidCacheWrong : (\u03c4 : ValType) \u2192 Set\u2081\n \u27e6_\u27e7CompTypeHidCacheWrong : (\u03c4 : CompType) \u2192 Set\u2081\n\n -- This line is the only change, up to now, for the caching semantics starting from CBPV.\n \u27e6 F \u03c4 \u27e7CompTypeHidCacheWrong = (\u03a3[ \u03c4\u2081 \u2208 Set ] \u27e6 \u03c4 \u27e7ValTypeHidCacheWrong \u00d7 \u03c4\u2081 )\n -- Delegation upward.\n \u27e6 \u03c4 \u27e7CompTypeHidCacheWrong = Lift \u27e6 \u03c4 \u27e7CompType\n \u27e6_\u27e7ValTypeHidCacheWrong = Lift \u2218 \u27e6_\u27e7ValType\n -- The above does not override what happens in recursive occurrences.\n\n \u27e6_\u27e7ValTypeHidCache : (\u03c4 : ValType) \u2192 Set\u2081\n \u27e6_\u27e7CompTypeHidCache : (\u03c4 : CompType) \u2192 Set\u2081\n\n \u27e6 U c \u27e7ValTypeHidCache = \u27e6 c \u27e7CompTypeHidCache\n \u27e6 B \u03b9 \u27e7ValTypeHidCache = Lift \u27e6 base \u03b9 \u27e7\n \u27e6 vUnit \u27e7ValTypeHidCache = Lift Unit\n \u27e6 \u03c4\u2081 v\u00d7 \u03c4\u2082 \u27e7ValTypeHidCache = \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache \u00d7 \u27e6 \u03c4\u2082 \u27e7ValTypeHidCache\n \u27e6 \u03c4\u2081 v+ \u03c4\u2082 \u27e7ValTypeHidCache = \u27e6 \u03c4\u2081 \u27e7ValTypeHidCache \u228e \u27e6 \u03c4\u2082 \u27e7ValTypeHidCache\n\n -- This line is the only change, up to now, for the caching semantics.\n \u27e6 F \u03c4 \u27e7CompTypeHidCache = (\u03a3[ \u03c4\u2081 \u2208 Set ] \u27e6 \u03c4 \u27e7ValTypeHidCache \u00d7 \u03c4\u2081 )\n \u27e6 \u03c3 \u21db \u03c4 \u27e7CompTypeHidCache = \u27e6 \u03c3 \u27e7ValTypeHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n\n \u27e6_\u27e7CtxHidCache : (\u0393 : Context) \u2192 Set\u2081\n \u27e6_\u27e7CtxHidCache = DependentList \u27e6_\u27e7TypeHidCache\n\n \u27e6_\u27e7ValCtxHidCache : (\u0393 : ValContext) \u2192 Set\u2081\n \u27e6_\u27e7ValCtxHidCache = DependentList \u27e6_\u27e7ValTypeHidCache\n\n {-\n \u27e6_\u27e7CompCtxHidCache : (\u0393 : CompContext) \u2192 Set\u2081\n \u27e6_\u27e7CompCtxHidCache = DependentList \u27e6_\u27e7CompTypeHidCache\n -}\n\n -- It's questionable that this is not polymorphic enough.\n \u27e6_\u27e7VarHidCache : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7CtxHidCache \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n \u27e6 this \u27e7VarHidCache (v \u2022 \u03c1) = v\n \u27e6 that x \u27e7VarHidCache (v \u2022 \u03c1) = \u27e6 x \u27e7VarHidCache \u03c1\n\n -- Now, let us define a caching semantics for terms.\n\n -- This proves to be hard, because we need to insert and remove caches where\n -- we apply constants.\n\n -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.\n\n --\n -- Inserting and removing caches --\n --\n\n -- Implementation note: The mutual recursion looks like a fold on exponentials, where you need to define the function and the inverse at the same time.\n -- Indeed, both functions seem structurally recursive on \u03c4.\n dropCache : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7TypeHidCache \u2192 \u27e6 \u03c4 \u27e7\n extend : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n\n dropCache {base \u03b9} v = lower v\n dropCache {\u03c3 \u21d2 \u03c4} v x = dropCache (proj\u2081 (proj\u2082 (v (extend x))))\n\n extend {base \u03b9} v = lift v\n extend {\u03c3 \u21d2 \u03c4} v = \u03bb x \u2192 , (extend (v (dropCache x)) , tt)\n\n -- OK, this version is syntax-directed, luckily enough, except on primitives\n -- (as expected). This reveals a bug of ours on higher-order primitives.\n --\n -- Moreover, we can somewhat safely assume that each call to extend and to\n -- dropCache is bad: then we see that the handling of constants is bad. That's\n -- correct, because constants will not return intermediate results in this\n -- schema :-(.\n \u27e6_\u27e7TermCache2 : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7CtxHidCache \u2192 \u27e6 \u03c4 \u27e7TypeHidCache\n \u27e6_\u27e7TermCache2 (const c args) \u03c1 = extend (\u27e6 const c args \u27e7 (map dropCache \u03c1))\n \u27e6_\u27e7TermCache2 (var x) \u03c1 = \u27e6 x \u27e7VarHidCache \u03c1\n\n -- It seems odd (a probable bug?) that the result of t needn't be stripped of\n -- its cache.\n \u27e6_\u27e7TermCache2 (app s t) \u03c1 = proj\u2081 (proj\u2082 ((\u27e6 s \u27e7TermCache2 \u03c1) (\u27e6 t \u27e7TermCache2 \u03c1)))\n\n -- Provide an empty cache!\n \u27e6_\u27e7TermCache2 (abs t) \u03c1 x = , (\u27e6 t \u27e7TermCache2 (x \u2022 \u03c1) , tt)\n\n -- The solution is to distinguish among different kinds of constants. Some are\n -- value constructors (and thus do not return caches), while others are\n -- computation constructors (and thus should return caches). For products, I\n -- believe we will only use the products which are values, not computations\n -- (XXX check CBPV paper for the name).\n \u27e6_\u27e7CompTermCache : \u2200 {\u03c4 \u0393} \u2192 Comp \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n \u27e6_\u27e7ValTermCache : \u2200 {\u03c4 \u0393} \u2192 Val \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n\n open import Base.Denotation.Environment ValType \u27e6_\u27e7ValTypeHidCache public\n using ()\n renaming (\u27e6_\u27e7Var to \u27e6_\u27e7ValVar)\n\n -- This says that the environment does not contain caches... sounds wrong!\n -- Either we add extra variables for the caches, or we store computations in\n -- the environment (but that does not make sense), or we store caches in\n -- values, by acting not on F but on something else (U?).\n\n -- I suspect the plan was to use extra variables; that's annoying to model in\n -- Agda but easier in implementations.\n\n \u27e6 vVar x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7ValVar \u03c1\n \u27e6 vThunk x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7CompTermCache \u03c1\n\n -- The real deal, finally.\n open import UNDEFINED\n -- XXX constants are still a slight mess because I'm abusing CBPV...\n -- (Actually, I just forgot the difference, and believe I had too little clue\n -- when I wrote these constructors... but some of them did make sense).\n \u27e6_\u27e7CompTermCache (cConst c args) \u03c1 = reveal UNDEFINED\n \u27e6_\u27e7CompTermCache (cConstV c args) \u03c1 = reveal UNDEFINED\n \u27e6_\u27e7CompTermCache (cConstV2 c args) \u03c1 = reveal UNDEFINED\n\n -- Also, where are introduction forms for pairs and sums among values? With\n -- them, we should see that we can interpret them without adding a cache.\n\n -- Thunks keep seeming noops.\n \u27e6_\u27e7CompTermCache (cForce x) \u03c1 = \u27e6 x \u27e7ValTermCache \u03c1\n\n -- Here, in an actual implementation, we would return the actual cache with\n -- all variables.\n --\n -- The effect of F is a writer monad of cached values (where the monoid is\n -- (isomorphic to) the free monoid over (\u2203 \u03c4 . \u03c4), but we push the\n -- existentials up when pairing things)!\n\n -- That's what we're interpreting computations in. XXX maybe consider using\n -- monads directly. But that doesn't deal with arity.\n \u27e6_\u27e7CompTermCache (cReturn v) \u03c1 = , (\u27e6 v \u27e7ValTermCache \u03c1 , tt)\n\n -- For this to be enough, a lambda needs to return a produce, not to forward\n -- the underlying one (unless there are no intermediate results). The correct\n -- requirements can maybe be enforced through a linear typing discipline.\n\n {-\n -- Here we'd have a problem with the original into from CBPV, because it does\n -- not require converting expressions to the \"CBPV A-normal form\".\n\n \u27e6_\u27e7CompTermCache (v\u2081 into v\u2082) \u03c1 =\n -- Sequence commands and combine their caches.\n {-\n let (_ , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (_ , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in , (r\u2082 , (c\u2081 ,\u2032 c\u2082))\n -}\n\n -- The code above does not work, because we only guarantee that v\u2082 is\n -- a computation, not that it's an F-computation - v\u2082 could also be function\n -- type or a computation product.\n\n -- We need a restricted CBPV, where these two possibilities are forbidden. If\n -- you want to save something between different lambdas, you need to add an F\n -- U to reflect that. (Double-check the papers which show how to encode arity\n -- using CBPV, it seems that they should find the same problem --- XXX they\n -- don't).\n\n -- Instead, just drop the first cache (XXX WRONG).\n \u27e6 v\u2082 \u27e7CompTermCache (proj\u2081 (proj\u2082 (\u27e6 v\u2081 \u27e7CompTermCache \u03c1)) \u2022 \u03c1)\n -}\n\n -- But if we alter _into_ as described above, composing the caches works!\n\n \u27e6_\u27e7CompTermCache (v\u2081 into v\u2082) \u03c1 =\n let (\u03c4\u2081 , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (\u03c4\u2082 , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in , (r\u2082 , (c\u2081 ,\u2032 c\u2082))\n\n -- Note the compositionality and luck: we don't need to do anything at the\n -- cReturn time, we just need the nested into to do their job, because as I\n -- said intermediate results are a writer monad.\n --\n -- Q: But then, do we still need to do all the other stuff? IOW, do we still\n -- need to forbid (\u03bb x . y <- f args; g args') and replace it with (\u03bb x . y <-\n -- f args; z <- g args'; z)?\n --\n -- A: One thing we still need is using the monadic version of into for the\n -- same reasons - which makes sense, since it has the type of monadic bind.\n --\n -- Maybe not: if we use a monad, which respects left and right identity, the\n -- two above forms are equivalent. But what about associativity? We don't have\n -- associativity with nested tuples in the middle. That's why the monad uses\n -- lists! We can also use nested tuple, as long as in the into case we don't\n -- do (a, b) but append a b (ahem, where?), which decomposes the first list\n -- and prepends it to the second. To this end, we need to know the type of the\n -- first element, or to ensure it's always a pair. Maybe we just want to reuse\n -- an HList.\n\n -- In abstractions, we should start collecting all variables...\n\n -- Here, unlike in \u27e6_\u27e7TermCache2, we don't need to invent an empty cache,\n -- that's moved into the handling of cReturn. This makes *the* difference for\n -- nested lambdas, where we don't need to create caches multiple times!\n\n \u27e6_\u27e7CompTermCache (cAbs v) \u03c1 x = \u27e6 v \u27e7CompTermCache (x \u2022 \u03c1)\n\n -- Here we see that we are in a sort of A-normal form, because the argument is\n -- a value (not quite ANF though, since values can be thunks - that is,\n -- computations which haven't been run yet, I guess. Do we have an use for\n -- that? That allows passing lambdas as arguments directly - which is fine,\n -- because producing a closure indeed does not have intermediate results!).\n \u27e6_\u27e7CompTermCache (cApp t v) \u03c1 = \u27e6 t \u27e7CompTermCache \u03c1 (\u27e6 v \u27e7ValTermCache \u03c1)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"e1edb95d9ae37f9ff8d1a6fdf36c68c75f7b3c33","subject":"Simplify","message":"Simplify\n","repos":"DSLsofMath\/DSLsofMath","old_file":"FLABloM\/SquareMatrix.agda","new_file":"FLABloM\/SquareMatrix.agda","new_contents":"-- Matrices indexed by shape\nmodule SquareMatrix where\n\nopen import Shape\nopen import Matrix\n\n-- Square matrices have the same shape in both dimensions\nSq : Set \u2192 Shape \u2192 Set\nSq a shape = M a shape shape\n\nopen import SemiNearRingRecord\nopen import Algebra.Structures\nopen import Relation.Binary\nopen import Data.Product hiding (swap)\n\nopen import Relation.Binary.PropositionalEquality hiding (trans; sym) renaming (refl to refl-\u2261)\nimport Relation.Binary.EqReasoning as EqReasoning\n\n\n-- Lifting a SNR to a to a Square matrix of some shape\nSquare : SemiNearRing \u2192 Shape \u2192 SemiNearRing\nSquare snr shape = SNR\n where\n open SemiNearRing snr\n\n S = Sq s shape\n\n infixr 60 _\u2219S_\n infixr 50 _+S_\n\n\n _+S_ : \u2200 {r c} \u2192 M s r c \u2192 M s r c \u2192 M s r c\n One x +S One x\u2081 = One (x +\u209b x\u2081)\n Row m m\u2081 +S Row n n\u2081 = Row (m +S n) (m\u2081 +S n\u2081)\n Col m m\u2081 +S Col n n\u2081 = Col (m +S n) (m\u2081 +S n\u2081)\n Q m00 m01\n m10 m11 +S Q n00 n01\n n10 n11 = Q (m00 +S n00) (m01 +S n01)\n (m10 +S n10) (m11 +S n11)\n\n _\u2219S_ : \u2200 {r m c} \u2192 M s r m \u2192 M s m c \u2192 M s r c\n One x \u2219S One x\u2081 = One (x \u2219\u209b x\u2081)\n One x \u2219S Row n n\u2081 = Row (One x \u2219S n) (One x \u2219S n\u2081)\n Row m m\u2081 \u2219S Col n n\u2081 = m \u2219S n +S m\u2081 \u2219S n\u2081\n Row m m\u2081 \u2219S Q n00 n01\n n10 n11 = Row (m \u2219S n00 +S m\u2081 \u2219S n10) (m \u2219S n01 +S m\u2081 \u2219S n11)\n Col m m\u2081 \u2219S One x = Col (m \u2219S One x) (m\u2081 \u2219S One x)\n Col m m\u2081 \u2219S Row n n\u2081 = Q (m \u2219S n) (m \u2219S n\u2081)\n (m\u2081 \u2219S n) (m\u2081 \u2219S n\u2081)\n Q m00 m01\n m10 m11 \u2219S Col n n\u2081 = Col (m00 \u2219S n +S m01 \u2219S n\u2081) (m10 \u2219S n +S m11 \u2219S n\u2081)\n Q m00 m01\n m10 m11 \u2219S Q n00 n01\n n10 n11 = Q (m00 \u2219S n00 +S m01 \u2219S n10) (m00 \u2219S n01 +S m01 \u2219S n11)\n (m10 \u2219S n00 +S m11 \u2219S n10) (m10 \u2219S n01 +S m11 \u2219S n11)\n\n 0S : (r c : Shape) \u2192 M s r c\n 0S L L = One 0\u209b\n 0S L (B s s\u2081) = Row (0S L s) (0S L s\u2081)\n 0S (B r r\u2081) L = Col (0S r L) (0S r\u2081 L)\n 0S (B r r\u2081) (B s s\u2081) =\n Q (0S r s) (0S r s\u2081)\n (0S r\u2081 s) (0S r\u2081 s\u2081)\n\n _\u2243S_ : {r c : Shape} \u2192\n M s r c \u2192 M s r c \u2192 Set\n _\u2243S_ {L} {L} (One x) (One x\u2081) = x \u2243\u209b x\u2081\n _\u2243S_ {L} {(B c\u2081 c\u2082)} (Row m m\u2081) (Row n n\u2081) =\n _\u2243S_ m n \u00d7 _\u2243S_ m\u2081 n\u2081\n _\u2243S_ {(B r\u2081 r\u2082)} {L} (Col m m\u2081) (Col n n\u2081) =\n _\u2243S_ m n \u00d7 _\u2243S_ m\u2081 n\u2081\n _\u2243S_ {(B r\u2081 r\u2082)} {(B c\u2081 c\u2082)} (Q m00 m01 m10 m11) (Q n00 n01 n10 n11) =\n _\u2243S_ m00 n00 \u00d7\n _\u2243S_ m01 n01 \u00d7\n _\u2243S_ m10 n10 \u00d7\n _\u2243S_ m11 n11\n\n\n reflS : (r c : Shape) \u2192\n {X : M s r c} \u2192 X \u2243S X\n reflS L L {X = One x} = refl\u209b {x}\n reflS L (B c\u2081 c\u2082) {X = Row X Y} = reflS L c\u2081 , reflS L c\u2082\n reflS (B r\u2081 r\u2082) L {X = Col X Y} = reflS r\u2081 L , reflS r\u2082 L\n reflS (B r\u2081 r\u2082) (B c\u2081 c\u2082) {X = Q X Y Z W} =\n reflS r\u2081 c\u2081 , reflS r\u2081 c\u2082 ,\n reflS r\u2082 c\u2081 , reflS r\u2082 c\u2082\n\n symS : (r c : Shape) \u2192\n {i j : M s r c} \u2192 i \u2243S j \u2192 j \u2243S i\n symS L L {One x} {One x\u2081} p = sym\u209b p\n symS L (B c\u2081 c\u2082) {Row i\u2081 i\u2082} {Row j\u2081 j\u2082} (p , q) = symS L c\u2081 p , symS L c\u2082 q\n symS (B r\u2081 r\u2082) L {Col i\u2081 i} {Col j j\u2081} (p , q) = symS r\u2081 L p , symS r\u2082 L q\n symS (B r r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j j\u2081 j\u2082 j\u2083} (p , q , x , y) =\n symS r c\u2081 p , symS r c\u2082 q ,\n symS r\u2082 c\u2081 x , symS r\u2082 c\u2082 y\n\n transS : (r c : Shape) \u2192\n {i j k : M s r c} \u2192 i \u2243S j \u2192 j \u2243S k \u2192 i \u2243S k\n transS L L {One x} {One x\u2081} {One x\u2082} p q = trans p q\n transS L (B c\u2081 c\u2082) {Row i i\u2081} {Row j j\u2081} {Row k k\u2081} (p , q) (p' , q') =\n transS L c\u2081 p p' , transS L c\u2082 q q'\n transS (B r\u2081 r\u2082) L {Col i i\u2081} {Col j j\u2081} {Col k k\u2081} (p , q) (p' , q') =\n transS r\u2081 L p p' , transS r\u2082 L q q'\n transS (B r\u2081 r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j\u2082 j j\u2083 j\u2081} {Q k k\u2081 k\u2082 k\u2083}\n (p , q , x , y) (p' , q' , x' , y') =\n transS r\u2081 c\u2081 p p' , transS r\u2081 c\u2082 q q' ,\n transS r\u2082 c\u2081 x x' , transS r\u2082 c\u2082 y y'\n\n isEquivS =\n record\n { refl = reflS shape shape\n ; sym = symS shape shape\n ; trans = transS shape shape }\n\n assocS : (r c : Shape) (x y z : M s r c) \u2192 ((x +S y) +S z) \u2243S (x +S (y +S z))\n assocS L L (One x) (One y) (One z) = assoc\u209b x y z\n assocS L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) (Row z z\u2081) =\n assocS L c x y z , assocS L c\u2081 x\u2081 y\u2081 z\u2081\n assocS (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) (Col z z\u2081) =\n assocS r L x y z , assocS r\u2081 L x\u2081 y\u2081 z\u2081\n assocS (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n (assocS r c x y z) , (assocS r c\u2081 x\u2081 y\u2081 z\u2081) ,\n (assocS r\u2081 c x\u2082 y\u2082 z\u2082) , (assocS r\u2081 c\u2081 x\u2083 y\u2083 z\u2083)\n\n <+S> : (r c : Shape) {x y u v : M s r c} \u2192\n x \u2243S y \u2192 u \u2243S v \u2192 (x +S u) \u2243S (y +S v)\n <+S> L L {One x} {One x\u2081} {One x\u2082} {One x\u2083} p q = p <+> q\n <+S> L (B c c\u2081) {Row x x\u2081} {Row y y\u2081} {Row u u\u2081} {Row v v\u2081} (p , p\u2081) (q , q\u2081) =\n <+S> L c p q , <+S> L c\u2081 p\u2081 q\u2081\n <+S> (B r r\u2081) L {Col x x\u2081} {Col y y\u2081} {Col u u\u2081} {Col v v\u2081} (p , p\u2081) (q , q\u2081) =\n <+S> r L p q , <+S> r\u2081 L p\u2081 q\u2081\n <+S> (B r r\u2081) (B c c\u2081) {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083}\n (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081 , q\u2082 , q\u2083) =\n <+S> r c p q , <+S> r c\u2081 p\u2081 q\u2081 ,\n <+S> r\u2081 c p\u2082 q\u2082 , <+S> r\u2081 c\u2081 p\u2083 q\u2083\n\n isSemgroupS =\n record\n { isEquivalence = isEquivS\n ; assoc = assocS shape shape\n ; \u2219-cong = <+S> shape shape }\n\n\n identS\u02e1 : (r c : Shape) (x : M s r c) \u2192\n 0S r c +S x \u2243S x\n identS\u02e1 L L (One x) = identity\u02e1\u209b x\n identS\u02e1 L (B c c\u2081) (Row x x\u2081) = identS\u02e1 L c x , identS\u02e1 L c\u2081 x\u2081\n identS\u02e1 (B r r\u2081) L (Col x x\u2081) = identS\u02e1 r L x , identS\u02e1 r\u2081 L x\u2081\n identS\u02e1 (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n identS\u02e1 r c x , identS\u02e1 r c\u2081 x\u2081 ,\n identS\u02e1 r\u2081 c x\u2082 , identS\u02e1 r\u2081 c\u2081 x\u2083\n\n commS : (r c : Shape) \u2192 (x y : M s r c) \u2192\n (x +S y) \u2243S (y +S x)\n commS L L (One x) (One x\u2081) = comm\u209b x x\u2081\n commS L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) = (commS L c x y) , (commS L c\u2081 x\u2081 y\u2081)\n commS (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) = (commS r L x y) , (commS r\u2081 L x\u2081 y\u2081)\n commS (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) =\n commS r c x y , commS r c\u2081 x\u2081 y\u2081 ,\n commS r\u2081 c x\u2082 y\u2082 , commS r\u2081 c\u2081 x\u2083 y\u2083\n\n isCommMonS =\n record\n { isSemigroup = isSemgroupS\n ; identity\u02e1 = identS\u02e1 shape shape\n ; comm = commS shape shape }\n\n\n -- used to make proofs nicer later on\n setoidS : {r c : Shape} \u2192 Setoid _ _\n setoidS {r} {c} =\n record\n { Carrier = M s r c\n ; _\u2248_ = _\u2243S_ {r} {c}\n ; isEquivalence =\n record\n { refl = reflS r c ; sym = symS r c ; trans = transS r c } }\n\n zerolHelp : \u2200 (r : Shape) {m m' c : Shape}\n (x : M s m c)\n (y : M s m' c) \u2192\n 0S r m \u2219S x \u2243S 0S r c \u2192\n 0S r m' \u2219S y \u2243S 0S r c \u2192\n 0S r m \u2219S x +S 0S r m' \u2219S y\n \u2243S 0S r c\n zerolHelp r {m} {m'} {c} x y p q =\n let open EqReasoning setoidS\n in begin\n 0S r m \u2219S x +S 0S r m' \u2219S y\n \u2248\u27e8 <+S> _ _ {0S r m \u2219S x} {0S r c} {0S r m' \u2219S y} {0S r c} p q \u27e9\n 0S r c +S 0S r c\n \u2248\u27e8 identS\u02e1 _ _ (0S r c) \u27e9\n 0S r c\n \u220e\n\n zeroS\u02e1 : (a b c : Shape) (x : M s b c) \u2192\n (0S a b \u2219S x) \u2243S 0S a c\n zeroS\u02e1 L L L (One x) = zero\u02e1 x\n zeroS\u02e1 L L (B c c\u2081) (Row x x\u2081) = (zeroS\u02e1 L L c x) , (zeroS\u02e1 L L c\u2081 x\u2081)\n zeroS\u02e1 L (B b b\u2081) L (Col x x\u2081) =\n zerolHelp L x x\u2081 (zeroS\u02e1 L b L x) (zeroS\u02e1 L b\u2081 L x\u2081)\n zeroS\u02e1 L (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n zerolHelp L x x\u2082 (zeroS\u02e1 L b c x) (zeroS\u02e1 L b\u2081 c x\u2082) ,\n zerolHelp L x\u2081 x\u2083 (zeroS\u02e1 L b c\u2081 x\u2081) (zeroS\u02e1 L b\u2081 c\u2081 x\u2083)\n zeroS\u02e1 (B a a\u2081) L L (One x) =\n zeroS\u02e1 a L L (One x) ,\n zeroS\u02e1 a\u2081 L L (One x)\n zeroS\u02e1 (B a a\u2081) L (B c c\u2081) (Row x x\u2081) =\n zeroS\u02e1 a L c x ,\n zeroS\u02e1 a L c\u2081 x\u2081 ,\n zeroS\u02e1 a\u2081 L c x ,\n zeroS\u02e1 a\u2081 L c\u2081 x\u2081\n zeroS\u02e1 (B a a\u2081) (B b b\u2081) L (Col x x\u2081) =\n zerolHelp a x x\u2081 (zeroS\u02e1 a b L x) (zeroS\u02e1 a b\u2081 L x\u2081) ,\n zerolHelp a\u2081 x x\u2081 (zeroS\u02e1 a\u2081 b L x) (zeroS\u02e1 a\u2081 b\u2081 L x\u2081)\n zeroS\u02e1 (B a a\u2081) (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n zerolHelp a x x\u2082 (zeroS\u02e1 a b c x) (zeroS\u02e1 a b\u2081 c x\u2082) ,\n zerolHelp a x\u2081 x\u2083 (zeroS\u02e1 a b c\u2081 x\u2081) (zeroS\u02e1 a b\u2081 c\u2081 x\u2083) ,\n zerolHelp a\u2081 x x\u2082 (zeroS\u02e1 a\u2081 b c x) (zeroS\u02e1 a\u2081 b\u2081 c x\u2082) ,\n zerolHelp a\u2081 x\u2081 x\u2083 (zeroS\u02e1 a\u2081 b c\u2081 x\u2081) (zeroS\u02e1 a\u2081 b\u2081 c\u2081 x\u2083)\n\n zerorHelp : \u2200 r {m m' c}\n (x : M s r m)\n (x\u2081 : M s r m') \u2192\n x \u2219S 0S m c \u2243S 0S r c \u2192\n x\u2081 \u2219S 0S m' c \u2243S 0S r c \u2192\n x \u2219S 0S m c +S x\u2081 \u2219S 0S m' c\n \u2243S 0S r c\n zerorHelp r {m} {m'} {c} x x\u2081 p q =\n let open EqReasoning setoidS\n in begin\n x \u2219S 0S m c +S x\u2081 \u2219S 0S m' c\n \u2248\u27e8 <+S> _ _ {x \u2219S 0S m c} {0S r c} {x\u2081 \u2219S 0S m' c} {0S r c} p q \u27e9\n 0S r c +S 0S r c\n \u2248\u27e8 identS\u02e1 r c (0S r c) \u27e9\n 0S r c\n \u220e\n\n zeroS\u02b3 : (a b c : Shape) (x : M s a b) \u2192\n (x \u2219S 0S b c) \u2243S 0S a c\n zeroS\u02b3 L L L (One x) = zero\u02b3 x\n zeroS\u02b3 L L (B c c\u2081) (One x) =\n (zeroS\u02b3 L L c (One x)) ,\n (zeroS\u02b3 L L c\u2081 (One x))\n zeroS\u02b3 L (B b b\u2081) L (Row x x\u2081) =\n zerorHelp L {c = L} x x\u2081 (zeroS\u02b3 L b L x) (zeroS\u02b3 L b\u2081 L x\u2081)\n zeroS\u02b3 L (B b b\u2081) (B c c\u2081) (Row x x\u2081) =\n zerorHelp L {c = c} x x\u2081 (zeroS\u02b3 L b c x) (zeroS\u02b3 L b\u2081 c x\u2081) ,\n zerorHelp L {c = c\u2081} x x\u2081 (zeroS\u02b3 L b c\u2081 x) (zeroS\u02b3 L b\u2081 c\u2081 x\u2081)\n zeroS\u02b3 (B a a\u2081) L L (Col x x\u2081) =\n zeroS\u02b3 a L L x ,\n zeroS\u02b3 a\u2081 L L x\u2081\n zeroS\u02b3 (B a a\u2081) L (B c c\u2081) (Col x x\u2081) =\n zeroS\u02b3 a L c x ,\n zeroS\u02b3 a L c\u2081 x ,\n zeroS\u02b3 a\u2081 L c x\u2081 ,\n zeroS\u02b3 a\u2081 L c\u2081 x\u2081\n zeroS\u02b3 (B a a\u2081) (B b b\u2081) L (Q x x\u2081 x\u2082 x\u2083) =\n zerorHelp a x x\u2081 (zeroS\u02b3 a b L x) (zeroS\u02b3 a b\u2081 L x\u2081) ,\n zerorHelp a\u2081 x\u2082 x\u2083 (zeroS\u02b3 a\u2081 b L x\u2082) (zeroS\u02b3 a\u2081 b\u2081 L x\u2083)\n zeroS\u02b3 (B a a\u2081) (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n zerorHelp a x x\u2081 (zeroS\u02b3 a b c x) (zeroS\u02b3 a b\u2081 c x\u2081) ,\n zerorHelp a x x\u2081 (zeroS\u02b3 a b c\u2081 x) (zeroS\u02b3 a b\u2081 c\u2081 x\u2081) ,\n zerorHelp a\u2081 x\u2082 x\u2083 (zeroS\u02b3 a\u2081 b c x\u2082) (zeroS\u02b3 a\u2081 b\u2081 c x\u2083) ,\n zerorHelp a\u2081 x\u2082 x\u2083 (zeroS\u02b3 a\u2081 b c\u2081 x\u2082) (zeroS\u02b3 a\u2081 b\u2081 c\u2081 x\u2083)\n\n <\u2219S> : (a b c : Shape) {x y : M s a b} {u v : M s b c} \u2192\n x \u2243S y \u2192 u \u2243S v \u2192 (x \u2219S u) \u2243S (y \u2219S v)\n <\u2219S> L L L {One x} {One x\u2081} {One x\u2082} {One x\u2083} p q = p <\u2219> q\n <\u2219S> L L (B c c\u2081) {One x} {One x\u2081} {Row u u\u2081} {Row v v\u2081} p (q , q\u2081) =\n (<\u2219S> L L c {One x} {One x\u2081} {u} {v} p q) ,\n <\u2219S> L L c\u2081 {One x} {One x\u2081} {u\u2081} {v\u2081} p q\u2081\n <\u2219S> L (B b b\u2081) L {Row x x\u2081} {Row y y\u2081} {Col u u\u2081} {Col v v\u2081} (p , p\u2081) (q , q\u2081) =\n -- Row x x\u2081 \u2219S Col u u\u2081 \u2243S Row y y\u2081 \u2219S Col v v\u2081\n let\n open EqReasoning setoidS\n ih = <\u2219S> _ _ _ {x} {y} {u} {v} p q\n ih\u2081 = <\u2219S> _ _ _ {x\u2081} {y\u2081} {u\u2081} {v\u2081} p\u2081 q\u2081\n in begin\n Row x x\u2081 \u2219S Col u u\u2081\n \u2261\u27e8 refl-\u2261 \u27e9\n x \u2219S u +S x\u2081 \u2219S u\u2081\n \u2248\u27e8 <+S> L L {x \u2219S u} {y \u2219S v} {x\u2081 \u2219S u\u2081} {y\u2081 \u2219S v\u2081} ih ih\u2081 \u27e9\n y \u2219S v +S y\u2081 \u2219S v\u2081\n \u220e\n <\u2219S> L (B b b\u2081) (B c c\u2081) {Row x x\u2081} {Row y y\u2081} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083} (p , p\u2081) (q , q\u2081 , q\u2082 , q\u2083) =\n (let\n ih = <\u2219S> L b c p q\n ih\u2081 = <\u2219S> L b\u2081 c p\u2081 q\u2082\n in <+S> L c ih ih\u2081) ,\n <+S> L c\u2081 (<\u2219S> L b c\u2081 p q\u2081) (<\u2219S> L b\u2081 c\u2081 p\u2081 q\u2083)\n <\u2219S> (B a a\u2081) L L {Col x x\u2081} {Col y y\u2081} {One x\u2082} {One x\u2083} (p , p\u2081) q =\n <\u2219S> a L L p q ,\n <\u2219S> a\u2081 L L p\u2081 q\n <\u2219S> (B a a\u2081) L (B c c\u2081) {Col x x\u2081} {Col y y\u2081} {Row u u\u2081} {Row v v\u2081} (p , p\u2081) (q , q\u2081) =\n <\u2219S> a L c p q ,\n <\u2219S> a L c\u2081 p q\u2081 ,\n <\u2219S> a\u2081 L c p\u2081 q ,\n <\u2219S> a\u2081 L c\u2081 p\u2081 q\u2081\n <\u2219S> (B a a\u2081) (B b b\u2081) L {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Col u u\u2081} {Col v v\u2081} (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081) =\n <+S> a L (<\u2219S> a b L p q) (<\u2219S> a b\u2081 L p\u2081 q\u2081) ,\n <+S> a\u2081 L (<\u2219S> a\u2081 b L p\u2082 q) (<\u2219S> a\u2081 b\u2081 L p\u2083 q\u2081 )\n <\u2219S> (B a a\u2081) (B b b\u2081) (B c c\u2081) {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083} (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081 , q\u2082 , q\u2083) =\n <+S> a c (<\u2219S> a b c p q) (<\u2219S> a b\u2081 c p\u2081 q\u2082) ,\n <+S> a c\u2081 (<\u2219S> a b c\u2081 p q\u2081) (<\u2219S> a b\u2081 c\u2081 p\u2081 q\u2083) ,\n <+S> a\u2081 c (<\u2219S> a\u2081 b c p\u2082 q) (<\u2219S> a\u2081 b\u2081 c p\u2083 q\u2082) ,\n <+S> a\u2081 c\u2081 (<\u2219S> a\u2081 b c\u2081 p\u2082 q\u2081) (<\u2219S> a\u2081 b\u2081 c\u2081 p\u2083 q\u2083)\n\n idemS : (r c : Shape) (x : M s r c) \u2192 x +S x \u2243S x\n idemS L L (One x) = idem x\n idemS L (B c c\u2081) (Row x x\u2081) = (idemS _ _ x) , (idemS _ _ x\u2081)\n idemS (B r r\u2081) L (Col x x\u2081) = (idemS _ _ x) , (idemS _ _ x\u2081)\n idemS (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (idemS _ _ x) , (idemS _ _ x\u2081 , (idemS _ _ x\u2082 , idemS _ _ x\u2083))\n\n swapMid : {r c : Shape} (x y z w : M s r c) \u2192\n (x +S y) +S (z +S w) \u2243S (x +S z) +S (y +S w)\n swapMid {r} {c} x y z w =\n let open EqReasoning setoidS\n in begin\n (x +S y) +S (z +S w)\n \u2248\u27e8 assocS _ _ x y (z +S w) \u27e9\n x +S y +S z +S w\n \u2248\u27e8 <+S> r c (reflS r c) (symS r c (assocS r c y z w)) \u27e9\n x +S (y +S z) +S w\n \u2248\u27e8 <+S> r c (reflS r c) (<+S> r c (commS r c y z) (reflS r c)) \u27e9\n x +S (z +S y) +S w\n \u2248\u27e8 <+S> r c (reflS r c) (assocS r c z y w) \u27e9\n x +S z +S y +S w\n \u2248\u27e8 symS r c (assocS r c x z (y +S w)) \u27e9\n (x +S z) +S (y +S w)\n \u220e\n\n distlHelp : \u2200 {a b b\u2081 c\u2081}\n (x : M s a b)\n (y z : M s b c\u2081)\n (x\u2081 : M s a b\u2081)\n (y\u2081 z\u2081 : M s b\u2081 c\u2081) \u2192\n (x \u2219S (y +S z)) \u2243S (x \u2219S y +S x \u2219S z) \u2192\n (x\u2081 \u2219S (y\u2081 +S z\u2081)) \u2243S (x\u2081 \u2219S y\u2081 +S x\u2081 \u2219S z\u2081) \u2192\n (x \u2219S (y +S z) +S x\u2081 \u2219S (y\u2081 +S z\u2081))\n \u2243S ((x \u2219S y +S x\u2081 \u2219S y\u2081) +S x \u2219S z +S x\u2081 \u2219S z\u2081)\n distlHelp x y z x\u2081 y\u2081 z\u2081 p q =\n let open EqReasoning setoidS\n in begin\n x \u2219S (y +S z) +S x\u2081 \u2219S (y\u2081 +S z\u2081)\n \u2248\u27e8 <+S> _ _ {x \u2219S (y +S z)} {x \u2219S y +S x \u2219S z}\n {x\u2081 \u2219S (y\u2081 +S z\u2081)} {x\u2081 \u2219S y\u2081 +S x\u2081 \u2219S z\u2081} p q \u27e9\n (x \u2219S y +S x \u2219S z) +S x\u2081 \u2219S y\u2081 +S x\u2081 \u2219S z\u2081\n \u2248\u27e8 swapMid (x \u2219S y) (x \u2219S z) (x\u2081 \u2219S y\u2081) (x\u2081 \u2219S z\u2081) \u27e9\n (x \u2219S y +S x\u2081 \u2219S y\u2081) +S x \u2219S z +S x\u2081 \u2219S z\u2081\n \u220e\n\n distlS : {a b c : Shape} (x : M s a b) (y z : M s b c) \u2192\n (x \u2219S (y +S z)) \u2243S ((x \u2219S y) +S (x \u2219S z))\n distlS {L} {L} {L} (One x) (One y) (One z) = distl x y z\n distlS {L} {L} {B c c\u2081} (One x) (Row y y\u2081) (Row z z\u2081) =\n distlS (One x) y z ,\n distlS (One x) y\u2081 z\u2081\n distlS {L} {(B b b\u2081)} {L} (Row x x\u2081) (Col y y\u2081) (Col z z\u2081) =\n distlHelp x y z x\u2081 y\u2081 z\u2081 (distlS x y z) (distlS x\u2081 y\u2081 z\u2081)\n distlS {L} {(B b b\u2081)} {(B c c\u2081)} (Row x x\u2081) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n distlHelp x y z x\u2081 y\u2082 z\u2082 (distlS x y z) (distlS x\u2081 y\u2082 z\u2082) ,\n distlHelp x y\u2081 z\u2081 x\u2081 y\u2083 z\u2083 (distlS x y\u2081 z\u2081) (distlS x\u2081 y\u2083 z\u2083)\n distlS {(B a a\u2081)} {L} {L} (Col x x\u2081) (One x\u2082) (One x\u2083) =\n distlS x (One x\u2082) (One x\u2083) ,\n distlS x\u2081 (One x\u2082) (One x\u2083)\n distlS {(B a a\u2081)} {L} {(B c c\u2081)} (Col x x\u2081) (Row y y\u2081) (Row z z\u2081) =\n distlS x y z ,\n distlS x y\u2081 z\u2081 ,\n distlS x\u2081 y z ,\n distlS x\u2081 y\u2081 z\u2081\n distlS {(B a a\u2081)} {(B b b\u2081)} {L} (Q x x\u2081 x\u2082 x\u2083) (Col y y\u2081) (Col z z\u2081) =\n distlHelp x y z x\u2081 y\u2081 z\u2081 (distlS x y z) (distlS x\u2081 y\u2081 z\u2081) ,\n distlHelp x\u2082 y z x\u2083 y\u2081 z\u2081 (distlS x\u2082 y z) (distlS x\u2083 y\u2081 z\u2081)\n distlS {(B a a\u2081)} {(B b b\u2081)} {(B c c\u2081)} (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n distlHelp x y z x\u2081 y\u2082 z\u2082 (distlS x y z) (distlS x\u2081 y\u2082 z\u2082) ,\n distlHelp x y\u2081 z\u2081 x\u2081 y\u2083 z\u2083 (distlS x y\u2081 z\u2081) (distlS x\u2081 y\u2083 z\u2083) ,\n distlHelp x\u2082 y z x\u2083 y\u2082 z\u2082 (distlS x\u2082 y z) (distlS x\u2083 y\u2082 z\u2082) ,\n distlHelp x\u2082 y\u2081 z\u2081 x\u2083 y\u2083 z\u2083 (distlS x\u2082 y\u2081 z\u2081) (distlS x\u2083 y\u2083 z\u2083)\n\n distrHelp : \u2200 {r m m\u2081 c : Shape}\n (x : M s m c)\n (y z : M s r m)\n (x\u2081 : M s m\u2081 c)\n (y\u2081 z\u2081 : M s r m\u2081) \u2192\n ((y +S z) \u2219S x) \u2243S (y \u2219S x +S z \u2219S x) \u2192\n ((y\u2081 +S z\u2081) \u2219S x\u2081) \u2243S (y\u2081 \u2219S x\u2081 +S z\u2081 \u2219S x\u2081) \u2192\n ((y +S z) \u2219S x +S (y\u2081 +S z\u2081) \u2219S x\u2081)\n \u2243S ((y \u2219S x +S y\u2081 \u2219S x\u2081) +S z \u2219S x +S z\u2081 \u2219S x\u2081)\n distrHelp x y z x\u2081 y\u2081 z\u2081 p q =\n let open EqReasoning setoidS\n in begin\n (y +S z) \u2219S x +S (y\u2081 +S z\u2081) \u2219S x\u2081\n \u2248\u27e8 <+S> _ _ {(y +S z) \u2219S x} {y \u2219S x +S z \u2219S x}\n {(y\u2081 +S z\u2081) \u2219S x\u2081} {y\u2081 \u2219S x\u2081 +S z\u2081 \u2219S x\u2081} p q \u27e9\n (y \u2219S x +S z \u2219S x) +S y\u2081 \u2219S x\u2081 +S z\u2081 \u2219S x\u2081\n \u2248\u27e8 swapMid (y \u2219S x) (z \u2219S x) (y\u2081 \u2219S x\u2081) (z\u2081 \u2219S x\u2081) \u27e9\n (y \u2219S x +S y\u2081 \u2219S x\u2081) +S z \u2219S x +S z\u2081 \u2219S x\u2081\n \u220e\n\n distrS : {r m c : Shape} (x : M s m c) (y z : M s r m) \u2192\n ((y +S z) \u2219S x) \u2243S ((y \u2219S x) +S (z \u2219S x))\n distrS {L} {L} {L} (One x) (One y) (One z) =\n distr x y z\n distrS {L} {L} {B c c\u2081} (Row x x\u2081) (One x\u2082) (One x\u2083) =\n (distrS x (One x\u2082) (One x\u2083)) ,\n (distrS x\u2081 (One x\u2082) (One x\u2083))\n distrS {L} {B m m\u2081} {L} (Col x x\u2081) (Row y y\u2081) (Row z z\u2081) =\n distrHelp x y z x\u2081 y\u2081 z\u2081 (distrS x y z) (distrS x\u2081 y\u2081 z\u2081)\n distrS {L} {B m m\u2081} {B c c\u2081} (Q x x\u2081 x\u2082 x\u2083) (Row y y\u2081) (Row z z\u2081) =\n (distrHelp x y z x\u2082 y\u2081 z\u2081 (distrS x y z) (distrS x\u2082 y\u2081 z\u2081)) ,\n (distrHelp x\u2081 y z x\u2083 y\u2081 z\u2081 (distrS x\u2081 y z) (distrS x\u2083 y\u2081 z\u2081))\n distrS {B r r\u2081} {L} {L} (One x) (Col y y\u2081) (Col z z\u2081) =\n distrS (One x) y z ,\n distrS (One x) y\u2081 z\u2081\n distrS {B r r\u2081} {L} {B c c\u2081} (Row x x\u2081) (Col y y\u2081) (Col z z\u2081) =\n (distrS x y z) ,\n (distrS x\u2081 y z) ,\n (distrS x y\u2081 z\u2081) ,\n (distrS x\u2081 y\u2081 z\u2081)\n distrS {B r r\u2081} {B m m\u2081} {L} (Col x x\u2081) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n (distrHelp x y z x\u2081 y\u2081 z\u2081 (distrS x y z) (distrS x\u2081 y\u2081 z\u2081)) ,\n (distrHelp x y\u2082 z\u2082 x\u2081 y\u2083 z\u2083 (distrS x y\u2082 z\u2082) (distrS x\u2081 y\u2083 z\u2083))\n distrS {B r r\u2081} {B m m\u2081} {B c c\u2081} (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n distrHelp x y z x\u2082 y\u2081 z\u2081 (distrS x y z) (distrS x\u2082 y\u2081 z\u2081) ,\n distrHelp x\u2081 y z x\u2083 y\u2081 z\u2081 (distrS x\u2081 y z) (distrS x\u2083 y\u2081 z\u2081) ,\n distrHelp x y\u2082 z\u2082 x\u2082 y\u2083 z\u2083 (distrS x y\u2082 z\u2082) (distrS x\u2082 y\u2083 z\u2083) ,\n distrHelp x\u2081 y\u2082 z\u2082 x\u2083 y\u2083 z\u2083 (distrS x\u2081 y\u2082 z\u2082) (distrS x\u2083 y\u2083 z\u2083)\n\n\n SNR : SemiNearRing\n SNR =\n record\n { s = S\n ; _\u2243\u209b_ = _\u2243S_ {shape} {shape}\n ; 0\u209b = 0S shape shape\n ; _+\u209b_ = _+S_\n ; _\u2219\u209b_ = _\u2219S_\n ; isCommMon = isCommMonS\n ; zero\u02e1 = zeroS\u02e1 shape shape shape\n ; zero\u02b3 = zeroS\u02b3 shape shape shape\n ; _<\u2219>_ = <\u2219S> shape shape shape\n ; idem = idemS shape shape\n ; distl = distlS {shape} {shape}\n ; distr = distrS {shape} {shape}\n }\n","old_contents":"-- Matrices indexed by shape\nmodule SquareMatrix where\n\nopen import Shape\nopen import Matrix\n\n-- Square matrices have the same shape in both dimensions\nSq : Set \u2192 Shape \u2192 Set\nSq a shape = M a shape shape\n\nopen import SemiNearRingRecord\nopen import Algebra.Structures\nopen import Relation.Binary\nopen import Data.Product hiding (swap)\n\nopen import Relation.Binary.PropositionalEquality hiding (trans; sym) renaming (refl to refl-\u2261)\nimport Relation.Binary.EqReasoning as EqReasoning\n\n\n-- Lifting a SNR to a to a Square matrix of some shape\nSquare : SemiNearRing \u2192 Shape \u2192 SemiNearRing\nSquare snr shape = SNR\n where\n open SemiNearRing snr\n\n S = Sq s shape\n\n infixr 60 _\u2219S_\n infixr 50 _+S_\n\n\n _+S_ : \u2200 {r c} \u2192 M s r c \u2192 M s r c \u2192 M s r c\n One x +S One x\u2081 = One (x +\u209b x\u2081)\n Row m m\u2081 +S Row n n\u2081 = Row (m +S n) (m\u2081 +S n\u2081)\n Col m m\u2081 +S Col n n\u2081 = Col (m +S n) (m\u2081 +S n\u2081)\n Q m00 m01\n m10 m11 +S Q n00 n01\n n10 n11 = Q (m00 +S n00) (m01 +S n01)\n (m10 +S n10) (m11 +S n11)\n\n _\u2219S_ : \u2200 {r m c} \u2192 M s r m \u2192 M s m c \u2192 M s r c\n One x \u2219S One x\u2081 = One (x \u2219\u209b x\u2081)\n One x \u2219S Row n n\u2081 = Row (One x \u2219S n) (One x \u2219S n\u2081)\n Row m m\u2081 \u2219S Col n n\u2081 = m \u2219S n +S m\u2081 \u2219S n\u2081\n Row m m\u2081 \u2219S Q n00 n01\n n10 n11 = Row (m \u2219S n00 +S m\u2081 \u2219S n10) (m \u2219S n01 +S m\u2081 \u2219S n11)\n Col m m\u2081 \u2219S One x = Col (m \u2219S One x) (m\u2081 \u2219S One x)\n Col m m\u2081 \u2219S Row n n\u2081 = Q (m \u2219S n) (m \u2219S n\u2081)\n (m\u2081 \u2219S n) (m\u2081 \u2219S n\u2081)\n Q m00 m01\n m10 m11 \u2219S Col n n\u2081 = Col (m00 \u2219S n +S m01 \u2219S n\u2081) (m10 \u2219S n +S m11 \u2219S n\u2081)\n Q m00 m01\n m10 m11 \u2219S Q n00 n01\n n10 n11 = Q (m00 \u2219S n00 +S m01 \u2219S n10) (m00 \u2219S n01 +S m01 \u2219S n11)\n (m10 \u2219S n00 +S m11 \u2219S n10) (m10 \u2219S n01 +S m11 \u2219S n11)\n\n 0S : (r c : Shape) \u2192 M s r c\n 0S L L = One 0\u209b\n 0S L (B s s\u2081) = Row (0S L s) (0S L s\u2081)\n 0S (B r r\u2081) L = Col (0S r L) (0S r\u2081 L)\n 0S (B r r\u2081) (B s s\u2081) =\n Q (0S r s) (0S r s\u2081)\n (0S r\u2081 s) (0S r\u2081 s\u2081)\n\n _\u2243S_ : {r c : Shape} \u2192\n M s r c \u2192 M s r c \u2192 Set\n _\u2243S_ {L} {L} (One x) (One x\u2081) = x \u2243\u209b x\u2081\n _\u2243S_ {L} {(B c\u2081 c\u2082)} (Row m m\u2081) (Row n n\u2081) =\n _\u2243S_ m n \u00d7 _\u2243S_ m\u2081 n\u2081\n _\u2243S_ {(B r\u2081 r\u2082)} {L} (Col m m\u2081) (Col n n\u2081) =\n _\u2243S_ m n \u00d7 _\u2243S_ m\u2081 n\u2081\n _\u2243S_ {(B r\u2081 r\u2082)} {(B c\u2081 c\u2082)} (Q m00 m01 m10 m11) (Q n00 n01 n10 n11) =\n _\u2243S_ m00 n00 \u00d7\n _\u2243S_ m01 n01 \u00d7\n _\u2243S_ m10 n10 \u00d7\n _\u2243S_ m11 n11\n\n\n reflS : (r c : Shape) \u2192\n {X : M s r c} \u2192 X \u2243S X\n reflS L L {X = One x} = refl\u209b {x}\n reflS L (B c\u2081 c\u2082) {X = Row X Y} = reflS L c\u2081 , reflS L c\u2082\n reflS (B r\u2081 r\u2082) L {X = Col X Y} = reflS r\u2081 L , reflS r\u2082 L\n reflS (B r\u2081 r\u2082) (B c\u2081 c\u2082) {X = Q X Y Z W} =\n reflS r\u2081 c\u2081 , reflS r\u2081 c\u2082 ,\n reflS r\u2082 c\u2081 , reflS r\u2082 c\u2082\n\n symS : (r c : Shape) \u2192\n {i j : M s r c} \u2192 i \u2243S j \u2192 j \u2243S i\n symS L L {One x} {One x\u2081} p = sym\u209b p\n symS L (B c\u2081 c\u2082) {Row i\u2081 i\u2082} {Row j\u2081 j\u2082} (p , q) = symS L c\u2081 p , symS L c\u2082 q\n symS (B r\u2081 r\u2082) L {Col i\u2081 i} {Col j j\u2081} (p , q) = symS r\u2081 L p , symS r\u2082 L q\n symS (B r r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j j\u2081 j\u2082 j\u2083} (p , q , x , y) =\n symS r c\u2081 p , symS r c\u2082 q ,\n symS r\u2082 c\u2081 x , symS r\u2082 c\u2082 y\n\n transS : (r c : Shape) \u2192\n {i j k : M s r c} \u2192 i \u2243S j \u2192 j \u2243S k \u2192 i \u2243S k\n transS L L {One x} {One x\u2081} {One x\u2082} p q = trans p q\n transS L (B c\u2081 c\u2082) {Row i i\u2081} {Row j j\u2081} {Row k k\u2081} (p , q) (p' , q') =\n transS L c\u2081 p p' , transS L c\u2082 q q'\n transS (B r\u2081 r\u2082) L {Col i i\u2081} {Col j j\u2081} {Col k k\u2081} (p , q) (p' , q') =\n transS r\u2081 L p p' , transS r\u2082 L q q'\n transS (B r\u2081 r\u2082) (B c\u2081 c\u2082) {Q i\u2082 i i\u2083 i\u2081} {Q j\u2082 j j\u2083 j\u2081} {Q k k\u2081 k\u2082 k\u2083}\n (p , q , x , y) (p' , q' , x' , y') =\n transS r\u2081 c\u2081 p p' , transS r\u2081 c\u2082 q q' ,\n transS r\u2082 c\u2081 x x' , transS r\u2082 c\u2082 y y'\n\n isEquivS =\n record\n { refl = reflS shape shape\n ; sym = symS shape shape\n ; trans = transS shape shape }\n\n assocS : (r c : Shape) (x y z : M s r c) \u2192 ((x +S y) +S z) \u2243S (x +S (y +S z))\n assocS L L (One x) (One y) (One z) = assoc\u209b x y z\n assocS L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) (Row z z\u2081) =\n assocS L c x y z , assocS L c\u2081 x\u2081 y\u2081 z\u2081\n assocS (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) (Col z z\u2081) =\n assocS r L x y z , assocS r\u2081 L x\u2081 y\u2081 z\u2081\n assocS (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n (assocS r c x y z) , (assocS r c\u2081 x\u2081 y\u2081 z\u2081) ,\n (assocS r\u2081 c x\u2082 y\u2082 z\u2082) , (assocS r\u2081 c\u2081 x\u2083 y\u2083 z\u2083)\n\n <+S> : (r c : Shape) {x y u v : M s r c} \u2192\n x \u2243S y \u2192 u \u2243S v \u2192 (x +S u) \u2243S (y +S v)\n <+S> L L {One x} {One x\u2081} {One x\u2082} {One x\u2083} p q = p <+> q\n <+S> L (B c c\u2081) {Row x x\u2081} {Row y y\u2081} {Row u u\u2081} {Row v v\u2081} (p , p\u2081) (q , q\u2081) =\n <+S> L c p q , <+S> L c\u2081 p\u2081 q\u2081\n <+S> (B r r\u2081) L {Col x x\u2081} {Col y y\u2081} {Col u u\u2081} {Col v v\u2081} (p , p\u2081) (q , q\u2081) =\n <+S> r L p q , <+S> r\u2081 L p\u2081 q\u2081\n <+S> (B r r\u2081) (B c c\u2081) {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083}\n (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081 , q\u2082 , q\u2083) =\n <+S> r c p q , <+S> r c\u2081 p\u2081 q\u2081 ,\n <+S> r\u2081 c p\u2082 q\u2082 , <+S> r\u2081 c\u2081 p\u2083 q\u2083\n\n isSemgroupS =\n record\n { isEquivalence = isEquivS\n ; assoc = assocS shape shape\n ; \u2219-cong = <+S> shape shape }\n\n\n identS\u02e1 : (r c : Shape) (x : M s r c) \u2192\n 0S r c +S x \u2243S x\n identS\u02e1 L L (One x) = identity\u02e1\u209b x\n identS\u02e1 L (B c c\u2081) (Row x x\u2081) = identS\u02e1 L c x , identS\u02e1 L c\u2081 x\u2081\n identS\u02e1 (B r r\u2081) L (Col x x\u2081) = identS\u02e1 r L x , identS\u02e1 r\u2081 L x\u2081\n identS\u02e1 (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n identS\u02e1 r c x , identS\u02e1 r c\u2081 x\u2081 ,\n identS\u02e1 r\u2081 c x\u2082 , identS\u02e1 r\u2081 c\u2081 x\u2083\n\n commS : (r c : Shape) \u2192 (x y : M s r c) \u2192\n (x +S y) \u2243S (y +S x)\n commS L L (One x) (One x\u2081) = comm\u209b x x\u2081\n commS L (B c c\u2081) (Row x x\u2081) (Row y y\u2081) = (commS L c x y) , (commS L c\u2081 x\u2081 y\u2081)\n commS (B r r\u2081) L (Col x x\u2081) (Col y y\u2081) = (commS r L x y) , (commS r\u2081 L x\u2081 y\u2081)\n commS (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) =\n commS r c x y , commS r c\u2081 x\u2081 y\u2081 ,\n commS r\u2081 c x\u2082 y\u2082 , commS r\u2081 c\u2081 x\u2083 y\u2083\n\n isCommMonS =\n record\n { isSemigroup = isSemgroupS\n ; identity\u02e1 = identS\u02e1 shape shape\n ; comm = commS shape shape }\n\n\n setoidS : {r c : Shape} \u2192 Setoid _ _\n setoidS {r} {c} =\n record\n { Carrier = M s r c\n ; _\u2248_ = _\u2243S_ {r} {c}\n ; isEquivalence =\n record\n { refl = reflS r c ; sym = symS r c ; trans = transS r c } }\n\n\n zeroS\u02e1 : (a b c : Shape) (x : M s b c) \u2192\n (0S a b \u2219S x) \u2243S 0S a c\n zeroS\u02e1 L L L (One x) = zero\u02e1 x\n zeroS\u02e1 L L (B c c\u2081) (Row x x\u2081) = (zeroS\u02e1 L L c x) , (zeroS\u02e1 L L c\u2081 x\u2081)\n zeroS\u02e1 L (B b b\u2081) L (Col x x\u2081) =\n let\n open EqReasoning setoidS\n ih = zeroS\u02e1 L b L x\n ih\u2081 = zeroS\u02e1 L b\u2081 L x\u2081\n in begin\n (Row (0S L b) (0S L b\u2081) \u2219S (Col x x\u2081))\n \u2261\u27e8 refl-\u2261 \u27e9\n (0S L b) \u2219S x +S (0S L b\u2081) \u2219S x\u2081\n \u2248\u27e8 <+S> L L {0S L b \u2219S x} {0S L L} {0S L b\u2081 \u2219S x\u2081} {0S L L} ih ih\u2081 \u27e9\n 0S L L +S 0S L L\n \u2248\u27e8 identS\u02e1 L L (0S L L) \u27e9\n 0S L L\n \u220e\n zeroS\u02e1 L (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (let\n open EqReasoning setoidS\n ih = zeroS\u02e1 L b c x\n ih\u2081 = zeroS\u02e1 L b\u2081 c x\u2082\n in begin\n 0S L b \u2219S x +S 0S L b\u2081 \u2219S x\u2082\n \u2248\u27e8 <+S> L c ih ih\u2081 \u27e9\n 0S L c +S 0S L c\n \u2248\u27e8 identS\u02e1 L c (0S L c) \u27e9\n 0S L c\n \u220e) ,\n (let\n open EqReasoning setoidS\n ih = zeroS\u02e1 L b c\u2081 x\u2081\n ih\u2081 = zeroS\u02e1 L b\u2081 c\u2081 x\u2083\n in begin\n 0S L b \u2219S x\u2081 +S 0S L b\u2081 \u2219S x\u2083\n \u2248\u27e8 <+S> L c\u2081 ih ih\u2081 \u27e9\n 0S L c\u2081 +S 0S L c\u2081\n \u2248\u27e8 identS\u02e1 L c\u2081 _ \u27e9\n 0S L c\u2081\n \u220e)\n zeroS\u02e1 (B a a\u2081) L L (One x) = zeroS\u02e1 a L L (One x) , zeroS\u02e1 a\u2081 L L (One x)\n zeroS\u02e1 (B a a\u2081) L (B c c\u2081) (Row x x\u2081) =\n zeroS\u02e1 a L c x , zeroS\u02e1 a L c\u2081 x\u2081 ,\n zeroS\u02e1 a\u2081 L c x , zeroS\u02e1 a\u2081 L c\u2081 x\u2081\n zeroS\u02e1 (B a a\u2081) (B b b\u2081) L (Col x x\u2081) =\n (let\n open EqReasoning setoidS\n ih = zeroS\u02e1 a b L x\n ih\u2081 = zeroS\u02e1 a b\u2081 L x\u2081\n in begin\n 0S a b \u2219S x +S 0S a b\u2081 \u2219S x\u2081\n \u2248\u27e8 <+S> a L ih ih\u2081 \u27e9\n 0S a L +S 0S a L\n \u2248\u27e8 identS\u02e1 a L _ \u27e9\n 0S a L\n \u220e) ,\n (let\n open EqReasoning setoidS\n ih = zeroS\u02e1 a\u2081 b L x\n ih\u2081 = zeroS\u02e1 a\u2081 b\u2081 L x\u2081\n in begin\n 0S a\u2081 b \u2219S x +S 0S a\u2081 b\u2081 \u2219S x\u2081\n \u2248\u27e8 <+S> a\u2081 L ih ih\u2081 \u27e9\n 0S a\u2081 L +S 0S a\u2081 L\n \u2248\u27e8 identS\u02e1 a\u2081 L _ \u27e9\n 0S a\u2081 L\n \u220e)\n zeroS\u02e1 (B a a\u2081) (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (let\n open EqReasoning setoidS\n ih = zeroS\u02e1 a b c x\n ih\u2081 = zeroS\u02e1 a b\u2081 c x\u2082\n in begin\n 0S a b \u2219S x +S 0S a b\u2081 \u2219S x\u2082\n \u2248\u27e8 <+S> a c ih ih\u2081 \u27e9\n 0S a c +S 0S a c\n \u2248\u27e8 identS\u02e1 a c _ \u27e9\n 0S a c\n \u220e) ,\n (let\n open EqReasoning setoidS\n ih = zeroS\u02e1 a b c\u2081 x\u2081\n ih\u2081 = zeroS\u02e1 a b\u2081 c\u2081 x\u2083\n in begin\n 0S a b \u2219S x\u2081 +S 0S a b\u2081 \u2219S x\u2083\n \u2248\u27e8 <+S> a c\u2081 ih ih\u2081 \u27e9\n 0S a c\u2081 +S 0S a c\u2081\n \u2248\u27e8 identS\u02e1 a c\u2081 _ \u27e9\n 0S a c\u2081\n \u220e) ,\n (let\n open EqReasoning setoidS\n ih = zeroS\u02e1 a\u2081 b c x\n ih\u2081 = zeroS\u02e1 a\u2081 b\u2081 c x\u2082\n in begin\n 0S a\u2081 b \u2219S x +S 0S a\u2081 b\u2081 \u2219S x\u2082\n \u2248\u27e8 <+S> a\u2081 c ih ih\u2081 \u27e9\n 0S a\u2081 c +S 0S a\u2081 c\n \u2248\u27e8 identS\u02e1 a\u2081 c _ \u27e9\n 0S a\u2081 c\n \u220e) ,\n (let\n open EqReasoning setoidS\n ih = zeroS\u02e1 a\u2081 b c\u2081 x\u2081\n ih\u2081 = zeroS\u02e1 a\u2081 b\u2081 c\u2081 x\u2083\n in begin\n 0S a\u2081 b \u2219S x\u2081 +S 0S a\u2081 b\u2081 \u2219S x\u2083\n \u2248\u27e8 <+S> a\u2081 c\u2081 ih ih\u2081 \u27e9\n 0S a\u2081 c\u2081 +S 0S a\u2081 c\u2081\n \u2248\u27e8 identS\u02e1 a\u2081 c\u2081 _ \u27e9\n 0S a\u2081 c\u2081\n \u220e)\n\n zeroS\u02b3 : (a b c : Shape) (x : M s a b) \u2192\n (x \u2219S 0S b c) \u2243S 0S a c\n zeroS\u02b3 L L L (One x) = zero\u02b3 x\n zeroS\u02b3 L L (B c c\u2081) (One x) =\n (zeroS\u02b3 L L c (One x)) , (zeroS\u02b3 L L c\u2081 (One x))\n zeroS\u02b3 L (B b b\u2081) L (Row x x\u2081) =\n let\n open EqReasoning setoidS\n ih = zeroS\u02b3 L b L x\n ih\u2081 = zeroS\u02b3 L b\u2081 L x\u2081\n in begin\n Row x x\u2081 \u2219S Col (0S b L) (0S b\u2081 L)\n \u2261\u27e8 refl-\u2261 \u27e9\n (x \u2219S 0S b L) +S (x\u2081 \u2219S 0S b\u2081 L)\n \u2248\u27e8 <+S> L L {x \u2219S 0S b L} {0S L L} {x\u2081 \u2219S 0S b\u2081 L} {0S L L} ih ih\u2081 \u27e9\n 0S L L +S 0S L L\n \u2248\u27e8 identS\u02e1 L L (0S L L) \u27e9\n 0S L L\n \u220e\n zeroS\u02b3 L (B b b\u2081) (B c c\u2081) (Row x x\u2081) =\n (let\n open EqReasoning setoidS\n ih = zeroS\u02b3 L b c x\n ih\u2081 = zeroS\u02b3 L b\u2081 c x\u2081\n in begin\n x \u2219S 0S b c +S x\u2081 \u2219S 0S b\u2081 c\n \u2248\u27e8 <+S> L c ih ih\u2081 \u27e9\n 0S L c +S 0S L c\n \u2248\u27e8 identS\u02e1 L c (0S L c) \u27e9\n 0S L c\n \u220e) ,\n (let\n open EqReasoning setoidS\n ih = zeroS\u02b3 L b c\u2081 x\n ih\u2081 = zeroS\u02b3 L b\u2081 c\u2081 x\u2081\n in begin\n x \u2219S 0S b c\u2081 +S x\u2081 \u2219S 0S b\u2081 c\u2081\n \u2248\u27e8 <+S> L c\u2081 ih ih\u2081 \u27e9\n 0S L c\u2081 +S 0S L c\u2081\n \u2248\u27e8 identS\u02e1 L c\u2081 (0S L c\u2081) \u27e9\n 0S L c\u2081\n \u220e)\n zeroS\u02b3 (B a a\u2081) L L (Col x x\u2081) = zeroS\u02b3 a L L x , zeroS\u02b3 a\u2081 L L x\u2081\n zeroS\u02b3 (B a a\u2081) L (B c c\u2081) (Col x x\u2081) =\n zeroS\u02b3 a L c x ,\n zeroS\u02b3 a L c\u2081 x ,\n zeroS\u02b3 a\u2081 L c x\u2081 ,\n zeroS\u02b3 a\u2081 L c\u2081 x\u2081\n zeroS\u02b3 (B a a\u2081) (B b b\u2081) L (Q x x\u2081 x\u2082 x\u2083) =\n (let\n open EqReasoning setoidS\n ih = zeroS\u02b3 a b L x\n ih\u2081 = zeroS\u02b3 a b\u2081 L x\u2081\n in begin\n x \u2219S 0S b L +S x\u2081 \u2219S 0S b\u2081 L\n \u2248\u27e8 <+S> a L ih ih\u2081 \u27e9\n 0S a L +S 0S a L\n \u2248\u27e8 identS\u02e1 a L (0S _ _) \u27e9\n 0S a L\n \u220e) ,\n (let\n open EqReasoning setoidS\n ih = zeroS\u02b3 a\u2081 b L x\u2082\n ih\u2081 = zeroS\u02b3 a\u2081 b\u2081 L x\u2083\n in begin\n x\u2082 \u2219S 0S b L +S x\u2083 \u2219S 0S b\u2081 L\n \u2248\u27e8 <+S> a\u2081 L ih ih\u2081 \u27e9\n 0S _ _ +S 0S _ _\n \u2248\u27e8 identS\u02e1 a\u2081 L (0S _ _) \u27e9\n 0S a\u2081 L\n \u220e)\n zeroS\u02b3 (B a a\u2081) (B b b\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (let\n open EqReasoning setoidS\n ih = zeroS\u02b3 a b c x\n ih\u2081 = zeroS\u02b3 a b\u2081 c x\u2081\n in begin\n x \u2219S 0S b c +S x\u2081 \u2219S 0S b\u2081 c\n \u2248\u27e8 <+S> a c ih ih\u2081 \u27e9\n 0S _ _ +S 0S _ _\n \u2248\u27e8 identS\u02e1 a c (0S _ _) \u27e9\n 0S _ _\n \u220e) ,\n (let\n ih = zeroS\u02b3 a b c\u2081 x\n ih\u2081 = zeroS\u02b3 a b\u2081 c\u2081 x\u2081\n in transS a c\u2081 (<+S> a c\u2081 ih ih\u2081) (identS\u02e1 a c\u2081 (0S _ _))\n ) ,\n (let\n open EqReasoning setoidS\n ih = zeroS\u02b3 a\u2081 b c x\u2082\n ih\u2081 = zeroS\u02b3 a\u2081 b\u2081 c x\u2083\n in begin\n x\u2082 \u2219S 0S b c +S x\u2083 \u2219S 0S b\u2081 c\n \u2248\u27e8 <+S> a\u2081 c ih ih\u2081 \u27e9\n 0S _ _ +S 0S _ _\n \u2248\u27e8 identS\u02e1 a\u2081 c (0S _ _) \u27e9\n 0S a\u2081 c\n \u220e) ,\n (let\n open EqReasoning setoidS\n ih = zeroS\u02b3 a\u2081 b c\u2081 x\u2082\n ih\u2081 = zeroS\u02b3 a\u2081 b\u2081 c\u2081 x\u2083\n in begin\n x\u2082 \u2219S 0S b c\u2081 +S x\u2083 \u2219S 0S b\u2081 c\u2081\n \u2248\u27e8 <+S> a\u2081 c\u2081 ih ih\u2081 \u27e9\n 0S _ _ +S 0S _ _\n \u2248\u27e8 identS\u02e1 a\u2081 c\u2081 (0S _ _) \u27e9\n 0S a\u2081 c\u2081\n \u220e)\n\n <\u2219S> : (a b c : Shape) {x y : M s a b} {u v : M s b c} \u2192\n x \u2243S y \u2192 u \u2243S v \u2192 (x \u2219S u) \u2243S (y \u2219S v)\n <\u2219S> L L L {One x} {One x\u2081} {One x\u2082} {One x\u2083} p q = p <\u2219> q\n <\u2219S> L L (B c c\u2081) {One x} {One x\u2081} {Row u u\u2081} {Row v v\u2081} p (q , q\u2081) =\n (<\u2219S> L L c {One x} {One x\u2081} {u} {v} p q) ,\n <\u2219S> L L c\u2081 {One x} {One x\u2081} {u\u2081} {v\u2081} p q\u2081\n <\u2219S> L (B b b\u2081) L {Row x x\u2081} {Row y y\u2081} {Col u u\u2081} {Col v v\u2081} (p , p\u2081) (q , q\u2081) =\n -- Row x x\u2081 \u2219S Col u u\u2081 \u2243S Row y y\u2081 \u2219S Col v v\u2081\n let\n open EqReasoning setoidS\n ih = <\u2219S> _ _ _ {x} {y} {u} {v} p q\n ih\u2081 = <\u2219S> _ _ _ {x\u2081} {y\u2081} {u\u2081} {v\u2081} p\u2081 q\u2081\n in begin\n Row x x\u2081 \u2219S Col u u\u2081\n \u2261\u27e8 refl-\u2261 \u27e9\n x \u2219S u +S x\u2081 \u2219S u\u2081\n \u2248\u27e8 <+S> L L {x \u2219S u} {y \u2219S v} {x\u2081 \u2219S u\u2081} {y\u2081 \u2219S v\u2081} ih ih\u2081 \u27e9\n y \u2219S v +S y\u2081 \u2219S v\u2081\n \u220e\n <\u2219S> L (B b b\u2081) (B c c\u2081) {Row x x\u2081} {Row y y\u2081} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083} (p , p\u2081) (q , q\u2081 , q\u2082 , q\u2083) =\n (let\n ih = <\u2219S> L b c p q\n ih\u2081 = <\u2219S> L b\u2081 c p\u2081 q\u2082\n in <+S> L c ih ih\u2081) ,\n <+S> L c\u2081 (<\u2219S> L b c\u2081 p q\u2081) (<\u2219S> L b\u2081 c\u2081 p\u2081 q\u2083)\n <\u2219S> (B a a\u2081) L L {Col x x\u2081} {Col y y\u2081} {One x\u2082} {One x\u2083} (p , p\u2081) q =\n <\u2219S> a L L p q ,\n <\u2219S> a\u2081 L L p\u2081 q\n <\u2219S> (B a a\u2081) L (B c c\u2081) {Col x x\u2081} {Col y y\u2081} {Row u u\u2081} {Row v v\u2081} (p , p\u2081) (q , q\u2081) =\n <\u2219S> a L c p q ,\n <\u2219S> a L c\u2081 p q\u2081 ,\n <\u2219S> a\u2081 L c p\u2081 q ,\n <\u2219S> a\u2081 L c\u2081 p\u2081 q\u2081\n <\u2219S> (B a a\u2081) (B b b\u2081) L {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Col u u\u2081} {Col v v\u2081} (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081) =\n <+S> a L (<\u2219S> a b L p q) (<\u2219S> a b\u2081 L p\u2081 q\u2081) ,\n <+S> a\u2081 L (<\u2219S> a\u2081 b L p\u2082 q) (<\u2219S> a\u2081 b\u2081 L p\u2083 q\u2081 )\n <\u2219S> (B a a\u2081) (B b b\u2081) (B c c\u2081) {Q x x\u2081 x\u2082 x\u2083} {Q y y\u2081 y\u2082 y\u2083} {Q u u\u2081 u\u2082 u\u2083} {Q v v\u2081 v\u2082 v\u2083} (p , p\u2081 , p\u2082 , p\u2083) (q , q\u2081 , q\u2082 , q\u2083) =\n <+S> a c (<\u2219S> a b c p q) (<\u2219S> a b\u2081 c p\u2081 q\u2082) ,\n <+S> a c\u2081 (<\u2219S> a b c\u2081 p q\u2081) (<\u2219S> a b\u2081 c\u2081 p\u2081 q\u2083) ,\n <+S> a\u2081 c (<\u2219S> a\u2081 b c p\u2082 q) (<\u2219S> a\u2081 b\u2081 c p\u2083 q\u2082) ,\n <+S> a\u2081 c\u2081 (<\u2219S> a\u2081 b c\u2081 p\u2082 q\u2081) (<\u2219S> a\u2081 b\u2081 c\u2081 p\u2083 q\u2083)\n\n idemS : (r c : Shape) (x : M s r c) \u2192 x +S x \u2243S x\n idemS L L (One x) = idem x\n idemS L (B c c\u2081) (Row x x\u2081) = (idemS _ _ x) , (idemS _ _ x\u2081)\n idemS (B r r\u2081) L (Col x x\u2081) = (idemS _ _ x) , (idemS _ _ x\u2081)\n idemS (B r r\u2081) (B c c\u2081) (Q x x\u2081 x\u2082 x\u2083) =\n (idemS _ _ x) , (idemS _ _ x\u2081 , (idemS _ _ x\u2082 , idemS _ _ x\u2083))\n\n swapMid : {r c : Shape} (x y z w : M s r c) \u2192\n (x +S y) +S (z +S w) \u2243S (x +S z) +S (y +S w)\n swapMid {r} {c} x y z w =\n let open EqReasoning setoidS\n in begin\n (x +S y) +S (z +S w)\n \u2248\u27e8 assocS _ _ x y (z +S w) \u27e9\n x +S y +S z +S w\n \u2248\u27e8 <+S> r c (reflS r c) (symS r c (assocS r c y z w)) \u27e9\n x +S (y +S z) +S w\n \u2248\u27e8 <+S> r c (reflS r c) (<+S> r c (commS r c y z) (reflS r c)) \u27e9\n x +S (z +S y) +S w\n \u2248\u27e8 <+S> r c (reflS r c) (assocS r c z y w) \u27e9\n x +S z +S y +S w\n \u2248\u27e8 symS r c (assocS r c x z (y +S w)) \u27e9\n (x +S z) +S (y +S w)\n \u220e\n\n distlHelp : \u2200 {a b b\u2081 c\u2081}\n (x : M s a b)\n (y z : M s b c\u2081)\n (x\u2081 : M s a b\u2081)\n (y\u2081 z\u2081 : M s b\u2081 c\u2081) \u2192\n (x \u2219S (y +S z)) \u2243S (x \u2219S y +S x \u2219S z) \u2192\n (x\u2081 \u2219S (y\u2081 +S z\u2081)) \u2243S (x\u2081 \u2219S y\u2081 +S x\u2081 \u2219S z\u2081) \u2192\n (x \u2219S (y +S z) +S x\u2081 \u2219S (y\u2081 +S z\u2081))\n \u2243S ((x \u2219S y +S x\u2081 \u2219S y\u2081) +S x \u2219S z +S x\u2081 \u2219S z\u2081)\n distlHelp x y z x\u2081 y\u2081 z\u2081 p q =\n let open EqReasoning setoidS\n in begin\n x \u2219S (y +S z) +S x\u2081 \u2219S (y\u2081 +S z\u2081)\n \u2248\u27e8 <+S> _ _ {x \u2219S (y +S z)} {x \u2219S y +S x \u2219S z}\n {x\u2081 \u2219S (y\u2081 +S z\u2081)} {x\u2081 \u2219S y\u2081 +S x\u2081 \u2219S z\u2081} p q \u27e9\n (x \u2219S y +S x \u2219S z) +S x\u2081 \u2219S y\u2081 +S x\u2081 \u2219S z\u2081\n \u2248\u27e8 swapMid (x \u2219S y) (x \u2219S z) (x\u2081 \u2219S y\u2081) (x\u2081 \u2219S z\u2081) \u27e9\n (x \u2219S y +S x\u2081 \u2219S y\u2081) +S x \u2219S z +S x\u2081 \u2219S z\u2081\n \u220e\n\n distlS : {a b c : Shape} (x : M s a b) (y z : M s b c) \u2192\n (x \u2219S (y +S z)) \u2243S ((x \u2219S y) +S (x \u2219S z))\n distlS {L} {L} {L} (One x) (One y) (One z) = distl x y z\n distlS {L} {L} {B c c\u2081} (One x) (Row y y\u2081) (Row z z\u2081) =\n distlS (One x) y z ,\n distlS (One x) y\u2081 z\u2081\n distlS {L} {(B b b\u2081)} {L} (Row x x\u2081) (Col y y\u2081) (Col z z\u2081) =\n distlHelp x y z x\u2081 y\u2081 z\u2081 (distlS x y z) (distlS x\u2081 y\u2081 z\u2081)\n distlS {L} {(B b b\u2081)} {(B c c\u2081)} (Row x x\u2081) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n distlHelp x y z x\u2081 y\u2082 z\u2082 (distlS x y z) (distlS x\u2081 y\u2082 z\u2082) ,\n distlHelp x y\u2081 z\u2081 x\u2081 y\u2083 z\u2083 (distlS x y\u2081 z\u2081) (distlS x\u2081 y\u2083 z\u2083)\n distlS {(B a a\u2081)} {L} {L} (Col x x\u2081) (One x\u2082) (One x\u2083) =\n distlS x (One x\u2082) (One x\u2083) ,\n distlS x\u2081 (One x\u2082) (One x\u2083)\n distlS {(B a a\u2081)} {L} {(B c c\u2081)} (Col x x\u2081) (Row y y\u2081) (Row z z\u2081) =\n distlS x y z ,\n distlS x y\u2081 z\u2081 ,\n distlS x\u2081 y z ,\n distlS x\u2081 y\u2081 z\u2081\n distlS {(B a a\u2081)} {(B b b\u2081)} {L} (Q x x\u2081 x\u2082 x\u2083) (Col y y\u2081) (Col z z\u2081) =\n distlHelp x y z x\u2081 y\u2081 z\u2081 (distlS x y z) (distlS x\u2081 y\u2081 z\u2081) ,\n distlHelp x\u2082 y z x\u2083 y\u2081 z\u2081 (distlS x\u2082 y z) (distlS x\u2083 y\u2081 z\u2081)\n distlS {(B a a\u2081)} {(B b b\u2081)} {(B c c\u2081)} (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n distlHelp x y z x\u2081 y\u2082 z\u2082 (distlS x y z) (distlS x\u2081 y\u2082 z\u2082) ,\n distlHelp x y\u2081 z\u2081 x\u2081 y\u2083 z\u2083 (distlS x y\u2081 z\u2081) (distlS x\u2081 y\u2083 z\u2083) ,\n distlHelp x\u2082 y z x\u2083 y\u2082 z\u2082 (distlS x\u2082 y z) (distlS x\u2083 y\u2082 z\u2082) ,\n distlHelp x\u2082 y\u2081 z\u2081 x\u2083 y\u2083 z\u2083 (distlS x\u2082 y\u2081 z\u2081) (distlS x\u2083 y\u2083 z\u2083)\n\n distrHelp : \u2200 {r m m\u2081 c : Shape}\n (x : M s m c)\n (y z : M s r m)\n (x\u2081 : M s m\u2081 c)\n (y\u2081 z\u2081 : M s r m\u2081) \u2192\n ((y +S z) \u2219S x) \u2243S (y \u2219S x +S z \u2219S x) \u2192\n ((y\u2081 +S z\u2081) \u2219S x\u2081) \u2243S (y\u2081 \u2219S x\u2081 +S z\u2081 \u2219S x\u2081) \u2192\n ((y +S z) \u2219S x +S (y\u2081 +S z\u2081) \u2219S x\u2081)\n \u2243S ((y \u2219S x +S y\u2081 \u2219S x\u2081) +S z \u2219S x +S z\u2081 \u2219S x\u2081)\n distrHelp x y z x\u2081 y\u2081 z\u2081 p q =\n let open EqReasoning setoidS\n in begin\n (y +S z) \u2219S x +S (y\u2081 +S z\u2081) \u2219S x\u2081\n \u2248\u27e8 <+S> _ _ {(y +S z) \u2219S x} {y \u2219S x +S z \u2219S x}\n {(y\u2081 +S z\u2081) \u2219S x\u2081} {y\u2081 \u2219S x\u2081 +S z\u2081 \u2219S x\u2081} p q \u27e9\n (y \u2219S x +S z \u2219S x) +S y\u2081 \u2219S x\u2081 +S z\u2081 \u2219S x\u2081\n \u2248\u27e8 swapMid (y \u2219S x) (z \u2219S x) (y\u2081 \u2219S x\u2081) (z\u2081 \u2219S x\u2081) \u27e9\n (y \u2219S x +S y\u2081 \u2219S x\u2081) +S z \u2219S x +S z\u2081 \u2219S x\u2081\n \u220e\n\n distrS : {r m c : Shape} (x : M s m c) (y z : M s r m) \u2192\n ((y +S z) \u2219S x) \u2243S ((y \u2219S x) +S (z \u2219S x))\n distrS {L} {L} {L} (One x) (One y) (One z) =\n distr x y z\n distrS {L} {L} {B c c\u2081} (Row x x\u2081) (One x\u2082) (One x\u2083) =\n (distrS x (One x\u2082) (One x\u2083)) ,\n (distrS x\u2081 (One x\u2082) (One x\u2083))\n distrS {L} {B m m\u2081} {L} (Col x x\u2081) (Row y y\u2081) (Row z z\u2081) =\n distrHelp x y z x\u2081 y\u2081 z\u2081 (distrS x y z) (distrS x\u2081 y\u2081 z\u2081)\n distrS {L} {B m m\u2081} {B c c\u2081} (Q x x\u2081 x\u2082 x\u2083) (Row y y\u2081) (Row z z\u2081) =\n (distrHelp x y z x\u2082 y\u2081 z\u2081 (distrS x y z) (distrS x\u2082 y\u2081 z\u2081)) ,\n (distrHelp x\u2081 y z x\u2083 y\u2081 z\u2081 (distrS x\u2081 y z) (distrS x\u2083 y\u2081 z\u2081))\n distrS {B r r\u2081} {L} {L} (One x) (Col y y\u2081) (Col z z\u2081) =\n distrS (One x) y z ,\n distrS (One x) y\u2081 z\u2081\n distrS {B r r\u2081} {L} {B c c\u2081} (Row x x\u2081) (Col y y\u2081) (Col z z\u2081) =\n (distrS x y z) ,\n (distrS x\u2081 y z) ,\n (distrS x y\u2081 z\u2081) ,\n (distrS x\u2081 y\u2081 z\u2081)\n distrS {B r r\u2081} {B m m\u2081} {L} (Col x x\u2081) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n (distrHelp x y z x\u2081 y\u2081 z\u2081 (distrS x y z) (distrS x\u2081 y\u2081 z\u2081)) ,\n (distrHelp x y\u2082 z\u2082 x\u2081 y\u2083 z\u2083 (distrS x y\u2082 z\u2082) (distrS x\u2081 y\u2083 z\u2083))\n distrS {B r r\u2081} {B m m\u2081} {B c c\u2081} (Q x x\u2081 x\u2082 x\u2083) (Q y y\u2081 y\u2082 y\u2083) (Q z z\u2081 z\u2082 z\u2083) =\n distrHelp x y z x\u2082 y\u2081 z\u2081 (distrS x y z) (distrS x\u2082 y\u2081 z\u2081) ,\n distrHelp x\u2081 y z x\u2083 y\u2081 z\u2081 (distrS x\u2081 y z) (distrS x\u2083 y\u2081 z\u2081) ,\n distrHelp x y\u2082 z\u2082 x\u2082 y\u2083 z\u2083 (distrS x y\u2082 z\u2082) (distrS x\u2082 y\u2083 z\u2083) ,\n distrHelp x\u2081 y\u2082 z\u2082 x\u2083 y\u2083 z\u2083 (distrS x\u2081 y\u2082 z\u2082) (distrS x\u2083 y\u2083 z\u2083)\n\n\n SNR : SemiNearRing\n SNR =\n record\n { s = S\n ; _\u2243\u209b_ = _\u2243S_ {shape} {shape}\n ; 0\u209b = 0S shape shape\n ; _+\u209b_ = _+S_\n ; _\u2219\u209b_ = _\u2219S_\n ; isCommMon = isCommMonS\n ; zero\u02e1 = zeroS\u02e1 shape shape shape\n ; zero\u02b3 = zeroS\u02b3 shape shape shape\n ; _<\u2219>_ = <\u2219S> shape shape shape\n ; idem = idemS shape shape\n ; distl = distlS {shape} {shape}\n ; distr = distrS {shape} {shape}\n }\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"7573d7f994c5821b13d69bce4942f9b5bb61dfc6","subject":"fix elgamal","message":"fix elgamal\n","repos":"crypto-agda\/crypto-agda","old_file":"elgamal.agda","new_file":"elgamal.agda","new_contents":"{-# OPTIONS --copatterns #-}\nopen import Type\nopen import Function\nopen import Data.Product\nopen import Data.Unit\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc; Fin\u25b9\u2115) renaming (#_ to ##_)\nopen import Data.Nat.NP hiding (_^_; _==_)\nopen import Data.Bit\nopen import Data.Bits hiding (_==_)\nopen import Relation.Binary.PropositionalEquality.NP as \u2261\nimport Game.DDH\nimport Game.IND-CPA\nimport Cipher.ElGamal.Generic\nopen import Search.Type\nopen import Search.Searchable renaming (Searchable to Explorable)\nopen import Search.Searchable.Product\nopen import Search.Searchable.Fin\nopen import Relation.Binary.NP\n\nmodule elgamal where\n\ndata `\u2605 : \u2605 where\n `\u22a4 : `\u2605\n `X : `\u2605\n _`\u00d7_ : `\u2605 \u2192 `\u2605 \u2192 `\u2605\ninfixr 2 _`\u00d7_\n\nmodule Univ (X : \u2605) where\n El : `\u2605 \u2192 \u2605\n El `\u22a4 = \u22a4\n El `X = X\n El (u\u2080 `\u00d7 u\u2081) = El u\u2080 \u00d7 El u\u2081\n\n record \u21ba (R : `\u2605) (A : \u2605) : \u2605 where\n constructor mk\n field\n run\u21ba : El R \u2192 A\n open \u21ba public\n\n EXP : (R : `\u2605) \u2192 \u2605\n EXP R = \u21ba R Bit\n\n Det : \u2605 \u2192 \u2605\n Det = \u21ba `\u22a4\n\n \u03bcU : Explorable X \u2192 \u2200 u \u2192 Explorable (El u)\n \u03bcU \u03bcX `\u22a4 = \u03bc\u22a4\n \u03bcU \u03bcX `X = \u03bcX\n \u03bcU \u03bcX (u\u2080 `\u00d7 u\u2081) = \u03bcU \u03bcX u\u2080 \u00d7-\u03bc \u03bcU \u03bcX u\u2081\n\nmodule \u2124q-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u03bc\u2124q : Explorable \u2124q)\n (\u229e-stable : \u2200 x \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u229e_ x))\n where\n\n -- open Sum\n open Univ \u2124q public\n open `\u2605 public renaming (`X to `\u2124q)\n\n #_ : \u2200 {u} \u2192 \u21ba u Bit \u2192 \u2115\n #_ {u} f = count (\u03bcU \u03bc\u2124q u) (run\u21ba f)\n\n #q_ : Count \u2124q\n #q_ = count \u03bc\u2124q\n\n \u2047 : \u2200 R \u2192 \u21ba R (El R)\n run\u21ba (\u2047 _) = id\n\n pure\u21ba : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n run\u21ba (pure\u21ba x) r = x -- turning r to _ produce an error\n\n \u27ea_\u27eb : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n \u27ea_\u27eb = pure\u21ba\n\n {-\n \u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n \u27ea_\u27eb\u1d30 = pure\u1d30\n -}\n\n map\u21ba : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n run\u21ba (map\u21ba f x) r = f (run\u21ba x r)\n\n infixl 4 _\u229b_\n _\u229b_ : \u2200 {R S A B} \u2192 \u21ba R (A \u2192 B) \u2192 \u21ba S A \u2192 \u21ba (R `\u00d7 S) B\n run\u21ba (af \u229b ax) rs = run\u21ba af (proj\u2081 rs) (run\u21ba ax (proj\u2082 rs))\n\n \u27ea_\u00b7_\u27eb : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n \u27ea f \u00b7 x \u27eb = map\u21ba f x\n\n \u27ea_\u00b7_\u00b7_\u27eb : \u2200 {A B C} {R S} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba R A \u2192 \u21ba S B \u2192 \u21ba (R `\u00d7 S) C\n \u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n _\u27e8\u229e\u27e9_ : \u2200 {R S} \u2192 \u21ba R \u2124q \u2192 \u21ba S \u2124q \u2192 \u21ba (R `\u00d7 S) \u2124q\n x \u27e8\u229e\u27e9 y = \u27ea _\u229e_ \u00b7 x \u00b7 y \u27eb\n\n \u27e8_\u229e\u27e9_ : \u2200 {R} \u2192 \u2124q \u2192 \u21ba R \u2124q \u2192 \u21ba R \u2124q\n \u27e8 x \u229e\u27e9 y = \u27ea _\u229e_ x \u00b7 y \u27eb\n\n infix 4 _\u2248\u21ba_ _\u2248\u1d2c_\n _\u2248\u21ba_ : \u2200 {R : `\u2605} (f g : EXP R) \u2192 \u2605\n _\u2248\u21ba_ = _\u2261_ on #_\n\n _\u2248\u1d2c_ : \u2200 {A R} (f g : \u21ba R A) \u2192 Set _\n _\u2248\u1d2c_ {A} f g = \u2200 (Adv : A \u2192 Bit) \u2192 \u27ea Adv \u00b7 f \u27eb \u2248\u21ba \u27ea Adv \u00b7 g \u27eb\n\n lem : \u2200 x \u2192 \u27e8 x \u229e\u27e9 (\u2047 `\u2124q) \u2248\u1d2c \u2047 _\n lem x Adv = sym (\u229e-stable x (Bit\u25b9\u2115 \u2218 Adv))\n\n -- \u2200 (A : \u2124q \u2192 Bit) \u2192 # (A \u2047)\n\nopen Fin.Modulo renaming (sucmod to [suc]; sucmod-inj to [suc]-inj)\n\n{-\nmodule \u2124q-implem (q-2 : \u2115) where\n q : \u2115\n q = 2 + q-2\n\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = zero\n\n [1] : \u2124q\n [1] = suc zero\n-}\nmodule \u2124q-implem (q-1 : \u2115) ([0]' [1]' : Fin (suc q-1)) where\n -- open Sum\n q : \u2115\n q = suc q-1\n\n \u2124q : \u2605\n \u2124q = Fin q\n\n \u03bc\u2124q : Explorable \u2124q\n \u03bc\u2124q = \u03bcFinSuc q-1\n\n sum\u2124q : Sum \u2124q\n sum\u2124q = sum \u03bc\u2124q\n\n [0] : \u2124q\n [0] = [0]'\n\n [1] : \u2124q\n [1] = [1]'\n\n [suc]-stable : SumStableUnder (sum \u03bc\u2124q) [suc]\n [suc]-stable = \u03bcFinSUI [suc] [suc]-inj\n\n _\u2115\u229e_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = m \u2115\u229e ([suc] n)\n\n \u2115\u229e-inj : \u2200 n {x y} \u2192 n \u2115\u229e x \u2261 n \u2115\u229e y \u2192 x \u2261 y\n \u2115\u229e-inj zero eq = eq\n \u2115\u229e-inj (suc n) eq = [suc]-inj (\u2115\u229e-inj n eq)\n\n \u2115\u229e-stable : \u2200 m \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u2115\u229e_ m)\n \u2115\u229e-stable m = \u03bcFinSUI (_\u2115\u229e_ m) (\u2115\u229e-inj m)\n\n _\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u229e n = Fin\u25b9\u2115 m \u2115\u229e n\n\n \u229e-inj : \u2200 m {x y} \u2192 m \u229e x \u2261 m \u229e y \u2192 x \u2261 y\n \u229e-inj m = \u2115\u229e-inj (Fin\u25b9\u2115 m)\n\n \u229e-stable : \u2200 m \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u229e_ m)\n \u229e-stable m = \u03bcFinSUI (_\u229e_ m) (\u229e-inj m)\n\n _\u2115\u22a0_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u22a0 n = [0]\n suc m \u2115\u22a0 n = n \u229e (m \u2115\u22a0 n)\n\n _\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u22a0 n = Fin\u25b9\u2115 m \u2115\u22a0 n\n\n _[^]\u2115_ : \u2124q \u2192 \u2115 \u2192 \u2124q\n m [^]\u2115 zero = [1]\n m [^]\u2115 suc n = m \u22a0 (m [^]\u2115 n)\n\n _[^]_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m [^] n = m [^]\u2115 (Fin\u25b9\u2115 n)\n\nmodule G-implem (p-1 q-1 : \u2115) (g' 0[p] 1[p] : Fin (suc p-1)) (0[q] 1[q] : Fin (suc q-1)) where\n open \u2124q-implem q-1 0[q] 1[q] public\n open \u2124q-implem p-1 0[p] 1[p] public using () renaming (\u2124q to G; _\u22a0_ to _\u2219_; _[^]\u2115_ to _^[p]_)\n\n g : G\n g = g'\n\n _^_ : G \u2192 \u2124q \u2192 G\n x ^ n = x ^[p] Fin\u25b9\u2115 n\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n {-\n g^-inj : \u2200 m n \u2192 g^ m \u2261 g^ n \u2192 m \u2261 n\n g^-inj = {!!}\n -}\n\nmodule G-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u03bc\u2124q : Explorable \u2124q)\n (\u229e-stable : \u2200 x \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u229e_ x))\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n where\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n open \u2124q-count \u2124q _\u229e_ \u03bc\u2124q \u229e-stable\n\n \u2047G : \u21ba `\u2124q G\n run\u21ba \u2047G x = g^ x\n\n #G : Count G\n #G f = #q (f \u2218 g^_)\n\n {-\n #G-\u2219 : \u2200 f m \u2192 #G (f \u2218 _\u2219_ m) \u2261 #G f\n #G-\u2219 f m = {!!}\n -}\n\nmodule El-Gamal-Generic\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (Message : \u2605)\n (_\u2219_ : G \u2192 Message \u2192 Message)\n\n -- Required for decryption\n (_\/_ : Message \u2192 G \u2192 Message)\n\n -- Required for the correctness proof\n (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n -- Required for the security proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : Explorable \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : Explorable R\u2090)\n where\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n -- g\u02e3 is the pk\n -- x is the sk\n\n R\u2093 : \u2605\n R\u2093 = \u2124q\n\n open Cipher.ElGamal.Generic Message \u2124q G g _^_ _\u2219_ _\/_\n\n functional-correctness : \u2200 x y m \u2192 Dec x (Enc (g^ x) m y) \u2261 m\n functional-correctness x y m rewrite comm-^ g x y | \/-\u2219 (g^ y ^ x) m = refl\n\n module IND-CPA = Game.IND-CPA PubKey SecKey Message CipherText R\u2091 R\u2096 R\u2090 R\u2093 KeyGen Enc\n open IND-CPA using (R)\n\n UnusedGame : (i : Bit) \u2192 IND-CPA.Adv \u2192 (Bit \u00d7 R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n UnusedGame i (m , d) (b , r\u2090 , x , y , z) = b == d r\u2090 g\u02e3 (g\u02b8 , \u03b6)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n \u03b4 = g\u02e3 ^ case i 0\u2192 y 1\u2192 z\n \u03b6 = \u03b4 \u2219 m r\u2090 g\u02e3 b\n\n module DDH = Game.DDH \u2124q G g _^_ R\u2090\n\n OTP\u2141 : (R\u2090 \u2192 G \u2192 Message) \u2192 (R\u2090 \u2192 G \u2192 G \u2192 Message \u2192 Bit) \u2192 R \u2192 Bit\n OTP\u2141 M d (r , x , y , z) = d r g\u02e3 g\u02b8 (g\u1dbb \u2219 M r g\u02e3)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n g\u1dbb = g^ z\n\n TrA : Bit \u2192 IND-CPA.Adv \u2192 DDH.Adv\n TrA b (m , d) r\u2090 g\u02e3 g\u02b8 g\u02e3\u02b8 = d r\u2090 g\u02e3 (g\u02b8 , g\u02e3\u02b8 \u2219 m r\u2090 g\u02e3 b)\n\n projM : IND-CPA.Adv \u2192 Bit \u2192 R\u2090 \u2192 G \u2192 Message\n projM (m , _) b r\u2090 g\u02e3 = m r\u2090 g\u02e3 b\n\n projD : IND-CPA.Adv \u2192 R\u2090 \u2192 G \u2192 G \u2192 Message \u2192 Bit\n projD (_ , d) r\u2090 g\u02e3 g\u02b8 g\u1dbb\u2219M = d r\u2090 g\u02e3 (g\u02b8 , g\u1dbb\u2219M)\n\n module Unused where\n like-\u2141 : Bit \u2192 IND-CPA.Game\n like-\u2141 b (m , d) (r\u2090 , x , y , _z) =\n d r\u2090 g\u02e3 (g\u02b8 , (g\u02e3 ^ y) \u2219 m r\u2090 g\u02e3 b)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n\n IND-CPA-\u2141\u2261like-\u2141 : IND-CPA.\u2141 \u2261 like-\u2141\n IND-CPA-\u2141\u2261like-\u2141 = refl\n\n -- R = R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q\n \u03bcR : Explorable R\n \u03bcR = \u03bcR\u2090 \u00d7-\u03bc \u03bc\u2124q \u00d7-\u03bc \u03bc\u2124q \u00d7-\u03bc \u03bc\u2124q\n\n #\u1d3f_ : Count R\n #\u1d3f_ = count \u03bcR\n\n #q_ : Count \u2124q\n #q_ = count \u03bc\u2124q\n\n _\u2248q_ : (f g : \u2124q \u2192 Bit) \u2192 \u2605\n f \u2248q g = #q f \u2261 #q g\n\n Re = (f g : R \u2192 Bit) \u2192 \u2605\n record Tra (_\u2248\u2080_ _\u2248\u2081_ : Re) (f g : R \u2192 Bit) : \u2605 where\n field\n h : R \u2192 Bit\n f\u2248\u2080h : f \u2248\u2080 h\n h\u2248\u2081g : h \u2248\u2081 g\n\n record _\u2248\u1d3f_ (f g : R \u2192 Bit) : \u2605 where\n constructor mk\n field\n un-\u2248\u1d3f : #\u1d3f f \u2261 #\u1d3f g\n open _\u2248\u1d3f_ public\n\n \u2248\u1d3f-trans : Transitive _\u2248\u1d3f_\n \u2248\u1d3f-trans (mk p) (mk q) = mk (\u2261.trans p q)\n\n module \u2248\u1d3f-Reasoning where\n open Trans-Reasoning _\u2248\u1d3f_ \u2248\u1d3f-trans public using () renaming (_\u2248\u27e8_\u27e9_ to _\u2248\u1d3f\u27e8_\u27e9_)\n infix 2 _\u220e\n\n _\u220e : \u2200 x \u2192 x \u2248\u1d3f x\n _ \u220e = mk refl\n\n module Proof\n (ddh-hyp : \u2200 A \u2192 DDH.\u2141\u2080 A \u2248\u1d3f DDH.\u2141\u2081 A)\n (otp-lem : \u2200 A m\u2080 m\u2081 \u2192 (\u03bb x \u2192 A (g^ x \u2219 m\u2080)) \u2248q (\u03bb x \u2192 A (g^ x \u2219 m\u2081)))\n (A : IND-CPA.Adv) (b : Bit)\n where\n OTP\u2141-lem : \u2200 d M\u2080 M\u2081 \u2192 OTP\u2141 M\u2080 d \u2248\u1d3f OTP\u2141 M\u2081 d\n OTP\u2141-lem d M\u2080 M\u2081 = mk (\n sum-ext \u03bcR\u2090 (\u03bb r \u2192\n sum-ext \u03bc\u2124q (\u03bb x \u2192\n sum-ext \u03bc\u2124q (\u03bb y \u2192\n pf r x y))))\n where\n pf : \u2200 r x y \u2192 count \u03bc\u2124q (\u03bb z \u2192 OTP\u2141 M\u2080 d (r , x , y , z))\n \u2261 count \u03bc\u2124q (\u03bb z \u2192 OTP\u2141 M\u2081 d (r , x , y , z))\n pf r x y rewrite otp-lem (d r (g^ x) (g^ y)) (M\u2080 r (g^ x)) (M\u2081 r (g^ x)) = refl\n\n -- moving this definition above OTP\u2141-lem breaks type-checking: ???\n \u00acb : Bit\n \u00acb = not b\n\n A\u1d47 = TrA b A\n A\u00ac\u1d47 = TrA \u00acb A\n\n pf0,5 : IND-CPA.\u2141 b A \u2257 DDH.\u2141\u2080 A\u1d47\n pf0,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n pf2,5 : DDH.\u2141\u2081 A\u1d47 \u2261 OTP\u2141 (projM A b) (projD A)\n pf2,5 = refl\n\n pf4,5 : IND-CPA.\u2141 \u00acb A \u2257 DDH.\u2141\u2080 A\u00ac\u1d47\n pf4,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n open \u2248\u1d3f-Reasoning\n\n final : IND-CPA.\u2141 b A \u2248\u1d3f IND-CPA.\u2141 \u00acb A\n final = IND-CPA.\u2141 b A\n \u2248\u1d3f\u27e8 mk (sum-ext \u03bcR (cong Bit\u25b9\u2115 \u2218 pf0,5)) \u27e9\n DDH.\u2141\u2080 A\u1d47\n \u2248\u1d3f\u27e8 ddh-hyp A\u1d47 \u27e9\n DDH.\u2141\u2081 A\u1d47\n \u2248\u1d3f\u27e8 OTP\u2141-lem (projD A) (projM A b) (projM A \u00acb) \u27e9\n DDH.\u2141\u2081 A\u00ac\u1d47\n \u2248\u1d3f\u27e8 mk (\u2261.sym (un-\u2248\u1d3f (ddh-hyp A\u00ac\u1d47))) \u27e9\n DDH.\u2141\u2080 A\u00ac\u1d47\n \u2248\u1d3f\u27e8 mk (\u2261.sym (sum-ext \u03bcR (cong Bit\u25b9\u2115 \u2218 pf4,5))) \u27e9\n IND-CPA.\u2141 \u00acb A\n \u220e\n\nmodule El-Gamal-Base\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n\n -- Required for decryption\n (_\/_ : G \u2192 G \u2192 G)\n\n -- Required for the correctness proof\n (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n {-\n (_\u207b\u00b9 : G \u2192 G)\n (\u207b\u00b9-inverse : \u2200 x \u2192 x \u207b\u00b9 \u2219 x \u2261 1G)\n -}\n\n -- Required for the proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : Explorable \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : Explorable R\u2090)\n where\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ G _\u2219_\n _\/_ \/-\u2219 comm-^\n dist-^-\u22a0 \u03bc\u2124q R\u2090 \u03bcR\u2090 public\n\n module OTP\u2141-LEM\n (otp-lem1 : \u2200 (A : G \u2192 Bit) m \u2192 (\u03bb x \u2192 A (g^ x \u2219 m)) \u2248q (\u03bb x \u2192 A (g^ x)))\n where\n\n otp-lem : \u2200 (A : G \u2192 Bit) m\u2080 m\u2081 \u2192 (\u03bb x \u2192 A (g^ x \u2219 m\u2080)) \u2248q (\u03bb x \u2192 A (g^ x \u2219 m\u2081))\n otp-lem A m\u2080 m\u2081 rewrite otp-lem1 A m\u2080 | otp-lem1 A m\u2081 = refl\n\nmodule El-Gamal-Hashed\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n -- (HKey : \u2605)\n (|M| : \u2115)\n (\u210b : {-HKey \u2192-} G \u2192 Bits |M|)\n\n -- (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n -- Required for the proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : Explorable \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : Explorable R\u2090)\n where\n\n Message = Bits |M|\n\n \u210b\u27e8_\u27e9\u2295_ : G \u2192 Message \u2192 Message\n \u210b\u27e8 \u03b4 \u27e9\u2295 m = \u210b \u03b4 \u2295 m\n\n _\/_ : Message \u2192 G \u2192 Message\n _\/_ m \u03b4 = \u210b \u03b4 \u2295 m\n{-\n\n \/-\u2219 : \u2200 x y \u2192 \u210b\u27e8 x \u27e9\u2295 y \/ x \u2261 y\n \/-\u2219 x y = {!!}\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ Message \u210b\u27e8_\u27e9\u2295_ _\/_ {!!} {!!}\n dist-^-\u22a0 sum\u2124q sum\u2124q-ext R\u2090 sumR\u2090 sumR\u2090-ext public\n -}\n\n {-\n OTP\u2141-lem : \u2200 d M\u2080 M\u2081 \u2192 OTP\u2141 M\u2080 d \u2248\u1d3f OTP\u2141 M\u2081 d\n OTP\u2141-lem = ?\n -}\n\nmodule \u27e8\u2124p\u27e9\u2605 p-3 {- p is prime -} (`R\u2090 : `\u2605) where\n p : \u2115\n p = 3 + p-3\n\n q : \u2115\n q = p \u2238 1\n\n module G = G-implem p q (## 2) (## 0) (## 1) (## 0) (## 1)\n open G\n\n postulate\n _\u207b\u00b9 : G \u2192 G\n\n _\/_ : G \u2192 G \u2192 G\n x \/ y = x \u2219 (y \u207b\u00b9)\n\n postulate\n \/-\u2022 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y\n dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y\n \u22a0-comm : \u2200 x y \u2192 x \u22a0 y \u2261 y \u22a0 x\n\n comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x\n comm-^ \u03b1 x y = (\u03b1 ^ x)^ y\n \u2261\u27e8 sym (dist-^-\u22a0 \u03b1 x y) \u27e9\n \u03b1 ^ (x \u22a0 y)\n \u2261\u27e8 cong (_^_ \u03b1) (\u22a0-comm x y) \u27e9\n \u03b1 ^ (y \u22a0 x)\n \u2261\u27e8 dist-^-\u22a0 \u03b1 y x \u27e9\n (\u03b1 ^ y)^ x\n \u220e\n where open \u2261-Reasoning\n\n open \u2124q-count \u2124q _\u229e_ \u03bc\u2124q \u229e-stable\n\n \u03bcR\u2090 : Explorable (El `R\u2090)\n \u03bcR\u2090 = \u03bcU \u03bc\u2124q `R\u2090\n\n R\u2090 = El `R\u2090\n sumR\u2090 = sum \u03bcR\u2090\n sumR\u2090-ext = sum-ext \u03bcR\u2090\n\n module EB = El-Gamal-Base _ _\u22a0_ G g _^_ _\u2219_ _\/_ \/-\u2022 comm-^ dist-^-\u22a0 \u03bc\u2124q R\u2090 \u03bcR\u2090\n open EB hiding (g^_)\n\n otp-base-lem : \u2200 (A : G \u2192 Bit) m \u2192 (A \u2218 g^_) \u2248q (A \u2218 g^_ \u2218 _\u229e_ m)\n otp-base-lem A m = \u229e-stable m (Bit\u25b9\u2115 \u2218 A \u2218 g^_)\n\n postulate\n ddh-hyp : (A : DDH.Adv) \u2192 DDH.\u2141\u2080 A \u2248\u1d3f DDH.\u2141\u2081 A\n otp-lem : \u2200 (A : G \u2192 Bit) m \u2192 (\u03bb x \u2192 A (g^ x \u2219 m)) \u2248q (\u03bb x \u2192 A (g^ x))\n\n\n open OTP\u2141-LEM otp-lem\n\n {-\n final : \u2200 A \u2192 IND-CPA.\u2141 A 0b \u2248\u1d3f IND-CPA.\u2141 A 1b\n final A = Proof.final ddh-hyp OTP\u2141-lem A 0b\n -}\n\nmodule \u27e8\u212411\u27e9\u2605 = \u27e8\u2124p\u27e9\u2605 (11 \u2238 3)\n `X -- the amount of adversary randomness\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","old_contents":"{-# OPTIONS --copatterns #-}\nopen import Type\nopen import Function\nopen import Data.Product\nopen import Data.Unit\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc) renaming (#_ to ##_; to\u2115 to Fin\u25b9\u2115)\nopen import Data.Nat.NP hiding (_^_; _==_)\nopen import Data.Bit\nopen import Data.Bits hiding (_==_)\nopen import Relation.Binary.PropositionalEquality.NP as \u2261\nimport Game.DDH\nimport Game.IND-CPA\nimport Cipher.ElGamal.Generic\nopen import Search.Type\nopen import Search.Searchable renaming (Searchable to Explorable)\nopen import Search.Searchable.Product\nopen import Search.Searchable.Fin\nopen import Relation.Binary.NP\n\nmodule elgamal where\n\ndata `\u2605 : \u2605 where\n `\u22a4 : `\u2605\n `X : `\u2605\n _`\u00d7_ : `\u2605 \u2192 `\u2605 \u2192 `\u2605\ninfixr 2 _`\u00d7_\n\nmodule Univ (X : \u2605) where\n El : `\u2605 \u2192 \u2605\n El `\u22a4 = \u22a4\n El `X = X\n El (u\u2080 `\u00d7 u\u2081) = El u\u2080 \u00d7 El u\u2081\n\n record \u21ba (R : `\u2605) (A : \u2605) : \u2605 where\n constructor mk\n field\n run\u21ba : El R \u2192 A\n open \u21ba public\n\n EXP : (R : `\u2605) \u2192 \u2605\n EXP R = \u21ba R Bit\n\n Det : \u2605 \u2192 \u2605\n Det = \u21ba `\u22a4\n\n \u03bcU : Explorable X \u2192 \u2200 u \u2192 Explorable (El u)\n \u03bcU \u03bcX `\u22a4 = \u03bc\u22a4\n \u03bcU \u03bcX `X = \u03bcX\n \u03bcU \u03bcX (u\u2080 `\u00d7 u\u2081) = \u03bcU \u03bcX u\u2080 \u00d7-\u03bc \u03bcU \u03bcX u\u2081\n\nmodule \u2124q-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u03bc\u2124q : Explorable \u2124q)\n (\u229e-stable : \u2200 x \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u229e_ x))\n where\n\n -- open Sum\n open Univ \u2124q public\n open `\u2605 public renaming (`X to `\u2124q)\n\n #_ : \u2200 {u} \u2192 \u21ba u Bit \u2192 \u2115\n #_ {u} f = count (\u03bcU \u03bc\u2124q u) (run\u21ba f)\n\n #q_ : Count \u2124q\n #q_ = count \u03bc\u2124q\n\n \u2047 : \u2200 R \u2192 \u21ba R (El R)\n run\u21ba (\u2047 _) = id\n\n pure\u21ba : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n run\u21ba (pure\u21ba x) r = x -- turning r to _ produce an error\n\n \u27ea_\u27eb : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n \u27ea_\u27eb = pure\u21ba\n\n {-\n \u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n \u27ea_\u27eb\u1d30 = pure\u1d30\n -}\n\n map\u21ba : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n run\u21ba (map\u21ba f x) r = f (run\u21ba x r)\n\n infixl 4 _\u229b_\n _\u229b_ : \u2200 {R S A B} \u2192 \u21ba R (A \u2192 B) \u2192 \u21ba S A \u2192 \u21ba (R `\u00d7 S) B\n run\u21ba (af \u229b ax) rs = run\u21ba af (proj\u2081 rs) (run\u21ba ax (proj\u2082 rs))\n\n \u27ea_\u00b7_\u27eb : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n \u27ea f \u00b7 x \u27eb = map\u21ba f x\n\n \u27ea_\u00b7_\u00b7_\u27eb : \u2200 {A B C} {R S} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba R A \u2192 \u21ba S B \u2192 \u21ba (R `\u00d7 S) C\n \u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n _\u27e8\u229e\u27e9_ : \u2200 {R S} \u2192 \u21ba R \u2124q \u2192 \u21ba S \u2124q \u2192 \u21ba (R `\u00d7 S) \u2124q\n x \u27e8\u229e\u27e9 y = \u27ea _\u229e_ \u00b7 x \u00b7 y \u27eb\n\n \u27e8_\u229e\u27e9_ : \u2200 {R} \u2192 \u2124q \u2192 \u21ba R \u2124q \u2192 \u21ba R \u2124q\n \u27e8 x \u229e\u27e9 y = \u27ea _\u229e_ x \u00b7 y \u27eb\n\n infix 4 _\u2248\u21ba_ _\u2248\u1d2c_\n _\u2248\u21ba_ : \u2200 {R : `\u2605} (f g : EXP R) \u2192 \u2605\n _\u2248\u21ba_ = _\u2261_ on #_\n\n _\u2248\u1d2c_ : \u2200 {A R} (f g : \u21ba R A) \u2192 Set _\n _\u2248\u1d2c_ {A} f g = \u2200 (Adv : A \u2192 Bit) \u2192 \u27ea Adv \u00b7 f \u27eb \u2248\u21ba \u27ea Adv \u00b7 g \u27eb\n\n lem : \u2200 x \u2192 \u27e8 x \u229e\u27e9 (\u2047 `\u2124q) \u2248\u1d2c \u2047 _\n lem x Adv = sym (\u229e-stable x (Bit\u25b9\u2115 \u2218 Adv))\n\n -- \u2200 (A : \u2124q \u2192 Bit) \u2192 # (A \u2047)\n\nopen Fin.Modulo renaming (sucmod to [suc]; sucmod-inj to [suc]-inj)\n\n{-\nmodule \u2124q-implem (q-2 : \u2115) where\n q : \u2115\n q = 2 + q-2\n\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = zero\n\n [1] : \u2124q\n [1] = suc zero\n-}\nmodule \u2124q-implem (q-1 : \u2115) ([0]' [1]' : Fin (suc q-1)) where\n -- open Sum\n q : \u2115\n q = suc q-1\n\n \u2124q : \u2605\n \u2124q = Fin q\n\n \u03bc\u2124q : Explorable \u2124q\n \u03bc\u2124q = \u03bcFinSuc q-1\n\n sum\u2124q : Sum \u2124q\n sum\u2124q = sum \u03bc\u2124q\n\n [0] : \u2124q\n [0] = [0]'\n\n [1] : \u2124q\n [1] = [1]'\n\n [suc]-stable : SumStableUnder (sum \u03bc\u2124q) [suc]\n [suc]-stable = \u03bcFinSUI [suc] [suc]-inj\n\n _\u2115\u229e_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = m \u2115\u229e ([suc] n)\n\n \u2115\u229e-inj : \u2200 n {x y} \u2192 n \u2115\u229e x \u2261 n \u2115\u229e y \u2192 x \u2261 y\n \u2115\u229e-inj zero eq = eq\n \u2115\u229e-inj (suc n) eq = [suc]-inj (\u2115\u229e-inj n eq)\n\n \u2115\u229e-stable : \u2200 m \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u2115\u229e_ m)\n \u2115\u229e-stable m = \u03bcFinSUI (_\u2115\u229e_ m) (\u2115\u229e-inj m)\n\n _\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u229e n = Fin\u25b9\u2115 m \u2115\u229e n\n\n \u229e-inj : \u2200 m {x y} \u2192 m \u229e x \u2261 m \u229e y \u2192 x \u2261 y\n \u229e-inj m = \u2115\u229e-inj (Fin\u25b9\u2115 m)\n\n \u229e-stable : \u2200 m \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u229e_ m)\n \u229e-stable m = \u03bcFinSUI (_\u229e_ m) (\u229e-inj m)\n\n _\u2115\u22a0_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u22a0 n = [0]\n suc m \u2115\u22a0 n = n \u229e (m \u2115\u22a0 n)\n\n _\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u22a0 n = Fin\u25b9\u2115 m \u2115\u22a0 n\n\n _[^]\u2115_ : \u2124q \u2192 \u2115 \u2192 \u2124q\n m [^]\u2115 zero = [1]\n m [^]\u2115 suc n = m \u22a0 (m [^]\u2115 n)\n\n _[^]_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m [^] n = m [^]\u2115 (Fin\u25b9\u2115 n)\n\nmodule G-implem (p-1 q-1 : \u2115) (g' 0[p] 1[p] : Fin (suc p-1)) (0[q] 1[q] : Fin (suc q-1)) where\n open \u2124q-implem q-1 0[q] 1[q] public\n open \u2124q-implem p-1 0[p] 1[p] public using () renaming (\u2124q to G; _\u22a0_ to _\u2219_; _[^]\u2115_ to _^[p]_)\n\n g : G\n g = g'\n\n _^_ : G \u2192 \u2124q \u2192 G\n x ^ n = x ^[p] Fin\u25b9\u2115 n\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n {-\n g^-inj : \u2200 m n \u2192 g^ m \u2261 g^ n \u2192 m \u2261 n\n g^-inj = {!!}\n -}\n\nmodule G-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u03bc\u2124q : Explorable \u2124q)\n (\u229e-stable : \u2200 x \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u229e_ x))\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n where\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n open \u2124q-count \u2124q _\u229e_ \u03bc\u2124q \u229e-stable\n\n \u2047G : \u21ba `\u2124q G\n run\u21ba \u2047G x = g^ x\n\n #G : Count G\n #G f = #q (f \u2218 g^_)\n\n {-\n #G-\u2219 : \u2200 f m \u2192 #G (f \u2218 _\u2219_ m) \u2261 #G f\n #G-\u2219 f m = {!!}\n -}\n\nmodule El-Gamal-Generic\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (Message : \u2605)\n (_\u2219_ : G \u2192 Message \u2192 Message)\n\n -- Required for decryption\n (_\/_ : Message \u2192 G \u2192 Message)\n\n -- Required for the correctness proof\n (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n -- Required for the security proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : Explorable \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : Explorable R\u2090)\n where\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n -- g\u02e3 is the pk\n -- x is the sk\n\n R\u2093 : \u2605\n R\u2093 = \u2124q\n\n open Cipher.ElGamal.Generic Message \u2124q G g _^_ _\u2219_ _\/_\n\n functional-correctness : \u2200 x y m \u2192 Dec x (Enc (g^ x) m y) \u2261 m\n functional-correctness x y m rewrite comm-^ g x y | \/-\u2219 (g^ y ^ x) m = refl\n\n module IND-CPA = Game.IND-CPA PubKey SecKey Message CipherText R\u2091 R\u2096 R\u2090 R\u2093 KeyGen Enc\n open IND-CPA using (R)\n\n UnusedGame : (i : Bit) \u2192 IND-CPA.Adv \u2192 (Bit \u00d7 R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n UnusedGame i (m , d) (b , r\u2090 , x , y , z) = b == d r\u2090 g\u02e3 (g\u02b8 , \u03b6)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n \u03b4 = g\u02e3 ^ case i 0\u2192 y 1\u2192 z\n \u03b6 = \u03b4 \u2219 m r\u2090 g\u02e3 b\n\n module DDH = Game.DDH \u2124q G g _^_ R\u2090\n\n OTP\u2141 : (R\u2090 \u2192 G \u2192 Message) \u2192 (R\u2090 \u2192 G \u2192 G \u2192 Message \u2192 Bit) \u2192 R \u2192 Bit\n OTP\u2141 M d (r , x , y , z) = d r g\u02e3 g\u02b8 (g\u1dbb \u2219 M r g\u02e3)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n g\u1dbb = g^ z\n\n TrA : Bit \u2192 IND-CPA.Adv \u2192 DDH.Adv\n TrA b (m , d) r\u2090 g\u02e3 g\u02b8 g\u02e3\u02b8 = d r\u2090 g\u02e3 (g\u02b8 , g\u02e3\u02b8 \u2219 m r\u2090 g\u02e3 b)\n\n projM : IND-CPA.Adv \u2192 Bit \u2192 R\u2090 \u2192 G \u2192 Message\n projM (m , _) b r\u2090 g\u02e3 = m r\u2090 g\u02e3 b\n\n projD : IND-CPA.Adv \u2192 R\u2090 \u2192 G \u2192 G \u2192 Message \u2192 Bit\n projD (_ , d) r\u2090 g\u02e3 g\u02b8 g\u1dbb\u2219M = d r\u2090 g\u02e3 (g\u02b8 , g\u1dbb\u2219M)\n\n module Unused where\n like-\u2141 : Bit \u2192 IND-CPA.Game\n like-\u2141 b (m , d) (r\u2090 , x , y , _z) =\n d r\u2090 g\u02e3 (g\u02b8 , (g\u02e3 ^ y) \u2219 m r\u2090 g\u02e3 b)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n\n IND-CPA-\u2141\u2261like-\u2141 : IND-CPA.\u2141 \u2261 like-\u2141\n IND-CPA-\u2141\u2261like-\u2141 = refl\n\n -- R = R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q\n \u03bcR : Explorable R\n \u03bcR = \u03bcR\u2090 \u00d7-\u03bc \u03bc\u2124q \u00d7-\u03bc \u03bc\u2124q \u00d7-\u03bc \u03bc\u2124q\n\n #\u1d3f_ : Count R\n #\u1d3f_ = count \u03bcR\n\n #q_ : Count \u2124q\n #q_ = count \u03bc\u2124q\n\n _\u2248q_ : (f g : \u2124q \u2192 Bit) \u2192 \u2605\n f \u2248q g = #q f \u2261 #q g\n\n Re = (f g : R \u2192 Bit) \u2192 \u2605\n record Tra (_\u2248\u2080_ _\u2248\u2081_ : Re) (f g : R \u2192 Bit) : \u2605 where\n field\n h : R \u2192 Bit\n f\u2248\u2080h : f \u2248\u2080 h\n h\u2248\u2081g : h \u2248\u2081 g\n\n record _\u2248\u1d3f_ (f g : R \u2192 Bit) : \u2605 where\n constructor mk\n field\n un-\u2248\u1d3f : #\u1d3f f \u2261 #\u1d3f g\n open _\u2248\u1d3f_ public\n\n \u2248\u1d3f-trans : Transitive _\u2248\u1d3f_\n \u2248\u1d3f-trans (mk p) (mk q) = mk (\u2261.trans p q)\n\n module \u2248\u1d3f-Reasoning where\n open Trans-Reasoning _\u2248\u1d3f_ \u2248\u1d3f-trans public using () renaming (_\u2248\u27e8_\u27e9_ to _\u2248\u1d3f\u27e8_\u27e9_)\n infix 2 _\u220e\n\n _\u220e : \u2200 x \u2192 x \u2248\u1d3f x\n _ \u220e = mk refl\n\n module Proof\n (ddh-hyp : \u2200 A \u2192 DDH.\u2141\u2080 A \u2248\u1d3f DDH.\u2141\u2081 A)\n (otp-lem : \u2200 A m\u2080 m\u2081 \u2192 (\u03bb x \u2192 A (g^ x \u2219 m\u2080)) \u2248q (\u03bb x \u2192 A (g^ x \u2219 m\u2081)))\n (A : IND-CPA.Adv) (b : Bit)\n where\n OTP\u2141-lem : \u2200 d M\u2080 M\u2081 \u2192 OTP\u2141 M\u2080 d \u2248\u1d3f OTP\u2141 M\u2081 d\n OTP\u2141-lem d M\u2080 M\u2081 = mk (\n sum-ext \u03bcR\u2090 (\u03bb r \u2192\n sum-ext \u03bc\u2124q (\u03bb x \u2192\n sum-ext \u03bc\u2124q (\u03bb y \u2192\n pf r x y))))\n where\n pf : \u2200 r x y \u2192 count \u03bc\u2124q (\u03bb z \u2192 OTP\u2141 M\u2080 d (r , x , y , z))\n \u2261 count \u03bc\u2124q (\u03bb z \u2192 OTP\u2141 M\u2081 d (r , x , y , z))\n pf r x y rewrite otp-lem (d r (g^ x) (g^ y)) (M\u2080 r (g^ x)) (M\u2081 r (g^ x)) = refl\n\n -- moving this definition above OTP\u2141-lem breaks type-checking: ???\n \u00acb : Bit\n \u00acb = not b\n\n A\u1d47 = TrA b A\n A\u00ac\u1d47 = TrA \u00acb A\n\n pf0,5 : IND-CPA.\u2141 b A \u2257 DDH.\u2141\u2080 A\u1d47\n pf0,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n pf2,5 : DDH.\u2141\u2081 A\u1d47 \u2261 OTP\u2141 (projM A b) (projD A)\n pf2,5 = refl\n\n pf4,5 : IND-CPA.\u2141 \u00acb A \u2257 DDH.\u2141\u2080 A\u00ac\u1d47\n pf4,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n open \u2248\u1d3f-Reasoning\n\n final : IND-CPA.\u2141 b A \u2248\u1d3f IND-CPA.\u2141 \u00acb A\n final = IND-CPA.\u2141 b A\n \u2248\u1d3f\u27e8 mk (sum-ext \u03bcR (cong Bit\u25b9\u2115 \u2218 pf0,5)) \u27e9\n DDH.\u2141\u2080 A\u1d47\n \u2248\u1d3f\u27e8 ddh-hyp A\u1d47 \u27e9\n DDH.\u2141\u2081 A\u1d47\n \u2248\u1d3f\u27e8 OTP\u2141-lem (projD A) (projM A b) (projM A \u00acb) \u27e9\n DDH.\u2141\u2081 A\u00ac\u1d47\n \u2248\u1d3f\u27e8 mk (\u2261.sym (un-\u2248\u1d3f (ddh-hyp A\u00ac\u1d47))) \u27e9\n DDH.\u2141\u2080 A\u00ac\u1d47\n \u2248\u1d3f\u27e8 mk (\u2261.sym (sum-ext \u03bcR (cong Bit\u25b9\u2115 \u2218 pf4,5))) \u27e9\n IND-CPA.\u2141 \u00acb A\n \u220e\n\nmodule El-Gamal-Base\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n\n -- Required for decryption\n (_\/_ : G \u2192 G \u2192 G)\n\n -- Required for the correctness proof\n (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n {-\n (_\u207b\u00b9 : G \u2192 G)\n (\u207b\u00b9-inverse : \u2200 x \u2192 x \u207b\u00b9 \u2219 x \u2261 1G)\n -}\n\n -- Required for the proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : Explorable \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : Explorable R\u2090)\n where\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ G _\u2219_\n _\/_ \/-\u2219 comm-^\n dist-^-\u22a0 \u03bc\u2124q R\u2090 \u03bcR\u2090 public\n\n module OTP\u2141-LEM\n (otp-lem1 : \u2200 (A : G \u2192 Bit) m \u2192 (\u03bb x \u2192 A (g^ x \u2219 m)) \u2248q (\u03bb x \u2192 A (g^ x)))\n where\n\n otp-lem : \u2200 (A : G \u2192 Bit) m\u2080 m\u2081 \u2192 (\u03bb x \u2192 A (g^ x \u2219 m\u2080)) \u2248q (\u03bb x \u2192 A (g^ x \u2219 m\u2081))\n otp-lem A m\u2080 m\u2081 rewrite otp-lem1 A m\u2080 | otp-lem1 A m\u2081 = refl\n\nmodule El-Gamal-Hashed\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n -- (HKey : \u2605)\n (|M| : \u2115)\n (\u210b : {-HKey \u2192-} G \u2192 Bits |M|)\n\n -- (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n -- Required for the proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : Explorable \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : Explorable R\u2090)\n where\n\n Message = Bits |M|\n\n \u210b\u27e8_\u27e9\u2295_ : G \u2192 Message \u2192 Message\n \u210b\u27e8 \u03b4 \u27e9\u2295 m = \u210b \u03b4 \u2295 m\n\n _\/_ : Message \u2192 G \u2192 Message\n _\/_ m \u03b4 = \u210b \u03b4 \u2295 m\n{-\n\n \/-\u2219 : \u2200 x y \u2192 \u210b\u27e8 x \u27e9\u2295 y \/ x \u2261 y\n \/-\u2219 x y = {!!}\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ Message \u210b\u27e8_\u27e9\u2295_ _\/_ {!!} {!!}\n dist-^-\u22a0 sum\u2124q sum\u2124q-ext R\u2090 sumR\u2090 sumR\u2090-ext public\n -}\n\n {-\n OTP\u2141-lem : \u2200 d M\u2080 M\u2081 \u2192 OTP\u2141 M\u2080 d \u2248\u1d3f OTP\u2141 M\u2081 d\n OTP\u2141-lem = ?\n -}\n\nmodule \u27e8\u2124p\u27e9\u2605 p-3 {- p is prime -} (`R\u2090 : `\u2605) where\n p : \u2115\n p = 3 + p-3\n\n q : \u2115\n q = p \u2238 1\n\n module G = G-implem p q (## 2) (## 0) (## 1) (## 0) (## 1)\n open G\n\n postulate\n _\u207b\u00b9 : G \u2192 G\n\n _\/_ : G \u2192 G \u2192 G\n x \/ y = x \u2219 (y \u207b\u00b9)\n\n postulate\n \/-\u2022 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y\n dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y\n \u22a0-comm : \u2200 x y \u2192 x \u22a0 y \u2261 y \u22a0 x\n\n comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x\n comm-^ \u03b1 x y = (\u03b1 ^ x)^ y\n \u2261\u27e8 sym (dist-^-\u22a0 \u03b1 x y) \u27e9\n \u03b1 ^ (x \u22a0 y)\n \u2261\u27e8 cong (_^_ \u03b1) (\u22a0-comm x y) \u27e9\n \u03b1 ^ (y \u22a0 x)\n \u2261\u27e8 dist-^-\u22a0 \u03b1 y x \u27e9\n (\u03b1 ^ y)^ x\n \u220e\n where open \u2261-Reasoning\n\n open \u2124q-count \u2124q _\u229e_ \u03bc\u2124q \u229e-stable\n\n \u03bcR\u2090 : Explorable (El `R\u2090)\n \u03bcR\u2090 = \u03bcU \u03bc\u2124q `R\u2090\n\n R\u2090 = El `R\u2090\n sumR\u2090 = sum \u03bcR\u2090\n sumR\u2090-ext = sum-ext \u03bcR\u2090\n\n module EB = El-Gamal-Base _ _\u22a0_ G g _^_ _\u2219_ _\/_ \/-\u2022 comm-^ dist-^-\u22a0 \u03bc\u2124q R\u2090 \u03bcR\u2090\n open EB hiding (g^_)\n\n otp-base-lem : \u2200 (A : G \u2192 Bit) m \u2192 (A \u2218 g^_) \u2248q (A \u2218 g^_ \u2218 _\u229e_ m)\n otp-base-lem A m = \u229e-stable m (Bit\u25b9\u2115 \u2218 A \u2218 g^_)\n\n postulate\n ddh-hyp : (A : DDH.Adv) \u2192 DDH.\u2141\u2080 A \u2248\u1d3f DDH.\u2141\u2081 A\n otp-lem : \u2200 (A : G \u2192 Bit) m \u2192 (\u03bb x \u2192 A (g^ x \u2219 m)) \u2248q (\u03bb x \u2192 A (g^ x))\n\n\n open OTP\u2141-LEM otp-lem\n\n {-\n final : \u2200 A \u2192 IND-CPA.\u2141 A 0b \u2248\u1d3f IND-CPA.\u2141 A 1b\n final A = Proof.final ddh-hyp OTP\u2141-lem A 0b\n -}\n\nmodule \u27e8\u212411\u27e9\u2605 = \u27e8\u2124p\u27e9\u2605 (11 \u2238 3)\n `X -- the amount of adversary randomness\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"d4894223fb978a68bdda86d285ca1023e8d45235","subject":"Removed unnecessary import.","message":"Removed unnecessary import.\n\nIgnore-this: e6495fab2648ecac4ecc85685b6b158a\n\ndarcs-hash:20100604032100-3bd4e-e89550df44e4c9bb127867dfcd4e3a8f4e6a9bc8.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Examples\/Division\/IsDIV-ER.agda","new_file":"Examples\/Division\/IsDIV-ER.agda","new_contents":"------------------------------------------------------------------------------\n-- The division satisfies the specification\n------------------------------------------------------------------------------\n\n-- This module formalize the division algorithm following [1].\n-- [1] Peter Dybjer. Program verification in a logical theory of\n-- constructions. In Jean-Pierre Jouannaud, editor. Functional\n-- Programming Languages and Computer Architecture, volume 201 of\n-- LNCS, 1985, pages 334-349. Appears in revised form as Programming\n-- Methodology Group Report 26, June 1986.\n\nmodule Examples.Division.IsDIV-ER where\n\nopen import LTC.Minimal\n\nopen import Examples.Division\nopen import Examples.Division.EquationsER\nopen import Examples.Division.IsCorrectER\nopen import Examples.Division.IsN-ER\nopen import Examples.Division.Specification\n\nopen import LTC.Data.N\nopen import LTC.Data.N.Induction.WellFoundedPCF\nopen import LTC.Function.ArithmeticPCF\nopen import LTC.Function.Arithmetic.PropertiesPCF-ER\nopen import LTC.Relation.InequalitiesPCF\nopen import LTC.Relation.Inequalities.PropertiesPCF-ER\n\n------------------------------------------------------------------------------\n\n-- The division result satifies the specification DIV\n-- when the dividend is less than the divisor\ndiv-x LT i j \u2192 DIV i j (div i j)\ndiv-x N j \u2192\n ({i' : D} \u2192 N i' \u2192 LT i' i \u2192 DIV i' j (div i' j)) \u2192\n GT j zero \u2192\n GE i j \u2192\n DIV i j (div i j)\ndiv-x\u2265y-DIV {i} {j} Ni Nj accH j>0 i\u2265j =\n (div-x\u2265y-N ih i\u2265j) , div-x\u2265y-correct Ni Nj ih i\u2265j\n\n where\n -- The inductive hypothesis on 'i - j'.\n ih : DIV (i - j) j (div (i - j) j)\n ih = accH {i - j}\n (minus-N Ni Nj )\n (x\u2265y\u2192y>0\u2192x-y0)\n\n------------------------------------------------------------------------------\n-- The division satisfies the specification\n\n-- We do the well-founded induction on 'i' and we keep 'j' fixed.\ndiv-DIV : {i j : D} \u2192 N i \u2192 N j \u2192 GT j zero \u2192 DIV i j (div i j)\ndiv-DIV {j = j} Ni Nj j>0 = wfIndN-LT P iStep Ni\n\n where\n P : D \u2192 Set\n P d = DIV d j (div d j)\n\n -- The inductive step doesn't use the variable 'i'\n -- (nor 'Ni'). To make this\n -- clear we write down the inductive step using the variables\n -- 'm' and 'n'.\n iStep : {n : D} \u2192 N n \u2192\n (accH : {m : D} \u2192 N m \u2192 LT m n \u2192 P m) \u2192\n P n\n iStep {n} Nn accH =\n [ div-x0 ] (x LT i j \u2192 DIV i j (div i j)\ndiv-x N j \u2192\n ({i' : D} \u2192 N i' \u2192 LT i' i \u2192 DIV i' j (div i' j)) \u2192\n GT j zero \u2192\n GE i j \u2192\n DIV i j (div i j)\ndiv-x\u2265y-DIV {i} {j} Ni Nj accH j>0 i\u2265j =\n (div-x\u2265y-N ih i\u2265j) , div-x\u2265y-correct Ni Nj ih i\u2265j\n\n where\n -- The inductive hypothesis on 'i - j'.\n ih : DIV (i - j) j (div (i - j) j)\n ih = accH {i - j}\n (minus-N Ni Nj )\n (x\u2265y\u2192y>0\u2192x-y0)\n\n------------------------------------------------------------------------------\n-- The division satisfies the specification\n\n-- We do the well-founded induction on 'i' and we keep 'j' fixed.\ndiv-DIV : {i j : D} \u2192 N i \u2192 N j \u2192 GT j zero \u2192 DIV i j (div i j)\ndiv-DIV {j = j} Ni Nj j>0 = wfIndN-LT P iStep Ni\n\n where\n P : D \u2192 Set\n P d = DIV d j (div d j)\n\n -- The inductive step doesn't use the variable 'i'\n -- (nor 'Ni'). To make this\n -- clear we write down the inductive step using the variables\n -- 'm' and 'n'.\n iStep : {n : D} \u2192 N n \u2192\n (accH : {m : D} \u2192 N m \u2192 LT m n \u2192 P m) \u2192\n P n\n iStep {n} Nn accH =\n [ div-x0 ] (x>_)\nopen import FFI.JS.Check\n using (check!)\n--renaming (check to check?)\n renaming (warn-check to check?)\n\nopen import FFI.JS.BigI\nopen import Data.List.Base using (List; foldr)\nopen import Algebra.Raw\nopen import Algebra.Group\nopen import Algebra.Group.Homomorphism\nopen import Algebra.Field\n\n-- TODO carry on a primality proof of q\nmodule Crypto.JS.BigI.FiniteField (q : BigI) where\n\n-- The constructor mk is not exported.\nprivate\n module Internals where\n data \u2124[_] : Set where\n mk : BigI \u2192 \u2124[_]\n\n mk-inj : \u2200 {x y : BigI} \u2192 \u2124[_].mk x \u2261 mk y \u2192 x \u2261 y\n mk-inj refl = refl\n\nopen Internals public using (\u2124[_])\nopen Internals\n\n\u2124q : Set\n\u2124q = \u2124[_]\n\nprivate\n mod-q : BigI \u2192 \u2124q\n mod-q x = mk (mod x q)\n\n-- There are two ways to go from BigI to \u2124q: BigI\u25b9\u2124[ q ] and mod-q\n-- Use BigI\u25b9\u2124[ q ] for untrusted input data and mod-q for internal\n-- computation.\nBigI\u25b9\u2124[_] : BigI \u2192 JS[ \u2124q ]\nBigI\u25b9\u2124[_] x =\n-- Console.log \"BigI\u25b9\u2124[_]\"\n check! \"below the modulus\" (x >\n check! \"positivity\" (x \u2265I 0I)\n (\u03bb _ \u2192 \"Should be positive: \" ++ toString x ++ \" < 0\") >>\n return (mk x)\n\ncheck-non-zero : \u2124q \u2192 BigI\ncheck-non-zero (mk x) = -- trace-call \"check-non-zero \"\n check? (x >I 0I) (\u03bb _ \u2192 \"Should be non zero\") x\n\n\u2124[_]\u25b9BigI : \u2124q \u2192 BigI\n\u2124[_]\u25b9BigI (mk x) = x\n\n0# 1# : \u2124q\n0# = mk 0I\n1# = mk 1I\n\n-- One could choose here to return a dummy value when 0 is given.\n-- Instead we throw an exception which in some circumstances could\n-- be bad if the runtime semantics is too eager.\n1\/_ : \u2124q \u2192 \u2124q\n1\/ x = mk (modInv (check-non-zero x) q)\n\n_^I_ : \u2124q \u2192 BigI \u2192 \u2124q\nmk x ^I y = mk (modPow x y q)\n\n\u2124q\u25b9BigI = \u2124[_]\u25b9BigI\nBigI\u25b9\u2124q = BigI\u25b9\u2124[_]\n\n_\u2297I_ : \u2124q \u2192 BigI \u2192 \u2124q\nx \u2297I y = mod-q (multiply (\u2124q\u25b9BigI x) y)\n\n_+_ _\u2212_ _*_ _\/_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n\nx + y = mod-q (add (\u2124q\u25b9BigI x) (\u2124q\u25b9BigI y))\nx \u2212 y = mod-q (subtract (\u2124q\u25b9BigI x) (\u2124q\u25b9BigI y))\nx * y = x \u2297I \u2124q\u25b9BigI y\nx \/ y = x * 1\/ y\n\n*-def : _*_ \u2261 (\u03bb x y \u2192 x \u2297I \u2124q\u25b9BigI y)\n*-def = refl\n\n0\u2212_ : \u2124q \u2192 \u2124q\n0\u2212 x = mod-q (negate (\u2124q\u25b9BigI x))\n\nsum prod : List \u2124q \u2192 \u2124q\nsum = foldr _+_ 0#\nprod = foldr _*_ 1#\n\ninstance\n \u2124[_]-Eq? : Eq? \u2124q\n \u2124[_]-Eq? = record\n { _==_ = _=='_\n ; \u2261\u21d2== = \u2261\u21d2=='\n ; ==\u21d2\u2261 = ==\u21d2\u2261' }\n where\n _=='_ : \u2124q \u2192 \u2124q \u2192 \ud835\udfda\n mk x ==' mk y = x == y\n \u2261\u21d2==' : \u2200 {x y} \u2192 x \u2261 y \u2192 \u2713 (x ==' y)\n \u2261\u21d2==' {mk x} {mk y} p = \u2261\u21d2== (mk-inj p)\n ==\u21d2\u2261' : \u2200 {x y} \u2192 \u2713 (x ==' y) \u2192 x \u2261 y\n ==\u21d2\u2261' {mk x} {mk y} p = ap mk (==\u21d2\u2261 p)\n\n\u2124q-Eq? = \u2124[_]-Eq?\n\n+-mon-ops : Monoid-Ops \u2124q\n+-mon-ops = _+_ , 0#\n\n+-grp-ops : Group-Ops \u2124q\n+-grp-ops = +-mon-ops , 0\u2212_\n\n*-mon-ops : Monoid-Ops \u2124q\n*-mon-ops = _*_ , 1#\n\n*-grp-ops : Group-Ops \u2124q\n*-grp-ops = *-mon-ops , 1\/_\n\nfld-ops : Field-Ops \u2124q\nfld-ops = +-grp-ops , *-grp-ops\n\npostulate fld-struct : Field-Struct fld-ops\n\nfld : Field \u2124q\nfld = fld-ops , fld-struct\n\nmodule fld = Field fld\n\n-- open fld using (+-grp) public\n\n\u2124[_]+-grp : Group \u2124q\n\u2124[_]+-grp = fld.+-grp\n\n\u2124q+-grp : Group \u2124q\n\u2124q+-grp = fld.+-grp\n\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Type.Eq using (_==_; Eq?; \u2261\u21d2==; ==\u21d2\u2261)\nopen import Data.Two.Base using (\ud835\udfda; \u2713)\nopen import Relation.Binary.PropositionalEquality.Base using (_\u2261_; refl; ap)\nopen import FFI.JS using (JS[_]; _++_; return; _>>_)\nopen import FFI.JS.Check\n using (check!)\n renaming (check to check?)\n--renaming (warn-check to check?)\n\nopen import FFI.JS.BigI\nopen import Data.List.Base using (List; foldr)\nopen import Algebra.Raw\nopen import Algebra.Group\nopen import Algebra.Group.Homomorphism\nopen import Algebra.Field\n\n-- TODO carry on a primality proof of q\nmodule Crypto.JS.BigI.FiniteField (q : BigI) where\n\n-- The constructor mk is not exported.\nprivate\n module Internals where\n data \u2124[_] : Set where\n mk : BigI \u2192 \u2124[_]\n\n mk-inj : \u2200 {x y : BigI} \u2192 \u2124[_].mk x \u2261 mk y \u2192 x \u2261 y\n mk-inj refl = refl\n\nopen Internals public using (\u2124[_])\nopen Internals\n\n\u2124q : Set\n\u2124q = \u2124[_]\n\nprivate\n mod-q : BigI \u2192 \u2124q\n mod-q x = mk (mod x q)\n\n-- There are two ways to go from BigI to \u2124q: BigI\u25b9\u2124[ q ] and mod-q\n-- Use BigI\u25b9\u2124[ q ] for untrusted input data and mod-q for internal\n-- computation.\nBigI\u25b9\u2124[_] : BigI \u2192 JS[ \u2124q ]\nBigI\u25b9\u2124[_] x =\n-- Console.log \"BigI\u25b9\u2124[_]\"\n check! \"below the modulus\" (x >\n check! \"positivity\" (x \u2265I 0I)\n (\u03bb _ \u2192 \"Should be positive: \" ++ toString x ++ \" < 0\") >>\n return (mk x)\n\ncheck-non-zero : \u2124q \u2192 BigI\ncheck-non-zero (mk x) = -- trace-call \"check-non-zero \"\n check? (x >I 0I) (\u03bb _ \u2192 \"Should be non zero\") x\n\n\u2124[_]\u25b9BigI : \u2124q \u2192 BigI\n\u2124[_]\u25b9BigI (mk x) = x\n\n0# 1# : \u2124q\n0# = mk 0I\n1# = mk 1I\n\n-- One could choose here to return a dummy value when 0 is given.\n-- Instead we throw an exception which in some circumstances could\n-- be bad if the runtime semantics is too eager.\n1\/_ : \u2124q \u2192 \u2124q\n1\/ x = mk (modInv (check-non-zero x) q)\n\n_^I_ : \u2124q \u2192 BigI \u2192 \u2124q\nmk x ^I y = mk (modPow x y q)\n\n\u2124q\u25b9BigI = \u2124[_]\u25b9BigI\nBigI\u25b9\u2124q = BigI\u25b9\u2124[_]\n\n_\u2297I_ : \u2124q \u2192 BigI \u2192 \u2124q\nx \u2297I y = mod-q (multiply (\u2124q\u25b9BigI x) y)\n\n_+_ _\u2212_ _*_ _\/_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n\nx + y = mod-q (add (\u2124q\u25b9BigI x) (\u2124q\u25b9BigI y))\nx \u2212 y = mod-q (subtract (\u2124q\u25b9BigI x) (\u2124q\u25b9BigI y))\nx * y = x \u2297I \u2124q\u25b9BigI y\nx \/ y = x * 1\/ y\n\n*-def : _*_ \u2261 (\u03bb x y \u2192 x \u2297I \u2124q\u25b9BigI y)\n*-def = refl\n\n0\u2212_ : \u2124q \u2192 \u2124q\n0\u2212 x = mod-q (negate (\u2124q\u25b9BigI x))\n\nsum prod : List \u2124q \u2192 \u2124q\nsum = foldr _+_ 0#\nprod = foldr _*_ 1#\n\ninstance\n \u2124[_]-Eq? : Eq? \u2124q\n \u2124[_]-Eq? = record\n { _==_ = _=='_\n ; \u2261\u21d2== = \u2261\u21d2=='\n ; ==\u21d2\u2261 = ==\u21d2\u2261' }\n where\n _=='_ : \u2124q \u2192 \u2124q \u2192 \ud835\udfda\n mk x ==' mk y = x == y\n \u2261\u21d2==' : \u2200 {x y} \u2192 x \u2261 y \u2192 \u2713 (x ==' y)\n \u2261\u21d2==' {mk x} {mk y} p = \u2261\u21d2== (mk-inj p)\n ==\u21d2\u2261' : \u2200 {x y} \u2192 \u2713 (x ==' y) \u2192 x \u2261 y\n ==\u21d2\u2261' {mk x} {mk y} p = ap mk (==\u21d2\u2261 p)\n\n\u2124q-Eq? = \u2124[_]-Eq?\n\n+-mon-ops : Monoid-Ops \u2124q\n+-mon-ops = _+_ , 0#\n\n+-grp-ops : Group-Ops \u2124q\n+-grp-ops = +-mon-ops , 0\u2212_\n\n*-mon-ops : Monoid-Ops \u2124q\n*-mon-ops = _*_ , 1#\n\n*-grp-ops : Group-Ops \u2124q\n*-grp-ops = *-mon-ops , 1\/_\n\nfld-ops : Field-Ops \u2124q\nfld-ops = +-grp-ops , *-grp-ops\n\npostulate fld-struct : Field-Struct fld-ops\n\nfld : Field \u2124q\nfld = fld-ops , fld-struct\n\nmodule fld = Field fld\n\n-- open fld using (+-grp) public\n\n\u2124[_]+-grp : Group \u2124q\n\u2124[_]+-grp = fld.+-grp\n\n\u2124q+-grp : Group \u2124q\n\u2124q+-grp = fld.+-grp\n\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"7710ed80e465665babad11564d7f05f8642d6a90","subject":"elgamal","message":"elgamal\n","repos":"crypto-agda\/crypto-agda","old_file":"elgamal.agda","new_file":"elgamal.agda","new_contents":"{-# OPTIONS --without-K #-}\n{-# OPTIONS --copatterns #-}\nopen import Type\nopen import Function\nopen import Function.Extensionality\nopen import Data.Product.NP\nopen import Data.Unit\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc; Fin\u25b9\u2115) renaming (#_ to ##_)\nopen import Data.Nat.NP hiding (_^_; _==_)\nopen import Data.Nat.Distance\nopen import Data.Bit\nopen import Data.Zero\nopen import Data.Two\nopen import Relation.Binary.NP\nopen import Data.Bits hiding (_==_)\nopen import Relation.Binary.PropositionalEquality.NP as \u2261 hiding (_\u2219_)\nopen import HoTT\nopen Equivalences\n\nopen import Explore.Core\nopen import Explore.Explorable\nopen import Explore.Universe.Type {\ud835\udfd8}\nopen import Explore.Universe.Base\nopen import Explore.Sum\nopen import Explore.Product\nimport Explore.GroupHomomorphism as GH\n\nimport Game.DDH\nimport Game.IND-CPA\nimport Cipher.ElGamal.Generic\n\nmodule elgamal where\n\n{-\ndata `\u2605 : \u2605 where\n `\u22a4 : `\u2605\n `X : `\u2605\n _`\u00d7_ : `\u2605 \u2192 `\u2605 \u2192 `\u2605\ninfixr 2 _`\u00d7_\n\nmodule Univ (X : \u2605) where\n El : `\u2605 \u2192 \u2605\n El `\u22a4 = \u22a4\n El `X = X\n El (u\u2080 `\u00d7 u\u2081) = El u\u2080 \u00d7 El u\u2081\n\n record \u21ba (R : `\u2605) (A : \u2605) : \u2605 where\n constructor mk\n field\n run\u21ba : El R \u2192 A\n open \u21ba public\n\n EXP : (R : `\u2605) \u2192 \u2605\n EXP R = \u21ba R Bit\n\n Det : \u2605 \u2192 \u2605\n Det = \u21ba `\u22a4\n\n \u03bcU : Explorable X \u2192 \u2200 u \u2192 Explorable (El u)\n \u03bcU \u03bcX `\u22a4 = \u03bc\u22a4\n \u03bcU \u03bcX `X = \u03bcX\n \u03bcU \u03bcX (u\u2080 `\u00d7 u\u2081) = \u03bcU \u03bcX u\u2080 \u00d7-\u03bc \u03bcU \u03bcX u\u2081\n\nmodule \u2124q-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u03bc\u2124q : Explorable \u2124q)\n (\u229e-stable : \u2200 x \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u229e_ x))\n where\n\n -- open Sum\n open Univ \u2124q public\n open `\u2605 public renaming (`X to `\u2124q)\n\n #_ : \u2200 {u} \u2192 \u21ba u Bit \u2192 \u2115\n #_ {u} f = count (\u03bcU \u03bc\u2124q u) (run\u21ba f)\n\n #q_ : Count \u2124q\n #q_ = count \u03bc\u2124q\n\n \u2047 : \u2200 R \u2192 \u21ba R (El R)\n run\u21ba (\u2047 _) = id\n\n pure\u21ba : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n run\u21ba (pure\u21ba x) r = x -- turning r to _ produce an error\n\n \u27ea_\u27eb : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n \u27ea_\u27eb = pure\u21ba\n\n {-\n \u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n \u27ea_\u27eb\u1d30 = pure\u1d30\n -}\n\n map\u21ba : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n run\u21ba (map\u21ba f x) r = f (run\u21ba x r)\n\n infixl 4 _\u229b_\n _\u229b_ : \u2200 {R S A B} \u2192 \u21ba R (A \u2192 B) \u2192 \u21ba S A \u2192 \u21ba (R `\u00d7 S) B\n run\u21ba (af \u229b ax) rs = run\u21ba af (fst rs) (run\u21ba ax (snd rs))\n\n \u27ea_\u00b7_\u27eb : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n \u27ea f \u00b7 x \u27eb = map\u21ba f x\n\n \u27ea_\u00b7_\u00b7_\u27eb : \u2200 {A B C} {R S} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba R A \u2192 \u21ba S B \u2192 \u21ba (R `\u00d7 S) C\n \u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n _\u27e8\u229e\u27e9_ : \u2200 {R S} \u2192 \u21ba R \u2124q \u2192 \u21ba S \u2124q \u2192 \u21ba (R `\u00d7 S) \u2124q\n x \u27e8\u229e\u27e9 y = \u27ea _\u229e_ \u00b7 x \u00b7 y \u27eb\n\n \u27e8_\u229e\u27e9_ : \u2200 {R} \u2192 \u2124q \u2192 \u21ba R \u2124q \u2192 \u21ba R \u2124q\n \u27e8 x \u229e\u27e9 y = \u27ea _\u229e_ x \u00b7 y \u27eb\n\n infix 4 _\u2248\u21ba_ _\u2248\u1d2c_\n _\u2248\u21ba_ : \u2200 {R : `\u2605} (f g : EXP R) \u2192 \u2605\n _\u2248\u21ba_ = _\u2261_ on #_\n\n _\u2248\u1d2c_ : \u2200 {A R} (f g : \u21ba R A) \u2192 Set _\n _\u2248\u1d2c_ {A} f g = \u2200 (Adv : A \u2192 Bit) \u2192 \u27ea Adv \u00b7 f \u27eb \u2248\u21ba \u27ea Adv \u00b7 g \u27eb\n\n lem : \u2200 x \u2192 \u27e8 x \u229e\u27e9 (\u2047 `\u2124q) \u2248\u1d2c \u2047 _\n lem x Adv = sym (\u229e-stable x (Bit\u25b9\u2115 \u2218 Adv))\n\n -- \u2200 (A : \u2124q \u2192 Bit) \u2192 # (A \u2047)\n-}\n\nmodule El-Gamal-Generic\n (\u2124q\u1d41 : U)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 El \u2124q\u1d41 \u2192 G)\n (Message : \u2605)\n (_\u2219_ : G \u2192 Message \u2192 Message)\n (_\/_ : Message \u2192 G \u2192 Message)\n (R\u2090\u1d41 : U)\n where\n \u2124q = El \u2124q\u1d41\n R\u2090 = El R\u2090\u1d41\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n -- g\u02e3 is the pk\n -- x is the sk\n\n R\u2093 : \u2605\n R\u2093 = \u2124q\n\n open Cipher.ElGamal.Generic Message Message \u2124q G g _^_ _\u2219_ _\/_\n\n module IND-CPA = Game.IND-CPA PubKey SecKey Message CipherText R\u2091 R\u2096 R\u2090 R\u2093 KeyGen Enc\n open IND-CPA renaming (R to R')\n\n -- R' = (R\u2090 \u00d7 R\u2096 \u00d7 R\u2091 \u00d7 R\u2093)\n R'\u1d41 = R\u2090\u1d41 \u00d7\u1d41 \u2124q\u1d41 \u00d7\u1d41 \u2124q\u1d41 \u00d7\u1d41 \u2124q\u1d41\n -- R' = El R'\u1d41\n sumR' = sum R'\u1d41\n R = \ud835\udfda \u00d7 R'\n R\u1d41 = \ud835\udfda\u1d41 \u00d7\u1d41 R'\u1d41\n sumR = sum R\u1d41\n \n sumExtR\u2090 = sum-ext R\u2090\u1d41\n sumExt\u2124q = sum-ext \u2124q\u1d41\n sumHomR' = sum-hom R'\u1d41\n sumExtR' = sum-ext R'\u1d41\n \n IND-CPA-\u2141 : IND-CPA.Adversary \u2192 R \u2192 \ud835\udfda\n IND-CPA-\u2141 = IND-CPA.game\n \n module DDH = Game.DDH \u2124q G g _^_ (\ud835\udfda \u00d7 R\u2090)\n\n DDH-\u2141\u2080 DDH-\u2141\u2081 : DDH.Adv \u2192 R \u2192 \ud835\udfda\n DDH-\u2141\u2080 A (b , r\u2090 , g\u02e3 , g\u02b8 , g\u1dbb) = DDH.\u2141\u2080 A ((b , r\u2090) , g\u02e3 , g\u02b8 , g\u1dbb)\n DDH-\u2141\u2081 A (b , r\u2090 , g\u02e3 , g\u02b8 , g\u1dbb) = DDH.\u2141\u2081 A ((b , r\u2090) , g\u02e3 , g\u02b8 , g\u1dbb)\n \n transformAdv : IND-CPA.Adversary \u2192 DDH.Adv\n transformAdv (m , d) (b , r\u2090) g\u02e3 g\u02b8 g\u1dbb = b == b\u2032\n where mb = m r\u2090 g\u02e3 b\n c = (g\u02b8 , g\u1dbb \u2219 mb)\n b\u2032 = d r\u2090 g\u02e3 c\n\n #q_ : Count \u2124q\n #q_ = count \u2124q\u1d41\n\n _\u2248q_ : (f g : \u2124q \u2192 \u2115) \u2192 \u2605\n f \u2248q g = sum \u2124q\u1d41 f \u2261 sum \u2124q\u1d41 g\n\n OTP-LEM = \u2200 (O : Message \u2192 \u2115) m\u2080 m\u2081 \u2192\n (\u03bb x \u2192 O((g ^ x) \u2219 m\u2080)) \u2248q (\u03bb x \u2192 O((g ^ x) \u2219 m\u2081))\n\n 1\/2 : R \u2192 \ud835\udfda\n 1\/2 (b , _) = b\n\n module _ {S} where \n _\u2248\u1d3f_ : (f g : R \u2192 S) \u2192 \u2605\n f \u2248\u1d3f g = \u2200 (X : S \u2192 \u2115) \u2192 sumR (X \u2218 f) \u2261 sumR (X \u2218 g) \n\n Dist : (f g : R \u2192 \ud835\udfda) \u2192 \u2115\n Dist f g = dist (count R\u1d41 f) (count R\u1d41 g)\n\n dist-cong : \u2200 {f g h i} \u2192 f \u2248\u1d3f g \u2192 h \u2248\u1d3f i \u2192 Dist f h \u2261 Dist g i\n dist-cong {f}{g}{h}{i} f\u2248g h\u2248i = ap\u2082 dist (f\u2248g \ud835\udfda\u25b9\u2115) (h\u2248i \ud835\udfda\u25b9\u2115)\n\n OTP-\u2141 : IND-CPA.Adversary \u2192 R \u2192 \ud835\udfda\n OTP-\u2141 (m , d) (b , r\u2090 , x , y , z) = b == d r\u2090 g\u02e3 (g\u02b8 , g\u1dbb \u2219 m r\u2090 g\u02e3 b)\n where g\u02e3 = g ^ x\n g\u02b8 = g ^ y\n g\u1dbb = g ^ z\n\n module Proof (otp-lem : OTP-LEM)\n\n (A : IND-CPA.Adversary) where\n\n module A = IND-CPA.Adversary A\n A\u2032 = transformAdv A\n\n goal4 : 1\/2 \u2248\u1d3f OTP-\u2141 A\n goal4 X = sumR' (\u03bb _ \u2192 X 0b) + sumR' (\u03bb _ \u2192 X 1b)\n \u2261\u27e8 sym (sumHomR' _ _) \u27e9\n sumR' (\u03bb _ \u2192 X 0b + X 1b)\n \u2261\u27e8 sumExtR' (lemma \u2218 B 0b) \u27e9\n sumR' (Y 0b 0b +\u00b0 Y 1b 0b)\n \u2261\u27e8 sumHomR' _ _ \u27e9\n sumR' (Y 0b 0b) + sumR' (Y 1b 0b)\n \u2261\u27e8 cong (_+_ (sumR' (Y 0b 0b))) lemma2 \u27e9\n sumR' (Y 0b 0b) + sumR' (Y 1b 1b)\n \u220e\n where\n open \u2261-Reasoning\n\n B : \ud835\udfda \u2192 R' \u2192 \ud835\udfda\n B b (r\u2090 , x , y , z) = A.b\u2032 r\u2090 (g ^ x) (g ^ y , (g ^ z) \u2219 A.m r\u2090 (g ^ x) b)\n\n Y = \u03bb bb bbb r \u2192 X (bb == B bbb r)\n\n lemma : \u2200 b \u2192 X 0b + X 1b \u2261 X (0b == b) + X (1b == b)\n lemma 1b = refl\n lemma 0b = \u2115\u00b0.+-comm (X 0b) _\n\n lemma2 : sumR' (Y 1b 0b) \u2261 sumR' (Y 1b 1b)\n lemma2 = sumExtR\u2090 \u03bb r\u2090 \u2192\n sumExt\u2124q \u03bb x \u2192\n sumExt\u2124q \u03bb y \u2192\n otp-lem (\u03bb m \u2192 X (A.b\u2032 r\u2090 (g ^ x) (g ^ y , m))) (A.m r\u2090 (g ^ x) 0\u2082) (A.m r\u2090 (g ^ x) 1\u2082)\n\n {-\n otp-lem (\u03bb m \u2192 snd A r\u2090 (g ^ x) (g ^ y , m))\n (fst A r\u2090 (g ^ x) 1b)\n (fst A r\u2090 (g ^ x) 0b)\n (\u03bb c \u2192 X (1b == c))\n -}\n\n module absDist {DIST : \u2605}(Dist : (f g : R \u2192 \ud835\udfda) \u2192 DIST)\n (dist-cong : \u2200 {f h i} \u2192 h \u2248\u1d3f i \u2192 Dist f h \u2261 Dist f i) where\n goal : Dist (IND-CPA-\u2141 A) 1\/2 \u2261 Dist (DDH-\u2141\u2080 A\u2032) (DDH-\u2141\u2081 A\u2032)\n goal = Dist (IND-CPA-\u2141 A) 1\/2\n \u2261\u27e8 refl \u27e9\n Dist (DDH-\u2141\u2080 A\u2032) 1\/2\n \u2261\u27e8 dist-cong goal4 \u27e9\n Dist (DDH-\u2141\u2080 A\u2032) (OTP-\u2141 A)\n \u2261\u27e8 refl \u27e9\n Dist (DDH-\u2141\u2080 A\u2032) (DDH-\u2141\u2081 A\u2032)\n \u220e\n where open \u2261-Reasoning\n\n open absDist Dist (\u03bb {f}{h}{i} \u2192 dist-cong {f}{f}{h}{i} (\u03bb _ \u2192 refl)) public\n\nmodule El-Gamal-Base\n (\u2124q\u1d41 : U)\n (\u2124q-grp : GH.Group (El \u2124q\u1d41))\n (G : \u2605)\n (G-grp : GH.Group G)\n (g : G)\n (_^_ : G \u2192 El \u2124q\u1d41 \u2192 G)\n (^-gh : GH.GroupHomomorphism \u2124q-grp G-grp (_^_ g))\n (dlog : (b y : G) \u2192 El \u2124q\u1d41)\n (dlog-ok : (b y : G) \u2192 b ^ dlog b y \u2261 y)\n (R\u2090\u1d41 : U)\n (R\u2090 : El R\u2090\u1d41)\n {{_ : FunExt}}\n {{_ : UA}}\n (open GH.Group \u2124q-grp renaming (_\u2219_ to _\u229e_))\n (\u229e-is-equiv : \u2200 k \u2192 Is-equiv (flip _\u229e_ k))\n where\n\n open GH.Group G-grp using (_\u2219_) renaming (-_ to _\u207b\u00b9)\n\n _\/_ : G \u2192 G \u2192 G\n x \/ y = x \u2219 (y \u207b\u00b9)\n\n open El-Gamal-Generic \u2124q\u1d41 G g _^_ G _\u2219_ _\/_ R\u2090\u1d41 public\n\n otp-lem : \u2200 (O : G \u2192 \u2115) m\u2080 m\u2081 \u2192\n (\u03bb x \u2192 O((g ^ x) \u2219 m\u2080)) \u2248q (\u03bb x \u2192 O((g ^ x) \u2219 m\u2081))\n otp-lem = GH.thm \u2124q-grp G-grp (_^_ g) (explore \u2124q\u1d41)\n ^-gh (dlog g) (dlog-ok g)\n (explore-ext \u2124q\u1d41) 0 _+_\n (\u03bb k f \u2192 ! sumStableUnder \u2124q\u1d41 (_ , \u229e-is-equiv k) f)\n open Proof otp-lem\n\n thm : \u2200 A \u2192\n let A\u2032 = transformAdv A in\n Dist (IND-CPA-\u2141 A) 1\/2 \u2261 Dist (DDH-\u2141\u2080 A\u2032) (DDH-\u2141\u2081 A\u2032)\n thm = goal\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","old_contents":"{-# OPTIONS --without-K #-}\n{-# OPTIONS --copatterns #-}\nopen import Type\nopen import Function\nopen import Function.Extensionality\nopen import Data.Product.NP\nopen import Data.Unit\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc; Fin\u25b9\u2115) renaming (#_ to ##_)\nopen import Data.Nat.NP hiding (_^_; _==_)\nopen import Data.Nat.Distance\nopen import Data.Bit\nopen import Data.Two\nopen import Relation.Binary.NP\nopen import Data.Bits hiding (_==_)\nopen import Relation.Binary.PropositionalEquality.NP as \u2261 hiding (_\u2219_)\nopen import HoTT\nopen Equivalences\n\nopen import Explore.Core\nopen import Explore.Explorable\nopen import Explore.Universe.Base\nopen import Explore.Sum -- renaming (\u03bcBit to \u03bc\ud835\udfda)\nopen import Explore.Product\nimport Explore.GroupHomomorphism as GH\n--open import Explore.Fin\n\nimport Game.DDH\nimport Game.IND-CPA\nimport Cipher.ElGamal.Generic\n\nmodule elgamal where\n\n{-\ndata `\u2605 : \u2605 where\n `\u22a4 : `\u2605\n `X : `\u2605\n _`\u00d7_ : `\u2605 \u2192 `\u2605 \u2192 `\u2605\ninfixr 2 _`\u00d7_\n\nmodule Univ (X : \u2605) where\n El : `\u2605 \u2192 \u2605\n El `\u22a4 = \u22a4\n El `X = X\n El (u\u2080 `\u00d7 u\u2081) = El u\u2080 \u00d7 El u\u2081\n\n record \u21ba (R : `\u2605) (A : \u2605) : \u2605 where\n constructor mk\n field\n run\u21ba : El R \u2192 A\n open \u21ba public\n\n EXP : (R : `\u2605) \u2192 \u2605\n EXP R = \u21ba R Bit\n\n Det : \u2605 \u2192 \u2605\n Det = \u21ba `\u22a4\n\n \u03bcU : Explorable X \u2192 \u2200 u \u2192 Explorable (El u)\n \u03bcU \u03bcX `\u22a4 = \u03bc\u22a4\n \u03bcU \u03bcX `X = \u03bcX\n \u03bcU \u03bcX (u\u2080 `\u00d7 u\u2081) = \u03bcU \u03bcX u\u2080 \u00d7-\u03bc \u03bcU \u03bcX u\u2081\n\nmodule \u2124q-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u03bc\u2124q : Explorable \u2124q)\n (\u229e-stable : \u2200 x \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u229e_ x))\n where\n\n -- open Sum\n open Univ \u2124q public\n open `\u2605 public renaming (`X to `\u2124q)\n\n #_ : \u2200 {u} \u2192 \u21ba u Bit \u2192 \u2115\n #_ {u} f = count (\u03bcU \u03bc\u2124q u) (run\u21ba f)\n\n #q_ : Count \u2124q\n #q_ = count \u03bc\u2124q\n\n \u2047 : \u2200 R \u2192 \u21ba R (El R)\n run\u21ba (\u2047 _) = id\n\n pure\u21ba : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n run\u21ba (pure\u21ba x) r = x -- turning r to _ produce an error\n\n \u27ea_\u27eb : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n \u27ea_\u27eb = pure\u21ba\n\n {-\n \u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n \u27ea_\u27eb\u1d30 = pure\u1d30\n -}\n\n map\u21ba : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n run\u21ba (map\u21ba f x) r = f (run\u21ba x r)\n\n infixl 4 _\u229b_\n _\u229b_ : \u2200 {R S A B} \u2192 \u21ba R (A \u2192 B) \u2192 \u21ba S A \u2192 \u21ba (R `\u00d7 S) B\n run\u21ba (af \u229b ax) rs = run\u21ba af (fst rs) (run\u21ba ax (snd rs))\n\n \u27ea_\u00b7_\u27eb : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n \u27ea f \u00b7 x \u27eb = map\u21ba f x\n\n \u27ea_\u00b7_\u00b7_\u27eb : \u2200 {A B C} {R S} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba R A \u2192 \u21ba S B \u2192 \u21ba (R `\u00d7 S) C\n \u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n _\u27e8\u229e\u27e9_ : \u2200 {R S} \u2192 \u21ba R \u2124q \u2192 \u21ba S \u2124q \u2192 \u21ba (R `\u00d7 S) \u2124q\n x \u27e8\u229e\u27e9 y = \u27ea _\u229e_ \u00b7 x \u00b7 y \u27eb\n\n \u27e8_\u229e\u27e9_ : \u2200 {R} \u2192 \u2124q \u2192 \u21ba R \u2124q \u2192 \u21ba R \u2124q\n \u27e8 x \u229e\u27e9 y = \u27ea _\u229e_ x \u00b7 y \u27eb\n\n infix 4 _\u2248\u21ba_ _\u2248\u1d2c_\n _\u2248\u21ba_ : \u2200 {R : `\u2605} (f g : EXP R) \u2192 \u2605\n _\u2248\u21ba_ = _\u2261_ on #_\n\n _\u2248\u1d2c_ : \u2200 {A R} (f g : \u21ba R A) \u2192 Set _\n _\u2248\u1d2c_ {A} f g = \u2200 (Adv : A \u2192 Bit) \u2192 \u27ea Adv \u00b7 f \u27eb \u2248\u21ba \u27ea Adv \u00b7 g \u27eb\n\n lem : \u2200 x \u2192 \u27e8 x \u229e\u27e9 (\u2047 `\u2124q) \u2248\u1d2c \u2047 _\n lem x Adv = sym (\u229e-stable x (Bit\u25b9\u2115 \u2218 Adv))\n\n -- \u2200 (A : \u2124q \u2192 Bit) \u2192 # (A \u2047)\n-}\n\nmodule El-Gamal-Generic\n (\u2124q\u1d41 : U)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 El \u2124q\u1d41 \u2192 G)\n (Message : \u2605)\n (_\u2219_ : G \u2192 Message \u2192 Message)\n (_\/_ : Message \u2192 G \u2192 Message)\n (R\u2090\u1d41 : U)\n where\n \u2124q = El \u2124q\u1d41\n R\u2090 = El R\u2090\u1d41\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n -- g\u02e3 is the pk\n -- x is the sk\n\n R\u2093 : \u2605\n R\u2093 = \u2124q\n\n open Cipher.ElGamal.Generic Message Message \u2124q G g _^_ _\u2219_ _\/_\n\n module IND-CPA = Game.IND-CPA PubKey SecKey Message CipherText R\u2091 R\u2096 R\u2090 R\u2093 KeyGen Enc\n open IND-CPA renaming (R to R')\n\n -- R' = (R\u2090 \u00d7 R\u2096 \u00d7 R\u2091 \u00d7 R\u2093)\n R'\u1d41 = R\u2090\u1d41 \u00d7\u1d41 \u2124q\u1d41 \u00d7\u1d41 \u2124q\u1d41 \u00d7\u1d41 \u2124q\u1d41\n -- R' = El R'\u1d41\n sumR' = sum R'\u1d41\n R = \ud835\udfda \u00d7 R'\n R\u1d41 = \ud835\udfda\u1d41 \u00d7\u1d41 R'\u1d41\n sumR = sum R\u1d41\n \n sumExtR\u2090 = sum-ext R\u2090\u1d41\n sumExt\u2124q = sum-ext \u2124q\u1d41\n sumHomR' = sum-hom R'\u1d41\n sumExtR' = sum-ext R'\u1d41\n \n IND-CPA-\u2141 : IND-CPA.Adversary \u2192 R \u2192 \ud835\udfda\n IND-CPA-\u2141 = IND-CPA.game\n \n module DDH = Game.DDH \u2124q G g _^_ (\ud835\udfda \u00d7 R\u2090)\n\n DDH-\u2141\u2080 DDH-\u2141\u2081 : DDH.Adv \u2192 R \u2192 \ud835\udfda\n DDH-\u2141\u2080 A (b , r\u2090 , g\u02e3 , g\u02b8 , g\u1dbb) = DDH.\u2141\u2080 A ((b , r\u2090) , g\u02e3 , g\u02b8 , g\u1dbb)\n DDH-\u2141\u2081 A (b , r\u2090 , g\u02e3 , g\u02b8 , g\u1dbb) = DDH.\u2141\u2081 A ((b , r\u2090) , g\u02e3 , g\u02b8 , g\u1dbb)\n \n transformAdv : IND-CPA.Adversary \u2192 DDH.Adv\n transformAdv (m , d) (b , r\u2090) g\u02e3 g\u02b8 g\u1dbb = b == b\u2032\n where mb = m r\u2090 g\u02e3 b\n c = (g\u02b8 , g\u1dbb \u2219 mb)\n b\u2032 = d r\u2090 g\u02e3 c\n\n #q_ : Count \u2124q\n #q_ = count \u2124q\u1d41\n\n _\u2248q_ : (f g : \u2124q \u2192 \u2115) \u2192 \u2605\n f \u2248q g = sum \u2124q\u1d41 f \u2261 sum \u2124q\u1d41 g\n\n OTP-LEM = \u2200 (O : Message \u2192 \u2115) m\u2080 m\u2081 \u2192\n (\u03bb x \u2192 O((g ^ x) \u2219 m\u2080)) \u2248q (\u03bb x \u2192 O((g ^ x) \u2219 m\u2081))\n\n 1\/2 : R \u2192 \ud835\udfda\n 1\/2 (b , _) = b\n\n module _ {S} where \n _\u2248\u1d3f_ : (f g : R \u2192 S) \u2192 \u2605\n f \u2248\u1d3f g = \u2200 (X : S \u2192 \u2115) \u2192 sumR (X \u2218 f) \u2261 sumR (X \u2218 g) \n\n Dist : (f g : R \u2192 \ud835\udfda) \u2192 \u2115\n Dist f g = dist (count R\u1d41 f) (count R\u1d41 g)\n\n dist-cong : \u2200 {f g h i} \u2192 f \u2248\u1d3f g \u2192 h \u2248\u1d3f i \u2192 Dist f h \u2261 Dist g i\n dist-cong {f}{g}{h}{i} f\u2248g h\u2248i = ap\u2082 dist (f\u2248g \ud835\udfda\u25b9\u2115) (h\u2248i \ud835\udfda\u25b9\u2115)\n\n OTP-\u2141 : IND-CPA.Adversary \u2192 R \u2192 \ud835\udfda\n OTP-\u2141 (m , d) (b , r\u2090 , x , y , z) = b == d r\u2090 g\u02e3 (g\u02b8 , g\u1dbb \u2219 m r\u2090 g\u02e3 b)\n where g\u02e3 = g ^ x\n g\u02b8 = g ^ y\n g\u1dbb = g ^ z\n\n module Proof (otp-lem : OTP-LEM)\n\n (A : IND-CPA.Adversary) where\n\n module A = IND-CPA.Adversary A\n A\u2032 = transformAdv A\n\n goal4 : 1\/2 \u2248\u1d3f OTP-\u2141 A\n goal4 X = sumR' (\u03bb _ \u2192 X 0b) + sumR' (\u03bb _ \u2192 X 1b)\n \u2261\u27e8 sym (sumHomR' _ _) \u27e9\n sumR' (\u03bb _ \u2192 X 0b + X 1b)\n \u2261\u27e8 sumExtR' (lemma \u2218 B 0b) \u27e9\n sumR' (Y 0b 0b +\u00b0 Y 1b 0b)\n \u2261\u27e8 sumHomR' _ _ \u27e9\n sumR' (Y 0b 0b) + sumR' (Y 1b 0b)\n \u2261\u27e8 cong (_+_ (sumR' (Y 0b 0b))) lemma2 \u27e9\n sumR' (Y 0b 0b) + sumR' (Y 1b 1b)\n \u220e\n where\n open \u2261-Reasoning\n\n B : \ud835\udfda \u2192 R' \u2192 \ud835\udfda\n B b (r\u2090 , x , y , z) = A.b\u2032 r\u2090 (g ^ x) (g ^ y , (g ^ z) \u2219 A.m r\u2090 (g ^ x) b)\n\n Y = \u03bb bb bbb r \u2192 X (bb == B bbb r)\n\n lemma : \u2200 b \u2192 X 0b + X 1b \u2261 X (0b == b) + X (1b == b)\n lemma 1b = refl\n lemma 0b = \u2115\u00b0.+-comm (X 0b) _\n\n lemma2 : sumR' (Y 1b 0b) \u2261 sumR' (Y 1b 1b)\n lemma2 = sumExtR\u2090 \u03bb r\u2090 \u2192\n sumExt\u2124q \u03bb x \u2192\n sumExt\u2124q \u03bb y \u2192\n otp-lem (\u03bb m \u2192 X (A.b\u2032 r\u2090 (g ^ x) (g ^ y , m))) (A.m r\u2090 (g ^ x) 0\u2082) (A.m r\u2090 (g ^ x) 1\u2082)\n\n {-\n otp-lem (\u03bb m \u2192 snd A r\u2090 (g ^ x) (g ^ y , m))\n (fst A r\u2090 (g ^ x) 1b)\n (fst A r\u2090 (g ^ x) 0b)\n (\u03bb c \u2192 X (1b == c))\n -}\n\n module absDist {DIST : \u2605}(Dist : (f g : R \u2192 \ud835\udfda) \u2192 DIST)\n (dist-cong : \u2200 {f h i} \u2192 h \u2248\u1d3f i \u2192 Dist f h \u2261 Dist f i) where\n goal : Dist (IND-CPA-\u2141 A) 1\/2 \u2261 Dist (DDH-\u2141\u2080 A\u2032) (DDH-\u2141\u2081 A\u2032)\n goal = Dist (IND-CPA-\u2141 A) 1\/2\n \u2261\u27e8 refl \u27e9\n Dist (DDH-\u2141\u2080 A\u2032) 1\/2\n \u2261\u27e8 dist-cong goal4 \u27e9\n Dist (DDH-\u2141\u2080 A\u2032) (OTP-\u2141 A)\n \u2261\u27e8 refl \u27e9\n Dist (DDH-\u2141\u2080 A\u2032) (DDH-\u2141\u2081 A\u2032)\n \u220e\n where open \u2261-Reasoning\n\n open absDist Dist (\u03bb {f}{h}{i} \u2192 dist-cong {f}{f}{h}{i} (\u03bb _ \u2192 refl)) public\n\nmodule El-Gamal-Base\n (\u2124q\u1d41 : U)\n (\u2124q-grp : GH.Group (El \u2124q\u1d41))\n (G : \u2605)\n (G-grp : GH.Group G)\n (g : G)\n (_^_ : G \u2192 El \u2124q\u1d41 \u2192 G)\n (^-gh : GH.GroupHomomorphism \u2124q-grp G-grp (_^_ g))\n (dlog : (b y : G) \u2192 El \u2124q\u1d41)\n (dlog-ok : (b y : G) \u2192 b ^ dlog b y \u2261 y)\n (R\u2090\u1d41 : U)\n (R\u2090 : El R\u2090\u1d41)\n {{_ : FunExt}}\n {{_ : UA}}\n (open GH.Group \u2124q-grp renaming (_\u2219_ to _\u229e_))\n (\u229e-is-equiv : \u2200 k \u2192 Is-equiv (flip _\u229e_ k))\n where\n\n open GH.Group G-grp using (_\u2219_) renaming (-_ to _\u207b\u00b9)\n\n _\/_ : G \u2192 G \u2192 G\n x \/ y = x \u2219 (y \u207b\u00b9)\n\n open El-Gamal-Generic \u2124q\u1d41 G g _^_ G _\u2219_ _\/_ R\u2090\u1d41 public\n\n otp-lem : \u2200 (O : G \u2192 \u2115) m\u2080 m\u2081 \u2192\n (\u03bb x \u2192 O((g ^ x) \u2219 m\u2080)) \u2248q (\u03bb x \u2192 O((g ^ x) \u2219 m\u2081))\n otp-lem = GH.thm \u2124q-grp G-grp (_^_ g) (explore \u2124q\u1d41)\n ^-gh (dlog g) (dlog-ok g)\n (explore-ext \u2124q\u1d41) 0 _+_\n (\u03bb k f \u2192 ! sumStableUnder \u2124q\u1d41 (_ , \u229e-is-equiv k) f)\n open Proof otp-lem\n\n thm : \u2200 A \u2192\n let A\u2032 = transformAdv A in\n Dist (IND-CPA-\u2141 A) 1\/2 \u2261 Dist (DDH-\u2141\u2080 A\u2032) (DDH-\u2141\u2081 A\u2032)\n thm = goal\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"ac531c140ff1e48d76d49f86e8052d8213243ff4","subject":"Fixup old comments","message":"Fixup old comments\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Evaluation.agda","new_file":"Parametric\/Change\/Evaluation.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Connecting Parametric.Change.Term and Parametric.Change.Value.\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Change.Type as ChangeType\nimport Parametric.Change.Term as ChangeTerm\nimport Parametric.Change.Value as ChangeValue\n\nmodule Parametric.Change.Evaluation\n {Base : Type.Structure}\n {Const : Term.Structure Base}\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (\u0394Base : ChangeType.Structure Base)\n (apply-base : ChangeTerm.ApplyStructure Const \u0394Base)\n (diff-base : ChangeTerm.DiffStructure Const \u0394Base)\n (\u27e6apply-base\u27e7 : ChangeValue.ApplyStructure Const \u27e6_\u27e7Base \u0394Base)\n (\u27e6diff-base\u27e7 : ChangeValue.DiffStructure Const \u27e6_\u27e7Base \u0394Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen ChangeType.Structure Base \u0394Base\nopen ChangeTerm.Structure Const \u0394Base diff-base apply-base\nopen ChangeValue.Structure Const \u27e6_\u27e7Base \u0394Base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Denotation.Notation\nopen import Postulate.Extensionality\n\n-- Extension point 1: Relating \u2295 and its value on base types\nApplyStructure : Set\nApplyStructure = \u2200 \u03b9 {\u0393} \u2192\n {t : Term \u0393 (base \u03b9)} {\u0394t : Term \u0393 (\u0394Type (base \u03b9))} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 t \u27e7 \u03c1 \u27e6\u2295\u208d base \u03b9 \u208e\u27e7 \u27e6 \u0394t \u27e7 \u03c1 \u2261 \u27e6 t \u2295\u208d base \u03b9 \u208e \u0394t \u27e7 \u03c1\n\n-- Extension point 2: Relating \u229d and its value on base types\nDiffStructure : Set\nDiffStructure = \u2200 \u03b9 {\u0393} \u2192\n {s : Term \u0393 (base \u03b9)} {t : Term \u0393 (base \u03b9)} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 s \u27e7 \u03c1 \u27e6\u229d\u208d base \u03b9 \u208e\u27e7 \u27e6 t \u27e7 \u03c1 \u2261 \u27e6 s \u229d\u208d base \u03b9 \u208e t \u27e7 \u03c1\n\nmodule Structure\n (meaning-\u2295-base : ApplyStructure)\n (meaning-\u229d-base : DiffStructure)\n where\n\n -- unique names with unambiguous types\n -- to help type inference figure things out\n private\n module Disambiguation where\n infixr 9 _\u22c6_\n _\u22c6_ : Type \u2192 Context \u2192 Context\n _\u22c6_ = _\u2022_\n\n -- We provide: Relating \u2295 and \u229d and their values on arbitrary types.\n meaning-\u2295 : \u2200 {\u03c4 \u0393}\n {t : Term \u0393 \u03c4} {\u0394t : Term \u0393 (\u0394Type \u03c4)} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 t \u27e7 \u03c1 \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u27e6 \u0394t \u27e7 \u03c1 \u2261 \u27e6 t \u2295\u208d \u03c4 \u208e \u0394t \u27e7 \u03c1\n\n meaning-\u229d : \u2200 {\u03c4 \u0393}\n {s : Term \u0393 \u03c4} {t : Term \u0393 \u03c4} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 s \u27e7 \u03c1 \u27e6\u229d\u208d \u03c4 \u208e\u27e7 \u27e6 t \u27e7 \u03c1 \u2261 \u27e6 s \u229d\u208d \u03c4 \u208e t \u27e7 \u03c1\n\n meaning-\u2295 {base \u03b9} {\u0393} {\u03c4} {\u0394t} {\u03c1} = meaning-\u2295-base \u03b9 {\u0393} {\u03c4} {\u0394t} {\u03c1}\n meaning-\u2295 {\u03c3 \u21d2 \u03c4} {\u0393} {t} {\u0394t} {\u03c1} = ext (\u03bb v \u2192\n let\n \u0393\u2032 = \u03c3 \u22c6 (\u03c3 \u21d2 \u03c4) \u22c6 \u0394Type (\u03c3 \u21d2 \u03c4) \u22c6 \u0393\n \u03c1\u2032 : \u27e6 \u0393\u2032 \u27e7\n \u03c1\u2032 = v \u2022 (\u27e6 t \u27e7 \u03c1) \u2022 (\u27e6 \u0394t \u27e7 \u03c1) \u2022 \u03c1\n x : Term \u0393\u2032 \u03c3\n x = var this\n f : Term \u0393\u2032 (\u03c3 \u21d2 \u03c4)\n f = var (that this)\n \u0394f : Term \u0393\u2032 (\u0394Type (\u03c3 \u21d2 \u03c4))\n \u0394f = var (that (that this))\n y = app f x\n \u0394y = app (app \u0394f x) (x \u229d x)\n in\n begin\n \u27e6 t \u27e7 \u03c1 v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u27e6 \u0394t \u27e7 \u03c1 v (v \u27e6\u229d\u208d \u03c3 \u208e\u27e7 v)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 \u03c1 v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u27e6 \u0394t \u27e7 \u03c1 v hole)\n (meaning-\u229d {s = x} {x} {\u03c1\u2032}) \u27e9\n \u27e6 t \u27e7 \u03c1 v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u27e6 \u0394t \u27e7 \u03c1 v (\u27e6 x \u229d x \u27e7 \u03c1\u2032)\n \u2261\u27e8 meaning-\u2295 {t = y} {\u0394t = \u0394y} {\u03c1\u2032} \u27e9\n \u27e6 y \u2295\u208d \u03c4 \u208e \u0394y \u27e7 \u03c1\u2032\n \u220e)\n where\n open \u2261-Reasoning\n open Disambiguation\n\n meaning-\u229d {base \u03b9} {\u0393} {s} {t} {\u03c1} = meaning-\u229d-base \u03b9 {\u0393} {s} {t} {\u03c1}\n meaning-\u229d {\u03c3 \u21d2 \u03c4} {\u0393} {s} {t} {\u03c1} =\n ext (\u03bb v \u2192 ext (\u03bb \u0394v \u2192\n let\n \u0393\u2032 = \u0394Type \u03c3 \u22c6 \u03c3 \u22c6 (\u03c3 \u21d2 \u03c4) \u22c6 (\u03c3 \u21d2 \u03c4) \u22c6 \u0393\n \u03c1\u2032 : \u27e6 \u0393\u2032 \u27e7Context\n \u03c1\u2032 = \u0394v \u2022 v \u2022 \u27e6 t \u27e7Term \u03c1 \u2022 \u27e6 s \u27e7Term \u03c1 \u2022 \u03c1\n \u0394x : Term \u0393\u2032 (\u0394Type \u03c3)\n \u0394x = var this\n x : Term \u0393\u2032 \u03c3\n x = var (that this)\n f : Term \u0393\u2032 (\u03c3 \u21d2 \u03c4)\n f = var (that (that this))\n g : Term \u0393\u2032 (\u03c3 \u21d2 \u03c4)\n g = var (that (that (that this)))\n y = app f x\n y\u2032 = app g (x \u2295\u208d \u03c3 \u208e \u0394x)\n in\n begin\n \u27e6 s \u27e7 \u03c1 (v \u27e6\u2295\u208d \u03c3 \u208e\u27e7 \u0394v) \u27e6\u229d\u208d \u03c4 \u208e\u27e7 \u27e6 t \u27e7 \u03c1 v\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 s \u27e7 \u03c1 hole \u27e6\u229d\u208d \u03c4 \u208e\u27e7 \u27e6 t \u27e7 \u03c1 v)\n (meaning-\u2295 {t = x} {\u0394t = \u0394x} {\u03c1\u2032}) \u27e9\n \u27e6 s \u27e7 \u03c1 (\u27e6 x \u2295\u208d \u03c3 \u208e \u0394x \u27e7 \u03c1\u2032) \u27e6\u229d\u208d \u03c4 \u208e\u27e7 \u27e6 t \u27e7 \u03c1 v\n \u2261\u27e8 meaning-\u229d {s = y\u2032} {y} {\u03c1\u2032} \u27e9\n \u27e6 y\u2032 \u229d y \u27e7 \u03c1\u2032\n \u220e))\n where\n open \u2261-Reasoning\n open Disambiguation\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Connecting Parametric.Change.Term and Parametric.Change.Value.\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Change.Type as ChangeType\nimport Parametric.Change.Term as ChangeTerm\nimport Parametric.Change.Value as ChangeValue\n\nmodule Parametric.Change.Evaluation\n {Base : Type.Structure}\n {Const : Term.Structure Base}\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (\u0394Base : ChangeType.Structure Base)\n (apply-base : ChangeTerm.ApplyStructure Const \u0394Base)\n (diff-base : ChangeTerm.DiffStructure Const \u0394Base)\n (\u27e6apply-base\u27e7 : ChangeValue.ApplyStructure Const \u27e6_\u27e7Base \u0394Base)\n (\u27e6diff-base\u27e7 : ChangeValue.DiffStructure Const \u27e6_\u27e7Base \u0394Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen ChangeType.Structure Base \u0394Base\nopen ChangeTerm.Structure Const \u0394Base diff-base apply-base\nopen ChangeValue.Structure Const \u27e6_\u27e7Base \u0394Base \u27e6apply-base\u27e7 \u27e6diff-base\u27e7\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Denotation.Notation\nopen import Postulate.Extensionality\n\n-- Extension point 1: Relating \u229d and its value on base types\nApplyStructure : Set\nApplyStructure = \u2200 \u03b9 {\u0393} \u2192\n {t : Term \u0393 (base \u03b9)} {\u0394t : Term \u0393 (\u0394Type (base \u03b9))} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 t \u27e7 \u03c1 \u27e6\u2295\u208d base \u03b9 \u208e\u27e7 \u27e6 \u0394t \u27e7 \u03c1 \u2261 \u27e6 t \u2295\u208d base \u03b9 \u208e \u0394t \u27e7 \u03c1\n\n-- Extension point 2: Relating \u2295 and its value on base types\nDiffStructure : Set\nDiffStructure = \u2200 \u03b9 {\u0393} \u2192\n {s : Term \u0393 (base \u03b9)} {t : Term \u0393 (base \u03b9)} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 s \u27e7 \u03c1 \u27e6\u229d\u208d base \u03b9 \u208e\u27e7 \u27e6 t \u27e7 \u03c1 \u2261 \u27e6 s \u229d\u208d base \u03b9 \u208e t \u27e7 \u03c1\n\nmodule Structure\n (meaning-\u2295-base : ApplyStructure)\n (meaning-\u229d-base : DiffStructure)\n where\n\n -- unique names with unambiguous types\n -- to help type inference figure things out\n private\n module Disambiguation where\n infixr 9 _\u22c6_\n _\u22c6_ : Type \u2192 Context \u2192 Context\n _\u22c6_ = _\u2022_\n\n -- We provide: Relating \u2295 and \u229d and their values on arbitrary types.\n meaning-\u2295 : \u2200 {\u03c4 \u0393}\n {t : Term \u0393 \u03c4} {\u0394t : Term \u0393 (\u0394Type \u03c4)} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 t \u27e7 \u03c1 \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u27e6 \u0394t \u27e7 \u03c1 \u2261 \u27e6 t \u2295\u208d \u03c4 \u208e \u0394t \u27e7 \u03c1\n\n meaning-\u229d : \u2200 {\u03c4 \u0393}\n {s : Term \u0393 \u03c4} {t : Term \u0393 \u03c4} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192\n \u27e6 s \u27e7 \u03c1 \u27e6\u229d\u208d \u03c4 \u208e\u27e7 \u27e6 t \u27e7 \u03c1 \u2261 \u27e6 s \u229d\u208d \u03c4 \u208e t \u27e7 \u03c1\n\n meaning-\u2295 {base \u03b9} {\u0393} {\u03c4} {\u0394t} {\u03c1} = meaning-\u2295-base \u03b9 {\u0393} {\u03c4} {\u0394t} {\u03c1}\n meaning-\u2295 {\u03c3 \u21d2 \u03c4} {\u0393} {t} {\u0394t} {\u03c1} = ext (\u03bb v \u2192\n let\n \u0393\u2032 = \u03c3 \u22c6 (\u03c3 \u21d2 \u03c4) \u22c6 \u0394Type (\u03c3 \u21d2 \u03c4) \u22c6 \u0393\n \u03c1\u2032 : \u27e6 \u0393\u2032 \u27e7\n \u03c1\u2032 = v \u2022 (\u27e6 t \u27e7 \u03c1) \u2022 (\u27e6 \u0394t \u27e7 \u03c1) \u2022 \u03c1\n x : Term \u0393\u2032 \u03c3\n x = var this\n f : Term \u0393\u2032 (\u03c3 \u21d2 \u03c4)\n f = var (that this)\n \u0394f : Term \u0393\u2032 (\u0394Type (\u03c3 \u21d2 \u03c4))\n \u0394f = var (that (that this))\n y = app f x\n \u0394y = app (app \u0394f x) (x \u229d x)\n in\n begin\n \u27e6 t \u27e7 \u03c1 v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u27e6 \u0394t \u27e7 \u03c1 v (v \u27e6\u229d\u208d \u03c3 \u208e\u27e7 v)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 \u03c1 v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u27e6 \u0394t \u27e7 \u03c1 v hole)\n (meaning-\u229d {s = x} {x} {\u03c1\u2032}) \u27e9\n \u27e6 t \u27e7 \u03c1 v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u27e6 \u0394t \u27e7 \u03c1 v (\u27e6 x \u229d x \u27e7 \u03c1\u2032)\n \u2261\u27e8 meaning-\u2295 {t = y} {\u0394t = \u0394y} {\u03c1\u2032} \u27e9\n \u27e6 y \u2295\u208d \u03c4 \u208e \u0394y \u27e7 \u03c1\u2032\n \u220e)\n where\n open \u2261-Reasoning\n open Disambiguation\n\n meaning-\u229d {base \u03b9} {\u0393} {s} {t} {\u03c1} = meaning-\u229d-base \u03b9 {\u0393} {s} {t} {\u03c1}\n meaning-\u229d {\u03c3 \u21d2 \u03c4} {\u0393} {s} {t} {\u03c1} =\n ext (\u03bb v \u2192 ext (\u03bb \u0394v \u2192\n let\n \u0393\u2032 = \u0394Type \u03c3 \u22c6 \u03c3 \u22c6 (\u03c3 \u21d2 \u03c4) \u22c6 (\u03c3 \u21d2 \u03c4) \u22c6 \u0393\n \u03c1\u2032 : \u27e6 \u0393\u2032 \u27e7Context\n \u03c1\u2032 = \u0394v \u2022 v \u2022 \u27e6 t \u27e7Term \u03c1 \u2022 \u27e6 s \u27e7Term \u03c1 \u2022 \u03c1\n \u0394x : Term \u0393\u2032 (\u0394Type \u03c3)\n \u0394x = var this\n x : Term \u0393\u2032 \u03c3\n x = var (that this)\n f : Term \u0393\u2032 (\u03c3 \u21d2 \u03c4)\n f = var (that (that this))\n g : Term \u0393\u2032 (\u03c3 \u21d2 \u03c4)\n g = var (that (that (that this)))\n y = app f x\n y\u2032 = app g (x \u2295\u208d \u03c3 \u208e \u0394x)\n in\n begin\n \u27e6 s \u27e7 \u03c1 (v \u27e6\u2295\u208d \u03c3 \u208e\u27e7 \u0394v) \u27e6\u229d\u208d \u03c4 \u208e\u27e7 \u27e6 t \u27e7 \u03c1 v\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 s \u27e7 \u03c1 hole \u27e6\u229d\u208d \u03c4 \u208e\u27e7 \u27e6 t \u27e7 \u03c1 v)\n (meaning-\u2295 {t = x} {\u0394t = \u0394x} {\u03c1\u2032}) \u27e9\n \u27e6 s \u27e7 \u03c1 (\u27e6 x \u2295\u208d \u03c3 \u208e \u0394x \u27e7 \u03c1\u2032) \u27e6\u229d\u208d \u03c4 \u208e\u27e7 \u27e6 t \u27e7 \u03c1 v\n \u2261\u27e8 meaning-\u229d {s = y\u2032} {y} {\u03c1\u2032} \u27e9\n \u27e6 y\u2032 \u229d y \u27e7 \u03c1\u2032\n \u220e))\n where\n open \u2261-Reasoning\n open Disambiguation\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"89ca2e37b8eb2d8ee3b6d8c9bf51b952925c6c45","subject":"Added x\u22650 (ER version).","message":"Added x\u22650 (ER version).\n\nIgnore-this: 156a203f7f01a3daa606860ed4c0c3ff\n\ndarcs-hash:20100502224943-3bd4e-e29c673de3e2161e48fc06c1830f38eee3d7f0f5.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Relation\/Inequalities\/PropertiesER.agda","new_file":"LTC\/Relation\/Inequalities\/PropertiesER.agda","new_contents":"------------------------------------------------------------------------------\n-- Properties of the inequalities (using equational reasoning)\n------------------------------------------------------------------------------\n\nmodule LTC.Relation.Inequalities.PropertiesER where\n\nopen import LTC.Minimal\nopen import LTC.MinimalER\n\nopen import LTC.Relation.Inequalities\nopen import LTC.Data.N\nopen import MyStdLib.Data.Sum\nopen import MyStdLib.Function\n\n------------------------------------------------------------------------------\n\nx\u22650 : {n : D} \u2192 N n \u2192 GE n zero\nx\u22650 zN = lt-00\nx\u22650 (sN {n} Nn) = lt-S0 n\n\n\u00ac0>x : {n : D} \u2192 N n \u2192 \u00ac (GT zero n)\n\u00ac0>x Nn 0>n = true\u2260false $ trans (sym 0>n ) $ x\u22650 Nn\n\n\u00acS\u22640 : {d : D} \u2192 \u00ac (LE (succ d) zero)\n\u00acS\u22640 {d} Sx\u22640 = true\u2260false $ trans (sym $ lt-0S d ) Sx\u22640\n\nx>y\u2228x\u2264y : {m n : D} \u2192 N m \u2192 N n \u2192 GT m n \u2228 LE m n\nx>y\u2228x\u2264y zN Nn = inj\u2082 $ x\u22650 Nn\nx>y\u2228x\u2264y (sN {m} Nm) zN = inj\u2081 $ lt-0S m\nx>y\u2228x\u2264y (sN {m} Nm) (sN {n} Nn) =\n subst (\u03bb a \u2192 a \u2261 true \u2228 a \u2261 false)\n (sym $ lt-SS n m)\n (x>y\u2228x\u2264y Nm Nn )\n\n\u00acxx : {n : D} \u2192 N n \u2192 \u00ac (GT zero n)\n\u00ac0>x Nn 0>n = true\u2260false $ trans (sym 0>n ) $ x\u22650 Nn\n\n\u00acS\u22640 : {d : D} \u2192 \u00ac (LE (succ d) zero)\n\u00acS\u22640 {d} Sx\u22640 = true\u2260false $ trans (sym $ lt-0S d ) Sx\u22640\n\nx>y\u2228x\u2264y : {m n : D} \u2192 N m \u2192 N n \u2192 GT m n \u2228 LE m n\nx>y\u2228x\u2264y zN Nn = inj\u2082 $ x\u22650 Nn\nx>y\u2228x\u2264y (sN {m} Nm) zN = inj\u2081 $ lt-0S m\nx>y\u2228x\u2264y (sN {m} Nm) (sN {n} Nn) =\n subst (\u03bb a \u2192 a \u2261 true \u2228 a \u2261 false)\n (sym $ lt-SS n m)\n (x>y\u2228x\u2264y Nm Nn )\n\n\u00acx Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n pair : Type -> Type -> Type\n\n--********************************\n-- Typed expressions\n--********************************\n\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\nVal (pair x y) = (Val x) * (Val y)\n\n\n-- Fix menu:\nexprFixMenu : FixMenu Type\nexprFixMenu = ( consE (consE nilE) , \n \\ty -> (const (Val ty), -- Val t\n (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void)))\n\n-- Indexed menu:\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\nchoiceMenu (pair x y) = nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\nchoiceDessert (pair x y) = Void\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\n\n-- Expression:\nexpr : TagIDesc Type\nexpr = exprFixMenu , exprSensitiveMenu\n\nexprIDesc : TagIDesc Type -> (Type -> IDesc Type)\nexprIDesc D = toIDesc Type D\n\n\n--********************************\n-- Closed terms\n--********************************\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : {ty : Type} -> IMu closeTerm ty -> Val ty\neval {ty} term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nEmpty : Type -> Set\nEmpty _ = Zero\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (expr ** Empty)\n\n--********************************\n-- Closed term' evaluation\n--********************************\n\neval' : {ty : Type} -> IMu closeTerm' ty -> Val ty\neval' {ty} term = cata Type closeTerm' Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm' ty |] Val -> Val ty\n evalOneStep _ (EZe , ())\n evalOneStep _ (ESu EZe , t) = t\n evalOneStep _ ((ESu (ESu EZe)) , (true , ( x , _))) = x\n evalOneStep _ ((ESu (ESu EZe)) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu (ESu EZe))) , (x , y)) = plus x y\n evalOneStep nat (((ESu (ESu (ESu (ESu ()))))) , t) \n evalOneStep bool ((ESu (ESu (ESu EZe))) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu (ESu ())))) , _) \n evalOneStep (pair x y) (ESu (ESu (ESu ())) , _)\n\n\n--********************************\n-- Open terms\n--********************************\n\n-- A context is a snoc-list of types\n-- put otherwise, a context is a type telescope\ndata Context : Set where\n [] : Context\n _,_ : Context -> Type -> Context\n\n-- The environment realizes the context, having a value for each type\nEnv : Context -> Set\nEnv [] = Unit\nEnv (G , S) = Env G * Val S\n\n-- A typed-variable indexes into the context, obtaining a proof that\n-- what we get is what you want (WWGIWYW)\nVar : Context -> Type -> Set\nVar [] T = Zero\nVar (G , S) T = Var G T + (S == T)\n\n-- The lookup gets into the context to extract the value\nlookup : (G : Context) -> Env G -> (T : Type) -> Var G T -> Val T\nlookup [] _ T ()\nlookup (G , .T) (g , t) T (r refl) = t\nlookup (G , S) (g , t) T (l x) = lookup G g T x \n\n-- Open term: holes are either values or variables in a context\nopenTerm : Context -> Type -> IDesc Type\nopenTerm c = toIDesc Type (expr ** (Var c))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\n-- |discharge| is the local substitution expected by |substI|. It is\n-- just sugar around context lookup\ndischarge : (context : Context) ->\n Env context ->\n (ty : Type) ->\n Var context ty ->\n IMu closeTerm' ty\ndischarge ctxt env ty variable = con (ESu EZe , lookup ctxt env ty variable ) \n\n-- |substExpr| is the specialized |substI| to expressions. We get it\n-- generically from the free monad construction.\nsubstExpr : {ty : Type}\n (context : Context)\n (sigma : (ty : Type) ->\n Var context ty ->\n IMu closeTerm' ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm' ty\nsubstExpr {ty} c sig term = \n substI (Var c) Empty expr sig ty term\n\n-- By first doing substitution to close the term, we can use\n-- evaluation of closed terms, obtaining evaluation of open terms\n-- under a valid context.\nevalOpen : {ty : Type}(context : Context) ->\n Env context ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen ctxt env tm = eval' (substExpr ctxt (discharge ctxt env) tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- Test context:\n-- V 0 :-> true, V 1 :-> 2, V 2 :-> ( false , 1 )\ntestContext : Context\ntestContext = (([] , bool) , nat) , pair bool nat\ntestEnv : Env testContext\ntestEnv = ((Void , true ) , su (su ze)) , (false , su ze) \n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , ( l (r refl) ) )\n\ntestSubst1 : IMu closeTerm' nat\ntestSubst1 = substExpr testContext \n (discharge testContext testEnv)\n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext testEnv test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con (ESu (ESu (ESu EZe)) , (con (ESu EZe , su ze) , con ( EZe , l (r refl) )) )\n\ntestSubst2 : IMu closeTerm' nat\ntestSubst2 = substExpr testContext \n (discharge testContext testEnv)\n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext testEnv test2\n\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu (ESu EZe) , (con (EZe , l (l (r refl))) ,\n (con (EZe , l (r refl)) ,\n con (ESu EZe , ze))))\n\ntestSubst3 : IMu closeTerm' nat\ntestSubst3 = substExpr testContext \n (discharge testContext testEnv)\n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext testEnv test3\n\n-- V 2\ntest4 : IMu (openTerm testContext) (pair bool nat)\ntest4 = con (EZe , r refl )\n\ntestSubst4 : IMu closeTerm' (pair bool nat)\ntestSubst4 = substExpr testContext \n (discharge testContext testEnv)\n test4\n-- = (false , 1)\ntestEval4 : Val (pair bool nat)\ntestEval4 = evalOpen testContext testEnv test4","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n pair : Type -> Type -> Type\n\n--********************************\n-- Typed expressions\n--********************************\n\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\nVal (pair x y) = (Val x) * (Val y)\n\n\n-- Fix menu:\nexprFixMenu : FixMenu Type\nexprFixMenu = ( consE (consE nilE) , \n \\ty -> (const (Val ty), -- Val t\n (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void)))\n\n-- Indexed menu:\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\nchoiceMenu (pair x y) = nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\nchoiceDessert (pair x y) = Void\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\n\n-- Expression:\nexpr : TagIDesc Type\nexpr = exprFixMenu , exprSensitiveMenu\n\nexprIDesc : TagIDesc Type -> (Type -> IDesc Type)\nexprIDesc D = toIDesc Type D\n\n\n--********************************\n-- Closed terms\n--********************************\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : {ty : Type} -> IMu closeTerm ty -> Val ty\neval {ty} term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nEmpty : Type -> Set\nEmpty _ = Zero\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (expr ** Empty)\n\n--********************************\n-- Closed term' evaluation\n--********************************\n\neval' : {ty : Type} -> IMu closeTerm' ty -> Val ty\neval' {ty} term = cata Type closeTerm' Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm' ty |] Val -> Val ty\n evalOneStep _ (EZe , ())\n evalOneStep _ (ESu EZe , t) = t\n evalOneStep _ ((ESu (ESu EZe)) , (true , ( x , _))) = x\n evalOneStep _ ((ESu (ESu EZe)) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu (ESu EZe))) , (x , y)) = plus x y\n evalOneStep nat (((ESu (ESu (ESu (ESu ()))))) , t) \n evalOneStep bool ((ESu (ESu (ESu EZe))) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu (ESu ())))) , _) \n evalOneStep (pair x y) (ESu (ESu (ESu ())) , _)\n\n\n--********************************\n-- Open terms\n--********************************\n\ndata Vec (A : Set) : Nat -> Set where\n vnil : Vec A ze\n vcons : {n : Nat} -> A -> Vec A n -> Vec A (su n)\n\ndata Fin : Nat -> Set where\n fze : {n : Nat} -> Fin (su n)\n fsu : {n : Nat} -> Fin n -> Fin (su n)\n\nContext : Nat -> Set\nContext n = Vec (Sigma Type (\\ty -> IMu closeTerm' ty)) n\n\ntypeAt : {n : Nat}(c : Context n) -> Fin n -> Type\ntypeAt {ze} c ()\ntypeAt {.(su n)} (vcons x xs) (fze {n}) = fst x\ntypeAt {.(su n)} (vcons x xs) (fsu {n} y) = typeAt xs y\n\nlookup : {n : Nat}(c : Context n)(i : Fin n) -> IMu closeTerm' (typeAt c i)\nlookup {ze} c ()\nlookup {su _} (vcons x _) fze = snd x\nlookup {su _} (vcons _ xs) (fsu y) = lookup xs y\n\n\nVar : {n : Nat} -> Context n -> Type -> Set\nVar {n} c ty = Sigma (Fin n) (\\i -> typeAt c i == ty)\n\nopenTerm : {n : Nat} -> Context n -> Type -> IDesc Type\nopenTerm c = toIDesc Type (expr ** (Var c))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : {n : Nat}\n {context : Context n}\n (ty : Type) ->\n Var context ty ->\n IMu closeTerm' ty\ndischarge {n} {c} ty variable = subst (cong (IMu closeTerm') (snd variable)) (lookup c (fst variable))\n\nsubstExpr : {n : Nat}\n {ty : Type}\n (context : Context n)\n (sigma : (ty : Type) ->\n Var context ty ->\n IMu closeTerm' ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm' ty\nsubstExpr {n} {ty} c sig term = \n substI (Var c) Empty expr sig ty term\n\nevalOpen : {n : Nat}{ty : Type}\n (context : Context n) ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen context tm = eval' (substExpr context discharge tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- V 0 :-> true, V 1 :-> 2, V 2 :-> ( false , 1 )\ntestContext : Context _\ntestContext = vcons (bool , con ((ESu EZe) , true )) \n (vcons (nat , con ((ESu EZe) , su (su ze)) ) \n (vcons (pair bool nat , con ((ESu EZe) , ( false , su ze )))\n vnil))\n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , ( fsu fze , refl ) )\n\ntestSubst1 : IMu closeTerm' nat\ntestSubst1 = substExpr testContext \n discharge \n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con ((ESu (ESu (ESu EZe))) , (con ((ESu EZe) , (su ze)) , con ( EZe , (fsu fze , refl) )) )\n\ntestSubst2 : IMu closeTerm' nat\ntestSubst2 = substExpr testContext \n discharge \n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext test2\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu (ESu EZe) , (con (EZe , (fze , refl)) ,\n (con (EZe , (fsu fze , refl)) ,\n con (ESu EZe , ze))))\n\ntestSubst3 : IMu closeTerm' nat\ntestSubst3 = substExpr testContext \n discharge \n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext test3\n\n-- V 2\ntest4 : IMu (openTerm testContext) (pair bool nat)\ntest4 = con (EZe , ( fsu (fsu fze) , refl ) )\n\ntestSubst4 : IMu closeTerm' (pair bool nat)\ntestSubst4 = substExpr testContext \n discharge \n test4\n-- = (false , 1)\ntestEval4 : Val (pair bool nat)\ntestEval4 = evalOpen testContext test4\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"385637be7ada0ab6d5a80eee57eb20d67e57decf","subject":"FunBigStepSILR.agda: progress till I get stuck","message":"FunBigStepSILR.agda: progress till I get stuck\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/FunBigStepSILR.agda","new_file":"Thesis\/FunBigStepSILR.agda","new_contents":"-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by Dargaye and Leroy (2010), \"A\n-- verified framework for higher-order uncurrying optimizations\" (and a bit by\n-- \"Functional Big-Step Semantics\"), though I deviate somewhere.\nmodule Thesis.FunBigStepSILR where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value, following \"Type Soundness Proofs with\n-- Definitional Interpreters\". Since we focus for now on STLC, unlike that\n-- paper, we can avoid error values by keeping types.\n--\n-- One could drop types and add error values, to reproduce what they do.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = \u2115 \u2192 ErrVal \u03c4\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) n = Done (intV v)\n\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval t \u03c1 zero = TimeOut\neval (const c) \u03c1 (suc n) = evalConst c n\neval (var x) \u03c1 (suc n) = Done (\u27e6 x \u27e7Var \u03c1)\neval (abs t) \u03c1 (suc n) = Done (closure t \u03c1)\neval (app s t) \u03c1 (suc n) with eval s \u03c1 n | eval t \u03c1 n\n... | Done f | Done a = apply f a n\n... | _ | _ = TimeOut\n\n-- Can we prove eval sound wrt. our reference denotational semantics? Yes! Very\n-- cool! (Commented out until I paste that semantics here.)\n-- eval-sound : \u2200 {\u0393 \u03c4} \u03c1 v n (t : Term \u0393 \u03c4) \u2192\n-- eval t \u03c1 n \u2261 Done v \u2192\n-- \u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 v \u27e7Val\n\n-- apply-sound : \u2200 {\u0393 \u03c3 \u03c4} \u03c1 v f a n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t \u2192\n-- \u27e6 s \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 f \u27e7Val \u2192\n-- \u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 a \u27e7Val \u2192\n-- apply f a n \u2261 Done v \u2192\n-- \u27e6 s \u27e7Term \u27e6 \u03c1 \u27e7Env (\u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env) \u2261 \u27e6 v \u27e7Val\n-- apply-sound _ v (closure ft \u03c1) a n s t feq aeq eq rewrite feq | aeq = eval-sound (a \u2022 \u03c1) v n ft eq\n\n-- eval-sound \u03c1 v zero t ()\n-- eval-sound \u03c1 v (\u2115.suc n) (const c) eq = {!!}\n-- eval-sound \u03c1 v (\u2115.suc n) (var x) refl = \u21a6-sound \u03c1 x\n-- eval-sound \u03c1 v (\u2115.suc n) (abs t) refl = refl\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) eq with eval s \u03c1 n | inspect (eval s \u03c1) n | eval t \u03c1 n | inspect (eval t \u03c1) n\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) eq | Done f | [ feq ] | Done a | [ aeq ] =\n-- let feq = eval-sound \u03c1 f n s feq; aeq = eval-sound \u03c1 a n t aeq in apply-sound \u03c1 v f a n s t feq aeq eq\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) () | Done f | _ | TimeOut | _\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) () | TimeOut | _ | _ | _\n-- -- eval-sound n (const c) eq = {!!}\n-- -- eval-sound n (var x) eq = {!!}\n-- -- eval-sound n (app s t) eq = {!!} -- with eval s \u03c1 n\n-- -- eval-sound n (abs t) eq = {!!}\n-- -- eval-sound n (const c) eq = {!!}\n-- -- eval-sound n (var x) eq = {!!}\n-- -- eval-sound n (app s t) eq = {!!} -- with eval s \u03c1 n\n-- -- eval-sound n (abs t) eq = {!!}\n\nrelV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\n-- Indexing not according to source. But I can't quite write the correct\n-- indexing without changing the definitions a lot.\nrelT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\nrelT {\u03c4} t1 t2 \u03c11 \u03c12 n =\n (v1 : Val \u03c4) \u2192\n eval t1 \u03c11 n \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] (eval t2 \u03c12 n \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 n)\n\nrelV \u03c4 v1 v2 zero = \u22a4\nrelV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) =\n \u2200 (k : \u2115) (k\u2264n : k \u2264 n) \u2192\n \u2200 v1 v2 \u2192 relV \u03c3 v1 v2 k \u2192 relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\nrelV nat v1 v2 (suc n) = v1 \u2261 v2\n\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl)\n\nrelV-apply-go : \u2200 {\u03c3 \u03c4} sv1 sv2 tv1 tv2\n n\n (svv : relV (\u03c3 \u21d2 \u03c4) sv1 sv2 n)\n (tvv : relV \u03c3 tv1 tv2 n)\n v1 \u2192\n apply sv1 tv1 n \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] (apply sv2 tv2 n \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 (suc n))\nrelV-apply-go (closure st1 \u03c11) (closure st2 \u03c12) tv1 tv2 zero svv tvv v1 ()\nrelV-apply-go (closure st1 \u03c11) (closure st2 \u03c12) tv1 tv2 (suc n) svv tvv v1 eqv1 with svv n \u2264-refl tv1 tv2 {! tvv !} v1 {! eqv1 !}\n... | v2 , eqv2 , final-v = v2 , {! eqv2 !} , {! final-v !}\n\nrelV-apply : \u2200 {\u03c3 \u03c4 sv1 sv2 tv1 tv2}\n n\n (svv : relV (\u03c3 \u21d2 \u03c4) sv1 sv2 n)\n (tvv : relV \u03c3 tv1 tv2 n)\n {v1} \u2192\n apply sv1 tv1 n \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] (apply sv2 tv2 n \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 (suc n))\nrelV-apply n svv tvv eqv1 = relV-apply-go _ _ _ _ n svv tvv _ eqv1\n\nopen import Data.Nat.Properties\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono zero n m\u2264n \u03c4 v1 v2 vv = tt\nrelV-mono (suc m) zero () \u03c4 v1 v2 vv\nrelV-mono (suc m) (suc n) m\u2264n nat v1 v2 vv = vv\nrelV-mono (suc m) (suc n) (_\u2264_.s\u2264s m\u2264n) (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) vv k k\u2264m = vv k (DecTotalOrder.trans decTotalOrder k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\n-- relV-mono \u03c4 v1 v2 vv = tt\n-- relV-mono nat v1 v2 (suc n) vv = vv\n-- relV-mono (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) vv k k\u2264n = vv k ?\n\n-- relV-mono : \u2200 \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 (suc n) \u2192 relV \u03c4 v1 v2 n\n-- relV-mono \u03c4 v1 v2 zero vv = tt\n-- relV-mono nat v1 v2 (suc n) vv = vv\n-- relV-mono (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) vv k k\u2264n = vv k (\u2264-step k\u2264n)\n\n-- fundamental lemma of logical relations.\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero _ _ _ _ ()\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 refl = v2 , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 v ()\n-- XXX trivial case for constants.\nfundamental (const (lit nv)) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(intV nv) refl = intV nv , refl , refl\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 refl = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1 (\u27e6 x \u27e7Var \u03c11) refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 (closure .t .\u03c11) refl =\n closure t \u03c12 , refl , \u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k _ (\u2264-step k\u2264n) _ _ _ \u03c1\u03c1)\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 with eval s \u03c11 n | inspect (eval s \u03c11) n | eval t \u03c11 n | inspect (eval t \u03c11) n\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | Done tv1 | [ teq1 ] with eval s \u03c12 n | inspect (eval s \u03c12) n | eval t \u03c12 n | fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 seq1 | fundamental t n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) tv1 teq1\nfundamental {\u03c4 = \u03c4} (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | Done tv1 | [ teq1 ] | Done sv2 | [ seq2 ] | Done tv2 | (.sv2 , refl , svv) | (.tv2 , refl , tvv) = relV-apply n svv tvv t\u03c11\u2193v1\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | Done sv1 | [ seq1 ] | TimeOut | [ teq1 ]\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | TimeOut | [ seq1 ] | tv1 | [ teq1 ]\n\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | Done tv1 | [ teq1 ] with eval s \u03c12 n | inspect (eval s \u03c12) n | eval t \u03c12 n | fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 seq1\n--... | sv2 | [ seq2 ] | tv2 | (sv2' , s\u03c12\u2193sv2 , svv) = ?\n\n\n-- TODO: match sv2 before matching on fundamental s.\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | Done tv1 with eval s \u03c12 n | inspect (eval s \u03c12) n | eval t \u03c12 n | fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 eq1\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | (Done tv2) | [ teq1 ] | (sv2' , s\u03c12\u2193sv2 , svv) = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | TimeOut | [ teq1 ] | (sv2' , s\u03c12\u2193sv2 , svv) with fundamental t n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) tv1 {!teq1!}\n-- ... | (tv2' , t\u03c12\u2193tv2 , tvv) = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | TimeOut | [ eq2 ] | b | [ teq1 ] | (sv2' , () , svv)\n\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | (Done tv2) with fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 eq1\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | (Done tv2) | (sv2' , s\u03c12\u2193sv2 , svv) = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | TimeOut = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | TimeOut | [ eq2 ] | tv2 = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | Done tv1 with fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 eq1\n-- fundamental {\u03c4 = \u03c4} (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | Done tv1 | (sv2 , s\u03c12\u2193sv2 , svv) = v2 , {!!}\n-- where\n-- v2 : Val \u03c4\n-- v2 = {!!}\n-- -- t\u03c12\u2193v2 : apply sv2 tv2 n \u2261 Done v1\n-- -- t\u03c12\u2193v2 = ?\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | Done sv | _ | TimeOut\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | TimeOut | _ | Done tv1\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | TimeOut | _ | TimeOut\n","old_contents":"-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"A verified framework for\n-- higher-order uncurrying optimizations\" (and a bit by \"Functional Big-Step\n-- Semantics\"), though I deviate somewhere.\nmodule Thesis.FunBigStepSILR where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value, following \"Type Soundness Proofs with\n-- Definitional Interpreters\". Since we focus for now on STLC, unlike that\n-- paper, we can avoid error values by keeping types.\n--\n-- One could drop types and add error values, to reproduce what they do.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = \u2115 \u2192 ErrVal \u03c4\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) n = Done (intV v)\n\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval t \u03c1 zero = TimeOut\neval (const c) \u03c1 (suc n) = evalConst c n\neval (var x) \u03c1 (suc n) = Done (\u27e6 x \u27e7Var \u03c1)\neval (abs t) \u03c1 (suc n) = Done (closure t \u03c1)\neval (app s t) \u03c1 (suc n) with eval s \u03c1 n | eval t \u03c1 n\n... | Done f | Done a = apply f a n\n... | _ | _ = TimeOut\n\n-- Can we prove eval sound wrt. our reference denotational semantics? Yes! Very\n-- cool! (Commented out until I paste that semantics here.)\n-- eval-sound : \u2200 {\u0393 \u03c4} \u03c1 v n (t : Term \u0393 \u03c4) \u2192\n-- eval t \u03c1 n \u2261 Done v \u2192\n-- \u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 v \u27e7Val\n\n-- apply-sound : \u2200 {\u0393 \u03c3 \u03c4} \u03c1 v f a n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t \u2192\n-- \u27e6 s \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 f \u27e7Val \u2192\n-- \u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 a \u27e7Val \u2192\n-- apply f a n \u2261 Done v \u2192\n-- \u27e6 s \u27e7Term \u27e6 \u03c1 \u27e7Env (\u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env) \u2261 \u27e6 v \u27e7Val\n-- apply-sound _ v (closure ft \u03c1) a n s t feq aeq eq rewrite feq | aeq = eval-sound (a \u2022 \u03c1) v n ft eq\n\n-- eval-sound \u03c1 v zero t ()\n-- eval-sound \u03c1 v (\u2115.suc n) (const c) eq = {!!}\n-- eval-sound \u03c1 v (\u2115.suc n) (var x) refl = \u21a6-sound \u03c1 x\n-- eval-sound \u03c1 v (\u2115.suc n) (abs t) refl = refl\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) eq with eval s \u03c1 n | inspect (eval s \u03c1) n | eval t \u03c1 n | inspect (eval t \u03c1) n\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) eq | Done f | [ feq ] | Done a | [ aeq ] =\n-- let feq = eval-sound \u03c1 f n s feq; aeq = eval-sound \u03c1 a n t aeq in apply-sound \u03c1 v f a n s t feq aeq eq\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) () | Done f | _ | TimeOut | _\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) () | TimeOut | _ | _ | _\n-- -- eval-sound n (const c) eq = {!!}\n-- -- eval-sound n (var x) eq = {!!}\n-- -- eval-sound n (app s t) eq = {!!} -- with eval s \u03c1 n\n-- -- eval-sound n (abs t) eq = {!!}\n-- -- eval-sound n (const c) eq = {!!}\n-- -- eval-sound n (var x) eq = {!!}\n-- -- eval-sound n (app s t) eq = {!!} -- with eval s \u03c1 n\n-- -- eval-sound n (abs t) eq = {!!}\n\nrelV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\n-- Indexing not according to source. But I can't quite write the correct\n-- indexing without changing the definitions a lot.\nrelT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\nrelT {\u03c4} t1 t2 \u03c11 \u03c12 n =\n (v1 : Val \u03c4) \u2192\n eval t1 \u03c11 n \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] (eval t2 \u03c12 n \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 n)\n\nrelV \u03c4 v1 v2 zero = \u22a4\nrelV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) =\n \u2200 (k : \u2115) (k\u2264n : k \u2264 n) \u2192\n \u2200 v1 v2 \u2192 relV \u03c3 v1 v2 k \u2192 relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\nrelV nat v1 v2 (suc n) = v1 \u2261 v2\n\nopen import Data.Nat.Properties\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono zero n m\u2264n \u03c4 v1 v2 vv = tt\nrelV-mono (suc m) zero () \u03c4 v1 v2 vv\nrelV-mono (suc m) (suc n) m\u2264n nat v1 v2 vv = vv\nrelV-mono (suc m) (suc n) (_\u2264_.s\u2264s m\u2264n) (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) vv k k\u2264m = vv k (DecTotalOrder.trans decTotalOrder k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\n-- relV-mono \u03c4 v1 v2 vv = tt\n-- relV-mono nat v1 v2 (suc n) vv = vv\n-- relV-mono (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) vv k k\u2264n = vv k ?\n\n-- relV-mono : \u2200 \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 (suc n) \u2192 relV \u03c4 v1 v2 n\n-- relV-mono \u03c4 v1 v2 zero vv = tt\n-- relV-mono nat v1 v2 (suc n) vv = vv\n-- relV-mono (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) vv k k\u2264n = vv k (\u2264-step k\u2264n)\n\n-- fundamental lemma of logical relations.\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero _ _ _ _ ()\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 refl = v2 , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 v ()\n-- XXX trivial case for constants.\nfundamental (const (lit nv)) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(intV nv) refl = intV nv , refl , refl\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 refl = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1 (\u27e6 x \u27e7Var \u03c11) refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 (closure .t .\u03c11) refl =\n closure t \u03c12 , refl , \u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k _ (\u2264-step k\u2264n) _ _ _ \u03c1\u03c1)\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 with eval s \u03c11 n | inspect (eval s \u03c11) n | eval t \u03c11 n\n-- TODO: match sv2 before matching on fundamental s.\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq ] | Done tv1 with fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 eq\nfundamental {\u03c4 = \u03c4} (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq ] | Done tv1 | (sv2 , s\u03c12\u2193sv2 , svv) = v2 , {!!}\n where\n v2 : Val \u03c4\n v2 = {!!}\n -- t\u03c12\u2193v2 : apply sv2 tv2 n \u2261 Done v1\n -- t\u03c12\u2193v2 = ?\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | Done sv | _ | TimeOut\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | TimeOut | _ | Done tv1\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | TimeOut | _ | TimeOut\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"89da077b4d098d6e0939d860c48061f66c5d3369","subject":"Add elimBool to operational semantics.","message":"Add elimBool to operational semantics.\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/Operational.agda","new_file":"formalization\/agda\/Spire\/Operational.agda","new_contents":"module Spire.Operational where\n\n----------------------------------------------------------------------\n\ndata Level : Set where\n zero : Level\n suc : Level \u2192 Level\n\n----------------------------------------------------------------------\n\ndata Context : Set\ndata Type (\u0393 : Context) : Set\ndata Value (\u0393 : Context) : Type \u0393 \u2192 Set\ndata Neutral (\u0393 : Context) : Type \u0393 \u2192 Set\n\n----------------------------------------------------------------------\n\ndata Context where\n \u2205 : Context\n _,_ : (\u0393 : Context) \u2192 Type \u0393 \u2192 Context\n\ndata Type \u0393 where\n `\u22a5 `\u22a4 `Bool : Type \u0393\n `Type : (\u2113 : Level) \u2192 Type \u0393\n `\u03a0 `\u03a3 : (A : Type \u0393) (B : Type (\u0393 , A)) \u2192 Type \u0393\n `\u27e6_\u27e7 : \u2200{\u2113} \u2192 Neutral \u0393 (`Type \u2113) \u2192 Type \u0393\n\n----------------------------------------------------------------------\n\n\u27e6_\u27e7 : \u2200{\u0393 \u2113} \u2192 Value \u0393 (`Type \u2113) \u2192 Type \u0393\n\npostulate\n wknT : \u2200{\u0393 A} \u2192 Type \u0393 \u2192 Type (\u0393 , A)\n subT : \u2200{\u0393 A} \u2192 Type (\u0393 , A) \u2192 Value \u0393 A \u2192 Type \u0393\n subV : \u2200{\u0393 A B} \u2192 Value (\u0393 , A) B \u2192 (x : Value \u0393 A) \u2192 Value \u0393 (subT B x)\n\ndata Var : (\u0393 : Context) (A : Type \u0393) \u2192 Set where\n here : \u2200{\u0393 A} \u2192 Var (\u0393 , A) (wknT A)\n there : \u2200{\u0393 A B} \u2192 Var \u0393 A \u2192 Var (\u0393 , B) (wknT A)\n\n----------------------------------------------------------------------\n\ndata Value \u0393 where\n {- Type introduction -}\n `\u22a5 `\u22a4 `Bool `Type : \u2200{\u2113} \u2192 Value \u0393 (`Type \u2113)\n `\u03a0 `\u03a3 : \u2200{\u2113} (A : Value \u0393 (`Type \u2113)) (B : Value (\u0393 , \u27e6 A \u27e7) (`Type \u2113)) \u2192 Value \u0393 (`Type \u2113)\n `\u27e6_\u27e7 : \u2200{\u2113} \u2192 Value \u0393 (`Type \u2113) \u2192 Value \u0393 (`Type (suc \u2113))\n\n {- Value introduction -}\n `tt : Value \u0393 `\u22a4\n `true `false : Value \u0393 `Bool\n _`,_ : \u2200{A B} (a : Value \u0393 A) (b : Value \u0393 (subT B a)) \u2192 Value \u0393 (`\u03a3 A B)\n `\u03bb : \u2200{A B} \u2192 Value (\u0393 , A) B \u2192 Value \u0393 (`\u03a0 A B)\n `neut : \u2200{A} \u2192 Neutral \u0393 A \u2192 Value \u0393 A\n\n----------------------------------------------------------------------\n\ndata Neutral \u0393 where\n {- Value elimination -}\n `var : \u2200{A} \u2192 Var \u0393 A \u2192 Neutral \u0393 A\n `if_`then_`else_ : \u2200{C} (b : Neutral \u0393 `Bool) (c\u2081 c\u2082 : Value \u0393 C) \u2192 Neutral \u0393 C\n `elimBool : \u2200{\u2113} (P : Value (\u0393 , `Bool) (`Type \u2113))\n (pt : Value \u0393 (subT \u27e6 P \u27e7 `true))\n (pf : Value \u0393 (subT \u27e6 P \u27e7 `false))\n (b : Neutral \u0393 `Bool) \u2192 Neutral \u0393 (subT \u27e6 P \u27e7 (`neut b))\n `proj\u2081 : \u2200{A B} \u2192 Neutral \u0393 (`\u03a3 A B) \u2192 Neutral \u0393 A\n `proj\u2082 : \u2200{A B} (ab : Neutral \u0393 (`\u03a3 A B)) \u2192 Neutral \u0393 (subT B (`neut (`proj\u2081 ab)))\n _`$_ : \u2200{A B} (f : Neutral \u0393 (`\u03a0 A B)) (a : Value \u0393 A) \u2192 Neutral \u0393 (subT B a)\n\n----------------------------------------------------------------------\n\n\u27e6 `\u03a0 A B \u27e7 = `\u03a0 \u27e6 A \u27e7 \u27e6 B \u27e7\n\u27e6 `\u03a3 A B \u27e7 = `\u03a3 \u27e6 A \u27e7 \u27e6 B \u27e7\n\u27e6 `\u22a5 \u27e7 = `\u22a5\n\u27e6 `\u22a4 \u27e7 = `\u22a4\n\u27e6 `Bool \u27e7 = `Bool\n\u27e6 `Type {zero} \u27e7 = `\u22a5\n\u27e6 `Type {suc \u2113} \u27e7 = `Type \u2113\n\u27e6 `\u27e6 A \u27e7 \u27e7 = \u27e6 A \u27e7\n\u27e6 `neut A \u27e7 = `\u27e6 A \u27e7\n\n----------------------------------------------------------------------\n\nif_then_else_ : \u2200{\u0393 C} (b : Value \u0393 `Bool) (c\u2081 c\u2082 : Value \u0393 C) \u2192 Value \u0393 C\nif `true then c\u2081 else c\u2082 = c\u2081\nif `false then c\u2081 else c\u2082 = c\u2082\nif `neut b then c\u2081 else c\u2082 = `neut (`if b `then c\u2081 `else c\u2082)\n\nelimBool : \u2200{\u0393 \u2113} (P : Value (\u0393 , `Bool) (`Type \u2113))\n (pt : Value \u0393 (subT \u27e6 P \u27e7 `true))\n (pf : Value \u0393 (subT \u27e6 P \u27e7 `false))\n (b : Value \u0393 `Bool)\n \u2192 Value \u0393 (subT \u27e6 P \u27e7 b)\nelimBool P pt pf `true = pt\nelimBool P pt pf `false = pf\nelimBool P pt pf (`neut b) = `neut (`elimBool P pt pf b)\n\n----------------------------------------------------------------------\n\nproj\u2081 : \u2200{\u0393 A B} \u2192 Value \u0393 (`\u03a3 A B) \u2192 Value \u0393 A\nproj\u2081 (a `, b) = a\nproj\u2081 (`neut ab) = `neut (`proj\u2081 ab)\n\nproj\u2082 : \u2200{\u0393 A B} (ab : Value \u0393 (`\u03a3 A B)) \u2192 Value \u0393 (subT B (proj\u2081 ab))\nproj\u2082 (a `, b) = b\nproj\u2082 (`neut ab) = `neut (`proj\u2082 ab)\n\n----------------------------------------------------------------------\n\n_$_ : \u2200{\u0393 A B} \u2192 Value \u0393 (`\u03a0 A B) \u2192 (a : Value \u0393 A) \u2192 Value \u0393 (subT B a)\n`\u03bb b $ a = subV b a\n`neut f $ a = `neut (f `$ a)\n\n----------------------------------------------------------------------\n\ndata Term (\u0393 : Context) : Type \u0393 \u2192 Set\neval : \u2200{\u0393 A} \u2192 Term \u0393 A \u2192 Value \u0393 A\n\ndata Term \u0393 where\n {- Type introduction -}\n `\u22a5 `\u22a4 `Bool `Type : \u2200{\u2113} \u2192 Term \u0393 (`Type \u2113)\n `\u03a0 `\u03a3 : \u2200{\u2113} (A : Term \u0393 (`Type \u2113)) (B : Term (\u0393 , \u27e6 eval A \u27e7) (`Type \u2113)) \u2192 Term \u0393 (`Type \u2113)\n `\u27e6_\u27e7 : \u2200{\u2113} \u2192 Term \u0393 (`Type \u2113) \u2192 Term \u0393 (`Type (suc \u2113))\n\n {- Value introduction -}\n `tt : Term \u0393 `\u22a4\n `true `false : Term \u0393 `Bool\n _`,_ : \u2200{A B}\n (a : Term \u0393 A) (b : Term \u0393 (subT B (eval a)))\n \u2192 Term \u0393 (`\u03a3 A B)\n \n {- Value elimination -}\n `var : \u2200{A} \u2192 Var \u0393 A \u2192 Term \u0393 A\n `if_`then_`else_ : \u2200{C}\n (b : Term \u0393 `Bool)\n (c\u2081 c\u2082 : Term \u0393 C)\n \u2192 Term \u0393 C\n _`$_ : \u2200{A B} (f : Term \u0393 (`\u03a0 A B)) (a : Term \u0393 A) \u2192 Term \u0393 (subT B (eval a))\n `proj\u2081 : \u2200{A B} \u2192 Term \u0393 (`\u03a3 A B) \u2192 Term \u0393 A\n `proj\u2082 : \u2200{A B} (ab : Term \u0393 (`\u03a3 A B)) \u2192 Term \u0393 (subT B (proj\u2081 (eval ab)))\n `elimBool : \u2200{\u2113} (P : Term (\u0393 , `Bool) (`Type \u2113))\n (pt : Term \u0393 (subT \u27e6 eval P \u27e7 `true))\n (pf : Term \u0393 (subT \u27e6 eval P \u27e7 `false))\n (b : Term \u0393 `Bool)\n \u2192 Term \u0393 (subT \u27e6 eval P \u27e7 (eval b))\n\n----------------------------------------------------------------------\n\n{- Type introduction -}\neval `\u22a5 = `\u22a5\neval `\u22a4 = `\u22a4\neval `Bool = `Bool\neval `Type = `Type\neval (`\u03a0 A B) = `\u03a0 (eval A) (eval B)\neval (`\u03a3 A B) = `\u03a3 (eval A) (eval B)\neval `\u27e6 A \u27e7 = `\u27e6 eval A \u27e7\n\n{- Value introduction -}\neval `tt = `tt\neval `true = `true\neval `false = `false\neval (a `, b) = eval a `, eval b\n\n{- Value elimination -}\neval (`var i) = `neut (`var i)\neval (`if b `then c\u2081 `else c\u2082) = if eval b then eval c\u2081 else eval c\u2082\neval (f `$ a) = eval f $ eval a\neval (`proj\u2081 ab) = proj\u2081 (eval ab)\neval (`proj\u2082 ab) = proj\u2082 (eval ab)\neval (`elimBool P pt pf b) = elimBool (eval P) (eval pt) (eval pf) (eval b)\n\n----------------------------------------------------------------------\n\n","old_contents":"module Spire.Operational where\n\n----------------------------------------------------------------------\n\ndata Level : Set where\n zero : Level\n suc : Level \u2192 Level\n\n----------------------------------------------------------------------\n\ndata Context : Set\ndata Type (\u0393 : Context) : Set\ndata Value (\u0393 : Context) : Type \u0393 \u2192 Set\ndata Neutral (\u0393 : Context) : Type \u0393 \u2192 Set\n\n----------------------------------------------------------------------\n\ndata Context where\n \u2205 : Context\n _,_ : (\u0393 : Context) \u2192 Type \u0393 \u2192 Context\n\ndata Type \u0393 where\n `\u22a5 `\u22a4 `Bool : Type \u0393\n `Type : (\u2113 : Level) \u2192 Type \u0393\n `\u03a0 `\u03a3 : (A : Type \u0393) (B : Type (\u0393 , A)) \u2192 Type \u0393\n `\u27e6_\u27e7 : \u2200{\u2113} \u2192 Neutral \u0393 (`Type \u2113) \u2192 Type \u0393\n\n----------------------------------------------------------------------\n\n\u27e6_\u27e7 : \u2200{\u0393 \u2113} \u2192 Value \u0393 (`Type \u2113) \u2192 Type \u0393\n\npostulate\n wknT : \u2200{\u0393 A} \u2192 Type \u0393 \u2192 Type (\u0393 , A)\n subT : \u2200{\u0393 A} \u2192 Type (\u0393 , A) \u2192 Value \u0393 A \u2192 Type \u0393\n subV : \u2200{\u0393 A B} \u2192 Value (\u0393 , A) B \u2192 (x : Value \u0393 A) \u2192 Value \u0393 (subT B x)\n\ndata Var : (\u0393 : Context) (A : Type \u0393) \u2192 Set where\n here : \u2200{\u0393 A} \u2192 Var (\u0393 , A) (wknT A)\n there : \u2200{\u0393 A B} \u2192 Var \u0393 A \u2192 Var (\u0393 , B) (wknT A)\n\n----------------------------------------------------------------------\n\ndata Value \u0393 where\n {- Type introduction -}\n `\u22a5 `\u22a4 `Bool `Type : \u2200{\u2113} \u2192 Value \u0393 (`Type \u2113)\n `\u03a0 `\u03a3 : \u2200{\u2113} (A : Value \u0393 (`Type \u2113)) (B : Value (\u0393 , \u27e6 A \u27e7) (`Type \u2113)) \u2192 Value \u0393 (`Type \u2113)\n `\u27e6_\u27e7 : \u2200{\u2113} \u2192 Value \u0393 (`Type \u2113) \u2192 Value \u0393 (`Type (suc \u2113))\n\n {- Value introduction -}\n `tt : Value \u0393 `\u22a4\n `true `false : Value \u0393 `Bool\n _`,_ : \u2200{A B} (a : Value \u0393 A) (b : Value \u0393 (subT B a)) \u2192 Value \u0393 (`\u03a3 A B)\n `\u03bb : \u2200{A B} \u2192 Value (\u0393 , A) B \u2192 Value \u0393 (`\u03a0 A B)\n `neut : \u2200{A} \u2192 Neutral \u0393 A \u2192 Value \u0393 A\n\n----------------------------------------------------------------------\n\ndata Neutral \u0393 where\n {- Value elimination -}\n `var : \u2200{A} \u2192 Var \u0393 A \u2192 Neutral \u0393 A\n `if_`then_`else_ : \u2200{C} (b : Neutral \u0393 `Bool) (c\u2081 c\u2082 : Value \u0393 C) \u2192 Neutral \u0393 C\n `proj\u2081 : \u2200{A B} \u2192 Neutral \u0393 (`\u03a3 A B) \u2192 Neutral \u0393 A\n `proj\u2082 : \u2200{A B} (ab : Neutral \u0393 (`\u03a3 A B)) \u2192 Neutral \u0393 (subT B (`neut (`proj\u2081 ab)))\n _`$_ : \u2200{A B} (f : Neutral \u0393 (`\u03a0 A B)) (a : Value \u0393 A) \u2192 Neutral \u0393 (subT B a)\n\n----------------------------------------------------------------------\n\n\u27e6 `\u03a0 A B \u27e7 = `\u03a0 \u27e6 A \u27e7 \u27e6 B \u27e7\n\u27e6 `\u03a3 A B \u27e7 = `\u03a3 \u27e6 A \u27e7 \u27e6 B \u27e7\n\u27e6 `\u22a5 \u27e7 = `\u22a5\n\u27e6 `\u22a4 \u27e7 = `\u22a4\n\u27e6 `Bool \u27e7 = `Bool\n\u27e6 `Type {zero} \u27e7 = `\u22a5\n\u27e6 `Type {suc \u2113} \u27e7 = `Type \u2113\n\u27e6 `\u27e6 A \u27e7 \u27e7 = \u27e6 A \u27e7\n\u27e6 `neut A \u27e7 = `\u27e6 A \u27e7\n\n----------------------------------------------------------------------\n\nif_then_else_ : \u2200{\u0393 C} (b : Value \u0393 `Bool) (c\u2081 c\u2082 : Value \u0393 C) \u2192 Value \u0393 C\nif `true then c\u2081 else c\u2082 = c\u2081\nif `false then c\u2081 else c\u2082 = c\u2082\nif `neut b then c\u2081 else c\u2082 = `neut (`if b `then c\u2081 `else c\u2082)\n\n----------------------------------------------------------------------\n\nproj\u2081 : \u2200{\u0393 A B} \u2192 Value \u0393 (`\u03a3 A B) \u2192 Value \u0393 A\nproj\u2081 (a `, b) = a\nproj\u2081 (`neut ab) = `neut (`proj\u2081 ab)\n\nproj\u2082 : \u2200{\u0393 A B} (ab : Value \u0393 (`\u03a3 A B)) \u2192 Value \u0393 (subT B (proj\u2081 ab))\nproj\u2082 (a `, b) = b\nproj\u2082 (`neut ab) = `neut (`proj\u2082 ab)\n\n----------------------------------------------------------------------\n\n_$_ : \u2200{\u0393 A B} \u2192 Value \u0393 (`\u03a0 A B) \u2192 (a : Value \u0393 A) \u2192 Value \u0393 (subT B a)\n`\u03bb b $ a = subV b a\n`neut f $ a = `neut (f `$ a)\n\n----------------------------------------------------------------------\n\ndata Term (\u0393 : Context) : Type \u0393 \u2192 Set\neval : \u2200{\u0393 A} \u2192 Term \u0393 A \u2192 Value \u0393 A\n\ndata Term \u0393 where\n {- Type introduction -}\n `\u22a5 `\u22a4 `Bool `Type : \u2200{\u2113} \u2192 Term \u0393 (`Type \u2113)\n `\u03a0 `\u03a3 : \u2200{\u2113} (A : Term \u0393 (`Type \u2113)) (B : Term (\u0393 , \u27e6 eval A \u27e7) (`Type \u2113)) \u2192 Term \u0393 (`Type \u2113)\n `\u27e6_\u27e7 : \u2200{\u2113} \u2192 Term \u0393 (`Type \u2113) \u2192 Term \u0393 (`Type (suc \u2113))\n\n {- Value introduction -}\n `tt : Term \u0393 `\u22a4\n `true `false : Term \u0393 `Bool\n _`,_ : \u2200{A B}\n (a : Term \u0393 A) (b : Term \u0393 (subT B (eval a)))\n \u2192 Term \u0393 (`\u03a3 A B)\n \n {- Value elimination -}\n `var : \u2200{A} \u2192 Var \u0393 A \u2192 Term \u0393 A\n `if_`then_`else_ : \u2200{C}\n (b : Term \u0393 `Bool)\n (c\u2081 c\u2082 : Term \u0393 C)\n \u2192 Term \u0393 C\n _`$_ : \u2200{A B} (f : Term \u0393 (`\u03a0 A B)) (a : Term \u0393 A) \u2192 Term \u0393 (subT B (eval a))\n `proj\u2081 : \u2200{A B} \u2192 Term \u0393 (`\u03a3 A B) \u2192 Term \u0393 A\n `proj\u2082 : \u2200{A B} (ab : Term \u0393 (`\u03a3 A B)) \u2192 Term \u0393 (subT B (proj\u2081 (eval ab)))\n\n----------------------------------------------------------------------\n\n{- Type introduction -}\neval `\u22a5 = `\u22a5\neval `\u22a4 = `\u22a4\neval `Bool = `Bool\neval `Type = `Type\neval (`\u03a0 A B) = `\u03a0 (eval A) (eval B)\neval (`\u03a3 A B) = `\u03a3 (eval A) (eval B)\neval `\u27e6 A \u27e7 = `\u27e6 eval A \u27e7\n\n{- Value introduction -}\neval `tt = `tt\neval `true = `true\neval `false = `false\neval (a `, b) = eval a `, eval b\n\n{- Value elimination -}\neval (`var i) = `neut (`var i)\neval (`if b `then c\u2081 `else c\u2082) = if eval b then eval c\u2081 else eval c\u2082\neval (f `$ a) = eval f $ eval a\neval (`proj\u2081 ab) = proj\u2081 (eval ab)\neval (`proj\u2082 ab) = proj\u2082 (eval ab)\n\n----------------------------------------------------------------------\n\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"b3753a80c591aeb5fa7f6af0bf11039337e9cc40","subject":"Redid model in terms of just leq, not leq and lt.","message":"Redid model in terms of just leq, not leq and lt.\n","repos":"agda\/agda-frp-js,agda\/agda-frp-js","old_file":"src\/agda\/FRP\/JS\/Model.agda","new_file":"src\/agda\/FRP\/JS\/Model.agda","new_contents":"open import FRP.JS.Level using ( Level ; _\u2294_ ) renaming ( zero to o ; suc to \u2191 )\nopen import FRP.JS.Time using ( Time ; _\u2264_ ; _<_ )\nopen import FRP.JS.Bool using ( Bool ; true ; false ; not ; _\u225f_ ) \nopen import FRP.JS.True using ( True ; tt )\n\nmodule FRP.JS.Model where\n\n-- This model is essentially System F-omega with a kind time\n-- together with a type for the partial order on time,\n-- and expressions for reflexivity and transitivity.\n-- We prove parametricity, and then show that parametricity implies causality.\n\n-- Note that this is a \"deep\" notion of causality, not the \"shallow\"\n-- causality usually used in FRP. The pragmatic upshot of this is that\n-- there is only one time model: nested signals are in the same time\n-- model, not a simulated time model. This fits with the JS implementation,\n-- which uses wall clock time for all signals.\n\n-- Propositional equality\n\ndata _\u2261_ {\u03b1} {A : Set \u03b1} (a : A) : A \u2192 Set \u03b1 where\n refl : a \u2261 a\n\nsym : \u2200 {\u03b1} {A : Set \u03b1} {a b : A} \u2192 (a \u2261 b) \u2192 (b \u2261 a)\nsym refl = refl\n\ntrans : \u2200 {\u03b1} {A : Set \u03b1} {a b c : A} \u2192 (a \u2261 b) \u2192 (b \u2261 c) \u2192 (a \u2261 c)\ntrans refl refl = refl\n\ncong : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : Set \u03b2} (f : A \u2192 B) {a\u2081 a\u2082 : A} \u2192\n (a\u2081 \u2261 a\u2082) \u2192 (f a\u2081 \u2261 f a\u2082)\ncong f refl = refl\n\napply : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : Set \u03b2} {F G : A \u2192 B} \u2192 (F \u2261 G) \u2192 \n \u2200 {a b} \u2192 (a \u2261 b) \u2192 (F a \u2261 G b)\napply refl refl = refl\n\ncast : \u2200 {\u03b1} {A B : Set \u03b1} \u2192 (A \u2261 B) \u2192 A \u2192 B\ncast refl a = a\n\ncast\u00b2 : \u2200 {\u03b1} {A B : Set \u03b1} {\u211c \u2111 : A \u2192 B \u2192 Set \u03b1} \u2192 (\u211c \u2261 \u2111) \u2192 \u2200 {a b} \u2192 \u211c a b \u2192 \u2111 a b\ncast\u00b2 refl a\u211cb = a\u211cb\n\nirrel : \u2200 b \u2192 (b\u2081 b\u2082 : True b) \u2192 (b\u2081 \u2261 b\u2082)\nirrel true tt tt = refl\nirrel false () ()\n\n-- Postulates (including dependent extensionality)\n\ndata _\u2264?_ (t u : Time) : Set where\n leq : True (t \u2264 u) \u2192 (t \u2264? u)\n geq : True (u \u2264 t) \u2192 (t \u2264? u)\n\npostulate\n \u2264-refl : \u2200 t \u2192 True (t \u2264 t)\n \u2264-trans : \u2200 t u v \u2192 True (t \u2264 u) \u2192 True (u \u2264 v) \u2192 True (t \u2264 v)\n \u2264-asym : \u2200 t u \u2192 True (t \u2264 u) \u2192 True (u \u2264 t) \u2192 (t \u2261 u)\n \u2264-total : \u2200 t u \u2192 (t \u2264? u) \n dext : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : A \u2192 Set \u03b2} {F G : \u2200 a \u2192 B a} \u2192 (\u2200 a \u2192 F a \u2261 G a) \u2192 (F \u2261 G)\n\next : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : Set \u03b2} {F G : A \u2192 B} \u2192\n (\u2200 a \u2192 F a \u2261 G a) \u2192 (F \u2261 G)\next = dext\n\niext : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : A \u2192 Set \u03b2} {F G : \u2200 {a} \u2192 B a} \u2192 \n (\u2200 a \u2192 F {a} \u2261 G {a}) \u2192 ((\u03bb {a} \u2192 F {a}) \u2261 (\u03bb {a} \u2192 G {a}))\niext F\u2248G = cong (\u03bb X {a} \u2192 X a) (dext F\u2248G)\n\n-- Finite products\n\nrecord \u22a4 {\u03b1} : Set \u03b1 where\n constructor tt\n\nopen \u22a4 public\n\nrecord \u03a3 {\u03b1 \u03b2} (A : Set \u03b1) (B : A \u2192 Set \u03b2) : Set (\u03b1 \u2294 \u03b2) where\n constructor _,_\n field\n proj\u2081 : A\n proj\u2082 : B proj\u2081\n\nopen \u03a3 public\n\n_\u00d7_ : \u2200 {\u03b1 \u03b2} \u2192 Set \u03b1 \u2192 Set \u03b2 \u2192 Set (\u03b1 \u2294 \u03b2)\nA \u00d7 B = \u03a3 A (\u03bb a \u2192 B)\n\n_\u00d7\u00b2_ : \u2200 {\u03b1 \u03b2} {A C : Set \u03b1} {B D : Set \u03b2} \u2192 \n (A \u2192 C \u2192 Set \u03b1) \u2192 (B \u2192 D \u2192 Set \u03b2) \u2192 ((A \u00d7 B) \u2192 (C \u00d7 D) \u2192 Set (\u03b1 \u2294 \u03b2))\n(\u211c \u00d7\u00b2 \u2111) (a , b) (c , d) = (\u211c a c \u00d7 \u2111 b d)\n\n_\u2192\u00b2_ : \u2200 {\u03b1 \u03b2} {A C : Set \u03b1} {B D : Set \u03b2} \u2192 \n (A \u2192 C \u2192 Set \u03b1) \u2192 (B \u2192 D \u2192 Set \u03b2) \u2192 ((A \u2192 B) \u2192 (C \u2192 D) \u2192 Set (\u03b1 \u2294 \u03b2))\n(\u211c \u2192\u00b2 \u2111) f g = \u2200 {a b} \u2192 \u211c a b \u2192 \u2111 (f a) (g b)\n\n-- Case on booleans\n\ndata Case (c : Bool) : Set where\n _,_ : \u2200 b \u2192 True (b \u225f c) \u2192 Case c\n\nswitch : \u2200 b \u2192 Case b\nswitch true = (true , tt)\nswitch false = (false , tt)\n\n-- Reactive sets\n\nRSet : \u2200 \u03b1 \u2192 Set (\u2191 \u03b1)\nRSet \u03b1 = Time \u2192 Set \u03b1\n\n-- Equalitional reasoning\n\ninfix 4 _IsRelatedTo_\ninfix 2 _\u220e\ninfixr 2 _\u2261\u27e8_\u27e9_\ninfix 1 begin_\n\ndata _IsRelatedTo_ {\u03b1} {A : Set \u03b1} (a b : A) : Set \u03b1 where\n relTo : (a\u2261b : a \u2261 b) \u2192 a IsRelatedTo b\n\nbegin_ : \u2200 {\u03b1} {A : Set \u03b1} {a b : A} \u2192 a IsRelatedTo b \u2192 a \u2261 b\nbegin relTo a\u2261b = a\u2261b\n\n_\u2261\u27e8_\u27e9_ : \u2200 {\u03b1} {A : Set \u03b1} a {b c : A} \u2192 a \u2261 b \u2192 b IsRelatedTo c \u2192 a IsRelatedTo c\n_ \u2261\u27e8 a\u2261b \u27e9 relTo b\u2261c = relTo (trans a\u2261b b\u2261c)\n\n_\u220e : \u2200 {\u03b1} {A : Set \u03b1} (a : A) \u2192 a IsRelatedTo a\n_\u220e _ = relTo refl\n\n-- Kinds\n\ndata Kind : Set where\n time : Kind\n set : Level \u2192 Kind\n _\u21d2_ : Kind \u2192 Kind \u2192 Kind\n\nlevel : Kind \u2192 Level\nlevel time = o\nlevel (set \u03b1) = \u2191 \u03b1\nlevel (K \u21d2 L) = level K \u2294 level L\n\nK\u27e6_\u27e7 : \u2200 K \u2192 Set (level K)\nK\u27e6 time \u27e7 = Time\nK\u27e6 set \u03b1 \u27e7 = Set \u03b1\nK\u27e6 K \u21d2 L \u27e7 = K\u27e6 K \u27e7 \u2192 K\u27e6 L \u27e7\n\n_\u220b_\u2194_ : \u2200 K \u2192 K\u27e6 K \u27e7 \u2192 K\u27e6 K \u27e7 \u2192 Set (level K)\ntime \u220b t \u2194 u = (t \u2261 u)\nset \u03b1 \u220b A \u2194 B = A \u2192 B \u2192 Set \u03b1\n(K \u21d2 L) \u220b F \u2194 G = \u2200 {A B} \u2192 (K \u220b A \u2194 B) \u2192 (L \u220b F A \u2194 G B)\n\n-- \u2261 can be used as a structural equivalence on relations.\n\nstruct : \u2200 K {A B C D} \u2192 (A \u2261 B) \u2192 (K \u220b B \u2194 D) \u2192 (C \u2261 D) \u2192 (K \u220b A \u2194 C)\nstruct K refl \u211c refl = \u211c\n\nstruct-ext : \u2200 K L {A B} {F G H I : K\u27e6 K \u21d2 L \u27e7} \n (F\u2248G : \u2200 A \u2192 F A \u2261 G A) (\u211c : (K \u21d2 L) \u220b G \u2194 I) (H\u2248I : \u2200 B \u2192 H B \u2261 I B) (\u2111 : K \u220b A \u2194 B) \u2192\n struct L (F\u2248G A) (\u211c \u2111) (H\u2248I B) \u2261 struct (K \u21d2 L) (ext F\u2248G) \u211c (ext H\u2248I) \u2111\nstruct-ext K L {A} {B} F\u2248G \u211c H\u2248I \u2111 \n with ext F\u2248G | ext H\u2248I | F\u2248G A | H\u2248I B\n... | refl | refl | refl | refl = refl\n\nstruct-apply : \u2200 K L {F G H I A B C D} \u2192 \n (F\u2261G : F \u2261 G) (\u211c : (K \u21d2 L) \u220b G \u2194 I) (H\u2261I : H \u2261 I) \u2192 \n (A\u2261B : A \u2261 B) (\u2111 : K \u220b B \u2194 D) (C\u2261D : C \u2261 D) \u2192 \n struct (K \u21d2 L) F\u2261G \u211c H\u2261I (struct K A\u2261B \u2111 C\u2261D)\n \u2261 struct L (apply F\u2261G A\u2261B) (\u211c \u2111) (apply H\u2261I C\u2261D)\nstruct-apply K L refl \u211c refl refl \u2111 refl = refl\n\nstruct-cast : \u2200 {\u03b1 A B C D} (\u211c : set \u03b1 \u220b B \u2194 D) (A\u2261B : A \u2261 B) (C\u2261D : C \u2261 D) {a c} \u2192\n struct (set \u03b1) A\u2261B \u211c C\u2261D a c \u2192 \u211c (cast A\u2261B a) (cast C\u2261D c)\nstruct-cast \u211c refl refl a\u211cc = a\u211cc\n\nstruct-sym : \u2200 K {A B C D \u2111 \u211c} \u2192 (A\u2261B : A \u2261 B) \u2192 (C\u2261D : C \u2261 D) \u2192\n (\u2111 \u2261 struct K A\u2261B \u211c C\u2261D) \u2192 \n (\u211c \u2261 struct K (sym A\u2261B) \u2111 (sym C\u2261D))\nstruct-sym K refl refl refl = refl\n\nstruct-trans : \u2200 K {A B C D E F}\n (A\u2261B : A \u2261 B) (B\u2261C : B \u2261 C) (\u211c : K \u220b C \u2194 F) (E\u2261F : E \u2261 F) (D\u2261E : D \u2261 E) \u2192\n struct K A\u2261B (struct K B\u2261C \u211c E\u2261F) D\u2261E \u2261\n struct K (trans A\u2261B B\u2261C) \u211c (trans D\u2261E E\u2261F)\nstruct-trans K refl refl \u211c refl refl = refl\n\n-- Type contexts\n\ninfixr 4 _\u2237_\n\ndata Kinds : Set where\n [] : Kinds\n _\u2237_ : Kind \u2192 Kinds \u2192 Kinds\n\nlevels : Kinds \u2192 Level\nlevels [] = o\nlevels (K \u2237 \u03a3) = level K \u2294 levels \u03a3\n\n\u03a3\u27e6_\u27e7 : \u2200 \u03a3 \u2192 Set (levels \u03a3)\n\u03a3\u27e6 [] \u27e7 = \u22a4\n\u03a3\u27e6 K \u2237 \u03a3 \u27e7 = K\u27e6 K \u27e7 \u00d7 \u03a3\u27e6 \u03a3 \u27e7\n\n_\u220b_\u2194*_ : \u2200 \u03a3 \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 Set (levels \u03a3)\n[] \u220b tt \u2194* tt = \u22a4\n(K \u2237 \u03a3) \u220b (A , As) \u2194* (B , Bs) = (K \u220b A \u2194 B) \u00d7 (\u03a3 \u220b As \u2194* Bs)\n\n-- Inclusion order on type contexts.\n-- Credited by Randy Pollack to Geuvers and Nederhof, JAR 1991.\n-- http:\/\/thread.gmane.org\/gmane.comp.lang.agda\/3259\/focus=3267\n\ndata _\u2291_ : Kinds \u2192 Kinds \u2192 Set where\n id : \u2200 {\u03a3} \u2192 \u03a3 \u2291 \u03a3\n keep : \u2200 K {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 ((K \u2237 \u03a3) \u2291 (K \u2237 \u03a5))\n skip : \u2200 K {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 (\u03a3 \u2291 (K \u2237 \u03a5))\n\n\u2291\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 \u03a3\u27e6 \u03a5 \u27e7 \u2192 \u03a3\u27e6 \u03a3 \u27e7\n\u2291\u27e6 id \u27e7 As = As\n\u2291\u27e6 keep K \u03a3\u2291\u03a5 \u27e7 (A , As) = (A , \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As)\n\u2291\u27e6 skip K \u03a3\u2291\u03a5 \u27e7 (A , As) = \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As\n\n\u2291\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5} \u2192 (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) \u2192 \u2200 {As Bs} \u2192 (\u03a5 \u220b As \u2194* Bs) \u2192 (\u03a3 \u220b \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As \u2194* \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 Bs)\n\u2291\u27e6 id \u27e7\u00b2 \u211cs = \u211cs\n\u2291\u27e6 keep K \u03a3\u2291\u03a5 \u27e7\u00b2 (\u211c , \u211cs) = (\u211c , \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)\n\u2291\u27e6 skip K \u03a3\u2291\u03a5 \u27e7\u00b2 (\u211c , \u211cs) = \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs\n\n-- Concatenation of type contexts\n\n_++_ : Kinds \u2192 Kinds \u2192 Kinds\n[] ++ \u03a5 = \u03a5\n(K \u2237 \u03a3) ++ \u03a5 = K \u2237 (\u03a3 ++ \u03a5)\n\n_\u220b_++_\u220b_ : \u2200 \u03a3 \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 \u2200 \u03a5 \u2192 \u03a3\u27e6 \u03a5 \u27e7 \u2192 \u03a3\u27e6 \u03a3 ++ \u03a5 \u27e7\n[] \u220b tt ++ \u03a5 \u220b Bs = Bs\n(K \u2237 \u03a3) \u220b (A , As) ++ \u03a5 \u220b Bs = (A , (\u03a3 \u220b As ++ \u03a5 \u220b Bs))\n\n_\u220b_++\u00b2_\u220b_ : \u2200 \u03a3 {As Bs} \u2192 (\u03a3 \u220b As \u2194* Bs) \u2192 \u2200 \u03a5 {Cs Ds} \u2192 (\u03a5 \u220b Cs \u2194* Ds) \u2192 \n ((\u03a3 ++ \u03a5) \u220b (\u03a3 \u220b As ++ \u03a5 \u220b Cs) \u2194* (\u03a3 \u220b Bs ++ \u03a5 \u220b Ds))\n[] \u220b tt ++\u00b2 \u03a5 \u220b \u2111s = \u2111s\n(K \u2237 \u03a3) \u220b (\u211c , \u211cs) ++\u00b2 \u03a5 \u220b \u2111s = (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n\n-- Type variables\n\ndata TVar (K : Kind) : Kinds \u2192 Set where\n zero : \u2200 {\u03a3} \u2192 TVar K (K \u2237 \u03a3)\n suc : \u2200 {L \u03a3} \u2192 TVar K \u03a3 \u2192 TVar K (L \u2237 \u03a3)\n\n\u03c4\u27e6_\u27e7 : \u2200 {\u03a3 K} (\u03c4 : TVar K \u03a3) \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 K\u27e6 K \u27e7\n\u03c4\u27e6 zero \u27e7 (A , As) = A\n\u03c4\u27e6 suc \u03c4 \u27e7 (A , As) = \u03c4\u27e6 \u03c4 \u27e7 As\n\n\u03c4\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 K} (\u03c4 : TVar K \u03a3) {As Bs} \u2192 (\u03a3 \u220b As \u2194* Bs) \u2192 (K \u220b \u03c4\u27e6 \u03c4 \u27e7 As \u2194 \u03c4\u27e6 \u03c4 \u27e7 Bs)\n\u03c4\u27e6 zero \u27e7\u00b2 (\u211c , \u211cs) = \u211c\n\u03c4\u27e6 suc \u03c4 \u27e7\u00b2 (\u211c , \u211cs) = \u03c4\u27e6 \u03c4 \u27e7\u00b2 \u211cs\n\n-- Type constants\n\ndata TConst : Kind \u2192 Set where\n prod fun : \u2200 {\u03b1 \u03b2} \u2192 TConst (set \u03b1 \u21d2 (set \u03b2 \u21d2 set (\u03b1 \u2294 \u03b2)))\n leq : TConst (time \u21d2 (time \u21d2 set o))\n univ : \u2200 K {\u03b1} \u2192 TConst ((K \u21d2 set \u03b1) \u21d2 set (level K \u2294 \u03b1))\n\nC\u27e6_\u27e7 : \u2200 {K} \u2192 (TConst K) \u2192 K\u27e6 K \u27e7\nC\u27e6 prod \u27e7 = \u03bb A B \u2192 (A \u00d7 B)\nC\u27e6 fun \u27e7 = \u03bb A B \u2192 (A \u2192 B)\nC\u27e6 leq \u27e7 = \u03bb t u \u2192 True (t \u2264 u)\nC\u27e6 univ K \u27e7 = \u03bb F \u2192 \u2200 A \u2192 F A\n\nC\u27e6_\u27e7\u00b2 : \u2200 {K} (C : TConst K) \u2192 (K \u220b C\u27e6 C \u27e7 \u2194 C\u27e6 C \u27e7)\nC\u27e6 prod \u27e7\u00b2 = \u03bb \u211c \u2111 \u2192 (\u211c \u00d7\u00b2 \u2111)\nC\u27e6 fun \u27e7\u00b2 = \u03bb \u211c \u2111 \u2192 (\u211c \u2192\u00b2 \u2111)\nC\u27e6 leq \u27e7\u00b2 = \u03bb _ _ _ _ \u2192 \u22a4\nC\u27e6 univ K \u27e7\u00b2 = \u03bb \u211c f g \u2192 \u2200 {a b} \u2111 \u2192 \u211c \u2111 (f a) (g b)\n\n-- Types\n\ndata Typ (\u03a3 : Kinds) : Kind \u2192 Set where\n const : \u2200 {K} \u2192 TConst K \u2192 Typ \u03a3 K\n abs : \u2200 K {L} \u2192 Typ (K \u2237 \u03a3) L \u2192 Typ \u03a3 (K \u21d2 L)\n app : \u2200 {K L} \u2192 Typ \u03a3 (K \u21d2 L) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L\n var : \u2200 {K} \u2192 TVar K \u03a3 \u2192 Typ \u03a3 K\n\ntlevel : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (set \u03b1) \u2192 Level\ntlevel {\u03a3} {\u03b1} T = \u03b1\n\nT\u27e6_\u27e7 : \u2200 {\u03a3 K} (T : Typ \u03a3 K) \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 K\u27e6 K \u27e7\nT\u27e6 const C \u27e7 As = C\u27e6 C \u27e7\nT\u27e6 abs K T \u27e7 As = \u03bb A \u2192 T\u27e6 T \u27e7 (A , As)\nT\u27e6 app T U \u27e7 As = T\u27e6 T \u27e7 As (T\u27e6 U \u27e7 As)\nT\u27e6 var \u03c4 \u27e7 As = \u03c4\u27e6 \u03c4 \u27e7 As\n\nT\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 K} (T : Typ \u03a3 K) {As Bs} \u2192 (\u03a3 \u220b As \u2194* Bs) \u2192 (K \u220b T\u27e6 T \u27e7 As \u2194 T\u27e6 T \u27e7 Bs)\nT\u27e6 const C \u27e7\u00b2 \u211cs = C\u27e6 C \u27e7\u00b2\nT\u27e6 abs K T \u27e7\u00b2 \u211cs = \u03bb \u211c \u2192 T\u27e6 T \u27e7\u00b2 (\u211c , \u211cs)\nT\u27e6 app T U \u27e7\u00b2 \u211cs = T\u27e6 T \u27e7\u00b2 \u211cs (T\u27e6 U \u27e7\u00b2 \u211cs)\nT\u27e6 var \u03c4 \u27e7\u00b2 \u211cs = \u03c4\u27e6 \u03c4 \u27e7\u00b2 \u211cs\n\n-- Type shorthands\n\napp\u2082 : \u2200 {\u03a3 K L M} \u2192 Typ \u03a3 (K \u21d2 (L \u21d2 M)) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L \u2192 Typ \u03a3 M\napp\u2082 T U V = app (app T U) V\n\ncapp : \u2200 {\u03a3 K L} \u2192 TConst (K \u21d2 L) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L\ncapp C = app (const C)\n\ncapp\u2082 : \u2200 {\u03a3 K L M} \u2192 TConst (K \u21d2 (L \u21d2 M)) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L \u2192 Typ \u03a3 M\ncapp\u2082 C = app\u2082 (const C)\n\n_\u2297_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (set \u03b1) \u2192 Typ \u03a3 (set \u03b2) \u2192 Typ \u03a3 (set (\u03b1 \u2294 \u03b2))\n_\u2297_ = capp\u2082 prod\n\n_\u22b8_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (set \u03b1) \u2192 Typ \u03a3 (set \u03b2) \u2192 Typ \u03a3 (set (\u03b1 \u2294 \u03b2))\n_\u22b8_ = capp\u2082 fun\n\n_\u227c_ : \u2200 {\u03a3} \u2192 Typ \u03a3 time \u2192 Typ \u03a3 time \u2192 Typ \u03a3 (set o)\n_\u227c_ = capp\u2082 leq\n\n\u03a0 : \u2200 {\u03a3 \u03b1} K \u2192 Typ (K \u2237 \u03a3) (set \u03b1) \u2192 Typ \u03a3 (set (level K \u2294 \u03b1))\n\u03a0 K T = capp (univ K) (abs K T)\n\ntvar\u2080 : \u2200 {\u03a3 K} \u2192 Typ (K \u2237 \u03a3) K\ntvar\u2080 = var zero\n\ntvar\u2081 : \u2200 {\u03a3 K L} \u2192 Typ (L \u2237 K \u2237 \u03a3) K\ntvar\u2081 = var (suc zero)\n\ntvar\u2082 : \u2200 {\u03a3 K L M} \u2192 Typ (M \u2237 L \u2237 K \u2237 \u03a3) K\ntvar\u2082 = var (suc (suc zero))\n\ntvar\u2083 : \u2200 {\u03a3 K L M N} \u2192 Typ (N \u2237 M \u2237 L \u2237 K \u2237 \u03a3) K\ntvar\u2083 = var (suc (suc (suc zero)))\n\nrset : Level \u2192 Kind\nrset \u03b1 = time \u21d2 set \u03b1\n\nrset\u2080 : Kind\nrset\u2080 = rset o\n\nprod\u02b3 : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 (rset \u03b2 \u21d2 rset (\u03b1 \u2294 \u03b2)))\nprod\u02b3 {\u03a3} {\u03b1} {\u03b2} = abs (rset \u03b1) (abs (rset \u03b2) (abs time (app tvar\u2082 tvar\u2080 \u2297 app tvar\u2081 tvar\u2080)))\n\n_\u2297\u02b3_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1) \u2192 Typ \u03a3 (rset \u03b2) \u2192 Typ \u03a3 (rset (\u03b1 \u2294 \u03b2))\n_\u2297\u02b3_ = app\u2082 prod\u02b3\n\nfun\u02b3 : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 (rset \u03b2 \u21d2 rset (\u03b1 \u2294 \u03b2)))\nfun\u02b3 {\u03a3} {\u03b1} {\u03b2} = abs (rset \u03b1) (abs (rset \u03b2) (abs time (app tvar\u2082 tvar\u2080 \u22b8 app tvar\u2081 tvar\u2080)))\n\n_\u22b8\u02b3_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1) \u2192 Typ \u03a3 (rset \u03b2) \u2192 Typ \u03a3 (rset (\u03b1 \u2294 \u03b2))\n_\u22b8\u02b3_ = app\u2082 fun\u02b3\n\nalways : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (set \u03b1 \u21d2 rset \u03b1)\nalways {\u03a3} {\u03b1} = abs (set \u03b1) (abs time tvar\u2081)\n\ninterval : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 (time \u21d2 (time \u21d2 set \u03b1)))\ninterval {\u03a3} {\u03b1} = abs (rset \u03b1) (abs time (abs time (\u03a0 time \n ((tvar\u2082 \u227c tvar\u2080) \u22b8 ((tvar\u2080 \u227c tvar\u2081) \u22b8 app tvar\u2083 tvar\u2080)))))\n\n_[_,_] : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (rset \u03b1) \u2192 Typ \u03a3 time \u2192 Typ \u03a3 time \u2192 Typ \u03a3 (set \u03b1)\nT [ t , u ] = app (app (app interval T) t) u\n\nconstreq : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 (rset \u03b2 \u21d2 rset (\u03b1 \u2294 \u03b2)))\nconstreq {\u03a3} {\u03b1} {\u03b2} = abs (rset \u03b1) (abs (rset \u03b2) (abs time (\u03a0 time \n ((tvar\u2081 \u227c tvar\u2080) \u22b8 ((tvar\u2083 [ tvar\u2081 , tvar\u2080 ]) \u22b8 app tvar\u2082 tvar\u2080)))))\n\n_\u22b5_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1) \u2192 Typ \u03a3 (rset \u03b2) \u2192 Typ \u03a3 (rset (\u03b1 \u2294 \u03b2))\nT \u22b5 U = app\u2082 constreq T U\n\n-- Contexts\n\ndata Typs (\u03a3 : Kinds) : Set where\n [] : Typs \u03a3\n _\u2237_ : \u2200 {\u03b1} \u2192 (Typ \u03a3 (set \u03b1)) \u2192 Typs \u03a3 \u2192 Typs \u03a3\n\ntlevels : \u2200 {\u03a3} \u2192 Typs \u03a3 \u2192 Level\ntlevels [] = o\ntlevels (T \u2237 \u0393) = tlevel T \u2294 tlevels \u0393\n\n\u0393\u27e6_\u27e7 : \u2200 {\u03a3} (\u0393 : Typs \u03a3) \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 Set (tlevels \u0393)\n\u0393\u27e6 [] \u27e7 As = \u22a4\n\u0393\u27e6 T \u2237 \u0393 \u27e7 As = T\u27e6 T \u27e7 As \u00d7 \u0393\u27e6 \u0393 \u27e7 As\n\n\u0393\u27e6_\u27e7\u00b2 : \u2200 {\u03a3} (\u0393 : Typs \u03a3) {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 (\u0393\u27e6 \u0393 \u27e7 As \u2192 \u0393\u27e6 \u0393 \u27e7 Bs \u2192 Set (tlevels \u0393))\n\u0393\u27e6 [] \u27e7\u00b2 \u211cs tt tt = \u22a4\n\u0393\u27e6 T \u2237 \u0393 \u27e7\u00b2 \u211cs (a , as) (b , bs) = T\u27e6 T \u27e7\u00b2 \u211cs a b \u00d7 \u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs as bs\n\n-- Weakening of type variables\n\n\u03c4weaken : \u2200 {\u03a3 \u03a5 K} \u2192 (\u03a3 \u2291 \u03a5) \u2192 TVar K \u03a3 \u2192 TVar K \u03a5\n\u03c4weaken id x = x\n\u03c4weaken (keep K \u03a3\u2291\u03a5) zero = zero\n\u03c4weaken (keep K \u03a3\u2291\u03a5) (suc x) = suc (\u03c4weaken \u03a3\u2291\u03a5 x)\n\u03c4weaken (skip K \u03a3\u2291\u03a5) x = suc (\u03c4weaken \u03a3\u2291\u03a5 x)\n\n\u03c4weaken\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5 K} (\u03c4 : TVar K \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) (As : \u03a3\u27e6 \u03a5 \u27e7) \u2192 \n \u03c4\u27e6 \u03c4 \u27e7 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As) \u2261 \u03c4\u27e6 \u03c4weaken \u03a3\u2291\u03a5 \u03c4 \u27e7 As\n\u03c4weaken\u27e6 \u03c4 \u27e7 id As = refl\n\u03c4weaken\u27e6 zero \u27e7 (keep K \u03a3\u2291\u03a5) (A , As) = refl\n\u03c4weaken\u27e6 suc \u03c4 \u27e7 (keep K \u03a3\u2291\u03a5) (A , As) = \u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As\n\u03c4weaken\u27e6 \u03c4 \u27e7 (skip K \u03a3\u2291\u03a5) (A , As) = \u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As\n\n\u03c4weaken\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5 K} (\u03c4 : TVar K \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) {As Bs} (\u211cs : \u03a5 \u220b As \u2194* Bs) \u2192 \n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) \u2261 \n struct K (\u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As) (\u03c4\u27e6 \u03c4weaken \u03a3\u2291\u03a5 \u03c4 \u27e7\u00b2 \u211cs) (\u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 Bs)\n\u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 id \u211cs = refl\n\u03c4weaken\u27e6 zero \u27e7\u00b2 (keep K \u03a3\u2291\u03a5) (\u211c , \u211cs) = refl\n\u03c4weaken\u27e6 suc \u03c4 \u27e7\u00b2 (keep K \u03a3\u2291\u03a5) (\u211c , \u211cs) = \u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs\n\u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 (skip K \u03a3\u2291\u03a5) (\u211c , \u211cs) = \u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs\n\n-- Weakening of types\n\nweaken : \u2200 {\u03a3 \u03a5 K} \u2192 (\u03a3 \u2291 \u03a5) \u2192 Typ \u03a3 K \u2192 Typ \u03a5 K\nweaken \u03a3\u2291\u03a5 (const C) = const C\nweaken \u03a3\u2291\u03a5 (abs K T) = abs K (weaken (keep K \u03a3\u2291\u03a5) T)\nweaken \u03a3\u2291\u03a5 (app T U) = app (weaken \u03a3\u2291\u03a5 T) (weaken \u03a3\u2291\u03a5 U)\nweaken \u03a3\u2291\u03a5 (var \u03c4) = var (\u03c4weaken \u03a3\u2291\u03a5 \u03c4)\n\nweaken\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5 K} (T : Typ \u03a3 K) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) (As : \u03a3\u27e6 \u03a5 \u27e7) \u2192 \n T\u27e6 T \u27e7 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As) \u2261 T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7 As\nweaken\u27e6 const C \u27e7 \u03a3\u2291\u03a5 As = refl\nweaken\u27e6 abs K T \u27e7 \u03a3\u2291\u03a5 As = ext (\u03bb A \u2192 weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (A , As))\nweaken\u27e6 app T U \u27e7 \u03a3\u2291\u03a5 As = apply (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) \nweaken\u27e6 var \u03c4 \u27e7 \u03a3\u2291\u03a5 As = \u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As\n \nweaken\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5 K} (T : Typ \u03a3 K) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) {As Bs} (\u211cs : \u03a5 \u220b As \u2194* Bs) \u2192 \n T\u27e6 T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) \u2261 struct K (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 Bs)\nweaken\u27e6 const C \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs = refl\nweaken\u27e6 abs K {L} T \u27e7\u00b2 \u03a3\u2291\u03a5 {As} {Bs} \u211cs =\n iext (\u03bb A \u2192 iext (\u03bb B \u2192 ext (\u03bb \u211c \u2192 begin\n T\u27e6 abs K T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) \u211c\n \u2261\u27e8 weaken\u27e6 T \u27e7\u00b2 (keep K \u03a3\u2291\u03a5) (\u211c , \u211cs) \u27e9\n struct L \n (weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (A , As)) \n (T\u27e6 weaken (keep K \u03a3\u2291\u03a5) T \u27e7\u00b2 (\u211c , \u211cs)) \n (weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (B , Bs))\n \u2261\u27e8 struct-ext K L \n (\u03bb A \u2192 weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (A , As)) \n (\u03bb \u211c \u2192 T\u27e6 weaken (keep K \u03a3\u2291\u03a5) T \u27e7\u00b2 (\u211c , \u211cs)) \n (\u03bb B \u2192 weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (B , Bs)) \u211c \u27e9\n struct (K \u21d2 L) \n (weaken\u27e6 abs K T \u27e7 \u03a3\u2291\u03a5 As)\n (T\u27e6 weaken \u03a3\u2291\u03a5 (abs K T) \u27e7\u00b2 \u211cs) \n (weaken\u27e6 abs K T \u27e7 \u03a3\u2291\u03a5 Bs) \u211c\n \u220e)))\nweaken\u27e6 app {K} {L} T U \u27e7\u00b2 \u03a3\u2291\u03a5 {As} {Bs} \u211cs = \n begin\n T\u27e6 app T U \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)\n \u2261\u27e8 cong (T\u27e6 T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)) (weaken\u27e6 U \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs) \u27e9\n T\u27e6 T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)\n (struct K (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs))\n \u2261\u27e8 cong (\u03bb X \u2192 X (struct K (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs)))\n (weaken\u27e6 T \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs) \u27e9\n (struct (K \u21d2 L) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 Bs))\n (struct K (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs))\n \u2261\u27e8 struct-apply K L \n (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 Bs) \n (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs) \u27e9\n struct L\n (weaken\u27e6 app T U \u27e7 \u03a3\u2291\u03a5 As)\n (T\u27e6 weaken \u03a3\u2291\u03a5 (app T U) \u27e7\u00b2 \u211cs) \n (weaken\u27e6 app T U \u27e7 \u03a3\u2291\u03a5 Bs)\n \u220e\nweaken\u27e6 var \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs = \u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs\n\n-- Weakening on type contexts\n\nweakens : \u2200 {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 Typs \u03a3 \u2192 Typs \u03a5\nweakens \u03a3\u2291\u03a5 [] = []\nweakens \u03a3\u2291\u03a5 (T \u2237 \u0393) = weaken \u03a3\u2291\u03a5 T \u2237 weakens \u03a3\u2291\u03a5 \u0393\n\nweakens\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5} (\u0393 : Typs \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) (As : \u03a3\u27e6 \u03a5 \u27e7) \u2192 \n \u0393\u27e6 \u0393 \u27e7 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As) \u2192 \u0393\u27e6 weakens \u03a3\u2291\u03a5 \u0393 \u27e7 As\nweakens\u27e6 [] \u27e7 \u03a3\u2291\u03a5 As tt = tt\nweakens\u27e6 T \u2237 \u0393 \u27e7 \u03a3\u2291\u03a5 As (B , Bs) = (cast (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) B , weakens\u27e6 \u0393 \u27e7 \u03a3\u2291\u03a5 As Bs)\n\nweakens\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5} (\u0393 : Typs \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) {As Bs} (\u211cs : \u03a5 \u220b As \u2194* Bs) {as bs} \u2192 \n \u0393\u27e6 \u0393 \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) as bs \u2192 \n \u0393\u27e6 weakens \u03a3\u2291\u03a5 \u0393 \u27e7\u00b2 \u211cs (weakens\u27e6 \u0393 \u27e7 \u03a3\u2291\u03a5 As as) (weakens\u27e6 \u0393 \u27e7 \u03a3\u2291\u03a5 Bs bs)\nweakens\u27e6 [] \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs tt\n = tt\nweakens\u27e6 T \u2237 \u0393 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs (a\u211cb , as\u211cbs) \n = ( struct-cast (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) \n (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 _) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 _) (cast\u00b2 (weaken\u27e6 T \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs) a\u211cb)\n , weakens\u27e6 \u0393 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs as\u211cbs)\n\n-- Susbtitution on type variables under a context\n\n\u03c4substn+ : \u2200 \u03a3 {\u03a5 K L} \u2192 TVar K (\u03a3 ++ (L \u2237 \u03a5)) \u2192 Typ \u03a5 L \u2192 Typ (\u03a3 ++ \u03a5) K\n\u03c4substn+ [] zero U = U\n\u03c4substn+ [] (suc \u03c4) U = var \u03c4\n\u03c4substn+ (K \u2237 \u03a3) zero U = var zero\n\u03c4substn+ (K \u2237 \u03a3) (suc \u03c4) U = weaken (skip K id) (\u03c4substn+ \u03a3 \u03c4 U)\n\n\u03c4substn+_\u27e6_\u27e7\u27e6_\u27e7 : \u2200 \u03a3 {\u03a5 K L} (\u03c4 : TVar K (\u03a3 ++ (L \u2237 \u03a5))) (U : Typ \u03a5 L) \n (As : \u03a3\u27e6 \u03a3 \u27e7) (Bs : \u03a3\u27e6 \u03a5 \u27e7) \u2192\n \u03c4\u27e6 \u03c4 \u27e7 (\u03a3 \u220b As ++ (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7 Bs , Bs)) \u2261 \n T\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (\u03a3 \u220b As ++ \u03a5 \u220b Bs)\n\u03c4substn+ [] \u27e6 zero \u27e7\u27e6 U \u27e7 tt Bs = refl\n\u03c4substn+ [] \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7 tt Bs = refl\n\u03c4substn+ (K \u2237 \u03a3) \u27e6 zero \u27e7\u27e6 U \u27e7 (A , As) Bs = refl\n\u03c4substn+ (K \u2237 \u03a3) \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7 (A , As) Bs = trans \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Bs) \n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip K id) (A , (\u03a3 \u220b As ++ _ \u220b Bs)))\n\n\u03c4substn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 : \u2200 \u03a3 {\u03a5 L K} (\u03c4 : TVar K (\u03a3 ++ (L \u2237 \u03a5))) (U : Typ \u03a5 L) {As Bs Cs Ds} \n (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 (\u2111s : \u03a5 \u220b Cs \u2194* Ds) \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s)) \u2261 \n struct K \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s) )\n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds)\n\u03c4substn+ [] \u27e6 zero \u27e7\u27e6 U \u27e7\u00b2 tt \u2111s = refl\n\u03c4substn+ [] \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7\u00b2 tt \u2111s = refl\n\u03c4substn+ (J \u2237 \u03a3) \u27e6 zero \u27e7\u27e6 U \u27e7\u00b2 (\u211c , \u211cs) \u2111s = refl\n\u03c4substn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 (J \u2237 \u03a3) {\u03a5} {L} {K} (suc \u03c4) U {A , As} {B , Bs} {Cs} {Ds} (\u211c , \u211cs) \u2111s = \n begin\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s))\n \u2261\u27e8 \u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s \u27e9\n struct K \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds)\n \u2261\u27e8 cong (\u03bb X \u2192 struct K (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs) X (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds)) \n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7\u00b2 (skip J id) (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))) \u27e9\n struct K \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs) \n (struct K\n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip J id) (A , (\u03a3 \u220b As ++ \u03a5 \u220b Cs))) \n (T\u27e6 weaken (skip J id) (\u03c4substn+ \u03a3 \u03c4 U) \u27e7\u00b2 (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))) \n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip J id) (B , (\u03a3 \u220b Bs ++ \u03a5 \u220b Ds)))) \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds)\n \u2261\u27e8 struct-trans K \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs) \n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip J id) (A , (\u03a3 \u220b As ++ \u03a5 \u220b Cs)))\n (T\u27e6 weaken (skip J id) (\u03c4substn+ \u03a3 \u03c4 U) \u27e7\u00b2 (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s)))\n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip J id) (B , (\u03a3 \u220b Bs ++ \u03a5 \u220b Ds)))\n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds) \u27e9\n struct K \n (\u03c4substn+ (J \u2237 \u03a3) \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7 (A , As) Cs) \n (T\u27e6 \u03c4substn+ (J \u2237 \u03a3) (suc \u03c4) U \u27e7\u00b2 (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s)) )\n (\u03c4substn+ (J \u2237 \u03a3) \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7 (B , Bs) Ds) \n \u220e\n\n-- Substitution on types under a context\n\nsubstn+ : \u2200 \u03a3 {\u03a5 K L} \u2192 Typ (\u03a3 ++ (L \u2237 \u03a5)) K \u2192 Typ \u03a5 L \u2192 Typ (\u03a3 ++ \u03a5) K\nsubstn+ \u03a3 (const C) U = const C\nsubstn+ \u03a3 (abs K T) U = abs K (substn+ (K \u2237 \u03a3) T U)\nsubstn+ \u03a3 (app S T) U = app (substn+ \u03a3 S U) (substn+ \u03a3 T U)\nsubstn+ \u03a3 (var \u03c4) U = \u03c4substn+ \u03a3 \u03c4 U\n\nsubstn+_\u27e6_\u27e7\u27e6_\u27e7 : \u2200 \u03a3 {\u03a5 K L} (T : Typ (\u03a3 ++ (L \u2237 \u03a5)) K) (U : Typ \u03a5 L) \n (As : \u03a3\u27e6 \u03a3 \u27e7) (Bs : \u03a3\u27e6 \u03a5 \u27e7) \u2192\n T\u27e6 T \u27e7 (\u03a3 \u220b As ++ (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7 Bs , Bs)) \u2261 \n T\u27e6 substn+ \u03a3 T U \u27e7 (\u03a3 \u220b As ++ \u03a5 \u220b Bs)\nsubstn+ \u03a3 \u27e6 const C \u27e7\u27e6 U \u27e7 As Bs = refl\nsubstn+ \u03a3 \u27e6 abs K T \u27e7\u27e6 U \u27e7 As Bs = ext (\u03bb A \u2192 substn+ K \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (A , As) Bs)\nsubstn+ \u03a3 \u27e6 app S T \u27e7\u27e6 U \u27e7 As Bs = apply (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 As Bs) (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Bs)\nsubstn+ \u03a3 \u27e6 var \u03c4 \u27e7\u27e6 U \u27e7 As Bs = \u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Bs\n\nsubstn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 : \u2200 \u03a3 {\u03a5 L K} (T : Typ (\u03a3 ++ (L \u2237 \u03a5)) K) (U : Typ \u03a5 L) {As Bs Cs Ds} \n (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 (\u2111s : \u03a5 \u220b Cs \u2194* Ds) \u2192\n T\u27e6 T \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s)) \u2261 \n struct K \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s) )\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds)\nsubstn+ \u03a3 \u27e6 const C \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s = refl\nsubstn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 \u03a3 {\u03a5} {L} (abs J {K} T) U {As} {Bs} {Cs} {Ds} \u211cs \u2111s = \n iext (\u03bb A \u2192 iext (\u03bb B \u2192 ext (\u03bb \u211c \u2192 begin\n T\u27e6 abs J T \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s)) \u211c\n \u2261\u27e8 substn+ (J \u2237 \u03a3) \u27e6 T \u27e7\u27e6 U \u27e7\u00b2 (\u211c , \u211cs) \u2111s \u27e9\n struct K \n (substn+ J \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (A , As) Cs) \n (T\u27e6 substn+ (J \u2237 \u03a3) T U \u27e7\u00b2 ((J \u2237 \u03a3) \u220b (\u211c , \u211cs) ++\u00b2 \u03a5 \u220b \u2111s)) \n (substn+ J \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (B , Bs) Ds)\n \u2261\u27e8 struct-ext J K \n (\u03bb A \u2192 substn+ J \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (A , As) Cs) \n (\u03bb \u211c \u2192 T\u27e6 substn+ (J \u2237 \u03a3) T U \u27e7\u00b2 ((J \u2237 \u03a3) \u220b \u211c , \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (\u03bb B \u2192 substn+ J \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (B , Bs) Ds) \u211c \u27e9\n struct (J \u21d2 K) \n (substn+ \u03a3 \u27e6 abs J T \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 substn+ \u03a3 (abs J T) U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s)) \n (substn+ \u03a3 \u27e6 abs J T \u27e7\u27e6 U \u27e7 Bs Ds) \u211c\n \u220e)))\nsubstn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 \u03a3 {\u03a5} {L} (app {J} {K} S T) U {As} {Bs} {Cs} {Ds} \u211cs \u2111s = \n begin\n T\u27e6 app S T \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 L \u2237 \u03a5 \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s))\n \u2261\u27e8 cong (T\u27e6 S \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 L \u2237 \u03a5 \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s))) (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s) \u27e9\n T\u27e6 S \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 L \u2237 \u03a5 \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s))\n (struct J \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds))\n \u2261\u27e8 cong (\u03bb X \u2192 X (struct J \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds))) \n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s) \u27e9\n struct (J \u21d2 K) \n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 substn+ \u03a3 S U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 Bs Ds) \n (struct J \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds))\n \u2261\u27e8 struct-apply J K \n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 substn+ \u03a3 S U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s)) \n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 Bs Ds) \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds) \u27e9\n struct K \n (substn+ \u03a3 \u27e6 app S T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 (app S T) U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 app S T \u27e7\u27e6 U \u27e7 Bs Ds)\n \u220e\nsubstn+ \u03a3 \u27e6 var \u03c4 \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s = \u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s\n\n-- Substitution on types\n\nsubstn : \u2200 {\u03a3 K L} \u2192 Typ (L \u2237 \u03a3) K \u2192 Typ \u03a3 L \u2192 Typ \u03a3 K\nsubstn = substn+ []\n\nsubstn\u27e6_\u27e7\u27e6_\u27e7 : \u2200 {\u03a3 K L} (T : Typ (L \u2237 \u03a3) K) (U : Typ \u03a3 L) (As : \u03a3\u27e6 \u03a3 \u27e7)\u2192\n T\u27e6 T \u27e7 (T\u27e6 U \u27e7 As , As) \u2261 T\u27e6 substn T U \u27e7 As\nsubstn\u27e6 T \u27e7\u27e6 U \u27e7 = substn+ [] \u27e6 T \u27e7\u27e6 U \u27e7 tt\n\nsubstn\u27e6_\u27e7\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 K L} (T : Typ (L \u2237 \u03a3) K) (U : Typ \u03a3 L) {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192\n T\u27e6 T \u27e7\u00b2 (T\u27e6 U \u27e7\u00b2 \u211cs , \u211cs) \u2261 \n struct K (substn\u27e6 T \u27e7\u27e6 U \u27e7 As) (T\u27e6 substn T U \u27e7\u00b2 \u211cs) (substn\u27e6 T \u27e7\u27e6 U \u27e7 Bs)\nsubstn\u27e6 T \u27e7\u27e6 U \u27e7\u00b2 = substn+ [] \u27e6 T \u27e7\u27e6 U \u27e7\u00b2 tt\n\n-- Eta-beta equivalence on types\n\ndata _\u220b_\u2263_ {\u03a3} : \u2200 K \u2192 Typ \u03a3 K \u2192 Typ \u03a3 K \u2192 Set where\n abs : \u2200 K {L T U} \u2192 (L \u220b T \u2263 U) \u2192 ((K \u21d2 L) \u220b abs K T \u2263 abs K U)\n app : \u2200 {K L F G T U} \u2192 ((K \u21d2 L) \u220b F \u2263 G) \u2192 (K \u220b T \u2263 U) \u2192 (L \u220b app F T \u2263 app G U)\n beta : \u2200 {K L} T U \u2192 (L \u220b app (abs K T) U \u2263 substn T U)\n eta : \u2200 {K L} T \u2192 ((K \u21d2 L) \u220b T \u2263 abs K (app (weaken (skip K id) T) tvar\u2080))\n \u2263-refl : \u2200 {K T} \u2192 (K \u220b T \u2263 T)\n \u2263-sym : \u2200 {K T U} \u2192 (K \u220b T \u2263 U) \u2192 (K \u220b U \u2263 T)\n \u2263-trans : \u2200 {K T U V} \u2192 (K \u220b T \u2263 U) \u2192 (K \u220b U \u2263 V) \u2192 (K \u220b T \u2263 V)\n\n\u2263\u27e6_\u27e7 : \u2200 {\u03a3 K} {T U : Typ \u03a3 K} \u2192 (K \u220b T \u2263 U) \u2192 \u2200 As \u2192 T\u27e6 T \u27e7 As \u2261 T\u27e6 U \u27e7 As\n\u2263\u27e6 abs K T\u2263U \u27e7 As = ext (\u03bb A \u2192 \u2263\u27e6 T\u2263U \u27e7 (A , As))\n\u2263\u27e6 app F\u2263G T\u2263U \u27e7 As = apply (\u2263\u27e6 F\u2263G \u27e7 As) (\u2263\u27e6 T\u2263U \u27e7 As)\n\u2263\u27e6 beta T U \u27e7 As = substn\u27e6 T \u27e7\u27e6 U \u27e7 As\n\u2263\u27e6 eta {K} T \u27e7 As = ext (\u03bb A \u2192 apply (weaken\u27e6 T \u27e7 (skip K id) (A , As)) refl)\n\u2263\u27e6 \u2263-refl \u27e7 As = refl\n\u2263\u27e6 \u2263-sym T\u2263U \u27e7 As = sym (\u2263\u27e6 T\u2263U \u27e7 As)\n\u2263\u27e6 \u2263-trans T\u2263U U\u2263V \u27e7 As = trans (\u2263\u27e6 T\u2263U \u27e7 As) (\u2263\u27e6 U\u2263V \u27e7 As)\n\n\u2263\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 K} {T U : Typ \u03a3 K} (T\u2263U : K \u220b T \u2263 U) {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 \n T\u27e6 T \u27e7\u00b2 \u211cs \u2261 struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs)\n\u2263\u27e6 abs K {L} {T} {U} T\u2263U \u27e7\u00b2 {As} {Bs} \u211cs = \n iext (\u03bb A \u2192 iext (\u03bb B \u2192 ext (\u03bb \u211c \u2192 begin\n T\u27e6 T \u27e7\u00b2 (\u211c , \u211cs)\n \u2261\u27e8 \u2263\u27e6 T\u2263U \u27e7\u00b2 (\u211c , \u211cs) \u27e9\n struct L (\u2263\u27e6 T\u2263U \u27e7 (A , As)) (T\u27e6 U \u27e7\u00b2 (\u211c , \u211cs)) (\u2263\u27e6 T\u2263U \u27e7 (B , Bs))\n \u2261\u27e8 struct-ext K L (\u03bb A \u2192 \u2263\u27e6 T\u2263U \u27e7 (A , As)) (\u03bb \u211c' \u2192 T\u27e6 U \u27e7\u00b2 (\u211c' , \u211cs)) (\u03bb B \u2192 \u2263\u27e6 T\u2263U \u27e7 (B , Bs)) \u211c \u27e9\n struct (K \u21d2 L) (\u2263\u27e6 abs K T\u2263U \u27e7 As) (T\u27e6 abs K U \u27e7\u00b2 \u211cs) (\u2263\u27e6 abs K T\u2263U \u27e7 Bs) \u211c\n \u220e)))\n\u2263\u27e6 app {K} {L} {F} {G} {T} {U} F\u2263G T\u2263U \u27e7\u00b2 {As} {Bs} \u211cs = \n begin\n T\u27e6 app F T \u27e7\u00b2 \u211cs\n \u2261\u27e8 cong (T\u27e6 F \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7\u00b2 \u211cs) \u27e9\n T\u27e6 F \u27e7\u00b2 \u211cs (struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs))\n \u2261\u27e8 cong (\u03bb X \u2192 X (struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs)))\n (\u2263\u27e6 F\u2263G \u27e7\u00b2 \u211cs) \u27e9\n struct (K \u21d2 L) (\u2263\u27e6 F\u2263G \u27e7 As) (T\u27e6 G \u27e7\u00b2 \u211cs) (\u2263\u27e6 F\u2263G \u27e7 Bs)\n (struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs))\n \u2261\u27e8 struct-apply K L\n (\u2263\u27e6 F\u2263G \u27e7 As) (T\u27e6 G \u27e7\u00b2 \u211cs) (\u2263\u27e6 F\u2263G \u27e7 Bs)\n (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs) \u27e9\n struct L (\u2263\u27e6 app F\u2263G T\u2263U \u27e7 As) (T\u27e6 app G U \u27e7\u00b2 \u211cs) (\u2263\u27e6 app F\u2263G T\u2263U \u27e7 Bs)\n \u220e\n\u2263\u27e6 beta T U \u27e7\u00b2 \u211cs = substn\u27e6 T \u27e7\u27e6 U \u27e7\u00b2 \u211cs\n\u2263\u27e6 eta {K} {L} T \u27e7\u00b2 {As} {Bs} \u211cs = iext (\u03bb A \u2192 iext (\u03bb B \u2192 ext (\u03bb \u211c \u2192 \n begin\n T\u27e6 T \u27e7\u00b2 \u211cs \u211c\n \u2261\u27e8 cong (\u03bb X \u2192 X \u211c) (weaken\u27e6 T \u27e7\u00b2 (skip K id) (\u211c , \u211cs)) \u27e9\n struct (K \u21d2 L) \n (weaken\u27e6 T \u27e7 (skip K id) (A , As))\n (T\u27e6 weaken (skip K id) T \u27e7\u00b2 (\u211c , \u211cs))\n (weaken\u27e6 T \u27e7 (skip K id) (B , Bs)) \u211c\n \u2261\u27e8 struct-apply K L \n (weaken\u27e6 T \u27e7 (skip K id) (A , As)) \n (T\u27e6 weaken (skip K id) T \u27e7\u00b2 (\u211c , \u211cs)) \n (weaken\u27e6 T \u27e7 (skip K id) (B , Bs)) refl \u211c refl \u27e9\n struct L \n (apply (weaken\u27e6 T \u27e7 (skip K id) (A , As)) refl)\n (T\u27e6 weaken (skip K id) T \u27e7\u00b2 (\u211c , \u211cs) \u211c)\n (apply (weaken\u27e6 T \u27e7 (skip K id) (B , Bs)) refl)\n \u2261\u27e8 struct-ext K L\n (\u03bb A \u2192 apply (weaken\u27e6 T \u27e7 (skip K id) (A , As)) refl) \n (\u03bb \u211c \u2192 T\u27e6 weaken (skip K id) T \u27e7\u00b2 (\u211c , \u211cs) \u211c) \n (\u03bb B \u2192 apply (weaken\u27e6 T \u27e7 (skip K id) (B , Bs)) refl) \u211c \u27e9\n struct (K \u21d2 L) \n (\u2263\u27e6 eta T \u27e7 As)\n (T\u27e6 abs K (app (weaken (skip K id) T) (var zero)) \u27e7\u00b2 \u211cs)\n (\u2263\u27e6 eta T \u27e7 Bs) \u211c\n \u220e)))\n\u2263\u27e6 \u2263-refl \u27e7\u00b2 \u211cs = refl\n\u2263\u27e6 \u2263-sym {K} {T} {U} T\u2263U \u27e7\u00b2 {As} {Bs} \u211cs = \n struct-sym K (\u2263\u27e6 T\u2263U \u27e7 As) (\u2263\u27e6 T\u2263U \u27e7 Bs) (\u2263\u27e6 T\u2263U \u27e7\u00b2 \u211cs)\n\u2263\u27e6 \u2263-trans {K} {T} {U} {V} T\u2263U U\u2263V \u27e7\u00b2 {As} {Bs} \u211cs =\n begin\n T\u27e6 T \u27e7\u00b2 \u211cs\n \u2261\u27e8 \u2263\u27e6 T\u2263U \u27e7\u00b2 \u211cs \u27e9\n struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs)\n \u2261\u27e8 cong (\u03bb X \u2192 struct K (\u2263\u27e6 T\u2263U \u27e7 As) X (\u2263\u27e6 T\u2263U \u27e7 Bs)) (\u2263\u27e6 U\u2263V \u27e7\u00b2 \u211cs) \u27e9\n struct K (\u2263\u27e6 T\u2263U \u27e7 As) (struct K (\u2263\u27e6 U\u2263V \u27e7 As) (T\u27e6 V \u27e7\u00b2 \u211cs) (\u2263\u27e6 U\u2263V \u27e7 Bs)) (\u2263\u27e6 T\u2263U \u27e7 Bs)\n \u2261\u27e8 struct-trans K (\u2263\u27e6 T\u2263U \u27e7 As) (\u2263\u27e6 U\u2263V \u27e7 As) (T\u27e6 V \u27e7\u00b2 \u211cs) (\u2263\u27e6 U\u2263V \u27e7 Bs) (\u2263\u27e6 T\u2263U \u27e7 Bs) \u27e9\n struct K (\u2263\u27e6 \u2263-trans T\u2263U U\u2263V \u27e7 As) (T\u27e6 V \u27e7\u00b2 \u211cs) (\u2263\u27e6 \u2263-trans T\u2263U U\u2263V \u27e7 Bs)\n \u220e\n\n-- Variables\n\ndata Var {\u03a3 : Kinds} {\u03b1} (T : Typ \u03a3 (set \u03b1)) : Typs \u03a3 \u2192 Set where\n zero : \u2200 {\u0393} \u2192 Var T (T \u2237 \u0393)\n suc : \u2200 {\u03b2 \u0393} {U : Typ \u03a3 (set \u03b2)} \u2192 Var T \u0393 \u2192 Var T (U \u2237 \u0393)\n\nx\u27e6_\u27e7 : \u2200 {\u03a3} {\u0393 : Typs \u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} \u2192 \n Var T \u0393 \u2192 (As : \u03a3\u27e6 \u03a3 \u27e7) \u2192 (as : \u0393\u27e6 \u0393 \u27e7 As) \u2192 (T\u27e6 T \u27e7 As)\nx\u27e6 zero \u27e7 As (a , as) = a\nx\u27e6 suc x \u27e7 As (a , as) = x\u27e6 x \u27e7 As as\n\nx\u27e6_\u27e7\u00b2 : \u2200 {\u03a3} {\u0393 : Typs \u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} (x : Var T \u0393) \u2192 \n \u2200 {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) {as bs} \u2192 \n (\u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs as bs) \u2192 (T\u27e6 T \u27e7\u00b2 \u211cs (x\u27e6 x \u27e7 As as) (x\u27e6 x \u27e7 Bs bs))\nx\u27e6 zero \u27e7\u00b2 \u211cs (a\u211cb , as\u211cbs) = a\u211cb\nx\u27e6 suc x \u27e7\u00b2 \u211cs (a\u211cb , as\u211cbs) = x\u27e6 x \u27e7\u00b2 \u211cs as\u211cbs\n\n-- Constants\n\ndata Const {\u03a3 : Kinds} : \u2200 {\u03b1} \u2192 Typ \u03a3 (set \u03b1) \u2192 Set where\n pair : \u2200 {\u03b1 \u03b2} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 (set \u03b2) (tvar\u2081 \u22b8 (tvar\u2080 \u22b8 (tvar\u2081 \u2297 tvar\u2080)))))\n fst : \u2200 {\u03b1 \u03b2} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 (set \u03b2) ((tvar\u2081 \u2297 tvar\u2080) \u22b8 tvar\u2081)))\n snd : \u2200 {\u03b1 \u03b2} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 (set \u03b2) ((tvar\u2081 \u2297 tvar\u2080) \u22b8 tvar\u2080)))\n \u227c-refl : Const (\u03a0 time (tvar\u2080 \u227c tvar\u2080))\n \u227c-trans : Const (\u03a0 time (\u03a0 time (\u03a0 time ((tvar\u2082 \u227c tvar\u2081) \u22b8 ((tvar\u2081 \u227c tvar\u2080) \u22b8 (tvar\u2082 \u227c tvar\u2080))))))\n \u227c-antisym : \u2200 {\u03b1} \u2192 Const (\u03a0 (rset \u03b1) (\u03a0 time (\u03a0 time ((tvar\u2081 \u227c tvar\u2080) \u22b8 ((tvar\u2080 \u227c tvar\u2081) \u22b8 (app tvar\u2082 tvar\u2081 \u22b8 app tvar\u2082 tvar\u2080))))))\n \u227c-case : \u2200 {\u03b1} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 time (\u03a0 time (((tvar\u2081 \u227c tvar\u2080) \u22b8 tvar\u2082) \u22b8 (((tvar\u2080 \u227c tvar\u2081) \u22b8 tvar\u2082) \u22b8 tvar\u2082)))))\n\n\u2264-antisym : \u2200 {\u03b1} (A : RSet \u03b1) t u \u2192 True (t \u2264 u) \u2192 True (u \u2264 t) \u2192 A t \u2192 A u\n\u2264-antisym A t u t\u2264u u\u2264t a with \u2264-asym t u t\u2264u u\u2264t\n\u2264-antisym A t .t _ _ a | refl = a\n\n\u2264-case\u2032 : \u2200 {\u03b1} {A : Set \u03b1} {t u} \u2192 (t \u2264? u) \u2192 (True (t \u2264 u) \u2192 A) \u2192 (True (u \u2264 t) \u2192 A) \u2192 A\n\u2264-case\u2032 (leq t\u2264u) f g = f t\u2264u\n\u2264-case\u2032 (geq u\u2264t) f g = g u\u2264t\n\n\u2264-case : \u2200 {\u03b1} (A : Set \u03b1) t u \u2192 (True (t \u2264 u) \u2192 A) \u2192 (True (u \u2264 t) \u2192 A) \u2192 A\n\u2264-case A t u = \u2264-case\u2032 (\u2264-total t u)\n\nc\u27e6_\u27e7 : \u2200 {\u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} \u2192 \n Const T \u2192 (As : \u03a3\u27e6 \u03a3 \u27e7) \u2192 (T\u27e6 T \u27e7 As)\nc\u27e6 pair \u27e7 As = \u03bb A B a b \u2192 (a , b)\nc\u27e6 fst \u27e7 As = \u03bb A B \u2192 proj\u2081\nc\u27e6 snd \u27e7 As = \u03bb A B \u2192 proj\u2082\nc\u27e6 \u227c-refl \u27e7 As = \u2264-refl\nc\u27e6 \u227c-trans \u27e7 As = \u2264-trans\nc\u27e6 \u227c-antisym \u27e7 As = \u2264-antisym\nc\u27e6 \u227c-case \u27e7 As = \u2264-case\n\nc\u27e6_\u27e7\u00b2 : \u2200 {\u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} (c : Const T) \u2192 \n \u2200 {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 \n (T\u27e6 T \u27e7\u00b2 \u211cs (c\u27e6 c \u27e7 As) (c\u27e6 c \u27e7 Bs))\nc\u27e6 pair \u27e7\u00b2 \u211cs = \u03bb \u211c \u2111 a\u211cb c\u2111d \u2192 (a\u211cb , c\u2111d)\nc\u27e6 fst \u27e7\u00b2 \u211cs = \u03bb \u211c \u2111 \u2192 proj\u2081\nc\u27e6 snd \u27e7\u00b2 \u211cs = \u03bb \u211c \u2111 \u2192 proj\u2082\nc\u27e6 \u227c-refl \u27e7\u00b2 \u211cs = _\nc\u27e6 \u227c-trans \u27e7\u00b2 \u211cs = _\nc\u27e6 \u227c-antisym {\u03b1} \u27e7\u00b2 \u211cs = lemma where\n lemma : \u2200 {\u03b1} {A B : RSet \u03b1} (\u211c : rset \u03b1 \u220b A \u2194 B) \u2192 \n {t u : Time} \u2192 (t\u2261u : t \u2261 u) \u2192 {v w : Time} \u2192 (v\u2261w : v \u2261 w) \u2192\n {t\u2264v : True (t \u2264 v)} {u\u2264w : True (u \u2264 w)} \u2192 \u22a4 \u2192\n {v\u2264t : True (v \u2264 t)} {w\u2264u : True (w \u2264 u)} \u2192 \u22a4 \u2192\n {a : A t} {b : B u} \u2192 \u211c t\u2261u a b \u2192\n \u211c v\u2261w (\u2264-antisym A t v t\u2264v v\u2264t a) (\u2264-antisym B u w u\u2264w w\u2264u b)\n lemma \u211c {t} refl {v} refl {t\u2264v} {u\u2264w} tt {v\u2264t} {w\u2264u} tt a\u211cb \n with irrel (t \u2264 v) t\u2264v u\u2264w | irrel (v \u2264 t) v\u2264t w\u2264u\n lemma \u211c {t} refl {v} refl {t\u2264v} tt {v\u2264t} tt a\u211cb\n | refl | refl with \u2264-asym t v t\u2264v v\u2264t\n lemma \u211c refl refl tt tt a\u211cb\n | refl | refl | refl = a\u211cb\nc\u27e6 \u227c-case {\u03b1} \u27e7\u00b2 \u211cs = lemma where\n lemma : \u2200 {\u03b1} {A B : Set \u03b1} (\u211c : set \u03b1 \u220b A \u2194 B) \u2192\n \u2200 {t u : Time} \u2192 (t\u2261u : t \u2261 u) \u2192 \u2200 {v w : Time} \u2192 (v\u2261w : v \u2261 w) \u2192 \n \u2200 {f g} \u2192 (\u2200 {t\u2264v} {u\u2264w} \u2192 \u22a4 \u2192 \u211c (f t\u2264v) (g u\u2264w)) \u2192\n \u2200 {h i} \u2192 (\u2200 {v\u2264t} {w\u2264u} \u2192 \u22a4 \u2192 \u211c (h v\u2264t) (i w\u2264u)) \u2192\n \u211c (\u2264-case A t v f h) (\u2264-case B u w g i)\n lemma \u211c {t} refl {v} refl {f} {g} f\u211cg {h} {i} h\u211ci = lemma\u2032 (\u2264-total t v) where\n lemma\u2032 : \u2200 t\u2264?v \u2192 \u211c (\u2264-case\u2032 t\u2264?v f h) (\u2264-case\u2032 t\u2264?v g i)\n lemma\u2032 (leq t\u2264v) = f\u211cg {t\u2264v} {t\u2264v} tt\n lemma\u2032 (geq v\u2264t) = h\u211ci {v\u2264t} {v\u2264t} tt\n \n-- Expressions\n\ndata Exp {\u03a3 : Kinds} (\u0393 : Typs \u03a3) : \u2200 {\u03b1} \u2192 Typ \u03a3 (set \u03b1) \u2192 Set where\n const : \u2200 {\u03b1} {T : Typ \u03a3 (set \u03b1)} \u2192 Const T \u2192 Exp \u0393 T\n abs : \u2200 {\u03b1 \u03b2} (T : Typ \u03a3 (set \u03b1)) {U : Typ \u03a3 (set \u03b2)} (M : Exp (T \u2237 \u0393) U) \u2192 Exp \u0393 (T \u22b8 U)\n app : \u2200 {\u03b1 \u03b2} {T : Typ \u03a3 (set \u03b1)} {U : Typ \u03a3 (set \u03b2)} (M : Exp \u0393 (T \u22b8 U)) (N : Exp \u0393 T) \u2192 Exp \u0393 U\n var : \u2200 {\u03b1} {T : Typ \u03a3 (set \u03b1)} \u2192 Var T \u0393 \u2192 Exp \u0393 T\n tabs : \u2200 K {\u03b1} {T : Typ (K \u2237 \u03a3) (set \u03b1)} (M : Exp (weakens (skip K id) \u0393) T) \u2192 Exp \u0393 (\u03a0 K T)\n tapp : \u2200 {K \u03b1} {T : Typ (K \u2237 \u03a3) (set \u03b1)} \u2192 Exp \u0393 (\u03a0 K T) \u2192 \u2200 U \u2192 Exp \u0393 (substn T U)\n eq : \u2200 {\u03b1 T U} \u2192 (set \u03b1 \u220b T \u2263 U) \u2192 (Exp \u0393 T) \u2192 (Exp \u0393 U)\n\nctxt : \u2200 {\u03a3 \u0393 \u03b1 T} \u2192 Exp {\u03a3} \u0393 {\u03b1} T \u2192 Typs \u03a3\nctxt {\u03a3} {\u0393} M = \u0393\n\nM\u27e6_\u27e7 : \u2200 {\u03a3} {\u0393 : Typs \u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} \u2192 \n Exp \u0393 T \u2192 (As : \u03a3\u27e6 \u03a3 \u27e7) \u2192 (as : \u0393\u27e6 \u0393 \u27e7 As) \u2192 (T\u27e6 T \u27e7 As)\nM\u27e6 const c \u27e7 As as = c\u27e6 c \u27e7 As\nM\u27e6 abs T M \u27e7 As as = \u03bb a \u2192 M\u27e6 M \u27e7 As (a , as)\nM\u27e6 app M N \u27e7 As as = M\u27e6 M \u27e7 As as (M\u27e6 N \u27e7 As as)\nM\u27e6 var x \u27e7 As as = x\u27e6 x \u27e7 As as\nM\u27e6 tabs K M \u27e7 As as = \u03bb A \u2192 \n M\u27e6 M \u27e7 (A , As) (weakens\u27e6 ctxt (tabs K M) \u27e7 (skip K id) (A , As) as)\nM\u27e6 tapp {T = T} M U \u27e7 As as = \n cast (substn\u27e6 T \u27e7\u27e6 U \u27e7 As) (M\u27e6 M \u27e7 As as (T\u27e6 U \u27e7 As))\nM\u27e6 eq T\u2263U M \u27e7 As as = cast (\u2263\u27e6 T\u2263U \u27e7 As) (M\u27e6 M \u27e7 As as)\n\nM\u27e6_\u27e7\u00b2 : \u2200 {\u03a3} {\u0393 : Typs \u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} (M : Exp \u0393 T) \u2192 \n \u2200 {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) {as bs} \u2192 \n (\u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs as bs) \u2192 (T\u27e6 T \u27e7\u00b2 \u211cs (M\u27e6 M \u27e7 As as) (M\u27e6 M \u27e7 Bs bs))\nM\u27e6 const c \u27e7\u00b2 \u211cs as\u211cbs = c\u27e6 c \u27e7\u00b2 \u211cs\nM\u27e6 abs T M \u27e7\u00b2 \u211cs as\u211cbs = \u03bb a\u211cb \u2192 M\u27e6 M \u27e7\u00b2 \u211cs (a\u211cb , as\u211cbs)\nM\u27e6 app M N \u27e7\u00b2 \u211cs as\u211cbs = M\u27e6 M \u27e7\u00b2 \u211cs as\u211cbs (M\u27e6 N \u27e7\u00b2 \u211cs as\u211cbs)\nM\u27e6 var x \u27e7\u00b2 \u211cs as\u211cbs = x\u27e6 x \u27e7\u00b2 \u211cs as\u211cbs\nM\u27e6 tabs K M \u27e7\u00b2 \u211cs as\u211cbs = \u03bb \u211c \u2192 \n M\u27e6 M \u27e7\u00b2 (\u211c , \u211cs) (weakens\u27e6 ctxt (tabs K M) \u27e7\u00b2 (skip K id) (\u211c , \u211cs) as\u211cbs)\nM\u27e6 tapp {T = T} M U \u27e7\u00b2 \u211cs as\u211cbs = \n struct-cast (T\u27e6 substn T U \u27e7\u00b2 \u211cs) (substn\u27e6 T \u27e7\u27e6 U \u27e7 _) (substn\u27e6 T \u27e7\u27e6 U \u27e7 _)\n (cast\u00b2 (substn\u27e6 T \u27e7\u27e6 U \u27e7\u00b2 \u211cs) (M\u27e6 M \u27e7\u00b2 \u211cs as\u211cbs (T\u27e6 U \u27e7\u00b2 \u211cs)))\nM\u27e6 eq {\u03b1} {T} {U} T\u2263U M \u27e7\u00b2 {As} {Bs} \u211cs as\u211cbs = \n struct-cast (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 As) (\u2263\u27e6 T\u2263U \u27e7 Bs) (cast\u00b2 (\u2263\u27e6 T\u2263U \u27e7\u00b2 \u211cs) (M\u27e6 M \u27e7\u00b2 \u211cs as\u211cbs))\n\n-- Types with a chosen free world variable\n\n_\u2237\u02b3_ : Kinds \u2192 Kind \u2192 Kinds\n[] \u2237\u02b3 K = K \u2237 []\n(T \u2237 \u03a3) \u2237\u02b3 K = T \u2237 (\u03a3 \u2237\u02b3 K)\n\nTVar+ : Kind \u2192 Kinds \u2192 Set\nTVar+ K \u03a3 = TVar K (\u03a3 \u2237\u02b3 rset\u2080)\n\nTyp+ : Kinds \u2192 Kind \u2192 Set\nTyp+ \u03a3 = Typ (\u03a3 \u2237\u02b3 rset\u2080)\n\nwvar : \u2200 \u03a3 \u2192 TVar+ rset\u2080 \u03a3\nwvar [] = zero\nwvar (K \u2237 \u03a3) = suc (wvar \u03a3)\n\nworld : \u2200 {\u03a3} \u2192 Typ+ \u03a3 rset\u2080\nworld {\u03a3} = var (wvar \u03a3)\n\nWorld : Time \u2192 Set\nWorld t = \u22a4\n\ntaut : \u2200 {\u03a3 \u03b1} \u2192 Typ+ \u03a3 (rset \u03b1 \u21d2 set \u03b1)\ntaut {\u03a3} {\u03b1} = abs (rset \u03b1) (\u03a0 time \n (app (world {time \u2237 rset \u03b1 \u2237 \u03a3}) tvar\u2080 \u22b8 app tvar\u2081 tvar\u2080))\n\n-- Surface types\n\ndata STyp : Kind \u2192 Set where\n \u27e8_\u27e9 : \u2200 {\u03b1} \u2192 STyp (set \u03b1) \u2192 STyp (rset \u03b1)\n [_] : \u2200 {\u03b1} \u2192 STyp (rset \u03b1) \u2192 STyp (set \u03b1)\n _\u22a0_ _\u21a6_ : \u2200 {\u03b1 \u03b2} \u2192 STyp (set \u03b1) \u2192 STyp (set \u03b2) \u2192 STyp (set (\u03b1 \u2294 \u03b2))\n _\u2227_ _\u21d2_ : \u2200 {\u03b1 \u03b2} \u2192 STyp (rset \u03b1) \u2192 STyp (rset \u03b2) \u2192 STyp (rset (\u03b1 \u2294 \u03b2))\n \u25a1 : \u2200 {\u03b1} \u2192 STyp (rset \u03b1) \u2192 STyp (rset \u03b1)\n\n\u27ea_\u27eb : \u2200 {K} \u2192 STyp K \u2192 Typ+ [] K\n\u27ea \u27e8 T \u27e9 \u27eb = app always \u27ea T \u27eb\n\u27ea [ T ] \u27eb = app (taut {[]}) \u27ea T \u27eb\n\u27ea T \u22a0 U \u27eb = \u27ea T \u27eb \u2297 \u27ea U \u27eb\n\u27ea T \u21a6 U \u27eb = \u27ea T \u27eb \u22b8 \u27ea U \u27eb\n\u27ea T \u2227 U \u27eb = \u27ea T \u27eb \u2297\u02b3 \u27ea U \u27eb\n\u27ea T \u21d2 U \u27eb = \u27ea T \u27eb \u22b8\u02b3 \u27ea U \u27eb\n\u27ea \u25a1 T \u27eb = tvar\u2080 \u22b5 \u27ea T \u27eb\n\nT\u27ea_\u27eb : \u2200 {K} \u2192 STyp K \u2192 K\u27e6 K \u27e7\nT\u27ea T \u27eb = T\u27e6 \u27ea T \u27eb \u27e7 (World , tt)\n\n-- Signals of T are iso to \u25a1 T\n\nSignal : \u2200 {\u03b1} \u2192 RSet \u03b1 \u2192 RSet \u03b1\nSignal A s = \u2200 t \u2192 True (s \u2264 t) \u2192 A t\n\nsig : \u2200 {\u03b1} (T : STyp (rset \u03b1)) s \u2192 \n T\u27ea \u25a1 T \u27eb s \u2192 Signal T\u27ea T \u27eb s\nsig T s \u03c3 t s\u2264t = \u03c3 t s\u2264t _\n\nsig\u207b\u00b9 : \u2200 {\u03b1} (T : STyp (rset \u03b1)) s \u2192 \n Signal T\u27ea T \u27eb s \u2192 T\u27ea \u25a1 T \u27eb s\nsig\u207b\u00b9 T s \u03c3 t s\u2264t _ = \u03c3 t s\u2264t\n\nsig-iso : \u2200 {\u03b1} (T : STyp (rset \u03b1)) s \u03c3 \u2192 \n (sig T s (sig\u207b\u00b9 T s \u03c3) \u2261 \u03c3)\nsig-iso T s \u03c3 = refl\n\nsig-iso\u207b\u00b9 : \u2200 {\u03b1} (T : STyp (rset \u03b1)) s \u03c3 \u2192\n (sig\u207b\u00b9 T s (sig T s \u03c3) \u2261 \u03c3)\nsig-iso\u207b\u00b9 T s \u03c3 = refl\n\n-- Signal functions from T to U are iso to \u25a1 T \u21d2 \u25a1 U\n\nSF : \u2200 {\u03b1 \u03b2} \u2192 RSet \u03b1 \u2192 RSet \u03b2 \u2192 RSet (\u03b1 \u2294 \u03b2)\nSF A B s = Signal A s \u2192 Signal B s\n\nsf : \u2200 {\u03b1 \u03b2} (T : STyp (rset \u03b1)) (U : STyp (rset \u03b2)) s \u2192\n T\u27ea \u25a1 T \u21d2 \u25a1 U \u27eb s \u2192 SF T\u27ea T \u27eb T\u27ea U \u27eb s\nsf T U s f \u03c3 = sig U s (f (sig\u207b\u00b9 T s \u03c3))\n\nsf\u207b\u00b9 : \u2200 {\u03b1 \u03b2} (T : STyp (rset \u03b1)) (U : STyp (rset \u03b2)) s \u2192\n SF T\u27ea T \u27eb T\u27ea U \u27eb s \u2192 T\u27ea \u25a1 T \u21d2 \u25a1 U \u27eb s\nsf\u207b\u00b9 T U s f \u03c3 = sig\u207b\u00b9 U s (f (sig T s \u03c3))\n\nsf-iso : \u2200 {\u03b1 \u03b2} (T : STyp (rset \u03b1)) (U : STyp (rset \u03b2)) s f \u2192 \n (sf T U s (sf\u207b\u00b9 T U s f) \u2261 f)\nsf-iso T U s f = refl\n\nsf-iso\u207b\u00b9 : \u2200 {\u03b1 \u03b2} (T : STyp (rset \u03b1)) (U : STyp (rset \u03b2)) s f \u2192 \n (sf\u207b\u00b9 T U s (sf T U s f) \u2261 f)\nsf-iso\u207b\u00b9 T U s f = refl\n\n-- Causality\n\nmutual\n\n _at_\u22a8_\u2248[_]_ : \u2200 {\u03b1} (T : STyp (rset \u03b1)) s \u2192 T\u27ea T \u27eb s \u2192 Time \u2192 T\u27ea T \u27eb s \u2192 Set \u03b1\n \u27e8 T \u27e9 at s \u22a8 a \u2248[ u ] b = T \u22a8 a \u2248[ u ] b\n (T \u2227 U) at s \u22a8 (a , b) \u2248[ u ] (c , d) = (T at s \u22a8 a \u2248[ u ] c) \u00d7 (U at s \u22a8 b \u2248[ u ] d)\n (T \u21d2 U) at s \u22a8 f \u2248[ u ] g = \u2200 a b \u2192 (T at s \u22a8 a \u2248[ u ] b) \u2192 (U at s \u22a8 f a \u2248[ u ] g b)\n \u25a1 T at s \u22a8 \u03c3 \u2248[ u ] \u03c4 = (\u2200 t s\u2264t \u2192 True (t \u2264 u) \u2192 (T at t \u22a8 \u03c3 t s\u2264t _ \u2248[ u ] \u03c4 t s\u2264t _))\n\n _\u22a8_\u2248[_]_ : \u2200 {\u03b1} \u2192 (T : STyp (set \u03b1)) \u2192 T\u27ea T \u27eb \u2192 Time \u2192 T\u27ea T \u27eb \u2192 Set \u03b1\n [ T ] \u22a8 \u03c3 \u2248[ u ] \u03c4 = \u2200 s \u2192 True (s \u2264 u) \u2192 (T at s \u22a8 \u03c3 s _ \u2248[ u ] \u03c4 s _)\n (T \u22a0 U) \u22a8 (a , b) \u2248[ u ] (c , d) = (T \u22a8 a \u2248[ u ] c) \u00d7 (U \u22a8 b \u2248[ u ] d)\n (T \u21a6 U) \u22a8 f \u2248[ u ] g = \u2200 a b \u2192 (T \u22a8 a \u2248[ u ] b) \u2192 (U \u22a8 f a \u2248[ u ] g b)\n\nCausal : \u2200 {\u03b1 \u03b2} (T : STyp (set \u03b1)) (U : STyp (set \u03b2)) \u2192 T\u27ea T \u21a6 U \u27eb \u2192 Set (\u03b1 \u2294 \u03b2)\nCausal T U f = \u2200 u \u03c3 \u03c4 \u2192 \n (T \u22a8 \u03c3 \u2248[ u ] \u03c4) \u2192 (U \u22a8 f \u03c3 \u2248[ u ] f \u03c4)\n\n-- Parametricity implies causality\n\n\u211c[_] : Time \u2192 (rset o \u220b World \u2194 World)\n\u211c[ u ] {t} s\u2261t tt tt = True (t \u2264 u)\n\nmutual\n\n \u211c-impl-\u2248_at : \u2200 {\u03b1} (T : STyp (rset \u03b1)) s u \u2192 True (s \u2264 u) \u2192 \u2200 a b \u2192\n (T\u27e6 \u27ea T \u27eb \u27e7\u00b2 (\u211c[ u ] , tt) refl a b) \u2192 (T at s \u22a8 a \u2248[ u ] b)\n \u211c-impl-\u2248 \u27e8 T \u27e9 at s u s\u2264u a b a\u211cb\n = \u211c-impl-\u2248 T u a b a\u211cb\n \u211c-impl-\u2248 (T \u2227 U) at s u s\u2264u (a , b) (c , d) (a\u211cc , b\u211cd) \n = (\u211c-impl-\u2248 T at s u s\u2264u a c a\u211cc , \u211c-impl-\u2248 U at s u s\u2264u b d b\u211cd)\n \u211c-impl-\u2248 (T \u21d2 U) at s u s\u2264u f g f\u211cg\n = \u03bb a b a\u2248b \u2192 \u211c-impl-\u2248 U at s u s\u2264u (f a) (g b) (f\u211cg (\u2248-impl-\u211c T at s u s\u2264u a b a\u2248b))\n \u211c-impl-\u2248_at (\u25a1 T) s u s\u2264u \u03c3 \u03c4 \u03c3\u211c\u03c4 = \u03bb t s\u2264t t\u2264u \u2192 \n \u211c-impl-\u2248 T at t u t\u2264u (\u03c3 t s\u2264t _) (\u03c4 t s\u2264t _) \n (\u03c3\u211c\u03c4 refl tt (\u03bb {r} _ _ {r\u2264t} _ \u2192 \u2264-trans r t u r\u2264t t\u2264u))\n\n \u2248-impl-\u211c_at : \u2200 {\u03b1} (T : STyp (rset \u03b1)) s u \u2192 True (s \u2264 u) \u2192 \u2200 a b \u2192\n (T at s \u22a8 a \u2248[ u ] b) \u2192 (T\u27e6 \u27ea T \u27eb \u27e7\u00b2 (\u211c[ u ] , tt) refl a b)\n \u2248-impl-\u211c \u27e8 T \u27e9 at s u s\u2264u a b a\u2248b\n = \u2248-impl-\u211c T u a b a\u2248b\n \u2248-impl-\u211c (T \u2227 U) at s u s\u2264u (a , b) (c , d) (a\u2248c , b\u2248d)\n = (\u2248-impl-\u211c T at s u s\u2264u a c a\u2248c , \u2248-impl-\u211c U at s u s\u2264u b d b\u2248d)\n \u2248-impl-\u211c (T \u21d2 U) at s u s\u2264u f g f\u2248g\n = \u03bb {a} {b} a\u211cb \u2192 \u2248-impl-\u211c U at s u s\u2264u (f a) (g b) (f\u2248g a b (\u211c-impl-\u2248 T at s u s\u2264u a b a\u211cb))\n \u2248-impl-\u211c (\u25a1 T) at s u s\u2264u \u03c3 \u03c4 \u03c3\u2248\u03c4 = lemma where\n lemma : T\u27e6 \u27ea \u25a1 T \u27eb \u27e7\u00b2 (\u211c[ u ] , tt) {s} refl \u03c3 \u03c4\n lemma {t} refl {s\u2264t} {s\u2264t\u2032} tt k\u211ck\u2032 with irrel (s \u2264 t) s\u2264t s\u2264t\u2032\n lemma {t} refl {s\u2264t} tt k\u211ck\u2032 | refl\n = \u2248-impl-\u211c T at t u t\u2264u (\u03c3 t s\u2264t _) (\u03c4 t s\u2264t _) (\u03c3\u2248\u03c4 t s\u2264t t\u2264u) where\n t\u2264u = k\u211ck\u2032 {t} refl {s\u2264t} {s\u2264t} tt {\u2264-refl t} {\u2264-refl t} tt\n\n \u211c-impl-\u2248 : \u2200 {\u03b1} (T : STyp (set \u03b1)) (u : Time) (a b : T\u27ea T \u27eb) \u2192\n (T\u27e6 \u27ea T \u27eb \u27e7\u00b2 (\u211c[ u ] , tt) a b) \u2192 (T \u22a8 a \u2248[ u ] b)\n \u211c-impl-\u2248 (T \u22a0 U) u (a , b) (c , d) (a\u211cc , b\u211cd)\n = (\u211c-impl-\u2248 T u a c a\u211cc , \u211c-impl-\u2248 U u b d b\u211cd)\n \u211c-impl-\u2248 (T \u21a6 U) u f g f\u211cg\n = \u03bb a b a\u2248b \u2192 \u211c-impl-\u2248 U u (f a) (g b) (f\u211cg (\u2248-impl-\u211c T u a b a\u2248b))\n \u211c-impl-\u2248 [ T ] u \u03c3 \u03c4 \u03c3\u211c\u03c4\n = \u03bb s s\u2264u \u2192 \u211c-impl-\u2248 T at s u s\u2264u (\u03c3 s _) (\u03c4 s _) (\u03c3\u211c\u03c4 refl s\u2264u)\n\n \u2248-impl-\u211c : \u2200 {\u03b1} (T : STyp (set \u03b1)) (u : Time) (a b : T\u27ea T \u27eb) \u2192\n (T \u22a8 a \u2248[ u ] b) \u2192 (T\u27e6 \u27ea T \u27eb \u27e7\u00b2 (\u211c[ u ] , tt) a b)\n \u2248-impl-\u211c (T \u22a0 U) u (a , b) (c , d) (a\u2248c , b\u2248d)\n = (\u2248-impl-\u211c T u a c a\u2248c , \u2248-impl-\u211c U u b d b\u2248d)\n \u2248-impl-\u211c (T \u21a6 U) u f g f\u2248g\n = \u03bb {a} {b} a\u211cb \u2192 \u2248-impl-\u211c U u (f a) (g b) (f\u2248g a b (\u211c-impl-\u2248 T u a b a\u211cb))\n \u2248-impl-\u211c [ T ] u \u03c3 \u03c4 \u03c3\u2248\u03c4 = lemma where\n lemma : T\u27e6 \u27ea [ T ] \u27eb \u27e7\u00b2 (\u211c[ u ] , tt) \u03c3 \u03c4\n lemma {s} refl s\u2264u = \u2248-impl-\u211c T at s u s\u2264u (\u03c3 s _) (\u03c4 s _) (\u03c3\u2248\u03c4 s s\u2264u)\n\n-- Every F-omega function is causal\n\ncausality : \u2200 {\u03b1 \u03b2} (T : STyp (set \u03b1)) (U : STyp (set \u03b2)) (M : Exp [] \u27ea T \u21a6 U \u27eb) \u2192 \n Causal T U (M\u27e6 M \u27e7 (World , tt) tt)\ncausality T U M u\n = \u211c-impl-\u2248 (T \u21a6 U) u\n (M\u27e6 M \u27e7 (World , tt) tt) \n (M\u27e6 M \u27e7 (World , tt) tt) \n (M\u27e6 M \u27e7\u00b2 (\u211c[ u ] , _) tt)\n","old_contents":"open import FRP.JS.Level using ( Level ; _\u2294_ ) renaming ( zero to o ; suc to \u2191 )\nopen import FRP.JS.Time using ( Time ; _\u2264_ ; _<_ )\nopen import FRP.JS.Bool using ( Bool ; true ; false ; not ; _\u225f_ ) \nopen import FRP.JS.True using ( True ; tt )\n\nmodule FRP.JS.Model where\n\n-- This model is essentially System F-omega with a kind time\n-- together with a type for the partial order on time,\n-- and expressions for reflexivity and transitivity.\n-- We prove parametricity, and then show that parametricity implies causality.\n\n-- Note that this is a \"deep\" notion of causality, not the \"shallow\"\n-- causality usually used in FRP. The pragmatic upshot of this is that\n-- there is only one time model: nested signals are in the same time\n-- model, not a simulated time model. This fits with the JS implementation,\n-- which uses wall clock time for all signals.\n\n-- Propositional equality\n\ndata _\u2261_ {\u03b1} {A : Set \u03b1} (a : A) : A \u2192 Set \u03b1 where\n refl : a \u2261 a\n\nsym : \u2200 {\u03b1} {A : Set \u03b1} {a b : A} \u2192 (a \u2261 b) \u2192 (b \u2261 a)\nsym refl = refl\n\ntrans : \u2200 {\u03b1} {A : Set \u03b1} {a b c : A} \u2192 (a \u2261 b) \u2192 (b \u2261 c) \u2192 (a \u2261 c)\ntrans refl refl = refl\n\ncong : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : Set \u03b2} (f : A \u2192 B) {a\u2081 a\u2082 : A} \u2192\n (a\u2081 \u2261 a\u2082) \u2192 (f a\u2081 \u2261 f a\u2082)\ncong f refl = refl\n\napply : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : Set \u03b2} {F G : A \u2192 B} \u2192 (F \u2261 G) \u2192 \n \u2200 {a b} \u2192 (a \u2261 b) \u2192 (F a \u2261 G b)\napply refl refl = refl\n\ncast : \u2200 {\u03b1} {A B : Set \u03b1} \u2192 (A \u2261 B) \u2192 A \u2192 B\ncast refl a = a\n\ncast\u00b2 : \u2200 {\u03b1} {A B : Set \u03b1} {\u211c \u2111 : A \u2192 B \u2192 Set \u03b1} \u2192 (\u211c \u2261 \u2111) \u2192 \u2200 {a b} \u2192 \u211c a b \u2192 \u2111 a b\ncast\u00b2 refl a\u211cb = a\u211cb\n\nirrel : \u2200 b \u2192 (b\u2081 b\u2082 : True b) \u2192 (b\u2081 \u2261 b\u2082)\nirrel true tt tt = refl\nirrel false () ()\n\n-- Postulates (including dependent extensionality)\n\npostulate\n \u2264-refl : \u2200 t \u2192 True (t \u2264 t)\n \u2264-trans : \u2200 t u v \u2192 True (t \u2264 u) \u2192 True (u \u2264 v) \u2192 True (t \u2264 v)\n dext : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : A \u2192 Set \u03b2} {F G : \u2200 a \u2192 B a} \u2192 (\u2200 a \u2192 F a \u2261 G a) \u2192 (F \u2261 G)\n\next : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : Set \u03b2} {F G : A \u2192 B} \u2192\n (\u2200 a \u2192 F a \u2261 G a) \u2192 (F \u2261 G)\next = dext\n\niext : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : A \u2192 Set \u03b2} {F G : \u2200 {a} \u2192 B a} \u2192 \n (\u2200 a \u2192 F {a} \u2261 G {a}) \u2192 ((\u03bb {a} \u2192 F {a}) \u2261 (\u03bb {a} \u2192 G {a}))\niext F\u2248G = cong (\u03bb X {a} \u2192 X a) (dext F\u2248G)\n\n-- Finite products\n\nrecord \u22a4 {\u03b1} : Set \u03b1 where\n constructor tt\n\nopen \u22a4 public\n\nrecord \u03a3 {\u03b1 \u03b2} (A : Set \u03b1) (B : A \u2192 Set \u03b2) : Set (\u03b1 \u2294 \u03b2) where\n constructor _,_\n field\n proj\u2081 : A\n proj\u2082 : B proj\u2081\n\nopen \u03a3 public\n\n_\u00d7_ : \u2200 {\u03b1 \u03b2} \u2192 Set \u03b1 \u2192 Set \u03b2 \u2192 Set (\u03b1 \u2294 \u03b2)\nA \u00d7 B = \u03a3 A (\u03bb a \u2192 B)\n\n_\u00d7\u00b2_ : \u2200 {\u03b1 \u03b2} {A C : Set \u03b1} {B D : Set \u03b2} \u2192 \n (A \u2192 C \u2192 Set \u03b1) \u2192 (B \u2192 D \u2192 Set \u03b2) \u2192 ((A \u00d7 B) \u2192 (C \u00d7 D) \u2192 Set (\u03b1 \u2294 \u03b2))\n(\u211c \u00d7\u00b2 \u2111) (a , b) (c , d) = (\u211c a c \u00d7 \u2111 b d)\n\n_\u2192\u00b2_ : \u2200 {\u03b1 \u03b2} {A C : Set \u03b1} {B D : Set \u03b2} \u2192 \n (A \u2192 C \u2192 Set \u03b1) \u2192 (B \u2192 D \u2192 Set \u03b2) \u2192 ((A \u2192 B) \u2192 (C \u2192 D) \u2192 Set (\u03b1 \u2294 \u03b2))\n(\u211c \u2192\u00b2 \u2111) f g = \u2200 {a b} \u2192 \u211c a b \u2192 \u2111 (f a) (g b)\n\n-- Case on booleans\n\ndata Case (c : Bool) : Set where\n _,_ : \u2200 b \u2192 True (b \u225f c) \u2192 Case c\n\nswitch : \u2200 b \u2192 Case b\nswitch true = (true , tt)\nswitch false = (false , tt)\n\n-- Reactive sets\n\nRSet : \u2200 \u03b1 \u2192 Set (\u2191 \u03b1)\nRSet \u03b1 = Time \u2192 Set \u03b1\n\n-- Equalitional reasoning\n\ninfix 4 _IsRelatedTo_\ninfix 2 _\u220e\ninfixr 2 _\u2261\u27e8_\u27e9_\ninfix 1 begin_\n\ndata _IsRelatedTo_ {\u03b1} {A : Set \u03b1} (a b : A) : Set \u03b1 where\n relTo : (a\u2261b : a \u2261 b) \u2192 a IsRelatedTo b\n\nbegin_ : \u2200 {\u03b1} {A : Set \u03b1} {a b : A} \u2192 a IsRelatedTo b \u2192 a \u2261 b\nbegin relTo a\u2261b = a\u2261b\n\n_\u2261\u27e8_\u27e9_ : \u2200 {\u03b1} {A : Set \u03b1} a {b c : A} \u2192 a \u2261 b \u2192 b IsRelatedTo c \u2192 a IsRelatedTo c\n_ \u2261\u27e8 a\u2261b \u27e9 relTo b\u2261c = relTo (trans a\u2261b b\u2261c)\n\n_\u220e : \u2200 {\u03b1} {A : Set \u03b1} (a : A) \u2192 a IsRelatedTo a\n_\u220e _ = relTo refl\n\n-- Kinds\n\ndata Kind : Set where\n time : Kind\n set : Level \u2192 Kind\n _\u21d2_ : Kind \u2192 Kind \u2192 Kind\n\nlevel : Kind \u2192 Level\nlevel time = o\nlevel (set \u03b1) = \u2191 \u03b1\nlevel (K \u21d2 L) = level K \u2294 level L\n\nK\u27e6_\u27e7 : \u2200 K \u2192 Set (level K)\nK\u27e6 time \u27e7 = Time\nK\u27e6 set \u03b1 \u27e7 = Set \u03b1\nK\u27e6 K \u21d2 L \u27e7 = K\u27e6 K \u27e7 \u2192 K\u27e6 L \u27e7\n\n_\u220b_\u2194_ : \u2200 K \u2192 K\u27e6 K \u27e7 \u2192 K\u27e6 K \u27e7 \u2192 Set (level K)\ntime \u220b t \u2194 u = (t \u2261 u)\nset \u03b1 \u220b A \u2194 B = A \u2192 B \u2192 Set \u03b1\n(K \u21d2 L) \u220b F \u2194 G = \u2200 {A B} \u2192 (K \u220b A \u2194 B) \u2192 (L \u220b F A \u2194 G B)\n\n-- \u2261 can be used as a structural equivalence on relations.\n\nstruct : \u2200 K {A B C D} \u2192 (A \u2261 B) \u2192 (K \u220b B \u2194 D) \u2192 (C \u2261 D) \u2192 (K \u220b A \u2194 C)\nstruct K refl \u211c refl = \u211c\n\nstruct-ext : \u2200 K L {A B} {F G H I : K\u27e6 K \u21d2 L \u27e7} \n (F\u2248G : \u2200 A \u2192 F A \u2261 G A) (\u211c : (K \u21d2 L) \u220b G \u2194 I) (H\u2248I : \u2200 B \u2192 H B \u2261 I B) (\u2111 : K \u220b A \u2194 B) \u2192\n struct L (F\u2248G A) (\u211c \u2111) (H\u2248I B) \u2261 struct (K \u21d2 L) (ext F\u2248G) \u211c (ext H\u2248I) \u2111\nstruct-ext K L {A} {B} F\u2248G \u211c H\u2248I \u2111 \n with ext F\u2248G | ext H\u2248I | F\u2248G A | H\u2248I B\n... | refl | refl | refl | refl = refl\n\nstruct-apply : \u2200 K L {F G H I A B C D} \u2192 \n (F\u2261G : F \u2261 G) (\u211c : (K \u21d2 L) \u220b G \u2194 I) (H\u2261I : H \u2261 I) \u2192 \n (A\u2261B : A \u2261 B) (\u2111 : K \u220b B \u2194 D) (C\u2261D : C \u2261 D) \u2192 \n struct (K \u21d2 L) F\u2261G \u211c H\u2261I (struct K A\u2261B \u2111 C\u2261D)\n \u2261 struct L (apply F\u2261G A\u2261B) (\u211c \u2111) (apply H\u2261I C\u2261D)\nstruct-apply K L refl \u211c refl refl \u2111 refl = refl\n\nstruct-cast : \u2200 {\u03b1 A B C D} (\u211c : set \u03b1 \u220b B \u2194 D) (A\u2261B : A \u2261 B) (C\u2261D : C \u2261 D) {a c} \u2192\n struct (set \u03b1) A\u2261B \u211c C\u2261D a c \u2192 \u211c (cast A\u2261B a) (cast C\u2261D c)\nstruct-cast \u211c refl refl a\u211cc = a\u211cc\n\nstruct-sym : \u2200 K {A B C D \u2111 \u211c} \u2192 (A\u2261B : A \u2261 B) \u2192 (C\u2261D : C \u2261 D) \u2192\n (\u2111 \u2261 struct K A\u2261B \u211c C\u2261D) \u2192 \n (\u211c \u2261 struct K (sym A\u2261B) \u2111 (sym C\u2261D))\nstruct-sym K refl refl refl = refl\n\nstruct-trans : \u2200 K {A B C D E F}\n (A\u2261B : A \u2261 B) (B\u2261C : B \u2261 C) (\u211c : K \u220b C \u2194 F) (E\u2261F : E \u2261 F) (D\u2261E : D \u2261 E) \u2192\n struct K A\u2261B (struct K B\u2261C \u211c E\u2261F) D\u2261E \u2261\n struct K (trans A\u2261B B\u2261C) \u211c (trans D\u2261E E\u2261F)\nstruct-trans K refl refl \u211c refl refl = refl\n\n-- Type contexts\n\ninfixr 4 _\u2237_\n\ndata Kinds : Set where\n [] : Kinds\n _\u2237_ : Kind \u2192 Kinds \u2192 Kinds\n\nlevels : Kinds \u2192 Level\nlevels [] = o\nlevels (K \u2237 \u03a3) = level K \u2294 levels \u03a3\n\n\u03a3\u27e6_\u27e7 : \u2200 \u03a3 \u2192 Set (levels \u03a3)\n\u03a3\u27e6 [] \u27e7 = \u22a4\n\u03a3\u27e6 K \u2237 \u03a3 \u27e7 = K\u27e6 K \u27e7 \u00d7 \u03a3\u27e6 \u03a3 \u27e7\n\n_\u220b_\u2194*_ : \u2200 \u03a3 \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 Set (levels \u03a3)\n[] \u220b tt \u2194* tt = \u22a4\n(K \u2237 \u03a3) \u220b (A , As) \u2194* (B , Bs) = (K \u220b A \u2194 B) \u00d7 (\u03a3 \u220b As \u2194* Bs)\n\n-- Inclusion order on type contexts.\n-- Credited by Randy Pollack to Geuvers and Nederhof, JAR 1991.\n-- http:\/\/thread.gmane.org\/gmane.comp.lang.agda\/3259\/focus=3267\n\ndata _\u2291_ : Kinds \u2192 Kinds \u2192 Set where\n id : \u2200 {\u03a3} \u2192 \u03a3 \u2291 \u03a3\n keep : \u2200 K {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 ((K \u2237 \u03a3) \u2291 (K \u2237 \u03a5))\n skip : \u2200 K {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 (\u03a3 \u2291 (K \u2237 \u03a5))\n\n\u2291\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 \u03a3\u27e6 \u03a5 \u27e7 \u2192 \u03a3\u27e6 \u03a3 \u27e7\n\u2291\u27e6 id \u27e7 As = As\n\u2291\u27e6 keep K \u03a3\u2291\u03a5 \u27e7 (A , As) = (A , \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As)\n\u2291\u27e6 skip K \u03a3\u2291\u03a5 \u27e7 (A , As) = \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As\n\n\u2291\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5} \u2192 (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) \u2192 \u2200 {As Bs} \u2192 (\u03a5 \u220b As \u2194* Bs) \u2192 (\u03a3 \u220b \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As \u2194* \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 Bs)\n\u2291\u27e6 id \u27e7\u00b2 \u211cs = \u211cs\n\u2291\u27e6 keep K \u03a3\u2291\u03a5 \u27e7\u00b2 (\u211c , \u211cs) = (\u211c , \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)\n\u2291\u27e6 skip K \u03a3\u2291\u03a5 \u27e7\u00b2 (\u211c , \u211cs) = \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs\n\n-- Concatenation of type contexts\n\n_++_ : Kinds \u2192 Kinds \u2192 Kinds\n[] ++ \u03a5 = \u03a5\n(K \u2237 \u03a3) ++ \u03a5 = K \u2237 (\u03a3 ++ \u03a5)\n\n_\u220b_++_\u220b_ : \u2200 \u03a3 \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 \u2200 \u03a5 \u2192 \u03a3\u27e6 \u03a5 \u27e7 \u2192 \u03a3\u27e6 \u03a3 ++ \u03a5 \u27e7\n[] \u220b tt ++ \u03a5 \u220b Bs = Bs\n(K \u2237 \u03a3) \u220b (A , As) ++ \u03a5 \u220b Bs = (A , (\u03a3 \u220b As ++ \u03a5 \u220b Bs))\n\n_\u220b_++\u00b2_\u220b_ : \u2200 \u03a3 {As Bs} \u2192 (\u03a3 \u220b As \u2194* Bs) \u2192 \u2200 \u03a5 {Cs Ds} \u2192 (\u03a5 \u220b Cs \u2194* Ds) \u2192 \n ((\u03a3 ++ \u03a5) \u220b (\u03a3 \u220b As ++ \u03a5 \u220b Cs) \u2194* (\u03a3 \u220b Bs ++ \u03a5 \u220b Ds))\n[] \u220b tt ++\u00b2 \u03a5 \u220b \u2111s = \u2111s\n(K \u2237 \u03a3) \u220b (\u211c , \u211cs) ++\u00b2 \u03a5 \u220b \u2111s = (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n\n-- Type variables\n\ndata TVar (K : Kind) : Kinds \u2192 Set where\n zero : \u2200 {\u03a3} \u2192 TVar K (K \u2237 \u03a3)\n suc : \u2200 {L \u03a3} \u2192 TVar K \u03a3 \u2192 TVar K (L \u2237 \u03a3)\n\n\u03c4\u27e6_\u27e7 : \u2200 {\u03a3 K} (\u03c4 : TVar K \u03a3) \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 K\u27e6 K \u27e7\n\u03c4\u27e6 zero \u27e7 (A , As) = A\n\u03c4\u27e6 suc \u03c4 \u27e7 (A , As) = \u03c4\u27e6 \u03c4 \u27e7 As\n\n\u03c4\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 K} (\u03c4 : TVar K \u03a3) {As Bs} \u2192 (\u03a3 \u220b As \u2194* Bs) \u2192 (K \u220b \u03c4\u27e6 \u03c4 \u27e7 As \u2194 \u03c4\u27e6 \u03c4 \u27e7 Bs)\n\u03c4\u27e6 zero \u27e7\u00b2 (\u211c , \u211cs) = \u211c\n\u03c4\u27e6 suc \u03c4 \u27e7\u00b2 (\u211c , \u211cs) = \u03c4\u27e6 \u03c4 \u27e7\u00b2 \u211cs\n\n-- Type constants\n\ndata TConst : Kind \u2192 Set where\n prod fun : \u2200 {\u03b1 \u03b2} \u2192 TConst (set \u03b1 \u21d2 (set \u03b2 \u21d2 set (\u03b1 \u2294 \u03b2)))\n leq lt : TConst (time \u21d2 (time \u21d2 set o))\n univ : \u2200 K {\u03b1} \u2192 TConst ((K \u21d2 set \u03b1) \u21d2 set (level K \u2294 \u03b1))\n\nC\u27e6_\u27e7 : \u2200 {K} \u2192 (TConst K) \u2192 K\u27e6 K \u27e7\nC\u27e6 prod \u27e7 = \u03bb A B \u2192 (A \u00d7 B)\nC\u27e6 fun \u27e7 = \u03bb A B \u2192 (A \u2192 B)\nC\u27e6 leq \u27e7 = \u03bb t u \u2192 True (t \u2264 u)\nC\u27e6 lt \u27e7 = \u03bb t u \u2192 True (t < u)\nC\u27e6 univ K \u27e7 = \u03bb F \u2192 \u2200 A \u2192 F A\n\nC\u27e6_\u27e7\u00b2 : \u2200 {K} (C : TConst K) \u2192 (K \u220b C\u27e6 C \u27e7 \u2194 C\u27e6 C \u27e7)\nC\u27e6 prod \u27e7\u00b2 = \u03bb \u211c \u2111 \u2192 (\u211c \u00d7\u00b2 \u2111)\nC\u27e6 fun \u27e7\u00b2 = \u03bb \u211c \u2111 \u2192 (\u211c \u2192\u00b2 \u2111)\nC\u27e6 leq \u27e7\u00b2 = \u03bb _ _ _ _ \u2192 \u22a4\nC\u27e6 lt \u27e7\u00b2 = \u03bb _ _ _ _ \u2192 \u22a4\nC\u27e6 univ K \u27e7\u00b2 = \u03bb \u211c f g \u2192 \u2200 {a b} \u2111 \u2192 \u211c \u2111 (f a) (g b)\n\n-- Types\n\ndata Typ (\u03a3 : Kinds) : Kind \u2192 Set where\n const : \u2200 {K} \u2192 TConst K \u2192 Typ \u03a3 K\n abs : \u2200 K {L} \u2192 Typ (K \u2237 \u03a3) L \u2192 Typ \u03a3 (K \u21d2 L)\n app : \u2200 {K L} \u2192 Typ \u03a3 (K \u21d2 L) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L\n var : \u2200 {K} \u2192 TVar K \u03a3 \u2192 Typ \u03a3 K\n\ntlevel : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (set \u03b1) \u2192 Level\ntlevel {\u03a3} {\u03b1} T = \u03b1\n\nT\u27e6_\u27e7 : \u2200 {\u03a3 K} (T : Typ \u03a3 K) \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 K\u27e6 K \u27e7\nT\u27e6 const C \u27e7 As = C\u27e6 C \u27e7\nT\u27e6 abs K T \u27e7 As = \u03bb A \u2192 T\u27e6 T \u27e7 (A , As)\nT\u27e6 app T U \u27e7 As = T\u27e6 T \u27e7 As (T\u27e6 U \u27e7 As)\nT\u27e6 var \u03c4 \u27e7 As = \u03c4\u27e6 \u03c4 \u27e7 As\n\nT\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 K} (T : Typ \u03a3 K) {As Bs} \u2192 (\u03a3 \u220b As \u2194* Bs) \u2192 (K \u220b T\u27e6 T \u27e7 As \u2194 T\u27e6 T \u27e7 Bs)\nT\u27e6 const C \u27e7\u00b2 \u211cs = C\u27e6 C \u27e7\u00b2\nT\u27e6 abs K T \u27e7\u00b2 \u211cs = \u03bb \u211c \u2192 T\u27e6 T \u27e7\u00b2 (\u211c , \u211cs)\nT\u27e6 app T U \u27e7\u00b2 \u211cs = T\u27e6 T \u27e7\u00b2 \u211cs (T\u27e6 U \u27e7\u00b2 \u211cs)\nT\u27e6 var \u03c4 \u27e7\u00b2 \u211cs = \u03c4\u27e6 \u03c4 \u27e7\u00b2 \u211cs\n\n-- Type shorthands\n\napp\u2082 : \u2200 {\u03a3 K L M} \u2192 Typ \u03a3 (K \u21d2 (L \u21d2 M)) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L \u2192 Typ \u03a3 M\napp\u2082 T U V = app (app T U) V\n\ncapp : \u2200 {\u03a3 K L} \u2192 TConst (K \u21d2 L) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L\ncapp C = app (const C)\n\ncapp\u2082 : \u2200 {\u03a3 K L M} \u2192 TConst (K \u21d2 (L \u21d2 M)) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L \u2192 Typ \u03a3 M\ncapp\u2082 C = app\u2082 (const C)\n\n_\u2297_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (set \u03b1) \u2192 Typ \u03a3 (set \u03b2) \u2192 Typ \u03a3 (set (\u03b1 \u2294 \u03b2))\n_\u2297_ = capp\u2082 prod\n\n_\u22b8_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (set \u03b1) \u2192 Typ \u03a3 (set \u03b2) \u2192 Typ \u03a3 (set (\u03b1 \u2294 \u03b2))\n_\u22b8_ = capp\u2082 fun\n\n_\u227c_ : \u2200 {\u03a3} \u2192 Typ \u03a3 time \u2192 Typ \u03a3 time \u2192 Typ \u03a3 (set o)\n_\u227c_ = capp\u2082 leq\n\n_\u227a_ : \u2200 {\u03a3} \u2192 Typ \u03a3 time \u2192 Typ \u03a3 time \u2192 Typ \u03a3 (set o)\n_\u227a_ = capp\u2082 lt\n\n\u03a0 : \u2200 {\u03a3 \u03b1} K \u2192 Typ (K \u2237 \u03a3) (set \u03b1) \u2192 Typ \u03a3 (set (level K \u2294 \u03b1))\n\u03a0 K T = capp (univ K) (abs K T)\n\ntvar\u2080 : \u2200 {\u03a3 K} \u2192 Typ (K \u2237 \u03a3) K\ntvar\u2080 = var zero\n\ntvar\u2081 : \u2200 {\u03a3 K L} \u2192 Typ (L \u2237 K \u2237 \u03a3) K\ntvar\u2081 = var (suc zero)\n\ntvar\u2082 : \u2200 {\u03a3 K L M} \u2192 Typ (M \u2237 L \u2237 K \u2237 \u03a3) K\ntvar\u2082 = var (suc (suc zero))\n\ntvar\u2083 : \u2200 {\u03a3 K L M N} \u2192 Typ (N \u2237 M \u2237 L \u2237 K \u2237 \u03a3) K\ntvar\u2083 = var (suc (suc (suc zero)))\n\nrset : Level \u2192 Kind\nrset \u03b1 = time \u21d2 set \u03b1\n\nrset\u2080 : Kind\nrset\u2080 = rset o\n\nprod\u02b3 : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 (rset \u03b2 \u21d2 rset (\u03b1 \u2294 \u03b2)))\nprod\u02b3 {\u03a3} {\u03b1} {\u03b2} = abs (rset \u03b1) (abs (rset \u03b2) (abs time (app tvar\u2082 tvar\u2080 \u2297 app tvar\u2081 tvar\u2080)))\n\n_\u2297\u02b3_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1) \u2192 Typ \u03a3 (rset \u03b2) \u2192 Typ \u03a3 (rset (\u03b1 \u2294 \u03b2))\n_\u2297\u02b3_ = app\u2082 prod\u02b3\n\nfun\u02b3 : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 (rset \u03b2 \u21d2 rset (\u03b1 \u2294 \u03b2)))\nfun\u02b3 {\u03a3} {\u03b1} {\u03b2} = abs (rset \u03b1) (abs (rset \u03b2) (abs time (app tvar\u2082 tvar\u2080 \u22b8 app tvar\u2081 tvar\u2080)))\n\n_\u22b8\u02b3_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1) \u2192 Typ \u03a3 (rset \u03b2) \u2192 Typ \u03a3 (rset (\u03b1 \u2294 \u03b2))\n_\u22b8\u02b3_ = app\u2082 fun\u02b3\n\nalways : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (set \u03b1 \u21d2 rset \u03b1)\nalways {\u03a3} {\u03b1} = abs (set \u03b1) (abs time tvar\u2081)\n\n\u00bdinterval : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 (time \u21d2 (time \u21d2 set \u03b1)))\n\u00bdinterval {\u03a3} {\u03b1} = abs (rset \u03b1) (abs time (abs time (\u03a0 time \n ((tvar\u2082 \u227a tvar\u2080) \u22b8 ((tvar\u2080 \u227c tvar\u2081) \u22b8 app tvar\u2083 tvar\u2080)))))\n\n_\u27e8_,_] : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (rset \u03b1) \u2192 Typ \u03a3 time \u2192 Typ \u03a3 time \u2192 Typ \u03a3 (set \u03b1)\nT \u27e8 t , u ] = app (app (app \u00bdinterval T) t) u\n\n-- Contexts\n\ndata Typs (\u03a3 : Kinds) : Set where\n [] : Typs \u03a3\n _\u2237_ : \u2200 {\u03b1} \u2192 (Typ \u03a3 (set \u03b1)) \u2192 Typs \u03a3 \u2192 Typs \u03a3\n\ntlevels : \u2200 {\u03a3} \u2192 Typs \u03a3 \u2192 Level\ntlevels [] = o\ntlevels (T \u2237 \u0393) = tlevel T \u2294 tlevels \u0393\n\n\u0393\u27e6_\u27e7 : \u2200 {\u03a3} (\u0393 : Typs \u03a3) \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 Set (tlevels \u0393)\n\u0393\u27e6 [] \u27e7 As = \u22a4\n\u0393\u27e6 T \u2237 \u0393 \u27e7 As = T\u27e6 T \u27e7 As \u00d7 \u0393\u27e6 \u0393 \u27e7 As\n\n\u0393\u27e6_\u27e7\u00b2 : \u2200 {\u03a3} (\u0393 : Typs \u03a3) {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 (\u0393\u27e6 \u0393 \u27e7 As \u2192 \u0393\u27e6 \u0393 \u27e7 Bs \u2192 Set (tlevels \u0393))\n\u0393\u27e6 [] \u27e7\u00b2 \u211cs tt tt = \u22a4\n\u0393\u27e6 T \u2237 \u0393 \u27e7\u00b2 \u211cs (a , as) (b , bs) = T\u27e6 T \u27e7\u00b2 \u211cs a b \u00d7 \u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs as bs\n\n-- Weakening of type variables\n\n\u03c4weaken : \u2200 {\u03a3 \u03a5 K} \u2192 (\u03a3 \u2291 \u03a5) \u2192 TVar K \u03a3 \u2192 TVar K \u03a5\n\u03c4weaken id x = x\n\u03c4weaken (keep K \u03a3\u2291\u03a5) zero = zero\n\u03c4weaken (keep K \u03a3\u2291\u03a5) (suc x) = suc (\u03c4weaken \u03a3\u2291\u03a5 x)\n\u03c4weaken (skip K \u03a3\u2291\u03a5) x = suc (\u03c4weaken \u03a3\u2291\u03a5 x)\n\n\u03c4weaken\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5 K} (\u03c4 : TVar K \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) (As : \u03a3\u27e6 \u03a5 \u27e7) \u2192 \n \u03c4\u27e6 \u03c4 \u27e7 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As) \u2261 \u03c4\u27e6 \u03c4weaken \u03a3\u2291\u03a5 \u03c4 \u27e7 As\n\u03c4weaken\u27e6 \u03c4 \u27e7 id As = refl\n\u03c4weaken\u27e6 zero \u27e7 (keep K \u03a3\u2291\u03a5) (A , As) = refl\n\u03c4weaken\u27e6 suc \u03c4 \u27e7 (keep K \u03a3\u2291\u03a5) (A , As) = \u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As\n\u03c4weaken\u27e6 \u03c4 \u27e7 (skip K \u03a3\u2291\u03a5) (A , As) = \u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As\n\n\u03c4weaken\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5 K} (\u03c4 : TVar K \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) {As Bs} (\u211cs : \u03a5 \u220b As \u2194* Bs) \u2192 \n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) \u2261 \n struct K (\u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As) (\u03c4\u27e6 \u03c4weaken \u03a3\u2291\u03a5 \u03c4 \u27e7\u00b2 \u211cs) (\u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 Bs)\n\u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 id \u211cs = refl\n\u03c4weaken\u27e6 zero \u27e7\u00b2 (keep K \u03a3\u2291\u03a5) (\u211c , \u211cs) = refl\n\u03c4weaken\u27e6 suc \u03c4 \u27e7\u00b2 (keep K \u03a3\u2291\u03a5) (\u211c , \u211cs) = \u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs\n\u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 (skip K \u03a3\u2291\u03a5) (\u211c , \u211cs) = \u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs\n\n-- Weakening of types\n\nweaken : \u2200 {\u03a3 \u03a5 K} \u2192 (\u03a3 \u2291 \u03a5) \u2192 Typ \u03a3 K \u2192 Typ \u03a5 K\nweaken \u03a3\u2291\u03a5 (const C) = const C\nweaken \u03a3\u2291\u03a5 (abs K T) = abs K (weaken (keep K \u03a3\u2291\u03a5) T)\nweaken \u03a3\u2291\u03a5 (app T U) = app (weaken \u03a3\u2291\u03a5 T) (weaken \u03a3\u2291\u03a5 U)\nweaken \u03a3\u2291\u03a5 (var \u03c4) = var (\u03c4weaken \u03a3\u2291\u03a5 \u03c4)\n\nweaken\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5 K} (T : Typ \u03a3 K) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) (As : \u03a3\u27e6 \u03a5 \u27e7) \u2192 \n T\u27e6 T \u27e7 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As) \u2261 T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7 As\nweaken\u27e6 const C \u27e7 \u03a3\u2291\u03a5 As = refl\nweaken\u27e6 abs K T \u27e7 \u03a3\u2291\u03a5 As = ext (\u03bb A \u2192 weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (A , As))\nweaken\u27e6 app T U \u27e7 \u03a3\u2291\u03a5 As = apply (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) \nweaken\u27e6 var \u03c4 \u27e7 \u03a3\u2291\u03a5 As = \u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As\n \nweaken\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5 K} (T : Typ \u03a3 K) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) {As Bs} (\u211cs : \u03a5 \u220b As \u2194* Bs) \u2192 \n T\u27e6 T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) \u2261 struct K (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 Bs)\nweaken\u27e6 const C \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs = refl\nweaken\u27e6 abs K {L} T \u27e7\u00b2 \u03a3\u2291\u03a5 {As} {Bs} \u211cs =\n iext (\u03bb A \u2192 iext (\u03bb B \u2192 ext (\u03bb \u211c \u2192 begin\n T\u27e6 abs K T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) \u211c\n \u2261\u27e8 weaken\u27e6 T \u27e7\u00b2 (keep K \u03a3\u2291\u03a5) (\u211c , \u211cs) \u27e9\n struct L \n (weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (A , As)) \n (T\u27e6 weaken (keep K \u03a3\u2291\u03a5) T \u27e7\u00b2 (\u211c , \u211cs)) \n (weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (B , Bs))\n \u2261\u27e8 struct-ext K L \n (\u03bb A \u2192 weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (A , As)) \n (\u03bb \u211c \u2192 T\u27e6 weaken (keep K \u03a3\u2291\u03a5) T \u27e7\u00b2 (\u211c , \u211cs)) \n (\u03bb B \u2192 weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (B , Bs)) \u211c \u27e9\n struct (K \u21d2 L) \n (weaken\u27e6 abs K T \u27e7 \u03a3\u2291\u03a5 As)\n (T\u27e6 weaken \u03a3\u2291\u03a5 (abs K T) \u27e7\u00b2 \u211cs) \n (weaken\u27e6 abs K T \u27e7 \u03a3\u2291\u03a5 Bs) \u211c\n \u220e)))\nweaken\u27e6 app {K} {L} T U \u27e7\u00b2 \u03a3\u2291\u03a5 {As} {Bs} \u211cs = \n begin\n T\u27e6 app T U \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)\n \u2261\u27e8 cong (T\u27e6 T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)) (weaken\u27e6 U \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs) \u27e9\n T\u27e6 T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)\n (struct K (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs))\n \u2261\u27e8 cong (\u03bb X \u2192 X (struct K (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs)))\n (weaken\u27e6 T \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs) \u27e9\n (struct (K \u21d2 L) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 Bs))\n (struct K (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs))\n \u2261\u27e8 struct-apply K L \n (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 Bs) \n (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs) \u27e9\n struct L\n (weaken\u27e6 app T U \u27e7 \u03a3\u2291\u03a5 As)\n (T\u27e6 weaken \u03a3\u2291\u03a5 (app T U) \u27e7\u00b2 \u211cs) \n (weaken\u27e6 app T U \u27e7 \u03a3\u2291\u03a5 Bs)\n \u220e\nweaken\u27e6 var \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs = \u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs\n\n-- Weakening on type contexts\n\nweakens : \u2200 {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 Typs \u03a3 \u2192 Typs \u03a5\nweakens \u03a3\u2291\u03a5 [] = []\nweakens \u03a3\u2291\u03a5 (T \u2237 \u0393) = weaken \u03a3\u2291\u03a5 T \u2237 weakens \u03a3\u2291\u03a5 \u0393\n\nweakens\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5} (\u0393 : Typs \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) (As : \u03a3\u27e6 \u03a5 \u27e7) \u2192 \n \u0393\u27e6 \u0393 \u27e7 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As) \u2192 \u0393\u27e6 weakens \u03a3\u2291\u03a5 \u0393 \u27e7 As\nweakens\u27e6 [] \u27e7 \u03a3\u2291\u03a5 As tt = tt\nweakens\u27e6 T \u2237 \u0393 \u27e7 \u03a3\u2291\u03a5 As (B , Bs) = (cast (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) B , weakens\u27e6 \u0393 \u27e7 \u03a3\u2291\u03a5 As Bs)\n\nweakens\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5} (\u0393 : Typs \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) {As Bs} (\u211cs : \u03a5 \u220b As \u2194* Bs) {as bs} \u2192 \n \u0393\u27e6 \u0393 \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) as bs \u2192 \n \u0393\u27e6 weakens \u03a3\u2291\u03a5 \u0393 \u27e7\u00b2 \u211cs (weakens\u27e6 \u0393 \u27e7 \u03a3\u2291\u03a5 As as) (weakens\u27e6 \u0393 \u27e7 \u03a3\u2291\u03a5 Bs bs)\nweakens\u27e6 [] \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs tt\n = tt\nweakens\u27e6 T \u2237 \u0393 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs (a\u211cb , as\u211cbs) \n = ( struct-cast (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) \n (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 _) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 _) (cast\u00b2 (weaken\u27e6 T \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs) a\u211cb)\n , weakens\u27e6 \u0393 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs as\u211cbs)\n\n-- Susbtitution on type variables under a context\n\n\u03c4substn+ : \u2200 \u03a3 {\u03a5 K L} \u2192 TVar K (\u03a3 ++ (L \u2237 \u03a5)) \u2192 Typ \u03a5 L \u2192 Typ (\u03a3 ++ \u03a5) K\n\u03c4substn+ [] zero U = U\n\u03c4substn+ [] (suc \u03c4) U = var \u03c4\n\u03c4substn+ (K \u2237 \u03a3) zero U = var zero\n\u03c4substn+ (K \u2237 \u03a3) (suc \u03c4) U = weaken (skip K id) (\u03c4substn+ \u03a3 \u03c4 U)\n\n\u03c4substn+_\u27e6_\u27e7\u27e6_\u27e7 : \u2200 \u03a3 {\u03a5 K L} (\u03c4 : TVar K (\u03a3 ++ (L \u2237 \u03a5))) (U : Typ \u03a5 L) \n (As : \u03a3\u27e6 \u03a3 \u27e7) (Bs : \u03a3\u27e6 \u03a5 \u27e7) \u2192\n \u03c4\u27e6 \u03c4 \u27e7 (\u03a3 \u220b As ++ (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7 Bs , Bs)) \u2261 \n T\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (\u03a3 \u220b As ++ \u03a5 \u220b Bs)\n\u03c4substn+ [] \u27e6 zero \u27e7\u27e6 U \u27e7 tt Bs = refl\n\u03c4substn+ [] \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7 tt Bs = refl\n\u03c4substn+ (K \u2237 \u03a3) \u27e6 zero \u27e7\u27e6 U \u27e7 (A , As) Bs = refl\n\u03c4substn+ (K \u2237 \u03a3) \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7 (A , As) Bs = trans \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Bs) \n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip K id) (A , (\u03a3 \u220b As ++ _ \u220b Bs)))\n\n\u03c4substn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 : \u2200 \u03a3 {\u03a5 L K} (\u03c4 : TVar K (\u03a3 ++ (L \u2237 \u03a5))) (U : Typ \u03a5 L) {As Bs Cs Ds} \n (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 (\u2111s : \u03a5 \u220b Cs \u2194* Ds) \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s)) \u2261 \n struct K \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s) )\n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds)\n\u03c4substn+ [] \u27e6 zero \u27e7\u27e6 U \u27e7\u00b2 tt \u2111s = refl\n\u03c4substn+ [] \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7\u00b2 tt \u2111s = refl\n\u03c4substn+ (J \u2237 \u03a3) \u27e6 zero \u27e7\u27e6 U \u27e7\u00b2 (\u211c , \u211cs) \u2111s = refl\n\u03c4substn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 (J \u2237 \u03a3) {\u03a5} {L} {K} (suc \u03c4) U {A , As} {B , Bs} {Cs} {Ds} (\u211c , \u211cs) \u2111s = \n begin\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s))\n \u2261\u27e8 \u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s \u27e9\n struct K \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds)\n \u2261\u27e8 cong (\u03bb X \u2192 struct K (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs) X (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds)) \n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7\u00b2 (skip J id) (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))) \u27e9\n struct K \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs) \n (struct K\n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip J id) (A , (\u03a3 \u220b As ++ \u03a5 \u220b Cs))) \n (T\u27e6 weaken (skip J id) (\u03c4substn+ \u03a3 \u03c4 U) \u27e7\u00b2 (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))) \n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip J id) (B , (\u03a3 \u220b Bs ++ \u03a5 \u220b Ds)))) \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds)\n \u2261\u27e8 struct-trans K \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs) \n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip J id) (A , (\u03a3 \u220b As ++ \u03a5 \u220b Cs)))\n (T\u27e6 weaken (skip J id) (\u03c4substn+ \u03a3 \u03c4 U) \u27e7\u00b2 (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s)))\n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip J id) (B , (\u03a3 \u220b Bs ++ \u03a5 \u220b Ds)))\n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds) \u27e9\n struct K \n (\u03c4substn+ (J \u2237 \u03a3) \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7 (A , As) Cs) \n (T\u27e6 \u03c4substn+ (J \u2237 \u03a3) (suc \u03c4) U \u27e7\u00b2 (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s)) )\n (\u03c4substn+ (J \u2237 \u03a3) \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7 (B , Bs) Ds) \n \u220e\n\n-- Substitution on types under a context\n\nsubstn+ : \u2200 \u03a3 {\u03a5 K L} \u2192 Typ (\u03a3 ++ (L \u2237 \u03a5)) K \u2192 Typ \u03a5 L \u2192 Typ (\u03a3 ++ \u03a5) K\nsubstn+ \u03a3 (const C) U = const C\nsubstn+ \u03a3 (abs K T) U = abs K (substn+ (K \u2237 \u03a3) T U)\nsubstn+ \u03a3 (app S T) U = app (substn+ \u03a3 S U) (substn+ \u03a3 T U)\nsubstn+ \u03a3 (var \u03c4) U = \u03c4substn+ \u03a3 \u03c4 U\n\nsubstn+_\u27e6_\u27e7\u27e6_\u27e7 : \u2200 \u03a3 {\u03a5 K L} (T : Typ (\u03a3 ++ (L \u2237 \u03a5)) K) (U : Typ \u03a5 L) \n (As : \u03a3\u27e6 \u03a3 \u27e7) (Bs : \u03a3\u27e6 \u03a5 \u27e7) \u2192\n T\u27e6 T \u27e7 (\u03a3 \u220b As ++ (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7 Bs , Bs)) \u2261 \n T\u27e6 substn+ \u03a3 T U \u27e7 (\u03a3 \u220b As ++ \u03a5 \u220b Bs)\nsubstn+ \u03a3 \u27e6 const C \u27e7\u27e6 U \u27e7 As Bs = refl\nsubstn+ \u03a3 \u27e6 abs K T \u27e7\u27e6 U \u27e7 As Bs = ext (\u03bb A \u2192 substn+ K \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (A , As) Bs)\nsubstn+ \u03a3 \u27e6 app S T \u27e7\u27e6 U \u27e7 As Bs = apply (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 As Bs) (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Bs)\nsubstn+ \u03a3 \u27e6 var \u03c4 \u27e7\u27e6 U \u27e7 As Bs = \u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Bs\n\nsubstn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 : \u2200 \u03a3 {\u03a5 L K} (T : Typ (\u03a3 ++ (L \u2237 \u03a5)) K) (U : Typ \u03a5 L) {As Bs Cs Ds} \n (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 (\u2111s : \u03a5 \u220b Cs \u2194* Ds) \u2192\n T\u27e6 T \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s)) \u2261 \n struct K \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s) )\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds)\nsubstn+ \u03a3 \u27e6 const C \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s = refl\nsubstn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 \u03a3 {\u03a5} {L} (abs J {K} T) U {As} {Bs} {Cs} {Ds} \u211cs \u2111s = \n iext (\u03bb A \u2192 iext (\u03bb B \u2192 ext (\u03bb \u211c \u2192 begin\n T\u27e6 abs J T \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s)) \u211c\n \u2261\u27e8 substn+ (J \u2237 \u03a3) \u27e6 T \u27e7\u27e6 U \u27e7\u00b2 (\u211c , \u211cs) \u2111s \u27e9\n struct K \n (substn+ J \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (A , As) Cs) \n (T\u27e6 substn+ (J \u2237 \u03a3) T U \u27e7\u00b2 ((J \u2237 \u03a3) \u220b (\u211c , \u211cs) ++\u00b2 \u03a5 \u220b \u2111s)) \n (substn+ J \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (B , Bs) Ds)\n \u2261\u27e8 struct-ext J K \n (\u03bb A \u2192 substn+ J \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (A , As) Cs) \n (\u03bb \u211c \u2192 T\u27e6 substn+ (J \u2237 \u03a3) T U \u27e7\u00b2 ((J \u2237 \u03a3) \u220b \u211c , \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (\u03bb B \u2192 substn+ J \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (B , Bs) Ds) \u211c \u27e9\n struct (J \u21d2 K) \n (substn+ \u03a3 \u27e6 abs J T \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 substn+ \u03a3 (abs J T) U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s)) \n (substn+ \u03a3 \u27e6 abs J T \u27e7\u27e6 U \u27e7 Bs Ds) \u211c\n \u220e)))\nsubstn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 \u03a3 {\u03a5} {L} (app {J} {K} S T) U {As} {Bs} {Cs} {Ds} \u211cs \u2111s = \n begin\n T\u27e6 app S T \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 L \u2237 \u03a5 \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s))\n \u2261\u27e8 cong (T\u27e6 S \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 L \u2237 \u03a5 \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s))) (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s) \u27e9\n T\u27e6 S \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 L \u2237 \u03a5 \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s))\n (struct J \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds))\n \u2261\u27e8 cong (\u03bb X \u2192 X (struct J \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds))) \n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s) \u27e9\n struct (J \u21d2 K) \n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 substn+ \u03a3 S U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 Bs Ds) \n (struct J \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds))\n \u2261\u27e8 struct-apply J K \n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 substn+ \u03a3 S U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s)) \n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 Bs Ds) \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds) \u27e9\n struct K \n (substn+ \u03a3 \u27e6 app S T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 (app S T) U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 app S T \u27e7\u27e6 U \u27e7 Bs Ds)\n \u220e\nsubstn+ \u03a3 \u27e6 var \u03c4 \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s = \u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s\n\n-- Substitution on types\n\nsubstn : \u2200 {\u03a3 K L} \u2192 Typ (L \u2237 \u03a3) K \u2192 Typ \u03a3 L \u2192 Typ \u03a3 K\nsubstn = substn+ []\n\nsubstn\u27e6_\u27e7\u27e6_\u27e7 : \u2200 {\u03a3 K L} (T : Typ (L \u2237 \u03a3) K) (U : Typ \u03a3 L) (As : \u03a3\u27e6 \u03a3 \u27e7)\u2192\n T\u27e6 T \u27e7 (T\u27e6 U \u27e7 As , As) \u2261 T\u27e6 substn T U \u27e7 As\nsubstn\u27e6 T \u27e7\u27e6 U \u27e7 = substn+ [] \u27e6 T \u27e7\u27e6 U \u27e7 tt\n\nsubstn\u27e6_\u27e7\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 K L} (T : Typ (L \u2237 \u03a3) K) (U : Typ \u03a3 L) {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192\n T\u27e6 T \u27e7\u00b2 (T\u27e6 U \u27e7\u00b2 \u211cs , \u211cs) \u2261 \n struct K (substn\u27e6 T \u27e7\u27e6 U \u27e7 As) (T\u27e6 substn T U \u27e7\u00b2 \u211cs) (substn\u27e6 T \u27e7\u27e6 U \u27e7 Bs)\nsubstn\u27e6 T \u27e7\u27e6 U \u27e7\u00b2 = substn+ [] \u27e6 T \u27e7\u27e6 U \u27e7\u00b2 tt\n\n-- Eta-beta equivalence on types\n\ndata _\u220b_\u2263_ {\u03a3} : \u2200 K \u2192 Typ \u03a3 K \u2192 Typ \u03a3 K \u2192 Set where\n abs : \u2200 K {L T U} \u2192 (L \u220b T \u2263 U) \u2192 ((K \u21d2 L) \u220b abs K T \u2263 abs K U)\n app : \u2200 {K L F G T U} \u2192 ((K \u21d2 L) \u220b F \u2263 G) \u2192 (K \u220b T \u2263 U) \u2192 (L \u220b app F T \u2263 app G U)\n beta : \u2200 {K L} T U \u2192 (L \u220b app (abs K T) U \u2263 substn T U)\n eta : \u2200 {K L} T \u2192 ((K \u21d2 L) \u220b T \u2263 abs K (app (weaken (skip K id) T) tvar\u2080))\n \u2263-refl : \u2200 {K T} \u2192 (K \u220b T \u2263 T)\n \u2263-sym : \u2200 {K T U} \u2192 (K \u220b T \u2263 U) \u2192 (K \u220b U \u2263 T)\n \u2263-trans : \u2200 {K T U V} \u2192 (K \u220b T \u2263 U) \u2192 (K \u220b U \u2263 V) \u2192 (K \u220b T \u2263 V)\n\n\u2263\u27e6_\u27e7 : \u2200 {\u03a3 K} {T U : Typ \u03a3 K} \u2192 (K \u220b T \u2263 U) \u2192 \u2200 As \u2192 T\u27e6 T \u27e7 As \u2261 T\u27e6 U \u27e7 As\n\u2263\u27e6 abs K T\u2263U \u27e7 As = ext (\u03bb A \u2192 \u2263\u27e6 T\u2263U \u27e7 (A , As))\n\u2263\u27e6 app F\u2263G T\u2263U \u27e7 As = apply (\u2263\u27e6 F\u2263G \u27e7 As) (\u2263\u27e6 T\u2263U \u27e7 As)\n\u2263\u27e6 beta T U \u27e7 As = substn\u27e6 T \u27e7\u27e6 U \u27e7 As\n\u2263\u27e6 eta {K} T \u27e7 As = ext (\u03bb A \u2192 apply (weaken\u27e6 T \u27e7 (skip K id) (A , As)) refl)\n\u2263\u27e6 \u2263-refl \u27e7 As = refl\n\u2263\u27e6 \u2263-sym T\u2263U \u27e7 As = sym (\u2263\u27e6 T\u2263U \u27e7 As)\n\u2263\u27e6 \u2263-trans T\u2263U U\u2263V \u27e7 As = trans (\u2263\u27e6 T\u2263U \u27e7 As) (\u2263\u27e6 U\u2263V \u27e7 As)\n\n\u2263\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 K} {T U : Typ \u03a3 K} (T\u2263U : K \u220b T \u2263 U) {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 \n T\u27e6 T \u27e7\u00b2 \u211cs \u2261 struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs)\n\u2263\u27e6 abs K {L} {T} {U} T\u2263U \u27e7\u00b2 {As} {Bs} \u211cs = \n iext (\u03bb A \u2192 iext (\u03bb B \u2192 ext (\u03bb \u211c \u2192 begin\n T\u27e6 T \u27e7\u00b2 (\u211c , \u211cs)\n \u2261\u27e8 \u2263\u27e6 T\u2263U \u27e7\u00b2 (\u211c , \u211cs) \u27e9\n struct L (\u2263\u27e6 T\u2263U \u27e7 (A , As)) (T\u27e6 U \u27e7\u00b2 (\u211c , \u211cs)) (\u2263\u27e6 T\u2263U \u27e7 (B , Bs))\n \u2261\u27e8 struct-ext K L (\u03bb A \u2192 \u2263\u27e6 T\u2263U \u27e7 (A , As)) (\u03bb \u211c' \u2192 T\u27e6 U \u27e7\u00b2 (\u211c' , \u211cs)) (\u03bb B \u2192 \u2263\u27e6 T\u2263U \u27e7 (B , Bs)) \u211c \u27e9\n struct (K \u21d2 L) (\u2263\u27e6 abs K T\u2263U \u27e7 As) (T\u27e6 abs K U \u27e7\u00b2 \u211cs) (\u2263\u27e6 abs K T\u2263U \u27e7 Bs) \u211c\n \u220e)))\n\u2263\u27e6 app {K} {L} {F} {G} {T} {U} F\u2263G T\u2263U \u27e7\u00b2 {As} {Bs} \u211cs = \n begin\n T\u27e6 app F T \u27e7\u00b2 \u211cs\n \u2261\u27e8 cong (T\u27e6 F \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7\u00b2 \u211cs) \u27e9\n T\u27e6 F \u27e7\u00b2 \u211cs (struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs))\n \u2261\u27e8 cong (\u03bb X \u2192 X (struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs)))\n (\u2263\u27e6 F\u2263G \u27e7\u00b2 \u211cs) \u27e9\n struct (K \u21d2 L) (\u2263\u27e6 F\u2263G \u27e7 As) (T\u27e6 G \u27e7\u00b2 \u211cs) (\u2263\u27e6 F\u2263G \u27e7 Bs)\n (struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs))\n \u2261\u27e8 struct-apply K L\n (\u2263\u27e6 F\u2263G \u27e7 As) (T\u27e6 G \u27e7\u00b2 \u211cs) (\u2263\u27e6 F\u2263G \u27e7 Bs)\n (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs) \u27e9\n struct L (\u2263\u27e6 app F\u2263G T\u2263U \u27e7 As) (T\u27e6 app G U \u27e7\u00b2 \u211cs) (\u2263\u27e6 app F\u2263G T\u2263U \u27e7 Bs)\n \u220e\n\u2263\u27e6 beta T U \u27e7\u00b2 \u211cs = substn\u27e6 T \u27e7\u27e6 U \u27e7\u00b2 \u211cs\n\u2263\u27e6 eta {K} {L} T \u27e7\u00b2 {As} {Bs} \u211cs = iext (\u03bb A \u2192 iext (\u03bb B \u2192 ext (\u03bb \u211c \u2192 \n begin\n T\u27e6 T \u27e7\u00b2 \u211cs \u211c\n \u2261\u27e8 cong (\u03bb X \u2192 X \u211c) (weaken\u27e6 T \u27e7\u00b2 (skip K id) (\u211c , \u211cs)) \u27e9\n struct (K \u21d2 L) \n (weaken\u27e6 T \u27e7 (skip K id) (A , As))\n (T\u27e6 weaken (skip K id) T \u27e7\u00b2 (\u211c , \u211cs))\n (weaken\u27e6 T \u27e7 (skip K id) (B , Bs)) \u211c\n \u2261\u27e8 struct-apply K L \n (weaken\u27e6 T \u27e7 (skip K id) (A , As)) \n (T\u27e6 weaken (skip K id) T \u27e7\u00b2 (\u211c , \u211cs)) \n (weaken\u27e6 T \u27e7 (skip K id) (B , Bs)) refl \u211c refl \u27e9\n struct L \n (apply (weaken\u27e6 T \u27e7 (skip K id) (A , As)) refl)\n (T\u27e6 weaken (skip K id) T \u27e7\u00b2 (\u211c , \u211cs) \u211c)\n (apply (weaken\u27e6 T \u27e7 (skip K id) (B , Bs)) refl)\n \u2261\u27e8 struct-ext K L\n (\u03bb A \u2192 apply (weaken\u27e6 T \u27e7 (skip K id) (A , As)) refl) \n (\u03bb \u211c \u2192 T\u27e6 weaken (skip K id) T \u27e7\u00b2 (\u211c , \u211cs) \u211c) \n (\u03bb B \u2192 apply (weaken\u27e6 T \u27e7 (skip K id) (B , Bs)) refl) \u211c \u27e9\n struct (K \u21d2 L) \n (\u2263\u27e6 eta T \u27e7 As)\n (T\u27e6 abs K (app (weaken (skip K id) T) (var zero)) \u27e7\u00b2 \u211cs)\n (\u2263\u27e6 eta T \u27e7 Bs) \u211c\n \u220e)))\n\u2263\u27e6 \u2263-refl \u27e7\u00b2 \u211cs = refl\n\u2263\u27e6 \u2263-sym {K} {T} {U} T\u2263U \u27e7\u00b2 {As} {Bs} \u211cs = \n struct-sym K (\u2263\u27e6 T\u2263U \u27e7 As) (\u2263\u27e6 T\u2263U \u27e7 Bs) (\u2263\u27e6 T\u2263U \u27e7\u00b2 \u211cs)\n\u2263\u27e6 \u2263-trans {K} {T} {U} {V} T\u2263U U\u2263V \u27e7\u00b2 {As} {Bs} \u211cs =\n begin\n T\u27e6 T \u27e7\u00b2 \u211cs\n \u2261\u27e8 \u2263\u27e6 T\u2263U \u27e7\u00b2 \u211cs \u27e9\n struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs)\n \u2261\u27e8 cong (\u03bb X \u2192 struct K (\u2263\u27e6 T\u2263U \u27e7 As) X (\u2263\u27e6 T\u2263U \u27e7 Bs)) (\u2263\u27e6 U\u2263V \u27e7\u00b2 \u211cs) \u27e9\n struct K (\u2263\u27e6 T\u2263U \u27e7 As) (struct K (\u2263\u27e6 U\u2263V \u27e7 As) (T\u27e6 V \u27e7\u00b2 \u211cs) (\u2263\u27e6 U\u2263V \u27e7 Bs)) (\u2263\u27e6 T\u2263U \u27e7 Bs)\n \u2261\u27e8 struct-trans K (\u2263\u27e6 T\u2263U \u27e7 As) (\u2263\u27e6 U\u2263V \u27e7 As) (T\u27e6 V \u27e7\u00b2 \u211cs) (\u2263\u27e6 U\u2263V \u27e7 Bs) (\u2263\u27e6 T\u2263U \u27e7 Bs) \u27e9\n struct K (\u2263\u27e6 \u2263-trans T\u2263U U\u2263V \u27e7 As) (T\u27e6 V \u27e7\u00b2 \u211cs) (\u2263\u27e6 \u2263-trans T\u2263U U\u2263V \u27e7 Bs)\n \u220e\n\n-- Variables\n\ndata Var {\u03a3 : Kinds} {\u03b1} (T : Typ \u03a3 (set \u03b1)) : Typs \u03a3 \u2192 Set where\n zero : \u2200 {\u0393} \u2192 Var T (T \u2237 \u0393)\n suc : \u2200 {\u03b2 \u0393} {U : Typ \u03a3 (set \u03b2)} \u2192 Var T \u0393 \u2192 Var T (U \u2237 \u0393)\n\nx\u27e6_\u27e7 : \u2200 {\u03a3} {\u0393 : Typs \u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} \u2192 \n Var T \u0393 \u2192 (As : \u03a3\u27e6 \u03a3 \u27e7) \u2192 (as : \u0393\u27e6 \u0393 \u27e7 As) \u2192 (T\u27e6 T \u27e7 As)\nx\u27e6 zero \u27e7 As (a , as) = a\nx\u27e6 suc x \u27e7 As (a , as) = x\u27e6 x \u27e7 As as\n\nx\u27e6_\u27e7\u00b2 : \u2200 {\u03a3} {\u0393 : Typs \u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} (x : Var T \u0393) \u2192 \n \u2200 {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) {as bs} \u2192 \n (\u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs as bs) \u2192 (T\u27e6 T \u27e7\u00b2 \u211cs (x\u27e6 x \u27e7 As as) (x\u27e6 x \u27e7 Bs bs))\nx\u27e6 zero \u27e7\u00b2 \u211cs (a\u211cb , as\u211cbs) = a\u211cb\nx\u27e6 suc x \u27e7\u00b2 \u211cs (a\u211cb , as\u211cbs) = x\u27e6 x \u27e7\u00b2 \u211cs as\u211cbs\n\n-- Constants\n\ndata Const {\u03a3 : Kinds} : \u2200 {\u03b1} \u2192 Typ \u03a3 (set \u03b1) \u2192 Set where\n pair : \u2200 {\u03b1 \u03b2} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 (set \u03b2) (tvar\u2081 \u22b8 (tvar\u2080 \u22b8 (tvar\u2081 \u2297 tvar\u2080)))))\n fst : \u2200 {\u03b1 \u03b2} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 (set \u03b2) ((tvar\u2081 \u2297 tvar\u2080) \u22b8 tvar\u2081)))\n snd : \u2200 {\u03b1 \u03b2} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 (set \u03b2) ((tvar\u2081 \u2297 tvar\u2080) \u22b8 tvar\u2080)))\n \u227c-refl : Const (\u03a0 time (tvar\u2080 \u227c tvar\u2080))\n \u227c-trans : Const (\u03a0 time (\u03a0 time (\u03a0 time ((tvar\u2082 \u227c tvar\u2081) \u22b8 ((tvar\u2081 \u227c tvar\u2080) \u22b8 (tvar\u2082 \u227c tvar\u2080))))))\n \u227c-absurd : \u2200 {\u03b1} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 time (\u03a0 time ((tvar\u2081 \u227a tvar\u2080) \u22b8 ((tvar\u2080 \u227c tvar\u2081) \u22b8 tvar\u2082)))))\n \u227c-case : \u2200 {\u03b1} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 time (\u03a0 time (((tvar\u2081 \u227c tvar\u2080) \u22b8 tvar\u2082) \u22b8 (((tvar\u2080 \u227a tvar\u2081) \u22b8 tvar\u2082) \u22b8 tvar\u2082)))))\n\nabsurd : \u2200 {\u03b1} {A : Set \u03b1} b \u2192 True b \u2192 True (not b) \u2192 A\nabsurd true tt ()\nabsurd false () tt\n\ncond : \u2200 {\u03b1} {A : Set \u03b1} b \u2192 (True (not b) \u2192 A) \u2192 (True b \u2192 A) \u2192 A\ncond true f g = g tt\ncond false f g = f tt\n\nc\u27e6_\u27e7 : \u2200 {\u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} \u2192 \n Const T \u2192 (As : \u03a3\u27e6 \u03a3 \u27e7) \u2192 (T\u27e6 T \u27e7 As)\nc\u27e6 pair \u27e7 As = \u03bb A B a b \u2192 (a , b)\nc\u27e6 fst \u27e7 As = \u03bb A B \u2192 proj\u2081\nc\u27e6 snd \u27e7 As = \u03bb A B \u2192 proj\u2082\nc\u27e6 \u227c-refl \u27e7 As = \u2264-refl\nc\u27e6 \u227c-trans \u27e7 As = \u2264-trans\nc\u27e6 \u227c-absurd \u27e7 As = \u03bb A t u \u2192 absurd (t < u)\nc\u27e6 \u227c-case \u27e7 As = \u03bb A t u \u2192 cond (u < t)\n\ncond\u00b2 : \u2200 {\u03b1} {A A\u2032 : Set \u03b1} (\u211c : A \u2192 A\u2032 \u2192 Set \u03b1) b \u2192\n \u2200 {f f\u2032} \u2192 (\u2200 b\u00d7 \u2192 \u211c (f b\u00d7) (f\u2032 b\u00d7)) \u2192\n \u2200 {g g\u2032} \u2192 (\u2200 b\u2713 \u2192 \u211c (g b\u2713) (g\u2032 b\u2713)) \u2192\n \u211c (cond b f g) (cond b f\u2032 g\u2032)\ncond\u00b2 \u211c true f\u211cf\u2032 g\u211cg\u2032 = g\u211cg\u2032 tt\ncond\u00b2 \u211c false f\u211cf\u2032 g\u211cg\u2032 = f\u211cf\u2032 tt\n\nc\u27e6_\u27e7\u00b2 : \u2200 {\u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} (c : Const T) \u2192 \n \u2200 {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 \n (T\u27e6 T \u27e7\u00b2 \u211cs (c\u27e6 c \u27e7 As) (c\u27e6 c \u27e7 Bs))\nc\u27e6 pair \u27e7\u00b2 \u211cs = \u03bb \u211c \u2111 a\u211cb c\u2111d \u2192 (a\u211cb , c\u2111d)\nc\u27e6 fst \u27e7\u00b2 \u211cs = \u03bb \u211c \u2111 \u2192 proj\u2081\nc\u27e6 snd \u27e7\u00b2 \u211cs = \u03bb \u211c \u2111 \u2192 proj\u2082\nc\u27e6 \u227c-refl \u27e7\u00b2 \u211cs = _\nc\u27e6 \u227c-trans \u27e7\u00b2 \u211cs = _\nc\u27e6 \u227c-absurd \u27e7\u00b2 \u211cs = \u03bb \u211c {t} t\u2261t {u} u\u2261u {t Set : is type family of responses for commands\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\" input to output\n -- input\n -- - pure function 'a\u2192b : a -> b'\n -- - postcondition 'b -> Set' (on output of a\u2192b)\n -- output\n -- - weakest precondition 'a -> Set' on input to a\u2192b that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n module WP0Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data isEven : \u2115 \u2192 Set where\n even-z : isEven Nat.zero\n even-s : {n : \u2115} \u2192 isEven n \u2192 isEven (Nat.suc (Nat.suc n))\n\n isEvenPT : \u2115 -> Set\n isEvenPT = wp0 (_+ 2) isEven\n\n isEven5 : Set\n isEven5 = isEvenPT 5\n\n isEven5' : isEvenPT 5 \u2261 isEven 7\n isEven5' = refl\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - enable via making dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n -- every element a that satisfies P is contained in the set of things that satifies Q\n --\n -- implication between predicates\n --\n -- P a says a \u2208 P\n -- if the extents of P, Q : A \u2192 Set is taken to be\n -- the subset of A satisfying predicates P, Q\n --\n -- the extent of P : A -> Set is the subset of terms of type A satisfying P.\n -- \"extent\" indicates a kind of black-box view of things,\n -- e.g., the \"extent\" of a function is the set of its input-output pairs.\n --\n -- https:\/\/en.wikipedia.org\/wiki\/Extension_%28semantics%29\n -- the extension of a concept, idea, or sign consists of the things to which it applies\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n module \u2286Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data _\u2264_ : \u2115 \u2192 \u2115 \u2192 Set where\n z\u2264n : \u2200 {n : \u2115} \u2192 0 \u2264 n\n s\u2264s : \u2200 {m n : \u2115} \u2192 m \u2264 n \u2192 Nat.suc m \u2264 Nat.suc n\n\n \u22641\u2286\u22642 : (_\u2264 1) \u2286 (_\u2264 2)\n -- (a : \u2115) (a \u2264 1) (a \u2264 2)\n \u22641\u2286\u22642 Zero _ = z\u2264n\n \u22641\u2286\u22642 (Succ Zero) (s\u2264s _) = s\u2264s z\u2264n\n\n {- cannot be proved\n \u22642\u2286\u22641 : (_\u2264 2) \u2286 (_\u2264 1)\n \u22642\u2286\u22641 Zero _ = z\u2264n\n \u22642\u2286\u22641 (Succ a) (s\u2264s p) = {!!}\n -}\n\n -- refinement relation between PTs : pt1 refined by pt2\n --\n -- (same thing as \u2286, but one level up)\n -- for every post condition, pt2 weakens the precondition\n -- \"weakest\" is the right side : pt2 P\n --\n -- pt1 , pt2 : preconditions for satisfying a program\n -- refinement : pt1 maybe be a stronger requirement than pt2\n --\n -- pt2 is a refinement because it weakens what is required by pt1\n -- e.g., pt1 pt2\n -- >= 20 >= 10\n --\n -- weakening the input assumptions gives a stronger result\n --\n -- contract says establish a pre and I will give you a post, i.e.,\n -- \"stronger\" post condition\n -- or \"weaker\" pre condition\n --\n -- refinement of predicate transformers\n --\n -- use case: relate different implementations of the \"same program\"\n -- pt1 \u2291 pt2 : pt1 is refined by pt2\n -- - if pt1 is understood as giving preconditions which satisfy\n -- postcondition P for some given program f1\n -- then his says the precondition can be \/weakened\/ and still guarantee the postcondition\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g\n -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation to\n - relate PT semantics between programs and specifications\n - show a program satisfies its specification\n - show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n - corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n note : for =Free C R b= programs means generating the same AST\n\n instead, define predicate transformers that give \/interpretations\/\n to a particular choice of command type C and response family R\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public using (_+_; _>_; _*_)\n renaming (\u2115 to Nat; zero to Zero; suc to Succ)\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n -- one command\n data C : Set where\n Abort : C -- no continuation\n\n -- response code : the program does not execute further\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n -- BIG-STEP SEMANTICS specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n module \u21d3Test where\n exv : Expr\n exv = Val 3\n exd : Expr\n exd = Div (Val 3) (Val 0)\n\n exb0 : Val 0 \u21d3 0\n exb0 = \u21d3Base\n\n exb3 : Val 3 \u21d3 3\n exb3 = \u21d3Base\n\n exs331 : Div (Val 3) (Val 3) \u21d3 1\n exs331 = \u21d3Step \u21d3Base \u21d3Base\n\n exs931 : Div (Val 9) (Val 3) \u21d3 3\n exs931 = \u21d3Step \u21d3Base \u21d3Base\n\n exs9d31 : Div (Val 9) (Div (Val 3) (Val 1)) \u21d3 3\n exs9d31 = \u21d3Step \u21d3Base (\u21d3Step \u21d3Base \u21d3Base)\n\n exsd91d31 : Div (Div (Val 9) (Val 1)) (Div (Val 3) (Val 1)) \u21d3 3\n exsd91d31 = \u21d3Step (\u21d3Step \u21d3Base \u21d3Base) (\u21d3Step \u21d3Base \u21d3Base)\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val n \u27e7 = return n\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\nl ->\n \u27e6 er \u27e7 >>= \\nr ->\n nl \u00f7 nr\n\n module InterpTest where\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n evv' : evv \u2261 Pure 3\n evv' = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n evd' : evd \u2261 Pure 1\n evd' = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n evd0' : evd0 \u2261 Step Abort (\u03bb ())\n evd0' = refl\n\n -- relate big step semantics to monadic interpreter\n\n -- convert post-condition for a pure function 'f : (x : a) -> b x'\n -- to post-condition on partial functions 'f : (x : A) -> Partial (b x)'\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba -- P holds if operation produces a value\n mustPT _ _ (Step Abort _) = \u22a5 -- if operation fails, there is no result to apply P to\n -- note: could give \u22a4 here, but want to rule out failure\n -- (total correctness)\n\n module MustPTTest where\n bs\u21d3 : Expr -> Nat -> Set\n bs\u21d3 = _\u21d3_\n\n bs\u21d3' : Set\n bs\u21d3' = Val 1 \u21d3 1\n\n bsp\u21d3 : Expr -> Partial Nat -> Set\n bsp\u21d3 = mustPT _\u21d3_\n\n bsp\u21d3' : Set\n bsp\u21d3' = mustPT _\u21d3_ (Val 1) (Pure 1)\n\n bsp\u21d3'x : mustPT _\u21d3_ (Val 1) (Pure 1)\n bsp\u21d3'x = \u21d3Base\n\n bsp\u21d3'' : Set\n bsp\u21d3'' = mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n {- nothing can be constructed with this type\n bsp\u21d3''x : mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n bsp\u21d3''x = {!!}\n -}\n xxx : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb n -> Pure 3 >>= _\u00f7 n))\n xxx = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx' : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n xxx' = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx'' : Set\n xxx'' = mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n\n {-\n to relate big step semantics to monadic interpreter\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - _\u21d3_ relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n\n to call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results P' : Partial b -> Set\n - done via 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so mustPT Abort case returns empty type\n -}\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n module WpPartialTest where\n exwpp : Expr -> Set\n exwpp = wpPartial \u27e6_\u27e7 _\u21d3_\n exwpp' : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n exwpp' = refl\n\n wppv1 : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n wppv1 = \u21d3Base\n\n wppd11 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n wppd11 = \u21d3Step \u21d3Base \u21d3Base\n wppd11' : Set\n wppd11' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n\n {- this type cannot be constructed\n wppd10 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n wppd10 = {!!}\n -}\n wppd10' : Set\n wppd10' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n\n {-\n wpPartial \u27e6_\u27e7 _\u21d3_ : a predicate on expressions\n - a way to express SUCCESSFUL evaluation of an Expr with intended semantics\n\n \u2200 exprs satisfying this predicate\n - monadic interpreter and\n - relational big-step specification\n must agree on result of evaluation\n\n for a given Expr, satisfaction of predicate means\n - Expr evaluates to a value by big-step operational semantics\n - if interpreter returns a value at all\n then that value agrees with big step semantics\n - if interpreter aborts\n then have a proof of \u22a5 (so, it does not abort)\n which will NOT agree with \u21d3 big-step semantics since \u21d3 does not allow div-by-0\n\n What does this say about correctness of interpreter?\n\n does not specify how many Expr that satisfy that characterization also satisfy wpPartial \u27e6_\u27e7 _\u21d3_\n\n to understand wpPartial \u27e6_\u27e7 _\u21d3_ better\n - define SafeDiv : safety criteria that we believe in\n - prove every expression that satisfies SafeDiv satisfies wpPartial \u27e6_\u27e7 _\u21d3_\n - SafeDiv refines wpPartial \u27e6_\u27e7 _\u21d3_\n - if we gave \u22a4 above, then 'correct' would not say that SafeDiv rules out undefined behavior\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val _) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n module SafeDivTest where\n exsdv3 : SafeDiv (Val 3) \u2261 \u22a4\n exsdv3 = refl\n exsdv0 : SafeDiv (Val 0) \u2261 \u22a4\n exsdv0 = refl\n exsdv0' : Set\n exsdv0' = SafeDiv (Val 0)\n\n exsd33 : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd33 = refl\n exsd33' : Set\n exsd33' = SafeDiv (Div (Val 3) (Val 3))\n\n exsd30 : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val Zero \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd30 = refl\n exsd30' : Set\n exsd30' = SafeDiv (Div (Val 3) (Val 0))\n\n {-\n Expect : any expr e for which SafeDiv e holds can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n -- (a : Expr) (SafeDiv a) (wpPartial \u27e6_\u27e7 _\u21d3_ a)\n correct (Val _) _ = \u21d3Base\n correct (Div el er) (er\u21d30\u2192\u22a5 , (sdel , sder))\n with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n ... | Pure _ | Pure Zero | _ | er\u21d30 = magic (er\u21d30\u2192\u22a5 er\u21d30)\n ... | Pure _ | Pure (Succ _) | el\u21d3nl | er\u21d3Snr = \u21d3Step el\u21d3nl er\u21d3Snr\n ... | Pure _ | Step Abort _ | _ | ()\n ... | Step Abort _ | _ | () | _\n\n module CorrectTest where\n xxx : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n xxx = \u21d3Base\n\n yyy : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 1))\n yyy = \u21d3Step \u21d3Base \u21d3Base\n {-\n zzz : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 0))\n zzz = {!!}\n -}\n {-\n Generalize above.\n Instead of manually defining SafeDiv,\n define more general predicate\n characterising the domain of a partial function using wpPartial\n -}\n\n -- HC: compare to 'mustPT'\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n -- can show that the two semantics agree precisely on the domain of the interpreter:\n\n -- everything in domain of interpreter\n -- gets evaluated in agreement with big step semantics\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n -- only things interpreted in agreement with big step semantics\n -- are those that are in domain of interpreter\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on argument expression\n\n sound (Val _) _ = \u21d3Base\n sound (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div _ _) () | Pure _ | Pure Zero | _ | _\n sound (Div _ _) _ | Pure nl | Pure (Succ nr) | \u22a4'zero\u2192el\u21d3nl | \u22a4'zero\u2192er\u21d3Succnr\n = \u21d3Step (\u22a4'zero\u2192el\u21d3nl tt) (\u22a4'zero\u2192er\u21d3Succnr tt)\n sound (Div _ _) () | Pure _ | Step Abort _ | _ | _\n sound (Div _ _) () | Step Abort _ | _ | _ | _\n\n inDom : {n : Nat}\n -> (e : Expr)\n -> \u27e6 e \u27e7 == Pure n\n -> dom \u27e6_\u27e7 e\n inDom (Val _) _ = tt\n inDom (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div _ _) () | Pure _ | Pure Zero\n inDom (Div _ _) _ | Pure _ | Pure (Succ _) = tt\n inDom (Div _ _) () | Pure _ | Step Abort _\n inDom (Div _ _) () | Step Abort _ | _\n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n : (e : Expr) (n : Nat)\n -> \u27e6 e \u27e7 \u2261 Pure n\n -> e \u21d3 n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n e _ eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} Diveler\u21d3_nldivSuccn\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 { _} { _} () | Pure _ | _ | Pure Zero\n wpPartial1 {el} { _} _ | Pure nl | [[[ eq ]]] | Pure (Succ _) = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n el nl eq\n wpPartial1 { _} { _} () | Pure _ | _ | Step Abort _\n wpPartial1 { _} { _} () | Step Abort _ | _ | _\n\n wpPartial2 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} _ with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 { _} {er} _ | Pure _ | _ | Pure nr | [[[ eqnr ]]] = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n er nr eqnr\n wpPartial2 { _} { _} () | Pure _ | _ | Step Abort _ | _\n wpPartial2 { _} { _} () | Step Abort _ | _ | _ | _\n\n complete (Val _) _ = tt\n complete (Div el er) h\n with \u27e6 el \u27e7\n | inspect \u27e6_\u27e7 el\n | \u27e6 er \u27e7\n | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div _ _) h | Pure nl | [[[ \u27e6el\u27e7\u2261Purenl ]]] | Pure Zero | [[[ \u27e6er\u27e7\u2261Pure0 ]]] | _ | _\n -- Goal\n -- mustPT (\u03bb _ _ \u2192 \u22a4' zero)\n -- (Div el er) (Pure nl >>= (\u03bb nl' \u2192 Pure Zero >>= _\u00f7_ nl'))\n -- \u22a5\n -- Context\n -- h : mustPT _\u21d3_ (Div el er) (\u27e6 el \u27e7 >>= (\u03bb nl' \u2192 \u27e6 er \u27e7 >>= _\u00f7_ nl'))\n rewrite\n \u27e6el\u27e7\u2261Purenl -- h : mustPT _\u21d3_ (Div el er) (\u27e6 er \u27e7 >>= _\u00f7_ nl)\n | \u27e6er\u27e7\u2261Pure0 -- h : \u22a5\n = h -- magic h\n complete (Div _ _) _ | Pure _ | _ | Pure (Succ _) | _ | _ | _ = tt\n complete (Div _ _) _ | Pure _ | _ | Step Abort _ | _ | _ | ()\n complete (Div _ _) _ | Step Abort _ | _ | _ | _ | () | _\n\n module CompleteTest where\n {-\n xxx : mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n xxx = {!!}\n -}\n xxx' : Set\n xxx' = mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement to relate Kleisli morphisms,\n and to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n add top two elements; can fail fail if stack has too few elements\n\n below shows how to prove the definition meets its specification\n\n Define specification in terms of a pre\/post condition.\n -}\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n {-\n [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n for non-dependent examples (e.g., type b does not depend on x : a)\n -}\n\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function\n\n -- specification for addition function : describes the desired postcondition\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n\n {-\n How to relate this specification to an implementation?\n Note: 'wpPartial' assigns predicate transformer semantics to functions\n - but do not yet have a corresponding predicate transform semantics for specifications.\n wpSpec function does that:\n -}\n\n -- Given a specification, Spec a b\n -- computes the weakest precondition that satisfies an arbitrary postcondition P\n -- i.e., the spec\u2019s precondition should hold and its postcondition must imply P.\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\x -> (pre x) \u2227 (post x \u2286 P x)\n\n -- Can now formulate program\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs =\n pop xs >>= \\{(x1 , xs) ->\n pop xs >>= \\{(x2 , xs) ->\n return ((x1 + x2) :: xs)}}\n\n -- that refines the specification given by addSpec:\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd P Nil (() , _)\n correctnessAdd P (x :: Nil) (Data.Nat.s\u2264s () , _)\n correctnessAdd P (x :: y :: xs) (_ , H) = H (x + y :: xs) AddStep\n\n {-\n This example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition,\n - to its implementation.\n\n Compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n get : State s\n get = Step Get return\n\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n statePT : forall {l l'} -> {b : Set l} -> (b \u00d7 s -> Set l') -> State b -> (s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> (a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> (a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s') = (flatten t' == (seq (s) (size t))) \u2227 (s + size t == s')\n\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n correctnessRelabel : forall {a : Set} -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set} -> (c : State a) (f : a -> State b) ->\n \u2200 i P -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification,\n -- by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence,\n -- specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b} (mx : State a) (f : a -> State b) P i ->\n statePT (wpState f \\_ -> P) mx i -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b} (mx : State a) (f : a -> State b) spec ->\n \u2200 P i -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i) ->\n Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P)) ->\n statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s} -> SpecK {zero} (a \u00d7 s) (b \u00d7 s) -> a -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a) (trans\n (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr ->\n Pair (fl \u2261 seq s sl) (s + sl \u2261 s') ->\n Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'') ->\n Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s -> wpSpec relabelSpec P (t , s) -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set} ->\n ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set} -> (c : Free C R a) (f : a -> Free C R b) ->\n \u2200 P -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P =\n cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R} -> (a -> Free C R b) -> (b -> Free C R c) -> a -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set} -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c) ->\n wpCR f1 \u2291 wpCR f2 ->\n wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","old_contents":"module PredicateTransformers where\n\nopen import Prelude hiding (map; all)\nopen import Level hiding (lift)\n\n------------------------------------------------------------------------------\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\n------------------------------------------------------------------------------\nABSTRACT\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nrefinement type https:\/\/en.wikipedia.org\/wiki\/Refinement_(computing)\n- type with a predicate which must hold for any element of the refined type\n- can express\n - preconditions when used as function args\n - postconditions when used as return types\n\nthe problem statement is centered around \/compositionality\/\n- pure functions enable compositional reasoning\n- effectful programs require reasoning about context\n- paper provides mechanism for reasoning about effectful programs (and for program calculation)\n\nmain idea\n- represent effectful computation as a free monad\n- write an \"interpreter\" that computes a predicate transformer\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to use this refinement relation to\n - show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\nmodule Free where\n -- Free : represents syntax of an effectful computation\n -- C : type of commands\n -- Free C R : returns an 'a' or issues command c : C\n -- For each c : C, there is a set of responses R c\n -- - R : C -> Set : is type family of responses for commands\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\" input to output\n -- input\n -- - pure function 'a\u2192b : a -> b'\n -- - postcondition 'b -> Set' (on output of a\u2192b)\n -- output\n -- - weakest precondition 'a -> Set' on input to a\u2192b that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n module WP0Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data isEven : \u2115 \u2192 Set where\n even-z : isEven Nat.zero\n even-s : {n : \u2115} \u2192 isEven n \u2192 isEven (Nat.suc (Nat.suc n))\n\n isEvenPT : \u2115 -> Set\n isEvenPT = wp0 (_+ 2) isEven\n\n isEven5 : Set\n isEven5 = isEvenPT 5\n\n isEven5' : isEvenPT 5 \u2261 isEven 7\n isEven5' = refl\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - enable via making dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n -- every element a that satisfies P is contained in the set of things that satifies Q\n --\n -- implication between predicates\n --\n -- P a says a \u2208 P\n -- if the extents of P, Q : A \u2192 Set is taken to be\n -- the subset of A satisfying predicates P, Q\n --\n -- the extent of P : A -> Set is the subset of terms of type A satisfying P.\n -- \"extent\" indicates a kind of black-box view of things,\n -- e.g., the \"extent\" of a function is the set of its input-output pairs.\n --\n -- https:\/\/en.wikipedia.org\/wiki\/Extension_%28semantics%29\n -- the extension of a concept, idea, or sign consists of the things to which it applies\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n -- refinement relation between PTs : pt1 refined by pt2\n --\n -- (same thing as \u2286, but one level up)\n -- for every post condition, pt2 weakens the precondition\n -- \"weakest\" is the right side : pt2 P\n --\n -- pt1 , pt2 : preconditions for satisfying a program\n -- refinement : pt1 maybe be a stronger requirement than pt2\n --\n -- pt2 is a refinement because it weakens what is required by pt1\n -- e.g., pt1 pt2\n -- >= 20 >= 10\n --\n -- weakening the input assumptions gives a stronger result\n --\n -- contract says establish a pre and I will give you a post, i.e.,\n -- \"stronger\" post condition\n -- or \"weaker\" pre condition\n --\n -- refinement of predicate transformers\n --\n -- use case: relate different implementations of the \"same program\"\n -- pt1 \u2291 pt2 : pt1 is refined by pt2\n -- - if pt1 is understood as giving preconditions which satisfy\n -- postcondition P for some given program f1\n -- then his says the precondition can be \/weakened\/ and still guarantee the postcondition\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g\n -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation to\n - relate PT semantics between programs and specifications\n - show a program satisfies its specification\n - show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n - corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n note : for =Free C R b= programs means generating the same AST\n\n instead, define predicate transformers that give \/interpretations\/\n to a particular choice of command type C and response family R\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public using (_+_; _>_; _*_)\n renaming (\u2115 to Nat; zero to Zero; suc to Succ)\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n -- one command\n data C : Set where\n Abort : C -- no continuation\n\n -- response code : the program does not execute further\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n -- BIG-STEP SEMANTICS specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n module \u21d3Test where\n exv : Expr\n exv = Val 3\n exd : Expr\n exd = Div (Val 3) (Val 0)\n\n exb0 : Val 0 \u21d3 0\n exb0 = \u21d3Base\n\n exb3 : Val 3 \u21d3 3\n exb3 = \u21d3Base\n\n exs331 : Div (Val 3) (Val 3) \u21d3 1\n exs331 = \u21d3Step \u21d3Base \u21d3Base\n\n exs931 : Div (Val 9) (Val 3) \u21d3 3\n exs931 = \u21d3Step \u21d3Base \u21d3Base\n\n exs9d31 : Div (Val 9) (Div (Val 3) (Val 1)) \u21d3 3\n exs9d31 = \u21d3Step \u21d3Base (\u21d3Step \u21d3Base \u21d3Base)\n\n exsd91d31 : Div (Div (Val 9) (Val 1)) (Div (Val 3) (Val 1)) \u21d3 3\n exsd91d31 = \u21d3Step (\u21d3Step \u21d3Base \u21d3Base) (\u21d3Step \u21d3Base \u21d3Base)\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val n \u27e7 = return n\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\nl ->\n \u27e6 er \u27e7 >>= \\nr ->\n nl \u00f7 nr\n\n module InterpTest where\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n evv' : evv \u2261 Pure 3\n evv' = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n evd' : evd \u2261 Pure 1\n evd' = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n evd0' : evd0 \u2261 Step Abort (\u03bb ())\n evd0' = refl\n\n {-\n to relate big step semantics to monadic interpreter\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - _\u21d3_ relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n\n to call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results P' : Partial b -> Set\n done via 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so Abort case returns empty type\n -}\n\n -- convert post-condition for a pure function 'f : (x : a) -> b x'\n -- to post-condition on partial functions 'f : (x : A) -> Partial (b x)'\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba -- P holds if operation produces a value\n mustPT _ _ (Step Abort _) = \u22a5 -- if operation fails, there is no result to apply P to\n -- note: could give \u22a4 here, but want to rule out failure\n -- (total correctness)\n\n module MustPTTest where\n bs\u21d3 : Expr -> Nat -> Set\n bs\u21d3 = _\u21d3_\n\n bs\u21d3' : Set\n bs\u21d3' = Val 1 \u21d3 1\n\n bsp\u21d3 : Expr -> Partial Nat -> Set\n bsp\u21d3 = mustPT _\u21d3_\n\n bsp\u21d3' : Set\n bsp\u21d3' = mustPT _\u21d3_ (Val 1) (Pure 1)\n\n bsp\u21d3'x : mustPT _\u21d3_ (Val 1) (Pure 1)\n bsp\u21d3'x = \u21d3Base\n\n bsp\u21d3'' : Set\n bsp\u21d3'' = mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n\n {- nothing can be constructed with this type\n bsp\u21d3''x : mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n bsp\u21d3''x = {!!}\n -}\n xxx : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb n -> Pure 3 >>= _\u00f7 n))\n xxx = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx' : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n xxx' = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx'' : Set\n xxx'' = mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n module WpPartialTest where\n exwpp : Expr -> Set\n exwpp = wpPartial \u27e6_\u27e7 _\u21d3_\n exwpp' : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n exwpp' = refl\n\n wppv1 : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n wppv1 = \u21d3Base\n\n wppd11 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n wppd11 = \u21d3Step \u21d3Base \u21d3Base\n wppd11' : Set\n wppd11' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n\n {- this type cannot be constructed\n wppd10 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n wppd10 = {!!}\n -}\n wppd10' : Set\n wppd10' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n\n {-\n wpPartial \u27e6_\u27e7 _\u21d3_ : a predicate on expressions\n - a way to express SUCCESSFUL evaluation of an Expr with intended semantics\n\n \u2200 exprs satisfying this predicate\n - monadic interpreter and\n - relational big-step specification\n must agree on result of evaluation\n\n for a given Expr, satisfaction of predicate means\n - Expr evaluates to a value by big-step operational semantics\n - if interpreter returns a value at all\n then that value agrees with big step semantics\n - if interpreter aborts\n then have a proof of \u22a5 (so, it does not abort)\n which will NOT agree with \u21d3 big-step semantics since \u21d3 does not allow div-by-0\n\n What does this say about correctness of interpreter?\n\n does not specify how many Expr that satisfy that characterization also satisfy wpPartial \u27e6_\u27e7 _\u21d3_\n\n to understand wpPartial \u27e6_\u27e7 _\u21d3_ better\n - define SafeDiv : safety criteria that we believe in\n - prove every expression that satisfies SafeDiv satisfies wpPartial \u27e6_\u27e7 _\u21d3_\n - SafeDiv refines wpPartial \u27e6_\u27e7 _\u21d3_\n - if we gave \u22a4 above, then 'correct' would not say that SafeDiv rules out undefined behavior\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val _) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n module SafeDivTest where\n exsdv3 : SafeDiv (Val 3) \u2261 \u22a4\n exsdv3 = refl\n exsdv0 : SafeDiv (Val 0) \u2261 \u22a4\n exsdv0 = refl\n exsdv0' : Set\n exsdv0' = SafeDiv (Val 0)\n\n exsd33 : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd33 = refl\n exsd33' : Set\n exsd33' = SafeDiv (Div (Val 3) (Val 3))\n\n exsd30 : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val Zero \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd30 = refl\n exsd30' : Set\n exsd30' = SafeDiv (Div (Val 3) (Val 0))\n\n {-\n Expect : any expr e for which SafeDiv e holds can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n correct (Val _) _ = \u21d3Base\n correct (Div el er) (er\u21d30\u2192\u22a5 , (sdel , sder))\n with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n ... | Pure _ | Pure Zero | _ | er\u21d30 = magic (er\u21d30\u2192\u22a5 er\u21d30)\n ... | Pure _ | Pure (Succ _) | el\u21d3nl | er\u21d3Snr = \u21d3Step el\u21d3nl er\u21d3Snr\n ... | Pure _ | Step Abort _ | _ | ()\n ... | Step Abort _ | _ | () | _\n\n {-\n Generalize above.\n Instead of manually defining SafeDiv,\n define more general predicate\n characterising the domain of a partial function using wpPartial\n -}\n\n -- HC: compare to 'mustPT'\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n -- can show that the two semantics agree precisely on the domain of the interpreter:\n\n -- everything in domain of interpreter\n -- gets evaluated in agreement with big step semantics\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n -- only things interpreted in agreement with big step semantics\n -- are those that are in domain of interpreter\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on argument expression\n\n sound (Val _) _ = \u21d3Base\n sound (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div _ _) () | Pure _ | Pure Zero | _ | _\n sound (Div _ _) _ | Pure nl | Pure (Succ nr) | \u22a4'zero\u2192el\u21d3nl | \u22a4'zero\u2192er\u21d3Succnr\n = \u21d3Step (\u22a4'zero\u2192el\u21d3nl tt) (\u22a4'zero\u2192er\u21d3Succnr tt)\n sound (Div _ _) () | Pure _ | Step Abort _ | _ | _\n sound (Div _ _) () | Step Abort _ | _ | _ | _\n\n inDom : {n : Nat}\n -> (e : Expr)\n -> \u27e6 e \u27e7 == Pure n\n -> dom \u27e6_\u27e7 e\n inDom (Val _) _ = tt\n inDom (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div _ _) () | Pure _ | Pure Zero\n inDom (Div _ _) _ | Pure _ | Pure (Succ _) = tt\n inDom (Div _ _) () | Pure _ | Step Abort _\n inDom (Div _ _) () | Step Abort _ | _\n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n : (e : Expr) (n : Nat)\n -> \u27e6 e \u27e7 \u2261 Pure n\n -> e \u21d3 n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n e _ eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} Diveler\u21d3_nldivSuccn\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 { _} { _} () | Pure _ | _ | Pure Zero\n wpPartial1 {el} { _} _ | Pure nl | [[[ eq ]]] | Pure (Succ _) = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n el nl eq\n wpPartial1 { _} { _} () | Pure _ | _ | Step Abort _\n wpPartial1 { _} { _} () | Step Abort _ | _ | _\n\n wpPartial2 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} _ with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 { _} {er} _ | Pure _ | _ | Pure nr | [[[ eqnr ]]] = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n er nr eqnr\n wpPartial2 { _} { _} () | Pure _ | _ | Step Abort _ | _\n wpPartial2 { _} { _} () | Step Abort _ | _ | _ | _\n\n complete (Val _) _ = tt\n complete (Div el er) h\n with \u27e6 el \u27e7\n | inspect \u27e6_\u27e7 el\n | \u27e6 er \u27e7\n | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div _ _) h | Pure nl | [[[ \u27e6el\u27e7\u2261Purenl ]]] | Pure Zero | [[[ \u27e6er\u27e7\u2261Pure0 ]]] | _ | _\n -- Goal\n -- mustPT (\u03bb _ _ \u2192 \u22a4' zero)\n -- (Div el er) (Pure nl >>= (\u03bb nl' \u2192 Pure Zero >>= _\u00f7_ nl'))\n -- \u22a5\n -- Context\n -- h : mustPT _\u21d3_ (Div el er) (\u27e6 el \u27e7 >>= (\u03bb nl' \u2192 \u27e6 er \u27e7 >>= _\u00f7_ nl'))\n rewrite\n \u27e6el\u27e7\u2261Purenl -- h : mustPT _\u21d3_ (Div el er) (\u27e6 er \u27e7 >>= _\u00f7_ nl)\n | \u27e6er\u27e7\u2261Pure0 -- h : \u22a5\n = h -- magic h\n complete (Div _ _) _ | Pure _ | _ | Pure (Succ _) | _ | _ | _ = tt\n complete (Div _ _) _ | Pure _ | _ | Step Abort _ | _ | _ | ()\n complete (Div _ _) _ | Step Abort _ | _ | _ | _ | () | _\n\n module CompleteTest where\n {-\n xxx : mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n xxx = {!!}\n -}\n xxx' : Set\n xxx' = mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement to relate Kleisli morphisms,\n and to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n add top two elements; can fail fail if stack has too few elements\n\n below shows how to prove the definition meets its specification\n\n Define specification in terms of a pre\/post condition.\n -}\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n {-\n [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n for non-dependent examples (e.g., type b does not depend on x : a)\n -}\n\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function\n\n -- specification for addition function : describes the desired postcondition\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n\n {-\n How to relate this specification to an implementation?\n Note: 'wpPartial' assigns predicate transformer semantics to functions\n - but do not yet have a corresponding predicate transform semantics for specifications.\n wpSpec function does that:\n -}\n\n -- Given a specification, Spec a b\n -- computes the weakest precondition that satisfies an arbitrary postcondition P\n -- i.e., the spec\u2019s precondition should hold and its postcondition must imply P.\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\x -> (pre x) \u2227 (post x \u2286 P x)\n\n -- Can now formulate program\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs =\n pop xs >>= \\{(x1 , xs) ->\n pop xs >>= \\{(x2 , xs) ->\n return ((x1 + x2) :: xs)}}\n\n -- that refines the specification given by addSpec:\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd P Nil (() , _)\n correctnessAdd P (x :: Nil) (Data.Nat.s\u2264s () , _)\n correctnessAdd P (x :: y :: xs) (_ , H) = H (x + y :: xs) AddStep\n\n {-\n This example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition,\n - to its implementation.\n\n Compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n get : State s\n get = Step Get return\n\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n statePT : forall {l l'} -> {b : Set l} -> (b \u00d7 s -> Set l') -> State b -> (s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> (a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> (a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s') = (flatten t' == (seq (s) (size t))) \u2227 (s + size t == s')\n\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n correctnessRelabel : forall {a : Set} -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set} -> (c : State a) (f : a -> State b) ->\n \u2200 i P -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification,\n -- by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence,\n -- specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b} (mx : State a) (f : a -> State b) P i ->\n statePT (wpState f \\_ -> P) mx i -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b} (mx : State a) (f : a -> State b) spec ->\n \u2200 P i -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i) ->\n Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P)) ->\n statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s} -> SpecK {zero} (a \u00d7 s) (b \u00d7 s) -> a -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a) (trans\n (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr ->\n Pair (fl \u2261 seq s sl) (s + sl \u2261 s') ->\n Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'') ->\n Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s -> wpSpec relabelSpec P (t , s) -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set} ->\n ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set} -> (c : Free C R a) (f : a -> Free C R b) ->\n \u2200 P -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P =\n cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R} -> (a -> Free C R b) -> (b -> Free C R c) -> a -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set} -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c) ->\n wpCR f1 \u2291 wpCR f2 ->\n wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"5748a712a365f87abbea3ba2e246ee805f07d463","subject":"progress progress #3","message":"progress progress #3\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress.agda","new_file":"progress.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\n\nopen import canonical-boxed-forms\nopen import canonical-value-forms\nopen import canonical-indeterminate-forms\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d boxedval + d indet + \u03a3[ d' \u2208 dhexp ] (d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n V : \u2200{d \u0394} \u2192 d boxedval \u2192 ok d \u0394\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = V (BVVal VConst)\n\n -- variables\n progress (TAVar x\u2081) = abort (somenotnone (! x\u2081))\n\n -- lambdas\n progress (TALam wt) = V (BVVal VLam)\n\n -- applications\n progress (TAAp wt1 wt2)\n with progress wt1 | progress wt2\n -- if the left steps, the whole thing steps\n progress (TAAp wt1 wt2) | S (_ , Step x y z) | _ = S (_ , Step (FHAp1 x) y (FHAp1 z))\n -- if the left is indeterminate, step the right\n progress (TAAp wt1 wt2) | I i | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n -- if they're both indeterminate, step when the cast steps and indet otherwise\n progress (TAAp wt1 wt2) | I x | I x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | I x\u2082 | CIFACast (d' , \u03c41 , \u03c42 , \u03c43 , \u03c44 , refl , d'' , ne ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | I x\u2082 | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet x\u2082))\n progress (TAAp wt1 wt2) | I x | I x\u2082 | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet x\u2082))\n progress (TAAp wt1 wt2) | I x | I x\u2082 | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet x\u2082))\n progress (TAAp wt1 wt2) | I x | I x\u2082 | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet x\u2082))\n progress (TAAp wt1 wt2) | I x | I x\u2082 | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet x\u2082))\n -- if the left is indetermiante but the right is a value\n progress (TAAp wt1 wt2) | I x | V x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | V x\u2082 | CIFACast (d' , \u03c41 , \u03c42 , \u03c43 , \u03c44 , refl , d'' , ne ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | V x\u2082 | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed x\u2082))\n progress (TAAp wt1 wt2) | I x | V x\u2082 | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed x\u2082))\n progress (TAAp wt1 wt2) | I x | V x\u2082 | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed x\u2082))\n progress (TAAp wt1 wt2) | I x | V x\u2082 | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed x\u2082))\n progress (TAAp wt1 wt2) | I x | V x\u2082 | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed x\u2082))\n -- if the left is a boxed value, inspect the right\n progress (TAAp wt1 wt2) | V v | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n progress (TAAp wt1 wt2) | V v | I i\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (x , d' , refl , qq) = S (_ , Step FHOuter ITLam FHOuter)\n ... | Inr (d' , \u03c41' , \u03c42' , refl , neq , qq) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | V v | V v\u2082\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (x , d' , refl , qq) = S (_ , Step FHOuter ITLam FHOuter)\n ... | Inr (d' , \u03c41' , \u03c42' , refl , neq , qq) = S (_ , Step FHOuter ITApCast FHOuter)\n\n -- empty holes\n progress (TAEHole x x\u2081) = I IEHole\n\n -- nonempty holes\n progress (TANEHole xin wt x\u2081)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHNEHole x) y (FHNEHole z))\n ... | I x = I (INEHole (FIndet x))\n ... | V x = I (INEHole (FBoxed x))\n\n -- casts\n progress (TACast wt con)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHCast x) y (FHCast z))\n -- indet cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | I x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole1) | I x = I (ICastGroundHole {!!} x) -- cyrus\n progress (TACast wt TCHole2) | I x = I (ICastHoleGround {!!} x {!!}) -- cyrus\n progress (TACast wt (TCArr c1 c2)) | I x = I (ICastArr {!!} x) -- cyrus\n -- boxed value cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | V x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole1) | V x = V (BVHoleCast {!!} x) -- cyrus\n progress (TACast wt TCHole2) | V x = {!!} -- I {!!} -- cyrus: missing rule for boxed values?\n progress (TACast wt (TCArr c1 c2)) | V x = V (BVArrCast {!!} x) -- cyrus\n\n -- failed casts\n progress (TAFailedCast wt y z w)\n with progress wt\n progress (TAFailedCast wt y z w) | S (_ , Step x a q) = S (_ , Step {!!} (ITCastFail y z w) {!!} )\n progress (TAFailedCast wt y z w) | I x = I (IFailedCast (FIndet x) y z w)\n progress (TAFailedCast wt y z w) | V x = I (IFailedCast (FBoxed x) y z w)\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\n\nopen import canonical-boxed-forms\nopen import canonical-value-forms\nopen import canonical-indeterminate-forms\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d boxedval + d indet + \u03a3[ d' \u2208 dhexp ] (d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n V : \u2200{d \u0394} \u2192 d boxedval \u2192 ok d \u0394\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = V (BVVal VConst)\n\n -- variables\n progress (TAVar x\u2081) = abort (somenotnone (! x\u2081))\n\n -- lambdas\n progress (TALam wt) = V (BVVal VLam)\n\n -- applications\n progress (TAAp wt1 wt2)\n with progress wt1 | progress wt2\n -- if the left steps, the whole thing steps\n progress (TAAp wt1 wt2) | S (_ , Step x y z) | _ = S (_ , Step (FHAp1 x) y (FHAp1 z))\n -- if the left is indeterminate, step the right\n progress (TAAp wt1 wt2) | I i | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n -- if they're both indeterminate, step when the cast steps and indet otherwise\n progress (TAAp wt1 wt2) | I x | I x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | I x\u2082 | CIFACast (d' , \u03c41 , \u03c42 , \u03c43 , \u03c44 , refl , d'' , ne ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | I x\u2082 | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet x\u2082))\n progress (TAAp wt1 wt2) | I x | I x\u2082 | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet x\u2082))\n progress (TAAp wt1 wt2) | I x | I x\u2082 | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet x\u2082))\n progress (TAAp wt1 wt2) | I x | I x\u2082 | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet x\u2082))\n progress (TAAp wt1 wt2) | I x | I x\u2082 | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet x\u2082))\n -- if the left is indetermiante but the right is a value\n progress (TAAp wt1 wt2) | I x | V x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | V x\u2082 | CIFACast (d' , \u03c41 , \u03c42 , \u03c43 , \u03c44 , refl , d'' , ne ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | V x\u2082 | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed x\u2082))\n progress (TAAp wt1 wt2) | I x | V x\u2082 | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed x\u2082))\n progress (TAAp wt1 wt2) | I x | V x\u2082 | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed x\u2082))\n progress (TAAp wt1 wt2) | I x | V x\u2082 | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed x\u2082))\n progress (TAAp wt1 wt2) | I x | V x\u2082 | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxed x\u2082))\n -- if the left is a boxed value, inspect the right\n progress (TAAp wt1 wt2) | V v | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n progress (TAAp wt1 wt2) | V v | I i\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (x , d' , refl , qq) = S (_ , Step FHOuter ITLam FHOuter)\n ... | Inr (d' , \u03c41' , \u03c42' , refl , neq , qq) = I (IAp {!!} (ICastArr neq {!!}) (FIndet i)) --cyrus, as below\n progress (TAAp wt1 wt2) | V v | V v\u2082\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (x , d' , refl , qq) = S (_ , Step FHOuter ITLam FHOuter)\n ... | Inr (d' , \u03c41' , \u03c42' , refl , neq , qq) = I (IAp {!!} (ICastArr neq {!!}) (FBoxed v\u2082)) --cyrus\n\n -- empty holes\n progress (TAEHole x x\u2081) = I IEHole\n\n -- nonempty holes\n progress (TANEHole xin wt x\u2081)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHNEHole x) y (FHNEHole z))\n ... | I x = I (INEHole (FIndet x))\n ... | V x = I (INEHole (FBoxed x))\n\n -- casts\n progress (TACast wt con)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHCast x) y (FHCast z))\n -- indet cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | I x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole1) | I x = I (ICastGroundHole {!!} x) -- cyrus\n progress (TACast wt TCHole2) | I x = I (ICastHoleGround {!!} x {!!}) -- cyrus\n progress (TACast wt (TCArr c1 c2)) | I x = I (ICastArr {!!} x) -- cyrus\n -- boxed value cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | V x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole1) | V x = V (BVHoleCast {!!} x) -- cyrus\n progress (TACast wt TCHole2) | V x = V {!!} -- cyrus: missing rule for boxed values?\n progress (TACast wt (TCArr c1 c2)) | V x = V (BVArrCast {!!} x) -- cyrus\n\n -- failed casts\n progress (TAFailedCast wt y z w)\n with progress wt\n progress (TAFailedCast wt y z w) | S (_ , Step x a q) = S (_ , Step {!!} (ITCastFail y z w) {!!} )\n progress (TAFailedCast wt y z w) | I x = I (IFailedCast (FIndet x) y z w)\n progress (TAFailedCast wt y z w) | V x = I (IFailedCast (FBoxed x) y z w)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"343a2d1d11e329ca7267e1e3098547a18992bcc5","subject":"Fixed indentation.","message":"Fixed indentation.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Program\/Collatz\/Collatz.agda","new_file":"src\/fot\/FOTC\/Program\/Collatz\/Collatz.agda","new_contents":"------------------------------------------------------------------------------\n-- The Collatz function: A function without a termination proof\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Program.Collatz.Collatz where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Program.Collatz.Data.Nat\n\n------------------------------------------------------------------------------\n-- The Collatz function.\npostulate\n collatz : D \u2192 D\n collatz-0 : collatz [0] \u2261 [1]\n collatz-1 : collatz [1] \u2261 [1]\n collatz-even : \u2200 {n} \u2192 Even (succ\u2081 (succ\u2081 n)) \u2192\n collatz (succ\u2081 (succ\u2081 n)) \u2261\n collatz (div (succ\u2081 (succ\u2081 n)) [2])\n collatz-noteven : \u2200 {n} \u2192 NotEven (succ\u2081 (succ\u2081 n)) \u2192\n collatz (succ\u2081 (succ\u2081 n)) \u2261\n collatz ([3] * (succ\u2081 (succ\u2081 n)) + [1])\n{-# ATP axiom collatz-0 collatz-1 collatz-even collatz-noteven #-}\n","old_contents":"------------------------------------------------------------------------------\n-- The Collatz function: A function without a termination proof\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Program.Collatz.Collatz where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Program.Collatz.Data.Nat\n\n------------------------------------------------------------------------------\n-- The Collatz function.\npostulate\n collatz : D \u2192 D\n collatz-0 : collatz [0] \u2261 [1]\n collatz-1 : collatz [1] \u2261 [1]\n collatz-even : \u2200 {n} \u2192 Even (succ\u2081 (succ\u2081 n)) \u2192\n collatz (succ\u2081 (succ\u2081 n)) \u2261\n collatz (div (succ\u2081 (succ\u2081 n)) [2])\n collatz-noteven : \u2200 {n} \u2192 NotEven (succ\u2081 (succ\u2081 n)) \u2192\n collatz (succ\u2081 (succ\u2081 n)) \u2261\n collatz ([3] * (succ\u2081 (succ\u2081 n)) + [1])\n{-# ATP axiom collatz-0 collatz-1 collatz-even collatz-noteven #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"61d871d15a9844e258a74c2b3e37da6993ed116f","subject":"Instance arguments changed...","message":"Instance arguments changed...\n","repos":"np\/NomPa","old_file":"lib\/NomPa\/Examples\/LC\/SymanticsTy.agda","new_file":"lib\/NomPa\/Examples\/LC\/SymanticsTy.agda","new_contents":"open import Relation.Binary using (Reflexive; Transitive)\nopen import Function\nopen import Function.InstanceArguments\nopen import Data.Nat\nopen import Data.Unit using (\u22a4)\n-- open import Reflection.NP using (\u03b7\u207f)\nopen import Data.Product.NP\nopen import Category.Functor\n renaming (RawFunctor to Functor; module RawFunctor to Functor)\nopen import Category.Applicative\n renaming (RawApplicative to Applicative; module RawApplicative to Applicative)\n\nmodule NomPa.Examples.LC.SymanticsTy\n where\n\nrecord Box {a} (A : Set a) : Set a where\n constructor mk\n field get : A\n\nmodule DataFunctorReader {E} where\n Reader : Set \u2192 Set\n Reader A = E \u2192 A\n functor : Functor Reader\n functor = record { _<$>_ = \u03bb f g \u2192 f \u2218 g }\n applicative : Applicative Reader\n applicative = record { pure = const; _\u229b_ = _\u02e2_ }\n\nmodule BasicSyms (Repr : Set \u2192 Set) where\n record BaseArithSym : Set where\n constructor mk\n field\n nat : \u2115 \u2192 Repr \u2115\n add : Repr (\u2115 \u2192 \u2115 \u2192 \u2115)\n mul : Repr (\u2115 \u2192 \u2115 \u2192 \u2115)\n\n AppSym' : Set\u2081\n AppSym' = \u2200 {A B : Set} \u2192 Repr (A \u2192 B) \u2192 Repr A \u2192 Repr B\n\n AppSym : Set\u2081\n AppSym = Box AppSym'\n\n record SimpleSym : Set\u2081 where\n constructor mk\n infixl 2 _$$_\n field\n baseArithSym : BaseArithSym\n appSym : AppSym\n open BaseArithSym baseArithSym public\n _$$_ : AppSym'\n _$$_ = Box.get appSym\n _+:_ : Repr \u2115 \u2192 Repr \u2115 \u2192 Repr \u2115\n x +: y = add $$ x $$ y\n _*:_ : Repr \u2115 \u2192 Repr \u2115 \u2192 Repr \u2115\n x *: y = mul $$ x $$ y\n\n -- lamS in Staged Haskell\n LamPure : Set\u2081\n LamPure = Box (\u2200 {A B} \u2192 (Repr A \u2192 Repr B) \u2192 Repr (A \u2192 B))\n\n AssertPos : Set\u2081\n AssertPos = \u2200 {A} \u2192 Repr \u2115 \u2192 Repr A \u2192 Repr A\n\nmodule IdSyms where\n open BasicSyms id\n baseArithSym : BaseArithSym\n baseArithSym = record { nat = id; add = _+_; mul = _*_ }\n _$$_ : AppSym\n _$$_ = mk id\n simpleSym : SimpleSym\n simpleSym = record { baseArithSym = baseArithSym; appSym = _$$_ }\n \u019b\u1d3e : LamPure\n \u019b\u1d3e = mk id\n\nmodule ApplicativeSyms {M Repr : Set \u2192 Set} (M-app : Applicative M) where\n open BasicSyms\n open Applicative M-app\n\n instance\n baseArithSym : {{sym : BaseArithSym Repr}} \u2192 BaseArithSym (M \u2218 Repr)\n baseArithSym = record { nat = pure \u2218 nat; add = pure add; mul = pure mul } where\n open BaseArithSym _ \u2026\n\n appSym : {{sym : AppSym Repr}} \u2192 AppSym (M \u2218 Repr)\n appSym {{mk appSym'}} = mk \u03bb x y \u2192 pure appSym' \u229b x \u229b y\n\n simpleSym : {{sym : SimpleSym Repr}} \u2192 SimpleSym (M \u2218 Repr)\n simpleSym {{mk _ _}} = mk \u2026 appSym\n\n assertPos : {{_ : AssertPos Repr}} \u2192 AssertPos (M \u2218 Repr)\n assertPos {{assertPos\u2032}} n\u1d39 a\u1d39 = pure assertPos\u2032 \u229b n\u1d39 \u229b a\u1d39\n\nopen BasicSyms\n\nJ : (M Repr : Set \u2192 Set) (A : Set) \u2192 Set\nJ M Repr A = M (Repr A)\n\nHV : (H : Set) (Repr : Set \u2192 Set) (A : Set) \u2192 Set\n-- HV H = J (\u03bb B \u2192 H \u2192 B)\nHV H Repr A = H \u2192 Repr A\n\nmodule HVSyms {H Repr} = ApplicativeSyms {\u03bb B \u2192 H \u2192 B} {Repr} DataFunctorReader.applicative\n\nhmap : \u2200 {A H\u2081 H\u2082 Repr} \u2192 (H\u2082 \u2192 H\u2081) \u2192 HV H\u2081 Repr A \u2192 HV H\u2082 Repr A\nhmap f g = g \u2218 f\n\n-- called runH in Staged Haskell\nrunHV : \u2200 {Repr A} \u2192 HV \u22a4 Repr A \u2192 Repr A\nrunHV m = m _\n\nrecord Lib : Set\u2081 where\n constructor mk\n field\n HA : (Repr : Set \u2192 Set) (S A : Set) \u2192 Set\n\n LamM : Set\u2081\n LamM = \u2200 {M Repr : Set \u2192 Set}{A B H} {{funM : Functor M}} {{sSym : SimpleSym Repr}}\n {{\u019b\u1d3e : LamPure Repr}} \u2192 (\u2200 {S} \u2192 HV (HA Repr S A \u00d7 H) Repr A \u2192 M (HV (HA Repr S A \u00d7 H) Repr B))\n \u2192 M (HV H Repr (A \u2192 B))\n\n field\n lam : LamM\n href : \u2200 {Repr S A H} \u2192 HV (HA Repr S A \u00d7 H) Repr A\n var : \u2200 {H M Repr A} {{M-app : Applicative M}} \u2192 HV H Repr A \u2192 M (HV H Repr A)\n weaken : \u2200 {H H\u2032 A : Set} {M Repr : Set \u2192 Set} {{M-app : Functor M}} \u2192 M (HV H Repr A) \u2192 M (HV (H\u2032 \u00d7 H) Repr A)\n\nmodule M2\n (Cst : Set)\n (Env : Set)\n -- (_\u21911 : Env \u2192 Env)\n (Binder : Set)\n (Ty : Set)\n (_,\u27e8_\u2236_\u27e9 : Env \u2192 Binder \u2192 Ty \u2192 Env)\n (_#_ : Binder \u2192 Env \u2192 Set)\n (_\u21911\u27e8_\u27e9 : Env \u2192 Ty \u2192 Env)\n (_\u2286_ : Env \u2192 Env \u2192 Set)\n (_\u21d2_ : Ty \u2192 Ty \u2192 Ty)\n (\u03b5 : Env)\n (\u2286-\u03b5 : \u2200 {\u0393} \u2192 \u03b5 \u2286 \u0393)\n (\u2286-# : \u2200 {\u0393 b \u03c4} \u2192 b # \u0393 \u2192 \u0393 \u2286 \u0393 ,\u27e8 b \u2236 \u03c4 \u27e9)\n (\u2286-cong-,\u1d30 : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u2286 \u0393\u2082 \u2192 \u0393\u2081 \u21911\u27e8 \u03c4 \u27e9 \u2286 \u0393\u2082 \u21911\u27e8 \u03c4 \u27e9)\n (\u2286-trans : Transitive _\u2286_)\n (_+1 : Env \u2192 Env)\n (\u2286-+1\u21911 : \u2200 {\u0393 \u03c4} \u2192 \u0393 +1 \u2286 \u0393 \u21911\u27e8 \u03c4 \u27e9)\n (_\u22a2_ : Env \u2192 Ty \u2192 Set) where\n\n record Sym : Set where\n infixl 6 _\u00b7_\n\n field\n _\u00b7_ : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : \u0393 \u22a2 (\u03c3 \u21d2 \u03c4)) (u : \u0393 \u22a2 \u03c3) \u2192 \u0393 \u22a2 \u03c4\n \u019b\u1d3a : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (f : \u2200 {b} \u2192 b # \u0393 \u2192 \u0393 ,\u27e8 b \u2236 \u03c3 \u27e9 \u22a2 \u03c3 \u2192 \u0393 ,\u27e8 b \u2236 \u03c3 \u27e9 \u22a2 \u03c4) \u2192 \u0393 \u22a2 (\u03c3 \u21d2 \u03c4)\n \u019b\u1d30 : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (f : \u0393 \u21911\u27e8 \u03c3 \u27e9 \u22a2 \u03c3 \u2192 \u0393 \u21911\u27e8 \u03c3 \u27e9 \u22a2 \u03c4) \u2192 \u0393 \u22a2 (\u03c3 \u21d2 \u03c4)\n -- Let : \u2200 {\u03b1} \u2192 (t : Tm \u03b1) (u : Tm (\u03b1 \u21911) \u2192 Tm (\u03b1 \u21911)) \u2192 Tm \u03b1\n -- \u019b : \u2200 {\u03b1} \u2192 (t : \u2200 {b} \u2192 Tm (b \u25c5 \u03b1) \u2192 Tm (b \u25c5 \u03b1)) \u2192 Tm \u03b1\n -- Let : \u2200 {\u03b1} \u2192 (t : Tm \u03b1) (u : \u2200 {b} \u2192 Tm (b \u25c5 \u03b1) \u2192 Tm (b \u25c5 \u03b1)) \u2192 Tm \u03b1\n -- `_ : \u2200 {\u03b1} \u2192 (\u03ba : Cst) \u2192 Tm \u03b1\n\n coerce\u2122 : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u2286 \u0393\u2082 \u2192 \u0393\u2081 \u22a2 \u03c4 \u2192 \u0393\u2082 \u22a2 \u03c4\n shift\u2122 : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 +1 \u2286 \u0393\u2082 \u2192 \u0393\u2081 \u22a2 \u03c4 \u2192 \u0393\u2082 \u22a2 \u03c4\n\n weaken\u2122 : \u2200 {\u0393 \u03c3 \u03c4} \u2192 \u0393 \u22a2 \u03c4 \u2192 \u0393 \u21911\u27e8 \u03c3 \u27e9 \u22a2 \u03c4\n weaken\u2122 = shift\u2122 \u2286-+1\u21911\n\n\n{-\n \u2286-cong-\u21911 : \u2200 {\u03b1 \u03b2}\n (\u03b1\u2286\u03b2 : \u03b1 \u2286 \u03b2)\n \u2192 \u03b1 \u21911 \u2286 \u03b2 \u21911\n-}\n\n module Examples\u1d3a (sym : Sym) where\n open Sym sym\n\n id\u2122 : \u2200 {\u03c4} \u2192 \u03b5 \u22a2 (\u03c4 \u21d2 \u03c4)\n id\u2122 = \u019b\u1d3a (\u03bb _ x \u2192 x)\n\n true\u2122 : \u2200 {\u03c3 \u03c4} \u2192 \u03b5 \u22a2 (\u03c4 \u21d2 (\u03c3 \u21d2 \u03c4))\n true\u2122 = \u019b\u1d3a (\u03bb _ x \u2192 \u019b\u1d3a (\u03bb y# y \u2192 coerce\u2122 (\u2286-# y#) x))\n\n false\u2122 : \u2200 {\u03c3 \u03c4} \u2192 \u03b5 \u22a2 (\u03c3 \u21d2 (\u03c4 \u21d2 \u03c4))\n false\u2122 = \u019b\u1d3a (\u03bb _ x \u2192 \u019b\u1d3a (\u03bb _ y \u2192 y))\n\n module Examples\u1d30 (sym : Sym) where\n open Sym sym\n\n id\u2122 : \u2200 {\u03c4} \u2192 \u03b5 \u22a2 (\u03c4 \u21d2 \u03c4)\n id\u2122 = \u019b\u1d30 (\u03bb x \u2192 x)\n\n true\u2122 : \u2200 {\u03c3 \u03c4} \u2192 \u03b5 \u22a2 (\u03c4 \u21d2 (\u03c3 \u21d2 \u03c4))\n true\u2122 = \u019b\u1d30 (\u03bb x \u2192 \u019b\u1d30 (\u03bb y \u2192 weaken\u2122 x))\n\n false\u2122 : \u2200 {\u03c3 \u03c4} \u2192 \u03b5 \u22a2 (\u03c3 \u21d2 (\u03c4 \u21d2 \u03c4))\n false\u2122 = \u019b\u1d30 (\u03bb x \u2192 \u019b\u1d30 (\u03bb y \u2192 y))\n\nmodule M3 where\n -- open module F {M : Set \u2192 Set} {{x}} = Functor {_} {M} x\n -- open module A {M : Set \u2192 Set} {{x}} = Applicative {_} {M} x\n\n record HA (Repr : Set \u2192 Set) (S A : Set) : Set where\n constructor mk\n field\n ha : Repr A\n\n href : \u2200 {Repr S A H} \u2192 HV (HA Repr S A \u00d7 H) Repr A \n href (mk x , h) = x \n\n -- dup\n LamM : Set\u2081\n LamM = \u2200 {M Repr : Set \u2192 Set}{A B H} {{M-fun : Functor M}} {{sSym : SimpleSym Repr}}\n {{\u019b\u1d3e : LamPure Repr}} \u2192 (\u2200 {S} \u2192 HV (HA Repr S A \u00d7 H) Repr A \u2192 M (HV (HA Repr S A \u00d7 H) Repr B))\n \u2192 M (HV H Repr (A \u2192 B))\n\n lam : LamM\n lam {{M-fun}} {{sym}} {{mk \u019b\u1d3e}} f = _<$>_ (\u03bb body h \u2192 \u019b\u1d3e (\u03bb x \u2192 body (mk x , h))) (f {\u22a4} href) where\n open SimpleSym _ sym\n open Functor M-fun\n\n var : \u2200 {H M Repr A} {{M-app : Applicative M}} \u2192 HV H Repr A \u2192 M (HV H Repr A)\n var = Applicative.pure \u2026\n\n weaken : \u2200 {H H\u2032 A : Set} {M Repr : Set \u2192 Set} {{M-app : Functor M}} \u2192 M (HV H Repr A) \u2192 M (HV (H\u2032 \u00d7 H) Repr A)\n weaken = Functor._<$>_ \u2026 (\u03bb g \u2192 g \u2218 snd) -- hmap goes yellow\n\n lib : Lib\n lib = mk HA lam href\n (\u03bb {\u03b7\u2081} {\u03b7\u2082} {\u03b7\u2083} {\u03b7\u2084} \u2192 var {\u03b7\u2081} {\u03b7\u2082} {\u03b7\u2083} {\u03b7\u2084}) -- FIXME: unquote (\u03b7\u207f (quote var))\n (\u03bb {\u03b7\u2081} {\u03b7\u2082} {\u03b7\u2083} {\u03b7\u2084} {\u03b7\u2085} \u2192 weaken {\u03b7\u2081} {\u03b7\u2082} {\u03b7\u2083} {\u03b7\u2084} {\u03b7\u2085})\n","old_contents":"open import Relation.Binary using (Reflexive; Transitive)\nopen import Function\nopen import Function.InstanceArguments\nopen import Data.Nat\nopen import Data.Unit using (\u22a4)\n-- open import Reflection.NP using (\u03b7\u207f)\nopen import Data.Product.NP\nopen import Category.Functor\n renaming (RawFunctor to Functor; module RawFunctor to Functor)\nopen import Category.Applicative\n renaming (RawApplicative to Applicative; module RawApplicative to Applicative)\n\nmodule NomPa.Examples.LC.SymanticsTy\n where\n\nmodule DataFunctorReader {E} where\n Reader : Set \u2192 Set\n Reader A = E \u2192 A\n functor : Functor Reader\n functor = record { _<$>_ = \u03bb f g \u2192 f \u2218 g }\n applicative : Applicative Reader\n applicative = record { pure = const; _\u229b_ = _\u02e2_ }\n\nmodule BasicSyms (Repr : Set \u2192 Set) where\n record BaseArithSym : Set where\n constructor mk\n field\n nat : \u2115 \u2192 Repr \u2115\n add : Repr (\u2115 \u2192 \u2115 \u2192 \u2115)\n mul : Repr (\u2115 \u2192 \u2115 \u2192 \u2115)\n\n AppSym : Set\u2081\n AppSym = \u2200 {A B : Set} \u2192 Repr (A \u2192 B) \u2192 Repr A \u2192 Repr B\n\n record SimpleSym : Set\u2081 where\n constructor mk\n infixl 2 _$$_\n field\n baseArithSym : BaseArithSym\n _$$_ : AppSym\n open BaseArithSym baseArithSym public\n _+:_ : Repr \u2115 \u2192 Repr \u2115 \u2192 Repr \u2115\n x +: y = add $$ x $$ y\n _*:_ : Repr \u2115 \u2192 Repr \u2115 \u2192 Repr \u2115\n x *: y = mul $$ x $$ y\n\n -- lamS in Staged Haskell\n LamPure : Set\u2081\n LamPure = \u2200 {A B} \u2192 (Repr A \u2192 Repr B) \u2192 Repr (A \u2192 B)\n\n AssertPos : Set\u2081\n AssertPos = \u2200 {A} \u2192 Repr \u2115 \u2192 Repr A \u2192 Repr A\n\nmodule IdSyms where\n open BasicSyms id\n baseArithSym : BaseArithSym\n baseArithSym = record { nat = id; add = _+_; mul = _*_ }\n _$$_ : AppSym\n _$$_ = id\n simpleSym : SimpleSym\n simpleSym = record { baseArithSym = baseArithSym; _$$_ = _$$_ }\n \u019b\u1d3e : LamPure\n \u019b\u1d3e = id\n\nmodule ApplicativeSyms {M Repr : Set \u2192 Set} (M-app : Applicative M) where\n open BasicSyms\n open Applicative M-app\n\n baseArithSym : {{sym : BaseArithSym Repr}} \u2192 BaseArithSym (M \u2218 Repr)\n baseArithSym = record { nat = pure \u2218 nat; add = pure add; mul = pure mul } where\n open BaseArithSym _ \u2026\n\n _$$_ : {{sym : AppSym Repr}} \u2192 AppSym (M \u2218 Repr)\n _$$_ {{_$$\u2032_}} x y = pure _$$\u2032_ \u229b x \u229b y\n\n simpleSym : {{sym : SimpleSym Repr}} \u2192 SimpleSym (M \u2218 Repr)\n simpleSym {{mk _ _}} = mk baseArithSym _$$_\n\n assertPos : {{_ : AssertPos Repr}} \u2192 AssertPos (M \u2218 Repr)\n assertPos {{assertPos\u2032}} n\u1d39 a\u1d39 = pure assertPos\u2032 \u229b n\u1d39 \u229b a\u1d39\n\nopen BasicSyms\n\nJ : (M Repr : Set \u2192 Set) (A : Set) \u2192 Set\nJ M Repr A = M (Repr A)\n\nHV : (H : Set) (Repr : Set \u2192 Set) (A : Set) \u2192 Set\n-- HV H = J (\u03bb B \u2192 H \u2192 B)\nHV H Repr A = H \u2192 Repr A\n\nmodule HVSyms {H Repr} = ApplicativeSyms {\u03bb B \u2192 H \u2192 B} {Repr} DataFunctorReader.applicative\n\nhmap : \u2200 {A H\u2081 H\u2082 Repr} \u2192 (H\u2082 \u2192 H\u2081) \u2192 HV H\u2081 Repr A \u2192 HV H\u2082 Repr A\nhmap f g = g \u2218 f\n\n-- called runH in Staged Haskell\nrunHV : \u2200 {Repr A} \u2192 HV \u22a4 Repr A \u2192 Repr A\nrunHV m = m _\n\nrecord Lib : Set\u2081 where\n constructor mk\n field\n HA : (Repr : Set \u2192 Set) (S A : Set) \u2192 Set\n\n LamM : Set\u2081\n LamM = \u2200 {M Repr : Set \u2192 Set}{A B H} {{funM : Functor M}} {{sSym : SimpleSym Repr}}\n {{\u019b\u1d3e : LamPure Repr}} \u2192 (\u2200 {S} \u2192 HV (HA Repr S A \u00d7 H) Repr A \u2192 M (HV (HA Repr S A \u00d7 H) Repr B))\n \u2192 M (HV H Repr (A \u2192 B))\n\n field\n lam : LamM\n href : \u2200 {Repr S A H} \u2192 HV (HA Repr S A \u00d7 H) Repr A\n var : \u2200 {H M Repr A} {{M-app : Applicative M}} \u2192 HV H Repr A \u2192 M (HV H Repr A)\n weaken : \u2200 {H H\u2032 A : Set} {M Repr : Set \u2192 Set} {{M-app : Functor M}} \u2192 M (HV H Repr A) \u2192 M (HV (H\u2032 \u00d7 H) Repr A)\n\nmodule M2\n (Cst : Set)\n (Env : Set)\n -- (_\u21911 : Env \u2192 Env)\n (Binder : Set)\n (Ty : Set)\n (_,\u27e8_\u2236_\u27e9 : Env \u2192 Binder \u2192 Ty \u2192 Env)\n (_#_ : Binder \u2192 Env \u2192 Set)\n (_\u21911\u27e8_\u27e9 : Env \u2192 Ty \u2192 Env)\n (_\u2286_ : Env \u2192 Env \u2192 Set)\n (_\u21d2_ : Ty \u2192 Ty \u2192 Ty)\n (\u03b5 : Env)\n (\u2286-\u03b5 : \u2200 {\u0393} \u2192 \u03b5 \u2286 \u0393)\n (\u2286-# : \u2200 {\u0393 b \u03c4} \u2192 b # \u0393 \u2192 \u0393 \u2286 \u0393 ,\u27e8 b \u2236 \u03c4 \u27e9)\n (\u2286-cong-,\u1d30 : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u2286 \u0393\u2082 \u2192 \u0393\u2081 \u21911\u27e8 \u03c4 \u27e9 \u2286 \u0393\u2082 \u21911\u27e8 \u03c4 \u27e9)\n (\u2286-trans : Transitive _\u2286_)\n (_+1 : Env \u2192 Env)\n (\u2286-+1\u21911 : \u2200 {\u0393 \u03c4} \u2192 \u0393 +1 \u2286 \u0393 \u21911\u27e8 \u03c4 \u27e9)\n (_\u22a2_ : Env \u2192 Ty \u2192 Set) where\n\n record Sym : Set where\n infixl 6 _\u00b7_\n\n field\n _\u00b7_ : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : \u0393 \u22a2 (\u03c3 \u21d2 \u03c4)) (u : \u0393 \u22a2 \u03c3) \u2192 \u0393 \u22a2 \u03c4\n \u019b\u1d3a : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (f : \u2200 {b} \u2192 b # \u0393 \u2192 \u0393 ,\u27e8 b \u2236 \u03c3 \u27e9 \u22a2 \u03c3 \u2192 \u0393 ,\u27e8 b \u2236 \u03c3 \u27e9 \u22a2 \u03c4) \u2192 \u0393 \u22a2 (\u03c3 \u21d2 \u03c4)\n \u019b\u1d30 : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (f : \u0393 \u21911\u27e8 \u03c3 \u27e9 \u22a2 \u03c3 \u2192 \u0393 \u21911\u27e8 \u03c3 \u27e9 \u22a2 \u03c4) \u2192 \u0393 \u22a2 (\u03c3 \u21d2 \u03c4)\n -- Let : \u2200 {\u03b1} \u2192 (t : Tm \u03b1) (u : Tm (\u03b1 \u21911) \u2192 Tm (\u03b1 \u21911)) \u2192 Tm \u03b1\n -- \u019b : \u2200 {\u03b1} \u2192 (t : \u2200 {b} \u2192 Tm (b \u25c5 \u03b1) \u2192 Tm (b \u25c5 \u03b1)) \u2192 Tm \u03b1\n -- Let : \u2200 {\u03b1} \u2192 (t : Tm \u03b1) (u : \u2200 {b} \u2192 Tm (b \u25c5 \u03b1) \u2192 Tm (b \u25c5 \u03b1)) \u2192 Tm \u03b1\n -- `_ : \u2200 {\u03b1} \u2192 (\u03ba : Cst) \u2192 Tm \u03b1\n\n coerce\u2122 : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u2286 \u0393\u2082 \u2192 \u0393\u2081 \u22a2 \u03c4 \u2192 \u0393\u2082 \u22a2 \u03c4\n shift\u2122 : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 +1 \u2286 \u0393\u2082 \u2192 \u0393\u2081 \u22a2 \u03c4 \u2192 \u0393\u2082 \u22a2 \u03c4\n\n weaken\u2122 : \u2200 {\u0393 \u03c3 \u03c4} \u2192 \u0393 \u22a2 \u03c4 \u2192 \u0393 \u21911\u27e8 \u03c3 \u27e9 \u22a2 \u03c4\n weaken\u2122 = shift\u2122 \u2286-+1\u21911\n\n\n{-\n \u2286-cong-\u21911 : \u2200 {\u03b1 \u03b2}\n (\u03b1\u2286\u03b2 : \u03b1 \u2286 \u03b2)\n \u2192 \u03b1 \u21911 \u2286 \u03b2 \u21911\n-}\n\n module Examples\u1d3a (sym : Sym) where\n open Sym sym\n\n id\u2122 : \u2200 {\u03c4} \u2192 \u03b5 \u22a2 (\u03c4 \u21d2 \u03c4)\n id\u2122 = \u019b\u1d3a (\u03bb _ x \u2192 x)\n\n true\u2122 : \u2200 {\u03c3 \u03c4} \u2192 \u03b5 \u22a2 (\u03c4 \u21d2 (\u03c3 \u21d2 \u03c4))\n true\u2122 = \u019b\u1d3a (\u03bb _ x \u2192 \u019b\u1d3a (\u03bb y# y \u2192 coerce\u2122 (\u2286-# y#) x))\n\n false\u2122 : \u2200 {\u03c3 \u03c4} \u2192 \u03b5 \u22a2 (\u03c3 \u21d2 (\u03c4 \u21d2 \u03c4))\n false\u2122 = \u019b\u1d3a (\u03bb _ x \u2192 \u019b\u1d3a (\u03bb _ y \u2192 y))\n\n module Examples\u1d30 (sym : Sym) where\n open Sym sym\n\n id\u2122 : \u2200 {\u03c4} \u2192 \u03b5 \u22a2 (\u03c4 \u21d2 \u03c4)\n id\u2122 = \u019b\u1d30 (\u03bb x \u2192 x)\n\n true\u2122 : \u2200 {\u03c3 \u03c4} \u2192 \u03b5 \u22a2 (\u03c4 \u21d2 (\u03c3 \u21d2 \u03c4))\n true\u2122 = \u019b\u1d30 (\u03bb x \u2192 \u019b\u1d30 (\u03bb y \u2192 weaken\u2122 x))\n\n false\u2122 : \u2200 {\u03c3 \u03c4} \u2192 \u03b5 \u22a2 (\u03c3 \u21d2 (\u03c4 \u21d2 \u03c4))\n false\u2122 = \u019b\u1d30 (\u03bb x \u2192 \u019b\u1d30 (\u03bb y \u2192 y))\n\nmodule M3 where\n -- open module F {M : Set \u2192 Set} {{x}} = Functor {_} {M} x\n -- open module A {M : Set \u2192 Set} {{x}} = Applicative {_} {M} x\n\n record HA (Repr : Set \u2192 Set) (S A : Set) : Set where\n constructor mk\n field\n ha : Repr A\n\n href : \u2200 {Repr S A H} \u2192 HV (HA Repr S A \u00d7 H) Repr A \n href (mk x , h) = x \n\n -- dup\n LamM : Set\u2081\n LamM = \u2200 {M Repr : Set \u2192 Set}{A B H} {{M-fun : Functor M}} {{sSym : SimpleSym Repr}}\n {{\u019b\u1d3e : LamPure Repr}} \u2192 (\u2200 {S} \u2192 HV (HA Repr S A \u00d7 H) Repr A \u2192 M (HV (HA Repr S A \u00d7 H) Repr B))\n \u2192 M (HV H Repr (A \u2192 B))\n\n lam : LamM\n lam {{M-fun}} {{sym}} {{\u019b\u1d3e}} f = _<$>_ (\u03bb body h \u2192 \u019b\u1d3e (\u03bb x \u2192 body (mk x , h))) (f {\u22a4} href) where\n open SimpleSym _ sym\n open Functor M-fun\n\n var : \u2200 {H M Repr A} {{M-app : Applicative M}} \u2192 HV H Repr A \u2192 M (HV H Repr A)\n var = Applicative.pure \u2026\n\n weaken : \u2200 {H H\u2032 A : Set} {M Repr : Set \u2192 Set} {{M-app : Functor M}} \u2192 M (HV H Repr A) \u2192 M (HV (H\u2032 \u00d7 H) Repr A)\n weaken = Functor._<$>_ \u2026 (\u03bb g \u2192 g \u2218 snd) -- hmap goes yellow\n\n lib : Lib\n lib = mk HA lam href\n (\u03bb {\u03b7\u2081} {\u03b7\u2082} {\u03b7\u2083} {\u03b7\u2084} \u2192 var {\u03b7\u2081} {\u03b7\u2082} {\u03b7\u2083} {\u03b7\u2084}) -- FIXME: unquote (\u03b7\u207f (quote var))\n (\u03bb {\u03b7\u2081} {\u03b7\u2082} {\u03b7\u2083} {\u03b7\u2084} {\u03b7\u2085} \u2192 weaken {\u03b7\u2081} {\u03b7\u2082} {\u03b7\u2083} {\u03b7\u2084} {\u03b7\u2085})\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"27d30eeac98b6cb21e267cddc3cc7b991fb3a144","subject":"pisearch: \u03a0 is in Function.NP","message":"pisearch: \u03a0 is in Function.NP\n","repos":"crypto-agda\/crypto-agda","old_file":"pisearch.agda","new_file":"pisearch.agda","new_contents":"{-# OPTIONS --type-in-type #-}\nmodule pisearch where\nopen import Type hiding (\u2605_)\nopen import Function.NP\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Bool.NP\nopen import Search.Type\nopen import Search.Searchable.Product\nopen import Search.Searchable\nopen import sum\n\nTree : \u2200 {A} \u2192 Search A \u2192 (A \u2192 \u2605) \u2192 \u2605\nTree sA B = sA _\u00d7_ B\n\nToFun : \u2200 {A} (sA : Search A) \u2192 \u2605\nToFun {A} sA = \u2200 {B} \u2192 Tree sA B \u2192 \u03a0 A B\n\nFromFun : \u2200 {A} (sA : Search A) \u2192 \u2605\nFromFun {A} sA = \u2200 {B} \u2192 \u03a0 A B \u2192 Tree sA B\n\nfromFun-searchInd : \u2200 {A} {sA : Search A} \u2192 SearchInd sA \u2192 FromFun sA\nfromFun-searchInd indA = indA (\u03bb s \u2192 Tree s _) _,_\n\ntoFun-\u03a3 : \u2200 {A} {B : A \u2192 \u2605} (sA : Search A) (sB : \u2200 {x} \u2192 Search (B x))\n \u2192 ToFun sA\n \u2192 (\u2200 {x} \u2192 ToFun (sB {x}))\n \u2192 ToFun (\u03a3Search sA (\u03bb {x} \u2192 sB {x}))\ntoFun-\u03a3 _ _ toFunA toFunB t = uncurry (toFunB \u2218 toFunA t)\n\nfromFun-\u03a3 : \u2200 {A} {B : A \u2192 \u2605} (sA : Search A) (sB : \u2200 {x} \u2192 Search (B x))\n \u2192 FromFun sA\n \u2192 (\u2200 {x} \u2192 FromFun (sB {x}))\n \u2192 FromFun (\u03a3Search sA (\u03bb {x} \u2192 sB {x}))\nfromFun-\u03a3 _ _ fromFunA fromFunB f = fromFunA (fromFunB \u2218 curry f)\n\nopen import Relation.Binary.PropositionalEquality\nToFrom : \u2200 {A} (sA : Search A)\n (toFunA : ToFun sA)\n (fromFunA : FromFun sA)\n \u2192 \u2605\nToFrom {A} sA toFunA fromFunA = \u2200 {B} (f : \u03a0 A B) x \u2192 toFunA (fromFunA f) x \u2261 f x\n\nFromTo : \u2200 {A} (sA : Search A)\n (toFunA : ToFun sA)\n (fromFunA : FromFun sA)\n \u2192 \u2605\nFromTo sA toFunA fromFunA = \u2200 {B} (t : Tree sA B) \u2192 fromFunA (toFunA t) \u2261 t\n\nmodule \u03a3-props {A} {B : A \u2192 \u2605}\n (\u03bcA : Searchable A) (\u03bcB : \u2200 {x} \u2192 Searchable (B x)) where\n sA = search \u03bcA\n sB : \u2200 {x} \u2192 Search (B x)\n sB {x} = search (\u03bcB {x})\n fromFunA : FromFun sA\n fromFunA = fromFun-searchInd (search-ind \u03bcA)\n fromFunB : \u2200 {x} \u2192 FromFun (sB {x})\n fromFunB {x} = fromFun-searchInd (search-ind (\u03bcB {x}))\n module ToFrom\n (toFunA : ToFun sA)\n (toFunB : \u2200 {x} \u2192 ToFun (sB {x}))\n (toFromA : ToFrom sA toFunA fromFunA)\n (toFromB : \u2200 {x} \u2192 ToFrom (sB {x}) toFunB fromFunB) where\n toFrom-\u03a3 : ToFrom (\u03a3Search sA (\u03bb {x} \u2192 sB {x})) (toFun-\u03a3 sA sB toFunA toFunB) (fromFun-\u03a3 sA sB fromFunA fromFunB)\n toFrom-\u03a3 f (x , y) rewrite toFromA (fromFunB \u2218 curry f) x = toFromB (curry f x) y\n\n {- we need a search-ind-ext...\n module FromTo\n (toFunA : ToFun sA)\n (toFunB : \u2200 {x} \u2192 ToFun (sB {x}))\n (toFromA : FromTo sA toFunA fromFunA)\n (toFromB : \u2200 {x} \u2192 FromTo (sB {x}) toFunB fromFunB) where\n toFrom-\u03a3 : FromTo (\u03a3Search sA (\u03bb {x} \u2192 sB {x})) (toFun-\u03a3 sA sB toFunA toFunB) (fromFun-\u03a3 sA sB fromFunA fromFunB)\n toFrom-\u03a3 t = {!toFromA!} -- {!(\u03bb x \u2192 toFromB (toFunA t x))!}\n -}\n\ntoFun-\u00d7 : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 ToFun sA \u2192 ToFun sB \u2192 ToFun (sA \u00d7Search sB)\ntoFun-\u00d7 sA sB toFunA toFunB = toFun-\u03a3 sA sB toFunA toFunB\n\nfromFun-\u00d7 : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 FromFun sA \u2192 FromFun sB \u2192 FromFun (sA \u00d7Search sB)\nfromFun-\u00d7 sA sB fromFunA fromFunB = fromFun-\u03a3 sA sB fromFunA fromFunB\n\ntoFun-Bit : ToFun (search \u03bcBit)\ntoFun-Bit (f , t) false = f\ntoFun-Bit (f , t) true = t\n\nfromFun-Bit : FromFun (search \u03bcBit)\nfromFun-Bit f = f false , f true\n\ntoFun-\u228e : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 ToFun sA \u2192 ToFun sB \u2192 ToFun (sA +Search sB)\ntoFun-\u228e sA sB toFunA toFunB (t , u) (inj\u2081 x) = toFunA t x\ntoFun-\u228e sA sB toFunA toFunB (t , u) (inj\u2082 x) = toFunB u x\n\nfromFun-\u228e : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 FromFun sA \u2192 FromFun sB \u2192 FromFun (sA +Search sB)\nfromFun-\u228e sA sB fromFunA fromFunB f = fromFunA (f \u2218 inj\u2081) , fromFunB (f \u2218 inj\u2082)\n\n-- toFun-searchInd : \u2200 {A} {sA : Search A} \u2192 SearchInd sA \u2192 ToFun sA\n-- toFun-searchInd {A} {sA} indA {B} t = ?\n","old_contents":"{-# OPTIONS --type-in-type #-}\nmodule pisearch where\nopen import Type hiding (\u2605_)\nopen import Function\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Bool.NP\nopen import Search.Type\nopen import Search.Searchable.Product\nopen import Search.Searchable\nopen import sum\n\nTree : \u2200 {A} \u2192 Search A \u2192 (A \u2192 \u2605) \u2192 \u2605\nTree sA B = sA _\u00d7_ B\n\n\u03a0 : (A : \u2605) \u2192 (B : A \u2192 \u2605) \u2192 \u2605\n\u03a0 A B = (x : A) \u2192 B x\n\nToFun : \u2200 {A} (sA : Search A) \u2192 \u2605\nToFun {A} sA = \u2200 {B} \u2192 Tree sA B \u2192 \u03a0 A B\n\nFromFun : \u2200 {A} (sA : Search A) \u2192 \u2605\nFromFun {A} sA = \u2200 {B} \u2192 \u03a0 A B \u2192 Tree sA B\n\nfromFun-searchInd : \u2200 {A} {sA : Search A} \u2192 SearchInd sA \u2192 FromFun sA\nfromFun-searchInd indA = indA (\u03bb s \u2192 Tree s _) _,_\n\ntoFun-\u03a3 : \u2200 {A} {B : A \u2192 \u2605} (sA : Search A) (sB : \u2200 {x} \u2192 Search (B x))\n \u2192 ToFun sA\n \u2192 (\u2200 {x} \u2192 ToFun (sB {x}))\n \u2192 ToFun (\u03a3Search sA (\u03bb {x} \u2192 sB {x}))\ntoFun-\u03a3 _ _ toFunA toFunB t = uncurry (toFunB \u2218 toFunA t)\n\nfromFun-\u03a3 : \u2200 {A} {B : A \u2192 \u2605} (sA : Search A) (sB : \u2200 {x} \u2192 Search (B x))\n \u2192 FromFun sA\n \u2192 (\u2200 {x} \u2192 FromFun (sB {x}))\n \u2192 FromFun (\u03a3Search sA (\u03bb {x} \u2192 sB {x}))\nfromFun-\u03a3 _ _ fromFunA fromFunB f = fromFunA (fromFunB \u2218 curry f)\n\nopen import Relation.Binary.PropositionalEquality\nToFrom : \u2200 {A} (sA : Search A)\n (toFunA : ToFun sA)\n (fromFunA : FromFun sA)\n \u2192 \u2605\nToFrom {A} sA toFunA fromFunA = \u2200 {B} (f : \u03a0 A B) x \u2192 toFunA (fromFunA f) x \u2261 f x\n\nFromTo : \u2200 {A} (sA : Search A)\n (toFunA : ToFun sA)\n (fromFunA : FromFun sA)\n \u2192 \u2605\nFromTo sA toFunA fromFunA = \u2200 {B} (t : Tree sA B) \u2192 fromFunA (toFunA t) \u2261 t\n\nmodule \u03a3-props {A} {B : A \u2192 \u2605}\n (\u03bcA : Searchable A) (\u03bcB : \u2200 {x} \u2192 Searchable (B x)) where\n sA = search \u03bcA\n sB : \u2200 {x} \u2192 Search (B x)\n sB {x} = search (\u03bcB {x})\n fromFunA : FromFun sA\n fromFunA = fromFun-searchInd (search-ind \u03bcA)\n fromFunB : \u2200 {x} \u2192 FromFun (sB {x})\n fromFunB {x} = fromFun-searchInd (search-ind (\u03bcB {x}))\n module ToFrom\n (toFunA : ToFun sA)\n (toFunB : \u2200 {x} \u2192 ToFun (sB {x}))\n (toFromA : ToFrom sA toFunA fromFunA)\n (toFromB : \u2200 {x} \u2192 ToFrom (sB {x}) toFunB fromFunB) where\n toFrom-\u03a3 : ToFrom (\u03a3Search sA (\u03bb {x} \u2192 sB {x})) (toFun-\u03a3 sA sB toFunA toFunB) (fromFun-\u03a3 sA sB fromFunA fromFunB)\n toFrom-\u03a3 f (x , y) rewrite toFromA (fromFunB \u2218 curry f) x = toFromB (curry f x) y\n\n {- we need a search-ind-ext...\n module FromTo\n (toFunA : ToFun sA)\n (toFunB : \u2200 {x} \u2192 ToFun (sB {x}))\n (toFromA : FromTo sA toFunA fromFunA)\n (toFromB : \u2200 {x} \u2192 FromTo (sB {x}) toFunB fromFunB) where\n toFrom-\u03a3 : FromTo (\u03a3Search sA (\u03bb {x} \u2192 sB {x})) (toFun-\u03a3 sA sB toFunA toFunB) (fromFun-\u03a3 sA sB fromFunA fromFunB)\n toFrom-\u03a3 t = {!toFromA!} -- {!(\u03bb x \u2192 toFromB (toFunA t x))!}\n -}\n\ntoFun-\u00d7 : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 ToFun sA \u2192 ToFun sB \u2192 ToFun (sA \u00d7Search sB)\ntoFun-\u00d7 sA sB toFunA toFunB = toFun-\u03a3 sA sB toFunA toFunB\n\nfromFun-\u00d7 : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 FromFun sA \u2192 FromFun sB \u2192 FromFun (sA \u00d7Search sB)\nfromFun-\u00d7 sA sB fromFunA fromFunB = fromFun-\u03a3 sA sB fromFunA fromFunB\n\ntoFun-Bit : ToFun (search \u03bcBit)\ntoFun-Bit (f , t) false = f\ntoFun-Bit (f , t) true = t\n\nfromFun-Bit : FromFun (search \u03bcBit)\nfromFun-Bit f = f false , f true\n\ntoFun-\u228e : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 ToFun sA \u2192 ToFun sB \u2192 ToFun (sA +Search sB)\ntoFun-\u228e sA sB toFunA toFunB (t , u) (inj\u2081 x) = toFunA t x\ntoFun-\u228e sA sB toFunA toFunB (t , u) (inj\u2082 x) = toFunB u x\n\nfromFun-\u228e : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 FromFun sA \u2192 FromFun sB \u2192 FromFun (sA +Search sB)\nfromFun-\u228e sA sB fromFunA fromFunB f = fromFunA (f \u2218 inj\u2081) , fromFunB (f \u2218 inj\u2082)\n\n-- toFun-searchInd : \u2200 {A} {sA : Search A} \u2192 SearchInd sA \u2192 ToFun sA\n-- toFun-searchInd {A} {sA} indA {B} t = ?\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"fcd66503bf65118ad5fda83327897a1b2b6fd85e","subject":"Minor changes to a test.","message":"Minor changes to a test.\n\nIgnore-this: 7b6fca5f08d62b3a275c60659cb5385b\n\ndarcs-hash:20100730043722-3bd4e-d33f283f3cb4b7f6d96c7b651cffce8783b5b2cc.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Test\/Succeed\/Definitions.agda","new_file":"Test\/Succeed\/Definitions.agda","new_contents":"module Test.Succeed.Definitions where\n\ninfixl 6 _+_\ninfix 4 _\u2261_\n\npostulate\n D : Set\n zero : D\n succ : D \u2192 D\n _\u2261_ : D \u2192 D \u2192 Set\n\n-- The LTC natural numbers type.\ndata N : D \u2192 Set where\n zN : N zero\n sN : {n : D} \u2192 N n \u2192 N (succ n)\n\n-- Induction principle for N (elimination rule).\nindN : (P : D \u2192 Set) \u2192\n P zero \u2192\n ({n : D} \u2192 N n \u2192 P n \u2192 P (succ n)) \u2192\n {n : D} \u2192 N n \u2192 P n\nindN P p0 h zN = p0\nindN P p0 h (sN Nn) = h Nn (indN P p0 h Nn)\n\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : (d : D) \u2192 zero + d \u2261 d\n +-Sx : (d e : D) \u2192 succ d + e \u2261 succ (d + e)\n{-# ATP axiom +-0x #-}\n{-# ATP axiom +-Sx #-}\n\nP : D \u2192 Set\nP i = zero + i \u2261 i\n{-# ATP definition P #-}\n\npostulate\n P0 : P zero\n{-# ATP prove P0 #-}\n\npostulate\n iStep : {i : D} \u2192 N i \u2192 P i \u2192 P (succ i)\n{-# ATP prove iStep #-}\n\n+-leftIdentity : {n : D} \u2192 N n \u2192 zero + n \u2261 n\n+-leftIdentity = indN (\u03bb i \u2192 P i) P0 iStep\n","old_contents":"module Test.Succeed.Definitions where\n\ninfixl 6 _+_\ninfix 4 _\u2261_\n\npostulate\n D : Set\n zero : D\n succ : D \u2192 D\n\n-- The identity type.\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n-- The LTC natural numbers type.\ndata N : D \u2192 Set where\n zN : N zero\n sN : {n : D} \u2192 N n \u2192 N (succ n)\n\n-- Induction principle for N (elimination rule).\nindN : (P : D \u2192 Set) \u2192\n P zero \u2192\n ({n : D} \u2192 N n \u2192 P n \u2192 P (succ n)) \u2192\n {n : D} \u2192 N n \u2192 P n\nindN P p0 h zN = p0\nindN P p0 h (sN Nn) = h Nn (indN P p0 h Nn)\n\npostulate\n _+_ : D \u2192 D \u2192 D\n add-x0 : (n : D) \u2192 n + zero \u2261 n\n add-xS : (m n : D) \u2192 m + succ n \u2261 succ (m + n)\n{-# ATP axiom add-x0 #-}\n{-# ATP axiom add-xS #-}\n\nP : D \u2192 Set\nP i = zero + i \u2261 i\n{-# ATP definition P #-}\n\npostulate\n P0 : P zero\n{-# ATP prove P0 #-}\n\npostulate\n iStep : {i : D} \u2192 N i \u2192 P i \u2192 P (succ i)\n{-# ATP prove iStep #-}\n\naddLeftIdentity : {n : D} \u2192 N n \u2192 zero + n \u2261 n\naddLeftIdentity = indN (\u03bb i \u2192 P i) P0 iStep\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"0a73b4bf215c087bbfe6a7f0207b9e8472ce9a05","subject":"ElGamal: functional correctness","message":"ElGamal: functional correctness\n","repos":"crypto-agda\/crypto-agda","old_file":"Cipher\/ElGamal\/Generic.agda","new_file":"Cipher\/ElGamal\/Generic.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Type\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Cipher.ElGamal.Generic\n (Message : \u2605)\n (\u2124q : \u2605)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n\n -- Required for encryption\n (_\u2666_ : G \u2192 Message \u2192 Message)\n\n -- Required for decryption\n (_\/_ : Message \u2192 G \u2192 Message)\nwhere\n\nPubKey = G\nSecKey = \u2124q\nCipherText = G \u00d7 Message\nR\u2096 = \u2124q\nR\u2091 = \u2124q\n\nPubKeyGen : R\u2096 \u2192 PubKey\nPubKeyGen x = g ^ x\n\nKeyGen : R\u2096 \u2192 PubKey \u00d7 SecKey\nKeyGen x = (g ^ x , x)\n\nEnc : PubKey \u2192 Message \u2192 R\u2091 \u2192 CipherText\nEnc g\u02e3 m y = g\u02b8 , \u03b6 where\n g\u02b8 = g ^ y\n \u03b4 = g\u02e3 ^ y\n \u03b6 = \u03b4 \u2666 m\n\nDec : SecKey \u2192 CipherText \u2192 Message\nDec x (g\u02b8 , \u03b6) = \u03b6 \/ (g\u02b8 ^ x)\n\nmodule FunctionalCorrectness\n (\/-\u2666 : \u2200 {x y} \u2192 (x \u2666 y) \/ x \u2261 y)\n (comm-^ : \u2200 {\u03b1 x y} \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n where\n\n functional-correctness : \u2200 x y m \u2192 Dec x (Enc (g ^ x) m y) \u2261 m\n functional-correctness x y m = ap Ctx comm-^ \u2219 \/-\u2666\n where Ctx = \u03bb z \u2192 (z \u2666 m) \/ ((g ^ y)^ x)\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Type\nopen import Data.Product\n\nmodule Cipher.ElGamal.Generic\n (Message : \u2605)\n (\u2124q : \u2605)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n\n -- Required for encryption\n (_\u2219_ : G \u2192 Message \u2192 Message)\n\n -- Required for decryption\n (_\/_ : Message \u2192 G \u2192 Message)\nwhere\n\nPubKey = G\nSecKey = \u2124q\nCipherText = G \u00d7 Message\nR\u2096 = \u2124q\nR\u2091 = \u2124q\n\nKeyGen : R\u2096 \u2192 PubKey \u00d7 SecKey\nKeyGen x = (g ^ x , x)\n\nEnc : PubKey \u2192 Message \u2192 R\u2091 \u2192 CipherText\nEnc g\u02e3 m y = g\u02b8 , \u03b6 where\n g\u02b8 = g ^ y\n \u03b4 = g\u02e3 ^ y\n \u03b6 = \u03b4 \u2219 m\n\nDec : SecKey \u2192 CipherText \u2192 Message\nDec x (g\u02b8 , \u03b6) = \u03b6 \/ (g\u02b8 ^ x)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"37bd60875400557d90e8376abde43fd1c27e2afe","subject":"Adapt Data.Bits.OperationSyntax","message":"Adapt Data.Bits.OperationSyntax\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits\/OperationSyntax.agda","new_file":"lib\/Data\/Bits\/OperationSyntax.agda","new_contents":"-- NOTE with-K\nopen import Data.Bit hiding (_==_)\nopen import Data.Bits\nopen import Data.Two hiding (_==_)\n\n-- TODO get rid of IFs\nopen import Data.Bool using (if_then_else_)\n\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Vec.NP\nopen import Data.Vec.Bijection\nopen import Data.Vec.Permutation\n\nopen import Function.Bijection.SyntaxKit\nopen import Function.NP\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\n\nmodule Data.Bits.OperationSyntax where\n\nmodule \ud835\udfdaBij = \ud835\udfdaBijection\nopen \ud835\udfdaBij public using (`id; \ud835\udfdaBij) renaming (bool-bijKit to bitBijKit; `not to `not\u1d2e)\nopen BijectionSyntax Bit \ud835\udfdaBij public\nopen BijectionSemantics bitBijKit public\n\n{-\n id \u03b5 id\n 0\u21941 swp-inners interchange\n not swap comm\n when0 first ...\n when1 second ...\n-}\n`not : \u2200 {n} \u2192 Bij (1 + n)\n`not = \ud835\udfdaBij.`not `\u2237 const `id\n\n`xor : \u2200 {n} \u2192 Bit \u2192 Bij (1 + n)\n`xor b = \ud835\udfdaBij.`xor b `\u2237 const `id\n\n`[0:_1:_] : \u2200 {n} \u2192 Bij n \u2192 Bij n \u2192 Bij (1 + n)\n`[0: f 1: g ] = \ud835\udfdaBij.`id `\u2237 [0: f 1: g ]\n\n`when0 : \u2200 {n} \u2192 Bij n \u2192 Bij (1 + n)\n`when0 f = `[0: f 1: `id ]\n\n`when1 : \u2200 {n} \u2192 Bij n \u2192 Bij (1 + n)\n`when1 f = `[0: `id 1: f ]\n\n-- law: `when0 f `\u204f `when1 g \u2261 `when1 g `; `when0 f\n\non-firsts : \u2200 {n} \u2192 Bij (1 + n) \u2192 Bij (2 + n)\non-firsts f = `0\u21941 `\u204f `when0 f `\u204f `0\u21941\n\n-- (a \u00b7 b) \u00b7 (c \u00b7 d)\n-- \u2261 right swap\n-- (a \u00b7 b) \u00b7 (d \u00b7 c)\n-- \u2261 interchange\n-- (a \u00b7 d) \u00b7 (b \u00b7 c)\n-- \u2261 right swap\n-- (a \u00b7 d) \u00b7 (c \u00b7 b)\nswp-seconds : \u2200 {n} \u2192 Bij (2 + n)\nswp-seconds = `when1 `not `\u204f `0\u21941 `\u204f `when1 `not\n\non-extremes : \u2200 {n} \u2192 Bij (1 + n) \u2192 Bij (2 + n)\n-- on-extremes f = swp-seconds `\u204f `when0 f `\u204f swp-seconds\n\n-- (a \u00b7 b) \u00b7 (c \u00b7 d)\n-- \u2261 right swap \u2261 when1 not\n-- (a \u00b7 b) \u00b7 (d \u00b7 c)\n-- \u2261 interchange \u2261 0\u21941\n-- (a \u00b7 d) \u00b7 (b \u00b7 c)\n-- \u2261 left f \u2261 when0 f\n-- (A \u00b7 D) \u00b7 (b \u00b7 c)\n-- where A \u00b7 D = f (a \u00b7 d)\n-- \u2261 interchange \u2261 0\u21941\n-- (A \u00b7 b) \u00b7 (D \u00b7 c)\n-- \u2261 right swap \u2261 when1 not\n-- (A \u00b7 b) \u00b7 (c \u00b7 D)\non-extremes f = `when1 `not `\u204f `0\u21941 `\u204f `when0 f `\u204f `0\u21941 `\u204f `when1 `not\n\nmap-inner : \u2200 {n} \u2192 Bij (1 + n) \u2192 Bij (2 + n)\nmap-inner f = `when1 `not `\u204f `0\u21941 `\u204f `when1 f `\u204f `0\u21941 `\u204f `when1 `not\n\nmap-outer : \u2200 {n} \u2192 Bij n \u2192 Bij n \u2192 Bij (1 + n)\nmap-outer = `[0:_1:_]\n\n0\u21941\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bij (1 + n)\n0\u21941\u2237 [] = `not\n0\u21941\u2237 (0\u2082 \u2237 p) = on-firsts (0\u21941\u2237 p)\n0\u21941\u2237 (1\u2082 \u2237 p) = on-extremes (0\u21941\u2237 p)\n\n0\u2194_ : \u2200 {n} \u2192 Bits n \u2192 Bij n\n0\u2194 [] = `id\n0\u2194 (0\u2082 \u2237 p) = `when0 (0\u2194 p)\n0\u2194 (1\u2082 \u2237 p) = 0\u21941\u2237 p\n\n\u27e80\u2194_\u27e9-sem : \u2200 {n} (p : Bits n) \u2192 Bits n \u2192 Bits n\n\u27e80\u2194 p \u27e9-sem xs = if 0\u207f == xs then p else if p == xs then 0\u207f else xs\n\nif\u2237 : \u2200 {n} a x (xs ys : Bits n) \u2192 (if a then (x \u2237 xs) else (x \u2237 ys)) \u2261 x \u2237 (if a then xs else ys)\nif\u2237 1\u2082 x xs ys = refl\nif\u2237 0\u2082 x xs ys = refl\n\nif-not\u2237 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (0\u2082 \u2237 xs) else (1\u2082 \u2237 ys)) \u2261 (not a) \u2237 (if a then xs else ys)\nif-not\u2237 1\u2082 xs ys = refl\nif-not\u2237 0\u2082 xs ys = refl\n\nif\u2237\u2032 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (1\u2082 \u2237 xs) else (0\u2082 \u2237 ys)) \u2261 a \u2237 (if a then xs else ys)\nif\u2237\u2032 1\u2082 xs ys = refl\nif\u2237\u2032 0\u2082 xs ys = refl\n\n\u27e80\u21941\u2237_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u21941\u2237 p \u00b7 xs \u2261 \u27e80\u2194 (1\u2237 p) \u27e9-sem xs\n\u27e80\u21941\u2237_\u27e9-spec [] (1\u2082 \u2237 []) = refl\n\u27e80\u21941\u2237_\u27e9-spec [] (0\u2082 \u2237 []) = refl\n\u27e80\u21941\u2237_\u27e9-spec (1\u2082 \u2237 ps) (1\u2082 \u2237 1\u2082 \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n... | 1\u2082 = refl\n... | 0\u2082 = refl\n\u27e80\u21941\u2237_\u27e9-spec (1\u2082 \u2237 ps) (1\u2082 \u2237 0\u2082 \u2237 xs) = refl\n\u27e80\u21941\u2237_\u27e9-spec (1\u2082 \u2237 ps) (0\u2082 \u2237 1\u2082 \u2237 xs) = refl\n\u27e80\u21941\u2237_\u27e9-spec (1\u2082 \u2237 ps) (0\u2082 \u2237 0\u2082 \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n... | 1\u2082 = refl\n... | 0\u2082 = refl\n\u27e80\u21941\u2237_\u27e9-spec (0\u2082 \u2237 ps) (1\u2082 \u2237 1\u2082 \u2237 xs) = refl\n\u27e80\u21941\u2237_\u27e9-spec (0\u2082 \u2237 ps) (1\u2082 \u2237 0\u2082 \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n... | 1\u2082 = refl\n... | 0\u2082 = refl\n\u27e80\u21941\u2237_\u27e9-spec (0\u2082 \u2237 ps) (0\u2082 \u2237 1\u2082 \u2237 xs) = refl\n\u27e80\u21941\u2237_\u27e9-spec (0\u2082 \u2237 ps) (0\u2082 \u2237 0\u2082 \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n... | 1\u2082 = refl\n... | 0\u2082 = refl\n\n\u27e80\u2194_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u2194 p \u00b7 xs \u2261 \u27e80\u2194 p \u27e9-sem xs\n\u27e80\u2194_\u27e9-spec [] [] = refl\n\u27e80\u2194_\u27e9-spec (0\u2082 \u2237 ps) (1\u2082 \u2237 xs) = refl\n\u27e80\u2194_\u27e9-spec (0\u2082 \u2237 ps) (0\u2082 \u2237 xs)\n rewrite \u27e80\u2194 ps \u27e9-spec xs\n | if\u2237 (ps == xs) 0b 0\u207f xs\n | if\u2237 (0\u207f == xs) 0b ps (if ps == xs then 0\u207f else xs)\n = refl\n\u27e80\u2194_\u27e9-spec (1\u2082 \u2237 p) xs = \u27e80\u21941\u2237 p \u27e9-spec xs\n\nprivate\n module P where\n open PermutationSyntax public\n open PermutationSemantics public\nopen P using (Perm; `id; `0\u21941; _`\u204f_)\n\n`\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij (1 + n)\n`\u27e80\u21941+ zero \u27e9 = `0\u21941\n`\u27e80\u21941+ suc i \u27e9 = `0\u21941 `\u204f `tl `\u27e80\u21941+ i \u27e9 `\u204f `0\u21941\n\n`\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u21941+ i \u27e9 \u00b7 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n`\u27e80\u21941+ zero \u27e9-spec xs = refl\n`\u27e80\u21941+ suc i \u27e9-spec (x \u2237 _ \u2237 xs) rewrite `\u27e80\u21941+ i \u27e9-spec (x \u2237 xs) = refl\n\n`\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij n\n`\u27e80\u2194 zero \u27e9 = `id\n`\u27e80\u2194 suc i \u27e9 = `\u27e80\u21941+ i \u27e9\n\n`\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u2194 i \u27e9 \u00b7 xs \u2261 \u27e80\u2194 i \u27e9 xs\n`\u27e80\u2194 zero \u27e9-spec xs = refl\n`\u27e80\u2194 suc i \u27e9-spec xs = `\u27e80\u21941+ i \u27e9-spec xs\n\n{-\n`\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Bij n\n`\u27e8 zero \u2194 j \u27e9 = `\u27e80\u2194 j \u27e9\n`\u27e8 i \u2194 zero \u27e9 = `\u27e80\u2194 i \u27e9\n`\u27e8 suc i \u2194 suc j \u27e9 = `tl `\u27e8 i \u2194 j \u27e9\n\n`\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) xs \u2192 `\u27e8 i \u2194 j \u27e9 \u00b7 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n`\u27e8_\u2194_\u27e9-spec zero j xs = `\u27e80\u2194 j \u27e9-spec xs\n`\u27e8_\u2194_\u27e9-spec (suc i) zero xs = `\u27e80\u21941+ i \u27e9-spec xs\n`\u27e8_\u2194_\u27e9-spec (suc i) (suc j) (x \u2237 xs) rewrite `\u27e8 i \u2194 j \u27e9-spec xs = refl\n-}\n\n`xor-head : \u2200 {n} \u2192 Bit \u2192 Bij (1 + n)\n`xor-head = [0: `id 1: `not ]\n\n`xor-head-spec : \u2200 b {n} x (xs : Bits n) \u2192 `xor-head b \u00b7 (x \u2237 xs) \u2261 (b xor x) \u2237 xs\n`xor-head-spec 1\u2082 x xs = refl\n`xor-head-spec 0\u2082 x xs = refl\n\n`\u27e8_\u2295\u27e9 : \u2200 {n} \u2192 Bits n \u2192 Bij n\n`\u27e8 [] \u2295\u27e9 = `id\n`\u27e8 b \u2237 xs \u2295\u27e9 = `xor-head b `\u204f `tl `\u27e8 xs \u2295\u27e9\n\n`\u27e8_\u2295\u27e9-spec : \u2200 {n} (xs ys : Bits n) \u2192 `\u27e8 xs \u2295\u27e9 \u00b7 ys \u2261 xs \u2295 ys\n`\u27e8 [] \u2295\u27e9-spec [] = refl\n`\u27e8 1\u2082 \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n`\u27e8 0\u2082 \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n\n\u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) x \u2192 0\u21941 pad \u2295 0\u21941 x \u2261 0\u21941 (pad \u2295 x)\n\u2295-dist-0\u21941 _ [] = refl\n\u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n\u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Data.Bit hiding (_==_)\nopen import Data.Bits\nopen import Data.Bool.NP hiding (_==_)\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Vec.NP\n\nopen import Function.Bijection.SyntaxKit\nopen import Function.NP\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\n\nmodule Data.Bits.OperationSyntax where\n\nmodule BitBij = BoolBijection\nopen BitBij public using (`id) renaming (BoolBij to BitBij; bool-bijKit to bitBijKit; `not to `not\u1d2e)\nopen BijectionSyntax Bit BitBij public\nopen BijectionSemantics bitBijKit public\n\n{-\n id \u03b5 id\n 0\u21941 swp-inners interchange\n not swap comm\n if0 first ...\n if1 second ...\n-}\n`not : \u2200 {n} \u2192 Bij (1 + n)\n`not = BitBij.`not `\u2237 const `id\n\n`xor : \u2200 {n} \u2192 Bit \u2192 Bij (1 + n)\n`xor b = BitBij.`xor b `\u2237 const `id\n\n`if : \u2200 {n} \u2192 Bij n \u2192 Bij n \u2192 Bij (1 + n)\n`if f g = BitBij.`id `\u2237 cond f g\n\n`if0 : \u2200 {n} \u2192 Bij n \u2192 Bij (1 + n)\n`if0 f = `if `id f\n\n`if1 : \u2200 {n} \u2192 Bij n \u2192 Bij (1 + n)\n`if1 f = `if f `id\n\n-- law: `if0 f `\u204f `if1 g \u2261 `if1 g `; `if0 f\n\non-firsts : \u2200 {n} \u2192 Bij (1 + n) \u2192 Bij (2 + n)\non-firsts f = `0\u21941 `\u204f `if0 f `\u204f `0\u21941\n\n-- (a \u2219 b) \u2219 (c \u2219 d)\n-- \u2261 right swap\n-- (a \u2219 b) \u2219 (d \u2219 c)\n-- \u2261 interchange\n-- (a \u2219 d) \u2219 (b \u2219 c)\n-- \u2261 right swap\n-- (a \u2219 d) \u2219 (c \u2219 b)\nswp-seconds : \u2200 {n} \u2192 Bij (2 + n)\nswp-seconds = `if1 `not `\u204f `0\u21941 `\u204f `if1 `not\n\non-extremes : \u2200 {n} \u2192 Bij (1 + n) \u2192 Bij (2 + n)\n-- on-extremes f = swp-seconds `\u204f `if0 f `\u204f swp-seconds\n\n-- (a \u2219 b) \u2219 (c \u2219 d)\n-- \u2261 right swap \u2261 if1 not\n-- (a \u2219 b) \u2219 (d \u2219 c)\n-- \u2261 interchange \u2261 0\u21941\n-- (a \u2219 d) \u2219 (b \u2219 c)\n-- \u2261 left f \u2261 if0 f\n-- (A \u2219 D) \u2219 (b \u2219 c)\n-- where A \u2219 D = f (a \u2219 d)\n-- \u2261 interchange \u2261 0\u21941\n-- (A \u2219 b) \u2219 (D \u2219 c)\n-- \u2261 right swap \u2261 if1 not\n-- (A \u2219 b) \u2219 (c \u2219 D)\non-extremes f = `if1 `not `\u204f `0\u21941 `\u204f `if0 f `\u204f `0\u21941 `\u204f `if1 `not\n\nmap-inner : \u2200 {n} \u2192 Bij (1 + n) \u2192 Bij (2 + n)\nmap-inner f = `if1 `not `\u204f `0\u21941 `\u204f `if1 f `\u204f `0\u21941 `\u204f `if1 `not\n\nmap-outer : \u2200 {n} \u2192 Bij n \u2192 Bij n \u2192 Bij (1 + n)\nmap-outer f g = `if g f\n\n0\u21941\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bij (1 + n)\n0\u21941\u2237 [] = `not\n0\u21941\u2237 (true {-1-} \u2237 p) = on-extremes (0\u21941\u2237 p)\n0\u21941\u2237 (false{-0-} \u2237 p) = on-firsts (0\u21941\u2237 p)\n\n0\u2194_ : \u2200 {n} \u2192 Bits n \u2192 Bij n\n0\u2194 [] = `id\n0\u2194 (false{-0-} \u2237 p) = `if0 (0\u2194 p)\n0\u2194 (true{-1-} \u2237 p) = 0\u21941\u2237 p\n\n\u27e80\u2194_\u27e9-sem : \u2200 {n} (p : Bits n) \u2192 Bits n \u2192 Bits n\n\u27e80\u2194 p \u27e9-sem xs = if 0\u207f == xs then p else if p == xs then 0\u207f else xs\n\nif\u2237 : \u2200 {n} a x (xs ys : Bits n) \u2192 (if a then (x \u2237 xs) else (x \u2237 ys)) \u2261 x \u2237 (if a then xs else ys)\nif\u2237 true x xs ys = refl\nif\u2237 false x xs ys = refl\n\nif-not\u2237 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (false \u2237 xs) else (true \u2237 ys)) \u2261 (not a) \u2237 (if a then xs else ys)\nif-not\u2237 true xs ys = refl\nif-not\u2237 false xs ys = refl\n\nif\u2237\u2032 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (true \u2237 xs) else (false \u2237 ys)) \u2261 a \u2237 (if a then xs else ys)\nif\u2237\u2032 true xs ys = refl\nif\u2237\u2032 false xs ys = refl\n\n\u27e80\u21941\u2237_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u21941\u2237 p \u2219 xs \u2261 \u27e80\u2194 (1\u2237 p) \u27e9-sem xs\n\u27e80\u21941\u2237_\u27e9-spec [] (true \u2237 []) = refl\n\u27e80\u21941\u2237_\u27e9-spec [] (false \u2237 []) = refl\n\u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 true \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n... | true = refl\n... | false = refl\n\u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 false \u2237 xs) = refl\n\u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 true \u2237 xs) = refl\n\u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n... | true = refl\n... | false = refl\n\u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 true \u2237 xs) = refl\n\u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n... | true = refl\n... | false = refl\n\u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 true \u2237 xs) = refl\n\u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n... | true = refl\n... | false = refl\n\n\u27e80\u2194_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u2194 p \u2219 xs \u2261 \u27e80\u2194 p \u27e9-sem xs\n\u27e80\u2194_\u27e9-spec [] [] = refl\n\u27e80\u2194_\u27e9-spec (false \u2237 ps) (true \u2237 xs) = refl\n\u27e80\u2194_\u27e9-spec (false \u2237 ps) (false \u2237 xs)\n rewrite \u27e80\u2194 ps \u27e9-spec xs\n | if\u2237 (ps == xs) 0b 0\u207f xs\n | if\u2237 (0\u207f == xs) 0b ps (if ps == xs then 0\u207f else xs)\n = refl\n\u27e80\u2194_\u27e9-spec (true \u2237 p) xs = \u27e80\u21941\u2237 p \u27e9-spec xs\n\nprivate\n module P where\n open PermutationSyntax public\n open PermutationSemantics public\nopen P using (Perm; `id; `0\u21941; _`\u204f_)\n\n`\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij (1 + n)\n`\u27e80\u21941+ zero \u27e9 = `0\u21941\n`\u27e80\u21941+ suc i \u27e9 = `0\u21941 `\u204f `tl `\u27e80\u21941+ i \u27e9 `\u204f `0\u21941\n\n`\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n`\u27e80\u21941+ zero \u27e9-spec xs = refl\n`\u27e80\u21941+ suc i \u27e9-spec (x \u2237 _ \u2237 xs) rewrite `\u27e80\u21941+ i \u27e9-spec (x \u2237 xs) = refl\n\n`\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij n\n`\u27e80\u2194 zero \u27e9 = `id\n`\u27e80\u2194 suc i \u27e9 = `\u27e80\u21941+ i \u27e9\n\n`\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n`\u27e80\u2194 zero \u27e9-spec xs = refl\n`\u27e80\u2194 suc i \u27e9-spec xs = `\u27e80\u21941+ i \u27e9-spec xs\n\n{-\n`\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Bij n\n`\u27e8 zero \u2194 j \u27e9 = `\u27e80\u2194 j \u27e9\n`\u27e8 i \u2194 zero \u27e9 = `\u27e80\u2194 i \u27e9\n`\u27e8 suc i \u2194 suc j \u27e9 = `tl `\u27e8 i \u2194 j \u27e9\n\n`\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) xs \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n`\u27e8_\u2194_\u27e9-spec zero j xs = `\u27e80\u2194 j \u27e9-spec xs\n`\u27e8_\u2194_\u27e9-spec (suc i) zero xs = `\u27e80\u21941+ i \u27e9-spec xs\n`\u27e8_\u2194_\u27e9-spec (suc i) (suc j) (x \u2237 xs) rewrite `\u27e8 i \u2194 j \u27e9-spec xs = refl\n-}\n\n`xor-head : \u2200 {n} \u2192 Bit \u2192 Bij (1 + n)\n`xor-head b = if b then `not else `id\n\n`xor-head-spec : \u2200 b {n} x (xs : Bits n) \u2192 `xor-head b \u2219 (x \u2237 xs) \u2261 (b xor x) \u2237 xs\n`xor-head-spec true x xs = refl\n`xor-head-spec false x xs = refl\n\n`\u27e8_\u2295\u27e9 : \u2200 {n} \u2192 Bits n \u2192 Bij n\n`\u27e8 [] \u2295\u27e9 = `id\n`\u27e8 b \u2237 xs \u2295\u27e9 = `xor-head b `\u204f `tl `\u27e8 xs \u2295\u27e9\n\n`\u27e8_\u2295\u27e9-spec : \u2200 {n} (xs ys : Bits n) \u2192 `\u27e8 xs \u2295\u27e9 \u2219 ys \u2261 xs \u2295 ys\n`\u27e8 [] \u2295\u27e9-spec [] = refl\n`\u27e8 true \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n`\u27e8 false \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n\n\u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) x \u2192 0\u21941 pad \u2295 0\u21941 x \u2261 0\u21941 (pad \u2295 x)\n\u2295-dist-0\u21941 _ [] = refl\n\u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n\u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"4949f9058a6fb480bcce92b2259a9d154a7abc47","subject":"filling in ealamhole rule","message":"filling in ealamhole rule\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"core.agda","new_file":"core.agda","new_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n mutual\n subst : Set -- todo: no idea if this is right; mutual thing is weird\n subst = dhexp ctx\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 subst) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 subst) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n <_>_ : htyp \u2192 dhexp \u2192 dhexp\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows, sums, and products\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = htyp ctx\n\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n id : tctx \u2192 subst\n id ctx x with ctx x\n id ctx x | Some \u03c4 = Some (X x)\n id ctx x | None = None\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 tctx \u00d7 htyp)\n u ::[ \u0393 ] \u03c4 = u , \u0393 , \u03c4\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {n : Nat} \u2192\n (n , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of n?\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192 -- todo: uniqueness of n?\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- todo: do we care about completeness of hexp or e-umlauts?\n -- those types without holes anywhere\n tcomplete : htyp \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (\u03c41 ==> \u03c42) = tcomplete \u03c41 \u00d7 tcomplete \u03c42\n\n -- those expressions without holes anywhere\n ecomplete : hexp \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: \u03c4) = ecomplete e1 \u00d7 tcomplete \u03c4\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ \u03c4 ] e) = tcomplete \u03c4 \u00d7 ecomplete e\n\n -- not a hole\n\n data _not-hole : dhexp \u2192 Set where\n CNotHole : c not-hole\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp1 : \u2200{\u0393 e1 e2 d2 d1 \u03941 \u03c42 \u03c41 \u03942} \u2192\n \u03941 ## \u03942 \u2192\n \u0393 \u22a2 e1 => \u2987\u2988 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u2987\u2988) ~> d1 :: \u03c41 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u2987\u2988 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u2987\u2988 ~> (< \u03c42 ==> \u2987\u2988 > d1) \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESAp2 : \u2200{\u0393 e1 \u03c42 \u03c4 d1 d2 \u03941 \u03942 \u03c42' e2} \u2192\n \u03941 ## \u03942 \u2192\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n (\u03c42 == \u03c42' \u2192 \u22a5) \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 (< \u03c42 > d2) \u22a3 (\u03941 \u222a \u03942)\n ESAp3 : \u2200{\u0393 e1 \u03c4 d1 \u03941 e2 \u03c42 d2 \u03942 } \u2192\n \u03941 ## \u03942 \u2192\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc1 : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u03c4 == \u03c4' \u2192 \u22a5) \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> (< \u03c4 > d) \u22a3 \u0394\n ESAsc2 : \u2200{\u0393 e \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c41 ==> \u03c42 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EALamHole : \u2200{\u0393 x e \u03c4 d \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u2987\u2988)) \u22a2 e \u21d0 \u2987\u2988 ~> d :: \u03c4 \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u2987\u2988 ~> \u00b7\u03bb x [ \u2987\u2988 ] d :: \u2987\u2988 ==> \u03c4 \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 (e == \u2987\u2988[ u ] \u2192 \u22a5)) \u2192\n ((e' : hexp) (u : Nat) \u2192 (e == \u2987 e' \u2988[ u ] \u2192 \u22a5)) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n mutual\n -- substitition type assignment\n _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 subst \u2192 tctx \u2192 Set\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' =\n (x : Nat) (d : dhexp) \u2192\n (x , d) \u2208 \u03c3 \u2192\n \u03a3[ \u03c4 \u2208 htyp ] (\u0393' x == Some \u03c4 \u00d7 \u0394 , \u0393 \u22a2 d :: \u03c4)\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c42 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u03c41 \u25b8arr (\u03c42 ==> \u03c4) \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4} \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n (\u0394 ,, u ::[ \u0393' ] \u03c4) , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n (\u0394 ,, u ::[ \u0393' ] \u03c4) , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c4 \u03c4'} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0394 , \u0393 \u22a2 < \u03c4 > d :: \u03c4\n\n postulate -- todo: write this later\n [_]_ : subst \u2192 dhexp \u2192 dhexp\n\n -- value\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n mutual\n -- indeterminate\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 d1 indet \u2192 d2 final \u2192 (d1 \u2218 d2) indet\n ICast : \u2200{d \u03c4} \u2192 d indet \u2192 (< \u03c4 > d) indet\n\n -- final\n data _final : (d : dhexp) \u2192 Set where\n FVal : \u2200{d} \u2192 d val \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n------------- these two judgements are still being figured out; form\n------------- changing, etc. double check everything here once it settles\n------------- before doing anything with it\n -- error\n data _err[_] : (d : dhexp) \u2192 (\u0394 : hctx) \u2192 Set where\n -- ERNEHole\n -- ERCastError\n -- ERLam\n -- ERAp1\n -- ERAp2\n -- ERCast\n\n -- small step semantics\n data _\u21a6_ : (d d' : dhexp) \u2192 Set where\n STHole : \u2200{ d d' u \u03c3 } \u2192\n d \u21a6 d' \u2192\n \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 \u21a6 \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9\n -- STCast\n STAp1 : \u2200{ d1 d2 d1' } \u2192\n d1 \u21a6 d1' \u2192\n (d1 \u2218 d2) \u21a6 (d1' \u2218 d2)\n STAp2 : \u2200{ d1 d2 d2' } \u2192\n d1 final \u2192\n d2 \u21a6 d2' \u2192\n (d1 \u2218 d2) \u21a6 (d1 \u2218 d2')\n -- STAp\u03b2 : \u2200{ d1 d2 \u03c4 x } \u2192\n -- d2 final \u2192\n -- ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u21a6 ([ \u27e6 x , {!!} \u27e7 ] d2)\n","old_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n mutual\n subst : Set -- todo: no idea if this is right; mutual thing is weird\n subst = dhexp ctx\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 subst) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 subst) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n <_>_ : htyp \u2192 dhexp \u2192 dhexp\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows, sums, and products\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = htyp ctx\n\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n id : tctx \u2192 subst\n id ctx x with ctx x\n id ctx x | Some \u03c4 = Some (X x)\n id ctx x | None = None\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 tctx \u00d7 htyp)\n u ::[ \u0393 ] \u03c4 = u , \u0393 , \u03c4\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {n : Nat} \u2192\n (n , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of n?\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192 -- todo: uniqueness of n?\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- todo: do we care about completeness of hexp or e-umlauts?\n -- those types without holes anywhere\n tcomplete : htyp \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (\u03c41 ==> \u03c42) = tcomplete \u03c41 \u00d7 tcomplete \u03c42\n\n -- those expressions without holes anywhere\n ecomplete : hexp \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: \u03c4) = ecomplete e1 \u00d7 tcomplete \u03c4\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ \u03c4 ] e) = tcomplete \u03c4 \u00d7 ecomplete e\n\n -- not a hole\n\n data _not-hole : dhexp \u2192 Set where\n CNotHole : c not-hole\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp1 : \u2200{\u0393 e1 e2 d2 d1 \u03941 \u03c42 \u03c41 \u03942} \u2192\n \u03941 ## \u03942 \u2192\n \u0393 \u22a2 e1 => \u2987\u2988 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u2987\u2988) ~> d1 :: \u03c41 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u2987\u2988 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u2987\u2988 ~> (< \u03c42 ==> \u2987\u2988 > d1) \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESAp2 : \u2200{\u0393 e1 \u03c42 \u03c4 d1 d2 \u03941 \u03942 \u03c42' e2} \u2192\n \u03941 ## \u03942 \u2192\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n (\u03c42 == \u03c42' \u2192 \u22a5) \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 (< \u03c42 > d2) \u22a3 (\u03941 \u222a \u03942)\n ESAp3 : \u2200{\u0393 e1 \u03c4 d1 \u03941 e2 \u03c42 d2 \u03942 } \u2192\n \u03941 ## \u03942 \u2192\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc1 : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u03c4 == \u03c4' \u2192 \u22a5) \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> (< \u03c4 > d) \u22a3 \u0394\n ESAsc2 : \u2200{\u0393 e \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c41 ==> \u03c42 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EALamHole : \u2200{\u0393 x e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n {!!} \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u2987\u2988 ~> \u00b7\u03bb x [ {!!} ] d :: {!!} \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 (e == \u2987\u2988[ u ] \u2192 \u22a5)) \u2192\n ((e' : hexp) (u : Nat) \u2192 (e == \u2987 e' \u2988[ u ] \u2192 \u22a5)) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n mutual\n -- substitition type assignment\n _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 subst \u2192 tctx \u2192 Set\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' =\n (x : Nat) (d : dhexp) \u2192\n (x , d) \u2208 \u03c3 \u2192\n \u03a3[ \u03c4 \u2208 htyp ] (\u0393' x == Some \u03c4 \u00d7 \u0394 , \u0393 \u22a2 d :: \u03c4)\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c42 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u03c41 \u25b8arr (\u03c42 ==> \u03c4) \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4} \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n (\u0394 ,, u ::[ \u0393' ] \u03c4) , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n (\u0394 ,, u ::[ \u0393' ] \u03c4) , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c4 \u03c4'} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0394 , \u0393 \u22a2 < \u03c4 > d :: \u03c4\n\n postulate -- todo: write this later\n [_]_ : subst \u2192 dhexp \u2192 dhexp\n\n -- value\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n mutual\n -- indeterminate\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 d1 indet \u2192 d2 final \u2192 (d1 \u2218 d2) indet\n ICast : \u2200{d \u03c4} \u2192 d indet \u2192 (< \u03c4 > d) indet\n\n -- final\n data _final : (d : dhexp) \u2192 Set where\n FVal : \u2200{d} \u2192 d val \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n------------- these two judgements are still being figured out; form\n------------- changing, etc. double check everything here once it settles\n------------- before doing anything with it\n -- error\n data _err[_] : (d : dhexp) \u2192 (\u0394 : hctx) \u2192 Set where\n -- ERNEHole\n -- ERCastError\n -- ERLam\n -- ERAp1\n -- ERAp2\n -- ERCast\n\n -- small step semantics\n data _\u21a6_ : (d d' : dhexp) \u2192 Set where\n STHole : \u2200{ d d' u \u03c3 } \u2192\n d \u21a6 d' \u2192\n \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 \u21a6 \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9\n -- STCast\n STAp1 : \u2200{ d1 d2 d1' } \u2192\n d1 \u21a6 d1' \u2192\n (d1 \u2218 d2) \u21a6 (d1' \u2218 d2)\n STAp2 : \u2200{ d1 d2 d2' } \u2192\n d1 final \u2192\n d2 \u21a6 d2' \u2192\n (d1 \u2218 d2) \u21a6 (d1 \u2218 d2')\n -- STAp\u03b2 : \u2200{ d1 d2 \u03c4 x } \u2192\n -- d2 final \u2192\n -- ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u21a6 ([ \u27e6 x , {!!} \u27e7 ] d2)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d135e593fa01a535210210ad2c6a558688023599","subject":"Nat: renaming","message":"Nat: renaming\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Nat\/NP.agda","new_file":"lib\/Data\/Nat\/NP.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Nat.NP where\n\nimport Algebra\nopen import Data.Nat public hiding (module GeneralisedArithmetic; module \u2264-Reasoning; fold)\nopen import Data.Nat.Properties as Props\nopen import Data.Nat.Logical\nopen import Data.Bool\nopen import Data.Product using (proj\u2081; proj\u2082; \u2203; _,_)\nopen import Data.Empty using (\u22a5-elim)\nopen import Function.NP\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_; module \u2261-Reasoning)\n\nmodule \u2115\u00b0 = Algebra.CommutativeSemiring Props.commutativeSemiring\nmodule \u2115cmp = StrictTotalOrder Props.strictTotalOrder\nmodule \u2115\u2264 = DecTotalOrder decTotalOrder\nmodule \u2294\u00b0 = Algebra.CommutativeSemiringWithoutOne \u2294-\u2293-0-commutativeSemiringWithoutOne\n\nmodule \u2264-Reasoning where\n open Preorder-Reasoning \u2115\u2264.preorder public renaming (_\u223c\u27e8_\u27e9_ to _\u2264\u27e8_\u27e9_)\n infixr 2 _\u2261\u27e8_\u27e9_\n _\u2261\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x \u2261 y \u2192 y \u2264 z \u2192 x \u2264 z\n _ \u2261\u27e8 \u2261.refl \u27e9 p = p\n infixr 2 _<\u27e8_\u27e9_\n _<\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x < y \u2192 y \u2264 z \u2192 x < z\n x <\u27e8 p \u27e9 q = suc x \u2264\u27e8 p \u27e9 q\n\nsuc-injective : \u2200 {n m : \u2115} \u2192 (suc n \u2236 \u2115) \u2261 suc m \u2192 n \u2261 m\nsuc-injective = \u2261.cong pred\n\nfold : \u2200 {a} {A : Set a} \u2192 A \u2192 Endo A \u2192 \u2115 \u2192 A\nfold x f n = nest n f x\n\n2*_ : \u2115 \u2192 \u2115\n2* x = x + x\n\n_==_ : (x y : \u2115) \u2192 Bool\nzero == zero = true\nzero == suc _ = false\nsuc _ == zero = false\nsuc m == suc n = m == n\n\nassoc-comm : \u2200 x y z \u2192 x + (y + z) \u2261 y + (x + z)\nassoc-comm x y z rewrite \u2261.sym (\u2115\u00b0.+-assoc x y z)\n | \u2115\u00b0.+-comm x y\n | \u2115\u00b0.+-assoc y x z = \u2261.refl\n\n2*\u2032_ : \u2115 \u2192 \u2115\n2*\u2032_ = fold 0 (suc \u2218\u2032 suc)\n\n2*\u2032-spec : \u2200 n \u2192 2*\u2032 n \u2261 2* n\n2*\u2032-spec zero = \u2261.refl\n2*\u2032-spec (suc n) rewrite 2*\u2032-spec n | assoc-comm 1 n n = \u2261.refl\n\ndist : \u2115 \u2192 \u2115 \u2192 \u2115\ndist zero y = y\ndist x zero = x\ndist (suc x) (suc y) = dist x y\n\ndist-refl : \u2200 x \u2192 dist x x \u2261 0\ndist-refl zero = \u2261.refl\ndist-refl (suc x) rewrite dist-refl x = \u2261.refl\n\ndist-0\u2261id : \u2200 x \u2192 dist 0 x \u2261 x\ndist-0\u2261id _ = \u2261.refl\n\ndist-x-x+y\u2261y : \u2200 x y \u2192 dist x (x + y) \u2261 y\ndist-x-x+y\u2261y zero y = \u2261.refl\ndist-x-x+y\u2261y (suc x) y = dist-x-x+y\u2261y x y\n\ndist-sym : \u2200 x y \u2192 dist x y \u2261 dist y x\ndist-sym zero zero = \u2261.refl\ndist-sym zero (suc y) = \u2261.refl\ndist-sym (suc x) zero = \u2261.refl\ndist-sym (suc x) (suc y) = dist-sym x y\n\ndist-x+ : \u2200 x y z \u2192 dist (x + y) (x + z) \u2261 dist y z\ndist-x+ zero y z = \u2261.refl\ndist-x+ (suc x) y z = dist-x+ x y z\n\ndist-2* : \u2200 x y \u2192 dist (2* x) (2* y) \u2261 2* dist x y\ndist-2* zero y = \u2261.refl\ndist-2* (suc x) zero = \u2261.refl\ndist-2* (suc x) (suc y) rewrite assoc-comm x 1 x\n | assoc-comm y 1 y = dist-2* x y\n\ndist-asym-def : \u2200 {x y} \u2192 x \u2264 y \u2192 x + dist x y \u2261 y\ndist-asym-def z\u2264n = \u2261.refl\ndist-asym-def (s\u2264s pf) = \u2261.cong suc (dist-asym-def pf)\n\ndist-sym-wlog : \u2200 (f : \u2115 \u2192 \u2115) \u2192 (\u2200 x k \u2192 dist (f x) (f (x + k)) \u2261 f k) \u2192 \u2200 x y \u2192 dist (f x) (f y) \u2261 f (dist x y)\ndist-sym-wlog f pf x y with compare x y\ndist-sym-wlog f pf x .(suc (x + k)) | less .x k with pf x (suc k)\n... | q rewrite assoc-comm x 1 k | q | \u2261.sym (assoc-comm x 1 k) | dist-x-x+y\u2261y x (suc k) = \u2261.refl\ndist-sym-wlog f pf .y y | equal .y with pf y 0\n... | q rewrite \u2115\u00b0.+-comm y 0 | dist-refl y = q\ndist-sym-wlog f pf .(suc (y + k)) y | greater .y k with pf y (suc k)\n... | q rewrite assoc-comm 1 y k | dist-sym (y + suc k) y | dist-x-x+y\u2261y y (suc k) | dist-sym (f (y + suc k)) (f y) = q\n\ndist-x* : \u2200 x y z \u2192 dist (x * y) (x * z) \u2261 x * dist y z\ndist-x* x = dist-sym-wlog (_*_ x) pf\n where pf : \u2200 a k \u2192 dist (x * a) (x * (a + k)) \u2261 x * k\n pf a k rewrite proj\u2081 \u2115\u00b0.distrib x a k = dist-x-x+y\u2261y (x * a) _\n\n2^\u27e8_\u27e9* : \u2115 \u2192 \u2115 \u2192 \u2115\n2^\u27e8 n \u27e9* x = fold x 2*_ n\n\n\u27e82^_*_\u27e9 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e82^ n * x \u27e9 = 2^\u27e8 n \u27e9* x\n\n2*-distrib : \u2200 x y \u2192 2* x + 2* y \u2261 2* (x + y) \n2*-distrib = solve 2 (\u03bb x y \u2192 2:* x :+ 2:* y := 2:* (x :+ y)) \u2261.refl\n where open SemiringSolver\n 2:* : \u2200 {n} \u2192 Polynomial n \u2192 Polynomial n\n 2:* x = x :+ x\n\n2^*-distrib : \u2200 k x y \u2192 \u27e82^ k * (x + y)\u27e9 \u2261 \u27e82^ k * x \u27e9 + \u27e82^ k * y \u27e9\n2^*-distrib zero x y = \u2261.refl\n2^*-distrib (suc k) x y rewrite 2^*-distrib k x y = \u2261.sym (2*-distrib \u27e82^ k * x \u27e9 \u27e82^ k * y \u27e9)\n\n2^*-2*-comm : \u2200 k x \u2192 \u27e82^ k * 2* x \u27e9 \u2261 2* \u27e82^ k * x \u27e9\n2^*-2*-comm k x = 2^*-distrib k x x\n\ndist-2^* : \u2200 x y z \u2192 dist \u27e82^ x * y \u27e9 \u27e82^ x * z \u27e9 \u2261 \u27e82^ x * dist y z \u27e9\ndist-2^* x = dist-sym-wlog (2^\u27e8 x \u27e9*) pf\n where pf : \u2200 a k \u2192 dist \u27e82^ x * a \u27e9 \u27e82^ x * (a + k) \u27e9 \u2261 \u27e82^ x * k \u27e9\n pf a k rewrite 2^*-distrib x a k = dist-x-x+y\u2261y \u27e82^ x * a \u27e9 \u27e82^ x * k \u27e9\n\n2*-mono : \u2200 {a b} \u2192 a \u2264 b \u2192 2* a \u2264 2* b\n2*-mono pf = pf +-mono pf\n\n2^*-mono : \u2200 k {a b} \u2192 a \u2264 b \u2192 \u27e82^ k * a \u27e9 \u2264 \u27e82^ k * b \u27e9\n2^*-mono zero pf = pf\n2^*-mono (suc k) pf = 2*-mono (2^*-mono k pf)\n\n2*-mono\u2032 : \u2200 {a b} \u2192 2* a \u2264 2* b \u2192 a \u2264 b\n2*-mono\u2032 {zero} pf = z\u2264n\n2*-mono\u2032 {suc a} {zero} ()\n2*-mono\u2032 {suc a} {suc b} pf rewrite assoc-comm a 1 a\n | assoc-comm b 1 b = s\u2264s (2*-mono\u2032 (\u2264-pred (\u2264-pred pf)))\n\n2^*-mono\u2032 : \u2200 k {a b} \u2192 \u27e82^ k * a \u27e9 \u2264 \u27e82^ k * b \u27e9 \u2192 a \u2264 b\n2^*-mono\u2032 zero = id\n2^*-mono\u2032 (suc k) = 2^*-mono\u2032 k \u2218 2*-mono\u2032\n\n2^-comm : \u2200 x y z \u2192 \u27e82^ x * \u27e82^ y * z \u27e9 \u27e9 \u2261 \u27e82^ y * \u27e82^ x * z \u27e9 \u27e9\n2^-comm zero y z = \u2261.refl\n2^-comm (suc x) y z rewrite 2^-comm x y z = \u2261.sym (2^*-2*-comm y \u27e82^ x * z \u27e9)\n\n2^-+ : \u2200 x y z \u2192 \u27e82^ x * \u27e82^ y * z \u27e9 \u27e9 \u2261 \u27e82^ (x + y) * z \u27e9\n2^-+ zero y z = \u2261.refl\n2^-+ (suc x) y z = \u2261.cong 2*_ (2^-+ x y z)\n\n2*\u2032-inj : \u2200 {m n} \u2192 \u27e6\u2115\u27e7 (2*\u2032 m) (2*\u2032 n) \u2192 \u27e6\u2115\u27e7 m n\n2*\u2032-inj {zero} {zero} _ = zero\n2*\u2032-inj {zero} {suc _} ()\n2*\u2032-inj {suc _} {zero} ()\n2*\u2032-inj {suc m} {suc n} (suc (suc p)) = suc (2*\u2032-inj p)\n\n2*-inj : \u2200 {m n} \u2192 2* m \u2261 2* n \u2192 m \u2261 n\n2*-inj {m} {n} p rewrite \u2261.sym (2*\u2032-spec m)\n | \u2261.sym (2*\u2032-spec n)\n = \u27e6\u2115\u27e7\u21d2\u2261 (2*\u2032-inj (\u27e6\u2115\u27e7\u02e2.reflexive p))\n\n2^-inj : \u2200 k {m n} \u2192 \u27e82^ k \u27e9* m \u2261 \u27e82^ k \u27e9* n \u2192 m \u2261 n\n2^-inj zero eq = eq\n2^-inj (suc k) eq = 2^-inj k (2*-inj eq)\n\n2\u207f*0\u22610 : \u2200 n \u2192 \u27e82^ n * 0 \u27e9 \u2261 0\n2\u207f*0\u22610 zero = \u2261.refl\n2\u207f*0\u22610 (suc n) = \u2261.cong\u2082 _+_ (2\u207f*0\u22610 n) (2\u207f*0\u22610 n)\n\n{-\npost--ulate\n dist-sum : \u2200 x y z \u2192 dist x y + dist y z \u2264 dist x z\n dist-\u2264 : \u2200 x y \u2192 dist x y \u2264 x\n dist-mono\u2081 : \u2200 x y z t \u2192 x \u2264 y \u2192 dist z t \u2264 dist (x + z) (y + t)\n-}\n\n-- Haskell\n-- let dist x y = abs (x - y)\n-- quickCheck (forAll (replicateM 3 (choose (0,100))) (\\ [x,y,z] -> dist (x * y) (x * z) == x * dist y z))\n\ninfix 8 _^_\n_^_ : \u2115 \u2192 \u2115 \u2192 \u2115\nb ^ zero = 1\nb ^ suc n = b * b ^ n\n\n2*-spec : \u2200 n \u2192 2* n \u2261 2 * n\n2*-spec n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\n\n2^_ : \u2115 \u2192 \u2115\n2^ n = \u27e82^ n * 1 \u27e9\n\n2^-spec : \u2200 n \u2192 2^ n \u2261 2 ^ n\n2^-spec zero = \u2261.refl\n2^-spec (suc n) rewrite 2^-spec n | 2*-spec (2 ^ n) = \u2261.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Hyper_operator\n_\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\na \u2191\u27e8 suc n \u27e9 (suc b) = a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\na \u2191\u27e8 0 \u27e9 b = suc b\na \u2191\u27e8 1 \u27e9 0 = a\na \u2191\u27e8 2 \u27e9 0 = 0\na \u2191\u27e8 suc (suc (suc n)) \u27e9 0 = 1\n\nmodule \u2191-Props where\n \u2191-fold : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 b \u2261 fold (a \u2191\u27e8 suc n \u27e9 0) (_\u2191\u27e8_\u27e9_ a n) b\n \u2191-fold a n zero = \u2261.refl\n \u2191-fold a n (suc b) = \u2261.cong (_\u2191\u27e8_\u27e9_ a n) (\u2191-fold a n b)\n\n \u2191\u2080-+ : \u2200 a b \u2192 a \u2191\u27e8 0 \u27e9 b \u2261 1 + b\n \u2191\u2080-+ a b = \u2261.refl\n\n \u2191\u2081-+ : \u2200 a b \u2192 a \u2191\u27e8 1 \u27e9 b \u2261 b + a\n \u2191\u2081-+ a zero = \u2261.refl\n \u2191\u2081-+ a (suc b) = \u2261.cong suc (\u2191\u2081-+ a b)\n\n \u2191\u2082-* : \u2200 a b \u2192 a \u2191\u27e8 2 \u27e9 b \u2261 b * a\n \u2191\u2082-* a zero = \u2261.refl\n \u2191\u2082-* a (suc b) rewrite \u2191\u2082-* a b\n | \u2191\u2081-+ a (b * a)\n | \u2115\u00b0.+-comm (b * a) a\n = \u2261.refl\n\n -- fold 1 (_*_ a) b \u2261 a ^ b\n \u2191\u2083-^ : \u2200 a b \u2192 a \u2191\u27e8 3 \u27e9 b \u2261 a ^ b\n \u2191\u2083-^ a zero = \u2261.refl\n \u2191\u2083-^ a (suc b) rewrite \u2191\u2083-^ a b\n | \u2191\u2082-* a (a ^ b)\n | \u2115\u00b0.*-comm (a ^ b) a\n = \u2261.refl\n\n _\u2191\u27e8_\u27e90 : \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u27e8 0 \u27e90 = 1\n a \u2191\u27e8 1 \u27e90 = a\n a \u2191\u27e8 2 \u27e90 = 0\n a \u2191\u27e8 suc (suc (suc n)) \u27e90 = 1\n\n _`\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _`\u2191\u27e8_\u27e9_ a 0 = suc\n _`\u2191\u27e8_\u27e9_ a (suc n) = fold (a \u2191\u27e8 suc n \u27e90) (_`\u2191\u27e8_\u27e9_ a n)\n\n \u2191-ind-rule : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 (suc b) \u2261 a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\n \u2191-ind-rule a n b = \u2261.refl\n\n _\u2191\u2032\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u2032\u27e8 0 \u27e9 b = suc b\n a \u2191\u2032\u27e8 1 \u27e9 b = a + b\n a \u2191\u2032\u27e8 2 \u27e9 b = a * b\n a \u2191\u2032\u27e8 3 \u27e9 b = a ^ b\n a \u2191\u2032\u27e8 suc (suc (suc (suc n))) \u27e9 b = a \u2191\u27e8 4 + n \u27e9 b\n\n \u2191-\u2191\u2032 : \u2200 a n b \u2192 a \u2191\u27e8 n \u27e9 b \u2261 a \u2191\u2032\u27e8 n \u27e9 b\n \u2191-\u2191\u2032 a 0 b = \u2261.refl\n \u2191-\u2191\u2032 a 1 b = \u2261.trans (\u2191\u2081-+ a b) (\u2115\u00b0.+-comm b a)\n \u2191-\u2191\u2032 a 2 b = \u2261.trans (\u2191\u2082-* a b) (\u2115\u00b0.*-comm b a)\n \u2191-\u2191\u2032 a 3 b = \u2191\u2083-^ a b\n \u2191-\u2191\u2032 a (suc (suc (suc (suc _)))) b = \u2261.refl\n\n _\u21912+\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _\u21912+\u27e8_\u27e9_ a = fold (_*_ a) (fold 1)\n\nmodule InflModule where\n -- Inflationary functions\n Infl< : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl< f = \u2200 {x} \u2192 x < f x\n\n Infl : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl f = Infl< (suc \u2218 f)\n\n InflT< : (f : Endo (Endo \u2115)) \u2192 Set\n InflT< f = \u2200 {g} \u2192 Infl< g \u2192 Infl< (f g)\n\n{-\n\u2200 {x} \u2192 x \u2264 f x\n\n\n 1 + x \u2264 1 + f x\n x < 1 + f x\n x < (suc \u2218 f) x\n Infl< (suc \u2218 f)\n\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n\nInfl< f \u2192 Infl f\nInfl< f \u2192 Infl< (suc \u2218 f)\n-}\n\n id-infl : Infl id\n id-infl = \u2115\u2264.refl\n\n \u2218-infl< : \u2200 {f g} \u2192 Infl< f \u2192 Infl< g \u2192 Infl< (f \u2218 g)\n \u2218-infl< inflf< inflg< = <-trans inflg< inflf<\n\n suc-infl< : Infl< suc\n suc-infl< = \u2115\u2264.refl\n\n suc-infl : Infl suc\n suc-infl = s\u2264s (\u2264-step \u2115\u2264.refl)\n\n nest-infl : \u2200 f (finfl : Infl f) n \u2192 Infl (nest n f)\n nest-infl f _ zero = \u2115\u2264.refl\n nest-infl f finfl (suc n) = \u2115\u2264.trans (nest-infl f finfl n) finfl\n\n nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n nest-infl< f finfl< zero = finfl<\n nest-infl< f finfl< (suc n) = <-trans (nest-infl< f finfl< n) finfl< \n\n-- See Function.NP.nest-Properties for more properties\nmodule fold-Props where\n\n fold-suc : \u2200 m n \u2192 fold m suc n \u2261 n + m\n fold-suc m zero = \u2261.refl\n fold-suc m (suc n) rewrite fold-suc m n = \u2261.refl\n\n fold-+ : \u2200 m n \u2192 fold 0 (_+_ m) n \u2261 n * m\n fold-+ m zero = \u2261.refl\n fold-+ m (suc n) rewrite fold-+ m n = \u2261.refl\n\n fold-* : \u2200 m n \u2192 fold 1 (_*_ m) n \u2261 m ^ n\n fold-* m zero = \u2261.refl\n fold-* m (suc n) rewrite fold-* m n = \u2261.refl\n\n{- TODO\n fold-^ : \u2200 m n \u2192 fold 1 (flip _^_ m) n \u2261 m \u2191\u27e8 4 \u27e9 n\n fold-^ m zero = \u2261.refl\n fold-^ m (suc n) rewrite fold-^ m n = ?\n-}\n\n cong-fold : \u2200 {A : Set} {f g : Endo A} (f\u2257g : f \u2257 g) {z} \u2192 fold z f \u2257 fold z g\n cong-fold eq zero = \u2261.refl\n cong-fold eq {z} (suc x) rewrite cong-fold eq {z} x = eq _\n\n private\n open \u2191-Props\n module Alt\u21912 where\n alt : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n alt a 0 = \u03bb b \u2192 a \u2191\u27e8 2 \u27e9 b\n alt a (suc n) = fold 1 (alt a n)\n\n alt-ok : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 alt a n\n alt-ok a zero = \u2261.sym \u2218 \u2191-\u2191\u2032 a 2\n alt-ok a (suc n) = cong-fold (alt-ok a n)\n\n{- TODO\n alt' : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 _\u2191\u27e8_\u27e9_ a (2 + n)\n alt' a zero b = \u2261.sym (\u2191-\u2191\u2032 a 2 _)\n alt' a (suc n) zero = \u2261.refl\n alt' a (suc n) (suc x) rewrite alt' a n (_\u21912+\u27e8_\u27e9_ a (suc n) x)\n = \u2261.cong (_\u2191\u27e8_\u27e9_ a (2 + n)) { fold 1 (fold (_*_ a) (fold 1) n) x} {a \u2191\u27e8 suc (suc (suc n)) \u27e9 x} (alt' a (suc n) x)\n-}\n\n open InflModule\n{-\n fold1-infl : \u2200 f \u2192 Infl f \u2192 Infl (fold 1 f)\n fold1-infl f finfl {zero} = {!z\u2264n!}\n fold1-infl f finfl {suc x} =\n x <\u27e8 {!s\u2264s (fold1-infl f finfl {x})!} \u27e9\n fold 1 f x <\u27e8 {!!} \u27e9\n {- f (fold 1 f x) \u2264\u27e8 finfl \u27e9\n f (fold 1 f x) \u2261\u27e8 \u2261.refl \u27e9 -}\n fold 1 f (1 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 f \u2218 suc)\n fold1+1-infl< f finfl< {zero} = finfl<\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + f (fold 1 f x) \u2264\u27e8 finfl< {f (fold 1 f x)} \u27e9\n f (suc (f (fold 1 f x))) \u2264\u27e8 {!!} \u27e9\n fold 1 f (2 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 (f \u2218 suc))\n fold1+1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 suc) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 suc) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+-infl< : \u2200 k f \u2192 Infl< (f \u2218\u2032 _+_ k) \u2192 Infl< (fold 1 (f \u2218\u2032 _+_ k))\n fold1+-infl< k f finfl< {zero} = s\u2264s z\u2264n\n fold1+-infl< k f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-infl< k f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 _+_ k) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 _+_ k) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n \n{-\n fold1+-infl< : \u2200 f \u2192 Infl+2< f \u2192 Infl+2< (fold 1 f)\n fold1+-infl< f finfl< {zero} _ = \u2115\u2264.refl\n fold1+-infl< f finfl< {suc zero} _ = {!!}\n fold1+-infl< f finfl< {suc (suc zero)} _ = {!!}\n fold1+-infl< f finfl< {suc (suc (suc x))} x>1 =\n 4 + x \u2264\u27e8 s\u2264s (fold1+-infl< f finfl< {suc (suc x)} (m\u2264m+n 2 x)) \u27e9\n 1 + fold 1 f (2 + x) \u2264\u27e8 finfl< {!!} \u27e9\n fold 1 f (3 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 f x \u2264\u27e8 finfl< \u27e9\n fold 1 f (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\nmodule ^-Props where\n open \u2261-Reasoning\n\n ^1 : \u2200 n \u2192 n ^ 1 \u2261 n\n ^1 zero = \u2261.refl\n ^1 (suc n) = \u2261.cong suc (proj\u2082 \u2115\u00b0.*-identity n)\n\n ^-+ : \u2200 b m n \u2192 b ^ (m + n) \u2261 b ^ m * b ^ n\n ^-+ b zero n = \u2261.sym (proj\u2082 \u2115\u00b0.+-identity (b ^ n))\n ^-+ b (suc m) n rewrite ^-+ b m n = \u2261.sym (\u2115\u00b0.*-assoc b (b ^ m) (b ^ n))\n\n ^-* : \u2200 b m n \u2192 b ^ (m * n) \u2261 (b ^ n) ^ m\n ^-* b zero n = \u2261.refl\n ^-* b (suc m) n = b ^ (n + m * n)\n \u2261\u27e8 ^-+ b n (m * n) \u27e9\n b ^ n * b ^ (m * n)\n \u2261\u27e8 \u2261.cong (_*_ (b ^ n)) (^-* b m n) \u27e9\n b ^ n * (b ^ n) ^ m \u220e\n\nack : \u2115 \u2192 \u2115 \u2192 \u2115\nack zero n = suc n\nack (suc m) zero = ack m (suc zero)\nack (suc m) (suc n) = ack m (ack (suc m) n)\n\nmodule ack-Props where\n lem\u2238 : \u2200 {m n} \u2192 m \u2264 n \u2192 m + (n \u2238 m) \u2261 n\n lem\u2238 z\u2264n = \u2261.refl\n lem\u2238 (s\u2264s {m} {n} m\u2264n) = \u2261.cong suc (lem\u2238 m\u2264n)\n\n -- n >= m \u2192 m + (n \u2238 m) \u2261 n\n -- n >= m \u2192 \u2203 k \u2192 n \u2261 m + k\n -- \u2203 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 m + ((m + k) \u2238 m) \u2261 m + k\n -- \u2200 k \u2192 m + k \u2261 m + k\n\n -- 3 \u2264 x \u2192 P x \u2192 \u2203 \u03bb k \u2192 P (3 + k)\n\n open InflModule\n\n fold1+-inflT< : \u2200 {z} \u2192 InflT< (fold (suc z))\n fold1+-inflT< _ {zero} = s\u2264s z\u2264n\n fold1+-inflT< {z} {f} finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-inflT< {z} {f} finfl< {x}) \u27e9\n 1 + fold (suc z) f x \u2264\u27e8 finfl< \u27e9\n f (fold (suc z) f x) \u2261\u27e8 \u2261.refl \u27e9\n fold (suc z) f (1 + x)\n \u220e\n where open \u2264-Reasoning\n\n Mon : (f : \u2115 \u2192 \u2115) \u2192 Set\n Mon f = \u2200 {x y} \u2192 x \u2264 y \u2192 f x \u2264 f y\n open InflModule\n\n\n \u2115-ind : \u2200 (P : \u2115 \u2192 Set) \u2192 P zero \u2192 (\u2200 {n} \u2192 P n \u2192 P (suc n)) \u2192 \u2200 {n} \u2192 P n\n \u2115-ind P P0 PS {zero} = P0\n \u2115-ind P P0 PS {suc n} = PS (\u2115-ind P P0 PS)\n\n open fold-Props\n fold-infl< : \u2200 {f g} \u2192 Infl< f \u2192 InflT< g \u2192 \u2200 {n} \u2192 Infl< (fold f g n)\n fold-infl< {f} {g} inflf< inflTg< {n} = \u2115-ind (Infl< \u2218 fold f g) inflf< inflTg< {n}\n\n fold-mon : \u2200 {f} (fmon : Mon f) (finfl< : Infl< f) {z} \u2192 Mon (fold z f)\n fold-mon fmon finfl< {_} {y = 0} z\u2264n = \u2115\u2264.refl\n fold-mon {f} fmon finfl< {z} {y = suc n} z\u2264n = z \u2264\u27e8 \u2264-step \u2115\u2264.refl \u27e9\n suc z \u2264\u27e8 nest-infl< f finfl< n {z} \u27e9\n fold z f (suc n) \u2261\u27e8 \u2261.refl \u27e9\n fold z f (suc n) \u220e\n where open \u2264-Reasoning\n fold-mon fmon finfl< (s\u2264s m\u2264n) = fmon (fold-mon fmon finfl< m\u2264n)\n\n MonT : Endo (Endo \u2115) \u2192 Set\n MonT g = \u2200 {f} \u2192 Mon f \u2192 Infl< f \u2192 Mon (g f)\n\n fold-mon' : \u2200 {f g} \u2192 Mon f \u2192 Infl< f \u2192 MonT g \u2192 InflT< g \u2192 \u2200 {n} \u2192 Mon (fold f g n)\n fold-mon' {f} {g} mon-f infl-f mont-g inflTg< {n} = go n where\n go : \u2200 n \u2192 Mon (fold f g n)\n go zero = mon-f\n go (suc n) = mont-g (go n) (fold-infl< infl-f inflTg< {n})\n\n\n -- +-fold : \u2200 a b \u2192 a + b \u2261 fold a suc b\n -- *-fold : \u2200 a b \u2192 a * b \u2261 fold 0 (_+_ a) b\n\n 1\u22641+a^b : \u2200 a b \u2192 1 \u2264 (1 + a) ^ b\n 1\u22641+a^b a zero = s\u2264s z\u2264n\n 1\u22641+a^b a (suc b) = 1\u22641+a^b a b +-mono z\u2264n\n\n 1+a^-mon : \u2200 {a} \u2192 Mon (_^_ (1 + a))\n 1+a^-mon {a} (z\u2264n {b}) = 1\u22641+a^b a b\n 1+a^-mon {a} (s\u2264s {m} {n} m\u2264n)\n = (1 + a) ^ (1 + m) \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ m + a * (1 + a) ^ m \u2264\u27e8 (1+a^-mon m\u2264n) +-mono ((a \u220e) *-mono (1+a^-mon m\u2264n)) \u27e9\n (1 + a) ^ n + a * (1 + a) ^ n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ (1 + n) \u220e\n where open \u2264-Reasoning\n\n postulate\n \u21913+ : \u2200 a n b \u2192 a \u2191\u27e8 3 + n \u27e9 b \u2261 fold (_^_ a) (fold 1) n b\n -- mon\u21913+'' : \u2200 a b \u2192 Mon (\u03bb n \u2192 fold (_^_ a) (fold 1) n b)\n\n mon\u21913+' : \u2200 b \u2192 Mon (\u03bb n \u2192 fold (_^_ 2) (fold 1) n (3 + b))\n -- mon\u21913+' b = {!!}\n\n mon\u21913+ : \u2200 b \u2192 Mon (\u03bb n \u2192 2 \u2191\u27e8 3 + n \u27e9 (3 + b))\n mon\u21913+ b {m} {n} rewrite \u21913+ 2 m (3 + b) | \u21913+ 2 n (3 + b) = mon\u21913+' b\n\n lem2^3 : \u2200 n \u2192 2 ^ 3 \u2264 2 ^ (3 + n)\n lem2^3 n = 1+a^-mon {1} {3} {3 + n} (s\u2264s (s\u2264s (s\u2264s z\u2264n)))\n\n open \u2191-Props\n lem>=3 : \u2200 m n \u2192 3 \u2264 2 \u2191\u27e8 3 + m \u27e9 (3 + n)\n lem>=3 m n = 3 \u2264\u27e8 s\u2264s (s\u2264s (s\u2264s z\u2264n)) \u27e9\n 2 \u2191\u27e8 3 \u27e9 3 \u2261\u27e8 \u2191\u2083-^ 2 3 \u27e9\n 2 ^ 3 \u2264\u27e8 lem2^3 n \u27e9\n 2 ^ (3 + n) \u2261\u27e8 \u2261.sym (\u2191\u2083-^ 2 (3 + n)) \u27e9\n 2 \u2191\u27e8 3 \u27e9 (3 + n) \u2264\u27e8 mon\u21913+ n z\u2264n \u27e9\n 2 \u2191\u27e8 3 + m \u27e9 (3 + n) \u220e\n where open \u2264-Reasoning\n\n 2* = _*_ 2\n\n -- fold 2* n 1 \u2261 2 ^ n\n\n -- nest-* : \u2200 m n \u2192 nest (m * n) f \u2257 nest m (nest n f)\n\n nest-2* : \u2200 m n \u2192 nest (m * n) (fold 1) 2* \u2261 nest m (nest n (fold 1)) 2*\n nest-2* m n = nest-Properties.nest-* (fold 1) m n 2*\n\n{-\n lem3\u2264 : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 2\n lem3\u2264 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264 (suc n) = \n 3 \u2264\u27e8 {!!} \u27e9\n fold 2* f1 (1 + n) 2 \u220e\n where open \u2264-Reasoning\n f1 = fold 1\n\n lem3\u2264' : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 3\n lem3\u2264' 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264' (suc n) = \n 3 \u2264\u27e8 lem3\u2264 n \u27e9\n fold 2* (fold 1) n 2 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) n (fold 2* (fold 1) 3 1) \u2261\u27e8 {!!} \u27e9\n{- \u2261\u27e8 nest-Properties.nest-* {!2*!} {!!} {!!} {!!} \u27e9\n fold 2* (fold 1) (3 * n) 1 \u2261\u27e8 {!!} \u27e9\n-}\n fold 1 (fold 2* (fold 1) n) 3 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) (1 + n) 3 \u220e\n where open \u2264-Reasoning\n-}\n---\n\n{-\nfold 2* (fold 1) (3 * n) 1 \u2264\n\nfold 2* (fold 1) n (fold 2* (fold 1) n (fold 2* (fold 1) n 1)) \u2264\n\nfold 1 (fold 2* (fold 1) n) 3 \u2264\nfold 2* (fold 1) (1 + n) 3\n-}\n\n lem>=3'' : \u2200 m n \u2192 3 \u2264 2 \u2191\u27e8 suc m \u27e9 (3 + n)\n lem>=3'' zero n = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n lem>=3'' (suc zero) n rewrite \u2191\u2082-* 2 (3 + n) = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n lem>=3'' (suc (suc m)) n = lem>=3 m n\n\n lem2221 : \u2200 m \u2192 2 \u2261 2 \u2191\u27e8 2 + m \u27e9 1\n lem2221 zero = \u2261.refl\n lem2221 (suc m) = lem2221 m\n\n lem4212 : \u2200 m \u2192 4 \u2261 2 \u2191\u27e8 1 + m \u27e9 2\n lem4212 zero = \u2261.refl\n lem4212 (suc m) = 4 \u2261\u27e8 lem4212 m \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 2 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 (1 + m)) (lem2221 m) \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 (2 \u2191\u27e8 2 + m \u27e9 1) \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 2 + m \u27e9 2 \u220e\n where open \u2261-Reasoning\n\n ack-\u2191 : \u2200 m n \u2192 3 + ack m n \u2261 2 \u2191\u27e8 m \u27e9 (3 + n)\n ack-\u2191 zero n = \u2261.refl\n ack-\u2191 (suc m) zero = 3 + ack (suc m) 0 \u2261\u27e8 ack-\u2191 m 1 \u27e9\n 2 \u2191\u27e8 m \u27e9 4 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 m) (lem4212 m) \u27e9\n 2 \u2191\u27e8 suc m \u27e9 3 \u220e\n where open \u2261-Reasoning\n ack-\u2191 (suc m) (suc n) = 3 + ack (suc m) (suc n)\n \u2261\u27e8 \u2261.refl \u27e9\n 3 + ack m (ack (suc m) n)\n \u2261\u27e8 \u2261.refl \u27e9\n 3 + ack m ((3 + ack (suc m) n) \u2238 3)\n \u2261\u27e8 \u2261.cong (\u03bb x \u2192 3 + ack m (x \u2238 3)) (ack-\u2191 (suc m) n) \u27e9\n 3 + ack m (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3)\n \u2261\u27e8 ack-\u2191 m (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3) \u27e9\n 2 \u2191\u27e8 m \u27e9 (3 + (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3))\n \u2261\u27e8 \u2261.cong (\u03bb x \u2192 2 \u2191\u27e8 m \u27e9 x) (lem\u2238 (lem>=3'' m n)) \u27e9\n 2 \u2191\u27e8 m \u27e9 (2 \u2191\u27e8 suc m \u27e9 (3 + n))\n \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 suc m \u27e9 (4 + n) \u220e \n where open \u2261-Reasoning\n\n postulate\n 1+a^-infl< : \u2200 {a} \u2192 Infl< (_^_ (1 + a))\n\n -- 2+a*1+b-infl< : \u2200 a \u2192 Infl< (\u03bb x \u2192 (2 + a) * (1 + x))\n -- \u2200 a b \u2192 b < (2 + a) * b\n -- fold-a*-fold1 : \u2200 {n a} \u2192 Infl< (_\u21912+\u27e8_\u27e9_ (2 + a) n)\n fold-a^-fold1 : \u2200 {n a} \u2192 Infl< (fold (_^_ (1 + a)) (fold 1) n)\n fold-a^-fold1 {n} = fold-infl< 1+a^-infl< fold1+-inflT< {n}\n\n \u21913+-mon : \u2200 a n \u2192 Mon (fold (_^_ (1 + a)) (fold 1) n)\n \u21913+-mon a n = fold-mon' 1+a^-mon 1+a^-infl< (\u03bb \u03b7\u2081 \u03b7\u2082 \u2192 fold-mon \u03b7\u2081 \u03b7\u2082) fold1+-inflT< {n}\n\n{-\n open \u2191-Props\n\n -*\u27e81+n\u27e9-infl : \u2200 n \u2192 Infl (_*_ (1 + n))\n -*\u27e81+n\u27e9-infl n = {!!}\n\n lem121 : \u2200 a b \u2192 b < (1 + a) \u2191\u27e8 2 \u27e9 (1 + b)\n lem121 a b rewrite \u2191\u2082-* (1 + a) (1 + b)\n = 1 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (1 + a) * (1 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (1 + a) (1 + b) \u27e9\n (1 + b) * (1 + a)\n \u220e\n where open \u2264-Reasoning\n\n -- \u2200 a b \u2192 b < (2 + a) * b\n lem222 : \u2200 a b \u2192 b < (2 + a) \u2191\u27e8 2 \u27e9 (2 + b)\n lem222 a b rewrite \u2191\u2082-* (2 + a) (2 + b)\n = 1 + b\n \u2264\u27e8 suc-infl \u27e9\n 2 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (2 + a) * (2 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (2 + a) (2 + b) \u27e9\n (2 + b) * (2 + a)\n \u220e\n where open \u2264-Reasoning\n-}\n{-\n open fold-Props\n\n module Foo a where\n f = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b\n f-lem : \u2200 n \u2192 f (suc n) \u2257 fold 1 (f n)\n f-lem n x = \u2261.refl\n\n f2 = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n f2-lem : \u2200 n \u2192 f2 (suc n) \u2257 fold 1 (f2 n)\n f2-lem n x = {!!}\n\n -- b < (1 + a) * (1 + b)\n\n -- nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n infl3< : \u2200 a n \u2192 Infl< (\u03bb b \u2192 (1 + a) \u21912+\u27e8 n \u27e9 (1 + b))\n infl3< a zero {b} = lem121 a b\n infl3< a (suc n) {b}\n = 1 + b\n -- fold1-infl< : \u2200 f \u2192 Infl< f \u2192 \u2200 x \u2192 x < fold 1 f x\n \u2264\u27e8 {!fold1f2-infl< {_}!} \u27e9\n fold 1 f1 b\n \u2264\u27e8 {!!} \u27e9\n{-\n \u2264\u27e8 fold1-infl< (f 0) (\u03bb {x} \u2192 {!infl3< a n {x}!}) {{!!}} \u27e9\n {!!}\n \u2264\u27e8 {!!} \u27e9\n-}\n{-\n 1 + b\n (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n \u2264\u27e8 {!nest-infl< (\u03bb b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b) ? b {(2 + a) \u21912+\u27e8 n \u27e9 (2 + b)}!} \u27e9\n fold (f 0 1) (f 0) (1 + b)\n \u2264\u27e8 {!\u2115\u2264.reflexive (\u2261.cong (\u03bb g \u2192 g 1) (nest-Properties.nest-+ (f 0) 1 (1 + b)))!} \u27e9\n-}\n fold 1 (f 0) (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n f 1 (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) \u21912+\u27e8 suc n \u27e9 (1 + b)\n \u220e\n\n where\n f = \u03bb k b \u2192 (1 + a) \u21912+\u27e8 k + n \u27e9 b\n f1 = f 0 \u2218 suc\n\n -- fold1f2-infl< : Infl< (fold 1 f2)\n -- fold1f2-infl< = fold1-infl< f2 {!(\u03bb b \u2192 infl3< a n b ?)!}\n\n fold1f1-infl<' : Infl< (fold 1 f1)\n fold1f1-infl<' = {!fold1+1-infl< (f 0) {!infl3< a n!}!}\n lem : \u2200 x \u2192 f 0 (1 + x) < f 0 (f 0 x)\n lem x = {!!}\n postulate\n finfl : Infl< (f 0)\n open \u2264-Reasoning\n-}\n\n_\u21d1\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\na \u21d1\u27e8 n , k \u27e9 b = fold k f n\n module M\u21d1 where \n f : \u2115 \u2192 \u2115\n f x = a \u2191\u27e8 2 + x \u27e9 b\n\nmodule \u21d1-Props where\n _\u21d1\u2032\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\n a \u21d1\u2032\u27e8 n\u2080 , k \u27e9 b = g n\u2080\n module M\u21d1\u2032 where\n h : \u2115 \u2192 \u2115\n h x = a \u2191\u27e8 2 + x \u27e9 b\n\n g : \u2115 \u2192 \u2115\n g 0 = k\n g (suc n) = h (g n)\n\n g-nest : \u2200 n \u2192 g n \u2261 h $\u27e8 n \u27e9 k\n g-nest zero = \u2261.refl\n g-nest (suc n) rewrite g-nest n = \u2261.refl\n\n \u21d1\u2080 : \u2200 a k b \u2192 a \u21d1\u27e8 0 , k \u27e9 b \u2261 k\n \u21d1\u2080 _ _ _ = \u2261.refl\n\n \u21d1\u2081 : \u2200 a k b \u2192 a \u21d1\u27e8 1 , k \u27e9 b \u2261 a \u2191\u27e8 2 + k \u27e9 b\n \u21d1\u2081 _ _ _ = \u2261.refl\n\n \u21d1-\u21d1\u2032 : \u2200 a n k b \u2192 a \u21d1\u27e8 n , k \u27e9 b \u2261 a \u21d1\u2032\u27e8 n , k \u27e9 b\n \u21d1-\u21d1\u2032 a n k b rewrite M\u21d1\u2032.g-nest a n k b n = \u2261.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Graham%27s_number\nGraham's-number : \u2115\nGraham's-number = 3 \u21d1\u27e8 64 , 4 \u27e9 3\n -- = (f\u2218\u2076\u2074\u2026\u2218f)4\n -- where f x = 3 \u2191\u27e8 2 + x \u27e9 3\n\n{-\nmodule GeneralisedArithmetic {a} {A : Set a} (0# : A) (1+ : A \u2192 A) where\n\n 1# : A\n 1# = 1+ 0#\n\n open Data.Nat.GeneralisedArithmetic 0# 1+ public\n\n exp : (* : A \u2192 A \u2192 A) \u2192 (\u2115 \u2192 A \u2192 A)\n exp _*_ n b = fold 1# (\u03bb s \u2192 b * s) n\n-}\n -- hyperop a n b = fold exp\n\nmodule == where\n _\u2248_ : (m n : \u2115) \u2192 Set\n m \u2248 n = T (m == n)\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {zero} {zero} _ = id\n subst P {suc _} {suc _} p = subst (P \u2218 suc) p\n subst _ {zero} {suc _} ()\n subst _ {suc _} {zero} ()\n\n sound : \u2200 m n \u2192 T (m == n) \u2192 m \u2261 n\n sound m n p = subst (_\u2261_ m) p \u2261.refl\n\n refl : Reflexive _\u2248_\n refl {zero} = _\n refl {suc n} = refl {n}\n\n sym : Symmetric _\u2248_\n sym {m} {n} eq rewrite sound m n eq = refl {n}\n\n trans : Transitive _\u2248_\n trans {m} {n} {o} m\u2248n n\u2248o rewrite sound m n m\u2248n | sound n o n\u2248o = refl {o}\n\n setoid : Setoid _ _\n setoid = record { Carrier = \u2115; _\u2248_ = _\u2248_\n ; isEquivalence =\n record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} } }\n\n open Setoid setoid public hiding (refl; sym; trans; _\u2248_)\n\n{-\ndata _`\u2264?`_\u219d_ : (m n : \u2115) \u2192 Dec (m \u2264 n) \u2192 Set where\n z\u2264?n : \u2200 {n} \u2192 zero `\u2264?` n \u219d yes z\u2264n\n s\u2264?z : \u2200 {m} \u2192 suc m `\u2264?` zero \u219d no \u03bb()\n s\u2264?s-yes : \u2200 {m n m\u2264n} \u2192 m `\u2264?` n \u219d yes m\u2264n \u2192 suc m `\u2264?` suc n \u219d yes (s\u2264s m\u2264n)\n s\u2264?s-no : \u2200 {m n m\u2270n} \u2192 m `\u2264?` n \u219d no m\u2270n \u2192 suc m `\u2264?` suc n \u219d no (m\u2270n \u2218 \u2264-pred)\n\n`\u2264?`-complete : \u2200 m n \u2192 m `\u2264?` n \u219d (m \u2264? n)\n`\u2264?`-complete zero n = z\u2264?n\n`\u2264?`-complete (suc n) zero = {!s\u2264?z!}\n`\u2264?`-complete (suc m) (suc n) with m \u2264? n | `\u2264?`-complete m n\n... | yes q | r = s\u2264?s-yes r\n... | no q | r = s\u2264?s-no {!!}\n-}\n\n_<=_ : (x y : \u2115) \u2192 Bool\nzero <= _ = true\nsuc _ <= zero = false\nsuc m <= suc n = m <= n\n\nmodule <= where\n sound : \u2200 m n \u2192 T (m <= n) \u2192 m \u2264 n\n sound zero _ _ = z\u2264n\n sound (suc m) (suc n) p = s\u2264s (sound m n p)\n sound (suc m) zero ()\n\n complete : \u2200 {m n} \u2192 m \u2264 n \u2192 T (m <= n)\n complete z\u2264n = _\n complete (s\u2264s m\u2264n) = complete m\u2264n\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (Props.\u2264-steps 1 1+n\u2264m)\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Nat.NP where\n\nimport Algebra\nopen import Data.Nat public hiding (module GeneralisedArithmetic; module \u2264-Reasoning; fold)\nopen import Data.Nat.Properties as Props\nopen import Data.Nat.Logical\nopen import Data.Bool\nopen import Data.Product using (proj\u2081; proj\u2082; \u2203; _,_)\nopen import Data.Empty using (\u22a5-elim)\nopen import Function.NP\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_; module \u2261-Reasoning)\n\nmodule \u2115\u00b0 = Algebra.CommutativeSemiring Props.commutativeSemiring\nmodule \u2115cmp = StrictTotalOrder Props.strictTotalOrder\nmodule \u2115\u2264 = DecTotalOrder decTotalOrder\nmodule \u2294\u00b0 = Algebra.CommutativeSemiringWithoutOne \u2294-\u2293-0-commutativeSemiringWithoutOne\n\nmodule \u2264-Reasoning where\n open Preorder-Reasoning \u2115\u2264.preorder public renaming (_\u223c\u27e8_\u27e9_ to _\u2264\u27e8_\u27e9_)\n infixr 2 _\u2261\u27e8_\u27e9_\n _\u2261\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x \u2261 y \u2192 y \u2264 z \u2192 x \u2264 z\n _ \u2261\u27e8 \u2261.refl \u27e9 p = p\n infixr 2 _<\u27e8_\u27e9_\n _<\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x < y \u2192 y \u2264 z \u2192 x < z\n x <\u27e8 p \u27e9 q = suc x \u2264\u27e8 p \u27e9 q\n\nsuc-injective : \u2200 {n m : \u2115} \u2192 (suc n \u2236 \u2115) \u2261 suc m \u2192 n \u2261 m\nsuc-injective = \u2261.cong pred\n\nfold : \u2200 {a} {A : Set a} \u2192 A \u2192 Endo A \u2192 \u2115 \u2192 A\nfold x f n = nest n f x\n\n2*_ : \u2115 \u2192 \u2115\n2* x = x + x\n\n_==_ : (x y : \u2115) \u2192 Bool\nzero == zero = true\nzero == suc _ = false\nsuc _ == zero = false\nsuc m == suc n = m == n\n\nassoc-comm : \u2200 x y z \u2192 x + (y + z) \u2261 y + (x + z)\nassoc-comm x y z rewrite \u2261.sym (\u2115\u00b0.+-assoc x y z)\n | \u2115\u00b0.+-comm x y\n | \u2115\u00b0.+-assoc y x z = \u2261.refl\n\n2*\u2032_ : \u2115 \u2192 \u2115\n2*\u2032_ = fold 0 (suc \u2218\u2032 suc)\n\n2*\u2032-spec : \u2200 n \u2192 2*\u2032 n \u2261 2* n\n2*\u2032-spec zero = \u2261.refl\n2*\u2032-spec (suc n) rewrite 2*\u2032-spec n | assoc-comm 1 n n = \u2261.refl\n\ndist : \u2115 \u2192 \u2115 \u2192 \u2115\ndist zero y = y\ndist x zero = x\ndist (suc x) (suc y) = dist x y\n\ndist-refl : \u2200 x \u2192 dist x x \u2261 0\ndist-refl zero = \u2261.refl\ndist-refl (suc x) rewrite dist-refl x = \u2261.refl\n\ndist-0\u2261id : \u2200 x \u2192 dist 0 x \u2261 x\ndist-0\u2261id _ = \u2261.refl\n\ndist-x-x+y\u2261y : \u2200 x y \u2192 dist x (x + y) \u2261 y\ndist-x-x+y\u2261y zero y = \u2261.refl\ndist-x-x+y\u2261y (suc x) y = dist-x-x+y\u2261y x y\n\ndist-sym : \u2200 x y \u2192 dist x y \u2261 dist y x\ndist-sym zero zero = \u2261.refl\ndist-sym zero (suc y) = \u2261.refl\ndist-sym (suc x) zero = \u2261.refl\ndist-sym (suc x) (suc y) = dist-sym x y\n\ndist-x+ : \u2200 x y z \u2192 dist (x + y) (x + z) \u2261 dist y z\ndist-x+ zero y z = \u2261.refl\ndist-x+ (suc x) y z = dist-x+ x y z\n\ndist-2* : \u2200 x y \u2192 dist (2* x) (2* y) \u2261 2* dist x y\ndist-2* zero y = \u2261.refl\ndist-2* (suc x) zero = \u2261.refl\ndist-2* (suc x) (suc y) rewrite assoc-comm x 1 x\n | assoc-comm y 1 y = dist-2* x y\n\ndist-asym-def : \u2200 {x y} \u2192 x \u2264 y \u2192 x + dist x y \u2261 y\ndist-asym-def z\u2264n = \u2261.refl\ndist-asym-def (s\u2264s pf) = \u2261.cong suc (dist-asym-def pf)\n\ndist-sym-wlog : \u2200 (f : \u2115 \u2192 \u2115) \u2192 (\u2200 x k \u2192 dist (f x) (f (x + k)) \u2261 f k) \u2192 \u2200 x y \u2192 dist (f x) (f y) \u2261 f (dist x y)\ndist-sym-wlog f pf x y with compare x y\ndist-sym-wlog f pf x .(suc (x + k)) | less .x k with pf x (suc k)\n... | q rewrite assoc-comm x 1 k | q | \u2261.sym (assoc-comm x 1 k) | dist-x-x+y\u2261y x (suc k) = \u2261.refl\ndist-sym-wlog f pf .y y | equal .y with pf y 0\n... | q rewrite \u2115\u00b0.+-comm y 0 | dist-refl y = q\ndist-sym-wlog f pf .(suc (y + k)) y | greater .y k with pf y (suc k)\n... | q rewrite assoc-comm 1 y k | dist-sym (y + suc k) y | dist-x-x+y\u2261y y (suc k) | dist-sym (f (y + suc k)) (f y) = q\n\ndist-x* : \u2200 x y z \u2192 dist (x * y) (x * z) \u2261 x * dist y z\ndist-x* x = dist-sym-wlog (_*_ x) pf\n where pf : \u2200 a k \u2192 dist (x * a) (x * (a + k)) \u2261 x * k\n pf a k rewrite proj\u2081 \u2115\u00b0.distrib x a k = dist-x-x+y\u2261y (x * a) _\n\n\u27e82^_\u27e9* : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e82^ zero \u27e9* = id\n\u27e82^ suc k \u27e9* = 2*_ \u2218 \u27e82^ k \u27e9*\n\n2*-distrib : \u2200 x y \u2192 2* x + 2* y \u2261 2* (x + y) \n2*-distrib = solve 2 (\u03bb x y \u2192 2:* x :+ 2:* y := 2:* (x :+ y)) \u2261.refl\n where open SemiringSolver\n 2:* : \u2200 {n} \u2192 Polynomial n \u2192 Polynomial n\n 2:* x = x :+ x\n\n2^*-distrib : \u2200 k x y \u2192 \u27e82^ k \u27e9* (x + y) \u2261 \u27e82^ k \u27e9* x + \u27e82^ k \u27e9* y\n2^*-distrib zero x y = \u2261.refl\n2^*-distrib (suc k) x y rewrite 2^*-distrib k x y = \u2261.sym (2*-distrib (\u27e82^ k \u27e9* x) (\u27e82^ k \u27e9* y))\n\n2^*-2*-comm : \u2200 k x \u2192 \u27e82^ k \u27e9* (2* x) \u2261 2* (\u27e82^ k \u27e9* x)\n2^*-2*-comm k x = 2^*-distrib k x x\n\ndist-2^* : \u2200 x y z \u2192 dist (\u27e82^ x \u27e9* y) (\u27e82^ x \u27e9* z) \u2261 \u27e82^ x \u27e9* (dist y z)\ndist-2^* x y z = dist-sym-wlog \u27e82^ x \u27e9* (pf x) y z\n where pf : \u2200 x a k \u2192 dist (\u27e82^ x \u27e9* a) (\u27e82^ x \u27e9* (a + k)) \u2261 \u27e82^ x \u27e9* k\n pf x a k rewrite 2^*-distrib x a k = dist-x-x+y\u2261y (\u27e82^ x \u27e9* a) (\u27e82^ x \u27e9* k)\n\n2*-mono : \u2200 {a b} \u2192 a \u2264 b \u2192 2* a \u2264 2* b\n2*-mono pf = pf +-mono pf\n\n2^*-mono : \u2200 k {a b} \u2192 a \u2264 b \u2192 \u27e82^ k \u27e9* a \u2264 \u27e82^ k \u27e9* b\n2^*-mono zero pf = pf\n2^*-mono (suc k) pf = 2*-mono (2^*-mono k pf)\n\n2*-mono\u2032 : \u2200 {a b} \u2192 2* a \u2264 2* b \u2192 a \u2264 b\n2*-mono\u2032 {zero} pf = z\u2264n\n2*-mono\u2032 {suc a} {zero} ()\n2*-mono\u2032 {suc a} {suc b} pf rewrite assoc-comm a 1 a\n | assoc-comm b 1 b = s\u2264s (2*-mono\u2032 (\u2264-pred (\u2264-pred pf)))\n\n2^*-mono\u2032 : \u2200 k {a b} \u2192 \u27e82^ k \u27e9* a \u2264 \u27e82^ k \u27e9* b \u2192 a \u2264 b\n2^*-mono\u2032 zero = id\n2^*-mono\u2032 (suc k) = 2^*-mono\u2032 k \u2218 2*-mono\u2032\n\n2^-comm : \u2200 x y z \u2192 \u27e82^ x \u27e9* (\u27e82^ y \u27e9* z) \u2261 \u27e82^ y \u27e9* (\u27e82^ x \u27e9* z)\n2^-comm zero y z = \u2261.refl\n2^-comm (suc x) y z rewrite 2^-comm x y z = \u2261.sym (2^*-2*-comm y (\u27e82^ x \u27e9* z))\n\n2^-+ : \u2200 x y z \u2192 \u27e82^ x \u27e9* (\u27e82^ y \u27e9* z) \u2261 \u27e82^ x + y \u27e9* z\n2^-+ zero y z = \u2261.refl\n2^-+ (suc x) y z = \u2261.cong 2*_ (2^-+ x y z)\n\n2*\u2032-inj : \u2200 {m n} \u2192 \u27e6\u2115\u27e7 (2*\u2032 m) (2*\u2032 n) \u2192 \u27e6\u2115\u27e7 m n\n2*\u2032-inj {zero} {zero} _ = zero\n2*\u2032-inj {zero} {suc _} ()\n2*\u2032-inj {suc _} {zero} ()\n2*\u2032-inj {suc m} {suc n} (suc (suc p)) = suc (2*\u2032-inj p)\n\n2*-inj : \u2200 {m n} \u2192 2* m \u2261 2* n \u2192 m \u2261 n\n2*-inj {m} {n} p rewrite \u2261.sym (2*\u2032-spec m)\n | \u2261.sym (2*\u2032-spec n)\n = \u27e6\u2115\u27e7\u21d2\u2261 (2*\u2032-inj (\u27e6\u2115\u27e7\u02e2.reflexive p))\n\n2^-inj : \u2200 k {m n} \u2192 \u27e82^ k \u27e9* m \u2261 \u27e82^ k \u27e9* n \u2192 m \u2261 n\n2^-inj zero eq = eq\n2^-inj (suc k) eq = 2^-inj k (2*-inj eq)\n\n{-\npost--ulate\n dist-sum : \u2200 x y z \u2192 dist x y + dist y z \u2264 dist x z\n dist-\u2264 : \u2200 x y \u2192 dist x y \u2264 x\n dist-mono\u2081 : \u2200 x y z t \u2192 x \u2264 y \u2192 dist z t \u2264 dist (x + z) (y + t)\n-}\n\n-- Haskell\n-- let dist x y = abs (x - y)\n-- quickCheck (forAll (replicateM 3 (choose (0,100))) (\\ [x,y,z] -> dist (x * y) (x * z) == x * dist y z))\n\ninfix 8 _^_\n_^_ : \u2115 \u2192 \u2115 \u2192 \u2115\nb ^ zero = 1\nb ^ suc n = b * b ^ n\n\n2*-spec : \u2200 n \u2192 2* n \u2261 2 * n\n2*-spec n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\n\n2^_ : \u2115 \u2192 \u2115\n2^ n = \u27e82^ n \u27e9* 1\n\n2^-spec : \u2200 n \u2192 2^ n \u2261 2 ^ n\n2^-spec zero = \u2261.refl\n2^-spec (suc n) rewrite 2^-spec n | 2*-spec (2 ^ n) = \u2261.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Hyper_operator\n_\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\na \u2191\u27e8 suc n \u27e9 (suc b) = a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\na \u2191\u27e8 0 \u27e9 b = suc b\na \u2191\u27e8 1 \u27e9 0 = a\na \u2191\u27e8 2 \u27e9 0 = 0\na \u2191\u27e8 suc (suc (suc n)) \u27e9 0 = 1\n\nmodule \u2191-Props where\n \u2191-fold : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 b \u2261 fold (a \u2191\u27e8 suc n \u27e9 0) (_\u2191\u27e8_\u27e9_ a n) b\n \u2191-fold a n zero = \u2261.refl\n \u2191-fold a n (suc b) = \u2261.cong (_\u2191\u27e8_\u27e9_ a n) (\u2191-fold a n b)\n\n \u2191\u2080-+ : \u2200 a b \u2192 a \u2191\u27e8 0 \u27e9 b \u2261 1 + b\n \u2191\u2080-+ a b = \u2261.refl\n\n \u2191\u2081-+ : \u2200 a b \u2192 a \u2191\u27e8 1 \u27e9 b \u2261 b + a\n \u2191\u2081-+ a zero = \u2261.refl\n \u2191\u2081-+ a (suc b) = \u2261.cong suc (\u2191\u2081-+ a b)\n\n \u2191\u2082-* : \u2200 a b \u2192 a \u2191\u27e8 2 \u27e9 b \u2261 b * a\n \u2191\u2082-* a zero = \u2261.refl\n \u2191\u2082-* a (suc b) rewrite \u2191\u2082-* a b\n | \u2191\u2081-+ a (b * a)\n | \u2115\u00b0.+-comm (b * a) a\n = \u2261.refl\n\n -- fold 1 (_*_ a) b \u2261 a ^ b\n \u2191\u2083-^ : \u2200 a b \u2192 a \u2191\u27e8 3 \u27e9 b \u2261 a ^ b\n \u2191\u2083-^ a zero = \u2261.refl\n \u2191\u2083-^ a (suc b) rewrite \u2191\u2083-^ a b\n | \u2191\u2082-* a (a ^ b)\n | \u2115\u00b0.*-comm (a ^ b) a\n = \u2261.refl\n\n _\u2191\u27e8_\u27e90 : \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u27e8 0 \u27e90 = 1\n a \u2191\u27e8 1 \u27e90 = a\n a \u2191\u27e8 2 \u27e90 = 0\n a \u2191\u27e8 suc (suc (suc n)) \u27e90 = 1\n\n _`\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _`\u2191\u27e8_\u27e9_ a 0 = suc\n _`\u2191\u27e8_\u27e9_ a (suc n) = fold (a \u2191\u27e8 suc n \u27e90) (_`\u2191\u27e8_\u27e9_ a n)\n\n \u2191-ind-rule : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 (suc b) \u2261 a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\n \u2191-ind-rule a n b = \u2261.refl\n\n _\u2191\u2032\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u2032\u27e8 0 \u27e9 b = suc b\n a \u2191\u2032\u27e8 1 \u27e9 b = a + b\n a \u2191\u2032\u27e8 2 \u27e9 b = a * b\n a \u2191\u2032\u27e8 3 \u27e9 b = a ^ b\n a \u2191\u2032\u27e8 suc (suc (suc (suc n))) \u27e9 b = a \u2191\u27e8 4 + n \u27e9 b\n\n \u2191-\u2191\u2032 : \u2200 a n b \u2192 a \u2191\u27e8 n \u27e9 b \u2261 a \u2191\u2032\u27e8 n \u27e9 b\n \u2191-\u2191\u2032 a 0 b = \u2261.refl\n \u2191-\u2191\u2032 a 1 b = \u2261.trans (\u2191\u2081-+ a b) (\u2115\u00b0.+-comm b a)\n \u2191-\u2191\u2032 a 2 b = \u2261.trans (\u2191\u2082-* a b) (\u2115\u00b0.*-comm b a)\n \u2191-\u2191\u2032 a 3 b = \u2191\u2083-^ a b\n \u2191-\u2191\u2032 a (suc (suc (suc (suc _)))) b = \u2261.refl\n\n _\u21912+\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _\u21912+\u27e8_\u27e9_ a = fold (_*_ a) (fold 1)\n\nmodule InflModule where\n -- Inflationary functions\n Infl< : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl< f = \u2200 {x} \u2192 x < f x\n\n Infl : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl f = Infl< (suc \u2218 f)\n\n InflT< : (f : Endo (Endo \u2115)) \u2192 Set\n InflT< f = \u2200 {g} \u2192 Infl< g \u2192 Infl< (f g)\n\n{-\n\u2200 {x} \u2192 x \u2264 f x\n\n\n 1 + x \u2264 1 + f x\n x < 1 + f x\n x < (suc \u2218 f) x\n Infl< (suc \u2218 f)\n\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n\nInfl< f \u2192 Infl f\nInfl< f \u2192 Infl< (suc \u2218 f)\n-}\n\n id-infl : Infl id\n id-infl = \u2115\u2264.refl\n\n \u2218-infl< : \u2200 {f g} \u2192 Infl< f \u2192 Infl< g \u2192 Infl< (f \u2218 g)\n \u2218-infl< inflf< inflg< = <-trans inflg< inflf<\n\n suc-infl< : Infl< suc\n suc-infl< = \u2115\u2264.refl\n\n suc-infl : Infl suc\n suc-infl = s\u2264s (\u2264-step \u2115\u2264.refl)\n\n nest-infl : \u2200 f (finfl : Infl f) n \u2192 Infl (nest n f)\n nest-infl f _ zero = \u2115\u2264.refl\n nest-infl f finfl (suc n) = \u2115\u2264.trans (nest-infl f finfl n) finfl\n\n nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n nest-infl< f finfl< zero = finfl<\n nest-infl< f finfl< (suc n) = <-trans (nest-infl< f finfl< n) finfl< \n\n-- See Function.NP.nest-Properties for more properties\nmodule fold-Props where\n\n fold-suc : \u2200 m n \u2192 fold m suc n \u2261 n + m\n fold-suc m zero = \u2261.refl\n fold-suc m (suc n) rewrite fold-suc m n = \u2261.refl\n\n fold-+ : \u2200 m n \u2192 fold 0 (_+_ m) n \u2261 n * m\n fold-+ m zero = \u2261.refl\n fold-+ m (suc n) rewrite fold-+ m n = \u2261.refl\n\n fold-* : \u2200 m n \u2192 fold 1 (_*_ m) n \u2261 m ^ n\n fold-* m zero = \u2261.refl\n fold-* m (suc n) rewrite fold-* m n = \u2261.refl\n\n{- TODO\n fold-^ : \u2200 m n \u2192 fold 1 (flip _^_ m) n \u2261 m \u2191\u27e8 4 \u27e9 n\n fold-^ m zero = \u2261.refl\n fold-^ m (suc n) rewrite fold-^ m n = ?\n-}\n\n cong-fold : \u2200 {A : Set} {f g : Endo A} (f\u2257g : f \u2257 g) {z} \u2192 fold z f \u2257 fold z g\n cong-fold eq zero = \u2261.refl\n cong-fold eq {z} (suc x) rewrite cong-fold eq {z} x = eq _\n\n private\n open \u2191-Props\n module Alt\u21912 where\n alt : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n alt a 0 = \u03bb b \u2192 a \u2191\u27e8 2 \u27e9 b\n alt a (suc n) = fold 1 (alt a n)\n\n alt-ok : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 alt a n\n alt-ok a zero = \u2261.sym \u2218 \u2191-\u2191\u2032 a 2\n alt-ok a (suc n) = cong-fold (alt-ok a n)\n\n{- TODO\n alt' : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 _\u2191\u27e8_\u27e9_ a (2 + n)\n alt' a zero b = \u2261.sym (\u2191-\u2191\u2032 a 2 _)\n alt' a (suc n) zero = \u2261.refl\n alt' a (suc n) (suc x) rewrite alt' a n (_\u21912+\u27e8_\u27e9_ a (suc n) x)\n = \u2261.cong (_\u2191\u27e8_\u27e9_ a (2 + n)) { fold 1 (fold (_*_ a) (fold 1) n) x} {a \u2191\u27e8 suc (suc (suc n)) \u27e9 x} (alt' a (suc n) x)\n-}\n\n open InflModule\n{-\n fold1-infl : \u2200 f \u2192 Infl f \u2192 Infl (fold 1 f)\n fold1-infl f finfl {zero} = {!z\u2264n!}\n fold1-infl f finfl {suc x} =\n x <\u27e8 {!s\u2264s (fold1-infl f finfl {x})!} \u27e9\n fold 1 f x <\u27e8 {!!} \u27e9\n {- f (fold 1 f x) \u2264\u27e8 finfl \u27e9\n f (fold 1 f x) \u2261\u27e8 \u2261.refl \u27e9 -}\n fold 1 f (1 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 f \u2218 suc)\n fold1+1-infl< f finfl< {zero} = finfl<\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + f (fold 1 f x) \u2264\u27e8 finfl< {f (fold 1 f x)} \u27e9\n f (suc (f (fold 1 f x))) \u2264\u27e8 {!!} \u27e9\n fold 1 f (2 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 (f \u2218 suc))\n fold1+1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 suc) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 suc) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+-infl< : \u2200 k f \u2192 Infl< (f \u2218\u2032 _+_ k) \u2192 Infl< (fold 1 (f \u2218\u2032 _+_ k))\n fold1+-infl< k f finfl< {zero} = s\u2264s z\u2264n\n fold1+-infl< k f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-infl< k f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 _+_ k) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 _+_ k) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n \n{-\n fold1+-infl< : \u2200 f \u2192 Infl+2< f \u2192 Infl+2< (fold 1 f)\n fold1+-infl< f finfl< {zero} _ = \u2115\u2264.refl\n fold1+-infl< f finfl< {suc zero} _ = {!!}\n fold1+-infl< f finfl< {suc (suc zero)} _ = {!!}\n fold1+-infl< f finfl< {suc (suc (suc x))} x>1 =\n 4 + x \u2264\u27e8 s\u2264s (fold1+-infl< f finfl< {suc (suc x)} (m\u2264m+n 2 x)) \u27e9\n 1 + fold 1 f (2 + x) \u2264\u27e8 finfl< {!!} \u27e9\n fold 1 f (3 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 f x \u2264\u27e8 finfl< \u27e9\n fold 1 f (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\nmodule ^-Props where\n open \u2261-Reasoning\n\n ^1 : \u2200 n \u2192 n ^ 1 \u2261 n\n ^1 zero = \u2261.refl\n ^1 (suc n) = \u2261.cong suc (proj\u2082 \u2115\u00b0.*-identity n)\n\n ^-+ : \u2200 b m n \u2192 b ^ (m + n) \u2261 b ^ m * b ^ n\n ^-+ b zero n = \u2261.sym (proj\u2082 \u2115\u00b0.+-identity (b ^ n))\n ^-+ b (suc m) n rewrite ^-+ b m n = \u2261.sym (\u2115\u00b0.*-assoc b (b ^ m) (b ^ n))\n\n ^-* : \u2200 b m n \u2192 b ^ (m * n) \u2261 (b ^ n) ^ m\n ^-* b zero n = \u2261.refl\n ^-* b (suc m) n = b ^ (n + m * n)\n \u2261\u27e8 ^-+ b n (m * n) \u27e9\n b ^ n * b ^ (m * n)\n \u2261\u27e8 \u2261.cong (_*_ (b ^ n)) (^-* b m n) \u27e9\n b ^ n * (b ^ n) ^ m \u220e\n\nack : \u2115 \u2192 \u2115 \u2192 \u2115\nack zero n = suc n\nack (suc m) zero = ack m (suc zero)\nack (suc m) (suc n) = ack m (ack (suc m) n)\n\nmodule ack-Props where\n lem\u2238 : \u2200 {m n} \u2192 m \u2264 n \u2192 m + (n \u2238 m) \u2261 n\n lem\u2238 z\u2264n = \u2261.refl\n lem\u2238 (s\u2264s {m} {n} m\u2264n) = \u2261.cong suc (lem\u2238 m\u2264n)\n\n -- n >= m \u2192 m + (n \u2238 m) \u2261 n\n -- n >= m \u2192 \u2203 k \u2192 n \u2261 m + k\n -- \u2203 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 m + ((m + k) \u2238 m) \u2261 m + k\n -- \u2200 k \u2192 m + k \u2261 m + k\n\n -- 3 \u2264 x \u2192 P x \u2192 \u2203 \u03bb k \u2192 P (3 + k)\n\n open InflModule\n\n fold1+-inflT< : \u2200 {z} \u2192 InflT< (fold (suc z))\n fold1+-inflT< _ {zero} = s\u2264s z\u2264n\n fold1+-inflT< {z} {f} finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-inflT< {z} {f} finfl< {x}) \u27e9\n 1 + fold (suc z) f x \u2264\u27e8 finfl< \u27e9\n f (fold (suc z) f x) \u2261\u27e8 \u2261.refl \u27e9\n fold (suc z) f (1 + x)\n \u220e\n where open \u2264-Reasoning\n\n Mon : (f : \u2115 \u2192 \u2115) \u2192 Set\n Mon f = \u2200 {x y} \u2192 x \u2264 y \u2192 f x \u2264 f y\n open InflModule\n\n\n \u2115-ind : \u2200 (P : \u2115 \u2192 Set) \u2192 P zero \u2192 (\u2200 {n} \u2192 P n \u2192 P (suc n)) \u2192 \u2200 {n} \u2192 P n\n \u2115-ind P P0 PS {zero} = P0\n \u2115-ind P P0 PS {suc n} = PS (\u2115-ind P P0 PS)\n\n open fold-Props\n fold-infl< : \u2200 {f g} \u2192 Infl< f \u2192 InflT< g \u2192 \u2200 {n} \u2192 Infl< (fold f g n)\n fold-infl< {f} {g} inflf< inflTg< {n} = \u2115-ind (Infl< \u2218 fold f g) inflf< inflTg< {n}\n\n fold-mon : \u2200 {f} (fmon : Mon f) (finfl< : Infl< f) {z} \u2192 Mon (fold z f)\n fold-mon fmon finfl< {_} {y = 0} z\u2264n = \u2115\u2264.refl\n fold-mon {f} fmon finfl< {z} {y = suc n} z\u2264n = z \u2264\u27e8 \u2264-step \u2115\u2264.refl \u27e9\n suc z \u2264\u27e8 nest-infl< f finfl< n {z} \u27e9\n fold z f (suc n) \u2261\u27e8 \u2261.refl \u27e9\n fold z f (suc n) \u220e\n where open \u2264-Reasoning\n fold-mon fmon finfl< (s\u2264s m\u2264n) = fmon (fold-mon fmon finfl< m\u2264n)\n\n MonT : Endo (Endo \u2115) \u2192 Set\n MonT g = \u2200 {f} \u2192 Mon f \u2192 Infl< f \u2192 Mon (g f)\n\n fold-mon' : \u2200 {f g} \u2192 Mon f \u2192 Infl< f \u2192 MonT g \u2192 InflT< g \u2192 \u2200 {n} \u2192 Mon (fold f g n)\n fold-mon' {f} {g} mon-f infl-f mont-g inflTg< {n} = go n where\n go : \u2200 n \u2192 Mon (fold f g n)\n go zero = mon-f\n go (suc n) = mont-g (go n) (fold-infl< infl-f inflTg< {n})\n\n\n -- +-fold : \u2200 a b \u2192 a + b \u2261 fold a suc b\n -- *-fold : \u2200 a b \u2192 a * b \u2261 fold 0 (_+_ a) b\n\n 1\u22641+a^b : \u2200 a b \u2192 1 \u2264 (1 + a) ^ b\n 1\u22641+a^b a zero = s\u2264s z\u2264n\n 1\u22641+a^b a (suc b) = 1\u22641+a^b a b +-mono z\u2264n\n\n 1+a^-mon : \u2200 {a} \u2192 Mon (_^_ (1 + a))\n 1+a^-mon {a} (z\u2264n {b}) = 1\u22641+a^b a b\n 1+a^-mon {a} (s\u2264s {m} {n} m\u2264n)\n = (1 + a) ^ (1 + m) \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ m + a * (1 + a) ^ m \u2264\u27e8 (1+a^-mon m\u2264n) +-mono ((a \u220e) *-mono (1+a^-mon m\u2264n)) \u27e9\n (1 + a) ^ n + a * (1 + a) ^ n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ (1 + n) \u220e\n where open \u2264-Reasoning\n\n postulate\n \u21913+ : \u2200 a n b \u2192 a \u2191\u27e8 3 + n \u27e9 b \u2261 fold (_^_ a) (fold 1) n b\n -- mon\u21913+'' : \u2200 a b \u2192 Mon (\u03bb n \u2192 fold (_^_ a) (fold 1) n b)\n\n mon\u21913+' : \u2200 b \u2192 Mon (\u03bb n \u2192 fold (_^_ 2) (fold 1) n (3 + b))\n -- mon\u21913+' b = {!!}\n\n mon\u21913+ : \u2200 b \u2192 Mon (\u03bb n \u2192 2 \u2191\u27e8 3 + n \u27e9 (3 + b))\n mon\u21913+ b {m} {n} rewrite \u21913+ 2 m (3 + b) | \u21913+ 2 n (3 + b) = mon\u21913+' b\n\n lem2^3 : \u2200 n \u2192 2 ^ 3 \u2264 2 ^ (3 + n)\n lem2^3 n = 1+a^-mon {1} {3} {3 + n} (s\u2264s (s\u2264s (s\u2264s z\u2264n)))\n\n open \u2191-Props\n lem>=3 : \u2200 m n \u2192 3 \u2264 2 \u2191\u27e8 3 + m \u27e9 (3 + n)\n lem>=3 m n = 3 \u2264\u27e8 s\u2264s (s\u2264s (s\u2264s z\u2264n)) \u27e9\n 2 \u2191\u27e8 3 \u27e9 3 \u2261\u27e8 \u2191\u2083-^ 2 3 \u27e9\n 2 ^ 3 \u2264\u27e8 lem2^3 n \u27e9\n 2 ^ (3 + n) \u2261\u27e8 \u2261.sym (\u2191\u2083-^ 2 (3 + n)) \u27e9\n 2 \u2191\u27e8 3 \u27e9 (3 + n) \u2264\u27e8 mon\u21913+ n z\u2264n \u27e9\n 2 \u2191\u27e8 3 + m \u27e9 (3 + n) \u220e\n where open \u2264-Reasoning\n\n 2* = _*_ 2\n\n -- fold 2* n 1 \u2261 2 ^ n\n\n -- nest-* : \u2200 m n \u2192 nest (m * n) f \u2257 nest m (nest n f)\n\n nest-2* : \u2200 m n \u2192 nest (m * n) (fold 1) 2* \u2261 nest m (nest n (fold 1)) 2*\n nest-2* m n = nest-Properties.nest-* (fold 1) m n 2*\n\n{-\n lem3\u2264 : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 2\n lem3\u2264 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264 (suc n) = \n 3 \u2264\u27e8 {!!} \u27e9\n fold 2* f1 (1 + n) 2 \u220e\n where open \u2264-Reasoning\n f1 = fold 1\n\n lem3\u2264' : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 3\n lem3\u2264' 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264' (suc n) = \n 3 \u2264\u27e8 lem3\u2264 n \u27e9\n fold 2* (fold 1) n 2 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) n (fold 2* (fold 1) 3 1) \u2261\u27e8 {!!} \u27e9\n{- \u2261\u27e8 nest-Properties.nest-* {!2*!} {!!} {!!} {!!} \u27e9\n fold 2* (fold 1) (3 * n) 1 \u2261\u27e8 {!!} \u27e9\n-}\n fold 1 (fold 2* (fold 1) n) 3 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) (1 + n) 3 \u220e\n where open \u2264-Reasoning\n-}\n---\n\n{-\nfold 2* (fold 1) (3 * n) 1 \u2264\n\nfold 2* (fold 1) n (fold 2* (fold 1) n (fold 2* (fold 1) n 1)) \u2264\n\nfold 1 (fold 2* (fold 1) n) 3 \u2264\nfold 2* (fold 1) (1 + n) 3\n-}\n\n lem>=3'' : \u2200 m n \u2192 3 \u2264 2 \u2191\u27e8 suc m \u27e9 (3 + n)\n lem>=3'' zero n = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n lem>=3'' (suc zero) n rewrite \u2191\u2082-* 2 (3 + n) = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n lem>=3'' (suc (suc m)) n = lem>=3 m n\n\n lem2221 : \u2200 m \u2192 2 \u2261 2 \u2191\u27e8 2 + m \u27e9 1\n lem2221 zero = \u2261.refl\n lem2221 (suc m) = lem2221 m\n\n lem4212 : \u2200 m \u2192 4 \u2261 2 \u2191\u27e8 1 + m \u27e9 2\n lem4212 zero = \u2261.refl\n lem4212 (suc m) = 4 \u2261\u27e8 lem4212 m \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 2 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 (1 + m)) (lem2221 m) \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 (2 \u2191\u27e8 2 + m \u27e9 1) \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 2 + m \u27e9 2 \u220e\n where open \u2261-Reasoning\n\n ack-\u2191 : \u2200 m n \u2192 3 + ack m n \u2261 2 \u2191\u27e8 m \u27e9 (3 + n)\n ack-\u2191 zero n = \u2261.refl\n ack-\u2191 (suc m) zero = 3 + ack (suc m) 0 \u2261\u27e8 ack-\u2191 m 1 \u27e9\n 2 \u2191\u27e8 m \u27e9 4 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 m) (lem4212 m) \u27e9\n 2 \u2191\u27e8 suc m \u27e9 3 \u220e\n where open \u2261-Reasoning\n ack-\u2191 (suc m) (suc n) = 3 + ack (suc m) (suc n)\n \u2261\u27e8 \u2261.refl \u27e9\n 3 + ack m (ack (suc m) n)\n \u2261\u27e8 \u2261.refl \u27e9\n 3 + ack m ((3 + ack (suc m) n) \u2238 3)\n \u2261\u27e8 \u2261.cong (\u03bb x \u2192 3 + ack m (x \u2238 3)) (ack-\u2191 (suc m) n) \u27e9\n 3 + ack m (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3)\n \u2261\u27e8 ack-\u2191 m (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3) \u27e9\n 2 \u2191\u27e8 m \u27e9 (3 + (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3))\n \u2261\u27e8 \u2261.cong (\u03bb x \u2192 2 \u2191\u27e8 m \u27e9 x) (lem\u2238 (lem>=3'' m n)) \u27e9\n 2 \u2191\u27e8 m \u27e9 (2 \u2191\u27e8 suc m \u27e9 (3 + n))\n \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 suc m \u27e9 (4 + n) \u220e \n where open \u2261-Reasoning\n\n postulate\n 1+a^-infl< : \u2200 {a} \u2192 Infl< (_^_ (1 + a))\n\n -- 2+a*1+b-infl< : \u2200 a \u2192 Infl< (\u03bb x \u2192 (2 + a) * (1 + x))\n -- \u2200 a b \u2192 b < (2 + a) * b\n -- fold-a*-fold1 : \u2200 {n a} \u2192 Infl< (_\u21912+\u27e8_\u27e9_ (2 + a) n)\n fold-a^-fold1 : \u2200 {n a} \u2192 Infl< (fold (_^_ (1 + a)) (fold 1) n)\n fold-a^-fold1 {n} = fold-infl< 1+a^-infl< fold1+-inflT< {n}\n\n \u21913+-mon : \u2200 a n \u2192 Mon (fold (_^_ (1 + a)) (fold 1) n)\n \u21913+-mon a n = fold-mon' 1+a^-mon 1+a^-infl< (\u03bb \u03b7\u2081 \u03b7\u2082 \u2192 fold-mon \u03b7\u2081 \u03b7\u2082) fold1+-inflT< {n}\n\n{-\n open \u2191-Props\n\n -*\u27e81+n\u27e9-infl : \u2200 n \u2192 Infl (_*_ (1 + n))\n -*\u27e81+n\u27e9-infl n = {!!}\n\n lem121 : \u2200 a b \u2192 b < (1 + a) \u2191\u27e8 2 \u27e9 (1 + b)\n lem121 a b rewrite \u2191\u2082-* (1 + a) (1 + b)\n = 1 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (1 + a) * (1 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (1 + a) (1 + b) \u27e9\n (1 + b) * (1 + a)\n \u220e\n where open \u2264-Reasoning\n\n -- \u2200 a b \u2192 b < (2 + a) * b\n lem222 : \u2200 a b \u2192 b < (2 + a) \u2191\u27e8 2 \u27e9 (2 + b)\n lem222 a b rewrite \u2191\u2082-* (2 + a) (2 + b)\n = 1 + b\n \u2264\u27e8 suc-infl \u27e9\n 2 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (2 + a) * (2 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (2 + a) (2 + b) \u27e9\n (2 + b) * (2 + a)\n \u220e\n where open \u2264-Reasoning\n-}\n{-\n open fold-Props\n\n module Foo a where\n f = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b\n f-lem : \u2200 n \u2192 f (suc n) \u2257 fold 1 (f n)\n f-lem n x = \u2261.refl\n\n f2 = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n f2-lem : \u2200 n \u2192 f2 (suc n) \u2257 fold 1 (f2 n)\n f2-lem n x = {!!}\n\n -- b < (1 + a) * (1 + b)\n\n -- nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n infl3< : \u2200 a n \u2192 Infl< (\u03bb b \u2192 (1 + a) \u21912+\u27e8 n \u27e9 (1 + b))\n infl3< a zero {b} = lem121 a b\n infl3< a (suc n) {b}\n = 1 + b\n -- fold1-infl< : \u2200 f \u2192 Infl< f \u2192 \u2200 x \u2192 x < fold 1 f x\n \u2264\u27e8 {!fold1f2-infl< {_}!} \u27e9\n fold 1 f1 b\n \u2264\u27e8 {!!} \u27e9\n{-\n \u2264\u27e8 fold1-infl< (f 0) (\u03bb {x} \u2192 {!infl3< a n {x}!}) {{!!}} \u27e9\n {!!}\n \u2264\u27e8 {!!} \u27e9\n-}\n{-\n 1 + b\n (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n \u2264\u27e8 {!nest-infl< (\u03bb b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b) ? b {(2 + a) \u21912+\u27e8 n \u27e9 (2 + b)}!} \u27e9\n fold (f 0 1) (f 0) (1 + b)\n \u2264\u27e8 {!\u2115\u2264.reflexive (\u2261.cong (\u03bb g \u2192 g 1) (nest-Properties.nest-+ (f 0) 1 (1 + b)))!} \u27e9\n-}\n fold 1 (f 0) (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n f 1 (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) \u21912+\u27e8 suc n \u27e9 (1 + b)\n \u220e\n\n where\n f = \u03bb k b \u2192 (1 + a) \u21912+\u27e8 k + n \u27e9 b\n f1 = f 0 \u2218 suc\n\n -- fold1f2-infl< : Infl< (fold 1 f2)\n -- fold1f2-infl< = fold1-infl< f2 {!(\u03bb b \u2192 infl3< a n b ?)!}\n\n fold1f1-infl<' : Infl< (fold 1 f1)\n fold1f1-infl<' = {!fold1+1-infl< (f 0) {!infl3< a n!}!}\n lem : \u2200 x \u2192 f 0 (1 + x) < f 0 (f 0 x)\n lem x = {!!}\n postulate\n finfl : Infl< (f 0)\n open \u2264-Reasoning\n-}\n\n_\u21d1\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\na \u21d1\u27e8 n , k \u27e9 b = fold k f n\n module M\u21d1 where \n f : \u2115 \u2192 \u2115\n f x = a \u2191\u27e8 2 + x \u27e9 b\n\nmodule \u21d1-Props where\n _\u21d1\u2032\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\n a \u21d1\u2032\u27e8 n\u2080 , k \u27e9 b = g n\u2080\n module M\u21d1\u2032 where\n h : \u2115 \u2192 \u2115\n h x = a \u2191\u27e8 2 + x \u27e9 b\n\n g : \u2115 \u2192 \u2115\n g 0 = k\n g (suc n) = h (g n)\n\n g-nest : \u2200 n \u2192 g n \u2261 h $\u27e8 n \u27e9 k\n g-nest zero = \u2261.refl\n g-nest (suc n) rewrite g-nest n = \u2261.refl\n\n \u21d1\u2080 : \u2200 a k b \u2192 a \u21d1\u27e8 0 , k \u27e9 b \u2261 k\n \u21d1\u2080 _ _ _ = \u2261.refl\n\n \u21d1\u2081 : \u2200 a k b \u2192 a \u21d1\u27e8 1 , k \u27e9 b \u2261 a \u2191\u27e8 2 + k \u27e9 b\n \u21d1\u2081 _ _ _ = \u2261.refl\n\n \u21d1-\u21d1\u2032 : \u2200 a n k b \u2192 a \u21d1\u27e8 n , k \u27e9 b \u2261 a \u21d1\u2032\u27e8 n , k \u27e9 b\n \u21d1-\u21d1\u2032 a n k b rewrite M\u21d1\u2032.g-nest a n k b n = \u2261.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Graham%27s_number\nGraham's-number : \u2115\nGraham's-number = 3 \u21d1\u27e8 64 , 4 \u27e9 3\n -- = (f\u2218\u2076\u2074\u2026\u2218f)4\n -- where f x = 3 \u2191\u27e8 2 + x \u27e9 3\n\n{-\nmodule GeneralisedArithmetic {a} {A : Set a} (0# : A) (1+ : A \u2192 A) where\n\n 1# : A\n 1# = 1+ 0#\n\n open Data.Nat.GeneralisedArithmetic 0# 1+ public\n\n exp : (* : A \u2192 A \u2192 A) \u2192 (\u2115 \u2192 A \u2192 A)\n exp _*_ n b = fold 1# (\u03bb s \u2192 b * s) n\n-}\n -- hyperop a n b = fold exp\n\nmodule == where\n _\u2248_ : (m n : \u2115) \u2192 Set\n m \u2248 n = T (m == n)\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {zero} {zero} _ = id\n subst P {suc _} {suc _} p = subst (P \u2218 suc) p\n subst _ {zero} {suc _} ()\n subst _ {suc _} {zero} ()\n\n sound : \u2200 m n \u2192 T (m == n) \u2192 m \u2261 n\n sound m n p = subst (_\u2261_ m) p \u2261.refl\n\n refl : Reflexive _\u2248_\n refl {zero} = _\n refl {suc n} = refl {n}\n\n sym : Symmetric _\u2248_\n sym {m} {n} eq rewrite sound m n eq = refl {n}\n\n trans : Transitive _\u2248_\n trans {m} {n} {o} m\u2248n n\u2248o rewrite sound m n m\u2248n | sound n o n\u2248o = refl {o}\n\n setoid : Setoid _ _\n setoid = record { Carrier = \u2115; _\u2248_ = _\u2248_\n ; isEquivalence =\n record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} } }\n\n open Setoid setoid public hiding (refl; sym; trans; _\u2248_)\n\n{-\ndata _`\u2264?`_\u219d_ : (m n : \u2115) \u2192 Dec (m \u2264 n) \u2192 Set where\n z\u2264?n : \u2200 {n} \u2192 zero `\u2264?` n \u219d yes z\u2264n\n s\u2264?z : \u2200 {m} \u2192 suc m `\u2264?` zero \u219d no \u03bb()\n s\u2264?s-yes : \u2200 {m n m\u2264n} \u2192 m `\u2264?` n \u219d yes m\u2264n \u2192 suc m `\u2264?` suc n \u219d yes (s\u2264s m\u2264n)\n s\u2264?s-no : \u2200 {m n m\u2270n} \u2192 m `\u2264?` n \u219d no m\u2270n \u2192 suc m `\u2264?` suc n \u219d no (m\u2270n \u2218 \u2264-pred)\n\n`\u2264?`-complete : \u2200 m n \u2192 m `\u2264?` n \u219d (m \u2264? n)\n`\u2264?`-complete zero n = z\u2264?n\n`\u2264?`-complete (suc n) zero = {!s\u2264?z!}\n`\u2264?`-complete (suc m) (suc n) with m \u2264? n | `\u2264?`-complete m n\n... | yes q | r = s\u2264?s-yes r\n... | no q | r = s\u2264?s-no {!!}\n-}\n\n_<=_ : (x y : \u2115) \u2192 Bool\nzero <= _ = true\nsuc _ <= zero = false\nsuc m <= suc n = m <= n\n\nmodule <= where\n sound : \u2200 m n \u2192 T (m <= n) \u2192 m \u2264 n\n sound zero _ _ = z\u2264n\n sound (suc m) (suc n) p = s\u2264s (sound m n p)\n sound (suc m) zero ()\n\n complete : \u2200 {m n} \u2192 m \u2264 n \u2192 T (m <= n)\n complete z\u2264n = _\n complete (s\u2264s m\u2264n) = complete m\u2264n\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (Props.\u2264-steps 1 1+n\u2264m)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"e9edc47e1e4dfd23330e7436b76e5a8cd65dc3c3","subject":"horsing around with 312 ABT style approach to proving substitution, where the renamings happen at the last second when you need them and sort of under the hood. i dont want to spend a lot of time on this because it seems like it might not actually go through, but if it does it wouldnt change the type of preservation so that would be good","message":"horsing around with 312 ABT style approach to proving substitution, where the renamings happen at the last second when you need them and sort of under the hood. i dont want to spend a lot of time on this because it seems like it might not actually go through, but if it does it wouldnt change the type of preservation so that would be good\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"lemmas-subst-ta.agda","new_file":"lemmas-subst-ta.agda","new_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import weakening\nopen import exchange\nopen import lemmas-disjointness\n\nmodule lemmas-subst-ta where\n -- there is always a natural number that's fresh for any expression,\n -- possibly avoiding one in particular\n exists-fresh : (d : dhexp) \u2192 \u03a3[ y \u2208 Nat ] (fresh y d)\n exists-fresh = {!!}\n\n -- rename all x's into y's in d to produce d'\n data rename : (x : Nat) \u2192 (y : Nat) \u2192 (d : dhexp) (d' : dhexp) \u2192 Set where\n RConst : \u2200{x y} \u2192 rename x y c c\n RVar1 : \u2200{x y z} \u2192 x \u2260 z \u2192 rename x y (X z) (X z)\n RVar2 : \u2200{x y} \u2192 rename x y (X x) (X y)\n -- RLam : \u2200{x y \u03c4 e} \u2192 rename x y ? ?\n -- REHole\n -- RNEHole\n -- RAp\n -- RCast\n -- RFailedCast\n\n -- have to do something about the context in the typing judgement as well\n -- and probably in the \u03c3s. bleh.\n\n exists-rename : (x y : Nat) (d : dhexp) \u2192 \u03a3[ d' \u2208 dhexp ](rename x y d d')\n exists-rename z y c = c , RConst\n exists-rename z y (X x) with natEQ z x\n exists-rename z y (X .z) | Inl refl = X y , RVar2\n exists-rename z y (X x) | Inr x\u2081 = X x , RVar1 x\u2081\n exists-rename z y (\u00b7\u03bb x [ x\u2081 ] d) = {!!}\n exists-rename z y \u2987\u2988\u27e8 x \u27e9 = {!!}\n exists-rename z y \u2987 d \u2988\u27e8 x \u27e9 = {!!}\n exists-rename z y (d \u2218 d\u2081) = {!!}\n exists-rename z y (d \u27e8 x \u21d2 x\u2081 \u27e9) = {!!}\n exists-rename z y (d \u27e8 x \u21d2\u2987\u2988\u21d2\u0338 x\u2081 \u27e9) = {!!}\n\n rename-ctx : {A : Set} \u2192 (x y : Nat) \u2192 (\u0393 : A ctx) \u2192 A ctx\n rename-ctx x y \u0393 q with natEQ y q -- if you're asking about the new variable y, it's now gonna be whatever x was\n rename-ctx x y \u0393 q | Inl x\u2081 = \u0393 x\n rename-ctx x y \u0393 q | Inr x\u2081 = \u0393 q\n\n -- is this the right direction? try to use it below; need that y # \u0393, likely\n rename-preserve : \u2200{x y d d' \u0394 \u0393 \u03c4} \u2192 (f : fresh y d) \u2192 rename x y d d' \u2192 \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192 \u0394 , (rename-ctx x y \u0393) \u22a2 d' :: \u03c4\n rename-preserve FConst RConst TAConst = TAConst\n rename-preserve (FVar x\u2082) (RVar1 x\u2081) (TAVar x\u2083) = TAVar {!!}\n rename-preserve (FVar x\u2082) RVar2 (TAVar x\u2083) = TAVar {!!}\n rename-preserve (FLam x\u2082 f) re (TALam x\u2083 wt) = {!!}\n rename-preserve (FHole x\u2082) re (TAEHole x\u2083 x\u2084) = {!!}\n rename-preserve (FNEHole x\u2082 f) re (TANEHole x\u2083 wt x\u2084) = {!!}\n rename-preserve (FAp f f\u2081) re (TAAp wt wt\u2081) = {!!}\n rename-preserve (FCast f) re (TACast wt x\u2082) = {!!}\n rename-preserve (FFailedCast f) re (TAFailedCast wt x\u2082 x\u2083 x\u2084) = {!!}\n\n lem-subst : \u2200{\u0394 \u0393 x \u03c41 d1 \u03c4 d2 } \u2192\n x # \u0393 \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 d1 :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 [ d2 \/ x ] d1 :: \u03c4\n lem-subst apt TAConst wt2 = TAConst\n lem-subst {x = x} apt (TAVar {x = x'} x\u2082) wt2 with natEQ x' x\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inl refl with lem-apart-union-eq {\u0393 = \u0393} apt x\u2083\n lem-subst apt (TAVar x\u2083) wt2 | Inl refl | refl = wt2\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inr x\u2082 = TAVar (lem-neq-union-eq {\u0393 = \u0393} x\u2082 x\u2083)\n lem-subst {\u0394 = \u0394} {\u0393 = \u0393} {x = x} {d2 = d2} x#\u0393 (TALam {x = y} {\u03c41 = \u03c41} {d = d} {\u03c42 = \u03c42} x\u2082 wt1) wt2\n with lem-union-none {\u0393 = \u0393} x\u2082\n ... | x\u2260y , y#\u0393 with natEQ y x\n ... | Inl eq = abort (x\u2260y (! eq))\n ... | Inr _ with exists-fresh d2\n ... | z , neq = TALam y#\u0393 (lem-subst {\u0394 = \u0394} {\u0393 = \u0393 ,, (y , \u03c41)} {x = x} {d1 = d} (apart-extend1 \u0393 x\u2260y x#\u0393) (exchange-ta-\u0393 {\u0393 = \u0393} x\u2260y wt1) (weaken-ta {!!} wt2))\n lem-subst apt (TAAp wt1 wt2) wt3 = TAAp (lem-subst apt wt1 wt3) (lem-subst apt wt2 wt3)\n lem-subst apt (TAEHole in\u0394 sub) wt2 = TAEHole in\u0394 (STASubst sub wt2)\n lem-subst apt (TANEHole x\u2081 wt1 x\u2082) wt2 = TANEHole x\u2081 (lem-subst apt wt1 wt2) (STASubst x\u2082 wt2)\n lem-subst apt (TACast wt1 x\u2081) wt2 = TACast (lem-subst apt wt1 wt2) x\u2081\n lem-subst apt (TAFailedCast wt1 x\u2081 x\u2082 x\u2083) wt2 = TAFailedCast (lem-subst apt wt1 wt2) x\u2081 x\u2082 x\u2083\n\n -- possible fix: add a premise from a new judgement like holes disjoint\n -- that classifies pairs of dhexps that share no variable names\n -- whatsoever. that should imply freshness here. then propagate that\n -- change to preservation. this is morally what \u03b1-equiv lets us do in a\n -- real setting.\n\n\n -- lem-subst {\u0394 = \u0394} {\u0393 = \u0393} {x = x} {d2 = d2} x#\u0393 (TALam {x = y} {\u03c41 = \u03c41} {d = d} {\u03c42 = \u03c42} x\u2082 wt1) wt2\n -- with lem-union-none {\u0393 = \u0393} x\u2082\n -- ... | x\u2260y , y#\u0393 with natEQ y x\n -- ... | Inl eq = abort (x\u2260y (! eq))\n -- ... | Inr _ with exists-fresh d2 None\n -- ... | z , neq = TALam y#\u0393 (lem-subst {\u0394 = \u0394} {\u0393 = \u0393 ,, (y , \u03c41)} {x = x} {d1 = d} (apart-extend1 \u0393 x\u2260y x#\u0393) (exchange-ta-\u0393 {\u0393 = \u0393} x\u2260y wt1) (weaken-ta {!!} wt2))\n","old_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import weakening\nopen import exchange\nopen import lemmas-disjointness\n\nmodule lemmas-subst-ta where\n lem-subst : \u2200{\u0394 \u0393 x \u03c41 d1 \u03c4 d2 } \u2192\n x # \u0393 \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 d1 :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 [ d2 \/ x ] d1 :: \u03c4\n lem-subst apt TAConst wt2 = TAConst\n lem-subst {x = x} apt (TAVar {x = x'} x\u2082) wt2 with natEQ x' x\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inl refl with lem-apart-union-eq {\u0393 = \u0393} apt x\u2083\n lem-subst apt (TAVar x\u2083) wt2 | Inl refl | refl = wt2\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inr x\u2082 = TAVar (lem-neq-union-eq {\u0393 = \u0393} x\u2082 x\u2083)\n lem-subst {\u0394 = \u0394} {\u0393 = \u0393} {x = x} x#\u0393 (TALam {x = y} {\u03c41 = \u03c41} {d = d} {\u03c42 = \u03c42} x\u2082 wt1) wt2\n with lem-union-none {\u0393 = \u0393} x\u2082\n ... | x\u2260y , y#\u0393 with natEQ y x\n ... | Inl eq = abort (x\u2260y (! eq))\n ... | Inr _ = TALam y#\u0393 (lem-subst {\u0394 = \u0394} {\u0393 = \u0393 ,, (y , \u03c41)} {x = x} {d1 = d} (apart-extend1 \u0393 x\u2260y x#\u0393) (exchange-ta-\u0393 {\u0393 = \u0393} x\u2260y wt1) (weaken-ta {!!} wt2))\n lem-subst apt (TAAp wt1 wt2) wt3 = TAAp (lem-subst apt wt1 wt3) (lem-subst apt wt2 wt3)\n lem-subst apt (TAEHole in\u0394 sub) wt2 = TAEHole in\u0394 (STASubst sub wt2)\n lem-subst apt (TANEHole x\u2081 wt1 x\u2082) wt2 = TANEHole x\u2081 (lem-subst apt wt1 wt2) (STASubst x\u2082 wt2)\n lem-subst apt (TACast wt1 x\u2081) wt2 = TACast (lem-subst apt wt1 wt2) x\u2081\n lem-subst apt (TAFailedCast wt1 x\u2081 x\u2082 x\u2083) wt2 = TAFailedCast (lem-subst apt wt1 wt2) x\u2081 x\u2082 x\u2083\n\n -- possible fix: add a premise from a new judgement like holes disjoint\n -- that classifies pairs of dhexps that share no variable names\n -- whatsoever. that should imply freshness here. then propagate that\n -- change to preservation. this is morally what \u03b1-equiv lets us do in a\n -- real setting.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f3003ac296a5a819510afb4fff39156c8247f0b5","subject":"Desc model: implicit refl, cong, cong2, reflFun, desc, and IMu","message":"Desc model: implicit refl, cong, cong2, reflFun, desc, and IMu","repos":"kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram,kwangkim\/pigment","old_file":"models\/IDesc_type_type.agda","new_file":"models\/IDesc_type_type.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : (I : Set)(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox I (var i) P x = var (i , x)\nbox I (const X) P x = const X\nbox I (prod D D') P (d , d') = prod (box I D P d) (box I D' P d')\nbox I (sigma S T) P (a , b) = box I (T a) P b\nbox I (pi S T) P f = pi S (\\s -> box I (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box I (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box I D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box I (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : (I : Set)(i : I) -> IDescl I\nvarl I i = con (lvar , i)\n\nconstl : (I : Set)(X : Set) -> IDescl I\nconstl I X = con (lconst , X)\n\nprodl : (I : Set)(D D' : IDescl I) -> IDescl I\nprodl I D D' = con (lprod , (D , D'))\n\npil : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\npil I S T = con (lpi , ( S , T))\n\nsigmal : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal I S T = con (lsigma , ( S , T))\n\ncases : (I : Set) \n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box Unit (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases I ( lvar , i ) hs = var i\ncases I ( lconst , X ) hs = const X\ncases I ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases I ( lpi , ( S , T ) ) hs = pi S hs\ncases I ( lsigma , ( S , T ) ) hs = sigma S hs\n\niso1 : (I : Set) -> IDescl I -> IDesc I\niso1 I d = induction Unit (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases I) Void d\n\niso2 : (I : Set) -> IDesc I -> IDescl I\niso2 I (var i) = varl I i\niso2 I (const X) = constl I X\niso2 I (prod D D') = prodl I (iso2 I D) (iso2 I D')\niso2 I (pi S T) = pil I S (\\s -> iso2 I (T s))\niso2 I (sigma S T) = sigmal I S (\\s -> iso2 I (T s))\n\n\n\nproof-iso1-iso2 : (I : Set) -> (D : IDesc I) -> iso1 I (iso2 I D) == D\nproof-iso1-iso2 I (var i) = refl\nproof-iso1-iso2 I (const x) = refl\nproof-iso1-iso2 I (prod D D') with proof-iso1-iso2 I D | proof-iso1-iso2 I D'\n... | p | q = cong2 prod p q\nproof-iso1-iso2 I (pi S T) = cong (pi S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\nproof-iso1-iso2 I (sigma S T) = cong (sigma S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\n\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = iso2 I (iso1 I D) == D\n\n\nproof-iso2-iso1 : (I : Set) -> (D : IDescl I) -> iso2 I (iso1 I D) == D\nproof-iso2-iso1 I D = induction Unit \n (\u03bb _ \u2192 descD I)\n (P I)\n (proof-iso2-iso1-casesW I) \n Void\n D\n where proof-iso2-iso1-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box Unit (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-iso2-iso1-cases I (lvar , i) hs = refl\n proof-iso2-iso1-cases I (lconst , x) hs = refl\n proof-iso2-iso1-cases I (lprod , ( D , D' )) ( p , q ) = cong2 (prodl I) p q \n proof-iso2-iso1-cases I (lpi , ( S , T )) hs = cong (pil I S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-cases I (lsigma , ( S , T )) hs = cong (sigmal I S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box Unit (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-iso2-iso1-casesW I Void = proof-iso2-iso1-cases I\n \n\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : (A : Set)(B : Set)(f : A -> B)(x y : A) -> x == y -> f x == f y\ncong A B f x .x refl = refl\n\ncong2 : (A B C : Set)(f : A -> B -> C)(x y : A)(z t : B) -> \n x == y -> z == t -> f x z == f y t\ncong2 A B C f x .x z .z refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : (A : Set)(B : Set)(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : (I : Set) -> IDesc I -> (I -> Set) -> Set\ndesc I (var i) P = P i\ndesc I (const X) P = X\ndesc I (prod D D') P = desc I D P * desc I D' P\ndesc I (sigma S T) P = Sigma S (\\s -> desc I (T s) P)\ndesc I (pi S T) P = (s : S) -> desc I (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu (I : Set)(R : I -> IDesc I)(i : I) : Set where\n con : desc I (R i) (\\j -> IMu I R j) -> IMu I R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : (I : Set)(D : IDesc I)(P : I -> Set) -> desc I D P -> IDesc (Sigma I P)\nbox I (var i) P x = var (i , x)\nbox I (const X) P x = const X\nbox I (prod D D') P (d , d') = prod (box I D P d) (box I D' P d')\nbox I (sigma S T) P (a , b) = box I (T a) P b\nbox I (pi S T) P f = pi S (\\s -> box I (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu I R) -> Set)\n (m : (i : I)\n (xs : desc I (R i) (IMu I R))\n (hs : desc (Sigma I (IMu I R)) (box I (R i) (IMu I R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu I R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc I D (IMu I R)) -> \n desc (Sigma I (IMu I R)) (box I D (IMu I R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu I R) -> Set)\n (m : (i : I)\n (xs : desc I (R i) (IMu I R))\n (hs : desc (Sigma I (IMu I R)) (box I (R i) (IMu I R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu I R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu Unit (\\_ -> descD I) Void\n\nvarl : (I : Set)(i : I) -> IDescl I\nvarl I i = con (lvar , i)\n\nconstl : (I : Set)(X : Set) -> IDescl I\nconstl I X = con (lconst , X)\n\nprodl : (I : Set)(D D' : IDescl I) -> IDescl I\nprodl I D D' = con (lprod , (D , D'))\n\npil : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\npil I S T = con (lpi , ( S , T))\n\nsigmal : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal I S T = con (lsigma , ( S , T))\n\ncases : (I : Set) \n (xs : desc Unit (descD I) (IMu Unit (\u03bb _ -> descD I)))\n (hs : desc (Sigma Unit (IMu Unit (\u03bb _ -> descD I)))\n (box Unit (descD I) (IMu Unit (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases I ( lvar , i ) hs = var i\ncases I ( lconst , X ) hs = const X\ncases I ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases I ( lpi , ( S , T ) ) hs = pi S hs\ncases I ( lsigma , ( S , T ) ) hs = sigma S hs\n\niso1 : (I : Set) -> IDescl I -> IDesc I\niso1 I d = induction Unit (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases I) Void d\n\niso2 : (I : Set) -> IDesc I -> IDescl I\niso2 I (var i) = varl I i\niso2 I (const X) = constl I X\niso2 I (prod D D') = prodl I (iso2 I D) (iso2 I D')\niso2 I (pi S T) = pil I S (\\s -> iso2 I (T s))\niso2 I (sigma S T) = sigmal I S (\\s -> iso2 I (T s))\n\n\n\nproof-iso1-iso2 : (I : Set) -> (D : IDesc I) -> iso1 I (iso2 I D) == D\nproof-iso1-iso2 I (var i) = refl\nproof-iso1-iso2 I (const x) = refl\nproof-iso1-iso2 I (prod D D') with proof-iso1-iso2 I D | proof-iso1-iso2 I D'\n... | p | q = cong2 (IDesc I) \n (IDesc I)\n (IDesc I) \n prod \n (iso1 I (iso2 I D))\n D \n (iso1 I (iso2 I D'))\n D' p q\nproof-iso1-iso2 I (pi S T) = cong (S \u2192 IDesc I)\n (IDesc I)\n (pi S) \n (\\x -> iso1 I (iso2 I (T x)))\n T \n (reflFun S (IDesc I)\n (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\nproof-iso1-iso2 I (sigma S T) = cong (S \u2192 IDesc I)\n (IDesc I)\n (sigma S) \n (\\x -> iso1 I (iso2 I (T x)))\n T \n (reflFun S \n (IDesc I)\n (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\n\n\n-- induction : (I : Set)\n-- (R : I -> IDesc I)\n-- (P : Sigma I (IMu I R) -> Set)\n-- (m : (i : I)\n-- (xs : desc I (R i) (IMu I R))\n-- (hs : desc (Sigma I (IMu I R)) (box I (R i) (IMu I R) xs) P) ->\n-- P ( i , con xs)) ->\n-- (i : I)(x : IMu I R i) -> P ( i , x )\n\n\nP : (I : Set) -> Sigma Unit (IMu Unit (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = iso2 I (iso1 I D) == D\n\n\nproof-iso2-iso1 : (I : Set) -> (D : IDescl I) -> iso2 I (iso1 I D) == D\nproof-iso2-iso1 I D = induction Unit \n (\u03bb _ \u2192 descD I)\n (P I)\n (proof-iso2-iso1-casesW I) \n Void\n D\n where proof-iso2-iso1-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc Unit (descDChoice I s)\n (IMu Unit (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc (Sigma Unit (IMu Unit (\u03bb x \u2192 sigma DescDConst (descDChoice I))))\n (box Unit (sigma DescDConst (descDChoice I))\n (IMu Unit (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-iso2-iso1-cases I (lvar , i) hs = refl\n proof-iso2-iso1-cases I (lconst , x) hs = refl\n proof-iso2-iso1-cases I (lprod , ( D , D' )) ( p , q ) = cong2 (IDescl I) \n (IDescl I) \n (IDescl I)\n (prodl I) \n (iso2 I (iso1 I D))\n D \n (iso2 I (iso1 I D'))\n D' p q \n proof-iso2-iso1-cases I (lpi , ( S , T )) hs = cong (S \u2192 IDescl I)\n (IDescl I)\n (pil I S) \n (\\x -> iso2 I (iso1 I (T x)))\n T \n (reflFun S \n (IDescl I)\n (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-cases I (lsigma , ( S , T )) hs = cong (S \u2192 IDescl I)\n (IDescl I)\n (sigmal I S) \n (\\x -> iso2 I (iso1 I (T x)))\n T \n (reflFun S \n (IDescl I)\n (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc Unit (descDChoice I s)\n (IMu Unit (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc (Sigma Unit (IMu Unit (\u03bb x \u2192 sigma DescDConst (descDChoice I))))\n (box Unit (sigma DescDConst (descDChoice I))\n (IMu Unit (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-iso2-iso1-casesW I Void = proof-iso2-iso1-cases I\n \n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"21354267de027c28eef0e195f783de60ff623a81","subject":"Use instance argument for iota-subscript, diaeresis and final form.","message":"Use instance argument for iota-subscript, diaeresis and final form.\n","repos":"scott-fleischman\/greek-grammar,scott-fleischman\/greek-grammar","old_file":"agda\/Text\/Greek\/Script.agda","new_file":"agda\/Text\/Greek\/Script.agda","new_contents":"module Text.Greek.Script where\n\nopen import Data.Maybe\nopen import Data.Vec\nopen import Relation.Nullary using (\u00ac_)\nopen import Relation.Binary.PropositionalEquality using (_\u2262_)\n\ndata Case : Set where\n lower upper : Case\n\ndata Letter : Set where\n \u03b1\u2032 \u03b2\u2032 \u03b3\u2032 \u03b4\u2032 \u03b5\u2032 \u03b6\u2032 \u03b7\u2032 \u03b8\u2032 \u03b9\u2032 \u03ba\u2032 \u03bb\u2032 \u03bc\u2032 \u03bd\u2032 \u03be\u2032 \u03bf\u2032 \u03c0\u2032 \u03c1\u2032 \u03c3\u2032 \u03c4\u2032 \u03c5\u2032 \u03c6\u2032 \u03c7\u2032 \u03c8\u2032 \u03c9\u2032 : Letter\n\nvowels : Vec _ _\nvowels = \u03b1\u2032 \u2237 \u03b5\u2032 \u2237 \u03b7\u2032 \u2237 \u03b9\u2032 \u2237 \u03bf\u2032 \u2237 \u03c5\u2032 \u2237 \u03c9\u2032 \u2237 []\n\nalways-short-letters : Vec _ _\nalways-short-letters = \u03b5\u2032 \u2237 \u03bf\u2032 \u2237 []\n\ndiaeresis-letters : Vec _ _\ndiaeresis-letters = \u03b9\u2032 \u2237 \u03c5\u2032 \u2237 []\n\niota-subscript-letters : Vec _ _\niota-subscript-letters = \u03b1\u2032 \u2237 \u03b7\u2032 \u2237 \u03c9\u2032 \u2237 []\n\ndata _vowel : Letter \u2192 Set where\n is-vowel : \u2200 {v} \u2192 v \u2208 vowels \u2192 v vowel\n\ndata _always-short : Letter \u2192 Set where\n is-always-short : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 always-short-letters \u2192 v always-short\n\ndata _diaeresis : Letter \u2192 Set where\n add-diaeresis : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 diaeresis-letters \u2192 v diaeresis\n\ndata _\u27e6_\u27e7-final : Letter \u2192 Case \u2192 Set where\n make-final : \u03c3\u2032 \u27e6 lower \u27e7-final\n\ndata _\u27e6_\u27e7-smooth : Letter \u2192 Case \u2192 Set where\n add-smooth-lower-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u27e6 lower \u27e7-smooth\n add-smooth-\u03c1 : \u03c1\u2032 \u27e6 lower \u27e7-smooth\n add-smooth-upper-vowel-not-\u03a5 : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2262 \u03c5\u2032 \u2192 v \u27e6 upper \u27e7-smooth\n\ndata _with-rough : Letter \u2192 Set where\n add-rough-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v with-rough\n add-rough-\u03c1 : \u03c1\u2032 with-rough\n\ndata _iota-subscript : Letter \u2192 Set where\n add-iota-subscript : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 iota-subscript-letters \u2192 v iota-subscript\n\ndata _\u27e6_\u27e7-breathing : Letter \u2192 Case \u2192 Set where\n smooth : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 \u27e6 c \u27e7-smooth \u2984 \u2192 \u2113 \u27e6 c \u27e7-breathing\n rough : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 with-rough \u2984 \u2192 \u2113 \u27e6 c \u27e7-breathing\n\ndata _long-vowel : Letter \u2192 Set where\n make-long-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 \u00ac v always-short \u2192 v long-vowel\n\ndata _accent : Letter \u2192 Set where\n acute : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n grave : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n circumflex : \u2200 {\u2113} \u2192 \u2983 p : \u2113 long-vowel \u2984 \u2192 \u2113 accent\n\ndata Token : Letter \u2192 Case \u2192 Set where\n unmarked : \u2200 {\u2113 c} \u2192 Token \u2113 c\n with-accent : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 Token \u2113 c\n with-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-diaeresis : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 diaeresis \u2984 \u2192 Token \u2113 c\n with-accent-diaeresis : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2983 p : \u2113 diaeresis \u2984 \u2192 Token \u2113 c\n with-accent-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-iota : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n final : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 \u27e6 c \u27e7-final \u2984 \u2192 Token \u2113 c\n\n-- Constructions\n\n-- \u0391 \u03b1\ninstance \u03b1-vowel : \u03b1\u2032 vowel\n\u03b1-vowel = is-vowel here\n\n\u00ac\u03b1-always-short : \u00ac \u03b1\u2032 always-short\n\u00ac\u03b1-always-short (is-always-short (there (there ())))\n\ninstance \u03b1-long-vowel : \u03b1\u2032 long-vowel\n\u03b1-long-vowel = make-long-vowel \u00ac\u03b1-always-short\n\ninstance \u03b1-smooth : \u03b1\u2032 \u27e6 lower \u27e7-smooth\n\u03b1-smooth = add-smooth-lower-vowel\n\ninstance \u0391-smooth : \u03b1\u2032 \u27e6 upper \u27e7-smooth\n\u0391-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b1-rough : \u03b1\u2032 with-rough\n\u03b1-rough = add-rough-vowel\n\ninstance \u03b1-iota-subscript : \u03b1\u2032 iota-subscript\n\u03b1-iota-subscript = add-iota-subscript here\n\n-- \u0395 \u03b5\ninstance \u03b5-vowel : \u03b5\u2032 vowel\n\u03b5-vowel = is-vowel (there here)\n\n\u03b5-always-short : \u03b5\u2032 always-short\n\u03b5-always-short = is-always-short here\n\ninstance \u03b5-smooth : \u03b5\u2032 \u27e6 lower \u27e7-smooth\n\u03b5-smooth = add-smooth-lower-vowel\n\ninstance \u0395-smooth : \u03b5\u2032 \u27e6 upper \u27e7-smooth\n\u0395-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b5-rough : \u03b5\u2032 with-rough\n\u03b5-rough = add-rough-vowel\n\n-- \u0397 \u03b7\ninstance \u03b7-vowel : \u03b7\u2032 vowel\n\u03b7-vowel = is-vowel (there (there here))\n\n\u00ac\u03b7-always-short : \u00ac \u03b7\u2032 always-short\n\u00ac\u03b7-always-short (is-always-short (there (there ())))\n\ninstance \u03b7-long-vowel : \u03b7\u2032 long-vowel\n\u03b7-long-vowel = make-long-vowel \u00ac\u03b7-always-short\n\ninstance \u03b7-smooth : \u03b7\u2032 \u27e6 lower \u27e7-smooth\n\u03b7-smooth = add-smooth-lower-vowel\n\ninstance \u0397-smooth : \u03b7\u2032 \u27e6 upper \u27e7-smooth\n\u0397-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b7-rough : \u03b7\u2032 with-rough\n\u03b7-rough = add-rough-vowel\n\n\u03b7-iota-subscript : \u03b7\u2032 iota-subscript\n\u03b7-iota-subscript = add-iota-subscript (there here)\n\n-- \u0399 \u03b9\n\ninstance \u03b9-vowel : \u03b9\u2032 vowel\n\u03b9-vowel = is-vowel (there (there (there here)))\n\n\u00ac\u03b9-always-short : \u00ac \u03b9\u2032 always-short\n\u00ac\u03b9-always-short (is-always-short (there (there ())))\n\ninstance \u03b9-long-vowel : \u03b9\u2032 long-vowel\n\u03b9-long-vowel = make-long-vowel \u00ac\u03b9-always-short\n\n\u03b9-not-\u03c5 : \u03b9\u2032 \u2262 \u03c5\u2032\n\u03b9-not-\u03c5 ()\n\ninstance \u03b9-smooth : \u03b9\u2032 \u27e6 lower \u27e7-smooth\n\u03b9-smooth = add-smooth-lower-vowel\n\ninstance \u0399-smooth : \u03b9\u2032 \u27e6 upper \u27e7-smooth\n\u0399-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b9-rough : \u03b9\u2032 with-rough\n\u03b9-rough = add-rough-vowel\n\n-- \u039f \u03bf\n\n-- Unicode\n-- U+0390 - U+03CE\n\u0390 : Token \u03b9\u2032 lower -- U+0390 GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS\n\u0390 = unmarked\n\u0391 : Token \u03b1\u2032 upper -- U+0391 GREEK CAPITAL LETTER ALPHA\n\u0391 = unmarked\n\u0392 : Token \u03b2\u2032 upper -- U+0392 GREEK CAPITAL LETTER BETA\n\u0392 = unmarked\n\u0393 : Token \u03b3\u2032 upper -- U+0393 GREEK CAPITAL LETTER GAMMA\n\u0393 = unmarked\n\u0394 : Token \u03b4\u2032 upper -- U+0394 GREEK CAPITAL LETTER DELTA\n\u0394 = unmarked\n\u0395 : Token \u03b5\u2032 upper -- U+0395 GREEK CAPITAL LETTER EPSILON\n\u0395 = unmarked\n\u0396 : Token \u03b6\u2032 upper -- U+0396 GREEK CAPITAL LETTER ZETA\n\u0396 = unmarked\n\u0397 : Token \u03b7\u2032 upper -- U+0397 GREEK CAPITAL LETTER ETA\n\u0397 = unmarked\n\u0398 : Token \u03b8\u2032 upper -- U+0398 GREEK CAPITAL LETTER THETA\n\u0398 = unmarked\n\u0399 : Token \u03b9\u2032 upper -- U+0399 GREEK CAPITAL LETTER IOTA\n\u0399 = unmarked\n\u039a : Token \u03ba\u2032 upper -- U+039A GREEK CAPITAL LETTER KAPPA\n\u039a = unmarked\n\u039b : Token \u03bb\u2032 upper -- U+039B GREEK CAPITAL LETTER LAMDA\n\u039b = unmarked\n\u039c : Token \u03bc\u2032 upper -- U+039C GREEK CAPITAL LETTER MU\n\u039c = unmarked\n\u039d : Token \u03bd\u2032 upper -- U+039D GREEK CAPITAL LETTER NU\n\u039d = unmarked\n\u039e : Token \u03be\u2032 upper -- U+039E GREEK CAPITAL LETTER XI\n\u039e = unmarked\n\u039f : Token \u03bf\u2032 upper -- U+039F GREEK CAPITAL LETTER OMICRON\n\u039f = unmarked\n\u03a0 : Token \u03c0\u2032 upper -- U+03A0 GREEK CAPITAL LETTER PI\n\u03a0 = unmarked\n\u03a1 : Token \u03c1\u2032 upper -- U+03A1 GREEK CAPITAL LETTER RHO\n\u03a1 = unmarked\n -- U+03A2 \n\u03a3 : Token \u03c3\u2032 upper -- U+03A3 GREEK CAPITAL LETTER SIGMA\n\u03a3 = unmarked\n\u03a4 : Token \u03c4\u2032 upper -- U+03A4 GREEK CAPITAL LETTER TAU\n\u03a4 = unmarked\n\u03a5 : Token \u03c5\u2032 upper -- U+03A5 GREEK CAPITAL LETTER UPSILON\n\u03a5 = unmarked\n\u03a6 : Token \u03c6\u2032 upper -- U+03A6 GREEK CAPITAL LETTER PHI\n\u03a6 = unmarked\n\u03a7 : Token \u03c7\u2032 upper -- U+03A7 GREEK CAPITAL LETTER CHI\n\u03a7 = unmarked\n\u03a8 : Token \u03c8\u2032 upper -- U+03A8 GREEK CAPITAL LETTER PSI\n\u03a8 = unmarked\n\u03a9 : Token \u03c9\u2032 upper -- U+03A9 GREEK CAPITAL LETTER OMEGA\n\u03a9 = unmarked\n\u03aa : Token \u03b9\u2032 upper -- U+03AA GREEK CAPITAL LETTER IOTA WITH DIALYTIKA\n\u03aa = unmarked\n\u03ab : Token \u03c5\u2032 upper -- U+03AB GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA\n\u03ab = unmarked\n\u03ac : Token \u03b1\u2032 lower -- U+03AC GREEK SMALL LETTER ALPHA WITH TONOS\n\u03ac = unmarked\n\u03ad : Token \u03b5\u2032 lower -- U+03AD GREEK SMALL LETTER EPSILON WITH TONOS\n\u03ad = unmarked\n\u03ae : Token \u03b7\u2032 lower -- U+03AE GREEK SMALL LETTER ETA WITH TONOS\n\u03ae = unmarked\n\u03af : Token \u03b9\u2032 lower -- U+03AF GREEK SMALL LETTER IOTA WITH TONOS\n\u03af = unmarked\n\u03b0 : Token \u03c5\u2032 lower -- U+03B0 GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS\n\u03b0 = unmarked\n\u03b1 : Token \u03b1\u2032 lower -- U+03B1 GREEK SMALL LETTER ALPHA\n\u03b1 = unmarked\n\u03b2 : Token \u03b2\u2032 lower -- U+03B2 GREEK SMALL LETTER BETA\n\u03b2 = unmarked\n\u03b3 : Token \u03b3\u2032 lower -- U+03B3 GREEK SMALL LETTER GAMMA\n\u03b3 = unmarked\n\u03b4 : Token \u03b4\u2032 lower -- U+03B4 GREEK SMALL LETTER DELTA\n\u03b4 = unmarked\n\u03b5 : Token \u03b5\u2032 lower -- U+03B5 GREEK SMALL LETTER EPSILON\n\u03b5 = unmarked\n\u03b6 : Token \u03b6\u2032 lower -- U+03B6 GREEK SMALL LETTER ZETA\n\u03b6 = unmarked\n\u03b7 : Token \u03b7\u2032 lower -- U+03B7 GREEK SMALL LETTER ETA\n\u03b7 = unmarked\n\u03b8 : Token \u03b8\u2032 lower -- U+03B8 GREEK SMALL LETTER THETA\n\u03b8 = unmarked\n\u03b9 : Token \u03b9\u2032 lower -- U+03B9 GREEK SMALL LETTER IOTA\n\u03b9 = unmarked\n\u03ba : Token \u03ba\u2032 lower -- U+03BA GREEK SMALL LETTER KAPPA\n\u03ba = unmarked\n\u2219\u03bb : Token \u03bb\u2032 lower -- U+03BB GREEK SMALL LETTER LAMDA\n\u2219\u03bb = unmarked\n\u03bc : Token \u03bc\u2032 lower -- U+03BC GREEK SMALL LETTER MU\n\u03bc = unmarked\n\u03bd : Token \u03bd\u2032 lower -- U+03BD GREEK SMALL LETTER NU\n\u03bd = unmarked\n\u03be : Token \u03be\u2032 lower -- U+03BE GREEK SMALL LETTER XI\n\u03be = unmarked\n\u03bf : Token \u03bf\u2032 lower -- U+03BF GREEK SMALL LETTER OMICRON\n\u03bf = unmarked\n\u03c0 : Token \u03c0\u2032 lower -- U+03C0 GREEK SMALL LETTER PI\n\u03c0 = unmarked\n\u03c1 : Token \u03c1\u2032 lower -- U+03C1 GREEK SMALL LETTER RHO\n\u03c1 = unmarked\n\u03c2 : Token \u03c3\u2032 lower -- U+03C2 GREEK SMALL LETTER FINAL SIGMA\n\u03c2 = unmarked\n\u03c3 : Token \u03c3\u2032 lower -- U+03C3 GREEK SMALL LETTER SIGMA\n\u03c3 = unmarked\n\u03c4 : Token \u03c4\u2032 lower -- U+03C4 GREEK SMALL LETTER TAU\n\u03c4 = unmarked\n\u03c5 : Token \u03c5\u2032 lower -- U+03C5 GREEK SMALL LETTER UPSILON\n\u03c5 = unmarked\n\u03c6 : Token \u03c6\u2032 lower -- U+03C6 GREEK SMALL LETTER PHI\n\u03c6 = unmarked\n\u03c7 : Token \u03c7\u2032 lower -- U+03C7 GREEK SMALL LETTER CHI\n\u03c7 = unmarked\n\u03c8 : Token \u03c8\u2032 lower -- U+03C8 GREEK SMALL LETTER PSI\n\u03c8 = unmarked\n\u03c9 : Token \u03c9\u2032 lower -- U+03C9 GREEK SMALL LETTER OMEGA\n\u03c9 = unmarked\n\u03ca : Token \u03b9\u2032 lower -- U+03CA GREEK SMALL LETTER IOTA WITH DIALYTIKA\n\u03ca = unmarked\n\u03cb : Token \u03c5\u2032 lower -- U+03CB GREEK SMALL LETTER UPSILON WITH DIALYTIKA\n\u03cb = unmarked\n\u03cc : Token \u03bf\u2032 lower -- U+03CC GREEK SMALL LETTER OMICRON WITH TONOS\n\u03cc = unmarked\n\u03cd : Token \u03c5\u2032 lower -- U+03CD GREEK SMALL LETTER UPSILON WITH TONOS\n\u03cd = unmarked\n\u03ce : Token \u03c9\u2032 lower -- U+03CE GREEK SMALL LETTER OMEGA WITH TONOS\n\u03ce = unmarked\n\n\n\u1f00 : Token \u03b1\u2032 lower\n\u1f00 = with-breathing smooth\n\u1f01 : Token \u03b1\u2032 lower\n\u1f01 = with-breathing rough\n\u1f02 : Token \u03b1\u2032 lower\n\u1f02 = with-accent-breathing grave smooth\n\u1f03 : Token \u03b1\u2032 lower\n\u1f03 = with-accent-breathing grave rough\n\u1f04 : Token \u03b1\u2032 lower\n\u1f04 = with-accent-breathing acute smooth\n\u1f05 : Token \u03b1\u2032 lower\n\u1f05 = with-accent-breathing acute rough\n\u1f06 : Token \u03b1\u2032 lower\n\u1f06 = with-accent-breathing circumflex smooth\n\u1f07 : Token \u03b1\u2032 lower\n\u1f07 = with-accent-breathing circumflex rough\n\u1f08 : Token \u03b1\u2032 upper\n\u1f08 = with-breathing smooth\n\u1f09 : Token \u03b1\u2032 upper\n\u1f09 = with-breathing rough\n\u1f0a : Token \u03b1\u2032 upper\n\u1f0a = with-accent-breathing grave smooth\n\u1f0b : Token \u03b1\u2032 upper\n\u1f0b = with-accent-breathing grave rough\n\u1f0c : Token \u03b1\u2032 upper\n\u1f0c = with-accent-breathing acute smooth\n\u1f0d : Token \u03b1\u2032 upper\n\u1f0d = with-accent-breathing acute rough\n\u1f0e : Token \u03b1\u2032 upper\n\u1f0e = with-accent-breathing circumflex smooth\n\u1f0f : Token \u03b1\u2032 upper\n\u1f0f = with-accent-breathing circumflex rough\n\n-- U+1F7x\n\u1f70 : Token \u03b1\u2032 lower\n\u1f70 = with-accent grave\n\u1f71 : Token \u03b1\u2032 lower\n\u1f71 = with-accent acute\n\n-- U+1F8x\n\u1f80 : Token \u03b1\u2032 lower\n\u1f80 = with-breathing-iota smooth\n\u1f81 : Token \u03b1\u2032 lower\n\u1f81 = with-breathing-iota rough\n\u1f82 : Token \u03b1\u2032 lower\n\u1f82 = with-accent-breathing-iota grave smooth\n\u1f83 : Token \u03b1\u2032 lower\n\u1f83 = with-accent-breathing-iota grave rough\n\u1f84 : Token \u03b1\u2032 lower\n\u1f84 = with-accent-breathing-iota acute smooth\n\u1f85 : Token \u03b1\u2032 lower\n\u1f85 = with-accent-breathing-iota acute rough\n\u1f86 : Token \u03b1\u2032 lower\n\u1f86 = with-accent-breathing-iota circumflex smooth\n\u1f87 : Token \u03b1\u2032 lower\n\u1f87 = with-accent-breathing-iota circumflex rough\n\u1f88 : Token \u03b1\u2032 upper\n\u1f88 = with-breathing-iota smooth\n\u1f89 : Token \u03b1\u2032 upper\n\u1f89 = with-breathing-iota rough\n\u1f8a : Token \u03b1\u2032 upper\n\u1f8a = with-accent-breathing-iota grave smooth\n\u1f8b : Token \u03b1\u2032 upper\n\u1f8b = with-accent-breathing-iota grave rough\n\u1f8c : Token \u03b1\u2032 upper\n\u1f8c = with-accent-breathing-iota acute smooth\n\u1f8d : Token \u03b1\u2032 upper\n\u1f8d = with-accent-breathing-iota acute rough\n\u1f8e : Token \u03b1\u2032 upper\n\u1f8e = with-accent-breathing-iota circumflex smooth\n\u1f8f : Token \u03b1\u2032 upper\n\u1f8f = with-accent-breathing-iota circumflex rough\n\n-- U+1FBx\n-- \u1fb0\n-- \u1fb1\n\u1fb2 : Token \u03b1\u2032 lower\n\u1fb2 = with-accent-iota grave\n\u1fb3 : Token \u03b1\u2032 lower\n\u1fb3 = with-iota\n\u1fb4 : Token \u03b1\u2032 lower\n\u1fb4 = with-accent-iota acute\n\u1fb6 : Token \u03b1\u2032 lower\n\u1fb6 = with-accent circumflex\n\u1fb7 : Token \u03b1\u2032 lower\n\u1fb7 = with-accent-iota circumflex\n-- \u1fb8\n-- \u1fb9\n\u1fba : Token \u03b1\u2032 upper\n\u1fba = with-accent grave\n\u1fbb : Token \u03b1\u2032 upper\n\u1fbb = with-accent acute\n\u1fbc : Token \u03b1\u2032 upper\n\u1fbc = with-iota\n\n-- Mapping\n\ndata Accent : Set where\n acute-mark grave-mark circumflex-mark : Accent\n\nletter-to-accent : \u2200 {v} \u2192 v accent \u2192 Accent\nletter-to-accent acute = acute-mark\nletter-to-accent grave = grave-mark\nletter-to-accent circumflex = circumflex-mark\n\nget-accent : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Accent\nget-accent (with-accent a) = just (letter-to-accent a)\nget-accent (with-accent-breathing a _) = just (letter-to-accent a)\nget-accent (with-accent-breathing-iota a _) = just (letter-to-accent a)\nget-accent (with-accent-diaeresis a) = just (letter-to-accent a)\nget-accent (with-accent-iota a) = just (letter-to-accent a)\nget-accent _ = nothing\n\ndata Breathing : Set where\n smooth-mark rough-mark : Breathing\n\nletter-to-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Breathing\nletter-to-breathing smooth = smooth-mark\nletter-to-breathing rough = rough-mark\n\nget-breathing : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Breathing\nget-breathing (with-breathing x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing _ x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing-iota _ x) = just (letter-to-breathing x)\nget-breathing (with-breathing-iota x) = just (letter-to-breathing x)\nget-breathing _ = nothing\n\ndata IotaSubscript : Set where\n iota-subscript-mark : IotaSubscript\n\nget-iota-subscript : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe IotaSubscript\nget-iota-subscript (with-accent-breathing-iota _ _) = just iota-subscript-mark\nget-iota-subscript (with-accent-iota _) = just iota-subscript-mark\nget-iota-subscript (with-breathing-iota _) = just iota-subscript-mark\nget-iota-subscript _ = nothing\n\ndata Diaeresis : Set where\n diaeresis-mark : Diaeresis\n\nget-diaeresis : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Diaeresis\nget-diaeresis with-diaeresis = just diaeresis-mark\nget-diaeresis (with-accent-diaeresis _) = just diaeresis-mark\nget-diaeresis _ = nothing\n\ndata FinalForm : Set where\n final-form : FinalForm\n\nget-final-form : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe FinalForm\nget-final-form final = just final-form\nget-final-form _ = nothing\n","old_contents":"module Text.Greek.Script where\n\nopen import Data.Maybe\nopen import Data.Vec\nopen import Relation.Nullary using (\u00ac_)\nopen import Relation.Binary.PropositionalEquality using (_\u2262_)\n\ndata Case : Set where\n lower upper : Case\n\ndata Letter : Set where\n \u03b1\u2032 \u03b2\u2032 \u03b3\u2032 \u03b4\u2032 \u03b5\u2032 \u03b6\u2032 \u03b7\u2032 \u03b8\u2032 \u03b9\u2032 \u03ba\u2032 \u03bb\u2032 \u03bc\u2032 \u03bd\u2032 \u03be\u2032 \u03bf\u2032 \u03c0\u2032 \u03c1\u2032 \u03c3\u2032 \u03c4\u2032 \u03c5\u2032 \u03c6\u2032 \u03c7\u2032 \u03c8\u2032 \u03c9\u2032 : Letter\n\nvowels : Vec _ _\nvowels = \u03b1\u2032 \u2237 \u03b5\u2032 \u2237 \u03b7\u2032 \u2237 \u03b9\u2032 \u2237 \u03bf\u2032 \u2237 \u03c5\u2032 \u2237 \u03c9\u2032 \u2237 []\n\nalways-short-letters : Vec _ _\nalways-short-letters = \u03b5\u2032 \u2237 \u03bf\u2032 \u2237 []\n\ndiaeresis-letters : Vec _ _\ndiaeresis-letters = \u03b9\u2032 \u2237 \u03c5\u2032 \u2237 []\n\niota-subscript-letters : Vec _ _\niota-subscript-letters = \u03b1\u2032 \u2237 \u03b7\u2032 \u2237 \u03c9\u2032 \u2237 []\n\ndata _vowel : Letter \u2192 Set where\n is-vowel : \u2200 {v} \u2192 v \u2208 vowels \u2192 v vowel\n\ndata _always-short : Letter \u2192 Set where\n is-always-short : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 always-short-letters \u2192 v always-short\n\ndata _diaeresis : Letter \u2192 Set where\n add-diaeresis : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 diaeresis-letters \u2192 v diaeresis\n\ndata _\u27e6_\u27e7-final : Letter \u2192 Case \u2192 Set where\n make-final : \u03c3\u2032 \u27e6 lower \u27e7-final\n\ndata _\u27e6_\u27e7-smooth : Letter \u2192 Case \u2192 Set where\n add-smooth-lower-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u27e6 lower \u27e7-smooth\n add-smooth-\u03c1 : \u03c1\u2032 \u27e6 lower \u27e7-smooth\n add-smooth-upper-vowel-not-\u03a5 : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2262 \u03c5\u2032 \u2192 v \u27e6 upper \u27e7-smooth\n\ndata _with-rough : Letter \u2192 Set where\n add-rough-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v with-rough\n add-rough-\u03c1 : \u03c1\u2032 with-rough\n\ndata _iota-subscript : Letter \u2192 Set where\n add-iota-subscript : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 iota-subscript-letters \u2192 v iota-subscript\n\ndata _\u27e6_\u27e7-breathing : Letter \u2192 Case \u2192 Set where\n smooth : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 \u27e6 c \u27e7-smooth \u2984 \u2192 \u2113 \u27e6 c \u27e7-breathing\n rough : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 with-rough \u2984 \u2192 \u2113 \u27e6 c \u27e7-breathing\n\ndata _long-vowel : Letter \u2192 Set where\n make-long-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 \u00ac v always-short \u2192 v long-vowel\n\ndata _accent : Letter \u2192 Set where\n acute : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n grave : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n circumflex : \u2200 {\u2113} \u2192 \u2983 p : \u2113 long-vowel \u2984 \u2192 \u2113 accent\n\ndata Token : Letter \u2192 Case \u2192 Set where\n unmarked : \u2200 {\u2113 c} \u2192 Token \u2113 c\n with-accent : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 Token \u2113 c\n with-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2113 iota-subscript \u2192 Token \u2113 c\n with-diaeresis : \u2200 {\u2113 c} \u2192 \u2113 diaeresis \u2192 Token \u2113 c\n with-accent-diaeresis : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 diaeresis \u2192 Token \u2113 c\n with-accent-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 iota-subscript \u2192 Token \u2113 c\n with-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2113 iota-subscript \u2192 Token \u2113 c\n with-iota : \u2200 {\u2113 c} \u2192 \u2113 iota-subscript \u2192 Token \u2113 c\n final : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-final \u2192 Token \u2113 c\n\n-- Constructions\n\n-- \u0391 \u03b1\ninstance \u03b1-vowel : \u03b1\u2032 vowel\n\u03b1-vowel = is-vowel here\n\n\u00ac\u03b1-always-short : \u00ac \u03b1\u2032 always-short\n\u00ac\u03b1-always-short (is-always-short (there (there ())))\n\ninstance \u03b1-long-vowel : \u03b1\u2032 long-vowel\n\u03b1-long-vowel = make-long-vowel \u00ac\u03b1-always-short\n\ninstance \u03b1-smooth : \u03b1\u2032 \u27e6 lower \u27e7-smooth\n\u03b1-smooth = add-smooth-lower-vowel\n\ninstance \u0391-smooth : \u03b1\u2032 \u27e6 upper \u27e7-smooth\n\u0391-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b1-rough : \u03b1\u2032 with-rough\n\u03b1-rough = add-rough-vowel\n\n\u03b1-iota-subscript : \u03b1\u2032 iota-subscript\n\u03b1-iota-subscript = add-iota-subscript here\n\n-- \u0395 \u03b5\ninstance \u03b5-vowel : \u03b5\u2032 vowel\n\u03b5-vowel = is-vowel (there here)\n\n\u03b5-always-short : \u03b5\u2032 always-short\n\u03b5-always-short = is-always-short here\n\ninstance \u03b5-smooth : \u03b5\u2032 \u27e6 lower \u27e7-smooth\n\u03b5-smooth = add-smooth-lower-vowel\n\ninstance \u0395-smooth : \u03b5\u2032 \u27e6 upper \u27e7-smooth\n\u0395-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b5-rough : \u03b5\u2032 with-rough\n\u03b5-rough = add-rough-vowel\n\n-- \u0397 \u03b7\ninstance \u03b7-vowel : \u03b7\u2032 vowel\n\u03b7-vowel = is-vowel (there (there here))\n\n\u00ac\u03b7-always-short : \u00ac \u03b7\u2032 always-short\n\u00ac\u03b7-always-short (is-always-short (there (there ())))\n\ninstance \u03b7-long-vowel : \u03b7\u2032 long-vowel\n\u03b7-long-vowel = make-long-vowel \u00ac\u03b7-always-short\n\ninstance \u03b7-smooth : \u03b7\u2032 \u27e6 lower \u27e7-smooth\n\u03b7-smooth = add-smooth-lower-vowel\n\ninstance \u0397-smooth : \u03b7\u2032 \u27e6 upper \u27e7-smooth\n\u0397-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b7-rough : \u03b7\u2032 with-rough\n\u03b7-rough = add-rough-vowel\n\n\u03b7-iota-subscript : \u03b7\u2032 iota-subscript\n\u03b7-iota-subscript = add-iota-subscript (there here)\n\n-- \u0399 \u03b9\n\ninstance \u03b9-vowel : \u03b9\u2032 vowel\n\u03b9-vowel = is-vowel (there (there (there here)))\n\n\u00ac\u03b9-always-short : \u00ac \u03b9\u2032 always-short\n\u00ac\u03b9-always-short (is-always-short (there (there ())))\n\ninstance \u03b9-long-vowel : \u03b9\u2032 long-vowel\n\u03b9-long-vowel = make-long-vowel \u00ac\u03b9-always-short\n\n\u03b9-not-\u03c5 : \u03b9\u2032 \u2262 \u03c5\u2032\n\u03b9-not-\u03c5 ()\n\ninstance \u03b9-smooth : \u03b9\u2032 \u27e6 lower \u27e7-smooth\n\u03b9-smooth = add-smooth-lower-vowel\n\ninstance \u0399-smooth : \u03b9\u2032 \u27e6 upper \u27e7-smooth\n\u0399-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b9-rough : \u03b9\u2032 with-rough\n\u03b9-rough = add-rough-vowel\n\n-- \u039f \u03bf\n\n-- Unicode\n-- U+0390 - U+03CE\n\u0390 : Token \u03b9\u2032 lower -- U+0390 GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS\n\u0390 = unmarked\n\u0391 : Token \u03b1\u2032 upper -- U+0391 GREEK CAPITAL LETTER ALPHA\n\u0391 = unmarked\n\u0392 : Token \u03b2\u2032 upper -- U+0392 GREEK CAPITAL LETTER BETA\n\u0392 = unmarked\n\u0393 : Token \u03b3\u2032 upper -- U+0393 GREEK CAPITAL LETTER GAMMA\n\u0393 = unmarked\n\u0394 : Token \u03b4\u2032 upper -- U+0394 GREEK CAPITAL LETTER DELTA\n\u0394 = unmarked\n\u0395 : Token \u03b5\u2032 upper -- U+0395 GREEK CAPITAL LETTER EPSILON\n\u0395 = unmarked\n\u0396 : Token \u03b6\u2032 upper -- U+0396 GREEK CAPITAL LETTER ZETA\n\u0396 = unmarked\n\u0397 : Token \u03b7\u2032 upper -- U+0397 GREEK CAPITAL LETTER ETA\n\u0397 = unmarked\n\u0398 : Token \u03b8\u2032 upper -- U+0398 GREEK CAPITAL LETTER THETA\n\u0398 = unmarked\n\u0399 : Token \u03b9\u2032 upper -- U+0399 GREEK CAPITAL LETTER IOTA\n\u0399 = unmarked\n\u039a : Token \u03ba\u2032 upper -- U+039A GREEK CAPITAL LETTER KAPPA\n\u039a = unmarked\n\u039b : Token \u03bb\u2032 upper -- U+039B GREEK CAPITAL LETTER LAMDA\n\u039b = unmarked\n\u039c : Token \u03bc\u2032 upper -- U+039C GREEK CAPITAL LETTER MU\n\u039c = unmarked\n\u039d : Token \u03bd\u2032 upper -- U+039D GREEK CAPITAL LETTER NU\n\u039d = unmarked\n\u039e : Token \u03be\u2032 upper -- U+039E GREEK CAPITAL LETTER XI\n\u039e = unmarked\n\u039f : Token \u03bf\u2032 upper -- U+039F GREEK CAPITAL LETTER OMICRON\n\u039f = unmarked\n\u03a0 : Token \u03c0\u2032 upper -- U+03A0 GREEK CAPITAL LETTER PI\n\u03a0 = unmarked\n\u03a1 : Token \u03c1\u2032 upper -- U+03A1 GREEK CAPITAL LETTER RHO\n\u03a1 = unmarked\n -- U+03A2 \n\u03a3 : Token \u03c3\u2032 upper -- U+03A3 GREEK CAPITAL LETTER SIGMA\n\u03a3 = unmarked\n\u03a4 : Token \u03c4\u2032 upper -- U+03A4 GREEK CAPITAL LETTER TAU\n\u03a4 = unmarked\n\u03a5 : Token \u03c5\u2032 upper -- U+03A5 GREEK CAPITAL LETTER UPSILON\n\u03a5 = unmarked\n\u03a6 : Token \u03c6\u2032 upper -- U+03A6 GREEK CAPITAL LETTER PHI\n\u03a6 = unmarked\n\u03a7 : Token \u03c7\u2032 upper -- U+03A7 GREEK CAPITAL LETTER CHI\n\u03a7 = unmarked\n\u03a8 : Token \u03c8\u2032 upper -- U+03A8 GREEK CAPITAL LETTER PSI\n\u03a8 = unmarked\n\u03a9 : Token \u03c9\u2032 upper -- U+03A9 GREEK CAPITAL LETTER OMEGA\n\u03a9 = unmarked\n\u03aa : Token \u03b9\u2032 upper -- U+03AA GREEK CAPITAL LETTER IOTA WITH DIALYTIKA\n\u03aa = unmarked\n\u03ab : Token \u03c5\u2032 upper -- U+03AB GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA\n\u03ab = unmarked\n\u03ac : Token \u03b1\u2032 lower -- U+03AC GREEK SMALL LETTER ALPHA WITH TONOS\n\u03ac = unmarked\n\u03ad : Token \u03b5\u2032 lower -- U+03AD GREEK SMALL LETTER EPSILON WITH TONOS\n\u03ad = unmarked\n\u03ae : Token \u03b7\u2032 lower -- U+03AE GREEK SMALL LETTER ETA WITH TONOS\n\u03ae = unmarked\n\u03af : Token \u03b9\u2032 lower -- U+03AF GREEK SMALL LETTER IOTA WITH TONOS\n\u03af = unmarked\n\u03b0 : Token \u03c5\u2032 lower -- U+03B0 GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS\n\u03b0 = unmarked\n\u03b1 : Token \u03b1\u2032 lower -- U+03B1 GREEK SMALL LETTER ALPHA\n\u03b1 = unmarked\n\u03b2 : Token \u03b2\u2032 lower -- U+03B2 GREEK SMALL LETTER BETA\n\u03b2 = unmarked\n\u03b3 : Token \u03b3\u2032 lower -- U+03B3 GREEK SMALL LETTER GAMMA\n\u03b3 = unmarked\n\u03b4 : Token \u03b4\u2032 lower -- U+03B4 GREEK SMALL LETTER DELTA\n\u03b4 = unmarked\n\u03b5 : Token \u03b5\u2032 lower -- U+03B5 GREEK SMALL LETTER EPSILON\n\u03b5 = unmarked\n\u03b6 : Token \u03b6\u2032 lower -- U+03B6 GREEK SMALL LETTER ZETA\n\u03b6 = unmarked\n\u03b7 : Token \u03b7\u2032 lower -- U+03B7 GREEK SMALL LETTER ETA\n\u03b7 = unmarked\n\u03b8 : Token \u03b8\u2032 lower -- U+03B8 GREEK SMALL LETTER THETA\n\u03b8 = unmarked\n\u03b9 : Token \u03b9\u2032 lower -- U+03B9 GREEK SMALL LETTER IOTA\n\u03b9 = unmarked\n\u03ba : Token \u03ba\u2032 lower -- U+03BA GREEK SMALL LETTER KAPPA\n\u03ba = unmarked\n\u2219\u03bb : Token \u03bb\u2032 lower -- U+03BB GREEK SMALL LETTER LAMDA\n\u2219\u03bb = unmarked\n\u03bc : Token \u03bc\u2032 lower -- U+03BC GREEK SMALL LETTER MU\n\u03bc = unmarked\n\u03bd : Token \u03bd\u2032 lower -- U+03BD GREEK SMALL LETTER NU\n\u03bd = unmarked\n\u03be : Token \u03be\u2032 lower -- U+03BE GREEK SMALL LETTER XI\n\u03be = unmarked\n\u03bf : Token \u03bf\u2032 lower -- U+03BF GREEK SMALL LETTER OMICRON\n\u03bf = unmarked\n\u03c0 : Token \u03c0\u2032 lower -- U+03C0 GREEK SMALL LETTER PI\n\u03c0 = unmarked\n\u03c1 : Token \u03c1\u2032 lower -- U+03C1 GREEK SMALL LETTER RHO\n\u03c1 = unmarked\n\u03c2 : Token \u03c3\u2032 lower -- U+03C2 GREEK SMALL LETTER FINAL SIGMA\n\u03c2 = unmarked\n\u03c3 : Token \u03c3\u2032 lower -- U+03C3 GREEK SMALL LETTER SIGMA\n\u03c3 = unmarked\n\u03c4 : Token \u03c4\u2032 lower -- U+03C4 GREEK SMALL LETTER TAU\n\u03c4 = unmarked\n\u03c5 : Token \u03c5\u2032 lower -- U+03C5 GREEK SMALL LETTER UPSILON\n\u03c5 = unmarked\n\u03c6 : Token \u03c6\u2032 lower -- U+03C6 GREEK SMALL LETTER PHI\n\u03c6 = unmarked\n\u03c7 : Token \u03c7\u2032 lower -- U+03C7 GREEK SMALL LETTER CHI\n\u03c7 = unmarked\n\u03c8 : Token \u03c8\u2032 lower -- U+03C8 GREEK SMALL LETTER PSI\n\u03c8 = unmarked\n\u03c9 : Token \u03c9\u2032 lower -- U+03C9 GREEK SMALL LETTER OMEGA\n\u03c9 = unmarked\n\u03ca : Token \u03b9\u2032 lower -- U+03CA GREEK SMALL LETTER IOTA WITH DIALYTIKA\n\u03ca = unmarked\n\u03cb : Token \u03c5\u2032 lower -- U+03CB GREEK SMALL LETTER UPSILON WITH DIALYTIKA\n\u03cb = unmarked\n\u03cc : Token \u03bf\u2032 lower -- U+03CC GREEK SMALL LETTER OMICRON WITH TONOS\n\u03cc = unmarked\n\u03cd : Token \u03c5\u2032 lower -- U+03CD GREEK SMALL LETTER UPSILON WITH TONOS\n\u03cd = unmarked\n\u03ce : Token \u03c9\u2032 lower -- U+03CE GREEK SMALL LETTER OMEGA WITH TONOS\n\u03ce = unmarked\n\n\n\u1f00 : Token \u03b1\u2032 lower\n\u1f00 = with-breathing smooth\n\u1f01 : Token \u03b1\u2032 lower\n\u1f01 = with-breathing rough\n\u1f02 : Token \u03b1\u2032 lower\n\u1f02 = with-accent-breathing grave smooth\n\u1f03 : Token \u03b1\u2032 lower\n\u1f03 = with-accent-breathing grave rough\n\u1f04 : Token \u03b1\u2032 lower\n\u1f04 = with-accent-breathing acute smooth\n\u1f05 : Token \u03b1\u2032 lower\n\u1f05 = with-accent-breathing acute rough\n\u1f06 : Token \u03b1\u2032 lower\n\u1f06 = with-accent-breathing circumflex smooth\n\u1f07 : Token \u03b1\u2032 lower\n\u1f07 = with-accent-breathing circumflex rough\n\u1f08 : Token \u03b1\u2032 upper\n\u1f08 = with-breathing smooth\n\u1f09 : Token \u03b1\u2032 upper\n\u1f09 = with-breathing rough\n\u1f0a : Token \u03b1\u2032 upper\n\u1f0a = with-accent-breathing grave smooth\n\u1f0b : Token \u03b1\u2032 upper\n\u1f0b = with-accent-breathing grave rough\n\u1f0c : Token \u03b1\u2032 upper\n\u1f0c = with-accent-breathing acute smooth\n\u1f0d : Token \u03b1\u2032 upper\n\u1f0d = with-accent-breathing acute rough\n\u1f0e : Token \u03b1\u2032 upper\n\u1f0e = with-accent-breathing circumflex smooth\n\u1f0f : Token \u03b1\u2032 upper\n\u1f0f = with-accent-breathing circumflex rough\n\n-- U+1F7x\n\u1f70 : Token \u03b1\u2032 lower\n\u1f70 = with-accent grave\n\u1f71 : Token \u03b1\u2032 lower\n\u1f71 = with-accent acute\n\n-- U+1F8x\n\u1f80 : Token \u03b1\u2032 lower\n\u1f80 = with-breathing-iota smooth \u03b1-iota-subscript\n\u1f81 : Token \u03b1\u2032 lower\n\u1f81 = with-breathing-iota rough \u03b1-iota-subscript\n\u1f82 : Token \u03b1\u2032 lower\n\u1f82 = with-accent-breathing-iota grave smooth \u03b1-iota-subscript\n\u1f83 : Token \u03b1\u2032 lower\n\u1f83 = with-accent-breathing-iota grave rough \u03b1-iota-subscript\n\u1f84 : Token \u03b1\u2032 lower\n\u1f84 = with-accent-breathing-iota acute smooth \u03b1-iota-subscript\n\u1f85 : Token \u03b1\u2032 lower\n\u1f85 = with-accent-breathing-iota acute rough \u03b1-iota-subscript\n\u1f86 : Token \u03b1\u2032 lower\n\u1f86 = with-accent-breathing-iota circumflex smooth \u03b1-iota-subscript\n\u1f87 : Token \u03b1\u2032 lower\n\u1f87 = with-accent-breathing-iota circumflex rough \u03b1-iota-subscript\n\u1f88 : Token \u03b1\u2032 upper\n\u1f88 = with-breathing-iota smooth \u03b1-iota-subscript\n\u1f89 : Token \u03b1\u2032 upper\n\u1f89 = with-breathing-iota rough \u03b1-iota-subscript\n\u1f8a : Token \u03b1\u2032 upper\n\u1f8a = with-accent-breathing-iota grave smooth \u03b1-iota-subscript\n\u1f8b : Token \u03b1\u2032 upper\n\u1f8b = with-accent-breathing-iota grave rough \u03b1-iota-subscript\n\u1f8c : Token \u03b1\u2032 upper\n\u1f8c = with-accent-breathing-iota acute smooth \u03b1-iota-subscript\n\u1f8d : Token \u03b1\u2032 upper\n\u1f8d = with-accent-breathing-iota acute rough \u03b1-iota-subscript\n\u1f8e : Token \u03b1\u2032 upper\n\u1f8e = with-accent-breathing-iota circumflex smooth \u03b1-iota-subscript\n\u1f8f : Token \u03b1\u2032 upper\n\u1f8f = with-accent-breathing-iota circumflex rough \u03b1-iota-subscript\n\n-- U+1FBx\n-- \u1fb0\n-- \u1fb1\n\u1fb2 : Token \u03b1\u2032 lower\n\u1fb2 = with-accent-iota grave \u03b1-iota-subscript\n\u1fb3 : Token \u03b1\u2032 lower\n\u1fb3 = with-iota \u03b1-iota-subscript\n\u1fb4 : Token \u03b1\u2032 lower\n\u1fb4 = with-accent-iota acute \u03b1-iota-subscript\n\u1fb6 : Token \u03b1\u2032 lower\n\u1fb6 = with-accent circumflex\n\u1fb7 : Token \u03b1\u2032 lower\n\u1fb7 = with-accent-iota circumflex \u03b1-iota-subscript\n-- \u1fb8\n-- \u1fb9\n\u1fba : Token \u03b1\u2032 upper\n\u1fba = with-accent grave\n\u1fbb : Token \u03b1\u2032 upper\n\u1fbb = with-accent acute\n\u1fbc : Token \u03b1\u2032 upper\n\u1fbc = with-iota \u03b1-iota-subscript\n\n-- Mapping\n\ndata Accent : Set where\n acute-mark grave-mark circumflex-mark : Accent\n\nletter-to-accent : \u2200 {v} \u2192 v accent \u2192 Accent\nletter-to-accent acute = acute-mark\nletter-to-accent grave = grave-mark\nletter-to-accent circumflex = circumflex-mark\n\nget-accent : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Accent\nget-accent (with-accent a) = just (letter-to-accent a)\nget-accent (with-accent-breathing a _) = just (letter-to-accent a)\nget-accent (with-accent-breathing-iota a _ _) = just (letter-to-accent a)\nget-accent (with-accent-diaeresis a _) = just (letter-to-accent a)\nget-accent (with-accent-iota a _) = just (letter-to-accent a)\nget-accent _ = nothing\n\ndata Breathing : Set where\n smooth-mark rough-mark : Breathing\n\nletter-to-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Breathing\nletter-to-breathing smooth = smooth-mark\nletter-to-breathing rough = rough-mark\n\nget-breathing : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Breathing\nget-breathing (with-breathing x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing _ x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing-iota _ x _) = just (letter-to-breathing x)\nget-breathing (with-breathing-iota x _) = just (letter-to-breathing x)\nget-breathing _ = nothing\n\ndata IotaSubscript : Set where\n iota-subscript-mark : IotaSubscript\n\nget-iota-subscript : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe IotaSubscript\nget-iota-subscript (with-accent-breathing-iota _ _ _) = just iota-subscript-mark\nget-iota-subscript (with-accent-iota _ _) = just iota-subscript-mark\nget-iota-subscript (with-breathing-iota _ _) = just iota-subscript-mark\nget-iota-subscript _ = nothing\n\ndata Diaeresis : Set where\n diaeresis-mark : Diaeresis\n\nget-diaeresis : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Diaeresis\nget-diaeresis (with-diaeresis _) = just diaeresis-mark\nget-diaeresis (with-accent-diaeresis _ _) = just diaeresis-mark\nget-diaeresis _ = nothing\n\ndata FinalForm : Set where\n final-form : FinalForm\n\nget-final-form : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe FinalForm\nget-final-form (final _) = just final-form\nget-final-form _ = nothing\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"4282673d8da7771de7f995705e4999074f98cbad","subject":"Added x+11\u223810\u2261Sx.","message":"Added x+11\u223810\u2261Sx.\n\nIgnore-this: e605e7c3f021e9be583d6affc58f9aec\n\ndarcs-hash:20110218165317-3bd4e-885efb1503edb19ed466ae8fb96f4a60342db5e6.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/McCarthy91\/ArithmeticATP.agda","new_file":"Draft\/McCarthy91\/ArithmeticATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties used by the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.ArithmeticATP where\n\nopen import LTC.Base\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\n\n------------------------------------------------------------------------------\n\npostulate\n 1+x-N : \u2200 {n} \u2192 N n \u2192 N (one + n)\n 11+x-N : \u2200 {n} \u2192 N n \u2192 N (eleven + n)\n x\u223810-N : \u2200 {n} \u2192 N n \u2192 N (n \u2238 ten)\n x+11-N : \u2200 {n} \u2192 N n \u2192 N (n + eleven)\n{-# ATP prove 1+x-N #-}\n{-# ATP prove 11+x-N #-}\n{-# ATP prove x\u223810-N \u2238-N N10 #-}\n{-# ATP prove x+11-N 11+x-N +-comm #-}\n\npostulate\n 91\u2261[100+11\u223810]\u223810 : (one-hundred + eleven \u2238 ten) \u2238 ten \u2261 ninety-one\n{-# ATP prove 91\u2261[100+11\u223810]\u223810 #-}\n\npostulate\n 100\u226189+11 : eighty-nine + eleven \u2261 one-hundred\n 101\u226190+11\u2261101 : ninety + eleven \u2261 hundred-one\n 101\u2261100+11-10 : (one-hundred + eleven) \u2238 ten \u2261 hundred-one\n 102\u226191+11 : ninety-one + eleven \u2261 hundred-two\n 103\u226192+1 : ninety-two + eleven \u2261 hundred-three\n 104\u226193+11 : ninety-three + eleven \u2261 hundred-four\n 105\u226194+11 : ninety-four + eleven \u2261 hundred-five\n 106\u226195+11 : ninety-five + eleven \u2261 hundred-six\n 107\u226196+11 : ninety-six + eleven \u2261 hundred-seven\n 108\u226197+11 : ninety-seven + eleven \u2261 hundred-eight\n 109\u226199+11 : ninety-eight + eleven \u2261 hundred-nine\n 110\u226199+11 : ninety-nine + eleven \u2261 hundred-ten\n 111\u2261100+11 : one-hundred + eleven \u2261 hundred-eleven\n\n{-# ATP prove 100\u226189+11 #-}\n{-# ATP prove 101\u226190+11\u2261101 #-}\n{-# ATP prove 101\u2261100+11-10 #-}\n{-# ATP prove 102\u226191+11 #-}\n{-# ATP prove 103\u226192+1 #-}\n{-# ATP prove 104\u226193+11 #-}\n{-# ATP prove 105\u226194+11 #-}\n{-# ATP prove 106\u226195+11 #-}\n{-# ATP prove 107\u226196+11 #-}\n{-# ATP prove 108\u226197+11 #-}\n{-# ATP prove 109\u226199+11 #-}\n{-# ATP prove 110\u226199+11 #-}\n{-# ATP prove 111\u2261100+11 #-}\n\npostulate\n 101>100' : GT hundred-one one-hundred\n 101>100 : GT ((one-hundred + eleven) \u2238 ten) one-hundred\n 102>100 : GT hundred-two one-hundred\n 103>100 : GT hundred-three one-hundred\n 104>100 : GT hundred-four one-hundred\n 105>100 : GT hundred-five one-hundred\n 106>100 : GT hundred-six one-hundred\n 107>100 : GT hundred-seven one-hundred\n 108>100 : GT hundred-eight one-hundred\n 109>100 : GT hundred-nine one-hundred\n 110>100 : GT hundred-ten one-hundred\n 111>100' : GT hundred-eleven one-hundred\n 111>100 : GT (one-hundred + eleven) one-hundred\n{-# ATP prove 101>100' #-}\n{-# ATP prove 101>100 101>100' 101\u2261100+11-10 #-}\n{-# ATP prove 102>100 #-}\n{-# ATP prove 103>100 #-}\n{-# ATP prove 104>100 #-}\n{-# ATP prove 105>100 #-}\n{-# ATP prove 106>100 #-}\n{-# ATP prove 107>100 #-}\n{-# ATP prove 108>100 #-}\n{-# ATP prove 109>100 #-}\n{-# ATP prove 110>100 #-}\n{-# ATP prove 111>100' #-}\n{-# ATP prove 111>100 111>100' 111\u2261100+11 #-}\n\npostulate\n 99+11>100 : GT (ninety-nine + eleven) one-hundred\n 98+11>100 : GT (ninety-eight + eleven) one-hundred\n 97+11>100 : GT (ninety-seven + eleven) one-hundred\n 96+11>100 : GT (ninety-six + eleven) one-hundred\n 95+11>100 : GT (ninety-five + eleven) one-hundred\n 94+11>100 : GT (ninety-four + eleven) one-hundred\n 93+11>100 : GT (ninety-three + eleven) one-hundred\n 92+11>100 : GT (ninety-two + eleven) one-hundred\n 91+11>100 : GT (ninety-one + eleven) one-hundred\n 90+11>100 : GT (ninety + eleven) one-hundred\n{-# ATP prove 99+11>100 110>100 110\u226199+11 #-}\n{-# ATP prove 98+11>100 109>100 109\u226199+11 #-}\n{-# ATP prove 97+11>100 108>100 108\u226197+11 #-}\n{-# ATP prove 96+11>100 107>100 107\u226196+11 #-}\n{-# ATP prove 95+11>100 106>100 106\u226195+11 #-}\n{-# ATP prove 94+11>100 105>100 105\u226194+11 #-}\n{-# ATP prove 93+11>100 104>100 104\u226193+11 #-}\n{-# ATP prove 92+11>100 103>100 103\u226192+1 #-}\n{-# ATP prove 91+11>100 102>100 102\u226191+11 #-}\n{-# ATP prove 90+11>100 101>100' 101\u226190+11\u2261101 #-}\n\npostulate\n 100<102' : LT one-hundred hundred-two\n 100<102 : LT one-hundred (ninety-one + eleven)\n{-# ATP prove 100<102' #-}\n{-# ATP prove 100<102 100<102' 102\u226191+11 #-}\n\nx+11\u223810\u2261Sx : \u2200 {n} \u2192 N n \u2192 (n + eleven) \u2238 ten \u2261 succ n\nx+11\u223810\u2261Sx Nn = [x+Sy]\u2238y\u2261Sx Nn N10\n\npostulate 91>100\u2192\u22a5 : GT ninety-one one-hundred \u2192 \u22a5\n{-# ATP prove 91>100\u2192\u22a5 #-}\n\npostulate x+1\u2264x\u223810+11 : \u2200 {n} \u2192 N n \u2192 LE (n + one) ((n \u2238 ten) + eleven)\n{-# ATP prove x+1\u2264x\u223810+11 x\u2264y+x\u2238y N10 1+x-N 11+x-N x\u223810-N +-comm #-}\n\npostulate x\u226489\u2192x+11>100\u2192\u22a5 : \u2200 {n} \u2192 N n \u2192 LE n eighty-nine \u2192\n GT (n + eleven) one-hundred \u2192 \u22a5\n{-# ATP prove x\u226489\u2192x+11>100\u2192\u22a5 x>y\u2192x\u2264y\u2192\u22a5 x\u2264y\u2192x+k\u2264y+k x+11-N N89 N100 100\u226189+11 #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties used by the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.ArithmeticATP where\n\nopen import LTC.Base\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\n\n------------------------------------------------------------------------------\n\npostulate\n 1+x-N : \u2200 {n} \u2192 N n \u2192 N (one + n)\n 11+x-N : \u2200 {n} \u2192 N n \u2192 N (eleven + n)\n x\u223810-N : \u2200 {n} \u2192 N n \u2192 N (n \u2238 ten)\n x+11-N : \u2200 {n} \u2192 N n \u2192 N (n + eleven)\n{-# ATP prove 1+x-N #-}\n{-# ATP prove 11+x-N #-}\n{-# ATP prove x\u223810-N \u2238-N N10 #-}\n{-# ATP prove x+11-N 11+x-N +-comm #-}\n\npostulate\n 91\u2261[100+11\u223810]\u223810 : (one-hundred + eleven \u2238 ten) \u2238 ten \u2261 ninety-one\n{-# ATP prove 91\u2261[100+11\u223810]\u223810 #-}\n\npostulate\n 100\u226189+11 : eighty-nine + eleven \u2261 one-hundred\n 101\u226190+11\u2261101 : ninety + eleven \u2261 hundred-one\n 101\u2261100+11-10 : (one-hundred + eleven) \u2238 ten \u2261 hundred-one\n 102\u226191+11 : ninety-one + eleven \u2261 hundred-two\n 103\u226192+1 : ninety-two + eleven \u2261 hundred-three\n 104\u226193+11 : ninety-three + eleven \u2261 hundred-four\n 105\u226194+11 : ninety-four + eleven \u2261 hundred-five\n 106\u226195+11 : ninety-five + eleven \u2261 hundred-six\n 107\u226196+11 : ninety-six + eleven \u2261 hundred-seven\n 108\u226197+11 : ninety-seven + eleven \u2261 hundred-eight\n 109\u226199+11 : ninety-eight + eleven \u2261 hundred-nine\n 110\u226199+11 : ninety-nine + eleven \u2261 hundred-ten\n 111\u2261100+11 : one-hundred + eleven \u2261 hundred-eleven\n\n{-# ATP prove 100\u226189+11 #-}\n{-# ATP prove 101\u226190+11\u2261101 #-}\n{-# ATP prove 101\u2261100+11-10 #-}\n{-# ATP prove 102\u226191+11 #-}\n{-# ATP prove 103\u226192+1 #-}\n{-# ATP prove 104\u226193+11 #-}\n{-# ATP prove 105\u226194+11 #-}\n{-# ATP prove 106\u226195+11 #-}\n{-# ATP prove 107\u226196+11 #-}\n{-# ATP prove 108\u226197+11 #-}\n{-# ATP prove 109\u226199+11 #-}\n{-# ATP prove 110\u226199+11 #-}\n{-# ATP prove 111\u2261100+11 #-}\n\npostulate\n 101>100' : GT hundred-one one-hundred\n 101>100 : GT ((one-hundred + eleven) \u2238 ten) one-hundred\n 102>100 : GT hundred-two one-hundred\n 103>100 : GT hundred-three one-hundred\n 104>100 : GT hundred-four one-hundred\n 105>100 : GT hundred-five one-hundred\n 106>100 : GT hundred-six one-hundred\n 107>100 : GT hundred-seven one-hundred\n 108>100 : GT hundred-eight one-hundred\n 109>100 : GT hundred-nine one-hundred\n 110>100 : GT hundred-ten one-hundred\n 111>100' : GT hundred-eleven one-hundred\n 111>100 : GT (one-hundred + eleven) one-hundred\n{-# ATP prove 101>100' #-}\n{-# ATP prove 101>100 101>100' 101\u2261100+11-10 #-}\n{-# ATP prove 102>100 #-}\n{-# ATP prove 103>100 #-}\n{-# ATP prove 104>100 #-}\n{-# ATP prove 105>100 #-}\n{-# ATP prove 106>100 #-}\n{-# ATP prove 107>100 #-}\n{-# ATP prove 108>100 #-}\n{-# ATP prove 109>100 #-}\n{-# ATP prove 110>100 #-}\n{-# ATP prove 111>100' #-}\n{-# ATP prove 111>100 111>100' 111\u2261100+11 #-}\n\npostulate\n 99+11>100 : GT (ninety-nine + eleven) one-hundred\n 98+11>100 : GT (ninety-eight + eleven) one-hundred\n 97+11>100 : GT (ninety-seven + eleven) one-hundred\n 96+11>100 : GT (ninety-six + eleven) one-hundred\n 95+11>100 : GT (ninety-five + eleven) one-hundred\n 94+11>100 : GT (ninety-four + eleven) one-hundred\n 93+11>100 : GT (ninety-three + eleven) one-hundred\n 92+11>100 : GT (ninety-two + eleven) one-hundred\n 91+11>100 : GT (ninety-one + eleven) one-hundred\n 90+11>100 : GT (ninety + eleven) one-hundred\n{-# ATP prove 99+11>100 110>100 110\u226199+11 #-}\n{-# ATP prove 98+11>100 109>100 109\u226199+11 #-}\n{-# ATP prove 97+11>100 108>100 108\u226197+11 #-}\n{-# ATP prove 96+11>100 107>100 107\u226196+11 #-}\n{-# ATP prove 95+11>100 106>100 106\u226195+11 #-}\n{-# ATP prove 94+11>100 105>100 105\u226194+11 #-}\n{-# ATP prove 93+11>100 104>100 104\u226193+11 #-}\n{-# ATP prove 92+11>100 103>100 103\u226192+1 #-}\n{-# ATP prove 91+11>100 102>100 102\u226191+11 #-}\n{-# ATP prove 90+11>100 101>100' 101\u226190+11\u2261101 #-}\n\npostulate\n 100<102' : LT one-hundred hundred-two\n 100<102 : LT one-hundred (ninety-one + eleven)\n{-# ATP prove 100<102' #-}\n{-# ATP prove 100<102 100<102' 102\u226191+11 #-}\n\npostulate 91>100\u2192\u22a5 : GT ninety-one one-hundred \u2192 \u22a5\n{-# ATP prove 91>100\u2192\u22a5 #-}\n\npostulate x+1\u2264x\u223810+11 : \u2200 {n} \u2192 N n \u2192 LE (n + one) ((n \u2238 ten) + eleven)\n{-# ATP prove x+1\u2264x\u223810+11 x\u2264y+x\u2238y N10 1+x-N 11+x-N x\u223810-N +-comm #-}\n\npostulate x\u226489\u2192x+11>100\u2192\u22a5 : \u2200 {n} \u2192 N n \u2192 LE n eighty-nine \u2192\n GT (n + eleven) one-hundred \u2192 \u22a5\n{-# ATP prove x\u226489\u2192x+11>100\u2192\u22a5 x>y\u2192x\u2264y\u2192\u22a5 x\u2264y\u2192x+k\u2264y+k x+11-N N89 N100 100\u226189+11 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"82b8cfb3b9f32510182670a10fd20292bf25b946","subject":"More flat-funs combinators","message":"More flat-funs combinators\n","repos":"crypto-agda\/crypto-agda","old_file":"flat-funs.agda","new_file":"flat-funs.agda","new_contents":"module flat-funs where\n\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2^_)\nopen import Data.Bool using (if_then_else_)\nopen import Function using (_\u2218\u2032_)\nimport Data.Vec.NP as V\nimport Level as L\nopen V using (Vec; []; _\u2237_)\n\nopen import Data.Bits using (Bit; Bits)\n\nimport bintree as Tree\nopen Tree using (Tree)\nopen import data-universe\n\nrecord FlatFuns {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n universe : Universe T\n _`\u2192_ : T \u2192 T \u2192 Set\n infix 0 _`\u2192_\n open Universe universe public\n\nmodule Defaults {t} {T : Set t} (\u266dFuns : FlatFuns T) where\n open FlatFuns \u266dFuns\n\n module CompositionNotations\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)) where\n\n infixr 1 _\u204f_\n infixr 1 _>>>_\n\n _\u204f_ : \u2200 {a b c} \u2192 (a `\u2192 b) \u2192 (b `\u2192 c) \u2192 (a `\u2192 c)\n f \u204f g = g \u2218 f\n\n _>>>_ : \u2200 {a b c} \u2192 (a `\u2192 b) \u2192 (b `\u2192 c) \u2192 (a `\u2192 c)\n f >>> g = f \u204f g\n\n module DefaultsFirstSecond\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (<_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)) where\n\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n first f = < f \u00d7 id >\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = < id \u00d7 f >\n\n module DefaultsGroup2\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4)\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = < f \u2218 fst , g \u2218 snd >\n\n open DefaultsFirstSecond id <_\u00d7_> public\n\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n dup = < id , id >\n\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n swap = < snd , fst >\n\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc = < fst \u2218 fst , first snd >\n\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n = < tt , id >\n\n snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A\n snd = snd\n\n module DefaultsGroup1\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4)\n (snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A)\n (dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A)\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)) where\n\n open CompositionNotations _\u2218_\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = swap \u204f first f \u204f swap\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = first f \u204f second g\n\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f , g > = dup \u204f < f \u00d7 g >\n\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n snd = first tt \u204f snd\n\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n fst = swap \u204f snd\n\n module <\u00d7>Default\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n where\n open CompositionNotations _\u2218_\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = first f \u204f swap \u204f first g \u204f swap\n\n module DefaultAssoc\u2032\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C)))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n where\n open CompositionNotations _\u2218_\n\n -- This definition would cost 1 unit of space instead of 0.\n -- assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n -- assoc\u2032 = < second fst , snd \u204f snd >\n\n assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n assoc\u2032 = swap \u204f first swap \u204f assoc \u204f swap \u204f first swap\n\n module DefaultCond\n (fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n cond = fork fst snd\n\n -- This definition cost 2 units of space instead of 1.\n module DefaultFork\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C))\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A) where\n\n open CompositionNotations _\u2218_\n\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork f g = second < f , g > \u204f cond\n\nrecord FlatFunsOps {t} {T : Set t} (\u266dFuns : FlatFuns T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n\n infixr 9 _\u2218_\n infixr 3 _***_\n infixr 3 _&&&_\n field\n -- Functions\n id : \u2200 {A} \u2192 A `\u2192 A\n _\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)\n\n -- Bit\n <0b> <1b> : \u2200 {_A} \u2192 _A `\u2192 `Bit\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n\n -- Unit\n tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4\n\n -- Products (group 1 primitive functions or derived from group 2)\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n\n -- Products (group 2 primitive functions or derived from group 1)\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A\n\n -- Products (derived from group 1 or 2)\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n\n -- Vectors\n nil : \u2200 {_A B} \u2192 _A `\u2192 `Vec B 0\n cons : \u2200 {n A} \u2192 (A `\u00d7 `Vec A n) `\u2192 `Vec A (1 + n)\n uncons : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 (A `\u00d7 `Vec A n)\n\n open Defaults \u266dFuns\n open CompositionNotations _\u2218_ public\n\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n f &&& g = < f , g >\n\n _***_ : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n f *** g = < f \u00d7 g >\n\n constBit : \u2200 {_A} \u2192 Bit \u2192 _A `\u2192 `Bit\n constBit b = if b then <1b> else <0b>\n\n : \u2200 {A B C} \u2192 (`\u22a4 `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n = \u204f < f \u00d7 g >\n\n <_,tt\u204f_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (`\u22a4 `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f ,tt\u204f g > = \u204f swap\n\n open DefaultAssoc\u2032 _\u2218_ assoc swap first public\n\n : \u2200 {A B} (b : A `\u2192 `Bit) (f g : A `\u2192 B) \u2192 A `\u2192 B\n = < b , id > \u204f fork if-0 if-1\n\n -- Like first, but applies on a triple associated the other way\n assoc-first : \u2200 {A B C D E} \u2192 (A `\u00d7 B `\u2192 D `\u00d7 E) \u2192 A `\u00d7 B `\u00d7 C `\u2192 D `\u00d7 E `\u00d7 C\n assoc-first f = assoc\u2032 \u204f first f \u204f assoc\n\n -- Like assoc-first but for second\n assoc-second : \u2200 {A B C D E} \u2192 (B `\u00d7 C `\u2192 E `\u00d7 D) \u2192 (A `\u00d7 B) `\u00d7 C `\u2192 (A `\u00d7 E) `\u00d7 D\n assoc-second f = assoc \u204f second f \u204f assoc\u2032\n\n <_`zip`_> : \u2200 {A B C D E F} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 ((D `\u00d7 E) `\u2192 F)\n \u2192 ((A `\u00d7 D) `\u00d7 (B `\u00d7 E)) `\u2192 (C `\u00d7 F)\n < f `zip` g > = assoc-first (assoc-second swap) \u204f < f \u00d7 g >\n\n{- This one use one unit of space\n < f `zip` g > = < < fst \u00d7 fst > \u204f f ,\n < snd \u00d7 snd > \u204f g >\n-}\n\n head : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n head = uncons \u204f fst\n\n tail : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n tail = uncons \u204f snd\n\n <_\u2237_> : \u2200 {m n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A m `\u2192 `Vec B n)\n \u2192 `Vec A (1 + m) `\u2192 `Vec B (1 + n)\n < f \u2237 g > = uncons \u204f < f \u00d7 g > \u204f cons\n\n <_\u2237\u2032_> : \u2200 {n A B C} \u2192 (A `\u2192 C) \u2192 (B `\u2192 `Vec C n)\n \u2192 A `\u00d7 B `\u2192 `Vec C (1 + n)\n < f \u2237\u2032 g > = < f \u00d7 g > \u204f cons\n\n -- Notice that this one costs 1 unit of space.\n dup\u204f<_\u2237\u2032_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n dup\u204f< f \u2237\u2032 g > = dup \u204f < f \u2237\u2032 g >\n\n : \u2200 {n A B} \u2192 (`\u22a4 `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n = \u204f cons\n\n <_\u2237\u2032tt\u204f_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`\u22a4 `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n < f \u2237\u2032tt\u204f g > = < f ,tt\u204f g > \u204f cons\n\n <_\u2237nil> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Vec B 1\n < f \u2237nil> = < f \u2237\u2032tt\u204f nil >\n\n <0,_> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Bit `\u00d7 B\n <0, f > = , f >\n\n <1,_> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Bit `\u00d7 B\n <1, f > = , f >\n\n <0,> : \u2200 {A} \u2192 A `\u2192 `Bit `\u00d7 A\n <0,> = <0, id >\n\n <1,> : \u2200 {A} \u2192 A `\u2192 `Bit `\u00d7 A\n <1,> = <1, id >\n\n <0\u2237_> : \u2200 {n A} \u2192 (A `\u2192 `Bits n) \u2192 A `\u2192 `Bits (1 + n)\n <0\u2237 f > = \u2237\u2032 f >\n\n <1\u2237_> : \u2200 {n A} \u2192 (A `\u2192 `Bits n) \u2192 A `\u2192 `Bits (1 + n)\n <1\u2237 f > = \u2237\u2032 f >\n\n <0\u2237> : \u2200 {n} \u2192 `Bits n `\u2192 `Bits (1 + n)\n <0\u2237> = <0\u2237 id >\n\n <1\u2237> : \u2200 {n} \u2192 `Bits n `\u2192 `Bits (1 + n)\n <1\u2237> = <1\u2237 id >\n\n constVec : \u2200 {n b _A} {B : Set b} {C} \u2192 (B \u2192 `\u22a4 `\u2192 C) \u2192 Vec B n \u2192 _A `\u2192 `Vec C n\n constVec f [] = nil\n constVec f (x \u2237 xs) = \n\n constBits : \u2200 {n _A} \u2192 Bits n \u2192 _A `\u2192 `Bits n\n constBits = constVec constBit\n\n foldl : \u2200 {n A B} \u2192 (B `\u00d7 A `\u2192 B) \u2192 (B `\u00d7 `Vec A n) `\u2192 B\n foldl {zero} f = fst\n foldl {suc n} f = second uncons\n \u204f assoc\u2032\n \u204f first f\n \u204f foldl f\n\n foldl\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldl\u2081 f = uncons \u204f foldl f\n\n foldr : \u2200 {n A B} \u2192 (A `\u00d7 B `\u2192 B) \u2192 (`Vec A n `\u00d7 B) `\u2192 B\n foldr {zero} f = snd\n foldr {suc n} f = first uncons\n \u204f assoc\n \u204f second (foldr f)\n \u204f f\n\n foldr\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldr\u2081 f = uncons \u204f swap \u204f foldr f\n\n <_\u229b> : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 `Vec A n `\u2192 `Vec B n\n <_\u229b> [] = nil\n <_\u229b> (f \u2237 fs) = < f \u2237 < fs \u229b> >\n\n map : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A n `\u2192 `Vec B n)\n map f = < V.replicate f \u229b>\n\n zipWith : \u2200 {n A B C} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec C n\n zipWith {zero} f = nil\n zipWith {suc n} f = < uncons \u00d7 uncons >\n \u204f < f `zip` (zipWith f) >\n \u204f cons\n\n zip : \u2200 {n A B} \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec (A `\u00d7 B) n\n zip = zipWith id\n\n : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Vec C 0 `\u00d7 B\n = \n\n <_,nil> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 A `\u2192 B `\u00d7 `Vec C 0\n < f ,nil> = < f ,tt\u204f nil >\n\n snoc : \u2200 {n A} \u2192 (`Vec A n `\u00d7 A) `\u2192 `Vec A (1 + n)\n snoc {zero} = < snd \u2237nil>\n snoc {suc n} = first uncons \u204f assoc \u204f second snoc \u204f cons\n\n reverse : \u2200 {n A} \u2192 `Vec A n `\u2192 `Vec A n\n reverse {zero} = nil\n reverse {suc n} = uncons \u204f swap \u204f first reverse \u204f snoc\n\n append : \u2200 {m n A} \u2192 (`Vec A m `\u00d7 `Vec A n) `\u2192 `Vec A (m + n)\n append {zero} = snd\n append {suc m} = first uncons\n \u204f assoc\n \u204f second append\n \u204f cons\n\n splitAt : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 (`Vec A m `\u00d7 `Vec A n)\n splitAt zero = \n splitAt (suc m) = uncons\n \u204f second (splitAt m)\n \u204f assoc\u2032\n \u204f first cons\n\n take : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A m\n take zero = nil\n take (suc m) = < id \u2237 take m >\n\n drop : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A n\n drop zero = id\n drop (suc m) = tail \u204f drop m\n\n folda : \u2200 n {A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (2^ n) `\u2192 A\n folda zero f = head\n folda (suc n) f = splitAt (2^ n) \u204f < folda n f \u00d7 folda n f > \u204f f\n\n init : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n init {zero} = nil\n init {suc n} = < id \u2237 init >\n\n last : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n last {zero} = head\n last {suc n} = tail \u204f last\n\n concat : \u2200 {m n A} \u2192 `Vec (`Vec A m) n `\u2192 `Vec A (n * m)\n concat {n = zero} = nil\n concat {n = suc n} = uncons \u204f second concat \u204f append\n\n group : \u2200 {A} n k \u2192 `Vec A (n * k) `\u2192 `Vec (`Vec A k) n\n group zero k = nil\n group (suc n) k = splitAt k \u204f second (group n k) \u204f cons\n\n bind : \u2200 {m n A B} \u2192 (A `\u2192 `Vec B m) \u2192 `Vec A n `\u2192 `Vec B (n * m)\n bind f = map f \u204f concat\n\n replicate : \u2200 {n A} \u2192 A `\u2192 `Vec A n\n replicate {zero} = nil\n replicate {suc n} = < id , replicate > \u204f cons\n\n replicate\u22a4 : \u2200 {n _A} \u2192 _A `\u2192 `Vec `\u22a4 n\n replicate\u22a4 {zero} = nil\n replicate\u22a4 {suc n} = \n\n loop : \u2200 {A} n \u2192 (A `\u2192 A) \u2192 (A `\u2192 A)\n loop n f = < id ,tt\u204f replicate\u22a4 {n} > \u204f foldl (fst \u204f f)\n\n `Maybe : T \u2192 T\n `Maybe A = `Bit `\u00d7 A\n\n : \u2200 {A} \u2192 A `\u2192 `Maybe A\n = <0,>\n\n : \u2200 {A} \u2192 A `\u2192 `Maybe A\n = <1,>\n\n : \u2200 {A B C} \u2192 (f : A `\u00d7 B `\u2192 C) (g : B `\u2192 C) \u2192 `Maybe A `\u00d7 B `\u2192 C\n = \n\n _\u2223?_ : \u2200 {A} \u2192 `Maybe A `\u00d7 `Maybe A `\u2192 `Maybe A\n _\u2223?_ = \u2236 id >\n\n _`\u2192?_ : T \u2192 T \u2192 Set\n A `\u2192? B = A `\u2192 `Maybe B\n\n search : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 (`Bits n `\u2192 A) \u2192 `\u22a4 `\u2192 A\n search {zero} _ f = nil \u204f f\n search {suc n} op f = ) , search op (f \u2218 <1\u2237>) > \u204f op\n\n find? : \u2200 {n A} \u2192 (`Bits n `\u2192? A) \u2192 `\u22a4 `\u2192? A\n find? = search _\u2223?_\n\n findB : \u2200 {n} \u2192 (`Bits n `\u2192 `Bit) \u2192 `\u22a4 `\u2192? `Bits n\n findB pred = find? else >\n\n fromTree : \u2200 {n A} \u2192 Tree (`\u22a4 `\u2192 A) n \u2192 `Bits n `\u2192 A\n fromTree (Tree.leaf x) = tt \u204f x\n fromTree (Tree.fork t\u2080 t\u2081) = uncons \u204f fork (fromTree t\u2080) (fromTree t\u2081)\n\n fromFun : \u2200 {n A} \u2192 (Bits n \u2192 `\u22a4 `\u2192 A) \u2192 `Bits n `\u2192 A\n fromFun = fromTree \u2218\u2032 Tree.fromFun\n\n fromBitsFun : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 `Bits i `\u2192 `Bits o\n fromBitsFun f = fromFun (constBits \u2218\u2032 f)\n\n module WithFin\n (`Fin : \u2115 \u2192 T)\n (fz : \u2200 {n _A} \u2192 _A `\u2192 `Fin (suc n))\n (fs : \u2200 {n} \u2192 `Fin n `\u2192 `Fin (suc n))\n (elim-Fin0 : \u2200 {A} \u2192 `Fin 0 `\u2192 A)\n (elim-Fin1+ : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Fin n `\u00d7 A `\u2192 B) \u2192 `Fin (suc n) `\u00d7 A `\u2192 B) where\n\n tabulate : \u2200 {n A _B} \u2192 (`Fin n `\u2192 A) \u2192 _B `\u2192 `Vec A n\n tabulate {zero} f = nil\n tabulate {suc n} f = \n\n lookup : \u2200 {n A} \u2192 `Fin n `\u00d7 `Vec A n `\u2192 A\n lookup {zero} = fst \u204f elim-Fin0\n lookup {suc n} = elim-Fin1+ head (second tail \u204f lookup)\n\n allFin : \u2200 {n _A} \u2192 _A `\u2192 `Vec (`Fin n) n\n allFin = tabulate id\n","old_contents":"module flat-funs where\n\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2^_)\nopen import Data.Bool using (if_then_else_)\nopen import Function using (_\u2218\u2032_)\nimport Data.Vec.NP as V\nimport Level as L\nopen V using (Vec; []; _\u2237_)\n\nopen import Data.Bits using (Bit; Bits)\n\nimport bintree as Tree\nopen Tree using (Tree)\nopen import data-universe\n\nrecord FlatFuns {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n universe : Universe T\n _`\u2192_ : T \u2192 T \u2192 Set\n infix 0 _`\u2192_\n open Universe universe public\n\nmodule Defaults {t} {T : Set t} (\u266dFuns : FlatFuns T) where\n open FlatFuns \u266dFuns\n\n module CompositionNotations\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)) where\n\n infixr 1 _\u204f_\n infixr 1 _>>>_\n\n _\u204f_ : \u2200 {a b c} \u2192 (a `\u2192 b) \u2192 (b `\u2192 c) \u2192 (a `\u2192 c)\n f \u204f g = g \u2218 f\n\n _>>>_ : \u2200 {a b c} \u2192 (a `\u2192 b) \u2192 (b `\u2192 c) \u2192 (a `\u2192 c)\n f >>> g = f \u204f g\n\n module DefaultsFirstSecond\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (<_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)) where\n\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n first f = < f \u00d7 id >\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = < id \u00d7 f >\n\n module DefaultsGroup2\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4)\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = < f \u2218 fst , g \u2218 snd >\n\n open DefaultsFirstSecond id <_\u00d7_> public\n\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n dup = < id , id >\n\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n swap = < snd , fst >\n\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc = < fst \u2218 fst , first snd >\n\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n = < tt , id >\n\n snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A\n snd = snd\n\n module DefaultsGroup1\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4)\n (snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A)\n (dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A)\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)) where\n\n open CompositionNotations _\u2218_\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = swap \u204f first f \u204f swap\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = first f \u204f second g\n\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f , g > = dup \u204f < f \u00d7 g >\n\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n snd = first tt \u204f snd\n\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n fst = swap \u204f snd\n\n module <\u00d7>Default\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n where\n open CompositionNotations _\u2218_\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = first f \u204f swap \u204f first g \u204f swap\n\n module DefaultAssoc\u2032\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C)))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n where\n open CompositionNotations _\u2218_\n\n -- This definition would cost 1 unit of space instead of 0.\n -- assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n -- assoc\u2032 = < second fst , snd \u204f snd >\n\n assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n assoc\u2032 = swap \u204f first swap \u204f assoc \u204f swap \u204f first swap\n\n module DefaultCond\n (fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n cond = fork fst snd\n\n -- This definition cost 2 units of space instead of 1.\n module DefaultFork\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C))\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A) where\n\n open CompositionNotations _\u2218_\n\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork f g = second < f , g > \u204f cond\n\nrecord FlatFunsOps {t} {T : Set t} (\u266dFuns : FlatFuns T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n\n infixr 9 _\u2218_\n infixr 3 _***_\n infixr 3 _&&&_\n field\n -- Functions\n id : \u2200 {A} \u2192 A `\u2192 A\n _\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)\n\n -- Bit\n <0b> <1b> : \u2200 {_A} \u2192 _A `\u2192 `Bit\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n\n -- Unit\n tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4\n\n -- Products (group 1 primitive functions or derived from group 2)\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n\n -- Products (group 2 primitive functions or derived from group 1)\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A\n\n -- Products (derived from group 1 or 2)\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n\n -- Vectors\n nil : \u2200 {_A B} \u2192 _A `\u2192 `Vec B 0\n cons : \u2200 {n A} \u2192 (A `\u00d7 `Vec A n) `\u2192 `Vec A (1 + n)\n uncons : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 (A `\u00d7 `Vec A n)\n\n open Defaults \u266dFuns\n open CompositionNotations _\u2218_ public\n\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n f &&& g = < f , g >\n\n _***_ : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n f *** g = < f \u00d7 g >\n\n constBit : \u2200 {_A} \u2192 Bit \u2192 _A `\u2192 `Bit\n constBit b = if b then <1b> else <0b>\n\n : \u2200 {A B C} \u2192 (`\u22a4 `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n = \u204f < f \u00d7 g >\n\n <_,tt\u204f_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (`\u22a4 `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f ,tt\u204f g > = \u204f swap\n\n open DefaultAssoc\u2032 _\u2218_ assoc swap first public\n\n : \u2200 {A B} (b : A `\u2192 `Bit) (f g : A `\u2192 B) \u2192 A `\u2192 B\n = < b , id > \u204f fork if-0 if-1\n\n assoc-first : \u2200 {A B C D E} \u2192 (A `\u00d7 B `\u2192 D `\u00d7 E) \u2192 A `\u00d7 B `\u00d7 C `\u2192 D `\u00d7 E `\u00d7 C\n assoc-first f = assoc\u2032 \u204f first f \u204f assoc\n\n assoc-second : \u2200 {A B C D E} \u2192 (B `\u00d7 C `\u2192 E `\u00d7 D) \u2192 (A `\u00d7 B) `\u00d7 C `\u2192 (A `\u00d7 E) `\u00d7 D\n assoc-second f = assoc \u204f second f \u204f assoc\u2032\n\n <_`zip`_> : \u2200 {A B C D E F} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 ((D `\u00d7 E) `\u2192 F)\n \u2192 ((A `\u00d7 D) `\u00d7 (B `\u00d7 E)) `\u2192 (C `\u00d7 F)\n < f `zip` g > = assoc-first (assoc-second swap) \u204f < f \u00d7 g >\n\n{- This one use one unit of space\n < f `zip` g > = < < fst \u00d7 fst > \u204f f ,\n < snd \u00d7 snd > \u204f g >\n-}\n\n head : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n head = uncons \u204f fst\n\n tail : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n tail = uncons \u204f snd\n\n <_\u2237_> : \u2200 {m n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A m `\u2192 `Vec B n)\n \u2192 `Vec A (1 + m) `\u2192 `Vec B (1 + n)\n < f \u2237 g > = uncons \u204f < f \u00d7 g > \u204f cons\n\n <_\u2237\u2032_> : \u2200 {n A B C} \u2192 (A `\u2192 C) \u2192 (B `\u2192 `Vec C n)\n \u2192 A `\u00d7 B `\u2192 `Vec C (1 + n)\n < f \u2237\u2032 g > = < f \u00d7 g > \u204f cons\n\n -- Notice that this one costs 1 unit of space.\n dup\u204f<_\u2237\u2032_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n dup\u204f< f \u2237\u2032 g > = dup \u204f < f \u2237\u2032 g >\n\n : \u2200 {n A B} \u2192 (`\u22a4 `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n = \u204f cons\n\n <_\u2237\u2032tt\u204f_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`\u22a4 `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n < f \u2237\u2032tt\u204f g > = < f ,tt\u204f g > \u204f cons\n\n <0,_> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Bit `\u00d7 B\n <0, f > = , f >\n\n <1,_> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Bit `\u00d7 B\n <1, f > = , f >\n\n <0,> : \u2200 {A} \u2192 A `\u2192 `Bit `\u00d7 A\n <0,> = <0, id >\n\n <1,> : \u2200 {A} \u2192 A `\u2192 `Bit `\u00d7 A\n <1,> = <1, id >\n\n <0\u2237_> : \u2200 {n A} \u2192 (A `\u2192 `Bits n) \u2192 A `\u2192 `Bits (1 + n)\n <0\u2237 f > = \u2237\u2032 f >\n\n <1\u2237_> : \u2200 {n A} \u2192 (A `\u2192 `Bits n) \u2192 A `\u2192 `Bits (1 + n)\n <1\u2237 f > = \u2237\u2032 f >\n\n <0\u2237> : \u2200 {n} \u2192 `Bits n `\u2192 `Bits (1 + n)\n <0\u2237> = <0\u2237 id >\n\n <1\u2237> : \u2200 {n} \u2192 `Bits n `\u2192 `Bits (1 + n)\n <1\u2237> = <1\u2237 id >\n\n constVec : \u2200 {n b _A} {B : Set b} {C} \u2192 (B \u2192 `\u22a4 `\u2192 C) \u2192 Vec B n \u2192 _A `\u2192 `Vec C n\n constVec f [] = nil\n constVec f (x \u2237 xs) = \n\n constBits : \u2200 {n _A} \u2192 Bits n \u2192 _A `\u2192 `Bits n\n constBits = constVec constBit\n\n foldl : \u2200 {n A B} \u2192 (B `\u00d7 A `\u2192 B) \u2192 (B `\u00d7 `Vec A n) `\u2192 B\n foldl {zero} f = fst\n foldl {suc n} f = second uncons\n \u204f assoc\u2032\n \u204f first f\n \u204f foldl f\n\n foldl\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldl\u2081 f = uncons \u204f foldl f\n\n foldr : \u2200 {n A B} \u2192 (A `\u00d7 B `\u2192 B) \u2192 (`Vec A n `\u00d7 B) `\u2192 B\n foldr {zero} f = snd\n foldr {suc n} f = first uncons\n \u204f assoc\n \u204f second (foldr f)\n \u204f f\n\n foldr\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldr\u2081 f = uncons \u204f swap \u204f foldr f\n\n <_\u229b> : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 `Vec A n `\u2192 `Vec B n\n <_\u229b> [] = nil\n <_\u229b> (f \u2237 fs) = < f \u2237 < fs \u229b> >\n\n map : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A n `\u2192 `Vec B n)\n map f = < V.replicate f \u229b>\n\n zipWith : \u2200 {n A B C} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec C n\n zipWith {zero} f = nil\n zipWith {suc n} f = < uncons \u00d7 uncons >\n \u204f < f `zip` (zipWith f) >\n \u204f cons\n\n zip : \u2200 {n A B} \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec (A `\u00d7 B) n\n zip = zipWith id\n\n : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Vec C 0 `\u00d7 B\n = \n\n <_,nil> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 A `\u2192 B `\u00d7 `Vec C 0\n < f ,nil> = < f ,tt\u204f nil >\n\n singleton : \u2200 {A} \u2192 A `\u2192 `Vec A 1\n singleton = < id ,nil> \u204f cons\n\n snoc : \u2200 {n A} \u2192 (`Vec A n `\u00d7 A) `\u2192 `Vec A (1 + n)\n snoc {zero} = snd \u204f singleton\n snoc {suc n} = first uncons \u204f assoc \u204f second snoc \u204f cons\n\n reverse : \u2200 {n A} \u2192 `Vec A n `\u2192 `Vec A n\n reverse {zero} = nil\n reverse {suc n} = uncons \u204f swap \u204f first reverse \u204f snoc\n\n append : \u2200 {m n A} \u2192 (`Vec A m `\u00d7 `Vec A n) `\u2192 `Vec A (m + n)\n append {zero} = snd\n append {suc m} = first uncons\n \u204f assoc\n \u204f second append\n \u204f cons\n\n splitAt : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 (`Vec A m `\u00d7 `Vec A n)\n splitAt zero = \n splitAt (suc m) = uncons\n \u204f second (splitAt m)\n \u204f assoc\u2032\n \u204f first cons\n\n take : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A m\n take zero = nil\n take (suc m) = < id \u2237 take m >\n\n drop : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A n\n drop zero = id\n drop (suc m) = tail \u204f drop m\n\n folda : \u2200 n {A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (2^ n) `\u2192 A\n folda zero f = head\n folda (suc n) f = splitAt (2^ n) \u204f < folda n f \u00d7 folda n f > \u204f f\n\n init : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n init {zero} = nil\n init {suc n} = < id \u2237 init >\n\n last : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n last {zero} = head\n last {suc n} = tail \u204f last\n\n concat : \u2200 {m n A} \u2192 `Vec (`Vec A m) n `\u2192 `Vec A (n * m)\n concat {n = zero} = nil\n concat {n = suc n} = uncons \u204f second concat \u204f append\n\n group : \u2200 {A} n k \u2192 `Vec A (n * k) `\u2192 `Vec (`Vec A k) n\n group zero k = nil\n group (suc n) k = splitAt k \u204f second (group n k) \u204f cons\n\n bind : \u2200 {m n A B} \u2192 (A `\u2192 `Vec B m) \u2192 `Vec A n `\u2192 `Vec B (n * m)\n bind f = map f \u204f concat\n\n replicate : \u2200 {n A} \u2192 A `\u2192 `Vec A n\n replicate {zero} = nil\n replicate {suc n} = < id , replicate > \u204f cons\n\n `Maybe : T \u2192 T\n `Maybe A = `Bit `\u00d7 A\n\n : \u2200 {A} \u2192 A `\u2192 `Maybe A\n = <0,>\n\n : \u2200 {A} \u2192 A `\u2192 `Maybe A\n = <1,>\n\n : \u2200 {A B C} \u2192 (f : A `\u00d7 B `\u2192 C) (g : B `\u2192 C) \u2192 `Maybe A `\u00d7 B `\u2192 C\n = \n\n _\u2223?_ : \u2200 {A} \u2192 `Maybe A `\u00d7 `Maybe A `\u2192 `Maybe A\n _\u2223?_ = \u2236 id >\n\n _`\u2192?_ : T \u2192 T \u2192 Set\n A `\u2192? B = A `\u2192 `Maybe B\n\n search : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 (`Bits n `\u2192 A) \u2192 `\u22a4 `\u2192 A\n search {zero} _ f = nil \u204f f\n search {suc n} op f = ) , search op (f \u2218 <1\u2237>) > \u204f op\n\n find? : \u2200 {n A} \u2192 (`Bits n `\u2192? A) \u2192 `\u22a4 `\u2192? A\n find? = search _\u2223?_\n\n findB : \u2200 {n} \u2192 (`Bits n `\u2192 `Bit) \u2192 `\u22a4 `\u2192? `Bits n\n findB pred = find? else >\n\n fromTree : \u2200 {n A} \u2192 Tree (`\u22a4 `\u2192 A) n \u2192 `Bits n `\u2192 A\n fromTree (Tree.leaf x) = tt \u204f x\n fromTree (Tree.fork t\u2080 t\u2081) = uncons \u204f fork (fromTree t\u2080) (fromTree t\u2081)\n\n fromFun : \u2200 {n A} \u2192 (Bits n \u2192 `\u22a4 `\u2192 A) \u2192 `Bits n `\u2192 A\n fromFun = fromTree \u2218\u2032 Tree.fromFun\n\n fromBitsFun : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 `Bits i `\u2192 `Bits o\n fromBitsFun f = fromFun (constBits \u2218\u2032 f)\n\n module WithFin\n (`Fin : \u2115 \u2192 T)\n (fz : \u2200 {n _A} \u2192 _A `\u2192 `Fin (suc n))\n (fs : \u2200 {n} \u2192 `Fin n `\u2192 `Fin (suc n))\n (elim-Fin0 : \u2200 {A} \u2192 `Fin 0 `\u2192 A)\n (elim-Fin1+ : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Fin n `\u00d7 A `\u2192 B) \u2192 `Fin (suc n) `\u00d7 A `\u2192 B) where\n\n tabulate : \u2200 {n A _B} \u2192 (`Fin n `\u2192 A) \u2192 _B `\u2192 `Vec A n\n tabulate {zero} f = nil\n tabulate {suc n} f = \u204f cons\n\n lookup : \u2200 {n A} \u2192 `Fin n `\u00d7 `Vec A n `\u2192 A\n lookup {zero} = fst \u204f elim-Fin0\n lookup {suc n} = elim-Fin1+ head (second tail \u204f lookup)\n\n allFin : \u2200 {n _A} \u2192 _A `\u2192 `Vec (`Fin n) n\n allFin = tabulate id\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"9ef129c5aad704a019f61d4c083b35985bf81762","subject":"Typo.","message":"Typo.\n\nIgnore-this: 931e5c7dc1533eed7aa34972dc910a62\n\ndarcs-hash:20110406162735-3bd4e-7baa4b3acc9f8747cd7dcdec04e615cf9c2fee2a.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Program\/McCarthy91\/MCR\/LT2MCR-ATP.agda","new_file":"src\/FOTC\/Program\/McCarthy91\/MCR\/LT2MCR-ATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Property LT2MCR which proves that the recursive calls of the\n-- McCarthy 91 function are on smaller arguments.\n------------------------------------------------------------------------------\n\nmodule FOTC.Program.McCarthy91.MCR.LT2MCR-ATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.IsN-ATP\n\nopen import FOTC.Program.McCarthy91.MCR\n\n------------------------------------------------------------------------------\n\nLT2MCR-helper : \u2200 {n m k} \u2192 N n \u2192 N m \u2192 N k \u2192\n LT m n \u2192 LT (succ n) k \u2192 LT (succ m) k \u2192\n LT (k \u2238 n) (k \u2238 m) \u2192 LT (k \u2238 succ n) (k \u2238 succ m)\nLT2MCR-helper zN Nm Nk p qn qm h = \u22a5-elim (x<0\u2192\u22a5 Nm p)\nLT2MCR-helper (sN Nn) Nm zN p qn qm h = \u22a5-elim (x<0\u2192\u22a5 (sN Nm) qm)\nLT2MCR-helper (sN {n} Nn) zN (sN {k} Nk) p qn qm h = prfS0S\n where\n postulate k\u2265Sn : GE k (succ n)\n k\u2238Sn0\u2192x\u2238y0\u2192x\u2238y Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {I : Set} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi (pi S T) = cong (pi S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n\n-- From embedding to embedding\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\\ _ -> descD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> descD I)) -> Set\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (descD I) (IDescl0 I))\n (hs : desc (box (descD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\\ s -> psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s -> psi (phi (T s)))\n T\n hs)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> desc (R i) T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = induction R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : desc D (IMu R))\n (ms : desc (box D (IMu R) xs) (\\it -> T (fst it))) -> \n desc D T\n replace (var i) T x y = y\n replace (const Z) T z z' = z'\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Meta-language\n--********************************\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n\n\n--********************************\n-- Typed expressions\n--********************************\n\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty),\n (prod (var bool) (prod (var ty) (var ty)), \n Void)))\n\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> desc (closeTerm ty) Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n\n--********************************\n-- Open terms\n--********************************\n\nVar : EnumU -> Type -> Set\nVar dom _ = EnumT dom\n\nopenTerm : EnumU -> Type -> IDesc Type\nopenTerm dom = exprIDesc (\\ty -> Val ty + Var dom ty) expr\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm ty\ndischarge ty vars context (l value) = con ( EZe , value )\ndischarge ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm ty) context variable \n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n desc (toIDesc I (R ** X) i) (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), \n Void))\n\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void)\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void )\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (exprFree ** Val)\n\nopenTerm' : EnumU -> Type -> IDesc Type\nopenTerm' dom = toIDesc Type (exprFree ** (\\ty -> Val ty + Var dom ty))\n\n--********************************\n-- Evaluation of open terms'\n--********************************\n\ndischarge' : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm' ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm' ty\ndischarge' ty vars context (l value) = con (EZe , value) \ndischarge' ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm' ty) context variable\n\nchooseDom : (ty : Type)(domNat domBool : EnumU) -> EnumU\nchooseDom nat domNat _ = domNat\nchooseDom bool _ domBool = domBool\n\nchooseGamma : (ty : Type)\n (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool)) ->\n spi dom (\\_ -> IMu closeTerm' ty)\nchooseGamma nat dom gammaNat gammaBool = gammaNat\nchooseGamma bool dom gammaNat gammaBool = gammaBool\n\nsubstExpr : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (sigma : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var dom ty) ->\n IMu closeTerm' ty)\n (ty : Type) ->\n IMu (openTerm' dom) ty ->\n IMu closeTerm' ty\nsubstExpr dom gammaNat gammaBool sig ty term = \n substI (\\ty -> Val ty + Var dom ty)\n Val\n exprFree\n (sig dom gammaNat gammaBool)\n ty\n term\n\n\nsigmaExpr : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var dom ty) ->\n IMu closeTerm' ty\nsigmaExpr dom gammaNat gammaBool ty v = \n discharge' ty\n dom\n (chooseGamma ty dom gammaNat gammaBool)\n v\n\ntest : IMu (openTerm' (consE (consE nilE))) nat ->\n IMu closeTerm' nat\ntest term = substExpr (consE (consE nilE)) \n ( con ( EZe , ze ) , ( con (EZe , su ze ) , Void )) \n ( con ( EZe , true ) , ( con ( EZe , false ) , Void ))\n sigmaExpr nat term\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {I : Set} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi (pi S T) = cong (pi S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n\n-- From embedding to embedding\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\\ _ -> descD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> descD I)) -> Set\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (descD I) (IDescl0 I))\n (hs : desc (box (descD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\\ s -> psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s -> psi (phi (T s)))\n T\n hs)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> desc (R i) T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = induction R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : desc D (IMu R))\n (ms : desc (box D (IMu R) xs) (\\it -> T (fst it))) -> \n desc D T\n replace (var i) T x y = y\n replace (const Z) T z z' = z'\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Meta-language\n--********************************\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n\n\n--********************************\n-- Typed expressions\n--********************************\n\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty),\n (prod (var bool) (prod (var ty) (var ty)), \n Void)))\n\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> desc (closeTerm ty) Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n\n--********************************\n-- Open terms\n--********************************\n\nVar : EnumU -> Type -> Set\nVar dom _ = EnumT dom\n\nopenTerm : EnumU -> Type -> IDesc Type\nopenTerm dom = exprIDesc (\\ty -> Val ty + Var dom ty) expr\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm ty\ndischarge ty vars context (l value) = con ( EZe , value )\ndischarge ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm ty) context variable \n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n desc (toIDesc I (R ** X) i) (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), \n Void))\n\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void)\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void )\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (exprFree ** Val)\n\nopenTerm' : EnumU -> Type -> IDesc Type\nopenTerm' dom = toIDesc Type (exprFree ** (\\ty -> Val ty + Var dom ty))\n\n--********************************\n-- Evaluation of open terms'\n--********************************\n\ndischarge' : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm' ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm' ty\ndischarge' ty vars context (l value) = con (EZe , value) \ndischarge' ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm' ty) context variable\n\nchooseDom : (ty : Type)(domNat domBool : EnumU) -> EnumU\nchooseDom nat domNat _ = domNat\nchooseDom bool _ domBool = domBool\n\nchooseGamma : (ty : Type)\n (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool)) ->\n spi dom (\\_ -> IMu closeTerm' ty)\nchooseGamma nat dom gammaNat gammaBool = gammaNat\nchooseGamma bool dom gammaNat gammaBool = gammaBool\n\nsubstExpr : (domNat domBool : EnumU)\n (gammaNat : spi domNat (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi domBool (\\_ -> IMu closeTerm' bool))\n (sigma : (domNat domBool : EnumU)\n (gammaNat : spi domNat (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi domBool (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var (chooseDom ty domNat domBool) ty) ->\n IMu closeTerm' ty)\n (ty : Type) ->\n IMu (toIDesc Type (exprFree ** (\\ty -> Val ty + Var (chooseDom ty domNat domBool) ty))) ty ->\n IMu closeTerm' ty\nsubstExpr domNat domBool gammaNat gammaBool sig ty term = \n substI (\\ty -> Val ty + Var (chooseDom ty domNat domBool) ty)\n Val\n exprFree\n (sig domNat domBool gammaNat gammaBool)\n ty\n term\n\n\n-- sigmaExpr : (domNat domBool : EnumU)\n-- (gammaNat : spi domNat (\\_ -> IMu closeTerm' nat))\n-- (gammaBool : spi domBool (\\_ -> IMu closeTerm' bool))\n-- (ty : Type) ->\n-- (Val ty + Var (chooseDom ty domNat domBool) ty) ->\n-- IMu closeTerm' ty\n-- sigmaExpr domNat domBool gammaNat gammaBool ty v = \n-- discharge' ty\n-- (chooseDom ty domNat domBool)\n-- (chooseGamma ty (chooseDom ty domNat domBool) gammaNat gammaBool)\n-- v\n\n-- discharge' : (ty : Type)\n-- (vars : EnumU)\n-- (context : spi vars (\\_ -> IMu closeTerm' ty)) ->\n-- (Val ty + Var vars ty) -> \n-- IMu closeTerm' ty\n\n-- chooseGamma : (ty : Type)\n-- (dom : EnumU)\n-- (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n-- (gammaBool : spi dom (\\_ -> IMu closeTerm' bool)) ->\n-- spi dom (\\_ -> IMu closeTerm' ty)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f274de0be6b9f871bb446ee6381969cac1a7ad8a","subject":"Fix order of returned caches","message":"Fix order of returned caches\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Denotation\/CachingEvaluation.agda","new_file":"Parametric\/Denotation\/CachingEvaluation.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Caching evaluation\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\n\nimport Parametric.Syntax.MType as MType\nimport Parametric.Syntax.MTerm as MTerm\n\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Denotation.MValue as MValue\nimport Parametric.Denotation.CachingMValue as CachingMValue\nimport Parametric.Denotation.MEvaluation as MEvaluation\n\nmodule Parametric.Denotation.CachingEvaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (ValConst : MTerm.ValConstStructure Const)\n (CompConst : MTerm.CompConstStructure Const)\n -- I should really switch to records - can it get sillier than this?\n (\u27e6_\u27e7ValBase : MEvaluation.ValStructure Const \u27e6_\u27e7Base ValConst CompConst)\n (\u27e6_\u27e7CompBase : MEvaluation.CompStructure Const \u27e6_\u27e7Base ValConst CompConst)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\n\nopen MType.Structure Base\nopen MTerm.Structure Const ValConst CompConst\n\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen MValue.Structure Base \u27e6_\u27e7Base\nopen CachingMValue.Structure Base \u27e6_\u27e7Base\nopen MEvaluation.Structure Const \u27e6_\u27e7Base ValConst CompConst \u27e6_\u27e7ValBase \u27e6_\u27e7CompBase\n\nopen import Base.Denotation.Notation\n\n-- Extension Point: Evaluation of fully applied constants.\nValStructure : Set\nValStructure = \u2200 {\u03a3 \u03c4} \u2192 ValConst \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n\nCompStructure : Set\nCompStructure = \u2200 {\u03a3 \u03c4} \u2192 CompConst \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n\nmodule Structure\n (\u27e6_\u27e7ValBaseTermCache : ValStructure)\n (\u27e6_\u27e7CompBaseTermCache : CompStructure)\n where\n\n {-\n -- Prototype here the type-correctness of a simple non-standard semantics.\n -- This describes a simplified version of the transformation by Liu and\n -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language\n -- terms, we produce host language terms to take advantage of the richer type\n -- system of the host language (in particular, here we need the unit type,\n -- product types and *existentials*).\n --\n -- As usual, we'll later switch to a real term transformation.\n -}\n open import Data.Product hiding (map)\n open import Data.Unit\n\n -- Defining a caching semantics for Term proves to be hard, requiring to\n -- insert and remove caches where we apply constants.\n\n -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.\n\n -- The solution is to distinguish among different kinds of constants. Some are\n -- value constructors (and thus do not return caches), while others are\n -- computation constructors (and thus should return caches). For products, I\n -- believe we will only use the products which are values, not computations\n -- (XXX check CBPV paper for the name).\n \u27e6_\u27e7CompTermCache : \u2200 {\u03c4 \u0393} \u2192 Comp \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n \u27e6_\u27e7ValTermCache : \u2200 {\u03c4 \u0393} \u2192 Val \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n \u27e6_\u27e7ValsTermCache : \u2200 {\u0393 \u03a3} \u2192 Vals \u0393 \u03a3 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache\n\n open import Base.Denotation.Environment ValType \u27e6_\u27e7ValTypeHidCache public\n using ()\n renaming (\u27e6_\u27e7Var to \u27e6_\u27e7ValVarHidCache)\n\n -- This says that the environment does not contain caches... sounds wrong!\n -- Either we add extra variables for the caches, or we store computations in\n -- the environment (but that does not make sense), or we store caches in\n -- values, by acting not on F but on something else (U?).\n\n -- Copy of \u27e6_\u27e7Vals\n \u27e6 \u2205 \u27e7ValsTermCache \u03c1 = \u2205\n \u27e6 vt \u2022 valtms \u27e7ValsTermCache \u03c1 = \u27e6 vt \u27e7ValTermCache \u03c1 \u2022 \u27e6 valtms \u27e7ValsTermCache \u03c1\n\n -- I suspect the plan was to use extra variables; that's annoying to model in\n -- Agda but easier in implementations.\n\n \u27e6 vVar x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7ValVarHidCache \u03c1\n \u27e6 vThunk x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7CompTermCache \u03c1\n -- No caching, because the arguments are values, so evaluating them does not\n -- produce intermediate results.\n \u27e6 vConst c args \u27e7ValTermCache \u03c1 = \u27e6 c \u27e7ValBaseTermCache (\u27e6 args \u27e7ValsTermCache \u03c1)\n\n -- The only caching is done by the interpretation of the constant (because the\n -- arguments are values so need no caching).\n \u27e6_\u27e7CompTermCache (cConst c args) \u03c1 = \u27e6 c \u27e7CompBaseTermCache (\u27e6 args \u27e7ValsTermCache \u03c1)\n\n -- Also, where are introduction forms for pairs and sums among values? With\n -- them, we should see that we can interpret them without adding a cache.\n\n -- Thunks keep seeming noops.\n \u27e6_\u27e7CompTermCache (cForce x) \u03c1 = \u27e6 x \u27e7ValTermCache \u03c1\n\n -- The effect of F is a writer monad of cached values, where the monoid is\n -- (isomorphic to) the free monoid over (\u2203 \u03c4 . \u03c4), but we push the\n -- existentials up when pairing things!\n\n -- That's what we're interpreting computations in. XXX maybe consider using\n -- monads directly. But that doesn't deal with arity.\n \u27e6_\u27e7CompTermCache (cReturn v) \u03c1 = vUnit , (\u27e6 v \u27e7ValTermCache \u03c1 , tt)\n\n -- For this to be enough, a lambda needs to return a produce, not to forward\n -- the underlying one (unless there are no intermediate results). The correct\n -- requirements can maybe be enforced through a linear typing discipline.\n\n {-\n -- Here we'd have a problem with the original into from CBPV, because it does\n -- not require converting expressions to the \"CBPV A-normal form\".\n\n \u27e6_\u27e7CompTermCache (v\u2081 into v\u2082) \u03c1 =\n -- Sequence commands and combine their caches.\n {-\n let (_ , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (_ , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in , (r\u2082 , (c\u2081 ,\u2032 c\u2082))\n -}\n\n -- The code above does not work, because we only guarantee that v\u2082 is\n -- a computation, not that it's an F-computation - v\u2082 could also be function\n -- type or a computation product.\n\n -- We need a restricted CBPV, where these two possibilities are forbidden. If\n -- you want to save something between different lambdas, you need to add an F\n -- U to reflect that. (Double-check the papers which show how to encode arity\n -- using CBPV, it seems that they should find the same problem --- XXX they\n -- don't).\n\n -- Instead, just drop the first cache (XXX WRONG).\n \u27e6 v\u2082 \u27e7CompTermCache (proj\u2081 (proj\u2082 (\u27e6 v\u2081 \u27e7CompTermCache \u03c1)) \u2022 \u03c1)\n -}\n\n -- But if we alter _into_ as described above, composing the caches works!\n -- However, we should not forget we also need to save the new intermediate\n -- result, that is the one produced by the first part of the let.\n \u27e6_\u27e7CompTermCache (_into_ {\u03c3} {\u03c4} v\u2081 v\u2082) \u03c1 =\n let (\u03c4\u2081 , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (\u03c4\u2082 , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in (\u03c4\u2081 v\u00d7 \u03c4\u2082 v\u00d7 \u03c3) , (r\u2082 ,\u2032 (c\u2081 ,\u2032 c\u2082 ,\u2032 r\u2081))\n\n -- Note the compositionality and luck: we don't need to do anything at the\n -- cReturn time, we just need the nested into to do their job, because as I\n -- said intermediate results are a writer monad.\n --\n -- Q: But then, do we still need to do all the other stuff? IOW, do we still\n -- need to forbid (\u03bb x . y <- f args; g args') and replace it with (\u03bb x . y <-\n -- f args; z <- g args'; z)?\n --\n -- A: One thing we still need is using the monadic version of into for the\n -- same reasons - which makes sense, since it has the type of monadic bind.\n --\n -- Maybe not: if we use a monad, which respects left and right identity, the\n -- two above forms are equivalent. But what about associativity? We don't have\n -- associativity with nested tuples in the middle. That's why the monad uses\n -- lists! We can also use nested tuple, as long as in the into case we don't\n -- do (a, b) but append a b (ahem, where?), which decomposes the first list\n -- and prepends it to the second. To this end, we need to know the type of the\n -- first element, or to ensure it's always a pair. Maybe we just want to reuse\n -- an HList.\n\n -- In abstractions, we should start collecting all variables...\n\n -- Here, unlike in \u27e6_\u27e7TermCache, we don't need to invent an empty cache,\n -- that's moved into the handling of cReturn. This makes *the* difference for\n -- nested lambdas, where we don't need to create caches multiple times!\n\n \u27e6_\u27e7CompTermCache (cAbs v) \u03c1 = \u03bb x \u2192 \u27e6 v \u27e7CompTermCache (x \u2022 \u03c1)\n\n -- Here we see that we are in a sort of A-normal form, because the argument is\n -- a value (not quite ANF though, since values can be thunks - that is,\n -- computations which haven't been run yet, I guess. Do we have an use for\n -- that? That allows passing lambdas as arguments directly - which is fine,\n -- because producing a closure indeed does not have intermediate results!).\n \u27e6_\u27e7CompTermCache (cApp t v) \u03c1 = \u27e6 t \u27e7CompTermCache \u03c1 (\u27e6 v \u27e7ValTermCache \u03c1)\n\n \u27e6_\u27e7TermCacheCBV : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 fromCBVCtx \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 cbvToCompType \u03c4 \u27e7CompTypeHidCache\n \u27e6 t \u27e7TermCacheCBV = \u27e6 fromCBV t \u27e7CompTermCache\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Caching evaluation\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\n\nimport Parametric.Syntax.MType as MType\nimport Parametric.Syntax.MTerm as MTerm\n\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Denotation.MValue as MValue\nimport Parametric.Denotation.CachingMValue as CachingMValue\nimport Parametric.Denotation.MEvaluation as MEvaluation\n\nmodule Parametric.Denotation.CachingEvaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (ValConst : MTerm.ValConstStructure Const)\n (CompConst : MTerm.CompConstStructure Const)\n -- I should really switch to records - can it get sillier than this?\n (\u27e6_\u27e7ValBase : MEvaluation.ValStructure Const \u27e6_\u27e7Base ValConst CompConst)\n (\u27e6_\u27e7CompBase : MEvaluation.CompStructure Const \u27e6_\u27e7Base ValConst CompConst)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\n\nopen MType.Structure Base\nopen MTerm.Structure Const ValConst CompConst\n\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen MValue.Structure Base \u27e6_\u27e7Base\nopen CachingMValue.Structure Base \u27e6_\u27e7Base\nopen MEvaluation.Structure Const \u27e6_\u27e7Base ValConst CompConst \u27e6_\u27e7ValBase \u27e6_\u27e7CompBase\n\nopen import Base.Denotation.Notation\n\n-- Extension Point: Evaluation of fully applied constants.\nValStructure : Set\nValStructure = \u2200 {\u03a3 \u03c4} \u2192 ValConst \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n\nCompStructure : Set\nCompStructure = \u2200 {\u03a3 \u03c4} \u2192 CompConst \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n\nmodule Structure\n (\u27e6_\u27e7ValBaseTermCache : ValStructure)\n (\u27e6_\u27e7CompBaseTermCache : CompStructure)\n where\n\n {-\n -- Prototype here the type-correctness of a simple non-standard semantics.\n -- This describes a simplified version of the transformation by Liu and\n -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language\n -- terms, we produce host language terms to take advantage of the richer type\n -- system of the host language (in particular, here we need the unit type,\n -- product types and *existentials*).\n --\n -- As usual, we'll later switch to a real term transformation.\n -}\n open import Data.Product hiding (map)\n open import Data.Unit\n\n -- Defining a caching semantics for Term proves to be hard, requiring to\n -- insert and remove caches where we apply constants.\n\n -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.\n\n -- The solution is to distinguish among different kinds of constants. Some are\n -- value constructors (and thus do not return caches), while others are\n -- computation constructors (and thus should return caches). For products, I\n -- believe we will only use the products which are values, not computations\n -- (XXX check CBPV paper for the name).\n \u27e6_\u27e7CompTermCache : \u2200 {\u03c4 \u0393} \u2192 Comp \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n \u27e6_\u27e7ValTermCache : \u2200 {\u03c4 \u0393} \u2192 Val \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n \u27e6_\u27e7ValsTermCache : \u2200 {\u0393 \u03a3} \u2192 Vals \u0393 \u03a3 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache\n\n open import Base.Denotation.Environment ValType \u27e6_\u27e7ValTypeHidCache public\n using ()\n renaming (\u27e6_\u27e7Var to \u27e6_\u27e7ValVarHidCache)\n\n -- This says that the environment does not contain caches... sounds wrong!\n -- Either we add extra variables for the caches, or we store computations in\n -- the environment (but that does not make sense), or we store caches in\n -- values, by acting not on F but on something else (U?).\n\n -- Copy of \u27e6_\u27e7Vals\n \u27e6 \u2205 \u27e7ValsTermCache \u03c1 = \u2205\n \u27e6 vt \u2022 valtms \u27e7ValsTermCache \u03c1 = \u27e6 vt \u27e7ValTermCache \u03c1 \u2022 \u27e6 valtms \u27e7ValsTermCache \u03c1\n\n -- I suspect the plan was to use extra variables; that's annoying to model in\n -- Agda but easier in implementations.\n\n \u27e6 vVar x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7ValVarHidCache \u03c1\n \u27e6 vThunk x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7CompTermCache \u03c1\n -- No caching, because the arguments are values, so evaluating them does not\n -- produce intermediate results.\n \u27e6 vConst c args \u27e7ValTermCache \u03c1 = \u27e6 c \u27e7ValBaseTermCache (\u27e6 args \u27e7ValsTermCache \u03c1)\n\n -- The only caching is done by the interpretation of the constant (because the\n -- arguments are values so need no caching).\n \u27e6_\u27e7CompTermCache (cConst c args) \u03c1 = \u27e6 c \u27e7CompBaseTermCache (\u27e6 args \u27e7ValsTermCache \u03c1)\n\n -- Also, where are introduction forms for pairs and sums among values? With\n -- them, we should see that we can interpret them without adding a cache.\n\n -- Thunks keep seeming noops.\n \u27e6_\u27e7CompTermCache (cForce x) \u03c1 = \u27e6 x \u27e7ValTermCache \u03c1\n\n -- The effect of F is a writer monad of cached values, where the monoid is\n -- (isomorphic to) the free monoid over (\u2203 \u03c4 . \u03c4), but we push the\n -- existentials up when pairing things!\n\n -- That's what we're interpreting computations in. XXX maybe consider using\n -- monads directly. But that doesn't deal with arity.\n \u27e6_\u27e7CompTermCache (cReturn v) \u03c1 = vUnit , (\u27e6 v \u27e7ValTermCache \u03c1 , tt)\n\n -- For this to be enough, a lambda needs to return a produce, not to forward\n -- the underlying one (unless there are no intermediate results). The correct\n -- requirements can maybe be enforced through a linear typing discipline.\n\n {-\n -- Here we'd have a problem with the original into from CBPV, because it does\n -- not require converting expressions to the \"CBPV A-normal form\".\n\n \u27e6_\u27e7CompTermCache (v\u2081 into v\u2082) \u03c1 =\n -- Sequence commands and combine their caches.\n {-\n let (_ , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (_ , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in , (r\u2082 , (c\u2081 ,\u2032 c\u2082))\n -}\n\n -- The code above does not work, because we only guarantee that v\u2082 is\n -- a computation, not that it's an F-computation - v\u2082 could also be function\n -- type or a computation product.\n\n -- We need a restricted CBPV, where these two possibilities are forbidden. If\n -- you want to save something between different lambdas, you need to add an F\n -- U to reflect that. (Double-check the papers which show how to encode arity\n -- using CBPV, it seems that they should find the same problem --- XXX they\n -- don't).\n\n -- Instead, just drop the first cache (XXX WRONG).\n \u27e6 v\u2082 \u27e7CompTermCache (proj\u2081 (proj\u2082 (\u27e6 v\u2081 \u27e7CompTermCache \u03c1)) \u2022 \u03c1)\n -}\n\n -- But if we alter _into_ as described above, composing the caches works!\n -- However, we should not forget we also need to save the new intermediate\n -- result, that is the one produced by the first part of the let.\n \u27e6_\u27e7CompTermCache (_into_ {\u03c3} {\u03c4} v\u2081 v\u2082) \u03c1 =\n let (\u03c4\u2081 , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (\u03c4\u2082 , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in (\u03c3 v\u00d7 \u03c4\u2081 v\u00d7 \u03c4\u2082) , (r\u2082 ,\u2032 ( r\u2081 ,\u2032 c\u2081 ,\u2032 c\u2082))\n\n -- Note the compositionality and luck: we don't need to do anything at the\n -- cReturn time, we just need the nested into to do their job, because as I\n -- said intermediate results are a writer monad.\n --\n -- Q: But then, do we still need to do all the other stuff? IOW, do we still\n -- need to forbid (\u03bb x . y <- f args; g args') and replace it with (\u03bb x . y <-\n -- f args; z <- g args'; z)?\n --\n -- A: One thing we still need is using the monadic version of into for the\n -- same reasons - which makes sense, since it has the type of monadic bind.\n --\n -- Maybe not: if we use a monad, which respects left and right identity, the\n -- two above forms are equivalent. But what about associativity? We don't have\n -- associativity with nested tuples in the middle. That's why the monad uses\n -- lists! We can also use nested tuple, as long as in the into case we don't\n -- do (a, b) but append a b (ahem, where?), which decomposes the first list\n -- and prepends it to the second. To this end, we need to know the type of the\n -- first element, or to ensure it's always a pair. Maybe we just want to reuse\n -- an HList.\n\n -- In abstractions, we should start collecting all variables...\n\n -- Here, unlike in \u27e6_\u27e7TermCache, we don't need to invent an empty cache,\n -- that's moved into the handling of cReturn. This makes *the* difference for\n -- nested lambdas, where we don't need to create caches multiple times!\n\n \u27e6_\u27e7CompTermCache (cAbs v) \u03c1 = \u03bb x \u2192 \u27e6 v \u27e7CompTermCache (x \u2022 \u03c1)\n\n -- Here we see that we are in a sort of A-normal form, because the argument is\n -- a value (not quite ANF though, since values can be thunks - that is,\n -- computations which haven't been run yet, I guess. Do we have an use for\n -- that? That allows passing lambdas as arguments directly - which is fine,\n -- because producing a closure indeed does not have intermediate results!).\n \u27e6_\u27e7CompTermCache (cApp t v) \u03c1 = \u27e6 t \u27e7CompTermCache \u03c1 (\u27e6 v \u27e7ValTermCache \u03c1)\n\n \u27e6_\u27e7TermCacheCBV : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 fromCBVCtx \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 cbvToCompType \u03c4 \u27e7CompTypeHidCache\n \u27e6 t \u27e7TermCacheCBV = \u27e6 fromCBV t \u27e7CompTermCache\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a127cbe0afe5735ec22de57b718586c54a38c33b","subject":"Fix testd","message":"Fix testd\n","repos":"louisswarren\/hieretikz","old_file":"arrow.agda","new_file":"arrow.agda","new_contents":"data Bool : Set where\n true : Bool\n false : Bool\n\n\n_or_ : Bool \u2192 Bool \u2192 Bool\ntrue or _ = true\n_ or true = true\nfalse or false = false\n\n_and_ : Bool \u2192 Bool \u2192 Bool\nfalse and _ = false\n_ and false = false\ntrue and true = true\n\n\n----------------------------------------\n\n\n\ndata \u2115 : Set where\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n{-# BUILTIN NATURAL \u2115 #-}\n\n\n_\u2261_ : \u2115 \u2192 \u2115 \u2192 Bool\nzero \u2261 zero = true\nsuc n \u2261 suc m = n \u2261 m\n_ \u2261 _ = false\n\n\n\n----------------------------------------\n\n\ninfixr 5 _\u2237_\n\ndata List (A : Set) : Set where\n \u2218 : List A\n _\u2237_ : A \u2192 List A \u2192 List A\n\n\nany : {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 Bool\nany _ \u2218 = false\nany f (x \u2237 xs) = (f x) or (any f xs)\n\n\n_\u2208_ : \u2115 \u2192 List \u2115 \u2192 Bool\nx \u2208 \u2218 = false\nx \u2208 (y \u2237 ys) with x \u2261 y\n... | true = true\n... | false = x \u2208 ys\n\n\n_\u220b_ : List \u2115 \u2192 \u2115 \u2192 Bool\nxs \u220b y = y \u2208 xs\n\n----------------------------------------\n\n\n\ndata Arrow : Set where\n \u21d2_ : \u2115 \u2192 Arrow\n _\u21d2_ : \u2115 \u2192 Arrow \u2192 Arrow\n\n_\u2261\u2261_ : Arrow \u2192 Arrow \u2192 Bool\n(\u21d2 q) \u2261\u2261 (\u21d2 s) = q \u2261 s\n(p \u21d2 q) \u2261\u2261 (r \u21d2 s) = (p \u2261 r) and (q \u2261\u2261 s)\n_ \u2261\u2261 _ = false\n\n_\u2208\u2208_ : Arrow \u2192 List Arrow \u2192 Bool\nx \u2208\u2208 \u2218 = false\nx \u2208\u2208 (y \u2237 ys) with x \u2261\u2261 y\n... | true = true\n... | false = x \u2208\u2208 ys\n\n\nclosure : List Arrow \u2192 List \u2115 \u2192 List \u2115\nclosure \u2218 found = found\nclosure ((\u21d2 n) \u2237 rest) found = n \u2237 (closure rest (n \u2237 found))\nclosure ((n \u21d2 q) \u2237 rest) found with (n \u2208 found) or (n \u2208 (closure rest found))\n... | true = closure (q \u2237 rest) found\n... | false = closure rest found\n\n\n\n_,_\u22a2_ : List Arrow \u2192 List \u2115 \u2192 \u2115 \u2192 Bool\ncs , ps \u22a2 q = q \u2208 (closure cs ps)\n\n_\u22a2_ : List Arrow \u2192 Arrow \u2192 Bool\ncs \u22a2 (\u21d2 q) = q \u2208 (closure cs \u2218)\ncs \u22a2 (p \u21d2 q) = ((\u21d2 p) \u2237 cs) \u22a2 q\n\n\n----------------------------------------\n\n\n\ndata Separation : Set where\n model : List \u2115 \u2192 List \u2115 \u2192 Separation\n\n\nmodelsupports : Separation \u2192 List Arrow \u2192 \u2115 \u2192 Bool\nmodelsupports (model holds _) cs n = cs , holds \u22a2 n\n\n\nmodeldenies : Separation \u2192 List Arrow \u2192 \u2115 \u2192 Bool\nmodeldenies (model _ fails) cs n = any (_\u220b_ (closure cs (n \u2237 \u2218))) fails\n\n\n_\u27ea!_\u27eb_ : List Arrow \u2192 Separation \u2192 Arrow \u2192 Bool\ncs \u27ea! m \u27eb (\u21d2 q) = modeldenies m cs q\ncs \u27ea! m \u27eb (p \u21d2 q) = (modelsupports m cs p) and (cs \u27ea! m \u27eb q)\n\n\n_\u27ea_\u27eb_ : List Arrow \u2192 List Separation \u2192 Arrow \u2192 Bool\ncs \u27ea \u2218 \u27eb arr = false\ncs \u27ea m \u2237 ms \u27eb arr = (cs \u27ea! m \u27eb arr) or (cs \u27ea ms \u27eb arr)\n\n\n\n----------------------------------------\n\n\n\ndata Relation : Set where\n Proved : Relation\n Derivable : Relation\n Separated : Relation\n Unknown : Relation\n\n\nconsider : List Arrow \u2192 List Separation \u2192 Arrow \u2192 Relation\nconsider cs ms arr with (arr \u2208\u2208 cs)\n... | true = Proved\n... | false with (cs \u22a2 arr)\n... | true = Derivable\n... | false with (cs \u27ea ms \u27eb arr)\n... | true = Separated\n... | false = Unknown\n\n\nproofs : List Arrow\nproofs =\n (3 \u21d2 (\u21d2 4)) \u2237\n-- (5 \u21d2 (\u21d2 4)) \u2237\n (6 \u21d2 (\u21d2 4)) \u2237\n (3 \u21d2 (\u21d2 3)) \u2237\n (3 \u21d2 (\u21d2 3)) \u2237\n (9 \u21d2 (\u21d2 3)) \u2237\n (9 \u21d2 (\u21d2 3)) \u2237\n (5 \u21d2 (\u21d2 7)) \u2237\n (9 \u21d2 (\u21d2 7)) \u2237\n (6 \u21d2 (\u21d2 8)) \u2237\n (10 \u21d2 (\u21d2 8)) \u2237\n (10 \u21d2 (\u21d2 7)) \u2237\n (5 \u21d2 (\u21d2 10)) \u2237\n (10 \u21d2 (\u21d2 4)) \u2237\n (5 \u21d2 (\u21d2 11)) \u2237\n (6 \u21d2 (\u21d2 11)) \u2237\n (11 \u21d2 (\u21d2 4)) \u2237\n (10 \u21d2 (\u21d2 7)) \u2237\n (8 \u21d2 (\u21d2 4)) \u2237\n (9 \u21d2 (\u21d2 7)) \u2237\n (9 \u21d2 (\u21d2 8)) \u2237\n (3 \u21d2 (\u21d2 8)) \u2237\n (5 \u21d2 (3 \u21d2 (\u21d2 9))) \u2237\n (6 \u21d2 (7 \u21d2 (\u21d2 10))) \u2237\n (6 \u21d2 (3 \u21d2 (\u21d2 3))) \u2237\n (7 \u21d2 (\u21d2 7)) \u2237\n (7 \u21d2 (\u21d2 7)) \u2237\n (7 \u21d2 (8 \u21d2 (\u21d2 10))) \u2237\n (3 \u21d2 (10 \u21d2 (\u21d2 9))) \u2237\n (5 \u21d2 (\u21d2 1)) \u2237\n (3 \u21d2 (1 \u21d2 (\u21d2 9))) \u2237\n (1 \u21d2 (\u21d2 2)) \u2237\n (10 \u21d2 (\u21d2 2)) \u2237 \u2218\n\ncms : List Separation\ncms =\n (model (12 \u2237 6 \u2237 11 \u2237 4 \u2237 1 \u2237 \u2218) (5 \u2237 3 \u2237 7 \u2237 7 \u2237 \u2218)) \u2237\n (model (6 \u2237 3 \u2237 11 \u2237 4 \u2237 7 \u2237 8 \u2237 3 \u2237 9 \u2237 10 \u2237 1 \u2237 \u2218) (5 \u2237 \u2218)) \u2237\n (model (12 \u2237 5 \u2237 11 \u2237 4 \u2237 1 \u2237 \u2218) (6 \u2237 3 \u2237 \u2218)) \u2237\n (model (5 \u2237 3 \u2237 11 \u2237 4 \u2237 7 \u2237 8 \u2237 3 \u2237 9 \u2237 10 \u2237 1 \u2237 \u2218) (6 \u2237 \u2218)) \u2237\n (model (12 \u2237 4 \u2237 11 \u2237 \u2218) (5 \u2237 6 \u2237 3 \u2237 8 \u2237 9 \u2237 1 \u2237 \u2218)) \u2237\n (model (12 \u2237 5 \u2237 6 \u2237 4 \u2237 11 \u2237 1 \u2237 \u2218) (3 \u2237 \u2218)) \u2237\n (model (12 \u2237 4 \u2237 11 \u2237 7 \u2237 \u2218) (9 \u2237 5 \u2237 6 \u2237 10 \u2237 8 \u2237 1 \u2237 \u2218)) \u2237\n (model (10 \u2237 9 \u2237 \u2218) (1 \u2237 \u2218)) \u2237\n (model (3 \u2237 4 \u2237 11 \u2237 \u2218) (9 \u2237 5 \u2237 6 \u2237 10 \u2237 7 \u2237 1 \u2237 \u2218)) \u2237\n (model (12 \u2237 7 \u2237 1 \u2237 \u2218) (4 \u2237 11 \u2237 8 \u2237 \u2218)) \u2237\n (model (9 \u2237 3 \u2237 10 \u2237 8 \u2237 1 \u2237 \u2218) (11 \u2237 \u2218)) \u2237\n (model (12 \u2237 4 \u2237 10 \u2237 1 \u2237 \u2218) (11 \u2237 3 \u2237 \u2218)) \u2237\n (model (3 \u2237 6 \u2237 5 \u2237 \u2218) (\u2218)) \u2237\n (model (1 \u2237 2 \u2237 3 \u2237 4 \u2237 5 \u2237 6 \u2237 7 \u2237 8 \u2237 9 \u2237 10 \u2237 11 \u2237 \u2218) (12 \u2237 \u2218)) \u2237 \u2218\n\ntestp : Arrow\ntestp = (5 \u21d2 (\u21d2 10))\n\ntestd : Arrow\ntestd = (5 \u21d2 (\u21d2 4))\n\ntests : Arrow\ntests = (5 \u21d2 (\u21d2 3))\n\ntestu : Arrow\ntestu = (6 \u21d2 (\u21d2 1))\n","old_contents":"data Bool : Set where\n true : Bool\n false : Bool\n\n\n_or_ : Bool \u2192 Bool \u2192 Bool\ntrue or _ = true\n_ or true = true\nfalse or false = false\n\n_and_ : Bool \u2192 Bool \u2192 Bool\nfalse and _ = false\n_ and false = false\ntrue and true = true\n\n\n----------------------------------------\n\n\n\ndata \u2115 : Set where\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n{-# BUILTIN NATURAL \u2115 #-}\n\n\n_\u2261_ : \u2115 \u2192 \u2115 \u2192 Bool\nzero \u2261 zero = true\nsuc n \u2261 suc m = n \u2261 m\n_ \u2261 _ = false\n\n\n\n----------------------------------------\n\n\ninfixr 5 _\u2237_\n\ndata List (A : Set) : Set where\n \u2218 : List A\n _\u2237_ : A \u2192 List A \u2192 List A\n\n\nany : {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 Bool\nany _ \u2218 = false\nany f (x \u2237 xs) = (f x) or (any f xs)\n\n\n_\u2208_ : \u2115 \u2192 List \u2115 \u2192 Bool\nx \u2208 \u2218 = false\nx \u2208 (y \u2237 ys) with x \u2261 y\n... | true = true\n... | false = x \u2208 ys\n\n\n_\u220b_ : List \u2115 \u2192 \u2115 \u2192 Bool\nxs \u220b y = y \u2208 xs\n\n----------------------------------------\n\n\n\ndata Arrow : Set where\n \u21d2_ : \u2115 \u2192 Arrow\n _\u21d2_ : \u2115 \u2192 Arrow \u2192 Arrow\n\n_\u2261\u2261_ : Arrow \u2192 Arrow \u2192 Bool\n(\u21d2 q) \u2261\u2261 (\u21d2 s) = q \u2261 s\n(p \u21d2 q) \u2261\u2261 (r \u21d2 s) = (p \u2261 r) and (q \u2261\u2261 s)\n_ \u2261\u2261 _ = false\n\n_\u2208\u2208_ : Arrow \u2192 List Arrow \u2192 Bool\nx \u2208\u2208 \u2218 = false\nx \u2208\u2208 (y \u2237 ys) with x \u2261\u2261 y\n... | true = true\n... | false = x \u2208\u2208 ys\n\n\nclosure : List Arrow \u2192 List \u2115 \u2192 List \u2115\nclosure \u2218 found = found\nclosure ((\u21d2 n) \u2237 rest) found = n \u2237 (closure rest (n \u2237 found))\nclosure ((n \u21d2 q) \u2237 rest) found with (n \u2208 found) or (n \u2208 (closure rest found))\n... | true = closure (q \u2237 rest) found\n... | false = closure rest found\n\n\n\n_,_\u22a2_ : List Arrow \u2192 List \u2115 \u2192 \u2115 \u2192 Bool\ncs , ps \u22a2 q = q \u2208 (closure cs ps)\n\n_\u22a2_ : List Arrow \u2192 Arrow \u2192 Bool\ncs \u22a2 (\u21d2 q) = q \u2208 (closure cs \u2218)\ncs \u22a2 (p \u21d2 q) = ((\u21d2 p) \u2237 cs) \u22a2 q\n\n\n----------------------------------------\n\n\n\ndata Separation : Set where\n model : List \u2115 \u2192 List \u2115 \u2192 Separation\n\n\nmodelsupports : Separation \u2192 List Arrow \u2192 \u2115 \u2192 Bool\nmodelsupports (model holds _) cs n = cs , holds \u22a2 n\n\n\nmodeldenies : Separation \u2192 List Arrow \u2192 \u2115 \u2192 Bool\nmodeldenies (model _ fails) cs n = any (_\u220b_ (closure cs (n \u2237 \u2218))) fails\n\n\n_\u27ea!_\u27eb_ : List Arrow \u2192 Separation \u2192 Arrow \u2192 Bool\ncs \u27ea! m \u27eb (\u21d2 q) = modeldenies m cs q\ncs \u27ea! m \u27eb (p \u21d2 q) = (modelsupports m cs p) and (cs \u27ea! m \u27eb q)\n\n\n_\u27ea_\u27eb_ : List Arrow \u2192 List Separation \u2192 Arrow \u2192 Bool\ncs \u27ea \u2218 \u27eb arr = false\ncs \u27ea m \u2237 ms \u27eb arr = (cs \u27ea! m \u27eb arr) or (cs \u27ea ms \u27eb arr)\n\n\n\n----------------------------------------\n\n\n\ndata Relation : Set where\n Proved : Relation\n Derivable : Relation\n Separated : Relation\n Unknown : Relation\n\n\nconsider : List Arrow \u2192 List Separation \u2192 Arrow \u2192 Relation\nconsider cs ms arr with (arr \u2208\u2208 cs)\n... | true = Proved\n... | false with (cs \u22a2 arr)\n... | true = Derivable\n... | false with (cs \u27ea ms \u27eb arr)\n... | true = Separated\n... | false = Unknown\n\n\nproofs : List Arrow\nproofs =\n (3 \u21d2 (\u21d2 4)) \u2237\n-- (5 \u21d2 (\u21d2 4)) \u2237\n (6 \u21d2 (\u21d2 4)) \u2237\n (3 \u21d2 (\u21d2 3)) \u2237\n (3 \u21d2 (\u21d2 3)) \u2237\n (9 \u21d2 (\u21d2 3)) \u2237\n (9 \u21d2 (\u21d2 3)) \u2237\n (5 \u21d2 (\u21d2 7)) \u2237\n (9 \u21d2 (\u21d2 7)) \u2237\n (6 \u21d2 (\u21d2 8)) \u2237\n (10 \u21d2 (\u21d2 8)) \u2237\n (10 \u21d2 (\u21d2 7)) \u2237\n (5 \u21d2 (\u21d2 10)) \u2237\n (10 \u21d2 (\u21d2 4)) \u2237\n (5 \u21d2 (\u21d2 11)) \u2237\n (6 \u21d2 (\u21d2 11)) \u2237\n (11 \u21d2 (\u21d2 4)) \u2237\n (10 \u21d2 (\u21d2 7)) \u2237\n (8 \u21d2 (\u21d2 4)) \u2237\n (9 \u21d2 (\u21d2 7)) \u2237\n (9 \u21d2 (\u21d2 8)) \u2237\n (3 \u21d2 (\u21d2 8)) \u2237\n (5 \u21d2 (3 \u21d2 (\u21d2 9))) \u2237\n (6 \u21d2 (7 \u21d2 (\u21d2 10))) \u2237\n (6 \u21d2 (3 \u21d2 (\u21d2 3))) \u2237\n (7 \u21d2 (\u21d2 7)) \u2237\n (7 \u21d2 (\u21d2 7)) \u2237\n (7 \u21d2 (8 \u21d2 (\u21d2 10))) \u2237\n (3 \u21d2 (10 \u21d2 (\u21d2 9))) \u2237\n (5 \u21d2 (\u21d2 1)) \u2237\n (3 \u21d2 (1 \u21d2 (\u21d2 9))) \u2237\n (1 \u21d2 (\u21d2 2)) \u2237\n (10 \u21d2 (\u21d2 2)) \u2237 \u2218\n\ncms : List Separation\ncms =\n (model (12 \u2237 6 \u2237 11 \u2237 4 \u2237 1 \u2237 \u2218) (5 \u2237 3 \u2237 7 \u2237 7 \u2237 \u2218)) \u2237\n (model (6 \u2237 3 \u2237 11 \u2237 4 \u2237 7 \u2237 8 \u2237 3 \u2237 9 \u2237 10 \u2237 1 \u2237 \u2218) (5 \u2237 \u2218)) \u2237\n (model (12 \u2237 5 \u2237 11 \u2237 4 \u2237 1 \u2237 \u2218) (6 \u2237 3 \u2237 \u2218)) \u2237\n (model (5 \u2237 3 \u2237 11 \u2237 4 \u2237 7 \u2237 8 \u2237 3 \u2237 9 \u2237 10 \u2237 1 \u2237 \u2218) (6 \u2237 \u2218)) \u2237\n (model (12 \u2237 4 \u2237 11 \u2237 \u2218) (5 \u2237 6 \u2237 3 \u2237 8 \u2237 9 \u2237 1 \u2237 \u2218)) \u2237\n (model (12 \u2237 5 \u2237 6 \u2237 4 \u2237 11 \u2237 1 \u2237 \u2218) (3 \u2237 \u2218)) \u2237\n (model (12 \u2237 4 \u2237 11 \u2237 7 \u2237 \u2218) (9 \u2237 5 \u2237 6 \u2237 10 \u2237 8 \u2237 1 \u2237 \u2218)) \u2237\n (model (10 \u2237 9 \u2237 \u2218) (1 \u2237 \u2218)) \u2237\n (model (3 \u2237 4 \u2237 11 \u2237 \u2218) (9 \u2237 5 \u2237 6 \u2237 10 \u2237 7 \u2237 1 \u2237 \u2218)) \u2237\n (model (12 \u2237 7 \u2237 1 \u2237 \u2218) (4 \u2237 11 \u2237 8 \u2237 \u2218)) \u2237\n (model (9 \u2237 3 \u2237 10 \u2237 8 \u2237 1 \u2237 \u2218) (11 \u2237 \u2218)) \u2237\n (model (12 \u2237 4 \u2237 10 \u2237 1 \u2237 \u2218) (11 \u2237 3 \u2237 \u2218)) \u2237\n (model (3 \u2237 6 \u2237 5 \u2237 \u2218) (\u2218)) \u2237\n (model (1 \u2237 2 \u2237 3 \u2237 4 \u2237 5 \u2237 6 \u2237 7 \u2237 8 \u2237 9 \u2237 10 \u2237 11 \u2237 \u2218) (12 \u2237 \u2218)) \u2237 \u2218\n\ntestp : Arrow\ntestp = (5 \u21d2 (\u21d2 10))\n\ntestd : Arrow\ntestd = (5 \u21d2 (\u21d2 7))\n\ntests : Arrow\ntests = (5 \u21d2 (\u21d2 3))\n\ntestu : Arrow\ntestu = (6 \u21d2 (\u21d2 1))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"50aed72e5cdc9e329d9804c724535dfc771ad1b6","subject":"Some progress on FunBigStepSILR2","message":"Some progress on FunBigStepSILR2\n\nHowever, relV is still slightly wrong. Fixed version in Alt. Gotta change a few\nlemmas now!\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/FunBigStepSILR2.agda","new_file":"Thesis\/FunBigStepSILR2.agda","new_contents":"{-# OPTIONS --allow-unsolved-metas #-}\n-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\" (ISAC below), POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n\n-- WARNING: ISAC's big-step semantics produces a step count as \"output\". But\n-- that would not help Agda establish termination. That's only a problem for a\n-- functional big-step semantics, not for a relational semantics.\n--\n-- So, instead, I tried to use a sort of writer monad: the interpreter gets fuel\n-- and returns the remaining fuel. That's the same trick as in \"functional\n-- big-step semantics\". However that doesn't help termination either,\n-- since Agda doesn't see that the returned fuel is no bigger.\n\n-- Since we focus for now on STLC, unlike that\n-- paper, we can avoid error values by keeping types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) zero = TimeOut\nevalConst (lit v) (suc n) = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\neval (app s t) \u03c1 (suc n) = (eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)) n\n\neval-const-dec : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-const-dec (lit v) zero n1 ()\neval-const-dec (lit v) (suc n0) .n0 refl = \u2264-step \u2264-refl\n\n{-# TERMINATING #-}\neval-dec : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-dec (const c) \u03c1 v n0 n1 eq = eval-const-dec c n0 n1 eq\neval-dec (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (app s t) \u03c1 v zero n1 ()\neval-dec (app s t) \u03c1 v (suc n0) n3 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv sn1 | [ seq ] with eval t \u03c1 sn1 | inspect (eval t \u03c1) sn1\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv@(closure st s\u03c1) sn1 | [ seq ] | (Done tv tn2) | [ teq ] = \u2264-step (\u2264-trans (\u2264-trans (eval-dec st _ _ _ _ eq) (eval-dec t _ _ _ _ teq)) (eval-dec s _ _ _ _ seq))\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | Error | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | TimeOut | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Error | [ seq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | TimeOut | [ seq ]\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1)\neval-const-mono (lit v) zero n1 ()\neval-const-mono (lit v) (suc n0) .n0 refl = refl\n\n-- ARGH\n{-# TERMINATING #-}\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1)\neval-mono (const c) \u03c1 v n0 n1 eq = eval-const-mono c n0 n1 eq\neval-mono (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\neval-mono (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\neval-mono (app s t) \u03c1 v zero n1 ()\neval-mono (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-mono t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n\nmodule Alt where\n mutual\n relT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\n relT {\u03c4} t1 t2 \u03c11 \u03c12 n =\n (v1 : Val \u03c4) \u2192\n \u2200 n-j (n-j\u2264n : n-j < n) \u2192\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n -- Here, computing t2 is allowed to take an unbounded number of steps. Having to write a number at all is annoying.\n\n relV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n -- Seems the proof for abs would go through even if here we do not step down.\n -- However, that only works as long as we use a typed language; not stepping\n -- down here, in an untyped language, gives a non-well-founded definition.\n relV nat v1 v2 n = v1 \u2261 v2\n relV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) n =\n \u2200 (k : \u2115) (k\u2264n : k < n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\nmutual\n relT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\n relT {\u03c4} t1 t2 \u03c11 \u03c12 zero = \u22a4\n relT {\u03c4} t1 t2 \u03c11 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n -- Here, computing t2 is allowed to take an unbounded number of steps. Having to write a number at all is annoying.\n\n relV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n relV \u03c4 v1 v2 zero = \u22a4\n -- Seems the proof for abs would go through even if here we do not step down.\n -- However, that only works as long as we use a typed language; not stepping\n -- down here, in an untyped language, gives a non-well-founded definition.\n relV nat v1 v2 (suc n) = v1 \u2261 v2\n relV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) =\n \u2200 (k : \u2115) (k\u2264n : k \u2264 n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\n -- Here, in the conclusion, I'm not relating app (closure t1 \u03c11) v1 with app\n -- (closure t2 \u03c12) v2 (or some encoding of that that actually works), but the\n -- result of taking a step from that configuration. That is important, because\n -- both Pitts' \"Step-Indexed Biorthogonality: a Tutorial Example\" and\n -- \"Imperative Self-Adjusting Computation\" do the same thing (and point out it's\n -- important).\n\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono zero n m\u2264n \u03c4 v1 v2 vv = tt\nrelV-mono (suc m) (suc n) (s\u2264s m\u2264n) nat v1 v2 vv = vv\nrelV-mono (suc m) (suc n) (s\u2264s m\u2264n) (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) ff k k\u2264m = ff k (\u2264-trans k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 .n n-j\u2264n refl = v2 , zero , zero , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\n-- relT (app s t) (app s t)\n\nrelV-apply : \u2200 {\u03c3 \u03c4 \u0393} (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t v1 \u03c12 n-j\n n1 sv1 sv2\n (svv : relV (\u03c3 \u21d2 \u03c4) sv1 sv2 n1)\n n2 tv1 tv2\n (tvv : relV \u03c3 tv1 tv2 (suc (suc n2)))\n (eq : apply sv1 tv1 n2 \u2261 Done v1 n-j) \u2192\n -- (tvv)\n -- (eqv1)\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval (app s t) \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\nrelV-apply s t v1 \u03c1 n-j n1 sv1 sv2 svv n2 tv1 tv2 tvv eq = {!!}\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\nfundamental (const (lit v)) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (const (lit v)) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 .(intV v) .n n-j\u2264n refl = intV v , suc zero , zero , refl , refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(closure t \u03c11) .n n-j\u2264n refl = closure t \u03c12 , n , n , refl , (\u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k (suc n) (\u2264-step k\u2264n) _ _ _ \u03c1\u03c1))\nfundamental (app s t) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq with eval s \u03c11 n | inspect (eval s \u03c11) n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done sv1 n1 | [ s1eq ] with eval t \u03c11 n1 | inspect (eval t \u03c11) n1\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done (closure st1 s\u03c11) n1 | [ s1eq ] | Done tv1 n2 | [ t1eq ] with fundamental s (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) (suc n1) (s\u2264s (eval-dec s \u03c11 _ n n1 s1eq)) (eval-mono s \u03c11 (closure st1 s\u03c11) n n1 s1eq) | fundamental t (suc (suc n1)) \u03c11 \u03c12 (rel\u03c1-mono (suc (suc n1)) (suc (suc n)) (s\u2264s (s\u2264s (eval-dec s \u03c11 _ n n1 s1eq))) _ _ _ \u03c1\u03c1) tv1 (suc n2) (s\u2264s (eval-dec t \u03c11 _ n1 n2 t1eq)) (eval-mono t \u03c11 tv1 n1 n2 t1eq)\n... | sv2@(closure st2 s\u03c12) , sn3 , sn4 , s2eq , svv | tv2 , tn3 , tn4 , t2eq , tvv = {! relV-apply s t v1 \u03c12 n-j _ _ sv2 svv _ tv1 tv2 tvv eq!}\n--\n-- {!eval s \u03c12 !}\n-- fundamental s (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) ? ?\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | Error | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | TimeOut | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Error | [ s1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | TimeOut | [ s1eq ]\n","old_contents":"{-# OPTIONS --allow-unsolved-metas #-}\n-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\", POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n-- Since we focus for now on STLC, unlike that\n-- paper, we can avoid error values by keeping types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) zero = TimeOut\nevalConst (lit v) (suc n) = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\neval (app s t) \u03c1 (suc n) = (eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)) n\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1)\neval-const-mono (lit v) zero n1 ()\neval-const-mono (lit v) (suc n0) .n0 refl = refl\n\n-- ARGH\n{-# TERMINATING #-}\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 \u2200 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1)\neval-mono (const c) \u03c1 v n0 n1 eq = eval-const-mono c n0 n1 eq\neval-mono (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\neval-mono (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\neval-mono (app s t) \u03c1 v zero n1 ()\neval-mono (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-mono t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n\nrelT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\n\nrelV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\nrelV \u03c4 v1 v2 zero = \u22a4\n-- Seems the proof for abs would go through even if here we do not step down.\n-- However, that only works as long as we use a typed language; not stepping\n-- down here, in an untyped language, gives a non-well-founded definition.\nrelV nat v1 v2 (suc n) = v1 \u2261 v2\nrelV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) =\n \u2200 (k : \u2115) (k\u2264n : k \u2264 n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\n\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\nrelT {\u03c4} t1 t2 \u03c11 \u03c12 zero = \u22a4\nrelT {\u03c4} t1 t2 \u03c11 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono zero n m\u2264n \u03c4 v1 v2 vv = tt\nrelV-mono (suc m) (suc n) (s\u2264s m\u2264n) nat v1 v2 vv = vv\nrelV-mono (suc m) (suc n) (s\u2264s m\u2264n) (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) ff k k\u2264m = ff k (\u2264-trans k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 .n n-j\u2264n refl = v2 , zero , zero , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\n-- relT (app s t) (app s t)\n\nrelV-apply : \u2200 {\u03c3 \u03c4 \u0393} (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t v1 \u03c12 n-j \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval (app s t) \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\nrelV-apply = {!!}\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\nfundamental (const (lit v)) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (const (lit v)) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 .(intV v) .n n-j\u2264n refl = intV v , suc zero , zero , refl , refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(closure t \u03c11) .n n-j\u2264n refl = closure t \u03c12 , n , n , refl , (\u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k (suc n) (\u2264-step k\u2264n) _ _ _ \u03c1\u03c1))\nfundamental (app s t) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq with eval s \u03c11 n | inspect (eval s \u03c11) n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done sv1 n1 | [ s1eq ] with eval t \u03c11 n1 | inspect (eval t \u03c11) n1\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done (closure st1 s\u03c11) n1 | [ s1eq ] | Done tv1 n2 | [ t1eq ] with fundamental s _ \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) (suc n1) (s\u2264s {!!}) (eval-mono s \u03c11 (closure st1 s\u03c11) n n1 s1eq) | fundamental t _ \u03c11 \u03c12 \u03c1\u03c1 tv1 (suc n2) {!!} {!eval-mono t \u03c11 tv1 n1 n2 t1eq!}\n... | closure st2 s\u03c12 , sn3 , sn4 , s2eq , svv | tv2 , tn3 , tn4 , t2eq , tvv = {!!}\n--\n-- {!eval s \u03c12 !}\n-- fundamental s (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) ? ?\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | Error | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | TimeOut | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Error | [ s1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | TimeOut | [ s1eq ]\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"442691e90525bfa889ed6b3aa1507ccd8fddc7df","subject":"Prove extensionality of d.o.e. (#332, #333)","message":"Prove extensionality of d.o.e. (#332, #333)\n\nOld-commit-hash: ae48f8fe6bde1aac0a03f2feb7177328be4ab5df\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Equivalence.agda","new_file":"Base\/Change\/Equivalence.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set a\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : FC.FunctionChange f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} dfxdx\u2259dgxdx = lemma\u2082\n where\n open \u2261-Reasoning\n open import Postulate.Extensionality\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 = dfxdx\u2259dgxdx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set a\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"475c999cc7f09cce353c6e8be0c4e04f7b3db93f","subject":"adding a few more exchange properties","message":"adding a few more exchange properties\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"exchange.agda","new_file":"exchange.agda","new_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import lemmas-disjointness\n\nmodule exchange where\n -- exchanging just two disequal elements produces the same context\n swap-little : {A : Set} {x y : Nat} {\u03c41 \u03c42 : A} \u2192 (x \u2260 y) \u2192\n ((\u25a0 (x , \u03c41)) ,, (y , \u03c42)) == ((\u25a0 (y , \u03c42)) ,, (x , \u03c41))\n swap-little {A} {x} {y} {\u03c41} {\u03c42} neq = \u222acomm (\u25a0 (x , \u03c41))\n (\u25a0 (y , \u03c42))\n (disjoint-singles neq)\n\n -- really the core of all the exchange arguments: contexts with two\n -- disequal elements exchanged are the same. we reassociate the unions,\n -- swap as above, and then associate them back.\n --\n -- note that this is generic in the contents of the context. the proofs\n -- below show the exchange properties that we actually need in the\n -- various other proofs; the remaning exchange properties for both \u0394 and\n -- \u0393 positions for all the other hypothetical judgements are exactly in\n -- this pattern.\n swap : {A : Set} {x y : Nat} {\u03c41 \u03c42 : A} (\u0393 : A ctx) (x\u2260y : x == y \u2192 \u22a5) \u2192\n ((\u0393 ,, (x , \u03c41)) ,, (y , \u03c42)) == ((\u0393 ,, (y , \u03c42)) ,, (x , \u03c41))\n swap {A} {x} {y} {\u03c41} {\u03c42} \u0393 neq =\n (\u222aassoc \u0393 (\u25a0 (x , \u03c41)) (\u25a0 (y , \u03c42)) (disjoint-singles neq) ) \u00b7\n (ap1 (\u03bb qq \u2192 \u0393 \u222a qq) (swap-little neq) \u00b7\n ! (\u222aassoc \u0393 (\u25a0 (y , \u03c42)) (\u25a0 (x , \u03c41)) (disjoint-singles (flip neq))))\n\n -- the above exchange principle used via transport in the judgements we needed\n exchange-subst-\u0393 : \u2200{\u0394 \u0393 x y \u03c41 \u03c42 \u03c3 \u0393'} \u2192\n x \u2260 y \u2192\n \u0394 , (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) \u22a2 \u03c3 :s: \u0393'\n exchange-subst-\u0393 {\u0394} {\u0393} {x} {y} {\u03c41} {\u03c42} {\u03c3} {\u0393'} x\u2260y =\n tr (\u03bb qq \u2192 \u0394 , qq \u22a2 \u03c3 :s: \u0393') (swap \u0393 x\u2260y)\n\n exchange-synth : \u2200{\u0393 x y \u03c4 \u03c41 \u03c42 e}\n \u2192 x \u2260 y\n \u2192 (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) \u22a2 e => \u03c4\n \u2192 (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) \u22a2 e => \u03c4\n exchange-synth {\u0393} {x} {y} {\u03c4} {\u03c41} {\u03c42} {e} neq =\n tr (\u03bb qq \u2192 qq \u22a2 e => \u03c4) (swap \u0393 neq)\n\n exchange-ana : \u2200{\u0393 x y \u03c4 \u03c41 \u03c42 e}\n \u2192 x \u2260 y\n \u2192 (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) \u22a2 e <= \u03c4\n \u2192 (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) \u22a2 e <= \u03c4\n exchange-ana {\u0393} {x} {y} {\u03c4} {\u03c41} {\u03c42} {e} neq =\n tr (\u03bb qq \u2192 qq \u22a2 e <= \u03c4) (swap \u0393 neq)\n\n exchange-expand-synth : \u2200{\u0393 x y \u03c41 \u03c42 e \u03c4 d \u0394} \u2192\n x \u2260 y \u2192\n (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394\n exchange-expand-synth {\u0393 = \u0393} {e = e} {\u03c4 = \u03c4} {d = d } {\u0394 = \u0394} neq =\n tr (\u03bb qq \u2192 qq \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394) (swap \u0393 neq)\n\n exchange-expand-ana : \u2200 {\u0393 x y \u03c41 \u03c42 \u03c4 \u03c4' d e \u0394} \u2192\n x \u2260 y \u2192\n (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n exchange-expand-ana {\u0393 = \u0393} {\u03c4 = \u03c4} {\u03c4' = \u03c4'} {d = d} {e = e} {\u0394 = \u0394} neq =\n tr (\u03bb qq \u2192 qq \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394) (swap \u0393 neq)\n","old_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import lemmas-disjointness\n\nmodule exchange where\n -- exchanging just two disequal elements produces the same context\n swap-little : {A : Set} {x y : Nat} {\u03c41 \u03c42 : A} \u2192 (x \u2260 y) \u2192\n ((\u25a0 (x , \u03c41)) ,, (y , \u03c42)) == ((\u25a0 (y , \u03c42)) ,, (x , \u03c41))\n swap-little {A} {x} {y} {\u03c41} {\u03c42} neq = \u222acomm (\u25a0 (x , \u03c41))\n (\u25a0 (y , \u03c42))\n (disjoint-singles neq)\n\n -- really the core of all the exchange arguments: contexts with two\n -- disequal elements exchanged are the same. we reassociate the unions,\n -- swap as above, and then associate them back.\n --\n -- note that this is generic in the contents of the context. the proofs\n -- below show the exchange properties that we actually need in the\n -- various other proofs; the remaning exchange properties for both \u0394 and\n -- \u0393 positions for all the other hypothetical judgements are exactly in\n -- this pattern.\n swap : {A : Set} {x y : Nat} {\u03c41 \u03c42 : A} (\u0393 : A ctx) (x\u2260y : x == y \u2192 \u22a5) \u2192\n ((\u0393 ,, (x , \u03c41)) ,, (y , \u03c42)) == ((\u0393 ,, (y , \u03c42)) ,, (x , \u03c41))\n swap {A} {x} {y} {\u03c41} {\u03c42} \u0393 neq =\n (\u222aassoc \u0393 (\u25a0 (x , \u03c41)) (\u25a0 (y , \u03c42)) (disjoint-singles neq) ) \u00b7\n (ap1 (\u03bb qq \u2192 \u0393 \u222a qq) (swap-little neq) \u00b7\n ! (\u222aassoc \u0393 (\u25a0 (y , \u03c42)) (\u25a0 (x , \u03c41)) (disjoint-singles (flip neq))))\n\n -- the above exchange principle used via transport in the judgements we needed\n exchange-subst-\u0393 : \u2200{\u0394 \u0393 x y \u03c41 \u03c42 \u03c3 \u0393'} \u2192\n x \u2260 y \u2192\n \u0394 , (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) \u22a2 \u03c3 :s: \u0393'\n exchange-subst-\u0393 {\u0394} {\u0393} {x} {y} {\u03c41} {\u03c42} {\u03c3} {\u0393'} x\u2260y xy =\n tr (\u03bb qq \u2192 \u0394 , qq \u22a2 \u03c3 :s: \u0393') (swap \u0393 x\u2260y) xy\n\n exchange-synth : \u2200{\u0393 x y \u03c4 \u03c41 \u03c42 e}\n \u2192 x \u2260 y\n \u2192 (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) \u22a2 e => \u03c4\n \u2192 (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) \u22a2 e => \u03c4\n exchange-synth {\u0393} {x} {y} {\u03c4} {\u03c41} {\u03c42} {e} neq synth =\n tr (\u03bb qq \u2192 qq \u22a2 e => \u03c4) (swap \u0393 neq) synth\n\n exchange-ana : \u2200{\u0393 x y \u03c4 \u03c41 \u03c42 e}\n \u2192 x \u2260 y\n \u2192 (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) \u22a2 e <= \u03c4\n \u2192 (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) \u22a2 e <= \u03c4\n exchange-ana {\u0393} {x} {y} {\u03c4} {\u03c41} {\u03c42} {e} neq ana =\n tr (\u03bb qq \u2192 qq \u22a2 e <= \u03c4) (swap \u0393 neq) ana\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"62397bbcc510a68f9c365f02e17fc7ec42140abc","subject":"Added doc.","message":"Added doc.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/LTC-PCF\/Data\/Nat\/Rec\/ConversionRules.agda","new_file":"src\/fot\/LTC-PCF\/Data\/Nat\/Rec\/ConversionRules.agda","new_contents":"------------------------------------------------------------------------------\n-- Conversion rules for the recursive operator rec\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule LTC-PCF.Data.Nat.Rec.ConversionRules where\n\nopen import Common.FOL.Relation.Binary.EqReasoning\nopen import Common.Function\n\nopen import LTC-PCF.Base\nopen import LTC-PCF.Data.Nat.Rec\n\n----------------------------------------------------------------------------\n\nprivate\n -- We follow the same methodology used in\n -- LTC-PCF.Program.Division.EquationsI (see it for the\n -- documentation).\n\n ----------------------------------------------------------------------------\n -- The steps\n\n -- Initially, the conversion rule fix-eq is applied.\n rec-s\u2081 : D \u2192 D \u2192 D \u2192 D\n rec-s\u2081 n a f = rech (fix rech) \u00b7 n \u00b7 a \u00b7 f\n\n -- First argument application.\n rec-s\u2082 : D \u2192 D\n rec-s\u2082 n = lam (\u03bb a \u2192 lam (\u03bb f \u2192\n (if (iszero\u2081 n)\n then a\n else f \u00b7 pred\u2081 n \u00b7 (fix rech \u00b7 pred\u2081 n \u00b7 a \u00b7 f))))\n\n -- Second argument application.\n rec-s\u2083 : D \u2192 D \u2192 D\n rec-s\u2083 n a = lam (\u03bb f \u2192\n (if (iszero\u2081 n)\n then a\n else f \u00b7 pred\u2081 n \u00b7 (fix rech \u00b7 pred\u2081 n \u00b7 a \u00b7 f)))\n\n -- Third argument application.\n rec-s\u2084 : D \u2192 D \u2192 D \u2192 D\n rec-s\u2084 n a f = if (iszero\u2081 n)\n then a\n else f \u00b7 pred\u2081 n \u00b7 (fix rech \u00b7 pred\u2081 n \u00b7 a \u00b7 f)\n\n -- Reduction iszero\u2081 n \u2261 b.\n rec-s\u2085 : D \u2192 D \u2192 D \u2192 D \u2192 D\n rec-s\u2085 n a f b = if b\n then a\n else f \u00b7 pred\u2081 n \u00b7 (fix rech \u00b7 pred\u2081 n \u00b7 a \u00b7 f)\n\n -- Reduction of iszero\u2081 n \u2261 true\n --\n -- It should be\n -- rec-s\u2086 : D \u2192 D \u2192 D \u2192 D\n -- rec-s\u2086 n a f = a\n --\n -- but we do not give a name to this step.\n\n -- Reduction iszero\u2081 n \u2261 false.\n rec-s\u2086 : D \u2192 D \u2192 D \u2192 D\n rec-s\u2086 n a f = f \u00b7 pred\u2081 n \u00b7 (fix rech \u00b7 pred\u2081 n \u00b7 a \u00b7 f)\n\n -- Reduction pred\u2081 (succ n) \u2261 n.\n rec-s\u2087 : D \u2192 D \u2192 D \u2192 D\n rec-s\u2087 n a f = f \u00b7 n \u00b7 (fix rech \u00b7 n \u00b7 a \u00b7 f)\n\n ----------------------------------------------------------------------------\n -- The execution steps\n\n -- We follow the same methodology used in\n -- LTC-PCF.Program.Division.EquationsI (see it for the\n -- documentation).\n\n -- Application of the conversion rule fix-eq.\n proof\u2080\u208b\u2081 : \u2200 n a f \u2192 fix rech \u00b7 n \u00b7 a \u00b7 f \u2261 rec-s\u2081 n a f\n proof\u2080\u208b\u2081 n a f = subst (\u03bb x \u2192 x \u00b7 n \u00b7 a \u00b7 f \u2261\n rech (fix rech) \u00b7 n \u00b7 a \u00b7 f )\n (sym (fix-eq rech))\n refl\n\n -- Application of the first argument.\n proof\u2081\u208b\u2082 : \u2200 n a f \u2192 rec-s\u2081 n a f \u2261 rec-s\u2082 n \u00b7 a \u00b7 f\n proof\u2081\u208b\u2082 n a f = subst (\u03bb x \u2192 x \u00b7 a \u00b7 f \u2261 rec-s\u2082 n \u00b7 a \u00b7 f)\n (sym (beta rec-s\u2082 n))\n refl\n\n -- Application of the second argument.\n proof\u2082\u208b\u2083 : \u2200 n a f \u2192 rec-s\u2082 n \u00b7 a \u00b7 f \u2261 rec-s\u2083 n a \u00b7 f\n proof\u2082\u208b\u2083 n a f = subst (\u03bb x \u2192 x \u00b7 f \u2261 rec-s\u2083 n a \u00b7 f)\n (sym (beta (rec-s\u2083 n) a))\n refl\n\n -- Application of the third argument.\n proof\u2083\u208b\u2084 : \u2200 n a f \u2192 rec-s\u2083 n a \u00b7 f \u2261 rec-s\u2084 n a f\n proof\u2083\u208b\u2084 n a f = beta (rec-s\u2084 n a) f\n\n -- 01 June 2013. This proof could use pattern matching on _\u2261_. See\n -- Agda issue 865.\n --\n -- Cases iszero\u2081 n \u2261 b using that proof.\n proof\u2084\u208b\u2085 : \u2200 n a f b \u2192 iszero\u2081 n \u2261 b \u2192 rec-s\u2084 n a f \u2261 rec-s\u2085 n a f b\n proof\u2084\u208b\u2085 n a f b h = subst (\u03bb x \u2192 rec-s\u2085 n a f x \u2261 rec-s\u2085 n a f b)\n (sym h)\n refl\n\n -- Reduction of if true ... using the conversion rule if-true.\n proof\u2085\u208a : \u2200 n a f \u2192 rec-s\u2085 n a f true \u2261 a\n proof\u2085\u208a n a f = if-true a\n\n -- Reduction of if false ... using the conversion rule if-false.\n proof\u2085\u208b\u2086 : \u2200 n a f \u2192 rec-s\u2085 n a f false \u2261 rec-s\u2086 n a f\n proof\u2085\u208b\u2086 n a f = if-false (rec-s\u2086 n a f)\n\n -- Reduction pred\u2081 (succ n) \u2261 n using the conversion rule pred\u2081-S.\n proof\u2086\u208b\u2087 : \u2200 n a f \u2192 rec-s\u2086 (succ\u2081 n) a f \u2261 rec-s\u2087 n a f\n proof\u2086\u208b\u2087 n a f = subst (\u03bb x \u2192 rec-s\u2087 x a f \u2261 rec-s\u2087 n a f)\n (sym (pred-S n))\n refl\n\n------------------------------------------------------------------------------\n-- The conversion rules for rec.\n\nrec-0 : \u2200 a {f} \u2192 rec zero a f \u2261 a\nrec-0 a {f} =\n fix rech \u00b7 zero \u00b7 a \u00b7 f \u2261\u27e8 proof\u2080\u208b\u2081 zero a f \u27e9\n rec-s\u2081 zero a f \u2261\u27e8 proof\u2081\u208b\u2082 zero a f \u27e9\n rec-s\u2082 zero \u00b7 a \u00b7 f \u2261\u27e8 proof\u2082\u208b\u2083 zero a f \u27e9\n rec-s\u2083 zero a \u00b7 f \u2261\u27e8 proof\u2083\u208b\u2084 zero a f \u27e9\n rec-s\u2084 zero a f \u2261\u27e8 proof\u2084\u208b\u2085 zero a f true iszero-0 \u27e9\n rec-s\u2085 zero a f true \u2261\u27e8 proof\u2085\u208a zero a f \u27e9\n a \u220e\n\nrec-S : \u2200 n a f \u2192 rec (succ\u2081 n) a f \u2261 f \u00b7 n \u00b7 (rec n a f)\nrec-S n a f =\n fix rech \u00b7 (succ\u2081 n) \u00b7 a \u00b7 f \u2261\u27e8 proof\u2080\u208b\u2081 (succ\u2081 n) a f \u27e9\n rec-s\u2081 (succ\u2081 n) a f \u2261\u27e8 proof\u2081\u208b\u2082 (succ\u2081 n) a f \u27e9\n rec-s\u2082 (succ\u2081 n) \u00b7 a \u00b7 f \u2261\u27e8 proof\u2082\u208b\u2083 (succ\u2081 n) a f \u27e9\n rec-s\u2083 (succ\u2081 n) a \u00b7 f \u2261\u27e8 proof\u2083\u208b\u2084 (succ\u2081 n) a f \u27e9\n rec-s\u2084 (succ\u2081 n) a f \u2261\u27e8 proof\u2084\u208b\u2085 (succ\u2081 n) a f false (iszero-S n) \u27e9\n rec-s\u2085 (succ\u2081 n) a f false \u2261\u27e8 proof\u2085\u208b\u2086 (succ\u2081 n) a f \u27e9\n rec-s\u2086 (succ\u2081 n) a f \u2261\u27e8 proof\u2086\u208b\u2087 n a f \u27e9\n rec-s\u2087 n a f \u220e\n","old_contents":"------------------------------------------------------------------------------\n-- Conversion rules for the recursive operator rec\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule LTC-PCF.Data.Nat.Rec.ConversionRules where\n\nopen import Common.FOL.Relation.Binary.EqReasoning\nopen import Common.Function\n\nopen import LTC-PCF.Base\nopen import LTC-PCF.Data.Nat.Rec\n\n----------------------------------------------------------------------------\n\nprivate\n -- We follow the same methodology used in\n -- LTC-PCF.Program.Division.EquationsI (see it for the\n -- documentation).\n\n ----------------------------------------------------------------------------\n -- The steps\n\n -- Initially, the conversion rule fix-eq is applied.\n rec-s\u2081 : D \u2192 D \u2192 D \u2192 D\n rec-s\u2081 n a f = rech (fix rech) \u00b7 n \u00b7 a \u00b7 f\n\n -- First argument application.\n rec-s\u2082 : D \u2192 D\n rec-s\u2082 n = lam (\u03bb a \u2192 lam (\u03bb f \u2192\n (if (iszero\u2081 n)\n then a\n else f \u00b7 pred\u2081 n \u00b7 (fix rech \u00b7 pred\u2081 n \u00b7 a \u00b7 f))))\n\n -- Second argument application.\n rec-s\u2083 : D \u2192 D \u2192 D\n rec-s\u2083 n a = lam (\u03bb f \u2192\n (if (iszero\u2081 n)\n then a\n else f \u00b7 pred\u2081 n \u00b7 (fix rech \u00b7 pred\u2081 n \u00b7 a \u00b7 f)))\n\n -- Third argument application.\n rec-s\u2084 : D \u2192 D \u2192 D \u2192 D\n rec-s\u2084 n a f = if (iszero\u2081 n)\n then a\n else f \u00b7 pred\u2081 n \u00b7 (fix rech \u00b7 pred\u2081 n \u00b7 a \u00b7 f)\n\n -- Reduction iszero\u2081 n \u2261 b.\n rec-s\u2085 : D \u2192 D \u2192 D \u2192 D \u2192 D\n rec-s\u2085 n a f b = if b\n then a\n else f \u00b7 pred\u2081 n \u00b7 (fix rech \u00b7 pred\u2081 n \u00b7 a \u00b7 f)\n\n -- Reduction of iszero\u2081 n \u2261 true\n --\n -- It should be\n -- rec-s\u2086 : D \u2192 D \u2192 D \u2192 D\n -- rec-s\u2086 n a f = a\n --\n -- but we do not give a name to this step.\n\n -- Reduction iszero\u2081 n \u2261 false.\n rec-s\u2086 : D \u2192 D \u2192 D \u2192 D\n rec-s\u2086 n a f = f \u00b7 pred\u2081 n \u00b7 (fix rech \u00b7 pred\u2081 n \u00b7 a \u00b7 f)\n\n -- Reduction pred\u2081 (succ n) \u2261 n.\n rec-s\u2087 : D \u2192 D \u2192 D \u2192 D\n rec-s\u2087 n a f = f \u00b7 n \u00b7 (fix rech \u00b7 n \u00b7 a \u00b7 f)\n\n ----------------------------------------------------------------------------\n -- The execution steps\n\n -- We follow the same methodology used in\n -- LTC-PCF.Program.Division.EquationsI (see it for the\n -- documentation).\n\n -- Application of the conversion rule fix-eq.\n proof\u2080\u208b\u2081 : \u2200 n a f \u2192 fix rech \u00b7 n \u00b7 a \u00b7 f \u2261 rec-s\u2081 n a f\n proof\u2080\u208b\u2081 n a f = subst (\u03bb x \u2192 x \u00b7 n \u00b7 a \u00b7 f \u2261 rech (fix rech) \u00b7 n \u00b7 a \u00b7 f )\n (sym (fix-eq rech))\n refl\n\n -- Application of the first argument.\n proof\u2081\u208b\u2082 : \u2200 n a f \u2192 rec-s\u2081 n a f \u2261 rec-s\u2082 n \u00b7 a \u00b7 f\n proof\u2081\u208b\u2082 n a f = subst (\u03bb x \u2192 x \u00b7 a \u00b7 f \u2261 rec-s\u2082 n \u00b7 a \u00b7 f)\n (sym (beta rec-s\u2082 n))\n refl\n\n -- Application of the second argument.\n proof\u2082\u208b\u2083 : \u2200 n a f \u2192 rec-s\u2082 n \u00b7 a \u00b7 f \u2261 rec-s\u2083 n a \u00b7 f\n proof\u2082\u208b\u2083 n a f = subst (\u03bb x \u2192 x \u00b7 f \u2261 rec-s\u2083 n a \u00b7 f)\n (sym (beta (rec-s\u2083 n) a))\n refl\n\n -- Application of the third argument.\n proof\u2083\u208b\u2084 : \u2200 n a f \u2192 rec-s\u2083 n a \u00b7 f \u2261 rec-s\u2084 n a f\n proof\u2083\u208b\u2084 n a f = beta (rec-s\u2084 n a) f\n\n -- Cases iszero\u2081 n \u2261 b using that proof.\n proof\u2084\u208b\u2085 : \u2200 n a f b \u2192 iszero\u2081 n \u2261 b \u2192 rec-s\u2084 n a f \u2261 rec-s\u2085 n a f b\n proof\u2084\u208b\u2085 n a f b h = subst (\u03bb x \u2192 rec-s\u2085 n a f x \u2261 rec-s\u2085 n a f b)\n (sym h)\n refl\n\n -- Reduction of if true ... using the conversion rule if-true.\n proof\u2085\u208a : \u2200 n a f \u2192 rec-s\u2085 n a f true \u2261 a\n proof\u2085\u208a n a f = if-true a\n\n -- Reduction of if false ... using the conversion rule if-false.\n proof\u2085\u208b\u2086 : \u2200 n a f \u2192 rec-s\u2085 n a f false \u2261 rec-s\u2086 n a f\n proof\u2085\u208b\u2086 n a f = if-false (rec-s\u2086 n a f)\n\n -- Reduction pred\u2081 (succ n) \u2261 n using the conversion rule pred\u2081-S.\n proof\u2086\u208b\u2087 : \u2200 n a f \u2192 rec-s\u2086 (succ\u2081 n) a f \u2261 rec-s\u2087 n a f\n proof\u2086\u208b\u2087 n a f = subst (\u03bb x \u2192 rec-s\u2087 x a f \u2261 rec-s\u2087 n a f)\n (sym (pred-S n))\n refl\n\n------------------------------------------------------------------------------\n-- The conversion rules for rec.\n\nrec-0 : \u2200 a {f} \u2192 rec zero a f \u2261 a\nrec-0 a {f} =\n fix rech \u00b7 zero \u00b7 a \u00b7 f \u2261\u27e8 proof\u2080\u208b\u2081 zero a f \u27e9\n rec-s\u2081 zero a f \u2261\u27e8 proof\u2081\u208b\u2082 zero a f \u27e9\n rec-s\u2082 zero \u00b7 a \u00b7 f \u2261\u27e8 proof\u2082\u208b\u2083 zero a f \u27e9\n rec-s\u2083 zero a \u00b7 f \u2261\u27e8 proof\u2083\u208b\u2084 zero a f \u27e9\n rec-s\u2084 zero a f \u2261\u27e8 proof\u2084\u208b\u2085 zero a f true iszero-0 \u27e9\n rec-s\u2085 zero a f true \u2261\u27e8 proof\u2085\u208a zero a f \u27e9\n a \u220e\n\nrec-S : \u2200 n a f \u2192 rec (succ\u2081 n) a f \u2261 f \u00b7 n \u00b7 (rec n a f)\nrec-S n a f =\n fix rech \u00b7 (succ\u2081 n) \u00b7 a \u00b7 f \u2261\u27e8 proof\u2080\u208b\u2081 (succ\u2081 n) a f \u27e9\n rec-s\u2081 (succ\u2081 n) a f \u2261\u27e8 proof\u2081\u208b\u2082 (succ\u2081 n) a f \u27e9\n rec-s\u2082 (succ\u2081 n) \u00b7 a \u00b7 f \u2261\u27e8 proof\u2082\u208b\u2083 (succ\u2081 n) a f \u27e9\n rec-s\u2083 (succ\u2081 n) a \u00b7 f \u2261\u27e8 proof\u2083\u208b\u2084 (succ\u2081 n) a f \u27e9\n rec-s\u2084 (succ\u2081 n) a f \u2261\u27e8 proof\u2084\u208b\u2085 (succ\u2081 n) a f false (iszero-S n) \u27e9\n rec-s\u2085 (succ\u2081 n) a f false \u2261\u27e8 proof\u2085\u208b\u2086 (succ\u2081 n) a f \u27e9\n rec-s\u2086 (succ\u2081 n) a f \u2261\u27e8 proof\u2086\u208b\u2087 n a f \u27e9\n rec-s\u2087 n a f \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"921eab0bc4910e71e779f2cb9286cf901b189466","subject":"Updated an issue.","message":"Updated an issue.\n\nIgnore-this: 6271a60d2dbe3a0f1d5d53e46e39ae9b\n\ndarcs-hash:20110820132419-3bd4e-4a602e1125c80e4134202edb8306da90d9c4f9ad.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Issues\/ProofTerm.agda","new_file":"Issues\/ProofTerm.agda","new_contents":"------------------------------------------------------------------------------\n-- Testing the erasure of proof terms\n------------------------------------------------------------------------------\n\n-- An internal error has occurred. Please report this as a bug.\n-- Location of the error: src\/AgdaLib\/Syntax\/DeBruijn.hs:417\n\nmodule Issues.ProofTerm where\n\npostulate\n D : Set\n N : D \u2192 Set\n _\u2261_ : D \u2192 D \u2192 Set\n _\u2237_ : D \u2192 D \u2192 D\n\ndata ListN : D \u2192 Set where\n consLN : \u2200 {n ns} \u2192 N n \u2192 (LNns : ListN ns) \u2192 ListN (n \u2237 ns)\n{-# ATP axiom consLN #-}\n\n-- We need to have at least one conjecture to generate a TPTP file.\npostulate refl : \u2200 d \u2192 d \u2261 d\n{-# ATP prove refl #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Testing the erasure of proof terms\n------------------------------------------------------------------------------\n\nmodule Issues.Tmp.ProofTerm where\n\npostulate\n D : Set\n N : D \u2192 Set\n _\u2261_ : D \u2192 D \u2192 Set\n _\u2237_ : D \u2192 D \u2192 D\n\ndata ListN : D \u2192 Set where\n consLN : \u2200 {n ns} \u2192 N n \u2192 (LNns : ListN ns) \u2192 ListN (n \u2237 ns)\n{-# ATP axiom consLN #-}\n\n-- We need to have at least one conjecture to generate a TPTP file.\npostulate refl : \u2200 d \u2192 d \u2261 d\n{-# ATP prove refl #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7d43e03aba3b449b2ebcdd9117c92952093ab3bb","subject":"some syntax to match the paper, more rules","message":"some syntax to match the paper, more rules\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"core.agda","new_file":"core.agda","new_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data \u03c4\u0307 : Set where\n b : \u03c4\u0307\n \u2987\u2988 : \u03c4\u0307\n _==>_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 \u03c4\u0307\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data e\u0307 : Set where\n c : e\u0307\n _\u00b7:_ : e\u0307 \u2192 \u03c4\u0307 \u2192 e\u0307\n X : Nat \u2192 e\u0307\n \u00b7\u03bb : Nat \u2192 e\u0307 \u2192 e\u0307\n \u00b7\u03bb_[_]_ : Nat \u2192 \u03c4\u0307 \u2192 e\u0307 \u2192 e\u0307\n \u2987\u2988[_] : Nat \u2192 e\u0307\n \u2987_\u2988[_] : e\u0307 \u2192 Nat \u2192 e\u0307\n _\u2218_ : e\u0307 \u2192 e\u0307 \u2192 e\u0307\n\n subst : Set -- todo: no idea if this is right\n subst = e\u0307 ctx\n\n -- expressions without ascriptions but with casts\n data \u00eb : Set where\n c : \u00eb\n X : Nat \u2192 \u00eb\n \u00b7\u03bb_[_]_ : Nat \u2192 \u03c4\u0307 \u2192 \u00eb \u2192 \u00eb\n \u2987\u2988[_&_] : Nat \u2192 subst \u2192 \u00eb\n \u2987_\u2988[_&_] : \u00eb \u2192 Nat \u2192 subst \u2192 \u00eb\n _\u2218_ : \u00eb \u2192 \u00eb \u2192 \u00eb\n <_>_ : \u00eb \u2192 \u03c4\u0307 \u2192 \u00eb\n\n -- type consistency\n data _~_ : (t1 : \u03c4\u0307) \u2192 (t2 : \u03c4\u0307) \u2192 Set where\n TCRefl : {t : \u03c4\u0307} \u2192 t ~ t\n TCHole1 : {t : \u03c4\u0307} \u2192 t ~ \u2987\u2988\n TCHole2 : {t : \u03c4\u0307} \u2192 \u2987\u2988 ~ t\n TCArr : {t1 t2 t1' t2' : \u03c4\u0307} \u2192\n t1 ~ t1' \u2192\n t2 ~ t2' \u2192\n t1 ==> t2 ~ t1' ==> t2'\n\n -- type inconsistency\n data _~\u0338_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n ICBaseArr1 : {t1 t2 : \u03c4\u0307} \u2192 b ~\u0338 t1 ==> t2\n ICBaseArr2 : {t1 t2 : \u03c4\u0307} \u2192 t1 ==> t2 ~\u0338 b\n ICArr1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t1 ~\u0338 t3 \u2192\n t1 ==> t2 ~\u0338 t3 ==> t4\n ICArr2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t2 ~\u0338 t4 \u2192\n t1 ==> t2 ~\u0338 t3 ==> t4\n\n --- matching for arrows, sums, and products\n data _\u25b8arr_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {t1 t2 : \u03c4\u0307} \u2192 t1 ==> t2 \u25b8arr t1 ==> t2\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = \u03c4\u0307 ctx\n\n hctx : Set\n hctx = (\u03c4\u0307 ctx \u00d7 \u03c4\u0307) ctx\n\n postulate -- todo: write this stuff later\n id : {A : Set} \u2192 A ctx \u2192 subst\n [_]_ : subst \u2192 \u00eb \u2192 \u00eb\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 \u03c4\u0307 \u2192 (Nat \u00d7 tctx \u00d7 \u03c4\u0307)\n u ::[ \u0393 ] t = u , \u0393 , t\n\n\n -- bidirectional type checking judgements for e\u0307\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) \u2192 (e : e\u0307) \u2192 (t : \u03c4\u0307) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : e\u0307} {t : \u03c4\u0307} \u2192\n \u0393 \u22a2 e <= t \u2192\n \u0393 \u22a2 (e \u00b7: t) => t\n SVar : {\u0393 : tctx} {t : \u03c4\u0307} {n : Nat} \u2192\n (n , t) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => t\n SAp : {\u0393 : tctx} {e1 e2 : e\u0307} {t t' t2 : \u03c4\u0307} \u2192\n \u0393 \u22a2 e1 => t \u2192\n t \u25b8arr t2 ==> t' \u2192\n \u0393 \u22a2 e2 <= t2 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => t'\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of n?\n SNEHole : {\u0393 : tctx} {e : e\u0307} {t : \u03c4\u0307} {u : Nat} \u2192 -- todo: uniqueness of n?\n \u0393 \u22a2 e => t \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : e\u0307} {t1 t2 : \u03c4\u0307} {x : Nat} \u2192\n x # \u0393 \u2192 -- todo\n (\u0393 ,, (x , t1)) \u22a2 e => t2 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ t1 ] e => t1 ==> t2\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : \u03c4\u0307 ctx) \u2192 (e : e\u0307) \u2192 (t : \u03c4\u0307) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : e\u0307} {t t' : \u03c4\u0307} \u2192\n \u0393 \u22a2 e => t' \u2192\n t ~ t' \u2192\n \u0393 \u22a2 e <= t\n ALam : {\u0393 : tctx} {e : e\u0307} {t t1 t2 : \u03c4\u0307} {x : Nat} \u2192\n x # \u0393 \u2192\n t \u25b8arr t1 ==> t2 \u2192\n (\u0393 ,, (x , t1)) \u22a2 e <= t2 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= t\n\n -- todo: do we care about completeness of e\u0307 or e-umlauts?\n -- those types without holes anywhere\n tcomplete : \u03c4\u0307 \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (t1 ==> t2) = tcomplete t1 \u00d7 tcomplete t2\n\n -- those expressions without holes anywhere\n ecomplete : e\u0307 \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: t) = ecomplete e1 \u00d7 tcomplete t\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ t ] e) = tcomplete t \u00d7 ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : e\u0307) (t : \u03c4\u0307) (e' : \u00eb) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x t} \u2192 (\u0393 ,, (x , t)) \u22a2 X x \u21d2 t ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x t1 t2 e e' \u0394 } \u2192\n (\u0393 ,, (x , t1)) \u22a2 e \u21d2 t2 ~> e' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ t1 ] e \u21d2 (t1 ==> t2) ~> \u00b7\u03bb x [ t1 ] e' \u22a3 \u2205\n\n -- todo: really ought to check disjointness of domains here ..\n ESAp1 : \u2200{\u0393 e1 e2 e2' e1' \u03941 t2 t1 \u03942} \u2192\n \u0393 \u22a2 e1 => \u2987\u2988 \u2192\n \u0393 \u22a2 e2 \u21d0 \u2987\u2988 ~> e2' :: t2 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u21d0 (t2 ==> \u2987\u2988) ~> e1' :: t1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u2987\u2988 ~> (< e1' > t2) \u2218 e2' \u22a3 (\u03941 \u222a \u03942)\n ESAp2 : \u2200{\u0393 e1 t2 t e1' e2' \u03941 \u03942 t2' e2} \u2192\n \u0393 \u22a2 e1 \u21d2 (t2 ==> t) ~> e1' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 t2 ~> e2' :: t2' \u22a3 \u03942 \u2192\n (t2 == t2' \u2192 \u22a5) \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 t ~> e1' \u2218 (< e2' > t2) \u22a3 (\u03941 \u222a \u03942)\n ESAp3 : \u2200{\u0393 e1 t e1' \u03941 e2 t2 e2' \u03942 } \u2192\n \u0393 \u22a2 e1 \u21d2 (t2 ==> t) ~> e1' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 t2 ~> e2' :: t2 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 t ~> e1' \u2218 e2' \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988[ u & id \u0393 ] \u22a3 (\u2205 ,, (u ::[ \u0393 ] \u2987\u2988))\n ESNEHole : \u2200{ \u0393 e t e' u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 t ~> e' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 e' \u2988[ u & id \u0393 ] \u22a3 (\u0394 ,, (u ::[ \u0393 ] \u2987\u2988))\n ESAsc1 : \u2200 {\u0393 e t e' t' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 t ~> e' :: t' \u22a3 \u0394 \u2192\n (t == t' \u2192 \u22a5) \u2192\n \u0393 \u22a2 (e \u00b7: t) \u21d2 t ~> (< e' > t) \u22a3 \u0394\n ESAsc2 : \u2200{\u0393 e t e' t' \u0394 } \u2192\n \u0393 \u22a2 e \u21d0 t ~> e' :: t' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: t) \u21d2 t ~> e' \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : e\u0307) (t : \u03c4\u0307) (e' : \u00eb) (t' : \u03c4\u0307)(\u0394 : hctx) \u2192 Set where\n -- EALam :\n -- EASubsume :\n -- EAEHole :\n -- EANEHole :\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (e' : \u00eb) (t : \u03c4\u0307) \u2192 Set where\n\n -- value\n data _val : \u00eb \u2192 Set where\n VConst : c val\n VLam : \u2200{x t e} \u2192 (\u00b7\u03bb x [ t ] e) val\n\n -- error\n data _err[_] : \u00eb \u2192 hctx \u2192 Set where -- todo not a context\n\n mutual\n -- indeterminate\n data _indet : \u00eb \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988[ u & \u03c3 ] indet\n INEHole : \u2200{e u \u03c3} \u2192 e final \u2192 \u2987 e \u2988[ u & \u03c3 ] indet\n IAp : \u2200{e1 e2} \u2192 e1 indet \u2192 e2 final \u2192 (e1 \u2218 e2) indet\n ICast : \u2200{e t} \u2192 e indet \u2192 (< e > t) indet\n\n -- final\n data _final : \u00eb \u2192 Set where\n FVal : \u2200{e} \u2192 e val \u2192 e final\n FIndet : \u2200{e} \u2192 e indet \u2192 e final\n\n -- small step semantics\n data _\u21a6_ : \u00eb \u2192 \u00eb \u2192 Set where\n","old_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data \u03c4\u0307 : Set where\n b : \u03c4\u0307\n \u2987\u2988 : \u03c4\u0307\n _==>_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 \u03c4\u0307\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data e\u0307 : Set where\n c : e\u0307\n _\u00b7:_ : e\u0307 \u2192 \u03c4\u0307 \u2192 e\u0307\n X : Nat \u2192 e\u0307\n \u00b7\u03bb : Nat \u2192 e\u0307 \u2192 e\u0307\n \u00b7\u03bb_[_]_ : Nat \u2192 \u03c4\u0307 \u2192 e\u0307 \u2192 e\u0307\n \u2987\u2988[_] : Nat \u2192 e\u0307\n \u2987_\u2988[_] : e\u0307 \u2192 Nat \u2192 e\u0307\n _\u2218_ : e\u0307 \u2192 e\u0307 \u2192 e\u0307\n\n subst : Set -- todo: no idea if this is right\n subst = e\u0307 ctx\n\n -- expressions without ascriptions but with casts\n data \u00eb : Set where\n c : \u00eb\n X : Nat \u2192 \u00eb\n \u00b7\u03bb_[_]_ : Nat \u2192 \u03c4\u0307 \u2192 \u00eb \u2192 \u00eb\n \u2987\u2988[_&_] : Nat \u2192 subst \u2192 \u00eb\n \u2987_\u2988[_&_] : \u00eb \u2192 Nat \u2192 subst \u2192 \u00eb\n _\u2218_ : \u00eb \u2192 \u00eb \u2192 \u00eb\n <_>_ : \u00eb \u2192 \u03c4\u0307 \u2192 \u00eb\n\n -- type consistency\n data _~_ : (t1 : \u03c4\u0307) \u2192 (t2 : \u03c4\u0307) \u2192 Set where\n TCRefl : {t : \u03c4\u0307} \u2192 t ~ t\n TCHole1 : {t : \u03c4\u0307} \u2192 t ~ \u2987\u2988\n TCHole2 : {t : \u03c4\u0307} \u2192 \u2987\u2988 ~ t\n TCArr : {t1 t2 t1' t2' : \u03c4\u0307} \u2192\n t1 ~ t1' \u2192\n t2 ~ t2' \u2192\n t1 ==> t2 ~ t1' ==> t2'\n\n -- type inconsistency\n data _~\u0338_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n ICBaseArr1 : {t1 t2 : \u03c4\u0307} \u2192 b ~\u0338 t1 ==> t2\n ICBaseArr2 : {t1 t2 : \u03c4\u0307} \u2192 t1 ==> t2 ~\u0338 b\n ICArr1 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t1 ~\u0338 t3 \u2192\n t1 ==> t2 ~\u0338 t3 ==> t4\n ICArr2 : {t1 t2 t3 t4 : \u03c4\u0307} \u2192\n t2 ~\u0338 t4 \u2192\n t1 ==> t2 ~\u0338 t3 ==> t4\n\n --- matching for arrows, sums, and products\n data _\u25b8arr_ : \u03c4\u0307 \u2192 \u03c4\u0307 \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {t1 t2 : \u03c4\u0307} \u2192 t1 ==> t2 \u25b8arr t1 ==> t2\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = \u03c4\u0307 ctx\n\n hctx : Set\n hctx = (\u03c4\u0307 ctx \u00d7 \u03c4\u0307) ctx\n\n -- bidirectional type checking judgements for e\u0307\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) \u2192 (e : e\u0307) \u2192 (t : \u03c4\u0307) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : e\u0307} {t : \u03c4\u0307} \u2192\n \u0393 \u22a2 e <= t \u2192\n \u0393 \u22a2 (e \u00b7: t) => t\n SVar : {\u0393 : tctx} {t : \u03c4\u0307} {n : Nat} \u2192\n (n , t) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => t\n SAp : {\u0393 : tctx} {e1 e2 : e\u0307} {t t' t2 : \u03c4\u0307} \u2192\n \u0393 \u22a2 e1 => t \u2192\n t \u25b8arr t2 ==> t' \u2192\n \u0393 \u22a2 e2 <= t2 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => t'\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of n?\n SNEHole : {\u0393 : tctx} {e : e\u0307} {t : \u03c4\u0307} {u : Nat} \u2192 -- todo: uniqueness of n?\n \u0393 \u22a2 e => t \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : e\u0307} {t1 t2 : \u03c4\u0307} {x : Nat} \u2192\n x # \u0393 \u2192 -- todo\n (\u0393 ,, (x , t1)) \u22a2 e => t2 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ t1 ] e => t1 ==> t2\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : \u03c4\u0307 ctx) \u2192 (e : e\u0307) \u2192 (t : \u03c4\u0307) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : e\u0307} {t t' : \u03c4\u0307} \u2192\n \u0393 \u22a2 e => t' \u2192\n t ~ t' \u2192\n \u0393 \u22a2 e <= t\n ALam : {\u0393 : tctx} {e : e\u0307} {t t1 t2 : \u03c4\u0307} {x : Nat} \u2192\n x # \u0393 \u2192\n t \u25b8arr t1 ==> t2 \u2192\n (\u0393 ,, (x , t1)) \u22a2 e <= t2 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= t\n\n -- those types without holes anywhere\n tcomplete : \u03c4\u0307 \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (t1 ==> t2) = tcomplete t1 \u00d7 tcomplete t2\n\n -- those expressions without holes anywhere\n ecomplete : e\u0307 \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: t) = ecomplete e1 \u00d7 tcomplete t\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ t ] e) = tcomplete t \u00d7 ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : e\u0307) (t : \u03c4\u0307) (e' : \u00eb) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x t} \u2192 (\u0393 ,, (x , t)) \u22a2 X x \u21d2 t ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x t1 t2 e e' \u0394 } \u2192\n (\u0393 ,, (x , t1)) \u22a2 e \u21d2 t2 ~> e' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ t1 ] e \u21d2 (t1 ==> t2) ~> \u00b7\u03bb x [ t1 ] e' \u22a3 \u2205\n\n -- todo: really ought to check disjointness of domains here ..\n ESAp1 : \u2200{\u0393 e1 e2 e2' e1' \u03941 t2 t1 \u03942} \u2192\n \u0393 \u22a2 e1 => \u2987\u2988 \u2192\n \u0393 \u22a2 e2 \u21d0 \u2987\u2988 ~> e2' :: t2 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u21d0 (t2 ==> \u2987\u2988) ~> e1' :: t1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) \u21d2 \u2987\u2988 ~> (< e1' > t2) \u2218 e2' \u22a3 (\u03941 \u222a \u03942)\n -- ESAp2\n -- ESAp3\n -- ESEHole\n -- ESNEHole\n -- ESAsc1\n -- ESAsc2\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : e\u0307) (t : \u03c4\u0307) (e' : \u00eb) (t' : \u03c4\u0307)(\u0394 : hctx) \u2192 Set where\n -- EALam\n -- EASubsume\n -- EAEHole\n -- EANEHole\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (e' : \u00eb) (t : \u03c4\u0307) \u2192 Set where\n\n -- todo: ugh\n postulate\n [_]_ : subst \u2192 \u00eb \u2192 \u00eb\n\n -- value\n data _val : \u00eb \u2192 Set where\n VConst : c val\n VLam : \u2200{x t e} \u2192 (\u00b7\u03bb x [ t ] e) val\n\n -- error\n data _err[_] : \u00eb \u2192 hctx \u2192 Set where -- todo not a context\n\n mutual\n -- indeterminate\n data _indet : \u00eb \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988[ u & \u03c3 ] indet\n INEHole : \u2200{e u \u03c3} \u2192 e final \u2192 \u2987 e \u2988[ u & \u03c3 ] indet\n IAp : \u2200{e1 e2} \u2192 e1 indet \u2192 e2 final \u2192 (e1 \u2218 e2) indet\n ICast : \u2200{e t} \u2192 e indet \u2192 (< e > t) indet\n\n -- final\n data _final : \u00eb \u2192 Set where\n FVal : \u2200{e} \u2192 e val \u2192 e final\n FIndet : \u2200{e} \u2192 e indet \u2192 e final\n\n -- small step semantics\n data _\u21a6_ : \u00eb \u2192 \u00eb \u2192 Set where\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"5bd5879c8a87ce091f5d83193ee97bd3d644101e","subject":"FunUniverse: not was broken","message":"FunUniverse: not was broken\n","repos":"crypto-agda\/crypto-agda","old_file":"FunUniverse\/Core.agda","new_file":"FunUniverse\/Core.agda","new_contents":"module FunUniverse.Core where\n\nopen import Type\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2^_)\nimport Data.Bool.NP as B\nopen B using (if_then_else_; true; false)\nopen import Data.Unit using (\u22a4)\nopen import Data.Fin using (Fin)\nopen import Function using (_\u2218\u2032_; flip)\nimport Data.Vec.NP as V\nimport Level as L\nopen V using (Vec; []; _\u2237_)\n\nopen import Data.Bit using (Bit; 0b; 1b)\nopen import Data.Bits using (Bits; _\u2192\u1d47_; RewireTbl; 0\u207f; 1\u207f)\n\nimport FunUniverse.BinTree as Tree\nopen Tree using (Tree)\nopen import FunUniverse.Data\n\nopen import FunUniverse.Types public\nimport FunUniverse.Defaults.FirstPart as Defaults\u27e8first-part\u27e9\nopen import FunUniverse.Rewiring.Linear\n\nrecord HasBijFork {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n field\n -- bijFork f\u2080 f\u2081 (0' , x) = 0' , f\u2080 x\n -- bijFork f\u2080 f\u2081 (1' , x) = 1' , f\u2081 x\n bijFork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 `Bit `\u00d7 B\n\n -- bijFork\u2032 f\u2080 f\u2081 (0' , x) = 0' , f\u2080 0' x\n -- bijFork\u2032 f\u2080 f\u2081 (1' , x) = 1' , f\u2081 1' x\n bijFork\u2032 : \u2200 {A B} \u2192 (Bit \u2192 A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 `Bit `\u00d7 B\n bijFork\u2032 f = bijFork (f 0b) (f 1b)\n\nrecord HasFork {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor _,_\n open FunUniverse funU\n field\n -- cond (0' , x\u2080 , x\u2081) = x\u2080\n -- cond (1' , x\u2080 , x\u2081) = x\u2081\n -- See Defaults.DefaultCond\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n\n -- fork f\u2080 f\u2081 (0' , x) = f\u2080 x\n -- fork f\u2080 f\u2081 (1' , x) = f\u2081 x\n -- See Defaults.DefaultFork\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n\n fork\u2032 : \u2200 {A B} \u2192 (Bit \u2192 A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork\u2032 f = fork (f 0b) (f 1b)\n\nrecord HasXor {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n field\n xor : \u2200 {n} \u2192 Bits n \u2192 `Endo (`Bits n)\n\n vnot : \u2200 {n} \u2192 `Endo (`Bits n)\n vnot = xor 1\u207f\n\n \u27e8\u2295_\u27e9 : \u2200 {n} \u2192 Bits n \u2192 `Endo (`Bits n)\n \u27e8\u2295 xs \u27e9 = xor xs\n\nrecord Bijective {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n\n field\n linRewiring : LinRewiring funU\n hasBijFork : HasBijFork funU\n hasXor : HasXor funU\n\nrecord Rewiring {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n\n field\n linRewiring : LinRewiring funU\n\n -- Unit (ignoring its argument)\n tt : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `\u22a4\n\n -- Products (all that comes from LinRewiring)\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n\n -- Vectors\n <[]> : \u2200 {_\u22a4 A} \u2192 _\u22a4 `\u2192 `Vec A 0\n -- * <\u2237> and uncons come from LinRewiring\n\n -- Products (group 1 primitive functions or derived from group 2)\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n\n rewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i `\u2192\u1d47 o\n rewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i `\u2192\u1d47 o\n\n open LinRewiring linRewiring public\n\n proj : \u2200 {A} \u2192 Bit \u2192 (A `\u00d7 A) `\u2192 A\n proj true = fst\n proj false = snd\n\n head : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n head = uncons \u204f fst\n\n tail : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n tail = uncons \u204f snd\n\n constVec : \u2200 {n a _\u22a4} {A : Set a} {B} \u2192 (A \u2192 `\u22a4 `\u2192 B) \u2192 Vec A n \u2192 _\u22a4 `\u2192 `Vec B n\n constVec f vec = tt \u204f constVec\u22a4 f vec\n\n take : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A m\n take zero = <[]>\n take (suc m) = < id \u2237 take m >\n\n drop : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A n\n drop zero = id\n drop (suc m) = tail \u204f drop m\n\n msb : \u2200 m {n} \u2192 (m + n) `\u2192\u1d47 m\n msb m = take m\n\n lsb : \u2200 {n} k \u2192 (n + k) `\u2192\u1d47 k\n lsb {n} _ = drop n\n\n init : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n init {zero} = <[]>\n init {suc n} = < id \u2237 init >\n\n last : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n last {zero} = head\n last {suc n} = tail \u204f last\n\n replicate : \u2200 {n A} \u2192 A `\u2192 `Vec A n\n replicate {zero} = <[]>\n replicate {suc n} = < id , replicate > \u204f <\u2237>\n\n constBits\u2032 : \u2200 {n A} \u2192 Bits n \u2192 (A `\u00d7 A) `\u2192 `Vec A n\n constBits\u2032 [] = <[]>\n constBits\u2032 (b \u2237 xs) = dup \u204f < proj b \u2237\u2032 constBits\u2032 xs >\n\nrecord FunOps {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n\n field\n rewiring : Rewiring funU\n hasFork : HasFork funU\n\n -- Bit\n <0b> <1b> : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `Bit\n\n -- Products\n -- * <_\u00d7_>; first; second; swap; assoc; ; snd come from LinRewiring\n -- * dup; <_,_>; fst; snd come from Rewiring\n\n -- Vectors\n -- <[]>; <\u2237>; uncons come from Rewiring\n\n open Defaults\u27e8first-part\u27e9 funU\n open Rewiring rewiring public\n open HasFork hasFork public\n\n -- Bad idea\u2122\n : \u2200 {A B} (b : A `\u2192 `Bit) (f g : A `\u2192 B) \u2192 A `\u2192 B\n = < b , id > \u204f fork if-0 if-1\n\n not : `Bit `\u2192 `Bit\n not = \u204f fork <1b> <0b>\n\n -- We might want it to be part of the interface\n hasXor : HasXor funU\n hasXor = mk (DefaultXor.xor id not <_\u229b>)\n\n hasBijFork : HasBijFork funU\n hasBijFork = mk (DefaultBijForkFromFork.bijFork <_,_> fst fork)\n\n bijective : Bijective funU\n bijective = mk linRewiring hasBijFork hasXor\n\n open HasXor hasXor public\n open HasBijFork hasBijFork public\n\n infixr 3 _&&&_\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n f &&& g = < f , g >\n\n constBit : \u2200 {_\u22a4} \u2192 Bit \u2192 _\u22a4 `\u2192 `Bit\n constBit b = if b then <1b> else <0b>\n\n -- Notice that this one costs 1 unit of space.\n dup\u204f<_\u2237\u2032_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n dup\u204f< f \u2237\u2032 g > = dup \u204f < f \u2237\u2032 g >\n\n <0,_> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Bit `\u00d7 B\n <0, f > = , f >\n\n <1,_> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Bit `\u00d7 B\n <1, f > = , f >\n\n <0,> : \u2200 {A} \u2192 A `\u2192 `Bit `\u00d7 A\n <0,> = <0, id >\n\n <1,> : \u2200 {A} \u2192 A `\u2192 `Bit `\u00d7 A\n <1,> = <1, id >\n\n <0,1> : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `Bit `\u00d7 `Bit\n <0,1> = <0, <1b> >\n\n <0\u2237_> : \u2200 {n A} \u2192 (A `\u2192 `Bits n) \u2192 A `\u2192 `Bits (1 + n)\n <0\u2237 f > = \u2237\u2032 f >\n\n <1\u2237_> : \u2200 {n A} \u2192 (A `\u2192 `Bits n) \u2192 A `\u2192 `Bits (1 + n)\n <1\u2237 f > = \u2237\u2032 f >\n\n <0\u2237> : \u2200 {n} \u2192 n `\u2192\u1d47 (1 + n)\n <0\u2237> = <0\u2237 id >\n\n <1\u2237> : \u2200 {n} \u2192 n `\u2192\u1d47 (1 + n)\n <1\u2237> = <1\u2237 id >\n\n constBits : \u2200 {n _\u22a4} \u2192 Bits n \u2192 _\u22a4 `\u2192 `Bits n\n constBits = constVec constBit\n\n <0\u207f> : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Bits n\n <0\u207f> = constBits 0\u207f\n\n <1\u207f> : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Bits n\n <1\u207f> = constBits 1\u207f\n\n constBits\u2032\u2032 : \u2200 {n _\u22a4} \u2192 Bits n \u2192 _\u22a4 `\u2192 `Bits n\n constBits\u2032\u2032 bs = <0,1> \u204f constBits\u2032 bs\n\n `Maybe : T \u2192 T\n `Maybe A = `Bit `\u00d7 A\n\n : \u2200 {A} \u2192 A `\u2192 `Maybe A\n = <0,>\n\n : \u2200 {A} \u2192 A `\u2192 `Maybe A\n = <1,>\n\n : \u2200 {A B C} \u2192 (f : A `\u00d7 B `\u2192 C) (g : B `\u2192 C) \u2192 `Maybe A `\u00d7 B `\u2192 C\n = \n\n _\u2223?_ : \u2200 {A} \u2192 `Maybe A `\u00d7 `Maybe A `\u2192 `Maybe A\n _\u2223?_ = \u2236 id >\n\n _`\u2192?_ : T \u2192 T \u2192 Set\n A `\u2192? B = A `\u2192 `Maybe B\n\n search : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 (`Bits n `\u2192 A) \u2192 `\u22a4 `\u2192 A\n search {zero} _ f = <[]> \u204f f\n search {suc n} op f = ) , search op (f \u2218 <1\u2237>) > \u204f op\n\n find? : \u2200 {n A} \u2192 (`Bits n `\u2192? A) \u2192 `\u22a4 `\u2192? A\n find? = search _\u2223?_\n\n findB : \u2200 {n} \u2192 (`Bits n `\u2192 `Bit) \u2192 `\u22a4 `\u2192? `Bits n\n findB pred = find? else >\n\n fromTree : \u2200 {n A} \u2192 Tree (`\u22a4 `\u2192 A) n \u2192 `Bits n `\u2192 A\n fromTree (Tree.leaf x) = tt \u204f x\n fromTree (Tree.fork t\u2080 t\u2081) = uncons \u204f fork (fromTree t\u2080) (fromTree t\u2081)\n\n fromFun : \u2200 {n A} \u2192 (Bits n \u2192 `\u22a4 `\u2192 A) \u2192 `Bits n `\u2192 A\n fromFun = fromTree \u2218\u2032 Tree.fromFun\n\n fromBitsFun : \u2200 {i o} \u2192 (i \u2192\u1d47 o) \u2192 i `\u2192\u1d47 o\n fromBitsFun f = fromFun (constBits \u2218\u2032 f)\n\n : `Bit `\u00d7 `Bit `\u2192 `Bit\n = fork id not\n\n : `Bit `\u00d7 `Bit `\u2192 `Bit\n = fork id <1b>\n\n : `Bit `\u00d7 `Bit `\u2192 `Bit\n = fork <0b> id\n\n <==\u1d47> : `Bit `\u00d7 `Bit `\u2192 `Bit\n <==\u1d47> = \u204f not\n\n <==> : \u2200 {n} \u2192 `Bits n `\u00d7 `Bits n `\u2192 `Bit\n <==> {zero} = <1b>\n <==> {suc n} = < uncons \u00d7 uncons > \u204f < <==\u1d47> `zip` <==> {n} > \u204f \n\n <\u2295> : \u2200 {n} \u2192 `Bits n `\u00d7 `Bits n `\u2192 `Bits n\n <\u2295> = zipWith \n\n -- vnot : \u2200 {n} \u2192 `Endo (`Bits n)\n -- vnot = map not\n\n allBits : \u2200 n \u2192 `\u22a4 `\u2192 `Vec (`Bits n) (2^ n)\n allBits zero = < <[]> \u2237[]>\n allBits (suc n) = < bs \u204f map <0\u2237> ++ bs \u204f map <1\u2237> >\n where bs = allBits n\n\n sucBCarry : \u2200 {n} \u2192 `Bits n `\u2192 `Bits (1 + n)\n sucBCarry {zero} = < <0b> \u2237[]>\n sucBCarry {suc n} = uncons\n \u204f fork <0\u2237 sucBCarry >\n (sucBCarry \u204f uncons \u204f fork <0\u2237 <1\u2237> > <1\u2237 <0\u2237> >)\n\n sucB : \u2200 {n} \u2192 `Bits n `\u2192 `Bits n\n sucB = sucBCarry \u204f tail\n\n half-adder : `Bit `\u00d7 `Bit `\u2192 `Bit `\u00d7 `Bit\n half-adder = < , >\n\n full-adder : `Bit `\u00d7 `Bit `\u00d7 `Bit `\u2192 `Bit `\u00d7 `Bit\n full-adder = < (sc\u2082 \u204f fst) , < (sc\u2081 \u204f snd) , (sc\u2082 \u204f snd) > \u204f >\n where a = snd \u204f fst\n b = snd \u204f snd\n c\u1d62\u2099 = fst\n sc\u2081 = < a , b > \u204f half-adder\n sc\u2082 = < (sc\u2081 \u204f fst) , c\u1d62\u2099 > \u204f half-adder\n\n lookupTbl : \u2200 {n A} \u2192 `Bits n `\u00d7 `Vec A (2^ n) `\u2192 A\n lookupTbl {zero} = snd \u204f head\n lookupTbl {suc n}\n = first uncons\n \u204f assoc\n \u204f fork (second (take (2^ n)) \u204f lookupTbl)\n (second (drop (2^ n)) \u204f lookupTbl)\n\n funFromTbl : \u2200 {n A} \u2192 Vec (`\u22a4 `\u2192 A) (2^ n) \u2192 (`Bits n `\u2192 A)\n funFromTbl {zero} (x \u2237 []) = tt \u204f x\n funFromTbl {suc n} tbl\n = uncons \u204f fork (funFromTbl (V.take (2^ n) tbl))\n (funFromTbl (V.drop (2^ n) tbl))\n\n tblFromFun : \u2200 {n A} \u2192 (`Bits n `\u2192 A) \u2192 `\u22a4 `\u2192 `Vec A (2^ n)\n tblFromFun {zero} f = < <[]> \u204f f \u2237[]>\n tblFromFun {suc n} f = < tblFromFun (<0\u2237> \u204f f) ++\n tblFromFun (<1\u2237> \u204f f) >\n\n module WithFin\n (`Fin : \u2115 \u2192 T)\n (fz : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Fin (suc n))\n (fs : \u2200 {n} \u2192 `Fin n `\u2192 `Fin (suc n))\n (elim-Fin0 : \u2200 {A} \u2192 `Fin 0 `\u2192 A)\n (elim-Fin1+ : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Fin n `\u00d7 A `\u2192 B) \u2192 `Fin (suc n) `\u00d7 A `\u2192 B) where\n\n tabulate : \u2200 {n A _B} \u2192 (`Fin n `\u2192 A) \u2192 _B `\u2192 `Vec A n\n tabulate {zero} f = <[]>\n tabulate {suc n} f = \n\n lookup : \u2200 {n A} \u2192 `Fin n `\u00d7 `Vec A n `\u2192 A\n lookup {zero} = fst \u204f elim-Fin0\n lookup {suc n} = elim-Fin1+ head (second tail \u204f lookup)\n\n allFin : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Vec (`Fin n) n\n allFin = tabulate id\n\nmodule Defaults {t} {T : Set t} (funU : FunUniverse T) where\n open FunUniverse funU\n open Defaults\u27e8first-part\u27e9 funU public\n\n module RewiringDefaults\n (linRewiring : LinRewiring funU)\n (tt : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `\u22a4)\n (dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A)\n (rewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i `\u2192\u1d47 o) where\n\n open LinRewiring linRewiring\n open DefaultsGroup1 _\u2218_ tt snd dup swap first public\n <[]> : \u2200 {_\u22a4 A} \u2192 _\u22a4 `\u2192 `Vec A 0\n <[]> = tt \u204f tt\u2192[]\n open DefaultRewireTbl rewire public\n-- -}\n","old_contents":"module FunUniverse.Core where\n\nopen import Type\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2^_)\nimport Data.Bool.NP as B\nopen B using (if_then_else_; true; false)\nopen import Data.Unit using (\u22a4)\nopen import Data.Fin using (Fin)\nopen import Function using (_\u2218\u2032_; flip)\nimport Data.Vec.NP as V\nimport Level as L\nopen V using (Vec; []; _\u2237_)\n\nopen import Data.Bit using (Bit; 0b; 1b)\nopen import Data.Bits using (Bits; _\u2192\u1d47_; RewireTbl; 0\u207f; 1\u207f)\n\nimport FunUniverse.BinTree as Tree\nopen Tree using (Tree)\nopen import FunUniverse.Data\n\nopen import FunUniverse.Types public\nimport FunUniverse.Defaults.FirstPart as Defaults\u27e8first-part\u27e9\nopen import FunUniverse.Rewiring.Linear\n\nrecord HasBijFork {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n field\n -- bijFork f\u2080 f\u2081 (0' , x) = 0' , f\u2080 x\n -- bijFork f\u2080 f\u2081 (1' , x) = 1' , f\u2081 x\n bijFork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 `Bit `\u00d7 B\n\n -- bijFork\u2032 f\u2080 f\u2081 (0' , x) = 0' , f\u2080 0' x\n -- bijFork\u2032 f\u2080 f\u2081 (1' , x) = 1' , f\u2081 1' x\n bijFork\u2032 : \u2200 {A B} \u2192 (Bit \u2192 A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 `Bit `\u00d7 B\n bijFork\u2032 f = bijFork (f 0b) (f 1b)\n\nrecord HasFork {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor _,_\n open FunUniverse funU\n field\n -- cond (0' , x\u2080 , x\u2081) = x\u2080\n -- cond (1' , x\u2080 , x\u2081) = x\u2081\n -- See Defaults.DefaultCond\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n\n -- fork f\u2080 f\u2081 (0' , x) = f\u2080 x\n -- fork f\u2080 f\u2081 (1' , x) = f\u2081 x\n -- See Defaults.DefaultFork\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n\n fork\u2032 : \u2200 {A B} \u2192 (Bit \u2192 A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork\u2032 f = fork (f 0b) (f 1b)\n\nrecord HasXor {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n field\n xor : \u2200 {n} \u2192 Bits n \u2192 `Endo (`Bits n)\n\n vnot : \u2200 {n} \u2192 `Endo (`Bits n)\n vnot = xor 1\u207f\n\n \u27e8\u2295_\u27e9 : \u2200 {n} \u2192 Bits n \u2192 `Endo (`Bits n)\n \u27e8\u2295 xs \u27e9 = xor xs\n\nrecord Bijective {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n\n field\n linRewiring : LinRewiring funU\n hasBijFork : HasBijFork funU\n hasXor : HasXor funU\n\nrecord Rewiring {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n\n field\n linRewiring : LinRewiring funU\n\n -- Unit (ignoring its argument)\n tt : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `\u22a4\n\n -- Products (all that comes from LinRewiring)\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n\n -- Vectors\n <[]> : \u2200 {_\u22a4 A} \u2192 _\u22a4 `\u2192 `Vec A 0\n -- * <\u2237> and uncons come from LinRewiring\n\n -- Products (group 1 primitive functions or derived from group 2)\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n\n rewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i `\u2192\u1d47 o\n rewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i `\u2192\u1d47 o\n\n open LinRewiring linRewiring public\n\n proj : \u2200 {A} \u2192 Bit \u2192 (A `\u00d7 A) `\u2192 A\n proj true = fst\n proj false = snd\n\n head : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n head = uncons \u204f fst\n\n tail : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n tail = uncons \u204f snd\n\n constVec : \u2200 {n a _\u22a4} {A : Set a} {B} \u2192 (A \u2192 `\u22a4 `\u2192 B) \u2192 Vec A n \u2192 _\u22a4 `\u2192 `Vec B n\n constVec f vec = tt \u204f constVec\u22a4 f vec\n\n take : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A m\n take zero = <[]>\n take (suc m) = < id \u2237 take m >\n\n drop : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A n\n drop zero = id\n drop (suc m) = tail \u204f drop m\n\n msb : \u2200 m {n} \u2192 (m + n) `\u2192\u1d47 m\n msb m = take m\n\n lsb : \u2200 {n} k \u2192 (n + k) `\u2192\u1d47 k\n lsb {n} _ = drop n\n\n init : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n init {zero} = <[]>\n init {suc n} = < id \u2237 init >\n\n last : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n last {zero} = head\n last {suc n} = tail \u204f last\n\n replicate : \u2200 {n A} \u2192 A `\u2192 `Vec A n\n replicate {zero} = <[]>\n replicate {suc n} = < id , replicate > \u204f <\u2237>\n\n constBits\u2032 : \u2200 {n A} \u2192 Bits n \u2192 (A `\u00d7 A) `\u2192 `Vec A n\n constBits\u2032 [] = <[]>\n constBits\u2032 (b \u2237 xs) = dup \u204f < proj b \u2237\u2032 constBits\u2032 xs >\n\nrecord FunOps {t} {T : Set t} (funU : FunUniverse T) : Set t where\n constructor mk\n open FunUniverse funU\n\n field\n rewiring : Rewiring funU\n hasFork : HasFork funU\n\n -- Bit\n <0b> <1b> : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `Bit\n\n -- Products\n -- * <_\u00d7_>; first; second; swap; assoc; ; snd come from LinRewiring\n -- * dup; <_,_>; fst; snd come from Rewiring\n\n -- Vectors\n -- <[]>; <\u2237>; uncons come from Rewiring\n\n open Defaults\u27e8first-part\u27e9 funU\n open Rewiring rewiring public\n open HasFork hasFork public\n\n -- Bad idea\u2122\n : \u2200 {A B} (b : A `\u2192 `Bit) (f g : A `\u2192 B) \u2192 A `\u2192 B\n = < b , id > \u204f fork if-0 if-1\n\n not : `Bit `\u2192 `Bit\n not = \u204f fork <0b> <1b>\n\n -- We might want it to be part of the interface\n hasXor : HasXor funU\n hasXor = mk (DefaultXor.xor id not <_\u229b>)\n\n hasBijFork : HasBijFork funU\n hasBijFork = mk (DefaultBijForkFromFork.bijFork <_,_> fst fork)\n\n bijective : Bijective funU\n bijective = mk linRewiring hasBijFork hasXor\n\n open HasXor hasXor public\n open HasBijFork hasBijFork public\n\n infixr 3 _&&&_\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n f &&& g = < f , g >\n\n constBit : \u2200 {_\u22a4} \u2192 Bit \u2192 _\u22a4 `\u2192 `Bit\n constBit b = if b then <1b> else <0b>\n\n -- Notice that this one costs 1 unit of space.\n dup\u204f<_\u2237\u2032_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n dup\u204f< f \u2237\u2032 g > = dup \u204f < f \u2237\u2032 g >\n\n <0,_> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Bit `\u00d7 B\n <0, f > = , f >\n\n <1,_> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Bit `\u00d7 B\n <1, f > = , f >\n\n <0,> : \u2200 {A} \u2192 A `\u2192 `Bit `\u00d7 A\n <0,> = <0, id >\n\n <1,> : \u2200 {A} \u2192 A `\u2192 `Bit `\u00d7 A\n <1,> = <1, id >\n\n <0,1> : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `Bit `\u00d7 `Bit\n <0,1> = <0, <1b> >\n\n <0\u2237_> : \u2200 {n A} \u2192 (A `\u2192 `Bits n) \u2192 A `\u2192 `Bits (1 + n)\n <0\u2237 f > = \u2237\u2032 f >\n\n <1\u2237_> : \u2200 {n A} \u2192 (A `\u2192 `Bits n) \u2192 A `\u2192 `Bits (1 + n)\n <1\u2237 f > = \u2237\u2032 f >\n\n <0\u2237> : \u2200 {n} \u2192 n `\u2192\u1d47 (1 + n)\n <0\u2237> = <0\u2237 id >\n\n <1\u2237> : \u2200 {n} \u2192 n `\u2192\u1d47 (1 + n)\n <1\u2237> = <1\u2237 id >\n\n constBits : \u2200 {n _\u22a4} \u2192 Bits n \u2192 _\u22a4 `\u2192 `Bits n\n constBits = constVec constBit\n\n <0\u207f> : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Bits n\n <0\u207f> = constBits 0\u207f\n\n <1\u207f> : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Bits n\n <1\u207f> = constBits 1\u207f\n\n constBits\u2032\u2032 : \u2200 {n _\u22a4} \u2192 Bits n \u2192 _\u22a4 `\u2192 `Bits n\n constBits\u2032\u2032 bs = <0,1> \u204f constBits\u2032 bs\n\n `Maybe : T \u2192 T\n `Maybe A = `Bit `\u00d7 A\n\n : \u2200 {A} \u2192 A `\u2192 `Maybe A\n = <0,>\n\n : \u2200 {A} \u2192 A `\u2192 `Maybe A\n = <1,>\n\n : \u2200 {A B C} \u2192 (f : A `\u00d7 B `\u2192 C) (g : B `\u2192 C) \u2192 `Maybe A `\u00d7 B `\u2192 C\n = \n\n _\u2223?_ : \u2200 {A} \u2192 `Maybe A `\u00d7 `Maybe A `\u2192 `Maybe A\n _\u2223?_ = \u2236 id >\n\n _`\u2192?_ : T \u2192 T \u2192 Set\n A `\u2192? B = A `\u2192 `Maybe B\n\n search : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 (`Bits n `\u2192 A) \u2192 `\u22a4 `\u2192 A\n search {zero} _ f = <[]> \u204f f\n search {suc n} op f = ) , search op (f \u2218 <1\u2237>) > \u204f op\n\n find? : \u2200 {n A} \u2192 (`Bits n `\u2192? A) \u2192 `\u22a4 `\u2192? A\n find? = search _\u2223?_\n\n findB : \u2200 {n} \u2192 (`Bits n `\u2192 `Bit) \u2192 `\u22a4 `\u2192? `Bits n\n findB pred = find? else >\n\n fromTree : \u2200 {n A} \u2192 Tree (`\u22a4 `\u2192 A) n \u2192 `Bits n `\u2192 A\n fromTree (Tree.leaf x) = tt \u204f x\n fromTree (Tree.fork t\u2080 t\u2081) = uncons \u204f fork (fromTree t\u2080) (fromTree t\u2081)\n\n fromFun : \u2200 {n A} \u2192 (Bits n \u2192 `\u22a4 `\u2192 A) \u2192 `Bits n `\u2192 A\n fromFun = fromTree \u2218\u2032 Tree.fromFun\n\n fromBitsFun : \u2200 {i o} \u2192 (i \u2192\u1d47 o) \u2192 i `\u2192\u1d47 o\n fromBitsFun f = fromFun (constBits \u2218\u2032 f)\n\n : `Bit `\u00d7 `Bit `\u2192 `Bit\n = fork id not\n\n : `Bit `\u00d7 `Bit `\u2192 `Bit\n = fork id <1b>\n\n : `Bit `\u00d7 `Bit `\u2192 `Bit\n = fork <0b> id\n\n <==\u1d47> : `Bit `\u00d7 `Bit `\u2192 `Bit\n <==\u1d47> = \u204f not\n\n <==> : \u2200 {n} \u2192 `Bits n `\u00d7 `Bits n `\u2192 `Bit\n <==> {zero} = <1b>\n <==> {suc n} = < uncons \u00d7 uncons > \u204f < <==\u1d47> `zip` <==> {n} > \u204f \n\n <\u2295> : \u2200 {n} \u2192 `Bits n `\u00d7 `Bits n `\u2192 `Bits n\n <\u2295> = zipWith \n\n -- vnot : \u2200 {n} \u2192 `Endo (`Bits n)\n -- vnot = map not\n\n allBits : \u2200 n \u2192 `\u22a4 `\u2192 `Vec (`Bits n) (2^ n)\n allBits zero = < <[]> \u2237[]>\n allBits (suc n) = < bs \u204f map <0\u2237> ++ bs \u204f map <1\u2237> >\n where bs = allBits n\n\n sucBCarry : \u2200 {n} \u2192 `Bits n `\u2192 `Bits (1 + n)\n sucBCarry {zero} = < <0b> \u2237[]>\n sucBCarry {suc n} = uncons\n \u204f fork <0\u2237 sucBCarry >\n (sucBCarry \u204f uncons \u204f fork <0\u2237 <1\u2237> > <1\u2237 <0\u2237> >)\n\n sucB : \u2200 {n} \u2192 `Bits n `\u2192 `Bits n\n sucB = sucBCarry \u204f tail\n\n half-adder : `Bit `\u00d7 `Bit `\u2192 `Bit `\u00d7 `Bit\n half-adder = < , >\n\n full-adder : `Bit `\u00d7 `Bit `\u00d7 `Bit `\u2192 `Bit `\u00d7 `Bit\n full-adder = < (sc\u2082 \u204f fst) , < (sc\u2081 \u204f snd) , (sc\u2082 \u204f snd) > \u204f >\n where a = snd \u204f fst\n b = snd \u204f snd\n c\u1d62\u2099 = fst\n sc\u2081 = < a , b > \u204f half-adder\n sc\u2082 = < (sc\u2081 \u204f fst) , c\u1d62\u2099 > \u204f half-adder\n\n lookupTbl : \u2200 {n A} \u2192 `Bits n `\u00d7 `Vec A (2^ n) `\u2192 A\n lookupTbl {zero} = snd \u204f head\n lookupTbl {suc n}\n = first uncons\n \u204f assoc\n \u204f fork (second (take (2^ n)) \u204f lookupTbl)\n (second (drop (2^ n)) \u204f lookupTbl)\n\n funFromTbl : \u2200 {n A} \u2192 Vec (`\u22a4 `\u2192 A) (2^ n) \u2192 (`Bits n `\u2192 A)\n funFromTbl {zero} (x \u2237 []) = tt \u204f x\n funFromTbl {suc n} tbl\n = uncons \u204f fork (funFromTbl (V.take (2^ n) tbl))\n (funFromTbl (V.drop (2^ n) tbl))\n\n tblFromFun : \u2200 {n A} \u2192 (`Bits n `\u2192 A) \u2192 `\u22a4 `\u2192 `Vec A (2^ n)\n tblFromFun {zero} f = < <[]> \u204f f \u2237[]>\n tblFromFun {suc n} f = < tblFromFun (<0\u2237> \u204f f) ++\n tblFromFun (<1\u2237> \u204f f) >\n\n module WithFin\n (`Fin : \u2115 \u2192 T)\n (fz : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Fin (suc n))\n (fs : \u2200 {n} \u2192 `Fin n `\u2192 `Fin (suc n))\n (elim-Fin0 : \u2200 {A} \u2192 `Fin 0 `\u2192 A)\n (elim-Fin1+ : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Fin n `\u00d7 A `\u2192 B) \u2192 `Fin (suc n) `\u00d7 A `\u2192 B) where\n\n tabulate : \u2200 {n A _B} \u2192 (`Fin n `\u2192 A) \u2192 _B `\u2192 `Vec A n\n tabulate {zero} f = <[]>\n tabulate {suc n} f = \n\n lookup : \u2200 {n A} \u2192 `Fin n `\u00d7 `Vec A n `\u2192 A\n lookup {zero} = fst \u204f elim-Fin0\n lookup {suc n} = elim-Fin1+ head (second tail \u204f lookup)\n\n allFin : \u2200 {n _\u22a4} \u2192 _\u22a4 `\u2192 `Vec (`Fin n) n\n allFin = tabulate id\n\nmodule Defaults {t} {T : Set t} (funU : FunUniverse T) where\n open FunUniverse funU\n open Defaults\u27e8first-part\u27e9 funU public\n\n module RewiringDefaults\n (linRewiring : LinRewiring funU)\n (tt : \u2200 {_\u22a4} \u2192 _\u22a4 `\u2192 `\u22a4)\n (dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A)\n (rewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i `\u2192\u1d47 o) where\n\n open LinRewiring linRewiring\n open DefaultsGroup1 _\u2218_ tt snd dup swap first public\n <[]> : \u2200 {_\u22a4 A} \u2192 _\u22a4 `\u2192 `Vec A 0\n <[]> = tt \u204f tt\u2192[]\n open DefaultRewireTbl rewire public\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"3682d42d35e0734a6a4d6a4ee62cbad3965309b9","subject":"Make _\u2295_ more coherent with _\u229d_","message":"Make _\u2295_ more coherent with _\u229d_\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Term.agda","new_file":"Parametric\/Change\/Term.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Terms that operate on changes (Fig. 3).\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Const : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\n-- Extension point 1: A term for \u229d on base types.\nDiffStructure : Set\nDiffStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))\n\n-- Extension point 2: A term for \u2295 on base types.\nApplyStructure : Set\nApplyStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\n\nmodule Structure\n (diff-base : DiffStructure)\n (apply-base : ApplyStructure)\n where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n -- We provide: terms for \u2295 and \u229d on arbitrary types.\n diff-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n apply-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n diff-term {base \u03b9} = diff-base\n diff-term {\u03c3 \u21d2 \u03c4} =\n (let\n _\u229d\u03c4_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff-term {\u03c4} {\u0393}) s t\n _\u2295\u03c3_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply-term {\u03c3} {\u0393}) \u0394t t\n in\n absV 4 (\u03bb g f x \u0394x \u2192 app g (x \u2295\u03c3 \u0394x) \u229d\u03c4 app f x))\n\n apply-term {base \u03b9} = apply-base\n apply-term {\u03c3 \u21d2 \u03c4} =\n (let\n _\u229d\u03c3_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff-term {\u03c3} {\u0393}) s t\n _\u2295\u03c4_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply-term {\u03c4} {\u0393}) \u0394t t\n in\n absV 3 (\u03bb \u0394h h y \u2192 app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y)))\n\n diff : \u2200 \u03c4 {\u0393} \u2192\n Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 (\u0394Type \u03c4)\n diff _ = app\u2082 diff-term\n\n apply : \u2200 \u03c4 {\u0393} \u2192\n Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\n apply _ = app\u2082 apply-term\n\n infixl 6 apply diff\n syntax apply \u03c4 x \u0394x = \u0394x \u2295\u208d \u03c4 \u208e x\n syntax diff \u03c4 x y = x \u229d\u208d \u03c4 \u208e y\n\n infixl 6 _\u2295_ _\u229d_\n _\u229d_ : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 (\u0394Type \u03c4)\n _\u229d_ {\u03c4} = diff \u03c4\n\n _\u2295_ : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\n _\u2295_ {\u03c4} = apply \u03c4\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Terms that operate on changes (Fig. 3).\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Const : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\n-- Extension point 1: A term for \u229d on base types.\nDiffStructure : Set\nDiffStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))\n\n-- Extension point 2: A term for \u2295 on base types.\nApplyStructure : Set\nApplyStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\n\nmodule Structure\n (diff-base : DiffStructure)\n (apply-base : ApplyStructure)\n where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n -- We provide: terms for \u2295 and \u229d on arbitrary types.\n diff-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n apply-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n diff-term {base \u03b9} = diff-base\n diff-term {\u03c3 \u21d2 \u03c4} =\n (let\n _\u229d\u03c4_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff-term {\u03c4} {\u0393}) s t\n _\u2295\u03c3_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply-term {\u03c3} {\u0393}) \u0394t t\n in\n absV 4 (\u03bb g f x \u0394x \u2192 app g (x \u2295\u03c3 \u0394x) \u229d\u03c4 app f x))\n\n apply-term {base \u03b9} = apply-base\n apply-term {\u03c3 \u21d2 \u03c4} =\n (let\n _\u229d\u03c3_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff-term {\u03c3} {\u0393}) s t\n _\u2295\u03c4_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply-term {\u03c4} {\u0393}) \u0394t t\n in\n absV 3 (\u03bb \u0394h h y \u2192 app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y)))\n\n diff : \u2200 \u03c4 {\u0393} \u2192\n Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 (\u0394Type \u03c4)\n diff _ = app\u2082 diff-term\n\n apply : \u2200 \u03c4 {\u0393} \u2192\n Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\n apply _ = app\u2082 apply-term\n\n infixl 6 apply diff\n syntax apply \u03c4 x \u0394x = \u0394x \u2295\u208d \u03c4 \u208e x\n syntax diff \u03c4 x y = x \u229d\u208d \u03c4 \u208e y\n\n infixl 6 _\u2295_ _\u229d_\n _\u229d_ : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 (\u0394Type \u03c4)\n _\u229d_ {\u03c4} = diff \u03c4\n\n _\u2295_ : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\n _\u2295_ {\u03c4} = app\u2082 apply-term\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ef4dba4a6f0ef00966897ba95d15854fd8cc756c","subject":"Fixed doc.","message":"Fixed doc.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Data\/Conat\/Type.agda","new_file":"src\/fot\/FOTC\/Data\/Conat\/Type.agda","new_contents":"------------------------------------------------------------------------------\n-- The FOTC co-inductive natural numbers type\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- N.B. This module is re-exported by FOTC.Data.Conat.\n\n-- Adapted from (Sander 1992).\n\n-- References:\n--\n-- \u2022 Herbert P. Sander. A logic of functional programs with an\n-- application to concurrency. PhD thesis, Chalmers University of\n-- Technology and University of Gothenburg, Department of Computer\n-- Sciences, 1992.\n\nmodule FOTC.Data.Conat.Type where\n\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n-- The FOTC co-inductive natural numbers type (co-inductive predicate\n-- for total co-inductive natural)\n\n-- Functional for the NatF predicate.\n-- NatF : (D \u2192 Set) \u2192 D \u2192 Set\n-- NatF P n = n = zero \u2228 (\u2203[ n' ] P n' \u2227 n = succ n')\n\n-- Conat is the greatest fixed-point of NatF (by Conat-unf and\n-- Conat-coind).\n\npostulate Conat : D \u2192 Set\n\n-- Conat is a post-fixed point of NatF, i.e.\n--\n-- Conat \u2264 NatF Conat.\npostulate\n Conat-unf : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] Conat n' \u2227 n \u2261 succ\u2081 n')\n{-# ATP axiom Conat-unf #-}\n\n-- Conat is the greatest post-fixed point of NatF, i.e\n--\n-- \u2200 P. P \u2264 NatF P \u21d2 P \u2264 Conat.\n--\n-- N.B. This is an axiom schema. Because in the automatic proofs we\n-- *must* use an instance, we do not add this postulate as an ATP\n-- axiom.\npostulate\n Conat-coind : \u2200 (A : D \u2192 Set) {n} \u2192\n -- A is post-fixed point of NatF.\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] A n' \u2227 n \u2261 succ\u2081 n')) \u2192\n -- Conat is greater than A.\n A n \u2192 Conat n\n\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Conat predicate is also a pre-fixed point of the functional NatF,\n-- i.e,\n--\n-- NatF Conat \u2264 Conat.\nConat-pre-fixed : \u2200 {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] Conat n' \u2227 n \u2261 succ\u2081 n')) \u2192\n Conat n\nConat-pre-fixed {n} h = Conat-coind A prf h\n where\n A : D \u2192 Set\n A m = m \u2261 zero \u2228 (\u2203[ m' ] Conat m' \u2227 m \u2261 succ\u2081 m')\n\n prf : A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] A n' \u2227 n \u2261 succ\u2081 n')\n prf (inj\u2081 n\u22610) = inj\u2081 n\u22610\n prf (inj\u2082 (n' , CNn' , n\u2261Sn')) = inj\u2082 (n' , Conat-unf CNn' , n\u2261Sn')\n","old_contents":"------------------------------------------------------------------------------\n-- The FOTC co-inductive natural numbers type\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- N.B. This module is re-exported by FOTC.Data.Conat.\n\n-- Adapted from (Sander 1992).\n\n-- References:\n--\n-- \u2022 Herbert P. Sander. A logic of functional programs with an\n-- application to concurrency. PhD thesis, Chalmers University of\n-- Technology and University of Gothenburg, Department of Computer\n-- Sciences, 1992.\n\nmodule FOTC.Data.Conat.Type where\n\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n-- The FOTC co-inductive natural numbers type (co-inductive predicate\n-- for total co-inductive natural)\n\n-- Functional for the ConatF predicate.\n-- ConatF : (D \u2192 Set) \u2192 D \u2192 Set\n-- ConatF P n = n = zero \u2228 (\u2203[ n' ] P n' \u2227 n = succ n')\n\n-- Conat is the greatest fixed-point of ConatF (by Conat-unf and\n-- Conat-coind).\n\npostulate Conat : D \u2192 Set\n\n-- Conat is a post-fixed point of ConatF, i.e.\n--\n-- Conat \u2264 ConatF Stream.\npostulate\n Conat-unf : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] Conat n' \u2227 n \u2261 succ\u2081 n')\n{-# ATP axiom Conat-unf #-}\n\n-- Conat is the greatest post-fixed point of ConatF, i.e\n--\n-- \u2200 P. P \u2264 ConatF P \u21d2 P \u2264 Conat.\n--\n-- N.B. This is an axiom schema. Because in the automatic proofs we\n-- *must* use an instance, we do not add this postulate as an ATP\n-- axiom.\npostulate\n Conat-coind : \u2200 (A : D \u2192 Set) {n} \u2192\n -- A is post-fixed point of ConatF.\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] A n' \u2227 n \u2261 succ\u2081 n')) \u2192\n -- Conat is greater than A.\n A n \u2192 Conat n\n\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Conat predicate is also a pre-fixed point of the functional ConatF,\n-- i.e,\n--\n-- ConatF Conat \u2264 Conat.\nConat-pre-fixed : \u2200 {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] Conat n' \u2227 n \u2261 succ\u2081 n')) \u2192\n Conat n\nConat-pre-fixed {n} h = Conat-coind A prf h\n where\n A : D \u2192 Set\n A m = m \u2261 zero \u2228 (\u2203[ m' ] Conat m' \u2227 m \u2261 succ\u2081 m')\n\n prf : A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] A n' \u2227 n \u2261 succ\u2081 n')\n prf (inj\u2081 n\u22610) = inj\u2081 n\u22610\n prf (inj\u2082 (n' , CNn' , n\u2261Sn')) = inj\u2082 (n' , Conat-unf CNn' , n\u2261Sn')\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"de127be14bbacaa2cfe0253a1a3caa890e694607","subject":"Add comment","message":"Add comment\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/Examples\/PropositionalDesc.agda","new_file":"formalization\/agda\/Spire\/Examples\/PropositionalDesc.agda","new_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nmodule Spire.Examples.PropositionalDesc where\n\n----------------------------------------------------------------------\n\nelimEq : (A : Set) (x : A) (P : (y : A) \u2192 x \u2261 y \u2192 Set)\n \u2192 P x refl\n \u2192 (y : A) (p : x \u2261 y) \u2192 P y p\nelimEq A .x P prefl x refl = prefl\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nCases : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nCases [] P = \u22a4\nCases (l \u2237 E) P = P here \u00d7 Cases E \u03bb t \u2192 P (there t)\n\ncase : (E : Enum) (P : Tag E \u2192 Set) (cs : Cases E P) (t : Tag E) \u2192 P t\ncase (l \u2237 E) P (c , cs) here = c\ncase (l \u2237 E) P (c , cs) (there t) = case E (\u03bb t \u2192 P (there t)) cs t\n\nUncurriedCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedCases E P X = Cases E P \u2192 X\n\nCurriedCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedCases [] P X = X\nCurriedCases (l \u2237 E) P X = P here \u2192 CurriedCases E (\u03bb t \u2192 P (there t)) X\n\ncurryCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n (f : UncurriedCases E P X) \u2192 CurriedCases E P X\ncurryCases [] P X f = f tt\ncurryCases (l \u2237 E) P X f = \u03bb c \u2192 curryCases E (\u03bb t \u2192 P (there t)) X (\u03bb cs \u2192 f (c , cs))\n\nuncurryCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n (f : CurriedCases E P X) \u2192 UncurriedCases E P X\nuncurryCases [] P X x tt = x\nuncurryCases (l \u2237 E) P X f (c , cs) = uncurryCases E (\u03bb t \u2192 P (there t)) X (f c) cs\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n `End : (i : I) \u2192 Desc I\n `Rec : (i : I) (D : Desc I) \u2192 Desc I\n `Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n `RecFun : (A : Set) (B : A \u2192 I) (D : Desc I) \u2192 Desc I\n\nISet : Set \u2192 Set\u2081\nISet I = I \u2192 Set\n\nEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 ISet I\nEl I (`End j) X i = j \u2261 i\nEl I (`Rec j D) X i = X j \u00d7 El I D X i\nEl I (`Arg A B) X i = \u03a3 A (\u03bb a \u2192 El I (B a) X i)\nEl I (`RecFun A B D) X i = ((a : A) \u2192 X (B a)) \u00d7 El I D X i\n\nAll : (I : Set) (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El I D X i) \u2192 Set\nAll I (`End j) X P i q = \u22a4\nAll I (`Rec j D) X P i (x , xs) = P j x \u00d7 All I D X P i xs\nAll I (`Arg A B) X P i (a , b) = All I (B a) X P i b\nAll I (`RecFun A B D) X P i (f , xs) = ((a : A) \u2192 P (B a) (f a)) \u00d7 All I D X P i xs\n\ncaseD : (E : Enum) (I : Set) (cs : Cases E (\u03bb _ \u2192 Desc I)) (t : Tag E) \u2192 Desc I\ncaseD E I cs t = case E (\u03bb _ \u2192 Desc I) cs t\n\n----------------------------------------------------------------------\n\nUncurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl I D X = {i : I} \u2192 El I D X i \u2192 X i\n\nCurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl I (`End i) X = X i\nCurriedEl I (`Rec j D) X = (x : X j) \u2192 CurriedEl I D X\nCurriedEl I (`Arg A B) X = (a : A) \u2192 CurriedEl I (B a) X\nCurriedEl I (`RecFun A B D) X = ((a : A) \u2192 X (B a)) \u2192 CurriedEl I D X\n\ncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : UncurriedEl I D X) \u2192 CurriedEl I D X\ncurryEl I (`End i) X cn = cn refl\ncurryEl I (`Rec i D) X cn = \u03bb x \u2192 curryEl I D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl I (`Arg A B) X cn = \u03bb a \u2192 curryEl I (B a) X (\u03bb xs \u2192 cn (a , xs))\ncurryEl I (`RecFun A B D) X cn = \u03bb f \u2192 curryEl I D X (\u03bb xs \u2192 cn (f , xs))\n\nuncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : CurriedEl I D X) \u2192 UncurriedEl I D X\nuncurryEl I (`End i) X cn refl = cn\nuncurryEl I (`Rec i D) X cn (x , xs) = uncurryEl I D X (cn x) xs\nuncurryEl I (`Arg A B) X cn (a , xs) = uncurryEl I (B a) X (cn a) xs\nuncurryEl I (`RecFun A B D) X cn (f , xs) = uncurryEl I D X (cn f) xs\n\ndata \u03bc (I : Set) (D : Desc I) (i : I) : Set where\n -- this equals UncurriedEl I D (\u03bc I D)\n con : El I D (\u03bc I D) i \u2192 \u03bc I D i\n\ncon2 : (I : Set) (D : Desc I) \u2192 CurriedEl I D (\u03bc I D)\ncon2 I D = curryEl I D (\u03bc I D) con\n\n----------------------------------------------------------------------\n\nUncurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : {i : I} \u2192 El I D X i \u2192 X i)\n \u2192 Set\nUncurriedAll I D X P cn =\n (i : I) (xs : El I D X i) \u2192 All I D X P i xs \u2192 P i (cn xs)\n\nCurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n \u2192 Set\nCurriedAll I (`End i) X P cn =\n P i (cn refl)\nCurriedAll I (`Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedAll I (`Arg A B) X P cn =\n (a : A) \u2192 CurriedAll I (B a) X P (\u03bb xs \u2192 cn (a , xs))\nCurriedAll I (`RecFun A B D) X P cn =\n (f : (a : A) \u2192 X (B a)) (ihf : (a : A) \u2192 P (B a) (f a)) \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (f , xs))\n\ncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : UncurriedAll I D X P cn)\n \u2192 CurriedAll I D X P cn\ncurryAll I (`End i) X P cn pf =\n pf i refl tt\ncurryAll I (`Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryAll I D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryAll I (`Arg A B) X P cn pf =\n \u03bb a \u2192 curryAll I (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\ncurryAll I (`RecFun A B D) X P cn pf =\n \u03bb f ihf \u2192 curryAll I D X P (\u03bb xs \u2192 cn (f , xs)) (\u03bb i xs ihs \u2192 pf i (f , xs) (ihf , ihs))\n\nuncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : CurriedAll I D X P cn)\n \u2192 UncurriedAll I D X P cn\nuncurryAll I (`End .i) X P cn pf i refl tt =\n pf\nuncurryAll I (`Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryAll I (`Arg A B) X P cn pf i (a , xs) ihs =\n uncurryAll I (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\nuncurryAll I (`RecFun A B D) X P cn pf i (f , xs) (ihf , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (f , ys)) (pf f ihf) i xs ihs\n\n----------------------------------------------------------------------\n\nind :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : UncurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\n\nhyps :\n (I : Set)\n (D\u2081 : Desc I)\n (P : (i : I) \u2192 \u03bc I D\u2081 i \u2192 Set)\n (pcon : UncurriedAll I D\u2081 (\u03bc I D\u2081) P con)\n (D\u2082 : Desc I)\n (i : I)\n (xs : El I D\u2082 (\u03bc I D\u2081) i)\n \u2192 All I D\u2082 (\u03bc I D\u2081) P i xs\n\nind I D P pcon i (con xs) = pcon i xs (hyps I D P pcon D i xs)\n\nhyps I D P pcon (`End j) i q = tt\nhyps I D P pcon (`Rec j A) i (x , xs) = ind I D P pcon j x , hyps I D P pcon A i xs\nhyps I D P pcon (`Arg A B) i (a , b) = hyps I D P pcon (B a) i b\nhyps I D P pcon (`RecFun A B E) i (f , xs) = (\u03bb a \u2192 ind I D P pcon (B a) (f a)) , hyps I D P pcon E i xs\n\n----------------------------------------------------------------------\n\nind2 :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : CurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\nind2 I D P pcon i x = ind I D P (uncurryAll I D (\u03bc I D) P con pcon) i x\n\nelim :\n (I : Set)\n (E : Enum)\n (Cs : Tag E \u2192 Desc I)\n \u2192 let D = `Arg (Tag E) Cs in\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n \u2192 UncurriedCases E\n (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))) \n ((i : I) (x : \u03bc I D i) \u2192 P i x)\nelim I E Cs P cs i x =\n let D = `Arg (Tag E) Cs in\n ind2 I D P (case E (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))) cs) i x\n\nelim2 :\n (I : Set)\n (E : Enum)\n (Cs : Tag E \u2192 Desc I)\n \u2192 let D = `Arg (Tag E) Cs in\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n \u2192 let Q = (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs)))\n X = ((i : I) (x : \u03bc I D i) \u2192 P i x)\n in CurriedCases E Q X\nelim2 I E Cs P =\n let D = `Arg (Tag E) Cs\n Q = (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs)))\n X = ((i : I) (x : \u03bc I D i) \u2192 P i x)\n in curryCases E Q X (elim I E Cs P)\n\n----------------------------------------------------------------------\n\nmodule Sugared where\n\n data \u2115T : Set where `zero `suc : \u2115T\n data VecT : Set where `nil `cons : VecT\n\n \u2115D : Desc \u22a4\n \u2115D = `Arg \u2115T \u03bb\n { `zero \u2192 `End tt\n ; `suc \u2192 `Rec tt (`End tt)\n }\n\n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n\n zero : \u2115 tt\n zero = con (`zero , refl)\n\n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (`suc , n , refl)\n\n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg VecT \u03bb\n { `nil \u2192 `End zero\n ; `cons \u2192 `Arg (\u2115 tt) \u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n))\n }\n\n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n\n nil : (A : Set) \u2192 Vec A zero\n nil A = con (`nil , refl)\n\n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (`cons , n , x , xs , refl)\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 n\n ; tt (`suc , m , q) (ih , tt) n \u2192 suc (ih n)\n }\n )\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 zero\n ; tt (`suc , m , q) (ih , tt) n \u2192 add n (ih n)\n }\n )\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) ih n ys \u2192 ys\n ; .(con (`suc , m , refl)) (`cons , m , x , xs , refl) (ih , tt) n ys \u2192 cons A (add m n) x (ih n ys)\n }\n )\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) tt \u2192 nil A\n ; .(con (`suc , n , refl)) (`cons , n , xs , xss , refl) (ih , tt) \u2192 append A m xs (mult n m) ih\n }\n )\n\n----------------------------------------------------------------------\n\nmodule Desugared where\n\n \u2115T : Enum\n \u2115T = \"zero\" \u2237 \"suc\" \u2237 []\n \n VecT : Enum\n VecT = \"nil\" \u2237 \"cons\" \u2237 []\n \n \u2115C : Tag \u2115T \u2192 Desc \u22a4\n \u2115C = caseD \u2115T \u22a4\n ( `End tt\n , `Rec tt (`End tt)\n , tt\n )\n \n \u2115D : Desc \u22a4\n \u2115D = `Arg (Tag \u2115T) \u2115C\n \n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n \n zero : \u2115 tt\n zero = con (here , refl)\n \n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (there here , n , refl)\n \n zero2 : \u2115 tt\n zero2 = con2 \u22a4 \u2115D here\n\n suc2 : \u2115 tt \u2192 \u2115 tt\n suc2 = con2 \u22a4 \u2115D (there here)\n\n VecC : (A : Set) \u2192 Tag VecT \u2192 Desc (\u2115 tt)\n VecC A = caseD VecT (\u2115 tt)\n ( `End zero\n , `Arg (\u2115 tt) (\u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n)))\n , tt\n )\n \n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg (Tag VecT) (VecC A)\n \n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n \n nil : (A : Set) \u2192 Vec A zero\n nil A = con (here , refl)\n \n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (there here , n , x , xs , refl)\n \n nil2 : (A : Set) \u2192 Vec A zero\n nil2 A = con2 (\u2115 tt) (VecD A) here\n\n cons2 : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons2 A = con2 (\u2115 tt) (VecD A) (there here)\n\n----------------------------------------------------------------------\n\n module Induction where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 n)\n , (\u03bb m,q ih,tt n \u2192 suc (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 zero)\n , (\u03bb m,q ih,tt n \u2192 add n (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb m t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) m)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)) m (t , c))\n (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n )\n ( (\u03bb q ih n ys \u2192 subst (\u03bb m \u2192 Vec A (add m n)) q ys)\n , (\u03bb m',x,xs,q ih,tt n ys \u2192\n let m' = proj\u2081 m',x,xs,q\n x = proj\u2081 (proj\u2082 m',x,xs,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 m',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb m \u2192 Vec A (add m n)) q (cons A (add m' n) x (ih n ys))\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC (Vec A m) t) (Vec (Vec A m)) n)\n (ih : All (\u2115 tt) (VecD (Vec A m)) (Vec (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m)) n (t , c))\n \u2192 Vec A (mult n m)\n )\n ( (\u03bb q ih \u2192 subst (\u03bb n \u2192 Vec A (mult n m)) q (nil A))\n , (\u03bb n',xs,xss,q ih,tt \u2192\n let n' = proj\u2081 n',xs,xss,q\n xs = proj\u2081 (proj\u2082 n',xs,xss,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 n',xs,xss,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb n \u2192 Vec A (mult n m)) q (append A m xs (mult n' m) ih)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n module Eliminator where\n\n elim\u2115 : (P : (\u2115 tt) \u2192 Set)\n (pzero : P zero)\n (psuc : (m : \u2115 tt) \u2192 P m \u2192 P (suc m))\n (n : \u2115 tt)\n \u2192 P n\n elim\u2115 P pzero psuc = ind \u22a4 \u2115D (\u03bb u n \u2192 P n)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 P n) u (t , c))\n \u2192 P (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (here , q)))\n pzero\n u q\n )\n , (\u03bb n,q ih,tt \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (there here , proj\u2081 n,q , q)))\n (psuc (proj\u2081 n,q) (proj\u2081 ih,tt))\n u (proj\u2082 n,q)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n\n elimVec : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set)\n (pnil : P zero (nil A))\n (pcons : (n : \u2115 tt) (a : A) (xs : Vec A n) \u2192 P n xs \u2192 P (suc n) (cons A n a xs))\n (n : \u2115 tt)\n (xs : Vec A n)\n \u2192 P n xs\n elimVec A P pnil pcons = ind (\u2115 tt) (VecD A) (\u03bb n xs \u2192 P n xs)\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) n)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb n xs \u2192 P n xs) n (t , c))\n \u2192 P n (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq (\u2115 tt) zero (\u03bb n q \u2192 P n (con (here , q)))\n pnil\n n q\n )\n , (\u03bb n',x,xs,q ih,tt \u2192\n let n' = proj\u2081 n',x,xs,q\n x = proj\u2081 (proj\u2082 n',x,xs,q)\n xs = proj\u2081 (proj\u2082 (proj\u2082 n',x,xs,q))\n q = proj\u2082 (proj\u2082 (proj\u2082 n',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n elimEq (\u2115 tt) (suc n') (\u03bb n q \u2192 P n (con (there here , n' , x , xs , q)))\n (pcons n' x xs ih )\n n q\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elimVec A (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elimVec (Vec A m) (\u03bb n xss \u2192 Vec A (mult n m))\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n\n module GenericEliminator where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim2 \u22a4 \u2115T \u2115C _\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim2 \u22a4 \u2115T \u2115C _\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elim2 (\u2115 tt) VecT (VecC A) _\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elim2 (\u2115 tt) VecT (VecC (Vec A m)) _\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n","old_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nmodule Spire.Examples.PropositionalDesc where\n\n----------------------------------------------------------------------\n\nelimEq : (A : Set) (x : A) (P : (y : A) \u2192 x \u2261 y \u2192 Set)\n \u2192 P x refl\n \u2192 (y : A) (p : x \u2261 y) \u2192 P y p\nelimEq A .x P prefl x refl = prefl\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nCases : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nCases [] P = \u22a4\nCases (l \u2237 E) P = P here \u00d7 Cases E \u03bb t \u2192 P (there t)\n\ncase : (E : Enum) (P : Tag E \u2192 Set) (cs : Cases E P) (t : Tag E) \u2192 P t\ncase (l \u2237 E) P (c , cs) here = c\ncase (l \u2237 E) P (c , cs) (there t) = case E (\u03bb t \u2192 P (there t)) cs t\n\nUncurriedCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedCases E P X = Cases E P \u2192 X\n\nCurriedCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedCases [] P X = X\nCurriedCases (l \u2237 E) P X = P here \u2192 CurriedCases E (\u03bb t \u2192 P (there t)) X\n\ncurryCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n (f : UncurriedCases E P X) \u2192 CurriedCases E P X\ncurryCases [] P X f = f tt\ncurryCases (l \u2237 E) P X f = \u03bb c \u2192 curryCases E (\u03bb t \u2192 P (there t)) X (\u03bb cs \u2192 f (c , cs))\n\nuncurryCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n (f : CurriedCases E P X) \u2192 UncurriedCases E P X\nuncurryCases [] P X x tt = x\nuncurryCases (l \u2237 E) P X f (c , cs) = uncurryCases E (\u03bb t \u2192 P (there t)) X (f c) cs\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n `End : (i : I) \u2192 Desc I\n `Rec : (i : I) (D : Desc I) \u2192 Desc I\n `Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n `RecFun : (A : Set) (B : A \u2192 I) (D : Desc I) \u2192 Desc I\n\nISet : Set \u2192 Set\u2081\nISet I = I \u2192 Set\n\nEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 ISet I\nEl I (`End j) X i = j \u2261 i\nEl I (`Rec j D) X i = X j \u00d7 El I D X i\nEl I (`Arg A B) X i = \u03a3 A (\u03bb a \u2192 El I (B a) X i)\nEl I (`RecFun A B D) X i = ((a : A) \u2192 X (B a)) \u00d7 El I D X i\n\ndata \u03bc (I : Set) (D : Desc I) (i : I) : Set where\n con : El I D (\u03bc I D) i \u2192 \u03bc I D i\n\nAll : (I : Set) (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El I D X i) \u2192 Set\nAll I (`End j) X P i q = \u22a4\nAll I (`Rec j D) X P i (x , xs) = P j x \u00d7 All I D X P i xs\nAll I (`Arg A B) X P i (a , b) = All I (B a) X P i b\nAll I (`RecFun A B D) X P i (f , xs) = ((a : A) \u2192 P (B a) (f a)) \u00d7 All I D X P i xs\n\ncaseD : (E : Enum) (I : Set) (cs : Cases E (\u03bb _ \u2192 Desc I)) (t : Tag E) \u2192 Desc I\ncaseD E I cs t = case E (\u03bb _ \u2192 Desc I) cs t\n\n----------------------------------------------------------------------\n\nUncurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl I D X = {i : I} \u2192 El I D X i \u2192 X i\n\nCurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl I (`End i) X = X i\nCurriedEl I (`Rec j D) X = (x : X j) \u2192 CurriedEl I D X\nCurriedEl I (`Arg A B) X = (a : A) \u2192 CurriedEl I (B a) X\nCurriedEl I (`RecFun A B D) X = ((a : A) \u2192 X (B a)) \u2192 CurriedEl I D X\n\ncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : UncurriedEl I D X) \u2192 CurriedEl I D X\ncurryEl I (`End i) X cn = cn refl\ncurryEl I (`Rec i D) X cn = \u03bb x \u2192 curryEl I D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl I (`Arg A B) X cn = \u03bb a \u2192 curryEl I (B a) X (\u03bb xs \u2192 cn (a , xs))\ncurryEl I (`RecFun A B D) X cn = \u03bb f \u2192 curryEl I D X (\u03bb xs \u2192 cn (f , xs))\n\nuncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : CurriedEl I D X) \u2192 UncurriedEl I D X\nuncurryEl I (`End i) X cn refl = cn\nuncurryEl I (`Rec i D) X cn (x , xs) = uncurryEl I D X (cn x) xs\nuncurryEl I (`Arg A B) X cn (a , xs) = uncurryEl I (B a) X (cn a) xs\nuncurryEl I (`RecFun A B D) X cn (f , xs) = uncurryEl I D X (cn f) xs\n\ncon2 : (I : Set) (D : Desc I) \u2192 CurriedEl I D (\u03bc I D)\ncon2 I D = curryEl I D (\u03bc I D) con\n\n----------------------------------------------------------------------\n\nUncurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : {i : I} \u2192 El I D X i \u2192 X i)\n \u2192 Set\nUncurriedAll I D X P cn =\n (i : I) (xs : El I D X i) \u2192 All I D X P i xs \u2192 P i (cn xs)\n\nCurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n \u2192 Set\nCurriedAll I (`End i) X P cn =\n P i (cn refl)\nCurriedAll I (`Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedAll I (`Arg A B) X P cn =\n (a : A) \u2192 CurriedAll I (B a) X P (\u03bb xs \u2192 cn (a , xs))\nCurriedAll I (`RecFun A B D) X P cn =\n (f : (a : A) \u2192 X (B a)) (ihf : (a : A) \u2192 P (B a) (f a)) \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (f , xs))\n\ncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : UncurriedAll I D X P cn)\n \u2192 CurriedAll I D X P cn\ncurryAll I (`End i) X P cn pf =\n pf i refl tt\ncurryAll I (`Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryAll I D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryAll I (`Arg A B) X P cn pf =\n \u03bb a \u2192 curryAll I (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\ncurryAll I (`RecFun A B D) X P cn pf =\n \u03bb f ihf \u2192 curryAll I D X P (\u03bb xs \u2192 cn (f , xs)) (\u03bb i xs ihs \u2192 pf i (f , xs) (ihf , ihs))\n\nuncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : CurriedAll I D X P cn)\n \u2192 UncurriedAll I D X P cn\nuncurryAll I (`End .i) X P cn pf i refl tt =\n pf\nuncurryAll I (`Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryAll I (`Arg A B) X P cn pf i (a , xs) ihs =\n uncurryAll I (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\nuncurryAll I (`RecFun A B D) X P cn pf i (f , xs) (ihf , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (f , ys)) (pf f ihf) i xs ihs\n\n----------------------------------------------------------------------\n\nind :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : UncurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\n\nhyps :\n (I : Set)\n (D\u2081 : Desc I)\n (P : (i : I) \u2192 \u03bc I D\u2081 i \u2192 Set)\n (pcon : UncurriedAll I D\u2081 (\u03bc I D\u2081) P con)\n (D\u2082 : Desc I)\n (i : I)\n (xs : El I D\u2082 (\u03bc I D\u2081) i)\n \u2192 All I D\u2082 (\u03bc I D\u2081) P i xs\n\nind I D P pcon i (con xs) = pcon i xs (hyps I D P pcon D i xs)\n\nhyps I D P pcon (`End j) i q = tt\nhyps I D P pcon (`Rec j A) i (x , xs) = ind I D P pcon j x , hyps I D P pcon A i xs\nhyps I D P pcon (`Arg A B) i (a , b) = hyps I D P pcon (B a) i b\nhyps I D P pcon (`RecFun A B E) i (f , xs) = (\u03bb a \u2192 ind I D P pcon (B a) (f a)) , hyps I D P pcon E i xs\n\n----------------------------------------------------------------------\n\nind2 :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : CurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\nind2 I D P pcon i x = ind I D P (uncurryAll I D (\u03bc I D) P con pcon) i x\n\nelim :\n (I : Set)\n (E : Enum)\n (Cs : Tag E \u2192 Desc I)\n \u2192 let D = `Arg (Tag E) Cs in\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n \u2192 UncurriedCases E\n (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))) \n ((i : I) (x : \u03bc I D i) \u2192 P i x)\nelim I E Cs P cs i x =\n let D = `Arg (Tag E) Cs in\n ind2 I D P (case E (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))) cs) i x\n\nelim2 :\n (I : Set)\n (E : Enum)\n (Cs : Tag E \u2192 Desc I)\n \u2192 let D = `Arg (Tag E) Cs in\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n \u2192 let Q = (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs)))\n X = ((i : I) (x : \u03bc I D i) \u2192 P i x)\n in CurriedCases E Q X\nelim2 I E Cs P =\n let D = `Arg (Tag E) Cs\n Q = (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs)))\n X = ((i : I) (x : \u03bc I D i) \u2192 P i x)\n in curryCases E Q X (elim I E Cs P)\n\n----------------------------------------------------------------------\n\nmodule Sugared where\n\n data \u2115T : Set where `zero `suc : \u2115T\n data VecT : Set where `nil `cons : VecT\n\n \u2115D : Desc \u22a4\n \u2115D = `Arg \u2115T \u03bb\n { `zero \u2192 `End tt\n ; `suc \u2192 `Rec tt (`End tt)\n }\n\n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n\n zero : \u2115 tt\n zero = con (`zero , refl)\n\n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (`suc , n , refl)\n\n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg VecT \u03bb\n { `nil \u2192 `End zero\n ; `cons \u2192 `Arg (\u2115 tt) \u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n))\n }\n\n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n\n nil : (A : Set) \u2192 Vec A zero\n nil A = con (`nil , refl)\n\n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (`cons , n , x , xs , refl)\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 n\n ; tt (`suc , m , q) (ih , tt) n \u2192 suc (ih n)\n }\n )\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 zero\n ; tt (`suc , m , q) (ih , tt) n \u2192 add n (ih n)\n }\n )\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) ih n ys \u2192 ys\n ; .(con (`suc , m , refl)) (`cons , m , x , xs , refl) (ih , tt) n ys \u2192 cons A (add m n) x (ih n ys)\n }\n )\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) tt \u2192 nil A\n ; .(con (`suc , n , refl)) (`cons , n , xs , xss , refl) (ih , tt) \u2192 append A m xs (mult n m) ih\n }\n )\n\n----------------------------------------------------------------------\n\nmodule Desugared where\n\n \u2115T : Enum\n \u2115T = \"zero\" \u2237 \"suc\" \u2237 []\n \n VecT : Enum\n VecT = \"nil\" \u2237 \"cons\" \u2237 []\n \n \u2115C : Tag \u2115T \u2192 Desc \u22a4\n \u2115C = caseD \u2115T \u22a4\n ( `End tt\n , `Rec tt (`End tt)\n , tt\n )\n \n \u2115D : Desc \u22a4\n \u2115D = `Arg (Tag \u2115T) \u2115C\n \n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n \n zero : \u2115 tt\n zero = con (here , refl)\n \n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (there here , n , refl)\n \n zero2 : \u2115 tt\n zero2 = con2 \u22a4 \u2115D here\n\n suc2 : \u2115 tt \u2192 \u2115 tt\n suc2 = con2 \u22a4 \u2115D (there here)\n\n VecC : (A : Set) \u2192 Tag VecT \u2192 Desc (\u2115 tt)\n VecC A = caseD VecT (\u2115 tt)\n ( `End zero\n , `Arg (\u2115 tt) (\u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n)))\n , tt\n )\n \n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg (Tag VecT) (VecC A)\n \n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n \n nil : (A : Set) \u2192 Vec A zero\n nil A = con (here , refl)\n \n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (there here , n , x , xs , refl)\n \n nil2 : (A : Set) \u2192 Vec A zero\n nil2 A = con2 (\u2115 tt) (VecD A) here\n\n cons2 : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons2 A = con2 (\u2115 tt) (VecD A) (there here)\n\n----------------------------------------------------------------------\n\n module Induction where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 n)\n , (\u03bb m,q ih,tt n \u2192 suc (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 zero)\n , (\u03bb m,q ih,tt n \u2192 add n (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb m t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) m)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)) m (t , c))\n (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n )\n ( (\u03bb q ih n ys \u2192 subst (\u03bb m \u2192 Vec A (add m n)) q ys)\n , (\u03bb m',x,xs,q ih,tt n ys \u2192\n let m' = proj\u2081 m',x,xs,q\n x = proj\u2081 (proj\u2082 m',x,xs,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 m',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb m \u2192 Vec A (add m n)) q (cons A (add m' n) x (ih n ys))\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC (Vec A m) t) (Vec (Vec A m)) n)\n (ih : All (\u2115 tt) (VecD (Vec A m)) (Vec (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m)) n (t , c))\n \u2192 Vec A (mult n m)\n )\n ( (\u03bb q ih \u2192 subst (\u03bb n \u2192 Vec A (mult n m)) q (nil A))\n , (\u03bb n',xs,xss,q ih,tt \u2192\n let n' = proj\u2081 n',xs,xss,q\n xs = proj\u2081 (proj\u2082 n',xs,xss,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 n',xs,xss,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb n \u2192 Vec A (mult n m)) q (append A m xs (mult n' m) ih)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n module Eliminator where\n\n elim\u2115 : (P : (\u2115 tt) \u2192 Set)\n (pzero : P zero)\n (psuc : (m : \u2115 tt) \u2192 P m \u2192 P (suc m))\n (n : \u2115 tt)\n \u2192 P n\n elim\u2115 P pzero psuc = ind \u22a4 \u2115D (\u03bb u n \u2192 P n)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 P n) u (t , c))\n \u2192 P (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (here , q)))\n pzero\n u q\n )\n , (\u03bb n,q ih,tt \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (there here , proj\u2081 n,q , q)))\n (psuc (proj\u2081 n,q) (proj\u2081 ih,tt))\n u (proj\u2082 n,q)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n\n elimVec : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set)\n (pnil : P zero (nil A))\n (pcons : (n : \u2115 tt) (a : A) (xs : Vec A n) \u2192 P n xs \u2192 P (suc n) (cons A n a xs))\n (n : \u2115 tt)\n (xs : Vec A n)\n \u2192 P n xs\n elimVec A P pnil pcons = ind (\u2115 tt) (VecD A) (\u03bb n xs \u2192 P n xs)\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) n)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb n xs \u2192 P n xs) n (t , c))\n \u2192 P n (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq (\u2115 tt) zero (\u03bb n q \u2192 P n (con (here , q)))\n pnil\n n q\n )\n , (\u03bb n',x,xs,q ih,tt \u2192\n let n' = proj\u2081 n',x,xs,q\n x = proj\u2081 (proj\u2082 n',x,xs,q)\n xs = proj\u2081 (proj\u2082 (proj\u2082 n',x,xs,q))\n q = proj\u2082 (proj\u2082 (proj\u2082 n',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n elimEq (\u2115 tt) (suc n') (\u03bb n q \u2192 P n (con (there here , n' , x , xs , q)))\n (pcons n' x xs ih )\n n q\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elimVec A (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elimVec (Vec A m) (\u03bb n xss \u2192 Vec A (mult n m))\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n \n----------------------------------------------------------------------\n\n module GenericEliminator where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim2 \u22a4 \u2115T \u2115C _\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim2 \u22a4 \u2115T \u2115C _\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elim2 (\u2115 tt) VecT (VecC A) _\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elim2 (\u2115 tt) VecT (VecC (Vec A m)) _\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"91edc677d2001791306e00aacdc57508ae030ed2","subject":"Binary: +Equivalence-Reasoning","message":"Binary: +Equivalence-Reasoning\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Relation\/Binary\/NP.agda","new_file":"lib\/Relation\/Binary\/NP.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Relation.Binary.NP where\n\nopen import Level\nopen import Relation.Binary public\nimport Relation.Binary.PropositionalEquality as PropEq\n\n-- could this be moved in place of Trans is Relation.Binary.Core\nTrans' : \u2200 {a b c \u2113\u2081 \u2113\u2082 \u2113\u2083} {A : Set a} {B : Set b} {C : Set c} \u2192\n REL A B \u2113\u2081 \u2192 REL B C \u2113\u2082 \u2192 REL A C \u2113\u2083 \u2192 Set _\nTrans' P Q R = \u2200 {i j k} (x : P i j) (xs : Q j k) \u2192 R i k\n\nsubstitutive-to-reflexive : \u2200 {a \u2113 \u2113'} {A : Set a} {\u2248 : Rel A \u2113} {\u2261 : Rel A \u2113'}\n \u2192 Substitutive \u2261 \u2113 \u2192 Reflexive \u2248 \u2192 \u2261 \u21d2 \u2248\nsubstitutive-to-reflexive {\u2248 = \u2248} \u2261-subst \u2248-refl x\u1d63 = \u2261-subst (\u2248 _) x\u1d63 \u2248-refl\n\nsubstitutive\u21d2\u2261 : \u2200 {a \u2113} {A : Set a} {\u2261 : Rel A \u2113} \u2192 Substitutive \u2261 a \u2192 \u2261 \u21d2 PropEq._\u2261_\nsubstitutive\u21d2\u2261 subst = substitutive-to-reflexive {\u2248 = PropEq._\u2261_} subst PropEq.refl\n\nrecord Equality {a \u2113} {A : Set a} (_\u2261_ : Rel A \u2113) : Set (suc a \u2294 \u2113) where\n field\n isEquivalence : IsEquivalence _\u2261_\n subst : Substitutive _\u2261_ a\n\n open IsEquivalence isEquivalence public\n\n to-reflexive : \u2200 {\u2248} \u2192 Reflexive \u2248 \u2192 _\u2261_ \u21d2 \u2248\n to-reflexive = substitutive-to-reflexive subst\n\n to-propositional : _\u2261_ \u21d2 PropEq._\u2261_\n to-propositional = substitutive\u21d2\u2261 subst\n\n-- Equational reasoning combinators.\n\nmodule Trans-Reasoning {a \u2113} {A : Set a} (_\u2248_ : Rel A \u2113) (trans : Transitive _\u2248_) where\n\n infix 2 finally\n infixr 2 _\u2248\u27e8_\u27e9_\n\n _\u2248\u27e8_\u27e9_ : \u2200 x {y z : A} \u2192 x \u2248 y \u2192 y \u2248 z \u2192 x \u2248 z\n _ \u2248\u27e8 x\u2248y \u27e9 y\u2248z = trans x\u2248y y\u2248z\n\n -- When there is no reflexivty available this\n -- combinator can be used to end the reasoning.\n finally : \u2200 (x y : A) \u2192 x \u2248 y \u2192 x \u2248 y\n finally _ _ x\u2248y = x\u2248y\n\n syntax finally x y x\u2248y = x \u2248\u27e8 x\u2248y \u27e9\u220e y \u220e\n\nmodule Equivalence-Reasoning\n {a \u2113} {A : Set a} {_\u2248_ : Rel A \u2113}\n (E : IsEquivalence _\u2248_) where\n open IsEquivalence E\n open Trans-Reasoning _\u2248_ trans public hiding (finally)\n\n infix 2 _\u220e\n\n _\u220e : \u2200 x \u2192 x \u2248 x\n _ \u220e = refl\n\nmodule Preorder-Reasoning\n {p\u2081 p\u2082 p\u2083} (P : Preorder p\u2081 p\u2082 p\u2083) where\n open Preorder P\n open Equivalence-Reasoning isEquivalence public renaming (_\u2248\u27e8_\u27e9_ to _\u223c\u27e8_\u27e9_)\n\nmodule Setoid-Reasoning {a \u2113} (s : Setoid a \u2113) where\n open Preorder-Reasoning (Setoid.preorder s) public renaming (_\u223c\u27e8_\u27e9_ to _\u2248\u27e8_\u27e9_)\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Relation.Binary.NP where\n\nopen import Level\nopen import Relation.Binary public\nimport Relation.Binary.PropositionalEquality as PropEq\n\n-- could this be moved in place of Trans is Relation.Binary.Core\nTrans' : \u2200 {a b c \u2113\u2081 \u2113\u2082 \u2113\u2083} {A : Set a} {B : Set b} {C : Set c} \u2192\n REL A B \u2113\u2081 \u2192 REL B C \u2113\u2082 \u2192 REL A C \u2113\u2083 \u2192 Set _\nTrans' P Q R = \u2200 {i j k} (x : P i j) (xs : Q j k) \u2192 R i k\n\nsubstitutive-to-reflexive : \u2200 {a \u2113 \u2113'} {A : Set a} {\u2248 : Rel A \u2113} {\u2261 : Rel A \u2113'}\n \u2192 Substitutive \u2261 \u2113 \u2192 Reflexive \u2248 \u2192 \u2261 \u21d2 \u2248\nsubstitutive-to-reflexive {\u2248 = \u2248} \u2261-subst \u2248-refl x\u1d63 = \u2261-subst (\u2248 _) x\u1d63 \u2248-refl\n\nsubstitutive\u21d2\u2261 : \u2200 {a \u2113} {A : Set a} {\u2261 : Rel A \u2113} \u2192 Substitutive \u2261 a \u2192 \u2261 \u21d2 PropEq._\u2261_\nsubstitutive\u21d2\u2261 subst = substitutive-to-reflexive {\u2248 = PropEq._\u2261_} subst PropEq.refl\n\nrecord Equality {a \u2113} {A : Set a} (_\u2261_ : Rel A \u2113) : Set (suc a \u2294 \u2113) where\n field\n isEquivalence : IsEquivalence _\u2261_\n subst : Substitutive _\u2261_ a\n\n open IsEquivalence isEquivalence public\n\n to-reflexive : \u2200 {\u2248} \u2192 Reflexive \u2248 \u2192 _\u2261_ \u21d2 \u2248\n to-reflexive = substitutive-to-reflexive subst\n\n to-propositional : _\u2261_ \u21d2 PropEq._\u2261_\n to-propositional = substitutive\u21d2\u2261 subst\n\n-- Equational reasoning combinators.\n\nmodule Trans-Reasoning {a \u2113} {A : Set a} (_\u2248_ : Rel A \u2113) (trans : Transitive _\u2248_) where\n\n infix 2 finally\n infixr 2 _\u2248\u27e8_\u27e9_\n\n _\u2248\u27e8_\u27e9_ : \u2200 x {y z : A} \u2192 x \u2248 y \u2192 y \u2248 z \u2192 x \u2248 z\n _ \u2248\u27e8 x\u2248y \u27e9 y\u2248z = trans x\u2248y y\u2248z\n\n -- When there is no reflexivty available this\n -- combinator can be used to end the reasoning.\n finally : \u2200 (x y : A) \u2192 x \u2248 y \u2192 x \u2248 y\n finally _ _ x\u2248y = x\u2248y\n\n syntax finally x y x\u2248y = x \u2248\u27e8 x\u2248y \u27e9\u220e y \u220e\n\nmodule Preorder-Reasoning\n {p\u2081 p\u2082 p\u2083} (P : Preorder p\u2081 p\u2082 p\u2083) where\n open Preorder P\n open Trans-Reasoning _\u223c_ trans public hiding (finally) renaming (_\u2248\u27e8_\u27e9_ to _\u223c\u27e8_\u27e9_)\n\n infix 2 _\u220e\n\n _\u220e : \u2200 x \u2192 x \u223c x\n _ \u220e = refl\n\nmodule Setoid-Reasoning {a \u2113} (s : Setoid a \u2113) where\n open Preorder-Reasoning (Setoid.preorder s) public renaming (_\u223c\u27e8_\u27e9_ to _\u2248\u27e8_\u27e9_)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"5cd94161fed3259ba24dc7c2099fc439a5fdadb1","subject":"Reorder definitions","message":"Reorder definitions\n\nMove definitions about change contexts next to their uses.\n","repos":"inc-lc\/ilc-agda","old_file":"New\/Derive.agda","new_file":"New\/Derive.agda","new_contents":"module New.Derive where\nopen import New.Lang\nopen import New.Changes\nopen import New.LangOps\n\nderiveConst : \u2200 {\u03c4} \u2192\n Const \u03c4 \u2192\n Term \u2205 (\u0394t \u03c4)\n-- dplus = \u03bb m dm n dn \u2192 (m + dm) + (n + dn) - (m + n) = dm + dn\nderiveConst plus = abs (abs (abs (abs (app\u2082 (const plus) (var (that (that this))) (var this)))))\n-- minus = \u03bb m n \u2192 m - n\n-- dminus = \u03bb m dm n dn \u2192 (m + dm) - (n + dn) - (m - n) = dm - dn\nderiveConst minus = abs (abs (abs (abs (app\u2082 (const minus) (var (that (that this))) (var this)))))\nderiveConst cons = abs (abs (abs (abs (app (app (const cons) (var (that (that this)))) (var this)))))\nderiveConst fst = abs (abs (app (const fst) (var this)))\nderiveConst snd = abs (abs (app (const snd) (var this)))\n-- deriveConst linj = abs (abs (app (const linj) (app (const linj) (var this))))\n-- deriveConst rinj = abs (abs (app (const linj) (app (const rinj) (var this))))\n-- deriveConst (match {t1} {t2} {t3}) =\n-- -- \u03bb s ds f df g dg \u2192\n-- abs (abs (abs (abs (abs (abs\n-- -- match ds\n-- (app\u2083 (const match) (var (that (that (that (that this)))))\n-- -- \u03bb ds\u2081 \u2192 match ds\u2081\n-- (abs (app\u2083 (const match) (var this)\n-- -- case inj\u2081 da \u2192 absV 1 (\u03bb da \u2192 match s\n-- (abs (app\u2083 (const match) (var (that (that (that (that (that (that (that this))))))))\n-- -- \u03bb a \u2192 app\u2082 df a da\n-- (abs (app\u2082 (var (that (that (that (that (that this)))))) (var this) (var (that this))))\n-- -- absurd: \u03bb b \u2192 dg b (nil b)\n-- (abs (app\u2082 (var (that (that (that this)))) (var this) (app (onil\u03c4o t2) (var this))))))\n-- -- case inj\u2082 db \u2192 absV 1 (\u03bb db \u2192 match s\n-- (abs (app\u2083 (const match) (var (that (that (that (that (that (that (that this))))))))\n-- -- absurd: \u03bb a \u2192 df a (nil a)\n-- (abs (app\u2082 (var (that (that (that (that (that this)))))) (var this) (app (onil\u03c4o t1) (var this))))\n-- -- \u03bb b \u2192 app\u2082 dg b db\n-- (abs (app\u2082 (var (that (that (that this)))) (var this) (var (that this))))))))\n-- -- recomputation branch:\n-- -- \u03bb s2 \u2192 ominus (match s2 (f \u2295 df) (g \u2295 dg)) (match s f g)\n-- (abs (app\u2082 (ominus\u03c4o t3)\n-- -- (match s2 (f \u2295 df) (g \u2295 dg))\n-- (app\u2083 (const match)\n-- (var this)\n-- (app\u2082 (oplus\u03c4o (t1 \u21d2 t3))\n-- (var (that (that (that (that this)))))\n-- (var (that (that (that this)))))\n-- (app\u2082 (oplus\u03c4o (t2 \u21d2 t3))\n-- (var (that (that this)))\n-- (var (that this))))\n-- -- (match s f g)\n-- (app\u2083 (const match)\n-- (var (that (that (that (that (that (that this)))))))\n-- (var (that (that (that (that this)))))\n-- (var (that (that this))))))))))))\n\n-- {-\n-- derive (\u03bb s f g \u2192 match s f g) =\n-- \u03bb s ds f df g dg \u2192\n-- case ds of\n-- ch1 da \u2192\n-- case s of\n-- inj1 a \u2192 df a da\n-- inj2 b \u2192 {- absurd -} dg b (nil b)\n-- ch2 db \u2192\n-- case s of\n-- inj2 b \u2192 dg b db\n-- inj1 a \u2192 {- absurd -} df a (nil a)\n-- rp s2 \u2192\n-- match (f \u2295 df) (g \u2295 dg) s2 \u229d\n-- match f g s\n-- -}\n\n\n\u0394\u0393 : Context \u2192 Context\n\u0394\u0393 \u2205 = \u2205\n\u0394\u0393 (\u03c4 \u2022 \u0393) = \u0394t \u03c4 \u2022 \u03c4 \u2022 \u0394\u0393 \u0393\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394\u0393 \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394t \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u227c\u0394\u0393\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394\u0393 \u0393) (\u0394t \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nfit : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394\u0393 \u0393) \u03c4\nfit = weaken \u0393\u227c\u0394\u0393\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394\u0393 \u0393) (\u0394t \u03c4)\nderive (const c) = weaken (\u2205\u227c\u0393 {\u0394\u0393 _}) (deriveConst c)\nderive (var x) = var (deriveVar x)\nderive (app s t) = app (app (derive s) (fit t)) (derive t)\nderive (abs t) = abs (abs (derive t))\n\nopen import New.LangChanges\n\n-- Lemmas needed to reason about derivation, for any correctness proof\nalternate : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7Context \u2192 eCh \u0393 \u2192 \u27e6 \u0394\u0393 \u0393 \u27e7Context\nalternate {\u2205} \u2205 \u2205 = \u2205\nalternate {\u03c4 \u2022 \u0393} (v \u2022 \u03c1) (dv \u2022 d\u03c1) = dv \u2022 v \u2022 alternate \u03c1 d\u03c1\n\n\u27e6\u0393\u227c\u0394\u0393\u27e7 : \u2200 {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n \u03c1 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7\u227c (alternate \u03c1 d\u03c1)\n\u27e6\u0393\u227c\u0394\u0393\u27e7 \u2205 \u2205 = refl\n\u27e6\u0393\u227c\u0394\u0393\u27e7 (v \u2022 \u03c1) (dv \u2022 d\u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1)\n\nfit-sound : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n \u27e6 t \u27e7Term \u03c1 \u2261 \u27e6 fit t \u27e7Term (alternate \u03c1 d\u03c1)\nfit-sound t \u03c1 d\u03c1 = trans\n (cong \u27e6 t \u27e7Term (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1))\n (sym (weaken-sound t _))\n\n-- The change semantics is just the semantics composed with derivation!\n\u27e6_\u27e7\u0394Var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 Cht \u03c4\n\u27e6 x \u27e7\u0394Var \u03c1 d\u03c1 = \u27e6 deriveVar x \u27e7Var (alternate \u03c1 d\u03c1)\n\n\u27e6_\u27e7\u0394Term : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 Cht \u03c4\n\u27e6 t \u27e7\u0394Term \u03c1 d\u03c1 = \u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1)\n\n\u27e6_\u27e7\u0394Const : \u2200 {\u03c4} (c : Const \u03c4) \u2192 Cht \u03c4\n\u27e6 c \u27e7\u0394Const = \u27e6 deriveConst c \u27e7Term \u2205\n\n\u27e6_\u27e7\u0394Const-rewrite : \u2200 {\u03c4 \u0393} (c : Const \u03c4) (\u03c1 : \u27e6 \u0393 \u27e7Context) d\u03c1 \u2192 \u27e6_\u27e7\u0394Term (const c) \u03c1 d\u03c1 \u2261 \u27e6 c \u27e7\u0394Const\n\u27e6 c \u27e7\u0394Const-rewrite \u03c1 d\u03c1 rewrite weaken-sound {\u0393\u2081\u227c\u0393\u2082 = \u2205\u227c\u0393} (deriveConst c) (alternate \u03c1 d\u03c1) | \u27e6\u2205\u227c\u0393\u27e7-\u2205 (alternate \u03c1 d\u03c1) = refl\n","old_contents":"module New.Derive where\nopen import New.Lang\nopen import New.Changes\nopen import New.LangOps\n\n\u0394\u0393 : Context \u2192 Context\n\u0394\u0393 \u2205 = \u2205\n\u0394\u0393 (\u03c4 \u2022 \u0393) = \u0394t \u03c4 \u2022 \u03c4 \u2022 \u0394\u0393 \u0393\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394\u0393 \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394t \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u227c\u0394\u0393\n\nderiveConst : \u2200 {\u03c4} \u2192\n Const \u03c4 \u2192\n Term \u2205 (\u0394t \u03c4)\n-- dplus = \u03bb m dm n dn \u2192 (m + dm) + (n + dn) - (m + n) = dm + dn\nderiveConst plus = abs (abs (abs (abs (app\u2082 (const plus) (var (that (that this))) (var this)))))\n-- minus = \u03bb m n \u2192 m - n\n-- dminus = \u03bb m dm n dn \u2192 (m + dm) - (n + dn) - (m - n) = dm - dn\nderiveConst minus = abs (abs (abs (abs (app\u2082 (const minus) (var (that (that this))) (var this)))))\nderiveConst cons = abs (abs (abs (abs (app (app (const cons) (var (that (that this)))) (var this)))))\nderiveConst fst = abs (abs (app (const fst) (var this)))\nderiveConst snd = abs (abs (app (const snd) (var this)))\n-- deriveConst linj = abs (abs (app (const linj) (app (const linj) (var this))))\n-- deriveConst rinj = abs (abs (app (const linj) (app (const rinj) (var this))))\n-- deriveConst (match {t1} {t2} {t3}) =\n-- -- \u03bb s ds f df g dg \u2192\n-- abs (abs (abs (abs (abs (abs\n-- -- match ds\n-- (app\u2083 (const match) (var (that (that (that (that this)))))\n-- -- \u03bb ds\u2081 \u2192 match ds\u2081\n-- (abs (app\u2083 (const match) (var this)\n-- -- case inj\u2081 da \u2192 absV 1 (\u03bb da \u2192 match s\n-- (abs (app\u2083 (const match) (var (that (that (that (that (that (that (that this))))))))\n-- -- \u03bb a \u2192 app\u2082 df a da\n-- (abs (app\u2082 (var (that (that (that (that (that this)))))) (var this) (var (that this))))\n-- -- absurd: \u03bb b \u2192 dg b (nil b)\n-- (abs (app\u2082 (var (that (that (that this)))) (var this) (app (onil\u03c4o t2) (var this))))))\n-- -- case inj\u2082 db \u2192 absV 1 (\u03bb db \u2192 match s\n-- (abs (app\u2083 (const match) (var (that (that (that (that (that (that (that this))))))))\n-- -- absurd: \u03bb a \u2192 df a (nil a)\n-- (abs (app\u2082 (var (that (that (that (that (that this)))))) (var this) (app (onil\u03c4o t1) (var this))))\n-- -- \u03bb b \u2192 app\u2082 dg b db\n-- (abs (app\u2082 (var (that (that (that this)))) (var this) (var (that this))))))))\n-- -- recomputation branch:\n-- -- \u03bb s2 \u2192 ominus (match s2 (f \u2295 df) (g \u2295 dg)) (match s f g)\n-- (abs (app\u2082 (ominus\u03c4o t3)\n-- -- (match s2 (f \u2295 df) (g \u2295 dg))\n-- (app\u2083 (const match)\n-- (var this)\n-- (app\u2082 (oplus\u03c4o (t1 \u21d2 t3))\n-- (var (that (that (that (that this)))))\n-- (var (that (that (that this)))))\n-- (app\u2082 (oplus\u03c4o (t2 \u21d2 t3))\n-- (var (that (that this)))\n-- (var (that this))))\n-- -- (match s f g)\n-- (app\u2083 (const match)\n-- (var (that (that (that (that (that (that this)))))))\n-- (var (that (that (that (that this)))))\n-- (var (that (that this))))))))))))\n\n-- {-\n-- derive (\u03bb s f g \u2192 match s f g) =\n-- \u03bb s ds f df g dg \u2192\n-- case ds of\n-- ch1 da \u2192\n-- case s of\n-- inj1 a \u2192 df a da\n-- inj2 b \u2192 {- absurd -} dg b (nil b)\n-- ch2 db \u2192\n-- case s of\n-- inj2 b \u2192 dg b db\n-- inj1 a \u2192 {- absurd -} df a (nil a)\n-- rp s2 \u2192\n-- match (f \u2295 df) (g \u2295 dg) s2 \u229d\n-- match f g s\n-- -}\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394\u0393 \u0393) (\u0394t \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nfit : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394\u0393 \u0393) \u03c4\nfit = weaken \u0393\u227c\u0394\u0393\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394\u0393 \u0393) (\u0394t \u03c4)\nderive (const c) = weaken (\u2205\u227c\u0393 {\u0394\u0393 _}) (deriveConst c)\nderive (var x) = var (deriveVar x)\nderive (app s t) = app (app (derive s) (fit t)) (derive t)\nderive (abs t) = abs (abs (derive t))\n\nopen import New.LangChanges\n\n-- Lemmas needed to reason about derivation, for any correctness proof\nalternate : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7Context \u2192 eCh \u0393 \u2192 \u27e6 \u0394\u0393 \u0393 \u27e7Context\nalternate {\u2205} \u2205 \u2205 = \u2205\nalternate {\u03c4 \u2022 \u0393} (v \u2022 \u03c1) (dv \u2022 d\u03c1) = dv \u2022 v \u2022 alternate \u03c1 d\u03c1\n\n\u27e6\u0393\u227c\u0394\u0393\u27e7 : \u2200 {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n \u03c1 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7\u227c (alternate \u03c1 d\u03c1)\n\u27e6\u0393\u227c\u0394\u0393\u27e7 \u2205 \u2205 = refl\n\u27e6\u0393\u227c\u0394\u0393\u27e7 (v \u2022 \u03c1) (dv \u2022 d\u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1)\n\nfit-sound : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192\n \u27e6 t \u27e7Term \u03c1 \u2261 \u27e6 fit t \u27e7Term (alternate \u03c1 d\u03c1)\nfit-sound t \u03c1 d\u03c1 = trans\n (cong \u27e6 t \u27e7Term (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c1 d\u03c1))\n (sym (weaken-sound t _))\n\n-- The change semantics is just the semantics composed with derivation!\n\u27e6_\u27e7\u0394Var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 Cht \u03c4\n\u27e6 x \u27e7\u0394Var \u03c1 d\u03c1 = \u27e6 deriveVar x \u27e7Var (alternate \u03c1 d\u03c1)\n\n\u27e6_\u27e7\u0394Term : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 Cht \u03c4\n\u27e6 t \u27e7\u0394Term \u03c1 d\u03c1 = \u27e6 derive t \u27e7Term (alternate \u03c1 d\u03c1)\n\n\u27e6_\u27e7\u0394Const : \u2200 {\u03c4} (c : Const \u03c4) \u2192 Cht \u03c4\n\u27e6 c \u27e7\u0394Const = \u27e6 deriveConst c \u27e7Term \u2205\n\n\u27e6_\u27e7\u0394Const-rewrite : \u2200 {\u03c4 \u0393} (c : Const \u03c4) (\u03c1 : \u27e6 \u0393 \u27e7Context) d\u03c1 \u2192 \u27e6_\u27e7\u0394Term (const c) \u03c1 d\u03c1 \u2261 \u27e6 c \u27e7\u0394Const\n\u27e6 c \u27e7\u0394Const-rewrite \u03c1 d\u03c1 rewrite weaken-sound {\u0393\u2081\u227c\u0393\u2082 = \u2205\u227c\u0393} (deriveConst c) (alternate \u03c1 d\u03c1) | \u27e6\u2205\u227c\u0393\u27e7-\u2205 (alternate \u03c1 d\u03c1) = refl\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"27936513218bb472408225b9883e149a0ed927b2","subject":"Control\/Protocol\/Choreography.agda","message":"Control\/Protocol\/Choreography.agda\n","repos":"crypto-agda\/crypto-agda","old_file":"Control\/Protocol\/Choreography.agda","new_file":"Control\/Protocol\/Choreography.agda","new_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\nopen import Data.One hiding (_\u225f_)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\n {-\ndata S<_> {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n\npattern com' q M P = com (mk q M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\n\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n Trace : Proto \u2192 Proto\n Trace end = end\n Trace (com c) = com (Trace\u1d9c c)\n\n Trace\u1d9c : Com \u2192 Com\n Trace\u1d9c (mk io M P) = \u03a3\u1d9c M \u03bb m \u2192 Trace (P m)\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c (mk q M P) = mk (dual\u1d35\u1d3c q) M \u03bb m \u2192 dual (P m)\n\ndata IsTrace : Proto \u2192 \u2605\u2081 where\n end : IsTrace end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsTrace (P m)) \u2192 IsTrace (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\nmutual\n data Dual\u1d9c : Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a0\u1d9c M P) (\u03a3\u1d9c M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a3\u1d9c M P) (\u03a0\u1d9c M Q)\n\n data Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n {-\n \u03a0\u2610\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P [ x ]) (Q x)) \u2192 Dual (\u03a0\u1d3e (\u2610 M) P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0\u2610 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q [ x ])) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e (\u2610 M) Q)\n -}\n\n {-\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Trace : Choreo I \u2192 Proto\n \u2102Trace \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Trace-IsTrace : \u2200 \u2102 \u2192 IsTrace (\u2102Trace \u2102)\n \u2102Trace-IsTrace (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com q M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com q M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\nTrace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261\u1d3e Trace P\nTrace-idempotent end = end\nTrace-idempotent (com' _ M P) = com Out M \u03bb m \u2192 Trace-idempotent (P m)\n\nTrace-dual-oblivious : \u2200 P \u2192 Trace (dual P) \u2261\u1d3e Trace P\nTrace-dual-oblivious end = end\nTrace-dual-oblivious (com' _ M P) = com Out M \u03bb m \u2192 Trace-dual-oblivious (P m)\n\nSink : Proto \u2192 Proto\nSink = dual \u2218 Trace\n-}\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n{-\n++Tele : \u2200 (P : Proto){Q : Tele P \u2192 Proto} (xs : Tele P) \u2192 Tele (Q xs) \u2192 Tele (P >>= Q)\n++Tele end _ ys = ys\n++Tele (com' q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com q M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com q M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n{-\nDual-sym (\u03a0\u2610\u00b7\u03a3 f) = {!\u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))!}\nDual-sym (\u03a3\u00b7\u03a0\u2610 f) = {!\u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))!}\n-}\n-}\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele (dual P) \u2261 Tele P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n El->>= : (P : Proto){Q : Tele P \u2192 Proto}{X : Tele (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Tele P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n-}\n\n>>=-com : (P : Proto){Q : Tele P \u2192 Proto}{R : Tele P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Tele P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Tele P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : \u2605_ a} (l r : A) \u2192 LR \u2192 A \n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP >>\u1d9c S = record P { P = \u03bb m \u2192 S (Com.P P m) }\n\n{-\nmodule V1 where\n mutual\n Sim\u1d3e : Proto \u2192 Proto \u2192 Proto\n Sim\u1d3e end Q = Q\n Sim\u1d3e P end = P\n Sim\u1d3e (com P\u1d38) (com P\u1d3f) = \u03a3\u1d3e LR (Sim\u1d9c P\u1d38 P\u1d3f)\n\n Sim\u1d9c : Com \u2192 Com \u2192 LR \u2192 Proto\n Sim\u1d9c P\u1d38 P\u1d3f `L = Sim\u1d9cL P\u1d38 P\u1d3f\n Sim\u1d9c P\u1d38 P\u1d3f `R = Sim\u1d9cR P\u1d38 P\u1d3f\n\n Sim\u1d9cL : Com \u2192 Com \u2192 Proto\n Sim\u1d9cL (mk q\u1d38 M\u1d38 P\u1d38) Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 Sim\u1d3e (P\u1d38 m) (com Q))\n\n Sim\u1d9cR : Com \u2192 Com \u2192 Proto\n Sim\u1d9cR P (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 Sim\u1d3e (com P) (P\u1d3f m))\n\nmodule V2 where\n mutual\n Sim\u1d3e : Proto \u2192 Proto \u2192 Proto\n Sim\u1d3e end Q = Q\n Sim\u1d3e (\u03a0\u1d3e M P) Q = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e (P m) Q\n Sim\u1d3e P end = P\n Sim\u1d3e P (\u03a0\u1d3e M Q) = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e P (Q m)\n Sim\u1d3e (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 Sim\u1d3e (P m) (\u03a3\u1d3e M' Q)) ,inr: (\u03bb m' \u2192 Sim\u1d3e (\u03a3\u1d3e M P) (Q m')) ]\n\n Sim\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\n Sim\u1d3e-assoc end Q R s = s\n Sim\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = Sim\u1d3e-assoc (P m) _ _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , Sim\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n Sim\u1d3e-sendR : \u2200 {M} P Q \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7 \u2192 (m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7\n Sim\u1d3e-sendR P Q s m = {!!}\n\n Sim\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7) \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7\n Sim\u1d3e-recvR P Q s = {!!}\n\n Sim\u1d3e-! : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 Sim\u1d3e Q P \u27e7\n Sim\u1d3e-! end Q p = {!!}\n Sim\u1d3e-! (\u03a0\u1d3e M P) end p x = Sim\u1d3e-! (P x) end (p x)\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-recvR (Q m') P \u03bb m \u2192 Sim\u1d3e-sendR {!!} {!!} (p m) m'\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 Sim\u1d3e-! (P m) (com (mk Out M' Q)) (p m) \n Sim\u1d3e-! (\u03a3\u1d3e M P) end p = p \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-! (com (mk Out M P)) (Q m') (p m') \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (Sim\u1d3e-! (P m) (com (mk Out M' Q)) p) \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (Sim\u1d3e-! (com (mk Out M P)) (Q m) p) \n\n Sim\u1d3e-apply : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n Sim\u1d3e-apply end Q s p = s\n Sim\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = Sim\u1d3e-apply (P m) Q (s m) p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = Sim\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n -}\n\nmodule V3 where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n P \u214b\u1d3e Q = \u03a3\u1d3e LR (Par\u1d3e P Q)\n\n Par\u1d3e : Proto \u2192 Proto \u2192 LR \u2192 Proto\n Par\u1d3e end Q `L = Q\n Par\u1d3e (com' q M P) Q `L = com' q M \u03bb m \u2192 P m \u214b\u1d3e Q\n Par\u1d3e P end `R = P\n Par\u1d3e P (com' q M Q) `R = com' q M \u03bb m \u2192 P \u214b\u1d3e Q m\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n P ox\u1d3e Q = \u03a0\u1d3e LR (Ten\u1d3e P Q)\n\n Ten\u1d3e : Proto \u2192 Proto \u2192 LR \u2192 Proto\n Ten\u1d3e end Q `L = Q\n Ten\u1d3e (com' q M P) Q `L = com' q M \u03bb m \u2192 P m ox\u1d3e Q\n Ten\u1d3e P end `R = P\n Ten\u1d3e P (com' q M Q) `R = com' q M \u03bb m \u2192 P ox\u1d3e Q m\n\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = {!!}\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id = {!!}\n \n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm P Q = {!!}\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e P Q p q = {!!}\n\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = {!!}\n\n \u2295\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map = {!!}\n\n &\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map = {!!}\n\n \u214b\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u214b\u1d3e R \u27e7 \u2192 \u27e6 Q \u214b\u1d3e S \u27e7\n \u214b\u1d3e-map = {!!}\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map = {!!}\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = let z = switchL _ _ _ (comma\u1d3e (P \u214b\u1d3e Q) (dual Q \u214b\u1d3e R) pq qr) in {!z!}\n\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Tele B \u00d7 Tele E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a3\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a0\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product\nopen import Data.One\n\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndualInOut : InOut \u2192 InOut\ndualInOut In = Out\ndualInOut Out = In\n\ndata Proto : \u2605\u2081 where\n end : Proto\n com : (q : InOut)(M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com q M P \u2261\u1d3e com q M Q\n\npattern \u03a0' M P = com In M P\npattern \u03a3' M P = com Out M P\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0S' : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0S' M P = \u03a0' (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3S' : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3S' M P = \u03a3' (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u27e6_\u27e7\u03a0\u03a3 : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u03a0\u03a3 In = \u03a0\n\u27e6_\u27e7\u03a0\u03a3 Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com q M P \u27e7 = \u27e6 q \u27e7\u03a0\u03a3 M \u03bb x \u2192 \u27e6 P x \u27e7\n\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\n_-[_]\u2192\u00f8\u204f_ : \u2200 {I}(A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\nA -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\ndata [_\/_\u2261_] {I} : Choreo I \u2192 I \u2192 Proto \u2192 \u2605\u2081 where\n I\u03a3D : \u2200 {A B M \u2102 \u2102A} \u2192 (\u2200 m \u2192 [ \u2102 m \/ A \u2261 \u2102A m ]) \u2192 [ (A -[ M ]\u2192 B \u204f \u2102) \/ A \u2261 \u03a3' M \u2102A ]\n I\u03a0D : \u2200 {A B M \u2102 \u2102B} \u2192 (\u2200 m \u2192 [ \u2102 m \/ B \u2261 \u2102B m ]) \u2192 [ (A -[ M ]\u2192 B \u204f \u2102) \/ B \u2261 \u03a0' M \u2102B ]\n-- I\u03a0S : \u2200 {A B C M \u2102 \u2102C} \u2192 (\u2200 ..m \u2192 [ \u2102 m \/ C \u2261 \u2102C m ]) \u2192 [ (A -[ M ]\u2192 B \u204f \u2102) \/ C \u2261 \u03a0' (\u2610 M) \u2102C ]\n \u2605\u03a3D : \u2200 {A M \u2102 \u2102A} \u2192 (\u2200 m \u2192 [ \u2102 m \/ A \u2261 \u2102A m ]) \u2192 [ (A -[ M ]\u2192\u2605\u204f \u2102) \/ A \u2261 \u03a3' M \u2102A ]\n \u2605\u03a0D : \u2200 {A B M \u2102 \u2102B} \u2192 (\u2200 m \u2192 [ \u2102 m \/ B \u2261 \u2102B m ]) \u2192 [ (A -[ M ]\u2192\u2605\u204f \u2102) \/ B \u2261 \u03a0' M \u2102B ]\n --\u00f8\u03a3D : \u2200 {A M \u2102 \u2102A} \u2192 (\u2200 ..m \u2192 [ \u2102 m \/ A \u2261 \u2102A m ]) \u2192 [ (A -[ M ]\u2192\u00f8\u204f \u2102) \/ A \u2261 \u03a3' S M \u2102A ]\n --\u00f8\u03a0D : \u2200 {A B M \u2102 \u2102B} \u2192 (\u2200 ..m \u2192 [ \u2102 m \/ B \u2261 \u2102B m ]) \u2192 [ (A -[ M ]\u2192\u00f8\u204f \u2102) \/ B \u2261 \u03a0' S M \u2102B ]\n end : \u2200 {A} \u2192 [ end \/ A \u2261 end ]\n\nTrace : Proto \u2192 Proto\nTrace end = end\nTrace (com _ A B) = \u03a3' A \u03bb m \u2192 Trace (B m)\n\ndual : Proto \u2192 Proto\ndual end = end\ndual (com q A B) = com (dualInOut q) A (\u03bb x \u2192 dual (B x))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com q M P\u2261Q) = cong (com q M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com q M P) = com q M (\u03bb m \u2192 \u2261\u1d3e-refl (P m))\n\nTrace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261\u1d3e Trace P\nTrace-idempotent end = end\nTrace-idempotent (com q M P) = \u03a3' M \u03bb m \u2192 Trace-idempotent (P m)\n\nTrace-dual-oblivious : \u2200 P \u2192 Trace (dual P) \u2261\u1d3e Trace P\nTrace-dual-oblivious end = end\nTrace-dual-oblivious (com q M P) = \u03a3' M \u03bb m \u2192 Trace-dual-oblivious (P m)\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>\u2261_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>\u2261 Q = Q _\ncom \u03c0 A B >>\u2261 Q = com \u03c0 A (\u03bb x \u2192 B x >>\u2261 (\u03bb xs \u2192 Q (x , xs)))\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>\u2261 \u03bb _ \u2192 Q\n\n++Tele : \u2200 (P : Proto){Q : Tele P \u2192 Proto} (xs : Tele P) \u2192 Tele (Q xs) \u2192 Tele (P >>\u2261 Q)\n++Tele end _ ys = ys\n++Tele (com q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\nright-unit : \u2200 (P : Proto) \u2192 (P >>\u2261 \u03bb x \u2192 end) \u2261\u1d3e P\nright-unit end = end\nright-unit (com q M P) = com q M \u03bb m \u2192 right-unit (P m)\n\nassoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>\u2261 Q) \u2192 Proto)\n \u2192 P >>\u2261 (\u03bb x \u2192 Q x >>\u2261 (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>\u2261 Q) >>\u2261 R)\nassoc end Q R = \u2261\u1d3e-refl (Q _ >>\u2261 R)\nassoc (com q M P) Q R = com q M \u03bb m \u2192 assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\n_\u00d7'_ : Set \u2192 Proto \u2192 Proto\nA \u00d7' B = \u03a3' A \u03bb _ \u2192 B\n\n_\u2192'_ : Set \u2192 Proto \u2192 Proto\nA \u2192' B = \u03a0' A \u03bb _ \u2192 B\n\ndata DualInOut : InOut \u2192 InOut \u2192 \u2605 where\n DInOut : DualInOut In Out\n DOutIn : DualInOut Out In\n\ndata Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {A B B'} \u2192 (\u2200 x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a0' A B) (\u03a3' A B')\n \u03a3\u00b7\u03a0 : \u2200 {A B B'} \u2192 (\u2200 x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a3' A B) (\u03a0' A B')\n\ndata Sing {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n sing : \u2200 x \u2192 Sing x\n\n-- Commit = \u03bb M R \u2192 \u03a3' D (\u2610 M) \u03bb { [ m ] \u2192 \u03a0' D R \u03bb r \u2192 \u03a3' D (Sing m) (\u03bb _ \u2192 end) }\nCommit = \u03bb M R \u2192 \u03a3S' M \u03bb m \u2192 \u03a0' R \u03bb r \u2192 \u03a3' (Sing m) (\u03bb _ \u2192 end)\n\ncommit : \u2200 M R (m : M) \u2192 \u27e6 Commit M R \u27e7\ncommit M R m = [ m ] , (\u03bb x \u2192 (sing m) , _)\n\ndecommit : \u2200 M R (r : R) \u2192 \u27e6 dual (Commit M R) \u27e7\ndecommit M R r = \u03bb { [ m ] \u2192 r , (\u03bb x \u2192 0\u2081) }\n\ndata [_&_\u2261_]InOut : InOut \u2192 InOut \u2192 InOut \u2192 \u2605\u2081 where\n \u03a0XX : \u2200 {X} \u2192 [ In & X \u2261 X ]InOut\n X\u03a0X : \u2200 {X} \u2192 [ X & In \u2261 X ]InOut\n\n&InOut-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]InOut \u2192 [ Q & P \u2261 R ]InOut\n&InOut-comm \u03a0XX = X\u03a0X\n&InOut-comm X\u03a0X = \u03a0XX\n\ndata [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n end& : \u2200 {P} \u2192 [ end & P \u2261 P ]\n &end : \u2200 {P} \u2192 [ P & end \u2261 P ]\n D&D : \u2200 {qP qQ qR M P Q R}(q : [ qP & qQ \u2261 qR ]InOut)(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ com qP M P & com qQ M Q \u2261 com qR M R ]\n S&D : \u2200 {qP qQ qR M P Q R}(q : [ qP & qQ \u2261 qR ]InOut)(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ com qP (\u2610 M) P & com qQ M Q \u2261 com qR M R ]\n D&S : \u2200 {qP qQ qR M P Q R}(q : [ qP & qQ \u2261 qR ]InOut)(P& : \u2200 m \u2192 [ P m & Q [ m ] \u2261 R m ]) \u2192 [ com qP M P & com qQ (\u2610 M) Q \u2261 com qR M R ]\n\n&-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n&-comm end& = &end\n&-comm &end = end&\n&-comm (D&D q P&) = D&D (&InOut-comm q) (\u03bb m \u2192 &-comm (P& m))\n&-comm (S&D q P&) = D&S (&InOut-comm q) (\u03bb m \u2192 &-comm (P& m))\n&-comm (D&S q P&) = S&D (&InOut-comm q) (\u03bb m \u2192 &-comm (P& m))\n\nDualInOut-sym : \u2200 {P Q} \u2192 DualInOut P Q \u2192 DualInOut Q P\nDualInOut-sym DInOut = DOutIn\nDualInOut-sym DOutIn = DInOut\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n\nDualInOut-spec : \u2200 P \u2192 DualInOut P (dualInOut P)\nDualInOut-spec In = DInOut\nDualInOut-spec Out = DOutIn\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (com In M P) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-spec (P x))\nDual-spec (com Out M P) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-spec (P x))\n\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele (dual P) \u2261 Tele P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com q M P) X = \u27e6 q \u27e7\u03a0\u03a3 M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n bind-spec : (P : Proto){Q : Tele P \u2192 Proto}{X : Tele (P >>\u2261 Q) \u2192 \u2605} \u2192 El (P >>\u2261 Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n bind-spec end = refl\n bind-spec (com q M P) = cong (\u27e6 q \u27e7\u03a0\u03a3 M) (funExt \u03bb m \u2192 bind-spec (P m))\n\n\nmodule _ {A B} where\n run-com : (P : Proto) \u2192 El P (const A) \u2192 El (dual P) (const B) \u2192 A \u00d7 B\n run-com end a b = a , b\n run-com (\u03a0' M P) p (m , q) = run-com (P m) (p m) q\n run-com (\u03a3' M P) (m , p) q = run-com (P m) p (q m)\n\ncom-cont : (P : Proto){A : Tele P \u2192 \u2605}{B : Tele (dual P) \u2192 \u2605} \u2192 El P A \u2192 El (dual P) B \u2192 \u03a3 (Tele P) A \u00d7 \u03a3 (Tele (dual P)) B\ncom-cont end p q = (_ , p) , (_ , q)\ncom-cont (\u03a0' A B) p (m , q) with com-cont (B m) (p m) q\n... | (X , a) , (Y , b) = ((m , X) , a) , (m , Y) , b\ncom-cont (\u03a3' A B) (m , p) q with com-cont (B m) p (q m)\n... | (X , a) , (Y , b) = ((m , X) , a) , (m , Y) , b\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Proto \u2192 \u2605\u2081 where\n recv : \u2200 {M P} \u2192 ((m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0' M P)\n send : \u2200 {M P} (m : M) \u2192 this (P m) \u2192 ProcessF this (\u03a3' M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0' (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3' (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process (A : \u2605) : Proto \u2192 \u2605\u2081 where\n do : \u2200 {P} \u2192 ProcessF (Process A) P \u2192 Process A P\n end : A \u2192 Process A end\n\nmutual\n SimL : Proto \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Proto \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n left : \u2200 {q M P Q} \u2192 SimL (com q M P) Q \u2192 Sim (com q M P) Q\n right : \u2200 {P q M Q} \u2192 SimR P (com q M Q) \u2192 Sim P (com q M Q)\n end : Sim end end\n\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = \u2200 {A} \u2192 Process A (dual P) \u2192 Process A Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& P& (right \u00b7PA) (right \u00b7PB) = {! (sim&R P& \u00b7PA \u00b7PB)!}\nsim& end& PA PB = PB\nsim& &end PA PB = PA\n\nsim&R (\u03a0D\u03a3D\u03a3S P&) (recv PQA) (send m PQB) = sendS m (sim& (P& m) (PQA m) PQB)\nsim&R (\u03a0D\u03a0D\u03a0D P&) (recv PQA) (recv PQB) = recv \u03bb m \u2192 sim& (P& m) (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) (recvS PQA) (recvS PQB) = recvS \u03bb m \u2192 sim& (P& m) (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) (recvS PQA) (sendS m PQB) = sendS m (sim& (P& m) (PQA m) PQB)\nsim&R (\u03a0S-D-D P&) (recvS PQA) (recv PQB) = recv \u03bb m \u2192 sim& (P& m) (PQA m) (PQB m)\nsim&R (\u03a0S-D-D P&) (recvS PQA) (send m PQB) = send m (sim& (P& m) (PQA m) PQB)\nsim&R (\u03a3D\u03a0D\u03a3S P&) (send m PQA) (recv PQB) = sendS m (sim& (P& m) PQA (PQB m))\nsim&R &end _ ()\n-}\n\n{-\nsim&R (\u03a3D\u03a0D\u03a3S x) (send x\u2081 x\u2082) (recv x\u2083) = {!!}\n-}\n\n{-\nsim& : \u2200 {PA PB PAB QA QB QAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 [ QA & QB \u2261 QAB ] \u2192 Sim PA QA \u2192 Sim PB QB \u2192 Sim PAB QAB\nsim&L : \u2200 {PA PB PAB QA QB QAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 [ QA & QB \u2261 QAB ] \u2192 SimL PA QA \u2192 SimL PB QB \u2192 SimL PAB QAB\nsim&R : \u2200 {PA PB PAB QA QB QAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 [ QA & QB \u2261 QAB ] \u2192 SimR PA QA \u2192 SimR PB QB \u2192 SimR PAB QAB\n\nsim& P& Q& (left PQA) (left PQB) = left (sim&L P& Q& PQA PQB)\nsim& P& Q& (left PQA) (right PQB) = {!!}\nsim& P& Q& (left PQA) end = {!!}\nsim& P& Q& (right x) PQB = {!!}\nsim& P& Q& end PQB = {!!}\n\nsim&L (\u03a0D\u03a3D\u03a3S P&) Q& (recv PQA) (send m PQB) = sendS m (sim& (P& m) Q& (PQA m) PQB)\nsim&L (\u03a0D\u03a0D\u03a0D P&) Q& (recv PQA) (recv PQB) = recv \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&L (\u03a0S-S-S P&) Q& (recvS PQA) (recvS PQB) = recvS \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&L (\u03a0S-S-S P&) Q& (recvS PQA) (sendS m PQB) = sendS m (sim& (P& m) Q& (PQA m) PQB)\nsim&L (\u03a0S-D-D P&) Q& (recvS PQA) (recv PQB) = recv \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&L (\u03a0S-D-D P&) Q& (recvS PQA) (send m PQB) = send m (sim& (P& m) Q& (PQA m) PQB)\nsim&L (\u03a3D\u03a0D\u03a3S P&) Q& (send m PQA) (recv PQB) = sendS m (sim& (P& m) Q& PQA (PQB m))\nsim&L &end Q& PQA ()\n\n{-\nsim&R (\u03a0D\u03a3D\u03a3S P&) Q& (recv PQA) (send m PQB) = ?\nsim&R (\u03a0D\u03a0D\u03a0D P&) Q& (recv PQA) (recv PQB) = ? -- recv \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) Q& (recvS PQA) (recvS PQB) = ? -- recvS \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) Q& (recvS PQA) (sendS m PQB) = ? -- sendS m (sim& (P& m) Q& (PQA m) PQB)\nsim&R (\u03a0S-D-D P&) Q& (recvS PQA) (recv PQB) = ? -- recv \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&R (\u03a0S-D-D P&) Q& (recvS PQA) (send m PQB) = ? -- send m (sim& (P& m) Q& (PQA m) PQB)\nsim&R (\u03a3D\u03a0D\u03a3S P&) Q& (send m PQA) (recv PQB) = ? -- sendS m (sim& (P& m) Q& PQA (PQB m))\nsim&R end& Q& PQA PQB = ?\n-}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0' A B) = right (recv (\u03bb x \u2192 left (send x (sim-id (B x)))))\nsim-id (\u03a3' A B) = left (recv (\u03bb x \u2192 right (send x (sim-id (B x)))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = left (recv (\u03bb m \u2192 right (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = right (recv (\u03bb m \u2192 left (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (left x) QR = left (sim-compL Q-Q' x QR)\nsim-comp Q-Q' (right x) (left x\u2081) = sim-compRL Q-Q' x x\u2081\nsim-comp Q-Q' (right x) (right x\u2081) = right (sim-compR Q-Q' (right x) x\u2081)\nsim-comp () (right x) end\nsim-comp end end QR = QR\n\nsim-compRL end () QR\nsim-compRL (\u03a0\u00b7\u03a3 x\u2081) (recv x) (send x\u2082 x\u2083) = sim-comp (x\u2081 x\u2082) (x x\u2082) x\u2083\nsim-compRL (\u03a3\u00b7\u03a0 x) (send x\u2081 x\u2082) (recv x\u2083) = sim-comp (x x\u2081) x\u2082 (x\u2083 x\u2081)\n\nsim-compL Q-Q' (recv PQ) QR = recv (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR)\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv (\u03bb m \u2192 sim-comp Q-Q' PQ (QR m))\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (left x) = right (sim-symL x)\n!\u02e2 (right x) = left (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process \ud835\udfd9 P\nsim-unit (right (recv P)) = do (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (right (send m P)) = do (send m (sim-unit P))\nsim-unit end = end 0\u2081\n\nmodule 3-way-trace where\n\n mod\u2081 : \u2200 {A A' B : \u2605} \u2192 (A \u2192 A') \u2192 A \u00d7 B \u2192 A' \u00d7 B\n mod\u2081 = \u03bb f \u2192 Data.Product.map f id\n\n mod\u2082 : \u2200 {A B B' : \u2605} \u2192 (B \u2192 B') \u2192 A \u00d7 B \u2192 A \u00d7 B'\n mod\u2082 = \u03bb f \u2192 Data.Product.map id f\n\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (right (send x x\u2082)) (left (recv x\u2083)) Q\u00b7 = mod\u2081 (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (right (recv x)) (left (send x\u2082 x\u2083)) Q\u00b7 = mod\u2081 (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (right (recv x)) (left (send x\u2082 x\u2083)) = mod\u2082 (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (right (send x x\u2082)) (left (recv x\u2083)) = mod\u2082 (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n mod\u2081 : \u2200 {A A' B : \u2605} \u2192 (A \u2192 A') \u2192 A \u00d7 B \u2192 A' \u00d7 B\n mod\u2081 = \u03bb f \u2192 Data.Product.map f id\n\n mod\u2082 : \u2200 {A B B' : \u2605} \u2192 (B \u2192 B') \u2192 A \u00d7 B \u2192 A \u00d7 B'\n mod\u2082 = \u03bb f \u2192 Data.Product.map id f\n\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Tele B \u00d7 Tele E\n trace {end} {end} end = _\n trace {end} {com q M P} (right (send m x)) = mod\u2082 (_,_ m) (trace x)\n trace {com q M P} {end} (left (send m x)) = mod\u2081 (_,_ m) (trace x)\n trace {com q M P} {com q\u2081 M\u2081 P\u2081} (left (send m x)) = mod\u2081 (_,_ m) (trace {_} {com (q\u2081) _ _} x)\n trace {com q M P} {com q\u2081 M\u2081 P\u2081} (right (send m x)) = mod\u2082 (_,_ m) (trace {com q _ _} x)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (left (recv x)) PQ QR = cong (left \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (left (send m x)) PQ QR = cong (left \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (right (recv x)) (left (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (right (send m x)) (left (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (right x) (right (recv x\u2081)) (left (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (right x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (right x) (right (send m x\u2081)) (left (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (right x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (right x) (right x\u2081) (right (recv x\u2082)) = cong (right \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (right x) (right x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (right x) (right x\u2081) (right (send m x\u2082)) = cong (right \u2218 send m) (sim-comp-assoc P-P' Q-Q' (right x) (right x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ A where\n Test : Proto\n Test = com Out A (const end)\n\n s : A \u2192 Sim Test Test\n s m = right (send m (left (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = right (recv (\u03bb m \u2192 left (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (left (recv x)) = cong (left \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (left (send m x)) = cong (left \u2218 send m) (sim-comp-id x)\n sim-comp-id (right (recv x)) = cong (right \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (right (send m x)) = {!cong (right \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n\n\u223c-\u00f8 : \u2200 {P}{s s' : Sim end P} \u2192 s \u223c s' \u2192 s \u2261 s'\n\u223c-\u00f8 s\u223cs' = s\u223cs' end end\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (left (recv x)) = cong (left \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (left (send x x\u2081)) = cong (left \u2218 send x) (sim-!! x\u2081)\n sim-!! (right (recv x)) = cong (right \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (right (send x x\u2081)) = cong (right \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (right (recv x)) (left (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (right (send x\u2081 x\u2082)) (left (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (right x) (right (recv x\u2081))\n = cong (left \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (right x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (right x) (right (send x\u2081 x\u2082))\n = cong (left \u2218 send x\u2081) (sim-comp-!-end Q-Q' (right x) x\u2082)\n sim-comp-!-end end end (right (recv x)) = cong (left \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (right (send m x)) = cong (left \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n{-\n\nunit-sim : \u2200 {P} \u2192 Process \ud835\udfd9 P \u2192 Sim end P\nunit-sim (do (send m x)) = right (send m (unit-sim x))\nunit-sim (do (recv x)) = right (recv (\u03bb m \u2192 unit-sim (x m)))\nunit-sim (end x) = end\n\n{-\ntoEl : \u2200 {P A} \u2192 Process A P \u2192 El P (const A)\ntoEl (end x) = x\ntoEl (do (recv f)) = \u03bb x \u2192 toEl (f x)\ntoEl (do (send m x)) = m , toEl x\n-}\n\nidP : \u2200 {A} \u2192 Process A (\u03a0' A (const end))\nidP = do (recv end)\n\ndual\u00b2 : \u2200 {P A} \u2192 Process A P \u2192 Process A (dual (dual P))\ndual\u00b2 (end x) = end x\ndual\u00b2 (do (recv x)) = do (recv (\u03bb m \u2192 dual\u00b2 (x m)))\ndual\u00b2 (do (send m x)) = do (send m (dual\u00b2 x))\n\napply-sim : \u2200 {P Q} \u2192 Sim P Q \u2192 P \u27f9\u1d3e Q\napply-sim (left (send m x)) (do (recv x\u2081)) = apply-sim x (x\u2081 m)\napply-sim (left (recv x)) (do (send m x\u2081)) = apply-sim (x m) x\u2081\napply-sim (right (send m x)) P\u2082 = do (send m (apply-sim x P\u2082))\napply-sim (right (recv x)) P\u2082 = do (recv (\u03bb m \u2192 apply-sim (x m) P\u2082))\napply-sim end P = P\n\napply-sim' : \u2200 {P Q} \u2192 Sim P Q \u2192 Q \u27f9\u1d3e P -- \u2200 {A} \u2192 Process A Q \u2192 Process A (dual P)\napply-sim' PQ P = apply-sim (sim-sym PQ) P\n\napply : \u2200 {P Q A} \u2192 P \u27f9 Q \u2192 Process A P \u2192 Process A Q\napply PQ P = apply-sim PQ (dual\u00b2 P)\n\nmodule _ (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n apply-comp : \u2200 {P Q R A}(PQ : Sim P Q)(QR : Sim (dual Q) R)(p : Process A (dual P)) \u2192 apply-sim (sim-comp PQ QR) p \u2261 apply QR (apply-sim PQ p)\n apply-comp (left (send m x)) QR (do (recv x\u2081)) = apply-comp x QR (x\u2081 m)\n apply-comp (left (recv x)) QR (do (send m x\u2081)) = apply-comp (x m) QR x\u2081\n apply-comp (right (send m x)) (left (recv x\u2081)) p = apply-comp x (x\u2081 m) p\n apply-comp (right (send m x)) (right (recv x\u2081)) p = cong (\u03bb X \u2192 do (recv X))\n (funExt (\u03bb m' \u2192 apply-comp (right (send m x)) (x\u2081 m') p))\n apply-comp (right (send m x)) (right (send m\u2081 x\u2081)) p\n rewrite apply-comp (right (send m x)) x\u2081 p = refl\n apply-comp (right (recv x)) (left (send m x\u2081)) p = apply-comp (x m) x\u2081 p\n apply-comp (right (recv x)) (right (send m x\u2081)) p\n rewrite apply-comp (right (recv x)) x\u2081 p = refl\n apply-comp (right (recv x)) (right (recv x\u2081)) p = cong (\u03bb X \u2192 do (recv X))\n (funExt (\u03bb m \u2192 apply-comp (right (recv x)) (x\u2081 m) p))\n apply-comp end QR (do ())\n apply-comp end QR (end x) = refl\n\n{-\n_>>=P_ : \u2200 {A B P}{Q : Tele P \u2192 Proto} \u2192 Process A P \u2192 ((p : Tele P) \u2192 A \u2192 Process B (Q p)) \u2192 Process B (P >>\u2261 Q)\nsend m p >>=P k = send m (p >>=P (\u03bb p\u2081 \u2192 k (m , p\u2081)))\nrecv x >>=P k = recv (\u03bb m \u2192 x m >>=P (\u03bb p \u2192 k (m , p)))\nend x >>=P k = k 0\u2081 x\n\n\n {-\nmodule _ where\n bind-com : (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (dual P) \u2192 Proto)\n (X : Tele (P >>\u2261 Q) \u2192 \u2605)(Y : Tele (dual P >>\u2261 R) \u2192 \u2605)\n \u2192 El (P >>\u2261 Q) X \u2192 El (dual P >>\u2261 R) Y \u2192 ? \u00d7 ?\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"a1609f07a9b1a099aa3b7854089b59bde5f5e80f","subject":"TypeIsos: a few more isos","message":"TypeIsos: a few more isos\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Function\/Related\/TypeIsomorphisms\/NP.agda","new_file":"lib\/Function\/Related\/TypeIsomorphisms\/NP.agda","new_contents":"-- {-# OPTIONS --without-K #-}\nmodule Function.Related.TypeIsomorphisms.NP where\n\nimport Level as L\nopen import Algebra\nimport Algebra.FunctionProperties as FP\nopen L using (Lift; lower; lift)\nopen import Type hiding (\u2605)\nopen import Function using (_\u02e2_; const)\n\nopen import Data.Fin using (Fin; zero; suc; pred)\nopen import Data.Vec.NP using (Vec; []; _\u2237_; uncons; \u2237-uncons)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_) renaming (_^_ to _**_)\nopen import Data.Maybe.NP\nopen import Data.Product.NP renaming (map to map\u00d7)\n--open import Data.Product.N-ary\nopen import Data.Sum renaming (map to map\u228e)\nopen import Data.One\nopen import Data.Zero\nopen import Data.Two using (\ud835\udfda; 0\u2082; 1\u2082; proj; \u2713; not; \u2261\u2192\u2713; \u2261\u2192\u2713not; \u2713\u2192\u2261; \u2713not\u2192\u2261)\n\nimport Function.NP as F\nopen F using (\u03a0)\nimport Function.Equality as FE\nopen FE using (_\u27e8$\u27e9_)\nopen import Function.Related as FR\nopen import Function.Related.TypeIsomorphisms public\nimport Function.Inverse.NP as Inv\nopen Inv using (_\u2194_; _\u2218_; sym; id; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nopen import Relation.Binary\nimport Relation.Binary.Indexed as I\nopen import Data.Indexed using (_\u228e\u00b0_)\nopen import Relation.Binary.Product.Pointwise\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_ ; _\u2262_; _\u2257_)\n\nmodule _ {A : Set} {p q : A \u2192 \ud835\udfda} where\n \u03a3AP : Set\n \u03a3AP = \u03a3 A (\u03bb x \u2192 p x \u2261 1\u2082)\n\n \u03a3AQ : Set\n \u03a3AQ = \u03a3 A (\u03bb x \u2192 q x \u2261 1\u2082)\n\n \u03a3AP\u00acQ : Set\n \u03a3AP\u00acQ = \u03a3 A (\u03bb x \u2192 p x \u2261 1\u2082 \u00d7 q x \u2261 0\u2082)\n\n \u03a3A\u00acPQ : Set\n \u03a3A\u00acPQ = \u03a3 A (\u03bb x \u2192 p x \u2261 0\u2082 \u00d7 q x \u2261 1\u2082)\n\n {-\n module M\n (f : \u03a3AP \u2192 \u03a3AQ)\n (f-1 : \u03a3AQ \u2192 \u03a3AP)\n (f-1f : \u2200 x \u2192 f-1 (f x) \u2261 x)\n (ff-1 : \u2200 x \u2192 f (f-1 x) \u2261 x)\n where\n\n f' : \u03a3AP\u00acQ \u2192 \u03a3A\u00acPQ\n f' (x , (p , nq)) = let y = f (x , p) in proj\u2081 y , {!proj\u2082 y!} , (proj\u2082 y)\n\n f-1' : \u03a3A\u00acPQ \u2192 \u03a3AP\u00acQ\n f-1' = {!!}\n\n f-1f' : \u2200 x \u2192 f-1' (f' x) \u2261 x\n f-1f' = {!!}\n\n ff-1' : \u2200 x \u2192 f' (f-1' x) \u2261 x\n ff-1' = {!!}\n -}\n\n module Work-In-Progress\n (f' : \u03a3AP\u00acQ \u2192 \u03a3A\u00acPQ)\n (f-1' : \u03a3A\u00acPQ \u2192 \u03a3AP\u00acQ)\n (f-1f' : \u2200 x \u2192 f-1' (f' x) \u2261 x)\n (ff-1' : \u2200 x \u2192 f' (f-1' x) \u2261 x)\n where\n\n f : (x : A) \u2192 p x \u2261 1\u2082 \u2192 q x \u2261 0\u2082 \u2192 \u03a3A\u00acPQ\n f x px qx = f' (x , (px , qx))\n\n f-1 : (x : A) \u2192 p x \u2261 0\u2082 \u2192 q x \u2261 1\u2082 \u2192 \u03a3AP\u00acQ\n f-1 x px qx = f-1' (x , (px , qx))\n\n f-1f : \u2200 x px nqx \u2192\n let y = proj\u2082 (f x px nqx) in proj\u2081 (f-1 (proj\u2081 (f x px nqx)) (proj\u2081 y) (proj\u2082 y)) \u2261 x\n f-1f x px nqx = \u2261.cong proj\u2081 (f-1f' (x , (px , nqx)))\n\n ff-1 : \u2200 x px nqx \u2192\n let y = proj\u2082 (f-1 x px nqx) in proj\u2081 (f (proj\u2081 (f-1 x px nqx)) (proj\u2081 y) (proj\u2082 y)) \u2261 x\n ff-1 x px nqx = \u2261.cong proj\u2081 (ff-1' (x , (px , nqx)))\n\n \u03c0' : (x : A) (px qx : \ud835\udfda) \u2192 p x \u2261 px \u2192 q x \u2261 qx \u2192 A\n \u03c0' x 1\u2082 1\u2082 px qx = x\n \u03c0' x 1\u2082 0\u2082 px qx = proj\u2081 (f x px qx)\n \u03c0' x 0\u2082 1\u2082 px qx = proj\u2081 (f-1 x px qx)\n \u03c0' x 0\u2082 0\u2082 px qx = x\n\n \u03c0 : A \u2192 A\n \u03c0 x = \u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n 0\u22621 : 0\u2082 \u2262 1\u2082\n 0\u22621 ()\n\n \u03c001 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px0 : p x \u2261 0\u2082) (qx1 : q x \u2261 1\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 0\u2082 1\u2082 px0 qx1\n \u03c001 x 1\u2082 _ ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym px0) ppx))\n \u03c001 x 0\u2082 1\u2082 ppx qqx px0 qx1 = \u2261.cong\u2082 (\u03bb z1 z2 \u2192 proj\u2081 (f-1 x z1 z2)) (\u2261.proof-irrelevance ppx px0) (\u2261.proof-irrelevance qqx qx1)\n \u03c001 x 0\u2082 0\u2082 ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qqx) qx1))\n\n \u03c010 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px1 : p x \u2261 1\u2082) (qx0 : q x \u2261 0\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 1\u2082 0\u2082 px1 qx0\n \u03c010 x 0\u2082 _ ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym ppx) px1))\n \u03c010 x 1\u2082 0\u2082 ppx qqx px1 qx0 = \u2261.cong\u2082 (\u03bb z1 z2 \u2192 proj\u2081 (f x z1 z2)) (\u2261.proof-irrelevance ppx px1) (\u2261.proof-irrelevance qqx qx0)\n \u03c010 x 1\u2082 1\u2082 ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qx0) qqx))\n\n \u03c0'bb : \u2200 {b} x (px : p x \u2261 b) (qx : q x \u2261 b) ppx qqx ([ppx] : p x \u2261 ppx) ([qqx] : q x \u2261 qqx) \u2192 \u03c0' x ppx qqx [ppx] [qqx] \u2261 x\n \u03c0'bb x px qx 1\u2082 1\u2082 [ppx] [qqx] = \u2261.refl\n \u03c0'bb x px qx 1\u2082 0\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [qqx]) (\u2261.trans qx (\u2261.trans (\u2261.sym px) [ppx]))))\n \u03c0'bb x px qx 0\u2082 1\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [ppx]) (\u2261.trans px (\u2261.trans (\u2261.sym qx) [qqx]))))\n \u03c0'bb x px qx 0\u2082 0\u2082 [ppx] [qqx] = \u2261.refl\n\n \u03c0\u03c0' : \u2200 x px qx [px] [qx] \u2192 let y = (\u03c0' x px qx [px] [qx]) in \u03c0' y (p y) (q y) \u2261.refl \u2261.refl \u2261 x\n \u03c0\u03c0' x 1\u2082 1\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n \u03c0\u03c0' x 1\u2082 0\u2082 px qx = let fx = f x px qx in let pfx = proj\u2081 (proj\u2082 fx) in let qfx = proj\u2082 (proj\u2082 fx) in \u2261.trans (\u03c001 (proj\u2081 fx) (p (proj\u2081 fx)) (q (proj\u2081 fx)) \u2261.refl \u2261.refl pfx qfx) (f-1f x px qx)\n \u03c0\u03c0' x 0\u2082 1\u2082 px qx = let fx = f-1 x px qx in let pfx = proj\u2081 (proj\u2082 fx) in let qfx = proj\u2082 (proj\u2082 fx) in \u2261.trans (\u03c010 (proj\u2081 fx) (p (proj\u2081 fx)) (q (proj\u2081 fx)) \u2261.refl \u2261.refl pfx qfx) (ff-1 x px qx)\n \u03c0\u03c0' x 0\u2082 0\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n\n \u03c0\u03c0 : \u2200 x \u2192 \u03c0 (\u03c0 x) \u2261 x\n \u03c0\u03c0 x = \u03c0\u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n prop' : \u2200 px qx x ([px] : p x \u2261 px) ([qx] : q x \u2261 qx) \u2192 q (\u03c0' x px qx [px] [qx]) \u2261 px\n prop' 1\u2082 1\u2082 x px qx = qx\n prop' 1\u2082 0\u2082 x px qx = proj\u2082 (proj\u2082 (f x px qx))\n prop' 0\u2082 1\u2082 x px qx = proj\u2082 (proj\u2082 (f-1 x px qx))\n prop' 0\u2082 0\u2082 x px qx = qx\n\n prop : \u2200 x \u2192 p x \u2261 q (\u03c0 x)\n prop x = \u2261.sym (prop' (p x) (q x) x \u2261.refl \u2261.refl)\n\nmodule _ {a b f} {A : Set a} {B : A \u2192 Set b}\n (F : (x : A) \u2192 B x \u2192 Set f) where\n\n -- Also called Axiom of dependent choice.\n dep-choice-iso : (\u03a0 A (\u03bb x \u2192 \u03a3 (B x) (F x)))\n \u2194 (\u03a3 (\u03a0 A B) \u03bb f \u2192 \u03a0 A (F \u02e2 f))\n dep-choice-iso = inverses (\u21d2) (uncurry <_,_>) (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n where\n \u21d2 = \u03bb f \u2192 (\u03bb x \u2192 proj\u2081 (f x)) , (\u03bb x \u2192 proj\u2082 (f x))\n\nMaybe-injective : \u2200 {A B : Set} \u2192 Maybe A \u2194 Maybe B \u2192 A \u2194 B\nMaybe-injective f = Iso.iso (g f) (g-empty f)\n module Maybe-injective where\n open Inverse using (injective; left-inverse-of; right-inverse-of)\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (tof-tot : \u2200 x \u2192 to f (just x) \u2262 nothing)\n (fof-tot : \u2200 x \u2192 from f (just x) \u2262 nothing) where\n\n CancelMaybe' : A \u2194 B\n CancelMaybe' = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n\n \u21d2 : _ \u2192 _\n \u21d2 x with to f (just x) | tof-tot x\n \u21d2 x | just y | _ = y\n \u21d2 x | nothing | p = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d0 : _ \u2192 _\n \u21d0 x with from f (just x) | fof-tot x\n \u21d0 x | just y | p = y\n \u21d0 x | nothing | p = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 x with to f (just x)\n | tof-tot x\n | from f (just (\u21d2 x))\n | fof-tot (\u21d2 x)\n | left-inverse-of f (just x)\n | right-inverse-of f (just (\u21d2 x))\n \u21d0\u21d2 x | just x\u2081 | p | just x\u2082 | q | b | c = just-injective (\u2261.trans (\u2261.sym (left-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong (from f) c) b))\n \u21d0\u21d2 x | just x\u2081 | p | nothing | q | _ | _ = \ud835\udfd8-elim (q \u2261.refl)\n \u21d0\u21d2 x | nothing | p | z | q | _ | _ = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 x with from f (just x)\n | fof-tot x\n | to f (just (\u21d0 x))\n | tof-tot (\u21d0 x)\n | right-inverse-of f (just x)\n | left-inverse-of f (just (\u21d0 x))\n \u21d2\u21d0 x | just x\u2081 | p | just x\u2082 | q | b | c = just-injective (\u2261.trans (\u2261.sym (right-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong (to f) c) b))\n \u21d2\u21d0 x | just x\u2081 | p | nothing | q | _ | _ = \ud835\udfd8-elim (q \u2261.refl)\n \u21d2\u21d0 x | nothing | p | z | q | _ | _ = \ud835\udfd8-elim (p \u2261.refl)\n\n module Iso {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (f-empty : to f nothing \u2261 nothing) where\n\n tof-tot : \u2200 x \u2192 to f (just x) \u2262 nothing\n tof-tot x eq with injective f (\u2261.trans eq (\u2261.sym f-empty))\n ... | ()\n\n f-empty' : from f nothing \u2261 nothing\n f-empty' = \u2261.trans (\u2261.sym (\u2261.cong (from f) f-empty)) (left-inverse-of f nothing)\n\n fof-tot : \u2200 x \u2192 from f (just x) \u2262 nothing\n fof-tot x eq with injective (sym f) (\u2261.trans eq (\u2261.sym f-empty'))\n ... | ()\n\n iso : A \u2194 B\n iso = CancelMaybe' f tof-tot fof-tot\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B) where\n\n g : Maybe A \u2194 Maybe B\n g = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n\n \u21d2 : Maybe A \u2192 Maybe B\n \u21d2 (just x) with to f (just x)\n ... | nothing = to f nothing\n ... | just y = just y\n \u21d2 nothing = nothing\n\n \u21d0 : Maybe B \u2192 Maybe A\n \u21d0 (just x) with from f (just x)\n ... | nothing = from f nothing\n ... | just y = just y\n \u21d0 nothing = nothing\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (just x) with to f (just x) | left-inverse-of f (just x)\n \u21d0\u21d2 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d0\u21d2 (just x) | nothing | p with to f nothing | left-inverse-of f nothing\n \u21d0\u21d2 (just x) | nothing | p | just _ | q rewrite q = p\n \u21d0\u21d2 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d0\u21d2 nothing = \u2261.refl\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (just x) with from f (just x) | right-inverse-of f (just x)\n \u21d2\u21d0 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d2\u21d0 (just x) | nothing | p with from f nothing | right-inverse-of f nothing\n \u21d2\u21d0 (just x) | nothing | p | just _ | q rewrite q = p\n \u21d2\u21d0 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d2\u21d0 nothing = \u2261.refl\n\n g-empty : to g nothing \u2261 nothing\n g-empty = \u2261.refl\n\nprivate\n Setoid\u2080 : \u2605 _\n Setoid\u2080 = Setoid L.zero L.zero\n\n\u03a3\u2261\u2194\ud835\udfd9 : \u2200 {a} {A : \u2605 a} x \u2192 \u03a3 A (_\u2261_ x) \u2194 \ud835\udfd9\n\u03a3\u2261\u2194\ud835\udfd9 x = inverses (F.const _) (\u03bb _ \u2192 _ , \u2261.refl)\n helper (\u03bb _ \u2192 \u2261.refl)\n where helper : (y : \u03a3 _ (_\u2261_ x)) \u2192 (x , \u2261.refl) \u2261 y\n helper (.x , \u2261.refl) = \u2261.refl\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : \u03a3 A B \u2192 \u2605 c} where\n curried : \u03a0 (\u03a3 A B) C \u2194 \u03a0 A \u03bb a \u2192 \u03a0 (B a) \u03bb b \u2192 C (a , b)\n curried = inverses curry uncurry (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u2192 \u2605 c} (f : A \u2194 B) where\n private\n left-f = Inverse.left-inverse-of f\n right-f = Inverse.right-inverse-of f\n coe : \u2200 x \u2192 C x \u2192 C (from f (to f x))\n coe x = \u2261.subst C (\u2261.sym (left-f x))\n \u21d2 : \u03a3 A C \u2192 \u03a3 B (C F.\u2218 from f)\n \u21d2 (x , p) = to f x , coe x p\n \u21d0 : \u03a3 B (C F.\u2218 from f) \u2192 \u03a3 A C\n \u21d0 = first (from f)\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , p) rewrite left-f x = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 p = mk\u03a3\u2261 (C F.\u2218 from f) (right-f (proj\u2081 p)) (helper p)\n where\n helper : \u2200 p \u2192 \u2261.subst (C F.\u2218 from f) (right-f (proj\u2081 p)) (coe (proj\u2081 (\u21d0 p)) (proj\u2082 (\u21d0 p))) \u2261 proj\u2082 p\n helper p with to f (from f (proj\u2081 p)) | right-f (proj\u2081 p) | left-f (from f (proj\u2081 p))\n helper _ | ._ | \u2261.refl | \u2261.refl = \u2261.refl\n first-iso : \u03a3 A C \u2194 \u03a3 B (C F.\u2218 from f)\n first-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : B \u2192 \u2605 c} (f : A \u2194 B) where\n sym-first-iso : \u03a3 A (C F.\u2218 to f) \u2194 \u03a3 B C\n sym-first-iso = sym (first-iso (sym f))\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} (f : \u2200 x \u2192 B x \u2194 C x) where\n private\n left-f = Inverse.left-inverse-of F.\u2218 f\n right-f = Inverse.right-inverse-of F.\u2218 f\n \u21d2 : \u03a3 A B \u2192 \u03a3 A C\n \u21d2 = second (to (f _))\n \u21d0 : \u03a3 A C \u2192 \u03a3 A B\n \u21d0 = second (from (f _))\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , y) rewrite left-f x y = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (x , y) rewrite right-f x y = \u2261.refl\n second-iso : \u03a3 A B \u2194 \u03a3 A C\n second-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b} {A : \u2605 a} {B : \u2605 b} (f\u2080 f\u2081 : A \u2194 B) where\n \ud835\udfda\u00d7-second-iso : (\ud835\udfda \u00d7 A) \u2194 (\ud835\udfda \u00d7 B)\n \ud835\udfda\u00d7-second-iso = second-iso {A = \ud835\udfda} {B = const A} {C = const B} (proj (f\u2080 , f\u2081))\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a3 (A \u228e B) C\n T = \u03a3 A (C F.\u2218 inj\u2081) \u228e \u03a3 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 (inj\u2081 x , y) = inj\u2081 (x , y)\n \u21d2 (inj\u2082 x , y) = inj\u2082 (x , y)\n \u21d0 : T \u2192 S\n \u21d0 (inj\u2081 (x , y)) = inj\u2081 x , y\n \u21d0 (inj\u2082 (x , y)) = inj\u2082 x , y\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (inj\u2081 _ , _) = \u2261.refl\n \u21d0\u21d2 (inj\u2082 _ , _) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n \u03a3\u228e-distrib : (\u03a3 (A \u228e B) C) \u2194 (\u03a3 A (C F.\u2218 inj\u2081) \u228e \u03a3 B (C F.\u2218 inj\u2082))\n \u03a3\u228e-distrib = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} where\n private\n S = \u03a3 A (B \u228e\u00b0 C)\n T = \u03a3 A B \u228e \u03a3 A C\n \u21d2 : S \u2192 T\n \u21d2 (x , inj\u2081 y) = inj\u2081 (x , y)\n \u21d2 (x , inj\u2082 y) = inj\u2082 (x , y)\n \u21d0 : T \u2192 S\n \u21d0 (inj\u2081 (x , y)) = x , inj\u2081 y\n \u21d0 (inj\u2082 (x , y)) = x , inj\u2082 y\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (_ , inj\u2081 _) = \u2261.refl\n \u21d0\u21d2 (_ , inj\u2082 _) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n \u03a3-\u228e-hom : \u03a3 A (B \u228e\u00b0 C) \u2194 (\u03a3 A B \u228e \u03a3 A C)\n \u03a3-\u228e-hom = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\n{-\n{- requires extensional equality\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a0 (A \u228e B) C\n T = \u03a0 A (C F.\u2218 inj\u2081) \u00d7 \u03a0 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 f = f F.\u2218 inj\u2081 , f F.\u2218 inj\u2082\n \u21d0 : T \u2192 S\n \u21d0 (f , g) = [ f , g ]\n \u21d0\u21d2 : \u2200 f x \u2192 \u21d0 (\u21d2 f) x \u2261 f x\n \u21d0\u21d2 f (inj\u2081 x) = \u2261.refl\n \u21d0\u21d2 f (inj\u2082 y) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (f , g) = \u2261.refl\n\n \u03a0\u00d7-distrib : S \u2194 T\n \u03a0\u00d7-distrib = inverses (\u21d2) (\u21d0) {!\u21d0\u21d2!} \u21d2\u21d0\n-}\n\n\u228e-ICommutativeMonoid : CommutativeMonoid _ _\n\u228e-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u228e-setoid_;\n \u03b5 = \u2261.setoid \ud835\udfd8;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u228e-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \ud835\udfd8) _\u228e-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = [ (\u03bb ()) , F.id ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = inj\u2082\n ; cong = \u2082\u223c\u2082\n }\n ; inverse-of = record\n { left-inverse-of = [ (\u03bb ()) , (\u03bb x \u2192 \u2082\u223c\u2082 refl) ]\n ; right-inverse-of = \u03bb x \u2192 refl\n }\n } where open Setoid A\n cong-to : Setoid._\u2248_ (\u2261.setoid \ud835\udfd8 \u228e-setoid A) =[ _ ]\u21d2 _\u2248_\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y) rewrite x\u223c\u2081y = refl\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = x\u223c\u2082y\n\n assoc : Associative _\u228e-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = [ [ inj\u2081 , inj\u2082 F.\u2218 inj\u2081 ] , inj\u2082 F.\u2218 inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = [ inj\u2081 F.\u2218 inj\u2081 , [ inj\u2081 F.\u2218 inj\u2082 , inj\u2082 ] ]\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = [ [ (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2081\u223c\u2081 (refl A))) , (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2082\u223c\u2082 (refl B)) ) ] , (\u03bb _ \u2192 \u2082\u223c\u2082 (refl C)) ]\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (refl A)) , [ (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2081\u223c\u2081 (refl B))) , (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2082\u223c\u2082 (refl C))) ] ]\n }\n } where\n open Setoid\n cong-to : _\u2248_ ((A \u228e-setoid B) \u228e-setoid C) =[ _ ]\u21d2 _\u2248_ (A \u228e-setoid (B \u228e-setoid C))\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2082 ()))\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 x\u223c\u2081y\n cong-to (\u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2082y)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = \u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)\n\n cong-from : _\u2248_ (A \u228e-setoid (B \u228e-setoid C)) =[ _ ]\u21d2 _\u2248_ ((A \u228e-setoid B) \u228e-setoid C)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 x\u223c\u2081y) = \u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2082 ()))\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 x\u223c\u2082y\n\n comm : Commutative _\u228e-setoid_\n comm A B = record\n { to = swap'\n ; from = swap'\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u228e-setoid B FE.\u27f6 B \u228e-setoid A\n swap' {A} {B} = record\n { _\u27e8$\u27e9_ = [ inj\u2082 , inj\u2081 ]\n ; cong = cong\n } where\n cong : Setoid._\u2248_ (A \u228e-setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (B \u228e-setoid A)\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2082\u223c\u2082 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2081\u223c\u2081 x\u223c\u2082y\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u228e-setoid B) (swap' FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (Setoid.refl A)) , (\u03bb _ \u2192 \u2082\u223c\u2082 (Setoid.refl B)) ]\n\n\u00d7-ICommutativeMonoid : CommutativeMonoid _ _\n\u00d7-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u00d7-setoid_;\n \u03b5 = \u2261.setoid \ud835\udfd9;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u00d7-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \ud835\udfd9) _\u00d7-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2082\n ; cong = proj\u2082\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 _ , x\n ; cong = \u03bb x \u2192 \u2261.refl , x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u2261.refl , (Setoid.refl A)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl A\n }\n }\n\n assoc : Associative _\u00d7-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n ; cong = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n }\n ; from = record\n { _\u27e8$\u27e9_ = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n ; cong = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb _ \u2192 Setoid.refl ((A \u00d7-setoid B) \u00d7-setoid C)\n ; right-inverse-of = \u03bb _ \u2192 Setoid.refl (A \u00d7-setoid (B \u00d7-setoid C))\n }\n }\n\n comm : Commutative _\u00d7-setoid_\n comm A B = record\n { to = swap' {A} {B}\n ; from = swap' {B} {A}\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u00d7-setoid B FE.\u27f6 B \u00d7-setoid A\n swap' {A}{B} = record { _\u27e8$\u27e9_ = swap; cong = swap }\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u00d7-setoid B) (swap' {B} {A} FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = \u03bb x \u2192 Setoid.refl (A \u00d7-setoid B)\n\n\u00d7\u228e-ICommutativeSemiring : CommutativeSemiring _ _\n\u00d7\u228e-ICommutativeSemiring = record\n { _\u2248_ = Inv.Inverse\n ; _+_ = _\u228e-setoid_\n ; _*_ = _\u00d7-setoid_\n ; 0# = \u2261.setoid \ud835\udfd8\n ; 1# = \u2261.setoid \ud835\udfd9\n ; isCommutativeSemiring = record\n { +-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u228e-ICommutativeMonoid\n ; *-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u00d7-ICommutativeMonoid\n ; distrib\u02b3 = distrib\u02b3\n ; zero\u02e1 = zero\u02e1\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n distrib\u02b3 : _\u00d7-setoid_ DistributesOver\u02b3 _\u228e-setoid_\n distrib\u02b3 A B C = record\n { to = record\n { _\u27e8$\u27e9_ = uncurry [ curry inj\u2081 , curry inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = frm\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = uncurry [ (\u03bb x y \u2192 (\u2081\u223c\u2081 (refl B)) , (refl A)) , (\u03bb x y \u2192 (\u2082\u223c\u2082 (refl C)) , (refl A)) ]\n ; right-inverse-of = [ uncurry (\u03bb x y \u2192 \u2081\u223c\u2081 ((refl B {x}) , (refl A {y})))\n , uncurry (\u03bb x y \u2192 \u2082\u223c\u2082 ((refl C {x}) , (refl A {y}))) ]\n }\n } where\n open Setoid\n A' = Carrier A\n B' = Carrier B\n C' = Carrier C\n\n cong-to : _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A) =[ _ ]\u21d2 _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A))\n cong-to (\u2081\u223c\u2082 () , _)\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y , A-rel) = \u2081\u223c\u2081 (x\u223c\u2081y , A-rel)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y , A-rel) = \u2082\u223c\u2082 (x\u223c\u2082y , A-rel)\n\n frm : B' \u00d7 A' \u228e C' \u00d7 A' \u2192 (B' \u228e C') \u00d7 A'\n frm = [ map\u00d7 inj\u2081 F.id , map\u00d7 inj\u2082 F.id ]\n\n cong-from : _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A)) =[ _ ]\u21d2 _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 (B-rel , A-rel)) = \u2081\u223c\u2081 B-rel , A-rel\n cong-from (\u2082\u223c\u2082 (C-rel , A-rel)) = \u2082\u223c\u2082 C-rel , A-rel\n\n zero\u02e1 : LeftZero (\u2261.setoid \ud835\udfd8) _\u00d7-setoid_\n zero\u02e1 A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2081\n ; cong = proj\u2081\n }\n ; from = record\n { _\u27e8$\u27e9_ = \ud835\udfd8-elim\n ; cong = \u03bb { {()} x }\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \ud835\udfd8-elim (proj\u2081 x)\n ; right-inverse-of = \u03bb x \u2192 \ud835\udfd8-elim x\n }\n }\n\nmodule \u00d7-CMon {a} = CommutativeMonoid (\u00d7-CommutativeMonoid FR.bijection a)\nmodule \u228e-CMon {a} = CommutativeMonoid (\u228e-CommutativeMonoid FR.bijection a)\nmodule \u00d7\u228e\u00b0 {a} = CommutativeSemiring (\u00d7\u228e-CommutativeSemiring FR.bijection a)\n\nmodule \u00d7-ICMon = CommutativeMonoid \u00d7-ICommutativeMonoid\nmodule \u228e-ICMon = CommutativeMonoid \u228e-ICommutativeMonoid\nmodule \u00d7\u228e\u00b0I = CommutativeSemiring \u00d7\u228e-ICommutativeSemiring\n\nlift-\u228e : \u2200 {A B : Set} \u2192 Inv.Inverse ((\u2261.setoid A) \u228e-setoid (\u2261.setoid B)) (\u2261.setoid (A \u228e B))\nlift-\u228e {A}{B} = record\n { to = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = cong\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = \u03bb x \u2192 Setoid.reflexive (\u2261.setoid A \u228e-setoid \u2261.setoid B) x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid A \u228e-setoid \u2261.setoid B)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid (A \u228e B))\n }\n } where\n cong : Setoid._\u2248_ (\u2261.setoid A \u228e-setoid \u2261.setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (\u2261.setoid (A \u228e B))\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2261.cong inj\u2081 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2261.cong inj\u2082 x\u223c\u2082y\n\nswap-iso : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 (A \u00d7 B) \u2194 (B \u00d7 A)\nswap-iso = inverses swap swap (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u00d7 B \u2192 \u2605 c} where\n \u03a3\u00d7-swap : \u03a3 (A \u00d7 B) C \u2194 \u03a3 (B \u00d7 A) (C F.\u2218 swap)\n \u03a3\u00d7-swap = first-iso swap-iso\n\nMaybe\u2194Lift\ud835\udfd9\u228e : \u2200 {\u2113 a} {A : \u2605 a} \u2192 Maybe A \u2194 (Lift {\u2113 = \u2113} \ud835\udfd9 \u228e A)\nMaybe\u2194Lift\ud835\udfd9\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe\u2194\ud835\udfd9\u228e : \u2200 {a} {A : \u2605 a} \u2192 Maybe A \u2194 (\ud835\udfd9 \u228e A)\nMaybe\u2194\ud835\udfd9\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 A \u2194 B \u2192 Maybe A \u2194 Maybe B\nMaybe-cong A\u2194B = sym Maybe\u2194\ud835\udfd9\u228e \u2218 id \u228e-cong A\u2194B \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe : \u2200 {a} {A : \u2605 a} n \u2192 Maybe (Maybe^ n A) \u2194 Maybe^ n (Maybe A)\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe zero = id\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe (suc n) = Maybe-cong (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n)\n\nMaybe^-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} n \u2192 A \u2194 B \u2192 Maybe^ n A \u2194 Maybe^ n B\nMaybe^-cong zero = F.id\nMaybe^-cong (suc n) = Maybe-cong F.\u2218 Maybe^-cong n\n\nVec0\u2194Lift\ud835\udfd9 : \u2200 {a \u2113} {A : \u2605 a} \u2192 Vec A 0 \u2194 Lift {_} {\u2113} \ud835\udfd9\nVec0\u2194Lift\ud835\udfd9 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec0\u2194\ud835\udfd9 : \u2200 {a} {A : \u2605 a} \u2192 Vec A 0 \u2194 \ud835\udfd9\nVec0\u2194\ud835\udfd9 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec\u2218suc\u2194A\u00d7Vec : \u2200 {a} {A : \u2605 a} {n} \u2192 Vec A (suc n) \u2194 (A \u00d7 Vec A n)\nVec\u2218suc\u2194A\u00d7Vec\n = inverses uncons (uncurry _\u2237_) \u2237-uncons (\u03bb _ \u2192 \u2261.refl)\n\ninfix 8 _^_\n\n_^_ : \u2200 {a} \u2192 \u2605 a \u2192 \u2115 \u2192 \u2605 a\nA ^ 0 = Lift \ud835\udfd9\nA ^ suc n = A \u00d7 A ^ n\n\n^\u2194Vec : \u2200 {a} {A : \u2605 a} n \u2192 (A ^ n) \u2194 Vec A n\n^\u2194Vec zero = sym Vec0\u2194Lift\ud835\udfd9\n^\u2194Vec (suc n) = sym Vec\u2218suc\u2194A\u00d7Vec \u2218 (id \u00d7-cong (^\u2194Vec n))\n\nFin0\u2194\ud835\udfd8 : Fin 0 \u2194 \ud835\udfd8\nFin0\u2194\ud835\udfd8 = inverses (\u03bb()) (\u03bb()) (\u03bb()) (\u03bb())\n\nFin1\u2194\ud835\udfd9 : Fin 1 \u2194 \ud835\udfd9\nFin1\u2194\ud835\udfd9 = inverses _ (\u03bb _ \u2192 zero) \u21d0\u21d2 (\u03bb _ \u2192 \u2261.refl)\n where \u21d0\u21d2 : (_ : Fin 1) \u2192 _\n \u21d0\u21d2 zero = \u2261.refl\n \u21d0\u21d2 (suc ())\n\nFin\u2218suc\u2194Maybe\u2218Fin : \u2200 {n} \u2192 Fin (suc n) \u2194 Maybe (Fin n)\nFin\u2218suc\u2194Maybe\u2218Fin {n}\n = inverses to' (maybe suc zero)\n (\u03bb { zero \u2192 \u2261.refl ; (suc n) \u2192 \u2261.refl })\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n where to' : Fin (suc n) \u2192 Maybe (Fin n)\n to' zero = nothing\n to' (suc n) = just n\n\nFin-injective : \u2200 {m n} \u2192 Fin m \u2194 Fin n \u2192 m \u2261 n\nFin-injective = go _ _ where\n go : \u2200 m n \u2192 Fin m \u2194 Fin n \u2192 m \u2261 n\n go zero zero iso = \u2261.refl\n go zero (suc n) iso with from iso zero\n ... | ()\n go (suc m) zero iso with to iso zero\n ... | ()\n go (suc m) (suc n) iso = \u2261.cong suc (go m n (Maybe-injective (Fin\u2218suc\u2194Maybe\u2218Fin \u2218 iso \u2218 sym Fin\u2218suc\u2194Maybe\u2218Fin)))\n\nLift\u2194id : \u2200 {a} {A : \u2605 a} \u2192 Lift {a} {a} A \u2194 A\nLift\u2194id = inverses lower lift (\u03bb { (lift x) \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\n\ud835\udfd9\u00d7A\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd9 \u00d7 A) \u2194 A\n\ud835\udfd9\u00d7A\u2194A = proj\u2081 \u00d7-CMon.identity _ \u2218 sym Lift\u2194id \u00d7-cong id\n\nA\u00d7\ud835\udfd9\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u00d7 \ud835\udfd9) \u2194 A\nA\u00d7\ud835\udfd9\u2194A = proj\u2082 \u00d7-CMon.identity _ \u2218 id \u00d7-cong sym Lift\u2194id\n\n\u03a0\ud835\udfd9F\u2194F : \u2200 {\u2113} {F : \ud835\udfd9 \u2192 \u2605_ \u2113} \u2192 \u03a0 \ud835\udfd9 F \u2194 F _\n\u03a0\ud835\udfd9F\u2194F = inverses (\u03bb x \u2192 x _) const (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\n\ud835\udfd9\u2192A\u2194A : \u2200 {\u2113} {A : \u2605_ \u2113} \u2192 (\ud835\udfd9 \u2192 A) \u2194 A\n\ud835\udfd9\u2192A\u2194A = \u03a0\ud835\udfd9F\u2194F\n\nmodule _ {a} {A : \u2605_ a} (ext\ud835\udfd8 : (f g : \ud835\udfd8 \u2192 A) \u2192 f \u2261 g) where\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 : (\ud835\udfd8 \u2192 A) \u2194 \ud835\udfd9\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 = inverses _ (const (\u03bb())) (ext\ud835\udfd8 (\u03bb ())) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {F : \ud835\udfd8 \u2192 \u2605_ \u2113} (ext\ud835\udfd8 : (f g : \u03a0 \ud835\udfd8 F) \u2192 f \u2261 g) where\n \u03a0\ud835\udfd8F\u2194\ud835\udfd9 : \u03a0 \ud835\udfd8 F \u2194 \ud835\udfd9\n \u03a0\ud835\udfd8F\u2194\ud835\udfd9 = inverses _ (const (\u03bb())) (ext\ud835\udfd8 (\u03bb ())) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {F : \ud835\udfda \u2192 \u2605_ \u2113} (ext\ud835\udfda : {f g : \u03a0 \ud835\udfda F} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 : \u03a0 \ud835\udfda F \u2194 (F 0\u2082 \u00d7 F 1\u2082)\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 = inverses (\u03bb f \u2192 f 0\u2082 , f 1\u2082) proj\n (\u03bb f \u2192 ext\ud835\udfda (\u03bb { 0\u2082 \u2192 \u2261.refl ; 1\u2082 \u2192 \u2261.refl }))\n (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {A : \u2605_ \u2113} (ext\ud835\udfda : {f g : \ud835\udfda \u2192 A} \u2192 f \u2257 g \u2192 f \u2261 g) where\n \ud835\udfda\u2192A\u2194A\u00d7A : (\ud835\udfda \u2192 A) \u2194 (A \u00d7 A)\n \ud835\udfda\u2192A\u2194A\u00d7A = \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 ext\ud835\udfda\n\n\ud835\udfd8\u228eA\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd8 \u228e A) \u2194 A\n\ud835\udfd8\u228eA\u2194A = proj\u2081 \u228e-CMon.identity _ \u2218 sym Lift\u2194id \u228e-cong id\n\nA\u228e\ud835\udfd8\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u228e \ud835\udfd8) \u2194 A\nA\u228e\ud835\udfd8\u2194A = proj\u2082 \u228e-CMon.identity _ \u2218 id \u228e-cong sym Lift\u2194id\n\n\ud835\udfd8\u00d7A\u2194\ud835\udfd8 : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd8 \u00d7 A) \u2194 \ud835\udfd8\n\ud835\udfd8\u00d7A\u2194\ud835\udfd8 = Lift\u2194id \u2218 proj\u2081 \u00d7\u228e\u00b0.zero _ \u2218 sym (Lift\u2194id \u00d7-cong id)\n\nMaybe\ud835\udfd8\u2194\ud835\udfd9 : Maybe \ud835\udfd8 \u2194 \ud835\udfd9\nMaybe\ud835\udfd8\u2194\ud835\udfd9 = A\u228e\ud835\udfd8\u2194A \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe^\ud835\udfd8\u2194Fin : \u2200 n \u2192 Maybe^ n \ud835\udfd8 \u2194 Fin n\nMaybe^\ud835\udfd8\u2194Fin zero = sym Fin0\u2194\ud835\udfd8\nMaybe^\ud835\udfd8\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\ud835\udfd8\u2194Fin n)\n\nMaybe^\ud835\udfd9\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \ud835\udfd9 \u2194 Fin (suc n)\nMaybe^\ud835\udfd9\u2194Fin1+ n = Maybe^\ud835\udfd8\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\ud835\udfd8\u2194\ud835\udfd9)\n\nMaybe-\u228e : \u2200 {a} {A B : \u2605 a} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e {a} = sym Maybe\u2194Lift\ud835\udfd9\u228e \u2218 \u228e-CMon.assoc (Lift {_} {a} \ud835\udfd9) _ _ \u2218 (Maybe\u2194Lift\ud835\udfd9\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \ud835\udfd8 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \ud835\udfd8\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n\n\u03a3\ud835\udfda\u2194\u228e : \u2200 {a} (F : \ud835\udfda \u2192 \u2605_ a) \u2192 \u03a3 \ud835\udfda F \u2194 (F 0\u2082 \u228e F 1\u2082)\n\u03a3\ud835\udfda\u2194\u228e F = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d2 : (x : \u03a3 _ _) \u2192 _\n \u21d2 (0\u2082 , p) = inj\u2081 p\n \u21d2 (1\u2082 , p) = inj\u2082 p\n \u21d0 : (x : _ \u228e _) \u2192 _\n \u21d0 (inj\u2081 x) = 0\u2082 , x\n \u21d0 (inj\u2082 y) = 1\u2082 , y\n\n \u21d0\u21d2 : (_ : \u03a3 _ _) \u2192 _\n \u21d0\u21d2 (0\u2082 , p) = \u2261.refl\n \u21d0\u21d2 (1\u2082 , p) = \u2261.refl\n \u21d2\u21d0 : (_ : _ \u228e _) \u2192 _\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n\ud835\udfda\u2194\ud835\udfd9\u228e\ud835\udfd9 : \ud835\udfda \u2194 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2194\ud835\udfd9\u228e\ud835\udfd9 = inverses (proj (inj\u2081 _ , inj\u2082 _)) [ F.const 0\u2082 , F.const 1\u2082 ] \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d0\u21d2 : (_ : \ud835\udfda) \u2192 _\n \u21d0\u21d2 0\u2082 = \u2261.refl\n \u21d0\u21d2 1\u2082 = \u2261.refl\n \u21d2\u21d0 : (_ : \ud835\udfd9 \u228e \ud835\udfd9) \u2192 _\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\nFin-\u228e-+ : \u2200 m n \u2192 (Fin m \u228e Fin n) \u2194 Fin (m + n)\nFin-\u228e-+ m n = Maybe^\ud835\udfd8\u2194Fin (m + n)\n \u2218 Maybe^-\u228e-+ m n\n \u2218 sym (Maybe^\ud835\udfd8\u2194Fin m \u228e-cong Maybe^\ud835\udfd8\u2194Fin n)\n\nFin\u2218suc\u2194\ud835\udfd9\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2194 (\ud835\udfd9 \u228e Fin n)\nFin\u2218suc\u2194\ud835\udfd9\u228eFin = Maybe\u2194\ud835\udfd9\u228e \u2218 Fin\u2218suc\u2194Maybe\u2218Fin\n\nFin-\u00d7-* : \u2200 m n \u2192 (Fin m \u00d7 Fin n) \u2194 Fin (m * n)\nFin-\u00d7-* zero n = (Fin 0 \u00d7 Fin n) \u2194\u27e8 Fin0\u2194\ud835\udfd8 \u00d7-cong id \u27e9\n (\ud835\udfd8 \u00d7 Fin n) \u2194\u27e8 \ud835\udfd8\u00d7A\u2194\ud835\udfd8 \u27e9\n \ud835\udfd8 \u2194\u27e8 sym Fin0\u2194\ud835\udfd8 \u27e9\n Fin 0 \u220e\n where open EquationalReasoning hiding (sym)\nFin-\u00d7-* (suc m) n = (Fin (suc m) \u00d7 Fin n) \u2194\u27e8 Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u00d7-cong id \u27e9\n ((\ud835\udfd9 \u228e Fin m) \u00d7 Fin n) \u2194\u27e8 \u00d7\u228e\u00b0.distrib\u02b3 (Fin n) \ud835\udfd9 (Fin m) \u27e9\n ((\ud835\udfd9 \u00d7 Fin n) \u228e (Fin m \u00d7 Fin n)) \u2194\u27e8 \ud835\udfd9\u00d7A\u2194A \u228e-cong Fin-\u00d7-* m n \u27e9\n (Fin n \u228e Fin (m * n)) \u2194\u27e8 Fin-\u228e-+ n (m * n) \u27e9\n Fin (suc m * n) \u220e\n where open EquationalReasoning hiding (sym)\n\nFin\u228e-injective : \u2200 {A B : Set} n \u2192 (Fin n \u228e A) \u2194 (Fin n \u228e B) \u2192 A \u2194 B\nFin\u228e-injective zero f = \ud835\udfd8\u228eA\u2194A \u2218 Fin0\u2194\ud835\udfd8 \u228e-cong id \u2218 f \u2218 sym Fin0\u2194\ud835\udfd8 \u228e-cong id \u2218 sym \ud835\udfd8\u228eA\u2194A\nFin\u228e-injective (suc n) f =\n Fin\u228e-injective n\n (Maybe-injective\n (sym Maybe\u2194\ud835\udfd9\u228e \u2218\n \u228e-CMon.assoc _ _ _ \u2218\n Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u228e-cong id \u2218\n f \u2218\n sym Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u228e-cong id \u2218\n sym (\u228e-CMon.assoc _ _ _) \u2218\n Maybe\u2194\ud835\udfd9\u228e))\n\n{-\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"-- {-# OPTIONS --without-K #-}\nmodule Function.Related.TypeIsomorphisms.NP where\n\nimport Level as L\nopen import Algebra\nimport Algebra.FunctionProperties as FP\nopen L using (Lift; lower; lift)\nopen import Type hiding (\u2605)\nopen import Function using (_\u02e2_; const)\n\nopen import Data.Fin using (Fin; zero; suc; pred)\nopen import Data.Vec.NP using (Vec; []; _\u2237_; uncons; \u2237-uncons)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_) renaming (_^_ to _**_)\nopen import Data.Maybe.NP\nopen import Data.Product.NP renaming (map to map\u00d7)\n--open import Data.Product.N-ary\nopen import Data.Sum renaming (map to map\u228e)\nopen import Data.One\nopen import Data.Zero\nopen import Data.Two using (\ud835\udfda; 0\u2082; 1\u2082; proj)\n\nimport Function.NP as F\nopen F using (\u03a0)\nimport Function.Equality as FE\nopen FE using (_\u27e8$\u27e9_)\nopen import Function.Related as FR\nopen import Function.Related.TypeIsomorphisms public\nimport Function.Inverse.NP as Inv\nopen Inv using (_\u2194_; _\u2218_; sym; id; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nopen import Relation.Binary\nimport Relation.Binary.Indexed as I\nopen import Relation.Binary.Product.Pointwise\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_ ; _\u2262_; _\u2257_)\n\nmodule _ {a b f} {A : Set a} {B : A \u2192 Set b}\n (F : (x : A) \u2192 B x \u2192 Set f) where\n\n -- Also called Axiom of dependent choice.\n dep-choice-iso : (\u03a0 A (\u03bb x \u2192 \u03a3 (B x) (F x)))\n \u2194 (\u03a3 (\u03a0 A B) \u03bb f \u2192 \u03a0 A (F \u02e2 f))\n dep-choice-iso = inverses (\u21d2) (uncurry <_,_>) (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n where\n \u21d2 = \u03bb f \u2192 (\u03bb x \u2192 proj\u2081 (f x)) , (\u03bb x \u2192 proj\u2082 (f x))\n\nMaybe-injective : \u2200 {A B : Set} \u2192 Maybe A \u2194 Maybe B \u2192 A \u2194 B\nMaybe-injective f = Iso.iso (g f) (g-empty f)\n module Maybe-injective where\n open Inverse using (injective; left-inverse-of; right-inverse-of)\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (tof-tot : \u2200 x \u2192 to f (just x) \u2262 nothing)\n (fof-tot : \u2200 x \u2192 from f (just x) \u2262 nothing) where\n\n CancelMaybe' : A \u2194 B\n CancelMaybe' = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n\n \u21d2 : _ \u2192 _\n \u21d2 x with to f (just x) | tof-tot x\n \u21d2 x | just y | _ = y\n \u21d2 x | nothing | p = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d0 : _ \u2192 _\n \u21d0 x with from f (just x) | fof-tot x\n \u21d0 x | just y | p = y\n \u21d0 x | nothing | p = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 x with to f (just x)\n | tof-tot x\n | from f (just (\u21d2 x))\n | fof-tot (\u21d2 x)\n | left-inverse-of f (just x)\n | right-inverse-of f (just (\u21d2 x))\n \u21d0\u21d2 x | just x\u2081 | p | just x\u2082 | q | b | c = just-injective (\u2261.trans (\u2261.sym (left-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong (from f) c) b))\n \u21d0\u21d2 x | just x\u2081 | p | nothing | q | _ | _ = \ud835\udfd8-elim (q \u2261.refl)\n \u21d0\u21d2 x | nothing | p | z | q | _ | _ = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 x with from f (just x)\n | fof-tot x\n | to f (just (\u21d0 x))\n | tof-tot (\u21d0 x)\n | right-inverse-of f (just x)\n | left-inverse-of f (just (\u21d0 x))\n \u21d2\u21d0 x | just x\u2081 | p | just x\u2082 | q | b | c = just-injective (\u2261.trans (\u2261.sym (right-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong (to f) c) b))\n \u21d2\u21d0 x | just x\u2081 | p | nothing | q | _ | _ = \ud835\udfd8-elim (q \u2261.refl)\n \u21d2\u21d0 x | nothing | p | z | q | _ | _ = \ud835\udfd8-elim (p \u2261.refl)\n\n module Iso {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (f-empty : to f nothing \u2261 nothing) where\n\n tof-tot : \u2200 x \u2192 to f (just x) \u2262 nothing\n tof-tot x eq with injective f (\u2261.trans eq (\u2261.sym f-empty))\n ... | ()\n\n f-empty' : from f nothing \u2261 nothing\n f-empty' = \u2261.trans (\u2261.sym (\u2261.cong (from f) f-empty)) (left-inverse-of f nothing)\n\n fof-tot : \u2200 x \u2192 from f (just x) \u2262 nothing\n fof-tot x eq with injective (sym f) (\u2261.trans eq (\u2261.sym f-empty'))\n ... | ()\n\n iso : A \u2194 B\n iso = CancelMaybe' f tof-tot fof-tot\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B) where\n\n g : Maybe A \u2194 Maybe B\n g = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n\n \u21d2 : Maybe A \u2192 Maybe B\n \u21d2 (just x) with to f (just x)\n ... | nothing = to f nothing\n ... | just y = just y\n \u21d2 nothing = nothing\n\n \u21d0 : Maybe B \u2192 Maybe A\n \u21d0 (just x) with from f (just x)\n ... | nothing = from f nothing\n ... | just y = just y\n \u21d0 nothing = nothing\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (just x) with to f (just x) | left-inverse-of f (just x)\n \u21d0\u21d2 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d0\u21d2 (just x) | nothing | p with to f nothing | left-inverse-of f nothing\n \u21d0\u21d2 (just x) | nothing | p | just _ | q rewrite q = p\n \u21d0\u21d2 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d0\u21d2 nothing = \u2261.refl\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (just x) with from f (just x) | right-inverse-of f (just x)\n \u21d2\u21d0 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d2\u21d0 (just x) | nothing | p with from f nothing | right-inverse-of f nothing\n \u21d2\u21d0 (just x) | nothing | p | just _ | q rewrite q = p\n \u21d2\u21d0 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d2\u21d0 nothing = \u2261.refl\n\n g-empty : to g nothing \u2261 nothing\n g-empty = \u2261.refl\n\nprivate\n Setoid\u2080 : \u2605 _\n Setoid\u2080 = Setoid L.zero L.zero\n\n\u03a3\u2261\u2194\ud835\udfd9 : \u2200 {a} {A : \u2605 a} x \u2192 \u03a3 A (_\u2261_ x) \u2194 \ud835\udfd9\n\u03a3\u2261\u2194\ud835\udfd9 x = inverses (F.const _) (\u03bb _ \u2192 _ , \u2261.refl)\n helper (\u03bb _ \u2192 \u2261.refl)\n where helper : (y : \u03a3 _ (_\u2261_ x)) \u2192 (x , \u2261.refl) \u2261 y\n helper (.x , \u2261.refl) = \u2261.refl\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : \u03a3 A B \u2192 \u2605 c} where\n curried : \u03a0 (\u03a3 A B) C \u2194 \u03a0 A \u03bb a \u2192 \u03a0 (B a) \u03bb b \u2192 C (a , b)\n curried = inverses curry uncurry (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u2192 \u2605 c} (f : A \u2194 B) where\n private\n left-f = Inverse.left-inverse-of f\n right-f = Inverse.right-inverse-of f\n coe : \u2200 x \u2192 C x \u2192 C (from f (to f x))\n coe x = \u2261.subst C (\u2261.sym (left-f x))\n \u21d2 : \u03a3 A C \u2192 \u03a3 B (C F.\u2218 from f)\n \u21d2 (x , p) = to f x , coe x p\n \u21d0 : \u03a3 B (C F.\u2218 from f) \u2192 \u03a3 A C\n \u21d0 = first (from f)\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , p) rewrite left-f x = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 p = mk\u03a3\u2261 (C F.\u2218 from f) (right-f (proj\u2081 p)) (helper p)\n where\n helper : \u2200 p \u2192 \u2261.subst (C F.\u2218 from f) (right-f (proj\u2081 p)) (coe (proj\u2081 (\u21d0 p)) (proj\u2082 (\u21d0 p))) \u2261 proj\u2082 p\n helper p with to f (from f (proj\u2081 p)) | right-f (proj\u2081 p) | left-f (from f (proj\u2081 p))\n helper _ | ._ | \u2261.refl | \u2261.refl = \u2261.refl\n first-iso : \u03a3 A C \u2194 \u03a3 B (C F.\u2218 from f)\n first-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : B \u2192 \u2605 c} (f : A \u2194 B) where\n sym-first-iso : \u03a3 A (C F.\u2218 to f) \u2194 \u03a3 B C\n sym-first-iso = sym (first-iso (sym f))\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} (f : \u2200 x \u2192 B x \u2194 C x) where\n private\n left-f = Inverse.left-inverse-of F.\u2218 f\n right-f = Inverse.right-inverse-of F.\u2218 f\n \u21d2 : \u03a3 A B \u2192 \u03a3 A C\n \u21d2 = second (to (f _))\n \u21d0 : \u03a3 A C \u2192 \u03a3 A B\n \u21d0 = second (from (f _))\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , y) rewrite left-f x y = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (x , y) rewrite right-f x y = \u2261.refl\n second-iso : \u03a3 A B \u2194 \u03a3 A C\n second-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b} {A : \u2605 a} {B : \u2605 b} (f\u2080 f\u2081 : A \u2194 B) where\n \ud835\udfda\u00d7-second-iso : (\ud835\udfda \u00d7 A) \u2194 (\ud835\udfda \u00d7 B)\n \ud835\udfda\u00d7-second-iso = second-iso {A = \ud835\udfda} {B = const A} {C = const B} (proj (f\u2080 , f\u2081))\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a3 (A \u228e B) C\n T = \u03a3 A (C F.\u2218 inj\u2081) \u228e \u03a3 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 (inj\u2081 x , y) = inj\u2081 (x , y)\n \u21d2 (inj\u2082 x , y) = inj\u2082 (x , y)\n \u21d0 : T \u2192 S\n \u21d0 (inj\u2081 (x , y)) = inj\u2081 x , y\n \u21d0 (inj\u2082 (x , y)) = inj\u2082 x , y\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (inj\u2081 _ , _) = \u2261.refl\n \u21d0\u21d2 (inj\u2082 _ , _) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n \u03a3\u228e-distrib : (\u03a3 (A \u228e B) C) \u2194 (\u03a3 A (C F.\u2218 inj\u2081) \u228e \u03a3 B (C F.\u2218 inj\u2082))\n \u03a3\u228e-distrib = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\n{- requires extensional equality\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a0 (A \u228e B) C\n T = \u03a0 A (C F.\u2218 inj\u2081) \u00d7 \u03a0 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 f = f F.\u2218 inj\u2081 , f F.\u2218 inj\u2082\n \u21d0 : T \u2192 S\n \u21d0 (f , g) = [ f , g ]\n \u21d0\u21d2 : \u2200 f x \u2192 \u21d0 (\u21d2 f) x \u2261 f x\n \u21d0\u21d2 f (inj\u2081 x) = \u2261.refl\n \u21d0\u21d2 f (inj\u2082 y) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (f , g) = \u2261.refl\n\n \u03a0\u00d7-distrib : S \u2194 T\n \u03a0\u00d7-distrib = inverses (\u21d2) (\u21d0) {!\u21d0\u21d2!} \u21d2\u21d0\n-}\n\n\u228e-ICommutativeMonoid : CommutativeMonoid _ _\n\u228e-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u228e-setoid_;\n \u03b5 = \u2261.setoid \ud835\udfd8;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u228e-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \ud835\udfd8) _\u228e-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = [ (\u03bb ()) , F.id ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = inj\u2082\n ; cong = \u2082\u223c\u2082\n }\n ; inverse-of = record\n { left-inverse-of = [ (\u03bb ()) , (\u03bb x \u2192 \u2082\u223c\u2082 refl) ]\n ; right-inverse-of = \u03bb x \u2192 refl\n }\n } where open Setoid A\n cong-to : Setoid._\u2248_ (\u2261.setoid \ud835\udfd8 \u228e-setoid A) =[ _ ]\u21d2 _\u2248_\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y) rewrite x\u223c\u2081y = refl\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = x\u223c\u2082y\n\n assoc : Associative _\u228e-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = [ [ inj\u2081 , inj\u2082 F.\u2218 inj\u2081 ] , inj\u2082 F.\u2218 inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = [ inj\u2081 F.\u2218 inj\u2081 , [ inj\u2081 F.\u2218 inj\u2082 , inj\u2082 ] ]\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = [ [ (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2081\u223c\u2081 (refl A))) , (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2082\u223c\u2082 (refl B)) ) ] , (\u03bb _ \u2192 \u2082\u223c\u2082 (refl C)) ]\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (refl A)) , [ (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2081\u223c\u2081 (refl B))) , (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2082\u223c\u2082 (refl C))) ] ]\n }\n } where\n open Setoid\n cong-to : _\u2248_ ((A \u228e-setoid B) \u228e-setoid C) =[ _ ]\u21d2 _\u2248_ (A \u228e-setoid (B \u228e-setoid C))\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2082 ()))\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 x\u223c\u2081y\n cong-to (\u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2082y)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = \u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)\n\n cong-from : _\u2248_ (A \u228e-setoid (B \u228e-setoid C)) =[ _ ]\u21d2 _\u2248_ ((A \u228e-setoid B) \u228e-setoid C)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 x\u223c\u2081y) = \u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2082 ()))\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 x\u223c\u2082y\n\n comm : Commutative _\u228e-setoid_\n comm A B = record\n { to = swap'\n ; from = swap'\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u228e-setoid B FE.\u27f6 B \u228e-setoid A\n swap' {A} {B} = record\n { _\u27e8$\u27e9_ = [ inj\u2082 , inj\u2081 ]\n ; cong = cong\n } where\n cong : Setoid._\u2248_ (A \u228e-setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (B \u228e-setoid A)\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2082\u223c\u2082 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2081\u223c\u2081 x\u223c\u2082y\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u228e-setoid B) (swap' FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (Setoid.refl A)) , (\u03bb _ \u2192 \u2082\u223c\u2082 (Setoid.refl B)) ]\n\n\u00d7-ICommutativeMonoid : CommutativeMonoid _ _\n\u00d7-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u00d7-setoid_;\n \u03b5 = \u2261.setoid \ud835\udfd9;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u00d7-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \ud835\udfd9) _\u00d7-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2082\n ; cong = proj\u2082\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 _ , x\n ; cong = \u03bb x \u2192 \u2261.refl , x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u2261.refl , (Setoid.refl A)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl A\n }\n }\n\n assoc : Associative _\u00d7-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n ; cong = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n }\n ; from = record\n { _\u27e8$\u27e9_ = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n ; cong = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb _ \u2192 Setoid.refl ((A \u00d7-setoid B) \u00d7-setoid C)\n ; right-inverse-of = \u03bb _ \u2192 Setoid.refl (A \u00d7-setoid (B \u00d7-setoid C))\n }\n }\n\n comm : Commutative _\u00d7-setoid_\n comm A B = record\n { to = swap' {A} {B}\n ; from = swap' {B} {A}\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u00d7-setoid B FE.\u27f6 B \u00d7-setoid A\n swap' {A}{B} = record { _\u27e8$\u27e9_ = swap; cong = swap }\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u00d7-setoid B) (swap' {B} {A} FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = \u03bb x \u2192 Setoid.refl (A \u00d7-setoid B)\n\n\u00d7\u228e-ICommutativeSemiring : CommutativeSemiring _ _\n\u00d7\u228e-ICommutativeSemiring = record\n { _\u2248_ = Inv.Inverse\n ; _+_ = _\u228e-setoid_\n ; _*_ = _\u00d7-setoid_\n ; 0# = \u2261.setoid \ud835\udfd8\n ; 1# = \u2261.setoid \ud835\udfd9\n ; isCommutativeSemiring = record\n { +-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u228e-ICommutativeMonoid\n ; *-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u00d7-ICommutativeMonoid\n ; distrib\u02b3 = distrib\u02b3\n ; zero\u02e1 = zero\u02e1\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n distrib\u02b3 : _\u00d7-setoid_ DistributesOver\u02b3 _\u228e-setoid_\n distrib\u02b3 A B C = record\n { to = record\n { _\u27e8$\u27e9_ = uncurry [ curry inj\u2081 , curry inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = frm\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = uncurry [ (\u03bb x y \u2192 (\u2081\u223c\u2081 (refl B)) , (refl A)) , (\u03bb x y \u2192 (\u2082\u223c\u2082 (refl C)) , (refl A)) ]\n ; right-inverse-of = [ uncurry (\u03bb x y \u2192 \u2081\u223c\u2081 ((refl B {x}) , (refl A {y})))\n , uncurry (\u03bb x y \u2192 \u2082\u223c\u2082 ((refl C {x}) , (refl A {y}))) ]\n }\n } where\n open Setoid\n A' = Carrier A\n B' = Carrier B\n C' = Carrier C\n\n cong-to : _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A) =[ _ ]\u21d2 _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A))\n cong-to (\u2081\u223c\u2082 () , _)\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y , A-rel) = \u2081\u223c\u2081 (x\u223c\u2081y , A-rel)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y , A-rel) = \u2082\u223c\u2082 (x\u223c\u2082y , A-rel)\n\n frm : B' \u00d7 A' \u228e C' \u00d7 A' \u2192 (B' \u228e C') \u00d7 A'\n frm = [ map\u00d7 inj\u2081 F.id , map\u00d7 inj\u2082 F.id ]\n\n cong-from : _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A)) =[ _ ]\u21d2 _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 (B-rel , A-rel)) = \u2081\u223c\u2081 B-rel , A-rel\n cong-from (\u2082\u223c\u2082 (C-rel , A-rel)) = \u2082\u223c\u2082 C-rel , A-rel\n\n zero\u02e1 : LeftZero (\u2261.setoid \ud835\udfd8) _\u00d7-setoid_\n zero\u02e1 A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2081\n ; cong = proj\u2081\n }\n ; from = record\n { _\u27e8$\u27e9_ = \ud835\udfd8-elim\n ; cong = \u03bb { {()} x }\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \ud835\udfd8-elim (proj\u2081 x)\n ; right-inverse-of = \u03bb x \u2192 \ud835\udfd8-elim x\n }\n }\n\nmodule \u00d7-CMon {a} = CommutativeMonoid (\u00d7-CommutativeMonoid FR.bijection a)\nmodule \u228e-CMon {a} = CommutativeMonoid (\u228e-CommutativeMonoid FR.bijection a)\nmodule \u00d7\u228e\u00b0 {a} = CommutativeSemiring (\u00d7\u228e-CommutativeSemiring FR.bijection a)\n\nmodule \u00d7-ICMon = CommutativeMonoid \u00d7-ICommutativeMonoid\nmodule \u228e-ICMon = CommutativeMonoid \u228e-ICommutativeMonoid\nmodule \u00d7\u228e\u00b0I = CommutativeSemiring \u00d7\u228e-ICommutativeSemiring\n\nlift-\u228e : \u2200 {A B : Set} \u2192 Inv.Inverse ((\u2261.setoid A) \u228e-setoid (\u2261.setoid B)) (\u2261.setoid (A \u228e B))\nlift-\u228e {A}{B} = record\n { to = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = cong\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = \u03bb x \u2192 Setoid.reflexive (\u2261.setoid A \u228e-setoid \u2261.setoid B) x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid A \u228e-setoid \u2261.setoid B)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid (A \u228e B))\n }\n } where\n cong : Setoid._\u2248_ (\u2261.setoid A \u228e-setoid \u2261.setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (\u2261.setoid (A \u228e B))\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2261.cong inj\u2081 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2261.cong inj\u2082 x\u223c\u2082y\n\nswap-iso : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 (A \u00d7 B) \u2194 (B \u00d7 A)\nswap-iso = inverses swap swap (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u00d7 B \u2192 \u2605 c} where\n \u03a3\u00d7-swap : \u03a3 (A \u00d7 B) C \u2194 \u03a3 (B \u00d7 A) (C F.\u2218 swap)\n \u03a3\u00d7-swap = first-iso swap-iso\n\nMaybe\u2194Lift\ud835\udfd9\u228e : \u2200 {\u2113 a} {A : \u2605 a} \u2192 Maybe A \u2194 (Lift {\u2113 = \u2113} \ud835\udfd9 \u228e A)\nMaybe\u2194Lift\ud835\udfd9\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe\u2194\ud835\udfd9\u228e : \u2200 {a} {A : \u2605 a} \u2192 Maybe A \u2194 (\ud835\udfd9 \u228e A)\nMaybe\u2194\ud835\udfd9\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 A \u2194 B \u2192 Maybe A \u2194 Maybe B\nMaybe-cong A\u2194B = sym Maybe\u2194\ud835\udfd9\u228e \u2218 id \u228e-cong A\u2194B \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe : \u2200 {a} {A : \u2605 a} n \u2192 Maybe (Maybe^ n A) \u2194 Maybe^ n (Maybe A)\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe zero = id\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe (suc n) = Maybe-cong (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n)\n\nMaybe^-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} n \u2192 A \u2194 B \u2192 Maybe^ n A \u2194 Maybe^ n B\nMaybe^-cong zero = F.id\nMaybe^-cong (suc n) = Maybe-cong F.\u2218 Maybe^-cong n\n\nVec0\u2194Lift\ud835\udfd9 : \u2200 {a \u2113} {A : \u2605 a} \u2192 Vec A 0 \u2194 Lift {_} {\u2113} \ud835\udfd9\nVec0\u2194Lift\ud835\udfd9 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec0\u2194\ud835\udfd9 : \u2200 {a} {A : \u2605 a} \u2192 Vec A 0 \u2194 \ud835\udfd9\nVec0\u2194\ud835\udfd9 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec\u2218suc\u2194A\u00d7Vec : \u2200 {a} {A : \u2605 a} {n} \u2192 Vec A (suc n) \u2194 (A \u00d7 Vec A n)\nVec\u2218suc\u2194A\u00d7Vec\n = inverses uncons (uncurry _\u2237_) \u2237-uncons (\u03bb _ \u2192 \u2261.refl)\n\ninfix 8 _^_\n\n_^_ : \u2200 {a} \u2192 \u2605 a \u2192 \u2115 \u2192 \u2605 a\nA ^ 0 = Lift \ud835\udfd9\nA ^ suc n = A \u00d7 A ^ n\n\n^\u2194Vec : \u2200 {a} {A : \u2605 a} n \u2192 (A ^ n) \u2194 Vec A n\n^\u2194Vec zero = sym Vec0\u2194Lift\ud835\udfd9\n^\u2194Vec (suc n) = sym Vec\u2218suc\u2194A\u00d7Vec \u2218 (id \u00d7-cong (^\u2194Vec n))\n\nFin0\u2194\ud835\udfd8 : Fin 0 \u2194 \ud835\udfd8\nFin0\u2194\ud835\udfd8 = inverses (\u03bb()) (\u03bb()) (\u03bb()) (\u03bb())\n\nFin1\u2194\ud835\udfd9 : Fin 1 \u2194 \ud835\udfd9\nFin1\u2194\ud835\udfd9 = inverses _ (\u03bb _ \u2192 zero) \u21d0\u21d2 (\u03bb _ \u2192 \u2261.refl)\n where \u21d0\u21d2 : (_ : Fin 1) \u2192 _\n \u21d0\u21d2 zero = \u2261.refl\n \u21d0\u21d2 (suc ())\n\nFin\u2218suc\u2194Maybe\u2218Fin : \u2200 {n} \u2192 Fin (suc n) \u2194 Maybe (Fin n)\nFin\u2218suc\u2194Maybe\u2218Fin {n}\n = inverses to' (maybe suc zero)\n (\u03bb { zero \u2192 \u2261.refl ; (suc n) \u2192 \u2261.refl })\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n where to' : Fin (suc n) \u2192 Maybe (Fin n)\n to' zero = nothing\n to' (suc n) = just n\n\nFin-injective : \u2200 {m n} \u2192 Fin m \u2194 Fin n \u2192 m \u2261 n\nFin-injective = go _ _ where\n go : \u2200 m n \u2192 Fin m \u2194 Fin n \u2192 m \u2261 n\n go zero zero iso = \u2261.refl\n go zero (suc n) iso with from iso zero\n ... | ()\n go (suc m) zero iso with to iso zero\n ... | ()\n go (suc m) (suc n) iso = \u2261.cong suc (go m n (Maybe-injective (Fin\u2218suc\u2194Maybe\u2218Fin \u2218 iso \u2218 sym Fin\u2218suc\u2194Maybe\u2218Fin)))\n\nLift\u2194id : \u2200 {a} {A : \u2605 a} \u2192 Lift {a} {a} A \u2194 A\nLift\u2194id = inverses lower lift (\u03bb { (lift x) \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\n\ud835\udfd9\u00d7A\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd9 \u00d7 A) \u2194 A\n\ud835\udfd9\u00d7A\u2194A = proj\u2081 \u00d7-CMon.identity _ \u2218 sym Lift\u2194id \u00d7-cong id\n\nA\u00d7\ud835\udfd9\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u00d7 \ud835\udfd9) \u2194 A\nA\u00d7\ud835\udfd9\u2194A = proj\u2082 \u00d7-CMon.identity _ \u2218 id \u00d7-cong sym Lift\u2194id\n\n\u03a0\ud835\udfd9F\u2194F : \u2200 {\u2113} {F : \ud835\udfd9 \u2192 \u2605_ \u2113} \u2192 \u03a0 \ud835\udfd9 F \u2194 F _\n\u03a0\ud835\udfd9F\u2194F = inverses (\u03bb x \u2192 x _) const (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\n\ud835\udfd9\u2192A\u2194A : \u2200 {\u2113} {A : \u2605_ \u2113} \u2192 (\ud835\udfd9 \u2192 A) \u2194 A\n\ud835\udfd9\u2192A\u2194A = \u03a0\ud835\udfd9F\u2194F\n\nmodule _ {a} {A : \u2605_ a} (ext\ud835\udfd8 : (f g : \ud835\udfd8 \u2192 A) \u2192 f \u2261 g) where\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 : (\ud835\udfd8 \u2192 A) \u2194 \ud835\udfd9\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 = inverses _ (const (\u03bb())) (ext\ud835\udfd8 (\u03bb ())) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {F : \ud835\udfd8 \u2192 \u2605_ \u2113} (ext\ud835\udfd8 : (f g : \u03a0 \ud835\udfd8 F) \u2192 f \u2261 g) where\n \u03a0\ud835\udfd8F\u2194\ud835\udfd9 : \u03a0 \ud835\udfd8 F \u2194 \ud835\udfd9\n \u03a0\ud835\udfd8F\u2194\ud835\udfd9 = inverses _ (const (\u03bb())) (ext\ud835\udfd8 (\u03bb ())) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {F : \ud835\udfda \u2192 \u2605_ \u2113} (ext\ud835\udfda : {f g : \u03a0 \ud835\udfda F} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 : \u03a0 \ud835\udfda F \u2194 (F 0\u2082 \u00d7 F 1\u2082)\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 = inverses (\u03bb f \u2192 f 0\u2082 , f 1\u2082) proj\n (\u03bb f \u2192 ext\ud835\udfda (\u03bb { 0\u2082 \u2192 \u2261.refl ; 1\u2082 \u2192 \u2261.refl }))\n (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {A : \u2605_ \u2113} (ext\ud835\udfda : {f g : \ud835\udfda \u2192 A} \u2192 f \u2257 g \u2192 f \u2261 g) where\n \ud835\udfda\u2192A\u2194A\u00d7A : (\ud835\udfda \u2192 A) \u2194 (A \u00d7 A)\n \ud835\udfda\u2192A\u2194A\u00d7A = \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 ext\ud835\udfda\n\n\ud835\udfd8\u228eA\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd8 \u228e A) \u2194 A\n\ud835\udfd8\u228eA\u2194A = proj\u2081 \u228e-CMon.identity _ \u2218 sym Lift\u2194id \u228e-cong id\n\nA\u228e\ud835\udfd8\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u228e \ud835\udfd8) \u2194 A\nA\u228e\ud835\udfd8\u2194A = proj\u2082 \u228e-CMon.identity _ \u2218 id \u228e-cong sym Lift\u2194id\n\n\ud835\udfd8\u00d7A\u2194\ud835\udfd8 : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd8 \u00d7 A) \u2194 \ud835\udfd8\n\ud835\udfd8\u00d7A\u2194\ud835\udfd8 = Lift\u2194id \u2218 proj\u2081 \u00d7\u228e\u00b0.zero _ \u2218 sym (Lift\u2194id \u00d7-cong id)\n\nMaybe\ud835\udfd8\u2194\ud835\udfd9 : Maybe \ud835\udfd8 \u2194 \ud835\udfd9\nMaybe\ud835\udfd8\u2194\ud835\udfd9 = A\u228e\ud835\udfd8\u2194A \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe^\ud835\udfd8\u2194Fin : \u2200 n \u2192 Maybe^ n \ud835\udfd8 \u2194 Fin n\nMaybe^\ud835\udfd8\u2194Fin zero = sym Fin0\u2194\ud835\udfd8\nMaybe^\ud835\udfd8\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\ud835\udfd8\u2194Fin n)\n\nMaybe^\ud835\udfd9\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \ud835\udfd9 \u2194 Fin (suc n)\nMaybe^\ud835\udfd9\u2194Fin1+ n = Maybe^\ud835\udfd8\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\ud835\udfd8\u2194\ud835\udfd9)\n\nMaybe-\u228e : \u2200 {a} {A B : \u2605 a} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e {a} = sym Maybe\u2194Lift\ud835\udfd9\u228e \u2218 \u228e-CMon.assoc (Lift {_} {a} \ud835\udfd9) _ _ \u2218 (Maybe\u2194Lift\ud835\udfd9\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \ud835\udfd8 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \ud835\udfd8\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n\n\u03a3\ud835\udfda\u2194\u228e : \u2200 {a} (F : \ud835\udfda \u2192 \u2605_ a) \u2192 \u03a3 \ud835\udfda F \u2194 (F 0\u2082 \u228e F 1\u2082)\n\u03a3\ud835\udfda\u2194\u228e F = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d2 : (x : \u03a3 _ _) \u2192 _\n \u21d2 (0\u2082 , p) = inj\u2081 p\n \u21d2 (1\u2082 , p) = inj\u2082 p\n \u21d0 : (x : _ \u228e _) \u2192 _\n \u21d0 (inj\u2081 x) = 0\u2082 , x\n \u21d0 (inj\u2082 y) = 1\u2082 , y\n\n \u21d0\u21d2 : (_ : \u03a3 _ _) \u2192 _\n \u21d0\u21d2 (0\u2082 , p) = \u2261.refl\n \u21d0\u21d2 (1\u2082 , p) = \u2261.refl\n \u21d2\u21d0 : (_ : _ \u228e _) \u2192 _\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n\ud835\udfda\u2194\ud835\udfd9\u228e\ud835\udfd9 : \ud835\udfda \u2194 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2194\ud835\udfd9\u228e\ud835\udfd9 = inverses (proj (inj\u2081 _ , inj\u2082 _)) [ F.const 0\u2082 , F.const 1\u2082 ] \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d0\u21d2 : (_ : \ud835\udfda) \u2192 _\n \u21d0\u21d2 0\u2082 = \u2261.refl\n \u21d0\u21d2 1\u2082 = \u2261.refl\n \u21d2\u21d0 : (_ : \ud835\udfd9 \u228e \ud835\udfd9) \u2192 _\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\nFin-\u228e-+ : \u2200 m n \u2192 (Fin m \u228e Fin n) \u2194 Fin (m + n)\nFin-\u228e-+ m n = Maybe^\ud835\udfd8\u2194Fin (m + n)\n \u2218 Maybe^-\u228e-+ m n\n \u2218 sym (Maybe^\ud835\udfd8\u2194Fin m \u228e-cong Maybe^\ud835\udfd8\u2194Fin n)\n\nFin\u2218suc\u2194\ud835\udfd9\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2194 (\ud835\udfd9 \u228e Fin n)\nFin\u2218suc\u2194\ud835\udfd9\u228eFin = Maybe\u2194\ud835\udfd9\u228e \u2218 Fin\u2218suc\u2194Maybe\u2218Fin\n\nFin-\u00d7-* : \u2200 m n \u2192 (Fin m \u00d7 Fin n) \u2194 Fin (m * n)\nFin-\u00d7-* zero n = (Fin 0 \u00d7 Fin n) \u2194\u27e8 Fin0\u2194\ud835\udfd8 \u00d7-cong id \u27e9\n (\ud835\udfd8 \u00d7 Fin n) \u2194\u27e8 \ud835\udfd8\u00d7A\u2194\ud835\udfd8 \u27e9\n \ud835\udfd8 \u2194\u27e8 sym Fin0\u2194\ud835\udfd8 \u27e9\n Fin 0 \u220e\n where open EquationalReasoning hiding (sym)\nFin-\u00d7-* (suc m) n = (Fin (suc m) \u00d7 Fin n) \u2194\u27e8 Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u00d7-cong id \u27e9\n ((\ud835\udfd9 \u228e Fin m) \u00d7 Fin n) \u2194\u27e8 \u00d7\u228e\u00b0.distrib\u02b3 (Fin n) \ud835\udfd9 (Fin m) \u27e9\n ((\ud835\udfd9 \u00d7 Fin n) \u228e (Fin m \u00d7 Fin n)) \u2194\u27e8 \ud835\udfd9\u00d7A\u2194A \u228e-cong Fin-\u00d7-* m n \u27e9\n (Fin n \u228e Fin (m * n)) \u2194\u27e8 Fin-\u228e-+ n (m * n) \u27e9\n Fin (suc m * n) \u220e\n where open EquationalReasoning hiding (sym)\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"3b9d65fd14f8a676b56dbf7c5ad3ab9ebb8f558c","subject":"Describe correctly relation between function changes","message":"Describe correctly relation between function changes\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/RelateToValidity.agda","new_file":"Thesis\/RelateToValidity.agda","new_contents":"module Thesis.RelateToValidity where\n\nopen import Relation.Binary.PropositionalEquality public hiding ([_])\n\nopen import Thesis.Changes\nopen import Thesis.Lang\n\nmodule _\n {A : Set} {B : Set} {{CA : ChangeStructure A}} {{CB : ChangeStructure B}} where\n\n WellDefinedFunChangePoint : \u2200 (f : A \u2192 B) \u2192 (df : Ch (A \u2192 B)) \u2192 \u2200 a da \u2192 Set\n WellDefinedFunChangePoint f df a da = (f \u2295 df) (a \u2295 da) \u2261 f a \u2295 df a da\n\n WellDefinedFunChangeFromTo\u2032 : \u2200 (f1 : A \u2192 B) \u2192 (df : Ch (A \u2192 B)) \u2192 Set\n WellDefinedFunChangeFromTo\u2032 f1 df = \u2200 da a \u2192 ch da from a to (a \u2295 da) \u2192 WellDefinedFunChangePoint f1 df a da\n\n open \u2261-Reasoning\n\n fromto\u2192WellDefined\u2032 : \u2200 {f1 f2 df} \u2192 ch df from f1 to f2 \u2192\n WellDefinedFunChangeFromTo\u2032 f1 df\n fromto\u2192WellDefined\u2032 {f1 = f1} {f2} {df} dff da a daa =\n begin\n (f1 \u2295 df) (a \u2295 da)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (a \u2295 da)) (fromto\u2192\u2295 df f1 f2 dff)\u27e9\n f2 (a \u2295 da)\n \u2261\u27e8 sym (fromto\u2192\u2295 _ _ _ (dff da _ _ daa)) \u27e9\n f1 a \u2295 df a da\n \u220e\n\n -- This is similar (not equivalent) to the old definition of function changes.\n -- However, such a function need not be defined on invalid changes, unlike the\n -- functions.\n --\n -- Hence this is sort of bigger than \u0394 f, though not really.\n f\u0394 : (A \u2192 B) \u2192 Set\n f\u0394 f = (a : A) \u2192 \u0394 a \u2192 \u0394 (f a)\n\n -- We can indeed map \u0394 f into f\u0394 f, via valid-functions-map-\u0394. If this mapping\n -- were an injection, we could say that f\u0394 f is bigger than \u0394 f. But we'd need\n -- first to quotient \u0394 f to turn valid-functions-map-\u0394 into an injection:\n --\n -- 1. We need to quotient \u0394 f by extensional equivalence of functions.\n -- Luckily, we can just postulate it.\n --\n -- 2. We need to quotient \u0394 f by identifying functions that only differ by\n -- behavior on invalid changes; such functions can't be distinguished after\n -- being injected into f\u0394 f.\n valid-functions-map-\u0394 : \u2200 (f : A \u2192 B) (df : \u0394 f) \u2192 f\u0394 f\n valid-functions-map-\u0394 f (df , dff) a (da , daa) = df a da , valid-res\n where\n valid-res : ch df a da from f a to (f a \u2295 df a da)\n valid-res rewrite sym (fromto\u2192WellDefined\u2032 dff da a daa) = dff da a (a \u2295 da) daa\n\n fromto-functions-map-\u0394 : \u2200 (f1 f2 : A \u2192 B) (df : Ch (A \u2192 B)) \u2192 ch df from f1 to f2 \u2192 f\u0394 f1\n fromto-functions-map-\u0394 f1 f2 df dff a (da , daa) = valid-functions-map-\u0394 f1 (df , dff\u2032) a (da , daa)\n where\n dff\u2032 : ch df from f1 to (f1 \u2295 df)\n dff\u2032 = subst (ch df from f1 to_) (sym (fromto\u2192\u2295 df f1 f2 dff)) dff\n\nopen import Thesis.LangChanges\n\nfromto\u2192WellDefined\u2032Lang : \u2200 {\u03c3 \u03c4 f1 f2 df} \u2192 [ \u03c3 \u21d2 \u03c4 ]\u03c4 df from f1 to f2 \u2192\n WellDefinedFunChangeFromTo\u2032 f1 df\nfromto\u2192WellDefined\u2032Lang {f1 = f1} {f2} {df} dff da a daa =\n fromto\u2192WellDefined\u2032 dff da a daa\n","old_contents":"module Thesis.RelateToValidity where\n\nopen import Relation.Binary.PropositionalEquality public hiding ([_])\n\nopen import Thesis.Changes\nopen import Thesis.Lang\n\nmodule _\n {A : Set} {B : Set} {{CA : ChangeStructure A}} {{CB : ChangeStructure B}} where\n\n WellDefinedFunChangePoint : \u2200 (f : A \u2192 B) \u2192 (df : Ch (A \u2192 B)) \u2192 \u2200 a da \u2192 Set\n WellDefinedFunChangePoint f df a da = (f \u2295 df) (a \u2295 da) \u2261 f a \u2295 df a da\n\nopen import Thesis.LangChanges\n\nWellDefinedFunChangeFromTo\u2032 : \u2200 {\u03c3 \u03c4} (f1 : \u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type) \u2192 (df : Ch\u03c4 (\u03c3 \u21d2 \u03c4)) \u2192 Set\nWellDefinedFunChangeFromTo\u2032 f1 df = \u2200 da a \u2192 [ _ ]\u03c4 da from a to (a \u2295 da) \u2192 WellDefinedFunChangePoint f1 df a da\n\nopen \u2261-Reasoning\n\nfromto\u2192WellDefined\u2032 : \u2200 {\u03c3 \u03c4 f1 f2 df} \u2192 [ \u03c3 \u21d2 \u03c4 ]\u03c4 df from f1 to f2 \u2192\n WellDefinedFunChangeFromTo\u2032 f1 df\nfromto\u2192WellDefined\u2032 {f1 = f1} {f2} {df} dff da a daa =\n begin\n (f1 \u2295 df) (a \u2295 da)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (a \u2295 da)) (fromto\u2192\u2295 df f1 f2 dff)\u27e9\n f2 (a \u2295 da)\n \u2261\u27e8 sym (fromto\u2192\u2295 _ _ _ (dff da _ _ daa)) \u27e9\n f1 a \u2295 df a da\n \u220e\n\n-- TODO: prove the converse of the above statement. Which is a bit harder, since\n-- you first need to state it.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"04013ce818e48ce926f5431eb540f47288f75584","subject":"trailing whitespaces","message":"trailing whitespaces\n","repos":"crypto-agda\/crypto-agda","old_file":"single-bit-one-time-pad.agda","new_file":"single-bit-one-time-pad.agda","new_contents":"module single-bit-one-time-pad where\n\nopen import Function\nopen import Data.Bool.NP\nopen import Data.Product\nopen import Data.Nat.NP\nopen import Relation.Binary.PropositionalEquality.NP\n\nopen import Data.Bits\nopen import flipbased-implem\nopen import program-distance\n\nK = Bit\nM = Bit\nC = Bit\n\nrecord Adv (S\u2080 S\u2081 S\u2082 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n step\u2081 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2082 : C \u00d7 S\u2081 \u2192 S\u2082\n step\u2083 : S\u2082 \u2192 Bit\n\nrecord Adv\u2081 (S\u2080 S\u2081 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n -- step\u2081 s\u2080 = id , s\u2080\n step\u2082 : C \u00d7 S\u2080 \u2192 S\u2081\n step\u2083 : S\u2081 \u2192 Bit\n\nAdv\u2082 : \u2200 ca \u2192 Set\nAdv\u2082 ca = \u21ba ca (C \u2192 Bit)\n\nmodule Run\u2141\u2032 {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open Adv A\n E : M \u2192 \u21ba 1 C\n E m = toss >>= \u03bb k \u2192 return\u21ba (m xor k)\n run\u2141\u2032 : \u2141? (ca + 1)\n run\u2141\u2032 b = step\u2080 >>= \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n E (m b) >>= \u03bb c \u2192\n return\u21ba (step\u2083 (step\u2082 (c , s\u2081)))}\n\nmodule Run\u2141 {S\u2080 S\u2081 S\u2082 ca} (E : K \u2192 M \u2192 C) (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Adv A\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n let c = E k (m b) in\n step\u2083 (step\u2082 (c , s\u2081))}\n\n run\u2141 : EXP (1 + ca)\n run\u2141 = toss >>= kont\u2080\n\n {- looks wrong\nmodule Run\u2141-Properties {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b k : Bit) where\n open Run\u2141 A\n kont\u2080-not : kont\u2080 b k \u2261 kont\u2080 (not b) (not k)\n kont\u2080-not rewrite xor-not-not b k = {!refl!}\n -}\n\nmodule SymAdv (homPrgDist : HomPrgDist) {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n step\u2081\u2032 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2081\u2032 s\u2080 = case step\u2081 s\u2080 of \u03bb { (m , s\u2081) \u2192 (m \u2218 not , s\u2081) }\n symA : Adv S\u2080 S\u2081 S\u2082 ca\n symA = mk step\u2080 step\u2081\u2032 step\u2082 (not \u2218 step\u2083)\n\n symA\u2032 : Adv S\u2080 S\u2081 S\u2082 ca\n symA\u2032 = mk step\u2080 step\u2081\u2032 step\u2082 step\u2083\n\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n open Run\u2141 _xor_ symA renaming (run\u2141 to runSymA)\n open Run\u2141 _xor_ symA\u2032 renaming (run\u2141 to runSymA\u2032)\n not\u21ba : \u2200 {n} \u2192 EXP n \u2192 EXP n\n not\u21ba = map\u21ba not\n {-\n helper : \u2200 {n} (g\u2080 g\u2081 : EXP n) \u2192 g\u2080 ]-[ g\u2081 \u2192 not\u21ba g\u2080 ]-[ not\u21ba g\u2081\n helper = {!!}\n lem : runA \u21d3 runSymA\n lem A-breaks-E = {!helper (uunSymA\u2032 0b) (runSymA\u2032 1)!}\n where pf : breaks runSymA\n pf = {!!}\n -}\n\nmodule Run\u2141\u2082 {ca} (A : Adv\u2082 ca) (b : Bit) where\n E : Bit \u2192 M \u2192 C\n E k m = m xor k\n\n m : Bit \u2192 Bit\n m = id\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n\n {-\n kont\u2080\u2032 : \u2141? _\n kont\u2080\u2032 k =\n conv-Adv A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n -}\n\n run\u2141\u2082 : EXP (1 + ca)\n run\u2141\u2082 = toss >>= kont\u2080\n\nmodule Run\u2141\u2082-Properties {ca} (A : Adv\u2082 ca) (b k : Bit) where\n open Run\u2141\u2082 A\n kont\u2080-not : kont\u2080 b k \u2261 kont\u2080 (not b) (not k)\n kont\u2080-not rewrite xor-not-not b k = refl\n\nconv-Adv : \u2200 {ca S\u2080 S\u2081 S\u2082} \u2192 Adv S\u2080 S\u2081 S\u2082 ca \u2192 Adv\u2082 ca\nconv-Adv A = step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n \u03bb c \u2192 m (step\u2083 (step\u2082 (c , s\u2081)))}\n where open Adv A\n\nopen \u2261-Reasoning\n\nmodule Conv-Adv-Props (homPrgDist : HomPrgDist) {ca S\u2080 S\u2081 S\u2082} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n -- open Run\u2141-Properties A\n\n A\u2032 : Adv\u2082 ca\n A\u2032 = conv-Adv A\n open Run\u2141\u2082 A\u2032 using () renaming (kont\u2080 to kont\u2080\u2032; run\u2141\u2082 to runA\u2032)\n\n kont\u2080\u2032\u2032 : \u2200 b k \u2192 EXP ca\n kont\u2080\u2032\u2032 b k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 ((m b) xor k , s\u2081)))}\n\n kont\u2080\u2032\u2032\u2032 : \u2200 b\u2032 \u2192 EXP ca\n kont\u2080\u2032\u2032\u2032 b\u2032 =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 (b\u2032 , s\u2081)))}\n\n {-\n kont\u2032\u2032-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032\u2032 b k)\n kont\u2032\u2032-lem true true = {!!}\n kont\u2032\u2032-lem false true = {!!}\n kont\u2032\u2032-lem true false = {!!}\n kont\u2032\u2032-lem false false = {!!}\n\n kont-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032 b k)\n kont-lem b true = {!!}\n kont-lem b false = {!!}\n\n conv-Adv-lem : runA \u2248\u2141? runA\u2032\n conv-Adv-lem b = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 _+_ (kont-lem b 0b) (kont-lem b 1b) \u27e9\n count\u21ba (kont\u2080\u2032 b 0b) + count\u21ba (kont\u2080\u2032 b 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA\u2032 b) \u220e\n\n conv-Adv-sound : runA \u21d3 runA\u2032\n conv-Adv-sound = ]-[-cong-\u2248\u21ba (conv-Adv-lem 0b) (conv-Adv-lem 1b)\n -}\n-- Cute fact: this is true by computation!\ncount\u21ba-toss->>= : \u2200 {c} (f : \u2141? c) \u2192 count\u21ba (toss >>= f) \u2261 count\u21ba (f 0b) + count\u21ba (f 1b)\ncount\u21ba-toss->>= f = refl\n\n{-\nmodule Run\u2141-Properties' {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n lem : count\u21ba (runA b) \u2261 count\u21ba (runA (not b))\n lem = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 (_+_ on count\u21ba) {x = kont\u2080 b 0b} {kont\u2080 (not b) 1b} {kont\u2080 b 1b} {kont\u2080 (not b) 0b} {!!} {!!} \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA (not b)) \u220e\n-}\nopen import program-distance\nopen import Relation.Nullary\n\nlem\u2082 : \u2200 {ca} (A : Adv\u2082 ca) b \u2192 count\u21ba (Run\u2141\u2082.run\u2141\u2082 A b) \u2261 count\u21ba (Run\u2141\u2082.run\u2141\u2082 A (not b))\nlem\u2082 A b = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 (_+_ on count\u21ba) (kont\u2080-not 0b) (kont\u2080-not 1b) \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA (not b)) \u220e\n where open Run\u2141\u2082 A renaming (run\u2141\u2082 to runA)\n open Run\u2141\u2082-Properties A b\n\nlem\u2083 : \u2200 {ca} (A : Adv\u2082 ca) \u2192 Safe\u2141? (Run\u2141\u2082.run\u2141\u2082 A)\nlem\u2083 A = lem\u2082 A 0b\n\n-- A specialized version of lem\u2082\nlem\u2084 : \u2200 {ca} (A : Adv\u2082 ca) \u2192 Safe\u2141? (Run\u2141\u2082.run\u2141\u2082 A)\nlem\u2084 A = count\u21ba (runA 0b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 0b 0b) + count\u21ba (kont\u2080 0b 1b)\n \u2261\u27e8 cong\u2082 (_+_ on count\u21ba) (kont\u2080-not 0b) (kont\u2080-not 1b) \u27e9\n count\u21ba (kont\u2080 1b 1b) + count\u21ba (kont\u2080 1b 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 1b 1b)) _ \u27e9\n count\u21ba (kont\u2080 1b 0b) + count\u21ba (kont\u2080 1b 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA 1b) \u220e\n where open Run\u2141\u2082 A renaming (run\u2141\u2082 to runA)\n open Run\u2141\u2082-Properties A 0b\n","old_contents":"module single-bit-one-time-pad where\n\nopen import Function\nopen import Data.Bool.NP\nopen import Data.Product\nopen import Data.Nat.NP\nopen import Relation.Binary.PropositionalEquality.NP\n\nopen import Data.Bits\nopen import flipbased-implem\nopen import program-distance\n\nK = Bit\nM = Bit\nC = Bit\n \nrecord Adv (S\u2080 S\u2081 S\u2082 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n step\u2081 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2082 : C \u00d7 S\u2081 \u2192 S\u2082\n step\u2083 : S\u2082 \u2192 Bit\n \nrecord Adv\u2081 (S\u2080 S\u2081 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n -- step\u2081 s\u2080 = id , s\u2080\n step\u2082 : C \u00d7 S\u2080 \u2192 S\u2081\n step\u2083 : S\u2081 \u2192 Bit\n \nAdv\u2082 : \u2200 ca \u2192 Set\nAdv\u2082 ca = \u21ba ca (C \u2192 Bit)\n\nmodule Run\u2141\u2032 {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open Adv A\n E : M \u2192 \u21ba 1 C\n E m = toss >>= \u03bb k \u2192 return\u21ba (m xor k)\n run\u2141\u2032 : \u2141? (ca + 1)\n run\u2141\u2032 b = step\u2080 >>= \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n E (m b) >>= \u03bb c \u2192\n return\u21ba (step\u2083 (step\u2082 (c , s\u2081)))}\n\nmodule Run\u2141 {S\u2080 S\u2081 S\u2082 ca} (E : K \u2192 M \u2192 C) (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Adv A\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n let c = E k (m b) in\n step\u2083 (step\u2082 (c , s\u2081))}\n\n run\u2141 : EXP (1 + ca)\n run\u2141 = toss >>= kont\u2080\n\n {- looks wrong\nmodule Run\u2141-Properties {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b k : Bit) where\n open Run\u2141 A\n kont\u2080-not : kont\u2080 b k \u2261 kont\u2080 (not b) (not k) \n kont\u2080-not rewrite xor-not-not b k = {!refl!}\n -}\n\nmodule SymAdv (homPrgDist : HomPrgDist) {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n step\u2081\u2032 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2081\u2032 s\u2080 = case step\u2081 s\u2080 of \u03bb { (m , s\u2081) \u2192 (m \u2218 not , s\u2081) }\n symA : Adv S\u2080 S\u2081 S\u2082 ca\n symA = mk step\u2080 step\u2081\u2032 step\u2082 (not \u2218 step\u2083)\n\n symA\u2032 : Adv S\u2080 S\u2081 S\u2082 ca\n symA\u2032 = mk step\u2080 step\u2081\u2032 step\u2082 step\u2083\n\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n open Run\u2141 _xor_ symA renaming (run\u2141 to runSymA)\n open Run\u2141 _xor_ symA\u2032 renaming (run\u2141 to runSymA\u2032)\n not\u21ba : \u2200 {n} \u2192 EXP n \u2192 EXP n\n not\u21ba = map\u21ba not\n {-\n helper : \u2200 {n} (g\u2080 g\u2081 : EXP n) \u2192 g\u2080 ]-[ g\u2081 \u2192 not\u21ba g\u2080 ]-[ not\u21ba g\u2081\n helper = {!!}\n lem : runA \u21d3 runSymA\n lem A-breaks-E = {!helper (uunSymA\u2032 0b) (runSymA\u2032 1)!}\n where pf : breaks runSymA\n pf = {!!}\n -}\n\nmodule Run\u2141\u2082 {ca} (A : Adv\u2082 ca) (b : Bit) where\n E : Bit \u2192 M \u2192 C\n E k m = m xor k\n\n m : Bit \u2192 Bit\n m = id\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n\n {-\n kont\u2080\u2032 : \u2141? _\n kont\u2080\u2032 k =\n conv-Adv A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n -}\n\n run\u2141\u2082 : EXP (1 + ca)\n run\u2141\u2082 = toss >>= kont\u2080\n\nmodule Run\u2141\u2082-Properties {ca} (A : Adv\u2082 ca) (b k : Bit) where\n open Run\u2141\u2082 A\n kont\u2080-not : kont\u2080 b k \u2261 kont\u2080 (not b) (not k) \n kont\u2080-not rewrite xor-not-not b k = refl\n\nconv-Adv : \u2200 {ca S\u2080 S\u2081 S\u2082} \u2192 Adv S\u2080 S\u2081 S\u2082 ca \u2192 Adv\u2082 ca\nconv-Adv A = step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n \u03bb c \u2192 m (step\u2083 (step\u2082 (c , s\u2081)))}\n where open Adv A\n\nopen \u2261-Reasoning\n\nmodule Conv-Adv-Props (homPrgDist : HomPrgDist) {ca S\u2080 S\u2081 S\u2082} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n -- open Run\u2141-Properties A\n\n A\u2032 : Adv\u2082 ca\n A\u2032 = conv-Adv A\n open Run\u2141\u2082 A\u2032 using () renaming (kont\u2080 to kont\u2080\u2032; run\u2141\u2082 to runA\u2032)\n\n kont\u2080\u2032\u2032 : \u2200 b k \u2192 EXP ca\n kont\u2080\u2032\u2032 b k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 ((m b) xor k , s\u2081)))}\n\n kont\u2080\u2032\u2032\u2032 : \u2200 b\u2032 \u2192 EXP ca\n kont\u2080\u2032\u2032\u2032 b\u2032 =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 (b\u2032 , s\u2081)))}\n\n {-\n kont\u2032\u2032-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032\u2032 b k)\n kont\u2032\u2032-lem true true = {!!}\n kont\u2032\u2032-lem false true = {!!}\n kont\u2032\u2032-lem true false = {!!}\n kont\u2032\u2032-lem false false = {!!}\n\n kont-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032 b k)\n kont-lem b true = {!!}\n kont-lem b false = {!!}\n\n conv-Adv-lem : runA \u2248\u2141? runA\u2032\n conv-Adv-lem b = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 _+_ (kont-lem b 0b) (kont-lem b 1b) \u27e9\n count\u21ba (kont\u2080\u2032 b 0b) + count\u21ba (kont\u2080\u2032 b 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA\u2032 b) \u220e\n\n conv-Adv-sound : runA \u21d3 runA\u2032\n conv-Adv-sound = ]-[-cong-\u2248\u21ba (conv-Adv-lem 0b) (conv-Adv-lem 1b)\n -}\n-- Cute fact: this is true by computation!\ncount\u21ba-toss->>= : \u2200 {c} (f : \u2141? c) \u2192 count\u21ba (toss >>= f) \u2261 count\u21ba (f 0b) + count\u21ba (f 1b)\ncount\u21ba-toss->>= f = refl\n\n{-\nmodule Run\u2141-Properties' {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n lem : count\u21ba (runA b) \u2261 count\u21ba (runA (not b))\n lem = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 (_+_ on count\u21ba) {x = kont\u2080 b 0b} {kont\u2080 (not b) 1b} {kont\u2080 b 1b} {kont\u2080 (not b) 0b} {!!} {!!} \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA (not b)) \u220e\n-}\nopen import program-distance\nopen import Relation.Nullary\n\nlem\u2082 : \u2200 {ca} (A : Adv\u2082 ca) b \u2192 count\u21ba (Run\u2141\u2082.run\u2141\u2082 A b) \u2261 count\u21ba (Run\u2141\u2082.run\u2141\u2082 A (not b))\nlem\u2082 A b = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 (_+_ on count\u21ba) (kont\u2080-not 0b) (kont\u2080-not 1b) \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA (not b)) \u220e\n where open Run\u2141\u2082 A renaming (run\u2141\u2082 to runA)\n open Run\u2141\u2082-Properties A b\n\nlem\u2083 : \u2200 {ca} (A : Adv\u2082 ca) \u2192 Safe\u2141? (Run\u2141\u2082.run\u2141\u2082 A)\nlem\u2083 A = lem\u2082 A 0b\n\n-- A specialized version of lem\u2082\nlem\u2084 : \u2200 {ca} (A : Adv\u2082 ca) \u2192 Safe\u2141? (Run\u2141\u2082.run\u2141\u2082 A)\nlem\u2084 A = count\u21ba (runA 0b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 0b 0b) + count\u21ba (kont\u2080 0b 1b)\n \u2261\u27e8 cong\u2082 (_+_ on count\u21ba) (kont\u2080-not 0b) (kont\u2080-not 1b) \u27e9\n count\u21ba (kont\u2080 1b 1b) + count\u21ba (kont\u2080 1b 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 1b 1b)) _ \u27e9\n count\u21ba (kont\u2080 1b 0b) + count\u21ba (kont\u2080 1b 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA 1b) \u220e\n where open Run\u2141\u2082 A renaming (run\u2141\u2082 to runA)\n open Run\u2141\u2082-Properties A 0b\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"472f1d2065bc28c6100e66b94daab1293925a60e","subject":"Control\/Protocol\/Choreography.agda","message":"Control\/Protocol\/Choreography.agda\n","repos":"crypto-agda\/crypto-agda","old_file":"Control\/Protocol\/Choreography.agda","new_file":"Control\/Protocol\/Choreography.agda","new_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\nopen import Data.One hiding (_\u225f_)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\n {-\ndata S<_> {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n\npattern com' q M P = com (mk q M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\n\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n Trace : Proto \u2192 Proto\n Trace end = end\n Trace (com c) = com (Trace\u1d9c c)\n\n Trace\u1d9c : Com \u2192 Com\n Trace\u1d9c (mk io M P) = \u03a3\u1d9c M \u03bb m \u2192 Trace (P m)\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c (mk q M P) = mk (dual\u1d35\u1d3c q) M \u03bb m \u2192 dual (P m)\n\ndata IsTrace : Proto \u2192 \u2605\u2081 where\n end : IsTrace end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsTrace (P m)) \u2192 IsTrace (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\nmutual\n data Dual\u1d9c : Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a0\u1d9c M P) (\u03a3\u1d9c M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a3\u1d9c M P) (\u03a0\u1d9c M Q)\n\n data Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n {-\n \u03a0\u2610\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P [ x ]) (Q x)) \u2192 Dual (\u03a0\u1d3e (\u2610 M) P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0\u2610 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q [ x ])) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e (\u2610 M) Q)\n -}\n\n {-\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Trace : Choreo I \u2192 Proto\n \u2102Trace \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Trace-IsTrace : \u2200 \u2102 \u2192 IsTrace (\u2102Trace \u2102)\n \u2102Trace-IsTrace (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com q M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com q M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\nTrace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261\u1d3e Trace P\nTrace-idempotent end = end\nTrace-idempotent (com' _ M P) = com Out M \u03bb m \u2192 Trace-idempotent (P m)\n\nTrace-dual-oblivious : \u2200 P \u2192 Trace (dual P) \u2261\u1d3e Trace P\nTrace-dual-oblivious end = end\nTrace-dual-oblivious (com' _ M P) = com Out M \u03bb m \u2192 Trace-dual-oblivious (P m)\n\nSink : Proto \u2192 Proto\nSink = dual \u2218 Trace\n-}\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n{-\n++Tele : \u2200 (P : Proto){Q : Tele P \u2192 Proto} (xs : Tele P) \u2192 Tele (Q xs) \u2192 Tele (P >>= Q)\n++Tele end _ ys = ys\n++Tele (com' q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com q M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com q M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n{-\nDual-sym (\u03a0\u2610\u00b7\u03a3 f) = {!\u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))!}\nDual-sym (\u03a3\u00b7\u03a0\u2610 f) = {!\u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))!}\n-}\n-}\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele (dual P) \u2261 Tele P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n El->>= : (P : Proto){Q : Tele P \u2192 Proto}{X : Tele (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Tele P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n-}\n\n>>=-com : (P : Proto){Q : Tele P \u2192 Proto}{R : Tele P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Tele P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Tele P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : \u2605_ a} (l r : A) \u2192 LR \u2192 A \n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP >>\u1d9c S = record P { P = \u03bb m \u2192 S (Com.P P m) }\n\n{-\nmodule V2 where\n mutual\n Sim\u1d3e : Proto \u2192 Proto \u2192 Proto\n Sim\u1d3e end Q = Q\n Sim\u1d3e (\u03a0\u1d3e M P) Q = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e (P m) Q\n Sim\u1d3e P end = P\n Sim\u1d3e P (\u03a0\u1d3e M Q) = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e P (Q m)\n Sim\u1d3e (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 Sim\u1d3e (P m) (\u03a3\u1d3e M' Q)) ,inr: (\u03bb m' \u2192 Sim\u1d3e (\u03a3\u1d3e M P) (Q m')) ]\n\n Sim\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\n Sim\u1d3e-assoc end Q R s = s\n Sim\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = Sim\u1d3e-assoc (P m) _ _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , Sim\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n Sim\u1d3e-sendR : \u2200 {M} P Q \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7 \u2192 (m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7\n Sim\u1d3e-sendR P Q s m = {!!}\n\n Sim\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7) \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7\n Sim\u1d3e-recvR P Q s = {!!}\n\n Sim\u1d3e-! : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 Sim\u1d3e Q P \u27e7\n Sim\u1d3e-! end Q p = {!!}\n Sim\u1d3e-! (\u03a0\u1d3e M P) end p x = Sim\u1d3e-! (P x) end (p x)\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-recvR (Q m') P \u03bb m \u2192 Sim\u1d3e-sendR {!!} {!!} (p m) m'\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 Sim\u1d3e-! (P m) (com (mk Out M' Q)) (p m) \n Sim\u1d3e-! (\u03a3\u1d3e M P) end p = p \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-! (com (mk Out M P)) (Q m') (p m') \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (Sim\u1d3e-! (P m) (com (mk Out M' Q)) p) \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (Sim\u1d3e-! (com (mk Out M P)) (Q m) p) \n\n Sim\u1d3e-apply : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n Sim\u1d3e-apply end Q s p = s\n Sim\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = Sim\u1d3e-apply (P m) Q (s m) p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = Sim\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n -}\n\nmodule _ where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n P \u214b\u1d3e end = P\n com P\u1d38 \u214b\u1d3e com P\u1d3f = \u03a3\u1d3e LR (P\u1d38 \u214b\u1d9c P\u1d3f)\n\n _\u214b\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 \u214b\u1d9c P\u1d3f) `L = P\u1d38 \u214b\u1d9cL P\u1d3f\n (P\u1d38 \u214b\u1d9c P\u1d3f) `R = P\u1d38 \u214b\u1d9cR P\u1d3f\n\n _\u214b\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) \u214b\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m \u214b\u1d3e com Q)\n\n _\u214b\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P \u214b\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P \u214b\u1d3e P\u1d3f m)\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end ox\u1d3e Q = Q\n P ox\u1d3e end = P\n com P\u1d38 ox\u1d3e com P\u1d3f = \u03a0\u1d3e LR (P\u1d38 ox\u1d9c P\u1d3f)\n\n _ox\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 ox\u1d9c P\u1d3f) `L = P\u1d38 ox\u1d9cL P\u1d3f\n (P\u1d38 ox\u1d9c P\u1d3f) `R = P\u1d38 ox\u1d9cR P\u1d3f\n\n _ox\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) ox\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m ox\u1d3e com Q)\n\n _ox\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P ox\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P ox\u1d3e P\u1d3f m)\n\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = {!!}\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id = {!!}\n \n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm P Q = {!!}\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e P Q p q = {!!}\n\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = {!!}\n\n \u2295\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map = {!!}\n\n &\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map = {!!}\n\n \u214b\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u214b\u1d3e R \u27e7 \u2192 \u27e6 Q \u214b\u1d3e S \u27e7\n \u214b\u1d3e-map = {!!}\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map = {!!}\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = let z = switchL _ _ _ (comma\u1d3e (P \u214b\u1d3e Q) (dual Q \u214b\u1d3e R) pq qr) in {!z!}\n\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Tele B \u00d7 Tele E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a3\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a0\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\nopen import Data.One hiding (_\u225f_)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\n {-\ndata S<_> {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n\npattern com' q M P = com (mk q M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\n\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n Trace : Proto \u2192 Proto\n Trace end = end\n Trace (com c) = com (Trace\u1d9c c)\n\n Trace\u1d9c : Com \u2192 Com\n Trace\u1d9c (mk io M P) = \u03a3\u1d9c M \u03bb m \u2192 Trace (P m)\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c (mk q M P) = mk (dual\u1d35\u1d3c q) M \u03bb m \u2192 dual (P m)\n\ndata IsTrace : Proto \u2192 \u2605\u2081 where\n end : IsTrace end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsTrace (P m)) \u2192 IsTrace (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\nmutual\n data Dual\u1d9c : Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a0\u1d9c M P) (\u03a3\u1d9c M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a3\u1d9c M P) (\u03a0\u1d9c M Q)\n\n data Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n {-\n \u03a0\u2610\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P [ x ]) (Q x)) \u2192 Dual (\u03a0\u1d3e (\u2610 M) P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0\u2610 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q [ x ])) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e (\u2610 M) Q)\n -}\n\n {-\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Trace : Choreo I \u2192 Proto\n \u2102Trace \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Trace-IsTrace : \u2200 \u2102 \u2192 IsTrace (\u2102Trace \u2102)\n \u2102Trace-IsTrace (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com q M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com q M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\nTrace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261\u1d3e Trace P\nTrace-idempotent end = end\nTrace-idempotent (com' _ M P) = com Out M \u03bb m \u2192 Trace-idempotent (P m)\n\nTrace-dual-oblivious : \u2200 P \u2192 Trace (dual P) \u2261\u1d3e Trace P\nTrace-dual-oblivious end = end\nTrace-dual-oblivious (com' _ M P) = com Out M \u03bb m \u2192 Trace-dual-oblivious (P m)\n\nSink : Proto \u2192 Proto\nSink = dual \u2218 Trace\n-}\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n{-\n++Tele : \u2200 (P : Proto){Q : Tele P \u2192 Proto} (xs : Tele P) \u2192 Tele (Q xs) \u2192 Tele (P >>= Q)\n++Tele end _ ys = ys\n++Tele (com' q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com q M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com q M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n{-\nDual-sym (\u03a0\u2610\u00b7\u03a3 f) = {!\u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))!}\nDual-sym (\u03a3\u00b7\u03a0\u2610 f) = {!\u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))!}\n-}\n-}\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele (dual P) \u2261 Tele P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n El->>= : (P : Proto){Q : Tele P \u2192 Proto}{X : Tele (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Tele P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n-}\n\n>>=-com : (P : Proto){Q : Tele P \u2192 Proto}{R : Tele P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Tele P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Tele P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : \u2605_ a} (l r : A) \u2192 LR \u2192 A \n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP >>\u1d9c S = record P { P = \u03bb m \u2192 S (Com.P P m) }\n\n{-\nmodule V1 where\n mutual\n Sim\u1d3e : Proto \u2192 Proto \u2192 Proto\n Sim\u1d3e end Q = Q\n Sim\u1d3e P end = P\n Sim\u1d3e (com P\u1d38) (com P\u1d3f) = \u03a3\u1d3e LR (Sim\u1d9c P\u1d38 P\u1d3f)\n\n Sim\u1d9c : Com \u2192 Com \u2192 LR \u2192 Proto\n Sim\u1d9c P\u1d38 P\u1d3f `L = Sim\u1d9cL P\u1d38 P\u1d3f\n Sim\u1d9c P\u1d38 P\u1d3f `R = Sim\u1d9cR P\u1d38 P\u1d3f\n\n Sim\u1d9cL : Com \u2192 Com \u2192 Proto\n Sim\u1d9cL (mk q\u1d38 M\u1d38 P\u1d38) Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 Sim\u1d3e (P\u1d38 m) (com Q))\n\n Sim\u1d9cR : Com \u2192 Com \u2192 Proto\n Sim\u1d9cR P (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 Sim\u1d3e (com P) (P\u1d3f m))\n\nmodule V2 where\n mutual\n Sim\u1d3e : Proto \u2192 Proto \u2192 Proto\n Sim\u1d3e end Q = Q\n Sim\u1d3e (\u03a0\u1d3e M P) Q = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e (P m) Q\n Sim\u1d3e P end = P\n Sim\u1d3e P (\u03a0\u1d3e M Q) = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e P (Q m)\n Sim\u1d3e (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 Sim\u1d3e (P m) (\u03a3\u1d3e M' Q)) ,inr: (\u03bb m' \u2192 Sim\u1d3e (\u03a3\u1d3e M P) (Q m')) ]\n\n Sim\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\n Sim\u1d3e-assoc end Q R s = s\n Sim\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = Sim\u1d3e-assoc (P m) _ _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , Sim\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n Sim\u1d3e-sendR : \u2200 {M} P Q \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7 \u2192 (m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7\n Sim\u1d3e-sendR P Q s m = {!!}\n\n Sim\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7) \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7\n Sim\u1d3e-recvR P Q s = {!!}\n\n Sim\u1d3e-! : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 Sim\u1d3e Q P \u27e7\n Sim\u1d3e-! end Q p = {!!}\n Sim\u1d3e-! (\u03a0\u1d3e M P) end p x = Sim\u1d3e-! (P x) end (p x)\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-recvR (Q m') P \u03bb m \u2192 Sim\u1d3e-sendR {!!} {!!} (p m) m'\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 Sim\u1d3e-! (P m) (com (mk Out M' Q)) (p m) \n Sim\u1d3e-! (\u03a3\u1d3e M P) end p = p \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-! (com (mk Out M P)) (Q m') (p m') \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (Sim\u1d3e-! (P m) (com (mk Out M' Q)) p) \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (Sim\u1d3e-! (com (mk Out M P)) (Q m) p) \n\n Sim\u1d3e-apply : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n Sim\u1d3e-apply end Q s p = s\n Sim\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = Sim\u1d3e-apply (P m) Q (s m) p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = Sim\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n -}\n\nmodule V3 where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n P \u214b\u1d3e Q = \u03a3\u1d3e LR (Par\u1d3e P Q)\n\n Par\u1d3e : Proto \u2192 Proto \u2192 LR \u2192 Proto\n Par\u1d3e end Q `L = Q\n Par\u1d3e (com' q M P) Q `L = com' q M \u03bb m \u2192 P m \u214b\u1d3e Q\n Par\u1d3e P end `R = P\n Par\u1d3e P (com' q M Q) `R = com' q M \u03bb m \u2192 P \u214b\u1d3e Q m\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n P ox\u1d3e Q = \u03a0\u1d3e LR (Ten\u1d3e P Q)\n\n Ten\u1d3e : Proto \u2192 Proto \u2192 LR \u2192 Proto\n Ten\u1d3e end Q `L = Q\n Ten\u1d3e (com' q M P) Q `L = com' q M \u03bb m \u2192 P m ox\u1d3e Q\n Ten\u1d3e P end `R = P\n Ten\u1d3e P (com' q M Q) `R = com' q M \u03bb m \u2192 P ox\u1d3e Q m\n\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = {!!}\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id = {!!}\n \n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm P Q = {!!}\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e P Q p q = {!!}\n\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = {!!}\n\n \u2295\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map = {!!}\n\n &\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map = {!!}\n\n \u214b\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u214b\u1d3e R \u27e7 \u2192 \u27e6 Q \u214b\u1d3e S \u27e7\n \u214b\u1d3e-map = {!!}\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map = {!!}\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = let z = switchL _ _ _ (comma\u1d3e (P \u214b\u1d3e Q) (dual Q \u214b\u1d3e R) pq qr) in {!z!}\n\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Tele B \u00d7 Tele E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a3\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a0\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"fb908ee6dff3ee32438dc93dca5f6c3a00df821f","subject":"Fixed a missing cosmetic change.","message":"Fixed a missing cosmetic change.\n\nIgnore-this: e48756f412daa48aab9a7214ed4a8ac2\n\ndarcs-hash:20110316115851-3bd4e-4fdfe28c58205b26d16932f3d4e1d16208a01338.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Data\/Stream\/Bisimilarity\/PropertiesI.agda","new_file":"src\/FOTC\/Data\/Stream\/Bisimilarity\/PropertiesI.agda","new_contents":"------------------------------------------------------------------------------\n-- Bisimilarity properties\n------------------------------------------------------------------------------\n\nmodule FOTC.Data.Stream.Bisimilarity.PropertiesI where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI using ( \u2237-injective )\n\nopen import FOTC.Data.Stream.Bisimilarity\n\n------------------------------------------------------------------------------\n\nx\u2237xs\u2248x\u2237ys\u2192xs\u2248ys : \u2200 {x xs ys} \u2192 x \u2237 xs \u2248 x \u2237 ys \u2192 xs \u2248 ys\nx\u2237xs\u2248x\u2237ys\u2192xs\u2248ys {x} {xs} {ys} x\u2237xs\u2248x\u2237ys = xs\u2248ys\n where\n x' : D\n x' = \u2203-proj\u2081 (\u2248-gfp\u2081 x\u2237xs\u2248x\u2237ys)\n\n xs' : D\n xs' = \u2203-proj\u2081 (\u2203-proj\u2082 (\u2248-gfp\u2081 x\u2237xs\u2248x\u2237ys))\n\n ys' : D\n ys' = \u2203-proj\u2081 (\u2203-proj\u2082 (\u2203-proj\u2082 (\u2248-gfp\u2081 x\u2237xs\u2248x\u2237ys)))\n\n helper : xs' \u2248 ys' \u2227 x \u2237 xs \u2261 x' \u2237 xs' \u2227 x \u2237 ys \u2261 x' \u2237 ys'\n helper = \u2203-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 (\u2248-gfp\u2081 x\u2237xs\u2248x\u2237ys)))\n\n xs'\u2248ys' : xs' \u2248 ys'\n xs'\u2248ys' = \u2227-proj\u2081 helper\n\n x\u2237xs\u2261x'\u2237xs' : x \u2237 xs \u2261 x' \u2237 xs'\n x\u2237xs\u2261x'\u2237xs' = \u2227-proj\u2081 (\u2227-proj\u2082 helper)\n\n x\u2237ys\u2261x'\u2237ys' : x \u2237 ys \u2261 x' \u2237 ys'\n x\u2237ys\u2261x'\u2237ys' = \u2227-proj\u2082 (\u2227-proj\u2082 helper)\n\n xs\u2261xs' : xs \u2261 xs'\n xs\u2261xs' = \u2227-proj\u2082 (\u2237-injective x\u2237xs\u2261x'\u2237xs')\n\n ys\u2261ys' : ys \u2261 ys'\n ys\u2261ys' = \u2227-proj\u2082 (\u2237-injective x\u2237ys\u2261x'\u2237ys')\n\n xs\u2248ys : xs \u2248 ys\n xs\u2248ys = subst (\u03bb t \u2192 t \u2248 ys)\n (sym xs\u2261xs')\n (subst (\u03bb t \u2192 xs' \u2248 t) (sym ys\u2261ys') xs'\u2248ys')\n","old_contents":"------------------------------------------------------------------------------\n-- Bisimilarity properties\n------------------------------------------------------------------------------\n\nmodule FOTC.Data.Stream.Bisimilarity.PropertiesI where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI using ( \u2237-injective )\n\nopen import FOTC.Data.Stream.Bisimilarity using ( _\u2248_ ; -\u2248-gfp\u2081 )\n\n------------------------------------------------------------------------------\n\nx\u2237xs\u2248x\u2237ys\u2192xs\u2248ys : \u2200 {x xs ys} \u2192 x \u2237 xs \u2248 x \u2237 ys \u2192 xs \u2248 ys\nx\u2237xs\u2248x\u2237ys\u2192xs\u2248ys {x} {xs} {ys} x\u2237xs\u2248x\u2237ys = xs\u2248ys\n where\n x' : D\n x' = \u2203-proj\u2081 (-\u2248-gfp\u2081 x\u2237xs\u2248x\u2237ys)\n\n xs' : D\n xs' = \u2203-proj\u2081 (\u2203-proj\u2082 (-\u2248-gfp\u2081 x\u2237xs\u2248x\u2237ys))\n\n ys' : D\n ys' = \u2203-proj\u2081 (\u2203-proj\u2082 (\u2203-proj\u2082 (-\u2248-gfp\u2081 x\u2237xs\u2248x\u2237ys)))\n\n helper : xs' \u2248 ys' \u2227 x \u2237 xs \u2261 x' \u2237 xs' \u2227 x \u2237 ys \u2261 x' \u2237 ys'\n helper = \u2203-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 (-\u2248-gfp\u2081 x\u2237xs\u2248x\u2237ys)))\n\n xs'\u2248ys' : xs' \u2248 ys'\n xs'\u2248ys' = \u2227-proj\u2081 helper\n\n x\u2237xs\u2261x'\u2237xs' : x \u2237 xs \u2261 x' \u2237 xs'\n x\u2237xs\u2261x'\u2237xs' = \u2227-proj\u2081 (\u2227-proj\u2082 helper)\n\n x\u2237ys\u2261x'\u2237ys' : x \u2237 ys \u2261 x' \u2237 ys'\n x\u2237ys\u2261x'\u2237ys' = \u2227-proj\u2082 (\u2227-proj\u2082 helper)\n\n xs\u2261xs' : xs \u2261 xs'\n xs\u2261xs' = \u2227-proj\u2082 (\u2237-injective x\u2237xs\u2261x'\u2237xs')\n\n ys\u2261ys' : ys \u2261 ys'\n ys\u2261ys' = \u2227-proj\u2082 (\u2237-injective x\u2237ys\u2261x'\u2237ys')\n\n xs\u2248ys : xs \u2248 ys\n xs\u2248ys = subst (\u03bb t \u2192 t \u2248 ys)\n (sym xs\u2261xs')\n (subst (\u03bb t \u2192 xs' \u2248 t) (sym ys\u2261ys') xs'\u2248ys')\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"42316d32e1b90cea16ece1ad29627cb678610bc6","subject":"Clarify that change algebra operations trivially preserve d.o.e.","message":"Clarify that change algebra operations trivially preserve d.o.e.\n\nOld-commit-hash: c11fb0c1abae938f0c7ec3dacff96f112f6bbee4\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Equivalence.agda","new_file":"Base\/Change\/Equivalence.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\n-- Extension Point: None (currently). Do we need to allow plugins to customize\n-- this concept?\nStructure : Set\nStructure = Unit\n\nmodule Structure (unused : Structure) where\n\n module _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set a\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n module _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = lemma\n where\n open \u2261-Reasoning\n open import Postulate.Extensionality\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\n-- Extension Point: None (currently). Do we need to allow plugins to customize\n-- this concept?\nStructure : Set\nStructure = Unit\n\nmodule Structure (unused : Structure) where\n\n module _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set a\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n module _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = lemma\n where\n open \u2261-Reasoning\n open import Postulate.Extensionality\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"0d3012e5695fb8ab9a9f6923e17ada2d99a1b7b6","subject":"Updated a note about the existential quantifier.","message":"Updated a note about the existential quantifier.\n\nIgnore-this: 8ad5316e644cba628b19f41d007d533\n\ndarcs-hash:20111130164653-3bd4e-9e00b7c21e8e910b34ab599f2d86be3c0e7b3570.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/thesis\/logical-framework\/Existential.agda","new_file":"notes\/thesis\/logical-framework\/Existential.agda","new_contents":"-- Tested on 30 November 2011.\n\nmodule Existential where\n\npostulate\n D : Set\n \u2203 : (P : D \u2192 Set) \u2192 Set\n _,_ : {P : D \u2192 Set}(d : D) \u2192 P d \u2192 \u2203 P\n \u2203-proj\u2081 : {P : D \u2192 Set} \u2192 \u2203 P \u2192 D\n \u2203-proj\u2082 : {P : D \u2192 Set}(p : \u2203 P) \u2192 P (\u2203-proj\u2081 p)\n\nsyntax \u2203 (\u03bb x \u2192 e) = \u2203[ x ] e\n\npostulate P : D \u2192 D \u2192 Set\n\n\u2203\u2200 : \u2203[ x ](\u2200 y \u2192 P x y) \u2192 \u2200 y \u2192 \u2203[ x ] P x y\n\u2203\u2200 h y = \u2203-proj\u2081 h , (\u2203-proj\u2082 h) y\n","old_contents":"module Existential where\n\npostulate\n D : Set\n \u2203 : (P : D \u2192 Set) \u2192 Set\n _,_ : {P : D \u2192 Set}(d : D) \u2192 P d \u2192 \u2203 P\n \u2203-proj\u2081 : {P : D \u2192 Set} \u2192 \u2203 P \u2192 D\n \u2203-proj\u2082 : {P : D \u2192 Set}(p : \u2203 P) \u2192 P (\u2203-proj\u2081 p)\n\npostulate P : D \u2192 D \u2192 Set\n\n\u2203\u2200 : (\u2203 \u03bb x \u2192 \u2200 y \u2192 P x y) \u2192 \u2200 y \u2192 \u2203 \u03bb x \u2192 P x y\n\u2203\u2200 h y = \u2203-proj\u2081 h , (\u2203-proj\u2082 h) y\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"36716645788ba47dbfa8cbb7912747ea4e52a921","subject":"Improved some proofs.","message":"Improved some proofs.\n\nIgnore-this: e7e0889f7060eb9f3adde2de878ad1d3\n\ndarcs-hash:20100524023850-3bd4e-59dcde27415395528e46b6323c5698f04b5ddac0.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Relation\/Inequalities\/Properties.agda","new_file":"LTC\/Relation\/Inequalities\/Properties.agda","new_contents":"------------------------------------------------------------------------------\n-- Properties of the inequalities\n------------------------------------------------------------------------------\n\nmodule LTC.Relation.Inequalities.Properties where\n\nopen import LTC.Minimal\n\nopen import LTC.Data.N\nopen import LTC.Function.Arithmetic\nopen import LTC.Function.Arithmetic.Properties\nopen import LTC.Relation.Inequalities\n\nopen import MyStdLib.Data.Sum\nopen import MyStdLib.Function\n\n------------------------------------------------------------------------------\n\n-- TODO: Why the ATP couldn't prove it?\n-- postulate\n-- x\u22650 : (n : D) \u2192 N n \u2192 GE n zero\n-- {-# ATP prove x\u22650 zN sN #-}\n\nx\u22650 : {n : D} \u2192 N n \u2192 GE n zero\nx\u22650 zN = lt-00\nx\u22650 (sN {n} Nn) = lt-S0 n\n-- {-# ATP hint x\u22650 #-}\n\n0\u2264x : {n : D} \u2192 N n \u2192 LE zero n\n0\u2264x Nn = x\u22650 Nn\n\n\u00acx<0 : {n : D} \u2192 N n \u2192 \u00ac (LT n zero)\n\u00acx<0 zN 0<0 = \u22a5-elim prf\n where\n postulate prf : \u22a5\n {-# ATP prove prf #-}\n\u00acx<0 (sN Nn) Sn<0 = \u22a5-elim prf\n where\n postulate prf : \u22a5\n {-# ATP prove prf #-}\n\npostulate\n \u00ac0>x : {n : D} \u2192 N n \u2192 \u00ac (GT zero n)\n{-# ATP prove \u00ac0>x x\u22650 #-}\n\npostulate\n \u00acS\u22640 : {d : D} \u2192 \u00ac (LE (succ d) zero)\n{-# ATP prove \u00acS\u22640 #-}\n\nxy\u2228x\u2264y : {m n : D} \u2192 N m \u2192 N n \u2192 GT m n \u2228 LE m n\nx>y\u2228x\u2264y zN Nn = inj\u2082 $ x\u22650 Nn\nx>y\u2228x\u2264y (sN {m} Nm) zN = inj\u2081 $ lt-0S m\nx>y\u2228x\u2264y (sN {m} Nm) (sN {n} Nn) = prf $ x>y\u2228x\u2264y Nm Nn\n where\n postulate\n prf : (GT m n) \u2228 (LE m n) \u2192\n GT (succ m) (succ n) \u2228 LE (succ m) (succ n)\n {-# ATP prove prf #-}\n\n\u00acxy\u2192x-y+y\u2261x : {m n : D} \u2192 N m \u2192 N n \u2192 GT m n \u2192 (m - n) + n \u2261 m\nx>y\u2192x-y+y\u2261x zN Nn 0>n = \u22a5-elim (\u00ac0>x Nn 0>n)\nx>y\u2192x-y+y\u2261x (sN {m} Nm) zN Sm>0 = prf\n where\n postulate prf : (succ m - zero) + zero \u2261 succ m\n {-# ATP prove prf +-rightIdentity minus-N #-}\n\nx>y\u2192x-y+y\u2261x (sN {m} Nm) (sN {n} Nn) Sm>Sn = prf (x>y\u2192x-y+y\u2261x Nm Nn m>n )\n where\n postulate m>n : GT m n\n {-# ATP prove m>n #-}\n\n postulate prf : (m - n) + n \u2261 m \u2192\n (succ m - succ n) + succ n \u2261 succ m\n {-# ATP prove prf +-comm minus-N sN #-}\n\nx\u2264y\u2192y-x+x\u2261y : {m n : D} \u2192 N m \u2192 N n \u2192 LE m n \u2192 (n - m) + m \u2261 n\nx\u2264y\u2192y-x+x\u2261y {n = n} zN Nn 0\u2264n = prf\n where\n postulate prf : (n - zero) + zero \u2261 n\n {-# ATP prove prf +-rightIdentity minus-N #-}\n\nx\u2264y\u2192y-x+x\u2261y (sN Nm) zN Sm\u22640 = \u22a5-elim (\u00acS\u22640 Sm\u22640)\n\nx\u2264y\u2192y-x+x\u2261y (sN {m} Nm) (sN {n} Nn) Sm\u2264Sn = prf (x\u2264y\u2192y-x+x\u2261y Nm Nn m\u2264n )\n where\n postulate m\u2264n : LE m n\n {-# ATP prove m\u2264n #-}\n\n postulate prf : (n - m) + m \u2261 n \u2192\n (succ n - succ m) + succ m \u2261 succ n\n {-# ATP prove prf +-comm minus-N sN #-}\n\n------------------------------------------------------------------------------\n-- Properties about LT\u2082\n\npostulate\n \u00acxy<00 : {m n : D} \u2192 N m \u2192 N n \u2192 \u00ac (LT\u2082 m n zero zero)\n{-# ATP prove \u00acxy<00 \u00acx<0 #-}\n\n[Sx-Sy,Sy]<[Sx,Sy] :\n {m n : D} \u2192 N m \u2192 N n \u2192\n LT\u2082 (succ m - succ n) (succ n) (succ m) (succ n)\n[Sx-Sy,Sy]<[Sx,Sy] {m} {n} Nm Nn = prf\n where\n postulate prf : LT\u2082 (succ m - succ n) (succ n) (succ m) (succ n)\n {-# ATP prove prf sN x-yx : {n : D} \u2192 N n \u2192 \u00ac (GT zero n)\n\u00ac0>x Nn 0>n = \u22a5-elim prf\n where\n postulate prf : \u22a5\n {-# ATP prove prf x\u22650 #-}\n\n\u00acS\u22640 : {d : D} \u2192 \u00ac (LE (succ d) zero)\n\u00acS\u22640 Sd\u22640 = \u22a5-elim prf\n where\n -- The proof uses the axiom true\u2260false\n postulate prf : \u22a5\n {-# ATP prove prf #-}\n\nxy\u2228x\u2264y : {m n : D} \u2192 N m \u2192 N n \u2192 GT m n \u2228 LE m n\nx>y\u2228x\u2264y zN Nn = inj\u2082 $ x\u22650 Nn\nx>y\u2228x\u2264y (sN {m} Nm) zN = inj\u2081 $ lt-0S m\nx>y\u2228x\u2264y (sN {m} Nm) (sN {n} Nn) = prf $ x>y\u2228x\u2264y Nm Nn\n where\n postulate\n prf : (GT m n) \u2228 (LE m n) \u2192\n GT (succ m) (succ n) \u2228 LE (succ m) (succ n)\n {-# ATP prove prf #-}\n\n\u00acxy\u2192x-y+y\u2261x : {m n : D} \u2192 N m \u2192 N n \u2192 GT m n \u2192 (m - n) + n \u2261 m\nx>y\u2192x-y+y\u2261x zN Nn 0>n = \u22a5-elim (\u00ac0>x Nn 0>n)\nx>y\u2192x-y+y\u2261x (sN {m} Nm) zN Sm>0 = prf\n where\n postulate prf : (succ m - zero) + zero \u2261 succ m\n {-# ATP prove prf +-rightIdentity minus-N #-}\n\nx>y\u2192x-y+y\u2261x (sN {m} Nm) (sN {n} Nn) Sm>Sn = prf (x>y\u2192x-y+y\u2261x Nm Nn m>n )\n where\n postulate m>n : GT m n\n {-# ATP prove m>n #-}\n\n postulate prf : (m - n) + n \u2261 m \u2192\n (succ m - succ n) + succ n \u2261 succ m\n {-# ATP prove prf +-comm minus-N sN #-}\n\nx\u2264y\u2192y-x+x\u2261y : {m n : D} \u2192 N m \u2192 N n \u2192 LE m n \u2192 (n - m) + m \u2261 n\nx\u2264y\u2192y-x+x\u2261y {n = n} zN Nn 0\u2264n = prf\n where\n postulate prf : (n - zero) + zero \u2261 n\n {-# ATP prove prf +-rightIdentity minus-N #-}\n\nx\u2264y\u2192y-x+x\u2261y (sN Nm) zN Sm\u22640 = \u22a5-elim (\u00acS\u22640 Sm\u22640)\n\nx\u2264y\u2192y-x+x\u2261y (sN {m} Nm) (sN {n} Nn) Sm\u2264Sn = prf (x\u2264y\u2192y-x+x\u2261y Nm Nn m\u2264n )\n where\n postulate m\u2264n : LE m n\n {-# ATP prove m\u2264n #-}\n\n postulate prf : (n - m) + m \u2261 n \u2192\n (succ n - succ m) + succ m \u2261 succ n\n {-# ATP prove prf +-comm minus-N sN #-}\n\n------------------------------------------------------------------------------\n-- Properties about LT\u2082\n\npostulate\n \u00acxy<00 : {m n : D} \u2192 N m \u2192 N n \u2192 \u00ac (LT\u2082 m n zero zero)\n{-# ATP prove \u00acxy<00 \u00acx<0 #-}\n\n[Sx-Sy,Sy]<[Sx,Sy] :\n {m n : D} \u2192 N m \u2192 N n \u2192\n LT\u2082 (succ m - succ n) (succ n) (succ m) (succ n)\n[Sx-Sy,Sy]<[Sx,Sy] {m} {n} Nm Nn = prf\n where\n postulate prf : LT\u2082 (succ m - succ n) (succ n) (succ m) (succ n)\n {-# ATP prove prf sN x-yy\u2192yy\u2192yn = \u22a5-elim $ \u00ac0>x Nn 0>n\nx>y\u2192yy\u2192ySn =\n trans (<-SS n m) (x>y\u2192ySn))\n\nx\u2265y\u2192x\u226ey : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GE m n \u2192 NLT m n\nx\u2265y\u2192x\u226ey zN zN _ = x\u2270x zN\nx\u2265y\u2192x\u226ey zN (sN Nn) 0\u2265Sn = \u22a5-elim $ \u00ac0\u2265S Nn 0\u2265Sn\nx\u2265y\u2192x\u226ey (sN {m} Nm) zN _ = <-S0 m\nx\u2265y\u2192x\u226ey (sN {m} Nm) (sN {n} Nn) Sm\u2265Sn =\n trans (<-SS m n) (x\u2265y\u2192x\u226ey Nm Nn (trans (sym $ <-SS n (succ m)) Sm\u2265Sn))\n\nx>y\u2192x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 NLE m n\nx>y\u2192x\u2270y zN Nn 0>m = \u22a5-elim $ \u00ac0>x Nn 0>m\nx>y\u2192x\u2270y (sN Nm) zN _ = S\u22700 Nm\nx>y\u2192x\u2270y (sN {m} Nm) (sN {n} Nn) Sm>Sn =\n x\u2270y\u2192Sx\u2270Sy m n (x>y\u2192x\u2270y Nm Nn (trans (sym $ <-SS n m) Sm>Sn))\n\npostulate\n x>y\u2192x\u2264y\u2192\u22a5 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 LE m n \u2192 \u22a5\n{-# ATP prove x>y\u2192x\u2264y\u2192\u22a5 x>y\u2192x\u2270y #-}\n\nx>y\u2228x\u2264y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2228 LE m n\nx>y\u2228x\u2264y zN Nn = inj\u2082 $ x\u22650 Nn\nx>y\u2228x\u2264y (sN {m} Nm) zN = inj\u2081 $ <-0S m\nx>y\u2228x\u2264y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m>n \u2192 inj\u2081 (trans (<-SS n m) m>n))\n , (\u03bb m\u2264n \u2192 inj\u2082 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n ] (x>y\u2228x\u2264y Nm Nn)\n\nxy\u2228x\u2264y Nn Nm\n\nx\u2264y\u2228x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2228 NLE m n\nx\u2264y\u2228x\u2270y zN Nn = inj\u2081 (0\u2264x Nn)\nx\u2264y\u2228x\u2270y (sN Nm) zN = inj\u2082 (S\u22700 Nm)\nx\u2264y\u2228x\u2270y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m\u2264n \u2192 inj\u2081 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n , (\u03bb m\u2270n \u2192 inj\u2082 (x\u2270y\u2192Sx\u2270Sy m n m\u2270n))\n ] (x\u2264y\u2228x\u2270y Nm Nn)\n\nx\u2261y\u2192x\u2264y : \u2200 {m n} {Nm : N m} {Nn : N n} \u2192 m \u2261 n \u2192 LE m n\nx\u2261y\u2192x\u2264y {Nm = Nm} refl = x\u2264x Nm\n\nxy\u2192x-y+y\u2261x : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 (m \u2238 n) + n \u2261 m\nx>y\u2192x-y+y\u2261x zN Nn 0>n = \u22a5-elim $ \u00ac0>x Nn 0>n\nx>y\u2192x-y+y\u2261x (sN {m} Nm) zN Sm>0 = prf\n where\n postulate prf : (succ m \u2238 zero) + zero \u2261 succ m\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx>y\u2192x-y+y\u2261x (sN {m} Nm) (sN {n} Nn) Sm>Sn = prf $ x>y\u2192x-y+y\u2261x Nm Nn m>n\n where\n postulate m>n : GT m n\n {-# ATP prove m>n #-}\n\n postulate prf : (m \u2238 n) + n \u2261 m \u2192 -- IH.\n (succ m \u2238 succ n) + succ n \u2261 succ m\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx\u2264y\u2192y-x+x\u2261y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2192 (n \u2238 m) + m \u2261 n\nx\u2264y\u2192y-x+x\u2261y {n = n} zN Nn 0\u2264n = prf\n where\n postulate prf : (n \u2238 zero) + zero \u2261 n\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx\u2264y\u2192y-x+x\u2261y (sN Nm) zN Sm\u22640 = \u22a5-elim $ \u00acS\u22640 Nm Sm\u22640\n\nx\u2264y\u2192y-x+x\u2261y (sN {m} Nm) (sN {n} Nn) Sm\u2264Sn = prf $ x\u2264y\u2192y-x+x\u2261y Nm Nn m\u2264n\n where\n postulate m\u2264n : LE m n\n {-# ATP prove m\u2264n #-}\n\n postulate prf : (n \u2238 m) + m \u2261 n \u2192 -- IH.\n (succ n \u2238 succ m) + succ m \u2261 succ n\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx0\u2192x-y0\u2192x-y0 = \u22a5-elim $ \u00acx>x zN 0>0\nx\u2265y\u2192y>0\u2192x-y0\u2192x-y0 = prf\n where\n postulate prf : LT (succ m \u2238 succ n) (succ m)\n {-# ATP prove prf x-yy\u2192yy\u2192yn = \u22a5-elim $ \u00ac0>x Nn 0>n\nx>y\u2192yy\u2192ySn =\n trans (<-SS n m) (x>y\u2192ySn))\n\nx\u2265y\u2192x\u226ey : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GE m n \u2192 NLT m n\nx\u2265y\u2192x\u226ey zN zN _ = x\u2270x zN\nx\u2265y\u2192x\u226ey zN (sN Nn) 0\u2265Sn = \u22a5-elim $ \u00ac0\u2265S Nn 0\u2265Sn\nx\u2265y\u2192x\u226ey (sN {m} Nm) zN _ = <-S0 m\nx\u2265y\u2192x\u226ey (sN {m} Nm) (sN {n} Nn) Sm\u2265Sn =\n trans (<-SS m n) (x\u2265y\u2192x\u226ey Nm Nn (trans (sym $ <-SS n (succ m)) Sm\u2265Sn))\n\nx>y\u2192x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 NLE m n\nx>y\u2192x\u2270y zN Nn 0>m = \u22a5-elim $ \u00ac0>x Nn 0>m\nx>y\u2192x\u2270y (sN Nm) zN _ = S\u22700 Nm\nx>y\u2192x\u2270y (sN {m} Nm) (sN {n} Nn) Sm>Sn =\n x\u2270y\u2192Sx\u2270Sy m n (x>y\u2192x\u2270y Nm Nn (trans (sym $ <-SS n m) Sm>Sn))\n\npostulate\n x>y\u2192x\u2264y\u2192\u22a5 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 LE m n \u2192 \u22a5\n{-# ATP prove x>y\u2192x\u2264y\u2192\u22a5 x>y\u2192x\u2270y #-}\n\nx>y\u2228x\u2264y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2228 LE m n\nx>y\u2228x\u2264y zN Nn = inj\u2082 $ x\u22650 Nn\nx>y\u2228x\u2264y (sN {m} Nm) zN = inj\u2081 $ <-0S m\nx>y\u2228x\u2264y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m>n \u2192 inj\u2081 (trans (<-SS n m) m>n))\n , (\u03bb m\u2264n \u2192 inj\u2082 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n ] (x>y\u2228x\u2264y Nm Nn)\n\nxy\u2228x\u2264y Nn Nm\n\nx\u2264y\u2228x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2228 NLE m n\nx\u2264y\u2228x\u2270y zN Nn = inj\u2081 (0\u2264x Nn)\nx\u2264y\u2228x\u2270y (sN Nm) zN = inj\u2082 (S\u22700 Nm)\nx\u2264y\u2228x\u2270y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m\u2264n \u2192 inj\u2081 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n , (\u03bb m\u2270n \u2192 inj\u2082 (x\u2270y\u2192Sx\u2270Sy m n m\u2270n))\n ] (x\u2264y\u2228x\u2270y Nm Nn)\n\nx\u2261y\u2192x\u2264y : \u2200 {m n} {Nm : N m} {Nn : N n} \u2192 m \u2261 n \u2192 LE m n\nx\u2261y\u2192x\u2264y {Nm = Nm} refl = x\u2264x Nm\n\nxy\u2192x-y+y\u2261x : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 (m \u2238 n) + n \u2261 m\nx>y\u2192x-y+y\u2261x zN Nn 0>n = \u22a5-elim $ \u00ac0>x Nn 0>n\nx>y\u2192x-y+y\u2261x (sN {m} Nm) zN Sm>0 = prf\n where\n postulate prf : (succ m \u2238 zero) + zero \u2261 succ m\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx>y\u2192x-y+y\u2261x (sN {m} Nm) (sN {n} Nn) Sm>Sn = prf $ x>y\u2192x-y+y\u2261x Nm Nn m>n\n where\n postulate m>n : GT m n\n {-# ATP prove m>n #-}\n\n postulate prf : (m \u2238 n) + n \u2261 m \u2192 -- IH.\n (succ m \u2238 succ n) + succ n \u2261 succ m\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx\u2264y\u2192y-x+x\u2261y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2192 (n \u2238 m) + m \u2261 n\nx\u2264y\u2192y-x+x\u2261y {n = n} zN Nn 0\u2264n = prf\n where\n postulate prf : (n \u2238 zero) + zero \u2261 n\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx\u2264y\u2192y-x+x\u2261y (sN Nm) zN Sm\u22640 = \u22a5-elim $ \u00acS\u22640 Nm Sm\u22640\n\nx\u2264y\u2192y-x+x\u2261y (sN {m} Nm) (sN {n} Nn) Sm\u2264Sn = prf $ x\u2264y\u2192y-x+x\u2261y Nm Nn m\u2264n\n where\n postulate m\u2264n : LE m n\n {-# ATP prove m\u2264n #-}\n\n postulate prf : (n \u2238 m) + m \u2261 n \u2192 -- IH.\n (succ n \u2238 succ m) + succ m \u2261 succ n\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx0\u2192x-y0\u2192x-y0 = \u22a5-elim $ \u00acx>x zN 0>0\nx\u2265y\u2192y>0\u2192x-y0\u2192x-y0 = prf\n where\n postulate prf : LT (succ m \u2238 succ n) (succ m)\n {-# ATP prove prf x-y Derivative f (apply df).\n -- Now, we try to prove that if Derivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 Derivative f (apply df) \u2192 Derivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 \u2259-sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\nopen import Function\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n\n -- To avoid unification problems, use a one-field record.\n record _\u2259_ dx dy : Set a where\n -- doe = Delta-Observational Equivalence.\n constructor doe\n field\n proof : x \u229e dx \u2261 x \u229e dy\n\n open _\u2259_ public\n\n -- Same priority as \u2261\n infix 4 _\u2259_\n\n open import Relation.Binary\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = doe refl\n\n \u2259-sym : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-sym \u2259 = doe $ sym $ proof \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = doe $ trans (proof \u2259\u2081) (proof \u2259\u2082)\n\n -- That's standard congruence applied to \u2259\n \u2259-cong : \u2200 {b} {B : Set b}\n (f : A \u2192 B) {dx dy} \u2192 dx \u2259 dy \u2192 f (x \u229e dx) \u2261 f (x \u229e dy)\n \u2259-cong f da\u2259db = cong f $ proof da\u2259db\n\n \u2259-isEquivalence : IsEquivalence (_\u2259_)\n \u2259-isEquivalence = record\n { refl = \u2259-refl\n ; sym = \u2259-sym\n ; trans = \u2259-trans\n }\n\n \u2259-setoid : Setoid \u2113 a\n \u2259-setoid = record\n { Carrier = \u0394 x\n ; _\u2248_ = _\u2259_\n ; isEquivalence = \u2259-isEquivalence\n }\n\n\n ------------------------------------------------------------------------\n -- Convenient syntax for equational reasoning\n\n import Relation.Binary.EqReasoning as EqR\n\n module \u2259-Reasoning where\n open EqR \u2259-setoid public\n renaming (_\u2248\u27e8_\u27e9_ to _\u2259\u27e8_\u27e9_)\n\n -- By update-nil, if dx = nil x, then x \u229e dx \u2261 x.\n -- As a consequence, if dx \u2259 nil x, then x \u229e dx \u2261 x\n nil-is-\u229e-unit : \u2200 dx \u2192 dx \u2259 nil x \u2192 x \u229e dx \u2261 x\n nil-is-\u229e-unit dx dx\u2259nil-x =\n begin\n x \u229e dx\n \u2261\u27e8 proof dx\u2259nil-x \u27e9\n x \u229e (nil x)\n \u2261\u27e8 update-nil x \u27e9\n x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Here we prove the inverse:\n \u229e-unit-is-nil : \u2200 dx \u2192 x \u229e dx \u2261 x \u2192 dx \u2259 nil x\n \u229e-unit-is-nil dx x\u229edx\u2261x = doe $\n begin\n x \u229e dx\n \u2261\u27e8 x\u229edx\u2261x \u27e9\n x\n \u2261\u27e8 sym (update-nil x) \u27e9\n x \u229e nil x\n \u220e\n where\n open \u2261-Reasoning\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n -- This results pairs with update-diff.\n diff-update : \u2200 {dx} \u2192 (x \u229e dx) \u229f x \u2259 dx\n diff-update {dx} = doe lemma\n where\n lemma : x \u229e (x \u229e dx \u229f x) \u2261 x \u229e dx\n lemma = update-diff (x \u229e dx) x\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = doe lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 \u2259-cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 \u2259-cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n open import Postulate.Extensionality\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} df-x-dx\u2259dg-x-dx = doe lemma\u2082\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 x dx = proof $ df-x-dx\u2259dg-x-dx x dx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n\n -- We know that Derivative f (apply (nil f)) (by nil-is-derivative).\n -- That is, df = nil f -> Derivative f (apply df).\n -- Now, we try to prove that if Derivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 Derivative f (apply df) \u2192 Derivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 \u2259-sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"fcd30d55c5986f78d2f7059910fd4cf1194fd76d","subject":"reexport _\u00d7-cong_ _\u228e-cong_","message":"reexport _\u00d7-cong_ _\u228e-cong_\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Function\/Related\/TypeIsomorphisms\/NP.agda","new_file":"lib\/Function\/Related\/TypeIsomorphisms\/NP.agda","new_contents":"-- {-# OPTIONS --without-K #-}\nmodule Function.Related.TypeIsomorphisms.NP where\n\nimport Level as L\nopen import Algebra\nimport Algebra.FunctionProperties as FP\nopen L using (Lift; lower; lift)\nopen import Type hiding (\u2605)\nopen import Function using (_\u02e2_; const)\n\nopen import Data.Fin using (Fin; zero; suc; pred)\nopen import Data.Vec.NP using (Vec; []; _\u2237_; uncons; \u2237-uncons)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_) renaming (_^_ to _**_)\nopen import Data.Maybe.NP\nopen import Data.Product.NP renaming (map to map\u00d7)\n--open import Data.Product.N-ary\nopen import Data.Sum renaming (map to map\u228e)\nopen import Data.One\nopen import Data.Zero\nopen import Data.Two using (\ud835\udfda; 0\u2082; 1\u2082; proj; \u2713; not; \u2261\u2192\u2713; \u2261\u2192\u2713not; \u2713\u2192\u2261; \u2713not\u2192\u2261 ; not-involutive)\n\nimport Function.NP as F\nopen F using (\u03a0)\nimport Function.Equality as FE\nopen FE using (_\u27e8$\u27e9_)\nopen import Function.Related as FR\nopen import Function.Related.TypeIsomorphisms public\nimport Function.Inverse.NP as Inv\nopen Inv using (_\u2194_; _\u2218_; sym; id; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nopen import Relation.Binary\nimport Relation.Binary.Indexed as I\nopen import Data.Indexed using (_\u228e\u00b0_)\nopen import Relation.Binary.Product.Pointwise public using (_\u00d7-cong_)\nopen import Relation.Binary.Sum public using (_\u228e-cong_)\nopen import Relation.Binary.Product.Pointwise\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_ ; _\u2262_; _\u2257_)\n\nmodule _ {A : Set} {p q : A \u2192 \ud835\udfda} where\n \u03a3AP : Set\n \u03a3AP = \u03a3 A (\u03bb x \u2192 p x \u2261 1\u2082)\n\n \u03a3AQ : Set\n \u03a3AQ = \u03a3 A (\u03bb x \u2192 q x \u2261 1\u2082)\n\n \u03a3AP\u00acQ : Set\n \u03a3AP\u00acQ = \u03a3 A (\u03bb x \u2192 p x \u2261 1\u2082 \u00d7 q x \u2261 0\u2082)\n\n \u03a3A\u00acPQ : Set\n \u03a3A\u00acPQ = \u03a3 A (\u03bb x \u2192 p x \u2261 0\u2082 \u00d7 q x \u2261 1\u2082)\n\n {-\n module M\n (f : \u03a3AP \u2192 \u03a3AQ)\n (f-1 : \u03a3AQ \u2192 \u03a3AP)\n (f-1f : \u2200 x \u2192 f-1 (f x) \u2261 x)\n (ff-1 : \u2200 x \u2192 f (f-1 x) \u2261 x)\n where\n\n f' : \u03a3AP\u00acQ \u2192 \u03a3A\u00acPQ\n f' (x , (p , nq)) = let y = f (x , p) in proj\u2081 y , {!proj\u2082 y!} , (proj\u2082 y)\n\n f-1' : \u03a3A\u00acPQ \u2192 \u03a3AP\u00acQ\n f-1' = {!!}\n\n f-1f' : \u2200 x \u2192 f-1' (f' x) \u2261 x\n f-1f' = {!!}\n\n ff-1' : \u2200 x \u2192 f' (f-1' x) \u2261 x\n ff-1' = {!!}\n -}\n\n module Work-In-Progress\n (f' : \u03a3AP\u00acQ \u2192 \u03a3A\u00acPQ)\n (f-1' : \u03a3A\u00acPQ \u2192 \u03a3AP\u00acQ)\n (f-1f' : \u2200 x \u2192 f-1' (f' x) \u2261 x)\n (ff-1' : \u2200 x \u2192 f' (f-1' x) \u2261 x)\n where\n\n f : (x : A) \u2192 p x \u2261 1\u2082 \u2192 q x \u2261 0\u2082 \u2192 \u03a3A\u00acPQ\n f x px qx = f' (x , (px , qx))\n\n f-1 : (x : A) \u2192 p x \u2261 0\u2082 \u2192 q x \u2261 1\u2082 \u2192 \u03a3AP\u00acQ\n f-1 x px qx = f-1' (x , (px , qx))\n\n f-1f : \u2200 x px nqx \u2192\n let y = proj\u2082 (f x px nqx) in proj\u2081 (f-1 (proj\u2081 (f x px nqx)) (proj\u2081 y) (proj\u2082 y)) \u2261 x\n f-1f x px nqx = \u2261.cong proj\u2081 (f-1f' (x , (px , nqx)))\n\n ff-1 : \u2200 x px nqx \u2192\n let y = proj\u2082 (f-1 x px nqx) in proj\u2081 (f (proj\u2081 (f-1 x px nqx)) (proj\u2081 y) (proj\u2082 y)) \u2261 x\n ff-1 x px nqx = \u2261.cong proj\u2081 (ff-1' (x , (px , nqx)))\n\n \u03c0' : (x : A) (px qx : \ud835\udfda) \u2192 p x \u2261 px \u2192 q x \u2261 qx \u2192 A\n \u03c0' x 1\u2082 1\u2082 px qx = x\n \u03c0' x 1\u2082 0\u2082 px qx = proj\u2081 (f x px qx)\n \u03c0' x 0\u2082 1\u2082 px qx = proj\u2081 (f-1 x px qx)\n \u03c0' x 0\u2082 0\u2082 px qx = x\n\n \u03c0 : A \u2192 A\n \u03c0 x = \u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n 0\u22621 : 0\u2082 \u2262 1\u2082\n 0\u22621 ()\n\n \u03c001 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px0 : p x \u2261 0\u2082) (qx1 : q x \u2261 1\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 0\u2082 1\u2082 px0 qx1\n \u03c001 x 1\u2082 _ ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym px0) ppx))\n \u03c001 x 0\u2082 1\u2082 ppx qqx px0 qx1 = \u2261.cong\u2082 (\u03bb z1 z2 \u2192 proj\u2081 (f-1 x z1 z2)) (\u2261.proof-irrelevance ppx px0) (\u2261.proof-irrelevance qqx qx1)\n \u03c001 x 0\u2082 0\u2082 ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qqx) qx1))\n\n \u03c010 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px1 : p x \u2261 1\u2082) (qx0 : q x \u2261 0\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 1\u2082 0\u2082 px1 qx0\n \u03c010 x 0\u2082 _ ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym ppx) px1))\n \u03c010 x 1\u2082 0\u2082 ppx qqx px1 qx0 = \u2261.cong\u2082 (\u03bb z1 z2 \u2192 proj\u2081 (f x z1 z2)) (\u2261.proof-irrelevance ppx px1) (\u2261.proof-irrelevance qqx qx0)\n \u03c010 x 1\u2082 1\u2082 ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qx0) qqx))\n\n \u03c0'bb : \u2200 {b} x (px : p x \u2261 b) (qx : q x \u2261 b) ppx qqx ([ppx] : p x \u2261 ppx) ([qqx] : q x \u2261 qqx) \u2192 \u03c0' x ppx qqx [ppx] [qqx] \u2261 x\n \u03c0'bb x px qx 1\u2082 1\u2082 [ppx] [qqx] = \u2261.refl\n \u03c0'bb x px qx 1\u2082 0\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [qqx]) (\u2261.trans qx (\u2261.trans (\u2261.sym px) [ppx]))))\n \u03c0'bb x px qx 0\u2082 1\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [ppx]) (\u2261.trans px (\u2261.trans (\u2261.sym qx) [qqx]))))\n \u03c0'bb x px qx 0\u2082 0\u2082 [ppx] [qqx] = \u2261.refl\n\n \u03c0\u03c0' : \u2200 x px qx [px] [qx] \u2192 let y = (\u03c0' x px qx [px] [qx]) in \u03c0' y (p y) (q y) \u2261.refl \u2261.refl \u2261 x\n \u03c0\u03c0' x 1\u2082 1\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n \u03c0\u03c0' x 1\u2082 0\u2082 px qx = let fx = f x px qx in let pfx = proj\u2081 (proj\u2082 fx) in let qfx = proj\u2082 (proj\u2082 fx) in \u2261.trans (\u03c001 (proj\u2081 fx) (p (proj\u2081 fx)) (q (proj\u2081 fx)) \u2261.refl \u2261.refl pfx qfx) (f-1f x px qx)\n \u03c0\u03c0' x 0\u2082 1\u2082 px qx = let fx = f-1 x px qx in let pfx = proj\u2081 (proj\u2082 fx) in let qfx = proj\u2082 (proj\u2082 fx) in \u2261.trans (\u03c010 (proj\u2081 fx) (p (proj\u2081 fx)) (q (proj\u2081 fx)) \u2261.refl \u2261.refl pfx qfx) (ff-1 x px qx)\n \u03c0\u03c0' x 0\u2082 0\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n\n \u03c0\u03c0 : \u2200 x \u2192 \u03c0 (\u03c0 x) \u2261 x\n \u03c0\u03c0 x = \u03c0\u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n prop' : \u2200 px qx x ([px] : p x \u2261 px) ([qx] : q x \u2261 qx) \u2192 q (\u03c0' x px qx [px] [qx]) \u2261 px\n prop' 1\u2082 1\u2082 x px qx = qx\n prop' 1\u2082 0\u2082 x px qx = proj\u2082 (proj\u2082 (f x px qx))\n prop' 0\u2082 1\u2082 x px qx = proj\u2082 (proj\u2082 (f-1 x px qx))\n prop' 0\u2082 0\u2082 x px qx = qx\n\n prop : \u2200 x \u2192 p x \u2261 q (\u03c0 x)\n prop x = \u2261.sym (prop' (p x) (q x) x \u2261.refl \u2261.refl)\n\nmodule _ {a b f} {A : Set a} {B : A \u2192 Set b}\n (F : (x : A) \u2192 B x \u2192 Set f) where\n\n -- Also called Axiom of dependent choice.\n dep-choice-iso : (\u03a0 A (\u03bb x \u2192 \u03a3 (B x) (F x)))\n \u2194 (\u03a3 (\u03a0 A B) \u03bb f \u2192 \u03a0 A (F \u02e2 f))\n dep-choice-iso = inverses (\u21d2) (uncurry <_,_>) (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n where\n \u21d2 = \u03bb f \u2192 (\u03bb x \u2192 proj\u2081 (f x)) , (\u03bb x \u2192 proj\u2082 (f x))\n\nMaybe-injective : \u2200 {A B : Set} \u2192 Maybe A \u2194 Maybe B \u2192 A \u2194 B\nMaybe-injective f = Iso.iso (g f) (g-empty f)\n module Maybe-injective where\n open Inverse using (injective; left-inverse-of; right-inverse-of)\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (tof-tot : \u2200 x \u2192 to f (just x) \u2262 nothing)\n (fof-tot : \u2200 x \u2192 from f (just x) \u2262 nothing) where\n\n CancelMaybe' : A \u2194 B\n CancelMaybe' = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n\n \u21d2 : _ \u2192 _\n \u21d2 x with to f (just x) | tof-tot x\n \u21d2 x | just y | _ = y\n \u21d2 x | nothing | p = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d0 : _ \u2192 _\n \u21d0 x with from f (just x) | fof-tot x\n \u21d0 x | just y | p = y\n \u21d0 x | nothing | p = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 x with to f (just x)\n | tof-tot x\n | from f (just (\u21d2 x))\n | fof-tot (\u21d2 x)\n | left-inverse-of f (just x)\n | right-inverse-of f (just (\u21d2 x))\n \u21d0\u21d2 x | just x\u2081 | p | just x\u2082 | q | b | c = just-injective (\u2261.trans (\u2261.sym (left-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong (from f) c) b))\n \u21d0\u21d2 x | just x\u2081 | p | nothing | q | _ | _ = \ud835\udfd8-elim (q \u2261.refl)\n \u21d0\u21d2 x | nothing | p | z | q | _ | _ = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 x with from f (just x)\n | fof-tot x\n | to f (just (\u21d0 x))\n | tof-tot (\u21d0 x)\n | right-inverse-of f (just x)\n | left-inverse-of f (just (\u21d0 x))\n \u21d2\u21d0 x | just x\u2081 | p | just x\u2082 | q | b | c = just-injective (\u2261.trans (\u2261.sym (right-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong (to f) c) b))\n \u21d2\u21d0 x | just x\u2081 | p | nothing | q | _ | _ = \ud835\udfd8-elim (q \u2261.refl)\n \u21d2\u21d0 x | nothing | p | z | q | _ | _ = \ud835\udfd8-elim (p \u2261.refl)\n\n module Iso {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (f-empty : to f nothing \u2261 nothing) where\n\n tof-tot : \u2200 x \u2192 to f (just x) \u2262 nothing\n tof-tot x eq with injective f (\u2261.trans eq (\u2261.sym f-empty))\n ... | ()\n\n f-empty' : from f nothing \u2261 nothing\n f-empty' = \u2261.trans (\u2261.sym (\u2261.cong (from f) f-empty)) (left-inverse-of f nothing)\n\n fof-tot : \u2200 x \u2192 from f (just x) \u2262 nothing\n fof-tot x eq with injective (sym f) (\u2261.trans eq (\u2261.sym f-empty'))\n ... | ()\n\n iso : A \u2194 B\n iso = CancelMaybe' f tof-tot fof-tot\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B) where\n\n g : Maybe A \u2194 Maybe B\n g = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n\n \u21d2 : Maybe A \u2192 Maybe B\n \u21d2 (just x) with to f (just x)\n ... | nothing = to f nothing\n ... | just y = just y\n \u21d2 nothing = nothing\n\n \u21d0 : Maybe B \u2192 Maybe A\n \u21d0 (just x) with from f (just x)\n ... | nothing = from f nothing\n ... | just y = just y\n \u21d0 nothing = nothing\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (just x) with to f (just x) | left-inverse-of f (just x)\n \u21d0\u21d2 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d0\u21d2 (just x) | nothing | p with to f nothing | left-inverse-of f nothing\n \u21d0\u21d2 (just x) | nothing | p | just _ | q rewrite q = p\n \u21d0\u21d2 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d0\u21d2 nothing = \u2261.refl\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (just x) with from f (just x) | right-inverse-of f (just x)\n \u21d2\u21d0 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d2\u21d0 (just x) | nothing | p with from f nothing | right-inverse-of f nothing\n \u21d2\u21d0 (just x) | nothing | p | just _ | q rewrite q = p\n \u21d2\u21d0 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d2\u21d0 nothing = \u2261.refl\n\n g-empty : to g nothing \u2261 nothing\n g-empty = \u2261.refl\n\nprivate\n Setoid\u2080 : \u2605 _\n Setoid\u2080 = Setoid L.zero L.zero\n\n\u03a3\u2261\u2194\ud835\udfd9 : \u2200 {a} {A : \u2605 a} x \u2192 \u03a3 A (_\u2261_ x) \u2194 \ud835\udfd9\n\u03a3\u2261\u2194\ud835\udfd9 x = inverses (F.const _) (\u03bb _ \u2192 _ , \u2261.refl)\n helper (\u03bb _ \u2192 \u2261.refl)\n where helper : (y : \u03a3 _ (_\u2261_ x)) \u2192 (x , \u2261.refl) \u2261 y\n helper (.x , \u2261.refl) = \u2261.refl\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : \u03a3 A B \u2192 \u2605 c} where\n curried : \u03a0 (\u03a3 A B) C \u2194 \u03a0 A \u03bb a \u2192 \u03a0 (B a) \u03bb b \u2192 C (a , b)\n curried = inverses curry uncurry (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u2192 \u2605 c} (f : A \u2194 B) where\n private\n left-f = Inverse.left-inverse-of f\n right-f = Inverse.right-inverse-of f\n coe : \u2200 x \u2192 C x \u2192 C (from f (to f x))\n coe x = \u2261.subst C (\u2261.sym (left-f x))\n \u21d2 : \u03a3 A C \u2192 \u03a3 B (C F.\u2218 from f)\n \u21d2 (x , p) = to f x , coe x p\n \u21d0 : \u03a3 B (C F.\u2218 from f) \u2192 \u03a3 A C\n \u21d0 = first (from f)\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , p) rewrite left-f x = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 p = mk\u03a3\u2261 (C F.\u2218 from f) (right-f (proj\u2081 p)) (helper p)\n where\n helper : \u2200 p \u2192 \u2261.subst (C F.\u2218 from f) (right-f (proj\u2081 p)) (coe (proj\u2081 (\u21d0 p)) (proj\u2082 (\u21d0 p))) \u2261 proj\u2082 p\n helper p with to f (from f (proj\u2081 p)) | right-f (proj\u2081 p) | left-f (from f (proj\u2081 p))\n helper _ | ._ | \u2261.refl | \u2261.refl = \u2261.refl\n first-iso : \u03a3 A C \u2194 \u03a3 B (C F.\u2218 from f)\n first-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : B \u2192 \u2605 c} (f : A \u2194 B) where\n sym-first-iso : \u03a3 A (C F.\u2218 to f) \u2194 \u03a3 B C\n sym-first-iso = sym (first-iso (sym f))\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} (f : \u2200 x \u2192 B x \u2194 C x) where\n private\n left-f = Inverse.left-inverse-of F.\u2218 f\n right-f = Inverse.right-inverse-of F.\u2218 f\n \u21d2 : \u03a3 A B \u2192 \u03a3 A C\n \u21d2 = second (to (f _))\n \u21d0 : \u03a3 A C \u2192 \u03a3 A B\n \u21d0 = second (from (f _))\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , y) rewrite left-f x y = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (x , y) rewrite right-f x y = \u2261.refl\n second-iso : \u03a3 A B \u2194 \u03a3 A C\n second-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b} {A : \u2605 a} {B : \u2605 b} (f\u2080 f\u2081 : A \u2194 B) where\n \ud835\udfda\u00d7-second-iso : (\ud835\udfda \u00d7 A) \u2194 (\ud835\udfda \u00d7 B)\n \ud835\udfda\u00d7-second-iso = second-iso {A = \ud835\udfda} {B = const A} {C = const B} (proj (f\u2080 , f\u2081))\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a3 (A \u228e B) C\n T = \u03a3 A (C F.\u2218 inj\u2081) \u228e \u03a3 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 (inj\u2081 x , y) = inj\u2081 (x , y)\n \u21d2 (inj\u2082 x , y) = inj\u2082 (x , y)\n \u21d0 : T \u2192 S\n \u21d0 (inj\u2081 (x , y)) = inj\u2081 x , y\n \u21d0 (inj\u2082 (x , y)) = inj\u2082 x , y\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (inj\u2081 _ , _) = \u2261.refl\n \u21d0\u21d2 (inj\u2082 _ , _) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n \u03a3\u228e-distrib : (\u03a3 (A \u228e B) C) \u2194 (\u03a3 A (C F.\u2218 inj\u2081) \u228e \u03a3 B (C F.\u2218 inj\u2082))\n \u03a3\u228e-distrib = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} where\n private\n S = \u03a3 A (B \u228e\u00b0 C)\n T = \u03a3 A B \u228e \u03a3 A C\n \u21d2 : S \u2192 T\n \u21d2 (x , inj\u2081 y) = inj\u2081 (x , y)\n \u21d2 (x , inj\u2082 y) = inj\u2082 (x , y)\n \u21d0 : T \u2192 S\n \u21d0 (inj\u2081 (x , y)) = x , inj\u2081 y\n \u21d0 (inj\u2082 (x , y)) = x , inj\u2082 y\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (_ , inj\u2081 _) = \u2261.refl\n \u21d0\u21d2 (_ , inj\u2082 _) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n \u03a3-\u228e-hom : \u03a3 A (B \u228e\u00b0 C) \u2194 (\u03a3 A B \u228e \u03a3 A C)\n \u03a3-\u228e-hom = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\n-- {-\n{- requires extensional equality\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a0 (A \u228e B) C\n T = \u03a0 A (C F.\u2218 inj\u2081) \u00d7 \u03a0 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 f = f F.\u2218 inj\u2081 , f F.\u2218 inj\u2082\n \u21d0 : T \u2192 S\n \u21d0 (f , g) = [ f , g ]\n \u21d0\u21d2 : \u2200 f x \u2192 \u21d0 (\u21d2 f) x \u2261 f x\n \u21d0\u21d2 f (inj\u2081 x) = \u2261.refl\n \u21d0\u21d2 f (inj\u2082 y) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (f , g) = \u2261.refl\n\n \u03a0\u00d7-distrib : S \u2194 T\n \u03a0\u00d7-distrib = inverses (\u21d2) (\u21d0) {!\u21d0\u21d2!} \u21d2\u21d0\n-}\n\n\u228e-ICommutativeMonoid : CommutativeMonoid _ _\n\u228e-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u228e-setoid_;\n \u03b5 = \u2261.setoid \ud835\udfd8;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u228e-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \ud835\udfd8) _\u228e-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = [ (\u03bb ()) , F.id ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = inj\u2082\n ; cong = \u2082\u223c\u2082\n }\n ; inverse-of = record\n { left-inverse-of = [ (\u03bb ()) , (\u03bb x \u2192 \u2082\u223c\u2082 refl) ]\n ; right-inverse-of = \u03bb x \u2192 refl\n }\n } where open Setoid A\n cong-to : Setoid._\u2248_ (\u2261.setoid \ud835\udfd8 \u228e-setoid A) =[ _ ]\u21d2 _\u2248_\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y) rewrite x\u223c\u2081y = refl\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = x\u223c\u2082y\n\n assoc : Associative _\u228e-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = [ [ inj\u2081 , inj\u2082 F.\u2218 inj\u2081 ] , inj\u2082 F.\u2218 inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = [ inj\u2081 F.\u2218 inj\u2081 , [ inj\u2081 F.\u2218 inj\u2082 , inj\u2082 ] ]\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = [ [ (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2081\u223c\u2081 (refl A))) , (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2082\u223c\u2082 (refl B)) ) ] , (\u03bb _ \u2192 \u2082\u223c\u2082 (refl C)) ]\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (refl A)) , [ (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2081\u223c\u2081 (refl B))) , (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2082\u223c\u2082 (refl C))) ] ]\n }\n } where\n open Setoid\n cong-to : _\u2248_ ((A \u228e-setoid B) \u228e-setoid C) =[ _ ]\u21d2 _\u2248_ (A \u228e-setoid (B \u228e-setoid C))\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2082 ()))\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 x\u223c\u2081y\n cong-to (\u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2082y)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = \u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)\n\n cong-from : _\u2248_ (A \u228e-setoid (B \u228e-setoid C)) =[ _ ]\u21d2 _\u2248_ ((A \u228e-setoid B) \u228e-setoid C)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 x\u223c\u2081y) = \u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2082 ()))\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 x\u223c\u2082y\n\n comm : Commutative _\u228e-setoid_\n comm A B = record\n { to = swap'\n ; from = swap'\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u228e-setoid B FE.\u27f6 B \u228e-setoid A\n swap' {A} {B} = record\n { _\u27e8$\u27e9_ = [ inj\u2082 , inj\u2081 ]\n ; cong = cong\n } where\n cong : Setoid._\u2248_ (A \u228e-setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (B \u228e-setoid A)\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2082\u223c\u2082 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2081\u223c\u2081 x\u223c\u2082y\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u228e-setoid B) (swap' FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (Setoid.refl A)) , (\u03bb _ \u2192 \u2082\u223c\u2082 (Setoid.refl B)) ]\n\n\u00d7-ICommutativeMonoid : CommutativeMonoid _ _\n\u00d7-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u00d7-setoid_;\n \u03b5 = \u2261.setoid \ud835\udfd9;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u00d7-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \ud835\udfd9) _\u00d7-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2082\n ; cong = proj\u2082\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 _ , x\n ; cong = \u03bb x \u2192 \u2261.refl , x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u2261.refl , (Setoid.refl A)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl A\n }\n }\n\n assoc : Associative _\u00d7-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n ; cong = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n }\n ; from = record\n { _\u27e8$\u27e9_ = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n ; cong = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb _ \u2192 Setoid.refl ((A \u00d7-setoid B) \u00d7-setoid C)\n ; right-inverse-of = \u03bb _ \u2192 Setoid.refl (A \u00d7-setoid (B \u00d7-setoid C))\n }\n }\n\n comm : Commutative _\u00d7-setoid_\n comm A B = record\n { to = swap' {A} {B}\n ; from = swap' {B} {A}\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u00d7-setoid B FE.\u27f6 B \u00d7-setoid A\n swap' {A}{B} = record { _\u27e8$\u27e9_ = swap; cong = swap }\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u00d7-setoid B) (swap' {B} {A} FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = \u03bb x \u2192 Setoid.refl (A \u00d7-setoid B)\n\n\u00d7\u228e-ICommutativeSemiring : CommutativeSemiring _ _\n\u00d7\u228e-ICommutativeSemiring = record\n { _\u2248_ = Inv.Inverse\n ; _+_ = _\u228e-setoid_\n ; _*_ = _\u00d7-setoid_\n ; 0# = \u2261.setoid \ud835\udfd8\n ; 1# = \u2261.setoid \ud835\udfd9\n ; isCommutativeSemiring = record\n { +-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u228e-ICommutativeMonoid\n ; *-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u00d7-ICommutativeMonoid\n ; distrib\u02b3 = distrib\u02b3\n ; zero\u02e1 = zero\u02e1\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n distrib\u02b3 : _\u00d7-setoid_ DistributesOver\u02b3 _\u228e-setoid_\n distrib\u02b3 A B C = record\n { to = record\n { _\u27e8$\u27e9_ = uncurry [ curry inj\u2081 , curry inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = frm\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = uncurry [ (\u03bb x y \u2192 (\u2081\u223c\u2081 (refl B)) , (refl A)) , (\u03bb x y \u2192 (\u2082\u223c\u2082 (refl C)) , (refl A)) ]\n ; right-inverse-of = [ uncurry (\u03bb x y \u2192 \u2081\u223c\u2081 ((refl B {x}) , (refl A {y})))\n , uncurry (\u03bb x y \u2192 \u2082\u223c\u2082 ((refl C {x}) , (refl A {y}))) ]\n }\n } where\n open Setoid\n A' = Carrier A\n B' = Carrier B\n C' = Carrier C\n\n cong-to : _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A) =[ _ ]\u21d2 _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A))\n cong-to (\u2081\u223c\u2082 () , _)\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y , A-rel) = \u2081\u223c\u2081 (x\u223c\u2081y , A-rel)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y , A-rel) = \u2082\u223c\u2082 (x\u223c\u2082y , A-rel)\n\n frm : B' \u00d7 A' \u228e C' \u00d7 A' \u2192 (B' \u228e C') \u00d7 A'\n frm = [ map\u00d7 inj\u2081 F.id , map\u00d7 inj\u2082 F.id ]\n\n cong-from : _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A)) =[ _ ]\u21d2 _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 (B-rel , A-rel)) = \u2081\u223c\u2081 B-rel , A-rel\n cong-from (\u2082\u223c\u2082 (C-rel , A-rel)) = \u2082\u223c\u2082 C-rel , A-rel\n\n zero\u02e1 : LeftZero (\u2261.setoid \ud835\udfd8) _\u00d7-setoid_\n zero\u02e1 A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2081\n ; cong = proj\u2081\n }\n ; from = record\n { _\u27e8$\u27e9_ = \ud835\udfd8-elim\n ; cong = \u03bb { {()} x }\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \ud835\udfd8-elim (proj\u2081 x)\n ; right-inverse-of = \u03bb x \u2192 \ud835\udfd8-elim x\n }\n }\n\nmodule \u00d7-CMon {a} = CommutativeMonoid (\u00d7-CommutativeMonoid FR.bijection a)\nmodule \u228e-CMon {a} = CommutativeMonoid (\u228e-CommutativeMonoid FR.bijection a)\nmodule \u00d7\u228e\u00b0 {a} = CommutativeSemiring (\u00d7\u228e-CommutativeSemiring FR.bijection a)\n\nmodule \u00d7-ICMon = CommutativeMonoid \u00d7-ICommutativeMonoid\nmodule \u228e-ICMon = CommutativeMonoid \u228e-ICommutativeMonoid\nmodule \u00d7\u228e\u00b0I = CommutativeSemiring \u00d7\u228e-ICommutativeSemiring\n\nlift-\u228e : \u2200 {A B : Set} \u2192 Inv.Inverse ((\u2261.setoid A) \u228e-setoid (\u2261.setoid B)) (\u2261.setoid (A \u228e B))\nlift-\u228e {A}{B} = record\n { to = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = cong\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = \u03bb x \u2192 Setoid.reflexive (\u2261.setoid A \u228e-setoid \u2261.setoid B) x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid A \u228e-setoid \u2261.setoid B)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid (A \u228e B))\n }\n } where\n cong : Setoid._\u2248_ (\u2261.setoid A \u228e-setoid \u2261.setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (\u2261.setoid (A \u228e B))\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2261.cong inj\u2081 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2261.cong inj\u2082 x\u223c\u2082y\n\nswap-iso : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 (A \u00d7 B) \u2194 (B \u00d7 A)\nswap-iso = inverses swap swap (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u00d7 B \u2192 \u2605 c} where\n \u03a3\u00d7-swap : \u03a3 (A \u00d7 B) C \u2194 \u03a3 (B \u00d7 A) (C F.\u2218 swap)\n \u03a3\u00d7-swap = first-iso swap-iso\n\nMaybe\u2194Lift\ud835\udfd9\u228e : \u2200 {\u2113 a} {A : \u2605 a} \u2192 Maybe A \u2194 (Lift {\u2113 = \u2113} \ud835\udfd9 \u228e A)\nMaybe\u2194Lift\ud835\udfd9\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe\u2194\ud835\udfd9\u228e : \u2200 {a} {A : \u2605 a} \u2192 Maybe A \u2194 (\ud835\udfd9 \u228e A)\nMaybe\u2194\ud835\udfd9\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 A \u2194 B \u2192 Maybe A \u2194 Maybe B\nMaybe-cong A\u2194B = sym Maybe\u2194\ud835\udfd9\u228e \u2218 id \u228e-cong A\u2194B \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe : \u2200 {a} {A : \u2605 a} n \u2192 Maybe (Maybe^ n A) \u2194 Maybe^ n (Maybe A)\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe zero = id\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe (suc n) = Maybe-cong (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n)\n\nMaybe^-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} n \u2192 A \u2194 B \u2192 Maybe^ n A \u2194 Maybe^ n B\nMaybe^-cong zero = F.id\nMaybe^-cong (suc n) = Maybe-cong F.\u2218 Maybe^-cong n\n\nVec0\u2194Lift\ud835\udfd9 : \u2200 {a \u2113} {A : \u2605 a} \u2192 Vec A 0 \u2194 Lift {_} {\u2113} \ud835\udfd9\nVec0\u2194Lift\ud835\udfd9 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec0\u2194\ud835\udfd9 : \u2200 {a} {A : \u2605 a} \u2192 Vec A 0 \u2194 \ud835\udfd9\nVec0\u2194\ud835\udfd9 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec\u2218suc\u2194A\u00d7Vec : \u2200 {a} {A : \u2605 a} {n} \u2192 Vec A (suc n) \u2194 (A \u00d7 Vec A n)\nVec\u2218suc\u2194A\u00d7Vec\n = inverses uncons (uncurry _\u2237_) \u2237-uncons (\u03bb _ \u2192 \u2261.refl)\n\ninfix 8 _^_\n\n_^_ : \u2200 {a} \u2192 \u2605 a \u2192 \u2115 \u2192 \u2605 a\nA ^ 0 = Lift \ud835\udfd9\nA ^ suc n = A \u00d7 A ^ n\n\n^\u2194Vec : \u2200 {a} {A : \u2605 a} n \u2192 (A ^ n) \u2194 Vec A n\n^\u2194Vec zero = sym Vec0\u2194Lift\ud835\udfd9\n^\u2194Vec (suc n) = sym Vec\u2218suc\u2194A\u00d7Vec \u2218 (id \u00d7-cong (^\u2194Vec n))\n\nFin0\u2194\ud835\udfd8 : Fin 0 \u2194 \ud835\udfd8\nFin0\u2194\ud835\udfd8 = inverses (\u03bb()) (\u03bb()) (\u03bb()) (\u03bb())\n\nFin1\u2194\ud835\udfd9 : Fin 1 \u2194 \ud835\udfd9\nFin1\u2194\ud835\udfd9 = inverses _ (\u03bb _ \u2192 zero) \u21d0\u21d2 (\u03bb _ \u2192 \u2261.refl)\n where \u21d0\u21d2 : (_ : Fin 1) \u2192 _\n \u21d0\u21d2 zero = \u2261.refl\n \u21d0\u21d2 (suc ())\n\nFin\u2218suc\u2194Maybe\u2218Fin : \u2200 {n} \u2192 Fin (suc n) \u2194 Maybe (Fin n)\nFin\u2218suc\u2194Maybe\u2218Fin {n}\n = inverses to' (maybe suc zero)\n (\u03bb { zero \u2192 \u2261.refl ; (suc n) \u2192 \u2261.refl })\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n where to' : Fin (suc n) \u2192 Maybe (Fin n)\n to' zero = nothing\n to' (suc n) = just n\n\nFin-injective : \u2200 {m n} \u2192 Fin m \u2194 Fin n \u2192 m \u2261 n\nFin-injective = go _ _ where\n go : \u2200 m n \u2192 Fin m \u2194 Fin n \u2192 m \u2261 n\n go zero zero iso = \u2261.refl\n go zero (suc n) iso with from iso zero\n ... | ()\n go (suc m) zero iso with to iso zero\n ... | ()\n go (suc m) (suc n) iso = \u2261.cong suc (go m n (Maybe-injective (Fin\u2218suc\u2194Maybe\u2218Fin \u2218 iso \u2218 sym Fin\u2218suc\u2194Maybe\u2218Fin)))\n\nLift\u2194id : \u2200 {a} {A : \u2605 a} \u2192 Lift {a} {a} A \u2194 A\nLift\u2194id = inverses lower lift (\u03bb { (lift x) \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\n\ud835\udfd9\u00d7A\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd9 \u00d7 A) \u2194 A\n\ud835\udfd9\u00d7A\u2194A = proj\u2081 \u00d7-CMon.identity _ \u2218 sym Lift\u2194id \u00d7-cong id\n\nA\u00d7\ud835\udfd9\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u00d7 \ud835\udfd9) \u2194 A\nA\u00d7\ud835\udfd9\u2194A = proj\u2082 \u00d7-CMon.identity _ \u2218 id \u00d7-cong sym Lift\u2194id\n\n\u03a0\ud835\udfd9F\u2194F : \u2200 {\u2113} {F : \ud835\udfd9 \u2192 \u2605_ \u2113} \u2192 \u03a0 \ud835\udfd9 F \u2194 F _\n\u03a0\ud835\udfd9F\u2194F = inverses (\u03bb x \u2192 x _) const (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\n\ud835\udfd9\u2192A\u2194A : \u2200 {\u2113} {A : \u2605_ \u2113} \u2192 (\ud835\udfd9 \u2192 A) \u2194 A\n\ud835\udfd9\u2192A\u2194A = \u03a0\ud835\udfd9F\u2194F\n\nnot-\ud835\udfda\u2194\ud835\udfda : \ud835\udfda \u2194 \ud835\udfda\nnot-\ud835\udfda\u2194\ud835\udfda = inverses not not not-involutive not-involutive\n\n\u2261-iso : \u2200 {\u2113 \u2113'}{A : \u2605_ \u2113}{B : \u2605_ \u2113'}{x y : A} \u2192 (\u03c0 : A \u2194 B) \u2192 (x \u2261 y) \u2194 (to \u03c0 x \u2261 to \u03c0 y)\n\u2261-iso {x = x}{y} \u03c0 = inverses (\u2261.cong (to \u03c0))\n (\u03bb p \u2192 \u2261.trans (\u2261.sym (Inverse.left-inverse-of \u03c0 x))\n (\u2261.trans (\u2261.cong (from \u03c0) p)\n (Inverse.left-inverse-of \u03c0 y)))\n (\u03bb x \u2192 \u2261.proof-irrelevance _ x) (\u03bb x \u2192 \u2261.proof-irrelevance _ x)\n\nmodule _ {a} {A : \u2605_ a} (ext\ud835\udfd8 : (f g : \ud835\udfd8 \u2192 A) \u2192 f \u2261 g) where\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 : (\ud835\udfd8 \u2192 A) \u2194 \ud835\udfd9\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 = inverses _ (const (\u03bb())) (ext\ud835\udfd8 (\u03bb ())) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {F : \ud835\udfd8 \u2192 \u2605_ \u2113} (ext\ud835\udfd8 : (f g : \u03a0 \ud835\udfd8 F) \u2192 f \u2261 g) where\n \u03a0\ud835\udfd8F\u2194\ud835\udfd9 : \u03a0 \ud835\udfd8 F \u2194 \ud835\udfd9\n \u03a0\ud835\udfd8F\u2194\ud835\udfd9 = inverses _ (const (\u03bb())) (ext\ud835\udfd8 (\u03bb ())) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {F : \ud835\udfda \u2192 \u2605_ \u2113} (ext\ud835\udfda : {f g : \u03a0 \ud835\udfda F} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 : \u03a0 \ud835\udfda F \u2194 (F 0\u2082 \u00d7 F 1\u2082)\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 = inverses (\u03bb f \u2192 f 0\u2082 , f 1\u2082) proj\n (\u03bb f \u2192 ext\ud835\udfda (\u03bb { 0\u2082 \u2192 \u2261.refl ; 1\u2082 \u2192 \u2261.refl }))\n (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {A : \u2605_ \u2113} (ext\ud835\udfda : {f g : \ud835\udfda \u2192 A} \u2192 f \u2257 g \u2192 f \u2261 g) where\n \ud835\udfda\u2192A\u2194A\u00d7A : (\ud835\udfda \u2192 A) \u2194 (A \u00d7 A)\n \ud835\udfda\u2192A\u2194A\u00d7A = \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 ext\ud835\udfda\n\n\ud835\udfd8\u228eA\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd8 \u228e A) \u2194 A\n\ud835\udfd8\u228eA\u2194A = proj\u2081 \u228e-CMon.identity _ \u2218 sym Lift\u2194id \u228e-cong id\n\nA\u228e\ud835\udfd8\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u228e \ud835\udfd8) \u2194 A\nA\u228e\ud835\udfd8\u2194A = proj\u2082 \u228e-CMon.identity _ \u2218 id \u228e-cong sym Lift\u2194id\n\n\ud835\udfd8\u00d7A\u2194\ud835\udfd8 : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd8 \u00d7 A) \u2194 \ud835\udfd8\n\ud835\udfd8\u00d7A\u2194\ud835\udfd8 = Lift\u2194id \u2218 proj\u2081 \u00d7\u228e\u00b0.zero _ \u2218 sym (Lift\u2194id \u00d7-cong id)\n\nMaybe\ud835\udfd8\u2194\ud835\udfd9 : Maybe \ud835\udfd8 \u2194 \ud835\udfd9\nMaybe\ud835\udfd8\u2194\ud835\udfd9 = A\u228e\ud835\udfd8\u2194A \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe^\ud835\udfd8\u2194Fin : \u2200 n \u2192 Maybe^ n \ud835\udfd8 \u2194 Fin n\nMaybe^\ud835\udfd8\u2194Fin zero = sym Fin0\u2194\ud835\udfd8\nMaybe^\ud835\udfd8\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\ud835\udfd8\u2194Fin n)\n\nMaybe^\ud835\udfd9\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \ud835\udfd9 \u2194 Fin (suc n)\nMaybe^\ud835\udfd9\u2194Fin1+ n = Maybe^\ud835\udfd8\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\ud835\udfd8\u2194\ud835\udfd9)\n\nMaybe-\u228e : \u2200 {a} {A B : \u2605 a} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e {a} = sym Maybe\u2194Lift\ud835\udfd9\u228e \u2218 \u228e-CMon.assoc (Lift {_} {a} \ud835\udfd9) _ _ \u2218 (Maybe\u2194Lift\ud835\udfd9\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \ud835\udfd8 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \ud835\udfd8\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n\n\u03a3\ud835\udfda\u2194\u228e : \u2200 {a} (F : \ud835\udfda \u2192 \u2605_ a) \u2192 \u03a3 \ud835\udfda F \u2194 (F 0\u2082 \u228e F 1\u2082)\n\u03a3\ud835\udfda\u2194\u228e F = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d2 : (x : \u03a3 _ _) \u2192 _\n \u21d2 (0\u2082 , p) = inj\u2081 p\n \u21d2 (1\u2082 , p) = inj\u2082 p\n \u21d0 : (x : _ \u228e _) \u2192 _\n \u21d0 (inj\u2081 x) = 0\u2082 , x\n \u21d0 (inj\u2082 y) = 1\u2082 , y\n\n \u21d0\u21d2 : (_ : \u03a3 _ _) \u2192 _\n \u21d0\u21d2 (0\u2082 , p) = \u2261.refl\n \u21d0\u21d2 (1\u2082 , p) = \u2261.refl\n \u21d2\u21d0 : (_ : _ \u228e _) \u2192 _\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n\ud835\udfda\u2194\ud835\udfd9\u228e\ud835\udfd9 : \ud835\udfda \u2194 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2194\ud835\udfd9\u228e\ud835\udfd9 = inverses (proj (inj\u2081 _ , inj\u2082 _)) [ F.const 0\u2082 , F.const 1\u2082 ] \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d0\u21d2 : (_ : \ud835\udfda) \u2192 _\n \u21d0\u21d2 0\u2082 = \u2261.refl\n \u21d0\u21d2 1\u2082 = \u2261.refl\n \u21d2\u21d0 : (_ : \ud835\udfd9 \u228e \ud835\udfd9) \u2192 _\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\nFin-\u228e-+ : \u2200 m n \u2192 (Fin m \u228e Fin n) \u2194 Fin (m + n)\nFin-\u228e-+ m n = Maybe^\ud835\udfd8\u2194Fin (m + n)\n \u2218 Maybe^-\u228e-+ m n\n \u2218 sym (Maybe^\ud835\udfd8\u2194Fin m \u228e-cong Maybe^\ud835\udfd8\u2194Fin n)\n\nFin\u2218suc\u2194\ud835\udfd9\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2194 (\ud835\udfd9 \u228e Fin n)\nFin\u2218suc\u2194\ud835\udfd9\u228eFin = Maybe\u2194\ud835\udfd9\u228e \u2218 Fin\u2218suc\u2194Maybe\u2218Fin\n\nFin-\u00d7-* : \u2200 m n \u2192 (Fin m \u00d7 Fin n) \u2194 Fin (m * n)\nFin-\u00d7-* zero n = (Fin 0 \u00d7 Fin n) \u2194\u27e8 Fin0\u2194\ud835\udfd8 \u00d7-cong id \u27e9\n (\ud835\udfd8 \u00d7 Fin n) \u2194\u27e8 \ud835\udfd8\u00d7A\u2194\ud835\udfd8 \u27e9\n \ud835\udfd8 \u2194\u27e8 sym Fin0\u2194\ud835\udfd8 \u27e9\n Fin 0 \u220e\n where open EquationalReasoning hiding (sym)\nFin-\u00d7-* (suc m) n = (Fin (suc m) \u00d7 Fin n) \u2194\u27e8 Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u00d7-cong id \u27e9\n ((\ud835\udfd9 \u228e Fin m) \u00d7 Fin n) \u2194\u27e8 \u00d7\u228e\u00b0.distrib\u02b3 (Fin n) \ud835\udfd9 (Fin m) \u27e9\n ((\ud835\udfd9 \u00d7 Fin n) \u228e (Fin m \u00d7 Fin n)) \u2194\u27e8 \ud835\udfd9\u00d7A\u2194A \u228e-cong Fin-\u00d7-* m n \u27e9\n (Fin n \u228e Fin (m * n)) \u2194\u27e8 Fin-\u228e-+ n (m * n) \u27e9\n Fin (suc m * n) \u220e\n where open EquationalReasoning hiding (sym)\n\nFin\u228e-injective : \u2200 {A B : Set} n \u2192 (Fin n \u228e A) \u2194 (Fin n \u228e B) \u2192 A \u2194 B\nFin\u228e-injective zero f = \ud835\udfd8\u228eA\u2194A \u2218 Fin0\u2194\ud835\udfd8 \u228e-cong id \u2218 f \u2218 sym Fin0\u2194\ud835\udfd8 \u228e-cong id \u2218 sym \ud835\udfd8\u228eA\u2194A\nFin\u228e-injective (suc n) f =\n Fin\u228e-injective n\n (Maybe-injective\n (sym Maybe\u2194\ud835\udfd9\u228e \u2218\n \u228e-CMon.assoc _ _ _ \u2218\n Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u228e-cong id \u2218\n f \u2218\n sym Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u228e-cong id \u2218\n sym (\u228e-CMon.assoc _ _ _) \u2218\n Maybe\u2194\ud835\udfd9\u228e))\n\n{-\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"-- {-# OPTIONS --without-K #-}\nmodule Function.Related.TypeIsomorphisms.NP where\n\nimport Level as L\nopen import Algebra\nimport Algebra.FunctionProperties as FP\nopen L using (Lift; lower; lift)\nopen import Type hiding (\u2605)\nopen import Function using (_\u02e2_; const)\n\nopen import Data.Fin using (Fin; zero; suc; pred)\nopen import Data.Vec.NP using (Vec; []; _\u2237_; uncons; \u2237-uncons)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_) renaming (_^_ to _**_)\nopen import Data.Maybe.NP\nopen import Data.Product.NP renaming (map to map\u00d7)\n--open import Data.Product.N-ary\nopen import Data.Sum renaming (map to map\u228e)\nopen import Data.One\nopen import Data.Zero\nopen import Data.Two using (\ud835\udfda; 0\u2082; 1\u2082; proj; \u2713; not; \u2261\u2192\u2713; \u2261\u2192\u2713not; \u2713\u2192\u2261; \u2713not\u2192\u2261 ; not-involutive)\n\nimport Function.NP as F\nopen F using (\u03a0)\nimport Function.Equality as FE\nopen FE using (_\u27e8$\u27e9_)\nopen import Function.Related as FR\nopen import Function.Related.TypeIsomorphisms public\nimport Function.Inverse.NP as Inv\nopen Inv using (_\u2194_; _\u2218_; sym; id; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nopen import Relation.Binary\nimport Relation.Binary.Indexed as I\nopen import Data.Indexed using (_\u228e\u00b0_)\nopen import Relation.Binary.Product.Pointwise\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_ ; _\u2262_; _\u2257_)\n\nmodule _ {A : Set} {p q : A \u2192 \ud835\udfda} where\n \u03a3AP : Set\n \u03a3AP = \u03a3 A (\u03bb x \u2192 p x \u2261 1\u2082)\n\n \u03a3AQ : Set\n \u03a3AQ = \u03a3 A (\u03bb x \u2192 q x \u2261 1\u2082)\n\n \u03a3AP\u00acQ : Set\n \u03a3AP\u00acQ = \u03a3 A (\u03bb x \u2192 p x \u2261 1\u2082 \u00d7 q x \u2261 0\u2082)\n\n \u03a3A\u00acPQ : Set\n \u03a3A\u00acPQ = \u03a3 A (\u03bb x \u2192 p x \u2261 0\u2082 \u00d7 q x \u2261 1\u2082)\n\n {-\n module M\n (f : \u03a3AP \u2192 \u03a3AQ)\n (f-1 : \u03a3AQ \u2192 \u03a3AP)\n (f-1f : \u2200 x \u2192 f-1 (f x) \u2261 x)\n (ff-1 : \u2200 x \u2192 f (f-1 x) \u2261 x)\n where\n\n f' : \u03a3AP\u00acQ \u2192 \u03a3A\u00acPQ\n f' (x , (p , nq)) = let y = f (x , p) in proj\u2081 y , {!proj\u2082 y!} , (proj\u2082 y)\n\n f-1' : \u03a3A\u00acPQ \u2192 \u03a3AP\u00acQ\n f-1' = {!!}\n\n f-1f' : \u2200 x \u2192 f-1' (f' x) \u2261 x\n f-1f' = {!!}\n\n ff-1' : \u2200 x \u2192 f' (f-1' x) \u2261 x\n ff-1' = {!!}\n -}\n\n module Work-In-Progress\n (f' : \u03a3AP\u00acQ \u2192 \u03a3A\u00acPQ)\n (f-1' : \u03a3A\u00acPQ \u2192 \u03a3AP\u00acQ)\n (f-1f' : \u2200 x \u2192 f-1' (f' x) \u2261 x)\n (ff-1' : \u2200 x \u2192 f' (f-1' x) \u2261 x)\n where\n\n f : (x : A) \u2192 p x \u2261 1\u2082 \u2192 q x \u2261 0\u2082 \u2192 \u03a3A\u00acPQ\n f x px qx = f' (x , (px , qx))\n\n f-1 : (x : A) \u2192 p x \u2261 0\u2082 \u2192 q x \u2261 1\u2082 \u2192 \u03a3AP\u00acQ\n f-1 x px qx = f-1' (x , (px , qx))\n\n f-1f : \u2200 x px nqx \u2192\n let y = proj\u2082 (f x px nqx) in proj\u2081 (f-1 (proj\u2081 (f x px nqx)) (proj\u2081 y) (proj\u2082 y)) \u2261 x\n f-1f x px nqx = \u2261.cong proj\u2081 (f-1f' (x , (px , nqx)))\n\n ff-1 : \u2200 x px nqx \u2192\n let y = proj\u2082 (f-1 x px nqx) in proj\u2081 (f (proj\u2081 (f-1 x px nqx)) (proj\u2081 y) (proj\u2082 y)) \u2261 x\n ff-1 x px nqx = \u2261.cong proj\u2081 (ff-1' (x , (px , nqx)))\n\n \u03c0' : (x : A) (px qx : \ud835\udfda) \u2192 p x \u2261 px \u2192 q x \u2261 qx \u2192 A\n \u03c0' x 1\u2082 1\u2082 px qx = x\n \u03c0' x 1\u2082 0\u2082 px qx = proj\u2081 (f x px qx)\n \u03c0' x 0\u2082 1\u2082 px qx = proj\u2081 (f-1 x px qx)\n \u03c0' x 0\u2082 0\u2082 px qx = x\n\n \u03c0 : A \u2192 A\n \u03c0 x = \u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n 0\u22621 : 0\u2082 \u2262 1\u2082\n 0\u22621 ()\n\n \u03c001 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px0 : p x \u2261 0\u2082) (qx1 : q x \u2261 1\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 0\u2082 1\u2082 px0 qx1\n \u03c001 x 1\u2082 _ ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym px0) ppx))\n \u03c001 x 0\u2082 1\u2082 ppx qqx px0 qx1 = \u2261.cong\u2082 (\u03bb z1 z2 \u2192 proj\u2081 (f-1 x z1 z2)) (\u2261.proof-irrelevance ppx px0) (\u2261.proof-irrelevance qqx qx1)\n \u03c001 x 0\u2082 0\u2082 ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qqx) qx1))\n\n \u03c010 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px1 : p x \u2261 1\u2082) (qx0 : q x \u2261 0\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 1\u2082 0\u2082 px1 qx0\n \u03c010 x 0\u2082 _ ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym ppx) px1))\n \u03c010 x 1\u2082 0\u2082 ppx qqx px1 qx0 = \u2261.cong\u2082 (\u03bb z1 z2 \u2192 proj\u2081 (f x z1 z2)) (\u2261.proof-irrelevance ppx px1) (\u2261.proof-irrelevance qqx qx0)\n \u03c010 x 1\u2082 1\u2082 ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qx0) qqx))\n\n \u03c0'bb : \u2200 {b} x (px : p x \u2261 b) (qx : q x \u2261 b) ppx qqx ([ppx] : p x \u2261 ppx) ([qqx] : q x \u2261 qqx) \u2192 \u03c0' x ppx qqx [ppx] [qqx] \u2261 x\n \u03c0'bb x px qx 1\u2082 1\u2082 [ppx] [qqx] = \u2261.refl\n \u03c0'bb x px qx 1\u2082 0\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [qqx]) (\u2261.trans qx (\u2261.trans (\u2261.sym px) [ppx]))))\n \u03c0'bb x px qx 0\u2082 1\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [ppx]) (\u2261.trans px (\u2261.trans (\u2261.sym qx) [qqx]))))\n \u03c0'bb x px qx 0\u2082 0\u2082 [ppx] [qqx] = \u2261.refl\n\n \u03c0\u03c0' : \u2200 x px qx [px] [qx] \u2192 let y = (\u03c0' x px qx [px] [qx]) in \u03c0' y (p y) (q y) \u2261.refl \u2261.refl \u2261 x\n \u03c0\u03c0' x 1\u2082 1\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n \u03c0\u03c0' x 1\u2082 0\u2082 px qx = let fx = f x px qx in let pfx = proj\u2081 (proj\u2082 fx) in let qfx = proj\u2082 (proj\u2082 fx) in \u2261.trans (\u03c001 (proj\u2081 fx) (p (proj\u2081 fx)) (q (proj\u2081 fx)) \u2261.refl \u2261.refl pfx qfx) (f-1f x px qx)\n \u03c0\u03c0' x 0\u2082 1\u2082 px qx = let fx = f-1 x px qx in let pfx = proj\u2081 (proj\u2082 fx) in let qfx = proj\u2082 (proj\u2082 fx) in \u2261.trans (\u03c010 (proj\u2081 fx) (p (proj\u2081 fx)) (q (proj\u2081 fx)) \u2261.refl \u2261.refl pfx qfx) (ff-1 x px qx)\n \u03c0\u03c0' x 0\u2082 0\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n\n \u03c0\u03c0 : \u2200 x \u2192 \u03c0 (\u03c0 x) \u2261 x\n \u03c0\u03c0 x = \u03c0\u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n prop' : \u2200 px qx x ([px] : p x \u2261 px) ([qx] : q x \u2261 qx) \u2192 q (\u03c0' x px qx [px] [qx]) \u2261 px\n prop' 1\u2082 1\u2082 x px qx = qx\n prop' 1\u2082 0\u2082 x px qx = proj\u2082 (proj\u2082 (f x px qx))\n prop' 0\u2082 1\u2082 x px qx = proj\u2082 (proj\u2082 (f-1 x px qx))\n prop' 0\u2082 0\u2082 x px qx = qx\n\n prop : \u2200 x \u2192 p x \u2261 q (\u03c0 x)\n prop x = \u2261.sym (prop' (p x) (q x) x \u2261.refl \u2261.refl)\n\nmodule _ {a b f} {A : Set a} {B : A \u2192 Set b}\n (F : (x : A) \u2192 B x \u2192 Set f) where\n\n -- Also called Axiom of dependent choice.\n dep-choice-iso : (\u03a0 A (\u03bb x \u2192 \u03a3 (B x) (F x)))\n \u2194 (\u03a3 (\u03a0 A B) \u03bb f \u2192 \u03a0 A (F \u02e2 f))\n dep-choice-iso = inverses (\u21d2) (uncurry <_,_>) (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n where\n \u21d2 = \u03bb f \u2192 (\u03bb x \u2192 proj\u2081 (f x)) , (\u03bb x \u2192 proj\u2082 (f x))\n\nMaybe-injective : \u2200 {A B : Set} \u2192 Maybe A \u2194 Maybe B \u2192 A \u2194 B\nMaybe-injective f = Iso.iso (g f) (g-empty f)\n module Maybe-injective where\n open Inverse using (injective; left-inverse-of; right-inverse-of)\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (tof-tot : \u2200 x \u2192 to f (just x) \u2262 nothing)\n (fof-tot : \u2200 x \u2192 from f (just x) \u2262 nothing) where\n\n CancelMaybe' : A \u2194 B\n CancelMaybe' = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n\n \u21d2 : _ \u2192 _\n \u21d2 x with to f (just x) | tof-tot x\n \u21d2 x | just y | _ = y\n \u21d2 x | nothing | p = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d0 : _ \u2192 _\n \u21d0 x with from f (just x) | fof-tot x\n \u21d0 x | just y | p = y\n \u21d0 x | nothing | p = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 x with to f (just x)\n | tof-tot x\n | from f (just (\u21d2 x))\n | fof-tot (\u21d2 x)\n | left-inverse-of f (just x)\n | right-inverse-of f (just (\u21d2 x))\n \u21d0\u21d2 x | just x\u2081 | p | just x\u2082 | q | b | c = just-injective (\u2261.trans (\u2261.sym (left-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong (from f) c) b))\n \u21d0\u21d2 x | just x\u2081 | p | nothing | q | _ | _ = \ud835\udfd8-elim (q \u2261.refl)\n \u21d0\u21d2 x | nothing | p | z | q | _ | _ = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 x with from f (just x)\n | fof-tot x\n | to f (just (\u21d0 x))\n | tof-tot (\u21d0 x)\n | right-inverse-of f (just x)\n | left-inverse-of f (just (\u21d0 x))\n \u21d2\u21d0 x | just x\u2081 | p | just x\u2082 | q | b | c = just-injective (\u2261.trans (\u2261.sym (right-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong (to f) c) b))\n \u21d2\u21d0 x | just x\u2081 | p | nothing | q | _ | _ = \ud835\udfd8-elim (q \u2261.refl)\n \u21d2\u21d0 x | nothing | p | z | q | _ | _ = \ud835\udfd8-elim (p \u2261.refl)\n\n module Iso {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (f-empty : to f nothing \u2261 nothing) where\n\n tof-tot : \u2200 x \u2192 to f (just x) \u2262 nothing\n tof-tot x eq with injective f (\u2261.trans eq (\u2261.sym f-empty))\n ... | ()\n\n f-empty' : from f nothing \u2261 nothing\n f-empty' = \u2261.trans (\u2261.sym (\u2261.cong (from f) f-empty)) (left-inverse-of f nothing)\n\n fof-tot : \u2200 x \u2192 from f (just x) \u2262 nothing\n fof-tot x eq with injective (sym f) (\u2261.trans eq (\u2261.sym f-empty'))\n ... | ()\n\n iso : A \u2194 B\n iso = CancelMaybe' f tof-tot fof-tot\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B) where\n\n g : Maybe A \u2194 Maybe B\n g = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n\n \u21d2 : Maybe A \u2192 Maybe B\n \u21d2 (just x) with to f (just x)\n ... | nothing = to f nothing\n ... | just y = just y\n \u21d2 nothing = nothing\n\n \u21d0 : Maybe B \u2192 Maybe A\n \u21d0 (just x) with from f (just x)\n ... | nothing = from f nothing\n ... | just y = just y\n \u21d0 nothing = nothing\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (just x) with to f (just x) | left-inverse-of f (just x)\n \u21d0\u21d2 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d0\u21d2 (just x) | nothing | p with to f nothing | left-inverse-of f nothing\n \u21d0\u21d2 (just x) | nothing | p | just _ | q rewrite q = p\n \u21d0\u21d2 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d0\u21d2 nothing = \u2261.refl\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (just x) with from f (just x) | right-inverse-of f (just x)\n \u21d2\u21d0 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d2\u21d0 (just x) | nothing | p with from f nothing | right-inverse-of f nothing\n \u21d2\u21d0 (just x) | nothing | p | just _ | q rewrite q = p\n \u21d2\u21d0 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d2\u21d0 nothing = \u2261.refl\n\n g-empty : to g nothing \u2261 nothing\n g-empty = \u2261.refl\n\nprivate\n Setoid\u2080 : \u2605 _\n Setoid\u2080 = Setoid L.zero L.zero\n\n\u03a3\u2261\u2194\ud835\udfd9 : \u2200 {a} {A : \u2605 a} x \u2192 \u03a3 A (_\u2261_ x) \u2194 \ud835\udfd9\n\u03a3\u2261\u2194\ud835\udfd9 x = inverses (F.const _) (\u03bb _ \u2192 _ , \u2261.refl)\n helper (\u03bb _ \u2192 \u2261.refl)\n where helper : (y : \u03a3 _ (_\u2261_ x)) \u2192 (x , \u2261.refl) \u2261 y\n helper (.x , \u2261.refl) = \u2261.refl\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : \u03a3 A B \u2192 \u2605 c} where\n curried : \u03a0 (\u03a3 A B) C \u2194 \u03a0 A \u03bb a \u2192 \u03a0 (B a) \u03bb b \u2192 C (a , b)\n curried = inverses curry uncurry (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u2192 \u2605 c} (f : A \u2194 B) where\n private\n left-f = Inverse.left-inverse-of f\n right-f = Inverse.right-inverse-of f\n coe : \u2200 x \u2192 C x \u2192 C (from f (to f x))\n coe x = \u2261.subst C (\u2261.sym (left-f x))\n \u21d2 : \u03a3 A C \u2192 \u03a3 B (C F.\u2218 from f)\n \u21d2 (x , p) = to f x , coe x p\n \u21d0 : \u03a3 B (C F.\u2218 from f) \u2192 \u03a3 A C\n \u21d0 = first (from f)\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , p) rewrite left-f x = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 p = mk\u03a3\u2261 (C F.\u2218 from f) (right-f (proj\u2081 p)) (helper p)\n where\n helper : \u2200 p \u2192 \u2261.subst (C F.\u2218 from f) (right-f (proj\u2081 p)) (coe (proj\u2081 (\u21d0 p)) (proj\u2082 (\u21d0 p))) \u2261 proj\u2082 p\n helper p with to f (from f (proj\u2081 p)) | right-f (proj\u2081 p) | left-f (from f (proj\u2081 p))\n helper _ | ._ | \u2261.refl | \u2261.refl = \u2261.refl\n first-iso : \u03a3 A C \u2194 \u03a3 B (C F.\u2218 from f)\n first-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : B \u2192 \u2605 c} (f : A \u2194 B) where\n sym-first-iso : \u03a3 A (C F.\u2218 to f) \u2194 \u03a3 B C\n sym-first-iso = sym (first-iso (sym f))\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} (f : \u2200 x \u2192 B x \u2194 C x) where\n private\n left-f = Inverse.left-inverse-of F.\u2218 f\n right-f = Inverse.right-inverse-of F.\u2218 f\n \u21d2 : \u03a3 A B \u2192 \u03a3 A C\n \u21d2 = second (to (f _))\n \u21d0 : \u03a3 A C \u2192 \u03a3 A B\n \u21d0 = second (from (f _))\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , y) rewrite left-f x y = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (x , y) rewrite right-f x y = \u2261.refl\n second-iso : \u03a3 A B \u2194 \u03a3 A C\n second-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b} {A : \u2605 a} {B : \u2605 b} (f\u2080 f\u2081 : A \u2194 B) where\n \ud835\udfda\u00d7-second-iso : (\ud835\udfda \u00d7 A) \u2194 (\ud835\udfda \u00d7 B)\n \ud835\udfda\u00d7-second-iso = second-iso {A = \ud835\udfda} {B = const A} {C = const B} (proj (f\u2080 , f\u2081))\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a3 (A \u228e B) C\n T = \u03a3 A (C F.\u2218 inj\u2081) \u228e \u03a3 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 (inj\u2081 x , y) = inj\u2081 (x , y)\n \u21d2 (inj\u2082 x , y) = inj\u2082 (x , y)\n \u21d0 : T \u2192 S\n \u21d0 (inj\u2081 (x , y)) = inj\u2081 x , y\n \u21d0 (inj\u2082 (x , y)) = inj\u2082 x , y\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (inj\u2081 _ , _) = \u2261.refl\n \u21d0\u21d2 (inj\u2082 _ , _) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n \u03a3\u228e-distrib : (\u03a3 (A \u228e B) C) \u2194 (\u03a3 A (C F.\u2218 inj\u2081) \u228e \u03a3 B (C F.\u2218 inj\u2082))\n \u03a3\u228e-distrib = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} where\n private\n S = \u03a3 A (B \u228e\u00b0 C)\n T = \u03a3 A B \u228e \u03a3 A C\n \u21d2 : S \u2192 T\n \u21d2 (x , inj\u2081 y) = inj\u2081 (x , y)\n \u21d2 (x , inj\u2082 y) = inj\u2082 (x , y)\n \u21d0 : T \u2192 S\n \u21d0 (inj\u2081 (x , y)) = x , inj\u2081 y\n \u21d0 (inj\u2082 (x , y)) = x , inj\u2082 y\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (_ , inj\u2081 _) = \u2261.refl\n \u21d0\u21d2 (_ , inj\u2082 _) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n \u03a3-\u228e-hom : \u03a3 A (B \u228e\u00b0 C) \u2194 (\u03a3 A B \u228e \u03a3 A C)\n \u03a3-\u228e-hom = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\n-- {-\n{- requires extensional equality\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a0 (A \u228e B) C\n T = \u03a0 A (C F.\u2218 inj\u2081) \u00d7 \u03a0 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 f = f F.\u2218 inj\u2081 , f F.\u2218 inj\u2082\n \u21d0 : T \u2192 S\n \u21d0 (f , g) = [ f , g ]\n \u21d0\u21d2 : \u2200 f x \u2192 \u21d0 (\u21d2 f) x \u2261 f x\n \u21d0\u21d2 f (inj\u2081 x) = \u2261.refl\n \u21d0\u21d2 f (inj\u2082 y) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (f , g) = \u2261.refl\n\n \u03a0\u00d7-distrib : S \u2194 T\n \u03a0\u00d7-distrib = inverses (\u21d2) (\u21d0) {!\u21d0\u21d2!} \u21d2\u21d0\n-}\n\n\u228e-ICommutativeMonoid : CommutativeMonoid _ _\n\u228e-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u228e-setoid_;\n \u03b5 = \u2261.setoid \ud835\udfd8;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u228e-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \ud835\udfd8) _\u228e-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = [ (\u03bb ()) , F.id ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = inj\u2082\n ; cong = \u2082\u223c\u2082\n }\n ; inverse-of = record\n { left-inverse-of = [ (\u03bb ()) , (\u03bb x \u2192 \u2082\u223c\u2082 refl) ]\n ; right-inverse-of = \u03bb x \u2192 refl\n }\n } where open Setoid A\n cong-to : Setoid._\u2248_ (\u2261.setoid \ud835\udfd8 \u228e-setoid A) =[ _ ]\u21d2 _\u2248_\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y) rewrite x\u223c\u2081y = refl\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = x\u223c\u2082y\n\n assoc : Associative _\u228e-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = [ [ inj\u2081 , inj\u2082 F.\u2218 inj\u2081 ] , inj\u2082 F.\u2218 inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = [ inj\u2081 F.\u2218 inj\u2081 , [ inj\u2081 F.\u2218 inj\u2082 , inj\u2082 ] ]\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = [ [ (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2081\u223c\u2081 (refl A))) , (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2082\u223c\u2082 (refl B)) ) ] , (\u03bb _ \u2192 \u2082\u223c\u2082 (refl C)) ]\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (refl A)) , [ (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2081\u223c\u2081 (refl B))) , (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2082\u223c\u2082 (refl C))) ] ]\n }\n } where\n open Setoid\n cong-to : _\u2248_ ((A \u228e-setoid B) \u228e-setoid C) =[ _ ]\u21d2 _\u2248_ (A \u228e-setoid (B \u228e-setoid C))\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2082 ()))\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 x\u223c\u2081y\n cong-to (\u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2082y)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = \u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)\n\n cong-from : _\u2248_ (A \u228e-setoid (B \u228e-setoid C)) =[ _ ]\u21d2 _\u2248_ ((A \u228e-setoid B) \u228e-setoid C)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 x\u223c\u2081y) = \u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2082 ()))\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 x\u223c\u2082y\n\n comm : Commutative _\u228e-setoid_\n comm A B = record\n { to = swap'\n ; from = swap'\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u228e-setoid B FE.\u27f6 B \u228e-setoid A\n swap' {A} {B} = record\n { _\u27e8$\u27e9_ = [ inj\u2082 , inj\u2081 ]\n ; cong = cong\n } where\n cong : Setoid._\u2248_ (A \u228e-setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (B \u228e-setoid A)\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2082\u223c\u2082 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2081\u223c\u2081 x\u223c\u2082y\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u228e-setoid B) (swap' FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (Setoid.refl A)) , (\u03bb _ \u2192 \u2082\u223c\u2082 (Setoid.refl B)) ]\n\n\u00d7-ICommutativeMonoid : CommutativeMonoid _ _\n\u00d7-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u00d7-setoid_;\n \u03b5 = \u2261.setoid \ud835\udfd9;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u00d7-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \ud835\udfd9) _\u00d7-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2082\n ; cong = proj\u2082\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 _ , x\n ; cong = \u03bb x \u2192 \u2261.refl , x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u2261.refl , (Setoid.refl A)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl A\n }\n }\n\n assoc : Associative _\u00d7-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n ; cong = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n }\n ; from = record\n { _\u27e8$\u27e9_ = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n ; cong = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb _ \u2192 Setoid.refl ((A \u00d7-setoid B) \u00d7-setoid C)\n ; right-inverse-of = \u03bb _ \u2192 Setoid.refl (A \u00d7-setoid (B \u00d7-setoid C))\n }\n }\n\n comm : Commutative _\u00d7-setoid_\n comm A B = record\n { to = swap' {A} {B}\n ; from = swap' {B} {A}\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u00d7-setoid B FE.\u27f6 B \u00d7-setoid A\n swap' {A}{B} = record { _\u27e8$\u27e9_ = swap; cong = swap }\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u00d7-setoid B) (swap' {B} {A} FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = \u03bb x \u2192 Setoid.refl (A \u00d7-setoid B)\n\n\u00d7\u228e-ICommutativeSemiring : CommutativeSemiring _ _\n\u00d7\u228e-ICommutativeSemiring = record\n { _\u2248_ = Inv.Inverse\n ; _+_ = _\u228e-setoid_\n ; _*_ = _\u00d7-setoid_\n ; 0# = \u2261.setoid \ud835\udfd8\n ; 1# = \u2261.setoid \ud835\udfd9\n ; isCommutativeSemiring = record\n { +-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u228e-ICommutativeMonoid\n ; *-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u00d7-ICommutativeMonoid\n ; distrib\u02b3 = distrib\u02b3\n ; zero\u02e1 = zero\u02e1\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n distrib\u02b3 : _\u00d7-setoid_ DistributesOver\u02b3 _\u228e-setoid_\n distrib\u02b3 A B C = record\n { to = record\n { _\u27e8$\u27e9_ = uncurry [ curry inj\u2081 , curry inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = frm\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = uncurry [ (\u03bb x y \u2192 (\u2081\u223c\u2081 (refl B)) , (refl A)) , (\u03bb x y \u2192 (\u2082\u223c\u2082 (refl C)) , (refl A)) ]\n ; right-inverse-of = [ uncurry (\u03bb x y \u2192 \u2081\u223c\u2081 ((refl B {x}) , (refl A {y})))\n , uncurry (\u03bb x y \u2192 \u2082\u223c\u2082 ((refl C {x}) , (refl A {y}))) ]\n }\n } where\n open Setoid\n A' = Carrier A\n B' = Carrier B\n C' = Carrier C\n\n cong-to : _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A) =[ _ ]\u21d2 _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A))\n cong-to (\u2081\u223c\u2082 () , _)\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y , A-rel) = \u2081\u223c\u2081 (x\u223c\u2081y , A-rel)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y , A-rel) = \u2082\u223c\u2082 (x\u223c\u2082y , A-rel)\n\n frm : B' \u00d7 A' \u228e C' \u00d7 A' \u2192 (B' \u228e C') \u00d7 A'\n frm = [ map\u00d7 inj\u2081 F.id , map\u00d7 inj\u2082 F.id ]\n\n cong-from : _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A)) =[ _ ]\u21d2 _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 (B-rel , A-rel)) = \u2081\u223c\u2081 B-rel , A-rel\n cong-from (\u2082\u223c\u2082 (C-rel , A-rel)) = \u2082\u223c\u2082 C-rel , A-rel\n\n zero\u02e1 : LeftZero (\u2261.setoid \ud835\udfd8) _\u00d7-setoid_\n zero\u02e1 A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2081\n ; cong = proj\u2081\n }\n ; from = record\n { _\u27e8$\u27e9_ = \ud835\udfd8-elim\n ; cong = \u03bb { {()} x }\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \ud835\udfd8-elim (proj\u2081 x)\n ; right-inverse-of = \u03bb x \u2192 \ud835\udfd8-elim x\n }\n }\n\nmodule \u00d7-CMon {a} = CommutativeMonoid (\u00d7-CommutativeMonoid FR.bijection a)\nmodule \u228e-CMon {a} = CommutativeMonoid (\u228e-CommutativeMonoid FR.bijection a)\nmodule \u00d7\u228e\u00b0 {a} = CommutativeSemiring (\u00d7\u228e-CommutativeSemiring FR.bijection a)\n\nmodule \u00d7-ICMon = CommutativeMonoid \u00d7-ICommutativeMonoid\nmodule \u228e-ICMon = CommutativeMonoid \u228e-ICommutativeMonoid\nmodule \u00d7\u228e\u00b0I = CommutativeSemiring \u00d7\u228e-ICommutativeSemiring\n\nlift-\u228e : \u2200 {A B : Set} \u2192 Inv.Inverse ((\u2261.setoid A) \u228e-setoid (\u2261.setoid B)) (\u2261.setoid (A \u228e B))\nlift-\u228e {A}{B} = record\n { to = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = cong\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = \u03bb x \u2192 Setoid.reflexive (\u2261.setoid A \u228e-setoid \u2261.setoid B) x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid A \u228e-setoid \u2261.setoid B)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid (A \u228e B))\n }\n } where\n cong : Setoid._\u2248_ (\u2261.setoid A \u228e-setoid \u2261.setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (\u2261.setoid (A \u228e B))\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2261.cong inj\u2081 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2261.cong inj\u2082 x\u223c\u2082y\n\nswap-iso : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 (A \u00d7 B) \u2194 (B \u00d7 A)\nswap-iso = inverses swap swap (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u00d7 B \u2192 \u2605 c} where\n \u03a3\u00d7-swap : \u03a3 (A \u00d7 B) C \u2194 \u03a3 (B \u00d7 A) (C F.\u2218 swap)\n \u03a3\u00d7-swap = first-iso swap-iso\n\nMaybe\u2194Lift\ud835\udfd9\u228e : \u2200 {\u2113 a} {A : \u2605 a} \u2192 Maybe A \u2194 (Lift {\u2113 = \u2113} \ud835\udfd9 \u228e A)\nMaybe\u2194Lift\ud835\udfd9\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe\u2194\ud835\udfd9\u228e : \u2200 {a} {A : \u2605 a} \u2192 Maybe A \u2194 (\ud835\udfd9 \u228e A)\nMaybe\u2194\ud835\udfd9\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 A \u2194 B \u2192 Maybe A \u2194 Maybe B\nMaybe-cong A\u2194B = sym Maybe\u2194\ud835\udfd9\u228e \u2218 id \u228e-cong A\u2194B \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe : \u2200 {a} {A : \u2605 a} n \u2192 Maybe (Maybe^ n A) \u2194 Maybe^ n (Maybe A)\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe zero = id\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe (suc n) = Maybe-cong (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n)\n\nMaybe^-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} n \u2192 A \u2194 B \u2192 Maybe^ n A \u2194 Maybe^ n B\nMaybe^-cong zero = F.id\nMaybe^-cong (suc n) = Maybe-cong F.\u2218 Maybe^-cong n\n\nVec0\u2194Lift\ud835\udfd9 : \u2200 {a \u2113} {A : \u2605 a} \u2192 Vec A 0 \u2194 Lift {_} {\u2113} \ud835\udfd9\nVec0\u2194Lift\ud835\udfd9 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec0\u2194\ud835\udfd9 : \u2200 {a} {A : \u2605 a} \u2192 Vec A 0 \u2194 \ud835\udfd9\nVec0\u2194\ud835\udfd9 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec\u2218suc\u2194A\u00d7Vec : \u2200 {a} {A : \u2605 a} {n} \u2192 Vec A (suc n) \u2194 (A \u00d7 Vec A n)\nVec\u2218suc\u2194A\u00d7Vec\n = inverses uncons (uncurry _\u2237_) \u2237-uncons (\u03bb _ \u2192 \u2261.refl)\n\ninfix 8 _^_\n\n_^_ : \u2200 {a} \u2192 \u2605 a \u2192 \u2115 \u2192 \u2605 a\nA ^ 0 = Lift \ud835\udfd9\nA ^ suc n = A \u00d7 A ^ n\n\n^\u2194Vec : \u2200 {a} {A : \u2605 a} n \u2192 (A ^ n) \u2194 Vec A n\n^\u2194Vec zero = sym Vec0\u2194Lift\ud835\udfd9\n^\u2194Vec (suc n) = sym Vec\u2218suc\u2194A\u00d7Vec \u2218 (id \u00d7-cong (^\u2194Vec n))\n\nFin0\u2194\ud835\udfd8 : Fin 0 \u2194 \ud835\udfd8\nFin0\u2194\ud835\udfd8 = inverses (\u03bb()) (\u03bb()) (\u03bb()) (\u03bb())\n\nFin1\u2194\ud835\udfd9 : Fin 1 \u2194 \ud835\udfd9\nFin1\u2194\ud835\udfd9 = inverses _ (\u03bb _ \u2192 zero) \u21d0\u21d2 (\u03bb _ \u2192 \u2261.refl)\n where \u21d0\u21d2 : (_ : Fin 1) \u2192 _\n \u21d0\u21d2 zero = \u2261.refl\n \u21d0\u21d2 (suc ())\n\nFin\u2218suc\u2194Maybe\u2218Fin : \u2200 {n} \u2192 Fin (suc n) \u2194 Maybe (Fin n)\nFin\u2218suc\u2194Maybe\u2218Fin {n}\n = inverses to' (maybe suc zero)\n (\u03bb { zero \u2192 \u2261.refl ; (suc n) \u2192 \u2261.refl })\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n where to' : Fin (suc n) \u2192 Maybe (Fin n)\n to' zero = nothing\n to' (suc n) = just n\n\nFin-injective : \u2200 {m n} \u2192 Fin m \u2194 Fin n \u2192 m \u2261 n\nFin-injective = go _ _ where\n go : \u2200 m n \u2192 Fin m \u2194 Fin n \u2192 m \u2261 n\n go zero zero iso = \u2261.refl\n go zero (suc n) iso with from iso zero\n ... | ()\n go (suc m) zero iso with to iso zero\n ... | ()\n go (suc m) (suc n) iso = \u2261.cong suc (go m n (Maybe-injective (Fin\u2218suc\u2194Maybe\u2218Fin \u2218 iso \u2218 sym Fin\u2218suc\u2194Maybe\u2218Fin)))\n\nLift\u2194id : \u2200 {a} {A : \u2605 a} \u2192 Lift {a} {a} A \u2194 A\nLift\u2194id = inverses lower lift (\u03bb { (lift x) \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\n\ud835\udfd9\u00d7A\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd9 \u00d7 A) \u2194 A\n\ud835\udfd9\u00d7A\u2194A = proj\u2081 \u00d7-CMon.identity _ \u2218 sym Lift\u2194id \u00d7-cong id\n\nA\u00d7\ud835\udfd9\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u00d7 \ud835\udfd9) \u2194 A\nA\u00d7\ud835\udfd9\u2194A = proj\u2082 \u00d7-CMon.identity _ \u2218 id \u00d7-cong sym Lift\u2194id\n\n\u03a0\ud835\udfd9F\u2194F : \u2200 {\u2113} {F : \ud835\udfd9 \u2192 \u2605_ \u2113} \u2192 \u03a0 \ud835\udfd9 F \u2194 F _\n\u03a0\ud835\udfd9F\u2194F = inverses (\u03bb x \u2192 x _) const (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\n\ud835\udfd9\u2192A\u2194A : \u2200 {\u2113} {A : \u2605_ \u2113} \u2192 (\ud835\udfd9 \u2192 A) \u2194 A\n\ud835\udfd9\u2192A\u2194A = \u03a0\ud835\udfd9F\u2194F\n\nnot-\ud835\udfda\u2194\ud835\udfda : \ud835\udfda \u2194 \ud835\udfda\nnot-\ud835\udfda\u2194\ud835\udfda = inverses not not not-involutive not-involutive\n\n\u2261-iso : \u2200 {\u2113 \u2113'}{A : \u2605_ \u2113}{B : \u2605_ \u2113'}{x y : A} \u2192 (\u03c0 : A \u2194 B) \u2192 (x \u2261 y) \u2194 (to \u03c0 x \u2261 to \u03c0 y)\n\u2261-iso {x = x}{y} \u03c0 = inverses (\u2261.cong (to \u03c0))\n (\u03bb p \u2192 \u2261.trans (\u2261.sym (Inverse.left-inverse-of \u03c0 x))\n (\u2261.trans (\u2261.cong (from \u03c0) p)\n (Inverse.left-inverse-of \u03c0 y)))\n (\u03bb x \u2192 \u2261.proof-irrelevance _ x) (\u03bb x \u2192 \u2261.proof-irrelevance _ x)\n\nmodule _ {a} {A : \u2605_ a} (ext\ud835\udfd8 : (f g : \ud835\udfd8 \u2192 A) \u2192 f \u2261 g) where\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 : (\ud835\udfd8 \u2192 A) \u2194 \ud835\udfd9\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 = inverses _ (const (\u03bb())) (ext\ud835\udfd8 (\u03bb ())) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {F : \ud835\udfd8 \u2192 \u2605_ \u2113} (ext\ud835\udfd8 : (f g : \u03a0 \ud835\udfd8 F) \u2192 f \u2261 g) where\n \u03a0\ud835\udfd8F\u2194\ud835\udfd9 : \u03a0 \ud835\udfd8 F \u2194 \ud835\udfd9\n \u03a0\ud835\udfd8F\u2194\ud835\udfd9 = inverses _ (const (\u03bb())) (ext\ud835\udfd8 (\u03bb ())) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {F : \ud835\udfda \u2192 \u2605_ \u2113} (ext\ud835\udfda : {f g : \u03a0 \ud835\udfda F} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 : \u03a0 \ud835\udfda F \u2194 (F 0\u2082 \u00d7 F 1\u2082)\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 = inverses (\u03bb f \u2192 f 0\u2082 , f 1\u2082) proj\n (\u03bb f \u2192 ext\ud835\udfda (\u03bb { 0\u2082 \u2192 \u2261.refl ; 1\u2082 \u2192 \u2261.refl }))\n (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {A : \u2605_ \u2113} (ext\ud835\udfda : {f g : \ud835\udfda \u2192 A} \u2192 f \u2257 g \u2192 f \u2261 g) where\n \ud835\udfda\u2192A\u2194A\u00d7A : (\ud835\udfda \u2192 A) \u2194 (A \u00d7 A)\n \ud835\udfda\u2192A\u2194A\u00d7A = \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 ext\ud835\udfda\n\n\ud835\udfd8\u228eA\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd8 \u228e A) \u2194 A\n\ud835\udfd8\u228eA\u2194A = proj\u2081 \u228e-CMon.identity _ \u2218 sym Lift\u2194id \u228e-cong id\n\nA\u228e\ud835\udfd8\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u228e \ud835\udfd8) \u2194 A\nA\u228e\ud835\udfd8\u2194A = proj\u2082 \u228e-CMon.identity _ \u2218 id \u228e-cong sym Lift\u2194id\n\n\ud835\udfd8\u00d7A\u2194\ud835\udfd8 : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd8 \u00d7 A) \u2194 \ud835\udfd8\n\ud835\udfd8\u00d7A\u2194\ud835\udfd8 = Lift\u2194id \u2218 proj\u2081 \u00d7\u228e\u00b0.zero _ \u2218 sym (Lift\u2194id \u00d7-cong id)\n\nMaybe\ud835\udfd8\u2194\ud835\udfd9 : Maybe \ud835\udfd8 \u2194 \ud835\udfd9\nMaybe\ud835\udfd8\u2194\ud835\udfd9 = A\u228e\ud835\udfd8\u2194A \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe^\ud835\udfd8\u2194Fin : \u2200 n \u2192 Maybe^ n \ud835\udfd8 \u2194 Fin n\nMaybe^\ud835\udfd8\u2194Fin zero = sym Fin0\u2194\ud835\udfd8\nMaybe^\ud835\udfd8\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\ud835\udfd8\u2194Fin n)\n\nMaybe^\ud835\udfd9\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \ud835\udfd9 \u2194 Fin (suc n)\nMaybe^\ud835\udfd9\u2194Fin1+ n = Maybe^\ud835\udfd8\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\ud835\udfd8\u2194\ud835\udfd9)\n\nMaybe-\u228e : \u2200 {a} {A B : \u2605 a} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e {a} = sym Maybe\u2194Lift\ud835\udfd9\u228e \u2218 \u228e-CMon.assoc (Lift {_} {a} \ud835\udfd9) _ _ \u2218 (Maybe\u2194Lift\ud835\udfd9\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \ud835\udfd8 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \ud835\udfd8\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n\n\u03a3\ud835\udfda\u2194\u228e : \u2200 {a} (F : \ud835\udfda \u2192 \u2605_ a) \u2192 \u03a3 \ud835\udfda F \u2194 (F 0\u2082 \u228e F 1\u2082)\n\u03a3\ud835\udfda\u2194\u228e F = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d2 : (x : \u03a3 _ _) \u2192 _\n \u21d2 (0\u2082 , p) = inj\u2081 p\n \u21d2 (1\u2082 , p) = inj\u2082 p\n \u21d0 : (x : _ \u228e _) \u2192 _\n \u21d0 (inj\u2081 x) = 0\u2082 , x\n \u21d0 (inj\u2082 y) = 1\u2082 , y\n\n \u21d0\u21d2 : (_ : \u03a3 _ _) \u2192 _\n \u21d0\u21d2 (0\u2082 , p) = \u2261.refl\n \u21d0\u21d2 (1\u2082 , p) = \u2261.refl\n \u21d2\u21d0 : (_ : _ \u228e _) \u2192 _\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n\ud835\udfda\u2194\ud835\udfd9\u228e\ud835\udfd9 : \ud835\udfda \u2194 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2194\ud835\udfd9\u228e\ud835\udfd9 = inverses (proj (inj\u2081 _ , inj\u2082 _)) [ F.const 0\u2082 , F.const 1\u2082 ] \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d0\u21d2 : (_ : \ud835\udfda) \u2192 _\n \u21d0\u21d2 0\u2082 = \u2261.refl\n \u21d0\u21d2 1\u2082 = \u2261.refl\n \u21d2\u21d0 : (_ : \ud835\udfd9 \u228e \ud835\udfd9) \u2192 _\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\nFin-\u228e-+ : \u2200 m n \u2192 (Fin m \u228e Fin n) \u2194 Fin (m + n)\nFin-\u228e-+ m n = Maybe^\ud835\udfd8\u2194Fin (m + n)\n \u2218 Maybe^-\u228e-+ m n\n \u2218 sym (Maybe^\ud835\udfd8\u2194Fin m \u228e-cong Maybe^\ud835\udfd8\u2194Fin n)\n\nFin\u2218suc\u2194\ud835\udfd9\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2194 (\ud835\udfd9 \u228e Fin n)\nFin\u2218suc\u2194\ud835\udfd9\u228eFin = Maybe\u2194\ud835\udfd9\u228e \u2218 Fin\u2218suc\u2194Maybe\u2218Fin\n\nFin-\u00d7-* : \u2200 m n \u2192 (Fin m \u00d7 Fin n) \u2194 Fin (m * n)\nFin-\u00d7-* zero n = (Fin 0 \u00d7 Fin n) \u2194\u27e8 Fin0\u2194\ud835\udfd8 \u00d7-cong id \u27e9\n (\ud835\udfd8 \u00d7 Fin n) \u2194\u27e8 \ud835\udfd8\u00d7A\u2194\ud835\udfd8 \u27e9\n \ud835\udfd8 \u2194\u27e8 sym Fin0\u2194\ud835\udfd8 \u27e9\n Fin 0 \u220e\n where open EquationalReasoning hiding (sym)\nFin-\u00d7-* (suc m) n = (Fin (suc m) \u00d7 Fin n) \u2194\u27e8 Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u00d7-cong id \u27e9\n ((\ud835\udfd9 \u228e Fin m) \u00d7 Fin n) \u2194\u27e8 \u00d7\u228e\u00b0.distrib\u02b3 (Fin n) \ud835\udfd9 (Fin m) \u27e9\n ((\ud835\udfd9 \u00d7 Fin n) \u228e (Fin m \u00d7 Fin n)) \u2194\u27e8 \ud835\udfd9\u00d7A\u2194A \u228e-cong Fin-\u00d7-* m n \u27e9\n (Fin n \u228e Fin (m * n)) \u2194\u27e8 Fin-\u228e-+ n (m * n) \u27e9\n Fin (suc m * n) \u220e\n where open EquationalReasoning hiding (sym)\n\nFin\u228e-injective : \u2200 {A B : Set} n \u2192 (Fin n \u228e A) \u2194 (Fin n \u228e B) \u2192 A \u2194 B\nFin\u228e-injective zero f = \ud835\udfd8\u228eA\u2194A \u2218 Fin0\u2194\ud835\udfd8 \u228e-cong id \u2218 f \u2218 sym Fin0\u2194\ud835\udfd8 \u228e-cong id \u2218 sym \ud835\udfd8\u228eA\u2194A\nFin\u228e-injective (suc n) f =\n Fin\u228e-injective n\n (Maybe-injective\n (sym Maybe\u2194\ud835\udfd9\u228e \u2218\n \u228e-CMon.assoc _ _ _ \u2218\n Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u228e-cong id \u2218\n f \u2218\n sym Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u228e-cong id \u2218\n sym (\u228e-CMon.assoc _ _ _) \u2218\n Maybe\u2194\ud835\udfd9\u228e))\n\n{-\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"4e1289551ff82bc8329c3c1caf7e5cb6ef47ade7","subject":"Only indentation.","message":"Only indentation.\n\nIgnore-this: f64e1fdc84de57eb5a1dda281b14b019\n\ndarcs-hash:20110209033859-3bd4e-244c58fa7b5bd53e5342b1b247aff5012816d9bd.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/McCarthy91\/Numbers.agda","new_file":"Draft\/McCarthy91\/Numbers.agda","new_contents":"------------------------------------------------------------------------------\n-- Some naturales numbers\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.Numbers where\n\nopen import LTC.Base\n\n------------------------------------------------------------------------------\n\none = succ zero\ntwo = succ one\nthree = succ two\nfour = succ three\nfive = succ four\nsix = succ five\nseven = succ six\neight = succ seven\nnine = succ eight\nten = succ nine\n\n{-# ATP definition one #-}\n{-# ATP definition two #-}\n{-# ATP definition three #-}\n{-# ATP definition four #-}\n{-# ATP definition five #-}\n{-# ATP definition six #-}\n{-# ATP definition seven #-}\n{-# ATP definition eight #-}\n{-# ATP definition nine #-}\n{-# ATP definition ten #-}\n\neleven = succ ten\ntwelve = succ eleven\nthirteen = succ twelve\nfourteen = succ thirteen\nfifteen = succ fourteen\nsixteen = succ fifteen\nseventeen = succ sixteen\neighteen = succ seventeen\nnineteen = succ eighteen\ntwenty = succ nineteen\n\n{-# ATP definition eleven #-}\n{-# ATP definition twelve #-}\n{-# ATP definition thirteen #-}\n{-# ATP definition fourteen #-}\n{-# ATP definition fifteen #-}\n{-# ATP definition sixteen #-}\n{-# ATP definition seventeen #-}\n{-# ATP definition eighteen #-}\n{-# ATP definition nineteen #-}\n{-# ATP definition twenty #-}\n\ntwenty-one = succ twenty\ntwenty-two = succ twenty-one\ntwenty-three = succ twenty-two\ntwenty-four = succ twenty-three\ntwenty-five = succ twenty-four\ntwenty-six = succ twenty-five\ntwenty-seven = succ twenty-six\ntwenty-eight = succ twenty-seven\ntwenty-nine = succ twenty-eight\nthirty = succ twenty-nine\n\n{-# ATP definition twenty-one #-}\n{-# ATP definition twenty-two #-}\n{-# ATP definition twenty-three #-}\n{-# ATP definition twenty-four #-}\n{-# ATP definition twenty-five #-}\n{-# ATP definition twenty-six #-}\n{-# ATP definition twenty-seven #-}\n{-# ATP definition twenty-eight #-}\n{-# ATP definition twenty-nine #-}\n{-# ATP definition thirty #-}\n\nthirty-one = succ thirty\nthirty-two = succ thirty-one\nthirty-three = succ thirty-two\nthirty-four = succ thirty-three\nthirty-five = succ thirty-four\nthirty-six = succ thirty-five\nthirty-seven = succ thirty-six\nthirty-eight = succ thirty-seven\nthirty-nine = succ thirty-eight\nforty = succ thirty-nine\n\n{-# ATP definition thirty-one #-}\n{-# ATP definition thirty-two #-}\n{-# ATP definition thirty-three #-}\n{-# ATP definition thirty-four #-}\n{-# ATP definition thirty-five #-}\n{-# ATP definition thirty-six #-}\n{-# ATP definition thirty-seven #-}\n{-# ATP definition thirty-eight #-}\n{-# ATP definition thirty-nine #-}\n{-# ATP definition forty #-}\n\nforty-one = succ forty\nforty-two = succ forty-one\nforty-three = succ forty-two\nforty-four = succ forty-three\nforty-five = succ forty-four\nforty-six = succ forty-five\nforty-seven = succ forty-six\nforty-eight = succ forty-seven\nforty-nine = succ forty-eight\nfifty = succ forty-nine\n\n{-# ATP definition forty-one #-}\n{-# ATP definition forty-two #-}\n{-# ATP definition forty-three #-}\n{-# ATP definition forty-four #-}\n{-# ATP definition forty-five #-}\n{-# ATP definition forty-six #-}\n{-# ATP definition forty-seven #-}\n{-# ATP definition forty-eight #-}\n{-# ATP definition forty-nine #-}\n{-# ATP definition fifty #-}\n\nfifty-one = succ fifty\nfifty-two = succ fifty-one\nfifty-three = succ fifty-two\nfifty-four = succ fifty-three\nfifty-five = succ fifty-four\nfifty-six = succ fifty-five\nfifty-seven = succ fifty-six\nfifty-eight = succ fifty-seven\nfifty-nine = succ fifty-eight\nsixty = succ fifty-nine\n\n{-# ATP definition fifty-one #-}\n{-# ATP definition fifty-two #-}\n{-# ATP definition fifty-three #-}\n{-# ATP definition fifty-four #-}\n{-# ATP definition fifty-five #-}\n{-# ATP definition fifty-six #-}\n{-# ATP definition fifty-seven #-}\n{-# ATP definition fifty-eight #-}\n{-# ATP definition fifty-nine #-}\n{-# ATP definition sixty #-}\n\nsixty-one = succ sixty\nsixty-two = succ sixty-one\nsixty-three = succ sixty-two\nsixty-four = succ sixty-three\nsixty-five = succ sixty-four\nsixty-six = succ sixty-five\nsixty-seven = succ sixty-six\nsixty-eight = succ sixty-seven\nsixty-nine = succ sixty-eight\nseventy = succ sixty-nine\n\n{-# ATP definition sixty-one #-}\n{-# ATP definition sixty-two #-}\n{-# ATP definition sixty-three #-}\n{-# ATP definition sixty-four #-}\n{-# ATP definition sixty-five #-}\n{-# ATP definition sixty-six #-}\n{-# ATP definition sixty-seven #-}\n{-# ATP definition sixty-eight #-}\n{-# ATP definition sixty-nine #-}\n{-# ATP definition seventy #-}\n\nseventy-one = succ seventy\nseventy-two = succ seventy-one\nseventy-three = succ seventy-two\nseventy-four = succ seventy-three\nseventy-five = succ seventy-four\nseventy-six = succ seventy-five\nseventy-seven = succ seventy-six\nseventy-eight = succ seventy-seven\nseventy-nine = succ seventy-eight\neighty = succ seventy-nine\n\n{-# ATP definition seventy-one #-}\n{-# ATP definition seventy-two #-}\n{-# ATP definition seventy-three #-}\n{-# ATP definition seventy-four #-}\n{-# ATP definition seventy-five #-}\n{-# ATP definition seventy-six #-}\n{-# ATP definition seventy-seven #-}\n{-# ATP definition seventy-eight #-}\n{-# ATP definition seventy-nine #-}\n{-# ATP definition eighty #-}\n\neighty-one = succ eighty\neighty-two = succ eighty-one\neighty-three = succ eighty-two\neighty-four = succ eighty-three\neighty-five = succ eighty-four\neighty-six = succ eighty-five\neighty-seven = succ eighty-six\neighty-eight = succ eighty-seven\neighty-nine = succ eighty-eight\nninety = succ eighty-nine\n\n{-# ATP definition eighty-one #-}\n{-# ATP definition eighty-two #-}\n{-# ATP definition eighty-three #-}\n{-# ATP definition eighty-four #-}\n{-# ATP definition eighty-five #-}\n{-# ATP definition eighty-six #-}\n{-# ATP definition eighty-seven #-}\n{-# ATP definition eighty-eight #-}\n{-# ATP definition eighty-nine #-}\n{-# ATP definition ninety #-}\n\nninety-one = succ ninety\nninety-two = succ ninety-one\nninety-three = succ ninety-two\nninety-four = succ ninety-three\nninety-five = succ ninety-four\nninety-six = succ ninety-five\nninety-seven = succ ninety-six\nninety-eight = succ ninety-seven\nninety-nine = succ ninety-eight\none-hundred = succ ninety-nine\n\n{-# ATP definition ninety-one #-}\n{-# ATP definition ninety-two #-}\n{-# ATP definition ninety-three #-}\n{-# ATP definition ninety-four #-}\n{-# ATP definition ninety-five #-}\n{-# ATP definition ninety-six #-}\n{-# ATP definition ninety-seven #-}\n{-# ATP definition ninety-eight #-}\n{-# ATP definition ninety-nine #-}\n{-# ATP definition one-hundred #-}\n\nhundred-one = succ one-hundred\nhundred-two = succ hundred-one\nhundred-three = succ hundred-two\nhundred-four = succ hundred-three\nhundred-five = succ hundred-four\nhundred-six = succ hundred-five\nhundred-seven = succ hundred-six\nhundred-eight = succ hundred-seven\nhundred-nine = succ hundred-eight\nhundred-ten = succ hundred-nine\n\n{-# ATP definition hundred-one #-}\n{-# ATP definition hundred-two #-}\n{-# ATP definition hundred-three #-}\n{-# ATP definition hundred-four #-}\n{-# ATP definition hundred-five #-}\n{-# ATP definition hundred-six #-}\n{-# ATP definition hundred-seven #-}\n{-# ATP definition hundred-eight #-}\n{-# ATP definition hundred-nine #-}\n{-# ATP definition hundred-ten #-}\n\nhundred-eleven = succ hundred-ten\n{-# ATP definition hundred-eleven #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Some naturales numbers\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.Numbers where\n\nopen import LTC.Base\n\n------------------------------------------------------------------------------\n\none = succ zero\ntwo = succ one\nthree = succ two\nfour = succ three\nfive = succ four\nsix = succ five\nseven = succ six\neight = succ seven\nnine = succ eight\nten = succ nine\n\n{-# ATP definition one #-}\n{-# ATP definition two #-}\n{-# ATP definition three #-}\n{-# ATP definition four #-}\n{-# ATP definition five #-}\n{-# ATP definition six #-}\n{-# ATP definition seven #-}\n{-# ATP definition eight #-}\n{-# ATP definition nine #-}\n{-# ATP definition ten #-}\n\neleven = succ ten\ntwelve = succ eleven\nthirteen = succ twelve\nfourteen = succ thirteen\nfifteen = succ fourteen\nsixteen = succ fifteen\nseventeen = succ sixteen\neighteen = succ seventeen\nnineteen = succ eighteen\ntwenty = succ nineteen\n\n{-# ATP definition eleven #-}\n{-# ATP definition twelve #-}\n{-# ATP definition thirteen #-}\n{-# ATP definition fourteen #-}\n{-# ATP definition fifteen #-}\n{-# ATP definition sixteen #-}\n{-# ATP definition seventeen #-}\n{-# ATP definition eighteen #-}\n{-# ATP definition nineteen #-}\n{-# ATP definition twenty #-}\n\ntwenty-one = succ twenty\ntwenty-two = succ twenty-one\ntwenty-three = succ twenty-two\ntwenty-four = succ twenty-three\ntwenty-five = succ twenty-four\ntwenty-six = succ twenty-five\ntwenty-seven = succ twenty-six\ntwenty-eight = succ twenty-seven\ntwenty-nine = succ twenty-eight\nthirty = succ twenty-nine\n\n{-# ATP definition twenty-one #-}\n{-# ATP definition twenty-two #-}\n{-# ATP definition twenty-three #-}\n{-# ATP definition twenty-four #-}\n{-# ATP definition twenty-five #-}\n{-# ATP definition twenty-six #-}\n{-# ATP definition twenty-seven #-}\n{-# ATP definition twenty-eight #-}\n{-# ATP definition twenty-nine #-}\n{-# ATP definition thirty #-}\n\nthirty-one = succ thirty\nthirty-two = succ thirty-one\nthirty-three = succ thirty-two\nthirty-four = succ thirty-three\nthirty-five = succ thirty-four\nthirty-six = succ thirty-five\nthirty-seven = succ thirty-six\nthirty-eight = succ thirty-seven\nthirty-nine = succ thirty-eight\nforty = succ thirty-nine\n\n{-# ATP definition thirty-one #-}\n{-# ATP definition thirty-two #-}\n{-# ATP definition thirty-three #-}\n{-# ATP definition thirty-four #-}\n{-# ATP definition thirty-five #-}\n{-# ATP definition thirty-six #-}\n{-# ATP definition thirty-seven #-}\n{-# ATP definition thirty-eight #-}\n{-# ATP definition thirty-nine #-}\n{-# ATP definition forty #-}\n\nforty-one = succ forty\nforty-two = succ forty-one\nforty-three = succ forty-two\nforty-four = succ forty-three\nforty-five = succ forty-four\nforty-six = succ forty-five\nforty-seven = succ forty-six\nforty-eight = succ forty-seven\nforty-nine = succ forty-eight\nfifty = succ forty-nine\n\n{-# ATP definition forty-one #-}\n{-# ATP definition forty-two #-}\n{-# ATP definition forty-three #-}\n{-# ATP definition forty-four #-}\n{-# ATP definition forty-five #-}\n{-# ATP definition forty-six #-}\n{-# ATP definition forty-seven #-}\n{-# ATP definition forty-eight #-}\n{-# ATP definition forty-nine #-}\n{-# ATP definition fifty #-}\n\nfifty-one = succ fifty\nfifty-two = succ fifty-one\nfifty-three = succ fifty-two\nfifty-four = succ fifty-three\nfifty-five = succ fifty-four\nfifty-six = succ fifty-five\nfifty-seven = succ fifty-six\nfifty-eight = succ fifty-seven\nfifty-nine = succ fifty-eight\nsixty = succ fifty-nine\n\n{-# ATP definition fifty-one #-}\n{-# ATP definition fifty-two #-}\n{-# ATP definition fifty-three #-}\n{-# ATP definition fifty-four #-}\n{-# ATP definition fifty-five #-}\n{-# ATP definition fifty-six #-}\n{-# ATP definition fifty-seven #-}\n{-# ATP definition fifty-eight #-}\n{-# ATP definition fifty-nine #-}\n{-# ATP definition sixty #-}\n\nsixty-one = succ sixty\nsixty-two = succ sixty-one\nsixty-three = succ sixty-two\nsixty-four = succ sixty-three\nsixty-five = succ sixty-four\nsixty-six = succ sixty-five\nsixty-seven = succ sixty-six\nsixty-eight = succ sixty-seven\nsixty-nine = succ sixty-eight\nseventy = succ sixty-nine\n\n{-# ATP definition sixty-one #-}\n{-# ATP definition sixty-two #-}\n{-# ATP definition sixty-three #-}\n{-# ATP definition sixty-four #-}\n{-# ATP definition sixty-five #-}\n{-# ATP definition sixty-six #-}\n{-# ATP definition sixty-seven #-}\n{-# ATP definition sixty-eight #-}\n{-# ATP definition sixty-nine #-}\n{-# ATP definition seventy #-}\n\nseventy-one = succ seventy\nseventy-two = succ seventy-one\nseventy-three = succ seventy-two\nseventy-four = succ seventy-three\nseventy-five = succ seventy-four\nseventy-six = succ seventy-five\nseventy-seven = succ seventy-six\nseventy-eight = succ seventy-seven\nseventy-nine = succ seventy-eight\neighty = succ seventy-nine\n\n{-# ATP definition seventy-one #-}\n{-# ATP definition seventy-two #-}\n{-# ATP definition seventy-three #-}\n{-# ATP definition seventy-four #-}\n{-# ATP definition seventy-five #-}\n{-# ATP definition seventy-six #-}\n{-# ATP definition seventy-seven #-}\n{-# ATP definition seventy-eight #-}\n{-# ATP definition seventy-nine #-}\n{-# ATP definition eighty #-}\n\neighty-one = succ eighty\neighty-two = succ eighty-one\neighty-three = succ eighty-two\neighty-four = succ eighty-three\neighty-five = succ eighty-four\neighty-six = succ eighty-five\neighty-seven = succ eighty-six\neighty-eight = succ eighty-seven\neighty-nine = succ eighty-eight\nninety = succ eighty-nine\n\n{-# ATP definition eighty-one #-}\n{-# ATP definition eighty-two #-}\n{-# ATP definition eighty-three #-}\n{-# ATP definition eighty-four #-}\n{-# ATP definition eighty-five #-}\n{-# ATP definition eighty-six #-}\n{-# ATP definition eighty-seven #-}\n{-# ATP definition eighty-eight #-}\n{-# ATP definition eighty-nine #-}\n{-# ATP definition ninety #-}\n\nninety-one = succ ninety\nninety-two = succ ninety-one\nninety-three = succ ninety-two\nninety-four = succ ninety-three\nninety-five = succ ninety-four\nninety-six = succ ninety-five\nninety-seven = succ ninety-six\nninety-eight = succ ninety-seven\nninety-nine = succ ninety-eight\none-hundred = succ ninety-nine\n\n{-# ATP definition ninety-one #-}\n{-# ATP definition ninety-two #-}\n{-# ATP definition ninety-three #-}\n{-# ATP definition ninety-four #-}\n{-# ATP definition ninety-five #-}\n{-# ATP definition ninety-six #-}\n{-# ATP definition ninety-seven #-}\n{-# ATP definition ninety-eight #-}\n{-# ATP definition ninety-nine #-}\n{-# ATP definition one-hundred #-}\n\nhundred-one = succ one-hundred\nhundred-two = succ hundred-one\nhundred-three = succ hundred-two\nhundred-four = succ hundred-three\nhundred-five = succ hundred-four\nhundred-six = succ hundred-five\nhundred-seven = succ hundred-six\nhundred-eight = succ hundred-seven\nhundred-nine = succ hundred-eight\nhundred-ten = succ hundred-nine\n\n{-# ATP definition hundred-one #-}\n{-# ATP definition hundred-two #-}\n{-# ATP definition hundred-three #-}\n{-# ATP definition hundred-four #-}\n{-# ATP definition hundred-five #-}\n{-# ATP definition hundred-six #-}\n{-# ATP definition hundred-seven #-}\n{-# ATP definition hundred-eight #-}\n{-# ATP definition hundred-nine #-}\n{-# ATP definition hundred-ten #-}\n\nhundred-eleven = succ hundred-ten\n{-# ATP definition hundred-eleven #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"929d2eace2cfc7a66c7c65a54ee19685ca37f159","subject":"Updated note on the existential quantifier.","message":"Updated note on the existential quantifier.\n\nIgnore-this: 42acf883a18689f1628a5fa5c479f987\n\ndarcs-hash:20120423162449-3bd4e-35b3d060e1280821b05f7c7b5e3c08ab39f89d37.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/thesis\/logical-framework\/Existential.agda","new_file":"notes\/thesis\/logical-framework\/Existential.agda","new_contents":"-- Tested with FOT on 23 April 2012.\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Existential where\n\nmodule LF where\n postulate\n D : Set\n \u2203 : (A : D \u2192 Set) \u2192 Set\n _,_ : {A : D \u2192 Set}(x : D) \u2192 A x \u2192 \u2203 A\n \u2203-proj\u2081 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n \u2203-proj\u2082 : {A : D \u2192 Set}(a : \u2203 A) \u2192 A (\u2203-proj\u2081 a)\n \u2203-elim : {A : D \u2192 Set}{B : Set} \u2192 \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B) \u2192 B\n\n syntax \u2203 (\u03bb x \u2192 e) = \u2203[ x ] e\n\n module FOL-Example where\n postulate A : D \u2192 D \u2192 Set\n\n -- Using the projections.\n \u2203\u2200\u2081 : \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2081 h y = \u2203-proj\u2081 h , (\u2203-proj\u2082 h) y\n\n -- Using the elimination\n \u2203\u2200\u2082 : \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2082 h y = \u2203-elim h (\u03bb {x} h\u2081 \u2192 x , h\u2081 y)\n\n module NonFOL-Example where\n\n -- Using the projections.\n non-FOL\u2081 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2081 h = \u2203-proj\u2081 h\n\n -- Using the elimination.\n non-FOL\u2082 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2082 h = \u2203-elim h (\u03bb {x} _ \u2192 x)\n\nmodule Inductive where\n\n open import Common.FOL.FOL\n\n -- The existential proyections.\n \u2203-proj\u2081 : \u2200 {A} \u2192 \u2203 A \u2192 D\n \u2203-proj\u2081 (x , _) = x\n\n \u2203-proj\u2082 : \u2200 {A} \u2192 (h : \u2203 A) \u2192 A (\u2203-proj\u2081 h)\n \u2203-proj\u2082 (_ , Ax) = Ax\n\n -- The existential elimination.\n \u2203-elim : {A : D \u2192 Set}{B : Set} \u2192 \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B) \u2192 B\n \u2203-elim (_ , Ax) h = h Ax\n\n module FOL-Example where\n postulate A : D \u2192 D \u2192 Set\n\n -- Using the projections.\n \u2203\u2200\u2081 : \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2081 h y = \u2203-proj\u2081 h , (\u2203-proj\u2082 h) y\n\n -- Using the elimination.\n \u2203\u2200\u2082 : \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2082 h y = \u2203-elim h (\u03bb {x} h\u2081 \u2192 x , h\u2081 y)\n\n -- Using pattern matching.\n \u2203\u2200\u2083 : \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2083 (x , Ax) y = x , Ax y\n\n module NonFOL-Example where\n\n -- Using the projections.\n non-FOL\u2081 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2081 h = \u2203-proj\u2081 h\n\n -- Using the elimination.\n non-FOL\u2082 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2082 h = \u2203-elim h (\u03bb {x} _ \u2192 x)\n\n -- Using the pattern matching.\n non-FOL\u2083 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2083 (x , _) = x\n","old_contents":"-- Tested with FOT on 16 February 2012.\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Existential where\n\nmodule LF where\n postulate\n D : Set\n \u2203 : (A : D \u2192 Set) \u2192 Set\n _,_ : {A : D \u2192 Set}(x : D) \u2192 A x \u2192 \u2203 A\n \u2203-proj\u2081 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n \u2203-proj\u2082 : {A : D \u2192 Set}(a : \u2203 A) \u2192 A (\u2203-proj\u2081 p)\n\n syntax \u2203 (\u03bb x \u2192 e) = \u2203[ x ] e\n\n postulate A : D \u2192 D \u2192 Set\n\n \u2203\u2200 : \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200 h y = \u2203-proj\u2081 h , (\u2203-proj\u2082 h) y\n\nmodule Inductive where\n\n open import Common.Universe\n open import Common.Data.Product\n\n postulate A : D \u2192 D \u2192 Set\n\n \u2203\u2200-el : \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200-el h y = \u2203-proj\u2081 h , (\u2203-proj\u2082 h) y\n\n \u2203\u2200 : \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200 (x , Ax) y = x , Ax y\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"93b21c38564d9ad524fbd8dd5f62c5be86166146","subject":"ThreeBallot.agda","message":"ThreeBallot.agda\n","repos":"crypto-agda\/crypto-agda","old_file":"ThreeBallot.agda","new_file":"ThreeBallot.agda","new_contents":"module ThreeBallot\n -- ()\n where\n\nopen import Data.Fin using (Fin ; zero ; suc )\nopen import Data.Nat\nopen import Data.Bool.NP using (to\u2115) renaming (false to 0b ; true to 1b)\n\nopen import Data.List using (List ; [] ; _\u2237_ ; length)\nopen import Data.Bits hiding (to\u2115 ; 0b ; 1b)\n\nopen import Data.Vec.NP renaming (map to vmap)\nopen import Data.Product\n\nimport Function.Inverse.NP as Inv\nopen Inv using (_\u2194_; _\u2218_; sym; id; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\n\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_ ; refl)\n\ndata Cand : Set where\n Nice'Guy Bad'Guy : Cand\n\nVote = Bits 2\nTag = \u2115\n\nrecord Ballot : Set where\n constructor mk\n field\n vote : Vote\n tag : Tag\n\nopen Ballot\n\nrecord 3'_ (A : Set) : Set where\n constructor [_][_][_]\n field\n 1t 2t 3t : A\n\n3Vote = 3' Vote\n3Ballot = 3' Ballot\n\n[_][_] : Bit \u2192 Bit \u2192 Bits 2\n[ x ][ y ] = x \u2237 y \u2237 []\n\nsvote : Vote\nsvote = [ 0b ][ 1b ]\n\nexample : 3Vote\nexample = [ [ 1b ][ 0b ] ][ [ 0b ][ 1b ] ][ [ 0b ][ 1b ] ]\n\nrow-const : Bit \u2192 3' Bit \u2192 Set\nrow-const b [ 1t ][ 2t ][ 3t ] = 1 + to\u2115 b \u2261 to\u2115 1t + to\u2115 2t + to\u2115 3t\n\nvoteFor : Vote \u2192 3Vote \u2192 Set\nvoteFor (n \u2237 b \u2237 []) [ x \u2237 x\u2081 \u2237 [] ][ x\u2082 \u2237 x\u2083 \u2237 [] ][ x\u2084 \u2237 x\u2085 \u2237 [] ] = row-const n [ x ][ x\u2082 ][ x\u2084 ]\n \u00d7 row-const b [ x\u2081 ][ x\u2083 ][ x\u2085 ]\n\nvote-example : voteFor svote example\nvote-example = refl , refl\n\ncast : (_ _ : Vote) \u2192 Vote\ncast i r = vnot r\n\n\nvote-any : (intent receipt : Vote) \u2192 voteFor intent [ intent ][ receipt ][ cast intent receipt ]\nvote-any (true \u2237 true \u2237 []) (true \u2237 true \u2237 []) = refl , refl\nvote-any (true \u2237 true \u2237 []) (true \u2237 false \u2237 []) = refl , refl\nvote-any (true \u2237 true \u2237 []) (false \u2237 true \u2237 []) = refl , refl\nvote-any (true \u2237 true \u2237 []) (false \u2237 false \u2237 []) = refl , refl\nvote-any (true \u2237 false \u2237 []) (true \u2237 true \u2237 []) = refl , refl\nvote-any (true \u2237 false \u2237 []) (true \u2237 false \u2237 []) = refl , refl\nvote-any (true \u2237 false \u2237 []) (false \u2237 true \u2237 []) = refl , refl\nvote-any (true \u2237 false \u2237 []) (false \u2237 false \u2237 []) = refl , refl\nvote-any (false \u2237 true \u2237 []) (true \u2237 true \u2237 []) = refl , refl\nvote-any (false \u2237 true \u2237 []) (true \u2237 false \u2237 []) = refl , refl\nvote-any (false \u2237 true \u2237 []) (false \u2237 true \u2237 []) = refl , refl\nvote-any (false \u2237 true \u2237 []) (false \u2237 false \u2237 []) = refl , refl\nvote-any (false \u2237 false \u2237 []) (true \u2237 true \u2237 []) = refl , refl\nvote-any (false \u2237 false \u2237 []) (true \u2237 false \u2237 []) = refl , refl\nvote-any (false \u2237 false \u2237 []) (false \u2237 true \u2237 []) = refl , refl\nvote-any (false \u2237 false \u2237 []) (false \u2237 false \u2237 []) = refl , refl\n\n{-\n\nNP :\n\n (R?\u00d7V)^n \u2192 (3V)^n \u2192 (3B)^n \u2192 B^3n -\u27e8 shuffle \u27e9\u2192 b^3n \u2192 T\n\n-}\n\nPerm : \u2115 \u2192 Set\nPerm n = Fin n \u2194 Fin n\n\nshuffle : \u2200 {n}{A : Set} \u2192 Perm n \u2192 Vec A n \u2192 Vec A n\nshuffle p xs = tabulate (\u03bb i \u2192 lookup (to p i) xs)\n\nmodule _\n (tally : \u2200 {n} \u2192 Vec Vote n \u2192 \u2115)\n (tally-stable : \u2200 {n} vs \u03c0 \u2192 tally {n} vs \u2261 tally (shuffle \u03c0 vs))\n where\n\n 3-tally : \u2200 {n} \u2192 Vec Ballot (3 * n) \u2192 \u2115\n 3-tally {n} bs = tally (vmap vote bs) \u2238 n\n\n\n lookup-map : \u2200 {n}{A B : Set}(f : A \u2192 B)(i : Fin n)(xs : Vec A n)\n \u2192 f (lookup i xs) \u2261 lookup i (vmap f xs)\n lookup-map f zero (x \u2237 xs) = refl\n lookup-map f (suc i) (x \u2237 xs) = lookup-map f i xs\n\n -- map g (tabulate f) = tabulate (g \u2218 f)\n -- f (lookup i xs) \u2261 lookup i (map f xs)\n 3-tally-stable : \u2200 {n} bs \u03c0 \u2192 3-tally {n} bs \u2261 3-tally {n} (shuffle \u03c0 bs)\n 3-tally-stable {n} bs p rewrite tally-stable (vmap vote bs) p\n | \u2261.sym (tabulate-ext (\u03bb i \u2192 lookup-map vote (to p i) bs))\n = \u2261.cong (\u03bb p\u2081 \u2192 tally p\u2081 \u2238 n) (tabulate-\u2218 _ _)\n\n{-\n Ballot privacy \u263a\n-}\n\nmodule BallotPrivacy\n (RA : Set)\n where\n\n data Msg : Set where\n vote! : (intent receipt : Vote) \u2192 Msg\n stop! : Msg\n\n Resp = List Ballot\n\n Adv\u2080 = RA \u2192 Vote\n Adv\u2081 = RA \u2192 Resp \u2192 Msg\n Adv\u2082 = RA \u2192 (l : Resp) \u2192 Vec Ballot (3 * length l) \u2192 Bit\n\n Adv = Adv\u2080 \u00d7 Adv\u2081 \u00d7 Adv\u2082\n\n\n G : Adv \u2192 (\u2200 {n} \u2192 Perm n) \u2192 RA \u2192 Bit \u2192 Bit\n G (A\u2080 , A\u2081 , A\u2082) \u03c0 Ra b = {!!} where\n \u03b5 = A\u2080 Ra\n\n go : (r : Resp) \u2192 Vec Ballot (3 * length r) \u2192 Bit\n go r bb with A\u2081 Ra r | b\n go r bb | vote! intent receipt | 1b = {!!}\n go r bb | vote! intent receipt | 0b = {!!}\n go r bb | stop! | _ = A\u2082 Ra r (shuffle \u03c0 bb)\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n3<4 : 3 < 4\n3<4 = s\u2264s (s\u2264s (s\u2264s (s\u2264s z\u2264n)))\n\n\n","old_contents":"module ThreeBallot\n -- ()\n where\n\nopen import Data.Fin using (Fin ; zero ; suc )\nopen import Data.Nat\nopen import Data.Bool.NP using (to\u2115) renaming (false to 0b ; true to 1b)\n\nopen import Data.List using (List ; [] ; _\u2237_ ; length)\nopen import Data.Bits hiding (to\u2115 ; 0b ; 1b)\n\nopen import Data.Vec.NP renaming (map to vmap)\nopen import Data.Product\n\nimport Function.Inverse.NP as Inv\nopen Inv using (_\u2194_; _\u2218_; sym; id; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\n\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_ ; refl)\n\ndata Cand : Set where\n Nice'Guy Bad'Guy : Cand\n\nVote = Bits 2\nTag = \u2115 \n\nrecord Ballot : Set where\n constructor mk\n field\n vote : Vote\n tag : Tag\n\nopen Ballot\n \nrecord 3'_ (A : Set) : Set where\n constructor [_][_][_]\n field\n 1t 2t 3t : A\n\n3Vote = 3' Vote\n3Ballot = 3' Ballot\n \n[_][_] : Bit \u2192 Bit \u2192 Bits 2\n[ x ][ y ] = x \u2237 y \u2237 []\n\nsvote : Vote\nsvote = [ 0b ][ 1b ]\n\nexample : 3Vote\nexample = [ [ 1b ][ 0b ] ][ [ 0b ][ 1b ] ][ [ 0b ][ 1b ] ] \n\nrow-const : Bit \u2192 3' Bit \u2192 Set\nrow-const b [ 1t ][ 2t ][ 3t ] = 1 + to\u2115 b \u2261 to\u2115 1t + to\u2115 2t + to\u2115 3t\n\nvoteFor : Vote \u2192 3Vote \u2192 Set\nvoteFor (n \u2237 b \u2237 []) [ x \u2237 x\u2081 \u2237 [] ][ x\u2082 \u2237 x\u2083 \u2237 [] ][ x\u2084 \u2237 x\u2085 \u2237 [] ] = row-const n [ x ][ x\u2082 ][ x\u2084 ]\n \u00d7 row-const b [ x\u2081 ][ x\u2083 ][ x\u2085 ]\n\nvote-example : voteFor svote example\nvote-example = refl , refl\n\ncast : (_ _ : Vote) \u2192 Vote\ncast i r = vnot r\n\n\nvote-any : (intent receipt : Vote) \u2192 voteFor intent [ intent ][ receipt ][ cast intent receipt ]\nvote-any (true \u2237 true \u2237 []) (true \u2237 true \u2237 []) = refl , refl \nvote-any (true \u2237 true \u2237 []) (true \u2237 false \u2237 []) = refl , refl\nvote-any (true \u2237 true \u2237 []) (false \u2237 true \u2237 []) = refl , refl\nvote-any (true \u2237 true \u2237 []) (false \u2237 false \u2237 []) = refl , refl \nvote-any (true \u2237 false \u2237 []) (true \u2237 true \u2237 []) = refl , refl \nvote-any (true \u2237 false \u2237 []) (true \u2237 false \u2237 []) = refl , refl \nvote-any (true \u2237 false \u2237 []) (false \u2237 true \u2237 []) = refl , refl \nvote-any (true \u2237 false \u2237 []) (false \u2237 false \u2237 []) = refl , refl \nvote-any (false \u2237 true \u2237 []) (true \u2237 true \u2237 []) = refl , refl \nvote-any (false \u2237 true \u2237 []) (true \u2237 false \u2237 []) = refl , refl \nvote-any (false \u2237 true \u2237 []) (false \u2237 true \u2237 []) = refl , refl \nvote-any (false \u2237 true \u2237 []) (false \u2237 false \u2237 []) = refl , refl \nvote-any (false \u2237 false \u2237 []) (true \u2237 true \u2237 []) = refl , refl \nvote-any (false \u2237 false \u2237 []) (true \u2237 false \u2237 []) = refl , refl \nvote-any (false \u2237 false \u2237 []) (false \u2237 true \u2237 []) = refl , refl \nvote-any (false \u2237 false \u2237 []) (false \u2237 false \u2237 []) = refl , refl \n\n{-\n\nNP :\n\n (R?\u00d7V)^n \u2192 (3V)^n \u2192 (3B)^n \u2192 B^3n -\u27e8 shuffle \u27e9\u2192 b^3n \u2192 T\n\n-}\n\nPerm : \u2115 \u2192 Set\nPerm n = Fin n \u2194 Fin n\n\nshuffle : \u2200 {n}{A : Set} \u2192 Perm n \u2192 Vec A n \u2192 Vec A n\nshuffle p xs = tabulate (\u03bb i \u2192 lookup (to p i) xs)\n\nmodule _\n (tally : \u2200 {n} \u2192 Vec Vote n \u2192 \u2115)\n (tally-stable : \u2200 {n} vs \u03c0 \u2192 tally {n} vs \u2261 tally (shuffle \u03c0 vs)) \n where\n\n 3-tally : \u2200 {n} \u2192 Vec Ballot (3 * n) \u2192 \u2115\n 3-tally {n} bs = tally (vmap vote bs) \u2238 n\n\n\n lookup-map : \u2200 {n}{A B : Set}(f : A \u2192 B)(i : Fin n)(xs : Vec A n)\n \u2192 f (lookup i xs) \u2261 lookup i (vmap f xs)\n lookup-map f zero (x \u2237 xs) = refl \n lookup-map f (suc i) (x \u2237 xs) = lookup-map f i xs\n \n -- map g (tabulate f) = tabulate (g \u2218 f)\n -- f (lookup i xs) \u2261 lookup i (map f xs)\n 3-tally-stable : \u2200 {n} bs \u03c0 \u2192 3-tally {n} bs \u2261 3-tally {n} (shuffle \u03c0 bs)\n 3-tally-stable {n} bs p rewrite tally-stable (vmap vote bs) p\n | \u2261.sym (tabulate-ext (\u03bb i \u2192 lookup-map vote (to p i) bs))\n = \u2261.cong (\u03bb p\u2081 \u2192 tally p\u2081 \u2238 n) (tabulate-\u2218 _ _)\n \n{-\n Ballot privacy \u263a\n-}\n\nmodule BallotPrivacy\n (RA : Set)\n where\n\n data Msg : Set where\n vote! : (intent receipt : Vote) \u2192 Msg\n stop! : Msg\n \n Resp = List Ballot\n \n Adv\u2080 = RA \u2192 Vote\n Adv\u2081 = RA \u2192 Resp \u2192 Msg\n Adv\u2082 = RA \u2192 (l : Resp) \u2192 Vec Ballot (3 * length l) \u2192 Bit\n \n Adv = Adv\u2080 \u00d7 Adv\u2081 \u00d7 Adv\u2082\n \n \n G : Adv \u2192 (\u2200 {n} \u2192 Perm n) \u2192 RA \u2192 Bit \u2192 Bit\n G (A\u2080 , A\u2081 , A\u2082) \u03c0 Ra b = {!!} where\n \u03b5 = A\u2080 Ra\n\n go : (r : Resp) \u2192 Vec Ballot (3 * length r) \u2192 Bit\n go r bb with A\u2081 Ra r | b\n go r bb | vote! intent receipt | 1b = {!!}\n go r bb | vote! intent receipt | 0b = {!!}\n go r bb | stop! | _ = A\u2082 Ra r (shuffle \u03c0 bb) \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n3<4 : 3 < 4\n3<4 = s\u2264s (s\u2264s (s\u2264s (s\u2264s z\u2264n)))\n\n\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"3328b0f0320cd70ea6918039e6d504a93c44d273","subject":"Added missing error message.","message":"Added missing error message.\n\nIgnore-this: 5fd5052a9a7dfad2534f325dc4dc1154\n\ndarcs-hash:20110322153008-3bd4e-28bb0c94f0a5761321053f1e67ab85c652a3039d.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Test\/Fail\/NotErasedProofTerm.agda","new_file":"Test\/Fail\/NotErasedProofTerm.agda","new_contents":"------------------------------------------------------------------------------\n-- We do not erase of the proofs terms in the translation\n------------------------------------------------------------------------------\n\nmodule Test.Fail.NotErasedProofTerm where\n\n-- Error message:\n-- agda2atp: It is necessary to erase a proof term, but we do know how do it. The internal representation of term to be erased is:\n-- Pi r(El (Type (Lit (LitLevel 0))) (Def Test.Fail.NotErasedProofTerm.D [])) (Abs \"k\" El (Type (Lit (LitLevel 0))) (Def Test.Fail.NotErasedProofTerm._\u2264_ [r(Var 0 []),r(Var 0 [])]))\n\npostulate\n D : Set\n _\u2261_ : D \u2192 D \u2192 Set\n _\u2264_ : D \u2192 D \u2192 Set\n zero : D\n succ : D \u2192 D\n\ndata N : D \u2192 Set where\n zN : N zero\n sN : \u2200 {n} \u2192 N n \u2192 N (succ n)\n\nthm : \u2200 n \u2192 N n \u2192 (\u2200 k \u2192 k \u2264 k) \u2192 n \u2261 n\nthm n Nn h = prf\n where\n\n -- The internal type of prf is\n\n -- \u2200 (n : D) (Nn : N n) (h : \u2200 k \u2192 k \u2264 k) \u2192 ...\n\n -- The agda2atp tool can erase the proof term Nn, but it cannot erase the\n -- proof term h.\n\n postulate prf : n \u2261 n\n {-# ATP prove prf #-}\n","old_contents":"------------------------------------------------------------------------------\n-- We do not erase of the proofs terms in the translation\n------------------------------------------------------------------------------\n\nmodule Test.Fail.NotErasedProofTerm where\n\npostulate\n D : Set\n _\u2261_ : D \u2192 D \u2192 Set\n _\u2264_ : D \u2192 D \u2192 Set\n zero : D\n succ : D \u2192 D\n\ndata N : D \u2192 Set where\n zN : N zero\n sN : \u2200 {n} \u2192 N n \u2192 N (succ n)\n\nthm : \u2200 n \u2192 N n \u2192 (\u2200 k \u2192 k \u2264 k) \u2192 n \u2261 n\nthm n Nn h = prf\n where\n\n -- The internal type of prf is\n\n -- \u2200 (n : D) (Nn : N n) (h : \u2200 k \u2192 k \u2264 k) \u2192 ...\n\n -- The agda2atp tool can erase the proof term Nn, but it cannot erase the\n -- proof term h.\n\n postulate prf : n \u2261 n\n {-# ATP prove prf #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"26feb31d8f07b57839d5ad020a366816d0ae21fb","subject":"insert missing space (cosmetic)","message":"insert missing space (cosmetic)\n\nOld-commit-hash: 4d559497f68fabe2674c6d523508f715c78256c8\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Denotation\/Evaluation.agda","new_file":"Parametric\/Denotation\/Evaluation.agda","new_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\n\nmodule Parametric.Denotation.Evaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\n\nopen import Base.Syntax.Context Type\nopen import Base.Denotation.Environment Type \u27e6_\u27e7Type\nopen import Base.Denotation.Notation\n\nStructure : Set\nStructure = \u2200 {\u03a3 \u03c4} \u2192 Const \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\nmodule Structure (\u27e6_\u27e7Const : Structure) where\n \u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\n \u27e6_\u27e7Terms : \u2200 {\u0393 \u03a3} \u2192 Terms \u0393 \u03a3 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03a3 \u27e7\n\n \u27e6 const c args \u27e7Term \u03c1 = \u27e6 c \u27e7Const (\u27e6 args \u27e7Terms \u03c1)\n \u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n \u27e6 app s t \u27e7Term \u03c1 = (\u27e6 s \u27e7Term \u03c1) (\u27e6 t \u27e7Term \u03c1)\n \u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\n \u27e6 \u2205 \u27e7Terms \u03c1 = \u2205\n \u27e6 s \u2022 terms \u27e7Terms \u03c1 = \u27e6 s \u27e7Term \u03c1 \u2022 \u27e6 terms \u27e7Terms \u03c1\n \n meaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\n meaningOfTerm = meaning \u27e6_\u27e7Term\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\n\nmodule Parametric.Denotation.Evaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\n\nopen import Base.Syntax.Context Type\nopen import Base.Denotation.Environment Type \u27e6_\u27e7Type\nopen import Base.Denotation.Notation\n\nStructure : Set\nStructure = \u2200 {\u03a3 \u03c4} \u2192 Const \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\nmodule Structure(\u27e6_\u27e7Const : Structure) where\n \u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\n \u27e6_\u27e7Terms : \u2200 {\u0393 \u03a3} \u2192 Terms \u0393 \u03a3 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03a3 \u27e7\n\n \u27e6 const c args \u27e7Term \u03c1 = \u27e6 c \u27e7Const (\u27e6 args \u27e7Terms \u03c1)\n \u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n \u27e6 app s t \u27e7Term \u03c1 = (\u27e6 s \u27e7Term \u03c1) (\u27e6 t \u27e7Term \u03c1)\n \u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\n \u27e6 \u2205 \u27e7Terms \u03c1 = \u2205\n \u27e6 s \u2022 terms \u27e7Terms \u03c1 = \u27e6 s \u27e7Term \u03c1 \u2022 \u27e6 terms \u27e7Terms \u03c1\n \n meaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\n meaningOfTerm = meaning \u27e6_\u27e7Term\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b444ae81fd6c852a6409937d2ea0c38c95693b1e","subject":"Bits: upgrading Op to Bij, to\u2115-inj, some 2 ^ n turned into 2^ n","message":"Bits: upgrading Op to Bij, to\u2115-inj, some 2 ^ n turned into 2^ n\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative.NP\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties.NP\nimport Data.List.NP as L\nopen import Function.Bijection.SyntaxKit\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0b \u2237 1b \u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u2219_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u2219 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u2219 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u2219-comm : \u2200 {m} (x y : A m) \u2192 x \u2219 y \u2261 y \u2219 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u2219-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nmodule OperationSyntax where\n module BitBij = BoolBijection\n open BitBij public using (`id) renaming (BoolBij to BitBij; bool-bijKit to bitBijKit; `not to `not\u1d2e)\n open BijectionSyntax Bit BitBij public\n open BijectionSemantics bitBijKit public\n\n {-\n id \u03b5 id\n 0\u21941 swp-inners interchange\n not swap comm\n if0 first ...\n if1 second ...\n -}\n `not : Bij\n `not = BitBij.`not `\u2237 const `id\n\n `xor : Bit \u2192 Bij\n `xor b = BitBij.`xor b `\u2237 const `id\n\n `if : Bij \u2192 Bij \u2192 Bij\n `if f g = BitBij.`id `\u2237 cond f g\n\n `if0 : Bij \u2192 Bij\n `if0 f = `if `id f\n\n `if1 : Bij \u2192 Bij\n `if1 f = `if f `id\n\n -- law: `if0 f `\u204f `if1 g \u2261 `if1 g `; `if0 f\n\n on-firsts : Bij \u2192 Bij\n on-firsts f = `0\u21941 `\u204f `if0 f `\u204f `0\u21941\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 right swap\n -- (a \u2219 d) \u2219 (c \u2219 b)\n swp-seconds : Bij\n swp-seconds = `if1 `not `\u204f `0\u21941 `\u204f `if1 `not\n\n on-extremes : Bij \u2192 Bij\n -- on-extremes f = swp-seconds `\u204f `if0 f `\u204f swp-seconds\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap \u2261 if1 not\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange \u2261 0\u21941\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 left f \u2261 if0 f\n -- (A \u2219 D) \u2219 (b \u2219 c)\n -- where A \u2219 D = f (a \u2219 d)\n -- \u2261 interchange \u2261 0\u21941\n -- (A \u2219 b) \u2219 (D \u2219 c)\n -- \u2261 right swap \u2261 if1 not\n -- (A \u2219 b) \u2219 (c \u2219 D)\n on-extremes f = `if1 `not `\u204f `0\u21941 `\u204f `if0 f `\u204f `0\u21941 `\u204f `if1 `not\n\n map-inner : Bij \u2192 Bij\n map-inner f = `if1 `not `\u204f `0\u21941 `\u204f `if1 f `\u204f `0\u21941 `\u204f `if1 `not\n\n map-outer : Bij \u2192 Bij \u2192 Bij\n map-outer f g = `if g f\n\n 0\u21941\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bij\n 0\u21941\u2237 [] = `not\n 0\u21941\u2237 (true {-1-} \u2237 p) = on-extremes (0\u21941\u2237 p)\n 0\u21941\u2237 (false{-0-} \u2237 p) = on-firsts (0\u21941\u2237 p)\n\n 0\u2194_ : \u2200 {n} \u2192 Bits n \u2192 Bij\n 0\u2194 [] = `id\n 0\u2194 (false{-0-} \u2237 p) = `if0 (0\u2194 p)\n 0\u2194 (true{-1-} \u2237 p) = 0\u21941\u2237 p\n\n \u27e80\u2194_\u27e9-sem : \u2200 {n} (p : Bits n) \u2192 Bits n \u2192 Bits n\n \u27e80\u2194 p \u27e9-sem xs = if 0\u207f == xs then p else if p == xs then 0\u207f else xs\n\n if\u2237 : \u2200 {n} a x (xs ys : Bits n) \u2192 (if a then (x \u2237 xs) else (x \u2237 ys)) \u2261 x \u2237 (if a then xs else ys)\n if\u2237 true x xs ys = refl\n if\u2237 false x xs ys = refl\n\n if-not\u2237 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (false \u2237 xs) else (true \u2237 ys)) \u2261 (not a) \u2237 (if a then xs else ys)\n if-not\u2237 true xs ys = refl\n if-not\u2237 false xs ys = refl\n\n if\u2237\u2032 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (true \u2237 xs) else (false \u2237 ys)) \u2261 a \u2237 (if a then xs else ys)\n if\u2237\u2032 true xs ys = refl\n if\u2237\u2032 false xs ys = refl\n\n \u27e80\u21941\u2237_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u21941\u2237 p \u2219 xs \u2261 \u27e80\u2194 (1\u2237 p) \u27e9-sem xs\n \u27e80\u21941\u2237_\u27e9-spec [] (true \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec [] (false \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 true \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 false \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n\n \u27e80\u2194_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u2194 p \u2219 xs \u2261 \u27e80\u2194 p \u27e9-sem xs\n \u27e80\u2194_\u27e9-spec [] [] = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (true \u2237 xs) = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (false \u2237 xs)\n rewrite \u27e80\u2194 ps \u27e9-spec xs\n | if\u2237 (ps == xs) 0b 0\u207f xs\n | if\u2237 (0\u207f == xs) 0b ps (if ps == xs then 0\u207f else xs)\n = refl\n \u27e80\u2194_\u27e9-spec (true \u2237 p) xs = \u27e80\u21941\u2237 p \u27e9-spec xs\n\n private\n module P where\n open PermutationSyntax public\n open PermutationSemantics public\n open P using (Perm; `id; `0\u21941; _`\u204f_)\n\n `\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij\n `\u27e80\u21941+ zero \u27e9 = `0\u21941\n `\u27e80\u21941+ suc i \u27e9 = `0\u21941 `\u204f `tl `\u27e80\u21941+ i \u27e9 `\u204f `0\u21941\n\n `\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n `\u27e80\u21941+ zero \u27e9-spec xs = refl\n `\u27e80\u21941+ suc i \u27e9-spec (x \u2237 _ \u2237 xs) rewrite `\u27e80\u21941+ i \u27e9-spec (x \u2237 xs) = refl\n\n `\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij\n `\u27e80\u2194 zero \u27e9 = `id\n `\u27e80\u2194 suc i \u27e9 = `\u27e80\u21941+ i \u27e9\n\n `\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n `\u27e80\u2194 zero \u27e9-spec xs = refl\n `\u27e80\u2194 suc i \u27e9-spec xs = `\u27e80\u21941+ i \u27e9-spec xs\n\n {-\n `\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Bij\n `\u27e8 zero \u2194 j \u27e9 = `\u27e80\u2194 j \u27e9\n `\u27e8 i \u2194 zero \u27e9 = `\u27e80\u2194 i \u27e9\n `\u27e8 suc i \u2194 suc j \u27e9 = `tl `\u27e8 i \u2194 j \u27e9\n\n `\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) xs \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n `\u27e8_\u2194_\u27e9-spec zero j xs = `\u27e80\u2194 j \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) zero xs = `\u27e80\u21941+ i \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) (suc j) (x \u2237 xs) rewrite `\u27e8 i \u2194 j \u27e9-spec xs = refl\n -}\n\n `xor-head : Bit \u2192 Bij\n `xor-head b = if b then `not else `id\n\n `xor-head-spec : \u2200 b {n} x (xs : Bits n) \u2192 `xor-head b \u2219 (x \u2237 xs) \u2261 (b xor x) \u2237 xs\n `xor-head-spec true x xs = refl\n `xor-head-spec false x xs = refl\n\n `\u27e8_\u2295\u27e9 : \u2200 {n} \u2192 Bits n \u2192 Bij\n `\u27e8 [] \u2295\u27e9 = `id\n `\u27e8 b \u2237 xs \u2295\u27e9 = `xor-head b `\u204f `tl `\u27e8 xs \u2295\u27e9\n\n `\u27e8_\u2295\u27e9-spec : \u2200 {n} (xs ys : Bits n) \u2192 `\u27e8 xs \u2295\u27e9 \u2219 ys \u2261 xs \u2295 ys\n `\u27e8 [] \u2295\u27e9-spec [] = refl\n `\u27e8 true \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n `\u27e8 false \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) x \u2192 0\u21941 pad \u2295 0\u21941 x \u2261 0\u21941 (pad \u2295 x)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\nmodule PermutationSyntax-Props where\n open PermutationSyntax\n open PermutationSemantics\n -- open PermutationProperties\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) xs \u2192 0\u21941 pad \u2295 0\u21941 xs \u2261 0\u21941 (pad \u2295 xs)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\n -- TODO make use of \u229b-dist-\u2219\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 fs `id xs = refl\n \u2295-dist-\u2219 fs `0\u21941 xs = \u2295-dist-0\u21941 fs xs\n \u2295-dist-\u2219 [] (`tl \u03c0) [] = refl\n \u2295-dist-\u2219 (f \u2237 fs) (`tl \u03c0) (x \u2237 xs) rewrite \u2295-dist-\u2219 fs \u03c0 xs = refl\n \u2295-dist-\u2219 fs (\u03c0\u2080 `\u204f \u03c0\u2081) xs rewrite \u2295-dist-\u2219 (\u03c0\u2080 \u2219 fs) \u03c0\u2081 (\u03c0\u2080 \u2219 xs)\n | \u2295-dist-\u2219 fs \u03c0\u2080 xs = refl\n {-\n -- \u229b-dist-\u2219 : \u2200 {n a} {A : Set a} (fs : Vec (A \u2192 A) n) \u03c0 xs \u2192 \u03c0 \u2219 fs \u229b \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (fs \u229b xs)\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 pad \u03c0 xs = \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs\n \u2261\u27e8 refl \u27e9\n vmap _xor_ (\u03c0 \u2219 pad) \u229b \u03c0 \u2219 xs\n \u2261\u27e8 {!!} \u27e9\n \u03c0 \u2219 vmap _xor_ pad \u229b \u03c0 \u2219 xs\n \u2261\u27e8 \u229b-dist-\u2219 _ (vmap _xor_ pad) \u03c0 xs \u27e9\n \u03c0 \u2219 (vmap _xor_ pad \u229b xs)\n \u2261\u27e8 refl \u27e9\n \u03c0 \u2219 (pad \u2295 xs)\n \u220e where open \u2261-Reasoning\n -- rans {!\u229b-dist-\u2219 (vmap _xor_ (op \u2219 pad)) op xs!} (\u229b-dist-\u2219 (vmap _xor_ pad) op xs)\n-}\nmodule SimpleSearch {a} {A : Set a} (_\u2219_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u2219_ public\n\n module SearchUnit \u03b5 (\u03b5\u2219\u03b5 : \u03b5 \u2219 \u03b5 \u2261 \u03b5) where\n search-const\u03b5\u2261\u03b5 : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-const\u03b5\u2261\u03b5 zero = refl\n search-const\u03b5\u2261\u03b5 (suc n) rewrite search-const\u03b5\u2261\u03b5 n = \u03b5\u2219\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n module SearchInterchange (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u2219 f\u2081 x) \u2261 search f\u2080 \u2219 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u2219-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e\n where open \u2261-Reasoning\n\n search-0\u21941 : \u2200 {n} (f : Bits n \u2192 A) \u2192 search {n} (f \u2218 0\u21941) \u2261 search {n} f\n search-0\u21941 {zero} _ = refl\n search-0\u21941 {suc zero} _ = refl\n search-0\u21941 {suc (suc n)} _ = \u2219-interchange _ _ _ _\n\n module Bij (\u2219-comm : Commutative _\u2261_ _\u2219_)\n (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n open SearchInterchange \u2219-interchange using (search-0\u21941)\n open OperationSyntax hiding (_\u2219_)\n search-bij : \u2200 {n} (f : Bits n \u2192 A) (g : Bij) \u2192 search {n} (f \u2218 eval g) \u2261 search {n} f\n search-bij f `id = refl\n search-bij f `0\u21941 = search-0\u21941 f\n search-bij f (g `\u204f h)\n rewrite search-bij (f \u2218 eval h) g = search-bij f h\n search-bij {zero} f (_ `\u2237 _) = refl\n search-bij {suc n} f (`id `\u2237 g)\n rewrite search-bij (f \u2218 0\u2237_) (g 0b)\n | search-bij (f \u2218 1\u2237_) (g 1b) = refl\n search-bij {suc n} f (`not\u1d2e `\u2237 g)\n rewrite search-bij (f \u2218 0\u2237_) (g 1b)\n | search-bij (f \u2218 1\u2237_) (g 0b) = \u2219-comm _ _\n\nmodule Sum where\n open SimpleSearch _+_ using (module Comm; module SearchInterchange; module SearchUnit; module Bij)\n open SimpleSearch _+_ public using () renaming (search to sum; search-\u2257 to sum-\u2257; searchBit to sumBit;\n search-\u2257\u2082 to sum-\u2257\u2082;\n search-+ to sum-+)\n open Comm \u2115\u00b0.+-comm public renaming (search-comm to sum-comm)\n open SearchUnit 0 refl public renaming\n (search-const\u03b5\u2261\u03b5 to sum-const0\u22610)\n open SearchInterchange +-interchange public renaming (\n search-dist to sum-dist;\n search-searchBit to sum-sumBit;\n search-search to sum-sum;\n search-swap to sum-swap)\n open Bij \u2115\u00b0.+-comm +-interchange public renaming (search-bij to sum-bij)\n\n sum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\n sum-const zero _ = refl\n sum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nmodule Count where\n open Sum\n open OperationSyntax\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n #\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n -- #-ext\n #-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n #-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n #-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n #-bij : \u2200 {n} (f : Bits n \u2192 Bit) (g : Bij) \u2192 #\u27e8 f \u2218 eval g \u27e9 \u2261 #\u27e8 f \u27e9\n #-bij f = sum-bij (Bool.to\u2115 \u2218 f)\n\n #-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n #-\u2295 = #-comm\n\n #-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n #-const n b = sum-const n (Bool.to\u2115 b)\n\n #never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n #never\u22610 = sum-const0\u22610\n\n #always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n #always\u22612^ n = sum-const n 1\n\n #-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 Bit) \u2192 sum (\u03bb x \u2192 Bool.to\u2115 (f\u2080 x) + Bool.to\u2115 (f\u2081 x)) \u2261 #\u27e8 f\u2080 \u27e9 + #\u27e8 f\u2081 \u27e9\n #-dist f\u2080 f\u2081 = sum-dist (Bool.to\u2115 \u2218 f\u2080) (Bool.to\u2115 \u2218 f\u2081)\n\n #-+ : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n #\u27e8 f \u27e9 \u2261 sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9 )\n #-+ {m} {n} f = sum-+ {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-# : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9)\n \u2261 sum {n} (\u03bb ys \u2192 #\u27e8 (\u03bb (xs : Bits m) \u2192 f (xs ++ ys)) \u27e9)\n #-# {m} {n} f = sum-sum {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-swap : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192 #\u27e8 f \u2218 vswap n {m} \u27e9 \u2261 #\u27e8 f \u27e9\n #-swap {m} {n} f = sum-swap {m} {n} (Bool.to\u2115 \u2218 f)\n\n #\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n #\u27e8== [] \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n \u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n \u2257-cong-# f g f\u2257g = sum-\u2257 _ _ (cong Bool.to\u2115 \u2218 f\u2257g)\n\n -- #-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n -- #-+ f f0 f1 rewrite f0 | f1 = refl\n\n #-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-take-drop zero n f g with f []\n ... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n ... | false = #never\u22610 n\n #-take-drop (suc m) n f g\n rewrite \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x))\n | #-take-drop m n (f \u2218 0\u2237_) g\n | \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x))\n | #-take-drop m n (f \u2218 1\u2237_) g\n = sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9)\n\n #-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n #\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n #\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n #\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n ... | true | true | _ = s\u2264s z\u2264n\n ... | true | false | p = \u22a5-elim (p _)\n ... | false | _ | _ = z\u2264n\n #\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n #-\u2227-\u2228\u1d47 : \u2200 x y \u2192 Bool.to\u2115 (x \u2227 y) + Bool.to\u2115 (x \u2228 y) \u2261 Bool.to\u2115 x + Bool.to\u2115 y\n #-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (Bool.to\u2115 y) 1 = refl\n #-\u2227-\u2228\u1d47 false y = refl\n\n #-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n #-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #\u2228' {zero} f g with f []\n ... | true = s\u2264s z\u2264n\n ... | false = \u2115\u2264.refl\n #\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n #\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n #\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n #\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n #-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n #-bound zero f = Bool.to\u2115\u22641 (f [])\n #-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n #-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n #-\u2218vnot _ f = #-\u2295 1\u207f f\n\n #-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n #-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n #-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n #-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n #-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n #-not\u2218 zero f with f []\n ... | true = \u2261.refl\n ... | false = \u2261.refl\n #-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n #-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n #-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\nopen SimpleSearch public\nopen Sum public\nopen Count public\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192\n search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = search-\u2257 op _ _ (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2^ n) (toFin xs)\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2^ n + to\u2115 xs\n\nto\u2115-bound : \u2200 {n} (xs : Bits n) \u2192 to\u2115 xs < 2^ n \nto\u2115-bound [] = s\u2264s z\u2264n\nto\u2115-bound {suc n} (1b \u2237 xs) rewrite +-assoc-comm 1 (2^ n) (to\u2115 xs) = \u2115\u2264.refl {2^ n} +-mono to\u2115-bound xs\nto\u2115-bound {suc n} (0b \u2237 xs) = \u2264-steps (2^ n) (to\u2115-bound xs)\n\nto\u2115-inj : \u2200 {n} (x y : Bits n) \u2192 to\u2115 x \u2261 to\u2115 y \u2192 x \u2261 y\nto\u2115-inj [] [] _ = refl\nto\u2115-inj (0b \u2237 xs) (0b \u2237 ys) p = cong 0\u2237_ (to\u2115-inj xs ys p)\nto\u2115-inj {suc n} (1b \u2237 xs) (1b \u2237 ys) p = cong 1\u2237_ (to\u2115-inj xs ys (cancel-+-left (2^ n) p))\nto\u2115-inj {suc n} (0b \u2237 xs) (1b \u2237 ys) p rewrite \u2115\u00b0.+-comm (2^ n) (to\u2115 ys) = \u22a5-elim (<\u2192\u2262 (\u2264-steps (to\u2115 ys) (to\u2115-bound xs)) p)\nto\u2115-inj {suc n} (1b \u2237 xs) (0b \u2237 ys) p rewrite \u2115\u00b0.+-comm (2^ n) (to\u2115 xs) = \u22a5-elim (<\u2192\u2262 (\u2264-steps (to\u2115 xs) (to\u2115-bound ys)) (\u2261.sym p))\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (drop (2^ n) tbl)\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_)\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_)) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_))\n | take-++ (2^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (drop (2^ n) tbl)\n = take-drop-lem (2^ n) tbl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nopen Defs public\n","old_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative.NP\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties.NP\nimport Data.List.NP as L\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0b \u2237 1b \u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u2219_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u2219 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u2219 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u2219-comm : \u2200 {m} (x y : A m) \u2192 x \u2219 y \u2261 y \u2219 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u2219-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nmodule OperationSyntax where\n infixr 1 _`\u204f_\n data Op : Set where\n `id `0\u21941 `not : Op\n `tl : Op \u2192 Op\n `if0 : Op \u2192 Op\n _`\u204f_ : Op \u2192 Op \u2192 Op\n\n {-\n id \u03b5 id\n 0\u21941 swp-inners interchange\n not swap comm\n if0 first ...\n if1 second ...\n -}\n\n infixr 9 _\u2219_\n _\u2219_ : Op \u2192 \u2200 {n} \u2192 Endo (Bits n)\n `id \u2219 xs = xs\n `0\u21941 \u2219 xs = 0\u21941 xs\n `not \u2219 [] = []\n `not \u2219 (x \u2237 xs) = not x \u2237 xs\n `tl f \u2219 [] = []\n `tl f \u2219 (x \u2237 xs) = x \u2237 f \u2219 xs\n `if0 f \u2219 [] = []\n `if0 f \u2219 (false \u2237 xs) = false \u2237 f \u2219 xs\n `if0 f \u2219 (true \u2237 xs) = true \u2237 xs\n (f `\u204f g) \u2219 xs = g \u2219 f \u2219 xs\n\n `if1 : Op \u2192 Op\n `if1 f = `not `\u204f `if0 f `\u204f `not\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 right swap\n -- (a \u2219 d) \u2219 (c \u2219 b)\n swp-seconds : Op\n swp-seconds = `if1 `not `\u204f `0\u21941 `\u204f `if1 `not\n\n on-extremes : Op \u2192 Op\n on-extremes f = swp-seconds `\u204f `if0 f `\u204f swp-seconds\n\n on-firsts : Op \u2192 Op\n on-firsts f = `0\u21941 `\u204f `if0 f `\u204f `0\u21941\n\n 0\u21941\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Op\n 0\u21941\u2237 [] = `not\n 0\u21941\u2237 (true {-1-} \u2237 p) = on-extremes (0\u21941\u2237 p)\n 0\u21941\u2237 (false{-0-} \u2237 p) = on-firsts (0\u21941\u2237 p)\n\n 0\u2194_ : \u2200 {n} \u2192 Bits n \u2192 Op\n 0\u2194 [] = `id\n 0\u2194 (false{-0-} \u2237 p) = `if0 (0\u2194 p)\n 0\u2194 (true{-1-} \u2237 p) = 0\u21941\u2237 p\n\n \u27e80\u2194_\u27e9-sem : \u2200 {n} (p : Bits n) \u2192 Bits n \u2192 Bits n\n \u27e80\u2194 p \u27e9-sem xs = if 0\u207f == xs then p else if p == xs then 0\u207f else xs\n\n if\u2237 : \u2200 {n} a x (xs ys : Bits n) \u2192 (if a then (x \u2237 xs) else (x \u2237 ys)) \u2261 x \u2237 (if a then xs else ys)\n if\u2237 true x xs ys = refl\n if\u2237 false x xs ys = refl\n\n if-not\u2237 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (false \u2237 xs) else (true \u2237 ys)) \u2261 (not a) \u2237 (if a then xs else ys)\n if-not\u2237 true xs ys = refl\n if-not\u2237 false xs ys = refl\n\n if\u2237\u2032 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (true \u2237 xs) else (false \u2237 ys)) \u2261 a \u2237 (if a then xs else ys)\n if\u2237\u2032 true xs ys = refl\n if\u2237\u2032 false xs ys = refl\n\n \u27e80\u21941\u2237_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u21941\u2237 p \u2219 xs \u2261 \u27e80\u2194 (1\u2237 p) \u27e9-sem xs\n \u27e80\u21941\u2237_\u27e9-spec [] (true \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec [] (false \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 true \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 false \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n\n \u27e80\u2194_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u2194 p \u2219 xs \u2261 \u27e80\u2194 p \u27e9-sem xs\n \u27e80\u2194_\u27e9-spec [] [] = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (true \u2237 xs) = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (false \u2237 xs)\n rewrite \u27e80\u2194 ps \u27e9-spec xs\n | if\u2237 (ps == xs) 0b 0\u207f xs\n | if\u2237 (0\u207f == xs) 0b ps (if ps == xs then 0\u207f else xs)\n = refl\n \u27e80\u2194_\u27e9-spec (true \u2237 p) xs = \u27e80\u21941\u2237 p \u27e9-spec xs\n\n open PermutationSyntax using (Perm; `id; `0\u21941; `tl; _`\u204f_)\n module P = PermutationSemantics\n\n `\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Op\n `\u27e80\u21941+ zero \u27e9 = `0\u21941\n `\u27e80\u21941+ suc i \u27e9 = `0\u21941 `\u204f `tl `\u27e80\u21941+ i \u27e9 `\u204f `0\u21941\n\n `\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n `\u27e80\u21941+ zero \u27e9-spec xs = refl\n `\u27e80\u21941+ suc i \u27e9-spec (x \u2237 _ \u2237 xs) rewrite `\u27e80\u21941+ i \u27e9-spec (x \u2237 xs) = refl\n\n `\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Op\n `\u27e80\u2194 zero \u27e9 = `id\n `\u27e80\u2194 suc i \u27e9 = `\u27e80\u21941+ i \u27e9\n\n `\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n `\u27e80\u2194 zero \u27e9-spec xs = refl\n `\u27e80\u2194 suc i \u27e9-spec xs = `\u27e80\u21941+ i \u27e9-spec xs\n\n `\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Op\n `\u27e8 zero \u2194 j \u27e9 = `\u27e80\u2194 j \u27e9\n `\u27e8 i \u2194 zero \u27e9 = `\u27e80\u2194 i \u27e9\n `\u27e8 suc i \u2194 suc j \u27e9 = `tl `\u27e8 i \u2194 j \u27e9\n\n `\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) xs \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n `\u27e8_\u2194_\u27e9-spec zero j xs = `\u27e80\u2194 j \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) zero xs = `\u27e80\u21941+ i \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) (suc j) (x \u2237 xs) rewrite `\u27e8 i \u2194 j \u27e9-spec xs = refl\n\n `xor-head : Bit \u2192 Op\n `xor-head b = if b then `not else `id\n\n `xor-head-spec : \u2200 b {n} x (xs : Bits n) \u2192 `xor-head b \u2219 (x \u2237 xs) \u2261 (b xor x) \u2237 xs\n `xor-head-spec true x xs = refl\n `xor-head-spec false x xs = refl\n\n `\u27e8_\u2295\u27e9 : \u2200 {n} \u2192 Bits n \u2192 Op\n `\u27e8 [] \u2295\u27e9 = `id\n `\u27e8 b \u2237 xs \u2295\u27e9 = `xor-head b `\u204f `tl `\u27e8 xs \u2295\u27e9\n\n `\u27e8_\u2295\u27e9-spec : \u2200 {n} (xs ys : Bits n) \u2192 `\u27e8 xs \u2295\u27e9 \u2219 ys \u2261 xs \u2295 ys\n `\u27e8 [] \u2295\u27e9-spec [] = refl\n `\u27e8 true \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n `\u27e8 false \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) x \u2192 0\u21941 pad \u2295 0\u21941 x \u2261 0\u21941 (pad \u2295 x)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\nmodule PermutationSyntax-Props where\n open PermutationSyntax\n open PermutationSemantics\n -- open PermutationProperties\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) xs \u2192 0\u21941 pad \u2295 0\u21941 xs \u2261 0\u21941 (pad \u2295 xs)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\n -- TODO make use of \u229b-dist-\u2219\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 fs `id xs = refl\n \u2295-dist-\u2219 fs `0\u21941 xs = \u2295-dist-0\u21941 fs xs\n \u2295-dist-\u2219 [] (`tl \u03c0) [] = refl\n \u2295-dist-\u2219 (f \u2237 fs) (`tl \u03c0) (x \u2237 xs) rewrite \u2295-dist-\u2219 fs \u03c0 xs = refl\n \u2295-dist-\u2219 fs (\u03c0\u2080 `\u204f \u03c0\u2081) xs rewrite \u2295-dist-\u2219 (\u03c0\u2080 \u2219 fs) \u03c0\u2081 (\u03c0\u2080 \u2219 xs)\n | \u2295-dist-\u2219 fs \u03c0\u2080 xs = refl\n {-\n -- \u229b-dist-\u2219 : \u2200 {n a} {A : Set a} (fs : Vec (A \u2192 A) n) \u03c0 xs \u2192 \u03c0 \u2219 fs \u229b \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (fs \u229b xs)\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 pad \u03c0 xs = \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs\n \u2261\u27e8 refl \u27e9\n vmap _xor_ (\u03c0 \u2219 pad) \u229b \u03c0 \u2219 xs\n \u2261\u27e8 {!!} \u27e9\n \u03c0 \u2219 vmap _xor_ pad \u229b \u03c0 \u2219 xs\n \u2261\u27e8 \u229b-dist-\u2219 _ (vmap _xor_ pad) \u03c0 xs \u27e9\n \u03c0 \u2219 (vmap _xor_ pad \u229b xs)\n \u2261\u27e8 refl \u27e9\n \u03c0 \u2219 (pad \u2295 xs)\n \u220e where open \u2261-Reasoning\n -- rans {!\u229b-dist-\u2219 (vmap _xor_ (op \u2219 pad)) op xs!} (\u229b-dist-\u2219 (vmap _xor_ pad) op xs)\n-}\nmodule SimpleSearch {a} {A : Set a} (_\u2219_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u2219_ public\n\n module SearchUnit \u03b5 (\u03b5\u2219\u03b5 : \u03b5 \u2219 \u03b5 \u2261 \u03b5) where\n search-const\u03b5\u2261\u03b5 : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-const\u03b5\u2261\u03b5 zero = refl\n search-const\u03b5\u2261\u03b5 (suc n) rewrite search-const\u03b5\u2261\u03b5 n = \u03b5\u2219\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n module SearchInterchange (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u2219 f\u2081 x) \u2261 search f\u2080 \u2219 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u2219-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e\n where open \u2261-Reasoning\n\n search-0\u21941 : \u2200 {n} (f : Bits n \u2192 A) \u2192 search {n} (f \u2218 0\u21941) \u2261 search {n} f\n search-0\u21941 {zero} _ = refl\n search-0\u21941 {suc zero} _ = refl\n search-0\u21941 {suc (suc n)} _ = \u2219-interchange _ _ _ _\n\n module Op (\u2219-comm : Commutative _\u2261_ _\u2219_)\n (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n open SearchInterchange \u2219-interchange using (search-0\u21941)\n open OperationSyntax renaming (_\u2219_ to op)\n search-op : \u2200 {n} (f : Bits n \u2192 A) (g : Op) \u2192 search {n} (f \u2218 op g) \u2261 search {n} f\n search-op f `id = refl\n search-op f `0\u21941 = search-0\u21941 f\n search-op {zero} f `not = refl\n search-op {suc n} f `not = \u2219-comm _ _\n search-op {zero} f (`tl g) = refl\n search-op {suc n} f (`tl g) rewrite search-op (f \u2218 0\u2237_) g | search-op (f \u2218 1\u2237_) g = refl\n search-op {zero} f (`if0 g) = refl\n search-op {suc n} f (`if0 g) rewrite search-op (f \u2218 0\u2237_) g = refl\n search-op f (g `\u204f h) rewrite search-op (f \u2218 op h) g = search-op f h\n\nmodule Sum where\n open SimpleSearch _+_ using (module Comm; module SearchInterchange; module SearchUnit; module Op)\n open SimpleSearch _+_ public using () renaming (search to sum; search-\u2257 to sum-\u2257; searchBit to sumBit;\n search-\u2257\u2082 to sum-\u2257\u2082;\n search-+ to sum-+)\n open Comm \u2115\u00b0.+-comm public renaming (search-comm to sum-comm)\n open SearchUnit 0 refl public renaming\n (search-const\u03b5\u2261\u03b5 to sum-const0\u22610)\n open SearchInterchange +-interchange public renaming (\n search-dist to sum-dist;\n search-searchBit to sum-sumBit;\n search-search to sum-sum;\n search-swap to sum-swap)\n open Op \u2115\u00b0.+-comm +-interchange public renaming (search-op to sum-op)\n\n sum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\n sum-const zero _ = refl\n sum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nmodule Count where\n open Sum\n open OperationSyntax renaming (_\u2219_ to op)\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n #\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n -- #-ext\n #-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n #-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n #-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n #-op : \u2200 {n} (f : Bits n \u2192 Bit) (g : Op) \u2192 #\u27e8 f \u2218 op g \u27e9 \u2261 #\u27e8 f \u27e9\n #-op f = sum-op (Bool.to\u2115 \u2218 f)\n\n #-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n #-\u2295 = #-comm\n\n #-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n #-const n b = sum-const n (Bool.to\u2115 b)\n\n #never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n #never\u22610 = sum-const0\u22610\n\n #always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n #always\u22612^ n = sum-const n 1\n\n #-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 Bit) \u2192 sum (\u03bb x \u2192 Bool.to\u2115 (f\u2080 x) + Bool.to\u2115 (f\u2081 x)) \u2261 #\u27e8 f\u2080 \u27e9 + #\u27e8 f\u2081 \u27e9\n #-dist f\u2080 f\u2081 = sum-dist (Bool.to\u2115 \u2218 f\u2080) (Bool.to\u2115 \u2218 f\u2081)\n\n #-+ : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n #\u27e8 f \u27e9 \u2261 sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9 )\n #-+ {m} {n} f = sum-+ {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-# : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9)\n \u2261 sum {n} (\u03bb ys \u2192 #\u27e8 (\u03bb (xs : Bits m) \u2192 f (xs ++ ys)) \u27e9)\n #-# {m} {n} f = sum-sum {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-swap : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192 #\u27e8 f \u2218 vswap n {m} \u27e9 \u2261 #\u27e8 f \u27e9\n #-swap {m} {n} f = sum-swap {m} {n} (Bool.to\u2115 \u2218 f)\n\n #\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n #\u27e8== [] \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n \u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n \u2257-cong-# f g f\u2257g = sum-\u2257 _ _ (cong Bool.to\u2115 \u2218 f\u2257g)\n\n -- #-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n -- #-+ f f0 f1 rewrite f0 | f1 = refl\n\n #-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-take-drop zero n f g with f []\n ... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n ... | false = #never\u22610 n\n #-take-drop (suc m) n f g\n rewrite \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x))\n | #-take-drop m n (f \u2218 0\u2237_) g\n | \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x))\n | #-take-drop m n (f \u2218 1\u2237_) g\n = sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9)\n\n #-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n #\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n #\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n #\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n ... | true | true | _ = s\u2264s z\u2264n\n ... | true | false | p = \u22a5-elim (p _)\n ... | false | _ | _ = z\u2264n\n #\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n #-\u2227-\u2228\u1d47 : \u2200 x y \u2192 Bool.to\u2115 (x \u2227 y) + Bool.to\u2115 (x \u2228 y) \u2261 Bool.to\u2115 x + Bool.to\u2115 y\n #-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (Bool.to\u2115 y) 1 = refl\n #-\u2227-\u2228\u1d47 false y = refl\n\n #-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n #-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #\u2228' {zero} f g with f []\n ... | true = s\u2264s z\u2264n\n ... | false = \u2115\u2264.refl\n #\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n #\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n #\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n #\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n #-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n #-bound zero f = Bool.to\u2115\u22641 (f [])\n #-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n #-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n #-\u2218vnot _ f = #-\u2295 1\u207f f\n\n #-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n #-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n #-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n #-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n #-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n #-not\u2218 zero f with f []\n ... | true = \u2261.refl\n ... | false = \u2261.refl\n #-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n #-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n #-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\nopen SimpleSearch public\nopen Sum public\nopen Count public\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192\n search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = search-\u2257 op _ _ (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nopen Defs public\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"6a3bcddd66afcd08d8384d14a3adb3c2367319a6","subject":"Bits: fx","message":"Bits: fx\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative.NP\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nopen import Relation.Nullary\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties.NP\nimport Data.List.NP as L\nopen import Function.Bijection.SyntaxKit\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0b \u2237 1b \u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u2219_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u2219 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u2219 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u2219-comm : \u2200 {m} (x y : A m) \u2192 x \u2219 y \u2261 y \u2219 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u2219-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nmodule OperationSyntax where\n module BitBij = BoolBijection\n open BitBij public using (`id) renaming (BoolBij to BitBij; bool-bijKit to bitBijKit; `not to `not\u1d2e)\n open BijectionSyntax Bit BitBij public\n open BijectionSemantics bitBijKit public\n\n {-\n id \u03b5 id\n 0\u21941 swp-inners interchange\n not swap comm\n if0 first ...\n if1 second ...\n -}\n `not : Bij\n `not = BitBij.`not `\u2237 const `id\n\n `xor : Bit \u2192 Bij\n `xor b = BitBij.`xor b `\u2237 const `id\n\n `if : Bij \u2192 Bij \u2192 Bij\n `if f g = BitBij.`id `\u2237 cond f g\n\n `if0 : Bij \u2192 Bij\n `if0 f = `if `id f\n\n `if1 : Bij \u2192 Bij\n `if1 f = `if f `id\n\n -- law: `if0 f `\u204f `if1 g \u2261 `if1 g `; `if0 f\n\n on-firsts : Bij \u2192 Bij\n on-firsts f = `0\u21941 `\u204f `if0 f `\u204f `0\u21941\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 right swap\n -- (a \u2219 d) \u2219 (c \u2219 b)\n swp-seconds : Bij\n swp-seconds = `if1 `not `\u204f `0\u21941 `\u204f `if1 `not\n\n on-extremes : Bij \u2192 Bij\n -- on-extremes f = swp-seconds `\u204f `if0 f `\u204f swp-seconds\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap \u2261 if1 not\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange \u2261 0\u21941\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 left f \u2261 if0 f\n -- (A \u2219 D) \u2219 (b \u2219 c)\n -- where A \u2219 D = f (a \u2219 d)\n -- \u2261 interchange \u2261 0\u21941\n -- (A \u2219 b) \u2219 (D \u2219 c)\n -- \u2261 right swap \u2261 if1 not\n -- (A \u2219 b) \u2219 (c \u2219 D)\n on-extremes f = `if1 `not `\u204f `0\u21941 `\u204f `if0 f `\u204f `0\u21941 `\u204f `if1 `not\n\n map-inner : Bij \u2192 Bij\n map-inner f = `if1 `not `\u204f `0\u21941 `\u204f `if1 f `\u204f `0\u21941 `\u204f `if1 `not\n\n map-outer : Bij \u2192 Bij \u2192 Bij\n map-outer f g = `if g f\n\n 0\u21941\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bij\n 0\u21941\u2237 [] = `not\n 0\u21941\u2237 (true {-1-} \u2237 p) = on-extremes (0\u21941\u2237 p)\n 0\u21941\u2237 (false{-0-} \u2237 p) = on-firsts (0\u21941\u2237 p)\n\n 0\u2194_ : \u2200 {n} \u2192 Bits n \u2192 Bij\n 0\u2194 [] = `id\n 0\u2194 (false{-0-} \u2237 p) = `if0 (0\u2194 p)\n 0\u2194 (true{-1-} \u2237 p) = 0\u21941\u2237 p\n\n \u27e80\u2194_\u27e9-sem : \u2200 {n} (p : Bits n) \u2192 Bits n \u2192 Bits n\n \u27e80\u2194 p \u27e9-sem xs = if 0\u207f == xs then p else if p == xs then 0\u207f else xs\n\n if\u2237 : \u2200 {n} a x (xs ys : Bits n) \u2192 (if a then (x \u2237 xs) else (x \u2237 ys)) \u2261 x \u2237 (if a then xs else ys)\n if\u2237 true x xs ys = refl\n if\u2237 false x xs ys = refl\n\n if-not\u2237 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (false \u2237 xs) else (true \u2237 ys)) \u2261 (not a) \u2237 (if a then xs else ys)\n if-not\u2237 true xs ys = refl\n if-not\u2237 false xs ys = refl\n\n if\u2237\u2032 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (true \u2237 xs) else (false \u2237 ys)) \u2261 a \u2237 (if a then xs else ys)\n if\u2237\u2032 true xs ys = refl\n if\u2237\u2032 false xs ys = refl\n\n \u27e80\u21941\u2237_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u21941\u2237 p \u2219 xs \u2261 \u27e80\u2194 (1\u2237 p) \u27e9-sem xs\n \u27e80\u21941\u2237_\u27e9-spec [] (true \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec [] (false \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 true \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 false \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n\n \u27e80\u2194_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u2194 p \u2219 xs \u2261 \u27e80\u2194 p \u27e9-sem xs\n \u27e80\u2194_\u27e9-spec [] [] = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (true \u2237 xs) = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (false \u2237 xs)\n rewrite \u27e80\u2194 ps \u27e9-spec xs\n | if\u2237 (ps == xs) 0b 0\u207f xs\n | if\u2237 (0\u207f == xs) 0b ps (if ps == xs then 0\u207f else xs)\n = refl\n \u27e80\u2194_\u27e9-spec (true \u2237 p) xs = \u27e80\u21941\u2237 p \u27e9-spec xs\n\n private\n module P where\n open PermutationSyntax public\n open PermutationSemantics public\n open P using (Perm; `id; `0\u21941; _`\u204f_)\n\n `\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij\n `\u27e80\u21941+ zero \u27e9 = `0\u21941\n `\u27e80\u21941+ suc i \u27e9 = `0\u21941 `\u204f `tl `\u27e80\u21941+ i \u27e9 `\u204f `0\u21941\n\n `\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n `\u27e80\u21941+ zero \u27e9-spec xs = refl\n `\u27e80\u21941+ suc i \u27e9-spec (x \u2237 _ \u2237 xs) rewrite `\u27e80\u21941+ i \u27e9-spec (x \u2237 xs) = refl\n\n `\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij\n `\u27e80\u2194 zero \u27e9 = `id\n `\u27e80\u2194 suc i \u27e9 = `\u27e80\u21941+ i \u27e9\n\n `\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n `\u27e80\u2194 zero \u27e9-spec xs = refl\n `\u27e80\u2194 suc i \u27e9-spec xs = `\u27e80\u21941+ i \u27e9-spec xs\n\n {-\n `\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Bij\n `\u27e8 zero \u2194 j \u27e9 = `\u27e80\u2194 j \u27e9\n `\u27e8 i \u2194 zero \u27e9 = `\u27e80\u2194 i \u27e9\n `\u27e8 suc i \u2194 suc j \u27e9 = `tl `\u27e8 i \u2194 j \u27e9\n\n `\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) xs \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n `\u27e8_\u2194_\u27e9-spec zero j xs = `\u27e80\u2194 j \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) zero xs = `\u27e80\u21941+ i \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) (suc j) (x \u2237 xs) rewrite `\u27e8 i \u2194 j \u27e9-spec xs = refl\n -}\n\n `xor-head : Bit \u2192 Bij\n `xor-head b = if b then `not else `id\n\n `xor-head-spec : \u2200 b {n} x (xs : Bits n) \u2192 `xor-head b \u2219 (x \u2237 xs) \u2261 (b xor x) \u2237 xs\n `xor-head-spec true x xs = refl\n `xor-head-spec false x xs = refl\n\n `\u27e8_\u2295\u27e9 : \u2200 {n} \u2192 Bits n \u2192 Bij\n `\u27e8 [] \u2295\u27e9 = `id\n `\u27e8 b \u2237 xs \u2295\u27e9 = `xor-head b `\u204f `tl `\u27e8 xs \u2295\u27e9\n\n `\u27e8_\u2295\u27e9-spec : \u2200 {n} (xs ys : Bits n) \u2192 `\u27e8 xs \u2295\u27e9 \u2219 ys \u2261 xs \u2295 ys\n `\u27e8 [] \u2295\u27e9-spec [] = refl\n `\u27e8 true \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n `\u27e8 false \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) x \u2192 0\u21941 pad \u2295 0\u21941 x \u2261 0\u21941 (pad \u2295 x)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\nmodule PermutationSyntax-Props where\n open PermutationSyntax\n open PermutationSemantics\n -- open PermutationProperties\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) xs \u2192 0\u21941 pad \u2295 0\u21941 xs \u2261 0\u21941 (pad \u2295 xs)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\n -- TODO make use of \u229b-dist-\u2219\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 fs `id xs = refl\n \u2295-dist-\u2219 fs `0\u21941 xs = \u2295-dist-0\u21941 fs xs\n \u2295-dist-\u2219 [] (`tl \u03c0) [] = refl\n \u2295-dist-\u2219 (f \u2237 fs) (`tl \u03c0) (x \u2237 xs) rewrite \u2295-dist-\u2219 fs \u03c0 xs = refl\n \u2295-dist-\u2219 fs (\u03c0\u2080 `\u204f \u03c0\u2081) xs rewrite \u2295-dist-\u2219 (\u03c0\u2080 \u2219 fs) \u03c0\u2081 (\u03c0\u2080 \u2219 xs)\n | \u2295-dist-\u2219 fs \u03c0\u2080 xs = refl\n {-\n -- \u229b-dist-\u2219 : \u2200 {n a} {A : Set a} (fs : Vec (A \u2192 A) n) \u03c0 xs \u2192 \u03c0 \u2219 fs \u229b \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (fs \u229b xs)\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 pad \u03c0 xs = \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs\n \u2261\u27e8 refl \u27e9\n vmap _xor_ (\u03c0 \u2219 pad) \u229b \u03c0 \u2219 xs\n \u2261\u27e8 TODO \u27e9\n \u03c0 \u2219 vmap _xor_ pad \u229b \u03c0 \u2219 xs\n \u2261\u27e8 \u229b-dist-\u2219 _ (vmap _xor_ pad) \u03c0 xs \u27e9\n \u03c0 \u2219 (vmap _xor_ pad \u229b xs)\n \u2261\u27e8 refl \u27e9\n \u03c0 \u2219 (pad \u2295 xs)\n \u220e where open \u2261-Reasoning\n -- rans {!\u229b-dist-\u2219 (vmap _xor_ (op \u2219 pad)) op xs!} (\u229b-dist-\u2219 (vmap _xor_ pad) op xs)\n-}\nmodule SimpleSearch {a} {A : Set a} (_\u2219_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u2219_ public\n\n module SearchUnit \u03b5 (\u03b5\u2219\u03b5 : \u03b5 \u2219 \u03b5 \u2261 \u03b5) where\n search-const\u03b5\u2261\u03b5 : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-const\u03b5\u2261\u03b5 zero = refl\n search-const\u03b5\u2261\u03b5 (suc n) rewrite search-const\u03b5\u2261\u03b5 n = \u03b5\u2219\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n module SearchInterchange (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u2219 f\u2081 x) \u2261 search f\u2080 \u2219 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u2219-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e where open \u2261-Reasoning\n\n search-0\u21941 : \u2200 {n} (f : Bits n \u2192 A) \u2192 search {n} (f \u2218 0\u21941) \u2261 search {n} f\n search-0\u21941 {zero} _ = refl\n search-0\u21941 {suc zero} _ = refl\n search-0\u21941 {suc (suc n)} _ = \u2219-interchange _ _ _ _\n\n module Bij (\u2219-comm : Commutative _\u2261_ _\u2219_)\n (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n open SearchInterchange \u2219-interchange using (search-0\u21941)\n open OperationSyntax hiding (_\u2219_)\n search-bij : \u2200 {n} f (g : Bits n \u2192 A) \u2192 search (g \u2218 eval f) \u2261 search g\n search-bij `id _ = refl\n search-bij `0\u21941 f = search-0\u21941 f\n search-bij (f `\u204f g) h\n rewrite search-bij f (h \u2218 eval g)\n | search-bij g h\n = refl\n search-bij {zero} (_ `\u2237 _) _ = refl\n search-bij {suc n} (`id `\u2237 f) g\n rewrite search-bij (f 0b) (g \u2218 0\u2237_)\n | search-bij (f 1b) (g \u2218 1\u2237_)\n = refl\n search-bij {suc n} (`not\u1d2e `\u2237 f) g\n rewrite search-bij (f 1b) (g \u2218 0\u2237_)\n | search-bij (f 0b) (g \u2218 1\u2237_)\n = \u2219-comm _ _\n\nmodule Sum where\n open SimpleSearch _+_ using (module Comm; module SearchInterchange; module SearchUnit; module Bij)\n open SimpleSearch _+_ public using () renaming (search to sum; search-\u2257 to sum-\u2257; searchBit to sumBit;\n search-\u2257\u2082 to sum-\u2257\u2082;\n search-+ to sum-+)\n open Comm \u2115\u00b0.+-comm public renaming (search-comm to sum-comm)\n open SearchUnit 0 refl public renaming\n (search-const\u03b5\u2261\u03b5 to sum-const0\u22610)\n open SearchInterchange +-interchange public renaming (\n search-dist to sum-dist;\n search-searchBit to sum-sumBit;\n search-search to sum-sum;\n search-swap to sum-swap)\n open Bij \u2115\u00b0.+-comm +-interchange public renaming (search-bij to sum-bij)\n\n sum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\n sum-const zero _ = refl\n sum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nmodule Count where\n open Sum\n open OperationSyntax\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n #\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n -- #-ext\n #-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n #-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n #-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n #-bij : \u2200 {n} f (g : Bits n \u2192 Bit) \u2192 #\u27e8 g \u2218 eval f \u27e9 \u2261 #\u27e8 g \u27e9\n #-bij f g = sum-bij f (Bool.to\u2115 \u2218 g)\n\n #-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n #-\u2295 = #-comm\n\n #-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n #-const n b = sum-const n (Bool.to\u2115 b)\n\n #never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n #never\u22610 = sum-const0\u22610\n\n #always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n #always\u22612^ n = sum-const n 1\n\n #-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 Bit) \u2192 sum (\u03bb x \u2192 Bool.to\u2115 (f\u2080 x) + Bool.to\u2115 (f\u2081 x)) \u2261 #\u27e8 f\u2080 \u27e9 + #\u27e8 f\u2081 \u27e9\n #-dist f\u2080 f\u2081 = sum-dist (Bool.to\u2115 \u2218 f\u2080) (Bool.to\u2115 \u2218 f\u2081)\n\n #-+ : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n #\u27e8 f \u27e9 \u2261 sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9 )\n #-+ {m} {n} f = sum-+ {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-# : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9)\n \u2261 sum {n} (\u03bb ys \u2192 #\u27e8 (\u03bb (xs : Bits m) \u2192 f (xs ++ ys)) \u27e9)\n #-# {m} {n} f = sum-sum {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-swap : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192 #\u27e8 f \u2218 vswap n {m} \u27e9 \u2261 #\u27e8 f \u27e9\n #-swap {m} {n} f = sum-swap {m} {n} (Bool.to\u2115 \u2218 f)\n\n #\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n #\u27e8== [] \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n \u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n \u2257-cong-# f g f\u2257g = sum-\u2257 _ _ (cong Bool.to\u2115 \u2218 f\u2257g)\n\n -- #-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n -- #-+ f f0 f1 rewrite f0 | f1 = refl\n\n #-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-take-drop zero n f g with f []\n ... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n ... | false = #never\u22610 n\n #-take-drop (suc m) n f g\n rewrite \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x))\n | #-take-drop m n (f \u2218 0\u2237_) g\n | \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x))\n | #-take-drop m n (f \u2218 1\u2237_) g\n = sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9)\n\n #-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n #\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n #\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n #\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n ... | true | true | _ = s\u2264s z\u2264n\n ... | true | false | p = \u22a5-elim (p _)\n ... | false | _ | _ = z\u2264n\n #\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n #-\u2227-\u2228\u1d47 : \u2200 x y \u2192 Bool.to\u2115 (x \u2227 y) + Bool.to\u2115 (x \u2228 y) \u2261 Bool.to\u2115 x + Bool.to\u2115 y\n #-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (Bool.to\u2115 y) 1 = refl\n #-\u2227-\u2228\u1d47 false y = refl\n\n #-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n #-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #\u2228' {zero} f g with f []\n ... | true = s\u2264s z\u2264n\n ... | false = \u2115\u2264.refl\n #\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n #\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n #\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n #\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n #-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n #-bound zero f = Bool.to\u2115\u22641 (f [])\n #-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n #-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n #-\u2218vnot _ f = #-\u2295 1\u207f f\n\n #-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n #-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n #-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n #-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n #-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n #-not\u2218 zero f with f []\n ... | true = \u2261.refl\n ... | false = \u2261.refl\n #-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n #-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n #-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\nopen SimpleSearch public\nopen Sum public\nopen Count public\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192\n search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = search-\u2257 op _ _ (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2^ n) (toFin xs)\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2^ n + to\u2115 xs\n\nto\u2115-bound : \u2200 {n} (xs : Bits n) \u2192 to\u2115 xs < 2^ n \nto\u2115-bound [] = s\u2264s z\u2264n\nto\u2115-bound {suc n} (1b \u2237 xs) rewrite +-assoc-comm 1 (2^ n) (to\u2115 xs) = \u2115\u2264.refl {2^ n} +-mono to\u2115-bound xs\nto\u2115-bound {suc n} (0b \u2237 xs) = \u2264-steps (2^ n) (to\u2115-bound xs)\n\nto\u2115\u22642\u207f+ : \u2200 {n} (x : Bits n) {y} \u2192 to\u2115 {n} x \u2264 2^ n + y\nto\u2115\u22642\u207f+ {n} x {y} = \u2115\u2264.trans (\u2264-steps y (\u2264-pred (\u2264-steps 1 (to\u2115-bound x))))\n (\u2115\u2264.reflexive (\u2115\u00b0.+-comm y (2^ n)))\n\n2\u207f+\u2270to\u2115 : \u2200 {n x} (y : Bits n) \u2192 2^ n + x \u2270 to\u2115 {n} y\n2\u207f+\u2270to\u2115 {n} {x} y p = \u00acn+\u2264y e \u2264 d, then d is the greatest post-fixed point\n-- of f).\n\n-- N.B. This is a second-order axiom. In the automatic proofs, we\n-- *must* use an instance. Therefore, we do not add this postulate as\n-- an ATP axiom.\npostulate\n Fair-gfp\u2082 : (P : D \u2192 Set) \u2192\n -- P is post-fixed point of FairF.\n (\u2200 {os} \u2192 P os \u2192\n \u2203 \u03bb ol \u2192 \u2203 \u03bb os' \u2192 O*L ol \u2227 P os' \u2227 os \u2261 ol ++ os') \u2192\n -- Fair is greater than P.\n \u2200 {os} \u2192 P os \u2192 Fair os\n\n-- Because a greatest post-fixed point is a fixed point, then the Fair\n-- predicate is also a pre-fixed point of the functor FairF (f d \u2264 d).\nFair-gfp\u2083 : \u2200 {os} \u2192\n (\u2203 \u03bb ol \u2192 \u2203 \u03bb os' \u2192 O*L ol \u2227 Fair os' \u2227 os \u2261 ol ++ os') \u2192\n Fair os\nFair-gfp\u2083 h = Fair-gfp\u2082 P helper h\n where\n P : D \u2192 Set\n P ws = \u2203 \u03bb wl \u2192 \u2203 \u03bb ws' \u2192 O*L wl \u2227 Fair ws' \u2227 ws \u2261 wl ++ ws'\n\n helper : {os : D} \u2192 P os \u2192 \u2203 \u03bb ol \u2192 \u2203 \u03bb os' \u2192 O*L ol \u2227 P os' \u2227 os \u2261 ol ++ os'\n helper (ol , os' , OLol , Fos' , h) = ol , os' , OLol , Fair-gfp\u2081 Fos' , h\n","old_contents":"------------------------------------------------------------------------------\n-- Fairness of the ABP channels\n------------------------------------------------------------------------------\n\nmodule FOTC.Program.ABP.Fair where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.List\n\nopen import FOTC.Program.ABP.Terms\n\n------------------------------------------------------------------------------\n-- The Fair co-inductive predicate\n\n-- From the paper: al : O*L if al is a list of zero or more O's\n-- followed by a final L.\ndata O*L : D \u2192 Set where\n nilO*L : O*L (L \u2237 [])\n consO*L : \u2200 ol \u2192 O*L ol \u2192 O*L (O \u2237 ol)\n\n-- Functor for the Fair type.\n-- FairF : (D \u2192 Set) \u2192 D \u2192 Set\n-- FairF X os = \u2203 \u03bb ol \u2192 \u2203 \u03bb os' \u2192 O*L ol \u2227 X os' \u2227 os \u2261 ol ++ os'\n\npostulate Fair : D \u2192 Set\n\n-- Fair is post-fixed point of FairF (d \u2264 f d).\npostulate\n Fair-gfp\u2081 : \u2200 {os} \u2192 Fair os \u2192\n \u2203 \u03bb ol \u2192 \u2203 \u03bb os' \u2192 O*L ol \u2227 Fair os' \u2227 os \u2261 ol ++ os'\n{-# ATP axiom Fair-gfp\u2081 #-}\n\n-- Fair is the greatest post-fixed of FairF.\n--\n-- (If \u2200 e. e \u2264 f e => e \u2264 d, then d is the greatest post-fixed point\n-- of f).\n\n-- N.B. This is a second-order axiom. In the automatic proofs, we\n-- *must* use an instance. Therefore, we do not add this postulate as\n-- an ATP axiom.\npostulate\n Fair-gfp\u2082 : (P : D \u2192 Set) \u2192\n -- P is post-fixed point of FairF.\n ( \u2200 {os} \u2192 P os \u2192\n \u2203 \u03bb ol \u2192 \u2203 \u03bb os' \u2192 O*L ol \u2227 P os' \u2227 os \u2261 ol ++ os' ) \u2192\n -- Fair is greater than P.\n \u2200 {os} \u2192 P os \u2192 Fair os\n\n-- Because a greatest post-fixed point is a fixed point, then the Fair\n-- predicate is also a pre-fixed point of the functor FairF (f d \u2264 d).\nFair-gfp\u2083 : \u2200 {os} \u2192\n (\u2203 \u03bb ol \u2192 \u2203 \u03bb os' \u2192 O*L ol \u2227 Fair os' \u2227 os \u2261 ol ++ os') \u2192\n Fair os\nFair-gfp\u2083 h = Fair-gfp\u2082 P helper h\n where\n P : D \u2192 Set\n P ws = \u2203 \u03bb wl \u2192 \u2203 \u03bb ws' \u2192 O*L wl \u2227 Fair ws' \u2227 ws \u2261 wl ++ ws'\n\n helper : {os : D} \u2192 P os \u2192 \u2203 \u03bb ol \u2192 \u2203 \u03bb os' \u2192 O*L ol \u2227 P os' \u2227 os \u2261 ol ++ os'\n helper (ol , os' , OLol , Fos' , h) = ol , os' , OLol , Fair-gfp\u2081 Fos' , h\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"68ba92194de15faf012a5b3ec096b43ef5b7dfc4","subject":"We cannot prove Colist-coind-stronger-ho from Colist-coind-ho.","message":"We cannot prove Colist-coind-stronger-ho from Colist-coind-ho.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/GreatestFixedPoints\/Colist.agda","new_file":"notes\/fixed-points\/GreatestFixedPoints\/Colist.agda","new_contents":"------------------------------------------------------------------------------\n-- Co-lists\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule GreatestFixedPoints.Colist where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\n\n------------------------------------------------------------------------------\n-- Colist is a greatest fixed-point of a functor\n\n-- The functor.\nColistF : (D \u2192 Set) \u2192 D \u2192 Set\nColistF A xs = xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs')\n\n-- Colist is the greatest fixed-point of ColistF.\npostulate\n Colist : D \u2192 Set\n\n -- Colist is a post-fixed point of ColistF, i.e.\n --\n -- Colist \u2264 ColistF Colist.\n Colist-out-ho : \u2200 {n} \u2192 Colist n \u2192 ColistF Colist n\n\n -- Colist is the greatest post-fixed point of ColistF, i.e.\n --\n -- \u2200 A. A \u2264 ColistF A \u21d2 A \u2264 Colist.\n Colist-coind-ho :\n (A : D \u2192 Set) \u2192\n -- A is post-fixed point of ColistF.\n (\u2200 {xs} \u2192 A xs \u2192 ColistF A xs) \u2192\n -- Colist is greater than A.\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\n\n------------------------------------------------------------------------------\n-- First-order versions\n\nColist-out : \u2200 {xs} \u2192\n Colist xs \u2192\n xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Colist xs')\nColist-out = Colist-out-ho\n\nColist-coind :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192 xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs')) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\nColist-coind = Colist-coind-ho\n\n------------------------------------------------------------------------------\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Colist predicate is also a pre-fixed point of the functional\n-- ColistF, i.e.\n--\n-- ColistF Colist \u2264 Colist.\nColist-in-ho : \u2200 {xs} \u2192 ColistF Colist xs \u2192 Colist xs\nColist-in-ho h = Colist-coind-ho A h' h\n where\n A : D \u2192 Set\n A xs = ColistF Colist xs\n\n h' : \u2200 {xs} \u2192 A xs \u2192 ColistF A xs\n h' (inj\u2081 xs\u22610) = inj\u2081 xs\u22610\n h' (inj\u2082 (x' , xs' , prf , CLxs' )) =\n inj\u2082 (x' , xs' , prf , Colist-out CLxs')\n\n-- The first-order version.\nColist-in : \u2200 {xs} \u2192\n xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Colist xs') \u2192\n Colist xs\nColist-in = Colist-in-ho\n\n------------------------------------------------------------------------------\n-- A stronger co-induction principle\n--\n-- From (Paulson, 1997. p. 16).\n\npostulate\n Colist-coind-stronger-ho :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192 ColistF A xs \u2228 Colist xs) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\n\nColist-coind-ho' :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192 ColistF A xs) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\nColist-coind-ho' A h Axs =\n Colist-coind-stronger-ho A (\u03bb Ays \u2192 inj\u2081 (h Ays)) Axs\n\n-- The first-order version.\nColist-coind-stronger :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192\n (xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs'))\n \u2228 Colist xs) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\nColist-coind-stronger = Colist-coind-stronger-ho\n\n-- 13 January 2014. As expected, we cannot prove\n-- Colist-coind-stronger-ho from Colist-coind-stronger.\nColist-coind-stronger-ho' :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192 ColistF A xs \u2228 Colist xs) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\nColist-coind-stronger-ho' A h {xs} Axs = case prf (\u03bb h' \u2192 h') (h Axs)\n where\n prf : ColistF A xs \u2192 Colist xs\n prf h' = Colist-coind-ho A {!!} Axs\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Paulson, L. C. (1997). Mechanizing Coinduction and Corecursion in\n-- Higher-order Logic. In: Journal of Logic and Computation 7.2,\n-- pp. 175\u2013204.\n","old_contents":"------------------------------------------------------------------------------\n-- Co-lists\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule GreatestFixedPoints.Colist where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\n\n------------------------------------------------------------------------------\n-- Colist is a greatest fixed-point of a functor\n\n-- The functor.\nColistF : (D \u2192 Set) \u2192 D \u2192 Set\nColistF A xs = xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs')\n\n-- Colist is the greatest fixed-point of ColistF.\npostulate\n Colist : D \u2192 Set\n\n -- Colist is a post-fixed point of ColistF, i.e.\n --\n -- Colist \u2264 ColistF Colist.\n Colist-out-ho : \u2200 {n} \u2192 Colist n \u2192 ColistF Colist n\n\n -- Colist is the greatest post-fixed point of ColistF, i.e.\n --\n -- \u2200 A. A \u2264 ColistF A \u21d2 A \u2264 Colist.\n Colist-coind-ho :\n (A : D \u2192 Set) \u2192\n -- A is post-fixed point of ColistF.\n (\u2200 {xs} \u2192 A xs \u2192 ColistF A xs) \u2192\n -- Colist is greater than A.\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\n\n------------------------------------------------------------------------------\n-- First-order versions\n\nColist-out : \u2200 {xs} \u2192\n Colist xs \u2192\n xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Colist xs')\nColist-out = Colist-out-ho\n\nColist-coind :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192 xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs')) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\nColist-coind = Colist-coind-ho\n\n------------------------------------------------------------------------------\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Colist predicate is also a pre-fixed point of the functional\n-- ColistF, i.e.\n--\n-- ColistF Colist \u2264 Colist.\nColist-in-ho : \u2200 {xs} \u2192 ColistF Colist xs \u2192 Colist xs\nColist-in-ho h = Colist-coind-ho A h' h\n where\n A : D \u2192 Set\n A xs = ColistF Colist xs\n\n h' : \u2200 {xs} \u2192 A xs \u2192 ColistF A xs\n h' (inj\u2081 xs\u22610) = inj\u2081 xs\u22610\n h' (inj\u2082 (x' , xs' , prf , CLxs' )) =\n inj\u2082 (x' , xs' , prf , Colist-out CLxs')\n\n-- The first-order version.\nColist-in : \u2200 {xs} \u2192\n xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Colist xs') \u2192\n Colist xs\nColist-in = Colist-in-ho\n\n------------------------------------------------------------------------------\n-- A stronger co-induction principle\n--\n-- From (Paulson, 1997. p. 16).\n\npostulate\n Colist-coind-stronger-ho :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192 ColistF A xs \u2228 Colist xs) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\n\nColist-coind-ho' :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192 ColistF A xs) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\nColist-coind-ho' A h Axs =\n Colist-coind-stronger-ho A (\u03bb Ays \u2192 inj\u2081 (h Ays)) Axs\n\n-- The first-order version.\nColist-coind-stronger :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192\n (xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs'))\n \u2228 Colist xs) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\nColist-coind-stronger = Colist-coind-stronger-ho\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Paulson, L. C. (1997). Mechanizing Coinduction and Corecursion in\n-- Higher-order Logic. In: Journal of Logic and Computation 7.2,\n-- pp. 175\u2013204.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9354d1da9a790382d98d46653573abc5a5b8bb56","subject":"finished stating and proving all but one checksum for #19","message":"finished stating and proving all but one checksum for #19\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"canonical-indeterminate-forms.agda","new_file":"canonical-indeterminate-forms.agda","new_contents":"open import Nat\nopen import Prelude\nopen import contexts\nopen import core\nopen import type-assignment-unicity\n\nmodule canonical-indeterminate-forms where\n canonical-indeterminate-forms-base : \u2200{\u0394 d} \u2192\n \u0394 , \u2205 \u22a2 d :: b \u2192\n d indet \u2192\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] b) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] b) \u2208 \u0394))) +\n \u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c42 \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: \u03c42 ==> b) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c42) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))\n +\n (\u03a3[ d' \u2208 dhexp ]\n ((d == d' \u27e8 \u2987\u2988 \u21d2 b \u27e9) \u00d7\n (d' indet) \u00d7\n ((d'' : dhexp) (\u03c4' : htyp) \u2192 d' \u2260 (d'' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9))))\n canonical-indeterminate-forms-base TAConst ()\n canonical-indeterminate-forms-base (TAVar x\u2081) ()\n canonical-indeterminate-forms-base (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-base (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-base (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x))\n canonical-indeterminate-forms-base (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) = Inr (Inr (Inr (_ , refl , ind , x\u2081)))\n\n canonical-indeterminate-forms-arr : \u2200{\u0394 d \u03c41 \u03c42 } \u2192\n \u0394 , \u2205 \u22a2 d :: (\u03c41 ==> \u03c42) \u2192\n d indet \u2192\n ((\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] (\u03c41 ==> \u03c42)) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] (\u03c41 ==> \u03c42)) \u2208 \u0394))) +\n (\u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c4 \u2208 htyp ] \u03a3[ \u03c41' \u2208 htyp ] \u03a3[ \u03c42' \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: \u03c4 ==> (\u03c41' ==> \u03c42')) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c4) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))) +\n (\u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c41 \u2208 htyp ] \u03a3[ \u03c42 \u2208 htyp ] \u03a3[ \u03c43 \u2208 htyp ] \u03a3[ \u03c44 \u2208 htyp ]\n ((d == d' \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9) \u00d7\n (d' indet) \u00d7\n ((\u03c41 ==> \u03c42) \u2260 (\u03c43 ==> \u03c44)))) +\n (\u03a3[ d' \u2208 dhexp ]\n ((\u03c41 == \u2987\u2988) \u00d7\n (\u03c42 == \u2987\u2988) \u00d7\n (d == (d' \u27e8 \u2987\u2988 \u21d2 \u2987\u2988 ==> \u2987\u2988 \u27e9)) \u00d7\n (d' indet) \u00d7\n ((d'' : dhexp) (\u03c4' : htyp) \u2192 d' \u2260 (d'' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9)))))\n canonical-indeterminate-forms-arr (TAVar x\u2081) ()\n canonical-indeterminate-forms-arr (TALam wt) ()\n canonical-indeterminate-forms-arr (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-arr (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-arr (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x ))\n canonical-indeterminate-forms-arr (TACast wt x) (ICastArr x\u2081 ind) = Inr (Inr (Inr (Inl (_ , _ , _ , _ , _ , refl , ind , x\u2081))))\n -- nb: this is the only one that required pattern matching (or equivalently a lemma i didn't bother to state) on premises\n canonical-indeterminate-forms-arr (TACast wt TCHole2) (ICastHoleGround x\u2081 ind GHole) = Inr (Inr (Inr (Inr (_ , refl , refl , refl , ind , x\u2081) )))\n\n canonical-indeterminate-forms-hole : \u2200{\u0394 d} \u2192\n \u0394 , \u2205 \u22a2 d :: \u2987\u2988 \u2192\n d indet \u2192\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] \u2987\u2988) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] \u2987\u2988) \u2208 \u0394))) +\n (\u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c42 \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: (\u03c42 ==> \u2987\u2988)) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c42) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))) +\n (\u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ]\n ((d == d' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9) \u00d7\n (\u03c4' ground) \u00d7\n (d indet))) --todo: this is\n --interesting; it's\n --the only clause\n --that's filled by\n --multiple patterns\n --below; maybe we\n --could say something\n --more specific?\n canonical-indeterminate-forms-hole (TAVar x\u2081) ()\n canonical-indeterminate-forms-hole (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-hole (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-hole (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x ))\n canonical-indeterminate-forms-hole (TACast wt x) (ICastGroundHole x\u2081 ind) = Inr (Inr (Inr (_ , _ , refl , x\u2081 , ICastGroundHole x\u2081 ind)))\n canonical-indeterminate-forms-hole (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) = Inr (Inr (Inr (_ , _ , refl , x\u2082 , ICastGroundHole x\u2082 ind)))\n\n canonical-indeterminate-forms-coverage : \u2200{\u0394 d \u03c4} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d indet \u2192\n \u03c4 \u2260 b \u2192\n ((\u03c41 : htyp) (\u03c42 : htyp) \u2192 \u03c4 \u2260 (\u03c41 ==> \u03c42)) \u2192\n \u03c4 \u2260 \u2987\u2988 \u2192\n \u22a5\n canonical-indeterminate-forms-coverage TAConst () nb na nh\n canonical-indeterminate-forms-coverage (TAVar x\u2081) () nb na nh\n canonical-indeterminate-forms-coverage (TALam wt) () nb na nh\n canonical-indeterminate-forms-coverage (TAAp wt wt\u2081) (IAp x ind x\u2081) nb na nh = {!!}\n canonical-indeterminate-forms-coverage (TAEHole x x\u2081) IEHole nb na nh = {!!}\n canonical-indeterminate-forms-coverage (TANEHole x wt x\u2081) (INEHole x\u2082) nb na nh = {!!}\n canonical-indeterminate-forms-coverage (TACast wt x) (ICastArr x\u2081 ind) nb na nh = na _ _ refl\n canonical-indeterminate-forms-coverage (TACast wt x) (ICastGroundHole x\u2081 ind) nb na nh = nh refl\n canonical-indeterminate-forms-coverage (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) nb na nh = {!!}\n","old_contents":"open import Nat\nopen import Prelude\nopen import contexts\nopen import core\nopen import type-assignment-unicity\n\nmodule canonical-indeterminate-forms where\n canonical-indeterminate-forms-base : \u2200{\u0394 d} \u2192\n \u0394 , \u2205 \u22a2 d :: b \u2192\n d indet \u2192\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] b) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] b) \u2208 \u0394))) +\n \u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c42 \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: \u03c42 ==> b) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c42) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))\n +\n (\u03a3[ d' \u2208 dhexp ]\n ((d == d' \u27e8 \u2987\u2988 \u21d2 b \u27e9) \u00d7\n (d' indet) \u00d7\n ((d'' : dhexp) (\u03c4' : htyp) \u2192 d' \u2260 (d'' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9))))\n canonical-indeterminate-forms-base TAConst ()\n canonical-indeterminate-forms-base (TAVar x\u2081) ()\n canonical-indeterminate-forms-base (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-base (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-base (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x))\n canonical-indeterminate-forms-base (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) = Inr (Inr (Inr (_ , refl , ind , x\u2081)))\n\n canonical-indeterminate-forms-arr : \u2200{\u0394 d \u03c41 \u03c42 } \u2192\n \u0394 , \u2205 \u22a2 d :: (\u03c41 ==> \u03c42) \u2192\n d indet \u2192\n {!!} +\n {!!} +\n {!!} +\n {!!} +\n {!!}\n canonical-indeterminate-forms-arr wt ind = {!!}\n\n canonical-indeterminate-forms-hole : \u2200{\u0394 d} \u2192\n \u0394 , \u2205 \u22a2 d :: \u2987\u2988 \u2192\n d indet \u2192\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] \u2987\u2988) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] \u2987\u2988) \u2208 \u0394))) +\n (\u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c42 \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: (\u03c42 ==> \u2987\u2988)) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c42) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))) +\n {!!}\n canonical-indeterminate-forms-hole (TAVar x\u2081) ()\n canonical-indeterminate-forms-hole (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-hole (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-hole (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x ))\n canonical-indeterminate-forms-hole (TACast wt x) (ICastGroundHole x\u2081 ind) = {!!}\n canonical-indeterminate-forms-hole (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) = {!!}\n\n canonical-indeterminate-forms-coverage : \u2200{\u0394 d \u03c4} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d indet \u2192\n \u03c4 \u2260 b \u2192\n ((\u03c41 : htyp) (\u03c42 : htyp) \u2192 \u03c4 \u2260 (\u03c41 ==> \u03c42)) \u2192\n \u03c4 \u2260 \u2987\u2988 \u2192\n \u22a5\n canonical-indeterminate-forms-coverage TAConst () nb na nh\n canonical-indeterminate-forms-coverage (TAVar x\u2081) () nb na nh\n canonical-indeterminate-forms-coverage (TALam wt) () nb na nh\n canonical-indeterminate-forms-coverage (TAAp wt wt\u2081) (IAp x ind x\u2081) nb na nh = {!!}\n canonical-indeterminate-forms-coverage (TAEHole x x\u2081) IEHole nb na nh = {!!}\n canonical-indeterminate-forms-coverage (TANEHole x wt x\u2081) (INEHole x\u2082) nb na nh = {!!}\n canonical-indeterminate-forms-coverage (TACast wt x) (ICastArr x\u2081 ind) nb na nh = na _ _ refl\n canonical-indeterminate-forms-coverage (TACast wt x) (ICastGroundHole x\u2081 ind) nb na nh = nh refl\n canonical-indeterminate-forms-coverage (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) nb na nh = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c85f33967d277406efd4e4db2010a9b1b64173b1","subject":"Drop dead code","message":"Drop dead code\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Algebra.agda","new_file":"Base\/Change\/Algebra.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Change Structures.\n--\n-- This module defines the notion of change structures,\n-- as well as change structures for groups, functions and lists.\n--\n-- This module corresponds to Section 2 of the PLDI paper.\n------------------------------------------------------------------------\n\nmodule Base.Change.Algebra where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Change Algebras\n-- ===============\n--\n-- In the paper, change algebras are called \"change structures\"\n-- and they are described in Section 2.1. We follow the design of\n-- the Agda standard library and define change algebras as two\n-- records, so Definition 2.1 from the PLDI paper maps to the\n-- records IsChangeAlgebra and ChangeAlgebra.\n--\n-- A value of type (IsChangeAlgebra Change update diff) proves that\n-- Change, update and diff together form a change algebra.\n--\n-- A value of type (ChangeAlgebra Carrier) contains the necessary\n-- ingredients to create a change algebra for a carrier set.\n--\n-- In the paper, Carrier is called V (Def. 2.1a),\n-- Change is called \u0394 (Def. 2.1b),\n-- update is written as infix \u2295 (Def. 2.1c),\n-- diff is written as infix \u229d (Def. 2.1d),\n-- and update-diff is specified in Def. 2.1e.\n\nrecord IsChangeAlgebra\n {c} {d}\n {Carrier : Set c}\n (Change : Carrier \u2192 Set d)\n (update : (v : Carrier) (dv : Change v) \u2192 Carrier)\n (diff : (u v : Carrier) \u2192 Change v)\n (nil : (u : Carrier) \u2192 Change u) : Set (c \u2294 d) where\n field\n update-diff : \u2200 u v \u2192 update v (diff u v) \u2261 u\n -- This corresponds to Lemma 2.3 from the paper.\n update-nil : \u2200 v \u2192 update v (nil v) \u2261 v\n\n -- abbreviations\n before : \u2200 {v} \u2192 Change v \u2192 Carrier\n before {v} _ = v\n\n after : \u2200 {v} \u2192 Change v \u2192 Carrier\n after {v} dv = update v dv\n\nrecord ChangeAlgebra {c} \u2113\n (Carrier : Set c) : Set (c \u2294 suc \u2113) where\n field\n Change : Carrier \u2192 Set \u2113\n update : (v : Carrier) (dv : Change v) \u2192 Carrier\n diff : (u v : Carrier) \u2192 Change v\n\n -- This generalizes Def. 2.2. from the paper.\n nil : (u : Carrier) \u2192 Change u\n\n isChangeAlgebra : IsChangeAlgebra Change update diff nil\n\n\n infixl 6 update diff\n\n open IsChangeAlgebra isChangeAlgebra public\n\n-- The following open ... public statement lets us use infix \u229e\n-- and \u229f for update and diff. In the paper, we use infix \u2295 and\n-- \u229d.\n\nopen ChangeAlgebra {{...}} public\n using\n ( update-diff\n ; update-nil\n ; nil\n ; before\n ; after\n )\n renaming\n ( Change to \u0394\n ; update to _\u229e_\n ; diff to _\u229f_\n )\n\n-- Families of Change Algebras\n-- ===========================\n--\n-- This is some Agda machinery to allow subscripting change\n-- algebra operations to avoid ambiguity. In the paper,\n-- we simply write (in the paragraph after Def. 2.1):\n--\n-- We overload operators \u2206, \u229d and \u2295 to refer to the\n-- corresponding operations of different change structures; we\n-- will subscript these symbols when needed to prevent\n-- ambiguity.\n--\n-- The following definitions implement this idea formally.\n\nrecord ChangeAlgebraFamily {a p} \u2113 {A : Set a} (P : A \u2192 Set p): Set (suc \u2113 \u2294 p \u2294 a) where\n constructor\n family\n field\n change-algebra : \u2200 x \u2192 ChangeAlgebra \u2113 (P x)\n module _ x where\n open ChangeAlgebra (change-algebra x) public\n\nmodule Family = ChangeAlgebraFamily {{...}}\n\nopen Family public\n using\n (\n )\n renaming\n ( Change to \u0394\u208d_\u208e\n ; nil to nil\u208d_\u208e\n ; update-diff to update-diff\u208d_\u208e\n ; update-nil to update-nil\u208d_\u208e\n ; change-algebra to change-algebra\u208d_\u208e\n ; before to before\u208d_\u208e\n ; after to after\u208d_\u208e\n )\n\n-- XXX not clear this is ever used\ninstance\n change-algebra-family-inst = change-algebra\u208d_\u208e\n\ninfixl 6 update\u2032 diff\u2032\n\nupdate\u2032 = Family.update\nsyntax update\u2032 x v dv = v \u229e\u208d x \u208e dv\n\ndiff\u2032 = Family.diff\nsyntax diff\u2032 x u v = u \u229f\u208d x \u208e v\n\n-- Derivatives\n-- ===========\n--\n-- This corresponds to Def. 2.4 in the paper.\n\nRawChange : \u2200 {a b c d} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra c A}} \u2192\n {{CB : ChangeAlgebra d B}} \u2192\n (f : A \u2192 B) \u2192\n Set (a \u2294 c \u2294 d)\nRawChange f = \u2200 a (da : \u0394 a) \u2192 \u0394 (f a)\n\nIsDerivative : \u2200 {a b c d} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra c A}} \u2192\n {{CB : ChangeAlgebra d B}} \u2192\n (f : A \u2192 B) \u2192\n (df : RawChange f) \u2192\n Set (a \u2294 b \u2294 c)\nIsDerivative f df = \u2200 a da \u2192 f a \u229e df a da \u2261 f (a \u229e da)\n\n-- This is a variant of IsDerivative for change algebra families.\nRawChange\u208d_,_\u208e : \u2200 {a b p q c d} {A : Set a} {B : Set b} {P : A \u2192 Set p} {Q : B \u2192 Set q} \u2192\n {{CP : ChangeAlgebraFamily c P}} \u2192\n {{CQ : ChangeAlgebraFamily d Q}} \u2192\n (x : A) \u2192\n (y : B) \u2192\n (f : P x \u2192 Q y) \u2192 Set (c \u2294 d \u2294 p)\nRawChange\u208d_,_\u208e x y f = \u2200 px (dpx : \u0394\u208d_\u208e x px) \u2192 \u0394\u208d_\u208e y (f px)\n\nIsDerivative\u208d_,_\u208e : \u2200 {a b p q c d} {A : Set a} {B : Set b} {P : A \u2192 Set p} {Q : B \u2192 Set q} \u2192\n {{CP : ChangeAlgebraFamily c P}} \u2192\n {{CQ : ChangeAlgebraFamily d Q}} \u2192\n (x : A) \u2192\n (y : B) \u2192\n (f : P x \u2192 Q y) \u2192\n (df : RawChange\u208d_,_\u208e x y f) \u2192\n Set (p \u2294 q \u2294 c)\nIsDerivative\u208d_,_\u208e {P = P} {{CP}} {{CQ}} x y f df = IsDerivative {{change-algebra\u208d _ \u208e}} {{change-algebra\u208d _ \u208e}} f df where\n CPx = change-algebra\u208d_\u208e {{CP}} x\n CQy = change-algebra\u208d_\u208e {{CQ}} y\n\n-- Lemma 2.5 appears in Base.Change.Equivalence.\n\n-- Abelian Groups Induce Change Algebras\n-- =====================================\n--\n-- In the paper, as the first example for change structures after\n-- Def. 2.1, we mention that \"each abelian group ... induces a\n-- change structure\". This is the formalization of this result.\n--\n-- The module GroupChanges below takes a proof that A forms an\n-- abelian group and provides a changeAlgebra for A. The proof of\n-- Def 2.1e is by equational reasoning using the group axioms, in\n-- the definition of changeAlgebra.isChangeAlgebra.update-diff.\n\nopen import Algebra.Structures\nopen import Data.Product\nopen import Function\n\nmodule GroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isAbelianGroup : IsAbelianGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsAbelianGroup isAbelianGroup\n hiding\n ( refl\n )\n renaming\n ( _-_ to _\u229d_\n ; \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n changeAlgebra : ChangeAlgebra a A\n changeAlgebra = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; nil = const \u03b5\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8 comm _ _ \u27e9\n (u \u229d v ) \u2295 v\n \u2261\u27e8\u27e9\n (u \u2295 (v \u207b\u00b9)) \u2295 v\n \u2261\u27e8 assoc _ _ _ \u27e9\n u \u2295 ((v \u207b\u00b9) \u2295 v)\n \u2261\u27e8 refl \u27e8\u2295\u27e9 proj\u2081 inverse v \u27e9\n u \u2295 \u03b5\n \u2261\u27e8 proj\u2082 identity u \u27e9\n u\n \u220e\n ; update-nil = proj\u2082 identity\n }\n }\n\n-- Function Changes\n-- ================\n--\n-- This is one of our most important results: Change structures\n-- can be lifted to function spaces. We formalize this as a module\n-- FunctionChanges that takes the change algebras for A and B as\n-- arguments, and provides a changeAlgebra for (A \u2192 B). The proofs\n-- are by equational reasoning using 2.1e for A and B.\n\nmodule FunctionChanges\n {a} {b} {c} {d} (A : Set a) (B : Set b) {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}}\n where\n -- This corresponds to Definition 2.6 in the paper.\n record FunctionChange (f : A \u2192 B) : Set (a \u2294 b \u2294 c \u2294 d) where\n field\n -- Definition 2.6a\n apply : RawChange f\n\n -- Definition 2.6b.\n -- (for some reason, the version in the paper has the arguments of \u2261\n -- flipped. Since \u2261 is symmetric, this doesn't matter).\n correct : (a : A) (da : \u0394 a) \u2192\n f (a \u229e da) \u229e apply (a \u229e da) (nil (a \u229e da)) \u2261 f a \u229e apply a da\n\n open FunctionChange public\n open \u2261-Reasoning\n open import Postulate.Extensionality\n\n funDiff : (g f : A \u2192 B) \u2192 FunctionChange f\n funDiff = \u03bb g f \u2192 record\n { apply = \u03bb a da \u2192 g (a \u229e da) \u229f f a\n -- the proof of correct is the first half of what we\n -- have to prove for Theorem 2.7.\n ; correct = \u03bb a da \u2192\n begin\n _\u229e_ {{CB}} (f (_\u229e_ {{CA}} a da)) (g (_\u229e_ {{CA}} (a \u229e da) (nil (a \u229e da))) \u229f f (a \u229e da))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 _\u229e_ {{CB}} (f (a \u229e da)) (g \u25a1 \u229f f (a \u229e da)))\n (update-nil {{CA}} (a \u229e da)) \u27e9\n _\u229e_ {{CB}} (f (a \u229e da)) (g (a \u229e da) \u229f f (a \u229e da))\n \u2261\u27e8 update-diff {{CB}} (g (a \u229e da)) (f (a \u229e da)) \u27e9\n g (a \u229e da)\n \u2261\u27e8 sym (update-diff {{CB}} (g (a \u229e da)) (f a)) \u27e9\n _\u229e_ {{CB}} (f a) (g (a \u229e da) \u229f f a)\n \u220e\n }\n\n funUpdate : \u2200 (f : A \u2192 B) (df : FunctionChange f) \u2192 A \u2192 B\n funUpdate = \u03bb f df a \u2192 f a \u229e apply df a (nil a)\n funNil = \u03bb f \u2192 funDiff f f\n\n mutual\n -- I have to write the type of funUpdateDiff without using changeAlgebra,\n -- so I just use the underlying implementations.\n funUpdateDiff : \u2200 u v \u2192 funUpdate v (funDiff u v) \u2261 u\n instance\n changeAlgebra : ChangeAlgebra (a \u2294 b \u2294 c \u2294 d) (A \u2192 B)\n\n changeAlgebra = record\n { Change = FunctionChange\n -- in the paper, update and diff below are in Def. 2.7\n ; update = funUpdate\n ; diff = funDiff\n ; nil = funNil\n ; isChangeAlgebra = record\n -- the proof of update-diff is the second half of what\n -- we have to prove for Theorem 2.8.\n { update-diff = funUpdateDiff\n ; update-nil = \u03bb v \u2192 funUpdateDiff v v\n }\n }\n -- XXX remove mutual recursion by inlining the algebra in here?\n funUpdateDiff = \u03bb g f \u2192 ext (\u03bb a \u2192\n begin\n _\u229e_ {{CB}} (f a) (g (_\u229e_ {{CA}} a (nil a)) \u229f f a)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 _\u229e_ {{CB}} (f a) (g \u25a1 \u229f f a)) (update-nil {{CA}} a) \u27e9\n _\u229e_ {{CB}} (f a) (g a \u229f f a)\n \u2261\u27e8 update-diff {{CB}} (g a) (f a) \u27e9\n g a\n \u220e)\n\n -- This is Theorem 2.9 in the paper.\n incrementalization : \u2200 (f : A \u2192 B) df a da \u2192\n (f \u229e df) (a \u229e da) \u2261 f a \u229e apply df a da\n incrementalization f df a da = correct df a da\n\n -- This is Theorem 2.10 in the paper. However, the derivative of f is just\n -- the apply component of `nil f`, not the full `nil f`, which also includes\n -- a proof. This is not an issue in the paper, which is formulated in a\n -- proof-irrelevant metalanguage.\n nil-is-derivative : \u2200 (f : A \u2192 B) \u2192\n IsDerivative f (apply (nil f))\n nil-is-derivative f a da =\n begin\n f a \u229e apply (nil f) a da\n \u2261\u27e8 sym (incrementalization f (nil f) a da) \u27e9\n (f \u229e nil {{changeAlgebra}} f) (a \u229e da)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (a \u229e da))\n (update-nil {{changeAlgebra}} f) \u27e9\n f (a \u229e da)\n \u220e\n\n -- Show that any derivative is a valid function change.\n\n -- In the paper, this is never actually stated. We just prove that nil\n -- changes are derivatives; the paper keeps talking about \"the derivative\",\n -- suggesting derivatives are unique. If derivatives were unique, we could\n -- say that the nil change is *the* derivative, hence the derivative is the\n -- nil change (hence also a change).\n --\n -- In fact, derivatives are only unique up to change equivalence and\n -- extensional equality; this is proven in Base.Change.Equivalence.derivative-unique.\n --\n Derivative-is-valid : \u2200 {f : A \u2192 B} df (IsDerivative-f-df : IsDerivative f df) a da \u2192\n f (a \u229e da) \u229e (df (a \u229e da) (nil (a \u229e da))) \u2261 f a \u229e df a da\n Derivative-is-valid {f} df IsDerivative-f-df a da rewrite IsDerivative-f-df (a \u229e da) (nil (a \u229e da)) | update-nil (a \u229e da) = sym (IsDerivative-f-df a da)\n\n DerivativeAsChange : \u2200 {f : A \u2192 B} {df} (IsDerivative-f-df : IsDerivative f df) \u2192 \u0394 f\n DerivativeAsChange {df = df} IsDerivative-f-df = record { apply = df ; correct = Derivative-is-valid df IsDerivative-f-df }\n -- In Equivalence.agda, derivative-is-nil-alternative then proves that a derivative is also a nil change.\n\n\n-- List (== Environment) Changes\n-- =============================\n--\n-- Here, we define a change structure on environments, given a\n-- change structure on the values in the environments. In the\n-- paper, we describe this in Definition 3.5. But note that this\n-- Agda formalization uses de Bruijn indices instead of names, so\n-- environments are just lists. Therefore, when we use Definition\n-- 3.5 in the paper, in this formalization, we use the list-like\n-- change structure defined here.\n\nopen import Data.List\nopen import Data.List.All\n\ndata All\u2032 {a p q} {A : Set a}\n {P : A \u2192 Set p}\n (Q : {x : A} \u2192 P x \u2192 Set q)\n : {xs : List A} (pxs : All P xs) \u2192 Set (p \u2294 q \u2294 a) where\n [] : All\u2032 Q []\n _\u2237_ : \u2200 {x xs} {px : P x} {pxs : All P xs} (qx : Q px) (qxs : All\u2032 Q pxs) \u2192 All\u2032 Q (px \u2237 pxs)\n\nmodule ListChanges\n {a} {c} {A : Set a} (P : A \u2192 Set) {{C : ChangeAlgebraFamily c P}}\n where\n update-all : \u2200 {xs} \u2192 (pxs : All P xs) \u2192 All\u2032 (\u0394\u208d_\u208e {{C}} _) pxs \u2192 All P xs\n update-all {[]} [] [] = []\n update-all {x \u2237 xs} (px \u2237 pxs) (dpx \u2237 dpxs) = (px \u229e\u208d x \u208e dpx) \u2237 update-all pxs dpxs\n\n diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 All\u2032 (\u0394\u208d_\u208e {{C}} _) pxs\n diff-all [] [] = []\n diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = (px\u2032 \u229f\u208d _ \u208e px) \u2237 diff-all pxs\u2032 pxs\n\n update-diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 update-all pxs (diff-all pxs\u2032 pxs) \u2261 pxs\u2032\n update-diff-all [] [] = refl\n update-diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = cong\u2082 _\u2237_ (update-diff\u208d_\u208e {{C}} _ px\u2032 px) (update-diff-all pxs\u2032 pxs)\n\n instance\n changeAlgebra : ChangeAlgebraFamily (c \u2294 a) (All P)\n\n changeAlgebra = record\n { change-algebra = \u03bb xs \u2192 record\n { Change = All\u2032 (\u0394\u208d_\u208e {{C}} _)\n ; update = update-all\n ; diff = diff-all\n ; nil = \u03bb xs \u2192 diff-all xs xs\n ; isChangeAlgebra = record\n { update-diff = update-diff-all\n ; update-nil = \u03bb xs\u2081 \u2192 update-diff-all xs\u2081 xs\u2081\n }\n }\n }\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Change Structures.\n--\n-- This module defines the notion of change structures,\n-- as well as change structures for groups, functions and lists.\n--\n-- This module corresponds to Section 2 of the PLDI paper.\n------------------------------------------------------------------------\n\nmodule Base.Change.Algebra where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Change Algebras\n-- ===============\n--\n-- In the paper, change algebras are called \"change structures\"\n-- and they are described in Section 2.1. We follow the design of\n-- the Agda standard library and define change algebras as two\n-- records, so Definition 2.1 from the PLDI paper maps to the\n-- records IsChangeAlgebra and ChangeAlgebra.\n--\n-- A value of type (IsChangeAlgebra Change update diff) proves that\n-- Change, update and diff together form a change algebra.\n--\n-- A value of type (ChangeAlgebra Carrier) contains the necessary\n-- ingredients to create a change algebra for a carrier set.\n--\n-- In the paper, Carrier is called V (Def. 2.1a),\n-- Change is called \u0394 (Def. 2.1b),\n-- update is written as infix \u2295 (Def. 2.1c),\n-- diff is written as infix \u229d (Def. 2.1d),\n-- and update-diff is specified in Def. 2.1e.\n\nrecord IsChangeAlgebra\n {c} {d}\n {Carrier : Set c}\n (Change : Carrier \u2192 Set d)\n (update : (v : Carrier) (dv : Change v) \u2192 Carrier)\n (diff : (u v : Carrier) \u2192 Change v)\n (nil : (u : Carrier) \u2192 Change u) : Set (c \u2294 d) where\n field\n update-diff : \u2200 u v \u2192 update v (diff u v) \u2261 u\n -- This corresponds to Lemma 2.3 from the paper.\n update-nil : \u2200 v \u2192 update v (nil v) \u2261 v\n\n -- abbreviations\n before : \u2200 {v} \u2192 Change v \u2192 Carrier\n before {v} _ = v\n\n after : \u2200 {v} \u2192 Change v \u2192 Carrier\n after {v} dv = update v dv\n\nrecord ChangeAlgebra {c} \u2113\n (Carrier : Set c) : Set (c \u2294 suc \u2113) where\n field\n Change : Carrier \u2192 Set \u2113\n update : (v : Carrier) (dv : Change v) \u2192 Carrier\n diff : (u v : Carrier) \u2192 Change v\n\n -- This generalizes Def. 2.2. from the paper.\n nil : (u : Carrier) \u2192 Change u\n\n isChangeAlgebra : IsChangeAlgebra Change update diff nil\n\n\n infixl 6 update diff\n\n open IsChangeAlgebra isChangeAlgebra public\n\n-- The following open ... public statement lets us use infix \u229e\n-- and \u229f for update and diff. In the paper, we use infix \u2295 and\n-- \u229d.\n\nopen ChangeAlgebra {{...}} public\n using\n ( update-diff\n ; update-nil\n ; nil\n ; before\n ; after\n )\n renaming\n ( Change to \u0394\n ; update to _\u229e_\n ; diff to _\u229f_\n )\n\n-- Families of Change Algebras\n-- ===========================\n--\n-- This is some Agda machinery to allow subscripting change\n-- algebra operations to avoid ambiguity. In the paper,\n-- we simply write (in the paragraph after Def. 2.1):\n--\n-- We overload operators \u2206, \u229d and \u2295 to refer to the\n-- corresponding operations of different change structures; we\n-- will subscript these symbols when needed to prevent\n-- ambiguity.\n--\n-- The following definitions implement this idea formally.\n\nrecord ChangeAlgebraFamily {a p} \u2113 {A : Set a} (P : A \u2192 Set p): Set (suc \u2113 \u2294 p \u2294 a) where\n constructor\n family\n field\n change-algebra : \u2200 x \u2192 ChangeAlgebra \u2113 (P x)\n\n {-\n instance\n change-algebra-extractor : \u2200 {x} \u2192 ChangeAlgebra \u2113 (P x)\n change-algebra-extractor {x} = change-algebra x\n -}\n module _ x where\n open ChangeAlgebra (change-algebra x) public\n\nmodule Family = ChangeAlgebraFamily {{...}}\n\nopen Family public\n using\n (\n )\n renaming\n ( Change to \u0394\u208d_\u208e\n ; nil to nil\u208d_\u208e\n ; update-diff to update-diff\u208d_\u208e\n ; update-nil to update-nil\u208d_\u208e\n ; change-algebra to change-algebra\u208d_\u208e\n ; before to before\u208d_\u208e\n ; after to after\u208d_\u208e\n )\n\n-- XXX not clear this is ever used\ninstance\n change-algebra-family-inst = change-algebra\u208d_\u208e\n\ninfixl 6 update\u2032 diff\u2032\n\nupdate\u2032 = Family.update\nsyntax update\u2032 x v dv = v \u229e\u208d x \u208e dv\n\ndiff\u2032 = Family.diff\nsyntax diff\u2032 x u v = u \u229f\u208d x \u208e v\n\n-- Derivatives\n-- ===========\n--\n-- This corresponds to Def. 2.4 in the paper.\n\nRawChange : \u2200 {a b c d} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra c A}} \u2192\n {{CB : ChangeAlgebra d B}} \u2192\n (f : A \u2192 B) \u2192\n Set (a \u2294 c \u2294 d)\nRawChange f = \u2200 a (da : \u0394 a) \u2192 \u0394 (f a)\n\nIsDerivative : \u2200 {a b c d} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra c A}} \u2192\n {{CB : ChangeAlgebra d B}} \u2192\n (f : A \u2192 B) \u2192\n (df : RawChange f) \u2192\n Set (a \u2294 b \u2294 c)\nIsDerivative f df = \u2200 a da \u2192 f a \u229e df a da \u2261 f (a \u229e da)\n\n-- This is a variant of IsDerivative for change algebra families.\nRawChange\u208d_,_\u208e : \u2200 {a b p q c d} {A : Set a} {B : Set b} {P : A \u2192 Set p} {Q : B \u2192 Set q} \u2192\n {{CP : ChangeAlgebraFamily c P}} \u2192\n {{CQ : ChangeAlgebraFamily d Q}} \u2192\n (x : A) \u2192\n (y : B) \u2192\n (f : P x \u2192 Q y) \u2192 Set (c \u2294 d \u2294 p)\nRawChange\u208d_,_\u208e x y f = \u2200 px (dpx : \u0394\u208d_\u208e x px) \u2192 \u0394\u208d_\u208e y (f px)\n\nIsDerivative\u208d_,_\u208e : \u2200 {a b p q c d} {A : Set a} {B : Set b} {P : A \u2192 Set p} {Q : B \u2192 Set q} \u2192\n {{CP : ChangeAlgebraFamily c P}} \u2192\n {{CQ : ChangeAlgebraFamily d Q}} \u2192\n (x : A) \u2192\n (y : B) \u2192\n (f : P x \u2192 Q y) \u2192\n (df : RawChange\u208d_,_\u208e x y f) \u2192\n Set (p \u2294 q \u2294 c)\nIsDerivative\u208d_,_\u208e {P = P} {{CP}} {{CQ}} x y f df = IsDerivative {{change-algebra\u208d _ \u208e}} {{change-algebra\u208d _ \u208e}} f df where\n CPx = change-algebra\u208d_\u208e {{CP}} x\n CQy = change-algebra\u208d_\u208e {{CQ}} y\n\n-- Lemma 2.5 appears in Base.Change.Equivalence.\n\n-- Abelian Groups Induce Change Algebras\n-- =====================================\n--\n-- In the paper, as the first example for change structures after\n-- Def. 2.1, we mention that \"each abelian group ... induces a\n-- change structure\". This is the formalization of this result.\n--\n-- The module GroupChanges below takes a proof that A forms an\n-- abelian group and provides a changeAlgebra for A. The proof of\n-- Def 2.1e is by equational reasoning using the group axioms, in\n-- the definition of changeAlgebra.isChangeAlgebra.update-diff.\n\nopen import Algebra.Structures\nopen import Data.Product\nopen import Function\n\nmodule GroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isAbelianGroup : IsAbelianGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsAbelianGroup isAbelianGroup\n hiding\n ( refl\n )\n renaming\n ( _-_ to _\u229d_\n ; \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n changeAlgebra : ChangeAlgebra a A\n changeAlgebra = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; nil = const \u03b5\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8 comm _ _ \u27e9\n (u \u229d v ) \u2295 v\n \u2261\u27e8\u27e9\n (u \u2295 (v \u207b\u00b9)) \u2295 v\n \u2261\u27e8 assoc _ _ _ \u27e9\n u \u2295 ((v \u207b\u00b9) \u2295 v)\n \u2261\u27e8 refl \u27e8\u2295\u27e9 proj\u2081 inverse v \u27e9\n u \u2295 \u03b5\n \u2261\u27e8 proj\u2082 identity u \u27e9\n u\n \u220e\n ; update-nil = proj\u2082 identity\n }\n }\n\n-- Function Changes\n-- ================\n--\n-- This is one of our most important results: Change structures\n-- can be lifted to function spaces. We formalize this as a module\n-- FunctionChanges that takes the change algebras for A and B as\n-- arguments, and provides a changeAlgebra for (A \u2192 B). The proofs\n-- are by equational reasoning using 2.1e for A and B.\n\nmodule FunctionChanges\n {a} {b} {c} {d} (A : Set a) (B : Set b) {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}}\n where\n -- This corresponds to Definition 2.6 in the paper.\n record FunctionChange (f : A \u2192 B) : Set (a \u2294 b \u2294 c \u2294 d) where\n field\n -- Definition 2.6a\n apply : RawChange f\n\n -- Definition 2.6b.\n -- (for some reason, the version in the paper has the arguments of \u2261\n -- flipped. Since \u2261 is symmetric, this doesn't matter).\n correct : (a : A) (da : \u0394 a) \u2192\n f (a \u229e da) \u229e apply (a \u229e da) (nil (a \u229e da)) \u2261 f a \u229e apply a da\n\n open FunctionChange public\n open \u2261-Reasoning\n open import Postulate.Extensionality\n\n funDiff : (g f : A \u2192 B) \u2192 FunctionChange f\n funDiff = \u03bb g f \u2192 record\n { apply = \u03bb a da \u2192 g (a \u229e da) \u229f f a\n -- the proof of correct is the first half of what we\n -- have to prove for Theorem 2.7.\n ; correct = \u03bb a da \u2192\n begin\n _\u229e_ {{CB}} (f (_\u229e_ {{CA}} a da)) (g (_\u229e_ {{CA}} (a \u229e da) (nil (a \u229e da))) \u229f f (a \u229e da))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 _\u229e_ {{CB}} (f (a \u229e da)) (g \u25a1 \u229f f (a \u229e da)))\n (update-nil {{CA}} (a \u229e da)) \u27e9\n _\u229e_ {{CB}} (f (a \u229e da)) (g (a \u229e da) \u229f f (a \u229e da))\n \u2261\u27e8 update-diff {{CB}} (g (a \u229e da)) (f (a \u229e da)) \u27e9\n g (a \u229e da)\n \u2261\u27e8 sym (update-diff {{CB}} (g (a \u229e da)) (f a)) \u27e9\n _\u229e_ {{CB}} (f a) (g (a \u229e da) \u229f f a)\n \u220e\n }\n\n funUpdate : \u2200 (f : A \u2192 B) (df : FunctionChange f) \u2192 A \u2192 B\n funUpdate = \u03bb f df a \u2192 f a \u229e apply df a (nil a)\n funNil = \u03bb f \u2192 funDiff f f\n\n mutual\n -- I have to write the type of funUpdateDiff without using changeAlgebra,\n -- so I just use the underlying implementations.\n funUpdateDiff : \u2200 u v \u2192 funUpdate v (funDiff u v) \u2261 u\n instance\n changeAlgebra : ChangeAlgebra (a \u2294 b \u2294 c \u2294 d) (A \u2192 B)\n\n changeAlgebra = record\n { Change = FunctionChange\n -- in the paper, update and diff below are in Def. 2.7\n ; update = funUpdate\n ; diff = funDiff\n ; nil = funNil\n ; isChangeAlgebra = record\n -- the proof of update-diff is the second half of what\n -- we have to prove for Theorem 2.8.\n { update-diff = funUpdateDiff\n ; update-nil = \u03bb v \u2192 funUpdateDiff v v\n }\n }\n -- XXX remove mutual recursion by inlining the algebra in here?\n funUpdateDiff = \u03bb g f \u2192 ext (\u03bb a \u2192\n begin\n _\u229e_ {{CB}} (f a) (g (_\u229e_ {{CA}} a (nil a)) \u229f f a)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 _\u229e_ {{CB}} (f a) (g \u25a1 \u229f f a)) (update-nil {{CA}} a) \u27e9\n _\u229e_ {{CB}} (f a) (g a \u229f f a)\n \u2261\u27e8 update-diff {{CB}} (g a) (f a) \u27e9\n g a\n \u220e)\n\n -- This is Theorem 2.9 in the paper.\n incrementalization : \u2200 (f : A \u2192 B) df a da \u2192\n (f \u229e df) (a \u229e da) \u2261 f a \u229e apply df a da\n incrementalization f df a da = correct df a da\n\n -- This is Theorem 2.10 in the paper. However, the derivative of f is just\n -- the apply component of `nil f`, not the full `nil f`, which also includes\n -- a proof. This is not an issue in the paper, which is formulated in a\n -- proof-irrelevant metalanguage.\n nil-is-derivative : \u2200 (f : A \u2192 B) \u2192\n IsDerivative f (apply (nil f))\n nil-is-derivative f a da =\n begin\n f a \u229e apply (nil f) a da\n \u2261\u27e8 sym (incrementalization f (nil f) a da) \u27e9\n (f \u229e nil {{changeAlgebra}} f) (a \u229e da)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (a \u229e da))\n (update-nil {{changeAlgebra}} f) \u27e9\n f (a \u229e da)\n \u220e\n\n -- Show that any derivative is a valid function change.\n\n -- In the paper, this is never actually stated. We just prove that nil\n -- changes are derivatives; the paper keeps talking about \"the derivative\",\n -- suggesting derivatives are unique. If derivatives were unique, we could\n -- say that the nil change is *the* derivative, hence the derivative is the\n -- nil change (hence also a change).\n --\n -- In fact, derivatives are only unique up to change equivalence and\n -- extensional equality; this is proven in Base.Change.Equivalence.derivative-unique.\n --\n Derivative-is-valid : \u2200 {f : A \u2192 B} df (IsDerivative-f-df : IsDerivative f df) a da \u2192\n f (a \u229e da) \u229e (df (a \u229e da) (nil (a \u229e da))) \u2261 f a \u229e df a da\n Derivative-is-valid {f} df IsDerivative-f-df a da rewrite IsDerivative-f-df (a \u229e da) (nil (a \u229e da)) | update-nil (a \u229e da) = sym (IsDerivative-f-df a da)\n\n DerivativeAsChange : \u2200 {f : A \u2192 B} {df} (IsDerivative-f-df : IsDerivative f df) \u2192 \u0394 f\n DerivativeAsChange {df = df} IsDerivative-f-df = record { apply = df ; correct = Derivative-is-valid df IsDerivative-f-df }\n -- In Equivalence.agda, derivative-is-nil-alternative then proves that a derivative is also a nil change.\n\n\n-- List (== Environment) Changes\n-- =============================\n--\n-- Here, we define a change structure on environments, given a\n-- change structure on the values in the environments. In the\n-- paper, we describe this in Definition 3.5. But note that this\n-- Agda formalization uses de Bruijn indices instead of names, so\n-- environments are just lists. Therefore, when we use Definition\n-- 3.5 in the paper, in this formalization, we use the list-like\n-- change structure defined here.\n\nopen import Data.List\nopen import Data.List.All\n\ndata All\u2032 {a p q} {A : Set a}\n {P : A \u2192 Set p}\n (Q : {x : A} \u2192 P x \u2192 Set q)\n : {xs : List A} (pxs : All P xs) \u2192 Set (p \u2294 q \u2294 a) where\n [] : All\u2032 Q []\n _\u2237_ : \u2200 {x xs} {px : P x} {pxs : All P xs} (qx : Q px) (qxs : All\u2032 Q pxs) \u2192 All\u2032 Q (px \u2237 pxs)\n\nmodule ListChanges\n {a} {c} {A : Set a} (P : A \u2192 Set) {{C : ChangeAlgebraFamily c P}}\n where\n update-all : \u2200 {xs} \u2192 (pxs : All P xs) \u2192 All\u2032 (\u0394\u208d_\u208e {{C}} _) pxs \u2192 All P xs\n update-all {[]} [] [] = []\n update-all {x \u2237 xs} (px \u2237 pxs) (dpx \u2237 dpxs) = (px \u229e\u208d x \u208e dpx) \u2237 update-all pxs dpxs\n\n diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 All\u2032 (\u0394\u208d_\u208e {{C}} _) pxs\n diff-all [] [] = []\n diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = (px\u2032 \u229f\u208d _ \u208e px) \u2237 diff-all pxs\u2032 pxs\n\n update-diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 update-all pxs (diff-all pxs\u2032 pxs) \u2261 pxs\u2032\n update-diff-all [] [] = refl\n update-diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = cong\u2082 _\u2237_ (update-diff\u208d_\u208e {{C}} _ px\u2032 px) (update-diff-all pxs\u2032 pxs)\n\n instance\n changeAlgebra : ChangeAlgebraFamily (c \u2294 a) (All P)\n\n changeAlgebra = record\n { change-algebra = \u03bb xs \u2192 record\n { Change = All\u2032 (\u0394\u208d_\u208e {{C}} _)\n ; update = update-all\n ; diff = diff-all\n ; nil = \u03bb xs \u2192 diff-all xs xs\n ; isChangeAlgebra = record\n { update-diff = update-diff-all\n ; update-nil = \u03bb xs\u2081 \u2192 update-diff-all xs\u2081 xs\u2081\n }\n }\n }\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"93fbc39f356356b47218530ff8eaa341187b6d0a","subject":"moar curry = moar sugar","message":"moar curry = moar sugar\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/Examples\/PropositionalDesc.agda","new_file":"formalization\/agda\/Spire\/Examples\/PropositionalDesc.agda","new_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nmodule Spire.Examples.PropositionalDesc where\n\n----------------------------------------------------------------------\n\nelimEq : (A : Set) (x : A) (P : (y : A) \u2192 x \u2261 y \u2192 Set)\n \u2192 P x refl\n \u2192 (y : A) (p : x \u2261 y) \u2192 P y p\nelimEq A .x P prefl x refl = prefl\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nCases : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nCases [] P = \u22a4\nCases (l \u2237 E) P = P here \u00d7 Cases E \u03bb t \u2192 P (there t)\n\ncase : (E : Enum) (P : Tag E \u2192 Set) (cs : Cases E P) (t : Tag E) \u2192 P t\ncase (l \u2237 E) P (c , cs) here = c\ncase (l \u2237 E) P (c , cs) (there t) = case E (\u03bb t \u2192 P (there t)) cs t\n\nUncurriedCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedCases E P X = Cases E P \u2192 X\n\nCurriedCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedCases [] P X = X\nCurriedCases (l \u2237 E) P X = P here \u2192 CurriedCases E (\u03bb t \u2192 P (there t)) X\n\ncurryCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n (f : UncurriedCases E P X) \u2192 CurriedCases E P X\ncurryCases [] P X f = f tt\ncurryCases (l \u2237 E) P X f = \u03bb c \u2192 curryCases E (\u03bb t \u2192 P (there t)) X (\u03bb cs \u2192 f (c , cs))\n\nuncurryCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n (f : CurriedCases E P X) \u2192 UncurriedCases E P X\nuncurryCases [] P X x tt = x\nuncurryCases (l \u2237 E) P X f (c , cs) = uncurryCases E (\u03bb t \u2192 P (there t)) X (f c) cs\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n `End : (i : I) \u2192 Desc I\n `Rec : (i : I) (D : Desc I) \u2192 Desc I\n `Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n `RecFun : (A : Set) (B : A \u2192 I) (D : Desc I) \u2192 Desc I\n\nISet : Set \u2192 Set\u2081\nISet I = I \u2192 Set\n\nEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 ISet I\nEl I (`End j) X i = j \u2261 i\nEl I (`Rec j D) X i = X j \u00d7 El I D X i\nEl I (`Arg A B) X i = \u03a3 A (\u03bb a \u2192 El I (B a) X i)\nEl I (`RecFun A B D) X i = ((a : A) \u2192 X (B a)) \u00d7 El I D X i\n\ndata \u03bc (I : Set) (D : Desc I) (i : I) : Set where\n con : El I D (\u03bc I D) i \u2192 \u03bc I D i\n\nAll : (I : Set) (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El I D X i) \u2192 Set\nAll I (`End j) X P i q = \u22a4\nAll I (`Rec j D) X P i (x , xs) = P j x \u00d7 All I D X P i xs\nAll I (`Arg A B) X P i (a , b) = All I (B a) X P i b\nAll I (`RecFun A B D) X P i (f , xs) = ((a : A) \u2192 P (B a) (f a)) \u00d7 All I D X P i xs\n\ncaseD : (E : Enum) (I : Set) (cs : Cases E (\u03bb _ \u2192 Desc I)) (t : Tag E) \u2192 Desc I\ncaseD E I cs t = case E (\u03bb _ \u2192 Desc I) cs t\n\n----------------------------------------------------------------------\n\nUncurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl I D X = {i : I} \u2192 El I D X i \u2192 X i\n\nCurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl I (`End i) X = X i\nCurriedEl I (`Rec j D) X = (x : X j) \u2192 CurriedEl I D X\nCurriedEl I (`Arg A B) X = (a : A) \u2192 CurriedEl I (B a) X\nCurriedEl I (`RecFun A B D) X = ((a : A) \u2192 X (B a)) \u2192 CurriedEl I D X\n\ncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : UncurriedEl I D X) \u2192 CurriedEl I D X\ncurryEl I (`End i) X cn = cn refl\ncurryEl I (`Rec i D) X cn = \u03bb x \u2192 curryEl I D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl I (`Arg A B) X cn = \u03bb a \u2192 curryEl I (B a) X (\u03bb xs \u2192 cn (a , xs))\ncurryEl I (`RecFun A B D) X cn = \u03bb f \u2192 curryEl I D X (\u03bb xs \u2192 cn (f , xs))\n\nuncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : CurriedEl I D X) \u2192 UncurriedEl I D X\nuncurryEl I (`End i) X cn refl = cn\nuncurryEl I (`Rec i D) X cn (x , xs) = uncurryEl I D X (cn x) xs\nuncurryEl I (`Arg A B) X cn (a , xs) = uncurryEl I (B a) X (cn a) xs\nuncurryEl I (`RecFun A B D) X cn (f , xs) = uncurryEl I D X (cn f) xs\n\ncon2 : (I : Set) (D : Desc I) \u2192 CurriedEl I D (\u03bc I D)\ncon2 I D = curryEl I D (\u03bc I D) con\n\n----------------------------------------------------------------------\n\nUncurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : {i : I} \u2192 El I D X i \u2192 X i)\n \u2192 Set\nUncurriedAll I D X P cn =\n (i : I) (xs : El I D X i) \u2192 All I D X P i xs \u2192 P i (cn xs)\n\nCurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n \u2192 Set\nCurriedAll I (`End i) X P cn =\n P i (cn refl)\nCurriedAll I (`Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedAll I (`Arg A B) X P cn =\n (a : A) \u2192 CurriedAll I (B a) X P (\u03bb xs \u2192 cn (a , xs))\nCurriedAll I (`RecFun A B D) X P cn =\n (f : (a : A) \u2192 X (B a)) (ihf : (a : A) \u2192 P (B a) (f a)) \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (f , xs))\n\ncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : UncurriedAll I D X P cn)\n \u2192 CurriedAll I D X P cn\ncurryAll I (`End i) X P cn pf =\n pf i refl tt\ncurryAll I (`Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryAll I D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryAll I (`Arg A B) X P cn pf =\n \u03bb a \u2192 curryAll I (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\ncurryAll I (`RecFun A B D) X P cn pf =\n \u03bb f ihf \u2192 curryAll I D X P (\u03bb xs \u2192 cn (f , xs)) (\u03bb i xs ihs \u2192 pf i (f , xs) (ihf , ihs))\n\nuncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : CurriedAll I D X P cn)\n \u2192 UncurriedAll I D X P cn\nuncurryAll I (`End .i) X P cn pf i refl tt =\n pf\nuncurryAll I (`Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryAll I (`Arg A B) X P cn pf i (a , xs) ihs =\n uncurryAll I (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\nuncurryAll I (`RecFun A B D) X P cn pf i (f , xs) (ihf , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (f , ys)) (pf f ihf) i xs ihs\n\n----------------------------------------------------------------------\n\nind :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : UncurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\n\nhyps :\n (I : Set)\n (D\u2081 : Desc I)\n (P : (i : I) \u2192 \u03bc I D\u2081 i \u2192 Set)\n (pcon : UncurriedAll I D\u2081 (\u03bc I D\u2081) P con)\n (D\u2082 : Desc I)\n (i : I)\n (xs : El I D\u2082 (\u03bc I D\u2081) i)\n \u2192 All I D\u2082 (\u03bc I D\u2081) P i xs\n\nind I D P pcon i (con xs) = pcon i xs (hyps I D P pcon D i xs)\n\nhyps I D P pcon (`End j) i q = tt\nhyps I D P pcon (`Rec j A) i (x , xs) = ind I D P pcon j x , hyps I D P pcon A i xs\nhyps I D P pcon (`Arg A B) i (a , b) = hyps I D P pcon (B a) i b\nhyps I D P pcon (`RecFun A B E) i (f , xs) = (\u03bb a \u2192 ind I D P pcon (B a) (f a)) , hyps I D P pcon E i xs\n\n----------------------------------------------------------------------\n\nind2 :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : CurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\nind2 I D P pcon i x = ind I D P (uncurryAll I D (\u03bc I D) P con pcon) i x\n\nelim :\n (I : Set)\n (E : Enum)\n (Cs : Tag E \u2192 Desc I)\n \u2192 let D = `Arg (Tag E) Cs in\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n \u2192 UncurriedCases E\n (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))) \n ((i : I) (x : \u03bc I D i) \u2192 P i x)\nelim I E Cs P cs i x =\n let D = `Arg (Tag E) Cs in\n ind2 I D P (case E (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))) cs) i x\n\nelim2 :\n (I : Set)\n (E : Enum)\n (Cs : Tag E \u2192 Desc I)\n \u2192 let D = `Arg (Tag E) Cs in\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n \u2192 let Q = (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs)))\n X = ((i : I) (x : \u03bc I D i) \u2192 P i x)\n in CurriedCases E Q X\nelim2 I E Cs P =\n let D = `Arg (Tag E) Cs\n Q = (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs)))\n X = ((i : I) (x : \u03bc I D i) \u2192 P i x)\n in curryCases E Q X (elim I E Cs P)\n\n----------------------------------------------------------------------\n\nmodule Sugared where\n\n data \u2115T : Set where `zero `suc : \u2115T\n data VecT : Set where `nil `cons : VecT\n\n \u2115D : Desc \u22a4\n \u2115D = `Arg \u2115T \u03bb\n { `zero \u2192 `End tt\n ; `suc \u2192 `Rec tt (`End tt)\n }\n\n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n\n zero : \u2115 tt\n zero = con (`zero , refl)\n\n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (`suc , n , refl)\n\n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg VecT \u03bb\n { `nil \u2192 `End zero\n ; `cons \u2192 `Arg (\u2115 tt) \u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n))\n }\n\n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n\n nil : (A : Set) \u2192 Vec A zero\n nil A = con (`nil , refl)\n\n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (`cons , n , x , xs , refl)\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 n\n ; tt (`suc , m , q) (ih , tt) n \u2192 suc (ih n)\n }\n )\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 zero\n ; tt (`suc , m , q) (ih , tt) n \u2192 add n (ih n)\n }\n )\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) ih n ys \u2192 ys\n ; .(con (`suc , m , refl)) (`cons , m , x , xs , refl) (ih , tt) n ys \u2192 cons A (add m n) x (ih n ys)\n }\n )\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) tt \u2192 nil A\n ; .(con (`suc , n , refl)) (`cons , n , xs , xss , refl) (ih , tt) \u2192 append A m xs (mult n m) ih\n }\n )\n\n----------------------------------------------------------------------\n\nmodule Desugared where\n\n \u2115T : Enum\n \u2115T = \"zero\" \u2237 \"suc\" \u2237 []\n \n VecT : Enum\n VecT = \"nil\" \u2237 \"cons\" \u2237 []\n \n \u2115C : Tag \u2115T \u2192 Desc \u22a4\n \u2115C = caseD \u2115T \u22a4\n ( `End tt\n , `Rec tt (`End tt)\n , tt\n )\n \n \u2115D : Desc \u22a4\n \u2115D = `Arg (Tag \u2115T) \u2115C\n \n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n \n zero : \u2115 tt\n zero = con (here , refl)\n \n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (there here , n , refl)\n \n zero2 : \u2115 tt\n zero2 = con2 \u22a4 \u2115D here\n\n suc2 : \u2115 tt \u2192 \u2115 tt\n suc2 = con2 \u22a4 \u2115D (there here)\n\n VecC : (A : Set) \u2192 Tag VecT \u2192 Desc (\u2115 tt)\n VecC A = caseD VecT (\u2115 tt)\n ( `End zero\n , `Arg (\u2115 tt) (\u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n)))\n , tt\n )\n \n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg (Tag VecT) (VecC A)\n \n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n \n nil : (A : Set) \u2192 Vec A zero\n nil A = con (here , refl)\n \n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (there here , n , x , xs , refl)\n \n nil2 : (A : Set) \u2192 Vec A zero\n nil2 A = con2 (\u2115 tt) (VecD A) here\n\n cons2 : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons2 A = con2 (\u2115 tt) (VecD A) (there here)\n\n----------------------------------------------------------------------\n\n module Induction where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 n)\n , (\u03bb m,q ih,tt n \u2192 suc (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 zero)\n , (\u03bb m,q ih,tt n \u2192 add n (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb m t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) m)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)) m (t , c))\n (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n )\n ( (\u03bb q ih n ys \u2192 subst (\u03bb m \u2192 Vec A (add m n)) q ys)\n , (\u03bb m',x,xs,q ih,tt n ys \u2192\n let m' = proj\u2081 m',x,xs,q\n x = proj\u2081 (proj\u2082 m',x,xs,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 m',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb m \u2192 Vec A (add m n)) q (cons A (add m' n) x (ih n ys))\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC (Vec A m) t) (Vec (Vec A m)) n)\n (ih : All (\u2115 tt) (VecD (Vec A m)) (Vec (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m)) n (t , c))\n \u2192 Vec A (mult n m)\n )\n ( (\u03bb q ih \u2192 subst (\u03bb n \u2192 Vec A (mult n m)) q (nil A))\n , (\u03bb n',xs,xss,q ih,tt \u2192\n let n' = proj\u2081 n',xs,xss,q\n xs = proj\u2081 (proj\u2082 n',xs,xss,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 n',xs,xss,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb n \u2192 Vec A (mult n m)) q (append A m xs (mult n' m) ih)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n module Eliminator where\n\n elim\u2115 : (P : (\u2115 tt) \u2192 Set)\n (pzero : P zero)\n (psuc : (m : \u2115 tt) \u2192 P m \u2192 P (suc m))\n (n : \u2115 tt)\n \u2192 P n\n elim\u2115 P pzero psuc = ind \u22a4 \u2115D (\u03bb u n \u2192 P n)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 P n) u (t , c))\n \u2192 P (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (here , q)))\n pzero\n u q\n )\n , (\u03bb n,q ih,tt \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (there here , proj\u2081 n,q , q)))\n (psuc (proj\u2081 n,q) (proj\u2081 ih,tt))\n u (proj\u2082 n,q)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n\n elimVec : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set)\n (pnil : P zero (nil A))\n (pcons : (n : \u2115 tt) (a : A) (xs : Vec A n) \u2192 P n xs \u2192 P (suc n) (cons A n a xs))\n (n : \u2115 tt)\n (xs : Vec A n)\n \u2192 P n xs\n elimVec A P pnil pcons = ind (\u2115 tt) (VecD A) (\u03bb n xs \u2192 P n xs)\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) n)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb n xs \u2192 P n xs) n (t , c))\n \u2192 P n (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq (\u2115 tt) zero (\u03bb n q \u2192 P n (con (here , q)))\n pnil\n n q\n )\n , (\u03bb n',x,xs,q ih,tt \u2192\n let n' = proj\u2081 n',x,xs,q\n x = proj\u2081 (proj\u2082 n',x,xs,q)\n xs = proj\u2081 (proj\u2082 (proj\u2082 n',x,xs,q))\n q = proj\u2082 (proj\u2082 (proj\u2082 n',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n elimEq (\u2115 tt) (suc n') (\u03bb n q \u2192 P n (con (there here , n' , x , xs , q)))\n (pcons n' x xs ih )\n n q\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elimVec A (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elimVec (Vec A m) (\u03bb n xss \u2192 Vec A (mult n m))\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n \n----------------------------------------------------------------------\n\n module GenericEliminator where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim2 \u22a4 \u2115T \u2115C _\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim2 \u22a4 \u2115T \u2115C _\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elim2 (\u2115 tt) VecT (VecC A) _\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elim2 (\u2115 tt) VecT (VecC (Vec A m)) _\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n","old_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nmodule Spire.Examples.PropositionalDesc where\n\n----------------------------------------------------------------------\n\nelimEq : (A : Set) (x : A) (P : (y : A) \u2192 x \u2261 y \u2192 Set)\n \u2192 P x refl\n \u2192 (y : A) (p : x \u2261 y) \u2192 P y p\nelimEq A .x P prefl x refl = prefl\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nCases : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nCases [] P = \u22a4\nCases (l \u2237 E) P = P here \u00d7 Cases E \u03bb t \u2192 P (there t)\n\ncase : (E : Enum) (P : Tag E \u2192 Set) (cs : Cases E P) (t : Tag E) \u2192 P t\ncase (l \u2237 E) P (c , cs) here = c\ncase (l \u2237 E) P (c , cs) (there t) = case E (\u03bb t \u2192 P (there t)) cs t\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n `End : (i : I) \u2192 Desc I\n `Rec : (i : I) (D : Desc I) \u2192 Desc I\n `Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n `RecFun : (A : Set) (B : A \u2192 I) (D : Desc I) \u2192 Desc I\n\nISet : Set \u2192 Set\u2081\nISet I = I \u2192 Set\n\nEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 ISet I\nEl I (`End j) X i = j \u2261 i\nEl I (`Rec j D) X i = X j \u00d7 El I D X i\nEl I (`Arg A B) X i = \u03a3 A (\u03bb a \u2192 El I (B a) X i)\nEl I (`RecFun A B D) X i = ((a : A) \u2192 X (B a)) \u00d7 El I D X i\n\ndata \u03bc (I : Set) (D : Desc I) (i : I) : Set where\n con : El I D (\u03bc I D) i \u2192 \u03bc I D i\n\nAll : (I : Set) (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El I D X i) \u2192 Set\nAll I (`End j) X P i q = \u22a4\nAll I (`Rec j D) X P i (x , xs) = P j x \u00d7 All I D X P i xs\nAll I (`Arg A B) X P i (a , b) = All I (B a) X P i b\nAll I (`RecFun A B D) X P i (f , xs) = ((a : A) \u2192 P (B a) (f a)) \u00d7 All I D X P i xs\n\ncaseD : (E : Enum) (I : Set) (cs : Cases E (\u03bb _ \u2192 Desc I)) (t : Tag E) \u2192 Desc I\ncaseD E I cs t = case E (\u03bb _ \u2192 Desc I) cs t\n\n----------------------------------------------------------------------\n\nUncurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl I D X = {i : I} \u2192 El I D X i \u2192 X i\n\nCurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl I (`End i) X = X i\nCurriedEl I (`Rec j D) X = (x : X j) \u2192 CurriedEl I D X\nCurriedEl I (`Arg A B) X = (a : A) \u2192 CurriedEl I (B a) X\nCurriedEl I (`RecFun A B D) X = ((a : A) \u2192 X (B a)) \u2192 CurriedEl I D X\n\ncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : UncurriedEl I D X) \u2192 CurriedEl I D X\ncurryEl I (`End i) X cn = cn refl\ncurryEl I (`Rec i D) X cn = \u03bb x \u2192 curryEl I D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl I (`Arg A B) X cn = \u03bb a \u2192 curryEl I (B a) X (\u03bb xs \u2192 cn (a , xs))\ncurryEl I (`RecFun A B D) X cn = \u03bb f \u2192 curryEl I D X (\u03bb xs \u2192 cn (f , xs))\n\nuncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : CurriedEl I D X) \u2192 UncurriedEl I D X\nuncurryEl I (`End i) X cn refl = cn\nuncurryEl I (`Rec i D) X cn (x , xs) = uncurryEl I D X (cn x) xs\nuncurryEl I (`Arg A B) X cn (a , xs) = uncurryEl I (B a) X (cn a) xs\nuncurryEl I (`RecFun A B D) X cn (f , xs) = uncurryEl I D X (cn f) xs\n\ncon2 : (I : Set) (D : Desc I) \u2192 CurriedEl I D (\u03bc I D)\ncon2 I D = curryEl I D (\u03bc I D) con\n\n----------------------------------------------------------------------\n\nUncurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : {i : I} \u2192 El I D X i \u2192 X i)\n \u2192 Set\nUncurriedAll I D X P cn =\n (i : I) (xs : El I D X i) \u2192 All I D X P i xs \u2192 P i (cn xs)\n\nCurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n \u2192 Set\nCurriedAll I (`End i) X P cn =\n P i (cn refl)\nCurriedAll I (`Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedAll I (`Arg A B) X P cn =\n (a : A) \u2192 CurriedAll I (B a) X P (\u03bb xs \u2192 cn (a , xs))\nCurriedAll I (`RecFun A B D) X P cn =\n (f : (a : A) \u2192 X (B a)) (ihf : (a : A) \u2192 P (B a) (f a)) \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (f , xs))\n\ncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : UncurriedAll I D X P cn)\n \u2192 CurriedAll I D X P cn\ncurryAll I (`End i) X P cn pf =\n pf i refl tt\ncurryAll I (`Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryAll I D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryAll I (`Arg A B) X P cn pf =\n \u03bb a \u2192 curryAll I (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\ncurryAll I (`RecFun A B D) X P cn pf =\n \u03bb f ihf \u2192 curryAll I D X P (\u03bb xs \u2192 cn (f , xs)) (\u03bb i xs ihs \u2192 pf i (f , xs) (ihf , ihs))\n\nuncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : CurriedAll I D X P cn)\n \u2192 UncurriedAll I D X P cn\nuncurryAll I (`End .i) X P cn pf i refl tt =\n pf\nuncurryAll I (`Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryAll I (`Arg A B) X P cn pf i (a , xs) ihs =\n uncurryAll I (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\nuncurryAll I (`RecFun A B D) X P cn pf i (f , xs) (ihf , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (f , ys)) (pf f ihf) i xs ihs\n\n----------------------------------------------------------------------\n\nind :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : UncurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\n\nhyps :\n (I : Set)\n (D\u2081 : Desc I)\n (P : (i : I) \u2192 \u03bc I D\u2081 i \u2192 Set)\n (pcon : UncurriedAll I D\u2081 (\u03bc I D\u2081) P con)\n (D\u2082 : Desc I)\n (i : I)\n (xs : El I D\u2082 (\u03bc I D\u2081) i)\n \u2192 All I D\u2082 (\u03bc I D\u2081) P i xs\n\nind I D P pcon i (con xs) = pcon i xs (hyps I D P pcon D i xs)\n\nhyps I D P pcon (`End j) i q = tt\nhyps I D P pcon (`Rec j A) i (x , xs) = ind I D P pcon j x , hyps I D P pcon A i xs\nhyps I D P pcon (`Arg A B) i (a , b) = hyps I D P pcon (B a) i b\nhyps I D P pcon (`RecFun A B E) i (f , xs) = (\u03bb a \u2192 ind I D P pcon (B a) (f a)) , hyps I D P pcon E i xs\n\n----------------------------------------------------------------------\n\nind2 :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : CurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\nind2 I D P pcon i x = ind I D P (uncurryAll I D (\u03bc I D) P con pcon) i x\n\nelim :\n (I : Set)\n (E : Enum)\n (Cs : Tag E \u2192 Desc I)\n \u2192 let D = `Arg (Tag E) Cs in\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (cs : Cases E (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))))\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\nelim I E Cs P cs i x =\n let D = `Arg (Tag E) Cs in\n ind2 I D P (case E (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))) cs) i x\n\n----------------------------------------------------------------------\n\nmodule Sugared where\n\n data \u2115T : Set where `zero `suc : \u2115T\n data VecT : Set where `nil `cons : VecT\n\n \u2115D : Desc \u22a4\n \u2115D = `Arg \u2115T \u03bb\n { `zero \u2192 `End tt\n ; `suc \u2192 `Rec tt (`End tt)\n }\n\n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n\n zero : \u2115 tt\n zero = con (`zero , refl)\n\n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (`suc , n , refl)\n\n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg VecT \u03bb\n { `nil \u2192 `End zero\n ; `cons \u2192 `Arg (\u2115 tt) \u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n))\n }\n\n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n\n nil : (A : Set) \u2192 Vec A zero\n nil A = con (`nil , refl)\n\n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (`cons , n , x , xs , refl)\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 n\n ; tt (`suc , m , q) (ih , tt) n \u2192 suc (ih n)\n }\n )\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 zero\n ; tt (`suc , m , q) (ih , tt) n \u2192 add n (ih n)\n }\n )\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) ih n ys \u2192 ys\n ; .(con (`suc , m , refl)) (`cons , m , x , xs , refl) (ih , tt) n ys \u2192 cons A (add m n) x (ih n ys)\n }\n )\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) tt \u2192 nil A\n ; .(con (`suc , n , refl)) (`cons , n , xs , xss , refl) (ih , tt) \u2192 append A m xs (mult n m) ih\n }\n )\n\n----------------------------------------------------------------------\n\nmodule Desugared where\n\n \u2115T : Enum\n \u2115T = \"zero\" \u2237 \"suc\" \u2237 []\n \n VecT : Enum\n VecT = \"nil\" \u2237 \"cons\" \u2237 []\n \n \u2115C : Tag \u2115T \u2192 Desc \u22a4\n \u2115C = caseD \u2115T \u22a4\n ( `End tt\n , `Rec tt (`End tt)\n , tt\n )\n \n \u2115D : Desc \u22a4\n \u2115D = `Arg (Tag \u2115T) \u2115C\n \n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n \n zero : \u2115 tt\n zero = con (here , refl)\n \n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (there here , n , refl)\n \n zero2 : \u2115 tt\n zero2 = con2 \u22a4 \u2115D here\n\n suc2 : \u2115 tt \u2192 \u2115 tt\n suc2 = con2 \u22a4 \u2115D (there here)\n\n VecC : (A : Set) \u2192 Tag VecT \u2192 Desc (\u2115 tt)\n VecC A = caseD VecT (\u2115 tt)\n ( `End zero\n , `Arg (\u2115 tt) (\u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n)))\n , tt\n )\n \n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg (Tag VecT) (VecC A)\n \n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n \n nil : (A : Set) \u2192 Vec A zero\n nil A = con (here , refl)\n \n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (there here , n , x , xs , refl)\n \n nil2 : (A : Set) \u2192 Vec A zero\n nil2 A = con2 (\u2115 tt) (VecD A) here\n\n cons2 : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons2 A = con2 (\u2115 tt) (VecD A) (there here)\n\n----------------------------------------------------------------------\n\n module Induction where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 n)\n , (\u03bb m,q ih,tt n \u2192 suc (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 zero)\n , (\u03bb m,q ih,tt n \u2192 add n (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb m t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) m)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)) m (t , c))\n (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n )\n ( (\u03bb q ih n ys \u2192 subst (\u03bb m \u2192 Vec A (add m n)) q ys)\n , (\u03bb m',x,xs,q ih,tt n ys \u2192\n let m' = proj\u2081 m',x,xs,q\n x = proj\u2081 (proj\u2082 m',x,xs,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 m',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb m \u2192 Vec A (add m n)) q (cons A (add m' n) x (ih n ys))\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC (Vec A m) t) (Vec (Vec A m)) n)\n (ih : All (\u2115 tt) (VecD (Vec A m)) (Vec (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m)) n (t , c))\n \u2192 Vec A (mult n m)\n )\n ( (\u03bb q ih \u2192 subst (\u03bb n \u2192 Vec A (mult n m)) q (nil A))\n , (\u03bb n',xs,xss,q ih,tt \u2192\n let n' = proj\u2081 n',xs,xss,q\n xs = proj\u2081 (proj\u2082 n',xs,xss,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 n',xs,xss,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb n \u2192 Vec A (mult n m)) q (append A m xs (mult n' m) ih)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n module Eliminator where\n\n elim\u2115 : (P : (\u2115 tt) \u2192 Set)\n (pzero : P zero)\n (psuc : (m : \u2115 tt) \u2192 P m \u2192 P (suc m))\n (n : \u2115 tt)\n \u2192 P n\n elim\u2115 P pzero psuc = ind \u22a4 \u2115D (\u03bb u n \u2192 P n)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 P n) u (t , c))\n \u2192 P (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (here , q)))\n pzero\n u q\n )\n , (\u03bb n,q ih,tt \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (there here , proj\u2081 n,q , q)))\n (psuc (proj\u2081 n,q) (proj\u2081 ih,tt))\n u (proj\u2082 n,q)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n\n elimVec : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set)\n (pnil : P zero (nil A))\n (pcons : (n : \u2115 tt) (a : A) (xs : Vec A n) \u2192 P n xs \u2192 P (suc n) (cons A n a xs))\n (n : \u2115 tt)\n (xs : Vec A n)\n \u2192 P n xs\n elimVec A P pnil pcons = ind (\u2115 tt) (VecD A) (\u03bb n xs \u2192 P n xs)\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) n)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb n xs \u2192 P n xs) n (t , c))\n \u2192 P n (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq (\u2115 tt) zero (\u03bb n q \u2192 P n (con (here , q)))\n pnil\n n q\n )\n , (\u03bb n',x,xs,q ih,tt \u2192\n let n' = proj\u2081 n',x,xs,q\n x = proj\u2081 (proj\u2082 n',x,xs,q)\n xs = proj\u2081 (proj\u2082 (proj\u2082 n',x,xs,q))\n q = proj\u2082 (proj\u2082 (proj\u2082 n',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n elimEq (\u2115 tt) (suc n') (\u03bb n q \u2192 P n (con (there here , n' , x , xs , q)))\n (pcons n' x xs ih )\n n q\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elimVec A (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elimVec (Vec A m) (\u03bb n xss \u2192 Vec A (mult n m))\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n \n----------------------------------------------------------------------\n\n module GenericEliminator where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim \u22a4 \u2115T \u2115C _\n ( (\u03bb n \u2192 n)\n , (\u03bb m ih n \u2192 suc (ih n))\n , tt\n )\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim \u22a4 \u2115T \u2115C _\n ( (\u03bb n \u2192 zero)\n , (\u03bb m ih n \u2192 add n (ih n))\n , tt\n )\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elim (\u2115 tt) VecT (VecC A) _\n ( (\u03bb n ys \u2192 ys)\n , (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n , tt\n )\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elim (\u2115 tt) VecT (VecC (Vec A m)) _\n ( (nil A)\n , (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n , tt\n )\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"0b0d9846a69f8b188de90f0c401104f21d016ae3","subject":"Add back generic eliminators in Darkwing, now improved with generic motives.","message":"Add back generic eliminators in Darkwing, now improved with generic motives.\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/Examples\/DarkwingDuck.agda","new_file":"formalization\/agda\/Spire\/Examples\/DarkwingDuck.agda","new_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nopen import Function\nmodule Spire.Examples.DarkwingDuck where\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nBranches : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nBranches [] P = \u22a4\nBranches (l \u2237 E) P = P here \u00d7 Branches E (\u03bb t \u2192 P (there t))\n\ncase : {E : Enum} (P : Tag E \u2192 Set) (cs : Branches E P) (t : Tag E) \u2192 P t\ncase P (c , cs) here = c\ncase P (c , cs) (there t) = case (\u03bb t \u2192 P (there t)) cs t\n\n----------------------------------------------------------------------\n\ndata Tel : Set where\n End : Tel\n Arg : (A : Set) (B : A \u2192 Tel) \u2192 Tel\n\nEl\u1d40 : Tel \u2192 Set\nEl\u1d40 End = \u22a4\nEl\u1d40 (Arg A B) = \u03a3 A (\u03bb a \u2192 El\u1d40 (B a))\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set where\n End : (i : I) \u2192 Desc I\n Rec : (i : I) (D : Desc I) \u2192 Desc I\n Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n\n----------------------------------------------------------------------\n\nISet : Set \u2192 Set\nISet I = I \u2192 Set\n\nEl\u1d30 : {I : Set} (D : Desc I) \u2192 ISet I \u2192 ISet I\nEl\u1d30 (End j) X i = j \u2261 i\nEl\u1d30 (Rec j D) X i = X j \u00d7 El\u1d30 D X i\nEl\u1d30 (Arg A B) X i = \u03a3 A (\u03bb a \u2192 El\u1d30 (B a) X i)\n\nHyps : {I : Set} (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El\u1d30 D X i) \u2192 Set\nHyps (End j) X P i q = \u22a4\nHyps (Rec j D) X P i (x , xs) = P j x \u00d7 Hyps D X P i xs\nHyps (Arg A B) X P i (a , b) = Hyps (B a) X P i b\n\n----------------------------------------------------------------------\n\ndata \u03bc {I : Set} (D : Desc I) (i : I) : Set where\n init : El\u1d30 D (\u03bc D) i \u2192 \u03bc D i\n\n----------------------------------------------------------------------\n\nind : {I : Set} (D : Desc I)\n (M : (i : I) \u2192 \u03bc D i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 D (\u03bc D) i) (ihs : Hyps D (\u03bc D) M i xs) \u2192 M i (init xs))\n (i : I)\n (x : \u03bc D i)\n \u2192 M i x\n\nprove : {I : Set} (D E : Desc I)\n (M : (i : I) \u2192 \u03bc E i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 E (\u03bc E) i) (ihs : Hyps E (\u03bc E) M i xs) \u2192 M i (init xs))\n (i : I) (xs : El\u1d30 D (\u03bc E) i) \u2192 Hyps D (\u03bc E) M i xs\n\nind D M \u03b1 i (init xs) = \u03b1 i xs (prove D D M \u03b1 i xs)\n\nprove (End j) E M \u03b1 i q = tt\nprove (Rec j D) E M \u03b1 i (x , xs) = ind E M \u03b1 j x , prove D E M \u03b1 i xs\nprove (Arg A B) E M \u03b1 i (a , xs) = prove (B a) E M \u03b1 i xs\n\n----------------------------------------------------------------------\n\nrecord Data : Set where\n field\n P : Tel\n I : El\u1d40 P \u2192 Tel\n E : Enum\n B : (A : El\u1d40 P) \u2192 Branches E (\u03bb _ \u2192 Desc (El\u1d40 (I A)))\n\n C : (A : El\u1d40 P) \u2192 Tag E \u2192 Desc (El\u1d40 (I A))\n C A = case (\u03bb _ \u2192 Desc (El\u1d40 (I A))) (B A)\n\n D : (A : El\u1d40 P) \u2192 Desc (El\u1d40 (I A))\n D A = Arg (Tag E) (C A)\n\n----------------------------------------------------------------------\n\nUncurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedBranches E P X = Branches E P \u2192 X\n\nCurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedBranches [] P X = X\nCurriedBranches (l \u2237 E) P X = P here \u2192 CurriedBranches E (\u03bb t \u2192 P (there t)) X\n\ncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 UncurriedBranches E P X \u2192 CurriedBranches E P X\ncurryBranches {[]} f = f tt\ncurryBranches {l \u2237 E} f = \u03bb c \u2192 curryBranches (\u03bb cs \u2192 f (c , cs))\n\nuncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 CurriedBranches E P X \u2192 UncurriedBranches E P X\nuncurryBranches {[]} x tt = x\nuncurryBranches {l \u2237 E} f (c , cs) = uncurryBranches (f c) cs\n\n----------------------------------------------------------------------\n\nUncurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nUncurriedEl\u1d40 T X = (xs : El\u1d40 T) \u2192 X xs\n\nCurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nCurriedEl\u1d40 End X = X tt\nCurriedEl\u1d40 (Arg A B) X = (a : A) \u2192 CurriedEl\u1d40 (B a) (\u03bb b \u2192 X (a , b))\n\ncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 UncurriedEl\u1d40 T X \u2192 CurriedEl\u1d40 T X\ncurryEl\u1d40 End X f = f tt\ncurryEl\u1d40 (Arg A B) X f = \u03bb a \u2192 curryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (\u03bb b \u2192 f (a , b))\n\nuncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 CurriedEl\u1d40 T X \u2192 UncurriedEl\u1d40 T X\nuncurryEl\u1d40 End X x tt = x\nuncurryEl\u1d40 (Arg A B) X f (a , b) = uncurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (f a) b\n\nICurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nICurriedEl\u1d40 End X = X tt\nICurriedEl\u1d40 (Arg A B) X = {a : A} \u2192 ICurriedEl\u1d40 (B a) (\u03bb b \u2192 X (a , b))\n\nicurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 UncurriedEl\u1d40 T X \u2192 ICurriedEl\u1d40 T X\nicurryEl\u1d40 End X f = f tt\nicurryEl\u1d40 (Arg A B) X f = \u03bb {a} \u2192 icurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (\u03bb b \u2192 f (a , b))\n\niuncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 ICurriedEl\u1d40 T X \u2192 UncurriedEl\u1d40 T X\niuncurryEl\u1d40 End X x tt = x\niuncurryEl\u1d40 (Arg A B) X f (a , b) = iuncurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) f b\n\n----------------------------------------------------------------------\n\nUncurriedEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl\u1d30 D X = \u2200{i} \u2192 El\u1d30 D X i \u2192 X i\n\nCurriedEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl\u1d30 (End i) X = X i\nCurriedEl\u1d30 (Rec i D) X = (x : X i) \u2192 CurriedEl\u1d30 D X\nCurriedEl\u1d30 (Arg A B) X = (a : A) \u2192 CurriedEl\u1d30 (B a) X\n\ncurryEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I)\n \u2192 UncurriedEl\u1d30 D X \u2192 CurriedEl\u1d30 D X\ncurryEl\u1d30 (End i) X cn = cn refl\ncurryEl\u1d30 (Rec i D) X cn = \u03bb x \u2192 curryEl\u1d30 D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl\u1d30 (Arg A B) X cn = \u03bb a \u2192 curryEl\u1d30 (B a) X (\u03bb xs \u2192 cn (a , xs))\n\n----------------------------------------------------------------------\n\nUncurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 Set\nUncurriedHyps D X P cn =\n \u2200 i (xs : El\u1d30 D X i) (ihs : Hyps D X P i xs) \u2192 P i (cn xs)\n\nCurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 Set\nCurriedHyps (End i) X P cn =\n P i (cn refl)\nCurriedHyps (Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedHyps D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedHyps (Arg A B) X P cn =\n (a : A) \u2192 CurriedHyps (B a) X P (\u03bb xs \u2192 cn (a , xs))\n\ncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 UncurriedHyps D X P cn\n \u2192 CurriedHyps D X P cn\ncurryHyps (End i) X P cn pf =\n pf i refl tt\ncurryHyps (Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryHyps D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryHyps (Arg A B) X P cn pf =\n \u03bb a \u2192 curryHyps (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\n\nuncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 CurriedHyps D X P cn\n \u2192 UncurriedHyps D X P cn\nuncurryHyps (End .i) X P cn pf i refl tt =\n pf\nuncurryHyps (Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryHyps D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryHyps (Arg A B) X P cn pf i (a , xs) ihs =\n uncurryHyps (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\n\n----------------------------------------------------------------------\n\nFormUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 UncurriedEl\u1d40 (Data.I R p) \u03bb i\n \u2192 Set\nFormUncurried R p i = \u03bc (Data.D R p) i\n\nForm : (R : Data)\n \u2192 CurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 CurriedEl\u1d40 (Data.I R p) \u03bb i\n \u2192 Set\nForm R =\n curryEl\u1d40 (Data.P R) (\u03bb p \u2192 CurriedEl\u1d40 (Data.I R p) \u03bb i \u2192 Set) \u03bb p \u2192\n curryEl\u1d40 (Data.I R p) (\u03bb i \u2192 Set) \u03bb i \u2192\n FormUncurried R p i\n\n----------------------------------------------------------------------\n\ninjUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p\n in CurriedEl\u1d30 D (\u03bc D)\ninjUncurried R p t = curryEl\u1d30 (Data.C R p t)\n (\u03bc (Data.D R p))\n (\u03bb xs \u2192 init (t , xs))\n\ninj : (R : Data)\n \u2192 ICurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p\n in CurriedEl\u1d30 D (\u03bc D)\ninj R = icurryEl\u1d40 (Data.P R)\n (\u03bb p \u2192 let D = Data.D R p in CurriedEl\u1d30 D (\u03bc D))\n (injUncurried R)\n\n----------------------------------------------------------------------\n\nindCurried : {I : Set} (D : Desc I)\n (M : (i : I) \u2192 \u03bc D i \u2192 Set)\n (f : CurriedHyps D (\u03bc D) M init)\n (i : I)\n (x : \u03bc D i)\n \u2192 M i x\nindCurried D M f i x =\n ind D M (uncurryHyps D (\u03bc D) M init f) i x\n\nSumCurriedHyps : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : \u2200 i \u2192 \u03bc D i \u2192 Set)\n \u2192 Tag (Data.E R) \u2192 Set\nSumCurriedHyps R p M t =\n CurriedHyps (Data.C R p t) (\u03bc (Data.D R p)) M (\u03bb xs \u2192 init (t , xs))\n\nelimUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : UncurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 UncurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (\u2200 i (x : \u03bc D i) \u2192 M i x)\nelimUncurried R p M cs i x =\n indCurried (Data.D R p) M\n (case (SumCurriedHyps R p M) cs)\n i x\n\nelim : (R : Data)\n \u2192 ICurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in CurriedBranches (Data.E R)\n (SumCurriedHyps R p unM)\n (\u2200 i (x : \u03bc D i) \u2192 unM i x)\nelim R = icurryEl\u1d40 (Data.P R)\n (\u03bb p \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in CurriedBranches (Data.E R)\n (SumCurriedHyps R p unM)\n (\u2200 i (x : \u03bc D i) \u2192 unM i x))\n (\u03bb p M \u2192 curryBranches\n (elimUncurried R p\n (uncurryEl\u1d40 (Data.I R p)\n ((\u03bb i \u2192 \u03bc (Data.D R p) i \u2192 Set)) M)))\n\n----------------------------------------------------------------------\n\n\u2115E : Enum\n\u2115E = \"zero\" \u2237 \"suc\" \u2237 []\n\nVecE : Enum\nVecE = \"nil\" \u2237 \"cons\" \u2237 []\n\n\u2115T : Set\n\u2115T = Tag \u2115E\n\nVecT : Set\nVecT = Tag VecE\n\nzeroT : \u2115T\nzeroT = here\n\nsucT : \u2115T\nsucT = there here\n\nnilT : VecT\nnilT = here\n\nconsT : VecT\nconsT = there here\n\n----------------------------------------------------------------------\n\n\u2115R : Data\n\u2115R = record\n { P = End\n ; I = \u03bb _ \u2192 End\n ; E = \u2115E\n ; B = \u03bb _ \u2192\n End tt\n , Rec tt (End tt)\n , tt\n }\n\n\u2115 : Set\n\u2115 = Form \u2115R\n\nzero : \u2115\nzero = inj \u2115R zeroT\n\nsuc : \u2115 \u2192 \u2115\nsuc = inj \u2115R sucT\n\nVecR : Data\nVecR = record\n { P = Arg Set (\u03bb _ \u2192 End)\n ; I = \u03bb _ \u2192 Arg \u2115 (\u03bb _ \u2192 End)\n ; E = VecE\n ; B = \u03bb A \u2192\n End (zero , tt)\n , Arg \u2115 (\u03bb n \u2192 Arg (proj\u2081 A) \u03bb _ \u2192 Rec (n , tt) (End (suc n , tt)))\n , tt\n }\n\nVec : (A : Set) \u2192 \u2115 \u2192 Set\nVec = Form VecR\n\nnil : {A : Set} \u2192 Vec A zero\nnil = inj VecR nilT\n\ncons : {A : Set} (n : \u2115) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\ncons = inj VecR consT\n\n----------------------------------------------------------------------\n\nadd : \u2115 \u2192 \u2115 \u2192 \u2115\nadd = elim \u2115R (\u03bb n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n tt\n\nmult : \u2115 \u2192 \u2115 \u2192 \u2115\nmult = elim \u2115R (\u03bb n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n tt\n\nappend : {A : Set} (m : \u2115) (xs : Vec A m) (n : \u2115) (ys : Vec A n) \u2192 Vec A (add m n)\nappend {A} m xs = elim VecR (\u03bb m xs \u2192 (n : \u2115) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons (add m n) x (ih n ys))\n (m , tt) xs\n\nconcat : {A : Set} (m n : \u2115) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\nconcat {A} m n xss = elim VecR (\u03bb n xss \u2192 Vec A (mult n m))\n nil\n (\u03bb n xs xss ih \u2192 append m xs (mult n m) ih)\n (n , tt) xss\n\n----------------------------------------------------------------------\n","old_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nopen import Function\nmodule Spire.Examples.DarkwingDuck where\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nBranches : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nBranches [] P = \u22a4\nBranches (l \u2237 E) P = P here \u00d7 Branches E (\u03bb t \u2192 P (there t))\n\ncase : {E : Enum} (P : Tag E \u2192 Set) (cs : Branches E P) (t : Tag E) \u2192 P t\ncase P (c , cs) here = c\ncase P (c , cs) (there t) = case (\u03bb t \u2192 P (there t)) cs t\n\n----------------------------------------------------------------------\n\ndata Tel : Set where\n End : Tel\n Arg : (A : Set) (B : A \u2192 Tel) \u2192 Tel\n\nEl\u1d40 : Tel \u2192 Set\nEl\u1d40 End = \u22a4\nEl\u1d40 (Arg A B) = \u03a3 A (\u03bb a \u2192 El\u1d40 (B a))\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set where\n End : (i : I) \u2192 Desc I\n Rec : (i : I) (D : Desc I) \u2192 Desc I\n Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n\n----------------------------------------------------------------------\n\nISet : Set \u2192 Set\nISet I = I \u2192 Set\n\nEl\u1d30 : {I : Set} (D : Desc I) \u2192 ISet I \u2192 ISet I\nEl\u1d30 (End j) X i = j \u2261 i\nEl\u1d30 (Rec j D) X i = X j \u00d7 El\u1d30 D X i\nEl\u1d30 (Arg A B) X i = \u03a3 A (\u03bb a \u2192 El\u1d30 (B a) X i)\n\nHyps : {I : Set} (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El\u1d30 D X i) \u2192 Set\nHyps (End j) X P i q = \u22a4\nHyps (Rec j D) X P i (x , xs) = P j x \u00d7 Hyps D X P i xs\nHyps (Arg A B) X P i (a , b) = Hyps (B a) X P i b\n\n----------------------------------------------------------------------\n\ndata \u03bc {I : Set} (D : Desc I) (i : I) : Set where\n init : El\u1d30 D (\u03bc D) i \u2192 \u03bc D i\n\n----------------------------------------------------------------------\n\nind : {I : Set} (D : Desc I)\n (M : (i : I) \u2192 \u03bc D i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 D (\u03bc D) i) (ihs : Hyps D (\u03bc D) M i xs) \u2192 M i (init xs))\n (i : I)\n (x : \u03bc D i)\n \u2192 M i x\n\nprove : {I : Set} (D E : Desc I)\n (M : (i : I) \u2192 \u03bc E i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 E (\u03bc E) i) (ihs : Hyps E (\u03bc E) M i xs) \u2192 M i (init xs))\n (i : I) (xs : El\u1d30 D (\u03bc E) i) \u2192 Hyps D (\u03bc E) M i xs\n\nind D M \u03b1 i (init xs) = \u03b1 i xs (prove D D M \u03b1 i xs)\n\nprove (End j) E M \u03b1 i q = tt\nprove (Rec j D) E M \u03b1 i (x , xs) = ind E M \u03b1 j x , prove D E M \u03b1 i xs\nprove (Arg A B) E M \u03b1 i (a , xs) = prove (B a) E M \u03b1 i xs\n\n----------------------------------------------------------------------\n\nrecord Data : Set where\n field\n P : Tel\n I : El\u1d40 P \u2192 Tel\n E : Enum\n B : (A : El\u1d40 P) \u2192 Branches E (\u03bb _ \u2192 Desc (El\u1d40 (I A)))\n\n C : (A : El\u1d40 P) \u2192 Tag E \u2192 Desc (El\u1d40 (I A))\n C A = case (\u03bb _ \u2192 Desc (El\u1d40 (I A))) (B A)\n\n D : (A : El\u1d40 P) \u2192 Desc (El\u1d40 (I A))\n D A = Arg (Tag E) (C A)\n\n----------------------------------------------------------------------\n\nUncurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedBranches E P X = Branches E P \u2192 X\n\nCurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedBranches [] P X = X\nCurriedBranches (l \u2237 E) P X = P here \u2192 CurriedBranches E (\u03bb t \u2192 P (there t)) X\n\ncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 UncurriedBranches E P X \u2192 CurriedBranches E P X\ncurryBranches {[]} f = f tt\ncurryBranches {l \u2237 E} f = \u03bb c \u2192 curryBranches (\u03bb cs \u2192 f (c , cs))\n\nuncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 CurriedBranches E P X \u2192 UncurriedBranches E P X\nuncurryBranches {[]} x tt = x\nuncurryBranches {l \u2237 E} f (c , cs) = uncurryBranches (f c) cs\n\n----------------------------------------------------------------------\n\nUncurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nUncurriedEl\u1d40 T X = (xs : El\u1d40 T) \u2192 X xs\n\nCurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nCurriedEl\u1d40 End X = X tt\nCurriedEl\u1d40 (Arg A B) X = (a : A) \u2192 CurriedEl\u1d40 (B a) (\u03bb b \u2192 X (a , b))\n\ncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 UncurriedEl\u1d40 T X \u2192 CurriedEl\u1d40 T X\ncurryEl\u1d40 End X f = f tt\ncurryEl\u1d40 (Arg A B) X f = \u03bb a \u2192 curryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (\u03bb b \u2192 f (a , b))\n\nICurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nICurriedEl\u1d40 End X = X tt\nICurriedEl\u1d40 (Arg A B) X = {a : A} \u2192 ICurriedEl\u1d40 (B a) (\u03bb b \u2192 X (a , b))\n\nicurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 UncurriedEl\u1d40 T X \u2192 ICurriedEl\u1d40 T X\nicurryEl\u1d40 End X f = f tt\nicurryEl\u1d40 (Arg A B) X f = \u03bb {a} \u2192 icurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (\u03bb b \u2192 f (a , b))\n\niuncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 ICurriedEl\u1d40 T X \u2192 UncurriedEl\u1d40 T X\niuncurryEl\u1d40 End X x tt = x\niuncurryEl\u1d40 (Arg A B) X f (a , b) = iuncurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) f b\n\n----------------------------------------------------------------------\n\nUncurriedEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl\u1d30 D X = \u2200{i} \u2192 El\u1d30 D X i \u2192 X i\n\nCurriedEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl\u1d30 (End i) X = X i\nCurriedEl\u1d30 (Rec i D) X = (x : X i) \u2192 CurriedEl\u1d30 D X\nCurriedEl\u1d30 (Arg A B) X = (a : A) \u2192 CurriedEl\u1d30 (B a) X\n\ncurryEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I)\n \u2192 UncurriedEl\u1d30 D X \u2192 CurriedEl\u1d30 D X\ncurryEl\u1d30 (End i) X cn = cn refl\ncurryEl\u1d30 (Rec i D) X cn = \u03bb x \u2192 curryEl\u1d30 D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl\u1d30 (Arg A B) X cn = \u03bb a \u2192 curryEl\u1d30 (B a) X (\u03bb xs \u2192 cn (a , xs))\n\n----------------------------------------------------------------------\n\nUncurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 Set\nUncurriedHyps D X P cn =\n \u2200 i (xs : El\u1d30 D X i) (ihs : Hyps D X P i xs) \u2192 P i (cn xs)\n\nCurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 Set\nCurriedHyps (End i) X P cn =\n P i (cn refl)\nCurriedHyps (Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedHyps D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedHyps (Arg A B) X P cn =\n (a : A) \u2192 CurriedHyps (B a) X P (\u03bb xs \u2192 cn (a , xs))\n\ncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 UncurriedHyps D X P cn\n \u2192 CurriedHyps D X P cn\ncurryHyps (End i) X P cn pf =\n pf i refl tt\ncurryHyps (Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryHyps D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryHyps (Arg A B) X P cn pf =\n \u03bb a \u2192 curryHyps (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\n\nuncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 CurriedHyps D X P cn\n \u2192 UncurriedHyps D X P cn\nuncurryHyps (End .i) X P cn pf i refl tt =\n pf\nuncurryHyps (Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryHyps D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryHyps (Arg A B) X P cn pf i (a , xs) ihs =\n uncurryHyps (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\n\n----------------------------------------------------------------------\n\nFormUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 UncurriedEl\u1d40 (Data.I R p) \u03bb i\n \u2192 Set\nFormUncurried R p i = \u03bc (Data.D R p) i\n\nForm : (R : Data)\n \u2192 CurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 CurriedEl\u1d40 (Data.I R p) \u03bb i\n \u2192 Set\nForm R =\n curryEl\u1d40 (Data.P R) (\u03bb p \u2192 CurriedEl\u1d40 (Data.I R p) \u03bb i \u2192 Set) \u03bb p \u2192\n curryEl\u1d40 (Data.I R p) (\u03bb i \u2192 Set) \u03bb i \u2192\n FormUncurried R p i\n\n----------------------------------------------------------------------\n\ninjUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p\n in CurriedEl\u1d30 D (\u03bc D)\ninjUncurried R p t = curryEl\u1d30 (Data.C R p t)\n (\u03bc (Data.D R p))\n (\u03bb xs \u2192 init (t , xs))\n\ninj : (R : Data)\n \u2192 ICurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p\n in CurriedEl\u1d30 D (\u03bc D)\ninj R = icurryEl\u1d40 (Data.P R)\n (\u03bb p \u2192 let D = Data.D R p in CurriedEl\u1d30 D (\u03bc D))\n (injUncurried R)\n\n----------------------------------------------------------------------\n\nindCurried : {I : Set} (D : Desc I)\n (M : (i : I) \u2192 \u03bc D i \u2192 Set)\n (f : CurriedHyps D (\u03bc D) M init)\n (i : I)\n (x : \u03bc D i)\n \u2192 M i x\nindCurried D M f i x =\n ind D M (uncurryHyps D (\u03bc D) M init f) i x\n\nSumCurriedHyps : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : \u2200 i \u2192 \u03bc D i \u2192 Set)\n \u2192 Tag (Data.E R) \u2192 Set\nSumCurriedHyps R p M t =\n CurriedHyps (Data.C R p t) (\u03bc (Data.D R p)) M (\u03bb xs \u2192 init (t , xs))\n\nelimUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : \u2200 i \u2192 \u03bc D i \u2192 Set)\n \u2192 UncurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (\u2200 i (x : \u03bc D i) \u2192 M i x)\nelimUncurried R p M cs i x =\n indCurried (Data.D R p) M\n (case (SumCurriedHyps R p M) cs)\n i x\n\nelim : (R : Data)\n \u2192 ICurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : \u2200 i \u2192 \u03bc D i \u2192 Set)\n \u2192 CurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (\u2200 i (x : \u03bc D i) \u2192 M i x)\nelim R = icurryEl\u1d40 (Data.P R)\n (\u03bb p \u2192 let D = Data.D R p in\n (M : \u2200 i \u2192 \u03bc D i \u2192 Set) \u2192\n CurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (\u2200 i (x : \u03bc D i) \u2192 M i x))\n (\u03bb p M \u2192 curryBranches (elimUncurried R p M))\n\n----------------------------------------------------------------------\n\n\u2115E : Enum\n\u2115E = \"zero\" \u2237 \"suc\" \u2237 []\n\nVecE : Enum\nVecE = \"nil\" \u2237 \"cons\" \u2237 []\n\n\u2115T : Set\n\u2115T = Tag \u2115E\n\nVecT : Set\nVecT = Tag VecE\n\nzeroT : \u2115T\nzeroT = here\n\nsucT : \u2115T\nsucT = there here\n\nnilT : VecT\nnilT = here\n\nconsT : VecT\nconsT = there here\n\n----------------------------------------------------------------------\n\n\u2115R : Data\n\u2115R = record\n { P = End\n ; I = \u03bb _ \u2192 End\n ; E = \u2115E\n ; B = \u03bb _ \u2192\n End tt\n , Rec tt (End tt)\n , tt\n }\n\n\u2115 : Set\n\u2115 = Form \u2115R\n\nzero : \u2115\nzero = inj \u2115R zeroT\n\nsuc : \u2115 \u2192 \u2115\nsuc = inj \u2115R sucT\n\nVecR : Data\nVecR = record\n { P = Arg Set (\u03bb _ \u2192 End)\n ; I = \u03bb _ \u2192 Arg \u2115 (\u03bb _ \u2192 End)\n ; E = VecE\n ; B = \u03bb A \u2192\n End (zero , tt)\n , Arg \u2115 (\u03bb n \u2192 Arg (proj\u2081 A) \u03bb _ \u2192 Rec (n , tt) (End (suc n , tt)))\n , tt\n }\n\nVec : (A : Set) \u2192 \u2115 \u2192 Set\nVec = Form VecR\n\nnil : {A : Set} \u2192 Vec A zero\nnil = inj VecR nilT\n\ncons : {A : Set} (n : \u2115) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\ncons = inj VecR consT\n\n----------------------------------------------------------------------\n\nadd : \u2115 \u2192 \u2115 \u2192 \u2115\nadd = elim \u2115R (\u03bb u n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n tt\n\nmult : \u2115 \u2192 \u2115 \u2192 \u2115\nmult = elim \u2115R (\u03bb u n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n tt\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"bc0f697e345522fad65844eed07cba984158e3b7","subject":"Parametric.Change.Validity: Comment out bug-triggering code","message":"Parametric.Change.Validity: Comment out bug-triggering code\n\nThis code is exported and used elsewhere :-(\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Validity.agda","new_file":"Parametric\/Change\/Validity.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e)\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Denotation.Value as Value\nopen import Base.Change.Algebra as CA\n using (ChangeAlgebraFamily)\n\nmodule Parametric.Change.Validity\n {Base : Type.Structure}\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Extension Point: Change algebras for base types\nStructure : Set\u2081\nStructure = ChangeAlgebraFamily zero \u27e6_\u27e7Base\n\nmodule Structure (change-algebra-base : Structure) where\n -- change algebras\n\n open CA public renaming\n ( [] to \u2205\n ; _\u2237_ to _\u2022_\n )\n\n -- We provide: change algebra for every type\n change-algebra : \u2200 \u03c4 \u2192 ChangeAlgebra zero \u27e6 \u03c4 \u27e7Type\n change-algebra (base \u03b9) = change-algebra\u208d \u03b9 \u208e\n change-algebra (\u03c4\u2081 \u21d2 \u03c4\u2082) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}}\n\n change-algebra-family : ChangeAlgebraFamily zero \u27e6_\u27e7Type\n change-algebra-family = family change-algebra\n\n -- function changes\n\n open FunctionChanges public using (cons)\n module _ {\u03c4\u2081 \u03c4\u2082 : Type} where\n open FunctionChanges.FunctionChange _ _ {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}} public\n renaming\n ( correct to is-valid\n ; apply to call-change\n )\n\n -- We also provide: change environments (aka. environment changes).\n\n {-\n open ListChanges \u27e6_\u27e7Type {{change-algebra-family}} public using () renaming\n ( changeAlgebra to environment-changes\n )\n\n after-env : \u2200 {\u0393 : Context} \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) \u2192 \u27e6 \u0393 \u27e7\n after-env {\u0393} = after\u208d \u0393 \u208e\n -}\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e)\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Denotation.Value as Value\nopen import Base.Change.Algebra as CA\n using (ChangeAlgebraFamily)\n\nmodule Parametric.Change.Validity\n {Base : Type.Structure}\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Extension Point: Change algebras for base types\nStructure : Set\u2081\nStructure = ChangeAlgebraFamily zero \u27e6_\u27e7Base\n\nmodule Structure (change-algebra-base : Structure) where\n -- change algebras\n\n open CA public renaming\n ( [] to \u2205\n ; _\u2237_ to _\u2022_\n )\n\n -- We provide: change algebra for every type\n change-algebra : \u2200 \u03c4 \u2192 ChangeAlgebra zero \u27e6 \u03c4 \u27e7Type\n change-algebra (base \u03b9) = change-algebra\u208d \u03b9 \u208e\n change-algebra (\u03c4\u2081 \u21d2 \u03c4\u2082) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}}\n\n change-algebra-family : ChangeAlgebraFamily zero \u27e6_\u27e7Type\n change-algebra-family = family change-algebra\n\n -- function changes\n\n open FunctionChanges public using (cons)\n module _ {\u03c4\u2081 \u03c4\u2082 : Type} where\n open FunctionChanges.FunctionChange _ _ {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}} public\n renaming\n ( correct to is-valid\n ; apply to call-change\n )\n\n -- We also provide: change environments (aka. environment changes).\n\n open ListChanges \u27e6_\u27e7Type {{change-algebra-family}} public using () renaming\n ( changeAlgebra to environment-changes\n )\n\n after-env : \u2200 {\u0393 : Context} \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) \u2192 \u27e6 \u0393 \u27e7\n after-env {\u0393} = after\u208d \u0393 \u208e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d4e4f011631cfbb914d7d51c617806ac6e74cd50","subject":"Typo.","message":"Typo.\n\nIgnore-this: 68af14a7e65f38003c3779ccf4921770\n\ndarcs-hash:20110930182805-3bd4e-97d00b34b1a4af679e44fc8768ed579a9589a669.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/DataPostulate.agda","new_file":"Draft\/DataPostulate.agda","new_contents":"------------------------------------------------------------------------------\n-- Data and postulates\n------------------------------------------------------------------------------\n\n-- Are the FOTC natural numbers defined by data and postulates the\n-- same?\n\nmodule DataPostulate where\n\n------------------------------------------------------------------------------\npostulate\n D : Set\n zero : D\n succ : D \u2192 D\n\n-- The FOTC natural numbers using data.\ndata N : D \u2192 Set where\n zN : N zero\n sN : \u2200 {n} \u2192 N n \u2192 N (succ n)\n\nindN : (P : D \u2192 Set) \u2192\n P zero \u2192\n (\u2200 {n} \u2192 N n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 N n \u2192 P n\nindN P P0 h zN = P0\nindN P P0 h (sN Nn) = h Nn (indN P P0 h Nn)\n\n-- The FOTC natural numbers using postulates (we chose 'M' by 'Model').\npostulate\n M : D \u2192 Set\n zM : M zero\n sM : \u2200 {n} \u2192 M n \u2192 M (succ n)\n indM : (P : D \u2192 Set) \u2192\n P zero \u2192\n (\u2200 {n} \u2192 M n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 M n \u2192 P n\n\n------------------------------------------------------------------------------\n-- The predicates\n\n-- From the data predicate to the postulated one: Using the induction\n-- principle.\nnat-D2P : \u2200 {n} \u2192 N n \u2192 M n\nnat-D2P = indN M zM (\u03bb _ Mn \u2192 sM Mn)\n\n-- From the data predicate to the postulated one: Using pattern\n-- matching.\nnat-D2P' : \u2200 {n} \u2192 N n \u2192 M n\nnat-D2P' zN = zM\nnat-D2P' (sN Nn) = sM (nat-D2P' Nn)\n\n-- From the postulated predicate to the data one.\nnat-P2D : \u2200 {n} \u2192 M n \u2192 N n\nnat-P2D = indM N zN (\u03bb _ Nn \u2192 sN Nn)\n\n------------------------------------------------------------------------------\n-- The induction principles\n\n-- The postulated inductive principle from the data one.\nindD2P : (P : D \u2192 Set) \u2192 P zero \u2192\n (\u2200 {n} \u2192 M n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 M n \u2192 P n\nindD2P P P0 ih Mn = indN P P0 (\u03bb {_} Nn \u2192 ih (nat-D2P Nn)) (nat-P2D Mn)\n\n-- The data inductive principle from the postulated one.\nindP2D : (P : D \u2192 Set) \u2192 P zero \u2192\n (\u2200 {n} \u2192 N n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 N n \u2192 P n\nindP2D P P0 ih Nn = indM P P0 (\u03bb {_} Mn \u2192 ih (nat-P2D Mn)) (nat-D2P Nn)\n","old_contents":"------------------------------------------------------------------------------\n-- Data and postulates\n------------------------------------------------------------------------------\n\n-- Are the FOTC natural numbers defined by data and postulates the\n-- same?\n\nmodule DataPostulate where\n\n------------------------------------------------------------------------------\npostulate\n D : Set\n zero : D\n succ : D \u2192 D\n\n-- The FOTC natural numbers using data.\ndata N : D \u2192 Set where\n zN : N zero\n sN : \u2200 {n} \u2192 N n \u2192 N (succ n)\n\nindN : (P : D \u2192 Set) \u2192\n P zero \u2192\n (\u2200 {n} \u2192 N n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 N n \u2192 P n\nindN P P0 h zN = P0\nindN P P0 h (sN Nn) = h Nn (indN P P0 h Nn)\n\n-- The FOTC natural numbers using postulates (we choose 'M' by 'Model').\npostulate\n M : D \u2192 Set\n zM : M zero\n sM : \u2200 {n} \u2192 M n \u2192 M (succ n)\n indM : (P : D \u2192 Set) \u2192\n P zero \u2192\n (\u2200 {n} \u2192 M n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 M n \u2192 P n\n\n------------------------------------------------------------------------------\n-- The predicates\n\n-- From the data predicate to the postulated one: Using the induction\n-- principle.\nnat-D2P : \u2200 {n} \u2192 N n \u2192 M n\nnat-D2P = indN M zM (\u03bb _ Mn \u2192 sM Mn)\n\n-- From the data predicate to the postulated one: Using pattern\n-- matching.\nnat-D2P' : \u2200 {n} \u2192 N n \u2192 M n\nnat-D2P' zN = zM\nnat-D2P' (sN Nn) = sM (nat-D2P' Nn)\n\n-- From the postulated predicate to the data one.\nnat-P2D : \u2200 {n} \u2192 M n \u2192 N n\nnat-P2D = indM N zN (\u03bb _ Nn \u2192 sN Nn)\n\n------------------------------------------------------------------------------\n-- The induction principles\n\n-- The postulated inductive principle from the data one.\nindD2P : (P : D \u2192 Set) \u2192 P zero \u2192\n (\u2200 {n} \u2192 M n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 M n \u2192 P n\nindD2P P P0 ih Mn = indN P P0 (\u03bb {_} Nn \u2192 ih (nat-D2P Nn)) (nat-P2D Mn)\n\n-- The data inductive principle from the postulated one.\nindP2D : (P : D \u2192 Set) \u2192 P zero \u2192\n (\u2200 {n} \u2192 N n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 N n \u2192 P n\nindP2D P P0 ih Nn = indM P P0 (\u03bb {_} Mn \u2192 ih (nat-P2D Mn)) (nat-D2P Nn)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ac25a046e34905a5fd630275f7f19cf585a2f9ab","subject":"Remove no longer relevant TODO","message":"Remove no longer relevant TODO\n","repos":"crypto-agda\/crypto-agda","old_file":"ZK\/JSChecker.agda","new_file":"ZK\/JSChecker.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule ZK.JSChecker where\n\nopen import Function using (id; _\u2218\u2032_; case_of_)\nopen import Data.Bool.Base using (Bool; true; false; _\u2227_)\nopen import Data.List.Base using (List; []; _\u2237_; and; foldr)\nopen import Data.String.Base using (String)\n\nopen import FFI.JS\nopen import FFI.JS.Check\n-- open import FFI.JS.Proc using (URI; JSProc; showURI; server)\n-- open import Control.Process.Type\nimport FFI.JS.Console as Console\nimport FFI.JS.Process as Process\nimport FFI.JS.FS as FS\n\nimport FFI.JS.BigI as BigI\nopen BigI using (BigI; bigI)\n\nimport Crypto.JS.BigI.ZqZp as ZqZp\n\n-- TODO dynamise me\nprimality-test-probability-bound : Number\nprimality-test-probability-bound = readNumber \"10\"\n\n-- TODO: check if this is large enough\nmin-bits-q : Number\nmin-bits-q = 256N\n\nmin-bits-p : Number\nmin-bits-p = 2048N\n\nbigdec : JSValue \u2192 BigI\nbigdec v = bigI (castString v) \"10\"\n\n-- TODO bug (undefined)!\nrecord ZK-chaum-pedersen-pok-elgamal-rnd {--(\u2124q \u2124p\u2605 : Set)--} : Set where\n field\n m c s : BigI {--\u2124q--}\n g p q y \u03b1 \u03b2 A B : BigI --\u2124p\u2605\n\nzk-check-chaum-pedersen-pok-elgamal-rnd! : ZK-chaum-pedersen-pok-elgamal-rnd {-BigI BigI-} \u2192 JS!\nzk-check-chaum-pedersen-pok-elgamal-rnd! pf\n = trace \"g=\" g \u03bb _ \u2192\n trace \"p=\" I.p \u03bb _ \u2192\n trace \"q=\" I.q \u03bb _ \u2192\n trace \"y=\" y \u03bb _ \u2192\n trace \"\u03b1=\" \u03b1 \u03bb _ \u2192\n trace \"\u03b2=\" \u03b2 \u03bb _ \u2192\n trace \"m=\" m \u03bb _ \u2192\n trace \"M=\" M \u03bb _ \u2192\n trace \"A=\" A \u03bb _ \u2192\n trace \"B=\" B \u03bb _ \u2192\n trace \"c=\" c \u03bb _ \u2192\n trace \"s=\" s \u03bb _ \u2192\n checks!\n >> check! \"g^s==A\u00b7\u03b1^c\" ((g ^ s) == (A \u00b7 (\u03b1 ^ c))) (\u03bb _ \u2192 \"\")\n >> check! \"y^s==B\u00b7(\u03b2\/M)^c\" ((y ^ s) == (B \u00b7 ((\u03b2 \u00b7\/ M) ^ c))) (\u03bb _ \u2192 \"\")\n module ZK-check-chaum-pedersen-pok-elgamal-rnd where\n module I = ZK-chaum-pedersen-pok-elgamal-rnd pf\n params = record\n { primality-test-probability-bound = primality-test-probability-bound\n ; min-bits-q = min-bits-q\n ; min-bits-p = min-bits-p\n ; qI = I.q\n ; pI = I.p\n ; gI = I.g\n }\n open module [\u2124q]\u2124p\u2605 = ZqZp params\n A = BigI\u25b9\u2124p\u2605 I.A\n B = BigI\u25b9\u2124p\u2605 I.B\n \u03b1 = BigI\u25b9\u2124p\u2605 I.\u03b1\n \u03b2 = BigI\u25b9\u2124p\u2605 I.\u03b2\n y = BigI\u25b9\u2124p\u2605 I.y\n s = BigI\u25b9\u2124q I.s\n c = BigI\u25b9\u2124q I.c\n m = BigI\u25b9\u2124q I.m\n M = g ^ m\n\nzk-check! : JSValue \u2192 JS!\nzk-check! arg =\n check! \"type of statement\" (typ === fromString cpt)\n (\u03bb _ \u2192 \"Expected type of statement: \" ++ cpt ++ \" not \" ++ toString typ)\n >> zk-check-chaum-pedersen-pok-elgamal-rnd! pok\n module Zk-check where\n cpt = \"chaum-pedersen-pok-elgamal-rnd\"\n stm = arg \u00b7\u00ab \"statement\" \u00bb\n typ = stm \u00b7\u00ab \"type\" \u00bb\n dat = stm \u00b7\u00ab \"data\" \u00bb\n g = bigdec (dat \u00b7\u00ab \"g\" \u00bb)\n p = bigdec (dat \u00b7\u00ab \"p\" \u00bb)\n q = bigdec (dat \u00b7\u00ab \"q\" \u00bb)\n y = bigdec (dat \u00b7\u00ab \"y\" \u00bb)\n m = bigdec (dat \u00b7\u00ab \"plain\" \u00bb)\n enc = dat \u00b7\u00ab \"enc\" \u00bb\n \u03b1 = bigdec (enc \u00b7\u00ab \"alpha\" \u00bb)\n \u03b2 = bigdec (enc \u00b7\u00ab \"beta\" \u00bb)\n prf = arg \u00b7\u00ab \"proof\" \u00bb\n com = prf \u00b7\u00ab \"commitment\" \u00bb\n A = bigdec (com \u00b7\u00ab \"A\" \u00bb)\n B = bigdec (com \u00b7\u00ab \"B\" \u00bb)\n c = bigdec (prf \u00b7\u00ab \"challenge\" \u00bb)\n s = bigdec (prf \u00b7\u00ab \"response\" \u00bb)\n pok = record { g = g; p = p; q = q; y = y; \u03b1 = \u03b1; \u03b2 = \u03b2; A = A; B = B; c = c; s = s; m = m }\n\n{-\nsrv : URI \u2192 JSProc\nsrv d =\n recv d \u03bb q \u2192\n send d (fromBool (zk-check q))\n end\n-}\n\n-- Working around Agda.Primitive.lsuc being undefined\n-- case_of_ : {A : Set} {B : Set} \u2192 A \u2192 (A \u2192 B) \u2192 B\n-- case x of f = f x\n\nmain : JS!\nmain =\n Process.argv !\u2081 \u03bb args \u2192\n case JSArray\u25b9ListString args of \u03bb {\n (_node \u2237 _run \u2237 _test \u2237 args') \u2192\n case args' of \u03bb {\n [] \u2192\n Console.log \"usage: No arguments\"\n {- server \"127.0.0.1\" \"1337\" srv !\u2081 \u03bb uri \u2192\n Console.log (showURI uri)\n -}\n ; (arg \u2237 args'') \u2192\n case args'' of \u03bb {\n [] \u2192\n Console.log (\"Reading input file: \" ++ arg) >>\n FS.readFile arg nullJS !\u2082 \u03bb err dat \u2192\n check! \"reading input file\" (is-null err)\n (\u03bb _ \u2192 \"readFile error: \" ++ toString err) >>\n zk-check! (JSON-parse (toString dat))\n ; _ \u2192\n Console.log \"usage: Too many arguments\"\n }\n }\n ; _ \u2192\n Console.log \"usage\"\n }\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule ZK.JSChecker where\n\nopen import Function using (id; _\u2218\u2032_; case_of_)\nopen import Data.Bool.Base using (Bool; true; false; _\u2227_)\nopen import Data.List.Base using (List; []; _\u2237_; and; foldr)\nopen import Data.String.Base using (String)\n\nopen import FFI.JS\nopen import FFI.JS.Check\n-- open import FFI.JS.Proc using (URI; JSProc; showURI; server)\n-- open import Control.Process.Type\nimport FFI.JS.Console as Console\nimport FFI.JS.Process as Process\nimport FFI.JS.FS as FS\n\nimport FFI.JS.BigI as BigI\nopen BigI using (BigI; bigI)\n\nimport Crypto.JS.BigI.ZqZp as ZqZp\n\n-- TODO dynamise me\nprimality-test-probability-bound : Number\nprimality-test-probability-bound = readNumber \"10\"\n\n-- TODO: check if this is large enough\nmin-bits-q : Number\nmin-bits-q = 256N\n\nmin-bits-p : Number\nmin-bits-p = 2048N\n\n-- TODO check with undefined\nbigdec : JSValue \u2192 BigI\nbigdec v = bigI (castString v) \"10\"\n\n-- TODO bug (undefined)!\nrecord ZK-chaum-pedersen-pok-elgamal-rnd {--(\u2124q \u2124p\u2605 : Set)--} : Set where\n field\n m c s : BigI {--\u2124q--}\n g p q y \u03b1 \u03b2 A B : BigI --\u2124p\u2605\n\nzk-check-chaum-pedersen-pok-elgamal-rnd! : ZK-chaum-pedersen-pok-elgamal-rnd {-BigI BigI-} \u2192 JS!\nzk-check-chaum-pedersen-pok-elgamal-rnd! pf\n = trace \"g=\" g \u03bb _ \u2192\n trace \"p=\" I.p \u03bb _ \u2192\n trace \"q=\" I.q \u03bb _ \u2192\n trace \"y=\" y \u03bb _ \u2192\n trace \"\u03b1=\" \u03b1 \u03bb _ \u2192\n trace \"\u03b2=\" \u03b2 \u03bb _ \u2192\n trace \"m=\" m \u03bb _ \u2192\n trace \"M=\" M \u03bb _ \u2192\n trace \"A=\" A \u03bb _ \u2192\n trace \"B=\" B \u03bb _ \u2192\n trace \"c=\" c \u03bb _ \u2192\n trace \"s=\" s \u03bb _ \u2192\n checks!\n >> check! \"g^s==A\u00b7\u03b1^c\" ((g ^ s) == (A \u00b7 (\u03b1 ^ c))) (\u03bb _ \u2192 \"\")\n >> check! \"y^s==B\u00b7(\u03b2\/M)^c\" ((y ^ s) == (B \u00b7 ((\u03b2 \u00b7\/ M) ^ c))) (\u03bb _ \u2192 \"\")\n module ZK-check-chaum-pedersen-pok-elgamal-rnd where\n module I = ZK-chaum-pedersen-pok-elgamal-rnd pf\n params = record\n { primality-test-probability-bound = primality-test-probability-bound\n ; min-bits-q = min-bits-q\n ; min-bits-p = min-bits-p\n ; qI = I.q\n ; pI = I.p\n ; gI = I.g\n }\n open module [\u2124q]\u2124p\u2605 = ZqZp params\n A = BigI\u25b9\u2124p\u2605 I.A\n B = BigI\u25b9\u2124p\u2605 I.B\n \u03b1 = BigI\u25b9\u2124p\u2605 I.\u03b1\n \u03b2 = BigI\u25b9\u2124p\u2605 I.\u03b2\n y = BigI\u25b9\u2124p\u2605 I.y\n s = BigI\u25b9\u2124q I.s\n c = BigI\u25b9\u2124q I.c\n m = BigI\u25b9\u2124q I.m\n M = g ^ m\n\nzk-check! : JSValue \u2192 JS!\nzk-check! arg =\n check! \"type of statement\" (typ === fromString cpt)\n (\u03bb _ \u2192 \"Expected type of statement: \" ++ cpt ++ \" not \" ++ toString typ)\n >> zk-check-chaum-pedersen-pok-elgamal-rnd! pok\n module Zk-check where\n cpt = \"chaum-pedersen-pok-elgamal-rnd\"\n stm = arg \u00b7\u00ab \"statement\" \u00bb\n typ = stm \u00b7\u00ab \"type\" \u00bb\n dat = stm \u00b7\u00ab \"data\" \u00bb\n g = bigdec (dat \u00b7\u00ab \"g\" \u00bb)\n p = bigdec (dat \u00b7\u00ab \"p\" \u00bb)\n q = bigdec (dat \u00b7\u00ab \"q\" \u00bb)\n y = bigdec (dat \u00b7\u00ab \"y\" \u00bb)\n m = bigdec (dat \u00b7\u00ab \"plain\" \u00bb)\n enc = dat \u00b7\u00ab \"enc\" \u00bb\n \u03b1 = bigdec (enc \u00b7\u00ab \"alpha\" \u00bb)\n \u03b2 = bigdec (enc \u00b7\u00ab \"beta\" \u00bb)\n prf = arg \u00b7\u00ab \"proof\" \u00bb\n com = prf \u00b7\u00ab \"commitment\" \u00bb\n A = bigdec (com \u00b7\u00ab \"A\" \u00bb)\n B = bigdec (com \u00b7\u00ab \"B\" \u00bb)\n c = bigdec (prf \u00b7\u00ab \"challenge\" \u00bb)\n s = bigdec (prf \u00b7\u00ab \"response\" \u00bb)\n pok = record { g = g; p = p; q = q; y = y; \u03b1 = \u03b1; \u03b2 = \u03b2; A = A; B = B; c = c; s = s; m = m }\n\n{-\nsrv : URI \u2192 JSProc\nsrv d =\n recv d \u03bb q \u2192\n send d (fromBool (zk-check q))\n end\n-}\n\n-- Working around Agda.Primitive.lsuc being undefined\n-- case_of_ : {A : Set} {B : Set} \u2192 A \u2192 (A \u2192 B) \u2192 B\n-- case x of f = f x\n\nmain : JS!\nmain =\n Process.argv !\u2081 \u03bb args \u2192\n case JSArray\u25b9ListString args of \u03bb {\n (_node \u2237 _run \u2237 _test \u2237 args') \u2192\n case args' of \u03bb {\n [] \u2192\n Console.log \"usage: No arguments\"\n {- server \"127.0.0.1\" \"1337\" srv !\u2081 \u03bb uri \u2192\n Console.log (showURI uri)\n -}\n ; (arg \u2237 args'') \u2192\n case args'' of \u03bb {\n [] \u2192\n Console.log (\"Reading input file: \" ++ arg) >>\n FS.readFile arg nullJS !\u2082 \u03bb err dat \u2192\n check! \"reading input file\" (is-null err)\n (\u03bb _ \u2192 \"readFile error: \" ++ toString err) >>\n zk-check! (JSON-parse (toString dat))\n ; _ \u2192\n Console.log \"usage: Too many arguments\"\n }\n }\n ; _ \u2192\n Console.log \"usage\"\n }\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"a6dfe1f553ba79964ad12503ce1a9095fe79ffe2","subject":"Add test","message":"Add test\n","repos":"louisswarren\/hieretikz","old_file":"arrow.agda","new_file":"arrow.agda","new_contents":"data Bool : Set where\n true : Bool\n false : Bool\n\n\n_or_ : Bool \u2192 Bool \u2192 Bool\ntrue or _ = true\n_ or true = true\nfalse or false = false\n\n_and_ : Bool \u2192 Bool \u2192 Bool\nfalse and _ = false\n_ and false = false\ntrue and true = true\n\n\n----------------------------------------\n\n\n\ndata \u2115 : Set where\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n{-# BUILTIN NATURAL \u2115 #-}\n\n\n_\u2261_ : \u2115 \u2192 \u2115 \u2192 Bool\nzero \u2261 zero = true\nsuc n \u2261 suc m = n \u2261 m\n_ \u2261 _ = false\n\n\n\n----------------------------------------\n\n\ninfixr 5 _\u2237_\n\ndata List (A : Set) : Set where\n \u2218 : List A\n _\u2237_ : A \u2192 List A \u2192 List A\n\n\nany : {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 Bool\nany _ \u2218 = false\nany f (x \u2237 xs) = (f x) or (any f xs)\n\n\n_\u2208_ : \u2115 \u2192 List \u2115 \u2192 Bool\nx \u2208 \u2218 = false\nx \u2208 (y \u2237 ys) with x \u2261 y\n... | true = true\n... | false = x \u2208 ys\n\n\n_\u220b_ : List \u2115 \u2192 \u2115 \u2192 Bool\nxs \u220b y = y \u2208 xs\n\n----------------------------------------\n\n\n\ndata Arrow : Set where\n \u21d2_ : \u2115 \u2192 Arrow\n _\u21d2_ : \u2115 \u2192 Arrow \u2192 Arrow\n\n_\u2261\u2261_ : Arrow \u2192 Arrow \u2192 Bool\n(\u21d2 q) \u2261\u2261 (\u21d2 s) = q \u2261 s\n(p \u21d2 q) \u2261\u2261 (r \u21d2 s) = (p \u2261 r) and (q \u2261\u2261 s)\n_ \u2261\u2261 _ = false\n\n_\u2208\u2208_ : Arrow \u2192 List Arrow \u2192 Bool\nx \u2208\u2208 \u2218 = false\nx \u2208\u2208 (y \u2237 ys) with x \u2261\u2261 y\n... | true = true\n... | false = x \u2208\u2208 ys\n\n\nclosure : List Arrow \u2192 List \u2115 \u2192 List \u2115\nclosure \u2218 found = found\nclosure ((\u21d2 n) \u2237 rest) found = n \u2237 (closure rest (n \u2237 found))\nclosure ((n \u21d2 q) \u2237 rest) found with (n \u2208 found) or (n \u2208 (closure rest found))\n... | true = closure (q \u2237 rest) found\n... | false = closure rest found\n\n\n\n_,_\u22a2_ : List Arrow \u2192 List \u2115 \u2192 \u2115 \u2192 Bool\ncs , ps \u22a2 q = q \u2208 (closure cs ps)\n\n_\u22a2_ : List Arrow \u2192 Arrow \u2192 Bool\ncs \u22a2 (\u21d2 q) = q \u2208 (closure cs \u2218)\ncs \u22a2 (p \u21d2 q) = ((\u21d2 p) \u2237 cs) \u22a2 q\n\n\n----------------------------------------\n\n\n\ndata Separation : Set where\n model : List \u2115 \u2192 List \u2115 \u2192 Separation\n\n\nmodelsupports : Separation \u2192 List Arrow \u2192 \u2115 \u2192 Bool\nmodelsupports (model holds _) cs n = cs , holds \u22a2 n\n\n\nmodeldenies : Separation \u2192 List Arrow \u2192 \u2115 \u2192 Bool\nmodeldenies (model _ fails) cs n = any (_\u220b_ (closure cs (n \u2237 \u2218))) fails\n\n\n_\u27ea!_\u27eb_ : List Arrow \u2192 Separation \u2192 Arrow \u2192 Bool\ncs \u27ea! m \u27eb (\u21d2 q) = modeldenies m cs q\ncs \u27ea! m \u27eb (p \u21d2 q) = (modelsupports m cs p) and (cs \u27ea! m \u27eb q)\n\n\n_\u27ea_\u27eb_ : List Arrow \u2192 List Separation \u2192 Arrow \u2192 Bool\ncs \u27ea \u2218 \u27eb arr = false\ncs \u27ea m \u2237 ms \u27eb arr = (cs \u27ea! m \u27eb arr) or (cs \u27ea ms \u27eb arr)\n\n\n\n----------------------------------------\n\n\n\ndata Relation : Set where\n Proved : Relation\n Derivable : Relation\n Separated : Relation\n Unknown : Relation\n\n\nconsider : List Arrow \u2192 List Separation \u2192 Arrow \u2192 Relation\nconsider cs ms arr with (arr \u2208\u2208 cs)\n... | true = Proved\n... | false with (cs \u22a2 arr)\n... | true = Derivable\n... | false with (cs \u27ea ms \u27eb arr)\n... | true = Separated\n... | false = Unknown\n\n\nproofs : List Arrow\nproofs =\n (3 \u21d2 (\u21d2 4)) \u2237\n-- (5 \u21d2 (\u21d2 4)) \u2237\n (6 \u21d2 (\u21d2 4)) \u2237\n (3 \u21d2 (\u21d2 3)) \u2237\n (3 \u21d2 (\u21d2 3)) \u2237\n (9 \u21d2 (\u21d2 3)) \u2237\n (9 \u21d2 (\u21d2 3)) \u2237\n (5 \u21d2 (\u21d2 7)) \u2237\n (9 \u21d2 (\u21d2 7)) \u2237\n (6 \u21d2 (\u21d2 8)) \u2237\n (10 \u21d2 (\u21d2 8)) \u2237\n (10 \u21d2 (\u21d2 7)) \u2237\n (5 \u21d2 (\u21d2 10)) \u2237\n (10 \u21d2 (\u21d2 4)) \u2237\n (5 \u21d2 (\u21d2 11)) \u2237\n (6 \u21d2 (\u21d2 11)) \u2237\n (11 \u21d2 (\u21d2 4)) \u2237\n (10 \u21d2 (\u21d2 7)) \u2237\n (8 \u21d2 (\u21d2 4)) \u2237\n (9 \u21d2 (\u21d2 7)) \u2237\n (9 \u21d2 (\u21d2 8)) \u2237\n (3 \u21d2 (\u21d2 8)) \u2237\n (5 \u21d2 (3 \u21d2 (\u21d2 9))) \u2237\n (6 \u21d2 (7 \u21d2 (\u21d2 10))) \u2237\n (6 \u21d2 (3 \u21d2 (\u21d2 3))) \u2237\n (7 \u21d2 (\u21d2 7)) \u2237\n (7 \u21d2 (\u21d2 7)) \u2237\n (7 \u21d2 (8 \u21d2 (\u21d2 10))) \u2237\n (3 \u21d2 (10 \u21d2 (\u21d2 9))) \u2237\n (5 \u21d2 (\u21d2 1)) \u2237\n (3 \u21d2 (1 \u21d2 (\u21d2 9))) \u2237\n (1 \u21d2 (\u21d2 2)) \u2237\n (10 \u21d2 (\u21d2 2)) \u2237 \u2218\n\ncms : List Separation\ncms =\n (model (12 \u2237 6 \u2237 11 \u2237 4 \u2237 1 \u2237 \u2218) (5 \u2237 3 \u2237 7 \u2237 7 \u2237 \u2218)) \u2237\n (model (6 \u2237 3 \u2237 11 \u2237 4 \u2237 7 \u2237 8 \u2237 3 \u2237 9 \u2237 10 \u2237 1 \u2237 \u2218) (5 \u2237 \u2218)) \u2237\n (model (12 \u2237 5 \u2237 11 \u2237 4 \u2237 1 \u2237 \u2218) (6 \u2237 3 \u2237 \u2218)) \u2237\n (model (5 \u2237 3 \u2237 11 \u2237 4 \u2237 7 \u2237 8 \u2237 3 \u2237 9 \u2237 10 \u2237 1 \u2237 \u2218) (6 \u2237 \u2218)) \u2237\n (model (12 \u2237 4 \u2237 11 \u2237 \u2218) (5 \u2237 6 \u2237 3 \u2237 8 \u2237 9 \u2237 1 \u2237 \u2218)) \u2237\n (model (12 \u2237 5 \u2237 6 \u2237 4 \u2237 11 \u2237 1 \u2237 \u2218) (3 \u2237 \u2218)) \u2237\n (model (12 \u2237 4 \u2237 11 \u2237 7 \u2237 \u2218) (9 \u2237 5 \u2237 6 \u2237 10 \u2237 8 \u2237 1 \u2237 \u2218)) \u2237\n (model (10 \u2237 9 \u2237 \u2218) (1 \u2237 \u2218)) \u2237\n (model (3 \u2237 4 \u2237 11 \u2237 \u2218) (9 \u2237 5 \u2237 6 \u2237 10 \u2237 7 \u2237 1 \u2237 \u2218)) \u2237\n (model (12 \u2237 7 \u2237 1 \u2237 \u2218) (4 \u2237 11 \u2237 8 \u2237 \u2218)) \u2237\n (model (9 \u2237 3 \u2237 10 \u2237 8 \u2237 1 \u2237 \u2218) (11 \u2237 \u2218)) \u2237\n (model (12 \u2237 4 \u2237 10 \u2237 1 \u2237 \u2218) (11 \u2237 3 \u2237 \u2218)) \u2237\n (model (3 \u2237 6 \u2237 5 \u2237 \u2218) (\u2218)) \u2237\n (model (1 \u2237 2 \u2237 3 \u2237 4 \u2237 5 \u2237 6 \u2237 7 \u2237 8 \u2237 9 \u2237 10 \u2237 11 \u2237 \u2218) (12 \u2237 \u2218)) \u2237 \u2218\n\ntestp : Arrow\ntestp = (5 \u21d2 (\u21d2 10))\n\ntestd : Arrow\ntestd = (5 \u21d2 (\u21d2 7))\n\ntests : Arrow\ntests = (5 \u21d2 (\u21d2 3))\n\ntestu : Arrow\ntestu = (6 \u21d2 (\u21d2 1))\n","old_contents":"data Bool : Set where\n true : Bool\n false : Bool\n\n\n_or_ : Bool \u2192 Bool \u2192 Bool\ntrue or _ = true\n_ or true = true\nfalse or false = false\n\n_and_ : Bool \u2192 Bool \u2192 Bool\nfalse and _ = false\n_ and false = false\ntrue and true = true\n\n\n----------------------------------------\n\n\n\ndata \u2115 : Set where\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n{-# BUILTIN NATURAL \u2115 #-}\n\n\n_\u2261_ : \u2115 \u2192 \u2115 \u2192 Bool\nzero \u2261 zero = true\nsuc n \u2261 suc m = n \u2261 m\n_ \u2261 _ = false\n\n\n\n----------------------------------------\n\n\n\ndata List (A : Set) : Set where\n \u2218 : List A\n _\u2237_ : A \u2192 List A \u2192 List A\n\n\nany : {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 Bool\nany _ \u2218 = false\nany f (x \u2237 xs) = (f x) or (any f xs)\n\n\napply : {A B : Set} \u2192 (A \u2192 B) \u2192 List A \u2192 List B\napply _ \u2218 = \u2218\napply f (x \u2237 xs) = (f x) \u2237 (apply f xs)\n\n\n_\u2208_ : \u2115 \u2192 List \u2115 \u2192 Bool\nx \u2208 \u2218 = false\nx \u2208 (y \u2237 ys) with x \u2261 y\n... | true = true\n... | false = x \u2208 ys\n\n\n_\u220b_ : List \u2115 \u2192 \u2115 \u2192 Bool\nxs \u220b y = y \u2208 xs\n\n----------------------------------------\n\n\n\ndata Arrow : Set where\n \u21d2_ : \u2115 \u2192 Arrow\n _\u21d2_ : \u2115 \u2192 Arrow \u2192 Arrow\n\n_\u2261\u2261_ : Arrow \u2192 Arrow \u2192 Bool\n(\u21d2 q) \u2261\u2261 (\u21d2 s) = q \u2261 s\n(p \u21d2 q) \u2261\u2261 (r \u21d2 s) = (p \u2261 r) and (q \u2261\u2261 s)\n_ \u2261\u2261 _ = false\n\n_\u2208\u2208_ : Arrow \u2192 List Arrow \u2192 Bool\nx \u2208\u2208 \u2218 = false\nx \u2208\u2208 (y \u2237 ys) with x \u2261\u2261 y\n... | true = true\n... | false = x \u2208\u2208 ys\n\n\nclosure : List Arrow \u2192 List \u2115 \u2192 List \u2115\nclosure \u2218 found = found\nclosure ((\u21d2 n) \u2237 rest) found = n \u2237 (closure rest (n \u2237 found))\nclosure ((n \u21d2 q) \u2237 rest) found with (n \u2208 found) or (n \u2208 (closure rest found))\n... | true = closure (q \u2237 rest) found\n... | false = closure rest found\n\n\n\n_,_\u22a2_ : List Arrow \u2192 List \u2115 \u2192 \u2115 \u2192 Bool\ncs , ps \u22a2 q = q \u2208 (closure cs ps)\n\n_\u22a2_ : List Arrow \u2192 Arrow \u2192 Bool\ncs \u22a2 (\u21d2 q) = q \u2208 (closure cs \u2218)\ncs \u22a2 (p \u21d2 q) = ((\u21d2 p) \u2237 cs) \u22a2 q\n\n\n----------------------------------------\n\n\n\ndata Separation : Set where\n model : List \u2115 \u2192 List \u2115 \u2192 Separation\n\n\nmodelsupports : Separation \u2192 List Arrow \u2192 \u2115 \u2192 Bool\nmodelsupports (model holds _) cs n = cs , holds \u22a2 n\n\n\nmodeldenies : Separation \u2192 List Arrow \u2192 \u2115 \u2192 Bool\nmodeldenies (model _ fails) cs n = any (_\u220b_ (closure cs (n \u2237 \u2218))) fails\n\n\n_\u27ea!_\u27eb_ : List Arrow \u2192 Separation \u2192 Arrow \u2192 Bool\ncs \u27ea! m \u27eb (\u21d2 q) = modeldenies m cs q\ncs \u27ea! m \u27eb (p \u21d2 q) = (modelsupports m cs p) and (cs \u27ea! m \u27eb q)\n\n\n_\u27ea_\u27eb_ : List Arrow \u2192 List Separation \u2192 Arrow \u2192 Bool\ncs \u27ea \u2218 \u27eb arr = false\ncs \u27ea m \u2237 ms \u27eb arr = (cs \u27ea! m \u27eb arr) or (cs \u27ea ms \u27eb arr)\n\n\n\n----------------------------------------\n\n\n\ndata Relation : Set where\n Proved : Relation\n Derivable : Relation\n Separated : Relation\n Unknown : Relation\n\n\nconsider : List Arrow \u2192 List Separation \u2192 Arrow \u2192 Relation\nconsider cs ms arr with (arr \u2208\u2208 cs)\n... | true = Proved\n... | false with (cs \u22a2 arr)\n... | true = Derivable\n... | false with (cs \u27ea ms \u27eb arr)\n... | true = Separated\n... | false = Unknown\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"255f75653eee710cb4309c5fe5ec0c5a42aea891","subject":"scraps","message":"scraps\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"lemmas-subst-ta.agda","new_file":"lemmas-subst-ta.agda","new_contents":"open import Prelude\nopen import Nat\nopen import List\nopen import core\nopen import contexts\nopen import weakening\nopen import exchange\nopen import lemmas-disjointness\n\nmodule lemmas-subst-ta where\n -- todo: add a premise from a new judgement like holes disjoint that\n -- classifies pairs of dhexps that share no variable names\n -- whatsoever. that should imply freshness here. then propagate that\n -- change to preservation. this is morally what \u03b1-equiv lets us do in a\n -- real setting.\n\n -- the variable name x does not appear in the term d\n data var-name-new : (x : Nat) (d : dhexp) \u2192 Set where\n VNNConst : \u2200{x} \u2192 var-name-new x c\n VNNVar : \u2200{x y} \u2192 x \u2260 y \u2192 var-name-new x (X y)\n VNNLam2 : \u2200{x d y \u03c4} \u2192 x \u2260 y\n \u2192 var-name-new x d\n \u2192 var-name-new x (\u00b7\u03bb_[_]_ y \u03c4 d)\n VNNHole : \u2200{x u \u03c3} \u2192 var-name-new x (\u2987\u2988\u27e8 u , \u03c3 \u27e9) -- todo something about \u03c3?\n VNNNEHole : \u2200{x u \u03c3 d } \u2192\n var-name-new x d \u2192\n var-name-new x (\u2987 d \u2988\u27e8 u , \u03c3 \u27e9) -- todo something about \u03c3?\n VNNAp : \u2200{ x d1 d2 } \u2192\n var-name-new x d1 \u2192\n var-name-new x d2 \u2192\n var-name-new x (d1 \u2218 d2)\n VNNCast : \u2200{x d \u03c41 \u03c42} \u2192 var-name-new x d \u2192 var-name-new x (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9)\n VNNFailedCast : \u2200{x d \u03c41 \u03c42} \u2192 var-name-new x d \u2192 var-name-new x (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n\n -- two terms that do not share any hole names\n data var-names-disjoint : (d1 : dhexp) \u2192 (d2 : dhexp) \u2192 Set where\n VNDConst : \u2200{d} \u2192 var-names-disjoint c d\n VNDVar : \u2200{x d} \u2192 var-names-disjoint (X x) d\n VNDLam : \u2200{x \u03c4 d1 d2} \u2192 var-names-disjoint d1 d2\n \u2192 var-names-disjoint (\u00b7\u03bb_[_]_ x \u03c4 d1) d2\n VNDHole : \u2200{u \u03c3 d2} \u2192 var-name-new u d2\n \u2192 var-names-disjoint (\u2987\u2988\u27e8 u , \u03c3 \u27e9) d2 -- todo something about \u03c3?\n VNDNEHole : \u2200{u \u03c3 d1 d2} \u2192 var-name-new u d2\n \u2192 var-names-disjoint d1 d2\n \u2192 var-names-disjoint (\u2987 d1 \u2988\u27e8 u , \u03c3 \u27e9) d2 -- todo something about \u03c3?\n VNDAp : \u2200{d1 d2 d3} \u2192 var-names-disjoint d1 d3\n \u2192 var-names-disjoint d2 d3\n \u2192 var-names-disjoint (d1 \u2218 d2) d3\n\n -- all the variable names in the term are unique\n data var-names-unique : dhexp \u2192 Set where\n VNUHole : var-names-unique c\n VNUVar : \u2200{x} \u2192 var-names-unique (X x)\n VNULam : {x : Nat} {\u03c4 : htyp} {d : dhexp} \u2192 var-names-unique d\n \u2192 var-name-new x d\n \u2192 var-names-unique (\u00b7\u03bb_[_]_ x \u03c4 d)\n VNUEHole : \u2200{u \u03c3} \u2192 var-names-unique (\u2987\u2988\u27e8 u , \u03c3 \u27e9) -- todo something about \u03c3?\n VNUNEHole : \u2200{u \u03c3 d} \u2192 var-names-unique d\n \u2192 var-names-unique (\u2987 d \u2988\u27e8 u , \u03c3 \u27e9) -- todo something about \u03c3?\n VNUAp : \u2200{d1 d2} \u2192 var-names-unique d1\n \u2192 var-names-unique d2\n \u2192 var-names-disjoint d1 d2\n \u2192 var-names-unique (d1 \u2218 d2)\n VNUCast : \u2200{d \u03c41 \u03c42} \u2192 var-names-unique d\n \u2192 var-names-unique (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9)\n VNUFailedCast : \u2200{d \u03c41 \u03c42} \u2192 var-names-unique d\n \u2192 var-names-unique (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n\n unique-fresh : \u2200{\u0394 \u0393 d \u03c4 y} \u2192 \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192 y # \u0393 \u2192 var-names-unique d \u2192 fresh y d\n unique-fresh TAConst apt VNUHole = FConst\n unique-fresh (TAVar x\u2081) apt VNUVar = {!!}\n unique-fresh (TALam x\u2081 wt) apt (VNULam unq x\u2082) = {!!}\n unique-fresh (TAAp wt wt\u2081) apt (VNUAp unq unq\u2081 x) = {!!}\n unique-fresh (TAEHole x x\u2081) apt VNUEHole = {!!}\n unique-fresh (TANEHole x wt x\u2081) apt (VNUNEHole unq) = {!!}\n unique-fresh (TACast wt x) apt (VNUCast unq) = {!!}\n unique-fresh (TAFailedCast wt x x\u2081 x\u2082) apt (VNUFailedCast unq) = {!!}\n\n lem-subst : \u2200{\u0394 \u0393 x \u03c41 d1 \u03c4 d2 } \u2192\n x # \u0393 \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 d1 :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 [ d2 \/ x ] d1 :: \u03c4\n lem-subst apt TAConst wt2 = TAConst\n lem-subst {x = x} apt (TAVar {x = x'} x\u2082) wt2 with natEQ x' x\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inl refl with lem-apart-union-eq {\u0393 = \u0393} apt x\u2083\n lem-subst apt (TAVar x\u2083) wt2 | Inl refl | refl = wt2\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inr x\u2082 = TAVar (lem-neq-union-eq {\u0393 = \u0393} x\u2082 x\u2083)\n lem-subst {\u0394 = \u0394} {\u0393 = \u0393} {x = x} {d2 = d2} x#\u0393 (TALam {x = y} {\u03c41 = \u03c41} {d = d} {\u03c42 = \u03c42} x\u2082 wt1) wt2\n with lem-union-none {\u0393 = \u0393} x\u2082\n ... | x\u2260y , y#\u0393 with natEQ y x\n ... | Inl eq = abort (x\u2260y (! eq))\n ... | Inr _ = TALam y#\u0393 (lem-subst {\u0394 = \u0394} {\u0393 = \u0393 ,, (y , \u03c41)} {x = x} {d1 = d} (apart-extend1 \u0393 x\u2260y x#\u0393) (exchange-ta-\u0393 {\u0393 = \u0393} x\u2260y wt1) (weaken-ta {!!} wt2))\n lem-subst apt (TAAp wt1 wt2) wt3 = TAAp (lem-subst apt wt1 wt3) (lem-subst apt wt2 wt3)\n lem-subst apt (TAEHole in\u0394 sub) wt2 = TAEHole in\u0394 (STASubst sub wt2)\n lem-subst apt (TANEHole x\u2081 wt1 x\u2082) wt2 = TANEHole x\u2081 (lem-subst apt wt1 wt2) (STASubst x\u2082 wt2)\n lem-subst apt (TACast wt1 x\u2081) wt2 = TACast (lem-subst apt wt1 wt2) x\u2081\n lem-subst apt (TAFailedCast wt1 x\u2081 x\u2082 x\u2083) wt2 = TAFailedCast (lem-subst apt wt1 wt2) x\u2081 x\u2082 x\u2083\n","old_contents":"open import Prelude\nopen import Nat\nopen import List\nopen import core\nopen import contexts\nopen import weakening\nopen import exchange\nopen import lemmas-disjointness\n\nmodule lemmas-subst-ta where\n -- todo: add a premise from a new judgement like holes disjoint that\n -- classifies pairs of dhexps that share no variable names\n -- whatsoever. that should imply freshness here. then propagate that\n -- change to preservation. this is morally what \u03b1-equiv lets us do in a\n -- real setting.\n\n -- the variable name x does not appear in the term d\n data var-name-new : (x : Nat) (d : dhexp) \u2192 Set where\n VNNConst : \u2200{x} \u2192 var-name-new x c\n VNNVar : \u2200{x y} \u2192 x \u2260 y \u2192 var-name-new x (X y)\n VNNLam2 : \u2200{x d y \u03c4} \u2192 x \u2260 y\n \u2192 var-name-new x d\n \u2192 var-name-new x (\u00b7\u03bb_[_]_ y \u03c4 d)\n VNNHole : \u2200{x u \u03c3} \u2192 var-name-new x (\u2987\u2988\u27e8 u , \u03c3 \u27e9) -- todo something about \u03c3?\n VNNNEHole : \u2200{x u \u03c3 d } \u2192\n var-name-new x d \u2192\n var-name-new x (\u2987 d \u2988\u27e8 u , \u03c3 \u27e9) -- todo something about \u03c3?\n VNNAp : \u2200{ x d1 d2 } \u2192\n var-name-new x d1 \u2192\n var-name-new x d2 \u2192\n var-name-new x (d1 \u2218 d2)\n VNNCast : \u2200{x d \u03c41 \u03c42} \u2192 var-name-new x d \u2192 var-name-new x (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9)\n VNNFailedCast : \u2200{x d \u03c41 \u03c42} \u2192 var-name-new x d \u2192 var-name-new x (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n\n -- two terms that do not share any hole names\n data var-names-disjoint : (d1 : dhexp) \u2192 (d2 : dhexp) \u2192 Set where\n VNDConst : \u2200{d} \u2192 var-names-disjoint c d\n VNDVar : \u2200{x d} \u2192 var-names-disjoint (X x) d\n VNDLam : \u2200{x \u03c4 d1 d2} \u2192 var-names-disjoint d1 d2\n \u2192 var-names-disjoint (\u00b7\u03bb_[_]_ x \u03c4 d1) d2\n VNDHole : \u2200{u \u03c3 d2} \u2192 var-name-new u d2\n \u2192 var-names-disjoint (\u2987\u2988\u27e8 u , \u03c3 \u27e9) d2 -- todo something about \u03c3?\n VNDNEHole : \u2200{u \u03c3 d1 d2} \u2192 var-name-new u d2\n \u2192 var-names-disjoint d1 d2\n \u2192 var-names-disjoint (\u2987 d1 \u2988\u27e8 u , \u03c3 \u27e9) d2 -- todo something about \u03c3?\n VNDAp : \u2200{d1 d2 d3} \u2192 var-names-disjoint d1 d3\n \u2192 var-names-disjoint d2 d3\n \u2192 var-names-disjoint (d1 \u2218 d2) d3\n\n -- all the variable names in the term are unique\n data var-names-unique : dhexp \u2192 Set where\n VNUHole : var-names-unique c\n VNUVar : \u2200{x} \u2192 var-names-unique (X x)\n VNULam : {x : Nat} {\u03c4 : htyp} {d : dhexp} \u2192 var-names-unique d\n \u2192 var-name-new x d\n \u2192 var-names-unique (\u00b7\u03bb_[_]_ x \u03c4 d)\n VNUEHole : \u2200{u \u03c3} \u2192 var-names-unique (\u2987\u2988\u27e8 u , \u03c3 \u27e9) -- todo something about \u03c3?\n VNUNEHole : \u2200{u \u03c3 d} \u2192 var-names-unique d\n \u2192 var-names-unique (\u2987 d \u2988\u27e8 u , \u03c3 \u27e9) -- todo something about \u03c3?\n VNUAp : \u2200{d1 d2} \u2192 var-names-unique d1\n \u2192 var-names-unique d2\n \u2192 var-names-disjoint d1 d2\n \u2192 var-names-unique (d1 \u2218 d2)\n VNUCast : \u2200{d \u03c41 \u03c42} \u2192 var-names-unique d\n \u2192 var-names-unique (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9)\n VNUFailedCast : \u2200{d \u03c41 \u03c42} \u2192 var-names-unique d\n \u2192 var-names-unique (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n\n lem-subst : \u2200{\u0394 \u0393 x \u03c41 d1 \u03c4 d2 } \u2192\n x # \u0393 \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 d1 :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 [ d2 \/ x ] d1 :: \u03c4\n lem-subst apt TAConst wt2 = TAConst\n lem-subst {x = x} apt (TAVar {x = x'} x\u2082) wt2 with natEQ x' x\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inl refl with lem-apart-union-eq {\u0393 = \u0393} apt x\u2083\n lem-subst apt (TAVar x\u2083) wt2 | Inl refl | refl = wt2\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inr x\u2082 = TAVar (lem-neq-union-eq {\u0393 = \u0393} x\u2082 x\u2083)\n lem-subst {\u0394 = \u0394} {\u0393 = \u0393} {x = x} {d2 = d2} x#\u0393 (TALam {x = y} {\u03c41 = \u03c41} {d = d} {\u03c42 = \u03c42} x\u2082 wt1) wt2\n with lem-union-none {\u0393 = \u0393} x\u2082\n ... | x\u2260y , y#\u0393 with natEQ y x\n ... | Inl eq = abort (x\u2260y (! eq))\n ... | Inr _ = TALam y#\u0393 (lem-subst {\u0394 = \u0394} {\u0393 = \u0393 ,, (y , \u03c41)} {x = x} {d1 = d} (apart-extend1 \u0393 x\u2260y x#\u0393) (exchange-ta-\u0393 {\u0393 = \u0393} x\u2260y wt1) (weaken-ta {!!} wt2))\n lem-subst apt (TAAp wt1 wt2) wt3 = TAAp (lem-subst apt wt1 wt3) (lem-subst apt wt2 wt3)\n lem-subst apt (TAEHole in\u0394 sub) wt2 = TAEHole in\u0394 (STASubst sub wt2)\n lem-subst apt (TANEHole x\u2081 wt1 x\u2082) wt2 = TANEHole x\u2081 (lem-subst apt wt1 wt2) (STASubst x\u2082 wt2)\n lem-subst apt (TACast wt1 x\u2081) wt2 = TACast (lem-subst apt wt1 wt2) x\u2081\n lem-subst apt (TAFailedCast wt1 x\u2081 x\u2082 x\u2083) wt2 = TAFailedCast (lem-subst apt wt1 wt2) x\u2081 x\u2082 x\u2083\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"13d78b19995ed646b53097d73e796ce8c886e695","subject":"Callback0 -> JS!","message":"Callback0 -> JS!\n","repos":"crypto-agda\/agda-libjs","old_file":"lib\/FFI\/JS\/Proc.agda","new_file":"lib\/FFI\/JS\/Proc.agda","new_contents":"module FFI.JS.Proc where\n\nopen import FFI.JS\nopen import Control.Process.Type\n\nabstract\n URI = String\n showURI : URI \u2192 String\n showURI x = x\n readURI : String \u2192 URI\n readURI x = x\n\nJSProc = Proc URI JSValue\n\n{-\npostulate server : (ip port : String)\n (proc : URI \u2192 JSProc)\n (callback : URI \u2192 JSCmd) \u2192 JSCmd\n{-# COMPILED_JS server require(\"proc\").serverCurry #-}\n\npostulate client : JSProc \u2192 JSCmd \u2192 JSCmd\n{-# COMPILED_JS client require(\"proc\").clientCurry #-}\n-}\n\npostulate server : (ip port : String)\n (proc : URI \u2192 JSProc)\n \u2192 Callback1 URI\n{-# COMPILED_JS server require(\"proc\").serverCurry #-}\n\npostulate client : JSProc \u2192 JS!\n{-# COMPILED_JS client require(\"proc\").clientCurry #-}\n","old_contents":"module FFI.JS.Proc where\n\nopen import FFI.JS\nopen import Control.Process.Type\n\nabstract\n URI = String\n showURI : URI \u2192 String\n showURI x = x\n readURI : String \u2192 URI\n readURI x = x\n\nJSProc = Proc URI JSValue\n\n{-\npostulate server : (ip port : String)\n (proc : URI \u2192 JSProc)\n (callback : URI \u2192 JSCmd) \u2192 JSCmd\n{-# COMPILED_JS server require(\"proc\").serverCurry #-}\n\npostulate client : JSProc \u2192 JSCmd \u2192 JSCmd\n{-# COMPILED_JS client require(\"proc\").clientCurry #-}\n-}\n\npostulate server : (ip port : String)\n (proc : URI \u2192 JSProc)\n \u2192 Callback1 URI\n{-# COMPILED_JS server require(\"proc\").serverCurry #-}\n\npostulate client : JSProc \u2192 Callback0\n{-# COMPILED_JS client require(\"proc\").clientCurry #-}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"9bf7b311f51bcaf7be6e3f874a7f24f1f1ad99d9","subject":"Testing automatic proofs with streams.","message":"Testing automatic proofs with streams.\n\nIgnore-this: 19fe2f88ffe2f0b524f957d35eb3978e\n\ndarcs-hash:20110725165341-3bd4e-ffb75f484bb59247ff637ca3f5013c1e9fad2044.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/FOTC\/Data\/Stream\/PropertiesATP.agda","new_file":"Draft\/FOTC\/Data\/Stream\/PropertiesATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Streams properties\n------------------------------------------------------------------------------\n\nmodule Draft.FOTC.Data.Stream.PropertiesATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Base.PropertiesATP\n\nopen import FOTC.Data.Stream.Type\n\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n\npostulate\n helper\u2081 : \u2200 {ws} \u2192 (\u2203 \u03bb zs \u2192 ws \u2248 zs) \u2192\n \u2203 (\u03bb w' \u2192 \u2203 (\u03bb ws' \u2192 ws \u2261 w' \u2237 ws' \u2227 (\u2203 \u03bb zs \u2192 ws' \u2248 zs)))\n{-# ATP prove helper\u2081 #-}\n\npostulate\n helper\u2082 : \u2200 {zs} \u2192 (\u2203 \u03bb ws \u2192 ws \u2248 zs) \u2192\n \u2203 (\u03bb z' \u2192 \u2203 (\u03bb zs' \u2192 zs \u2261 z' \u2237 zs' \u2227 (\u2203 \u03bb ws \u2192 ws \u2248 zs')))\n{-# ATP prove helper\u2082 #-}\n\n\u2248\u2192Stream : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Stream xs \u2227 Stream ys\n\u2248\u2192Stream {xs} {ys} xs\u2248ys = Stream-gfp\u2082 P\u2081 helper\u2081 (ys , xs\u2248ys)\n , Stream-gfp\u2082 P\u2082 helper\u2082 (xs , xs\u2248ys)\n where\n P\u2081 : D \u2192 Set\n P\u2081 ws = \u2203 \u03bb zs \u2192 ws \u2248 zs\n\n P\u2082 : D \u2192 Set\n P\u2082 zs = \u2203 \u03bb ws \u2192 ws \u2248 zs\n","old_contents":"------------------------------------------------------------------------------\n-- Streams properties\n------------------------------------------------------------------------------\n\nmodule Draft.FOTC.Data.Stream.PropertiesATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Base.PropertiesATP\n\nopen import FOTC.Data.Stream.Type\n\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n\ntailS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\ntailS {x} {xs} h = subst Stream (sym (\u2227-proj\u2082 (\u2237-injective x\u2237xs\u2261e\u2237es))) Ses\n where\n unfold : \u2203 \u03bb e \u2192 \u2203 \u03bb es \u2192 x \u2237 xs \u2261 e \u2237 es \u2227 Stream es\n unfold = Stream-gfp\u2081 h\n\n e : D\n e = \u2203-proj\u2081 unfold\n\n es : D\n es = \u2203-proj\u2081 (\u2203-proj\u2082 unfold)\n\n x\u2237xs\u2261e\u2237es : x \u2237 xs \u2261 e \u2237 es\n x\u2237xs\u2261e\u2237es = \u2227-proj\u2081 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold))\n\n Ses : Stream es\n Ses = \u2227-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold))\n\n\u2248\u2192Stream : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Stream xs \u2227 Stream ys\n\u2248\u2192Stream {xs} {ys} xs\u2248ys = Stream-gfp\u2082 P\u2081 helper\u2081 (ys , xs\u2248ys)\n , Stream-gfp\u2082 P\u2082 helper\u2082 (xs , xs\u2248ys)\n where\n P\u2081 : D \u2192 Set\n P\u2081 ws = \u2203 \u03bb zs \u2192 ws \u2248 zs\n\n postulate\n helper\u2081 : \u2200 {ws} \u2192 P\u2081 ws \u2192\n \u2203 (\u03bb w' \u2192 \u2203 (\u03bb ws' \u2192 ws \u2261 w' \u2237 ws' \u2227 P\u2081 ws'))\n {-# ATP prove helper\u2081 #-}\n\n P\u2082 : D \u2192 Set\n P\u2082 zs = \u2203 \u03bb ws \u2192 ws \u2248 zs\n\n postulate\n helper\u2082 : \u2200 {zs} \u2192 P\u2082 zs \u2192 \u2203 (\u03bb z' \u2192 \u2203 (\u03bb zs' \u2192 zs \u2261 z' \u2237 zs' \u2227 P\u2082 zs'))\n {-# ATP prove helper\u2082 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"16e36c67b2fcfd4a473120e797a5f65761648085","subject":"Added Stream-build.","message":"Added Stream-build.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/Functors.agda","new_file":"notes\/fixed-points\/Functors.agda","new_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Based on (Vene, 2000).\n\nmodule Functors where\n\ninfixr 1 _+_\ninfixr 2 _\u00d7_\n\ndata Bool : Set where\n false true : Bool\n\ndata _+_ (A B : Set) : Set where\n inl : A \u2192 A + B\n inr : B \u2192 A + B\n\ndata _\u00d7_ (A B : Set) : Set where\n _,_ : A \u2192 B \u2192 A \u00d7 B\n\n-- The terminal object.\ndata \u22a4 : Set where\n <> : \u22a4\n\npostulate\n -- The least fixed-point.\n\n -- Haskell definitions:\n\n -- data Mu f = In (f (Mu f))\n\n -- unIn :: Mu f \u2192 f (Mu f)\n -- unIn (In x) = x\n\n \u03bc : (Set \u2192 Set) \u2192 Set\n In : {F : Set \u2192 Set} \u2192 F (\u03bc F) \u2192 \u03bc F\n unIn : {F : Set \u2192 Set} \u2192 \u03bc F \u2192 F (\u03bc F)\n\npostulate\n -- The greatest fixed-point.\n\n -- Haskell definitions:\n\n -- data Nu f = Wrap (f (Nu f))\n\n -- out :: Nu f \u2192 (f (Nu f))\n -- out (Wrap x) = x\n\n \u03bd : (Set \u2192 Set) \u2192 Set\n Wrap : {F : Set \u2192 Set} \u2192 F (\u03bd F) \u2192 \u03bd F\n out : {F : Set \u2192 Set} \u2192 \u03bd F \u2192 F (\u03bd F)\n\n------------------------------------------------------------------------------\n-- Functors\n\n-- The identity functor (the functor for the empty and unit types).\nIdF : Set \u2192 Set\nIdF X = X\n\n-- The (co)natural numbers functor.\nNatF : Set \u2192 Set\nNatF X = \u22a4 + X\n\n-- The (co)list functor.\nListF : Set \u2192 Set \u2192 Set\nListF A X = \u22a4 + A \u00d7 X\n\n-- The stream functor.\nStreamF : Set \u2192 Set \u2192 Set\nStreamF A X = A \u00d7 X\n\n------------------------------------------------------------------------------\n-- Types as least fixed-points\n\n-- The empty type is a least fixed-point.\n\u22a5 : Set\n\u22a5 = \u03bc IdF\n\n-- The natural numbers type is a least fixed-point.\nN : Set\nN = \u03bc NatF\n\n-- The data constructors for the natural numbers.\nzero : N\nzero = In (inl <>)\n\nsucc : N \u2192 N\nsucc n = In (inr n)\n\n-- The list type is a least fixed-point.\nList : Set \u2192 Set\nList A = \u03bc (ListF A)\n\n-- The data constructors for List.\nnil : {A : Set} \u2192 List A\nnil = In (inl <>)\n\ncons : {A : Set} \u2192 A \u2192 List A \u2192 List A\ncons x xs = In (inr (x , xs))\n\n------------------------------------------------------------------------------\n-- Types as greatest fixed-points\n\n-- The unit type is a greatest fixed-point.\nUnit : Set\nUnit = \u03bd IdF\n\n-- Non-structural recursion\n-- unit : Nu IdF\n-- unit = Wrap IdF {!unit!}\n\n-- The conat type is a greatest fixed-point.\nConat : Set\nConat = \u03bd NatF\n\nzeroC : Conat\nzeroC = Wrap (inl <>)\n\nsuccC : Conat \u2192 Conat\nsuccC cn = Wrap (inr cn)\n\n-- Non-structural recursion for the definition of \u221eC.\n-- \u221eC : Conat\n-- \u221eC = succC {!\u221eC!}\n\n-- The pred function is the conat destructor.\npred : Conat \u2192 \u22a4 + Conat\npred cn with out cn\n... | inl _ = inl <>\n... | inr x = inr x\n\n-- The colist type is a greatest fixed-point.\nColist : Set \u2192 Set\nColist A = \u03bd (ListF A)\n\n-- The colist data constructors.\nnilCL : {A : Set} \u2192 Colist A\nnilCL = Wrap (inl <>)\n\nconsCL : {A : Set} \u2192 A \u2192 Colist A \u2192 Colist A\nconsCL x xs = Wrap (inr (x , xs))\n\n-- The colist destructors.\nnullCL : {A : Set} \u2192 Colist A \u2192 Bool\nnullCL xs with out xs\n... | inl _ = true\n... | inr _ = false\n\n-- headCL : {A : Set} \u2192 Colist A \u2192 A\n-- headCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (x , _) = x\n\n-- tailCL : {A : Set} \u2192 Colist A \u2192 Colist A\n-- tailCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (_ , xs') = xs'\n\n-- The stream type is a greatest fixed-point.\nStream : Set \u2192 Set\nStream A = \u03bd (StreamF A)\n\n-- The stream data constructor.\nconsS : {A : Set} \u2192 A \u2192 Stream A \u2192 Stream A\nconsS x xs = Wrap (x , xs)\n\n-- The stream destructors.\nheadS : {A : Set} \u2192 Stream A \u2192 A\nheadS xs with out xs\n... | x , _ = x\n\ntailS : {A : Set} \u2192 Stream A \u2192 Stream A\ntailS xs with out xs\n... | _ , xs' = xs'\n\n-- From (Leclerc and Paulin-Mohring 1994, p. 195).\n{-# NO_TERMINATION_CHECK #-}\nStream-build :\n {A X : Set} \u2192\n (X \u2192 StreamF A X) \u2192\n X \u2192 Stream A\nStream-build f x with f x\n... | a , x' = Wrap (a , Stream-build f x')\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Leclerc, F. and Paulin-Mohring, C. (1994). Programming with Streams\n-- in Coq. A case study : the Sieve of Eratosthenes. In: Types for\n-- Proofs and Programs (TYPES \u201993). Ed. by Barendregt, H. and Nipkow,\n-- T. Vol. 806. LNCS. Springer, pp. 191\u2013212.\n--\n-- Vene, Varmo (2000). Categorical programming with inductive and\n-- coinductive types. PhD thesis. Faculty of Mathematics: University\n-- of Tartu.\n","old_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Based on (Vene, 2000).\n\nmodule Functors where\n\ninfixr 1 _+_\ninfixr 2 _\u00d7_\n\ndata Bool : Set where\n false true : Bool\n\ndata _+_ (A B : Set) : Set where\n inl : A \u2192 A + B\n inr : B \u2192 A + B\n\ndata _\u00d7_ (A B : Set) : Set where\n _,_ : A \u2192 B \u2192 A \u00d7 B\n\n-- The terminal object.\ndata \u22a4 : Set where\n <> : \u22a4\n\npostulate\n -- The least fixed-point.\n\n -- Haskell definitions:\n\n -- data Mu f = In (f (Mu f))\n\n -- unIn :: Mu f \u2192 f (Mu f)\n -- unIn (In x) = x\n\n \u03bc : (Set \u2192 Set) \u2192 Set\n In : {F : Set \u2192 Set} \u2192 F (\u03bc F) \u2192 \u03bc F\n unIn : {F : Set \u2192 Set} \u2192 \u03bc F \u2192 F (\u03bc F)\n\npostulate\n -- The greatest fixed-point.\n\n -- Haskell definitions:\n\n -- data Nu f = Wrap (f (Nu f))\n\n -- out :: Nu f \u2192 (f (Nu f))\n -- out (Wrap x) = x\n\n \u03bd : (Set \u2192 Set) \u2192 Set\n Wrap : {F : Set \u2192 Set} \u2192 F (\u03bd F) \u2192 \u03bd F\n out : {F : Set \u2192 Set} \u2192 \u03bd F \u2192 F (\u03bd F)\n\n------------------------------------------------------------------------------\n-- Functors\n\n-- The identity functor (the functor for the empty and unit types).\nIdF : Set \u2192 Set\nIdF X = X\n\n-- The (co)natural numbers functor.\nNatF : Set \u2192 Set\nNatF X = \u22a4 + X\n\n-- The (co)list functor.\nListF : Set \u2192 Set \u2192 Set\nListF A X = \u22a4 + A \u00d7 X\n\n-- The stream functor.\nStreamF : Set \u2192 Set \u2192 Set\nStreamF A X = A \u00d7 X\n\n------------------------------------------------------------------------------\n-- Types as least fixed-points\n\n-- The empty type is a least fixed-point.\n\u22a5 : Set\n\u22a5 = \u03bc IdF\n\n-- The natural numbers type is a least fixed-point.\nN : Set\nN = \u03bc NatF\n\n-- The data constructors for the natural numbers.\nzero : N\nzero = In (inl <>)\n\nsucc : N \u2192 N\nsucc n = In (inr n)\n\n-- The list type is a least fixed-point.\nList : Set \u2192 Set\nList A = \u03bc (ListF A)\n\n-- The data constructors for List.\nnil : {A : Set} \u2192 List A\nnil = In (inl <>)\n\ncons : {A : Set} \u2192 A \u2192 List A \u2192 List A\ncons x xs = In (inr (x , xs))\n\n------------------------------------------------------------------------------\n-- Types as greatest fixed-points\n\n-- The unit type is a greatest fixed-point.\nUnit : Set\nUnit = \u03bd IdF\n\n-- Non-structural recursion\n-- unit : Nu IdF\n-- unit = Wrap IdF {!unit!}\n\n-- The conat type is a greatest fixed-point.\nConat : Set\nConat = \u03bd NatF\n\nzeroC : Conat\nzeroC = Wrap (inl <>)\n\nsuccC : Conat \u2192 Conat\nsuccC cn = Wrap (inr cn)\n\n-- Non-structural recursion for the definition of \u221eC.\n-- \u221eC : Conat\n-- \u221eC = succC {!\u221eC!}\n\n-- The pred function is the conat destructor.\npred : Conat \u2192 \u22a4 + Conat\npred cn with out cn\n... | inl _ = inl <>\n... | inr x = inr x\n\n-- The colist type is a greatest fixed-point.\nColist : Set \u2192 Set\nColist A = \u03bd (ListF A)\n\n-- The colist data constructors.\nnilCL : {A : Set} \u2192 Colist A\nnilCL = Wrap (inl <>)\n\nconsCL : {A : Set} \u2192 A \u2192 Colist A \u2192 Colist A\nconsCL x xs = Wrap (inr (x , xs))\n\n-- The colist destructors.\nnullCL : {A : Set} \u2192 Colist A \u2192 Bool\nnullCL xs with out xs\n... | inl _ = true\n... | inr _ = false\n\n-- headCL : {A : Set} \u2192 Colist A \u2192 A\n-- headCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (x , _) = x\n\n-- tailCL : {A : Set} \u2192 Colist A \u2192 Colist A\n-- tailCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (_ , xs') = xs'\n\n-- The stream type is a greatest fixed-point.\nStream : Set \u2192 Set\nStream A = \u03bd (StreamF A)\n\n-- The stream data constructor.\nconsS : {A : Set} \u2192 A \u2192 Stream A \u2192 Stream A\nconsS x xs = Wrap (x , xs)\n\n-- The stream destructors.\nheadS : {A : Set} \u2192 Stream A \u2192 A\nheadS xs with out xs\n... | x , _ = x\n\ntailS : {A : Set} \u2192 Stream A \u2192 Stream A\ntailS xs with out xs\n... | _ , xs' = xs'\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Vene, Varmo (2000). Categorical programming with inductive and\n-- coinductive types. PhD thesis. Faculty of Mathematics: University\n-- of Tartu.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"bf41bcaafc48c3940d90b646ceaad93f43a83882","subject":"Fixed typo.","message":"Fixed typo.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/DistributiveLaws\/PropertiesI.agda","new_file":"src\/fot\/DistributiveLaws\/PropertiesI.agda","new_contents":"------------------------------------------------------------------------------\n-- Distributive laws properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule DistributiveLaws.PropertiesI where\n\nopen import DistributiveLaws.Base\n\n------------------------------------------------------------------------------\n-- Congruence properties\n\n-- The propositional equality is compatible with the binary operation.\n\n-- \u00b7-cong : \u2200 {a b c d} \u2192 a \u2261 b \u2192 c \u2261 d \u2192 a \u00b7 c \u2261 b \u00b7 d\n-- \u00b7-cong = cong\u2082 _\u00b7_\n\n\u00b7-leftCong : \u2200 {a b c} \u2192 a \u2261 b \u2192 a \u00b7 c \u2261 b \u00b7 c\n\u00b7-leftCong refl = refl\n\n\u00b7-rightCong : \u2200 {a b c} \u2192 b \u2261 c \u2192 a \u00b7 b \u2261 a \u00b7 c\n\u00b7-rightCong refl = refl\n","old_contents":"------------------------------------------------------------------------------\n-- Group theory properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule DistributiveLaws.PropertiesI where\n\nopen import DistributiveLaws.Base\n\n------------------------------------------------------------------------------\n-- Congruence properties\n\n-- The propositional equality is compatible with the binary operation.\n\n-- \u00b7-cong : \u2200 {a b c d} \u2192 a \u2261 b \u2192 c \u2261 d \u2192 a \u00b7 c \u2261 b \u00b7 d\n-- \u00b7-cong = cong\u2082 _\u00b7_\n\n\u00b7-leftCong : \u2200 {a b c} \u2192 a \u2261 b \u2192 a \u00b7 c \u2261 b \u00b7 c\n\u00b7-leftCong refl = refl\n\n\u00b7-rightCong : \u2200 {a b c} \u2192 b \u2261 c \u2192 a \u00b7 b \u2261 a \u00b7 c\n\u00b7-rightCong refl = refl\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d849c7f1f712c85e23c46f6ec0ec2adec592154a","subject":"Added missing modules to README.agda","message":"Added missing modules to README.agda\n\nIgnore-this: fb16a8a7c5d2c7e33af18e9d9645d7a7\n\ndarcs-hash:20110517124334-3bd4e-4e668149cae7521fd5188937e01c04544df7bcb8.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"README.agda","new_file":"README.agda","new_contents":"","old_contents":"","returncode":0,"stderr":"unknown","license":"mit","lang":"Agda"} {"commit":"709fb9f2b23ad42da6f0fbb124a140b01bf8bc3a","subject":"Drop (unused) System L","message":"Drop (unused) System L\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Syntax\/LType.agda","new_file":"Parametric\/Syntax\/LType.agda","new_contents":"","old_contents":"import Parametric.Syntax.Type as Type\n\n{--\n-- Encoding types of polarized System L, as described in \"A dissection of L\".\n-- However, I formalized contexts in a wrong way, hence there are problems.\n--}\n\nmodule Parametric.Syntax.LType where\n\n-- The treatment of base type seems questionable. We might want instead to\n-- distinguish base value types and base computation types. Or not, I'm not\n-- sure.\n\nmodule Structure (Base : Type.Structure) where\n open Type.Structure Base\n\n mutual\n -- Polarized L.\n -- Values (positive types)\n data VType : Set where\n vB : (\u03b9 : Base) \u2192 VType\n\n _\u2297_ : (\u03c4\u2081 : VType) \u2192 (\u03c4\u2082 : VType) \u2192 VType\n vUnit : VType\n\n _\u2295_ : (\u03c4\u2081 : VType) \u2192 (\u03c4\u2082 : VType) \u2192 VType\n vZero : VType\n\n ! : VType \u2192 VType\n \u2193 : (c : CType) \u2192 VType\n\n data CType : Set where\n cB : (\u03b9 : Base) \u2192 CType\n\n _\u214b_ : CType \u2192 CType \u2192 CType\n c\u22a5 : CType\n\n _&_ : CType \u2192 CType \u2192 CType\n c\u22a4 : CType\n\n \u00bf : CType \u2192 CType\n \u2191 : VType \u2192 CType\n\n _\u2020v : VType \u2192 CType\n _\u2020c : CType \u2192 VType\n\n vB \u03b9 \u2020v = cB \u03b9\n (v\u2081 \u2297 v\u2082) \u2020v = (v\u2081 \u2020v) \u214b (v\u2082 \u2020v)\n vUnit \u2020v = c\u22a5\n (v\u2081 \u2295 v\u2082) \u2020v = (v\u2081 \u2020v) & (v\u2082 \u2020v)\n vZero \u2020v = c\u22a4\n ! v \u2020v = \u00bf (v \u2020v)\n \u2193 c \u2020v = \u2191 (c \u2020c)\n\n cB \u03b9 \u2020c = vB \u03b9\n (c\u2081 \u214b c\u2082) \u2020c = c\u2081 \u2020c \u2297 c\u2082 \u2020c\n c\u22a5 \u2020c = vUnit\n (c\u2081 & c\u2082) \u2020c = (c\u2081 \u2020c) \u2295 (c\u2082 \u2020c)\n c\u22a4 \u2020c = vZero\n \u00bf c \u2020c = ! (c \u2020c)\n \u2191 v \u2020c = \u2193 (v \u2020v)\n\n open import Relation.Binary.PropositionalEquality\n\n -- This proof really calls for tactics, doesn't it?\n invV : \u2200 {v} \u2192 v \u2261 v \u2020v \u2020c\n invC : \u2200 {c} \u2192 c \u2261 c \u2020c \u2020v\n\n invV {vB \u03b9} = refl\n invV {v\u2081 \u2297 v\u2082} = cong\u2082 _\u2297_ invV invV\n invV {vUnit} = refl\n invV {v\u2081 \u2295 v\u2082} = cong\u2082 _\u2295_ invV invV\n invV {vZero} = refl\n invV { ! v} = cong ! invV\n invV {\u2193 c} = cong \u2193 invC\n\n invC {cB \u03b9} = refl\n invC {c\u2081 \u214b c\u2082} = cong\u2082 _\u214b_ invC invC\n invC {c\u22a5} = refl\n invC {c\u2081 & c\u2082} = cong\u2082 _&_ invC invC\n invC {c\u22a4} = refl\n invC {\u00bf c} = cong \u00bf invC\n invC {\u2191 x} = cong \u2191 invV\n\n open import Base.Syntax.Context VType public\n using ()\n renaming\n ( \u2205 to \u2205\u2205\n ; _\u2022_ to _\u2022\u2022_\n ; _\u22ce_ to _\u22ce\u22ce_\n ; mapContext to mapVCtx\n ; Var to VVar\n ; Context to VContext\n ; this to vThis; that to vThat)\n\n fromVar : \u2200 {\u0393 \u03c4} \u2192 (f : Type \u2192 VType) \u2192 Var \u0393 \u03c4 \u2192 VVar (mapVCtx f \u0393) (f \u03c4)\n fromVar {x \u2022 \u0393} f this = vThis\n fromVar {x \u2022 \u0393} f (that v) = vThat (fromVar f v)\n\n data VTerm : (\u039e : VContext) \u2192 (\u0393 : VContext) \u2192 VType \u2192 Set\n data CTerm : (\u039e : VContext) \u2192 (\u0393 : VContext) \u2192 CType \u2192 Set\n\n -- Each constructor of the following types encodes a rule from the paper (Fig.\n -- 4); the original names are given in comments at the end of each row.\n\n data Cmd : (\u039e : VContext) \u2192 (\u0393 : VContext) \u2192 Set where\n -- I swapped the operands compared to the cut rule in the paper (Fig. 4),\n -- but this should be purely cosmetic.\n \u27e8_\u2223_\u27e9 : \u2200 {\u039e \u0393 \u0394 \u03c4} \u2192 (v : CTerm \u039e \u0394 (\u03c4 \u2020v)) \u2192 (u : VTerm \u039e \u0393 \u03c4) \u2192 Cmd \u039e (\u0393 \u22ce\u22ce \u0394) -- cut\n\n data VTerm where\n lvar : \u2200 {\u039e A} \u2192 VTerm \u039e (A \u2022\u2022 \u2205\u2205) A -- id\n vvar : \u2200 {\u039e A} \u2192 VVar \u039e A \u2192 VTerm \u039e \u2205\u2205 A -- id'\n \u03bc\u21d1_ : \u2200 {\u039e \u0393 N} \u2192 Cmd \u039e (N \u2020c \u2022\u2022 \u0393) \u2192 VTerm \u039e \u0393 (\u2193 N) -- \u2193\n\n -- Multiplicative fragment\n _,_ : \u2200 {\u039e \u0393 \u0394 A B} \u2192 VTerm \u039e \u0393 A \u2192 VTerm \u039e \u0394 B \u2192 VTerm \u039e (\u0393 \u22ce\u22ce \u0394) (A \u2297 B) -- \u2297\n \u27e8\u27e9 : \u2200 {\u039e} \u2192 VTerm \u039e \u2205\u2205 vUnit --1\n\n -- Additive fragment\n _\u2081as_ : \u2200 {\u039e \u0393 A} \u2192 VTerm \u039e \u0393 A \u2192 \u2200 B \u2192 VTerm \u039e \u0393 (A \u2295 B) -- \u2295L\n _\u2082as_ : \u2200 {\u039e \u0393 B} \u2192 VTerm \u039e \u0393 B \u2192 \u2200 A \u2192 VTerm \u039e \u0393 (A \u2295 B) -- \u2295R\n\n -- No rule for vZero\n\n -- Exponential fragment (in the sense of linear logic)\n\n \u2983\u2984_ : \u2200 {\u039e A} \u2192 VTerm \u039e \u2205\u2205 A \u2192 VTerm \u039e \u2205\u2205 (! A) -- !\n -- : \u2200 {\u039e \u0393 A} \u2192\n\n -- Compared to the paper, we avoid using \u2020 in indexes of the result, and\n -- exploit the fact that \u2020 is involutive. See for instance \u03bc\n data CTerm where\n \u03bc : \u2200 {\u039e \u0393 N} \u2192 Cmd \u039e (N \u2020c \u2022\u2022 \u0393) \u2192 CTerm \u039e \u0393 N\n \u21d1 : \u2200 {\u039e \u0393 A} \u2192 VTerm \u039e \u0393 A \u2192 CTerm \u039e \u0393 (\u2191 A) -- \u2191\n\n -- Multiplicative fragment\n \u03bc, : \u2200 {\u039e \u0393 N M} \u2192 Cmd \u039e (N \u2020c \u2022\u2022 M \u2020c \u2022\u2022 \u0393) \u2192 CTerm \u039e \u0393 (N \u214b M) -- \u214b\n \u03bc\u27e8\u27e9 : \u2200 {\u039e \u0393} \u2192 Cmd \u039e \u0393 \u2192 CTerm \u039e \u0393 c\u22a5 -- \u22a5\n\n -- Additive fragment\n \u03bc\u2081_\u03bc\u2082_ : \u2200 {\u039e \u0393 N M} \u2192 Cmd \u039e (N \u2020c \u2022\u2022 \u0393) \u2192 Cmd \u039e (M \u2020c \u2022\u2022 \u0393) \u2192 CTerm \u039e \u0393 (N & M)\n\n -- Exponential fragment (in the sense of linear logic)\n \u03bc\u2983\u2984 : \u2200 {\u039e \u0393 N} \u2192 Cmd (N \u2020c \u2022\u2022 \u039e) \u0393 \u2192 CTerm \u039e \u0393 (\u00bf N) -- ?\n\n -- : \u2200 {\u039e \u0393 \u03c4} \u2192\n\n cast-invV : \u2200 {\u039e \u0393 N} \u2192 VTerm \u039e \u0393 N \u2192 VTerm \u039e \u0393 ((N \u2020v) \u2020c)\n cast-invV {\u039e} {\u0393} t = subst (VTerm \u039e \u0393) invV t\n\n cast-invV2 : \u2200 {\u039e \u0393 N} \u2192 VTerm \u039e \u0393 ((N \u2020v) \u2020c) \u2192 VTerm \u039e \u0393 N\n cast-invV2 {\u039e} {\u0393} t = subst (VTerm \u039e \u0393) (sym invV) t\n\n cast-invC : \u2200 {\u039e \u0393 N} \u2192 CTerm \u039e \u0393 N \u2192 CTerm \u039e \u0393 ((N \u2020c) \u2020v)\n cast-invC {\u039e} {\u0393} t = subst (CTerm \u039e \u0393) invC t\n\n -- Check that type rules match by doing eta-expansion.\n -- (Is this checking harmony?)\n \u03b7-\u03bc : \u2200 {\u039e \u0393 N} \u2192 CTerm \u039e \u0393 N \u2192 CTerm \u039e \u0393 N\n \u03b7-\u03bc t = \u03bc \u27e8 cast-invC t \u2223 lvar \u27e9\n\n maybe-\u03b7-\u03bc\u21d1 : \u2200 {\u039e \u0393 A} \u2192 VTerm \u039e \u0393 A \u2192 VTerm \u039e \u0393 (\u2193 (\u2191 A))\n maybe-\u03b7-\u03bc\u21d1 t = \u03bc\u21d1 \u27e8 cast-invC (\u21d1 t) \u2223 lvar \u27e9\n\n -- A \"thunk\" combinator (from paper)\n \u21d3 : \u2200 {\u039e \u0393 N} \u2192 CTerm \u039e \u0393 N \u2192 VTerm \u039e \u0393 (\u2193 N)\n \u21d3 t = \u03bc\u21d1 \u27e8 cast-invC t \u2223 lvar \u27e9\n\n open import UNDEFINED\n\n -- from paper\n force : \u2200 {\u039e \u0393 N} \u2192 VTerm \u039e \u0393 (\u2193 N) \u2192 CTerm \u039e \u0393 N\n force t = reveal UNDEFINED -- {! \u03bc \u27e8 \u21d1 lvar \u2223 t \u27e9 !}\n\n -- XXX Here we \"just\" need to reorder variables in contexts. OMG, I screwed up\n -- (which is bad), or they use implicit exchange (which would be worse).\n \u03b7-\u03bc\u21d1 : \u2200 {\u039e \u0393 N} \u2192 VTerm \u039e \u0393 (\u2193 N) \u2192 VTerm \u039e \u0393 (\u2193 N)\n \u03b7-\u03bc\u21d1 t = \u03bc\u21d1 (reveal UNDEFINED) -- {! \u27e8 \u21d1 lvar \u2223 t \u27e9 !}\n\n n-\u03bc, : \u2200 {\u039e \u0393 N M} \u2192 CTerm \u039e \u0393 (N \u214b M) \u2192 CTerm \u039e \u0393 (N \u214b M)\n n-\u03bc, t = \u03bc, \u27e8 cast-invC t \u2223 lvar , lvar \u27e9 -- The two lvars are in different contexts, so they have different types!\n\n \u03b7-\u03bc\u27e8\u27e9 : \u2200 {\u039e \u0393} \u2192 CTerm \u039e \u0393 c\u22a5 \u2192 CTerm \u039e \u0393 c\u22a5\n \u03b7-\u03bc\u27e8\u27e9 t = \u03bc\u27e8\u27e9 \u27e8 t \u2223 \u27e8\u27e9 \u27e9\n\n \u03b7-\u03bc\u2983\u2984 : \u2200 {\u039e \u0393 N} \u2192 CTerm \u039e \u0393 (\u00bf N) \u2192 CTerm \u039e \u0393 (\u00bf N)\n \u03b7-\u03bc\u2983\u2984 t = \u03bc\u2983\u2984 \u27e8 cast-invC (reveal UNDEFINED) {- weaken t -} \u2223 \u2983\u2984 (vvar vThis) \u27e9\n\n \u03b2-expand-\u03bc\u27e8\u27e9 : \u2200 {\u039e \u0393} \u2192 Cmd \u039e \u0393 \u2192 Cmd \u039e \u0393\n \u03b2-expand-\u03bc\u27e8\u27e9 c = \u27e8 \u03bc\u27e8\u27e9 c \u2223 \u27e8\u27e9 \u27e9\n\n -- \u03b7-rules for sums look different.\n maybe-\u03b7-\u03bc\u2081 : \u2200 {\u039e \u0393 N M} \u2192 CTerm \u039e \u0393 N \u2192 CTerm \u039e \u0393 M \u2192 CTerm \u039e \u0393 (N & M)\n maybe-\u03b7-\u03bc\u2081 t\u2081 t\u2082 = \u03bc\u2081 \u27e8 cast-invC t\u2081 \u2223 lvar \u27e9 \u03bc\u2082 \u27e8 cast-invC t\u2082 \u2223 lvar \u27e9\n\n \u03b7-\u03bc\u2081 : \u2200 {\u039e \u0393 N M} \u2192 CTerm \u039e \u0393 N \u2192 CTerm \u039e \u0393 M \u2192 CTerm \u039e \u0393 N\n \u03b7-\u03bc\u2081 {M = M} t\u2081 t\u2082 =\n \u03bc \u27e8\n \u03bc\u2081 \u27e8 cast-invC (cast-invC t\u2081) \u2223 lvar \u27e9 \u03bc\u2082 \u27e8 cast-invC (cast-invC t\u2082) \u2223 lvar \u27e9\n \u2223\n lvar \u2081as (M \u2020c)\n \u27e9\n\n \u03b7-\u03bc\u2082 : \u2200 {\u039e \u0393 N M} \u2192 CTerm \u039e \u0393 N \u2192 CTerm \u039e \u0393 M \u2192 CTerm \u039e \u0393 M\n \u03b7-\u03bc\u2082 {N = N} t\u2081 t\u2082 =\n \u03bc \u27e8\n \u03bc\u2081 \u27e8 cast-invC (cast-invC t\u2081) \u2223 lvar \u27e9 \u03bc\u2082 \u27e8 cast-invC (cast-invC t\u2082) \u2223 lvar \u27e9\n \u2223\n lvar \u2082as (N \u2020c)\n \u27e9\n\n -- Some syntactic sugar.\n _\u22b8_ : VType \u2192 CType \u2192 CType\n \u03c3 \u22b8 \u03c4 = (\u03c3 \u2020v) \u214b \u03c4\n\n _\u21db_ : VType \u2192 CType \u2192 CType\n \u03c3 \u21db \u03c4 = ! \u03c3 \u22b8 \u03c4\n\n\n -- Not checked again yet. This code comes from CBPV and was refactored for\n -- renamings done to get polarized L, so it might or might not work.\n\n cbnToCType : Type \u2192 CType\n cbnToCType (base \u03b9) = \u2191 (vB \u03b9)\n cbnToCType (\u03c3 \u21d2 \u03c4) = \u2193 (cbnToCType \u03c3) \u21db cbnToCType \u03c4\n\n cbvToVType : Type \u2192 VType\n cbvToVType (base \u03b9) = vB \u03b9\n cbvToVType (\u03c3 \u21d2 \u03c4) = \u2193 (cbvToVType \u03c3 \u21db \u2191 (cbvToVType \u03c4))\n\n cbnToVType : Type \u2192 VType\n cbnToVType \u03c4 = \u2193 (cbnToCType \u03c4)\n\n cbvToCType : Type \u2192 CType\n cbvToCType \u03c4 = \u2191 (cbvToVType \u03c4)\n\n fromCBNCtx : Context \u2192 VContext\n fromCBNCtx \u0393 = mapVCtx cbnToVType \u0393\n\n fromCBVCtx : Context \u2192 VContext\n fromCBVCtx \u0393 = mapVCtx cbvToVType \u0393\n\n open import Data.List\n open Data.List using (List) public\n fromCBVToCompList : Context \u2192 List CType\n fromCBVToCompList \u0393 = mapVCtx cbvToCType \u0393\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"51432b971511d6e3b28904b1d33fc55d32888906","subject":"FunExt: add a version with implicit arguments","message":"FunExt: add a version with implicit arguments\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Function\/Extensionality.agda","new_file":"lib\/Function\/Extensionality.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule Function.Extensionality where\n\nopen import Function.NP\nopen import Relation.Binary.PropositionalEquality.NP -- renaming (subst to tr)\n\nhapply : \u2200 {a b}{A : Set a}{B : A \u2192 Set b}{f g : (x : A) \u2192 B x}\n \u2192 f \u2261 g \u2192 (x : A) \u2192 f x \u2261 g x\nhapply p x = ap (\u03bb f \u2192 f x) p\n\npostulate\n FunExt : Set\n \u03bb= : \u2200 {a b}{A : Set a}{B : A \u2192 Set b}{f\u2080 f\u2081 : (x : A) \u2192 B x}{{fe : FunExt}}\n (f= : \u2200 x \u2192 f\u2080 x \u2261 f\u2081 x) \u2192 f\u2080 \u2261 f\u2081\n\n happly-\u03bb= : \u2200 {a b}{A : Set a}{B : A \u2192 Set b}{f g : (x : A) \u2192 B x}{{fe : FunExt}}\n (fg : \u2200 x \u2192 f x \u2261 g x) \u2192 happly (\u03bb= fg) \u2261 fg\n\n \u03bb=-happly : \u2200 {a b}{A : Set a}{B : A \u2192 Set b}{f g : (x : A) \u2192 B x}{{fe : FunExt}}\n (\u03b1 : f \u2261 g) \u2192 \u03bb= (happly \u03b1) \u2261 \u03b1\n\n -- This should be derivable if I had a proper proof of \u03bb=\n tr-\u03bb= : \u2200 {a b p}{A : Set a}{B : A \u2192 Set b}{x}(P : B x \u2192 Set p)\n {f g : (x : A) \u2192 B x}{{fe : FunExt}}(fg : (x : A) \u2192 f x \u2261 g x)\n \u2192 tr (\u03bb f \u2192 P (f x)) (\u03bb= fg) \u2261 tr P (fg x)\n\n\n!-\u03b1-\u03bb= : \u2200 {a b}{A : Set a}{B : A \u2192 Set b}{f g : (x : A) \u2192 B x}{{fe : FunExt}}\n (\u03b1 : f \u2261 g) \u2192 ! \u03b1 \u2261 \u03bb= (!_ \u2218 happly \u03b1)\n!-\u03b1-\u03bb= refl = ! \u03bb=-happly refl\n\n!-\u03bb= : \u2200 {a b}{A : Set a}{B : A \u2192 Set b}{f g : (x : A) \u2192 B x}{{fe : FunExt}}\n (fg : \u2200 x \u2192 f x \u2261 g x) \u2192 ! (\u03bb= fg) \u2261 \u03bb= (!_ \u2218 fg)\n!-\u03bb= fg = !-\u03b1-\u03bb= (\u03bb= fg) \u2219 ap \u03bb= (\u03bb= (\u03bb x \u2192 ap !_ (happly (happly-\u03bb= fg) x)))\n\nmodule _ {a b}{A : Set a}{B : A \u2192 Set b}{f\u2080 f\u2081 : (x : A) \u2192 B x}{{fe : FunExt}} where\n \u03bb=\u2071 : (f= : \u2200 {x} \u2192 f\u2080 x \u2261 f\u2081 x) \u2192 f\u2080 \u2261 f\u2081\n \u03bb=\u2071 f= = \u03bb= \u03bb x \u2192 f= {x}\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule Function.Extensionality where\n\nopen import Function.NP\nopen import Relation.Binary.PropositionalEquality.NP -- renaming (subst to tr)\n\nhapply : \u2200 {a b}{A : Set a}{B : A \u2192 Set b}{f g : (x : A) \u2192 B x}\n \u2192 f \u2261 g \u2192 (x : A) \u2192 f x \u2261 g x\nhapply p x = ap (\u03bb f \u2192 f x) p\n\npostulate\n FunExt : Set\n \u03bb= : \u2200 {a b}{A : Set a}{B : A \u2192 Set b}{f\u2080 f\u2081 : (x : A) \u2192 B x}{{fe : FunExt}}\n (f= : \u2200 x \u2192 f\u2080 x \u2261 f\u2081 x) \u2192 f\u2080 \u2261 f\u2081\n\n happly-\u03bb= : \u2200 {a b}{A : Set a}{B : A \u2192 Set b}{f g : (x : A) \u2192 B x}{{fe : FunExt}}\n (fg : \u2200 x \u2192 f x \u2261 g x) \u2192 happly (\u03bb= fg) \u2261 fg\n\n \u03bb=-happly : \u2200 {a b}{A : Set a}{B : A \u2192 Set b}{f g : (x : A) \u2192 B x}{{fe : FunExt}}\n (\u03b1 : f \u2261 g) \u2192 \u03bb= (happly \u03b1) \u2261 \u03b1\n\n -- This should be derivable if I had a proper proof of \u03bb=\n tr-\u03bb= : \u2200 {a b p}{A : Set a}{B : A \u2192 Set b}{x}(P : B x \u2192 Set p)\n {f g : (x : A) \u2192 B x}{{fe : FunExt}}(fg : (x : A) \u2192 f x \u2261 g x)\n \u2192 tr (\u03bb f \u2192 P (f x)) (\u03bb= fg) \u2261 tr P (fg x)\n\n\n!-\u03b1-\u03bb= : \u2200 {a b}{A : Set a}{B : A \u2192 Set b}{f g : (x : A) \u2192 B x}{{fe : FunExt}}\n (\u03b1 : f \u2261 g) \u2192 ! \u03b1 \u2261 \u03bb= (!_ \u2218 happly \u03b1)\n!-\u03b1-\u03bb= refl = ! \u03bb=-happly refl\n\n!-\u03bb= : \u2200 {a b}{A : Set a}{B : A \u2192 Set b}{f g : (x : A) \u2192 B x}{{fe : FunExt}}\n (fg : \u2200 x \u2192 f x \u2261 g x) \u2192 ! (\u03bb= fg) \u2261 \u03bb= (!_ \u2218 fg)\n!-\u03bb= fg = !-\u03b1-\u03bb= (\u03bb= fg) \u2219 ap \u03bb= (\u03bb= (\u03bb x \u2192 ap !_ (happly (happly-\u03bb= fg) x)))\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"3ed5e78d2041cfe6ec4072cfe37bc4f1950cd73f","subject":"[ notes ] Commented out an unprovable conjecture","message":"[ notes ] Commented out an unprovable conjecture\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/FOT\/FOTC\/Data\/Nat\/Pow.agda","new_file":"notes\/FOT\/FOTC\/Data\/Nat\/Pow.agda","new_contents":"------------------------------------------------------------------------------\n-- Some proofs related to the power function\n------------------------------------------------------------------------------\n\n{-# OPTIONS --exact-split #-}\n{-# OPTIONS --no-sized-types #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Data.Nat.Pow where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.TotalityATP\n\n------------------------------------------------------------------------------\n\npostulate\n _^_ : D \u2192 D \u2192 D\n ^-0 : \u2200 n \u2192 n ^ zero \u2261 1'\n ^-S : \u2200 m n \u2192 m ^ succ\u2081 n \u2261 m * m ^ n\n{-# ATP axiom ^-0 ^-S #-}\n\nthm\u2081 : \u2200 {n} \u2192 N n \u2192 5' \u2264 n \u2192 n ^ 5' \u2264 5' ^ n\nthm\u2081 nzero h = prf\n where postulate prf : zero ^ 5' \u2264 5' ^ zero\n {-# ATP prove prf #-}\nthm\u2081 (nsucc {n} Nn) h = prf (thm\u2081 Nn)\n where postulate prf : (5' \u2264 n \u2192 n ^ 5' \u2264 5' ^ n) \u2192\n (succ\u2081 n) ^ 5' \u2264 5' ^ (succ\u2081 n)\n -- 09 December 2014: The ATPs could not prove the theorem (240 sec).\n -- {-# ATP prove prf 5-N #-}\n\nthm\u2082 : \u2200 {n} \u2192 N n \u2192\n ((2' ^ n) \u2238 1') + 1' + ((2' ^ n) \u2238 1') \u2261 2' ^ (n + 1') \u2238 1'\nthm\u2082 nzero = prf\n where\n postulate prf : ((2' ^ zero) \u2238 1') + 1' + ((2' ^ zero) \u2238 1') \u2261\n 2' ^ (zero + 1') \u2238 1'\n {-# ATP prove prf #-}\nthm\u2082 (nsucc {n} Nn) = prf (thm\u2082 Nn)\n where\n postulate prf : ((2' ^ n) \u2238 1') + 1' + ((2' ^ n) \u2238 1') \u2261\n 2' ^ (n + 1') \u2238 1' \u2192\n ((2' ^ succ\u2081 n) \u2238 1') + 1' + ((2' ^ succ\u2081 n) \u2238 1') \u2261\n 2' ^ (succ\u2081 n + 1') \u2238 1'\n -- 09 December 2014: The ATPs could not prove the theorem (240 sec).\n -- {-# ATP prove prf #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Some proofs related to the power function\n------------------------------------------------------------------------------\n\n{-# OPTIONS --exact-split #-}\n{-# OPTIONS --no-sized-types #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Data.Nat.Pow where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.TotalityATP\n\n------------------------------------------------------------------------------\n\npostulate\n _^_ : D \u2192 D \u2192 D\n ^-0 : \u2200 n \u2192 n ^ zero \u2261 1'\n ^-S : \u2200 m n \u2192 m ^ succ\u2081 n \u2261 m * m ^ n\n{-# ATP axiom ^-0 ^-S #-}\n\nthm\u2081 : \u2200 {n} \u2192 N n \u2192 5' \u2264 n \u2192 n ^ 5' \u2264 5' ^ n\nthm\u2081 nzero h = prf\n where postulate prf : zero ^ 5' \u2264 5' ^ zero\n {-# ATP prove prf #-}\nthm\u2081 (nsucc {n} Nn) h = prf (thm\u2081 Nn)\n where postulate prf : (5' \u2264 n \u2192 n ^ 5' \u2264 5' ^ n) \u2192\n (succ\u2081 n) ^ 5' \u2264 5' ^ (succ\u2081 n)\n -- 09 December 2014: The ATPs could not prove the theorem (240 sec).\n -- {-# ATP prove prf 5-N #-}\n\nthm\u2082 : \u2200 {n} \u2192 N n \u2192\n ((2' ^ n) \u2238 1') + 1' + ((2' ^ n) \u2238 1') \u2261 2' ^ (n + 1') \u2238 1'\nthm\u2082 nzero = prf\n where\n postulate prf : ((2' ^ zero) \u2238 1') + 1' + ((2' ^ zero) \u2238 1') \u2261\n 2' ^ (zero + 1') \u2238 1'\n {-# ATP prove prf #-}\nthm\u2082 (nsucc {n} Nn) = prf (thm\u2082 Nn)\n where\n postulate prf : ((2' ^ n) \u2238 1') + 1' + ((2' ^ n) \u2238 1') \u2261\n 2' ^ (n + 1') \u2238 1' \u2192\n ((2' ^ succ\u2081 n) \u2238 1') + 1' + ((2' ^ succ\u2081 n) \u2238 1') \u2261\n 2' ^ (succ\u2081 n + 1') \u2238 1'\n -- 09 December 2014: The ATPs could not prove the theorem (240 sec).\n {-# ATP prove prf #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f313f949b97f138dd32728e2129de3db80950f64","subject":"Fin: _=?_, iterate","message":"Fin: _=?_, iterate\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Fin\/NP.agda","new_file":"lib\/Data\/Fin\/NP.agda","new_contents":"-- NOTE with-K\nmodule Data.Fin.NP where\n\nopen import Type hiding (\u2605)\nopen import Function\nopen import Data.Zero\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Fin public renaming (to\u2115 to Fin\u25b9\u2115)\nopen import Data.Nat.NP using (\u2115; zero; suc; _<=_; module \u2115\u00b0) renaming (_+_ to _+\u2115_)\nopen import Data.Two using (\ud835\udfda; 0\u2082; 1\u2082; [0:_1:_]; case_0:_1:_)\nimport Data.Vec.NP as Vec\nopen Vec using (Vec; []; _\u2237_; _\u2237\u02b3_; allFin; lookup; rot\u2081; tabulate; foldr) renaming (map to vmap)\nimport Data.Vec.Properties as Vec\nopen import Data.Maybe.NP\nopen import Data.Sum as Sum\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\nopen import Relation.Binary.PropositionalEquality as \u2261\n\nsuc-injective : \u2200 {m}{i j : Fin m} \u2192 Fin.suc i \u2261 suc j \u2192 i \u2261 j\nsuc-injective refl = refl\n\n-- The isomorphisms about Fin, \ud835\udfd8, \ud835\udfd9, \ud835\udfda are in Function.Related.TypeIsomorphisms.NP\n\nFin\u25b9\ud835\udfd8 : Fin 0 \u2192 \ud835\udfd8\nFin\u25b9\ud835\udfd8 ()\n\n\ud835\udfd8\u25b9Fin : \ud835\udfd8 \u2192 Fin 0\n\ud835\udfd8\u25b9Fin ()\n\nFin\u25b9\ud835\udfd9 : Fin 1 \u2192 \ud835\udfd9\nFin\u25b9\ud835\udfd9 _ = _\n\n\ud835\udfd9\u25b9Fin : \ud835\udfd9 \u2192 Fin 1\n\ud835\udfd9\u25b9Fin _ = zero\n\nFin\u25b9\ud835\udfda : Fin 2 \u2192 \ud835\udfda\nFin\u25b9\ud835\udfda zero = 0\u2082\nFin\u25b9\ud835\udfda (suc _) = 1\u2082\n\n\ud835\udfda\u25b9Fin : \ud835\udfda \u2192 Fin 2\n\ud835\udfda\u25b9Fin = [0: # 0 1: # 1 ]\n\n_+\u2032_ : \u2200 {m n} (x : Fin m) (y : Fin n) \u2192 Fin (m +\u2115 n)\n_+\u2032_ {suc m} {n} zero y rewrite \u2115\u00b0.+-comm (suc m) n = inject+ _ y\nsuc x +\u2032 y = suc (x +\u2032 y)\n\n_\u225f_ : \u2200 {n} (i j : Fin n) \u2192 Dec (i \u2261 j)\nzero \u225f zero = yes refl\nzero \u225f suc j = no (\u03bb())\nsuc i \u225f zero = no (\u03bb())\nsuc i \u225f suc j with i \u225f j\nsuc i \u225f suc j | yes p = yes (cong suc p)\nsuc i \u225f suc j | no \u00acp = no (\u00acp \u2218 suc-injective)\n\n_==_ : \u2200 {n} (x y : Fin n) \u2192 \ud835\udfda\nx == y = \u230a x \u225f y \u230b\n{-helper (compare x y) where\n helper : \u2200 {n} {i j : Fin n} \u2192 Ordering i j \u2192 \ud835\udfda\n helper (equal _) = 1\u2082\n helper _ = 0\u2082-}\n\nswap : \u2200 {i} (x y : Fin i) \u2192 Fin i \u2192 Fin i\nswap x y z = case x == z 0: (case y == z 0: z 1: x) 1: y\n\nmodule _ {a} {A : \u2605 a}\n (B : \u2115 \u2192 \u2605\u2080)\n (_\u25c5_ : \u2200 {n} \u2192 A \u2192 B n \u2192 B (suc n))\n (\u03b5 : B zero) where\n iterate : \u2200 {n} (f : Fin n \u2192 A) \u2192 B n\n iterate {zero} f = \u03b5\n iterate {suc n} f = f zero \u25c5 iterate (f \u2218 suc)\n\n iterate-foldr\u2218tabulate :\n \u2200 {n} (f : Fin n \u2192 A) \u2192 iterate f \u2261 foldr B _\u25c5_ \u03b5 (tabulate f)\n iterate-foldr\u2218tabulate {zero} f = refl\n iterate-foldr\u2218tabulate {suc n} f = cong (_\u25c5_ (f zero)) (iterate-foldr\u2218tabulate (f \u2218 suc))\n\nmodule _ {a} {A : \u2605 a} (B : \u2605\u2080)\n (_\u25c5_ : A \u2192 B \u2192 B)\n (\u03b5 : B) where\n iterate\u2032 : \u2200 {n} (f : Fin n \u2192 A) \u2192 B\n iterate\u2032 f = iterate _ _\u25c5_ \u03b5 f\n\ndata FinSum m n : Fin (m +\u2115 n) \u2192 \u2605\u2080 where\n bound : (x : Fin m) \u2192 FinSum m n (inject+ n x)\n free : (x : Fin n) \u2192 FinSum m n (raise m x)\n\nopen import Relation.Binary.PropositionalEquality\n\ncmp : \u2200 m n (x : Fin (m +\u2115 n)) \u2192 FinSum m n x\ncmp zero n x = free x\ncmp (suc m) n zero = bound zero\ncmp (suc m) n (suc x) with cmp m n x\ncmp (suc m) n (suc .(inject+ n x)) | bound x = bound (suc x)\ncmp (suc m) n (suc .(raise m x)) | free x = free x\n\nmax : \u2200 n \u2192 Fin (suc n)\nmax = from\u2115\n\n-- reverse x = n \u2238 (1 + x)\nreverse : \u2200 {n} \u2192 Fin n \u2192 Fin n\nreverse {suc n} zero = from\u2115 n\nreverse {suc n} (suc x) = inject\u2081 (reverse {n} x)\n\nopen import Data.Nat\nopen import Data.Nat.Properties\nopen import Data.Fin.Props renaming (reverse to reverse-old)\n\nreverse-from\u2115 : \u2200 n \u2192 reverse (from\u2115 n) \u2261 zero\nreverse-from\u2115 zero = refl\nreverse-from\u2115 (suc n) rewrite reverse-from\u2115 n = refl\n\nreverse-inject\u2081 : \u2200 {n} (x : Fin n) \u2192 reverse (inject\u2081 x) \u2261 suc (reverse x)\nreverse-inject\u2081 zero = refl\nreverse-inject\u2081 (suc x) rewrite reverse-inject\u2081 x = refl\n\n{-\nreverse-involutive : \u2200 {n} (x : Fin n) \u2192 reverse (reverse x) \u2261 x\nreverse-involutive zero = reverse-from\u2115 _\nreverse-involutive (suc x) rewrite reverse-inject\u2081 (reverse x) | reverse-involutive x = refl\n-}\n\nreverse-lem : \u2200 {n} (x : Fin n) \u2192 Fin\u25b9\u2115 (reverse x) \u2261 n \u2238 suc (Fin\u25b9\u2115 x)\nreverse-lem zero = to-from _\nreverse-lem (suc x) rewrite inject\u2081-lemma (reverse x) = reverse-lem x\n\nFin\u25b9\u2115-\u2115-lem : \u2200 {n} (x : Fin (suc n)) \u2192 Fin\u25b9\u2115 (n \u2115- x) \u2261 n \u2238 Fin\u25b9\u2115 x\nFin\u25b9\u2115-\u2115-lem zero = to-from _\nFin\u25b9\u2115-\u2115-lem {zero} (suc ())\nFin\u25b9\u2115-\u2115-lem {suc n} (suc x) = Fin\u25b9\u2115-\u2115-lem x\n\nreverse-old-lem : \u2200 {n} (x : Fin n) \u2192 Fin\u25b9\u2115 (reverse-old x) \u2261 n \u2238 suc (Fin\u25b9\u2115 x)\nreverse-old-lem {zero} ()\nreverse-old-lem {suc n} x rewrite inject\u2264-lemma (n \u2115- x) (n\u2238m\u2264n (Fin\u25b9\u2115 x) (suc n)) = Fin\u25b9\u2115-\u2115-lem x\n\ndata FinView {n} : Fin (suc n) \u2192 \u2605\u2080 where\n `from\u2115 : FinView (from\u2115 n)\n `inject\u2081 : \u2200 x \u2192 FinView (inject\u2081 x)\n\nsucFinView : \u2200 {n} {i : Fin (suc n)} \u2192 FinView i \u2192 FinView (suc i)\nsucFinView `from\u2115 = `from\u2115\nsucFinView (`inject\u2081 x) = `inject\u2081 (suc x)\n\nfinView : \u2200 {n} \u2192 (i : Fin (suc n)) \u2192 FinView i\nfinView {zero} zero = `from\u2115\nfinView {suc n} zero = `inject\u2081 zero\nfinView {suc n} (suc i) = sucFinView (finView i)\nfinView {zero} (suc ())\n\nmodule Modulo where\n modq : \u2200 {q} \u2192 Fin (suc q) \u2192 Maybe (Fin q)\n modq {zero} _ = nothing\n modq {suc q} zero = just zero\n modq {suc q} (suc x) = map? suc (modq x)\n\n modq-inj : \u2200 {q} (i j : Fin (suc q)) \u2192 modq i \u2261 modq j \u2192 i \u2261 j\n modq-inj {zero} zero zero eq = refl\n modq-inj {zero} zero (suc ()) eq\n modq-inj {zero} (suc ()) zero eq\n modq-inj {zero} (suc ()) (suc ()) eq\n modq-inj {suc q} zero zero eq = refl\n modq-inj {suc q} zero (suc j) eq with modq j\n modq-inj {suc q} zero (suc j) () | nothing\n modq-inj {suc q} zero (suc j) () | just j'\n modq-inj {suc q} (suc i) zero eq with modq i\n modq-inj {suc q} (suc i) zero () | just x\n modq-inj {suc q} (suc i) zero () | nothing\n modq-inj {suc q} (suc i) (suc j) eq with modq i | modq j | modq-inj i j\n modq-inj {suc q} (suc i) (suc j) eq | just x | just x\u2081 | p = cong suc (p (cong just (suc-injective (just-injective eq))))\n modq-inj {suc q} (suc i) (suc j) () | just x | nothing | p\n modq-inj {suc q} (suc i) (suc j) () | nothing | just x | p\n modq-inj {suc q} (suc i) (suc j) eq | nothing | nothing | p = cong suc (p refl)\n\n modq\u2032 : \u2200 {q} \u2192 Fin (suc q) \u2192 Fin (suc q)\n modq\u2032 {zero} _ = zero\n modq\u2032 {suc q} zero = suc zero\n modq\u2032 {suc q} (suc x) = lift 1 suc (modq\u2032 x)\n\n modqq : \u2200 {q} \u2192 Fin q \u2192 Fin q\n modqq {zero} x = x\n modqq {suc q} x = modq\u2032 x\n\n -- Maybe (Fin n) \u2245 Fin (suc n)\n\n modq\u2032\u2032 : \u2200 {q} \u2192 Fin (suc q) \u2192 Maybe (Fin q)\n modq\u2032\u2032 x with modq\u2032 x\n ... | zero = nothing\n ... | suc y = just y\n\n zero\u2203 : \u2200 {q} \u2192 Fin q \u2192 Fin q\n zero\u2203 {zero} ()\n zero\u2203 {suc q} _ = zero\n\n sucmod : \u2200 {q} \u2192 Fin q \u2192 Fin q\n sucmod x with modq (suc x)\n ... | nothing = zero\u2203 x\n ... | just y = y\n\n modq-suc : \u2200 {q} (i j : Fin q) \u2192 modq (suc i) \u2262 just (zero\u2203 j)\n modq-suc {zero} () () eq\n modq-suc {suc q} i j eq with modq i\n modq-suc {suc q} i j () | just x\n modq-suc {suc q} i j () | nothing\n\n sucmod-inj : \u2200 {q}{i j : Fin q} \u2192 sucmod i \u2261 sucmod j \u2192 i \u2261 j\n sucmod-inj {i = i} {j} eq with modq (suc i) | modq (suc j) | modq-inj (suc i) (suc j) | modq-suc i j | modq-suc j i\n sucmod-inj eq | just _ | just _ | p | _ | _ = suc-injective (p (cong just eq))\n sucmod-inj eq | nothing | nothing | p | _ | _ = suc-injective (p refl)\n sucmod-inj eq | just _ | nothing | _ | p | _ = \ud835\udfd8-elim (p (cong Maybe.just eq))\n sucmod-inj eq | nothing | just _ | _ | _ | p = \ud835\udfd8-elim (p (cong Maybe.just (sym eq)))\n\n modq-from\u2115 : \u2200 q \u2192 modq (from\u2115 q) \u2261 nothing\n modq-from\u2115 zero = refl\n modq-from\u2115 (suc q) rewrite modq-from\u2115 q = refl\n\n modq-inject\u2081 : \u2200 {q} (i : Fin q) \u2192 modq (inject\u2081 i) \u2261 just i\n modq-inject\u2081 zero = refl\n modq-inject\u2081 (suc i) rewrite modq-inject\u2081 i = refl\n\n sucmod-from\u2115 : \u2200 q \u2192 sucmod (from\u2115 q) \u2261 zero\n sucmod-from\u2115 q rewrite modq-from\u2115 q = refl\n\n sucmod-inject\u2081 : \u2200 {n} (i : Fin n) \u2192 sucmod (inject\u2081 i) \u2261 suc i\n sucmod-inject\u2081 i rewrite modq-inject\u2081 i = refl\n\n lem-inject\u2081 : \u2200 {n a} {A : \u2605 a} (i : Fin n) (xs : Vec A n) x \u2192 lookup (inject\u2081 i) (xs \u2237\u02b3 x) \u2261 lookup i xs\n lem-inject\u2081 zero (x\u2080 \u2237 xs) x\u2081 = refl\n lem-inject\u2081 (suc i) (x\u2080 \u2237 xs) x\u2081 = lem-inject\u2081 i xs x\u2081\n\n lem-from\u2115 : \u2200 {n a} {A : \u2605 a} (xs : Vec A n) x \u2192 lookup (from\u2115 n) (xs \u2237\u02b3 x) \u2261 x\n lem-from\u2115 {zero} [] x = refl\n lem-from\u2115 {suc n} (_ \u2237 xs) x = lem-from\u2115 xs x\n\n lookup-sucmod : \u2200 {n a} {A : \u2605 a} (i : Fin (suc n)) (x : A) xs\n \u2192 lookup i (xs \u2237\u02b3 x) \u2261 lookup (sucmod i) (x \u2237 xs)\n lookup-sucmod i x xs with finView i\n lookup-sucmod {n} .(from\u2115 n) x xs | `from\u2115 rewrite sucmod-from\u2115 n = lem-from\u2115 xs x\n lookup-sucmod .(inject\u2081 x) x\u2081 xs | `inject\u2081 x rewrite sucmod-inject\u2081 x = lem-inject\u2081 x xs x\u2081\n\n lookup-map : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b} (f : A \u2192 B) i (xs : Vec A n)\n \u2192 lookup i (vmap f xs) \u2261 f (lookup i xs)\n lookup-map f zero (x \u2237 xs) = refl\n lookup-map f (suc i) (x \u2237 xs) = lookup-map f i xs\n\n vec\u2257\u21d2\u2261 : \u2200 {n a} {A : \u2605 a} (xs ys : Vec A n) \u2192 flip lookup xs \u2257 flip lookup ys \u2192 xs \u2261 ys\n vec\u2257\u21d2\u2261 [] [] _ = refl\n vec\u2257\u21d2\u2261 (x \u2237 xs) (y \u2237 ys) p rewrite vec\u2257\u21d2\u2261 xs ys (p \u2218 suc) | p zero = refl\n\n -- most likely this is subsumed by the StableUnderInjection parts\n private\n module Unused where\n lookup-sucmod-rot\u2081 : \u2200 {n a} {A : \u2605 a} (i : Fin n) (xs : Vec A n)\n \u2192 lookup i (rot\u2081 xs) \u2261 lookup (sucmod i) xs\n lookup-sucmod-rot\u2081 {zero} () xs\n lookup-sucmod-rot\u2081 {suc n} i (x \u2237 xs) = lookup-sucmod i x xs\n\n lookup-rot\u2081-allFin : \u2200 {n} i \u2192 lookup i (rot\u2081 (allFin n)) \u2261 lookup i (vmap sucmod (allFin n))\n lookup-rot\u2081-allFin {n} i rewrite lookup-sucmod-rot\u2081 i (allFin _)\n | Vec.lookup-allFin (sucmod i)\n | lookup-map sucmod i (allFin n)\n | Vec.lookup\u2218tabulate id i\n = refl\n\n rot\u2081-map-sucmod : \u2200 n \u2192 rot\u2081 (allFin n) \u2261 vmap sucmod (allFin n)\n rot\u2081-map-sucmod _ = vec\u2257\u21d2\u2261 _ _ lookup-rot\u2081-allFin\n\n {-\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","old_contents":"-- NOTE with-K\nmodule Data.Fin.NP where\n\nopen import Type hiding (\u2605)\nopen import Function\nopen import Data.Zero\nopen import Data.One\nopen import Data.Fin public renaming (to\u2115 to Fin\u25b9\u2115)\nopen import Data.Nat.NP using (\u2115; zero; suc; _<=_; module \u2115\u00b0) renaming (_+_ to _+\u2115_)\nopen import Data.Two hiding (_==_)\nimport Data.Vec.NP as Vec\nopen Vec using (Vec; []; _\u2237_; _\u2237\u02b3_; allFin; lookup; rot\u2081) renaming (map to vmap)\nimport Data.Vec.Properties as Vec\nopen import Data.Maybe.NP\nopen import Data.Sum as Sum\nopen import Relation.Binary.PropositionalEquality as \u2261\n\nsuc-injective : \u2200 {m}{i j : Fin m} \u2192 Fin.suc i \u2261 suc j \u2192 i \u2261 j\nsuc-injective refl = refl\n\n-- The isomorphisms about Fin, \ud835\udfd8, \ud835\udfd9, \ud835\udfda are in Function.Related.TypeIsomorphisms.NP\n\nFin\u25b9\ud835\udfd8 : Fin 0 \u2192 \ud835\udfd8\nFin\u25b9\ud835\udfd8 ()\n\n\ud835\udfd8\u25b9Fin : \ud835\udfd8 \u2192 Fin 0\n\ud835\udfd8\u25b9Fin ()\n\nFin\u25b9\ud835\udfd9 : Fin 1 \u2192 \ud835\udfd9\nFin\u25b9\ud835\udfd9 _ = _\n\n\ud835\udfd9\u25b9Fin : \ud835\udfd9 \u2192 Fin 1\n\ud835\udfd9\u25b9Fin _ = zero\n\nFin\u25b9\ud835\udfda : Fin 2 \u2192 \ud835\udfda\nFin\u25b9\ud835\udfda zero = 0\u2082\nFin\u25b9\ud835\udfda (suc _) = 1\u2082\n\n\ud835\udfda\u25b9Fin : \ud835\udfda \u2192 Fin 2\n\ud835\udfda\u25b9Fin = [0: # 0 1: # 1 ]\n\n_+\u2032_ : \u2200 {m n} (x : Fin m) (y : Fin n) \u2192 Fin (m +\u2115 n)\n_+\u2032_ {suc m} {n} zero y rewrite \u2115\u00b0.+-comm (suc m) n = inject+ _ y\nsuc x +\u2032 y = suc (x +\u2032 y)\n\n_==_ : \u2200 {n} (x y : Fin n) \u2192 \ud835\udfda\nx == y = helper (compare x y) where\n helper : \u2200 {n} {i j : Fin n} \u2192 Ordering i j \u2192 \ud835\udfda\n helper (equal _) = 1\u2082\n helper _ = 0\u2082\n\nswap : \u2200 {i} (x y : Fin i) \u2192 Fin i \u2192 Fin i\nswap x y z = case x == z 0: (case y == z 0: z 1: x) 1: y\n\ndata FinSum m n : Fin (m +\u2115 n) \u2192 \u2605\u2080 where\n bound : (x : Fin m) \u2192 FinSum m n (inject+ n x)\n free : (x : Fin n) \u2192 FinSum m n (raise m x)\n\nopen import Relation.Binary.PropositionalEquality\n\ncmp : \u2200 m n (x : Fin (m +\u2115 n)) \u2192 FinSum m n x\ncmp zero n x = free x\ncmp (suc m) n zero = bound zero\ncmp (suc m) n (suc x) with cmp m n x\ncmp (suc m) n (suc .(inject+ n x)) | bound x = bound (suc x)\ncmp (suc m) n (suc .(raise m x)) | free x = free x\n\nmax : \u2200 n \u2192 Fin (suc n)\nmax = from\u2115\n\n-- reverse x = n \u2238 (1 + x)\nreverse : \u2200 {n} \u2192 Fin n \u2192 Fin n\nreverse {suc n} zero = from\u2115 n\nreverse {suc n} (suc x) = inject\u2081 (reverse {n} x)\n\nopen import Data.Nat\nopen import Data.Nat.Properties\nopen import Data.Fin.Props renaming (reverse to reverse-old)\n\nreverse-from\u2115 : \u2200 n \u2192 reverse (from\u2115 n) \u2261 zero\nreverse-from\u2115 zero = refl\nreverse-from\u2115 (suc n) rewrite reverse-from\u2115 n = refl\n\nreverse-inject\u2081 : \u2200 {n} (x : Fin n) \u2192 reverse (inject\u2081 x) \u2261 suc (reverse x)\nreverse-inject\u2081 zero = refl\nreverse-inject\u2081 (suc x) rewrite reverse-inject\u2081 x = refl\n\n{-\nreverse-involutive : \u2200 {n} (x : Fin n) \u2192 reverse (reverse x) \u2261 x\nreverse-involutive zero = reverse-from\u2115 _\nreverse-involutive (suc x) rewrite reverse-inject\u2081 (reverse x) | reverse-involutive x = refl\n-}\n\nreverse-lem : \u2200 {n} (x : Fin n) \u2192 Fin\u25b9\u2115 (reverse x) \u2261 n \u2238 suc (Fin\u25b9\u2115 x)\nreverse-lem zero = to-from _\nreverse-lem (suc x) rewrite inject\u2081-lemma (reverse x) = reverse-lem x\n\nFin\u25b9\u2115-\u2115-lem : \u2200 {n} (x : Fin (suc n)) \u2192 Fin\u25b9\u2115 (n \u2115- x) \u2261 n \u2238 Fin\u25b9\u2115 x\nFin\u25b9\u2115-\u2115-lem zero = to-from _\nFin\u25b9\u2115-\u2115-lem {zero} (suc ())\nFin\u25b9\u2115-\u2115-lem {suc n} (suc x) = Fin\u25b9\u2115-\u2115-lem x\n\nreverse-old-lem : \u2200 {n} (x : Fin n) \u2192 Fin\u25b9\u2115 (reverse-old x) \u2261 n \u2238 suc (Fin\u25b9\u2115 x)\nreverse-old-lem {zero} ()\nreverse-old-lem {suc n} x rewrite inject\u2264-lemma (n \u2115- x) (n\u2238m\u2264n (Fin\u25b9\u2115 x) (suc n)) = Fin\u25b9\u2115-\u2115-lem x\n\ndata FinView {n} : Fin (suc n) \u2192 \u2605\u2080 where\n `from\u2115 : FinView (from\u2115 n)\n `inject\u2081 : \u2200 x \u2192 FinView (inject\u2081 x)\n\nsucFinView : \u2200 {n} {i : Fin (suc n)} \u2192 FinView i \u2192 FinView (suc i)\nsucFinView `from\u2115 = `from\u2115\nsucFinView (`inject\u2081 x) = `inject\u2081 (suc x)\n\nfinView : \u2200 {n} \u2192 (i : Fin (suc n)) \u2192 FinView i\nfinView {zero} zero = `from\u2115\nfinView {suc n} zero = `inject\u2081 zero\nfinView {suc n} (suc i) = sucFinView (finView i)\nfinView {zero} (suc ())\n\nmodule Modulo where\n modq : \u2200 {q} \u2192 Fin (suc q) \u2192 Maybe (Fin q)\n modq {zero} _ = nothing\n modq {suc q} zero = just zero\n modq {suc q} (suc x) = map? suc (modq x)\n\n modq-inj : \u2200 {q} (i j : Fin (suc q)) \u2192 modq i \u2261 modq j \u2192 i \u2261 j\n modq-inj {zero} zero zero eq = refl\n modq-inj {zero} zero (suc ()) eq\n modq-inj {zero} (suc ()) zero eq\n modq-inj {zero} (suc ()) (suc ()) eq\n modq-inj {suc q} zero zero eq = refl\n modq-inj {suc q} zero (suc j) eq with modq j\n modq-inj {suc q} zero (suc j) () | nothing\n modq-inj {suc q} zero (suc j) () | just j'\n modq-inj {suc q} (suc i) zero eq with modq i\n modq-inj {suc q} (suc i) zero () | just x\n modq-inj {suc q} (suc i) zero () | nothing\n modq-inj {suc q} (suc i) (suc j) eq with modq i | modq j | modq-inj i j\n modq-inj {suc q} (suc i) (suc j) eq | just x | just x\u2081 | p = cong suc (p (cong just (suc-injective (just-injective eq))))\n modq-inj {suc q} (suc i) (suc j) () | just x | nothing | p\n modq-inj {suc q} (suc i) (suc j) () | nothing | just x | p\n modq-inj {suc q} (suc i) (suc j) eq | nothing | nothing | p = cong suc (p refl)\n\n modq\u2032 : \u2200 {q} \u2192 Fin (suc q) \u2192 Fin (suc q)\n modq\u2032 {zero} _ = zero\n modq\u2032 {suc q} zero = suc zero\n modq\u2032 {suc q} (suc x) = lift 1 suc (modq\u2032 x)\n\n modqq : \u2200 {q} \u2192 Fin q \u2192 Fin q\n modqq {zero} x = x\n modqq {suc q} x = modq\u2032 x\n\n -- Maybe (Fin n) \u2245 Fin (suc n)\n\n modq\u2032\u2032 : \u2200 {q} \u2192 Fin (suc q) \u2192 Maybe (Fin q)\n modq\u2032\u2032 x with modq\u2032 x\n ... | zero = nothing\n ... | suc y = just y\n\n zero\u2203 : \u2200 {q} \u2192 Fin q \u2192 Fin q\n zero\u2203 {zero} ()\n zero\u2203 {suc q} _ = zero\n\n sucmod : \u2200 {q} \u2192 Fin q \u2192 Fin q\n sucmod x with modq (suc x)\n ... | nothing = zero\u2203 x\n ... | just y = y\n\n modq-suc : \u2200 {q} (i j : Fin q) \u2192 modq (suc i) \u2262 just (zero\u2203 j)\n modq-suc {zero} () () eq\n modq-suc {suc q} i j eq with modq i\n modq-suc {suc q} i j () | just x\n modq-suc {suc q} i j () | nothing\n\n sucmod-inj : \u2200 {q}{i j : Fin q} \u2192 sucmod i \u2261 sucmod j \u2192 i \u2261 j\n sucmod-inj {i = i} {j} eq with modq (suc i) | modq (suc j) | modq-inj (suc i) (suc j) | modq-suc i j | modq-suc j i\n sucmod-inj eq | just _ | just _ | p | _ | _ = suc-injective (p (cong just eq))\n sucmod-inj eq | nothing | nothing | p | _ | _ = suc-injective (p refl)\n sucmod-inj eq | just _ | nothing | _ | p | _ = \ud835\udfd8-elim (p (cong Maybe.just eq))\n sucmod-inj eq | nothing | just _ | _ | _ | p = \ud835\udfd8-elim (p (cong Maybe.just (sym eq)))\n\n modq-from\u2115 : \u2200 q \u2192 modq (from\u2115 q) \u2261 nothing\n modq-from\u2115 zero = refl\n modq-from\u2115 (suc q) rewrite modq-from\u2115 q = refl\n\n modq-inject\u2081 : \u2200 {q} (i : Fin q) \u2192 modq (inject\u2081 i) \u2261 just i\n modq-inject\u2081 zero = refl\n modq-inject\u2081 (suc i) rewrite modq-inject\u2081 i = refl\n\n sucmod-from\u2115 : \u2200 q \u2192 sucmod (from\u2115 q) \u2261 zero\n sucmod-from\u2115 q rewrite modq-from\u2115 q = refl\n\n sucmod-inject\u2081 : \u2200 {n} (i : Fin n) \u2192 sucmod (inject\u2081 i) \u2261 suc i\n sucmod-inject\u2081 i rewrite modq-inject\u2081 i = refl\n\n lem-inject\u2081 : \u2200 {n a} {A : \u2605 a} (i : Fin n) (xs : Vec A n) x \u2192 lookup (inject\u2081 i) (xs \u2237\u02b3 x) \u2261 lookup i xs\n lem-inject\u2081 zero (x\u2080 \u2237 xs) x\u2081 = refl\n lem-inject\u2081 (suc i) (x\u2080 \u2237 xs) x\u2081 = lem-inject\u2081 i xs x\u2081\n\n lem-from\u2115 : \u2200 {n a} {A : \u2605 a} (xs : Vec A n) x \u2192 lookup (from\u2115 n) (xs \u2237\u02b3 x) \u2261 x\n lem-from\u2115 {zero} [] x = refl\n lem-from\u2115 {suc n} (_ \u2237 xs) x = lem-from\u2115 xs x\n\n lookup-sucmod : \u2200 {n a} {A : \u2605 a} (i : Fin (suc n)) (x : A) xs\n \u2192 lookup i (xs \u2237\u02b3 x) \u2261 lookup (sucmod i) (x \u2237 xs)\n lookup-sucmod i x xs with finView i\n lookup-sucmod {n} .(from\u2115 n) x xs | `from\u2115 rewrite sucmod-from\u2115 n = lem-from\u2115 xs x\n lookup-sucmod .(inject\u2081 x) x\u2081 xs | `inject\u2081 x rewrite sucmod-inject\u2081 x = lem-inject\u2081 x xs x\u2081\n\n lookup-map : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b} (f : A \u2192 B) i (xs : Vec A n)\n \u2192 lookup i (vmap f xs) \u2261 f (lookup i xs)\n lookup-map f zero (x \u2237 xs) = refl\n lookup-map f (suc i) (x \u2237 xs) = lookup-map f i xs\n\n vec\u2257\u21d2\u2261 : \u2200 {n a} {A : \u2605 a} (xs ys : Vec A n) \u2192 flip lookup xs \u2257 flip lookup ys \u2192 xs \u2261 ys\n vec\u2257\u21d2\u2261 [] [] _ = refl\n vec\u2257\u21d2\u2261 (x \u2237 xs) (y \u2237 ys) p rewrite vec\u2257\u21d2\u2261 xs ys (p \u2218 suc) | p zero = refl\n\n -- most likely this is subsumed by the StableUnderInjection parts\n private\n module Unused where\n lookup-sucmod-rot\u2081 : \u2200 {n a} {A : \u2605 a} (i : Fin n) (xs : Vec A n)\n \u2192 lookup i (rot\u2081 xs) \u2261 lookup (sucmod i) xs\n lookup-sucmod-rot\u2081 {zero} () xs\n lookup-sucmod-rot\u2081 {suc n} i (x \u2237 xs) = lookup-sucmod i x xs\n\n lookup-rot\u2081-allFin : \u2200 {n} i \u2192 lookup i (rot\u2081 (allFin n)) \u2261 lookup i (vmap sucmod (allFin n))\n lookup-rot\u2081-allFin {n} i rewrite lookup-sucmod-rot\u2081 i (allFin _)\n | Vec.lookup-allFin (sucmod i)\n | lookup-map sucmod i (allFin n)\n | Vec.lookup\u2218tabulate id i\n = refl\n\n rot\u2081-map-sucmod : \u2200 n \u2192 rot\u2081 (allFin n) \u2261 vmap sucmod (allFin n)\n rot\u2081-map-sucmod _ = vec\u2257\u21d2\u2261 _ _ lookup-rot\u2081-allFin\n\n {-\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"b531cfe0eaf8c265a52d179884b4b7d3964e4f81","subject":"Vec: unconvincing experiment with reverse","message":"Vec: unconvincing experiment with reverse\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Vec\/NP.agda","new_file":"lib\/Data\/Vec\/NP.agda","new_contents":"module Data.Vec.NP where\n\nopen import Data.Vec public hiding (_\u229b_; zipWith; zip; map)\nopen import Data.Nat using (\u2115; suc; zero; _+_)\nopen import Data.Fin renaming (_+_ to _+\u1da0_)\nimport Data.Fin.Props as F\nopen import Data.Bool\nopen import Data.Product hiding (map; zip)\nopen import Function\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\n\nmodule waiting-for-a-fix-in-the-stdlib where\n\n infixl 4 _\u229b_\n\n _\u229b_ : \u2200 {a b n} {A : Set a} {B : Set b} \u2192\n Vec (A \u2192 B) n \u2192 Vec A n \u2192 Vec B n\n _\u229b_ {n = zero} fs xs = []\n _\u229b_ {n = suc n} fs xs = head fs (head xs) \u2237 (tail fs \u229b tail xs)\n\n map : \u2200 {a b n} {A : Set a} {B : Set b} \u2192\n (A \u2192 B) \u2192 Vec A n \u2192 Vec B n\n map f xs = replicate f \u229b xs\n\n zipWith : \u2200 {a b c n} {A : Set a} {B : Set b} {C : Set c} \u2192\n (A \u2192 B \u2192 C) \u2192 Vec A n \u2192 Vec B n \u2192 Vec C n\n zipWith _\u2295_ xs ys = replicate _\u2295_ \u229b xs \u229b ys\n\n zip : \u2200 {a b n} {A : Set a} {B : Set b} \u2192\n Vec A n \u2192 Vec B n \u2192 Vec (A \u00d7 B) n\n zip = zipWith _,_\n\nopen waiting-for-a-fix-in-the-stdlib public\n\n-- Trying to get rid of the foldl in the definition of reverse and\n-- without using equations on natural numbers.\n-- In the end that's not very convincing.\nmodule Alternative-Reverse where\n rev-+ : \u2115 \u2192 \u2115 \u2192 \u2115\n rev-+ zero = id\n rev-+ (suc x) = rev-+ x \u2218 suc\n\n rev-aux : \u2200 {a} {A : Set a} {m} n \u2192\n Vec A (rev-+ n zero) \u2192\n (\u2200 {m} \u2192 A \u2192 Vec A (rev-+ n m) \u2192 Vec A (rev-+ n (suc m))) \u2192\n Vec A m \u2192 Vec A (rev-+ n m)\n rev-aux m acc op [] = acc\n rev-aux m acc op (x \u2237 xs) = rev-aux (suc m) (op x acc) op xs\n\n alt-reverse : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n alt-reverse = rev-aux 0 [] _\u2237_\n\nvuncurry : \u2200 {n a b} {A : Set a} {B : Set b} (f : A \u2192 Vec A n \u2192 B) \u2192 Vec A (1 + n) \u2192 B\nvuncurry f (x \u2237 xs) = f x xs\n\ncount\u1da0 : \u2200 {n a} {A : Set a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 Fin (suc n)\ncount\u1da0 pred = foldr (Fin \u2218 suc) (\u03bb x \u2192 if pred x then suc else inject\u2081) zero\n\ncount : \u2200 {n a} {A : Set a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 \u2115\ncount pred = to\u2115 \u2218 count\u1da0 pred\n\ncount-\u2218 : \u2200 {n a b} {A : Set a} {B : Set b} (f : A \u2192 B) (pred : B \u2192 Bool) \u2192\n count {n} (pred \u2218 f) \u2257 count pred \u2218 map f\ncount-\u2218 f pred [] = refl\ncount-\u2218 f pred (x \u2237 xs) with pred (f x)\n... | true rewrite count-\u2218 f pred xs = refl\n... | false rewrite F.inject\u2081-lemma (count\u1da0 pred (map f xs))\n | F.inject\u2081-lemma (count\u1da0 (pred \u2218 f) xs)\n | count-\u2218 f pred xs = refl\n\ncount-++ : \u2200 {m n a} {A : Set a} (pred : A \u2192 Bool) (xs : Vec A m) (ys : Vec A n)\n \u2192 count pred (xs ++ ys) \u2261 count pred xs + count pred ys\ncount-++ pred [] ys = refl\ncount-++ pred (x \u2237 xs) ys with pred x\n... | true rewrite count-++ pred xs ys = refl\n... | false rewrite F.inject\u2081-lemma (count\u1da0 pred (xs ++ ys))\n | F.inject\u2081-lemma (count\u1da0 pred xs) | count-++ pred xs ys = refl\n\next-count\u1da0 : \u2200 {n a} {A : Set a} {f g : A \u2192 Bool} \u2192 f \u2257 g \u2192 (xs : Vec A n) \u2192 count\u1da0 f xs \u2261 count\u1da0 g xs\next-count\u1da0 f\u2257g [] = refl\next-count\u1da0 f\u2257g (x \u2237 xs) rewrite ext-count\u1da0 f\u2257g xs | f\u2257g x = refl\n\nfilter : \u2200 {n a} {A : Set a} (pred : A \u2192 Bool) (xs : Vec A n) \u2192 Vec A (count pred xs)\nfilter pred [] = []\nfilter pred (x \u2237 xs) with pred x\n... | true = x \u2237 filter pred xs\n... | false rewrite F.inject\u2081-lemma (count\u1da0 pred xs) = filter pred xs\n\n\u03b7 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7 = tabulate \u2218 flip lookup\n\n\u03b7\u2032 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7\u2032 {zero} = \u03bb _ \u2192 []\n\u03b7\u2032 {suc n} = \u03bb xs \u2192 head xs \u2237 \u03b7 (tail xs)\n\nuncons : \u2200 {n a} {A : Set a} \u2192 Vec A (1 + n) \u2192 (A \u00d7 Vec A n)\nuncons (x \u2237 xs) = x , xs\n\nsplitAt\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A m \u00d7 Vec A n\nsplitAt\u2032 m xs = case splitAt m xs of \u03bb { (ys , zs , _) \u2192 (ys , zs) }\n\n++-decomp : \u2200 {m n a} {A : Set a} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2261 (zs ++ ts) \u2192 (xs \u2261 zs \u00d7 ys \u2261 ts)\n++-decomp {zero} {xs = []} {[]} p = refl , p\n++-decomp {suc m} {xs = x \u2237 xs} {z \u2237 zs} eq with ++-decomp {m} {xs = xs} {zs} (cong tail eq)\n... | (q\u2081 , q\u2082) = (cong\u2082 _\u2237_ (cong head eq) q\u2081) , q\u2082\n\n{-\nopen import Data.Vec.Equality\n\nmodule Here {a} {A : Set a} where\n open Equality (\u2261.setoid A)\n \u2248-splitAt : \u2200 {m n} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2248 (zs ++ ts) \u2192 (xs \u2248 zs \u00d7 ys \u2248 ts)\n \u2248-splitAt {zero} {xs = []} {[]} p = []-cong , p\n \u2248-splitAt {suc m} {xs = x \u2237 xs} {z \u2237 zs} (x\u00b9\u2248x\u00b2 \u2237-cong p) with \u2248-splitAt {m} {xs = xs} {zs} p\n ... | (q\u2081 , q\u2082) = x\u00b9\u2248x\u00b2 \u2237-cong q\u2081 , q\u2082\n-}\n\n++-inj\u2081 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 xs \u2261 ys\n++-inj\u2081 xs ys eq = proj\u2081 (++-decomp eq)\n\n++-inj\u2082 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 zs \u2261 ts\n++-inj\u2082 xs ys eq = proj\u2082 (++-decomp {xs = xs} {ys} eq)\n\ntake-\u2237 : \u2200 {m a} {A : Set a} n x (xs : Vec A (n + m)) \u2192 take (suc n) (x \u2237 xs) \u2261 x \u2237 take n xs\ntake-\u2237 n x xs with splitAt n xs\ntake-\u2237 _ _ ._ | _ , _ , refl = refl\n\ndrop-\u2237 : \u2200 {m a} {A : Set a} n x (xs : Vec A (n + m)) \u2192 drop (suc n) (x \u2237 xs) \u2261 drop n xs\ndrop-\u2237 n x xs with splitAt n xs\ndrop-\u2237 _ _ ._ | _ , _ , refl = refl\n\ntake-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 take m (xs ++ ys) \u2261 xs\ntake-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ntake-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2081 xs\u2081 xs eq)\n\ndrop-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 drop m (xs ++ ys) \u2261 ys\ndrop-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ndrop-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2082 xs\u2081 xs eq)\n\ntake-drop-lem : \u2200 m {n} {a} {A : Set a} (xs : Vec A (m + n)) \u2192 take m xs ++ drop m xs \u2261 xs\ntake-drop-lem m xs with splitAt m xs\ntake-drop-lem m .(ys ++ zs) | ys , zs , refl = refl\n\ntake-them-all : \u2200 n {a} {A : Set a} (xs : Vec A (n + 0)) \u2192 take n xs ++ [] \u2261 xs\ntake-them-all n xs with splitAt n xs\ntake-them-all n .(ys ++ []) | ys , [] , refl = refl\n\ndrop\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A n\ndrop\u2032 zero = id\ndrop\u2032 (suc m) = drop\u2032 m \u2218 tail\n\ndrop\u2032-spec : \u2200 {a} {A : Set a} m {n} \u2192 drop\u2032 {A = A} m {n} \u2257 drop m {n}\ndrop\u2032-spec zero xs = refl\ndrop\u2032-spec (suc m) (x \u2237 xs) rewrite drop\u2032-spec m xs | drop-\u2237 m x xs = refl\n\ntake\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A m\ntake\u2032 zero _ = []\ntake\u2032 (suc m) xs = head xs \u2237 take\u2032 m (tail xs)\n\ntake\u2032-spec : \u2200 {a} {A : Set a} m {n} \u2192 take\u2032 {A = A} m {n} \u2257 take m {n}\ntake\u2032-spec zero xs = refl\ntake\u2032-spec (suc m) (x \u2237 xs) rewrite take\u2032-spec m xs | take-\u2237 m x xs = refl\n\nrewire : \u2200 {a i o} {A : Set a} \u2192 (Fin o \u2192 Fin i) \u2192 Vec A i \u2192 Vec A o\nrewire f v = tabulate (flip lookup v \u2218 f)\n\nRewireTbl : (i o : \u2115) \u2192 Set\nRewireTbl i o = Vec (Fin i) o\n\nrewireTbl : \u2200 {a i o} {A : Set a} \u2192 RewireTbl i o \u2192 Vec A i \u2192 Vec A o\nrewireTbl tbl v = map (flip lookup v) tbl\n\non\u1d62 : \u2200 {a} {A : Set a} (f : A \u2192 A) {n} (i : Fin n) \u2192 Vec A n \u2192 Vec A n\non\u1d62 f zero (x \u2237 xs) = f x \u2237 xs\non\u1d62 f (suc i) (x \u2237 xs) = x \u2237 on\u1d62 f i xs\n\n-- Exchange elements at positions 0 and 1 of a given vector\n-- (this only apply if the vector is long enough).\n0\u21941 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n0\u21941 (x\u2080 \u2237 x\u2081 \u2237 xs) = x\u2081 \u2237 x\u2080 \u2237 xs\n0\u21941 xs = xs\n\nmap-tail : \u2200 {m n a} {A : Set a} \u2192 (Vec A m \u2192 Vec A n) \u2192 Vec A (suc m) \u2192 Vec A (suc n)\nmap-tail f (x \u2237 xs) = x \u2237 f xs\n\nmap-tail-id : \u2200 {n a} {A : Set a} \u2192 map-tail id \u2257 id {A = Vec A (suc n)}\nmap-tail-id (x \u2237 xs) = \u2261.refl\n\n-- \u27e8 i \u2194+1\u27e9: Exchange elements at position i and i + 1.\n\u27e8_\u2194+1\u27e9 : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u27e8 zero \u2194+1\u27e9 = 0\u21941\n\u27e8 suc i \u2194+1\u27e9 = map-tail \u27e8 i \u2194+1\u27e9\n\n-- rot-up-to i (x\u2080 \u2237 x\u2081 \u2237 x\u2082 \u2237 \u2026 \u2237 x\u1d62 \u2237 xs)\n-- \u2261 (x\u2081 \u2237 x\u2082 \u2237 x\u2083 \u2237 \u2026 \u2237 x\u2080 \u2237 xs)\nrot-up-to : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\nrot-up-to zero = id\nrot-up-to (suc i) = map-tail (rot-up-to i) \u2218 0\u21941\n\n-- Inverse of rot-up-to\nrot\u207b\u00b9-up-to : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\nrot\u207b\u00b9-up-to zero = id\nrot\u207b\u00b9-up-to (suc i) = 0\u21941 \u2218 map-tail (rot\u207b\u00b9-up-to i)\n\nrot\u207b\u00b9-up-to\u2218rot-up-to : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 rot\u207b\u00b9-up-to i \u2218 rot-up-to i \u2257 id {a} {Vec A n}\nrot\u207b\u00b9-up-to\u2218rot-up-to zero _ = \u2261.refl\nrot\u207b\u00b9-up-to\u2218rot-up-to (suc i) {A = A} (x\u2080 \u2237 []) rewrite rot\u207b\u00b9-up-to\u2218rot-up-to i {A = A} [] = \u2261.refl\nrot\u207b\u00b9-up-to\u2218rot-up-to (suc i) (x\u2080 \u2237 x\u2081 \u2237 xs) rewrite rot\u207b\u00b9-up-to\u2218rot-up-to i (x\u2080 \u2237 xs) = \u2261.refl\n\n-- \u27e80\u2194 i \u27e9: Exchange elements at position 0 and i.\n\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u27e80\u2194 zero \u27e9 = id\n\u27e80\u2194 suc i \u27e9 = rot\u207b\u00b9-up-to (inject\u2081 i) \u2218 rot-up-to (suc i)\n\n\u27e80\u2194zero\u27e9 : \u2200 {n a} {A : Set a} \u2192 \u27e80\u2194 zero \u27e9 \u2257 id {A = Vec A (suc n)}\n\u27e80\u2194zero\u27e9 _ = \u2261.refl\n\n-- \u27e8 i \u2194 j \u27e9: Exchange elements at position i and j.\n\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u27e8 i \u2194 j \u27e9 = \u27e80\u2194 i \u27e9 \u2218 \u27e80\u2194 j \u27e9 \u2218 \u27e80\u2194 i \u27e9\n\n\u27e80\u2194\u27e9-spec : \u2200 {n a} {A : Set a} (i : Fin (suc n)) \u2192 \u27e80\u2194 i \u27e9 \u2257 \u27e8 zero \u2194 i \u27e9 {A = A}\n\u27e80\u2194\u27e9-spec _ _ = \u2261.refl\n\n\u27e8_\u2194_\u27e9\u2032 : \u2200 {n} (i j : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u27e8 zero \u2194 j \u27e9\u2032 = \u27e80\u2194 j \u27e9\n\u27e8 i \u2194 zero \u27e9\u2032 = \u27e80\u2194 i \u27e9\n\u27e8 suc i \u2194 suc j \u27e9\u2032 = map-tail \u27e8 i \u2194 j \u27e9\u2032\n\n\u27e8\u2194\u27e9\u2032-comm : \u2200 {n a} {A : Set a} (i j : Fin n) \u2192 \u27e8 i \u2194 j \u27e9\u2032 \u2257 \u27e8 j \u2194 i \u27e9\u2032 {A = A}\n\u27e8\u2194\u27e9\u2032-comm zero zero _ = \u2261.refl\n\u27e8\u2194\u27e9\u2032-comm zero (suc _) _ = \u2261.refl\n\u27e8\u2194\u27e9\u2032-comm (suc _) zero _ = \u2261.refl\n\u27e8\u2194\u27e9\u2032-comm (suc i) (suc j) (x \u2237 xs) rewrite \u27e8\u2194\u27e9\u2032-comm i j xs = \u2261.refl\n\n\u27e8\u2194+1\u27e9-spec : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 \u27e8 inject\u2081 i \u2194+1\u27e9 \u2257 \u27e8 inject\u2081 i \u2194 suc i \u27e9\u2032 {A = A}\n\u27e8\u2194+1\u27e9-spec zero xs rewrite map-tail-id (0\u21941 xs) = \u2261.refl\n\u27e8\u2194+1\u27e9-spec (suc i) (x \u2237 xs) rewrite \u27e8\u2194+1\u27e9-spec i xs = \u2261.refl\n\n\u27e80\u2194_\u27e9\u2032 : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u27e80\u2194_\u27e9\u2032 {zero} i xs = xs\n\u27e80\u2194_\u27e9\u2032 {suc n} i xs = lookup i xs \u2237 tail (xs [ i ]\u2254 head xs)\n","old_contents":"module Data.Vec.NP where\n\nopen import Data.Vec public hiding (_\u229b_; zipWith; zip; map)\nopen import Data.Nat using (\u2115; suc; zero; _+_)\nopen import Data.Fin renaming (_+_ to _+\u1da0_)\nopen import Data.Fin.Props\nopen import Data.Bool\nopen import Data.Product hiding (map; zip)\nopen import Function\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\n\nmodule waiting-for-a-fix-in-the-stdlib where\n\n infixl 4 _\u229b_\n\n _\u229b_ : \u2200 {a b n} {A : Set a} {B : Set b} \u2192\n Vec (A \u2192 B) n \u2192 Vec A n \u2192 Vec B n\n _\u229b_ {n = zero} fs xs = []\n _\u229b_ {n = suc n} fs xs = head fs (head xs) \u2237 (tail fs \u229b tail xs)\n\n map : \u2200 {a b n} {A : Set a} {B : Set b} \u2192\n (A \u2192 B) \u2192 Vec A n \u2192 Vec B n\n map f xs = replicate f \u229b xs\n\n zipWith : \u2200 {a b c n} {A : Set a} {B : Set b} {C : Set c} \u2192\n (A \u2192 B \u2192 C) \u2192 Vec A n \u2192 Vec B n \u2192 Vec C n\n zipWith _\u2295_ xs ys = replicate _\u2295_ \u229b xs \u229b ys\n\n zip : \u2200 {a b n} {A : Set a} {B : Set b} \u2192\n Vec A n \u2192 Vec B n \u2192 Vec (A \u00d7 B) n\n zip = zipWith _,_\n\nopen waiting-for-a-fix-in-the-stdlib public\n\nvuncurry : \u2200 {n a b} {A : Set a} {B : Set b} (f : A \u2192 Vec A n \u2192 B) \u2192 Vec A (1 + n) \u2192 B\nvuncurry f (x \u2237 xs) = f x xs\n\ncount\u1da0 : \u2200 {n a} {A : Set a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 Fin (suc n)\ncount\u1da0 pred = foldr (Fin \u2218 suc) (\u03bb x \u2192 if pred x then suc else inject\u2081) zero\n\ncount : \u2200 {n a} {A : Set a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 \u2115\ncount pred = to\u2115 \u2218 count\u1da0 pred\n\ncount-\u2218 : \u2200 {n a b} {A : Set a} {B : Set b} (f : A \u2192 B) (pred : B \u2192 Bool) \u2192\n count {n} (pred \u2218 f) \u2257 count pred \u2218 map f\ncount-\u2218 f pred [] = refl\ncount-\u2218 f pred (x \u2237 xs) with pred (f x)\n... | true rewrite count-\u2218 f pred xs = refl\n... | false rewrite inject\u2081-lemma (count\u1da0 pred (map f xs))\n | inject\u2081-lemma (count\u1da0 (pred \u2218 f) xs)\n | count-\u2218 f pred xs = refl\n\ncount-++ : \u2200 {m n a} {A : Set a} (pred : A \u2192 Bool) (xs : Vec A m) (ys : Vec A n)\n \u2192 count pred (xs ++ ys) \u2261 count pred xs + count pred ys\ncount-++ pred [] ys = refl\ncount-++ pred (x \u2237 xs) ys with pred x\n... | true rewrite count-++ pred xs ys = refl\n... | false rewrite inject\u2081-lemma (count\u1da0 pred (xs ++ ys))\n | inject\u2081-lemma (count\u1da0 pred xs) | count-++ pred xs ys = refl\n\next-count\u1da0 : \u2200 {n a} {A : Set a} {f g : A \u2192 Bool} \u2192 f \u2257 g \u2192 (xs : Vec A n) \u2192 count\u1da0 f xs \u2261 count\u1da0 g xs\next-count\u1da0 f\u2257g [] = refl\next-count\u1da0 f\u2257g (x \u2237 xs) rewrite ext-count\u1da0 f\u2257g xs | f\u2257g x = refl\n\nfilter : \u2200 {n a} {A : Set a} (pred : A \u2192 Bool) (xs : Vec A n) \u2192 Vec A (count pred xs)\nfilter pred [] = []\nfilter pred (x \u2237 xs) with pred x\n... | true = x \u2237 filter pred xs\n... | false rewrite inject\u2081-lemma (count\u1da0 pred xs) = filter pred xs\n\n\u03b7 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7 = tabulate \u2218 flip lookup\n\n\u03b7\u2032 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7\u2032 {zero} = \u03bb _ \u2192 []\n\u03b7\u2032 {suc n} = \u03bb xs \u2192 head xs \u2237 \u03b7 (tail xs)\n\nuncons : \u2200 {n a} {A : Set a} \u2192 Vec A (1 + n) \u2192 (A \u00d7 Vec A n)\nuncons (x \u2237 xs) = x , xs\n\nsplitAt\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A m \u00d7 Vec A n\nsplitAt\u2032 m xs = case splitAt m xs of \u03bb { (ys , zs , _) \u2192 (ys , zs) }\n\n++-decomp : \u2200 {m n a} {A : Set a} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2261 (zs ++ ts) \u2192 (xs \u2261 zs \u00d7 ys \u2261 ts)\n++-decomp {zero} {xs = []} {[]} p = refl , p\n++-decomp {suc m} {xs = x \u2237 xs} {z \u2237 zs} eq with ++-decomp {m} {xs = xs} {zs} (cong tail eq)\n... | (q\u2081 , q\u2082) = (cong\u2082 _\u2237_ (cong head eq) q\u2081) , q\u2082\n\n{-\nopen import Data.Vec.Equality\n\nmodule Here {a} {A : Set a} where\n open Equality (\u2261.setoid A)\n \u2248-splitAt : \u2200 {m n} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2248 (zs ++ ts) \u2192 (xs \u2248 zs \u00d7 ys \u2248 ts)\n \u2248-splitAt {zero} {xs = []} {[]} p = []-cong , p\n \u2248-splitAt {suc m} {xs = x \u2237 xs} {z \u2237 zs} (x\u00b9\u2248x\u00b2 \u2237-cong p) with \u2248-splitAt {m} {xs = xs} {zs} p\n ... | (q\u2081 , q\u2082) = x\u00b9\u2248x\u00b2 \u2237-cong q\u2081 , q\u2082\n-}\n\n++-inj\u2081 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 xs \u2261 ys\n++-inj\u2081 xs ys eq = proj\u2081 (++-decomp eq)\n\n++-inj\u2082 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 zs \u2261 ts\n++-inj\u2082 xs ys eq = proj\u2082 (++-decomp {xs = xs} {ys} eq)\n\ntake-\u2237 : \u2200 {m a} {A : Set a} n x (xs : Vec A (n + m)) \u2192 take (suc n) (x \u2237 xs) \u2261 x \u2237 take n xs\ntake-\u2237 n x xs with splitAt n xs\ntake-\u2237 _ _ ._ | _ , _ , refl = refl\n\ndrop-\u2237 : \u2200 {m a} {A : Set a} n x (xs : Vec A (n + m)) \u2192 drop (suc n) (x \u2237 xs) \u2261 drop n xs\ndrop-\u2237 n x xs with splitAt n xs\ndrop-\u2237 _ _ ._ | _ , _ , refl = refl\n\ntake-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 take m (xs ++ ys) \u2261 xs\ntake-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ntake-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2081 xs\u2081 xs eq)\n\ndrop-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 drop m (xs ++ ys) \u2261 ys\ndrop-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ndrop-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2082 xs\u2081 xs eq)\n\ntake-drop-lem : \u2200 m {n} {a} {A : Set a} (xs : Vec A (m + n)) \u2192 take m xs ++ drop m xs \u2261 xs\ntake-drop-lem m xs with splitAt m xs\ntake-drop-lem m .(ys ++ zs) | ys , zs , refl = refl\n\ntake-them-all : \u2200 n {a} {A : Set a} (xs : Vec A (n + 0)) \u2192 take n xs ++ [] \u2261 xs\ntake-them-all n xs with splitAt n xs\ntake-them-all n .(ys ++ []) | ys , [] , refl = refl\n\ndrop\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A n\ndrop\u2032 zero = id\ndrop\u2032 (suc m) = drop\u2032 m \u2218 tail\n\ndrop\u2032-spec : \u2200 {a} {A : Set a} m {n} \u2192 drop\u2032 {A = A} m {n} \u2257 drop m {n}\ndrop\u2032-spec zero xs = refl\ndrop\u2032-spec (suc m) (x \u2237 xs) rewrite drop\u2032-spec m xs | drop-\u2237 m x xs = refl\n\ntake\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A m\ntake\u2032 zero _ = []\ntake\u2032 (suc m) xs = head xs \u2237 take\u2032 m (tail xs)\n\ntake\u2032-spec : \u2200 {a} {A : Set a} m {n} \u2192 take\u2032 {A = A} m {n} \u2257 take m {n}\ntake\u2032-spec zero xs = refl\ntake\u2032-spec (suc m) (x \u2237 xs) rewrite take\u2032-spec m xs | take-\u2237 m x xs = refl\n\nrewire : \u2200 {a i o} {A : Set a} \u2192 (Fin o \u2192 Fin i) \u2192 Vec A i \u2192 Vec A o\nrewire f v = tabulate (flip lookup v \u2218 f)\n\nRewireTbl : (i o : \u2115) \u2192 Set\nRewireTbl i o = Vec (Fin i) o\n\nrewireTbl : \u2200 {a i o} {A : Set a} \u2192 RewireTbl i o \u2192 Vec A i \u2192 Vec A o\nrewireTbl tbl v = map (flip lookup v) tbl\n\non\u1d62 : \u2200 {a} {A : Set a} (f : A \u2192 A) {n} (i : Fin n) \u2192 Vec A n \u2192 Vec A n\non\u1d62 f zero (x \u2237 xs) = f x \u2237 xs\non\u1d62 f (suc i) (x \u2237 xs) = x \u2237 on\u1d62 f i xs\n\n-- Exchange elements at positions 0 and 1 of a given vector\n-- (this only apply if the vector is long enough).\n0\u21941 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n0\u21941 (x\u2080 \u2237 x\u2081 \u2237 xs) = x\u2081 \u2237 x\u2080 \u2237 xs\n0\u21941 xs = xs\n\nmap-tail : \u2200 {m n a} {A : Set a} \u2192 (Vec A m \u2192 Vec A n) \u2192 Vec A (suc m) \u2192 Vec A (suc n)\nmap-tail f (x \u2237 xs) = x \u2237 f xs\n\nmap-tail-id : \u2200 {n a} {A : Set a} \u2192 map-tail id \u2257 id {A = Vec A (suc n)}\nmap-tail-id (x \u2237 xs) = \u2261.refl\n\n-- \u27e8 i \u2194+1\u27e9: Exchange elements at position i and i + 1.\n\u27e8_\u2194+1\u27e9 : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u27e8 zero \u2194+1\u27e9 = 0\u21941\n\u27e8 suc i \u2194+1\u27e9 = map-tail \u27e8 i \u2194+1\u27e9\n\n-- rot-up-to i (x\u2080 \u2237 x\u2081 \u2237 x\u2082 \u2237 \u2026 \u2237 x\u1d62 \u2237 xs)\n-- \u2261 (x\u2081 \u2237 x\u2082 \u2237 x\u2083 \u2237 \u2026 \u2237 x\u2080 \u2237 xs)\nrot-up-to : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\nrot-up-to zero = id\nrot-up-to (suc i) = map-tail (rot-up-to i) \u2218 0\u21941\n\n-- Inverse of rot-up-to\nrot\u207b\u00b9-up-to : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\nrot\u207b\u00b9-up-to zero = id\nrot\u207b\u00b9-up-to (suc i) = 0\u21941 \u2218 map-tail (rot\u207b\u00b9-up-to i)\n\nrot\u207b\u00b9-up-to\u2218rot-up-to : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 rot\u207b\u00b9-up-to i \u2218 rot-up-to i \u2257 id {a} {Vec A n}\nrot\u207b\u00b9-up-to\u2218rot-up-to zero _ = \u2261.refl\nrot\u207b\u00b9-up-to\u2218rot-up-to (suc i) {A = A} (x\u2080 \u2237 []) rewrite rot\u207b\u00b9-up-to\u2218rot-up-to i {A = A} [] = \u2261.refl\nrot\u207b\u00b9-up-to\u2218rot-up-to (suc i) (x\u2080 \u2237 x\u2081 \u2237 xs) rewrite rot\u207b\u00b9-up-to\u2218rot-up-to i (x\u2080 \u2237 xs) = \u2261.refl\n\n-- \u27e80\u2194 i \u27e9: Exchange elements at position 0 and i.\n\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u27e80\u2194 zero \u27e9 = id\n\u27e80\u2194 suc i \u27e9 = rot\u207b\u00b9-up-to (inject\u2081 i) \u2218 rot-up-to (suc i)\n\n\u27e80\u2194zero\u27e9 : \u2200 {n a} {A : Set a} \u2192 \u27e80\u2194 zero \u27e9 \u2257 id {A = Vec A (suc n)}\n\u27e80\u2194zero\u27e9 _ = \u2261.refl\n\n-- \u27e8 i \u2194 j \u27e9: Exchange elements at position i and j.\n\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u27e8 i \u2194 j \u27e9 = \u27e80\u2194 i \u27e9 \u2218 \u27e80\u2194 j \u27e9 \u2218 \u27e80\u2194 i \u27e9\n\n\u27e80\u2194\u27e9-spec : \u2200 {n a} {A : Set a} (i : Fin (suc n)) \u2192 \u27e80\u2194 i \u27e9 \u2257 \u27e8 zero \u2194 i \u27e9 {A = A}\n\u27e80\u2194\u27e9-spec _ _ = \u2261.refl\n\n\u27e8_\u2194_\u27e9\u2032 : \u2200 {n} (i j : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u27e8 zero \u2194 j \u27e9\u2032 = \u27e80\u2194 j \u27e9\n\u27e8 i \u2194 zero \u27e9\u2032 = \u27e80\u2194 i \u27e9\n\u27e8 suc i \u2194 suc j \u27e9\u2032 = map-tail \u27e8 i \u2194 j \u27e9\u2032\n\n\u27e8\u2194\u27e9\u2032-comm : \u2200 {n a} {A : Set a} (i j : Fin n) \u2192 \u27e8 i \u2194 j \u27e9\u2032 \u2257 \u27e8 j \u2194 i \u27e9\u2032 {A = A}\n\u27e8\u2194\u27e9\u2032-comm zero zero _ = \u2261.refl\n\u27e8\u2194\u27e9\u2032-comm zero (suc _) _ = \u2261.refl\n\u27e8\u2194\u27e9\u2032-comm (suc _) zero _ = \u2261.refl\n\u27e8\u2194\u27e9\u2032-comm (suc i) (suc j) (x \u2237 xs) rewrite \u27e8\u2194\u27e9\u2032-comm i j xs = \u2261.refl\n\n\u27e8\u2194+1\u27e9-spec : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 \u27e8 inject\u2081 i \u2194+1\u27e9 \u2257 \u27e8 inject\u2081 i \u2194 suc i \u27e9\u2032 {A = A}\n\u27e8\u2194+1\u27e9-spec zero xs rewrite map-tail-id (0\u21941 xs) = \u2261.refl\n\u27e8\u2194+1\u27e9-spec (suc i) (x \u2237 xs) rewrite \u27e8\u2194+1\u27e9-spec i xs = \u2261.refl\n\n\u27e80\u2194_\u27e9\u2032 : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u27e80\u2194_\u27e9\u2032 {zero} i xs = xs\n\u27e80\u2194_\u27e9\u2032 {suc n} i xs = lookup i xs \u2237 tail (xs [ i ]\u2254 head xs)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"c12d0f122a68f42ab685380279a40c4bed632639","subject":"Added thesis' examples.","message":"Added thesis' examples.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/thesis\/LogicalFramework\/AdequacyTheorems.agda","new_file":"notes\/thesis\/LogicalFramework\/AdequacyTheorems.agda","new_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule LogicalFramework.AdequacyTheorems where\n\nmodule Example5 where\n\n -- First-order logic with equality.\n open import Common.FOL.FOL-Eq public\n\n postulate\n A B C : Set\n f\u2081 : A \u2192 C\n f\u2082 : B \u2192 C\n\n g : (A \u2192 C) \u2192 (B \u2192 C) \u2192 A \u2228 B \u2192 C\n g f\u2081 f\u2082 (inj\u2081 a) = f\u2081 a\n g f\u2081 f\u2082 (inj\u2082 b) = f\u2082 b\n\n g' : (A \u2192 C) \u2192 (B \u2192 C) \u2192 A \u2228 B \u2192 C\n g' f\u2081 f\u2082 x = case f\u2081 f\u2082 x\n\nmodule Example7 where\n\n -- First-order logic with equality.\n open import Common.FOL.FOL-Eq public\n\n postulate\n C : D \u2192 D \u2192 Set\n d : \u2200 {a} \u2192 C a a\n\n g : \u2200 {a b} \u2192 a \u2261 b \u2192 C a b\n g refl = d\n\n g' : \u2200 {a b} \u2192 a \u2261 b \u2192 C a b\n g' {a} h = subst (\u03bb x \u2192 C a x) h d\n\nmodule Example10 where\n\n -- First-order logic with equality.\n open import Common.FOL.FOL-Eq public\n\n postulate\n A B C : Set\n f\u2081 : A \u2192 C\n f\u2082 : B \u2192 C\n\n f : A \u2228 B \u2192 C\n f (inj\u2081 a) = f\u2081 a\n f (inj\u2082 b) = f\u2082 b\n\n f' : A \u2228 B \u2192 C\n f' = case f\u2081 f\u2082\n\nmodule Example20 where\n\n -- First-order logic with equality.\n open import Common.FOL.FOL-Eq public\n\n f : {A : D \u2192 Set}{t t' : D} \u2192 t \u2261 t' \u2192 A t \u2192 A t'\n f {A} {t} {.t} refl At = d At\n where\n postulate d : A t \u2192 A t\n\n f' : {A : D \u2192 Set}{t t' : D} \u2192 t \u2261 t' \u2192 A t \u2192 A t'\n f' {A} h At = subst A h At\n\nmodule Example30 where\n\n -- First-order logic with equality.\n open import Common.FOL.FOL-Eq public\n\n postulate\n A B C E : Set\n f\u2081 : A \u2192 E\n f\u2082 : B \u2192 E\n f\u2083 : C \u2192 E\n\n g : (A \u2228 B) \u2228 C \u2192 E\n g (inj\u2081 (inj\u2081 a)) = f\u2081 a\n g (inj\u2081 (inj\u2082 b)) = f\u2082 b\n g (inj\u2082 c) = f\u2083 c\n\n g' : (A \u2228 B) \u2228 C \u2192 E\n g' = case (case f\u2081 f\u2082) f\u2083\n\nmodule Example40 where\n\n infixl 9 _+_ _+'_\n infix 7 _\u2261_\n\n data M : Set where\n zero : M\n succ : M \u2192 M\n\n PA-ind : (A : M \u2192 Set) \u2192 A zero \u2192 (\u2200 n \u2192 A n \u2192 A (succ n)) \u2192 \u2200 n \u2192 A n\n PA-ind A A0 h zero = A0\n PA-ind A A0 h (succ n) = h n (PA-ind A A0 h n)\n\n data _\u2261_ (x : M) : M \u2192 Set where\n refl : x \u2261 x\n\n subst : (A : M \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\n subst A refl Ax = Ax\n\n _+_ : M \u2192 M \u2192 M\n zero + n = n\n succ m + n = succ (m + n)\n\n _+'_ : M \u2192 M \u2192 M\n m +' n = PA-ind (\u03bb _ \u2192 M) n (\u03bb x y \u2192 succ y) m\n\n -- Properties using pattern matching.\n succCong : \u2200 {m n} \u2192 m \u2261 n \u2192 succ m \u2261 succ n\n succCong refl = refl\n\n +-rightIdentity : \u2200 n \u2192 n + zero \u2261 n\n +-rightIdentity zero = refl\n +-rightIdentity (succ n) = succCong (+-rightIdentity n)\n\n -- Properties using the basic inductive constants.\n succCong' : \u2200 {m n} \u2192 m \u2261 n \u2192 succ m \u2261 succ n\n succCong' {m} h = subst (\u03bb x \u2192 succ m \u2261 succ x) h refl\n\n +'-leftIdentity : \u2200 n \u2192 zero +' n \u2261 n\n +'-leftIdentity n = refl\n\n +'-rightIdentity : \u2200 n \u2192 n +' zero \u2261 n\n +'-rightIdentity = PA-ind A A0 is\n where\n A : M \u2192 Set\n A n = n +' zero \u2261 n\n\n A0 : A zero\n A0 = refl\n\n is : \u2200 n \u2192 A n \u2192 A (succ n)\n is n ih = succCong' ih\n","old_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule LogicalFramework.AdequacyTheorems where\n\nmodule Example10 where\n\n -- First-order logic with equality.\n open import Common.FOL.FOL-Eq public\n\n postulate\n A B C : Set\n f\u2081 : A \u2192 C\n f\u2082 : B \u2192 C\n\n f : A \u2228 B \u2192 C\n f (inj\u2081 a) = f\u2081 a\n f (inj\u2082 b) = f\u2082 b\n\n f' : A \u2228 B \u2192 C\n f' = case f\u2081 f\u2082\n\nmodule Example20 where\n\n -- First-order logic with equality.\n open import Common.FOL.FOL-Eq public\n\n f : {A : D \u2192 Set}{t t' : D} \u2192 t \u2261 t' \u2192 A t \u2192 A t'\n f {A} {t} {.t} refl At = d At\n where\n postulate d : A t \u2192 A t\n\n f' : {A : D \u2192 Set}{t t' : D} \u2192 t \u2261 t' \u2192 A t \u2192 A t'\n f' {A} h At = subst A h At\n\nmodule Example30 where\n\n -- First-order logic with equality.\n open import Common.FOL.FOL-Eq public\n\n postulate\n A B C E : Set\n f\u2081 : A \u2192 E\n f\u2082 : B \u2192 E\n f\u2083 : C \u2192 E\n\n f : (A \u2228 B) \u2228 C \u2192 E\n f (inj\u2081 (inj\u2081 a)) = f\u2081 a\n f (inj\u2081 (inj\u2082 b)) = f\u2082 b\n f (inj\u2082 c) = f\u2083 c\n\n f' : (A \u2228 B) \u2228 C \u2192 E\n f' = case (case f\u2081 f\u2082) f\u2083\n\nmodule Example40 where\n\n infixl 9 _+_ _+'_\n infix 7 _\u2261_\n\n data M : Set where\n zero : M\n succ : M \u2192 M\n\n PA-ind : (A : M \u2192 Set) \u2192 A zero \u2192 (\u2200 n \u2192 A n \u2192 A (succ n)) \u2192 \u2200 n \u2192 A n\n PA-ind A A0 h zero = A0\n PA-ind A A0 h (succ n) = h n (PA-ind A A0 h n)\n\n data _\u2261_ (x : M) : M \u2192 Set where\n refl : x \u2261 x\n\n subst : (A : M \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\n subst A refl Ax = Ax\n\n _+_ : M \u2192 M \u2192 M\n zero + n = n\n succ m + n = succ (m + n)\n\n _+'_ : M \u2192 M \u2192 M\n m +' n = PA-ind (\u03bb _ \u2192 M) n (\u03bb x y \u2192 succ y) m\n\n -- Properties using pattern matching.\n succCong : \u2200 {m n} \u2192 m \u2261 n \u2192 succ m \u2261 succ n\n succCong refl = refl\n\n +-rightIdentity : \u2200 n \u2192 n + zero \u2261 n\n +-rightIdentity zero = refl\n +-rightIdentity (succ n) = succCong (+-rightIdentity n)\n\n -- Properties using the basic inductive constants.\n succCong' : \u2200 {m n} \u2192 m \u2261 n \u2192 succ m \u2261 succ n\n succCong' {m} h = subst (\u03bb x \u2192 succ m \u2261 succ x) h refl\n\n +'-leftIdentity : \u2200 n \u2192 zero +' n \u2261 n\n +'-leftIdentity n = refl\n\n +'-rightIdentity : \u2200 n \u2192 n +' zero \u2261 n\n +'-rightIdentity = PA-ind A A0 is\n where\n A : M \u2192 Set\n A n = n +' zero \u2261 n\n\n A0 : A zero\n A0 = refl\n\n is : \u2200 n \u2192 A n \u2192 A (succ n)\n is n ih = succCong' ih\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"64aeff24b29bf417d3c1ddff59acd073ff749e88","subject":"scraps","message":"scraps\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"lemmas-subst-ta.agda","new_file":"lemmas-subst-ta.agda","new_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import lemmas-disjointness\nopen import structural\n\nmodule lemmas-subst-ta where\n lem-subst-\u03c3 : \u2200{\u0394 x \u0393 \u03c41 \u03c3 \u0393' d } \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 Subst d x \u03c3 :s: \u0393'\n lem-subst-\u03c3 s wt = STASubst s wt\n\n -- todo: i'm worried this may actually be false without knowing that x \u2260\n -- y, but that's kind of what we usually mean on paper anyway\n exchange-subst-\u0393 : \u2200{\u0394 \u0393 x y \u03c41 \u03c42 \u03c3 \u0393'} \u2192\n x \u2260 y \u2192\n \u0394 , (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) \u22a2 \u03c3 :s: \u0393'\n exchange-subst-\u0393 {\u0394} {\u0393} {x} {y} {\u03c41} {\u03c42} {\u03c3} {\u0393'} x\u2260y xy = tr (\u03bb qq \u2192 \u0394 , qq \u22a2 \u03c3 :s: \u0393') (funext swap) xy\n where\n swap : (z : Nat) \u2192 (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) z == (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) z\n swap = {!!}\n\n mutual\n data envfresh : Nat \u2192 env \u2192 Set where\n EFId : \u2200{x \u0393} \u2192 x # \u0393 \u2192 envfresh x (Id \u0393)\n EFSubst : \u2200{x d \u03c3 y} \u2192 fresh x d\n \u2192 envfresh x \u03c3\n \u2192 x \u2260 y\n \u2192 envfresh x (Subst d y \u03c3)\n\n data fresh : Nat \u2192 dhexp \u2192 Set where\n FConst : \u2200{x} \u2192 fresh x c\n FVar : \u2200{x y} \u2192 x \u2260 y \u2192 fresh x (X y)\n FLam : \u2200{x y \u03c4 d} \u2192 x \u2260 y \u2192 fresh x d \u2192 fresh x (\u00b7\u03bb y [ \u03c4 ] d)\n FHole : \u2200{x u \u03c3} \u2192 envfresh x \u03c3 \u2192 fresh x (\u2987\u2988\u27e8 u , \u03c3 \u27e9)\n FNEHole : \u2200{x d u \u03c3} \u2192 envfresh x \u03c3 \u2192 fresh x d \u2192 fresh x (\u2987 d \u2988\u27e8 u , \u03c3 \u27e9)\n FAp : \u2200{x d1 d2} \u2192 fresh x d1 \u2192 fresh x d2 \u2192 fresh x (d1 \u2218 d2)\n FCast : \u2200{x d \u03c41 \u03c42} \u2192 fresh x d \u2192 fresh x (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9)\n FFailedCast : \u2200{x d \u03c41 \u03c42} \u2192 fresh x d \u2192 fresh x (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n\n mutual\n weaken-subst-\u0393 : \u2200{ x \u0393 \u0394 \u03c3 \u0393' \u03c4} \u2192\n envfresh x \u03c3 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , (\u0393 ,, (x , \u03c4)) \u22a2 \u03c3 :s: \u0393'\n weaken-subst-\u0393 {\u0393 = \u0393} (EFId x\u2081) (STAId x\u2082) = STAId (\u03bb x \u03c4 x\u2083 \u2192 x\u2208\u222al \u0393 _ x \u03c4 (x\u2082 x \u03c4 x\u2083) )\n weaken-subst-\u0393 {x = x} {\u0393 = \u0393} (EFSubst x\u2081 efrsh x\u2082) (STASubst {y = y} {\u03c4 = \u03c4'} subst x\u2083) =\n STASubst (exchange-subst-\u0393 {\u0393 = \u0393} (flip x\u2082) (weaken-subst-\u0393 {\u0393 = \u0393 ,, (y , \u03c4')} efrsh subst))\n (weaken-ta x\u2081 x\u2083)\n\n weaken-ta : \u2200{x \u0393 \u0394 d \u03c4 \u03c4'} \u2192\n fresh x d \u2192 -- x # \u0393 ?\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 ,, (x , \u03c4') \u22a2 d :: \u03c4\n weaken-ta _ TAConst = TAConst\n weaken-ta {x} {\u0393} {_} {_} {\u03c4} {\u03c4'} (FVar x\u2082) (TAVar x\u2083) = TAVar (x\u2208\u222al \u0393 (\u25a0 (x , \u03c4')) _ _ x\u2083)\n weaken-ta {x = x} frsh (TALam {x = y} x\u2082 wt) with natEQ x y\n weaken-ta (FLam x\u2081 x\u2082) (TALam x\u2083 wt) | Inl refl = abort (x\u2081 refl)\n weaken-ta {\u0393 = \u0393} {\u03c4' = \u03c4'} (FLam x\u2081 x\u2083) (TALam {x = y} x\u2084 wt) | Inr x\u2082 = TALam (apart-parts \u0393 _ _ x\u2084 (apart-singleton (flip x\u2081)))\n {!weaken-ta {\u0393 = \u0393 ,, (y , ?)} x\u2083 wt!}\n weaken-ta (FAp frsh frsh\u2081) (TAAp wt wt\u2081) = TAAp (weaken-ta frsh wt) (weaken-ta frsh\u2081 wt\u2081)\n weaken-ta (FHole x\u2081) (TAEHole x\u2082 x\u2083) = TAEHole x\u2082 (weaken-subst-\u0393 x\u2081 x\u2083)\n weaken-ta (FNEHole x\u2081 frsh) (TANEHole x\u2082 wt x\u2083) = TANEHole x\u2082 (weaken-ta frsh wt) (weaken-subst-\u0393 x\u2081 x\u2083)\n weaken-ta (FCast frsh) (TACast wt x\u2081) = TACast (weaken-ta frsh wt) x\u2081\n weaken-ta (FFailedCast frsh) (TAFailedCast wt x\u2081 x\u2082 x\u2083) = TAFailedCast (weaken-ta frsh wt) x\u2081 x\u2082 x\u2083\n\n lem-subst : \u2200{\u0394 \u0393 x \u03c41 d1 \u03c4 d2 } \u2192\n x # \u0393 \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 d1 :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 [ d2 \/ x ] d1 :: \u03c4\n lem-subst apt TAConst wt2 = TAConst\n lem-subst {x = x} apt (TAVar {x = x'} x\u2082) wt2 with natEQ x' x\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inl refl with lem-apart-union-eq {\u0393 = \u0393} apt x\u2083\n lem-subst apt (TAVar x\u2083) wt2 | Inl refl | refl = wt2\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inr x\u2082 = TAVar (lem-neq-union-eq {\u0393 = \u0393} x\u2082 x\u2083)\n lem-subst {\u0393 = \u0393} {x = x} apt (TALam {x = y} x\u2082 wt1) wt2 -- = {!weaken-ta ? (TALam x\u2082 wt1)!}\n with natEQ y x\n lem-subst {\u0393 = \u0393} {x = x} apt (TALam x\u2083 wt1) wt2 | Inl refl = abort ((\u03c01(lem-union-none {\u0393 = \u0393} x\u2083)) refl)\n lem-subst {\u0393 = \u0393} apt (TALam x\u2083 wt1) wt2 | Inr x\u2082\n with lem-union-none {\u0393 = \u0393} x\u2083 -- | lem-subst apt wt1 -- probably not the straight IH; need to weaken\n ... | neq , r = {!!} -- TALam r (lem-subst {!!} (weaken-ta {!!} {!!}) (weaken-ta r wt2))\n lem-subst apt (TAAp wt1 wt2) wt3 = TAAp (lem-subst apt wt1 wt3) (lem-subst apt wt2 wt3)\n lem-subst apt (TAEHole in\u0394 sub) wt2 = TAEHole in\u0394 (lem-subst-\u03c3 sub wt2)\n lem-subst apt (TANEHole x\u2081 wt1 x\u2082) wt2 = TANEHole x\u2081 (lem-subst apt wt1 wt2) (lem-subst-\u03c3 x\u2082 wt2)\n lem-subst apt (TACast wt1 x\u2081) wt2 = TACast (lem-subst apt wt1 wt2) x\u2081\n lem-subst apt (TAFailedCast wt1 x\u2081 x\u2082 x\u2083) wt2 = TAFailedCast (lem-subst apt wt1 wt2) x\u2081 x\u2082 x\u2083\n","old_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import lemmas-disjointness\nopen import structural\n\nmodule lemmas-subst-ta where\n lem-subst-\u03c3 : \u2200{\u0394 x \u0393 \u03c41 \u03c3 \u0393' d } \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 Subst d x \u03c3 :s: \u0393'\n lem-subst-\u03c3 s wt = STASubst s wt\n\n -- todo: i'm worried this may actually be false without knowing that x \u2260\n -- y, but that's kind of what we usually mean on paper anyway\n exchange-subst-\u0393 : \u2200{\u0394 \u0393 x y \u03c41 \u03c42 \u03c3 \u0393'} \u2192\n x \u2260 y \u2192\n \u0394 , (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) \u22a2 \u03c3 :s: \u0393'\n exchange-subst-\u0393 {\u0394} {\u0393} {x} {y} {\u03c41} {\u03c42} {\u03c3} {\u0393'} x\u2260y xy = tr (\u03bb qq \u2192 \u0394 , qq \u22a2 \u03c3 :s: \u0393') (funext swap) xy\n where\n swap : (z : Nat) \u2192 (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) z == (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) z\n swap = {!!}\n\n mutual\n data envfresh : Nat \u2192 env \u2192 Set where\n EFId : \u2200{x \u0393} \u2192 x # \u0393 \u2192 envfresh x (Id \u0393)\n EFSubst : \u2200{x d \u03c3 y} \u2192 fresh x d\n \u2192 envfresh x \u03c3\n \u2192 x \u2260 y\n \u2192 envfresh x (Subst d y \u03c3)\n\n data fresh : Nat \u2192 dhexp \u2192 Set where\n FConst : \u2200{x} \u2192 fresh x c\n FVar : \u2200{x y} \u2192 x \u2260 y \u2192 fresh x (X y)\n FLam : \u2200{x y \u03c4 d} \u2192 x \u2260 y \u2192 fresh x d \u2192 fresh x (\u00b7\u03bb y [ \u03c4 ] d)\n FHole : \u2200{x u \u03c3} \u2192 envfresh x \u03c3 \u2192 fresh x (\u2987\u2988\u27e8 u , \u03c3 \u27e9)\n FNEHole : \u2200{x d u \u03c3} \u2192 envfresh x \u03c3 \u2192 fresh x d \u2192 fresh x (\u2987 d \u2988\u27e8 u , \u03c3 \u27e9)\n FAp : \u2200{x d1 d2} \u2192 fresh x d1 \u2192 fresh x d2 \u2192 fresh x (d1 \u2218 d2)\n FCast : \u2200{x d \u03c41 \u03c42} \u2192 fresh x d \u2192 fresh x (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9)\n FFailedCast : \u2200{x d \u03c41 \u03c42} \u2192 fresh x d \u2192 fresh x (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n\n mutual\n weaken-subst-\u0393 : \u2200{ x \u0393 \u0394 \u03c3 \u0393' \u03c4} \u2192\n envfresh x \u03c3 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , (\u0393 ,, (x , \u03c4)) \u22a2 \u03c3 :s: \u0393'\n weaken-subst-\u0393 {\u0393 = \u0393} (EFId x\u2081) (STAId x\u2082) = STAId (\u03bb x \u03c4 x\u2083 \u2192 x\u2208\u222al \u0393 _ x \u03c4 (x\u2082 x \u03c4 x\u2083) )\n weaken-subst-\u0393 {x = x} {\u0393 = \u0393} (EFSubst x\u2081 efrsh x\u2082) (STASubst {y = y} {\u03c4 = \u03c4'} subst x\u2083) =\n STASubst (exchange-subst-\u0393 {\u0393 = \u0393} (flip x\u2082) (weaken-subst-\u0393 {\u0393 = \u0393 ,, (y , \u03c4')} efrsh subst))\n (weaken-ta x\u2081 x\u2083)\n\n weaken-ta : \u2200{x \u0393 \u0394 d \u03c4 \u03c4'} \u2192\n fresh x d \u2192 -- x # \u0393 ?\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 ,, (x , \u03c4') \u22a2 d :: \u03c4\n weaken-ta _ TAConst = TAConst\n weaken-ta {x} {\u0393} {_} {_} {\u03c4} {\u03c4'} (FVar x\u2082) (TAVar x\u2083) = TAVar (x\u2208\u222al \u0393 (\u25a0 (x , \u03c4')) _ _ x\u2083)\n weaken-ta {x = x} frsh (TALam {x = y} x\u2082 wt) with natEQ x y\n weaken-ta (FLam x\u2081 x\u2082) (TALam x\u2083 wt) | Inl refl = abort (x\u2081 refl)\n weaken-ta {\u0393 = \u0393} {\u03c4' = \u03c4'} (FLam x\u2081 x\u2083) (TALam {x = y} x\u2084 wt) | Inr x\u2082 = TALam (apart-parts \u0393 _ _ x\u2084 (apart-singleton (flip x\u2081))) (weaken-ta {\u0393 = {!\u25a0 (y , ? )!} \u222a \u0393} x\u2083 wt)\n weaken-ta (FAp frsh frsh\u2081) (TAAp wt wt\u2081) = TAAp (weaken-ta frsh wt) (weaken-ta frsh\u2081 wt\u2081)\n weaken-ta (FHole x\u2081) (TAEHole x\u2082 x\u2083) = TAEHole x\u2082 (weaken-subst-\u0393 x\u2081 x\u2083)\n weaken-ta (FNEHole x\u2081 frsh) (TANEHole x\u2082 wt x\u2083) = TANEHole x\u2082 (weaken-ta frsh wt) (weaken-subst-\u0393 x\u2081 x\u2083)\n weaken-ta (FCast frsh) (TACast wt x\u2081) = TACast (weaken-ta frsh wt) x\u2081\n weaken-ta (FFailedCast frsh) (TAFailedCast wt x\u2081 x\u2082 x\u2083) = TAFailedCast (weaken-ta frsh wt) x\u2081 x\u2082 x\u2083\n\n lem-subst : \u2200{\u0394 \u0393 x \u03c41 d1 \u03c4 d2 } \u2192\n x # \u0393 \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 d1 :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 [ d2 \/ x ] d1 :: \u03c4\n lem-subst apt TAConst wt2 = TAConst\n lem-subst {x = x} apt (TAVar {x = x'} x\u2082) wt2 with natEQ x' x\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inl refl with lem-apart-union-eq {\u0393 = \u0393} apt x\u2083\n lem-subst apt (TAVar x\u2083) wt2 | Inl refl | refl = wt2\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inr x\u2082 = TAVar (lem-neq-union-eq {\u0393 = \u0393} x\u2082 x\u2083)\n lem-subst {\u0393 = \u0393} {x = x} apt (TALam {x = y} x\u2082 wt1) wt2 -- = {!weaken-ta ? (TALam x\u2082 wt1)!}\n with natEQ y x\n lem-subst {\u0393 = \u0393} {x = x} apt (TALam x\u2083 wt1) wt2 | Inl refl = abort ((\u03c01(lem-union-none {\u0393 = \u0393} x\u2083)) refl)\n lem-subst {\u0393 = \u0393} apt (TALam x\u2083 wt1) wt2 | Inr x\u2082\n with lem-union-none {\u0393 = \u0393} x\u2083 -- | lem-subst apt wt1 -- probably not the straight IH; need to weaken\n ... | neq , r = {!!} -- TALam r (lem-subst {!!} (weaken-ta {!!} {!!}) (weaken-ta r wt2))\n lem-subst apt (TAAp wt1 wt2) wt3 = TAAp (lem-subst apt wt1 wt3) (lem-subst apt wt2 wt3)\n lem-subst apt (TAEHole in\u0394 sub) wt2 = TAEHole in\u0394 (lem-subst-\u03c3 sub wt2)\n lem-subst apt (TANEHole x\u2081 wt1 x\u2082) wt2 = TANEHole x\u2081 (lem-subst apt wt1 wt2) (lem-subst-\u03c3 x\u2082 wt2)\n lem-subst apt (TACast wt1 x\u2081) wt2 = TACast (lem-subst apt wt1 wt2) x\u2081\n lem-subst apt (TAFailedCast wt1 x\u2081 x\u2082 x\u2083) wt2 = TAFailedCast (lem-subst apt wt1 wt2) x\u2081 x\u2082 x\u2083\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"aa4bab642a60975cadb113e2eddba536d1671fc3","subject":"Add doc.","message":"Add doc.\n\nIgnore-this: 88c8569ac71a4dadfb6bb8be36321c8f\n\ndarcs-hash:20110102201203-3bd4e-b02ac257d8d12f84b6f4a78bf73e3b3c41da75c1.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/LTC\/Program\/SortList\/Properties\/Closures\/OrdList\/FlattenI.agda","new_file":"src\/LTC\/Program\/SortList\/Properties\/Closures\/OrdList\/FlattenI.agda","new_contents":"------------------------------------------------------------------------------\n-- Closures properties respect to OrdList (flatten-OrdList-aux)\n------------------------------------------------------------------------------\n\n-- Agda bug? The recursive calls in flatten-OrdList-aux are\n-- structurally smaller, so it is not clear why we need the option\n-- --no-termination-check. We tested the option --termination-depth=N\n-- with some values, but it had no effect.\n{-# OPTIONS --no-termination-check #-}\n\nmodule LTC.Program.SortList.Properties.Closures.OrdList.FlattenI where\n\nopen import LTC.Base\n\n-- open import Common.Function\n\nopen import LTC.Data.Bool\nopen import LTC.Data.Bool.PropertiesI\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesI\nopen import LTC.Data.Nat.Type\nopen import LTC.Data.List\n\nopen import LTC.Program.SortList.Properties.Closures.BoolI\nopen import LTC.Program.SortList.Properties.Closures.ListN-I\nopen import LTC.Program.SortList.Properties.Closures.OrdTreeI\nopen import LTC.Program.SortList.Properties.MiscellaneousI\nopen import LTC.Program.SortList.SortList\n\nopen import LTC.Relation.Binary.EqReasoning\n\n------------------------------------------------------------------------------\n\nflatten-OrdList-aux : {t\u2081 i t\u2082 : D} \u2192 Tree t\u2081 \u2192 N i \u2192 Tree t\u2082 \u2192\n OrdTree (node t\u2081 i t\u2082) \u2192\n LE-Lists (flatten t\u2081) (flatten t\u2082)\n\nflatten-OrdList-aux {t\u2082 = t\u2082} nilT Ni Tt\u2082 OTt =\n subst (\u03bb t \u2192 LE-Lists t (flatten t\u2082))\n (sym (flatten-nilTree))\n (\u2264-Lists-[] (flatten t\u2082))\n\nflatten-OrdList-aux (tipT {i\u2081} Ni\u2081) _ nilT OTt =\n begin\n \u2264-Lists (flatten (tip i\u2081)) (flatten nilTree)\n \u2261\u27e8 subst\u2082 (\u03bb x\u2081 x\u2082 \u2192 \u2264-Lists (flatten (tip i\u2081)) (flatten nilTree) \u2261\n \u2264-Lists x\u2081 x\u2082)\n (flatten-tip i\u2081)\n flatten-nilTree\n refl\n \u27e9\n \u2264-Lists (i\u2081 \u2237 []) []\n \u2261\u27e8 \u2264-Lists-\u2237 i\u2081 [] [] \u27e9\n \u2264-ItemList i\u2081 [] && \u2264-Lists [] []\n \u2261\u27e8 subst\u2082 (\u03bb x\u2081 x\u2082 \u2192 \u2264-ItemList i\u2081 [] && \u2264-Lists [] [] \u2261 x\u2081 && x\u2082 )\n (\u2264-ItemList-[] i\u2081)\n (\u2264-Lists-[] [])\n refl\n \u27e9\n true && true\n \u2261\u27e8 &&-tt \u27e9\n true\n \u220e\n\nflatten-OrdList-aux {i = i} (tipT {i\u2081} Ni\u2081) Ni (tipT {i\u2082} Ni\u2082) OTt =\n begin\n \u2264-Lists (flatten (tip i\u2081)) (flatten (tip i\u2082))\n \u2261\u27e8 subst\u2082 (\u03bb x\u2081 x\u2082 \u2192 \u2264-Lists (flatten (tip i\u2081)) (flatten (tip i\u2082)) \u2261\n \u2264-Lists x\u2081 x\u2082)\n (flatten-tip i\u2081)\n (flatten-tip i\u2082)\n refl\n \u27e9\n \u2264-Lists (i\u2081 \u2237 []) (i\u2082 \u2237 [])\n \u2261\u27e8 \u2264-Lists-\u2237 i\u2081 [] (i\u2082 \u2237 []) \u27e9\n \u2264-ItemList i\u2081 (i\u2082 \u2237 []) && \u2264-Lists [] (i\u2082 \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 \u2264-ItemList i\u2081 (i\u2082 \u2237 []) && \u2264-Lists [] (i\u2082 \u2237 []) \u2261\n t && \u2264-Lists [] (i\u2082 \u2237 []))\n (\u2264-ItemList-\u2237 i\u2081 i\u2082 [])\n refl\n \u27e9\n (i\u2081 \u2264 i\u2082 && \u2264-ItemList i\u2081 []) && \u2264-Lists [] (i\u2082 \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 (i\u2081 \u2264 i\u2082 && \u2264-ItemList i\u2081 []) && \u2264-Lists [] (i\u2082 \u2237 []) \u2261\n (t && \u2264-ItemList i\u2081 []) && \u2264-Lists [] (i\u2082 \u2237 []))\n lemma\n refl\n \u27e9\n (true && \u2264-ItemList i\u2081 []) && \u2264-Lists [] (i\u2082 \u2237 [])\n \u2261\u27e8 subst\u2082 (\u03bb x\u2081 x\u2082 \u2192 (true && \u2264-ItemList i\u2081 []) && \u2264-Lists [] (i\u2082 \u2237 []) \u2261\n (true && x\u2081) && x\u2082)\n (\u2264-ItemList-[] i\u2081)\n (\u2264-Lists-[] (i\u2082 \u2237 []))\n refl\n \u27e9\n (true && true) && true\n \u2261\u27e8 &&-assoc tB tB tB \u27e9\n true && true && true\n \u2261\u27e8 &&-true\u2083 \u27e9\n true\n \u220e\n where\n aux\u2081 : Bool (ordTree (tip i\u2081))\n aux\u2081 = ordTree-Bool (tipT Ni\u2081)\n\n aux\u2082 : Bool (ordTree (tip i\u2082))\n aux\u2082 = ordTree-Bool (tipT Ni\u2082)\n\n aux\u2083 : Bool (\u2264-TreeItem (tip i\u2081) i)\n aux\u2083 = \u2264-TreeItem-Bool (tipT Ni\u2081) Ni\n\n aux\u2084 : Bool (\u2264-ItemTree i (tip i\u2082))\n aux\u2084 = \u2264-ItemTree-Bool Ni (tipT Ni\u2082)\n\n aux\u2085 : ordTree (tip i\u2081) &&\n ordTree (tip i\u2082) &&\n \u2264-TreeItem (tip i\u2081) i &&\n \u2264-ItemTree i (tip i\u2082) \u2261 true\n aux\u2085 = trans (sym (ordTree-node (tip i\u2081) i (tip i\u2082))) OTt\n\n lemma : LE i\u2081 i\u2082\n lemma = \u2264-trans Ni\u2081 Ni Ni\u2082 i\u2081\u2264i i\u2264i\u2082\n where\n i\u2081\u2264i : LE i\u2081 i\n i\u2081\u2264i = trans (sym (\u2264-TreeItem-tip i\u2081 i))\n (&&\u2083-proj\u2083 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n i\u2264i\u2082 : LE i i\u2082\n i\u2264i\u2082 = trans (sym (\u2264-ItemTree-tip i i\u2082))\n (&&\u2083-proj\u2084 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\nflatten-OrdList-aux {i = i} (tipT {i\u2081} Ni\u2081) Ni\n (nodeT {t\u2082\u2081} {i\u2082} {t\u2082\u2082} Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082) OTt =\n begin\n \u2264-Lists (flatten (tip i\u2081)) (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082))\n \u2261\u27e8 subst (\u03bb x \u2192 \u2264-Lists (flatten (tip i\u2081)) (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082)) \u2261\n \u2264-Lists (flatten (tip i\u2081)) x)\n (flatten-node t\u2082\u2081 i\u2082 t\u2082\u2082)\n refl\n \u27e9\n \u2264-Lists (flatten (tip i\u2081)) (flatten t\u2082\u2081 ++ flatten t\u2082\u2082)\n \u2261\u27e8 xs\u2264ys\u2192xs\u2264zs\u2192xs\u2264ys++zs (flatten-ListN (tipT Ni\u2081))\n (flatten-ListN Tt\u2082\u2081)\n (flatten-ListN Tt\u2082\u2082)\n lemma\u2081\n lemma\u2082\n \u27e9\n true\n \u220e\n where\n -- Auxiliary terms to get the conjuncts from OTt.\n aux\u2081 = ordTree-Bool (tipT Ni\u2081)\n aux\u2082 = ordTree-Bool (nodeT Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082)\n aux\u2083 = \u2264-TreeItem-Bool (tipT Ni\u2081) Ni\n aux\u2084 = \u2264-ItemTree-Bool Ni (nodeT Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082)\n aux\u2085 = trans (sym (ordTree-node (tip i\u2081) i (node t\u2082\u2081 i\u2082 t\u2082\u2082))) OTt\n\n -- Auxiliary terms to get the conjuncts from the fourth conjunct of OTt.\n aux\u2086 = \u2264-ItemTree-Bool Ni Tt\u2082\u2081\n aux\u2087 = \u2264-ItemTree-Bool Ni Tt\u2082\u2082\n aux\u2088 = trans (sym (\u2264-ItemTree-node i t\u2082\u2081 i\u2082 t\u2082\u2082))\n (&&\u2083-proj\u2084 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n -- Common terms for the lemma\u2081 and lemma\u2082.\n OrdTree-tip-i\u2081 : OrdTree (tip i\u2081)\n OrdTree-tip-i\u2081 = &&\u2083-proj\u2081 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085\n\n LE-TreeItem-tip-i\u2081-i : LE-TreeItem (tip i\u2081) i\n LE-TreeItem-tip-i\u2081-i = &&\u2083-proj\u2083 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085\n\n lemma\u2081 : LE-Lists (flatten (tip i\u2081)) (flatten t\u2082\u2081)\n lemma\u2081 = flatten-OrdList-aux (tipT Ni\u2081) Ni Tt\u2082\u2081 OT -- IH.\n where\n OrdTree-t\u2082\u2081 : OrdTree t\u2082\u2081\n OrdTree-t\u2082\u2081 = leftSubTree-OrdTree Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082\n (&&\u2083-proj\u2082 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-ItemTree-i-t\u2082\u2081 : LE-ItemTree i t\u2082\u2081\n LE-ItemTree-i-t\u2082\u2081 = &&-proj\u2081 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node (tip i\u2081) i t\u2082\u2081)\n OT =\n begin\n ordTree (node (tip i\u2081) i t\u2082\u2081)\n \u2261\u27e8 ordTree-node (tip i\u2081) i t\u2082\u2081 \u27e9\n ordTree (tip i\u2081) &&\n ordTree t\u2082\u2081 &&\n \u2264-TreeItem (tip i\u2081) i &&\n \u2264-ItemTree i t\u2082\u2081\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree (tip i\u2081) &&\n ordTree t\u2082\u2081 &&\n \u2264-TreeItem (tip i\u2081) i &&\n \u2264-ItemTree i t\u2082\u2081 \u2261\n w && x && y && z)\n OrdTree-tip-i\u2081\n OrdTree-t\u2082\u2081\n LE-TreeItem-tip-i\u2081-i\n LE-ItemTree-i-t\u2082\u2081\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n\n lemma\u2082 : LE-Lists (flatten (tip i\u2081)) (flatten t\u2082\u2082)\n lemma\u2082 = flatten-OrdList-aux (tipT Ni\u2081) Ni Tt\u2082\u2082 OT -- IH.\n where\n OrdTree-t\u2082\u2082 : OrdTree t\u2082\u2082\n OrdTree-t\u2082\u2082 = rightSubTree-OrdTree Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082\n (&&\u2083-proj\u2082 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-ItemTree-i-t\u2082\u2082 : LE-ItemTree i t\u2082\u2082\n LE-ItemTree-i-t\u2082\u2082 = &&-proj\u2082 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node (tip i\u2081) i t\u2082\u2082)\n OT =\n begin\n ordTree (node (tip i\u2081) i t\u2082\u2082)\n \u2261\u27e8 ordTree-node (tip i\u2081) i t\u2082\u2082 \u27e9\n ordTree (tip i\u2081) &&\n ordTree t\u2082\u2082 &&\n \u2264-TreeItem (tip i\u2081) i &&\n \u2264-ItemTree i t\u2082\u2082\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree (tip i\u2081) &&\n ordTree t\u2082\u2082 &&\n \u2264-TreeItem (tip i\u2081) i &&\n \u2264-ItemTree i t\u2082\u2082 \u2261\n w && x && y && z)\n OrdTree-tip-i\u2081\n OrdTree-t\u2082\u2082\n LE-TreeItem-tip-i\u2081-i\n LE-ItemTree-i-t\u2082\u2082\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n\nflatten-OrdList-aux {i = i} (nodeT {t\u2081\u2081} {i\u2081} {t\u2081\u2082} Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082) Ni nilT OTt =\n begin\n \u2264-Lists (flatten (node t\u2081\u2081 i\u2081 t\u2081\u2082)) (flatten nilTree)\n \u2261\u27e8 subst (\u03bb x \u2192 \u2264-Lists (flatten (node t\u2081\u2081 i\u2081 t\u2081\u2082)) (flatten nilTree) \u2261\n \u2264-Lists x (flatten nilTree))\n (flatten-node t\u2081\u2081 i\u2081 t\u2081\u2082 )\n refl\n \u27e9\n \u2264-Lists (flatten t\u2081\u2081 ++ flatten t\u2081\u2082) (flatten nilTree)\n \u2261\u27e8 xs\u2264zs\u2192ys\u2264zs\u2192xs++ys\u2264zs (flatten-ListN Tt\u2081\u2081)\n (flatten-ListN Tt\u2081\u2082)\n (flatten-ListN nilT)\n lemma\u2081\n lemma\u2082\n \u27e9\n true\n \u220e\n where\n -- Auxiliary terms to get the conjuncts from OTt.\n aux\u2081 = ordTree-Bool (nodeT Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082)\n aux\u2082 = ordTree-Bool nilT\n aux\u2083 = \u2264-TreeItem-Bool (nodeT Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082) Ni\n aux\u2084 = \u2264-ItemTree-Bool Ni nilT\n aux\u2085 = trans (sym (ordTree-node (node t\u2081\u2081 i\u2081 t\u2081\u2082) i nilTree )) OTt\n\n -- Auxiliary terms to get the conjuncts from the third conjunct of OTt.\n aux\u2086 = \u2264-TreeItem-Bool Tt\u2081\u2081 Ni\n aux\u2087 = \u2264-TreeItem-Bool Tt\u2081\u2082 Ni\n aux\u2088 = trans (sym (\u2264-TreeItem-node t\u2081\u2081 i\u2081 t\u2081\u2082 i))\n (&&\u2083-proj\u2083 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n -- Common terms for the lemma\u2081 and lemma\u2082.\n LE-ItemTree-i-niltree : LE-ItemTree i nilTree\n LE-ItemTree-i-niltree = &&\u2083-proj\u2084 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085\n\n lemma\u2081 : LE-Lists (flatten t\u2081\u2081) (flatten nilTree)\n lemma\u2081 = flatten-OrdList-aux Tt\u2081\u2081 Ni nilT OT -- IH.\n where\n OrdTree-t\u2081\u2081 : OrdTree t\u2081\u2081\n OrdTree-t\u2081\u2081 =\n leftSubTree-OrdTree Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082\n (&&\u2083-proj\u2081 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-TreeItem-t\u2081\u2081-i : LE-TreeItem t\u2081\u2081 i\n LE-TreeItem-t\u2081\u2081-i = &&-proj\u2081 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node t\u2081\u2081 i nilTree)\n OT =\n begin\n ordTree (node t\u2081\u2081 i nilTree )\n \u2261\u27e8 ordTree-node t\u2081\u2081 i nilTree \u27e9\n ordTree t\u2081\u2081 &&\n ordTree nilTree &&\n \u2264-TreeItem t\u2081\u2081 i &&\n \u2264-ItemTree i nilTree\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree t\u2081\u2081 &&\n ordTree nilTree &&\n \u2264-TreeItem t\u2081\u2081 i &&\n \u2264-ItemTree i nilTree \u2261\n w && x && y && z)\n OrdTree-t\u2081\u2081\n ordTree-nilTree\n LE-TreeItem-t\u2081\u2081-i\n LE-ItemTree-i-niltree\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n\n lemma\u2082 : LE-Lists (flatten t\u2081\u2082) (flatten nilTree)\n lemma\u2082 = flatten-OrdList-aux Tt\u2081\u2082 Ni nilT OT -- IH.\n where\n OrdTree-t\u2081\u2082 : OrdTree t\u2081\u2082\n OrdTree-t\u2081\u2082 =\n rightSubTree-OrdTree Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082 (&&\u2083-proj\u2081 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-TreeItem-t\u2081\u2082-i : LE-TreeItem t\u2081\u2082 i\n LE-TreeItem-t\u2081\u2082-i = &&-proj\u2082 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node t\u2081\u2082 i nilTree)\n OT =\n begin\n ordTree (node t\u2081\u2082 i nilTree )\n \u2261\u27e8 ordTree-node t\u2081\u2082 i nilTree \u27e9\n ordTree t\u2081\u2082 &&\n ordTree nilTree &&\n \u2264-TreeItem t\u2081\u2082 i &&\n \u2264-ItemTree i nilTree\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree t\u2081\u2082 &&\n ordTree nilTree &&\n \u2264-TreeItem t\u2081\u2082 i &&\n \u2264-ItemTree i nilTree \u2261\n w && x && y && z)\n OrdTree-t\u2081\u2082\n ordTree-nilTree\n LE-TreeItem-t\u2081\u2082-i\n LE-ItemTree-i-niltree\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n\nflatten-OrdList-aux {i = i} (nodeT {t\u2081\u2081} {i\u2081} {t\u2081\u2082} Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082) Ni\n (tipT {i\u2082} Ni\u2082) OTt =\n begin\n \u2264-Lists (flatten (node t\u2081\u2081 i\u2081 t\u2081\u2082)) (flatten (tip i\u2082))\n \u2261\u27e8 subst (\u03bb x \u2192 \u2264-Lists (flatten (node t\u2081\u2081 i\u2081 t\u2081\u2082)) (flatten (tip i\u2082)) \u2261\n \u2264-Lists x (flatten (tip i\u2082)))\n (flatten-node t\u2081\u2081 i\u2081 t\u2081\u2082 )\n refl\n \u27e9\n \u2264-Lists (flatten t\u2081\u2081 ++ flatten t\u2081\u2082) (flatten (tip i\u2082))\n \u2261\u27e8 xs\u2264zs\u2192ys\u2264zs\u2192xs++ys\u2264zs (flatten-ListN Tt\u2081\u2081)\n (flatten-ListN Tt\u2081\u2082)\n (flatten-ListN (tipT Ni\u2082))\n lemma\u2081\n lemma\u2082\n \u27e9\n true\n \u220e\n where\n -- Auxiliary terms to get the conjuncts from OTt.\n aux\u2081 = ordTree-Bool (nodeT Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082)\n aux\u2082 = ordTree-Bool (tipT Ni\u2082)\n aux\u2083 = \u2264-TreeItem-Bool (nodeT Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082) Ni\n aux\u2084 = \u2264-ItemTree-Bool Ni (tipT Ni\u2082)\n aux\u2085 = trans (sym (ordTree-node (node t\u2081\u2081 i\u2081 t\u2081\u2082) i (tip i\u2082))) OTt\n\n -- Auxiliary terms to get the conjuncts from the third conjunct of OTt.\n aux\u2086 = \u2264-TreeItem-Bool Tt\u2081\u2081 Ni\n aux\u2087 = \u2264-TreeItem-Bool Tt\u2081\u2082 Ni\n aux\u2088 = trans (sym (\u2264-TreeItem-node t\u2081\u2081 i\u2081 t\u2081\u2082 i))\n (&&\u2083-proj\u2083 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n -- Common terms for the lemma\u2081 and lemma\u2082.\n OrdTree-tip-i\u2082 : OrdTree (tip i\u2082)\n OrdTree-tip-i\u2082 = &&\u2083-proj\u2082 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085\n\n LE-ItemTree-i-tip-i\u2082 : LE-ItemTree i (tip i\u2082)\n LE-ItemTree-i-tip-i\u2082 = &&\u2083-proj\u2084 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085\n\n lemma\u2081 : LE-Lists (flatten t\u2081\u2081) (flatten (tip i\u2082))\n lemma\u2081 = flatten-OrdList-aux Tt\u2081\u2081 Ni (tipT Ni\u2082) OT -- IH.\n where\n OrdTree-t\u2081\u2081 : OrdTree t\u2081\u2081\n OrdTree-t\u2081\u2081 =\n leftSubTree-OrdTree Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082 (&&\u2083-proj\u2081 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-TreeItem-t\u2081\u2081-i : LE-TreeItem t\u2081\u2081 i\n LE-TreeItem-t\u2081\u2081-i = &&-proj\u2081 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node t\u2081\u2081 i (tip i\u2082))\n OT =\n begin\n ordTree (node t\u2081\u2081 i (tip i\u2082) )\n \u2261\u27e8 ordTree-node t\u2081\u2081 i (tip i\u2082) \u27e9\n ordTree t\u2081\u2081 &&\n ordTree (tip i\u2082) &&\n \u2264-TreeItem t\u2081\u2081 i &&\n \u2264-ItemTree i (tip i\u2082)\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree t\u2081\u2081 &&\n ordTree (tip i\u2082) &&\n \u2264-TreeItem t\u2081\u2081 i &&\n \u2264-ItemTree i (tip i\u2082) \u2261\n w && x && y && z)\n OrdTree-t\u2081\u2081\n OrdTree-tip-i\u2082\n LE-TreeItem-t\u2081\u2081-i\n LE-ItemTree-i-tip-i\u2082\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n\n lemma\u2082 : LE-Lists (flatten t\u2081\u2082) (flatten (tip i\u2082))\n lemma\u2082 = flatten-OrdList-aux Tt\u2081\u2082 Ni (tipT Ni\u2082) OT -- IH.\n where\n OrdTree-t\u2081\u2082 : OrdTree t\u2081\u2082\n OrdTree-t\u2081\u2082 =\n rightSubTree-OrdTree Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082 (&&\u2083-proj\u2081 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-TreeItem-t\u2081\u2082-i : LE-TreeItem t\u2081\u2082 i\n LE-TreeItem-t\u2081\u2082-i = &&-proj\u2082 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node t\u2081\u2082 i (tip i\u2082))\n OT =\n begin\n ordTree (node t\u2081\u2082 i (tip i\u2082) )\n \u2261\u27e8 ordTree-node t\u2081\u2082 i (tip i\u2082) \u27e9\n ordTree t\u2081\u2082 &&\n ordTree (tip i\u2082) &&\n \u2264-TreeItem t\u2081\u2082 i &&\n \u2264-ItemTree i (tip i\u2082)\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree t\u2081\u2082 &&\n ordTree (tip i\u2082) &&\n \u2264-TreeItem t\u2081\u2082 i &&\n \u2264-ItemTree i (tip i\u2082) \u2261\n w && x && y && z)\n OrdTree-t\u2081\u2082\n OrdTree-tip-i\u2082\n LE-TreeItem-t\u2081\u2082-i\n LE-ItemTree-i-tip-i\u2082\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n\nflatten-OrdList-aux {i = i} (nodeT {t\u2081\u2081} {i\u2081} {t\u2081\u2082} Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082) Ni\n (nodeT {t\u2082\u2081} {i\u2082} {t\u2082\u2082} Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082) OTt =\n begin\n \u2264-Lists (flatten (node t\u2081\u2081 i\u2081 t\u2081\u2082)) (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082))\n \u2261\u27e8 subst (\u03bb x \u2192 \u2264-Lists (flatten (node t\u2081\u2081 i\u2081 t\u2081\u2082))\n (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082)) \u2261\n \u2264-Lists x (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082)))\n (flatten-node t\u2081\u2081 i\u2081 t\u2081\u2082 )\n refl\n \u27e9\n \u2264-Lists (flatten t\u2081\u2081 ++ flatten t\u2081\u2082) (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082))\n \u2261\u27e8 xs\u2264zs\u2192ys\u2264zs\u2192xs++ys\u2264zs (flatten-ListN Tt\u2081\u2081)\n (flatten-ListN Tt\u2081\u2082)\n (flatten-ListN (nodeT Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082))\n lemma\u2081\n lemma\u2082\n \u27e9\n true\n \u220e\n where\n -- Auxiliary terms to get the conjuncts from OTt.\n aux\u2081 = ordTree-Bool (nodeT Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082)\n aux\u2082 = ordTree-Bool (nodeT Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082)\n aux\u2083 = \u2264-TreeItem-Bool (nodeT Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082) Ni\n aux\u2084 = \u2264-ItemTree-Bool Ni (nodeT Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082)\n aux\u2085 = trans (sym (ordTree-node (node t\u2081\u2081 i\u2081 t\u2081\u2082) i (node t\u2082\u2081 i\u2082 t\u2082\u2082))) OTt\n\n -- Auxiliary terms to get the conjuncts from the third conjunct of OTt.\n aux\u2086 = \u2264-TreeItem-Bool Tt\u2081\u2081 Ni\n aux\u2087 = \u2264-TreeItem-Bool Tt\u2081\u2082 Ni\n aux\u2088 = trans (sym (\u2264-TreeItem-node t\u2081\u2081 i\u2081 t\u2081\u2082 i))\n (&&\u2083-proj\u2083 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n -- Common terms for the lemma\u2081 and lemma\u2082.\n OrdTree-node-t\u2082\u2081-i\u2082-t\u2082\u2082 : OrdTree (node t\u2082\u2081 i\u2082 t\u2082\u2082)\n OrdTree-node-t\u2082\u2081-i\u2082-t\u2082\u2082 = &&\u2083-proj\u2082 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085\n\n LE-ItemTree-i-node-t\u2082\u2081-i\u2082-t\u2082\u2082 : LE-ItemTree i (node t\u2082\u2081 i\u2082 t\u2082\u2082)\n LE-ItemTree-i-node-t\u2082\u2081-i\u2082-t\u2082\u2082 = &&\u2083-proj\u2084 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085\n\n lemma\u2081 : LE-Lists (flatten t\u2081\u2081) (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082))\n lemma\u2081 = flatten-OrdList-aux Tt\u2081\u2081 Ni (nodeT Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082) OT -- IH.\n where\n OrdTree-t\u2081\u2081 : OrdTree t\u2081\u2081\n OrdTree-t\u2081\u2081 =\n leftSubTree-OrdTree Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082 (&&\u2083-proj\u2081 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-TreeItem-t\u2081\u2081-i : LE-TreeItem t\u2081\u2081 i\n LE-TreeItem-t\u2081\u2081-i = &&-proj\u2081 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node t\u2081\u2081 i (node t\u2082\u2081 i\u2082 t\u2082\u2082))\n OT =\n begin\n ordTree (node t\u2081\u2081 i (node t\u2082\u2081 i\u2082 t\u2082\u2082) )\n \u2261\u27e8 ordTree-node t\u2081\u2081 i (node t\u2082\u2081 i\u2082 t\u2082\u2082) \u27e9\n ordTree t\u2081\u2081 &&\n ordTree (node t\u2082\u2081 i\u2082 t\u2082\u2082) &&\n \u2264-TreeItem t\u2081\u2081 i &&\n \u2264-ItemTree i (node t\u2082\u2081 i\u2082 t\u2082\u2082)\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree t\u2081\u2081 &&\n ordTree (node t\u2082\u2081 i\u2082 t\u2082\u2082) &&\n \u2264-TreeItem t\u2081\u2081 i &&\n \u2264-ItemTree i (node t\u2082\u2081 i\u2082 t\u2082\u2082) \u2261\n w && x && y && z)\n OrdTree-t\u2081\u2081\n OrdTree-node-t\u2082\u2081-i\u2082-t\u2082\u2082\n LE-TreeItem-t\u2081\u2081-i\n LE-ItemTree-i-node-t\u2082\u2081-i\u2082-t\u2082\u2082\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n\n lemma\u2082 : LE-Lists (flatten t\u2081\u2082) (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082))\n lemma\u2082 = flatten-OrdList-aux Tt\u2081\u2082 Ni (nodeT Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082) OT -- IH.\n where\n OrdTree-t\u2081\u2082 : OrdTree t\u2081\u2082\n OrdTree-t\u2081\u2082 =\n rightSubTree-OrdTree Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082 (&&\u2083-proj\u2081 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-TreeItem-t\u2081\u2082-i : LE-TreeItem t\u2081\u2082 i\n LE-TreeItem-t\u2081\u2082-i = &&-proj\u2082 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node t\u2081\u2082 i (node t\u2082\u2081 i\u2082 t\u2082\u2082))\n OT =\n begin\n ordTree (node t\u2081\u2082 i (node t\u2082\u2081 i\u2082 t\u2082\u2082) )\n \u2261\u27e8 ordTree-node t\u2081\u2082 i (node t\u2082\u2081 i\u2082 t\u2082\u2082) \u27e9\n ordTree t\u2081\u2082 &&\n ordTree (node t\u2082\u2081 i\u2082 t\u2082\u2082) &&\n \u2264-TreeItem t\u2081\u2082 i &&\n \u2264-ItemTree i (node t\u2082\u2081 i\u2082 t\u2082\u2082)\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree t\u2081\u2082 &&\n ordTree (node t\u2082\u2081 i\u2082 t\u2082\u2082) &&\n \u2264-TreeItem t\u2081\u2082 i &&\n \u2264-ItemTree i (node t\u2082\u2081 i\u2082 t\u2082\u2082) \u2261\n w && x && y && z)\n OrdTree-t\u2081\u2082\n OrdTree-node-t\u2082\u2081-i\u2082-t\u2082\u2082\n LE-TreeItem-t\u2081\u2082-i\n LE-ItemTree-i-node-t\u2082\u2081-i\u2082-t\u2082\u2082\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n","old_contents":"------------------------------------------------------------------------------\n-- Closures properties respect to OrdList (flatten-OrdList-aux)\n------------------------------------------------------------------------------\n\n-- TODO\n{-# OPTIONS --no-termination-check #-}\n\nmodule LTC.Program.SortList.Properties.Closures.OrdList.FlattenI where\n\nopen import LTC.Base\n\n-- open import Common.Function\n\nopen import LTC.Data.Bool\nopen import LTC.Data.Bool.PropertiesI\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesI\nopen import LTC.Data.Nat.Type\nopen import LTC.Data.List\n\nopen import LTC.Program.SortList.Properties.Closures.BoolI\nopen import LTC.Program.SortList.Properties.Closures.ListN-I\nopen import LTC.Program.SortList.Properties.Closures.OrdTreeI\nopen import LTC.Program.SortList.Properties.MiscellaneousI\nopen import LTC.Program.SortList.SortList\n\nopen import LTC.Relation.Binary.EqReasoning\n\n------------------------------------------------------------------------------\n\nflatten-OrdList-aux : {t\u2081 i t\u2082 : D} \u2192 Tree t\u2081 \u2192 N i \u2192 Tree t\u2082 \u2192\n OrdTree (node t\u2081 i t\u2082) \u2192\n LE-Lists (flatten t\u2081) (flatten t\u2082)\n\nflatten-OrdList-aux {t\u2082 = t\u2082} nilT Ni Tt\u2082 OTt =\n subst (\u03bb t \u2192 LE-Lists t (flatten t\u2082))\n (sym (flatten-nilTree))\n (\u2264-Lists-[] (flatten t\u2082))\n\nflatten-OrdList-aux (tipT {i\u2081} Ni\u2081) _ nilT OTt =\n begin\n \u2264-Lists (flatten (tip i\u2081)) (flatten nilTree)\n \u2261\u27e8 subst\u2082 (\u03bb x\u2081 x\u2082 \u2192 \u2264-Lists (flatten (tip i\u2081)) (flatten nilTree) \u2261\n \u2264-Lists x\u2081 x\u2082)\n (flatten-tip i\u2081)\n flatten-nilTree\n refl\n \u27e9\n \u2264-Lists (i\u2081 \u2237 []) []\n \u2261\u27e8 \u2264-Lists-\u2237 i\u2081 [] [] \u27e9\n \u2264-ItemList i\u2081 [] && \u2264-Lists [] []\n \u2261\u27e8 subst\u2082 (\u03bb x\u2081 x\u2082 \u2192 \u2264-ItemList i\u2081 [] && \u2264-Lists [] [] \u2261 x\u2081 && x\u2082 )\n (\u2264-ItemList-[] i\u2081)\n (\u2264-Lists-[] [])\n refl\n \u27e9\n true && true\n \u2261\u27e8 &&-tt \u27e9\n true\n \u220e\n\nflatten-OrdList-aux {i = i} (tipT {i\u2081} Ni\u2081) Ni (tipT {i\u2082} Ni\u2082) OTt =\n begin\n \u2264-Lists (flatten (tip i\u2081)) (flatten (tip i\u2082))\n \u2261\u27e8 subst\u2082 (\u03bb x\u2081 x\u2082 \u2192 \u2264-Lists (flatten (tip i\u2081)) (flatten (tip i\u2082)) \u2261\n \u2264-Lists x\u2081 x\u2082)\n (flatten-tip i\u2081)\n (flatten-tip i\u2082)\n refl\n \u27e9\n \u2264-Lists (i\u2081 \u2237 []) (i\u2082 \u2237 [])\n \u2261\u27e8 \u2264-Lists-\u2237 i\u2081 [] (i\u2082 \u2237 []) \u27e9\n \u2264-ItemList i\u2081 (i\u2082 \u2237 []) && \u2264-Lists [] (i\u2082 \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 \u2264-ItemList i\u2081 (i\u2082 \u2237 []) && \u2264-Lists [] (i\u2082 \u2237 []) \u2261\n t && \u2264-Lists [] (i\u2082 \u2237 []))\n (\u2264-ItemList-\u2237 i\u2081 i\u2082 [])\n refl\n \u27e9\n (i\u2081 \u2264 i\u2082 && \u2264-ItemList i\u2081 []) && \u2264-Lists [] (i\u2082 \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 (i\u2081 \u2264 i\u2082 && \u2264-ItemList i\u2081 []) && \u2264-Lists [] (i\u2082 \u2237 []) \u2261\n (t && \u2264-ItemList i\u2081 []) && \u2264-Lists [] (i\u2082 \u2237 []))\n lemma\n refl\n \u27e9\n (true && \u2264-ItemList i\u2081 []) && \u2264-Lists [] (i\u2082 \u2237 [])\n \u2261\u27e8 subst\u2082 (\u03bb x\u2081 x\u2082 \u2192 (true && \u2264-ItemList i\u2081 []) && \u2264-Lists [] (i\u2082 \u2237 []) \u2261\n (true && x\u2081) && x\u2082)\n (\u2264-ItemList-[] i\u2081)\n (\u2264-Lists-[] (i\u2082 \u2237 []))\n refl\n \u27e9\n (true && true) && true\n \u2261\u27e8 &&-assoc tB tB tB \u27e9\n true && true && true\n \u2261\u27e8 &&-true\u2083 \u27e9\n true\n \u220e\n where\n aux\u2081 : Bool (ordTree (tip i\u2081))\n aux\u2081 = ordTree-Bool (tipT Ni\u2081)\n\n aux\u2082 : Bool (ordTree (tip i\u2082))\n aux\u2082 = ordTree-Bool (tipT Ni\u2082)\n\n aux\u2083 : Bool (\u2264-TreeItem (tip i\u2081) i)\n aux\u2083 = \u2264-TreeItem-Bool (tipT Ni\u2081) Ni\n\n aux\u2084 : Bool (\u2264-ItemTree i (tip i\u2082))\n aux\u2084 = \u2264-ItemTree-Bool Ni (tipT Ni\u2082)\n\n aux\u2085 : ordTree (tip i\u2081) &&\n ordTree (tip i\u2082) &&\n \u2264-TreeItem (tip i\u2081) i &&\n \u2264-ItemTree i (tip i\u2082) \u2261 true\n aux\u2085 = trans (sym (ordTree-node (tip i\u2081) i (tip i\u2082))) OTt\n\n lemma : LE i\u2081 i\u2082\n lemma = \u2264-trans Ni\u2081 Ni Ni\u2082 i\u2081\u2264i i\u2264i\u2082\n where\n i\u2081\u2264i : LE i\u2081 i\n i\u2081\u2264i = trans (sym (\u2264-TreeItem-tip i\u2081 i))\n (&&\u2083-proj\u2083 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n i\u2264i\u2082 : LE i i\u2082\n i\u2264i\u2082 = trans (sym (\u2264-ItemTree-tip i i\u2082))\n (&&\u2083-proj\u2084 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\nflatten-OrdList-aux {i = i} (tipT {i\u2081} Ni\u2081) Ni\n (nodeT {t\u2082\u2081} {i\u2082} {t\u2082\u2082} Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082) OTt =\n begin\n \u2264-Lists (flatten (tip i\u2081)) (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082))\n \u2261\u27e8 subst (\u03bb x \u2192 \u2264-Lists (flatten (tip i\u2081)) (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082)) \u2261\n \u2264-Lists (flatten (tip i\u2081)) x)\n (flatten-node t\u2082\u2081 i\u2082 t\u2082\u2082)\n refl\n \u27e9\n \u2264-Lists (flatten (tip i\u2081)) (flatten t\u2082\u2081 ++ flatten t\u2082\u2082)\n \u2261\u27e8 xs\u2264ys\u2192xs\u2264zs\u2192xs\u2264ys++zs (flatten-ListN (tipT Ni\u2081))\n (flatten-ListN Tt\u2082\u2081)\n (flatten-ListN Tt\u2082\u2082)\n lemma\u2081\n lemma\u2082\n \u27e9\n true\n \u220e\n where\n -- Auxiliary terms to get the conjuncts from OTt.\n aux\u2081 = ordTree-Bool (tipT Ni\u2081)\n aux\u2082 = ordTree-Bool (nodeT Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082)\n aux\u2083 = \u2264-TreeItem-Bool (tipT Ni\u2081) Ni\n aux\u2084 = \u2264-ItemTree-Bool Ni (nodeT Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082)\n aux\u2085 = trans (sym (ordTree-node (tip i\u2081) i (node t\u2082\u2081 i\u2082 t\u2082\u2082))) OTt\n\n -- Auxiliary terms to get the conjuncts from the fourth conjunct of OTt.\n aux\u2086 = \u2264-ItemTree-Bool Ni Tt\u2082\u2081\n aux\u2087 = \u2264-ItemTree-Bool Ni Tt\u2082\u2082\n aux\u2088 = trans (sym (\u2264-ItemTree-node i t\u2082\u2081 i\u2082 t\u2082\u2082))\n (&&\u2083-proj\u2084 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n -- Common terms for the lemma\u2081 and lemma\u2082.\n OrdTree-tip-i\u2081 : OrdTree (tip i\u2081)\n OrdTree-tip-i\u2081 = &&\u2083-proj\u2081 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085\n\n LE-TreeItem-tip-i\u2081-i : LE-TreeItem (tip i\u2081) i\n LE-TreeItem-tip-i\u2081-i = &&\u2083-proj\u2083 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085\n\n lemma\u2081 : LE-Lists (flatten (tip i\u2081)) (flatten t\u2082\u2081)\n lemma\u2081 = flatten-OrdList-aux (tipT Ni\u2081) Ni Tt\u2082\u2081 OT -- IH.\n where\n OrdTree-t\u2082\u2081 : OrdTree t\u2082\u2081\n OrdTree-t\u2082\u2081 = leftSubTree-OrdTree Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082\n (&&\u2083-proj\u2082 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-ItemTree-i-t\u2082\u2081 : LE-ItemTree i t\u2082\u2081\n LE-ItemTree-i-t\u2082\u2081 = &&-proj\u2081 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node (tip i\u2081) i t\u2082\u2081)\n OT =\n begin\n ordTree (node (tip i\u2081) i t\u2082\u2081)\n \u2261\u27e8 ordTree-node (tip i\u2081) i t\u2082\u2081 \u27e9\n ordTree (tip i\u2081) &&\n ordTree t\u2082\u2081 &&\n \u2264-TreeItem (tip i\u2081) i &&\n \u2264-ItemTree i t\u2082\u2081\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree (tip i\u2081) &&\n ordTree t\u2082\u2081 &&\n \u2264-TreeItem (tip i\u2081) i &&\n \u2264-ItemTree i t\u2082\u2081 \u2261\n w && x && y && z)\n OrdTree-tip-i\u2081\n OrdTree-t\u2082\u2081\n LE-TreeItem-tip-i\u2081-i\n LE-ItemTree-i-t\u2082\u2081\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n\n lemma\u2082 : LE-Lists (flatten (tip i\u2081)) (flatten t\u2082\u2082)\n lemma\u2082 = flatten-OrdList-aux (tipT Ni\u2081) Ni Tt\u2082\u2082 OT -- IH.\n where\n OrdTree-t\u2082\u2082 : OrdTree t\u2082\u2082\n OrdTree-t\u2082\u2082 = rightSubTree-OrdTree Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082\n (&&\u2083-proj\u2082 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-ItemTree-i-t\u2082\u2082 : LE-ItemTree i t\u2082\u2082\n LE-ItemTree-i-t\u2082\u2082 = &&-proj\u2082 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node (tip i\u2081) i t\u2082\u2082)\n OT =\n begin\n ordTree (node (tip i\u2081) i t\u2082\u2082)\n \u2261\u27e8 ordTree-node (tip i\u2081) i t\u2082\u2082 \u27e9\n ordTree (tip i\u2081) &&\n ordTree t\u2082\u2082 &&\n \u2264-TreeItem (tip i\u2081) i &&\n \u2264-ItemTree i t\u2082\u2082\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree (tip i\u2081) &&\n ordTree t\u2082\u2082 &&\n \u2264-TreeItem (tip i\u2081) i &&\n \u2264-ItemTree i t\u2082\u2082 \u2261\n w && x && y && z)\n OrdTree-tip-i\u2081\n OrdTree-t\u2082\u2082\n LE-TreeItem-tip-i\u2081-i\n LE-ItemTree-i-t\u2082\u2082\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n\nflatten-OrdList-aux {i = i} (nodeT {t\u2081\u2081} {i\u2081} {t\u2081\u2082} Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082) Ni nilT OTt =\n begin\n \u2264-Lists (flatten (node t\u2081\u2081 i\u2081 t\u2081\u2082)) (flatten nilTree)\n \u2261\u27e8 subst (\u03bb x \u2192 \u2264-Lists (flatten (node t\u2081\u2081 i\u2081 t\u2081\u2082)) (flatten nilTree) \u2261\n \u2264-Lists x (flatten nilTree))\n (flatten-node t\u2081\u2081 i\u2081 t\u2081\u2082 )\n refl\n \u27e9\n \u2264-Lists (flatten t\u2081\u2081 ++ flatten t\u2081\u2082) (flatten nilTree)\n \u2261\u27e8 xs\u2264zs\u2192ys\u2264zs\u2192xs++ys\u2264zs (flatten-ListN Tt\u2081\u2081)\n (flatten-ListN Tt\u2081\u2082)\n (flatten-ListN nilT)\n lemma\u2081\n lemma\u2082\n \u27e9\n true\n \u220e\n where\n -- Auxiliary terms to get the conjuncts from OTt.\n aux\u2081 = ordTree-Bool (nodeT Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082)\n aux\u2082 = ordTree-Bool nilT\n aux\u2083 = \u2264-TreeItem-Bool (nodeT Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082) Ni\n aux\u2084 = \u2264-ItemTree-Bool Ni nilT\n aux\u2085 = trans (sym (ordTree-node (node t\u2081\u2081 i\u2081 t\u2081\u2082) i nilTree )) OTt\n\n -- Auxiliary terms to get the conjuncts from the third conjunct of OTt.\n aux\u2086 = \u2264-TreeItem-Bool Tt\u2081\u2081 Ni\n aux\u2087 = \u2264-TreeItem-Bool Tt\u2081\u2082 Ni\n aux\u2088 = trans (sym (\u2264-TreeItem-node t\u2081\u2081 i\u2081 t\u2081\u2082 i))\n (&&\u2083-proj\u2083 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n -- Common terms for the lemma\u2081 and lemma\u2082.\n LE-ItemTree-i-niltree : LE-ItemTree i nilTree\n LE-ItemTree-i-niltree = &&\u2083-proj\u2084 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085\n\n lemma\u2081 : LE-Lists (flatten t\u2081\u2081) (flatten nilTree)\n lemma\u2081 = flatten-OrdList-aux Tt\u2081\u2081 Ni nilT OT -- IH.\n where\n OrdTree-t\u2081\u2081 : OrdTree t\u2081\u2081\n OrdTree-t\u2081\u2081 =\n leftSubTree-OrdTree Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082\n (&&\u2083-proj\u2081 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-TreeItem-t\u2081\u2081-i : LE-TreeItem t\u2081\u2081 i\n LE-TreeItem-t\u2081\u2081-i = &&-proj\u2081 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node t\u2081\u2081 i nilTree)\n OT =\n begin\n ordTree (node t\u2081\u2081 i nilTree )\n \u2261\u27e8 ordTree-node t\u2081\u2081 i nilTree \u27e9\n ordTree t\u2081\u2081 &&\n ordTree nilTree &&\n \u2264-TreeItem t\u2081\u2081 i &&\n \u2264-ItemTree i nilTree\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree t\u2081\u2081 &&\n ordTree nilTree &&\n \u2264-TreeItem t\u2081\u2081 i &&\n \u2264-ItemTree i nilTree \u2261\n w && x && y && z)\n OrdTree-t\u2081\u2081\n ordTree-nilTree\n LE-TreeItem-t\u2081\u2081-i\n LE-ItemTree-i-niltree\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n\n lemma\u2082 : LE-Lists (flatten t\u2081\u2082) (flatten nilTree)\n lemma\u2082 = flatten-OrdList-aux Tt\u2081\u2082 Ni nilT OT -- IH.\n where\n OrdTree-t\u2081\u2082 : OrdTree t\u2081\u2082\n OrdTree-t\u2081\u2082 =\n rightSubTree-OrdTree Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082 (&&\u2083-proj\u2081 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-TreeItem-t\u2081\u2082-i : LE-TreeItem t\u2081\u2082 i\n LE-TreeItem-t\u2081\u2082-i = &&-proj\u2082 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node t\u2081\u2082 i nilTree)\n OT =\n begin\n ordTree (node t\u2081\u2082 i nilTree )\n \u2261\u27e8 ordTree-node t\u2081\u2082 i nilTree \u27e9\n ordTree t\u2081\u2082 &&\n ordTree nilTree &&\n \u2264-TreeItem t\u2081\u2082 i &&\n \u2264-ItemTree i nilTree\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree t\u2081\u2082 &&\n ordTree nilTree &&\n \u2264-TreeItem t\u2081\u2082 i &&\n \u2264-ItemTree i nilTree \u2261\n w && x && y && z)\n OrdTree-t\u2081\u2082\n ordTree-nilTree\n LE-TreeItem-t\u2081\u2082-i\n LE-ItemTree-i-niltree\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n\nflatten-OrdList-aux {i = i} (nodeT {t\u2081\u2081} {i\u2081} {t\u2081\u2082} Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082) Ni\n (tipT {i\u2082} Ni\u2082) OTt =\n begin\n \u2264-Lists (flatten (node t\u2081\u2081 i\u2081 t\u2081\u2082)) (flatten (tip i\u2082))\n \u2261\u27e8 subst (\u03bb x \u2192 \u2264-Lists (flatten (node t\u2081\u2081 i\u2081 t\u2081\u2082)) (flatten (tip i\u2082)) \u2261\n \u2264-Lists x (flatten (tip i\u2082)))\n (flatten-node t\u2081\u2081 i\u2081 t\u2081\u2082 )\n refl\n \u27e9\n \u2264-Lists (flatten t\u2081\u2081 ++ flatten t\u2081\u2082) (flatten (tip i\u2082))\n \u2261\u27e8 xs\u2264zs\u2192ys\u2264zs\u2192xs++ys\u2264zs (flatten-ListN Tt\u2081\u2081)\n (flatten-ListN Tt\u2081\u2082)\n (flatten-ListN (tipT Ni\u2082))\n lemma\u2081\n lemma\u2082\n \u27e9\n true\n \u220e\n where\n -- Auxiliary terms to get the conjuncts from OTt.\n aux\u2081 = ordTree-Bool (nodeT Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082)\n aux\u2082 = ordTree-Bool (tipT Ni\u2082)\n aux\u2083 = \u2264-TreeItem-Bool (nodeT Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082) Ni\n aux\u2084 = \u2264-ItemTree-Bool Ni (tipT Ni\u2082)\n aux\u2085 = trans (sym (ordTree-node (node t\u2081\u2081 i\u2081 t\u2081\u2082) i (tip i\u2082))) OTt\n\n -- Auxiliary terms to get the conjuncts from the third conjunct of OTt.\n aux\u2086 = \u2264-TreeItem-Bool Tt\u2081\u2081 Ni\n aux\u2087 = \u2264-TreeItem-Bool Tt\u2081\u2082 Ni\n aux\u2088 = trans (sym (\u2264-TreeItem-node t\u2081\u2081 i\u2081 t\u2081\u2082 i))\n (&&\u2083-proj\u2083 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n -- Common terms for the lemma\u2081 and lemma\u2082.\n OrdTree-tip-i\u2082 : OrdTree (tip i\u2082)\n OrdTree-tip-i\u2082 = &&\u2083-proj\u2082 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085\n\n LE-ItemTree-i-tip-i\u2082 : LE-ItemTree i (tip i\u2082)\n LE-ItemTree-i-tip-i\u2082 = &&\u2083-proj\u2084 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085\n\n lemma\u2081 : LE-Lists (flatten t\u2081\u2081) (flatten (tip i\u2082))\n lemma\u2081 = flatten-OrdList-aux Tt\u2081\u2081 Ni (tipT Ni\u2082) OT -- IH.\n where\n OrdTree-t\u2081\u2081 : OrdTree t\u2081\u2081\n OrdTree-t\u2081\u2081 =\n leftSubTree-OrdTree Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082 (&&\u2083-proj\u2081 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-TreeItem-t\u2081\u2081-i : LE-TreeItem t\u2081\u2081 i\n LE-TreeItem-t\u2081\u2081-i = &&-proj\u2081 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node t\u2081\u2081 i (tip i\u2082))\n OT =\n begin\n ordTree (node t\u2081\u2081 i (tip i\u2082) )\n \u2261\u27e8 ordTree-node t\u2081\u2081 i (tip i\u2082) \u27e9\n ordTree t\u2081\u2081 &&\n ordTree (tip i\u2082) &&\n \u2264-TreeItem t\u2081\u2081 i &&\n \u2264-ItemTree i (tip i\u2082)\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree t\u2081\u2081 &&\n ordTree (tip i\u2082) &&\n \u2264-TreeItem t\u2081\u2081 i &&\n \u2264-ItemTree i (tip i\u2082) \u2261\n w && x && y && z)\n OrdTree-t\u2081\u2081\n OrdTree-tip-i\u2082\n LE-TreeItem-t\u2081\u2081-i\n LE-ItemTree-i-tip-i\u2082\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n\n lemma\u2082 : LE-Lists (flatten t\u2081\u2082) (flatten (tip i\u2082))\n lemma\u2082 = flatten-OrdList-aux Tt\u2081\u2082 Ni (tipT Ni\u2082) OT -- IH.\n where\n OrdTree-t\u2081\u2082 : OrdTree t\u2081\u2082\n OrdTree-t\u2081\u2082 =\n rightSubTree-OrdTree Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082 (&&\u2083-proj\u2081 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-TreeItem-t\u2081\u2082-i : LE-TreeItem t\u2081\u2082 i\n LE-TreeItem-t\u2081\u2082-i = &&-proj\u2082 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node t\u2081\u2082 i (tip i\u2082))\n OT =\n begin\n ordTree (node t\u2081\u2082 i (tip i\u2082) )\n \u2261\u27e8 ordTree-node t\u2081\u2082 i (tip i\u2082) \u27e9\n ordTree t\u2081\u2082 &&\n ordTree (tip i\u2082) &&\n \u2264-TreeItem t\u2081\u2082 i &&\n \u2264-ItemTree i (tip i\u2082)\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree t\u2081\u2082 &&\n ordTree (tip i\u2082) &&\n \u2264-TreeItem t\u2081\u2082 i &&\n \u2264-ItemTree i (tip i\u2082) \u2261\n w && x && y && z)\n OrdTree-t\u2081\u2082\n OrdTree-tip-i\u2082\n LE-TreeItem-t\u2081\u2082-i\n LE-ItemTree-i-tip-i\u2082\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n\nflatten-OrdList-aux {i = i} (nodeT {t\u2081\u2081} {i\u2081} {t\u2081\u2082} Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082) Ni\n (nodeT {t\u2082\u2081} {i\u2082} {t\u2082\u2082} Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082) OTt =\n begin\n \u2264-Lists (flatten (node t\u2081\u2081 i\u2081 t\u2081\u2082)) (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082))\n \u2261\u27e8 subst (\u03bb x \u2192 \u2264-Lists (flatten (node t\u2081\u2081 i\u2081 t\u2081\u2082))\n (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082)) \u2261\n \u2264-Lists x (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082)))\n (flatten-node t\u2081\u2081 i\u2081 t\u2081\u2082 )\n refl\n \u27e9\n \u2264-Lists (flatten t\u2081\u2081 ++ flatten t\u2081\u2082) (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082))\n \u2261\u27e8 xs\u2264zs\u2192ys\u2264zs\u2192xs++ys\u2264zs (flatten-ListN Tt\u2081\u2081)\n (flatten-ListN Tt\u2081\u2082)\n (flatten-ListN (nodeT Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082))\n lemma\u2081\n lemma\u2082\n \u27e9\n true\n \u220e\n where\n -- Auxiliary terms to get the conjuncts from OTt.\n aux\u2081 = ordTree-Bool (nodeT Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082)\n aux\u2082 = ordTree-Bool (nodeT Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082)\n aux\u2083 = \u2264-TreeItem-Bool (nodeT Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082) Ni\n aux\u2084 = \u2264-ItemTree-Bool Ni (nodeT Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082)\n aux\u2085 = trans (sym (ordTree-node (node t\u2081\u2081 i\u2081 t\u2081\u2082) i (node t\u2082\u2081 i\u2082 t\u2082\u2082))) OTt\n\n -- Auxiliary terms to get the conjuncts from the third conjunct of OTt.\n aux\u2086 = \u2264-TreeItem-Bool Tt\u2081\u2081 Ni\n aux\u2087 = \u2264-TreeItem-Bool Tt\u2081\u2082 Ni\n aux\u2088 = trans (sym (\u2264-TreeItem-node t\u2081\u2081 i\u2081 t\u2081\u2082 i))\n (&&\u2083-proj\u2083 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n -- Common terms for the lemma\u2081 and lemma\u2082.\n OrdTree-node-t\u2082\u2081-i\u2082-t\u2082\u2082 : OrdTree (node t\u2082\u2081 i\u2082 t\u2082\u2082)\n OrdTree-node-t\u2082\u2081-i\u2082-t\u2082\u2082 = &&\u2083-proj\u2082 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085\n\n LE-ItemTree-i-node-t\u2082\u2081-i\u2082-t\u2082\u2082 : LE-ItemTree i (node t\u2082\u2081 i\u2082 t\u2082\u2082)\n LE-ItemTree-i-node-t\u2082\u2081-i\u2082-t\u2082\u2082 = &&\u2083-proj\u2084 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085\n\n lemma\u2081 : LE-Lists (flatten t\u2081\u2081) (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082))\n lemma\u2081 = flatten-OrdList-aux Tt\u2081\u2081 Ni (nodeT Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082) OT -- IH.\n where\n OrdTree-t\u2081\u2081 : OrdTree t\u2081\u2081\n OrdTree-t\u2081\u2081 =\n leftSubTree-OrdTree Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082 (&&\u2083-proj\u2081 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-TreeItem-t\u2081\u2081-i : LE-TreeItem t\u2081\u2081 i\n LE-TreeItem-t\u2081\u2081-i = &&-proj\u2081 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node t\u2081\u2081 i (node t\u2082\u2081 i\u2082 t\u2082\u2082))\n OT =\n begin\n ordTree (node t\u2081\u2081 i (node t\u2082\u2081 i\u2082 t\u2082\u2082) )\n \u2261\u27e8 ordTree-node t\u2081\u2081 i (node t\u2082\u2081 i\u2082 t\u2082\u2082) \u27e9\n ordTree t\u2081\u2081 &&\n ordTree (node t\u2082\u2081 i\u2082 t\u2082\u2082) &&\n \u2264-TreeItem t\u2081\u2081 i &&\n \u2264-ItemTree i (node t\u2082\u2081 i\u2082 t\u2082\u2082)\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree t\u2081\u2081 &&\n ordTree (node t\u2082\u2081 i\u2082 t\u2082\u2082) &&\n \u2264-TreeItem t\u2081\u2081 i &&\n \u2264-ItemTree i (node t\u2082\u2081 i\u2082 t\u2082\u2082) \u2261\n w && x && y && z)\n OrdTree-t\u2081\u2081\n OrdTree-node-t\u2082\u2081-i\u2082-t\u2082\u2082\n LE-TreeItem-t\u2081\u2081-i\n LE-ItemTree-i-node-t\u2082\u2081-i\u2082-t\u2082\u2082\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n\n lemma\u2082 : LE-Lists (flatten t\u2081\u2082) (flatten (node t\u2082\u2081 i\u2082 t\u2082\u2082))\n lemma\u2082 = flatten-OrdList-aux Tt\u2081\u2082 Ni (nodeT Tt\u2082\u2081 Ni\u2082 Tt\u2082\u2082) OT -- IH.\n where\n OrdTree-t\u2081\u2082 : OrdTree t\u2081\u2082\n OrdTree-t\u2081\u2082 =\n rightSubTree-OrdTree Tt\u2081\u2081 Ni\u2081 Tt\u2081\u2082 (&&\u2083-proj\u2081 aux\u2081 aux\u2082 aux\u2083 aux\u2084 aux\u2085)\n\n LE-TreeItem-t\u2081\u2082-i : LE-TreeItem t\u2081\u2082 i\n LE-TreeItem-t\u2081\u2082-i = &&-proj\u2082 aux\u2086 aux\u2087 aux\u2088\n\n OT : OrdTree (node t\u2081\u2082 i (node t\u2082\u2081 i\u2082 t\u2082\u2082))\n OT =\n begin\n ordTree (node t\u2081\u2082 i (node t\u2082\u2081 i\u2082 t\u2082\u2082) )\n \u2261\u27e8 ordTree-node t\u2081\u2082 i (node t\u2082\u2081 i\u2082 t\u2082\u2082) \u27e9\n ordTree t\u2081\u2082 &&\n ordTree (node t\u2082\u2081 i\u2082 t\u2082\u2082) &&\n \u2264-TreeItem t\u2081\u2082 i &&\n \u2264-ItemTree i (node t\u2082\u2081 i\u2082 t\u2082\u2082)\n \u2261\u27e8 subst\u2084 (\u03bb w x y z \u2192 ordTree t\u2081\u2082 &&\n ordTree (node t\u2082\u2081 i\u2082 t\u2082\u2082) &&\n \u2264-TreeItem t\u2081\u2082 i &&\n \u2264-ItemTree i (node t\u2082\u2081 i\u2082 t\u2082\u2082) \u2261\n w && x && y && z)\n OrdTree-t\u2081\u2082\n OrdTree-node-t\u2082\u2081-i\u2082-t\u2082\u2082\n LE-TreeItem-t\u2081\u2082-i\n LE-ItemTree-i-node-t\u2082\u2081-i\u2082-t\u2082\u2082\n refl\n \u27e9\n true && true && true && true\n \u2261\u27e8 &&-true\u2084 \u27e9\n true\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3ae029b724ce3c3f266068195aa755f9d65a415e","subject":"Boring.","message":"Boring.\n\nIgnore-this: c17974f2bac758a84b125f426d4798a0\n\ndarcs-hash:20100410164802-3bd4e-10acc80d0acab3f763cace7626e83817333a8176.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Test\/Fail\/UsingPredicatesWithConjectures.agda","new_file":"Test\/Fail\/UsingPredicatesWithConjectures.agda","new_contents":"","old_contents":"module Test.Fail.UsingPredicatesWithConjectures where\n\ninfixl 6 _+_\ninfix 4 _\u2261_\n\npostulate\n D : Set\n zero : D\n succ : D \u2192 D\n\n-- The identity type.\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n-- The LTC natural numbers type.\ndata N : D \u2192 Set where\n zN : N zero\n sN : {n : D} \u2192 N n \u2192 N (succ n)\n\n-- Induction principle for N (elimination rule).\nindN : (P : D \u2192 Set) \u2192\n P zero \u2192\n ({n : D} \u2192 N n \u2192 P n \u2192 P (succ n)) \u2192\n {n : D} \u2192 N n \u2192 P n\nindN P p0 h zN = p0\nindN P p0 h (sN Nn) = h Nn (indN P p0 h Nn)\n\npostulate\n _+_ : D \u2192 D \u2192 D\n add-x0 : (n : D) \u2192 n + zero \u2261 n\n add-xS : (m n : D) \u2192 m + succ n \u2261 succ (m + n)\n\n{-# ATP axiom add-x0 #-}\n{-# ATP axiom add-xS #-}\n\nP : D \u2192 Set\nP i = zero + i \u2261 i\n\npostulate\n P0\u2081 : zero + zero \u2261 zero\n{-# ATP prove P0\u2081 #-}\n\npostulate\n iStep\u2081 : {i : D} \u2192 N i \u2192 zero + i \u2261 i \u2192 zero + (succ i) \u2261 succ i\n{-# ATP prove iStep\u2081 #-}\n\naddLeftIdentity\u2081 : {n : D} \u2192 N n \u2192 zero + n \u2261 n\naddLeftIdentity\u2081 = indN (\u03bb i \u2192 P i) P0\u2081 iStep\u2081\n\n-- Equinox cannot prove the postulates P0\u2082 and iStep\u2082 because he does\n-- not the definition of the predicate P. It seems we should use the\n-- TPTP definitions.\npostulate\n P0\u2082 : P zero\n-- {-# ATP prove P0\u2082 #-}\n\npostulate\n iStep\u2082 : {i : D} \u2192 N i \u2192 P i \u2192 P (succ i)\n-- {-# ATP prove iStep\u2082 #-}\n\naddLeftIdentity\u2082 : {n : D} \u2192 N n \u2192 zero + n \u2261 n\naddLeftIdentity\u2082 = indN (\u03bb i \u2192 P i) P0\u2082 iStep\u2082\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"702fcd37b6983eaf128ea29fca185751c2444c69","subject":"Fix in Relation.Binary.Permutation","message":"Fix in Relation.Binary.Permutation\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Relation\/Binary\/Permutation.agda","new_file":"lib\/Relation\/Binary\/Permutation.agda","new_contents":"--TODO {-# OPTIONS --without-K #-}\nmodule Relation.Binary.Permutation where\n\nopen import Level\nopen import Data.Product.NP\nopen import Data.Zero\nopen import Data.One\nopen import Data.Sum\nopen import Data.List\nopen import Relation.Nullary\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_;_\u2262_)\n\nprivate\n _\u21d4_ : \u2200 {a b \u2113\u2081 \u2113\u2082} {A : Set a} {B : Set b} (R\u2081 : REL A B \u2113\u2081) (R\u2082 : REL A B \u2113\u2082) \u2192 Set _\n R\u2081 \u21d4 R\u2082 = R\u2081 \u21d2 R\u2082 \u00d7 R\u2082 \u21d2 R\u2081\n\n infix 2 _\u21d4_\n\n\u27e8_,_\u27e9\u2208_ : \u2200 {\u2113 a b} {A : Set a} {B : Set b} (x : A) (y : B) \u2192 REL A B \u2113 \u2192 Set \u2113\n\u27e8_,_\u27e9\u2208_ x y R = R x y\n\ndata _[_\u2194_] {a} {A : Set a} (R : Rel A a) (x y : A) : Rel A a where\n\n here\u2081 : \u2200 {j}\n (yRj : \u27e8 y , j \u27e9\u2208 R )\n \u2192 ----------------------\n \u27e8 x , j \u27e9\u2208 R [ x \u2194 y ]\n\n here\u2082 : \u2200 {j}\n (xRj : \u27e8 x , j \u27e9\u2208 R )\n \u2192 ----------------------\n \u27e8 y , j \u27e9\u2208 R [ x \u2194 y ]\n\n there : \u2200 {i j}\n (x\u2262i : x \u2262 i )\n (y\u2262i : y \u2262 i )\n (iRj : \u27e8 i , j \u27e9\u2208 R )\n \u2192 -----------------------\n \u27e8 i , j \u27e9\u2208 R [ x \u2194 y ]\n\nmodule PermComm {a} {A : Set a} {R : Rel A a} where\n \u27f9 : \u2200 {x y} \u2192 R [ x \u2194 y ] \u21d2 R [ y \u2194 x ]\n \u27f9 (here\u2081 yRj) = here\u2082 yRj\n \u27f9 (here\u2082 xRj) = here\u2081 xRj\n \u27f9 (there x\u2262i x\u2262j iRj) = there x\u2262j x\u2262i iRj\n\n lem : \u2200 {x y} \u2192 R [ x \u2194 y ] \u21d4 R [ y \u2194 x ]\n lem = (\u03bb {_} \u2192 \u27f9) , (\u03bb {_} \u2192 \u27f9)\n\nmodule PermIdem {a} {A : Set a} (_\u225f_ : Decidable {A = A} _\u2261_) {R : Rel A a} where\n \u21d0 : \u2200 {x y} \u2192 R [ x \u2194 y ] [ x \u2194 y ] \u21d2 R\n \u21d0 (here\u2081 (here\u2081 yRj)) = yRj\n \u21d0 (here\u2081 (here\u2082 xRj)) = xRj\n \u21d0 (here\u2081 (there _ y\u2262y _)) = \ud835\udfd8-elim (y\u2262y \u2261.refl)\n \u21d0 (here\u2082 (here\u2081 yRj)) = yRj\n \u21d0 (here\u2082 (here\u2082 xRj)) = xRj\n \u21d0 (here\u2082 (there x\u2262x _ _)) = \ud835\udfd8-elim (x\u2262x \u2261.refl)\n \u21d0 (there x\u2262x _ (here\u2081 _)) = \ud835\udfd8-elim (x\u2262x \u2261.refl)\n \u21d0 (there _ y\u2262y (here\u2082 _)) = \ud835\udfd8-elim (y\u2262y \u2261.refl)\n \u21d0 (there _ _ (there _ _ iRj)) = iRj\n\n \u27f9 : \u2200 {x y} \u2192 R \u21d2 R [ x \u2194 y ] [ x \u2194 y ]\n \u27f9 {x} {y} {i} {j} R\n with x \u225f i | y \u225f i\n ... | yes x\u2261i | _ rewrite x\u2261i = here\u2081 (here\u2082 R)\n ... | _ | yes y\u2261i rewrite y\u2261i = here\u2082 (here\u2081 R)\n ... | no x\u2262i | no y\u2262i = there x\u2262i y\u2262i (there x\u2262i y\u2262i R)\n\n lem : \u2200 {x y} \u2192 R \u21d4 R [ x \u2194 y ] [ x \u2194 y ]\n lem = (\u03bb {_} \u2192 \u27f9) , \u03bb {_} \u2192 \u21d0\n\nPermutation : \u2200 {a} \u2192 Set a \u2192 Set a\nPermutation A = List (A \u00d7 A)\n\npermRel : \u2200 {a} {A : Set a} \u2192 (\u03c0 : Permutation A) \u2192 Rel A a \u2192 Rel A a\npermRel \u03c0 R = foldr (\u03bb p r \u2192 r [ fst p \u2194 snd p ]) R \u03c0\n\ntoRel : \u2200 {a} {A : Set a} \u2192 (\u03c0 : Permutation A) \u2192 Rel A a\ntoRel \u03c0 = permRel \u03c0 (\u03bb _ _ \u2192 Lift \ud835\udfd9)\n\n{-\n _\u27e8$\u27e9\u2081_ : \u2200 {a} {A : Set a} \u2192 Permutation A \u2192 A \u2192 Maybe A\n [] \u27e8$\u27e9\u2081 y = nothing\n (x \u2237 xs) \u27e8$\u27e9\u2081 y = if \u230a x \u225fA y \u230b then ? else\n-}\n","old_contents":"--TODO {-# OPTIONS --without-K #-}\nmodule Relation.Binary.Permutation where\n\nopen import Level\nopen import Data.Product.NP\nopen import Data.Zero\nopen import Data.One\nopen import Data.Sum\nopen import Data.List\nopen import Relation.Nullary\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_;_\u2262_)\n\nprivate\n _\u21d4_ : \u2200 {a b \u2113\u2081 \u2113\u2082} {A : Set a} {B : Set b} (R\u2081 : REL A B \u2113\u2081) (R\u2082 : REL A B \u2113\u2082) \u2192 Set _\n R\u2081 \u21d4 R\u2082 = R\u2081 \u21d2 R\u2082 \u00d7 R\u2082 \u21d2 R\u2081\n\n infix 2 _\u21d4_\n\n\u27e8_,_\u27e9\u2208_ : \u2200 {\u2113 a b} {A : Set a} {B : Set b} (x : A) (y : B) \u2192 REL A B \u2113 \u2192 Set \u2113\n\u27e8_,_\u27e9\u2208_ x y R = R x y\n\ndata _[_\u2194_] {a} {A : Set a} (R : Rel A a) (x y : A) : Rel A a where\n\n here\u2081 : \u2200 {j}\n (yRj : \u27e8 y , j \u27e9\u2208 R )\n \u2192 ----------------------\n \u27e8 x , j \u27e9\u2208 R [ x \u2194 y ]\n\n here\u2082 : \u2200 {j}\n (xRj : \u27e8 x , j \u27e9\u2208 R )\n \u2192 ----------------------\n \u27e8 y , j \u27e9\u2208 R [ x \u2194 y ]\n\n there : \u2200 {i j}\n (x\u2262i : x \u2262 i )\n (y\u2262i : y \u2262 i )\n (iRj : \u27e8 i , j \u27e9\u2208 R )\n \u2192 -----------------------\n \u27e8 i , j \u27e9\u2208 R [ x \u2194 y ]\n\nmodule PermComm {a} {A : Set a} {R : Rel A a} where\n \u27f9 : \u2200 {x y} \u2192 R [ x \u2194 y ] \u21d2 R [ y \u2194 x ]\n \u27f9 (here\u2081 yRj) = here\u2082 yRj\n \u27f9 (here\u2082 xRj) = here\u2081 xRj\n \u27f9 (there x\u2262i x\u2262j iRj) = there x\u2262j x\u2262i iRj\n\n lem : \u2200 {x y} \u2192 R [ x \u2194 y ] \u21d4 R [ y \u2194 x ]\n lem = (\u03bb {_} \u2192 \u27f9) , (\u03bb {_} \u2192 \u27f9)\n\nmodule PermIdem {a} {A : Set a} (_\u225f_ : Decidable {A = A} _\u2261_) {x y : A} {R : Rel A a} where\n \u21d0 : \u2200 {x y} \u2192 R [ x \u2194 y ] [ x \u2194 y ] \u21d2 R\n \u21d0 (here\u2081 (here\u2081 yRj)) = yRj\n \u21d0 (here\u2081 (here\u2082 xRj)) = xRj\n \u21d0 (here\u2081 (there _ y\u2262y _)) = \ud835\udfd8-elim (y\u2262y \u2261.refl)\n \u21d0 (here\u2082 (here\u2081 yRj)) = yRj\n \u21d0 (here\u2082 (here\u2082 xRj)) = xRj\n \u21d0 (here\u2082 (there x\u2262x _ _)) = \ud835\udfd8-elim (x\u2262x \u2261.refl)\n \u21d0 (there x\u2262x _ (here\u2081 _)) = \ud835\udfd8-elim (x\u2262x \u2261.refl)\n \u21d0 (there _ y\u2262y (here\u2082 _)) = \ud835\udfd8-elim (y\u2262y \u2261.refl)\n \u21d0 (there _ _ (there _ _ iRj)) = iRj\n\n \u27f9 : R \u21d2 R [ x \u2194 y ] [ x \u2194 y ]\n \u27f9 {i} {j} R\n with x \u225f i | y \u225f i\n ... | yes x\u2261i | _ rewrite x\u2261i = here\u2081 (here\u2082 R)\n ... | _ | yes y\u2261i rewrite y\u2261i = here\u2082 (here\u2081 R)\n ... | no x\u2262i | no y\u2262i = there x\u2262i y\u2262i (there x\u2262i y\u2262i R)\n\n lem : R \u21d4 R [ x \u2194 y ] [ x \u2194 y ]\n lem = (\u03bb {_} \u2192 \u27f9) , \u03bb {_} \u2192 \u21d0\n\nPermutation : \u2200 {a} \u2192 Set a \u2192 Set a\nPermutation A = List (A \u00d7 A)\n\npermRel : \u2200 {a} {A : Set a} \u2192 (\u03c0 : Permutation A) \u2192 Rel A a \u2192 Rel A a\npermRel \u03c0 R = foldr (\u03bb p r \u2192 r [ fst p \u2194 snd p ]) R \u03c0\n\ntoRel : \u2200 {a} {A : Set a} \u2192 (\u03c0 : Permutation A) \u2192 Rel A a\ntoRel \u03c0 = permRel \u03c0 (\u03bb _ _ \u2192 Lift \ud835\udfd9)\n\n{-\n _\u27e8$\u27e9\u2081_ : \u2200 {a} {A : Set a} \u2192 Permutation A \u2192 A \u2192 Maybe A\n [] \u27e8$\u27e9\u2081 y = nothing\n (x \u2237 xs) \u27e8$\u27e9\u2081 y = if \u230a x \u225fA y \u230b then ? else\n-}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"42981be31978bf60f777fb69f84e05a34f8bee99","subject":"More protocols","message":"More protocols\n","repos":"crypto-agda\/crypto-agda","old_file":"Control\/Protocol\/Choreography.agda","new_file":"Control\/Protocol\/Choreography.agda","new_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\nopen import Data.One hiding (_\u225f_)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP as \u2261\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\n {-\ndata S<_> {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n\npattern com' q M P = com (mk q M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\nopen ProtoRel _\u2261_ public renaming (_\u2248\u1d3e_ to _\u2261\u1d3e_)\n\n{-\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n -}\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n Trace : Proto \u2192 Proto\n Trace end = end\n Trace (com c) = com (Trace\u1d9c c)\n\n Trace\u1d9c : Com \u2192 Com\n Trace\u1d9c c = \u03a3\u1d9c (Com.M c) \u03bb m \u2192 Trace (Com.P c m)\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c c = mk (dual\u1d35\u1d3c (Com.io c)) (Com.M c) \u03bb m \u2192 dual (Com.P c m)\n\ndata IsTrace : Proto \u2192 \u2605\u2081 where\n end : IsTrace end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsTrace (P m)) \u2192 IsTrace (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\nmutual\n data Dual\u1d9c : Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a0\u1d9c M P) (\u03a3\u1d9c M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a3\u1d9c M P) (\u03a0\u1d9c M Q)\n\n data Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n {-\n \u03a0\u2610\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P [ x ]) (Q x)) \u2192 Dual (\u03a0\u1d3e (\u2610 M) P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0\u2610 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q [ x ])) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e (\u2610 M) Q)\n -}\n\n {-\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Trace : Choreo I \u2192 Proto\n \u2102Trace \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Trace-IsTrace : \u2200 \u2102 \u2192 IsTrace (\u2102Trace \u2102)\n \u2102Trace-IsTrace (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n-}\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com refl M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com refl M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\ndual\u1d35\u1d3c-involutive : \u2200 io \u2192 dual\u1d35\u1d3c (dual\u1d35\u1d3c io) \u2261 io\ndual\u1d35\u1d3c-involutive In = refl\ndual\u1d35\u1d3c-involutive Out = refl\n\ndual-involutive : \u2200 P \u2192 dual (dual P) \u2261\u1d3e P\ndual-involutive end = end\ndual-involutive (com' q M P) = com (dual\u1d35\u1d3c-involutive q) M (\u03bb m \u2192 dual-involutive (P m))\n\nTrace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261\u1d3e Trace P\nTrace-idempotent end = end\nTrace-idempotent (com' _ M P) = com refl M \u03bb m \u2192 Trace-idempotent (P m)\n\nTrace-dual-oblivious : \u2200 P \u2192 Trace (dual P) \u2261\u1d3e Trace P\nTrace-dual-oblivious end = end\nTrace-dual-oblivious (com' _ M P) = com refl M \u03bb m \u2192 Trace-dual-oblivious (P m)\n\nSink : Proto \u2192 Proto\nSink = dual \u2218 Trace\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n{-\n++Tele : \u2200 (P : Proto){Q : Tele P \u2192 Proto} (xs : Tele P) \u2192 Tele (Q xs) \u2192 Tele (P >>= Q)\n++Tele end _ ys = ys\n++Tele (com' q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com q M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com q M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n{-\nDual-sym (\u03a0\u2610\u00b7\u03a3 f) = {!\u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))!}\nDual-sym (\u03a3\u00b7\u03a0\u2610 f) = {!\u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))!}\n-}\n-}\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele (dual P) \u2261 Tele P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n El->>= : (P : Proto){Q : Tele P \u2192 Proto}{X : Tele (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Tele P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n-}\n\n>>=-com : (P : Proto){Q : Tele P \u2192 Proto}{R : Tele P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Tele P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Tele P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : \u2605_ a} (l r : A) \u2192 LR \u2192 A \n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP >>\u1d9c S = record P { P = \u03bb m \u2192 S (Com.P P m) }\n\n{-\nmodule V2 where\n mutual\n Sim\u1d3e : Proto \u2192 Proto \u2192 Proto\n Sim\u1d3e end Q = Q\n Sim\u1d3e (\u03a0\u1d3e M P) Q = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e (P m) Q\n Sim\u1d3e P end = P\n Sim\u1d3e P (\u03a0\u1d3e M Q) = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e P (Q m)\n Sim\u1d3e (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 Sim\u1d3e (P m) (\u03a3\u1d3e M' Q)) ,inr: (\u03bb m' \u2192 Sim\u1d3e (\u03a3\u1d3e M P) (Q m')) ]\n\n Sim\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\n Sim\u1d3e-assoc end Q R s = s\n Sim\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = Sim\u1d3e-assoc (P m) _ _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , Sim\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n Sim\u1d3e-sendR : \u2200 {M} P Q \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7 \u2192 (m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7\n Sim\u1d3e-sendR P Q s m = {!!}\n\n Sim\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7) \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7\n Sim\u1d3e-recvR P Q s = {!!}\n\n Sim\u1d3e-! : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 Sim\u1d3e Q P \u27e7\n Sim\u1d3e-! end Q p = {!!}\n Sim\u1d3e-! (\u03a0\u1d3e M P) end p x = Sim\u1d3e-! (P x) end (p x)\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-recvR (Q m') P \u03bb m \u2192 Sim\u1d3e-sendR {!!} {!!} (p m) m'\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 Sim\u1d3e-! (P m) (com (mk Out M' Q)) (p m) \n Sim\u1d3e-! (\u03a3\u1d3e M P) end p = p \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-! (com (mk Out M P)) (Q m') (p m') \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (Sim\u1d3e-! (P m) (com (mk Out M' Q)) p) \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (Sim\u1d3e-! (com (mk Out M P)) (Q m) p) \n\n Sim\u1d3e-apply : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n Sim\u1d3e-apply end Q s p = s\n Sim\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = Sim\u1d3e-apply (P m) Q (s m) p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = Sim\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n P \u214b\u1d3e end = P\n com P \u214b\u1d3e com Q = \u03a3\u1d3e LR (P \u214b\u1d9c Q)\n\n _\u214b\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 \u214b\u1d9c P\u1d3f) `L = P\u1d38 \u214b\u1d9cL P\u1d3f\n (P\u1d38 \u214b\u1d9c P\u1d3f) `R = P\u1d38 \u214b\u1d9cR P\u1d3f\n\n _\u214b\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) \u214b\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m \u214b\u1d3e com Q)\n\n _\u214b\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P \u214b\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P \u214b\u1d3e P\u1d3f m)\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end ox\u1d3e Q = Q\n P ox\u1d3e end = P\n com P\u1d38 ox\u1d3e com P\u1d3f = \u03a0\u1d3e LR (P\u1d38 ox\u1d9c P\u1d3f)\n\n _ox\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 ox\u1d9c P\u1d3f) `L = P\u1d38 ox\u1d9cL P\u1d3f\n (P\u1d38 ox\u1d9c P\u1d3f) `R = P\u1d38 ox\u1d9cR P\u1d3f\n\n _ox\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) ox\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m ox\u1d3e com Q)\n\n _ox\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P ox\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P ox\u1d3e P\u1d3f m)\n\n data ViewProc : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200 M(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewProc (\u03a3\u1d3e M P) (m , p)\n recv : \u2200 M(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewProc (\u03a0\u1d3e M P) p\n end : ViewProc end _\n\n view-proc : \u2200 P (p : \u27e6 P \u27e7) \u2192 ViewProc P p\n view-proc end _ = end\n view-proc (\u03a0\u1d3e M P) p = recv _ _ p\n view-proc (\u03a3\u1d3e M P) (m , p) = send _ _ m p\n\n data ViewCom : \u2200 P \u2192 \u27e6 com P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200{M}(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewCom (\u03a3\u1d9c M P) (m , p)\n recv : \u2200{M}(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewCom (\u03a0\u1d9c M P) p\n\n view-com : \u2200 P (p : \u27e6 com P \u27e7) \u2192 ViewCom P p\n view-com (\u03a0\u1d9c M P) p = recv _ p\n view-com (\u03a3\u1d9c M P) (m , p) = send _ m p\n\n data View-\u214b : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n sendL' : \u2200 {M} (P : M \u2192 Proto) Q (m : M)(p : \u27e6 P m \u214b\u1d3e com Q \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (com Q) (`L , m , p)\n sendR' : \u2200 {M} P (Q : M \u2192 Proto)(m : M)(p : \u27e6 com P \u214b\u1d3e Q m \u27e7) \u2192 View-\u214b (com P) (\u03a3\u1d3e M Q) (`R , m , p)\n recvL' : \u2200 {M} (P : M \u2192 Proto) Q (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e com Q \u27e7)) \u2192 View-\u214b (\u03a0\u1d3e M P) (com Q) (`L , p)\n recvR' : \u2200 {M} P (Q : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 com P \u214b\u1d3e Q m \u27e7)) \u2192 View-\u214b (com P) (\u03a0\u1d3e M Q) (`R , p)\n endL : \u2200 Q (p : \u27e6 Q \u27e7) \u2192 View-\u214b end Q p\n endR : \u2200 P (p : \u27e6 com P \u27e7) \u2192 View-\u214b (com P) end p\n\n view-\u214b : \u2200 P Q (p : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 View-\u214b P Q p\n view-\u214b end Q p = endL _ _\n view-\u214b (com x) end p = endR _ _\n view-\u214b (\u03a0\u1d3e M P) (com Q) (`L , p) = recvL' _ _ _\n view-\u214b (\u03a3\u1d3e M P) (com Q) (`L , (m , p)) = sendL' _ _ _ _\n view-\u214b (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) (`R , p) = recvR' _ _ _\n view-\u214b (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) (`R , (m , p)) = sendR' _ _ _ _\n view-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) (`R , p) = recvR' _ _ _\n view-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (`R , (m , p)) = sendR' _ _ _ _\n\n data Viewox : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n com : \u2200{P Q}(p : \u27e6 \u03a0\u1d3e LR (P ox\u1d9c Q) \u27e7) \u2192 Viewox (com P) (com Q) p\n endL : \u2200{Q}(p : \u27e6 Q \u27e7) \u2192 Viewox end Q p\n endR : \u2200{P}(p : \u27e6 com P \u27e7) \u2192 Viewox (com P) end p\n\n viewox : \u2200 P Q (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 Viewox P Q p\n viewox end Q p = endL p\n viewox (com P) end p = endR p\n viewox (com P) (com Q) p = com p\n\n data View-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u2605\u2081 where\n sendLL : \u2200 {M} (P : M \u2192 Proto) Q R (m : M)(p : \u27e6 P m \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e M P) (com Q) R (`L , m , p) q\n recvLL : \u2200 {M} (P : M \u2192 Proto) Q R\n (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e com Q \u27e7))(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a0\u1d3e M P) (com Q) R (`L , p) q\n sendRR : \u2200 {M} P Q (R : M \u2192 Proto)(m : M)(p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a3\u1d3e M R) p (`R , m , q)\n recvRR : \u2200 {M} P Q (R : M \u2192 Proto)\n (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : (m : M) \u2192 \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a0\u1d3e M R) p (`R , q)\n\n sendR-recvL : \u2200 {M} P (Q : M \u2192 Proto) R (m : M)(p : \u27e6 com P \u214b\u1d3e Q m \u27e7)(q : (m : M) \u2192 \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a3\u1d3e M Q) (com R) (`R , m , p) (`L , q)\n recvR-sendL : \u2200 {M} P (Q : M \u2192 Proto) R (p : (m : M) \u2192 \u27e6 com P \u214b\u1d3e Q m \u27e7)(m : M)(q : \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a0\u1d3e M Q) (com R) (`R , p) (`L , m , q)\n\n endL : \u2200 Q R\n \u2192 (q : \u27e6 Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 end Q R q qr\n\n endM : \u2200 P R\n \u2192 (p : \u27e6 com P \u27e7)(r : \u27e6 R \u27e7)\n \u2192 View-\u2218 (com P) end R p r\n endR : \u2200 P Q\n \u2192 (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u27e7)\n \u2192 View-\u2218 (com P) (com Q) end p q\n\n view-\u2218 : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7) \u2192 View-\u2218 P Q R pq qr\n view-\u2218 P Q R pq qr = view-\u2218-view (view-\u214b P Q pq) (view-\u214b (dual Q) R qr)\n where\n view-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u214b P Q pq \u2192 View-\u214b (dual Q) R qr \u2192 View-\u2218 P Q R pq qr\n view-\u2218-view (sendL' _ _ _ _) _ = sendLL _ _ _ _ _ _\n view-\u2218-view (recvL' _ _ _) _ = recvLL _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvL' ._ _ _) = sendR-recvL _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (endR ._ _) = endR _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendL' ._ _ _ _) = recvR-sendL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (endR ._ ._) = endR _ _ _ _\n view-\u2218-view (endL _ _) _ = endL _ _ _ _\n view-\u2218-view (endR _ _) _ = endM _ _ _ _\n\n record Equiv {A B : \u2605}(f : A \u2192 B) : \u2605 where\n field\n linv : B \u2192 A\n is-linv : \u2200 x \u2192 linv (f x) \u2261 x\n rinv : B \u2192 A\n is-rinv : \u2200 x \u2192 f (rinv x) \u2261 x\n\n id\u1d31 : \u2200 {A} \u2192 Equiv {A} id\n id\u1d31 = record\n { linv = id\n ; is-linv = \u03bb _ \u2192 refl\n ; rinv = id\n ; is-rinv = \u03bb _ \u2192 refl\n }\n\n module _ {A B C}{g : B \u2192 C}{f : A \u2192 B} where\n _\u2218\u1d31_ : Equiv g \u2192 Equiv f \u2192 Equiv (g \u2218 f)\n g\u1d31 \u2218\u1d31 f\u1d31 = record { linv = F.linv \u2218 G.linv ; is-linv = \u03bb x \u2192 cong F.linv (G.is-linv (f x)) \u2219 F.is-linv x\n ; rinv = F.rinv \u2218 G.rinv ; is-rinv = \u03bb x \u2192 cong g (F.is-rinv _) \u2219 G.is-rinv x }\n where\n module G = Equiv g\u1d31\n module F = Equiv f\u1d31\n\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = \u03a3 (A \u2192 B) Equiv\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end = id\n \u214b\u1d3e-rend (com _) = id\n\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (com P) m p = `R , (m , p)\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL (com _) m p = `L , (m , p)\n\n \u214b\u1d3e-recvR : \u2200 {M}P{Q : M \u2192 Proto} \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e com' In M Q \u27e7\n \u214b\u1d3e-recvR end f = f\n \u214b\u1d3e-recvR (com _) f = `R , f\n\n \u214b\u1d3e-recvL : \u2200 {M}{P : M \u2192 Proto}Q \u2192 ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7) \u2192 \u27e6 \u03a0\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-recvL {P = P} end f x = \u214b\u1d3e-rend (P x) (f x)\n \u214b\u1d3e-recvL (com _) f = `L , f\n\n ox\u1d3e-rend : \u2200 P \u2192 \u27e6 P ox\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-rend end = id\n ox\u1d3e-rend (com x) = id\n\n ox\u1d3e-sendR : \u2200 {M P}{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n ox\u1d3e-sendR {P = end} m p = m , p\n ox\u1d3e-sendR {P = com x} m p = `R , (m , p)\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id end = _\n \u214b\u1d3e-id (com (mk In M P)) = `R , \u03bb m \u2192 \u214b\u1d3e-sendL (P m) m (\u214b\u1d3e-id (P m))\n \u214b\u1d3e-id (com (mk Out M P)) = `L , \u03bb m \u2192 \u214b\u1d3e-sendR (dual (P m)) m (\u214b\u1d3e-id (P m))\n\n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm P Q = {!!}\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e end Q p q = q\n comma\u1d3e (com _) end p q = p\n comma\u1d3e (\u03a3\u1d3e M P) (com Q) (m , p) q `L = m , comma\u1d3e (P m) (com Q) p q\n comma\u1d3e (\u03a0\u1d3e M P) (com Q) p q `L = \u03bb m \u2192 comma\u1d3e (P m) (com Q) (p m) q\n comma\u1d3e (com P) (\u03a3\u1d3e M Q) p (m , q) `R = m , comma\u1d3e (com P) (Q m) p q\n comma\u1d3e (com P) (\u03a0\u1d3e M Q) p q `R = \u03bb m \u2192 comma\u1d3e (com P) (Q m) p (q m)\n\n \u00d7\u2192ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n \u00d7\u2192ox\u1d3e P Q (p , q) = comma\u1d3e P Q p q\n\n ox\u1d3e-fst : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-fst end Q pq = _\n ox\u1d3e-fst (com _) end pq = pq\n ox\u1d3e-fst (\u03a0\u1d3e M P) (com Q) pq = \u03bb m \u2192 ox\u1d3e-fst (P m) (com Q) (pq `L m)\n ox\u1d3e-fst (\u03a3\u1d3e M P) (com Q) pq = \u00d7-map id (\u03bb {x} \u2192 ox\u1d3e-fst (P x) (com Q)) (pq `L)\n\n ox\u1d3e-snd : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 Q \u27e7\n ox\u1d3e-snd end Q pq = pq\n ox\u1d3e-snd (com _) end pq = _\n ox\u1d3e-snd (com P) (\u03a0\u1d3e M' Q) pq = \u03bb m' \u2192 ox\u1d3e-snd (com P) (Q m') (pq `R m')\n ox\u1d3e-snd (com P) (\u03a3\u1d3e M' Q) pq = \u00d7-map id (\u03bb {m'} \u2192 ox\u1d3e-snd (com P) (Q m')) (pq `R)\n\n ox\u1d3e\u2192\u00d7 : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n ox\u1d3e\u2192\u00d7 P Q p = ox\u1d3e-fst P Q p , ox\u1d3e-snd P Q p\n\n ox\u1d3e-comma-fst : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-fst P Q (comma\u1d3e P Q p q) \u2261 p\n ox\u1d3e-comma-fst end Q p q = refl\n ox\u1d3e-comma-fst (com P) end p q = refl\n ox\u1d3e-comma-fst (com P) (com Q) p q with view-com P p\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | send P m p = cong (_,_ m) (ox\u1d3e-comma-fst (P m) (com Q) p q)\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | recv P p = funExt \u03bb m \u2192 ox\u1d3e-comma-fst (P m) (com Q) (p m) q\n\n ox\u1d3e-comma-snd : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-snd P Q (comma\u1d3e P Q p q) \u2261 q\n ox\u1d3e-comma-snd end Q p q = refl\n ox\u1d3e-comma-snd (com P) end p q = refl\n ox\u1d3e-comma-snd (com P) (com Q) p q with view-com Q q\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p (q m)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p (q m)\n\n {-\n ox\u1d3e\u2192\u00d7-rinv : \u2200 {P Q} (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 \u00d7\u2192ox\u1d3e P Q (ox\u1d3e\u2192\u00d7 P Q p) \u2261 p\n ox\u1d3e\u2192\u00d7-rinv {P} {Q} p with viewox P Q p\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk In M\u2081 P\u2081} .p = funExt \u03bb { `L \u2192 funExt \u03bb m \u2192 {!ox\u1d3e\u2192\u00d7-rinv {P m} {\u03a0\u1d3e _ P\u2081} ?!} ; `R \u2192 {!!} }\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk In M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | endL .p = refl\n ox\u1d3e\u2192\u00d7-rinv p | endR {P} .p = refl\n\n \u00d7\u2192ox\u1d3e-equiv : \u2200 P Q \u2192 Equiv (\u00d7\u2192ox\u1d3e P Q)\n \u00d7\u2192ox\u1d3e-equiv P Q = record { linv = ox\u1d3e\u2192\u00d7 P Q\n ; is-linv = \u03bb { (x , y) \u2192 cong\u2082 _,_ (ox\u1d3e-comma-fst P Q x y) (ox\u1d3e-comma-snd P Q x y) }\n ; rinv = ox\u1d3e\u2192\u00d7 P Q\n ; is-rinv = ox\u1d3e\u2192\u00d7-rinv {P} {Q} }\n -}\n\n -- left-biased strategy\n par : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par P Q p q = par-view (view-proc P p)\n where par-view : \u2200 {P} {p : \u27e6 P \u27e7} \u2192 ViewProc P p \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par-view (send M P m p) = \u214b\u1d3e-sendL Q m (par (P m) Q p q)\n par-view (recv M P p) = \u214b\u1d3e-recvL Q \u03bb m \u2192 par (P m) Q (p m) q\n par-view end = q\n\n \u214b\u1d3e-apply : \u2200 {P Q} \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply {P} {Q} pq p with view-\u214b P Q pq\n \u214b\u1d3e-apply ._ p | sendL' P Q m pq = \u214b\u1d3e-apply {P m} pq (p m)\n \u214b\u1d3e-apply ._ p | sendR' P Q m pq = m , \u214b\u1d3e-apply {com P} {Q m} pq p\n \u214b\u1d3e-apply ._ (m , p) | recvL' P Q pq = \u214b\u1d3e-apply {P m} (pq m) p\n \u214b\u1d3e-apply ._ p | recvR' P Q pq = \u03bb m \u2192 \u214b\u1d3e-apply {com P} {Q m} (pq m) p\n \u214b\u1d3e-apply pq p | endL Q .pq = pq\n \u214b\u1d3e-apply pq p | endR P .pq = _\n\n \u214b\u1d3e-apply' : \u2200 {P Q} \u2192 \u27e6 dual P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply' {P} {Q} pq p = \u214b\u1d3e-apply {dual P} {Q} pq (subst \u27e6_\u27e7 (\u2261.sym (\u2261\u1d3e-sound funExt (dual-involutive P))) p)\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = \u214b\u1d3e-\u2218-view (view-\u2218 P Q R pq qr)\n where\n \u214b\u1d3e-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u2218 P Q R pq qr \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218-view (sendLL P Q R m p qr) = \u214b\u1d3e-sendL R m (\u214b\u1d3e-\u2218 (P m) (com Q) R p qr)\n \u214b\u1d3e-\u2218-view (recvLL P Q R p qr) = \u214b\u1d3e-recvL R (\u03bb m \u2192 \u214b\u1d3e-\u2218 (P m) (com Q) R (p m) qr)\n \u214b\u1d3e-\u2218-view (sendRR P Q R m pq q) = \u214b\u1d3e-sendR (com P) m (\u214b\u1d3e-\u2218 (com P) (com Q) (R m) pq q)\n \u214b\u1d3e-\u2218-view (recvRR P Q R pq q) = \u214b\u1d3e-recvR (com P) (\u03bb m\u2081 \u2192 \u214b\u1d3e-\u2218 (com P) (com Q) (R m\u2081) pq (q m\u2081))\n \u214b\u1d3e-\u2218-view (sendR-recvL P Q R m p q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) p (q m)\n \u214b\u1d3e-\u2218-view (recvR-sendL P Q R p m q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) (p m) q\n \u214b\u1d3e-\u2218-view (endL Q R pq qr) = \u214b\u1d3e-apply' {Q} {R} qr pq\n \u214b\u1d3e-\u2218-view (endM P R pq qr) = par (com P) R pq qr\n \u214b\u1d3e-\u2218-view (endR P Q pq qr) = {!\u214b\u1d3e-apply {?} {?} pq qr!}\n\n {-\n \u214b\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u214b\u1d3e R \u27e7 \u2192 \u27e6 Q \u214b\u1d3e S \u27e7\n \u214b\u1d3e-map P Q R S f g p with view-\u214b P R p\n \u214b\u1d3e-map .(com (mk Out M P)) Q\u2081 .(com Q) S f g .(`L , m , p) | sendL' M P Q m p = {!!}\n \u214b\u1d3e-map .(com P) Q\u2081 .(com (mk Out M Q)) S f g .(`R , m , p) | sendR' P M Q m p = {!!}\n \u214b\u1d3e-map .(com (mk In M P)) Q\u2081 .(com Q) S f g .(`L , p) | recvL' M P Q p = {!!}\n \u214b\u1d3e-map .(com P) Q\u2081 .(com (mk In M Q)) S f g .(`R , p) | recvR' P M Q p = {!!}\n \u214b\u1d3e-map .end Q R S f g p | endL .R .p = {!!}\n \u214b\u1d3e-map .(com P) Q .end S f g p | endR P .p = {!!}\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map P Q R S f g p = comma\u1d3e Q S (f (ox\u1d3e-fst P R p)) (g (ox\u1d3e-snd P R p))\n\n switchL' : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL' P Q R p\u214bq r with view-\u214b P Q p\u214bq\n switchL' ._ ._ R .(`L , m , p) r | sendL' M P Q m p = \u214b\u1d3e-sendL (com Q ox\u1d3e R) m (switchL' (P m) (com Q) R p r)\n switchL' ._ ._ R .(`R , m , p) r | sendR' P M Q m p = \u214b\u1d3e-map (com P) (com P) (Q m ox\u1d3e R) (\u03a3\u1d3e M Q ox\u1d3e R) id\n (ox\u1d3e-map (Q m) (\u03a3\u1d3e M Q) R R (_,_ m) id)\n (switchL' (com P) (Q m) R p r)\n switchL' ._ ._ R .(`L , p) r | recvL' M P Q p = \u214b\u1d3e-recvL (com Q ox\u1d3e R) \u03bb m \u2192 switchL' (P m) (com Q) R (p m) r\n switchL' ._ ._ R .(`R , p) r | recvR' (\u03a0\u1d9c M' P) M Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL' ._ ._ R .(`R , p) r | recvR' (\u03a3\u1d9c M' P) M Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL' ._ ._ R p\u214bq r | endL Q .p\u214bq = comma\u1d3e Q R p\u214bq r\n switchL' .(com P) .end R p\u214bq r | endR P .p\u214bq = par (com P) R p\u214bq r\n\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = switchL' P Q R (ox\u1d3e-fst (P \u214b\u1d3e Q) R pqr) (ox\u1d3e-snd (P \u214b\u1d3e Q) R pqr)\n -}\n\n {-\n \u2295\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map = {!!}\n\n &\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map = {!!}\n\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Tele B \u00d7 Tele E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a3\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a0\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\nopen import Data.One hiding (_\u225f_)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\n {-\ndata S<_> {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n\npattern com' q M P = com (mk q M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\n\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n Trace : Proto \u2192 Proto\n Trace end = end\n Trace (com c) = com (Trace\u1d9c c)\n\n Trace\u1d9c : Com \u2192 Com\n Trace\u1d9c (mk io M P) = \u03a3\u1d9c M \u03bb m \u2192 Trace (P m)\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c (mk q M P) = mk (dual\u1d35\u1d3c q) M \u03bb m \u2192 dual (P m)\n\ndata IsTrace : Proto \u2192 \u2605\u2081 where\n end : IsTrace end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsTrace (P m)) \u2192 IsTrace (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\nmutual\n data Dual\u1d9c : Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a0\u1d9c M P) (\u03a3\u1d9c M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a3\u1d9c M P) (\u03a0\u1d9c M Q)\n\n data Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n {-\n \u03a0\u2610\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P [ x ]) (Q x)) \u2192 Dual (\u03a0\u1d3e (\u2610 M) P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0\u2610 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q [ x ])) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e (\u2610 M) Q)\n -}\n\n {-\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Trace : Choreo I \u2192 Proto\n \u2102Trace \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Trace-IsTrace : \u2200 \u2102 \u2192 IsTrace (\u2102Trace \u2102)\n \u2102Trace-IsTrace (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com q M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com q M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\nTrace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261\u1d3e Trace P\nTrace-idempotent end = end\nTrace-idempotent (com' _ M P) = com Out M \u03bb m \u2192 Trace-idempotent (P m)\n\nTrace-dual-oblivious : \u2200 P \u2192 Trace (dual P) \u2261\u1d3e Trace P\nTrace-dual-oblivious end = end\nTrace-dual-oblivious (com' _ M P) = com Out M \u03bb m \u2192 Trace-dual-oblivious (P m)\n\nSink : Proto \u2192 Proto\nSink = dual \u2218 Trace\n-}\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n{-\n++Tele : \u2200 (P : Proto){Q : Tele P \u2192 Proto} (xs : Tele P) \u2192 Tele (Q xs) \u2192 Tele (P >>= Q)\n++Tele end _ ys = ys\n++Tele (com' q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com q M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com q M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n{-\nDual-sym (\u03a0\u2610\u00b7\u03a3 f) = {!\u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))!}\nDual-sym (\u03a3\u00b7\u03a0\u2610 f) = {!\u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))!}\n-}\n-}\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele (dual P) \u2261 Tele P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n El->>= : (P : Proto){Q : Tele P \u2192 Proto}{X : Tele (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Tele P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n-}\n\n>>=-com : (P : Proto){Q : Tele P \u2192 Proto}{R : Tele P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Tele P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Tele P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : \u2605_ a} (l r : A) \u2192 LR \u2192 A \n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP >>\u1d9c S = record P { P = \u03bb m \u2192 S (Com.P P m) }\n\n{-\nmodule V2 where\n mutual\n Sim\u1d3e : Proto \u2192 Proto \u2192 Proto\n Sim\u1d3e end Q = Q\n Sim\u1d3e (\u03a0\u1d3e M P) Q = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e (P m) Q\n Sim\u1d3e P end = P\n Sim\u1d3e P (\u03a0\u1d3e M Q) = \u03a0\u1d3e M \u03bb m \u2192 Sim\u1d3e P (Q m)\n Sim\u1d3e (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 Sim\u1d3e (P m) (\u03a3\u1d3e M' Q)) ,inr: (\u03bb m' \u2192 Sim\u1d3e (\u03a3\u1d3e M P) (Q m')) ]\n\n Sim\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\n Sim\u1d3e-assoc end Q R s = s\n Sim\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = Sim\u1d3e-assoc (P m) _ _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , Sim\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , Sim\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n Sim\u1d3e-sendR : \u2200 {M} P Q \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7 \u2192 (m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7\n Sim\u1d3e-sendR P Q s m = {!!}\n\n Sim\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 Sim\u1d3e P (Q m) \u27e7) \u2192 \u27e6 Sim\u1d3e P (\u03a0\u1d3e M Q) \u27e7\n Sim\u1d3e-recvR P Q s = {!!}\n\n Sim\u1d3e-! : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 Sim\u1d3e Q P \u27e7\n Sim\u1d3e-! end Q p = {!!}\n Sim\u1d3e-! (\u03a0\u1d3e M P) end p x = Sim\u1d3e-! (P x) end (p x)\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-recvR (Q m') P \u03bb m \u2192 Sim\u1d3e-sendR {!!} {!!} (p m) m'\n Sim\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 Sim\u1d3e-! (P m) (com (mk Out M' Q)) (p m) \n Sim\u1d3e-! (\u03a3\u1d3e M P) end p = p \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 Sim\u1d3e-! (com (mk Out M P)) (Q m') (p m') \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (Sim\u1d3e-! (P m) (com (mk Out M' Q)) p) \n Sim\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (Sim\u1d3e-! (com (mk Out M P)) (Q m) p) \n\n Sim\u1d3e-apply : \u2200 P Q \u2192 \u27e6 Sim\u1d3e P Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n Sim\u1d3e-apply end Q s p = s\n Sim\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = Sim\u1d3e-apply (P m) Q (s m) p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = Sim\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n Sim\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , Sim\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n -}\n\nmodule _ where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n P \u214b\u1d3e end = P\n com P\u1d38 \u214b\u1d3e com P\u1d3f = \u03a3\u1d3e LR (P\u1d38 \u214b\u1d9c P\u1d3f)\n\n _\u214b\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 \u214b\u1d9c P\u1d3f) `L = P\u1d38 \u214b\u1d9cL P\u1d3f\n (P\u1d38 \u214b\u1d9c P\u1d3f) `R = P\u1d38 \u214b\u1d9cR P\u1d3f\n\n _\u214b\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) \u214b\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m \u214b\u1d3e com Q)\n\n _\u214b\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P \u214b\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P \u214b\u1d3e P\u1d3f m)\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end ox\u1d3e Q = Q\n P ox\u1d3e end = P\n com P\u1d38 ox\u1d3e com P\u1d3f = \u03a0\u1d3e LR (P\u1d38 ox\u1d9c P\u1d3f)\n\n _ox\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 ox\u1d9c P\u1d3f) `L = P\u1d38 ox\u1d9cL P\u1d3f\n (P\u1d38 ox\u1d9c P\u1d3f) `R = P\u1d38 ox\u1d9cR P\u1d3f\n\n _ox\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) ox\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m ox\u1d3e com Q)\n\n _ox\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P ox\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P ox\u1d3e P\u1d3f m)\n\n record Equiv {A B : \u2605}(f : A \u2192 B) : \u2605 where\n field\n linv : B \u2192 A\n is-linv : \u2200 x \u2192 linv (f x) \u2261 x\n rinv : B \u2192 A\n is-rinv : \u2200 x \u2192 f (rinv x) \u2261 x\n\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = \u03a3 (A \u2192 B) Equiv\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end = id\n \u214b\u1d3e-rend (com x) = id\n\n \u214b\u1d3e-sendR : \u2200 {M P}{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR {P = end} m p = m , p\n \u214b\u1d3e-sendR {P = com x} m p = `R , (m , p)\n\n \u214b\u1d3e-sendL : \u2200 {M P}{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 Q m \u214b\u1d3e P \u27e7 \u2192 \u27e6 com' Out M Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-sendL {P = end}{Q} m p = m , \u214b\u1d3e-rend (Q m) p\n \u214b\u1d3e-sendL {P = com x} m p = `L , (m , p)\n\n \u214b\u1d3e-recvR : \u2200 {M P}{Q : M \u2192 Proto} \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e com' In M Q \u27e7\n \u214b\u1d3e-recvR {P = end} f = f\n \u214b\u1d3e-recvR {P = com x} f = `R , f\n\n \u214b\u1d3e-recvL : \u2200 {M P}{Q : M \u2192 Proto} \u2192 ((m : M) \u2192 \u27e6 Q m \u214b\u1d3e P \u27e7) \u2192 \u27e6 com' In M Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-recvL {P = end}{Q} f x = \u214b\u1d3e-rend (Q x) (f x)\n \u214b\u1d3e-recvL {P = com x} f = `L , f\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id end = _\n \u214b\u1d3e-id (com (mk In M P)) = `R , \u03bb m \u2192 \u214b\u1d3e-sendL {P = P m} m (\u214b\u1d3e-id (P m))\n \u214b\u1d3e-id (com (mk Out M P)) = `L , \u03bb m \u2192 \u214b\u1d3e-sendR {P = dual (P m)} m (\u214b\u1d3e-id (P m))\n\n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm P Q = {!!}\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e end Q p q = q\n comma\u1d3e (com x) end p q = p\n comma\u1d3e (\u03a3\u1d3e M P) (com Q) (m , p) q `L = m , comma\u1d3e (P m) (com Q) p q\n comma\u1d3e (\u03a0\u1d3e M P) (com Q) p q `L = \u03bb m \u2192 comma\u1d3e (P m) (com Q) (p m) q\n comma\u1d3e (com P) (\u03a3\u1d3e M Q) p (m , q) `R = m , comma\u1d3e (com P) (Q m) p q\n comma\u1d3e (com P) (\u03a0\u1d3e M Q) p q `R = \u03bb m \u2192 comma\u1d3e (com P) (Q m) p (q m)\n\n comma\u1d3e-equiv : \u2200 P Q \u2192 Equiv (comma\u1d3e P Q)\n comma\u1d3e-equiv P Q = {!!}\n\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = {!!}\n\n \u2295\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map = {!!}\n\n &\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map = {!!}\n\n \u214b\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u214b\u1d3e R \u27e7 \u2192 \u27e6 Q \u214b\u1d3e S \u27e7\n \u214b\u1d3e-map = {!!}\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map = {!!}\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = let z = switchL _ _ _ (comma\u1d3e (P \u214b\u1d3e Q) (dual Q \u214b\u1d3e R) pq qr) in {!z!}\n\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Tele B \u00d7 Tele E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a3\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a0\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"6382a6d1e7dc0c527b8890748438731dc69f3134","subject":"Agda upgrade","message":"Agda upgrade\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/Type.agda","new_file":"formalization\/agda\/Spire\/Type.agda","new_contents":"module Spire.Type where\n\n----------------------------------------------------------------------\n\ndata \u22a5 : Set where\nrecord \u22a4 : Set where constructor tt\ndata Bool : Set where true false : Bool\n\ndata \u2115 : Set where\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n{-# BUILTIN NATURAL \u2115 #-}\n\nrecord \u03a3 (A : Set) (B : A \u2192 Set) : Set where\n constructor _,_\n field\n proj\u2081 : A\n proj\u2082 : B proj\u2081\nopen \u03a3 public\n\n----------------------------------------------------------------------\n\nconst : {A : Set\u2081} {B : Set} \u2192 A \u2192 B \u2192 A\nconst a _ = a\n\nuncurry : {A : Set} {B : A \u2192 Set} {C : \u03a3 A B \u2192 Set} \u2192\n ((a : A) \u2192 (b : B a) \u2192 C (a , b)) \u2192\n ((p : \u03a3 A B) \u2192 C p)\nuncurry f (a , b) = f a b\n\n_\u2218_ : {A B C : Set\u2081} \u2192 (B \u2192 C) \u2192 (A \u2192 B) \u2192 A \u2192 C\n_\u2218_ g f x = g (f x)\n\n----------------------------------------------------------------------\n\n\nelim\u22a5 : {A : Set} \u2192 \u22a5 \u2192 A\nelim\u22a5 ()\n\nelimBool : (P : Bool \u2192 Set)\n (pt : P true)\n (pf : P false)\n (b : Bool) \u2192 P b\nelimBool P pt pf true = pt\nelimBool P pt pf false = pf\n\nif_then_else_ : {C : Set} \u2192 Bool \u2192 C \u2192 C \u2192 C\nif b then c\u2081 else c\u2082 = elimBool _ c\u2081 c\u2082 b\n\nelim\u2115 : (P : \u2115 \u2192 Set)\n (pz : P zero)\n (ps : (n : \u2115) \u2192 P n \u2192 P (suc n))\n (n : \u2115) \u2192 P n\nelim\u2115 P pz ps zero = pz\nelim\u2115 P pz ps (suc n) = ps n (elim\u2115 P pz ps n)\n\n----------------------------------------------------------------------\n\nrecord Universe : Set\u2081 where\n field\n Codes : Set\n Meaning : Codes \u2192 Set\n\n----------------------------------------------------------------------\n\ndata DescForm (U : Universe) : Set where\n `\u22a4 `X : DescForm U\n `\u03a0 `\u03a3 : (A : Universe.Codes U)\n (D : Universe.Meaning U A \u2192 DescForm U)\n \u2192 DescForm U\n\n\u27e6_\/_\u27e7\u1d48 : (U : Universe) \u2192 DescForm U \u2192 Set \u2192 Set\n\u27e6 U \/ `\u22a4 \u27e7\u1d48 X = \u22a4\n\u27e6 U \/ `X \u27e7\u1d48 X = X\n\u27e6 U \/ `\u03a0 A D \u27e7\u1d48 X =\n (a : Universe.Meaning U A) \u2192 \u27e6 U \/ D a \u27e7\u1d48 X\n\u27e6 U \/ `\u03a3 A D \u27e7\u1d48 X =\n \u03a3 (Universe.Meaning U A) (\u03bb a \u2192 \u27e6 U \/ D a \u27e7\u1d48 X)\n\ndata \u03bc {U : Universe} (D : DescForm U) : Set where\n con : \u27e6 U \/ D \u27e7\u1d48 (\u03bc D) \u2192 \u03bc D\n\n----------------------------------------------------------------------\n\ndata TypeForm (U : Universe) : Set\n\u27e6_\/_\u27e7 : (U : Universe) \u2192 TypeForm U \u2192 Set\n\ndata TypeForm U where\n `\u22a5 `\u22a4 `Bool `\u2115 `Desc `Type : TypeForm U\n `\u03a0 `\u03a3 : (A : TypeForm U)\n (B : \u27e6 U \/ A \u27e7 \u2192 TypeForm U)\n \u2192 TypeForm U\n `\u27e6_\u27e7 : Universe.Codes U \u2192 TypeForm U\n `\u27e6_\u27e7\u1d48 : DescForm U \u2192 TypeForm U \u2192 TypeForm U\n `\u03bc : DescForm U \u2192 TypeForm U\n\n\u27e6 U \/ `\u22a5 \u27e7 = \u22a5\n\u27e6 U \/ `\u22a4 \u27e7 = \u22a4\n\u27e6 U \/ `Bool \u27e7 = Bool\n\u27e6 U \/ `\u2115 \u27e7 = \u2115\n\u27e6 U \/ `\u03a0 A B \u27e7 = (a : \u27e6 U \/ A \u27e7) \u2192 \u27e6 U \/ B a \u27e7\n\u27e6 U \/ `\u03a3 A B \u27e7 = \u03a3 \u27e6 U \/ A \u27e7 (\u03bb a \u2192 \u27e6 U \/ B a \u27e7)\n\u27e6 U \/ `Type \u27e7 = Universe.Codes U\n\u27e6 U \/ `\u27e6 A \u27e7 \u27e7 = Universe.Meaning U A\n\u27e6 U \/ `Desc \u27e7 = DescForm U\n\u27e6 U \/ `\u27e6 D \u27e7\u1d48 X \u27e7 = \u27e6 U \/ D \u27e7\u1d48 \u27e6 U \/ X \u27e7\n\u27e6 U \/ `\u03bc D \u27e7 = \u03bc D\n\n----------------------------------------------------------------------\n\n_`\u2192_ : \u2200{U} (A B : TypeForm U) \u2192 TypeForm U\nA `\u2192 B = `\u03a0 A (\u03bb _ \u2192 B)\n\nLevel : (\u2113 : \u2115) \u2192 Universe\nLevel zero = record { Codes = \u22a5 ; Meaning = \u03bb() }\nLevel (suc \u2113) = record { Codes = TypeForm (Level \u2113)\n ; Meaning = \u27e6_\/_\u27e7 (Level \u2113) }\n\nType : \u2115 \u2192 Set\nType \u2113 = TypeForm (Level \u2113)\n\nDesc : \u2115 \u2192 Set\nDesc \u2113 = DescForm (Level \u2113)\n\ninfix 0 \u27e6_\u2223_\u27e7\n\u27e6_\u2223_\u27e7 : (\u2113 : \u2115) \u2192 Type \u2113 \u2192 Set\n\u27e6 \u2113 \u2223 A \u27e7 = \u27e6 Level \u2113 \/ A \u27e7\n\n\u27e6_\u2223_\u27e7\u1d48 : (\u2113 : \u2115) \u2192 Desc \u2113 \u2192 Set \u2192 Set\n\u27e6 \u2113 \u2223 D \u27e7\u1d48 X = \u27e6 Level \u2113 \/ D \u27e7\u1d48 X\n\n----------------------------------------------------------------------\n\nelimDesc : (P : (\u2113 : \u2115) \u2192 Desc \u2113 \u2192 Set)\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `\u22a4)\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `X)\n \u2192 ((\u2113 : \u2115) (A : Type \u2113) (D : \u27e6 \u2113 \u2223 A \u27e7 \u2192 Desc (suc \u2113))\n (rec : (a : \u27e6 \u2113 \u2223 A \u27e7) \u2192 P (suc \u2113) (D a))\n \u2192 P (suc \u2113) (`\u03a0 A D))\n \u2192 ((\u2113 : \u2115) (A : Type \u2113) (D : \u27e6 \u2113 \u2223 A \u27e7 \u2192 Desc (suc \u2113))\n (rec : (a : \u27e6 \u2113 \u2223 A \u27e7) \u2192 P (suc \u2113) (D a))\n \u2192 P (suc \u2113) (`\u03a3 A D))\n \u2192 (\u2113 : \u2115) (D : Desc \u2113) \u2192 P \u2113 D\nelimDesc P p\u22a4 pX p\u03a0 p\u03a3 \u2113 `\u22a4 = p\u22a4 \u2113\nelimDesc P p\u22a4 pX p\u03a0 p\u03a3 \u2113 `X = pX \u2113\nelimDesc P p\u22a4 pX p\u03a0 p\u03a3 zero (`\u03a0 () D)\nelimDesc P p\u22a4 pX p\u03a0 p\u03a3 (suc \u2113) (`\u03a0 A D) =\n let f = elimDesc P p\u22a4 pX p\u03a0 p\u03a3 (suc \u2113)\n in p\u03a0 \u2113 A D (\u03bb a \u2192 f (D a))\nelimDesc P p\u22a4 pX p\u03a0 p\u03a3 zero (`\u03a3 () D)\nelimDesc P p\u22a4 pX p\u03a0 p\u03a3 (suc \u2113) (`\u03a3 A D) =\n let f = elimDesc P p\u22a4 pX p\u03a0 p\u03a3 (suc \u2113)\n in p\u03a3 \u2113 A D (\u03bb a \u2192 f (D a))\n\ndes : \u2200{\u2113} {D : Desc \u2113} \u2192 \u03bc D \u2192 \u27e6 \u2113 \u2223 D \u27e7\u1d48 (\u03bc D)\ndes (con x) = x\n\n----------------------------------------------------------------------\n\nelimType : (P : (\u2113 : \u2115) \u2192 Type \u2113 \u2192 Set)\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `\u22a5)\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `\u22a4)\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `Bool)\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `\u2115)\n \u2192 ((\u2113 : \u2115) (A : Type \u2113) (B : \u27e6 \u2113 \u2223 A \u27e7 \u2192 Type \u2113)\n (rec\u2081 : P \u2113 A)\n (rec\u2082 : (a : \u27e6 \u2113 \u2223 A \u27e7) \u2192 P \u2113 (B a))\n \u2192 P \u2113 (`\u03a0 A B))\n \u2192 ((\u2113 : \u2115) (A : Type \u2113) (B : \u27e6 \u2113 \u2223 A \u27e7 \u2192 Type \u2113)\n (rec\u2081 : P \u2113 A)\n (rec\u2082 : (a : \u27e6 \u2113 \u2223 A \u27e7) \u2192 P \u2113 (B a))\n \u2192 P \u2113 (`\u03a3 A B))\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `Desc)\n \u2192 ((\u2113 : \u2115) (D : Desc \u2113) (X : Type \u2113) (rec : P \u2113 X) \u2192 P \u2113 (`\u27e6 D \u27e7\u1d48 X))\n \u2192 ((\u2113 : \u2115) (D : Desc \u2113) \u2192 P \u2113 (`\u03bc D))\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `Type)\n \u2192 ((\u2113 : \u2115) (A : Type \u2113) (rec : P \u2113 A) \u2192 P (suc \u2113) `\u27e6 A \u27e7)\n \u2192 (\u2113 : \u2115) (A : Type \u2113) \u2192 P \u2113 A\n\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 `\u22a5 = p\u22a5 \u2113\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 `\u22a4 = p\u22a4 \u2113\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 `Bool = pBool \u2113\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 `\u2115 = p\u2115 \u2113\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 (`\u03a0 A B) =\n let f = elimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7 \u2113\n in p\u03a0 \u2113 A B (f A) (\u03bb a \u2192 f (B a))\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 (`\u03a3 A B) =\n let f = elimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7 \u2113\n in p\u03a3 \u2113 A B (f A) (\u03bb a \u2192 f (B a))\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 `Type = pType \u2113\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 `Desc = pDesc \u2113\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 (`\u27e6 D \u27e7\u1d48 X) =\n let f = elimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7 \u2113\n in p\u27e6D\u27e7\u1d48 \u2113 D X (f X)\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 (`\u03bc D) = p\u03bc \u2113 D\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n zero `\u27e6 () \u27e7\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n (suc \u2113) `\u27e6 A \u27e7 =\n let f = elimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7 \u2113\n in p\u27e6A\u27e7 \u2113 A (f A)\n\n----------------------------------------------------------------------\n","old_contents":"module Spire.Type where\n\n----------------------------------------------------------------------\n\ndata \u22a5 : Set where\nrecord \u22a4 : Set where constructor tt\ndata Bool : Set where true false : Bool\n\ndata \u2115 : Set where\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n{-# BUILTIN NATURAL \u2115 #-}\n{-# BUILTIN ZERO zero #-}\n{-# BUILTIN SUC suc #-}\n\nrecord \u03a3 (A : Set) (B : A \u2192 Set) : Set where\n constructor _,_\n field\n proj\u2081 : A\n proj\u2082 : B proj\u2081\nopen \u03a3 public\n\n----------------------------------------------------------------------\n\nconst : {A : Set\u2081} {B : Set} \u2192 A \u2192 B \u2192 A\nconst a _ = a\n\nuncurry : {A : Set} {B : A \u2192 Set} {C : \u03a3 A B \u2192 Set} \u2192\n ((a : A) \u2192 (b : B a) \u2192 C (a , b)) \u2192\n ((p : \u03a3 A B) \u2192 C p)\nuncurry f (a , b) = f a b\n\n_\u2218_ : {A B C : Set\u2081} \u2192 (B \u2192 C) \u2192 (A \u2192 B) \u2192 A \u2192 C\n_\u2218_ g f x = g (f x)\n\n----------------------------------------------------------------------\n\n\nelim\u22a5 : {A : Set} \u2192 \u22a5 \u2192 A\nelim\u22a5 ()\n\nelimBool : (P : Bool \u2192 Set)\n (pt : P true)\n (pf : P false)\n (b : Bool) \u2192 P b\nelimBool P pt pf true = pt\nelimBool P pt pf false = pf\n\nif_then_else_ : {C : Set} \u2192 Bool \u2192 C \u2192 C \u2192 C\nif b then c\u2081 else c\u2082 = elimBool _ c\u2081 c\u2082 b\n\nelim\u2115 : (P : \u2115 \u2192 Set)\n (pz : P zero)\n (ps : (n : \u2115) \u2192 P n \u2192 P (suc n))\n (n : \u2115) \u2192 P n\nelim\u2115 P pz ps zero = pz\nelim\u2115 P pz ps (suc n) = ps n (elim\u2115 P pz ps n)\n\n----------------------------------------------------------------------\n\nrecord Universe : Set\u2081 where\n field\n Codes : Set\n Meaning : Codes \u2192 Set\n\n----------------------------------------------------------------------\n\ndata DescForm (U : Universe) : Set where\n `\u22a4 `X : DescForm U\n `\u03a0 `\u03a3 : (A : Universe.Codes U)\n (D : Universe.Meaning U A \u2192 DescForm U)\n \u2192 DescForm U\n\n\u27e6_\/_\u27e7\u1d48 : (U : Universe) \u2192 DescForm U \u2192 Set \u2192 Set\n\u27e6 U \/ `\u22a4 \u27e7\u1d48 X = \u22a4\n\u27e6 U \/ `X \u27e7\u1d48 X = X\n\u27e6 U \/ `\u03a0 A D \u27e7\u1d48 X =\n (a : Universe.Meaning U A) \u2192 \u27e6 U \/ D a \u27e7\u1d48 X\n\u27e6 U \/ `\u03a3 A D \u27e7\u1d48 X =\n \u03a3 (Universe.Meaning U A) (\u03bb a \u2192 \u27e6 U \/ D a \u27e7\u1d48 X)\n\ndata \u03bc {U : Universe} (D : DescForm U) : Set where\n con : \u27e6 U \/ D \u27e7\u1d48 (\u03bc D) \u2192 \u03bc D\n\n----------------------------------------------------------------------\n\ndata TypeForm (U : Universe) : Set\n\u27e6_\/_\u27e7 : (U : Universe) \u2192 TypeForm U \u2192 Set\n\ndata TypeForm U where\n `\u22a5 `\u22a4 `Bool `\u2115 `Desc `Type : TypeForm U\n `\u03a0 `\u03a3 : (A : TypeForm U)\n (B : \u27e6 U \/ A \u27e7 \u2192 TypeForm U)\n \u2192 TypeForm U\n `\u27e6_\u27e7 : Universe.Codes U \u2192 TypeForm U\n `\u27e6_\u27e7\u1d48 : DescForm U \u2192 TypeForm U \u2192 TypeForm U\n `\u03bc : DescForm U \u2192 TypeForm U\n\n\u27e6 U \/ `\u22a5 \u27e7 = \u22a5\n\u27e6 U \/ `\u22a4 \u27e7 = \u22a4\n\u27e6 U \/ `Bool \u27e7 = Bool\n\u27e6 U \/ `\u2115 \u27e7 = \u2115\n\u27e6 U \/ `\u03a0 A B \u27e7 = (a : \u27e6 U \/ A \u27e7) \u2192 \u27e6 U \/ B a \u27e7\n\u27e6 U \/ `\u03a3 A B \u27e7 = \u03a3 \u27e6 U \/ A \u27e7 (\u03bb a \u2192 \u27e6 U \/ B a \u27e7)\n\u27e6 U \/ `Type \u27e7 = Universe.Codes U\n\u27e6 U \/ `\u27e6 A \u27e7 \u27e7 = Universe.Meaning U A\n\u27e6 U \/ `Desc \u27e7 = DescForm U\n\u27e6 U \/ `\u27e6 D \u27e7\u1d48 X \u27e7 = \u27e6 U \/ D \u27e7\u1d48 \u27e6 U \/ X \u27e7\n\u27e6 U \/ `\u03bc D \u27e7 = \u03bc D\n\n----------------------------------------------------------------------\n\n_`\u2192_ : \u2200{U} (A B : TypeForm U) \u2192 TypeForm U\nA `\u2192 B = `\u03a0 A (\u03bb _ \u2192 B)\n\nLevel : (\u2113 : \u2115) \u2192 Universe\nLevel zero = record { Codes = \u22a5 ; Meaning = \u03bb() }\nLevel (suc \u2113) = record { Codes = TypeForm (Level \u2113)\n ; Meaning = \u27e6_\/_\u27e7 (Level \u2113) }\n\nType : \u2115 \u2192 Set\nType \u2113 = TypeForm (Level \u2113)\n\nDesc : \u2115 \u2192 Set\nDesc \u2113 = DescForm (Level \u2113)\n\ninfix 0 \u27e6_\u2223_\u27e7\n\u27e6_\u2223_\u27e7 : (\u2113 : \u2115) \u2192 Type \u2113 \u2192 Set\n\u27e6 \u2113 \u2223 A \u27e7 = \u27e6 Level \u2113 \/ A \u27e7\n\n\u27e6_\u2223_\u27e7\u1d48 : (\u2113 : \u2115) \u2192 Desc \u2113 \u2192 Set \u2192 Set\n\u27e6 \u2113 \u2223 D \u27e7\u1d48 X = \u27e6 Level \u2113 \/ D \u27e7\u1d48 X\n\n----------------------------------------------------------------------\n\nelimDesc : (P : (\u2113 : \u2115) \u2192 Desc \u2113 \u2192 Set)\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `\u22a4)\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `X)\n \u2192 ((\u2113 : \u2115) (A : Type \u2113) (D : \u27e6 \u2113 \u2223 A \u27e7 \u2192 Desc (suc \u2113))\n (rec : (a : \u27e6 \u2113 \u2223 A \u27e7) \u2192 P (suc \u2113) (D a))\n \u2192 P (suc \u2113) (`\u03a0 A D))\n \u2192 ((\u2113 : \u2115) (A : Type \u2113) (D : \u27e6 \u2113 \u2223 A \u27e7 \u2192 Desc (suc \u2113))\n (rec : (a : \u27e6 \u2113 \u2223 A \u27e7) \u2192 P (suc \u2113) (D a))\n \u2192 P (suc \u2113) (`\u03a3 A D))\n \u2192 (\u2113 : \u2115) (D : Desc \u2113) \u2192 P \u2113 D\nelimDesc P p\u22a4 pX p\u03a0 p\u03a3 \u2113 `\u22a4 = p\u22a4 \u2113\nelimDesc P p\u22a4 pX p\u03a0 p\u03a3 \u2113 `X = pX \u2113\nelimDesc P p\u22a4 pX p\u03a0 p\u03a3 zero (`\u03a0 () D)\nelimDesc P p\u22a4 pX p\u03a0 p\u03a3 (suc \u2113) (`\u03a0 A D) =\n let f = elimDesc P p\u22a4 pX p\u03a0 p\u03a3 (suc \u2113)\n in p\u03a0 \u2113 A D (\u03bb a \u2192 f (D a))\nelimDesc P p\u22a4 pX p\u03a0 p\u03a3 zero (`\u03a3 () D)\nelimDesc P p\u22a4 pX p\u03a0 p\u03a3 (suc \u2113) (`\u03a3 A D) =\n let f = elimDesc P p\u22a4 pX p\u03a0 p\u03a3 (suc \u2113)\n in p\u03a3 \u2113 A D (\u03bb a \u2192 f (D a))\n\ndes : \u2200{\u2113} {D : Desc \u2113} \u2192 \u03bc D \u2192 \u27e6 \u2113 \u2223 D \u27e7\u1d48 (\u03bc D)\ndes (con x) = x\n\n----------------------------------------------------------------------\n\nelimType : (P : (\u2113 : \u2115) \u2192 Type \u2113 \u2192 Set)\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `\u22a5)\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `\u22a4)\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `Bool)\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `\u2115)\n \u2192 ((\u2113 : \u2115) (A : Type \u2113) (B : \u27e6 \u2113 \u2223 A \u27e7 \u2192 Type \u2113)\n (rec\u2081 : P \u2113 A)\n (rec\u2082 : (a : \u27e6 \u2113 \u2223 A \u27e7) \u2192 P \u2113 (B a))\n \u2192 P \u2113 (`\u03a0 A B))\n \u2192 ((\u2113 : \u2115) (A : Type \u2113) (B : \u27e6 \u2113 \u2223 A \u27e7 \u2192 Type \u2113)\n (rec\u2081 : P \u2113 A)\n (rec\u2082 : (a : \u27e6 \u2113 \u2223 A \u27e7) \u2192 P \u2113 (B a))\n \u2192 P \u2113 (`\u03a3 A B))\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `Desc)\n \u2192 ((\u2113 : \u2115) (D : Desc \u2113) (X : Type \u2113) (rec : P \u2113 X) \u2192 P \u2113 (`\u27e6 D \u27e7\u1d48 X))\n \u2192 ((\u2113 : \u2115) (D : Desc \u2113) \u2192 P \u2113 (`\u03bc D))\n \u2192 ((\u2113 : \u2115) \u2192 P \u2113 `Type)\n \u2192 ((\u2113 : \u2115) (A : Type \u2113) (rec : P \u2113 A) \u2192 P (suc \u2113) `\u27e6 A \u27e7)\n \u2192 (\u2113 : \u2115) (A : Type \u2113) \u2192 P \u2113 A\n\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 `\u22a5 = p\u22a5 \u2113\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 `\u22a4 = p\u22a4 \u2113\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 `Bool = pBool \u2113\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 `\u2115 = p\u2115 \u2113\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 (`\u03a0 A B) =\n let f = elimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7 \u2113\n in p\u03a0 \u2113 A B (f A) (\u03bb a \u2192 f (B a))\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 (`\u03a3 A B) =\n let f = elimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7 \u2113\n in p\u03a3 \u2113 A B (f A) (\u03bb a \u2192 f (B a))\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 `Type = pType \u2113\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 `Desc = pDesc \u2113\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 (`\u27e6 D \u27e7\u1d48 X) =\n let f = elimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7 \u2113\n in p\u27e6D\u27e7\u1d48 \u2113 D X (f X)\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n \u2113 (`\u03bc D) = p\u03bc \u2113 D\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n zero `\u27e6 () \u27e7\nelimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7\n (suc \u2113) `\u27e6 A \u27e7 =\n let f = elimType P p\u22a5 p\u22a4 pBool p\u2115 p\u03a0 p\u03a3 pDesc p\u27e6D\u27e7\u1d48 p\u03bc pType p\u27e6A\u27e7 \u2113\n in p\u27e6A\u27e7 \u2113 A (f A)\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"3822d110c060a75d914e9d0e4fc105f00e99980a","subject":"Atlas: fix bug in `union` by postulating conjunction","message":"Atlas: fix bug in `union` by postulating conjunction\n\nOld-commit-hash: 69ed9a83b2b162709e5dfcf541f9d0bf18c7e970\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Language\/Atlas.agda","new_file":"Syntax\/Language\/Atlas.agda","new_contents":"module Syntax.Language.Atlas where\n\n-- Base types of the calculus Atlas\n-- to be used with Plotkin-style language description\n--\n-- Atlas supports maps with neutral elements. The change type to\n-- `Map \u03ba \u03b9` is `Map \u03ba \u0394\u03b9`, where \u0394\u03b9 is the change type of the\n-- ground type \u03b9. Such a change to maps can support insertions\n-- and deletions as well: Inserting `k -> v` means mapping `k` to\n-- the change from the neutral element to `v`, and deleting\n-- `k -> v` means mapping `k` to the change from `v` to the\n-- neutral element.\n\nopen import Syntax.Language.Calculus\n\ndata Atlas-type : Set where\n Bool : Atlas-type\n Map : (\u03ba : Atlas-type) (\u03b9 : Atlas-type) \u2192 Atlas-type\n\ndata Atlas-const : Set where\n\n true : Atlas-const\n false : Atlas-const\n xor : Atlas-const\n\n empty : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n update : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n zip : \u2200 {\u03ba a b c : Atlas-type} \u2192 Atlas-const\n\nAtlas-lookup : Atlas-const \u2192 Type Atlas-type\n\nAtlas-lookup true = base Bool\nAtlas-lookup false = base Bool\nAtlas-lookup xor = base Bool \u21d2 base Bool \u21d2 base Bool\n\nAtlas-lookup (empty {\u03ba} {\u03b9}) = base (Map \u03ba \u03b9)\n\n-- `update key val my-map` would\n-- - insert if `key` is not present in `my-map`\n-- - delete if `val` is the neutral element\n-- - make an update otherwise\nAtlas-lookup (update {\u03ba} {\u03b9}) =\n base \u03ba \u21d2 base \u03b9 \u21d2 base (Map \u03ba \u03b9) \u21d2 base (Map \u03ba \u03b9)\n\n-- Model of zip = Haskell Data.List.zipWith\n-- zipWith :: (a \u2192 b \u2192 c) \u2192 [a] \u2192 [b] \u2192 [c]\nAtlas-lookup (zip {\u03ba} {a} {b} {c}) =\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u21d2\n base (Map \u03ba a) \u21d2 base (Map \u03ba b) \u21d2 base (Map \u03ba c)\n\nAtlas-\u0394base : Atlas-type \u2192 Atlas-type\n-- change to a boolean is a xor-rand\nAtlas-\u0394base Bool = Bool\n-- change to a map is change to its values\nAtlas-\u0394base (Map key val) = (Map key (Atlas-\u0394base val))\n\nAtlas-\u0394type : Type Atlas-type \u2192 Type Atlas-type\nAtlas-\u0394type = lift-\u0394type\u2080 Atlas-\u0394base\n\nAtlas-context : Set\nAtlas-context = Context {Type Atlas-type}\n\nAtlas-term : Atlas-context \u2192 Type Atlas-type \u2192 Set\nAtlas-term = Term {Atlas-type} {Atlas-const} {Atlas-lookup}\n\n-- Every base type has a known nil-change.\n-- The nil-change of \u03b9 is also the neutral element of Map \u03ba \u0394\u03b9.\n\nneutral : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const\nneutral {Bool} = false\nneutral {Map \u03ba \u03b9} = empty {\u03ba} {\u03b9}\n\nneutral-term : \u2200 {\u03b9 \u0393} \u2192 Atlas-term \u0393 (base \u03b9)\nneutral-term {Bool} = const (neutral {Bool})\nneutral-term {Map \u03ba \u03b9} = const (neutral {Map \u03ba \u03b9})\n\nnil-const : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const\nnil-const {\u03b9} = neutral {Atlas-\u0394base \u03b9}\n\nnil-term : \u2200 {\u03b9 \u0393} \u2192 Atlas-term \u0393 (base (Atlas-\u0394base \u03b9))\nnil-term {Bool} = const (nil-const {Bool})\nnil-term {Map \u03ba \u03b9} = const (nil-const {Map \u03ba \u03b9})\n\n-- Shorthands of constants\n--\n-- There's probably a uniform way to lift constants\n-- into term constructors.\n--\n-- TODO: write this and call it Syntax.Term.Plotkin.lift-term\n\nzip! : \u2200 {\u03ba a b c \u0393} \u2192\n Atlas-term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u2192\n Atlas-term \u0393 (base (Map \u03ba a)) \u2192 Atlas-term \u0393 (base (Map \u03ba b)) \u2192\n Atlas-term \u0393 (base (Map \u03ba c))\n\nzip! f m\u2081 m\u2082 = app (app (app (const zip) f) m\u2081) m\u2082\n\nlookup! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Atlas-term \u0393 (base \u03b9)\n\nlookup! = {!!} -- TODO: add constant `lookup`\n\n-- diff-term and apply-term\n\n-- b\u2080 \u229d b\u2081 = b\u2080 xor b\u2081\n-- m\u2080 \u229d m\u2081 = zip _\u229d_ m\u2080 m\u2081\n\nAtlas-diff : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 Atlas-\u0394type (base \u03b9))\nAtlas-diff {Bool} = const xor\nAtlas-diff {Map \u03ba \u03b9} = app (const zip) (abs Atlas-diff)\n\n-- b \u2295 \u0394b = b xor \u0394b\n-- m \u2295 \u0394m = zip _\u2295_ m \u0394m\n\nAtlas-apply : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\nAtlas-apply {Bool} = const xor\nAtlas-apply {Map \u03ba \u03b9} = app (const zip) (abs Atlas-apply)\n\n-- Shorthands for working with diff-term and apply-term\n\ndiff : \u2200 {\u03c4 \u0393} \u2192\n Atlas-term \u0393 \u03c4 \u2192 Atlas-term \u0393 \u03c4 \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4)\ndiff s t = app (app (lift-diff Atlas-diff Atlas-apply) s) t\n\napply : \u2200 {\u03c4 \u0393} \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4) \u2192 Atlas-term \u0393 \u03c4 \u2192\n Atlas-term \u0393 \u03c4\napply s t = app (app (lift-apply Atlas-diff Atlas-apply) s) t\n\n-- Shorthands for creating changes corresponding to\n-- insertion\/deletion.\n\nupdate! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9))\n\nupdate! k v m = app (app (app (const update) k) v) m\n\ninsert : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n -- last argument is the change accumulator\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ndelete : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ninsert k v acc = update! k (diff v neutral-term) acc\ndelete k v acc = update! k (diff neutral-term v) acc\n\n-- The binary operator such that\n-- union t t \u2261 t\n\n-- To implement union, we need for each non-map base-type\n-- an operator such that `op v v = v` on all values `v`.\n-- for Booleans, conjunction is good enough.\n--\n-- TODO (later): support conjunction, probably by Boolean\n-- elimination form if-then-else\n\npostulate\n and! : \u2200 {\u0393} \u2192\n Atlas-term \u0393 (base Bool) \u2192 Atlas-term \u0393 (base Bool) \u2192\n Atlas-term \u0393 (base Bool)\n\nunion : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03b9) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (base \u03b9)\nunion {Bool} s t = and! s t\nunion {Map \u03ba \u03b9} s t =\n let\n union-term = abs (abs (union (var (that this)) (var this)))\n in\n zip! (abs union-term) s t\n\n-- Shorthand for 4-way zip\nzip4! : \u2200 {\u03ba a b c d e \u0393} \u2192\n let\n t:_ = \u03bb \u03b9 \u2192 Atlas-term \u0393 (base \u03b9)\n in\n Atlas-term \u0393\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c \u21d2 base d \u21d2 base e) \u2192\n t: Map \u03ba a \u2192 t: Map \u03ba b \u2192 t: Map \u03ba c \u2192 t: Map \u03ba d \u2192 t: Map \u03ba e\n\nzip4! f m\u2081 m\u2082 m\u2083 m\u2084 =\n let\n v\u2081 = var (that this)\n v\u2082 = var this\n v\u2083 = var (that this)\n v\u2084 = var this\n k\u2081\u2082 = var (that (that this))\n k\u2083\u2084 = var (that (that this))\n f\u2081\u2082 = abs (abs (abs (app (app (app (app (app\n (weaken\u2083 f) k\u2081\u2082) v\u2081) v\u2082)\n (lookup! k\u2081\u2082 (weaken\u2083 m\u2083))) (lookup! k\u2081\u2082 (weaken\u2083 m\u2084)))))\n f\u2083\u2084 = abs (abs (abs (app (app (app (app (app\n (weaken\u2083 f) k\u2083\u2084)\n (lookup! k\u2083\u2084 (weaken\u2083 m\u2081)))\n (lookup! k\u2083\u2084 (weaken\u2083 m\u2082))) v\u2083) v\u2084)))\n in\n -- A correct but inefficient implementation.\n -- May want to speed it up after constants are finalized.\n union (zip! f\u2081\u2082 m\u2081 m\u2082) (zip! f\u2083\u2084 m\u2083 m\u2084)\n\n-- Type signature of Atlas-\u0394const is boilerplate.\nAtlas-\u0394const : \u2200 {\u0393} \u2192 (c : Atlas-const) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (Atlas-lookup c))\n\nAtlas-\u0394const true = const false\nAtlas-\u0394const false = const false\n\n-- \u0394xor = \u03bb x \u0394x y \u0394y \u2192 \u0394x xor \u0394y\nAtlas-\u0394const xor =\n let\n \u0394x = var (that (that this))\n \u0394y = var this\n in abs (abs (abs (abs (app (app (const xor) \u0394x) \u0394y))))\n\nAtlas-\u0394const empty = const empty\n\n-- If k \u2295 \u0394k \u2261 k, then\n-- \u0394update k \u0394k v \u0394v m \u0394m = update k \u0394v \u0394m\n-- else it is a deletion followed by insertion:\n-- \u0394update k \u0394k v \u0394v m \u0394m =\n-- insert (k \u2295 \u0394k) (v \u2295 \u0394v) (delete k v \u0394m)\n--\n-- We implement the else-branch only for the moment.\nAtlas-\u0394const update =\n let\n k = var (that (that (that (that (that this)))))\n \u0394k = var (that (that (that (that this))))\n v = var (that (that (that this)))\n \u0394v = var (that (that this))\n -- m = var (that this) -- unused parameter\n \u0394m = var this\n in\n abs (abs (abs (abs (abs (abs\n (insert (apply \u0394k k) (apply \u0394v v)\n (delete k v \u0394m)))))))\n\n-- \u0394zip f \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082 | true? (f \u2295 \u0394f \u2261 f)\n--\n-- ... | true =\n-- zip (\u03bb k \u0394v\u2081 \u0394v\u2082 \u2192 \u0394f (lookup k m\u2081) \u0394v\u2081 (lookup k m\u2082) \u0394v\u2082)\n-- \u0394m\u2081 \u0394m\u2082\n--\n-- ... | false = zip\u2084 \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082\n--\n-- we implement the false-branch for the moment.\nAtlas-\u0394const zip =\n let\n \u0394f = var (that (that (that (that this))))\n m\u2081 = var (that (that (that this)))\n \u0394m\u2081 = var (that (that this))\n m\u2082 = var (that this)\n \u0394m\u2082 = var this\n g = abs (app (app (weaken\u2081 \u0394f) (var this)) nil-term)\n in\n abs (abs (abs (abs (abs (abs (zip4! g m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082))))))\n\nAtlas = calculus-with\n Atlas-type\n Atlas-const\n Atlas-lookup\n Atlas-\u0394type\n Atlas-\u0394const\n","old_contents":"module Syntax.Language.Atlas where\n\n-- Base types of the calculus Atlas\n-- to be used with Plotkin-style language description\n--\n-- Atlas supports maps with neutral elements. The change type to\n-- `Map \u03ba \u03b9` is `Map \u03ba \u0394\u03b9`, where \u0394\u03b9 is the change type of the\n-- ground type \u03b9. Such a change to maps can support insertions\n-- and deletions as well: Inserting `k -> v` means mapping `k` to\n-- the change from the neutral element to `v`, and deleting\n-- `k -> v` means mapping `k` to the change from `v` to the\n-- neutral element.\n\nopen import Syntax.Language.Calculus\n\ndata Atlas-type : Set where\n Bool : Atlas-type\n Map : (\u03ba : Atlas-type) (\u03b9 : Atlas-type) \u2192 Atlas-type\n\ndata Atlas-const : Set where\n\n true : Atlas-const\n false : Atlas-const\n xor : Atlas-const\n\n empty : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n update : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n zip : \u2200 {\u03ba a b c : Atlas-type} \u2192 Atlas-const\n\nAtlas-lookup : Atlas-const \u2192 Type Atlas-type\n\nAtlas-lookup true = base Bool\nAtlas-lookup false = base Bool\nAtlas-lookup xor = base Bool \u21d2 base Bool \u21d2 base Bool\n\nAtlas-lookup (empty {\u03ba} {\u03b9}) = base (Map \u03ba \u03b9)\n\n-- `update key val my-map` would\n-- - insert if `key` is not present in `my-map`\n-- - delete if `val` is the neutral element\n-- - make an update otherwise\nAtlas-lookup (update {\u03ba} {\u03b9}) =\n base \u03ba \u21d2 base \u03b9 \u21d2 base (Map \u03ba \u03b9) \u21d2 base (Map \u03ba \u03b9)\n\n-- Model of zip = Haskell Data.List.zipWith\n-- zipWith :: (a \u2192 b \u2192 c) \u2192 [a] \u2192 [b] \u2192 [c]\nAtlas-lookup (zip {\u03ba} {a} {b} {c}) =\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u21d2\n base (Map \u03ba a) \u21d2 base (Map \u03ba b) \u21d2 base (Map \u03ba c)\n\nAtlas-\u0394base : Atlas-type \u2192 Atlas-type\n-- change to a boolean is a xor-rand\nAtlas-\u0394base Bool = Bool\n-- change to a map is change to its values\nAtlas-\u0394base (Map key val) = (Map key (Atlas-\u0394base val))\n\nAtlas-\u0394type : Type Atlas-type \u2192 Type Atlas-type\nAtlas-\u0394type = lift-\u0394type\u2080 Atlas-\u0394base\n\nAtlas-context : Set\nAtlas-context = Context {Type Atlas-type}\n\nAtlas-term : Atlas-context \u2192 Type Atlas-type \u2192 Set\nAtlas-term = Term {Atlas-type} {Atlas-const} {Atlas-lookup}\n\n-- Every base type has a known nil-change.\n-- The nil-change of \u03b9 is also the neutral element of Map \u03ba \u0394\u03b9.\n\nneutral : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const\nneutral {Bool} = false\nneutral {Map \u03ba \u03b9} = empty {\u03ba} {\u03b9}\n\nneutral-term : \u2200 {\u03b9 \u0393} \u2192 Atlas-term \u0393 (base \u03b9)\nneutral-term {Bool} = const (neutral {Bool})\nneutral-term {Map \u03ba \u03b9} = const (neutral {Map \u03ba \u03b9})\n\nnil-const : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const\nnil-const {\u03b9} = neutral {Atlas-\u0394base \u03b9}\n\nnil-term : \u2200 {\u03b9 \u0393} \u2192 Atlas-term \u0393 (base (Atlas-\u0394base \u03b9))\nnil-term {Bool} = const (nil-const {Bool})\nnil-term {Map \u03ba \u03b9} = const (nil-const {Map \u03ba \u03b9})\n\n-- Shorthands of constants\n--\n-- There's probably a uniform way to lift constants\n-- into term constructors.\n--\n-- TODO: write this and call it Syntax.Term.Plotkin.lift-term\n\nzip! : \u2200 {\u03ba a b c \u0393} \u2192\n Atlas-term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u2192\n Atlas-term \u0393 (base (Map \u03ba a)) \u2192 Atlas-term \u0393 (base (Map \u03ba b)) \u2192\n Atlas-term \u0393 (base (Map \u03ba c))\n\nzip! f m\u2081 m\u2082 = app (app (app (const zip) f) m\u2081) m\u2082\n\nlookup! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Atlas-term \u0393 (base \u03b9)\n\nlookup! = {!!} -- TODO: add constant `lookup`\n\n-- diff-term and apply-term\n\n-- b\u2080 \u229d b\u2081 = b\u2080 xor b\u2081\n-- m\u2080 \u229d m\u2081 = zip _\u229d_ m\u2080 m\u2081\n\nAtlas-diff : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 Atlas-\u0394type (base \u03b9))\nAtlas-diff {Bool} = const xor\nAtlas-diff {Map \u03ba \u03b9} = app (const zip) (abs Atlas-diff)\n\n-- b \u2295 \u0394b = b xor \u0394b\n-- m \u2295 \u0394m = zip _\u2295_ m \u0394m\n\nAtlas-apply : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\nAtlas-apply {Bool} = const xor\nAtlas-apply {Map \u03ba \u03b9} = app (const zip) (abs Atlas-apply)\n\n-- Shorthands for working with diff-term and apply-term\n\ndiff : \u2200 {\u03c4 \u0393} \u2192\n Atlas-term \u0393 \u03c4 \u2192 Atlas-term \u0393 \u03c4 \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4)\ndiff s t = app (app (lift-diff Atlas-diff Atlas-apply) s) t\n\napply : \u2200 {\u03c4 \u0393} \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4) \u2192 Atlas-term \u0393 \u03c4 \u2192\n Atlas-term \u0393 \u03c4\napply s t = app (app (lift-apply Atlas-diff Atlas-apply) s) t\n\n-- Shorthands for creating changes corresponding to\n-- insertion\/deletion.\n\nupdate! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9))\n\nupdate! k v m = app (app (app (const update) k) v) m\n\ninsert : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n -- last argument is the change accumulator\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ndelete : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ninsert k v acc = update! k (diff v neutral-term) acc\ndelete k v acc = update! k (diff neutral-term v) acc\n\n-- The binary operator with which all base-type values\n-- form a group\n\nunion : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03b9) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (base \u03b9)\nunion {Bool} s t = app (app (const xor) s) t\nunion {Map \u03ba \u03b9} s t =\n let\n union-term = abs (abs (union (var (that this)) (var this)))\n in\n zip! (abs union-term) s t\n\n-- Shorthand for 4-way zip\nzip4! : \u2200 {\u03ba a b c d e \u0393} \u2192\n let\n t:_ = \u03bb \u03b9 \u2192 Atlas-term \u0393 (base \u03b9)\n in\n Atlas-term \u0393\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c \u21d2 base d \u21d2 base e) \u2192\n t: Map \u03ba a \u2192 t: Map \u03ba b \u2192 t: Map \u03ba c \u2192 t: Map \u03ba d \u2192 t: Map \u03ba e\n\nzip4! f m\u2081 m\u2082 m\u2083 m\u2084 =\n let\n v\u2081 = var (that this)\n v\u2082 = var this\n v\u2083 = var (that this)\n v\u2084 = var this\n k\u2081\u2082 = var (that (that this))\n k\u2083\u2084 = var (that (that this))\n f\u2081\u2082 = abs (abs (abs (app (app (app (app (app\n (weaken\u2083 f) k\u2081\u2082) v\u2081) v\u2082)\n (lookup! k\u2081\u2082 (weaken\u2083 m\u2083))) (lookup! k\u2081\u2082 (weaken\u2083 m\u2084)))))\n f\u2083\u2084 = abs (abs (abs (app (app (app (app (app\n (weaken\u2083 f) k\u2083\u2084)\n (lookup! k\u2083\u2084 (weaken\u2083 m\u2081)))\n (lookup! k\u2083\u2084 (weaken\u2083 m\u2082))) v\u2083) v\u2084)))\n in\n -- A correct but inefficient implementation.\n -- May want to speed it up after constants are finalized.\n union (zip! f\u2081\u2082 m\u2081 m\u2082) (zip! f\u2083\u2084 m\u2083 m\u2084)\n\n-- Type signature of Atlas-\u0394const is boilerplate.\nAtlas-\u0394const : \u2200 {\u0393} \u2192 (c : Atlas-const) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (Atlas-lookup c))\n\nAtlas-\u0394const true = const false\nAtlas-\u0394const false = const false\n\n-- \u0394xor = \u03bb x \u0394x y \u0394y \u2192 \u0394x xor \u0394y\nAtlas-\u0394const xor =\n let\n \u0394x = var (that (that this))\n \u0394y = var this\n in abs (abs (abs (abs (app (app (const xor) \u0394x) \u0394y))))\n\nAtlas-\u0394const empty = const empty\n\n-- If k \u2295 \u0394k \u2261 k, then\n-- \u0394update k \u0394k v \u0394v m \u0394m = update k \u0394v \u0394m\n-- else it is a deletion followed by insertion:\n-- \u0394update k \u0394k v \u0394v m \u0394m =\n-- insert (k \u2295 \u0394k) (v \u2295 \u0394v) (delete k v \u0394m)\n--\n-- We implement the else-branch only for the moment.\nAtlas-\u0394const update =\n let\n k = var (that (that (that (that (that this)))))\n \u0394k = var (that (that (that (that this))))\n v = var (that (that (that this)))\n \u0394v = var (that (that this))\n -- m = var (that this) -- unused parameter\n \u0394m = var this\n in\n abs (abs (abs (abs (abs (abs\n (insert (apply \u0394k k) (apply \u0394v v)\n (delete k v \u0394m)))))))\n\n-- \u0394zip f \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082 | true? (f \u2295 \u0394f \u2261 f)\n--\n-- ... | true =\n-- zip (\u03bb k \u0394v\u2081 \u0394v\u2082 \u2192 \u0394f (lookup k m\u2081) \u0394v\u2081 (lookup k m\u2082) \u0394v\u2082)\n-- \u0394m\u2081 \u0394m\u2082\n--\n-- ... | false = zip\u2084 \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082\n--\n-- we implement the false-branch for the moment.\nAtlas-\u0394const zip =\n let\n \u0394f = var (that (that (that (that this))))\n m\u2081 = var (that (that (that this)))\n \u0394m\u2081 = var (that (that this))\n m\u2082 = var (that this)\n \u0394m\u2082 = var this\n g = abs (app (app (weaken\u2081 \u0394f) (var this)) nil-term)\n in\n abs (abs (abs (abs (abs (abs (zip4! g m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082))))))\n\nAtlas = calculus-with\n Atlas-type\n Atlas-const\n Atlas-lookup\n Atlas-\u0394type\n Atlas-\u0394const\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ced202e3f87365cfeb07406d81b0480bc81320bf","subject":"Sum: untag","message":"Sum: untag\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Sum\/NP.agda","new_file":"lib\/Data\/Sum\/NP.agda","new_contents":"-- NOTE with-K\nmodule Data.Sum.NP where\n\nopen import Data.Sum public renaming (inj\u2081 to inl; inj\u2082 to inr)\n\nopen import Type hiding (\u2605)\nopen import Level.NP\nopen import Function\nopen import Data.Nat using (\u2115; zero; suc)\nopen import Data.Zero\nopen import Data.One\nopen import Relation.Binary\nopen import Relation.Binary.Logical\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_;_\u2262_;_\u2257_)\nopen import Data.Two hiding (twist)\nopen \u2261 using (\u2192-to-\u27f6)\n\n[inl:_,inr:_] = [_,_]\n\ninl-inj : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} {x y : A} \u2192 inl {B = B} x \u2261 inl y \u2192 x \u2261 y\ninl-inj \u2261.refl = \u2261.refl\n\ninr-inj : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} {x y : B} \u2192 inr {A = A} x \u2261 inr y \u2192 x \u2261 y\ninr-inj \u2261.refl = \u2261.refl\n\nmodule _ {a} {A : \u2605 a} where\n untag : A \u228e A \u2192 A\n untag = [inl: id ,inr: id ]\n\nmodule _ {a\u2081 a\u2082 b\u2081 b\u2082}\n {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082}\n {c} {C : \u2605 c} (f : A\u2081 \u228e B\u2081 \u2192 A\u2082 \u228e B\u2082 \u2192 C) where\n on-inl = \u03bb i j \u2192 f (inl i) (inl j)\n on-inr = \u03bb i j \u2192 f (inr i) (inr j)\n\n[,]-assoc : \u2200 {a\u2081 a\u2082 b\u2081 b\u2082 c} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082} {C : \u2605 c}\n {f\u2081 : B\u2081 \u2192 C} {g\u2081 : A\u2081 \u2192 B\u2081} {f\u2082 : B\u2082 \u2192 C} {g\u2082 : A\u2082 \u2192 B\u2082} \u2192\n [ f\u2081 , f\u2082 ] \u2218\u2032 map g\u2081 g\u2082 \u2257 [ f\u2081 \u2218 g\u2081 , f\u2082 \u2218 g\u2082 ]\n[,]-assoc (inl _) = \u2261.refl\n[,]-assoc (inr _) = \u2261.refl\n\n[,]-factor : \u2200 {a\u2081 a\u2082 b c} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} {B : \u2605 b} {C : \u2605 c}\n {f : B \u2192 C} {g\u2081 : A\u2081 \u2192 B} {g\u2082 : A\u2082 \u2192 B} \u2192\n [ f \u2218 g\u2081 , f \u2218 g\u2082 ] \u2257 f \u2218 [ g\u2081 , g\u2082 ]\n[,]-factor (inl _) = \u2261.refl\n[,]-factor (inr _) = \u2261.refl\n\nmap-assoc : \u2200 {a\u2081 a\u2082 b\u2081 b\u2082 c\u2081 c\u2082} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082} {C\u2081 : \u2605 c\u2081} {C\u2082 : \u2605 c\u2082}\n {f\u2081 : B\u2081 \u2192 C\u2081} {g\u2081 : A\u2081 \u2192 B\u2081} {f\u2082 : B\u2082 \u2192 C\u2082} {g\u2082 : A\u2082 \u2192 B\u2082} \u2192\n map f\u2081 f\u2082 \u2218\u2032 map g\u2081 g\u2082 \u2257 map (f\u2081 \u2218 g\u2081) (f\u2082 \u2218 g\u2082)\nmap-assoc = [,]-assoc\n\nopen import Data.Product\nopen import Function.Inverse\nopen import Function.LeftInverse\n\n{- bad names\n\u228e-fst : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 A \u228e B \u2192 \ud835\udfda\n\u228e-fst (inl _) = 0\u2082\n\u228e-fst (inr _) = 1\u2082\n\n\u228e-snd : \u2200 {\u2113} {A B : \u2605 \u2113} (x : A \u228e B) \u2192 case \u228e-fst x 0: A 1: B\n\u228e-snd (inl x) = x\n\u228e-snd (inr x) = x\n-}\n\n-- Function.Related.TypeIsomorphisms.NP for the A \u228e B, \u03a3 \ud835\udfda [0: A 1: B ] iso.\n\n\ud835\udfd9\u228e^ : \u2115 \u2192 \u2605\u2080\n\ud835\udfd9\u228e^ zero = \ud835\udfd8\n\ud835\udfd9\u228e^ (suc n) = \ud835\udfd9 \u228e \ud835\udfd9\u228e^ n\n\nmodule _ {a b} {A : \u2605 a} {B : \u2605 b} where\n twist : A \u228e B \u2192 B \u228e A\n twist = [inl: inr ,inr: inl ]\n","old_contents":"-- NOTE with-K\nmodule Data.Sum.NP where\n\nopen import Data.Sum public renaming (inj\u2081 to inl; inj\u2082 to inr)\n\nopen import Type hiding (\u2605)\nopen import Level.NP\nopen import Function\nopen import Data.Nat using (\u2115; zero; suc)\nopen import Data.Zero\nopen import Data.One\nopen import Relation.Binary\nopen import Relation.Binary.Logical\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_;_\u2262_;_\u2257_)\nopen import Data.Two hiding (twist)\nopen \u2261 using (\u2192-to-\u27f6)\n\n[inl:_,inr:_] = [_,_]\n\ninl-inj : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} {x y : A} \u2192 inl {B = B} x \u2261 inl y \u2192 x \u2261 y\ninl-inj \u2261.refl = \u2261.refl\n\ninr-inj : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} {x y : B} \u2192 inr {A = A} x \u2261 inr y \u2192 x \u2261 y\ninr-inj \u2261.refl = \u2261.refl\n\nmodule _ {a\u2081 a\u2082 b\u2081 b\u2082}\n {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082}\n {c} {C : \u2605 c} (f : A\u2081 \u228e B\u2081 \u2192 A\u2082 \u228e B\u2082 \u2192 C) where\n on-inl = \u03bb i j \u2192 f (inl i) (inl j)\n on-inr = \u03bb i j \u2192 f (inr i) (inr j)\n\n[,]-assoc : \u2200 {a\u2081 a\u2082 b\u2081 b\u2082 c} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082} {C : \u2605 c}\n {f\u2081 : B\u2081 \u2192 C} {g\u2081 : A\u2081 \u2192 B\u2081} {f\u2082 : B\u2082 \u2192 C} {g\u2082 : A\u2082 \u2192 B\u2082} \u2192\n [ f\u2081 , f\u2082 ] \u2218\u2032 map g\u2081 g\u2082 \u2257 [ f\u2081 \u2218 g\u2081 , f\u2082 \u2218 g\u2082 ]\n[,]-assoc (inl _) = \u2261.refl\n[,]-assoc (inr _) = \u2261.refl\n\n[,]-factor : \u2200 {a\u2081 a\u2082 b c} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} {B : \u2605 b} {C : \u2605 c}\n {f : B \u2192 C} {g\u2081 : A\u2081 \u2192 B} {g\u2082 : A\u2082 \u2192 B} \u2192\n [ f \u2218 g\u2081 , f \u2218 g\u2082 ] \u2257 f \u2218 [ g\u2081 , g\u2082 ]\n[,]-factor (inl _) = \u2261.refl\n[,]-factor (inr _) = \u2261.refl\n\nmap-assoc : \u2200 {a\u2081 a\u2082 b\u2081 b\u2082 c\u2081 c\u2082} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082} {C\u2081 : \u2605 c\u2081} {C\u2082 : \u2605 c\u2082}\n {f\u2081 : B\u2081 \u2192 C\u2081} {g\u2081 : A\u2081 \u2192 B\u2081} {f\u2082 : B\u2082 \u2192 C\u2082} {g\u2082 : A\u2082 \u2192 B\u2082} \u2192\n map f\u2081 f\u2082 \u2218\u2032 map g\u2081 g\u2082 \u2257 map (f\u2081 \u2218 g\u2081) (f\u2082 \u2218 g\u2082)\nmap-assoc = [,]-assoc\n\nopen import Data.Product\nopen import Function.Inverse\nopen import Function.LeftInverse\n\n{- bad names\n\u228e-fst : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 A \u228e B \u2192 \ud835\udfda\n\u228e-fst (inl _) = 0\u2082\n\u228e-fst (inr _) = 1\u2082\n\n\u228e-snd : \u2200 {\u2113} {A B : \u2605 \u2113} (x : A \u228e B) \u2192 case \u228e-fst x 0: A 1: B\n\u228e-snd (inl x) = x\n\u228e-snd (inr x) = x\n-}\n\n-- Function.Related.TypeIsomorphisms.NP for the A \u228e B, \u03a3 \ud835\udfda [0: A 1: B ] iso.\n\n\ud835\udfd9\u228e^ : \u2115 \u2192 \u2605\u2080\n\ud835\udfd9\u228e^ zero = \ud835\udfd8\n\ud835\udfd9\u228e^ (suc n) = \ud835\udfd9 \u228e \ud835\udfd9\u228e^ n\n\nmodule _ {a b} {A : \u2605 a} {B : \u2605 b} where\n twist : A \u228e B \u2192 B \u228e A\n twist = [inl: inr ,inr: inl ]\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"23cf45d8db2b7852d5a8c6726d7534b076de6c92","subject":"Iso attepmt for fin","message":"Iso attepmt for fin\n","repos":"crypto-agda\/explore","old_file":"lib\/Explore\/Fin.agda","new_file":"lib\/Explore\/Fin.agda","new_contents":"{-# OPTIONS --without-K #-}\n\nopen import Level.NP\nopen import Type\nopen import Function\nopen import Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Zero\nopen import Data.One\nopen import Data.Nat\nopen import Data.Product\nopen import Data.Sum\nopen import Function.Related.TypeIsomorphisms.NP\nimport Function.Inverse.NP as Inv\nopen Inv using (_\u2194_; sym; id; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nopen import Relation.Binary.NP\nopen import Explore.Type\nopen import Explore.Explorable\nimport Explore.Isomorphism as Iso\n--open import Explore.Explorable.Maybe\n\nmodule Explore.Fin where\n\n\ud835\udfd9\u228e^ : \u2115 \u2192 \u2605\u2080\n\ud835\udfd9\u228e^ zero = \ud835\udfd8\n\ud835\udfd9\u228e^ (suc n) = \ud835\udfd9 \u228e \ud835\udfd9\u228e^ n\n\nFin\u2194\ud835\udfd9\u228e^\ud835\udfd8 : \u2200 n \u2192 Fin n \u2194 \ud835\udfd9\u228e^ n\nFin\u2194\ud835\udfd9\u228e^\ud835\udfd8 zero = Fin0\u2194\ud835\udfd8\nFin\u2194\ud835\udfd9\u228e^\ud835\udfd8 (suc n) = {!{!Inv.id!} \u228e-cong ?!} Inv.\u2218 Fin\u2218suc\u2194\ud835\udfd9\u228eFin\n\n{-\nMaybe^\ud835\udfd8\u2194Fin\nopen Iso (Fin\n\n{-\nmodule _ {\u2113} where\n Fin\u1d49 : \u2200 n \u2192 Explore \u2113 (Fin n)\n Fin\u1d49 zero z _\u2219_ f = z\n Fin\u1d49 (suc n) z _\u2219_ f = f zero \u2219 Fin\u1d49 n z _\u2219_ (f \u2218 suc)\n\n Fin\u2071 : \u2200 {p} n \u2192 ExploreInd p (Fin\u1d49 n)\n Fin\u2071 zero P z _\u2219_ f = z\n Fin\u2071 (suc n) P z _\u2219_ f = f zero \u2219 Fin\u2071 n Psuc z _\u2219_ (f \u2218 suc)\n where Psuc = \u03bb e \u2192 P (\u03bb z op f \u2192 e z op (f \u2218 suc))\n\nmodule _ {\u2113} where\n Fin\u02e1 : \u2200 n \u2192 Lookup {\u2113} (Fin\u1d49 n)\n Fin\u02e1 (suc _) (b , _) zero = b\n Fin\u02e1 (suc n) (_ , xs) (suc x) = Fin\u02e1 n xs x\n\n Fin\u1da0 : \u2200 n \u2192 Focus {\u2113} (Fin\u1d49 n)\n Fin\u1da0 (suc n) (zero , b) = inj\u2081 b\n Fin\u1da0 (suc n) (suc x , b) = inj\u2082 (Fin\u1da0 n (x , b))\n\nmodule _ n where\n open Explorable\u2080 (Fin\u2071 n) public using () renaming (sum to Fin\u02e2; product to Fin\u1d56)\n open Explorable\u2081\u2080 (Fin\u2071 n) public using () renaming (reify to Fin\u02b3)\n open Explorable\u2081\u2081 (Fin\u2071 n) public using () renaming (unfocus to Fin\u1d58)\n\npostulate\n Postulate-Fin\u02e2-ok : \u2605\n Postulate-FinFun\u02e2-ok : \u2605\n\n Fin\u02e2-ok : \u2200 {{_ : Postulate-Fin\u02e2-ok}} n \u2192 AdequateSum (Fin\u02e2 n)\n\n{-\nFin\u1d56-ok : \u2200 n \u2192 AdequateProduct (Fin\u1d56 n)\n-}\n\nmodule _ {A : \u2605}(\u03bcA : Explorable A) where\n\n e\u1d2c = explore \u03bcA\n\n extend : \u2200 {n} \u2192 A \u2192 (Fin n \u2192 A) \u2192 Fin (suc n) \u2192 A\n extend x g zero = x\n extend x g (suc i) = g i\n\n \u00acFin0 : Fin 0 \u2192 A\n \u00acFin0 ()\n\n -- There is one function Fin 0 \u2192 A (called abs) so this should be fine\n -- if not there is a version below that forces the domain to be non-empty\n FinFun\u1d49 : \u2200 n \u2192 Explore _ (Fin n \u2192 A)\n FinFun\u1d49 zero z op f = f \u00acFin0\n FinFun\u1d49 (suc n) z op f = e\u1d2c z op (\u03bb x \u2192 FinFun\u1d49 n z op (f \u2218 extend x))\n\n FinFun\u2071 : \u2200 n \u2192 ExploreInd _ (FinFun\u1d49 n)\n FinFun\u2071 zero P Pz P\u2219 Pf = Pf _\n FinFun\u2071 (suc n) P Pz P\u2219 Pf =\n explore-ind \u03bcA (\u03bb sa \u2192 P (\u03bb z op f \u2192 sa z op (\u03bb x \u2192 FinFun\u1d49 n z op (f \u2218 extend x))))\n Pz P\u2219\n (\u03bb x \u2192 FinFun\u2071 n (\u03bb sf \u2192 P (\u03bb z op f \u2192 sf z op (f \u2218 extend x)))\n Pz P\u2219 (Pf \u2218 extend x))\n\n FinFun\u02e2 : \u2200 n \u2192 Sum (Fin n \u2192 A)\n FinFun\u02e2 n = FinFun\u1d49 n 0 _+_\n\n postulate\n FinFun\u02e2-ok : \u2200 {{_ : Postulate-FinFun\u02e2-ok}} n \u2192 AdequateSum (FinFun\u02e2 n)\n\n \u03bcFinFun : \u2200 {{_ : Postulate-FinFun\u02e2-ok}} {n} \u2192 Explorable (Fin n \u2192 A)\n \u03bcFinFun = mk _ (FinFun\u2071 _) (FinFun\u02e2-ok _)\n\n\u03bcFin : \u2200 {{_ : Postulate-Fin\u02e2-ok}} n \u2192 Explorable (Fin n)\n\u03bcFin n = mk _ (Fin\u2071 n) (Fin\u02e2-ok n)\n\n{-\n\u03bcFinSUI : \u2200 {n} \u2192 SumStableUnderInjection (sum (\u03bcFin n))\n-}\n\nmodule BigDistr\n {{_ : Postulate-Fin\u02e2-ok}}\n {{_ : Postulate-FinFun\u02e2-ok}}\n {A : \u2605}(\u03bcA : Explorable A)\n (cm : CommutativeMonoid \u2080 \u2080)\n -- we want (open CMon cm) !!!\n (_\u25ce_ : let open CMon cm in C \u2192 C \u2192 C)\n (\u03b5-\u25ce : let open CMon cm in Zero _\u2248_ \u03b5 _\u25ce_)\n (distrib : let open CMon cm in _DistributesOver_ _\u2248_ _\u25ce_ _\u2219_)\n (_\u25ce-cong_ : let open CMon cm in _\u25ce_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_) where\n\n open CMon cm renaming (sym to \u2248-sym)\n\n \u03bcF\u2192A = \u03bcFinFun \u03bcA\n\n -- Sum over A\n \u03a3\u1d2c = explore \u03bcA \u03b5 _\u2219_\n\n -- Sum over (Fin(1+I)\u2192A) functions\n \u03a3' : \u2200 {I} \u2192 ((Fin I \u2192 A) \u2192 C) \u2192 C\n \u03a3' = explore \u03bcF\u2192A \u03b5 _\u2219_\n\n -- Product over Fin(1+I) values\n \u03a0' = \u03bb I \u2192 explore (\u03bcFin I) \u03b5 _\u25ce_\n\n bigDistr : \u2200 I F \u2192 \u03a0' I (\u03a3\u1d2c \u2218 F) \u2248 \u03a3' (\u03a0' I \u2218 _\u02e2_ F)\n bigDistr zero _ = refl\n bigDistr (suc I) F\n = \u03a3\u1d2c (F zero) \u25ce \u03a0' I (\u03a3\u1d2c \u2218 F \u2218 suc)\n \u2248\u27e8 refl \u25ce-cong bigDistr I (F \u2218 suc) \u27e9\n \u03a3\u1d2c (F zero) \u25ce \u03a3' (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc))\n \u2248\u27e8 \u2248-sym (explore-lin\u02b3 \u03bcA monoid _\u25ce_ (F zero) (\u03a3' (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc))) (proj\u2081 \u03b5-\u25ce _) (proj\u2082 distrib)) \u27e9\n \u03a3\u1d2c (\u03bb j \u2192 F zero j \u25ce \u03a3' (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc)))\n \u2248\u27e8 explore-mon-ext \u03bcA monoid (\u03bb j \u2192 \u2248-sym (explore-lin\u02e1 \u03bcF\u2192A monoid _\u25ce_ (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc)) (F zero j) (proj\u2082 \u03b5-\u25ce _) (proj\u2081 distrib))) \u27e9\n (\u03a3\u1d2c \u03bb j \u2192 \u03a3' \u03bb f \u2192 F zero j \u25ce \u03a0' I ((F \u2218 suc) \u02e2 f))\n \u220e\n\nmodule _\n {{_ : Postulate-Fin\u02e2-ok}}\n {{_ : Postulate-FinFun\u02e2-ok}} where\n\n FinDist : \u2200 {n} \u2192 DistFun (\u03bcFin n) (\u03bb \u03bcX \u2192 \u03bcFinFun \u03bcX)\n FinDist \u03bcB c \u25ce distrib \u25ce-cong f = BigDistr.bigDistr \u03bcB c \u25ce distrib \u25ce-cong f _\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\n\nopen import Level.NP\nopen import Type\nopen import Function\nopen import Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Nat\nopen import Data.Product\nopen import Data.Sum\nopen import Function.Related.TypeIsomorphisms.NP\nimport Function.Inverse.NP as Inv\nopen Inv using (_\u2194_; sym; id; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nopen import Relation.Binary.NP\nopen import Explore.Type\nopen import Explore.Explorable\nopen import Explore.Isomorphism\n--open import Explore.Explorable.Maybe\n\nmodule Explore.Fin where\n\nmodule _ {\u2113} where\n Fin\u1d49 : \u2200 n \u2192 Explore \u2113 (Fin n)\n Fin\u1d49 zero z _\u2219_ f = z\n Fin\u1d49 (suc n) z _\u2219_ f = f zero \u2219 Fin\u1d49 n z _\u2219_ (f \u2218 suc)\n\n Fin\u2071 : \u2200 {p} n \u2192 ExploreInd p (Fin\u1d49 n)\n Fin\u2071 zero P z _\u2219_ f = z\n Fin\u2071 (suc n) P z _\u2219_ f = f zero \u2219 Fin\u2071 n Psuc z _\u2219_ (f \u2218 suc)\n where Psuc = \u03bb e \u2192 P (\u03bb z op f \u2192 e z op (f \u2218 suc))\n\nmodule _ {\u2113} where\n Fin\u02e1 : \u2200 n \u2192 Lookup {\u2113} (Fin\u1d49 n)\n Fin\u02e1 (suc _) (b , _) zero = b\n Fin\u02e1 (suc n) (_ , xs) (suc x) = Fin\u02e1 n xs x\n\n Fin\u1da0 : \u2200 n \u2192 Focus {\u2113} (Fin\u1d49 n)\n Fin\u1da0 (suc n) (zero , b) = inj\u2081 b\n Fin\u1da0 (suc n) (suc x , b) = inj\u2082 (Fin\u1da0 n (x , b))\n\nmodule _ n where\n open Explorable\u2080 (Fin\u2071 n) public using () renaming (sum to Fin\u02e2; product to Fin\u1d56)\n open Explorable\u2081\u2080 (Fin\u2071 n) public using () renaming (reify to Fin\u02b3)\n open Explorable\u2081\u2081 (Fin\u2071 n) public using () renaming (unfocus to Fin\u1d58)\n\npostulate\n Postulate-Fin\u02e2-ok : \u2605\n Postulate-FinFun\u02e2-ok : \u2605\n\n Fin\u02e2-ok : \u2200 {{_ : Postulate-Fin\u02e2-ok}} n \u2192 AdequateSum (Fin\u02e2 n)\n\n{-\nFin\u1d56-ok : \u2200 n \u2192 AdequateProduct (Fin\u1d56 n)\n-}\n\nmodule _ {A : \u2605}(\u03bcA : Explorable A) where\n\n e\u1d2c = explore \u03bcA\n\n extend : \u2200 {n} \u2192 A \u2192 (Fin n \u2192 A) \u2192 Fin (suc n) \u2192 A\n extend x g zero = x\n extend x g (suc i) = g i\n\n \u00acFin0 : Fin 0 \u2192 A\n \u00acFin0 ()\n\n -- There is one function Fin 0 \u2192 A (called abs) so this should be fine\n -- if not there is a version below that forces the domain to be non-empty\n FinFun\u1d49 : \u2200 n \u2192 Explore _ (Fin n \u2192 A)\n FinFun\u1d49 zero z op f = f \u00acFin0\n FinFun\u1d49 (suc n) z op f = e\u1d2c z op (\u03bb x \u2192 FinFun\u1d49 n z op (f \u2218 extend x))\n\n FinFun\u2071 : \u2200 n \u2192 ExploreInd _ (FinFun\u1d49 n)\n FinFun\u2071 zero P Pz P\u2219 Pf = Pf _\n FinFun\u2071 (suc n) P Pz P\u2219 Pf =\n explore-ind \u03bcA (\u03bb sa \u2192 P (\u03bb z op f \u2192 sa z op (\u03bb x \u2192 FinFun\u1d49 n z op (f \u2218 extend x))))\n Pz P\u2219\n (\u03bb x \u2192 FinFun\u2071 n (\u03bb sf \u2192 P (\u03bb z op f \u2192 sf z op (f \u2218 extend x)))\n Pz P\u2219 (Pf \u2218 extend x))\n\n FinFun\u02e2 : \u2200 n \u2192 Sum (Fin n \u2192 A)\n FinFun\u02e2 n = FinFun\u1d49 n 0 _+_\n\n postulate\n FinFun\u02e2-ok : \u2200 {{_ : Postulate-FinFun\u02e2-ok}} n \u2192 AdequateSum (FinFun\u02e2 n)\n\n \u03bcFinFun : \u2200 {{_ : Postulate-FinFun\u02e2-ok}} {n} \u2192 Explorable (Fin n \u2192 A)\n \u03bcFinFun = mk _ (FinFun\u2071 _) (FinFun\u02e2-ok _)\n\n\u03bcFin : \u2200 {{_ : Postulate-Fin\u02e2-ok}} n \u2192 Explorable (Fin n)\n\u03bcFin n = mk _ (Fin\u2071 n) (Fin\u02e2-ok n)\n\n{-\n\u03bcFinSUI : \u2200 {n} \u2192 SumStableUnderInjection (sum (\u03bcFin n))\n-}\n\nmodule BigDistr\n {{_ : Postulate-Fin\u02e2-ok}}\n {{_ : Postulate-FinFun\u02e2-ok}}\n {A : \u2605}(\u03bcA : Explorable A)\n (cm : CommutativeMonoid \u2080 \u2080)\n -- we want (open CMon cm) !!!\n (_\u25ce_ : let open CMon cm in C \u2192 C \u2192 C)\n (\u03b5-\u25ce : let open CMon cm in Zero _\u2248_ \u03b5 _\u25ce_)\n (distrib : let open CMon cm in _DistributesOver_ _\u2248_ _\u25ce_ _\u2219_)\n (_\u25ce-cong_ : let open CMon cm in _\u25ce_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_) where\n\n open CMon cm renaming (sym to \u2248-sym)\n\n \u03bcF\u2192A = \u03bcFinFun \u03bcA\n\n -- Sum over A\n \u03a3\u1d2c = explore \u03bcA \u03b5 _\u2219_\n\n -- Sum over (Fin(1+I)\u2192A) functions\n \u03a3' : \u2200 {I} \u2192 ((Fin I \u2192 A) \u2192 C) \u2192 C\n \u03a3' = explore \u03bcF\u2192A \u03b5 _\u2219_\n\n -- Product over Fin(1+I) values\n \u03a0' = \u03bb I \u2192 explore (\u03bcFin I) \u03b5 _\u25ce_\n\n bigDistr : \u2200 I F \u2192 \u03a0' I (\u03a3\u1d2c \u2218 F) \u2248 \u03a3' (\u03a0' I \u2218 _\u02e2_ F)\n bigDistr zero _ = refl\n bigDistr (suc I) F\n = \u03a3\u1d2c (F zero) \u25ce \u03a0' I (\u03a3\u1d2c \u2218 F \u2218 suc)\n \u2248\u27e8 refl \u25ce-cong bigDistr I (F \u2218 suc) \u27e9\n \u03a3\u1d2c (F zero) \u25ce \u03a3' (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc))\n \u2248\u27e8 \u2248-sym (explore-lin\u02b3 \u03bcA monoid _\u25ce_ (F zero) (\u03a3' (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc))) (proj\u2081 \u03b5-\u25ce _) (proj\u2082 distrib)) \u27e9\n \u03a3\u1d2c (\u03bb j \u2192 F zero j \u25ce \u03a3' (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc)))\n \u2248\u27e8 explore-mon-ext \u03bcA monoid (\u03bb j \u2192 \u2248-sym (explore-lin\u02e1 \u03bcF\u2192A monoid _\u25ce_ (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc)) (F zero j) (proj\u2082 \u03b5-\u25ce _) (proj\u2081 distrib))) \u27e9\n (\u03a3\u1d2c \u03bb j \u2192 \u03a3' \u03bb f \u2192 F zero j \u25ce \u03a0' I ((F \u2218 suc) \u02e2 f))\n \u220e\n\nmodule _\n {{_ : Postulate-Fin\u02e2-ok}}\n {{_ : Postulate-FinFun\u02e2-ok}} where\n\n FinDist : \u2200 {n} \u2192 DistFun (\u03bcFin n) (\u03bb \u03bcX \u2192 \u03bcFinFun \u03bcX)\n FinDist \u03bcB c \u25ce distrib \u25ce-cong f = BigDistr.bigDistr \u03bcB c \u25ce distrib \u25ce-cong f _\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"12687bc02ef41455749e8da3f343fe679cd80336","subject":"adding new files to the \"\"\"cm file\"\"\"","message":"adding new files to the \"\"\"cm file\"\"\"\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"all.agda","new_file":"all.agda","new_contents":"open import List\nopen import Nat\nopen import Prelude\n\nopen import core\n-- open import correspondence\n-- open import expandability\n-- open import expansion-unicity\n-- open import preservation\n-- open import progress\n-- open import type-assignment-unicity\n-- open import typed-expansion\n","old_contents":"open import List\nopen import Nat\nopen import Prelude\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"87c6c3bc67648f23a50338540793cd6b1bd51f0c","subject":"scraps","message":"scraps\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"all.agda","new_file":"all.agda","new_contents":"open import List\nopen import Nat\nopen import Prelude\n\nopen import contexts\nopen import core\n\nopen import lemmas-gcomplete\n\nopen import lemmas-disjointness\n--open import disjointness -- working here\n\nopen import structural-assumptions\n\nopen import focus-formation\nopen import ground-decidable\nopen import matched-ground-invariant\nopen import finality\n\n--open import lemmas-subst-ta -- working here\nopen import lemmas-ground\nopen import lemmas-consistency\nopen import lemmas-matching\nopen import synth-unicity\nopen import htype-decidable\n\n--open import expansion-generality -- depend on disjointness\n--open import expandability -- ditto\nopen import expansion-unicity\nopen import type-assignment-unicity\nopen import typed-expansion\n\nopen import canonical-value-forms\nopen import canonical-boxed-forms\nopen import canonical-indeterminate-forms\n\nopen import lemmas-progress-checks\nopen import progress-checks\nopen import progress\nopen import preservation\n\nopen import lemmas-complete\nopen import cast-inert\nopen import complete-preservation\nopen import complete-progress\nopen import complete-expansion\n\nopen import weakening\nopen import exchange\nopen import lemmas-freshness\nopen import contraction\n","old_contents":"open import List\nopen import Nat\nopen import Prelude\n\nopen import contexts\nopen import core\n\nopen import lemmas-gcomplete\n\nopen import lemmas-disjointness\nopen import disjointness -- work here\n\nopen import structural-assumptions\n\nopen import focus-formation\nopen import ground-decidable\nopen import matched-ground-invariant\nopen import finality\n\nopen import lemmas-subst-ta -- work here\nopen import lemmas-ground\nopen import lemmas-consistency\nopen import lemmas-matching\nopen import synth-unicity\nopen import htype-decidable\n\nopen import expansion-generality\nopen import expandability\nopen import expansion-unicity\nopen import type-assignment-unicity\nopen import typed-expansion\n\nopen import canonical-value-forms\nopen import canonical-boxed-forms\nopen import canonical-indeterminate-forms\n\nopen import lemmas-progress-checks\nopen import progress-checks\nopen import progress\nopen import preservation\n\nopen import lemmas-complete\nopen import cast-inert\nopen import complete-preservation\nopen import complete-progress\nopen import complete-expansion\n\nopen import weakening\nopen import exchange\nopen import lemmas-freshness\nopen import contraction\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"344eedbe0e9625bd6a43619ab9c60b3aaa817443","subject":"agda : wouter : predicate transformer","message":"agda : wouter : predicate transformer\n","repos":"haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc","old_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/x.agda","new_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/x.agda","new_contents":"{-# OPTIONS --type-in-type #-} -- NOT SOUND!\n\nopen import Data.Empty using (\u22a5)\nopen import Data.Nat renaming (\u2115 to Nat)\nopen import Data.Nat.DivMod\nopen import Data.Product hiding (map)\nopen import Data.Unit using (\u22a4)\n------------------------------------------------------------------------------\nopen import Relation.Binary.PropositionalEquality using (_\u2261_; refl)\n\nmodule x where\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\nabstract\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nINTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to\n - use this refinement relation to show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n2 BACKGROUND\n\nFree monads\n-}\n\n-- C : type of commands\n-- Free C R : returns an 'a' or issues command c : C\n-- For each c : C, there is a set of responses R c\n-- 2nd arg of Step is continuation : how to proceed after receiving response R c\ndata Free (C : Set) (R : C \u2192 Set) (a : Set) : Set where\n Pure : a \u2192 Free C R a\n Step : (c : C) \u2192 (R c \u2192 Free C R a) \u2192 Free C R a\n\n-- show that 'Free' is a monad:\n\nmap : \u2200 {a b C : Set} {R : C \u2192 Set} \u2192 (a \u2192 b) \u2192 Free C R a \u2192 Free C R b\nmap f (Pure x) = Pure (f x)\nmap f (Step c k) = Step c (\u03bb r \u2192 map f (k r))\n\nreturn : \u2200 {a C : Set} {R : C \u2192 Set} \u2192 a \u2192 Free C R a\nreturn = Pure\n\n_>>=_ : \u2200 {a b C : Set} {R : C \u2192 Set} \u2192 Free C R a \u2192 (a \u2192 Free C R b) \u2192 Free C R b\nPure x >>= f = f x\nStep c x >>= f = Step c (\u03bb r \u2192 x r >>= f)\n\n{-\ndifferent effects choose C and R differently, depending on their ops\n\nWeakest precondition semantics\n\nidea of associating weakest precondition semantics with imperative programs\ndates to Dijkstra\u2019s Guarded Command Language [1975]\n\nways to specify behaviour of function f : a \u2192 b\n- reference implementation\n- define a relation R : a \u2192 b \u2192 Set\n- write contracts and test cases\n- PT semantics\n\ncall values of type a \u2192 Set : predicate on type a\n\nPTs are functions between predicates\ne.g., weakest precondition:\n-}\n\n-- \"maps\"\n-- function f : a \u2192 b and\n-- desired postcondition on the function\u2019s output, b \u2192 Set\n-- to weakest precondition a \u2192 Set on function\u2019s input that ensures postcondition satisfied\n--\n-- note: definition is just reverse function composition\n-- wp0 : \u2200 {a b : Set} \u2192 (f : a \u2192 b) \u2192 (b \u2192 Set) \u2192 (a \u2192 Set)\nwp0 : \u2200 {a : Set} {b : Set} (f : a \u2192 b) \u2192 (b \u2192 Set) \u2192 (a \u2192 Set)\nwp0 f P = \u03bb x \u2192 P (f x)\n{-\nabove wp semantics is sometimes too restrictive\n- no way to specify that output is related to input\n- fix via making f dependent:\n-}\nwp : \u2200 {a : Set} {b : a \u2192 Set} (f : (x : a) \u2192 b x) \u2192 ((x : a) \u2192 b x \u2192 Set) \u2192 (a \u2192 Set)\nwp f P = \u03bb x \u2192 P x (f x)\n\n-- shorthand for working with predicates and predicates transformers\n_\u2286_ : \u2200 {a : Set} \u2192 (a \u2192 Set) \u2192 (a \u2192 Set) \u2192 Set\nP \u2286 Q = \u2200 x \u2192 P x \u2192 Q x\n\n-- refinement relation defined between PTs\n_\u2291_ : \u2200 {a : Set} {b : a \u2192 Set} \u2192 (pt1 pt2 : ((x : a) \u2192 b x \u2192 Set) \u2192 (a \u2192 Set)) \u2192 Set\u2081\npt1 \u2291 pt2 = \u2200 P \u2192 pt1 P \u2286 pt2 P\n\n{-\nuse refinement relation\n- to relate PT semantics between programs and specifications\n- to show a program satisfies its specification; or\n- to show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\nin pure setting, this refinement relation is not interesting:\nthe refinement relation corresponds to extensional equality between functions:\n\nlemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\nrefinement : \u2200 (f g : a \u2192 b) \u2192 (wp f \u2291 wp g) \u2194 (\u2200 x \u2192 f x \u2261 g x)\n\nthis paper defines PT semantics for Kleisli arrows of form\n\n a \u2192 Free C R b\n\ncould use 'wp' to assign semantics to these computations directly,\nbut typically not interested in syntactic equality between free monads\n\nrather want to study semantics of effectful programs they represent\n\nto define a PT semantics for effects\ndefine a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a \u2192 Set) \u2192 Free C R a \u2192 Set\n\n'pt' def depends on semantics desired for a particulr free monad\n\nCrucially,\nchoice of pt and weakest precondition semantics, wp, together\ngive a way to assign weakest precondition semantics to Kleisli arrows\nrepresenting effectful computations\n\n3 PARTIALITY\n\nPartial computations : i.e., 'Maybe'\n\nmake choices for commands C and responses R\n-}\n\ndata C : Set where\n Abort : C -- no continuation\n\nR : C \u2192 Set\nR Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\nPartial : Set \u2192 Set\nPartial = Free C R\n\n-- smart constructor for failure:\nabort : \u2200 {a : Set} \u2192 Partial a\nabort = Step Abort (\u03bb ())\n\n{-\ncomputation of type Partial a will either\n- return a value of type a or\n- fail, issuing abort command\n\nNote that responses to Abort command are empty;\nabort smart constructor abort uses this to discharge the continuation\nin the second argument of the Step constructor\n\nExample: division\n\nexpression language, closed under division and natural numbers:\n-}\n\ndata Expr : Set where\n Val : Nat \u2192 Expr\n Div : Expr \u2192 Expr \u2192 Expr\n\nexv : Expr\nexv = Val 3\nexd : Expr\nexd = Div (Val 3) (Val 3)\n\n-- semantics specified using inductively defined RELATION:\n-- def rules out erroneous results by requiring the divisor evaluates to non-zero\ndata _\u21d3_ : Expr \u2192 Nat \u2192 Set where\n Base : \u2200 {x : Nat}\n \u2192 Val x \u21d3 x\n Step : \u2200 {l r : Expr} {v1 v2 : Nat}\n \u2192 l \u21d3 v1\n \u2192 r \u21d3 (suc v2)\n \u2192 Div l r \u21d3 (v1 div (suc v2))\n\nexb : Val 3 \u21d3 3\nexb = Base\n\nexs : Div (Val 3) (Val 3) \u21d3 1\nexs = Step Base Base\n\n-- Alternatively\n-- evaluate Expr via monadic INTERPRETER, using Partial to handle division-by-zero\n\n-- op used by \u27e6_\u27e7 interpreter\n_\u00f7_ : Nat \u2192 Nat \u2192 Partial Nat\nn \u00f7 zero = abort\nn \u00f7 (suc k) = return (n div (suc k))\n\n\u27e6_\u27e7 : Expr \u2192 Partial Nat\n\u27e6 Val x \u27e7 = return x\n\u27e6 Div e1 e2 \u27e7 = \u27e6 e1 \u27e7 >>= \u03bb v1 \u2192 \u27e6 e2 \u27e7 >>= \u03bb v2 \u2192 v1 \u00f7 v2\n\nevv : Free C R Nat\nevv = \u27e6 Val 3 \u27e7\nevv' : evv \u2261 Pure 3\nevv' = refl\n\nevd : Free C R Nat\nevd = \u27e6 Div (Val 3) (Val 3) \u27e7\nevd' : evd \u2261 Pure 1\nevd' = refl\n\nevd0 : Free C R Nat\nevd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\nevd0' : evd0 \u2261 Step Abort (\u03bb ())\nevd0' = refl\n\n{-\nHow to relate two definitions:\n- std lib 'div' requires implicit proof that divisor is non-zero\n - \u21d3 relation generates via pattern matching\n - _\u00f7_ does explicit check\n- interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\nAssign a weakest precondition semantics to Kleisli arrows of the form\n\n a \u2192 Partial b\n-}\n\nwpPartial : \u2200 {a : Set} {b : a \u2192 Set}\n \u2192 (f : (x : a) \u2192 Partial (b x))\n \u2192 (P : (x : a) \u2192 b x \u2192 Set)\n \u2192 (a \u2192 Set)\nwpPartial f P = wp f (mustPT P)\n where\n mustPT : \u2200 {a : Set} {b : a \u2192 Set}\n \u2192 (P : (x : a) \u2192 b x \u2192 Set)\n \u2192 (x : a)\n \u2192 Partial (b x) \u2192 Set\n mustPT P _ (Pure y) = P _ y\n mustPT P _ (Step Abort _) = \u22a5\n{-\nTo call 'wp', must show how to transform\n- predicate P : b \u2192 Set\n- to a predicate on partial results : Partial b \u2192 Set\nDone via proposition 'mustPT P c'\n- holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\nparticular PT semantics of partial computations determined by def mustPT\nhere: rule out failure entirely\n- so case Abort returns empty type\n\nGiven this PT semantics for Kleisli arrows in general,\ncan now study semantics of above monadic interpreter\nvia passing\n- interpreter: \u27e6_\u27e7\n- desired postcondition : _\u21d3_\nas arguments to wpPartial:\n\n wpPartial \u27e6_\u27e7 _\u21d3_ : Expr \u2192 Set\n\nresulting in a predicate on expressions\n\nfor all expressions satisfying this predicate,\nthe monadic interpreter and the relational specification, _\u21d3_,\nmust agree on the result of evaluation\n\nWhat does this say about correctness of interpreter?\nTo understand the predicate better, consider defining this predicate on expressions:\n-}\n\nSafeDiv : Expr \u2192 Set\nSafeDiv (Val x) = \u22a4\nSafeDiv (Div e1 e2) = (e2 \u21d3 zero \u2192 \u22a5) {-\u2227-} \u00d7 SafeDiv e1 {-\u2227-} \u00d7 SafeDiv e2\n\n{-\nExpect : any expr e for which SafeDiv e holds\ncan be evaluated without division-by-zero\n\ncan prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n-- lemma relates the two semantics\n-- expressed as a relation and an evaluator\n-- for those expressions that satisfy the SafeDiv property\ncorrect : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\nInstead of manually defining SafeDiv, define more general predicate\ncharacterising the domain of a partial function:\n-}\n\ndom : \u2200 {a : Set} {b : a \u2192 Set}\n \u2192 ((x : a)\n \u2192 Partial (b x))\n \u2192 (a \u2192 Set)\ndom f = wpPartial f (\u03bb _ _ \u2192 \u22a4)\n\n{-\ncan show that the two semantics agree precisely on the domain of the interpreter:\n\nsound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\ncomplete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\nboth proofs proceed by induction on the argument expression\n\nRefinement\n\nweakest precondition semantics on partial computations give rise\nto a refinement relation on Kleisli arrows of the form a \u2192 Partial b\n\ncan characterise this relation by proving:\n\nrefinement : (f g : a \u2192 Maybe b)\n \u2192 (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x \u2192 (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\nuse refinement to relate Kleisli morphisms,\nand to relate a program to a specification given by a pre- and postcondition\n\n\nExample: Add (interpreter for stack machine)\n\nadd top two elements; can fail fail if stack has too few elements\n\nbelow shows how to prove the definition meets its specification\n\nDefine specification in terms of a pre\/post condition.\nThe specification of a function of type (x : a) \u2192 b x consists of\n-}\nrecord Spec (a : Set) (b : a \u2192 Set) : Set where\n constructor [_,_]\n field\n pre : a \u2192 Set -- a precondition on a, and\n post : (x : a) \u2192 b x \u2192 Set -- a postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n","old_contents":"{-# OPTIONS --type-in-type #-} -- NOT SOUND!\n\nopen import Data.Empty using (\u22a5)\nopen import Data.Nat renaming (\u2115 to Nat)\nopen import Data.Nat.DivMod\nopen import Data.Product hiding (map)\nopen import Data.Unit using (\u22a4)\n\nmodule x where\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\nabstract\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nINTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising\n from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to\n - use this refinement relation to show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n2 BACKGROUND\n\nFree monads\n-}\n\n-- C : type of commands\n-- Free C R : returns an 'a' or issues command c : C\n-- For each c : C, there is a set of responses R c\n-- 2nd arg of Step is continuation : how to proceed after receiving response R c\ndata Free (C : Set) (R : C \u2192 Set) (a : Set) : Set where\n Pure : a \u2192 Free C R a\n Step : (c : C) \u2192 (R c \u2192 Free C R a) \u2192 Free C R a\n\n-- show that 'Free' is a monad:\n\nmap : \u2200 {a b C : Set} {R : C \u2192 Set} \u2192 (a \u2192 b) \u2192 Free C R a \u2192 Free C R b\nmap f (Pure x) = Pure (f x)\nmap f (Step c k) = Step c (\u03bb r \u2192 map f (k r))\n\nreturn : \u2200 {a C : Set} {R : C \u2192 Set} \u2192 a \u2192 Free C R a\nreturn = Pure\n\n_>>=_ : \u2200 {a b C : Set} {R : C \u2192 Set} \u2192 Free C R a \u2192 (a \u2192 Free C R b) \u2192 Free C R b\nPure x >>= f = f x\nStep c x >>= f = Step c (\u03bb r \u2192 x r >>= f)\n\n{-\ndifferent effects choose C and R differently, depending on their ops\n\nWeakest precondition semantics\n\nidea of associating weakest precondition semantics with imperative programs\ndates to Dijkstra\u2019s Guarded Command Language [1975]\n\nways to specify behaviour of function f : a \u2192 b\n- reference implementation\n- define a relation R : a \u2192 b \u2192 Set\n- write contracts and test cases\n- PT semantics\n\ncall values of type a \u2192 Set : predicate on type a\n\nPTs are functions between predicates\ne.g., weakest precondition:\n-}\n\n-- \"maps\"\n-- function f : a \u2192 b and\n-- desired postcondition on the function\u2019s output, b \u2192 Set\n-- to weakest precondition a \u2192 Set on function\u2019s input that ensures postcondition satisfied\n--\n-- note: definition is just reverse function composition\n-- wp0 : \u2200 {a b : Set} \u2192 (f : a \u2192 b) \u2192 (b \u2192 Set) \u2192 (a \u2192 Set)\nwp0 : \u2200 {a : Set} {b : Set} (f : a \u2192 b) \u2192 (b \u2192 Set) \u2192 (a \u2192 Set)\nwp0 f P = \u03bb x \u2192 P (f x)\n{-\nabove wp semantics is sometimes too restrictive\n- no way to specify that output is related to input\n- fix via making f dependent:\n-}\nwp : \u2200 {a : Set} {b : a \u2192 Set} (f : (x : a) \u2192 b x) \u2192 ((x : a) \u2192 b x \u2192 Set) \u2192 (a \u2192 Set)\nwp f P = \u03bb x \u2192 P x (f x)\n\n-- shorthand for working with predicates and predicates transformers\n_\u2286_ : \u2200 {a : Set} \u2192 (a \u2192 Set) \u2192 (a \u2192 Set) \u2192 Set\nP \u2286 Q = \u2200 x \u2192 P x \u2192 Q x\n\n-- refinement relation defined between PTs\n_\u2291_ : \u2200 {a : Set} {b : a \u2192 Set} \u2192 (pt1 pt2 : ((x : a) \u2192 b x \u2192 Set) \u2192 (a \u2192 Set)) \u2192 Set\u2081\npt1 \u2291 pt2 = \u2200 P \u2192 pt1 P \u2286 pt2 P\n\n{-\nuse refinement relation\n- to relate PT semantics between programs and specifications\n- to show a program satisfies its specification; or\n- to show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\nin pure setting, this refinement relation is not interesting:\nthe refinement relation corresponds to extensional equality between functions:\n\nlemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\nrefinement : \u2200 (f g : a \u2192 b) \u2192 (wp f \u2291 wp g) \u2194 (\u2200 x \u2192 f x \u2261 g x)\n\nthis paper defines PT semantics for Kleisli arrows of form\n\n a \u2192 Free C R b\n\ncould use 'wp' to assign semantics to these computations directly,\nbut typically not interested in syntactic equality between free monads\n\nrather want to study semantics of effectful programs they represent\n\nto define a PT semantics for effects\ndefine a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a \u2192 Set) \u2192 Free C R a \u2192 Set\n\n'pt' def depends on semantics desired for a particulr free monad\n\nCrucially,\nchoice of pt and weakest precondition semantics, wp, together\ngive a way to assign weakest precondition semantics to Kleisli arrows\nrepresenting effectful computations\n\n3 PARTIALITY\n\nPartial computations : i.e., 'Maybe'\n\nmake choices for commands C and responses R\n-}\n\ndata C : Set where\n Abort : C -- no continuation\n\nR : C \u2192 Set\nR Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\nPartial : Set \u2192 Set\nPartial = Free C R\n\n-- smart constructor for failure:\nabort : \u2200 {a : Set} \u2192 Partial a\nabort = Step Abort (\u03bb ())\n\n{-\ncomputation of type Partial a will either\n- return a value of type a or\n- fail, issuing abort command\n\nNote that responses to Abort command are empty;\nabort smart constructor abort uses this to discharge the continuation\nin the second argument of the Step constructor\n\nExample: division\n\nexpression language, closed under division and natural numbers:\n-}\n\ndata Expr : Set where\n Val : Nat \u2192 Expr\n Div : Expr \u2192 Expr \u2192 Expr\n\n-- semantics specified using inductively defined RELATION:\n-- def rules out erroneous results by requiring the divisor evaluates to non-zero\ndata _\u21d3_ : Expr \u2192 Nat \u2192 Set where\n Base : \u2200 {x : Nat}\n \u2192 Val x \u21d3 x\n Step : \u2200 {l r : Expr} {v1 v2 : Nat}\n \u2192 l \u21d3 v1\n \u2192 r \u21d3 (suc v2)\n \u2192 Div l r \u21d3 (v1 div (suc v2))\n\n-- Alternatively\n-- evaluate Expr via monadic INTERPRETER, using Partial to handle division-by-zero\n\n-- op used by \u27e6_\u27e7 interpreter\n_\u00f7_ : Nat \u2192 Nat \u2192 Partial Nat\nn \u00f7 zero = abort\nn \u00f7 (suc k) = return (n div (suc k))\n\n\u27e6_\u27e7 : Expr \u2192 Partial Nat\n\u27e6 Val x \u27e7 = return x\n\u27e6 Div e1 e2 \u27e7 = \u27e6 e1 \u27e7 >>= \u03bb v1 \u2192 \u27e6 e2 \u27e7 >>= \u03bb v2 \u2192 v1 \u00f7 v2\n\n{-\nstd lib 'div' requires implicit proof that divisor is non-zero\n\nwhen divisor is zero, interpreter fail explicitly with abort\n\nHow to relate these two definitions?\n\nAssign a weakest precondition semantics to Kleisli arrows of the form\n\n a \u2192 Partial b\n-}\n\nwpPartial : \u2200 {a : Set} {b : a \u2192 Set}\n \u2192 (f : (x : a) \u2192 Partial (b x))\n \u2192 (P : (x : a) \u2192 b x \u2192 Set)\n \u2192 (a \u2192 Set)\nwpPartial f P = wp f (mustPT P)\n where\n mustPT : \u2200 {a : Set} {b : a \u2192 Set}\n \u2192 (P : (x : a) \u2192 b x \u2192 Set)\n \u2192 (x : a)\n \u2192 Partial (b x) \u2192 Set\n mustPT P _ (Pure y) = P _ y\n mustPT P _ (Step Abort _) = \u22a5\n{-\nTo call 'wp', must show how to transform\n- predicate P : b \u2192 Set\n- to a predicate on partial results : Partial b \u2192 Set\nDone via proposition 'mustPT P c'\n- holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\nparticular PT semantics of partial computations determined by def mustPT\nhere: rule out failure entirely\n- so case Abort returns empty type\n\nGiven this PT semantics for Kleisli arrows in general,\ncan now study semantics of above monadic interpreter\nvia passing\n- interpreter: \u27e6_\u27e7\n- desired postcondition : _\u21d3_\nas arguments to wpPartial:\n\n wpPartial \u27e6_\u27e7 _\u21d3_ : Expr \u2192 Set\n\nresulting in a predicate on expressions\n\nfor all expressions satisfying this predicate,\nthe monadic interpreter and the relational specification, _\u21d3_,\nmust agree on the result of evaluation\n\nWhat does this say about correctness of interpreter?\nTo understand the predicate better, consider defining this predicate on expressions:\n-}\n\nSafeDiv : Expr \u2192 Set\nSafeDiv (Val x) = \u22a4\nSafeDiv (Div e1 e2) = (e2 \u21d3 zero \u2192 \u22a5) {-\u2227-} \u00d7 SafeDiv e1 {-\u2227-} \u00d7 SafeDiv e2\n\n{-\nExpect : any expr e for which SafeDiv e holds\ncan be evaluated without division-by-zero\n\ncan prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n-- lemma relates the two semantics\n-- expressed as a relation and an evaluator\n-- for those expressions that satisfy the SafeDiv property\ncorrect : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\nInstead of manually defining SafeDiv, define more general predicate\ncharacterising the domain of a partial function:\n-}\n\ndom : \u2200 {a : Set} {b : a \u2192 Set}\n \u2192 ((x : a)\n \u2192 Partial (b x))\n \u2192 (a \u2192 Set)\ndom f = wpPartial f (\u03bb _ _ \u2192 \u22a4)\n\n{-\ncan show that the two semantics agree precisely on the domain of the interpreter:\n\nsound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\ncomplete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\nboth proofs proceed by induction on the argument expression\n\nRefinement\n\nweakest precondition semantics on partial computations give rise\nto a refinement relation on Kleisli arrows of the form a \u2192 Partial b\n\ncan characterise this relation by proving:\n\nrefinement : (f g : a \u2192 Maybe b)\n \u2192 (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x \u2192 (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\nuse refinement to relate Kleisli morphisms,\nand to relate a program to a specification given by a pre- and postcondition\n\n\nExample: Add (interpreter for stack machine)\n\nadd top two elements; can fail fail if stack has too few elements\n\nbelow shows how to prove the definition meets its specification\n\nDefine specification in terms of a pre\/post condition.\nThe specification of a function of type (x : a) \u2192 b x consists of\n-}\nrecord Spec (a : Set) (b : a \u2192 Set) : Set where\n constructor [_,_]\n field\n pre : a \u2192 Set -- a precondition on a, and\n post : (x : a) \u2192 b x \u2192 Set -- a postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"ab67f882ab3e8b9fa88063a7fe823a848af25c4c","subject":"Only white spaces.","message":"Only white spaces.\n\nIgnore-this: 6ca18b898d016f181623e766c9dcebeb\n\ndarcs-hash:20100727155339-3bd4e-b1c40a641d81f05c0e0291cedb65fa69721cfd7d.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Data\/Nat.agda","new_file":"LTC\/Data\/Nat.agda","new_contents":"------------------------------------------------------------------------------\n-- LTC natural numbers\n------------------------------------------------------------------------------\n\nmodule LTC.Data.Nat where\n\nopen import LTC.Minimal\n\ninfixl 7 _*_\ninfixl 6 _+_ _-_\n\n------------------------------------------------------------------------------\n-- The LTC natural numbers type.\nopen import LTC.Data.Nat.Type public\n\n------------------------------------------------------------------------------\n-- Arithmetic operations\n\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : (d : D) \u2192 zero + d \u2261 d\n +-Sx : (d e : D) \u2192 succ d + e \u2261 succ (d + e)\n{-# ATP axiom +-0x #-}\n{-# ATP axiom +-Sx #-}\n\npostulate\n _-_ : D \u2192 D \u2192 D\n minus-x0 : (d : D) \u2192 d - zero \u2261 d\n minus-0S : (d : D) \u2192 zero - succ d \u2261 zero\n minus-SS : (d e : D) \u2192 succ d - succ e \u2261 d - e\n{-# ATP axiom minus-x0 #-}\n{-# ATP axiom minus-0S #-}\n{-# ATP axiom minus-SS #-}\n\npostulate\n _*_ : D \u2192 D \u2192 D\n *-0x : (d : D) \u2192 zero * d \u2261 zero\n *-Sx : (d e : D) \u2192 succ d * e \u2261 e + d * e\n{-# ATP axiom *-0x #-}\n{-# ATP axiom *-Sx #-}\n","old_contents":"------------------------------------------------------------------------------\n-- LTC natural numbers\n------------------------------------------------------------------------------\n\nmodule LTC.Data.Nat where\n\nopen import LTC.Minimal\n\ninfixl 7 _*_\ninfixl 6 _+_ _-_\n\n------------------------------------------------------------------------------\n-- The LTC natural numbers type.\nopen import LTC.Data.Nat.Type public\n\n------------------------------------------------------------------------------\n-- Arithmetic operations\n\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : (d : D) \u2192 zero + d \u2261 d\n +-Sx : (d e : D) \u2192 succ d + e \u2261 succ (d + e)\n{-# ATP axiom +-0x #-}\n{-# ATP axiom +-Sx #-}\n\npostulate\n _-_ : D \u2192 D \u2192 D\n minus-x0 : (d : D) \u2192 d - zero \u2261 d\n minus-0S : (d : D) \u2192 zero - succ d \u2261 zero\n minus-SS : (d e : D) \u2192 succ d - succ e \u2261 d - e\n{-# ATP axiom minus-x0 #-}\n{-# ATP axiom minus-0S #-}\n{-# ATP axiom minus-SS #-}\n\npostulate\n _*_ : D \u2192 D \u2192 D\n *-0x : (d : D) \u2192 zero * d \u2261 zero\n *-Sx : (d e : D) \u2192 succ d * e \u2261 e + d * e\n{-# ATP axiom *-0x #-}\n{-# ATP axiom *-Sx #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"117ba614c0592b120afb1203485a93d78bc6bfbc","subject":"Data.Bits: \u2192\u1d47","message":"Data.Bits: \u2192\u1d47\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_\u2264_; _==_)\nopen import Data.Nat.DivMod\nopen import Data.Bool.NP hiding (_==_)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise)\nopen import Data.Vec.NP hiding (_\u229b_) renaming (map to vmap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_)\nopen import Data.Vec.NP public using ([]; _\u2237_; head; tail; replicate)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x = cong (_\u2237_ x) (vnot\u2218vnot\u2257id xs)\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n\n where open RawMonad L.monad\nallBits : \u2200 n \u2192 Vec (Bits n) (2 ^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) rewrite \u2115\u00b0.+-comm (2 ^ n) 0 = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2 ^ n))\n#\u27e8 pred \u27e9 = count pred (allBits _)\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nopen Defs public\n","old_contents":"module Data.Bits where\n\n-- cleanup\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_\u2264_; _==_)\nopen import Data.Nat.DivMod\nopen import Data.Bool.NP hiding (_==_)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise)\nopen import Data.Vec.NP hiding (_\u229b_) renaming (map to vmap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_)\nopen import Data.Vec.NP public using ([]; _\u2237_; head; tail; replicate)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x = cong (_\u2237_ x) (vnot\u2218vnot\u2257id xs)\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n\n where open RawMonad L.monad\nallBits : \u2200 n \u2192 Vec (Bits n) (2 ^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) rewrite \u2115\u00b0.+-comm (2 ^ n) 0 = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2 ^ n))\n#\u27e8 pred \u27e9 = count pred (allBits _)\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nopen Defs public\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"835301426a8c885216f233fec0a8c592e4daf8f6","subject":"Updated installation instructions.","message":"Updated installation instructions.\n\nIgnore-this: efdef7acbf124dd693f607adbc492261\n\ndarcs-hash:20110507165243-3bd4e-964560d62ba745ad1c29061ecd3f5ccbc02b505f.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"README.agda","new_file":"README.agda","new_contents":"------------------------------------------------------------------------------\n-- FOT (First Order Theories)\n------------------------------------------------------------------------------\n\n-- Code accompanying the paper \"Combining Interactive and Automatic\n-- Reasoning about Functional Programs\" by Ana Bove, Peter Dybjer, and\n-- Andr\u00e9s Sicard-Ram\u00edrez.\n\n------------------------------------------------------------------------------\n\nmodule README where\n\n------------------------------------------------------------------------------\n-- Description\n------------------------------------------------------------------------------\n\n-- Examples of the formalization of first order theories showing the\n-- combination of interactive proofs with automatics proofs carried\n-- out by first order automatic theorem provers (ATPs).\n\n------------------------------------------------------------------------------\n-- Prerequisites and use\n------------------------------------------------------------------------------\n\n-- See http:\/\/www1.eafit.edu.co\/asicard\/code\/fotc\/.\n\n------------------------------------------------------------------------------\n-- First order theories\n------------------------------------------------------------------------------\n\n-- Conventions\n\n-- The following modules show the formalization of some first order\n-- theories. If the module's name ends in 'I' the module contains\n-- interactive proofs, if it ends in 'ATP' the module contains\n-- combined proofs, otherwise the module contains definions and\/or\n-- interactive proofs that are used by the interactive and combined\n-- proofs.\n\n------------------------------------------------------------------------------\n-- 1. Predicate logic\n\n-- 1.1 Definition of the connectives and quantifiers\nopen import Common.LogicalConstants\nopen import PredicateLogic.Constants\n\n-- 1.2 The law of the excluded middle\nopen import PredicateLogic.ClassicalATP\n\n-- 1.3 Non-empty domains\nopen import PredicateLogic.NonEmptyDomain.TheoremsATP\nopen import PredicateLogic.NonEmptyDomain.TheoremsI\n\n-- 1.4 Logical schemas\nopen import PredicateLogic.SchemasATP\n\n-- 1.5 Propositional logic theorems\nopen import PredicateLogic.Propositional.TheoremsATP\nopen import PredicateLogic.Propositional.TheoremsI\n\n-- 1.6 Predicate logic theorems\nopen import PredicateLogic.TheoremsATP\nopen import PredicateLogic.TheoremsI\n\n------------------------------------------------------------------------------\n-- 2. Equality\n\n-- 2.1 Definition of equality and some properties about it\nopen import Common.Relation.Binary.PropositionalEquality\n\n-- 2.2 The equality reasoning\nopen import Common.Relation.Binary.PreorderReasoning\n\n------------------------------------------------------------------------------\n-- 3. Group theory\n\n-- 3.1 The axioms\nopen import GroupTheory.Base\n\n-- 3.2 Basic properties\nopen import GroupTheory.PropertiesATP\nopen import GroupTheory.PropertiesI\n\n-- 3.3 Commutator properties\nopen import GroupTheory.Commutator.PropertiesATP\nopen import GroupTheory.Commutator.PropertiesI\n\n-- 3.4 Abelian groups\nopen import GroupTheory.AbelianGroup.PropertiesATP\n\n------------------------------------------------------------------------------\n-- 4. Stanovsk\u00fd example (distributive laws on a binary operation)\n\n-- We prove the proposition 2 (task B) of [1]. Let _\u00b7_ be a\n-- left-associative binary operation, the task B consist in given the\n-- left and right distributive axioms\n\n-- \u2200 x y z \u2192 x \u2219 (y \u2219 z) \u2261 (x \u2219 y) \u2219 (x \u2219 z)\n-- \u2200 x y z \u2192 (x \u2219 y) \u2219 z \u2261 (x \u2219 z) \u2219 (y \u2219 z)\n\n-- to prove the theorem\n\n-- \u2200 u x y z \u2192 (x \u2219 y \u2219 (z \u2219 u)) \u2219\n-- (( x \u2219 y \u2219 ( z \u2219 u)) \u2219 (x \u2219 z \u2219 (y \u2219 u))) \u2261\n-- x \u2219 z \u2219 (y \u2219 u)\n\n-- [1] David Stanovsk\u00fd. Distributive groupoids are symmetrical-by-medial:\n-- An elementary proof. Commentations Mathematicae Universitatis\n-- Carolinae, 49(4):541\u2013546, 2008.\n\n-- 4.1 The ATPs could not prove the theorem\nopen import DistributiveLaws.TaskB-ATP\n\n-- 4.2 The interactive and combined proofs\nopen import DistributiveLaws.TaskB-HalvedStepsATP\nopen import DistributiveLaws.TaskB-I\nopen import DistributiveLaws.TaskB-TopDownATP\n\n------------------------------------------------------------------------------\n-- 5. Peano arithmetic (PA)\n\n-- We write two formalizations of PA. In the axiomatic formalization\n-- we use Agda postulates for Peano's axioms. In the inductive\n-- formalization, we use Agda data types and primitive recursive\n-- functions.\n\n-- 5.1 Axiomatic PA\n-- 5.1.1 The axioms\nopen import PA.Axiomatic.Base\n\n-- 5.1.2 Some properties\nopen import PA.Axiomatic.PropertiesATP\nopen import PA.Axiomatic.PropertiesI\n\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityATP\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityI\n\n-- 5.2. Inductive PA\n-- 5.2.1 Some properties\nopen import PA.Inductive.Properties\nopen import PA.Inductive.PropertiesATP\nopen import PA.Inductive.PropertiesI\n\nopen import PA.Inductive.PropertiesByInduction\nopen import PA.Inductive.PropertiesByInductionATP\nopen import PA.Inductive.PropertiesByInductionI\n\n------------------------------------------------------------------------------\n-- 6. FOTC\n\n-- Formalization of (a version of) Azcel's First Order Theory of Combinators.\n\n-- 6.1 The axioms\nopen import FOTC.Base\n\n-- 6.2 Booleans\n\n-- 6.2.2 The axioms\nopen import FOTC.Data.Bool\n\n-- 6.2.3 Properties\nopen import FOTC.Data.Bool.PropertiesATP\nopen import FOTC.Data.Bool.PropertiesI\n\n-- 6.3. Natural numbers\n\n-- 6.3.1 The axioms\nopen import FOTC.Data.Nat\n\n-- 6.3.2 Properties\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Data.Nat.PropertiesI\n\nopen import FOTC.Data.Nat.PropertiesByInductionATP\nopen import FOTC.Data.Nat.PropertiesByInductionI\n\n-- 6.3.3 Divisibility relation\nopen import FOTC.Data.Nat.Divisibility.PropertiesATP\nopen import FOTC.Data.Nat.Divisibility.PropertiesI\n\n-- 6.3.4 Induction\nopen import FOTC.Data.Nat.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.Nat.Induction.NonAcc.LexicographicI\n\n-- 6.3.5 Inequalites\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.Inequalities.PropertiesI\n\n-- 6.3.6 Unary numbers\nopen import FOTC.Data.Nat.UnaryNumbers.IsN-ATP\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\n\n-- 6.4 Lists\n\n-- 6.4.1 The axioms\nopen import FOTC.Data.List\n\n-- 6.4.2 Properties\nopen import FOTC.Data.List.PropertiesATP\nopen import FOTC.Data.List.PropertiesI\n\n-- 6.4.3 Well-founded induction\nopen import FOTC.Data.List.LT-Cons.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.List.LT-Cons.PropertiesI\nopen import FOTC.Data.List.LT-Length.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.List.LT-Length.PropertiesI\n\n-- 6.4.4 Lists of natural numbers\nopen import FOTC.Data.Nat.List.PropertiesATP\nopen import FOTC.Data.Nat.List.PropertiesI\n\n-- 6.5 Programs\n\n-- 6.5.1 The Collatz function: A function without a termination proof\nopen import FOTC.Program.Collatz.PropertiesATP\nopen import FOTC.Program.Collatz.PropertiesI\n\n-- 6.5.2 The GCD algorithm: A non-structurally recursive algorithm\nopen import FOTC.Program.GCD.ProofSpecificationATP\nopen import FOTC.Program.GCD.ProofSpecificationI\n\n-- 6.5.3 The McCarthy 91 function: A function with nested recursion\nopen import FOTC.Program.McCarthy91.Properties.MainATP\n\n-- 6.5.4 The mirror function: A function with higher-order recursion\nopen import FOTC.Program.Mirror.PropertiesATP\nopen import FOTC.Program.Mirror.PropertiesI\n\n-- 6.5.5 Burstall's sort list algorithm: A structurally recursive algorithm\nopen import FOTC.Program.SortList.ProofSpecificationATP\nopen import FOTC.Program.SortList.ProofSpecificationI\n\n------------------------------------------------------------------------------\n-- ATPs failures\n------------------------------------------------------------------------------\n\n-- The ATPs (E, Equinox, Metis and Vampire) could not prove some\n-- theorems.\n\n-- This module was imported in the Stanovsk\u00fd example\n-- open import DistributiveLaws.TaskB-ATP\nopen import LTC-PCF.Program.GCD.EquationsATP\nopen import PA.Axiomatic.PropertiesATP\n\n------------------------------------------------------------------------------\n-- Agsy examples\n------------------------------------------------------------------------------\n\n-- We cannot import the Agsy examples because some modules contain\n-- unsolved metas, therefore see src\/Agsy\/README.txt\n\n------------------------------------------------------------------------------\n-- Other theories\n------------------------------------------------------------------------------\n\n-- 1. LTC-PCF\n-- Formalization of a version of Azcel's Logical Theory of Constructions.\n-- N.B. This was the theory shown in our PLPV'09 paper.\n\n-- 1.1. The axioms\nopen import LTC-PCF.Base\n\n-- 1.2 Natural numberes\n\n-- 1.2.1 The axioms\nopen import LTC-PCF.Data.Nat\n\n-- 1.2.2 Properties\nopen import LTC-PCF.Data.Nat.PropertiesATP\nopen import LTC-PCF.Data.Nat.PropertiesI\n\n-- 1.2.3 Divisibility relation\nopen import LTC-PCF.Data.Nat.Divisibility.Properties\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesATP\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesI\n\n-- 1.2.4 Induction\nopen import LTC-PCF.Data.Nat.Induction.NonAcc.LexicographicI\nopen import LTC-PCF.Data.Nat.Induction.NonAcc.WellFoundedInductionI\n\n-- 1.2.5 Inequalites\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesATP\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesI\n\n-- 1.2.6 The recursive operator\nopen import LTC-PCF.Data.Nat.Rec.EquationsATP\nopen import LTC-PCF.Data.Nat.Rec.EquationsI\n\n-- 1.3 Programs\n\n-- 1.3.1 The division algorithm: A non-structurally recursive algorithm\nopen import LTC-PCF.Program.Division.ProofSpecificationATP\nopen import LTC-PCF.Program.Division.ProofSpecificationI\n\n-- 1.3.2 The GCD algorithm: A non-structurally recursive algorithm\nopen import LTC-PCF.Program.GCD.ProofSpecificationATP\nopen import LTC-PCF.Program.GCD.ProofSpecificationI\n","old_contents":"------------------------------------------------------------------------------\n-- FOT (First Order Theories)\n------------------------------------------------------------------------------\n\n-- Code accompanying the paper \"Combining Interactive and Automatic\n-- Theorem Proving for Reasoning about Functional Programs\" by Ana\n-- Bove, Peter Dybjer, and Andr\u00e9s Sicard-Ram\u00edrez.\n\n------------------------------------------------------------------------------\n\nmodule README where\n\n------------------------------------------------------------------------------\n-- Description\n------------------------------------------------------------------------------\n\n-- Examples of the formalization of first order theories showing the\n-- combination of interactive proofs with automatics proofs carried\n-- out by first order automatic theorem provers (ATPs).\n\n------------------------------------------------------------------------------\n-- Prerequisites\n------------------------------------------------------------------------------\n\n-- You can download the agda2atp tool (described in above paper) using\n-- darcs with the following command:\n\n-- $ darcs get http:\/\/patch-tag.com\/r\/asr\/agda2atp\n\n-- The agda2atp tool and the FOT code require a modified version of\n-- Agda. See agda2atp\/README.txt for the related instructions.\n\n------------------------------------------------------------------------------\n-- Use\n------------------------------------------------------------------------------\n\n-- Let's suppose you want to use the Metis ATP to prove the group\n-- theory properties stated in the module\n-- GroupTheory.PropertiesATP. First, you should type-check the module using\n-- Agda\n\n-- $ agda -isrc src\/GroupTheory\/PropertiesATP.agda\n\n-- Second, you call the agda2tool using the Metis ATP\n\n-- $ agda2atp -isrc --atp=metis src\/GroupTheory\/PropertiesATP.agda\n\n------------------------------------------------------------------------------\n-- First order theories\n------------------------------------------------------------------------------\n\n-- Conventions\n\n-- The following modules show the formalization of some first order\n-- theories. If the module's name ends in 'I' the module contains\n-- interactive proofs, if it ends in 'ATP' the module contains\n-- combined proofs, otherwise the module contains definions and\/or\n-- interactive proofs that are used by the interactive and combined\n-- proofs.\n\n------------------------------------------------------------------------------\n-- 1. Predicate logic\n\n-- 1.1 Definition of the connectives and quantifiers\nopen import Common.LogicalConstants\nopen import PredicateLogic.Constants\n\n-- 1.2 The law of the excluded middle\nopen import PredicateLogic.ClassicalATP\n\n-- 1.3 Non-empty domains\nopen import PredicateLogic.NonEmptyDomain.TheoremsATP\nopen import PredicateLogic.NonEmptyDomain.TheoremsI\n\n-- 1.4 Logical schemas\nopen import PredicateLogic.SchemasATP\n\n-- 1.5 Propositional logic theorems\nopen import PredicateLogic.Propositional.TheoremsATP\nopen import PredicateLogic.Propositional.TheoremsI\n\n-- 1.6 Predicate logic theorems\nopen import PredicateLogic.TheoremsATP\nopen import PredicateLogic.TheoremsI\n\n------------------------------------------------------------------------------\n-- 2. Equality\n\n-- 2.1 Definition of equality and some properties about it\nopen import Common.Relation.Binary.PropositionalEquality\n\n-- 2.2 The equality reasoning\nopen import Common.Relation.Binary.PreorderReasoning\n\n------------------------------------------------------------------------------\n-- 3. Group theory\n\n-- 3.1 The axioms\nopen import GroupTheory.Base\n\n-- 3.2 Basic properties\nopen import GroupTheory.PropertiesATP\nopen import GroupTheory.PropertiesI\n\n-- 3.3 Commutator properties\nopen import GroupTheory.Commutator.PropertiesATP\nopen import GroupTheory.Commutator.PropertiesI\n\n-- 3.4 Abelian groups\nopen import GroupTheory.AbelianGroup.PropertiesATP\n\n------------------------------------------------------------------------------\n-- 4. Stanovsk\u00fd example (distributive laws on a binary operation)\n\n-- We prove the proposition 2 (task B) of [1]. Let _\u00b7_ be a\n-- left-associative binary operation, the task B consist in given the\n-- left and right distributive axioms\n\n-- \u2200 x y z \u2192 x \u2219 (y \u2219 z) \u2261 (x \u2219 y) \u2219 (x \u2219 z)\n-- \u2200 x y z \u2192 (x \u2219 y) \u2219 z \u2261 (x \u2219 z) \u2219 (y \u2219 z)\n\n-- to prove the theorem\n\n-- \u2200 u x y z \u2192 (x \u2219 y \u2219 (z \u2219 u)) \u2219\n-- (( x \u2219 y \u2219 ( z \u2219 u)) \u2219 (x \u2219 z \u2219 (y \u2219 u))) \u2261\n-- x \u2219 z \u2219 (y \u2219 u)\n\n-- [1] David Stanovsk\u00fd. Distributive groupoids are symmetrical-by-medial:\n-- An elementary proof. Commentations Mathematicae Universitatis\n-- Carolinae, 49(4):541\u2013546, 2008.\n\n-- 4.1 The ATPs could not prove the theorem\nopen import DistributiveLaws.TaskB-ATP\n\n-- 4.2 The interactive and combined proofs\nopen import DistributiveLaws.TaskB-HalvedStepsATP\nopen import DistributiveLaws.TaskB-I\nopen import DistributiveLaws.TaskB-TopDownATP\n\n------------------------------------------------------------------------------\n-- 5. Peano arithmetic (PA)\n\n-- We write two formalizations of PA. In the axiomatic formalization\n-- we use Agda postulates for Peano's axioms. In the inductive\n-- formalization, we use Agda data types and primitive recursive\n-- functions.\n\n-- 5.1 Axiomatic PA\n-- 5.1.1 The axioms\nopen import PA.Axiomatic.Base\n\n-- 5.1.2 Some properties\nopen import PA.Axiomatic.PropertiesATP\nopen import PA.Axiomatic.PropertiesI\n\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityATP\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityI\n\n-- 5.2. Inductive PA\n-- 5.2.1 Some properties\nopen import PA.Inductive.Properties\nopen import PA.Inductive.PropertiesATP\nopen import PA.Inductive.PropertiesI\n\nopen import PA.Inductive.PropertiesByInduction\nopen import PA.Inductive.PropertiesByInductionATP\nopen import PA.Inductive.PropertiesByInductionI\n\n------------------------------------------------------------------------------\n-- 6. FOTC\n\n-- Formalization of (a version of) Azcel's First Order Theory of Combinators.\n\n-- 6.1 The axioms\nopen import FOTC.Base\n\n-- 6.2 Booleans\n\n-- 6.2.2 The axioms\nopen import FOTC.Data.Bool\n\n-- 6.2.3 Properties\nopen import FOTC.Data.Bool.PropertiesATP\nopen import FOTC.Data.Bool.PropertiesI\n\n-- 6.3. Natural numbers\n\n-- 6.3.1 The axioms\nopen import FOTC.Data.Nat\n\n-- 6.3.2 Properties\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Data.Nat.PropertiesI\n\nopen import FOTC.Data.Nat.PropertiesByInductionATP\nopen import FOTC.Data.Nat.PropertiesByInductionI\n\n-- 6.3.3 Divisibility relation\nopen import FOTC.Data.Nat.Divisibility.PropertiesATP\nopen import FOTC.Data.Nat.Divisibility.PropertiesI\n\n-- 6.3.4 Induction\nopen import FOTC.Data.Nat.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.Nat.Induction.NonAcc.LexicographicI\n\n-- 6.3.5 Inequalites\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.Inequalities.PropertiesI\n\n-- 6.3.6 Unary numbers\nopen import FOTC.Data.Nat.UnaryNumbers.IsN-ATP\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\n\n-- 6.4 Lists\n\n-- 6.4.1 The axioms\nopen import FOTC.Data.List\n\n-- 6.4.2 Properties\nopen import FOTC.Data.List.PropertiesATP\nopen import FOTC.Data.List.PropertiesI\n\n-- 6.4.3 Well-founded induction\nopen import FOTC.Data.List.LT-Cons.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.List.LT-Cons.PropertiesI\nopen import FOTC.Data.List.LT-Length.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.List.LT-Length.PropertiesI\n\n-- 6.4.4 Lists of natural numbers\nopen import FOTC.Data.Nat.List.PropertiesATP\nopen import FOTC.Data.Nat.List.PropertiesI\n\n-- 6.5 Programs\n\n-- 6.5.1 The Collatz function: A function without a termination proof\nopen import FOTC.Program.Collatz.PropertiesATP\nopen import FOTC.Program.Collatz.PropertiesI\n\n-- 6.5.2 The GCD algorithm: A non-structurally recursive algorithm\nopen import FOTC.Program.GCD.ProofSpecificationATP\nopen import FOTC.Program.GCD.ProofSpecificationI\n\n-- 6.5.3 The McCarthy 91 function: A function with nested recursion\nopen import FOTC.Program.McCarthy91.Properties.MainATP\n\n-- 6.5.4 The mirror function: A function with higher-order recursion\nopen import FOTC.Program.Mirror.PropertiesATP\nopen import FOTC.Program.Mirror.PropertiesI\n\n-- 6.5.5 Burstall's sort list algorithm: A structurally recursive algorithm\nopen import FOTC.Program.SortList.ProofSpecificationATP\nopen import FOTC.Program.SortList.ProofSpecificationI\n\n------------------------------------------------------------------------------\n-- ATPs failures\n------------------------------------------------------------------------------\n\n-- The ATPs (E, Equinox, Metis and Vampire) could not prove some\n-- theorems.\n\n-- This module was imported in the Stanovsk\u00fd example\n-- open import DistributiveLaws.TaskB-ATP\nopen import LTC-PCF.Program.GCD.EquationsATP\nopen import PA.Axiomatic.PropertiesATP\n\n------------------------------------------------------------------------------\n-- Agsy examples\n------------------------------------------------------------------------------\n\n-- We cannot import the Agsy examples because some modules contain\n-- unsolved metas, therefore see src\/Agsy\/README.txt\n\n------------------------------------------------------------------------------\n-- Other theories\n------------------------------------------------------------------------------\n\n-- 1. LTC-PCF\n-- Formalization of a version of Azcel's Logical Theory of Constructions.\n-- N.B. This was the theory shown in our PLPV'09 paper.\n\n-- 1.1. The axioms\nopen import LTC-PCF.Base\n\n-- 1.2 Natural numberes\n\n-- 1.2.1 The axioms\nopen import LTC-PCF.Data.Nat\n\n-- 1.2.2 Properties\nopen import LTC-PCF.Data.Nat.PropertiesATP\nopen import LTC-PCF.Data.Nat.PropertiesI\n\n-- 1.2.3 Divisibility relation\nopen import LTC-PCF.Data.Nat.Divisibility.Properties\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesATP\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesI\n\n-- 1.2.4 Induction\nopen import LTC-PCF.Data.Nat.Induction.NonAcc.LexicographicI\nopen import LTC-PCF.Data.Nat.Induction.NonAcc.WellFoundedInductionI\n\n-- 1.2.5 Inequalites\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesATP\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesI\n\n-- 1.2.6 The recursive operator\nopen import LTC-PCF.Data.Nat.Rec.EquationsATP\nopen import LTC-PCF.Data.Nat.Rec.EquationsI\n\n-- 1.3 Programs\n\n-- 1.3.1 The division algorithm: A non-structurally recursive algorithm\nopen import LTC-PCF.Program.Division.ProofSpecificationATP\nopen import LTC-PCF.Program.Division.ProofSpecificationI\n\n-- 1.3.2 The GCD algorithm: A non-structurally recursive algorithm\nopen import LTC-PCF.Program.GCD.ProofSpecificationATP\nopen import LTC-PCF.Program.GCD.ProofSpecificationI\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b9f8c7e62100cb6c18942ebd737297d5edfa9267","subject":"IDesc model: implement lists","message":"IDesc model: implement lists\n","repos":"mietek\/epigram2,larrytheliquid\/pigit,mietek\/epigram2","old_file":"models\/IDesc.agda","new_file":"models\/IDesc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (l : Level)(I : Set l) : Set (suc l) where\n var : I -> IDesc l I\n const : Set l -> IDesc l I\n prod : IDesc l I -> IDesc l I -> IDesc l I\n sigma : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n pi : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\ndesc _ I (var i) P = P i\ndesc _ I (const X) P = X\ndesc x I (prod D D') P = desc x I D P * desc x I D' P\ndesc x I (sigma S T) P = Sigma S (\\s -> desc x I (T s) P)\ndesc x I (pi S T) P = (s : S) -> desc x I (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l where\n con : desc l I (R i) (\\j -> IMu l I R j) -> IMu l I R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : (l : Level)(I : Set l)(D : IDesc l I)(P : I -> Set l) -> desc l I D P -> IDesc l (Sigma I P)\nbox _ I (var i) P x = var (i , x)\nbox _ I (const X) P x = const X\nbox x I (prod D D') P (d , d') = prod (box x I D P d) (box x I D' P d')\nbox x I (sigma S T) P (a , b) = box x I (T a) P b\nbox x I (pi S T) P f = pi S (\\s -> box x I (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (l : Level)\n (I : Set l)\n (R : I -> IDesc l I)\n (P : Sigma I (IMu l I R) -> Set l)\n (m : (i : I)\n (xs : desc l I (R i) (IMu l I R))\n (hs : desc l (Sigma I (IMu l I R)) (box l I (R i) (IMu l I R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu l I R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc l I) -> \n (xs : desc l I D (IMu l I R)) -> \n desc l (Sigma I (IMu l I R)) (box l I D (IMu l I R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (l : Level)\n (I : Set l)\n (R : I -> IDesc l I)\n (P : Sigma I (IMu l I R) -> Set l)\n (m : (i : I)\n (xs : desc l I (R i) (IMu l I R))\n (hs : desc l (Sigma I (IMu l I R)) (box l I (R i) (IMu l I R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu l I R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- List\n--********************************************\n\ndata ListDConst (l : Level) : Set l where\n cnil : ListDConst l\n ccons : ListDConst l\n\nlistDChoice : (l : Level) -> Set l -> ListDConst l -> IDesc l Unit\nlistDChoice x X cnil = const Unit\nlistDChoice x X ccons = sigma X (\\_ -> var Void)\n\nlistD : (l : Level) -> Set l -> IDesc l Unit\nlistD x X = sigma (ListDConst x) (listDChoice x X)\n\nlist : (l : Level) -> Set l -> Set l\nlist x X = IMu x Unit (\\_ -> listD x X) Void\n\n\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst (l : Level) : Set l where\n lvar : DescDConst l\n lconst : DescDConst l\n lprod : DescDConst l\n lpi : DescDConst l\n lsigma : DescDConst l\n\ndescDChoice : (l : Level) -> Set l -> DescDConst (suc l) -> IDesc (suc l) Unit\ndescDChoice _ I lvar = const (lift I)\ndescDChoice x _ lconst = const (Set x)\ndescDChoice _ _ lprod = prod (var Void) (var Void)\ndescDChoice x _ lpi = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice x _ lsigma = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\ndescD x I = sigma (DescDConst (suc x)) (descDChoice x I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (suc x) Unit (\\_ -> descD x I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst (suc x)\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst (suc x)\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst (suc x)\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst (suc x)\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst (suc x)\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n ","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (l : Level)(I : Set l) : Set (suc l) where\n var : I -> IDesc l I\n const : Set l -> IDesc l I\n prod : IDesc l I -> IDesc l I -> IDesc l I\n sigma : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n pi : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\ndesc _ I (var i) P = P i\ndesc _ I (const X) P = X\ndesc x I (prod D D') P = desc x I D P * desc x I D' P\ndesc x I (sigma S T) P = Sigma S (\\s -> desc x I (T s) P)\ndesc x I (pi S T) P = (s : S) -> desc x I (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l where\n con : desc l I (R i) (\\j -> IMu l I R j) -> IMu l I R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : (l : Level)(I : Set l)(D : IDesc l I)(P : I -> Set l) -> desc l I D P -> IDesc l (Sigma I P)\nbox _ I (var i) P x = var (i , x)\nbox _ I (const X) P x = const X\nbox x I (prod D D') P (d , d') = prod (box x I D P d) (box x I D' P d')\nbox x I (sigma S T) P (a , b) = box x I (T a) P b\nbox x I (pi S T) P f = pi S (\\s -> box x I (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (l : Level)\n (I : Set l)\n (R : I -> IDesc l I)\n (P : Sigma I (IMu l I R) -> Set l)\n (m : (i : I)\n (xs : desc l I (R i) (IMu l I R))\n (hs : desc l (Sigma I (IMu l I R)) (box l I (R i) (IMu l I R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu l I R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc l I) -> \n (xs : desc l I D (IMu l I R)) -> \n desc l (Sigma I (IMu l I R)) (box l I D (IMu l I R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (l : Level)\n (I : Set l)\n (R : I -> IDesc l I)\n (P : Sigma I (IMu l I R) -> Set l)\n (m : (i : I)\n (xs : desc l I (R i) (IMu l I R))\n (hs : desc l (Sigma I (IMu l I R)) (box l I (R i) (IMu l I R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu l I R i) -> P ( i , x )\ninduction = Elim.induction\n\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst (l : Level) : Set l where\n lvar : DescDConst l\n lconst : DescDConst l\n lprod : DescDConst l\n lpi : DescDConst l\n lsigma : DescDConst l\n\ndescDChoice : (l : Level) -> Set l -> DescDConst (suc l) -> IDesc (suc l) Unit\ndescDChoice _ I lvar = const (lift I)\ndescDChoice x _ lconst = const (Set x)\ndescDChoice _ _ lprod = prod (var Void) (var Void)\ndescDChoice x _ lpi = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice x _ lsigma = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\ndescD x I = sigma (DescDConst (suc x)) (descDChoice x I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (suc x) Unit (\\_ -> descD x I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst (suc x)\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst (suc x)\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst (suc x)\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst (suc x)\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst (suc x)\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n ","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7a558dcc2540370cdaf9ebd714d4a564bda778c0","subject":"\u2200 P \u2192 Rel P","message":"\u2200 P \u2192 Rel P\n","repos":"crypto-agda\/crypto-agda","old_file":"Control\/Protocol\/Choreography.agda","new_file":"Control\/Protocol\/Choreography.agda","new_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map; proj\u2081 to fst; proj\u2082 to snd)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\nopen import Data.One hiding (_\u225f_)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP as \u2261\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\n {-\ndata S<_> {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n\npattern com' q M P = com (mk q M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\nopen ProtoRel _\u2261_ public renaming (_\u2248\u1d3e_ to _\u2261\u1d3e_)\n\n{-\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n -}\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n Trace : Proto \u2192 Proto\n Trace end = end\n Trace (com c) = com (Trace\u1d9c c)\n\n Trace\u1d9c : Com \u2192 Com\n Trace\u1d9c c = \u03a3\u1d9c (Com.M c) \u03bb m \u2192 Trace (Com.P c m)\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c c = mk (dual\u1d35\u1d3c (Com.io c)) (Com.M c) \u03bb m \u2192 dual (Com.P c m)\n\ndata IsTrace : Proto \u2192 \u2605\u2081 where\n end : IsTrace end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsTrace (P m)) \u2192 IsTrace (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n\u27e6_\u27e7\u27e8_\u2248_\u27e9 : (P : Proto) (p q : \u27e6 P \u27e7) \u2192 \u2605\n\u27e6 end \u27e7\u27e8 p \u2248 q \u27e9 = \ud835\udfd9\n\u27e6 \u03a0\u1d3e M P \u27e7\u27e8 p \u2248 q \u27e9 = (m : M) \u2192 \u27e6 P m \u27e7\u27e8 p m \u2248 q m \u27e9\n\u27e6 \u03a3\u1d3e M P \u27e7\u27e8 p \u2248 q \u27e9 = \u03a3 (fst p \u2261 fst q) \u03bb e \u2192 \u27e6 P (fst q) \u27e7\u27e8 subst (\u27e6_\u27e7 \u2218 P) e (snd p) \u2248 snd q \u27e9\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\nmutual\n data Dual\u1d9c : Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a0\u1d9c M P) (\u03a3\u1d9c M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a3\u1d9c M P) (\u03a0\u1d9c M Q)\n\n data Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n {-\n \u03a0\u2610\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P [ x ]) (Q x)) \u2192 Dual (\u03a0\u1d3e (\u2610 M) P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0\u2610 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q [ x ])) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e (\u2610 M) Q)\n -}\n\n {-\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Trace : Choreo I \u2192 Proto\n \u2102Trace \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Trace-IsTrace : \u2200 \u2102 \u2192 IsTrace (\u2102Trace \u2102)\n \u2102Trace-IsTrace (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n-}\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com refl M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com refl M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\ndual\u1d35\u1d3c-involutive : \u2200 io \u2192 dual\u1d35\u1d3c (dual\u1d35\u1d3c io) \u2261 io\ndual\u1d35\u1d3c-involutive In = refl\ndual\u1d35\u1d3c-involutive Out = refl\n\ndual-involutive : \u2200 P \u2192 dual (dual P) \u2261\u1d3e P\ndual-involutive end = end\ndual-involutive (com' q M P) = com (dual\u1d35\u1d3c-involutive q) M (\u03bb m \u2192 dual-involutive (P m))\n\nTrace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261\u1d3e Trace P\nTrace-idempotent end = end\nTrace-idempotent (com' _ M P) = com refl M \u03bb m \u2192 Trace-idempotent (P m)\n\nTrace-dual-oblivious : \u2200 P \u2192 Trace (dual P) \u2261\u1d3e Trace P\nTrace-dual-oblivious end = end\nTrace-dual-oblivious (com' _ M P) = com refl M \u03bb m \u2192 Trace-dual-oblivious (P m)\n\nSink : Proto \u2192 Proto\nSink = dual \u2218 Trace\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n{-\n++Tele : \u2200 (P : Proto){Q : Tele P \u2192 Proto} (xs : Tele P) \u2192 Tele (Q xs) \u2192 Tele (P >>= Q)\n++Tele end _ ys = ys\n++Tele (com' q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com q M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com q M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n{-\nDual-sym (\u03a0\u2610\u00b7\u03a3 f) = {!\u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))!}\nDual-sym (\u03a3\u00b7\u03a0\u2610 f) = {!\u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))!}\n-}\n-}\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele (dual P) \u2261 Tele P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n El->>= : (P : Proto){Q : Tele P \u2192 Proto}{X : Tele (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Tele P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n-}\n\n>>=-com : (P : Proto){Q : Tele P \u2192 Proto}{R : Tele P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Tele P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Tele P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : \u2605_ a} (l r : A) \u2192 LR \u2192 A \n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a0\u1d3e LR [L: l R: r ]\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP >>\u1d9c S = record P { P = \u03bb m \u2192 S (Com.P P m) }\n\nmodule Equivalences\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n record Equiv {A B : \u2605}(f : A \u2192 B) : \u2605 where\n field\n linv : B \u2192 A\n is-linv : \u2200 x \u2192 linv (f x) \u2261 x\n rinv : B \u2192 A\n is-rinv : \u2200 x \u2192 f (rinv x) \u2261 x\n\n id\u1d31 : \u2200 {A} \u2192 Equiv {A} id\n id\u1d31 = record\n { linv = id\n ; is-linv = \u03bb _ \u2192 refl\n ; rinv = id\n ; is-rinv = \u03bb _ \u2192 refl\n }\n\n module _ {A B C}{g : B \u2192 C}{f : A \u2192 B} where\n _\u2218\u1d31_ : Equiv g \u2192 Equiv f \u2192 Equiv (g \u2218 f)\n g\u1d31 \u2218\u1d31 f\u1d31 = record { linv = F.linv \u2218 G.linv ; is-linv = \u03bb x \u2192 cong F.linv (G.is-linv (f x)) \u2219 F.is-linv x\n ; rinv = F.rinv \u2218 G.rinv ; is-rinv = \u03bb x \u2192 cong g (F.is-rinv _) \u2219 G.is-rinv x }\n where\n module G = Equiv g\u1d31\n module F = Equiv f\u1d31\n\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = \u03a3 (A \u2192 B) Equiv\n\n module _ {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b} where\n \u03a3-ext : \u2200 {x y : \u03a3 A B} \u2192 (p : fst x \u2261 fst y) \u2192 subst B p (snd x) \u2261 snd y \u2192 x \u2261 y\n \u03a3-ext refl = cong (_,_ _)\n\ndata ViewProc : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200 M(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewProc (\u03a3\u1d3e M P) (m , p)\n recv : \u2200 M(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewProc (\u03a0\u1d3e M P) p\n end : ViewProc end _\n\nview-proc : \u2200 P (p : \u27e6 P \u27e7) \u2192 ViewProc P p\nview-proc end _ = end\nview-proc (\u03a0\u1d3e M P) p = recv _ _ p\nview-proc (\u03a3\u1d3e M P) (m , p) = send _ _ m p\n\ndata ViewCom : \u2200 P \u2192 \u27e6 com P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200{M}(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewCom (\u03a3\u1d9c M P) (m , p)\n recv : \u2200{M}(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewCom (\u03a0\u1d9c M P) p\n\nview-com : \u2200 P (p : \u27e6 com P \u27e7) \u2192 ViewCom P p\nview-com (\u03a0\u1d9c M P) p = recv _ p\nview-com (\u03a3\u1d9c M P) (m , p) = send _ m p\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open Equivalences funExt\n\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n \u03a0\u1d3e M P \u214b\u1d3e Q = \u03a0\u1d3e M \u03bb m \u2192 P m \u214b\u1d3e Q\n P \u214b\u1d3e end = P\n P \u214b\u1d3e \u03a0\u1d3e M Q = \u03a0\u1d3e M \u03bb m \u2192 P \u214b\u1d3e Q m\n \u03a3\u1d3e M P \u214b\u1d3e \u03a3\u1d3e M' Q = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 P m \u214b\u1d3e \u03a3\u1d3e M' Q)\n ,inr: (\u03bb m' \u2192 \u03a3\u1d3e M P \u214b\u1d3e Q m') ]\n\n data View-\u214b : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n sendL' : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto)(m : M )(p : \u27e6 P m \u214b\u1d3e \u03a3\u1d3e M' Q \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p)\n sendR' : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto)(m' : M')(p : \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q m' \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m' , p)\n recvL' : \u2200 {M} (P : M \u2192 Proto) Q (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7)) \u2192 View-\u214b (\u03a0\u1d3e M P) Q p\n recvR' : \u2200 {M M'} (P : M \u2192 Proto) (Q : M' \u2192 Proto)(p : (m' : M') \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q m' \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p\n endL : \u2200 Q (p : \u27e6 Q \u27e7) \u2192 View-\u214b end Q p\n endR : \u2200 {M} (P : M \u2192 Proto) (p : \u27e6 \u03a3\u1d3e M P \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) end p\n\n view-\u214b : \u2200 P Q (p : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 View-\u214b P Q p\n view-\u214b end Q p = endL Q p\n view-\u214b (\u03a0\u1d3e M P) Q p = recvL' P Q p\n view-\u214b (\u03a3\u1d3e M P) end p = endR P p\n view-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = recvR' P Q p\n view-\u214b (com (mk Out M P)) (com (mk Out M' Q)) (inl x , p) = sendL' P Q x p\n view-\u214b (com (mk Out M P)) (com (mk Out M' Q)) (inr y , p) = sendR' P Q y p\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7 \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7\n \u214b\u1d3e-assoc end Q R s = s\n \u214b\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = \u214b\u1d3e-assoc (P m) _ _ (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , \u214b\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end p = p\n \u214b\u1d3e-rend (\u03a0\u1d3e _ P) p = \u03bb m \u2192 \u214b\u1d3e-rend (P m) (p m)\n \u214b\u1d3e-rend (\u03a3\u1d3e _ _) p = p\n\n \u214b\u1d3e-rend' : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u214b\u1d3e end \u27e7\n \u214b\u1d3e-rend' end p = p\n \u214b\u1d3e-rend' (\u03a0\u1d3e _ P) p = \u03bb m \u2192 \u214b\u1d3e-rend' (P m) (p m)\n \u214b\u1d3e-rend' (\u03a3\u1d3e _ _) p = p\n\n {-\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n -}\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (\u03a3\u1d3e M P) m p = inr m , p\n \u214b\u1d3e-sendR (\u03a0\u1d3e M P) m p = \u03bb x \u2192 \u214b\u1d3e-sendR (P x) m (p x)\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL (\u03a0\u1d3e M Q) m p = \u03bb m' \u2192 \u214b\u1d3e-sendL (Q m') m {!p!}\n \u214b\u1d3e-sendL (\u03a3\u1d3e M Q) m p = {!!} , p\n\n {-\n \u214b\u1d3e-sendR : \u2200 {M} P Q \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e M Q \u27e7 \u2192 (m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7\n \u214b\u1d3e-sendR P Q s m = {!!}\n -}\n\n \u214b\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e M Q \u27e7\n \u214b\u1d3e-recvR end Q s = s\n \u214b\u1d3e-recvR (\u03a0\u1d3e M P) Q s = \u03bb x \u2192 \u214b\u1d3e-recvR (P x) Q (\u03bb m \u2192 s m x)\n \u214b\u1d3e-recvR (\u03a3\u1d3e M P) Q s = s\n\n mutual\n \u214b\u1d3e-! : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-! P Q p = {!!}\n\n \u214b\u1d3e-!-view : \u2200 {P Q} {pq : \u27e6 P \u214b\u1d3e Q \u27e7} \u2192 View-\u214b P Q pq \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-!-view (sendL' P Q m p) = \u214b\u1d3e-sendR (\u03a3\u1d3e _ Q) m (\u214b\u1d3e-! (P m) (com (mk Out _ Q)) p)\n \u214b\u1d3e-!-view (sendR' P Q m' p) = {!\u214b\u1d3e-sendL (\u03a3\u1d3e _ P) m'!}\n \u214b\u1d3e-!-view (recvL' P Q pq) = {!!}\n \u214b\u1d3e-!-view (recvR' P Q pq) = {!!}\n \u214b\u1d3e-!-view (endL Q pq) = {!!}\n \u214b\u1d3e-!-view (endR P pq) = {!!}\n\n {-\n end Q p = \u214b\u1d3e-rend' Q p\n {-\n \u214b\u1d3e-! end Q p = \u214b\u1d3e-rend' Q p\n \u214b\u1d3e-! (\u03a0\u1d3e M P) end p x = \u214b\u1d3e-! (P x) end (p x)\n \u214b\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 {!\u214b\u1d3e-! (\u03a0 !}\n \u214b\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 \u214b\u1d3e-! (P m) (com (mk Out M' Q)) (p m)\n \u214b\u1d3e-! (\u03a3\u1d3e M P) end p = p\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 \u214b\u1d3e-! (com (mk Out M P)) (Q m') (p m')\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (\u214b\u1d3e-! (P m) (com (mk Out M' Q)) p)\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (\u214b\u1d3e-! (com (mk Out M P)) (Q m) p)\n\n {-\n \u214b\u1d3e-apply : \u2200 P Q \u2192 \u27e6 \u214b\u1d3e P Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply end Q s p = s\n \u214b\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = \u214b\u1d3e-apply (P m) Q (s m) p\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = \u214b\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = \u214b\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , \u214b\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n -}\n\n {-\nmodule V4\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n P \u214b\u1d3e end = P\n com P \u214b\u1d3e com Q = \u03a3\u1d3e LR (P \u214b\u1d9c Q)\n\n _\u214b\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 \u214b\u1d9c P\u1d3f) `L = P\u1d38 \u214b\u1d9cL P\u1d3f\n (P\u1d38 \u214b\u1d9c P\u1d3f) `R = P\u1d38 \u214b\u1d9cR P\u1d3f\n\n _\u214b\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) \u214b\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m \u214b\u1d3e com Q)\n\n _\u214b\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P \u214b\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P \u214b\u1d3e P\u1d3f m)\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end ox\u1d3e Q = Q\n P ox\u1d3e end = P\n com P\u1d38 ox\u1d3e com P\u1d3f = \u03a0\u1d3e LR (P\u1d38 ox\u1d9c P\u1d3f)\n\n _ox\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 ox\u1d9c P\u1d3f) `L = P\u1d38 ox\u1d9cL P\u1d3f\n (P\u1d38 ox\u1d9c P\u1d3f) `R = P\u1d38 ox\u1d9cR P\u1d3f\n\n _ox\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) ox\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m ox\u1d3e com Q)\n\n _ox\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P ox\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P ox\u1d3e P\u1d3f m)\n\n data Viewox : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n com : \u2200{P Q}(p : \u27e6 \u03a0\u1d3e LR (P ox\u1d9c Q) \u27e7) \u2192 Viewox (com P) (com Q) p\n endL : \u2200{Q}(p : \u27e6 Q \u27e7) \u2192 Viewox end Q p\n endR : \u2200{P}(p : \u27e6 com P \u27e7) \u2192 Viewox (com P) end p\n\n viewox : \u2200 P Q (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 Viewox P Q p\n viewox end Q p = endL p\n viewox (com P) end p = endR p\n viewox (com P) (com Q) p = com p\n\n data View-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u2605\u2081 where\n sendLL : \u2200 {M} (P : M \u2192 Proto) Q R (m : M)(p : \u27e6 P m \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e M P) (com Q) R (`L , m , p) q\n recvLL : \u2200 {M} (P : M \u2192 Proto) Q R\n (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e com Q \u27e7))(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a0\u1d3e M P) (com Q) R (`L , p) q\n sendRR : \u2200 {M} P Q (R : M \u2192 Proto)(m : M)(p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a3\u1d3e M R) p (`R , m , q)\n recvRR : \u2200 {M} P Q (R : M \u2192 Proto)\n (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : (m : M) \u2192 \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a0\u1d3e M R) p (`R , q)\n\n sendR-recvL : \u2200 {M} P (Q : M \u2192 Proto) R (m : M)(p : \u27e6 com P \u214b\u1d3e Q m \u27e7)(q : (m : M) \u2192 \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a3\u1d3e M Q) (com R) (`R , m , p) (`L , q)\n recvR-sendL : \u2200 {M} P (Q : M \u2192 Proto) R (p : (m : M) \u2192 \u27e6 com P \u214b\u1d3e Q m \u27e7)(m : M)(q : \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a0\u1d3e M Q) (com R) (`R , p) (`L , m , q)\n\n endL : \u2200 Q R\n \u2192 (q : \u27e6 Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 end Q R q qr\n\n endM : \u2200 P R\n \u2192 (p : \u27e6 com P \u27e7)(r : \u27e6 R \u27e7)\n \u2192 View-\u2218 (com P) end R p r\n endR : \u2200 P Q\n \u2192 (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u27e7)\n \u2192 View-\u2218 (com P) (com Q) end p q\n\n view-\u2218 : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7) \u2192 View-\u2218 P Q R pq qr\n view-\u2218 P Q R pq qr = view-\u2218-view (view-\u214b P Q pq) (view-\u214b (dual Q) R qr)\n where\n view-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u214b P Q pq \u2192 View-\u214b (dual Q) R qr \u2192 View-\u2218 P Q R pq qr\n view-\u2218-view (sendL' _ _ _ _) _ = sendLL _ _ _ _ _ _\n view-\u2218-view (recvL' _ _ _) _ = recvLL _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvL' ._ _ _) = sendR-recvL _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (endR ._ _) = endR _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendL' ._ _ _ _) = recvR-sendL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (endR ._ ._) = endR _ _ _ _\n view-\u2218-view (endL _ _) _ = endL _ _ _ _\n view-\u2218-view (endR _ _) _ = endM _ _ _ _\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end = id\n \u214b\u1d3e-rend (com _) = id\n\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (com P) m p = `R , (m , p)\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL (com _) m p = `L , (m , p)\n\n \u214b\u1d3e-recvR : \u2200 {M}P{Q : M \u2192 Proto} \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e com' In M Q \u27e7\n \u214b\u1d3e-recvR end f = f\n \u214b\u1d3e-recvR (com _) f = `R , f\n\n \u214b\u1d3e-recvL : \u2200 {M}{P : M \u2192 Proto}Q \u2192 ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7) \u2192 \u27e6 \u03a0\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-recvL {P = P} end f x = \u214b\u1d3e-rend (P x) (f x)\n \u214b\u1d3e-recvL (com _) f = `L , f\n\n ox\u1d3e-rend : \u2200 P \u2192 \u27e6 P ox\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-rend end = id\n ox\u1d3e-rend (com x) = id\n\n ox\u1d3e-sendR : \u2200 {M P}{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n ox\u1d3e-sendR {P = end} m p = m , p\n ox\u1d3e-sendR {P = com x} m p = `R , (m , p)\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id end = _\n \u214b\u1d3e-id (com (mk In M P)) = `R , \u03bb m \u2192 \u214b\u1d3e-sendL (P m) m (\u214b\u1d3e-id (P m))\n \u214b\u1d3e-id (com (mk Out M P)) = `L , \u03bb m \u2192 \u214b\u1d3e-sendR (dual (P m)) m (\u214b\u1d3e-id (P m))\n\n module _ where\n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm = \u03bb P Q \u2192 to P Q , equiv P Q\n where\n to : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n to end end pq = pq\n to end (com x) pq = pq\n to (com x) end pq = pq\n to (com (mk In M P)) (com x\u2081) (`L , pq) = `R , (\u03bb m \u2192 to (P m) (com x\u2081) (pq m))\n to (com (mk Out M P)) (com x\u2081) (`L , m , pq) = `R , m , to (P m) (com x\u2081) pq\n to (com x) (com (mk In M P)) (`R , pq) = `L , (\u03bb m \u2192 to (com x) (P m) (pq m))\n to (com x) (com (mk Out M P)) (`R , m , pq) = `L , m , to (com x) (P m) pq\n\n toto : \u2200 P Q (x : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 to Q P (to P Q x) \u2261 x\n toto end end x = refl\n toto end (com (mk io M P)) x\u2081 = refl\n toto (com (mk io M P)) end x\u2081 = refl\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' In M\u2081 P\u2081) (pq x))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' Out M\u2081 P\u2081) (pq x))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' In M\u2081 P\u2081) pq))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' Out M\u2081 P\u2081) pq))\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' In M P) (P\u2081 x) (pq x)))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' In M P) (P\u2081 m) pq))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' Out M P) (P\u2081 x) (pq x)))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' Out M P) (P\u2081 m) pq))\n\n equiv : \u2200 P Q \u2192 Equiv (to P Q)\n equiv P Q = record { linv = to Q P ; is-linv = toto P Q ; rinv = to Q P ; is-rinv = toto Q P }\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e end Q p q = q\n comma\u1d3e (com _) end p q = p\n comma\u1d3e (\u03a3\u1d3e M P) (com Q) (m , p) q `L = m , comma\u1d3e (P m) (com Q) p q\n comma\u1d3e (\u03a0\u1d3e M P) (com Q) p q `L = \u03bb m \u2192 comma\u1d3e (P m) (com Q) (p m) q\n comma\u1d3e (com P) (\u03a3\u1d3e M Q) p (m , q) `R = m , comma\u1d3e (com P) (Q m) p q\n comma\u1d3e (com P) (\u03a0\u1d3e M Q) p q `R = \u03bb m \u2192 comma\u1d3e (com P) (Q m) p (q m)\n\n \u00d7\u2192ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n \u00d7\u2192ox\u1d3e P Q (p , q) = comma\u1d3e P Q p q\n\n ox\u1d3e-fst : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-fst end Q pq = _\n ox\u1d3e-fst (com _) end pq = pq\n ox\u1d3e-fst (\u03a0\u1d3e M P) (com Q) pq = \u03bb m \u2192 ox\u1d3e-fst (P m) (com Q) (pq `L m)\n ox\u1d3e-fst (\u03a3\u1d3e M P) (com Q) pq = \u00d7-map id (\u03bb {x} \u2192 ox\u1d3e-fst (P x) (com Q)) (pq `L)\n\n ox\u1d3e-snd : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 Q \u27e7\n ox\u1d3e-snd end Q pq = pq\n ox\u1d3e-snd (com _) end pq = _\n ox\u1d3e-snd (com P) (\u03a0\u1d3e M' Q) pq = \u03bb m' \u2192 ox\u1d3e-snd (com P) (Q m') (pq `R m')\n ox\u1d3e-snd (com P) (\u03a3\u1d3e M' Q) pq = \u00d7-map id (\u03bb {m'} \u2192 ox\u1d3e-snd (com P) (Q m')) (pq `R)\n\n ox\u1d3e\u2192\u00d7 : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n ox\u1d3e\u2192\u00d7 P Q p = ox\u1d3e-fst P Q p , ox\u1d3e-snd P Q p\n\n ox\u1d3e-comma-fst : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-fst P Q (comma\u1d3e P Q p q) \u2261 p\n ox\u1d3e-comma-fst end Q p q = refl\n ox\u1d3e-comma-fst (com P) end p q = refl\n ox\u1d3e-comma-fst (com P) (com Q) p q with view-com P p\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | send P m p = cong (_,_ m) (ox\u1d3e-comma-fst (P m) (com Q) p q)\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | recv P p = funExt \u03bb m \u2192 ox\u1d3e-comma-fst (P m) (com Q) (p m) q\n\n ox\u1d3e-comma-snd : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-snd P Q (comma\u1d3e P Q p q) \u2261 q\n ox\u1d3e-comma-snd end Q p q = refl\n ox\u1d3e-comma-snd (com P) end p q = refl\n ox\u1d3e-comma-snd (com P) (com Q) p q with view-com Q q\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p (q m)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p (q m)\n\n {-\n ox\u1d3e\u2192\u00d7-rinv : \u2200 {P Q} (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 \u00d7\u2192ox\u1d3e P Q (ox\u1d3e\u2192\u00d7 P Q p) \u2261 p\n ox\u1d3e\u2192\u00d7-rinv {P} {Q} p with viewox P Q p\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk In M\u2081 P\u2081} .p = funExt \u03bb { `L \u2192 funExt \u03bb m \u2192 {!ox\u1d3e\u2192\u00d7-rinv {P m} {\u03a0\u1d3e _ P\u2081} ?!} ; `R \u2192 {!!} }\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk In M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | endL .p = refl\n ox\u1d3e\u2192\u00d7-rinv p | endR {P} .p = refl\n\n \u00d7\u2192ox\u1d3e-equiv : \u2200 P Q \u2192 Equiv (\u00d7\u2192ox\u1d3e P Q)\n \u00d7\u2192ox\u1d3e-equiv P Q = record { linv = ox\u1d3e\u2192\u00d7 P Q\n ; is-linv = \u03bb { (x , y) \u2192 cong\u2082 _,_ (ox\u1d3e-comma-fst P Q x y) (ox\u1d3e-comma-snd P Q x y) }\n ; rinv = ox\u1d3e\u2192\u00d7 P Q\n ; is-rinv = ox\u1d3e\u2192\u00d7-rinv {P} {Q} }\n -}\n\n -- left-biased strategy\n par : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par P Q p q = par-view (view-proc P p)\n where par-view : \u2200 {P} {p : \u27e6 P \u27e7} \u2192 ViewProc P p \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par-view (send M P m p) = \u214b\u1d3e-sendL Q m (par (P m) Q p q)\n par-view (recv M P p) = \u214b\u1d3e-recvL Q \u03bb m \u2192 par (P m) Q (p m) q\n par-view end = q\n\n \u214b\u1d3e-apply : \u2200 {P Q} \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply {P} {Q} pq p with view-\u214b P Q pq\n \u214b\u1d3e-apply ._ p | sendL' P Q m pq = \u214b\u1d3e-apply {P m} pq (p m)\n \u214b\u1d3e-apply ._ p | sendR' P Q m pq = m , \u214b\u1d3e-apply {com P} {Q m} pq p\n \u214b\u1d3e-apply ._ (m , p) | recvL' P Q pq = \u214b\u1d3e-apply {P m} (pq m) p\n \u214b\u1d3e-apply ._ p | recvR' P Q pq = \u03bb m \u2192 \u214b\u1d3e-apply {com P} {Q m} (pq m) p\n \u214b\u1d3e-apply pq p | endL Q .pq = pq\n \u214b\u1d3e-apply pq p | endR P .pq = _\n\n \u214b\u1d3e-apply' : \u2200 {P Q} \u2192 \u27e6 dual P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply' {P} {Q} pq p = \u214b\u1d3e-apply {dual P} {Q} pq (subst \u27e6_\u27e7 (\u2261.sym (\u2261\u1d3e-sound funExt (dual-involutive P))) p)\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = \u214b\u1d3e-\u2218-view (view-\u2218 P Q R pq qr)\n where\n \u214b\u1d3e-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u2218 P Q R pq qr \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218-view (sendLL P Q R m p qr) = \u214b\u1d3e-sendL R m (\u214b\u1d3e-\u2218 (P m) (com Q) R p qr)\n \u214b\u1d3e-\u2218-view (recvLL P Q R p qr) = \u214b\u1d3e-recvL R (\u03bb m \u2192 \u214b\u1d3e-\u2218 (P m) (com Q) R (p m) qr)\n \u214b\u1d3e-\u2218-view (sendRR P Q R m pq q) = \u214b\u1d3e-sendR (com P) m (\u214b\u1d3e-\u2218 (com P) (com Q) (R m) pq q)\n \u214b\u1d3e-\u2218-view (recvRR P Q R pq q) = \u214b\u1d3e-recvR (com P) (\u03bb m\u2081 \u2192 \u214b\u1d3e-\u2218 (com P) (com Q) (R m\u2081) pq (q m\u2081))\n \u214b\u1d3e-\u2218-view (sendR-recvL P Q R m p q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) p (q m)\n \u214b\u1d3e-\u2218-view (recvR-sendL P Q R p m q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) (p m) q\n \u214b\u1d3e-\u2218-view (endL Q R pq qr) = \u214b\u1d3e-apply' {Q} {R} qr pq\n \u214b\u1d3e-\u2218-view (endM P R pq qr) = par (com P) R pq qr\n \u214b\u1d3e-\u2218-view (endR P Q pq qr) = \u214b\u1d3e-apply {com Q} {com P} (fst (\u214b\u1d3e-comm (com P) (com Q)) pq) qr\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map P Q R S f g p = comma\u1d3e Q S (f (ox\u1d3e-fst P R p)) (g (ox\u1d3e-snd P R p))\n\n {-\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = switchL' P Q R (ox\u1d3e-fst (P \u214b\u1d3e Q) R pqr) (ox\u1d3e-snd (P \u214b\u1d3e Q) R pqr)\n -}\n\n {-\n switchL' : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7) (r : \u27e6 R \u27e7) \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL' P Q R pq r = {!switchL-view {!!}!}\n where\n switchL-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{r : \u27e6 R \u27e7} \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL-view {P} {Q} {R} {p\u214bq} {r} with view-\u214b P Q p\u214bq | view-proc R r\n switchL-view | sendL' P\u2081 Q\u2081 m p | vr = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | send M\u2081 P\u2082 m\u2081 p\u2081 = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | end = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | send M\u2081 P\u2082 m p\u2081 = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | end = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | send M\u2081 P\u2082 m p\u2081 = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | end = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | send M P\u2081 m p = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | recv M P\u2081 r\u2081 = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | end = {!!}\n switchL-view | endR P\u2081 pq\u2081 | send M P\u2082 m p = {!!}\n switchL-view | endR P\u2081 pq\u2081 | recv M P\u2082 r\u2081 = {!!}\n switchL-view | endR P\u2081 pq\u2081 | end = {!!}\n {-\n switchL-view {R = R}{r = r} | sendL' P Q m p = \u214b\u1d3e-sendL {!!} m (switchL' {!!} {!!} {!!} p r)\n switchL-view {R = R}{r = r} | sendR' P Q m p with view-proc R r\n switchL-view | sendR' {M\u2081} P\u2081 Q m\u2081 p\u2081 | send M P m p = {!!}\n switchL-view | sendR' {M\u2081} P\u2081 Q m p | recv M P r = {!!}\n switchL-view | sendR' {M} P Q m p | end = {!!}\n -- {!\u214b\u1d3e-map (com P) (com P) (Q m ox\u1d3e R) (\u03a3\u1d3e M Q ox\u1d3e R) id (ox\u1d3e-map (Q m) (\u03a3\u1d3e M Q) R R (_,_ m) id) (switchL-view (com P) (Q m) R p r)!}\n switchL-view {R = R}{r = r} | recvL' P Q p = \u214b\u1d3e-recvL (com Q ox\u1d3e R) \u03bb m \u2192 switchL' (P m) (com Q) R (p m) r\n switchL-view {R = R}{r = r} | recvR' (\u03a0\u1d9c M' P) Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL-view {R = R}{r = r} | recvR' (\u03a3\u1d9c M' P) Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL-view {R = R}{r = r} | endL Q p\u214bq = comma\u1d3e Q R p\u214bq r\n switchL-view {R = R}{r = r} | endR P p\u214bq = par (com P) R p\u214bq r\n -}\n -}\n\n \u2295\u1d3e-map : \u2200 {P Q R S} \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map f g (`L , pr) = `L , f pr\n \u2295\u1d3e-map f g (`R , pr) = `R , g pr\n\n &\u1d3e-map : \u2200 {P Q R S} \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map f g p `L = {!!}\n &\u1d3e-map f g p `R = {!!}\n -}\n\n {-\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Tele B \u00d7 Tele E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a3\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a0\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\nopen import Data.One hiding (_\u225f_)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP as \u2261\n\nmodule Control.Protocol.Choreography where\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\n {-\ndata S<_> {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n\npattern com' q M P = com (mk q M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\nopen ProtoRel _\u2261_ public renaming (_\u2248\u1d3e_ to _\u2261\u1d3e_)\n\n{-\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n -}\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n Trace : Proto \u2192 Proto\n Trace end = end\n Trace (com c) = com (Trace\u1d9c c)\n\n Trace\u1d9c : Com \u2192 Com\n Trace\u1d9c c = \u03a3\u1d9c (Com.M c) \u03bb m \u2192 Trace (Com.P c m)\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c c = mk (dual\u1d35\u1d3c (Com.io c)) (Com.M c) \u03bb m \u2192 dual (Com.P c m)\n\ndata IsTrace : Proto \u2192 \u2605\u2081 where\n end : IsTrace end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsTrace (P m)) \u2192 IsTrace (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 (M : \u2605) (P : M \u2192 \u2605) \u2192 \u2605\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : Proto \u2192 \u2605\n\u27e6 end \u27e7 = \ud835\udfd9\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\nmutual\n data Dual\u1d9c : Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a0\u1d9c M P) (\u03a3\u1d9c M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual\u1d9c (\u03a3\u1d9c M P) (\u03a0\u1d9c M Q)\n\n data Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n {-\n \u03a0\u2610\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P [ x ]) (Q x)) \u2192 Dual (\u03a0\u1d3e (\u2610 M) P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0\u2610 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q [ x ])) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e (\u2610 M) Q)\n -}\n\n {-\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Trace : Choreo I \u2192 Proto\n \u2102Trace \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Trace-IsTrace : \u2200 \u2102 \u2192 IsTrace (\u2102Trace \u2102)\n \u2102Trace-IsTrace (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Trace-IsTrace (\u2102 m)\n \u2102Trace-IsTrace end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Trace \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n-}\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com refl M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com refl M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\ndual\u1d35\u1d3c-involutive : \u2200 io \u2192 dual\u1d35\u1d3c (dual\u1d35\u1d3c io) \u2261 io\ndual\u1d35\u1d3c-involutive In = refl\ndual\u1d35\u1d3c-involutive Out = refl\n\ndual-involutive : \u2200 P \u2192 dual (dual P) \u2261\u1d3e P\ndual-involutive end = end\ndual-involutive (com' q M P) = com (dual\u1d35\u1d3c-involutive q) M (\u03bb m \u2192 dual-involutive (P m))\n\nTrace-idempotent : \u2200 P \u2192 Trace (Trace P) \u2261\u1d3e Trace P\nTrace-idempotent end = end\nTrace-idempotent (com' _ M P) = com refl M \u03bb m \u2192 Trace-idempotent (P m)\n\nTrace-dual-oblivious : \u2200 P \u2192 Trace (dual P) \u2261\u1d3e Trace P\nTrace-dual-oblivious end = end\nTrace-dual-oblivious (com' _ M P) = com refl M \u03bb m \u2192 Trace-dual-oblivious (P m)\n\nSink : Proto \u2192 Proto\nSink = dual \u2218 Trace\n\nTele : Proto \u2192 \u2605\nTele P = \u27e6 Trace P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n{-\n++Tele : \u2200 (P : Proto){Q : Tele P \u2192 Proto} (xs : Tele P) \u2192 Tele (Q xs) \u2192 Tele (P >>= Q)\n++Tele end _ ys = ys\n++Tele (com' q M P) (x , xs) ys = x , ++Tele (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com q M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Tele P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com q M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n{-\nDual-sym (\u03a0\u2610\u00b7\u03a3 f) = {!\u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))!}\nDual-sym (\u03a3\u00b7\u03a0\u2610 f) = {!\u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))!}\n-}\n-}\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele (dual P) \u2261 Tele P\n dual-Tele P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound funExt (Trace-dual-oblivious P))\n\nEl : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n El->>= : (P : Proto){Q : Tele P \u2192 Proto}{X : Tele (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Tele P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n-}\n\n>>=-com : (P : Proto){Q : Tele P \u2192 Proto}{R : Tele P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Tele P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Tele P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecvS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecvS = recv \u2218 un\u2610\n\nsendS : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsendS m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : \u2605_ a} (l r : A) \u2192 LR \u2192 A \n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a0\u1d3e LR [L: l R: r ]\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP >>\u1d9c S = record P { P = \u03bb m \u2192 S (Com.P P m) }\n\nmodule Equivalences\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n record Equiv {A B : \u2605}(f : A \u2192 B) : \u2605 where\n field\n linv : B \u2192 A\n is-linv : \u2200 x \u2192 linv (f x) \u2261 x\n rinv : B \u2192 A\n is-rinv : \u2200 x \u2192 f (rinv x) \u2261 x\n\n id\u1d31 : \u2200 {A} \u2192 Equiv {A} id\n id\u1d31 = record\n { linv = id\n ; is-linv = \u03bb _ \u2192 refl\n ; rinv = id\n ; is-rinv = \u03bb _ \u2192 refl\n }\n\n module _ {A B C}{g : B \u2192 C}{f : A \u2192 B} where\n _\u2218\u1d31_ : Equiv g \u2192 Equiv f \u2192 Equiv (g \u2218 f)\n g\u1d31 \u2218\u1d31 f\u1d31 = record { linv = F.linv \u2218 G.linv ; is-linv = \u03bb x \u2192 cong F.linv (G.is-linv (f x)) \u2219 F.is-linv x\n ; rinv = F.rinv \u2218 G.rinv ; is-rinv = \u03bb x \u2192 cong g (F.is-rinv _) \u2219 G.is-rinv x }\n where\n module G = Equiv g\u1d31\n module F = Equiv f\u1d31\n\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = \u03a3 (A \u2192 B) Equiv\n\n module _ {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b} where\n \u03a3-ext : \u2200 {x y : \u03a3 A B} \u2192 (p : proj\u2081 x \u2261 proj\u2081 y) \u2192 subst B p (proj\u2082 x) \u2261 proj\u2082 y \u2192 x \u2261 y\n \u03a3-ext refl = cong (_,_ _)\n\ndata ViewProc : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200 M(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewProc (\u03a3\u1d3e M P) (m , p)\n recv : \u2200 M(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewProc (\u03a0\u1d3e M P) p\n end : ViewProc end _\n\nview-proc : \u2200 P (p : \u27e6 P \u27e7) \u2192 ViewProc P p\nview-proc end _ = end\nview-proc (\u03a0\u1d3e M P) p = recv _ _ p\nview-proc (\u03a3\u1d3e M P) (m , p) = send _ _ m p\n\ndata ViewCom : \u2200 P \u2192 \u27e6 com P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200{M}(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewCom (\u03a3\u1d9c M P) (m , p)\n recv : \u2200{M}(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewCom (\u03a0\u1d9c M P) p\n\nview-com : \u2200 P (p : \u27e6 com P \u27e7) \u2192 ViewCom P p\nview-com (\u03a0\u1d9c M P) p = recv _ p\nview-com (\u03a3\u1d9c M P) (m , p) = send _ m p\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open Equivalences funExt\n\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n \u03a0\u1d3e M P \u214b\u1d3e Q = \u03a0\u1d3e M \u03bb m \u2192 P m \u214b\u1d3e Q\n P \u214b\u1d3e end = P\n P \u214b\u1d3e \u03a0\u1d3e M Q = \u03a0\u1d3e M \u03bb m \u2192 P \u214b\u1d3e Q m\n \u03a3\u1d3e M P \u214b\u1d3e \u03a3\u1d3e M' Q = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 P m \u214b\u1d3e \u03a3\u1d3e M' Q)\n ,inr: (\u03bb m' \u2192 \u03a3\u1d3e M P \u214b\u1d3e Q m') ]\n\n data View-\u214b : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n sendL' : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto)(m : M )(p : \u27e6 P m \u214b\u1d3e \u03a3\u1d3e M' Q \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p)\n sendR' : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto)(m' : M')(p : \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q m' \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m' , p)\n recvL' : \u2200 {M} (P : M \u2192 Proto) Q (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7)) \u2192 View-\u214b (\u03a0\u1d3e M P) Q p\n recvR' : \u2200 {M M'} (P : M \u2192 Proto) (Q : M' \u2192 Proto)(p : (m' : M') \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q m' \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p\n endL : \u2200 Q (p : \u27e6 Q \u27e7) \u2192 View-\u214b end Q p\n endR : \u2200 {M} (P : M \u2192 Proto) (p : \u27e6 \u03a3\u1d3e M P \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) end p\n\n view-\u214b : \u2200 P Q (p : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 View-\u214b P Q p\n view-\u214b end Q p = endL Q p\n view-\u214b (\u03a0\u1d3e M P) Q p = recvL' P Q p\n view-\u214b (\u03a3\u1d3e M P) end p = endR P p\n view-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = recvR' P Q p\n view-\u214b (com (mk Out M P)) (com (mk Out M' Q)) (inl x , p) = sendL' P Q x p\n view-\u214b (com (mk Out M P)) (com (mk Out M' Q)) (inr y , p) = sendR' P Q y p\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7 \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7\n \u214b\u1d3e-assoc end Q R s = s\n \u214b\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = \u214b\u1d3e-assoc (P m) _ _ (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , \u214b\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end p = p\n \u214b\u1d3e-rend (\u03a0\u1d3e _ P) p = \u03bb m \u2192 \u214b\u1d3e-rend (P m) (p m)\n \u214b\u1d3e-rend (\u03a3\u1d3e _ _) p = p\n\n \u214b\u1d3e-rend' : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u214b\u1d3e end \u27e7\n \u214b\u1d3e-rend' end p = p\n \u214b\u1d3e-rend' (\u03a0\u1d3e _ P) p = \u03bb m \u2192 \u214b\u1d3e-rend' (P m) (p m)\n \u214b\u1d3e-rend' (\u03a3\u1d3e _ _) p = p\n\n {-\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n -}\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (\u03a3\u1d3e M P) m p = inr m , p\n \u214b\u1d3e-sendR (\u03a0\u1d3e M P) m p = \u03bb x \u2192 \u214b\u1d3e-sendR (P x) m (p x)\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL (\u03a0\u1d3e M Q) m p = \u03bb m' \u2192 \u214b\u1d3e-sendL (Q m') m {!p!}\n \u214b\u1d3e-sendL (\u03a3\u1d3e M Q) m p = {!!} , p\n\n {-\n \u214b\u1d3e-sendR : \u2200 {M} P Q \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e M Q \u27e7 \u2192 (m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7\n \u214b\u1d3e-sendR P Q s m = {!!}\n -}\n\n \u214b\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e M Q \u27e7\n \u214b\u1d3e-recvR end Q s = s\n \u214b\u1d3e-recvR (\u03a0\u1d3e M P) Q s = \u03bb x \u2192 \u214b\u1d3e-recvR (P x) Q (\u03bb m \u2192 s m x)\n \u214b\u1d3e-recvR (\u03a3\u1d3e M P) Q s = s\n\n mutual\n \u214b\u1d3e-! : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-! P Q p = {!!}\n\n \u214b\u1d3e-!-view : \u2200 {P Q} {pq : \u27e6 P \u214b\u1d3e Q \u27e7} \u2192 View-\u214b P Q pq \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-!-view (sendL' P Q m p) = \u214b\u1d3e-sendR (\u03a3\u1d3e _ Q) m (\u214b\u1d3e-! (P m) (com (mk Out _ Q)) p)\n \u214b\u1d3e-!-view (sendR' P Q m' p) = {!\u214b\u1d3e-sendL (\u03a3\u1d3e _ P) m'!}\n \u214b\u1d3e-!-view (recvL' P Q pq) = {!!}\n \u214b\u1d3e-!-view (recvR' P Q pq) = {!!}\n \u214b\u1d3e-!-view (endL Q pq) = {!!}\n \u214b\u1d3e-!-view (endR P pq) = {!!}\n\n {-\n end Q p = \u214b\u1d3e-rend' Q p\n {-\n \u214b\u1d3e-! end Q p = \u214b\u1d3e-rend' Q p\n \u214b\u1d3e-! (\u03a0\u1d3e M P) end p x = \u214b\u1d3e-! (P x) end (p x)\n \u214b\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 {!\u214b\u1d3e-! (\u03a0 !}\n \u214b\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 \u214b\u1d3e-! (P m) (com (mk Out M' Q)) (p m)\n \u214b\u1d3e-! (\u03a3\u1d3e M P) end p = p\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 \u214b\u1d3e-! (com (mk Out M P)) (Q m') (p m')\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (\u214b\u1d3e-! (P m) (com (mk Out M' Q)) p)\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (\u214b\u1d3e-! (com (mk Out M P)) (Q m) p)\n\n {-\n \u214b\u1d3e-apply : \u2200 P Q \u2192 \u27e6 \u214b\u1d3e P Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply end Q s p = s\n \u214b\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = \u214b\u1d3e-apply (P m) Q (s m) p\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = \u214b\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = \u214b\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , \u214b\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n -}\n\n {-\nmodule V4\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n P \u214b\u1d3e end = P\n com P \u214b\u1d3e com Q = \u03a3\u1d3e LR (P \u214b\u1d9c Q)\n\n _\u214b\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 \u214b\u1d9c P\u1d3f) `L = P\u1d38 \u214b\u1d9cL P\u1d3f\n (P\u1d38 \u214b\u1d9c P\u1d3f) `R = P\u1d38 \u214b\u1d9cR P\u1d3f\n\n _\u214b\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) \u214b\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m \u214b\u1d3e com Q)\n\n _\u214b\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P \u214b\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P \u214b\u1d3e P\u1d3f m)\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end ox\u1d3e Q = Q\n P ox\u1d3e end = P\n com P\u1d38 ox\u1d3e com P\u1d3f = \u03a0\u1d3e LR (P\u1d38 ox\u1d9c P\u1d3f)\n\n _ox\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 ox\u1d9c P\u1d3f) `L = P\u1d38 ox\u1d9cL P\u1d3f\n (P\u1d38 ox\u1d9c P\u1d3f) `R = P\u1d38 ox\u1d9cR P\u1d3f\n\n _ox\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) ox\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m ox\u1d3e com Q)\n\n _ox\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P ox\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P ox\u1d3e P\u1d3f m)\n\n data Viewox : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n com : \u2200{P Q}(p : \u27e6 \u03a0\u1d3e LR (P ox\u1d9c Q) \u27e7) \u2192 Viewox (com P) (com Q) p\n endL : \u2200{Q}(p : \u27e6 Q \u27e7) \u2192 Viewox end Q p\n endR : \u2200{P}(p : \u27e6 com P \u27e7) \u2192 Viewox (com P) end p\n\n viewox : \u2200 P Q (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 Viewox P Q p\n viewox end Q p = endL p\n viewox (com P) end p = endR p\n viewox (com P) (com Q) p = com p\n\n data View-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u2605\u2081 where\n sendLL : \u2200 {M} (P : M \u2192 Proto) Q R (m : M)(p : \u27e6 P m \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e M P) (com Q) R (`L , m , p) q\n recvLL : \u2200 {M} (P : M \u2192 Proto) Q R\n (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e com Q \u27e7))(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a0\u1d3e M P) (com Q) R (`L , p) q\n sendRR : \u2200 {M} P Q (R : M \u2192 Proto)(m : M)(p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a3\u1d3e M R) p (`R , m , q)\n recvRR : \u2200 {M} P Q (R : M \u2192 Proto)\n (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : (m : M) \u2192 \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a0\u1d3e M R) p (`R , q)\n\n sendR-recvL : \u2200 {M} P (Q : M \u2192 Proto) R (m : M)(p : \u27e6 com P \u214b\u1d3e Q m \u27e7)(q : (m : M) \u2192 \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a3\u1d3e M Q) (com R) (`R , m , p) (`L , q)\n recvR-sendL : \u2200 {M} P (Q : M \u2192 Proto) R (p : (m : M) \u2192 \u27e6 com P \u214b\u1d3e Q m \u27e7)(m : M)(q : \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a0\u1d3e M Q) (com R) (`R , p) (`L , m , q)\n\n endL : \u2200 Q R\n \u2192 (q : \u27e6 Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 end Q R q qr\n\n endM : \u2200 P R\n \u2192 (p : \u27e6 com P \u27e7)(r : \u27e6 R \u27e7)\n \u2192 View-\u2218 (com P) end R p r\n endR : \u2200 P Q\n \u2192 (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u27e7)\n \u2192 View-\u2218 (com P) (com Q) end p q\n\n view-\u2218 : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7) \u2192 View-\u2218 P Q R pq qr\n view-\u2218 P Q R pq qr = view-\u2218-view (view-\u214b P Q pq) (view-\u214b (dual Q) R qr)\n where\n view-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u214b P Q pq \u2192 View-\u214b (dual Q) R qr \u2192 View-\u2218 P Q R pq qr\n view-\u2218-view (sendL' _ _ _ _) _ = sendLL _ _ _ _ _ _\n view-\u2218-view (recvL' _ _ _) _ = recvLL _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvL' ._ _ _) = sendR-recvL _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (endR ._ _) = endR _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendL' ._ _ _ _) = recvR-sendL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (endR ._ ._) = endR _ _ _ _\n view-\u2218-view (endL _ _) _ = endL _ _ _ _\n view-\u2218-view (endR _ _) _ = endM _ _ _ _\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end = id\n \u214b\u1d3e-rend (com _) = id\n\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (com P) m p = `R , (m , p)\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL (com _) m p = `L , (m , p)\n\n \u214b\u1d3e-recvR : \u2200 {M}P{Q : M \u2192 Proto} \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e com' In M Q \u27e7\n \u214b\u1d3e-recvR end f = f\n \u214b\u1d3e-recvR (com _) f = `R , f\n\n \u214b\u1d3e-recvL : \u2200 {M}{P : M \u2192 Proto}Q \u2192 ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7) \u2192 \u27e6 \u03a0\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-recvL {P = P} end f x = \u214b\u1d3e-rend (P x) (f x)\n \u214b\u1d3e-recvL (com _) f = `L , f\n\n ox\u1d3e-rend : \u2200 P \u2192 \u27e6 P ox\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-rend end = id\n ox\u1d3e-rend (com x) = id\n\n ox\u1d3e-sendR : \u2200 {M P}{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n ox\u1d3e-sendR {P = end} m p = m , p\n ox\u1d3e-sendR {P = com x} m p = `R , (m , p)\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id end = _\n \u214b\u1d3e-id (com (mk In M P)) = `R , \u03bb m \u2192 \u214b\u1d3e-sendL (P m) m (\u214b\u1d3e-id (P m))\n \u214b\u1d3e-id (com (mk Out M P)) = `L , \u03bb m \u2192 \u214b\u1d3e-sendR (dual (P m)) m (\u214b\u1d3e-id (P m))\n\n module _ where\n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm = \u03bb P Q \u2192 to P Q , equiv P Q\n where\n to : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n to end end pq = pq\n to end (com x) pq = pq\n to (com x) end pq = pq\n to (com (mk In M P)) (com x\u2081) (`L , pq) = `R , (\u03bb m \u2192 to (P m) (com x\u2081) (pq m))\n to (com (mk Out M P)) (com x\u2081) (`L , m , pq) = `R , m , to (P m) (com x\u2081) pq\n to (com x) (com (mk In M P)) (`R , pq) = `L , (\u03bb m \u2192 to (com x) (P m) (pq m))\n to (com x) (com (mk Out M P)) (`R , m , pq) = `L , m , to (com x) (P m) pq\n\n toto : \u2200 P Q (x : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 to Q P (to P Q x) \u2261 x\n toto end end x = refl\n toto end (com (mk io M P)) x\u2081 = refl\n toto (com (mk io M P)) end x\u2081 = refl\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' In M\u2081 P\u2081) (pq x))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' Out M\u2081 P\u2081) (pq x))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' In M\u2081 P\u2081) pq))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' Out M\u2081 P\u2081) pq))\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' In M P) (P\u2081 x) (pq x)))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' In M P) (P\u2081 m) pq))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' Out M P) (P\u2081 x) (pq x)))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' Out M P) (P\u2081 m) pq))\n\n equiv : \u2200 P Q \u2192 Equiv (to P Q)\n equiv P Q = record { linv = to Q P ; is-linv = toto P Q ; rinv = to Q P ; is-rinv = toto Q P }\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e end Q p q = q\n comma\u1d3e (com _) end p q = p\n comma\u1d3e (\u03a3\u1d3e M P) (com Q) (m , p) q `L = m , comma\u1d3e (P m) (com Q) p q\n comma\u1d3e (\u03a0\u1d3e M P) (com Q) p q `L = \u03bb m \u2192 comma\u1d3e (P m) (com Q) (p m) q\n comma\u1d3e (com P) (\u03a3\u1d3e M Q) p (m , q) `R = m , comma\u1d3e (com P) (Q m) p q\n comma\u1d3e (com P) (\u03a0\u1d3e M Q) p q `R = \u03bb m \u2192 comma\u1d3e (com P) (Q m) p (q m)\n\n \u00d7\u2192ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n \u00d7\u2192ox\u1d3e P Q (p , q) = comma\u1d3e P Q p q\n\n ox\u1d3e-fst : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-fst end Q pq = _\n ox\u1d3e-fst (com _) end pq = pq\n ox\u1d3e-fst (\u03a0\u1d3e M P) (com Q) pq = \u03bb m \u2192 ox\u1d3e-fst (P m) (com Q) (pq `L m)\n ox\u1d3e-fst (\u03a3\u1d3e M P) (com Q) pq = \u00d7-map id (\u03bb {x} \u2192 ox\u1d3e-fst (P x) (com Q)) (pq `L)\n\n ox\u1d3e-snd : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 Q \u27e7\n ox\u1d3e-snd end Q pq = pq\n ox\u1d3e-snd (com _) end pq = _\n ox\u1d3e-snd (com P) (\u03a0\u1d3e M' Q) pq = \u03bb m' \u2192 ox\u1d3e-snd (com P) (Q m') (pq `R m')\n ox\u1d3e-snd (com P) (\u03a3\u1d3e M' Q) pq = \u00d7-map id (\u03bb {m'} \u2192 ox\u1d3e-snd (com P) (Q m')) (pq `R)\n\n ox\u1d3e\u2192\u00d7 : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n ox\u1d3e\u2192\u00d7 P Q p = ox\u1d3e-fst P Q p , ox\u1d3e-snd P Q p\n\n ox\u1d3e-comma-fst : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-fst P Q (comma\u1d3e P Q p q) \u2261 p\n ox\u1d3e-comma-fst end Q p q = refl\n ox\u1d3e-comma-fst (com P) end p q = refl\n ox\u1d3e-comma-fst (com P) (com Q) p q with view-com P p\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | send P m p = cong (_,_ m) (ox\u1d3e-comma-fst (P m) (com Q) p q)\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | recv P p = funExt \u03bb m \u2192 ox\u1d3e-comma-fst (P m) (com Q) (p m) q\n\n ox\u1d3e-comma-snd : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-snd P Q (comma\u1d3e P Q p q) \u2261 q\n ox\u1d3e-comma-snd end Q p q = refl\n ox\u1d3e-comma-snd (com P) end p q = refl\n ox\u1d3e-comma-snd (com P) (com Q) p q with view-com Q q\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p (q m)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p (q m)\n\n {-\n ox\u1d3e\u2192\u00d7-rinv : \u2200 {P Q} (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 \u00d7\u2192ox\u1d3e P Q (ox\u1d3e\u2192\u00d7 P Q p) \u2261 p\n ox\u1d3e\u2192\u00d7-rinv {P} {Q} p with viewox P Q p\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk In M\u2081 P\u2081} .p = funExt \u03bb { `L \u2192 funExt \u03bb m \u2192 {!ox\u1d3e\u2192\u00d7-rinv {P m} {\u03a0\u1d3e _ P\u2081} ?!} ; `R \u2192 {!!} }\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk In M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | endL .p = refl\n ox\u1d3e\u2192\u00d7-rinv p | endR {P} .p = refl\n\n \u00d7\u2192ox\u1d3e-equiv : \u2200 P Q \u2192 Equiv (\u00d7\u2192ox\u1d3e P Q)\n \u00d7\u2192ox\u1d3e-equiv P Q = record { linv = ox\u1d3e\u2192\u00d7 P Q\n ; is-linv = \u03bb { (x , y) \u2192 cong\u2082 _,_ (ox\u1d3e-comma-fst P Q x y) (ox\u1d3e-comma-snd P Q x y) }\n ; rinv = ox\u1d3e\u2192\u00d7 P Q\n ; is-rinv = ox\u1d3e\u2192\u00d7-rinv {P} {Q} }\n -}\n\n -- left-biased strategy\n par : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par P Q p q = par-view (view-proc P p)\n where par-view : \u2200 {P} {p : \u27e6 P \u27e7} \u2192 ViewProc P p \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par-view (send M P m p) = \u214b\u1d3e-sendL Q m (par (P m) Q p q)\n par-view (recv M P p) = \u214b\u1d3e-recvL Q \u03bb m \u2192 par (P m) Q (p m) q\n par-view end = q\n\n \u214b\u1d3e-apply : \u2200 {P Q} \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply {P} {Q} pq p with view-\u214b P Q pq\n \u214b\u1d3e-apply ._ p | sendL' P Q m pq = \u214b\u1d3e-apply {P m} pq (p m)\n \u214b\u1d3e-apply ._ p | sendR' P Q m pq = m , \u214b\u1d3e-apply {com P} {Q m} pq p\n \u214b\u1d3e-apply ._ (m , p) | recvL' P Q pq = \u214b\u1d3e-apply {P m} (pq m) p\n \u214b\u1d3e-apply ._ p | recvR' P Q pq = \u03bb m \u2192 \u214b\u1d3e-apply {com P} {Q m} (pq m) p\n \u214b\u1d3e-apply pq p | endL Q .pq = pq\n \u214b\u1d3e-apply pq p | endR P .pq = _\n\n \u214b\u1d3e-apply' : \u2200 {P Q} \u2192 \u27e6 dual P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply' {P} {Q} pq p = \u214b\u1d3e-apply {dual P} {Q} pq (subst \u27e6_\u27e7 (\u2261.sym (\u2261\u1d3e-sound funExt (dual-involutive P))) p)\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = \u214b\u1d3e-\u2218-view (view-\u2218 P Q R pq qr)\n where\n \u214b\u1d3e-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u2218 P Q R pq qr \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218-view (sendLL P Q R m p qr) = \u214b\u1d3e-sendL R m (\u214b\u1d3e-\u2218 (P m) (com Q) R p qr)\n \u214b\u1d3e-\u2218-view (recvLL P Q R p qr) = \u214b\u1d3e-recvL R (\u03bb m \u2192 \u214b\u1d3e-\u2218 (P m) (com Q) R (p m) qr)\n \u214b\u1d3e-\u2218-view (sendRR P Q R m pq q) = \u214b\u1d3e-sendR (com P) m (\u214b\u1d3e-\u2218 (com P) (com Q) (R m) pq q)\n \u214b\u1d3e-\u2218-view (recvRR P Q R pq q) = \u214b\u1d3e-recvR (com P) (\u03bb m\u2081 \u2192 \u214b\u1d3e-\u2218 (com P) (com Q) (R m\u2081) pq (q m\u2081))\n \u214b\u1d3e-\u2218-view (sendR-recvL P Q R m p q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) p (q m)\n \u214b\u1d3e-\u2218-view (recvR-sendL P Q R p m q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) (p m) q\n \u214b\u1d3e-\u2218-view (endL Q R pq qr) = \u214b\u1d3e-apply' {Q} {R} qr pq\n \u214b\u1d3e-\u2218-view (endM P R pq qr) = par (com P) R pq qr\n \u214b\u1d3e-\u2218-view (endR P Q pq qr) = \u214b\u1d3e-apply {com Q} {com P} (proj\u2081 (\u214b\u1d3e-comm (com P) (com Q)) pq) qr\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map P Q R S f g p = comma\u1d3e Q S (f (ox\u1d3e-fst P R p)) (g (ox\u1d3e-snd P R p))\n\n {-\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = switchL' P Q R (ox\u1d3e-fst (P \u214b\u1d3e Q) R pqr) (ox\u1d3e-snd (P \u214b\u1d3e Q) R pqr)\n -}\n\n {-\n switchL' : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7) (r : \u27e6 R \u27e7) \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL' P Q R pq r = {!switchL-view {!!}!}\n where\n switchL-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{r : \u27e6 R \u27e7} \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL-view {P} {Q} {R} {p\u214bq} {r} with view-\u214b P Q p\u214bq | view-proc R r\n switchL-view | sendL' P\u2081 Q\u2081 m p | vr = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | send M\u2081 P\u2082 m\u2081 p\u2081 = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | end = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | send M\u2081 P\u2082 m p\u2081 = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | end = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | send M\u2081 P\u2082 m p\u2081 = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | end = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | send M P\u2081 m p = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | recv M P\u2081 r\u2081 = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | end = {!!}\n switchL-view | endR P\u2081 pq\u2081 | send M P\u2082 m p = {!!}\n switchL-view | endR P\u2081 pq\u2081 | recv M P\u2082 r\u2081 = {!!}\n switchL-view | endR P\u2081 pq\u2081 | end = {!!}\n {-\n switchL-view {R = R}{r = r} | sendL' P Q m p = \u214b\u1d3e-sendL {!!} m (switchL' {!!} {!!} {!!} p r)\n switchL-view {R = R}{r = r} | sendR' P Q m p with view-proc R r\n switchL-view | sendR' {M\u2081} P\u2081 Q m\u2081 p\u2081 | send M P m p = {!!}\n switchL-view | sendR' {M\u2081} P\u2081 Q m p | recv M P r = {!!}\n switchL-view | sendR' {M} P Q m p | end = {!!}\n -- {!\u214b\u1d3e-map (com P) (com P) (Q m ox\u1d3e R) (\u03a3\u1d3e M Q ox\u1d3e R) id (ox\u1d3e-map (Q m) (\u03a3\u1d3e M Q) R R (_,_ m) id) (switchL-view (com P) (Q m) R p r)!}\n switchL-view {R = R}{r = r} | recvL' P Q p = \u214b\u1d3e-recvL (com Q ox\u1d3e R) \u03bb m \u2192 switchL' (P m) (com Q) R (p m) r\n switchL-view {R = R}{r = r} | recvR' (\u03a0\u1d9c M' P) Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL-view {R = R}{r = r} | recvR' (\u03a3\u1d9c M' P) Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL-view {R = R}{r = r} | endL Q p\u214bq = comma\u1d3e Q R p\u214bq r\n switchL-view {R = R}{r = r} | endR P p\u214bq = par (com P) R p\u214bq r\n -}\n -}\n\n \u2295\u1d3e-map : \u2200 {P Q R S} \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map f g (`L , pr) = `L , f pr\n \u2295\u1d3e-map f g (`R , pr) = `R , g pr\n\n &\u1d3e-map : \u2200 {P Q R S} \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map f g p `L = {!!}\n &\u1d3e-map f g p `R = {!!}\n -}\n\n {-\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Tele P \u00d7 Tele Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Tele B \u00d7 Tele E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _\n (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a3\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [ (\u03bb m \u2192 add\u03a0\u1d3e (P m)) , A\u1d3e ]\u2032\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"3191b7e15dd3762e428eec4635cc26f606ccab0f","subject":"Desc stratified model: descD implicit.","message":"Desc stratified model: descD implicit.\n","repos":"larrytheliquid\/pigit,mietek\/epigram2,mietek\/epigram2","old_file":"models\/IDesc.agda","new_file":"models\/IDesc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l : Level}{A B : Set l}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l : Level}{A B C : Set l}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {l : Level}{A B : Set l}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set l) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set l} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set l}(R : I -> IDesc I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set l}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set l -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const (lift I)\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : {l : Level}(I : Set l) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (\\_ -> descD I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst {l = suc x}\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst {l = suc x}\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst {l = suc x}\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst {l = suc x}\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst {l = suc x}\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n \n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\n-- desc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\n-- descD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\n-- IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l\n\n-- cases : (l : Level)\n-- (I : Set l)\n-- (xs : desc (suc l) (lift I) (descD l I) (IMu (suc l) Unit (\\ _ -> descD l I)))\n-- (hs : desc l I (box l I (descD l I) (IMu l Unit (\u03bb _ -> descD l I)) xs) (\u03bb _ -> IDesc (suc l) I)) ->\n-- IDesc (suc l) I\n-- cases x I ( lvar , i ) hs = var i\n-- cases x I ( lconst , X ) hs = const X\n-- cases x I ( lprod , (D , D') ) ( d , d' ) = prod d d'\n-- cases x I ( lpi , ( S , T ) ) hs = pi S hs\n-- cases x I ( lsigma , ( S , T ) ) hs = sigma S hs\n\n-- phi : {I : Set} -> IDescl I -> IDesc I\n-- phi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l : Level}{A B : Set l}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l : Level}{A B C : Set l}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {l : Level}{A B : Set l}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set l) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set l} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set l}(R : I -> IDesc I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set l}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set l -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const (lift I)\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : (l : Level)(I : Set l) -> IDesc Unit\ndescD x I = sigma DescDConst (descDChoice I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (\\_ -> descD x I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst {l = suc x}\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst {l = suc x}\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst {l = suc x}\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst {l = suc x}\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst {l = suc x}\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n \n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\n-- desc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\n-- descD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\n-- IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l\n\n-- cases : (l : Level)\n-- (I : Set l)\n-- (xs : desc (suc l) (lift I) (descD l I) (IMu (suc l) Unit (\\ _ -> descD l I)))\n-- (hs : desc l I (box l I (descD l I) (IMu l Unit (\u03bb _ -> descD l I)) xs) (\u03bb _ -> IDesc (suc l) I)) ->\n-- IDesc (suc l) I\n-- cases x I ( lvar , i ) hs = var i\n-- cases x I ( lconst , X ) hs = const X\n-- cases x I ( lprod , (D , D') ) ( d , d' ) = prod d d'\n-- cases x I ( lpi , ( S , T ) ) hs = pi S hs\n-- cases x I ( lsigma , ( S , T ) ) hs = sigma S hs\n\n-- phi : {I : Set} -> IDescl I -> IDesc I\n-- phi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ff2adf91d069f86e9db39f1138dcc36e94b4be7f","subject":"close #30","message":"close #30\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"canonical-indeterminate-forms.agda","new_file":"canonical-indeterminate-forms.agda","new_contents":"open import Nat\nopen import Prelude\nopen import contexts\nopen import core\nopen import type-assignment-unicity\n\nmodule canonical-indeterminate-forms where\n canonical-indeterminate-forms-base : \u2200{\u0394 d} \u2192\n \u0394 , \u2205 \u22a2 d :: b \u2192\n d indet \u2192\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] b) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] b) \u2208 \u0394))) +\n \u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c42 \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: \u03c42 ==> b) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c42) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))\n +\n (\u03a3[ d' \u2208 dhexp ]\n ((d == d' \u27e8 \u2987\u2988 \u21d2 b \u27e9) \u00d7\n (d' indet) \u00d7\n ((d'' : dhexp) (\u03c4' : htyp) \u2192 d' \u2260 (d'' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9))))\n canonical-indeterminate-forms-base TAConst ()\n canonical-indeterminate-forms-base (TAVar x\u2081) ()\n canonical-indeterminate-forms-base (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-base (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-base (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x))\n canonical-indeterminate-forms-base (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) = Inr (Inr (Inr (_ , refl , ind , x\u2081)))\n\n canonical-indeterminate-forms-arr : \u2200{\u0394 d \u03c41 \u03c42 } \u2192\n \u0394 , \u2205 \u22a2 d :: (\u03c41 ==> \u03c42) \u2192\n d indet \u2192\n ((\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] (\u03c41 ==> \u03c42)) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] (\u03c41 ==> \u03c42)) \u2208 \u0394))) +\n (\u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c4 \u2208 htyp ] \u03a3[ \u03c41' \u2208 htyp ] \u03a3[ \u03c42' \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: \u03c4 ==> (\u03c41' ==> \u03c42')) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c4) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))) +\n (\u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c41 \u2208 htyp ] \u03a3[ \u03c42 \u2208 htyp ] \u03a3[ \u03c43 \u2208 htyp ] \u03a3[ \u03c44 \u2208 htyp ]\n ((d == d' \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9) \u00d7\n (d' indet) \u00d7\n ((\u03c41 ==> \u03c42) \u2260 (\u03c43 ==> \u03c44)))) +\n (\u03a3[ d' \u2208 dhexp ]\n ((\u03c41 == \u2987\u2988) \u00d7\n (\u03c42 == \u2987\u2988) \u00d7\n (d == (d' \u27e8 \u2987\u2988 \u21d2 \u2987\u2988 ==> \u2987\u2988 \u27e9)) \u00d7\n (d' indet) \u00d7\n ((d'' : dhexp) (\u03c4' : htyp) \u2192 d' \u2260 (d'' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9)))))\n canonical-indeterminate-forms-arr (TAVar x\u2081) ()\n canonical-indeterminate-forms-arr (TALam wt) ()\n canonical-indeterminate-forms-arr (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-arr (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-arr (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x ))\n canonical-indeterminate-forms-arr (TACast wt x) (ICastArr x\u2081 ind) = Inr (Inr (Inr (Inl (_ , _ , _ , _ , _ , refl , ind , x\u2081))))\n -- todo \/ cyrus: this is the only one that required pattern matching (or equivalently a lemma i didn't bother to state) on premises\n canonical-indeterminate-forms-arr (TACast wt TCHole2) (ICastHoleGround x\u2081 ind GHole) = Inr (Inr (Inr (Inr (_ , refl , refl , refl , ind , x\u2081))))\n\n canonical-indeterminate-forms-hole : \u2200{\u0394 d} \u2192\n \u0394 , \u2205 \u22a2 d :: \u2987\u2988 \u2192\n d indet \u2192\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] \u2987\u2988) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] \u2987\u2988) \u2208 \u0394))) +\n (\u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c42 \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: (\u03c42 ==> \u2987\u2988)) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c42) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))) +\n (\u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ]\n ((d == d' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9) \u00d7\n (\u03c4' ground) \u00d7\n (d' indet)))\n canonical-indeterminate-forms-hole (TAVar x\u2081) ()\n canonical-indeterminate-forms-hole (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-hole (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-hole (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x ))\n canonical-indeterminate-forms-hole (TACast wt x) (ICastGroundHole x\u2081 ind) = Inr (Inr (Inr (_ , _ , refl , x\u2081 , ind)))\n canonical-indeterminate-forms-hole (TACast wt x) (ICastHoleGround x\u2081 ind ())\n\n canonical-indeterminate-forms-coverage : \u2200{\u0394 d \u03c4} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d indet \u2192\n \u03c4 \u2260 b \u2192\n ((\u03c41 : htyp) (\u03c42 : htyp) \u2192 \u03c4 \u2260 (\u03c41 ==> \u03c42)) \u2192\n \u03c4 \u2260 \u2987\u2988 \u2192\n \u22a5\n canonical-indeterminate-forms-coverage TAConst () nb na nh\n canonical-indeterminate-forms-coverage (TAVar x\u2081) () nb na nh\n canonical-indeterminate-forms-coverage (TALam wt) () nb na nh\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TAAp wt wt\u2081) (IAp x ind x\u2081) nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TAAp wt wt\u2081) (IAp x ind x\u2081) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TAAp wt wt\u2081) (IAp x ind x\u2081) nb na nh = na \u03c4 \u03c4\u2081 refl\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TAEHole x x\u2081) IEHole nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TAEHole x x\u2081) IEHole nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TAEHole x x\u2081) IEHole nb na nh = na \u03c4 \u03c4\u2081 refl\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TANEHole x wt x\u2081) (INEHole x\u2082) nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TANEHole x wt x\u2081) (INEHole x\u2082) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TANEHole x wt x\u2081) (INEHole x\u2082) nb na nh = na \u03c4 \u03c4\u2081 refl\n canonical-indeterminate-forms-coverage (TACast wt x) (ICastArr x\u2081 ind) nb na nh = na _ _ refl\n canonical-indeterminate-forms-coverage (TACast wt x) (ICastGroundHole x\u2081 ind) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) nb na nh = na \u03c4 \u03c4\u2081 refl\n","old_contents":"open import Nat\nopen import Prelude\nopen import contexts\nopen import core\nopen import type-assignment-unicity\n\nmodule canonical-indeterminate-forms where\n canonical-indeterminate-forms-base : \u2200{\u0394 d} \u2192\n \u0394 , \u2205 \u22a2 d :: b \u2192\n d indet \u2192\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] b) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] b) \u2208 \u0394))) +\n \u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c42 \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: \u03c42 ==> b) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c42) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))\n +\n (\u03a3[ d' \u2208 dhexp ]\n ((d == d' \u27e8 \u2987\u2988 \u21d2 b \u27e9) \u00d7\n (d' indet) \u00d7\n ((d'' : dhexp) (\u03c4' : htyp) \u2192 d' \u2260 (d'' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9))))\n canonical-indeterminate-forms-base TAConst ()\n canonical-indeterminate-forms-base (TAVar x\u2081) ()\n canonical-indeterminate-forms-base (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-base (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-base (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x))\n canonical-indeterminate-forms-base (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) = Inr (Inr (Inr (_ , refl , ind , x\u2081)))\n\n canonical-indeterminate-forms-arr : \u2200{\u0394 d \u03c41 \u03c42 } \u2192\n \u0394 , \u2205 \u22a2 d :: (\u03c41 ==> \u03c42) \u2192\n d indet \u2192\n ((\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] (\u03c41 ==> \u03c42)) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] (\u03c41 ==> \u03c42)) \u2208 \u0394))) +\n (\u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c4 \u2208 htyp ] \u03a3[ \u03c41' \u2208 htyp ] \u03a3[ \u03c42' \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: \u03c4 ==> (\u03c41' ==> \u03c42')) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c4) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))) +\n (\u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c41 \u2208 htyp ] \u03a3[ \u03c42 \u2208 htyp ] \u03a3[ \u03c43 \u2208 htyp ] \u03a3[ \u03c44 \u2208 htyp ]\n ((d == d' \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9) \u00d7\n (d' indet) \u00d7\n ((\u03c41 ==> \u03c42) \u2260 (\u03c43 ==> \u03c44)))) +\n (\u03a3[ d' \u2208 dhexp ]\n ((\u03c41 == \u2987\u2988) \u00d7\n (\u03c42 == \u2987\u2988) \u00d7\n (d == (d' \u27e8 \u2987\u2988 \u21d2 \u2987\u2988 ==> \u2987\u2988 \u27e9)) \u00d7\n (d' indet) \u00d7\n ((d'' : dhexp) (\u03c4' : htyp) \u2192 d' \u2260 (d'' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9)))))\n canonical-indeterminate-forms-arr (TAVar x\u2081) ()\n canonical-indeterminate-forms-arr (TALam wt) ()\n canonical-indeterminate-forms-arr (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-arr (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-arr (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x ))\n canonical-indeterminate-forms-arr (TACast wt x) (ICastArr x\u2081 ind) = Inr (Inr (Inr (Inl (_ , _ , _ , _ , _ , refl , ind , x\u2081))))\n -- todo \/ cyrus: this is the only one that required pattern matching (or equivalently a lemma i didn't bother to state) on premises\n canonical-indeterminate-forms-arr (TACast wt TCHole2) (ICastHoleGround x\u2081 ind GHole) = Inr (Inr (Inr (Inr (_ , refl , refl , refl , ind , x\u2081))))\n\n canonical-indeterminate-forms-hole : \u2200{\u0394 d} \u2192\n \u0394 , \u2205 \u22a2 d :: \u2987\u2988 \u2192\n d indet \u2192\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987\u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n ((u ::[ \u0393' ] \u2987\u2988) \u2208 \u0394))) +\n (\u03a3[ u \u2208 Nat ] \u03a3[ \u03c3 \u2208 subst ] \u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ] \u03a3[ \u0393' \u2208 tctx ]\n ((d == \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9) \u00d7\n (d' final) \u00d7\n (\u0394 , \u2205 \u22a2 d' :: \u03c4') \u00d7\n ((u ::[ \u0393' ] \u2987\u2988) \u2208 \u0394))) +\n (\u03a3[ d1 \u2208 dhexp ] \u03a3[ d2 \u2208 dhexp ] \u03a3[ \u03c42 \u2208 htyp ]\n ((d == d1 \u2218 d2) \u00d7\n (\u0394 , \u2205 \u22a2 d1 :: (\u03c42 ==> \u2987\u2988)) \u00d7\n (\u0394 , \u2205 \u22a2 d2 :: \u03c42) \u00d7\n (d1 indet) \u00d7\n (d2 final) \u00d7\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9)))) +\n (\u03a3[ d' \u2208 dhexp ] \u03a3[ \u03c4' \u2208 htyp ]\n ((d == d' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9) \u00d7\n (\u03c4' ground) \u00d7\n (d indet))) --todo \/ cyrus: this is\n --interesting; it's\n --the only clause\n --that's filled by\n --multiple patterns\n --below; maybe we\n --could say something\n --more specific?\n canonical-indeterminate-forms-hole (TAVar x\u2081) ()\n canonical-indeterminate-forms-hole (TAAp wt wt\u2081) (IAp x ind x\u2081) = Inr (Inr (Inl (_ , _ , _ , refl , wt , wt\u2081 , ind , x\u2081 , x)))\n canonical-indeterminate-forms-hole (TAEHole x x\u2081) IEHole = Inl (_ , _ , _ , refl , x)\n canonical-indeterminate-forms-hole (TANEHole x wt x\u2081) (INEHole x\u2082) = Inr (Inl (_ , _ , _ , _ , _ , refl , x\u2082 , wt , x ))\n canonical-indeterminate-forms-hole (TACast wt x) (ICastGroundHole x\u2081 ind) = Inr (Inr (Inr (_ , _ , refl , x\u2081 , ICastGroundHole x\u2081 ind)))\n canonical-indeterminate-forms-hole (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) = Inr (Inr (Inr (_ , _ , refl , x\u2082 , ICastGroundHole x\u2082 ind)))\n\n canonical-indeterminate-forms-coverage : \u2200{\u0394 d \u03c4} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d indet \u2192\n \u03c4 \u2260 b \u2192\n ((\u03c41 : htyp) (\u03c42 : htyp) \u2192 \u03c4 \u2260 (\u03c41 ==> \u03c42)) \u2192\n \u03c4 \u2260 \u2987\u2988 \u2192\n \u22a5\n canonical-indeterminate-forms-coverage TAConst () nb na nh\n canonical-indeterminate-forms-coverage (TAVar x\u2081) () nb na nh\n canonical-indeterminate-forms-coverage (TALam wt) () nb na nh\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TAAp wt wt\u2081) (IAp x ind x\u2081) nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TAAp wt wt\u2081) (IAp x ind x\u2081) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TAAp wt wt\u2081) (IAp x ind x\u2081) nb na nh = na \u03c4 \u03c4\u2081 refl\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TAEHole x x\u2081) IEHole nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TAEHole x x\u2081) IEHole nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TAEHole x x\u2081) IEHole nb na nh = na \u03c4 \u03c4\u2081 refl\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TANEHole x wt x\u2081) (INEHole x\u2082) nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TANEHole x wt x\u2081) (INEHole x\u2082) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TANEHole x wt x\u2081) (INEHole x\u2082) nb na nh = na \u03c4 \u03c4\u2081 refl\n canonical-indeterminate-forms-coverage (TACast wt x) (ICastArr x\u2081 ind) nb na nh = na _ _ refl\n canonical-indeterminate-forms-coverage (TACast wt x) (ICastGroundHole x\u2081 ind) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = b} (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) nb na nh = nb refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u2987\u2988} (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) nb na nh = nh refl\n canonical-indeterminate-forms-coverage {\u03c4 = \u03c4 ==> \u03c4\u2081} (TACast wt x) (ICastHoleGround x\u2081 ind x\u2082) nb na nh = na \u03c4 \u03c4\u2081 refl\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"14b2f16af804d20fd6a8890ced468fa56fc999d4","subject":"lecture 1","message":"lecture 1\n","repos":"siddhartha-gadgil\/LogicTypesSpaces,siddhartha-gadgil\/LogicTypesSpaces,siddhartha-gadgil\/LogicTypesSpaces,siddhartha-gadgil\/LogicTypesSpaces","old_file":"livecode\/Boolean.agda","new_file":"livecode\/Boolean.agda","new_contents":"module Boolean where\n\ndata Bool : Set where \n true : Bool\n false : Bool\n\nnot : Bool \u2192 Bool\nnot true = false\nnot false = true\n\nand : Bool -> (Bool -> Bool)\nand true true = true\nand false _ = false\nand true false = false\n","old_contents":"module Boolean where\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3e0d9a0e494b869a18e730c5ce93e481a4161646","subject":"Yet more fiddling","message":"Yet more fiddling\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/BigStep.agda","new_file":"Thesis\/BigStep.agda","new_contents":"{-# OPTIONS --allow-unsolved-metas #-}\nmodule Thesis.BigStep where\n\nopen import Data.Empty\nopen import Data.Unit\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat using (\u2115; zero; suc; decTotalOrder; _<_)\n\nopen import Thesis.Syntax hiding (suc)\nopen import Thesis.Lang hiding (\u27e6_\u27e7Context; \u27e6_\u27e7Var; suc)\n\n-- open import Base.Syntax.Context Type public\n-- open import Base.Syntax.Vars Type public\n\n-- data Const : (\u03c4 : Type) \u2192 Set where\n-- lit : \u2124 \u2192 Const int\n-- -- succ : Const (int \u21d2 int)\n\n-- data Term (\u0393 : Context) :\n-- (\u03c4 : Type) \u2192 Set where\n-- -- constants aka. primitives\n-- const : \u2200 {\u03c4} \u2192\n-- (c : Const \u03c4) \u2192\n-- Term \u0393 \u03c4\n-- var : \u2200 {\u03c4} \u2192\n-- (x : Var \u0393 \u03c4) \u2192\n-- Term \u0393 \u03c4\n-- app : \u2200 {\u03c3 \u03c4}\n-- (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n-- (t : Term \u0393 \u03c3) \u2192\n-- Term \u0393 \u03c4\n-- -- we use de Bruijn indices, so we don't need binding occurrences.\n-- abs : \u2200 {\u03c3 \u03c4}\n-- (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n-- Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2124) \u2192 Val int\n -- prim : \u2200 {\u03c3 \u03c4} \u2192 (f : Val \u03c3 \u2192 Val \u03c4) \u2192 Val (\u03c3 \u21d2 \u03c4)\n -- prim : \u2200 {\u03c3 \u03c4} \u2192 (f : \u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type) \u2192 Val (\u03c3 \u21d2 \u03c4)\n pairV : \u2200 {\u03c3 \u03c4} \u2192 Val \u03c3 \u2192 Val \u03c4 \u2192 Val (pair \u03c3 \u03c4)\n -- prim : \u2200 {\u03c3 \u03c4} \u2192 (f : \u27e6 \u03c3 \u27e7Type \u2192 Val \u03c4) \u2192 Val (\u03c3 \u21d2 \u03c4)\n\n-- TODO: add defunctionalized interpretations for primitives. Yes, annoying but mechanical.\n\n-- Doesn't work\n-- data Val2 : \u2115 \u2192 Type \u2192 Set where\n-- prim : \u2200 {\u03c3 \u03c4 n} \u2192 (f : Val2 n \u03c3 \u2192 Val2 n \u03c4) \u2192 Val2 (\u2115.suc n) (\u03c3 \u21d2 \u03c4)\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7Context \u2192 Den.\u27e6 \u0393 \u27e7Context\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 closure t \u03c1 \u27e7Val = \u03bb v \u2192 (\u27e6 t \u27e7Term) (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\u27e6 intV n \u27e7Val = n\n-- \u27e6 prim f \u27e7Val = \u03bb v \u2192 \u27e6 f v \u27e7Val\n\u27e6 pairV a b \u27e7Val = (\u27e6 a \u27e7Val , \u27e6 b \u27e7Val)\n-- \u27e6 true \u27e7Val = true\n-- \u27e6 false \u27e7Val = false\n\n\u21a6-sound : \u2200 {\u0393 \u03c4} \u03c1 (x : Var \u0393 \u03c4) \u2192\n Den.\u27e6 x \u27e7Var \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 \u27e6 x \u27e7Var \u03c1 \u27e7Val\n\u21a6-sound (px \u2022 \u03c1) this = refl\n\u21a6-sound (px \u2022 \u03c1) (that x) = \u21a6-sound \u03c1 x\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value, following \"Type Soundness Proofs with\n-- Definitional Interpreters\". Since we focus for now on STLC, unlike that\n-- paper, we can avoid error values by keeping types.\n--\n-- One could drop types and add error values, to reproduce what they do.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = \u2115 \u2192 ErrVal \u03c4\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) n = Done (intV v)\nevalConst c n = {!!}\n\n-- evalConst plus n = Done (prim (\u03bb a \u2192 prim (\u03bb b \u2192 intV (a + b))))\n-- evalConst minus n = Done (prim (\u03bb a \u2192 prim (\u03bb b \u2192 intV (a - b))))\n-- evalConst cons n = Done (prim (\u03bb a \u2192 prim (\u03bb b \u2192 {!pairV !})))\n-- evalConst fst n = {!!}\n-- evalConst snd n = {!!}\n-- evalConst linj n = {!!}\n-- evalConst rinj n = {!!}\n-- evalConst match n = {!!}\n-- evalConst zero = intV (+ 0)\n-- evalConst succ = {!!}\n\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n-- apply (prim f) a n = Done (f \u27e6 a \u27e7Val)\n\neval t \u03c1 zero = TimeOut\neval (const c) \u03c1 (suc n) = evalConst c n\neval (var x) \u03c1 (suc n) = Done (\u27e6 x \u27e7Var \u03c1)\neval (abs t) \u03c1 (suc n) = Done (closure t \u03c1)\neval (app s t) \u03c1 (suc n) with eval s \u03c1 n | eval t \u03c1 n\n... | Done f | Done a = apply f a n\n... | _ | _ = TimeOut\n-- eval t0 \u03c1 (suc n) with t0\n-- ... | const c = evalConst c n\n-- ... | var x = Done (\u27e6 x \u27e7Var \u03c1)\n-- ... | abs t = Done (closure t \u03c1)\n-- ... | app s t with eval s \u03c1 n | eval t \u03c1 n\n-- ... | Done f | Done a = apply f a n\n-- ... | _ | _ = TimeOut\n\n-- Can we prove eval sound wrt. our reference denotational semantics? Yes! Very\n-- cool!\neval-sound : \u2200 {\u0393 \u03c4} \u03c1 v n (t : Term \u0393 \u03c4) \u2192\n eval t \u03c1 n \u2261 Done v \u2192\n \u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 v \u27e7Val\n\napply-sound : \u2200 {\u0393 \u03c3 \u03c4} \u03c1 v f a n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t \u2192\n \u27e6 s \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 f \u27e7Val \u2192\n \u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 a \u27e7Val \u2192\n apply f a n \u2261 Done v \u2192\n \u27e6 s \u27e7Term \u27e6 \u03c1 \u27e7Env (\u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env) \u2261 \u27e6 v \u27e7Val\napply-sound _ v (closure ft \u03c1) a n s t feq aeq eq rewrite feq | aeq = eval-sound (a \u2022 \u03c1) v n ft eq\n\neval-sound \u03c1 v zero t ()\neval-sound \u03c1 v (\u2115.suc n) (const c) eq = {!!}\neval-sound \u03c1 v (\u2115.suc n) (var x) refl = \u21a6-sound \u03c1 x\neval-sound \u03c1 v (\u2115.suc n) (abs t) refl = refl\neval-sound \u03c1 v (\u2115.suc n) (app s t) eq with eval s \u03c1 n | inspect (eval s \u03c1) n | eval t \u03c1 n | inspect (eval t \u03c1) n\neval-sound \u03c1 v (\u2115.suc n) (app s t) eq | Done f | [ feq ] | Done a | [ aeq ] =\n let feq = eval-sound \u03c1 f n s feq; aeq = eval-sound \u03c1 a n t aeq in apply-sound \u03c1 v f a n s t feq aeq eq\neval-sound \u03c1 v (\u2115.suc n) (app s t) () | Done f | _ | TimeOut | _\neval-sound \u03c1 v (\u2115.suc n) (app s t) () | TimeOut | _ | _ | _\n-- eval-sound n (const c) eq = {!!}\n-- eval-sound n (var x) eq = {!!}\n-- eval-sound n (app s t) eq = {!!} -- with eval s \u03c1 n\n-- eval-sound n (abs t) eq = {!!}\n-- eval-sound n (const c) eq = {!!}\n-- eval-sound n (var x) eq = {!!}\n-- eval-sound n (app s t) eq = {!!} -- with eval s \u03c1 n\n-- eval-sound n (abs t) eq = {!!}\n\n-- Next, we can try defining change structures and validity.\n\ndapply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u0394t (\u03c3 \u21d2 \u03c4)) \u2192 Val \u03c3 \u2192 Val (\u0394t \u03c3) \u2192 Res (\u0394t \u03c4)\ndapply df a1 da zero = TimeOut\ndapply df a1 da (suc zero) = TimeOut\ndapply df a1 da (suc (suc n)) with apply df a1 n\n... | Done dfa = apply dfa da (suc n)\n... | TimeOut = TimeOut\n\n-- apply (closure (abs (derive t)) d\u03c1) a1 (suc (suc n))\n-- eval (derive (abs t)) d\u03c1 (suc n) \u2261\n-- dapply-sound-0\n-- dapply (closure (abs (derive t)) d\u03c1) a1 da (suc n) \u2261\n-- eval (derive t) (da \u2022 a1 \u2022 d\u03c1) n\n\ndapply' : \u2200 {\u03c3 \u03c4} \u2192 ErrVal (\u0394t (\u03c3 \u21d2 \u03c4)) \u2192 ErrVal \u03c3 \u2192 ErrVal (\u0394t \u03c3) \u2192 Res (\u0394t \u03c4)\ndapply' (Done df) (Done v) (Done dv) n = dapply df v dv n\ndapply' _ _ _ _ = TimeOut\n\n-- Which step-indexes should we use in dapply's specification and\n-- implementation? That's highly non-obvious. To come up with them, I had to\n-- step through the evaluation of eval (app (app ds t) dt), and verify that I\n-- never invoke eval or apply on the same inputs at different step-indexes.\n--\n-- However, I expect I wouldn't be able to get a working proof if I got the\n-- indexes wrong.\ndapply-sound : \u2200 {\u0393 \u03c3 \u03c4} n \u03c1 (ds : Term \u0393 (\u0394t (\u03c3 \u21d2 \u03c4))) t dt \u2192\n eval (app (app ds t) dt) \u03c1 (suc (suc n))\n \u2261\n dapply' (eval ds \u03c1 n) (eval t \u03c1 n) (eval dt \u03c1 (suc n)) (suc (suc n))\ndapply-sound zero \u03c1 ds t dt = refl\ndapply-sound (suc n) \u03c1 ds t dt with\n eval ds \u03c1 (suc n) | eval t \u03c1 (suc n) | eval dt \u03c1 (suc (suc n))\ndapply-sound (suc n) \u03c1 ds t dt | Done df | Done v | Done dv with apply df v (suc n)\n... | Done dfv = refl\n... | TimeOut = refl\ndapply-sound (suc n) \u03c1 ds t dt | Done df | Done v | TimeOut with apply df v (suc n)\n... | Done dfv = refl\n... | TimeOut = refl\ndapply-sound (suc n) \u03c1 ds t dt | Done df | TimeOut | dv = refl\ndapply-sound (suc n) \u03c1 ds t dt | TimeOut | v | dv = refl\n\n\u0394\u0393 : Context \u2192 Context\n\u0394\u0393 \u2205 = \u2205\n\u0394\u0393 (\u03c4 \u2022 \u0393) = \u0394t \u03c4 \u2022 \u03c4 \u2022 \u0394\u0393 \u0393\n\nCh\u0393 : \u2200 (\u0393 : Context) \u2192 Set\nCh\u0393 \u0393 = \u27e6 \u0394\u0393 \u0393 \u27e7Context\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394\u0393 \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394t \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u227c\u0394\u0393\n\nfit : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394\u0393 \u0393) \u03c4\nfit = weaken \u0393\u227c\u0394\u0393\n\nderiveConst : \u2200 {\u03c4} \u2192\n Const \u03c4 \u2192\n Term \u2205 (\u0394t \u03c4)\nderiveConst = {!!}\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394\u0393 \u0393) (\u0394t \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394\u0393 \u0393) (\u0394t \u03c4)\nderive (const c) = weaken (\u2205\u227c\u0393 {\u0394\u0393 _}) (deriveConst c)\nderive (var x) = var (deriveVar x)\nderive (app s t) = app (app (derive s) (fit t)) (derive t)\nderive (abs t) = abs (abs (derive t))\n\n\n-- open import Thesis.Derive\nCh\u03c4 : Type \u2192 Set\nCh\u03c4 \u03c4 = Val (\u0394t \u03c4)\n\ndeval : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0393 \u0393) \u2192 Res (\u0394t \u03c4)\ndeval t \u03c1 d\u03c1 = eval (derive t) d\u03c1\n\n\ndapply-equiv : \u2200 {\u0393 \u03c3 \u03c4} n\n (t : Term (\u03c3 \u2022 \u0393) \u03c4)\n (d\u03c1 : Ch\u0393 \u0393) (\u03c11 : \u27e6 \u0393 \u27e7Context)\n (da : Val (\u0394t \u03c3)) (a1 : Val \u03c3) \u2192\n dapply (closure (abs (derive t)) d\u03c1) a1 da (suc (suc (suc n))) \u2261\n eval (derive t) (da \u2022 a1 \u2022 d\u03c1) (suc (suc n))\ndapply-equiv n t d\u03c1 \u03c11 da a1 = refl\n\n-- Target statement:\n\n_[_]\u03c4_from_to_ : (n : \u2115) \u2192 \u2200 \u03c4 \u2192 Val (\u0394t \u03c4) \u2192 Val \u03c4 \u2192 Val \u03c4 \u2192 Set\n_[_]\u03c4'_from_to_ : (n : \u2115) \u2192 \u2200 \u03c4 \u2192 ErrVal (\u0394t \u03c4) \u2192 ErrVal \u03c4 \u2192 ErrVal \u03c4 \u2192 Set\nn [ \u03c4 ]\u03c4' Done dv from Done v1 to Done v2 = n [ \u03c4 ]\u03c4 dv from v1 to v2\nn [ \u03c4 ]\u03c4' TimeOut from Done v1 to Done v2 = \u22a5\nn [ \u03c4 ]\u03c4' dv from _ to _ = \u22a4\n\n-- XXX: This is a lie for now, but all papers agree we'll want this in the end.\n_[_]\u03c4mono_from_to : (n : \u2115) \u2192 \u2200 \u03c4 \u2192 (dv : Val (\u0394t \u03c4)) \u2192\n \u2200 v1 v2 n' \u2192\n n' < n \u2192\n n [ \u03c4 ]\u03c4 dv from v1 to v2 \u2192\n n' [ \u03c4 ]\u03c4 dv from v1 to v2\n_[_]\u03c4mono_from_to = {!!}\n\n\n_[_]\u03c4'_fromToTimeOut : \u2200 n \u03c4 edv \u2192 n [ \u03c4 ]\u03c4' edv from TimeOut to TimeOut \u2261 \u22a4\nn [ \u03c4 ]\u03c4' Done v fromToTimeOut = refl\nn [ \u03c4 ]\u03c4' TimeOut fromToTimeOut = refl\n-- n [ \u03c4 ]\u03c4' dv from Done v1 to TimeOut = \u22a4\n-- n [ \u03c4 ]\u03c4' dv from TimeOut to Done v2 = \u22a4\n-- n [ \u03c4 ]\u03c4' dv from TimeOut to TimeOut = \u22a4\n\n-- In the static-caching paper, the specification we use is similar to this one:\n-- n [ \u03c3 \u21d2 \u03c4 ]\u03c4 df from f1 to f2 = \u2200 (da : Val (\u0394t \u03c3)) \u2192 (a1 a2 : Val \u03c3) \u2192\n-- n [ \u03c3 ]\u03c4 da from a1 to a2 \u2192\n-- \u2200 v1 v2 \u2192 apply f1 a1 n \u2261 Done v1 \u2192 apply f2 a2 n \u2261 Done v2 \u2192\n-- \u03a3[ dv \u2208 Val (\u0394t \u03c4) ]\n-- dapply df a1 da n \u2261 Done dv\n-- \u00d7 n [ \u03c4 ]\u03c4 dv from v1 to v2\n-- I defined _[_]\u03c4'_from_to_ to be able to write an equivalent (hopefully) specification more quickly.\n\nzero [ \u03c4 ]\u03c4 dv from v1 to v2 = \u22a4\n-- suc n [ \u03c3 \u21d2 \u03c4 ]\u03c4 df from f1 to f2 =\n-- \u2200 (da : Val (\u0394t \u03c3)) \u2192 (a1 a2 : Val \u03c3) \u2192\n-- n [ \u03c3 ]\u03c4 da from a1 to a2 \u2192\n-- n [ \u03c4 ]\u03c4' dapply df a1 da n from apply f1 a1 n to apply f2 a2 n\n\nsuc n [ \u03c3 \u21d2 \u03c4 ]\u03c4 df from f1 to f2 = \u2200 (da : Val (\u0394t \u03c3)) \u2192 (a1 a2 : Val \u03c3) \u2192\n suc n [ \u03c3 ]\u03c4 da from a1 to a2 \u2192\n\n-- This definition violates all tips from Pitts tutorial (Step-Indexed\n-- Biorthogonality: a Tutorial Example). Compare with Definition 4.1,\n-- equation (10), and remark 4.4.\n--\n-- For extra complication, the setting is rather different, so a direct\n-- comparison doesn't quite work.\n \u2200 v1 v2 \u2192 apply f1 a1 (suc n) \u2261 Done v1 \u2192 apply f2 a2 (suc n) \u2261 Done v2 \u2192\n \u03a3[ dv \u2208 Val (\u0394t \u03c4) ]\n dapply df a1 da (suc (suc n)) \u2261 Done dv\n \u00d7 n [ \u03c4 ]\u03c4 dv from v1 to v2\n\nsuc n [ int ]\u03c4 intV dn from intV n1 to intV n2 = n1 + dn \u2261 n2\nsuc n [ pair \u03c3 \u03c4 ]\u03c4 pairV da db from pairV a1 b1 to pairV a2 b2 =\n suc n [ \u03c3 ]\u03c4 da from a1 to a2 \u00d7 suc n [ \u03c4 ]\u03c4 db from b1 to b2\nsuc n [ sum \u03c3 \u03c4 ]\u03c4 () from v1 to v2\n\ndata _[_]\u0393_from_to_ : \u2200 \u2115 \u0393 \u2192 Ch\u0393 \u0393 \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 Set where\n v\u2205 : \u2200 {n} \u2192 n [ \u2205 ]\u0393 \u2205 from \u2205 to \u2205\n _v\u2022_ : \u2200 {n \u03c4 \u0393 dv v1 v2 d\u03c1 \u03c11 \u03c12} \u2192\n (dvv : n [ \u03c4 ]\u03c4 dv from v1 to v2) \u2192\n (d\u03c1\u03c1 : n [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12) \u2192\n n [ \u03c4 \u2022 \u0393 ]\u0393 (dv \u2022 v1 \u2022 d\u03c1) from (v1 \u2022 \u03c11) to (v2 \u2022 \u03c12)\n\nfromtoDeriveVar : \u2200 {\u0393} \u03c4 n \u2192 (x : Var \u0393 \u03c4) \u2192\n (d\u03c1 : Ch\u0393 \u0393) (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 n [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n n [ \u03c4 ]\u03c4 (\u27e6 deriveVar x \u27e7Var d\u03c1) from \u27e6 x \u27e7Var \u03c11 to \u27e6 x \u27e7Var \u03c12\nfromtoDeriveVar \u03c4 n this (dv \u2022 .(v1 \u2022 _)) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dvv v\u2022 d\u03c1\u03c1) = dvv\nfromtoDeriveVar \u03c4 n (that x) (dv \u2022 (.v1 \u2022 d\u03c1)) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dvv v\u2022 d\u03c1\u03c1) = fromtoDeriveVar \u03c4 n x d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1\n\nfoo : \u2200 {\u0393 \u03c3 \u03c4 v1 v2 dv} n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n (d\u03c1 : Ch\u0393 \u0393) (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context)\n (da : Val (\u0394t \u03c3)) (a1 a2 : Val \u03c3) \u2192\n (eqv1 : eval t (a1 \u2022 \u03c11) (suc (suc n)) \u2261 Done v1) \u2192\n (eqv2 : eval t (a2 \u2022 \u03c12) (suc (suc n)) \u2261 Done v2) \u2192\n (eqvv : eval (derive t) (da \u2022 a1 \u2022 d\u03c1) (suc (suc n)) \u2261 Done dv) \u2192\n suc (suc n) [ \u03c4 ]\u03c4' eval (derive t) (da \u2022 a1 \u2022 d\u03c1) (suc (suc n)) from\n eval t (a1 \u2022 \u03c11) (suc (suc n)) to eval t (a2 \u2022 \u03c12) (suc (suc n)) \u2192\n suc n [ \u03c4 ]\u03c4 dv from v1 to v2\nfoo n t d\u03c1 \u03c11 \u03c12 da a1 a2 eqv1 eqv2 eqvv dvv with eval t (a1 \u2022 \u03c11) (suc (suc n)) | eval t (a2 \u2022 \u03c12) (suc (suc n)) | eval (derive t) (da \u2022 a1 \u2022 d\u03c1) (suc (suc n))\nfoo n t d\u03c1 \u03c11 \u03c12 da a1 a2 refl refl refl dvv | Done v1 | (Done v2) | (Done dv) = _[_]\u03c4mono_from_to (suc (suc n)) _ dv v1 v2 (suc n) (DecTotalOrder.reflexive Data.Nat.decTotalOrder refl) dvv\nfoo n t d\u03c1 \u03c11 \u03c12 da a1 a2 refl refl () dvv | Done v\u2081 | (Done v) | TimeOut\nfoo n t d\u03c1 \u03c11 \u03c12 da a1 a2 refl () eqvv dvv | Done v | TimeOut | s\nfoo n t d\u03c1 \u03c11 \u03c12 da a1 a2 () eqv2 eqvv dvv | TimeOut | r | s\n-- q r s eqv1 eqv2 eqvv\n\nfromtoDerive : \u2200 {\u0393} \u03c4 n \u2192 (t : Term \u0393 \u03c4) \u2192\n (d\u03c1 : Ch\u0393 \u0393) (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 n [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n n [ \u03c4 ]\u03c4' (deval t \u03c11 d\u03c1 n) from eval t \u03c11 n to eval t \u03c12 n\nfromtoDerive \u03c4 zero t d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 rewrite zero [ \u03c4 ]\u03c4' (deval t \u03c11 d\u03c1 (suc zero)) fromToTimeOut = tt\nfromtoDerive \u03c4 (suc n) (const c) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 = {!!}\nfromtoDerive \u03c4 (suc n) (var x) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 = fromtoDeriveVar \u03c4 (suc n) x d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1\nfromtoDerive \u03c4 (suc n) (app s t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 = {!fromtoDerive _ (suc n) s d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1!}\n-- fromtoDerive (\u03c3 \u21d2 \u03c4) (suc n) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa\n-- rewrite dapply-equiv n t d\u03c1 \u03c11 da a1 =\n-- fromtoDerive \u03c4 (suc n) t (da \u2022 a1 \u2022 d\u03c1) (a1 \u2022 \u03c11) (a2 \u2022 \u03c12) (daa v\u2022 d\u03c1\u03c1)\nfromtoDerive (\u03c3 \u21d2 \u03c4) (suc zero) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa v1 v2 eqv1 eqv2 = {!LIE!!}\nfromtoDerive (\u03c3 \u21d2 \u03c4) (suc (suc n)) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa v1 v2 eqv1 eqv2 with\n eval (derive t) (da \u2022 a1 \u2022 d\u03c1) (suc (suc n)) | inspect (eval (derive t) (da \u2022 a1 \u2022 d\u03c1)) (suc (suc n))\nfromtoDerive (\u03c3 \u21d2 \u03c4) (suc (suc n)) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa v1 v2 eqv1 eqv2 | Done dv | [ eqvv ] = dv , refl , foo n t d\u03c1 \u03c11 \u03c12 da a1 a2 eqv1 eqv2 eqvv (fromtoDerive \u03c4 (suc (suc n)) t (da \u2022 a1 \u2022 d\u03c1) (a1 \u2022 \u03c11) (a2 \u2022 \u03c12) (daa v\u2022 d\u03c1\u03c1))\nfromtoDerive (\u03c3 \u21d2 \u03c4) (suc (suc n)) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa v1 v2 eqv1 eqv2 | TimeOut | eq = {!LIE!!}\n-- {!eval (derive t)!} , {!fromtoDerive \u03c4 (suc n) t (da \u2022 a1 \u2022 d\u03c1) (a1 \u2022 \u03c11) (a2 \u2022 \u03c12) ?!}\n\n-- fromtoDerive (\u03c3 \u21d2 \u03c4) (suc (suc zero)) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa = {!!}\n-- fromtoDerive (\u03c3 \u21d2 \u03c4) (suc (suc (suc n))) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa\n-- with eval (abs (derive t)) (a1 \u2022 d\u03c1) n | inspect (eval (abs (derive t)) (a1 \u2022 d\u03c1)) n\n-- fromtoDerive (\u03c3 \u21d2 \u03c4) (suc (suc (suc n))) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa | Done v | [ eq ] = {!fromtoDerive \u03c4 (suc (suc n)) t (da \u2022 a1 \u2022 d\u03c1) (a1 \u2022 \u03c11) (a2 \u2022 \u03c12) _!}\n-- fromtoDerive (\u03c3 \u21d2 \u03c4) (suc (suc (suc n))) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa | TimeOut | _ = {!!}\n-- -- fromtoDerive \u03c4 n t (da \u2022 a1 \u2022 d\u03c1) (a1 \u2022 \u03c11) (a2 \u2022 \u03c12) (daa v\u2022 ?)\n-- -- fromtoDerive \u03c4 (suc (suc n)) t (da \u2022 a1 \u2022 d\u03c1) (a1 \u2022 \u03c11) (a2 \u2022 \u03c12) {! daa v\u2022 d\u03c1\u03c1!}\n-- -- fromtoDerive \u03c4 n t (da \u2022 a1 \u2022 d\u03c1) (a1 \u2022 \u03c11) (a2 \u2022 \u03c12) {! daa v\u2022 d\u03c1\u03c1!}\n","old_contents":"{-# OPTIONS --allow-unsolved-metas #-}\nmodule Thesis.BigStep where\n\nopen import Thesis.Syntax hiding (suc)\nopen import Thesis.Lang hiding (\u27e6_\u27e7Context; \u27e6_\u27e7Var; suc)\n\nopen import Relation.Binary.PropositionalEquality\n\n-- open import Base.Syntax.Context Type public\n-- open import Base.Syntax.Vars Type public\n\n-- data Const : (\u03c4 : Type) \u2192 Set where\n-- lit : \u2124 \u2192 Const int\n-- -- succ : Const (int \u21d2 int)\n\n-- data Term (\u0393 : Context) :\n-- (\u03c4 : Type) \u2192 Set where\n-- -- constants aka. primitives\n-- const : \u2200 {\u03c4} \u2192\n-- (c : Const \u03c4) \u2192\n-- Term \u0393 \u03c4\n-- var : \u2200 {\u03c4} \u2192\n-- (x : Var \u0393 \u03c4) \u2192\n-- Term \u0393 \u03c4\n-- app : \u2200 {\u03c3 \u03c4}\n-- (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n-- (t : Term \u0393 \u03c3) \u2192\n-- Term \u0393 \u03c4\n-- -- we use de Bruijn indices, so we don't need binding occurrences.\n-- abs : \u2200 {\u03c3 \u03c4}\n-- (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n-- Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\nopen import Data.Nat using (\u2115; zero; suc; decTotalOrder)\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2124) \u2192 Val int\n -- prim : \u2200 {\u03c3 \u03c4} \u2192 (f : Val \u03c3 \u2192 Val \u03c4) \u2192 Val (\u03c3 \u21d2 \u03c4)\n -- prim : \u2200 {\u03c3 \u03c4} \u2192 (f : \u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type) \u2192 Val (\u03c3 \u21d2 \u03c4)\n pairV : \u2200 {\u03c3 \u03c4} \u2192 Val \u03c3 \u2192 Val \u03c4 \u2192 Val (pair \u03c3 \u03c4)\n -- prim : \u2200 {\u03c3 \u03c4} \u2192 (f : \u27e6 \u03c3 \u27e7Type \u2192 Val \u03c4) \u2192 Val (\u03c3 \u21d2 \u03c4)\n\n-- TODO: add defunctionalized interpretations for primitives. Yes, annoying but mechanical.\n\n-- Doesn't work\n-- data Val2 : \u2115 \u2192 Type \u2192 Set where\n-- prim : \u2200 {\u03c3 \u03c4 n} \u2192 (f : Val2 n \u03c3 \u2192 Val2 n \u03c4) \u2192 Val2 (\u2115.suc n) (\u03c3 \u21d2 \u03c4)\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7Context \u2192 Den.\u27e6 \u0393 \u27e7Context\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 closure t \u03c1 \u27e7Val = \u03bb v \u2192 (\u27e6 t \u27e7Term) (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\u27e6 intV n \u27e7Val = n\n-- \u27e6 prim f \u27e7Val = \u03bb v \u2192 \u27e6 f v \u27e7Val\n\u27e6 pairV a b \u27e7Val = (\u27e6 a \u27e7Val , \u27e6 b \u27e7Val)\n-- \u27e6 true \u27e7Val = true\n-- \u27e6 false \u27e7Val = false\n\n\u21a6-sound : \u2200 {\u0393 \u03c4} \u03c1 (x : Var \u0393 \u03c4) \u2192\n Den.\u27e6 x \u27e7Var \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 \u27e6 x \u27e7Var \u03c1 \u27e7Val\n\u21a6-sound (px \u2022 \u03c1) this = refl\n\u21a6-sound (px \u2022 \u03c1) (that x) = \u21a6-sound \u03c1 x\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value, following \"Type Soundness Proofs with\n-- Definitional Interpreters\". Since we focus for now on STLC, unlike that\n-- paper, we can avoid error values by keeping types.\n--\n-- One could drop types and add error values, to reproduce what they do.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = \u2115 \u2192 ErrVal \u03c4\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) n = Done (intV v)\nevalConst c n = {!!}\n\n-- evalConst plus n = Done (prim (\u03bb a \u2192 prim (\u03bb b \u2192 intV (a + b))))\n-- evalConst minus n = Done (prim (\u03bb a \u2192 prim (\u03bb b \u2192 intV (a - b))))\n-- evalConst cons n = Done (prim (\u03bb a \u2192 prim (\u03bb b \u2192 {!pairV !})))\n-- evalConst fst n = {!!}\n-- evalConst snd n = {!!}\n-- evalConst linj n = {!!}\n-- evalConst rinj n = {!!}\n-- evalConst match n = {!!}\n-- evalConst zero = intV (+ 0)\n-- evalConst succ = {!!}\n\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n-- apply (prim f) a n = Done (f \u27e6 a \u27e7Val)\n\neval t \u03c1 zero = TimeOut\neval (const c) \u03c1 (suc n) = evalConst c n\neval (var x) \u03c1 (suc n) = Done (\u27e6 x \u27e7Var \u03c1)\neval (abs t) \u03c1 (suc n) = Done (closure t \u03c1)\neval (app s t) \u03c1 (suc n) with eval s \u03c1 n | eval t \u03c1 n\n... | Done f | Done a = apply f a n\n... | _ | _ = TimeOut\n-- eval t0 \u03c1 (suc n) with t0\n-- ... | const c = evalConst c n\n-- ... | var x = Done (\u27e6 x \u27e7Var \u03c1)\n-- ... | abs t = Done (closure t \u03c1)\n-- ... | app s t with eval s \u03c1 n | eval t \u03c1 n\n-- ... | Done f | Done a = apply f a n\n-- ... | _ | _ = TimeOut\n\n-- Can we prove eval sound wrt. our reference denotational semantics? Yes! Very\n-- cool!\neval-sound : \u2200 {\u0393 \u03c4} \u03c1 v n (t : Term \u0393 \u03c4) \u2192\n eval t \u03c1 n \u2261 Done v \u2192\n \u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 v \u27e7Val\n\napply-sound : \u2200 {\u0393 \u03c3 \u03c4} \u03c1 v f a n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t \u2192\n \u27e6 s \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 f \u27e7Val \u2192\n \u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 a \u27e7Val \u2192\n apply f a n \u2261 Done v \u2192\n \u27e6 s \u27e7Term \u27e6 \u03c1 \u27e7Env (\u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env) \u2261 \u27e6 v \u27e7Val\napply-sound _ v (closure ft \u03c1) a n s t feq aeq eq rewrite feq | aeq = eval-sound (a \u2022 \u03c1) v n ft eq\n\neval-sound \u03c1 v zero t ()\neval-sound \u03c1 v (\u2115.suc n) (const c) eq = {!!}\neval-sound \u03c1 v (\u2115.suc n) (var x) refl = \u21a6-sound \u03c1 x\neval-sound \u03c1 v (\u2115.suc n) (abs t) refl = refl\neval-sound \u03c1 v (\u2115.suc n) (app s t) eq with eval s \u03c1 n | inspect (eval s \u03c1) n | eval t \u03c1 n | inspect (eval t \u03c1) n\neval-sound \u03c1 v (\u2115.suc n) (app s t) eq | Done f | [ feq ] | Done a | [ aeq ] =\n let feq = eval-sound \u03c1 f n s feq; aeq = eval-sound \u03c1 a n t aeq in apply-sound \u03c1 v f a n s t feq aeq eq\neval-sound \u03c1 v (\u2115.suc n) (app s t) () | Done f | _ | TimeOut | _\neval-sound \u03c1 v (\u2115.suc n) (app s t) () | TimeOut | _ | _ | _\n-- eval-sound n (const c) eq = {!!}\n-- eval-sound n (var x) eq = {!!}\n-- eval-sound n (app s t) eq = {!!} -- with eval s \u03c1 n\n-- eval-sound n (abs t) eq = {!!}\n-- eval-sound n (const c) eq = {!!}\n-- eval-sound n (var x) eq = {!!}\n-- eval-sound n (app s t) eq = {!!} -- with eval s \u03c1 n\n-- eval-sound n (abs t) eq = {!!}\n\n-- Next, we can try defining change structures and validity.\n\ndapply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u0394t (\u03c3 \u21d2 \u03c4)) \u2192 Val \u03c3 \u2192 Val (\u0394t \u03c3) \u2192 Res (\u0394t \u03c4)\ndapply df a1 da zero = TimeOut\ndapply df a1 da (suc zero) = TimeOut\ndapply df a1 da (suc (suc n)) with apply df a1 n\n... | Done dfa = apply dfa da (suc n)\n... | TimeOut = TimeOut\n\n-- apply (closure (abs (derive t)) d\u03c1) a1 (suc (suc n))\n-- eval (derive (abs t)) d\u03c1 (suc n) \u2261\n-- dapply-sound-0\n-- dapply (closure (abs (derive t)) d\u03c1) a1 da (suc n) \u2261\n-- eval (derive t) (da \u2022 a1 \u2022 d\u03c1) n\n\ndapply' : \u2200 {\u03c3 \u03c4} \u2192 ErrVal (\u0394t (\u03c3 \u21d2 \u03c4)) \u2192 ErrVal \u03c3 \u2192 ErrVal (\u0394t \u03c3) \u2192 Res (\u0394t \u03c4)\ndapply' (Done df) (Done v) (Done dv) n = dapply df v dv n\ndapply' _ _ _ _ = TimeOut\n\n-- Which step-indexes should we use in dapply's specification and\n-- implementation? That's highly non-obvious. To come up with them, I had to\n-- step through the evaluation of eval (app (app ds t) dt), and verify that I\n-- never invoke eval or apply on the same inputs at different step-indexes.\n--\n-- However, I expect I wouldn't be able to get a working proof if I got the\n-- indexes wrong.\ndapply-sound : \u2200 {\u0393 \u03c3 \u03c4} n \u03c1 (ds : Term \u0393 (\u0394t (\u03c3 \u21d2 \u03c4))) t dt \u2192\n eval (app (app ds t) dt) \u03c1 (suc (suc n))\n \u2261\n dapply' (eval ds \u03c1 n) (eval t \u03c1 n) (eval dt \u03c1 (suc n)) (suc (suc n))\ndapply-sound zero \u03c1 ds t dt = refl\ndapply-sound (suc n) \u03c1 ds t dt with\n eval ds \u03c1 (suc n) | eval t \u03c1 (suc n) | eval dt \u03c1 (suc (suc n))\ndapply-sound (suc n) \u03c1 ds t dt | Done df | Done v | Done dv with apply df v (suc n)\n... | Done dfv = refl\n... | TimeOut = refl\ndapply-sound (suc n) \u03c1 ds t dt | Done df | Done v | TimeOut with apply df v (suc n)\n... | Done dfv = refl\n... | TimeOut = refl\ndapply-sound (suc n) \u03c1 ds t dt | Done df | TimeOut | dv = refl\ndapply-sound (suc n) \u03c1 ds t dt | TimeOut | v | dv = refl\n\nopen import Data.Empty\nopen import Data.Unit\n\n\u0394\u0393 : Context \u2192 Context\n\u0394\u0393 \u2205 = \u2205\n\u0394\u0393 (\u03c4 \u2022 \u0393) = \u0394t \u03c4 \u2022 \u03c4 \u2022 \u0394\u0393 \u0393\n\nCh\u0393 : \u2200 (\u0393 : Context) \u2192 Set\nCh\u0393 \u0393 = \u27e6 \u0394\u0393 \u0393 \u27e7Context\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394\u0393 \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394t \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u227c\u0394\u0393\n\nfit : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394\u0393 \u0393) \u03c4\nfit = weaken \u0393\u227c\u0394\u0393\n\nderiveConst : \u2200 {\u03c4} \u2192\n Const \u03c4 \u2192\n Term \u2205 (\u0394t \u03c4)\nderiveConst = {!!}\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394\u0393 \u0393) (\u0394t \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394\u0393 \u0393) (\u0394t \u03c4)\nderive (const c) = weaken (\u2205\u227c\u0393 {\u0394\u0393 _}) (deriveConst c)\nderive (var x) = var (deriveVar x)\nderive (app s t) = app (app (derive s) (fit t)) (derive t)\nderive (abs t) = abs (abs (derive t))\n\n\n-- open import Thesis.Derive\nCh\u03c4 : Type \u2192 Set\nCh\u03c4 \u03c4 = Val (\u0394t \u03c4)\n\ndeval : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0393 \u0393) \u2192 Res (\u0394t \u03c4)\ndeval t \u03c1 d\u03c1 = eval (derive t) d\u03c1\n\n\ndapply-equiv : \u2200 {\u0393 \u03c3 \u03c4} n\n (t : Term (\u03c3 \u2022 \u0393) \u03c4)\n (d\u03c1 : Ch\u0393 \u0393) (\u03c11 : \u27e6 \u0393 \u27e7Context)\n (da : Val (\u0394t \u03c3)) (a1 : Val \u03c3) \u2192\n dapply (closure (abs (derive t)) d\u03c1) a1 da (suc (suc (suc n))) \u2261\n eval (derive t) (da \u2022 a1 \u2022 d\u03c1) (suc (suc n))\ndapply-equiv n t d\u03c1 \u03c11 da a1 = refl\n\n-- Target statement:\n\n_[_]\u03c4_from_to_ : (n : \u2115) \u2192 \u2200 \u03c4 \u2192 Val (\u0394t \u03c4) \u2192 Val \u03c4 \u2192 Val \u03c4 \u2192 Set\n_[_]\u03c4'_from_to_ : (n : \u2115) \u2192 \u2200 \u03c4 \u2192 ErrVal (\u0394t \u03c4) \u2192 ErrVal \u03c4 \u2192 ErrVal \u03c4 \u2192 Set\nn [ \u03c4 ]\u03c4' Done dv from Done v1 to Done v2 = n [ \u03c4 ]\u03c4 dv from v1 to v2\nn [ \u03c4 ]\u03c4' TimeOut from Done v1 to Done v2 = \u22a5\nn [ \u03c4 ]\u03c4' dv from _ to _ = \u22a4\n\nopen import Data.Nat.Base using (_<_)\n\n-- XXX: This is a lie for now, but all papers agree we'll want this in the end.\n_[_]\u03c4mono_from_to : (n : \u2115) \u2192 \u2200 \u03c4 \u2192 (dv : Val (\u0394t \u03c4)) \u2192\n \u2200 v1 v2 n' \u2192\n n' < n \u2192\n n [ \u03c4 ]\u03c4 dv from v1 to v2 \u2192\n n' [ \u03c4 ]\u03c4 dv from v1 to v2\n_[_]\u03c4mono_from_to = {!!}\n\n\n_[_]\u03c4'_fromToTimeOut : \u2200 n \u03c4 edv \u2192 n [ \u03c4 ]\u03c4' edv from TimeOut to TimeOut \u2261 \u22a4\nn [ \u03c4 ]\u03c4' Done v fromToTimeOut = refl\nn [ \u03c4 ]\u03c4' TimeOut fromToTimeOut = refl\n-- n [ \u03c4 ]\u03c4' dv from Done v1 to TimeOut = \u22a4\n-- n [ \u03c4 ]\u03c4' dv from TimeOut to Done v2 = \u22a4\n-- n [ \u03c4 ]\u03c4' dv from TimeOut to TimeOut = \u22a4\n\n-- In the static-caching paper, the specification we use is similar to this one:\n-- n [ \u03c3 \u21d2 \u03c4 ]\u03c4 df from f1 to f2 = \u2200 (da : Val (\u0394t \u03c3)) \u2192 (a1 a2 : Val \u03c3) \u2192\n-- n [ \u03c3 ]\u03c4 da from a1 to a2 \u2192\n-- \u2200 v1 v2 \u2192 apply f1 a1 n \u2261 Done v1 \u2192 apply f2 a2 n \u2261 Done v2 \u2192\n-- \u03a3[ dv \u2208 Val (\u0394t \u03c4) ]\n-- dapply df a1 da n \u2261 Done dv\n-- \u00d7 n [ \u03c4 ]\u03c4 dv from v1 to v2\n-- I defined _[_]\u03c4'_from_to_ to be able to write an equivalent (hopefully) specification more quickly.\n\nzero [ \u03c4 ]\u03c4 dv from v1 to v2 = \u22a4\n-- suc n [ \u03c3 \u21d2 \u03c4 ]\u03c4 df from f1 to f2 =\n-- \u2200 (da : Val (\u0394t \u03c3)) \u2192 (a1 a2 : Val \u03c3) \u2192\n-- n [ \u03c3 ]\u03c4 da from a1 to a2 \u2192\n-- n [ \u03c4 ]\u03c4' dapply df a1 da n from apply f1 a1 n to apply f2 a2 n\n\nsuc n [ \u03c3 \u21d2 \u03c4 ]\u03c4 df from f1 to f2 = \u2200 (da : Val (\u0394t \u03c3)) \u2192 (a1 a2 : Val \u03c3) \u2192\n suc n [ \u03c3 ]\u03c4 da from a1 to a2 \u2192\n\n-- This definition violates all tips from Pitts tutorial (Step-Indexed\n-- Biorthogonality: a Tutorial Example). Compare with Definition 4.1,\n-- equation (10), and remark 4.4.\n--\n-- For extra complication, the setting is rather different, so a direct\n-- comparison doesn't quite work.\n \u2200 v1 v2 \u2192 apply f1 a1 (suc n) \u2261 Done v1 \u2192 apply f2 a2 (suc n) \u2261 Done v2 \u2192\n \u03a3[ dv \u2208 Val (\u0394t \u03c4) ]\n dapply df a1 da (suc (suc n)) \u2261 Done dv\n \u00d7 n [ \u03c4 ]\u03c4 dv from v1 to v2\n\nsuc n [ int ]\u03c4 intV dn from intV n1 to intV n2 = n1 + dn \u2261 n2\nsuc n [ pair \u03c3 \u03c4 ]\u03c4 pairV da db from pairV a1 b1 to pairV a2 b2 =\n suc n [ \u03c3 ]\u03c4 da from a1 to a2 \u00d7 suc n [ \u03c4 ]\u03c4 db from b1 to b2\nsuc n [ sum \u03c3 \u03c4 ]\u03c4 () from v1 to v2\n\ndata _[_]\u0393_from_to_ : \u2200 \u2115 \u0393 \u2192 Ch\u0393 \u0393 \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 Set where\n v\u2205 : \u2200 {n} \u2192 n [ \u2205 ]\u0393 \u2205 from \u2205 to \u2205\n _v\u2022_ : \u2200 {n \u03c4 \u0393 dv v1 v2 d\u03c1 \u03c11 \u03c12} \u2192\n (dvv : n [ \u03c4 ]\u03c4 dv from v1 to v2) \u2192\n (d\u03c1\u03c1 : n [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12) \u2192\n n [ \u03c4 \u2022 \u0393 ]\u0393 (dv \u2022 v1 \u2022 d\u03c1) from (v1 \u2022 \u03c11) to (v2 \u2022 \u03c12)\n\nfromtoDeriveVar : \u2200 {\u0393} \u03c4 n \u2192 (x : Var \u0393 \u03c4) \u2192\n (d\u03c1 : Ch\u0393 \u0393) (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 n [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n n [ \u03c4 ]\u03c4 (\u27e6 deriveVar x \u27e7Var d\u03c1) from \u27e6 x \u27e7Var \u03c11 to \u27e6 x \u27e7Var \u03c12\nfromtoDeriveVar \u03c4 n this (dv \u2022 .(v1 \u2022 _)) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dvv v\u2022 d\u03c1\u03c1) = dvv\nfromtoDeriveVar \u03c4 n (that x) (dv \u2022 (.v1 \u2022 d\u03c1)) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dvv v\u2022 d\u03c1\u03c1) = fromtoDeriveVar \u03c4 n x d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1\n\nopen import Relation.Binary hiding (_\u21d2_)\nfoo : \u2200 {\u0393 \u03c3 \u03c4 v1 v2 dv} n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n (d\u03c1 : Ch\u0393 \u0393) (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context)\n (da : Val (\u0394t \u03c3)) (a1 a2 : Val \u03c3) \u2192\n (eqv1 : eval t (a1 \u2022 \u03c11) (suc (suc n)) \u2261 Done v1) \u2192\n (eqv2 : eval t (a2 \u2022 \u03c12) (suc (suc n)) \u2261 Done v2) \u2192\n (eqvv : eval (derive t) (da \u2022 a1 \u2022 d\u03c1) (suc (suc n)) \u2261 Done dv) \u2192\n suc (suc n) [ \u03c4 ]\u03c4' eval (derive t) (da \u2022 a1 \u2022 d\u03c1) (suc (suc n)) from\n eval t (a1 \u2022 \u03c11) (suc (suc n)) to eval t (a2 \u2022 \u03c12) (suc (suc n)) \u2192\n suc n [ \u03c4 ]\u03c4 dv from v1 to v2\nfoo n t d\u03c1 \u03c11 \u03c12 da a1 a2 eqv1 eqv2 eqvv dvv with eval t (a1 \u2022 \u03c11) (suc (suc n)) | eval t (a2 \u2022 \u03c12) (suc (suc n)) | eval (derive t) (da \u2022 a1 \u2022 d\u03c1) (suc (suc n))\nfoo n t d\u03c1 \u03c11 \u03c12 da a1 a2 refl refl refl dvv | Done v1 | (Done v2) | (Done dv) = _[_]\u03c4mono_from_to (suc (suc n)) _ dv v1 v2 (suc n) (DecTotalOrder.reflexive Data.Nat.decTotalOrder refl) dvv\nfoo n t d\u03c1 \u03c11 \u03c12 da a1 a2 refl refl () dvv | Done v\u2081 | (Done v) | TimeOut\nfoo n t d\u03c1 \u03c11 \u03c12 da a1 a2 refl () eqvv dvv | Done v | TimeOut | s\nfoo n t d\u03c1 \u03c11 \u03c12 da a1 a2 () eqv2 eqvv dvv | TimeOut | r | s\n-- q r s eqv1 eqv2 eqvv\n\nfromtoDerive : \u2200 {\u0393} \u03c4 n \u2192 (t : Term \u0393 \u03c4) \u2192\n (d\u03c1 : Ch\u0393 \u0393) (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 n [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n n [ \u03c4 ]\u03c4' (deval t \u03c11 d\u03c1 n) from eval t \u03c11 n to eval t \u03c12 n\nfromtoDerive \u03c4 zero t d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 rewrite zero [ \u03c4 ]\u03c4' (deval t \u03c11 d\u03c1 (suc zero)) fromToTimeOut = tt\nfromtoDerive \u03c4 (suc n) (const c) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 = {!!}\nfromtoDerive \u03c4 (suc n) (var x) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 = fromtoDeriveVar \u03c4 (suc n) x d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1\nfromtoDerive \u03c4 (suc n) (app s t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 = {!!}\n-- fromtoDerive (\u03c3 \u21d2 \u03c4) (suc n) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa\n-- rewrite dapply-equiv n t d\u03c1 \u03c11 da a1 =\n-- fromtoDerive \u03c4 (suc n) t (da \u2022 a1 \u2022 d\u03c1) (a1 \u2022 \u03c11) (a2 \u2022 \u03c12) (daa v\u2022 d\u03c1\u03c1)\nfromtoDerive (\u03c3 \u21d2 \u03c4) (suc zero) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa v1 v2 eqv1 eqv2 = {!!}\nfromtoDerive (\u03c3 \u21d2 \u03c4) (suc (suc n)) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa v1 v2 eqv1 eqv2 with\n eval (derive t) (da \u2022 a1 \u2022 d\u03c1) (suc (suc n)) | inspect (eval (derive t) (da \u2022 a1 \u2022 d\u03c1)) (suc (suc n))\nfromtoDerive (\u03c3 \u21d2 \u03c4) (suc (suc n)) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa v1 v2 eqv1 eqv2 | Done dv | [ eqvv ] = dv , refl , foo n t d\u03c1 \u03c11 \u03c12 da a1 a2 eqv1 eqv2 eqvv (fromtoDerive \u03c4 (suc (suc n)) t (da \u2022 a1 \u2022 d\u03c1) (a1 \u2022 \u03c11) (a2 \u2022 \u03c12) (daa v\u2022 d\u03c1\u03c1))\nfromtoDerive (\u03c3 \u21d2 \u03c4) (suc (suc n)) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa v1 v2 eqv1 eqv2 | TimeOut | eq = {!!}\n-- {!eval (derive t)!} , {!fromtoDerive \u03c4 (suc n) t (da \u2022 a1 \u2022 d\u03c1) (a1 \u2022 \u03c11) (a2 \u2022 \u03c12) ?!}\n\n-- fromtoDerive (\u03c3 \u21d2 \u03c4) (suc (suc zero)) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa = {!!}\n-- fromtoDerive (\u03c3 \u21d2 \u03c4) (suc (suc (suc n))) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa\n-- with eval (abs (derive t)) (a1 \u2022 d\u03c1) n | inspect (eval (abs (derive t)) (a1 \u2022 d\u03c1)) n\n-- fromtoDerive (\u03c3 \u21d2 \u03c4) (suc (suc (suc n))) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa | Done v | [ eq ] = {!fromtoDerive \u03c4 (suc (suc n)) t (da \u2022 a1 \u2022 d\u03c1) (a1 \u2022 \u03c11) (a2 \u2022 \u03c12) _!}\n-- fromtoDerive (\u03c3 \u21d2 \u03c4) (suc (suc (suc n))) (abs t) d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1 da a1 a2 daa | TimeOut | _ = {!!}\n-- -- fromtoDerive \u03c4 n t (da \u2022 a1 \u2022 d\u03c1) (a1 \u2022 \u03c11) (a2 \u2022 \u03c12) (daa v\u2022 ?)\n-- -- fromtoDerive \u03c4 (suc (suc n)) t (da \u2022 a1 \u2022 d\u03c1) (a1 \u2022 \u03c11) (a2 \u2022 \u03c12) {! daa v\u2022 d\u03c1\u03c1!}\n-- -- fromtoDerive \u03c4 n t (da \u2022 a1 \u2022 d\u03c1) (a1 \u2022 \u03c11) (a2 \u2022 \u03c12) {! daa v\u2022 d\u03c1\u03c1!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"6115015065a0334d84d5cfc5f45b76faa010f32a","subject":"Swierstra_A_Predicate_Transformer_for_Effects","message":"Swierstra_A_Predicate_Transformer_for_Effects\n","repos":"haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc","old_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/wouter-swierstra-predicate-transformers-6fe8f13\/PredicateTransformers.agda","new_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/wouter-swierstra-predicate-transformers-6fe8f13\/PredicateTransformers.agda","new_contents":"module PredicateTransformers where\n\nopen import Prelude hiding (map; all)\nopen import Level hiding (lift)\n\n------------------------------------------------------------------------------\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\n------------------------------------------------------------------------------\nABSTRACT\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nrefinement type https:\/\/en.wikipedia.org\/wiki\/Refinement_(computing)\n- type with a predicate which must hold for any element of the refined type\n- can express\n - preconditions when used as function args\n - postconditions when used as return types\n\nthe problem statement is centered around \/compositionality\/\n- pure functions enable compositional reasoning\n- effectful programs require reasoning about context\n- paper provides mechanism for reasoning about effectful programs (and for program calculation)\n\nmain idea\n- represent effectful computation as a free monad\n- write an \"interpreter\" that computes a predicate transformer\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to use this refinement relation to\n - show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\nmodule Free where\n -- Free : represents syntax of an effectful computation\n -- C : type of commands\n -- Free C R : returns an 'a' or issues command c : C\n -- For each c : C, there is a set of responses R c\n -- - R : C -> Set : is type family of responses for commands\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\" input to output\n -- input\n -- - pure function 'a\u2192b : a -> b'\n -- - postcondition 'b -> Set' (on output of a\u2192b)\n -- output\n -- - weakest precondition 'a -> Set' on input to a\u2192b that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n module WP0Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data isEven : \u2115 \u2192 Set where\n even-z : isEven Nat.zero\n even-s : {n : \u2115} \u2192 isEven n \u2192 isEven (Nat.suc (Nat.suc n))\n\n isEvenPT : \u2115 -> Set\n isEvenPT = wp0 (_+ 2) isEven\n\n isEven5 : Set\n isEven5 = isEvenPT 5\n\n isEven5' : isEvenPT 5 \u2261 isEven 7\n isEven5' = refl\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - enable via making dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n -- every element a that satisfies P is contained in the set of things that satifies Q\n --\n -- implication between predicates\n --\n -- P a says a \u2208 P,\n -- if the extents of P, Q : A \u2192 Set is taken to be\n -- the subset of A satisfying predicates P, Q\n --\n -- the extent of P : A -> Set is the subset of terms of type A satisfying P.\n -- \"extent\" indicates a kind of black-box view of things,\n -- e.g., the \"extent\" of a function is the set of its input-output pairs.\n --\n -- https:\/\/en.wikipedia.org\/wiki\/Extension_%28semantics%29\n -- the extension of a concept, idea, or sign consists of the things to which it applies\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n -- refinement relation between PTs : pt1 refined by pt2\n --\n -- (same thing as \u2286, but one level up)\n -- for every post condition, pt2 weakens the precondition\n -- \"weakest\" is the right side : pt2 P\n --\n -- pt1 , pt2 : preconditions for satisfying a program\n -- refinement : pt1 maybe be a stronger requirement than pt2\n --\n -- pt2 is a refinement because it weakens what is required by pt1\n -- e.g., pt1 pt2\n -- >= 20 >= 10\n --\n -- weakening the input assumptions gives a stronger result\n --\n -- contract says establish a pre and I will give you a post, i.e.,\n -- \"stronger\" post condition\n -- or \"weaker\" pre condition\n --\n -- refinement of predicate transformers\n --\n -- use case: relate different implementations of the \"same program\"\n -- pt1 \u2291 pt2 : pt1 is refined by pt2\n -- - if pt1 is understood as giving preconditions which satisfy\n -- postcondition P for some given program f1\n -- then his says the precondition can be \/weakened\/ and still guarantee the postcondition\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g\n -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation to\n - relate PT semantics between programs and specifications\n - show a program satisfies its specification\n - show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n - corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n note : for =Free C R b= programs means generating the same AST\n\n instead, define predicate transformers that give \/interpretations\/\n to a particular choice of command type C and response family R\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public using (_+_; _>_; _*_)\n renaming (\u2115 to Nat; zero to Zero; suc to Succ)\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n -- one command\n data C : Set where\n Abort : C -- no continuation\n\n -- response code : the program does not execute further\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n -- BIG-STEP SEMANTICS specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n module \u21d3Test where\n exv : Expr\n exv = Val 3\n exd : Expr\n exd = Div (Val 3) (Val 0)\n\n exb0 : Val 0 \u21d3 0\n exb0 = \u21d3Base\n\n exb3 : Val 3 \u21d3 3\n exb3 = \u21d3Base\n\n exs331 : Div (Val 3) (Val 3) \u21d3 1\n exs331 = \u21d3Step \u21d3Base \u21d3Base\n\n exs931 : Div (Val 9) (Val 3) \u21d3 3\n exs931 = \u21d3Step \u21d3Base \u21d3Base\n\n exs9d31 : Div (Val 9) (Div (Val 3) (Val 1)) \u21d3 3\n exs9d31 = \u21d3Step \u21d3Base (\u21d3Step \u21d3Base \u21d3Base)\n\n exsd91d31 : Div (Div (Val 9) (Val 1)) (Div (Val 3) (Val 1)) \u21d3 3\n exsd91d31 = \u21d3Step (\u21d3Step \u21d3Base \u21d3Base) (\u21d3Step \u21d3Base \u21d3Base)\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val n \u27e7 = return n\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\nl ->\n \u27e6 er \u27e7 >>= \\nr ->\n nl \u00f7 nr\n\n module InterpTest where\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n evv' : evv \u2261 Pure 3\n evv' = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n evd' : evd \u2261 Pure 1\n evd' = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n evd0' : evd0 \u2261 Step Abort (\u03bb ())\n evd0' = refl\n\n {-\n to relate big step semantics to monadic interpreter\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - _\u21d3_ relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n\n to call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results P' : Partial b -> Set\n done via 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so Abort case returns empty type\n -}\n\n -- convert post-condition for a pure function 'f : (x : a) -> b x'\n -- to post-condition on partial functions 'f : (x : A) -> Partial (b x)'\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba -- P holds if operation produces a value\n mustPT _ _ (Step Abort _) = \u22a5 -- if operation fails, there is no result to apply P to\n -- note: could give \u22a4 here, but want to rule out failure\n -- (total correctness)\n\n module MustPTTest where\n bs\u21d3 : Expr -> Nat -> Set\n bs\u21d3 = _\u21d3_\n\n bs\u21d3' : Set\n bs\u21d3' = Val 1 \u21d3 1\n\n bsp\u21d3 : Expr -> Partial Nat -> Set\n bsp\u21d3 = mustPT _\u21d3_\n\n bsp\u21d3' : Set\n bsp\u21d3' = mustPT _\u21d3_ (Val 1) (Pure 1)\n\n bsp\u21d3'x : mustPT _\u21d3_ (Val 1) (Pure 1)\n bsp\u21d3'x = \u21d3Base\n\n bsp\u21d3'' : Set\n bsp\u21d3'' = mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n\n {- nothing can be constructed with this type\n bsp\u21d3''x : mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n bsp\u21d3''x = {!!}\n -}\n xxx : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb n -> Pure 3 >>= _\u00f7 n))\n xxx = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx' : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n xxx' = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx'' : Set\n xxx'' = mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n module WpPartialTest where\n exwpp : Expr -> Set\n exwpp = wpPartial \u27e6_\u27e7 _\u21d3_\n exwpp' : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n exwpp' = refl\n\n wppv1 : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n wppv1 = \u21d3Base\n\n wppd11 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n wppd11 = \u21d3Step \u21d3Base \u21d3Base\n wppd11' : Set\n wppd11' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n\n {- this type cannot be constructed\n wppd10 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n wppd10 = {!!}\n -}\n wppd10' : Set\n wppd10' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n\n {-\n wpPartial \u27e6_\u27e7 _\u21d3_ : a predicate on expressions\n - a way to express SUCCESSFUL evaluation of an Expr with intended semantics\n\n \u2200 exprs satisfying this predicate\n - monadic interpreter and\n - relational big-step specification\n must agree on result of evaluation\n\n for a given Expr, satisfaction of predicate means\n - Expr evaluates to a value by big-step operational semantics\n - if interpreter returns a value at all\n then that value agrees with big step semantics\n - if interpreter aborts\n then have a proof of \u22a5 (so, it does not abort)\n which will NOT agree with \u21d3 big-step semantics since \u21d3 does not allow div-by-0\n\n What does this say about correctness of interpreter?\n\n does not specify how many Expr that satisfy that characterization also satisfy wpPartial \u27e6_\u27e7 _\u21d3_\n\n to understand wpPartial \u27e6_\u27e7 _\u21d3_ better\n - define SafeDiv : safety criteria that we believe in\n - prove every expression that satisfies SafeDiv satisfies wpPartial \u27e6_\u27e7 _\u21d3_\n - SafeDiv refines wpPartial \u27e6_\u27e7 _\u21d3_\n - if we gave \u22a4 above, then 'correct' would not say that SafeDiv rules out undefined behavior\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val _) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n module SafeDivTest where\n exsdv3 : SafeDiv (Val 3) \u2261 \u22a4\n exsdv3 = refl\n exsdv0 : SafeDiv (Val 0) \u2261 \u22a4\n exsdv0 = refl\n exsdv0' : Set\n exsdv0' = SafeDiv (Val 0)\n\n exsd33 : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd33 = refl\n exsd33' : Set\n exsd33' = SafeDiv (Div (Val 3) (Val 3))\n\n exsd30 : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val Zero \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd30 = refl\n exsd30' : Set\n exsd30' = SafeDiv (Div (Val 3) (Val 0))\n\n {-\n Expect : any expr e for which SafeDiv e holds can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n correct (Val _) _ = \u21d3Base\n correct (Div el er) (ernz , (sdel , sder))\n with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n ... | Pure _ | Pure Zero | _ | er\u21d3Znr = magic (ernz er\u21d3Znr)\n ... | Pure _ | Pure (Succ _) | el\u21d3nl | er\u21d3Snr = \u21d3Step el\u21d3nl er\u21d3Snr\n ... | Pure _ | Step Abort _ | _ | ()\n ... | Step Abort _ | _ | () | _\n\n {-\n Generalize above.\n Instead of manually defining SafeDiv,\n define more general predicate\n characterising the domain of a partial function using wpPartial\n -}\n\n -- HC: compare to 'mustPT'\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n --can show that the two semantics agree precisely on the domain of the interpreter:\n\n -- everything in domain of interpreter\n -- gets evaluated in agreement with big step semantics\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n -- only things interpreted in agreement with big step semantics\n -- are those that are in domain of interpreter\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on argument expression\n\n sound (Val _) _ = \u21d3Base\n sound (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div _ _) () | Pure _ | Pure Zero | _ | _\n sound (Div _ _) _ | Pure nl | Pure (Succ nr) | \u22a4'zero\u2192el\u21d3nl | \u22a4'zero\u2192er\u21d3Succnr\n = \u21d3Step (\u22a4'zero\u2192el\u21d3nl tt) (\u22a4'zero\u2192er\u21d3Succnr tt)\n sound (Div _ _) () | Pure _ | Step Abort _ | _ | _\n sound (Div _ _) () | Step Abort _ | _ | _ | _\n\n inDom : {n : Nat}\n -> (e : Expr)\n -> \u27e6 e \u27e7 == Pure n\n -> dom \u27e6_\u27e7 e\n inDom (Val _) _ = tt\n inDom (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div _ _) () | Pure _ | Pure Zero\n inDom (Div _ _) _ | Pure _ | Pure (Succ _) = tt\n inDom (Div _ _) () | Pure _ | Step Abort _\n inDom (Div _ _) () | Step Abort _ | _\n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n : (e : Expr) (n : Nat)\n -> \u27e6 e \u27e7 \u2261 Pure n\n -> e \u21d3 n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n e _ eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} Diveler\u21d3_nldivSuccn\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 { _} { _} () | Pure _ | _ | Pure Zero\n wpPartial1 {el} { _} _ | Pure nl | [[[ eq ]]] | Pure (Succ _) = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n el nl eq\n wpPartial1 { _} { _} () | Pure _ | _ | Step Abort _\n wpPartial1 { _} { _} () | Step Abort _ | _ | _\n\n wpPartial2 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} _ with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 { _} {er} _ | Pure _ | _ | Pure nr | [[[ eqnr ]]] = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n er nr eqnr\n wpPartial2 { _} { _} () | Pure _ | _ | Step Abort _ | _\n wpPartial2 { _} { _} () | Step Abort _ | _ | _ | _\n\n complete (Val _) _ = tt\n complete (Div el er) h\n with \u27e6 el \u27e7\n | inspect \u27e6_\u27e7 el\n | \u27e6 er \u27e7\n | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div _ _) h | Pure nl | [[[ \u27e6el\u27e7\u2261Purenl ]]] | Pure Zero | [[[ \u27e6er\u27e7\u2261Pure0 ]]] | _ | _\n -- Goal\n -- mustPT (\u03bb _ _ \u2192 \u22a4' zero)\n -- (Div el er) (Pure nl >>= (\u03bb nl' \u2192 Pure Zero >>= _\u00f7_ nl'))\n -- \u22a5\n -- Context\n -- h : mustPT _\u21d3_ (Div el er) (\u27e6 el \u27e7 >>= (\u03bb nl' \u2192 \u27e6 er \u27e7 >>= _\u00f7_ nl'))\n rewrite\n \u27e6el\u27e7\u2261Purenl -- h : mustPT _\u21d3_ (Div el er) (\u27e6 er \u27e7 >>= _\u00f7_ nl)\n | \u27e6er\u27e7\u2261Pure0 -- h : \u22a5\n = h -- magic h\n complete (Div _ _) _ | Pure _ | _ | Pure (Succ _) | _ | _ | _ = tt\n complete (Div _ _) _ | Pure _ | _ | Step Abort _ | _ | _ | ()\n complete (Div _ _) _ | Step Abort _ | _ | _ | _ | () | _\n\n module CompleteTest where\n {-\n xxx : mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n xxx = {!!}\n -}\n xxx' : Set\n xxx' = mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement to relate Kleisli morphisms,\n and to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n add top two elements; can fail fail if stack has too few elements\n\n below shows how to prove the definition meets its specification\n\n Define specification in terms of a pre\/post condition.\n -}\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n {-\n [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n for non-dependent examples (e.g., type b does not depend on x : a)\n -}\n\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function\n\n -- specification for addition function : describes the desired postcondition\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n\n {-\n How to relate this specification to an implementation?\n Note: 'wpPartial' assigns predicate transformer semantics to functions\n - but do not yet have a corresponding predicate transform semantics for specifications.\n wpSpec function does that:\n -}\n\n -- Given a specification, Spec a b\n -- computes the weakest precondition that satisfies an arbitrary postcondition P\n -- i.e., the spec\u2019s precondition should hold and its postcondition must imply P.\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\x -> (pre x) \u2227 (post x \u2286 P x)\n\n -- Can now formulate program\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs =\n pop xs >>= \\{(x1 , xs) ->\n pop xs >>= \\{(x2 , xs) ->\n return ((x1 + x2) :: xs)}}\n\n -- that refines the specification given by addSpec:\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd P Nil (() , _)\n correctnessAdd P (x :: Nil) (Data.Nat.s\u2264s () , _)\n correctnessAdd P (x :: y :: xs) (_ , H) = H (x + y :: xs) AddStep\n\n {-\n This example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition,\n - to its implementation.\n\n Compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n get : State s\n get = Step Get return\n\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n statePT : forall {l l'} -> {b : Set l} -> (b \u00d7 s -> Set l') -> State b -> (s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> (a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> (a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s') = (flatten t' == (seq (s) (size t))) \u2227 (s + size t == s')\n\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n correctnessRelabel : forall {a : Set} -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set} -> (c : State a) (f : a -> State b) ->\n \u2200 i P -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification,\n -- by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence,\n -- specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b} (mx : State a) (f : a -> State b) P i ->\n statePT (wpState f \\_ -> P) mx i -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b} (mx : State a) (f : a -> State b) spec ->\n \u2200 P i -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i) ->\n Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P)) ->\n statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s} -> SpecK {zero} (a \u00d7 s) (b \u00d7 s) -> a -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a) (trans\n (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr ->\n Pair (fl \u2261 seq s sl) (s + sl \u2261 s') ->\n Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'') ->\n Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s -> wpSpec relabelSpec P (t , s) -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set} ->\n ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set} -> (c : Free C R a) (f : a -> Free C R b) ->\n \u2200 P -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P =\n cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R} -> (a -> Free C R b) -> (b -> Free C R c) -> a -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set} -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c) ->\n wpCR f1 \u2291 wpCR f2 ->\n wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","old_contents":"module PredicateTransformers where\n\nopen import Prelude hiding (map; all)\nopen import Level hiding (lift)\n\n------------------------------------------------------------------------------\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\n------------------------------------------------------------------------------\nABSTRACT\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nrefinement type https:\/\/en.wikipedia.org\/wiki\/Refinement_(computing)\n- type with a predicate which must hold for any element of the refined type\n- can express\n - preconditions when used as function args\n - postconditions when used as return types\n\nthe problem statement is centered around \/compositionality\/\n- pure functions enable compositional reasoning\n- effectful programs require reasoning about context\n- paper provides mechanism for reasoning about effectful programs (and for program calculation)\n\nmain idea\n- represent effectful computation as a free monad\n- write an \"interpreter\" that computes a predicate transformer\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to use this refinement relation to\n - show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\nmodule Free where\n -- Free : represents syntax of an effectful computation\n -- C : type of commands\n -- Free C R : returns an 'a' or issues command c : C\n -- For each c : C, there is a set of responses R c\n -- - R : C -> Set : is type family of responses for commands\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\" input to output\n -- input\n -- - pure function 'a\u2192b : a -> b'\n -- - postcondition 'b -> Set' (on output of a\u2192b)\n -- output\n -- - weakest precondition 'a -> Set' on input to a\u2192b that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n module WP0Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data isEven : \u2115 \u2192 Set where\n even-z : isEven Nat.zero\n even-s : {n : \u2115} \u2192 isEven n \u2192 isEven (Nat.suc (Nat.suc n))\n\n isEvenPT : \u2115 -> Set\n isEvenPT = wp0 (_+ 2) isEven\n\n isEven5 : Set\n isEven5 = isEvenPT 5\n\n isEven5' : isEvenPT 5 \u2261 isEven 7\n isEven5' = refl\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - enable via making dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n -- every element a that satisfies P is contained in the set of things that satifies Q\n --\n -- implication between predicates\n --\n -- P a says a \u2208 P,\n -- if the extents of P, Q : A \u2192 Set is taken to be\n -- the subset of A satisfying predicates P, Q\n --\n -- the extent of P : A -> Set is the subset of terms of type A satisfying P.\n -- \"extent\" indicates a kind of black-box view of things,\n -- e.g., the \"extent\" of a function is the set of its input-output pairs.\n --\n -- https:\/\/en.wikipedia.org\/wiki\/Extension_%28semantics%29\n -- the extension of a concept, idea, or sign consists of the things to which it applies\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n -- refinement relation between PTs : pt1 refined by pt2\n --\n -- (same thing as \u2286, but one level up)\n -- for every post condition, pt2 weakens the precondition\n -- \"weakest\" is the right side : pt2 P\n --\n -- pt1 , pt2 : preconditions for satisfying a program\n -- refinement : pt1 maybe be a stronger requirement than pt2\n --\n -- pt2 is a refinement because it weakens what is required by pt1\n -- e.g., pt1 pt2\n -- >= 20 >= 10\n --\n -- weakening the input assumptions gives a stronger result\n --\n -- contract says establish a pre and I will give you a post, i.e.,\n -- \"stronger\" post condition\n -- or \"weaker\" pre condition\n --\n -- refinement of predicate transformers\n --\n -- use case: relate different implementations of the \"same program\"\n -- pt1 \u2291 pt2 : pt1 is refined by pt2\n -- - if pt1 is understood as giving preconditions which satisfy\n -- postcondition P for some given program f1\n -- then his says the precondition can be \/weakened\/ and still guarantee the postcondition\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g\n -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation to\n - relate PT semantics between programs and specifications\n - show a program satisfies its specification\n - show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n - corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n note : for =Free C R b= programs means generating the same AST\n\n instead, define predicate transformers that give \/interpretations\/\n to a particular choice of command type C and response family R\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public using (_+_; _>_; _*_)\n renaming (\u2115 to Nat; zero to Zero; suc to Succ)\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n -- one command\n data C : Set where\n Abort : C -- no continuation\n\n -- response code : the program does not execute further\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n -- BIG-STEP SEMANTICS specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n module \u21d3Test where\n exv : Expr\n exv = Val 3\n exd : Expr\n exd = Div (Val 3) (Val 0)\n\n exb0 : Val 0 \u21d3 0\n exb0 = \u21d3Base\n\n exb3 : Val 3 \u21d3 3\n exb3 = \u21d3Base\n\n exs331 : Div (Val 3) (Val 3) \u21d3 1\n exs331 = \u21d3Step \u21d3Base \u21d3Base\n\n exs931 : Div (Val 9) (Val 3) \u21d3 3\n exs931 = \u21d3Step \u21d3Base \u21d3Base\n\n exs9d31 : Div (Val 9) (Div (Val 3) (Val 1)) \u21d3 3\n exs9d31 = \u21d3Step \u21d3Base (\u21d3Step \u21d3Base \u21d3Base)\n\n exsd91d31 : Div (Div (Val 9) (Val 1)) (Div (Val 3) (Val 1)) \u21d3 3\n exsd91d31 = \u21d3Step (\u21d3Step \u21d3Base \u21d3Base) (\u21d3Step \u21d3Base \u21d3Base)\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val n \u27e7 = return n\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\nl ->\n \u27e6 er \u27e7 >>= \\nr ->\n nl \u00f7 nr\n\n module InterpTest where\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n evv' : evv \u2261 Pure 3\n evv' = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n evd' : evd \u2261 Pure 1\n evd' = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n evd0' : evd0 \u2261 Step Abort (\u03bb ())\n evd0' = refl\n\n {-\n to relate big step semantics to monadic interpreter\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - _\u21d3_ relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n\n to call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results P' : Partial b -> Set\n done via 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so Abort case returns empty type\n -}\n\n -- convert post-condition for a pure function 'f : (x : a) -> b x'\n -- to post-condition on partial functions 'f : (x : A) -> Partial (b x)'\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba -- P holds if operation produces a value\n mustPT _ _ (Step Abort _) = \u22a5 -- if operation fails, there is no result to apply P to\n -- note: could give \u22a4 here, but want to rule out failure\n -- (total correctness)\n\n module MustPTTest where\n bs\u21d3 : Expr -> Nat -> Set\n bs\u21d3 = _\u21d3_\n\n bs\u21d3' : Set\n bs\u21d3' = Val 1 \u21d3 1\n\n bsp\u21d3 : Expr -> Partial Nat -> Set\n bsp\u21d3 = mustPT _\u21d3_\n\n bsp\u21d3' : Set\n bsp\u21d3' = mustPT _\u21d3_ (Val 1) (Pure 1)\n\n bsp\u21d3'x : mustPT _\u21d3_ (Val 1) (Pure 1)\n bsp\u21d3'x = \u21d3Base\n\n bsp\u21d3'' : Set\n bsp\u21d3'' = mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n\n {- nothing can be constructed with this type\n bsp\u21d3''x : mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n bsp\u21d3''x = {!!}\n -}\n xxx : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb v -> Pure 3 >>= _\u00f7 v))\n xxx = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx' : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n xxx' = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx'' : Set\n xxx'' = mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n module WpPartialTest where\n exwpp : Expr -> Set\n exwpp = wpPartial \u27e6_\u27e7 _\u21d3_\n exwpp' : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n exwpp' = refl\n\n wppv1 : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n wppv1 = \u21d3Base\n\n wppd11 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n wppd11 = \u21d3Step \u21d3Base \u21d3Base\n wppd11' : Set\n wppd11' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n\n {- this type cannot be constructed\n wppd10 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n wppd10 = {!!}\n -}\n wppd10' : Set\n wppd10' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n\n {-\n wpPartial \u27e6_\u27e7 _\u21d3_ : a predicate on expressions\n - a way to express SUCCESSFUL evaluation of an Expr with intended semantics\n\n \u2200 exprs satisfying this predicate\n - monadic interpreter and\n - relational big-step specification\n must agree on result of evaluation\n\n for a given Expr, satisfaction of predicate means\n - Expr evaluates to a value by big-step operational semantics\n - if interpreter returns a value at all\n then that value agrees with big step semantics\n - if interpreter aborts\n then have a proof of \u22a5 (so, it does not abort)\n which will NOT agree with \u21d3 big-step semantics since \u21d3 does not allow div-by-0\n\n What does this say about correctness of interpreter?\n\n does not specify how many Expr that satisfy that characterization also satisfy wpPartial \u27e6_\u27e7 _\u21d3_\n\n to understand wpPartial \u27e6_\u27e7 _\u21d3_ better\n - define SafeDiv : safety criteria that we believe in\n - prove every expression that satisfies SafeDiv satisfies wpPartial \u27e6_\u27e7 _\u21d3_\n - SafeDiv refines wpPartial \u27e6_\u27e7 _\u21d3_\n - if we gave \u22a4 above, then 'correct' would not say that SafeDiv rules out undefined behavior\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val _) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n module SafeDivTest where\n exsdv3 : SafeDiv (Val 3) \u2261 \u22a4\n exsdv3 = refl\n exsdv0 : SafeDiv (Val 0) \u2261 \u22a4\n exsdv0 = refl\n exsdv0' : Set\n exsdv0' = SafeDiv (Val 0)\n\n exsd33 : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd33 = refl\n exsd33' : Set\n exsd33' = SafeDiv (Div (Val 3) (Val 3))\n\n exsd30 : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val Zero \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd30 = refl\n exsd30' : Set\n exsd30' = SafeDiv (Div (Val 3) (Val 0))\n\n {-\n Expect : any expr e for which SafeDiv e holds can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n correct (Val _) _ = \u21d3Base\n correct (Div el er) (ernz , (sdel , sder))\n with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n ... | Pure _ | Pure Zero | _ | er\u21d3Znr = magic (ernz er\u21d3Znr)\n ... | Pure _ | Pure (Succ _) | el\u21d3nl | er\u21d3Snr = \u21d3Step el\u21d3nl er\u21d3Snr\n ... | Pure _ | Step Abort _ | _ | ()\n ... | Step Abort _ | _ | () | _\n\n {-\n Generalize above.\n Instead of manually defining SafeDiv,\n define more general predicate\n characterising the domain of a partial function using wpPartial\n -}\n\n -- HC: compare to 'mustPT'\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n --can show that the two semantics agree precisely on the domain of the interpreter:\n\n -- everything in domain of interpreter\n -- gets evaluated in agreement with big step semantics\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n -- only things interpreted in agreement with big step semantics\n -- are those that are in domain of interpreter\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on argument expression\n\n sound (Val _) _ = \u21d3Base\n sound (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div _ _) () | Pure _ | Pure Zero | _ | _\n sound (Div _ _) _ | Pure nl | Pure (Succ nr) | \u22a4'zero\u2192el\u21d3nl | \u22a4'zero\u2192er\u21d3Succnr\n = \u21d3Step (\u22a4'zero\u2192el\u21d3nl tt) (\u22a4'zero\u2192er\u21d3Succnr tt)\n sound (Div _ _) () | Pure _ | Step Abort _ | _ | _\n sound (Div _ _) () | Step Abort _ | _ | _ | _\n\n inDom : {n : Nat}\n -> (e : Expr)\n -> \u27e6 e \u27e7 == Pure n\n -> dom \u27e6_\u27e7 e\n inDom (Val _) _ = tt\n inDom (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div _ _) () | Pure _ | Pure Zero\n inDom (Div _ _) _ | Pure _ | Pure (Succ _) = tt\n inDom (Div _ _) () | Pure _ | Step Abort _\n inDom (Div _ _) () | Step Abort _ | _\n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n : (e : Expr) (n : Nat)\n -> \u27e6 e \u27e7 \u2261 Pure n\n -> e \u21d3 n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n e _ eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} Diveler\u21d3_nldivSuccn\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 { _} { _} () | Pure _ | _ | Pure Zero\n wpPartial1 {el} { _} _ | Pure nl | [[[ eq ]]] | Pure (Succ _) = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n el nl eq\n wpPartial1 { _} { _} () | Pure _ | _ | Step Abort _\n wpPartial1 { _} { _} () | Step Abort _ | _ | _\n\n wpPartial2 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} _ with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 { _} {er} _ | Pure _ | _ | Pure nr | [[[ eqnr ]]] = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n er nr eqnr\n wpPartial2 { _} { _} () | Pure _ | _ | Step Abort _ | _\n wpPartial2 { _} { _} () | Step Abort _ | _ | _ | _\n\n complete (Val _) _ = tt\n complete (Div el er) h\n with \u27e6 el \u27e7\n | inspect \u27e6_\u27e7 el\n | \u27e6 er \u27e7\n | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div _ _) h | Pure nl | [[[ \u27e6el\u27e7\u2261Purenl ]]] | Pure Zero | [[[ \u27e6er\u27e7\u2261Pure0 ]]] | _ | _\n -- Goal\n -- mustPT (\u03bb _ _ \u2192 \u22a4' zero) (Div el er) (Pure nl >>= (\u03bb v1 \u2192 Pure Zero >>= _\u00f7_ v1))\n -- \u22a5\n -- Context\n -- h : mustPT _\u21d3_ (Div el er) (\u27e6 el \u27e7 >>= (\u03bb v1 \u2192 \u27e6 er \u27e7 >>= _\u00f7_ v1))\n rewrite\n \u27e6el\u27e7\u2261Purenl -- h : mustPT _\u21d3_ (Div el er) (\u27e6 er \u27e7 >>= _\u00f7_ nl)\n | \u27e6er\u27e7\u2261Pure0 -- h : \u22a5\n = magic h\n complete (Div _ _) _ | Pure _ | _ | Pure (Succ _) | _ | _ | _ = tt\n complete (Div _ _) _ | Pure _ | _ | Step Abort _ | _ | _ | ()\n complete (Div _ _) _ | Step Abort _ | _ | _ | _ | () | _\n\n module CompleteTest where\n {-\n xxx : mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n xxx = {!!}\n -}\n xxx' : Set\n xxx' = mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement to relate Kleisli morphisms,\n and to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n add top two elements; can fail fail if stack has too few elements\n\n below shows how to prove the definition meets its specification\n\n Define specification in terms of a pre\/post condition.\n -}\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n {-\n [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n for non-dependent examples (e.g., type b does not depend on x : a)\n -}\n\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function\n\n -- specification for addition function : describes the desired postcondition\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n\n {-\n How to relate this specification to an implementation?\n Note: 'wpPartial' assigns predicate transformer semantics to functions\n - but do not yet have a corresponding predicate transform semantics for specifications.\n wpSpec function does that:\n -}\n\n -- Given a specification, Spec a b\n -- computes the weakest precondition that satisfies an arbitrary postcondition P\n -- i.e., the spec\u2019s precondition should hold and its postcondition must imply P.\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\x -> (pre x) \u2227 (post x \u2286 P x)\n\n -- Can now formulate program\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs =\n pop xs >>= \\{(x1 , xs) ->\n pop xs >>= \\{(x2 , xs) ->\n return ((x1 + x2) :: xs)}}\n\n -- that refines the specification given by addSpec:\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd P Nil (() , _)\n correctnessAdd P (x :: Nil) (Data.Nat.s\u2264s () , _)\n correctnessAdd P (x :: y :: xs) (_ , H) = H (x + y :: xs) AddStep\n\n {-\n This example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition,\n - to its implementation.\n\n Compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n get : State s\n get = Step Get return\n\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n statePT : forall {l l'} -> {b : Set l} -> (b \u00d7 s -> Set l') -> State b -> (s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> (a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> (a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s') = (flatten t' == (seq (s) (size t))) \u2227 (s + size t == s')\n\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n correctnessRelabel : forall {a : Set} -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set} -> (c : State a) (f : a -> State b) ->\n \u2200 i P -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification,\n -- by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence,\n -- specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b} (mx : State a) (f : a -> State b) P i ->\n statePT (wpState f \\_ -> P) mx i -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b} (mx : State a) (f : a -> State b) spec ->\n \u2200 P i -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i) ->\n Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P)) ->\n statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s} -> SpecK {zero} (a \u00d7 s) (b \u00d7 s) -> a -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a) (trans\n (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr ->\n Pair (fl \u2261 seq s sl) (s + sl \u2261 s') ->\n Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'') ->\n Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s -> wpSpec relabelSpec P (t , s) -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set} ->\n ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set} -> (c : Free C R a) (f : a -> Free C R b) ->\n \u2200 P -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P =\n cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R} -> (a -> Free C R b) -> (b -> Free C R c) -> a -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set} -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c) ->\n wpCR f1 \u2291 wpCR f2 ->\n wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"ebfc5b9adb1c6f827ad5ba98efb7170f25fd251b","subject":"agda: Document \u0394Val","message":"agda: Document \u0394Val\n\nOld-commit-hash: 0db0b8be91a20a06651cdeb565c7e6d63de28db6\n","repos":"inc-lc\/ilc-agda","old_file":"experimental\/TaggedDeltaTypes.agda","new_file":"experimental\/TaggedDeltaTypes.agda","new_contents":"{-\nThe goal of this file is to make the domain of \u0394 types smaller\nso as to be nearer to full abstraction and hopefully close\nenough for the purpose of having explicit nil changes.\n-}\n\nmodule TaggedDeltaTypes where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Nat\nopen import Data.Unit using (\u22a4 ; tt)\nimport Data.Integer as \u2124\nimport Data.Product as Product\nopen import Data.Product using (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\nimport Level\n\n---------------------------------------------------------\n-- Postulates: Extensionality and bag properties (#55) --\n---------------------------------------------------------\n\npostulate extensionality : Extensionality Level.zero Level.zero\n-- Instead of:\n-- open import Data.NatBag renaming\n--- (map to mapBag ; empty to emptyBag ; update to updateBag)\n-- open import Data.NatBag.Properties\npostulate Bag : Set\npostulate emptyBag : Bag\npostulate mapBag : (\u2115 \u2192 \u2115) \u2192 Bag \u2192 Bag\npostulate _++_ : Bag \u2192 Bag \u2192 Bag\npostulate _\\\\_ : Bag \u2192 Bag \u2192 Bag\ninfixr 5 _++_\ninfixl 9 _\\\\_\npostulate b++\u2205=b : \u2200 {b : Bag} \u2192 b ++ emptyBag \u2261 b\npostulate b++[d\\\\b]=d : \u2200 {b d} \u2192 b ++ (d \\\\ b) \u2261 d\npostulate [b++d]\\\\b=d : \u2200 {b d} \u2192 (b ++ d) \\\\ b \u2261 d\npostulate\n map-over-++ : \u2200 {f b d} \u2192\n mapBag f (b ++ d) \u2261 mapBag f b ++ mapBag f d\n\n----------------------------\n-- Useful data structures --\n----------------------------\n\nrecord Quadruple\n (A : Set) (B : A \u2192 Set) (C : (a : A) \u2192 B a \u2192 Set)\n (D : (a : A) \u2192 (b : B a) \u2192 (c : C a b) \u2192 Set): Set where\n constructor cons\n field\n car : A\n cadr : B car\n caddr : C car cadr\n cdddr : D car cadr caddr\n\nopen Quadruple public\n\ncouple : Set \u2192 Set \u2192 Set\ncouple A B = Quadruple A (\u03bb _ \u2192 B) (\u03bb _ _ \u2192 \u22a4) (\u03bb _ _ _ \u2192 \u22a4)\n\ntriple : Set \u2192 Set \u2192 Set \u2192 Set\ntriple A B C = Quadruple A (\u03bb _ \u2192 B) (\u03bb _ _ \u2192 C) (\u03bb _ _ _ \u2192 \u22a4)\n\n------------------------\n-- Syntax of programs --\n------------------------\n\ndata Type : Set where\n nats : Type\n bags : Type\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u03c4 \u0393} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u03c3 \u03c4 \u0393} \u2192 (x : Var \u0393 \u03c3) \u2192 Var (\u03c4 \u2022 \u0393) \u03c3\n\ndata Term : Context -> Type -> Set where\n\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n bag : \u2200 {\u0393} \u2192 (b : Bag) \u2192 Term \u0393 bags\n\n var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4 \u0393} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 Term \u0393 (\u03c3 \u21d2 \u03c4)\n app : \u2200 {\u03c3 \u03c4 \u0393} \u2192 (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) (t : Term \u0393 \u03c3) \u2192 Term \u0393 \u03c4\n\n add : \u2200 {\u0393} \u2192 (s : Term \u0393 nats) \u2192 (t : Term \u0393 nats) \u2192 Term \u0393 nats\n map : \u2200 {\u0393} \u2192 (f : Term \u0393 (nats \u21d2 nats)) \u2192 (b : Term \u0393 bags) \u2192\n Term \u0393 bags\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u0393\u227c\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0393\n\u0393\u227c\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0393 {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u0393\u227c\u0393 {\u0393}\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\nweaken subctx (bag b) = bag b\nweaken subctx (add t\u2081 t\u2082) = add (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (map f b) = map (weaken subctx f) (weaken subctx b)\n\n---------------------------\n-- Semantics of programs --\n---------------------------\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 nats \u27e7Type = \u2115\n\u27e6 bags \u27e7Type = Bag\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nidentity-weakening : \u2200 {\u0393} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192 \u27e6 \u0393\u227c\u0393 {\u0393} \u27e7 \u03c1 \u2261 \u03c1\nidentity-weakening {\u2205} {\u2205} = refl\nidentity-weakening {\u03c4 \u2022 \u0393} {v \u2022 \u03c1} =\n cong\u2082 _\u2022_ {x = v} refl (identity-weakening {\u0393} {\u03c1})\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\u27e6 bag b \u27e7Term \u03c1 = b\n\u27e6 add m n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 map f b \u27e7Term \u03c1 = mapBag (\u27e6 f \u27e7Term \u03c1) (\u27e6 b \u27e7Term \u03c1)\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n_\u27e8$\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\n\n-- infix 0 $ in Haskell\ninfixl 0 _\u27e8$\u27e9_\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = weaken-sound t\u2081 \u03c1 \u27e8$\u27e9 weaken-sound t\u2082 \u03c1\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (bag b) \u03c1 = refl\nweaken-sound (add m n) \u03c1 = cong\u2082 _+_ (weaken-sound m \u03c1) (weaken-sound n \u03c1)\nweaken-sound (map f b) \u03c1 =\n cong\u2082 mapBag (weaken-sound f \u03c1) (weaken-sound b \u03c1)\n\n-----------------------\n-- Syntax of changes --\n-----------------------\n\n-- Validity proofs are not literally embedded in terms.\n-- They are introduced and checked at interpretation time.\n-- Invalid programs are well-formed terms,\n-- they just denote the empty function.\n--\n-- Thus do we avoid the horrible mutual recursions between\n-- the syntax and semantics of changes and between the\n-- program transformation and its correctness, which drives\n-- the type checker to thrashing.\n\ndata \u0394Term : Context \u2192 Type \u2192 Set where\n -- changes to numbers are replacement pairs\n \u0394nat : \u2200 {\u0393} \u2192 (old : \u2115) \u2192 (new : \u2115) \u2192 \u0394Term \u0393 nats\n -- changes to bags are bags\n \u0394bag : \u2200 {\u0393} \u2192 (db : Bag) \u2192 \u0394Term \u0393 bags\n -- changes to variables are variables\n \u0394var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 \u0394Term \u0393 \u03c4\n -- changes to abstractions are binders of x and dx\n \u0394abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (dt : \u0394Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192\n \u0394Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n -- changes to applications are applications of a value and a change\n \u0394app : \u2200 {\u03c3 \u03c4 \u0393}\n (ds : \u0394Term \u0393 (\u03c3 \u21d2 \u03c4))\n ( t : Term \u0393 \u03c3)\n (dt : \u0394Term \u0393 \u03c3) \u2192\n \u0394Term \u0393 \u03c4\n -- changes to addition are changes to their components\n \u0394add : \u2200 {\u0393}\n (ds : \u0394Term \u0393 nats)\n (dt : \u0394Term \u0393 nats) \u2192\n \u0394Term \u0393 nats\n -- There are two kinds of changes to maps:\n -- 0. recomputation,\n -- 1. mapping over changes,\n -- the latter used only with some form of isNil available.\n \u0394map\u2080 : \u2200 {\u0393}\n ( s : Term \u0393 (nats \u21d2 nats))\n (ds : \u0394Term \u0393 (nats \u21d2 nats))\n ( t : Term \u0393 bags)\n (dt : \u0394Term \u0393 bags) \u2192\n \u0394Term \u0393 bags\n \u0394map\u2081 : \u2200 {\u0393}\n ( s : Term \u0393 (nats \u21d2 nats))\n (dt : \u0394Term \u0393 bags) \u2192\n \u0394Term \u0393 bags\n\n---------------------------------\n-- Semantic domains of changes --\n---------------------------------\n\n\u0394Val : Type \u2192 Set\n\u0394Env : Context \u2192 Set\nvalid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\n\n--\n-- \u0394Val \u03c4 is intended to be the semantic domain for changes of values of type\n-- \u03c4, which was obtained by \u27e6 \u0394-Type \u03c4 \u27e7 in other formalizations.\n--\n-- \u0394Val : Type \u2192 Set\n\u0394Val nats = \u2115 \u00d7 \u2115\n\u0394Val bags = Bag\n\u0394Val (\u03c3 \u21d2 \u03c4) = (v : \u27e6 \u03c3 \u27e7) \u2192 (dv : \u0394Val \u03c3) \u2192 valid v dv \u2192 \u0394Val \u03c4\n\n-- \u0394Env : Context \u2192 Set\n\u0394Env \u2205 = EmptySet\n\u0394Env (\u03c4 \u2022 \u0393) = Quadruple\n \u27e6 \u03c4 \u27e7\n (\u03bb _ \u2192 \u0394Val \u03c4)\n (\u03bb v dv \u2192 valid v dv)\n (\u03bb _ _ _ \u2192 \u0394Env \u0393)\n\n_\u2295_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n_\u229d_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\ninfixl 6 _\u2295_ _\u229d_ -- as with + - in GHC.Num\n\n-- valid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\nvalid {nats} n dn = n \u2261 proj\u2081 dn\nvalid {bags} b db = \u22a4\nvalid {\u03c3 \u21d2 \u03c4} f df =\n \u2200 (v : \u27e6 \u03c3 \u27e7) (dv : \u0394Val \u03c3) (R[v,dv] : valid v dv)\n \u2192 valid (f v) (df v dv R[v,dv])\n \u00d7 (f \u2295 df) (v \u2295 dv) \u2261 f v \u2295 df v dv R[v,dv]\n\nR[v,u\u229dv] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229d v)\n\n-- _\u2295_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n_\u2295_ {nats} n dn = proj\u2082 dn\n_\u2295_ {bags} b db = b ++ db\n_\u2295_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u2295 df v (v \u229d v) R[v,u\u229dv]\n\n-- _\u229d_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\n_\u229d_ {nats} m n = (n , m)\n_\u229d_ {bags} b d = b \\\\ d\n_\u229d_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb v dv R[v,dv] \u2192 f (v \u2295 dv) \u229d g v\n\nv\u2295[u\u229dv]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 v \u2295 (u \u229d v) \u2261 u\nv\u2295[u\u229dv]=u {nats} {u} {v} = refl\nv\u2295[u\u229dv]=u {bags} {u} {v} = b++[d\\\\b]=d {v} {u}\nv\u2295[u\u229dv]=u {\u03c3 \u21d2 \u03c4} {u} {v} = extensionality (\u03bb w \u2192\n begin\n (v \u2295 (u \u229d v)) w\n \u2261\u27e8 refl \u27e9 -- for clarity\n v w \u2295 (u (w \u2295 (w \u229d w)) \u229d v w)\n \u2261\u27e8 cong (\u03bb hole \u2192 v w \u2295 (u hole \u229d v w)) v\u2295[u\u229dv]=u \u27e9\n v w \u2295 (u w \u229d v w)\n \u2261\u27e8 v\u2295[u\u229dv]=u \u27e9\n u w\n \u220e) where open \u2261-Reasoning\n\n-- R[v,u\u229dv] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229d v)\nR[v,u\u229dv] {nats} {u} {v} = refl\nR[v,u\u229dv] {bags} {u} {v} = tt\nR[v,u\u229dv] {\u03c3 \u21d2 \u03c4} {u} {v} = \u03bb w dw R[w,dw] \u2192\n let\n w\u2032 = w \u2295 dw\n in\n R[v,u\u229dv]\n ,\n (begin\n (v \u2295 (u \u229d v)) w\u2032\n \u2261\u27e8 cong (\u03bb hole \u2192 hole w\u2032) (v\u2295[u\u229dv]=u {u = u} {v}) \u27e9\n u w\u2032\n \u2261\u27e8 sym (v\u2295[u\u229dv]=u {u = u w\u2032} {v w}) \u27e9\n v w \u2295 (u \u229d v) w dw R[w,dw]\n \u220e) where open \u2261-Reasoning\n\nignore : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394Env \u0393) \u2192 \u27e6 \u0393 \u27e7\nignore {\u2205} \u03c1 = \u2205\nignore {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) = v \u2022 ignore \u03c1\n\nupdate : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394Env \u0393) \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u03c1 = \u2205\nupdate {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) = (v \u2295 dv) \u2022 update \u03c1\n\n--------------------------\n-- Semantics of changes --\n--------------------------\n\n\u27e6_\u27e7\u0394Var : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 \u0394Env \u0393 \u2192 \u0394Val \u03c4\n\u27e6 this \u27e7\u0394Var (cons v dv R[v,dv] \u03c1) = dv\n\u27e6 that x \u27e7\u0394Var (cons v dv R[v,dv] \u03c1) = \u27e6 x \u27e7\u0394Var \u03c1\n\n-- Used to signal free variables of a term.\n-- Totally like subcontext relation _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set\ndata Vars : Context \u2192 Set where\n \u2205 : Vars \u2205\n alter : \u2200 {\u03c4 \u0393} \u2192 Vars \u0393 \u2192 Vars (\u03c4 \u2022 \u0393)\n abide : \u2200 {\u03c4 \u0393} \u2192 Vars \u0393 \u2192 Vars (\u03c4 \u2022 \u0393)\n\n-- Declare everything in \u0393 to be volatile.\nselect-none : {\u0393 : Context} \u2192 Vars \u0393\nselect-none {\u2205} = \u2205\nselect-none {\u03c4 \u2022 \u0393} = alter (select-none {\u0393})\n\nselect-just : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Vars \u0393\nselect-just {\u0393 = \u03c4 \u2022 \u0393\u2080} this = abide select-none\nselect-just (that x) = alter (select-just x)\n\n-- De-facto union of free variables\nFV-union : \u2200 {\u0393} \u2192 Vars \u0393 \u2192 Vars \u0393 \u2192 Vars \u0393\nFV-union \u2205 \u2205 = \u2205\nFV-union (alter us) (alter vs) = alter (FV-union us vs)\nFV-union (alter us) (abide vs) = abide (FV-union us vs)\nFV-union (abide us) (alter vs) = abide (FV-union us vs)\nFV-union (abide us) (abide vs) = abide (FV-union us vs)\n\ntail : \u2200 {\u03c4 \u0393} \u2192 Vars (\u03c4 \u2022 \u0393) \u2192 Vars \u0393\ntail (abide vars) = vars\ntail (alter vars) = vars\n\n-- Free variables of a term.\n-- Free variables are marked as abiding, bound variables altering.\nFV : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Vars \u0393\nFV {\u0393 = \u0393} (nat n) = select-none\nFV {\u0393 = \u0393} (bag b) = select-none\nFV (var x) = select-just x\nFV (abs t) = tail (FV t)\nFV (app s t) = FV-union (FV s) (FV t)\nFV (add s t) = FV-union (FV s) (FV t)\nFV (map s t) = FV-union (FV s) (FV t)\n\n-- A description of variables is honest w.r.t. a \u0394-environment\n-- if every variable described as stable receives the nil change.\ndata Honest : \u2200 {\u0393} \u2192 \u0394Env \u0393 \u2192 Vars \u0393 \u2192 Set where\n clearly : Honest \u2205 \u2205\n alter : \u2200 {\u0393 \u03c4} {v : \u27e6 \u03c4 \u27e7} {dv R[v,dv] vars \u03c1} \u2192\n Honest {\u0393} \u03c1 vars \u2192\n Honest {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) (alter vars)\n abide : \u2200 {\u0393 \u03c4} {v : \u27e6 \u03c4 \u27e7} {dv R[v,dv] vars \u03c1} \u2192\n v \u2295 dv \u2261 v \u2192\n Honest {\u0393} \u03c1 vars \u2192\n Honest {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) (abide vars)\n\n_is-valid-for_ : \u2200 {\u03c4 \u0393} \u2192 \u0394Term \u0393 \u03c4 \u2192 \u0394Env \u0393 \u2192 Set\n\n\u27e6_\u27e7\u0394 : \u2200 {\u03c4 \u0393} \u2192\n (t : \u0394Term \u0393 \u03c4) \u2192 (\u03c1 : \u0394Env \u0393) \u2192 t is-valid-for \u03c1 \u2192\n \u0394Val \u03c4\n\n-- _is-valid-for_ : \u2200 {\u03c4 \u0393} \u2192 \u0394Term \u0393 \u03c4 \u2192 \u0394Env \u0393 \u2192 Set\n\u0394nat old new is-valid-for \u03c1 = \u22a4\n\u0394bag db is-valid-for \u03c1 = \u22a4\n\u0394var x is-valid-for \u03c1 = \u22a4\n\n_is-valid-for_ {\u03c3 \u21d2 \u03c4} (\u0394abs dt) \u03c1 =\n (v : \u27e6 \u03c3 \u27e7) (dv : \u0394Val \u03c3) (R[v,dv] : valid v dv) \u2192\n _is-valid-for_ dt (cons v dv R[v,dv] \u03c1)\n\n\u0394app ds t dt is-valid-for \u03c1 = Quadruple\n (ds is-valid-for \u03c1)\n (\u03bb _ \u2192 dt is-valid-for \u03c1)\n (\u03bb _ v-dt \u2192 valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1 v-dt))\n (\u03bb _ _ _ \u2192 \u22a4)\n\n\u0394add ds dt is-valid-for \u03c1 = couple\n (ds is-valid-for \u03c1)\n (dt is-valid-for \u03c1)\n\n\u0394map\u2080 s ds t dt is-valid-for \u03c1 = couple\n (ds is-valid-for \u03c1)\n (dt is-valid-for \u03c1)\n\n\u0394map\u2081 s db is-valid-for \u03c1 = couple\n (db is-valid-for \u03c1)\n (Honest \u03c1 (FV s))\n\n\u27e6 \u0394nat old new \u27e7\u0394 \u03c1 tt = old , new\n\u27e6 \u0394bag db \u27e7\u0394 \u03c1 tt = db\n\u27e6 \u0394var x \u27e7\u0394 \u03c1 tt = \u27e6 x \u27e7\u0394Var \u03c1\n\n\u27e6 \u0394abs dt \u27e7\u0394 \u03c1 make-valid = \u03bb v dv R[v,dv] \u2192\n \u27e6 dt \u27e7\u0394 (cons v dv R[v,dv] \u03c1) (make-valid v dv R[v,dv])\n\n\u27e6 \u0394app ds t dt \u27e7\u0394 \u03c1 (cons v-ds v-dt R[ds,dt] _) =\n \u27e6 ds \u27e7\u0394 \u03c1 v-ds (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1 v-dt) R[ds,dt]\n\n\u27e6 \u0394add ds dt \u27e7\u0394 \u03c1 (cons v-ds v-dt _ _) =\n let\n (old-s , new-s) = \u27e6 ds \u27e7\u0394 \u03c1 v-ds\n (old-t , new-t) = \u27e6 dt \u27e7\u0394 \u03c1 v-dt\n in\n (old-s + old-t , new-s + new-t)\n\n\u27e6 \u0394map\u2080 s ds t dt \u27e7\u0394 \u03c1 (cons v-ds v-dt _ _) =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n v = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 ds \u27e7\u0394 \u03c1 v-ds\n dv = \u27e6 dt \u27e7\u0394 \u03c1 v-dt\n in\n mapBag (f \u2295 df) (v \u2295 dv) \u229d mapBag f v\n\n\u27e6 \u0394map\u2081 s dt \u27e7\u0394 \u03c1 (cons v-dt honesty _ _) =\n mapBag (\u27e6 s \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1 v-dt)\n\n-- Minor issue about concrete syntax\n--\n-- Because \u27e6_\u27e7\u0394 have dependently typed arguments,\n-- we can't make it an instance of the Meaning\n-- type class and can't use \u27e6_\u27e7 on \u0394Terms.\n--\n-- Error message:\n-- Cannot instantiate the metavariable _872 to solution ((\u03c1 : \u0394Env .\u0393)\n-- \u2192 t is-valid-for \u03c1 \u2192 \u0394Val .\u03c4) since it contains the variable t\n-- which is not in scope of the metavariable or irrelevant in the\n-- metavariable but relevant in the solution\n-- when checking that the expression \u27e6_\u27e7\u0394 has type\n-- \u0394Term .\u0393 .\u03c4 \u2192 _Semantics_872\n--\n-- meaning-\u0394Term : \u2200 {\u03c4 \u0393} \u2192 Meaning (\u0394Term \u0393 \u03c4)\n-- meaning-\u0394Term = meaning \u27e6_\u27e7\u0394\n\n----------------------------\n-- Program transformation --\n----------------------------\n\nderive : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394Term \u0393 \u03c4\n\nderive (nat n) = \u0394nat n n\nderive (bag b) = \u0394bag emptyBag\nderive (var x) = \u0394var x\nderive (abs t) = \u0394abs (derive t)\nderive (app s t) = \u0394app (derive s) t (derive t)\nderive (add s t) = \u0394add (derive s) (derive t)\nderive (map f b) = \u0394map\u2080 f (derive f) b (derive b)\n\n-----------------\n-- Correctness --\n-----------------\n\nunrestricted : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) {\u03c1 : \u0394Env \u0393} \u2192\n derive t is-valid-for \u03c1\n\nvalidity : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t))\n\ncorrectness : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393}\n \u2192 \u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n \u2261 \u27e6 t \u27e7 (update \u03c1)\n\n-- Corollary: (f \u2295 df) (v \u2295 dv) = f v \u2295 df v dv\n\ncorollary : \u2200 {\u03c3 \u03c4 \u0393}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) (t : Term \u0393 \u03c3) {\u03c1 : \u0394Env \u0393} \u2192\n (\u27e6 s \u27e7 (ignore \u03c1) \u2295 \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s))\n (\u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t))\n \u2261 \u27e6 s \u27e7 (ignore \u03c1) (\u27e6 t \u27e7 (ignore \u03c1)) \u2295\n \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s) (\u27e6 t \u27e7 (ignore \u03c1))\n (\u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)) (validity {t = t})\n\ncorollary s t {\u03c1} = proj\u2082\n (validity {t = s} (\u27e6 t \u27e7 (ignore \u03c1))\n (\u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)) (validity {t = t}))\n\nunrestricted (nat n) = tt\nunrestricted (bag b) = tt\nunrestricted (var x) {\u03c1} = tt\nunrestricted (abs t) {\u03c1} = (\u03bb _ _ _ \u2192 unrestricted t)\nunrestricted (app s t) {\u03c1} = cons\n (unrestricted (s))\n (unrestricted (t))\n (validity {t = t}) tt\nunrestricted (add s t) {\u03c1} = cons\n (unrestricted (s))\n (unrestricted (t)) tt tt\nunrestricted (map s t) {\u03c1} = cons\n (unrestricted (s))\n (unrestricted (t)) tt tt\n\nvalidity-var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} \u2192 valid (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 x \u27e7\u0394Var \u03c1)\n\nvalidity-var this {cons v dv R[v,dv] \u03c1} = R[v,dv]\nvalidity-var (that x) {cons v dv R[v,dv] \u03c1} = validity-var x\n\nvalidity {t = nat n} = refl\nvalidity {t = bag b} = tt\nvalidity {t = var x} = validity-var x\nvalidity {t = map f b} = tt\nvalidity {t = add s t} = cong\u2082 _+_ (validity {t = s}) (validity {t = t})\n\nvalidity {t = app s t} {\u03c1} =\n let\n v = \u27e6 t \u27e7 (ignore \u03c1)\n dv = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n in\n proj\u2081 (validity {t = s} {\u03c1} v dv (validity {t = t} {\u03c1}))\n\nvalidity {t = abs t} {\u03c1} = \u03bb v dv R[v,dv] \u2192\n let\n v\u2032 = v \u2295 dv\n dv\u2032 = v\u2032 \u229d v\u2032\n \u03c1\u2081 = cons v dv R[v,dv] \u03c1\n \u03c1\u2082 = cons v\u2032 dv\u2032 R[v,u\u229dv] \u03c1\n in\n validity {t = t} {\u03c1\u2081}\n ,\n (begin\n \u27e6 t \u27e7 (ignore \u03c1\u2082) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1\u2082 (unrestricted t)\n \u2261\u27e8 correctness {t = t} {\u03c1\u2082} \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2082)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 (hole \u2022 update \u03c1)) v\u2295[u\u229dv]=u \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2081)\n \u2261\u27e8 sym (correctness {t = t} {\u03c1\u2081}) \u27e9\n \u27e6 t \u27e7 (ignore \u03c1\u2081) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1\u2081 (unrestricted t)\n \u220e) where open \u2261-Reasoning\n\ncorrectVar : \u2200 {\u03c4 \u0393} {x : Var \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n \u27e6 x \u27e7 (ignore \u03c1) \u2295 \u27e6 x \u27e7\u0394Var \u03c1 \u2261 \u27e6 x \u27e7 (update \u03c1)\n\ncorrectVar {x = this } {cons v dv R[v,dv] \u03c1} = refl\ncorrectVar {x = that y} {cons v dv R[v,dv] \u03c1} = correctVar {x = y} {\u03c1}\n\ncorrectness {t = nat n} = refl\ncorrectness {t = bag b} = b++\u2205=b\ncorrectness {t = var x} = correctVar {x = x}\n\ncorrectness {t = add s t} =\n cong\u2082 _+_ (correctness {t = s}) (correctness {t = t})\n\ncorrectness {t = map s t} {\u03c1} =\n trans (b++[d\\\\b]=d {mapBag f b} {mapBag (f \u2295 df) (b \u2295 db)})\n (cong\u2082 mapBag (correctness {t = s}) (correctness {t = t}))\n where\n f = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n db = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n\ncorrectness {t = app s t} {\u03c1} =\n let\n v = \u27e6 t \u27e7 (ignore \u03c1)\n dv = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n in trans\n (sym (proj\u2082 (validity {t = s} {\u03c1} v dv (validity {t = t} {\u03c1}))))\n (correctness {t = s} \u27e8$\u27e9 correctness {t = t})\n\ncorrectness {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u0393} {abs t} {\u03c1} = extensionality (\u03bb v \u2192\n let\n \u03c1\u2032 : \u0394Env (\u03c4\u2081 \u2022 \u0393)\n \u03c1\u2032 = cons v (v \u229d v) R[v,u\u229dv] \u03c1\n in\n begin\n \u27e6 t \u27e7 (ignore \u03c1\u2032) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1\u2032 (unrestricted t)\n \u2261\u27e8 correctness {t = t} {\u03c1\u2032} \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2032)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 (hole \u2022 update \u03c1)) v\u2295[u\u229dv]=u \u27e9\n \u27e6 t \u27e7 (v \u2022 update \u03c1)\n \u220e\n ) where open \u2261-Reasoning\n","old_contents":"{-\nThe goal of this file is to make the domain of \u0394 types smaller\nso as to be nearer to full abstraction and hopefully close\nenough for the purpose of having explicit nil changes.\n-}\n\nmodule TaggedDeltaTypes where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Nat\nopen import Data.Unit using (\u22a4 ; tt)\nimport Data.Integer as \u2124\nimport Data.Product as Product\nopen import Data.Product using (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\nimport Level\n\n---------------------------------------------------------\n-- Postulates: Extensionality and bag properties (#55) --\n---------------------------------------------------------\n\npostulate extensionality : Extensionality Level.zero Level.zero\n-- Instead of:\n-- open import Data.NatBag renaming\n--- (map to mapBag ; empty to emptyBag ; update to updateBag)\n-- open import Data.NatBag.Properties\npostulate Bag : Set\npostulate emptyBag : Bag\npostulate mapBag : (\u2115 \u2192 \u2115) \u2192 Bag \u2192 Bag\npostulate _++_ : Bag \u2192 Bag \u2192 Bag\npostulate _\\\\_ : Bag \u2192 Bag \u2192 Bag\ninfixr 5 _++_\ninfixl 9 _\\\\_\npostulate b++\u2205=b : \u2200 {b : Bag} \u2192 b ++ emptyBag \u2261 b\npostulate b++[d\\\\b]=d : \u2200 {b d} \u2192 b ++ (d \\\\ b) \u2261 d\npostulate [b++d]\\\\b=d : \u2200 {b d} \u2192 (b ++ d) \\\\ b \u2261 d\npostulate\n map-over-++ : \u2200 {f b d} \u2192\n mapBag f (b ++ d) \u2261 mapBag f b ++ mapBag f d\n\n----------------------------\n-- Useful data structures --\n----------------------------\n\nrecord Quadruple\n (A : Set) (B : A \u2192 Set) (C : (a : A) \u2192 B a \u2192 Set)\n (D : (a : A) \u2192 (b : B a) \u2192 (c : C a b) \u2192 Set): Set where\n constructor cons\n field\n car : A\n cadr : B car\n caddr : C car cadr\n cdddr : D car cadr caddr\n\nopen Quadruple public\n\ncouple : Set \u2192 Set \u2192 Set\ncouple A B = Quadruple A (\u03bb _ \u2192 B) (\u03bb _ _ \u2192 \u22a4) (\u03bb _ _ _ \u2192 \u22a4)\n\ntriple : Set \u2192 Set \u2192 Set \u2192 Set\ntriple A B C = Quadruple A (\u03bb _ \u2192 B) (\u03bb _ _ \u2192 C) (\u03bb _ _ _ \u2192 \u22a4)\n\n------------------------\n-- Syntax of programs --\n------------------------\n\ndata Type : Set where\n nats : Type\n bags : Type\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u03c4 \u0393} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u03c3 \u03c4 \u0393} \u2192 (x : Var \u0393 \u03c3) \u2192 Var (\u03c4 \u2022 \u0393) \u03c3\n\ndata Term : Context -> Type -> Set where\n\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n bag : \u2200 {\u0393} \u2192 (b : Bag) \u2192 Term \u0393 bags\n\n var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4 \u0393} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 Term \u0393 (\u03c3 \u21d2 \u03c4)\n app : \u2200 {\u03c3 \u03c4 \u0393} \u2192 (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) (t : Term \u0393 \u03c3) \u2192 Term \u0393 \u03c4\n\n add : \u2200 {\u0393} \u2192 (s : Term \u0393 nats) \u2192 (t : Term \u0393 nats) \u2192 Term \u0393 nats\n map : \u2200 {\u0393} \u2192 (f : Term \u0393 (nats \u21d2 nats)) \u2192 (b : Term \u0393 bags) \u2192\n Term \u0393 bags\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u0393\u227c\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0393\n\u0393\u227c\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0393 {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u0393\u227c\u0393 {\u0393}\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\nweaken subctx (bag b) = bag b\nweaken subctx (add t\u2081 t\u2082) = add (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (map f b) = map (weaken subctx f) (weaken subctx b)\n\n---------------------------\n-- Semantics of programs --\n---------------------------\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 nats \u27e7Type = \u2115\n\u27e6 bags \u27e7Type = Bag\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nidentity-weakening : \u2200 {\u0393} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192 \u27e6 \u0393\u227c\u0393 {\u0393} \u27e7 \u03c1 \u2261 \u03c1\nidentity-weakening {\u2205} {\u2205} = refl\nidentity-weakening {\u03c4 \u2022 \u0393} {v \u2022 \u03c1} =\n cong\u2082 _\u2022_ {x = v} refl (identity-weakening {\u0393} {\u03c1})\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\u27e6 bag b \u27e7Term \u03c1 = b\n\u27e6 add m n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 map f b \u27e7Term \u03c1 = mapBag (\u27e6 f \u27e7Term \u03c1) (\u27e6 b \u27e7Term \u03c1)\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n_\u27e8$\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\n\n-- infix 0 $ in Haskell\ninfixl 0 _\u27e8$\u27e9_\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = weaken-sound t\u2081 \u03c1 \u27e8$\u27e9 weaken-sound t\u2082 \u03c1\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (bag b) \u03c1 = refl\nweaken-sound (add m n) \u03c1 = cong\u2082 _+_ (weaken-sound m \u03c1) (weaken-sound n \u03c1)\nweaken-sound (map f b) \u03c1 =\n cong\u2082 mapBag (weaken-sound f \u03c1) (weaken-sound b \u03c1)\n\n-----------------------\n-- Syntax of changes --\n-----------------------\n\n-- Validity proofs are not literally embedded in terms.\n-- They are introduced and checked at interpretation time.\n-- Invalid programs are well-formed terms,\n-- they just denote the empty function.\n--\n-- Thus do we avoid the horrible mutual recursions between\n-- the syntax and semantics of changes and between the\n-- program transformation and its correctness, which drives\n-- the type checker to thrashing.\n\ndata \u0394Term : Context \u2192 Type \u2192 Set where\n -- changes to numbers are replacement pairs\n \u0394nat : \u2200 {\u0393} \u2192 (old : \u2115) \u2192 (new : \u2115) \u2192 \u0394Term \u0393 nats\n -- changes to bags are bags\n \u0394bag : \u2200 {\u0393} \u2192 (db : Bag) \u2192 \u0394Term \u0393 bags\n -- changes to variables are variables\n \u0394var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 \u0394Term \u0393 \u03c4\n -- changes to abstractions are binders of x and dx\n \u0394abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (dt : \u0394Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192\n \u0394Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n -- changes to applications are applications of a value and a change\n \u0394app : \u2200 {\u03c3 \u03c4 \u0393}\n (ds : \u0394Term \u0393 (\u03c3 \u21d2 \u03c4))\n ( t : Term \u0393 \u03c3)\n (dt : \u0394Term \u0393 \u03c3) \u2192\n \u0394Term \u0393 \u03c4\n -- changes to addition are changes to their components\n \u0394add : \u2200 {\u0393}\n (ds : \u0394Term \u0393 nats)\n (dt : \u0394Term \u0393 nats) \u2192\n \u0394Term \u0393 nats\n -- There are two kinds of changes to maps:\n -- 0. recomputation,\n -- 1. mapping over changes,\n -- the latter used only with some form of isNil available.\n \u0394map\u2080 : \u2200 {\u0393}\n ( s : Term \u0393 (nats \u21d2 nats))\n (ds : \u0394Term \u0393 (nats \u21d2 nats))\n ( t : Term \u0393 bags)\n (dt : \u0394Term \u0393 bags) \u2192\n \u0394Term \u0393 bags\n \u0394map\u2081 : \u2200 {\u0393}\n ( s : Term \u0393 (nats \u21d2 nats))\n (dt : \u0394Term \u0393 bags) \u2192\n \u0394Term \u0393 bags\n\n---------------------------------\n-- Semantic domains of changes --\n---------------------------------\n\n\u0394Val : Type \u2192 Set\n\u0394Env : Context \u2192 Set\nvalid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\n\n-- \u0394Val : Type \u2192 Set\n\u0394Val nats = \u2115 \u00d7 \u2115\n\u0394Val bags = Bag\n\u0394Val (\u03c3 \u21d2 \u03c4) = (v : \u27e6 \u03c3 \u27e7) \u2192 (dv : \u0394Val \u03c3) \u2192 valid v dv \u2192 \u0394Val \u03c4\n\n-- \u0394Env : Context \u2192 Set\n\u0394Env \u2205 = EmptySet\n\u0394Env (\u03c4 \u2022 \u0393) = Quadruple\n \u27e6 \u03c4 \u27e7\n (\u03bb _ \u2192 \u0394Val \u03c4)\n (\u03bb v dv \u2192 valid v dv)\n (\u03bb _ _ _ \u2192 \u0394Env \u0393)\n\n_\u2295_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n_\u229d_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\ninfixl 6 _\u2295_ _\u229d_ -- as with + - in GHC.Num\n\n-- valid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\nvalid {nats} n dn = n \u2261 proj\u2081 dn\nvalid {bags} b db = \u22a4\nvalid {\u03c3 \u21d2 \u03c4} f df =\n \u2200 (v : \u27e6 \u03c3 \u27e7) (dv : \u0394Val \u03c3) (R[v,dv] : valid v dv)\n \u2192 valid (f v) (df v dv R[v,dv])\n \u00d7 (f \u2295 df) (v \u2295 dv) \u2261 f v \u2295 df v dv R[v,dv]\n\nR[v,u\u229dv] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229d v)\n\n-- _\u2295_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n_\u2295_ {nats} n dn = proj\u2082 dn\n_\u2295_ {bags} b db = b ++ db\n_\u2295_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u2295 df v (v \u229d v) R[v,u\u229dv]\n\n-- _\u229d_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\n_\u229d_ {nats} m n = (n , m)\n_\u229d_ {bags} b d = b \\\\ d\n_\u229d_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb v dv R[v,dv] \u2192 f (v \u2295 dv) \u229d g v\n\nv\u2295[u\u229dv]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 v \u2295 (u \u229d v) \u2261 u\nv\u2295[u\u229dv]=u {nats} {u} {v} = refl\nv\u2295[u\u229dv]=u {bags} {u} {v} = b++[d\\\\b]=d {v} {u}\nv\u2295[u\u229dv]=u {\u03c3 \u21d2 \u03c4} {u} {v} = extensionality (\u03bb w \u2192\n begin\n (v \u2295 (u \u229d v)) w\n \u2261\u27e8 refl \u27e9 -- for clarity\n v w \u2295 (u (w \u2295 (w \u229d w)) \u229d v w)\n \u2261\u27e8 cong (\u03bb hole \u2192 v w \u2295 (u hole \u229d v w)) v\u2295[u\u229dv]=u \u27e9\n v w \u2295 (u w \u229d v w)\n \u2261\u27e8 v\u2295[u\u229dv]=u \u27e9\n u w\n \u220e) where open \u2261-Reasoning\n\n-- R[v,u\u229dv] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229d v)\nR[v,u\u229dv] {nats} {u} {v} = refl\nR[v,u\u229dv] {bags} {u} {v} = tt\nR[v,u\u229dv] {\u03c3 \u21d2 \u03c4} {u} {v} = \u03bb w dw R[w,dw] \u2192\n let\n w\u2032 = w \u2295 dw\n in\n R[v,u\u229dv]\n ,\n (begin\n (v \u2295 (u \u229d v)) w\u2032\n \u2261\u27e8 cong (\u03bb hole \u2192 hole w\u2032) (v\u2295[u\u229dv]=u {u = u} {v}) \u27e9\n u w\u2032\n \u2261\u27e8 sym (v\u2295[u\u229dv]=u {u = u w\u2032} {v w}) \u27e9\n v w \u2295 (u \u229d v) w dw R[w,dw]\n \u220e) where open \u2261-Reasoning\n\nignore : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394Env \u0393) \u2192 \u27e6 \u0393 \u27e7\nignore {\u2205} \u03c1 = \u2205\nignore {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) = v \u2022 ignore \u03c1\n\nupdate : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394Env \u0393) \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u03c1 = \u2205\nupdate {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) = (v \u2295 dv) \u2022 update \u03c1\n\n--------------------------\n-- Semantics of changes --\n--------------------------\n\n\u27e6_\u27e7\u0394Var : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 \u0394Env \u0393 \u2192 \u0394Val \u03c4\n\u27e6 this \u27e7\u0394Var (cons v dv R[v,dv] \u03c1) = dv\n\u27e6 that x \u27e7\u0394Var (cons v dv R[v,dv] \u03c1) = \u27e6 x \u27e7\u0394Var \u03c1\n\n-- Used to signal free variables of a term.\n-- Totally like subcontext relation _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set\ndata Vars : Context \u2192 Set where\n \u2205 : Vars \u2205\n alter : \u2200 {\u03c4 \u0393} \u2192 Vars \u0393 \u2192 Vars (\u03c4 \u2022 \u0393)\n abide : \u2200 {\u03c4 \u0393} \u2192 Vars \u0393 \u2192 Vars (\u03c4 \u2022 \u0393)\n\n-- Declare everything in \u0393 to be volatile.\nselect-none : {\u0393 : Context} \u2192 Vars \u0393\nselect-none {\u2205} = \u2205\nselect-none {\u03c4 \u2022 \u0393} = alter (select-none {\u0393})\n\nselect-just : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Vars \u0393\nselect-just {\u0393 = \u03c4 \u2022 \u0393\u2080} this = abide select-none\nselect-just (that x) = alter (select-just x)\n\n-- De-facto union of free variables\nFV-union : \u2200 {\u0393} \u2192 Vars \u0393 \u2192 Vars \u0393 \u2192 Vars \u0393\nFV-union \u2205 \u2205 = \u2205\nFV-union (alter us) (alter vs) = alter (FV-union us vs)\nFV-union (alter us) (abide vs) = abide (FV-union us vs)\nFV-union (abide us) (alter vs) = abide (FV-union us vs)\nFV-union (abide us) (abide vs) = abide (FV-union us vs)\n\ntail : \u2200 {\u03c4 \u0393} \u2192 Vars (\u03c4 \u2022 \u0393) \u2192 Vars \u0393\ntail (abide vars) = vars\ntail (alter vars) = vars\n\n-- Free variables of a term.\n-- Free variables are marked as abiding, bound variables altering.\nFV : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Vars \u0393\nFV {\u0393 = \u0393} (nat n) = select-none\nFV {\u0393 = \u0393} (bag b) = select-none\nFV (var x) = select-just x\nFV (abs t) = tail (FV t)\nFV (app s t) = FV-union (FV s) (FV t)\nFV (add s t) = FV-union (FV s) (FV t)\nFV (map s t) = FV-union (FV s) (FV t)\n\n-- A description of variables is honest w.r.t. a \u0394-environment\n-- if every variable described as stable receives the nil change.\ndata Honest : \u2200 {\u0393} \u2192 \u0394Env \u0393 \u2192 Vars \u0393 \u2192 Set where\n clearly : Honest \u2205 \u2205\n alter : \u2200 {\u0393 \u03c4} {v : \u27e6 \u03c4 \u27e7} {dv R[v,dv] vars \u03c1} \u2192\n Honest {\u0393} \u03c1 vars \u2192\n Honest {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) (alter vars)\n abide : \u2200 {\u0393 \u03c4} {v : \u27e6 \u03c4 \u27e7} {dv R[v,dv] vars \u03c1} \u2192\n v \u2295 dv \u2261 v \u2192\n Honest {\u0393} \u03c1 vars \u2192\n Honest {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) (abide vars)\n\n_is-valid-for_ : \u2200 {\u03c4 \u0393} \u2192 \u0394Term \u0393 \u03c4 \u2192 \u0394Env \u0393 \u2192 Set\n\n\u27e6_\u27e7\u0394 : \u2200 {\u03c4 \u0393} \u2192\n (t : \u0394Term \u0393 \u03c4) \u2192 (\u03c1 : \u0394Env \u0393) \u2192 t is-valid-for \u03c1 \u2192\n \u0394Val \u03c4\n\n-- _is-valid-for_ : \u2200 {\u03c4 \u0393} \u2192 \u0394Term \u0393 \u03c4 \u2192 \u0394Env \u0393 \u2192 Set\n\u0394nat old new is-valid-for \u03c1 = \u22a4\n\u0394bag db is-valid-for \u03c1 = \u22a4\n\u0394var x is-valid-for \u03c1 = \u22a4\n\n_is-valid-for_ {\u03c3 \u21d2 \u03c4} (\u0394abs dt) \u03c1 =\n (v : \u27e6 \u03c3 \u27e7) (dv : \u0394Val \u03c3) (R[v,dv] : valid v dv) \u2192\n _is-valid-for_ dt (cons v dv R[v,dv] \u03c1)\n\n\u0394app ds t dt is-valid-for \u03c1 = Quadruple\n (ds is-valid-for \u03c1)\n (\u03bb _ \u2192 dt is-valid-for \u03c1)\n (\u03bb _ v-dt \u2192 valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1 v-dt))\n (\u03bb _ _ _ \u2192 \u22a4)\n\n\u0394add ds dt is-valid-for \u03c1 = couple\n (ds is-valid-for \u03c1)\n (dt is-valid-for \u03c1)\n\n\u0394map\u2080 s ds t dt is-valid-for \u03c1 = couple\n (ds is-valid-for \u03c1)\n (dt is-valid-for \u03c1)\n\n\u0394map\u2081 s db is-valid-for \u03c1 = couple\n (db is-valid-for \u03c1)\n (Honest \u03c1 (FV s))\n\n\u27e6 \u0394nat old new \u27e7\u0394 \u03c1 tt = old , new\n\u27e6 \u0394bag db \u27e7\u0394 \u03c1 tt = db\n\u27e6 \u0394var x \u27e7\u0394 \u03c1 tt = \u27e6 x \u27e7\u0394Var \u03c1\n\n\u27e6 \u0394abs dt \u27e7\u0394 \u03c1 make-valid = \u03bb v dv R[v,dv] \u2192\n \u27e6 dt \u27e7\u0394 (cons v dv R[v,dv] \u03c1) (make-valid v dv R[v,dv])\n\n\u27e6 \u0394app ds t dt \u27e7\u0394 \u03c1 (cons v-ds v-dt R[ds,dt] _) =\n \u27e6 ds \u27e7\u0394 \u03c1 v-ds (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1 v-dt) R[ds,dt]\n\n\u27e6 \u0394add ds dt \u27e7\u0394 \u03c1 (cons v-ds v-dt _ _) =\n let\n (old-s , new-s) = \u27e6 ds \u27e7\u0394 \u03c1 v-ds\n (old-t , new-t) = \u27e6 dt \u27e7\u0394 \u03c1 v-dt\n in\n (old-s + old-t , new-s + new-t)\n\n\u27e6 \u0394map\u2080 s ds t dt \u27e7\u0394 \u03c1 (cons v-ds v-dt _ _) =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n v = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 ds \u27e7\u0394 \u03c1 v-ds\n dv = \u27e6 dt \u27e7\u0394 \u03c1 v-dt\n in\n mapBag (f \u2295 df) (v \u2295 dv) \u229d mapBag f v\n\n\u27e6 \u0394map\u2081 s dt \u27e7\u0394 \u03c1 (cons v-dt honesty _ _) =\n mapBag (\u27e6 s \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1 v-dt)\n\n-- Minor issue about concrete syntax\n--\n-- Because \u27e6_\u27e7\u0394 have dependently typed arguments,\n-- we can't make it an instance of the Meaning\n-- type class and can't use \u27e6_\u27e7 on \u0394Terms.\n--\n-- Error message:\n-- Cannot instantiate the metavariable _872 to solution ((\u03c1 : \u0394Env .\u0393)\n-- \u2192 t is-valid-for \u03c1 \u2192 \u0394Val .\u03c4) since it contains the variable t\n-- which is not in scope of the metavariable or irrelevant in the\n-- metavariable but relevant in the solution\n-- when checking that the expression \u27e6_\u27e7\u0394 has type\n-- \u0394Term .\u0393 .\u03c4 \u2192 _Semantics_872\n--\n-- meaning-\u0394Term : \u2200 {\u03c4 \u0393} \u2192 Meaning (\u0394Term \u0393 \u03c4)\n-- meaning-\u0394Term = meaning \u27e6_\u27e7\u0394\n\n----------------------------\n-- Program transformation --\n----------------------------\n\nderive : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394Term \u0393 \u03c4\n\nderive (nat n) = \u0394nat n n\nderive (bag b) = \u0394bag emptyBag\nderive (var x) = \u0394var x\nderive (abs t) = \u0394abs (derive t)\nderive (app s t) = \u0394app (derive s) t (derive t)\nderive (add s t) = \u0394add (derive s) (derive t)\nderive (map f b) = \u0394map\u2080 f (derive f) b (derive b)\n\n-----------------\n-- Correctness --\n-----------------\n\nunrestricted : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) {\u03c1 : \u0394Env \u0393} \u2192\n derive t is-valid-for \u03c1\n\nvalidity : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t))\n\ncorrectness : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393}\n \u2192 \u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n \u2261 \u27e6 t \u27e7 (update \u03c1)\n\n-- Corollary: (f \u2295 df) (v \u2295 dv) = f v \u2295 df v dv\n\ncorollary : \u2200 {\u03c3 \u03c4 \u0393}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) (t : Term \u0393 \u03c3) {\u03c1 : \u0394Env \u0393} \u2192\n (\u27e6 s \u27e7 (ignore \u03c1) \u2295 \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s))\n (\u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t))\n \u2261 \u27e6 s \u27e7 (ignore \u03c1) (\u27e6 t \u27e7 (ignore \u03c1)) \u2295\n \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s) (\u27e6 t \u27e7 (ignore \u03c1))\n (\u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)) (validity {t = t})\n\ncorollary s t {\u03c1} = proj\u2082\n (validity {t = s} (\u27e6 t \u27e7 (ignore \u03c1))\n (\u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)) (validity {t = t}))\n\nunrestricted (nat n) = tt\nunrestricted (bag b) = tt\nunrestricted (var x) {\u03c1} = tt\nunrestricted (abs t) {\u03c1} = (\u03bb _ _ _ \u2192 unrestricted t)\nunrestricted (app s t) {\u03c1} = cons\n (unrestricted (s))\n (unrestricted (t))\n (validity {t = t}) tt\nunrestricted (add s t) {\u03c1} = cons\n (unrestricted (s))\n (unrestricted (t)) tt tt\nunrestricted (map s t) {\u03c1} = cons\n (unrestricted (s))\n (unrestricted (t)) tt tt\n\nvalidity-var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} \u2192 valid (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 x \u27e7\u0394Var \u03c1)\n\nvalidity-var this {cons v dv R[v,dv] \u03c1} = R[v,dv]\nvalidity-var (that x) {cons v dv R[v,dv] \u03c1} = validity-var x\n\nvalidity {t = nat n} = refl\nvalidity {t = bag b} = tt\nvalidity {t = var x} = validity-var x\nvalidity {t = map f b} = tt\nvalidity {t = add s t} = cong\u2082 _+_ (validity {t = s}) (validity {t = t})\n\nvalidity {t = app s t} {\u03c1} =\n let\n v = \u27e6 t \u27e7 (ignore \u03c1)\n dv = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n in\n proj\u2081 (validity {t = s} {\u03c1} v dv (validity {t = t} {\u03c1}))\n\nvalidity {t = abs t} {\u03c1} = \u03bb v dv R[v,dv] \u2192\n let\n v\u2032 = v \u2295 dv\n dv\u2032 = v\u2032 \u229d v\u2032\n \u03c1\u2081 = cons v dv R[v,dv] \u03c1\n \u03c1\u2082 = cons v\u2032 dv\u2032 R[v,u\u229dv] \u03c1\n in\n validity {t = t} {\u03c1\u2081}\n ,\n (begin\n \u27e6 t \u27e7 (ignore \u03c1\u2082) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1\u2082 (unrestricted t)\n \u2261\u27e8 correctness {t = t} {\u03c1\u2082} \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2082)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 (hole \u2022 update \u03c1)) v\u2295[u\u229dv]=u \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2081)\n \u2261\u27e8 sym (correctness {t = t} {\u03c1\u2081}) \u27e9\n \u27e6 t \u27e7 (ignore \u03c1\u2081) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1\u2081 (unrestricted t)\n \u220e) where open \u2261-Reasoning\n\ncorrectVar : \u2200 {\u03c4 \u0393} {x : Var \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n \u27e6 x \u27e7 (ignore \u03c1) \u2295 \u27e6 x \u27e7\u0394Var \u03c1 \u2261 \u27e6 x \u27e7 (update \u03c1)\n\ncorrectVar {x = this } {cons v dv R[v,dv] \u03c1} = refl\ncorrectVar {x = that y} {cons v dv R[v,dv] \u03c1} = correctVar {x = y} {\u03c1}\n\ncorrectness {t = nat n} = refl\ncorrectness {t = bag b} = b++\u2205=b\ncorrectness {t = var x} = correctVar {x = x}\n\ncorrectness {t = add s t} =\n cong\u2082 _+_ (correctness {t = s}) (correctness {t = t})\n\ncorrectness {t = map s t} {\u03c1} =\n trans (b++[d\\\\b]=d {mapBag f b} {mapBag (f \u2295 df) (b \u2295 db)})\n (cong\u2082 mapBag (correctness {t = s}) (correctness {t = t}))\n where\n f = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n db = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n\ncorrectness {t = app s t} {\u03c1} =\n let\n v = \u27e6 t \u27e7 (ignore \u03c1)\n dv = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n in trans\n (sym (proj\u2082 (validity {t = s} {\u03c1} v dv (validity {t = t} {\u03c1}))))\n (correctness {t = s} \u27e8$\u27e9 correctness {t = t})\n\ncorrectness {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u0393} {abs t} {\u03c1} = extensionality (\u03bb v \u2192\n let\n \u03c1\u2032 : \u0394Env (\u03c4\u2081 \u2022 \u0393)\n \u03c1\u2032 = cons v (v \u229d v) R[v,u\u229dv] \u03c1\n in\n begin\n \u27e6 t \u27e7 (ignore \u03c1\u2032) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1\u2032 (unrestricted t)\n \u2261\u27e8 correctness {t = t} {\u03c1\u2032} \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2032)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 (hole \u2022 update \u03c1)) v\u2295[u\u229dv]=u \u27e9\n \u27e6 t \u27e7 (v \u2022 update \u03c1)\n \u220e\n ) where open \u2261-Reasoning\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"0a23f43324de7047f601450cf6e3c023a7f6ce68","subject":"Updated doc for an unproven conjecture.","message":"Updated doc for an unproven conjecture.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/PA\/Axiomatic\/Mendelson\/Properties\/UnprovedATP.agda","new_file":"src\/fot\/PA\/Axiomatic\/Mendelson\/Properties\/UnprovedATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Unproven PA properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule PA.Axiomatic.Mendelson.Properties.UnprovedATP where\n\nopen import PA.Axiomatic.Mendelson.Base\nopen import PA.Axiomatic.Mendelson.PropertiesATP\n\n------------------------------------------------------------------------------\n\n+-asocc : \u2200 m n o \u2192 m + n + o \u2248 m + (n + o)\n+-asocc m n o = S\u2089 A A0 is m\n where\n A : \u2115 \u2192 Set\n A i = i + n + o \u2248 i + (n + o)\n {-# ATP definition A #-}\n\n postulate A0 : A zero\n {-# ATP prove A0 +-leftCong #-}\n\n -- 25 November 2013: Vampire 0.6 proves the theorem using a time out\n -- of (300 sec).\n postulate is : \u2200 i \u2192 A i \u2192 A (succ i)\n {-# ATP prove is +-leftCong #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Unproven PA properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule PA.Axiomatic.Mendelson.Properties.UnprovedATP where\n\nopen import PA.Axiomatic.Mendelson.Base\nopen import PA.Axiomatic.Mendelson.PropertiesATP\n\n------------------------------------------------------------------------------\n\n+-asocc : \u2200 m n o \u2192 m + n + o \u2248 m + (n + o)\n+-asocc m n o = S\u2089 A A0 is m\n where\n A : \u2115 \u2192 Set\n A i = i + n + o \u2248 i + (n + o)\n {-# ATP definition A #-}\n\n postulate A0 : A zero\n {-# ATP prove A0 +-leftCong #-}\n\n -- 31 July 2013: The ATPs could not prove the theorem (240 sec).\n --\n -- After the addition of the inequality _\u2249_, no ATP proves the\n -- theorem. Before it, only Equinox 5.0alpha (2010-06-29) had proved\n -- the theorem.\n postulate is : \u2200 i \u2192 A i \u2192 A (succ i)\n {-# ATP prove is +-leftCong #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"61e9334f7220a5789eb6f91603f319d08db77ec7","subject":"Agda 2.4.2 regression? (see issue 1264)","message":"Agda 2.4.2 regression? (see issue 1264)\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/FOT\/FOTC\/Program\/ABP\/ABP-SL.agda","new_file":"notes\/FOT\/FOTC\/Program\/ABP\/ABP-SL.agda","new_contents":"------------------------------------------------------------------------------\n-- The ABP using Agda standard library\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Program.ABP.ABP-SL where\n\nopen import Coinduction\nopen import Data.Bool\nopen import Data.Product\nopen import Data.Stream\nopen import Relation.Nullary\n\n------------------------------------------------------------------------------\n\nBit : Set\nBit = Bool\n\n-- Data type used to model the fair unreliable transmission channel.\ndata Err (A : Set) : Set where\n ok : (x : A) \u2192 Err A\n error : Err A\n\n-- The mutual sender functions.\n--\n-- 25 June 2014. Requires the non-termination flag when using\n-- --without-K. See Agda issue 1214.\n{-# NO_TERMINATION_CHECK #-}\nsendA : {A : Set} \u2192 Bit \u2192 Stream A \u2192 Stream (Err Bit) \u2192 Stream (A \u00d7 Bool)\nawaitA : {A : Set} \u2192 Bit \u2192 Stream A \u2192 Stream (Err Bit) \u2192 Stream (A \u00d7 Bit)\n\nsendA b (i \u2237 is) ds = (i , b) \u2237 \u266f awaitA b (i \u2237 is) ds\n\nawaitA b (i \u2237 is) (ok b' \u2237 ds) with b \u225f b'\n... | yes p = sendA (not b) (\u266d is) (\u266d ds)\n... | no \u00acp = (i , b) \u2237 \u266f (awaitA b (i \u2237 is) (\u266d ds))\nawaitA b (i \u2237 is) (error \u2237 ds) = (i , b) \u2237 \u266f (awaitA b (i \u2237 is) (\u266d ds))\n\n-- The receiver functions.\n--\n-- 25 June 2014. Requires the non-termination flag when using\n-- --without-K. See Agda issue 1214.\n{-# NO_TERMINATION_CHECK #-}\nackA : {A : Set} \u2192 Bit \u2192 Stream (Err (A \u00d7 Bit)) \u2192 Stream Bit\nackA b (ok (_ , b') \u2237 bs) with b \u225f b'\n... | yes p = b \u2237 \u266f (ackA (not b) (\u266d bs))\n... | no \u00acp = not b \u2237 \u266f (ackA b (\u266d bs))\nackA b (error \u2237 bs) = not b \u2237 \u266f (ackA b (\u266d bs))\n\n{-# NO_TERMINATION_CHECK #-}\noutA : {A : Set} \u2192 Bit \u2192 Stream (Err (A \u00d7 Bit)) \u2192 Stream A\noutA b (ok (i , b') \u2237 bs) with b \u225f b'\n... | yes p = i \u2237 \u266f (outA (not b) (\u266d bs))\n... | no \u00acp = outA b (\u266d bs)\noutA b (error \u2237 bs) = outA b (\u266d bs)\n\n-- Model the fair unreliable tranmission channel.\n--\n-- 29 August 2014 Requires the non-termination flag when using\n-- --without-K after the release of Agda 2.4.2. See issue 1264.\n{-# NO_TERMINATION_CHECK #-}\ncorruptA : {A : Set} \u2192 Stream Bit \u2192 Stream A \u2192 Stream (Err A)\ncorruptA (true \u2237 os) (_ \u2237 xs) = error \u2237 \u266f (corruptA (\u266d os) (\u266d xs))\ncorruptA (false \u2237 os) (x \u2237 xs) = ok x \u2237 \u266f (corruptA (\u266d os) (\u266d xs))\n\n-- The ABP transfer function.\n{-# NO_TERMINATION_CHECK #-}\nabpTransA : {A : Set} \u2192 Bit \u2192 Stream Bit \u2192 Stream Bit \u2192 Stream A \u2192 Stream A\nabpTransA {A} b os\u2081 os\u2082 is = outA b bs\n where\n as : Stream (A \u00d7 Bit)\n bs : Stream (Err (A \u00d7 Bit))\n cs : Stream Bit\n ds : Stream (Err Bit)\n\n as = sendA b is ds\n bs = corruptA os\u2081 as\n cs = ackA b bs\n ds = corruptA os\u2082 cs\n","old_contents":"------------------------------------------------------------------------------\n-- The ABP using Agda standard library\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Program.ABP.ABP-SL where\n\nopen import Coinduction\nopen import Data.Bool\nopen import Data.Product\nopen import Data.Stream\nopen import Relation.Nullary\n\n------------------------------------------------------------------------------\n\nBit : Set\nBit = Bool\n\n-- Data type used to model the fair unreliable transmission channel.\ndata Err (A : Set) : Set where\n ok : (x : A) \u2192 Err A\n error : Err A\n\n-- The mutual sender functions.\n--\n-- 25 June 2014. Requires the non-termination flag when using\n-- --without-K. See Agda issue 1214.\n{-# NO_TERMINATION_CHECK #-}\nsendA : {A : Set} \u2192 Bit \u2192 Stream A \u2192 Stream (Err Bit) \u2192 Stream (A \u00d7 Bool)\nawaitA : {A : Set} \u2192 Bit \u2192 Stream A \u2192 Stream (Err Bit) \u2192 Stream (A \u00d7 Bit)\n\nsendA b (i \u2237 is) ds = (i , b) \u2237 \u266f awaitA b (i \u2237 is) ds\n\nawaitA b (i \u2237 is) (ok b' \u2237 ds) with b \u225f b'\n... | yes p = sendA (not b) (\u266d is) (\u266d ds)\n... | no \u00acp = (i , b) \u2237 \u266f (awaitA b (i \u2237 is) (\u266d ds))\nawaitA b (i \u2237 is) (error \u2237 ds) = (i , b) \u2237 \u266f (awaitA b (i \u2237 is) (\u266d ds))\n\n-- The receiver functions.\n--\n-- 25 June 2014. Requires the non-termination flag when using\n-- --without-K. See Agda issue 1214.\n{-# NO_TERMINATION_CHECK #-}\nackA : {A : Set} \u2192 Bit \u2192 Stream (Err (A \u00d7 Bit)) \u2192 Stream Bit\nackA b (ok (_ , b') \u2237 bs) with b \u225f b'\n... | yes p = b \u2237 \u266f (ackA (not b) (\u266d bs))\n... | no \u00acp = not b \u2237 \u266f (ackA b (\u266d bs))\nackA b (error \u2237 bs) = not b \u2237 \u266f (ackA b (\u266d bs))\n\n{-# NO_TERMINATION_CHECK #-}\noutA : {A : Set} \u2192 Bit \u2192 Stream (Err (A \u00d7 Bit)) \u2192 Stream A\noutA b (ok (i , b') \u2237 bs) with b \u225f b'\n... | yes p = i \u2237 \u266f (outA (not b) (\u266d bs))\n... | no \u00acp = outA b (\u266d bs)\noutA b (error \u2237 bs) = outA b (\u266d bs)\n\n-- Model the fair unreliable tranmission channel.\ncorruptA : {A : Set} \u2192 Stream Bit \u2192 Stream A \u2192 Stream (Err A)\ncorruptA (true \u2237 os) (_ \u2237 xs) = error \u2237 \u266f (corruptA (\u266d os) (\u266d xs))\ncorruptA (false \u2237 os) (x \u2237 xs) = ok x \u2237 \u266f (corruptA (\u266d os) (\u266d xs))\n\n-- The ABP transfer function.\n{-# NO_TERMINATION_CHECK #-}\nabpTransA : {A : Set} \u2192 Bit \u2192 Stream Bit \u2192 Stream Bit \u2192 Stream A \u2192 Stream A\nabpTransA {A} b os\u2081 os\u2082 is = outA b bs\n where\n as : Stream (A \u00d7 Bit)\n bs : Stream (Err (A \u00d7 Bit))\n cs : Stream Bit\n ds : Stream (Err Bit)\n\n as = sendA b is ds\n bs = corruptA os\u2081 as\n cs = ackA b bs\n ds = corruptA os\u2082 cs\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"faa65e48031abe5b7a8ce7be25f4151cfb5f80a8","subject":"Cosmetic changes.","message":"Cosmetic changes.\n\nIgnore-this: 2c3aec9f499291f0b940d407c92b0b57\n\ndarcs-hash:20110404171438-3bd4e-6e04071ad7513f0de4e7893d81c5d1aec08974a5.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/old\/LTC.agda","new_file":"Draft\/old\/LTC.agda","new_contents":"module LTC where\n\nmodule Core where\n\n{-\nAgda as a logical framework for LTC\n\nLTC Agda\n* Logical constants * Curry-Howard isomorphism\n* Equality * Identity type\n* Term language * Postulates\n* Inductive predicates * Inductive families\n-}\n\n-- Fixity declarations (precedence level and associativity)\n-- Agda default: infix 20\n\ninfixl 80 _`_\ninfixl 60 _\u2227_\ninfixl 50 _\u2228_\ninfix 40 if#_then_else_\ninfix 30 _==_\n\n-------------------------------------------------------------------------\n-- Equality: identity type\n-------------------------------------------------------------------------\n\n-- The identity type\ndata _==_ {A : Set} : A -> A -> Set where\n ==-refl : {x : A} -> x == x\n\n==-subst : {A : Set}(P : A -> Set){x y : A} -> x == y -> P x -> P y\n==-subst P ==-refl px = px\n\n==-sym : {A : Set} {x y : A} -> x == y -> y == x\n==-sym ==-refl = ==-refl\n\n==-trans : {A : Set} {x y z : A} -> x == y -> y == z -> x == z\n==-trans ==-refl y==z = y==z\n\n-------------------------------------------------------------------------\n-- Logical constants: Curry-Howard isomorphism\n-------------------------------------------------------------------------\n\n-- The false type\ndata \u22a5 : Set where\n\n\u22a5-elim : {A : Set} -> \u22a5 -> A\n\u22a5-elim ()\n\n\u00ac : Set -> Set\n\u00ac A = A -> \u22a5\n\n-- The disjunction type ('\\vee', not 'v')\ndata _\u2228_ (A B : Set) : Set where\n \u2228-il : A -> A \u2228 B\n \u2228-ir : B -> A \u2228 B\n\n\u2228-elim : {A B C : Set} -> (A -> C) -> (B -> C) -> A \u2228 B -> C\n\u2228-elim f g (\u2228-il a) = f a\n\u2228-elim f g (\u2228-ir b) = g b\n\n-- The conjunction type\ndata _\u2227_ (A B : Set) : Set where\n \u2227-i : A -> B -> A \u2227 B\n\n\u2227-fst : {A B : Set} -> A \u2227 B -> A\n\u2227-fst (\u2227-i a b) = a\n\n\u2227-snd : {A B : Set} -> A \u2227 B -> B\n\u2227-snd (\u2227-i a b) = b\n\n-- The existential quantifier type\ndata \u2203 (A : Set)(P : A -> Set) : Set where\n \u2203-i : (witness : A) -> P witness -> \u2203 A P\n\n\u2203-fst : {A : Set}{P : A -> Set} -> \u2203 A P -> A\n\u2203-fst (\u2203-i x px) = x\n\n\u2203-snd : {A : Set}{P : A -> Set} -> (x-px : \u2203 A P) -> P (\u2203-fst x-px)\n\u2203-snd (\u2203-i x px) = px\n\n-- The implication and the universal quantifier\n-- We use Agda (dependent) function type\n\n-------------------------------------------------------------------------\n-- The term language: postulates\n-------------------------------------------------------------------------\n\n{-\nD : a universal domain of terms\n\nPre-types (or weak types): Agda's simple type lambda calculus on 'D'\n\n T ::= D | T -> T\n t := x | \\x -> t | t t | c (constants)\n\n-}\n\n-------------------------\n-- The universal domain\n\npostulate D : Set\n\n--------------------\n-- Constants terms\n\npostulate\n\n -- LTC booleans\n true# : D\n false# : D\n if#_then_else_ : D -> D -> D -> D\n\n -- LTC natural numbers\n -- We will refer to these partial numbers as 'N#'\n zero# : D\n suc# : D -> D\n rec# : D -> D -> (D -> D -> D) -> D\n\n -- LTC abstraction and application\n \u03bb : (D -> D) -> D\n -- Left associative aplication operator\n _`_ : D -> D -> D\n\n----------------------\n-- Conversion rules\n\npostulate\n -- Conversion rules for booleans\n CB1 : (a : D){b : D} -> if# true# then a else b == a\n CB2 : {a : D}(b : D) -> if# false# then a else b == b\n\n -- Conversion rules for natural numbers\n CN1 : (a : D){f : D -> D -> D} -> rec# zero# a f == a\n CN2 : (a n : D)(f : D -> D -> D) ->\n rec# (suc# n) a f == f n (rec# n a f)\n\n -- Conversion rule for the abstraction and the application\n beta : (f : D -> D)(a : D) -> (\u03bb f) ` a == f a\n\n--------------------------------------------------------------\n-- Inductive predicate for natural numbers : Inductive family\n--------------------------------------------------------------\n-- The inductive predicate 'N' represents the type of the natural\n-- numbers. They are a subset of 'D'.\n\n-- The natural numbers type\ndata N : D -> Set where\n N-zero : N zero#\n N-suc : {n : D} -> N n -> N (suc# n)\n\n-- Induction principle on 'N' (elimination rule)\nN-ind : (P : D -> Set) ->\n P zero# ->\n ({n : D} -> N n -> P n -> P (suc# n)) ->\n {n : D} -> N n -> P n\nN-ind P p0 h N-zero = p0\nN-ind P p0 h (N-suc Nn) = h Nn (N-ind P p0 h Nn)\n\n\n\nmodule Recursive-Functions-LTC where\n\n -- We define some primitive recursive functions via LTC terms\n\n open Core\n\n import Equality-Reasoning\n open module ER-Recursive-Functions-LTC =\n Equality-Reasoning (_==_ {D}) ==-refl ==-trans\n\n ------------------------------------------------------------------------\n -- Remark: We are using Agda's definitional equality '=' as\n -- LTC's definitional equality\n\n -- Fixity declarations (precedence level and associativity)\n -- Agda default: infix 20\n\n infixl 70 _*_\n infixl 60 _+_ _-_\n\n _+_ : D -> D -> D\n m + n = rec# n m (\\x y -> suc# y)\n\n -- recursion on the second argument\n _*_ : D -> D -> D\n m * n = rec# n zero# (\\x y -> y + n)\n\n pred : D -> D\n pred n = rec# n zero# (\\x y -> x)\n\n -- m - 0 = m\n -- m - (succ n) = pred (m - n)\n _-_ : D -> D -> D\n m - n = rec# n m (\\x y -> pred y)\n\n or : D -> D -> D\n or a b = if# a then true# else b\n\n isZero : D -> D\n isZero n = rec# n true# (\\x y -> false#)\n\n -- The function 'equi n' return a function which establish if an\n -- argument 'm' is equal to 'n'\n -- equi zero = \\m -> isZero m\n -- equi (succ n) = \\m -> case m of\n -- zero -> false\n -- (succ m') -> (equi n) m\n\n equi : D -> D\n equi n = rec# n (\u03bb (\\m -> isZero m))\n (\\x y -> \u03bb (\\m -> rec# m false# (\\m' z -> y ` m')))\n\n -- equality on N#\n eq : D -> D -> D\n eq m n = (equi n) ` m\n\n -- inequality on N#\n -- lt m 0 = false#\n -- lt m (succ n) = (lt m n) \u2228 (m = n)\n lt : D -> D -> D\n lt m n = rec# n false# (\\x y -> or y (eq m x))\n\n -- inequality on N#\n gt : D -> D -> D\n gt m n = lt n m\n","old_contents":"module LTC where\n\n\nmodule Core where\n\n{-\nAgda as a logical framework for LTC\n\nLTC Agda\n* Logical constants * Curry-Howard isomorphism\n* Equality * Identity type\n* Term language * Postulates\n* Inductive predicates * Inductive families\n-}\n\n\n-- Fixity declarations (precedence level and associativity)\n-- Agda default: infix 20\n\ninfixl 80 _`_\ninfixl 60 _\u2227_\ninfixl 50 _\u2228_\ninfix 40 if#_then_else_\ninfix 30 _==_\n\n-------------------------------------------------------------------------\n-- Equality: identity type\n-------------------------------------------------------------------------\n\n-- The identity type\ndata _==_ {A : Set} : A -> A -> Set where\n ==-refl : {x : A} -> x == x\n\n==-subst : {A : Set}(P : A -> Set){x y : A} -> x == y -> P x -> P y\n==-subst P ==-refl px = px\n\n==-sym : {A : Set} {x y : A} -> x == y -> y == x\n==-sym ==-refl = ==-refl\n\n==-trans : {A : Set} {x y z : A} -> x == y -> y == z -> x == z\n==-trans ==-refl y==z = y==z\n\n-------------------------------------------------------------------------\n-- Logical constants: Curry-Howard isomorphism\n-------------------------------------------------------------------------\n\n-- The false type\ndata \u22a5 : Set where\n\n\u22a5-elim : {A : Set} -> \u22a5 -> A\n\u22a5-elim ()\n\n\u00ac : Set -> Set\n\u00ac A = A -> \u22a5\n\n-- The disjunction type ('\\vee', not 'v')\ndata _\u2228_ (A B : Set) : Set where\n \u2228-il : A -> A \u2228 B\n \u2228-ir : B -> A \u2228 B\n\n\u2228-elim : {A B C : Set} -> (A -> C) -> (B -> C) -> A \u2228 B -> C\n\u2228-elim f g (\u2228-il a) = f a\n\u2228-elim f g (\u2228-ir b) = g b\n\n-- The conjunction type\ndata _\u2227_ (A B : Set) : Set where\n \u2227-i : A -> B -> A \u2227 B\n\n\u2227-fst : {A B : Set} -> A \u2227 B -> A\n\u2227-fst (\u2227-i a b) = a\n\n\u2227-snd : {A B : Set} -> A \u2227 B -> B\n\u2227-snd (\u2227-i a b) = b\n\n-- The existential quantifier type\ndata \u2203 (A : Set)(P : A -> Set) : Set where\n \u2203-i : (witness : A) -> P witness -> \u2203 A P\n\n\u2203-fst : {A : Set}{P : A -> Set} -> \u2203 A P -> A\n\u2203-fst (\u2203-i x px) = x\n\n\u2203-snd : {A : Set}{P : A -> Set} -> (x-px : \u2203 A P) -> P (\u2203-fst x-px)\n\u2203-snd (\u2203-i x px) = px\n\n-- The implication and the universal quantifier\n-- We use Agda (dependent) function type\n\n-------------------------------------------------------------------------\n-- The term language: postulates\n-------------------------------------------------------------------------\n\n{-\nD : a universal domain of terms\n\nPre-types (or weak types): Agda's simple type lambda calculus on 'D'\n\n T ::= D | T -> T\n t := x | \\x -> t | t t | c (constants)\n\n-}\n\n-------------------------\n-- The universal domain\n\npostulate D : Set\n\n--------------------\n-- Constants terms\n\npostulate\n\n -- LTC booleans\n true# : D\n false# : D\n if#_then_else_ : D -> D -> D -> D\n\n -- LTC natural numbers\n -- We will refer to these partial numbers as 'N#'\n zero# : D\n suc# : D -> D\n rec# : D -> D -> (D -> D -> D) -> D\n\n -- LTC abstraction and application\n \u03bb : (D -> D) -> D\n -- Left associative aplication operator\n _`_ : D -> D -> D\n\n----------------------\n-- Conversion rules\n\npostulate\n -- Conversion rules for booleans\n CB1 : (a : D){b : D} -> if# true# then a else b == a\n CB2 : {a : D}(b : D) -> if# false# then a else b == b\n\n -- Conversion rules for natural numbers\n CN1 : (a : D){f : D -> D -> D} -> rec# zero# a f == a\n CN2 : (a n : D)(f : D -> D -> D) ->\n rec# (suc# n) a f == f n (rec# n a f)\n\n -- Conversion rule for the abstraction and the application\n beta : (f : D -> D)(a : D) -> (\u03bb f) ` a == f a\n\n\n--------------------------------------------------------------\n-- Inductive predicate for natural numbers : Inductive family\n--------------------------------------------------------------\n-- The inductive predicate 'N' represents the type of the natural\n-- numbers. They are a subset of 'D'.\n\n-- The natural numbers type\ndata N : D -> Set where\n N-zero : N zero#\n N-suc : {n : D} -> N n -> N (suc# n)\n\n-- Induction principle on 'N' (elimination rule)\nN-ind : (P : D -> Set) ->\n P zero# ->\n ({n : D} -> N n -> P n -> P (suc# n)) ->\n {n : D} -> N n -> P n\nN-ind P p0 h N-zero = p0\nN-ind P p0 h (N-suc Nn) = h Nn (N-ind P p0 h Nn)\n\n\n\nmodule Recursive-Functions-LTC where\n\n -- We define some primitive recursive functions via LTC terms\n\n open Core\n\n import Equality-Reasoning\n open module ER-Recursive-Functions-LTC =\n Equality-Reasoning (_==_ {D}) ==-refl ==-trans\n\n\n ------------------------------------------------------------------------\n -- Remark: We are using Agda's definitional equality '=' as\n -- LTC's definitional equality\n\n -- Fixity declarations (precedence level and associativity)\n -- Agda default: infix 20\n\n infixl 70 _*_\n infixl 60 _+_ _-_\n\n\n _+_ : D -> D -> D\n m + n = rec# n m (\\x y -> suc# y)\n\n -- recursion on the second argument\n _*_ : D -> D -> D\n m * n = rec# n zero# (\\x y -> y + n)\n\n pred : D -> D\n pred n = rec# n zero# (\\x y -> x)\n\n\n -- m - 0 = m\n -- m - (succ n) = pred (m - n)\n _-_ : D -> D -> D\n m - n = rec# n m (\\x y -> pred y)\n\n or : D -> D -> D\n or a b = if# a then true# else b\n\n isZero : D -> D\n isZero n = rec# n true# (\\x y -> false#)\n\n\n -- The function 'equi n' return a function which establish if an\n -- argument 'm' is equal to 'n'\n -- equi zero = \\m -> isZero m\n -- equi (succ n) = \\m -> case m of\n -- zero -> false\n -- (succ m') -> (equi n) m\n\n equi : D -> D\n equi n = rec# n (\u03bb (\\m -> isZero m))\n (\\x y -> \u03bb (\\m -> rec# m false# (\\m' z -> y ` m')))\n\n\n -- equality on N#\n eq : D -> D -> D\n eq m n = (equi n) ` m\n\n -- inequality on N#\n -- lt m 0 = false#\n -- lt m (succ n) = (lt m n) \u2228 (m = n)\n lt : D -> D -> D\n lt m n = rec# n false# (\\x y -> or y (eq m x))\n\n -- inequality on N#\n gt : D -> D -> D\n gt m n = lt n m\n\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"e48ae532437abaf69855a88c32dbfa753f8dcb92","subject":"ZK.JSChecker: better error message while checking the type of the statement","message":"ZK.JSChecker: better error message while checking the type of the statement\n","repos":"crypto-agda\/crypto-agda","old_file":"ZK\/JSChecker.agda","new_file":"ZK\/JSChecker.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule ZK.JSChecker where\n\nopen import Function using (id; _\u2218\u2032_; case_of_)\nopen import Data.Bool.Base using (Bool; true; false; _\u2227_)\nopen import Data.List.Base using (List; []; _\u2237_; and; foldr)\nopen import Data.String.Base using (String)\n\nopen import FFI.JS\nopen import FFI.JS.Check\n-- open import FFI.JS.Proc using (URI; JSProc; showURI; server)\n-- open import Control.Process.Type\nimport FFI.JS.Console as Console\nimport FFI.JS.Process as Process\nimport FFI.JS.FS as FS\n\nimport FFI.JS.BigI as BigI\nopen BigI using (BigI; bigI)\n\nimport Crypto.JS.BigI.ZqZp as ZqZp\n\n-- TODO dynamise me\nprimality-test-probability-bound : Number\nprimality-test-probability-bound = readNumber \"10\"\n\n-- TODO: check if this is large enough\nmin-bits-q : Number\nmin-bits-q = 256N\n\nmin-bits-p : Number\nmin-bits-p = 2048N\n\n-- TODO check with undefined\nbigdec : JSValue \u2192 BigI\nbigdec v = bigI (castString v) \"10\"\n\n-- TODO bug (undefined)!\nrecord ZK-chaum-pedersen-pok-elgamal-rnd {--(\u2124q \u2124p\u2605 : Set)--} : Set where\n field\n m c s : BigI {--\u2124q--}\n g p q y \u03b1 \u03b2 A B : BigI --\u2124p\u2605\n\nzk-check-chaum-pedersen-pok-elgamal-rnd! : ZK-chaum-pedersen-pok-elgamal-rnd {-BigI BigI-} \u2192 JS!\nzk-check-chaum-pedersen-pok-elgamal-rnd! pf\n = trace \"g=\" g \u03bb _ \u2192\n trace \"p=\" I.p \u03bb _ \u2192\n trace \"q=\" I.q \u03bb _ \u2192\n trace \"y=\" y \u03bb _ \u2192\n trace \"\u03b1=\" \u03b1 \u03bb _ \u2192\n trace \"\u03b2=\" \u03b2 \u03bb _ \u2192\n trace \"m=\" m \u03bb _ \u2192\n trace \"M=\" M \u03bb _ \u2192\n trace \"A=\" A \u03bb _ \u2192\n trace \"B=\" B \u03bb _ \u2192\n trace \"c=\" c \u03bb _ \u2192\n trace \"s=\" s \u03bb _ \u2192\n checks!\n >> check! \"g^s==A\u00b7\u03b1^c\" ((g ^ s) == (A \u00b7 (\u03b1 ^ c))) (\u03bb _ \u2192 \"\")\n >> check! \"y^s==B\u00b7(\u03b2\/M)^c\" ((y ^ s) == (B \u00b7 ((\u03b2 \u00b7\/ M) ^ c))) (\u03bb _ \u2192 \"\")\n module ZK-check-chaum-pedersen-pok-elgamal-rnd where\n module I = ZK-chaum-pedersen-pok-elgamal-rnd pf\n params = record\n { primality-test-probability-bound = primality-test-probability-bound\n ; min-bits-q = min-bits-q\n ; min-bits-p = min-bits-p\n ; qI = I.q\n ; pI = I.p\n ; gI = I.g\n }\n open module [\u2124q]\u2124p\u2605 = ZqZp params\n A = BigI\u25b9\u2124p\u2605 I.A\n B = BigI\u25b9\u2124p\u2605 I.B\n \u03b1 = BigI\u25b9\u2124p\u2605 I.\u03b1\n \u03b2 = BigI\u25b9\u2124p\u2605 I.\u03b2\n y = BigI\u25b9\u2124p\u2605 I.y\n s = BigI\u25b9\u2124q I.s\n c = BigI\u25b9\u2124q I.c\n m = BigI\u25b9\u2124q I.m\n M = g ^ m\n\nzk-check! : JSValue \u2192 JS!\nzk-check! arg =\n check! \"type of statement\" (typ === fromString cpt)\n (\u03bb _ \u2192 \"Expected type of statement: \" ++ cpt ++ \" not \" ++ toString typ)\n >> zk-check-chaum-pedersen-pok-elgamal-rnd! pok\n module Zk-check where\n cpt = \"chaum-pedersen-pok-elgamal-rnd\"\n stm = arg \u00b7\u00ab \"statement\" \u00bb\n typ = stm \u00b7\u00ab \"type\" \u00bb\n dat = stm \u00b7\u00ab \"data\" \u00bb\n g = bigdec (dat \u00b7\u00ab \"g\" \u00bb)\n p = bigdec (dat \u00b7\u00ab \"p\" \u00bb)\n q = bigdec (dat \u00b7\u00ab \"q\" \u00bb)\n y = bigdec (dat \u00b7\u00ab \"y\" \u00bb)\n m = bigdec (dat \u00b7\u00ab \"plain\" \u00bb)\n enc = dat \u00b7\u00ab \"enc\" \u00bb\n \u03b1 = bigdec (enc \u00b7\u00ab \"alpha\" \u00bb)\n \u03b2 = bigdec (enc \u00b7\u00ab \"beta\" \u00bb)\n prf = arg \u00b7\u00ab \"proof\" \u00bb\n com = prf \u00b7\u00ab \"commitment\" \u00bb\n A = bigdec (com \u00b7\u00ab \"A\" \u00bb)\n B = bigdec (com \u00b7\u00ab \"B\" \u00bb)\n c = bigdec (prf \u00b7\u00ab \"challenge\" \u00bb)\n s = bigdec (prf \u00b7\u00ab \"response\" \u00bb)\n pok = record { g = g; p = p; q = q; y = y; \u03b1 = \u03b1; \u03b2 = \u03b2; A = A; B = B; c = c; s = s; m = m }\n\n{-\nsrv : URI \u2192 JSProc\nsrv d =\n recv d \u03bb q \u2192\n send d (fromBool (zk-check q))\n end\n-}\n\n-- Working around Agda.Primitive.lsuc being undefined\n-- case_of_ : {A : Set} {B : Set} \u2192 A \u2192 (A \u2192 B) \u2192 B\n-- case x of f = f x\n\nmain : JS!\nmain =\n Process.argv !\u2081 \u03bb args \u2192\n case JSArray\u25b9ListString args of \u03bb {\n (_node \u2237 _run \u2237 _test \u2237 args') \u2192\n case args' of \u03bb {\n [] \u2192\n Console.log \"usage: No arguments\"\n {- server \"127.0.0.1\" \"1337\" srv !\u2081 \u03bb uri \u2192\n Console.log (showURI uri)\n -}\n ; (arg \u2237 args'') \u2192\n case args'' of \u03bb {\n [] \u2192\n Console.log (\"Reading input file: \" ++ arg) >>\n FS.readFile arg nullJS !\u2082 \u03bb err dat \u2192\n check! \"reading input file\" (is-null err)\n (\u03bb _ \u2192 \"readFile error: \" ++ toString err) >>\n zk-check! (JSON-parse (toString dat))\n ; _ \u2192\n Console.log \"usage: Too many arguments\"\n }\n }\n ; _ \u2192\n Console.log \"usage\"\n }\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule ZK.JSChecker where\n\nopen import Function using (id; _\u2218\u2032_; case_of_)\nopen import Data.Bool.Base using (Bool; true; false; _\u2227_)\nopen import Data.List.Base using (List; []; _\u2237_; and; foldr)\nopen import Data.String.Base using (String)\n\nopen import FFI.JS\nopen import FFI.JS.Check\n-- open import FFI.JS.Proc using (URI; JSProc; showURI; server)\n-- open import Control.Process.Type\nimport FFI.JS.Console as Console\nimport FFI.JS.Process as Process\nimport FFI.JS.FS as FS\n\nimport FFI.JS.BigI as BigI\nopen BigI using (BigI; bigI)\n\nimport Crypto.JS.BigI.ZqZp as ZqZp\n\n-- TODO dynamise me\nprimality-test-probability-bound : Number\nprimality-test-probability-bound = readNumber \"10\"\n\n-- TODO: check if this is large enough\nmin-bits-q : Number\nmin-bits-q = 256N\n\nmin-bits-p : Number\nmin-bits-p = 2048N\n\n-- TODO check with undefined\nbigdec : JSValue \u2192 BigI\nbigdec v = bigI (castString v) \"10\"\n\n-- TODO bug (undefined)!\nrecord ZK-chaum-pedersen-pok-elgamal-rnd {--(\u2124q \u2124p\u2605 : Set)--} : Set where\n field\n m c s : BigI {--\u2124q--}\n g p q y \u03b1 \u03b2 A B : BigI --\u2124p\u2605\n\nzk-check-chaum-pedersen-pok-elgamal-rnd! : ZK-chaum-pedersen-pok-elgamal-rnd {-BigI BigI-} \u2192 JS!\nzk-check-chaum-pedersen-pok-elgamal-rnd! pf\n = trace \"g=\" g \u03bb _ \u2192\n trace \"p=\" I.p \u03bb _ \u2192\n trace \"q=\" I.q \u03bb _ \u2192\n trace \"y=\" y \u03bb _ \u2192\n trace \"\u03b1=\" \u03b1 \u03bb _ \u2192\n trace \"\u03b2=\" \u03b2 \u03bb _ \u2192\n trace \"m=\" m \u03bb _ \u2192\n trace \"M=\" M \u03bb _ \u2192\n trace \"A=\" A \u03bb _ \u2192\n trace \"B=\" B \u03bb _ \u2192\n trace \"c=\" c \u03bb _ \u2192\n trace \"s=\" s \u03bb _ \u2192\n checks!\n >> check! \"g^s==A\u00b7\u03b1^c\" ((g ^ s) == (A \u00b7 (\u03b1 ^ c))) (\u03bb _ \u2192 \"\")\n >> check! \"y^s==B\u00b7(\u03b2\/M)^c\" ((y ^ s) == (B \u00b7 ((\u03b2 \u00b7\/ M) ^ c))) (\u03bb _ \u2192 \"\")\n module ZK-check-chaum-pedersen-pok-elgamal-rnd where\n module I = ZK-chaum-pedersen-pok-elgamal-rnd pf\n params = record\n { primality-test-probability-bound = primality-test-probability-bound\n ; min-bits-q = min-bits-q\n ; min-bits-p = min-bits-p\n ; qI = I.q\n ; pI = I.p\n ; gI = I.g\n }\n open module [\u2124q]\u2124p\u2605 = ZqZp params\n A = BigI\u25b9\u2124p\u2605 I.A\n B = BigI\u25b9\u2124p\u2605 I.B\n \u03b1 = BigI\u25b9\u2124p\u2605 I.\u03b1\n \u03b2 = BigI\u25b9\u2124p\u2605 I.\u03b2\n y = BigI\u25b9\u2124p\u2605 I.y\n s = BigI\u25b9\u2124q I.s\n c = BigI\u25b9\u2124q I.c\n m = BigI\u25b9\u2124q I.m\n M = g ^ m\n\nzk-check! : JSValue \u2192 JS!\nzk-check! arg =\n check! \"type of statement\" (typ === fromString \"chaum-pedersen-pok-elgamal-rnd\")\n (\u03bb _ \u2192 \"\")\n >> zk-check-chaum-pedersen-pok-elgamal-rnd! pok\n module Zk-check where\n stm = arg \u00b7\u00ab \"statement\" \u00bb\n typ = stm \u00b7\u00ab \"type\" \u00bb\n dat = stm \u00b7\u00ab \"data\" \u00bb\n g = bigdec (dat \u00b7\u00ab \"g\" \u00bb)\n p = bigdec (dat \u00b7\u00ab \"p\" \u00bb)\n q = bigdec (dat \u00b7\u00ab \"q\" \u00bb)\n y = bigdec (dat \u00b7\u00ab \"y\" \u00bb)\n m = bigdec (dat \u00b7\u00ab \"plain\" \u00bb)\n enc = dat \u00b7\u00ab \"enc\" \u00bb\n \u03b1 = bigdec (enc \u00b7\u00ab \"alpha\" \u00bb)\n \u03b2 = bigdec (enc \u00b7\u00ab \"beta\" \u00bb)\n prf = arg \u00b7\u00ab \"proof\" \u00bb\n com = prf \u00b7\u00ab \"commitment\" \u00bb\n A = bigdec (com \u00b7\u00ab \"A\" \u00bb)\n B = bigdec (com \u00b7\u00ab \"B\" \u00bb)\n c = bigdec (prf \u00b7\u00ab \"challenge\" \u00bb)\n s = bigdec (prf \u00b7\u00ab \"response\" \u00bb)\n pok = record { g = g; p = p; q = q; y = y; \u03b1 = \u03b1; \u03b2 = \u03b2; A = A; B = B; c = c; s = s; m = m }\n\n{-\nsrv : URI \u2192 JSProc\nsrv d =\n recv d \u03bb q \u2192\n send d (fromBool (zk-check q))\n end\n-}\n\n-- Working around Agda.Primitive.lsuc being undefined\n-- case_of_ : {A : Set} {B : Set} \u2192 A \u2192 (A \u2192 B) \u2192 B\n-- case x of f = f x\n\nmain : JS!\nmain =\n Process.argv !\u2081 \u03bb args \u2192\n case JSArray\u25b9ListString args of \u03bb {\n (_node \u2237 _run \u2237 _test \u2237 args') \u2192\n case args' of \u03bb {\n [] \u2192\n Console.log \"usage: No arguments\"\n {- server \"127.0.0.1\" \"1337\" srv !\u2081 \u03bb uri \u2192\n Console.log (showURI uri)\n -}\n ; (arg \u2237 args'') \u2192\n case args'' of \u03bb {\n [] \u2192\n Console.log (\"Reading input file: \" ++ arg) >>\n FS.readFile arg nullJS !\u2082 \u03bb err dat \u2192\n check! \"reading input file\" (is-null err)\n (\u03bb _ \u2192 \"readFile error: \" ++ toString err) >>\n zk-check! (JSON-parse (toString dat))\n ; _ \u2192\n Console.log \"usage: Too many arguments\"\n }\n }\n ; _ \u2192\n Console.log \"usage\"\n }\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"080a08a480530a0bfbe7ee42fb486407caea4a5e","subject":"Remove extra imports","message":"Remove extra imports\n\nOld-commit-hash: 3a9775f6afd97585f6b425390b5431b43807b03e\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Equivalence.agda","new_file":"Parametric\/Change\/Equivalence.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Parametric.Change.Equivalence where\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\n-- Extension Point: None (currently). Do we need to allow plugins to customize\n-- this concept?\nStructure : Set\nStructure = Unit\n\nmodule Structure (unused : Structure) where\n\n module _ {\u2113} {A} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely. That should be true for functions\n -- using changes parametrically, for derivatives and function changes, and\n -- for functions using only the interface to changes (including the fact\n -- that function changes are functions). Stating the general result, though,\n -- seems hard, we should rather have lemmas proving that certain classes of\n -- functions respect this equivalence.\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nimport Parametric.Syntax.Type as Type\nimport Parametric.Denotation.Value as Value\n\nmodule Parametric.Change.Equivalence\n {Base : Type.Structure}\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\n-- Extension Point: None (currently). Do we need to allow plugins to customize\n-- this concept?\nStructure : Set\nStructure = Unit\n\nmodule Structure (unused : Structure) where\n\n module _ {\u2113} {A} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely. That should be true for functions\n -- using changes parametrically, for derivatives and function changes, and\n -- for functions using only the interface to changes (including the fact\n -- that function changes are functions). Stating the general result, though,\n -- seems hard, we should rather have lemmas proving that certain classes of\n -- functions respect this equivalence.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3409935502fb668aa913b86adee0b90b81efb211","subject":"Use standard syntax","message":"Use standard syntax\n","repos":"louisswarren\/hieretikz","old_file":"arrow.agda","new_file":"arrow.agda","new_contents":"data Bool : Set where\n true : Bool\n false : Bool\n\n\n_\u2228_ : Bool \u2192 Bool \u2192 Bool\ntrue \u2228 _ = true\nfalse \u2228 b = b\n\n\n_\u2227_ : Bool \u2192 Bool \u2192 Bool\nfalse \u2227 _ = false\ntrue \u2227 b = b\n\n\n----------------------------------------\n\n\n\ndata \u2115 : Set where\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n{-# BUILTIN NATURAL \u2115 #-}\n\n\n_==_ : \u2115 \u2192 \u2115 \u2192 Bool\nzero == zero = true\nsuc n == suc m = n == m\n_ == _ = false\n\n\n\n----------------------------------------\n\n\n\ndata List (A : Set) : Set where\n [] : List A\n _\u2237_ : A \u2192 List A \u2192 List A\ninfixr 5 _\u2237_\n\n\n[_] : {A : Set} \u2192 A \u2192 List A\n[ x ] = x \u2237 []\n\n\nany : {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 Bool\nany _ [] = false\nany f (x \u2237 xs) = (f x) \u2228 (any f xs)\n\n\n_\u2208_ : \u2115 \u2192 List \u2115 \u2192 Bool\nx \u2208 [] = false\nx \u2208 (y \u2237 ys) with x == y\n... | true = true\n... | false = x \u2208 ys\n\n\n_\u220b_ : List \u2115 \u2192 \u2115 \u2192 Bool\nxs \u220b y = y \u2208 xs\n\n\n\n----------------------------------------\n\n\n\ndata Arrow : Set where\n \u21d2_ : \u2115 \u2192 Arrow\n _\u21d2_ : \u2115 \u2192 Arrow \u2192 Arrow\n\n\n_\u2261\u2261_ : Arrow \u2192 Arrow \u2192 Bool\n(\u21d2 q) \u2261\u2261 (\u21d2 s) = q == s\n(p \u21d2 q) \u2261\u2261 (r \u21d2 s) = (p == r) \u2227 (q \u2261\u2261 s)\n_ \u2261\u2261 _ = false\n\n\n_\u2208\u2208_ : Arrow \u2192 List Arrow \u2192 Bool\nx \u2208\u2208 [] = false\nx \u2208\u2208 (y \u2237 ys) with x \u2261\u2261 y\n... | true = true\n... | false = x \u2208\u2208 ys\n\n\nclosure : List Arrow \u2192 List \u2115 \u2192 List \u2115\nclosure [] found = found\nclosure ((\u21d2 n) \u2237 rest) found = n \u2237 (closure rest (n \u2237 found))\nclosure ((n \u21d2 q) \u2237 rest) found with (n \u2208 found) \u2228 (n \u2208 (closure rest found))\n... | true = closure (q \u2237 rest) found\n... | false = closure rest found\n\n\n_,_\u22a2_ : List Arrow \u2192 List \u2115 \u2192 \u2115 \u2192 Bool\ncs , ps \u22a2 q = q \u2208 (closure cs ps)\n\n\n_\u22a2_ : List Arrow \u2192 Arrow \u2192 Bool\ncs \u22a2 (\u21d2 q) = q \u2208 (closure cs [])\ncs \u22a2 (p \u21d2 q) = ((\u21d2 p) \u2237 cs) \u22a2 q\n\n\n\n----------------------------------------\n\n\n\ndata Separation : Set where\n model : List \u2115 \u2192 List \u2115 \u2192 Separation\n\n\nmodelsupports : Separation \u2192 List Arrow \u2192 \u2115 \u2192 Bool\nmodelsupports (model holds _) cs n = cs , holds \u22a2 n\n\n\nmodeldenies : Separation \u2192 List Arrow \u2192 \u2115 \u2192 Bool\nmodeldenies (model _ fails) cs n = any (_\u220b_ (closure cs ([ n ]))) fails\n\n\n_\u27ea!_\u27eb_ : List Arrow \u2192 Separation \u2192 Arrow \u2192 Bool\ncs \u27ea! m \u27eb (\u21d2 q) = modeldenies m cs q\ncs \u27ea! m \u27eb (p \u21d2 q) = (modelsupports m cs p) \u2227 (cs \u27ea! m \u27eb q)\n\n\n_\u27ea_\u27eb_ : List Arrow \u2192 List Separation \u2192 Arrow \u2192 Bool\ncs \u27ea [] \u27eb arr = false\ncs \u27ea m \u2237 ms \u27eb arr = (cs \u27ea! m \u27eb arr) \u2228 (cs \u27ea ms \u27eb arr)\n\n\n\n----------------------------------------\n\n\n\ndata Relation : Set where\n Proved : Relation\n Derivable : Relation\n Separated : Relation\n Unknown : Relation\n\n\nconsider : List Arrow \u2192 List Separation \u2192 Arrow \u2192 Relation\nconsider cs ms arr with (arr \u2208\u2208 cs)\n... | true = Proved\n... | false with (cs \u22a2 arr)\n... | true = Derivable\n... | false with (cs \u27ea ms \u27eb arr)\n... | true = Separated\n... | false = Unknown\n\n\nproofs : List Arrow\nproofs =\n (3 \u21d2 (\u21d2 4)) \u2237\n-- (5 \u21d2 (\u21d2 4)) \u2237\n (6 \u21d2 (\u21d2 4)) \u2237\n (3 \u21d2 (\u21d2 3)) \u2237\n (3 \u21d2 (\u21d2 3)) \u2237\n (9 \u21d2 (\u21d2 3)) \u2237\n (9 \u21d2 (\u21d2 3)) \u2237\n (5 \u21d2 (\u21d2 7)) \u2237\n (9 \u21d2 (\u21d2 7)) \u2237\n (6 \u21d2 (\u21d2 8)) \u2237\n (10 \u21d2 (\u21d2 8)) \u2237\n (10 \u21d2 (\u21d2 7)) \u2237\n (5 \u21d2 (\u21d2 10)) \u2237\n (10 \u21d2 (\u21d2 4)) \u2237\n (5 \u21d2 (\u21d2 11)) \u2237\n (6 \u21d2 (\u21d2 11)) \u2237\n (11 \u21d2 (\u21d2 4)) \u2237\n (10 \u21d2 (\u21d2 7)) \u2237\n (8 \u21d2 (\u21d2 4)) \u2237\n (9 \u21d2 (\u21d2 7)) \u2237\n (9 \u21d2 (\u21d2 8)) \u2237\n (3 \u21d2 (\u21d2 8)) \u2237\n (5 \u21d2 (3 \u21d2 (\u21d2 9))) \u2237\n (6 \u21d2 (7 \u21d2 (\u21d2 10))) \u2237\n (6 \u21d2 (3 \u21d2 (\u21d2 3))) \u2237\n (7 \u21d2 (\u21d2 7)) \u2237\n (7 \u21d2 (\u21d2 7)) \u2237\n (7 \u21d2 (8 \u21d2 (\u21d2 10))) \u2237\n (3 \u21d2 (10 \u21d2 (\u21d2 9))) \u2237\n (5 \u21d2 (\u21d2 1)) \u2237\n (3 \u21d2 (1 \u21d2 (\u21d2 9))) \u2237\n (1 \u21d2 (\u21d2 2)) \u2237\n (10 \u21d2 (\u21d2 2)) \u2237 []\n\ncms : List Separation\ncms =\n (model (12 \u2237 6 \u2237 11 \u2237 4 \u2237 1 \u2237 []) (5 \u2237 3 \u2237 7 \u2237 7 \u2237 [])) \u2237\n (model (6 \u2237 3 \u2237 11 \u2237 4 \u2237 7 \u2237 8 \u2237 3 \u2237 9 \u2237 10 \u2237 1 \u2237 []) (5 \u2237 [])) \u2237\n (model (12 \u2237 5 \u2237 11 \u2237 4 \u2237 1 \u2237 []) (6 \u2237 3 \u2237 [])) \u2237\n (model (5 \u2237 3 \u2237 11 \u2237 4 \u2237 7 \u2237 8 \u2237 3 \u2237 9 \u2237 10 \u2237 1 \u2237 []) (6 \u2237 [])) \u2237\n (model (12 \u2237 4 \u2237 11 \u2237 []) (5 \u2237 6 \u2237 3 \u2237 8 \u2237 9 \u2237 1 \u2237 [])) \u2237\n (model (12 \u2237 5 \u2237 6 \u2237 4 \u2237 11 \u2237 1 \u2237 []) (3 \u2237 [])) \u2237\n (model (12 \u2237 4 \u2237 11 \u2237 7 \u2237 []) (9 \u2237 5 \u2237 6 \u2237 10 \u2237 8 \u2237 1 \u2237 [])) \u2237\n (model (10 \u2237 9 \u2237 []) (1 \u2237 [])) \u2237\n (model (3 \u2237 4 \u2237 11 \u2237 []) (9 \u2237 5 \u2237 6 \u2237 10 \u2237 7 \u2237 1 \u2237 [])) \u2237\n (model (12 \u2237 7 \u2237 1 \u2237 []) (4 \u2237 11 \u2237 8 \u2237 [])) \u2237\n (model (9 \u2237 3 \u2237 10 \u2237 8 \u2237 1 \u2237 []) (11 \u2237 [])) \u2237\n (model (12 \u2237 4 \u2237 10 \u2237 1 \u2237 []) (11 \u2237 3 \u2237 [])) \u2237\n (model (3 \u2237 6 \u2237 5 \u2237 []) ([])) \u2237\n (model (1 \u2237 2 \u2237 3 \u2237 4 \u2237 5 \u2237 6 \u2237 7 \u2237 8 \u2237 9 \u2237 10 \u2237 11 \u2237 []) (12 \u2237 [])) \u2237 []\n\ntestp : Arrow\ntestp = (5 \u21d2 (\u21d2 10))\n\ntestd : Arrow\ntestd = (5 \u21d2 (\u21d2 4))\n\ntests : Arrow\ntests = (5 \u21d2 (\u21d2 3))\n\ntestu : Arrow\ntestu = (6 \u21d2 (\u21d2 1))\n","old_contents":"data Bool : Set where\n true : Bool\n false : Bool\n\n\n_or_ : Bool \u2192 Bool \u2192 Bool\ntrue or _ = true\n_ or true = true\nfalse or false = false\n\n_and_ : Bool \u2192 Bool \u2192 Bool\nfalse and _ = false\n_ and false = false\ntrue and true = true\n\n\n----------------------------------------\n\n\n\ndata \u2115 : Set where\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n{-# BUILTIN NATURAL \u2115 #-}\n\n\n_\u2261_ : \u2115 \u2192 \u2115 \u2192 Bool\nzero \u2261 zero = true\nsuc n \u2261 suc m = n \u2261 m\n_ \u2261 _ = false\n\n\n\n----------------------------------------\n\n\ninfixr 5 _\u2237_\n\ndata List (A : Set) : Set where\n \u2218 : List A\n _\u2237_ : A \u2192 List A \u2192 List A\n\n\nany : {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 Bool\nany _ \u2218 = false\nany f (x \u2237 xs) = (f x) or (any f xs)\n\n\n_\u2208_ : \u2115 \u2192 List \u2115 \u2192 Bool\nx \u2208 \u2218 = false\nx \u2208 (y \u2237 ys) with x \u2261 y\n... | true = true\n... | false = x \u2208 ys\n\n\n_\u220b_ : List \u2115 \u2192 \u2115 \u2192 Bool\nxs \u220b y = y \u2208 xs\n\n----------------------------------------\n\n\n\ndata Arrow : Set where\n \u21d2_ : \u2115 \u2192 Arrow\n _\u21d2_ : \u2115 \u2192 Arrow \u2192 Arrow\n\n_\u2261\u2261_ : Arrow \u2192 Arrow \u2192 Bool\n(\u21d2 q) \u2261\u2261 (\u21d2 s) = q \u2261 s\n(p \u21d2 q) \u2261\u2261 (r \u21d2 s) = (p \u2261 r) and (q \u2261\u2261 s)\n_ \u2261\u2261 _ = false\n\n_\u2208\u2208_ : Arrow \u2192 List Arrow \u2192 Bool\nx \u2208\u2208 \u2218 = false\nx \u2208\u2208 (y \u2237 ys) with x \u2261\u2261 y\n... | true = true\n... | false = x \u2208\u2208 ys\n\n\nclosure : List Arrow \u2192 List \u2115 \u2192 List \u2115\nclosure \u2218 found = found\nclosure ((\u21d2 n) \u2237 rest) found = n \u2237 (closure rest (n \u2237 found))\nclosure ((n \u21d2 q) \u2237 rest) found with (n \u2208 found) or (n \u2208 (closure rest found))\n... | true = closure (q \u2237 rest) found\n... | false = closure rest found\n\n\n\n_,_\u22a2_ : List Arrow \u2192 List \u2115 \u2192 \u2115 \u2192 Bool\ncs , ps \u22a2 q = q \u2208 (closure cs ps)\n\n_\u22a2_ : List Arrow \u2192 Arrow \u2192 Bool\ncs \u22a2 (\u21d2 q) = q \u2208 (closure cs \u2218)\ncs \u22a2 (p \u21d2 q) = ((\u21d2 p) \u2237 cs) \u22a2 q\n\n\n----------------------------------------\n\n\n\ndata Separation : Set where\n model : List \u2115 \u2192 List \u2115 \u2192 Separation\n\n\nmodelsupports : Separation \u2192 List Arrow \u2192 \u2115 \u2192 Bool\nmodelsupports (model holds _) cs n = cs , holds \u22a2 n\n\n\nmodeldenies : Separation \u2192 List Arrow \u2192 \u2115 \u2192 Bool\nmodeldenies (model _ fails) cs n = any (_\u220b_ (closure cs (n \u2237 \u2218))) fails\n\n\n_\u27ea!_\u27eb_ : List Arrow \u2192 Separation \u2192 Arrow \u2192 Bool\ncs \u27ea! m \u27eb (\u21d2 q) = modeldenies m cs q\ncs \u27ea! m \u27eb (p \u21d2 q) = (modelsupports m cs p) and (cs \u27ea! m \u27eb q)\n\n\n_\u27ea_\u27eb_ : List Arrow \u2192 List Separation \u2192 Arrow \u2192 Bool\ncs \u27ea \u2218 \u27eb arr = false\ncs \u27ea m \u2237 ms \u27eb arr = (cs \u27ea! m \u27eb arr) or (cs \u27ea ms \u27eb arr)\n\n\n\n----------------------------------------\n\n\n\ndata Relation : Set where\n Proved : Relation\n Derivable : Relation\n Separated : Relation\n Unknown : Relation\n\n\nconsider : List Arrow \u2192 List Separation \u2192 Arrow \u2192 Relation\nconsider cs ms arr with (arr \u2208\u2208 cs)\n... | true = Proved\n... | false with (cs \u22a2 arr)\n... | true = Derivable\n... | false with (cs \u27ea ms \u27eb arr)\n... | true = Separated\n... | false = Unknown\n\n\nproofs : List Arrow\nproofs =\n (3 \u21d2 (\u21d2 4)) \u2237\n-- (5 \u21d2 (\u21d2 4)) \u2237\n (6 \u21d2 (\u21d2 4)) \u2237\n (3 \u21d2 (\u21d2 3)) \u2237\n (3 \u21d2 (\u21d2 3)) \u2237\n (9 \u21d2 (\u21d2 3)) \u2237\n (9 \u21d2 (\u21d2 3)) \u2237\n (5 \u21d2 (\u21d2 7)) \u2237\n (9 \u21d2 (\u21d2 7)) \u2237\n (6 \u21d2 (\u21d2 8)) \u2237\n (10 \u21d2 (\u21d2 8)) \u2237\n (10 \u21d2 (\u21d2 7)) \u2237\n (5 \u21d2 (\u21d2 10)) \u2237\n (10 \u21d2 (\u21d2 4)) \u2237\n (5 \u21d2 (\u21d2 11)) \u2237\n (6 \u21d2 (\u21d2 11)) \u2237\n (11 \u21d2 (\u21d2 4)) \u2237\n (10 \u21d2 (\u21d2 7)) \u2237\n (8 \u21d2 (\u21d2 4)) \u2237\n (9 \u21d2 (\u21d2 7)) \u2237\n (9 \u21d2 (\u21d2 8)) \u2237\n (3 \u21d2 (\u21d2 8)) \u2237\n (5 \u21d2 (3 \u21d2 (\u21d2 9))) \u2237\n (6 \u21d2 (7 \u21d2 (\u21d2 10))) \u2237\n (6 \u21d2 (3 \u21d2 (\u21d2 3))) \u2237\n (7 \u21d2 (\u21d2 7)) \u2237\n (7 \u21d2 (\u21d2 7)) \u2237\n (7 \u21d2 (8 \u21d2 (\u21d2 10))) \u2237\n (3 \u21d2 (10 \u21d2 (\u21d2 9))) \u2237\n (5 \u21d2 (\u21d2 1)) \u2237\n (3 \u21d2 (1 \u21d2 (\u21d2 9))) \u2237\n (1 \u21d2 (\u21d2 2)) \u2237\n (10 \u21d2 (\u21d2 2)) \u2237 \u2218\n\ncms : List Separation\ncms =\n (model (12 \u2237 6 \u2237 11 \u2237 4 \u2237 1 \u2237 \u2218) (5 \u2237 3 \u2237 7 \u2237 7 \u2237 \u2218)) \u2237\n (model (6 \u2237 3 \u2237 11 \u2237 4 \u2237 7 \u2237 8 \u2237 3 \u2237 9 \u2237 10 \u2237 1 \u2237 \u2218) (5 \u2237 \u2218)) \u2237\n (model (12 \u2237 5 \u2237 11 \u2237 4 \u2237 1 \u2237 \u2218) (6 \u2237 3 \u2237 \u2218)) \u2237\n (model (5 \u2237 3 \u2237 11 \u2237 4 \u2237 7 \u2237 8 \u2237 3 \u2237 9 \u2237 10 \u2237 1 \u2237 \u2218) (6 \u2237 \u2218)) \u2237\n (model (12 \u2237 4 \u2237 11 \u2237 \u2218) (5 \u2237 6 \u2237 3 \u2237 8 \u2237 9 \u2237 1 \u2237 \u2218)) \u2237\n (model (12 \u2237 5 \u2237 6 \u2237 4 \u2237 11 \u2237 1 \u2237 \u2218) (3 \u2237 \u2218)) \u2237\n (model (12 \u2237 4 \u2237 11 \u2237 7 \u2237 \u2218) (9 \u2237 5 \u2237 6 \u2237 10 \u2237 8 \u2237 1 \u2237 \u2218)) \u2237\n (model (10 \u2237 9 \u2237 \u2218) (1 \u2237 \u2218)) \u2237\n (model (3 \u2237 4 \u2237 11 \u2237 \u2218) (9 \u2237 5 \u2237 6 \u2237 10 \u2237 7 \u2237 1 \u2237 \u2218)) \u2237\n (model (12 \u2237 7 \u2237 1 \u2237 \u2218) (4 \u2237 11 \u2237 8 \u2237 \u2218)) \u2237\n (model (9 \u2237 3 \u2237 10 \u2237 8 \u2237 1 \u2237 \u2218) (11 \u2237 \u2218)) \u2237\n (model (12 \u2237 4 \u2237 10 \u2237 1 \u2237 \u2218) (11 \u2237 3 \u2237 \u2218)) \u2237\n (model (3 \u2237 6 \u2237 5 \u2237 \u2218) (\u2218)) \u2237\n (model (1 \u2237 2 \u2237 3 \u2237 4 \u2237 5 \u2237 6 \u2237 7 \u2237 8 \u2237 9 \u2237 10 \u2237 11 \u2237 \u2218) (12 \u2237 \u2218)) \u2237 \u2218\n\ntestp : Arrow\ntestp = (5 \u21d2 (\u21d2 10))\n\ntestd : Arrow\ntestd = (5 \u21d2 (\u21d2 4))\n\ntests : Arrow\ntests = (5 \u21d2 (\u21d2 3))\n\ntestu : Arrow\ntestu = (6 \u21d2 (\u21d2 1))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"19fcdd904a770324b30cfe4d9f92a08b16860ac6","subject":"Bits: +and +\u2713-and +\u2713-and'","message":"Bits: +and +\u2713-and +\u2713-and'\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule Data.Bits where\n\nopen import Algebra\nopen import Level.NP\nopen import Type hiding (\u2605)\nopen import Data.Nat.NP hiding (_==_) renaming (_<=_ to _\u2115<=_)\nopen import Data.Bit using (Bit)\nopen import Data.Two renaming (_==_ to _==\u1d47_)\nopen import Data.Fin.NP using (Fin; zero; suc; inject\u2081; inject+; raise; Fin\u25b9\u2115)\nopen import Data.Vec.NP\nopen import Function.NP\nimport Data.List.NP as L\n\n-- Re-export some vector functions, maybe they should be given\n-- less generic types.\nopen Data.Vec.NP public using ([]; _\u2237_; _++_; head; tail; map; replicate; RewireTbl; rewire; rewireTbl; on\u1d62)\n\nBits : \u2115 \u2192 \u2605\u2080\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 \u2605\u2080\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0\u2082\n\n-- Notice that all empty vectors are the same, hence 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1\u2082\n\npattern 0\u2237_ xs = 0\u2082 \u2237 xs\npattern 1\u2237_ xs = 1\u2082 \u2237 xs\n{-\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0\u2082 \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1\u2082 \u2237 xs\n-}\n\n_!_ : \u2200 {a n} {A : \u2605 a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n-- see Data.Bits.Properties\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bit\n[] == [] = 1\u2082\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n_<=_ : \u2200 {n} (xs ys : Bits n) \u2192 Bit\n[] <= [] = 1\u2082\n(1\u2082 \u2237 xs) <= (0\u2082 \u2237 ys) = 0\u2082\n(0\u2082 \u2237 xs) <= (1\u2082 \u2237 ys) = 1\u2082\n(_ \u2237 xs) <= (_ \u2237 ys) = xs <= ys\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n\u2295-group : \u2115 \u2192 Group \u2080 \u2080\n\u2295-group = LiftGroup.group Xor\u00b0.+-group\n\nmodule \u2295-Group (n : \u2115) = Group (\u2295-group n)\nmodule \u2295-Monoid (n : \u2115) = Monoid (\u2295-Group.monoid n)\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 k {n} \u2192 Bits (n + k) \u2192 Bits k\nlsb _ {n} = drop n\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0\u2237 bs) = inject\u2081 (#1 bs)\n#1 (1\u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 map not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0\u2237 1\u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = map 0\u2237_ bs ++ map 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1\u2082\n\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0\u2082\n\n_\u2228\u00b0_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_\u2228\u00b0_ f g x = f x \u2228 g x\n\n_\u2227\u00b0_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_\u2227\u00b0_ f g x = f x \u2227 g x\n\nnot\u00b0 : \u2200 {n} (f : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\nnot\u00b0 f = not \u2218 f\n\nview\u2237 : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b} \u2192 (A \u2192 Vec A n \u2192 B) \u2192 Vec A (suc n) \u2192 B\nview\u2237 f (x \u2237 xs) = f x xs\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0\u2237 []\nsucBCarry (0\u2237 xs) = 0\u2237 sucBCarry xs\nsucBCarry (1\u2237 xs) = view\u2237 (\u03bb x xs \u2192 x \u2237 not x \u2237 xs) (sucBCarry xs)\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n--_[mod_] : \u2115 \u2192 \u2115 \u2192 \u2605\u2080\n--a [mod b ] = DivMod' a b\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0\u2237 xs) = 1\u2237 xs\n sucRB (1\u2237 xs) = 0\u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2^ n)\ntoFin [] = zero\ntoFin (0\u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1\u2237 xs) = raise (2^ n) (toFin xs)\n\nBits\u25b9\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nBits\u25b9\u2115 [] = zero\nBits\u25b9\u2115 (0\u2237 xs) = Bits\u25b9\u2115 xs\nBits\u25b9\u2115 {suc n} (1\u2237 xs) = 2^ n + Bits\u25b9\u2115 xs\n\n\u2115\u25b9Bits : \u2200 {n} \u2192 \u2115 \u2192 Bits n\n\u2115\u25b9Bits {zero} _ = []\n\u2115\u25b9Bits {suc n} x = [0: 0\u2237 \u2115\u25b9Bits x\n 1: 1\u2237 \u2115\u25b9Bits (x \u2238 2^ n)\n ]\u2032 (2^ n \u2115<= x)\n\n\u2115\u25b9Bits\u2032 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\n\u2115\u25b9Bits\u2032 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2^ n) \u2192 Bits n\nfromFin = \u2115\u25b9Bits \u2218 Fin\u25b9\u2115\n\nlookupTbl : \u2200 {n a} {A : \u2605 a} \u2192 Bits n \u2192 Vec A (2^ n) \u2192 A\nlookupTbl [] = head\nlookupTbl (0\u2237 key) = lookupTbl key \u2218 take _\nlookupTbl {suc n} (1\u2237 key) = lookupTbl key \u2218 drop (2^ n)\n\nfunFromTbl : \u2200 {n a} {A : \u2605 a} \u2192 Vec A (2^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : \u2605 a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_)\n ++ tblFromFun {n} (f \u2218 1\u2237_)\n\nand : \u2200 {n} \u2192 Bits n \u2192 \ud835\udfda\nand = foldr _ _\u2227_ 1\u2082\n\n\u2713-and : \u2200 {n}{xs : Bits n} \u2192 (\u2200 l \u2192 \u2713(xs \u203c l)) \u2192 \u2713(and xs)\n\u2713-and {xs = []} p = _\n\u2713-and {xs = x \u2237 xs} p = \u2713\u2227 (p zero) (\u2713-and (p \u2218 suc))\n\n\u2713-and' : \u2200 {n}{xs : Bits n} \u2192 \u2713(and xs) \u2192 \u2200 l \u2192 \u2713(xs \u203c l)\n\u2713-and' {xs = x \u2237 xs} e zero = \u2713\u2227\u2081 {x} e\n\u2713-and' {xs = x \u2237 xs} e (suc l) = \u2713-and' (\u2713\u2227\u2082 {x} e) l\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule Data.Bits where\n\nopen import Algebra\nopen import Level.NP\nopen import Type hiding (\u2605)\nopen import Data.Nat.NP hiding (_==_) renaming (_<=_ to _\u2115<=_)\nopen import Data.Bit using (Bit)\nopen import Data.Two renaming (_==_ to _==\u1d47_)\nopen import Data.Fin.NP using (Fin; zero; suc; inject\u2081; inject+; raise; Fin\u25b9\u2115)\nopen import Data.Vec.NP\nopen import Function.NP\nimport Data.List.NP as L\n\n-- Re-export some vector functions, maybe they should be given\n-- less generic types.\nopen Data.Vec.NP public using ([]; _\u2237_; _++_; head; tail; map; replicate; RewireTbl; rewire; rewireTbl; on\u1d62)\n\nBits : \u2115 \u2192 \u2605\u2080\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 \u2605\u2080\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0\u2082\n\n-- Notice that all empty vectors are the same, hence 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1\u2082\n\npattern 0\u2237_ xs = 0\u2082 \u2237 xs\npattern 1\u2237_ xs = 1\u2082 \u2237 xs\n{-\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0\u2082 \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1\u2082 \u2237 xs\n-}\n\n_!_ : \u2200 {a n} {A : \u2605 a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n-- see Data.Bits.Properties\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bit\n[] == [] = 1\u2082\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n_<=_ : \u2200 {n} (xs ys : Bits n) \u2192 Bit\n[] <= [] = 1\u2082\n(1\u2082 \u2237 xs) <= (0\u2082 \u2237 ys) = 0\u2082\n(0\u2082 \u2237 xs) <= (1\u2082 \u2237 ys) = 1\u2082\n(_ \u2237 xs) <= (_ \u2237 ys) = xs <= ys\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n\u2295-group : \u2115 \u2192 Group \u2080 \u2080\n\u2295-group = LiftGroup.group Xor\u00b0.+-group\n\nmodule \u2295-Group (n : \u2115) = Group (\u2295-group n)\nmodule \u2295-Monoid (n : \u2115) = Monoid (\u2295-Group.monoid n)\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 k {n} \u2192 Bits (n + k) \u2192 Bits k\nlsb _ {n} = drop n\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0\u2237 bs) = inject\u2081 (#1 bs)\n#1 (1\u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 map not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0\u2237 1\u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = map 0\u2237_ bs ++ map 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1\u2082\n\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0\u2082\n\n_\u2228\u00b0_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_\u2228\u00b0_ f g x = f x \u2228 g x\n\n_\u2227\u00b0_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_\u2227\u00b0_ f g x = f x \u2227 g x\n\nnot\u00b0 : \u2200 {n} (f : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\nnot\u00b0 f = not \u2218 f\n\nview\u2237 : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b} \u2192 (A \u2192 Vec A n \u2192 B) \u2192 Vec A (suc n) \u2192 B\nview\u2237 f (x \u2237 xs) = f x xs\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0\u2237 []\nsucBCarry (0\u2237 xs) = 0\u2237 sucBCarry xs\nsucBCarry (1\u2237 xs) = view\u2237 (\u03bb x xs \u2192 x \u2237 not x \u2237 xs) (sucBCarry xs)\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n--_[mod_] : \u2115 \u2192 \u2115 \u2192 \u2605\u2080\n--a [mod b ] = DivMod' a b\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0\u2237 xs) = 1\u2237 xs\n sucRB (1\u2237 xs) = 0\u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2^ n)\ntoFin [] = zero\ntoFin (0\u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1\u2237 xs) = raise (2^ n) (toFin xs)\n\nBits\u25b9\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nBits\u25b9\u2115 [] = zero\nBits\u25b9\u2115 (0\u2237 xs) = Bits\u25b9\u2115 xs\nBits\u25b9\u2115 {suc n} (1\u2237 xs) = 2^ n + Bits\u25b9\u2115 xs\n\n\u2115\u25b9Bits : \u2200 {n} \u2192 \u2115 \u2192 Bits n\n\u2115\u25b9Bits {zero} _ = []\n\u2115\u25b9Bits {suc n} x = [0: 0\u2237 \u2115\u25b9Bits x\n 1: 1\u2237 \u2115\u25b9Bits (x \u2238 2^ n)\n ]\u2032 (2^ n \u2115<= x)\n\n\u2115\u25b9Bits\u2032 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\n\u2115\u25b9Bits\u2032 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2^ n) \u2192 Bits n\nfromFin = \u2115\u25b9Bits \u2218 Fin\u25b9\u2115\n\nlookupTbl : \u2200 {n a} {A : \u2605 a} \u2192 Bits n \u2192 Vec A (2^ n) \u2192 A\nlookupTbl [] = head\nlookupTbl (0\u2237 key) = lookupTbl key \u2218 take _\nlookupTbl {suc n} (1\u2237 key) = lookupTbl key \u2218 drop (2^ n)\n\nfunFromTbl : \u2200 {n a} {A : \u2605 a} \u2192 Vec A (2^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : \u2605 a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_)\n ++ tblFromFun {n} (f \u2218 1\u2237_)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"3bbe7c4df0212e945a979a2c61dc0def85743b6e","subject":"Nicer statement for fromtoDeriveConst","message":"Nicer statement for fromtoDeriveConst\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/DeriveCorrect.agda","new_file":"Thesis\/DeriveCorrect.agda","new_contents":"module Thesis.DeriveCorrect where\n\nopen import Thesis.Lang\nopen import Thesis.Changes\nopen import Thesis.LangChanges\nopen import Thesis.Derive\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Theorem.Groups-Nehemiah\n\nfromtoDeriveConst : \u2200 {\u03c4 : Type} (c : Const \u03c4) \u2192\n ch \u27e6 c \u27e7\u0394Const from \u27e6 c \u27e7Const to \u27e6 c \u27e7Const\nfromtoDeriveConst (lit n) = right-id-int n\nfromtoDeriveConst plus da a1 a2 daa db b1 b2 dbb rewrite sym daa | sym dbb = sym (mn\u00b7pq=mp\u00b7nq {a1} {da} {b1} {db})\nfromtoDeriveConst minus da a1 a2 daa db b1 b2 dbb rewrite sym daa | sym dbb | sym (-m\u00b7-n=-mn {b1} {db}) = sym (mn\u00b7pq=mp\u00b7nq {a1} {da} { - b1} { - db})\nfromtoDeriveConst cons da a1 a2 daa db b1 b2 dbb = daa , dbb\nfromtoDeriveConst fst (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = daa\nfromtoDeriveConst snd (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = dbb\nfromtoDeriveConst linj da a1 a2 daa = sft\u2081 daa\nfromtoDeriveConst rinj db b1 b2 dbb = sft\u2082 dbb\nfromtoDeriveConst match .(inj\u2081 (inj\u2081 _)) .(inj\u2081 _) .(inj\u2081 _) (sft\u2081 daa) df f1 f2 dff dg g1 g2 dgg = dff _ _ _ daa\nfromtoDeriveConst match .(inj\u2081 (inj\u2082 _)) .(inj\u2082 _) .(inj\u2082 _) (sft\u2082 dbb) df f1 f2 dff dg g1 g2 dgg = dgg _ _ _ dbb\nfromtoDeriveConst match .(inj\u2082 (inj\u2082 b2)) .(inj\u2081 a1) .(inj\u2082 b2) (sftrp\u2081 a1 b2) df f1 f2 dff dg g1 g2 dgg\n rewrite changeMatchSem-lem1 f1 df g1 dg a1 b2\n | sym (fromto\u2192\u2295 dg g1 g2 dgg) = \u229d-fromto (f1 a1) ((g1 \u2295 dg) b2)\nfromtoDeriveConst match .(inj\u2082 (inj\u2081 a2)) .(inj\u2082 b1) .(inj\u2081 a2) (sftrp\u2082 b1 a2) df f1 f2 dff dg g1 g2 dgg\n rewrite changeMatchSem-lem2 f1 df g1 dg b1 a2\n | sym (fromto\u2192\u2295 df f1 f2 dff)\n = \u229d-fromto (g1 b1) ((f1 \u2295 df) a2)\n\nfromtoDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192\n \u2200 {d\u03c1 \u03c11 \u03c12} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ]\u03c4 (\u27e6 x \u27e7\u0394Var \u03c11 d\u03c1) from (\u27e6 x \u27e7Var \u03c11) to (\u27e6 x \u27e7Var \u03c12)\nfromtoDeriveVar this (dvv v\u2022 d\u03c1\u03c1) = dvv\nfromtoDeriveVar (that x) (dvv v\u2022 d\u03c1\u03c1) = fromtoDeriveVar x d\u03c1\u03c1\n\nfromtoDerive : \u2200 {\u0393} \u03c4 \u2192 (t : Term \u0393 \u03c4) \u2192\n {d\u03c1 : Ch\u0393 \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ]\u03c4 (\u27e6 t \u27e7\u0394Term \u03c11 d\u03c1) from (\u27e6 t \u27e7Term \u03c11) to (\u27e6 t \u27e7Term \u03c12)\nfromtoDerive \u03c4 (const c) {d\u03c1} {\u03c11} d\u03c1\u03c1 rewrite \u27e6 c \u27e7\u0394Const-rewrite \u03c11 d\u03c1 = fromtoDeriveConst c\nfromtoDerive \u03c4 (var x) d\u03c1\u03c1 = fromtoDeriveVar x d\u03c1\u03c1\nfromtoDerive \u03c4 (app {\u03c3} s t) d\u03c1\u03c1 rewrite sym (fit-sound t d\u03c1\u03c1) =\n let fromToF = fromtoDerive (\u03c3 \u21d2 \u03c4) s d\u03c1\u03c1\n in let fromToB = fromtoDerive \u03c3 t d\u03c1\u03c1 in fromToF _ _ _ fromToB\nfromtoDerive (\u03c3 \u21d2 \u03c4) (abs t) d\u03c1\u03c1 = \u03bb dv v1 v2 dvv \u2192\n fromtoDerive \u03c4 t (dvv v\u2022 d\u03c1\u03c1)\n\n-- Getting to the original equation 1 from PLDI'14.\n\ncorrectDeriveOplus : \u2200 {\u0393} \u03c4 \u2192 (t : Term \u0393 \u03c4) \u2192\n {d\u03c1 : Ch\u0393 \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n (\u27e6 t \u27e7Term \u03c11) \u2295 (\u27e6 t \u27e7\u0394Term \u03c11 d\u03c1) \u2261 (\u27e6 t \u27e7Term \u03c12)\ncorrectDeriveOplus \u03c4 t d\u03c1\u03c1 = fromto\u2192\u2295 _ _ _ (fromtoDerive \u03c4 t d\u03c1\u03c1)\n\nopen import Thesis.LangOps\n\ncorrectDeriveOplus\u03c4 : \u2200 {\u0393} \u03c4 \u2192 (t : Term \u0393 \u03c4)\n {d\u03c1 : Ch\u0393 \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n (\u27e6 app\u2082 (oplus\u03c4o \u03c4) (fit t) (derive t) \u27e7Term d\u03c1) \u2261 (\u27e6 t \u27e7Term \u03c12)\ncorrectDeriveOplus\u03c4 \u03c4 t {d\u03c1 = d\u03c1} {\u03c11 = \u03c11} d\u03c1\u03c1\n rewrite oplus\u03c4-equiv _ d\u03c1 _ (\u27e6 fit t \u27e7Term d\u03c1) (\u27e6 derive t \u27e7Term d\u03c1)\n | sym (fit-sound t d\u03c1\u03c1)\n = correctDeriveOplus \u03c4 t d\u03c1\u03c1\n\nderiveGivesDerivative : \u2200 {\u0393} \u03c3 \u03c4 \u2192 (f : Term \u0393 (\u03c3 \u21d2 \u03c4)) (a : Term \u0393 \u03c3)\u2192\n {d\u03c1 : Ch\u0393 \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n (\u27e6 app f a \u27e7Term \u03c11) \u2295 (\u27e6 app f a \u27e7\u0394Term \u03c11 d\u03c1) \u2261 (\u27e6 app f a \u27e7Term \u03c12)\nderiveGivesDerivative \u03c3 \u03c4 f a d\u03c1\u03c1 = correctDeriveOplus \u03c4 (app f a) d\u03c1\u03c1\n\nderiveGivesDerivative\u2082 : \u2200 {\u0393} \u03c3 \u03c4 \u2192 (f : Term \u0393 (\u03c3 \u21d2 \u03c4)) (a : Term \u0393 \u03c3) \u2192\n {d\u03c1 : Ch\u0393 \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n (\u27e6 app\u2082 (oplus\u03c4o \u03c4) (fit (app f a)) (app\u2082 (derive f) (fit a) (derive a)) \u27e7Term d\u03c1) \u2261 (\u27e6 app f a \u27e7Term \u03c12)\nderiveGivesDerivative\u2082 \u03c3 \u03c4 f a d\u03c1\u03c1 = correctDeriveOplus\u03c4 \u03c4 (app f a) d\u03c1\u03c1\n\n-- Proof of the original equation 1 from PLDI'14. The original was restricted to\n-- closed terms. This is a generalization, because it holds also for open terms,\n-- *as long as* the environment change is a nil change.\neq1 : \u2200 {\u0393} \u03c3 \u03c4 \u2192\n {nil\u03c1 : Ch\u0393 \u0393} {\u03c1 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 nil\u03c1 from \u03c1 to \u03c1 \u2192\n \u2200 (f : Term \u0393 (\u03c3 \u21d2 \u03c4)) (a : Term \u0393 \u03c3) (da : Term (\u0394\u0393 \u0393) (\u0394t \u03c3)) \u2192\n (daa : [ \u03c3 ]\u03c4 (\u27e6 da \u27e7Term nil\u03c1) from (\u27e6 a \u27e7Term \u03c1) to (\u27e6 a \u27e7Term \u03c1 \u2295 \u27e6 da \u27e7Term nil\u03c1)) \u2192\n \u27e6 app\u2082 (oplus\u03c4o \u03c4) (fit (app f a)) (app\u2082 (derive f) (fit a) da) \u27e7Term nil\u03c1 \u2261 \u27e6 app (fit f) (app\u2082 (oplus\u03c4o \u03c3) (fit a) da) \u27e7Term nil\u03c1\neq1 \u03c3 \u03c4 {nil\u03c1} {\u03c1} d\u03c1\u03c1 f a da daa\n rewrite\n oplus\u03c4-equiv _ nil\u03c1 _ (\u27e6 fit (app f a) \u27e7Term nil\u03c1) (\u27e6 (app\u2082 (derive f) (fit a) da) \u27e7Term nil\u03c1)\n | sym (fit-sound f d\u03c1\u03c1)\n | oplus\u03c4-equiv _ nil\u03c1 _ (\u27e6 fit a \u27e7Term nil\u03c1) (\u27e6 da \u27e7Term nil\u03c1)\n | sym (fit-sound a d\u03c1\u03c1)\n = fromto\u2192\u2295 (\u27e6 f \u27e7\u0394Term \u03c1 nil\u03c1 (\u27e6 a \u27e7Term \u03c1) (\u27e6 da \u27e7Term nil\u03c1)) _ _\n (fromtoDerive _ f d\u03c1\u03c1 (\u27e6 da \u27e7Term nil\u03c1) (\u27e6 a \u27e7Term \u03c1)\n (\u27e6 a \u27e7Term \u03c1 \u2295 \u27e6 da \u27e7Term nil\u03c1) daa)\n","old_contents":"module Thesis.DeriveCorrect where\n\nopen import Thesis.Lang\nopen import Thesis.Changes\nopen import Thesis.LangChanges\nopen import Thesis.Derive\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Theorem.Groups-Nehemiah\n\nfromtoDeriveConst : \u2200 {\u03c4} c \u2192\n [ \u03c4 ]\u03c4 \u27e6 deriveConst c \u27e7Term \u2205 from \u27e6 c \u27e7Const to \u27e6 c \u27e7Const\nfromtoDeriveConst (lit n) = right-id-int n\nfromtoDeriveConst plus da a1 a2 daa db b1 b2 dbb rewrite sym daa | sym dbb = sym (mn\u00b7pq=mp\u00b7nq {a1} {da} {b1} {db})\nfromtoDeriveConst minus da a1 a2 daa db b1 b2 dbb rewrite sym daa | sym dbb | sym (-m\u00b7-n=-mn {b1} {db}) = sym (mn\u00b7pq=mp\u00b7nq {a1} {da} { - b1} { - db})\nfromtoDeriveConst cons da a1 a2 daa db b1 b2 dbb = daa , dbb\nfromtoDeriveConst fst (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = daa\nfromtoDeriveConst snd (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = dbb\nfromtoDeriveConst linj da a1 a2 daa = sft\u2081 daa\nfromtoDeriveConst rinj db b1 b2 dbb = sft\u2082 dbb\nfromtoDeriveConst match .(inj\u2081 (inj\u2081 _)) .(inj\u2081 _) .(inj\u2081 _) (sft\u2081 daa) df f1 f2 dff dg g1 g2 dgg = dff _ _ _ daa\nfromtoDeriveConst match .(inj\u2081 (inj\u2082 _)) .(inj\u2082 _) .(inj\u2082 _) (sft\u2082 dbb) df f1 f2 dff dg g1 g2 dgg = dgg _ _ _ dbb\nfromtoDeriveConst match .(inj\u2082 (inj\u2082 b2)) .(inj\u2081 a1) .(inj\u2082 b2) (sftrp\u2081 a1 b2) df f1 f2 dff dg g1 g2 dgg\n rewrite changeMatchSem-lem1 f1 df g1 dg a1 b2\n | sym (fromto\u2192\u2295 dg g1 g2 dgg) = \u229d-fromto (f1 a1) ((g1 \u2295 dg) b2)\nfromtoDeriveConst match .(inj\u2082 (inj\u2081 a2)) .(inj\u2082 b1) .(inj\u2081 a2) (sftrp\u2082 b1 a2) df f1 f2 dff dg g1 g2 dgg\n rewrite changeMatchSem-lem2 f1 df g1 dg b1 a2\n | sym (fromto\u2192\u2295 df f1 f2 dff)\n = \u229d-fromto (g1 b1) ((f1 \u2295 df) a2)\n\nfromtoDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192\n \u2200 {d\u03c1 \u03c11 \u03c12} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ]\u03c4 (\u27e6 x \u27e7\u0394Var \u03c11 d\u03c1) from (\u27e6 x \u27e7Var \u03c11) to (\u27e6 x \u27e7Var \u03c12)\nfromtoDeriveVar this (dvv v\u2022 d\u03c1\u03c1) = dvv\nfromtoDeriveVar (that x) (dvv v\u2022 d\u03c1\u03c1) = fromtoDeriveVar x d\u03c1\u03c1\n\nfromtoDerive : \u2200 {\u0393} \u03c4 \u2192 (t : Term \u0393 \u03c4) \u2192\n {d\u03c1 : Ch\u0393 \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ]\u03c4 (\u27e6 t \u27e7\u0394Term \u03c11 d\u03c1) from (\u27e6 t \u27e7Term \u03c11) to (\u27e6 t \u27e7Term \u03c12)\nfromtoDerive \u03c4 (const c) {d\u03c1} {\u03c11} d\u03c1\u03c1 rewrite \u27e6 c \u27e7\u0394Const-rewrite \u03c11 d\u03c1 = fromtoDeriveConst c\nfromtoDerive \u03c4 (var x) d\u03c1\u03c1 = fromtoDeriveVar x d\u03c1\u03c1\nfromtoDerive \u03c4 (app {\u03c3} s t) d\u03c1\u03c1 rewrite sym (fit-sound t d\u03c1\u03c1) =\n let fromToF = fromtoDerive (\u03c3 \u21d2 \u03c4) s d\u03c1\u03c1\n in let fromToB = fromtoDerive \u03c3 t d\u03c1\u03c1 in fromToF _ _ _ fromToB\nfromtoDerive (\u03c3 \u21d2 \u03c4) (abs t) d\u03c1\u03c1 = \u03bb dv v1 v2 dvv \u2192\n fromtoDerive \u03c4 t (dvv v\u2022 d\u03c1\u03c1)\n\n-- Getting to the original equation 1 from PLDI'14.\n\ncorrectDeriveOplus : \u2200 {\u0393} \u03c4 \u2192 (t : Term \u0393 \u03c4) \u2192\n {d\u03c1 : Ch\u0393 \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n (\u27e6 t \u27e7Term \u03c11) \u2295 (\u27e6 t \u27e7\u0394Term \u03c11 d\u03c1) \u2261 (\u27e6 t \u27e7Term \u03c12)\ncorrectDeriveOplus \u03c4 t d\u03c1\u03c1 = fromto\u2192\u2295 _ _ _ (fromtoDerive \u03c4 t d\u03c1\u03c1)\n\nopen import Thesis.LangOps\n\ncorrectDeriveOplus\u03c4 : \u2200 {\u0393} \u03c4 \u2192 (t : Term \u0393 \u03c4)\n {d\u03c1 : Ch\u0393 \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n (\u27e6 app\u2082 (oplus\u03c4o \u03c4) (fit t) (derive t) \u27e7Term d\u03c1) \u2261 (\u27e6 t \u27e7Term \u03c12)\ncorrectDeriveOplus\u03c4 \u03c4 t {d\u03c1 = d\u03c1} {\u03c11 = \u03c11} d\u03c1\u03c1\n rewrite oplus\u03c4-equiv _ d\u03c1 _ (\u27e6 fit t \u27e7Term d\u03c1) (\u27e6 derive t \u27e7Term d\u03c1)\n | sym (fit-sound t d\u03c1\u03c1)\n = correctDeriveOplus \u03c4 t d\u03c1\u03c1\n\nderiveGivesDerivative : \u2200 {\u0393} \u03c3 \u03c4 \u2192 (f : Term \u0393 (\u03c3 \u21d2 \u03c4)) (a : Term \u0393 \u03c3)\u2192\n {d\u03c1 : Ch\u0393 \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n (\u27e6 app f a \u27e7Term \u03c11) \u2295 (\u27e6 app f a \u27e7\u0394Term \u03c11 d\u03c1) \u2261 (\u27e6 app f a \u27e7Term \u03c12)\nderiveGivesDerivative \u03c3 \u03c4 f a d\u03c1\u03c1 = correctDeriveOplus \u03c4 (app f a) d\u03c1\u03c1\n\nderiveGivesDerivative\u2082 : \u2200 {\u0393} \u03c3 \u03c4 \u2192 (f : Term \u0393 (\u03c3 \u21d2 \u03c4)) (a : Term \u0393 \u03c3) \u2192\n {d\u03c1 : Ch\u0393 \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n (\u27e6 app\u2082 (oplus\u03c4o \u03c4) (fit (app f a)) (app\u2082 (derive f) (fit a) (derive a)) \u27e7Term d\u03c1) \u2261 (\u27e6 app f a \u27e7Term \u03c12)\nderiveGivesDerivative\u2082 \u03c3 \u03c4 f a d\u03c1\u03c1 = correctDeriveOplus\u03c4 \u03c4 (app f a) d\u03c1\u03c1\n\n-- Proof of the original equation 1 from PLDI'14. The original was restricted to\n-- closed terms. This is a generalization, because it holds also for open terms,\n-- *as long as* the environment change is a nil change.\neq1 : \u2200 {\u0393} \u03c3 \u03c4 \u2192\n {nil\u03c1 : Ch\u0393 \u0393} {\u03c1 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 nil\u03c1 from \u03c1 to \u03c1 \u2192\n \u2200 (f : Term \u0393 (\u03c3 \u21d2 \u03c4)) (a : Term \u0393 \u03c3) (da : Term (\u0394\u0393 \u0393) (\u0394t \u03c3)) \u2192\n (daa : [ \u03c3 ]\u03c4 (\u27e6 da \u27e7Term nil\u03c1) from (\u27e6 a \u27e7Term \u03c1) to (\u27e6 a \u27e7Term \u03c1 \u2295 \u27e6 da \u27e7Term nil\u03c1)) \u2192\n \u27e6 app\u2082 (oplus\u03c4o \u03c4) (fit (app f a)) (app\u2082 (derive f) (fit a) da) \u27e7Term nil\u03c1 \u2261 \u27e6 app (fit f) (app\u2082 (oplus\u03c4o \u03c3) (fit a) da) \u27e7Term nil\u03c1\neq1 \u03c3 \u03c4 {nil\u03c1} {\u03c1} d\u03c1\u03c1 f a da daa\n rewrite\n oplus\u03c4-equiv _ nil\u03c1 _ (\u27e6 fit (app f a) \u27e7Term nil\u03c1) (\u27e6 (app\u2082 (derive f) (fit a) da) \u27e7Term nil\u03c1)\n | sym (fit-sound f d\u03c1\u03c1)\n | oplus\u03c4-equiv _ nil\u03c1 _ (\u27e6 fit a \u27e7Term nil\u03c1) (\u27e6 da \u27e7Term nil\u03c1)\n | sym (fit-sound a d\u03c1\u03c1)\n = fromto\u2192\u2295 (\u27e6 f \u27e7\u0394Term \u03c1 nil\u03c1 (\u27e6 a \u27e7Term \u03c1) (\u27e6 da \u27e7Term nil\u03c1)) _ _\n (fromtoDerive _ f d\u03c1\u03c1 (\u27e6 da \u27e7Term nil\u03c1) (\u27e6 a \u27e7Term \u03c1)\n (\u27e6 a \u27e7Term \u03c1 \u2295 \u27e6 da \u27e7Term nil\u03c1) daa)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"66053c0d43dccf3dd648c77092869da45b9a0486","subject":"lam case","message":"lam case\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"expandability.agda","new_file":"expandability.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import htype-decidable\n\nmodule expandability where\n mutual\n expandability-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ]\n (\u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394)\n expandability-synth SConst = c , \u2205 , ESConst\n expandability-synth (SAsc {\u03c4 = \u03c4} wt)\n with expandability-ana wt\n ... | d' , \u0394' , \u03c4' , D with htype-dec \u03c4 \u03c4'\n ... | Inl _ = d' , \u0394' , ESAsc2 D\n ... | Inr x = (< _ > d') , \u0394' , ESAsc1 D x\n expandability-synth (SVar {n = n} x) = X n , \u2205 , ESVar x\n expandability-synth (SAp wt1 MAHole wt2)\n with expandability-synth wt1 | expandability-ana wt2\n ... | d1 , \u03941 , D1\n | d2 , \u03942 , \u03c42 , D2 = ((< \u03c42 ==> \u2987\u2988 > d1) \u2218 d2) , (\u03941 \u222a \u03942) , {!!} -- ESAp1 {!!} wt1 {!!} D2\n expandability-synth (SAp wt1 (MAArr {\u03c42 = \u03c42}) wt2)\n with expandability-synth wt1 | expandability-ana wt2\n ... | d1 , \u03941 , D1\n | d2 , \u03942 , \u03c42' , D2\n with htype-dec \u03c42 \u03c42'\n expandability-synth (SAp wt1 MAArr wt2) | d1 , \u03941 , D1 | d2 , \u03942 , \u03c42' , D2 | Inr neq = (d1 \u2218 (< {!!} > d2)) , (\u03941 \u222a \u03942) , ESAp2 {\u03941 = \u03941} {\u03942 = \u03942} {!!} {!D1!} {!!} neq\n expandability-synth (SAp wt1 MAArr wt2) | d1 , \u03941 , D1 | d2 , \u03942 , \u03c42 , D2 | Inl refl = (d1 \u2218 d2) , (\u03941 \u222a \u03942) , ESAp3 {\u03941 = \u03941} {\u03942 = \u03942} {!!} {!D1!} {!!}\n expandability-synth SEHole = _ , _ , ESEHole\n expandability-synth (SNEHole wt) with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESNEHole wt'\n expandability-synth (SLam x\u2081 wt) with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESLam x\u2081 wt'\n\n expandability-ana : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ] \u03a3[ \u03c4' \u2208 htyp ]\n (\u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394)\n expandability-ana {e = \u2987\u2988[ x ]} (ASubsume wt x\u2081) = _ , _ , _ , EAEHole\n expandability-ana {e = \u2987 e' \u2988[ x ]} (ASubsume (SNEHole wt) x\u2081) with expandability-synth wt\n ... | d , \u0394 , D = _ , _ , _ , EANEHole D\n expandability-ana {e = c} (ASubsume SConst x\u2081) = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) ESConst x\u2081\n expandability-ana {e = e \u00b7: x} (ASubsume (SAsc x\u2081) x\u2082) = {!!}\n expandability-ana {e = X x} (ASubsume (SVar x\u2081) x\u2082) = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) (ESVar x\u2081) x\u2082\n expandability-ana {e = \u00b7\u03bb x e} (ASubsume () x\u2081)\n expandability-ana {e = \u00b7\u03bb x [ x\u2081 ] e} (ASubsume (SLam x\u2082 wt) x\u2083) with expandability-synth wt\n ... | d , \u0394 , D = _ , _ , _ , EASubsume (\u03bb u \u2192 \u03bb ()) (\u03bb e' u \u2192 \u03bb ()) (ESLam x\u2082 D) x\u2083\n expandability-ana {e = e1 \u2218 e\u2081} (ASubsume (SAp wt MAHole x\u2081) x\u2082) = {!!}\n expandability-ana {e = e1 \u2218 e\u2081} (ASubsume (SAp wt MAArr x\u2081) x\u2082) = {!!}\n expandability-ana (ALam x\u2081 MAHole wt) with expandability-ana wt\n ... | d' , \u0394' , \u03c4' , D' = {!!} , {!!} , {!!} , {!!}\n expandability-ana (ALam x\u2081 MAArr wt) with expandability-ana wt\n ... | d' , \u0394'\u202f, \u03c4' , D' = _ , _ , _ , EALam x\u2081 D'\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import htype-decidable\n\nmodule expandability where\n mutual\n expandability-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ]\n (\u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394)\n expandability-synth SConst = c , \u2205 , ESConst\n expandability-synth (SAsc {\u03c4 = \u03c4} wt)\n with expandability-ana wt\n ... | d' , \u0394' , \u03c4' , D with htype-dec \u03c4 \u03c4'\n ... | Inl _ = d' , \u0394' , ESAsc2 D\n ... | Inr x = (< _ > d') , \u0394' , ESAsc1 D x\n expandability-synth (SVar {n = n} x) = X n , \u2205 , ESVar x\n expandability-synth (SAp wt1 MAHole wt2)\n with expandability-synth wt1 | expandability-ana wt2\n ... | d1 , \u03941 , D1\n | d2 , \u03942 , \u03c42 , D2 = ((< \u03c42 ==> \u2987\u2988 > d1) \u2218 d2) , (\u03941 \u222a \u03942) , {!!} -- ESAp1 {!!} wt1 {!!} D2\n expandability-synth (SAp wt1 (MAArr {\u03c42 = \u03c42}) wt2)\n with expandability-synth wt1 | expandability-ana wt2\n ... | d1 , \u03941 , D1\n | d2 , \u03942 , \u03c42' , D2\n with htype-dec \u03c42 \u03c42'\n expandability-synth (SAp wt1 MAArr wt2) | d1 , \u03941 , D1 | d2 , \u03942 , \u03c42' , D2 | Inr neq = (d1 \u2218 (< {!!} > d2)) , (\u03941 \u222a \u03942) , ESAp2 {\u03941 = \u03941} {\u03942 = \u03942} {!!} {!D1!} {!!} neq\n expandability-synth (SAp wt1 MAArr wt2) | d1 , \u03941 , D1 | d2 , \u03942 , \u03c42 , D2 | Inl refl = (d1 \u2218 d2) , (\u03941 \u222a \u03942) , ESAp3 {\u03941 = \u03941} {\u03942 = \u03942} {!!} {!D1!} {!!}\n expandability-synth SEHole = _ , _ , ESEHole\n expandability-synth (SNEHole wt) with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESNEHole wt'\n expandability-synth (SLam x\u2081 wt) with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESLam x\u2081 wt'\n\n expandability-ana : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ] \u03a3[ \u03c4' \u2208 htyp ]\n (\u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394)\n expandability-ana {e = \u2987\u2988[ x ]} (ASubsume wt x\u2081) = _ , _ , _ , EAEHole\n expandability-ana {e = \u2987 e' \u2988[ x ]} (ASubsume (SNEHole wt) x\u2081) with expandability-synth wt\n ... | d , \u0394 , D = _ , _ , _ , EANEHole D\n expandability-ana {e = c} (ASubsume SConst x\u2081) = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) ESConst x\u2081\n expandability-ana {e = e \u00b7: x} (ASubsume (SAsc x\u2081) x\u2082) = {!!}\n expandability-ana {e = X x} (ASubsume (SVar x\u2081) x\u2082) = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) (ESVar x\u2081) x\u2082\n expandability-ana {e = \u00b7\u03bb x e} (ASubsume () x\u2081)\n expandability-ana {e = \u00b7\u03bb x [ x\u2081 ] e} (ASubsume (SLam x\u2082 wt) x\u2083) = _ , _ , _ , EASubsume {!!} {!!} (ESLam x\u2082 {!!}) x\u2083\n expandability-ana {e = e1 \u2218 e\u2081} (ASubsume (SAp wt MAHole x\u2081) x\u2082) = {!!}\n expandability-ana {e = e1 \u2218 e\u2081} (ASubsume (SAp wt MAArr x\u2081) x\u2082) = {!!}\n expandability-ana (ALam x\u2081 MAHole wt) with expandability-ana wt\n ... | d' , \u0394' , \u03c4' , D' = {!!}\n expandability-ana (ALam x\u2081 MAArr wt) with expandability-ana wt\n ... | d' , \u0394'\u202f, \u03c4' , D' = _ , _ , _ , EALam x\u2081 D'\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"6d3a8e3bd26e9797ee4be36b510dac7615f13f71","subject":"Bits: to\u2115\/from\u2115","message":"Bits: to\u2115\/from\u2115\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative.NP\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nopen import Relation.Nullary\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties.NP\nimport Data.List.NP as L\nopen import Function.Bijection.SyntaxKit\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0b \u2237 1b \u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u2219_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u2219 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u2219 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u2219-comm : \u2200 {m} (x y : A m) \u2192 x \u2219 y \u2261 y \u2219 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u2219-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nmodule OperationSyntax where\n module BitBij = BoolBijection\n open BitBij public using (`id) renaming (BoolBij to BitBij; bool-bijKit to bitBijKit; `not to `not\u1d2e)\n open BijectionSyntax Bit BitBij public\n open BijectionSemantics bitBijKit public\n\n {-\n id \u03b5 id\n 0\u21941 swp-inners interchange\n not swap comm\n if0 first ...\n if1 second ...\n -}\n `not : Bij\n `not = BitBij.`not `\u2237 const `id\n\n `xor : Bit \u2192 Bij\n `xor b = BitBij.`xor b `\u2237 const `id\n\n `if : Bij \u2192 Bij \u2192 Bij\n `if f g = BitBij.`id `\u2237 cond f g\n\n `if0 : Bij \u2192 Bij\n `if0 f = `if `id f\n\n `if1 : Bij \u2192 Bij\n `if1 f = `if f `id\n\n -- law: `if0 f `\u204f `if1 g \u2261 `if1 g `; `if0 f\n\n on-firsts : Bij \u2192 Bij\n on-firsts f = `0\u21941 `\u204f `if0 f `\u204f `0\u21941\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 right swap\n -- (a \u2219 d) \u2219 (c \u2219 b)\n swp-seconds : Bij\n swp-seconds = `if1 `not `\u204f `0\u21941 `\u204f `if1 `not\n\n on-extremes : Bij \u2192 Bij\n -- on-extremes f = swp-seconds `\u204f `if0 f `\u204f swp-seconds\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap \u2261 if1 not\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange \u2261 0\u21941\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 left f \u2261 if0 f\n -- (A \u2219 D) \u2219 (b \u2219 c)\n -- where A \u2219 D = f (a \u2219 d)\n -- \u2261 interchange \u2261 0\u21941\n -- (A \u2219 b) \u2219 (D \u2219 c)\n -- \u2261 right swap \u2261 if1 not\n -- (A \u2219 b) \u2219 (c \u2219 D)\n on-extremes f = `if1 `not `\u204f `0\u21941 `\u204f `if0 f `\u204f `0\u21941 `\u204f `if1 `not\n\n map-inner : Bij \u2192 Bij\n map-inner f = `if1 `not `\u204f `0\u21941 `\u204f `if1 f `\u204f `0\u21941 `\u204f `if1 `not\n\n map-outer : Bij \u2192 Bij \u2192 Bij\n map-outer f g = `if g f\n\n 0\u21941\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bij\n 0\u21941\u2237 [] = `not\n 0\u21941\u2237 (true {-1-} \u2237 p) = on-extremes (0\u21941\u2237 p)\n 0\u21941\u2237 (false{-0-} \u2237 p) = on-firsts (0\u21941\u2237 p)\n\n 0\u2194_ : \u2200 {n} \u2192 Bits n \u2192 Bij\n 0\u2194 [] = `id\n 0\u2194 (false{-0-} \u2237 p) = `if0 (0\u2194 p)\n 0\u2194 (true{-1-} \u2237 p) = 0\u21941\u2237 p\n\n \u27e80\u2194_\u27e9-sem : \u2200 {n} (p : Bits n) \u2192 Bits n \u2192 Bits n\n \u27e80\u2194 p \u27e9-sem xs = if 0\u207f == xs then p else if p == xs then 0\u207f else xs\n\n if\u2237 : \u2200 {n} a x (xs ys : Bits n) \u2192 (if a then (x \u2237 xs) else (x \u2237 ys)) \u2261 x \u2237 (if a then xs else ys)\n if\u2237 true x xs ys = refl\n if\u2237 false x xs ys = refl\n\n if-not\u2237 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (false \u2237 xs) else (true \u2237 ys)) \u2261 (not a) \u2237 (if a then xs else ys)\n if-not\u2237 true xs ys = refl\n if-not\u2237 false xs ys = refl\n\n if\u2237\u2032 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (true \u2237 xs) else (false \u2237 ys)) \u2261 a \u2237 (if a then xs else ys)\n if\u2237\u2032 true xs ys = refl\n if\u2237\u2032 false xs ys = refl\n\n \u27e80\u21941\u2237_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u21941\u2237 p \u2219 xs \u2261 \u27e80\u2194 (1\u2237 p) \u27e9-sem xs\n \u27e80\u21941\u2237_\u27e9-spec [] (true \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec [] (false \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 true \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 false \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n\n \u27e80\u2194_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u2194 p \u2219 xs \u2261 \u27e80\u2194 p \u27e9-sem xs\n \u27e80\u2194_\u27e9-spec [] [] = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (true \u2237 xs) = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (false \u2237 xs)\n rewrite \u27e80\u2194 ps \u27e9-spec xs\n | if\u2237 (ps == xs) 0b 0\u207f xs\n | if\u2237 (0\u207f == xs) 0b ps (if ps == xs then 0\u207f else xs)\n = refl\n \u27e80\u2194_\u27e9-spec (true \u2237 p) xs = \u27e80\u21941\u2237 p \u27e9-spec xs\n\n private\n module P where\n open PermutationSyntax public\n open PermutationSemantics public\n open P using (Perm; `id; `0\u21941; _`\u204f_)\n\n `\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij\n `\u27e80\u21941+ zero \u27e9 = `0\u21941\n `\u27e80\u21941+ suc i \u27e9 = `0\u21941 `\u204f `tl `\u27e80\u21941+ i \u27e9 `\u204f `0\u21941\n\n `\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n `\u27e80\u21941+ zero \u27e9-spec xs = refl\n `\u27e80\u21941+ suc i \u27e9-spec (x \u2237 _ \u2237 xs) rewrite `\u27e80\u21941+ i \u27e9-spec (x \u2237 xs) = refl\n\n `\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij\n `\u27e80\u2194 zero \u27e9 = `id\n `\u27e80\u2194 suc i \u27e9 = `\u27e80\u21941+ i \u27e9\n\n `\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n `\u27e80\u2194 zero \u27e9-spec xs = refl\n `\u27e80\u2194 suc i \u27e9-spec xs = `\u27e80\u21941+ i \u27e9-spec xs\n\n {-\n `\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Bij\n `\u27e8 zero \u2194 j \u27e9 = `\u27e80\u2194 j \u27e9\n `\u27e8 i \u2194 zero \u27e9 = `\u27e80\u2194 i \u27e9\n `\u27e8 suc i \u2194 suc j \u27e9 = `tl `\u27e8 i \u2194 j \u27e9\n\n `\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) xs \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n `\u27e8_\u2194_\u27e9-spec zero j xs = `\u27e80\u2194 j \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) zero xs = `\u27e80\u21941+ i \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) (suc j) (x \u2237 xs) rewrite `\u27e8 i \u2194 j \u27e9-spec xs = refl\n -}\n\n `xor-head : Bit \u2192 Bij\n `xor-head b = if b then `not else `id\n\n `xor-head-spec : \u2200 b {n} x (xs : Bits n) \u2192 `xor-head b \u2219 (x \u2237 xs) \u2261 (b xor x) \u2237 xs\n `xor-head-spec true x xs = refl\n `xor-head-spec false x xs = refl\n\n `\u27e8_\u2295\u27e9 : \u2200 {n} \u2192 Bits n \u2192 Bij\n `\u27e8 [] \u2295\u27e9 = `id\n `\u27e8 b \u2237 xs \u2295\u27e9 = `xor-head b `\u204f `tl `\u27e8 xs \u2295\u27e9\n\n `\u27e8_\u2295\u27e9-spec : \u2200 {n} (xs ys : Bits n) \u2192 `\u27e8 xs \u2295\u27e9 \u2219 ys \u2261 xs \u2295 ys\n `\u27e8 [] \u2295\u27e9-spec [] = refl\n `\u27e8 true \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n `\u27e8 false \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) x \u2192 0\u21941 pad \u2295 0\u21941 x \u2261 0\u21941 (pad \u2295 x)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\nmodule PermutationSyntax-Props where\n open PermutationSyntax\n open PermutationSemantics\n -- open PermutationProperties\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) xs \u2192 0\u21941 pad \u2295 0\u21941 xs \u2261 0\u21941 (pad \u2295 xs)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\n -- TODO make use of \u229b-dist-\u2219\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 fs `id xs = refl\n \u2295-dist-\u2219 fs `0\u21941 xs = \u2295-dist-0\u21941 fs xs\n \u2295-dist-\u2219 [] (`tl \u03c0) [] = refl\n \u2295-dist-\u2219 (f \u2237 fs) (`tl \u03c0) (x \u2237 xs) rewrite \u2295-dist-\u2219 fs \u03c0 xs = refl\n \u2295-dist-\u2219 fs (\u03c0\u2080 `\u204f \u03c0\u2081) xs rewrite \u2295-dist-\u2219 (\u03c0\u2080 \u2219 fs) \u03c0\u2081 (\u03c0\u2080 \u2219 xs)\n | \u2295-dist-\u2219 fs \u03c0\u2080 xs = refl\n {-\n -- \u229b-dist-\u2219 : \u2200 {n a} {A : Set a} (fs : Vec (A \u2192 A) n) \u03c0 xs \u2192 \u03c0 \u2219 fs \u229b \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (fs \u229b xs)\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 pad \u03c0 xs = \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs\n \u2261\u27e8 refl \u27e9\n vmap _xor_ (\u03c0 \u2219 pad) \u229b \u03c0 \u2219 xs\n \u2261\u27e8 {!!} \u27e9\n \u03c0 \u2219 vmap _xor_ pad \u229b \u03c0 \u2219 xs\n \u2261\u27e8 \u229b-dist-\u2219 _ (vmap _xor_ pad) \u03c0 xs \u27e9\n \u03c0 \u2219 (vmap _xor_ pad \u229b xs)\n \u2261\u27e8 refl \u27e9\n \u03c0 \u2219 (pad \u2295 xs)\n \u220e where open \u2261-Reasoning\n -- rans {!\u229b-dist-\u2219 (vmap _xor_ (op \u2219 pad)) op xs!} (\u229b-dist-\u2219 (vmap _xor_ pad) op xs)\n-}\nmodule SimpleSearch {a} {A : Set a} (_\u2219_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u2219_ public\n\n module SearchUnit \u03b5 (\u03b5\u2219\u03b5 : \u03b5 \u2219 \u03b5 \u2261 \u03b5) where\n search-const\u03b5\u2261\u03b5 : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-const\u03b5\u2261\u03b5 zero = refl\n search-const\u03b5\u2261\u03b5 (suc n) rewrite search-const\u03b5\u2261\u03b5 n = \u03b5\u2219\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n module SearchInterchange (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u2219 f\u2081 x) \u2261 search f\u2080 \u2219 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u2219-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e\n where open \u2261-Reasoning\n\n search-0\u21941 : \u2200 {n} (f : Bits n \u2192 A) \u2192 search {n} (f \u2218 0\u21941) \u2261 search {n} f\n search-0\u21941 {zero} _ = refl\n search-0\u21941 {suc zero} _ = refl\n search-0\u21941 {suc (suc n)} _ = \u2219-interchange _ _ _ _\n\n module Bij (\u2219-comm : Commutative _\u2261_ _\u2219_)\n (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n open SearchInterchange \u2219-interchange using (search-0\u21941)\n open OperationSyntax hiding (_\u2219_)\n search-bij : \u2200 {n} (f : Bits n \u2192 A) (g : Bij) \u2192 search {n} (f \u2218 eval g) \u2261 search {n} f\n search-bij f `id = refl\n search-bij f `0\u21941 = search-0\u21941 f\n search-bij f (g `\u204f h)\n rewrite search-bij (f \u2218 eval h) g = search-bij f h\n search-bij {zero} f (_ `\u2237 _) = refl\n search-bij {suc n} f (`id `\u2237 g)\n rewrite search-bij (f \u2218 0\u2237_) (g 0b)\n | search-bij (f \u2218 1\u2237_) (g 1b) = refl\n search-bij {suc n} f (`not\u1d2e `\u2237 g)\n rewrite search-bij (f \u2218 0\u2237_) (g 1b)\n | search-bij (f \u2218 1\u2237_) (g 0b) = \u2219-comm _ _\n\nmodule Sum where\n open SimpleSearch _+_ using (module Comm; module SearchInterchange; module SearchUnit; module Bij)\n open SimpleSearch _+_ public using () renaming (search to sum; search-\u2257 to sum-\u2257; searchBit to sumBit;\n search-\u2257\u2082 to sum-\u2257\u2082;\n search-+ to sum-+)\n open Comm \u2115\u00b0.+-comm public renaming (search-comm to sum-comm)\n open SearchUnit 0 refl public renaming\n (search-const\u03b5\u2261\u03b5 to sum-const0\u22610)\n open SearchInterchange +-interchange public renaming (\n search-dist to sum-dist;\n search-searchBit to sum-sumBit;\n search-search to sum-sum;\n search-swap to sum-swap)\n open Bij \u2115\u00b0.+-comm +-interchange public renaming (search-bij to sum-bij)\n\n sum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\n sum-const zero _ = refl\n sum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nmodule Count where\n open Sum\n open OperationSyntax\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n #\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n -- #-ext\n #-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n #-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n #-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n #-bij : \u2200 {n} (f : Bits n \u2192 Bit) (g : Bij) \u2192 #\u27e8 f \u2218 eval g \u27e9 \u2261 #\u27e8 f \u27e9\n #-bij f = sum-bij (Bool.to\u2115 \u2218 f)\n\n #-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n #-\u2295 = #-comm\n\n #-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n #-const n b = sum-const n (Bool.to\u2115 b)\n\n #never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n #never\u22610 = sum-const0\u22610\n\n #always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n #always\u22612^ n = sum-const n 1\n\n #-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 Bit) \u2192 sum (\u03bb x \u2192 Bool.to\u2115 (f\u2080 x) + Bool.to\u2115 (f\u2081 x)) \u2261 #\u27e8 f\u2080 \u27e9 + #\u27e8 f\u2081 \u27e9\n #-dist f\u2080 f\u2081 = sum-dist (Bool.to\u2115 \u2218 f\u2080) (Bool.to\u2115 \u2218 f\u2081)\n\n #-+ : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n #\u27e8 f \u27e9 \u2261 sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9 )\n #-+ {m} {n} f = sum-+ {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-# : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9)\n \u2261 sum {n} (\u03bb ys \u2192 #\u27e8 (\u03bb (xs : Bits m) \u2192 f (xs ++ ys)) \u27e9)\n #-# {m} {n} f = sum-sum {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-swap : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192 #\u27e8 f \u2218 vswap n {m} \u27e9 \u2261 #\u27e8 f \u27e9\n #-swap {m} {n} f = sum-swap {m} {n} (Bool.to\u2115 \u2218 f)\n\n #\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n #\u27e8== [] \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n \u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n \u2257-cong-# f g f\u2257g = sum-\u2257 _ _ (cong Bool.to\u2115 \u2218 f\u2257g)\n\n -- #-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n -- #-+ f f0 f1 rewrite f0 | f1 = refl\n\n #-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-take-drop zero n f g with f []\n ... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n ... | false = #never\u22610 n\n #-take-drop (suc m) n f g\n rewrite \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x))\n | #-take-drop m n (f \u2218 0\u2237_) g\n | \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x))\n | #-take-drop m n (f \u2218 1\u2237_) g\n = sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9)\n\n #-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n #\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n #\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n #\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n ... | true | true | _ = s\u2264s z\u2264n\n ... | true | false | p = \u22a5-elim (p _)\n ... | false | _ | _ = z\u2264n\n #\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n #-\u2227-\u2228\u1d47 : \u2200 x y \u2192 Bool.to\u2115 (x \u2227 y) + Bool.to\u2115 (x \u2228 y) \u2261 Bool.to\u2115 x + Bool.to\u2115 y\n #-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (Bool.to\u2115 y) 1 = refl\n #-\u2227-\u2228\u1d47 false y = refl\n\n #-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n #-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #\u2228' {zero} f g with f []\n ... | true = s\u2264s z\u2264n\n ... | false = \u2115\u2264.refl\n #\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n #\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n #\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n #\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n #-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n #-bound zero f = Bool.to\u2115\u22641 (f [])\n #-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n #-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n #-\u2218vnot _ f = #-\u2295 1\u207f f\n\n #-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n #-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n #-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n #-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n #-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n #-not\u2218 zero f with f []\n ... | true = \u2261.refl\n ... | false = \u2261.refl\n #-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n #-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n #-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\nopen SimpleSearch public\nopen Sum public\nopen Count public\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192\n search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = search-\u2257 op _ _ (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2^ n) (toFin xs)\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2^ n + to\u2115 xs\n\nto\u2115-bound : \u2200 {n} (xs : Bits n) \u2192 to\u2115 xs < 2^ n \nto\u2115-bound [] = s\u2264s z\u2264n\nto\u2115-bound {suc n} (1b \u2237 xs) rewrite +-assoc-comm 1 (2^ n) (to\u2115 xs) = \u2115\u2264.refl {2^ n} +-mono to\u2115-bound xs\nto\u2115-bound {suc n} (0b \u2237 xs) = \u2264-steps (2^ n) (to\u2115-bound xs)\n\nto\u2115\u22642\u207f+ : \u2200 {n} (x : Bits n) {y} \u2192 to\u2115 {n} x \u2264 2^ n + y\nto\u2115\u22642\u207f+ {n} x {y} = \u2115\u2264.trans (\u2264-steps y (\u2264-pred (\u2264-steps 1 (to\u2115-bound x))))\n (\u2115\u2264.reflexive (\u2115\u00b0.+-comm y (2^ n)))\n\n2\u207f+\u2270to\u2115 : \u2200 {n x} (y : Bits n) \u2192 2^ n + x \u2270 to\u2115 {n} y\n2\u207f+\u2270to\u2115 {n} {x} y p = \u00acn+\u2264y\n pair-hom = Compose.\u2218-hom _ _ _\n _ (Delta.\u0394-hom \ud835\udd38)\n _ (Zip.zip-hom _ _ _ _ _ \u03c6\u2080-hom _ \u03c6\u2081-hom)\n -- OR:\n -- pair-hom = mk (ap\u2082 _,_ (hom \u03c6\u2080-hom) (hom \u03c6\u2081-hom))\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule Algebra.Group.Homomorphism where\n\nopen import Type using (Type_)\nopen import Function.NP using (Op\u2082; _\u2218_; id)\nimport Algebra.FunctionProperties.Eq\nopen Algebra.FunctionProperties.Eq.Implicits\nopen import Algebra.Monoid\nopen import Algebra.Monoid.Homomorphism\nopen import Algebra.Raw\nopen import Algebra.Group\nopen import Algebra.Group.Constructions\nopen import Level.NP\nopen import Data.Product.NP\nopen import Data.Nat.NP using (1+_)\nopen import Data.Integer.NP using (\u2124; -[1+_]; +_; -_; module \u2124\u00b0)\nopen import Relation.Binary.PropositionalEquality.NP\nopen \u2261-Reasoning\n\nrecord GroupHomomorphism {a}{A : Type a}{b}{B : Type b}\n (grpA0+ : Group A)(grpB1* : Group B)\n (f : A \u2192 B) : Type (a \u2294 b) where\n constructor mk\n\n open Additive-Group grpA0+\n open Multiplicative-Group grpB1*\n\n field\n hom : Homomorphic\u2082 f _+_ _*_\n\n pres-unit : f 0# \u2261 1#\n pres-unit = unique-1-left part\n where part = f 0# * f 0# \u2261\u27e8 ! hom \u27e9\n f (0# + 0#) \u2261\u27e8 ap f (fst +-identity) \u27e9\n f 0# \u220e\n\n mon-hom : MonoidHomomorphism +-mon *-mon f\n mon-hom = pres-unit , hom\n\n open MonoidHomomorphism mon-hom public\n\n pres-inv : \u2200 {x} \u2192 f (0\u2212 x) \u2261 (f x)\u207b\u00b9\n pres-inv {x} = unique-\u207b\u00b9 part\n where part = f (0\u2212 x) * f x \u2261\u27e8 ! hom \u27e9\n f (0\u2212 x + x) \u2261\u27e8 ap f (fst 0\u2212-inverse) \u27e9\n f 0# \u2261\u27e8 pres-unit \u27e9\n 1# \u220e\n\n 0\u2212-\u207b\u00b9 = pres-inv\n\n \u2212-\/ : \u2200 {x y} \u2192 f (x \u2212 y) \u2261 f x \/ f y\n \u2212-\/ {x} {y} = f (x \u2212 y) \u2261\u27e8 hom \u27e9\n f x * f (0\u2212 y) \u2261\u27e8 ap (_*_ (f x)) pres-inv \u27e9\n f x \/ f y \u220e\n\n hom-iterated\u207b : \u2200 {x} n \u2192 f (x \u2297\u207b n) \u2261 f x ^\u207b n\n hom-iterated\u207b {x} n =\n f (x \u2297\u207b n) \u2261\u27e8by-definition\u27e9\n f (0\u2212(x \u2297\u207a n)) \u2261\u27e8 pres-inv \u27e9\n f(x \u2297\u207a n)\u207b\u00b9 \u2261\u27e8 ap _\u207b\u00b9 (hom-iterated\u207a n) \u27e9\n (f x ^\u207a n)\u207b\u00b9 \u2261\u27e8by-definition\u27e9\n f x ^\u207b n \u220e\n\n hom-iterated : \u2200 {x} i \u2192 f (x \u2297 i) \u2261 f x ^ i\n hom-iterated -[1+ n ] = hom-iterated\u207b (1+ n)\n hom-iterated (+ n) = hom-iterated\u207a n\n\n\u2124+-grp-ops : Group-Ops \u2124\n\u2124+-grp-ops = \u2124+-mon-ops , -_\n\n\u2124+-grp-struct : Group-Struct \u2124+-grp-ops\n\u2124+-grp-struct = \u2124+-mon-struct\n , (\u03bb{x} \u2192 fst \u2124\u00b0.-\u203finverse x)\n , (\u03bb{x} \u2192 snd \u2124\u00b0.-\u203finverse x)\n\n\u2124+-grp : Group \u2124\n\u2124+-grp = _ , \u2124+-grp-struct\n\nmodule \u2124+ = Additive-Group \u2124+-grp\n\nmodule _ {\u2113}{G : Type \u2113}(\ud835\udd3e : Group G) where\n open Group\u1d52\u1d56\n open Group \ud835\udd3e\n\n module \u207b\u00b9-Hom where\n -- The proper type for \u207b\u00b9-hom\u2032\n \u207b\u00b9-hom' : GroupHomomorphism \ud835\udd3e (\ud835\udd3e \u1d52\u1d56) _\u207b\u00b9\n \u207b\u00b9-hom' = mk \u207b\u00b9-hom\u2032\n open GroupHomomorphism \u207b\u00b9-hom' public\n\n module \u2124+-^-Hom {b} where\n ^-+-hom : GroupHomomorphism \u2124+-grp \ud835\udd3e (_^_ b)\n ^-+-hom = mk (\u03bb {i} {j} \u2192 ^-+ i j)\n\n open GroupHomomorphism ^-+-hom public\n\nmodule Stability-Minimal\n {a}{A : Type a}\n {b}{B : Type b}\n (\u03c6 : A \u2192 B)\n (_+_ : Op\u2082 A)\n (_*_ : Op\u2082 B)\n (\u03c6-+-* : \u2200 {x y} \u2192 \u03c6 (x + y) \u2261 \u03c6 x * \u03c6 y)\n {c}{C : Type c}\n (F : (A \u2192 B) \u2192 C)\n (F= : \u2200 {f g : A \u2192 B} \u2192 f \u2257 g \u2192 F f \u2261 F g)\n (F\u03c6* : \u2200 {k} \u2192 F \u03c6 \u2261 F (_*_ k \u2218 \u03c6))\n where\n\n +-stable : \u2200 {k} \u2192 F \u03c6 \u2261 F (\u03c6 \u2218 _+_ k)\n +-stable {k} =\n F \u03c6 \u2261\u27e8 F\u03c6* \u27e9\n F (_*_ (\u03c6 k) \u2218 \u03c6) \u2261\u27e8 F= (\u03bb x \u2192 ! \u03c6-+-*) \u27e9\n F (\u03c6 \u2218 _+_ k) \u220e\n\nmodule Stability\n {a}{A : Type a}\n {b}{B : Type b}\n (G+ : Group A)\n (G* : Group B)\n (\u03c6 : A \u2192 B)\n (\u03c6-hom : GroupHomomorphism G+ G* \u03c6)\n where\n open Additive-Group G+\n open Multiplicative-Group G*\n open GroupHomomorphism \u03c6-hom\n\n open Stability-Minimal \u03c6 _+_ _*_ hom public\n\nopen GroupHomomorphism\n\nmodule Identity\n {a}{A : Type a}\n (\ud835\udd38 : Group A)\n where\n\n id-hom : GroupHomomorphism \ud835\udd38 \ud835\udd38 id\n id-hom = mk refl\n\nmodule Compose\n {a}{A : Type a}\n {b}{B : Type b}\n {c}{C : Type c}\n (\ud835\udd38 : Group A)\n (\ud835\udd39 : Group B)\n (\u2102 : Group C)\n (\u03c8 : A \u2192 B)\n (\u03c8-hom : GroupHomomorphism \ud835\udd38 \ud835\udd39 \u03c8)\n (\u03c6 : B \u2192 C)\n (\u03c6-hom : GroupHomomorphism \ud835\udd39 \u2102 \u03c6)\n where\n\n \u2218-hom : GroupHomomorphism \ud835\udd38 \u2102 (\u03c6 \u2218 \u03c8)\n \u2218-hom = mk (ap \u03c6 (hom \u03c8-hom) \u2219 hom \u03c6-hom)\n\nmodule Delta\n {a}{A : Type a}\n (\ud835\udd38 : Group A)\n where\n open Algebra.Group.Constructions.Product\n\n \u0394-hom : GroupHomomorphism \ud835\udd38 (\u00d7-grp \ud835\udd38 \ud835\udd38) (\u03bb x \u2192 x , x)\n \u0394-hom = mk refl\n\nmodule Zip\n {a\u2080}{A\u2080 : Type a\u2080}\n {a\u2081}{A\u2081 : Type a\u2081}\n {b\u2080}{B\u2080 : Type b\u2080}\n {b\u2081}{B\u2081 : Type b\u2081}\n (\ud835\udd38\u2080 : Group A\u2080)\n (\ud835\udd38\u2081 : Group A\u2081)\n (\ud835\udd39\u2080 : Group B\u2080)\n (\ud835\udd39\u2081 : Group B\u2081)\n (\u03c6\u2080 : A\u2080 \u2192 B\u2080)\n (\u03c6\u2080-hom : GroupHomomorphism \ud835\udd38\u2080 \ud835\udd39\u2080 \u03c6\u2080)\n (\u03c6\u2081 : A\u2081 \u2192 B\u2081)\n (\u03c6\u2081-hom : GroupHomomorphism \ud835\udd38\u2081 \ud835\udd39\u2081 \u03c6\u2081)\n where\n open Algebra.Group.Constructions.Product\n\n zip-hom : GroupHomomorphism (\u00d7-grp \ud835\udd38\u2080 \ud835\udd38\u2081) (\u00d7-grp \ud835\udd39\u2080 \ud835\udd39\u2081) (map \u03c6\u2080 \u03c6\u2081)\n zip-hom = mk (ap\u2082 _,_ (hom \u03c6\u2080-hom) (hom \u03c6\u2081-hom))\n\nmodule Pair\n {a}{A : Type a}\n {b\u2080}{B\u2080 : Type b\u2080}\n {b\u2081}{B\u2081 : Type b\u2081}\n (\ud835\udd38 : Group A)\n (\ud835\udd39\u2080 : Group B\u2080)\n (\ud835\udd39\u2081 : Group B\u2081)\n (\u03c6\u2080 : A \u2192 B\u2080)\n (\u03c6\u2080-hom : GroupHomomorphism \ud835\udd38 \ud835\udd39\u2080 \u03c6\u2080)\n (\u03c6\u2081 : A \u2192 B\u2081)\n (\u03c6\u2081-hom : GroupHomomorphism \ud835\udd38 \ud835\udd39\u2081 \u03c6\u2081)\n where\n\n -- pair = zip \u2218 \u0394\n pair-hom : GroupHomomorphism \ud835\udd38 (Product.\u00d7-grp \ud835\udd39\u2080 \ud835\udd39\u2081) < \u03c6\u2080 , \u03c6\u2081 >\n pair-hom = Compose.\u2218-hom _ _ _\n _ (Delta.\u0394-hom \ud835\udd38)\n _ (Zip.zip-hom _ _ _ _ _ \u03c6\u2080-hom _ \u03c6\u2081-hom)\n -- OR:\n pair-hom = mk (ap\u2082 _,_ (hom \u03c6\u2080-hom) (hom \u03c6\u2081-hom))\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"48e8cfa77c1c8a68a6beee0c3263161084c49025","subject":"Bits: lift the xor group structure to vectors","message":"Bits: lift the xor group structure to vectors\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule Data.Bits where\n\nopen import Algebra\nopen import Level.NP\nopen import Type hiding (\u2605)\nopen import Data.Nat.NP hiding (_==_) renaming (_<=_ to _\u2115<=_)\nopen import Data.Bit using (Bit)\nopen import Data.Two renaming (_==_ to _==\u1d47_)\nopen import Data.Fin.NP using (Fin; zero; suc; inject\u2081; inject+; raise; Fin\u25b9\u2115)\nopen import Data.Vec.NP\nopen import Function.NP\nimport Data.List.NP as L\n\n-- Re-export some vector functions, maybe they should be given\n-- less generic types.\nopen Data.Vec.NP public using ([]; _\u2237_; _++_; head; tail; map; replicate; RewireTbl; rewire; rewireTbl; on\u1d62)\n\nBits : \u2115 \u2192 \u2605\u2080\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 \u2605\u2080\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0\u2082\n\n-- Notice that all empty vectors are the same, hence 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1\u2082\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0\u2082 \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1\u2082 \u2237 xs\n\n_!_ : \u2200 {a n} {A : \u2605 a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n-- see Data.Bits.Properties\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bit\n[] == [] = 1\u2082\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n_<=_ : \u2200 {n} (xs ys : Bits n) \u2192 Bit\n[] <= [] = 1\u2082\n(1\u2082 \u2237 xs) <= (0\u2082 \u2237 ys) = 0\u2082\n(0\u2082 \u2237 xs) <= (1\u2082 \u2237 ys) = 1\u2082\n(_ \u2237 xs) <= (_ \u2237 ys) = xs <= ys\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n\u2295-group : \u2115 \u2192 Group \u2080 \u2080\n\u2295-group = LiftGroup.group Xor\u00b0.+-group\n\nmodule \u2295-Group (n : \u2115) = Group (\u2295-group n)\nmodule \u2295-Monoid (n : \u2115) = Monoid (\u2295-Group.monoid n)\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 k {n} \u2192 Bits (n + k) \u2192 Bits k\nlsb _ {n} = drop n\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0\u2082 \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1\u2082 \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 map not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0\u2082 \u2237 1\u2082 \u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = map 0\u2237_ bs ++ map 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1\u2082\n\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0\u2082\n\n_\u2228\u00b0_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_\u2228\u00b0_ f g x = f x \u2228 g x\n\n_\u2227\u00b0_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_\u2227\u00b0_ f g x = f x \u2227 g x\n\nnot\u00b0 : \u2200 {n} (f : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\nnot\u00b0 f = not \u2218 f\n\nview\u2237 : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b} \u2192 (A \u2192 Vec A n \u2192 B) \u2192 Vec A (suc n) \u2192 B\nview\u2237 f (x \u2237 xs) = f x xs\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0\u2082 \u2237 []\nsucBCarry (0\u2082 \u2237 xs) = 0\u2082 \u2237 sucBCarry xs\nsucBCarry (1\u2082 \u2237 xs) = view\u2237 (\u03bb x xs \u2192 x \u2237 not x \u2237 xs) (sucBCarry xs)\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n--_[mod_] : \u2115 \u2192 \u2115 \u2192 \u2605\u2080\n--a [mod b ] = DivMod' a b\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0\u2082 \u2237 xs) = 1\u2082 \u2237 xs\n sucRB (1\u2082 \u2237 xs) = 0\u2082 \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2^ n)\ntoFin [] = zero\ntoFin (0\u2082 \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1\u2082 \u2237 xs) = raise (2^ n) (toFin xs)\n\nBits\u25b9\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nBits\u25b9\u2115 [] = zero\nBits\u25b9\u2115 (0\u2082 \u2237 xs) = Bits\u25b9\u2115 xs\nBits\u25b9\u2115 {suc n} (1\u2082 \u2237 xs) = 2^ n + Bits\u25b9\u2115 xs\n\n\u2115\u25b9Bits : \u2200 {n} \u2192 \u2115 \u2192 Bits n\n\u2115\u25b9Bits {zero} _ = []\n\u2115\u25b9Bits {suc n} x = [0: 0\u2237 \u2115\u25b9Bits x\n 1: 1\u2237 \u2115\u25b9Bits (x \u2238 2^ n)\n ]\u2032 (2^ n \u2115<= x)\n\n\u2115\u25b9Bits\u2032 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\n\u2115\u25b9Bits\u2032 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2^ n) \u2192 Bits n\nfromFin = \u2115\u25b9Bits \u2218 Fin\u25b9\u2115\n\nlookupTbl : \u2200 {n a} {A : \u2605 a} \u2192 Bits n \u2192 Vec A (2^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0\u2082 \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1\u2082 \u2237 key) tbl = lookupTbl key (drop (2^ n) tbl)\n\nfunFromTbl : \u2200 {n a} {A : \u2605 a} \u2192 Vec A (2^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : \u2605 a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_)\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule Data.Bits where\n\nopen import Type hiding (\u2605)\nopen import Data.Nat.NP hiding (_==_) renaming (_<=_ to _\u2115<=_)\nopen import Data.Bit using (Bit)\nopen import Data.Two renaming (_==_ to _==\u1d47_)\nopen import Data.Fin.NP using (Fin; zero; suc; inject\u2081; inject+; raise; Fin\u25b9\u2115)\nopen import Data.Vec.NP\nopen import Function.NP\nimport Data.List.NP as L\n\n-- Re-export some vector functions, maybe they should be given\n-- less generic types.\nopen Data.Vec.NP public using ([]; _\u2237_; _++_; head; tail; map; replicate; RewireTbl; rewire; rewireTbl; on\u1d62)\n\nBits : \u2115 \u2192 \u2605\u2080\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 \u2605\u2080\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0\u2082\n\n-- Notice that all empty vectors are the same, hence 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1\u2082\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0\u2082 \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1\u2082 \u2237 xs\n\n_!_ : \u2200 {a n} {A : \u2605 a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n-- see Data.Bits.Properties\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bit\n[] == [] = 1\u2082\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n_<=_ : \u2200 {n} (xs ys : Bits n) \u2192 Bit\n[] <= [] = 1\u2082\n(1\u2082 \u2237 xs) <= (0\u2082 \u2237 ys) = 0\u2082\n(0\u2082 \u2237 xs) <= (1\u2082 \u2237 ys) = 1\u2082\n(_ \u2237 xs) <= (_ \u2237 ys) = xs <= ys\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 k {n} \u2192 Bits (n + k) \u2192 Bits k\nlsb _ {n} = drop n\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0\u2082 \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1\u2082 \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 map not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0\u2082 \u2237 1\u2082 \u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = map 0\u2237_ bs ++ map 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1\u2082\n\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0\u2082\n\n_\u2228\u00b0_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_\u2228\u00b0_ f g x = f x \u2228 g x\n\n_\u2227\u00b0_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_\u2227\u00b0_ f g x = f x \u2227 g x\n\nnot\u00b0 : \u2200 {n} (f : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\nnot\u00b0 f = not \u2218 f\n\nview\u2237 : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b} \u2192 (A \u2192 Vec A n \u2192 B) \u2192 Vec A (suc n) \u2192 B\nview\u2237 f (x \u2237 xs) = f x xs\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0\u2082 \u2237 []\nsucBCarry (0\u2082 \u2237 xs) = 0\u2082 \u2237 sucBCarry xs\nsucBCarry (1\u2082 \u2237 xs) = view\u2237 (\u03bb x xs \u2192 x \u2237 not x \u2237 xs) (sucBCarry xs)\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n--_[mod_] : \u2115 \u2192 \u2115 \u2192 \u2605\u2080\n--a [mod b ] = DivMod' a b\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0\u2082 \u2237 xs) = 1\u2082 \u2237 xs\n sucRB (1\u2082 \u2237 xs) = 0\u2082 \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2^ n)\ntoFin [] = zero\ntoFin (0\u2082 \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1\u2082 \u2237 xs) = raise (2^ n) (toFin xs)\n\nBits\u25b9\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nBits\u25b9\u2115 [] = zero\nBits\u25b9\u2115 (0\u2082 \u2237 xs) = Bits\u25b9\u2115 xs\nBits\u25b9\u2115 {suc n} (1\u2082 \u2237 xs) = 2^ n + Bits\u25b9\u2115 xs\n\n\u2115\u25b9Bits : \u2200 {n} \u2192 \u2115 \u2192 Bits n\n\u2115\u25b9Bits {zero} _ = []\n\u2115\u25b9Bits {suc n} x = [0: 0\u2237 \u2115\u25b9Bits x\n 1: 1\u2237 \u2115\u25b9Bits (x \u2238 2^ n)\n ]\u2032 (2^ n \u2115<= x)\n\n\u2115\u25b9Bits\u2032 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\n\u2115\u25b9Bits\u2032 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2^ n) \u2192 Bits n\nfromFin = \u2115\u25b9Bits \u2218 Fin\u25b9\u2115\n\nlookupTbl : \u2200 {n a} {A : \u2605 a} \u2192 Bits n \u2192 Vec A (2^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0\u2082 \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1\u2082 \u2237 key) tbl = lookupTbl key (drop (2^ n) tbl)\n\nfunFromTbl : \u2200 {n a} {A : \u2605 a} \u2192 Vec A (2^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : \u2605 a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"6b9e09214fc24fa6f8d233d537942f27cc0bb168","subject":"Added doc.","message":"Added doc.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Base.agda","new_file":"src\/fot\/FOTC\/Base.agda","new_contents":"------------------------------------------------------------------------------\n-- The first-order theory of combinators (FOTC) base\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n{-\nFOTC The logical framework (Agda)\n\n* Logical constants * Curry-Howard isomorphism\n* Equality * Identity type\n* Term language and conversion rules * Postulates\n* Inductively defined predicates * Inductive families\n* Co-inductively defined predicates * Greatest fixed-points\n-}\n\n-- References:\n--\n-- \u2022 Peter Aczel. The strength of Martin-L\u00f6f's intuitionistic type\n-- theory with one universe. In Miettinen and V\u00e4\u00e4nanen,\n-- editors. Proc. of the Symposium on Mathematical Logic (Oulu,\n-- 1974), Report No. 2, Department of Philosopy, University of\n-- Helsinki, Helsinki, 1977, pages 1\u201332.\n\nmodule FOTC.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfixl 9 _\u00b7_ -- The symbol is '\\cdot'.\ninfix 8 if_then_else_\n\n------------------------------------------------------------------------------\n-- First-order logic with equality.\nopen import Common.FOL.FOL-Eq public\n\n-- Common definitions.\nopen import Common.DefinitionsATP public\n\n------------------------------------------------------------------------------\n-- The term language of FOTC\n\n-- t ::= x | t \u00b7 t |\n-- | true | false | if\n-- | 0 | succ | pred | iszero\n-- | loop\n\npostulate\n _\u00b7_ : D \u2192 D \u2192 D -- FOTC application.\n true false if : D -- FOTC partial Booleans.\n zero succ pred iszero : D -- FOTC partial natural numbers.\n loop : D -- FOTC looping programs.\n\n------------------------------------------------------------------------------\n-- Definitions\n\n-- We define some function symbols for convenience in writing and\n-- looking for an optimization for the ATPs.\n\n-- 2012-03-20. The definitions are inside an abstract block because\n-- the conversion rules (see below) are based on them, so want to\n-- avoid their expansion.\n\nabstract\n if_then_else_ : D \u2192 D \u2192 D \u2192 D\n if b then d\u2081 else d\u2082 = if \u00b7 b \u00b7 d\u2081 \u00b7 d\u2082\n -- {-# ATP definition if_then_else_ #-}\n\n succ\u2081 : D \u2192 D\n succ\u2081 n = succ \u00b7 n\n -- {-# ATP definition succ\u2081 #-}\n\n pred\u2081 : D \u2192 D\n pred\u2081 n = pred \u00b7 n\n -- {-# ATP definition pred\u2081 #-}\n\n iszero\u2081 : D \u2192 D\n iszero\u2081 n = iszero \u00b7 n\n -- {-# ATP definition iszero\u2081 #-}\n\n------------------------------------------------------------------------------\n-- Conversion rules\n\n-- The conversion relation _conv_ satifies (Aczel 1977, p. 8)\n--\n-- x conv y <=> FOTC \u22a2 x \u2261 y,\n--\n-- therefore, we introduce the conversion rules as non-logical axioms.\n\n-- N.B. Looking for an optimization for the ATPs, we write the\n-- conversion rules on the defined function symbols instead of on the\n-- term constants.\n\n-- Conversion rules for Booleans.\n-- if-true : \u2200 d\u2081 {d\u2082} \u2192 if \u00b7 true \u00b7 d\u2081 \u00b7 d\u2082 \u2261 d\u2081\n-- if-false : \u2200 {d\u2081} d\u2082 \u2192 if \u00b7 false \u00b7 d\u2081 \u00b7 d\u2082 \u2261 d\u2082\npostulate\n if-true : \u2200 d\u2081 {d\u2082} \u2192 if true then d\u2081 else d\u2082 \u2261 d\u2081\n if-false : \u2200 {d\u2081} d\u2082 \u2192 if false then d\u2081 else d\u2082 \u2261 d\u2082\n{-# ATP axiom if-true if-false #-}\n\n-- Conversion rules for pred.\n-- N.B. We don't need this equation.\n-- pred-0 : pred \u00b7 zero \u2261 zero\n-- pred-S : \u2200 n \u2192 pred \u00b7 (succ \u00b7 n) \u2261 n\npostulate\n pred-S : \u2200 n \u2192 pred\u2081 (succ\u2081 n) \u2261 n\n{-# ATP axiom pred-S #-}\n\n-- Conversion rules for iszero.\n-- iszero-0 : iszero \u00b7 zero \u2261 true\n-- iszero-S : \u2200 n \u2192 iszero \u00b7 (succ \u00b7 n) \u2261 false\npostulate\n iszero-0 : iszero\u2081 zero \u2261 true\n iszero-S : \u2200 n \u2192 iszero\u2081 (succ\u2081 n) \u2261 false\n{-# ATP axiom iszero-0 iszero-S #-}\n\n-- Conversion rule for loop.\n--\n-- The equation loop-eq adds anything to the logic (because\n-- reflexivity is already an axiom of equality), therefore we won't\n-- add this equation as a first-order logic axiom.\npostulate loop-eq : loop \u2261 loop\n\n------------------------------------------------------------------------------\n-- Discrimination rules\n\n-- 0\u2262S : \u2200 {n} \u2192 zero \u2262 succ \u00b7 n\npostulate\n true\u2262false : true \u2262 false\n 0\u2262S : \u2200 {n} \u2192 zero \u2262 succ\u2081 n\n{-# ATP axiom true\u2262false 0\u2262S #-}\n\n------------------------------------------------------------------------------\n-- FOTC combinators for lists, colists, streams, etc.\n\nmodule BList where\n\n -- We add 3 to the fixities of the standard library.\n infixr 8 _\u2237_\n\n -- List constants.\n postulate\n [] cons head tail null : D -- FOTC lists.\n\n -- Definitions\n abstract\n _\u2237_ : D \u2192 D \u2192 D\n x \u2237 xs = cons \u00b7 x \u00b7 xs\n -- {-# ATP definition _\u2237_ #-}\n\n head\u2081 : D \u2192 D\n head\u2081 xs = head \u00b7 xs\n -- {-# ATP definition head\u2081 #-}\n\n tail\u2081 : D \u2192 D\n tail\u2081 xs = tail \u00b7 xs\n -- {-# ATP definition tail\u2081 #-}\n\n null\u2081 : D \u2192 D\n null\u2081 xs = null \u00b7 xs\n -- {-# ATP definition null\u2081 #-}\n\n -- Conversion rules for null.\n -- null-[] : null \u00b7 nil \u2261 true\n -- null-\u2237 : \u2200 x xs \u2192 null \u00b7 (cons \u00b7 x \u00b7 xs) \u2261 false\n postulate\n null-[] : null\u2081 [] \u2261 true\n null-\u2237 : \u2200 x xs \u2192 null\u2081 (x \u2237 xs) \u2261 false\n\n -- Conversion rule for head.\n -- head-\u2237 : \u2200 x xs \u2192 head \u00b7 (cons \u00b7 x \u00b7 xs) \u2261 x\n postulate head-\u2237 : \u2200 x xs \u2192 head\u2081 (x \u2237 xs) \u2261 x\n {-# ATP axiom head-\u2237 #-}\n\n -- Conversion rule for tail.\n -- tail-\u2237 : \u2200 x xs \u2192 tail \u00b7 (cons \u00b7 x \u00b7 xs) \u2261 xs\n postulate tail-\u2237 : \u2200 x xs \u2192 tail\u2081 (x \u2237 xs) \u2261 xs\n {-# ATP axiom tail-\u2237 #-}\n\n -- Discrimination rules\n -- postulate []\u2262cons : \u2200 {x xs} \u2192 [] \u2262 cons \u00b7 x \u00b7 xs\n postulate []\u2262cons : \u2200 {x xs} \u2192 [] \u2262 x \u2237 xs\n","old_contents":"------------------------------------------------------------------------------\n-- The first-order theory of combinators (FOTC) base\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n{-\nFOTC The logical framework (Agda)\n\n* Logical constants * Curry-Howard isomorphism\n* Equality * Identity type\n* Term language and conversion rules * Postulates\n* Inductively defined predicates * Inductive families\n* Co-inductively defined predicates * Greatest fixed-points\n-}\n\n-- References:\n--\n-- \u2022 Peter Aczel. The strength of Martin-L\u00f6f's intuitionistic type\n-- theory with one universe. In Miettinen and V\u00e4\u00e4nanen,\n-- editors. Proc. of the Symposium on Mathematical Logic (Oulu,\n-- 1974), Report No. 2, Department of Philosopy, University of\n-- Helsinki, Helsinki, 1977, pages 1\u201332.\n\nmodule FOTC.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfixl 9 _\u00b7_ -- The symbol is '\\cdot'.\ninfix 8 if_then_else_\n\n------------------------------------------------------------------------------\n-- First-order logic with equality.\nopen import Common.FOL.FOL-Eq public\n\n-- Common definitions.\nopen import Common.DefinitionsATP public\n\n------------------------------------------------------------------------------\n-- The term language of FOTC\n\n-- t ::= x | t \u00b7 t |\n-- | true | false | if\n-- | 0 | succ | pred | iszero\n-- | loop\n\npostulate\n _\u00b7_ : D \u2192 D \u2192 D -- FOTC application.\n true false if : D -- FOTC partial Booleans.\n zero succ pred iszero : D -- FOTC partial natural numbers.\n loop : D -- FOTC looping programs.\n\n------------------------------------------------------------------------------\n-- Definitions\n\n-- We define some function symbols for convenience in writing and\n-- looking for an optimization for the ATPs.\n\n-- 2012-03-20. The definitions are inside an abstract block because\n-- the conversion rules (see below) are based on them, so want to\n-- avoid their expansion.\n\nabstract\n if_then_else_ : D \u2192 D \u2192 D \u2192 D\n if b then d\u2081 else d\u2082 = if \u00b7 b \u00b7 d\u2081 \u00b7 d\u2082\n -- {-# ATP definition if_then_else_ #-}\n\n succ\u2081 : D \u2192 D\n succ\u2081 n = succ \u00b7 n\n -- {-# ATP definition succ\u2081 #-}\n\n pred\u2081 : D \u2192 D\n pred\u2081 n = pred \u00b7 n\n -- {-# ATP definition pred\u2081 #-}\n\n iszero\u2081 : D \u2192 D\n iszero\u2081 n = iszero \u00b7 n\n -- {-# ATP definition iszero\u2081 #-}\n\n------------------------------------------------------------------------------\n-- Conversion rules\n\n-- The conversion relation _conv_ satifies (Aczel 1977, p. 8)\n--\n-- x conv y <=> FOTC \u22a2 x \u2261 y,\n--\n-- therefore, we introduce the conversion rules as non-logical axioms.\n\n-- N.B. Looking for an optimization for the ATPs, we write the\n-- conversion rules on the defined function symbols instead of on the\n-- term constants.\n\n-- Conversion rules for Booleans.\npostulate\n -- if-true : \u2200 d\u2081 {d\u2082} \u2192 if \u00b7 true \u00b7 d\u2081 \u00b7 d\u2082 \u2261 d\u2081\n -- if-false : \u2200 {d\u2081} d\u2082 \u2192 if \u00b7 false \u00b7 d\u2081 \u00b7 d\u2082 \u2261 d\u2082\n if-true : \u2200 d\u2081 {d\u2082} \u2192 if true then d\u2081 else d\u2082 \u2261 d\u2081\n if-false : \u2200 {d\u2081} d\u2082 \u2192 if false then d\u2081 else d\u2082 \u2261 d\u2082\n{-# ATP axiom if-true if-false #-}\n\n-- Conversion rules for pred.\npostulate\n -- N.B. We don't need this equation.\n -- pred-0 : pred \u00b7 zero \u2261 zero\n -- pred-S : \u2200 n \u2192 pred \u00b7 (succ \u00b7 n) \u2261 n\n pred-S : \u2200 n \u2192 pred\u2081 (succ\u2081 n) \u2261 n\n{-# ATP axiom pred-S #-}\n\n-- Conversion rules for iszero.\npostulate\n -- iszero-0 : iszero \u00b7 zero \u2261 true\n -- iszero-S : \u2200 n \u2192 iszero \u00b7 (succ \u00b7 n) \u2261 false\n iszero-0 : iszero\u2081 zero \u2261 true\n iszero-S : \u2200 n \u2192 iszero\u2081 (succ\u2081 n) \u2261 false\n{-# ATP axiom iszero-0 iszero-S #-}\n\n-- Conversion rule for loop.\n--\n-- The equation loop-eq adds anything to the logic (because\n-- reflexivity is already an axiom of equality), therefore we won't\n-- add this equation as a first-order logic axiom.\npostulate loop-eq : loop \u2261 loop\n\n------------------------------------------------------------------------------\n-- Discrimination rules\n\npostulate\n true\u2262false : true \u2262 false\n-- 0\u2262S : \u2200 {n} \u2192 zero \u2262 succ \u00b7 n\n 0\u2262S : \u2200 {n} \u2192 zero \u2262 succ\u2081 n\n{-# ATP axiom true\u2262false 0\u2262S #-}\n\n------------------------------------------------------------------------------\n-- FOTC combinators for lists, colists, streams, etc.\n\nmodule BList where\n\n -- We add 3 to the fixities of the standard library.\n infixr 8 _\u2237_\n\n -- List constants.\n postulate\n [] cons head tail null : D -- FOTC lists.\n\n -- Definitions\n abstract\n _\u2237_ : D \u2192 D \u2192 D\n x \u2237 xs = cons \u00b7 x \u00b7 xs\n -- {-# ATP definition _\u2237_ #-}\n\n head\u2081 : D \u2192 D\n head\u2081 xs = head \u00b7 xs\n -- {-# ATP definition head\u2081 #-}\n\n tail\u2081 : D \u2192 D\n tail\u2081 xs = tail \u00b7 xs\n -- {-# ATP definition tail\u2081 #-}\n\n null\u2081 : D \u2192 D\n null\u2081 xs = null \u00b7 xs\n -- {-# ATP definition null\u2081 #-}\n\n -- Conversion rules for null.\n postulate\n null-[] : null\u2081 [] \u2261 true\n null-\u2237 : \u2200 x xs \u2192 null\u2081 (x \u2237 xs) \u2261 false\n\n -- Conversion rule for head.\n postulate head-\u2237 : \u2200 x xs \u2192 head\u2081 (x \u2237 xs) \u2261 x\n {-# ATP axiom head-\u2237 #-}\n\n -- Conversion rule for tail.\n postulate tail-\u2237 : \u2200 x xs \u2192 tail\u2081 (x \u2237 xs) \u2261 xs\n {-# ATP axiom tail-\u2237 #-}\n\n -- Discrimination rules\n postulate []\u2262cons : \u2200 {x xs} \u2192 [] \u2262 x \u2237 xs\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"cb42e120fdc89dd9482591952c126c8f09c37f02","subject":"formal paramater madness","message":"formal paramater madness\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"expandability.agda","new_file":"expandability.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import htype-decidable\nopen import lemmas-matching\nopen import disjointness\n\nmodule expandability where\n mutual\n expandability-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ]\n (\u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394)\n expandability-synth SConst = _ , _ , ESConst\n expandability-synth (SAsc {\u03c4 = \u03c4} wt)\n with expandability-ana wt\n ... | _ , _ , \u03c4' , D = _ , _ , ESAsc D\n expandability-synth (SVar x) = _ , _ , ESVar x\n expandability-synth (SAp dis wt1 m wt2)\n with expandability-ana (ASubsume wt1 (match-consist m)) | expandability-ana wt2\n ... | _ , _ , _ , D1 | _ , _ , _ , D2 = _ , _ , ESAp dis (expand-ana-disjoint dis D1 D2) wt1 m D1 D2\n expandability-synth SEHole = _ , _ , ESEHole\n expandability-synth (SNEHole new wt)\n with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESNEHole (expand-new-disjoint-synth new wt') wt'\n expandability-synth (SLam x\u2081 wt)\n with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESLam x\u2081 wt'\n\n expandability-ana : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ] \u03a3[ \u03c4' \u2208 htyp ]\n (\u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394)\n expandability-ana {e = e} (ASubsume D x\u2081)\n with expandability-synth D\n -- these cases just pass through, but we need to pattern match so we can prove things aren't holes\n expandability-ana {e = c} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = e \u00b7: x} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = X x} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = \u00b7\u03bb x e} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = \u00b7\u03bb x [ x\u2081 ] e} (ASubsume D x\u2082) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2082\n expandability-ana {e = e1 \u2218 e2} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n -- the two holes are special-cased\n expandability-ana {e = \u2987\u2988[ x ]} (ASubsume _ _ ) | _ , _ , _ = _ , _ , _ , EAEHole\n expandability-ana {\u0393} {\u2987 e \u2988[ x ]} (ASubsume (SNEHole new wt) x\u2082) | _ , _ , ESNEHole x\u2081 D' with expandability-synth wt\n ... | w , y , z = _ , _ , _ , EANEHole (expand-new-disjoint-synth new z) z\n -- the lambda cases\n expandability-ana (ALam x\u2081 m wt)\n with expandability-ana wt\n ... | _ , _ , _ , D' = _ , _ , _ , EALam x\u2081 m D'\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import htype-decidable\nopen import lemmas-matching\nopen import disjointness\n\nmodule expandability where\n mutual\n expandability-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ]\n (\u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394)\n expandability-synth SConst = _ , _ , ESConst\n expandability-synth (SAsc {\u03c4 = \u03c4} wt)\n with expandability-ana wt\n ... | _ , _ , \u03c4' , D = _ , _ , ESAsc D\n expandability-synth (SVar {n = n} x) = _ , _ , ESVar x\n expandability-synth (SAp dis wt1 m wt2)\n with expandability-ana (ASubsume wt1 (match-consist m)) | expandability-ana wt2\n ... | _ , _ , _ , D1 | _ , _ , _ , D2 = _ , _ , ESAp dis (expand-ana-disjoint dis D1 D2) wt1 m D1 D2\n expandability-synth SEHole = _ , _ , ESEHole\n expandability-synth (SNEHole new wt)\n with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESNEHole (expand-new-disjoint-synth new wt') wt'\n expandability-synth (SLam x\u2081 wt)\n with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESLam x\u2081 wt'\n\n expandability-ana : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ] \u03a3[ \u03c4' \u2208 htyp ]\n (\u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394)\n expandability-ana {e = e} (ASubsume D x\u2081)\n with expandability-synth D\n -- these cases just pass through, but we need to pattern match so we can prove things aren't holes\n expandability-ana {e = c} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = e \u00b7: x} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = X x} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = \u00b7\u03bb x e} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = \u00b7\u03bb x [ x\u2081 ] e} (ASubsume D x\u2082) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2082\n expandability-ana {e = e1 \u2218 e2} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n -- the two holes are special-cased\n expandability-ana {e = \u2987\u2988[ x ]} (ASubsume _ _ ) | _ , _ , _ = _ , _ , _ , EAEHole\n expandability-ana {\u0393} {\u2987 e \u2988[ x ]} (ASubsume (SNEHole new wt) x\u2082) | _ , _ , ESNEHole x\u2081 D' with expandability-synth wt\n ... | w , y , z = _ , _ , _ , EANEHole (expand-new-disjoint-synth new z) z\n -- the lambda cases\n expandability-ana (ALam x\u2081 m wt)\n with expandability-ana wt\n ... | _ , _ , _ , D' = _ , _ , _ , EALam x\u2081 m D'\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"2691816104bfb57b0b4e5094a85c36377cf2cc33","subject":"now uses equational reasoning","message":"now uses equational reasoning\n","repos":"piyush-kurur\/sample-code","old_file":"agda\/Nat.agda","new_file":"agda\/Nat.agda","new_contents":"module Nat where\n\ndata \u2115 : Set where\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero + b = b\nsucc a + b = succ (a + b)\n\n_\u00d7_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero \u00d7 b = zero\nsucc a \u00d7 b = (a \u00d7 b) + b\n\n\nopen import Relation.Binary.PropositionalEquality\n\n0-is-right-identity-of-+ : \u2200 (n : \u2115) \u2192 n + zero \u2261 n\n0-is-right-identity-of-+ zero = refl\n0-is-right-identity-of-+ (succ n) = cong succ (0-is-right-identity-of-+ n)\n\n\n+-is-associative : \u2200 (a b c : \u2115) \u2192 a + (b + c) \u2261 (a + b) + c\n+-is-associative zero b c = refl\n+-is-associative (succ a) b c = cong succ (+-is-associative a b c)\n\n\nlemma : \u2200 (a b : \u2115) \u2192 a + succ b \u2261 succ (a + b)\nlemma zero b = refl\nlemma (succ a) b = cong succ (lemma a b)\n\nimport Relation.Binary.EqReasoning as EqR\nopen module EqNat = EqR (setoid \u2115)\n\n+-is-commutative : \u2200 (a b : \u2115) \u2192 a + b \u2261 b + a\n+-is-commutative a zero = 0-is-right-identity-of-+ a\n+-is-commutative a (succ b) =\n begin\n a + succ b\n \u2248\u27e8 lemma a b \u27e9\n succ (a + b)\n \u2248\u27e8 cong succ (+-is-commutative a b) \u27e9\n succ (b + a)\n \u2248\u27e8 refl \u27e9\n succ b + a\n \u220e\n","old_contents":"module Nat where\n\ndata \u2115 : Set where\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero + b = b\nsucc a + b = succ (a + b)\n\n_\u00d7_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero \u00d7 b = zero\nsucc a \u00d7 b = (a \u00d7 b) + b\n\n\nopen import Relation.Binary.PropositionalEquality\n\n0-is-right-identity-of-+ : \u2200 (n : \u2115) \u2192 n + zero \u2261 n\n0-is-right-identity-of-+ zero = refl\n0-is-right-identity-of-+ (succ n) = cong succ (0-is-right-identity-of-+ n)\n\n\n+-is-associative : \u2200 (a b c : \u2115) \u2192 a + (b + c) \u2261 (a + b) + c\n+-is-associative zero b c = refl\n+-is-associative (succ a) b c = cong succ (+-is-associative a b c)\n\n\nlemma : \u2200 (a b : \u2115) \u2192 a + succ b \u2261 succ (a + b)\nlemma zero b = refl\nlemma (succ a) b = cong succ (lemma a b)\n\n+-is-commutative : \u2200 (a b : \u2115) \u2192 a + b \u2261 b + a\n+-is-commutative a zero = 0-is-right-identity-of-+ a\n+-is-commutative a (succ b)\n = trans (lemma a b) (cong succ (+-is-commutative a b))\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"e9b6ce771b34dca21826aef620fdab37447e6d80","subject":"Renamed one function.","message":"Renamed one function.\n\nIgnore-this: 9c78acc0d4b0f6406274ce4cd5708f45\n\ndarcs-hash:20110402162531-3bd4e-972702749229997e6b55c5c45134d470e56c6257.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/FOTC\/Data\/Bool\/AndTotality.agda","new_file":"Draft\/FOTC\/Data\/Bool\/AndTotality.agda","new_contents":"------------------------------------------------------------------------------\n-- Testing\n------------------------------------------------------------------------------\n\nmodule Draft.FOTC.Data.Bool.AndTotality where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Bool.Type\n\n------------------------------------------------------------------------------\n\npostulate\n thm : \u2200 {b}(P : D \u2192 Set) \u2192 (Bool b \u2227 P true \u2227 P false) \u2192 P b\n-- The ATPs couldn't prove this postulate.\n-- {-# ATP prove thm #-}\n\npostulate\n thm\u2081 : \u2200 {P : D \u2192 Set}{x y z} \u2192 Bool x \u2192 P y \u2192 P z \u2192 P (if x then y else z)\n-- The ATPs couldn't prove this postulate.\n-- {-# ATP prove thm\u2081 #-}\n\n-- Typing of the if-then-else.\nif-T : \u2200 (P : D \u2192 Set){x y z} \u2192 Bool x \u2192 P y \u2192 P z \u2192\n P (if x then y else z)\nif-T P {y = y} tB Py Pz = subst P (sym (if-true y)) Py\nif-T P {z = z} fB Py Pz = subst P (sym (if-false z)) Pz\n\n_&&_ : D \u2192 D \u2192 D\nx && y = if x then y else false\n{-# ATP definition _&&_ #-}\n\npostulate\n &&-Bool : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n{-# ATP prove &&-Bool if-T #-}\n\n&&-Bool\u2081 : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n&&-Bool\u2081 {y = y} tB By = prf\n where\n postulate prf : Bool (true && y)\n {-# ATP prove prf if-T #-}\n&&-Bool\u2081 {y = y} fB By = prf\n where\n postulate prf : Bool (false && y)\n {-# ATP prove prf if-T #-}\n\n&&-Bool\u2082 : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n&&-Bool\u2082 tB By = if-T Bool tB By fB\n&&-Bool\u2082 fB By = if-T Bool fB By fB\n","old_contents":"------------------------------------------------------------------------------\n-- Testing\n------------------------------------------------------------------------------\n\nmodule Draft.FOTC.Data.Bool.AndTotality where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Bool.Type\n\n------------------------------------------------------------------------------\n\npostulate\n thm : \u2200 {b}(P : D \u2192 Set) \u2192 (Bool b \u2227 P true \u2227 P false) \u2192 P b\n-- The ATPs couldn't prove this postulate.\n-- {-# ATP prove thm #-}\n\npostulate\n thm\u2081 : \u2200 {P : D \u2192 Set}{x y z} \u2192 Bool x \u2192 P y \u2192 P z \u2192 P (if x then y else z)\n-- The ATPs couldn't prove this postulate.\n-- {-# ATP prove thm\u2081 #-}\n\nBool-elim : \u2200 (P : D \u2192 Set){x y z} \u2192 Bool x \u2192 P y \u2192 P z \u2192\n P (if x then y else z)\nBool-elim P {y = y} tB Py Pz = subst P (sym (if-true y)) Py\nBool-elim P {z = z} fB Py Pz = subst P (sym (if-false z)) Pz\n\n_&&_ : D \u2192 D \u2192 D\nx && y = if x then y else false\n{-# ATP definition _&&_ #-}\n\npostulate\n &&-Bool : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n{-# ATP prove &&-Bool Bool-elim #-}\n\n&&-Bool\u2081 : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n&&-Bool\u2081 {y = y} tB By = prf\n where\n postulate prf : Bool (true && y)\n {-# ATP prove prf Bool-elim #-}\n&&-Bool\u2081 {y = y} fB By = prf\n where\n postulate prf : Bool (false && y)\n {-# ATP prove prf Bool-elim #-}\n\n&&-Bool\u2082 : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n&&-Bool\u2082 tB By = Bool-elim Bool tB By fB\n&&-Bool\u2082 fB By = Bool-elim Bool fB By fB\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"eb62ebad0cbcd277118fcfd907c7981c71725bdc","subject":"Minor changes.","message":"Minor changes.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Program\/McCarthy91\/PropertiesATP.agda","new_file":"src\/fot\/FOTC\/Program\/McCarthy91\/PropertiesATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Main properties of the McCarthy 91 function\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- The main properties proved of the McCarthy 91 function (called\n-- mc91) are\n\n-- 1. The function always terminates.\n-- 2. For all n, n < mc91 n + 11.\n-- 3. For all n > 100, then mc91 n = n - 10.\n-- 4. For all n <= 100, then mc91 n = 91.\n\n-- N.B This module does not contain combined proofs, but it imports\n-- modules which contain combined proofs.\n\nmodule FOTC.Program.McCarthy91.PropertiesATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers.TotalityATP\nopen import FOTC.Program.McCarthy91.ArithmeticATP\nopen import FOTC.Program.McCarthy91.AuxiliaryPropertiesATP\nopen import FOTC.Program.McCarthy91.McCarthy91\nopen import FOTC.Program.McCarthy91.WF-Relation\nopen import FOTC.Program.McCarthy91.WF-Relation.LT2WF-RelationATP\nopen import FOTC.Program.McCarthy91.WF-Relation.Induction.Acc.WF-ATP\n\n------------------------------------------------------------------------------\n\nmc91-res : \u2200 {n} \u2192 N n \u2192 (n > [100] \u2227 mc91 n \u2261 n \u2238 [10]) \u2228\n (n \u226f [100] \u2227 mc91 n \u2261 [91])\nmc91-res = \u226a-wfind A h\n where\n A : D \u2192 Set\n A d = (d > [100] \u2227 mc91 d \u2261 d \u2238 [10]) \u2228\n (d \u226f [100] \u2227 mc91 d \u2261 [91])\n\n h : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 k \u226a m \u2192 A k) \u2192 A m\n h {m} Nm f with x>y\u2228x\u226fy Nm 100-N\n ... | inj\u2081 m>100 = inj\u2081 ( m>100 , mc91-eq-aux m m>100 )\n ... | inj\u2082 m\u226f100 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 99-N m\u226f100\n ... | inj\u2082 m\u2261100 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-100 m\u2261100 )\n ... | inj\u2081 m\u226f99 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 98-N m\u226f99\n ... | inj\u2082 m\u226199 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-99 m\u226199 )\n ... | inj\u2081 m\u226f98 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 97-N m\u226f98\n ... | inj\u2082 m\u226198 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-98 m\u226198 )\n ... | inj\u2081 m\u226f97 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 96-N m\u226f97\n ... | inj\u2082 m\u226197 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-97 m\u226197 )\n ... | inj\u2081 m\u226f96 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 95-N m\u226f96\n ... | inj\u2082 m\u226196 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-96 m\u226196 )\n ... | inj\u2081 m\u226f95 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 94-N m\u226f95\n ... | inj\u2082 m\u226195 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-95 m\u226195 )\n ... | inj\u2081 m\u226f94 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 93-N m\u226f94\n ... | inj\u2082 m\u226194 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-94 m\u226194 )\n ... | inj\u2081 m\u226f93 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 92-N m\u226f93\n ... | inj\u2082 m\u226193 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-93 m\u226193 )\n ... | inj\u2081 m\u226f92 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 91-N m\u226f92\n ... | inj\u2082 m\u226192 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-92 m\u226192 )\n ... | inj\u2081 m\u226f91 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 90-N m\u226f91\n ... | inj\u2082 m\u226191 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-91 m\u226191 )\n ... | inj\u2081 m\u226f90 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 89-N m\u226f90\n ... | inj\u2082 m\u226190 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-90 m\u226190 )\n ... | inj\u2081 m\u226f89 = inj\u2082 ( m\u226f100 , mc91-res-m\u226f89 )\n where\n m\u226489 : m \u2264 [89]\n m\u226489 = x\u226fy\u2192x\u2264y Nm 89-N m\u226f89\n\n mc91-res-m+11 : mc91 (m + [11]) \u2261 [91]\n mc91-res-m+11 with f (x+11-N Nm) (<\u2192\u226a (x+11-N Nm) Nm m\u226f100 (x100 , _ ) = \u22a5-elim (x\u226489\u2192x+11>100\u2192\u22a5 Nm m\u226489 m+11>100)\n ... | inj\u2082 ( _ , res ) = res\n\n mc91-res-m\u226f89 : mc91 m \u2261 [91]\n mc91-res-m\u226f89 = mc91x-res\u226f100 m [91] m\u226f100 mc91-res-m+11 mc91-res-91\n\n------------------------------------------------------------------------------\n-- Main properties\n\n-- For all n > 100, then mc91 n = n - 10.\nmc91-res>100 : \u2200 {n} \u2192 N n \u2192 n > [100] \u2192 mc91 n \u2261 n \u2238 [10]\nmc91-res>100 Nn n>100 with mc91-res Nn\n... | inj\u2081 ( _ , res ) = res\n... | inj\u2082 ( n\u226f100 , _ ) = \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nn 100-N n>100\n (x\u226fy\u2192x\u2264y Nn 100-N n\u226f100))\n-- For all n <= 100, then mc91 n = 91.\nmc91-res\u226f100 : \u2200 {n} \u2192 N n \u2192 n \u226f [100] \u2192 mc91 n \u2261 [91]\nmc91-res\u226f100 Nn n\u226f100 with mc91-res Nn\n... | inj\u2081 ( n>100 , _ ) = \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nn 100-N n>100\n (x\u226fy\u2192x\u2264y Nn 100-N n\u226f100))\n... | inj\u2082 ( _ , res ) = res\n\n-- The function always terminates.\nmc91-N : \u2200 {n} \u2192 N n \u2192 N (mc91 n)\nmc91-N Nn with x>y\u2228x\u226fy Nn 100-N\n... | inj\u2081 n>100 = subst N (sym (mc91-res>100 Nn n>100)) (\u2238-N Nn 10-N)\n... | inj\u2082 n\u226e100 = subst N (sym (mc91-res\u226f100 Nn n\u226e100)) 91-N\n\n-- For all n, n < mc91 n + 11.\nmc91-N-ineq : \u2200 {n} \u2192 N n \u2192 n < mc91 n + [11]\nmc91-N-ineq = \u226a-wfind A h\n where\n A : D \u2192 Set\n A d = d < mc91 d + [11]\n\n h : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 k \u226a m \u2192 A k) \u2192 A m\n h {m} Nm f with x>y\u2228x\u226fy Nm 100-N\n ... | inj\u2081 m>100 = x100 Nm m>100\n ... | inj\u2082 m\u226f100 =\n let mc91-m+11-N : N (mc91 (m + [11]))\n mc91-m+11-N = mc91-N (+-N Nm 11-N)\n\n h\u2081 : A (m + [11])\n h\u2081 = f (x+11-N Nm) (<\u2192\u226a (x+11-N Nm) Nm m\u226f100 (x 100, then mc91 n = n - 10.\n-- 4. For all n <= 100, then mc91 n = 91.\n\n-- N.B This module does not contain combined proofs, but it imports\n-- modules which contain combined proofs.\n\nmodule FOTC.Program.McCarthy91.PropertiesATP where\n\nopen import Common.FOL.Relation.Binary.EqReasoning\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers.TotalityATP\nopen import FOTC.Program.McCarthy91.ArithmeticATP\nopen import FOTC.Program.McCarthy91.AuxiliaryPropertiesATP\nopen import FOTC.Program.McCarthy91.McCarthy91\nopen import FOTC.Program.McCarthy91.WF-Relation\nopen import FOTC.Program.McCarthy91.WF-Relation.LT2WF-RelationATP\nopen import FOTC.Program.McCarthy91.WF-Relation.Induction.Acc.WF-ATP\n\n------------------------------------------------------------------------------\n\nmc91-res : \u2200 {n} \u2192 N n \u2192 (n > [100] \u2227 mc91 n \u2261 n \u2238 [10]) \u2228\n (n \u226f [100] \u2227 mc91 n \u2261 [91])\nmc91-res = \u226a-wfind A h\n where\n A : D \u2192 Set\n A d = (d > [100] \u2227 mc91 d \u2261 d \u2238 [10]) \u2228\n (d \u226f [100] \u2227 mc91 d \u2261 [91])\n\n h : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 k \u226a m \u2192 A k) \u2192 A m\n h {m} Nm f with x>y\u2228x\u226fy Nm 100-N\n ... | inj\u2081 m>100 = inj\u2081 ( m>100 , mc91-eq-aux m m>100 )\n ... | inj\u2082 m\u226f100 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 99-N m\u226f100\n ... | inj\u2082 m\u2261100 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-100 m\u2261100 )\n ... | inj\u2081 m\u226f99 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 98-N m\u226f99\n ... | inj\u2082 m\u226199 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-99 m\u226199 )\n ... | inj\u2081 m\u226f98 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 97-N m\u226f98\n ... | inj\u2082 m\u226198 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-98 m\u226198 )\n ... | inj\u2081 m\u226f97 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 96-N m\u226f97\n ... | inj\u2082 m\u226197 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-97 m\u226197 )\n ... | inj\u2081 m\u226f96 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 95-N m\u226f96\n ... | inj\u2082 m\u226196 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-96 m\u226196 )\n ... | inj\u2081 m\u226f95 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 94-N m\u226f95\n ... | inj\u2082 m\u226195 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-95 m\u226195 )\n ... | inj\u2081 m\u226f94 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 93-N m\u226f94\n ... | inj\u2082 m\u226194 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-94 m\u226194 )\n ... | inj\u2081 m\u226f93 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 92-N m\u226f93\n ... | inj\u2082 m\u226193 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-93 m\u226193 )\n ... | inj\u2081 m\u226f92 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 91-N m\u226f92\n ... | inj\u2082 m\u226192 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-92 m\u226192 )\n ... | inj\u2081 m\u226f91 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 90-N m\u226f91\n ... | inj\u2082 m\u226191 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-91 m\u226191 )\n ... | inj\u2081 m\u226f90 with x\u226fSy\u2192x\u226fy\u2228x\u2261Sy Nm 89-N m\u226f90\n ... | inj\u2082 m\u226190 = inj\u2082 ( m\u226f100 , mc91-res-aux mc91-res-90 m\u226190 )\n ... | inj\u2081 m\u226f89 = inj\u2082 ( m\u226f100 , mc91-res-m\u226f89 )\n where\n m\u226489 : m \u2264 [89]\n m\u226489 = x\u226fy\u2192x\u2264y Nm 89-N m\u226f89\n\n mc91-res-m+11 : mc91 (m + [11]) \u2261 [91]\n mc91-res-m+11 with f (x+11-N Nm) (<\u2192\u226a (x+11-N Nm) Nm m\u226f100 (x100 , _ ) = \u22a5-elim (x\u226489\u2192x+11>100\u2192\u22a5 Nm m\u226489 m+11>100)\n ... | inj\u2082 ( _ , res ) = res\n\n mc91-res-m\u226f89 : mc91 m \u2261 [91]\n mc91-res-m\u226f89 = mc91x-res\u226f100 m [91] m\u226f100 mc91-res-m+11 mc91-res-91\n\n------------------------------------------------------------------------------\n-- Main properties\n\n-- For all n > 100, then mc91 n = n - 10.\nmc91-res>100 : \u2200 {n} \u2192 N n \u2192 n > [100] \u2192 mc91 n \u2261 n \u2238 [10]\nmc91-res>100 Nn n>100 with mc91-res Nn\n... | inj\u2081 ( _ , res ) = res\n... | inj\u2082 ( n\u226f100 , _ ) = \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nn 100-N n>100\n (x\u226fy\u2192x\u2264y Nn 100-N n\u226f100))\n-- For all n <= 100, then mc91 n = 91.\nmc91-res\u226f100 : \u2200 {n} \u2192 N n \u2192 n \u226f [100] \u2192 mc91 n \u2261 [91]\nmc91-res\u226f100 Nn n\u226f100 with mc91-res Nn\n... | inj\u2081 ( n>100 , _ ) = \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nn 100-N n>100\n (x\u226fy\u2192x\u2264y Nn 100-N n\u226f100))\n... | inj\u2082 ( _ , res ) = res\n\n-- The function always terminates.\nmc91-N : \u2200 {n} \u2192 N n \u2192 N (mc91 n)\nmc91-N Nn with x>y\u2228x\u226fy Nn 100-N\n... | inj\u2081 n>100 = subst N (sym (mc91-res>100 Nn n>100)) (\u2238-N Nn 10-N)\n... | inj\u2082 n\u226e100 = subst N (sym (mc91-res\u226f100 Nn n\u226e100)) 91-N\n\n-- For all n, n < mc91 n + 11.\nmc91-N-ineq : \u2200 {n} \u2192 N n \u2192 n < mc91 n + [11]\nmc91-N-ineq = \u226a-wfind A h\n where\n A : D \u2192 Set\n A d = d < mc91 d + [11]\n\n h : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 k \u226a m \u2192 A k) \u2192 A m\n h {m} Nm f with x>y\u2228x\u226fy Nm 100-N\n ... | inj\u2081 m>100 = x100 Nm m>100\n ... | inj\u2082 m\u226f100 =\n let mc91-m-11-N : N (mc91 (m + [11]))\n mc91-m-11-N = mc91-N (+-N Nm 11-N)\n\n h\u2081 : A (m + [11])\n h\u2081 = f (x+11-N Nm) (<\u2192\u226a (x+11-N Nm) Nm m\u226f100 (x Set : is type family of responses for commands\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\" input to output\n -- input\n -- - pure function 'a\u2192b : a -> b'\n -- - postcondition 'b -> Set' (on output of a\u2192b)\n -- output\n -- - weakest precondition 'a -> Set' on input to a\u2192b that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n module WP0Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data isEven : \u2115 \u2192 Set where\n even-z : isEven Nat.zero\n even-s : {n : \u2115} \u2192 isEven n \u2192 isEven (Nat.suc (Nat.suc n))\n\n isEvenPT : \u2115 -> Set\n isEvenPT = wp0 (_+ 2) isEven\n\n isEven' : isEvenPT \u2261 isEven \u2218 (_+ 2)\n isEven' = refl\n\n isEven5 : Set\n isEven5 = isEvenPT 5\n\n isEven5' : isEvenPT 5 \u2261 isEven 7\n isEven5' = refl\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - enable via making dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n -- every element a that satisfies P is contained in the set of things that satifies Q\n --\n -- implication between predicates\n --\n -- P a says a \u2208 P\n -- if the extents of P, Q : A \u2192 Set is taken to be\n -- the subset of A satisfying predicates P, Q\n --\n -- the extent of P : A -> Set is the subset of terms of type A satisfying P.\n -- \"extent\" indicates a kind of black-box view of things,\n -- e.g., the \"extent\" of a function is the set of its input-output pairs.\n --\n -- https:\/\/en.wikipedia.org\/wiki\/Extension_%28semantics%29\n -- the extension of a concept, idea, or sign consists of the things to which it applies\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n module \u2286Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data _\u2264_ : \u2115 \u2192 \u2115 \u2192 Set where\n z\u2264n : \u2200 {n : \u2115} \u2192 0 \u2264 n\n s\u2264s : \u2200 {m n : \u2115} \u2192 m \u2264 n \u2192 Nat.suc m \u2264 Nat.suc n\n\n \u22641\u2286\u22642 : (_\u2264 1) \u2286 (_\u2264 2)\n -- P \u2286 Q\n -- \u2200 a -> P a -> Q a\n -- (a : \u2115) -> (a \u2264 1) -> (a \u2264 2)\n \u22641\u2286\u22642 Zero _ = z\u2264n\n \u22641\u2286\u22642 (Succ Zero) (s\u2264s _) = s\u2264s z\u2264n\n\n {- cannot be proved\n \u22642\u2286\u22641 : (_\u2264 2) \u2286 (_\u2264 1)\n \u22642\u2286\u22641 Zero _ = z\u2264n\n \u22642\u2286\u22641 (Succ a) (s\u2264s p) = {!!}\n -}\n\n -- refinement relation between PTs : pt1 refined by pt2\n --\n -- (same thing as \u2286, but one level up)\n -- for every post condition, pt2 weakens the precondition\n -- \"weakest\" is the right side : pt2 P\n --\n -- pt1 , pt2 : preconditions for satisfying a program\n -- refinement : pt1 maybe be a stronger requirement than pt2\n --\n -- pt2 is a refinement because it weakens what is required by pt1\n -- e.g., pt1 pt2\n -- >= 20 >= 10\n --\n -- weakening the input assumptions gives a stronger result\n --\n -- contract says establish a pre and I will give you a post, i.e.,\n -- \"stronger\" post condition\n -- or \"weaker\" pre condition\n --\n -- refinement of predicate transformers\n --\n -- use case: relate different implementations of the \"same program\"\n -- pt1 \u2291 pt2 : pt1 is refined by pt2\n -- - if pt1 is understood as giving preconditions which satisfy\n -- postcondition P for some given program f1\n -- then his says the precondition can be \/weakened\/ and still guarantee the postcondition\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g\n -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation to\n - relate PT semantics between programs and specifications\n - show a program satisfies its specification\n - show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n - corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n note : for =Free C R b= programs means generating the same AST\n\n instead, define predicate transformers that give \/interpretations\/\n to a particular choice of command type C and response family R\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public using (_+_; _>_; _*_)\n renaming (\u2115 to Nat; zero to Zero; suc to Succ)\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n -- one command\n data C : Set where\n Abort : C -- no continuation\n\n -- response code : the program does not execute further\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n -- BIG-STEP SEMANTICS specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n module \u21d3Test where\n exv : Expr\n exv = Val 3\n exd : Expr\n exd = Div (Val 3) (Val 0)\n\n exb0 : Val 0 \u21d3 0\n exb0 = \u21d3Base\n\n exb3 : Val 3 \u21d3 3\n exb3 = \u21d3Base\n\n exs331 : Div (Val 3) (Val 3) \u21d3 1\n exs331 = \u21d3Step \u21d3Base \u21d3Base\n\n exs931 : Div (Val 9) (Val 3) \u21d3 3\n exs931 = \u21d3Step \u21d3Base \u21d3Base\n\n exs9d31 : Div (Val 9) (Div (Val 3) (Val 1)) \u21d3 3\n exs9d31 = \u21d3Step \u21d3Base (\u21d3Step \u21d3Base \u21d3Base)\n\n exsd91d31 : Div (Div (Val 9) (Val 1)) (Div (Val 3) (Val 1)) \u21d3 3\n exsd91d31 = \u21d3Step (\u21d3Step \u21d3Base \u21d3Base) (\u21d3Step \u21d3Base \u21d3Base)\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val n \u27e7 = return n\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\nl ->\n \u27e6 er \u27e7 >>= \\nr ->\n nl \u00f7 nr\n\n module InterpTest where\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n evv' : evv \u2261 Pure 3\n evv' = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n evd' : evd \u2261 Pure 1\n evd' = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n evd0' : evd0 \u2261 Step Abort (\u03bb ())\n evd0' = refl\n\n -- relate big step semantics to monadic interpreter\n\n -- convert post-condition for a pure function 'f : (x : A) -> b x'\n -- to post-condition on partial functions 'f : (x : A) -> Partial (b x)'\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba -- P holds if operation produces a value\n mustPT _ _ (Step Abort _) = \u22a5 -- if operation fails, there is no result to apply P to\n -- note: could give \u22a4 here, but want to rule out failure\n -- (total correctness)\n\n module MustPTTest where\n bs\u21d3 : Expr -> Nat -> Set\n bs\u21d3 = _\u21d3_\n\n bs\u21d3' : Set\n bs\u21d3' = Val 1 \u21d3 1\n\n bsp\u21d3 : Expr -> Partial Nat -> Set\n bsp\u21d3 = mustPT _\u21d3_\n\n bsp\u21d3' : Set\n bsp\u21d3' = mustPT _\u21d3_ (Val 1) (Pure 1)\n\n bsp\u21d3'x : mustPT _\u21d3_ (Val 1) (Pure 1)\n bsp\u21d3'x = \u21d3Base\n\n bsp\u21d3'' : Set\n bsp\u21d3'' = mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n {- nothing can be constructed with this type\n bsp\u21d3''x : mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n bsp\u21d3''x = {!!}\n -}\n xxx : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb n -> Pure 3 >>= _\u00f7 n))\n xxx = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx' : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n xxx' = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx'' : Set\n xxx'' = mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n\n {-\n to relate big step semantics to monadic interpreter\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - _\u21d3_ relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n\n to call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results P' : Partial b -> Set\n - done via 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so mustPT Abort case returns empty type\n -}\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n module WpPartialTest where\n exwpp : Expr -> Set\n exwpp = wpPartial \u27e6_\u27e7 _\u21d3_\n exwpp' : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n exwpp' = refl\n\n wppv1 : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n wppv1 = \u21d3Base\n\n wppd11 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n wppd11 = \u21d3Step \u21d3Base \u21d3Base\n wppd11' : Set\n wppd11' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n\n {- this type cannot be constructed\n wppd10 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n wppd10 = {!!}\n -}\n wppd10' : Set\n wppd10' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n\n {-\n wpPartial \u27e6_\u27e7 _\u21d3_ : a predicate on expressions\n - a way to express SUCCESSFUL evaluation of an Expr with intended semantics\n\n \u2200 exprs satisfying this predicate\n - monadic interpreter and\n - relational big-step specification\n must agree on result of evaluation\n\n for a given Expr, satisfaction of predicate means\n - Expr evaluates to a value by big-step operational semantics\n - if interpreter returns a value at all\n then that value agrees with big step semantics\n - if interpreter aborts\n then have a proof of \u22a5 (so, it does not abort)\n which will NOT agree with \u21d3 big-step semantics since \u21d3 does not allow div-by-0\n\n What does this say about correctness of interpreter?\n\n does not specify how many Expr that satisfy that characterization also satisfy wpPartial \u27e6_\u27e7 _\u21d3_\n\n to understand wpPartial \u27e6_\u27e7 _\u21d3_ better\n - define SafeDiv : safety criteria that we believe in\n - prove every expression that satisfies SafeDiv satisfies wpPartial \u27e6_\u27e7 _\u21d3_\n - SafeDiv refines wpPartial \u27e6_\u27e7 _\u21d3_\n - if we gave \u22a4 above, then 'correct' would not say that SafeDiv rules out undefined behavior\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val _) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n module SafeDivTest where\n exsdv3 : SafeDiv (Val 3) \u2261 \u22a4\n exsdv3 = refl\n exsdv0 : SafeDiv (Val 0) \u2261 \u22a4\n exsdv0 = refl\n exsdv0' : Set\n exsdv0' = SafeDiv (Val 0)\n\n exsd33 : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd33 = refl\n exsd33' : Set\n exsd33' = SafeDiv (Div (Val 3) (Val 3))\n\n exsd30 : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val Zero \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd30 = refl\n exsd30' : Set\n exsd30' = SafeDiv (Div (Val 3) (Val 0))\n\n {-\n expect : any expr : e for which SafeDiv e holds can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n -- (a : Expr) (SafeDiv a) (wpPartial \u27e6_\u27e7 _\u21d3_ a)\n correct (Val _) _ = \u21d3Base\n correct (Div el er) (er\u21d30\u2192\u22a5 , (sdel , sder))\n with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n ... | Pure _ | Pure Zero | _ | er\u21d30 = magic (er\u21d30\u2192\u22a5 er\u21d30)\n ... | Pure _ | Pure (Succ _) | el\u21d3nl | er\u21d3Snr = \u21d3Step el\u21d3nl er\u21d3Snr\n ... | Pure _ | Step Abort _ | _ | ()\n ... | Step Abort _ | _ | () | _\n\n module CorrectTest where\n xxx : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n xxx = \u21d3Base\n\n xxx' : Set\n xxx' = wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n\n yyy : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 1))\n yyy = \u21d3Step \u21d3Base \u21d3Base\n {-\n zzz : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 0))\n zzz = {!!}\n -}\n {-\n Generalization.\n Instead of manually defining SafeDiv,\n define more general predicate\n characterising the domain of a partial function using wpPartial.\n -}\n\n -- returns \u22a4 on Pure; \u22a5 on Step Abort ...\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n module DomTest where\n domIs : dom \u27e6_\u27e7 \u2261 \u03bb z \u2192 mustPT (\u03bb x _ \u2192 \u22a4' zero) z \u27e6 z \u27e7\n domIs = refl\n domIs' : dom \u27e6_\u27e7 (Val 0) \u2261 \u22a4' zero\n domIs' = refl\n domIs'' : dom \u27e6_\u27e7 (Div (Val 1) (Val 0)) \u2261 \u22a5\n domIs'' = refl\n\n -- can show that the two semantics agree precisely on the domain of the interpreter:\n\n -- everything in domain of interpreter\n -- gets evaluated in agreement with big step semantics\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n -- only things interpreted in agreement with big step semantics\n -- are those that are in domain of interpreter\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on argument expression\n\n sound (Val _) _ = \u21d3Base\n sound (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div _ _) () | Pure _ | Pure Zero | _ | _\n sound (Div _ _) _ | Pure nl | Pure (Succ nr) | \u22a4'zero\u2192el\u21d3nl | \u22a4'zero\u2192er\u21d3Succnr\n = \u21d3Step (\u22a4'zero\u2192el\u21d3nl tt) (\u22a4'zero\u2192er\u21d3Succnr tt)\n sound (Div _ _) () | Pure _ | Step Abort _ | _ | _\n sound (Div _ _) () | Step Abort _ | _ | _ | _\n\n inDom : {n : Nat}\n -> (e : Expr)\n -> \u27e6 e \u27e7 == Pure n\n -> dom \u27e6_\u27e7 e\n inDom (Val _) _ = tt\n inDom (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div _ _) () | Pure _ | Pure Zero\n inDom (Div _ _) _ | Pure _ | Pure (Succ _) = tt\n inDom (Div _ _) () | Pure _ | Step Abort _\n inDom (Div _ _) () | Step Abort _ | _\n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n : (e : Expr) (n : Nat)\n -> \u27e6 e \u27e7 \u2261 Pure n\n -> e \u21d3 n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n e _ eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} Diveler\u21d3_nldivSuccn\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 { _} { _} () | Pure _ | _ | Pure Zero\n wpPartial1 {el} { _} _ | Pure nl | [[[ eqnl ]]] | Pure (Succ _) = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n el nl eqnl\n wpPartial1 { _} { _} () | Pure _ | _ | Step Abort _\n wpPartial1 { _} { _} () | Step Abort _ | _ | _\n\n wpPartial2 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} _ with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 { _} {er} _ | Pure _ | _ | Pure nr | [[[ eqnr ]]] = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n er nr eqnr\n wpPartial2 { _} { _} () | Pure _ | _ | Step Abort _ | _\n wpPartial2 { _} { _} () | Step Abort _ | _ | _ | _\n\n complete (Val _) _ = tt\n complete (Div el er) h\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div _ _) h | Pure nl | [[[ \u27e6el\u27e7\u2261Purenl ]]] | Pure Zero | [[[ \u27e6er\u27e7\u2261Pure0 ]]] | _ | _\n -- Goal\n -- mustPT (\u03bb _ _ \u2192 \u22a4' zero)\n -- (Div el er) (Pure nl >>= (\u03bb nl' \u2192 Pure Zero >>= _\u00f7_ nl'))\n -- \u22a5\n -- Context\n -- h : mustPT _\u21d3_ (Div el er) (\u27e6 el \u27e7 >>= (\u03bb nl' \u2192 \u27e6 er \u27e7 >>= _\u00f7_ nl'))\n rewrite\n \u27e6el\u27e7\u2261Purenl -- h : mustPT _\u21d3_ (Div el er) (\u27e6 er \u27e7 >>= _\u00f7_ nl)\n | \u27e6er\u27e7\u2261Pure0 -- h : \u22a5\n = h -- magic h\n complete (Div _ _) _ | Pure _ | _ | Pure (Succ _) | _ | _ | _ = tt\n complete (Div _ _) _ | Pure _ | _ | Step Abort _ | _ | _ | ()\n complete (Div _ _) _ | Step Abort _ | _ | _ | _ | () | _\n\n module CompleteTest where\n {-\n xxx : mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n xxx = {!!}\n -}\n xxx' : Set\n xxx' = mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement to relate Kleisli morphisms,\n and to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n add top two elements; can fail fail if stack has too few elements\n\n below shows how to prove the definition meets its specification\n\n Define specification in terms of a pre\/post condition.\n -}\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n {-\n [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n for non-dependent examples (e.g., type b does not depend on x : a)\n -}\n\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function\n\n -- specification for addition function : describes the desired postcondition\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n\n {-\n How to relate this specification to an implementation?\n Note: 'wpPartial' assigns predicate transformer semantics to functions\n - but do not yet have a corresponding predicate transform semantics for specifications.\n wpSpec function does that:\n -}\n\n -- Given a specification, Spec a b\n -- computes the weakest precondition that satisfies an arbitrary postcondition P\n -- i.e., the spec\u2019s precondition should hold and its postcondition must imply P.\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\x -> (pre x) \u2227 (post x \u2286 P x)\n\n -- Can now formulate program\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs =\n pop xs >>= \\{(x1 , xs) ->\n pop xs >>= \\{(x2 , xs) ->\n return ((x1 + x2) :: xs)}}\n\n -- that refines the specification given by addSpec:\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd P Nil (() , _)\n correctnessAdd P (x :: Nil) (Data.Nat.s\u2264s () , _)\n correctnessAdd P (x :: y :: xs) (_ , H) = H (x + y :: xs) AddStep\n\n {-\n This example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition,\n - to its implementation.\n\n Compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n get : State s\n get = Step Get return\n\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n statePT : forall {l l'} -> {b : Set l} -> (b \u00d7 s -> Set l') -> State b -> (s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> (a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> (a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s') = (flatten t' == (seq (s) (size t))) \u2227 (s + size t == s')\n\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n correctnessRelabel : forall {a : Set} -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set} -> (c : State a) (f : a -> State b) ->\n \u2200 i P -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification,\n -- by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence,\n -- specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b} (mx : State a) (f : a -> State b) P i ->\n statePT (wpState f \\_ -> P) mx i -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b} (mx : State a) (f : a -> State b) spec ->\n \u2200 P i -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i) ->\n Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P)) ->\n statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s} -> SpecK {zero} (a \u00d7 s) (b \u00d7 s) -> a -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a) (trans\n (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr ->\n Pair (fl \u2261 seq s sl) (s + sl \u2261 s') ->\n Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'') ->\n Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s -> wpSpec relabelSpec P (t , s) -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set} ->\n ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set} -> (c : Free C R a) (f : a -> Free C R b) ->\n \u2200 P -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P =\n cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R} -> (a -> Free C R b) -> (b -> Free C R c) -> a -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set} -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c) ->\n wpCR f1 \u2291 wpCR f2 ->\n wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","old_contents":"module PredicateTransformers where\n\nopen import Prelude hiding (map; all)\nopen import Level hiding (lift)\n\n------------------------------------------------------------------------------\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\n------------------------------------------------------------------------------\nABSTRACT\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nrefinement type https:\/\/en.wikipedia.org\/wiki\/Refinement_(computing)\n- type with a predicate which must hold for any element of the refined type\n- can express\n - preconditions when used as function args\n - postconditions when used as return types\n\nthe problem statement is centered around \/compositionality\/\n- pure functions enable compositional reasoning\n- effectful programs require reasoning about context\n- paper provides mechanism for reasoning about effectful programs (and for program calculation)\n\nmain idea\n- represent effectful computation as a free monad\n- write an \"interpreter\" that computes a predicate transformer\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to use this refinement relation to\n - show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\nmodule Free where\n -- Free : represents syntax of an effectful computation\n -- C : type of commands\n -- Free C R : returns an 'a' or issues command c : C\n -- For each c : C, there is a set of responses R c\n -- - R : C -> Set : is type family of responses for commands\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\" input to output\n -- input\n -- - pure function 'a\u2192b : a -> b'\n -- - postcondition 'b -> Set' (on output of a\u2192b)\n -- output\n -- - weakest precondition 'a -> Set' on input to a\u2192b that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n module WP0Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data isEven : \u2115 \u2192 Set where\n even-z : isEven Nat.zero\n even-s : {n : \u2115} \u2192 isEven n \u2192 isEven (Nat.suc (Nat.suc n))\n\n isEvenPT : \u2115 -> Set\n isEvenPT = wp0 (_+ 2) isEven\n\n isEven' : isEvenPT \u2261 isEven \u2218 (_+ 2)\n isEven' = refl\n\n isEven5 : Set\n isEven5 = isEvenPT 5\n\n isEven5' : isEvenPT 5 \u2261 isEven 7\n isEven5' = refl\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - enable via making dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n -- every element a that satisfies P is contained in the set of things that satifies Q\n --\n -- implication between predicates\n --\n -- P a says a \u2208 P\n -- if the extents of P, Q : A \u2192 Set is taken to be\n -- the subset of A satisfying predicates P, Q\n --\n -- the extent of P : A -> Set is the subset of terms of type A satisfying P.\n -- \"extent\" indicates a kind of black-box view of things,\n -- e.g., the \"extent\" of a function is the set of its input-output pairs.\n --\n -- https:\/\/en.wikipedia.org\/wiki\/Extension_%28semantics%29\n -- the extension of a concept, idea, or sign consists of the things to which it applies\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n module \u2286Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data _\u2264_ : \u2115 \u2192 \u2115 \u2192 Set where\n z\u2264n : \u2200 {n : \u2115} \u2192 0 \u2264 n\n s\u2264s : \u2200 {m n : \u2115} \u2192 m \u2264 n \u2192 Nat.suc m \u2264 Nat.suc n\n\n \u22641\u2286\u22642 : (_\u2264 1) \u2286 (_\u2264 2)\n -- P \u2286 Q\n -- \u2200 a -> P a -> Q a\n -- (a : \u2115) -> (a \u2264 1) -> (a \u2264 2)\n \u22641\u2286\u22642 Zero _ = z\u2264n\n \u22641\u2286\u22642 (Succ Zero) (s\u2264s _) = s\u2264s z\u2264n\n\n {- cannot be proved\n \u22642\u2286\u22641 : (_\u2264 2) \u2286 (_\u2264 1)\n \u22642\u2286\u22641 Zero _ = z\u2264n\n \u22642\u2286\u22641 (Succ a) (s\u2264s p) = {!!}\n -}\n\n -- refinement relation between PTs : pt1 refined by pt2\n --\n -- (same thing as \u2286, but one level up)\n -- for every post condition, pt2 weakens the precondition\n -- \"weakest\" is the right side : pt2 P\n --\n -- pt1 , pt2 : preconditions for satisfying a program\n -- refinement : pt1 maybe be a stronger requirement than pt2\n --\n -- pt2 is a refinement because it weakens what is required by pt1\n -- e.g., pt1 pt2\n -- >= 20 >= 10\n --\n -- weakening the input assumptions gives a stronger result\n --\n -- contract says establish a pre and I will give you a post, i.e.,\n -- \"stronger\" post condition\n -- or \"weaker\" pre condition\n --\n -- refinement of predicate transformers\n --\n -- use case: relate different implementations of the \"same program\"\n -- pt1 \u2291 pt2 : pt1 is refined by pt2\n -- - if pt1 is understood as giving preconditions which satisfy\n -- postcondition P for some given program f1\n -- then his says the precondition can be \/weakened\/ and still guarantee the postcondition\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g\n -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation to\n - relate PT semantics between programs and specifications\n - show a program satisfies its specification\n - show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n - corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n note : for =Free C R b= programs means generating the same AST\n\n instead, define predicate transformers that give \/interpretations\/\n to a particular choice of command type C and response family R\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public using (_+_; _>_; _*_)\n renaming (\u2115 to Nat; zero to Zero; suc to Succ)\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n -- one command\n data C : Set where\n Abort : C -- no continuation\n\n -- response code : the program does not execute further\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n -- BIG-STEP SEMANTICS specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n module \u21d3Test where\n exv : Expr\n exv = Val 3\n exd : Expr\n exd = Div (Val 3) (Val 0)\n\n exb0 : Val 0 \u21d3 0\n exb0 = \u21d3Base\n\n exb3 : Val 3 \u21d3 3\n exb3 = \u21d3Base\n\n exs331 : Div (Val 3) (Val 3) \u21d3 1\n exs331 = \u21d3Step \u21d3Base \u21d3Base\n\n exs931 : Div (Val 9) (Val 3) \u21d3 3\n exs931 = \u21d3Step \u21d3Base \u21d3Base\n\n exs9d31 : Div (Val 9) (Div (Val 3) (Val 1)) \u21d3 3\n exs9d31 = \u21d3Step \u21d3Base (\u21d3Step \u21d3Base \u21d3Base)\n\n exsd91d31 : Div (Div (Val 9) (Val 1)) (Div (Val 3) (Val 1)) \u21d3 3\n exsd91d31 = \u21d3Step (\u21d3Step \u21d3Base \u21d3Base) (\u21d3Step \u21d3Base \u21d3Base)\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val n \u27e7 = return n\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\nl ->\n \u27e6 er \u27e7 >>= \\nr ->\n nl \u00f7 nr\n\n module InterpTest where\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n evv' : evv \u2261 Pure 3\n evv' = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n evd' : evd \u2261 Pure 1\n evd' = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n evd0' : evd0 \u2261 Step Abort (\u03bb ())\n evd0' = refl\n\n -- relate big step semantics to monadic interpreter\n\n -- convert post-condition for a pure function 'f : (x : a) -> b x'\n -- to post-condition on partial functions 'f : (x : A) -> Partial (b x)'\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba -- P holds if operation produces a value\n mustPT _ _ (Step Abort _) = \u22a5 -- if operation fails, there is no result to apply P to\n -- note: could give \u22a4 here, but want to rule out failure\n -- (total correctness)\n\n module MustPTTest where\n bs\u21d3 : Expr -> Nat -> Set\n bs\u21d3 = _\u21d3_\n\n bs\u21d3' : Set\n bs\u21d3' = Val 1 \u21d3 1\n\n bsp\u21d3 : Expr -> Partial Nat -> Set\n bsp\u21d3 = mustPT _\u21d3_\n\n bsp\u21d3' : Set\n bsp\u21d3' = mustPT _\u21d3_ (Val 1) (Pure 1)\n\n bsp\u21d3'x : mustPT _\u21d3_ (Val 1) (Pure 1)\n bsp\u21d3'x = \u21d3Base\n\n bsp\u21d3'' : Set\n bsp\u21d3'' = mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n {- nothing can be constructed with this type\n bsp\u21d3''x : mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n bsp\u21d3''x = {!!}\n -}\n xxx : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb n -> Pure 3 >>= _\u00f7 n))\n xxx = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx' : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n xxx' = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx'' : Set\n xxx'' = mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n\n {-\n to relate big step semantics to monadic interpreter\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - _\u21d3_ relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n\n to call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results P' : Partial b -> Set\n - done via 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so mustPT Abort case returns empty type\n -}\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n module WpPartialTest where\n exwpp : Expr -> Set\n exwpp = wpPartial \u27e6_\u27e7 _\u21d3_\n exwpp' : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n exwpp' = refl\n\n wppv1 : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n wppv1 = \u21d3Base\n\n wppd11 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n wppd11 = \u21d3Step \u21d3Base \u21d3Base\n wppd11' : Set\n wppd11' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n\n {- this type cannot be constructed\n wppd10 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n wppd10 = {!!}\n -}\n wppd10' : Set\n wppd10' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n\n {-\n wpPartial \u27e6_\u27e7 _\u21d3_ : a predicate on expressions\n - a way to express SUCCESSFUL evaluation of an Expr with intended semantics\n\n \u2200 exprs satisfying this predicate\n - monadic interpreter and\n - relational big-step specification\n must agree on result of evaluation\n\n for a given Expr, satisfaction of predicate means\n - Expr evaluates to a value by big-step operational semantics\n - if interpreter returns a value at all\n then that value agrees with big step semantics\n - if interpreter aborts\n then have a proof of \u22a5 (so, it does not abort)\n which will NOT agree with \u21d3 big-step semantics since \u21d3 does not allow div-by-0\n\n What does this say about correctness of interpreter?\n\n does not specify how many Expr that satisfy that characterization also satisfy wpPartial \u27e6_\u27e7 _\u21d3_\n\n to understand wpPartial \u27e6_\u27e7 _\u21d3_ better\n - define SafeDiv : safety criteria that we believe in\n - prove every expression that satisfies SafeDiv satisfies wpPartial \u27e6_\u27e7 _\u21d3_\n - SafeDiv refines wpPartial \u27e6_\u27e7 _\u21d3_\n - if we gave \u22a4 above, then 'correct' would not say that SafeDiv rules out undefined behavior\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val _) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n module SafeDivTest where\n exsdv3 : SafeDiv (Val 3) \u2261 \u22a4\n exsdv3 = refl\n exsdv0 : SafeDiv (Val 0) \u2261 \u22a4\n exsdv0 = refl\n exsdv0' : Set\n exsdv0' = SafeDiv (Val 0)\n\n exsd33 : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd33 = refl\n exsd33' : Set\n exsd33' = SafeDiv (Div (Val 3) (Val 3))\n\n exsd30 : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val Zero \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd30 = refl\n exsd30' : Set\n exsd30' = SafeDiv (Div (Val 3) (Val 0))\n\n {-\n expect : any expr : e for which SafeDiv e holds can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n -- (a : Expr) (SafeDiv a) (wpPartial \u27e6_\u27e7 _\u21d3_ a)\n correct (Val _) _ = \u21d3Base\n correct (Div el er) (er\u21d30\u2192\u22a5 , (sdel , sder))\n with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n ... | Pure _ | Pure Zero | _ | er\u21d30 = magic (er\u21d30\u2192\u22a5 er\u21d30)\n ... | Pure _ | Pure (Succ _) | el\u21d3nl | er\u21d3Snr = \u21d3Step el\u21d3nl er\u21d3Snr\n ... | Pure _ | Step Abort _ | _ | ()\n ... | Step Abort _ | _ | () | _\n\n module CorrectTest where\n xxx : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n xxx = \u21d3Base\n\n xxx' : Set\n xxx' = wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n\n yyy : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 1))\n yyy = \u21d3Step \u21d3Base \u21d3Base\n {-\n zzz : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 0))\n zzz = {!!}\n -}\n {-\n Generalization.\n Instead of manually defining SafeDiv,\n define more general predicate\n characterising the domain of a partial function using wpPartial\n -}\n\n -- HC: compare to 'mustPT'\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n -- can show that the two semantics agree precisely on the domain of the interpreter:\n\n -- everything in domain of interpreter\n -- gets evaluated in agreement with big step semantics\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n -- only things interpreted in agreement with big step semantics\n -- are those that are in domain of interpreter\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on argument expression\n\n sound (Val _) _ = \u21d3Base\n sound (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div _ _) () | Pure _ | Pure Zero | _ | _\n sound (Div _ _) _ | Pure nl | Pure (Succ nr) | \u22a4'zero\u2192el\u21d3nl | \u22a4'zero\u2192er\u21d3Succnr\n = \u21d3Step (\u22a4'zero\u2192el\u21d3nl tt) (\u22a4'zero\u2192er\u21d3Succnr tt)\n sound (Div _ _) () | Pure _ | Step Abort _ | _ | _\n sound (Div _ _) () | Step Abort _ | _ | _ | _\n\n inDom : {n : Nat}\n -> (e : Expr)\n -> \u27e6 e \u27e7 == Pure n\n -> dom \u27e6_\u27e7 e\n inDom (Val _) _ = tt\n inDom (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div _ _) () | Pure _ | Pure Zero\n inDom (Div _ _) _ | Pure _ | Pure (Succ _) = tt\n inDom (Div _ _) () | Pure _ | Step Abort _\n inDom (Div _ _) () | Step Abort _ | _\n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n : (e : Expr) (n : Nat)\n -> \u27e6 e \u27e7 \u2261 Pure n\n -> e \u21d3 n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n e _ eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} Diveler\u21d3_nldivSuccn\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 { _} { _} () | Pure _ | _ | Pure Zero\n wpPartial1 {el} { _} _ | Pure nl | [[[ eqnl ]]] | Pure (Succ _) = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n el nl eqnl\n wpPartial1 { _} { _} () | Pure _ | _ | Step Abort _\n wpPartial1 { _} { _} () | Step Abort _ | _ | _\n\n wpPartial2 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} _ with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 { _} {er} _ | Pure _ | _ | Pure nr | [[[ eqnr ]]] = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n er nr eqnr\n wpPartial2 { _} { _} () | Pure _ | _ | Step Abort _ | _\n wpPartial2 { _} { _} () | Step Abort _ | _ | _ | _\n\n complete (Val _) _ = tt\n complete (Div el er) h\n with \u27e6 el \u27e7\n | inspect \u27e6_\u27e7 el\n | \u27e6 er \u27e7\n | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div _ _) h | Pure nl | [[[ \u27e6el\u27e7\u2261Purenl ]]] | Pure Zero | [[[ \u27e6er\u27e7\u2261Pure0 ]]] | _ | _\n -- Goal\n -- mustPT (\u03bb _ _ \u2192 \u22a4' zero)\n -- (Div el er) (Pure nl >>= (\u03bb nl' \u2192 Pure Zero >>= _\u00f7_ nl'))\n -- \u22a5\n -- Context\n -- h : mustPT _\u21d3_ (Div el er) (\u27e6 el \u27e7 >>= (\u03bb nl' \u2192 \u27e6 er \u27e7 >>= _\u00f7_ nl'))\n rewrite\n \u27e6el\u27e7\u2261Purenl -- h : mustPT _\u21d3_ (Div el er) (\u27e6 er \u27e7 >>= _\u00f7_ nl)\n | \u27e6er\u27e7\u2261Pure0 -- h : \u22a5\n = h -- magic h\n complete (Div _ _) _ | Pure _ | _ | Pure (Succ _) | _ | _ | _ = tt\n complete (Div _ _) _ | Pure _ | _ | Step Abort _ | _ | _ | ()\n complete (Div _ _) _ | Step Abort _ | _ | _ | _ | () | _\n\n module CompleteTest where\n {-\n xxx : mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n xxx = {!!}\n -}\n xxx' : Set\n xxx' = mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement to relate Kleisli morphisms,\n and to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n add top two elements; can fail fail if stack has too few elements\n\n below shows how to prove the definition meets its specification\n\n Define specification in terms of a pre\/post condition.\n -}\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n {-\n [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n for non-dependent examples (e.g., type b does not depend on x : a)\n -}\n\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function\n\n -- specification for addition function : describes the desired postcondition\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n\n {-\n How to relate this specification to an implementation?\n Note: 'wpPartial' assigns predicate transformer semantics to functions\n - but do not yet have a corresponding predicate transform semantics for specifications.\n wpSpec function does that:\n -}\n\n -- Given a specification, Spec a b\n -- computes the weakest precondition that satisfies an arbitrary postcondition P\n -- i.e., the spec\u2019s precondition should hold and its postcondition must imply P.\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\x -> (pre x) \u2227 (post x \u2286 P x)\n\n -- Can now formulate program\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs =\n pop xs >>= \\{(x1 , xs) ->\n pop xs >>= \\{(x2 , xs) ->\n return ((x1 + x2) :: xs)}}\n\n -- that refines the specification given by addSpec:\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd P Nil (() , _)\n correctnessAdd P (x :: Nil) (Data.Nat.s\u2264s () , _)\n correctnessAdd P (x :: y :: xs) (_ , H) = H (x + y :: xs) AddStep\n\n {-\n This example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition,\n - to its implementation.\n\n Compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n get : State s\n get = Step Get return\n\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n statePT : forall {l l'} -> {b : Set l} -> (b \u00d7 s -> Set l') -> State b -> (s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> (a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> (a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s') = (flatten t' == (seq (s) (size t))) \u2227 (s + size t == s')\n\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n correctnessRelabel : forall {a : Set} -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set} -> (c : State a) (f : a -> State b) ->\n \u2200 i P -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification,\n -- by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence,\n -- specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b} (mx : State a) (f : a -> State b) P i ->\n statePT (wpState f \\_ -> P) mx i -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b} (mx : State a) (f : a -> State b) spec ->\n \u2200 P i -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i) ->\n Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P)) ->\n statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s} -> SpecK {zero} (a \u00d7 s) (b \u00d7 s) -> a -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a) (trans\n (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr ->\n Pair (fl \u2261 seq s sl) (s + sl \u2261 s') ->\n Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'') ->\n Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s -> wpSpec relabelSpec P (t , s) -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set} ->\n ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set} -> (c : Free C R a) (f : a -> Free C R b) ->\n \u2200 P -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P =\n cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R} -> (a -> Free C R b) -> (b -> Free C R c) -> a -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set} -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c) ->\n wpCR f1 \u2291 wpCR f2 ->\n wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"442388d90f9fc78a0d4b2abcc7aa451bbf8b52e9","subject":"Add +1 primitive but not really :-|","message":"Add +1 primitive but not really :-|\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/StepIndexedRelBigStepTypedAnfIlcCorrect.agda","new_file":"Thesis\/StepIndexedRelBigStepTypedAnfIlcCorrect.agda","new_contents":"-- Step-indexed logical relations based on relational big-step semantics\n-- for ILC-based incremental computation.\n\n-- Goal: prove the fundamental lemma for a ternary logical relation (change\n-- validity) across t1, dt and t2. The fundamnetal lemma relates t, derive t and\n-- t. That is, we relate a term evaluated relative to an original environment,\n-- its derivative evaluated relative to a valid environment change, and the\n-- original term evaluated relative to an updated environment.\n--\n-- Missing goal: here \u2295 isn't defined and wouldn't yet agree with change\n-- validity.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\" (ISAC below), POPL'08, including the choice of using ANF syntax\n-- to simplify some step-indexing proofs.\n--\n-- In fact, this development is typed, hence some parts of the model are closer\n-- to Ahmed (ESOP 2006), \"Step-Indexed Syntactic Logical Relations for Recursive\n-- and Quantified Types\". But for many relevant aspects, the two papers are\n-- very similar. In fact, I first defined similar logical relations\n-- without types, but they require a trickier recursion scheme for well-founded\n-- recursion, and I failed to do any proof in that setting.\n--\n-- The original inspiration came from Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but we ended up looking\n-- at their source.\n--\n-- The main insight from the ISAC paper missing from the other one is how to\n-- step-index a big-step semantics correctly: just ensure that the steps in the\n-- big-step semantics agree with the ones in the small-step semantics. *Then*\n-- everything just works with big-step semantics. Quite a few other details are\n-- fiddly, but those are the same in small-step semantics.\n--\n-- The crucial novelty here is that we relate two computations on different\n-- inputs. So we only conclude their results are related if both terminate; the\n-- relation for computations does not prove that if the first computation\n-- terminates, then the second terminates as well.\n--\n-- Instead, e1, de and e2 are related at k steps if, whenever e1 terminates in j\n-- < k steps and e2 terminates with any step count, then de terminates (with any\n-- step count) and their results are related at k - j steps.\n--\n-- Even when e1 terminates in j steps implies that e2 terminates, e2 gets no\n-- bound. Similarly, we do not bound in how many steps de terminates, since on\n-- big inputs it might take long.\n\nmodule Thesis.StepIndexedRelBigStepTypedAnfIlcCorrect where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen import Data.Nat.Properties.Simple\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\ninfixr 20 _\u21d2_\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\n-- Decidable equivalence for types and contexts. Needed later for \u2295 on closures.\n\n\u21d2-inj : \u2200 {\u03c41 \u03c42 \u03c43 \u03c44 : Type} \u2192 _\u2261_ {A = Type} (\u03c41 \u21d2 \u03c42) (\u03c43 \u21d2 \u03c44) \u2192 \u03c41 \u2261 \u03c43 \u00d7 \u03c42 \u2261 \u03c44\n\u21d2-inj refl = refl , refl\n\n_\u225fType_ : (\u03c41 \u03c42 : Type) \u2192 Dec (\u03c41 \u2261 \u03c42)\n(\u03c41 \u21d2 \u03c42) \u225fType (\u03c43 \u21d2 \u03c44) with \u03c41 \u225fType \u03c43 | \u03c42 \u225fType \u03c44\n(\u03c41 \u21d2 \u03c42) \u225fType (.\u03c41 \u21d2 .\u03c42) | yes refl | yes refl = yes refl\n(\u03c41 \u21d2 \u03c42) \u225fType (.\u03c41 \u21d2 \u03c44) | yes refl | no \u00acq = no (\u03bb x \u2192 \u00acq (proj\u2082 (\u21d2-inj x)))\n(\u03c41 \u21d2 \u03c42) \u225fType (\u03c43 \u21d2 \u03c44) | no \u00acp | q = no (\u03bb x \u2192 \u00acp (proj\u2081 (\u21d2-inj x)))\n(\u03c41 \u21d2 \u03c42) \u225fType nat = no (\u03bb ())\nnat \u225fType (\u03c42 \u21d2 \u03c43) = no (\u03bb ())\nnat \u225fType nat = yes refl\n\n\u2022-inj : \u2200 {\u03c41 \u03c42 : Type} {\u03931 \u03932 : Context} \u2192 _\u2261_ {A = Context} (\u03c41 \u2022 \u03931) (\u03c42 \u2022 \u03932) \u2192 \u03c41 \u2261 \u03c42 \u00d7 \u03931 \u2261 \u03932\n\u2022-inj refl = refl , refl\n\n_\u225fCtx_ : (\u03931 \u03932 : Context) \u2192 Dec (\u03931 \u2261 \u03932)\n\u2205 \u225fCtx \u2205 = yes refl\n\u2205 \u225fCtx (\u03c42 \u2022 \u03932) = no (\u03bb ())\n(\u03c41 \u2022 \u03931) \u225fCtx \u2205 = no (\u03bb ())\n(\u03c41 \u2022 \u03931) \u225fCtx (\u03c42 \u2022 \u03932) with \u03c41 \u225fType \u03c42 | \u03931 \u225fCtx \u03932\n(\u03c41 \u2022 \u03931) \u225fCtx (.\u03c41 \u2022 .\u03931) | yes refl | yes refl = yes refl\n(\u03c41 \u2022 \u03931) \u225fCtx (.\u03c41 \u2022 \u03932) | yes refl | no \u00acq = no (\u03bb x \u2192 \u00acq (proj\u2082 (\u2022-inj x)))\n(\u03c41 \u2022 \u03931) \u225fCtx (\u03c42 \u2022 \u03932) | no \u00acp | q = no (\u03bb x \u2192 \u00acp (proj\u2081 (\u2022-inj x)))\n\n\u225fCtx-refl : \u2200 \u0393 \u2192 \u0393 \u225fCtx \u0393 \u2261 yes refl\n\u225fCtx-refl \u0393 with \u0393 \u225fCtx \u0393\n\u225fCtx-refl \u0393 | yes refl = refl\n\u225fCtx-refl \u0393 | no \u00acp = \u22a5-elim (\u00acp refl)\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- Adding this changes nothing without changes to the semantics.\n succ : Const (nat \u21d2 nat)\n\ndata Term (\u0393 : Context) (\u03c4 : Type) : Set\n-- Source values\ndata SVal (\u0393 : Context) : (\u03c4 : Type) \u2192 Set where\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n SVal \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n SVal \u0393 (\u03c3 \u21d2 \u03c4)\ndata Term (\u0393 : Context) (\u03c4 : Type) where\n val :\n SVal \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\n -- constants aka. primitives\n const :\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n app : \u2200 {\u03c3}\n (vs : SVal \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (vt : SVal \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n lett : \u2200 {\u03c3}\n (s : Term \u0393 \u03c3) \u2192\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 \u03c4\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n natV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Data.Integer as I\nopen I using (\u2124)\n\n-- Yann's idea.\ndata HasIdx : Set where\n true : HasIdx\n false : HasIdx\ndata Idx : HasIdx \u2192 Set where\n i' : \u2115 \u2192 Idx true\n no : Idx false\n\ni : {hasIdx : HasIdx} \u2192 \u2115 \u2192 Idx hasIdx\ni {false} j = no\ni {true} j = i' j\n\nmodule _ {hasIdx : HasIdx} where\n data _\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) : \u2200 {\u03c4} \u2192 Term \u0393 \u03c4 \u2192 Idx hasIdx \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u03c3 \u03c4} {t : Term (\u03c3 \u2022 \u0393) \u03c4} \u2192\n \u03c1 \u22a2 val (abs t) \u2193[ i 1 ] closure t \u03c1\n var : \u2200 {\u03c4} (x : Var \u0393 \u03c4) \u2192\n \u03c1 \u22a2 val (var x) \u2193[ i 1 ] (\u27e6 x \u27e7Var \u03c1)\n app : \u2200 n {\u0393\u2032 \u03c3 \u03c4 \u03c1\u2032} vtv {v} {vs : SVal \u0393 (\u03c3 \u21d2 \u03c4)} {vt : SVal \u0393 \u03c3} {t : Term (\u03c3 \u2022 \u0393\u2032) \u03c4} \u2192\n \u03c1 \u22a2 val vs \u2193[ i 0 ] closure t \u03c1\u2032 \u2192\n \u03c1 \u22a2 val vt \u2193[ i 0 ] vtv \u2192\n (vtv \u2022 \u03c1\u2032) \u22a2 t \u2193[ i n ] v \u2192\n \u03c1 \u22a2 app vs vt \u2193[ i (suc n) ] v\n lett :\n \u2200 n1 n2 {\u03c3 \u03c4} vsv {v} (s : Term \u0393 \u03c3) (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n \u03c1 \u22a2 s \u2193[ i n1 ] vsv \u2192\n (vsv \u2022 \u03c1) \u22a2 t \u2193[ i n2 ] v \u2192\n \u03c1 \u22a2 lett s t \u2193[ i (suc n1 + n2) ] v\n lit : \u2200 n \u2192\n \u03c1 \u22a2 const (lit n) \u2193[ i 1 ] natV n\n\n-- data DType : Set where\n-- _\u21d2_ : (\u03c3 \u03c4 : DType) \u2192 DType\n-- int : DType\nDType = Type\n\nimport Base.Syntax.Context DType as DC\n\n\u0394\u03c4 : Type \u2192 DType\n\u0394\u03c4 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394\u03c4 \u03c3 \u21d2 \u0394\u03c4 \u03c4\n\u0394\u03c4 nat = nat\n\n\u0394\u0394 : Context \u2192 DC.Context\n\u0394\u0394 \u2205 = \u2205\n\u0394\u0394 (\u03c4 \u2022 \u0393) = \u0394\u03c4 \u03c4 \u2022 \u0394\u0394 \u0393\n--\u0394\u0394 \u0393 = \u0393\n\n-- A DTerm evaluates in normal context \u0394, change context (\u0394\u0394 \u0394), and produces\n-- a result of type (\u0394t \u03c4).\ndata DTerm (\u0394 : Context) (\u03c4 : DType) : Set\ndata DSVal (\u0394 : Context) : (\u03c4 : DType) \u2192 Set where\n dvar : \u2200 {\u03c4} \u2192\n (x : Var (\u0394\u0394 \u0394) (\u0394\u03c4 \u03c4)) \u2192\n DSVal \u0394 \u03c4\n dabs : \u2200 {\u03c3 \u03c4}\n (dt : DTerm (\u03c3 \u2022 \u0394) \u03c4) \u2192\n DSVal \u0394 (\u03c3 \u21d2 \u03c4)\n\ndata DTerm (\u0394 : Context) (\u03c4 : DType) where\n dval :\n DSVal \u0394 \u03c4 \u2192\n DTerm \u0394 \u03c4\n -- constants aka. primitives\n dconst :\n (c : Const \u03c4) \u2192\n DTerm \u0394 \u03c4\n dapp : \u2200 {\u03c3}\n (dvs : DSVal \u0394 (\u03c3 \u21d2 \u03c4)) \u2192\n (vt : SVal \u0394 \u03c3) \u2192\n (dvt : DSVal \u0394 \u03c3) \u2192\n DTerm \u0394 \u03c4\n dlett : \u2200 {\u03c3}\n (s : Term \u0394 \u03c3) \u2192\n (ds : DTerm \u0394 \u03c3) \u2192\n (dt : DTerm (\u03c3 \u2022 \u0394) \u03c4) \u2192\n DTerm \u0394 \u03c4\n\nderive-dvar : \u2200 {\u0394 \u03c3} \u2192 (x : Var \u0394 \u03c3) \u2192 Var (\u0394\u0394 \u0394) (\u0394\u03c4 \u03c3)\nderive-dvar this = this\nderive-dvar (that x) = that (derive-dvar x)\n\nderive-dterm : \u2200 {\u0394 \u03c3} \u2192 (t : Term \u0394 \u03c3) \u2192 DTerm \u0394 \u03c3\n\nderive-dsval : \u2200 {\u0394 \u03c3} \u2192 (t : SVal \u0394 \u03c3) \u2192 DSVal \u0394 \u03c3\nderive-dsval (var x) = dvar (derive-dvar x)\n\nderive-dsval (abs t) = dabs (derive-dterm t)\n\nderive-dterm (val x) = dval (derive-dsval x)\nderive-dterm (const c) = dconst c\nderive-dterm (app vs vt) = dapp (derive-dsval vs) vt (derive-dsval vt)\nderive-dterm (lett s t) = dlett s (derive-dterm s) (derive-dterm t)\n\n-- Nontrivial because of unification problems in pattern matching. I wanted to\n-- use it to define \u2295 on closures purely on terms of the closure change.\n\n-- Instead, I decided to use decidable equality on contexts: that's a lot of\n-- tedious boilerplate, but not too hard, but the proof that validity and \u2295\n-- agree becomes easier.\n-- -- Define a DVar and be done?\n-- underive-dvar : \u2200 {\u0394 \u03c3} \u2192 Var (\u0394\u0394 \u0394) (\u0394\u03c4 \u03c3) \u2192 Var \u0394 \u03c3\n-- underive-dvar {\u2205} ()\n-- underive-dvar {\u03c4 \u2022 \u0394} x = {!!}\n\n--underive-dvar {\u03c3 \u2022 \u0394} (that x) = that (underive-dvar x)\n\ndata DVal : Type \u2192 Set\nimport Base.Denotation.Environment DType DVal as D\n\nCh\u0394 : \u2200 (\u0394 : Context) \u2192 Set\nCh\u0394 \u0394 = D.\u27e6 \u0394 \u27e7Context\n\ndata DVal where\n bang : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 DVal \u03c4\n dclosure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (dt : DTerm (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 (d\u03c1 : Ch\u0394 \u0393) \u2192 DVal (\u03c3 \u21d2 \u03c4)\n dnatV : \u2200 (n : \u2115) \u2192 DVal nat\n\n_\u2295_ : \u2200 {\u03c4} \u2192 (v1 : Val \u03c4) (dv : DVal \u03c4) \u2192 Val \u03c4\n\n_\u2295\u03c1_ : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7Context \u2192 Ch\u0394 \u0393 \u2192 \u27e6 \u0393 \u27e7Context\n\u2205 \u2295\u03c1 \u2205 = \u2205\n(v \u2022 \u03c11) \u2295\u03c1 (dv \u2022 d\u03c1) = v \u2295 dv \u2022 \u03c11 \u2295\u03c1 d\u03c1\n\nv1 \u2295 bang v2 = v2\nclosure {\u0393} t \u03c1 \u2295 dclosure {\u03931} dt \u03c1\u2081 d\u03c1 with \u0393 \u225fCtx \u03931\nclosure {\u0393} t \u03c1 \u2295 dclosure {.\u0393} dt \u03c1\u2081 d\u03c1 | yes refl = closure t (\u03c1 \u2295\u03c1 d\u03c1)\n... | no \u00acp = closure t \u03c1\n_\u2295_ (natV n) (dnatV dn) = natV (n + dn)\n\ndata _D_\u22a2_\u2193_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) : \u2200 {\u03c4} \u2192 DTerm \u0393 \u03c4 \u2192 DVal \u03c4 \u2192 Set where\n dabs : \u2200 {\u03c3 \u03c4} {dt : DTerm (\u03c3 \u2022 \u0393) \u03c4} \u2192\n \u03c1 D d\u03c1 \u22a2 dval (dabs dt) \u2193 dclosure dt \u03c1 d\u03c1\n dvar : \u2200 {\u03c4} (x : DC.Var \u0393 \u03c4) \u2192\n \u03c1 D d\u03c1 \u22a2 dval (dvar (derive-dvar x)) \u2193 D.\u27e6 x \u27e7Var d\u03c1\n dlit : \u2200 n \u2192\n \u03c1 D d\u03c1 \u22a2 dconst (lit n) \u2193 dnatV 0\n dapp : \u2200 {hasIdx} {n : Idx hasIdx}\n {\u0393\u2032 \u03c3 \u03c4 \u03c1\u2032 d\u03c1\u2032}\n {dvs} {vt} {dvt}\n {vtv} {dvtv}\n {dt : DTerm (\u03c3 \u2022 \u0393\u2032) \u03c4} {dv} \u2192\n \u03c1 D d\u03c1 \u22a2 dval dvs \u2193 dclosure dt \u03c1\u2032 d\u03c1\u2032 \u2192\n \u03c1 \u22a2 val vt \u2193[ n ] vtv \u2192\n \u03c1 D d\u03c1 \u22a2 dval dvt \u2193 dvtv \u2192\n (vtv \u2022 \u03c1\u2032) D (dvtv \u2022 d\u03c1\u2032) \u22a2 dt \u2193 dv \u2192\n \u03c1 D d\u03c1 \u22a2 dapp dvs vt dvt \u2193 dv\n dlett : \u2200 {hasIdx} {n : Idx hasIdx}\n {\u03c3 \u03c4} {s : Term \u0393 \u03c3} {ds} {dt : DTerm (\u03c3 \u2022 \u0393) \u03c4}\n {vsv dvsv dv} \u2192\n \u03c1 \u22a2 s \u2193[ n ] vsv \u2192\n \u03c1 D d\u03c1 \u22a2 ds \u2193 dvsv \u2192\n (vsv \u2022 \u03c1) D (dvsv \u2022 d\u03c1) \u22a2 dt \u2193 dv \u2192\n \u03c1 D d\u03c1 \u22a2 dlett s ds dt \u2193 dv\n bangapp : \u2200 {hasIdx} {n1 n2 : Idx hasIdx}\n {\u0393\u2032 \u03c3 \u03c4 \u03c1\u2032}\n {dvs} {vt} {dvt}\n {vtv2}\n {t : Term (\u03c3 \u2022 \u0393\u2032) \u03c4} {v2} \u2192\n \u03c1 D d\u03c1 \u22a2 dval dvs \u2193 bang (closure t \u03c1\u2032) \u2192\n (\u03c1 \u2295\u03c1 d\u03c1) \u22a2 val vt \u2193[ n1 ] vtv2 \u2192\n (vtv2 \u2022 \u03c1\u2032) \u22a2 t \u2193[ n2 ] v2 \u2192\n \u03c1 D d\u03c1 \u22a2 dapp dvs vt dvt \u2193 bang v2\n\nmutual\n rrelT3 : \u2200 {\u03c4 \u0393} (t1 : Term \u0393 \u03c4) (dt : DTerm \u0393 \u03c4) (t2 : Term \u0393 \u03c4) (\u03c11 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) (\u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\n rrelT3 {\u03c4} t1 dt t2 \u03c11 d\u03c1 \u03c12 k =\n (v1 v2 : Val \u03c4) \u2192\n \u2200 j (j_ : Color \u2192 Clearance \u2192 Set where\n : \u2200 {c} \u2192 c c> Low\n : Green c> High\n\n data _follows_ : Position \u2192 Position \u2192 Set where\n stay : \u2200 {x y} \u2192 pos x y gas Low follows pos x y gas Low\n next : \u2200 {c cl x y}{{_ : c c> cl}} \u2192\n pos (suc x) y gas Low follows pos x y gas cl\n back : \u2200 {c cl x y}{{_ : c c> cl}} \u2192\n pos x y gas Low follows pos (suc x) y gas cl\n -- jump : \u2200 {x y} \u2192 pos x (suc y) gas High follows pos x y gas Low\n fall : \u2200 {cl x y} \u2192 pos x y gas cl follows pos x (suc y) gas High\n\n\n infixr 5 _\u21a0\u27e8_\u27e9_\n data Path (Koopa : KoopaTroopa Color) : Position \u2192 Position \u2192 Set where\n [] : \u2200 {p} \u2192 Path Koopa p p\n _\u21a0\u27e8_\u27e9_ : {q r : Position} \u2192 (p : Position) \u2192 q follows p\n \u2192 (qs : Path Koopa q r) \u2192 Path Koopa p r\n\n ex_path : Path (Red KT) (pos 0 0 gas Low) (pos 0 0 gas Low)\n ex_path = pos 0 0 gas Low \u21a0\u27e8 next {Red} \u27e9\n pos 1 0 gas Low \u21a0\u27e8 back \u27e9\n pos 0 0 gas Low \u21a0\u27e8 stay \u27e9 []\n\n matToPosVec : {n : \u2115} \u2192 Vec Material n \u2192 Vec Material n \u2192 \u2115 \u2192 \u2115 \u2192\n Vec Position n\n matToPosVec [] [] _ _ = []\n matToPosVec (mat \u2237 mats) (under \u2237 unders) x y =\n pos x y mat cl \u2237 matToPosVec mats unders (x + 1) y\n where\n clearance : Material \u2192 Material \u2192 Clearance\n clearance gas gas = High\n clearance gas solid = Low\n clearance solid _ = God\n cl = clearance mat under\n\n matToPosVecs : {w h : \u2115} \u2192 Vec (Vec Material w) h \u2192 Vec (Vec Position w) h\n matToPosVecs [] = []\n matToPosVecs (_\u2237_ {y} mats matss) = matToPosVec mats mats 0 y \u2237 matToPosVecs matss\n\n matsToMat : {w h : \u2115} \u2192 Vec (Vec Material w) h \u2192 Matrix Position w h\n matsToMat matss = Mat (matToPosVecs matss)\n\n \u25a1 : Material\n \u25a1 = gas\n \u25a0 : Material\n \u25a0 = solid\n example_level : Matrix Position 10 7\n example_level = matsToMat (\n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a0 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 []) \u2237 \n (\u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 []) \u2237 [])\n","old_contents":"{-\n\n Verified Koopa Troopa Movement\n Toon Nolten\n\n-}\n\nmodule koopa where\n open import Data.Nat\n open import Data.Fin renaming (_+_ to _F+_)\n open import Data.List\n open import Data.Vec renaming (map to vmap; lookup to vlookup)\n\n module Matrix where\n data Matrix (A : Set) : \u2115 \u2192 \u2115 \u2192 Set where\n Mat : {w h : \u2115} \u2192 Vec (Vec A w) h \u2192 Matrix A w h\n\n lookup : \u2200 {w h} {A : Set} \u2192 Fin h \u2192 Fin w \u2192 Matrix A w h \u2192 A\n lookup row column (Mat rows) = vlookup column (vlookup row rows)\n open Matrix\n\n data Color : Set where\n Green : Color\n Red : Color\n\n data KoopaTroopa Color : Set where\n _KT : Color \u2192 KoopaTroopa Color\n\n data Material : Set where\n gas : Material\n -- liquid : Material\n solid : Material\n\n record Position : Set where\n constructor pos\n field\n x : \u2115\n y : \u2115\n mat : Material\n\n data _follows_ : Position \u2192 Position \u2192 Set where\n stay : \u2200 {p} \u2192 p follows p\n next : \u2200 {x y mat} \u2192 pos (suc x) y mat follows pos x y mat\n back : \u2200 {x y mat} \u2192 pos x y mat follows pos (suc x) y mat\n -- jump : \u2200 {x y mat} \u2192 pos x (suc y) mat follows pos x y mat\n fall : \u2200 {x y mat} \u2192 pos x y mat follows pos x (suc y) mat\n\n\n infixr 5 _\u21a0\u27e8_\u27e9_\n data Path (Koopa : KoopaTroopa Color) : Position \u2192 Position \u2192 Set where\n [] : \u2200 {p} \u2192 Path Koopa p p\n _\u21a0\u27e8_\u27e9_ : {q r : Position} \u2192 (p : Position) \u2192 q follows p\n \u2192 (qs : Path Koopa q r) \u2192 Path Koopa p r\n\n ex_path : Path (Red KT) (pos 0 0 solid) (pos 0 0 solid)\n ex_path = pos 0 0 solid \u21a0\u27e8 next \u27e9\n pos 1 0 solid \u21a0\u27e8 back \u27e9\n pos 0 0 solid \u21a0\u27e8 stay \u27e9 []\n\n matToPosVec : {n : \u2115} \u2192 Vec Material n \u2192 \u2115 \u2192 \u2115 \u2192 Vec Position n\n matToPosVec [] _ _ = []\n matToPosVec (mat \u2237 mats) x y = pos x y mat \u2237 matToPosVec mats (x + 1) y\n\n matToPosVecs : {w h : \u2115} \u2192 Vec (Vec Material w) h \u2192 Vec (Vec Position w) h\n matToPosVecs [] = []\n matToPosVecs (_\u2237_ {y} mats matss) = matToPosVec mats 0 y \u2237 matToPosVecs matss\n\n matToMat : {w h : \u2115} \u2192 Vec (Vec Material w) h \u2192 Matrix Position w h\n matToMat matss = Mat (matToPosVecs matss)\n\n \u25a1 : Material\n \u25a1 = gas\n \u25a0 : Material\n \u25a0 = solid\n example_level : Matrix Position 10 7\n example_level = Mat (matToPosVecs (\n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a0 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 []) \u2237 \n (\u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 []) \u2237 []))\n\n","returncode":0,"stderr":"","license":"bsd-2-clause","lang":"Agda"} {"commit":"20a1abbafdf38b144be117be78c688be2786fbee","subject":"Update to Neglible","message":"Update to Neglible\n","repos":"crypto-agda\/crypto-agda","old_file":"Neglible.agda","new_file":"Neglible.agda","new_contents":"{-# OPTIONS --copatterns #-}\nopen import Algebra\n\nopen import Function\n\nopen import Data.Nat.NP\nopen import Data.Nat.Distance\nopen import Data.Nat.Properties\nopen import Data.Two\nopen import Data.Zero\nopen import Data.Product\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\n\nopen import HoTT\nopen Equivalences\n\nopen import Explore.Core\nopen import Explore.Universe.Type {\ud835\udfd8}\nopen import Explore.Universe.Base\n\nmodule Neglible where\n\nmodule prop = CommutativeSemiring commutativeSemiring\nmodule OR = Poset (DecTotalOrder.poset decTotalOrder)\n\n\u2264-*-cancel : \u2200 {x m n} \u2192 1 \u2264 x \u2192 x * m \u2264 x * n \u2192 m \u2264 n\n\u2264-*-cancel {suc x} {m} {n} (s\u2264s le) mn\n rewrite prop.*-comm (suc x) m | prop.*-comm (suc x) n = cancel-*-right-\u2264 _ _ _ mn\n\nrecord \u2115\u2192\u211a : Set where\n constructor _\/_[_]\n field\n \u03b5N : (n : \u2115) \u2192 \u2115\n \u03b5D : (n : \u2115) \u2192 \u2115\n \u03b5D-pos : \u2200 n \u2192 \u03b5D n > 0\n\nrecord Is-Neg (\u03b5 : \u2115\u2192\u211a) : Set where\n constructor mk\n open \u2115\u2192\u211a \u03b5\n field\n c\u2099 : (c : \u2115) \u2192 \u2115\n prf : \u2200(c n : \u2115) \u2192 n > c\u2099 n \u2192 n ^ c * \u03b5N n \u2264 \u03b5D n\nopen Is-Neg\n\n0\u2115\u211a : \u2115\u2192\u211a\n\u2115\u2192\u211a.\u03b5N 0\u2115\u211a _ = 0\n\u2115\u2192\u211a.\u03b5D 0\u2115\u211a _ = 1\n\u2115\u2192\u211a.\u03b5D-pos 0\u2115\u211a _ = s\u2264s z\u2264n\n\n0\u2115\u211a-neg : Is-Neg 0\u2115\u211a\nc\u2099 0\u2115\u211a-neg _ = 0\nprf 0\u2115\u211a-neg c n x = OR.trans (OR.reflexive (proj\u2082 prop.zero (n ^ c))) z\u2264n\n\n_+\u2115\u211a_ : \u2115\u2192\u211a \u2192 \u2115\u2192\u211a \u2192 \u2115\u2192\u211a\n\u2115\u2192\u211a.\u03b5N ((\u03b5N \/ \u03b5D [ _ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ _ ])) n = \u03b5N n * \u03bcD n + \u03bcN n * \u03b5D n\n\u2115\u2192\u211a.\u03b5D ((\u03b5N \/ \u03b5D [ _ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ _ ])) n = \u03b5D n * \u03bcD n\n\u2115\u2192\u211a.\u03b5D-pos ((\u03b5N \/ \u03b5D [ \u03b5D+ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ \u03bcD+ ])) n = \u03b5D+ n *-mono \u03bcD+ n\n\n\n+\u2115\u211a-neg : {\u03b5 \u03bc : \u2115\u2192\u211a} \u2192 Is-Neg \u03b5 \u2192 Is-Neg \u03bc \u2192 Is-Neg (\u03b5 +\u2115\u211a \u03bc)\nc\u2099 (+\u2115\u211a-neg \u03b5 \u03bc) n = 1 + c\u2099 \u03b5 n + c\u2099 \u03bc n\nprf (+\u2115\u211a-neg {\u03b5M} {\u03bcM} \u03b5 \u03bc) c n n>nc = \u2264-*-cancel {x = n} (OR.trans (s\u2264s z\u2264n) n>nc) lemma\n where\n\n open \u2264-Reasoning\n open \u2115\u2192\u211a \u03b5M\n open \u2115\u2192\u211a \u03bcM renaming (\u03b5N to \u03bcN; \u03b5D to \u03bcD; \u03b5D-pos to \u03bcD-pos)\n\n lemma = n * (n ^ c * (\u03b5N n * \u03bcD n + \u03bcN n * \u03b5D n))\n \u2261\u27e8 ! prop.*-assoc n (n ^ c) _\n \u2219 proj\u2081 prop.distrib (n ^ (1 + c)) (\u03b5N n * \u03bcD n) (\u03bcN n * \u03b5D n)\n \u2219 ap\u2082 _+_ (! prop.*-assoc (n ^ (1 + c)) (\u03b5N n) (\u03bcD n))\n (! (prop.*-assoc (n ^ (1 + c)) (\u03bcN n) (\u03b5D n))) \u27e9\n n ^ (1 + c) * \u03b5N n * \u03bcD n + n ^ (1 + c) * \u03bcN n * \u03b5D n\n \u2264\u27e8 (prf \u03b5 (1 + c) n (OR.trans (s\u2264s (\u2264-step (m\u2264m+n (c\u2099 \u03b5 n) (c\u2099 \u03bc n)))) n>nc) *-mono (\u03bcD n \u220e))\n +-mono (prf \u03bc (1 + c) n (OR.trans (s\u2264s (\u2264-step (n\u2264m+n (c\u2099 \u03b5 n) (c\u2099 \u03bc n)))) n>nc) *-mono (\u03b5D n \u220e)) \u27e9\n \u03b5D n * \u03bcD n + \u03bcD n * \u03b5D n\n \u2261\u27e8 ap\u2082 _+_ (refl {x = \u03b5D n * \u03bcD n}) (prop.*-comm (\u03bcD n) (\u03b5D n) \u2219 ! proj\u2082 prop.+-identity (\u03b5D n * \u03bcD n)) \u27e9\n 2 * (\u03b5D n * \u03bcD n)\n \u2264\u27e8 OR.trans (s\u2264s (s\u2264s z\u2264n)) n>nc *-mono (\u03b5D n * \u03bcD n \u220e) \u27e9\n n * (\u03b5D n * \u03bcD n)\n \u220e\n\ninfix 4 _\u2264\u2192_\nrecord _\u2264\u2192_ (f g : \u2115\u2192\u211a) : Set where\n constructor mk\n open \u2115\u2192\u211a f renaming (\u03b5N to fN; \u03b5D to fD)\n open \u2115\u2192\u211a g renaming (\u03b5N to gN; \u03b5D to gD)\n field\n -- fN k \/ fD k \u2264 gN k \/ gD k\n \u2264\u2192 : \u2200 k \u2192 fN k * gD k \u2264 gN k * fD k\n\n\u2264\u2192-trans : \u2200 {f g h} \u2192 f \u2264\u2192 g \u2192 g \u2264\u2192 h \u2192 f \u2264\u2192 h\n_\u2264\u2192_.\u2264\u2192 (\u2264\u2192-trans {fN \/ fD [ fD-pos ]} {gN \/ gD [ gD-pos ]} {hN \/ hD [ hD-pos ]} (mk fg) (mk gh)) k\n = \u2264-*-cancel (gD-pos k) lemma\n where\n open \u2264-Reasoning\n lemma : gD k * (fN k * hD k) \u2264 gD k * (hN k * fD k)\n lemma = gD k * (fN k * hD k)\n \u2261\u27e8 ! prop.*-assoc (gD k) (fN k) (hD k)\n \u2219 ap (flip _*_ (hD k)) (prop.*-comm (gD k) (fN k))\n \u27e9\n (fN k * gD k) * hD k\n \u2264\u27e8 fg k *-mono OR.refl \u27e9\n (gN k * fD k) * hD k\n \u2261\u27e8 prop.*-assoc (gN k) (fD k) (hD k)\n \u2219 ap (_*_ (gN k)) (prop.*-comm (fD k) (hD k))\n \u2219 ! prop.*-assoc (gN k) (hD k) (fD k)\n \u27e9\n (gN k * hD k) * fD k\n \u2264\u27e8 gh k *-mono OR.refl \u27e9\n (hN k * gD k) * fD k\n \u2261\u27e8 ap (flip _*_ (fD k)) (prop.*-comm (hN k) (gD k))\n \u2219 prop.*-assoc (gD k) (hN k) (fD k)\n \u27e9\n gD k * (hN k * fD k)\n \u220e\n\n+\u2115\u211a-mono : \u2200 {f f' g g'} \u2192 f \u2264\u2192 f' \u2192 g \u2264\u2192 g' \u2192 f +\u2115\u211a g \u2264\u2192 f' +\u2115\u211a g'\n_\u2264\u2192_.\u2264\u2192 (+\u2115\u211a-mono {fN \/ fD [ _ ]} {f'N \/ f'D [ _ ]} {gN \/ gD [ _ ]} {g'N \/ g'D [ _ ]} (mk ff) (mk gg)) k\n = (fN k * gD k + gN k * fD k) * (f'D k * g'D k)\n \u2261\u27e8 proj\u2082 prop.distrib (f'D k * g'D k) (fN k * gD k) (gN k * fD k) \u27e9\n fN k * gD k * (f'D k * g'D k) + gN k * fD k * (f'D k * g'D k)\n \u2261\u27e8 ap\u2082 _+_ (*-interchange (fN k) (gD k) (f'D k) (g'D k) \u2219 ap (_*_ (fN k * f'D k)) (prop.*-comm (gD k) (g'D k)))\n (ap (_*_ (gN k * fD k)) (prop.*-comm (f'D k) (g'D k)) \u2219 *-interchange (gN k) (fD k) (g'D k) (f'D k))\n \u27e9\n fN k * f'D k * (g'D k * gD k) + gN k * g'D k * (fD k * f'D k)\n \u2264\u27e8 (ff k *-mono OR.refl) +-mono (gg k *-mono OR.refl) \u27e9\n f'N k * fD k * (g'D k * gD k) + g'N k * gD k * (fD k * f'D k)\n \u2261\u27e8 ap\u2082 _+_ (*-interchange (f'N k) (fD k) (g'D k) (gD k))\n (ap (_*_ (g'N k * gD k)) (prop.*-comm (fD k) (f'D k))\n \u2219 *-interchange (g'N k) (gD k) (f'D k) (fD k)\n \u2219 ap (_*_ (g'N k * f'D k)) (prop.*-comm (gD k) (fD k)))\n \u27e9\n f'N k * g'D k * (fD k * gD k) + g'N k * f'D k * (fD k * gD k)\n \u2261\u27e8 ! proj\u2082 prop.distrib (fD k * gD k) (f'N k * g'D k) (g'N k * f'D k) \u27e9\n (f'N k * g'D k + g'N k * f'D k) * (fD k * gD k)\n \u220e\n where\n open \u2264-Reasoning\n\nrecord NegBounded (f : \u2115\u2192\u211a) : Set where\n constructor mk\n field\n \u03b5 : \u2115\u2192\u211a\n \u03b5-neg : Is-Neg \u03b5\n bounded : f \u2264\u2192 \u03b5\n\nmodule _ where\n open NegBounded\n \u2264-NB : {f g : \u2115\u2192\u211a} \u2192 f \u2264\u2192 g \u2192 NegBounded g \u2192 NegBounded f\n \u03b5 (\u2264-NB le nb) = \u03b5 nb\n \u03b5-neg (\u2264-NB le nb) = \u03b5-neg nb\n bounded (\u2264-NB le nb) = \u2264\u2192-trans le (bounded nb)\n\n _+NB_ : {f g : \u2115\u2192\u211a} \u2192 NegBounded f \u2192 NegBounded g \u2192 NegBounded (f +\u2115\u211a g)\n \u03b5 (fNB +NB gNB) = \u03b5 fNB +\u2115\u211a \u03b5 gNB\n \u03b5-neg (fNB +NB gNB) = +\u2115\u211a-neg (\u03b5-neg fNB) (\u03b5-neg gNB)\n bounded (fNB +NB gNB) = +\u2115\u211a-mono (bounded fNB) (bounded gNB)\n\nmodule ~-NegBounded (R\u1d41 : \u2115 \u2192 U)(let R = \u03bb n \u2192 El (R\u1d41 n))(inh : \u2200 x \u2192 0 < Card (R\u1d41 x)) where\n\n # : \u2200 {n} \u2192 Count (R n)\n # {n} = count (R\u1d41 n)\n\n ~dist : (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) \u2192 \u2115\u2192\u211a\n \u2115\u2192\u211a.\u03b5N (~dist f g) n = dist (# (f n)) (# (g n))\n \u2115\u2192\u211a.\u03b5D (~dist f g) n = Card (R\u1d41 n)\n \u2115\u2192\u211a.\u03b5D-pos (~dist f g) n = inh n\n\n ~dist-sum : \u2200 f g h \u2192 ~dist f h \u2264\u2192 ~dist f g +\u2115\u211a ~dist g h\n _\u2264\u2192_.\u2264\u2192 (~dist-sum f g h) k\n = #fh * (|R| * |R|)\n \u2264\u27e8 dist-sum #f #g #h *-mono OR.refl \u27e9\n (#fg + #gh) * (|R| * |R|)\n \u2261\u27e8 ! prop.*-assoc (#fg + #gh) |R| |R| \u2219 ap (flip _*_ |R|) (proj\u2082 prop.distrib |R| #fg #gh) \u27e9\n (#fg * |R| + #gh * |R|) * |R|\n \u220e\n where\n open \u2264-Reasoning\n |R| = Card (R\u1d41 k)\n #f = # (f k)\n #g = # (g k)\n #h = # (h k)\n #fh = dist #f #h\n #fg = dist #f #g\n #gh = dist #g #h\n\n record _~_ (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) : Set where\n constructor mk\n field\n ~ : NegBounded (~dist f g)\n\n ~-trans : Transitive _~_\n _~_.~ (~-trans {f}{g}{h} (mk fg) (mk gh)) = \u2264-NB (~dist-sum f g h) (fg +NB gh)\n\nmodule ~-Inlined (R\u1d41 : \u2115 \u2192 U)(let R = \u03bb n \u2192 El (R\u1d41 n)) where\n\n # : \u2200 {n} \u2192 Count (R n)\n # {n} = count (R\u1d41 n)\n\n record _~_ (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) : Set where\n constructor mk\n field\n \u03b5 : \u2115\u2192\u211a\n open \u2115\u2192\u211a \u03b5\n field\n \u03b5-neg : Is-Neg \u03b5\n bounded : \u2200 k \u2192 \u03b5D k * dist (# (f k)) (# (g k)) \u2264 Card (R\u1d41 k) * \u03b5N k\n\n\n ~-trans : Transitive _~_\n _~_.\u03b5 (~-trans x x\u2081) = _\n _~_.\u03b5-neg (~-trans x x\u2081) = +\u2115\u211a-neg (_~_.\u03b5-neg x) (_~_.\u03b5-neg x\u2081)\n _~_.bounded (~-trans {f}{g}{h}(mk \u03b5\u2080 \u03b5\u2080-neg fg) (mk \u03b5\u2081 \u03b5\u2081-neg gh)) k\n = (b * d) * dist #f #h\n \u2264\u27e8 (b * d \u220e) *-mono dist-sum #f #g #h \u27e9\n (b * d) * (dist #f #g + dist #g #h)\n \u2261\u27e8 proj\u2081 prop.distrib (b * d) (dist #f #g) (dist #g #h)\n \u2219 ap\u2082 _+_ (ap\u2082 _*_ (prop.*-comm b d) refl\n \u2219 prop.*-assoc d b (dist #f #g)) (prop.*-assoc b d (dist #g #h))\n \u27e9\n d * (b * dist #f #g) + b * (d * dist #g #h)\n \u2264\u27e8 ((d \u220e) *-mono fg k) +-mono ((b \u220e) *-mono gh k) \u27e9\n d * (|R| * a) + b * (|R| * c)\n \u2261\u27e8 ap\u2082 _+_ (rot d |R| a) (rot b |R| c) \u2219 ! proj\u2081 prop.distrib |R| (a * d) (c * b) \u27e9\n |R| * \u2115\u2192\u211a.\u03b5N (\u03b5\u2080 +\u2115\u211a \u03b5\u2081) k\n \u220e\n where\n open \u2264-Reasoning\n rot : \u2200 x y z \u2192 x * (y * z) \u2261 y * (z * x)\n rot x y z = prop.*-comm x (y * z) \u2219 prop.*-assoc y z x\n |R| = Card (R\u1d41 k)\n #f = # (f k)\n #g = # (g k)\n #h = # (h k)\n a = \u2115\u2192\u211a.\u03b5N \u03b5\u2080 k\n b = \u2115\u2192\u211a.\u03b5D \u03b5\u2080 k\n c = \u2115\u2192\u211a.\u03b5N \u03b5\u2081 k\n d = \u2115\u2192\u211a.\u03b5D \u03b5\u2081 k\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --copatterns #-}\nopen import Algebra\n\nopen import Function\n\nopen import Data.Nat.NP\nopen import Data.Nat.Distance\nopen import Data.Nat.Properties\nopen import Data.Two\nopen import Data.Zero\nopen import Data.Product\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\n\nopen import HoTT\nopen Equivalences\n\nopen import Explore.Core\nopen import Explore.Universe.Type {\ud835\udfd8}\nopen import Explore.Universe.Base\n\nmodule Neglible where\n\nmodule prop = CommutativeSemiring commutativeSemiring\nmodule OR = Poset (DecTotalOrder.poset decTotalOrder)\n\n\u2264-*-cancel : \u2200 {x m n} \u2192 1 \u2264 x \u2192 x * m \u2264 x * n \u2192 m \u2264 n\n\u2264-*-cancel {suc x} {m} {n} (s\u2264s le) mn\n rewrite prop.*-comm (suc x) m | prop.*-comm (suc x) n = cancel-*-right-\u2264 _ _ _ mn\n\nrecord \u2115\u2192\u211a : Set where\n constructor _\/_[_]\n field\n \u03b5N : (n : \u2115) \u2192 \u2115\n \u03b5D : (n : \u2115) \u2192 \u2115\n \u03b5D-pos : \u2200 n \u2192 \u03b5D n > 0\n\nrecord Is-Neg (\u03b5 : \u2115\u2192\u211a) : Set where\n constructor mk\n open \u2115\u2192\u211a \u03b5\n field\n c\u2099 : (c : \u2115) \u2192 \u2115\n prf : \u2200(c n : \u2115) \u2192 n > c\u2099 n \u2192 n ^ c * \u03b5N n \u2264 \u03b5D n\nopen Is-Neg\n\n0\u2115\u211a : \u2115\u2192\u211a\n\u2115\u2192\u211a.\u03b5N 0\u2115\u211a _ = 0\n\u2115\u2192\u211a.\u03b5D 0\u2115\u211a _ = 1\n\u2115\u2192\u211a.\u03b5D-pos 0\u2115\u211a _ = s\u2264s z\u2264n\n\n0\u2115\u211a-neg : Is-Neg 0\u2115\u211a\nc\u2099 0\u2115\u211a-neg _ = 0\nprf 0\u2115\u211a-neg c n x = OR.trans (OR.reflexive (proj\u2082 prop.zero (n ^ c))) z\u2264n\n\n_+\u2115\u211a_ : \u2115\u2192\u211a \u2192 \u2115\u2192\u211a \u2192 \u2115\u2192\u211a\n\u2115\u2192\u211a.\u03b5N ((\u03b5N \/ \u03b5D [ _ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ _ ])) n = \u03b5N n * \u03bcD n + \u03bcN n * \u03b5D n\n\u2115\u2192\u211a.\u03b5D ((\u03b5N \/ \u03b5D [ _ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ _ ])) n = \u03b5D n * \u03bcD n\n\u2115\u2192\u211a.\u03b5D-pos ((\u03b5N \/ \u03b5D [ \u03b5D+ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ \u03bcD+ ])) n = \u03b5D+ n *-mono \u03bcD+ n\n\n\n+\u2115\u211a-neg : {\u03b5 \u03bc : \u2115\u2192\u211a} \u2192 Is-Neg \u03b5 \u2192 Is-Neg \u03bc \u2192 Is-Neg (\u03b5 +\u2115\u211a \u03bc)\nc\u2099 (+\u2115\u211a-neg \u03b5 \u03bc) n = 1 + c\u2099 \u03b5 n + c\u2099 \u03bc n\nprf (+\u2115\u211a-neg {\u03b5M} {\u03bcM} \u03b5 \u03bc) c n n>nc = \u2264-*-cancel {x = n} (OR.trans (s\u2264s z\u2264n) n>nc) lemma\n where\n\n open \u2264-Reasoning\n open \u2115\u2192\u211a \u03b5M\n open \u2115\u2192\u211a \u03bcM renaming (\u03b5N to \u03bcN; \u03b5D to \u03bcD; \u03b5D-pos to \u03bcD-pos)\n\n lemma = n * (n ^ c * (\u03b5N n * \u03bcD n + \u03bcN n * \u03b5D n))\n \u2261\u27e8 ! prop.*-assoc n (n ^ c) _\n \u2219 proj\u2081 prop.distrib (n ^ (1 + c)) (\u03b5N n * \u03bcD n) (\u03bcN n * \u03b5D n)\n \u2219 ap\u2082 _+_ (! prop.*-assoc (n ^ (1 + c)) (\u03b5N n) (\u03bcD n))\n (! (prop.*-assoc (n ^ (1 + c)) (\u03bcN n) (\u03b5D n))) \u27e9\n n ^ (1 + c) * \u03b5N n * \u03bcD n + n ^ (1 + c) * \u03bcN n * \u03b5D n\n \u2264\u27e8 (prf \u03b5 (1 + c) n (OR.trans (s\u2264s (\u2264-step (m\u2264m+n (c\u2099 \u03b5 n) (c\u2099 \u03bc n)))) n>nc) *-mono (\u03bcD n \u220e))\n +-mono (prf \u03bc (1 + c) n (OR.trans (s\u2264s (\u2264-step (n\u2264m+n (c\u2099 \u03b5 n) (c\u2099 \u03bc n)))) n>nc) *-mono (\u03b5D n \u220e)) \u27e9\n \u03b5D n * \u03bcD n + \u03bcD n * \u03b5D n\n \u2261\u27e8 ap\u2082 _+_ (refl {x = \u03b5D n * \u03bcD n}) (prop.*-comm (\u03bcD n) (\u03b5D n) \u2219 ! proj\u2082 prop.+-identity (\u03b5D n * \u03bcD n)) \u27e9\n 2 * (\u03b5D n * \u03bcD n)\n \u2264\u27e8 OR.trans (s\u2264s (s\u2264s z\u2264n)) n>nc *-mono (\u03b5D n * \u03bcD n \u220e) \u27e9\n n * (\u03b5D n * \u03bcD n)\n \u220e\n\n\nmodule _ (R\u1d41 : \u2115 \u2192 U)(let R = \u03bb n \u2192 El (R\u1d41 n)) where\n\n # : \u2200 {n} \u2192 Count (R n)\n # {n} = count (R\u1d41 n)\n\n record _~_ (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) : Set where\n constructor mk\n field\n \u03b5 : \u2115\u2192\u211a\n open \u2115\u2192\u211a \u03b5\n field\n \u03b5-neg : Is-Neg \u03b5\n bounded : \u2200 k \u2192 \u03b5D k * dist (# (f k)) (# (g k)) \u2264 Card (R\u1d41 k) * \u03b5N k\n\n\n ~-trans : Transitive _~_\n _~_.\u03b5 (~-trans x x\u2081) = _\n _~_.\u03b5-neg (~-trans x x\u2081) = +\u2115\u211a-neg (_~_.\u03b5-neg x) (_~_.\u03b5-neg x\u2081)\n _~_.bounded (~-trans {f}{g}{h}(mk \u03b5\u2080 \u03b5\u2080-neg fg) (mk \u03b5\u2081 \u03b5\u2081-neg gh)) k\n = (b * d) * dist #f #h\n \u2264\u27e8 (b * d \u220e) *-mono dist-sum #f #g #h \u27e9\n (b * d) * (dist #f #g + dist #g #h)\n \u2261\u27e8 proj\u2081 prop.distrib (b * d) (dist #f #g) (dist #g #h)\n \u2219 ap\u2082 _+_ (ap\u2082 _*_ (prop.*-comm b d) refl\n \u2219 prop.*-assoc d b (dist #f #g)) (prop.*-assoc b d (dist #g #h))\n \u27e9\n d * (b * dist #f #g) + b * (d * dist #g #h)\n \u2264\u27e8 ((d \u220e) *-mono fg k) +-mono ((b \u220e) *-mono gh k) \u27e9\n d * (|R| * a) + b * (|R| * c)\n \u2261\u27e8 ap\u2082 _+_ (rot d |R| a) (rot b |R| c) \u2219 ! proj\u2081 prop.distrib |R| (a * d) (c * b) \u27e9\n |R| * \u2115\u2192\u211a.\u03b5N (\u03b5\u2080 +\u2115\u211a \u03b5\u2081) k\n \u220e\n where\n open \u2264-Reasoning\n rot : \u2200 x y z \u2192 x * (y * z) \u2261 y * (z * x)\n rot x y z = prop.*-comm x (y * z) \u2219 prop.*-assoc y z x\n |R| = Card (R\u1d41 k)\n #f = # (f k)\n #g = # (g k)\n #h = # (h k)\n a = \u2115\u2192\u211a.\u03b5N \u03b5\u2080 k\n b = \u2115\u2192\u211a.\u03b5D \u03b5\u2080 k\n c = \u2115\u2192\u211a.\u03b5N \u03b5\u2081 k\n d = \u2115\u2192\u211a.\u03b5D \u03b5\u2081 k\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"398fc8e688c6a28e9c5c3e7f5691b7219c54a35f","subject":"more cases for #5","message":"more cases for #5\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress-checks.agda","new_file":"progress-checks.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import canonical-forms\nopen import type-assignment-unicity\n\n\n-- taken together, the theorems in this file argue that for any expression\n-- d, at most one summand of the labeled sum that results from progress may\n-- be true at any time, i.e. that values, indeterminates, errors, and\n-- expressions that step are pairwise disjoint. (note that as a consequence\n-- of currying and comutativity of products, this means that there are six\n-- theorems to prove)\nmodule progress-checks where\n -- values and indeterminates are disjoint\n vi : \u2200{d} \u2192 d val \u2192 d indet \u2192 \u22a5\n vi VConst ()\n vi VLam ()\n\n -- values and errors are disjoint\n ve : \u2200{d \u0394} \u2192 d val \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n ve VConst ()\n ve VLam ()\n\n -- values and expressions that step are disjoint\n vs : \u2200{d \u0394} \u2192 d val \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n vs VConst (d , Step (FHFinal x) () (FHFinal x\u2082))\n vs VConst (_ , Step (FHFinal x) () FHEHole)\n vs VConst (_ , Step (FHFinal x) () FHNEHoleEvaled)\n vs VConst (_ , Step (FHFinal x) () (FHNEHoleFinal x\u2082))\n vs VConst (_ , Step (FHFinal x) () (FHCastFinal x\u2082))\n vs VLam (d , Step (FHFinal x\u2081) () (FHFinal x\u2083))\n vs VLam (_ , Step (FHFinal x\u2081) () FHEHole)\n vs VLam (_ , Step (FHFinal x\u2081) () FHNEHoleEvaled)\n vs VLam (_ , Step (FHFinal x\u2081) () (FHNEHoleFinal x\u2083))\n vs VLam (_ , Step (FHFinal x\u2081) () (FHCastFinal x\u2083))\n\n -- indeterminates and errors are disjoint\n ie : \u2200{d \u0394} \u2192 d indet \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n ie IEHole ()\n ie (INEHole (FVal x)) (ENEHole e) = ve x e\n ie (INEHole (FIndet x)) (ENEHole e) = ie x e\n ie (IAp i x) (EAp1 e) = ie i e\n ie (IAp i (FVal x)) (EAp2 e) = ve x e\n ie (IAp i (FIndet x)) (EAp2 e) = ie x e\n\n\n -- todo: these are bad names\n lem2 : \u2200{d \u0394 d'} \u2192 d indet \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem2 IEHole ()\n lem2 (INEHole x) ()\n lem2 (IAp () x\u2081) (ITLam x\u2082)\n\n lem3 : \u2200{d \u0394 d'} \u2192 d val \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem3 VConst ()\n lem3 VLam ()\n\n lem1 : \u2200{d \u0394 d'} \u2192 d final \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem1 (FVal x) st = lem3 x st\n lem1 (FIndet x) st = lem2 x st\n\n -- indeterminates and expressions that step are disjoint\n is : \u2200{d \u0394} \u2192 d indet \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n is IEHole (_ , Step (FHFinal x) q _) = lem1 x q\n is IEHole (_ , Step FHEHole () (FHFinal x))\n is IEHole (_ , Step FHEHole () FHEHole)\n is IEHole (_ , Step FHEHole () FHNEHoleEvaled)\n is IEHole (_ , Step FHEHole () (FHNEHoleFinal x))\n is IEHole (_ , Step FHEHole () (FHCastFinal x))\n is (INEHole x) (_ , Step (FHFinal x\u2081) q _) = lem1 x\u2081 q\n is (INEHole x) (_ , Step FHNEHoleEvaled () (FHFinal x\u2081))\n is (INEHole x) (_ , Step FHNEHoleEvaled () FHEHole)\n is (INEHole x) (_ , Step FHNEHoleEvaled () FHNEHoleEvaled)\n is (INEHole x) (_ , Step FHNEHoleEvaled () (FHNEHoleFinal x\u2081))\n is (INEHole x) (_ , Step FHNEHoleEvaled () (FHCastFinal x\u2081))\n is (IAp i x) (_ , Step (FHFinal x\u2081) q _) = lem1 x\u2081 q\n is (IAp i (FVal x)) (_ , Step (FHAp1 x\u2081 p) q (FHAp1 x\u2082 r)) = vs x (_ , Step p q r)\n is (IAp i (FIndet x)) (_ , Step (FHAp1 x\u2081 p) q (FHAp1 x\u2082 r)) = is x (_ , Step p q r)\n is (IAp i x) (_ , Step (FHAp2 p) q (FHAp2 r)) = is i (_ , (Step p q r))\n\n\n -- final expressions are not errors (not one of the 6 cases for progress)\n fe : \u2200{d \u0394} \u2192 d final \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n fe (FVal x) er = ve x er\n fe (FIndet x) er = ie x er\n\n -- errors and expressions that step are disjoint\n es : \u2200{d \u0394} \u2192 \u0394 \u22a2 d err \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n -- cast error cases\n es (ECastError x x\u2081) (d' , Step (FHFinal x\u2082) x\u2083 x\u2084) = lem1 x\u2082 x\u2083\n es (ECastError x x\u2081) (_ , Step (FHCast x\u2082) x\u2083 (FHCast x\u2084)) = {!!}\n es (ECastError x x\u2081) (d' , Step (FHCastFinal x\u2082) (ITCast x\u2083 x\u2084 x\u2085) x\u2086)\n with type-assignment-unicity x x\u2084\n ... | refl = ~apart x\u2081 x\u2085\n\n -- ap1 cases\n es (EAp1 er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (EAp1 er) (_ , Step (FHAp1 x x\u2081) x\u2082 (FHAp1 x\u2083 x\u2084)) = fe x er\n es (EAp1 er) (_ , Step (FHAp2 x) x\u2081 (FHAp2 x\u2082)) = es er (_ , Step x x\u2081 x\u2082)\n\n -- ap2 cases\n es (EAp2 er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (EAp2 er) (_ , Step (FHAp1 x x\u2081) x\u2082 (FHAp1 x\u2083 x\u2084)) = es er (_ , Step x\u2081 x\u2082 x\u2084)\n es (EAp2 er) (_ , Step (FHAp2 x) x\u2081 (FHAp2 x\u2082)) = {!!}\n\n -- nehole cases\n es (ENEHole er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (ENEHole er) (d' , Step FHNEHoleEvaled () x\u2082)\n es (ENEHole er) (_ , Step (FHNEHoleInside x) x\u2081 (FHNEHoleInside x\u2082)) = es er (_ , Step x x\u2081 x\u2082)\n es (ENEHole er) (d' , Step (FHNEHoleFinal x) x\u2081 x\u2082) = fe x er\n\n -- castprop cases\n es (ECastProp er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (ECastProp er) (_ , Step (FHCast x) x\u2081 (FHCast x\u2082)) = es er (_ , Step x x\u2081 x\u2082)\n es (ECastProp er) (d' , Step (FHCastFinal x) x\u2081 x\u2082) = fe x er\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import canonical-forms\nopen import type-assignment-unicity\n\n\n-- taken together, the theorems in this file argue that for any expression\n-- d, at most one summand of the labeled sum that results from progress may\n-- be true at any time, i.e. that values, indeterminates, errors, and\n-- expressions that step are pairwise disjoint. (note that as a consequence\n-- of currying and comutativity of products, this means that there are six\n-- theorems to prove)\nmodule progress-checks where\n -- values and indeterminates are disjoint\n vi : \u2200{d} \u2192 d val \u2192 d indet \u2192 \u22a5\n vi VConst ()\n vi VLam ()\n\n -- values and errors are disjoint\n ve : \u2200{d \u0394} \u2192 d val \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n ve VConst ()\n ve VLam ()\n\n -- values and expressions that step are disjoint\n vs : \u2200{d \u0394} \u2192 d val \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n vs VConst (d , Step (FHFinal x) () (FHFinal x\u2082))\n vs VConst (_ , Step (FHFinal x) () FHEHole)\n vs VConst (_ , Step (FHFinal x) () FHNEHoleEvaled)\n vs VConst (_ , Step (FHFinal x) () (FHNEHoleFinal x\u2082))\n vs VConst (_ , Step (FHFinal x) () (FHCastFinal x\u2082))\n vs VLam (d , Step (FHFinal x\u2081) () (FHFinal x\u2083))\n vs VLam (_ , Step (FHFinal x\u2081) () FHEHole)\n vs VLam (_ , Step (FHFinal x\u2081) () FHNEHoleEvaled)\n vs VLam (_ , Step (FHFinal x\u2081) () (FHNEHoleFinal x\u2083))\n vs VLam (_ , Step (FHFinal x\u2081) () (FHCastFinal x\u2083))\n\n -- indeterminates and errors are disjoint\n ie : \u2200{d \u0394} \u2192 d indet \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n ie IEHole ()\n ie (INEHole (FVal x)) (ENEHole e) = ve x e\n ie (INEHole (FIndet x)) (ENEHole e) = ie x e\n ie (IAp i x) (EAp1 e) = ie i e\n ie (IAp i (FVal x)) (EAp2 e) = ve x e\n ie (IAp i (FIndet x)) (EAp2 e) = ie x e\n\n\n -- todo: these are bad names\n lem2 : \u2200{d \u0394 d'} \u2192 d indet \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem2 IEHole ()\n lem2 (INEHole x) ()\n lem2 (IAp () x\u2081) (ITLam x\u2082)\n\n lem3 : \u2200{d \u0394 d'} \u2192 d val \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem3 VConst ()\n lem3 VLam ()\n\n lem1 : \u2200{d \u0394 d'} \u2192 d final \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem1 (FVal x) st = lem3 x st\n lem1 (FIndet x) st = lem2 x st\n\n -- indeterminates and expressions that step are disjoint\n is : \u2200{d \u0394} \u2192 d indet \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n is IEHole (_ , Step (FHFinal x) q _) = lem1 x q\n is IEHole (_ , Step FHEHole () (FHFinal x))\n is IEHole (_ , Step FHEHole () FHEHole)\n is IEHole (_ , Step FHEHole () FHNEHoleEvaled)\n is IEHole (_ , Step FHEHole () (FHNEHoleFinal x))\n is IEHole (_ , Step FHEHole () (FHCastFinal x))\n is (INEHole x) (_ , Step (FHFinal x\u2081) q _) = lem1 x\u2081 q\n is (INEHole x) (_ , Step FHNEHoleEvaled () (FHFinal x\u2081))\n is (INEHole x) (_ , Step FHNEHoleEvaled () FHEHole)\n is (INEHole x) (_ , Step FHNEHoleEvaled () FHNEHoleEvaled)\n is (INEHole x) (_ , Step FHNEHoleEvaled () (FHNEHoleFinal x\u2081))\n is (INEHole x) (_ , Step FHNEHoleEvaled () (FHCastFinal x\u2081))\n is (IAp i x) (_ , Step (FHFinal x\u2081) q _) = lem1 x\u2081 q\n is (IAp i (FVal x)) (_ , Step (FHAp1 x\u2081 p) q (FHAp1 x\u2082 r)) = vs x (_ , Step p q r)\n is (IAp i (FIndet x)) (_ , Step (FHAp1 x\u2081 p) q (FHAp1 x\u2082 r)) = is x (_ , Step p q r)\n is (IAp i x) (_ , Step (FHAp2 p) q (FHAp2 r)) = is i (_ , (Step p q r))\n\n\n -- final expressions are not errors (not one of the 6 cases for progress)\n fe : \u2200{d \u0394} \u2192 d final \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n fe (FVal x) er = ve x er\n fe (FIndet x) er = ie x er\n\n -- errors and expressions that step are disjoint\n es : \u2200{d \u0394} \u2192 \u0394 \u22a2 d err \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n -- cast error cases\n es (ECastError x x\u2081) (d' , Step (FHFinal x\u2082) x\u2083 x\u2084) = lem1 x\u2082 x\u2083\n es (ECastError x x\u2081) (_ , Step (FHCast x\u2082) x\u2083 (FHCast x\u2084)) = {!!}\n es (ECastError x x\u2081) (d' , Step (FHCastFinal x\u2082) (ITCast x\u2083 x\u2084 x\u2085) x\u2086)\n with type-assignment-unicity x x\u2084\n ... | refl = ~apart x\u2081 x\u2085\n\n -- ap1 cases\n es (EAp1 er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (EAp1 er) (_ , Step (FHAp1 x x\u2081) x\u2082 (FHAp1 x\u2083 x\u2084)) = fe x er\n es (EAp1 er) (_ , Step (FHAp2 x) x\u2081 (FHAp2 x\u2082)) = es er (_ , Step x x\u2081 x\u2082)\n\n -- ap2 cases\n es (EAp2 er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (EAp2 er) (_ , Step (FHAp1 x x\u2081) x\u2082 (FHAp1 x\u2083 x\u2084)) = es er (_ , Step x\u2081 x\u2082 x\u2084)\n es (EAp2 er) (d' , Step (FHAp2 x) x\u2081 x\u2082) = {!!}\n\n -- nehole cases\n es (ENEHole er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (ENEHole er) (d' , Step FHNEHoleEvaled x\u2081 x\u2082) = {!!}\n es (ENEHole er) (d' , Step (FHNEHoleInside x) x\u2081 x\u2082) = {!!}\n es (ENEHole er) (d' , Step (FHNEHoleFinal x) x\u2081 x\u2082) = {!!}\n\n -- castprop cases\n es (ECastProp er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (ECastProp er) (d' , Step (FHCast x) x\u2081 x\u2082) = {!!}\n es (ECastProp er) (d' , Step (FHCastFinal x) x\u2081 x\u2082) = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"56068be9d5f2204763d86c0e8e47cf811b5878f7","subject":"Removed unnecesary hint.","message":"Removed unnecesary hint.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Program\/Collatz\/PropertiesATP.agda","new_file":"src\/fot\/FOTC\/Program\/Collatz\/PropertiesATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Properties of the Collatz function\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Program.Collatz.PropertiesATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.TotalityATP\nopen import FOTC.Program.Collatz.Collatz\nopen import FOTC.Program.Collatz.Data.Nat\nopen import FOTC.Program.Collatz.Data.Nat.PropertiesATP\n\n------------------------------------------------------------------------------\n\nhelper-helper : \u2200 {n} \u2192 N n \u2192 collatz ([2] ^ succ\u2081 n) \u2261\n collatz (div ([2] ^ succ\u2081 n) [2])\nhelper-helper nzero = prf\n where postulate prf : collatz ([2] ^ succ\u2081 zero) \u2261\n collatz (div ([2] ^ succ\u2081 zero) [2])\n {-# ATP prove prf #-}\n\nhelper-helper (nsucc {n} Nn) = prf\n where postulate prf : collatz ([2] ^ succ\u2081 (succ\u2081 n)) \u2261\n collatz (div ([2] ^ succ\u2081 (succ\u2081 n)) [2])\n {-# ATP prove prf +\u22382 ^-N 2-N 2^x\u22620 2^[x+1]\u22621\n x-Even\u2192SSx-Even \u2238-N \u2238-Even 2^[x+1]-Even 2-Even\n #-}\n\n-- We help the ATPs proving the helper-helper property first.\npostulate helper : \u2200 {n} \u2192 N n \u2192 collatz ([2] ^ succ\u2081 n) \u2261 collatz ([2] ^ n)\n{-# ATP prove helper helper-helper div-2^[x+1]-2\u22612^x #-}\n\ncollatz-2^x : \u2200 {n} \u2192 N n \u2192 collatz ([2] ^ n) \u2261 [1]\ncollatz-2^x nzero = prf\n where postulate prf : collatz ([2] ^ [0]) \u2261 [1]\n {-# ATP prove prf #-}\n\ncollatz-2^x (nsucc {n} Nn) = prf (collatz-2^x Nn)\n where postulate prf : collatz ([2] ^ n) \u2261 [1] \u2192 collatz ([2] ^ succ\u2081 n) \u2261 [1]\n {-# ATP prove prf helper #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Properties of the Collatz function\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Program.Collatz.PropertiesATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.TotalityATP\nopen import FOTC.Program.Collatz.Collatz\nopen import FOTC.Program.Collatz.Data.Nat\nopen import FOTC.Program.Collatz.Data.Nat.PropertiesATP\n\n------------------------------------------------------------------------------\n\nhelper-helper : \u2200 {n} \u2192 N n \u2192 collatz ([2] ^ succ\u2081 n) \u2261\n collatz (div ([2] ^ succ\u2081 n) [2])\nhelper-helper nzero = prf\n where postulate prf : collatz ([2] ^ succ\u2081 zero) \u2261\n collatz (div ([2] ^ succ\u2081 zero) [2])\n {-# ATP prove prf #-}\n\nhelper-helper (nsucc {n} Nn) = prf\n where postulate prf : collatz ([2] ^ succ\u2081 (succ\u2081 n)) \u2261\n collatz (div ([2] ^ succ\u2081 (succ\u2081 n)) [2])\n {-# ATP prove prf +\u22382 ^-N 2-N 2^x\u22620 2^[x+1]\u22621\n collatz-even x-Even\u2192SSx-Even \u2238-N \u2238-Even 2^[x+1]-Even\n 2-Even\n #-}\n\n-- We help the ATPs proving the helper-helper property first.\npostulate helper : \u2200 {n} \u2192 N n \u2192 collatz ([2] ^ succ\u2081 n) \u2261 collatz ([2] ^ n)\n{-# ATP prove helper helper-helper div-2^[x+1]-2\u22612^x #-}\n\ncollatz-2^x : \u2200 {n} \u2192 N n \u2192 collatz ([2] ^ n) \u2261 [1]\ncollatz-2^x nzero = prf\n where postulate prf : collatz ([2] ^ [0]) \u2261 [1]\n {-# ATP prove prf #-}\n\ncollatz-2^x (nsucc {n} Nn) = prf (collatz-2^x Nn)\n where postulate prf : collatz ([2] ^ n) \u2261 [1] \u2192 collatz ([2] ^ succ\u2081 n) \u2261 [1]\n {-# ATP prove prf helper #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"6d4a90d69f37d2853048dcfc6038e4e79f9c1db6","subject":"agda\/total.agda: Sketch symbolic derivation for if","message":"agda\/total.agda: Sketch symbolic derivation for if\n\nI left some holes for helper functions, and didn't prove correctness\nyet.\n\nOld-commit-hash: 4cbf58539c1fd77a425abf1e609db4a03e1d8c0f\n","repos":"inc-lc\/ilc-agda","old_file":"total.agda","new_file":"total.agda","new_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nimport Relation.Binary as B\n\nopen import Relation.Binary using\n (IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)\nimport Relation.Binary.EqReasoning as EqR\n\nopen import Relation.Nullary using (\u00ac_)\n\nopen import meaning\n\n-- SIMPLE TYPES\n\n-- Syntax\n\ndata Type : Set where\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n bool : Type\n\ninfixr 5 _\u21d2_\n\n-- Denotational Semantics\n\ndata Bool : Set where\n true false : Bool\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 bool \u27e7Type = Bool\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\n-- Value Equivalence\n\ndata _\u2261_ : \u2200 {\u03c4} \u2192 (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192 Set where\n ext : \u2200 {\u03c4\u2081 \u03c4\u2082} {f\u2081 f\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} \u2192\n (\u2200 v \u2192 f\u2081 v \u2261 f\u2082 v) \u2192\n f\u2081 \u2261 f\u2082\n bool : \u2200 {b : Bool} \u2192\n b \u2261 b\n\n\u2261-refl : \u2200 {\u03c4} {v : \u27e6 \u03c4 \u27e7} \u2192\n v \u2261 v\n\u2261-refl {\u03c4\u2081 \u21d2 \u03c4\u2082} = ext (\u03bb v \u2192 \u2261-refl)\n\u2261-refl {bool} = bool\n\n\u2261-sym : \u2200 {\u03c4} {v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2081\n\u2261-sym {\u03c4\u2081 \u21d2 \u03c4\u2082} (ext \u2261) = ext (\u03bb v \u2192 \u2261-sym (\u2261 v))\n\u2261-sym {bool} bool = bool\n\n\u2261-trans : \u2200 {\u03c4} {v\u2081 v\u2082 v\u2083 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2083 \u2192 v\u2081 \u2261 v\u2083\n\u2261-trans {\u03c4\u2081 \u21d2 \u03c4\u2082} {f} (ext \u2261\u2081) (ext \u2261\u2082) =\n ext (\u03bb v \u2192 \u2261-trans (\u2261\u2081 v) (\u2261\u2082 v))\n\u2261-trans {bool} bool bool = bool\n\n\u2261-cong : \u2200 {\u03c4\u2082 \u03c4\u2081 v\u2081 v\u2082} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 f v\u2081 \u2261 f v\u2082\n\u2261-cong {\u03c4\u2081 \u21d2 \u03c4\u2082} f \u2261 = ext (\u03bb v \u2192 \u2261-cong (\u03bb x \u2192 f x v) \u2261)\n--\u2261-cong {bool} {bool} {v\u2081} f bool = bool\n\u2261-cong {bool} {bool} f bool = bool\n\u2261-cong {bool} {\u03c4\u2083 \u21d2 \u03c4\u2084} {v\u2081} {v\u2082} f (ext \u2261\u2081) = {!!}\n\n\u2261-cong\u2082 : \u2200 {\u03c4\u2083 \u03c4\u2081 \u03c4\u2082 v\u2081 v\u2082 v\u2083 v\u2084} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7 \u2192 \u27e6 \u03c4\u2083 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 f v\u2081 v\u2083 \u2261 f v\u2082 v\u2084\n\u2261-cong\u2082 {\u03c4\u2081 \u21d2 \u03c4\u2082} f \u2261\u2081 \u2261\u2082 = ext (\u03bb v \u2192 \u2261-cong\u2082 (\u03bb x y \u2192 f x y v) \u2261\u2081 \u2261\u2082)\n\u2261-cong\u2082 {bool} {bool} {bool} f bool bool = bool\n\u2261-cong\u2082 {bool} {bool} {\u03c4\u2082 \u21d2 \u03c4\u2083} f bool (ext \u2261\u2082) = {!!}\n\u2261-cong\u2082 {bool} {\u03c4\u2081 \u21d2 \u03c4\u2082} {bool} f (ext \u2261\u2081) (bool) = {!!}\n\u2261-cong\u2082 {bool} {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u03c4\u2083 \u21d2 \u03c4\u2084} f (ext \u2261\u2081) (ext \u2261\u2082) = {!!}\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = \u2261-cong\u2082 (\u03bb x y \u2192 x y)\n\n\u2261-isEquivalence : \u2200 {\u03c4} \u2192 IsEquivalence (_\u2261_ {\u03c4})\n\u2261-isEquivalence = record\n { refl = \u2261-refl\n ; sym = \u2261-sym\n ; trans = \u2261-trans\n }\n\n\u2261-setoid : Type \u2192 Setoid _ _\n\u2261-setoid \u03c4 = record\n { Carrier = \u27e6 \u03c4 \u27e7\n ; _\u2248_ = _\u2261_\n ; isEquivalence = \u2261-isEquivalence\n }\n\nmodule \u2261-Reasoning where\n module _ {\u03c4 : Type} where\n open EqR (\u2261-setoid \u03c4) public\n hiding (_\u2261\u27e8_\u27e9_) renaming (_\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_)\n\n\u2261-consistent : \u00ac (\u2200 (\u03c4 : Type) \u2192 (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192 v\u2081 \u2261 v\u2082)\n\u2261-consistent H with H bool true false\n... | ()\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\u0394-Type (bool) = bool -- true means negate, false means nil change\n\nderive : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\napply : \u2200 {\u03c4} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\ndiff : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ndiff {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 diff (f\u2081 (apply dv v)) (f\u2082 v)\ndiff {bool} true true = false\ndiff {bool} true false = true\ndiff {bool} false true = true\ndiff {bool} false false = false\n\nderive {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 diff (f (apply dv v)) (f v)\nderive {bool} b = false\n\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} df f = \u03bb v \u2192 apply (df v (derive v)) (f v)\napply {bool} true true = false\napply {bool} true false = true\napply {bool} false true = true\napply {bool} false false = false\n\ncompose : \u2200 {\u03c4} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} df\u2081 df\u2082 = \u03bb v dv \u2192 compose (df\u2081 v dv) (df\u2082 v dv)\ncompose {bool} true true = false\ncompose {bool} true false = true\ncompose {bool} false true = true\ncompose {bool} false false = false\n\n-- CONGRUENCE rules for change operations\n\n\u2261-diff : \u2200 {\u03c4 : Type} {v\u2081 v\u2082 v\u2083 v\u2084 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 diff v\u2081 v\u2083 \u2261 diff v\u2082 v\u2084\n\u2261-diff = \u2261-cong\u2082 diff\n\n\u2261-apply : \u2200 {\u03c4 : Type} {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} {v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 v\u2081 \u2261 v\u2082 \u2192 apply dv\u2081 v\u2081 \u2261 apply dv\u2082 v\u2082\n\u2261-apply = \u2261-cong\u2082 apply\n\n-- PROPERTIES of changes\n\ndiff-derive : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192\n diff v v \u2261 derive v\ndiff-derive {\u03c4\u2081 \u21d2 \u03c4\u2082} v = \u2261-refl\ndiff-derive {bool} true = bool\ndiff-derive {bool} false = bool\n\ndiff-apply : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n diff (apply dv v) v \u2261 dv\ndiff-apply {\u03c4\u2081 \u21d2 \u03c4\u2082} df f = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n {!!}))\ndiff-apply {bool} true true = bool\ndiff-apply {bool} true false = bool\ndiff-apply {bool} false true = bool\ndiff-apply {bool} false false = bool\n\napply-diff : \u2200 {\u03c4} (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192\n apply (diff v\u2082 v\u2081) v\u2081 \u2261 v\u2082\n\napply-derive : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192\n apply (derive v) v \u2261 v\n\napply-diff {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = ext (\u03bb v \u2192\n begin\n apply (diff f\u2082 f\u2081) f\u2081 v\n \u2261\u27e8 \u2261-refl \u27e9\n apply (diff (f\u2082 (apply (derive v) v)) (f\u2081 v)) (f\u2081 v)\n \u2261\u27e8 \u2261-apply (\u2261-diff (\u2261-cong f\u2082 (apply-derive v)) \u2261-refl) \u2261-refl \u27e9\n apply (diff (f\u2082 v) (f\u2081 v)) (f\u2081 v)\n \u2261\u27e8 apply-diff (f\u2081 v) (f\u2082 v) \u27e9\n f\u2082 v\n \u220e) where open \u2261-Reasoning\napply-diff {bool} true true = bool\napply-diff {bool} true false = bool\napply-diff {bool} false true = bool\napply-diff {bool} false false = bool\n\napply-derive {\u03c4\u2081 \u21d2 \u03c4\u2082} f = ext (\u03bb v \u2192\n begin\n apply (derive f) f v\n \u2261\u27e8 \u2261-refl \u27e9\n apply (diff (f (apply (derive v) v)) (f v)) (f v)\n \u2261\u27e8 \u2261-cong (\u03bb x \u2192 apply (diff (f x) (f v)) (f v)) (apply-derive v) \u27e9\n apply (diff (f v) (f v)) (f v)\n \u2261\u27e8 apply-diff (f v) (f v)\u27e9\n f v\n \u220e) where open \u2261-Reasoning\napply-derive {bool} true = bool\napply-derive {bool} false = bool\n\napply-compose : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) (dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n apply (compose dv\u2081 dv\u2082) v \u2261 apply dv\u2081 (apply dv\u2082 v)\napply-compose {\u03c4\u2081 \u21d2 \u03c4\u2082} f df\u2081 df\u2082 = ext (\u03bb v \u2192\n apply-compose (f v) (df\u2081 v (derive v)) (df\u2082 v (derive v)))\napply-compose {bool} true true true = bool\napply-compose {bool} true true false = bool\napply-compose {bool} true false true = bool\napply-compose {bool} true false false = bool\napply-compose {bool} false true true = bool\napply-compose {bool} false true false = bool\napply-compose {bool} false false true = bool\napply-compose {bool} false false false = bool\n\ncompose-assoc : \u2200 {\u03c4} (dv\u2081 dv\u2082 dv\u2083 : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n compose dv\u2081 (compose dv\u2082 dv\u2083) \u2261 compose (compose dv\u2081 dv\u2082) dv\u2083\ncompose-assoc {\u03c4\u2081 \u21d2 \u03c4\u2082} df\u2081 df\u2082 df\u2083 = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n compose-assoc (df\u2081 v dv) (df\u2082 v dv) (df\u2083 v dv)))\ncompose-assoc {bool} true true true = bool\ncompose-assoc {bool} true true false = bool\ncompose-assoc {bool} true false true = bool\ncompose-assoc {bool} true false false = bool\ncompose-assoc {bool} false true true = bool\ncompose-assoc {bool} false true false = bool\ncompose-assoc {bool} false false true = bool\ncompose-assoc {bool} false false false = bool\n\n-- TYPING CONTEXTS, VARIABLES and WEAKENING\n\nopen import binding Type \u27e6_\u27e7Type\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393\n\nupdate : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 \u03c1) = apply dv v \u2022 update \u03c1\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore {\u2205} \u2205 = \u2205\nignore {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 \u03c1) = v \u2022 ignore \u03c1\n\n\u0394-Context\u2032 : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\n\u0394-Context\u2032 \u0393 \u2205 = \u0394-Context \u0393\n\u0394-Context\u2032 (.\u03c4 \u2022 \u0393) (\u03c4 \u2022 \u0393\u2032) = \u03c4 \u2022 \u0394-Context\u2032 \u0393 \u0393\u2032\n\nupdate\u2032 : \u2200 {\u0393} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nupdate\u2032 \u2205 \u03c1 = update \u03c1\nupdate\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) = v \u2022 update\u2032 \u0393\u2032 \u03c1\n\nignore\u2032 : \u2200 {\u0393} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore\u2032 \u2205 \u03c1 = ignore \u03c1\nignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) = v \u2022 ignore\u2032 \u0393\u2032 \u03c1\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n true false : \u2200 {\u0393} \u2192 Term \u0393 bool\n if : \u2200 {\u0393 \u03c4} \u2192 (t\u2081 : Term \u0393 bool) (t\u2082 t\u2083 : Term \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n -- `\u0394 t` describes how t changes if its free variables or arguments change\n \u0394 : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n_and_ : \u2200 {\u0393} \u2192 Term \u0393 bool \u2192 Term \u0393 bool \u2192 Term \u0393 bool\na and b = ?\n\n!_ : \u2200 {\u0393} \u2192 Term \u0393 bool \u2192 Term \u0393 bool\n! x = ?\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 true \u27e7Term \u03c1 = true\n\u27e6 false \u27e7Term \u03c1 = false\n\u27e6 if t\u2081 t\u2082 t\u2083 \u27e7Term \u03c1 with \u27e6 t\u2081 \u27e7Term \u03c1\n... | true = \u27e6 t\u2082 \u27e7Term \u03c1\n... | false = \u27e6 t\u2083 \u27e7Term \u03c1\n\u27e6 \u0394 t \u27e7Term \u03c1 = diff (\u27e6 t \u27e7Term (update \u03c1)) (\u27e6 t \u27e7Term (ignore \u03c1))\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n-- Term Equivalence\n\nmodule _ {\u0393} {\u03c4} where\n data _\u2248_ (t\u2081 t\u2082 : Term \u0393 \u03c4) : Set where\n ext :\n (\u2200 \u03c1 \u2192 \u27e6 t\u2081 \u27e7 \u03c1 \u2261 \u27e6 t\u2082 \u27e7 \u03c1) \u2192\n t\u2081 \u2248 t\u2082\n\n \u2248-refl : Reflexive _\u2248_\n \u2248-refl = ext (\u03bb \u03c1 \u2192 \u2261-refl)\n\n \u2248-sym : Symmetric _\u2248_\n \u2248-sym (ext \u2248) = ext (\u03bb \u03c1 \u2192 \u2261-sym (\u2248 \u03c1))\n\n \u2248-trans : Transitive _\u2248_\n \u2248-trans (ext \u2248\u2081) (ext \u2248\u2082) = ext (\u03bb \u03c1 \u2192 \u2261-trans (\u2248\u2081 \u03c1) (\u2248\u2082 \u03c1))\n\n \u2248-isEquivalence : IsEquivalence _\u2248_\n \u2248-isEquivalence = record\n { refl = \u2248-refl\n ; sym = \u2248-sym\n ; trans = \u2248-trans\n }\n\n\u2248-setoid : Context \u2192 Type \u2192 Setoid _ _\n\u2248-setoid \u0393 \u03c4 = record\n { Carrier = Term \u0393 \u03c4\n ; _\u2248_ = _\u2248_\n ; isEquivalence = \u2248-isEquivalence\n }\n\n\u2248-app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} {t\u2081 t\u2082 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2083 t\u2084 : Term \u0393 \u03c4\u2081} \u2192\n t\u2081 \u2248 t\u2082 \u2192 t\u2083 \u2248 t\u2084 \u2192 app t\u2081 t\u2083 \u2248 app t\u2082 t\u2084\n\u2248-app (ext \u2248\u2081) (ext \u2248\u2082) = ext (\u03bb \u03c1 \u2192\n \u2261-cong\u2082 (\u03bb x y \u2192 x y) (\u2248\u2081 \u03c1) (\u2248\u2082 \u03c1))\n\n\u2248-abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} {t\u2081 t\u2082 : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n t\u2081 \u2248 t\u2082 \u2192 abs t\u2081 \u2248 abs t\u2082\n\u2248-abs (ext \u2248) = ext (\u03bb \u03c1 \u2192\n ext (\u03bb v \u2192 \u2248 (v \u2022 \u03c1)))\n\n\u2248-\u0394 : \u2200 {\u03c4 \u0393} {t\u2081 t\u2082 : Term \u0393 \u03c4} \u2192\n t\u2081 \u2248 t\u2082 \u2192 \u0394 t\u2081 \u2248 \u0394 t\u2082\n\u2248-\u0394 (ext \u2248) = ext (\u03bb \u03c1 \u2192 \u2261-diff (\u2248 (update \u03c1)) (\u2248 (ignore \u03c1)))\n\nmodule \u2248-Reasoning where\n module _ {\u0393 : Context} {\u03c4 : Type} where\n open EqR (\u2248-setoid \u0393 \u03c4) public\n hiding (_\u2261\u27e8_\u27e9_)\n\n\u2248-consistent : \u00ac (\u2200 {\u0393 \u03c4} (t\u2081 t\u2082 : Term \u0393 \u03c4) \u2192 t\u2081 \u2248 t\u2082)\n\u2248-consistent H with H {\u2205} true false\n... | ext x with x \u2205\n... | ()\n\n-- LIFTING terms into \u0394-Contexts\n\nlift-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) \u03c4\nlift-var this = that this\nlift-var (that x) = that (that (lift-var x))\n\nlift-var\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-var\u2032 \u2205 x = lift-var x\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) this = this\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) (that x) = that (lift-var\u2032 \u0393\u2032 x)\n\nlift-term\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-term\u2032 \u0393\u2032 (abs t) = abs (lift-term\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term\u2032 \u0393\u2032 (app t\u2081 t\u2082) = app (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082)\nlift-term\u2032 \u0393\u2032 (var x) = var (lift-var\u2032 \u0393\u2032 x)\nlift-term\u2032 \u0393\u2032 true = true\nlift-term\u2032 \u0393\u2032 false = false\nlift-term\u2032 \u0393\u2032 (if t\u2081 t\u2082 t\u2083) = if (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082) (lift-term\u2032 \u0393\u2032 t\u2083)\nlift-term\u2032 \u0393\u2032 (\u0394 t) = {!!}\n\nlift-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) \u03c4\nlift-term = lift-term\u2032 \u2205\n\n-- PROPERTIES of lift-term\n\nlift-var-ignore : \u2200 {\u0393 \u03c4} (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore \u03c1)\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) (that x) = lift-var-ignore \u03c1 x\n\nlift-var-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) (\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var\u2032 \u0393\u2032 x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-var-ignore\u2032 \u2205 \u03c1 x = lift-var-ignore \u03c1 x\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) (that x) = lift-var-ignore\u2032 \u0393\u2032 \u03c1 x\n\nlift-term-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) {\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term\u2032 \u0393\u2032 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-term-ignore\u2032 \u0393\u2032 (abs t) =\n ext (\u03bb v \u2192 lift-term-ignore\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term-ignore\u2032 \u0393\u2032 (app t\u2081 t\u2082) =\n \u2261-app (lift-term-ignore\u2032 \u0393\u2032 t\u2081) (lift-term-ignore\u2032 \u0393\u2032 t\u2082)\nlift-term-ignore\u2032 \u0393\u2032 (var x) = lift-var-ignore\u2032 \u0393\u2032 _ x\nlift-term-ignore\u2032 \u0393\u2032 true = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 false = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 {\u03c1} (if t\u2081 t\u2082 t\u2083)\n with \u27e6 lift-term\u2032 \u0393\u2032 t\u2081 \u27e7 \u03c1\n | \u27e6 t\u2081 \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\n | lift-term-ignore\u2032 \u0393\u2032 {\u03c1} t\u2081\n... | true | true | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2082\n... | false | false | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2083\nlift-term-ignore\u2032 \u0393\u2032 (\u0394 t) = {!!}\n\nlift-term-ignore : \u2200 {\u0393 \u03c4} {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore \u03c1)\nlift-term-ignore = lift-term-ignore\u2032 \u2205\n\n\n-- PROPERTIES of \u0394\n\n\u0394-abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192\n \u0394 (abs t) \u2248 abs (abs (\u0394 t))\n\u0394-abs t = ext (\u03bb \u03c1 \u2192 \u2261-refl)\n\n\u0394-app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192\n \u0394 (app t\u2081 t\u2082) \u2248 app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n\u0394-app t\u2081 t\u2082 = \u2248-sym (ext (\u03bb \u03c1 \u2192\n begin\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 lift-term t\u2082 \u27e7 \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 lift-term t\u2082 \u27e7 \u03c1))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))) x))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) x))\n (lift-term-ignore {\u03c1 = \u03c1} t\u2082) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff (\u27e6 t\u2081 \u27e7 (update \u03c1) x) (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (apply-diff (\u27e6 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 t\u2082 \u27e7 (update \u03c1))) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u220e)) where open \u2261-Reasoning\n\n-- SYMBOLIC DERIVATION\n\nderive-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-var this = this\nderive-var (that x) = that (that (derive-var x))\n\ndiff-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394-Type \u03c4)\ndiff-term = {!!}\n\napply-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 (\u0394-Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4\napply-term = {!!}\n\nderive-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-term (abs t) = abs (abs (derive-term t))\nderive-term (app t\u2081 t\u2082) = app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\nderive-term (var x) = var (derive-var x)\nderive-term true = false\nderive-term false = false\nderive-term (if c t e) =\n if ((derive-term c) and (lift-term c))\n (diff-term (apply-term (derive-term e) (lift-term e)) (lift-term t))\n (if ((derive-term c) and (lift-term (! c)))\n (diff-term (apply-term (derive-term t) (lift-term t)) (lift-term e))\n (if (lift-term c)\n (derive-term t)\n (derive-term e)))\n\nderive-term (\u0394 t) = \u0394 (derive-term t)\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n \u0394 t \u2248 derive-term t\nderive-term-correct {\u0393} (abs t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct t)) \u27e9\n abs (abs (derive-term t))\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (abs t)\n \u220e where open \u2248-Reasoning\nderive-term-correct (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct t\u2081) \u2248-refl) (derive-term-correct t\u2082) \u27e9\n app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct (var x) = ext (\u03bb \u03c1 \u2192 derive-var-correct \u03c1 x)\nderive-term-correct true = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\n\n-- NATURAL SEMANTICS\n\n-- (without support for \u0394 for now)\n\n-- Syntax\n\ndata Env : Context \u2192 Set\ndata Val : Type \u2192 Set\n\ndata Val where\n \u27e8abs_,_\u27e9 : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) (\u03c1 : Env \u0393) \u2192 Val (\u03c4\u2081 \u21d2 \u03c4\u2082)\n\ndata Env where\n \u2205 : Env \u2205\n _\u2022_ : \u2200 {\u0393 \u03c4} \u2192 Val \u03c4 \u2192 Env \u0393 \u2192 Env (\u03c4 \u2022 \u0393)\n\n-- Lookup\n\ninfixr 8 _\u22a2_\u2193_ _\u22a2_\u21a6_\n\ndata _\u22a2_\u21a6_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Var \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n this : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {v : Val \u03c4} \u2192\n v \u2022 \u03c1 \u22a2 this \u21a6 v\n that : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 x} {\u03c1 : Env \u0393} {v\u2081 : Val \u03c4\u2081} {v\u2082 : Val \u03c4\u2082} \u2192\n \u03c1 \u22a2 x \u21a6 v\u2082 \u2192\n v\u2081 \u2022 \u03c1 \u22a2 that x \u21a6 v\u2082\n\n-- Reduction\n\ndata _\u22a2_\u2193_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Term \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c1} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n \u03c1 \u22a2 abs t \u2193 \u27e8abs t , \u03c1 \u27e9\n app : \u2200 {\u0393 \u0393\u2032 \u03c4\u2081 \u03c4\u2082 \u03c1 \u03c1\u2032 v\u2082 v\u2032} {t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2082 : Term \u0393 \u03c4\u2081} {t\u2032 : Term (\u03c4\u2081 \u2022 \u0393\u2032) \u03c4\u2082} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 \u27e8abs t\u2032 , \u03c1\u2032 \u27e9 \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n v\u2082 \u2022 \u03c1\u2032 \u22a2 t\u2032 \u2193 v\u2032 \u2192\n \u03c1 \u22a2 app t\u2081 t\u2082 \u2193 v\u2032\n var : \u2200 {\u0393 \u03c4 x} {\u03c1 : Env \u0393} {v : Val \u03c4}\u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u03c1 \u22a2 var x \u2193 v\n\n-- SOUNDNESS of natural semantics\n\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 Env \u0393 \u2192 \u27e6 \u0393 \u27e7\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 \u27e8abs t , \u03c1 \u27e9 \u27e7Val = \u03bb v \u2192 \u27e6 t \u27e7 (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\nmeaningOfEnv : \u2200 {\u0393} \u2192 Meaning (Env \u0393)\nmeaningOfEnv = meaning \u27e6_\u27e7Env\n\nmeaningOfVal : \u2200 {\u03c4} \u2192 Meaning (Val \u03c4)\nmeaningOfVal = meaning \u27e6_\u27e7Val\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = \u2261-refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = \u2261-refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) =\n \u2261-trans\n (\u2261-cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082))\n (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n\n-- WEAKENING\n\n-- Weaken a term to a super context\n\n{-\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken {\u0393\u2081} {\u0393\u2082} (abs {\u03c4\u2081 = \u03c4} t) = abs (weaken {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} t)\nweaken {\u0393\u2081} {\u0393\u2082} (app t\u2081 t\u2082) = app (weaken {\u0393\u2081} {\u0393\u2082} t\u2081) (weaken {\u0393\u2081} {\u0393\u2082} t\u2082)\nweaken {\u0393\u2081} {\u0393\u2082} (var x) = var (lift {\u0393\u2081} {\u0393\u2082} x)\nweaken {\u0393\u2081} {\u0393\u2082} (\u0394 t) = ?\n-}\n","old_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nimport Relation.Binary as B\n\nopen import Relation.Binary using\n (IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)\nimport Relation.Binary.EqReasoning as EqR\n\nopen import Relation.Nullary using (\u00ac_)\n\nopen import meaning\n\n-- SIMPLE TYPES\n\n-- Syntax\n\ndata Type : Set where\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n bool : Type\n\ninfixr 5 _\u21d2_\n\n-- Denotational Semantics\n\ndata Bool : Set where\n true false : Bool\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 bool \u27e7Type = Bool\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\n-- Value Equivalence\n\ndata _\u2261_ : \u2200 {\u03c4} \u2192 (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192 Set where\n ext : \u2200 {\u03c4\u2081 \u03c4\u2082} {f\u2081 f\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} \u2192\n (\u2200 v \u2192 f\u2081 v \u2261 f\u2082 v) \u2192\n f\u2081 \u2261 f\u2082\n bool : \u2200 {b : Bool} \u2192\n b \u2261 b\n\n\u2261-refl : \u2200 {\u03c4} {v : \u27e6 \u03c4 \u27e7} \u2192\n v \u2261 v\n\u2261-refl {\u03c4\u2081 \u21d2 \u03c4\u2082} = ext (\u03bb v \u2192 \u2261-refl)\n\u2261-refl {bool} = bool\n\n\u2261-sym : \u2200 {\u03c4} {v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2081\n\u2261-sym {\u03c4\u2081 \u21d2 \u03c4\u2082} (ext \u2261) = ext (\u03bb v \u2192 \u2261-sym (\u2261 v))\n\u2261-sym {bool} bool = bool\n\n\u2261-trans : \u2200 {\u03c4} {v\u2081 v\u2082 v\u2083 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2083 \u2192 v\u2081 \u2261 v\u2083\n\u2261-trans {\u03c4\u2081 \u21d2 \u03c4\u2082} {f} (ext \u2261\u2081) (ext \u2261\u2082) =\n ext (\u03bb v \u2192 \u2261-trans (\u2261\u2081 v) (\u2261\u2082 v))\n\u2261-trans {bool} bool bool = bool\n\n\u2261-cong : \u2200 {\u03c4\u2082 \u03c4\u2081 v\u2081 v\u2082} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 f v\u2081 \u2261 f v\u2082\n\u2261-cong {\u03c4\u2081 \u21d2 \u03c4\u2082} f \u2261 = ext (\u03bb v \u2192 \u2261-cong (\u03bb x \u2192 f x v) \u2261)\n--\u2261-cong {bool} {bool} {v\u2081} f bool = bool\n\u2261-cong {bool} {bool} f bool = bool\n\u2261-cong {bool} {\u03c4\u2083 \u21d2 \u03c4\u2084} {v\u2081} {v\u2082} f (ext \u2261\u2081) = {!!}\n\n\u2261-cong\u2082 : \u2200 {\u03c4\u2083 \u03c4\u2081 \u03c4\u2082 v\u2081 v\u2082 v\u2083 v\u2084} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7 \u2192 \u27e6 \u03c4\u2083 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 f v\u2081 v\u2083 \u2261 f v\u2082 v\u2084\n\u2261-cong\u2082 {\u03c4\u2081 \u21d2 \u03c4\u2082} f \u2261\u2081 \u2261\u2082 = ext (\u03bb v \u2192 \u2261-cong\u2082 (\u03bb x y \u2192 f x y v) \u2261\u2081 \u2261\u2082)\n\u2261-cong\u2082 {bool} {bool} {bool} f bool bool = bool\n\u2261-cong\u2082 {bool} {bool} {\u03c4\u2082 \u21d2 \u03c4\u2083} f bool (ext \u2261\u2082) = {!!}\n\u2261-cong\u2082 {bool} {\u03c4\u2081 \u21d2 \u03c4\u2082} {bool} f (ext \u2261\u2081) (bool) = {!!}\n\u2261-cong\u2082 {bool} {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u03c4\u2083 \u21d2 \u03c4\u2084} f (ext \u2261\u2081) (ext \u2261\u2082) = {!!}\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = \u2261-cong\u2082 (\u03bb x y \u2192 x y)\n\n\u2261-isEquivalence : \u2200 {\u03c4} \u2192 IsEquivalence (_\u2261_ {\u03c4})\n\u2261-isEquivalence = record\n { refl = \u2261-refl\n ; sym = \u2261-sym\n ; trans = \u2261-trans\n }\n\n\u2261-setoid : Type \u2192 Setoid _ _\n\u2261-setoid \u03c4 = record\n { Carrier = \u27e6 \u03c4 \u27e7\n ; _\u2248_ = _\u2261_\n ; isEquivalence = \u2261-isEquivalence\n }\n\nmodule \u2261-Reasoning where\n module _ {\u03c4 : Type} where\n open EqR (\u2261-setoid \u03c4) public\n hiding (_\u2261\u27e8_\u27e9_) renaming (_\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_)\n\n\u2261-consistent : \u00ac (\u2200 (\u03c4 : Type) \u2192 (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192 v\u2081 \u2261 v\u2082)\n\u2261-consistent H with H bool true false\n... | ()\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\u0394-Type (bool) = bool -- true means negate, false means nil change\n\nderive : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\napply : \u2200 {\u03c4} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\ndiff : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ndiff {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 diff (f\u2081 (apply dv v)) (f\u2082 v)\ndiff {bool} true true = false\ndiff {bool} true false = true\ndiff {bool} false true = true\ndiff {bool} false false = false\n\nderive {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 diff (f (apply dv v)) (f v)\nderive {bool} b = false\n\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} df f = \u03bb v \u2192 apply (df v (derive v)) (f v)\napply {bool} true true = false\napply {bool} true false = true\napply {bool} false true = true\napply {bool} false false = false\n\ncompose : \u2200 {\u03c4} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} df\u2081 df\u2082 = \u03bb v dv \u2192 compose (df\u2081 v dv) (df\u2082 v dv)\ncompose {bool} true true = false\ncompose {bool} true false = true\ncompose {bool} false true = true\ncompose {bool} false false = false\n\n-- CONGRUENCE rules for change operations\n\n\u2261-diff : \u2200 {\u03c4 : Type} {v\u2081 v\u2082 v\u2083 v\u2084 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 diff v\u2081 v\u2083 \u2261 diff v\u2082 v\u2084\n\u2261-diff = \u2261-cong\u2082 diff\n\n\u2261-apply : \u2200 {\u03c4 : Type} {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} {v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 v\u2081 \u2261 v\u2082 \u2192 apply dv\u2081 v\u2081 \u2261 apply dv\u2082 v\u2082\n\u2261-apply = \u2261-cong\u2082 apply\n\n-- PROPERTIES of changes\n\ndiff-derive : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192\n diff v v \u2261 derive v\ndiff-derive {\u03c4\u2081 \u21d2 \u03c4\u2082} v = \u2261-refl\ndiff-derive {bool} true = bool\ndiff-derive {bool} false = bool\n\ndiff-apply : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n diff (apply dv v) v \u2261 dv\ndiff-apply {\u03c4\u2081 \u21d2 \u03c4\u2082} df f = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n {!!}))\ndiff-apply {bool} true true = bool\ndiff-apply {bool} true false = bool\ndiff-apply {bool} false true = bool\ndiff-apply {bool} false false = bool\n\napply-diff : \u2200 {\u03c4} (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192\n apply (diff v\u2082 v\u2081) v\u2081 \u2261 v\u2082\n\napply-derive : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192\n apply (derive v) v \u2261 v\n\napply-diff {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = ext (\u03bb v \u2192\n begin\n apply (diff f\u2082 f\u2081) f\u2081 v\n \u2261\u27e8 \u2261-refl \u27e9\n apply (diff (f\u2082 (apply (derive v) v)) (f\u2081 v)) (f\u2081 v)\n \u2261\u27e8 \u2261-apply (\u2261-diff (\u2261-cong f\u2082 (apply-derive v)) \u2261-refl) \u2261-refl \u27e9\n apply (diff (f\u2082 v) (f\u2081 v)) (f\u2081 v)\n \u2261\u27e8 apply-diff (f\u2081 v) (f\u2082 v) \u27e9\n f\u2082 v\n \u220e) where open \u2261-Reasoning\napply-diff {bool} true true = bool\napply-diff {bool} true false = bool\napply-diff {bool} false true = bool\napply-diff {bool} false false = bool\n\napply-derive {\u03c4\u2081 \u21d2 \u03c4\u2082} f = ext (\u03bb v \u2192\n begin\n apply (derive f) f v\n \u2261\u27e8 \u2261-refl \u27e9\n apply (diff (f (apply (derive v) v)) (f v)) (f v)\n \u2261\u27e8 \u2261-cong (\u03bb x \u2192 apply (diff (f x) (f v)) (f v)) (apply-derive v) \u27e9\n apply (diff (f v) (f v)) (f v)\n \u2261\u27e8 apply-diff (f v) (f v)\u27e9\n f v\n \u220e) where open \u2261-Reasoning\napply-derive {bool} true = bool\napply-derive {bool} false = bool\n\napply-compose : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) (dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n apply (compose dv\u2081 dv\u2082) v \u2261 apply dv\u2081 (apply dv\u2082 v)\napply-compose {\u03c4\u2081 \u21d2 \u03c4\u2082} f df\u2081 df\u2082 = ext (\u03bb v \u2192\n apply-compose (f v) (df\u2081 v (derive v)) (df\u2082 v (derive v)))\napply-compose {bool} true true true = bool\napply-compose {bool} true true false = bool\napply-compose {bool} true false true = bool\napply-compose {bool} true false false = bool\napply-compose {bool} false true true = bool\napply-compose {bool} false true false = bool\napply-compose {bool} false false true = bool\napply-compose {bool} false false false = bool\n\ncompose-assoc : \u2200 {\u03c4} (dv\u2081 dv\u2082 dv\u2083 : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n compose dv\u2081 (compose dv\u2082 dv\u2083) \u2261 compose (compose dv\u2081 dv\u2082) dv\u2083\ncompose-assoc {\u03c4\u2081 \u21d2 \u03c4\u2082} df\u2081 df\u2082 df\u2083 = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n compose-assoc (df\u2081 v dv) (df\u2082 v dv) (df\u2083 v dv)))\ncompose-assoc {bool} true true true = bool\ncompose-assoc {bool} true true false = bool\ncompose-assoc {bool} true false true = bool\ncompose-assoc {bool} true false false = bool\ncompose-assoc {bool} false true true = bool\ncompose-assoc {bool} false true false = bool\ncompose-assoc {bool} false false true = bool\ncompose-assoc {bool} false false false = bool\n\n-- TYPING CONTEXTS, VARIABLES and WEAKENING\n\nopen import binding Type \u27e6_\u27e7Type\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393\n\nupdate : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 \u03c1) = apply dv v \u2022 update \u03c1\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore {\u2205} \u2205 = \u2205\nignore {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 \u03c1) = v \u2022 ignore \u03c1\n\n\u0394-Context\u2032 : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\n\u0394-Context\u2032 \u0393 \u2205 = \u0394-Context \u0393\n\u0394-Context\u2032 (.\u03c4 \u2022 \u0393) (\u03c4 \u2022 \u0393\u2032) = \u03c4 \u2022 \u0394-Context\u2032 \u0393 \u0393\u2032\n\nupdate\u2032 : \u2200 {\u0393} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nupdate\u2032 \u2205 \u03c1 = update \u03c1\nupdate\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) = v \u2022 update\u2032 \u0393\u2032 \u03c1\n\nignore\u2032 : \u2200 {\u0393} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore\u2032 \u2205 \u03c1 = ignore \u03c1\nignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) = v \u2022 ignore\u2032 \u0393\u2032 \u03c1\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n true false : \u2200 {\u0393} \u2192 Term \u0393 bool\n if : \u2200 {\u0393 \u03c4} \u2192 (t\u2081 : Term \u0393 bool) (t\u2082 t\u2083 : Term \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n -- `\u0394 t` describes how t changes if its free variables or arguments change\n \u0394 : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 true \u27e7Term \u03c1 = true\n\u27e6 false \u27e7Term \u03c1 = false\n\u27e6 if t\u2081 t\u2082 t\u2083 \u27e7Term \u03c1 with \u27e6 t\u2081 \u27e7Term \u03c1\n... | true = \u27e6 t\u2082 \u27e7Term \u03c1\n... | false = \u27e6 t\u2083 \u27e7Term \u03c1\n\u27e6 \u0394 t \u27e7Term \u03c1 = diff (\u27e6 t \u27e7Term (update \u03c1)) (\u27e6 t \u27e7Term (ignore \u03c1))\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n-- Term Equivalence\n\nmodule _ {\u0393} {\u03c4} where\n data _\u2248_ (t\u2081 t\u2082 : Term \u0393 \u03c4) : Set where\n ext :\n (\u2200 \u03c1 \u2192 \u27e6 t\u2081 \u27e7 \u03c1 \u2261 \u27e6 t\u2082 \u27e7 \u03c1) \u2192\n t\u2081 \u2248 t\u2082\n\n \u2248-refl : Reflexive _\u2248_\n \u2248-refl = ext (\u03bb \u03c1 \u2192 \u2261-refl)\n\n \u2248-sym : Symmetric _\u2248_\n \u2248-sym (ext \u2248) = ext (\u03bb \u03c1 \u2192 \u2261-sym (\u2248 \u03c1))\n\n \u2248-trans : Transitive _\u2248_\n \u2248-trans (ext \u2248\u2081) (ext \u2248\u2082) = ext (\u03bb \u03c1 \u2192 \u2261-trans (\u2248\u2081 \u03c1) (\u2248\u2082 \u03c1))\n\n \u2248-isEquivalence : IsEquivalence _\u2248_\n \u2248-isEquivalence = record\n { refl = \u2248-refl\n ; sym = \u2248-sym\n ; trans = \u2248-trans\n }\n\n\u2248-setoid : Context \u2192 Type \u2192 Setoid _ _\n\u2248-setoid \u0393 \u03c4 = record\n { Carrier = Term \u0393 \u03c4\n ; _\u2248_ = _\u2248_\n ; isEquivalence = \u2248-isEquivalence\n }\n\n\u2248-app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} {t\u2081 t\u2082 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2083 t\u2084 : Term \u0393 \u03c4\u2081} \u2192\n t\u2081 \u2248 t\u2082 \u2192 t\u2083 \u2248 t\u2084 \u2192 app t\u2081 t\u2083 \u2248 app t\u2082 t\u2084\n\u2248-app (ext \u2248\u2081) (ext \u2248\u2082) = ext (\u03bb \u03c1 \u2192\n \u2261-cong\u2082 (\u03bb x y \u2192 x y) (\u2248\u2081 \u03c1) (\u2248\u2082 \u03c1))\n\n\u2248-abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} {t\u2081 t\u2082 : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n t\u2081 \u2248 t\u2082 \u2192 abs t\u2081 \u2248 abs t\u2082\n\u2248-abs (ext \u2248) = ext (\u03bb \u03c1 \u2192\n ext (\u03bb v \u2192 \u2248 (v \u2022 \u03c1)))\n\n\u2248-\u0394 : \u2200 {\u03c4 \u0393} {t\u2081 t\u2082 : Term \u0393 \u03c4} \u2192\n t\u2081 \u2248 t\u2082 \u2192 \u0394 t\u2081 \u2248 \u0394 t\u2082\n\u2248-\u0394 (ext \u2248) = ext (\u03bb \u03c1 \u2192 \u2261-diff (\u2248 (update \u03c1)) (\u2248 (ignore \u03c1)))\n\nmodule \u2248-Reasoning where\n module _ {\u0393 : Context} {\u03c4 : Type} where\n open EqR (\u2248-setoid \u0393 \u03c4) public\n hiding (_\u2261\u27e8_\u27e9_)\n\n\u2248-consistent : \u00ac (\u2200 {\u0393 \u03c4} (t\u2081 t\u2082 : Term \u0393 \u03c4) \u2192 t\u2081 \u2248 t\u2082)\n\u2248-consistent H with H {\u2205} true false\n... | ext x with x \u2205\n... | ()\n\n-- LIFTING terms into \u0394-Contexts\n\nlift-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) \u03c4\nlift-var this = that this\nlift-var (that x) = that (that (lift-var x))\n\nlift-var\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-var\u2032 \u2205 x = lift-var x\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) this = this\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) (that x) = that (lift-var\u2032 \u0393\u2032 x)\n\nlift-term\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-term\u2032 \u0393\u2032 (abs t) = abs (lift-term\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term\u2032 \u0393\u2032 (app t\u2081 t\u2082) = app (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082)\nlift-term\u2032 \u0393\u2032 (var x) = var (lift-var\u2032 \u0393\u2032 x)\nlift-term\u2032 \u0393\u2032 true = true\nlift-term\u2032 \u0393\u2032 false = false\nlift-term\u2032 \u0393\u2032 (if t\u2081 t\u2082 t\u2083) = if (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082) (lift-term\u2032 \u0393\u2032 t\u2083)\nlift-term\u2032 \u0393\u2032 (\u0394 t) = {!!}\n\nlift-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) \u03c4\nlift-term = lift-term\u2032 \u2205\n\n-- PROPERTIES of lift-term\n\nlift-var-ignore : \u2200 {\u0393 \u03c4} (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore \u03c1)\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) (that x) = lift-var-ignore \u03c1 x\n\nlift-var-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) (\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var\u2032 \u0393\u2032 x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-var-ignore\u2032 \u2205 \u03c1 x = lift-var-ignore \u03c1 x\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) (that x) = lift-var-ignore\u2032 \u0393\u2032 \u03c1 x\n\nlift-term-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) {\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term\u2032 \u0393\u2032 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-term-ignore\u2032 \u0393\u2032 (abs t) =\n ext (\u03bb v \u2192 lift-term-ignore\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term-ignore\u2032 \u0393\u2032 (app t\u2081 t\u2082) =\n \u2261-app (lift-term-ignore\u2032 \u0393\u2032 t\u2081) (lift-term-ignore\u2032 \u0393\u2032 t\u2082)\nlift-term-ignore\u2032 \u0393\u2032 (var x) = lift-var-ignore\u2032 \u0393\u2032 _ x\nlift-term-ignore\u2032 \u0393\u2032 true = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 false = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 {\u03c1} (if t\u2081 t\u2082 t\u2083)\n with \u27e6 lift-term\u2032 \u0393\u2032 t\u2081 \u27e7 \u03c1\n | \u27e6 t\u2081 \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\n | lift-term-ignore\u2032 \u0393\u2032 {\u03c1} t\u2081\n... | true | true | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2082\n... | false | false | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2083\nlift-term-ignore\u2032 \u0393\u2032 (\u0394 t) = {!!}\n\nlift-term-ignore : \u2200 {\u0393 \u03c4} {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore \u03c1)\nlift-term-ignore = lift-term-ignore\u2032 \u2205\n\n\n-- PROPERTIES of \u0394\n\n\u0394-abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192\n \u0394 (abs t) \u2248 abs (abs (\u0394 t))\n\u0394-abs t = ext (\u03bb \u03c1 \u2192 \u2261-refl)\n\n\u0394-app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192\n \u0394 (app t\u2081 t\u2082) \u2248 app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n\u0394-app t\u2081 t\u2082 = \u2248-sym (ext (\u03bb \u03c1 \u2192\n begin\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 lift-term t\u2082 \u27e7 \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 lift-term t\u2082 \u27e7 \u03c1))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))) x))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) x))\n (lift-term-ignore {\u03c1 = \u03c1} t\u2082) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff (\u27e6 t\u2081 \u27e7 (update \u03c1) x) (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (apply-diff (\u27e6 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 t\u2082 \u27e7 (update \u03c1))) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u220e)) where open \u2261-Reasoning\n\n-- SYMBOLIC DERIVATION\n\nderive-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-var this = this\nderive-var (that x) = that (that (derive-var x))\n\nderive-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-term (abs t) = abs (abs (derive-term t))\nderive-term (app t\u2081 t\u2082) = app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\nderive-term (var x) = var (derive-var x)\nderive-term true = false\nderive-term false = false\nderive-term (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term (\u0394 t) = \u0394 (derive-term t)\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n \u0394 t \u2248 derive-term t\nderive-term-correct {\u0393} (abs t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct t)) \u27e9\n abs (abs (derive-term t))\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (abs t)\n \u220e where open \u2248-Reasoning\nderive-term-correct (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct t\u2081) \u2248-refl) (derive-term-correct t\u2082) \u27e9\n app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct (var x) = ext (\u03bb \u03c1 \u2192 derive-var-correct \u03c1 x)\nderive-term-correct true = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\n\n-- NATURAL SEMANTICS\n\n-- (without support for \u0394 for now)\n\n-- Syntax\n\ndata Env : Context \u2192 Set\ndata Val : Type \u2192 Set\n\ndata Val where\n \u27e8abs_,_\u27e9 : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) (\u03c1 : Env \u0393) \u2192 Val (\u03c4\u2081 \u21d2 \u03c4\u2082)\n\ndata Env where\n \u2205 : Env \u2205\n _\u2022_ : \u2200 {\u0393 \u03c4} \u2192 Val \u03c4 \u2192 Env \u0393 \u2192 Env (\u03c4 \u2022 \u0393)\n\n-- Lookup\n\ninfixr 8 _\u22a2_\u2193_ _\u22a2_\u21a6_\n\ndata _\u22a2_\u21a6_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Var \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n this : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {v : Val \u03c4} \u2192\n v \u2022 \u03c1 \u22a2 this \u21a6 v\n that : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 x} {\u03c1 : Env \u0393} {v\u2081 : Val \u03c4\u2081} {v\u2082 : Val \u03c4\u2082} \u2192\n \u03c1 \u22a2 x \u21a6 v\u2082 \u2192\n v\u2081 \u2022 \u03c1 \u22a2 that x \u21a6 v\u2082\n\n-- Reduction\n\ndata _\u22a2_\u2193_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Term \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c1} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n \u03c1 \u22a2 abs t \u2193 \u27e8abs t , \u03c1 \u27e9\n app : \u2200 {\u0393 \u0393\u2032 \u03c4\u2081 \u03c4\u2082 \u03c1 \u03c1\u2032 v\u2082 v\u2032} {t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2082 : Term \u0393 \u03c4\u2081} {t\u2032 : Term (\u03c4\u2081 \u2022 \u0393\u2032) \u03c4\u2082} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 \u27e8abs t\u2032 , \u03c1\u2032 \u27e9 \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n v\u2082 \u2022 \u03c1\u2032 \u22a2 t\u2032 \u2193 v\u2032 \u2192\n \u03c1 \u22a2 app t\u2081 t\u2082 \u2193 v\u2032\n var : \u2200 {\u0393 \u03c4 x} {\u03c1 : Env \u0393} {v : Val \u03c4}\u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u03c1 \u22a2 var x \u2193 v\n\n-- SOUNDNESS of natural semantics\n\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 Env \u0393 \u2192 \u27e6 \u0393 \u27e7\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 \u27e8abs t , \u03c1 \u27e9 \u27e7Val = \u03bb v \u2192 \u27e6 t \u27e7 (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\nmeaningOfEnv : \u2200 {\u0393} \u2192 Meaning (Env \u0393)\nmeaningOfEnv = meaning \u27e6_\u27e7Env\n\nmeaningOfVal : \u2200 {\u03c4} \u2192 Meaning (Val \u03c4)\nmeaningOfVal = meaning \u27e6_\u27e7Val\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = \u2261-refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = \u2261-refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) =\n \u2261-trans\n (\u2261-cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082))\n (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n\n-- WEAKENING\n\n-- Weaken a term to a super context\n\n{-\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken {\u0393\u2081} {\u0393\u2082} (abs {\u03c4\u2081 = \u03c4} t) = abs (weaken {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} t)\nweaken {\u0393\u2081} {\u0393\u2082} (app t\u2081 t\u2082) = app (weaken {\u0393\u2081} {\u0393\u2082} t\u2081) (weaken {\u0393\u2081} {\u0393\u2082} t\u2082)\nweaken {\u0393\u2081} {\u0393\u2082} (var x) = var (lift {\u0393\u2081} {\u0393\u2082} x)\nweaken {\u0393\u2081} {\u0393\u2082} (\u0394 t) = ?\n-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"2fa68e863455907e4dbafa0d9e4c28fae5211452","subject":"sum: SearchInd","message":"sum: SearchInd\n","repos":"crypto-agda\/crypto-agda","old_file":"sum.agda","new_file":"sum.agda","new_contents":"import Level as L\nopen import Type\nopen import Function\nopen import Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\nopen import Data.Unit hiding (_\u2264_)\nopen import Data.Sum\nopen import Data.Maybe.NP\nopen import Data.Product\nopen import Data.Bits\nopen import Data.Bool.NP as Bool\nopen import Function.Equality using (_\u27e8$\u27e9_)\nimport Function.Inverse as FI\nopen FI using (_\u2194_; module Inverse)\nopen import Function.Related.TypeIsomorphisms.NP\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_; _\u2257_)\n\nmodule sum where\n\n_\u2264\u00b0_ : \u2200 {A : \u2605}(f g : A \u2192 \u2115) \u2192 \u2605\nf \u2264\u00b0 g = \u2200 x \u2192 f x \u2264 g x\n\nSemigroup\u2080 = Semigroup L.zero L.zero\nMonoid\u2080 = Monoid L.zero L.zero\nCommutativeMonoid\u2080 = CommutativeMonoid L.zero L.zero\nPreorder\u2080 = Preorder L.zero L.zero L.zero\n\nmodule PO (po : Preorder\u2080) where\n open Preorder po public renaming (_\u223c_ to _\u2286_)\n _\u2286\u00b0_ : \u2200 {A : \u2605}(f g : A \u2192 Carrier) \u2192 \u2605\n f \u2286\u00b0 g = \u2200 x \u2192 f x \u2286 g x\n\nmodule SgrpExtra (sg : Semigroup\u2080) where\n open Semigroup sg\n open Setoid-Reasoning (Semigroup.setoid sg) public\n C : \u2605\n C = Carrier\n _\u2248\u00b0_ : \u2200 {A : \u2605} (f g : A \u2192 C) \u2192 \u2605\n f \u2248\u00b0 g = \u2200 x \u2192 f x \u2248 g x\n _\u2219\u00b0_ : \u2200 {A : \u2605} (f g : A \u2192 C) \u2192 A \u2192 C\n (f \u2219\u00b0 g) x = f x \u2219 g x\n infixl 7 _-\u2219-_\n _-\u2219-_ : _\u2219_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_\n _-\u2219-_ = \u2219-cong\n\nmodule Sgrp (sg : Semigroup\u2080) where\n open Semigroup sg public\n open SgrpExtra sg public\n\nmodule Mon (m : Monoid\u2080) where\n open Monoid m public\n sg = semigroup\n open SgrpExtra semigroup public\n Is-\u03b5 : Carrier \u2192 \u2605\n Is-\u03b5 x = x \u2248 \u03b5\n\nmodule CMon (cm : CommutativeMonoid\u2080) where\n open CommutativeMonoid cm public\n sg = semigroup\n m = monoid\n open SgrpExtra sg public\n\n \u2219-interchange : Interchange _\u2248_ _\u2219_ _\u2219_\n \u2219-interchange = InterchangeFromAssocCommCong.\u2219-interchange\n _\u2248_ isEquivalence\n _\u2219_ assoc comm (\u03bb _ \u2192 flip \u2219-cong refl)\n\nSearch : \u2605 \u2192 \u2605\u2081\nSearch A = \u2200 {B} \u2192 (_\u2219_ : B \u2192 B \u2192 B) \u2192 (A \u2192 B) \u2192 B\n-- Search A = \u2200 {I : \u2605} {F : I \u2192 \u2605} \u2192 (_\u2219_ : \u2200 {i} \u2192 F i \u2192 F i \u2192 F i) \u2192 \u2200 {i} \u2192 (A \u2192 F i) \u2192 F i\n\nSearchMon : \u2605 \u2192 \u2605\u2081\nSearchMon A = (m : Monoid\u2080) \u2192 let open Mon m in\n (A \u2192 C) \u2192 C\n\nsearchMonFromSearch : \u2200 {A} \u2192 Search A \u2192 SearchMon A\nsearchMonFromSearch s m = s _\u2219_ where open Mon m\n\nSum : \u2605 \u2192 \u2605\nSum A = (A \u2192 \u2115) \u2192 \u2115\n\nCount : \u2605 \u2192 \u2605\nCount A = (A \u2192 Bit) \u2192 \u2115\n\nSearchInd : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchInd {A} srch = \u2200 {C}\n (P : ((A \u2192 C) \u2192 C) \u2192 \u2605)\n {_\u2219_ : Op\u2082 C}\n (P\u2219 : \u2200 {s\u2080 s\u2081} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb f \u2192 s\u2080 f \u2219 s\u2081 f))\n (Pf : \u2200 (x : A) \u2192 P (\u03bb f \u2192 f x))\n \u2192 P (srch _\u2219_)\n\nSearchInd' : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchInd' {A} srch = \u2200 {C}\n (P : C \u2192 \u2605)\n {_\u2219_ : C \u2192 C \u2192 C}\n (P\u2219 : \u2200 {x y} \u2192 P x \u2192 P y \u2192 P (x \u2219 y))\n {f : A \u2192 C}\n (Pf : \u2200 x \u2192 P (f x))\n \u2192 P (srch _\u2219_ f)\n\nsearchInd' : \u2200 {A} {s\u1d2c : Search A} \u2192 SearchInd s\u1d2c \u2192 SearchInd' s\u1d2c\nsearchInd' Ps\u1d2c P P\u2219 {f} Pf = Ps\u1d2c (\u03bb s \u2192 P (s f)) P\u2219 Pf\n\nSearchMono : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchMono s\u1d2c = \u2200 {C : \u2605} (_\u2286_ : C \u2192 C \u2192 \u2605) \u2192 -- let open PO {C} _\u2286_ in\n \u2200 {_\u2219_} (\u2219-mono : _\u2219_ Preserves\u2082 _\u2286_ \u27f6 _\u2286_ \u27f6 _\u2286_)\n {f g} \u2192\n (\u2200 x \u2192 f x \u2286 g x) \u2192 s\u1d2c _\u2219_ f \u2286 s\u1d2c _\u2219_ g\n\nsearch-mono' : \u2200 {A} (s\u1d2c : Search A) \u2192 SearchInd s\u1d2c \u2192 SearchMono s\u1d2c\nsearch-mono' s\u1d2c Ps\u1d2c _\u2286_ {_\u2219_} _\u2219-mono_ {f} {g} f\u2286\u00b0g = Ps\u1d2c (\u03bb s \u2192 s f \u2286 s g) _\u2219-mono_ f\u2286\u00b0g\n\nSearchExt : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchExt s\u1d2c = \u2200 sg {f g} \u2192 let open Sgrp sg in\n f \u2248\u00b0 g \u2192 s\u1d2c _\u2219_ f \u2248 s\u1d2c _\u2219_ g\n\nSumExt : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumExt sum\u1d2c = \u2200 {f g} \u2192 f \u2257 g \u2192 sum\u1d2c f \u2261 sum\u1d2c g\n\nCountExt : \u2200 {A} \u2192 Count A \u2192 \u2605\nCountExt count\u1d2c = \u2200 {f g} \u2192 f \u2257 g \u2192 count\u1d2c f \u2261 count\u1d2c g\n\nSearch\u03b5 : \u2200 {A} \u2192 SearchMon A \u2192 \u2605\u2081\nSearch\u03b5 s\u1d2c = \u2200 m \u2192 let open Mon m in\n s\u1d2c m (const \u03b5) \u2248 \u03b5\n\nsearch-\u03b5' : \u2200 {A} (s\u1d2c : Search A) \u2192 SearchInd s\u1d2c \u2192 Search\u03b5 (searchMonFromSearch s\u1d2c)\nsearch-\u03b5' s\u1d2c Ps\u1d2c m = searchInd' Ps\u1d2c Is-\u03b5 (\u03bb x\u2248\u03b5 y\u2248\u03b5 \u2192 trans (\u2219-cong x\u2248\u03b5 y\u2248\u03b5) (proj\u2081 identity \u03b5)) (\u03bb _ \u2192 refl)\n where open Mon m\n\nsearch-ext' : \u2200 {A} (s\u1d2c : Search A) \u2192 SearchInd s\u1d2c \u2192 SearchExt s\u1d2c\nsearch-ext' s\u1d2c Ps\u1d2c sg {f} {g} f\u2248\u00b0g = Ps\u1d2c (\u03bb s \u2192 s f \u2248 s g) \u2219-cong f\u2248\u00b0g\n where open Sgrp sg\n\nSumZero : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumZero sum\u1d2c = sum\u1d2c (\u03bb _ \u2192 0) \u2261 0\n\nSumLin : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumLin sum\u1d2c = \u2200 f k \u2192 sum\u1d2c (\u03bb x \u2192 k * f x) \u2261 k * sum\u1d2c f\n\nSearchHom : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchHom s\u1d2c = \u2200 sg f g \u2192 let open Sgrp sg in\n s\u1d2c _\u2219_ (f \u2219\u00b0 g) \u2248 s\u1d2c _\u2219_ f \u2219 s\u1d2c _\u2219_ g\n\nSearchMonHom : \u2200 {A} \u2192 SearchMon A \u2192 \u2605\u2081\nSearchMonHom s\u1d2c = \u2200 (cm : CommutativeMonoid\u2080) f g \u2192\n let open CMon cm in\n s\u1d2c m (f \u2219\u00b0 g) \u2248 s\u1d2c m f \u2219 s\u1d2c m g\n\n{-search-hom\u2032 :\n \u2200 {a b}\n {A : Set a} {B : Set b} {R}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : R \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom\u2032 = ?\n{-\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n-}\n-}\n\nSumHom : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumHom sum\u1d2c = \u2200 f g \u2192 sum\u1d2c (\u03bb x \u2192 f x + g x) \u2261 sum\u1d2c f + sum\u1d2c g\n\nSumMono : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumMono sum\u1d2c = \u2200 {f g} \u2192 f \u2264\u00b0 g \u2192 sum\u1d2c f \u2264 sum\u1d2c g\n\nSearchSwap : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchSwap {A} s\u1d2c = \u2200 {B} sg f \u2192 let open Sgrp sg in \u2200 {s\u1d2e : (B \u2192 C) \u2192 C}\n\n \u2192 (hom : \u2200 f g \u2192 s\u1d2e (f \u2219\u00b0 g) \u2248 s\u1d2e f \u2219 s\u1d2e g)\n \u2192 s\u1d2c _\u2219_ (s\u1d2e \u2218 f) \u2248 s\u1d2e (s\u1d2c _\u2219_ \u2218 flip f)\n\nSumLin\u21d2SumZero : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumLin sum \u2192 SumZero sum\nSumLin\u21d2SumZero sum-lin = sum-lin (\u03bb _ \u2192 0) 0\n\nmkSumExt : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumMono sum \u2192 SumExt sum\nmkSumExt sum-mono f\u2257g = \u2115\u2264.antisym (sum-mono (\u2115\u2264.reflexive \u2218 f\u2257g)) (sum-mono (\u2115\u2264.reflexive \u2218 \u2261.sym \u2218 f\u2257g))\n\nmkSumMon : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumExt sum \u2192 SumHom sum \u2192 SumMono sum\nmkSumMon {sum = sum} sum-ext sum-hom {f} {g} f\u2264\u00b0g =\n sum f \u2264\u27e8 m\u2264m+n _ _ \u27e9\n sum f + sum (\u03bb x \u2192 g x \u2238 f x) \u2261\u27e8 \u2261.sym (sum-hom _ _) \u27e9\n sum (\u03bb x \u2192 f x + (g x \u2238 f x)) \u2261\u27e8 sum-ext (m+n\u2238m\u2261n \u2218 f\u2264\u00b0g) \u27e9\n sum g \u220e where open \u2264-Reasoning\n\nrecord SumProp A : \u2605\u2081 where\n constructor mk\n field\n search : Search A\n search-ext : SearchExt search\n search-mono : SearchMono search\n search-swap : SearchSwap search\n searchMon : SearchMon A\n searchMon m = let open Mon m in search _\u2219_\n field\n search-\u03b5 : Search\u03b5 searchMon\n search-hom : SearchMonHom searchMon\n sum : Sum A\n sum = search _+_\n sum-ext : SumExt sum\n sum-ext = search-ext \u2115+.semigroup\n sum-zero : SumZero sum\n sum-zero = search-\u03b5 \u2115+.monoid\n sum-hom : SumHom sum\n sum-hom = search-hom \u2115\u00b0.+-commutativeMonoid\n sum-mono : SumMono sum\n sum-mono = search-mono _\u2264_ _+-mono_\n\n sum-lin : SumLin sum\n sum-lin f zero = sum-zero\n sum-lin f (suc k) = {!!}\n {-\n rewrite sum-hom f (\u03bb x \u2192 k * f x)\n | sum-lin f k = {!\u2261.refl!}\n -}\n\n Card : \u2115\n Card = sum (const 1)\n\n count : Count A\n count f = sum (Bool.to\u2115 \u2218 f)\n\n count-ext : CountExt count\n count-ext f\u2257g = sum-ext (\u2261.cong Bool.to\u2115 \u2218 f\u2257g)\n\nopen SumProp public\n\nsearch-swap' : \u2200 {A B} cm (\u03bcA : SumProp A) (\u03bcB : SumProp B) f \u2192\n let open CMon cm\n s\u1d2c = search \u03bcA _\u2219_\n s\u1d2e = search \u03bcB _\u2219_ in\n s\u1d2c (s\u1d2e \u2218 f) \u2248 s\u1d2e (s\u1d2c \u2218 flip f)\nsearch-swap' cm \u03bcA \u03bcB f = search-swap \u03bcA sg f (search-hom \u03bcB cm)\n where open CMon cm\n\nsum-swap : \u2200 {A B} (\u03bcA : SumProp A) (\u03bcB : SumProp B) f \u2192\n sum \u03bcA (sum \u03bcB \u2218 f) \u2261 sum \u03bcB (sum \u03bcA \u2218 flip f)\nsum-swap = search-swap' \u2115\u00b0.+-commutativeMonoid\n\n_\u2248Sum_ : \u2200 {A} \u2192 (sum\u2080 sum\u2081 : Sum A) \u2192 \u2605\nsum\u2080 \u2248Sum sum\u2081 = \u2200 f \u2192 sum\u2080 f \u2261 sum\u2081 f\n\n\u03bc\u22a4 : SumProp \u22a4\n\u03bc\u22a4 = mk search\u22a4 ext mono search\u22a4-swap eps hom\n where\n search\u22a4 : Search \u22a4\n search\u22a4 _ f = f _\n\n searchMon\u22a4 : SearchMon \u22a4\n searchMon\u22a4 _ f = f _\n\n ind : SearchInd search\u22a4\n ind _ _ Pf = Pf _\n\n ext : SearchExt search\u22a4\n ext _ f\u2257g = f\u2257g _\n\n mono : SearchMono search\u22a4\n mono _ _ f\u2264\u00b0g = f\u2264\u00b0g _\n\n eps : Search\u03b5 searchMon\u22a4\n eps m = Monoid.refl m\n\n search\u22a4-swap : SearchSwap search\u22a4\n search\u22a4-swap sg f hom = refl\n where open Sgrp sg\n\n hom : SearchMonHom searchMon\u22a4\n hom m f g = CommutativeMonoid.refl m\n\n\u03bcBit : SumProp Bit\n\u03bcBit = mk searchBit ext mono swp eps hom\n where\n searchBit : Search Bit\n searchBit _\u2219_ f = f 0b \u2219 f 1b\n\n searchMonBit : SearchMon Bit\n searchMonBit m f = f 0b \u2219 f 1b\n where open Mon m\n\n ext : SearchExt searchBit\n ext sg f\u2257g = f\u2257g 0b -\u2219- f\u2257g 1b\n where open Sgrp sg\n\n mono : SearchMono searchBit\n mono po _\u2219-mono_ f\u2286\u00b0g = f\u2286\u00b0g 0b \u2219-mono f\u2286\u00b0g 1b\n\n eps : Search\u03b5 searchMonBit\n eps m = proj\u2081 identity \u03b5\n where open Monoid m\n\n hom : SearchMonHom searchMonBit\n hom cm f g = \u2219-interchange (f 0b) (g 0b) (f 1b) (g 1b)\n where open CMon cm\n\n sumBit : Sum Bit\n sumBit f = f 0b + f 1b\n\n swp : SearchSwap searchBit\n swp sg f hom\u1d2e = sym (hom\u1d2e (f 0b) (f 1b))\n where open Sgrp sg\n\ninfixr 4 _+Search_\n\n_+Search_ : \u2200 {A B} \u2192 Search A \u2192 Search B \u2192 Search (A \u228e B)\n(search\u1d2c +Search search\u1d2e) _\u2219_ f = search\u1d2c _\u2219_ (f \u2218 inj\u2081) \u2219 search\u1d2e _\u2219_ (f \u2218 inj\u2082)\n\n_+SearchInd'_ : \u2200 {A B} {s\u1d2c : Search A} {s\u1d2e : Search B} \u2192\n SearchInd' s\u1d2c \u2192 SearchInd' s\u1d2e \u2192 SearchInd' (s\u1d2c +Search s\u1d2e)\n(Ps\u1d2c +SearchInd' Ps\u1d2e) P P\u2219 Pf = P\u2219 (Ps\u1d2c P P\u2219 (Pf \u2218 inj\u2081)) (Ps\u1d2e P P\u2219 (Pf \u2218 inj\u2082))\n\n_+SearchInd_ : \u2200 {A B} {s\u1d2c : Search A} {s\u1d2e : Search B} \u2192\n SearchInd s\u1d2c \u2192 SearchInd s\u1d2e \u2192 SearchInd (s\u1d2c +Search s\u1d2e)\n(Ps\u1d2c +SearchInd Ps\u1d2e) P P\u2219 Pf\n = P\u2219 (Ps\u1d2c (\u03bb s \u2192 P (\u03bb f \u2192 s (f \u2218 inj\u2081))) P\u2219 (Pf \u2218 inj\u2081))\n (Ps\u1d2e (\u03bb s \u2192 P (\u03bb f \u2192 s (f \u2218 inj\u2082))) P\u2219 (Pf \u2218 inj\u2082))\n\ninfixr 4 _+Sum_\n\n_+Sum_ : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u228e B)\n(sum\u1d2c +Sum sum\u1d2e) f = sum\u1d2c (f \u2218 inj\u2081) + sum\u1d2e (f \u2218 inj\u2082)\n\n_+\u03bc_ : \u2200 {A B} \u2192 SumProp A\n \u2192 SumProp B\n \u2192 SumProp (A \u228e B)\n_+\u03bc_ {A} {B} \u03bcA \u03bcB = ?\n{-\n_+\u03bc_ {A} {B} \u03bcA \u03bcB = mk srch ext mono swp eps hom\n where\n s\u1d2c : Search A\n s\u1d2c = search \u03bcA\n s\u1d2e : Search B\n s\u1d2e = search \u03bcB\n srch : Search (A \u228e B)\n srch = s\u1d2c +Search s\u1d2e\n sMon\u1d2c = searchMon \u03bcA\n sMon\u1d2e = searchMon \u03bcB\n srchMon = searchMonFromSearch srch\n ext : SearchExt srch\n ext sg f\u2257g = search-ext \u03bcA sg (f\u2257g \u2218 inj\u2081) -\u2219- search-ext \u03bcB sg (f\u2257g \u2218 inj\u2082)\n where open Sgrp sg\n\n eps : Search\u03b5 srchMon\n eps m = srchMon m (const \u03b5)\n \u2248\u27e8 search-\u03b5 \u03bcA m -\u2219- search-\u03b5 \u03bcB m \u27e9\n \u03b5 \u2219 \u03b5\n \u2248\u27e8 proj\u2081 identity \u03b5 \u27e9\n \u03b5\n \u220e\n where open Mon m\n\n mono : SearchMono srch\n mono po _\u2219-mono_ f\u2286\u00b0g = mono\u1d2c (f\u2286\u00b0g \u2218 inj\u2081) \u2219-mono mono\u1d2e (f\u2286\u00b0g \u2218 inj\u2082)\n where\n mono\u1d2c = search-mono \u03bcA po _\u2219-mono_\n mono\u1d2e = search-mono \u03bcB po _\u2219-mono_\n\n hom : SearchMonHom srchMon\n hom cm f g\n = srchMon m (f \u2219\u00b0 g)\n \u2248\u27e8 search-hom \u03bcA cm (f \u2218 inj\u2081) (g \u2218 inj\u2081) -\u2219-\n search-hom \u03bcB cm (f \u2218 inj\u2082) (g \u2218 inj\u2082) \u27e9\n (sMon\u1d2c m (f \u2218 inj\u2081) \u2219 sMon\u1d2c m (g \u2218 inj\u2081)) \u2219\n (sMon\u1d2e m (f \u2218 inj\u2082) \u2219 sMon\u1d2e m (g \u2218 inj\u2082))\n \u2248\u27e8 \u2219-interchange (sMon\u1d2c m (f \u2218 inj\u2081)) (sMon\u1d2c m (g \u2218 inj\u2081))\n (sMon\u1d2e m (f \u2218 inj\u2082)) (sMon\u1d2e m (g \u2218 inj\u2082)) \u27e9\n srchMon m f \u2219 srchMon m g\n \u220e\n where open CMon cm\n\n swp : SearchSwap srch\n swp sg f hom = trans (\u2219-cong (search-swap \u03bcA sg (f \u2218 inj\u2081) hom) (search-swap \u03bcB sg (f \u2218 inj\u2082) hom)) (sym (hom g h))\n where open Sgrp sg\n g = \u03bb x \u2192 search \u03bcA _\u2219_ (\u03bb y \u2192 f (inj\u2081 y) x)\n h = \u03bb x \u2192 search \u03bcB _\u2219_ (\u03bb y \u2192 f (inj\u2082 y) x)\n-}\ninfixr 4 _\u00d7Search_\n\n-- liftM2 _,_ in the continuation monad\n_\u00d7Search_ : \u2200 {A B} \u2192 Search A \u2192 Search B \u2192 Search (A \u00d7 B)\n(search\u1d2c \u00d7Search search\u1d2e) m f = search\u1d2c m (\u03bb x\u2080 \u2192\n search\u1d2e m (\u03bb x\u2081 \u2192\n f (x\u2080 , x\u2081)))\n\n_\u00d7SearchInd'_ : \u2200 {A B} {s\u1d2c : Search A} {s\u1d2e : Search B}\n (Ps\u1d2c : SearchInd' s\u1d2c)\n (Ps\u1d2e : SearchInd' s\u1d2e) \u2192 SearchInd' (s\u1d2c \u00d7Search s\u1d2e)\n(Ps\u1d2c \u00d7SearchInd' Ps\u1d2e) P P\u2219 Pf = Ps\u1d2c P P\u2219 (\u03bb x \u2192 Ps\u1d2e P P\u2219 (curry Pf x))\n\n_\u00d7SearchInd_ : \u2200 {A B} {s\u1d2c : Search A} {s\u1d2e : Search B}\n (Ps\u1d2c : SearchInd s\u1d2c)\n (Ps\u1d2e : SearchInd s\u1d2e) \u2192 SearchInd (s\u1d2c \u00d7Search s\u1d2e)\n_\u00d7SearchInd_ {s\u1d2c = s\u1d2c} {s\u1d2e} Ps\u1d2c Ps\u1d2e {C} P {_\u2219_} P\u2219 Pf =\n Ps\u1d2c (\u03bb s \u2192 P (\u03bb f \u2192 s (\u03bb x \u2192 s\u1d2e _\u2219_ (curry f x)))) P\u2219 (\u03bb x \u2192 Ps\u1d2e (\u03bb s \u2192 P (\u03bb f \u2192 s (curry f x))) P\u2219 (curry Pf x))\n\n_\u00d7SearchExt_ : \u2200 {A B} {s\u1d2c : Search A} {s\u1d2e : Search B} \u2192\n SearchExt s\u1d2c \u2192 SearchExt s\u1d2e \u2192 SearchExt (s\u1d2c \u00d7Search s\u1d2e)\n(s\u1d2c-ext \u00d7SearchExt s\u1d2e-ext) sg f\u2257g = s\u1d2c-ext sg (s\u1d2e-ext sg \u2218 curry f\u2257g)\n\ninfixr 4 _\u00d7Sum_\n\n-- liftM2 _,_ in the continuation monad\n_\u00d7Sum_ : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u00d7 B)\n(sum\u1d2c \u00d7Sum sum\u1d2e) f = sum\u1d2c (\u03bb x\u2080 \u2192\n sum\u1d2e (\u03bb x\u2081 \u2192\n f (x\u2080 , x\u2081)))\n\ninfixr 4 _\u00d7\u03bc_\n\n_\u00d7\u03bc_ : \u2200 {A B} \u2192 SumProp A\n \u2192 SumProp B\n \u2192 SumProp (A \u00d7 B)\n(\u03bcA \u00d7\u03bc \u03bcB) = ?\n{-\n = mk srch ext mono swp eps hom\n where\n srch : Search _ -- (A \u00d7 B)\n srch = search \u03bcA \u00d7Search search \u03bcB\n srchMon = searchMonFromSearch srch\n\n ext : SearchExt srch\n ext sg f\u2257g = search-ext \u03bcA sg (search-ext \u03bcB sg \u2218 curry f\u2257g)\n\n eps : Search\u03b5 srchMon\n eps m = srchMon m (const \u03b5)\n \u2248\u27e8 search-ext \u03bcA sg (const (search-\u03b5 \u03bcB m)) \u27e9\n searchMon \u03bcA m (const \u03b5)\n \u2248\u27e8 search-\u03b5 \u03bcA m \u27e9\n \u03b5\n \u220e\n where open Mon m\n\n mono : SearchMono srch\n mono po \u2219mono f\u2264\u00b0g = mono\u1d2c (mono\u1d2e \u2218 curry f\u2264\u00b0g)\n where\n mono\u1d2c = search-mono \u03bcA po \u2219mono\n mono\u1d2e = search-mono \u03bcB po \u2219mono\n\n hom : SearchMonHom srchMon\n hom cm f g\n = srch _\u2219_ (f \u2219\u00b0 g)\n \u2248\u27e8 search-ext \u03bcA sg (\u03bb x \u2192 search-hom \u03bcB cm (curry f x) (curry g x)) \u27e9\n search \u03bcA _\u2219_ (\u03bb x \u2192 search \u03bcB _\u2219_ (curry f x) \u2219 search \u03bcB _\u2219_ (curry g x))\n \u2248\u27e8 search-hom \u03bcA cm (search \u03bcB _\u2219_ \u2218 curry f) (search \u03bcB _\u2219_ \u2218 curry g) \u27e9\n srch _\u2219_ f \u2219 srch _\u2219_ g\n \u220e where open CMon cm\n\n swp : SearchSwap srch\n swp sg f {g} hom = s\u1d2c\u1d2e (g \u2218 f)\n \u2248\u27e8 search-ext \u03bcA sg (\u03bb x \u2192 search-swap \u03bcB sg (curry f x) hom) \u27e9\n s\u1d2c (\u03bb z \u2192 g (\u03bb x \u2192 s\u1d2e (curry (flip f x) z)))\n \u2248\u27e8 search-swap \u03bcA sg (\u03bb z x \u2192 s\u1d2e (curry (flip f x) z)) hom \u27e9\n g (s\u1d2c\u1d2e \u2218 flip f)\n \u220e\n where open Sgrp sg\n s\u1d2c = search \u03bcA _\u2219_\n s\u1d2e = search \u03bcB _\u2219_\n s\u1d2c\u1d2e = srch _\u2219_\n-}\nsum-const : \u2200 {A} (\u03bcA : SumProp A) \u2192 \u2200 k \u2192 sum \u03bcA (const k) \u2261 Card \u03bcA * k\nsum-const \u03bcA k\n rewrite \u2115\u00b0.*-comm (Card \u03bcA) k\n | \u2261.sym (sum-lin \u03bcA (const 1) k)\n | proj\u2082 \u2115\u00b0.*-identity k = \u2261.refl\n\ninfixr 4 _\u00d7Sum-proj\u2081_ _\u00d7Sum-proj\u2081'_ _\u00d7Sum-proj\u2082_ _\u00d7Sum-proj\u2082'_\n\n_\u00d7Sum-proj\u2081_ : \u2200 {A B}\n (\u03bcA : SumProp A)\n (\u03bcB : SumProp B)\n f \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 Card \u03bcB * sum \u03bcA f\n(\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n rewrite sum-ext \u03bcA (sum-const \u03bcB \u2218 f)\n = sum-lin \u03bcA f (Card \u03bcB)\n\n_\u00d7Sum-proj\u2082_ : \u2200 {A B}\n (\u03bcA : SumProp A)\n (\u03bcB : SumProp B)\n f \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 Card \u03bcA * sum \u03bcB f\n(\u03bcA \u00d7Sum-proj\u2082 \u03bcB) f = sum-const \u03bcA (sum \u03bcB f)\n\n_\u00d7Sum-proj\u2081'_ : \u2200 {A B}\n (\u03bcA : SumProp A) (\u03bcB : SumProp B)\n {f} {g} \u2192\n sum \u03bcA f \u2261 sum \u03bcA g \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 sum (\u03bcA \u00d7\u03bc \u03bcB) (g \u2218 proj\u2081)\n(\u03bcA \u00d7Sum-proj\u2081' \u03bcB) {f} {g} sumf\u2261sumg\n rewrite (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n | (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) g\n | sumf\u2261sumg = \u2261.refl\n\n_\u00d7Sum-proj\u2082'_ : \u2200 {A B}\n (\u03bcA : SumProp A) (\u03bcB : SumProp B)\n {f} {g} \u2192\n sum \u03bcB f \u2261 sum \u03bcB g \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 sum (\u03bcA \u00d7\u03bc \u03bcB) (g \u2218 proj\u2082)\n(\u03bcA \u00d7Sum-proj\u2082' \u03bcB) sumf\u2261sumg = sum-ext \u03bcA (const sumf\u2261sumg)\n\n\u03bc-view : \u2200 {A B} \u2192 (A \u2192 B) \u2192 SumProp A \u2192 SumProp B\n\u03bc-view {A}{B} A\u2192B \u03bcA = mk search\u1d2e ext mono swp eps hom\n where\n search\u1d2e : Search B\n search\u1d2e m f = search \u03bcA m (f \u2218 A\u2192B)\n searchMon\u1d2e = searchMonFromSearch search\u1d2e\n\n ext : SearchExt search\u1d2e\n ext m f\u2257g = search-ext \u03bcA m (f\u2257g \u2218 A\u2192B)\n\n eps : Search\u03b5 searchMon\u1d2e\n eps = search-\u03b5 \u03bcA\n\n mono : SearchMono search\u1d2e\n mono po \u2219mono f\u2264\u00b0g = search-mono \u03bcA po \u2219mono (f\u2264\u00b0g \u2218 A\u2192B)\n\n hom : SearchMonHom searchMon\u1d2e\n hom m f g = search-hom \u03bcA m (f \u2218 A\u2192B) (g \u2218 A\u2192B)\n\n swp : SearchSwap search\u1d2e\n swp sg f hom = search-swap \u03bcA sg (f \u2218 A\u2192B) hom\n\n\u03bc-iso : \u2200 {A B} \u2192 (A \u2194 B) \u2192 SumProp A \u2192 SumProp B\n\u03bc-iso A\u2194B = \u03bc-view (_\u27e8$\u27e9_ (Inverse.to A\u2194B))\n\n\u03bc-view-preserve : \u2200 {A B} (A\u2192B : A \u2192 B)(B\u2192A : B \u2192 A)(A\u2248B : id \u2257 B\u2192A \u2218 A\u2192B) f (\u03bcA : SumProp A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-view A\u2192B \u03bcA) (f \u2218 B\u2192A)\n\u03bc-view-preserve A\u2192B B\u2192A A\u2248B f \u03bcA = sum-ext \u03bcA (\u2261.cong f \u2218 A\u2248B)\n\n\u03bc-iso-preserve : \u2200 {A B} (A\u2194B : A \u2194 B) f (\u03bcA : SumProp A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-iso A\u2194B \u03bcA) (f \u2218 _\u27e8$\u27e9_ (Inverse.from A\u2194B))\n\u03bc-iso-preserve A\u2194B f \u03bcA = \u03bc-view-preserve (_\u27e8$\u27e9_ (Inverse.to A\u2194B)) (_\u27e8$\u27e9_ (Inverse.from A\u2194B))\n (\u2261.sym \u2218 Inverse.left-inverse-of A\u2194B) f \u03bcA\n\nopen import Data.Fin hiding (_+_)\nopen import Data.Vec.NP as Vec renaming (map to vmap; sum to vsum; foldr to vfoldr)\n\nvmsum : \u2200 m {n} \u2192 let open Mon m in\n Vec C n \u2192 C\nvmsum m = vfoldr _ _\u2219_ \u03b5\n where open Monoid m\n\nsearchMonFin : \u2200 n \u2192 SearchMon (Fin n)\nsearchMonFin n m f = vmsum m (vmap f (allFin n)) -- or vsum (tabulate f)\n\nsearchFinSuc : \u2200 n \u2192 Search (Fin (suc n))\nsearchFinSuc n _\u2219_ f = vfoldr _ _\u2219_ (f zero) (vmap (f \u2218 suc) (allFin n))\n\nsumFin : \u2200 n \u2192 Sum (Fin n)\nsumFin n f = vsum (vmap f (allFin n)) -- or vsum (tabulate f)\n\n\u03bcMaybe : \u2200 {A} \u2192 SumProp A \u2192 SumProp (Maybe A)\n\u03bcMaybe \u03bcA = \u03bc-iso (FI.sym Maybe\u2194\u22a4\u228e) (\u03bc\u22a4 +\u03bc \u03bcA)\n\n\u03bcMaybe^ : \u2200 {A} n \u2192 SumProp A \u2192 SumProp (Maybe^ n A)\n\u03bcMaybe^ zero \u03bcA = \u03bcA\n\u03bcMaybe^ (suc n) \u03bcA = \u03bcMaybe (\u03bcMaybe^ n \u03bcA)\n\n\u03bcFin : \u2200 n \u2192 SumProp (Fin (suc n))\n\u03bcFin n = \u03bc-iso (Maybe^\u22a4\u2194Fin1+ n) (\u03bcMaybe^ n \u03bc\u22a4)\n{-\n\u03bcFin n = mk (searchFin n) ext eps hom sumFin-mon sumFin-swap\n module SumFin where\n ext : SearchMonExt (searchFin n)\n ext m f\u2257g = {!map-ext f\u2257g (allFin n)!}\n where open Mon m\n\n eps : Search\u03b5 (searchFin n)\n eps = {!!}\n\n hom : SearchMonHom (searchFin n)\n hom m f g = {!sum-linear f g (allFin n)!}\n\n sumFin-mon : SumMono (sumFin n)\n sumFin-mon f\u2264\u00b0g = sum-mono f\u2264\u00b0g (allFin n)\n\n sumFin-swap : SumSwap (sumFin n)\n sumFin-swap f {sum\u02e3} sum\u02e3-linear = inner (allFin n) where\n inner : \u2200 {m}(xs : Vec (Fin n) m) \u2192 vsum (vmap (sum\u02e3 \u2218 f) xs) \u2261 sum\u02e3 (\u03bb x \u2192 vsum (vmap (flip f x) xs))\n inner [] = \u2261.sym (SumLinear.sum-lin sum\u02e3-linear (const 1337) 0)\n inner (x \u2237 xs) rewrite inner xs = \u2261.sym\n (SumLinear.sum-hom sum\u02e3-linear (f x)\n (\u03bb y \u2192 vsum (vmap (flip f y) xs)))\n-}\n\n\u03bcVec : \u2200 {A} (\u03bcA : SumProp A) n \u2192 SumProp (Vec A n)\n\u03bcVec \u03bcA zero = \u03bc-view (const []) \u03bc\u22a4\n\u03bcVec \u03bcA (suc n) = \u03bc-view (uncurry _\u2237_) (\u03bcA \u00d7\u03bc \u03bcVec \u03bcA n)\n\n{-\nsearchVec : \u2200 {A} n \u2192 Search A \u2192 Search (Vec A n)\nsearchVec zero search\u1d2c m f = f []\nsearchVec (suc n) search\u1d2c m f = search\u1d2c m (\u03bb x \u2192 searchVec n search\u1d2c m (f \u2218 _\u2237_ x))\n\nsumVec : \u2200 {A} n \u2192 Sum A \u2192 Sum (Vec A n)\nsumVec n sum\u1d2c = searchVec n (\u03bb m \u2192 {!sum\u1d2c!}) \u2115+.monoid\n\nsumVec zero sum\u1d2c f = f []\nsumVec (suc n) sum\u1d2c f = (sum\u1d2c \u00d7Sum sumVec n sum\u1d2c) (\u03bb { (x , xs) \u2192 f (x \u2237 xs) })\n\n\u03bcVec' : \u2200 {A} (\u03bcA : SumProp A) n \u2192 SumProp (Vec A n)\n\u03bcVec' {A} \u03bcA n = mk (searchVec n (search \u03bcA)) (mk (sumVec-lin n) (sumVec-hom n)) (sumVec-ext n) (sumVec-mon n) (sumVec-swap n)\n where\n sV = flip sumVec (sum \u03bcA)\n\n sumVec-ext : \u2200 n \u2192 SumExt (sV n)\n sumVec-ext zero f\u2257g = f\u2257g []\n sumVec-ext (suc n) f\u2257g = sum-ext \u03bcA (\u03bb x \u2192 sumVec-ext n (f\u2257g \u2218 _\u2237_ x))\n\n sumVec-lin : \u2200 n \u2192 SumLin (sV n)\n sumVec-lin zero f k = \u2261.refl\n sumVec-lin (suc n) f k rewrite sum-ext \u03bcA (\u03bb x \u2192 sumVec-lin n (f \u2218 _\u2237_ x) k)\n = sum-lin \u03bcA (\u03bb x \u2192 sV n (f \u2218 _\u2237_ x)) k\n\n sumVec-hom : \u2200 n \u2192 SumHom (sV n)\n sumVec-hom zero f g = \u2261.refl\n sumVec-hom (suc n) f g rewrite sum-ext \u03bcA (\u03bb x \u2192 sumVec-hom n (f \u2218 _\u2237_ x) (g \u2218 _\u2237_ x)) \n = sum-hom \u03bcA (\u03bb x \u2192 sV n (f \u2218 _\u2237_ x)) (\u03bb x \u2192 sV n (g \u2218 _\u2237_ x))\n\n sumVec-mon : \u2200 n \u2192 SumMono (sV n)\n sumVec-mon zero f\u2264\u00b0g = f\u2264\u00b0g []\n sumVec-mon (suc n) f\u2264\u00b0g = sum-mono \u03bcA (\u03bb x \u2192 sumVec-mon n (f\u2264\u00b0g \u2218 _\u2237_ x))\n\n sumVec-swap : \u2200 n \u2192 SumSwap (sV n)\n sumVec-swap zero f \u03bc\u02e3-linear = \u2261.refl\n sumVec-swap (suc n) f {sum\u02e3} \u03bc\u02e3-linear rewrite sum-ext \u03bcA (\u03bb x \u2192 sumVec-swap n (f \u2218 _\u2237_ x) \u03bc\u02e3-linear)\n = sum-swap \u03bcA (\u03bb z x \u2192 sumVec n (sum \u03bcA) (\u03bb y \u2192 f (z \u2237 y) x)) \u03bc\u02e3-linear\n-}\n\nsearchVec-++ : \u2200 {A} n {m} (\u03bcA : SumProp A) sg\n \u2192 let open Sgrp sg in\n (f : Vec A (n + m) \u2192 C)\n \u2192 search (\u03bcVec \u03bcA (n + m)) _\u2219_ f\n \u2248 search (\u03bcVec \u03bcA n) _\u2219_ (\u03bb xs \u2192 search (\u03bcVec \u03bcA m) _\u2219_\n (\u03bb ys \u2192 f (xs ++ ys)))\nsearchVec-++ zero \u03bcA sg f = Sgrp.refl sg\nsearchVec-++ (suc n) \u03bcA sg f = search-ext \u03bcA sg (\u03bb x \u2192\n searchVec-++ n \u03bcA sg (f \u2218 _\u2237_ x))\n\nsumVec-swap : \u2200 {A} n {m} (\u03bcA : SumProp A)(f : Vec A (n + m) \u2192 \u2115)\n \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 f (xs ++ ys)))\nsumVec-swap n {m} \u03bcA f = sum-swap (\u03bcVec \u03bcA n) (\u03bcVec \u03bcA m) (\u03bb xs ys \u2192 f (xs ++ ys))\n\nswapS : \u2200 {A B} \u2192 SumProp (A \u00d7 B) \u2192 SumProp (B \u00d7 A)\nswapS = \u03bc-view Data.Product.swap\n -- \u03bc-iso \u00d7-comm\n\nswapS-preserve : \u2200 {A B} f (\u03bcA\u00d7B : SumProp (A \u00d7 B)) \u2192 sum \u03bcA\u00d7B f \u2261 sum (swapS \u03bcA\u00d7B) (f \u2218 Data.Product.swap)\nswapS-preserve f \u03bcA\u00d7B = \u03bc-view-preserve Data.Product.swap Data.Product.swap (\u03bb x \u2192 \u2261.refl) f \u03bcA\u00d7B {- or \u2261.refl -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"import Level as L\nopen import Type\nopen import Function\nopen import Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\nopen import Data.Unit hiding (_\u2264_)\nopen import Data.Sum\nopen import Data.Maybe.NP\nopen import Data.Product\nopen import Data.Bits\nopen import Data.Bool.NP as Bool\nopen import Function.Equality using (_\u27e8$\u27e9_)\nimport Function.Inverse as FI\nopen FI using (_\u2194_; module Inverse)\nopen import Function.Related.TypeIsomorphisms.NP\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_; _\u2257_)\n\nmodule sum where\n\n_\u2264\u00b0_ : \u2200 {A : \u2605}(f g : A \u2192 \u2115) \u2192 \u2605\nf \u2264\u00b0 g = \u2200 x \u2192 f x \u2264 g x\n\nSemigroup\u2080 = Semigroup L.zero L.zero\nMonoid\u2080 = Monoid L.zero L.zero\nCommutativeMonoid\u2080 = CommutativeMonoid L.zero L.zero\nPreorder\u2080 = Preorder L.zero L.zero L.zero\n\nmodule PO (po : Preorder\u2080) where\n open Preorder po public renaming (_\u223c_ to _\u2286_)\n _\u2286\u00b0_ : \u2200 {A : \u2605}(f g : A \u2192 Carrier) \u2192 \u2605\n f \u2286\u00b0 g = \u2200 x \u2192 f x \u2286 g x\n \nmodule SgrpExtra (sg : Semigroup\u2080) where\n open Semigroup sg\n open Setoid-Reasoning (Semigroup.setoid sg) public\n C : \u2605\n C = Carrier\n _\u2248\u00b0_ : \u2200 {A : \u2605} (f g : A \u2192 C) \u2192 \u2605\n f \u2248\u00b0 g = \u2200 x \u2192 f x \u2248 g x \n _\u2219\u00b0_ : \u2200 {A : \u2605} (f g : A \u2192 C) \u2192 A \u2192 C\n (f \u2219\u00b0 g) x = f x \u2219 g x\n infixl 7 _-\u2219-_\n _-\u2219-_ : _\u2219_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_\n _-\u2219-_ = \u2219-cong\n\nmodule Sgrp (sg : Semigroup\u2080) where\n open Semigroup sg public\n open SgrpExtra sg public\n\nmodule Mon (m : Monoid\u2080) where\n open Monoid m public\n sg = semigroup\n open SgrpExtra semigroup public\n\nmodule CMon (cm : CommutativeMonoid\u2080) where\n open CommutativeMonoid cm public\n sg = semigroup\n m = monoid\n open SgrpExtra sg public\n\n \u2219-interchange : Interchange _\u2248_ _\u2219_ _\u2219_\n \u2219-interchange = InterchangeFromAssocCommCong.\u2219-interchange\n _\u2248_ isEquivalence\n _\u2219_ assoc comm (\u03bb _ \u2192 flip \u2219-cong refl)\n\nSearch : \u2605 \u2192 \u2605\u2081\nSearch A = \u2200 {B} \u2192 (_\u2219_ : B \u2192 B \u2192 B) \u2192 (A \u2192 B) \u2192 B\n\nSearchMon : \u2605 \u2192 \u2605\u2081\nSearchMon A = (m : Monoid\u2080) \u2192 let open Mon m in\n (A \u2192 C) \u2192 C\n\nSum : \u2605 \u2192 \u2605\nSum A = (A \u2192 \u2115) \u2192 \u2115\n\nCount : \u2605 \u2192 \u2605\nCount A = (A \u2192 Bit) \u2192 \u2115\n\nSearchExt : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchExt s\u1d2c = \u2200 sg {f g} \u2192 let open Sgrp sg in\n f \u2248\u00b0 g \u2192 s\u1d2c _\u2219_ f \u2248 s\u1d2c _\u2219_ g\n\nSumExt : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumExt sum\u1d2c = \u2200 {f g} \u2192 f \u2257 g \u2192 sum\u1d2c f \u2261 sum\u1d2c g\n\nCountExt : \u2200 {A} \u2192 Count A \u2192 \u2605\nCountExt count\u1d2c = \u2200 {f g} \u2192 f \u2257 g \u2192 count\u1d2c f \u2261 count\u1d2c g\n\nSearch\u03b5 : \u2200 {A} \u2192 SearchMon A \u2192 \u2605\u2081\nSearch\u03b5 s\u1d2c = \u2200 m \u2192 let open Mon m in\n s\u1d2c m (const \u03b5) \u2248 \u03b5\n\nSumZero : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumZero sum\u1d2c = sum\u1d2c (\u03bb _ \u2192 0) \u2261 0\n\nSumLin : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumLin sum\u1d2c = \u2200 f k \u2192 sum\u1d2c (\u03bb x \u2192 k * f x) \u2261 k * sum\u1d2c f\n\nSearchHom : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchHom s\u1d2c = \u2200 sg f g \u2192 let open Sgrp sg in\n s\u1d2c _\u2219_ (f \u2219\u00b0 g) \u2248 s\u1d2c _\u2219_ f \u2219 s\u1d2c _\u2219_ g\n\nSearchMonHom : \u2200 {A} \u2192 SearchMon A \u2192 \u2605\u2081\nSearchMonHom s\u1d2c = \u2200 (cm : CommutativeMonoid\u2080) f g \u2192\n let open CMon cm in\n s\u1d2c m (f \u2219\u00b0 g) \u2248 s\u1d2c m f \u2219 s\u1d2c m g\n\n{-search-hom\u2032 :\n \u2200 {a b}\n {A : Set a} {B : Set b} {R}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : R \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom\u2032 = ?\n{-\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n-}\n-}\n\nSumHom : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumHom sum\u1d2c = \u2200 f g \u2192 sum\u1d2c (\u03bb x \u2192 f x + g x) \u2261 sum\u1d2c f + sum\u1d2c g\n\nSearchMono : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchMono s\u1d2c = \u2200 {C : \u2605} (_\u2286_ : C \u2192 C \u2192 \u2605) \u2192 -- let open PO {C} _\u2286_ in\n \u2200 {_\u2219_} (\u2219-mono : _\u2219_ Preserves\u2082 _\u2286_ \u27f6 _\u2286_ \u27f6 _\u2286_)\n {f g} \u2192\n (\u2200 x \u2192 f x \u2286 g x) \u2192 s\u1d2c _\u2219_ f \u2286 s\u1d2c _\u2219_ g\n\nSumMono : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumMono sum\u1d2c = \u2200 {f g} \u2192 f \u2264\u00b0 g \u2192 sum\u1d2c f \u2264 sum\u1d2c g\n\nSearchSwap : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchSwap {A} s\u1d2c = \u2200 {B} sg f \u2192 let open Sgrp sg in \u2200 {s\u1d2e : (B \u2192 C) \u2192 C}\n\n \u2192 (hom : \u2200 f g \u2192 s\u1d2e (f \u2219\u00b0 g) \u2248 s\u1d2e f \u2219 s\u1d2e g)\n \u2192 s\u1d2c _\u2219_ (s\u1d2e \u2218 f) \u2248 s\u1d2e (s\u1d2c _\u2219_ \u2218 flip f)\n\nSumLin\u21d2SumZero : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumLin sum \u2192 SumZero sum\nSumLin\u21d2SumZero sum-lin = sum-lin (\u03bb _ \u2192 0) 0\n\nmkSumExt : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumMono sum \u2192 SumExt sum\nmkSumExt sum-mono f\u2257g = \u2115\u2264.antisym (sum-mono (\u2115\u2264.reflexive \u2218 f\u2257g)) (sum-mono (\u2115\u2264.reflexive \u2218 \u2261.sym \u2218 f\u2257g))\n\nmkSumMon : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumExt sum \u2192 SumHom sum \u2192 SumMono sum\nmkSumMon {sum = sum} sum-ext sum-hom {f} {g} f\u2264\u00b0g =\n sum f \u2264\u27e8 m\u2264m+n _ _ \u27e9\n sum f + sum (\u03bb x \u2192 g x \u2238 f x) \u2261\u27e8 \u2261.sym (sum-hom _ _) \u27e9\n sum (\u03bb x \u2192 f x + (g x \u2238 f x)) \u2261\u27e8 sum-ext (m+n\u2238m\u2261n \u2218 f\u2264\u00b0g) \u27e9\n sum g \u220e where open \u2264-Reasoning\n\nsearchMonFromSearch : \u2200 {A} \u2192 Search A \u2192 SearchMon A\nsearchMonFromSearch s m = s _\u2219_ where open Mon m\n\nrecord SumProp A : \u2605\u2081 where\n constructor mk\n field\n search : Search A\n search-ext : SearchExt search\n search-mono : SearchMono search\n search-swap : SearchSwap search\n searchMon : SearchMon A\n searchMon m = let open Mon m in search _\u2219_\n field\n search-\u03b5 : Search\u03b5 searchMon\n search-hom : SearchMonHom searchMon\n sum : Sum A\n sum = search _+_\n sum-ext : SumExt sum\n sum-ext = search-ext \u2115+.semigroup\n sum-zero : SumZero sum\n sum-zero = search-\u03b5 \u2115+.monoid\n sum-hom : SumHom sum\n sum-hom = search-hom \u2115\u00b0.+-commutativeMonoid\n sum-mono : SumMono sum\n sum-mono = search-mono _\u2264_ _+-mono_\n\n sum-lin : SumLin sum\n sum-lin f zero = sum-zero\n sum-lin f (suc k) = ?\n {-\n rewrite sum-hom f (\u03bb x \u2192 k * f x)\n | sum-lin f k = {!\u2261.refl!}\n -}\n\n Card : \u2115\n Card = sum (const 1)\n\n count : Count A\n count f = sum (Bool.to\u2115 \u2218 f)\n\n count-ext : CountExt count\n count-ext f\u2257g = sum-ext (\u2261.cong Bool.to\u2115 \u2218 f\u2257g)\n\nopen SumProp public\n\nsearch-swap' : \u2200 {A B} cm (\u03bcA : SumProp A) (\u03bcB : SumProp B) f \u2192\n let open CMon cm\n s\u1d2c = search \u03bcA _\u2219_\n s\u1d2e = search \u03bcB _\u2219_ in\n s\u1d2c (s\u1d2e \u2218 f) \u2248 s\u1d2e (s\u1d2c \u2218 flip f)\nsearch-swap' cm \u03bcA \u03bcB f = search-swap \u03bcA sg f (search-hom \u03bcB cm)\n where open CMon cm\n\nsum-swap : \u2200 {A B} (\u03bcA : SumProp A) (\u03bcB : SumProp B) f \u2192\n sum \u03bcA (sum \u03bcB \u2218 f) \u2261 sum \u03bcB (sum \u03bcA \u2218 flip f)\nsum-swap = search-swap' \u2115\u00b0.+-commutativeMonoid\n\n_\u2248Sum_ : \u2200 {A} \u2192 (sum\u2080 sum\u2081 : Sum A) \u2192 \u2605\nsum\u2080 \u2248Sum sum\u2081 = \u2200 f \u2192 sum\u2080 f \u2261 sum\u2081 f\n\n\u03bc\u22a4 : SumProp \u22a4\n\u03bc\u22a4 = mk search\u22a4 ext mono search\u22a4-swap eps hom\n where\n search\u22a4 : Search \u22a4\n search\u22a4 _ f = f _\n\n searchMon\u22a4 : SearchMon \u22a4\n searchMon\u22a4 _ f = f _\n\n ext : SearchExt search\u22a4\n ext _ f\u2257g = f\u2257g _\n\n mono : SearchMono search\u22a4\n mono _ _ f\u2264\u00b0g = f\u2264\u00b0g _\n\n eps : Search\u03b5 searchMon\u22a4\n eps m = Monoid.refl m\n\n search\u22a4-swap : SearchSwap search\u22a4\n search\u22a4-swap sg f hom = refl\n where open Sgrp sg\n\n hom : SearchMonHom searchMon\u22a4\n hom m f g = CommutativeMonoid.refl m\n\n\u03bcBit : SumProp Bit\n\u03bcBit = mk searchBit ext mono swp eps hom\n where\n searchBit : Search Bit\n searchBit _\u2219_ f = f 0b \u2219 f 1b\n\n searchMonBit : SearchMon Bit\n searchMonBit m f = f 0b \u2219 f 1b\n where open Mon m\n\n ext : SearchExt searchBit\n ext sg f\u2257g = f\u2257g 0b -\u2219- f\u2257g 1b\n where open Sgrp sg\n\n mono : SearchMono searchBit\n mono po _\u2219-mono_ f\u2286\u00b0g = f\u2286\u00b0g 0b \u2219-mono f\u2286\u00b0g 1b\n\n eps : Search\u03b5 searchMonBit\n eps m = proj\u2081 identity \u03b5\n where open Monoid m\n\n hom : SearchMonHom searchMonBit\n hom cm f g = \u2219-interchange (f 0b) (g 0b) (f 1b) (g 1b)\n where open CMon cm\n\n sumBit : Sum Bit\n sumBit f = f 0b + f 1b\n\n swp : SearchSwap searchBit\n swp sg f hom\u1d2e = sym (hom\u1d2e (f 0b) (f 1b))\n where open Sgrp sg\n\ninfixr 4 _+Search_\n\n_+Search_ : \u2200 {A B} \u2192 Search A \u2192 Search B \u2192 Search (A \u228e B)\n(search\u1d2c +Search search\u1d2e) _\u2219_ f = search\u1d2c _\u2219_ (f \u2218 inj\u2081) \u2219 search\u1d2e _\u2219_ (f \u2218 inj\u2082)\n\ninfixr 4 _+Sum_\n\n_+Sum_ : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u228e B)\n(sum\u1d2c +Sum sum\u1d2e) f = sum\u1d2c (f \u2218 inj\u2081) + sum\u1d2e (f \u2218 inj\u2082)\n\n_+\u03bc_ : \u2200 {A B} \u2192 SumProp A\n \u2192 SumProp B\n \u2192 SumProp (A \u228e B)\n(\u03bcA +\u03bc \u03bcB) = mk srch ext mono swp eps hom\n where\n s\u1d2c = search \u03bcA\n s\u1d2e = search \u03bcB\n srch = s\u1d2c +Search s\u1d2e\n sMon\u1d2c = searchMon \u03bcA\n sMon\u1d2e = searchMon \u03bcB\n srchMon = searchMonFromSearch srch\n ext : SearchExt srch\n ext sg f\u2257g = search-ext \u03bcA sg (f\u2257g \u2218 inj\u2081) -\u2219- search-ext \u03bcB sg (f\u2257g \u2218 inj\u2082)\n where open Sgrp sg\n\n eps : Search\u03b5 srchMon\n eps m = srchMon m (const \u03b5)\n \u2248\u27e8 search-\u03b5 \u03bcA m -\u2219- search-\u03b5 \u03bcB m \u27e9\n \u03b5 \u2219 \u03b5\n \u2248\u27e8 proj\u2081 identity \u03b5 \u27e9\n \u03b5\n \u220e\n where open Mon m\n\n mono : SearchMono srch\n mono po _\u2219-mono_ f\u2286\u00b0g = mono\u1d2c (f\u2286\u00b0g \u2218 inj\u2081) \u2219-mono mono\u1d2e (f\u2286\u00b0g \u2218 inj\u2082)\n where\n mono\u1d2c = search-mono \u03bcA po _\u2219-mono_\n mono\u1d2e = search-mono \u03bcB po _\u2219-mono_\n\n hom : SearchMonHom srchMon\n hom cm f g\n = srchMon m (f \u2219\u00b0 g)\n \u2248\u27e8 search-hom \u03bcA cm (f \u2218 inj\u2081) (g \u2218 inj\u2081) -\u2219-\n search-hom \u03bcB cm (f \u2218 inj\u2082) (g \u2218 inj\u2082) \u27e9\n (sMon\u1d2c m (f \u2218 inj\u2081) \u2219 sMon\u1d2c m (g \u2218 inj\u2081)) \u2219\n (sMon\u1d2e m (f \u2218 inj\u2082) \u2219 sMon\u1d2e m (g \u2218 inj\u2082))\n \u2248\u27e8 \u2219-interchange (sMon\u1d2c m (f \u2218 inj\u2081)) (sMon\u1d2c m (g \u2218 inj\u2081))\n (sMon\u1d2e m (f \u2218 inj\u2082)) (sMon\u1d2e m (g \u2218 inj\u2082)) \u27e9\n srchMon m f \u2219 srchMon m g\n \u220e \n where open CMon cm\n\n swp : SearchSwap srch\n swp sg f hom = trans (\u2219-cong (search-swap \u03bcA sg (f \u2218 inj\u2081) hom) (search-swap \u03bcB sg (f \u2218 inj\u2082) hom)) (sym (hom g h))\n where open Sgrp sg\n g = \u03bb x \u2192 search \u03bcA _\u2219_ (\u03bb y \u2192 f (inj\u2081 y) x)\n h = \u03bb x \u2192 search \u03bcB _\u2219_ (\u03bb y \u2192 f (inj\u2082 y) x)\n\ninfixr 4 _\u00d7Search_\n\n-- liftM2 _,_ in the continuation monad\n_\u00d7Search_ : \u2200 {A B} \u2192 Search A \u2192 Search B \u2192 Search (A \u00d7 B)\n(search\u1d2c \u00d7Search search\u1d2e) m f = search\u1d2c m (\u03bb x\u2080 \u2192\n search\u1d2e m (\u03bb x\u2081 \u2192\n f (x\u2080 , x\u2081)))\n\n_\u00d7SearchExt_ : \u2200 {A B} {s\u1d2c : Search A} {s\u1d2e : Search B} \u2192\n SearchExt s\u1d2c \u2192 SearchExt s\u1d2e \u2192 SearchExt (s\u1d2c \u00d7Search s\u1d2e)\n(s\u1d2c-ext \u00d7SearchExt s\u1d2e-ext) sg f\u2257g = s\u1d2c-ext sg (s\u1d2e-ext sg \u2218 curry f\u2257g)\n\ninfixr 4 _\u00d7Sum_\n\n-- liftM2 _,_ in the continuation monad\n_\u00d7Sum_ : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u00d7 B)\n(sum\u1d2c \u00d7Sum sum\u1d2e) f = sum\u1d2c (\u03bb x\u2080 \u2192\n sum\u1d2e (\u03bb x\u2081 \u2192\n f (x\u2080 , x\u2081)))\n\ninfixr 4 _\u00d7\u03bc_\n\n_\u00d7\u03bc_ : \u2200 {A B} \u2192 SumProp A\n \u2192 SumProp B\n \u2192 SumProp (A \u00d7 B)\n(\u03bcA \u00d7\u03bc \u03bcB)\n = mk srch ext mono swp eps hom\n where\n srch = search \u03bcA \u00d7Search search \u03bcB\n srchMon = searchMonFromSearch srch\n\n ext : SearchExt srch\n ext sg f\u2257g = search-ext \u03bcA sg (search-ext \u03bcB sg \u2218 curry f\u2257g)\n\n eps : Search\u03b5 srchMon\n eps m = srchMon m (const \u03b5)\n \u2248\u27e8 search-ext \u03bcA sg (const (search-\u03b5 \u03bcB m)) \u27e9\n searchMon \u03bcA m (const \u03b5)\n \u2248\u27e8 search-\u03b5 \u03bcA m \u27e9\n \u03b5\n \u220e\n where open Mon m\n\n mono : SearchMono srch\n mono po \u2219mono f\u2264\u00b0g = mono\u1d2c (mono\u1d2e \u2218 curry f\u2264\u00b0g)\n where\n mono\u1d2c = search-mono \u03bcA po \u2219mono\n mono\u1d2e = search-mono \u03bcB po \u2219mono\n\n hom : SearchMonHom srchMon\n hom cm f g\n = srch _\u2219_ (f \u2219\u00b0 g)\n \u2248\u27e8 search-ext \u03bcA sg (\u03bb x \u2192 search-hom \u03bcB cm (curry f x) (curry g x)) \u27e9\n search \u03bcA _\u2219_ (\u03bb x \u2192 search \u03bcB _\u2219_ (curry f x) \u2219 search \u03bcB _\u2219_ (curry g x))\n \u2248\u27e8 search-hom \u03bcA cm (search \u03bcB _\u2219_ \u2218 curry f) (search \u03bcB _\u2219_ \u2218 curry g) \u27e9\n srch _\u2219_ f \u2219 srch _\u2219_ g\n \u220e where open CMon cm\n\n swp : SearchSwap srch\n swp sg f {g} hom = s\u1d2c\u1d2e (g \u2218 f)\n \u2248\u27e8 search-ext \u03bcA sg (\u03bb x \u2192 search-swap \u03bcB sg (curry f x) hom) \u27e9\n s\u1d2c (\u03bb z \u2192 g (\u03bb x \u2192 s\u1d2e (curry (flip f x) z)))\n \u2248\u27e8 search-swap \u03bcA sg (\u03bb z x \u2192 s\u1d2e (curry (flip f x) z)) hom \u27e9\n g (s\u1d2c\u1d2e \u2218 flip f)\n \u220e\n where open Sgrp sg\n s\u1d2c = search \u03bcA _\u2219_\n s\u1d2e = search \u03bcB _\u2219_\n s\u1d2c\u1d2e = srch _\u2219_\n\n\nsum-const : \u2200 {A} (\u03bcA : SumProp A) \u2192 \u2200 k \u2192 sum \u03bcA (const k) \u2261 Card \u03bcA * k \nsum-const \u03bcA k\n rewrite \u2115\u00b0.*-comm (Card \u03bcA) k\n | \u2261.sym (sum-lin \u03bcA (const 1) k)\n | proj\u2082 \u2115\u00b0.*-identity k = \u2261.refl\n\ninfixr 4 _\u00d7Sum-proj\u2081_ _\u00d7Sum-proj\u2081'_ _\u00d7Sum-proj\u2082_ _\u00d7Sum-proj\u2082'_\n\n_\u00d7Sum-proj\u2081_ : \u2200 {A B}\n (\u03bcA : SumProp A)\n (\u03bcB : SumProp B)\n f \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 Card \u03bcB * sum \u03bcA f\n(\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n rewrite sum-ext \u03bcA (sum-const \u03bcB \u2218 f)\n = sum-lin \u03bcA f (Card \u03bcB)\n\n_\u00d7Sum-proj\u2082_ : \u2200 {A B}\n (\u03bcA : SumProp A)\n (\u03bcB : SumProp B)\n f \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 Card \u03bcA * sum \u03bcB f\n(\u03bcA \u00d7Sum-proj\u2082 \u03bcB) f = sum-const \u03bcA (sum \u03bcB f)\n\n_\u00d7Sum-proj\u2081'_ : \u2200 {A B}\n (\u03bcA : SumProp A) (\u03bcB : SumProp B)\n {f} {g} \u2192\n sum \u03bcA f \u2261 sum \u03bcA g \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 sum (\u03bcA \u00d7\u03bc \u03bcB) (g \u2218 proj\u2081)\n(\u03bcA \u00d7Sum-proj\u2081' \u03bcB) {f} {g} sumf\u2261sumg\n rewrite (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n | (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) g\n | sumf\u2261sumg = \u2261.refl\n\n_\u00d7Sum-proj\u2082'_ : \u2200 {A B}\n (\u03bcA : SumProp A) (\u03bcB : SumProp B)\n {f} {g} \u2192\n sum \u03bcB f \u2261 sum \u03bcB g \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 sum (\u03bcA \u00d7\u03bc \u03bcB) (g \u2218 proj\u2082)\n(\u03bcA \u00d7Sum-proj\u2082' \u03bcB) sumf\u2261sumg = sum-ext \u03bcA (const sumf\u2261sumg)\n\n\u03bc-view : \u2200 {A B} \u2192 (A \u2192 B) \u2192 SumProp A \u2192 SumProp B\n\u03bc-view {A}{B} A\u2192B \u03bcA = mk search\u1d2e ext mono swp eps hom\n where\n search\u1d2e : Search B\n search\u1d2e m f = search \u03bcA m (f \u2218 A\u2192B)\n searchMon\u1d2e = searchMonFromSearch search\u1d2e\n\n ext : SearchExt search\u1d2e\n ext m f\u2257g = search-ext \u03bcA m (f\u2257g \u2218 A\u2192B)\n\n eps : Search\u03b5 searchMon\u1d2e\n eps = search-\u03b5 \u03bcA\n\n mono : SearchMono search\u1d2e\n mono po \u2219mono f\u2264\u00b0g = search-mono \u03bcA po \u2219mono (f\u2264\u00b0g \u2218 A\u2192B)\n\n hom : SearchMonHom searchMon\u1d2e\n hom m f g = search-hom \u03bcA m (f \u2218 A\u2192B) (g \u2218 A\u2192B)\n\n swp : SearchSwap search\u1d2e\n swp sg f hom = search-swap \u03bcA sg (f \u2218 A\u2192B) hom\n\n\u03bc-iso : \u2200 {A B} \u2192 (A \u2194 B) \u2192 SumProp A \u2192 SumProp B\n\u03bc-iso A\u2194B = \u03bc-view (_\u27e8$\u27e9_ (Inverse.to A\u2194B))\n\n\u03bc-view-preserve : \u2200 {A B} (A\u2192B : A \u2192 B)(B\u2192A : B \u2192 A)(A\u2248B : id \u2257 B\u2192A \u2218 A\u2192B) f (\u03bcA : SumProp A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-view A\u2192B \u03bcA) (f \u2218 B\u2192A)\n\u03bc-view-preserve A\u2192B B\u2192A A\u2248B f \u03bcA = sum-ext \u03bcA (\u2261.cong f \u2218 A\u2248B)\n\n\u03bc-iso-preserve : \u2200 {A B} (A\u2194B : A \u2194 B) f (\u03bcA : SumProp A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-iso A\u2194B \u03bcA) (f \u2218 _\u27e8$\u27e9_ (Inverse.from A\u2194B))\n\u03bc-iso-preserve A\u2194B f \u03bcA = \u03bc-view-preserve (_\u27e8$\u27e9_ (Inverse.to A\u2194B)) (_\u27e8$\u27e9_ (Inverse.from A\u2194B))\n (\u2261.sym \u2218 Inverse.left-inverse-of A\u2194B) f \u03bcA\n\nopen import Data.Fin hiding (_+_)\nopen import Data.Vec.NP as Vec renaming (map to vmap; sum to vsum; foldr to vfoldr)\n\nvmsum : \u2200 m {n} \u2192 let open Mon m in\n Vec C n \u2192 C\nvmsum m = vfoldr _ _\u2219_ \u03b5\n where open Monoid m\n\nsearchMonFin : \u2200 n \u2192 SearchMon (Fin n)\nsearchMonFin n m f = vmsum m (vmap f (allFin n)) -- or vsum (tabulate f)\n\nsearchFinSuc : \u2200 n \u2192 Search (Fin (suc n))\nsearchFinSuc n _\u2219_ f = vfoldr _ _\u2219_ (f zero) (vmap (f \u2218 suc) (allFin n))\n\nsumFin : \u2200 n \u2192 Sum (Fin n)\nsumFin n f = vsum (vmap f (allFin n)) -- or vsum (tabulate f)\n\n\u03bcMaybe : \u2200 {A} \u2192 SumProp A \u2192 SumProp (Maybe A)\n\u03bcMaybe \u03bcA = \u03bc-iso (FI.sym Maybe\u2194\u22a4\u228e) (\u03bc\u22a4 +\u03bc \u03bcA)\n\n\u03bcMaybe^ : \u2200 {A} n \u2192 SumProp A \u2192 SumProp (Maybe^ n A)\n\u03bcMaybe^ zero \u03bcA = \u03bcA\n\u03bcMaybe^ (suc n) \u03bcA = \u03bcMaybe (\u03bcMaybe^ n \u03bcA)\n\n\u03bcFin : \u2200 n \u2192 SumProp (Fin (suc n))\n\u03bcFin n = \u03bc-iso (Maybe^\u22a4\u2194Fin1+ n) (\u03bcMaybe^ n \u03bc\u22a4)\n{-\n\u03bcFin n = mk (searchFin n) ext eps hom sumFin-mon sumFin-swap\n module SumFin where\n ext : SearchMonExt (searchFin n)\n ext m f\u2257g = {!map-ext f\u2257g (allFin n)!}\n where open Mon m\n\n eps : Search\u03b5 (searchFin n)\n eps = {!!}\n\n hom : SearchMonHom (searchFin n)\n hom m f g = {!sum-linear f g (allFin n)!}\n\n sumFin-mon : SumMono (sumFin n)\n sumFin-mon f\u2264\u00b0g = sum-mono f\u2264\u00b0g (allFin n)\n\n sumFin-swap : SumSwap (sumFin n)\n sumFin-swap f {sum\u02e3} sum\u02e3-linear = inner (allFin n) where\n inner : \u2200 {m}(xs : Vec (Fin n) m) \u2192 vsum (vmap (sum\u02e3 \u2218 f) xs) \u2261 sum\u02e3 (\u03bb x \u2192 vsum (vmap (flip f x) xs))\n inner [] = \u2261.sym (SumLinear.sum-lin sum\u02e3-linear (const 1337) 0)\n inner (x \u2237 xs) rewrite inner xs = \u2261.sym\n (SumLinear.sum-hom sum\u02e3-linear (f x)\n (\u03bb y \u2192 vsum (vmap (flip f y) xs)))\n-}\n\n\u03bcVec : \u2200 {A} (\u03bcA : SumProp A) n \u2192 SumProp (Vec A n)\n\u03bcVec \u03bcA zero = \u03bc-view (const []) \u03bc\u22a4\n\u03bcVec \u03bcA (suc n) = \u03bc-view (uncurry _\u2237_) (\u03bcA \u00d7\u03bc \u03bcVec \u03bcA n)\n\n{-\nsearchVec : \u2200 {A} n \u2192 Search A \u2192 Search (Vec A n)\nsearchVec zero search\u1d2c m f = f []\nsearchVec (suc n) search\u1d2c m f = search\u1d2c m (\u03bb x \u2192 searchVec n search\u1d2c m (f \u2218 _\u2237_ x))\n\nsumVec : \u2200 {A} n \u2192 Sum A \u2192 Sum (Vec A n)\nsumVec n sum\u1d2c = searchVec n (\u03bb m \u2192 {!sum\u1d2c!}) \u2115+.monoid\n\nsumVec zero sum\u1d2c f = f []\nsumVec (suc n) sum\u1d2c f = (sum\u1d2c \u00d7Sum sumVec n sum\u1d2c) (\u03bb { (x , xs) \u2192 f (x \u2237 xs) })\n\n\u03bcVec' : \u2200 {A} (\u03bcA : SumProp A) n \u2192 SumProp (Vec A n)\n\u03bcVec' {A} \u03bcA n = mk (searchVec n (search \u03bcA)) (mk (sumVec-lin n) (sumVec-hom n)) (sumVec-ext n) (sumVec-mon n) (sumVec-swap n)\n where\n sV = flip sumVec (sum \u03bcA)\n\n sumVec-ext : \u2200 n \u2192 SumExt (sV n)\n sumVec-ext zero f\u2257g = f\u2257g []\n sumVec-ext (suc n) f\u2257g = sum-ext \u03bcA (\u03bb x \u2192 sumVec-ext n (f\u2257g \u2218 _\u2237_ x))\n\n sumVec-lin : \u2200 n \u2192 SumLin (sV n)\n sumVec-lin zero f k = \u2261.refl\n sumVec-lin (suc n) f k rewrite sum-ext \u03bcA (\u03bb x \u2192 sumVec-lin n (f \u2218 _\u2237_ x) k)\n = sum-lin \u03bcA (\u03bb x \u2192 sV n (f \u2218 _\u2237_ x)) k\n\n sumVec-hom : \u2200 n \u2192 SumHom (sV n)\n sumVec-hom zero f g = \u2261.refl\n sumVec-hom (suc n) f g rewrite sum-ext \u03bcA (\u03bb x \u2192 sumVec-hom n (f \u2218 _\u2237_ x) (g \u2218 _\u2237_ x)) \n = sum-hom \u03bcA (\u03bb x \u2192 sV n (f \u2218 _\u2237_ x)) (\u03bb x \u2192 sV n (g \u2218 _\u2237_ x))\n\n sumVec-mon : \u2200 n \u2192 SumMono (sV n)\n sumVec-mon zero f\u2264\u00b0g = f\u2264\u00b0g []\n sumVec-mon (suc n) f\u2264\u00b0g = sum-mono \u03bcA (\u03bb x \u2192 sumVec-mon n (f\u2264\u00b0g \u2218 _\u2237_ x))\n\n sumVec-swap : \u2200 n \u2192 SumSwap (sV n)\n sumVec-swap zero f \u03bc\u02e3-linear = \u2261.refl\n sumVec-swap (suc n) f {sum\u02e3} \u03bc\u02e3-linear rewrite sum-ext \u03bcA (\u03bb x \u2192 sumVec-swap n (f \u2218 _\u2237_ x) \u03bc\u02e3-linear)\n = sum-swap \u03bcA (\u03bb z x \u2192 sumVec n (sum \u03bcA) (\u03bb y \u2192 f (z \u2237 y) x)) \u03bc\u02e3-linear\n-}\n\nsearchVec-++ : \u2200 {A} n {m} (\u03bcA : SumProp A) sg\n \u2192 let open Sgrp sg in\n (f : Vec A (n + m) \u2192 C)\n \u2192 search (\u03bcVec \u03bcA (n + m)) _\u2219_ f\n \u2248 search (\u03bcVec \u03bcA n) _\u2219_ (\u03bb xs \u2192 search (\u03bcVec \u03bcA m) _\u2219_\n (\u03bb ys \u2192 f (xs ++ ys)))\nsearchVec-++ zero \u03bcA sg f = Sgrp.refl sg\nsearchVec-++ (suc n) \u03bcA sg f = search-ext \u03bcA sg (\u03bb x \u2192\n searchVec-++ n \u03bcA sg (f \u2218 _\u2237_ x))\n\nsumVec-swap : \u2200 {A} n {m} (\u03bcA : SumProp A)(f : Vec A (n + m) \u2192 \u2115)\n \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 f (xs ++ ys)))\nsumVec-swap n {m} \u03bcA f = sum-swap (\u03bcVec \u03bcA n) (\u03bcVec \u03bcA m) (\u03bb xs ys \u2192 f (xs ++ ys))\n\nswapS : \u2200 {A B} \u2192 SumProp (A \u00d7 B) \u2192 SumProp (B \u00d7 A)\nswapS = \u03bc-view Data.Product.swap\n -- \u03bc-iso \u00d7-comm\n\nswapS-preserve : \u2200 {A B} f (\u03bcA\u00d7B : SumProp (A \u00d7 B)) \u2192 sum \u03bcA\u00d7B f \u2261 sum (swapS \u03bcA\u00d7B) (f \u2218 Data.Product.swap)\nswapS-preserve f \u03bcA\u00d7B = \u03bc-view-preserve Data.Product.swap Data.Product.swap (\u03bb x \u2192 \u2261.refl) f \u03bcA\u00d7B {- or \u2261.refl -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"697a6af2b714cdc17629bc600252cb44b2721351","subject":"Agda factorized Arg.","message":"Agda factorized Arg.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Program\/ABP\/Lemma2I.agda","new_file":"src\/fot\/FOTC\/Program\/ABP\/Lemma2I.agda","new_contents":"------------------------------------------------------------------------------\n-- ABP lemma 2\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- From Dybjer and Sander's paper: The second lemma states that given\n-- a state of the latter kind (see lemma 1) we will arrive at a new\n-- start state, which is identical to the old start state except that\n-- the bit has alternated and the first item in the input stream has\n-- been removed.\n\nmodule FOTC.Program.ABP.Lemma2I where\n\nopen import Common.FOL.Relation.Binary.EqReasoning\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Base.List.PropertiesI\nopen import FOTC.Base.Loop\nopen import FOTC.Base.PropertiesI\nopen import FOTC.Data.Bool\nopen import FOTC.Data.Bool.PropertiesI\nopen import FOTC.Data.List\nopen import FOTC.Data.List.PropertiesI\nopen import FOTC.Program.ABP.ABP\nopen import FOTC.Program.ABP.Fair\nopen import FOTC.Program.ABP.Fair.PropertiesI\nopen import FOTC.Program.ABP.Terms\n\n------------------------------------------------------------------------------\n-- Helper function for the ABP lemma 2\n\nmodule Helper where\n\n helper : \u2200 {b i' is' os\u2080' os\u2081' as' bs' cs' ds' js'} \u2192\n Bit b \u2192\n Fair os\u2080' \u2192\n ABP' b i' is' os\u2080' os\u2081' as' bs' cs' ds' js' \u2192\n \u2200 ft\u2081 os\u2081'' \u2192 F*T ft\u2081 \u2192 Fair os\u2081'' \u2192 os\u2081' \u2261 ft\u2081 ++ os\u2081'' \u2192\n \u2203[ os\u2080'' ] \u2203[ os\u2081'' ] \u2203[ as'' ] \u2203[ bs'' ] \u2203[ cs'' ] \u2203[ ds'' ]\n Fair os\u2080''\n \u2227 Fair os\u2081''\n \u2227 ABP (not b) is' os\u2080'' os\u2081'' as'' bs'' cs'' ds'' js'\n helper {b} {i'} {is'} {os\u2080'} {os\u2081'} {as'} {bs'} {cs'} {ds'} {js'}\n Bb Fos\u2080' (ds'ABP' , as'ABP , bs'ABP' , cs'ABP' , js'ABP')\n .(T \u2237 []) os\u2081'' f*tnil Fos\u2081'' os\u2081'-eq =\n os\u2080' , os\u2081'' , as'' , bs'' , cs'' , ds''\n , Fos\u2080' , Fos\u2081''\n , as''-eq , bs''-eq , cs''-eq , refl , js'-eq\n\n where\n os'\u2081-eq-helper : os\u2081' \u2261 T \u2237 os\u2081''\n os'\u2081-eq-helper =\n os\u2081' \u2261\u27e8 os\u2081'-eq \u27e9\n (true \u2237 []) ++ os\u2081'' \u2261\u27e8 ++-\u2237 true [] os\u2081'' \u27e9\n true \u2237 [] ++ os\u2081'' \u2261\u27e8 \u2237-rightCong (++-leftIdentity os\u2081'') \u27e9\n true \u2237 os\u2081'' \u220e\n\n ds'' : D\n ds'' = corrupt \u00b7 os\u2081'' \u00b7 cs'\n\n ds'-eq : ds' \u2261 ok b \u2237 ds''\n ds'-eq =\n ds'\n \u2261\u27e8 ds'ABP' \u27e9\n corrupt \u00b7 os\u2081' \u00b7 (b \u2237 cs')\n \u2261\u27e8 \u00b7-leftCong (\u00b7-rightCong os'\u2081-eq-helper) \u27e9\n corrupt \u00b7 (T \u2237 os\u2081'') \u00b7 (b \u2237 cs')\n \u2261\u27e8 corrupt-T os\u2081'' b cs' \u27e9\n ok b \u2237 corrupt \u00b7 os\u2081'' \u00b7 cs'\n \u2261\u27e8 refl \u27e9\n ok b \u2237 ds'' \u220e\n\n as'' : D\n as'' = as'\n\n as''-eq : as'' \u2261 send \u00b7 not b \u00b7 is' \u00b7 ds''\n as''-eq =\n as'' \u2261\u27e8 as'ABP \u27e9\n await b i' is' ds' \u2261\u27e8 cong (await b i' is') ds'-eq \u27e9\n await b i' is' (ok b \u2237 ds'') \u2261\u27e8 await-ok\u2261 b b i' is' ds'' refl \u27e9\n send \u00b7 not b \u00b7 is' \u00b7 ds'' \u220e\n\n bs'' : D\n bs'' = bs'\n\n bs''-eq : bs'' \u2261 corrupt \u00b7 os\u2080' \u00b7 as'\n bs''-eq = bs'ABP'\n\n cs'' : D\n cs'' = cs'\n\n cs''-eq : cs'' \u2261 ack \u00b7 not b \u00b7 bs'\n cs''-eq = cs'ABP'\n\n js'-eq : js' \u2261 out \u00b7 not b \u00b7 bs''\n js'-eq = js'ABP'\n\n helper {b} {i'} {is'} {os\u2080'} {os\u2081'} {as'} {bs'} {cs'} {ds'} {js'}\n Bb Fos\u2080' (ds'ABP' , as'ABP , bs'ABP' , cs'ABP' , js'ABP')\n .(F \u2237 ft\u2081) os\u2081'' (f*tcons {ft\u2081} FTft\u2081) Fos\u2081'' os\u2081'-eq\n = helper Bb (tail-Fair Fos\u2080') ABP'IH ft\u2081 os\u2081'' FTft\u2081 Fos\u2081'' refl\n\n where\n os\u2080\u2075 : D\n os\u2080\u2075 = tail\u2081 os\u2080'\n\n os\u2081\u2075 : D\n os\u2081\u2075 = ft\u2081 ++ os\u2081''\n\n os\u2081'-eq-helper : os\u2081' \u2261 F \u2237 os\u2081\u2075\n os\u2081'-eq-helper = os\u2081' \u2261\u27e8 os\u2081'-eq \u27e9\n (F \u2237 ft\u2081) ++ os\u2081'' \u2261\u27e8 ++-\u2237 _ _ _ \u27e9\n F \u2237 ft\u2081 ++ os\u2081'' \u2261\u27e8 refl \u27e9\n F \u2237 os\u2081\u2075 \u220e\n\n ds\u2075 : D\n ds\u2075 = corrupt \u00b7 os\u2081\u2075 \u00b7 cs'\n\n ds'-eq : ds' \u2261 error \u2237 ds\u2075\n ds'-eq =\n ds'\n \u2261\u27e8 ds'ABP' \u27e9\n corrupt \u00b7 os\u2081' \u00b7 (b \u2237 cs')\n \u2261\u27e8 \u00b7-leftCong (\u00b7-rightCong os\u2081'-eq-helper) \u27e9\n corrupt \u00b7 (F \u2237 os\u2081\u2075) \u00b7 (b \u2237 cs')\n \u2261\u27e8 corrupt-F _ _ _ \u27e9\n error \u2237 corrupt \u00b7 os\u2081\u2075 \u00b7 cs'\n \u2261\u27e8 refl \u27e9\n error \u2237 ds\u2075 \u220e\n\n as\u2075 : D\n as\u2075 = await b i' is' ds\u2075\n\n as'-eq : as' \u2261 < i' , b > \u2237 as\u2075\n as'-eq = as' \u2261\u27e8 as'ABP \u27e9\n await b i' is' ds' \u2261\u27e8 cong (await b i' is') ds'-eq \u27e9\n await b i' is' (error \u2237 ds\u2075) \u2261\u27e8 await-error _ _ _ _ \u27e9\n < i' , b > \u2237 await b i' is' ds\u2075 \u2261\u27e8 refl \u27e9\n < i' , b > \u2237 as\u2075 \u220e\n\n bs\u2075 : D\n bs\u2075 = corrupt \u00b7 os\u2080\u2075 \u00b7 as\u2075\n\n bs'-eq-helper\u2081 : os\u2080' \u2261 T \u2237 tail\u2081 os\u2080' \u2192 bs' \u2261 ok < i' , b > \u2237 bs\u2075\n bs'-eq-helper\u2081 h =\n bs'\n \u2261\u27e8 bs'ABP' \u27e9\n corrupt \u00b7 os\u2080' \u00b7 as'\n \u2261\u27e8 subst\u2082 (\u03bb t\u2081 t\u2082 \u2192 corrupt \u00b7 os\u2080' \u00b7 as' \u2261 corrupt \u00b7 t\u2081 \u00b7 t\u2082)\n h\n as'-eq\n refl\n \u27e9\n corrupt \u00b7 (T \u2237 tail\u2081 os\u2080') \u00b7 (< i' , b > \u2237 as\u2075)\n \u2261\u27e8 corrupt-T _ _ _ \u27e9\n ok < i' , b > \u2237 corrupt \u00b7 (tail\u2081 os\u2080') \u00b7 as\u2075\n \u2261\u27e8 refl \u27e9\n ok < i' , b > \u2237 bs\u2075 \u220e\n\n bs'-eq-helper\u2082 : os\u2080' \u2261 F \u2237 tail\u2081 os\u2080' \u2192 bs' \u2261 error \u2237 bs\u2075\n bs'-eq-helper\u2082 h =\n bs'\n \u2261\u27e8 bs'ABP' \u27e9\n corrupt \u00b7 os\u2080' \u00b7 as'\n \u2261\u27e8 subst\u2082 (\u03bb t\u2081 t\u2082 \u2192 corrupt \u00b7 os\u2080' \u00b7 as' \u2261 corrupt \u00b7 t\u2081 \u00b7 t\u2082)\n h\n as'-eq\n refl\n \u27e9\n corrupt \u00b7 (F \u2237 tail\u2081 os\u2080') \u00b7 (< i' , b > \u2237 as\u2075)\n \u2261\u27e8 corrupt-F _ _ _ \u27e9\n error \u2237 corrupt \u00b7 (tail\u2081 os\u2080') \u00b7 as\u2075\n \u2261\u27e8 refl \u27e9\n error \u2237 bs\u2075 \u220e\n\n bs'-eq : bs' \u2261 ok < i' , b > \u2237 bs\u2075 \u2228 bs' \u2261 error \u2237 bs\u2075\n bs'-eq = case (\u03bb h \u2192 inj\u2081 (bs'-eq-helper\u2081 h))\n (\u03bb h \u2192 inj\u2082 (bs'-eq-helper\u2082 h))\n (head-tail-Fair Fos\u2080')\n\n cs\u2075 : D\n cs\u2075 = ack \u00b7 not b \u00b7 bs\u2075\n\n cs'-eq-helper\u2081 : bs' \u2261 ok < i' , b > \u2237 bs\u2075 \u2192 cs' \u2261 b \u2237 cs\u2075\n cs'-eq-helper\u2081 h =\n cs'\n \u2261\u27e8 cs'ABP' \u27e9\n ack \u00b7 not b \u00b7 bs'\n \u2261\u27e8 \u00b7-rightCong h \u27e9\n ack \u00b7 not b \u00b7 (ok < i' , b > \u2237 bs\u2075)\n \u2261\u27e8 ack-ok\u2262 _ _ _ _ (not-x\u2262x Bb) \u27e9\n not (not b) \u2237 ack \u00b7 not b \u00b7 bs\u2075\n \u2261\u27e8 \u2237-leftCong (not-involutive Bb) \u27e9\n b \u2237 ack \u00b7 not b \u00b7 bs\u2075\n \u2261\u27e8 refl \u27e9\n b \u2237 cs\u2075 \u220e\n\n cs'-eq-helper\u2082 : bs' \u2261 error \u2237 bs\u2075 \u2192 cs' \u2261 b \u2237 cs\u2075\n cs'-eq-helper\u2082 h =\n cs' \u2261\u27e8 cs'ABP' \u27e9\n ack \u00b7 not b \u00b7 bs' \u2261\u27e8 \u00b7-rightCong h \u27e9\n ack \u00b7 not b \u00b7 (error \u2237 bs\u2075) \u2261\u27e8 ack-error _ _ \u27e9\n not (not b) \u2237 ack \u00b7 not b \u00b7 bs\u2075 \u2261\u27e8 \u2237-leftCong (not-involutive Bb) \u27e9\n b \u2237 ack \u00b7 not b \u00b7 bs\u2075 \u2261\u27e8 refl \u27e9\n b \u2237 cs\u2075 \u220e\n\n cs'-eq : cs' \u2261 b \u2237 cs\u2075\n cs'-eq = case cs'-eq-helper\u2081 cs'-eq-helper\u2082 bs'-eq\n\n js'-eq-helper\u2081 : bs' \u2261 ok < i' , b > \u2237 bs\u2075 \u2192 js' \u2261 out \u00b7 not b \u00b7 bs\u2075\n js'-eq-helper\u2081 h =\n js'\n \u2261\u27e8 js'ABP' \u27e9\n out \u00b7 not b \u00b7 bs'\n \u2261\u27e8 \u00b7-rightCong h \u27e9\n out \u00b7 not b \u00b7 (ok < i' , b > \u2237 bs\u2075)\n \u2261\u27e8 out-ok\u2262 (not b) b i' bs\u2075 (not-x\u2262x Bb) \u27e9\n out \u00b7 not b \u00b7 bs\u2075 \u220e\n\n js'-eq-helper\u2082 : bs' \u2261 error \u2237 bs\u2075 \u2192 js' \u2261 out \u00b7 not b \u00b7 bs\u2075\n js'-eq-helper\u2082 h =\n js' \u2261\u27e8 js'ABP' \u27e9\n out \u00b7 not b \u00b7 bs' \u2261\u27e8 \u00b7-rightCong h \u27e9\n out \u00b7 not b \u00b7 (error \u2237 bs\u2075) \u2261\u27e8 out-error (not b) bs\u2075 \u27e9\n out \u00b7 not b \u00b7 bs\u2075 \u220e\n\n js'-eq : js' \u2261 out \u00b7 not b \u00b7 bs\u2075\n js'-eq = case js'-eq-helper\u2081 js'-eq-helper\u2082 bs'-eq\n\n ds\u2075-eq : ds\u2075 \u2261 corrupt \u00b7 os\u2081\u2075 \u00b7 (b \u2237 cs\u2075)\n ds\u2075-eq = \u00b7-rightCong cs'-eq\n\n ABP'IH : ABP' b i' is' os\u2080\u2075 os\u2081\u2075 as\u2075 bs\u2075 cs\u2075 ds\u2075 js'\n ABP'IH = ds\u2075-eq , refl , refl , refl , js'-eq\n\n------------------------------------------------------------------------------\n-- From Dybjer and Sander's paper: From the assumption that os\u2081 \u2208\n-- Fair, and hence by unfolding Fair we conclude that there are ft\u2081 :\n-- F*T and os\u2081'' : Fair, such that os\u2081' = ft\u2081 ++ os\u2081''.\n--\n-- We proceed by induction on ft\u2081 : F*T using helper.\n\nopen Helper\nlemma\u2082 : \u2200 {b i' is' os\u2080' os\u2081' as' bs' cs' ds' js'} \u2192\n Bit b \u2192\n Fair os\u2080' \u2192\n Fair os\u2081' \u2192\n ABP' b i' is' os\u2080' os\u2081' as' bs' cs' ds' js' \u2192\n \u2203[ os\u2080'' ] \u2203[ os\u2081'' ] \u2203[ as'' ] \u2203[ bs'' ] \u2203[ cs'' ] \u2203[ ds'' ]\n Fair os\u2080''\n \u2227 Fair os\u2081''\n \u2227 ABP (not b) is' os\u2080'' os\u2081'' as'' bs'' cs'' ds'' js'\nlemma\u2082 Bb Fos\u2080' Fos\u2081' abp' with Fair-unf Fos\u2081'\n... | ft , os\u2080'' , FTft , Fos\u2080'' , h =\n helper Bb Fos\u2080' abp' ft os\u2080'' FTft Fos\u2080'' h\n","old_contents":"------------------------------------------------------------------------------\n-- ABP lemma 2\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- From Dybjer and Sander's paper: The second lemma states that given\n-- a state of the latter kind (see lemma 1) we will arrive at a new\n-- start state, which is identical to the old start state except that\n-- the bit has alternated and the first item in the input stream has\n-- been removed.\n\nmodule FOTC.Program.ABP.Lemma2I where\n\nopen import Common.FOL.Relation.Binary.EqReasoning\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Base.List.PropertiesI\nopen import FOTC.Base.Loop\nopen import FOTC.Base.PropertiesI\nopen import FOTC.Data.Bool\nopen import FOTC.Data.Bool.PropertiesI\nopen import FOTC.Data.List\nopen import FOTC.Data.List.PropertiesI\nopen import FOTC.Program.ABP.ABP\nopen import FOTC.Program.ABP.Fair\nopen import FOTC.Program.ABP.Fair.PropertiesI\nopen import FOTC.Program.ABP.Terms\n\n------------------------------------------------------------------------------\n-- Helper function for the ABP lemma 2\n\nmodule Helper where\n\n helper : \u2200 {b i' is' os\u2080' os\u2081' as' bs' cs' ds' js'} \u2192\n Bit b \u2192\n Fair os\u2080' \u2192\n ABP' b i' is' os\u2080' os\u2081' as' bs' cs' ds' js' \u2192\n \u2200 ft\u2081 os\u2081'' \u2192 F*T ft\u2081 \u2192 Fair os\u2081'' \u2192 os\u2081' \u2261 ft\u2081 ++ os\u2081'' \u2192\n \u2203[ os\u2080'' ] \u2203[ os\u2081'' ] \u2203[ as'' ] \u2203[ bs'' ] \u2203[ cs'' ] \u2203[ ds'' ]\n Fair os\u2080''\n \u2227 Fair os\u2081''\n \u2227 ABP (not b) is' os\u2080'' os\u2081'' as'' bs'' cs'' ds'' js'\n helper {b} {i'} {is'} {os\u2080'} {os\u2081'} {as'} {bs'} {cs'} {ds'} {js'}\n Bb Fos\u2080' (ds'ABP' , as'ABP , bs'ABP' , cs'ABP' , js'ABP')\n .(T \u2237 []) os\u2081'' f*tnil Fos\u2081'' os\u2081'-eq =\n os\u2080' , os\u2081'' , as'' , bs'' , cs'' , ds''\n , Fos\u2080' , Fos\u2081''\n , as''-eq , bs''-eq , cs''-eq , refl , js'-eq\n\n where\n os'\u2081-eq-helper : os\u2081' \u2261 T \u2237 os\u2081''\n os'\u2081-eq-helper =\n os\u2081' \u2261\u27e8 os\u2081'-eq \u27e9\n (true \u2237 []) ++ os\u2081'' \u2261\u27e8 ++-\u2237 true [] os\u2081'' \u27e9\n true \u2237 [] ++ os\u2081'' \u2261\u27e8 \u2237-rightCong (++-leftIdentity os\u2081'') \u27e9\n true \u2237 os\u2081'' \u220e\n\n ds'' : D\n ds'' = corrupt \u00b7 os\u2081'' \u00b7 cs'\n\n ds'-eq : ds' \u2261 ok b \u2237 ds''\n ds'-eq =\n ds'\n \u2261\u27e8 ds'ABP' \u27e9\n corrupt \u00b7 os\u2081' \u00b7 (b \u2237 cs')\n \u2261\u27e8 \u00b7-leftCong (\u00b7-rightCong os'\u2081-eq-helper) \u27e9\n corrupt \u00b7 (T \u2237 os\u2081'') \u00b7 (b \u2237 cs')\n \u2261\u27e8 corrupt-T os\u2081'' b cs' \u27e9\n ok b \u2237 corrupt \u00b7 os\u2081'' \u00b7 cs'\n \u2261\u27e8 refl \u27e9\n ok b \u2237 ds'' \u220e\n\n as'' : D\n as'' = as'\n\n as''-eq : as'' \u2261 send \u00b7 not b \u00b7 is' \u00b7 ds''\n as''-eq =\n as'' \u2261\u27e8 as'ABP \u27e9\n await b i' is' ds' \u2261\u27e8 cong (await b i' is') ds'-eq \u27e9\n await b i' is' (ok b \u2237 ds'') \u2261\u27e8 await-ok\u2261 b b i' is' ds'' refl \u27e9\n send \u00b7 not b \u00b7 is' \u00b7 ds'' \u220e\n\n bs'' : D\n bs'' = bs'\n\n bs''-eq : bs'' \u2261 corrupt \u00b7 os\u2080' \u00b7 as'\n bs''-eq = bs'ABP'\n\n cs'' : D\n cs'' = cs'\n\n cs''-eq : cs'' \u2261 ack \u00b7 not b \u00b7 bs'\n cs''-eq = cs'ABP'\n\n js'-eq : js' \u2261 out \u00b7 not b \u00b7 bs''\n js'-eq = js'ABP'\n\n helper {b} {i'} {is'} {os\u2080'} {os\u2081'} {as'} {bs'} {cs'} {ds'} {js'}\n Bb Fos\u2080' (ds'ABP' , as'ABP , bs'ABP' , cs'ABP' , js'ABP')\n .(F \u2237 ft\u2081) os\u2081'' (f*tcons {ft\u2081} FTft\u2081) Fos\u2081'' os\u2081'-eq\n = helper Bb (tail-Fair Fos\u2080') ABP'IH ft\u2081 os\u2081'' FTft\u2081 Fos\u2081'' refl\n\n where\n os\u2080\u2075 : D\n os\u2080\u2075 = tail\u2081 os\u2080'\n\n os\u2081\u2075 : D\n os\u2081\u2075 = ft\u2081 ++ os\u2081''\n\n os\u2081'-eq-helper : os\u2081' \u2261 F \u2237 os\u2081\u2075\n os\u2081'-eq-helper = os\u2081' \u2261\u27e8 os\u2081'-eq \u27e9\n (F \u2237 ft\u2081) ++ os\u2081'' \u2261\u27e8 ++-\u2237 _ _ _ \u27e9\n F \u2237 ft\u2081 ++ os\u2081'' \u2261\u27e8 refl \u27e9\n F \u2237 os\u2081\u2075 \u220e\n\n ds\u2075 : D\n ds\u2075 = corrupt \u00b7 os\u2081\u2075 \u00b7 cs'\n\n ds'-eq : ds' \u2261 error \u2237 ds\u2075\n ds'-eq =\n ds'\n \u2261\u27e8 ds'ABP' \u27e9\n corrupt \u00b7 os\u2081' \u00b7 (b \u2237 cs')\n \u2261\u27e8 \u00b7-leftCong (\u00b7-rightCong os\u2081'-eq-helper) \u27e9\n corrupt \u00b7 (F \u2237 os\u2081\u2075) \u00b7 (b \u2237 cs')\n \u2261\u27e8 corrupt-F _ _ _ \u27e9\n error \u2237 corrupt \u00b7 os\u2081\u2075 \u00b7 cs'\n \u2261\u27e8 refl \u27e9\n error \u2237 ds\u2075 \u220e\n\n as\u2075 : D\n as\u2075 = await b i' is' ds\u2075\n\n as'-eq : as' \u2261 < i' , b > \u2237 as\u2075\n as'-eq = as' \u2261\u27e8 as'ABP \u27e9\n await b i' is' ds' \u2261\u27e8 cong (await b i' is') ds'-eq \u27e9\n await b i' is' (error \u2237 ds\u2075) \u2261\u27e8 await-error _ _ _ _ \u27e9\n < i' , b > \u2237 await b i' is' ds\u2075 \u2261\u27e8 refl \u27e9\n < i' , b > \u2237 as\u2075 \u220e\n\n bs\u2075 : D\n bs\u2075 = corrupt \u00b7 os\u2080\u2075 \u00b7 as\u2075\n\n bs'-eq-helper\u2081 : os\u2080' \u2261 T \u2237 tail\u2081 os\u2080' \u2192 bs' \u2261 ok < i' , b > \u2237 bs\u2075\n bs'-eq-helper\u2081 h =\n bs'\n \u2261\u27e8 bs'ABP' \u27e9\n corrupt \u00b7 os\u2080' \u00b7 as'\n \u2261\u27e8 subst\u2082 (\u03bb t\u2081 t\u2082 \u2192 corrupt \u00b7 os\u2080' \u00b7 as' \u2261 corrupt \u00b7 t\u2081 \u00b7 t\u2082)\n h\n as'-eq\n refl\n \u27e9\n corrupt \u00b7 (T \u2237 tail\u2081 os\u2080') \u00b7 (< i' , b > \u2237 as\u2075)\n \u2261\u27e8 corrupt-T _ _ _ \u27e9\n ok < i' , b > \u2237 corrupt \u00b7 (tail\u2081 os\u2080') \u00b7 as\u2075\n \u2261\u27e8 refl \u27e9\n ok < i' , b > \u2237 bs\u2075 \u220e\n\n bs'-eq-helper\u2082 : os\u2080' \u2261 F \u2237 tail\u2081 os\u2080' \u2192 bs' \u2261 error \u2237 bs\u2075\n bs'-eq-helper\u2082 h =\n bs'\n \u2261\u27e8 bs'ABP' \u27e9\n corrupt \u00b7 os\u2080' \u00b7 as'\n \u2261\u27e8 subst\u2082 (\u03bb t\u2081 t\u2082 \u2192 corrupt \u00b7 os\u2080' \u00b7 as' \u2261 corrupt \u00b7 t\u2081 \u00b7 t\u2082)\n h\n as'-eq\n refl\n \u27e9\n corrupt \u00b7 (F \u2237 tail\u2081 os\u2080') \u00b7 (< i' , b > \u2237 as\u2075)\n \u2261\u27e8 corrupt-F _ _ _ \u27e9\n error \u2237 corrupt \u00b7 (tail\u2081 os\u2080') \u00b7 as\u2075\n \u2261\u27e8 refl \u27e9\n error \u2237 bs\u2075 \u220e\n\n bs'-eq : bs' \u2261 ok < i' , b > \u2237 bs\u2075 \u2228 bs' \u2261 error \u2237 bs\u2075\n bs'-eq = case (\u03bb h \u2192 inj\u2081 (bs'-eq-helper\u2081 h))\n (\u03bb h \u2192 inj\u2082 (bs'-eq-helper\u2082 h))\n (head-tail-Fair Fos\u2080')\n\n cs\u2075 : D\n cs\u2075 = ack \u00b7 not b \u00b7 bs\u2075\n\n cs'-eq-helper\u2081 : bs' \u2261 ok < i' , b > \u2237 bs\u2075 \u2192 cs' \u2261 b \u2237 cs\u2075\n cs'-eq-helper\u2081 h =\n cs'\n \u2261\u27e8 cs'ABP' \u27e9\n ack \u00b7 not b \u00b7 bs'\n \u2261\u27e8 \u00b7-rightCong h \u27e9\n ack \u00b7 not b \u00b7 (ok < i' , b > \u2237 bs\u2075)\n \u2261\u27e8 ack-ok\u2262 _ _ _ _ (not-x\u2262x Bb) \u27e9\n not (not b) \u2237 ack \u00b7 not b \u00b7 bs\u2075\n \u2261\u27e8 \u2237-leftCong (not-involutive Bb) \u27e9\n b \u2237 ack \u00b7 not b \u00b7 bs\u2075\n \u2261\u27e8 refl \u27e9\n b \u2237 cs\u2075 \u220e\n\n cs'-eq-helper\u2082 : bs' \u2261 error \u2237 bs\u2075 \u2192 cs' \u2261 b \u2237 cs\u2075\n cs'-eq-helper\u2082 h =\n cs' \u2261\u27e8 cs'ABP' \u27e9\n ack \u00b7 not b \u00b7 bs' \u2261\u27e8 \u00b7-rightCong h \u27e9\n ack \u00b7 not b \u00b7 (error \u2237 bs\u2075) \u2261\u27e8 ack-error _ _ \u27e9\n not (not b) \u2237 ack \u00b7 not b \u00b7 bs\u2075 \u2261\u27e8 \u2237-leftCong (not-involutive Bb) \u27e9\n b \u2237 ack \u00b7 not b \u00b7 bs\u2075 \u2261\u27e8 refl \u27e9\n b \u2237 cs\u2075 \u220e\n\n cs'-eq : cs' \u2261 b \u2237 cs\u2075\n cs'-eq = case cs'-eq-helper\u2081 cs'-eq-helper\u2082 bs'-eq\n\n js'-eq-helper\u2081 : bs' \u2261 ok < i' , b > \u2237 bs\u2075 \u2192 js' \u2261 out \u00b7 not b \u00b7 bs\u2075\n js'-eq-helper\u2081 h =\n js'\n \u2261\u27e8 js'ABP' \u27e9\n out \u00b7 not b \u00b7 bs'\n \u2261\u27e8 \u00b7-rightCong h \u27e9\n out \u00b7 not b \u00b7 (ok < i' , b > \u2237 bs\u2075)\n \u2261\u27e8 out-ok\u2262 (not b) b i' bs\u2075 (not-x\u2262x Bb) \u27e9\n out \u00b7 not b \u00b7 bs\u2075 \u220e\n\n js'-eq-helper\u2082 : bs' \u2261 error \u2237 bs\u2075 \u2192 js' \u2261 out \u00b7 not b \u00b7 bs\u2075\n js'-eq-helper\u2082 h =\n js' \u2261\u27e8 js'ABP' \u27e9\n out \u00b7 not b \u00b7 bs' \u2261\u27e8 \u00b7-rightCong h \u27e9\n out \u00b7 not b \u00b7 (error \u2237 bs\u2075) \u2261\u27e8 out-error (not b) bs\u2075 \u27e9\n out \u00b7 not b \u00b7 bs\u2075 \u220e\n\n js'-eq : js' \u2261 out \u00b7 not b \u00b7 bs\u2075\n js'-eq = case js'-eq-helper\u2081 js'-eq-helper\u2082 bs'-eq\n\n ds\u2075-eq : ds\u2075 \u2261 corrupt \u00b7 os\u2081\u2075 \u00b7 (b \u2237 cs\u2075)\n ds\u2075-eq = \u00b7-rightCong cs'-eq\n\n ABP'IH : ABP' b i' is' os\u2080\u2075 os\u2081\u2075 as\u2075 bs\u2075 cs\u2075 ds\u2075 js'\n ABP'IH = ds\u2075-eq , refl , refl , refl , js'-eq\n\n------------------------------------------------------------------------------\n-- From Dybjer and Sander's paper: From the assumption that os\u2081 \u2208\n-- Fair, and hence by unfolding Fair we conclude that there are ft\u2081 :\n-- F*T and os\u2081'' : Fair, such that os\u2081' = ft\u2081 ++ os\u2081''.\n--\n-- We proceed by induction on ft\u2081 : F*T using helper.\n\nopen Helper\nlemma\u2082 : \u2200 {b i' is' os\u2080' os\u2081' as' bs' cs' ds' js'} \u2192\n Bit b \u2192\n Fair os\u2080' \u2192\n Fair os\u2081' \u2192\n ABP' b i' is' os\u2080' os\u2081' as' bs' cs' ds' js' \u2192\n \u2203[ os\u2080'' ] \u2203[ os\u2081'' ] \u2203[ as'' ] \u2203[ bs'' ] \u2203[ cs'' ] \u2203[ ds'' ]\n Fair os\u2080''\n \u2227 Fair os\u2081''\n \u2227 ABP (not b) is' os\u2080'' os\u2081'' as'' bs'' cs'' ds'' js'\nlemma\u2082 Bb Fos\u2080' Fos\u2081' abp' with Fair-unf Fos\u2081'\n... | ft , os\u2080'' , FTft , Fos\u2080'' , h =\n helper Bb Fos\u2080' abp' ft os\u2080'' FTft Fos\u2080'' h\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b099c4796003a90ed3a5269d9137f3a8e04c3d42","subject":"pisearch: renaming P* to {To,From}Fun","message":"pisearch: renaming P* to {To,From}Fun\n","repos":"crypto-agda\/crypto-agda","old_file":"pisearch.agda","new_file":"pisearch.agda","new_contents":"{-# OPTIONS --type-in-type #-}\nmodule pisearch where\nopen import Type hiding (\u2605_)\nopen import Function\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Bool.NP\nopen import Search.Type\nopen import Search.Searchable.Product\nopen import Search.Searchable\nopen import sum\n\nTree : \u2200 {A} \u2192 Search A \u2192 (A \u2192 \u2605) \u2192 \u2605\nTree sA B = sA _\u00d7_ B\n\n\u03a0 : (A : \u2605) \u2192 (B : A \u2192 \u2605) \u2192 \u2605\n\u03a0 A B = (x : A) \u2192 B x\n\nToFun : \u2200 {A} (sA : Search A) \u2192 \u2605\nToFun {A} sA = \u2200 {B} \u2192 Tree sA B \u2192 \u03a0 A B\n\nFromFun : \u2200 {A} (sA : Search A) \u2192 \u2605\nFromFun {A} sA = \u2200 {B} \u2192 \u03a0 A B \u2192 Tree sA B\n\ntoFun-\u03a3 : \u2200 {A} {B : A \u2192 \u2605} (sA : Search A) (sB : \u2200 {x} \u2192 Search (B x))\n \u2192 ToFun sA\n \u2192 (\u2200 {x} \u2192 ToFun (sB {x}))\n \u2192 ToFun (\u03a3Search sA (\u03bb {x} \u2192 sB {x}))\ntoFun-\u03a3 _ _ PA PB t = uncurry (PB \u2218 PA t)\n\nfromFun-\u03a3 : \u2200 {A} {B : A \u2192 \u2605} (sA : Search A) (sB : \u2200 {x} \u2192 Search (B x))\n \u2192 FromFun sA\n \u2192 (\u2200 {x} \u2192 FromFun (sB {x}))\n \u2192 FromFun (\u03a3Search sA (\u03bb {x} \u2192 sB {x}))\nfromFun-\u03a3 _ _ PA PB f = PA (PB \u2218 curry f)\n\ntoFun-\u00d7 : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 ToFun sA \u2192 ToFun sB \u2192 ToFun (sA \u00d7Search sB)\ntoFun-\u00d7 sA sB PA PB = toFun-\u03a3 sA sB PA PB\n\nfromFun-\u00d7 : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 FromFun sA \u2192 FromFun sB \u2192 FromFun (sA \u00d7Search sB)\nfromFun-\u00d7 sA sB PA PB = fromFun-\u03a3 sA sB PA PB\n\ntoFun-Bit : ToFun (search \u03bcBit)\ntoFun-Bit (f , t) false = f\ntoFun-Bit (f , t) true = t\n\nfromFun-Bit : FromFun (search \u03bcBit)\nfromFun-Bit f = f false , f true\n\ntoFun-\u228e : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 ToFun sA \u2192 ToFun sB \u2192 ToFun (sA +Search sB)\ntoFun-\u228e sA sB PA PB (t , u) (inj\u2081 x) = PA t x\ntoFun-\u228e sA sB PA PB (t , u) (inj\u2082 x) = PB u x\n\nfromFun-\u228e : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 FromFun sA \u2192 FromFun sB \u2192 FromFun (sA +Search sB)\nfromFun-\u228e sA sB PA PB f = PA (f \u2218 inj\u2081) , PB (f \u2218 inj\u2082)\n\n-- toFun-searchInd : \u2200 {A} {sA : Search A} \u2192 SearchInd sA \u2192 ToFun sA\n-- toFun-searchInd {A} {sA} indA {B} t = ?\n\nfromFun-searchInd : \u2200 {A} {sA : Search A} \u2192 SearchInd sA \u2192 FromFun sA\nfromFun-searchInd indA = indA (\u03bb s \u2192 Tree s _) _,_\n","old_contents":"{-# OPTIONS --type-in-type #-}\nmodule pisearch where\nopen import Type hiding (\u2605_)\nopen import Function\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Bool.NP\nopen import Search.Type\nopen import Search.Searchable.Product\nopen import Search.Searchable\nopen import sum\n\n\u03a0' : \u2200 {A} \u2192 Search A \u2192 (A \u2192 \u2605) \u2192 \u2605\n\u03a0' sA B = sA _\u00d7_ B\n\n\u03a0 : (A : \u2605\u2080) \u2192 (B : A \u2192 \u2605\u2080) \u2192 \u2605\u2080\n\u03a0 A B = (x : A) \u2192 B x\n\nP\u2192 : \u2200 {A} (sA : Search A) \u2192 \u2605\u2080\nP\u2192 {A} sA = \u2200 {B} \u2192 \u03a0' sA B \u2192 \u03a0 A B\n\nP\u2190 : \u2200 {A} (sA : Search A) \u2192 \u2605\u2080\nP\u2190 {A} sA = \u2200 {B} \u2192 \u03a0 A B \u2192 \u03a0' sA B\n\nP\u2192\u03a3 : \u2200 {A} {B : A \u2192 \u2605} (sA : Search A) (sB : \u2200 {x} \u2192 Search (B x))\n \u2192 P\u2192 sA\n \u2192 (\u2200 {x} \u2192 P\u2192 (sB {x}))\n \u2192 P\u2192 (\u03a3Search sA (\u03bb {x} \u2192 sB {x}))\nP\u2192\u03a3 _ _ PA PB t = uncurry (PB \u2218 PA t)\n\nP\u2190\u03a3 : \u2200 {A} {B : A \u2192 \u2605} (sA : Search A) (sB : \u2200 {x} \u2192 Search (B x))\n \u2192 P\u2190 sA\n \u2192 (\u2200 {x} \u2192 P\u2190 (sB {x}))\n \u2192 P\u2190 (\u03a3Search sA (\u03bb {x} \u2192 sB {x}))\nP\u2190\u03a3 _ _ PA PB f = PA (PB \u2218 curry f)\n\nP\u2192\u00d7 : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 P\u2192 sA \u2192 P\u2192 sB \u2192 P\u2192 (sA \u00d7Search sB)\nP\u2192\u00d7 sA sB PA PB = P\u2192\u03a3 sA sB PA PB\n\nP\u2190\u00d7 : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 P\u2190 sA \u2192 P\u2190 sB \u2192 P\u2190 (sA \u00d7Search sB)\nP\u2190\u00d7 sA sB PA PB = P\u2190\u03a3 sA sB PA PB\n\nP\u2192Bit : P\u2192 (search \u03bcBit)\nP\u2192Bit (f , t) false = f\nP\u2192Bit (f , t) true = t\n\nP\u2190Bit : P\u2190 (search \u03bcBit)\nP\u2190Bit f = f false , f true\n\nP\u2192\u228e : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 P\u2192 sA \u2192 P\u2192 sB \u2192 P\u2192 (sA +Search sB)\nP\u2192\u228e sA sB PA PB (t , u) (inj\u2081 x) = PA t x\nP\u2192\u228e sA sB PA PB (t , u) (inj\u2082 x) = PB u x\n\nP\u2190\u228e : \u2200 {A B} (sA : Search A) (sB : Search B) \u2192 P\u2190 sA \u2192 P\u2190 sB \u2192 P\u2190 (sA +Search sB)\nP\u2190\u228e sA sB PA PB f = PA (f \u2218 inj\u2081) , PB (f \u2218 inj\u2082)\n\n-- P\u2192Ind : \u2200 {A} {sA : Search A} \u2192 SearchInd sA \u2192 P\u2192 sA\n-- P\u2192Ind {A} {sA} indA {B} t = ?\n\nP\u2190Ind : \u2200 {A} {sA : Search A} \u2192 SearchInd sA \u2192 P\u2190 sA\nP\u2190Ind indA = indA (\u03bb s \u2192 \u03a0' s _) _,_\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"4ce8f99bf6219694ddca34b04eec4ee51fddd10e","subject":"ECC (generic modular exp actually)","message":"ECC (generic modular exp actually)\n","repos":"crypto-agda\/crypto-agda","old_file":"ECC\/ecc.agda","new_file":"ECC\/ecc.agda","new_contents":"--open import prelude renaming (Bool to \ud835\udfda; true to 1\u2082; false to 0\u2082)\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Data.Two.Base\nopen import Data.List\nopen import Function\nopen import Algebra.FunctionProperties.Eq\nopen Implicits\nopen import Algebra.Raw\nopen import Algebra.Field\nopen import Algebra.Monoid\nopen import Algebra.Monoid.Commutative\nopen import Algebra.Group\n\nmodule ecc\n (\u2119 : Set)\n (\u2119-monoid : Commutative-Monoid \u2119)\n (Number : Set)\n (+-comm-mon : Commutative-Monoid Number)\n (*-mon : Monoid Number)\n (open Additive-Commutative-Monoid +-comm-mon)\n (open Multiplicative-Monoid *-mon)\n (+-*-distr : \u2200 {x y z} \u2192 (x + y) * z \u2261 x * z + y * z)\n (*-+-distr : \u2200 {x y z} \u2192 x * (y + z) \u2261 x * y + x * z)\n (0*-zero : \u2200 {x} \u2192 0# * x \u2261 0#)\n (*0-zero : \u2200 {x} \u2192 x * 0# \u2261 0#)\n --(modinv-*-distr : \u2200 {x y} \u2192 modinv (x * y) \u2261 modinv x * modinv y)\n --(modinv-modinv : \u2200 {x} \u2192 modinv (modinv x) \u2261 x)\n --(*-assoc : \u2200 {x y z} \u2192 (x * y) * z \u2261 x * (y * z))\n --(*-comm : \u2200 {x y} \u2192 x * y \u2261 y * x)\n --(modinv-cancel : \u2200 {x y} \u2192 x * modinv x * y \u2261 y)\n --(2*1\u2099 : 2* 1# \u2261 2\u2099)\n --(2*-spec : \u2200 {n} \u2192 2* n \u2261 2\u2099 * n)\n where\n\nmodule \u2295 = Commutative-Monoid \u2119-monoid\nopen \u2295\n renaming\n ( _\u2219_ to _\u2295_\n ; \u2219= to \u2295=\n ; \u03b5\u2219-identity to \u03b5\u2295-identity\n ; \u2219\u03b5-identity to \u2295\u03b5-identity\n )\n\n2\u00b7_ : \u2119 \u2192 \u2119\n2\u00b7 P = P \u2295 P\n\n2\u00b7-\u2295-distr : \u2200 {P Q} \u2192 2\u00b7 (P \u2295 Q) \u2261 2\u00b7 P \u2295 2\u00b7 Q\n2\u00b7-\u2295-distr = \u2295.interchange\n\n2\u00b7-\u2295 : \u2200 {P Q R} \u2192 2\u00b7 P \u2295 (Q \u2295 R) \u2261 (P \u2295 Q) \u2295 (P \u2295 R)\n2\u00b7-\u2295 = \u2295.interchange\n\n{-\nec-multiply-bin : List \ud835\udfda \u2192 \u2119 \u2192 \u2119\nec-multiply-bin scalar P = go scalar\n where\n go : List \ud835\udfda \u2192 \u2119\n go [] = P\n go (b \u2237 bs) = [0: x\u2080 1: x\u2081 ] b\n where x\u2080 = 2\u00b7 go bs\n x\u2081 = P \u2295 x\u2080\n\nec-multiply : Number \u2192 \u2119 \u2192 \u2119\nec-multiply scalar P =\n -- if scalar == 0 or scalar >= N: raise Exception(\"Invalid Scalar\/Private Key\")\n ec-multiply-bin (bin scalar) P\n\n_\u00b7_ = ec-multiply\ninfixr 8 _\u00b7_\n-}\n\n\nopen From-Op\u2082.From-Assoc-Comm _+_ +-assoc +-comm\n renaming ( on-sides to +-on-sides)\n\ninfixl 6 1+_\ninfixl 7 2*_ 1+2*_\n1+_ = \u03bb x \u2192 1# + x\n2*_ = \u03bb x \u2192 x + x\n1+2*_ = \u03bb x \u2192 1+ 2* x\n\ndata Parity-View : Number \u2192 Set where\n zero\u27e8_\u27e9 : \u2200 {n} \u2192 n \u2261 0# \u2192 Parity-View n\n even_by\u27e8_\u27e9 : \u2200 {m n} \u2192 Parity-View m \u2192 n \u2261 2* m \u2192 Parity-View n\n odd_by\u27e8_\u27e9 : \u2200 {m n} \u2192 Parity-View m \u2192 n \u2261 1+ 2* m \u2192 Parity-View n\n\ncast_by\u27e8_\u27e9 : \u2200 {x y} \u2192 Parity-View x \u2192 y \u2261 x \u2192 Parity-View y\ncast zero\u27e8 x\u2091 \u27e9 by\u27e8 y\u2091 \u27e9 = zero\u27e8 y\u2091 \u2219 x\u2091 \u27e9\ncast even x\u209a by\u27e8 x\u2091 \u27e9 by\u27e8 y\u2091 \u27e9 = even x\u209a by\u27e8 y\u2091 \u2219 x\u2091 \u27e9\ncast odd x\u209a by\u27e8 x\u2091 \u27e9 by\u27e8 y\u2091 \u27e9 = odd x\u209a by\u27e8 y\u2091 \u2219 x\u2091 \u27e9\n\ninfixr 8 _\u00b7\u209a_\n_\u00b7\u209a_ : \u2200 {n} (p : Parity-View n) \u2192 \u2119 \u2192 \u2119\nzero\u27e8 e \u27e9 \u00b7\u209a P = \u03b5\neven p by\u27e8 e \u27e9 \u00b7\u209a P = 2\u00b7 (p \u00b7\u209a P)\nodd p by\u27e8 e \u27e9 \u00b7\u209a P = P \u2295 (2\u00b7 (p \u00b7\u209a P))\n\n_+2*_ : \ud835\udfda \u2192 Number \u2192 Number\n0\u2082 +2* m = 2* m\n1\u2082 +2* m = 1+2* m\n\n{-\npostulate\n bin-2* : \u2200 {n} \u2192 bin (2* n) \u2261 0\u2082 \u2237 bin n\n bin-1+2* : \u2200 {n} \u2192 bin (1+2* n) \u2261 1\u2082 \u2237 bin n\n\nbin-+2* : (b : \ud835\udfda)(n : Number) \u2192 bin (b +2* n) \u2261 b \u2237 bin n\nbin-+2* 1\u2082 n = bin-1+2*\nbin-+2* 0\u2082 n = bin-2*\n-}\n\n-- (msb) most significant bit first\nbin\u209a : \u2200 {n} \u2192 Parity-View n \u2192 List \ud835\udfda\nbin\u209a zero\u27e8 e \u27e9 = []\nbin\u209a even p by\u27e8 e \u27e9 = 0\u2082 \u2237 bin\u209a p\nbin\u209a odd p by\u27e8 e \u27e9 = 1\u2082 \u2237 bin\u209a p\n\nhalf : \u2200 {n} \u2192 Parity-View n \u2192 Number\nhalf zero\u27e8 _ \u27e9 = 0#\nhalf (even_by\u27e8_\u27e9 {m} _ _) = m\nhalf (odd_by\u27e8_\u27e9 {m} _ _) = m\n\n{-\nbin-parity : \u2200 {n} (p : ParityView n) \u2192 bin n \u2261 parity p \u2237 bin (half p)\nbin-parity (even n) = bin-2*\nbin-parity (odd n) = bin-1+2*\n-}\n\ninfixl 6 1+\u209a_ _+\u209a_\n1+\u209a_ : \u2200 {x} \u2192 Parity-View x \u2192 Parity-View (1+ x)\n1+\u209a zero\u27e8 e \u27e9 = odd zero\u27e8 refl \u27e9 by\u27e8 ap 1+_ (e \u2219 ! 0+-identity) \u27e9\n1+\u209a even p by\u27e8 e \u27e9 = odd p by\u27e8 ap 1+_ e \u27e9\n1+\u209a odd p by\u27e8 e \u27e9 = even 1+\u209a p by\u27e8 ap 1+_ e \u2219 ! +-assoc \u2219 +-interchange \u27e9\n\n_+\u209a_ : \u2200 {x y} \u2192 Parity-View x \u2192 Parity-View y \u2192 Parity-View (x + y)\nzero\u27e8 x\u2091 \u27e9 +\u209a y\u209a = cast y\u209a by\u27e8 ap (\u03bb z \u2192 z + _) x\u2091 \u2219 0+-identity \u27e9\nx\u209a +\u209a zero\u27e8 y\u2091 \u27e9 = cast x\u209a by\u27e8 ap (_+_ _) y\u2091 \u2219 +-comm \u2219 0+-identity \u27e9\neven x\u209a by\u27e8 x\u2091 \u27e9 +\u209a even y\u209a by\u27e8 y\u2091 \u27e9 = even x\u209a +\u209a y\u209a by\u27e8 += x\u2091 y\u2091 \u2219 +-interchange \u27e9\neven x\u209a by\u27e8 x\u2091 \u27e9 +\u209a odd y\u209a by\u27e8 y\u2091 \u27e9 = odd x\u209a +\u209a y\u209a by\u27e8 += x\u2091 y\u2091 \u2219 +-comm \u2219 +-assoc \u2219 ap 1+_ (+-comm \u2219 +-interchange) \u27e9\nodd x\u209a by\u27e8 x\u2091 \u27e9 +\u209a even y\u209a by\u27e8 y\u2091 \u27e9 = odd x\u209a +\u209a y\u209a by\u27e8 += x\u2091 y\u2091 \u2219 +-assoc \u2219 ap 1+_ +-interchange \u27e9\nodd x\u209a by\u27e8 x\u2091 \u27e9 +\u209a odd y\u209a by\u27e8 y\u2091 \u27e9 = even 1+\u209a (x\u209a +\u209a y\u209a) by\u27e8 += x\u2091 y\u2091 \u2219 +-on-sides refl +-interchange \u27e9\n\ninfixl 7 2*\u209a_\n2*\u209a_ : \u2200 {x} \u2192 Parity-View x \u2192 Parity-View (2* x)\n2*\u209a x\u209a = x\u209a +\u209a x\u209a\n\nopen \u2261-Reasoning\n\nmodule _ {P Q} where\n \u00b7\u209a-\u2295-distr : \u2200 {x} (x\u209a : Parity-View x) \u2192 x\u209a \u00b7\u209a (P \u2295 Q) \u2261 x\u209a \u00b7\u209a P \u2295 x\u209a \u00b7\u209a Q\n \u00b7\u209a-\u2295-distr zero\u27e8 x\u2091 \u27e9 = ! \u03b5\u2295-identity\n \u00b7\u209a-\u2295-distr even x\u209a by\u27e8 x\u2091 \u27e9 = ap 2\u00b7_ (\u00b7\u209a-\u2295-distr x\u209a) \u2219 2\u00b7-\u2295-distr\n \u00b7\u209a-\u2295-distr odd x\u209a by\u27e8 x\u2091 \u27e9 = ap (\u03bb z \u2192 P \u2295 Q \u2295 2\u00b7 z) (\u00b7\u209a-\u2295-distr x\u209a)\n \u2219 ap (\u03bb z \u2192 P \u2295 Q \u2295 z) (! 2\u00b7-\u2295)\n \u2219 \u2295.interchange\n\nmodule _ {P} where\n cast-\u00b7\u209a-distr : \u2200 {x y} (x\u209a : Parity-View x) (y\u2091 : y \u2261 x) \u2192 cast x\u209a by\u27e8 y\u2091 \u27e9 \u00b7\u209a P \u2261 x\u209a \u00b7\u209a P\n cast-\u00b7\u209a-distr zero\u27e8 x\u2081 \u27e9 y\u2091 = refl\n cast-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2081 \u27e9 y\u2091 = refl\n cast-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2081 \u27e9 y\u2091 = refl\n\n 1+\u209a-\u00b7\u209a-distr : \u2200 {x} (x\u209a : Parity-View x) \u2192 (1+\u209a x\u209a) \u00b7\u209a P \u2261 P \u2295 x\u209a \u00b7\u209a P\n 1+\u209a-\u00b7\u209a-distr zero\u27e8 x\u2091 \u27e9 = ap (_\u2295_ P) \u03b5\u2295-identity\n 1+\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 = refl\n 1+\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 = ap 2\u00b7_ (1+\u209a-\u00b7\u209a-distr x\u209a) \u2219 \u2295.interchange \u2219 \u2295.assoc\n\n +\u209a-\u00b7\u209a-distr : \u2200 {x y} (x\u209a : Parity-View x) (y\u209a : Parity-View y)\n \u2192 (x\u209a +\u209a y\u209a) \u00b7\u209a P \u2261 x\u209a \u00b7\u209a P \u2295 y\u209a \u00b7\u209a P\n +\u209a-\u00b7\u209a-distr zero\u27e8 x\u2091 \u27e9 y\u209a = cast-\u00b7\u209a-distr y\u209a _ \u2219 ! \u03b5\u2295-identity\n\n +\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 zero\u27e8 y\u2091 \u27e9 = ! \u2295\u03b5-identity\n +\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 zero\u27e8 y\u2091 \u27e9 = ! \u2295\u03b5-identity\n\n +\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 even y\u209a by\u27e8 y\u2091 \u27e9 = ap 2\u00b7_ (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u2219 2\u00b7-\u2295-distr\n +\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 odd y\u209a by\u27e8 y\u2091 \u27e9 = ap (_\u2295_ P) (ap 2\u00b7_ (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u2219 2\u00b7-\u2295-distr) \u2219 \u2295.assoc-comm\n +\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 even y\u209a by\u27e8 y\u2091 \u27e9 = ap (_\u2295_ P) (ap 2\u00b7_ (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u2219 2\u00b7-\u2295-distr) \u2219 ! \u2295.assoc\n +\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 odd y\u209a by\u27e8 y\u2091 \u27e9\n = (odd x\u209a by\u27e8 x\u2091 \u27e9 +\u209a odd y\u209a by\u27e8 y\u2091 \u27e9) \u00b7\u209a P\n \u2261\u27e8by-definition\u27e9\n 2\u00b7((1+\u209a (x\u209a +\u209a y\u209a)) \u00b7\u209a P)\n \u2261\u27e8 ap 2\u00b7_ helper \u27e9\n 2\u00b7(P \u2295 (x\u209a \u00b7\u209a P \u2295 y\u209a \u00b7\u209a P))\n \u2261\u27e8 2\u00b7-\u2295-distr \u27e9\n 2\u00b7 P \u2295 (2\u00b7(x\u209a \u00b7\u209a P \u2295 y\u209a \u00b7\u209a P))\n \u2261\u27e8 ap (_\u2295_ (2\u00b7 P)) 2\u00b7-\u2295-distr \u27e9\n 2\u00b7 P \u2295 (2\u00b7(x\u209a \u00b7\u209a P) \u2295 2\u00b7(y\u209a \u00b7\u209a P))\n \u2261\u27e8 2\u00b7-\u2295 \u27e9\n P \u2295 2\u00b7(x\u209a \u00b7\u209a P) \u2295 (P \u2295 2\u00b7(y\u209a \u00b7\u209a P))\n \u2261\u27e8by-definition\u27e9\n odd x\u209a by\u27e8 x\u2091 \u27e9 \u00b7\u209a P \u2295 odd y\u209a by\u27e8 y\u2091 \u27e9 \u00b7\u209a P\n \u220e\n where helper = (1+\u209a (x\u209a +\u209a y\u209a)) \u00b7\u209a P\n \u2261\u27e8 1+\u209a-\u00b7\u209a-distr (x\u209a +\u209a y\u209a) \u27e9\n P \u2295 ((x\u209a +\u209a y\u209a) \u00b7\u209a P)\n \u2261\u27e8 ap (_\u2295_ P) (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u27e9\n P \u2295 (x\u209a \u00b7\u209a P \u2295 y\u209a \u00b7\u209a P)\n \u220e\n\n*-1+-distr : \u2200 {x y} \u2192 x * (1+ y) \u2261 x + x * y\n*-1+-distr = *-+-distr \u2219 += *1-identity refl\n\n1+-*-distr : \u2200 {x y} \u2192 (1+ x) * y \u2261 y + x * y\n1+-*-distr = +-*-distr \u2219 += 1*-identity refl\n\ninfixl 7 _*\u209a_\n_*\u209a_ : \u2200 {x y} \u2192 Parity-View x \u2192 Parity-View y \u2192 Parity-View (x * y)\nzero\u27e8 x\u2091 \u27e9 *\u209a y\u209a = zero\u27e8 *= x\u2091 refl \u2219 0*-zero \u27e9\nx\u209a *\u209a zero\u27e8 y\u2091 \u27e9 = zero\u27e8 *= refl y\u2091 \u2219 *0-zero \u27e9\neven x\u209a by\u27e8 x\u2091 \u27e9 *\u209a y\u209a = even (x\u209a *\u209a y\u209a) by\u27e8 *= x\u2091 refl \u2219 +-*-distr \u27e9\nx\u209a *\u209a even y\u209a by\u27e8 y\u2091 \u27e9 = even (x\u209a *\u209a y\u209a) by\u27e8 *= refl y\u2091 \u2219 *-+-distr \u27e9\nodd x\u209a by\u27e8 x\u2091 \u27e9 *\u209a odd y\u209a by\u27e8 y\u2091 \u27e9 = odd (x\u209a +\u209a y\u209a +\u209a 2*\u209a (x\u209a *\u209a y\u209a)) by\u27e8 *= x\u2091 y\u2091 \u2219 helper \u27e9\n where\n x = _\n y = _\n helper = (1+2* x)*(1+2* y)\n \u2261\u27e8 1+-*-distr \u27e9\n 1+2* y + 2* x * 1+2* y\n \u2261\u27e8 ap (\u03bb z \u2192 1+2* y + z)\n (2* x * 1+2* y\n \u2261\u27e8 *-1+-distr \u27e9\n (2* x + 2* x * 2* y)\n \u2261\u27e8 += refl *-+-distr \u2219 +-interchange \u27e9\n (2* (x + 2* x * y))\n \u220e) \u27e9\n 1+2* y + 2* (x + 2* x * y)\n \u2261\u27e8 +-assoc \u2219 ap 1+_ +-interchange \u27e9\n 1+2*(y + (x + 2* x * y))\n \u2261\u27e8 ap 1+2*_ (! +-assoc \u2219 += +-comm refl) \u27e9\n 1+2*(x + y + 2* x * y)\n \u2261\u27e8 ap (\u03bb z \u2192 1+2*(x + y + z)) +-*-distr \u27e9\n 1+2*(x + y + 2* (x * y))\n \u220e\n\nmodule _ {P} where\n 2\u00b7-\u00b7\u209a-distr : \u2200 {x} (x\u209a : Parity-View x) \u2192 2\u00b7(x\u209a \u00b7\u209a P) \u2261 x\u209a \u00b7\u209a 2\u00b7 P\n 2\u00b7-\u00b7\u209a-distr x\u209a = ! \u00b7\u209a-\u2295-distr x\u209a\n\n 2*\u209a-\u00b7\u209a-distr : \u2200 {x} (x\u209a : Parity-View x) \u2192 (2*\u209a x\u209a) \u00b7\u209a P \u2261 2\u00b7(x\u209a \u00b7\u209a P)\n 2*\u209a-\u00b7\u209a-distr x\u209a = +\u209a-\u00b7\u209a-distr x\u209a x\u209a\n\n\u00b7\u209a-\u03b5 : \u2200 {x} (x\u209a : Parity-View x) \u2192 x\u209a \u00b7\u209a \u03b5 \u2261 \u03b5\n\u00b7\u209a-\u03b5 zero\u27e8 x\u2091 \u27e9 = refl\n\u00b7\u209a-\u03b5 even x\u209a by\u27e8 x\u2091 \u27e9 = ap 2\u00b7_ (\u00b7\u209a-\u03b5 x\u209a) \u2219 \u03b5\u2295-identity\n\u00b7\u209a-\u03b5 odd x\u209a by\u27e8 x\u2091 \u27e9 = \u03b5\u2295-identity \u2219 ap 2\u00b7_ (\u00b7\u209a-\u03b5 x\u209a) \u2219 \u03b5\u2295-identity\n\n*\u209a-\u00b7\u209a-distr : \u2200 {x y} (x\u209a : Parity-View x) (y\u209a : Parity-View y) {P} \u2192 (x\u209a *\u209a y\u209a) \u00b7\u209a P \u2261 x\u209a \u00b7\u209a y\u209a \u00b7\u209a P\n*\u209a-\u00b7\u209a-distr zero\u27e8 x\u2091 \u27e9 y\u209a = refl\n*\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 zero\u27e8 y\u2091 \u27e9 = ! \u00b7\u209a-\u03b5 even x\u209a by\u27e8 x\u2091 \u27e9\n*\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 zero\u27e8 y\u2091 \u27e9 = ! \u00b7\u209a-\u03b5 odd x\u209a by\u27e8 x\u2091 \u27e9\n\n*\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 even y\u209a by\u27e8 y\u2091 \u27e9 = ap 2\u00b7_ (*\u209a-\u00b7\u209a-distr x\u209a even y\u209a by\u27e8 y\u2091 \u27e9)\n*\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 odd y\u209a by\u27e8 y\u2091 \u27e9 = ap 2\u00b7_ (*\u209a-\u00b7\u209a-distr x\u209a odd y\u209a by\u27e8 y\u2091 \u27e9)\n\n*\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 even y\u209a by\u27e8 y\u2091 \u27e9 {P} =\n ap 2\u00b7_ (*\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 y\u209a) \u2219 2\u00b7-\u2295-distr \u2219 ap (\u03bb z \u2192 2\u00b7 (y\u209a \u00b7\u209a P) \u2295 2\u00b7 z) (2\u00b7-\u00b7\u209a-distr x\u209a)\n*\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 odd y\u209a by\u27e8 y\u2091 \u27e9 {P} =\n ap (_\u2295_ P)\n (ap 2\u00b7_\n (+\u209a-\u00b7\u209a-distr (x\u209a +\u209a y\u209a) (2*\u209a (x\u209a *\u209a y\u209a))\n \u2219 \u2295= (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) (2*\u209a-\u00b7\u209a-distr (x\u209a *\u209a y\u209a))\n \u2219 \u2295= \u2295.comm (ap 2\u00b7_ (*\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u2219 2\u00b7-\u00b7\u209a-distr x\u209a) \u2219 \u2295.assoc\n \u2219 \u2295= refl (! \u00b7\u209a-\u2295-distr x\u209a) ) \u2219 2\u00b7-\u2295-distr)\n \u2219 ! \u2295.assoc\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"--open import prelude renaming (Bool to \ud835\udfda; true to 1\u2082; false to 0\u2082)\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Data.Two.Base\nopen import Data.List\nopen import Function\nopen import Algebra.FunctionProperties.Eq\n\nmodule _ where\n\ninfixl 6 _+_ -- _-_\ninfixl 7 _*_ -- _%_\ninfixl 6 _+P_\n\npostulate\n Number : Set\n _+_ _*_ : Number \u2192 Number \u2192 Number\n -- _-_ _\/_ _%_ : Number \u2192 Number \u2192 Number\n -- Pcurve Acurve : Number\n 0\u2099 1\u2099 {-2\u2099 3\u2099-} : Number\n -- bin : Number \u2192 List \ud835\udfda\n\n Point : Set\n _+P_ : Point \u2192 Point \u2192 Point\n +P-assoc : \u2200 {P Q R} \u2192 (P +P Q) +P R \u2261 P +P (Q +P R)\n +P-comm : \u2200 {P Q} \u2192 P +P Q \u2261 Q +P P\n\n 0P : Point\n 0P+-cancel : \u2200 {x} \u2192 0P +P x \u2261 x\n\n+0P-cancel : \u2200 {x} \u2192 x +P 0P \u2261 x\n+0P-cancel = +P-comm \u2219 0P+-cancel\n\n2\u00b7_ : Point \u2192 Point\n2\u00b7 P = P +P P\n\n+P= : \u2200 {x x' y y'} \u2192 x \u2261 x' \u2192 y \u2261 y' \u2192 x +P y \u2261 x' +P y'\n+P= {x} {y' = y'} p q = ap (_+P_ x) q \u2219 ap (\u03bb z \u2192 z +P y') p\n\n+P-interchange : \u2200 {x y z t} \u2192 (x +P y) +P (z +P t) \u2261 (x +P z) +P (y +P t)\n+P-interchange = InterchangeFromAssocComm.\u00b7-interchange _+P_ +P-assoc +P-comm\n\n2\u00b7-+P-2\u00b7 : \u2200 {P Q} \u2192 2\u00b7 (P +P Q) \u2261 2\u00b7 P +P 2\u00b7 Q\n2\u00b7-+P-2\u00b7 = +P-interchange\n\n+P-comm-2of3 : \u2200 {P Q R} \u2192 P +P (Q +P R) \u2261 Q +P (P +P R)\n+P-comm-2of3 = ! +P-assoc \u2219 +P= +P-comm refl \u2219 +P-assoc\n\n2\u00b7-+P : \u2200 {P Q R} \u2192 2\u00b7 P +P (Q +P R) \u2261 (P +P Q) +P (P +P R)\n2\u00b7-+P = +P-interchange\n \n{-\nec-multiply-bin : List \ud835\udfda \u2192 Point \u2192 Point\nec-multiply-bin scalar P = go scalar\n where\n go : List \ud835\udfda \u2192 Point\n go [] = P\n go (b \u2237 bs) = [0: x\u2080 1: x\u2081 ] b\n where x\u2080 = 2\u00b7 go bs\n x\u2081 = P +P x\u2080\n\nec-multiply : Number \u2192 Point \u2192 Point\nec-multiply scalar P =\n -- if scalar == 0 or scalar >= N: raise Exception(\"Invalid Scalar\/Private Key\")\n ec-multiply-bin (bin scalar) P\n\n_\u00b7_ = ec-multiply\ninfixr 8 _\u00b7_\n-}\n\ninfixl 6 1+_\ninfixl 7 2*_ 1+2*_\n1+_ = \u03bb x \u2192 1\u2099 + x\n2*_ = \u03bb x \u2192 x + x\n1+2*_ = \u03bb x \u2192 1+ 2* x\n\ndata Parity-View : Number \u2192 Set where\n zero\u27e8_\u27e9 : \u2200 {n} \u2192 n \u2261 0\u2099 \u2192 Parity-View n\n even_by\u27e8_\u27e9 : \u2200 {m n} \u2192 Parity-View m \u2192 n \u2261 2* m \u2192 Parity-View n\n odd_by\u27e8_\u27e9 : \u2200 {m n} \u2192 Parity-View m \u2192 n \u2261 1+ 2* m \u2192 Parity-View n\n\ncast_by\u27e8_\u27e9 : \u2200 {x y} \u2192 Parity-View x \u2192 y \u2261 x \u2192 Parity-View y\ncast zero\u27e8 x\u2091 \u27e9 by\u27e8 y\u2091 \u27e9 = zero\u27e8 y\u2091 \u2219 x\u2091 \u27e9\ncast even x\u209a by\u27e8 x\u2091 \u27e9 by\u27e8 y\u2091 \u27e9 = even x\u209a by\u27e8 y\u2091 \u2219 x\u2091 \u27e9\ncast odd x\u209a by\u27e8 x\u2091 \u27e9 by\u27e8 y\u2091 \u27e9 = odd x\u209a by\u27e8 y\u2091 \u2219 x\u2091 \u27e9\n\ninfixr 8 _\u00b7\u209a_\n_\u00b7\u209a_ : \u2200 {n} (p : Parity-View n) \u2192 Point \u2192 Point\nzero\u27e8 e \u27e9 \u00b7\u209a P = 0P\neven p by\u27e8 e \u27e9 \u00b7\u209a P = 2\u00b7 (p \u00b7\u209a P)\nodd p by\u27e8 e \u27e9 \u00b7\u209a P = P +P (2\u00b7 (p \u00b7\u209a P))\n\n+= : \u2200 {x x' y y'} \u2192 x \u2261 x' \u2192 y \u2261 y' \u2192 x + y \u2261 x' + y'\n+= {x} {y' = y'} p q = ap (_+_ x) q \u2219 ap (\u03bb z \u2192 z + y') p\n\n*= : \u2200 {x x' y y'} \u2192 x \u2261 x' \u2192 y \u2261 y' \u2192 x * y \u2261 x' * y'\n*= {x} {y' = y'} p q = ap (_*_ x) q \u2219 ap (\u03bb z \u2192 z * y') p\n\nmodule Add\u209a\n (+-assoc : \u2200 {x y z} \u2192 (x + y) + z \u2261 x + (y + z))\n (+-comm : \u2200 {x y} \u2192 x + y \u2261 y + x)\n (0+-cancel : \u2200 {x} \u2192 0\u2099 + x \u2261 x)\n where\n\n +-interchange : \u2200 {x y z t} \u2192 (x + y) + (z + t) \u2261 (x + z) + (y + t)\n +-interchange = InterchangeFromAssocComm.\u00b7-interchange _+_ +-assoc +-comm\n\n +-on-sides : \u2200 {x x' y y' z z' t t'}\n \u2192 x + z \u2261 x' + z'\n \u2192 y + t \u2261 y' + t'\n \u2192 (x + y) + (z + t) \u2261 (x' + y') + (z' + t')\n +-on-sides p q = +-interchange \u2219 += p q \u2219 +-interchange\n\n -- only needs interchange and comm\n {- UNUSED\n +-inner : \u2200 {x y y' z z' t} \u2192 y + z \u2261 z' + y' \u2192 (x + y) + (z + t) \u2261 (x + y') + (z' + t)\n +-inner p = += +-comm refl \u2219 +-on-sides (p \u2219 +-comm) refl \u2219 += +-comm refl\n -}\n\n _+2*_ : \ud835\udfda \u2192 Number \u2192 Number\n 0\u2082 +2* m = 2* m\n 1\u2082 +2* m = 1+2* m\n\n {-\n postulate\n bin-2* : \u2200 {n} \u2192 bin (2* n) \u2261 0\u2082 \u2237 bin n\n bin-1+2* : \u2200 {n} \u2192 bin (1+2* n) \u2261 1\u2082 \u2237 bin n\n\n bin-+2* : (b : \ud835\udfda)(n : Number) \u2192 bin (b +2* n) \u2261 b \u2237 bin n\n bin-+2* 1\u2082 n = bin-1+2*\n bin-+2* 0\u2082 n = bin-2*\n -}\n\n -- (msb) most significant bit first\n bin\u209a : \u2200 {n} \u2192 Parity-View n \u2192 List \ud835\udfda\n bin\u209a zero\u27e8 e \u27e9 = []\n bin\u209a even p by\u27e8 e \u27e9 = 0\u2082 \u2237 bin\u209a p\n bin\u209a odd p by\u27e8 e \u27e9 = 1\u2082 \u2237 bin\u209a p\n\n half : \u2200 {n} \u2192 Parity-View n \u2192 Number\n half zero\u27e8 _ \u27e9 = 0\u2099\n half (even_by\u27e8_\u27e9 {m} _ _) = m\n half (odd_by\u27e8_\u27e9 {m} _ _) = m\n\n {-\n bin-parity : \u2200 {n} (p : ParityView n) \u2192 bin n \u2261 parity p \u2237 bin (half p)\n bin-parity (even n) = bin-2*\n bin-parity (odd n) = bin-1+2*\n -}\n\n infixl 6 1+\u209a_ _+\u209a_\n 1+\u209a_ : \u2200 {x} \u2192 Parity-View x \u2192 Parity-View (1+ x)\n 1+\u209a zero\u27e8 e \u27e9 = odd zero\u27e8 refl \u27e9 by\u27e8 ap 1+_ (e \u2219 ! 0+-cancel) \u27e9\n 1+\u209a even p by\u27e8 e \u27e9 = odd p by\u27e8 ap 1+_ e \u27e9\n 1+\u209a odd p by\u27e8 e \u27e9 = even 1+\u209a p by\u27e8 ap 1+_ e \u2219 ! +-assoc \u2219 +-interchange \u27e9\n\n _+\u209a_ : \u2200 {x y} \u2192 Parity-View x \u2192 Parity-View y \u2192 Parity-View (x + y)\n zero\u27e8 x\u2091 \u27e9 +\u209a y\u209a = cast y\u209a by\u27e8 ap (\u03bb z \u2192 z + _) x\u2091 \u2219 0+-cancel \u27e9\n x\u209a +\u209a zero\u27e8 y\u2091 \u27e9 = cast x\u209a by\u27e8 ap (_+_ _) y\u2091 \u2219 +-comm \u2219 0+-cancel \u27e9\n even x\u209a by\u27e8 x\u2091 \u27e9 +\u209a even y\u209a by\u27e8 y\u2091 \u27e9 = even x\u209a +\u209a y\u209a by\u27e8 += x\u2091 y\u2091 \u2219 +-interchange \u27e9\n even x\u209a by\u27e8 x\u2091 \u27e9 +\u209a odd y\u209a by\u27e8 y\u2091 \u27e9 = odd x\u209a +\u209a y\u209a by\u27e8 += x\u2091 y\u2091 \u2219 +-comm \u2219 +-assoc \u2219 ap 1+_ (+-comm \u2219 +-interchange) \u27e9\n odd x\u209a by\u27e8 x\u2091 \u27e9 +\u209a even y\u209a by\u27e8 y\u2091 \u27e9 = odd x\u209a +\u209a y\u209a by\u27e8 += x\u2091 y\u2091 \u2219 +-assoc \u2219 ap 1+_ +-interchange \u27e9\n odd x\u209a by\u27e8 x\u2091 \u27e9 +\u209a odd y\u209a by\u27e8 y\u2091 \u27e9 = even 1+\u209a (x\u209a +\u209a y\u209a) by\u27e8 += x\u2091 y\u2091 \u2219 +-on-sides refl +-interchange \u27e9\n\n infixl 7 2*\u209a_\n 2*\u209a_ : \u2200 {x} \u2192 Parity-View x \u2192 Parity-View (2* x)\n 2*\u209a x\u209a = x\u209a +\u209a x\u209a\n\n open \u2261-Reasoning\n\n module _ {P Q} where\n \u00b7\u209a-+P-distr : \u2200 {x} (x\u209a : Parity-View x) \u2192 x\u209a \u00b7\u209a (P +P Q) \u2261 x\u209a \u00b7\u209a P +P x\u209a \u00b7\u209a Q\n \u00b7\u209a-+P-distr zero\u27e8 x\u2091 \u27e9 = ! 0P+-cancel\n \u00b7\u209a-+P-distr even x\u209a by\u27e8 x\u2091 \u27e9 = ap 2\u00b7_ (\u00b7\u209a-+P-distr x\u209a) \u2219 2\u00b7-+P-2\u00b7\n \u00b7\u209a-+P-distr odd x\u209a by\u27e8 x\u2091 \u27e9 = ap (\u03bb z \u2192 P +P Q +P 2\u00b7 z) (\u00b7\u209a-+P-distr x\u209a)\n \u2219 ap (\u03bb z \u2192 P +P Q +P z) (! 2\u00b7-+P)\n \u2219 +P-interchange\n\n module _ {P} where\n cast-\u00b7\u209a-distr : \u2200 {x y} (x\u209a : Parity-View x) (y\u2091 : y \u2261 x) \u2192 cast x\u209a by\u27e8 y\u2091 \u27e9 \u00b7\u209a P \u2261 x\u209a \u00b7\u209a P\n cast-\u00b7\u209a-distr zero\u27e8 x\u2081 \u27e9 y\u2091 = refl\n cast-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2081 \u27e9 y\u2091 = refl\n cast-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2081 \u27e9 y\u2091 = refl\n \n 1+\u209a-\u00b7\u209a-distr : \u2200 {x} (x\u209a : Parity-View x) \u2192 (1+\u209a x\u209a) \u00b7\u209a P \u2261 P +P x\u209a \u00b7\u209a P\n 1+\u209a-\u00b7\u209a-distr zero\u27e8 x\u2091 \u27e9 = ap (_+P_ P) 0P+-cancel\n 1+\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 = refl\n 1+\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 = ap 2\u00b7_ (1+\u209a-\u00b7\u209a-distr x\u209a) \u2219 +P-interchange \u2219 +P-assoc\n\n +\u209a-\u00b7\u209a-distr : \u2200 {x y} (x\u209a : Parity-View x) (y\u209a : Parity-View y)\n \u2192 (x\u209a +\u209a y\u209a) \u00b7\u209a P \u2261 x\u209a \u00b7\u209a P +P y\u209a \u00b7\u209a P\n +\u209a-\u00b7\u209a-distr zero\u27e8 x\u2091 \u27e9 y\u209a = cast-\u00b7\u209a-distr y\u209a _ \u2219 ! 0P+-cancel\n\n +\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 zero\u27e8 y\u2091 \u27e9 = ! +0P-cancel\n +\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 zero\u27e8 y\u2091 \u27e9 = ! +0P-cancel\n\n +\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 even y\u209a by\u27e8 y\u2091 \u27e9 = ap 2\u00b7_ (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u2219 2\u00b7-+P-2\u00b7\n +\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 odd y\u209a by\u27e8 y\u2091 \u27e9 = ap (_+P_ P) (ap 2\u00b7_ (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u2219 2\u00b7-+P-2\u00b7) \u2219 +P-comm-2of3\n +\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 even y\u209a by\u27e8 y\u2091 \u27e9 = ap (_+P_ P) (ap 2\u00b7_ (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u2219 2\u00b7-+P-2\u00b7) \u2219 ! +P-assoc\n +\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 odd y\u209a by\u27e8 y\u2091 \u27e9\n = (odd x\u209a by\u27e8 x\u2091 \u27e9 +\u209a odd y\u209a by\u27e8 y\u2091 \u27e9) \u00b7\u209a P\n \u2261\u27e8by-definition\u27e9\n 2\u00b7((1+\u209a (x\u209a +\u209a y\u209a)) \u00b7\u209a P)\n \u2261\u27e8 ap 2\u00b7_ helper \u27e9\n 2\u00b7(P +P (x\u209a \u00b7\u209a P +P y\u209a \u00b7\u209a P))\n \u2261\u27e8 2\u00b7-+P-2\u00b7 \u27e9\n 2\u00b7 P +P (2\u00b7(x\u209a \u00b7\u209a P +P y\u209a \u00b7\u209a P))\n \u2261\u27e8 ap (_+P_ (2\u00b7 P)) 2\u00b7-+P-2\u00b7 \u27e9\n 2\u00b7 P +P (2\u00b7(x\u209a \u00b7\u209a P) +P 2\u00b7(y\u209a \u00b7\u209a P))\n \u2261\u27e8 2\u00b7-+P \u27e9\n P +P 2\u00b7(x\u209a \u00b7\u209a P) +P (P +P 2\u00b7(y\u209a \u00b7\u209a P))\n \u2261\u27e8by-definition\u27e9\n odd x\u209a by\u27e8 x\u2091 \u27e9 \u00b7\u209a P +P odd y\u209a by\u27e8 y\u2091 \u27e9 \u00b7\u209a P\n \u220e\n where helper = (1+\u209a (x\u209a +\u209a y\u209a)) \u00b7\u209a P\n \u2261\u27e8 1+\u209a-\u00b7\u209a-distr (x\u209a +\u209a y\u209a) \u27e9\n P +P ((x\u209a +\u209a y\u209a) \u00b7\u209a P)\n \u2261\u27e8 ap (_+P_ P) (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u27e9\n P +P (x\u209a \u00b7\u209a P +P y\u209a \u00b7\u209a P)\n \u220e\n\n module Mult\u209a\n (+-*-distr : \u2200 {x y z} \u2192 (x + y) * z \u2261 x * z + y * z)\n (*-+-distr : \u2200 {x y z} \u2192 x * (y + z) \u2261 x * y + x * z)\n (1*-cancel : \u2200 {x} \u2192 1\u2099 * x \u2261 x)\n (*1-cancel : \u2200 {x} \u2192 x * 1\u2099 \u2261 x)\n (0*-cancel : \u2200 {x} \u2192 0\u2099 * x \u2261 0\u2099)\n (*0-cancel : \u2200 {x} \u2192 x * 0\u2099 \u2261 0\u2099)\n --(modinv-*-distr : \u2200 {x y} \u2192 modinv (x * y) \u2261 modinv x * modinv y)\n --(modinv-modinv : \u2200 {x} \u2192 modinv (modinv x) \u2261 x)\n --(*-assoc : \u2200 {x y z} \u2192 (x * y) * z \u2261 x * (y * z))\n --(*-comm : \u2200 {x y} \u2192 x * y \u2261 y * x)\n --(modinv-cancel : \u2200 {x y} \u2192 x * modinv x * y \u2261 y)\n --(2*1\u2099 : 2* 1\u2099 \u2261 2\u2099)\n --(2*-spec : \u2200 {n} \u2192 2* n \u2261 2\u2099 * n)\n where\n\n *-1+-distr : \u2200 {x y} \u2192 x * (1+ y) \u2261 x + x * y\n *-1+-distr = *-+-distr \u2219 += *1-cancel refl\n\n 1+-*-distr : \u2200 {x y} \u2192 (1+ x) * y \u2261 y + x * y\n 1+-*-distr = +-*-distr \u2219 += 1*-cancel refl\n\n infixl 7 _*\u209a_\n _*\u209a_ : \u2200 {x y} \u2192 Parity-View x \u2192 Parity-View y \u2192 Parity-View (x * y)\n zero\u27e8 x\u2091 \u27e9 *\u209a y\u209a = zero\u27e8 *= x\u2091 refl \u2219 0*-cancel \u27e9\n x\u209a *\u209a zero\u27e8 y\u2091 \u27e9 = zero\u27e8 *= refl y\u2091 \u2219 *0-cancel \u27e9\n even x\u209a by\u27e8 x\u2091 \u27e9 *\u209a y\u209a = even (x\u209a *\u209a y\u209a) by\u27e8 *= x\u2091 refl \u2219 +-*-distr \u27e9\n x\u209a *\u209a even y\u209a by\u27e8 y\u2091 \u27e9 = even (x\u209a *\u209a y\u209a) by\u27e8 *= refl y\u2091 \u2219 *-+-distr \u27e9\n odd x\u209a by\u27e8 x\u2091 \u27e9 *\u209a odd y\u209a by\u27e8 y\u2091 \u27e9 = odd (x\u209a +\u209a y\u209a +\u209a 2*\u209a (x\u209a *\u209a y\u209a)) by\u27e8 *= x\u2091 y\u2091 \u2219 helper \u27e9\n where\n x = _\n y = _\n helper = (1+2* x)*(1+2* y)\n \u2261\u27e8 1+-*-distr \u27e9\n 1+2* y + 2* x * 1+2* y\n \u2261\u27e8 ap (\u03bb z \u2192 1+2* y + z)\n (2* x * 1+2* y\n \u2261\u27e8 *-1+-distr \u27e9\n (2* x + 2* x * 2* y)\n \u2261\u27e8 += refl *-+-distr \u2219 +-interchange \u27e9\n (2* (x + 2* x * y))\n \u220e) \u27e9\n 1+2* y + 2* (x + 2* x * y)\n \u2261\u27e8 +-assoc \u2219 ap 1+_ +-interchange \u27e9\n 1+2*(y + (x + 2* x * y))\n \u2261\u27e8 ap 1+2*_ (! +-assoc \u2219 += +-comm refl) \u27e9\n 1+2*(x + y + 2* x * y)\n \u2261\u27e8 ap (\u03bb z \u2192 1+2*(x + y + z)) +-*-distr \u27e9\n 1+2*(x + y + 2* (x * y))\n \u220e\n\n module _ {P} where\n 2\u00b7-\u00b7\u209a-distr : \u2200 {x} (x\u209a : Parity-View x) \u2192 2\u00b7(x\u209a \u00b7\u209a P) \u2261 x\u209a \u00b7\u209a 2\u00b7 P\n 2\u00b7-\u00b7\u209a-distr x\u209a = ! \u00b7\u209a-+P-distr x\u209a\n\n 2*\u209a-\u00b7\u209a-distr : \u2200 {x} (x\u209a : Parity-View x) \u2192 (2*\u209a x\u209a) \u00b7\u209a P \u2261 2\u00b7(x\u209a \u00b7\u209a P)\n 2*\u209a-\u00b7\u209a-distr x\u209a = +\u209a-\u00b7\u209a-distr x\u209a x\u209a\n\n \u00b7\u209a-0P : \u2200 {x} (x\u209a : Parity-View x) \u2192 x\u209a \u00b7\u209a 0P \u2261 0P\n \u00b7\u209a-0P zero\u27e8 x\u2091 \u27e9 = refl\n \u00b7\u209a-0P even x\u209a by\u27e8 x\u2091 \u27e9 = ap 2\u00b7_ (\u00b7\u209a-0P x\u209a) \u2219 0P+-cancel\n \u00b7\u209a-0P odd x\u209a by\u27e8 x\u2091 \u27e9 = 0P+-cancel \u2219 ap 2\u00b7_ (\u00b7\u209a-0P x\u209a) \u2219 0P+-cancel\n\n *\u209a-\u00b7\u209a-distr : \u2200 {x y} (x\u209a : Parity-View x) (y\u209a : Parity-View y) {P} \u2192 (x\u209a *\u209a y\u209a) \u00b7\u209a P \u2261 x\u209a \u00b7\u209a y\u209a \u00b7\u209a P\n *\u209a-\u00b7\u209a-distr zero\u27e8 x\u2091 \u27e9 y\u209a = refl\n *\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 zero\u27e8 y\u2091 \u27e9 = ! \u00b7\u209a-0P even x\u209a by\u27e8 x\u2091 \u27e9\n *\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 zero\u27e8 y\u2091 \u27e9 = ! \u00b7\u209a-0P odd x\u209a by\u27e8 x\u2091 \u27e9\n\n *\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 even y\u209a by\u27e8 y\u2091 \u27e9 = ap 2\u00b7_ (*\u209a-\u00b7\u209a-distr x\u209a even y\u209a by\u27e8 y\u2091 \u27e9)\n *\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 odd y\u209a by\u27e8 y\u2091 \u27e9 = ap 2\u00b7_ (*\u209a-\u00b7\u209a-distr x\u209a odd y\u209a by\u27e8 y\u2091 \u27e9)\n\n *\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 even y\u209a by\u27e8 y\u2091 \u27e9 {P} = ap 2\u00b7_ (*\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 y\u209a) \u2219 2\u00b7-+P-2\u00b7 \u2219 ap (\u03bb z \u2192 2\u00b7 (y\u209a \u00b7\u209a P) +P 2\u00b7 z) (2\u00b7-\u00b7\u209a-distr x\u209a)\n *\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 odd y\u209a by\u27e8 y\u2091 \u27e9 {P} =\n ap (_+P_ P)\n (ap 2\u00b7_\n (+\u209a-\u00b7\u209a-distr (x\u209a +\u209a y\u209a) (2*\u209a (x\u209a *\u209a y\u209a))\n \u2219 +P= (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) (2*\u209a-\u00b7\u209a-distr (x\u209a *\u209a y\u209a))\n \u2219 +P= +P-comm (ap 2\u00b7_ (*\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u2219 2\u00b7-\u00b7\u209a-distr x\u209a) \u2219 +P-assoc\n \u2219 +P= refl (! \u00b7\u209a-+P-distr x\u209a) ) \u2219 2\u00b7-+P-2\u00b7)\n \u2219 ! +P-assoc\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"dc4c757de571dbf9decd0ec09fa3bce159f0b948","subject":"Direct proof of well-founded induction.","message":"Direct proof of well-founded induction.\n\nIgnore-this: be4f83042601bf8c031fec62e2fb0878\n\ndarcs-hash:20100525021915-3bd4e-695229f36277d68ee396016369b2744726dd84c7.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Data\/N\/Induction\/WellFounded.agda","new_file":"LTC\/Data\/N\/Induction\/WellFounded.agda","new_contents":"------------------------------------------------------------------------------\n-- Well-founded induction on N\n------------------------------------------------------------------------------\n\nmodule LTC.Data.N.Induction.WellFounded where\n\nopen import LTC.Minimal\n\nopen import LTC.Data.N\nopen import LTC.Relation.Inequalities\nopen import LTC.Relation.Inequalities.Properties\n\n------------------------------------------------------------------------------\n-- Well-founded induction on N\n-- Adapted from http:\/\/code.haskell.org\/~dolio\/agda-share\/induction\/.\n\nwfIndN-LT :\n (P : D \u2192 Set) \u2192\n ({m : D} \u2192 N m \u2192 ({n : D} \u2192 N n \u2192 LT n m \u2192 P n ) \u2192 P m ) \u2192\n {n : D} \u2192 N n \u2192 P n\nwfIndN-LT P accH Nn = accH Nn (wfAux Nn)\n where\n wfAux : {m : D} \u2192 N m \u2192 {n : D} \u2192 N n \u2192 LT n m \u2192 P n\n wfAux zN Nn n<0 = \u22a5-elim (\u00acx<0 Nn n<0)\n wfAux (sN Nm) zN 0x : \u2200 {n} \u2192 N n \u2192 \u00ac (GT zero n)\n{-# ATP prove \u00ac0>x 0\u226fx #-}\n\nx\u2270x : \u2200 {n} \u2192 N n \u2192 NLT n n\nx\u2270x zN = <-00\nx\u2270x (sN {n} Nn) = trans (<-SS n n) (x\u2270x Nn)\n\nS\u22700 : \u2200 {n} \u2192 N n \u2192 NLE (succ n) zero\nS\u22700 zN = x\u2270x (sN zN)\nS\u22700 (sN {n} Nn) = trans (<-SS (succ n) zero) (<-S0 n)\n\n\u00acS\u22640 : \u2200 {n} \u2192 N n \u2192 \u00ac (LE (succ n) zero)\n\u00acS\u22640 {d} Nn Sn\u22640 = true\u2260false $ trans (sym Sn\u22640) (S\u22700 Nn)\n\n\u00ac0\u2265S : \u2200 {n} \u2192 N n \u2192 \u00ac (GE zero (succ n))\n\u00ac0\u2265S Nn 0\u2265Sn = \u00acS\u22640 Nn 0\u2265Sn\n\nxx : \u2200 {n} \u2192 N n \u2192 \u00ac (GT n n)\n\u00acx>x Nn = \u00acxy\u2192yy\u2192yn = \u22a5-elim $ \u00ac0>x Nn 0>n\nx>y\u2192yy\u2192ySn =\n trans (<-SS n m) (x>y\u2192ySn))\n\nx\u2265y\u2192x\u226ey : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GE m n \u2192 NLT m n\nx\u2265y\u2192x\u226ey zN zN _ = x\u2270x zN\nx\u2265y\u2192x\u226ey zN (sN Nn) 0\u2265Sn = \u22a5-elim $ \u00ac0\u2265S Nn 0\u2265Sn\nx\u2265y\u2192x\u226ey (sN {m} Nm) zN _ = <-S0 m\nx\u2265y\u2192x\u226ey (sN {m} Nm) (sN {n} Nn) Sm\u2265Sn =\n trans (<-SS m n) (x\u2265y\u2192x\u226ey Nm Nn (trans (sym $ <-SS n (succ m)) Sm\u2265Sn))\n\nx>y\u2192x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 NLE m n\nx>y\u2192x\u2270y zN Nn 0>m = \u22a5-elim $ \u00ac0>x Nn 0>m\nx>y\u2192x\u2270y (sN Nm) zN _ = S\u22700 Nm\nx>y\u2192x\u2270y (sN {m} Nm) (sN {n} Nn) Sm>Sn =\n x\u2270y\u2192Sx\u2270Sy m n (x>y\u2192x\u2270y Nm Nn (trans (sym $ <-SS n m) Sm>Sn))\n\nx>y\u2228x\u2264y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2228 LE m n\nx>y\u2228x\u2264y zN Nn = inj\u2082 $ x\u22650 Nn\nx>y\u2228x\u2264y (sN {m} Nm) zN = inj\u2081 $ <-0S m\nx>y\u2228x\u2264y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m>n \u2192 inj\u2081 (trans (<-SS n m) m>n))\n , (\u03bb m\u2264n \u2192 inj\u2082 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n ] (x>y\u2228x\u2264y Nm Nn)\n\nxy\u2228x\u2264y Nn Nm\n\nx\u2264y\u2228x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2228 NLE m n\nx\u2264y\u2228x\u2270y zN Nn = inj\u2081 (0\u2264x Nn)\nx\u2264y\u2228x\u2270y (sN Nm) zN = inj\u2082 (S\u22700 Nm)\nx\u2264y\u2228x\u2270y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m\u2264n \u2192 inj\u2081 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n , (\u03bb m\u2270n \u2192 inj\u2082 (x\u2270y\u2192Sx\u2270Sy m n m\u2270n))\n ] (x\u2264y\u2228x\u2270y Nm Nn)\n\nx\u2261y\u2192x\u2264y : \u2200 {m n} {Nm : N m} {Nn : N n} \u2192 m \u2261 n \u2192 LE m n\nx\u2261y\u2192x\u2264y {Nm = Nm} refl = x\u2264x Nm\n\nxy\u2192x-y+y\u2261x : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 (m \u2238 n) + n \u2261 m\nx>y\u2192x-y+y\u2261x zN Nn 0>n = \u22a5-elim $ \u00ac0>x Nn 0>n\nx>y\u2192x-y+y\u2261x (sN {m} Nm) zN Sm>0 = prf\n where\n postulate prf : (succ m \u2238 zero) + zero \u2261 succ m\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx>y\u2192x-y+y\u2261x (sN {m} Nm) (sN {n} Nn) Sm>Sn = prf $ x>y\u2192x-y+y\u2261x Nm Nn m>n\n where\n postulate m>n : GT m n\n {-# ATP prove m>n #-}\n\n postulate prf : (m \u2238 n) + n \u2261 m \u2192 -- IH.\n (succ m \u2238 succ n) + succ n \u2261 succ m\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx\u2264y\u2192y-x+x\u2261y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2192 (n \u2238 m) + m \u2261 n\nx\u2264y\u2192y-x+x\u2261y {n = n} zN Nn 0\u2264n = prf\n where\n postulate prf : (n \u2238 zero) + zero \u2261 n\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx\u2264y\u2192y-x+x\u2261y (sN Nm) zN Sm\u22640 = \u22a5-elim $ \u00acS\u22640 Nm Sm\u22640\n\nx\u2264y\u2192y-x+x\u2261y (sN {m} Nm) (sN {n} Nn) Sm\u2264Sn = prf $ x\u2264y\u2192y-x+x\u2261y Nm Nn m\u2264n\n where\n postulate m\u2264n : LE m n\n {-# ATP prove m\u2264n #-}\n\n postulate prf : (n \u2238 m) + m \u2261 n \u2192 -- IH.\n (succ n \u2238 succ m) + succ m \u2261 succ n\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx0\u2192x-y0\u2192x-y0 = \u22a5-elim $ \u00acx>x zN 0>0\nx\u2265y\u2192y>0\u2192x-y0\u2192x-y0 = prf\n where\n postulate prf : LT (succ m \u2238 succ n) (succ m)\n {-# ATP prove prf x-yx : \u2200 {n} \u2192 N n \u2192 \u00ac (GT zero n)\n{-# ATP prove \u00ac0>x 0\u226fx #-}\n\nx\u2270x : \u2200 {n} \u2192 N n \u2192 NLT n n\nx\u2270x zN = <-00\nx\u2270x (sN {n} Nn) = trans (<-SS n n) (x\u2270x Nn)\n\nS\u22700 : \u2200 {n} \u2192 N n \u2192 NLE (succ n) zero\nS\u22700 zN = x\u2270x (sN zN)\nS\u22700 (sN {n} Nn) = trans (<-SS (succ n) zero) (<-S0 n)\n\n\u00acS\u22640 : \u2200 {n} \u2192 N n \u2192 \u00ac (LE (succ n) zero)\n\u00acS\u22640 {d} Nn Sn\u22640 = true\u2260false $ trans (sym Sn\u22640) (S\u22700 Nn)\n\n\u00ac0\u2265S : \u2200 {n} \u2192 N n \u2192 \u00ac (GE zero (succ n))\n\u00ac0\u2265S Nn 0\u2265Sn = \u00acS\u22640 Nn 0\u2265Sn\n\nxx : \u2200 {n} \u2192 N n \u2192 \u00ac (GT n n)\n\u00acx>x Nn = \u00acxy\u2192yy\u2192yn = \u22a5-elim $ \u00ac0>x Nn 0>n\nx>y\u2192yy\u2192ySn =\n trans (<-SS n m) (x>y\u2192ySn))\n\nx\u2265y\u2192x\u226ey : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GE m n \u2192 NLT m n\nx\u2265y\u2192x\u226ey zN zN _ = x\u2270x zN\nx\u2265y\u2192x\u226ey zN (sN Nn) 0\u2265Sn = \u22a5-elim $ \u00ac0\u2265S Nn 0\u2265Sn\nx\u2265y\u2192x\u226ey (sN {m} Nm) zN _ = <-S0 m\nx\u2265y\u2192x\u226ey (sN {m} Nm) (sN {n} Nn) Sm\u2265Sn =\n trans (<-SS m n) (x\u2265y\u2192x\u226ey Nm Nn (trans (sym $ <-SS n (succ m)) Sm\u2265Sn))\n\nx>y\u2192x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 NLE m n\nx>y\u2192x\u2270y zN Nn 0>m = \u22a5-elim $ \u00ac0>x Nn 0>m\nx>y\u2192x\u2270y (sN Nm) zN _ = S\u22700 Nm\nx>y\u2192x\u2270y (sN {m} Nm) (sN {n} Nn) Sm>Sn =\n x\u2270y\u2192Sx\u2270Sy m n (x>y\u2192x\u2270y Nm Nn (trans (sym $ <-SS n m) Sm>Sn))\n\nx>y\u2228x\u2264y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2228 LE m n\nx>y\u2228x\u2264y zN Nn = inj\u2082 $ x\u22650 Nn\nx>y\u2228x\u2264y (sN {m} Nm) zN = inj\u2081 $ <-0S m\nx>y\u2228x\u2264y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m>n \u2192 inj\u2081 (trans (<-SS n m) m>n))\n , (\u03bb m\u2264n \u2192 inj\u2082 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n ] (x>y\u2228x\u2264y Nm Nn)\n\nxy\u2228x\u2264y Nn Nm\n\nx\u2264y\u2228x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2228 NLE m n\nx\u2264y\u2228x\u2270y zN Nn = inj\u2081 (0\u2264x Nn)\nx\u2264y\u2228x\u2270y (sN Nm) zN = inj\u2082 (S\u22700 Nm)\nx\u2264y\u2228x\u2270y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m\u2264n \u2192 inj\u2081 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n , (\u03bb m\u2270n \u2192 inj\u2082 (x\u2270y\u2192Sx\u2270Sy m n m\u2270n))\n ] (x\u2264y\u2228x\u2270y Nm Nn)\n\nx\u2261y\u2192x\u2264y : \u2200 {m n} {Nm : N m} {Nn : N n} \u2192 m \u2261 n \u2192 LE m n\nx\u2261y\u2192x\u2264y {Nm = Nm} refl = x\u2264x Nm\n\nxy\u2192x-y+y\u2261x : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 (m \u2238 n) + n \u2261 m\nx>y\u2192x-y+y\u2261x zN Nn 0>n = \u22a5-elim $ \u00ac0>x Nn 0>n\nx>y\u2192x-y+y\u2261x (sN {m} Nm) zN Sm>0 = prf\n where\n postulate prf : (succ m \u2238 zero) + zero \u2261 succ m\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx>y\u2192x-y+y\u2261x (sN {m} Nm) (sN {n} Nn) Sm>Sn = prf $ x>y\u2192x-y+y\u2261x Nm Nn m>n\n where\n postulate m>n : GT m n\n {-# ATP prove m>n #-}\n\n postulate prf : (m \u2238 n) + n \u2261 m \u2192 -- IH.\n (succ m \u2238 succ n) + succ n \u2261 succ m\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx\u2264y\u2192y-x+x\u2261y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2192 (n \u2238 m) + m \u2261 n\nx\u2264y\u2192y-x+x\u2261y {n = n} zN Nn 0\u2264n = prf\n where\n postulate prf : (n \u2238 zero) + zero \u2261 n\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx\u2264y\u2192y-x+x\u2261y (sN Nm) zN Sm\u22640 = \u22a5-elim $ \u00acS\u22640 Nm Sm\u22640\n\nx\u2264y\u2192y-x+x\u2261y (sN {m} Nm) (sN {n} Nn) Sm\u2264Sn = prf $ x\u2264y\u2192y-x+x\u2261y Nm Nn m\u2264n\n where\n postulate m\u2264n : LE m n\n {-# ATP prove m\u2264n #-}\n\n postulate prf : (n \u2238 m) + m \u2261 n \u2192 -- IH.\n (succ n \u2238 succ m) + succ m \u2261 succ n\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx0\u2192x-y0\u2192x-y0 = \u22a5-elim $ \u00acx>x zN 0>0\nx\u2265y\u2192y>0\u2192x-y0\u2192x-y0 = prf\n where\n postulate prf : LT (succ m \u2238 succ n) (succ m)\n {-# ATP prove prf x-y \u00aca \u00acb c = \ud835\udfd8-elim (p (\u2264-pred c))\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (\u2264-steps 1 1+n\u2264m)\n\nnot<=\u2192< : \u2200 x y \u2192 \u2713 (not (x <= y)) \u2192 \u2713 (suc y <= x)\nnot<=\u2192< x y p = <=.complete (\u2270\u2192< x y (\u2713-not-\u00ac p \u2218 <=.complete))\n\neven? odd? : \u2115 \u2192 \ud835\udfda\neven? zero = 1\u2082\neven? (suc n) = odd? n \nodd? n = not (even? n)\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule Data.Nat.NP where\n\nopen import Type hiding (\u2605)\nimport Algebra\nopen import Algebra.FunctionProperties.Eq\nopen import Data.Two.Base hiding (_==_; _\u00b2)\nopen import Data.Product using (\u2203; _,_) renaming (proj\u2081 to fst; proj\u2082 to snd)\nopen import Data.Sum renaming (map to \u228e-map)\nopen import Data.Zero using (\ud835\udfd8-elim; \ud835\udfd8)\nopen import Data.One using (\ud835\udfd9)\nopen import Function.NP\nopen import Function.Extensionality\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_; _\u2257_; module \u2261-Reasoning; !_; _\u2219_; ap; ap\u2082; coe)\n renaming (refl to idp)\nopen import HoTT\nopen Equivalences\n\nopen import Data.Nat public hiding (module GeneralisedArithmetic; module \u2264-Reasoning; fold)\nopen import Data.Nat.Properties\n\npattern 1+_ x = suc x\npattern 2+_ x = 1+ suc x\npattern 3+_ x = 2+ suc x\npattern 4+_ x = 3+ suc x\n\n\u27e80\u21941\u27e9 : \u2115 \u2192 \u2115\n\u27e80\u21941\u27e9 0 = 1\n\u27e80\u21941\u27e9 1 = 0\n\u27e80\u21941\u27e9 n = n\n\nprivate\n _\u00b2 : \u2200 {A : \u2605\u2080} \u2192 Endo (Endo A)\n f \u00b2 = f \u2218 f\n\n\u27e80\u21941\u27e9-involutive : \u27e80\u21941\u27e9 \u2218 \u27e80\u21941\u27e9 \u2257 id\n\u27e80\u21941\u27e9-involutive 0 = idp\n\u27e80\u21941\u27e9-involutive 1 = idp\n\u27e80\u21941\u27e9-involutive (suc (suc _)) = idp\n\n\u21d1\u27e8_\u27e9 : (\u2115 \u2192 \u2115) \u2192 (\u2115 \u2192 \u2115)\n\u21d1\u27e8 f \u27e9 zero = zero\n\u21d1\u27e8 f \u27e9 (suc n) = suc (f n)\n\n\u27e80\u21941+_\u27e9 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e80\u21941+ 0 \u27e9 = \u27e80\u21941\u27e9\n\u27e80\u21941+ suc n \u27e9 = \u27e80\u21941\u27e9 \u2218 \u21d1\u27e8 \u27e80\u21941+ n \u27e9 \u27e9 \u2218 \u27e80\u21941\u27e9\n\n\u27e8_\u2194+1\u27e9 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e8 0 \u2194+1\u27e9 = \u27e80\u21941\u27e9\n\u27e8 suc n \u2194+1\u27e9 0 = 0\n\u27e8 suc n \u2194+1\u27e9 (suc m) = suc (\u27e8 n \u2194+1\u27e9 m)\n\n\u27e8_\u2194+1\u27e9-involutive : \u2200 n \u2192 \u27e8 n \u2194+1\u27e9 \u2218 \u27e8 n \u2194+1\u27e9 \u2257 id\n\u27e8_\u2194+1\u27e9-involutive 0 = \u27e80\u21941\u27e9-involutive\n\u27e8_\u2194+1\u27e9-involutive (suc _) 0 = idp\n\u27e8_\u2194+1\u27e9-involutive (suc n) (suc m) = ap suc (\u27e8 n \u2194+1\u27e9-involutive m)\n\n\u27e8_\u2194+1\u27e9-equiv : \u2115 \u2192 \u2115 \u2243 \u2115\n\u27e8 n \u2194+1\u27e9-equiv = self-inv-equiv \u27e8 n \u2194+1\u27e9 \u27e8 n \u2194+1\u27e9-involutive\n\n\u21d1\u27e8_\u27e9-involutive : \u2200 {f} \u2192 f \u00b2 \u2257 id \u2192 \u21d1\u27e8 f \u27e9 \u00b2 \u2257 id\n\u21d1\u27e8 f\u00b2id \u27e9-involutive zero = idp\n\u21d1\u27e8 f\u00b2id \u27e9-involutive (suc x) = ap suc (f\u00b2id x)\n\n\u27e80\u21941+_\u27e9-involutive : \u2200 n \u2192 \u27e80\u21941+ n \u27e9 \u00b2 \u2257 id\n\u27e80\u21941+_\u27e9-involutive zero = \u27e80\u21941\u27e9-involutive\n\u27e80\u21941+_\u27e9-involutive (suc n) x = ap (\u27e80\u21941\u27e9 \u2218 \u21d1\u27e8 \u27e80\u21941+ n \u27e9 \u27e9) (\u27e80\u21941\u27e9-involutive (\u21d1\u27e8 \u27e80\u21941+ n \u27e9 \u27e9 (\u27e80\u21941\u27e9 x)))\n \u2219 ap \u27e80\u21941\u27e9 (\u21d1\u27e8 \u27e80\u21941+ n \u27e9-involutive \u27e9-involutive (\u27e80\u21941\u27e9 x)) \u2219 \u27e80\u21941\u27e9-involutive x\n\nmodule _ {{_ : UA}} where\n \u27e8_\u2194+1\u27e9-eq : \u2115 \u2192 \u2115 \u2261 \u2115\n \u27e8_\u2194+1\u27e9-eq = ua \u2218 \u27e8_\u2194+1\u27e9-equiv\n\n \u27e8_\u2194+1\u27e9-eq-\u03b2 : \u2200 n m \u2192 coe \u27e8 n \u2194+1\u27e9-eq m \u2261 \u27e8 n \u2194+1\u27e9 m\n \u27e8_\u2194+1\u27e9-eq-\u03b2 = coe-\u03b2 \u2218 \u27e8_\u2194+1\u27e9-equiv\n\n\u2115\u02e2 = \u2261.setoid \u2115\n\nmodule \u2115cmp = StrictTotalOrder strictTotalOrder\nmodule \u2115\u2264 = DecTotalOrder decTotalOrder\nmodule \u2115\u00b0 = Algebra.CommutativeSemiring commutativeSemiring\nmodule \u2115+ = Algebra.CommutativeMonoid \u2115\u00b0.+-commutativeMonoid\nmodule \u2115+\u2032 = Algebra.Monoid \u2115\u00b0.+-monoid\nmodule \u2294\u00b0 = Algebra.CommutativeSemiringWithoutOne \u2294-\u2293-0-commutativeSemiringWithoutOne\nmodule \u2115\u02e2 = Setoid \u2115\u02e2\n\ninfixr 8 _\u2219\u2264_\n_\u2219\u2264_ = \u2115\u2264.trans\n_\u2219cmp_ = \u2115cmp.trans\n_\u2219<_ = <-trans\n\n[P:_zero:_suc:_] : \u2200 {p} (P : \u2115 \u2192 \u2605 p) \u2192 P zero \u2192 (\u2200 {n} \u2192 P n \u2192 P (suc n)) \u2192 \u2200 n \u2192 P n\n[P: _ zero: z suc: _ ] zero = z\n[P: P zero: z suc: s ] (suc n) = s ([P: P zero: z suc: s ] n)\n\n[zero:_suc:_] : \u2200 {a} {A : \u2605 a} \u2192 A \u2192 (\u2115 \u2192 A \u2192 A) \u2192 \u2115 \u2192 A\n[zero: z suc: s ] = [P: _ zero: z suc: (\u03bb {n} \u2192 s n) ]\n\nmodule \u2264-Reasoning where\n open Preorder-Reasoning \u2115\u2264.preorder public renaming (_\u223c\u27e8_\u27e9_ to _\u2264\u27e8_\u27e9_)\n infixr 2 _\u2261\u27e8_\u27e9_\n _\u2261\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x \u2261 y \u2192 y \u2264 z \u2192 x \u2264 z\n _ \u2261\u27e8 idp \u27e9 p = p\n infixr 2 _<\u27e8_\u27e9_\n _<\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x < y \u2192 y \u2264 z \u2192 x < z\n x <\u27e8 p \u27e9 q = suc x \u2264\u27e8 p \u27e9 q\n\nsuc-injective : \u2200 {n m : \u2115} \u2192 \u2115.suc n \u2261 suc m \u2192 n \u2261 m\nsuc-injective = ap pred\n\n+-\u2264-inj : \u2200 x {y z} \u2192 x + y \u2264 x + z \u2192 y \u2264 z\n+-\u2264-inj zero = id\n+-\u2264-inj (suc x) = +-\u2264-inj x \u2218 \u2264-pred\n\ninfixl 6 _+\u00b0_\ninfixl 7 _*\u00b0_ _\u2293\u00b0_\ninfixl 6 _\u2238\u00b0_ _\u2294\u00b0_\n\n_+\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 \u2115) \u2192 A \u2192 \u2115\n(f +\u00b0 g) x = f x + g x\n\n_\u2238\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 \u2115) \u2192 A \u2192 \u2115\n(f \u2238\u00b0 g) x = f x \u2238 g x\n\n_*\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 \u2115) \u2192 A \u2192 \u2115\n(f *\u00b0 g) x = f x * g x\n\n_\u2294\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 \u2115) \u2192 A \u2192 \u2115\n(f \u2294\u00b0 g) x = f x \u2294 g x\n\n_\u2293\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 \u2115) \u2192 A \u2192 \u2115\n(f \u2293\u00b0 g) x = f x \u2293 g x\n\n-- this one is not completly in line with the\n-- others\n_\u2264\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 \u2115) \u2192 \u2605 a\nf \u2264\u00b0 g = \u2200 x \u2192 f x \u2264 g x\n\nsucx\u2270x : \u2200 x \u2192 suc x \u2270 x\nsucx\u2270x zero = \u03bb()\nsucx\u2270x (suc x) = sucx\u2270x x \u2218 \u2264-pred\n\ntotal-\u2264 : \u2200 a b \u2192 a \u2264 b \u228e b \u2264 a\ntotal-\u2264 zero b = inj\u2081 z\u2264n\ntotal-\u2264 (suc a) zero = inj\u2082 z\u2264n\ntotal-\u2264 (suc a) (suc b) with total-\u2264 a b\n... | inj\u2081 p = inj\u2081 (s\u2264s p)\n... | inj\u2082 p = inj\u2082 (s\u2264s p)\n\na\u2261a\u2293b+a\u2238b : \u2200 a b \u2192 a \u2261 a \u2293 b + (a \u2238 b)\na\u2261a\u2293b+a\u2238b zero zero = idp\na\u2261a\u2293b+a\u2238b zero (suc b) = idp\na\u2261a\u2293b+a\u2238b (suc a) zero = idp\na\u2261a\u2293b+a\u2238b (suc a) (suc b) rewrite ! a\u2261a\u2293b+a\u2238b a b = idp\n\n\u00acn\u2264x \u00aca \u00acb c = \ud835\udfd8-elim (p (\u2264-pred c))\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (\u2264-steps 1 1+n\u2264m)\n\nnot<=\u2192< : \u2200 x y \u2192 \u2713 (not (x <= y)) \u2192 \u2713 (suc y <= x)\nnot<=\u2192< x y p = <=.complete (\u2270\u2192< x y (\u2713-not-\u00ac p \u2218 <=.complete))\n\neven? odd? : \u2115 \u2192 \ud835\udfda\neven? zero = 1\u2082\neven? (suc n) = odd? n \nodd? n = not (even? n)\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"f5f267e3952095f52394a2377e2f10618505d866","subject":"Added Agsy examples.","message":"Added Agsy examples.\n\nIgnore-this: 6a5ebd515088c189d2d71d42872cd0e8\n\ndarcs-hash:20101203172202-3bd4e-06ca65a535ff142673537ff8af3ac4820edad9fb.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/Agsy\/AgsyTest.agda","new_file":"Draft\/Agsy\/AgsyTest.agda","new_contents":"------------------------------------------------------------------------------\n-- Testing Agsy using the Agda standard library\n------------------------------------------------------------------------------\n\nmodule AgsyTest where\n\nopen import Data.Nat\n\nopen import Relation.Binary.PropositionalEquality\nopen \u2261-Reasoning\n\n------------------------------------------------------------------------------\n\n+-assoc : \u2200 m n o \u2192 m + n + o \u2261 m + (n + o) -- via Agsy {-c}\n+-assoc zero n o = refl\n+-assoc (suc m) n o = cong suc (+-assoc m n o)\n\nx+1+y\u22611+x+y : \u2200 m n \u2192 m + suc n \u2261 suc (m + n) -- via Agsy {-c}\nx+1+y\u22611+x+y zero n = refl\nx+1+y\u22611+x+y (suc m) n = cong suc (x+1+y\u22611+x+y m n)\n\n0+n\u2261n+0 : \u2200 n \u2192 0 + n \u2261 n + 0 -- via Agsy {-c}\n0+n\u2261n+0 zero = refl\n0+n\u2261n+0 (suc n) = cong suc (0+n\u2261n+0 n)\n\n+-comm : \u2200 m n \u2192 m + n \u2261 n + m -- via Agsy {-c -m}\n+-comm zero n = 0+n\u2261n+0 n\n+-comm (suc m) n =\n begin\n suc (m + n) \u2261\u27e8 cong suc (+-comm m n) \u27e9\n suc (n + m) \u2261\u27e8 sym (x+1+y\u22611+x+y n m) \u27e9\n n + suc m\n \u220e\n","old_contents":"------------------------------------------------------------------------------\n-- Testing Agsy using the Agda standard library\n------------------------------------------------------------------------------\n\nmodule AgsyTest where\n\nopen import Data.Nat\n\nopen import Relation.Binary.PropositionalEquality\nopen \u2261-Reasoning\n\n------------------------------------------------------------------------------\n\n0+m\u2261m+0 : \u2200 m \u2192 0 + m \u2261 m + 0 -- via Agsy {-c}\n0+m\u2261m+0 zero = refl\n0+m\u2261m+0 (suc n) = cong suc (0+m\u2261m+0 n)\n\n+-comm : \u2200 m n \u2192 m + n \u2261 n + m\n+-comm zero zero = refl -- via Agsy\n+-comm zero (suc n) = sym (cong suc (+-comm n zero)) -- via Agsy\n+-comm (suc m) zero = sym (cong suc (+-comm zero m)) -- via Agsy\n+-comm (suc m) (suc n) = {!-t 20!} -- Agsy: No solution found at time out (20s)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f7a0824e23aa553c512f0fbff1879941ee29f6d0","subject":"JS: add is-null","message":"JS: add is-null\n","repos":"crypto-agda\/agda-libjs","old_file":"lib\/FFI\/JS.agda","new_file":"lib\/FFI\/JS.agda","new_contents":"module FFI.JS where\n\nopen import Data.Empty public renaming (\u22a5 to \ud835\udfd8)\nopen import Data.Unit.Base public renaming (\u22a4 to \ud835\udfd9)\nopen import Data.Char.Base public using (Char)\nopen import Data.String.Base public using (String)\nopen import Data.Bool.Base public using (Bool; true; false)\nopen import Data.List.Base using (List; []; _\u2237_)\nopen import Data.Product using (_\u00d7_) renaming (proj\u2081 to fst; proj\u2082 to snd)\nopen import Function using (id; _\u2218_)\n\nopen import Control.Process.Type\n\n{-# COMPILED_JS Bool function (x,v) { return ((x)? v[\"true\"]() : v[\"false\"]()); } #-}\n{-# COMPILED_JS true true #-}\n{-# COMPILED_JS false false #-}\n\npostulate\n Number : Set\n JSArray : Set \u2192 Set\n JSObject : Set\n JSValue : Set\n\npostulate readNumber : String \u2192 Number\n{-# COMPILED_JS readNumber Number #-}\n\npostulate 0N : Number\n{-# COMPILED_JS 0N 0 #-}\n\npostulate 1N : Number\n{-# COMPILED_JS 1N 1 #-}\n\npostulate 2N : Number\n{-# COMPILED_JS 2N 2 #-}\n\npostulate 4N : Number\n{-# COMPILED_JS 4N 4 #-}\n\npostulate 8N : Number\n{-# COMPILED_JS 8N 8 #-}\n\npostulate 16N : Number\n{-# COMPILED_JS 16N 16 #-}\n\npostulate 32N : Number\n{-# COMPILED_JS 32N 32 #-}\n\npostulate 64N : Number\n{-# COMPILED_JS 64N 64 #-}\n\npostulate 128N : Number\n{-# COMPILED_JS 128N 128 #-}\n\npostulate 256N : Number\n{-# COMPILED_JS 256N 256 #-}\n\npostulate 512N : Number\n{-# COMPILED_JS 512N 512 #-}\n\npostulate 1024N : Number\n{-# COMPILED_JS 1024N 1024 #-}\n\npostulate 2048N : Number\n{-# COMPILED_JS 2048N 2048 #-}\n\npostulate 4096N : Number\n{-# COMPILED_JS 4096N 4096 #-}\n\npostulate _+_ : Number \u2192 Number \u2192 Number\n{-# COMPILED_JS _+_ function(x) { return function(y) { return x + y; }; } #-}\n\npostulate _\u2212_ : Number \u2192 Number \u2192 Number\n{-# COMPILED_JS _\u2212_ function(x) { return function(y) { return x - y; }; } #-}\n\npostulate _*_ : Number \u2192 Number \u2192 Number\n{-# COMPILED_JS _*_ function(x) { return function(y) { return x * y; }; } #-}\n\npostulate _\/_ : Number \u2192 Number \u2192 Number\n{-# COMPILED_JS _\/_ function(x) { return function(y) { return x \/ y; }; } #-}\n\ninfixr 5 _++_\npostulate _++_ : String \u2192 String \u2192 String\n{-# COMPILED_JS _++_ function(x) { return function(y) { return x + y; }; } #-}\n\npostulate _+JS_ : JSValue \u2192 JSValue \u2192 JSValue\n{-# COMPILED_JS _+JS_ function(x) { return function(y) { return x + y; }; } #-}\n\npostulate _\u2264JS_ : JSValue \u2192 JSValue \u2192 Bool\n{-# COMPILED_JS _\u2264JS_ function(x) { return function(y) { return x <= y; }; } #-}\n\npostulate _JS_ : JSValue \u2192 JSValue \u2192 Bool\n{-# COMPILED_JS _>JS_ function(x) { return function(y) { return x > y; }; } #-}\n\npostulate _\u2265JS_ : JSValue \u2192 JSValue \u2192 Bool\n{-# COMPILED_JS _\u2265JS_ function(x) { return function(y) { return x >= y; }; } #-}\n\npostulate _===_ : JSValue \u2192 JSValue \u2192 Bool\n{-# COMPILED_JS _===_ function(x) { return function(y) { return x === y; }; } #-}\n\npostulate reverse : {A : Set} \u2192 JSArray A \u2192 JSArray A\n{-# COMPILED_JS reverse function(ty) { return function(x) { return x.reverse(); }; } #-}\n\npostulate sort : {A : Set} \u2192 JSArray A \u2192 JSArray A\n{-# COMPILED_JS sort function(ty) { return function(x) { return x.sort(); }; } #-}\n\npostulate split : (sep target : String) \u2192 JSArray String\n{-# COMPILED_JS split function(sep) { return function(target) { return target.split(sep); }; } #-}\n\npostulate join : (sep : String)(target : JSArray String) \u2192 String\n{-# COMPILED_JS join function(sep) { return function(target) { return target.join(sep); }; } #-}\n\npostulate fromList : {A B : Set}(xs : List A)(fromElt : A \u2192 B) \u2192 JSArray B\n{-# COMPILED_JS fromList require(\"libagda\").fromList #-}\n\npostulate length : String \u2192 Number\n{-# COMPILED_JS length function(s) { return s.length; } #-}\n\npostulate JSON-stringify : JSValue \u2192 String\n{-# COMPILED_JS JSON-stringify JSON.stringify #-}\n\npostulate JSON-parse : String \u2192 JSValue\n{-# COMPILED_JS JSON-parse JSON.parse #-}\n\npostulate toString : JSValue \u2192 String\n{-# COMPILED_JS toString function(x) { return x.toString(); } #-}\n\npostulate fromBool : Bool \u2192 JSValue\n{-# COMPILED_JS fromBool function(x) { return x; } #-}\n\npostulate fromString : String \u2192 JSValue\n{-# COMPILED_JS fromString function(x) { return x; } #-}\n\npostulate fromChar : Char \u2192 JSValue\n{-# COMPILED_JS fromChar String #-}\n\npostulate Char\u25b9String : Char \u2192 String\n{-# COMPILED_JS Char\u25b9String String #-}\n\npostulate fromNumber : Number \u2192 JSValue\n{-# COMPILED_JS fromNumber function(x) { return x; } #-}\n\npostulate fromJSArray : {A : Set} \u2192 JSArray A \u2192 JSValue\n{-# COMPILED_JS fromJSArray function(ty) { return function(x) { return x; }; } #-}\n\npostulate fromJSObject : JSObject \u2192 JSValue\n{-# COMPILED_JS fromJSObject function(x) { return x; } #-}\n\npostulate objectFromList : {A : Set}(xs : List A)(fromKey : A \u2192 String)(fromVal : A \u2192 JSValue) \u2192 JSObject\n{-# COMPILED_JS objectFromList require(\"libagda\").objectFromList #-}\n\npostulate decodeJSArray : {A B : Set}(arr : JSArray A)(fromElt : Number \u2192 A \u2192 B) \u2192 List B\n{-# COMPILED_JS decodeJSArray require(\"libagda\").decodeJSArray #-}\n\npostulate castNumber : JSValue \u2192 Number\n{-# COMPILED_JS castNumber Number #-}\n\npostulate castString : JSValue \u2192 String\n{-# COMPILED_JS castString String #-}\n\n-- TODO dyn check of length 1?\npostulate castChar : JSValue \u2192 Char\n{-# COMPILED_JS castChar String #-}\n\n-- TODO dyn check of length 1?\npostulate String\u25b9Char : String \u2192 Char\n{-# COMPILED_JS String\u25b9Char String #-}\n\n-- TODO dyn check?\npostulate castJSArray : JSValue \u2192 JSArray JSValue\n{-# COMPILED_JS castJSArray function(x) { return x; } #-}\n\n-- TODO dyn check?\npostulate castJSObject : JSValue \u2192 JSObject\n{-# COMPILED_JS castJSObject function(x) { return x; } #-}\n\npostulate nullJS : JSValue\n{-# COMPILED_JS nullJS null #-}\n\npostulate _\u00b7[_] : JSValue \u2192 JSValue \u2192 JSValue\n{-# COMPILED_JS _\u00b7[_] require(\"libagda\").readProp #-}\n\npostulate _Array[_] : {A : Set} \u2192 JSArray A \u2192 Number \u2192 A\n{-# COMPILED_JS _Array[_] function(ty) { return require(\"libagda\").readProp; } #-}\n\npostulate onJSArray : {A : Set} (f : JSArray JSValue \u2192 A) \u2192 JSValue \u2192 A\n{-# COMPILED_JS onJSArray require(\"libagda\").onJSArray #-}\n\npostulate onString : {A : Set} (f : String \u2192 A) \u2192 JSValue \u2192 A\n{-# COMPILED_JS onString require(\"libagda\").onString #-}\n\n-- Writes 'msg' and 'inp' to the console and then returns `f inp`\npostulate trace : {A B : Set}(msg : String)(inp : A)(f : A \u2192 B) \u2192 B\n{-# COMPILED_JS trace require(\"libagda\").trace #-}\n\npostulate throw : {A : Set} \u2192 String \u2192 A \u2192 A\n{-# COMPILED_JS throw require(\"libagda\").throw #-}\n\npostulate is-null : JSValue \u2192 Bool\n{-# COMPILED_JS is-null function(x) { return (x == null); } #-}\n\ndata Value : Set\u2080 where\n array : List Value \u2192 Value\n object : List (String \u00d7 Value) \u2192 Value\n string : String \u2192 Value\n number : Number \u2192 Value\n bool : Bool \u2192 Value\n null : Value\n\nObject = List (String \u00d7 JSValue)\n\npostulate fromValue : Value \u2192 JSValue\n{-# COMPILED_JS fromValue require(\"libagda\").fromValue #-}\n\n-- TODO we could make it a COMPILED type and remove the encoding by using JSValue as the internal repr.\ndata ValueView : Set\u2080 where\n array : JSArray JSValue \u2192 ValueView\n object : JSObject \u2192 ValueView\n string : String \u2192 ValueView\n number : Number \u2192 ValueView\n bool : Bool \u2192 ValueView\n null : ValueView\n\n-- TODO not yet tested\npostulate viewJSValue : JSValue \u2192 ValueView\n{-# COMPILED_JS viewJSValue require(\"libagda\").viewJSValue #-}\n\nBool\u25b9String : Bool \u2192 String\nBool\u25b9String true = \"true\"\nBool\u25b9String false = \"false\"\n\nList\u25b9String : List Char \u2192 String\nList\u25b9String xs = join \"\" (fromList xs Char\u25b9String)\n\nString\u25b9List : String \u2192 List Char\nString\u25b9List s = decodeJSArray (split \"\" s) (\u03bb _ \u2192 String\u25b9Char)\n\nNumber\u25b9String : Number \u2192 String\nNumber\u25b9String = castString \u2218 fromNumber\n\nJSArray\u25b9ListString : {A : Set} \u2192 JSArray A \u2192 List A\nJSArray\u25b9ListString a = decodeJSArray a (\u03bb _ \u2192 id)\n\nfromObject : Object \u2192 JSObject\nfromObject o = objectFromList o fst snd\n\n_\u2264Char_ : Char \u2192 Char \u2192 Bool\nx \u2264Char y = fromChar x \u2264JS fromChar y\n\n_Char_ : Char \u2192 Char \u2192 Bool\nx >Char y = fromChar x >JS fromChar y\n\n_\u2265Char_ : Char \u2192 Char \u2192 Bool\nx \u2265Char y = fromChar x \u2265JS fromChar y\n\n_\u2264String_ : String \u2192 String \u2192 Bool\nx \u2264String y = fromString x \u2264JS fromString y\n\n_String_ : String \u2192 String \u2192 Bool\nx >String y = fromString x >JS fromString y\n\n_\u2265String_ : String \u2192 String \u2192 Bool\nx \u2265String y = fromString x \u2265JS fromString y\n\n_\u2264Number_ : Number \u2192 Number \u2192 Bool\nx \u2264Number y = fromNumber x \u2264JS fromNumber y\n\n_Number_ : Number \u2192 Number \u2192 Bool\nx >Number y = fromNumber x >JS fromNumber y\n\n_\u2265Number_ : Number \u2192 Number \u2192 Bool\nx \u2265Number y = fromNumber x \u2265JS fromNumber y\n\n_\u00b7\u00ab_\u00bb : JSValue \u2192 String \u2192 JSValue\nv \u00b7\u00ab s \u00bb = v \u00b7[ fromString s ]\n\n_\u00b7\u00ab_\u00bbA : JSValue \u2192 String \u2192 JSArray JSValue\nv \u00b7\u00ab s \u00bbA = castJSArray (v \u00b7\u00ab s \u00bb)\n\ntrace-call : {A B : Set} \u2192 String \u2192 (A \u2192 B) \u2192 A \u2192 B\ntrace-call s f x = trace s (f x) id\n\npostulate JSCmd : Set \u2192 Set\n\nCallback1 : Set \u2192 Set\nCallback1 A = JSCmd ((A \u2192 \ud835\udfd8) \u2192 \ud835\udfd8)\n\nCallback0 : Set\nCallback0 = Callback1 \ud835\udfd9\n\nCallback2 : Set \u2192 Set \u2192 Set\nCallback2 A B = JSCmd ((A \u2192 B \u2192 \ud835\udfd8) \u2192 \ud835\udfd8)\n\npostulate assert : Bool \u2192 Callback0\n{-# COMPILED_JS assert require(\"libagda\").assert #-}\n\ncheck : {A : Set}(pred : Bool)(errmsg : \ud835\udfd9 \u2192 String)(input : A) \u2192 A\ncheck true errmsg x = x\ncheck false errmsg x = throw (errmsg _) x\n\nwarn-check : {A : Set}(pred : Bool)(errmsg : \ud835\udfd9 \u2192 String)(input : A) \u2192 A\nwarn-check true errmsg x = x\nwarn-check false errmsg x = trace (\"Warning: \" ++ errmsg _) x id\n\ninfixr 0 _>>_ _!\u2081_ _!\u2082_\ndata JS! : Set\u2081 where\n end : JS!\n _!\u2081_ : {A : Set}(cmd : Callback1 A)(cb : A \u2192 JS!) \u2192 JS!\n _!\u2082_ : {A B : Set}(cmd : JSCmd ((A \u2192 B \u2192 \ud835\udfd8) \u2192 \ud835\udfd8))(cb : A \u2192 B \u2192 JS!) \u2192 JS!\n\n_>>_ : Callback0 \u2192 JS! \u2192 JS!\ncmd >> cont = cmd !\u2081 \u03bb _ \u2192 cont\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"module FFI.JS where\n\nopen import Data.Empty public renaming (\u22a5 to \ud835\udfd8)\nopen import Data.Unit.Base public renaming (\u22a4 to \ud835\udfd9)\nopen import Data.Char.Base public using (Char)\nopen import Data.String.Base public using (String)\nopen import Data.Bool.Base public using (Bool; true; false)\nopen import Data.List.Base using (List; []; _\u2237_)\nopen import Data.Product using (_\u00d7_) renaming (proj\u2081 to fst; proj\u2082 to snd)\nopen import Function using (id; _\u2218_)\n\nopen import Control.Process.Type\n\n{-# COMPILED_JS Bool function (x,v) { return ((x)? v[\"true\"]() : v[\"false\"]()); } #-}\n{-# COMPILED_JS true true #-}\n{-# COMPILED_JS false false #-}\n\npostulate\n Number : Set\n JSArray : Set \u2192 Set\n JSObject : Set\n JSValue : Set\n\npostulate readNumber : String \u2192 Number\n{-# COMPILED_JS readNumber Number #-}\n\npostulate 0N : Number\n{-# COMPILED_JS 0N 0 #-}\n\npostulate 1N : Number\n{-# COMPILED_JS 1N 1 #-}\n\npostulate 2N : Number\n{-# COMPILED_JS 2N 2 #-}\n\npostulate 4N : Number\n{-# COMPILED_JS 4N 4 #-}\n\npostulate 8N : Number\n{-# COMPILED_JS 8N 8 #-}\n\npostulate 16N : Number\n{-# COMPILED_JS 16N 16 #-}\n\npostulate 32N : Number\n{-# COMPILED_JS 32N 32 #-}\n\npostulate 64N : Number\n{-# COMPILED_JS 64N 64 #-}\n\npostulate 128N : Number\n{-# COMPILED_JS 128N 128 #-}\n\npostulate 256N : Number\n{-# COMPILED_JS 256N 256 #-}\n\npostulate 512N : Number\n{-# COMPILED_JS 512N 512 #-}\n\npostulate 1024N : Number\n{-# COMPILED_JS 1024N 1024 #-}\n\npostulate 2048N : Number\n{-# COMPILED_JS 2048N 2048 #-}\n\npostulate 4096N : Number\n{-# COMPILED_JS 4096N 4096 #-}\n\npostulate _+_ : Number \u2192 Number \u2192 Number\n{-# COMPILED_JS _+_ function(x) { return function(y) { return x + y; }; } #-}\n\npostulate _\u2212_ : Number \u2192 Number \u2192 Number\n{-# COMPILED_JS _\u2212_ function(x) { return function(y) { return x - y; }; } #-}\n\npostulate _*_ : Number \u2192 Number \u2192 Number\n{-# COMPILED_JS _*_ function(x) { return function(y) { return x * y; }; } #-}\n\npostulate _\/_ : Number \u2192 Number \u2192 Number\n{-# COMPILED_JS _\/_ function(x) { return function(y) { return x \/ y; }; } #-}\n\ninfixr 5 _++_\npostulate _++_ : String \u2192 String \u2192 String\n{-# COMPILED_JS _++_ function(x) { return function(y) { return x + y; }; } #-}\n\npostulate _+JS_ : JSValue \u2192 JSValue \u2192 JSValue\n{-# COMPILED_JS _+JS_ function(x) { return function(y) { return x + y; }; } #-}\n\npostulate _\u2264JS_ : JSValue \u2192 JSValue \u2192 Bool\n{-# COMPILED_JS _\u2264JS_ function(x) { return function(y) { return x <= y; }; } #-}\n\npostulate _JS_ : JSValue \u2192 JSValue \u2192 Bool\n{-# COMPILED_JS _>JS_ function(x) { return function(y) { return x > y; }; } #-}\n\npostulate _\u2265JS_ : JSValue \u2192 JSValue \u2192 Bool\n{-# COMPILED_JS _\u2265JS_ function(x) { return function(y) { return x >= y; }; } #-}\n\npostulate _===_ : JSValue \u2192 JSValue \u2192 Bool\n{-# COMPILED_JS _===_ function(x) { return function(y) { return x === y; }; } #-}\n\npostulate reverse : {A : Set} \u2192 JSArray A \u2192 JSArray A\n{-# COMPILED_JS reverse function(ty) { return function(x) { return x.reverse(); }; } #-}\n\npostulate sort : {A : Set} \u2192 JSArray A \u2192 JSArray A\n{-# COMPILED_JS sort function(ty) { return function(x) { return x.sort(); }; } #-}\n\npostulate split : (sep target : String) \u2192 JSArray String\n{-# COMPILED_JS split function(sep) { return function(target) { return target.split(sep); }; } #-}\n\npostulate join : (sep : String)(target : JSArray String) \u2192 String\n{-# COMPILED_JS join function(sep) { return function(target) { return target.join(sep); }; } #-}\n\npostulate fromList : {A B : Set}(xs : List A)(fromElt : A \u2192 B) \u2192 JSArray B\n{-# COMPILED_JS fromList require(\"libagda\").fromList #-}\n\npostulate length : String \u2192 Number\n{-# COMPILED_JS length function(s) { return s.length; } #-}\n\npostulate JSON-stringify : JSValue \u2192 String\n{-# COMPILED_JS JSON-stringify JSON.stringify #-}\n\npostulate JSON-parse : String \u2192 JSValue\n{-# COMPILED_JS JSON-parse JSON.parse #-}\n\npostulate toString : JSValue \u2192 String\n{-# COMPILED_JS toString function(x) { return x.toString(); } #-}\n\npostulate fromBool : Bool \u2192 JSValue\n{-# COMPILED_JS fromBool function(x) { return x; } #-}\n\npostulate fromString : String \u2192 JSValue\n{-# COMPILED_JS fromString function(x) { return x; } #-}\n\npostulate fromChar : Char \u2192 JSValue\n{-# COMPILED_JS fromChar String #-}\n\npostulate Char\u25b9String : Char \u2192 String\n{-# COMPILED_JS Char\u25b9String String #-}\n\npostulate fromNumber : Number \u2192 JSValue\n{-# COMPILED_JS fromNumber function(x) { return x; } #-}\n\npostulate fromJSArray : {A : Set} \u2192 JSArray A \u2192 JSValue\n{-# COMPILED_JS fromJSArray function(ty) { return function(x) { return x; }; } #-}\n\npostulate fromJSObject : JSObject \u2192 JSValue\n{-# COMPILED_JS fromJSObject function(x) { return x; } #-}\n\npostulate objectFromList : {A : Set}(xs : List A)(fromKey : A \u2192 String)(fromVal : A \u2192 JSValue) \u2192 JSObject\n{-# COMPILED_JS objectFromList require(\"libagda\").objectFromList #-}\n\npostulate decodeJSArray : {A B : Set}(arr : JSArray A)(fromElt : Number \u2192 A \u2192 B) \u2192 List B\n{-# COMPILED_JS decodeJSArray require(\"libagda\").decodeJSArray #-}\n\npostulate castNumber : JSValue \u2192 Number\n{-# COMPILED_JS castNumber Number #-}\n\npostulate castString : JSValue \u2192 String\n{-# COMPILED_JS castString String #-}\n\n-- TODO dyn check of length 1?\npostulate castChar : JSValue \u2192 Char\n{-# COMPILED_JS castChar String #-}\n\n-- TODO dyn check of length 1?\npostulate String\u25b9Char : String \u2192 Char\n{-# COMPILED_JS String\u25b9Char String #-}\n\n-- TODO dyn check?\npostulate castJSArray : JSValue \u2192 JSArray JSValue\n{-# COMPILED_JS castJSArray function(x) { return x; } #-}\n\n-- TODO dyn check?\npostulate castJSObject : JSValue \u2192 JSObject\n{-# COMPILED_JS castJSObject function(x) { return x; } #-}\n\npostulate nullJS : JSValue\n{-# COMPILED_JS nullJS null #-}\n\npostulate _\u00b7[_] : JSValue \u2192 JSValue \u2192 JSValue\n{-# COMPILED_JS _\u00b7[_] require(\"libagda\").readProp #-}\n\npostulate _Array[_] : {A : Set} \u2192 JSArray A \u2192 Number \u2192 A\n{-# COMPILED_JS _Array[_] function(ty) { return require(\"libagda\").readProp; } #-}\n\npostulate onJSArray : {A : Set} (f : JSArray JSValue \u2192 A) \u2192 JSValue \u2192 A\n{-# COMPILED_JS onJSArray require(\"libagda\").onJSArray #-}\n\npostulate onString : {A : Set} (f : String \u2192 A) \u2192 JSValue \u2192 A\n{-# COMPILED_JS onString require(\"libagda\").onString #-}\n\n-- Writes 'msg' and 'inp' to the console and then returns `f inp`\npostulate trace : {A B : Set}(msg : String)(inp : A)(f : A \u2192 B) \u2192 B\n{-# COMPILED_JS trace require(\"libagda\").trace #-}\n\npostulate throw : {A : Set} \u2192 String \u2192 A \u2192 A\n{-# COMPILED_JS throw require(\"libagda\").throw #-}\n\ndata Value : Set\u2080 where\n array : List Value \u2192 Value\n object : List (String \u00d7 Value) \u2192 Value\n string : String \u2192 Value\n number : Number \u2192 Value\n bool : Bool \u2192 Value\n null : Value\n\nObject = List (String \u00d7 JSValue)\n\npostulate fromValue : Value \u2192 JSValue\n{-# COMPILED_JS fromValue require(\"libagda\").fromValue #-}\n\n-- TODO we could make it a COMPILED type and remove the encoding by using JSValue as the internal repr.\ndata ValueView : Set\u2080 where\n array : JSArray JSValue \u2192 ValueView\n object : JSObject \u2192 ValueView\n string : String \u2192 ValueView\n number : Number \u2192 ValueView\n bool : Bool \u2192 ValueView\n null : ValueView\n\n-- TODO not yet tested\npostulate viewJSValue : JSValue \u2192 ValueView\n{-# COMPILED_JS viewJSValue require(\"libagda\").viewJSValue #-}\n\nBool\u25b9String : Bool \u2192 String\nBool\u25b9String true = \"true\"\nBool\u25b9String false = \"false\"\n\nList\u25b9String : List Char \u2192 String\nList\u25b9String xs = join \"\" (fromList xs Char\u25b9String)\n\nString\u25b9List : String \u2192 List Char\nString\u25b9List s = decodeJSArray (split \"\" s) (\u03bb _ \u2192 String\u25b9Char)\n\nNumber\u25b9String : Number \u2192 String\nNumber\u25b9String = castString \u2218 fromNumber\n\nJSArray\u25b9ListString : {A : Set} \u2192 JSArray A \u2192 List A\nJSArray\u25b9ListString a = decodeJSArray a (\u03bb _ \u2192 id)\n\nfromObject : Object \u2192 JSObject\nfromObject o = objectFromList o fst snd\n\n_\u2264Char_ : Char \u2192 Char \u2192 Bool\nx \u2264Char y = fromChar x \u2264JS fromChar y\n\n_Char_ : Char \u2192 Char \u2192 Bool\nx >Char y = fromChar x >JS fromChar y\n\n_\u2265Char_ : Char \u2192 Char \u2192 Bool\nx \u2265Char y = fromChar x \u2265JS fromChar y\n\n_\u2264String_ : String \u2192 String \u2192 Bool\nx \u2264String y = fromString x \u2264JS fromString y\n\n_String_ : String \u2192 String \u2192 Bool\nx >String y = fromString x >JS fromString y\n\n_\u2265String_ : String \u2192 String \u2192 Bool\nx \u2265String y = fromString x \u2265JS fromString y\n\n_\u2264Number_ : Number \u2192 Number \u2192 Bool\nx \u2264Number y = fromNumber x \u2264JS fromNumber y\n\n_Number_ : Number \u2192 Number \u2192 Bool\nx >Number y = fromNumber x >JS fromNumber y\n\n_\u2265Number_ : Number \u2192 Number \u2192 Bool\nx \u2265Number y = fromNumber x \u2265JS fromNumber y\n\n_\u00b7\u00ab_\u00bb : JSValue \u2192 String \u2192 JSValue\nv \u00b7\u00ab s \u00bb = v \u00b7[ fromString s ]\n\n_\u00b7\u00ab_\u00bbA : JSValue \u2192 String \u2192 JSArray JSValue\nv \u00b7\u00ab s \u00bbA = castJSArray (v \u00b7\u00ab s \u00bb)\n\ntrace-call : {A B : Set} \u2192 String \u2192 (A \u2192 B) \u2192 A \u2192 B\ntrace-call s f x = trace s (f x) id\n\npostulate JSCmd : Set \u2192 Set\n\nCallback1 : Set \u2192 Set\nCallback1 A = JSCmd ((A \u2192 \ud835\udfd8) \u2192 \ud835\udfd8)\n\nCallback0 : Set\nCallback0 = Callback1 \ud835\udfd9\n\nCallback2 : Set \u2192 Set \u2192 Set\nCallback2 A B = JSCmd ((A \u2192 B \u2192 \ud835\udfd8) \u2192 \ud835\udfd8)\n\npostulate assert : Bool \u2192 Callback0\n{-# COMPILED_JS assert require(\"libagda\").assert #-}\n\ncheck : {A : Set}(pred : Bool)(errmsg : \ud835\udfd9 \u2192 String)(input : A) \u2192 A\ncheck true errmsg x = x\ncheck false errmsg x = throw (errmsg _) x\n\nwarn-check : {A : Set}(pred : Bool)(errmsg : \ud835\udfd9 \u2192 String)(input : A) \u2192 A\nwarn-check true errmsg x = x\nwarn-check false errmsg x = trace (\"Warning: \" ++ errmsg _) x id\n\ninfixr 0 _>>_ _!\u2081_ _!\u2082_\ndata JS! : Set\u2081 where\n end : JS!\n _!\u2081_ : {A : Set}(cmd : Callback1 A)(cb : A \u2192 JS!) \u2192 JS!\n _!\u2082_ : {A B : Set}(cmd : JSCmd ((A \u2192 B \u2192 \ud835\udfd8) \u2192 \ud835\udfd8))(cb : A \u2192 B \u2192 JS!) \u2192 JS!\n\n_>>_ : Callback0 \u2192 JS! \u2192 JS!\ncmd >> cont = cmd !\u2081 \u03bb _ \u2192 cont\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"6077652e83bff7230d91cf06d3df4541b171a639","subject":"elgamal is proved... almost","message":"elgamal is proved... almost\n","repos":"crypto-agda\/crypto-agda","old_file":"elgamal.agda","new_file":"elgamal.agda","new_contents":"{-# OPTIONS --copatterns #-}\nopen import Function\nopen import Data.Product\nopen import Data.Bool.NP as Bool\nopen import Data.Unit\nopen import Data.Maybe.NP\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc) renaming (#_ to ##_)\nopen import Data.Nat.NP hiding (_^_)\nopen import Data.Bits\nimport Data.Vec.NP as Vec\nopen Vec using (Vec; []; _\u2237_; _\u2237\u02b3_; allFin; lookup) renaming (map to vmap)\n--import Data.Vec.Properties as Vec\nopen import Algebra.FunctionProperties\nopen import Relation.Binary.PropositionalEquality as \u2261\n\nmodule elgamal where\n\n\u2605\u2081 : Set\u2082\n\u2605\u2081 = Set\u2081\n\n\u2605 : Set\u2081\n\u2605 = Set\n\n[0\u2192_,1\u2192_] : \u2200 {a} {A : Set a} \u2192 A \u2192 A \u2192 Bit \u2192 A\n[0\u2192 e\u2080 ,1\u2192 e\u2081 ] b = if b then e\u2081 else e\u2080\n\ncase_0\u2192_1\u2192_ : \u2200 {a} {A : Set a} \u2192 Bit \u2192 A \u2192 A \u2192 A\ncase b 0\u2192 e\u2080 1\u2192 e\u2081 = if b then e\u2081 else e\u2080\n\nSum : \u2605 \u2192 \u2605\nSum A = (A \u2192 \u2115) \u2192 \u2115\n\nCount : \u2605 \u2192 \u2605\nCount A = (A \u2192 Bit) \u2192 \u2115\n\nSumExt : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumExt sumA = \u2200 f g \u2192 f \u2257 g \u2192 sumA f \u2261 sumA g\n\nsumToCount : \u2200 {A} \u2192 Sum A \u2192 Count A\nsumToCount sumA f = sumA (Bool.to\u2115 \u2218 f)\n\nsumBit : Sum Bit\nsumBit f = f 0b + f 1b\n\n-- liftM2 _,_ in the continuation monad\nsumProd : \u2200 {A B : Set} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u00d7 B)\nsumProd sumA sumB f = sumA (\u03bb x\u2080 \u2192\n sumB (\u03bb x\u2081 \u2192\n f (x\u2080 , x\u2081)))\n\nsumProd-ext : \u2200 {A B : Set} {sumA : Sum A} {sumB : Sum B} \u2192\n SumExt sumA \u2192 SumExt sumB \u2192 SumExt (sumProd sumA sumB)\nsumProd-ext sumA-ext sumB-ext f g f\u2257g = sumA-ext _ _ (\u03bb x \u2192 sumB-ext _ _ (\u03bb y \u2192 f\u2257g (x , y)))\n\ndata `\u2605 : \u2605 where\n `\u22a4 : `\u2605\n `X : `\u2605\n _`\u00d7_ : `\u2605 \u2192 `\u2605 \u2192 `\u2605\ninfixr 2 _`\u00d7_\n\nmodule Univ (X : \u2605) where\n El : `\u2605 \u2192 \u2605\n El `\u22a4 = \u22a4\n El `X = X\n El (u\u2080 `\u00d7 u\u2081) = El u\u2080 \u00d7 El u\u2081\n\n record \u21ba (R : `\u2605) (A : \u2605) : \u2605 where\n constructor mk\n field\n run\u21ba : El R \u2192 A\n open \u21ba public\n\n EXP : (R : `\u2605) \u2192 \u2605\n EXP R = \u21ba R Bit\n\n Det : \u2605 \u2192 \u2605\n Det = \u21ba `\u22a4\n\nmodule \u2124q-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (sum\u2124q : Sum \u2124q)\n (sum\u2124q-lem : \u2200 f x \u2192 sum\u2124q (f \u2218 _\u229e_ x) \u2261 sum\u2124q f)\n where\n\n open Univ \u2124q\n open `\u2605 public renaming (`X to `\u2124q)\n\n sum : (u : `\u2605) \u2192 Sum (El u)\n sum `\u22a4 f = f _\n sum `X f = sum\u2124q f\n sum (u\u2080 `\u00d7 u\u2081) f = sum u\u2080 (\u03bb x\u2080 \u2192\n sum u\u2081 (\u03bb x\u2081 \u2192\n f (x\u2080 , x\u2081)))\n\n #_ : \u2200 {u} \u2192 \u21ba u Bit \u2192 \u2115\n #_ {u} f = sum u (Bool.to\u2115 \u2218 run\u21ba f)\n\n \u2047 : \u2200 R \u2192 \u21ba R (El R)\n run\u21ba (\u2047 _) = id\n\n pure\u21ba : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n run\u21ba (pure\u21ba x) r = x -- turning r to _ produce an error\n\n \u27ea_\u27eb : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n \u27ea_\u27eb = pure\u21ba\n\n {-\n \u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n \u27ea_\u27eb\u1d30 = pure\u1d30\n -}\n\n map\u21ba : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n run\u21ba (map\u21ba f x) r = f (run\u21ba x r)\n\n infixl 4 _\u229b_\n _\u229b_ : \u2200 {R S A B} \u2192 \u21ba R (A \u2192 B) \u2192 \u21ba S A \u2192 \u21ba (R `\u00d7 S) B\n run\u21ba (af \u229b ax) rs = run\u21ba af (proj\u2081 rs) (run\u21ba ax (proj\u2082 rs))\n\n \u27ea_\u00b7_\u27eb : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n \u27ea f \u00b7 x \u27eb = map\u21ba f x\n\n \u27ea_\u00b7_\u00b7_\u27eb : \u2200 {A B C} {R S} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba R A \u2192 \u21ba S B \u2192 \u21ba (R `\u00d7 S) C\n \u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n _\u27e8\u229e\u27e9_ : \u2200 {R S} \u2192 \u21ba R \u2124q \u2192 \u21ba S \u2124q \u2192 \u21ba (R `\u00d7 S) \u2124q\n x \u27e8\u229e\u27e9 y = \u27ea _\u229e_ \u00b7 x \u00b7 y \u27eb\n\n \u27e8_\u229e\u27e9_ : \u2200 {R} \u2192 \u2124q \u2192 \u21ba R \u2124q \u2192 \u21ba R \u2124q\n \u27e8 x \u229e\u27e9 y = \u27ea _\u229e_ x \u00b7 y \u27eb\n\n infix 4 _\u2248\u21ba_ _\u2248\u1d2c_\n _\u2248\u21ba_ : \u2200 {R : `\u2605} (f g : EXP R) \u2192 \u2605\n _\u2248\u21ba_ = _\u2261_ on #_\n\n _\u2248\u1d2c_ : \u2200 {A R} (f g : \u21ba R A) \u2192 Set _\n _\u2248\u1d2c_ {A} f g = \u2200 (Adv : A \u2192 Bit) \u2192 \u27ea Adv \u00b7 f \u27eb \u2248\u21ba \u27ea Adv \u00b7 g \u27eb\n\n lem : \u2200 x \u2192 \u27e8 x \u229e\u27e9 (\u2047 `\u2124q) \u2248\u1d2c \u2047 _ \n lem x Adv = sum\u2124q-lem (Bool.to\u2115 \u2218 Adv) x\n\n -- \u2200 (A : \u2124q \u2192 Bit) \u2192 # (A \u2047) \n\nopen Fin.Modulo renaming (sucmod to [suc])\n\n{-\nmodule \u2124q-implem (q-2 : \u2115) where\n q : \u2115\n q = 2 + q-2\n\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = zero\n\n [1] : \u2124q\n [1] = suc zero\n-}\nmodule \u2124q-implem (q : \u2115) ([0]' [1]' : Fin q) where\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = [0]'\n\n [1] : \u2124q\n [1] = [1]'\n\n _\u2115\u229e_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = m \u2115\u229e ([suc] n)\n {-\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = [suc] (m \u2115\u229e n)\n -}\n\n _\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u229e n = Fin.to\u2115 m \u2115\u229e n\n\n _\u2115\u22a0_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u22a0 n = [0]\n suc m \u2115\u22a0 n = n \u229e (m \u2115\u22a0 n)\n\n _\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u22a0 n = Fin.to\u2115 m \u2115\u22a0 n\n\n _[^]\u2115_ : \u2124q \u2192 \u2115 \u2192 \u2124q\n m [^]\u2115 zero = [1]\n m [^]\u2115 suc n = m \u22a0 (m [^]\u2115 n)\n\n _[^]_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m [^] n = m [^]\u2115 (Fin.to\u2115 n)\n\n all\u2124q : Vec \u2124q q\n all\u2124q = allFin q\n\n sum\u2124q : Sum \u2124q\n sum\u2124q f = Vec.sum (vmap f all\u2124q)\n\n sum\u2124q-[suc]-lem : \u2200 f \u2192 sum\u2124q (f \u2218 [suc]) \u2261 sum\u2124q f\n sum\u2124q-[suc]-lem f rewrite \u2261.sym (Vec.sum-map-rot\u2081 f all\u2124q)\n | Vec.map-\u2218 f [suc] all\u2124q\n | rot\u2081-map-sucmod q\n = refl\n\n -- comm-[suc]-\u2115\u229e : \u2200 m n \u2192 [suc] (m \u2115\u229e n) \u2261 m \u2115\u229e ([suc] n)\n\n sum\u2124q-\u2115\u229e-lem : \u2200 m f \u2192 sum\u2124q (f \u2218 _\u2115\u229e_ m) \u2261 sum\u2124q f\n sum\u2124q-\u2115\u229e-lem zero f = refl\n sum\u2124q-\u2115\u229e-lem (suc m) f rewrite sum\u2124q-[suc]-lem (f \u2218 _\u2115\u229e_ m)\n | sum\u2124q-\u2115\u229e-lem m f = refl\n\n sum\u2124q-\u229e-lem : \u2200 m f \u2192 sum\u2124q (f \u2218 _\u229e_ m) \u2261 sum\u2124q f\n sum\u2124q-\u229e-lem = sum\u2124q-\u2115\u229e-lem \u2218 Fin.to\u2115\n\nmodule G-implem (p q : \u2115) (g' : Fin p) (0[p] 1[p] : Fin p) (0[q] 1[q] : Fin q) where\n open \u2124q-implem q 0[q] 1[q] public\n open \u2124q-implem p 0[p] 1[p] public using () renaming (\u2124q to G; _\u22a0_ to _\u2219_; _[^]\u2115_ to _^[p]_)\n\n g : G\n g = g'\n\n _^_ : G \u2192 \u2124q \u2192 G\n x ^ n = x ^[p] Fin.to\u2115 n\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n postulate\n g^-inj : \u2200 m n \u2192 g^ m \u2261 g^ n \u2192 m \u2261 n\n\nmodule G-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (sum\u2124q : Sum \u2124q)\n (sum\u2124q-lem : \u2200 f x \u2192 sum\u2124q (f \u2218 _\u229e_ x) \u2261 sum\u2124q f)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n where\n\n open Univ \u2124q\n open \u2124q-count \u2124q _\u229e_ sum\u2124q sum\u2124q-lem\n\n \u2047G : \u21ba `\u2124q G\n run\u21ba \u2047G x = g ^ x\n\nmodule DDH\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g^_ : \u2124q \u2192 G)\n where\n DDHAdv : \u2605 \u2192 \u2605\n DDHAdv R = R \u2192 G \u2192 G \u2192 G \u2192 Bit\n\n DDH\u2141\u2080 : \u2200 {R} {_I : \u2605} \u2192 DDHAdv R \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n DDH\u2141\u2080 D (r , x , y , _) = D r (g^ x) (g^ y) (g^ (x \u22a0 y))\n\n DDH\u2141\u2081 : \u2200 {R} \u2192 DDHAdv R \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n DDH\u2141\u2081 D (r , x , y , z) = D r (g^ x) (g^ y) (g^ z)\n\n DDH\u2141 : \u2200 {R} \u2192 DDHAdv R \u2192 Bit \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n DDH\u2141 D b = (case b 0\u2192 DDH\u2141\u2080 1\u2192 DDH\u2141\u2081) D\n\n -- \u2141\u2032 : \u2200 {R} \u2192 DDHAdv R \u2192 (Bit \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q \u00d7 R) \u2192 Bit\n -- \u2141\u2032 D (b , x , y , z , r) = DDH\u2141 D b (x , y , z , r)\n\n module With\u21ba where\n open Univ \u2124q\n open `\u2605 public renaming (`X to `\u2124q)\n DDHAdv\u21ba : `\u2605 \u2192 \u2605\n DDHAdv\u21ba R = G \u2192 G \u2192 G \u2192 \u21ba R Bit\n DDH\u2141\u2080\u21ba : \u2200 {R _I} \u2192 DDHAdv\u21ba R \u2192 \u21ba (R `\u00d7 `\u2124q `\u00d7 `\u2124q `\u00d7 _I) Bit\n run\u21ba (DDH\u2141\u2080\u21ba D) = DDH\u2141\u2080 (\u03bb a b c d \u2192 run\u21ba (D b c d) a)\n\nmodule El-Gamal-Generic\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (Message : \u2605)\n (_\u2295_ : G \u2192 Message \u2192 Message)\n (_\u2295\u207b\u00b9_ : G \u2192 Message \u2192 Message)\n where\n\n -- \u03b1 is the pk\n -- \u03b1 = g ^ x\n -- x is the sk\n\n PubKey = G\n SecKey = \u2124q\n KeyPair = PubKey \u00d7 SecKey\n CipherText = G \u00d7 Message\n\n M = Message\n C = CipherText\n\n KeyGen : \u2124q \u2192 KeyPair\n KeyGen x = (g ^ x , x)\n\n -- KeyGen\u21ba : \u21ba \u2124q KeyPair\n -- KeyGen\u21ba = mk KeyGen\n\n Enc : PubKey \u2192 Message \u2192 \u2124q \u2192 CipherText\n Enc \u03b1 m y = \u03b2 , \u03b6 where\n \u03b2 = g ^ y\n \u03b4 = \u03b1 ^ y\n \u03b6 = \u03b4 \u2295 m\n\n -- Enc\u21ba : PubKey \u2192 Message \u2192 \u21ba \u2124q CipherText\n -- Enc\u21ba \u03b1 m = mk (Enc \u03b1 m)\n\n Dec : SecKey \u2192 CipherText \u2192 Message\n Dec x (\u03b2 , \u03b6) = (\u03b2 ^ x) \u2295\u207b\u00b9 \u03b6\n\n EncAdv : \u2605 \u2192 \u2605\n EncAdv R = PubKey \u2192 R \u2192 (Bit \u2192 M) \u00d7 (C \u2192 Bit)\n\n {-\n Game0 : \u2200 {R _I : Set} \u2192 EncAdv R \u2192 (Bit \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I \u00d7 R) \u2192 Bit\n Game0 A (b , x , y , z , r) =\n let (pk , sk) = KeyGen x\n (m , D) = A pk r in\n D (Enc pk (m b) y)\n\n Game : (i : Bit) \u2192 \u2200 {R} \u2192 EncAdv R \u2192 (Bit \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q \u00d7 R) \u2192 Bit\n Game i A (b , x , y , z , r) =\n let (\u03b1 , sk) = KeyGen x\n (m , D) = A \u03b1 r\n \u03b2 = g ^ y\n \u03b4 = \u03b1 ^ case i 0\u2192 y 1\u2192 z\n \u03b6 = \u03b4 \u2295 m b\n in {-b ==\u1d47-} D (\u03b2 , \u03b6)\n\n Game-0b\u2261Game0 : \u2200 {R} \u2192 Game 0b \u2261 Game0 {R}\n Game-0b\u2261Game0 = refl\n -}\n\n SS\u2141 : \u2200 {R _I : Set} \u2192 EncAdv R \u2192 Bit \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n SS\u2141 A b (r , x , y , z) =\n let -- (pk , sk) = KeyGen x\n (m , D) = A pk r in\n D (Enc pk (m b) y)\n where pk = g ^ x\n\n open DDH \u2124q _\u22a0_ G (_^_ g) public\n\n -- Game0 \u2248 Game 0b\n -- Game1 = Game 1b\n\n -- Game0 \u2264 \u03b5\n -- Game1 \u2261 0\n\n -- \u2047 \u2295 x \u2248 \u2047\n\n -- g ^ \u2047 \u2219 x \u2248 g ^ \u2047\n\nmodule El-Gamal-Base\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u207b\u00b9 : G \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n where\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n _\/_ : G \u2192 G \u2192 G\n x \/ y = x \u2219 (y \u207b\u00b9)\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ G _\u2219_ (flip _\/_) public\n\n TrA : \u2200 {R} \u2192 Bit \u2192 EncAdv R \u2192 DDHAdv R\n TrA b A r g\u02e3 g\u02b8 g\u02e3\u02b8 = d (g\u02b8 , g\u02e3\u02b8 \u2219 m b)\n where m,d = A g\u02e3 r\n m = proj\u2081 m,d\n d = proj\u2082 m,d\n\n like-SS\u2141 : \u2200 {R _I : Set} \u2192 EncAdv R \u2192 Bit \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n like-SS\u2141 A b (r , x , y , _z) =\n let -- g\u02e3 = g ^ x\n -- g\u02b8 = g ^ y\n (m , D) = A g\u02e3 r in\n D (g\u02b8 , (g\u02e3 ^ y) \u2219 m b)\n where g\u02e3 = g ^ x\n g\u02b8 = g ^ y\n\n SS\u2141\u2261like-SS\u2141 : \u2200 {R _I} \u2192 SS\u2141 {R} {_I} \u2261 like-SS\u2141\n SS\u2141\u2261like-SS\u2141 = refl\n\n OTP\u2141 : \u2200 {R : Set} \u2192 (R \u2192 G \u2192 G) \u2192 (R \u2192 G \u2192 G \u2192 G \u2192 Bit) \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n OTP\u2141 M D (r , x , y , z) = D r g\u02e3 g\u02b8 (g\u1dbb \u2219 M r g\u02e3)\n where g\u02e3 = g ^ x\n g\u02b8 = g ^ y\n g\u1dbb = g ^ z\n\n module With\u2124qProps\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n -- (\u22a0-comm : Commutative _\u2261_ _\u22a0_)\n -- (^-comm : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x) ^ y \u2261 (\u03b1 ^ y) ^ x)\n (sum\u2124q : Sum \u2124q)\n (sum\u2124q-ext : SumExt sum\u2124q) where\n #q_ : Count \u2124q\n #q_ = sumToCount sum\u2124q\n\n module SumU\n (R : Set)\n (sumR : Sum R)\n (sumR-ext : SumExt sumR)\n where\n U = R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q\n sumU : Sum U\n sumU = sumProd sumR (sumProd sum\u2124q (sumProd sum\u2124q sum\u2124q))\n #U_ : Count U\n #U_ = sumToCount sumU\n sumU-ext : SumExt sumU\n sumU-ext = sumProd-ext sumR-ext (sumProd-ext sum\u2124q-ext (sumProd-ext sum\u2124q-ext sum\u2124q-ext))\n\n module WithSumR\n (R : Set)\n (sumR : Sum R)\n (sumR-ext : SumExt sumR)\n where\n open SumU R sumR sumR-ext\n\n module EvenMoreProof\n (ddh-hyp : (A : DDHAdv R) \u2192 #U (DDH\u2141 A 0b) \u2261 #U (DDH\u2141 A 1b))\n\n (otp-lem : \u2200 (A : G \u2192 Bit) m \u2192 #q(\u03bb x \u2192 A (g^ x \u2219 m)) \u2261 #q(\u03bb x \u2192 A (g^ x)))\n where\n\n _\u2248U_ : (f g : U \u2192 Bit) \u2192 Set\n f \u2248U g = #U f \u2261 #U g\n\n otp-lem' : \u2200 D M\u2080 M\u2081 \u2192 OTP\u2141 M\u2080 D \u2248U OTP\u2141 M\u2081 D\n otp-lem' D M\u2080 M\u2081 = sumR-ext (f3 M\u2080) (f3 M\u2081) (\u03bb r \u2192\n sum\u2124q-ext (f2 M\u2080 r) (f2 M\u2081 r) (\u03bb x \u2192\n sum\u2124q-ext (f1 M\u2080 r x) (f1 M\u2081 r x) (\u03bb y \u2192\n pf r x y)))\n where\n f0 = \u03bb M r x y z \u2192 OTP\u2141 M D (r , x , y , z)\n f1 = \u03bb M r x y \u2192 sum\u2124q (Bool.to\u2115 \u2218 f0 M r x y)\n f2 = \u03bb M r x \u2192 sum\u2124q (f1 M r x)\n f3 = \u03bb M r \u2192 sum\u2124q (f2 M r)\n pf : \u2200 r x y \u2192 f1 M\u2080 r x y \u2261 f1 M\u2081 r x y\n pf r x y = pf'\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n m0 = M\u2080 r g\u02e3\n m1 = M\u2081 r g\u02e3\n f5 = \u03bb M z \u2192 D r g\u02e3 g\u02b8 (g^ z \u2219 M)\n pf' : #q(f5 m0) \u2261 #q(f5 m1)\n pf' rewrite otp-lem (D r g\u02e3 g\u02b8) m0\n | otp-lem (D r g\u02e3 g\u02b8) m1 = refl\n\n {-\n #-proj\u2081 : \u2200 {A B : Set} {f g : A \u2192 Bit} \u2192 # f \u2261 # g\n \u2192 #_ {A \u00d7 B} (f \u2218 proj\u2081) \u2261 #_ {A \u00d7 B} (g \u2218 proj\u2081)\n #-proj\u2081 = {!!}\n\n otp-lem'' : \u2200 (A : G \u2192 Bit) m\n \u2192 #(\u03bb { (x , y) \u2192 A (g^ x \u2219 m) }) \u2261 #(\u03bb { (x , y) \u2192 A (g ^ x) })\n otp-lem'' = {!!}\n -}\n projM : EncAdv R \u2192 Bit \u2192 R \u2192 G \u2192 G\n projM A b r g\u02e3 = proj\u2081 (A g\u02e3 r) b\n\n projD : EncAdv R \u2192 R \u2192 G \u2192 G \u2192 G \u2192 Bit\n projD A r g\u02e3 g\u02b8 g\u1dbb\u2219M = proj\u2082 (A g\u02e3 r) (g\u02b8 , g\u1dbb\u2219M)\n\n\n module WithAdversary (A : EncAdv R) b where\n A\u1d47 = TrA b A\n A\u00ac\u1d47 = TrA (not b) A\n\n pf0,5 : SS\u2141 A b \u2257 DDH\u2141 A\u1d47 0b\n pf0,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n pf1 : #U(SS\u2141 A b) \u2261 #U(DDH\u2141 A\u1d47 0b)\n pf1 = sumU-ext _ _ (cong Bool.to\u2115 \u2218 pf0,5)\n\n pf2 : DDH\u2141 A\u1d47 0b \u2248U DDH\u2141 A\u1d47 1b\n pf2 = ddh-hyp A\u1d47\n\n pf2,5 : DDH\u2141 A\u1d47 1b \u2261 OTP\u2141 (projM A b) (projD A)\n pf2,5 = refl\n\n pf3 : DDH\u2141 A\u1d47 1b \u2248U DDH\u2141 A\u00ac\u1d47 1b\n pf3 = otp-lem' (projD A) (projM A b) (projM A (not b))\n\n pf4 : DDH\u2141 A\u00ac\u1d47 1b \u2248U DDH\u2141 A\u00ac\u1d47 0b\n pf4 = \u2261.sym (ddh-hyp A\u00ac\u1d47)\n\n pf4,5 : SS\u2141 A (not b) \u2257 DDH\u2141 A\u00ac\u1d47 0b\n pf4,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n pf5 : #U(SS\u2141 A (not b)) \u2261 #U(DDH\u2141 A\u00ac\u1d47 0b)\n pf5 = sumU-ext _ _ (cong Bool.to\u2115 \u2218 pf4,5)\n\n final : #U(SS\u2141 A b) \u2261 #U(SS\u2141 A (not b))\n final rewrite pf1 | pf2 | pf3 | pf4 | pf5 = refl\n\n\n {-\n pf1 : \u2200 {R} A r \u2192 Game 0b {R} A r \u2261 \u2141\u2032 (TrA 1b A) r\n pf1 A (true , x , y , z , r) rewrite dist-^-\u22a0 g x y = refl\n pf1 A (false , x , y , z , r) = {!!}\n\n pf2 : \u2200 {R} A r \u2192 Game 1b {R} A r \u2261 \u2141\u2032 (TrA 0b A) r\n pf2 A (true , x , y , z , r) = {!!}\n pf2 A (false , x , y , z , r) = {!!}\n-}\nmodule El-Gamal-Hashed\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n -- (HKey : \u2605)\n (|M| : \u2115)\n (\u210b : {-HKey \u2192-} G \u2192 Bits |M|) where\n\n Message = Bits |M|\n\n \u210b\u27e8_\u27e9\u2295_ : G \u2192 Message \u2192 Message\n \u210b\u27e8 \u03b4 \u27e9\u2295 m = \u210b \u03b4 \u2295 m\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ Message \u210b\u27e8_\u27e9\u2295_\n\n module Proof\n -- (\u22a0-comm : Commutative _\u2261_ _\u22a0_)\n -- (^-lem : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (^-comm : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x) ^ y \u2261 (\u03b1 ^ y) ^ x)\n -- (x \u2219 x \u207b\u00b9 \u2261 0)\n (\u211a : \u2605)\n (dist : \u211a \u2192 \u211a \u2192 \u211a)\n (1\/2 : \u211a)\n (_\u2264_ : \u211a \u2192 \u211a \u2192 \u2605)\n (\u03b5-DDH : \u211a)\n where\n\n Pr[S_] : Bool \u2192 \u211a\n Pr[S b ] = {!!}\n\n -- SS\n -- SS = dist Pr[\n\n Pr[S\u2080] = Pr[S 0b ]\n Pr[S\u2081] = Pr[S 1b ]\n\n pf1 : Pr[S\u2081] \u2261 1\/2\n pf1 = {!!}\n\n pf2 : dist Pr[S\u2080] 1\/2 \u2261 dist Pr[S\u2080] Pr[S\u2081]\n pf2 = {!!}\n\n pf3 : dist Pr[S\u2080] Pr[S\u2081] \u2264 \u03b5-DDH\n pf3 = {!!}\n\n pf4 : dist Pr[S\u2080] 1\/2 \u2264 \u03b5-DDH\n pf4 = {!!}\n\nmodule G11 = G-implem 11 10 (## 2) (## 0) (## 1) (## 0) (## 1)\nopen G11\nmodule E11 = El-Gamal-Base _ _\u22a0_ G g _^_ {!!} _\u2219_\nopen E11\nopen With\u2124qProps ?\n-- open Proof {!!}\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","old_contents":"{-# OPTIONS --copatterns #-}\nopen import Function\nopen import Data.Product\nopen import Data.Bool.NP as Bool\nopen import Data.Unit\nopen import Data.Maybe.NP\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc) renaming (#_ to ##_)\nopen import Data.Nat.NP hiding (_^_)\nopen import Data.Bits\nimport Data.Vec.NP as Vec\nopen Vec using (Vec; []; _\u2237_; _\u2237\u02b3_; allFin; lookup) renaming (map to vmap)\n--import Data.Vec.Properties as Vec\nopen import Algebra.FunctionProperties\nopen import Relation.Binary.PropositionalEquality as \u2261\n\nmodule elgamal where\n\n\u2605\u2081 : Set\u2082\n\u2605\u2081 = Set\u2081\n\n\u2605 : Set\u2081\n\u2605 = Set\n\n[0\u2192_,1\u2192_] : \u2200 {a} {A : Set a} \u2192 A \u2192 A \u2192 Bool \u2192 A\n[0\u2192 e\u2080 ,1\u2192 e\u2081 ] b = if b then e\u2081 else e\u2080\n\ncase_0\u2192_1\u2192_ : \u2200 {a} {A : Set a} \u2192 Bool \u2192 A \u2192 A \u2192 A\ncase b 0\u2192 e\u2080 1\u2192 e\u2081 = if b then e\u2081 else e\u2080\n\nsumBit : (Bit \u2192 \u2115) \u2192 \u2115\nsumBit f = f 0b + f 1b\n\ndata `\u2605 : \u2605 where\n `\u22a4 : `\u2605\n `X : `\u2605\n _`\u00d7_ : `\u2605 \u2192 `\u2605 \u2192 `\u2605\ninfixr 2 _`\u00d7_\n\nmodule Univ (X : \u2605) where\n El : `\u2605 \u2192 \u2605\n El `\u22a4 = \u22a4\n El `X = X\n El (u\u2080 `\u00d7 u\u2081) = El u\u2080 \u00d7 El u\u2081\n\n record \u21ba (R : `\u2605) (A : \u2605) : \u2605 where\n constructor mk\n field\n run\u21ba : El R \u2192 A\n open \u21ba public\n\n EXP : (R : `\u2605) \u2192 \u2605\n EXP R = \u21ba R Bit\n\n Det : \u2605 \u2192 \u2605\n Det = \u21ba `\u22a4\n\nmodule \u2124q-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (sum\u2124q : (\u2124q \u2192 \u2115) \u2192 \u2115)\n (sum\u2124q-lem : \u2200 f x \u2192 sum\u2124q (f \u2218 _\u229e_ x) \u2261 sum\u2124q f)\n where\n\n open Univ \u2124q\n open `\u2605 public renaming (`X to `\u2124q)\n\n sum : (u : `\u2605) \u2192 (El u \u2192 \u2115) \u2192 \u2115\n sum `\u22a4 f = f _\n sum `X f = sum\u2124q f\n sum (u\u2080 `\u00d7 u\u2081) f = sum u\u2080 (\u03bb x\u2080 \u2192\n sum u\u2081 (\u03bb x\u2081 \u2192\n f (x\u2080 , x\u2081)))\n\n #_ : \u2200 {u} \u2192 \u21ba u Bit \u2192 \u2115\n #_ {u} f = sum u (Bool.to\u2115 \u2218 run\u21ba f)\n\n \u2047 : \u2200 R \u2192 \u21ba R (El R)\n run\u21ba (\u2047 _) = id\n\n pure\u21ba : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n run\u21ba (pure\u21ba x) r = x -- turning r to _ produce an error\n\n \u27ea_\u27eb : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n \u27ea_\u27eb = pure\u21ba\n\n {-\n \u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n \u27ea_\u27eb\u1d30 = pure\u1d30\n -}\n\n map\u21ba : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n run\u21ba (map\u21ba f x) r = f (run\u21ba x r)\n\n infixl 4 _\u229b_\n _\u229b_ : \u2200 {R S A B} \u2192 \u21ba R (A \u2192 B) \u2192 \u21ba S A \u2192 \u21ba (R `\u00d7 S) B\n run\u21ba (af \u229b ax) rs = run\u21ba af (proj\u2081 rs) (run\u21ba ax (proj\u2082 rs))\n\n \u27ea_\u00b7_\u27eb : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n \u27ea f \u00b7 x \u27eb = map\u21ba f x\n\n \u27ea_\u00b7_\u00b7_\u27eb : \u2200 {A B C} {R S} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba R A \u2192 \u21ba S B \u2192 \u21ba (R `\u00d7 S) C\n \u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n _\u27e8\u229e\u27e9_ : \u2200 {R S} \u2192 \u21ba R \u2124q \u2192 \u21ba S \u2124q \u2192 \u21ba (R `\u00d7 S) \u2124q\n x \u27e8\u229e\u27e9 y = \u27ea _\u229e_ \u00b7 x \u00b7 y \u27eb\n\n \u27e8_\u229e\u27e9_ : \u2200 {R} \u2192 \u2124q \u2192 \u21ba R \u2124q \u2192 \u21ba R \u2124q\n \u27e8 x \u229e\u27e9 y = \u27ea _\u229e_ x \u00b7 y \u27eb\n\n infix 4 _\u2248\u21ba_ _\u2248\u1d2c_\n _\u2248\u21ba_ : \u2200 {R : `\u2605} (f g : EXP R) \u2192 \u2605\n _\u2248\u21ba_ = _\u2261_ on #_\n\n _\u2248\u1d2c_ : \u2200 {A R} (f g : \u21ba R A) \u2192 Set _\n _\u2248\u1d2c_ {A} f g = \u2200 (Adv : A \u2192 Bit) \u2192 \u27ea Adv \u00b7 f \u27eb \u2248\u21ba \u27ea Adv \u00b7 g \u27eb\n\n lem : \u2200 x \u2192 \u27e8 x \u229e\u27e9 (\u2047 `\u2124q) \u2248\u1d2c \u2047 _ \n lem x Adv = sum\u2124q-lem (Bool.to\u2115 \u2218 Adv) x\n\n -- \u2200 (A : \u2124q \u2192 Bit) \u2192 # (A \u2047) \n\nopen Fin.Modulo renaming (sucmod to [suc])\n\n{-\nmodule \u2124q-implem (q-2 : \u2115) where\n q : \u2115\n q = 2 + q-2\n\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = zero\n\n [1] : \u2124q\n [1] = suc zero\n-}\nmodule \u2124q-implem (q : \u2115) ([0]' [1]' : Fin q) where\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = [0]'\n\n [1] : \u2124q\n [1] = [1]'\n\n _\u2115\u229e_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = m \u2115\u229e ([suc] n)\n {-\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = [suc] (m \u2115\u229e n)\n -}\n\n _\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u229e n = Fin.to\u2115 m \u2115\u229e n\n\n _\u2115\u22a0_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u22a0 n = [0]\n suc m \u2115\u22a0 n = n \u229e (m \u2115\u22a0 n)\n\n _\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u22a0 n = Fin.to\u2115 m \u2115\u22a0 n\n\n _[^]\u2115_ : \u2124q \u2192 \u2115 \u2192 \u2124q\n m [^]\u2115 zero = [1]\n m [^]\u2115 suc n = m \u22a0 (m [^]\u2115 n)\n\n _[^]_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m [^] n = m [^]\u2115 (Fin.to\u2115 n)\n\n all\u2124q : Vec \u2124q q\n all\u2124q = allFin q\n\n sum\u2124q : (\u2124q \u2192 \u2115) \u2192 \u2115\n sum\u2124q f = Vec.sum (vmap f all\u2124q)\n\n sum\u2124q-[suc]-lem : \u2200 f \u2192 sum\u2124q (f \u2218 [suc]) \u2261 sum\u2124q f\n sum\u2124q-[suc]-lem f rewrite \u2261.sym (Vec.sum-map-rot\u2081 f all\u2124q)\n | Vec.map-\u2218 f [suc] all\u2124q\n | rot\u2081-map-sucmod q\n = refl\n\n -- comm-[suc]-\u2115\u229e : \u2200 m n \u2192 [suc] (m \u2115\u229e n) \u2261 m \u2115\u229e ([suc] n)\n\n sum\u2124q-\u2115\u229e-lem : \u2200 m f \u2192 sum\u2124q (f \u2218 _\u2115\u229e_ m) \u2261 sum\u2124q f\n sum\u2124q-\u2115\u229e-lem zero f = refl\n sum\u2124q-\u2115\u229e-lem (suc m) f rewrite sum\u2124q-[suc]-lem (f \u2218 _\u2115\u229e_ m)\n | sum\u2124q-\u2115\u229e-lem m f = refl\n\n sum\u2124q-\u229e-lem : \u2200 m f \u2192 sum\u2124q (f \u2218 _\u229e_ m) \u2261 sum\u2124q f\n sum\u2124q-\u229e-lem = sum\u2124q-\u2115\u229e-lem \u2218 Fin.to\u2115\n\nmodule G-implem (p q : \u2115) (g' : Fin p) (0[p] 1[p] : Fin p) (0[q] 1[q] : Fin q) where\n open \u2124q-implem q 0[q] 1[q] public\n open \u2124q-implem p 0[p] 1[p] public using () renaming (\u2124q to G; _\u22a0_ to _\u2219_; _[^]\u2115_ to _^[p]_)\n\n g : G\n g = g'\n\n _^_ : G \u2192 \u2124q \u2192 G\n x ^ n = x ^[p] Fin.to\u2115 n\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n postulate\n g^-inj : \u2200 m n \u2192 g^ m \u2261 g^ n \u2192 m \u2261 n\n\nmodule G-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (sum\u2124q : (\u2124q \u2192 \u2115) \u2192 \u2115)\n (sum\u2124q-lem : \u2200 f x \u2192 sum\u2124q (f \u2218 _\u229e_ x) \u2261 sum\u2124q f)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n where\n\n open Univ \u2124q\n open \u2124q-count \u2124q _\u229e_ sum\u2124q sum\u2124q-lem\n\n \u2047G : \u21ba `\u2124q G\n run\u21ba \u2047G x = g ^ x\n\nmodule DDH\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g^_ : \u2124q \u2192 G)\n where\n DDHAdv : \u2605 \u2192 \u2605\n DDHAdv R = G \u2192 G \u2192 G \u2192 R \u2192 Bool\n\n DDH\u2141\u2080 : \u2200 {R} {_I : \u2605} \u2192 DDHAdv R \u2192 (\u2124q \u00d7 \u2124q \u00d7 _I \u00d7 R) \u2192 Bool\n DDH\u2141\u2080 D (x , y , _ , r) = D (g^ x) (g^ y) (g^ (x \u22a0 y)) r\n\n DDH\u2141\u2081 : \u2200 {R} \u2192 DDHAdv R \u2192 (\u2124q \u00d7 \u2124q \u00d7 \u2124q \u00d7 R) \u2192 Bool\n DDH\u2141\u2081 D (x , y , z , r) = D (g^ x) (g^ y) (g^ z) r\n\n DDH\u2141 : \u2200 {R} \u2192 DDHAdv R \u2192 Bool \u2192 (\u2124q \u00d7 \u2124q \u00d7 \u2124q \u00d7 R) \u2192 Bool\n DDH\u2141 D b = (case b 0\u2192 DDH\u2141\u2080 1\u2192 DDH\u2141\u2081) D\n\n -- \u2141\u2032 : \u2200 {R} \u2192 DDHAdv R \u2192 (Bool \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q \u00d7 R) \u2192 Bool\n -- \u2141\u2032 D (b , x , y , z , r) = DDH\u2141 D b (x , y , z , r)\n\n module With\u21ba where\n open Univ \u2124q\n open `\u2605 public renaming (`X to `\u2124q)\n DDHAdv\u21ba : `\u2605 \u2192 \u2605\n DDHAdv\u21ba R = G \u2192 G \u2192 G \u2192 \u21ba R Bool\n \u2141\u2080\u21ba : \u2200 {R _I} \u2192 DDHAdv\u21ba R \u2192 \u21ba (`\u2124q `\u00d7 `\u2124q `\u00d7 _I `\u00d7 R) Bool\n run\u21ba (\u2141\u2080\u21ba D) = \u2141\u2080 (\u03bb a b c \u2192 run\u21ba (D a b c))\n\nmodule El-Gamal-Generic\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (Message : \u2605)\n (_\u2295_ : G \u2192 Message \u2192 Message)\n (_\u2295\u207b\u00b9_ : G \u2192 Message \u2192 Message)\n where\n\n -- \u03b1 is the pk\n -- \u03b1 = g ^ x\n -- x is the sk\n\n PubKey = G\n SecKey = \u2124q\n KeyPair = PubKey \u00d7 SecKey\n CipherText = G \u00d7 Message\n\n M = Message\n C = CipherText\n\n KeyGen : \u2124q \u2192 KeyPair\n KeyGen x = (g ^ x , x)\n\n -- KeyGen\u21ba : \u21ba \u2124q KeyPair\n -- KeyGen\u21ba = mk KeyGen\n\n Enc : PubKey \u2192 Message \u2192 \u2124q \u2192 CipherText\n Enc \u03b1 m y = \u03b2 , \u03b6 where\n \u03b2 = g ^ y\n \u03b4 = \u03b1 ^ y\n \u03b6 = \u03b4 \u2295 m\n\n -- Enc\u21ba : PubKey \u2192 Message \u2192 \u21ba \u2124q CipherText\n -- Enc\u21ba \u03b1 m = mk (Enc \u03b1 m)\n\n Dec : SecKey \u2192 CipherText \u2192 Message\n Dec x (\u03b2 , \u03b6) = (\u03b2 ^ x) \u2295\u207b\u00b9 \u03b6\n\n EncAdv : \u2605 \u2192 \u2605\n EncAdv R = PubKey \u2192 R \u2192 (Bool \u2192 M) \u00d7 (C \u2192 Bool)\n\n {-\n Game0 : \u2200 {R _I : Set} \u2192 EncAdv R \u2192 (Bool \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I \u00d7 R) \u2192 Bool\n Game0 A (b , x , y , z , r) =\n let (pk , sk) = KeyGen x\n (m , D) = A pk r in\n D (Enc pk (m b) y)\n\n Game : (i : Bool) \u2192 \u2200 {R} \u2192 EncAdv R \u2192 (Bool \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q \u00d7 R) \u2192 Bool\n Game i A (b , x , y , z , r) =\n let (\u03b1 , sk) = KeyGen x\n (m , D) = A \u03b1 r\n \u03b2 = g ^ y\n \u03b4 = \u03b1 ^ case i 0\u2192 y 1\u2192 z\n \u03b6 = \u03b4 \u2295 m b\n in {-b ==\u1d47-} D (\u03b2 , \u03b6)\n\n Game-0b\u2261Game0 : \u2200 {R} \u2192 Game 0b \u2261 Game0 {R}\n Game-0b\u2261Game0 = refl\n -}\n\n SS\u2141 : \u2200 {R _I : Set} \u2192 EncAdv R \u2192 Bool \u2192 (\u2124q \u00d7 \u2124q \u00d7 _I \u00d7 R) \u2192 Bool\n SS\u2141 A b (x , y , z , r) =\n let -- (pk , sk) = KeyGen x\n (m , D) = A pk r in\n D (Enc pk (m b) y)\n where pk = g ^ x\n\n open DDH \u2124q _\u22a0_ G (_^_ g) public\n\n -- Game0 \u2248 Game 0b\n -- Game1 = Game 1b\n\n -- Game0 \u2264 \u03b5\n -- Game1 \u2261 0\n\n -- \u2047 \u2295 x \u2248 \u2047\n\n -- g ^ \u2047 \u2219 x \u2248 g ^ \u2047\n\nmodule El-Gamal-Base\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u207b\u00b9 : G \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n where\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n _\/_ : G \u2192 G \u2192 G\n x \/ y = x \u2219 (y \u207b\u00b9)\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ G _\u2219_ (flip _\/_) public\n\n TrA : \u2200 {R} \u2192 Bool \u2192 EncAdv R \u2192 DDHAdv R\n TrA b A g\u02e3 g\u02b8 g\u02e3\u02b8 r = d (g\u02b8 , g\u02e3\u02b8 \u2219 m b)\n where m,d = A g\u02e3 r\n m = proj\u2081 m,d\n d = proj\u2082 m,d\n\n like-SS\u2141 : \u2200 {R _I : Set} \u2192 EncAdv R \u2192 Bool \u2192 (\u2124q \u00d7 \u2124q \u00d7 _I \u00d7 R) \u2192 Bool\n like-SS\u2141 A b (x , y , z , r) =\n let -- g\u02e3 = g ^ x\n -- g\u02b8 = g ^ y\n (m , D) = A g\u02e3 r in\n D (g\u02b8 , (g\u02e3 ^ y) \u2219 m b)\n where g\u02e3 = g ^ x\n g\u02b8 = g ^ y\n\n SS\u2141\u2261like-SS\u2141 : \u2200 {R _I} \u2192 SS\u2141 {R} {_I} \u2261 like-SS\u2141\n SS\u2141\u2261like-SS\u2141 = refl\n\n module Proof\n -- (\u22a0-comm : Commutative _\u2261_ _\u22a0_)\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n -- (^-comm : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x) ^ y \u2261 (\u03b1 ^ y) ^ x)\n\n (#_ : \u2200 {R} \u2192 (R \u2192 Bit) \u2192 \u2115)\n (ddh-hyp : \u2200 {R} (A : DDHAdv R) \u2192 # (DDH\u2141 A 0b) \u2261 # (DDH\u2141 A 1b))\n (otp-lem : \u2200 (A : G \u2192 Bit) m \u2192 #(\u03bb x \u2192 A (g^ x \u2219 m)) \u2261 #(\u03bb x \u2192 A (g ^ x)))\n -- (wk\u2248 : \u2200 {R1 R2} (f : R2 \u2192 R1)(x y : R1 \u2192 Bit) (z t : R2 \u2192 Bit) \u2192 x \u2248 y \u2192 z \u2248 t)\n where\n\n _\u2248_ : \u2200 {R} \u2192 (f g : R \u2192 Bit) \u2192 Set\n f \u2248 g = # f \u2261 # g\n\n pf1 : \u2200 {R} A b r \u2192 SS\u2141 {R} A b r \u2261 DDH\u2141 (TrA b A) 0b r\n pf1 A b (x , y , z , r) rewrite dist-^-\u22a0 g x y = refl\n\n pf2 : \u2200 {R} A b \u2192 DDH\u2141 {R} (TrA b A) 0b \u2248 DDH\u2141 (TrA b A) 1b\n pf2 A b = ddh-hyp (TrA b A)\n\n pf3 : \u2200 {R} A b \u2192 DDH\u2141 {R} (TrA b A) 1b \u2248 DDH\u2141 (TrA (not b) A) 1b\n pf3 {R} A b = pf3'\n where\n A\u1d47 = TrA b A\n\n -- pf3'' : # (DDH\u2141\u2081 {R} A\u1d47) \u2248 DDH\u2141\u2081 (TrA (not b) A)\n -- pf3'' = ?\n\n pf3' : DDH\u2141\u2081 {R} A\u1d47 \u2248 DDH\u2141\u2081 (TrA (not b) A)\n pf3' = {!wk\u2248 (\u03bb { (x , y , z , r) \u2192 {!!} }) _ _ _ _ (otp-lem {!!} {!!})!}\n\n {-\n pf1 : \u2200 {R} A r \u2192 Game 0b {R} A r \u2261 \u2141\u2032 (TrA 1b A) r\n pf1 A (true , x , y , z , r) rewrite dist-^-\u22a0 g x y = refl\n pf1 A (false , x , y , z , r) = {!!}\n\n pf2 : \u2200 {R} A r \u2192 Game 1b {R} A r \u2261 \u2141\u2032 (TrA 0b A) r\n pf2 A (true , x , y , z , r) = {!!}\n pf2 A (false , x , y , z , r) = {!!}\n-}\nmodule El-Gamal-Hashed\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n -- (HKey : \u2605)\n (|M| : \u2115)\n (\u210b : {-HKey \u2192-} G \u2192 Bits |M|) where\n\n Message = Bits |M|\n\n \u210b\u27e8_\u27e9\u2295_ : G \u2192 Message \u2192 Message\n \u210b\u27e8 \u03b4 \u27e9\u2295 m = \u210b \u03b4 \u2295 m\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ Message \u210b\u27e8_\u27e9\u2295_\n\n module Proof\n -- (\u22a0-comm : Commutative _\u2261_ _\u22a0_)\n -- (^-lem : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (^-comm : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x) ^ y \u2261 (\u03b1 ^ y) ^ x)\n -- (x \u2219 x \u207b\u00b9 \u2261 0)\n (\u211a : \u2605)\n (dist : \u211a \u2192 \u211a \u2192 \u211a)\n (1\/2 : \u211a)\n (_\u2264_ : \u211a \u2192 \u211a \u2192 \u2605)\n (\u03b5-DDH : \u211a)\n where\n\n Pr[S_] : Bool \u2192 \u211a\n Pr[S b ] = {!!}\n\n -- SS\n -- SS = dist Pr[\n\n Pr[S\u2080] = Pr[S 0b ]\n Pr[S\u2081] = Pr[S 1b ]\n\n pf1 : Pr[S\u2081] \u2261 1\/2\n pf1 = {!!}\n\n pf2 : dist Pr[S\u2080] 1\/2 \u2261 dist Pr[S\u2080] Pr[S\u2081]\n pf2 = {!!}\n\n pf3 : dist Pr[S\u2080] Pr[S\u2081] \u2264 \u03b5-DDH\n pf3 = {!!}\n\n pf4 : dist Pr[S\u2080] 1\/2 \u2264 \u03b5-DDH\n pf4 = {!!}\n\nmodule G11 = G-implem 11 10 (## 2) (## 0) (## 1) (## 0) (## 1)\nopen G11\nmodule E11 = El-Gamal-Base _ _\u22a0_ G g _^_ {!!} _\u2219_\nopen E11\nopen Proof {!!}\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"7b2c31fcd7415cecc448653df3d386001cbebf1b","subject":"Put constructor arguments on different lines.","message":"Put constructor arguments on different lines.\n\nOld-commit-hash: 02f9c59c59ddef7050c2c035457c98ecb00464dc\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Term\/Plotkin.agda","new_file":"Syntax\/Term\/Plotkin.agda","new_contents":"import Syntax.Type.Plotkin as Type\n\nmodule Syntax.Term.Plotkin\n {B : Set {- of base types -}}\n {C : Type.Type B \u2192 Set {- of constants -}}\n where\n\n-- Terms of languages described in Plotkin style\n\nopen import Function using (_\u2218_)\nopen import Data.Product\nopen Type B\nopen import Syntax.Context {Type}\n\ndata Term\n (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set\n where\n const : \u2200 {\u03c4} \u2192\n (c : C \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\n-- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n-- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\nlift-diff-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4) \u00d7 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-diff-apply diff apply {base \u03b9} = diff , apply\nlift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\nlift-diff : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4)\n\nlift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\nlift-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n-- Weakening\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\nweaken \u0393\u2081\u227c\u0393\u2082 (const c) = const c\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (lift \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\n-- Specialized weakening\nweaken\u2081 : \u2200 {\u0393 \u03c3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4\nweaken\u2081 t = weaken (drop _ \u2022 \u227c-refl) t\n\nweaken\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u0393) \u03c4\nweaken\u2082 t = weaken (drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\nweaken\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u03b3 \u2022 \u0393) \u03c4\nweaken\u2083 t = weaken (drop _ \u2022 drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n-- Shorthands for nested applications\napp\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3\napp\u2082 f x = app (app f x)\n\napp\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4\napp\u2083 f x = app\u2082 (app f x)\n\napp\u2084 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5\napp\u2084 f x = app\u2083 (app f x)\n\napp\u2085 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6\napp\u2085 f x = app\u2084 (app f x)\n\napp\u2086 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6 \u21d2 \u03b7) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6 \u2192 Term \u0393 \u03b7\napp\u2086 f x = app\u2085 (app f x)\n","old_contents":"import Syntax.Type.Plotkin as Type\n\nmodule Syntax.Term.Plotkin\n {B : Set {- of base types -}}\n {C : Type.Type B \u2192 Set {- of constants -}}\n where\n\n-- Terms of languages described in Plotkin style\n\nopen import Function using (_\u2218_)\nopen import Data.Product\nopen Type B\nopen import Syntax.Context {Type}\n\ndata Term\n (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set\n where\n const : \u2200 {\u03c4} \u2192 (c : C \u03c4) \u2192 Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4))\n (t : Term \u0393 \u03c3) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 Term \u0393 (\u03c3 \u21d2 \u03c4)\n\n-- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n-- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\nlift-diff-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4) \u00d7 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-diff-apply diff apply {base \u03b9} = diff , apply\nlift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\nlift-diff : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4)\n\nlift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\nlift-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n-- Weakening\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\nweaken \u0393\u2081\u227c\u0393\u2082 (const c) = const c\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (lift \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\n-- Specialized weakening\nweaken\u2081 : \u2200 {\u0393 \u03c3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4\nweaken\u2081 t = weaken (drop _ \u2022 \u227c-refl) t\n\nweaken\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u0393) \u03c4\nweaken\u2082 t = weaken (drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\nweaken\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u03b3 \u2022 \u0393) \u03c4\nweaken\u2083 t = weaken (drop _ \u2022 drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n-- Shorthands for nested applications\napp\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3\napp\u2082 f x = app (app f x)\n\napp\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4\napp\u2083 f x = app\u2082 (app f x)\n\napp\u2084 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5\napp\u2084 f x = app\u2083 (app f x)\n\napp\u2085 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6\napp\u2085 f x = app\u2084 (app f x)\n\napp\u2086 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6 \u21d2 \u03b7) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6 \u2192 Term \u0393 \u03b7\napp\u2086 f x = app\u2085 (app f x)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3d7e95768705107ac7dd83dd8c8b12e1304b3b17","subject":"Cleaning.","message":"Cleaning.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/FOT\/FOTC\/Program\/ABP\/Lemma1WithoutHelperATP.agda","new_file":"notes\/FOT\/FOTC\/Program\/ABP\/Lemma1WithoutHelperATP.agda","new_contents":"------------------------------------------------------------------------------\n-- ABP lemma 1\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- From Dybjer and Sander's paper: The first lemma states that given a\n-- start state S of the ABP, we will arrive at a state S', where the\n-- message has been received by the receiver, but where the\n-- acknowledgement has not yet been received by the sender.\n\nmodule FOT.FOTC.Program.ABP.Lemma1WithoutHelperATP where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Base.Loop\nopen import FOTC.Data.Bool\nopen import FOTC.Data.Bool.PropertiesATP using ( x\u2262not-x )\nopen import FOTC.Data.List\nopen import FOTC.Program.ABP.ABP\nopen import FOTC.Program.ABP.Fair.Type\nopen import FOTC.Program.ABP.Fair.PropertiesATP\nopen import FOTC.Program.ABP.Terms\n\n------------------------------------------------------------------------------\n\nas^ : \u2200 b i' is' ds \u2192 D\nas^ b i' is' ds = await b i' is' ds\n{-# ATP definition as^ #-}\n\nbs^ : D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D\nbs^ b i' is' ds os\u2081^ = corrupt os\u2081^ \u00b7 (as^ b i' is' ds)\n{-# ATP definition bs^ #-}\n\ncs^ : D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D\ncs^ b i' is' ds os\u2081^ = ack b \u00b7 (bs^ b i' is' ds os\u2081^)\n{-# ATP definition cs^ #-}\n\nds^ : D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D\nds^ b i' is' ds os\u2081^ os\u2082^ = corrupt os\u2082^ \u00b7 cs^ b i' is' ds os\u2081^\n{-# ATP definition ds^ #-}\n\nos\u2081^ : D \u2192 D \u2192 D\nos\u2081^ os\u2081' ft\u2081^ = ft\u2081^ ++ os\u2081'\n{-# ATP definition os\u2081^ #-}\n\nos\u2082^ : D \u2192 D\nos\u2082^ os\u2082 = tail\u2081 os\u2082\n{-# ATP definition os\u2082^ #-}\n\n-- Helper function for the ABP lemma 1\nmodule Helper where\n -- 30 November 2013. If we don't have the following definitions\n -- outside the where clause, the ATPs cannot prove the theorems.\n\n helper : \u2200 b i' is' os\u2081 os\u2082 as bs cs ds js \u2192\n Bit b \u2192\n Fair os\u2082 \u2192\n S b (i' \u2237 is') os\u2081 os\u2082 as bs cs ds js \u2192\n \u2200 ft\u2081 os\u2081' \u2192 F*T ft\u2081 \u2192 Fair os\u2081' \u2192 os\u2081 \u2261 ft\u2081 ++ os\u2081' \u2192\n \u2203[ os\u2081' ] \u2203[ os\u2082' ] \u2203[ as' ] \u2203[ bs' ] \u2203[ cs' ] \u2203[ ds' ] \u2203[ js' ]\n Fair os\u2081'\n \u2227 Fair os\u2082'\n \u2227 S' b i' is' os\u2081' os\u2082' as' bs' cs' ds' js'\n \u2227 js \u2261 i' \u2237 js'\n helper b i' is' os\u2081 os\u2082 as bs cs ds js\n Bb Fos\u2082 s .(T \u2237 []) os\u2081' f*tnil Fos\u2081' os\u2081-eq = prf\n where\n postulate\n prf : \u2203[ os\u2081' ] \u2203[ os\u2082' ] \u2203[ as' ] \u2203[ bs' ] \u2203[ cs' ] \u2203[ ds' ] \u2203[ js' ]\n Fair os\u2081'\n \u2227 Fair os\u2082'\n \u2227 (as' \u2261 await b i' is' ds'\n \u2227 bs' \u2261 corrupt os\u2081' \u00b7 as'\n \u2227 cs' \u2261 ack (not b) \u00b7 bs'\n \u2227 ds' \u2261 corrupt os\u2082' \u00b7 (b \u2237 cs')\n \u2227 js' \u2261 out (not b) \u00b7 bs')\n \u2227 js \u2261 i' \u2237 js'\n {-# ATP prove prf #-}\n\n -- TODO (25 January 2014): Why Agda cannot refine using the helper\n -- function? Agda bug?\n helper b i' is' os\u2081 os\u2082 as bs cs ds js Bb Fos\u2082 s\n .(F \u2237 ft\u2081^) os\u2081' (f*tcons {ft\u2081^} FTft\u2081^) Fos\u2081' os\u2081-eq =\n helper b i' is'\n (ft\u2081^ ++ os\u2081')\n (tail\u2081 os\u2082)\n (await b i' is' ds)\n (corrupt (ft\u2081^ ++ os\u2081') \u00b7 await b i' is' ds)\n (ack b \u00b7 (corrupt (ft\u2081^ ++ os\u2081') \u00b7 await b i' is' ds))\n (corrupt (tail\u2081 os\u2082) \u00b7\n (ack b \u00b7 (corrupt (ft\u2081^ ++ os\u2081') \u00b7 await b i' is' ds)))\n js\n Bb (tail-Fair Fos\u2082) ihS ft\u2081^ os\u2081' FTft\u2081^ Fos\u2081' refl\n where\n postulate os\u2081-eq-helper : os\u2081 \u2261 F \u2237 os\u2081^ os\u2081' ft\u2081^\n {-# ATP prove os\u2081-eq-helper #-}\n\n postulate as-eq : as \u2261 < i' , b > \u2237 (as^ b i' is' ds)\n {-# ATP prove as-eq #-}\n\n postulate bs-eq : bs \u2261 error \u2237 (bs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^))\n {-# ATP prove bs-eq os\u2081-eq-helper as-eq #-}\n\n postulate cs-eq : cs \u2261 not b \u2237 cs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^)\n {-# ATP prove cs-eq bs-eq #-}\n\n postulate\n ds-eq-helper\u2081 :\n os\u2082 \u2261 T \u2237 tail\u2081 os\u2082 \u2192\n ds \u2261 ok (not b) \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n {-# ATP prove ds-eq-helper\u2081 cs-eq #-}\n\n postulate\n ds-eq-helper\u2082 : os\u2082 \u2261 F \u2237 tail\u2081 os\u2082 \u2192\n ds \u2261 error \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n {-# ATP prove ds-eq-helper\u2082 cs-eq #-}\n\n ds-eq : ds \u2261 ok (not b) \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n \u2228 ds \u2261 error \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n ds-eq = case (\u03bb h \u2192 inj\u2081 (ds-eq-helper\u2081 h))\n (\u03bb h \u2192 inj\u2082 (ds-eq-helper\u2082 h))\n (head-tail-Fair Fos\u2082)\n\n postulate\n as^-eq-helper\u2081 :\n ds \u2261 ok (not b) \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082) \u2192\n as^ b i' is' ds \u2261\n send b \u00b7 (i' \u2237 is') \u00b7 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n {-# ATP prove as^-eq-helper\u2081 x\u2262not-x #-}\n\n postulate\n as^-eq-helper\u2082 :\n ds \u2261 error \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082) \u2192\n as^ b i' is' ds \u2261\n send b \u00b7 (i' \u2237 is') \u00b7 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n {-# ATP prove as^-eq-helper\u2082 #-}\n\n as^-eq : as^ b i' is' ds \u2261\n send b \u00b7 (i' \u2237 is') \u00b7 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n as^-eq = case as^-eq-helper\u2081 as^-eq-helper\u2082 ds-eq\n\n postulate js-eq : js \u2261 out b \u00b7 bs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^)\n {-# ATP prove js-eq bs-eq #-}\n\n ihS : S b\n (i' \u2237 is')\n (os\u2081^ os\u2081' ft\u2081^)\n (os\u2082^ os\u2082)\n (as^ b i' is' ds)\n (bs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^))\n (cs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^))\n (ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082))\n js\n ihS = as^-eq , refl , refl , refl , js-eq\n\n------------------------------------------------------------------------------\n-- From Dybjer and Sander's paper: From the assumption that os\u2081\u00a0\u2208\u00a0Fair\n-- and hence by unfolding Fair, we conclude that there are ft\u2081\u00a0:\u00a0 F*T\n-- and os\u2081'\u00a0:\u00a0Fair, such that os\u2081\u00a0=\u00a0ft\u2081\u00a0++ os\u2081'.\n--\n-- We proceed by induction on ft\u2081\u00a0:\u00a0F*T using helper.\n\n-- lemma\u2081 : \u2200 b i' is' os\u2081 os\u2082 as bs cs ds js \u2192\n-- Bit b \u2192\n-- Fair os\u2081 \u2192\n-- Fair os\u2082 \u2192\n-- S b (i' \u2237 is') os\u2081 os\u2082 as bs cs ds js \u2192\n-- \u2203[ os\u2081' ] \u2203[ os\u2082' ] \u2203[ as' ] \u2203[ bs' ] \u2203[ cs' ] \u2203[ ds' ] \u2203[ js' ]\n-- Fair os\u2081'\n-- \u2227 Fair os\u2082'\n-- \u2227 S' b i' is' os\u2081' os\u2082' as' bs' cs' ds' js'\n-- \u2227 js \u2261 i' \u2237 js'\n-- lemma\u2081 b i' is' os\u2081 os\u2082 as bs cs ds js Bb Fos\u2081 Fos\u2082 s with Fair-out Fos\u2081\n-- ... | .(true \u2237 []) , os\u2081' , f*tnil , os\u2081\u2261ft\u2081++os\u2081' , Fos\u2081' = prf\n-- where\n-- postulate\n-- prf : \u2203[ os\u2081' ] \u2203[ os\u2082' ] \u2203[ as' ] \u2203[ bs' ] \u2203[ cs' ] \u2203[ ds' ] \u2203[ js' ]\n-- Fair os\u2081'\n-- \u2227 Fair os\u2082'\n-- \u2227 (as' \u2261 await b i' is' ds'\n-- \u2227 bs' \u2261 corrupt os\u2081' \u00b7 as'\n-- \u2227 cs' \u2261 ack (not b) \u00b7 bs'\n-- \u2227 ds' \u2261 corrupt os\u2082' \u00b7 (b \u2237 cs')\n-- \u2227 js' \u2261 out (not b) \u00b7 bs')\n-- \u2227 js \u2261 i' \u2237 js'\n-- {-# ATP prove prf #-}\n\n-- ... | .(F \u2237 ft\u2081^) , os\u2081' , f*tcons {ft\u2081^} FTft\u2081 , os\u2081\u2261ft\u2081++os\u2081' , Fos\u2081' =\n-- lemma\u2081 b i' is'\n-- (ft\u2081^ ++ os\u2081')\n-- (tail\u2081 os\u2082)\n-- (await b i' is' ds)\n-- (corrupt (ft\u2081^ ++ os\u2081') \u00b7 await b i' is' ds)\n-- (ack b \u00b7 (corrupt (ft\u2081^ ++ os\u2081') \u00b7 await b i' is' ds))\n-- (corrupt (tail\u2081 os\u2082) \u00b7\n-- (ack b \u00b7 (corrupt (ft\u2081^ ++ os\u2081') \u00b7 await b i' is' ds)))\n-- js Bb ft\u2081^++-os\u2081'-Fair ((tail-Fair Fos\u2082)) ihS\n-- where\n-- ft\u2081^++-os\u2081'-Fair : Fair (ft\u2081^ ++ os\u2081')\n-- ft\u2081^++-os\u2081'-Fair = Fair-in (ft\u2081^ , os\u2081' , FTft\u2081 , refl , Fos\u2081')\n\n-- postulate os\u2081-eq-helper : os\u2081 \u2261 F \u2237 os\u2081^ os\u2081' ft\u2081^\n-- {-# ATP prove os\u2081-eq-helper #-}\n\n-- postulate as-eq : as \u2261 < i' , b > \u2237 (as^ b i' is' ds)\n-- {-# ATP prove as-eq #-}\n\n-- postulate bs-eq : bs \u2261 error \u2237 (bs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^))\n-- {-# ATP prove bs-eq os\u2081-eq-helper as-eq #-}\n\n-- postulate cs-eq : cs \u2261 not b \u2237 cs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^)\n-- {-# ATP prove cs-eq bs-eq #-}\n\n-- postulate\n-- ds-eq-helper\u2081 :\n-- os\u2082 \u2261 T \u2237 tail\u2081 os\u2082 \u2192\n-- ds \u2261 ok (not b) \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n-- {-# ATP prove ds-eq-helper\u2081 cs-eq #-}\n\n-- postulate\n-- ds-eq-helper\u2082 : os\u2082 \u2261 F \u2237 tail\u2081 os\u2082 \u2192\n-- ds \u2261 error \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n-- {-# ATP prove ds-eq-helper\u2082 cs-eq #-}\n\n-- ds-eq : ds \u2261 ok (not b) \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n-- \u2228 ds \u2261 error \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n-- ds-eq = case (\u03bb h \u2192 inj\u2081 (ds-eq-helper\u2081 h))\n-- (\u03bb h \u2192 inj\u2082 (ds-eq-helper\u2082 h))\n-- (head-tail-Fair Fos\u2082)\n\n-- postulate\n-- as^-eq-helper\u2081 :\n-- ds \u2261 ok (not b) \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082) \u2192\n-- as^ b i' is' ds \u2261\n-- send b \u00b7 (i' \u2237 is') \u00b7 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n-- {-# ATP prove as^-eq-helper\u2081 x\u2262not-x #-}\n\n-- postulate\n-- as^-eq-helper\u2082 :\n-- ds \u2261 error \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082) \u2192\n-- as^ b i' is' ds \u2261\n-- send b \u00b7 (i' \u2237 is') \u00b7 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n-- {-# ATP prove as^-eq-helper\u2082 #-}\n\n-- as^-eq : as^ b i' is' ds \u2261\n-- send b \u00b7 (i' \u2237 is') \u00b7 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n-- as^-eq = case as^-eq-helper\u2081 as^-eq-helper\u2082 ds-eq\n\n-- postulate js-eq : js \u2261 out b \u00b7 bs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^)\n-- {-# ATP prove js-eq bs-eq #-}\n\n-- ihS : S b\n-- (i' \u2237 is')\n-- (os\u2081^ os\u2081' ft\u2081^)\n-- (os\u2082^ os\u2082)\n-- (as^ b i' is' ds)\n-- (bs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^))\n-- (cs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^))\n-- (ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082))\n-- js\n-- ihS = as^-eq , refl , refl , refl , js-eq\n","old_contents":"------------------------------------------------------------------------------\n-- ABP lemma 1\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- From Dybjer and Sander's paper: The first lemma states that given a\n-- start state S of the ABP, we will arrive at a state S', where the\n-- message has been received by the receiver, but where the\n-- acknowledgement has not yet been received by the sender.\n\nmodule FOT.FOTC.Program.ABP.Lemma1WithoutHelperATP where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Base.Loop\nopen import FOTC.Data.Bool\nopen import FOTC.Data.Bool.PropertiesATP using ( x\u2262not-x )\nopen import FOTC.Data.List\nopen import FOTC.Program.ABP.ABP\nopen import FOTC.Program.ABP.Fair.Type\nopen import FOTC.Program.ABP.Fair.PropertiesATP\nopen import FOTC.Program.ABP.Terms\n\n------------------------------------------------------------------------------\n\nas^ : \u2200 b i' is' ds \u2192 D\nas^ b i' is' ds = await b i' is' ds\n{-# ATP definition as^ #-}\n\nbs^ : D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D\nbs^ b i' is' ds os\u2081^ = corrupt os\u2081^ \u00b7 (as^ b i' is' ds)\n{-# ATP definition bs^ #-}\n\ncs^ : D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D\ncs^ b i' is' ds os\u2081^ = ack b \u00b7 (bs^ b i' is' ds os\u2081^)\n{-# ATP definition cs^ #-}\n\nds^ : D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D\nds^ b i' is' ds os\u2081^ os\u2082^ = corrupt os\u2082^ \u00b7 cs^ b i' is' ds os\u2081^\n{-# ATP definition ds^ #-}\n\nos\u2081^ : D \u2192 D \u2192 D\nos\u2081^ os\u2081' ft\u2081^ = ft\u2081^ ++ os\u2081'\n{-# ATP definition os\u2081^ #-}\n\nos\u2082^ : D \u2192 D\nos\u2082^ os\u2082 = tail\u2081 os\u2082\n{-# ATP definition os\u2082^ #-}\n\n-- Helper function for the ABP lemma 1\n-- module Helper where\n-- -- 30 November 2013. If we don't have the following definitions\n-- -- outside the where clause, the ATPs cannot prove the theorems.\n\n-- helper : \u2200 b i' is' os\u2081 os\u2082 as bs cs ds js \u2192\n-- Bit b \u2192\n-- Fair os\u2082 \u2192\n-- S b (i' \u2237 is') os\u2081 os\u2082 as bs cs ds js \u2192\n-- \u2200 ft\u2081 os\u2081' \u2192 F*T ft\u2081 \u2192 Fair os\u2081' \u2192 os\u2081 \u2261 ft\u2081 ++ os\u2081' \u2192\n-- \u2203[ os\u2081' ] \u2203[ os\u2082' ] \u2203[ as' ] \u2203[ bs' ] \u2203[ cs' ] \u2203[ ds' ] \u2203[ js' ]\n-- Fair os\u2081'\n-- \u2227 Fair os\u2082'\n-- \u2227 S' b i' is' os\u2081' os\u2082' as' bs' cs' ds' js'\n-- \u2227 js \u2261 i' \u2237 js'\n-- helper b i' is' os\u2081 os\u2082 as bs cs ds js\n-- Bb Fos\u2082 s .(T \u2237 []) os\u2081' f*tnil Fos\u2081' os\u2081-eq = prf\n-- where\n-- postulate\n-- prf : \u2203[ os\u2081' ] \u2203[ os\u2082' ] \u2203[ as' ] \u2203[ bs' ] \u2203[ cs' ] \u2203[ ds' ] \u2203[ js' ]\n-- Fair os\u2081'\n-- \u2227 Fair os\u2082'\n-- \u2227 (as' \u2261 await b i' is' ds'\n-- \u2227 bs' \u2261 corrupt os\u2081' \u00b7 as'\n-- \u2227 cs' \u2261 ack (not b) \u00b7 bs'\n-- \u2227 ds' \u2261 corrupt os\u2082' \u00b7 (b \u2237 cs')\n-- \u2227 js' \u2261 out (not b) \u00b7 bs')\n-- \u2227 js \u2261 i' \u2237 js'\n-- {-# ATP prove prf #-}\n\n-- -- TODO (25 January 2014): Why Agda cannot refine using the helper\n-- -- function? Agda bug?\n-- helper b i' is' os\u2081 os\u2082 as bs cs ds js Bb Fos\u2082 s\n-- .(F \u2237 ft\u2081^) os\u2081' (f*tcons {ft\u2081^} FTft\u2081^) Fos\u2081' os\u2081-eq =\n-- helper b i' is'\n-- (ft\u2081^ ++ os\u2081')\n-- (tail\u2081 os\u2082)\n-- (await b i' is' ds)\n-- (corrupt (ft\u2081^ ++ os\u2081') \u00b7 await b i' is' ds)\n-- (ack b \u00b7 (corrupt (ft\u2081^ ++ os\u2081') \u00b7 await b i' is' ds))\n-- (corrupt (tail\u2081 os\u2082) \u00b7\n-- (ack b \u00b7 (corrupt (ft\u2081^ ++ os\u2081') \u00b7 await b i' is' ds)))\n-- js\n-- Bb (tail-Fair Fos\u2082) ihS ft\u2081^ os\u2081' FTft\u2081^ Fos\u2081' refl\n-- where\n-- postulate os\u2081-eq-helper : os\u2081 \u2261 F \u2237 os\u2081^ os\u2081' ft\u2081^\n-- {-# ATP prove os\u2081-eq-helper #-}\n\n-- postulate as-eq : as \u2261 < i' , b > \u2237 (as^ b i' is' ds)\n-- {-# ATP prove as-eq #-}\n\n-- postulate bs-eq : bs \u2261 error \u2237 (bs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^))\n-- {-# ATP prove bs-eq os\u2081-eq-helper as-eq #-}\n\n-- postulate cs-eq : cs \u2261 not b \u2237 cs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^)\n-- {-# ATP prove cs-eq bs-eq #-}\n\n-- postulate\n-- ds-eq-helper\u2081 :\n-- os\u2082 \u2261 T \u2237 tail\u2081 os\u2082 \u2192\n-- ds \u2261 ok (not b) \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n-- {-# ATP prove ds-eq-helper\u2081 cs-eq #-}\n\n-- postulate\n-- ds-eq-helper\u2082 : os\u2082 \u2261 F \u2237 tail\u2081 os\u2082 \u2192\n-- ds \u2261 error \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n-- {-# ATP prove ds-eq-helper\u2082 cs-eq #-}\n\n-- ds-eq : ds \u2261 ok (not b) \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n-- \u2228 ds \u2261 error \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n-- ds-eq = case (\u03bb h \u2192 inj\u2081 (ds-eq-helper\u2081 h))\n-- (\u03bb h \u2192 inj\u2082 (ds-eq-helper\u2082 h))\n-- (head-tail-Fair Fos\u2082)\n\n-- postulate\n-- as^-eq-helper\u2081 :\n-- ds \u2261 ok (not b) \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082) \u2192\n-- as^ b i' is' ds \u2261\n-- send b \u00b7 (i' \u2237 is') \u00b7 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n-- {-# ATP prove as^-eq-helper\u2081 x\u2262not-x #-}\n\n-- postulate\n-- as^-eq-helper\u2082 :\n-- ds \u2261 error \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082) \u2192\n-- as^ b i' is' ds \u2261\n-- send b \u00b7 (i' \u2237 is') \u00b7 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n-- {-# ATP prove as^-eq-helper\u2082 #-}\n\n-- as^-eq : as^ b i' is' ds \u2261\n-- send b \u00b7 (i' \u2237 is') \u00b7 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n-- as^-eq = case as^-eq-helper\u2081 as^-eq-helper\u2082 ds-eq\n\n-- postulate js-eq : js \u2261 out b \u00b7 bs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^)\n-- {-# ATP prove js-eq bs-eq #-}\n\n-- ihS : S b\n-- (i' \u2237 is')\n-- (os\u2081^ os\u2081' ft\u2081^)\n-- (os\u2082^ os\u2082)\n-- (as^ b i' is' ds)\n-- (bs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^))\n-- (cs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^))\n-- (ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082))\n-- js\n-- ihS = as^-eq , refl , refl , refl , js-eq\n\n------------------------------------------------------------------------------\n-- From Dybjer and Sander's paper: From the assumption that os\u2081\u00a0\u2208\u00a0Fair\n-- and hence by unfolding Fair, we conclude that there are ft\u2081\u00a0:\u00a0 F*T\n-- and os\u2081'\u00a0:\u00a0Fair, such that os\u2081\u00a0=\u00a0ft\u2081\u00a0++ os\u2081'.\n--\n-- We proceed by induction on ft\u2081\u00a0:\u00a0F*T using helper.\n\n-- open Helper\nlemma\u2081 : \u2200 b i' is' os\u2081 os\u2082 as bs cs ds js \u2192\n Bit b \u2192\n Fair os\u2081 \u2192\n Fair os\u2082 \u2192\n S b (i' \u2237 is') os\u2081 os\u2082 as bs cs ds js \u2192\n \u2203[ os\u2081' ] \u2203[ os\u2082' ] \u2203[ as' ] \u2203[ bs' ] \u2203[ cs' ] \u2203[ ds' ] \u2203[ js' ]\n Fair os\u2081'\n \u2227 Fair os\u2082'\n \u2227 S' b i' is' os\u2081' os\u2082' as' bs' cs' ds' js'\n \u2227 js \u2261 i' \u2237 js'\nlemma\u2081 b i' is' os\u2081 os\u2082 as bs cs ds js Bb Fos\u2081 Fos\u2082 s with Fair-out Fos\u2081\n... | .(true \u2237 []) , os\u2081' , f*tnil , os\u2081\u2261ft\u2081++os\u2081' , Fos\u2081' = prf\n where\n postulate\n prf : \u2203[ os\u2081' ] \u2203[ os\u2082' ] \u2203[ as' ] \u2203[ bs' ] \u2203[ cs' ] \u2203[ ds' ] \u2203[ js' ]\n Fair os\u2081'\n \u2227 Fair os\u2082'\n \u2227 (as' \u2261 await b i' is' ds'\n \u2227 bs' \u2261 corrupt os\u2081' \u00b7 as'\n \u2227 cs' \u2261 ack (not b) \u00b7 bs'\n \u2227 ds' \u2261 corrupt os\u2082' \u00b7 (b \u2237 cs')\n \u2227 js' \u2261 out (not b) \u00b7 bs')\n \u2227 js \u2261 i' \u2237 js'\n {-# ATP prove prf #-}\n\n... | .(F \u2237 ft\u2081^) , os\u2081' , f*tcons {ft\u2081^} FTft\u2081 , os\u2081\u2261ft\u2081++os\u2081' , Fos\u2081' =\n lemma\u2081 b i' is'\n (ft\u2081^ ++ os\u2081')\n (tail\u2081 os\u2082)\n (await b i' is' ds)\n (corrupt (ft\u2081^ ++ os\u2081') \u00b7 await b i' is' ds)\n (ack b \u00b7 (corrupt (ft\u2081^ ++ os\u2081') \u00b7 await b i' is' ds))\n (corrupt (tail\u2081 os\u2082) \u00b7\n (ack b \u00b7 (corrupt (ft\u2081^ ++ os\u2081') \u00b7 await b i' is' ds)))\n js Bb ft\u2081^++-os\u2081'-Fair ((tail-Fair Fos\u2082)) ihS\n where\n ft\u2081^++-os\u2081'-Fair : Fair (ft\u2081^ ++ os\u2081')\n ft\u2081^++-os\u2081'-Fair = Fair-in (ft\u2081^ , os\u2081' , FTft\u2081 , refl , Fos\u2081')\n\n postulate os\u2081-eq-helper : os\u2081 \u2261 F \u2237 os\u2081^ os\u2081' ft\u2081^\n {-# ATP prove os\u2081-eq-helper #-}\n\n postulate as-eq : as \u2261 < i' , b > \u2237 (as^ b i' is' ds)\n {-# ATP prove as-eq #-}\n\n postulate bs-eq : bs \u2261 error \u2237 (bs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^))\n {-# ATP prove bs-eq os\u2081-eq-helper as-eq #-}\n\n postulate cs-eq : cs \u2261 not b \u2237 cs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^)\n {-# ATP prove cs-eq bs-eq #-}\n\n postulate\n ds-eq-helper\u2081 :\n os\u2082 \u2261 T \u2237 tail\u2081 os\u2082 \u2192\n ds \u2261 ok (not b) \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n {-# ATP prove ds-eq-helper\u2081 cs-eq #-}\n\n postulate\n ds-eq-helper\u2082 : os\u2082 \u2261 F \u2237 tail\u2081 os\u2082 \u2192\n ds \u2261 error \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n {-# ATP prove ds-eq-helper\u2082 cs-eq #-}\n\n ds-eq : ds \u2261 ok (not b) \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n \u2228 ds \u2261 error \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n ds-eq = case (\u03bb h \u2192 inj\u2081 (ds-eq-helper\u2081 h))\n (\u03bb h \u2192 inj\u2082 (ds-eq-helper\u2082 h))\n (head-tail-Fair Fos\u2082)\n\n postulate\n as^-eq-helper\u2081 :\n ds \u2261 ok (not b) \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082) \u2192\n as^ b i' is' ds \u2261\n send b \u00b7 (i' \u2237 is') \u00b7 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n {-# ATP prove as^-eq-helper\u2081 x\u2262not-x #-}\n\n postulate\n as^-eq-helper\u2082 :\n ds \u2261 error \u2237 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082) \u2192\n as^ b i' is' ds \u2261\n send b \u00b7 (i' \u2237 is') \u00b7 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n {-# ATP prove as^-eq-helper\u2082 #-}\n\n as^-eq : as^ b i' is' ds \u2261\n send b \u00b7 (i' \u2237 is') \u00b7 ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082)\n as^-eq = case as^-eq-helper\u2081 as^-eq-helper\u2082 ds-eq\n\n postulate js-eq : js \u2261 out b \u00b7 bs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^)\n {-# ATP prove js-eq bs-eq #-}\n\n ihS : S b\n (i' \u2237 is')\n (os\u2081^ os\u2081' ft\u2081^)\n (os\u2082^ os\u2082)\n (as^ b i' is' ds)\n (bs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^))\n (cs^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^))\n (ds^ b i' is' ds (os\u2081^ os\u2081' ft\u2081^) (os\u2082^ os\u2082))\n js\n ihS = as^-eq , refl , refl , refl , js-eq\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"fbbd955ba2c82259764f220c075ee5ab09f0a3c1","subject":"Update Desc model. Follow ICFP paper. With type-in-type, but termination-checker-friendly.","message":"Update Desc model. Follow ICFP paper. With type-in-type, but termination-checker-friendly.\n","repos":"mietek\/epigram2,larrytheliquid\/pigit,mietek\/epigram2","old_file":"models\/Desc.agda","new_file":"models\/Desc.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule Desc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata Desc : Set where\n id : Desc\n const : Set -> Desc\n prod : Desc -> Desc -> Desc\n sigma : (S : Set) -> (S -> Desc) -> Desc\n pi : (S : Set) -> (S -> Desc) -> Desc\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|]_ : Desc -> Set -> Set\n[| id |] Z = Z\n[| const X |] Z = X\n[| prod D D' |] Z = [| D |] Z * [| D' |] Z\n[| sigma S T |] Z = Sigma S (\\s -> [| T s |] Z)\n[| pi S T |] Z = (s : S) -> [| T s |] Z\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata Mu (D : Desc) : Set where\n con : [| D |] (Mu D) -> Mu D\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : (D : Desc)(X : Set)(P : X -> Set) -> [| D |] X -> Set\nAll id X P x = P x\nAll (const Z) X P x = Z\nAll (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')\nAll (sigma S T) X P (a , b) = All (T a) X P b\nAll (pi S T) X P f = (s : S) -> All (T s) X P (f s)\n\nall : (D : Desc)(X : Set)(P : X -> Set)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x\nall id X P R x = R x\nall (const Z) X P R z = z\nall (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'\nall (sigma S T) X P R (a , b) = all (T a) X P R b\nall (pi S T) X P R f = \\ s -> all (T s) X P R (f s)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n{-\ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms (con xs) = ms xs (all D (Mu D) P (\\x -> induction D P ms x) xs)\n-}\n\nmodule Elim (D : Desc)\n (P : Mu D -> Set)\n (ms : (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x))\n where\n\n mutual\n induction : (x : Mu D) -> P x\n induction (con xs) = ms xs (hyps D xs)\n \n hyps : (D' : Desc)\n (xs : [| D' |] (Mu D)) ->\n All D' (Mu D) P xs\n hyps id x = induction x\n hyps (const Z) z = z\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (sigma S T) (a , b) = hyps (T a) b\n hyps (pi S T) f = \\s -> hyps (T s) (f s)\n \ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms x = Elim.induction D P ms x\n\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Suc : NatConst\n\nnatCases : NatConst -> Desc\nnatCases Ze = const Unit\nnatCases Suc = id\n\nNatD : Desc\nNatD = sigma NatConst natCases\n\nNat : Set\nNat = Mu NatD\n\nze : Nat\nze = con (Ze , Void)\n\nsuc : Nat -> Nat\nsuc n = con (Suc , n)\n\n--****************\n-- List\n--****************\n\ndata ListConst : Set where\n Nil : ListConst\n Cons : ListConst\n\nlistCases : Set -> ListConst -> Desc\nlistCases X Nil = const Unit\nlistCases X Cons = sigma X (\\_ -> id)\n\nListD : Set -> Desc\nListD X = sigma ListConst (listCases X)\n\nList : Set -> Set\nList X = Mu (ListD X)\n\nnil : {X : Set} -> List X\nnil = con ( Nil , Void )\n\ncons : {X : Set} -> X -> List X -> List X\ncons x t = con ( Cons , ( x , t ))\n\n--****************\n-- Tree\n--****************\n\ndata TreeConst : Set where\n Leaf : TreeConst\n Node : TreeConst\n\ntreeCases : Set -> TreeConst -> Desc\ntreeCases X Leaf = const Unit\ntreeCases X Node = sigma X (\\_ -> prod id id)\n\nTreeD : Set -> Desc\nTreeD X = sigma TreeConst (treeCases X)\n\nTree : Set -> Set\nTree X = Mu (TreeD X)\n\nleaf : {X : Set} -> Tree X\nleaf = con (Leaf , Void)\n\nnode : {X : Set} -> X -> Tree X -> Tree X -> Tree X\nnode x le ri = con (Node , (x , (le , ri)))\n\n--********************************************\n-- Finite sets\n--********************************************\n\nEnumU : Set\nEnumU = Nat\n\nnilE : EnumU\nnilE = ze\n\nconsE : EnumU -> EnumU\nconsE e = suc e\n\n{-\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n-}\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\ncasesSpi : (xs : [| NatD |] Nat) -> \n All NatD Nat (\\e -> (P' : EnumT e -> Set) -> Set) xs -> \n (P' : EnumT (con xs) -> Set) -> Set\ncasesSpi (Ze , Void) hs P' = Unit\ncasesSpi (Suc , n) hs P' = P' EZe * hs (\\e -> P' (ESu e))\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi e P = induction NatD (\\e -> (P : EnumT e -> Set) -> Set) casesSpi e P\n\n{-\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n-}\n\n\ncasesSwitch : (xs : [| NatD |] Nat) ->\n All NatD Nat (\\e -> (P' : EnumT e -> Set)(b' : spi e P')(x' : EnumT e) -> P' x') xs ->\n (P' : EnumT (con xs) -> Set)(b' : spi (con xs) P')(x' : EnumT (con xs)) -> P' x'\ncasesSwitch (Ze , Void) hs P' b' ()\ncasesSwitch (Suc , n) hs P' b' EZe = fst b'\ncasesSwitch (Suc , n) hs P' b' (ESu e') = hs (\\e -> P' (ESu e)) (snd b') e'\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch e P b x = induction NatD (\\e -> (P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x) casesSwitch e P b x\n\n{-\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n-}\n\n--********************************************\n-- Tagged description\n--********************************************\n\nTagDesc : Set\nTagDesc = Sigma EnumU (\\e -> spi e (\\_ -> Desc))\n\ntoDesc : TagDesc -> Desc\ntoDesc (B , F) = sigma (EnumT B) (\\e -> switch B (\\_ -> Desc) F e)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (D : Desc)\n (T : Set) ->\n ([| D |] T -> T) ->\n (Mu D) -> T\ncata D T phi x = induction D (\\_ -> T) (\\x ms -> phi (replace D T x ms )) x\n where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\\_ -> T) xs) -> [| D' |] T\n replace id T x y = y\n replace (const Z) T z z' = z'\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : TagDesc -> (X : Set) -> TagDesc\n(e , D) ** X = consE e , (const X , D)\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : (D : TagDesc)(X Y : Set) ->\n (X -> Mu (toDesc (D ** Y))) ->\n [| toDesc (D ** X) |] (Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\napply (E , B) X Y sig (EZe , x) = sig x\napply (E , B) X Y sig (ESu n , t) = con (ESu n , t)\n\nsubst : (D : TagDesc)(X Y : Set) ->\n Mu (toDesc (D ** X)) ->\n (X -> Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\nsubst D X Y x sig = cata (toDesc (D ** X)) (Mu (toDesc (D ** Y))) (apply D X Y sig) x\n","old_contents":"{-# OPTIONS --type-in-type\n --no-termination-check\n --no-positivity-check #-}\n\nmodule Desc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\nrecord One : Set where\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n\n\n--********************************************\n-- Desc code\n--********************************************\n\n-- Inductive types are implemented as a Universe. Hence, in this\n-- section, we implement their code.\n\n-- We can read this code as follow (see Conor's \"Ornamental\n-- algebras\"): a description in |Desc| is a program to read one node\n-- of the described tree.\n\ndata Desc : Set where\n Arg : (X : Set) -> (X -> Desc) -> Desc\n -- Read a field in |X|; continue, given its value\n\n -- Often, |X| is an |EnumT|, hence allowing to choose a constructor\n -- among a finite set of constructors\n\n Ind : (H : Set) -> Desc -> Desc\n -- Read a field in H; read a recursive subnode given the field,\n -- continue regarless of the subnode\n\n -- Often, |H| is |1|, hence |Ind| simplifies to |Inf : Desc -> Desc|,\n -- meaning: read a recursive subnode and continue regardless\n\n Done : Desc\n -- Stop reading\n\n\n--********************************************\n-- Desc decoder\n--********************************************\n\n-- Provided the type of the recursive subnodes |R|, we decode a\n-- description as a record describing the node.\n\n[|_|]_ : Desc -> Set -> Set\n[| Arg A D |] R = Sigma A (\\ a -> [| D a |] R)\n[| Ind H D |] R = (H -> R) * [| D |] R\n[| Done |] R = One\n\n\n--********************************************\n-- Functions on codes\n--********************************************\n\n-- Saying that a \"predicate\" |p| holds everywhere in |v| amounts to\n-- write something of the following type:\n\nEverywhere : (d : Desc) (D : Set) (bp : D -> Set) (V : [| d |] D) -> Set\nEverywhere (Arg A f) d p v = Everywhere (f (fst v)) d p (snd v)\n-- It must hold for this constructor\n\nEverywhere (Ind H x) d p v = ((y : H) -> p (fst v y)) * Everywhere x d p (snd v)\n-- It must hold for the subtrees \n\nEverywhere Done _ _ _ = _\n-- It trivially holds at endpoints\n\n-- Then, we can build terms that inhabits this type. That is, a\n-- function that takes a \"predicate\" |bp| and makes it hold everywhere\n-- in the data-structure. It is the equivalent of a \"map\", but in a\n-- dependently-typed setting.\n\neverywhere : (d : Desc) (D : Set) (bp : D -> Set) ->\n ((y : D) -> bp y) -> (v : [| d |] D) ->\n Everywhere d D bp v\neverywhere (Arg a f) d bp p v = everywhere (f (fst v)) d bp p (snd v)\n-- It holds everywhere on this constructor\n\neverywhere (Ind H x) d bp p v = (\\y -> p (fst v y)) , everywhere x d bp p (snd v)\n-- It holds here, and down in the recursive subtrees\n\neverywhere Done _ _ _ _ = One\n-- Nothing needs to be done on endpoints\n\n\n-- Looking at the decoder, a natural thing to do is to define its\n-- fixpoint |Mu D|, hence instantiating |R| with |Mu D| itself.\n\ndata Mu (D : Desc) : Set where\n Con : [| D |] (Mu D) -> Mu D\n\n-- Using the \"map\" defined by |everywhere|, we can implement a \"fold\"\n-- over the |Mu| fixpoint:\n\nfoldDesc : (D : Desc) (bp : Mu D -> Set) ->\n ((x : [| D |] (Mu D)) -> Everywhere D (Mu D) bp x -> bp (Con x)) ->\n (v : Mu D) ->\n bp v\nfoldDesc D bp p (Con v) = p v (everywhere D (Mu D) bp (\\x -> foldDesc D bp p x) v) \n\n\n--********************************************\n-- Nat\n--********************************************\n\ndata NatConst : Set where\n ZE : NatConst\n SU : NatConst\n\nnatc : NatConst -> Desc\nnatc ZE = Done\nnatc SU = Ind One Done\n\nnatd : Desc\nnatd = Arg NatConst natc\n\nnat : Set\nnat = Mu natd\n\nzero : nat\nzero = Con ( ZE , _ )\n\nsuc : nat -> nat\nsuc n = Con ( SU , ( (\\_ -> n) , _ ) )\n\ntwo : nat\ntwo = suc (suc zero)\n\nfour : nat\nfour = suc (suc (suc (suc zero)))\n\nsum : nat -> \n ((x : Sigma NatConst (\\ a -> [| natc a |] Mu (Arg NatConst natc))) ->\n Everywhere (natc (fst x)) (Mu (Arg NatConst natc)) (\\ _ -> Mu (Arg NatConst natc)) (snd x) ->\n Mu (Arg NatConst natc))\nsum n2 (ZE , _) p = n2\nsum n2 (SU , f) p = suc ( fst p _) \n\n\nplus : nat -> nat -> nat\nplus n1 n2 = foldDesc natd (\\_ -> nat) (sum n2) n1 \n\nx : nat\nx = plus two two\n\n\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"656ee7f653c5f51df1fca8401f63c98f11f269c7","subject":"Environment changes: define \u229a without \u229d","message":"Environment changes: define \u229a without \u229d\n\nDefine \u229a as in terms of \u229a for entries, not in terms of \u229d on\nenvironments.\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/LangChanges.agda","new_file":"Thesis\/LangChanges.agda","new_contents":"","old_contents":"","returncode":0,"stderr":"unknown","license":"mit","lang":"Agda"} {"commit":"bb08669e1bdc2f7d2df9bb9289bda1b42f3dece8","subject":"Alegbra.FunctionProperties.Eq: add Endo.Cycle","message":"Alegbra.FunctionProperties.Eq: add Endo.Cycle\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Algebra\/FunctionProperties\/Eq.agda","new_file":"lib\/Algebra\/FunctionProperties\/Eq.agda","new_contents":"{-# OPTIONS --without-K #-}\n-- Like Algebra.FunctionProperties but specialized to _\u2261_ and using implict arguments\n-- Moreover there is some extensions such as:\n-- * interchange\n-- * non-zero inversions\n-- * cancelation and non-zero cancelation\n\n-- These properties can (for instance) be used to define algebraic\n-- structures.\n\nopen import Level\nopen import Function.NP using (_$\u27e8_\u27e9_; flip)\nopen import Data.Nat.Base using (\u2115)\nopen import Data.Product\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nopen import Relation.Binary.PropositionalEquality.NP\n\n-- The properties are specified using the following relation as\n-- \"equality\".\n\nmodule Algebra.FunctionProperties.Eq {a} {A : Set a} where\n\n------------------------------------------------------------------------\n-- Unary and binary operations\n\nopen import Algebra.FunctionProperties.Core public\n\n------------------------------------------------------------------------\n-- Properties of operations\n\nAssociative : Op\u2082 A \u2192 Set _\nAssociative _\u00b7_ = \u2200 {x y z} \u2192 ((x \u00b7 y) \u00b7 z) \u2261 (x \u00b7 (y \u00b7 z))\n\nCommutative : Op\u2082 A \u2192 Set _\nCommutative _\u00b7_ = \u2200 {x y} \u2192 (x \u00b7 y) \u2261 (y \u00b7 x)\n\nLeftIdentity : A \u2192 Op\u2082 A \u2192 Set _\nLeftIdentity e _\u00b7_ = \u2200 {x} \u2192 (e \u00b7 x) \u2261 x\n\nRightIdentity : A \u2192 Op\u2082 A \u2192 Set _\nRightIdentity e _\u00b7_ = \u2200 {x} \u2192 (x \u00b7 e) \u2261 x\n\nIdentity : A \u2192 Op\u2082 A \u2192 Set _\nIdentity e \u00b7 = LeftIdentity e \u00b7 \u00d7 RightIdentity e \u00b7\n\nLeftZero : A \u2192 Op\u2082 A \u2192 Set _\nLeftZero z _\u00b7_ = \u2200 {x} \u2192 (z \u00b7 x) \u2261 z\n\nRightZero : A \u2192 Op\u2082 A \u2192 Set _\nRightZero z _\u00b7_ = \u2200 {x} \u2192 (x \u00b7 z) \u2261 z\n\nZero : A \u2192 Op\u2082 A \u2192 Set _\nZero z \u00b7 = LeftZero z \u00b7 \u00d7 RightZero z \u00b7\n\nLeftInverse : A \u2192 Op\u2081 A \u2192 Op\u2082 A \u2192 Set _\nLeftInverse e _\u207b\u00b9 _\u00b7_ = \u2200 {x} \u2192 (x \u207b\u00b9 \u00b7 x) \u2261 e\n\nLeftInverseNonZero : (zero e : A) \u2192 Op\u2081 A \u2192 Op\u2082 A \u2192 Set _\nLeftInverseNonZero zero e _\u207b\u00b9 _\u00b7_ = \u2200 {x} \u2192 x \u2262 zero \u2192 (x \u207b\u00b9 \u00b7 x) \u2261 e\n\nRightInverse : A \u2192 Op\u2081 A \u2192 Op\u2082 A \u2192 Set _\nRightInverse e _\u207b\u00b9 _\u00b7_ = \u2200 {x} \u2192 (x \u00b7 (x \u207b\u00b9)) \u2261 e\n\nRightInverseNonZero : (zero e : A) \u2192 Op\u2081 A \u2192 Op\u2082 A \u2192 Set _\nRightInverseNonZero zero e _\u207b\u00b9 _\u00b7_ = \u2200 {x} \u2192 x \u2262 zero \u2192 (x \u00b7 (x \u207b\u00b9)) \u2261 e\n\nInverse : A \u2192 Op\u2081 A \u2192 Op\u2082 A \u2192 Set _\nInverse e \u207b\u00b9 \u00b7 = LeftInverse e \u207b\u00b9 \u00b7 \u00d7 RightInverse e \u207b\u00b9 \u00b7\n\nInverseNonZero : (zero e : A) \u2192 Op\u2081 A \u2192 Op\u2082 A \u2192 Set _\nInverseNonZero zero e \u207b\u00b9 \u00b7 = LeftInverse e \u207b\u00b9 \u00b7 \u00d7 RightInverse e \u207b\u00b9 \u00b7\n\n_DistributesOver\u02e1_ : Op\u2082 A \u2192 Op\u2082 A \u2192 Set _\n_*_ DistributesOver\u02e1 _+_ =\n \u2200 {x y z} \u2192 (x * (y + z)) \u2261 ((x * y) + (x * z))\n\n_DistributesOver\u02b3_ : Op\u2082 A \u2192 Op\u2082 A \u2192 Set _\n_*_ DistributesOver\u02b3 _+_ =\n \u2200 {x y z} \u2192 ((y + z) * x) \u2261 ((y * x) + (z * x))\n\n_DistributesOver_ : Op\u2082 A \u2192 Op\u2082 A \u2192 Set _\n* DistributesOver + = (* DistributesOver\u02e1 +) \u00d7 (* DistributesOver\u02b3 +)\n\n_IdempotentOn_ : Op\u2082 A \u2192 A \u2192 Set _\n_\u00b7_ IdempotentOn x = (x \u00b7 x) \u2261 x\n\nIdempotent : Op\u2082 A \u2192 Set _\nIdempotent \u00b7 = \u2200 {x} \u2192 \u00b7 IdempotentOn x\n\nIdempotentFun : Op\u2081 A \u2192 Set _\nIdempotentFun f = \u2200 {x} \u2192 f (f x) \u2261 f x\n\n_Absorbs_ : Op\u2082 A \u2192 Op\u2082 A \u2192 Set _\n_\u00b7_ Absorbs _\u2218_ = \u2200 {x y} \u2192 (x \u00b7 (x \u2218 y)) \u2261 x\n\nAbsorptive : Op\u2082 A \u2192 Op\u2082 A \u2192 Set _\nAbsorptive \u00b7 \u2218 = (\u00b7 Absorbs \u2218) \u00d7 (\u2218 Absorbs \u00b7)\n\nInvolutive : Op\u2081 A \u2192 Set _\nInvolutive f = \u2200 {x} \u2192 f (f x) \u2261 x\n\nInterchange : Op\u2082 A \u2192 Op\u2082 A \u2192 Set _\nInterchange _\u00b7_ _\u2218_ = \u2200 {x y z t} \u2192 ((x \u00b7 y) \u2218 (z \u00b7 t)) \u2261 ((x \u2218 z) \u00b7 (y \u2218 t))\n\nLeftCancel : Op\u2082 A \u2192 Set _\nLeftCancel _\u00b7_ = \u2200 {c x y} \u2192 c \u00b7 x \u2261 c \u00b7 y \u2192 x \u2261 y\n\nRightCancel : Op\u2082 A \u2192 Set _\nRightCancel _\u00b7_ = \u2200 {c x y} \u2192 x \u00b7 c \u2261 y \u00b7 c \u2192 x \u2261 y\n\nLeftCancelNonZero : A \u2192 Op\u2082 A \u2192 Set _\nLeftCancelNonZero zero _\u00b7_ = \u2200 {c x y} \u2192 c \u2262 zero \u2192 c \u00b7 x \u2261 c \u00b7 y \u2192 x \u2261 y\n\nRightCancelNonZero : A \u2192 Op\u2082 A \u2192 Set _\nRightCancelNonZero zero _\u00b7_ = \u2200 {c x y} \u2192 c \u2262 zero \u2192 x \u00b7 c \u2261 y \u00b7 c \u2192 x \u2261 y\n\nmodule InterchangeFromAssocComm\n (_\u00b7_ : Op\u2082 A)\n (\u00b7-assoc : Associative _\u00b7_)\n (\u00b7-comm : Commutative _\u00b7_)\n where\n\n open \u2261-Reasoning\n\n \u00b7= : \u2200 {x x' y y'} \u2192 x \u2261 x' \u2192 y \u2261 y' \u2192 (x \u00b7 y) \u2261 (x' \u00b7 y')\n \u00b7= refl refl = refl\n\n \u00b7-interchange : Interchange _\u00b7_ _\u00b7_\n \u00b7-interchange {x} {y} {z} {t}\n = (x \u00b7 y) \u00b7 (z \u00b7 t)\n \u2261\u27e8 \u00b7-assoc \u27e9\n x \u00b7 (y \u00b7 (z \u00b7 t))\n \u2261\u27e8 \u00b7= refl (! \u00b7-assoc) \u27e9\n x \u00b7 ((y \u00b7 z) \u00b7 t)\n \u2261\u27e8 \u00b7= refl (\u00b7= \u00b7-comm refl) \u27e9\n x \u00b7 ((z \u00b7 y) \u00b7 t)\n \u2261\u27e8 \u00b7= refl \u00b7-assoc \u27e9\n x \u00b7 (z \u00b7 (y \u00b7 t))\n \u2261\u27e8 ! \u00b7-assoc \u27e9\n (x \u00b7 z) \u00b7 (y \u00b7 t)\n \u220e\n\nmodule _ {b} {B : Set b} (f : A \u2192 B) where\n\n Injective : Set (b \u2294 a)\n Injective = \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y\n\n Conflict : Set (b \u2294 a)\n Conflict = \u2203 \u03bb x \u2192 \u2203 \u03bb y \u2192 (x \u2262 y) \u00d7 f x \u2261 f y\n\nmodule _ {b} {B : Set b} {f : A \u2192 B} where\n Injective-\u00acConflict : Injective f \u2192 \u00ac (Conflict f)\n Injective-\u00acConflict inj (x , y , x\u2262y , fx\u2261fy) = x\u2262y (inj fx\u2261fy)\n\n Conflict-\u00acInjective : Conflict f \u2192 \u00ac (Injective f)\n Conflict-\u00acInjective = flip Injective-\u00acConflict\n\nmodule Endo {f : A \u2192 A} where\n Cycle^ : \u2115 \u2192 Set _\n Cycle^ n = \u2203 \u03bb x \u2192 f $\u27e8 n \u27e9 x \u2261 x\n\n Cycle = \u2203 Cycle^\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\n-- Like Algebra.FunctionProperties but specialized to _\u2261_ and using implict arguments\n-- Moreover there is some extensions such as:\n-- * interchange\n-- * non-zero inversions\n-- * cancelation and non-zero cancelation\n\n-- These properties can (for instance) be used to define algebraic\n-- structures.\n\nopen import Level\nopen import Function using (flip)\nopen import Data.Product\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nopen import Relation.Binary.PropositionalEquality.NP\n\n-- The properties are specified using the following relation as\n-- \"equality\".\n\nmodule Algebra.FunctionProperties.Eq {a} {A : Set a} where\n\n------------------------------------------------------------------------\n-- Unary and binary operations\n\nopen import Algebra.FunctionProperties.Core public\n\n------------------------------------------------------------------------\n-- Properties of operations\n\nAssociative : Op\u2082 A \u2192 Set _\nAssociative _\u00b7_ = \u2200 {x y z} \u2192 ((x \u00b7 y) \u00b7 z) \u2261 (x \u00b7 (y \u00b7 z))\n\nCommutative : Op\u2082 A \u2192 Set _\nCommutative _\u00b7_ = \u2200 {x y} \u2192 (x \u00b7 y) \u2261 (y \u00b7 x)\n\nLeftIdentity : A \u2192 Op\u2082 A \u2192 Set _\nLeftIdentity e _\u00b7_ = \u2200 {x} \u2192 (e \u00b7 x) \u2261 x\n\nRightIdentity : A \u2192 Op\u2082 A \u2192 Set _\nRightIdentity e _\u00b7_ = \u2200 {x} \u2192 (x \u00b7 e) \u2261 x\n\nIdentity : A \u2192 Op\u2082 A \u2192 Set _\nIdentity e \u00b7 = LeftIdentity e \u00b7 \u00d7 RightIdentity e \u00b7\n\nLeftZero : A \u2192 Op\u2082 A \u2192 Set _\nLeftZero z _\u00b7_ = \u2200 {x} \u2192 (z \u00b7 x) \u2261 z\n\nRightZero : A \u2192 Op\u2082 A \u2192 Set _\nRightZero z _\u00b7_ = \u2200 {x} \u2192 (x \u00b7 z) \u2261 z\n\nZero : A \u2192 Op\u2082 A \u2192 Set _\nZero z \u00b7 = LeftZero z \u00b7 \u00d7 RightZero z \u00b7\n\nLeftInverse : A \u2192 Op\u2081 A \u2192 Op\u2082 A \u2192 Set _\nLeftInverse e _\u207b\u00b9 _\u00b7_ = \u2200 {x} \u2192 (x \u207b\u00b9 \u00b7 x) \u2261 e\n\nLeftInverseNonZero : (zero e : A) \u2192 Op\u2081 A \u2192 Op\u2082 A \u2192 Set _\nLeftInverseNonZero zero e _\u207b\u00b9 _\u00b7_ = \u2200 {x} \u2192 x \u2262 zero \u2192 (x \u207b\u00b9 \u00b7 x) \u2261 e\n\nRightInverse : A \u2192 Op\u2081 A \u2192 Op\u2082 A \u2192 Set _\nRightInverse e _\u207b\u00b9 _\u00b7_ = \u2200 {x} \u2192 (x \u00b7 (x \u207b\u00b9)) \u2261 e\n\nRightInverseNonZero : (zero e : A) \u2192 Op\u2081 A \u2192 Op\u2082 A \u2192 Set _\nRightInverseNonZero zero e _\u207b\u00b9 _\u00b7_ = \u2200 {x} \u2192 x \u2262 zero \u2192 (x \u00b7 (x \u207b\u00b9)) \u2261 e\n\nInverse : A \u2192 Op\u2081 A \u2192 Op\u2082 A \u2192 Set _\nInverse e \u207b\u00b9 \u00b7 = LeftInverse e \u207b\u00b9 \u00b7 \u00d7 RightInverse e \u207b\u00b9 \u00b7\n\nInverseNonZero : (zero e : A) \u2192 Op\u2081 A \u2192 Op\u2082 A \u2192 Set _\nInverseNonZero zero e \u207b\u00b9 \u00b7 = LeftInverse e \u207b\u00b9 \u00b7 \u00d7 RightInverse e \u207b\u00b9 \u00b7\n\n_DistributesOver\u02e1_ : Op\u2082 A \u2192 Op\u2082 A \u2192 Set _\n_*_ DistributesOver\u02e1 _+_ =\n \u2200 {x y z} \u2192 (x * (y + z)) \u2261 ((x * y) + (x * z))\n\n_DistributesOver\u02b3_ : Op\u2082 A \u2192 Op\u2082 A \u2192 Set _\n_*_ DistributesOver\u02b3 _+_ =\n \u2200 {x y z} \u2192 ((y + z) * x) \u2261 ((y * x) + (z * x))\n\n_DistributesOver_ : Op\u2082 A \u2192 Op\u2082 A \u2192 Set _\n* DistributesOver + = (* DistributesOver\u02e1 +) \u00d7 (* DistributesOver\u02b3 +)\n\n_IdempotentOn_ : Op\u2082 A \u2192 A \u2192 Set _\n_\u00b7_ IdempotentOn x = (x \u00b7 x) \u2261 x\n\nIdempotent : Op\u2082 A \u2192 Set _\nIdempotent \u00b7 = \u2200 {x} \u2192 \u00b7 IdempotentOn x\n\nIdempotentFun : Op\u2081 A \u2192 Set _\nIdempotentFun f = \u2200 {x} \u2192 f (f x) \u2261 f x\n\n_Absorbs_ : Op\u2082 A \u2192 Op\u2082 A \u2192 Set _\n_\u00b7_ Absorbs _\u2218_ = \u2200 {x y} \u2192 (x \u00b7 (x \u2218 y)) \u2261 x\n\nAbsorptive : Op\u2082 A \u2192 Op\u2082 A \u2192 Set _\nAbsorptive \u00b7 \u2218 = (\u00b7 Absorbs \u2218) \u00d7 (\u2218 Absorbs \u00b7)\n\nInvolutive : Op\u2081 A \u2192 Set _\nInvolutive f = \u2200 {x} \u2192 f (f x) \u2261 x\n\nInterchange : Op\u2082 A \u2192 Op\u2082 A \u2192 Set _\nInterchange _\u00b7_ _\u2218_ = \u2200 {x y z t} \u2192 ((x \u00b7 y) \u2218 (z \u00b7 t)) \u2261 ((x \u2218 z) \u00b7 (y \u2218 t))\n\nLeftCancel : Op\u2082 A \u2192 Set _\nLeftCancel _\u00b7_ = \u2200 {c x y} \u2192 c \u00b7 x \u2261 c \u00b7 y \u2192 x \u2261 y\n\nRightCancel : Op\u2082 A \u2192 Set _\nRightCancel _\u00b7_ = \u2200 {c x y} \u2192 x \u00b7 c \u2261 y \u00b7 c \u2192 x \u2261 y\n\nLeftCancelNonZero : A \u2192 Op\u2082 A \u2192 Set _\nLeftCancelNonZero zero _\u00b7_ = \u2200 {c x y} \u2192 c \u2262 zero \u2192 c \u00b7 x \u2261 c \u00b7 y \u2192 x \u2261 y\n\nRightCancelNonZero : A \u2192 Op\u2082 A \u2192 Set _\nRightCancelNonZero zero _\u00b7_ = \u2200 {c x y} \u2192 c \u2262 zero \u2192 x \u00b7 c \u2261 y \u00b7 c \u2192 x \u2261 y\n\nmodule InterchangeFromAssocComm\n (_\u00b7_ : Op\u2082 A)\n (\u00b7-assoc : Associative _\u00b7_)\n (\u00b7-comm : Commutative _\u00b7_)\n where\n\n open \u2261-Reasoning\n\n \u00b7= : \u2200 {x x' y y'} \u2192 x \u2261 x' \u2192 y \u2261 y' \u2192 (x \u00b7 y) \u2261 (x' \u00b7 y')\n \u00b7= refl refl = refl\n\n \u00b7-interchange : Interchange _\u00b7_ _\u00b7_\n \u00b7-interchange {x} {y} {z} {t}\n = (x \u00b7 y) \u00b7 (z \u00b7 t)\n \u2261\u27e8 \u00b7-assoc \u27e9\n x \u00b7 (y \u00b7 (z \u00b7 t))\n \u2261\u27e8 \u00b7= refl (! \u00b7-assoc) \u27e9\n x \u00b7 ((y \u00b7 z) \u00b7 t)\n \u2261\u27e8 \u00b7= refl (\u00b7= \u00b7-comm refl) \u27e9\n x \u00b7 ((z \u00b7 y) \u00b7 t)\n \u2261\u27e8 \u00b7= refl \u00b7-assoc \u27e9\n x \u00b7 (z \u00b7 (y \u00b7 t))\n \u2261\u27e8 ! \u00b7-assoc \u27e9\n (x \u00b7 z) \u00b7 (y \u00b7 t)\n \u220e\n\nmodule _ {b} {B : Set b} (f : A \u2192 B) where\n\n Injective : Set (b \u2294 a)\n Injective = \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y\n\n Conflict : Set (b \u2294 a)\n Conflict = \u2203 \u03bb x \u2192 \u2203 \u03bb y \u2192 (x \u2262 y) \u00d7 f x \u2261 f y\n\nmodule _ {b} {B : Set b} {f : A \u2192 B} where\n Injective-\u00acConflict : Injective f \u2192 \u00ac (Conflict f)\n Injective-\u00acConflict inj (x , y , x\u2262y , fx\u2261fy) = x\u2262y (inj fx\u2261fy)\n\n Conflict-\u00acInjective : Conflict f \u2192 \u00ac (Injective f)\n Conflict-\u00acInjective = flip Injective-\u00acConflict\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"1101c4e828759adabf2e6d1502aa9a74f67ca917","subject":"progress cases, removing some redundant ones, adding a couple comments","message":"progress cases, removing some redundant ones, adding a couple comments\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress.agda","new_file":"progress.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import canonical-forms\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d val + d indet + d err[ \u0394 ] + \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n V : \u2200{d \u0394} \u2192 d val \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n E : \u2200{d \u0394} \u2192 \u0394 \u22a2 d err \u2192 ok d \u0394\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d') \u2192 ok d \u0394\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = V VConst\n\n -- variables\n progress (TAVar x) = abort (somenotnone (! x))\n\n -- lambdas\n progress (TALam D) = V VLam\n\n -- applications\n progress (TAAp D1 x D2)\n with progress D1 | progress D2\n -- left applicand value\n progress (TAAp TAConst () D2) | V VConst | _\n progress (TAAp {d2 = d2} D1 MAArr D2) | V (VLam {x = x} {d = d}) | V x\u2081 = S ([ d2 \/ x ] d , Step (FHAp1 (FVal VLam) {!!}) (ITLam (FVal x\u2081)) {!!} )\n progress (TAAp {d2 = d2} D1 MAArr D2) | V (VLam {x = x} {d = d}) | I x\u2081 = S ([ d2 \/ x ] d , Step (FHAp1 (FVal VLam) {!!}) (ITLam (FIndet x\u2081)) {!!} )\n -- errors propagate\n progress (TAAp D1 x\u2082 D2) | _ | E x\u2081 = E (EAp2 x\u2081)\n progress (TAAp D1 x\u2082 D2) | E x | _ = E (EAp1 x)\n -- indeterminates\n progress (TAAp D1 x\u2082 D2) | I i | V v = I (IAp i (FVal v))\n progress (TAAp D1 x\u2082 D2) | I x | I x\u2081 = I (IAp x (FIndet x\u2081))\n -- either applicand steps\n progress (TAAp {d1 = d1} D1 x\u2084 D2) | _ | S (d , Step x\u2081 x\u2082 x\u2083) = S ((d1 \u2218 d) , {!!})\n progress (TAAp {d2 = d2} D1 x\u2082 D2) | S (\u03c01 , \u03c02) | _ = S ((\u03c01 \u2218 d2) , {!!})\n\n -- empty holes\n progress (TAEHole {m = \u2713} x x\u2081) = I IEHole\n progress (TAEHole {m = \u2717} x x\u2081) = S (_ , Step FHEHole ITEHole FHEHole)\n\n -- non-empty holes\n progress (TANEHole x D x\u2081)\n with progress D\n progress (TANEHole {m = \u2713} x\u2081 D x\u2082) | V v = I (INEHole (FVal v))\n progress (TANEHole {m = \u2717} x\u2081 D x\u2082) | V v = S ( _ , Step (FHNEHoleFinal (FVal v)) (ITNEHole (FVal v)) FHNEHoleEvaled)\n progress (TANEHole {m = \u2713} x\u2081 D x\u2082) | I x = I (INEHole (FIndet x))\n progress (TANEHole {m = \u2717} x\u2081 D x\u2082) | I x = S (_ , Step (FHNEHoleFinal (FIndet x)) (ITNEHole (FIndet x)) FHNEHoleEvaled )\n progress (TANEHole x\u2081 D x\u2082) | E x = E (ENEHole x)\n progress (TANEHole {d = d} {u = u} {\u03c3 = \u03c3} {m = m} x\u2083 D x\u2084) | S (d' , Step x x\u2081 x\u2082) = S ( \u2987 d' \u2988\u27e8 u , \u03c3 , m \u27e9 , {!!}) -- maybe depends on m\n\n -- casts\n progress (TACast D x)\n with progress D\n progress (TACast TAConst con) | V VConst = S (c , Step (FHCastFinal (FVal VConst)) (ITCast (FVal VConst) TAConst con) (FHFinal (FVal VConst)))\n progress (TACast D m) | V VLam = S (_ , Step (FHCastFinal (FVal VLam)) (ITCast (FVal VLam) D m) (FHFinal (FVal VLam)))\n progress (TACast D x\u2081) | I x = I (ICast x)\n progress (TACast D x\u2081) | E x = E (ECastProp x)\n progress (TACast D x\u2083) | S (d , Step x x\u2081 x\u2082) = S (_ , Step (FHCast x) x\u2081 (FHCast x\u2082))\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import canonical-forms\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d val + d indet + d err[ \u0394 ] + \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n V : \u2200{d \u0394} \u2192 d val \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n E : \u2200{d \u0394} \u2192 \u0394 \u22a2 d err \u2192 ok d \u0394\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d') \u2192 ok d \u0394\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = V VConst\n\n -- variables\n progress (TAVar x) = abort (somenotnone (! x))\n\n -- lambdas\n progress (TALam D) = V VLam\n\n -- applications\n progress (TAAp D1 x D2)\n with progress D1 | progress D2\n progress (TAAp TAConst () D2) | V VConst | _\n progress (TAAp {d2 = d2} D1 MAArr D2) | V (VLam {x = x} {d = d}) | V x\u2081 = S ([ d2 \/ x ] d , Step (FHAp1 (FVal VLam) {!!}) (ITLam (FVal x\u2081)) {!!} )\n progress (TAAp {d2 = d2} D1 MAArr D2) | V (VLam {x = x} {d = d}) | I x\u2081 = S ([ d2 \/ x ] d , Step (FHAp1 (FVal VLam) {!!}) (ITLam (FIndet x\u2081)) {!!} )\n progress (TAAp D1 x\u2082 D2) | V x | S x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | _ | E x\u2081 = E (EAp2 x\u2081)\n progress (TAAp D1 x\u2082 D2) | E x | _ = E (EAp1 x)\n progress (TAAp D1 x\u2082 D2) | I IEHole | V x\u2081 = I (IAp IEHole (FVal x\u2081))\n progress (TAAp D1 x\u2082 D2) | I (INEHole x) | V x\u2081 = I (IAp (INEHole x) (FVal x\u2081))\n progress (TAAp D1 x\u2083 D2) | I (IAp x x\u2081) | V x\u2082 = I (IAp (IAp x x\u2081) (FVal x\u2082))\n progress (TAAp D1 x\u2082 D2) | I (ICast x) | V x\u2081 = I (IAp (ICast x) (FVal x\u2081))\n progress (TAAp D1 x\u2082 D2) | I x | I x\u2081 = I (IAp x (FIndet x\u2081))\n progress (TAAp {d1 = d1} D1 x\u2084 D2) | _ | S (d , Step x\u2081 x\u2082 x\u2083) = S ((d1 \u2218 d) , (Step {!!} (ITLam (FIndet {!!})) {!!}))\n progress (TAAp {d2 = d2} D1 x\u2082 D2) | S (\u03c01 , \u03c02) | _ = S ((\u03c01 \u2218 d2) , {!!})\n\n -- empty holes\n progress (TAEHole {m = \u2713} x x\u2081) = I IEHole\n progress (TAEHole {m = \u2717} x x\u2081) = S (_ , Step FHEHole ITEHole FHEHole)\n\n -- non-empty holes\n progress (TANEHole x D x\u2081)\n with progress D\n progress (TANEHole {m = \u2713} x\u2081 D x\u2082) | V v = I (INEHole (FVal v))\n progress (TANEHole {m = \u2717} x\u2081 D x\u2082) | V v = S ( _ , Step (FHNEHoleFinal (FVal v)) (ITNEHole (FVal v)) FHNEHoleEvaled)\n progress (TANEHole {m = \u2713} x\u2081 D x\u2082) | I x = I (INEHole (FIndet x))\n progress (TANEHole {m = \u2717} x\u2081 D x\u2082) | I x = {!!}\n progress (TANEHole x\u2081 D x\u2082) | E x = E (ENEHole x)\n progress (TANEHole x\u2083 D x\u2084) | S (d , Step x x\u2081 x\u2082) = S {!!} -- S (_ , (Step (FNEHole x) x\u2081 (FNEHole x\u2082)))\n\n -- casts\n progress (TACast D x)\n with progress D\n progress (TACast TAConst con) | V VConst = S (c , Step (FHCastFinal (FVal VConst)) (ITCast (FVal VConst) TAConst con) (FHFinal (FVal VConst)))\n progress (TACast D m) | V VLam = S (_ , Step (FHCastFinal (FVal VLam)) (ITCast (FVal VLam) D m) (FHFinal (FVal VLam)))\n progress (TACast D x\u2081) | I x = I (ICast x)\n progress (TACast D x\u2081) | E x = E (ECastProp x)\n progress (TACast D x\u2083) | S (d , Step x x\u2081 x\u2082) = S (_ , Step (FHCast x) x\u2081 (FHCast x\u2082))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a70fc323eaa6eec7d9c617ee3ccef5722e7d7af5","subject":"Data.Nat","message":"Data.Nat\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Nat\/NP.agda","new_file":"lib\/Data\/Nat\/NP.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Nat.NP where\n\nimport Algebra\nopen import Data.Nat public hiding (module GeneralisedArithmetic; module \u2264-Reasoning)\nopen import Data.Nat.Properties as Props\nopen import Data.Bool\nopen import Data.Product using (proj\u2081; proj\u2082; \u2203; _,_)\nopen import Data.Empty using (\u22a5-elim)\nopen import Function.NP\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_; module \u2261-Reasoning)\n\nmodule \u2115\u00b0 = Algebra.CommutativeSemiring Props.commutativeSemiring\nmodule \u2115cmp = StrictTotalOrder Props.strictTotalOrder\nmodule \u2115\u2264 = DecTotalOrder decTotalOrder\nmodule \u2294\u00b0 = Algebra.CommutativeSemiringWithoutOne \u2294-\u2293-0-commutativeSemiringWithoutOne\n\nmodule \u2264-Reasoning where\n open Preorder-Reasoning \u2115\u2264.preorder public renaming (_\u223c\u27e8_\u27e9_ to _\u2264\u27e8_\u27e9_)\n infixr 2 _\u2261\u27e8_\u27e9_\n _\u2261\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x \u2261 y \u2192 y \u2264 z \u2192 x \u2264 z\n _ \u2261\u27e8 \u2261.refl \u27e9 p = p\n infixr 2 _<\u27e8_\u27e9_\n _<\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x < y \u2192 y \u2264 z \u2192 x < z\n x <\u27e8 p \u27e9 q = suc x \u2264\u27e8 p \u27e9 q\n\nsuc-injective : \u2200 {n m : \u2115} \u2192 (suc n \u2236 \u2115) \u2261 suc m \u2192 n \u2261 m\nsuc-injective = \u2261.cong pred\n\n_==_ : (x y : \u2115) \u2192 Bool\nzero == zero = true\nzero == suc _ = false\nsuc _ == zero = false\nsuc m == suc n = m == n\n\ninfix 8 _^_\n_^_ : \u2115 \u2192 \u2115 \u2192 \u2115\nb ^ zero = 1\nb ^ suc n = b * b ^ n\n\n-- https:\/\/en.wikipedia.org\/wiki\/Hyper_operator\n_\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\na \u2191\u27e8 suc n \u27e9 (suc b) = a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\na \u2191\u27e8 0 \u27e9 b = suc b\na \u2191\u27e8 1 \u27e9 0 = a\na \u2191\u27e8 2 \u27e9 0 = 0\na \u2191\u27e8 suc (suc (suc n)) \u27e9 0 = 1\n\nmodule \u2191-Props where\n \u2191-fold : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 b \u2261 fold (a \u2191\u27e8 suc n \u27e9 0) (_\u2191\u27e8_\u27e9_ a n) b\n \u2191-fold a n zero = \u2261.refl\n \u2191-fold a n (suc b) = \u2261.cong (_\u2191\u27e8_\u27e9_ a n) (\u2191-fold a n b)\n\n \u2191\u2080-+ : \u2200 a b \u2192 a \u2191\u27e8 0 \u27e9 b \u2261 1 + b\n \u2191\u2080-+ a b = \u2261.refl\n\n \u2191\u2081-+ : \u2200 a b \u2192 a \u2191\u27e8 1 \u27e9 b \u2261 b + a\n \u2191\u2081-+ a zero = \u2261.refl\n \u2191\u2081-+ a (suc b) = \u2261.cong suc (\u2191\u2081-+ a b)\n\n \u2191\u2082-* : \u2200 a b \u2192 a \u2191\u27e8 2 \u27e9 b \u2261 b * a\n \u2191\u2082-* a zero = \u2261.refl\n \u2191\u2082-* a (suc b) rewrite \u2191\u2082-* a b\n | \u2191\u2081-+ a (b * a)\n | \u2115\u00b0.+-comm (b * a) a\n = \u2261.refl\n\n -- fold 1 (_*_ a) b \u2261 a ^ b\n \u2191\u2083-^ : \u2200 a b \u2192 a \u2191\u27e8 3 \u27e9 b \u2261 a ^ b\n \u2191\u2083-^ a zero = \u2261.refl\n \u2191\u2083-^ a (suc b) rewrite \u2191\u2083-^ a b\n | \u2191\u2082-* a (a ^ b)\n | \u2115\u00b0.*-comm (a ^ b) a\n = \u2261.refl\n\n _\u2191\u27e8_\u27e90 : \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u27e8 0 \u27e90 = 1\n a \u2191\u27e8 1 \u27e90 = a\n a \u2191\u27e8 2 \u27e90 = 0\n a \u2191\u27e8 suc (suc (suc n)) \u27e90 = 1\n\n _`\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _`\u2191\u27e8_\u27e9_ a 0 = suc\n _`\u2191\u27e8_\u27e9_ a (suc n) = fold (a \u2191\u27e8 suc n \u27e90) (_`\u2191\u27e8_\u27e9_ a n)\n\n \u2191-ind-rule : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 (suc b) \u2261 a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\n \u2191-ind-rule a n b = \u2261.refl\n\n _\u2191\u2032\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u2032\u27e8 0 \u27e9 b = suc b\n a \u2191\u2032\u27e8 1 \u27e9 b = a + b\n a \u2191\u2032\u27e8 2 \u27e9 b = a * b\n a \u2191\u2032\u27e8 3 \u27e9 b = a ^ b\n a \u2191\u2032\u27e8 suc (suc (suc (suc n))) \u27e9 b = a \u2191\u27e8 4 + n \u27e9 b\n\n \u2191-\u2191\u2032 : \u2200 a n b \u2192 a \u2191\u27e8 n \u27e9 b \u2261 a \u2191\u2032\u27e8 n \u27e9 b\n \u2191-\u2191\u2032 a 0 b = \u2261.refl\n \u2191-\u2191\u2032 a 1 b = \u2261.trans (\u2191\u2081-+ a b) (\u2115\u00b0.+-comm b a)\n \u2191-\u2191\u2032 a 2 b = \u2261.trans (\u2191\u2082-* a b) (\u2115\u00b0.*-comm b a)\n \u2191-\u2191\u2032 a 3 b = \u2191\u2083-^ a b\n \u2191-\u2191\u2032 a (suc (suc (suc (suc _)))) b = \u2261.refl\n\n _\u21912+\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _\u21912+\u27e8_\u27e9_ a = fold (_*_ a) (fold 1)\n\nmodule InflModule where\n -- Inflationary functions\n Infl< : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl< f = \u2200 {x} \u2192 x < f x\n\n Infl : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl f = Infl< (suc \u2218 f)\n\n InflT< : (f : Endo (Endo \u2115)) \u2192 Set\n InflT< f = \u2200 {g} \u2192 Infl< g \u2192 Infl< (f g)\n\n{-\n\u2200 {x} \u2192 x \u2264 f x\n\n\n 1 + x \u2264 1 + f x\n x < 1 + f x\n x < (suc \u2218 f) x\n Infl< (suc \u2218 f)\n\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n\nInfl< f \u2192 Infl f\nInfl< f \u2192 Infl< (suc \u2218 f)\n-}\n\n id-infl : Infl id\n id-infl = \u2115\u2264.refl\n\n \u2218-infl< : \u2200 {f g} \u2192 Infl< f \u2192 Infl< g \u2192 Infl< (f \u2218 g)\n \u2218-infl< inflf< inflg< = <-trans inflg< inflf<\n\n suc-infl< : Infl< suc\n suc-infl< = \u2115\u2264.refl\n\n suc-infl : Infl suc\n suc-infl = s\u2264s (\u2264-step \u2115\u2264.refl)\n\n nest-infl : \u2200 f (finfl : Infl f) n \u2192 Infl (nest n f)\n nest-infl f _ zero = \u2115\u2264.refl\n nest-infl f finfl (suc n) = \u2115\u2264.trans (nest-infl f finfl n) finfl\n\n nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n nest-infl< f finfl< zero = finfl<\n nest-infl< f finfl< (suc n) = <-trans (nest-infl< f finfl< n) finfl< \n\n-- See Function.NP.nest-Properties for more properties\nmodule fold-Props where\n\n fold-suc : \u2200 m n \u2192 fold m suc n \u2261 n + m\n fold-suc m zero = \u2261.refl\n fold-suc m (suc n) rewrite fold-suc m n = \u2261.refl\n\n fold-+ : \u2200 m n \u2192 fold 0 (_+_ m) n \u2261 n * m\n fold-+ m zero = \u2261.refl\n fold-+ m (suc n) rewrite fold-+ m n = \u2261.refl\n\n fold-* : \u2200 m n \u2192 fold 1 (_*_ m) n \u2261 m ^ n\n fold-* m zero = \u2261.refl\n fold-* m (suc n) rewrite fold-* m n = \u2261.refl\n\n{- TODO\n fold-^ : \u2200 m n \u2192 fold 1 (flip _^_ m) n \u2261 m \u2191\u27e8 4 \u27e9 n\n fold-^ m zero = \u2261.refl\n fold-^ m (suc n) rewrite fold-^ m n = ?\n-}\n\n cong-fold : \u2200 {A : Set} {f g : Endo A} (f\u2257g : f \u2257 g) {z} \u2192 fold z f \u2257 fold z g\n cong-fold eq zero = \u2261.refl\n cong-fold eq {z} (suc x) rewrite cong-fold eq {z} x = eq _\n\n private\n open \u2191-Props\n module Alt\u21912 where\n alt : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n alt a 0 = \u03bb b \u2192 a \u2191\u27e8 2 \u27e9 b\n alt a (suc n) = fold 1 (alt a n)\n\n alt-ok : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 alt a n\n alt-ok a zero = \u2261.sym \u2218 \u2191-\u2191\u2032 a 2\n alt-ok a (suc n) = cong-fold (alt-ok a n)\n\n{- TODO\n alt' : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 _\u2191\u27e8_\u27e9_ a (2 + n)\n alt' a zero b = \u2261.sym (\u2191-\u2191\u2032 a 2 _)\n alt' a (suc n) zero = \u2261.refl\n alt' a (suc n) (suc x) rewrite alt' a n (_\u21912+\u27e8_\u27e9_ a (suc n) x)\n = \u2261.cong (_\u2191\u27e8_\u27e9_ a (2 + n)) { fold 1 (fold (_*_ a) (fold 1) n) x} {a \u2191\u27e8 suc (suc (suc n)) \u27e9 x} (alt' a (suc n) x)\n-}\n\n open InflModule\n{-\n fold1-infl : \u2200 f \u2192 Infl f \u2192 Infl (fold 1 f)\n fold1-infl f finfl {zero} = {!z\u2264n!}\n fold1-infl f finfl {suc x} =\n x <\u27e8 {!s\u2264s (fold1-infl f finfl {x})!} \u27e9\n fold 1 f x <\u27e8 {!!} \u27e9\n {- f (fold 1 f x) \u2264\u27e8 finfl \u27e9\n f (fold 1 f x) \u2261\u27e8 \u2261.refl \u27e9 -}\n fold 1 f (1 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 f \u2218 suc)\n fold1+1-infl< f finfl< {zero} = finfl<\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + f (fold 1 f x) \u2264\u27e8 finfl< {f (fold 1 f x)} \u27e9\n f (suc (f (fold 1 f x))) \u2264\u27e8 {!!} \u27e9\n fold 1 f (2 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 (f \u2218 suc))\n fold1+1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 suc) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 suc) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+-infl< : \u2200 k f \u2192 Infl< (f \u2218\u2032 _+_ k) \u2192 Infl< (fold 1 (f \u2218\u2032 _+_ k))\n fold1+-infl< k f finfl< {zero} = s\u2264s z\u2264n\n fold1+-infl< k f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-infl< k f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 _+_ k) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 _+_ k) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n \n{-\n fold1+-infl< : \u2200 f \u2192 Infl+2< f \u2192 Infl+2< (fold 1 f)\n fold1+-infl< f finfl< {zero} _ = \u2115\u2264.refl\n fold1+-infl< f finfl< {suc zero} _ = {!!}\n fold1+-infl< f finfl< {suc (suc zero)} _ = {!!}\n fold1+-infl< f finfl< {suc (suc (suc x))} x>1 =\n 4 + x \u2264\u27e8 s\u2264s (fold1+-infl< f finfl< {suc (suc x)} (m\u2264m+n 2 x)) \u27e9\n 1 + fold 1 f (2 + x) \u2264\u27e8 finfl< {!!} \u27e9\n fold 1 f (3 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 f x \u2264\u27e8 finfl< \u27e9\n fold 1 f (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\nmodule ^-Props where\n open \u2261-Reasoning\n\n ^1 : \u2200 n \u2192 n ^ 1 \u2261 n\n ^1 zero = \u2261.refl\n ^1 (suc n) = \u2261.cong suc (proj\u2082 \u2115\u00b0.*-identity n)\n\n ^-+ : \u2200 b m n \u2192 b ^ (m + n) \u2261 b ^ m * b ^ n\n ^-+ b zero n = \u2261.sym (proj\u2082 \u2115\u00b0.+-identity (b ^ n))\n ^-+ b (suc m) n rewrite ^-+ b m n = \u2261.sym (\u2115\u00b0.*-assoc b (b ^ m) (b ^ n))\n\n ^-* : \u2200 b m n \u2192 b ^ (m * n) \u2261 (b ^ n) ^ m\n ^-* b zero n = \u2261.refl\n ^-* b (suc m) n = b ^ (n + m * n)\n \u2261\u27e8 ^-+ b n (m * n) \u27e9\n b ^ n * b ^ (m * n)\n \u2261\u27e8 \u2261.cong (_*_ (b ^ n)) (^-* b m n) \u27e9\n b ^ n * (b ^ n) ^ m \u220e\n\nack : \u2115 \u2192 \u2115 \u2192 \u2115\nack zero n = suc n\nack (suc m) zero = ack m (suc zero)\nack (suc m) (suc n) = ack m (ack (suc m) n)\n\nmodule ack-Props where\n lem\u2238 : \u2200 {m n} \u2192 m \u2264 n \u2192 m + (n \u2238 m) \u2261 n\n lem\u2238 z\u2264n = \u2261.refl\n lem\u2238 (s\u2264s {m} {n} m\u2264n) = \u2261.cong suc (lem\u2238 m\u2264n)\n\n -- n >= m \u2192 m + (n \u2238 m) \u2261 n\n -- n >= m \u2192 \u2203 k \u2192 n \u2261 m + k\n -- \u2203 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 m + ((m + k) \u2238 m) \u2261 m + k\n -- \u2200 k \u2192 m + k \u2261 m + k\n\n -- 3 \u2264 x \u2192 P x \u2192 \u2203 \u03bb k \u2192 P (3 + k)\n\n open InflModule\n\n fold1+-inflT< : \u2200 {z} \u2192 InflT< (fold (suc z))\n fold1+-inflT< _ {zero} = s\u2264s z\u2264n\n fold1+-inflT< {z} {f} finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-inflT< {z} {f} finfl< {x}) \u27e9\n 1 + fold (suc z) f x \u2264\u27e8 finfl< \u27e9\n f (fold (suc z) f x) \u2261\u27e8 \u2261.refl \u27e9\n fold (suc z) f (1 + x)\n \u220e\n where open \u2264-Reasoning\n\n Mon : (f : \u2115 \u2192 \u2115) \u2192 Set\n Mon f = \u2200 {x y} \u2192 x \u2264 y \u2192 f x \u2264 f y\n open InflModule\n\n\n \u2115-ind : \u2200 (P : \u2115 \u2192 Set) \u2192 P zero \u2192 (\u2200 {n} \u2192 P n \u2192 P (suc n)) \u2192 \u2200 {n} \u2192 P n\n \u2115-ind P P0 PS {zero} = P0\n \u2115-ind P P0 PS {suc n} = PS (\u2115-ind P P0 PS)\n\n open fold-Props\n fold-infl< : \u2200 {f g} \u2192 Infl< f \u2192 InflT< g \u2192 \u2200 {n} \u2192 Infl< (fold f g n)\n fold-infl< {f} {g} inflf< inflTg< {n} = \u2115-ind (Infl< \u2218 fold f g) inflf< inflTg< {n}\n\n fold-mon : \u2200 {f} (fmon : Mon f) (finfl< : Infl< f) {z} \u2192 Mon (fold z f)\n fold-mon fmon finfl< {_} {y = 0} z\u2264n = \u2115\u2264.refl\n fold-mon {f} fmon finfl< {z} {y = suc n} z\u2264n = z \u2264\u27e8 \u2264-step \u2115\u2264.refl \u27e9\n suc z \u2264\u27e8 nest-infl< f finfl< n {z} \u27e9\n fold z f (suc n) \u2261\u27e8 \u2261.refl \u27e9\n fold z f (suc n) \u220e\n where open \u2264-Reasoning\n fold-mon fmon finfl< (s\u2264s m\u2264n) = fmon (fold-mon fmon finfl< m\u2264n)\n\n MonT : Endo (Endo \u2115) \u2192 Set\n MonT g = \u2200 {f} \u2192 Mon f \u2192 Infl< f \u2192 Mon (g f)\n\n fold-mon' : \u2200 {f g} \u2192 Mon f \u2192 Infl< f \u2192 MonT g \u2192 InflT< g \u2192 \u2200 {n} \u2192 Mon (fold f g n)\n fold-mon' {f} {g} mon-f infl-f mont-g inflTg< {n} = go n where\n go : \u2200 n \u2192 Mon (fold f g n)\n go zero = mon-f\n go (suc n) = mont-g (go n) (fold-infl< infl-f inflTg< {n})\n\n\n -- +-fold : \u2200 a b \u2192 a + b \u2261 fold a suc b\n -- *-fold : \u2200 a b \u2192 a * b \u2261 fold 0 (_+_ a) b\n\n 1\u22641+a^b : \u2200 a b \u2192 1 \u2264 (1 + a) ^ b\n 1\u22641+a^b a zero = s\u2264s z\u2264n\n 1\u22641+a^b a (suc b) = 1\u22641+a^b a b +-mono z\u2264n\n\n 1+a^-mon : \u2200 {a} \u2192 Mon (_^_ (1 + a))\n 1+a^-mon {a} (z\u2264n {b}) = 1\u22641+a^b a b\n 1+a^-mon {a} (s\u2264s {m} {n} m\u2264n)\n = (1 + a) ^ (1 + m) \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ m + a * (1 + a) ^ m \u2264\u27e8 (1+a^-mon m\u2264n) +-mono ((a \u220e) *-mono (1+a^-mon m\u2264n)) \u27e9\n (1 + a) ^ n + a * (1 + a) ^ n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ (1 + n) \u220e\n where open \u2264-Reasoning\n\n postulate\n \u21913+ : \u2200 a n b \u2192 a \u2191\u27e8 3 + n \u27e9 b \u2261 fold (_^_ a) (fold 1) n b\n -- mon\u21913+'' : \u2200 a b \u2192 Mon (\u03bb n \u2192 fold (_^_ a) (fold 1) n b)\n\n mon\u21913+' : \u2200 b \u2192 Mon (\u03bb n \u2192 fold (_^_ 2) (fold 1) n (3 + b))\n -- mon\u21913+' b = {!!}\n\n mon\u21913+ : \u2200 b \u2192 Mon (\u03bb n \u2192 2 \u2191\u27e8 3 + n \u27e9 (3 + b))\n mon\u21913+ b {m} {n} rewrite \u21913+ 2 m (3 + b) | \u21913+ 2 n (3 + b) = mon\u21913+' b\n\n lem2^3 : \u2200 n \u2192 2 ^ 3 \u2264 2 ^ (3 + n)\n lem2^3 n = 1+a^-mon {1} {3} {3 + n} (s\u2264s (s\u2264s (s\u2264s z\u2264n)))\n\n open \u2191-Props\n lem>=3 : \u2200 m n \u2192 3 \u2264 2 \u2191\u27e8 3 + m \u27e9 (3 + n)\n lem>=3 m n = 3 \u2264\u27e8 s\u2264s (s\u2264s (s\u2264s z\u2264n)) \u27e9\n 2 \u2191\u27e8 3 \u27e9 3 \u2261\u27e8 \u2191\u2083-^ 2 3 \u27e9\n 2 ^ 3 \u2264\u27e8 lem2^3 n \u27e9\n 2 ^ (3 + n) \u2261\u27e8 \u2261.sym (\u2191\u2083-^ 2 (3 + n)) \u27e9\n 2 \u2191\u27e8 3 \u27e9 (3 + n) \u2264\u27e8 mon\u21913+ n z\u2264n \u27e9\n 2 \u2191\u27e8 3 + m \u27e9 (3 + n) \u220e\n where open \u2264-Reasoning\n\n 2* = _*_ 2\n\n -- fold 2* n 1 \u2261 2 ^ n\n\n -- nest-* : \u2200 m n \u2192 nest (m * n) f \u2257 nest m (nest n f)\n\n nest-2* : \u2200 m n \u2192 nest (m * n) (fold 1) 2* \u2261 nest m (nest n (fold 1)) 2*\n nest-2* m n = nest-Properties.nest-* (fold 1) m n 2*\n\n{-\n lem3\u2264 : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 2\n lem3\u2264 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264 (suc n) = \n 3 \u2264\u27e8 {!!} \u27e9\n fold 2* f1 (1 + n) 2 \u220e\n where open \u2264-Reasoning\n f1 = fold 1\n\n lem3\u2264' : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 3\n lem3\u2264' 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264' (suc n) = \n 3 \u2264\u27e8 lem3\u2264 n \u27e9\n fold 2* (fold 1) n 2 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) n (fold 2* (fold 1) 3 1) \u2261\u27e8 {!!} \u27e9\n{- \u2261\u27e8 nest-Properties.nest-* {!2*!} {!!} {!!} {!!} \u27e9\n fold 2* (fold 1) (3 * n) 1 \u2261\u27e8 {!!} \u27e9\n-}\n fold 1 (fold 2* (fold 1) n) 3 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) (1 + n) 3 \u220e\n where open \u2264-Reasoning\n-}\n---\n\n{-\nfold 2* (fold 1) (3 * n) 1 \u2264\n\nfold 2* (fold 1) n (fold 2* (fold 1) n (fold 2* (fold 1) n 1)) \u2264\n\nfold 1 (fold 2* (fold 1) n) 3 \u2264\nfold 2* (fold 1) (1 + n) 3\n-}\n\n lem>=3'' : \u2200 m n \u2192 3 \u2264 2 \u2191\u27e8 suc m \u27e9 (3 + n)\n lem>=3'' zero n = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n lem>=3'' (suc zero) n rewrite \u2191\u2082-* 2 (3 + n) = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n lem>=3'' (suc (suc m)) n = lem>=3 m n\n\n lem2221 : \u2200 m \u2192 2 \u2261 2 \u2191\u27e8 2 + m \u27e9 1\n lem2221 zero = \u2261.refl\n lem2221 (suc m) = lem2221 m\n\n lem4212 : \u2200 m \u2192 4 \u2261 2 \u2191\u27e8 1 + m \u27e9 2\n lem4212 zero = \u2261.refl\n lem4212 (suc m) = 4 \u2261\u27e8 lem4212 m \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 2 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 (1 + m)) (lem2221 m) \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 (2 \u2191\u27e8 2 + m \u27e9 1) \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 2 + m \u27e9 2 \u220e\n where open \u2261-Reasoning\n\n ack-\u2191 : \u2200 m n \u2192 3 + ack m n \u2261 2 \u2191\u27e8 m \u27e9 (3 + n)\n ack-\u2191 zero n = \u2261.refl\n ack-\u2191 (suc m) zero = 3 + ack (suc m) 0 \u2261\u27e8 ack-\u2191 m 1 \u27e9\n 2 \u2191\u27e8 m \u27e9 4 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 m) (lem4212 m) \u27e9\n 2 \u2191\u27e8 suc m \u27e9 3 \u220e\n where open \u2261-Reasoning\n ack-\u2191 (suc m) (suc n) = 3 + ack (suc m) (suc n)\n \u2261\u27e8 \u2261.refl \u27e9\n 3 + ack m (ack (suc m) n)\n \u2261\u27e8 \u2261.refl \u27e9\n 3 + ack m ((3 + ack (suc m) n) \u2238 3)\n \u2261\u27e8 \u2261.cong (\u03bb x \u2192 3 + ack m (x \u2238 3)) (ack-\u2191 (suc m) n) \u27e9\n 3 + ack m (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3)\n \u2261\u27e8 ack-\u2191 m (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3) \u27e9\n 2 \u2191\u27e8 m \u27e9 (3 + (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3))\n \u2261\u27e8 \u2261.cong (\u03bb x \u2192 2 \u2191\u27e8 m \u27e9 x) (lem\u2238 (lem>=3'' m n)) \u27e9\n 2 \u2191\u27e8 m \u27e9 (2 \u2191\u27e8 suc m \u27e9 (3 + n))\n \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 suc m \u27e9 (4 + n) \u220e \n where open \u2261-Reasoning\n\n postulate\n 1+a^-infl< : \u2200 {a} \u2192 Infl< (_^_ (1 + a))\n\n -- 2+a*1+b-infl< : \u2200 a \u2192 Infl< (\u03bb x \u2192 (2 + a) * (1 + x))\n -- \u2200 a b \u2192 b < (2 + a) * b\n -- fold-a*-fold1 : \u2200 {n a} \u2192 Infl< (_\u21912+\u27e8_\u27e9_ (2 + a) n)\n fold-a^-fold1 : \u2200 {n a} \u2192 Infl< (fold (_^_ (1 + a)) (fold 1) n)\n fold-a^-fold1 {n} = fold-infl< 1+a^-infl< fold1+-inflT< {n}\n\n \u21913+-mon : \u2200 a n \u2192 Mon (fold (_^_ (1 + a)) (fold 1) n)\n \u21913+-mon a n = fold-mon' 1+a^-mon 1+a^-infl< (\u03bb \u03b7\u2081 \u03b7\u2082 \u2192 fold-mon \u03b7\u2081 \u03b7\u2082) fold1+-inflT< {n}\n\n{-\n open \u2191-Props\n\n -*\u27e81+n\u27e9-infl : \u2200 n \u2192 Infl (_*_ (1 + n))\n -*\u27e81+n\u27e9-infl n = {!!}\n\n lem121 : \u2200 a b \u2192 b < (1 + a) \u2191\u27e8 2 \u27e9 (1 + b)\n lem121 a b rewrite \u2191\u2082-* (1 + a) (1 + b)\n = 1 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (1 + a) * (1 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (1 + a) (1 + b) \u27e9\n (1 + b) * (1 + a)\n \u220e\n where open \u2264-Reasoning\n\n -- \u2200 a b \u2192 b < (2 + a) * b\n lem222 : \u2200 a b \u2192 b < (2 + a) \u2191\u27e8 2 \u27e9 (2 + b)\n lem222 a b rewrite \u2191\u2082-* (2 + a) (2 + b)\n = 1 + b\n \u2264\u27e8 suc-infl \u27e9\n 2 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (2 + a) * (2 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (2 + a) (2 + b) \u27e9\n (2 + b) * (2 + a)\n \u220e\n where open \u2264-Reasoning\n-}\n{-\n open fold-Props\n\n module Foo a where\n f = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b\n f-lem : \u2200 n \u2192 f (suc n) \u2257 fold 1 (f n)\n f-lem n x = \u2261.refl\n\n f2 = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n f2-lem : \u2200 n \u2192 f2 (suc n) \u2257 fold 1 (f2 n)\n f2-lem n x = {!!}\n\n -- b < (1 + a) * (1 + b)\n\n -- nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n infl3< : \u2200 a n \u2192 Infl< (\u03bb b \u2192 (1 + a) \u21912+\u27e8 n \u27e9 (1 + b))\n infl3< a zero {b} = lem121 a b\n infl3< a (suc n) {b}\n = 1 + b\n -- fold1-infl< : \u2200 f \u2192 Infl< f \u2192 \u2200 x \u2192 x < fold 1 f x\n \u2264\u27e8 {!fold1f2-infl< {_}!} \u27e9\n fold 1 f1 b\n \u2264\u27e8 {!!} \u27e9\n{-\n \u2264\u27e8 fold1-infl< (f 0) (\u03bb {x} \u2192 {!infl3< a n {x}!}) {{!!}} \u27e9\n {!!}\n \u2264\u27e8 {!!} \u27e9\n-}\n{-\n 1 + b\n (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n \u2264\u27e8 {!nest-infl< (\u03bb b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b) ? b {(2 + a) \u21912+\u27e8 n \u27e9 (2 + b)}!} \u27e9\n fold (f 0 1) (f 0) (1 + b)\n \u2264\u27e8 {!\u2115\u2264.reflexive (\u2261.cong (\u03bb g \u2192 g 1) (nest-Properties.nest-+ (f 0) 1 (1 + b)))!} \u27e9\n-}\n fold 1 (f 0) (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n f 1 (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) \u21912+\u27e8 suc n \u27e9 (1 + b)\n \u220e\n\n where\n f = \u03bb k b \u2192 (1 + a) \u21912+\u27e8 k + n \u27e9 b\n f1 = f 0 \u2218 suc\n\n -- fold1f2-infl< : Infl< (fold 1 f2)\n -- fold1f2-infl< = fold1-infl< f2 {!(\u03bb b \u2192 infl3< a n b ?)!}\n\n fold1f1-infl<' : Infl< (fold 1 f1)\n fold1f1-infl<' = {!fold1+1-infl< (f 0) {!infl3< a n!}!}\n lem : \u2200 x \u2192 f 0 (1 + x) < f 0 (f 0 x)\n lem x = {!!}\n postulate\n finfl : Infl< (f 0)\n open \u2264-Reasoning\n-}\n\n_\u21d1\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\na \u21d1\u27e8 n , k \u27e9 b = fold k f n\n module M\u21d1 where \n f : \u2115 \u2192 \u2115\n f x = a \u2191\u27e8 2 + x \u27e9 b\n\nmodule \u21d1-Props where\n _\u21d1\u2032\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\n a \u21d1\u2032\u27e8 n\u2080 , k \u27e9 b = g n\u2080\n module M\u21d1\u2032 where\n h : \u2115 \u2192 \u2115\n h x = a \u2191\u27e8 2 + x \u27e9 b\n\n g : \u2115 \u2192 \u2115\n g 0 = k\n g (suc n) = h (g n)\n\n g-nest : \u2200 n \u2192 g n \u2261 h $\u27e8 n \u27e9 k\n g-nest zero = \u2261.refl\n g-nest (suc n) rewrite g-nest n = \u2261.refl\n\n \u21d1\u2080 : \u2200 a k b \u2192 a \u21d1\u27e8 0 , k \u27e9 b \u2261 k\n \u21d1\u2080 _ _ _ = \u2261.refl\n\n \u21d1\u2081 : \u2200 a k b \u2192 a \u21d1\u27e8 1 , k \u27e9 b \u2261 a \u2191\u27e8 2 + k \u27e9 b\n \u21d1\u2081 _ _ _ = \u2261.refl\n\n \u21d1-\u21d1\u2032 : \u2200 a n k b \u2192 a \u21d1\u27e8 n , k \u27e9 b \u2261 a \u21d1\u2032\u27e8 n , k \u27e9 b\n \u21d1-\u21d1\u2032 a n k b rewrite M\u21d1\u2032.g-nest a n k b n = \u2261.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Graham%27s_number\nGraham's-number : \u2115\nGraham's-number = 3 \u21d1\u27e8 64 , 4 \u27e9 3\n -- = (f\u2218\u2076\u2074\u2026\u2218f)4\n -- where f x = 3 \u2191\u27e8 2 + x \u27e9 3\n\n{-\nmodule GeneralisedArithmetic {a} {A : Set a} (0# : A) (1+ : A \u2192 A) where\n\n 1# : A\n 1# = 1+ 0#\n\n open Data.Nat.GeneralisedArithmetic 0# 1+ public\n\n exp : (* : A \u2192 A \u2192 A) \u2192 (\u2115 \u2192 A \u2192 A)\n exp _*_ n b = fold 1# (\u03bb s \u2192 b * s) n\n-}\n -- hyperop a n b = fold exp\n\nmodule == where\n _\u2248_ : (m n : \u2115) \u2192 Set\n m \u2248 n = T (m == n)\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {zero} {zero} _ = id\n subst P {suc _} {suc _} p = subst (P \u2218 suc) p\n subst _ {zero} {suc _} ()\n subst _ {suc _} {zero} ()\n\n sound : \u2200 m n \u2192 T (m == n) \u2192 m \u2261 n\n sound m n p = subst (_\u2261_ m) p \u2261.refl\n\n refl : Reflexive _\u2248_\n refl {zero} = _\n refl {suc n} = refl {n}\n\n sym : Symmetric _\u2248_\n sym {m} {n} eq rewrite sound m n eq = refl {n}\n\n trans : Transitive _\u2248_\n trans {m} {n} {o} m\u2248n n\u2248o rewrite sound m n m\u2248n | sound n o n\u2248o = refl {o}\n\n setoid : Setoid _ _\n setoid = record { Carrier = \u2115; _\u2248_ = _\u2248_\n ; isEquivalence =\n record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} } }\n\n open Setoid setoid public hiding (refl; sym; trans; _\u2248_)\n\n{-\ndata _`\u2264?`_\u219d_ : (m n : \u2115) \u2192 Dec (m \u2264 n) \u2192 Set where\n z\u2264?n : \u2200 {n} \u2192 zero `\u2264?` n \u219d yes z\u2264n\n s\u2264?z : \u2200 {m} \u2192 suc m `\u2264?` zero \u219d no \u03bb()\n s\u2264?s-yes : \u2200 {m n m\u2264n} \u2192 m `\u2264?` n \u219d yes m\u2264n \u2192 suc m `\u2264?` suc n \u219d yes (s\u2264s m\u2264n)\n s\u2264?s-no : \u2200 {m n m\u2270n} \u2192 m `\u2264?` n \u219d no m\u2270n \u2192 suc m `\u2264?` suc n \u219d no (m\u2270n \u2218 \u2264-pred)\n\n`\u2264?`-complete : \u2200 m n \u2192 m `\u2264?` n \u219d (m \u2264? n)\n`\u2264?`-complete zero n = z\u2264?n\n`\u2264?`-complete (suc n) zero = {!s\u2264?z!}\n`\u2264?`-complete (suc m) (suc n) with m \u2264? n | `\u2264?`-complete m n\n... | yes q | r = s\u2264?s-yes r\n... | no q | r = s\u2264?s-no {!!}\n-}\n\n_<=_ : (x y : \u2115) \u2192 Bool\nzero <= _ = true\nsuc _ <= zero = false\nsuc m <= suc n = m <= n\n\nmodule <= where\n sound : \u2200 m n \u2192 T (m <= n) \u2192 m \u2264 n\n sound zero _ _ = z\u2264n\n sound (suc m) (suc n) p = s\u2264s (sound m n p)\n sound (suc m) zero ()\n\n complete : \u2200 {m n} \u2192 m \u2264 n \u2192 T (m <= n)\n complete z\u2264n = _\n complete (s\u2264s m\u2264n) = complete m\u2264n\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (Props.\u2264-steps 1 1+n\u2264m)\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Nat.NP where\n\nimport Algebra\nopen import Data.Nat public hiding (module GeneralisedArithmetic; module \u2264-Reasoning)\nopen import Data.Nat.Properties as Props\nopen import Data.Bool\nopen import Data.Product using (proj\u2081; proj\u2082; \u2203; _,_)\nopen import Data.Empty using (\u22a5-elim)\nopen import Function.NP\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_; module \u2261-Reasoning)\n\nmodule \u2115\u00b0 = Algebra.CommutativeSemiring Props.commutativeSemiring\nmodule \u2115cmp = StrictTotalOrder Props.strictTotalOrder\nmodule \u2115\u2264 = DecTotalOrder decTotalOrder\n\nmodule \u2264-Reasoning where\n open Preorder-Reasoning \u2115\u2264.preorder public renaming (_\u223c\u27e8_\u27e9_ to _\u2264\u27e8_\u27e9_)\n infixr 2 _\u2261\u27e8_\u27e9_\n _\u2261\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x \u2261 y \u2192 y \u2264 z \u2192 x \u2264 z\n _ \u2261\u27e8 \u2261.refl \u27e9 p = p\n infixr 2 _<\u27e8_\u27e9_\n _<\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x < y \u2192 y \u2264 z \u2192 x < z\n x <\u27e8 p \u27e9 q = suc x \u2264\u27e8 p \u27e9 q\n\nsuc-injective : \u2200 {n m : \u2115} \u2192 (suc n \u2236 \u2115) \u2261 suc m \u2192 n \u2261 m\nsuc-injective = \u2261.cong pred\n\n_==_ : (x y : \u2115) \u2192 Bool\nzero == zero = true\nzero == suc _ = false\nsuc _ == zero = false\nsuc m == suc n = m == n\n\ninfix 8 _^_\n_^_ : \u2115 \u2192 \u2115 \u2192 \u2115\nb ^ zero = 1\nb ^ suc n = b * b ^ n\n\n-- https:\/\/en.wikipedia.org\/wiki\/Hyper_operator\n_\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\na \u2191\u27e8 suc n \u27e9 (suc b) = a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\na \u2191\u27e8 0 \u27e9 b = suc b\na \u2191\u27e8 1 \u27e9 0 = a\na \u2191\u27e8 2 \u27e9 0 = 0\na \u2191\u27e8 suc (suc (suc n)) \u27e9 0 = 1\n\nmodule \u2191-Props where\n \u2191-fold : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 b \u2261 fold (a \u2191\u27e8 suc n \u27e9 0) (_\u2191\u27e8_\u27e9_ a n) b\n \u2191-fold a n zero = \u2261.refl\n \u2191-fold a n (suc b) = \u2261.cong (_\u2191\u27e8_\u27e9_ a n) (\u2191-fold a n b)\n\n \u2191\u2080-+ : \u2200 a b \u2192 a \u2191\u27e8 0 \u27e9 b \u2261 1 + b\n \u2191\u2080-+ a b = \u2261.refl\n\n \u2191\u2081-+ : \u2200 a b \u2192 a \u2191\u27e8 1 \u27e9 b \u2261 b + a\n \u2191\u2081-+ a zero = \u2261.refl\n \u2191\u2081-+ a (suc b) = \u2261.cong suc (\u2191\u2081-+ a b)\n\n \u2191\u2082-* : \u2200 a b \u2192 a \u2191\u27e8 2 \u27e9 b \u2261 b * a\n \u2191\u2082-* a zero = \u2261.refl\n \u2191\u2082-* a (suc b) rewrite \u2191\u2082-* a b\n | \u2191\u2081-+ a (b * a)\n | \u2115\u00b0.+-comm (b * a) a\n = \u2261.refl\n\n -- fold 1 (_*_ a) b \u2261 a ^ b\n \u2191\u2083-^ : \u2200 a b \u2192 a \u2191\u27e8 3 \u27e9 b \u2261 a ^ b\n \u2191\u2083-^ a zero = \u2261.refl\n \u2191\u2083-^ a (suc b) rewrite \u2191\u2083-^ a b\n | \u2191\u2082-* a (a ^ b)\n | \u2115\u00b0.*-comm (a ^ b) a\n = \u2261.refl\n\n _\u2191\u27e8_\u27e90 : \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u27e8 0 \u27e90 = 1\n a \u2191\u27e8 1 \u27e90 = a\n a \u2191\u27e8 2 \u27e90 = 0\n a \u2191\u27e8 suc (suc (suc n)) \u27e90 = 1\n\n _`\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _`\u2191\u27e8_\u27e9_ a 0 = suc\n _`\u2191\u27e8_\u27e9_ a (suc n) = fold (a \u2191\u27e8 suc n \u27e90) (_`\u2191\u27e8_\u27e9_ a n)\n\n \u2191-ind-rule : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 (suc b) \u2261 a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\n \u2191-ind-rule a n b = \u2261.refl\n\n _\u2191\u2032\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u2032\u27e8 0 \u27e9 b = suc b\n a \u2191\u2032\u27e8 1 \u27e9 b = a + b\n a \u2191\u2032\u27e8 2 \u27e9 b = a * b\n a \u2191\u2032\u27e8 3 \u27e9 b = a ^ b\n a \u2191\u2032\u27e8 suc (suc (suc (suc n))) \u27e9 b = a \u2191\u27e8 4 + n \u27e9 b\n\n \u2191-\u2191\u2032 : \u2200 a n b \u2192 a \u2191\u27e8 n \u27e9 b \u2261 a \u2191\u2032\u27e8 n \u27e9 b\n \u2191-\u2191\u2032 a 0 b = \u2261.refl\n \u2191-\u2191\u2032 a 1 b = \u2261.trans (\u2191\u2081-+ a b) (\u2115\u00b0.+-comm b a)\n \u2191-\u2191\u2032 a 2 b = \u2261.trans (\u2191\u2082-* a b) (\u2115\u00b0.*-comm b a)\n \u2191-\u2191\u2032 a 3 b = \u2191\u2083-^ a b\n \u2191-\u2191\u2032 a (suc (suc (suc (suc _)))) b = \u2261.refl\n\n _\u21912+\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _\u21912+\u27e8_\u27e9_ a = fold (_*_ a) (fold 1)\n\nmodule InflModule where\n -- Inflationary functions\n Infl< : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl< f = \u2200 {x} \u2192 x < f x\n\n Infl : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl f = Infl< (suc \u2218 f)\n\n InflT< : (f : Endo (Endo \u2115)) \u2192 Set\n InflT< f = \u2200 {g} \u2192 Infl< g \u2192 Infl< (f g)\n\n{-\n\u2200 {x} \u2192 x \u2264 f x\n\n\n 1 + x \u2264 1 + f x\n x < 1 + f x\n x < (suc \u2218 f) x\n Infl< (suc \u2218 f)\n\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n\nInfl< f \u2192 Infl f\nInfl< f \u2192 Infl< (suc \u2218 f)\n-}\n\n id-infl : Infl id\n id-infl = \u2115\u2264.refl\n\n \u2218-infl< : \u2200 {f g} \u2192 Infl< f \u2192 Infl< g \u2192 Infl< (f \u2218 g)\n \u2218-infl< inflf< inflg< = <-trans inflg< inflf<\n\n suc-infl< : Infl< suc\n suc-infl< = \u2115\u2264.refl\n\n suc-infl : Infl suc\n suc-infl = s\u2264s (\u2264-step \u2115\u2264.refl)\n\n nest-infl : \u2200 f (finfl : Infl f) n \u2192 Infl (nest n f)\n nest-infl f _ zero = \u2115\u2264.refl\n nest-infl f finfl (suc n) = \u2115\u2264.trans (nest-infl f finfl n) finfl\n\n nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n nest-infl< f finfl< zero = finfl<\n nest-infl< f finfl< (suc n) = <-trans (nest-infl< f finfl< n) finfl< \n\n-- See Function.NP.nest-Properties for more properties\nmodule fold-Props where\n\n fold-suc : \u2200 m n \u2192 fold m suc n \u2261 n + m\n fold-suc m zero = \u2261.refl\n fold-suc m (suc n) rewrite fold-suc m n = \u2261.refl\n\n fold-+ : \u2200 m n \u2192 fold 0 (_+_ m) n \u2261 n * m\n fold-+ m zero = \u2261.refl\n fold-+ m (suc n) rewrite fold-+ m n = \u2261.refl\n\n fold-* : \u2200 m n \u2192 fold 1 (_*_ m) n \u2261 m ^ n\n fold-* m zero = \u2261.refl\n fold-* m (suc n) rewrite fold-* m n = \u2261.refl\n\n{- TODO\n fold-^ : \u2200 m n \u2192 fold 1 (flip _^_ m) n \u2261 m \u2191\u27e8 4 \u27e9 n\n fold-^ m zero = \u2261.refl\n fold-^ m (suc n) rewrite fold-^ m n = ?\n-}\n\n cong-fold : \u2200 {A : Set} {f g : Endo A} (f\u2257g : f \u2257 g) {z} \u2192 fold z f \u2257 fold z g\n cong-fold eq zero = \u2261.refl\n cong-fold eq {z} (suc x) rewrite cong-fold eq {z} x = eq _\n\n private\n open \u2191-Props\n module Alt\u21912 where\n alt : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n alt a 0 = \u03bb b \u2192 a \u2191\u27e8 2 \u27e9 b\n alt a (suc n) = fold 1 (alt a n)\n\n alt-ok : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 alt a n\n alt-ok a zero = \u2261.sym \u2218 \u2191-\u2191\u2032 a 2\n alt-ok a (suc n) = cong-fold (alt-ok a n)\n\n{- TODO\n alt' : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 _\u2191\u27e8_\u27e9_ a (2 + n)\n alt' a zero b = \u2261.sym (\u2191-\u2191\u2032 a 2 _)\n alt' a (suc n) zero = \u2261.refl\n alt' a (suc n) (suc x) rewrite alt' a n (_\u21912+\u27e8_\u27e9_ a (suc n) x)\n = \u2261.cong (_\u2191\u27e8_\u27e9_ a (2 + n)) { fold 1 (fold (_*_ a) (fold 1) n) x} {a \u2191\u27e8 suc (suc (suc n)) \u27e9 x} (alt' a (suc n) x)\n-}\n\n open InflModule\n{-\n fold1-infl : \u2200 f \u2192 Infl f \u2192 Infl (fold 1 f)\n fold1-infl f finfl {zero} = {!z\u2264n!}\n fold1-infl f finfl {suc x} =\n x <\u27e8 {!s\u2264s (fold1-infl f finfl {x})!} \u27e9\n fold 1 f x <\u27e8 {!!} \u27e9\n {- f (fold 1 f x) \u2264\u27e8 finfl \u27e9\n f (fold 1 f x) \u2261\u27e8 \u2261.refl \u27e9 -}\n fold 1 f (1 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 f \u2218 suc)\n fold1+1-infl< f finfl< {zero} = finfl<\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + f (fold 1 f x) \u2264\u27e8 finfl< {f (fold 1 f x)} \u27e9\n f (suc (f (fold 1 f x))) \u2264\u27e8 {!!} \u27e9\n fold 1 f (2 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 (f \u2218 suc))\n fold1+1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 suc) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 suc) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+-infl< : \u2200 k f \u2192 Infl< (f \u2218\u2032 _+_ k) \u2192 Infl< (fold 1 (f \u2218\u2032 _+_ k))\n fold1+-infl< k f finfl< {zero} = s\u2264s z\u2264n\n fold1+-infl< k f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-infl< k f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 _+_ k) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 _+_ k) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n \n{-\n fold1+-infl< : \u2200 f \u2192 Infl+2< f \u2192 Infl+2< (fold 1 f)\n fold1+-infl< f finfl< {zero} _ = \u2115\u2264.refl\n fold1+-infl< f finfl< {suc zero} _ = {!!}\n fold1+-infl< f finfl< {suc (suc zero)} _ = {!!}\n fold1+-infl< f finfl< {suc (suc (suc x))} x>1 =\n 4 + x \u2264\u27e8 s\u2264s (fold1+-infl< f finfl< {suc (suc x)} (m\u2264m+n 2 x)) \u27e9\n 1 + fold 1 f (2 + x) \u2264\u27e8 finfl< {!!} \u27e9\n fold 1 f (3 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 f x \u2264\u27e8 finfl< \u27e9\n fold 1 f (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\nmodule ^-Props where\n open \u2261-Reasoning\n\n ^1 : \u2200 n \u2192 n ^ 1 \u2261 n\n ^1 zero = \u2261.refl\n ^1 (suc n) = \u2261.cong suc (proj\u2082 \u2115\u00b0.*-identity n)\n\n ^-+ : \u2200 b m n \u2192 b ^ (m + n) \u2261 b ^ m * b ^ n\n ^-+ b zero n = \u2261.sym (proj\u2082 \u2115\u00b0.+-identity (b ^ n))\n ^-+ b (suc m) n rewrite ^-+ b m n = \u2261.sym (\u2115\u00b0.*-assoc b (b ^ m) (b ^ n))\n\n ^-* : \u2200 b m n \u2192 b ^ (m * n) \u2261 (b ^ n) ^ m\n ^-* b zero n = \u2261.refl\n ^-* b (suc m) n = b ^ (n + m * n)\n \u2261\u27e8 ^-+ b n (m * n) \u27e9\n b ^ n * b ^ (m * n)\n \u2261\u27e8 \u2261.cong (_*_ (b ^ n)) (^-* b m n) \u27e9\n b ^ n * (b ^ n) ^ m \u220e\n\nack : \u2115 \u2192 \u2115 \u2192 \u2115\nack zero n = suc n\nack (suc m) zero = ack m (suc zero)\nack (suc m) (suc n) = ack m (ack (suc m) n)\n\nmodule ack-Props where\n lem\u2238 : \u2200 {m n} \u2192 m \u2264 n \u2192 m + (n \u2238 m) \u2261 n\n lem\u2238 z\u2264n = \u2261.refl\n lem\u2238 (s\u2264s {m} {n} m\u2264n) = \u2261.cong suc (lem\u2238 m\u2264n)\n\n -- n >= m \u2192 m + (n \u2238 m) \u2261 n\n -- n >= m \u2192 \u2203 k \u2192 n \u2261 m + k\n -- \u2203 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 m + ((m + k) \u2238 m) \u2261 m + k\n -- \u2200 k \u2192 m + k \u2261 m + k\n\n -- 3 \u2264 x \u2192 P x \u2192 \u2203 \u03bb k \u2192 P (3 + k)\n\n open InflModule\n\n fold1+-inflT< : \u2200 {z} \u2192 InflT< (fold (suc z))\n fold1+-inflT< _ {zero} = s\u2264s z\u2264n\n fold1+-inflT< {z} {f} finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-inflT< {z} {f} finfl< {x}) \u27e9\n 1 + fold (suc z) f x \u2264\u27e8 finfl< \u27e9\n f (fold (suc z) f x) \u2261\u27e8 \u2261.refl \u27e9\n fold (suc z) f (1 + x)\n \u220e\n where open \u2264-Reasoning\n\n Mon : (f : \u2115 \u2192 \u2115) \u2192 Set\n Mon f = \u2200 {x y} \u2192 x \u2264 y \u2192 f x \u2264 f y\n open InflModule\n\n\n \u2115-ind : \u2200 (P : \u2115 \u2192 Set) \u2192 P zero \u2192 (\u2200 {n} \u2192 P n \u2192 P (suc n)) \u2192 \u2200 {n} \u2192 P n\n \u2115-ind P P0 PS {zero} = P0\n \u2115-ind P P0 PS {suc n} = PS (\u2115-ind P P0 PS)\n\n open fold-Props\n fold-infl< : \u2200 {f g} \u2192 Infl< f \u2192 InflT< g \u2192 \u2200 {n} \u2192 Infl< (fold f g n)\n fold-infl< {f} {g} inflf< inflTg< {n} = \u2115-ind (Infl< \u2218 fold f g) inflf< inflTg< {n}\n\n fold-mon : \u2200 {f} (fmon : Mon f) (finfl< : Infl< f) {z} \u2192 Mon (fold z f)\n fold-mon fmon finfl< {_} {y = 0} z\u2264n = \u2115\u2264.refl\n fold-mon {f} fmon finfl< {z} {y = suc n} z\u2264n = z \u2264\u27e8 \u2264-step \u2115\u2264.refl \u27e9\n suc z \u2264\u27e8 nest-infl< f finfl< n {z} \u27e9\n fold z f (suc n) \u2261\u27e8 \u2261.refl \u27e9\n fold z f (suc n) \u220e\n where open \u2264-Reasoning\n fold-mon fmon finfl< (s\u2264s m\u2264n) = fmon (fold-mon fmon finfl< m\u2264n)\n\n MonT : Endo (Endo \u2115) \u2192 Set\n MonT g = \u2200 {f} \u2192 Mon f \u2192 Infl< f \u2192 Mon (g f)\n\n fold-mon' : \u2200 {f g} \u2192 Mon f \u2192 Infl< f \u2192 MonT g \u2192 InflT< g \u2192 \u2200 {n} \u2192 Mon (fold f g n)\n fold-mon' {f} {g} mon-f infl-f mont-g inflTg< {n} = go n where\n go : \u2200 n \u2192 Mon (fold f g n)\n go zero = mon-f\n go (suc n) = mont-g (go n) (fold-infl< infl-f inflTg< {n})\n\n\n -- +-fold : \u2200 a b \u2192 a + b \u2261 fold a suc b\n -- *-fold : \u2200 a b \u2192 a * b \u2261 fold 0 (_+_ a) b\n\n 1\u22641+a^b : \u2200 a b \u2192 1 \u2264 (1 + a) ^ b\n 1\u22641+a^b a zero = s\u2264s z\u2264n\n 1\u22641+a^b a (suc b) = 1\u22641+a^b a b +-mono z\u2264n\n\n 1+a^-mon : \u2200 {a} \u2192 Mon (_^_ (1 + a))\n 1+a^-mon {a} (z\u2264n {b}) = 1\u22641+a^b a b\n 1+a^-mon {a} (s\u2264s {m} {n} m\u2264n)\n = (1 + a) ^ (1 + m) \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ m + a * (1 + a) ^ m \u2264\u27e8 (1+a^-mon m\u2264n) +-mono ((a \u220e) *-mono (1+a^-mon m\u2264n)) \u27e9\n (1 + a) ^ n + a * (1 + a) ^ n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ (1 + n) \u220e\n where open \u2264-Reasoning\n\n postulate\n \u21913+ : \u2200 a n b \u2192 a \u2191\u27e8 3 + n \u27e9 b \u2261 fold (_^_ a) (fold 1) n b\n -- mon\u21913+'' : \u2200 a b \u2192 Mon (\u03bb n \u2192 fold (_^_ a) (fold 1) n b)\n\n mon\u21913+' : \u2200 b \u2192 Mon (\u03bb n \u2192 fold (_^_ 2) (fold 1) n (3 + b))\n -- mon\u21913+' b = {!!}\n\n mon\u21913+ : \u2200 b \u2192 Mon (\u03bb n \u2192 2 \u2191\u27e8 3 + n \u27e9 (3 + b))\n mon\u21913+ b {m} {n} rewrite \u21913+ 2 m (3 + b) | \u21913+ 2 n (3 + b) = mon\u21913+' b\n\n lem2^3 : \u2200 n \u2192 2 ^ 3 \u2264 2 ^ (3 + n)\n lem2^3 n = 1+a^-mon {1} {3} {3 + n} (s\u2264s (s\u2264s (s\u2264s z\u2264n)))\n\n open \u2191-Props\n lem>=3 : \u2200 m n \u2192 3 \u2264 2 \u2191\u27e8 3 + m \u27e9 (3 + n)\n lem>=3 m n = 3 \u2264\u27e8 s\u2264s (s\u2264s (s\u2264s z\u2264n)) \u27e9\n 2 \u2191\u27e8 3 \u27e9 3 \u2261\u27e8 \u2191\u2083-^ 2 3 \u27e9\n 2 ^ 3 \u2264\u27e8 lem2^3 n \u27e9\n 2 ^ (3 + n) \u2261\u27e8 \u2261.sym (\u2191\u2083-^ 2 (3 + n)) \u27e9\n 2 \u2191\u27e8 3 \u27e9 (3 + n) \u2264\u27e8 mon\u21913+ n z\u2264n \u27e9\n 2 \u2191\u27e8 3 + m \u27e9 (3 + n) \u220e\n where open \u2264-Reasoning\n\n 2* = _*_ 2\n\n -- fold 2* n 1 \u2261 2 ^ n\n\n -- nest-* : \u2200 m n \u2192 nest (m * n) f \u2257 nest m (nest n f)\n\n nest-2* : \u2200 m n \u2192 nest (m * n) (fold 1) 2* \u2261 nest m (nest n (fold 1)) 2*\n nest-2* m n = nest-Properties.nest-* (fold 1) m n 2*\n\n{-\n lem3\u2264 : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 2\n lem3\u2264 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264 (suc n) = \n 3 \u2264\u27e8 {!!} \u27e9\n fold 2* f1 (1 + n) 2 \u220e\n where open \u2264-Reasoning\n f1 = fold 1\n\n lem3\u2264' : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 3\n lem3\u2264' 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264' (suc n) = \n 3 \u2264\u27e8 lem3\u2264 n \u27e9\n fold 2* (fold 1) n 2 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) n (fold 2* (fold 1) 3 1) \u2261\u27e8 {!!} \u27e9\n{- \u2261\u27e8 nest-Properties.nest-* {!2*!} {!!} {!!} {!!} \u27e9\n fold 2* (fold 1) (3 * n) 1 \u2261\u27e8 {!!} \u27e9\n-}\n fold 1 (fold 2* (fold 1) n) 3 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) (1 + n) 3 \u220e\n where open \u2264-Reasoning\n-}\n---\n\n{-\nfold 2* (fold 1) (3 * n) 1 \u2264\n\nfold 2* (fold 1) n (fold 2* (fold 1) n (fold 2* (fold 1) n 1)) \u2264\n\nfold 1 (fold 2* (fold 1) n) 3 \u2264\nfold 2* (fold 1) (1 + n) 3\n-}\n\n lem>=3'' : \u2200 m n \u2192 3 \u2264 2 \u2191\u27e8 suc m \u27e9 (3 + n)\n lem>=3'' zero n = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n lem>=3'' (suc zero) n rewrite \u2191\u2082-* 2 (3 + n) = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n lem>=3'' (suc (suc m)) n = lem>=3 m n\n\n lem2221 : \u2200 m \u2192 2 \u2261 2 \u2191\u27e8 2 + m \u27e9 1\n lem2221 zero = \u2261.refl\n lem2221 (suc m) = lem2221 m\n\n lem4212 : \u2200 m \u2192 4 \u2261 2 \u2191\u27e8 1 + m \u27e9 2\n lem4212 zero = \u2261.refl\n lem4212 (suc m) = 4 \u2261\u27e8 lem4212 m \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 2 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 (1 + m)) (lem2221 m) \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 (2 \u2191\u27e8 2 + m \u27e9 1) \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 2 + m \u27e9 2 \u220e\n where open \u2261-Reasoning\n\n ack-\u2191 : \u2200 m n \u2192 3 + ack m n \u2261 2 \u2191\u27e8 m \u27e9 (3 + n)\n ack-\u2191 zero n = \u2261.refl\n ack-\u2191 (suc m) zero = 3 + ack (suc m) 0 \u2261\u27e8 ack-\u2191 m 1 \u27e9\n 2 \u2191\u27e8 m \u27e9 4 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 m) (lem4212 m) \u27e9\n 2 \u2191\u27e8 suc m \u27e9 3 \u220e\n where open \u2261-Reasoning\n ack-\u2191 (suc m) (suc n) = 3 + ack (suc m) (suc n)\n \u2261\u27e8 \u2261.refl \u27e9\n 3 + ack m (ack (suc m) n)\n \u2261\u27e8 \u2261.refl \u27e9\n 3 + ack m ((3 + ack (suc m) n) \u2238 3)\n \u2261\u27e8 \u2261.cong (\u03bb x \u2192 3 + ack m (x \u2238 3)) (ack-\u2191 (suc m) n) \u27e9\n 3 + ack m (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3)\n \u2261\u27e8 ack-\u2191 m (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3) \u27e9\n 2 \u2191\u27e8 m \u27e9 (3 + (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3))\n \u2261\u27e8 \u2261.cong (\u03bb x \u2192 2 \u2191\u27e8 m \u27e9 x) (lem\u2238 (lem>=3'' m n)) \u27e9\n 2 \u2191\u27e8 m \u27e9 (2 \u2191\u27e8 suc m \u27e9 (3 + n))\n \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 suc m \u27e9 (4 + n) \u220e \n where open \u2261-Reasoning\n\n postulate\n 1+a^-infl< : \u2200 {a} \u2192 Infl< (_^_ (1 + a))\n\n -- 2+a*1+b-infl< : \u2200 a \u2192 Infl< (\u03bb x \u2192 (2 + a) * (1 + x))\n -- \u2200 a b \u2192 b < (2 + a) * b\n -- fold-a*-fold1 : \u2200 {n a} \u2192 Infl< (_\u21912+\u27e8_\u27e9_ (2 + a) n)\n fold-a^-fold1 : \u2200 {n a} \u2192 Infl< (fold (_^_ (1 + a)) (fold 1) n)\n fold-a^-fold1 {n} = fold-infl< 1+a^-infl< fold1+-inflT< {n}\n\n \u21913+-mon : \u2200 a n \u2192 Mon (fold (_^_ (1 + a)) (fold 1) n)\n \u21913+-mon a n = fold-mon' 1+a^-mon 1+a^-infl< (\u03bb \u03b7\u2081 \u03b7\u2082 \u2192 fold-mon \u03b7\u2081 \u03b7\u2082) fold1+-inflT< {n}\n\n{-\n open \u2191-Props\n\n -*\u27e81+n\u27e9-infl : \u2200 n \u2192 Infl (_*_ (1 + n))\n -*\u27e81+n\u27e9-infl n = {!!}\n\n lem121 : \u2200 a b \u2192 b < (1 + a) \u2191\u27e8 2 \u27e9 (1 + b)\n lem121 a b rewrite \u2191\u2082-* (1 + a) (1 + b)\n = 1 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (1 + a) * (1 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (1 + a) (1 + b) \u27e9\n (1 + b) * (1 + a)\n \u220e\n where open \u2264-Reasoning\n\n -- \u2200 a b \u2192 b < (2 + a) * b\n lem222 : \u2200 a b \u2192 b < (2 + a) \u2191\u27e8 2 \u27e9 (2 + b)\n lem222 a b rewrite \u2191\u2082-* (2 + a) (2 + b)\n = 1 + b\n \u2264\u27e8 suc-infl \u27e9\n 2 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (2 + a) * (2 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (2 + a) (2 + b) \u27e9\n (2 + b) * (2 + a)\n \u220e\n where open \u2264-Reasoning\n-}\n{-\n open fold-Props\n\n module Foo a where\n f = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b\n f-lem : \u2200 n \u2192 f (suc n) \u2257 fold 1 (f n)\n f-lem n x = \u2261.refl\n\n f2 = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n f2-lem : \u2200 n \u2192 f2 (suc n) \u2257 fold 1 (f2 n)\n f2-lem n x = {!!}\n\n -- b < (1 + a) * (1 + b)\n\n -- nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n infl3< : \u2200 a n \u2192 Infl< (\u03bb b \u2192 (1 + a) \u21912+\u27e8 n \u27e9 (1 + b))\n infl3< a zero {b} = lem121 a b\n infl3< a (suc n) {b}\n = 1 + b\n -- fold1-infl< : \u2200 f \u2192 Infl< f \u2192 \u2200 x \u2192 x < fold 1 f x\n \u2264\u27e8 {!fold1f2-infl< {_}!} \u27e9\n fold 1 f1 b\n \u2264\u27e8 {!!} \u27e9\n{-\n \u2264\u27e8 fold1-infl< (f 0) (\u03bb {x} \u2192 {!infl3< a n {x}!}) {{!!}} \u27e9\n {!!}\n \u2264\u27e8 {!!} \u27e9\n-}\n{-\n 1 + b\n (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n \u2264\u27e8 {!nest-infl< (\u03bb b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b) ? b {(2 + a) \u21912+\u27e8 n \u27e9 (2 + b)}!} \u27e9\n fold (f 0 1) (f 0) (1 + b)\n \u2264\u27e8 {!\u2115\u2264.reflexive (\u2261.cong (\u03bb g \u2192 g 1) (nest-Properties.nest-+ (f 0) 1 (1 + b)))!} \u27e9\n-}\n fold 1 (f 0) (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n f 1 (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) \u21912+\u27e8 suc n \u27e9 (1 + b)\n \u220e\n\n where\n f = \u03bb k b \u2192 (1 + a) \u21912+\u27e8 k + n \u27e9 b\n f1 = f 0 \u2218 suc\n\n -- fold1f2-infl< : Infl< (fold 1 f2)\n -- fold1f2-infl< = fold1-infl< f2 {!(\u03bb b \u2192 infl3< a n b ?)!}\n\n fold1f1-infl<' : Infl< (fold 1 f1)\n fold1f1-infl<' = {!fold1+1-infl< (f 0) {!infl3< a n!}!}\n lem : \u2200 x \u2192 f 0 (1 + x) < f 0 (f 0 x)\n lem x = {!!}\n postulate\n finfl : Infl< (f 0)\n open \u2264-Reasoning\n-}\n\n_\u21d1\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\na \u21d1\u27e8 n , k \u27e9 b = fold k f n\n module M\u21d1 where \n f : \u2115 \u2192 \u2115\n f x = a \u2191\u27e8 2 + x \u27e9 b\n\nmodule \u21d1-Props where\n _\u21d1\u2032\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\n a \u21d1\u2032\u27e8 n\u2080 , k \u27e9 b = g n\u2080\n module M\u21d1\u2032 where\n h : \u2115 \u2192 \u2115\n h x = a \u2191\u27e8 2 + x \u27e9 b\n\n g : \u2115 \u2192 \u2115\n g 0 = k\n g (suc n) = h (g n)\n\n g-nest : \u2200 n \u2192 g n \u2261 h $\u27e8 n \u27e9 k\n g-nest zero = \u2261.refl\n g-nest (suc n) rewrite g-nest n = \u2261.refl\n\n \u21d1\u2080 : \u2200 a k b \u2192 a \u21d1\u27e8 0 , k \u27e9 b \u2261 k\n \u21d1\u2080 _ _ _ = \u2261.refl\n\n \u21d1\u2081 : \u2200 a k b \u2192 a \u21d1\u27e8 1 , k \u27e9 b \u2261 a \u2191\u27e8 2 + k \u27e9 b\n \u21d1\u2081 _ _ _ = \u2261.refl\n\n \u21d1-\u21d1\u2032 : \u2200 a n k b \u2192 a \u21d1\u27e8 n , k \u27e9 b \u2261 a \u21d1\u2032\u27e8 n , k \u27e9 b\n \u21d1-\u21d1\u2032 a n k b rewrite M\u21d1\u2032.g-nest a n k b n = \u2261.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Graham%27s_number\nGraham's-number : \u2115\nGraham's-number = 3 \u21d1\u27e8 64 , 4 \u27e9 3\n -- = (f\u2218\u2076\u2074\u2026\u2218f)4\n -- where f x = 3 \u2191\u27e8 2 + x \u27e9 3\n\n{-\nmodule GeneralisedArithmetic {a} {A : Set a} (0# : A) (1+ : A \u2192 A) where\n\n 1# : A\n 1# = 1+ 0#\n\n open Data.Nat.GeneralisedArithmetic 0# 1+ public\n\n exp : (* : A \u2192 A \u2192 A) \u2192 (\u2115 \u2192 A \u2192 A)\n exp _*_ n b = fold 1# (\u03bb s \u2192 b * s) n\n-}\n -- hyperop a n b = fold exp\n\nmodule == where\n _\u2248_ : (m n : \u2115) \u2192 Set\n m \u2248 n = T (m == n)\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {zero} {zero} _ = id\n subst P {suc _} {suc _} p = subst (P \u2218 suc) p\n subst _ {zero} {suc _} ()\n subst _ {suc _} {zero} ()\n\n sound : \u2200 m n \u2192 T (m == n) \u2192 m \u2261 n\n sound m n p = subst (_\u2261_ m) p \u2261.refl\n\n refl : Reflexive _\u2248_\n refl {zero} = _\n refl {suc n} = refl {n}\n\n sym : Symmetric _\u2248_\n sym {m} {n} eq rewrite sound m n eq = refl {n}\n\n trans : Transitive _\u2248_\n trans {m} {n} {o} m\u2248n n\u2248o rewrite sound m n m\u2248n | sound n o n\u2248o = refl {o}\n\n setoid : Setoid _ _\n setoid = record { Carrier = \u2115; _\u2248_ = _\u2248_\n ; isEquivalence =\n record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} } }\n\n open Setoid setoid public hiding (refl; sym; trans; _\u2248_)\n\n{-\ndata _`\u2264?`_\u219d_ : (m n : \u2115) \u2192 Dec (m \u2264 n) \u2192 Set where\n z\u2264?n : \u2200 {n} \u2192 zero `\u2264?` n \u219d yes z\u2264n\n s\u2264?z : \u2200 {m} \u2192 suc m `\u2264?` zero \u219d no \u03bb()\n s\u2264?s-yes : \u2200 {m n m\u2264n} \u2192 m `\u2264?` n \u219d yes m\u2264n \u2192 suc m `\u2264?` suc n \u219d yes (s\u2264s m\u2264n)\n s\u2264?s-no : \u2200 {m n m\u2270n} \u2192 m `\u2264?` n \u219d no m\u2270n \u2192 suc m `\u2264?` suc n \u219d no (m\u2270n \u2218 \u2264-pred)\n\n`\u2264?`-complete : \u2200 m n \u2192 m `\u2264?` n \u219d (m \u2264? n)\n`\u2264?`-complete zero n = z\u2264?n\n`\u2264?`-complete (suc n) zero = {!s\u2264?z!}\n`\u2264?`-complete (suc m) (suc n) with m \u2264? n | `\u2264?`-complete m n\n... | yes q | r = s\u2264?s-yes r\n... | no q | r = s\u2264?s-no {!!}\n-}\n\n_<=_ : (x y : \u2115) \u2192 Bool\nzero <= _ = true\nsuc _ <= zero = false\nsuc m <= suc n = m <= n\n\nmodule <= where\n sound : \u2200 m n \u2192 T (m <= n) \u2192 m \u2264 n\n sound zero _ _ = z\u2264n\n sound (suc m) (suc n) p = s\u2264s (sound m n p)\n sound (suc m) zero ()\n\n complete : \u2200 {m n} \u2192 m \u2264 n \u2192 T (m <= n)\n complete z\u2264n = _\n complete (s\u2264s m\u2264n) = complete m\u2264n\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (Props.\u2264-steps 1 1+n\u2264m)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"3e2e2af74570385b18c0b4160f86bc1c38ea5cc0","subject":"Realign member order in env. validity","message":"Realign member order in env. validity\n","repos":"inc-lc\/ilc-agda","old_file":"New\/NewNew.agda","new_file":"New\/NewNew.agda","new_contents":"module New.NewNew where\n\nopen import New.Changes\nopen import New.LangChanges\nopen import New.Lang\nopen import New.Types\nopen import New.Derive\n\n[_]_from_to_ : \u2200 (\u03c4 : Type) \u2192 (dv : Cht \u03c4) \u2192 (v1 v2 : \u27e6 \u03c4 \u27e7Type) \u2192 Set\n[ \u03c3 \u21d2 \u03c4 ] df from f1 to f2 =\n \u2200 (da : Cht \u03c3) (a1 a2 : \u27e6 \u03c3 \u27e7Type) \u2192\n [ \u03c3 ] da from a1 to a2 \u2192 [ \u03c4 ] df a1 da from f1 a1 to f2 a2\n[ int ] dv from v1 to v2 = v2 \u2261 v1 + dv\n[ pair \u03c3 \u03c4 ] (da , db) from (a1 , b1) to (a2 , b2) = [ \u03c3 ] da from a1 to a2 \u00d7 [ \u03c4 ] db from b1 to b2\n\n[_]\u0393_from_to_ : \u2200 \u0393 \u2192 eCh \u0393 \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 Set\n[ \u2205 ]\u0393 \u2205 from \u2205 to \u2205 = \u22a4\n[ \u03c4 \u2022 \u0393 ]\u0393 (dv \u2022 d\u03c1) from (v1 \u2022 \u03c11) to (v2 \u2022 \u03c12) =\n [ \u03c4 ] dv from v1 to v2 \u00d7 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12\n\nfromtoDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192\n (d\u03c1 : eCh \u0393) (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ] (\u27e6 x \u27e7\u0394Var \u03c11 d\u03c1) from (\u27e6 x \u27e7Var \u03c11) to (\u27e6 x \u27e7Var \u03c12)\nfromtoDeriveVar this (dv \u2022 d\u03c1) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dvv , d\u03c1\u03c1) = dvv\nfromtoDeriveVar (that x) (dv \u2022 d\u03c1) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dvv , d\u03c1\u03c1) = fromtoDeriveVar x d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1\n\nfromtoDeriveConst : \u2200 {\u03c4} c \u2192\n [ \u03c4 ] \u27e6 deriveConst c \u27e7Term \u2205 from \u27e6 c \u27e7Const to \u27e6 c \u27e7Const\nfromtoDeriveConst plus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb = mn\u00b7pq=mp\u00b7nq {a1} {da} {b1} {db}\nfromtoDeriveConst minus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb | sym (-m\u00b7-n=-mn {b1} {db}) = mn\u00b7pq=mp\u00b7nq {a1} {da} { - b1} { - db}\nfromtoDeriveConst cons da a1 a2 daa db b1 b2 dbb = daa , dbb\nfromtoDeriveConst fst (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = daa\nfromtoDeriveConst snd (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = dbb\n\nfromtoDerive : \u2200 {\u0393} \u03c4 \u2192 (t : Term \u0393 \u03c4) \u2192\n {d\u03c1 : eCh \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ] (\u27e6 t \u27e7\u0394Term \u03c11 d\u03c1) from (\u27e6 t \u27e7Term \u03c11) to (\u27e6 t \u27e7Term \u03c12)\nfromtoDerive \u03c4 (const c) {d\u03c1} {\u03c11} d\u03c1\u03c1 rewrite \u27e6 c \u27e7\u0394Const-rewrite \u03c11 d\u03c1 = fromtoDeriveConst c\nfromtoDerive \u03c4 (var x) d\u03c1\u03c1 = fromtoDeriveVar x _ _ _ d\u03c1\u03c1\nfromtoDerive \u03c4 (app s t) {d\u03c1} {\u03c11} d\u03c1\u03c1 rewrite sym (fit-sound t \u03c11 d\u03c1) =\n let fromToF = fromtoDerive (_ \u21d2 \u03c4) s d\u03c1\u03c1\n in let fromToB = fromtoDerive _ t d\u03c1\u03c1 in fromToF _ _ _ fromToB\nfromtoDerive (\u03c3 \u21d2 \u03c4) (abs t) d\u03c1\u03c1 = \u03bb da a1 a2 daa \u2192\n fromtoDerive _ t (daa , d\u03c1\u03c1)\n","old_contents":"module New.NewNew where\n\nopen import New.Changes\nopen import New.LangChanges\nopen import New.Lang\nopen import New.Types\nopen import New.Derive\n\n[_]_from_to_ : \u2200 (\u03c4 : Type) \u2192 (dv : Cht \u03c4) \u2192 (v1 v2 : \u27e6 \u03c4 \u27e7Type) \u2192 Set\n[ \u03c3 \u21d2 \u03c4 ] df from f1 to f2 =\n \u2200 (da : Cht \u03c3) (a1 a2 : \u27e6 \u03c3 \u27e7Type) \u2192\n [ \u03c3 ] da from a1 to a2 \u2192 [ \u03c4 ] df a1 da from f1 a1 to f2 a2\n[ int ] dv from v1 to v2 = v2 \u2261 v1 + dv\n[ pair \u03c3 \u03c4 ] (da , db) from (a1 , b1) to (a2 , b2) = [ \u03c3 ] da from a1 to a2 \u00d7 [ \u03c4 ] db from b1 to b2\n\n[_]\u0393_from_to_ : \u2200 \u0393 \u2192 eCh \u0393 \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 Set\n[ \u2205 ]\u0393 \u2205 from \u2205 to \u2205 = \u22a4\n[ \u03c4 \u2022 \u0393 ]\u0393 (dv \u2022 d\u03c1) from (v1 \u2022 \u03c11) to (v2 \u2022 \u03c12) =\n [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u00d7 [ \u03c4 ] dv from v1 to v2\n\nfromtoDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192\n (d\u03c1 : eCh \u0393) (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ] (\u27e6 x \u27e7\u0394Var \u03c11 d\u03c1) from (\u27e6 x \u27e7Var \u03c11) to (\u27e6 x \u27e7Var \u03c12)\nfromtoDeriveVar this (dv \u2022 d\u03c1) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dvv , d\u03c1\u03c1) = d\u03c1\u03c1\nfromtoDeriveVar (that x) (dv \u2022 d\u03c1) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dvv , d\u03c1\u03c1) = fromtoDeriveVar x d\u03c1 \u03c11 \u03c12 dvv\n\nfromtoDeriveConst : \u2200 {\u03c4} c \u2192\n [ \u03c4 ] \u27e6 deriveConst c \u27e7Term \u2205 from \u27e6 c \u27e7Const to \u27e6 c \u27e7Const\nfromtoDeriveConst plus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb = mn\u00b7pq=mp\u00b7nq {a1} {da} {b1} {db}\nfromtoDeriveConst minus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb | sym (-m\u00b7-n=-mn {b1} {db}) = mn\u00b7pq=mp\u00b7nq {a1} {da} { - b1} { - db}\nfromtoDeriveConst cons da a1 a2 daa db b1 b2 dbb = daa , dbb\nfromtoDeriveConst fst (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = daa\nfromtoDeriveConst snd (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = dbb\n\nfromtoDerive : \u2200 {\u0393} \u03c4 \u2192 (t : Term \u0393 \u03c4) \u2192\n {d\u03c1 : eCh \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ] (\u27e6 t \u27e7\u0394Term \u03c11 d\u03c1) from (\u27e6 t \u27e7Term \u03c11) to (\u27e6 t \u27e7Term \u03c12)\nfromtoDerive \u03c4 (const c) {d\u03c1} {\u03c11} d\u03c1\u03c1 rewrite \u27e6 c \u27e7\u0394Const-rewrite \u03c11 d\u03c1 = fromtoDeriveConst c\nfromtoDerive \u03c4 (var x) d\u03c1\u03c1 = fromtoDeriveVar x _ _ _ d\u03c1\u03c1\nfromtoDerive \u03c4 (app s t) {d\u03c1} {\u03c11} d\u03c1\u03c1 rewrite sym (fit-sound t \u03c11 d\u03c1) =\n let fromToF = fromtoDerive (_ \u21d2 \u03c4) s d\u03c1\u03c1\n in let fromToB = fromtoDerive _ t d\u03c1\u03c1 in fromToF _ _ _ fromToB\nfromtoDerive (\u03c3 \u21d2 \u03c4) (abs t) d\u03c1\u03c1 = \u03bb da a1 a2 daa \u2192\n fromtoDerive _ t (d\u03c1\u03c1 , daa)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"e7633a6efa1638c88b0e30d79e852a1320869251","subject":"tree permutations, made easy!","message":"tree permutations, made easy!\n","repos":"crypto-agda\/crypto-agda","old_file":"prefect-bintree.agda","new_file":"prefect-bintree.agda","new_contents":"module prefect-bintree where\n\nopen import Function\nopen import Data.Nat.NP using (\u2115; zero; suc; 2^_; _+_; module \u2115\u00b0)\nopen import Data.Bool\nopen import Data.Bits\nopen import Data.Vec using (Vec; _++_)\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Algebra.FunctionProperties\n\ndata Tree {a} (A : Set a) : \u2115 \u2192 Set a where\n leaf : (x : A) \u2192 Tree A zero\n fork : \u2200 {n} (left right : Tree A n) \u2192 Tree A (suc n)\n\nfromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Tree A n\nfromFun {zero} f = leaf (f [])\nfromFun {suc n} f = fork (fromFun (f \u2218 0\u2237_)) (fromFun (f \u2218 1\u2237_))\n\ntoFun : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Bits n \u2192 A\ntoFun (leaf x) _ = x\ntoFun (fork left right) (b \u2237 bs) = toFun (if b then right else left) bs\n\nlookup : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Tree A n \u2192 A\nlookup = flip toFun\n\nfold : \u2200 {n a} {A : Set a} (op : A \u2192 A \u2192 A) \u2192 Tree A n \u2192 A\nfold _ (leaf x) = x\nfold _\u00b7_ (fork t\u2080 t\u2081) = fold _\u00b7_ t\u2080 \u00b7 fold _\u00b7_ t\u2081\n\nsearch\u2261fold\u2218fromFun : \u2200 {n a} {A : Set a} op (f : Bits n \u2192 A) \u2192 search op f \u2261 fold op (fromFun f)\nsearch\u2261fold\u2218fromFun {zero} op f = refl\nsearch\u2261fold\u2218fromFun {suc n} op f\n rewrite search\u2261fold\u2218fromFun op (f \u2218 0\u2237_)\n | search\u2261fold\u2218fromFun op (f \u2218 1\u2237_) = refl\n\n-- Returns the flat vector of leaves underlying the perfect binary tree.\ntoVec : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Vec A (2^ n)\ntoVec (leaf x) = x \u2237 []\ntoVec (fork t\u2080 t\u2081) = toVec t\u2080 ++ toVec t\u2081\n\nlookup' : \u2200 {m n a} {A : Set a} \u2192 Bits m \u2192 Tree A (m + n) \u2192 Tree A n\nlookup' [] t = t\nlookup' (b \u2237 bs) (fork t t\u2081) = lookup' bs (if b then t\u2081 else t)\n\n\nupdate' : \u2200 {m n a} {A : Set a} \u2192 Bits m \u2192 Tree A n \u2192 Tree A (m + n) \u2192 Tree A (m + n)\nupdate' [] val tree = val\nupdate' (b \u2237 key) val (fork tree tree\u2081) = if b then fork tree (update' key val tree\u2081) \n else fork (update' key val tree) tree\u2081\n\nmap : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 Tree A n \u2192 Tree B n\nmap f (leaf x) = leaf (f x)\nmap f (fork t\u2080 t\u2081) = fork (map f t\u2080) (map f t\u2081)\n\nopen import Relation.Binary\nopen import Data.Star using (Star; \u03b5; _\u25c5_)\n\ndata Swp {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n left : \u2200 {n} {left\u2080 left\u2081 right : Tree A n} \u2192\n Swp left\u2080 left\u2081 \u2192\n Swp (fork left\u2080 right) (fork left\u2081 right)\n right : \u2200 {n} {left right\u2080 right\u2081 : Tree A n} \u2192\n Swp right\u2080 right\u2081 \u2192\n Swp (fork left right\u2080) (fork left right\u2081)\n swp\u2081 : \u2200 {n} {left right : Tree A n} \u2192\n Swp (fork left right) (fork right left)\n swp\u2082 : \u2200 {n} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Swp (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2081\u2081 t\u2080\u2081) (fork t\u2081\u2080 t\u2080\u2080))\n\nSwp\u2605 : \u2200 {n a} {A : Set a} (left right : Tree A n) \u2192 Set a\nSwp\u2605 = Star Swp\n\nSwp-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Swp {A = A} {n})\nSwp-sym (left s) = left (Swp-sym s)\nSwp-sym (right s) = right (Swp-sym s)\nSwp-sym swp\u2081 = swp\u2081\nSwp-sym swp\u2082 = swp\u2082\n\ndata Rot {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n leaf : \u2200 x \u2192 Rot (leaf x) (leaf x)\n fork : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 left\u2081 \u2192\n Rot right\u2080 right\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n krof : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 right\u2081 \u2192\n Rot right\u2080 left\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n\nRot-refl : \u2200 {n a} {A : Set a} \u2192 Reflexive (Rot {A = A} {n})\nRot-refl {x = leaf x} = leaf x\nRot-refl {x = fork _ _} = fork Rot-refl Rot-refl\n\nRot-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Rot {A = A} {n})\nRot-sym (leaf x) = leaf x\nRot-sym (fork p\u2080 p\u2081) = fork (Rot-sym p\u2080) (Rot-sym p\u2081)\nRot-sym (krof p\u2080 p\u2081) = krof (Rot-sym p\u2081) (Rot-sym p\u2080)\n\nRot-trans : \u2200 {n a} {A : Set a} \u2192 Transitive (Rot {A = A} {n})\nRot-trans (leaf x) q = q\nRot-trans (fork p\u2080 p\u2081) (fork q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (fork p\u2080 p\u2081) (krof q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (krof p\u2080 p\u2081) (fork q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\nRot-trans (krof p\u2080 p\u2081) (krof q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\n\ndata SwpOp : \u2115 \u2192 Set where\n \u03b5 : \u2200 {n} \u2192 SwpOp n\n\n _\u204f_ : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp n \u2192 SwpOp n\n\n first : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp (suc n)\n\n swp : \u2200 {n} \u2192 SwpOp (suc n)\n\n swp-seconds : \u2200 {n} \u2192 SwpOp (2 + n)\n\ndata Perm {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n \u03b5 : \u2200 {n} {t : Tree A n} \u2192 Perm t t\n\n _\u204f_ : \u2200 {n} {t u v : Tree A n} \u2192 Perm t u \u2192 Perm u v \u2192 Perm t v\n\n first : \u2200 {n} {tA tB tC : Tree A n} \u2192\n Perm tA tB \u2192\n Perm (fork tA tC) (fork tB tC)\n\n swp : \u2200 {n} {tA tB : Tree A n} \u2192\n Perm (fork tA tB) (fork tB tA)\n\n swp-seconds : \u2200 {n} {tA tB tC tD : Tree A n} \u2192\n Perm (fork (fork tA tB) (fork tC tD))\n (fork (fork tA tD) (fork tC tB))\n\ndata Perm0\u2194 {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n \u03b5 : \u2200 {n} {t : Tree A n} \u2192 Perm0\u2194 t t\n\n swp : \u2200 {n} {tA tB : Tree A n} \u2192\n Perm0\u2194 (fork tA tB) (fork tB tA)\n\n first : \u2200 {n} {tA tB tC : Tree A n} \u2192\n Perm0\u2194 tA tB \u2192\n Perm0\u2194 (fork tA tC) (fork tB tC)\n\n firsts : \u2200 {n} {tA tB tC tD tE tF : Tree A n} \u2192\n Perm0\u2194 (fork tA tC) (fork tE tF) \u2192\n Perm0\u2194 (fork (fork tA tB) (fork tC tD))\n (fork (fork tE tB) (fork tF tD))\n\n extremes : \u2200 {n} {tA tB tC tD tE tF : Tree A n} \u2192\n Perm0\u2194 (fork tA tD) (fork tE tF) \u2192\n Perm0\u2194 (fork (fork tA tB) (fork tC tD))\n (fork (fork tA tB) (fork tC tF))\n\n-- Star Perm0\u2194 can then model any permutation\n\ninfixr 1 _\u204f_\n\nsecond-perm : \u2200 {a} {A : Set a} {n} {left right\u2080 right\u2081 : Tree A n} \u2192\n Perm right\u2080 right\u2081 \u2192\n Perm (fork left right\u2080) (fork left right\u2081)\nsecond-perm f = swp \u204f first f \u204f swp\n\nsecond-swpop : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp (suc n)\nsecond-swpop f = swp \u204f first f \u204f swp\n\n<_\u00d7_>-perm : \u2200 {a} {A : Set a} {n} {left\u2080 right\u2080 left\u2081 right\u2081 : Tree A n} \u2192\n Perm left\u2080 left\u2081 \u2192\n Perm right\u2080 right\u2081 \u2192\n Perm (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n< f \u00d7 g >-perm = first f \u204f second-perm g\n\nswp\u2082-perm : \u2200 {a n} {A : Set a} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Perm (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2081\u2081 t\u2080\u2081) (fork t\u2081\u2080 t\u2080\u2080))\nswp\u2082-perm = first swp \u204f swp-seconds \u204f first swp\n\nswp\u2083-perm : \u2200 {a n} {A : Set a} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Perm (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2080\u2080 t\u2081\u2080) (fork t\u2080\u2081 t\u2081\u2081))\nswp\u2083-perm = second-perm swp \u204f swp-seconds \u204f second-perm swp\n\nswp-firsts-perm : \u2200 {n a} {A : Set a} {tA tB tC tD : Tree A n} \u2192\n Perm (fork (fork tA tB) (fork tC tD))\n (fork (fork tC tB) (fork tA tD))\nswp-firsts-perm = < swp \u00d7 swp >-perm \u204f swp-seconds \u204f < swp \u00d7 swp >-perm\n\nSwp\u21d2Perm : \u2200 {n a} {A : Set a} \u2192 Swp {a} {A} {n} \u21d2 Perm {n = n}\nSwp\u21d2Perm (left pf) = first (Swp\u21d2Perm pf)\nSwp\u21d2Perm (right pf) = second-perm (Swp\u21d2Perm pf)\nSwp\u21d2Perm swp\u2081 = swp\nSwp\u21d2Perm swp\u2082 = swp\u2082-perm\n\nSwp\u2605\u21d2Perm : \u2200 {n a} {A : Set a} \u2192 Swp\u2605 {n} {a} {A} \u21d2 Perm {n = n}\nSwp\u2605\u21d2Perm \u03b5 = \u03b5\nSwp\u2605\u21d2Perm (x \u25c5 xs) = Swp\u21d2Perm x \u204f Swp\u2605\u21d2Perm xs\n\nswp-inners : \u2200 {n} \u2192 SwpOp (2 + n)\nswp-inners = second-swpop swp \u204f swp-seconds \u204f second-swpop swp\n\non-extremes : \u2200 {n} \u2192 SwpOp (1 + n) \u2192 SwpOp (2 + n)\non-extremes f = swp-seconds \u204f first f \u204f swp-seconds\n\non-firsts : \u2200 {n} \u2192 SwpOp (1 + n) \u2192 SwpOp (2 + n)\non-firsts f = swp-inners \u204f first f \u204f swp-inners\n\n0\u2194_ : \u2200 {m n} \u2192 Bits m \u2192 SwpOp (m + n)\n0\u2194 [] = \u03b5\n0\u2194 (false{-0-} \u2237 p) = first (0\u2194 p)\n0\u2194 (true{-1-} \u2237 []) = swp\n0\u2194 (true{-1-} \u2237 true {-1-} \u2237 p) = on-extremes (0\u2194 (1b \u2237 p))\n0\u2194 (true{-1-} \u2237 false{-0-} \u2237 p) = on-firsts (0\u2194 (1b \u2237 p))\n\n0\u2194\u2032_ : \u2200 {n} \u2192 Bits n \u2192 SwpOp n\n0\u2194\u2032_ {n} rewrite cong SwpOp (sym (\u2115\u00b0.+-comm n 0)) = 0\u2194_ {n} {0}\n\n[_\u2194_] : \u2200 {m n} (p q : Bits m) \u2192 SwpOp (m + n)\n[ p \u2194 q ] = 0\u2194 p \u204f 0\u2194 q\n\n[_\u2194\u2032_] : \u2200 {n} (p q : Bits n) \u2192 SwpOp n\n[ p \u2194\u2032 q ] = 0\u2194\u2032 p \u204f 0\u2194\u2032 q\n\n_$swp_ : \u2200 {n a} {A : Set a} \u2192 SwpOp n \u2192 Tree A n \u2192 Tree A n\n\u03b5 $swp t = t\n(f \u204f g) $swp t = g $swp (f $swp t)\n(first f) $swp (fork t\u2080 t\u2081) = fork (f $swp t\u2080) t\u2081\nswp $swp (fork t\u2080 t\u2081) = fork t\u2081 t\u2080\nswp-seconds $swp (fork (fork t\u2080 t\u2081) (fork t\u2082 t\u2083)) = fork (fork t\u2080 t\u2083) (fork t\u2082 t\u2081)\n\nswpRel : \u2200 {n a} {A : Set a} (f : SwpOp n) (t : Tree A n) \u2192 Perm t (f $swp t)\nswpRel \u03b5 _ = \u03b5\nswpRel (f \u204f g) _ = swpRel f _ \u204f swpRel g _\nswpRel (first f) (fork _ _) = first (swpRel f _)\nswpRel swp (fork _ _) = swp\nswpRel swp-seconds\n (fork (fork _ _) (fork _ _)) = swp-seconds\n\n[0\u2194_]-Rel : \u2200 {m n a} {A : Set a} (p : Bits m) (t : Tree A (m + n)) \u2192 Perm t ((0\u2194 p) $swp t)\n[0\u2194 p ]-Rel = swpRel (0\u2194 p)\n\nswpOp' : \u2200 {n a} {A : Set a} {t u : Tree A n} \u2192 Perm0\u2194 t u \u2192 SwpOp n\nswpOp' \u03b5 = \u03b5\nswpOp' (first f) = first (swpOp' f)\nswpOp' swp = swp\nswpOp' (firsts f) = on-firsts (swpOp' f)\nswpOp' (extremes f) = on-extremes (swpOp' f)\n\nswpOp : \u2200 {n a} {A : Set a} {t u : Tree A n} \u2192 Perm t u \u2192 SwpOp n\nswpOp \u03b5 = \u03b5\nswpOp (f \u204f g) = swpOp f \u204f swpOp g\nswpOp (first f) = first (swpOp f)\nswpOp swp = swp\nswpOp swp-seconds = swp-seconds\n\nswpOp-sym : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp n\nswpOp-sym \u03b5 = \u03b5\nswpOp-sym (f \u204f g) = swpOp-sym g \u204f swpOp-sym f\nswpOp-sym (first f) = first (swpOp-sym f)\nswpOp-sym swp = swp\nswpOp-sym swp-seconds = swp-seconds\n\nswpOp-sym-involutive : \u2200 {n} (f : SwpOp n) \u2192 swpOp-sym (swpOp-sym f) \u2261 f\nswpOp-sym-involutive \u03b5 = refl\nswpOp-sym-involutive (f \u204f g) rewrite swpOp-sym-involutive f | swpOp-sym-involutive g = refl\nswpOp-sym-involutive (first f) rewrite swpOp-sym-involutive f = refl\nswpOp-sym-involutive swp = refl\nswpOp-sym-involutive swp-seconds = refl\n\nswpOp-sym-sound : \u2200 {n a} {A : Set a} (f : SwpOp n) (t : Tree A n) \u2192 swpOp-sym f $swp (f $swp t) \u2261 t\nswpOp-sym-sound \u03b5 t = refl\nswpOp-sym-sound (f \u204f g) t rewrite swpOp-sym-sound g (f $swp t) | swpOp-sym-sound f t = refl\nswpOp-sym-sound (first f) (fork t _) rewrite swpOp-sym-sound f t = refl\nswpOp-sym-sound swp (fork _ _) = refl\nswpOp-sym-sound swp-seconds (fork (fork _ _) (fork _ _)) = refl\n\nmodule \u00acswp-comm where\n data X : Set where\n A B C D E F G H : X\n n : \u2115\n n = 3\n t : Tree X n\n t = fork (fork (fork (leaf A) (leaf B))(fork (leaf C) (leaf D))) (fork (fork (leaf E) (leaf F))(fork (leaf G) (leaf H)))\n f : SwpOp n\n f = swp\n g : SwpOp n\n g = first swp\n pf : f $swp (g $swp t) \u2262 g $swp (f $swp t)\n pf ()\n\nswp-leaf : \u2200 {a} {A : Set a} (f : SwpOp 0) (x : A) \u2192 f $swp (leaf x) \u2261 leaf x\nswp-leaf \u03b5 x = refl\nswp-leaf (f \u204f g) x rewrite swp-leaf f x | swp-leaf g x = refl\n\nswpOp-sound : \u2200 {n a} {A : Set a} {t u : Tree A n} (perm : Perm t u) \u2192 (swpOp perm $swp t \u2261 u)\nswpOp-sound \u03b5 = refl\nswpOp-sound (f \u204f f\u2081) rewrite swpOp-sound f | swpOp-sound f\u2081 = refl\nswpOp-sound (first f) rewrite swpOp-sound f = refl\nswpOp-sound swp = refl\nswpOp-sound swp-seconds = refl\n\nopen import Relation.Nullary using (Dec ; yes ; no)\nopen import Relation.Nullary.Negation\n\nmodule new-approach where\n\n open import Data.Empty\n\n import Function.Inverse as FI\n open FI using (_\u2194_; module Inverse; _InverseOf_)\n open import Function.Related\n import Function.Equality\n import Relation.Binary.PropositionalEquality as P\n\n data _\u2208_ {a}{A : Set a}(x : A) : {n : \u2115} \u2192 Tree A n \u2192 Set a where\n here : x \u2208 leaf x\n left : {n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 t\u2081 \u2192 x \u2208 fork t\u2081 t\u2082\n right : {n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 t\u2082 \u2192 x \u2208 fork t\u2081 t\u2082\n\n toBits : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t : Tree A n} \u2192 x \u2208 t \u2192 Bits n\n toBits here = []\n toBits (left key) = 0b \u2237 toBits key\n toBits (right key) = 1b \u2237 toBits key\n\n \u2208-lookup : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t : Tree A n}(path : x \u2208 t) \u2192 lookup (toBits path) t \u2261 x\n \u2208-lookup here = refl\n \u2208-lookup (left path) = \u2208-lookup path\n \u2208-lookup (right path) = \u2208-lookup path\n\n lookup-\u2208 : \u2200 {a}{A : Set a}{n : \u2115}(key : Bits n)(t : Tree A n) \u2192 lookup key t \u2208 t\n lookup-\u2208 [] (leaf x) = here\n lookup-\u2208 (true \u2237 key) (fork tree tree\u2081) = right (lookup-\u2208 key tree\u2081)\n lookup-\u2208 (false \u2237 key) (fork tree tree\u2081) = left (lookup-\u2208 key tree)\n\n _\u2248_ : \u2200 {a}{A : Set a}{n : \u2115} \u2192 Tree A n \u2192 Tree A n \u2192 Set _\n t\u2081 \u2248 t\u2082 = \u2200 x \u2192 (x \u2208 t\u2081) \u2194 (x \u2208 t\u2082) \n\n \u2248-refl : {a : _}{A : Set a}{n : \u2115}{t : Tree A n} \u2192 t \u2248 t\n \u2248-refl _ = FI.id\n\n move : \u2200 {a}{A : Set a}{n : \u2115}{t s : Tree A n}{x : A} \u2192 t \u2248 s \u2192 x \u2208 t \u2192 x \u2208 s\n move t\u2248s x\u2208t = Inverse.to (t\u2248s _) Function.Equality.\u27e8$\u27e9 x\u2208t\n\n swap\u2080 : \u2200 {a}{A : Set a}{n : \u2115}(t\u2081 t\u2082 : Tree A n) \u2192 fork t\u2081 t\u2082 \u2248 fork t\u2082 t\u2081\n swap\u2080 t\u2081 t\u2082 = \u03bb x \u2192 record \n { to = \u2192-to-\u27f6 swap\n ; from = \u2192-to-\u27f6 swap\n ; inverse-of = record { left-inverse-of = swap-inv \n ; right-inverse-of = swap-inv } \n } where\n swap : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 fork t\u2081 t\u2082 \u2192 x \u2208 fork t\u2082 t\u2081\n swap (left path) = right path\n swap (right path) = left path\n\n swap-inv : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t\u2081 t\u2082 : Tree A n}(p : x \u2208 fork t\u2081 t\u2082) \u2192 swap (swap p) \u2261 p\n swap-inv (left p) = refl\n swap-inv (right p) = refl\n\n _\u27e8fork\u27e9_ : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 s\u2081 s\u2082 : Tree A n} \u2192 t\u2081 \u2248 s\u2081 \u2192 t\u2082 \u2248 s\u2082 \u2192 fork t\u2081 t\u2082 \u2248 fork s\u2081 s\u2082\n (t1\u2248s1 \u27e8fork\u27e9 t2\u2248s2) y = record \n { to = to\n ; from = from\n ; inverse-of = record { left-inverse-of = frk-linv\n ; right-inverse-of = frk-rinv } \n } where\n \n frk : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 s\u2081 s\u2082 : Tree A n}{x : A} \u2192 t\u2081 \u2248 s\u2081 \u2192 t\u2082 \u2248 s\u2082 \u2192 x \u2208 fork t\u2081 t\u2082 \u2192 x \u2208 fork s\u2081 s\u2082\n frk t1\u2248s1 t2\u2248s2 (left x\u2208t1) = left (move t1\u2248s1 x\u2208t1)\n frk t1\u2248s1 t2\u2248s2 (right x\u2208t2) = right (move t2\u2248s2 x\u2208t2)\n \n to = \u2192-to-\u27f6 (frk t1\u2248s1 t2\u2248s2)\n from = \u2192-to-\u27f6 (frk (\u03bb x \u2192 FI.sym (t1\u2248s1 x)) (\u03bb x \u2192 FI.sym (t2\u2248s2 x)))\n\n \n open Function.Equality using (_\u27e8$\u27e9_)\n open import Function.LeftInverse\n\n frk-linv : from LeftInverseOf to\n frk-linv (left x) = cong left (_InverseOf_.left-inverse-of (Inverse.inverse-of (t1\u2248s1 y)) x)\n frk-linv (right x) = cong right (_InverseOf_.left-inverse-of (Inverse.inverse-of (t2\u2248s2 y)) x)\n\n frk-rinv : from RightInverseOf to -- \u2200 x \u2192 to \u27e8$\u27e9 (from \u27e8$\u27e9 x) \u2261 x\n frk-rinv (left x) = cong left (_InverseOf_.right-inverse-of (Inverse.inverse-of (t1\u2248s1 y)) x)\n frk-rinv (right x) = cong right (_InverseOf_.right-inverse-of (Inverse.inverse-of (t2\u2248s2 y)) x)\n\n Rot\u27f6\u2248 : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 Rot t\u2081 t\u2082 \u2192 t\u2081 \u2248 t\u2082\n Rot\u27f6\u2248 (leaf x) y = FI.id\n Rot\u27f6\u2248 (fork rot rot\u2081) y = (Rot\u27f6\u2248 rot \u27e8fork\u27e9 Rot\u27f6\u2248 rot\u2081) y\n Rot\u27f6\u2248 (krof {_} {l} {l'} {r} {r'} rot rot\u2081) y = \n y \u2208 fork l r \u2194\u27e8 (Rot\u27f6\u2248 rot \u27e8fork\u27e9 Rot\u27f6\u2248 rot\u2081) y \u27e9\n y \u2208 fork r' l' \u2194\u27e8 swap\u2080 r' l' y \u27e9 \n y \u2208 fork l' r' \u220e\n where open EquationalReasoning\n\n put : {a : _}{A : Set a}{n : \u2115} \u2192 Bits n \u2192 A \u2192 Tree A n \u2192 Tree A n\n put [] val tree = leaf val\n put (x \u2237 key) val (fork tree tree\u2081) = if x then fork tree (put key val tree\u2081) \n else fork (put key val tree) tree\u2081\n\n -- move-me\n _\u2237\u2262_ : {n : \u2115}{xs ys : Bits n}(x : Bit) \u2192 x \u2237 xs \u2262 x \u2237 ys \u2192 xs \u2262 ys\n _\u2237\u2262_ x = contraposition $ cong $ _\u2237_ x\n\n \u2208-put : {a : _}{A : Set a}{n : \u2115}(p : Bits n){x : A}(t : Tree A n) \u2192 x \u2208 put p x t\n \u2208-put [] t = here\n \u2208-put (true \u2237 p) (fork t t\u2081) = right (\u2208-put p t\u2081)\n \u2208-put (false \u2237 p) (fork t t\u2081) = left (\u2208-put p t)\n\n \u2208-put-\u2262 : {a : _}{A : Set a}{n : \u2115}(p : Bits n){x y : A}{t : Tree A n}(path : x \u2208 t)\n \u2192 p \u2262 toBits path \u2192 x \u2208 put p y t\n \u2208-put-\u2262 [] here neg = \u22a5-elim (neg refl)\n \u2208-put-\u2262 (true \u2237 p) (left path) neg = left path\n \u2208-put-\u2262 (false \u2237 p) (left path) neg = left (\u2208-put-\u2262 p path (false \u2237\u2262 neg))\n \u2208-put-\u2262 (true \u2237 p) (right path) neg = right (\u2208-put-\u2262 p path (true \u2237\u2262 neg))\n \u2208-put-\u2262 (false \u2237 p) (right path) neg = right path\n\n swap : {a : _}{A : Set a}{n : \u2115} \u2192 (p\u2081 p\u2082 : Bits n) \u2192 Tree A n \u2192 Tree A n\n swap p\u2081 p\u2082 t = put p\u2081 a\u2082 (put p\u2082 a\u2081 t)\n where\n a\u2081 = lookup p\u2081 t\n a\u2082 = lookup p\u2082 t\n\n swap-perm\u2081 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p : x \u2208 t) \u2192 t \u2248 swap (toBits p) (toBits p) t\n swap-perm\u2081 here = \u2248-refl \n swap-perm\u2081 (left path) = swap-perm\u2081 path \u27e8fork\u27e9 \u2248-refl\n swap-perm\u2081 (right path) = \u2248-refl \u27e8fork\u27e9 swap-perm\u2081 path\n\n swap-comm : {a : _}{A : Set a}{n : \u2115} (p\u2081 p\u2082 : Bits n)(t : Tree A n) \u2192 swap p\u2082 p\u2081 t \u2261 swap p\u2081 p\u2082 t\n swap-comm [] [] (leaf x) = refl\n swap-comm (true \u2237 p\u2081) (true \u2237 p\u2082) (fork t t\u2081) = cong (fork t) (swap-comm p\u2081 p\u2082 t\u2081)\n swap-comm (true \u2237 p\u2081) (false \u2237 p\u2082) (fork t t\u2081) = refl\n swap-comm (false \u2237 p\u2081) (true \u2237 p\u2082) (fork t t\u2081) = refl\n swap-comm (false \u2237 p\u2081) (false \u2237 p\u2082) (fork t t\u2081) = cong (flip fork t\u2081) (swap-comm p\u2081 p\u2082 t)\n\n swap-perm\u2082 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p' : Bits n)(p : x \u2208 t) \n \u2192 x \u2208 swap (toBits p) p' t\n swap-perm\u2082 _ here = here\n swap-perm\u2082 (true \u2237 p) (left path) rewrite \u2208-lookup path = right (\u2208-put p _)\n swap-perm\u2082 (false \u2237 p) (left path) = left (swap-perm\u2082 p path)\n swap-perm\u2082 (true \u2237 p) (right path) = right (swap-perm\u2082 p path)\n swap-perm\u2082 (false \u2237 p) (right path) rewrite \u2208-lookup path = left (\u2208-put p _)\n\n swap-perm\u2083 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p\u2081 p\u2082 : Bits n)(p : x \u2208 t)\n \u2192 p\u2081 \u2262 toBits p \u2192 p\u2082 \u2262 toBits p \u2192 x \u2208 swap p\u2081 p\u2082 t\n swap-perm\u2083 [] [] here neg\u2081 neg\u2082 = here\n swap-perm\u2083 (true \u2237 p\u2081) (true \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left path\n swap-perm\u2083 (true \u2237 p\u2081) (false \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left (\u2208-put-\u2262 _ path (false \u2237\u2262 neg\u2082))\n swap-perm\u2083 (false \u2237 p\u2081) (true \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left (\u2208-put-\u2262 _ path (false \u2237\u2262 neg\u2081))\n swap-perm\u2083 (false \u2237 p\u2081) (false \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left \n (swap-perm\u2083 p\u2081 p\u2082 path (false \u2237\u2262 neg\u2081) (false \u2237\u2262 neg\u2082))\n swap-perm\u2083 (true \u2237 p\u2081) (true \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right\n (swap-perm\u2083 p\u2081 p\u2082 path (true \u2237\u2262 neg\u2081) (true \u2237\u2262 neg\u2082))\n swap-perm\u2083 (true \u2237 p\u2081) (false \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right (\u2208-put-\u2262 _ path (true \u2237\u2262 neg\u2081))\n swap-perm\u2083 (false \u2237 p\u2081) (true \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right (\u2208-put-\u2262 _ path (true \u2237\u2262 neg\u2082))\n swap-perm\u2083 (false \u2237 p\u2081) (false \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right path\n\nmodule FoldProp {a} {A : Set a} (_\u00b7_ : Op\u2082 A) (op-comm : Commutative _\u2261_ _\u00b7_) (op-assoc : Associative _\u2261_ _\u00b7_) where\n\n \u27ea_\u27eb : \u2200 {n} \u2192 Tree A n \u2192 A\n \u27ea_\u27eb = fold _\u00b7_\n\n _=[fold]\u21d2\u2032_ : \u2200 {\u2113\u2081 \u2113\u2082} \u2192 (\u2200 {m n} \u2192 REL (Tree A m) (Tree A n) \u2113\u2081) \u2192 Rel A \u2113\u2082 \u2192 Set _\n -- _\u223c\u2080_ =[fold]\u21d2 _\u223c\u2081_ = \u2200 {m n} \u2192 _\u223c\u2080_ {m} {n} =[ fold {n} _\u00b7_ ]\u21d2 _\u223c\u2081_\n _\u223c\u2080_ =[fold]\u21d2\u2032 _\u223c\u2081_ = \u2200 {m n} {t : Tree A m} {u : Tree A n} \u2192 t \u223c\u2080 u \u2192 \u27ea t \u27eb \u223c\u2081 \u27ea u \u27eb\n\n _=[fold]\u21d2_ : \u2200 {\u2113\u2081 \u2113\u2082} \u2192 (\u2200 {n} \u2192 Rel (Tree A n) \u2113\u2081) \u2192 Rel A \u2113\u2082 \u2192 Set _\n _\u223c\u2080_ =[fold]\u21d2 _\u223c\u2081_ = \u2200 {n} \u2192 _\u223c\u2080_ =[ fold {n} _\u00b7_ ]\u21d2 _\u223c\u2081_\n\n fold-rot : Rot =[fold]\u21d2 _\u2261_\n fold-rot (leaf x) = refl\n fold-rot (fork rot rot\u2081) = cong\u2082 _\u00b7_ (fold-rot rot) (fold-rot rot\u2081)\n fold-rot (krof rot rot\u2081) rewrite fold-rot rot | fold-rot rot\u2081 = op-comm _ _\n\n -- t \u223c u \u2192 fork v t \u223c fork u w\n\n lem : \u2200 x y z t \u2192 (x \u00b7 y) \u00b7 (z \u00b7 t) \u2261 (t \u00b7 y) \u00b7 (z \u00b7 x)\n lem x y z t = (x \u00b7 y) \u00b7 (z \u00b7 t)\n \u2261\u27e8 op-assoc x y _ \u27e9\n x \u00b7 (y \u00b7 (z \u00b7 t))\n \u2261\u27e8 op-comm x _ \u27e9\n (y \u00b7 (z \u00b7 t)) \u00b7 x\n \u2261\u27e8 op-assoc y (z \u00b7 t) _ \u27e9\n y \u00b7 ((z \u00b7 t) \u00b7 x)\n \u2261\u27e8 cong (\u03bb u \u2192 y \u00b7 (u \u00b7 x)) (op-comm z t) \u27e9\n y \u00b7 ((t \u00b7 z) \u00b7 x)\n \u2261\u27e8 cong (_\u00b7_ y) (op-assoc t z x) \u27e9\n y \u00b7 (t \u00b7 (z \u00b7 x))\n \u2261\u27e8 sym (op-assoc y t _) \u27e9\n (y \u00b7 t) \u00b7 (z \u00b7 x)\n \u2261\u27e8 cong (\u03bb u \u2192 u \u00b7 (z \u00b7 x)) (op-comm y t) \u27e9\n (t \u00b7 y) \u00b7 (z \u00b7 x)\n \u220e\n where open \u2261-Reasoning\n\n fold-swp : Swp =[fold]\u21d2 _\u2261_\n fold-swp (left pf) rewrite fold-swp pf = refl\n fold-swp (right pf) rewrite fold-swp pf = refl\n fold-swp swp\u2081 = op-comm _ _\n fold-swp (swp\u2082 {_} {t\u2080\u2080} {t\u2080\u2081} {t\u2081\u2080} {t\u2081\u2081}) = lem \u27ea t\u2080\u2080 \u27eb \u27ea t\u2080\u2081 \u27eb \u27ea t\u2081\u2080 \u27eb \u27ea t\u2081\u2081 \u27eb\n\n fold-swp\u2605 : Swp\u2605 =[fold]\u21d2 _\u2261_\n fold-swp\u2605 \u03b5 = refl\n fold-swp\u2605 (x \u25c5 xs) rewrite fold-swp x | fold-swp\u2605 xs = refl\n","old_contents":"module prefect-bintree where\n\nopen import Function\nopen import Data.Nat.NP using (\u2115; zero; suc; 2^_; _+_)\nopen import Data.Bool\nopen import Data.Bits\nopen import Data.Vec using (Vec; _++_)\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Algebra.FunctionProperties\n\ndata Tree {a} (A : Set a) : \u2115 \u2192 Set a where\n leaf : (x : A) \u2192 Tree A zero\n fork : \u2200 {n} (left right : Tree A n) \u2192 Tree A (suc n)\n\nfromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Tree A n\nfromFun {zero} f = leaf (f [])\nfromFun {suc n} f = fork (fromFun (f \u2218 0\u2237_)) (fromFun (f \u2218 1\u2237_))\n\ntoFun : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Bits n \u2192 A\ntoFun (leaf x) _ = x\ntoFun (fork left right) (b \u2237 bs) = toFun (if b then right else left) bs\n\nlookup : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Tree A n \u2192 A\nlookup = flip toFun\n\nfold : \u2200 {n a} {A : Set a} (op : A \u2192 A \u2192 A) \u2192 Tree A n \u2192 A\nfold _ (leaf x) = x\nfold _\u00b7_ (fork t\u2080 t\u2081) = fold _\u00b7_ t\u2080 \u00b7 fold _\u00b7_ t\u2081\n\nsearch\u2261fold\u2218fromFun : \u2200 {n a} {A : Set a} op (f : Bits n \u2192 A) \u2192 search op f \u2261 fold op (fromFun f)\nsearch\u2261fold\u2218fromFun {zero} op f = refl\nsearch\u2261fold\u2218fromFun {suc n} op f\n rewrite search\u2261fold\u2218fromFun op (f \u2218 0\u2237_)\n | search\u2261fold\u2218fromFun op (f \u2218 1\u2237_) = refl\n\n-- Returns the flat vector of leaves underlying the perfect binary tree.\ntoVec : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Vec A (2^ n)\ntoVec (leaf x) = x \u2237 []\ntoVec (fork t\u2080 t\u2081) = toVec t\u2080 ++ toVec t\u2081\n\nlookup' : \u2200 {m n a} {A : Set a} \u2192 Bits m \u2192 Tree A (m + n) \u2192 Tree A n\nlookup' [] t = t\nlookup' (b \u2237 bs) (fork t t\u2081) = lookup' bs (if b then t\u2081 else t)\n\n\nupdate' : \u2200 {m n a} {A : Set a} \u2192 Bits m \u2192 Tree A n \u2192 Tree A (m + n) \u2192 Tree A (m + n)\nupdate' [] val tree = val\nupdate' (b \u2237 key) val (fork tree tree\u2081) = if b then fork tree (update' key val tree\u2081) \n else fork (update' key val tree) tree\u2081\n\nmap : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 Tree A n \u2192 Tree B n\nmap f (leaf x) = leaf (f x)\nmap f (fork t\u2080 t\u2081) = fork (map f t\u2080) (map f t\u2081)\n\nopen import Relation.Binary\nopen import Data.Star using (Star; \u03b5; _\u25c5_)\n\ndata Swp {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n left : \u2200 {n} {left\u2080 left\u2081 right : Tree A n} \u2192\n Swp left\u2080 left\u2081 \u2192\n Swp (fork left\u2080 right) (fork left\u2081 right)\n right : \u2200 {n} {left right\u2080 right\u2081 : Tree A n} \u2192\n Swp right\u2080 right\u2081 \u2192\n Swp (fork left right\u2080) (fork left right\u2081)\n swp\u2081 : \u2200 {n} {left right : Tree A n} \u2192\n Swp (fork left right) (fork right left)\n swp\u2082 : \u2200 {n} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Swp (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2081\u2081 t\u2080\u2081) (fork t\u2081\u2080 t\u2080\u2080))\n\nSwp\u2605 : \u2200 {n a} {A : Set a} (left right : Tree A n) \u2192 Set a\nSwp\u2605 = Star Swp\n\nSwp-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Swp {A = A} {n})\nSwp-sym (left s) = left (Swp-sym s)\nSwp-sym (right s) = right (Swp-sym s)\nSwp-sym swp\u2081 = swp\u2081\nSwp-sym swp\u2082 = swp\u2082\n\ndata Rot {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n leaf : \u2200 x \u2192 Rot (leaf x) (leaf x)\n fork : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 left\u2081 \u2192\n Rot right\u2080 right\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n krof : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 right\u2081 \u2192\n Rot right\u2080 left\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n\nRot-refl : \u2200 {n a} {A : Set a} \u2192 Reflexive (Rot {A = A} {n})\nRot-refl {x = leaf x} = leaf x\nRot-refl {x = fork _ _} = fork Rot-refl Rot-refl\n\nRot-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Rot {A = A} {n})\nRot-sym (leaf x) = leaf x\nRot-sym (fork p\u2080 p\u2081) = fork (Rot-sym p\u2080) (Rot-sym p\u2081)\nRot-sym (krof p\u2080 p\u2081) = krof (Rot-sym p\u2081) (Rot-sym p\u2080)\n\nRot-trans : \u2200 {n a} {A : Set a} \u2192 Transitive (Rot {A = A} {n})\nRot-trans (leaf x) q = q\nRot-trans (fork p\u2080 p\u2081) (fork q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (fork p\u2080 p\u2081) (krof q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (krof p\u2080 p\u2081) (fork q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\nRot-trans (krof p\u2080 p\u2081) (krof q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\n\ndata AC {a} {A : Set a} : \u2200 {m n} (left : Tree A m) (right : Tree A n) \u2192 Set a where\n \u03b5 : \u2200 {n} {t : Tree A n} \u2192 AC t t\n\n _\u204f_ : \u2200 {m n o} {t : Tree A m} {u : Tree A n} {v : Tree A o} \u2192 AC t u \u2192 AC u v \u2192 AC t v\n\n first : \u2200 {n} {left\u2080 left\u2081 right : Tree A n} \u2192\n AC left\u2080 left\u2081 \u2192\n AC (fork left\u2080 right) (fork left\u2081 right)\n\n swp : \u2200 {n} {left right : Tree A n} \u2192\n AC (fork left right) (fork right left)\n\n swp\u2082 : \u2200 {n} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n AC (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2081\u2081 t\u2080\u2081) (fork t\u2081\u2080 t\u2080\u2080))\n {-\n zip : \u2200 {m n} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A m} {u\u2080 u\u2081 : Tree A n} \u2192\n AC (fork t\u2080\u2080 t\u2081\u2081) u\u2080 \u2192\n AC (fork t\u2080\u2081 t\u2081\u2080) u\u2081 \u2192\n AC (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork u\u2080 u\u2081)\n -}\n\ninfixr 1 _\u204f_\n\nsecond-AC : \u2200 {a} {A : Set a} {n} {left right\u2080 right\u2081 : Tree A n} \u2192\n AC right\u2080 right\u2081 \u2192\n AC (fork left right\u2080) (fork left right\u2081)\nsecond-AC f = swp \u204f first f \u204f swp\n\n<_\u00d7_>-AC : \u2200 {a} {A : Set a} {n} {left\u2080 right\u2080 left\u2081 right\u2081 : Tree A n} \u2192\n AC left\u2080 left\u2081 \u2192\n AC right\u2080 right\u2081 \u2192\n AC (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n< f \u00d7 g >-AC = first f \u204f second-AC g\n\nswp\u2083-AC : \u2200 {a n} {A : Set a} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n AC (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2080\u2080 t\u2081\u2080) (fork t\u2080\u2081 t\u2081\u2081))\nswp\u2083-AC = < swp \u00d7 swp >-AC \u204f swp\u2082 \u204f < swp \u00d7 swp >-AC\n\nSwp\u21d2AC : \u2200 {n a} {A : Set a} \u2192 Swp {a} {A} {n} \u21d2 AC {n = n}\nSwp\u21d2AC (left pf) = first (Swp\u21d2AC pf)\nSwp\u21d2AC (right pf) = second-AC (Swp\u21d2AC pf)\nSwp\u21d2AC swp\u2081 = swp\nSwp\u21d2AC (swp\u2082 {t\u2080\u2080 = A} {B} {C} {D}) = swp\u2082 -- zip {!!} {!!} {-first swp \u204f zip swp \u03b5 \u204f {!!}-}\n\nSwp\u2605\u21d2AC : \u2200 {n a} {A : Set a} \u2192 Swp\u2605 {n} {a} {A} \u21d2 AC {n = n}\nSwp\u2605\u21d2AC \u03b5 = \u03b5\nSwp\u2605\u21d2AC (x \u25c5 xs) = Swp\u21d2AC x \u204f Swp\u2605\u21d2AC xs\n\nopen import Relation.Nullary using (Dec ; yes ; no)\nopen import Relation.Nullary.Negation\n\nmodule new-approach where\n\n open import Data.Empty\n\n import Function.Inverse as FI\n open FI using (_\u2194_; module Inverse; _InverseOf_)\n open import Function.Related\n import Function.Equality\n import Relation.Binary.PropositionalEquality as P\n\n data _\u2208_ {a}{A : Set a}(x : A) : {n : \u2115} \u2192 Tree A n \u2192 Set a where\n here : x \u2208 leaf x\n left : {n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 t\u2081 \u2192 x \u2208 fork t\u2081 t\u2082\n right : {n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 t\u2082 \u2192 x \u2208 fork t\u2081 t\u2082\n\n toBits : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t : Tree A n} \u2192 x \u2208 t \u2192 Bits n\n toBits here = []\n toBits (left key) = 0b \u2237 toBits key\n toBits (right key) = 1b \u2237 toBits key\n\n \u2208-lookup : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t : Tree A n}(path : x \u2208 t) \u2192 lookup (toBits path) t \u2261 x\n \u2208-lookup here = refl\n \u2208-lookup (left path) = \u2208-lookup path\n \u2208-lookup (right path) = \u2208-lookup path\n\n lookup-\u2208 : \u2200 {a}{A : Set a}{n : \u2115}(key : Bits n)(t : Tree A n) \u2192 lookup key t \u2208 t\n lookup-\u2208 [] (leaf x) = here\n lookup-\u2208 (true \u2237 key) (fork tree tree\u2081) = right (lookup-\u2208 key tree\u2081)\n lookup-\u2208 (false \u2237 key) (fork tree tree\u2081) = left (lookup-\u2208 key tree)\n\n _\u2248_ : \u2200 {a}{A : Set a}{n : \u2115} \u2192 Tree A n \u2192 Tree A n \u2192 Set _\n t\u2081 \u2248 t\u2082 = \u2200 x \u2192 (x \u2208 t\u2081) \u2194 (x \u2208 t\u2082) \n\n \u2248-refl : {a : _}{A : Set a}{n : \u2115}{t : Tree A n} \u2192 t \u2248 t\n \u2248-refl _ = FI.id\n\n move : \u2200 {a}{A : Set a}{n : \u2115}{t s : Tree A n}{x : A} \u2192 t \u2248 s \u2192 x \u2208 t \u2192 x \u2208 s\n move t\u2248s x\u2208t = Inverse.to (t\u2248s _) Function.Equality.\u27e8$\u27e9 x\u2208t\n\n swap\u2080 : \u2200 {a}{A : Set a}{n : \u2115}(t\u2081 t\u2082 : Tree A n) \u2192 fork t\u2081 t\u2082 \u2248 fork t\u2082 t\u2081\n swap\u2080 t\u2081 t\u2082 = \u03bb x \u2192 record \n { to = \u2192-to-\u27f6 swap\n ; from = \u2192-to-\u27f6 swap\n ; inverse-of = record { left-inverse-of = swap-inv \n ; right-inverse-of = swap-inv } \n } where\n swap : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 fork t\u2081 t\u2082 \u2192 x \u2208 fork t\u2082 t\u2081\n swap (left path) = right path\n swap (right path) = left path\n\n swap-inv : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t\u2081 t\u2082 : Tree A n}(p : x \u2208 fork t\u2081 t\u2082) \u2192 swap (swap p) \u2261 p\n swap-inv (left p) = refl\n swap-inv (right p) = refl\n\n _\u27e8fork\u27e9_ : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 s\u2081 s\u2082 : Tree A n} \u2192 t\u2081 \u2248 s\u2081 \u2192 t\u2082 \u2248 s\u2082 \u2192 fork t\u2081 t\u2082 \u2248 fork s\u2081 s\u2082\n (t1\u2248s1 \u27e8fork\u27e9 t2\u2248s2) y = record \n { to = to\n ; from = from\n ; inverse-of = record { left-inverse-of = frk-linv\n ; right-inverse-of = frk-rinv } \n } where\n \n frk : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 s\u2081 s\u2082 : Tree A n}{x : A} \u2192 t\u2081 \u2248 s\u2081 \u2192 t\u2082 \u2248 s\u2082 \u2192 x \u2208 fork t\u2081 t\u2082 \u2192 x \u2208 fork s\u2081 s\u2082\n frk t1\u2248s1 t2\u2248s2 (left x\u2208t1) = left (move t1\u2248s1 x\u2208t1)\n frk t1\u2248s1 t2\u2248s2 (right x\u2208t2) = right (move t2\u2248s2 x\u2208t2)\n \n to = \u2192-to-\u27f6 (frk t1\u2248s1 t2\u2248s2)\n from = \u2192-to-\u27f6 (frk (\u03bb x \u2192 FI.sym (t1\u2248s1 x)) (\u03bb x \u2192 FI.sym (t2\u2248s2 x)))\n\n \n open Function.Equality using (_\u27e8$\u27e9_)\n open import Function.LeftInverse\n\n frk-linv : from LeftInverseOf to\n frk-linv (left x) = cong left (_InverseOf_.left-inverse-of (Inverse.inverse-of (t1\u2248s1 y)) x)\n frk-linv (right x) = cong right (_InverseOf_.left-inverse-of (Inverse.inverse-of (t2\u2248s2 y)) x)\n\n frk-rinv : from RightInverseOf to -- \u2200 x \u2192 to \u27e8$\u27e9 (from \u27e8$\u27e9 x) \u2261 x\n frk-rinv (left x) = cong left (_InverseOf_.right-inverse-of (Inverse.inverse-of (t1\u2248s1 y)) x)\n frk-rinv (right x) = cong right (_InverseOf_.right-inverse-of (Inverse.inverse-of (t2\u2248s2 y)) x)\n\n Rot\u27f6\u2248 : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 Rot t\u2081 t\u2082 \u2192 t\u2081 \u2248 t\u2082\n Rot\u27f6\u2248 (leaf x) y = FI.id\n Rot\u27f6\u2248 (fork rot rot\u2081) y = (Rot\u27f6\u2248 rot \u27e8fork\u27e9 Rot\u27f6\u2248 rot\u2081) y\n Rot\u27f6\u2248 (krof {_} {l} {l'} {r} {r'} rot rot\u2081) y = \n y \u2208 fork l r \u2194\u27e8 (Rot\u27f6\u2248 rot \u27e8fork\u27e9 Rot\u27f6\u2248 rot\u2081) y \u27e9\n y \u2208 fork r' l' \u2194\u27e8 swap\u2080 r' l' y \u27e9 \n y \u2208 fork l' r' \u220e\n where open EquationalReasoning\n\n put : {a : _}{A : Set a}{n : \u2115} \u2192 Bits n \u2192 A \u2192 Tree A n \u2192 Tree A n\n put [] val tree = leaf val\n put (x \u2237 key) val (fork tree tree\u2081) = if x then fork tree (put key val tree\u2081) \n else fork (put key val tree) tree\u2081\n\n -- move-me\n _\u2237\u2262_ : {n : \u2115}{xs ys : Bits n}(x : Bit) \u2192 x \u2237 xs \u2262 x \u2237 ys \u2192 xs \u2262 ys\n _\u2237\u2262_ x = contraposition $ cong $ _\u2237_ x\n\n \u2208-put : {a : _}{A : Set a}{n : \u2115}(p : Bits n){x : A}(t : Tree A n) \u2192 x \u2208 put p x t\n \u2208-put [] t = here\n \u2208-put (true \u2237 p) (fork t t\u2081) = right (\u2208-put p t\u2081)\n \u2208-put (false \u2237 p) (fork t t\u2081) = left (\u2208-put p t)\n\n \u2208-put-\u2262 : {a : _}{A : Set a}{n : \u2115}(p : Bits n){x y : A}{t : Tree A n}(path : x \u2208 t)\n \u2192 p \u2262 toBits path \u2192 x \u2208 put p y t\n \u2208-put-\u2262 [] here neg = \u22a5-elim (neg refl)\n \u2208-put-\u2262 (true \u2237 p) (left path) neg = left path\n \u2208-put-\u2262 (false \u2237 p) (left path) neg = left (\u2208-put-\u2262 p path (false \u2237\u2262 neg))\n \u2208-put-\u2262 (true \u2237 p) (right path) neg = right (\u2208-put-\u2262 p path (true \u2237\u2262 neg))\n \u2208-put-\u2262 (false \u2237 p) (right path) neg = right path\n\n swap : {a : _}{A : Set a}{n : \u2115} \u2192 (p\u2081 p\u2082 : Bits n) \u2192 Tree A n \u2192 Tree A n\n swap p\u2081 p\u2082 t = put p\u2081 a\u2082 (put p\u2082 a\u2081 t)\n where\n a\u2081 = lookup p\u2081 t\n a\u2082 = lookup p\u2082 t\n\n swap-perm\u2081 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p : x \u2208 t) \u2192 t \u2248 swap (toBits p) (toBits p) t\n swap-perm\u2081 here = \u2248-refl \n swap-perm\u2081 (left path) = swap-perm\u2081 path \u27e8fork\u27e9 \u2248-refl\n swap-perm\u2081 (right path) = \u2248-refl \u27e8fork\u27e9 swap-perm\u2081 path\n\n swap-comm : {a : _}{A : Set a}{n : \u2115} (p\u2081 p\u2082 : Bits n)(t : Tree A n) \u2192 swap p\u2082 p\u2081 t \u2261 swap p\u2081 p\u2082 t\n swap-comm [] [] (leaf x) = refl\n swap-comm (true \u2237 p\u2081) (true \u2237 p\u2082) (fork t t\u2081) = cong (fork t) (swap-comm p\u2081 p\u2082 t\u2081)\n swap-comm (true \u2237 p\u2081) (false \u2237 p\u2082) (fork t t\u2081) = refl\n swap-comm (false \u2237 p\u2081) (true \u2237 p\u2082) (fork t t\u2081) = refl\n swap-comm (false \u2237 p\u2081) (false \u2237 p\u2082) (fork t t\u2081) = cong (flip fork t\u2081) (swap-comm p\u2081 p\u2082 t)\n\n swap-perm\u2082 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p' : Bits n)(p : x \u2208 t) \n \u2192 x \u2208 swap (toBits p) p' t\n swap-perm\u2082 _ here = here\n swap-perm\u2082 (true \u2237 p) (left path) rewrite \u2208-lookup path = right (\u2208-put p _)\n swap-perm\u2082 (false \u2237 p) (left path) = left (swap-perm\u2082 p path)\n swap-perm\u2082 (true \u2237 p) (right path) = right (swap-perm\u2082 p path)\n swap-perm\u2082 (false \u2237 p) (right path) rewrite \u2208-lookup path = left (\u2208-put p _)\n\n swap-perm\u2083 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p\u2081 p\u2082 : Bits n)(p : x \u2208 t)\n \u2192 p\u2081 \u2262 toBits p \u2192 p\u2082 \u2262 toBits p \u2192 x \u2208 swap p\u2081 p\u2082 t\n swap-perm\u2083 [] [] here neg\u2081 neg\u2082 = here\n swap-perm\u2083 (true \u2237 p\u2081) (true \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left path\n swap-perm\u2083 (true \u2237 p\u2081) (false \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left (\u2208-put-\u2262 _ path (false \u2237\u2262 neg\u2082))\n swap-perm\u2083 (false \u2237 p\u2081) (true \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left (\u2208-put-\u2262 _ path (false \u2237\u2262 neg\u2081))\n swap-perm\u2083 (false \u2237 p\u2081) (false \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left \n (swap-perm\u2083 p\u2081 p\u2082 path (false \u2237\u2262 neg\u2081) (false \u2237\u2262 neg\u2082))\n swap-perm\u2083 (true \u2237 p\u2081) (true \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right\n (swap-perm\u2083 p\u2081 p\u2082 path (true \u2237\u2262 neg\u2081) (true \u2237\u2262 neg\u2082))\n swap-perm\u2083 (true \u2237 p\u2081) (false \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right (\u2208-put-\u2262 _ path (true \u2237\u2262 neg\u2081))\n swap-perm\u2083 (false \u2237 p\u2081) (true \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right (\u2208-put-\u2262 _ path (true \u2237\u2262 neg\u2082))\n swap-perm\u2083 (false \u2237 p\u2081) (false \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right path\n\nmodule FoldProp {a} {A : Set a} (_\u00b7_ : Op\u2082 A) (op-comm : Commutative _\u2261_ _\u00b7_) (op-assoc : Associative _\u2261_ _\u00b7_) where\n\n \u27ea_\u27eb : \u2200 {n} \u2192 Tree A n \u2192 A\n \u27ea_\u27eb = fold _\u00b7_\n\n _=[fold]\u21d2\u2032_ : \u2200 {\u2113\u2081 \u2113\u2082} \u2192 (\u2200 {m n} \u2192 REL (Tree A m) (Tree A n) \u2113\u2081) \u2192 Rel A \u2113\u2082 \u2192 Set _\n -- _\u223c\u2080_ =[fold]\u21d2 _\u223c\u2081_ = \u2200 {m n} \u2192 _\u223c\u2080_ {m} {n} =[ fold {n} _\u00b7_ ]\u21d2 _\u223c\u2081_\n _\u223c\u2080_ =[fold]\u21d2\u2032 _\u223c\u2081_ = \u2200 {m n} {t : Tree A m} {u : Tree A n} \u2192 t \u223c\u2080 u \u2192 \u27ea t \u27eb \u223c\u2081 \u27ea u \u27eb\n\n _=[fold]\u21d2_ : \u2200 {\u2113\u2081 \u2113\u2082} \u2192 (\u2200 {n} \u2192 Rel (Tree A n) \u2113\u2081) \u2192 Rel A \u2113\u2082 \u2192 Set _\n _\u223c\u2080_ =[fold]\u21d2 _\u223c\u2081_ = \u2200 {n} \u2192 _\u223c\u2080_ =[ fold {n} _\u00b7_ ]\u21d2 _\u223c\u2081_\n\n fold-rot : Rot =[fold]\u21d2 _\u2261_\n fold-rot (leaf x) = refl\n fold-rot (fork rot rot\u2081) = cong\u2082 _\u00b7_ (fold-rot rot) (fold-rot rot\u2081)\n fold-rot (krof rot rot\u2081) rewrite fold-rot rot | fold-rot rot\u2081 = op-comm _ _\n\n -- t \u223c u \u2192 fork v t \u223c fork u w\n\n lem : \u2200 x y z t \u2192 (x \u00b7 y) \u00b7 (z \u00b7 t) \u2261 (t \u00b7 y) \u00b7 (z \u00b7 x)\n lem x y z t = (x \u00b7 y) \u00b7 (z \u00b7 t)\n \u2261\u27e8 op-assoc x y _ \u27e9\n x \u00b7 (y \u00b7 (z \u00b7 t))\n \u2261\u27e8 op-comm x _ \u27e9\n (y \u00b7 (z \u00b7 t)) \u00b7 x\n \u2261\u27e8 op-assoc y (z \u00b7 t) _ \u27e9\n y \u00b7 ((z \u00b7 t) \u00b7 x)\n \u2261\u27e8 cong (\u03bb u \u2192 y \u00b7 (u \u00b7 x)) (op-comm z t) \u27e9\n y \u00b7 ((t \u00b7 z) \u00b7 x)\n \u2261\u27e8 cong (_\u00b7_ y) (op-assoc t z x) \u27e9\n y \u00b7 (t \u00b7 (z \u00b7 x))\n \u2261\u27e8 sym (op-assoc y t _) \u27e9\n (y \u00b7 t) \u00b7 (z \u00b7 x)\n \u2261\u27e8 cong (\u03bb u \u2192 u \u00b7 (z \u00b7 x)) (op-comm y t) \u27e9\n (t \u00b7 y) \u00b7 (z \u00b7 x)\n \u220e\n where open \u2261-Reasoning\n\n fold-swp : Swp =[fold]\u21d2 _\u2261_\n fold-swp (left pf) rewrite fold-swp pf = refl\n fold-swp (right pf) rewrite fold-swp pf = refl\n fold-swp swp\u2081 = op-comm _ _\n fold-swp (swp\u2082 {_} {t\u2080\u2080} {t\u2080\u2081} {t\u2081\u2080} {t\u2081\u2081}) = lem \u27ea t\u2080\u2080 \u27eb \u27ea t\u2080\u2081 \u27eb \u27ea t\u2081\u2080 \u27eb \u27ea t\u2081\u2081 \u27eb\n\n fold-swp\u2605 : Swp\u2605 =[fold]\u21d2 _\u2261_\n fold-swp\u2605 \u03b5 = refl\n fold-swp\u2605 (x \u25c5 xs) rewrite fold-swp x | fold-swp\u2605 xs = refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"0655a6d0e4899c4ba47d263fc94275319d74c339","subject":"IDesc example: rework Hutton shaver with a more traditional context","message":"IDesc example: rework Hutton shaver with a more traditional context","repos":"kwangkim\/pigment,kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram","old_file":"models\/IDescTT.agda","new_file":"models\/IDescTT.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n\n\n--********************************\n-- Typed expressions\n--********************************\n\n-- Fix menu:\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty), -- Val t\n (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void)))\n\n-- Indexed menu:\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\n\n-- Expression:\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), \n Void))\n\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void)\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void )\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (exprFree ** Val)\n\n--********************************\n-- Closed term' evaluation\n--********************************\n\neval' : {ty : Type} -> IMu closeTerm' ty -> Val ty\neval' {ty} term = cata Type closeTerm' Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm' ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n\n--********************************\n-- Open terms\n--********************************\n\ndata Vec (A : Set) : Nat -> Set where\n vnil : Vec A ze\n vcons : {n : Nat} -> A -> Vec A n -> Vec A (su n)\n\ndata Fin : Nat -> Set where\n fze : {n : Nat} -> Fin (su n)\n fsu : {n : Nat} -> Fin n -> Fin (su n)\n\nContext : Nat -> Set\nContext n = Vec (Sigma Type (\\ty -> IMu closeTerm' ty)) n\n\ntypeAt : {n : Nat}(c : Context n) -> Fin n -> Type\ntypeAt {ze} c ()\ntypeAt {.(su n)} (vcons x xs) (fze {n}) = fst x\ntypeAt {.(su n)} (vcons x xs) (fsu {n} y) = typeAt xs y\n\nlookup : {n : Nat}(c : Context n)(i : Fin n) -> IMu closeTerm' (typeAt c i)\nlookup {ze} c ()\nlookup {su _} (vcons x _) fze = snd x\nlookup {su _} (vcons _ xs) (fsu y) = lookup xs y\n\n\nVar : {n : Nat} -> Context n -> Type -> Set\nVar {n} c ty = Sigma (Fin n) (\\i -> typeAt c i == ty)\n\nopenTerm : {n : Nat} -> Context n -> Type -> IDesc Type\nopenTerm c = toIDesc Type (exprFree ** (\\ty -> Val ty + Var c ty))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : {n : Nat}\n {context : Context n}\n (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm' ty\ndischarge ty (l value) = con (EZe , value)\ndischarge {n} {c} ty (r variable) = subst (cong (IMu closeTerm') (snd variable)) (lookup c (fst variable))\n\nsubstExpr : {n : Nat}\n {ty : Type}\n (context : Context n)\n (sigma : (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm' ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm' ty\nsubstExpr {n} {ty} c sig term = \n substI (\\ty -> Val ty + Var c ty) Val exprFree sig ty term\n\nevalOpen : {n : Nat}{ty : Type}\n (context : Context n) ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen context tm = eval' (substExpr context discharge tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- V 0 :-> true, V 1 :-> 2\ntestContext : Context _\ntestContext = vcons (bool , con (EZe , true )) (vcons (nat , con (EZe , su (su ze)) ) vnil)\n\n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , r ( fsu fze , refl ) )\n\ntestSubst1 : IMu closeTerm' nat\ntestSubst1 = substExpr testContext \n discharge \n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con (ESu (ESu EZe) , (con (EZe , l (su ze)) , con ( EZe , r (fsu fze , refl) )) )\n\ntestSubst2 : IMu closeTerm' nat\ntestSubst2 = substExpr testContext \n discharge \n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext test2\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu EZe , (con (EZe , r (fze , refl)) ,\n (con (EZe , r (fsu fze , refl)) ,\n con (EZe , l ze))))\n\ntestSubst3 : IMu closeTerm' nat\ntestSubst3 = substExpr testContext \n discharge \n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext test3","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n\n\n--********************************\n-- Typed expressions\n--********************************\n\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty),\n (prod (var bool) (prod (var ty) (var ty)), \n Void)))\n\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n\n--********************************\n-- Open terms\n--********************************\n\nVar : EnumU -> Type -> Set\nVar dom _ = EnumT dom\n\nopenTerm : EnumU -> Type -> IDesc Type\nopenTerm dom = exprIDesc (\\ty -> Val ty + Var dom ty) expr\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm ty\ndischarge ty vars context (l value) = con ( EZe , value )\ndischarge ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm ty) context variable \n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), \n Void))\n\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void)\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void )\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (exprFree ** Val)\n\nopenTerm' : EnumU -> Type -> IDesc Type\nopenTerm' dom = toIDesc Type (exprFree ** (\\ty -> Val ty + Var dom ty))\n\n--********************************\n-- Evaluation of open terms'\n--********************************\n\ndischarge' : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm' ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm' ty\ndischarge' ty vars context (l value) = con (EZe , value) \ndischarge' ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm' ty) context variable\n\nchooseDom : (ty : Type)(domNat domBool : EnumU) -> EnumU\nchooseDom nat domNat _ = domNat\nchooseDom bool _ domBool = domBool\n\nchooseGamma : (ty : Type)\n (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool)) ->\n spi dom (\\_ -> IMu closeTerm' ty)\nchooseGamma nat dom gammaNat gammaBool = gammaNat\nchooseGamma bool dom gammaNat gammaBool = gammaBool\n\nsubstExpr : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (sigma : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var dom ty) ->\n IMu closeTerm' ty)\n (ty : Type) ->\n IMu (openTerm' dom) ty ->\n IMu closeTerm' ty\nsubstExpr dom gammaNat gammaBool sig ty term = \n substI (\\ty -> Val ty + Var dom ty)\n Val\n exprFree\n (sig dom gammaNat gammaBool)\n ty\n term\n\n\nsigmaExpr : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var dom ty) ->\n IMu closeTerm' ty\nsigmaExpr dom gammaNat gammaBool ty v = \n discharge' ty\n dom\n (chooseGamma ty dom gammaNat gammaBool)\n v\n\ntest : IMu (openTerm' (consE (consE nilE))) nat ->\n IMu closeTerm' nat\ntest term = substExpr (consE (consE nilE)) \n ( con ( EZe , ze ) , ( con (EZe , su ze ) , Void )) \n ( con ( EZe , true ) , ( con ( EZe , false ) , Void ))\n sigmaExpr nat term\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"09a448f0651733ec90348ae060a019caff44fa89","subject":"agda\/derivation.agda: progress on \u0394-term","message":"agda\/derivation.agda: progress on \u0394-term\n\nImplement \u0394-term on applications, assuming the availability of a nabla\nmetafunction (which we expect to be implemented using a term\nconstructor, but who knows).\n\nOld-commit-hash: 803596156f6f88a497e4f039116e047d66aedf3c\n","repos":"inc-lc\/ilc-agda","old_file":"derivation.agda","new_file":"derivation.agda","new_contents":"module derivation where\n\nopen import lambda\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\u0394-Type bool = {!!}\n\napply : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbf. \u03bbx. apply ( df x ) ( f x )\napply {bool} = {!!}\n\ncompose : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4)\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app (compose {\u03c4\u2082}) (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbdg. \u03bbx. compose ( df x ) ( dg x )\ncompose {bool} = {!!}\n\nnil : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4)\nnil {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (nil {\u03c4\u2082})\n -- \u03bbx. nil\nnil {bool} = {!!}\n\n-- Hey, apply is \u03b1-equivalent to compose, what's going on?\n-- Oh, and `\u0394-Type` is the identity function?\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u03c4 \u2022 \u0394-Type \u03c4 \u2022 \u0394-Context \u0393\n\n-- CHANGE VARIABLES\n\n-- changes 'x' to 'dx'\n\nrename : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nrename this = that this\nrename (that x) = that (that (rename x))\n\nadaptVar : \u2200 {\u03c4} \u0393\u2081 \u0393\u2082 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) \u03c4\nadaptVar \u2205 (\u03c4\u2082 \u2022 \u0393\u2082) this = this\nadaptVar \u2205 (\u03c4\u2082 \u2022 \u0393\u2082) (that x) = that (that (adaptVar \u2205 \u0393\u2082 x))\nadaptVar (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 this = this\nadaptVar (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 (that x) = that (adaptVar \u0393\u2081 \u0393\u2082 x)\n\nadapt : \u2200 {\u03c4} \u0393\u2081 \u0393\u2082 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) \u03c4\nadapt {\u03c4\u2081 \u21d2 \u03c4\u2082} \u0393\u2081 \u0393\u2082 (abs t) = abs (adapt (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 t)\nadapt \u0393\u2081 \u0393\u2082 (app t\u2081 t\u2082) = app (adapt \u0393\u2081 \u0393\u2082 t\u2081) (adapt \u0393\u2081 \u0393\u2082 t\u2082)\nadapt \u0393\u2081 \u0393\u2082 (var t) = var (adaptVar \u0393\u2081 \u0393\u2082 t)\nadapt \u0393\u2081 \u0393\u2082 true = true\nadapt \u0393\u2081 \u0393\u2082 false = false\nadapt \u0393\u2081 \u0393\u2082 (cond tc t\u2081 t\u2082) = cond (adapt \u0393\u2081 \u0393\u2082 tc) (adapt \u0393\u2081 \u0393\u2082 t\u2081) (adapt \u0393\u2081 \u0393\u2082 t\u2082)\n\n-- changes 'x' to 'nil' (if internally bound) or 'dx' (if externally bound)\n\n\u0394-var : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-var {\u2205} x = var (rename x)\n\u0394-var {\u03c4 \u2022 \u0393} this = nil {\u03c4}\n\u0394-var {\u03c4 \u2022 \u0393} (that x) = \u0394-var {\u0393} x\n\n-- Note: this should be the derivative with respect to the first variable.\nnabla : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2080 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082)\nnabla = {!!}\n\n-- CHANGE TERMS\n\u0394-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-term {\u0393} (abs {\u03c4\u2081 = \u03c4} t) = abs (\u0394-term {\u03c4 \u2022 \u0393} t)\n\n\u0394-term {\u0393\u2081} {\u0393\u2082} {\u03c4} (app t\u2081 t\u2082) = app (app (compose {\u03c4}) (app (\u0394-term {\u0393\u2081} t\u2081) (adapt \u0393\u2081 \u0393\u2082 t\u2082)))\n (app (adapt \u0393\u2081 \u0393\u2082 (app (nabla {\u0393\u2081 \u22ce \u0393\u2082} t\u2081) t\u2082)) (\u0394-term {\u0393\u2081} {\u0393\u2082} t\u2082))\n\n\u0394-term {\u0393} (var x) = weaken {\u2205} {\u0393} (\u0394-var {\u0393} x)\n\u0394-term {\u0393} true = {!!}\n\u0394-term {\u0393} false = {!!}\n\u0394-term {\u0393} (cond t\u2081 t\u2082 t\u2083) = {!!}\n","old_contents":"module derivation where\n\nopen import lambda\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\u0394-Type bool = {!!}\n\napply : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbf. \u03bbx. apply ( df x ) ( f x )\napply {bool} = {!!}\n\ncompose : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4)\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app (compose {\u03c4\u2082}) (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbdg. \u03bbx. compose ( df x ) ( dg x )\ncompose {bool} = {!!}\n\nnil : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4)\nnil {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (nil {\u03c4\u2082})\n -- \u03bbx. nil\nnil {bool} = {!!}\n\n-- Hey, apply is \u03b1-equivalent to compose, what's going on?\n-- Oh, and `\u0394-Type` is the identity function?\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u03c4 \u2022 \u0394-Type \u03c4 \u2022 \u0394-Context \u0393\n\n-- CHANGE VARIABLES\n\n-- changes 'x' to 'dx'\n\nrename : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nrename this = that this\nrename (that x) = that (that (rename x))\n\n-- changes 'x' to 'nil' (if internally bound) or 'dx' (if externally bound)\n\n\u0394-var : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-var {\u2205} x = var (rename x)\n\u0394-var {\u03c4 \u2022 \u0393} this = nil {\u03c4}\n\u0394-var {\u03c4 \u2022 \u0393} (that x) = \u0394-var {\u0393} x\n\n-- CHANGE TERMS\n\n\u0394-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-term {\u0393} (abs {\u03c4\u2081 = \u03c4} t) = abs (\u0394-term {\u03c4 \u2022 \u0393} t)\n\u0394-term {\u0393} (app t\u2081 t\u2082) = {!!}\n\u0394-term {\u0393} (var x) = weaken {\u2205} {\u0393} (\u0394-var {\u0393} x)\n\u0394-term {\u0393} true = {!!}\n\u0394-term {\u0393} false = {!!}\n\u0394-term {\u0393} (cond t\u2081 t\u2082 t\u2083) = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9843e14b03993bc42349a93ec9737bc06686d1b9","subject":"Added setoid for FOTC.","message":"Added setoid for FOTC.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/setoids\/FOTC.agda","new_file":"notes\/setoids\/FOTC.agda","new_contents":"------------------------------------------------------------------------------\n-- Using setoids for formalizing FOTC\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-positivity-check #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n{-\n\nFrom Peter emails:\n\nAt that time I was considering an inductive data type D and an\ninductively defined equality on D. But I think what we are doing now\nis better.\n\nFor =: The reason is that with the propositional identity we have\nidentity elimination which lets us replace equals by equals. We cannot\ndo that in general if we have setoid equality.\n\nFor D: the reason why I prefer to postulate D is that we have no use\nfor the inductive structure of D, and this inductive structure would\nmake e g 0 + 0 different from 0. So in that case we need setoid\nequality.\n\n-}\n\n-- References:\n--\n-- \u2022 Gilles Barthe, Venanzio Capretta, and Olivier Pons. Setoids in\n-- type theory. Journal of Functional Programming, 13(2):261\u2013293,\n-- 2003.\n\nmodule FOTC where\n\nmodule Aczel-CA where\n -- From Peter's slides\n -- http:\/\/www.cse.chalmers.se\/~peterd\/slides\/Amagasaki.pdf\n\n -- We add 3 to the fixities of the standard library.\n infixl 9 _\u00b7_ -- The symbol is '\\cdot'.\n infix 7 _\u2250_\n\n data D : Set where\n K S : D\n _\u00b7_ : D \u2192 D \u2192 D\n\n -- The setoid equality.\n data _\u2250_ : D \u2192 D \u2192 Set where\n refl : \u2200 {x} \u2192 x \u2250 x\n sym : \u2200 {x y} \u2192 x \u2250 y \u2192 y \u2250 x\n trans : \u2200 {x y z} \u2192 x \u2250 y \u2192 y \u2250 z \u2192 x \u2250 z\n cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2250 x\u2082 \u2192 y\u2081 \u2250 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2250 x\u2082 \u00b7 y\u2082\n K-eq : \u2200 x y \u2192 K \u00b7 x \u00b7 y \u2250 x\n S-eq : \u2200 x y z \u2192 S \u00b7 x \u00b7 y \u00b7 z \u2250 x \u00b7 z \u00b7 (y \u00b7 z)\n\n -- The identity type.\n data _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n ----------------------------------------------------------------------------\n -- 14 May 2012: Using the inductive structure we cannot prove\n --\n -- K \u00b7 x \u00b7 y \u2261 x,\n --\n -- we need the setoid equality.\n -- K-eq : \u2200 {x y} \u2192 (K \u00b7 x \u00b7 y) \u2261 x\n\n ----------------------------------------------------------------------------\n -- 14 May 2012. We cannot define the identity elimination using the\n -- setoid equality.\n --\n -- Adapted from Peter's email:\n\n -- Given\n postulate subst : (A : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2250 y \u2192 A x \u2192 A y\n\n -- we can proof\n\n \u2250\u2192\u2261 : \u2200 {x y} \u2192 x \u2250 y \u2192 x \u2261 y\n \u2250\u2192\u2261 {x} h = subst (\u03bb z \u2192 x \u2261 z) h refl\n\n -- but this doesn't hold because \"x \u2261 y\" (propositional equality)\n -- means identical expressions. We do NOT have K \u00b7 x \u00b7 y \u2261 x.\n --\n -- The point is that \u2250 is a non-trivial equivalence relation, and\n -- not all properties preserve it. However, all properties are\n -- preserved by \u2261.\n\n------------------------------------------------------------------------------\n\nmodule FOTC where\n\n infixl 9 _\u00b7_ -- The symbol is '\\cdot'.\n infix 7 _\u2250_\n\n data D : Set where\n _\u00b7_ : D \u2192 D \u2192 D\n lam fix : (D \u2192 D) \u2192 D\n true false if zero succ pred iszero : D\n\n -- The setoid equality.\n data _\u2250_ : D \u2192 D \u2192 Set where\n refl : \u2200 {x} \u2192 x \u2250 x\n sym : \u2200 {x y} \u2192 x \u2250 y \u2192 y \u2250 x\n trans : \u2200 {x y z} \u2192 x \u2250 y \u2192 y \u2250 z \u2192 x \u2250 z\n if-true : \u2200 d\u2081 d\u2082 \u2192 if \u00b7 true \u00b7 d\u2081 \u00b7 d\u2082 \u2250 d\u2081\n if-false : \u2200 d\u2081 d\u2082 \u2192 if \u00b7 false \u00b7 d\u2081 \u00b7 d\u2082 \u2250 d\u2082\n pred-0 : pred \u00b7 zero \u2250 zero\n pred-S : \u2200 n \u2192 pred \u00b7 (succ \u00b7 n) \u2250 n\n iszero-0 : iszero \u00b7 zero \u2250 true\n iszero-S : \u2200 n \u2192 iszero \u00b7 (succ \u00b7 n) \u2250 false\n beta : \u2200 f a \u2192 lam f \u00b7 a \u2250 f a\n fix-eq : \u2200 f \u2192 fix f \u2250 f (fix f)\n\n------------------------------------------------------------------------------\n\nmodule LeibnizEquality where\n\n infix 7 _\u2261_\n\n data D : Set where\n _\u00b7_ : D \u2192 D \u2192 D\n lam fix : (D \u2192 D) \u2192 D\n true false if zero succ pred iszero : D\n\n -- (Barthe et al. 2003, p. 262) use the Leibniz equality when\n -- they talk about setoids.\n\n -- Using the Leibniz equality (adapted from\n -- Agda\/examples\/lib\/Logic\/Leibniz.agda)\n\n _\u2261_ : D \u2192 D \u2192 Set\u2081\n x \u2261 y = (A : D \u2192 Set) \u2192 A x \u2192 A y\n\n -- we can prove the setoids properties\n\n refl : \u2200 x \u2192 x \u2261 x\n refl x A Ax = Ax\n\n sym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\n sym {x} h A Ay = h (\u03bb z \u2192 A z \u2192 A x) (\u03bb Ax \u2192 Ax) Ay\n\n trans : \u2200 x y z \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n trans x y z h\u2081 h\u2082 A Ax = h\u2082 A (h\u2081 A Ax)\n\n -- and the identity elimination\n\n subst : (A : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\n subst A x\u2261y = x\u2261y A\n\n -- and the congruency\n\n cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2261 x\u2082 \u2192 y\u2081 \u2261 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2261 x\u2082 \u00b7 y\u2082\n cong {x\u2081} {x\u2082} {y\u2081} {y\u2082} h\u2081 h\u2082 A Ax\u2081x\u2082 =\n h\u2082 (\u03bb z \u2192 A (x\u2082 \u00b7 z)) (h\u2081 (\u03bb z \u2192 A (z \u00b7 y\u2081)) Ax\u2081x\u2082)\n","old_contents":"------------------------------------------------------------------------------\n-- Using setoids for formalizing the FOTC\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n{-\n\nFrom Peter emails:\n\nAt that time I was considering an inductive data type D and an\ninductively defined equality on D. But I think what we are doing now\nis better.\n\nFor =: The reason is that with the propositional identity we have\nidentity elimination which lets us replace equals by equals. We cannot\ndo that in general if we have setoid equality.\n\nFor D: the reason why I prefer to postulate D is that we have no use\nfor the inductive structure of D, and this inductive structure would\nmake e g 0 + 0 different from 0. So in that case we need setoid\nequality.\n\n-}\n\nmodule FOTC where\n\n-- References:\n--\n-- \u2022 Gilles Barthe, Venanzio Capretta, and Olivier Pons. Setoids in\n-- type theory. Journal of Functional Programming, 13(2):261\u2013293,\n-- 2003.\n\n------------------------------------------------------------------------------\n\nmodule Setoid where\n\n -- From Peter's slides\n -- http:\/\/www.cse.chalmers.se\/~peterd\/slides\/Amagasaki.pdf\n\n -- We add 3 to the fixities of the standard library.\n infixl 9 _\u00b7_ -- The symbol is '\\cdot'.\n infix 7 _\u2250_\n\n data D : Set where\n K S : D\n _\u00b7_ : D \u2192 D \u2192 D\n\n -- The setoid equality.\n data _\u2250_ : D \u2192 D \u2192 Set where\n refl : \u2200 x \u2192 x \u2250 x\n sym : \u2200 {x y} \u2192 x \u2250 y \u2192 y \u2250 x\n trans : \u2200 {x y z} \u2192 x \u2250 y \u2192 y \u2250 z \u2192 x \u2250 z\n cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2250 x\u2082 \u2192 y\u2081 \u2250 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2250 x\u2082 \u00b7 y\u2082\n Kax : \u2200 x y \u2192 K \u00b7 x \u00b7 y \u2250 x\n Sax : \u2200 x y z \u2192 S \u00b7 x \u00b7 y \u00b7 z \u2250 x \u00b7 z \u00b7 (y \u00b7 z)\n\n -- The identity type.\n data _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n ----------------------------------------------------------------------------\n -- 14 May 2012: Using the inductive structure we cannot prove\n --\n -- K \u00b7 x \u00b7 y \u2261 x,\n --\n -- we need the setoid equality.\n -- K-eq : \u2200 {x y} \u2192 (K \u00b7 x \u00b7 y) \u2261 x\n\n ----------------------------------------------------------------------------\n -- 14 May 2012. We cannot define the identity elimination using the\n -- setoid equality.\n --\n -- Adapted from Peter's email:\n\n -- Given\n postulate subst : (A : D \u2192 Set) \u2192 \u2200 x y \u2192 x \u2250 y \u2192 A x \u2192 A y\n\n -- you can get the instance\n\n subst-aux : \u2200 x y \u2192 x \u2250 y \u2192 x \u2261 x \u2192 x \u2261 y\n subst-aux x y h\u2081 h\u2082 = subst A x y h\u2081 refl\n where A : D \u2192 Set\n A z = x \u2261 z\n\n -- hence you can prove\n\n thm : \u2200 {x y} \u2192 x \u2250 y \u2192 x \u2261 y\n thm {x} {y} h = subst-aux x y h refl\n\n -- but this doesn't hold because \"x \u2261 y\" (propositional equality)\n -- means identical expressions. We do NOT have K \u00b7 x \u00b7 y \u2261 x.\n --\n -- The point is that \u2250 is a non-trivial equivalence relation, and\n -- not all properties preserve it. However, all properties are\n -- preserved by \u2261.\n\n------------------------------------------------------------------------------\n\nmodule LeibnizEquality where\n\n infix 7 _\u2261_\n\n data D : Set where\n K S : D\n _\u00b7_ : D \u2192 D \u2192 D\n\n -- (Barthe et al. 2003, p. 262) use the Leibniz equality when\n -- they talk about setoids.\n\n -- Using the Leibniz equality (adapted from\n -- Agda\/examples\/lib\/Logic\/Leibniz.agda)\n\n _\u2261_ : D \u2192 D \u2192 Set\u2081\n x \u2261 y = (A : D \u2192 Set) \u2192 A x \u2192 A y\n\n -- we can prove the setoids properties\n\n refl : \u2200 x \u2192 x \u2261 x\n refl x A Ax = Ax\n\n sym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\n sym {x} h A Ay = h (\u03bb z \u2192 A z \u2192 A x) (\u03bb Ax \u2192 Ax) Ay\n\n trans : \u2200 x y z \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n trans x y z h\u2081 h\u2082 A Ax = h\u2082 A (h\u2081 A Ax)\n\n -- and the identity elimination\n\n subst : (A : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\n subst A x\u2261y = x\u2261y A\n\n -- and the congruency\n\n cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2261 x\u2082 \u2192 y\u2081 \u2261 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2261 x\u2082 \u00b7 y\u2082\n cong {x\u2081} {x\u2082} {y\u2081} {y\u2082} h\u2081 h\u2082 A Ax\u2081x\u2082 =\n h\u2082 (\u03bb z \u2192 A (x\u2082 \u00b7 z)) (h\u2081 (\u03bb z \u2192 A (z \u00b7 y\u2081)) Ax\u2081x\u2082)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"037a6a0d1770f4eb88369b436af7b960161dd9ad","subject":"Reported issue #11.","message":"Reported issue #11.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Program\/ABP\/Terms.agda","new_file":"src\/fot\/FOTC\/Program\/ABP\/Terms.agda","new_contents":"------------------------------------------------------------------------------\n-- ABP terms\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Program.ABP.Terms where\n\nopen import FOTC.Base\nopen import FOTC.Base.Loop\nopen import FOTC.Data.Bool\n\n------------------------------------------------------------------------------\n-- N.B. We did not define @Bit = Bool@ due to the issue #11.\nBit : D \u2192 Set\nBit b = Bool b\n{-# ATP definition Bit #-}\n\nF : D\nF = false\n{-# ATP definition F #-}\n\nT : D\nT = true\n{-# ATP definition T #-}\n\npostulate\n <_,_> : D \u2192 D \u2192 D\n ok : D \u2192 D\n","old_contents":"------------------------------------------------------------------------------\n-- ABP terms\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Program.ABP.Terms where\n\nopen import FOTC.Base\nopen import FOTC.Base.Loop\nopen import FOTC.Data.Bool\n\n------------------------------------------------------------------------------\n\nBit : D \u2192 Set\nBit b = Bool b\n{-# ATP definition Bit #-}\n\nF : D\nF = false\n{-# ATP definition F #-}\n\nT : D\nT = true\n{-# ATP definition T #-}\n\npostulate\n <_,_> : D \u2192 D \u2192 D\n ok : D \u2192 D\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"6dddc3bd8f8e569a803a9ed68746ad93c6653498","subject":"Desc stratified model: fresh start","message":"Desc stratified model: fresh start\n","repos":"mietek\/epigram2,larrytheliquid\/pigit,mietek\/epigram2","old_file":"models\/IDesc.agda","new_file":"models\/IDesc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l : Level}{A B : Set l}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l : Level}{A B C : Set l}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {l : Level}{A B : Set l}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (l : Level)(I : Set l) : Set (suc l) where\n var : I -> IDesc l I\n const : Set l -> IDesc l I\n prod : IDesc l I -> IDesc l I -> IDesc l I\n sigma : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n pi : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\ndesc _ I (var i) P = P i\ndesc _ I (const X) P = X\ndesc x I (prod D D') P = desc x I D P * desc x I D' P\ndesc x I (sigma S T) P = Sigma S (\\s -> desc x I (T s) P)\ndesc x I (pi S T) P = (s : S) -> desc x I (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l where\n con : desc l I (R i) (\\j -> IMu l I R j) -> IMu l I R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : (l : Level)(I : Set l)(D : IDesc l I)(P : I -> Set l) -> desc l I D P -> IDesc l (Sigma I P)\nbox _ I (var i) P x = var (i , x)\nbox _ I (const X) P x = const X\nbox x I (prod D D') P (d , d') = prod (box x I D P d) (box x I D' P d')\nbox x I (sigma S T) P (a , b) = box x I (T a) P b\nbox x I (pi S T) P f = pi S (\\s -> box x I (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (l : Level)\n (I : Set l)\n (R : I -> IDesc l I)\n (P : Sigma I (IMu l I R) -> Set l)\n (m : (i : I)\n (xs : desc l I (R i) (IMu l I R))\n (hs : desc l (Sigma I (IMu l I R)) (box l I (R i) (IMu l I R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu l I R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc l I) -> \n (xs : desc l I D (IMu l I R)) -> \n desc l (Sigma I (IMu l I R)) (box l I D (IMu l I R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (l : Level)\n (I : Set l)\n (R : I -> IDesc l I)\n (P : Sigma I (IMu l I R) -> Set l)\n (m : (i : I)\n (xs : desc l I (R i) (IMu l I R))\n (hs : desc l (Sigma I (IMu l I R)) (box l I (R i) (IMu l I R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu l I R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst (l : Level) : Set l where\n lvar : DescDConst l\n lconst : DescDConst l\n lprod : DescDConst l\n lpi : DescDConst l\n lsigma : DescDConst l\n\ndescDChoice : (l : Level) -> Set l -> DescDConst (suc l) -> IDesc (suc l) Unit\ndescDChoice _ I lvar = const (lift I)\ndescDChoice x _ lconst = const (Set x)\ndescDChoice _ _ lprod = prod (var Void) (var Void)\ndescDChoice x _ lpi = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice x _ lsigma = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\ndescD x I = sigma (DescDConst (suc x)) (descDChoice x I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (suc x) Unit (\\_ -> descD x I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst (suc x)\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst (suc x)\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst (suc x)\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst (suc x)\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst (suc x)\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n \n\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (l : Level)(I : Set l) : Set (suc l) where\n var : I -> IDesc l I\n const : Set l -> IDesc l I\n prod : IDesc l I -> IDesc l I -> IDesc l I\n sigma : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n pi : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\ndesc _ I (var i) P = P i\ndesc _ I (const X) P = X\ndesc x I (prod D D') P = desc x I D P * desc x I D' P\ndesc x I (sigma S T) P = Sigma S (\\s -> desc x I (T s) P)\ndesc x I (pi S T) P = (s : S) -> desc x I (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l where\n con : desc l I (R i) (\\j -> IMu l I R j) -> IMu l I R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : (l : Level)(I : Set l)(D : IDesc l I)(P : I -> Set l) -> desc l I D P -> IDesc l (Sigma I P)\nbox _ I (var i) P x = var (i , x)\nbox _ I (const X) P x = const X\nbox x I (prod D D') P (d , d') = prod (box x I D P d) (box x I D' P d')\nbox x I (sigma S T) P (a , b) = box x I (T a) P b\nbox x I (pi S T) P f = pi S (\\s -> box x I (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (l : Level)\n (I : Set l)\n (R : I -> IDesc l I)\n (P : Sigma I (IMu l I R) -> Set l)\n (m : (i : I)\n (xs : desc l I (R i) (IMu l I R))\n (hs : desc l (Sigma I (IMu l I R)) (box l I (R i) (IMu l I R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu l I R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc l I) -> \n (xs : desc l I D (IMu l I R)) -> \n desc l (Sigma I (IMu l I R)) (box l I D (IMu l I R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (l : Level)\n (I : Set l)\n (R : I -> IDesc l I)\n (P : Sigma I (IMu l I R) -> Set l)\n (m : (i : I)\n (xs : desc l I (R i) (IMu l I R))\n (hs : desc l (Sigma I (IMu l I R)) (box l I (R i) (IMu l I R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu l I R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- List\n--********************************************\n\ndata ListDConst (l : Level) : Set l where\n cnil : ListDConst l\n ccons : ListDConst l\n\nlistDChoice : (l : Level) -> Set l -> ListDConst l -> IDesc l Unit\nlistDChoice x X cnil = const Unit\nlistDChoice x X ccons = sigma X (\\_ -> var Void)\n\nlistD : (l : Level) -> Set l -> IDesc l Unit\nlistD x X = sigma (ListDConst x) (listDChoice x X)\n\nlist : (l : Level) -> Set l -> Set l\nlist x X = IMu x Unit (\\_ -> listD x X) Void\n\n\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst (l : Level) : Set l where\n lvar : DescDConst l\n lconst : DescDConst l\n lprod : DescDConst l\n lpi : DescDConst l\n lsigma : DescDConst l\n\ndescDChoice : (l : Level) -> Set l -> DescDConst (suc l) -> IDesc (suc l) Unit\ndescDChoice _ I lvar = const (lift I)\ndescDChoice x _ lconst = const (Set x)\ndescDChoice _ _ lprod = prod (var Void) (var Void)\ndescDChoice x _ lpi = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice x _ lsigma = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\ndescD x I = sigma (DescDConst (suc x)) (descDChoice x I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (suc x) Unit (\\_ -> descD x I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst (suc x)\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst (suc x)\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst (suc x)\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst (suc x)\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst (suc x)\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n ","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b4c7bc65da42597091f08c82956e4c5a2dfbce3a","subject":"Updated setoid note.","message":"Updated setoid note.\n\nIgnore-this: e3efd15c1aa4c5d0d3b13ce3c8b9d2c0\n\ndarcs-hash:20120514124154-3bd4e-4a0539c4fe3ebec97983e47f466c258e4221a3a0.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/setoids\/FOTC.agda","new_file":"notes\/setoids\/FOTC.agda","new_contents":"------------------------------------------------------------------------------\n-- Using setoids for formalizing the FOTC\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Tested with the development version of Agda on 14 May 2012.\n\n{-\n\nFrom Peter emails:\n\nAt that time I was considering an inductive data type D and an\ninductively defined equality on D. But I think what we are doing now\nis better.\n\nFor =: The reason is that with the propositional identity we have\nidentity elimination which lets us replace equals by equals. We cannot\ndo that in general if we have setoid equality.\n\nFor D: the reason why I prefer to postulate D is that we have no use\nfor the inductive structure of D, and this inductive structure would\nmake e g 0 + 0 different from 0. So in that case we need setoid\nequality.\n\n-}\n\nmodule FOTC where\n\n-- References:\n--\n-- \u2022 Gilles Barthe, Venanzio Capretta, and Olivier Pons. Setoids in\n-- type theory. Journal of Functional Programming, 13(2):261\u2013293,\n-- 2003.\n\n------------------------------------------------------------------------------\n\nmodule Setoid where\n\n -- From Peter's slides\n -- http:\/\/www.cse.chalmers.se\/~peterd\/slides\/Amagasaki.pdf\n\n -- We add 3 to the fixities of the standard library.\n infixl 9 _\u00b7_ -- The symbol is '\\cdot'.\n infix 7 _\u2250_\n\n data D : Set where\n K S : D\n _\u00b7_ : D \u2192 D \u2192 D\n\n -- The setoid equality.\n data _\u2250_ : D \u2192 D \u2192 Set where\n refl : \u2200 x \u2192 x \u2250 x\n sym : \u2200 {x y} \u2192 x \u2250 y \u2192 y \u2250 x\n trans : \u2200 {x y z} \u2192 x \u2250 y \u2192 y \u2250 z \u2192 x \u2250 z\n cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2250 x\u2082 \u2192 y\u2081 \u2250 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2250 x\u2082 \u00b7 y\u2082\n Kax : \u2200 x y \u2192 K \u00b7 x \u00b7 y \u2250 x\n Sax : \u2200 x y z \u2192 S \u00b7 x \u00b7 y \u00b7 z \u2250 x \u00b7 z \u00b7 (y \u00b7 z)\n\n -- 14 May 2012. We cannot define the identity elimination using the\n -- setoid equality.\n --\n -- subst : (A : D \u2192 Set) \u2192 \u2200 x y \u2192 x \u2250 y \u2192 A x \u2192 A y\n\n -- The identity type.\n data _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n -- 14 May 2012: Using the inductive structure we cannot prove\n --\n -- K \u00b7 x \u00b7 y \u2261 x,\n --\n -- we need the setoid equality.\n -- K-eq : \u2200 {x y} \u2192 (K \u00b7 x \u00b7 y) \u2261 x\n\n------------------------------------------------------------------------------\n\nmodule LeibnizEquality where\n\n infix 7 _\u2261_\n\n data D : Set where\n K S : D\n _\u00b7_ : D \u2192 D \u2192 D\n\n -- (Barthe et al. 2003, p. 262) use the Leibniz equality when\n -- they talk about setoids.\n\n -- Using the Leibniz equality (adapted from\n -- Agda\/examples\/lib\/Logic\/Leibniz.agda)\n\n _\u2261_ : D \u2192 D \u2192 Set\u2081\n x \u2261 y = (A : D \u2192 Set) \u2192 A x \u2192 A y\n\n -- we can prove the setoids properties\n\n refl : \u2200 x \u2192 x \u2261 x\n refl x A Ax = Ax\n\n sym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\n sym {x} h A Ay = h (\u03bb z \u2192 A z \u2192 A x) (\u03bb Ax \u2192 Ax) Ay\n\n trans : \u2200 x y z \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n trans x y z h\u2081 h\u2082 A Ax = h\u2082 A (h\u2081 A Ax)\n\n -- and the identity elimination\n\n subst : (A : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\n subst A x\u2261y = x\u2261y A\n\n -- and the congruency\n\n cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2261 x\u2082 \u2192 y\u2081 \u2261 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2261 x\u2082 \u00b7 y\u2082\n cong {x\u2081} {x\u2082} {y\u2081} {y\u2082} h\u2081 h\u2082 A Ax\u2081x\u2082 =\n h\u2082 (\u03bb z \u2192 A (x\u2082 \u00b7 z)) (h\u2081 (\u03bb z \u2192 A (z \u00b7 y\u2081)) Ax\u2081x\u2082)\n","old_contents":"------------------------------------------------------------------------------\n-- Using setoids for formalizing the FOTC\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Tested with the development version of Agda on 13 May 2012.\n\n{-\n\nFrom Peter emails:\n\nAt that time I was considering an inductive data type D and an\ninductively defined equality on D. But I think what we are doing now\nis better.\n\nFor =: The reason is that with the propositional identity we have\nidentity elimination which lets us replace equals by equals. We cannot\ndo that in general if we have setoid equality.\n\nFor D: the reason why I prefer to postulate D is that we have no use\nfor the inductive structure of D, and this inductive structure would\nmake e g 0 + 0 different from 0. So in that case we need setoid\nequality.\n\n-}\n\nmodule FOTC where\n\n-- References:\n--\n-- \u2022 Gilles Barthe, Venanzio Capretta, and Olivier Pons. Setoids in\n-- type theory. Journal of Functional Programming, 13(2):261\u2013293,\n-- 2003.\n\n------------------------------------------------------------------------------\n\nmodule Setoid where\n\n -- From Peter's slides\n -- http:\/\/www.cse.chalmers.se\/~peterd\/slides\/Amagasaki.pdf\n\n -- We add 3 to the fixities of the standard library.\n infixl 9 _\u00b7_ -- The symbol is '\\cdot'.\n infix 7 _\u2250_\n\n data D : Set where\n K S : D\n _\u00b7_ : D \u2192 D \u2192 D\n\n -- The setoid equality\n data _\u2250_ : D \u2192 D \u2192 Set where\n refl : \u2200 x \u2192 x \u2250 x\n sym : \u2200 {x y} \u2192 x \u2250 y \u2192 y \u2250 x\n trans : \u2200 {x y z} \u2192 x \u2250 y \u2192 y \u2250 z \u2192 x \u2250 z\n cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2250 x\u2082 \u2192 y\u2081 \u2250 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2250 x\u2082 \u00b7 y\u2082\n Kax : \u2200 x y \u2192 K \u00b7 x \u00b7 y \u2250 x\n Sax : \u2200 x y z \u2192 S \u00b7 x \u00b7 y \u00b7 z \u2250 x \u00b7 z \u00b7 (y \u00b7 z)\n\n -- 13 May 2012. It seems we cannot define the identity elimination\n -- using the setoid equality.\n --\n -- subst : (A : D \u2192 Set) \u2192 \u2200 x y \u2192 x \u2250 y \u2192 A x \u2192 A y\n\n -- The identity type\n data _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n -- 13 May 2012: Using the inductive structure we cannot prove\n --\n -- K \u00b7 x \u00b7 y \u2261 x,\n --\n -- we need the setoid equality.\n -- K-eq : \u2200 {x y} \u2192 (K \u00b7 x \u00b7 y) \u2261 x\n\n------------------------------------------------------------------------------\n\nmodule LeibnizEquality where\n\n data D : Set where\n K S : D\n _\u00b7_ : D \u2192 D \u2192 D\n\n -- (Barthe et al. 2003, p. 262) use the Leibniz equality when\n -- they talk about setoids.\n\n -- Using the Leibniz equality\n -- (Adapted from Agda\/examples\/lib\/Logic\/Leibniz.agda)\n\n infix 7 _\u2261_\n\n _\u2261_ : D \u2192 D \u2192 Set\u2081\n x \u2261 y = (A : D \u2192 Set) \u2192 A x \u2192 A y\n\n -- we can prove the setoids properties\n\n refl : \u2200 x \u2192 x \u2261 x\n refl x A Ax = Ax\n\n sym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\n sym {x} h A Ay = h (\u03bb z \u2192 A z \u2192 A x) (\u03bb Ax \u2192 Ax) Ay\n\n trans : \u2200 x y z \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n trans x y z h\u2081 h\u2082 A Ax = h\u2082 A (h\u2081 A Ax)\n\n -- and the identity elimination\n\n subst : (A : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\n subst A x\u2261y = x\u2261y A\n\n -- and the congruency\n\n cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2261 x\u2082 \u2192 y\u2081 \u2261 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2261 x\u2082 \u00b7 y\u2082\n cong {x\u2081} {x\u2082} {y\u2081} {y\u2082} h\u2081 h\u2082 A Ax\u2081x\u2082 =\n h\u2082 (\u03bb z \u2192 A (x\u2082 \u00b7 z)) (h\u2081 (\u03bb z \u2192 A (z \u00b7 y\u2081)) Ax\u2081x\u2082)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"675459709ff3563cde300754c35f229c6b963f10","subject":"Rename \u0394Env-Entry to ValidChange.","message":"Rename \u0394Env-Entry to ValidChange.\n\nAlso rename operations related to \u0394Env-Entry.\n\nOld-commit-hash: 35ab981b89bab6eced9a7b20753bb140f6caf7a8\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Validity.agda","new_file":"Parametric\/Change\/Validity.agda","new_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Denotation.Value as Value\n\nmodule Parametric.Change.Validity\n {Base : Type.Structure}\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\n\n-- Changes for Calculus Popl14\n--\n-- Contents\n-- - Mutually recursive concepts: \u0394Val, validity.\n-- Under module Syntax, the corresponding concepts of\n-- \u0394Type and \u0394Context reside in separate files.\n-- Now they have to be together due to mutual recursiveness.\n-- - `diff` and `apply` on semantic values of changes:\n-- they have to be here as well because they are mutually\n-- recursive with validity.\n-- - The lemma diff-is-valid: it has to be here because it is\n-- mutually recursive with `apply`\n-- - The lemma apply-diff: it is mutually recursive with `apply`\n-- and `diff`\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\nopen import Data.Product hiding (map)\n\nimport Structure.Tuples as Tuples\nopen Tuples\n\nimport Base.Data.DependentList as DependentList\nopen DependentList\n\nopen import Relation.Unary using (_\u2286_)\n\nrecord Structure : Set\u2081 where\n ----------------\n -- Parameters --\n ----------------\n\n field\n \u0394Val-base : Base \u2192 Set\n valid-base : \u2200 {\u03b9} \u2192 \u27e6 \u03b9 \u27e7Base \u2192 \u0394Val-base \u03b9 \u2192 Set\n apply-\u0394Val-base : \u2200 \u03b9 \u2192 \u27e6 \u03b9 \u27e7Base \u2192 \u0394Val-base \u03b9 \u2192 \u27e6 \u03b9 \u27e7Base\n diff-\u0394Val-base : \u2200 \u03b9 \u2192 \u27e6 \u03b9 \u27e7Base \u2192 \u27e6 \u03b9 \u27e7Base \u2192 \u0394Val-base \u03b9\n R[v,u-v]-base : \u2200 {\u03b9} {u v : \u27e6 \u03b9 \u27e7Base} \u2192 valid-base {\u03b9} v (diff-\u0394Val-base \u03b9 u v)\n v+[u-v]=u-base : \u2200 {\u03b9} {u v : \u27e6 \u03b9 \u27e7Base} \u2192 apply-\u0394Val-base \u03b9 v (diff-\u0394Val-base \u03b9 u v) \u2261 u\n\n ---------------\n -- Interface --\n ---------------\n\n \u0394Val : Type \u2192 Set\n valid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\n\n apply-\u0394Val : \u2200 \u03c4 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n diff-\u0394Val : \u2200 \u03c4 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\n\n infixl 6 apply-\u0394Val diff-\u0394Val -- as with + - in GHC.Num\n syntax apply-\u0394Val \u03c4 v dv = v \u229e\u208d \u03c4 \u208e dv\n syntax diff-\u0394Val \u03c4 u v = u \u229f\u208d \u03c4 \u208e v\n\n -- Lemma diff-is-valid\n R[v,u-v] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229f\u208d \u03c4 \u208e v)\n\n -- Lemma apply-diff\n v+[u-v]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192\n v \u229e\u208d \u03c4 \u208e (u \u229f\u208d \u03c4 \u208e v) \u2261 u\n\n --------------------\n -- Implementation --\n --------------------\n\n -- (\u0394Val \u03c4) is the set of changes of type \u03c4. This set is\n -- strictly smaller than \u27e6 \u0394Type \u03c4\u27e7 if \u03c4 is a function type. In\n -- particular, (\u0394Val (\u03c3 \u21d2 \u03c4)) is a function that accepts only\n -- valid changes, while \u27e6 \u0394Type (\u03c3 \u21d2 \u03c4) \u27e7 accepts also invalid\n -- changes.\n --\n -- \u0394Val \u03c4 is the target of the denotational specification \u27e6_\u27e7\u0394.\n -- Detailed motivation:\n --\n -- https:\/\/github.com\/ps-mr\/ilc\/blob\/184a6291ac6eef80871c32d2483e3e62578baf06\/POPL14\/paper\/sec-formal.tex\n -- \u0394Val : Type \u2192 Set\n \u0394Val (base \u03b9) = \u0394Val-base \u03b9\n \u0394Val (\u03c3 \u21d2 \u03c4) = (v : \u27e6 \u03c3 \u27e7) \u2192 (dv : \u0394Val \u03c3) \u2192 valid v dv \u2192 \u0394Val \u03c4\n\n -- _\u229e_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n n \u229e\u208d base \u03b9 \u208e \u0394n = apply-\u0394Val-base \u03b9 n \u0394n\n f \u229e\u208d \u03c3 \u21d2 \u03c4 \u208e \u0394f = \u03bb v \u2192 f v \u229e\u208d \u03c4 \u208e \u0394f v (v \u229f\u208d \u03c3 \u208e v) (R[v,u-v] {\u03c3})\n\n -- _\u229f_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\n m \u229f\u208d base \u03b9 \u208e n = diff-\u0394Val-base \u03b9 m n\n g \u229f\u208d \u03c3 \u21d2 \u03c4 \u208e f = \u03bb v \u0394v R[v,\u0394v] \u2192 g (v \u229e\u208d \u03c3 \u208e \u0394v) \u229f\u208d \u03c4 \u208e f v\n\n -- valid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\n valid {base \u03b9} n \u0394n = valid-base {\u03b9} n \u0394n\n valid {\u03c3 \u21d2 \u03c4} f \u0394f =\n \u2200 (v : \u27e6 \u03c3 \u27e7) (\u0394v : \u0394Val \u03c3) (R[v,\u0394v] : valid v \u0394v)\n \u2192 valid {\u03c4} (f v) (\u0394f v \u0394v R[v,\u0394v])\n -- \u00d7 (f \u229e \u0394f) (v \u229e \u0394v) \u2261 f v \u229e \u0394f v \u0394v R[v,\u0394v]\n \u00d7 (f \u229e\u208d \u03c3 \u21d2 \u03c4 \u208e \u0394f) (v \u229e\u208d \u03c3 \u208e \u0394v) \u2261 f v \u229e\u208d \u03c4 \u208e \u0394f v \u0394v R[v,\u0394v]\n\n -- v+[u-v]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 v \u229e (u \u229f v) \u2261 u\n v+[u-v]=u {base \u03b9} {u} {v} = v+[u-v]=u-base {\u03b9} {u} {v}\n v+[u-v]=u {\u03c3 \u21d2 \u03c4} {u} {v} =\n ext {-\u27e6 \u03c3 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4 \u27e7-} (\u03bb w \u2192\n begin\n (v \u229e\u208d \u03c3 \u21d2 \u03c4 \u208e (u \u229f\u208d \u03c3 \u21d2 \u03c4 \u208e v)) w\n \u2261\u27e8 refl \u27e9\n v w \u229e\u208d \u03c4 \u208e (u (w \u229e\u208d \u03c3 \u208e (w \u229f\u208d \u03c3 \u208e w)) \u229f\u208d \u03c4 \u208e v w)\n \u2261\u27e8 cong (\u03bb hole \u2192 v w \u229e\u208d \u03c4 \u208e (u hole \u229f\u208d \u03c4 \u208e v w)) (v+[u-v]=u {\u03c3}) \u27e9\n v w \u229e\u208d \u03c4 \u208e (u w \u229f\u208d \u03c4 \u208e v w)\n \u2261\u27e8 v+[u-v]=u {\u03c4} \u27e9\n u w\n \u220e) where\n open \u2261-Reasoning\n\n R[v,u-v] {base \u03b9} {u} {v} = R[v,u-v]-base {\u03b9} {u} {v}\n R[v,u-v] {\u03c3 \u21d2 \u03c4} {u} {v} = \u03bb w \u0394w R[w,\u0394w] \u2192\n let\n w\u2032 = w \u229e\u208d \u03c3 \u208e \u0394w\n in\n R[v,u-v] {\u03c4}\n ,\n (begin\n (v \u229e\u208d \u03c3 \u21d2 \u03c4 \u208e (u \u229f\u208d \u03c3 \u21d2 \u03c4 \u208e v)) w\u2032\n \u2261\u27e8 cong (\u03bb hole \u2192 hole w\u2032) (v+[u-v]=u {\u03c3 \u21d2 \u03c4} {u} {v}) \u27e9\n u w\u2032\n \u2261\u27e8 sym (v+[u-v]=u {\u03c4} {u w\u2032} {v w}) \u27e9\n v w \u229e\u208d \u03c4 \u208e (u \u229f\u208d \u03c3 \u21d2 \u03c4 \u208e v) w \u0394w R[w,\u0394w]\n \u220e) where open \u2261-Reasoning\n\n -- syntactic sugar for implicit indices\n infixl 6 _\u229e_ _\u229f_ -- as with + - in GHC.Num\n\n _\u229e_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n _\u229e_ {\u03c4} v dv = v \u229e\u208d \u03c4 \u208e dv\n\n _\u229f_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\n _\u229f_ {\u03c4} u v = u \u229f\u208d \u03c4 \u208e v\n\n ------------------\n -- Environments --\n ------------------\n\n open DependentList public using (\u2205; _\u2022_)\n open Tuples public using (cons)\n\n ValidChange : Type \u2192 Set\n ValidChange \u03c4 = Triple\n \u27e6 \u03c4 \u27e7\n (\u03bb _ \u2192 \u0394Val \u03c4)\n (\u03bb v dv \u2192 valid {\u03c4} v dv)\n\n \u0394Env : Context \u2192 Set\n \u0394Env = DependentList ValidChange\n\n ignore-valid-change : ValidChange \u2286 \u27e6_\u27e7\n ignore-valid-change (cons v _ _) = v\n\n update-valid-change : ValidChange \u2286 \u27e6_\u27e7\n update-valid-change {\u03c4} (cons v dv R[v,dv]) = v \u229e\u208d \u03c4 \u208e dv\n\n ignore : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394Env \u0393) \u2192 \u27e6 \u0393 \u27e7\n ignore = map (\u03bb {\u03c4} \u2192 ignore-valid-change {\u03c4})\n\n update : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394Env \u0393) \u2192 \u27e6 \u0393 \u27e7\n update = map (\u03bb {\u03c4} \u2192 update-valid-change {\u03c4})\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Denotation.Value as Value\n\nmodule Parametric.Change.Validity\n {Base : Type.Structure}\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\n\n-- Changes for Calculus Popl14\n--\n-- Contents\n-- - Mutually recursive concepts: \u0394Val, validity.\n-- Under module Syntax, the corresponding concepts of\n-- \u0394Type and \u0394Context reside in separate files.\n-- Now they have to be together due to mutual recursiveness.\n-- - `diff` and `apply` on semantic values of changes:\n-- they have to be here as well because they are mutually\n-- recursive with validity.\n-- - The lemma diff-is-valid: it has to be here because it is\n-- mutually recursive with `apply`\n-- - The lemma apply-diff: it is mutually recursive with `apply`\n-- and `diff`\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\nopen import Data.Product hiding (map)\n\nimport Structure.Tuples as Tuples\nopen Tuples\n\nimport Base.Data.DependentList as DependentList\nopen DependentList\n\nopen import Relation.Unary using (_\u2286_)\n\nrecord Structure : Set\u2081 where\n ----------------\n -- Parameters --\n ----------------\n\n field\n \u0394Val-base : Base \u2192 Set\n valid-base : \u2200 {\u03b9} \u2192 \u27e6 \u03b9 \u27e7Base \u2192 \u0394Val-base \u03b9 \u2192 Set\n apply-\u0394Val-base : \u2200 \u03b9 \u2192 \u27e6 \u03b9 \u27e7Base \u2192 \u0394Val-base \u03b9 \u2192 \u27e6 \u03b9 \u27e7Base\n diff-\u0394Val-base : \u2200 \u03b9 \u2192 \u27e6 \u03b9 \u27e7Base \u2192 \u27e6 \u03b9 \u27e7Base \u2192 \u0394Val-base \u03b9\n R[v,u-v]-base : \u2200 {\u03b9} {u v : \u27e6 \u03b9 \u27e7Base} \u2192 valid-base {\u03b9} v (diff-\u0394Val-base \u03b9 u v)\n v+[u-v]=u-base : \u2200 {\u03b9} {u v : \u27e6 \u03b9 \u27e7Base} \u2192 apply-\u0394Val-base \u03b9 v (diff-\u0394Val-base \u03b9 u v) \u2261 u\n\n ---------------\n -- Interface --\n ---------------\n\n \u0394Val : Type \u2192 Set\n valid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\n\n apply-\u0394Val : \u2200 \u03c4 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n diff-\u0394Val : \u2200 \u03c4 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\n\n infixl 6 apply-\u0394Val diff-\u0394Val -- as with + - in GHC.Num\n syntax apply-\u0394Val \u03c4 v dv = v \u229e\u208d \u03c4 \u208e dv\n syntax diff-\u0394Val \u03c4 u v = u \u229f\u208d \u03c4 \u208e v\n\n -- Lemma diff-is-valid\n R[v,u-v] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229f\u208d \u03c4 \u208e v)\n\n -- Lemma apply-diff\n v+[u-v]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192\n v \u229e\u208d \u03c4 \u208e (u \u229f\u208d \u03c4 \u208e v) \u2261 u\n\n --------------------\n -- Implementation --\n --------------------\n\n -- (\u0394Val \u03c4) is the set of changes of type \u03c4. This set is\n -- strictly smaller than \u27e6 \u0394Type \u03c4\u27e7 if \u03c4 is a function type. In\n -- particular, (\u0394Val (\u03c3 \u21d2 \u03c4)) is a function that accepts only\n -- valid changes, while \u27e6 \u0394Type (\u03c3 \u21d2 \u03c4) \u27e7 accepts also invalid\n -- changes.\n --\n -- \u0394Val \u03c4 is the target of the denotational specification \u27e6_\u27e7\u0394.\n -- Detailed motivation:\n --\n -- https:\/\/github.com\/ps-mr\/ilc\/blob\/184a6291ac6eef80871c32d2483e3e62578baf06\/POPL14\/paper\/sec-formal.tex\n -- \u0394Val : Type \u2192 Set\n \u0394Val (base \u03b9) = \u0394Val-base \u03b9\n \u0394Val (\u03c3 \u21d2 \u03c4) = (v : \u27e6 \u03c3 \u27e7) \u2192 (dv : \u0394Val \u03c3) \u2192 valid v dv \u2192 \u0394Val \u03c4\n\n -- _\u229e_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n n \u229e\u208d base \u03b9 \u208e \u0394n = apply-\u0394Val-base \u03b9 n \u0394n\n f \u229e\u208d \u03c3 \u21d2 \u03c4 \u208e \u0394f = \u03bb v \u2192 f v \u229e\u208d \u03c4 \u208e \u0394f v (v \u229f\u208d \u03c3 \u208e v) (R[v,u-v] {\u03c3})\n\n -- _\u229f_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\n m \u229f\u208d base \u03b9 \u208e n = diff-\u0394Val-base \u03b9 m n\n g \u229f\u208d \u03c3 \u21d2 \u03c4 \u208e f = \u03bb v \u0394v R[v,\u0394v] \u2192 g (v \u229e\u208d \u03c3 \u208e \u0394v) \u229f\u208d \u03c4 \u208e f v\n\n -- valid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\n valid {base \u03b9} n \u0394n = valid-base {\u03b9} n \u0394n\n valid {\u03c3 \u21d2 \u03c4} f \u0394f =\n \u2200 (v : \u27e6 \u03c3 \u27e7) (\u0394v : \u0394Val \u03c3) (R[v,\u0394v] : valid v \u0394v)\n \u2192 valid {\u03c4} (f v) (\u0394f v \u0394v R[v,\u0394v])\n -- \u00d7 (f \u229e \u0394f) (v \u229e \u0394v) \u2261 f v \u229e \u0394f v \u0394v R[v,\u0394v]\n \u00d7 (f \u229e\u208d \u03c3 \u21d2 \u03c4 \u208e \u0394f) (v \u229e\u208d \u03c3 \u208e \u0394v) \u2261 f v \u229e\u208d \u03c4 \u208e \u0394f v \u0394v R[v,\u0394v]\n\n -- v+[u-v]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 v \u229e (u \u229f v) \u2261 u\n v+[u-v]=u {base \u03b9} {u} {v} = v+[u-v]=u-base {\u03b9} {u} {v}\n v+[u-v]=u {\u03c3 \u21d2 \u03c4} {u} {v} =\n ext {-\u27e6 \u03c3 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4 \u27e7-} (\u03bb w \u2192\n begin\n (v \u229e\u208d \u03c3 \u21d2 \u03c4 \u208e (u \u229f\u208d \u03c3 \u21d2 \u03c4 \u208e v)) w\n \u2261\u27e8 refl \u27e9\n v w \u229e\u208d \u03c4 \u208e (u (w \u229e\u208d \u03c3 \u208e (w \u229f\u208d \u03c3 \u208e w)) \u229f\u208d \u03c4 \u208e v w)\n \u2261\u27e8 cong (\u03bb hole \u2192 v w \u229e\u208d \u03c4 \u208e (u hole \u229f\u208d \u03c4 \u208e v w)) (v+[u-v]=u {\u03c3}) \u27e9\n v w \u229e\u208d \u03c4 \u208e (u w \u229f\u208d \u03c4 \u208e v w)\n \u2261\u27e8 v+[u-v]=u {\u03c4} \u27e9\n u w\n \u220e) where\n open \u2261-Reasoning\n\n R[v,u-v] {base \u03b9} {u} {v} = R[v,u-v]-base {\u03b9} {u} {v}\n R[v,u-v] {\u03c3 \u21d2 \u03c4} {u} {v} = \u03bb w \u0394w R[w,\u0394w] \u2192\n let\n w\u2032 = w \u229e\u208d \u03c3 \u208e \u0394w\n in\n R[v,u-v] {\u03c4}\n ,\n (begin\n (v \u229e\u208d \u03c3 \u21d2 \u03c4 \u208e (u \u229f\u208d \u03c3 \u21d2 \u03c4 \u208e v)) w\u2032\n \u2261\u27e8 cong (\u03bb hole \u2192 hole w\u2032) (v+[u-v]=u {\u03c3 \u21d2 \u03c4} {u} {v}) \u27e9\n u w\u2032\n \u2261\u27e8 sym (v+[u-v]=u {\u03c4} {u w\u2032} {v w}) \u27e9\n v w \u229e\u208d \u03c4 \u208e (u \u229f\u208d \u03c3 \u21d2 \u03c4 \u208e v) w \u0394w R[w,\u0394w]\n \u220e) where open \u2261-Reasoning\n\n -- syntactic sugar for implicit indices\n infixl 6 _\u229e_ _\u229f_ -- as with + - in GHC.Num\n\n _\u229e_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n _\u229e_ {\u03c4} v dv = v \u229e\u208d \u03c4 \u208e dv\n\n _\u229f_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\n _\u229f_ {\u03c4} u v = u \u229f\u208d \u03c4 \u208e v\n\n ------------------\n -- Environments --\n ------------------\n\n open DependentList public using (\u2205; _\u2022_)\n open Tuples public using (cons)\n\n \u0394Env-Entry : Type \u2192 Set\n \u0394Env-Entry \u03c4 = Triple\n \u27e6 \u03c4 \u27e7\n (\u03bb _ \u2192 \u0394Val \u03c4)\n (\u03bb v dv \u2192 valid {\u03c4} v dv)\n\n \u0394Env : Context \u2192 Set\n \u0394Env = DependentList \u0394Env-Entry\n\n ignore-entry : \u0394Env-Entry \u2286 \u27e6_\u27e7\n ignore-entry (cons v _ _) = v\n\n update-entry : \u0394Env-Entry \u2286 \u27e6_\u27e7\n update-entry {\u03c4} (cons v dv R[v,dv]) = v \u229e\u208d \u03c4 \u208e dv\n\n ignore : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394Env \u0393) \u2192 \u27e6 \u0393 \u27e7\n ignore = map (\u03bb {\u03c4} \u2192 ignore-entry {\u03c4})\n\n update : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394Env \u0393) \u2192 \u27e6 \u0393 \u27e7\n update = map (\u03bb {\u03c4} \u2192 update-entry {\u03c4})\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"5f825cae8441f0fd6b7b1d5bdcba4517455f3cf5","subject":"lib\/Explore\/Summable.agda","message":"lib\/Explore\/Summable.agda\n","repos":"crypto-agda\/explore","old_file":"lib\/Explore\/Summable.agda","new_file":"lib\/Explore\/Summable.agda","new_contents":"module Explore.Summable where\n\nopen import Type\nopen import Function.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_ ; _\u2257_ ; _\u2257\u2082_)\nopen import Explore.Type\nopen import Explore.Product\nopen import Data.Product\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\nopen import Data.Bool.NP renaming (Bool to \ud835\udfda; true to 1b; false to 0b; to\u2115 to \ud835\udfda\u25b9\u2115)\nopen Data.Bool.NP.Indexed\n\nmodule FromSum {A : \u2605} (sum : Sum A) where\n Card : \u2115\n Card = sum (const 1)\n\n count : Count A\n count f = sum (\ud835\udfda\u25b9\u2115 \u2218 f)\n\nmodule FromSumInd {A : \u2605}\n {sum : Sum A}\n (sum-ind : SumInd sum) where\n open FromSum sum public\n\n sum-ext : SumExt sum\n sum-ext = sum-ind (\u03bb s \u2192 s _ \u2261 s _) (\u2261.cong\u2082 _+_)\n\n sum-zero : SumZero sum\n sum-zero = sum-ind (\u03bb s \u2192 s (const 0) \u2261 0) (\u2261.cong\u2082 _+_) (\u03bb _ \u2192 \u2261.refl)\n\n sum-hom : SumHom sum\n sum-hom f g = sum-ind (\u03bb s \u2192 s (f +\u00b0 g) \u2261 s f + s g)\n (\u03bb {s\u2080} {s\u2081} p\u2080 p\u2081 \u2192 \u2261.trans (\u2261.cong\u2082 _+_ p\u2080 p\u2081) (+-interchange (s\u2080 _) (s\u2080 _) _ _))\n (\u03bb _ \u2192 \u2261.refl)\n\n sum-mono : SumMono sum\n sum-mono = sum-ind (\u03bb s \u2192 s _ \u2264 s _) _+-mono_\n\n sum-lin : SumLin sum\n sum-lin f zero = sum-zero\n sum-lin f (suc k) = \u2261.trans (sum-hom f (\u03bb x \u2192 k * f x)) (\u2261.cong\u2082 _+_ (\u2261.refl {x = sum f}) (sum-lin f k))\n\n module _ (f g : A \u2192 \u2115) where\n open \u2261.\u2261-Reasoning\n\n sum-\u2293-\u2238 : sum f \u2261 sum (f \u2293\u00b0 g) + sum (f \u2238\u00b0 g)\n sum-\u2293-\u2238 = sum f \u2261\u27e8 sum-ext (f \u27e8 a\u2261a\u2293b+a\u2238b \u27e9\u00b0 g) \u27e9\n sum ((f \u2293\u00b0 g) +\u00b0 (f \u2238\u00b0 g)) \u2261\u27e8 sum-hom (f \u2293\u00b0 g) (f \u2238\u00b0 g) \u27e9\n sum (f \u2293\u00b0 g) + sum (f \u2238\u00b0 g) \u220e\n\n sum-\u2294-\u2293 : sum f + sum g \u2261 sum (f \u2294\u00b0 g) + sum (f \u2293\u00b0 g)\n sum-\u2294-\u2293 = sum f + sum g \u2261\u27e8 \u2261.sym (sum-hom f g) \u27e9\n sum (f +\u00b0 g) \u2261\u27e8 sum-ext (f \u27e8 a+b\u2261a\u2294b+a\u2293b \u27e9\u00b0 g) \u27e9\n sum (f \u2294\u00b0 g +\u00b0 f \u2293\u00b0 g) \u2261\u27e8 sum-hom (f \u2294\u00b0 g) (f \u2293\u00b0 g) \u27e9\n sum (f \u2294\u00b0 g) + sum (f \u2293\u00b0 g) \u220e\n\n sum-\u2294 : sum (f \u2294\u00b0 g) \u2264 sum f + sum g\n sum-\u2294 = \u2115\u2264.trans (sum-mono (f \u27e8 \u2294\u2264+ \u27e9\u00b0 g)) (\u2115\u2264.reflexive (sum-hom f g))\n\n count-ext : CountExt count\n count-ext f\u2257g = sum-ext (\u2261.cong \ud835\udfda\u25b9\u2115 \u2218 f\u2257g)\n\n sum-const : \u2200 k \u2192 sum (const k) \u2261 Card * k\n sum-const k\n rewrite \u2115\u00b0.*-comm Card k\n | \u2261.sym (sum-lin (const 1) k)\n | proj\u2082 \u2115\u00b0.*-identity k = \u2261.refl\n\n module _ f g where\n count-\u2227-not : count f \u2261 count (f \u2227\u00b0 g) + count (f \u2227\u00b0 not\u00b0 g)\n count-\u2227-not rewrite sum-\u2293-\u2238 (\ud835\udfda\u25b9\u2115 \u2218 f) (\ud835\udfda\u25b9\u2115 \u2218 g)\n | sum-ext (f \u27e8 to\u2115-\u2293 \u27e9\u00b0 g)\n | sum-ext (f \u27e8 to\u2115-\u2238 \u27e9\u00b0 g)\n = \u2261.refl\n\n count-\u2228-\u2227 : count f + count g \u2261 count (f \u2228\u00b0 g) + count (f \u2227\u00b0 g)\n count-\u2228-\u2227 rewrite sum-\u2294-\u2293 (\ud835\udfda\u25b9\u2115 \u2218 f) (\ud835\udfda\u25b9\u2115 \u2218 g)\n | sum-ext (f \u27e8 to\u2115-\u2294 \u27e9\u00b0 g)\n | sum-ext (f \u27e8 to\u2115-\u2293 \u27e9\u00b0 g)\n = \u2261.refl\n\n count-\u2228\u2264+ : count (f \u2228\u00b0 g) \u2264 count f + count g\n count-\u2228\u2264+ = \u2115\u2264.trans (\u2115\u2264.reflexive (sum-ext (\u2261.sym \u2218 (f \u27e8 to\u2115-\u2294 \u27e9\u00b0 g))))\n (sum-\u2294 (\ud835\udfda\u25b9\u2115 \u2218 f) (\ud835\udfda\u25b9\u2115 \u2218 g))\n\nmodule FromSum\u00d7\n {A B}\n {sum\u1d2c : Sum A}\n (sum-ind\u1d2c : SumInd sum\u1d2c)\n {sum\u1d2e : Sum B}\n (sum-ind\u1d2e : SumInd sum\u1d2e) where\n\n module |A| = FromSumInd sum-ind\u1d2c\n module |B| = FromSumInd sum-ind\u1d2e\n open Operators\n\n sum\u1d2c\u1d2e = sum\u1d2c \u00d7\u02e2 sum\u1d2e\n\n sum-\u2218proj\u2081\u2261Card* : \u2200 f \u2192 sum\u1d2c\u1d2e (f \u2218 proj\u2081) \u2261 |B|.Card * sum\u1d2c f\n sum-\u2218proj\u2081\u2261Card* f\n rewrite |A|.sum-ext (|B|.sum-const \u2218 f)\n = |A|.sum-lin f |B|.Card\n\n sum-\u2218proj\u2082\u2261Card* : \u2200 f \u2192 sum\u1d2c\u1d2e (f \u2218 proj\u2082) \u2261 |A|.Card * sum\u1d2e f\n sum-\u2218proj\u2082\u2261Card* = |A|.sum-const \u2218 sum\u1d2e\n\n sum-\u2218proj\u2081 : \u2200 {f} {g} \u2192 sum\u1d2c f \u2261 sum\u1d2c g \u2192 sum\u1d2c\u1d2e (f \u2218 proj\u2081) \u2261 sum\u1d2c\u1d2e (g \u2218 proj\u2081)\n sum-\u2218proj\u2081 {f} {g} sumf\u2261sumg\n rewrite sum-\u2218proj\u2081\u2261Card* f\n | sum-\u2218proj\u2081\u2261Card* g\n | sumf\u2261sumg = \u2261.refl\n\n sum-\u2218proj\u2082 : \u2200 {f} {g} \u2192 sum\u1d2e f \u2261 sum\u1d2e g \u2192 sum\u1d2c\u1d2e (f \u2218 proj\u2082) \u2261 sum\u1d2c\u1d2e (g \u2218 proj\u2082)\n sum-\u2218proj\u2082 sumf\u2261sumg = |A|.sum-ext (const sumf\u2261sumg)\n\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"module Explore.Summable where\n\nopen import Type\nopen import Function.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_ ; _\u2257_ ; _\u2257\u2082_)\nopen import Explore.Type\nopen import Explore.Product\nopen import Data.Product\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\nopen import Data.Bool.NP renaming (Bool to \ud835\udfda; true to 1b; false to 0b; to\u2115 to \ud835\udfda\u25b9\u2115)\nopen Data.Bool.NP.Indexed\n\nmodule FromSum {A : \u2605} (sum : Sum A) where\n Card : \u2115\n Card = sum (const 1)\n\n count : Count A\n count f = sum (\ud835\udfda\u25b9\u2115 \u2218 f)\n\nmodule FromSumInd {A : \u2605}\n {sum : Sum A}\n (sum-ind : SumInd sum) where\n open FromSum sum public\n\n sum-ext : SumExt sum\n sum-ext = sum-ind (\u03bb s \u2192 s _ \u2261 s _) (\u2261.cong\u2082 _+_)\n\n sum-zero : SumZero sum\n sum-zero = sum-ind (\u03bb s \u2192 s (const 0) \u2261 0) (\u2261.cong\u2082 _+_) (\u03bb _ \u2192 \u2261.refl)\n\n sum-hom : SumHom sum\n sum-hom f g = sum-ind (\u03bb s \u2192 s (f +\u00b0 g) \u2261 s f + s g)\n (\u03bb {s\u2080} {s\u2081} p\u2080 p\u2081 \u2192 \u2261.trans (\u2261.cong\u2082 _+_ p\u2080 p\u2081) (+-interchange (s\u2080 _) (s\u2080 _) _ _))\n (\u03bb _ \u2192 \u2261.refl)\n\n sum-mono : SumMono sum\n sum-mono = sum-ind (\u03bb s \u2192 s _ \u2264 s _) _+-mono_\n\n sum-lin : SumLin sum\n sum-lin f zero = sum-zero\n sum-lin f (suc k) = \u2261.trans (sum-hom f (\u03bb x \u2192 k * f x)) (\u2261.cong\u2082 _+_ (\u2261.refl {x = sum f}) (sum-lin f k))\n\n module _ (f g : A \u2192 \u2115) where\n open \u2261.\u2261-Reasoning\n\n sum-\u2293-\u2238 : sum f \u2261 sum (f \u2293\u00b0 g) + sum (f \u2238\u00b0 g)\n sum-\u2293-\u2238 = sum f \u2261\u27e8 sum-ext (f \u27e8 a\u2261a\u2293b+a\u2238b \u27e9\u00b0 g) \u27e9\n sum ((f \u2293\u00b0 g) +\u00b0 (f \u2238\u00b0 g)) \u2261\u27e8 sum-hom (f \u2293\u00b0 g) (f \u2238\u00b0 g) \u27e9\n sum (f \u2293\u00b0 g) + sum (f \u2238\u00b0 g) \u220e\n\n sum-\u2294-\u2293 : sum f + sum g \u2261 sum (f \u2294\u00b0 g) + sum (f \u2293\u00b0 g)\n sum-\u2294-\u2293 = sum f + sum g \u2261\u27e8 \u2261.sym (sum-hom f g) \u27e9\n sum (f +\u00b0 g) \u2261\u27e8 sum-ext (f \u27e8 a+b\u2261a\u2294b+a\u2293b \u27e9\u00b0 g) \u27e9\n sum (f \u2294\u00b0 g +\u00b0 f \u2293\u00b0 g) \u2261\u27e8 sum-hom (f \u2294\u00b0 g) (f \u2293\u00b0 g) \u27e9\n sum (f \u2294\u00b0 g) + sum (f \u2293\u00b0 g) \u220e\n\n sum-\u2294 : sum (f \u2294\u00b0 g) \u2264 sum f + sum g\n sum-\u2294 = \u2115\u2264.trans (sum-mono (f \u27e8 \u2294\u2264+ \u27e9\u00b0 g)) (\u2115\u2264.reflexive (sum-hom f g))\n\n count-ext : CountExt count\n count-ext f\u2257g = sum-ext (\u2261.cong \ud835\udfda\u25b9\u2115 \u2218 f\u2257g)\n\n sum-const : \u2200 k \u2192 sum (const k) \u2261 Card * k\n sum-const k\n rewrite \u2115\u00b0.*-comm Card k\n | \u2261.sym (sum-lin (const 1) k)\n | proj\u2082 \u2115\u00b0.*-identity k = \u2261.refl\n\n module _ f g where\n count-\u2227-not : count f \u2261 count (f \u2227\u00b0 g) + count (f \u2227\u00b0 not\u00b0 g)\n count-\u2227-not rewrite sum-\u2293-\u2238 (\ud835\udfda\u25b9\u2115 \u2218 f) (\ud835\udfda\u25b9\u2115 \u2218 g)\n | sum-ext (f \u27e8 to\u2115-\u2293 \u27e9\u00b0 g)\n | sum-ext (f \u27e8 to\u2115-\u2238 \u27e9\u00b0 g)\n = \u2261.refl\n\n count-\u2228-\u2227 : count f + count g \u2261 count (f \u2228\u00b0 g) + count (f \u2227\u00b0 g)\n count-\u2228-\u2227 rewrite sum-\u2294-\u2293 (\ud835\udfda\u25b9\u2115 \u2218 f) (\ud835\udfda\u25b9\u2115 \u2218 g)\n | sum-ext (f \u27e8 to\u2115-\u2294 \u27e9\u00b0 g)\n | sum-ext (f \u27e8 to\u2115-\u2293 \u27e9\u00b0 g)\n = \u2261.refl\n\n count-\u2228\u2264+ : count (f \u2228\u00b0 g) \u2264 count f + count g\n count-\u2228\u2264+ = \u2115\u2264.trans (\u2115\u2264.reflexive (sum-ext (\u2261.sym \u2218 (f \u27e8 to\u2115-\u2294 \u27e9\u00b0 g))))\n (sum-\u2294 (\ud835\udfda\u25b9\u2115 \u2218 f) (\ud835\udfda\u25b9\u2115 \u2218 g))\n\nmodule FromSum\u00d7\n {A B}\n {sum\u1d2c : Sum A}\n (sum-ind\u1d2c : SumInd sum\u1d2c)\n {sum\u1d2e : Sum B}\n (sum-ind\u1d2e : SumInd sum\u1d2e) where\n\n module |A| = FromSumInd sum-ind\u1d2c\n module |B| = FromSumInd sum-ind\u1d2e\n open Operators\n \n sum\u1d2c\u1d2e = sum\u1d2c \u00d7\u02e2 sum\u1d2e\n\n sum-\u2218proj\u2081\u2261Card* : \u2200 f \u2192 sum\u1d2c\u1d2e (f \u2218 proj\u2081) \u2261 |B|.Card * sum\u1d2c f\n sum-\u2218proj\u2081\u2261Card* f\n rewrite |A|.sum-ext (|B|.sum-const \u2218 f)\n = |A|.sum-lin f |B|.Card\n\n sum-\u2218proj\u2082\u2261Card* : \u2200 f \u2192 sum\u1d2c\u1d2e (f \u2218 proj\u2082) \u2261 |A|.Card * sum\u1d2e f\n sum-\u2218proj\u2082\u2261Card* = |A|.sum-const \u2218 sum\u1d2e\n\n sum-\u2218proj\u2081 : \u2200 {f} {g} \u2192 sum\u1d2c f \u2261 sum\u1d2c g \u2192 sum\u1d2c\u1d2e (f \u2218 proj\u2081) \u2261 sum\u1d2c\u1d2e (g \u2218 proj\u2081)\n sum-\u2218proj\u2081 {f} {g} sumf\u2261sumg\n rewrite sum-\u2218proj\u2081\u2261Card* f\n | sum-\u2218proj\u2081\u2261Card* g\n | sumf\u2261sumg = \u2261.refl\n\n sum-\u2218proj\u2082 : \u2200 {f} {g} \u2192 sum\u1d2e f \u2261 sum\u1d2e g \u2192 sum\u1d2c\u1d2e (f \u2218 proj\u2082) \u2261 sum\u1d2c\u1d2e (g \u2218 proj\u2082)\n sum-\u2218proj\u2082 sumf\u2261sumg = |A|.sum-ext (const sumf\u2261sumg)\n\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"bc3f375b16b82ac64c6d762e6ffd4f7483bd4984","subject":"update linear solver with isomorphisms","message":"update linear solver with isomorphisms\n","repos":"crypto-agda\/crypto-agda","old_file":"linear-solver.agda","new_file":"linear-solver.agda","new_contents":"module linear-solver where\n\nopen import Data.Nat as \u2115 using (\u2115)\nopen import Data.Fin as F using (Fin)\nimport Data.Fin.Props as FP\n\nmodule Syntax {a} (A : Set a)(_x_ : A \u2192 A \u2192 A)(T : A)\n (R' : A \u2192 A \u2192 Set)\n (id' : \u2200 {A} \u2192 R' A A)\n (_\u223b'_ : \u2200 {A B C} \u2192 R' A B \u2192 R' B C \u2192 R' A C)\n (' : \u2200 {A} \u2192 R' (A x T) A)\n (\u207b\u00b9' : \u2200 {A} \u2192 R' A (A x T))\n (' : \u2200 {A} \u2192 R' (T x A) A)\n (\u207b\u00b9' : \u2200 {A} \u2192 R' A (T x A))\n (\u27e8_\u00d7'_\u27e9 : \u2200 {A B C D} \u2192 R' A C \u2192 R' B D \u2192 R' (A x B) (C x D))\n (first' : \u2200 {A B C} \u2192 R' A B \u2192 R' (A x C) (B x C))\n (second' : \u2200 {A B C} \u2192 R' B C \u2192 R' (A x B) (A x C))\n (assoc' : \u2200 {A B C} \u2192 R' (A x (B x C)) ((A x B) x C))\n (assoc\u207b\u00b9' : \u2200 {A B C} \u2192 R' ((A x B) x C) (A x (B x C)))\n (swap' : \u2200 {A B} \u2192 R' (A x B) (B x A))\n nrVars (!_ : Fin nrVars \u2192 A) where\n\n Var = Fin nrVars\n\n open import Relation.Nullary using (yes ; no)\n open import Relation.Nullary.Decidable\n\n data Syn : Set where\n var : Var \u2192 Syn\n tt : Syn\n _,_ : Syn \u2192 Syn \u2192 Syn\n\n #_ : \u2200 m {m : \u2200 {A} \u2192 R (A , tt) A\n : \u2200 {A} \u2192 R (tt , A) A\n \u207b\u00b9 : \u2200 {A} \u2192 R A (tt , A)\n \u207b\u00b9 : \u2200 {A} \u2192 R A (A , tt)\n \u27e8_\u00d7''_\u27e9 : \u2200 {A B C D} \u2192 R A C \u2192 R B D \u2192 R (A , B) (C , D)\n assoc : \u2200 {A B C} \u2192 R (A , (B , C)) ((A , B) , C)\n assoc\u207b\u00b9 : \u2200 {A B C} \u2192 R ((A , B) , C) (A , (B , C))\n id : \u2200 {A} \u2192 R A A\n swap : \u2200 {A B} \u2192 R (A , B) (B , A)\n\n \u27e8_\u00d7_\u27e9 : \u2200 {A B C D} \u2192 R A C \u2192 R B D \u2192 R (A , B) (C , D)\n \u27e8 id \u00d7 id \u27e9 = id\n \u27e8 r\u2081 \u00d7 r\u2082 \u27e9 = \u27e8 r\u2081 \u00d7'' r\u2082 \u27e9\n\n _\u223b_ : \u2200 {A B C} \u2192 R A B \u2192 R B C \u2192 R A C\n id \u223b r\u2082 = r\u2082\n r\u2081 \u223b id = r\u2081\n \u207b\u00b9 \u223b = id\n \u207b\u00b9 \u223b = id\n \u223b \u207b\u00b9 = id\n \u223b \u207b\u00b9 = id\n swap \u223b = \n swap \u223b = \n \u207b\u00b9 \u223b swap = \u207b\u00b9\n \u207b\u00b9 \u223b swap = \u207b\u00b9\n assoc \u223b assoc\u207b\u00b9 = id\n assoc \u223b (assoc\u207b\u00b9 \u223b'' r) = r\n assoc\u207b\u00b9 \u223b assoc = id\n assoc\u207b\u00b9 \u223b (assoc \u223b'' r) = r\n swap \u223b swap = id\n swap \u223b (swap \u223b'' r) = r\n (r\u2081 \u223b'' r\u2082) \u223b r\u2083 = r\u2081 \u223b (r\u2082 \u223b r\u2083)\n \u27e8 r\u2081 \u00d7'' r\u2082 \u27e9 \u223b \u27e8 r\u2083 \u00d7'' r\u2084 \u27e9 = \u27e8 r\u2081 \u223b r\u2083 \u00d7 r\u2082 \u223b r\u2084 \u27e9\n \u27e8 r\u2081 \u00d7'' r\u2082 \u27e9 \u223b (\u27e8 r\u2083 \u00d7'' r\u2084 \u27e9 \u223b'' r\u2085) with \u27e8 r\u2081 \u223b r\u2083 \u00d7 r\u2082 \u223b r\u2084 \u27e9\n ... |\u00a0id = r\u2085\n ... |\u00a0r\u2086 = r\u2086 \u223b'' r\u2085\n r\u2081 \u223b r\u2082 = r\u2081 \u223b'' r\u2082 \n\n sym : \u2200 {S S'} \u2192 R S S' \u2192 R S' S\n sym (r \u223b'' r\u2081) = sym r\u2081 \u223b sym r\n sym = \u207b\u00b9\n sym = \u207b\u00b9\n sym \u207b\u00b9 = \n sym \u207b\u00b9 = \n sym \u27e8 r \u00d7'' r\u2081 \u27e9 = \u27e8 sym r \u00d7 sym r\u2081 \u27e9\n sym assoc = assoc\u207b\u00b9\n sym assoc\u207b\u00b9 = assoc\n sym id = id\n sym swap = swap\n\n proof\u2081 : \u2200 {S S'} \u2192 R S S' \u2192 R' (eval S) (eval S')\n proof\u2081 (r \u223b'' r\u2081) = proof\u2081 r \u223b' proof\u2081 r\u2081\n proof\u2081 = '\n proof\u2081 = '\n proof\u2081 \u207b\u00b9 = \u207b\u00b9'\n proof\u2081 \u207b\u00b9 = \u207b\u00b9'\n proof\u2081 \u27e8 id \u00d7'' r \u27e9 = second' (proof\u2081 r)\n proof\u2081 \u27e8 r \u00d7'' id \u27e9 = first' (proof\u2081 r)\n proof\u2081 \u27e8 r \u00d7'' r\u2081 \u27e9 = \u27e8 proof\u2081 r \u00d7' proof\u2081 r\u2081 \u27e9\n proof\u2081 assoc = assoc'\n proof\u2081 assoc\u207b\u00b9 = assoc\u207b\u00b9'\n proof\u2081 id = id'\n proof\u2081 swap = swap'\n\n proof\u2082 : \u2200 {S S'} \u2192 R S S' \u2192 R' (eval S') (eval S)\n proof\u2082 r = proof\u2081 (sym r)\n\n data NF : Syn \u2192 Set where\n tt : NF tt\n var : (x : Var) \u2192 NF (var x)\n var_::_ : \u2200 {S}(i : Var) \u2192 NF S \u2192 NF (var i , S)\n\n record NFP S : Set where\n constructor _\u22a2_\n field\n {S'} : Syn\n term : NF S'\n proof : R S' S\n \n \n merge : \u2200 {S S'} \u2192 NF S \u2192 NF S' \u2192 NFP (S , S')\n merge tt n2 = n2 \u22a2 \u207b\u00b9\n merge (var i) n2 = (var i :: n2) \u22a2 id\n merge (var i :: n1) n2 with merge n1 n2\n ... | t \u22a2 p = (var i :: t) \u22a2 (\u27e8 id \u00d7 p \u27e9 \u223b assoc)\n\n norm : (x : Syn) \u2192 NFP x\n norm (var x) = (var x) \u22a2 id\n norm tt = tt \u22a2 id\n norm (x , x\u2081) with norm x | norm x\u2081\n ... | t1 \u22a2 p1 | t2 \u22a2 p2 with merge t1 t2\n ... | t3 \u22a2 p3 = t3 \u22a2 (p3 \u223b \u27e8 p1 \u00d7 p2 \u27e9)\n\n insert : \u2200 {S} \u2192 (x : Var) \u2192 NF S \u2192 NFP (var x , S)\n insert y tt = (var y) \u22a2 \u207b\u00b9\n insert y (var i) with (F.to\u2115 y) \u2115.\u2264? (F.to\u2115 i)\n ... | yes _ = (var y :: var i) \u22a2 id\n ... | no _ = (var i :: var y) \u22a2 swap\n insert y (var i :: n1) with (F.to\u2115 y) \u2115.\u2264? (F.to\u2115 i)\n ... | yes _ = (var y :: (var i :: n1)) \u22a2 id\n ... | no _ with insert y n1\n ... | t \u22a2 p = (var i :: t) \u22a2 (\u27e8 id \u00d7 p \u27e9 \u223b (assoc \u223b (\u27e8 swap \u00d7 id \u27e9 \u223b assoc\u207b\u00b9)))\n\n sort : \u2200 {x : Syn} \u2192 NF x \u2192 NFP x\n sort tt = tt \u22a2 id\n sort (var i) = var i \u22a2 id\n sort (var i :: n1) with sort n1\n ... | t1 \u22a2 p1 with insert i t1\n ... | t2 \u22a2 p2 = t2 \u22a2 (p2 \u223b \u27e8 id \u00d7 p1 \u27e9)\n\n normal : (x : Syn) \u2192 NFP x\n normal x with norm x\n ... | t1 \u22a2 p1 with sort t1\n ... | t2 \u22a2 p2 = t2 \u22a2 (p2 \u223b p1)\n\n open import Relation.Binary.PropositionalEquality using (_\u2261_ ; refl)\n open import Relation.Nullary\n\n import Data.Unit\n import Data.Empty\n\n id\u2261 : \u2200 {S S'} \u2192 S \u2261 S' \u2192 R S S'\n id\u2261 refl = id\n\n _\u2262_ : \u2200 {A : Set} \u2192 A \u2192 A \u2192 Set\n x \u2262 y = x \u2261 y \u2192 Data.Empty.\u22a5\n\n \u2262-cong : \u2200 {A B}{x y : A}(f : A \u2192 B) \u2192 f x \u2262 f y \u2192 x \u2262 y\n \u2262-cong f fr refl = fr refl\n\n var-inj : \u2200 {i j : Fin nrVars} \u2192 i \u2262 j \u2192 Syn.var i \u2262 var j\n var-inj p refl = p refl\n\n ,-inj\u2081 : \u2200 {x y a b} \u2192 x \u2262 y \u2192 (x Syn., a) \u2262 (y , b)\n ,-inj\u2081 p refl = p refl\n\n ,-inj\u2082 : \u2200 {x y a b} \u2192 a \u2262 b \u2192 (x Syn., a) \u2262 (y , b)\n ,-inj\u2082 p refl = p refl\n\n _\u225f_ : (x y : Syn) \u2192 Dec (x \u2261 y)\n var x \u225f var x\u2081 with x FP.\u225f x\u2081\n var .x\u2081 \u225f var x\u2081 | yes refl = yes refl\n ... | no p = no (var-inj p)\n var x \u225f tt = no (\u03bb ())\n var x \u225f (y , y\u2081) = no (\u03bb ())\n tt \u225f var x = no (\u03bb ())\n tt \u225f tt = yes refl\n tt \u225f (y , y\u2081) = no (\u03bb ())\n (x , x\u2081) \u225f var x\u2082 = no (\u03bb ())\n (x , x\u2081) \u225f tt = no (\u03bb ())\n (x , x\u2081) \u225f (y , y\u2081) with x \u225f y | x\u2081 \u225f y\u2081\n (x , x\u2081) \u225f (.x , .x\u2081) | yes refl | yes refl = yes refl\n (x , x\u2081) \u225f (y , y\u2081) | yes p | no \u00acp = no (,-inj\u2082 \u00acp)\n (x , x\u2081) \u225f (y , y\u2081) | no \u00acp | q = no (,-inj\u2081 \u00acp)\n\n CHECK : Syn \u2192 Syn \u2192 Set\n CHECK s1 s2 with s1 \u225f s2\n ... | yes p = Data.Unit.\u22a4\n ... | no p = Data.Empty.\u22a5\n\n rewire : (S\u2081 S\u2082 : Syn) \u2192 CHECK (NFP.S' (normal S\u2081)) (NFP.S' (normal S\u2082)) \u2192 R' (eval S\u2081) (eval S\u2082)\n rewire s\u2081 s\u2082 eq with NFP.S' (normal s\u2081) \u225f NFP.S' (normal s\u2082)\n ... | yes p = proof\u2081\n ((sym (NFP.proof (normal s\u2081)) \u223b id\u2261 p) \u223b NFP.proof (normal s\u2082))\n rewire _ _ () | no _\n -- proof\u2082 (NFP.proof (normal s\u2081)) \u223b' (eq \u223b' proof\u2081 (NFP.proof (normal s\u2082)))\n\n infix 4 _\u21db_\n\n record Eq : Set where\n constructor _\u21db_\n field\n LHS RHS : Syn\n\n open import Data.Vec.N-ary using (N-ary ; _$\u207f_)\n open import Data.Vec using (allFin) renaming (map to vmap)\n\n rewire' : (f : N-ary nrVars Syn Eq) \u2192 let (S\u2081 \u21db S\u2082) = f $\u207f (vmap Syn.var (allFin nrVars))\n in CHECK (NFP.S' (normal S\u2081)) (NFP.S' (normal S\u2082)) \u2192 R' (eval S\u2081) (eval S\u2082)\n rewire' f eq = let S \u21db S' = f $\u207f vmap Syn.var (allFin nrVars)\n in rewire S S' eq\n \nmodule example where\n\n open import Data.Vec\n\n open import Data.Product\n open import Data.Unit\n\n open import Function\n\n -- need to etaexpand this because otherwise we get an error\n module STest n M = Syntax Set _\u00d7_ \u22a4 (\u03bb x x\u2081 \u2192 x \u2192 x\u2081) (\u03bb x \u2192 x) \n (\u03bb x x\u2081 x\u2082 \u2192 x\u2081 (x x\u2082)) (\u03bb x \u2192 proj\u2081 x) (\u03bb x \u2192 x , tt) \n (\u03bb x \u2192 proj\u2082 x) (\u03bb x \u2192 tt , x) (\u03bb x x\u2081 x\u2082 \u2192 (x (proj\u2081 x\u2082)) , (x\u2081 (proj\u2082 x\u2082))) \n (\u03bb x x\u2081 \u2192 (x (proj\u2081 x\u2081)) , (proj\u2082 x\u2081)) (\u03bb x x\u2081 \u2192 (proj\u2081 x\u2081) , (x (proj\u2082 x\u2081))) \n (\u03bb x \u2192 ((proj\u2081 x) , (proj\u2081 (proj\u2082 x))) , (proj\u2082 (proj\u2082 x))) \n (\u03bb x \u2192 (proj\u2081 (proj\u2081 x)) , ((proj\u2082 (proj\u2081 x)) , (proj\u2082 x))) \n (\u03bb x \u2192 (proj\u2082 x) , (proj\u2081 x)) n M\n\n test : (A B C : Set) \u2192 (A \u00d7 B) \u00d7 C \u2192 (B \u00d7 A) \u00d7 C\n test A B C = rewire LHS RHS _ where\n open STest 3 (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n\n LHS = (# 0 , # 1) , # 2\n RHS = (# 1 , # 0) , # 2\n\n test2 : (A B C : Set) \u2192 (A \u00d7 B) \u00d7 C \u2192 (B \u00d7 A) \u00d7 C\n test2 A B C = rewire' (\u03bb a b c \u2192 (a , b) , c \u21db (b , a) , c) _ where\n open STest 3 (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n\n\nmodule example\u2082 where\n\n open import Data.Vec\n\n data Ty : Set where\n _\u00d7_ : Ty \u2192 Ty \u2192 Ty\n \u22a4 : Ty\n\n infix 4 _\u27f6_ \n\n data _\u27f6_ : Ty \u2192 Ty \u2192 Set where\n id' : \u2200 {A} \u2192 A \u27f6 A\n _\u223b'_ : \u2200 {A B C} \u2192 A \u27f6 B \u2192 B \u27f6 C \u2192 A \u27f6 C\n ' : \u2200 {A} \u2192 (A \u00d7 \u22a4) \u27f6 A\n \u207b\u00b9' : \u2200 {A} \u2192 A \u27f6 (A \u00d7 \u22a4)\n ' : \u2200 {A} \u2192 (\u22a4 \u00d7 A) \u27f6 A\n \u207b\u00b9' : \u2200 {A} \u2192 A \u27f6 (\u22a4 \u00d7 A)\n \u27e8_\u00d7'_\u27e9 : \u2200 {A B C D} \u2192 A \u27f6 C \u2192 B \u27f6 D \u2192 (A \u00d7 B) \u27f6 (C \u00d7 D)\n first : \u2200 {A B C} \u2192 A \u27f6 B \u2192 A \u00d7 C \u27f6 B \u00d7 C\n second : \u2200 {A B C} \u2192 B \u27f6 C \u2192 A \u00d7 B \u27f6 A \u00d7 C \n assoc' : \u2200 {A B C} \u2192 (A \u00d7 (B \u00d7 C)) \u27f6 ((A \u00d7 B) \u00d7 C)\n assoc\u207b\u00b9' : \u2200 {A B C} \u2192 ((A \u00d7 B) \u00d7 C) \u27f6 (A \u00d7 (B \u00d7 C))\n swap' : \u2200 {A B} \u2192 (A \u00d7 B) \u27f6 (B \u00d7 A)\n \n\n module STest n M = Syntax Ty _\u00d7_ \u22a4 _\u27f6_ id' _\u223b'_ ' \u207b\u00b9' ' \u207b\u00b9' \u27e8_\u00d7'_\u27e9 first second assoc' assoc\u207b\u00b9' swap' n M\n\n test2 : (A B C : Ty) \u2192 (A \u00d7 B) \u00d7 C \u27f6 (B \u00d7 A) \u00d7 C\n test2 A B C = rewire ((# 0 , # 1) , # 2) ((# 1 , # 0) , # 2) _ where\n open STest 3 (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n\n\nmodule example\u2083 where\n\n open import Data.Unit\n open import Data.Product\n open import Data.Vec\n\n open import Function using (flip ; const)\n \n open import Function.Inverse\n open import Function.Related.TypeIsomorphisms.NP\n\n open \u00d7-CMon using () renaming (\u2219-cong to \u00d7-cong ; assoc to \u00d7-assoc)\n\n module STest n M = Syntax Set _\u00d7_ \u22a4 _\u2194_ id (flip _\u2218_) A\u00d7\u22a4\u2194A (sym A\u00d7\u22a4\u2194A) (A\u00d7\u22a4\u2194A \u2218 swap-iso) (swap-iso \u2218 sym A\u00d7\u22a4\u2194A)\n \u00d7-cong first-iso (\u03bb x \u2192 second-iso (const x))\n (sym (\u00d7-assoc _ _ _)) (\u00d7-assoc _ _ _) swap-iso n M\n\n test : \u2200 A B C \u2192 ((A \u00d7 B) \u00d7 C) \u2194 (C \u00d7 (B \u00d7 A))\n test A B C = rewire ((# 0 , # 1) , # 2) (# 2 , (# 1 , # 0)) _ where\n open STest 3 (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n\n","old_contents":"module linear-solver where\n\nopen import Data.Nat as \u2115 using (\u2115)\nopen import Data.Fin as F using (Fin)\nimport Data.Fin.Props as FP\n\nmodule Syntax {a} (A : Set a)(_x_ : A \u2192 A \u2192 A)(T : A)\n (R' : A \u2192 A \u2192 Set)\n (id' : \u2200 {A} \u2192 R' A A)\n (_\u223b'_ : \u2200 {A B C} \u2192 R' A B \u2192 R' B C \u2192 R' A C)\n (' : \u2200 {A} \u2192 R' (A x T) A)\n (\u207b\u00b9' : \u2200 {A} \u2192 R' A (A x T))\n (' : \u2200 {A} \u2192 R' (T x A) A)\n (\u207b\u00b9' : \u2200 {A} \u2192 R' A (T x A))\n (\u27e8_\u00d7'_\u27e9 : \u2200 {A B C D} \u2192 R' A C \u2192 R' B D \u2192 R' (A x B) (C x D))\n (first' : \u2200 {A B C} \u2192 R' A B \u2192 R' (A x C) (B x C))\n (second' : \u2200 {A B C} \u2192 R' B C \u2192 R' (A x B) (A x C))\n (assoc' : \u2200 {A B C} \u2192 R' (A x (B x C)) ((A x B) x C))\n (assoc\u207b\u00b9' : \u2200 {A B C} \u2192 R' ((A x B) x C) (A x (B x C)))\n (swap' : \u2200 {A B} \u2192 R' (A x B) (B x A))\n nrVars (!_ : Fin nrVars \u2192 A) where\n\n Var = Fin nrVars\n\n open import Relation.Nullary using (yes ; no)\n open import Relation.Nullary.Decidable\n\n data Syn : Set where\n var : Var \u2192 Syn\n tt : Syn\n _,_ : Syn \u2192 Syn \u2192 Syn\n\n #_ : \u2200 m {m : \u2200 {A} \u2192 R (A , tt) A\n : \u2200 {A} \u2192 R (tt , A) A\n \u207b\u00b9 : \u2200 {A} \u2192 R A (tt , A)\n \u207b\u00b9 : \u2200 {A} \u2192 R A (A , tt)\n \u27e8_\u00d7''_\u27e9 : \u2200 {A B C D} \u2192 R A C \u2192 R B D \u2192 R (A , B) (C , D)\n assoc : \u2200 {A B C} \u2192 R (A , (B , C)) ((A , B) , C)\n assoc\u207b\u00b9 : \u2200 {A B C} \u2192 R ((A , B) , C) (A , (B , C))\n id : \u2200 {A} \u2192 R A A\n swap : \u2200 {A B} \u2192 R (A , B) (B , A)\n\n \u27e8_\u00d7_\u27e9 : \u2200 {A B C D} \u2192 R A C \u2192 R B D \u2192 R (A , B) (C , D)\n \u27e8 id \u00d7 id \u27e9 = id\n \u27e8 r\u2081 \u00d7 r\u2082 \u27e9 = \u27e8 r\u2081 \u00d7'' r\u2082 \u27e9\n\n _\u223b_ : \u2200 {A B C} \u2192 R A B \u2192 R B C \u2192 R A C\n id \u223b r\u2082 = r\u2082\n r\u2081 \u223b id = r\u2081\n \u207b\u00b9 \u223b = id\n \u207b\u00b9 \u223b = id\n \u223b \u207b\u00b9 = id\n \u223b \u207b\u00b9 = id\n swap \u223b = \n swap \u223b = \n \u207b\u00b9 \u223b swap = \u207b\u00b9\n \u207b\u00b9 \u223b swap = \u207b\u00b9\n assoc \u223b assoc\u207b\u00b9 = id\n assoc \u223b (assoc\u207b\u00b9 \u223b'' r) = r\n assoc\u207b\u00b9 \u223b assoc = id\n assoc\u207b\u00b9 \u223b (assoc \u223b'' r) = r\n swap \u223b swap = id\n swap \u223b (swap \u223b'' r) = r\n (r\u2081 \u223b'' r\u2082) \u223b r\u2083 = r\u2081 \u223b (r\u2082 \u223b r\u2083)\n \u27e8 r\u2081 \u00d7'' r\u2082 \u27e9 \u223b \u27e8 r\u2083 \u00d7'' r\u2084 \u27e9 = \u27e8 r\u2081 \u223b r\u2083 \u00d7 r\u2082 \u223b r\u2084 \u27e9\n \u27e8 r\u2081 \u00d7'' r\u2082 \u27e9 \u223b (\u27e8 r\u2083 \u00d7'' r\u2084 \u27e9 \u223b'' r\u2085) with \u27e8 r\u2081 \u223b r\u2083 \u00d7 r\u2082 \u223b r\u2084 \u27e9\n ... |\u00a0id = r\u2085\n ... |\u00a0r\u2086 = r\u2086 \u223b'' r\u2085\n r\u2081 \u223b r\u2082 = r\u2081 \u223b'' r\u2082 \n\n sym : \u2200 {S S'} \u2192 R S S' \u2192 R S' S\n sym (r \u223b'' r\u2081) = sym r\u2081 \u223b sym r\n sym = \u207b\u00b9\n sym = \u207b\u00b9\n sym \u207b\u00b9 = \n sym \u207b\u00b9 = \n sym \u27e8 r \u00d7'' r\u2081 \u27e9 = \u27e8 sym r \u00d7 sym r\u2081 \u27e9\n sym assoc = assoc\u207b\u00b9\n sym assoc\u207b\u00b9 = assoc\n sym id = id\n sym swap = swap\n\n proof\u2081 : \u2200 {S S'} \u2192 R S S' \u2192 R' (eval S) (eval S')\n proof\u2081 (r \u223b'' r\u2081) = proof\u2081 r \u223b' proof\u2081 r\u2081\n proof\u2081 = '\n proof\u2081 = '\n proof\u2081 \u207b\u00b9 = \u207b\u00b9'\n proof\u2081 \u207b\u00b9 = \u207b\u00b9'\n proof\u2081 \u27e8 id \u00d7'' r \u27e9 = second' (proof\u2081 r)\n proof\u2081 \u27e8 r \u00d7'' id \u27e9 = first' (proof\u2081 r)\n proof\u2081 \u27e8 r \u00d7'' r\u2081 \u27e9 = \u27e8 proof\u2081 r \u00d7' proof\u2081 r\u2081 \u27e9\n proof\u2081 assoc = assoc'\n proof\u2081 assoc\u207b\u00b9 = assoc\u207b\u00b9'\n proof\u2081 id = id'\n proof\u2081 swap = swap'\n\n proof\u2082 : \u2200 {S S'} \u2192 R S S' \u2192 R' (eval S') (eval S)\n proof\u2082 r = proof\u2081 (sym r)\n\n data NF : Syn \u2192 Set where\n tt : NF tt\n var : (x : Var) \u2192 NF (var x)\n var_::_ : \u2200 {S}(i : Var) \u2192 NF S \u2192 NF (var i , S)\n\n record NFP S : Set where\n constructor _\u22a2_\n field\n {S'} : Syn\n term : NF S'\n proof : R S' S\n \n \n merge : \u2200 {S S'} \u2192 NF S \u2192 NF S' \u2192 NFP (S , S')\n merge tt n2 = n2 \u22a2 \u207b\u00b9\n merge (var i) n2 = (var i :: n2) \u22a2 id\n merge (var i :: n1) n2 with merge n1 n2\n ... | t \u22a2 p = (var i :: t) \u22a2 (\u27e8 id \u00d7 p \u27e9 \u223b assoc)\n\n norm : (x : Syn) \u2192 NFP x\n norm (var x) = (var x) \u22a2 id\n norm tt = tt \u22a2 id\n norm (x , x\u2081) with norm x | norm x\u2081\n ... | t1 \u22a2 p1 | t2 \u22a2 p2 with merge t1 t2\n ... | t3 \u22a2 p3 = t3 \u22a2 (p3 \u223b \u27e8 p1 \u00d7 p2 \u27e9)\n\n insert : \u2200 {S} \u2192 (x : Var) \u2192 NF S \u2192 NFP (var x , S)\n insert y tt = (var y) \u22a2 \u207b\u00b9\n insert y (var i) with (F.to\u2115 y) \u2115.\u2264? (F.to\u2115 i)\n ... | yes _ = (var y :: var i) \u22a2 id\n ... | no _ = (var i :: var y) \u22a2 swap\n insert y (var i :: n1) with (F.to\u2115 y) \u2115.\u2264? (F.to\u2115 i)\n ... | yes _ = (var y :: (var i :: n1)) \u22a2 id\n ... | no _ with insert y n1\n ... | t \u22a2 p = (var i :: t) \u22a2 (\u27e8 id \u00d7 p \u27e9 \u223b (assoc \u223b (\u27e8 swap \u00d7 id \u27e9 \u223b assoc\u207b\u00b9)))\n\n sort : \u2200 {x : Syn} \u2192 NF x \u2192 NFP x\n sort tt = tt \u22a2 id\n sort (var i) = var i \u22a2 id\n sort (var i :: n1) with sort n1\n ... | t1 \u22a2 p1 with insert i t1\n ... | t2 \u22a2 p2 = t2 \u22a2 (p2 \u223b \u27e8 id \u00d7 p1 \u27e9)\n\n normal : (x : Syn) \u2192 NFP x\n normal x with norm x\n ... | t1 \u22a2 p1 with sort t1\n ... | t2 \u22a2 p2 = t2 \u22a2 (p2 \u223b p1)\n\n open import Relation.Binary.PropositionalEquality using (_\u2261_ ; refl)\n open import Relation.Nullary\n\n import Data.Unit\n import Data.Empty\n\n id\u2261 : \u2200 {S S'} \u2192 S \u2261 S' \u2192 R S S'\n id\u2261 refl = id\n\n _\u2262_ : \u2200 {A : Set} \u2192 A \u2192 A \u2192 Set\n x \u2262 y = x \u2261 y \u2192 Data.Empty.\u22a5\n\n \u2262-cong : \u2200 {A B}{x y : A}(f : A \u2192 B) \u2192 f x \u2262 f y \u2192 x \u2262 y\n \u2262-cong f fr refl = fr refl\n\n var-inj : \u2200 {i j : Fin nrVars} \u2192 i \u2262 j \u2192 Syn.var i \u2262 var j\n var-inj p refl = p refl\n\n ,-inj\u2081 : \u2200 {x y a b} \u2192 x \u2262 y \u2192 (x Syn., a) \u2262 (y , b)\n ,-inj\u2081 p refl = p refl\n\n ,-inj\u2082 : \u2200 {x y a b} \u2192 a \u2262 b \u2192 (x Syn., a) \u2262 (y , b)\n ,-inj\u2082 p refl = p refl\n\n _\u225f_ : (x y : Syn) \u2192 Dec (x \u2261 y)\n var x \u225f var x\u2081 with x FP.\u225f x\u2081\n var .x\u2081 \u225f var x\u2081 | yes refl = yes refl\n ... | no p = no (var-inj p)\n var x \u225f tt = no (\u03bb ())\n var x \u225f (y , y\u2081) = no (\u03bb ())\n tt \u225f var x = no (\u03bb ())\n tt \u225f tt = yes refl\n tt \u225f (y , y\u2081) = no (\u03bb ())\n (x , x\u2081) \u225f var x\u2082 = no (\u03bb ())\n (x , x\u2081) \u225f tt = no (\u03bb ())\n (x , x\u2081) \u225f (y , y\u2081) with x \u225f y | x\u2081 \u225f y\u2081\n (x , x\u2081) \u225f (.x , .x\u2081) | yes refl | yes refl = yes refl\n (x , x\u2081) \u225f (y , y\u2081) | yes p | no \u00acp = no (,-inj\u2082 \u00acp)\n (x , x\u2081) \u225f (y , y\u2081) | no \u00acp | q = no (,-inj\u2081 \u00acp)\n\n CHECK : Syn \u2192 Syn \u2192 Set\n CHECK s1 s2 with s1 \u225f s2\n ... | yes p = Data.Unit.\u22a4\n ... | no p = Data.Empty.\u22a5\n\n rewire : (S\u2081 S\u2082 : Syn) \u2192 CHECK (NFP.S' (normal S\u2081)) (NFP.S' (normal S\u2082)) \u2192 R' (eval S\u2081) (eval S\u2082)\n rewire s\u2081 s\u2082 eq with NFP.S' (normal s\u2081) \u225f NFP.S' (normal s\u2082)\n ... | yes p = proof\u2081\n ((sym (NFP.proof (normal s\u2081)) \u223b id\u2261 p) \u223b NFP.proof (normal s\u2082))\n rewire _ _ () | no _\n -- proof\u2082 (NFP.proof (normal s\u2081)) \u223b' (eq \u223b' proof\u2081 (NFP.proof (normal s\u2082)))\n\n infix 4 _\u21db_\n\n record Eq : Set where\n constructor _\u21db_\n field\n LHS RHS : Syn\n\n open import Data.Vec.N-ary using (N-ary ; _$\u207f_)\n open import Data.Vec using (allFin) renaming (map to vmap)\n\n rewire' : (f : N-ary nrVars Syn Eq) \u2192 let (S\u2081 \u21db S\u2082) = f $\u207f (vmap Syn.var (allFin nrVars))\n in CHECK (NFP.S' (normal S\u2081)) (NFP.S' (normal S\u2082)) \u2192 R' (eval S\u2081) (eval S\u2082)\n rewire' f eq = let S \u21db S' = f $\u207f vmap Syn.var (allFin nrVars)\n in rewire S S' eq\n \nmodule example where\n\n open import Data.Vec\n\n open import Data.Product\n open import Data.Unit\n\n open import Function\n\n -- need to etaexpand this because otherwise we get an error\n module STest n M = Syntax Set _\u00d7_ \u22a4 (\u03bb x x\u2081 \u2192 x \u2192 x\u2081) (\u03bb x \u2192 x) \n (\u03bb x x\u2081 x\u2082 \u2192 x\u2081 (x x\u2082)) (\u03bb x \u2192 proj\u2081 x) (\u03bb x \u2192 x , tt) \n (\u03bb x \u2192 proj\u2082 x) (\u03bb x \u2192 tt , x) (\u03bb x x\u2081 x\u2082 \u2192 (x (proj\u2081 x\u2082)) , (x\u2081 (proj\u2082 x\u2082))) \n (\u03bb x x\u2081 \u2192 (x (proj\u2081 x\u2081)) , (proj\u2082 x\u2081)) (\u03bb x x\u2081 \u2192 (proj\u2081 x\u2081) , (x (proj\u2082 x\u2081))) \n (\u03bb x \u2192 ((proj\u2081 x) , (proj\u2081 (proj\u2082 x))) , (proj\u2082 (proj\u2082 x))) \n (\u03bb x \u2192 (proj\u2081 (proj\u2081 x)) , ((proj\u2082 (proj\u2081 x)) , (proj\u2082 x))) \n (\u03bb x \u2192 (proj\u2082 x) , (proj\u2081 x)) n M\n\n test : (A B C : Set) \u2192 (A \u00d7 B) \u00d7 C \u2192 (B \u00d7 A) \u00d7 C\n test A B C = rewire LHS RHS _ where\n open STest 3 (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n\n LHS = (# 0 , # 1) , # 2\n RHS = (# 1 , # 0) , # 2\n\n test2 : (A B C : Set) \u2192 (A \u00d7 B) \u00d7 C \u2192 (B \u00d7 A) \u00d7 C\n test2 A B C = rewire' (\u03bb a b c \u2192 (a , b) , c \u21db (b , a) , c) _ where\n open STest 3 (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n\n\nmodule example\u2082 where\n\n open import Data.Vec\n\n data Ty : Set where\n _\u00d7_ : Ty \u2192 Ty \u2192 Ty\n \u22a4 : Ty\n\n infix 4 _\u27f6_ \n\n data _\u27f6_ : Ty \u2192 Ty \u2192 Set where\n id' : \u2200 {A} \u2192 A \u27f6 A\n _\u223b'_ : \u2200 {A B C} \u2192 A \u27f6 B \u2192 B \u27f6 C \u2192 A \u27f6 C\n ' : \u2200 {A} \u2192 (A \u00d7 \u22a4) \u27f6 A\n \u207b\u00b9' : \u2200 {A} \u2192 A \u27f6 (A \u00d7 \u22a4)\n ' : \u2200 {A} \u2192 (\u22a4 \u00d7 A) \u27f6 A\n \u207b\u00b9' : \u2200 {A} \u2192 A \u27f6 (\u22a4 \u00d7 A)\n \u27e8_\u00d7'_\u27e9 : \u2200 {A B C D} \u2192 A \u27f6 C \u2192 B \u27f6 D \u2192 (A \u00d7 B) \u27f6 (C \u00d7 D)\n first : \u2200 {A B C} \u2192 A \u27f6 B \u2192 A \u00d7 C \u27f6 B \u00d7 C\n second : \u2200 {A B C} \u2192 B \u27f6 C \u2192 A \u00d7 B \u27f6 A \u00d7 C \n assoc' : \u2200 {A B C} \u2192 (A \u00d7 (B \u00d7 C)) \u27f6 ((A \u00d7 B) \u00d7 C)\n assoc\u207b\u00b9' : \u2200 {A B C} \u2192 ((A \u00d7 B) \u00d7 C) \u27f6 (A \u00d7 (B \u00d7 C))\n swap' : \u2200 {A B} \u2192 (A \u00d7 B) \u27f6 (B \u00d7 A)\n \n\n module STest n M = Syntax Ty _\u00d7_ \u22a4 _\u27f6_ id' _\u223b'_ ' \u207b\u00b9' ' \u207b\u00b9' \u27e8_\u00d7'_\u27e9 first second assoc' assoc\u207b\u00b9' swap' n M\n\n test2 : (A B C : Ty) \u2192 (A \u00d7 B) \u00d7 C \u27f6 (B \u00d7 A) \u00d7 C\n test2 A B C = rewire ((# 0 , # 1) , # 2) ((# 1 , # 0) , # 2) _ where\n open STest 3 (\u03bb i \u2192 lookup i (A \u2237 B \u2237 C \u2237 []))\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"d3b3af37e2a3db45c5989097d40b354cd10cca92","subject":"Cosmetic change.","message":"Cosmetic change.\n\nIgnore-this: 56f256a7d0c9d89dea0c836aed175f19\n\ndarcs-hash:20120303031914-3bd4e-94a323682a68c7fa19333fcfea1528dc4853b741.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/PA\/README.agda","new_file":"src\/PA\/README.agda","new_contents":"------------------------------------------------------------------------------\n-- First-order Peano arithmetic (PA)\n------------------------------------------------------------------------------\n\nmodule PA.README where\n\n-- Two formalizations of first-order Peano aritmetic using:\n\n-- \u2022 Agda postulates for the non-logical constants and the Peano's\n-- axioms, using axioms based on the propositional equality (see, for\n-- example, [Machover, 1996, p. 263], [H\u00e1jek and Pudl\u00e1k, 1998, p. 28]).\n\n-- \u2022 Agda data types and primitive recursive functions for addition and\n-- multiplication.\n\n-- References;\n\n-- Mosh\u00e9 Machover. Set theory, logic and their limitations. Cambridge\n-- University Press, 1996.\n\n-- Petr H\u00e1jek and Pavel Pudl\u00e1k. Metamathematics of First-Order\n-- Arithmetic. Springer, 1998. 2nd printing.\n\n------------------------------------------------------------------------------\n-- Axiomatic PA\n\n-- The axioms\nopen import PA.Axiomatic.Standard.Base\n\n-- Some properties\nopen import PA.Axiomatic.Standard.PropertiesATP\nopen import PA.Axiomatic.Standard.PropertiesI\n\n------------------------------------------------------------------------------\n-- Inductive PA\n\n-- Inductive definitions\nopen import PA.Inductive.Base\n\n-- Some properties\nopen import PA.Inductive.Properties\nopen import PA.Inductive.PropertiesATP\nopen import PA.Inductive.PropertiesI\n\nopen import PA.Inductive.PropertiesByInduction\nopen import PA.Inductive.PropertiesByInductionATP\nopen import PA.Inductive.PropertiesByInductionI\n","old_contents":"------------------------------------------------------------------------------\n-- First-order Peano arithmetic (PA)\n------------------------------------------------------------------------------\n\nmodule PA.README where\n\n-- Two formalizations of first-order Peano aritmetic using:\n\n-- * Agda postulates for the non-logical constants and the Peano's\n-- axioms, using axioms based on the propositional equality (see, for\n-- example, [Machover, 1996, p. 263], [H\u00e1jek and Pudl\u00e1k, 1998, p. 28]).\n\n-- * Agda data types and primitive recursive functions for addition and\n-- multiplication.\n\n-- References;\n\n-- Mosh\u00e9 Machover. Set theory, logic and their limitations. Cambridge\n-- University Press, 1996.\n\n-- Petr H\u00e1jek and Pavel Pudl\u00e1k. Metamathematics of First-Order\n-- Arithmetic. Springer, 1998. 2nd printing.\n\n------------------------------------------------------------------------------\n-- Axiomatic PA\n\n-- The axioms\nopen import PA.Axiomatic.Standard.Base\n\n-- Some properties\nopen import PA.Axiomatic.Standard.PropertiesATP\nopen import PA.Axiomatic.Standard.PropertiesI\n\n------------------------------------------------------------------------------\n-- Inductive PA\n\n-- Inductive definitions\nopen import PA.Inductive.Base\n\n-- Some properties\nopen import PA.Inductive.Properties\nopen import PA.Inductive.PropertiesATP\nopen import PA.Inductive.PropertiesI\n\nopen import PA.Inductive.PropertiesByInduction\nopen import PA.Inductive.PropertiesByInductionATP\nopen import PA.Inductive.PropertiesByInductionI\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a1572f3d4a91267b68f0d9fae0ab866d4c0337e1","subject":"Syntactic sugar for replacement pairs","message":"Syntactic sugar for replacement pairs\n\nOld-commit-hash: 930ad8e0b6ee020c54d0c8372373e433fb184bad\n","repos":"inc-lc\/ilc-agda","old_file":"nats\/Extension-00.agda","new_file":"nats\/Extension-00.agda","new_contents":"{-\n\nChecklist of stuff to add when adding syntactic constructs\n\n- derive (symbolic derivation; most important!)\n- weaken\n- \u27e6_\u27e7Term\n- weaken-sound\n- validity-of-derive\n- correctness-of-derive\n\n-}\n\nmodule Extension-00 where\n\nopen import Data.Product\nopen import Data.Nat using (\u2115)\nopen import Data.Unit using (\u22a4)\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Level using\n (zero ; Level ; suc)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\npostulate extensionality : Extensionality zero zero\n\ndata Type : Set where\n nats : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n\u27e8\u2205\u27e9 : \u2205 \u2261 \u2205\n\u27e8\u2205\u27e9 = refl\n\n_\u27e8\u2022\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} \u2192 \u03c4\u2081 \u2261 \u03c4\u2082 \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u03c4\u2081 \u2022 \u0393\u2081 \u2261 \u03c4\u2082 \u2022 \u0393\u2082\n_\u27e8\u2022\u27e9_ = cong\u2082 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n -- ad-hoc extensions\n foldNat : \u2200 {\u0393 \u03c4} \u2192 (n : Term \u0393 nats) \u2192\n (f : Term \u0393 (\u03c4 \u21d2 \u03c4)) \u2192 (z : Term \u0393 \u03c4)\n \u2192 Term \u0393 \u03c4\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u227c-reflexivity : Reflexive _\u227c_\n\u227c-reflexivity {\u2205} = \u2205\u227c\u2205\n\u227c-reflexivity {\u03c4 \u2022 x} = keep \u03c4 \u2022 \u227c-reflexivity\n\n\u227c-reflexive : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u0393\u2081 \u227c \u0393\u2082\n\u227c-reflexive refl = \u227c-reflexivity\n\n\u0393\u227c\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0393\n\u0393\u227c\u0393 = \u227c-reflexive refl\n\n\u227c-transitive : Transitive _\u227c_\n\u227c-transitive \u2205\u227c\u2205 rel1 = rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n keep \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (keep \u03c4 \u2022 rel0) rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n drop \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (drop \u03c4 \u2022 rel0) rel1\n\n\u227c-isPreorder : IsPreorder _\u2261_ _\u227c_\n\u227c-isPreorder = record\n { isEquivalence = isEquivalence\n ; reflexive = \u227c-reflexive\n ; trans = \u227c-transitive\n }\n\n\u227c-preorder : Preorder _ _ _\n\u227c-preorder = record\n { Carrier = Context\n ; _\u2248_ = _\u2261_\n ; isPreorder = \u227c-isPreorder\n }\n\nmodule \u227c-reasoning where\n open import Relation.Binary.PreorderReasoning \u227c-preorder public\n renaming\n ( _\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_\n ; _\u223c\u27e8_\u27e9_ to _\u227c\u27e8_\u27e9_\n ; _\u2248\u27e8\u27e9_ to _\u2261\u27e8\u27e9_\n )\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\n\nweaken subctx (foldNat n f z) = {!!}\n\nrecord Meaning (Syntax : Set) {\u2113 : Level} : Set (suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 nats \u27e7Type = \u2115\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\n\u27e6 foldNat n f z \u27e7Term \u03c1 = {!!}\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = cong\u2082 (\u03bb x y \u2192 x y)\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = \u2261-app (weaken-sound t\u2081 \u03c1) (weaken-sound t\u2082 \u03c1)\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (foldNat n f z) \u03c1 = {!!}\n\n-- Changes to \u2115 are replacement Church pairs. The only arguments\n-- of conern are `fst` and `snd`, so the Church pairs don't have\n-- to be polymorphic.\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type nats = (nats \u21d2 nats \u21d2 nats) \u21d2 nats\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\n-- Combination as type-indexed family of terms\n-- _\u2295_ : \u2200 {\u03c4 \u0393} \u2192 TODO: IMPLEMENT ME!! {!!}\n\n-- Replacement-pairs on all types as syntactic sugar\n\nreplace_by_ : \u2200 {\u03c4 \u0393} \u2192 (old : Term \u0393 \u03c4) (new : Term \u0393 \u03c4)\n \u2192 Term \u0393 (\u0394-Type \u03c4)\n\n-- replace n by m = \u03bb f . f n m\n-- replace f by g = \u03bb x . \u03bb dx . replace (f x) by (g (x \u2295 dx))\n--\n-- Remark. Amazingly, weakening is identical to arbitrary\n-- legal adjustment of de-Bruijn indices.\nreplace_by_ {nats} {\u0393} old new =\n abs (app (app (var this) (weaken drop-f old)) (weaken drop-f new))\n where drop-f = drop _ \u2022 \u0393\u227c\u0393\n-- Think: the new must compute upon changes...\nreplace_by_ {\u03c4\u2081 \u21d2 \u03c4\u2082} old new =\n abs (abs (replace (app (weaken drop! old) (var (that this)))\n by (app (weaken drop! new) {!!})))\n where drop! = drop (\u0394-Type \u03c4\u2081) \u2022 drop \u03c4\u2081 \u2022 \u0393\u227c\u0393\n\n\n-- It is clear that \u27e6\u229d\u27e7 exists on the semantic level:\n-- there exists an Agda value to describe the change between any\n-- two Agda values denoted by terms. If we have (not dependently-\n-- typed) arrays, no term denotes the change between two arrays\n-- of different lengths. Thus no full abstraction.\n\n\u27e6derive\u27e7 : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ninfixl 6 _\u27e6\u2295\u27e7_\ninfixl 6 _\u27e6\u229d\u27e7_\n\n\u27e6fst\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e6snd\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\n\u27e6derive\u27e7 {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 f (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f v\n\u27e6derive\u27e7 {nats} n = \u03bb f \u2192 f n n\n\n_\u27e6\u229d\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 f\u2081 (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f\u2082 v\n_\u27e6\u229d\u27e7_ {nats} m n = \u03bb f \u2192 f n m\n-- m \u27e6\u229d\u27e7 n ::= replace n by m\n\n_\u27e6\u2295\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n_\u27e6\u2295\u27e7_ {nats} n dn = dn \u27e6snd\u27e7\n\n\u27e6fst\u27e7 m n = m\n\u27e6snd\u27e7 m n = n\n\n-- Strong validity!\ndata \u27e6Valid\u0394\u27e7 : {\u03c4 : Type} \u2192 (v : \u27e6 \u03c4 \u27e7) \u2192 (dv : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n -- Following Pierce's case names `T-Var`, `T-Abs`, `T-App`\n -- with Agda's capitalization convention\n -- generalized to the semantic (value) domain\n v-nat : (n : \u2115) \u2192 (dn : (\u2115 \u2192 \u2115 \u2192 \u2115) \u2192 \u2115) \u2192\n n \u2261 dn \u27e6fst\u27e7 \u2192\n \u27e6Valid\u0394\u27e7 n dn\n\n v-fun : {\u03c4\u2081 \u03c4\u2082 : Type} \u2192\n (f : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) \u2192 (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) \u2192\n -- A strong antecedent: f and df map invalid changes to\n -- valid changes! So long as invalid changes exist,\n -- this is NOT satisfied by\n --\n -- f = \u27e6 \u03bb x. x \u27e7 = identity\n -- df = \u27e6 \u03bb x dx. dx \u27e7 = \u27e6snd\u27e7,\n --\n -- negating any hope of \u27e6Valid\u0394\u27e7 \u27e6 t \u27e7 \u27e6 derive t \u27e7.\n (\u2200 {s ds} \u2192 {-Valid\u0394 s ds \u2192-} \u27e6Valid\u0394\u27e7 (f s) (df s ds)) \u2192\n (\u2200 {s ds} \u2192 (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 (f s \u27e6\u2295\u27e7 df s ds)) \u2192\n \u27e6Valid\u0394\u27e7 f df\n\nabsurdity-of-0=1 : 0 \u2261 1 \u2192 (\u2200 {A : Set} \u2192 A)\nabsurdity-of-0=1 ()\n\nit-is-absurd-that : \u27e6Valid\u0394\u27e7 {nats} 0 (\u03bb f \u2192 f 1 1) \u2192 (\u2200 {A : Set} \u2192 A)\nit-is-absurd-that (v-nat .0 .(\u03bb f \u2192 f 1 1) 0=1) = absurdity-of-0=1 0=1\n\n-- If (\u03bb x dx \u2192 dx) is a \u27e6Valid\u0394\u27e7 to (\u03bb x \u2192 x),\n-- then impossible is nothing.\nno-way : \u27e6Valid\u0394\u27e7 {nats \u21d2 nats} (\u03bb x \u2192 x) (\u03bb x dx \u2192 dx) \u2192\n (\u2200 {A : Set} \u2192 A)\nno-way (v-fun .(\u03bb x \u2192 x) .(\u03bb x dx \u2192 dx) validity correctness) =\n it-is-absurd-that (validity {0} {\u03bb f \u2192 f 1 1})\n\n-- Cool lemmas\n\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u229d\u27e7 f \u2261 \u27e6derive\u27e7 f\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {nats} f = refl\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = refl\n\nf\u2295[g\u229df]=g : \u2200 {\u03c4 : Type} (f g : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) \u2261 g\n\nf\u2295\u0394f=f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (\u27e6derive\u27e7 f) \u2261 f\n\nf\u2295[g\u229df]=g {nats} m n = refl\nf\u2295[g\u229df]=g {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = f x} {y = f x} refl\n (cong\u2082 _\u27e6\u229d\u27e7_ (cong g (f\u2295\u0394f=f x)) refl) \u27e9\n f x \u27e6\u2295\u27e7 (g x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (g x) \u27e9\n g x\n \u220e\n ) where open \u2261-Reasoning\n\nf\u2295\u0394f=f {nats} n = refl\nf\u2295\u0394f=f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 \u27e6derive\u27e7 f) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (f (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong (\u03bb hole \u2192 f x \u27e6\u2295\u27e7 (f hole \u27e6\u229d\u27e7 f x)) (f\u2295\u0394f=f x) \u27e9\n f x \u27e6\u2295\u27e7 (f x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (f x) \u27e9\n f x\n \u220e\n ) where open \u2261-Reasoning\n\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {nats} n dn = n \u2261 dn \u27e6fst\u27e7\nvalid-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (s : \u27e6 \u03c4\u2081 \u27e7) (ds : \u27e6 \u0394-Type \u03c4\u2081 \u27e7) (R[s,ds] : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7 -- (valid-\u0394:1)\n (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 f s \u27e6\u2295\u27e7 df s ds -- (valid-\u0394:2)\n\nR[f,g\u229df] : \u2200 {\u03c4} (f g : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (g \u27e6\u229d\u27e7 f)\nR[f,g\u229df] {nats} m n = refl\nR[f,g\u229df] {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb x dx R[x,dx] \u2192\n R[f,g\u229df] {\u03c4\u2082} (f x) (g (x \u27e6\u2295\u27e7 dx)) -- (valid-\u0394:1)\n , -- tuple constructor\n (begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 refl \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7\n (g ((x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 \u27e6derive\u27e7 (x \u27e6\u2295\u27e7 dx)) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 cong (\u03bb hole \u2192 f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g hole \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx)))\n (f\u2295\u0394f=f (x \u27e6\u2295\u27e7 dx)) \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 f\u2295[g\u229df]=g (f (x \u27e6\u2295\u27e7 dx)) (g (x \u27e6\u2295\u27e7 dx)) \u27e9\n g (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (f x) (g (x \u27e6\u2295\u27e7 dx))) \u27e9\n f x \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) x dx\n \u220e)\n where open \u2261-Reasoning\n\nR[f,\u0394f] : \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (\u27e6derive\u27e7 f)\nR[f,\u0394f] f rewrite sym (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f f) = R[f,g\u229df] f f\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393 -- push \u03c4, then push \u0394\u03c4.\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 (keep \u03c4 \u2022 \u0393\u227c\u0394\u0393)\n\n-- Data type to hold proofs that environments are valid\ndata Valid-\u0394env : {\u0393 : Context} (\u03c1 : \u27e6 \u0393 \u27e7) (\u0394\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is its own valid \u0394-environment.\n \u03c1=\u2205 : Valid-\u0394env {\u2205} \u2205 \u2205\n -- Induction case: the change introduced therein should be valid,\n -- and the smaller \u0394-environment should be valid as well.\n \u03c1=v\u2022\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {\u03c1\u2080 : \u27e6 \u0393\u2080 \u27e7} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Valid-\u0394env \u03c1\u2080 d\u03c1\u2080 \u2192\n Valid-\u0394env {\u03c4 \u2022 \u0393\u2080} (v \u2022 \u03c1\u2080) (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- Data type to hold proofs that a \u0394-env is consistent with self\ndata Consistent-\u0394env : {\u0393 : Context} (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is consistent with itself\n d\u03c1=\u2205 : Consistent-\u0394env {\u2205} \u2205\n -- Induction case: the change introduced at top level\n -- should be valid.\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Consistent-\u0394env d\u03c1\u2080 \u2192\n Consistent-\u0394env {\u03c4 \u2022 \u0393\u2080} (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- If a \u0394-environment is valid for some other environment,\n-- then it is also consistent with itself.\n\nvalid-\u0394env-is-consistent :\n \u2200 {\u0393 : Context} {\u03c1 : \u27e6 \u0393 \u27e7} {d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Valid-\u0394env \u03c1 d\u03c1 \u2192 Consistent-\u0394env d\u03c1\n\nvalid-\u0394env-is-consistent \u03c1=\u2205 = d\u03c1=\u2205\nvalid-\u0394env-is-consistent (\u03c1=v\u2022\u03c1\u2080 valid[v,dv] valid[\u03c1\u2080,d\u03c1\u2080]) =\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] (valid-\u0394env-is-consistent valid[\u03c1\u2080,d\u03c1\u2080])\n\n-- finally, update and ignore\n\nupdate : \u2200 {\u0393} \u2192 (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env d\u03c1} \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 {d\u03c1=\u2205} = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 d\u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] consistent[d\u03c1\u2080]} =\n (v \u27e6\u2295\u27e7 dv) \u2022 update d\u03c1 {consistent[d\u03c1\u2080]}\n\n-- Ignorance is bliss (don't have to pass consistency proofs around :D)\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 -- Using a proof to describe computation\n\n-- Naming scheme follows weakenVar\/weaken\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- derive(n) = \u03bbf. f n n\nderive (nat n) = abs (app (app (var this) (nat n)) (nat n))\n\n-- derive(x) = dx\nderive (var x) = var (deriveVar x)\n\n-- derive(\u03bbx. t) = \u03bbx. \u03bbdx. derive(t)\nderive (abs t) = abs (abs (derive t))\n\n-- derive(f s) = derive(f) s derive(s)\nderive (app f s) = app (app (derive f) (weaken \u0393\u227c\u0394\u0393 s)) (derive s)\n\n-- derive (foldNat n f z) = ?\nderive (foldNat n f z) = {!!}\n\n-- Extensional equivalence for changes\ndata as-\u0394_is_ext-equiv-to_ :\n \u2200 (\u03c4 : Type) \u2192 (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 (dg : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n ext-\u0394 : \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n (\u2200 (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192\n (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)) \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg\n\ninfix 3 as-\u0394_is_ext-equiv-to_\n\n-- Extractor for extensional-equivalence-as-changes:\n-- Given a value of the data type holding the proof,\n-- returns the proof in applicable form.\n--\n-- It would not be necessary if such\n-- proof-holding types were defined as a function in\n-- the first place, say in the manner of `valid-\u0394`.\n--\nextract-\u0394equiv :\n \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg \u2192\n (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192 (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)\n\nextract-\u0394equiv (ext-\u0394 proof-method) = proof-method\n\n-- Distribution lemmas of validity over \u27e6\u229d\u27e7 and \u27e6\u2295\u27e7\n\napplication-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v) \u2261 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\napplication-over-\u2295-and-\u229d f g df v =\n begin\n f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (cong\u2082 _\u27e6\u2295\u27e7_\n (cong f ((sym (f\u2295\u0394f=f v))))\n (cong\u2082 df\n (sym (f\u2295\u0394f=f v))\n (cong \u27e6derive\u27e7 (sym (f\u2295\u0394f=f v)))))\n refl \u27e9\n f (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v)\n \u27e6\u2295\u27e7 df (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v) (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v))\n \u27e6\u229d\u27e7 f v\n \u2261\u27e8 refl \u27e9\n (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e where open \u2261-Reasoning\n\nvalidity-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n valid-\u0394 g (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) \u2192\n valid-\u0394 (g v) (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\nvalidity-over-\u2295-and-\u229d f g df v R[g,f\u2295df]\n rewrite application-over-\u2295-and-\u229d f g df v =\n proj\u2081 (R[g,f\u2295df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))\n\n-- diff-apply\ndf=f\u2295df\u229df :\n \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n valid-\u0394 f df \u2192 as-\u0394 \u03c4 is df ext-equiv-to f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f\n\n-- Case nat: this REFL is more obvious to Agda than to a human.\ndf=f\u2295df\u229df {nats} n dn valid-f-df =\n ext-\u0394 (\u03bb m valid-m-dn valid-rhs \u2192 refl)\n\ndf=f\u2295df\u229df {\u03c4\u2081 \u21d2 \u03c4\u2082} f df R[f,df] = ext-\u0394 (\n \u03bb g R[g,df] R[g,f\u2295df\u229df] \u2192\n extensionality (\u03bb v \u2192\n begin\n g v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n \u2261\u27e8 extract-\u0394equiv\n (df=f\u2295df\u229df\n (f v) (df v (\u27e6derive\u27e7 v))\n (proj\u2081 (R[f,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))))\n (g v)\n (proj\u2081 (R[g,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v)))\n (validity-over-\u2295-and-\u229d f g df v R[g,f\u2295df\u229df]) \u27e9\n g v \u27e6\u2295\u27e7 (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = g v} {y = g v} refl\n (application-over-\u2295-and-\u229d f g df v) \u27e9\n g v \u27e6\u2295\u27e7 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e\n )\n )\n where open \u2261-Reasoning\n\n\n\ncorrectness-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 deriveVar x \u27e7 \u03c1\n ext-equiv-to\n \u27e6 x \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 x \u27e7 (ignore \u03c1)\n\ncorrectness-of-deriveVar {\u03c4 \u2022 \u0393\u2080} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n this = df=f\u2295df\u229df {\u03c4} v dv R[v,dv]\n\ncorrectness-of-deriveVar {\u03c4\u2080 \u2022 \u0393\u2080} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4\u2080} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n (that x) = correctness-of-deriveVar \u03c1 x\n\n\n\ncorrectness-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 derive t \u27e7 \u03c1\n ext-equiv-to\n \u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 t \u27e7 (ignore \u03c1)\n\n-- Mutually recursive lemma: derivatives are valid\nvalidity-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 deriveVar x \u27e7 \u03c1)\n\nvalidity-of-deriveVar {\u03c4 \u2022 \u0393} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n this = R[v,dv]\n\nvalidity-of-deriveVar {\u03c4\u2080 \u2022 \u0393} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n (that x) = validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 {consistency} (var x) =\n validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 (nat n) = refl\n\nvalidity-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs t) = \u03bb v dv R[v,dv] \u2192\n validity-of-derive (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency} t\n , -- tuple constructor\n (begin\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n ((R[f,g\u229df] (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 f\u2295[g\u229df]=g (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u2261\u27e8 cong \u27e6 t \u27e7 (cong\u2082 _\u2022_ (f\u2295\u0394f=f (v \u27e6\u2295\u27e7 dv)) refl) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency}))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 sym (extract-\u0394equiv\n (correctness-of-derive\n ((dv \u2022 v \u2022 \u03c1))\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (R[f,g\u229df] (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u220e)\n where open \u2261-Reasoning\n\nvalidity-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} t\u2081 t\u2082)\n = R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7]\n where\n open \u2261-Reasoning\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 : \u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1) \u2261 v\u2081 v\u2082\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 rewrite (sym v\u2082=old-v\u2082) = refl\n\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 : \u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1 \u2261 dv\u2081 v\u2082 dv\u2082\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = refl\n\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] : valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (dv\u2081 v\u2082 dv\u2082)\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] rewrite \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 = R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n -- What I want to write:\n {-\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] rewrite \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n -}\n\n -- What I have to write:\n\n R : {\u03c4 : Type} \u2192 {v : \u27e6 \u03c4 \u27e7} \u2192 {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 valid-\u0394 v dv\u2081 \u2192 valid-\u0394 v dv\u2082\n\n R {nats} dv\u2081=dv\u2082 refl = cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl\n\n --R {\u03c4\u2081 \u21d2 \u03c4\u2082} dv\u2081=dv\u2082 valid-dv\u2081 rewrite dv\u2081=dv\u2082 = {!valid-dv\u2081!}\n R {\u03c4\u2081 \u21d2 \u03c4\u2082} {v} {dv\u2081} {dv\u2082} dv\u2081=dv\u2082 valid-dv\u2081 =\n \u03bb s ds R[s,ds] \u2192\n R {\u03c4\u2082} {v s} {dv\u2081 s ds} {dv\u2082 s ds}\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl)\n (proj\u2081 (valid-dv\u2081 s ds R[s,ds]))\n ,\n (begin\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2082 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v (s \u27e6\u2295\u27e7 ds)} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) (sym dv\u2081=dv\u2082) refl) refl) \u27e9\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2081 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 sym (proj\u2082 (valid-dv\u2081\n (s \u27e6\u2295\u27e7 ds)\n (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n (R[f,\u0394f] (s \u27e6\u2295\u27e7 ds)))) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds \u27e6\u2295\u27e7 \u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong (v \u27e6\u2295\u27e7 dv\u2081) (f\u2295\u0394f=f (s \u27e6\u2295\u27e7 ds)) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds)\n \u2261\u27e8 proj\u2082 (valid-dv\u2081 s ds R[s,ds]) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2081 s ds\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v s} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2082 s ds\n \u220e) where open \u2261-Reasoning\n\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] = R \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n\nvalidity-of-derive \u03c1 {consistency} (foldNat n f z) = {!!}\n\n\ncorrectness-of-derive \u03c1 (var x) = correctness-of-deriveVar \u03c1 x\n\ncorrectness-of-derive \u03c1 (nat n) = ext-\u0394 (\u03bb _ _ _ \u2192 refl)\n\ncorrectness-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs {.\u03c4\u2081} {.\u03c4\u2082} t) =\n ext-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n extensionality {\u27e6 \u03c4\u2081 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4\u2082 \u27e7} (\u03bb x \u2192\n begin\n f x \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive {\u03c4\u2081 \u2022 \u0393} {\u03c4\u2082}\n (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency}\n t)\n (f x)\n (proj\u2081 (R[f,\u0394t] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x)))\n (proj\u2081 (R[f,t\u2032\u229dt] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x))) \u27e9\n f x\n \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 (update (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency})\n \u27e6\u229d\u27e7 \u27e6 t \u27e7 (x \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u220e\n )) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} {\u0393} t\u2081 t\u2082) =\n ext-\u0394 {\u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n begin\n f \u27e6\u2295\u27e7 \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n \u2261\u27e8 extract-\u0394equiv ext-\u0394[t\u2081t\u2082] f R[f,\u0394t] R[f,t\u2032\u229dt] \u27e9\n f \u27e6\u2295\u27e7\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n \u220e)\n where\n open \u2261-Reasoning\n\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n v\u2081\u2295dv\u2081=v\u2081\u2032 : v\u2081 \u27e6\u2295\u27e7 dv\u2081 \u2261 v\u2081\u2032\n v\u2081\u2295dv\u2081=v\u2081\u2032 =\n begin\n v\u2081 \u27e6\u2295\u27e7 dv\u2081\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2081)\n v\u2081 valid-dv\u2081 (R[f,g\u229df] v\u2081 v\u2081\u2032) \u27e9\n v\u2081 \u27e6\u2295\u27e7 (v\u2081\u2032 \u27e6\u229d\u27e7 v\u2081)\n \u2261\u27e8 f\u2295[g\u229df]=g v\u2081 v\u2081\u2032 \u27e9\n v\u2081\u2032\n \u220e\n\n -- TODO: remove code duplication.\n v\u2082\u2295dv\u2082=v\u2082\u2032 : v\u2082 \u27e6\u2295\u27e7 dv\u2082 \u2261 v\u2082\u2032\n v\u2082\u2295dv\u2082=v\u2082\u2032 rewrite v\u2082=old-v\u2082 =\n begin\n old-v\u2082 \u27e6\u2295\u27e7 dv\u2082\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2082)\n old-v\u2082\n (validity-of-derive \u03c1 {consistency} t\u2082)\n (R[f,g\u229df] old-v\u2082 v\u2082\u2032) \u27e9\n old-v\u2082 \u27e6\u2295\u27e7 (v\u2082\u2032 \u27e6\u229d\u27e7 old-v\u2082)\n \u2261\u27e8 f\u2295[g\u229df]=g old-v\u2082 v\u2082\u2032 \u27e9\n v\u2082\u2032\n \u220e\n where old-v\u2082 = \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] : v\u2081\u2032 v\u2082\u2032 \u2261 (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082)\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] = sym (cong\u2082 (\u03bb f x \u2192 f x) v\u2081\u2295dv\u2081=v\u2081\u2032 v\u2082\u2295dv\u2082=v\u2082\u2032)\n\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 :\n (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082) \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 = proj\u2082 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082 \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n cong\u2082 _\u27e6\u229d\u27e7_\n (trans v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082)\n refl\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n as-\u0394 \u03c4\u2082 is dv\u2081 v\u2082 dv\u2082 ext-equiv-to v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n df=f\u2295df\u229df (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082) R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 : as-\u0394 \u03c4\u2082 is dv\u2081 v\u2082 dv\u2082 ext-equiv-to v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 rewrite v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082\n\n ext-\u0394[t\u2081t\u2082] :\n as-\u0394 \u03c4\u2082 is\n \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n ext-equiv-to\n \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency}) (\u27e6 t\u2082 \u27e7 (update \u03c1 {consistency}))\n \u27e6\u229d\u27e7 \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n ext-\u0394[t\u2081t\u2082] rewrite sym v\u2082=old-v\u2082 = dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082\n\ncorrectness-of-derive {\u0393} {\u03c4}\n \u03c1 {consistency} (foldNat n f z) = {!!}\n\ncorrectness-on-closed-terms : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192\n \u2200 (f : Term \u2205 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192\n \u2200 (s : Term \u2205 \u03c4\u2081) (ds : Term \u2205 (\u0394-Type \u03c4\u2081))\n {R[v,dv] : valid-\u0394 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)} \u2192\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205 \u27e6\u2295\u27e7 \u27e6 ds \u27e7 \u2205)\n \u2261\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)\n\ncorrectness-on-closed-terms {\u03c4\u2081} {\u03c4\u2082} f s ds {R[v,dv]} =\n begin\n h (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (f\u2295[g\u229df]=g h h))\n refl \u27e9\n (h \u27e6\u2295\u27e7 (h \u27e6\u229d\u27e7 h)) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (extract-\u0394equiv\n (correctness-of-derive \u2205 f)\n h\n (validity-of-derive \u2205 {d\u03c1=\u2205} f)\n (R[f,g\u229df] h h)))\n refl \u27e9\n (h \u27e6\u2295\u27e7 \u0394h) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 proj\u2082 (validity-of-derive \u2205 {d\u03c1=\u2205} f v dv R[v,dv]) \u27e9\n h v \u27e6\u2295\u27e7 \u0394h v dv\n \u220e\n where\n open \u2261-Reasoning\n h : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n h = \u27e6 f \u27e7 \u2205\n \u0394h : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n \u0394h = \u27e6 derive f \u27e7 \u2205\n v : \u27e6 \u03c4\u2081 \u27e7\n v = \u27e6 s \u27e7 \u2205\n dv : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv = \u27e6 ds \u27e7 \u2205\n\n","old_contents":"{-\n\nChecklist of stuff to add when adding syntactic constructs\n\n- derive (symbolic derivation; most important!)\n- weaken\n- \u27e6_\u27e7Term\n- weaken-sound\n- validity-of-derive\n- correctness-of-derive\n\n-}\n\nmodule Extension-00 where\n\nopen import Data.Product\nopen import Data.Nat using (\u2115)\nopen import Data.Unit using (\u22a4)\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Level using\n (zero ; Level ; suc)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\npostulate extensionality : Extensionality zero zero\n\ndata Type : Set where\n nats : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n\u27e8\u2205\u27e9 : \u2205 \u2261 \u2205\n\u27e8\u2205\u27e9 = refl\n\n_\u27e8\u2022\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} \u2192 \u03c4\u2081 \u2261 \u03c4\u2082 \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u03c4\u2081 \u2022 \u0393\u2081 \u2261 \u03c4\u2082 \u2022 \u0393\u2082\n_\u27e8\u2022\u27e9_ = cong\u2082 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n -- ad-hoc extensions\n foldNat : \u2200 {\u0393 \u03c4} \u2192 (n : Term \u0393 nats) \u2192\n (f : Term \u0393 (\u03c4 \u21d2 \u03c4)) \u2192 (z : Term \u0393 \u03c4)\n \u2192 Term \u0393 \u03c4\n\n -- Change to nats = replacement Church pairs\n -- 3 -> 5 ::= \u03bbf. f 3 5\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u227c-reflexivity : Reflexive _\u227c_\n\u227c-reflexivity {\u2205} = \u2205\u227c\u2205\n\u227c-reflexivity {\u03c4 \u2022 x} = keep \u03c4 \u2022 \u227c-reflexivity\n\n\u227c-reflexive : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u0393\u2081 \u227c \u0393\u2082\n\u227c-reflexive refl = \u227c-reflexivity\n\n\u227c-transitive : Transitive _\u227c_\n\u227c-transitive \u2205\u227c\u2205 rel1 = rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n keep \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (keep \u03c4 \u2022 rel0) rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n drop \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (drop \u03c4 \u2022 rel0) rel1\n\n\u227c-isPreorder : IsPreorder _\u2261_ _\u227c_\n\u227c-isPreorder = record\n { isEquivalence = isEquivalence\n ; reflexive = \u227c-reflexive\n ; trans = \u227c-transitive\n }\n\n\u227c-preorder : Preorder _ _ _\n\u227c-preorder = record\n { Carrier = Context\n ; _\u2248_ = _\u2261_\n ; isPreorder = \u227c-isPreorder\n }\n\nmodule \u227c-reasoning where\n open import Relation.Binary.PreorderReasoning \u227c-preorder public\n renaming\n ( _\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_\n ; _\u223c\u27e8_\u27e9_ to _\u227c\u27e8_\u27e9_\n ; _\u2248\u27e8\u27e9_ to _\u2261\u27e8\u27e9_\n )\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\n\nweaken subctx (foldNat n f z) = {!!}\n\nrecord Meaning (Syntax : Set) {\u2113 : Level} : Set (suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 nats \u27e7Type = \u2115\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\n\u27e6 foldNat n f z \u27e7Term \u03c1 = {!!}\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = cong\u2082 (\u03bb x y \u2192 x y)\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = \u2261-app (weaken-sound t\u2081 \u03c1) (weaken-sound t\u2082 \u03c1)\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (foldNat n f z) \u03c1 = {!!}\n\n-- Changes to \u2115 are replacement Church pairs. The only arguments\n-- of conern are `fst` and `snd`, so the Church pairs don't have\n-- to be polymorphic.\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type nats = (nats \u21d2 nats \u21d2 nats) \u21d2 nats\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\n-- It is clear that \u27e6\u229d\u27e7 exists on the semantic level:\n-- there exists an Agda value to describe the change between any\n-- two Agda values denoted by terms. If we have (not dependently-\n-- typed) arrays, no term denotes the change between two arrays\n-- of different lengths. Thus no full abstraction.\n\n\u27e6derive\u27e7 : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ninfixl 6 _\u27e6\u2295\u27e7_\ninfixl 6 _\u27e6\u229d\u27e7_\n\n\u27e6fst\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e6snd\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\n\u27e6derive\u27e7 {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 f (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f v\n\u27e6derive\u27e7 {nats} n = \u03bb f \u2192 f n n\n\n_\u27e6\u229d\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 f\u2081 (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f\u2082 v\n_\u27e6\u229d\u27e7_ {nats} m n = \u03bb f \u2192 f n m\n-- m \u27e6\u229d\u27e7 n ::= replace n by m\n\n_\u27e6\u2295\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n_\u27e6\u2295\u27e7_ {nats} n dn = dn \u27e6snd\u27e7\n\n\u27e6fst\u27e7 m n = m\n\u27e6snd\u27e7 m n = n\n\n-- Strong validity!\ndata \u27e6Valid\u0394\u27e7 : {\u03c4 : Type} \u2192 (v : \u27e6 \u03c4 \u27e7) \u2192 (dv : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n -- Following Pierce's case names `T-Var`, `T-Abs`, `T-App`\n -- with Agda's capitalization convention\n -- generalized to the semantic (value) domain\n v-nat : (n : \u2115) \u2192 (dn : (\u2115 \u2192 \u2115 \u2192 \u2115) \u2192 \u2115) \u2192\n n \u2261 dn \u27e6fst\u27e7 \u2192\n \u27e6Valid\u0394\u27e7 n dn\n\n v-fun : {\u03c4\u2081 \u03c4\u2082 : Type} \u2192\n (f : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) \u2192 (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) \u2192\n -- A strong antecedent: f and df map invalid changes to\n -- valid changes! So long as invalid changes exist,\n -- this is NOT satisfied by\n --\n -- f = \u27e6 \u03bb x. x \u27e7 = identity\n -- df = \u27e6 \u03bb x dx. dx \u27e7 = \u27e6snd\u27e7,\n --\n -- negating any hope of \u27e6Valid\u0394\u27e7 \u27e6 t \u27e7 \u27e6 derive t \u27e7.\n (\u2200 {s ds} \u2192 {-Valid\u0394 s ds \u2192-} \u27e6Valid\u0394\u27e7 (f s) (df s ds)) \u2192\n (\u2200 {s ds} \u2192 (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 (f s \u27e6\u2295\u27e7 df s ds)) \u2192\n \u27e6Valid\u0394\u27e7 f df\n\nabsurdity-of-0=1 : 0 \u2261 1 \u2192 (\u2200 {A : Set} \u2192 A)\nabsurdity-of-0=1 ()\n\nit-is-absurd-that : \u27e6Valid\u0394\u27e7 {nats} 0 (\u03bb f \u2192 f 1 1) \u2192 (\u2200 {A : Set} \u2192 A)\nit-is-absurd-that (v-nat .0 .(\u03bb f \u2192 f 1 1) 0=1) = absurdity-of-0=1 0=1\n\n-- If (\u03bb x dx \u2192 dx) is a \u27e6Valid\u0394\u27e7 to (\u03bb x \u2192 x),\n-- then impossible is nothing.\nno-way : \u27e6Valid\u0394\u27e7 {nats \u21d2 nats} (\u03bb x \u2192 x) (\u03bb x dx \u2192 dx) \u2192\n (\u2200 {A : Set} \u2192 A)\nno-way (v-fun .(\u03bb x \u2192 x) .(\u03bb x dx \u2192 dx) validity correctness) =\n it-is-absurd-that (validity {0} {\u03bb f \u2192 f 1 1})\n\n-- Cool lemmas\n\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u229d\u27e7 f \u2261 \u27e6derive\u27e7 f\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {nats} f = refl\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = refl\n\nf\u2295[g\u229df]=g : \u2200 {\u03c4 : Type} (f g : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) \u2261 g\n\nf\u2295\u0394f=f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (\u27e6derive\u27e7 f) \u2261 f\n\nf\u2295[g\u229df]=g {nats} m n = refl\nf\u2295[g\u229df]=g {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = f x} {y = f x} refl\n (cong\u2082 _\u27e6\u229d\u27e7_ (cong g (f\u2295\u0394f=f x)) refl) \u27e9\n f x \u27e6\u2295\u27e7 (g x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (g x) \u27e9\n g x\n \u220e\n ) where open \u2261-Reasoning\n\nf\u2295\u0394f=f {nats} n = refl\nf\u2295\u0394f=f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 \u27e6derive\u27e7 f) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (f (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong (\u03bb hole \u2192 f x \u27e6\u2295\u27e7 (f hole \u27e6\u229d\u27e7 f x)) (f\u2295\u0394f=f x) \u27e9\n f x \u27e6\u2295\u27e7 (f x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (f x) \u27e9\n f x\n \u220e\n ) where open \u2261-Reasoning\n\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {nats} n dn = n \u2261 dn \u27e6fst\u27e7\nvalid-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (s : \u27e6 \u03c4\u2081 \u27e7) (ds : \u27e6 \u0394-Type \u03c4\u2081 \u27e7) (R[s,ds] : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7 -- (valid-\u0394:1)\n (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 f s \u27e6\u2295\u27e7 df s ds -- (valid-\u0394:2)\n\nR[f,g\u229df] : \u2200 {\u03c4} (f g : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (g \u27e6\u229d\u27e7 f)\nR[f,g\u229df] {nats} m n = refl\nR[f,g\u229df] {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb x dx R[x,dx] \u2192\n R[f,g\u229df] {\u03c4\u2082} (f x) (g (x \u27e6\u2295\u27e7 dx)) -- (valid-\u0394:1)\n , -- tuple constructor\n (begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 refl \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7\n (g ((x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 \u27e6derive\u27e7 (x \u27e6\u2295\u27e7 dx)) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 cong (\u03bb hole \u2192 f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g hole \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx)))\n (f\u2295\u0394f=f (x \u27e6\u2295\u27e7 dx)) \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 f\u2295[g\u229df]=g (f (x \u27e6\u2295\u27e7 dx)) (g (x \u27e6\u2295\u27e7 dx)) \u27e9\n g (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (f x) (g (x \u27e6\u2295\u27e7 dx))) \u27e9\n f x \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) x dx\n \u220e)\n where open \u2261-Reasoning\n\nR[f,\u0394f] : \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (\u27e6derive\u27e7 f)\nR[f,\u0394f] f rewrite sym (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f f) = R[f,g\u229df] f f\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393 -- push \u03c4, then push \u0394\u03c4.\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 (keep \u03c4 \u2022 \u0393\u227c\u0394\u0393)\n\n-- Data type to hold proofs that environments are valid\ndata Valid-\u0394env : {\u0393 : Context} (\u03c1 : \u27e6 \u0393 \u27e7) (\u0394\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is its own valid \u0394-environment.\n \u03c1=\u2205 : Valid-\u0394env {\u2205} \u2205 \u2205\n -- Induction case: the change introduced therein should be valid,\n -- and the smaller \u0394-environment should be valid as well.\n \u03c1=v\u2022\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {\u03c1\u2080 : \u27e6 \u0393\u2080 \u27e7} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Valid-\u0394env \u03c1\u2080 d\u03c1\u2080 \u2192\n Valid-\u0394env {\u03c4 \u2022 \u0393\u2080} (v \u2022 \u03c1\u2080) (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- Data type to hold proofs that a \u0394-env is consistent with self\ndata Consistent-\u0394env : {\u0393 : Context} (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is consistent with itself\n d\u03c1=\u2205 : Consistent-\u0394env {\u2205} \u2205\n -- Induction case: the change introduced at top level\n -- should be valid.\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Consistent-\u0394env d\u03c1\u2080 \u2192\n Consistent-\u0394env {\u03c4 \u2022 \u0393\u2080} (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- If a \u0394-environment is valid for some other environment,\n-- then it is also consistent with itself.\n\nvalid-\u0394env-is-consistent :\n \u2200 {\u0393 : Context} {\u03c1 : \u27e6 \u0393 \u27e7} {d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Valid-\u0394env \u03c1 d\u03c1 \u2192 Consistent-\u0394env d\u03c1\n\nvalid-\u0394env-is-consistent \u03c1=\u2205 = d\u03c1=\u2205\nvalid-\u0394env-is-consistent (\u03c1=v\u2022\u03c1\u2080 valid[v,dv] valid[\u03c1\u2080,d\u03c1\u2080]) =\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] (valid-\u0394env-is-consistent valid[\u03c1\u2080,d\u03c1\u2080])\n\n-- finally, update and ignore\n\nupdate : \u2200 {\u0393} \u2192 (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env d\u03c1} \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 {d\u03c1=\u2205} = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 d\u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] consistent[d\u03c1\u2080]} =\n (v \u27e6\u2295\u27e7 dv) \u2022 update d\u03c1 {consistent[d\u03c1\u2080]}\n\n-- Ignorance is bliss (don't have to pass consistency proofs around :D)\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 -- Using a proof to describe computation\n\n-- Naming scheme follows weakenVar\/weaken\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- derive(n) = \u03bbf. f n n\nderive (nat n) = abs (app (app (var this) (nat n)) (nat n))\n\n-- derive(x) = dx\nderive (var x) = var (deriveVar x)\n\n-- derive(\u03bbx. t) = \u03bbx. \u03bbdx. derive(t)\nderive (abs t) = abs (abs (derive t))\n\n-- derive(f s) = derive(f) s derive(s)\nderive (app f s) = app (app (derive f) (weaken \u0393\u227c\u0394\u0393 s)) (derive s)\n\n-- derive (foldNat f n z) =\nderive (foldNat f n z) = {!!}\n\n-- Extensional equivalence for changes\ndata as-\u0394_is_ext-equiv-to_ :\n \u2200 (\u03c4 : Type) \u2192 (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 (dg : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n ext-\u0394 : \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n (\u2200 (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192\n (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)) \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg\n\ninfix 3 as-\u0394_is_ext-equiv-to_\n\n-- Extractor for extensional-equivalence-as-changes:\n-- Given a value of the data type holding the proof,\n-- returns the proof in applicable form.\n--\n-- It would not be necessary if such\n-- proof-holding types were defined as a function in\n-- the first place, say in the manner of `valid-\u0394`.\n--\nextract-\u0394equiv :\n \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg \u2192\n (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192 (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)\n\nextract-\u0394equiv (ext-\u0394 proof-method) = proof-method\n\n-- Distribution lemmas of validity over \u27e6\u229d\u27e7 and \u27e6\u2295\u27e7\n\napplication-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v) \u2261 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\napplication-over-\u2295-and-\u229d f g df v =\n begin\n f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (cong\u2082 _\u27e6\u2295\u27e7_\n (cong f ((sym (f\u2295\u0394f=f v))))\n (cong\u2082 df\n (sym (f\u2295\u0394f=f v))\n (cong \u27e6derive\u27e7 (sym (f\u2295\u0394f=f v)))))\n refl \u27e9\n f (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v)\n \u27e6\u2295\u27e7 df (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v) (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v))\n \u27e6\u229d\u27e7 f v\n \u2261\u27e8 refl \u27e9\n (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e where open \u2261-Reasoning\n\nvalidity-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n valid-\u0394 g (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) \u2192\n valid-\u0394 (g v) (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\nvalidity-over-\u2295-and-\u229d f g df v R[g,f\u2295df]\n rewrite application-over-\u2295-and-\u229d f g df v =\n proj\u2081 (R[g,f\u2295df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))\n\n-- diff-apply\ndf=f\u2295df\u229df :\n \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n valid-\u0394 f df \u2192 as-\u0394 \u03c4 is df ext-equiv-to f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f\n\n-- Case nat: this REFL is more obvious to Agda than to a human.\ndf=f\u2295df\u229df {nats} n dn valid-f-df =\n ext-\u0394 (\u03bb m valid-m-dn valid-rhs \u2192 refl)\n\ndf=f\u2295df\u229df {\u03c4\u2081 \u21d2 \u03c4\u2082} f df R[f,df] = ext-\u0394 (\n \u03bb g R[g,df] R[g,f\u2295df\u229df] \u2192\n extensionality (\u03bb v \u2192\n begin\n g v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n \u2261\u27e8 extract-\u0394equiv\n (df=f\u2295df\u229df\n (f v) (df v (\u27e6derive\u27e7 v))\n (proj\u2081 (R[f,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))))\n (g v)\n (proj\u2081 (R[g,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v)))\n (validity-over-\u2295-and-\u229d f g df v R[g,f\u2295df\u229df]) \u27e9\n g v \u27e6\u2295\u27e7 (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = g v} {y = g v} refl\n (application-over-\u2295-and-\u229d f g df v) \u27e9\n g v \u27e6\u2295\u27e7 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e\n )\n )\n where open \u2261-Reasoning\n\n\n\ncorrectness-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 deriveVar x \u27e7 \u03c1\n ext-equiv-to\n \u27e6 x \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 x \u27e7 (ignore \u03c1)\n\ncorrectness-of-deriveVar {\u03c4 \u2022 \u0393\u2080} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n this = df=f\u2295df\u229df {\u03c4} v dv R[v,dv]\n\ncorrectness-of-deriveVar {\u03c4\u2080 \u2022 \u0393\u2080} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4\u2080} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n (that x) = correctness-of-deriveVar \u03c1 x\n\n\n\ncorrectness-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 derive t \u27e7 \u03c1\n ext-equiv-to\n \u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 t \u27e7 (ignore \u03c1)\n\n-- Mutually recursive lemma: derivatives are valid\nvalidity-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 deriveVar x \u27e7 \u03c1)\n\nvalidity-of-deriveVar {\u03c4 \u2022 \u0393} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n this = R[v,dv]\n\nvalidity-of-deriveVar {\u03c4\u2080 \u2022 \u0393} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n (that x) = validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 {consistency} (var x) =\n validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 (nat n) = refl\n\nvalidity-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs t) = \u03bb v dv R[v,dv] \u2192\n validity-of-derive (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency} t\n , -- tuple constructor\n (begin\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n ((R[f,g\u229df] (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 f\u2295[g\u229df]=g (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u2261\u27e8 cong \u27e6 t \u27e7 (cong\u2082 _\u2022_ (f\u2295\u0394f=f (v \u27e6\u2295\u27e7 dv)) refl) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency}))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 sym (extract-\u0394equiv\n (correctness-of-derive\n ((dv \u2022 v \u2022 \u03c1))\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (R[f,g\u229df] (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u220e)\n where open \u2261-Reasoning\n\nvalidity-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} t\u2081 t\u2082)\n = R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7]\n where\n open \u2261-Reasoning\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 : \u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1) \u2261 v\u2081 v\u2082\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 rewrite (sym v\u2082=old-v\u2082) = refl\n\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 : \u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1 \u2261 dv\u2081 v\u2082 dv\u2082\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = refl\n\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] : valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (dv\u2081 v\u2082 dv\u2082)\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] rewrite \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 = R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n -- What I want to write:\n {-\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] rewrite \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n -}\n\n -- What I have to write:\n\n R : {\u03c4 : Type} \u2192 {v : \u27e6 \u03c4 \u27e7} \u2192 {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 valid-\u0394 v dv\u2081 \u2192 valid-\u0394 v dv\u2082\n\n R {nats} dv\u2081=dv\u2082 refl = cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl\n\n --R {\u03c4\u2081 \u21d2 \u03c4\u2082} dv\u2081=dv\u2082 valid-dv\u2081 rewrite dv\u2081=dv\u2082 = {!valid-dv\u2081!}\n R {\u03c4\u2081 \u21d2 \u03c4\u2082} {v} {dv\u2081} {dv\u2082} dv\u2081=dv\u2082 valid-dv\u2081 =\n \u03bb s ds R[s,ds] \u2192\n R {\u03c4\u2082} {v s} {dv\u2081 s ds} {dv\u2082 s ds}\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl)\n (proj\u2081 (valid-dv\u2081 s ds R[s,ds]))\n ,\n (begin\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2082 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v (s \u27e6\u2295\u27e7 ds)} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) (sym dv\u2081=dv\u2082) refl) refl) \u27e9\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2081 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 sym (proj\u2082 (valid-dv\u2081\n (s \u27e6\u2295\u27e7 ds)\n (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n (R[f,\u0394f] (s \u27e6\u2295\u27e7 ds)))) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds \u27e6\u2295\u27e7 \u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong (v \u27e6\u2295\u27e7 dv\u2081) (f\u2295\u0394f=f (s \u27e6\u2295\u27e7 ds)) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds)\n \u2261\u27e8 proj\u2082 (valid-dv\u2081 s ds R[s,ds]) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2081 s ds\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v s} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2082 s ds\n \u220e) where open \u2261-Reasoning\n\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] = R \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n\nvalidity-of-derive \u03c1 {consistency} (foldNat n f z) = {!!}\n\n\ncorrectness-of-derive \u03c1 (var x) = correctness-of-deriveVar \u03c1 x\n\ncorrectness-of-derive \u03c1 (nat n) = ext-\u0394 (\u03bb _ _ _ \u2192 refl)\n\ncorrectness-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs {.\u03c4\u2081} {.\u03c4\u2082} t) =\n ext-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n extensionality {\u27e6 \u03c4\u2081 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4\u2082 \u27e7} (\u03bb x \u2192\n begin\n f x \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive {\u03c4\u2081 \u2022 \u0393} {\u03c4\u2082}\n (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency}\n t)\n (f x)\n (proj\u2081 (R[f,\u0394t] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x)))\n (proj\u2081 (R[f,t\u2032\u229dt] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x))) \u27e9\n f x\n \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 (update (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency})\n \u27e6\u229d\u27e7 \u27e6 t \u27e7 (x \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u220e\n )) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} {\u0393} t\u2081 t\u2082) =\n ext-\u0394 {\u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n begin\n f \u27e6\u2295\u27e7 \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n \u2261\u27e8 extract-\u0394equiv ext-\u0394[t\u2081t\u2082] f R[f,\u0394t] R[f,t\u2032\u229dt] \u27e9\n f \u27e6\u2295\u27e7\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n \u220e)\n where\n open \u2261-Reasoning\n\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n v\u2081\u2295dv\u2081=v\u2081\u2032 : v\u2081 \u27e6\u2295\u27e7 dv\u2081 \u2261 v\u2081\u2032\n v\u2081\u2295dv\u2081=v\u2081\u2032 =\n begin\n v\u2081 \u27e6\u2295\u27e7 dv\u2081\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2081)\n v\u2081 valid-dv\u2081 (R[f,g\u229df] v\u2081 v\u2081\u2032) \u27e9\n v\u2081 \u27e6\u2295\u27e7 (v\u2081\u2032 \u27e6\u229d\u27e7 v\u2081)\n \u2261\u27e8 f\u2295[g\u229df]=g v\u2081 v\u2081\u2032 \u27e9\n v\u2081\u2032\n \u220e\n\n -- TODO: remove code duplication.\n v\u2082\u2295dv\u2082=v\u2082\u2032 : v\u2082 \u27e6\u2295\u27e7 dv\u2082 \u2261 v\u2082\u2032\n v\u2082\u2295dv\u2082=v\u2082\u2032 rewrite v\u2082=old-v\u2082 =\n begin\n old-v\u2082 \u27e6\u2295\u27e7 dv\u2082\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2082)\n old-v\u2082\n (validity-of-derive \u03c1 {consistency} t\u2082)\n (R[f,g\u229df] old-v\u2082 v\u2082\u2032) \u27e9\n old-v\u2082 \u27e6\u2295\u27e7 (v\u2082\u2032 \u27e6\u229d\u27e7 old-v\u2082)\n \u2261\u27e8 f\u2295[g\u229df]=g old-v\u2082 v\u2082\u2032 \u27e9\n v\u2082\u2032\n \u220e\n where old-v\u2082 = \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] : v\u2081\u2032 v\u2082\u2032 \u2261 (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082)\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] = sym (cong\u2082 (\u03bb f x \u2192 f x) v\u2081\u2295dv\u2081=v\u2081\u2032 v\u2082\u2295dv\u2082=v\u2082\u2032)\n\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 :\n (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082) \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 = proj\u2082 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082 \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n cong\u2082 _\u27e6\u229d\u27e7_\n (trans v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082)\n refl\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n as-\u0394 \u03c4\u2082 is dv\u2081 v\u2082 dv\u2082 ext-equiv-to v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n df=f\u2295df\u229df (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082) R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 : as-\u0394 \u03c4\u2082 is dv\u2081 v\u2082 dv\u2082 ext-equiv-to v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 rewrite v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082\n\n ext-\u0394[t\u2081t\u2082] :\n as-\u0394 \u03c4\u2082 is\n \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n ext-equiv-to\n \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency}) (\u27e6 t\u2082 \u27e7 (update \u03c1 {consistency}))\n \u27e6\u229d\u27e7 \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n ext-\u0394[t\u2081t\u2082] rewrite sym v\u2082=old-v\u2082 = dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082\n\ncorrectness-of-derive {\u0393} {\u03c4}\n \u03c1 {consistency} (foldNat n f z) = {!!}\n\ncorrectness-on-closed-terms : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192\n \u2200 (f : Term \u2205 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192\n \u2200 (s : Term \u2205 \u03c4\u2081) (ds : Term \u2205 (\u0394-Type \u03c4\u2081))\n {R[v,dv] : valid-\u0394 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)} \u2192\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205 \u27e6\u2295\u27e7 \u27e6 ds \u27e7 \u2205)\n \u2261\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)\n\ncorrectness-on-closed-terms {\u03c4\u2081} {\u03c4\u2082} f s ds {R[v,dv]} =\n begin\n h (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (f\u2295[g\u229df]=g h h))\n refl \u27e9\n (h \u27e6\u2295\u27e7 (h \u27e6\u229d\u27e7 h)) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (extract-\u0394equiv\n (correctness-of-derive \u2205 f)\n h\n (validity-of-derive \u2205 {d\u03c1=\u2205} f)\n (R[f,g\u229df] h h)))\n refl \u27e9\n (h \u27e6\u2295\u27e7 \u0394h) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 proj\u2082 (validity-of-derive \u2205 {d\u03c1=\u2205} f v dv R[v,dv]) \u27e9\n h v \u27e6\u2295\u27e7 \u0394h v dv\n \u220e\n where\n open \u2261-Reasoning\n h : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n h = \u27e6 f \u27e7 \u2205\n \u0394h : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n \u0394h = \u27e6 derive f \u27e7 \u2205\n v : \u27e6 \u03c4\u2081 \u27e7\n v = \u27e6 s \u27e7 \u2205\n dv : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv = \u27e6 ds \u27e7 \u2205\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"12a874526963109760b8a84a9007561aa0d82700","subject":"Fixed doc.","message":"Fixed doc.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/FOT\/FOTC\/Data\/Nat\/AddTotality.agda","new_file":"notes\/FOT\/FOTC\/Data\/Nat\/AddTotality.agda","new_contents":"------------------------------------------------------------------------------\n-- Totality of natural numbers addition\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --schematic-propositional-functions #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Data.Nat.AddTotality where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n-- Interactive proof using the induction principle for natural numbers.\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = N-ind A A0 is Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n A0 : A zero\n A0 = subst N (sym (+-0x n)) Nn\n\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} ih = subst N (sym (+-Sx i n)) (nsucc ih)\n\n-- Interactive proof using an instance of the induction principle.\n+-N-ind : \u2200 {n} \u2192\n N (zero + n) \u2192\n (\u2200 {m} \u2192 N (m + n) \u2192 N (succ\u2081 m + n)) \u2192\n \u2200 {m} \u2192 N m \u2192 N (m + n)\n+-N-ind {n} = N-ind (\u03bb i \u2192 N (i + n))\n\n+-N\u2081 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N\u2081 {n = n} Nm Nn = +-N-ind prf\u2081 prf\u2082 Nm\n where\n prf\u2081 : N (zero + n)\n prf\u2081 = subst N (sym (+-0x n)) Nn\n\n prf\u2082 : \u2200 {m} \u2192 N (m + n) \u2192 N (succ\u2081 m + n)\n prf\u2082 {m} ih = subst N (sym (+-Sx m n)) (nsucc ih)\n\n-- Combined proof using an instance of the induction principle.\npostulate +-N\u2082 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n{-# ATP prove +-N\u2082 +-N-ind #-}\n\n-- Combined proof using the induction principle.\n\n-- The translation is\n-- \u2200 p. app\u2081(p,zero) \u2192\n-- (\u2200 x. app\u2081(n,x) \u2192 app\u2081(p,x) \u2192 app\u2081(p,appFn(succ,x))) \u2192 -- N-ind\n-- (\u2200 x. app\u2081(n,x) \u2192 app\u2081(p,x))\n----------------------------------------------------------------\n-- \u2200 x y. app\u2081(n,x) \u2192 app\u2081(n,y) \u2192 app\u2081(n,appFn(appFn(+,x),y)) -- +-N\u2082\n\npostulate +-N\u2083 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n-- Because the ATPs don't handle induction, them cannot prove this\n-- postulate.\n-- {-# ATP prove +-N\u2083 N-ind #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Totality of natural numbers addition\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --schematic-propositional-functions #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Data.Nat.AddTotality where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n-- Interactive proof using the induction principle for natural numbers.\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = N-ind A A0 is Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n A0 : A zero\n A0 = subst N (sym (+-0x n)) Nn\n\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} ih = subst N (sym (+-Sx i n)) (nsucc ih)\n\n-- Combined proof using an instance of the induction principle.\n+-N-ind : \u2200 {n} \u2192\n N (zero + n) \u2192\n (\u2200 {m} \u2192 N (m + n) \u2192 N (succ\u2081 m + n)) \u2192\n \u2200 {m} \u2192 N m \u2192 N (m + n)\n+-N-ind {n} = N-ind (\u03bb i \u2192 N (i + n))\n\n+-N\u2081 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N\u2081 {n = n} Nm Nn = +-N-ind prf\u2081 prf\u2082 Nm\n where\n prf\u2081 : N (zero + n)\n prf\u2081 = subst N (sym (+-0x n)) Nn\n\n prf\u2082 : \u2200 {m} \u2192 N (m + n) \u2192 N (succ\u2081 m + n)\n prf\u2082 {m} ih = subst N (sym (+-Sx m n)) (nsucc ih)\n\npostulate +-N\u2082 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n{-# ATP prove +-N\u2082 +-N-ind #-}\n\n-- Combined proof using the induction principle.\n\n-- The translation is\n-- \u2200 p. app\u2081(p,zero) \u2192\n-- (\u2200 x. app\u2081(n,x) \u2192 app\u2081(p,x) \u2192 app\u2081(p,appFn(succ,x))) \u2192 -- N-ind\n-- (\u2200 x. app\u2081(n,x) \u2192 app\u2081(p,x))\n----------------------------------------------------------------\n-- \u2200 x y. app\u2081(n,x) \u2192 app\u2081(n,y) \u2192 app\u2081(n,appFn(appFn(+,x),y)) -- +-N\u2082\n\npostulate +-N\u2083 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n-- Because the ATPs don't handle induction, them cannot prove this\n-- postulate.\n-- {-# ATP prove +-N\u2083 N-ind #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d635357fe352bf547faec865695ca24e9607d7c9","subject":"Renamed a property.","message":"Renamed a property.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Base\/List\/PropertiesI.agda","new_file":"src\/fot\/FOTC\/Base\/List\/PropertiesI.agda","new_contents":"------------------------------------------------------------------------------\n-- FOTC list terms properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Base.List.PropertiesI where\n\nopen import Common.FOL.Relation.Binary.EqReasoning\n\nopen import FOTC.Base\nopen import FOTC.Base.List\n\n------------------------------------------------------------------------------\n-- Congruence properties\n\n\u2237-leftCong : \u2200 {x y xs} \u2192 x \u2261 y \u2192 x \u2237 xs \u2261 y \u2237 xs\n\u2237-leftCong refl = refl\n\n\u2237-rightCong : \u2200 {x xs ys} \u2192 xs \u2261 ys \u2192 x \u2237 xs \u2261 x \u2237 ys\n\u2237-rightCong refl = refl\n\n\u2237-cong : \u2200 {x y xs ys} \u2192 x \u2261 y \u2192 xs \u2261 ys \u2192 x \u2237 xs \u2261 y \u2237 ys\n\u2237-cong refl refl = refl\n\nheadCong : \u2200 {xs ys} \u2192 xs \u2261 ys \u2192 head\u2081 xs \u2261 head\u2081 ys\nheadCong refl = refl\n\ntailCong : \u2200 {xs ys} \u2192 xs \u2261 ys \u2192 tail\u2081 xs \u2261 tail\u2081 ys\ntailCong refl = refl\n\n------------------------------------------------------------------------------\n-- Injective properties\n\n\u2237-injective : \u2200 {x y xs ys} \u2192 x \u2237 xs \u2261 y \u2237 ys \u2192 x \u2261 y \u2227 xs \u2261 ys\n\u2237-injective {x} {y} {xs} {ys} h = x\u2261y , xs\u2261ys\n where\n x\u2261y : x \u2261 y\n x\u2261y = x \u2261\u27e8 sym (head-\u2237 x xs) \u27e9\n head\u2081 (x \u2237 xs) \u2261\u27e8 headCong h \u27e9\n head\u2081 (y \u2237 ys) \u2261\u27e8 head-\u2237 y ys \u27e9\n y \u220e\n\n xs\u2261ys : xs \u2261 ys\n xs\u2261ys = xs \u2261\u27e8 sym (tail-\u2237 x xs) \u27e9\n tail\u2081 (x \u2237 xs) \u2261\u27e8 tailCong h \u27e9\n tail\u2081 (y \u2237 ys) \u2261\u27e8 tail-\u2237 y ys \u27e9\n ys \u220e\n","old_contents":"------------------------------------------------------------------------------\n-- FOTC list terms properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Base.List.PropertiesI where\n\nopen import Common.FOL.Relation.Binary.EqReasoning\n\nopen import FOTC.Base\nopen import FOTC.Base.List\n\n------------------------------------------------------------------------------\n-- Congruence properties\n\n\u2237-leftCong : \u2200 {x y xs} \u2192 x \u2261 y \u2192 x \u2237 xs \u2261 y \u2237 xs\n\u2237-leftCong refl = refl\n\n\u2237-rightCong : \u2200 {x xs ys} \u2192 xs \u2261 ys \u2192 x \u2237 xs \u2261 x \u2237 ys\n\u2237-rightCong refl = refl\n\n\u2237-Cong : \u2200 {x y xs ys} \u2192 x \u2261 y \u2192 xs \u2261 ys \u2192 x \u2237 xs \u2261 y \u2237 ys\n\u2237-Cong refl refl = refl\n\nheadCong : \u2200 {xs ys} \u2192 xs \u2261 ys \u2192 head\u2081 xs \u2261 head\u2081 ys\nheadCong refl = refl\n\ntailCong : \u2200 {xs ys} \u2192 xs \u2261 ys \u2192 tail\u2081 xs \u2261 tail\u2081 ys\ntailCong refl = refl\n\n------------------------------------------------------------------------------\n-- Injective properties\n\n\u2237-injective : \u2200 {x y xs ys} \u2192 x \u2237 xs \u2261 y \u2237 ys \u2192 x \u2261 y \u2227 xs \u2261 ys\n\u2237-injective {x} {y} {xs} {ys} h = x\u2261y , xs\u2261ys\n where\n x\u2261y : x \u2261 y\n x\u2261y = x \u2261\u27e8 sym (head-\u2237 x xs) \u27e9\n head\u2081 (x \u2237 xs) \u2261\u27e8 headCong h \u27e9\n head\u2081 (y \u2237 ys) \u2261\u27e8 head-\u2237 y ys \u27e9\n y \u220e\n\n xs\u2261ys : xs \u2261 ys\n xs\u2261ys = xs \u2261\u27e8 sym (tail-\u2237 x xs) \u27e9\n tail\u2081 (x \u2237 xs) \u2261\u27e8 tailCong h \u27e9\n tail\u2081 (y \u2237 ys) \u2261\u27e8 tail-\u2237 y ys \u27e9\n ys \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ed6d75a98df4fd8dc85536ae6f51df7b5b4a2ebc","subject":"Data.Two.Base: +\u2713\u2227\u00d7","message":"Data.Two.Base: +\u2713\u2227\u00d7\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Two\/Base.agda","new_file":"lib\/Data\/Two\/Base.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule Data.Two.Base where\n\nopen import Data.Zero\n\nopen import Data.Bool\n public\n hiding (if_then_else_)\n renaming (Bool to \ud835\udfda; false to 0\u2082; true to 1\u2082; T to \u2713)\n\nopen import Data.Product\n renaming (proj\u2081 to fst; proj\u2082 to snd)\n\nopen import Data.Sum\n using (_\u228e_) renaming (inj\u2081 to inl; inj\u2082 to inr)\n\nopen import Data.Nat.Base\n using (\u2115; _\u2264_; z\u2264n; s\u2264s; _\u2293_; _\u2294_; _\u2238_)\n\nopen import Type using (\u2605_)\n\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\n\nopen import Relation.Binary.PropositionalEquality\n using (_\u2261_; _\u2262_; refl)\n\n0\u22621\u2082 : 0\u2082 \u2262 1\u2082\n0\u22621\u2082 ()\n\n_\u00b2 : \u2200 {a} \u2192 \u2605 a \u2192 \u2605 a\nA \u00b2 = \ud835\udfda \u2192 A\n\nmodule _ {p} {P : \ud835\udfda \u2192 \u2605 p} where\n\n [0:_1:_] : P 0\u2082 \u2192 P 1\u2082 \u2192 (b : \ud835\udfda) \u2192 P b\n [0: e\u2080 1: e\u2081 ] 0\u2082 = e\u2080\n [0: e\u2080 1: e\u2081 ] 1\u2082 = e\u2081\n\n tabulate\u2082 : ((b : \ud835\udfda) \u2192 P b) \u2192 P 0\u2082 \u00d7 P 1\u2082\n tabulate\u2082 f = f 0\u2082 , f 1\u2082\n\n \u03b7-[0:1:] : \u2200 (f : (b : \ud835\udfda) \u2192 P b) b \u2192 [0: f 0\u2082 1: f 1\u2082 ] b \u2261 f b\n \u03b7-[0:1:] f 0\u2082 = refl\n \u03b7-[0:1:] f 1\u2082 = refl\n\n proj : P 0\u2082 \u00d7 P 1\u2082 \u2192 (b : \ud835\udfda) \u2192 P b\n proj = uncurry [0:_1:_]\n\n proj-tabulate\u2082 : \u2200 (f : (b : \ud835\udfda) \u2192 P b) b \u2192 proj (tabulate\u2082 f) b \u2261 f b\n proj-tabulate\u2082 = \u03b7-[0:1:]\n\nmodule _ {a} {A : \u2605 a} where\n\n [0:_1:_]\u2032 : A \u2192 A \u2192 A \u00b2\n [0:_1:_]\u2032 = [0:_1:_]\n\n case_0:_1:_ : \ud835\udfda \u2192 A \u2192 A \u2192 A\n case b 0: e\u2080 1: e\u2081 = [0: e\u2080\n 1: e\u2081 ] b\n\n proj\u2032 : A \u00d7 A \u2192 A \u00b2\n proj\u2032 = proj\n\n proj[_] : \ud835\udfda \u2192 A \u00d7 A \u2192 A\n proj[_] = [0: fst 1: snd ]\n\n mux : \ud835\udfda \u00d7 (A \u00d7 A) \u2192 A\n mux (s , e\u1d62) = proj e\u1d62 s\n\nnor : (b\u2080 b\u2081 : \ud835\udfda) \u2192 \ud835\udfda\nnor b\u2080 b\u2081 = not (b\u2080 \u2228 b\u2081)\n\nnand : (b\u2080 b\u2081 : \ud835\udfda) \u2192 \ud835\udfda\nnand b\u2080 b\u2081 = not (b\u2080 \u2227 b\u2081)\n\n-- For properties about _==_ see Data.Two.Equality\n_==_ : (b\u2080 b\u2081 : \ud835\udfda) \u2192 \ud835\udfda\nb\u2080 == b\u2081 = (not b\u2080) xor b\u2081\n\n\u2261\u2192\u2713 : \u2200 {b} \u2192 b \u2261 1\u2082 \u2192 \u2713 b\n\u2261\u2192\u2713 refl = _\n\n\u2261\u2192\u2713not : \u2200 {b} \u2192 b \u2261 0\u2082 \u2192 \u2713 (not b)\n\u2261\u2192\u2713not refl = _\n\n\u2713\u2192\u2261 : \u2200 {b} \u2192 \u2713 b \u2192 b \u2261 1\u2082\n\u2713\u2192\u2261 {1\u2082} _ = refl\n\u2713\u2192\u2261 {0\u2082} ()\n\n\u2713not\u2192\u2261 : \u2200 {b} \u2192 \u2713 (not b) \u2192 b \u2261 0\u2082\n\u2713not\u2192\u2261 {0\u2082} _ = refl\n\u2713not\u2192\u2261 {1\u2082} ()\n\n-- See also \u2713-\u2227\n\u2713\u2227 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 b\u2081 \u2192 \u2713 b\u2082 \u2192 \u2713 (b\u2081 \u2227 b\u2082)\n\u2713\u2227 {0\u2082} p q = p\n\u2713\u2227 {1\u2082} p q = q\n\n-- See also \u2713-\u2227\n\u2713\u2227\u2081 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 (b\u2081 \u2227 b\u2082) \u2192 \u2713 b\u2081\n\u2713\u2227\u2081 {0\u2082} ()\n\u2713\u2227\u2081 {1\u2082} = _\n\n\u2713\u2227\u2082 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 (b\u2081 \u2227 b\u2082) \u2192 \u2713 b\u2082\n\u2713\u2227\u2082 {0\u2082} ()\n\u2713\u2227\u2082 {1\u2082} p = p\n\n\u2713\u2227\u00d7 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 (b\u2081 \u2227 b\u2082) \u2192 \u2713 b\u2081 \u00d7 \u2713 b\u2082\n\u2713\u2227\u00d7 {0\u2082} ()\n\u2713\u2227\u00d7 {1\u2082} p = _ , p\n\n-- Similar to \u2713-\u2228\n\u2713\u2228-\u228e : \u2200 {b\u2081 b\u2082} \u2192 \u2713 (b\u2081 \u2228 b\u2082) \u2192 \u2713 b\u2081 \u228e \u2713 b\u2082\n\u2713\u2228-\u228e {0\u2082} = inr\n\u2713\u2228-\u228e {1\u2082} = inl\n\n-- Similar to \u2713-\u2228\n\u2713\u2228\u2081 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 b\u2081 \u2192 \u2713 (b\u2081 \u2228 b\u2082)\n\u2713\u2228\u2081 {1\u2082} = _\n\u2713\u2228\u2081 {0\u2082} ()\n\n-- Similar to \u2713-\u2228\n\u2713\u2228\u2082 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 b\u2082 \u2192 \u2713 (b\u2081 \u2228 b\u2082)\n\u2713\u2228\u2082 {0\u2082} p = p\n\u2713\u2228\u2082 {1\u2082} _ = _\n\n\u2713-not-\u00ac : \u2200 {b} \u2192 \u2713 (not b) \u2192 \u00ac (\u2713 b)\n\u2713-not-\u00ac {0\u2082} _ = \u03bb()\n\u2713-not-\u00ac {1\u2082} ()\n\n\u2713-\u00ac-not : \u2200 {b} \u2192 \u00ac (\u2713 b) \u2192 \u2713 (not b)\n\u2713-\u00ac-not {0\u2082} _ = _\n\u2713-\u00ac-not {1\u2082} f = f _\n\n\u2227\u21d2\u2228 : \u2200 x y \u2192 \u2713 (x \u2227 y) \u2192 \u2713 (x \u2228 y)\n\u2227\u21d2\u2228 0\u2082 _ = \u03bb ()\n\u2227\u21d2\u2228 1\u2082 _ = _\n\n-- This particular implementation has been\n-- chosen for computational content.\n-- Namely the proof is \"re-created\" when b is 1\u2082.\ncheck : \u2200 b \u2192 {pf : \u2713 b} \u2192 \u2713 b\ncheck 0\u2082 {}\ncheck 1\u2082 = _\n\n\u2713dec : \u2200 b \u2192 Dec (\u2713 b)\n\u2713dec = [0: no (\u03bb())\n 1: yes _ ]\n\nde-morgan : \u2200 x y \u2192 not (x \u2228 y) \u2261 not x \u2227 not y\nde-morgan 0\u2082 _ = refl\nde-morgan 1\u2082 _ = refl\n\n\u22620\u2192\u22611 : \u2200 {x} \u2192 x \u2262 0\u2082 \u2192 x \u2261 1\u2082\n\u22620\u2192\u22611 {1\u2082} p = refl\n\u22620\u2192\u22611 {0\u2082} p = \ud835\udfd8-elim (p refl)\n\n\u22621\u2192\u22610 : \u2200 {x} \u2192 x \u2262 1\u2082 \u2192 x \u2261 0\u2082\n\u22621\u2192\u22610 {0\u2082} p = refl\n\u22621\u2192\u22610 {1\u2082} p = \ud835\udfd8-elim (p refl)\n\n-- 0\u2082 is 0 and 1\u2082 is 1\n\ud835\udfda\u25b9\u2115 : \ud835\udfda \u2192 \u2115\n\ud835\udfda\u25b9\u2115 = [0: 0\n 1: 1 ]\n\n\ud835\udfda\u25b9\u2115\u22641 : \u2200 b \u2192 \ud835\udfda\u25b9\u2115 b \u2264 1\n\ud835\udfda\u25b9\u2115\u22641 = [0: z\u2264n\n 1: s\u2264s z\u2264n ]\n\n\ud835\udfda\u25b9\u2115-\u2293 : \u2200 a b \u2192 \ud835\udfda\u25b9\u2115 a \u2293 \ud835\udfda\u25b9\u2115 b \u2261 \ud835\udfda\u25b9\u2115 (a \u2227 b)\n\ud835\udfda\u25b9\u2115-\u2293 1\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2293 1\u2082 1\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2293 0\u2082 _ = refl\n\n\ud835\udfda\u25b9\u2115-\u2294 : \u2200 a b \u2192 \ud835\udfda\u25b9\u2115 a \u2294 \ud835\udfda\u25b9\u2115 b \u2261 \ud835\udfda\u25b9\u2115 (a \u2228 b)\n\ud835\udfda\u25b9\u2115-\u2294 1\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2294 1\u2082 1\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2294 0\u2082 _ = refl\n\n\ud835\udfda\u25b9\u2115-\u2238 : \u2200 a b \u2192 \ud835\udfda\u25b9\u2115 a \u2238 \ud835\udfda\u25b9\u2115 b \u2261 \ud835\udfda\u25b9\u2115 (a \u2227 not b)\n\ud835\udfda\u25b9\u2115-\u2238 0\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2238 0\u2082 1\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2238 1\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2238 1\u2082 1\u2082 = refl\n\nnot-inj : \u2200 {x y} \u2192 not x \u2261 not y \u2192 x \u2261 y\nnot-inj {0\u2082} {0\u2082} _ = refl\nnot-inj {1\u2082} {1\u2082} _ = refl\nnot-inj {0\u2082} {1\u2082} ()\nnot-inj {1\u2082} {0\u2082} ()\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule Data.Two.Base where\n\nopen import Data.Zero\n\nopen import Data.Bool\n public\n hiding (if_then_else_)\n renaming (Bool to \ud835\udfda; false to 0\u2082; true to 1\u2082; T to \u2713)\n\nopen import Data.Product\n renaming (proj\u2081 to fst; proj\u2082 to snd)\n\nopen import Data.Sum\n using (_\u228e_) renaming (inj\u2081 to inl; inj\u2082 to inr)\n\nopen import Data.Nat.Base\n using (\u2115; _\u2264_; z\u2264n; s\u2264s; _\u2293_; _\u2294_; _\u2238_)\n\nopen import Type using (\u2605_)\n\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\n\nopen import Relation.Binary.PropositionalEquality\n using (_\u2261_; _\u2262_; refl)\n\n0\u22621\u2082 : 0\u2082 \u2262 1\u2082\n0\u22621\u2082 ()\n\n_\u00b2 : \u2200 {a} \u2192 \u2605 a \u2192 \u2605 a\nA \u00b2 = \ud835\udfda \u2192 A\n\nmodule _ {p} {P : \ud835\udfda \u2192 \u2605 p} where\n\n [0:_1:_] : P 0\u2082 \u2192 P 1\u2082 \u2192 (b : \ud835\udfda) \u2192 P b\n [0: e\u2080 1: e\u2081 ] 0\u2082 = e\u2080\n [0: e\u2080 1: e\u2081 ] 1\u2082 = e\u2081\n\n tabulate\u2082 : ((b : \ud835\udfda) \u2192 P b) \u2192 P 0\u2082 \u00d7 P 1\u2082\n tabulate\u2082 f = f 0\u2082 , f 1\u2082\n\n \u03b7-[0:1:] : \u2200 (f : (b : \ud835\udfda) \u2192 P b) b \u2192 [0: f 0\u2082 1: f 1\u2082 ] b \u2261 f b\n \u03b7-[0:1:] f 0\u2082 = refl\n \u03b7-[0:1:] f 1\u2082 = refl\n\n proj : P 0\u2082 \u00d7 P 1\u2082 \u2192 (b : \ud835\udfda) \u2192 P b\n proj = uncurry [0:_1:_]\n\n proj-tabulate\u2082 : \u2200 (f : (b : \ud835\udfda) \u2192 P b) b \u2192 proj (tabulate\u2082 f) b \u2261 f b\n proj-tabulate\u2082 = \u03b7-[0:1:]\n\nmodule _ {a} {A : \u2605 a} where\n\n [0:_1:_]\u2032 : A \u2192 A \u2192 A \u00b2\n [0:_1:_]\u2032 = [0:_1:_]\n\n case_0:_1:_ : \ud835\udfda \u2192 A \u2192 A \u2192 A\n case b 0: e\u2080 1: e\u2081 = [0: e\u2080\n 1: e\u2081 ] b\n\n proj\u2032 : A \u00d7 A \u2192 A \u00b2\n proj\u2032 = proj\n\n proj[_] : \ud835\udfda \u2192 A \u00d7 A \u2192 A\n proj[_] = [0: fst 1: snd ]\n\n mux : \ud835\udfda \u00d7 (A \u00d7 A) \u2192 A\n mux (s , e\u1d62) = proj e\u1d62 s\n\nnor : (b\u2080 b\u2081 : \ud835\udfda) \u2192 \ud835\udfda\nnor b\u2080 b\u2081 = not (b\u2080 \u2228 b\u2081)\n\nnand : (b\u2080 b\u2081 : \ud835\udfda) \u2192 \ud835\udfda\nnand b\u2080 b\u2081 = not (b\u2080 \u2227 b\u2081)\n\n-- For properties about _==_ see Data.Two.Equality\n_==_ : (b\u2080 b\u2081 : \ud835\udfda) \u2192 \ud835\udfda\nb\u2080 == b\u2081 = (not b\u2080) xor b\u2081\n\n\u2261\u2192\u2713 : \u2200 {b} \u2192 b \u2261 1\u2082 \u2192 \u2713 b\n\u2261\u2192\u2713 refl = _\n\n\u2261\u2192\u2713not : \u2200 {b} \u2192 b \u2261 0\u2082 \u2192 \u2713 (not b)\n\u2261\u2192\u2713not refl = _\n\n\u2713\u2192\u2261 : \u2200 {b} \u2192 \u2713 b \u2192 b \u2261 1\u2082\n\u2713\u2192\u2261 {1\u2082} _ = refl\n\u2713\u2192\u2261 {0\u2082} ()\n\n\u2713not\u2192\u2261 : \u2200 {b} \u2192 \u2713 (not b) \u2192 b \u2261 0\u2082\n\u2713not\u2192\u2261 {0\u2082} _ = refl\n\u2713not\u2192\u2261 {1\u2082} ()\n\n-- See also \u2713-\u2227\n\u2713\u2227 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 b\u2081 \u2192 \u2713 b\u2082 \u2192 \u2713 (b\u2081 \u2227 b\u2082)\n\u2713\u2227 {0\u2082} p q = p\n\u2713\u2227 {1\u2082} p q = q\n\n-- See also \u2713-\u2227\n\u2713\u2227\u2081 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 (b\u2081 \u2227 b\u2082) \u2192 \u2713 b\u2081\n\u2713\u2227\u2081 {0\u2082} ()\n\u2713\u2227\u2081 {1\u2082} = _\n\n\u2713\u2227\u2082 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 (b\u2081 \u2227 b\u2082) \u2192 \u2713 b\u2082\n\u2713\u2227\u2082 {0\u2082} ()\n\u2713\u2227\u2082 {1\u2082} p = p\n\n-- Similar to \u2713-\u2228\n\u2713\u2228-\u228e : \u2200 {b\u2081 b\u2082} \u2192 \u2713 (b\u2081 \u2228 b\u2082) \u2192 \u2713 b\u2081 \u228e \u2713 b\u2082\n\u2713\u2228-\u228e {0\u2082} = inr\n\u2713\u2228-\u228e {1\u2082} = inl\n\n-- Similar to \u2713-\u2228\n\u2713\u2228\u2081 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 b\u2081 \u2192 \u2713 (b\u2081 \u2228 b\u2082)\n\u2713\u2228\u2081 {1\u2082} = _\n\u2713\u2228\u2081 {0\u2082} ()\n\n-- Similar to \u2713-\u2228\n\u2713\u2228\u2082 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 b\u2082 \u2192 \u2713 (b\u2081 \u2228 b\u2082)\n\u2713\u2228\u2082 {0\u2082} p = p\n\u2713\u2228\u2082 {1\u2082} _ = _\n\n\u2713-not-\u00ac : \u2200 {b} \u2192 \u2713 (not b) \u2192 \u00ac (\u2713 b)\n\u2713-not-\u00ac {0\u2082} _ = \u03bb()\n\u2713-not-\u00ac {1\u2082} ()\n\n\u2713-\u00ac-not : \u2200 {b} \u2192 \u00ac (\u2713 b) \u2192 \u2713 (not b)\n\u2713-\u00ac-not {0\u2082} _ = _\n\u2713-\u00ac-not {1\u2082} f = f _\n\n\u2227\u21d2\u2228 : \u2200 x y \u2192 \u2713 (x \u2227 y) \u2192 \u2713 (x \u2228 y)\n\u2227\u21d2\u2228 0\u2082 _ = \u03bb ()\n\u2227\u21d2\u2228 1\u2082 _ = _\n\n-- This particular implementation has been\n-- chosen for computational content.\n-- Namely the proof is \"re-created\" when b is 1\u2082.\ncheck : \u2200 b \u2192 {pf : \u2713 b} \u2192 \u2713 b\ncheck 0\u2082 {}\ncheck 1\u2082 = _\n\n\u2713dec : \u2200 b \u2192 Dec (\u2713 b)\n\u2713dec = [0: no (\u03bb())\n 1: yes _ ]\n\nde-morgan : \u2200 x y \u2192 not (x \u2228 y) \u2261 not x \u2227 not y\nde-morgan 0\u2082 _ = refl\nde-morgan 1\u2082 _ = refl\n\n\u22620\u2192\u22611 : \u2200 {x} \u2192 x \u2262 0\u2082 \u2192 x \u2261 1\u2082\n\u22620\u2192\u22611 {1\u2082} p = refl\n\u22620\u2192\u22611 {0\u2082} p = \ud835\udfd8-elim (p refl)\n\n\u22621\u2192\u22610 : \u2200 {x} \u2192 x \u2262 1\u2082 \u2192 x \u2261 0\u2082\n\u22621\u2192\u22610 {0\u2082} p = refl\n\u22621\u2192\u22610 {1\u2082} p = \ud835\udfd8-elim (p refl)\n\n-- 0\u2082 is 0 and 1\u2082 is 1\n\ud835\udfda\u25b9\u2115 : \ud835\udfda \u2192 \u2115\n\ud835\udfda\u25b9\u2115 = [0: 0\n 1: 1 ]\n\n\ud835\udfda\u25b9\u2115\u22641 : \u2200 b \u2192 \ud835\udfda\u25b9\u2115 b \u2264 1\n\ud835\udfda\u25b9\u2115\u22641 = [0: z\u2264n\n 1: s\u2264s z\u2264n ]\n\n\ud835\udfda\u25b9\u2115-\u2293 : \u2200 a b \u2192 \ud835\udfda\u25b9\u2115 a \u2293 \ud835\udfda\u25b9\u2115 b \u2261 \ud835\udfda\u25b9\u2115 (a \u2227 b)\n\ud835\udfda\u25b9\u2115-\u2293 1\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2293 1\u2082 1\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2293 0\u2082 _ = refl\n\n\ud835\udfda\u25b9\u2115-\u2294 : \u2200 a b \u2192 \ud835\udfda\u25b9\u2115 a \u2294 \ud835\udfda\u25b9\u2115 b \u2261 \ud835\udfda\u25b9\u2115 (a \u2228 b)\n\ud835\udfda\u25b9\u2115-\u2294 1\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2294 1\u2082 1\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2294 0\u2082 _ = refl\n\n\ud835\udfda\u25b9\u2115-\u2238 : \u2200 a b \u2192 \ud835\udfda\u25b9\u2115 a \u2238 \ud835\udfda\u25b9\u2115 b \u2261 \ud835\udfda\u25b9\u2115 (a \u2227 not b)\n\ud835\udfda\u25b9\u2115-\u2238 0\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2238 0\u2082 1\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2238 1\u2082 0\u2082 = refl\n\ud835\udfda\u25b9\u2115-\u2238 1\u2082 1\u2082 = refl\n\nnot-inj : \u2200 {x y} \u2192 not x \u2261 not y \u2192 x \u2261 y\nnot-inj {0\u2082} {0\u2082} _ = refl\nnot-inj {1\u2082} {1\u2082} _ = refl\nnot-inj {0\u2082} {1\u2082} ()\nnot-inj {1\u2082} {0\u2082} ()\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"ff3903157144200af948a03571b132a3eabed199","subject":"Weaken the \u2219-opt assumption! Adapt generic-zero-knowledge-interactive to new sums","message":"Weaken the \u2219-opt assumption! Adapt generic-zero-knowledge-interactive to new sums\n","repos":"crypto-agda\/crypto-agda","old_file":"generic-zero-knowledge-interactive.agda","new_file":"generic-zero-knowledge-interactive.agda","new_contents":"open import Data.Bool.NP as Bool hiding (check)\nopen import Data.Nat\nopen import Data.Maybe\nopen import Data.Product.NP\nopen import Data.Bits\nopen import Function.NP\nopen import Relation.Binary.PropositionalEquality.NP\n\nopen import sum\n\nmodule generic-zero-knowledge-interactive where\n\nprivate\n \u2605 : Set\u2081\n \u2605 = Set\n\n-- A random argument, this is only a formal notation to\n-- indicate that the argument is supposed to be picked\n-- at random uniformly. (do not confuse with our randomness\n-- monad).\nrecord \u21ba (A : \u2605) : \u2605 where\n constructor rand\n field get : A\n\nmodule M (Permutation : \u2605)\n (_\u207b\u00b9 : Endo Permutation)\n (sum\u03c0 : Sum Permutation)\n (\u03bc\u03c0 : SumProp sum\u03c0)\n\n (R\u209a-xtra : \u2605) -- extra prover\/adversary randomness\n (sumR\u209a-xtra : Sum R\u209a-xtra)\n (\u03bcR\u209a-xtra : SumProp sumR\u209a-xtra)\n\n (Problem : \u2605)\n (_==_ : Problem \u2192 Problem \u2192 Bit)\n (==-refl : \u2200 {pb} \u2192 (pb == pb) \u2261 true)\n (_\u2219P_ : Permutation \u2192 Endo Problem)\n (\u207b\u00b9-inverseP : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219P (\u03c0 \u2219P x) \u2261 x)\n\n (Solution : \u2605)\n (_\u2219S_ : Permutation \u2192 Endo Solution)\n (\u207b\u00b9-inverseS : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219S (\u03c0 \u2219S x) \u2261 x)\n\n (check : Problem \u2192 Solution \u2192 Bit)\n (check-\u2219 : \u2200 p s \u03c0 \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 check p s)\n\n (easy-pb : Permutation \u2192 Problem)\n (easy-sol : Permutation \u2192 Solution)\n (check-easy : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P easy-pb \u03c0) (\u03c0 \u2219S easy-sol \u03c0) \u2261 true)\n where\n\n -- prover\/adversary randomness\n R\u209a : \u2605\n R\u209a = Permutation \u00d7 R\u209a-xtra\n\n sumR\u209a : Sum R\u209a\n sumR\u209a = sum\u03c0 \u00d7Sum sumR\u209a-xtra\n\n \u03bcR\u209a : SumProp sumR\u209a\n \u03bcR\u209a = \u03bc\u03c0 \u00d7\u03bc \u03bcR\u209a-xtra\n\n R = Bit \u00d7 R\u209a\n\n sumR : Sum R\n sumR = sumBit \u00d7Sum sumR\u209a\n\n \u03bcR : SumProp sumR\n \u03bcR = \u03bcBit \u00d7\u03bc \u03bcR\u209a\n\n check-\u03c0 : Problem \u2192 Solution \u2192 R\u209a \u2192 Bit\n check-\u03c0 p s (\u03c0 , _) = check (\u03c0 \u2219P p) (\u03c0 \u2219S s)\n\n otp-\u2219-check : let #_ = count \u03bcR\u209a\n in\n \u2200 p\u2080 s\u2080 p\u2081 s\u2081 \u2192\n check p\u2080 s\u2080 \u2261 check p\u2081 s\u2081 \u2192\n #(check-\u03c0 p\u2080 s\u2080) \u2261 #(check-\u03c0 p\u2081 s\u2081)\n otp-\u2219-check p\u2080 s\u2080 p\u2081 s\u2081 check-pf =\n count-ext \u03bcR\u209a {f = check-\u03c0 p\u2080 s\u2080} {check-\u03c0 p\u2081 s\u2081} (\u03bb \u03c0,r \u2192\n check-\u03c0 p\u2080 s\u2080 \u03c0,r \u2261\u27e8 check-\u2219 p\u2080 s\u2080 (proj\u2081 \u03c0,r) \u27e9\n check p\u2080 s\u2080 \u2261\u27e8 check-pf \u27e9\n check p\u2081 s\u2081 \u2261\u27e8 sym (check-\u2219 p\u2081 s\u2081 (proj\u2081 \u03c0,r)) \u27e9\n check-\u03c0 p\u2081 s\u2081 \u03c0,r \u220e)\n where open \u2261-Reasoning\n\n #_ : (\u21ba (Bit \u00d7 Permutation \u00d7 R\u209a-xtra) \u2192 Bit) \u2192 \u2115\n # f = count \u03bcR (f \u2218 rand)\n\n _\u2261#_ : (f g : \u21ba (Bit \u00d7 R\u209a) \u2192 Bit) \u2192 \u2605\n f \u2261# g = # f \u2261 # g\n\n{-\n otp-\u2219 : let otp = \u03bb O pb s \u2192 count \u03bcR\u209a (\u03bb { (\u03c0 , _) \u2192 O (\u03c0 \u2219P pb) (\u03c0 \u2219S s) }) in\n \u2200 pb\u2080 s\u2080 pb\u2081 s\u2081 \u2192\n check pb\u2080 s\u2080 \u2261 check pb\u2081 s\u2081 \u2192\n (O : _ \u2192 _ \u2192 Bit) \u2192 otp O pb\u2080 s\u2080 \u2261 otp O pb\u2081 s\u2081\n otp-\u2219 pb\u2080 s\u2080 pb\u2081 s\u2081 check-pf O = {!(\u03bc\u03c0 \u00d7Sum-proj\u2082 \u03bcR\u209a-xtra ?!}\n-}\n Answer : Bit \u2192 \u2605\n Answer false{-0b-} = Permutation\n Answer true {-1b-} = Solution\n\n answer : Permutation \u2192 Solution \u2192 \u2200 b \u2192 Answer b\n answer \u03c0 _ false = \u03c0\n answer _ s true = s\n\n -- The prover is the advesary in the generic terminology,\n -- and the verifier is the challenger.\n DepProver : \u2605\n DepProver = Problem \u2192 \u21ba R\u209a \u2192 (b : Bit) \u2192 Problem \u00d7 Answer b\n\n Prover\u2080 : \u2605\n Prover\u2080 = Problem \u2192 \u21ba R\u209a \u2192 Problem \u00d7 Permutation\n\n Prover\u2081 : \u2605\n Prover\u2081 = Problem \u2192 \u21ba R\u209a \u2192 Problem \u00d7 Solution\n\n Prover : \u2605\n Prover = Prover\u2080 \u00d7 Prover\u2081\n\n prover : DepProver \u2192 Prover\n prover dpr = (\u03bb pb r \u2192 dpr pb r 0b) , (\u03bb pb r \u2192 dpr pb r 1b)\n\n depProver : Prover \u2192 DepProver\n depProver (pr\u2080 , pr\u2081) pb r false = pr\u2080 pb r\n depProver (pr\u2080 , pr\u2081) pb r true = pr\u2081 pb r\n\n -- Here we show that the explicit commitment step seems useless given\n -- the formalization. The verifier can \"trust\" the prover on the fact\n -- that any choice is going to be govern only by the problem and the\n -- randomness.\n module WithCommitment (Commitment : \u2605)\n (AnswerWC : Bit \u2192 \u2605)\n (reveal : \u2200 b \u2192 Commitment \u2192 AnswerWC b \u2192 Problem \u00d7 Answer b) where\n ProverWC = (Problem \u2192 R\u209a \u2192 Commitment)\n \u00d7 (Problem \u2192 R\u209a \u2192 (b : Bit) \u2192 AnswerWC b)\n\n depProver' : ProverWC \u2192 DepProver\n depProver' (pr\u2080 , pr\u2081) pb (rand r\u209a) b = reveal b (pr\u2080 pb r\u209a) (pr\u2081 pb r\u209a b)\n\n Verif : Problem \u2192 \u2200 b \u2192 Problem \u00d7 Answer b \u2192 Bit\n Verif pb false{-0b-} (\u03c0\u2219pb , \u03c0) = (\u03c0 \u2219P pb) == \u03c0\u2219pb\n Verif pb true {-1b-} (\u03c0\u2219pb , \u03c0\u2219s) = check \u03c0\u2219pb \u03c0\u2219s\n\n _\u21c4\u2032_ : Problem \u2192 DepProver \u2192 Bit \u2192 \u21ba R\u209a \u2192 Bit\n (pb \u21c4\u2032 pr) b (rand r\u209a) = Verif pb b (pr pb (rand r\u209a) b)\n\n _\u21c4_ : Problem \u2192 DepProver \u2192 \u21ba (Bit \u00d7 R\u209a) \u2192 Bit\n (pb \u21c4 pr) (rand (b , r\u209a)) = (pb \u21c4\u2032 pr) b (rand r\u209a)\n\n _\u21c4''_ : Problem \u2192 Prover \u2192 \u21ba (Bit \u00d7 R\u209a) \u2192 Bit\n pb \u21c4'' pr = pb \u21c4 depProver pr\n\n honest : (Problem \u2192 Maybe Solution) \u2192 DepProver\n honest solve pb (rand (\u03c0 , r\u209a)) b = (\u03c0 \u2219P pb , answer \u03c0 sol b)\n module Honest where\n sol : Solution\n sol with solve pb\n ... | just sol = \u03c0 \u2219S sol\n ... | nothing = \u03c0 \u2219S easy-sol \u03c0\n\n module WithCorrectSolver (pb : Problem)\n (s : Solution)\n (check-s : check pb s \u2261 true)\n where\n\n -- When the honest prover has a solution, he gets accepted\n -- unconditionally by the verifier.\n honest-accepted : \u2200 r \u2192 (pb \u21c4 honest (const (just s))) r \u2261 1b\n honest-accepted (rand (true , \u03c0 , r\u209a)) rewrite check-\u2219 pb s \u03c0 = check-s\n honest-accepted (rand (false , \u03c0 , r\u209a)) = ==-refl\n\n honest-\u2141 = \u03bb pb s \u2192 (pb \u21c4 honest (const (just s)))\n\n module HonestLeakZeroKnowledge (pb\u2080 pb\u2081 : Problem)\n (s\u2080 s\u2081 : Solution)\n (check-pf : check pb\u2080 s\u2080 \u2261 check pb\u2081 s\u2081) where\n\n helper : \u2200 r\u209a \u2192 Bool.to\u2115 ((pb\u2080 \u21c4\u2032 honest (const (just s\u2080))) 0b (rand r\u209a))\n \u2261 Bool.to\u2115 ((pb\u2081 \u21c4\u2032 honest (const (just s\u2081))) 0b (rand r\u209a))\n helper (\u03c0 , r\u209a) rewrite ==-refl {\u03c0 \u2219P pb\u2080} | ==-refl {\u03c0 \u2219P pb\u2081} = refl\n\n honest-leak : honest-\u2141 pb\u2080 s\u2080 \u2261# honest-\u2141 pb\u2081 s\u2081\n honest-leak rewrite otp-\u2219-check pb\u2080 s\u2080 pb\u2081 s\u2081 check-pf | sum-ext \u03bcR\u209a helper = refl\n\n module HonestLeakZeroKnowledge' (pb : Problem)\n (s\u2080 s\u2081 : Solution)\n (check-pf : check pb s\u2080 \u2261 check pb s\u2081) where\n\n honest-leak : honest-\u2141 pb s\u2080 \u2261# honest-\u2141 pb s\u2081\n honest-leak = HonestLeakZeroKnowledge.honest-leak pb pb s\u2080 s\u2081 check-pf\n\n -- Predicts b=b\u2032\n cheater : \u2200 b\u2032 \u2192 DepProver\n cheater b\u2032 pb (rand (\u03c0 , _)) b = \u03c0 \u2219P (case b\u2032 0\u2192 pb 1\u2192 easy-pb \u03c0)\n , answer \u03c0 (\u03c0 \u2219S easy-sol \u03c0) b\n\n -- If cheater predicts correctly, verifer accepts him\n cheater-accepted : \u2200 b pb r\u209a \u2192 (pb \u21c4\u2032 cheater b) b r\u209a \u2261 1b\n cheater-accepted true pb (rand (\u03c0 , r\u209a)) = check-easy \u03c0\n cheater-accepted false pb (rand (\u03c0 , r\u209a)) = ==-refl\n\n -- If cheater predicts incorrecty, verifier rejects him\n module CheaterRejected (pb : Problem)\n (not-easy-sol : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P pb) (\u03c0 \u2219S easy-sol \u03c0) \u2261 false)\n (not-easy-pb : \u2200 \u03c0 \u2192 ((\u03c0 \u2219P pb) == (\u03c0 \u2219P easy-pb \u03c0)) \u2261 false) where\n\n cheater-rejected : \u2200 b r\u209a \u2192 (pb \u21c4\u2032 cheater (not b)) b r\u209a \u2261 0b\n cheater-rejected true (rand (\u03c0 , r\u209a)) = not-easy-sol \u03c0\n cheater-rejected false (rand (\u03c0 , r\u209a)) = not-easy-pb \u03c0\n\nmodule DLog (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u229f_ : \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n (\u229f-\u229e : \u2200 \u03c0 x \u2192 (\u229f \u03c0) \u229e (\u03c0 \u229e x) \u2261 x)\n (^\u229f-\u2219 : \u2200 \u03b1 \u03b2 x \u2192 ((\u03b1 ^ (\u229f x)) \u2219 ((\u03b1 ^ x) \u2219 \u03b2)) \u2261 \u03b2)\n -- (\u2219-assoc : \u2200 \u03b1 \u03b2 \u03b3 \u2192 \u03b1 \u2219 (\u03b2 \u2219 \u03b3) \u2261 (\u03b1 \u2219 \u03b2) \u2219 \u03b3)\n (dist-^-\u229e : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u229e y) \u2261 (\u03b1 ^ x) \u2219 (\u03b1 ^ y))\n (_==_ : G \u2192 G \u2192 Bool)\n (==-refl : \u2200 {\u03b1} \u2192 (\u03b1 == \u03b1) \u2261 true)\n (==-true : \u2200 {\u03b1 \u03b2} \u2192 \u03b1 == \u03b2 \u2261 true \u2192 \u03b1 \u2261 \u03b2)\n (==-false : \u2200 {\u03b1 \u03b2} \u2192 \u03b1 == \u03b2 \u2261 false \u2192 \u03b1 \u2262 \u03b2)\n (sum\u2124q : Sum \u2124q)\n (\u03bc\u2124q : SumProp sum\u2124q)\n (R\u209a-xtra : \u2605) -- extra prover\/adversary randomness\n (sumR\u209a-xtra : Sum R\u209a-xtra)\n (\u03bcR\u209a-xtra : SumProp sumR\u209a-xtra)\n (some-\u2124q : \u2124q)\n where\n\n Permutation = \u2124q\n Problem = G\n Solution = \u2124q\n\n _\u207b\u00b9 : Endo Permutation\n \u03c0 \u207b\u00b9 = \u229f \u03c0\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n _\u2219P_ : Permutation \u2192 Endo Problem\n \u03c0 \u2219P p = g^ \u03c0 \u2219 p\n\n \u207b\u00b9-inverseP : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219P (\u03c0 \u2219P x) \u2261 x\n \u207b\u00b9-inverseP \u03c0 x rewrite ^\u229f-\u2219 g x \u03c0 = refl\n\n _\u2219S_ : Permutation \u2192 Endo Solution\n \u03c0 \u2219S s = \u03c0 \u229e s\n\n \u207b\u00b9-inverseS : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219S (\u03c0 \u2219S x) \u2261 x\n \u207b\u00b9-inverseS = \u229f-\u229e\n\n check : Problem \u2192 Solution \u2192 Bit\n check p s = p == g^ s\n\n check-\u2219' : \u2200 p s \u03c0 b \u2192 check p s \u2261 b \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 b\n check-\u2219' p s \u03c0 true check-p-s rewrite dist-^-\u229e g \u03c0 s | ==-true check-p-s = ==-refl\n check-\u2219' p s \u03c0 false check-p-s rewrite dist-^-\u229e g \u03c0 s\n with ==-false check-p-s ?\n ... | ()\n\n check-\u2219 : \u2200 p s \u03c0 \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 check p s\n check-\u2219 p s \u03c0 = check-\u2219' p s \u03c0 (check p s) refl\n\n easy-sol : Permutation \u2192 Solution\n easy-sol \u03c0 = some-\u2124q\n\n easy-pb : Permutation \u2192 Problem\n easy-pb \u03c0 = g^(easy-sol \u03c0)\n\n check-easy : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P easy-pb \u03c0) (\u03c0 \u2219S easy-sol \u03c0) \u2261 true\n check-easy \u03c0 rewrite dist-^-\u229e g \u03c0 (easy-sol \u03c0) = ==-refl\n\n open M Permutation _\u207b\u00b9 sum\u2124q \u03bc\u2124q R\u209a-xtra sumR\u209a-xtra \u03bcR\u209a-xtra\n Problem _==_ ==-refl _\u2219P_ \u207b\u00b9-inverseP Solution _\u2219S_ \u207b\u00b9-inverseS check check-\u2219 easy-pb easy-sol check-easy\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"open import Data.Bool.NP as Bool\nopen import Data.Nat\nopen import Data.Maybe\nopen import Data.Product.NP\nopen import Data.Bits\nopen import Function.NP\nopen import Relation.Binary.PropositionalEquality\n\nopen import sum\n\nmodule generic-zero-knowledge-interactive where\n\nprivate\n \u2605 : Set\u2081\n \u2605 = Set\n\n-- record VerifiableProblem :\n\n-- A random argument, this is only a formal notation to\n-- indicate that the argument is supposed to be picked\n-- at random uniformly. (do not confuse with our randomness\n-- monad).\nrecord \u21ba (A : \u2605) : \u2605 where\n constructor rand\n field get : A\n\n\u27e80,\u27e9 : \u2200 {A} \u2192 \u21ba A \u2192 \u21ba (Bit \u00d7 A)\n\u27e80,\u27e9 (rand x) = rand (0b , x)\n\n\u27e81,\u27e9 : \u2200 {A} \u2192 \u21ba A \u2192 \u21ba (Bit \u00d7 A)\n\u27e81,\u27e9 (rand x) = rand (1b , x)\n\nmodule G (Permutation : \u2605)\n (sum\u03c0 : Sum Permutation)\n (sum\u03c0-ext : SumExt sum\u03c0)\n -- (_\u207b\u00b9 : Endo Permutation)\n\n (R\u209a-xtra : \u2605) -- extra prover\/adversary randomness\n (sumR\u209a-xtra : Sum R\u209a-xtra)\n (sumR\u209a-xtra-ext : SumExt sumR\u209a-xtra)\n\n (Problem : \u2605)\n (_==_ : Problem \u2192 Problem \u2192 Bit)\n (==-refl : \u2200 {pb} \u2192 (pb == pb) \u2261 true)\n (_\u2219P_ : Permutation \u2192 Endo Problem)\n\n (Solution : \u2605)\n (_\u2219S_ : Permutation \u2192 Endo Solution)\n\n (otp-\u2219 : let otp = \u03bb A pb s \u2192 (sumToCount (sumProd sum\u03c0 sumR\u209a-xtra)) (\u03bb { (\u03c0 , _) \u2192 A (\u03c0 \u2219P pb) (\u03c0 \u2219S s) }) in\n \u2200 pb\u2080 s\u2080 pb\u2081 s\u2081 (A : _ \u2192 _ \u2192 Bit) \u2192 otp A pb\u2080 s\u2080 \u2261 otp A pb\u2081 s\u2081)\n {-\n -- is this enough\n (otp-\u2219\u2032 : let otp = \u03bb A pb s \u2192 sum\u03c0 (\u03bb \u03c0 \u2192 A (\u03c0 \u2219P pb) (\u03c0 \u2219S s)) in\n \u2200 pb\u2080 s\u2080 pb\u2081 s\u2081 (A : _ \u2192 _ \u2192 \u2115) \u2192 otp A pb\u2080 s\u2080 \u2261 otp A pb\u2081 s\u2081)\n -}\n\n (check : Problem \u2192 Solution \u2192 Bit)\n (check-\u2219 : \u2200 p s \u03c0 \u2192 check p s \u2261 check (\u03c0 \u2219P p) (\u03c0 \u2219S s))\n\n (easy-pb : Permutation \u2192 Problem)\n (easy-sol : Permutation \u2192 Solution)\n (check-easy : \u2200 \u03c0 \u2192 check (easy-pb \u03c0) (easy-sol \u03c0) \u2261 true)\n where\n\n -- prover\/adversary randomness\n R\u209a : \u2605\n R\u209a = Permutation \u00d7 R\u209a-xtra\n\n sumR\u209a : Sum R\u209a\n sumR\u209a = sumProd sum\u03c0 sumR\u209a-xtra\n\n #R\u209a : Count R\u209a\n #R\u209a = sumToCount sumR\u209a\n\n sumR\u209a-ext : SumExt sumR\u209a\n sumR\u209a-ext = sumProd-ext sum\u03c0-ext sumR\u209a-xtra-ext\n\n sum : Sum (Bit \u00d7 R\u209a)\n sum = sumProd sumBit sumR\u209a\n\n sum-ext : SumExt sum\n sum-ext = sumProd-ext sumBit-ext sumR\u209a-ext\n\n #_ : (\u21ba (Bit \u00d7 Permutation \u00d7 R\u209a-xtra) \u2192 Bit) \u2192 \u2115\n # f = sumToCount sum (f \u2218 rand)\n\n _\u2261#_ : (f g : \u21ba (Bit \u00d7 R\u209a) \u2192 Bit) \u2192 \u2605\n f \u2261# g = # f \u2261 # g\n\n {-\n otp-\u2219 : let otp = \u03bb A pb s \u2192 #R\u209a(\u03bb { (\u03c0 , _) \u2192 A (\u03c0 \u2219P pb) (\u03c0 \u2219S s) }) in\n \u2200 pb\u2080 s\u2080 pb\u2081 s\u2081 (A : _ \u2192 _ \u2192 Bit) \u2192 otp A pb\u2080 s\u2080 \u2261 otp A pb\u2081 s\u2081\n otp-\u2219 pb\u2080 s\u2080 pb\u2081 s\u2081 A = ?\n -}\n\n Answer : Bit \u2192 \u2605\n Answer false{-0b-} = Permutation\n Answer true {-1b-} = Solution\n\n -- The prover is the advesary in the generic terminology,\n -- and the verifier is the challenger.\n Prover : \u2605\n Prover = Problem \u2192 \u21ba R\u209a \u2192 (b : Bit) \u2192 Problem \u00d7 Answer b\n\n -- Here we show that the explicit commitment step seems useless given\n -- the formalization. The verifier can \"trust\" the prover on the fact\n -- that any choice is going to be govern only by the problem and the\n -- randomness.\n module WithCommitment (Commitment : \u2605)\n (AnswerWC : Bit \u2192 \u2605)\n (reveal : \u2200 b \u2192 Commitment \u2192 AnswerWC b \u2192 Problem \u00d7 Answer b) where\n ProverWC = (Problem \u2192 R\u209a \u2192 Commitment)\n \u00d7 (Problem \u2192 R\u209a \u2192 (b : Bit) \u2192 AnswerWC b)\n prover : ProverWC \u2192 Prover\n prover (pr\u2080 , pr\u2081) pb (rand r\u209a) b = reveal b (pr\u2080 pb r\u209a) (pr\u2081 pb r\u209a b)\n\n Verif : Problem \u2192 \u2200 b \u2192 Problem \u00d7 Answer b \u2192 Bit\n Verif pb false{-0b-} (\u03c0\u2219pb , \u03c0) = (\u03c0 \u2219P pb) == \u03c0\u2219pb\n Verif pb true {-1b-} (\u03c0\u2219pb , s) = check \u03c0\u2219pb s\n\n _\u21c4\u2032_ : Problem \u2192 Prover \u2192 Bit \u2192 \u21ba R\u209a \u2192 Bit\n (pb \u21c4\u2032 pr) b (rand r\u209a) = Verif pb b (pr pb (rand r\u209a) b)\n\n _\u21c4_ : Problem \u2192 Prover \u2192 \u21ba (Bit \u00d7 R\u209a) \u2192 Bit\n (pb \u21c4 pr) (rand (b , r\u209a)) = (pb \u21c4\u2032 pr) b (rand r\u209a)\n\n honest : (Problem \u2192 Maybe Solution) \u2192 Prover\n honest solve pb (rand (\u03c0 , r\u209a)) b = (\u03c0 \u2219P pb , ans b)\n module Honest where\n ans : \u2200 b \u2192 Answer b\n ans false = \u03c0\n ans true with solve pb\n ... | just sol = \u03c0 \u2219S sol\n ... | nothing = easy-sol \u03c0\n\n module WithCorrectSolver (pb : Problem)\n (s : Solution)\n (check-s : check pb s \u2261 true)\n where\n\n -- When the honest prover has a solution, he gets accepted\n -- unconditionally by the verifier.\n honest-accepted : \u2200 r \u2192 (pb \u21c4 honest (const (just s))) r \u2261 1b\n honest-accepted (rand (true , \u03c0 , r\u209a)) rewrite sym (check-\u2219 pb s \u03c0) = check-s\n honest-accepted (rand (false , \u03c0 , r\u209a)) = ==-refl\n\n honest-\u2141 = \u03bb pb s \u2192 (pb \u21c4 honest (const (just s)))\n\n module HonestLeakZeroKnowledge (pb\u2080 pb\u2081 : Problem)\n (s\u2080 s\u2081 : Solution) where\n\n helper : \u2200 r\u209a \u2192 Bool.to\u2115 ((pb\u2080 \u21c4\u2032 honest (const (just s\u2080))) 0b (rand r\u209a))\n \u2261 Bool.to\u2115 ((pb\u2081 \u21c4\u2032 honest (const (just s\u2081))) 0b (rand r\u209a))\n helper (\u03c0 , r\u209a) rewrite ==-refl {\u03c0 \u2219P pb\u2080} | ==-refl {\u03c0 \u2219P pb\u2081} = refl\n\n honest-leak : honest-\u2141 pb\u2080 s\u2080 \u2261# honest-\u2141 pb\u2081 s\u2081\n honest-leak rewrite otp-\u2219 pb\u2080 s\u2080 pb\u2081 s\u2081 check | sumR\u209a-ext helper = refl\n\n -- Predicts b=b\u2032\n cheater : \u2200 b\u2032 \u2192 Prover\n cheater b\u2032 pb (rand (\u03c0 , _)) b = pb\u2032 b\u2032 , ans b\n module Cheater where\n pb\u2032 : \u2200 b\u2032 \u2192 Problem\n pb\u2032 true = easy-pb \u03c0\n pb\u2032 false = \u03c0 \u2219P pb\n ans : \u2200 b \u2192 Answer b\n ans true = easy-sol \u03c0\n ans false = \u03c0\n\n -- If cheater predicts correctly, verifer accepts him\n cheater-accepted : \u2200 b pb r\u209a \u2192 (pb \u21c4\u2032 cheater b) b r\u209a \u2261 1b\n cheater-accepted true pb (rand (\u03c0 , r\u209a)) = check-easy \u03c0\n cheater-accepted false pb (rand (\u03c0 , r\u209a)) = ==-refl\n\n -- If cheater predicts incorrecty, verifier rejects him\n module CheaterRejected (pb : Problem)\n (not-easy-sol : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P pb) (easy-sol \u03c0) \u2261 false)\n (not-easy-pb : \u2200 \u03c0 \u2192 ((\u03c0 \u2219P pb) == easy-pb \u03c0) \u2261 false) where\n\n cheater-rejected : \u2200 b r\u209a \u2192 (pb \u21c4\u2032 cheater (not b)) b r\u209a \u2261 0b\n cheater-rejected true (rand (\u03c0 , r\u209a)) = not-easy-sol \u03c0\n cheater-rejected false (rand (\u03c0 , r\u209a)) = not-easy-pb \u03c0\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"09a96ec629e65a1c06ec3c1e22b81d09c9f1f854","subject":"easy cases","message":"easy cases\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"expansion-unicity.agda","new_file":"expansion-unicity.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nmodule expansion-unicity where\n mutual\n expansion-unicity-synth : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {d1 d2 : dhexp} {\u03941 \u03942 : hctx} \u2192\n \u0393 \u22a2 e \u21d2 \u03c41 ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e \u21d2 \u03c42 ~> d2 \u22a3 \u03942 \u2192\n \u03c41 == \u03c42 \u00d7 d1 == d2 \u00d7 \u03941 == \u03942\n expansion-unicity-synth ESConst ESConst = refl , refl , refl\n expansion-unicity-synth (ESVar {\u0393 = \u0393} x\u2081) (ESVar x\u2082) = ctxunicity {\u0393 = \u0393} x\u2081 x\u2082 , refl , refl\n expansion-unicity-synth (ESLam d1) (ESLam d2) with expansion-unicity-synth d1 d2\n ... | ih1 , ih2 , ih3 = ap1 _ ih1 , ap1 _ ih2 , refl\n expansion-unicity-synth (ESAp1 x x\u2081 x\u2082 x\u2083) (ESAp1 x\u2084 x\u2085 x\u2086 x\u2087) = {!!}\n expansion-unicity-synth (ESAp1 x x\u2081 x\u2082 x\u2083) (ESAp2 x\u2084 d5 x\u2085 x\u2086) = {!!}\n expansion-unicity-synth (ESAp1 x x\u2081 x\u2082 x\u2083) (ESAp3 x\u2084 d5 x\u2085) = {!!}\n expansion-unicity-synth (ESAp2 x d5 x\u2081 x\u2082) (ESAp1 x\u2083 x\u2084 x\u2085 x\u2086) = {!!}\n expansion-unicity-synth (ESAp2 x d5 x\u2081 x\u2082) (ESAp2 x\u2083 d6 x\u2084 x\u2085) = {!!}\n expansion-unicity-synth (ESAp2 x d5 x\u2081 x\u2082) (ESAp3 x\u2083 d6 x\u2084) = {!!}\n expansion-unicity-synth (ESAp3 x d5 x\u2081) (ESAp1 x\u2082 x\u2083 x\u2084 x\u2085) = {!!}\n expansion-unicity-synth (ESAp3 x d5 x\u2081) (ESAp2 x\u2082 d6 x\u2083 x\u2084) = {!!}\n expansion-unicity-synth (ESAp3 x d5 x\u2081) (ESAp3 x\u2082 d6 x\u2083) = {!!}\n expansion-unicity-synth ESEHole ESEHole = refl , refl , refl\n expansion-unicity-synth (ESNEHole d1) (ESNEHole d2) with expansion-unicity-synth d1 d2\n ... | ih1 , ih2 , ih3 = refl , ap1 _ ih2 , ap1 _ ih3\n expansion-unicity-synth (ESAsc1 x x\u2081) (ESAsc1 x\u2082 x\u2083) = {!!}\n expansion-unicity-synth (ESAsc1 x x\u2081) (ESAsc2 x\u2082) = {!!}\n expansion-unicity-synth (ESAsc2 x) (ESAsc1 x\u2081 x\u2082) = {!!}\n expansion-unicity-synth (ESAsc2 x) (ESAsc2 x\u2081) = {!!}\n\n expansion-unicity-ana : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c41' \u03c42 \u03c42' : htyp} {d1 d2 : dhexp} {\u03941 \u03942 : hctx} \u2192\n \u0393 \u22a2 e \u21d0 \u03c41 ~> d1 :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n \u03c41 == \u03c42 \u00d7 d1 == d2 \u00d7 \u03c41' == \u03c42' \u00d7 \u03941 == \u03942\n expansion-unicity-ana (EALam d1) (EALam d2) = {!!}\n expansion-unicity-ana (EALam d1) (EASubsume x\u2081 x\u2082 x\u2083 x\u2084) = {!!}\n expansion-unicity-ana (EASubsume x\u2081 x\u2082 x\u2083 x\u2084) (EALam d2) = {!!}\n expansion-unicity-ana (EASubsume x x\u2081 x\u2082 x\u2083) (EASubsume x\u2084 x\u2085 x\u2086 x\u2087) = {!!}\n expansion-unicity-ana (EASubsume x x\u2081 x\u2082 x\u2083) EAEHole = {!!}\n expansion-unicity-ana (EASubsume x x\u2081 x\u2082 x\u2083) (EANEHole x\u2084 x\u2085) = {!!}\n expansion-unicity-ana EAEHole (EASubsume x x\u2081 x\u2082 x\u2083) = {!!}\n expansion-unicity-ana EAEHole EAEHole = {!!}\n expansion-unicity-ana (EANEHole x x\u2081) (EASubsume x\u2082 x\u2083 x\u2084 x\u2085) = {!!}\n expansion-unicity-ana (EANEHole x x\u2081) (EANEHole x\u2082 x\u2083) = {!!}\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\n\nmodule expansion-unicity where\n mutual\n expansion-unicity-synth : (\u0393 : tctx) (e : hexp) (\u03c41 \u03c42 : htyp) (d1 d2 : dhexp) (\u03941 \u03942 : hctx) \u2192\n \u0393 \u22a2 e \u21d2 \u03c41 ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e \u21d2 \u03c42 ~> d2 \u22a3 \u03942 \u2192\n \u03c41 == \u03c42 \u00d7 d1 == d2 \u00d7 \u03941 == \u03942\n expansion-unicity-synth = {!!}\n\n expansion-unicity-ana : (\u0393 : tctx) (e : hexp) (\u03c41 \u03c41' \u03c42 \u03c42' : htyp) (d1 d2 : dhexp) (\u03941 \u03942 : hctx) \u2192\n \u0393 \u22a2 e \u21d0 \u03c41 ~> d1 :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n \u03c41 == \u03c42 \u00d7 d1 == d2 \u00d7 \u03c41' == \u03c42' \u00d7 \u03941 == \u03942\n expansion-unicity-ana = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"0e5b51a126b354cb6a2c34c080deb1fdcc75c7cb","subject":"Generalize environments to values in higher universes.","message":"Generalize environments to values in higher universes.\n\nOld-commit-hash: 4ff4102742306da9be77151d785aa608f8558977\n","repos":"inc-lc\/ilc-agda","old_file":"Denotational\/Environments.agda","new_file":"Denotational\/Environments.agda","new_contents":"module Denotational.Environments\n (Type : Set)\n {\u2113}\n (\u27e6_\u27e7Type : Type \u2192 Set \u2113)\n where\n\n-- ENVIRONMENTS\n--\n-- This module defines the meaning of contexts, that is,\n-- the type of environments that fit a context, together\n-- with operations and properties of these operations.\n--\n-- This module is parametric in the syntax and semantics\n-- of types, so it can be reused for different calculi\n-- and models.\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Contexts Type\nopen import Denotational.Notation\n\nprivate\n meaningOfType : Meaning Type\n meaningOfType = meaning \u27e6_\u27e7Type\n\n-- TYPING CONTEXTS\n\n-- Denotational Semantics : Contexts Represent Environments\n\ndata Empty : Set \u2113 where\n \u2205 : Empty\n\ndata Bind A B : Set \u2113 where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set \u2113\n\u27e6 \u2205 \u27e7Context = Empty\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n-- VARIABLES\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n-- WEAKENING\n\n-- Remove a variable from an environment\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u0393\u2032 : \u0393\u2081 \u227c \u0393\u2082) \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 \u0393\u2032 \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 \u0393\u2032 \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 \u0393\u2032 \u27e7\u227c (v \u2022 \u03c1) = \u27e6 \u0393\u2032 \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\n-- Properties\n\n\u27e6\u27e7-\u227c-trans : \u2200 {\u0393\u2083 \u0393\u2081 \u0393\u2082} \u2192 (\u0393\u2032 : \u0393\u2081 \u227c \u0393\u2082) (\u0393\u2033 : \u0393\u2082 \u227c \u0393\u2083) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2083 \u27e7) \u2192 \u27e6 \u227c-trans \u0393\u2032 \u0393\u2033 \u27e7 \u03c1 \u2261 \u27e6 \u0393\u2032 \u27e7 (\u27e6 \u0393\u2033 \u27e7 \u03c1)\n\u27e6\u27e7-\u227c-trans \u0393\u2032 \u2205 \u2205 = refl\n\u27e6\u27e7-\u227c-trans (keep \u03c4 \u2022 \u0393\u2032) (keep .\u03c4 \u2022 \u0393\u2033) (v \u2022 \u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u27e7-\u227c-trans \u0393\u2032 \u0393\u2033 \u03c1)\n\u27e6\u27e7-\u227c-trans (drop \u03c4 \u2022 \u0393\u2032) (keep .\u03c4 \u2022 \u0393\u2033) (v \u2022 \u03c1) = \u27e6\u27e7-\u227c-trans \u0393\u2032 \u0393\u2033 \u03c1\n\u27e6\u27e7-\u227c-trans \u0393\u2032 (drop \u03c4 \u2022 \u0393\u2033) (v \u2022 \u03c1) = \u27e6\u27e7-\u227c-trans \u0393\u2032 \u0393\u2033 \u03c1\n\n\u27e6\u27e7-\u227c-refl : \u2200 {\u0393 : Context} \u2192\n \u2200 (\u03c1 : \u27e6 \u0393 \u27e7) \u2192 \u27e6 \u227c-refl \u27e7 \u03c1 \u2261 \u03c1\n\u27e6\u27e7-\u227c-refl {\u2205} \u2205 = refl\n\u27e6\u27e7-\u227c-refl {\u03c4 \u2022 \u0393} (v \u2022 \u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u27e7-\u227c-refl \u03c1)\n\n-- SOUNDNESS of variable lifting\n\nlift-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (\u0393\u2032 : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 lift \u0393\u2032 x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 \u0393\u2032 \u27e7 \u03c1)\nlift-sound \u2205 () \u03c1\nlift-sound (keep \u03c4 \u2022 \u0393\u2032) this (v \u2022 \u03c1) = refl\nlift-sound (keep \u03c4 \u2022 \u0393\u2032) (that x) (v \u2022 \u03c1) = lift-sound \u0393\u2032 x \u03c1\nlift-sound (drop \u03c4 \u2022 \u0393\u2032) this (v \u2022 \u03c1) = lift-sound \u0393\u2032 this \u03c1\nlift-sound (drop \u03c4 \u2022 \u0393\u2032) (that x) (v \u2022 \u03c1) = lift-sound \u0393\u2032 (that x) \u03c1\n","old_contents":"module Denotational.Environments\n (Type : Set)\n (\u27e6_\u27e7Type : Type \u2192 Set)\n where\n\n-- ENVIRONMENTS\n--\n-- This module defines the meaning of contexts, that is,\n-- the type of environments that fit a context, together\n-- with operations and properties of these operations.\n--\n-- This module is parametric in the syntax and semantics\n-- of types, so it can be reused for different calculi\n-- and models.\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Contexts Type\nopen import Denotational.Notation\n\nprivate\n meaningOfType : Meaning Type\n meaningOfType = meaning \u27e6_\u27e7Type\n\n-- TYPING CONTEXTS\n\n-- Denotational Semantics : Contexts Represent Environments\n\ndata Empty : Set where\n \u2205 : Empty\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = Empty\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n-- VARIABLES\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n-- WEAKENING\n\n-- Remove a variable from an environment\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u0393\u2032 : \u0393\u2081 \u227c \u0393\u2082) \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 \u0393\u2032 \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 \u0393\u2032 \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 \u0393\u2032 \u27e7\u227c (v \u2022 \u03c1) = \u27e6 \u0393\u2032 \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\n-- Properties\n\n\u27e6\u27e7-\u227c-trans : \u2200 {\u0393\u2083 \u0393\u2081 \u0393\u2082} \u2192 (\u0393\u2032 : \u0393\u2081 \u227c \u0393\u2082) (\u0393\u2033 : \u0393\u2082 \u227c \u0393\u2083) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2083 \u27e7) \u2192 \u27e6 \u227c-trans \u0393\u2032 \u0393\u2033 \u27e7 \u03c1 \u2261 \u27e6 \u0393\u2032 \u27e7 (\u27e6 \u0393\u2033 \u27e7 \u03c1)\n\u27e6\u27e7-\u227c-trans \u0393\u2032 \u2205 \u2205 = refl\n\u27e6\u27e7-\u227c-trans (keep \u03c4 \u2022 \u0393\u2032) (keep .\u03c4 \u2022 \u0393\u2033) (v \u2022 \u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u27e7-\u227c-trans \u0393\u2032 \u0393\u2033 \u03c1)\n\u27e6\u27e7-\u227c-trans (drop \u03c4 \u2022 \u0393\u2032) (keep .\u03c4 \u2022 \u0393\u2033) (v \u2022 \u03c1) = \u27e6\u27e7-\u227c-trans \u0393\u2032 \u0393\u2033 \u03c1\n\u27e6\u27e7-\u227c-trans \u0393\u2032 (drop \u03c4 \u2022 \u0393\u2033) (v \u2022 \u03c1) = \u27e6\u27e7-\u227c-trans \u0393\u2032 \u0393\u2033 \u03c1\n\n\u27e6\u27e7-\u227c-refl : \u2200 {\u0393 : Context} \u2192\n \u2200 (\u03c1 : \u27e6 \u0393 \u27e7) \u2192 \u27e6 \u227c-refl \u27e7 \u03c1 \u2261 \u03c1\n\u27e6\u27e7-\u227c-refl {\u2205} \u2205 = refl\n\u27e6\u27e7-\u227c-refl {\u03c4 \u2022 \u0393} (v \u2022 \u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u27e7-\u227c-refl \u03c1)\n\n-- SOUNDNESS of variable lifting\n\nlift-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (\u0393\u2032 : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 lift \u0393\u2032 x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 \u0393\u2032 \u27e7 \u03c1)\nlift-sound \u2205 () \u03c1\nlift-sound (keep \u03c4 \u2022 \u0393\u2032) this (v \u2022 \u03c1) = refl\nlift-sound (keep \u03c4 \u2022 \u0393\u2032) (that x) (v \u2022 \u03c1) = lift-sound \u0393\u2032 x \u03c1\nlift-sound (drop \u03c4 \u2022 \u0393\u2032) this (v \u2022 \u03c1) = lift-sound \u0393\u2032 this \u03c1\nlift-sound (drop \u03c4 \u2022 \u0393\u2032) (that x) (v \u2022 \u03c1) = lift-sound \u0393\u2032 (that x) \u03c1\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b7aea6f9087802948420259deb04a07ff6105ffc","subject":"Working in the equivalence N as lfp and N using data.","message":"Working in the equivalence N as lfp and N using data.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/LeastFixedPoints\/N.agda","new_file":"notes\/fixed-points\/LeastFixedPoints\/N.agda","new_contents":"------------------------------------------------------------------------------\n-- Equivalence: N as the least fixed-point and N using Agda's data constructor\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule LeastFixedPoints.N where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- Basic definitions\n\n-- Pre-fixed point : d is a pre-fixed point of f if f d \u2264 d\n\n-- Post-fixed point : d is a post-fixed point of f if d \u2264 f d\n\n-- Fixed-point : d is a fixed-point of f if f d = d\n\n-- Least pre-fixed point : d is the least pre-fixed point of f if\n-- 1. f d \u2264 d -- d is a pre-fixed point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Least fixed-point : d is the least fixed-point of f if\n-- 1. f d = d -- d is a fixed-point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Thm: If d is the least pre-fixed point of f, then d is the least\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n-- Thm: If d is the greatest post-fixed point of f, then d is the greatest\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n------------------------------------------------------------------------------\n-- Auxiliary definitions and properties\n\nflip : {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\nflip f b a = f a b\n\ncong : (f : D \u2192 D) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2261 f y\ncong f refl = refl\n\n------------------------------------------------------------------------------\nmodule LFP where\n\n -- N is a least fixed-point of a functor\n\n -- The functor.\n NatF : (D \u2192 Set) \u2192 D \u2192 Set\n NatF A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n\n -- The natural numbers are the least fixed-point of NatF.\n postulate\n N : D \u2192 Set\n\n -- N is a pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the introduction rules.\n N-in : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n\n -- The higher-order version.\n N-in-ho : \u2200 {n} \u2192 NatF N n \u2192 N n\n\n -- N is the least pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the elimination rule of an inductively\n -- defined predicate.\n N-least-pre-fixed :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\n\n -- Higher-order version (incomplete?).\n N-least-pre-fixed-ho :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (NatF A n \u2192 A n) \u2192\n N n \u2192 A n\n\n ----------------------------------------------------------------------------\n -- From\/to N-in\/N-in-ho.\n\n N-in\u2081 : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n N-in\u2081 = N-in-ho\n\n N-in-ho\u2081 : \u2200 {n} \u2192 NatF N n \u2192 N n\n N-in-ho\u2081 = N-in\u2081\n\n ----------------------------------------------------------------------------\n -- From\/to N-least-pre-fixed\/N-least-pre-fixed-ho\n N-least-pre-fixed' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\n N-least-pre-fixed' = N-least-pre-fixed-ho\n\n N-least-pre-fixed-ho' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (NatF A n \u2192 A n) \u2192\n N n \u2192 A n\n N-least-pre-fixed-ho' = N-least-pre-fixed\n\n ----------------------------------------------------------------------------\n -- The data constructors of N.\n nzero : N zero\n nzero = N-in (inj\u2081 refl)\n\n nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n nsucc Nn = N-in (inj\u2082 (_ , refl , Nn))\n\n ----------------------------------------------------------------------------\n -- Because N is the least pre-fixed point of NatF (i.e. N-in and\n -- N-ind), we can proof that N is also a post-fixed point of NatF.\n\n -- N is a post-fixed point of NatF.\n N-post-fixed : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n')\n N-post-fixed = N-least-pre-fixed A h\n where\n A : D \u2192 Set\n A m = m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 N m')\n\n h : \u2200 {m} \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 prf) = inj\u2081 prf\n h (inj\u2082 (m' , prf , Am')) = inj\u2082 (m' , prf , helper Am')\n where\n helper : A m' \u2192 N m'\n helper (inj\u2081 prf') = subst N (sym prf') nzero\n helper (inj\u2082 (m'' , prf' , Am'')) = subst N (sym prf') (nsucc Am'')\n\n ----------------------------------------------------------------------------\n -- The induction principle for N *with* the hypothesis N n in the\n -- induction step.\n\n N-ind\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2081 A A0 is {n} Nn = N-least-pre-fixed A h Nn\n where\n h : n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n\n h (inj\u2081 prf) = subst A (sym prf) A0\n h (inj\u2082 (n' , prf , An')) = subst A (sym prf) (is helper An')\n where\n helper : N n'\n helper with N-post-fixed Nn\n ... | inj\u2081 n\u22610 = \u22a5-elim (0\u2262S (trans (sym n\u22610) prf))\n ... | inj\u2082 (m' , prf' , Nm') =\n subst N (succInjective (trans (sym prf') prf)) Nm'\n\n ----------------------------------------------------------------------------\n -- The induction principle for N *without* the hypothesis N n in the\n -- induction step.\n\n N-ind\u2082 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2082 A A0 is {n} = N-least-pre-fixed A h\n where\n h : n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n\n h (inj\u2081 prf) = subst A (sym prf) A0\n h (inj\u2082 (n' , prf , An')) = subst A (sym prf) (is An')\n\n ----------------------------------------------------------------------------\n -- Example: We will use N-least-pre-fixed as the induction principle on N.\n\n postulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ\u2081 d + e \u2261 succ\u2081 (d + e)\n\n +-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n +-leftIdentity n = +-0x n\n\n +-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n +-N {m} {n} Nm Nn = N-least-pre-fixed A h Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n h : m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 prf) = subst N (cong (flip _+_ n) (sym prf)) A0\n where\n A0 : A zero\n A0 = subst N (sym (+-leftIdentity n)) Nn\n h (inj\u2082 (m' , m\u2261Sm' , Am')) =\n subst N (cong (flip _+_ n) (sym m\u2261Sm')) (is Am')\n where\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} Ai = subst N (sym (+-Sx i n)) (nsucc Ai)\n\n ----------------------------------------------------------------------------\n -- Example: A proof using N-post-fixed.\n\n pred-N : \u2200 {n} \u2192 N n \u2192 N (pred\u2081 n)\n pred-N {n} Nn = case h\u2081 h\u2082 (N-post-fixed Nn)\n where\n h\u2081 : n \u2261 zero \u2192 N (pred\u2081 n)\n h\u2081 n\u22610 = subst N (sym (trans (predCong n\u22610) pred-0)) nzero\n\n h\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n' \u2192 N (pred\u2081 n)\n h\u2082 (n' , prf , Nn') = subst N (sym (trans (predCong prf) (pred-S n'))) Nn'\n\n ----------------------------------------------------------------------------\n -- From\/to N as a least fixed-point to\/from N as data type.\n\n data N' : D \u2192 Set where\n nzero' : N' zero\n nsucc' : \u2200 {n} \u2192 N' n \u2192 N' (succ\u2081 n)\n\n N'\u2192N : \u2200 {n} \u2192 N' n \u2192 N n\n N'\u2192N nzero' = nzero\n N'\u2192N (nsucc' Nn) = nsucc (N'\u2192N Nn)\n\n -- Using N-ind\u2081.\n N\u2192N' : \u2200 {n} \u2192 N n \u2192 N' n\n N\u2192N' = N-ind\u2081 N' nzero' (\u03bb _ \u2192 nsucc')\n\n -- Using N-ind\u2082.\n N\u2192N'\u2081 : \u2200 {n} \u2192 N n \u2192 N' n\n N\u2192N'\u2081 = N-ind\u2082 N' nzero' nsucc'\n\n------------------------------------------------------------------------------\nmodule Data where\n\n data N : D \u2192 Set where\n nzero : N zero\n nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n\n -- The induction principle for N *with* the hypothesis N n in the\n -- induction step.\n N-ind\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2081 A A0 is nzero = A0\n N-ind\u2081 A A0 is (nsucc Nn) = is Nn (N-ind\u2081 A A0 is Nn)\n\n -- The induction principle for N *without* the hypothesis N n in the\n -- induction step.\n N-ind\u2082 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2082 A A0 h nzero = A0\n N-ind\u2082 A A0 h (nsucc Nn) = h (N-ind\u2082 A A0 h Nn)\n\n ----------------------------------------------------------------------------\n -- N-in.\n\n N-in : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n N-in {n} h = case prf\u2081 prf\u2082 h\n where\n prf\u2081 : n \u2261 zero \u2192 N n\n prf\u2081 n\u22610 = subst N (sym n\u22610) nzero\n\n prf\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n' \u2192 N n\n prf\u2082 (n' , prf , Nn') = subst N (sym prf) (nsucc Nn')\n\n ----------------------------------------------------------------------------\n -- From N-ind\u2082 to N-least-pre-fixed.\n\n N\u21920\u2228S : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n')\n N\u21920\u2228S = N-ind\u2082 A A0 is\n where\n A : D \u2192 Set\n A i = i \u2261 zero \u2228 (\u2203[ i' ] i \u2261 succ\u2081 i' \u2227 N i')\n\n A0 : A zero\n A0 = inj\u2081 refl\n\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} Ai = case prf\u2081 prf\u2082 Ai\n where\n prf\u2081 : i \u2261 zero \u2192 succ\u2081 i \u2261 zero \u2228 (\u2203[ i' ] succ\u2081 i \u2261 succ\u2081 i' \u2227 N i')\n prf\u2081 h' = inj\u2082 (i , refl , (subst N (sym h') nzero))\n\n prf\u2082 : \u2203[ i' ] i \u2261 succ\u2081 i' \u2227 N i' \u2192\n succ\u2081 i \u2261 zero \u2228 (\u2203[ i' ] succ\u2081 i \u2261 succ\u2081 i' \u2227 N i')\n prf\u2082 (i' , prf , Ni') = inj\u2082 (i , refl , subst N (sym prf) (nsucc Ni'))\n\n N-least-pre-fixed\u2082 :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\n N-least-pre-fixed\u2082 A {n} h Nn = case prf\u2081 prf\u2082 (N\u21920\u2228S Nn)\n where\n prf\u2081 : n \u2261 zero \u2192 A n\n prf\u2081 n\u22610 = h (inj\u2081 n\u22610)\n\n prf\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n' \u2192 A n\n prf\u2082 (n' , prf , Nn') = h (inj\u2082 (n' , prf , {!!}))\n","old_contents":"------------------------------------------------------------------------------\n-- From N as the least fixed-point to N using Agda's data constructor\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- We want to represent the total natural numbers data type\n--\n-- data N : D \u2192 Set where\n-- nzero : N zero\n-- nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n--\n-- using the representation of N as the least fixed-point.\n\nmodule LeastFixedPoints.N where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- Basic definitions\n\n-- Pre-fixed point : d is a pre-fixed point of f if f d \u2264 d\n\n-- Post-fixed point : d is a post-fixed point of f if d \u2264 f d\n\n-- Fixed-point : d is a fixed-point of f if f d = d\n\n-- Least pre-fixed point : d is the least pre-fixed point of f if\n-- 1. f d \u2264 d -- d is a pre-fixed point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Least fixed-point : d is the least fixed-point of f if\n-- 1. f d = d -- d is a fixed-point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Thm: If d is the least pre-fixed point of f, then d is the least\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n-- Thm: If d is the greatest post-fixed point of f, then d is the greatest\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n------------------------------------------------------------------------------\n-- Auxiliary definitions and properties\n\nflip : {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\nflip f b a = f a b\n\ncong : (f : D \u2192 D) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2261 f y\ncong f refl = refl\n\n------------------------------------------------------------------------------\n-- N is a least fixed-point of a functor\n\n-- The functor.\nNatF : (D \u2192 Set) \u2192 D \u2192 Set\nNatF A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n\n-- The natural numbers are the least fixed-point of NatF.\npostulate\n N : D \u2192 Set\n\n -- N is a pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the introduction rules.\n N-in : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n\n -- The higher-order version.\n N-in-ho : \u2200 {n} \u2192 NatF N n \u2192 N n\n\n -- N is the least pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the elimination rule of an inductively\n -- defined predicate.\n N-least-pre-fixed :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\n\n -- Higher-order version (incomplete?)\n N-least-pre-fixed-ho :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (NatF A n \u2192 A n) \u2192\n N n \u2192 A n\n\n------------------------------------------------------------------------------\n-- From\/to N-in\/N-in-ho.\n\nN-in\u2081 : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\nN-in\u2081 = N-in-ho\n\nN-in-ho\u2081 : \u2200 {n} \u2192 NatF N n \u2192 N n\nN-in-ho\u2081 = N-in\u2081\n\n------------------------------------------------------------------------------\n-- From\/to N-least-pre-fixed\/N-least-pre-fixed-ho\nN-least-pre-fixed' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\nN-least-pre-fixed' = N-least-pre-fixed-ho\n\nN-least-pre-fixed-ho' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (NatF A n \u2192 A n) \u2192\n N n \u2192 A n\nN-least-pre-fixed-ho' = N-least-pre-fixed\n\n------------------------------------------------------------------------------\n-- The data constructors of N.\nnzero : N zero\nnzero = N-in (inj\u2081 refl)\n\nnsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\nnsucc Nn = N-in (inj\u2082 (_ , refl , Nn))\n\n------------------------------------------------------------------------------\n-- Because N is the least pre-fixed point of NatF (i.e. N-in and\n-- N-ind), we can proof that N is also a post-fixed point of NatF.\n\n-- N is a post-fixed point of NatF.\nN-post-fixed : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n')\nN-post-fixed = N-least-pre-fixed A h\n where\n A : D \u2192 Set\n A m = m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 N m')\n\n h : \u2200 {m} \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 prf) = inj\u2081 prf\n h (inj\u2082 (m' , prf , Am')) = inj\u2082 (m' , prf , helper Am')\n where\n helper : A m' \u2192 N m'\n helper (inj\u2081 prf') = subst N (sym prf') nzero\n helper (inj\u2082 (m'' , prf' , Am'')) = subst N (sym prf') (nsucc Am'')\n\n------------------------------------------------------------------------------\n-- The induction principle for N *with* the hypothesis N n in the\n-- induction step.\nN-ind\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\nN-ind\u2081 A A0 is {n} Nn = N-least-pre-fixed A h Nn\n where\n h : n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n\n h (inj\u2081 prf) = subst A (sym prf) A0\n h (inj\u2082 (n' , prf , An')) = subst A (sym prf) (is helper An')\n where\n helper : N n'\n helper with N-post-fixed Nn\n ... | inj\u2081 n\u22610 = \u22a5-elim (0\u2262S (trans (sym n\u22610) prf))\n ... | inj\u2082 (m' , prf' , Nm') = subst N (succInjective (trans (sym prf') prf)) Nm'\n\n------------------------------------------------------------------------------\n-- The induction principle for N *without* the hypothesis N n in the\n-- induction step.\nN-ind\u2082 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\nN-ind\u2082 A A0 is {n} = N-least-pre-fixed A h\n where\n h : n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n\n h (inj\u2081 prf) = subst A (sym prf) A0\n h (inj\u2082 (n' , prf , An')) = subst A (sym prf) (is An')\n\n------------------------------------------------------------------------------\n-- Example: We will use N-least-pre-fixed as the induction principle on N.\n\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ\u2081 d + e \u2261 succ\u2081 (d + e)\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity n = +-0x n\n\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = N-least-pre-fixed A h Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n h : m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 prf) = subst N (cong (flip _+_ n) (sym prf)) A0\n where\n A0 : A zero\n A0 = subst N (sym (+-leftIdentity n)) Nn\n h (inj\u2082 (m' , m\u2261Sm' , Am')) = subst N (cong (flip _+_ n) (sym m\u2261Sm')) (is Am')\n where\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} Ai = subst N (sym (+-Sx i n)) (nsucc Ai)\n\n------------------------------------------------------------------------------\n-- Example: A proof using N-post-fixed\n\npred-N : \u2200 {n} \u2192 N n \u2192 N (pred\u2081 n)\npred-N {n} Nn = case h\u2081 h\u2082 (N-post-fixed Nn)\n where\n h\u2081 : n \u2261 zero \u2192 N (pred\u2081 n)\n h\u2081 n\u22610 = subst N (sym (trans (predCong n\u22610) pred-0)) nzero\n\n h\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n' \u2192 N (pred\u2081 n)\n h\u2082 (n' , prf , Nn') = subst N (sym (trans (predCong prf) (pred-S n'))) Nn'\n\n------------------------------------------------------------------------------\n-- From\/to N as a least fixed-point to\/from N as data type.\n\nopen import FOTC.Data.Nat.Type renaming\n ( N to N'\n ; nsucc to nsucc'\n ; nzero to nzero'\n )\n\nN'\u2192N : \u2200 {n} \u2192 N' n \u2192 N n\nN'\u2192N nzero' = nzero\nN'\u2192N (nsucc' Nn) = nsucc (N'\u2192N Nn)\n\n-- Using N-ind\u2081.\nN\u2192N' : \u2200 {n} \u2192 N n \u2192 N' n\nN\u2192N' = N-ind\u2081 N' nzero' (\u03bb _ \u2192 nsucc')\n\n-- Using N-ind\u2082.\nN\u2192N'\u2081 : \u2200 {n} \u2192 N n \u2192 N' n\nN\u2192N'\u2081 = N-ind\u2082 N' nzero' nsucc'\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b35ce26d137fb197a7e90b212d09b5eb09dcf5dd","subject":"Swapped premises and the conclusion in the introduction rules.","message":"Swapped premises and the conclusion in the introduction rules.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/LeastFixedPoints.agda","new_file":"notes\/fixed-points\/LeastFixedPoints.agda","new_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule LeastFixedPoints where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n-- infixl 9 _+_\n\n------------------------------------------------------------------------------\n-- Basic definitions\n\n-- Pre-fixed point : d is a pre-fixed point of f if f d \u2264 d\n\n-- Post-fixed point : d is a post-fixed point of f if d \u2264 f d\n\n-- Fixed-point : d is a fixed-point of f if f d = d\n\n-- Least pre-fixed point : d is the least pre-fixed point of f if\n-- 1. f d \u2264 d -- d is a pre-fixed point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Least fixed-point : d is the least fixed-point of f if\n-- 1. f d = d -- d is a fixed-point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Thm: If d is the least pre-fixed point of f, then d is the least\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n-- Thm: If d is the greatest post-fixed point of f, then d is the greatest\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n------------------------------------------------------------------------------\n-- Auxiliary definitions\n\nflip : {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\nflip f b a = f a b\n\n------------------------------------------------------------------------------\n-- N is a least fixed-point of a functor\n\n-- Instead defining the least fixed-point via a (higher-order)\n-- operator, we will define it using an instance of that operator.\n\n-- The functor\nNatF : (D \u2192 Set) \u2192 D \u2192 Set\nNatF P n = n \u2261 zero \u2228 (\u2203[ m ] P m \u2227 n \u2261 succ\u2081 m)\n\n-- The natural numbers are the least fixed-point of NatF.\npostulate\n N : D \u2192 Set\n\n -- N is a pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the introduction rules.\n N-lfp\u2081 : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ m ] N m \u2227 n \u2261 succ\u2081 m) \u2192 N n\n -- N-lfp\u2081 : \u2200 n \u2192 NatF N n \u2192 N n -- Higher-order version\n\n -- N is a the least pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the elimination rule of an inductively\n -- defined predicate.\n N-lfp\u2082 : (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ m ] A m \u2227 n \u2261 succ\u2081 m) \u2192 A n) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n -- N-lfp\u2082 : (A : D \u2192 Set) \u2192 -- Higher-order version\n -- (\u2200 {n} \u2192 NatF A n \u2192 A n) \u2192\n -- \u2200 {n} \u2192 N n \u2192 A n\n\n------------------------------------------------------------------------------\n-- The data constructors of N.\nzN : N zero\nzN = N-lfp\u2081 (inj\u2081 refl)\n\nsN : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\nsN Nn = N-lfp\u2081 (inj\u2082 (_ , (Nn , refl)))\n\n------------------------------------------------------------------------------\n-- Because N is the least pre-fixed point of NatF (i.e. N-lfp\u2081 and\n-- N-lfp\u2082), we can proof that N is also a post-fixed point of NatF.\n\n-- N is a post-fixed point of NatF.\nN-lfp\u2083 : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 N m \u2227 n \u2261 succ\u2081 m)\nN-lfp\u2083 Nn = N-lfp\u2082 A prf Nn\n where\n A : D \u2192 Set\n A x = x \u2261 zero \u2228 (\u2203 \u03bb m \u2192 N m \u2227 x \u2261 succ\u2081 m)\n\n prf : \u2200 {n'} \u2192 n' \u2261 zero \u2228 (\u2203[ m ] A m \u2227 n' \u2261 succ\u2081 m) \u2192 A n'\n prf {n'} h = case inj\u2081 ((\u03bb h\u2081 \u2192 inj\u2082 (prf\u2081 h\u2081))) h -- case inj\u2081 prf\u2081 h\n where\n prf\u2081 : \u2203 (\u03bb m \u2192 A m \u2227 n' \u2261 succ\u2081 m) \u2192 \u2203 (\u03bb m \u2192 N m \u2227 n' \u2261 succ\u2081 m)\n prf\u2081 (m , Am , n'=Sm) = m , prf\u2082 Am , n'=Sm\n where\n prf\u2082 : A m \u2192 N m\n prf\u2082 Am = case (\u03bb ah \u2192 subst N (sym ah) zN) prf\u2083 Am\n where\n prf\u2083 : \u2203 (\u03bb m' \u2192 N m' \u2227 m \u2261 succ\u2081 m') \u2192 N m\n prf\u2083 (_ , Nm' , m\u2261Sm' ) = subst N (sym m\u2261Sm') (sN Nm')\n\n------------------------------------------------------------------------------\n-- The induction principle for N *without* the hypothesis N n in the\n-- induction step.\nindN\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\nindN\u2081 A A0 is Nn = N-lfp\u2082 A (case prf\u2081 prf\u2082) Nn\n where\n prf\u2081 : \u2200 {n'} \u2192 n' \u2261 zero \u2192 A n'\n prf\u2081 n'\u22610 = subst A (sym n'\u22610) A0\n\n prf\u2082 : \u2200 {n'} \u2192 \u2203 (\u03bb m \u2192 A m \u2227 n' \u2261 succ\u2081 m) \u2192 A n'\n prf\u2082 (_ , Am , n'\u2261Sm) = subst A (sym n'\u2261Sm) (is Am)\n\n-- The induction principle for N *with* the hypothesis N n in the\n-- induction step.\n--\n-- 2012-03-06. We cannot proof this principle because N-lfp\u2082 does not\n-- have the hypothesis N n.\n--\n-- indN\u2082 : (A : D \u2192 Set) \u2192\n-- A zero \u2192\n-- (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n-- \u2200 {n} \u2192 N n \u2192 A n\n-- indN\u2082 A A0 is Nn = N-lfp\u2082 A [ prf\u2081 , prf\u2082 ] Nn\n-- where\n-- prf\u2081 : \u2200 {n'} \u2192 n' \u2261 zero \u2192 A n'\n-- prf\u2081 n'\u22610 = subst A (sym n'\u22610) A0\n\n-- prf\u2082 : \u2200 {n'} \u2192 \u2203 (\u03bb m \u2192 n' \u2261 succ\u2081 m \u2227 A m) \u2192 A n'\n-- prf\u2082 {n'} (m , n'\u2261Sm , Am) = subst A (sym n'\u2261Sm) (is helper Am)\n-- where\n-- helper : N m\n-- helper = [ prf\u2083 , prf\u2084 ] (N-lfp\u2083 {!!})\n-- where\n-- prf\u2083 : n' \u2261 zero \u2192 N m\n-- prf\u2083 n'\u22610 = \u22a5-elim (0\u2262S (trans (sym n'\u22610) n'\u2261Sm))\n\n-- prf\u2084 : \u2203 (\u03bb m' \u2192 n' \u2261 succ\u2081 m' \u2227 N m') \u2192 N m\n-- prf\u2084 (_ , n'\u2261Sm' , Nm') =\n-- subst N (succInjective (trans (sym n'\u2261Sm') n'\u2261Sm)) Nm'\n\n------------------------------------------------------------------------------\n-- Example: We will use N-lfp\u2082 as the induction principle on N.\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ\u2081 d + e \u2261 succ\u2081 (d + e)\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity n = +-0x n\n\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {n = n} Nm Nn = N-lfp\u2082 A prf Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n prf : \u2200 {m'} \u2192 m' \u2261 zero \u2228 \u2203 (\u03bb m'' \u2192 A m'' \u2227 m' \u2261 succ\u2081 m'') \u2192 A m'\n prf h = case prf\u2081 prf\u2082 h\n where\n A0 : A zero\n A0 = subst N (sym (+-leftIdentity n)) Nn\n\n prf\u2081 : \u2200 {m} \u2192 m \u2261 zero \u2192 A m\n prf\u2081 h\u2081 = subst N (cong (flip _+_ n) (sym h\u2081)) A0\n\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} ih = subst N (sym (+-Sx i n)) (sN ih)\n\n prf\u2082 : \u2200 {m} \u2192 \u2203 (\u03bb m'' \u2192 A m'' \u2227 m \u2261 succ\u2081 m'') \u2192 A m\n prf\u2082 (_ , Am'' , m\u2261Sm'') =\n subst N (cong (flip _+_ n) (sym m\u2261Sm'')) (is Am'')\n","old_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule LeastFixedPoints where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\ninfixl 9 _+_\n\n------------------------------------------------------------------------------\n-- Basic definitions\n\n-- Pre-fixed point : d is a pre-fixed point of f if f d \u2264 d\n\n-- Post-fixed point : d is a post-fixed point of f if d \u2264 f d\n\n-- Fixed-point : d is a fixed-point of f if f d = d\n\n-- Least pre-fixed point : d is the least pre-fixed point of f if\n-- 1. f d \u2264 d -- d is a pre-fixed point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Least fixed-point : d is the least fixed-point of f if\n-- 1. f d = d -- d is a fixed-point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Thm: If d is the least pre-fixed point of f, then d is the least\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n-- Thm: If d is the greatest post-fixed point of f, then d is the greatest\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n------------------------------------------------------------------------------\n-- Auxiliary definitions\n\nflip : {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\nflip f b a = f a b\n\n------------------------------------------------------------------------------\n-- N is a least fixed-point of a functor\n\n-- Instead defining the least fixed-point via a (higher-order)\n-- operator, we will define it using an instance of that operator.\n\n-- The functor\nNatF : (D \u2192 Set) \u2192 D \u2192 Set\nNatF P n = n \u2261 zero \u2228 (\u2203[ m ] n \u2261 succ\u2081 m \u2227 P m)\n\n-- The natural numbers are the least fixed-point of NatF.\npostulate\n N : D \u2192 Set\n\n -- N is a pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the introduction rules.\n N-lfp\u2081 : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ m ] n \u2261 succ\u2081 m \u2227 N m) \u2192 N n\n -- N-lfp\u2081 : \u2200 n \u2192 NatF N n \u2192 N n -- Higher-order version\n\n -- N is a the least pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the elimination rule of an inductively\n -- defined predicate.\n N-lfp\u2082 : (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ m ] n \u2261 succ\u2081 m \u2227 A m) \u2192 A n) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n -- N-lfp\u2082 : (A : D \u2192 Set) \u2192 -- Higher-order version\n -- (\u2200 {n} \u2192 NatF A n \u2192 A n) \u2192\n -- \u2200 {n} \u2192 N n \u2192 A n\n\n------------------------------------------------------------------------------\n-- The data constructors of N.\nzN : N zero\nzN = N-lfp\u2081 (inj\u2081 refl)\n\nsN : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\nsN Nn = N-lfp\u2081 (inj\u2082 (_ , (refl , Nn)))\n\n------------------------------------------------------------------------------\n-- Because N is the least pre-fixed point of NatF (i.e. N-lfp\u2081 and\n-- N-lfp\u2082), we can proof that N is also a post-fixed point of NatF.\n\n-- N is a post-fixed point of NatF.\nN-lfp\u2083 : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ\u2081 m \u2227 N m)\nN-lfp\u2083 Nn = N-lfp\u2082 A prf Nn\n where\n A : D \u2192 Set\n A x = x \u2261 zero \u2228 \u2203 \u03bb m \u2192 x \u2261 succ\u2081 m \u2227 N m\n\n prf : \u2200 {n'} \u2192 n' \u2261 zero \u2228 \u2203 (\u03bb m \u2192 n' \u2261 succ\u2081 m \u2227 A m) \u2192 A n'\n prf {n'} h = case inj\u2081 (\u03bb h\u2081 \u2192 inj\u2082 (prf\u2081 h\u2081)) h\n where\n prf\u2081 : \u2203 (\u03bb m \u2192 n' \u2261 succ\u2081 m \u2227 (m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 N m'))) \u2192\n \u2203 \u03bb m \u2192 n' \u2261 succ\u2081 m \u2227 N m\n prf\u2081 (m , n'=Sm , h\u2082) = m , n'=Sm , prf\u2082 h\u2082\n where\n prf\u2082 : m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 N m') \u2192 N m\n prf\u2082 h\u2082 = case (\u03bb h\u2083 \u2192 subst N (sym h\u2083) zN) prf\u2083 h\u2082\n where\n prf\u2083 : \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 N m') \u2192 N m\n prf\u2083 (_ , m\u2261Sm' , Nm') = subst N (sym m\u2261Sm') (sN Nm')\n\n------------------------------------------------------------------------------\n-- The induction principle for N *without* the hypothesis N n in the\n-- induction step.\nindN\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\nindN\u2081 A A0 is Nn = N-lfp\u2082 A (case prf\u2081 prf\u2082) Nn\n where\n prf\u2081 : \u2200 {n'} \u2192 n' \u2261 zero \u2192 A n'\n prf\u2081 n'\u22610 = subst A (sym n'\u22610) A0\n\n prf\u2082 : \u2200 {n'} \u2192 \u2203 (\u03bb m \u2192 n' \u2261 succ\u2081 m \u2227 A m) \u2192 A n'\n prf\u2082 (_ , n'\u2261Sm , Am) = subst A (sym n'\u2261Sm) (is Am)\n\n-- The induction principle for N *with* the hypothesis N n in the\n-- induction step.\n--\n-- 2012-03-06. We cannot proof this principle because N-lfp\u2082 does not\n-- have the hypothesis N n.\n--\n-- indN\u2082 : (A : D \u2192 Set) \u2192\n-- A zero \u2192\n-- (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n-- \u2200 {n} \u2192 N n \u2192 A n\n-- indN\u2082 A A0 is Nn = N-lfp\u2082 A [ prf\u2081 , prf\u2082 ] Nn\n-- where\n-- prf\u2081 : \u2200 {n'} \u2192 n' \u2261 zero \u2192 A n'\n-- prf\u2081 n'\u22610 = subst A (sym n'\u22610) A0\n\n-- prf\u2082 : \u2200 {n'} \u2192 \u2203 (\u03bb m \u2192 n' \u2261 succ\u2081 m \u2227 A m) \u2192 A n'\n-- prf\u2082 {n'} (m , n'\u2261Sm , Am) = subst A (sym n'\u2261Sm) (is helper Am)\n-- where\n-- helper : N m\n-- helper = [ prf\u2083 , prf\u2084 ] (N-lfp\u2083 {!!})\n-- where\n-- prf\u2083 : n' \u2261 zero \u2192 N m\n-- prf\u2083 n'\u22610 = \u22a5-elim (0\u2262S (trans (sym n'\u22610) n'\u2261Sm))\n\n-- prf\u2084 : \u2203 (\u03bb m' \u2192 n' \u2261 succ\u2081 m' \u2227 N m') \u2192 N m\n-- prf\u2084 (_ , n'\u2261Sm' , Nm') =\n-- subst N (succInjective (trans (sym n'\u2261Sm') n'\u2261Sm)) Nm'\n\n------------------------------------------------------------------------------\n-- Example: We will use N-lfp\u2082 as the induction principle on N.\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ\u2081 d + e \u2261 succ\u2081 (d + e)\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity n = +-0x n\n\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {n = n} Nm Nn = N-lfp\u2082 A prf Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n prf : \u2200 {m'} \u2192 m' \u2261 zero \u2228 \u2203 (\u03bb m'' \u2192 m' \u2261 succ\u2081 m'' \u2227 A m'') \u2192 A m'\n prf h = case prf\u2081 prf\u2082 h\n where\n A0 : A zero\n A0 = subst N (sym (+-leftIdentity n)) Nn\n\n prf\u2081 : \u2200 {m} \u2192 m \u2261 zero \u2192 A m\n prf\u2081 h\u2081 = subst N (cong (flip _+_ n) (sym h\u2081)) A0\n\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} ih = subst N (sym (+-Sx i n)) (sN ih)\n\n prf\u2082 : \u2200 {m} \u2192 \u2203 (\u03bb m'' \u2192 m \u2261 succ\u2081 m'' \u2227 A m'') \u2192 A m\n prf\u2082 (_ , m\u2261Sm'' , Am'') =\n subst N (cong (flip _+_ n) (sym m\u2261Sm'')) (is Am'')\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"db2f77d45aa65090c8c3fb549e22ff6740c08815","subject":"Added x+1+y\u22611+x+y (ER version).","message":"Added x+1+y\u22611+x+y (ER version).\n\nIgnore-this: cc262563efb9ce925db01b6a8ada7680\n\ndarcs-hash:20100531135507-3bd4e-18c905f00f1d32eeba52f68aef14a2d237602dbb.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Function\/Arithmetic\/PropertiesER.agda","new_file":"LTC\/Function\/Arithmetic\/PropertiesER.agda","new_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties using equational reasoning\n------------------------------------------------------------------------------\n\nmodule LTC.Function.Arithmetic.PropertiesER where\n\nopen import LTC.Minimal\nopen import LTC.MinimalER\n\nopen import LTC.Data.N\nopen import LTC.Function.Arithmetic\nopen import LTC.Function.Arithmetic.Properties using ( +-comm )\nopen import LTC.Relation.Equalities.PropertiesER\n\nopen import MyStdLib.Function\nimport MyStdLib.Relation.Binary.EqReasoning\nopen module APER = MyStdLib.Relation.Binary.EqReasoning.StdLib _\u2261_ refl trans\n\n------------------------------------------------------------------------------\n\nminus-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m - n)\nminus-N {m} Nm zN = subst (\u03bb t \u2192 N t) (sym (minus-x0 m)) Nm\nminus-N zN (sN {n} Nn) = subst (\u03bb t \u2192 N t) (sym (minus-0S n)) zN\nminus-N (sN {m} Nm) (sN {n} Nn) = subst (\u03bb t \u2192 N t)\n (sym (minus-SS m n))\n (minus-N Nm Nn)\n\nminus-0x : {n : D} \u2192 N n \u2192 zero - n \u2261 zero\nminus-0x zN = minus-x0 zero\nminus-0x (sN {n} Nn) = minus-0S n\n\n+-leftIdentity : {n : D} \u2192 N n \u2192 zero + n \u2261 n\n+-leftIdentity {n} _ = +-0x n\n\n+-rightIdentity : {n : D} \u2192 N n \u2192 n + zero \u2261 n\n+-rightIdentity zN = +-leftIdentity zN\n+-rightIdentity (sN {n} Nn) =\n trans (+-Sx n zero)\n (subst (\u03bb t \u2192 succ (n + zero) \u2261 succ t)\n (+-rightIdentity Nn)\n refl\n )\n\n+-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N zN Nn = subst (\u03bb t \u2192 N t) (sym (+-leftIdentity Nn)) Nn\n+-N {n = n} (sN {m} Nm ) Nn =\n subst (\u03bb t \u2192 N t) (sym (+-Sx m n)) (sN (+-N Nm Nn))\n\n+-assoc : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc {n = n} {o = o} zN Nn No =\n begin\n zero + n + o \u2261\u27e8 subst (\u03bb t \u2192 zero + n + o \u2261 t + o)\n (+-leftIdentity Nn)\n refl\n \u27e9\n n + o \u2261\u27e8 sym (+-leftIdentity (+-N Nn No)) \u27e9\n zero + (n + o)\n \u220e\n\n+-assoc {n = n} {o = o} (sN {m} Nm) Nn No =\n begin\n succ m + n + o \u2261\u27e8 subst (\u03bb t \u2192 succ m + n + o \u2261 t + o)\n (+-Sx m n)\n refl\n \u27e9\n succ (m + n) + o \u2261\u27e8 +-Sx (m + n) o \u27e9\n succ (m + n + o) \u2261\u27e8 subst (\u03bb t \u2192 succ (m + n + o) \u2261 succ t)\n (+-assoc Nm Nn No)\n refl\n \u27e9\n succ (m + (n + o)) \u2261\u27e8 sym (+-Sx m (n + o)) \u27e9\n succ m + (n + o)\n \u220e\n\n[x+y]-[x+z]\u2261y-z : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192\n (m + n) - (m + o) \u2261 n - o\n[x+y]-[x+z]\u2261y-z {n = n} {o} zN Nn No =\n begin\n (zero + n) - (zero + o) \u2261\u27e8 subst (\u03bb t \u2192 (zero + n) - (zero + o) \u2261\n t - (zero + o))\n (+-0x n) refl\n \u27e9\n n - (zero + o) \u2261\u27e8 subst (\u03bb t \u2192 n - (zero + o) \u2261 n - t)\n (+-0x o)\n refl \u27e9\n n - o\n \u220e\n\n[x+y]-[x+z]\u2261y-z {n = n} {o} (sN {m} Nm) Nn No =\n begin\n (succ m + n) - (succ m + o) \u2261\u27e8 subst (\u03bb t \u2192 succ m + n - (succ m + o) \u2261\n t - (succ m + o))\n (+-Sx m n)\n refl\n \u27e9\n succ (m + n) - (succ m + o) \u2261\u27e8 subst (\u03bb t \u2192 succ (m + n) - (succ m + o) \u2261\n succ (m + n) - t)\n (+-Sx m o)\n refl \u27e9\n succ (m + n) - succ (m + o) \u2261\u27e8 minus-SS (m + n) (m + o) \u27e9\n (m + n) - (m + o) \u2261\u27e8 [x+y]-[x+z]\u2261y-z Nm Nn No \u27e9\n n - o\n \u220e\n\n*-leftZero : {n : D} \u2192 N n \u2192 zero * n \u2261 zero\n*-leftZero {n} _ = *-0x n\n\n*-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m * n)\n*-N zN Nn = subst (\u03bb t \u2192 N t) (sym (*-leftZero Nn)) zN\n*-N {n = n} (sN {m} Nm) Nn =\n subst (\u03bb t \u2192 N t) (sym (*-Sx m n)) (+-N Nn (*-N Nm Nn))\n\n*-rightZero : {n : D} \u2192 N n \u2192 n * zero \u2261 zero\n*-rightZero zN = *-leftZero zN\n*-rightZero (sN {n} Nn) =\n trans (*-Sx n zero)\n (trans (+-leftIdentity (*-N Nn zN)) (*-rightZero Nn))\n\nx*1+y\u2261x+xy : {m n : D} \u2192 N m \u2192 N n \u2192 m * succ n \u2261 m + m * n\nx*1+y\u2261x+xy {n = n} zN Nn = sym\n (\n begin\n zero + zero * n \u2261\u27e8 subst (\u03bb t \u2192 zero + zero * n \u2261 zero + t)\n (*-leftZero Nn)\n refl\n \u27e9\n zero + zero \u2261\u27e8 +-leftIdentity zN \u27e9\n zero \u2261\u27e8 sym (*-leftZero (sN Nn)) \u27e9\n zero * succ n\n \u220e\n )\n\nx*1+y\u2261x+xy {n = n} (sN {m} Nm) Nn =\n begin\n succ m * succ n \u2261\u27e8 *-Sx m (succ n) \u27e9\n succ n + m * succ n \u2261\u27e8 subst (\u03bb t \u2192 succ n + m * succ n \u2261 succ n + t)\n (x*1+y\u2261x+xy Nm Nn)\n refl\n \u27e9\n succ n + (m + m * n) \u2261\u27e8 +-Sx n (m + m * n) \u27e9\n succ (n + (m + m * n)) \u2261\u27e8 subst (\u03bb t \u2192 succ (n + (m + m * n)) \u2261 succ t)\n (sym (+-assoc Nn Nm (*-N Nm Nn)))\n refl\n \u27e9\n succ (n + m + m * n) \u2261\u27e8 subst (\u03bb t \u2192 succ (n + m + m * n) \u2261\n succ (t + m * n))\n (+-comm Nn Nm)\n refl\n \u27e9\n succ (m + n + m * n) \u2261\u27e8 subst (\u03bb t \u2192 succ (m + n + m * n) \u2261 succ t)\n (+-assoc Nm Nn (*-N Nm Nn))\n refl\n \u27e9\n\n succ (m + (n + m * n)) \u2261\u27e8 sym (+-Sx m (n + m * n)) \u27e9\n succ m + (n + m * n) \u2261\u27e8 subst (\u03bb t \u2192 succ m + (n + m * n) \u2261 succ m + t)\n (sym (*-Sx m n))\n refl\n \u27e9\n succ m + succ m * n\n \u220e\n\n*-comm : {m n : D} \u2192 N m \u2192 N n \u2192 m * n \u2261 n * m\n*-comm zN Nn = trans (*-leftZero Nn) (sym (*-rightZero Nn))\n*-comm {n = n} (sN {m} Nm) Nn =\n begin\n succ m * n \u2261\u27e8 *-Sx m n \u27e9\n n + m * n \u2261\u27e8 subst (\u03bb t \u2192 n + m * n \u2261 n + t)\n (*-comm Nm Nn)\n refl\n \u27e9\n n + n * m \u2261\u27e8 sym (x*1+y\u2261x+xy Nn Nm) \u27e9\n n * succ m\n \u220e\n\n[x-y]z\u2261xz*yz : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 (m - n) * o \u2261 m * o - n * o\n[x-y]z\u2261xz*yz {m} {o = o} Nm zN No =\n begin\n (m - zero) * o \u2261\u27e8 subst (\u03bb t \u2192 (m - zero) * o \u2261 t * o)\n (minus-x0 m)\n refl\n \u27e9\n m * o \u2261\u27e8 sym (minus-x0 (m * o)) \u27e9\n m * o - zero \u2261\u27e8 subst (\u03bb t \u2192 m * o - zero \u2261 m * o - t)\n (sym (*-0x o))\n refl\n \u27e9\n m * o - zero * o\n \u220e\n\n[x-y]z\u2261xz*yz {o = o} zN (sN {n} Nn) No =\n begin\n (zero - succ n) * o \u2261\u27e8 subst (\u03bb t \u2192 (zero - succ n) * o \u2261 t * o)\n (minus-0S n)\n refl\n \u27e9\n zero * o \u2261\u27e8 *-0x o \u27e9\n zero \u2261\u27e8 sym (minus-0x (*-N (sN Nn) No)) \u27e9\n zero - succ n * o \u2261\u27e8 subst (\u03bb t \u2192 zero - succ n * o \u2261 t - succ n * o)\n (sym (*-0x o))\n refl\n \u27e9\n zero * o - succ n * o\n \u220e\n\n[x-y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) zN =\n begin\n (succ m - succ n) * zero \u2261\u27e8 *-comm (minus-N (sN Nm) (sN Nn)) zN \u27e9\n zero * (succ m - succ n) \u2261\u27e8 *-0x (succ m - succ n) \u27e9\n zero \u2261\u27e8 sym (minus-0x (*-N (sN Nn) zN)) \u27e9\n zero - succ n * zero \u2261\u27e8 subst (\u03bb t \u2192 zero - succ n * zero \u2261\n t - succ n * zero)\n (sym (*-0x (succ m)))\n refl\n \u27e9\n zero * succ m - succ n * zero \u2261\u27e8 subst\n (\u03bb t \u2192 zero * succ m - succ n * zero \u2261\n t - succ n * zero)\n (*-comm zN (sN Nm))\n refl\n \u27e9\n succ m * zero - succ n * zero\n \u220e\n\n[x-y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) (sN {o} No) =\n begin\n (succ m - succ n) * succ o \u2261\u27e8 subst (\u03bb t \u2192 (succ m - succ n) * succ o \u2261\n t * succ o)\n (minus-SS m n)\n refl\n \u27e9\n (m - n) * succ o \u2261\u27e8 [x-y]z\u2261xz*yz Nm Nn (sN No) \u27e9\n m * succ o - n * succ o \u2261\u27e8 sym ([x+y]-[x+z]\u2261y-z (sN No)\n (*-N Nm (sN No))\n (*-N Nn (sN No)))\n \u27e9\n (succ o + m * succ o) - (succ o + n * succ o)\n \u2261\u27e8 subst (\u03bb t \u2192 (succ o + m * succ o) - (succ o + n * succ o) \u2261\n t - (succ o + n * succ o))\n (sym (*-Sx m (succ o)))\n refl\n \u27e9\n (succ m * succ o) - (succ o + n * succ o)\n \u2261\u27e8 subst (\u03bb t \u2192 (succ m * succ o) - (succ o + n * succ o) \u2261\n (succ m * succ o) - t)\n (sym (*-Sx n (succ o)))\n refl\n \u27e9\n (succ m * succ o) - (succ n * succ o)\n \u220e\n\n[x+y]z\u2261xz*yz : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 (m + n) * o \u2261 m * o + n * o\n[x+y]z\u2261xz*yz {m} {n} Nm Nn zN =\n begin\n (m + n) * zero \u2261\u27e8 *-comm (+-N Nm Nn) zN \u27e9\n zero * (m + n) \u2261\u27e8 *-0x (m + n) \u27e9\n zero \u2261\u27e8 sym (*-0x m) \u27e9\n zero * m \u2261\u27e8 *-comm zN Nm \u27e9\n m * zero \u2261\u27e8 sym (+-rightIdentity (*-N Nm zN)) \u27e9\n m * zero + zero \u2261\u27e8 subst (\u03bb t \u2192 m * zero + zero \u2261 m * zero + t)\n (trans (sym (*-0x n)) (*-comm zN Nn))\n refl\n \u27e9\n m * zero + n * zero\n \u220e\n\n[x+y]z\u2261xz*yz {n = n} zN Nn (sN {o} No) =\n begin\n (zero + n) * succ o \u2261\u27e8 subst (\u03bb t \u2192 (zero + n) * succ o \u2261 t * succ o)\n (+-leftIdentity Nn)\n refl\n \u27e9\n n * succ o \u2261\u27e8 sym (+-leftIdentity (*-N Nn (sN No))) \u27e9\n zero + n * succ o \u2261\u27e8 subst (\u03bb t \u2192 zero + n * succ o \u2261 t + n * succ o)\n (sym (*-0x (succ o)))\n refl\n \u27e9\n zero * succ o + n * succ o\n \u220e\n\n[x+y]z\u2261xz*yz (sN {m} Nm) zN (sN {o} No) =\n begin\n (succ m + zero) * succ o \u2261\u27e8 subst (\u03bb t \u2192 (succ m + zero) * succ o \u2261\n t * succ o)\n (+-rightIdentity (sN Nm))\n refl\n \u27e9\n succ m * succ o \u2261\u27e8 sym (+-rightIdentity (*-N (sN Nm) (sN No))) \u27e9\n succ m * succ o + zero \u2261\u27e8 subst (\u03bb t \u2192 succ m * succ o + zero \u2261\n succ m * succ o + t)\n (sym (*-leftZero (sN No)))\n refl\n \u27e9\n succ m * succ o + zero * succ o\n \u220e\n\n[x+y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) (sN {o} No) =\n begin\n (succ m + succ n) * succ o\n \u2261\u27e8 subst (\u03bb t \u2192 (succ m + succ n) * succ o \u2261 t * succ o)\n (+-Sx m (succ n))\n refl\n \u27e9\n succ ( m + succ n) * succ o \u2261\u27e8 *-Sx (m + succ n) (succ o) \u27e9\n succ o + (m + succ n) * succ o\n \u2261\u27e8 subst (\u03bb t \u2192 succ o + (m + succ n) * succ o \u2261 succ o + t)\n ([x+y]z\u2261xz*yz Nm (sN Nn) (sN No))\n refl\n \u27e9\n succ o + (m * succ o + succ n * succ o)\n \u2261\u27e8 sym (+-assoc (sN No) (*-N Nm (sN No)) (*-N (sN Nn) (sN No))) \u27e9\n succ o + m * succ o + succ n * succ o\n \u2261\u27e8 subst (\u03bb t \u2192 succ o + m * succ o + succ n * succ o \u2261\n t + succ n * succ o)\n (sym (*-Sx m (succ o)))\n refl\n \u27e9\n succ m * succ o + succ n * succ o\n \u220e","old_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties using equational reasoning\n------------------------------------------------------------------------------\n\nmodule LTC.Function.Arithmetic.PropertiesER where\n\nopen import LTC.Minimal\nopen import LTC.MinimalER\n\nopen import LTC.Data.N\nopen import LTC.Function.Arithmetic\nopen import LTC.Function.Arithmetic.Properties\n using ( x*1+y\u2261x+xy )\nopen import LTC.Relation.Equalities.PropertiesER\n\nopen import MyStdLib.Function\nimport MyStdLib.Relation.Binary.EqReasoning\nopen module APER = MyStdLib.Relation.Binary.EqReasoning.StdLib _\u2261_ refl trans\n\n------------------------------------------------------------------------------\n\nminus-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m - n)\nminus-N {m} Nm zN = subst (\u03bb t \u2192 N t) (sym (minus-x0 m)) Nm\nminus-N zN (sN {n} Nn) = subst (\u03bb t \u2192 N t) (sym (minus-0S n)) zN\nminus-N (sN {m} Nm) (sN {n} Nn) = subst (\u03bb t \u2192 N t)\n (sym (minus-SS m n))\n (minus-N Nm Nn)\n\nminus-0x : {n : D} \u2192 N n \u2192 zero - n \u2261 zero\nminus-0x zN = minus-x0 zero\nminus-0x (sN {n} Nn) = minus-0S n\n\n+-leftIdentity : {n : D} \u2192 N n \u2192 zero + n \u2261 n\n+-leftIdentity {n} _ = +-0x n\n\n+-rightIdentity : {n : D} \u2192 N n \u2192 n + zero \u2261 n\n+-rightIdentity zN = +-leftIdentity zN\n+-rightIdentity (sN {n} Nn) =\n trans (+-Sx n zero)\n (subst (\u03bb t \u2192 succ (n + zero) \u2261 succ t)\n (+-rightIdentity Nn)\n refl\n )\n\n+-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N zN Nn = subst (\u03bb t \u2192 N t) (sym (+-leftIdentity Nn)) Nn\n+-N {n = n} (sN {m} Nm ) Nn =\n subst (\u03bb t \u2192 N t) (sym (+-Sx m n)) (sN (+-N Nm Nn))\n\n+-assoc : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc {n = n} {o = o} zN Nn No =\n begin\n zero + n + o \u2261\u27e8 subst (\u03bb t \u2192 zero + n + o \u2261 t + o)\n (+-leftIdentity Nn)\n refl\n \u27e9\n n + o \u2261\u27e8 sym (+-leftIdentity (+-N Nn No)) \u27e9\n zero + (n + o)\n \u220e\n\n+-assoc {n = n} {o = o} (sN {m} Nm) Nn No =\n begin\n succ m + n + o \u2261\u27e8 subst (\u03bb t \u2192 succ m + n + o \u2261 t + o)\n (+-Sx m n)\n refl\n \u27e9\n succ (m + n) + o \u2261\u27e8 +-Sx (m + n) o \u27e9\n succ (m + n + o) \u2261\u27e8 subst (\u03bb t \u2192 succ (m + n + o) \u2261 succ t)\n (+-assoc Nm Nn No)\n refl\n \u27e9\n succ (m + (n + o)) \u2261\u27e8 sym (+-Sx m (n + o)) \u27e9\n succ m + (n + o)\n \u220e\n\n[x+y]-[x+z]\u2261y-z : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192\n (m + n) - (m + o) \u2261 n - o\n[x+y]-[x+z]\u2261y-z {n = n} {o} zN Nn No =\n begin\n (zero + n) - (zero + o) \u2261\u27e8 subst (\u03bb t \u2192 (zero + n) - (zero + o) \u2261\n t - (zero + o))\n (+-0x n) refl\n \u27e9\n n - (zero + o) \u2261\u27e8 subst (\u03bb t \u2192 n - (zero + o) \u2261 n - t)\n (+-0x o)\n refl \u27e9\n n - o\n \u220e\n\n[x+y]-[x+z]\u2261y-z {n = n} {o} (sN {m} Nm) Nn No =\n begin\n (succ m + n) - (succ m + o) \u2261\u27e8 subst (\u03bb t \u2192 succ m + n - (succ m + o) \u2261\n t - (succ m + o))\n (+-Sx m n)\n refl\n \u27e9\n succ (m + n) - (succ m + o) \u2261\u27e8 subst (\u03bb t \u2192 succ (m + n) - (succ m + o) \u2261\n succ (m + n) - t)\n (+-Sx m o)\n refl \u27e9\n succ (m + n) - succ (m + o) \u2261\u27e8 minus-SS (m + n) (m + o) \u27e9\n (m + n) - (m + o) \u2261\u27e8 [x+y]-[x+z]\u2261y-z Nm Nn No \u27e9\n n - o\n \u220e\n\n*-leftZero : {n : D} \u2192 N n \u2192 zero * n \u2261 zero\n*-leftZero {n} _ = *-0x n\n\n*-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m * n)\n*-N zN Nn = subst (\u03bb t \u2192 N t) (sym (*-leftZero Nn)) zN\n*-N {n = n} (sN {m} Nm) Nn =\n subst (\u03bb t \u2192 N t) (sym (*-Sx m n)) (+-N Nn (*-N Nm Nn))\n\n*-rightZero : {n : D} \u2192 N n \u2192 n * zero \u2261 zero\n*-rightZero zN = *-leftZero zN\n*-rightZero (sN {n} Nn) =\n trans (*-Sx n zero)\n (trans (+-leftIdentity (*-N Nn zN)) (*-rightZero Nn))\n\n*-comm : {m n : D} \u2192 N m \u2192 N n \u2192 m * n \u2261 n * m\n*-comm zN Nn = trans (*-leftZero Nn) (sym (*-rightZero Nn))\n*-comm {n = n} (sN {m} Nm) Nn =\n begin\n succ m * n \u2261\u27e8 *-Sx m n \u27e9\n n + m * n \u2261\u27e8 subst (\u03bb t \u2192 n + m * n \u2261 n + t)\n (*-comm Nm Nn)\n refl\n \u27e9\n n + n * m \u2261\u27e8 sym (x*1+y\u2261x+xy Nn Nm) \u27e9\n n * succ m\n \u220e\n\n[x-y]z\u2261xz*yz : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 (m - n) * o \u2261 m * o - n * o\n[x-y]z\u2261xz*yz {m} {o = o} Nm zN No =\n begin\n (m - zero) * o \u2261\u27e8 subst (\u03bb t \u2192 (m - zero) * o \u2261 t * o)\n (minus-x0 m)\n refl\n \u27e9\n m * o \u2261\u27e8 sym (minus-x0 (m * o)) \u27e9\n m * o - zero \u2261\u27e8 subst (\u03bb t \u2192 m * o - zero \u2261 m * o - t)\n (sym (*-0x o))\n refl\n \u27e9\n m * o - zero * o\n \u220e\n\n[x-y]z\u2261xz*yz {o = o} zN (sN {n} Nn) No =\n begin\n (zero - succ n) * o \u2261\u27e8 subst (\u03bb t \u2192 (zero - succ n) * o \u2261 t * o)\n (minus-0S n)\n refl\n \u27e9\n zero * o \u2261\u27e8 *-0x o \u27e9\n zero \u2261\u27e8 sym (minus-0x (*-N (sN Nn) No)) \u27e9\n zero - succ n * o \u2261\u27e8 subst (\u03bb t \u2192 zero - succ n * o \u2261 t - succ n * o)\n (sym (*-0x o))\n refl\n \u27e9\n zero * o - succ n * o\n \u220e\n\n[x-y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) zN =\n begin\n (succ m - succ n) * zero \u2261\u27e8 *-comm (minus-N (sN Nm) (sN Nn)) zN \u27e9\n zero * (succ m - succ n) \u2261\u27e8 *-0x (succ m - succ n) \u27e9\n zero \u2261\u27e8 sym (minus-0x (*-N (sN Nn) zN)) \u27e9\n zero - succ n * zero \u2261\u27e8 subst (\u03bb t \u2192 zero - succ n * zero \u2261\n t - succ n * zero)\n (sym (*-0x (succ m)))\n refl\n \u27e9\n zero * succ m - succ n * zero \u2261\u27e8 subst\n (\u03bb t \u2192 zero * succ m - succ n * zero \u2261\n t - succ n * zero)\n (*-comm zN (sN Nm))\n refl\n \u27e9\n succ m * zero - succ n * zero\n \u220e\n\n[x-y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) (sN {o} No) =\n begin\n (succ m - succ n) * succ o \u2261\u27e8 subst (\u03bb t \u2192 (succ m - succ n) * succ o \u2261\n t * succ o)\n (minus-SS m n)\n refl\n \u27e9\n (m - n) * succ o \u2261\u27e8 [x-y]z\u2261xz*yz Nm Nn (sN No) \u27e9\n m * succ o - n * succ o \u2261\u27e8 sym ([x+y]-[x+z]\u2261y-z (sN No)\n (*-N Nm (sN No))\n (*-N Nn (sN No)))\n \u27e9\n (succ o + m * succ o) - (succ o + n * succ o)\n \u2261\u27e8 subst (\u03bb t \u2192 (succ o + m * succ o) - (succ o + n * succ o) \u2261\n t - (succ o + n * succ o))\n (sym (*-Sx m (succ o)))\n refl\n \u27e9\n (succ m * succ o) - (succ o + n * succ o)\n \u2261\u27e8 subst (\u03bb t \u2192 (succ m * succ o) - (succ o + n * succ o) \u2261\n (succ m * succ o) - t)\n (sym (*-Sx n (succ o)))\n refl\n \u27e9\n (succ m * succ o) - (succ n * succ o)\n \u220e\n\n[x+y]z\u2261xz*yz : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 (m + n) * o \u2261 m * o + n * o\n[x+y]z\u2261xz*yz {m} {n} Nm Nn zN =\n begin\n (m + n) * zero \u2261\u27e8 *-comm (+-N Nm Nn) zN \u27e9\n zero * (m + n) \u2261\u27e8 *-0x (m + n) \u27e9\n zero \u2261\u27e8 sym (*-0x m) \u27e9\n zero * m \u2261\u27e8 *-comm zN Nm \u27e9\n m * zero \u2261\u27e8 sym (+-rightIdentity (*-N Nm zN)) \u27e9\n m * zero + zero \u2261\u27e8 subst (\u03bb t \u2192 m * zero + zero \u2261 m * zero + t)\n (trans (sym (*-0x n)) (*-comm zN Nn))\n refl\n \u27e9\n m * zero + n * zero\n \u220e\n\n[x+y]z\u2261xz*yz {n = n} zN Nn (sN {o} No) =\n begin\n (zero + n) * succ o \u2261\u27e8 subst (\u03bb t \u2192 (zero + n) * succ o \u2261 t * succ o)\n (+-leftIdentity Nn)\n refl\n \u27e9\n n * succ o \u2261\u27e8 sym (+-leftIdentity (*-N Nn (sN No))) \u27e9\n zero + n * succ o \u2261\u27e8 subst (\u03bb t \u2192 zero + n * succ o \u2261 t + n * succ o)\n (sym (*-0x (succ o)))\n refl\n \u27e9\n zero * succ o + n * succ o\n \u220e\n\n[x+y]z\u2261xz*yz (sN {m} Nm) zN (sN {o} No) =\n begin\n (succ m + zero) * succ o \u2261\u27e8 subst (\u03bb t \u2192 (succ m + zero) * succ o \u2261\n t * succ o)\n (+-rightIdentity (sN Nm))\n refl\n \u27e9\n succ m * succ o \u2261\u27e8 sym (+-rightIdentity (*-N (sN Nm) (sN No))) \u27e9\n succ m * succ o + zero \u2261\u27e8 subst (\u03bb t \u2192 succ m * succ o + zero \u2261\n succ m * succ o + t)\n (sym (*-leftZero (sN No)))\n refl\n \u27e9\n succ m * succ o + zero * succ o\n \u220e\n\n[x+y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) (sN {o} No) =\n begin\n (succ m + succ n) * succ o\n \u2261\u27e8 subst (\u03bb t \u2192 (succ m + succ n) * succ o \u2261 t * succ o)\n (+-Sx m (succ n))\n refl\n \u27e9\n succ ( m + succ n) * succ o \u2261\u27e8 *-Sx (m + succ n) (succ o) \u27e9\n succ o + (m + succ n) * succ o\n \u2261\u27e8 subst (\u03bb t \u2192 succ o + (m + succ n) * succ o \u2261 succ o + t)\n ([x+y]z\u2261xz*yz Nm (sN Nn) (sN No))\n refl\n \u27e9\n succ o + (m * succ o + succ n * succ o)\n \u2261\u27e8 sym (+-assoc (sN No) (*-N Nm (sN No)) (*-N (sN Nn) (sN No))) \u27e9\n succ o + m * succ o + succ n * succ o\n \u2261\u27e8 subst (\u03bb t \u2192 succ o + m * succ o + succ n * succ o \u2261\n t + succ n * succ o)\n (sym (*-Sx m (succ o)))\n refl\n \u27e9\n succ m * succ o + succ n * succ o\n \u220e","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"1242f3ff7ac516bd4e5d6845eb083992c3527b0b","subject":"Updated agda2atp: The TPTP definitions use an unique name.","message":"Updated agda2atp: The TPTP definitions use an unique name.\n\nIgnore-this: 46560a3592efd4fdf23dc29fe3ee2d5a\n\ndarcs-hash:20101201173120-3bd4e-1c669d5b56abab12ba22ba65c0c8ed7ac50be6b8.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/PA\/Properties.agda","new_file":"src\/PA\/Properties.agda","new_contents":"------------------------------------------------------------------------------\n-- PA properties\n------------------------------------------------------------------------------\n\nmodule PA.Properties where\n\nopen import PA.Base\n\n------------------------------------------------------------------------------\n-- Some proofs are based on the proofs in the standard library.\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity = S\u2085\n\n+-rightIdentity : \u2200 n \u2192 n + zero \u2261 n\n+-rightIdentity = S\u2089 P P0 iStep\n where\n P : \u2115 \u2192 Set\n P i = i + zero \u2261 i\n {-# ATP definition P #-}\n\n P0 : P zero\n P0 = S\u2085 zero\n\n postulate\n iStep : \u2200 i \u2192 P i \u2192 P (succ i)\n {-# ATP prove iStep #-}\n\n+-assoc : \u2200 m n o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc m n o = S\u2089 P P0 iStep m\n where\n P : \u2115 \u2192 Set\n P i = i + n + o \u2261 i + (n + o)\n {-# ATP definition P #-}\n\n postulate\n P0 : P zero\n {-# ATP prove P0 #-}\n\n postulate\n iStep : \u2200 i \u2192 P i \u2192 P (succ i)\n {-# ATP prove iStep #-}\n\nx+1+y\u22611+x+y : \u2200 m n \u2192 m + succ n \u2261 succ (m + n)\nx+1+y\u22611+x+y m n = S\u2089 P P0 iStep m\n where\n P : \u2115 \u2192 Set\n P i = i + succ n \u2261 succ (i + n)\n {-# ATP definition P #-}\n\n postulate\n P0 : P zero\n {-# ATP prove P0 #-}\n\n postulate\n iStep : \u2200 i \u2192 P i \u2192 P (succ i)\n {-# ATP prove iStep #-}\n\n+-comm : \u2200 m n \u2192 m + n \u2261 n + m\n+-comm m n = S\u2089 P P0 iStep m\n where\n P : \u2115 \u2192 Set\n P i = i + n \u2261 n + i\n {-# ATP definition P #-}\n\n postulate\n P0 : P zero\n {-# ATP prove P0 +-rightIdentity #-}\n\n postulate\n iStep : \u2200 i \u2192 P i \u2192 P (succ i)\n {-# ATP prove iStep x+1+y\u22611+x+y #-}\n","old_contents":"------------------------------------------------------------------------------\n-- PA properties\n------------------------------------------------------------------------------\n\nmodule PA.Properties where\n\nopen import PA.Base\n\n------------------------------------------------------------------------------\n-- Some proofs are based on the proofs in the standard library.\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity = S\u2085\n\n+-rightIdentity : \u2200 n \u2192 n + zero \u2261 n\n+-rightIdentity = S\u2089 P P0 iStep\n where\n P : \u2115 \u2192 Set\n P i = i + zero \u2261 i\n\n P0 : zero + zero \u2261 zero\n P0 = S\u2085 zero\n\n postulate\n iStep : \u2200 i \u2192\n i + zero \u2261 i \u2192\n succ i + zero \u2261 succ i\n {-# ATP prove iStep #-}\n\n+-assoc : \u2200 m n o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc m n o = S\u2089 P P0 iStep m\n where\n P : \u2115 \u2192 Set\n P i = i + n + o \u2261 i + (n + o)\n\n postulate\n P0 : zero + n + o \u2261 zero + (n + o)\n {-# ATP prove P0 #-}\n\n postulate\n iStep : \u2200 i \u2192\n i + n + o \u2261 i + (n + o) \u2192\n succ i + n + o \u2261 succ i + (n + o)\n {-# ATP prove iStep #-}\n\nx+1+y\u22611+x+y : \u2200 m n \u2192 m + succ n \u2261 succ (m + n)\nx+1+y\u22611+x+y m n = S\u2089 P P0 iStep m\n where\n P : \u2115 \u2192 Set\n P i = i + succ n \u2261 succ (i + n)\n\n postulate\n P0 : zero + succ n \u2261 succ (zero + n)\n {-# ATP prove P0 #-}\n\n postulate\n iStep : \u2200 i \u2192\n i + succ n \u2261 succ (i + n) \u2192\n succ i + succ n \u2261 succ (succ i + n)\n {-# ATP prove iStep #-}\n\n+-comm : \u2200 m n \u2192 m + n \u2261 n + m\n+-comm m n = S\u2089 P P0 iStep m\n where\n P : \u2115 \u2192 Set\n P i = i + n \u2261 n + i\n\n postulate\n P0 : zero + n \u2261 n + zero\n {-# ATP prove P0 +-rightIdentity #-}\n\n postulate\n iStep : \u2200 i \u2192\n i + n \u2261 n + i \u2192\n succ i + n \u2261 n + succ i\n {-# ATP prove iStep x+1+y\u22611+x+y #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b0b16a5abb26bcc3d09fb6b74b2343c21159e33c","subject":"#31 missed another file","message":"#31 missed another file\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress.agda","new_file":"progress.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\n\nopen import canonical-boxed-forms\nopen import canonical-indeterminate-forms\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d boxedval + d indet + d casterr[ \u0394 ] + \u03a3[ d' \u2208 dhexp ] (d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 ok d \u0394\n E : \u2200{d \u0394} \u2192 d casterr \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n V : \u2200{d \u0394} \u2192 d boxedval \u2192 ok d \u0394\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = V (BVVal VConst)\n\n -- variables\n progress (TAVar x\u2081) = abort (somenotnone (! x\u2081))\n\n -- lambdas\n progress (TALam wt) = V (BVVal VLam)\n\n -- applications\n progress (TAAp wt1 wt2)\n with progress wt1 | progress wt2\n -- if the left steps, the whole thing steps\n progress (TAAp wt1 wt2) | S (_ , Step x y z) | _ = S (_ , Step (FHAp1 x) y (FHAp1 z))\n -- if the left is an error, the whole thing is an error\n progress (TAAp wt1 wt2) | E x | _ = E (CECong (FHAp1 FHOuter) x)\n -- if the left is indeterminate, inspect the right\n progress (TAAp wt1 wt2) | I i | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n progress (TAAp wt1 wt2) | I x | E x\u2081 = E (CECong (FHAp2 FHOuter) x\u2081)\n progress (TAAp wt1 wt2) | I x | I x\u2081 = I (IAp {!!} x (FIndet x\u2081)) -- todo: check that it's not that form, otherwise the cast can progress maybe\n progress (TAAp wt1 wt2) | I x | V x\u2081 = I (IAp {!!} x (FBoxed x\u2081)) --\n -- if the left is a boxed value, inspect the right\n progress (TAAp wt1 wt2) | V v | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n progress (TAAp wt1 wt2) | V v | E e = E (CECong (FHAp2 FHOuter) e)\n progress (TAAp wt1 wt2) | V v | I i\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (x , d' , refl , qq) = S (_ , Step FHOuter ITLam FHOuter)\n ... | Inr (d' , \u03c41' , \u03c42' , refl , neq , qq) = I (IAp {!!} (ICastArr neq {!!}) (FIndet i)) --cyrus, as below\n progress (TAAp wt1 wt2) | V v | V v\u2082\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (x , d' , refl , qq) = S (_ , Step FHOuter ITLam FHOuter)\n ... | Inr (d' , \u03c41' , \u03c42' , refl , neq , qq) = I (IAp {!!} (ICastArr neq {!!}) (FBoxed v\u2082)) --cyrus\n\n -- empty holes\n progress (TAEHole x x\u2081) = I IEHole\n\n -- nonempty holes\n progress (TANEHole xin wt x\u2081)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHNEHole x) y (FHNEHole z))\n ... | E x = E (CECong (FHNEHole FHOuter) x)\n ... | I x = I (INEHole (FIndet x))\n ... | V x = I (INEHole (FBoxed x))\n\n -- casts\n progress (TACast wt con)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHCast x) y (FHCast z))\n ... | E x = E (CECong (FHCast FHOuter) x)\n -- indet cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | I x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole1) | I x = I (ICastGroundHole {!!} x) -- cyrus\n progress (TACast wt TCHole2) | I x = I (ICastHoleGround {!!} x {!!}) -- cyrus\n progress (TACast wt (TCArr c1 c2)) | I x = I (ICastArr {!!} x) -- cyrus\n -- boxed value cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | V x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole1) | V x = V (BVHoleCast {!!} x) -- cyrus\n progress (TACast wt TCHole2) | V x = V {!!} -- cyrus: missing rule for boxed values?\n progress (TACast wt (TCArr c1 c2)) | V x = V (BVArrCast {!!} x) -- cyrus\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\n\nopen import canonical-boxed-forms\nopen import canonical-indeterminate-forms\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d boxedval + d indet + d casterr[ \u0394 ] + \u03a3[ d' \u2208 dhexp ] (d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 ok d \u0394\n E : \u2200{d \u0394} \u2192 d casterr \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n V : \u2200{d \u0394} \u2192 d boxedval \u2192 ok d \u0394\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = V (BVVal VConst)\n\n -- variables\n progress (TAVar x\u2081) = abort (somenotnone (! x\u2081))\n\n -- lambdas\n progress (TALam wt) = V (BVVal VLam)\n\n -- applications\n progress (TAAp wt1 wt2)\n with progress wt1 | progress wt2\n -- if the left steps, the whole thing steps\n progress (TAAp wt1 wt2) | S (_ , Step x y z) | _ = S (_ , Step (FHAp1 x) y (FHAp1 z))\n -- if the left is an error, the whole thing is an error\n progress (TAAp wt1 wt2) | E x | _ = E (CECong (FHAp1 FHOuter) x)\n -- if the left is indeterminate, inspect the right\n progress (TAAp wt1 wt2) | I i | S (_ , Step x y z) = S (_ , Step (FHAp2 (FIndet i) x) y (FHAp2 (FIndet i) z))\n progress (TAAp wt1 wt2) | I x | E x\u2081 = E (CECong (FHAp2 (FIndet x) FHOuter) x\u2081)\n progress (TAAp wt1 wt2) | I x | I x\u2081 = I (IAp {!!} x (FIndet x\u2081)) -- todo: check that it's not that form, otherwise the cast can progress maybe\n progress (TAAp wt1 wt2) | I x | V x\u2081 = I (IAp {!!} x (FBoxed x\u2081)) --\n -- if the left is a boxed value, inspect the right\n progress (TAAp wt1 wt2) | V v | S (_ , Step x y z) = S (_ , Step (FHAp2 (FBoxed v) x) y (FHAp2 (FBoxed v) z))\n progress (TAAp wt1 wt2) | V v | E e = E (CECong (FHAp2 (FBoxed v) FHOuter) e)\n progress (TAAp wt1 wt2) | V v | I i\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (x , d' , refl , qq) = S (_ , Step FHOuter (ITLam (FIndet i)) FHOuter)\n ... | Inr (d' , \u03c41' , \u03c42' , refl , neq , qq) = I (IAp {!!} (ICastArr neq {!!}) (FIndet i)) --cyrus, as below\n progress (TAAp wt1 wt2) | V v | V v\u2082\n with canonical-boxed-forms-arr wt1 v\n ... | Inl (x , d' , refl , qq) = S (_ , Step FHOuter (ITLam (FBoxed v\u2082)) FHOuter)\n ... | Inr (d' , \u03c41' , \u03c42' , refl , neq , qq) = I (IAp {!!} (ICastArr neq {!!}) (FBoxed v\u2082)) --cyrus\n\n -- empty holes\n progress (TAEHole x x\u2081) = I IEHole\n\n -- nonempty holes\n progress (TANEHole xin wt x\u2081)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHNEHole x) y (FHNEHole z))\n ... | E x = E (CECong (FHNEHole FHOuter) x)\n ... | I x = I (INEHole (FIndet x))\n ... | V x = I (INEHole (FBoxed x))\n\n -- casts\n progress (TACast wt con)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHCast x) y (FHCast z))\n ... | E x = E (CECong (FHCast FHOuter) x)\n -- indet cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | I x = S (_ , Step FHOuter (ITCastID (FIndet x)) FHOuter)\n progress (TACast wt TCHole1) | I x = I (ICastGroundHole {!!} x) -- cyrus\n progress (TACast wt TCHole2) | I x = I (ICastHoleGround {!!} x {!!}) -- cyrus\n progress (TACast wt (TCArr c1 c2)) | I x = I (ICastArr {!!} x) -- cyrus\n -- boxed value cases, inspect how the casts are realted by consistency\n progress (TACast wt TCRefl) | V x = S (_ , Step FHOuter (ITCastID (FBoxed x)) FHOuter)\n progress (TACast wt TCHole1) | V x = V (BVHoleCast {!!} x) -- cyrus\n progress (TACast wt TCHole2) | V x = V {!!} -- cyrus: missing rule for boxed values?\n progress (TACast wt (TCArr c1 c2)) | V x = V (BVArrCast {!!} x) -- cyrus\n\n\n\n\n -- this would fill two above, but it's false: \u2987\u2988 indet but its type, \u2987\u2988, is not ground\n postulate\n lem-groundindet : \u2200{ \u0394 d \u03c4} \u2192 \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192 d indet \u2192 \u03c4 ground\n\n -- this is also false, but tempting looking in two holes above from the ap case\n postulate\n lem : \u2200{ \u0394 d1 \u03c4 \u03c4'} \u2192 \u0394 , \u2205 \u22a2 d1 :: (\u03c4 ==> \u03c4') \u2192\n d1 indet \u2192\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9))\n\n counter : \u03a3[ d \u2208 dhexp ] \u03a3[ \u03c4 \u2208 htyp ] \u03a3[ \u03c4' \u2208 htyp ] \u03a3[ \u0394 \u2208 hctx ]\n ((d indet) \u00d7 (\u0394 , \u2205 \u22a2 d :: \u03c4 ==> \u03c4'))\n counter = (\u2987\u2988\u27e8 Z , \u2205 \u27e9 \u27e8 b ==> b \u21d2 b ==> \u2987\u2988 \u27e9) ,\n b , \u2987\u2988 , \u25a0 (Z , \u2205 , b ==> b ) ,\n ICastArr (\u03bb ()) IEHole , TACast (TAEHole refl (\u03bb x d \u2192 \u03bb ())) (TCArr TCRefl TCHole1)\n\n oops : \u22a5\n oops = lem (\u03c02 (\u03c02 (\u03c02 (\u03c02 (\u03c02 counter)))))\n (\u03c01 (\u03c02 (\u03c02 (\u03c02 (\u03c02 counter))))) b b b \u2987\u2988 \u2987\u2988\u27e8 Z , \u2205 \u27e9 refl\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"0718040f19ebe9f759f025f31d845d8f2b8f381b","subject":"The agda2atp tool fixed the issue related to the eta-expansion of TPTP definitions.","message":"The agda2atp tool fixed the issue related to the eta-expansion of TPTP definitions.\n\nIgnore-this: 447b379a77cb14d71c8a7b51a2b87837\n\ndarcs-hash:20110730142709-3bd4e-d570ee27862523ca742a98082ef46b71ab657230.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Data\/Stream\/PropertiesATP.agda","new_file":"src\/FOTC\/Data\/Stream\/PropertiesATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Streams properties\n------------------------------------------------------------------------------\n\nmodule FOTC.Data.Stream.PropertiesATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Base.PropertiesATP\n\nopen import FOTC.Data.Stream.Type\n\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n\n\u2248\u2192Stream : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Stream xs \u2227 Stream ys\n\u2248\u2192Stream {xs} {ys} xs\u2248ys = Stream-gfp\u2082 P\u2081 helper\u2081 (ys , xs\u2248ys)\n , Stream-gfp\u2082 P\u2082 helper\u2082 (xs , xs\u2248ys)\n where\n P\u2081 : D \u2192 Set\n P\u2081 ws = \u2203 \u03bb zs \u2192 ws \u2248 zs\n {-# ATP definition P\u2081 #-}\n\n P\u2082 : D \u2192 Set\n P\u2082 zs = \u2203 \u03bb ws \u2192 ws \u2248 zs\n {-# ATP definition P\u2082 #-}\n\n postulate\n helper\u2081 : \u2200 {ws} \u2192 P\u2081 ws \u2192\n \u2203 (\u03bb w' \u2192 \u2203 (\u03bb ws' \u2192 ws \u2261 w' \u2237 ws' \u2227 P\u2081 ws'))\n {-# ATP prove helper\u2081 #-}\n\n postulate\n helper\u2082 : \u2200 {zs} \u2192 P\u2082 zs \u2192\n \u2203 (\u03bb z' \u2192 \u2203 (\u03bb zs' \u2192 zs \u2261 z' \u2237 zs' \u2227 P\u2082 zs'))\n {-# ATP prove helper\u2082 #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Streams properties\n------------------------------------------------------------------------------\n\nmodule FOTC.Data.Stream.PropertiesATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Base.PropertiesATP\n\nopen import FOTC.Data.Stream.Type\n\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n\n\u2248\u2192Stream : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Stream xs \u2227 Stream ys\n\u2248\u2192Stream {xs} {ys} xs\u2248ys = Stream-gfp\u2082 P\u2081 helper\u2081 (ys , xs\u2248ys)\n , Stream-gfp\u2082 P\u2082 helper\u2082 (xs , xs\u2248ys)\n where\n P\u2081 : D \u2192 Set\n P\u2081 ws = \u2203 \u03bb zs \u2192 ws \u2248 zs\n\n P\u2082 : D \u2192 Set\n P\u2082 zs = \u2203 \u03bb ws \u2192 ws \u2248 zs\n {-# ATP definition P\u2082 #-}\n\n -- TODO: We don't use the predicate P\u2081 in the type of the function\n -- helper\u2081, because at the moment the agda2atp tool doesn't handle\n -- the eta-expansion for equations.\n postulate\n helper\u2081 : \u2200 {ws} \u2192 (\u2203 \u03bb zs \u2192 ws \u2248 zs) \u2192\n \u2203 (\u03bb w' \u2192 \u2203 (\u03bb ws' \u2192 ws \u2261 w' \u2237 ws' \u2227 (\u2203 \u03bb zs \u2192 ws' \u2248 zs)))\n {-# ATP prove helper\u2081 #-}\n\n postulate\n helper\u2082 : \u2200 {zs} \u2192 P\u2082 zs \u2192\n \u2203 (\u03bb z' \u2192 \u2203 (\u03bb zs' \u2192 zs \u2261 z' \u2237 zs' \u2227 P\u2082 zs'))\n {-# ATP prove helper\u2082 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"0ec5eda09729e608ae00f5997b50db1941b4baf5","subject":"Iso: two more isos requiring funext","message":"Iso: two more isos requiring funext\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Function\/Related\/TypeIsomorphisms\/NP.agda","new_file":"lib\/Function\/Related\/TypeIsomorphisms\/NP.agda","new_contents":"-- {-# OPTIONS --without-K #-}\nmodule Function.Related.TypeIsomorphisms.NP where\n\nopen import Level.NP\nopen import Algebra\nimport Algebra.FunctionProperties as FP\nopen import Type hiding (\u2605)\nopen import Function using (_\u02e2_; const)\n\nopen import Data.Fin using (Fin; zero; suc; pred)\nopen import Data.Vec.NP using (Vec; []; _\u2237_; uncons; \u2237-uncons)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_) renaming (_^_ to _**_)\nopen import Data.Maybe.NP\nopen import Data.Product.NP renaming (map to map\u00d7)\n--open import Data.Product.N-ary\nopen import Data.Sum.NP renaming (map to map\u228e)\nopen import Data.One\nopen import Data.Zero\nopen import Data.Two using (\ud835\udfda; 0\u2082; 1\u2082; proj; \u2713; not; \u2261\u2192\u2713; \u2261\u2192\u2713not; \u2713\u2192\u2261; \u2713not\u2192\u2261 ; not-involutive; [0:_1:_])\n\nimport Function.NP as F\nopen F using (\u03a0)\nimport Function.Equality as FE\nopen FE using (_\u27e8$\u27e9_)\nopen import Function.Related as FR\nopen import Function.Related.TypeIsomorphisms public\nimport Function.Inverse.NP as Inv\nopen Inv using (_\u2194_; _\u2218_; sym; id; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nopen import Relation.Binary\nimport Relation.Binary.Indexed as I\nopen import Data.Indexed using (_\u228e\u00b0_)\nopen import Relation.Binary.Product.Pointwise public using (_\u00d7-cong_)\nopen import Relation.Binary.Sum public using (_\u228e-cong_)\nopen import Relation.Binary.Product.Pointwise\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_ ; _\u2262_; _\u2257_)\n\nmodule _ {A : Set} {p q : A \u2192 \ud835\udfda} where\n \u03a3AP : Set\n \u03a3AP = \u03a3 A (\u03bb x \u2192 p x \u2261 1\u2082)\n\n \u03a3AQ : Set\n \u03a3AQ = \u03a3 A (\u03bb x \u2192 q x \u2261 1\u2082)\n\n \u03a3AP\u00acQ : Set\n \u03a3AP\u00acQ = \u03a3 A (\u03bb x \u2192 p x \u2261 1\u2082 \u00d7 q x \u2261 0\u2082)\n\n \u03a3A\u00acPQ : Set\n \u03a3A\u00acPQ = \u03a3 A (\u03bb x \u2192 p x \u2261 0\u2082 \u00d7 q x \u2261 1\u2082)\n\n {-\n module M\n (f : \u03a3AP \u2192 \u03a3AQ)\n (f-1 : \u03a3AQ \u2192 \u03a3AP)\n (f-1f : \u2200 x \u2192 f-1 (f x) \u2261 x)\n (ff-1 : \u2200 x \u2192 f (f-1 x) \u2261 x)\n where\n\n f' : \u03a3AP\u00acQ \u2192 \u03a3A\u00acPQ\n f' (x , (p , nq)) = let y = f (x , p) in proj\u2081 y , {!proj\u2082 y!} , (proj\u2082 y)\n\n f-1' : \u03a3A\u00acPQ \u2192 \u03a3AP\u00acQ\n f-1' = {!!}\n\n f-1f' : \u2200 x \u2192 f-1' (f' x) \u2261 x\n f-1f' = {!!}\n\n ff-1' : \u2200 x \u2192 f' (f-1' x) \u2261 x\n ff-1' = {!!}\n -}\n\n module Work-In-Progress\n (f' : \u03a3AP\u00acQ \u2192 \u03a3A\u00acPQ)\n (f-1' : \u03a3A\u00acPQ \u2192 \u03a3AP\u00acQ)\n (f-1f' : \u2200 x \u2192 f-1' (f' x) \u2261 x)\n (ff-1' : \u2200 x \u2192 f' (f-1' x) \u2261 x)\n where\n\n f : (x : A) \u2192 p x \u2261 1\u2082 \u2192 q x \u2261 0\u2082 \u2192 \u03a3A\u00acPQ\n f x px qx = f' (x , (px , qx))\n\n f-1 : (x : A) \u2192 p x \u2261 0\u2082 \u2192 q x \u2261 1\u2082 \u2192 \u03a3AP\u00acQ\n f-1 x px qx = f-1' (x , (px , qx))\n\n f-1f : \u2200 x px nqx \u2192\n let y = proj\u2082 (f x px nqx) in proj\u2081 (f-1 (proj\u2081 (f x px nqx)) (proj\u2081 y) (proj\u2082 y)) \u2261 x\n f-1f x px nqx = \u2261.cong proj\u2081 (f-1f' (x , (px , nqx)))\n\n ff-1 : \u2200 x px nqx \u2192\n let y = proj\u2082 (f-1 x px nqx) in proj\u2081 (f (proj\u2081 (f-1 x px nqx)) (proj\u2081 y) (proj\u2082 y)) \u2261 x\n ff-1 x px nqx = \u2261.cong proj\u2081 (ff-1' (x , (px , nqx)))\n\n \u03c0' : (x : A) (px qx : \ud835\udfda) \u2192 p x \u2261 px \u2192 q x \u2261 qx \u2192 A\n \u03c0' x 1\u2082 1\u2082 px qx = x\n \u03c0' x 1\u2082 0\u2082 px qx = proj\u2081 (f x px qx)\n \u03c0' x 0\u2082 1\u2082 px qx = proj\u2081 (f-1 x px qx)\n \u03c0' x 0\u2082 0\u2082 px qx = x\n\n \u03c0 : A \u2192 A\n \u03c0 x = \u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n 0\u22621 : 0\u2082 \u2262 1\u2082\n 0\u22621 ()\n\n \u03c001 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px0 : p x \u2261 0\u2082) (qx1 : q x \u2261 1\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 0\u2082 1\u2082 px0 qx1\n \u03c001 x 1\u2082 _ ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym px0) ppx))\n \u03c001 x 0\u2082 1\u2082 ppx qqx px0 qx1 = \u2261.cong\u2082 (\u03bb z1 z2 \u2192 proj\u2081 (f-1 x z1 z2)) (\u2261.proof-irrelevance ppx px0) (\u2261.proof-irrelevance qqx qx1)\n \u03c001 x 0\u2082 0\u2082 ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qqx) qx1))\n\n \u03c010 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px1 : p x \u2261 1\u2082) (qx0 : q x \u2261 0\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 1\u2082 0\u2082 px1 qx0\n \u03c010 x 0\u2082 _ ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym ppx) px1))\n \u03c010 x 1\u2082 0\u2082 ppx qqx px1 qx0 = \u2261.cong\u2082 (\u03bb z1 z2 \u2192 proj\u2081 (f x z1 z2)) (\u2261.proof-irrelevance ppx px1) (\u2261.proof-irrelevance qqx qx0)\n \u03c010 x 1\u2082 1\u2082 ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qx0) qqx))\n\n \u03c0'bb : \u2200 {b} x (px : p x \u2261 b) (qx : q x \u2261 b) ppx qqx ([ppx] : p x \u2261 ppx) ([qqx] : q x \u2261 qqx) \u2192 \u03c0' x ppx qqx [ppx] [qqx] \u2261 x\n \u03c0'bb x px qx 1\u2082 1\u2082 [ppx] [qqx] = \u2261.refl\n \u03c0'bb x px qx 1\u2082 0\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [qqx]) (\u2261.trans qx (\u2261.trans (\u2261.sym px) [ppx]))))\n \u03c0'bb x px qx 0\u2082 1\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [ppx]) (\u2261.trans px (\u2261.trans (\u2261.sym qx) [qqx]))))\n \u03c0'bb x px qx 0\u2082 0\u2082 [ppx] [qqx] = \u2261.refl\n\n \u03c0\u03c0' : \u2200 x px qx [px] [qx] \u2192 let y = (\u03c0' x px qx [px] [qx]) in \u03c0' y (p y) (q y) \u2261.refl \u2261.refl \u2261 x\n \u03c0\u03c0' x 1\u2082 1\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n \u03c0\u03c0' x 1\u2082 0\u2082 px qx = let fx = f x px qx in let pfx = proj\u2081 (proj\u2082 fx) in let qfx = proj\u2082 (proj\u2082 fx) in \u2261.trans (\u03c001 (proj\u2081 fx) (p (proj\u2081 fx)) (q (proj\u2081 fx)) \u2261.refl \u2261.refl pfx qfx) (f-1f x px qx)\n \u03c0\u03c0' x 0\u2082 1\u2082 px qx = let fx = f-1 x px qx in let pfx = proj\u2081 (proj\u2082 fx) in let qfx = proj\u2082 (proj\u2082 fx) in \u2261.trans (\u03c010 (proj\u2081 fx) (p (proj\u2081 fx)) (q (proj\u2081 fx)) \u2261.refl \u2261.refl pfx qfx) (ff-1 x px qx)\n \u03c0\u03c0' x 0\u2082 0\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n\n \u03c0\u03c0 : \u2200 x \u2192 \u03c0 (\u03c0 x) \u2261 x\n \u03c0\u03c0 x = \u03c0\u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n prop' : \u2200 px qx x ([px] : p x \u2261 px) ([qx] : q x \u2261 qx) \u2192 q (\u03c0' x px qx [px] [qx]) \u2261 px\n prop' 1\u2082 1\u2082 x px qx = qx\n prop' 1\u2082 0\u2082 x px qx = proj\u2082 (proj\u2082 (f x px qx))\n prop' 0\u2082 1\u2082 x px qx = proj\u2082 (proj\u2082 (f-1 x px qx))\n prop' 0\u2082 0\u2082 x px qx = qx\n\n prop : \u2200 x \u2192 p x \u2261 q (\u03c0 x)\n prop x = \u2261.sym (prop' (p x) (q x) x \u2261.refl \u2261.refl)\n\nmodule _ {a b f} {A : Set a} {B : A \u2192 Set b}\n (F : (x : A) \u2192 B x \u2192 Set f) where\n\n -- Also called Axiom of dependent choice.\n dep-choice-iso : (\u03a0 A (\u03bb x \u2192 \u03a3 (B x) (F x)))\n \u2194 (\u03a3 (\u03a0 A B) \u03bb f \u2192 \u03a0 A (F \u02e2 f))\n dep-choice-iso = inverses (\u21d2) (uncurry <_,_>) (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n where\n \u21d2 = \u03bb f \u2192 (\u03bb x \u2192 proj\u2081 (f x)) , (\u03bb x \u2192 proj\u2082 (f x))\n\nMaybe-injective : \u2200 {A B : Set} \u2192 Maybe A \u2194 Maybe B \u2192 A \u2194 B\nMaybe-injective f = Iso.iso (g f) (g-empty f)\n module Maybe-injective where\n open Inverse using (injective; left-inverse-of; right-inverse-of)\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (tof-tot : \u2200 x \u2192 to f (just x) \u2262 nothing)\n (fof-tot : \u2200 x \u2192 from f (just x) \u2262 nothing) where\n\n CancelMaybe' : A \u2194 B\n CancelMaybe' = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n\n \u21d2 : _ \u2192 _\n \u21d2 x with to f (just x) | tof-tot x\n \u21d2 x | just y | _ = y\n \u21d2 x | nothing | p = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d0 : _ \u2192 _\n \u21d0 x with from f (just x) | fof-tot x\n \u21d0 x | just y | p = y\n \u21d0 x | nothing | p = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 x with to f (just x)\n | tof-tot x\n | from f (just (\u21d2 x))\n | fof-tot (\u21d2 x)\n | left-inverse-of f (just x)\n | right-inverse-of f (just (\u21d2 x))\n \u21d0\u21d2 x | just x\u2081 | p | just x\u2082 | q | b | c = just-injective (\u2261.trans (\u2261.sym (left-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong (from f) c) b))\n \u21d0\u21d2 x | just x\u2081 | p | nothing | q | _ | _ = \ud835\udfd8-elim (q \u2261.refl)\n \u21d0\u21d2 x | nothing | p | z | q | _ | _ = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 x with from f (just x)\n | fof-tot x\n | to f (just (\u21d0 x))\n | tof-tot (\u21d0 x)\n | right-inverse-of f (just x)\n | left-inverse-of f (just (\u21d0 x))\n \u21d2\u21d0 x | just x\u2081 | p | just x\u2082 | q | b | c = just-injective (\u2261.trans (\u2261.sym (right-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong (to f) c) b))\n \u21d2\u21d0 x | just x\u2081 | p | nothing | q | _ | _ = \ud835\udfd8-elim (q \u2261.refl)\n \u21d2\u21d0 x | nothing | p | z | q | _ | _ = \ud835\udfd8-elim (p \u2261.refl)\n\n module Iso {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (f-empty : to f nothing \u2261 nothing) where\n\n tof-tot : \u2200 x \u2192 to f (just x) \u2262 nothing\n tof-tot x eq with injective f (\u2261.trans eq (\u2261.sym f-empty))\n ... | ()\n\n f-empty' : from f nothing \u2261 nothing\n f-empty' = \u2261.trans (\u2261.sym (\u2261.cong (from f) f-empty)) (left-inverse-of f nothing)\n\n fof-tot : \u2200 x \u2192 from f (just x) \u2262 nothing\n fof-tot x eq with injective (sym f) (\u2261.trans eq (\u2261.sym f-empty'))\n ... | ()\n\n iso : A \u2194 B\n iso = CancelMaybe' f tof-tot fof-tot\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B) where\n\n g : Maybe A \u2194 Maybe B\n g = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n\n \u21d2 : Maybe A \u2192 Maybe B\n \u21d2 (just x) with to f (just x)\n ... | nothing = to f nothing\n ... | just y = just y\n \u21d2 nothing = nothing\n\n \u21d0 : Maybe B \u2192 Maybe A\n \u21d0 (just x) with from f (just x)\n ... | nothing = from f nothing\n ... | just y = just y\n \u21d0 nothing = nothing\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (just x) with to f (just x) | left-inverse-of f (just x)\n \u21d0\u21d2 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d0\u21d2 (just x) | nothing | p with to f nothing | left-inverse-of f nothing\n \u21d0\u21d2 (just x) | nothing | p | just _ | q rewrite q = p\n \u21d0\u21d2 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d0\u21d2 nothing = \u2261.refl\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (just x) with from f (just x) | right-inverse-of f (just x)\n \u21d2\u21d0 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d2\u21d0 (just x) | nothing | p with from f nothing | right-inverse-of f nothing\n \u21d2\u21d0 (just x) | nothing | p | just _ | q rewrite q = p\n \u21d2\u21d0 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d2\u21d0 nothing = \u2261.refl\n\n g-empty : to g nothing \u2261 nothing\n g-empty = \u2261.refl\n\nprivate\n Setoid\u2080 : \u2605 _\n Setoid\u2080 = Setoid \u2080 \u2080\n\n-- requires extensionality\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c}\n (extB : {f g : \u03a0 A B} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n (extC : {f g : \u03a0 A C} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n (f : \u2200 x \u2192 B x \u2194 C x) where\n private\n left-f = Inverse.left-inverse-of F.\u2218 f\n right-f = Inverse.right-inverse-of F.\u2218 f\n \u21d2 : \u03a0 A B \u2192 \u03a0 A C\n \u21d2 g x = to (f x) (g x)\n \u21d0 : \u03a0 A C \u2192 \u03a0 A B\n \u21d0 g x = from (f x) (g x)\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 g = extB \u03bb x \u2192 left-f x (g x)\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 g = extC \u03bb x \u2192 right-f x (g x)\n\n fiber-iso : \u03a0 A B \u2194 \u03a0 A C\n fiber-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\n\u03a3\u2261\u2194\ud835\udfd9 : \u2200 {a} {A : \u2605 a} x \u2192 \u03a3 A (_\u2261_ x) \u2194 \ud835\udfd9\n\u03a3\u2261\u2194\ud835\udfd9 x = inverses (F.const _) (\u03bb _ \u2192 _ , \u2261.refl)\n helper (\u03bb _ \u2192 \u2261.refl)\n where helper : (y : \u03a3 _ (_\u2261_ x)) \u2192 (x , \u2261.refl) \u2261 y\n helper (.x , \u2261.refl) = \u2261.refl\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : \u03a3 A B \u2192 \u2605 c} where\n curried : \u03a0 (\u03a3 A B) C \u2194 \u03a0 A \u03bb a \u2192 \u03a0 (B a) \u03bb b \u2192 C (a , b)\n curried = inverses curry uncurry (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u2192 \u2605 c} (f : A \u2194 B) where\n private\n left-f = Inverse.left-inverse-of f\n right-f = Inverse.right-inverse-of f\n coe : \u2200 x \u2192 C x \u2192 C (from f (to f x))\n coe x = \u2261.subst C (\u2261.sym (left-f x))\n \u21d2 : \u03a3 A C \u2192 \u03a3 B (C F.\u2218 from f)\n \u21d2 (x , p) = to f x , coe x p\n \u21d0 : \u03a3 B (C F.\u2218 from f) \u2192 \u03a3 A C\n \u21d0 = first (from f)\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , p) rewrite left-f x = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 p = mk\u03a3\u2261 (C F.\u2218 from f) (right-f (proj\u2081 p)) (helper p)\n where\n helper : \u2200 p \u2192 \u2261.subst (C F.\u2218 from f) (right-f (proj\u2081 p)) (coe (proj\u2081 (\u21d0 p)) (proj\u2082 (\u21d0 p))) \u2261 proj\u2082 p\n helper p with to f (from f (proj\u2081 p)) | right-f (proj\u2081 p) | left-f (from f (proj\u2081 p))\n helper _ | ._ | \u2261.refl | \u2261.refl = \u2261.refl\n first-iso : \u03a3 A C \u2194 \u03a3 B (C F.\u2218 from f)\n first-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : B \u2192 \u2605 c} (f : A \u2194 B) where\n sym-first-iso : \u03a3 A (C F.\u2218 to f) \u2194 \u03a3 B C\n sym-first-iso = sym (first-iso (sym f))\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} (f : \u2200 x \u2192 B x \u2194 C x) where\n private\n left-f = Inverse.left-inverse-of F.\u2218 f\n right-f = Inverse.right-inverse-of F.\u2218 f\n \u21d2 : \u03a3 A B \u2192 \u03a3 A C\n \u21d2 = second (to (f _))\n \u21d0 : \u03a3 A C \u2192 \u03a3 A B\n \u21d0 = second (from (f _))\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , y) rewrite left-f x y = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (x , y) rewrite right-f x y = \u2261.refl\n second-iso : \u03a3 A B \u2194 \u03a3 A C\n second-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b} {A : \u2605 a} {B : \u2605 b} (f\u2080 f\u2081 : A \u2194 B) where\n \ud835\udfda\u00d7-second-iso : (\ud835\udfda \u00d7 A) \u2194 (\ud835\udfda \u00d7 B)\n \ud835\udfda\u00d7-second-iso = second-iso {A = \ud835\udfda} {B = const A} {C = const B} (proj (f\u2080 , f\u2081))\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a3 (A \u228e B) C\n T = \u03a3 A (C F.\u2218 inj\u2081) \u228e \u03a3 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 (inj\u2081 x , y) = inj\u2081 (x , y)\n \u21d2 (inj\u2082 x , y) = inj\u2082 (x , y)\n \u21d0 : T \u2192 S\n \u21d0 (inj\u2081 (x , y)) = inj\u2081 x , y\n \u21d0 (inj\u2082 (x , y)) = inj\u2082 x , y\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (inj\u2081 _ , _) = \u2261.refl\n \u21d0\u21d2 (inj\u2082 _ , _) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n \u03a3\u228e-distrib : (\u03a3 (A \u228e B) C) \u2194 (\u03a3 A (C F.\u2218 inj\u2081) \u228e \u03a3 B (C F.\u2218 inj\u2082))\n \u03a3\u228e-distrib = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} where\n private\n S = \u03a3 A (B \u228e\u00b0 C)\n T = \u03a3 A B \u228e \u03a3 A C\n \u21d2 : S \u2192 T\n \u21d2 (x , inj\u2081 y) = inj\u2081 (x , y)\n \u21d2 (x , inj\u2082 y) = inj\u2082 (x , y)\n \u21d0 : T \u2192 S\n \u21d0 (inj\u2081 (x , y)) = x , inj\u2081 y\n \u21d0 (inj\u2082 (x , y)) = x , inj\u2082 y\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (_ , inj\u2081 _) = \u2261.refl\n \u21d0\u21d2 (_ , inj\u2082 _) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n \u03a3-\u228e-hom : \u03a3 A (B \u228e\u00b0 C) \u2194 (\u03a3 A B \u228e \u03a3 A C)\n \u03a3-\u228e-hom = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\n-- requires extensional equality\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c}\n (ext : {f g : \u03a0 (A \u228e B) C} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n private\n S = \u03a0 (A \u228e B) C\n T = \u03a0 A (C F.\u2218 inj\u2081) \u00d7 \u03a0 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 f = f F.\u2218 inj\u2081 , f F.\u2218 inj\u2082\n \u21d0 : T \u2192 S\n \u21d0 (f , g) = [ f , g ]\n \u21d0\u21d2 : \u2200 f x \u2192 \u21d0 (\u21d2 f) x \u2261 f x\n \u21d0\u21d2 f (inj\u2081 x) = \u2261.refl\n \u21d0\u21d2 f (inj\u2082 y) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (f , g) = \u2261.refl\n\n \u03a0\u00d7-distrib : \u03a0 (A \u228e B) C \u2194 (\u03a0 A (C F.\u2218 inj\u2081) \u00d7 \u03a0 B (C F.\u2218 inj\u2082))\n \u03a0\u00d7-distrib = inverses (\u21d2) (\u21d0) (\u03bb f \u2192 ext (\u21d0\u21d2 f)) \u21d2\u21d0\n\n\u228e-ICommutativeMonoid : CommutativeMonoid _ _\n\u228e-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u228e-setoid_;\n \u03b5 = \u2261.setoid \ud835\udfd8;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u228e-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = \u2080}{f\u2082 = \u2080})\n\n left-identity : LeftIdentity (\u2261.setoid \ud835\udfd8) _\u228e-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = [ (\u03bb ()) , F.id ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = inj\u2082\n ; cong = \u2082\u223c\u2082\n }\n ; inverse-of = record\n { left-inverse-of = [ (\u03bb ()) , (\u03bb x \u2192 \u2082\u223c\u2082 refl) ]\n ; right-inverse-of = \u03bb x \u2192 refl\n }\n } where open Setoid A\n cong-to : Setoid._\u2248_ (\u2261.setoid \ud835\udfd8 \u228e-setoid A) =[ _ ]\u21d2 _\u2248_\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y) rewrite x\u223c\u2081y = refl\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = x\u223c\u2082y\n\n assoc : Associative _\u228e-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = [ [ inj\u2081 , inj\u2082 F.\u2218 inj\u2081 ] , inj\u2082 F.\u2218 inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = [ inj\u2081 F.\u2218 inj\u2081 , [ inj\u2081 F.\u2218 inj\u2082 , inj\u2082 ] ]\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = [ [ (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2081\u223c\u2081 (refl A))) , (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2082\u223c\u2082 (refl B)) ) ] , (\u03bb _ \u2192 \u2082\u223c\u2082 (refl C)) ]\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (refl A)) , [ (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2081\u223c\u2081 (refl B))) , (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2082\u223c\u2082 (refl C))) ] ]\n }\n } where\n open Setoid\n cong-to : _\u2248_ ((A \u228e-setoid B) \u228e-setoid C) =[ _ ]\u21d2 _\u2248_ (A \u228e-setoid (B \u228e-setoid C))\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2082 ()))\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 x\u223c\u2081y\n cong-to (\u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2082y)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = \u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)\n\n cong-from : _\u2248_ (A \u228e-setoid (B \u228e-setoid C)) =[ _ ]\u21d2 _\u2248_ ((A \u228e-setoid B) \u228e-setoid C)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 x\u223c\u2081y) = \u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2082 ()))\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 x\u223c\u2082y\n\n comm : Commutative _\u228e-setoid_\n comm A B = record\n { to = swap'\n ; from = swap'\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u228e-setoid B FE.\u27f6 B \u228e-setoid A\n swap' {A} {B} = record\n { _\u27e8$\u27e9_ = [ inj\u2082 , inj\u2081 ]\n ; cong = cong\n } where\n cong : Setoid._\u2248_ (A \u228e-setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (B \u228e-setoid A)\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2082\u223c\u2082 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2081\u223c\u2081 x\u223c\u2082y\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u228e-setoid B) (swap' FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (Setoid.refl A)) , (\u03bb _ \u2192 \u2082\u223c\u2082 (Setoid.refl B)) ]\n\n\u00d7-ICommutativeMonoid : CommutativeMonoid _ _\n\u00d7-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u00d7-setoid_;\n \u03b5 = \u2261.setoid \ud835\udfd9;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u00d7-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = \u2080}{f\u2082 = \u2080})\n\n left-identity : LeftIdentity (\u2261.setoid \ud835\udfd9) _\u00d7-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2082\n ; cong = proj\u2082\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 _ , x\n ; cong = \u03bb x \u2192 \u2261.refl , x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u2261.refl , (Setoid.refl A)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl A\n }\n }\n\n assoc : Associative _\u00d7-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n ; cong = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n }\n ; from = record\n { _\u27e8$\u27e9_ = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n ; cong = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb _ \u2192 Setoid.refl ((A \u00d7-setoid B) \u00d7-setoid C)\n ; right-inverse-of = \u03bb _ \u2192 Setoid.refl (A \u00d7-setoid (B \u00d7-setoid C))\n }\n }\n\n comm : Commutative _\u00d7-setoid_\n comm A B = record\n { to = swap' {A} {B}\n ; from = swap' {B} {A}\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u00d7-setoid B FE.\u27f6 B \u00d7-setoid A\n swap' {A}{B} = record { _\u27e8$\u27e9_ = swap; cong = swap }\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u00d7-setoid B) (swap' {B} {A} FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = \u03bb x \u2192 Setoid.refl (A \u00d7-setoid B)\n\n\u00d7\u228e-ICommutativeSemiring : CommutativeSemiring _ _\n\u00d7\u228e-ICommutativeSemiring = record\n { _\u2248_ = Inv.Inverse\n ; _+_ = _\u228e-setoid_\n ; _*_ = _\u00d7-setoid_\n ; 0# = \u2261.setoid \ud835\udfd8\n ; 1# = \u2261.setoid \ud835\udfd9\n ; isCommutativeSemiring = record\n { +-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u228e-ICommutativeMonoid\n ; *-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u00d7-ICommutativeMonoid\n ; distrib\u02b3 = distrib\u02b3\n ; zero\u02e1 = zero\u02e1\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = \u2080}{f\u2082 = \u2080})\n\n distrib\u02b3 : _\u00d7-setoid_ DistributesOver\u02b3 _\u228e-setoid_\n distrib\u02b3 A B C = record\n { to = record\n { _\u27e8$\u27e9_ = uncurry [ curry inj\u2081 , curry inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = frm\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = uncurry [ (\u03bb x y \u2192 (\u2081\u223c\u2081 (refl B)) , (refl A)) , (\u03bb x y \u2192 (\u2082\u223c\u2082 (refl C)) , (refl A)) ]\n ; right-inverse-of = [ uncurry (\u03bb x y \u2192 \u2081\u223c\u2081 ((refl B {x}) , (refl A {y})))\n , uncurry (\u03bb x y \u2192 \u2082\u223c\u2082 ((refl C {x}) , (refl A {y}))) ]\n }\n } where\n open Setoid\n A' = Carrier A\n B' = Carrier B\n C' = Carrier C\n\n cong-to : _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A) =[ _ ]\u21d2 _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A))\n cong-to (\u2081\u223c\u2082 () , _)\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y , A-rel) = \u2081\u223c\u2081 (x\u223c\u2081y , A-rel)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y , A-rel) = \u2082\u223c\u2082 (x\u223c\u2082y , A-rel)\n\n frm : B' \u00d7 A' \u228e C' \u00d7 A' \u2192 (B' \u228e C') \u00d7 A'\n frm = [ map\u00d7 inj\u2081 F.id , map\u00d7 inj\u2082 F.id ]\n\n cong-from : _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A)) =[ _ ]\u21d2 _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 (B-rel , A-rel)) = \u2081\u223c\u2081 B-rel , A-rel\n cong-from (\u2082\u223c\u2082 (C-rel , A-rel)) = \u2082\u223c\u2082 C-rel , A-rel\n\n zero\u02e1 : LeftZero (\u2261.setoid \ud835\udfd8) _\u00d7-setoid_\n zero\u02e1 A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2081\n ; cong = proj\u2081\n }\n ; from = record\n { _\u27e8$\u27e9_ = \ud835\udfd8-elim\n ; cong = \u03bb { {()} x }\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \ud835\udfd8-elim (proj\u2081 x)\n ; right-inverse-of = \u03bb x \u2192 \ud835\udfd8-elim x\n }\n }\n\nmodule \u00d7-CMon {a} = CommutativeMonoid (\u00d7-CommutativeMonoid FR.bijection a)\nmodule \u228e-CMon {a} = CommutativeMonoid (\u228e-CommutativeMonoid FR.bijection a)\nmodule \u00d7\u228e\u00b0 {a} = CommutativeSemiring (\u00d7\u228e-CommutativeSemiring FR.bijection a)\n\nmodule \u00d7-ICMon = CommutativeMonoid \u00d7-ICommutativeMonoid\nmodule \u228e-ICMon = CommutativeMonoid \u228e-ICommutativeMonoid\nmodule \u00d7\u228e\u00b0I = CommutativeSemiring \u00d7\u228e-ICommutativeSemiring\n\nlift-\u228e : \u2200 {A B : Set} \u2192 Inv.Inverse ((\u2261.setoid A) \u228e-setoid (\u2261.setoid B)) (\u2261.setoid (A \u228e B))\nlift-\u228e {A}{B} = record\n { to = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = cong\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = \u03bb x \u2192 Setoid.reflexive (\u2261.setoid A \u228e-setoid \u2261.setoid B) x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid A \u228e-setoid \u2261.setoid B)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid (A \u228e B))\n }\n } where\n cong : Setoid._\u2248_ (\u2261.setoid A \u228e-setoid \u2261.setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (\u2261.setoid (A \u228e B))\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2261.cong inj\u2081 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2261.cong inj\u2082 x\u223c\u2082y\n\nswap-iso : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 (A \u00d7 B) \u2194 (B \u00d7 A)\nswap-iso = inverses swap swap (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u00d7 B \u2192 \u2605 c} where\n \u03a3\u00d7-swap : \u03a3 (A \u00d7 B) C \u2194 \u03a3 (B \u00d7 A) (C F.\u2218 swap)\n \u03a3\u00d7-swap = first-iso swap-iso\n\nMaybe\u2194Lift\ud835\udfd9\u228e : \u2200 {\u2113 a} {A : \u2605 a} \u2192 Maybe A \u2194 (Lift {\u2113 = \u2113} \ud835\udfd9 \u228e A)\nMaybe\u2194Lift\ud835\udfd9\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe\u2194\ud835\udfd9\u228e : \u2200 {a} {A : \u2605 a} \u2192 Maybe A \u2194 (\ud835\udfd9 \u228e A)\nMaybe\u2194\ud835\udfd9\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 A \u2194 B \u2192 Maybe A \u2194 Maybe B\nMaybe-cong A\u2194B = sym Maybe\u2194\ud835\udfd9\u228e \u2218 id \u228e-cong A\u2194B \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe : \u2200 {a} {A : \u2605 a} n \u2192 Maybe (Maybe^ n A) \u2194 Maybe^ n (Maybe A)\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe zero = id\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe (suc n) = Maybe-cong (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n)\n\nMaybe^-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} n \u2192 A \u2194 B \u2192 Maybe^ n A \u2194 Maybe^ n B\nMaybe^-cong zero = F.id\nMaybe^-cong (suc n) = Maybe-cong F.\u2218 Maybe^-cong n\n\nVec0\u2194Lift\ud835\udfd9 : \u2200 {a \u2113} {A : \u2605 a} \u2192 Vec A 0 \u2194 Lift {_} {\u2113} \ud835\udfd9\nVec0\u2194Lift\ud835\udfd9 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec0\u2194\ud835\udfd9 : \u2200 {a} {A : \u2605 a} \u2192 Vec A 0 \u2194 \ud835\udfd9\nVec0\u2194\ud835\udfd9 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec\u2218suc\u2194A\u00d7Vec : \u2200 {a} {A : \u2605 a} {n} \u2192 Vec A (suc n) \u2194 (A \u00d7 Vec A n)\nVec\u2218suc\u2194A\u00d7Vec\n = inverses uncons (uncurry _\u2237_) \u2237-uncons (\u03bb _ \u2192 \u2261.refl)\n\ninfix 8 _^_\n\n_^_ : \u2200 {a} \u2192 \u2605 a \u2192 \u2115 \u2192 \u2605 a\nA ^ 0 = Lift \ud835\udfd9\nA ^ suc n = A \u00d7 A ^ n\n\n^\u2194Vec : \u2200 {a} {A : \u2605 a} n \u2192 (A ^ n) \u2194 Vec A n\n^\u2194Vec zero = sym Vec0\u2194Lift\ud835\udfd9\n^\u2194Vec (suc n) = sym Vec\u2218suc\u2194A\u00d7Vec \u2218 (id \u00d7-cong (^\u2194Vec n))\n\nFin0\u2194\ud835\udfd8 : Fin 0 \u2194 \ud835\udfd8\nFin0\u2194\ud835\udfd8 = inverses (\u03bb()) (\u03bb()) (\u03bb()) (\u03bb())\n\nFin1\u2194\ud835\udfd9 : Fin 1 \u2194 \ud835\udfd9\nFin1\u2194\ud835\udfd9 = inverses _ (\u03bb _ \u2192 zero) \u21d0\u21d2 (\u03bb _ \u2192 \u2261.refl)\n where \u21d0\u21d2 : (_ : Fin 1) \u2192 _\n \u21d0\u21d2 zero = \u2261.refl\n \u21d0\u21d2 (suc ())\n\nFin\u2218suc\u2194Maybe\u2218Fin : \u2200 {n} \u2192 Fin (suc n) \u2194 Maybe (Fin n)\nFin\u2218suc\u2194Maybe\u2218Fin {n}\n = inverses to' (maybe suc zero)\n (\u03bb { zero \u2192 \u2261.refl ; (suc n) \u2192 \u2261.refl })\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n where to' : Fin (suc n) \u2192 Maybe (Fin n)\n to' zero = nothing\n to' (suc n) = just n\n\nFin-injective : \u2200 {m n} \u2192 Fin m \u2194 Fin n \u2192 m \u2261 n\nFin-injective = go _ _ where\n go : \u2200 m n \u2192 Fin m \u2194 Fin n \u2192 m \u2261 n\n go zero zero iso = \u2261.refl\n go zero (suc n) iso with from iso zero\n ... | ()\n go (suc m) zero iso with to iso zero\n ... | ()\n go (suc m) (suc n) iso = \u2261.cong suc (go m n (Maybe-injective (Fin\u2218suc\u2194Maybe\u2218Fin \u2218 iso \u2218 sym Fin\u2218suc\u2194Maybe\u2218Fin)))\n\nLift\u2194id : \u2200 {a} {A : \u2605 a} \u2192 Lift {a} {a} A \u2194 A\nLift\u2194id = inverses lower lift (\u03bb { (lift x) \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\n\ud835\udfd9\u00d7A\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd9 \u00d7 A) \u2194 A\n\ud835\udfd9\u00d7A\u2194A = proj\u2081 \u00d7-CMon.identity _ \u2218 sym Lift\u2194id \u00d7-cong id\n\nA\u00d7\ud835\udfd9\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u00d7 \ud835\udfd9) \u2194 A\nA\u00d7\ud835\udfd9\u2194A = proj\u2082 \u00d7-CMon.identity _ \u2218 id \u00d7-cong sym Lift\u2194id\n\n\u03a0\ud835\udfd9F\u2194F : \u2200 {\u2113} {F : \ud835\udfd9 \u2192 \u2605_ \u2113} \u2192 \u03a0 \ud835\udfd9 F \u2194 F _\n\u03a0\ud835\udfd9F\u2194F = inverses (\u03bb x \u2192 x _) const (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\n\ud835\udfd9\u2192A\u2194A : \u2200 {\u2113} {A : \u2605_ \u2113} \u2192 (\ud835\udfd9 \u2192 A) \u2194 A\n\ud835\udfd9\u2192A\u2194A = \u03a0\ud835\udfd9F\u2194F\n\nnot-\ud835\udfda\u2194\ud835\udfda : \ud835\udfda \u2194 \ud835\udfda\nnot-\ud835\udfda\u2194\ud835\udfda = inverses not not not-involutive not-involutive\n\n\u2261-iso : \u2200 {\u2113 \u2113'}{A : \u2605_ \u2113}{B : \u2605_ \u2113'}{x y : A} \u2192 (\u03c0 : A \u2194 B) \u2192 (x \u2261 y) \u2194 (to \u03c0 x \u2261 to \u03c0 y)\n\u2261-iso {x = x}{y} \u03c0 = inverses (\u2261.cong (to \u03c0))\n (\u03bb p \u2192 \u2261.trans (\u2261.sym (Inverse.left-inverse-of \u03c0 x))\n (\u2261.trans (\u2261.cong (from \u03c0) p)\n (Inverse.left-inverse-of \u03c0 y)))\n (\u03bb x \u2192 \u2261.proof-irrelevance _ x) (\u03bb x \u2192 \u2261.proof-irrelevance _ x)\n\n-- requires extensionality\nmodule _ {a} {A : \u2605_ a} (ext\ud835\udfd8 : (f g : \ud835\udfd8 \u2192 A) \u2192 f \u2261 g) where\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 : (\ud835\udfd8 \u2192 A) \u2194 \ud835\udfd9\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 = inverses _ (\u03bb _ ()) (\u03bb h \u2192 ext\ud835\udfd8 _ h) (\u03bb _ \u2192 \u2261.refl)\n\n-- requires extensionality\nmodule _ {\u2113} {F : \ud835\udfd8 \u2192 \u2605_ \u2113} (ext\ud835\udfd8 : (f g : \u03a0 \ud835\udfd8 F) \u2192 f \u2261 g) where\n \u03a0\ud835\udfd8\u2194\ud835\udfd9 : \u03a0 \ud835\udfd8 F \u2194 \ud835\udfd9\n \u03a0\ud835\udfd8\u2194\ud835\udfd9 = inverses _ (\u03bb _ ()) (\u03bb h \u2192 ext\ud835\udfd8 _ h) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {F : \ud835\udfda \u2192 \u2605_ \u2113} (ext\ud835\udfda : {f g : \u03a0 \ud835\udfda F} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 : \u03a0 \ud835\udfda F \u2194 (F 0\u2082 \u00d7 F 1\u2082)\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 = inverses (\u03bb f \u2192 f 0\u2082 , f 1\u2082) proj\n (\u03bb f \u2192 ext\ud835\udfda (\u03bb { 0\u2082 \u2192 \u2261.refl ; 1\u2082 \u2192 \u2261.refl }))\n (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {A : \u2605_ \u2113} (ext\ud835\udfda : {f g : \ud835\udfda \u2192 A} \u2192 f \u2257 g \u2192 f \u2261 g) where\n \ud835\udfda\u2192A\u2194A\u00d7A : (\ud835\udfda \u2192 A) \u2194 (A \u00d7 A)\n \ud835\udfda\u2192A\u2194A\u00d7A = \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 ext\ud835\udfda\n\n\ud835\udfd8\u228eA\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd8 \u228e A) \u2194 A\n\ud835\udfd8\u228eA\u2194A = proj\u2081 \u228e-CMon.identity _ \u2218 sym Lift\u2194id \u228e-cong id\n\nA\u228e\ud835\udfd8\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u228e \ud835\udfd8) \u2194 A\nA\u228e\ud835\udfd8\u2194A = proj\u2082 \u228e-CMon.identity _ \u2218 id \u228e-cong sym Lift\u2194id\n\n\ud835\udfd8\u00d7A\u2194\ud835\udfd8 : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd8 \u00d7 A) \u2194 \ud835\udfd8\n\ud835\udfd8\u00d7A\u2194\ud835\udfd8 = Lift\u2194id \u2218 proj\u2081 \u00d7\u228e\u00b0.zero _ \u2218 sym (Lift\u2194id \u00d7-cong id)\n\nMaybe\ud835\udfd8\u2194\ud835\udfd9 : Maybe \ud835\udfd8 \u2194 \ud835\udfd9\nMaybe\ud835\udfd8\u2194\ud835\udfd9 = A\u228e\ud835\udfd8\u2194A \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe^\ud835\udfd8\u2194Fin : \u2200 n \u2192 Maybe^ n \ud835\udfd8 \u2194 Fin n\nMaybe^\ud835\udfd8\u2194Fin zero = sym Fin0\u2194\ud835\udfd8\nMaybe^\ud835\udfd8\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\ud835\udfd8\u2194Fin n)\n\nMaybe^\ud835\udfd9\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \ud835\udfd9 \u2194 Fin (suc n)\nMaybe^\ud835\udfd9\u2194Fin1+ n = Maybe^\ud835\udfd8\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\ud835\udfd8\u2194\ud835\udfd9)\n\nMaybe-\u228e : \u2200 {a} {A B : \u2605 a} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e {a} = sym Maybe\u2194Lift\ud835\udfd9\u228e \u2218 \u228e-CMon.assoc (Lift {_} {a} \ud835\udfd9) _ _ \u2218 (Maybe\u2194Lift\ud835\udfd9\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \ud835\udfd8 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \ud835\udfd8\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n\n\u03a3\ud835\udfd8\u2194\ud835\udfd8 : \u2200 {a} (F : \ud835\udfd8 \u2192 \u2605_ a) \u2192 \u03a3 \ud835\udfd8 F \u2194 \ud835\udfd8\n\u03a3\ud835\udfd8\u2194\ud835\udfd8 F = inverses proj\u2081 (\u03bb ()) (\u03bb { ((), _) }) (\u03bb ())\n\n\u03a3\ud835\udfda\u2194\u228e : \u2200 {a} (F : \ud835\udfda \u2192 \u2605_ a) \u2192 \u03a3 \ud835\udfda F \u2194 (F 0\u2082 \u228e F 1\u2082)\n\u03a3\ud835\udfda\u2194\u228e F = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d2 : (x : \u03a3 _ _) \u2192 _\n \u21d2 (0\u2082 , p) = inj\u2081 p\n \u21d2 (1\u2082 , p) = inj\u2082 p\n \u21d0 : (x : _ \u228e _) \u2192 _\n \u21d0 (inj\u2081 x) = 0\u2082 , x\n \u21d0 (inj\u2082 y) = 1\u2082 , y\n\n \u21d0\u21d2 : (_ : \u03a3 _ _) \u2192 _\n \u21d0\u21d2 (0\u2082 , p) = \u2261.refl\n \u21d0\u21d2 (1\u2082 , p) = \u2261.refl\n \u21d2\u21d0 : (_ : _ \u228e _) \u2192 _\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n\u228e\u21ff\u03a32 : \u2200 {\u2113} {A B : \u2605 \u2113} \u2192 (A \u228e B) \u2194 \u03a3 \ud835\udfda [0: A 1: B ]\n\u228e\u21ff\u03a32 {A = A} {B} = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d2 : A \u228e B \u2192 _\n \u21d2 (inj\u2081 x) = 0\u2082 , x\n \u21d2 (inj\u2082 x) = 1\u2082 , x\n \u21d0 : \u03a3 _ _ \u2192 A \u228e B\n \u21d0 (0\u2082 , x) = inj\u2081 x\n \u21d0 (1\u2082 , x) = inj\u2082 x\n \u21d0\u21d2 : (_ : _ \u228e _) \u2192 _\n \u21d0\u21d2 (inj\u2081 x) = \u2261.refl\n \u21d0\u21d2 (inj\u2082 x) = \u2261.refl\n \u21d2\u21d0 : (_ : \u03a3 _ _) \u2192 _\n \u21d2\u21d0 (0\u2082 , x) = \u2261.refl\n \u21d2\u21d0 (1\u2082 , x) = \u2261.refl\n\n\ud835\udfda\u2194\ud835\udfd9\u228e\ud835\udfd9 : \ud835\udfda \u2194 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2194\ud835\udfd9\u228e\ud835\udfd9 = inverses (proj (inj\u2081 _ , inj\u2082 _)) [ F.const 0\u2082 , F.const 1\u2082 ] \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d0\u21d2 : (_ : \ud835\udfda) \u2192 _\n \u21d0\u21d2 0\u2082 = \u2261.refl\n \u21d0\u21d2 1\u2082 = \u2261.refl\n \u21d2\u21d0 : (_ : \ud835\udfd9 \u228e \ud835\udfd9) \u2192 _\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\nFin-\u228e-+ : \u2200 m n \u2192 (Fin m \u228e Fin n) \u2194 Fin (m + n)\nFin-\u228e-+ m n = Maybe^\ud835\udfd8\u2194Fin (m + n)\n \u2218 Maybe^-\u228e-+ m n\n \u2218 sym (Maybe^\ud835\udfd8\u2194Fin m \u228e-cong Maybe^\ud835\udfd8\u2194Fin n)\n\nFin\u2218suc\u2194\ud835\udfd9\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2194 (\ud835\udfd9 \u228e Fin n)\nFin\u2218suc\u2194\ud835\udfd9\u228eFin = Maybe\u2194\ud835\udfd9\u228e \u2218 Fin\u2218suc\u2194Maybe\u2218Fin\n\nFin\u2194\ud835\udfd9\u228e^\ud835\udfd8 : \u2200 n \u2192 Fin n \u2194 \ud835\udfd9\u228e^ n\nFin\u2194\ud835\udfd9\u228e^\ud835\udfd8 zero = Fin0\u2194\ud835\udfd8\nFin\u2194\ud835\udfd9\u228e^\ud835\udfd8 (suc n) = Inv.id \u228e-cong Fin\u2194\ud835\udfd9\u228e^\ud835\udfd8 n Inv.\u2218 Fin\u2218suc\u2194\ud835\udfd9\u228eFin\n\nFin-\u00d7-* : \u2200 m n \u2192 (Fin m \u00d7 Fin n) \u2194 Fin (m * n)\nFin-\u00d7-* zero n = (Fin 0 \u00d7 Fin n) \u2194\u27e8 Fin0\u2194\ud835\udfd8 \u00d7-cong id \u27e9\n (\ud835\udfd8 \u00d7 Fin n) \u2194\u27e8 \ud835\udfd8\u00d7A\u2194\ud835\udfd8 \u27e9\n \ud835\udfd8 \u2194\u27e8 sym Fin0\u2194\ud835\udfd8 \u27e9\n Fin 0 \u220e\n where open EquationalReasoning hiding (sym)\nFin-\u00d7-* (suc m) n = (Fin (suc m) \u00d7 Fin n) \u2194\u27e8 Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u00d7-cong id \u27e9\n ((\ud835\udfd9 \u228e Fin m) \u00d7 Fin n) \u2194\u27e8 \u00d7\u228e\u00b0.distrib\u02b3 (Fin n) \ud835\udfd9 (Fin m) \u27e9\n ((\ud835\udfd9 \u00d7 Fin n) \u228e (Fin m \u00d7 Fin n)) \u2194\u27e8 \ud835\udfd9\u00d7A\u2194A \u228e-cong Fin-\u00d7-* m n \u27e9\n (Fin n \u228e Fin (m * n)) \u2194\u27e8 Fin-\u228e-+ n (m * n) \u27e9\n Fin (suc m * n) \u220e\n where open EquationalReasoning hiding (sym)\n\nFin\u228e-injective : \u2200 {A B : Set} n \u2192 (Fin n \u228e A) \u2194 (Fin n \u228e B) \u2192 A \u2194 B\nFin\u228e-injective zero f = \ud835\udfd8\u228eA\u2194A \u2218 Fin0\u2194\ud835\udfd8 \u228e-cong id \u2218 f \u2218 sym Fin0\u2194\ud835\udfd8 \u228e-cong id \u2218 sym \ud835\udfd8\u228eA\u2194A\nFin\u228e-injective (suc n) f =\n Fin\u228e-injective n\n (Maybe-injective\n (sym Maybe\u2194\ud835\udfd9\u228e \u2218\n \u228e-CMon.assoc _ _ _ \u2218\n Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u228e-cong id \u2218\n f \u2218\n sym Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u228e-cong id \u2218\n sym (\u228e-CMon.assoc _ _ _) \u2218\n Maybe\u2194\ud835\udfd9\u228e))\n\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"-- {-# OPTIONS --without-K #-}\nmodule Function.Related.TypeIsomorphisms.NP where\n\nopen import Level.NP\nopen import Algebra\nimport Algebra.FunctionProperties as FP\nopen import Type hiding (\u2605)\nopen import Function using (_\u02e2_; const)\n\nopen import Data.Fin using (Fin; zero; suc; pred)\nopen import Data.Vec.NP using (Vec; []; _\u2237_; uncons; \u2237-uncons)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_) renaming (_^_ to _**_)\nopen import Data.Maybe.NP\nopen import Data.Product.NP renaming (map to map\u00d7)\n--open import Data.Product.N-ary\nopen import Data.Sum.NP renaming (map to map\u228e)\nopen import Data.One\nopen import Data.Zero\nopen import Data.Two using (\ud835\udfda; 0\u2082; 1\u2082; proj; \u2713; not; \u2261\u2192\u2713; \u2261\u2192\u2713not; \u2713\u2192\u2261; \u2713not\u2192\u2261 ; not-involutive; [0:_1:_])\n\nimport Function.NP as F\nopen F using (\u03a0)\nimport Function.Equality as FE\nopen FE using (_\u27e8$\u27e9_)\nopen import Function.Related as FR\nopen import Function.Related.TypeIsomorphisms public\nimport Function.Inverse.NP as Inv\nopen Inv using (_\u2194_; _\u2218_; sym; id; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nopen import Relation.Binary\nimport Relation.Binary.Indexed as I\nopen import Data.Indexed using (_\u228e\u00b0_)\nopen import Relation.Binary.Product.Pointwise public using (_\u00d7-cong_)\nopen import Relation.Binary.Sum public using (_\u228e-cong_)\nopen import Relation.Binary.Product.Pointwise\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_ ; _\u2262_; _\u2257_)\n\nmodule _ {A : Set} {p q : A \u2192 \ud835\udfda} where\n \u03a3AP : Set\n \u03a3AP = \u03a3 A (\u03bb x \u2192 p x \u2261 1\u2082)\n\n \u03a3AQ : Set\n \u03a3AQ = \u03a3 A (\u03bb x \u2192 q x \u2261 1\u2082)\n\n \u03a3AP\u00acQ : Set\n \u03a3AP\u00acQ = \u03a3 A (\u03bb x \u2192 p x \u2261 1\u2082 \u00d7 q x \u2261 0\u2082)\n\n \u03a3A\u00acPQ : Set\n \u03a3A\u00acPQ = \u03a3 A (\u03bb x \u2192 p x \u2261 0\u2082 \u00d7 q x \u2261 1\u2082)\n\n {-\n module M\n (f : \u03a3AP \u2192 \u03a3AQ)\n (f-1 : \u03a3AQ \u2192 \u03a3AP)\n (f-1f : \u2200 x \u2192 f-1 (f x) \u2261 x)\n (ff-1 : \u2200 x \u2192 f (f-1 x) \u2261 x)\n where\n\n f' : \u03a3AP\u00acQ \u2192 \u03a3A\u00acPQ\n f' (x , (p , nq)) = let y = f (x , p) in proj\u2081 y , {!proj\u2082 y!} , (proj\u2082 y)\n\n f-1' : \u03a3A\u00acPQ \u2192 \u03a3AP\u00acQ\n f-1' = {!!}\n\n f-1f' : \u2200 x \u2192 f-1' (f' x) \u2261 x\n f-1f' = {!!}\n\n ff-1' : \u2200 x \u2192 f' (f-1' x) \u2261 x\n ff-1' = {!!}\n -}\n\n module Work-In-Progress\n (f' : \u03a3AP\u00acQ \u2192 \u03a3A\u00acPQ)\n (f-1' : \u03a3A\u00acPQ \u2192 \u03a3AP\u00acQ)\n (f-1f' : \u2200 x \u2192 f-1' (f' x) \u2261 x)\n (ff-1' : \u2200 x \u2192 f' (f-1' x) \u2261 x)\n where\n\n f : (x : A) \u2192 p x \u2261 1\u2082 \u2192 q x \u2261 0\u2082 \u2192 \u03a3A\u00acPQ\n f x px qx = f' (x , (px , qx))\n\n f-1 : (x : A) \u2192 p x \u2261 0\u2082 \u2192 q x \u2261 1\u2082 \u2192 \u03a3AP\u00acQ\n f-1 x px qx = f-1' (x , (px , qx))\n\n f-1f : \u2200 x px nqx \u2192\n let y = proj\u2082 (f x px nqx) in proj\u2081 (f-1 (proj\u2081 (f x px nqx)) (proj\u2081 y) (proj\u2082 y)) \u2261 x\n f-1f x px nqx = \u2261.cong proj\u2081 (f-1f' (x , (px , nqx)))\n\n ff-1 : \u2200 x px nqx \u2192\n let y = proj\u2082 (f-1 x px nqx) in proj\u2081 (f (proj\u2081 (f-1 x px nqx)) (proj\u2081 y) (proj\u2082 y)) \u2261 x\n ff-1 x px nqx = \u2261.cong proj\u2081 (ff-1' (x , (px , nqx)))\n\n \u03c0' : (x : A) (px qx : \ud835\udfda) \u2192 p x \u2261 px \u2192 q x \u2261 qx \u2192 A\n \u03c0' x 1\u2082 1\u2082 px qx = x\n \u03c0' x 1\u2082 0\u2082 px qx = proj\u2081 (f x px qx)\n \u03c0' x 0\u2082 1\u2082 px qx = proj\u2081 (f-1 x px qx)\n \u03c0' x 0\u2082 0\u2082 px qx = x\n\n \u03c0 : A \u2192 A\n \u03c0 x = \u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n 0\u22621 : 0\u2082 \u2262 1\u2082\n 0\u22621 ()\n\n \u03c001 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px0 : p x \u2261 0\u2082) (qx1 : q x \u2261 1\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 0\u2082 1\u2082 px0 qx1\n \u03c001 x 1\u2082 _ ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym px0) ppx))\n \u03c001 x 0\u2082 1\u2082 ppx qqx px0 qx1 = \u2261.cong\u2082 (\u03bb z1 z2 \u2192 proj\u2081 (f-1 x z1 z2)) (\u2261.proof-irrelevance ppx px0) (\u2261.proof-irrelevance qqx qx1)\n \u03c001 x 0\u2082 0\u2082 ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qqx) qx1))\n\n \u03c010 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px1 : p x \u2261 1\u2082) (qx0 : q x \u2261 0\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 1\u2082 0\u2082 px1 qx0\n \u03c010 x 0\u2082 _ ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym ppx) px1))\n \u03c010 x 1\u2082 0\u2082 ppx qqx px1 qx0 = \u2261.cong\u2082 (\u03bb z1 z2 \u2192 proj\u2081 (f x z1 z2)) (\u2261.proof-irrelevance ppx px1) (\u2261.proof-irrelevance qqx qx0)\n \u03c010 x 1\u2082 1\u2082 ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qx0) qqx))\n\n \u03c0'bb : \u2200 {b} x (px : p x \u2261 b) (qx : q x \u2261 b) ppx qqx ([ppx] : p x \u2261 ppx) ([qqx] : q x \u2261 qqx) \u2192 \u03c0' x ppx qqx [ppx] [qqx] \u2261 x\n \u03c0'bb x px qx 1\u2082 1\u2082 [ppx] [qqx] = \u2261.refl\n \u03c0'bb x px qx 1\u2082 0\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [qqx]) (\u2261.trans qx (\u2261.trans (\u2261.sym px) [ppx]))))\n \u03c0'bb x px qx 0\u2082 1\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [ppx]) (\u2261.trans px (\u2261.trans (\u2261.sym qx) [qqx]))))\n \u03c0'bb x px qx 0\u2082 0\u2082 [ppx] [qqx] = \u2261.refl\n\n \u03c0\u03c0' : \u2200 x px qx [px] [qx] \u2192 let y = (\u03c0' x px qx [px] [qx]) in \u03c0' y (p y) (q y) \u2261.refl \u2261.refl \u2261 x\n \u03c0\u03c0' x 1\u2082 1\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n \u03c0\u03c0' x 1\u2082 0\u2082 px qx = let fx = f x px qx in let pfx = proj\u2081 (proj\u2082 fx) in let qfx = proj\u2082 (proj\u2082 fx) in \u2261.trans (\u03c001 (proj\u2081 fx) (p (proj\u2081 fx)) (q (proj\u2081 fx)) \u2261.refl \u2261.refl pfx qfx) (f-1f x px qx)\n \u03c0\u03c0' x 0\u2082 1\u2082 px qx = let fx = f-1 x px qx in let pfx = proj\u2081 (proj\u2082 fx) in let qfx = proj\u2082 (proj\u2082 fx) in \u2261.trans (\u03c010 (proj\u2081 fx) (p (proj\u2081 fx)) (q (proj\u2081 fx)) \u2261.refl \u2261.refl pfx qfx) (ff-1 x px qx)\n \u03c0\u03c0' x 0\u2082 0\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n\n \u03c0\u03c0 : \u2200 x \u2192 \u03c0 (\u03c0 x) \u2261 x\n \u03c0\u03c0 x = \u03c0\u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n prop' : \u2200 px qx x ([px] : p x \u2261 px) ([qx] : q x \u2261 qx) \u2192 q (\u03c0' x px qx [px] [qx]) \u2261 px\n prop' 1\u2082 1\u2082 x px qx = qx\n prop' 1\u2082 0\u2082 x px qx = proj\u2082 (proj\u2082 (f x px qx))\n prop' 0\u2082 1\u2082 x px qx = proj\u2082 (proj\u2082 (f-1 x px qx))\n prop' 0\u2082 0\u2082 x px qx = qx\n\n prop : \u2200 x \u2192 p x \u2261 q (\u03c0 x)\n prop x = \u2261.sym (prop' (p x) (q x) x \u2261.refl \u2261.refl)\n\nmodule _ {a b f} {A : Set a} {B : A \u2192 Set b}\n (F : (x : A) \u2192 B x \u2192 Set f) where\n\n -- Also called Axiom of dependent choice.\n dep-choice-iso : (\u03a0 A (\u03bb x \u2192 \u03a3 (B x) (F x)))\n \u2194 (\u03a3 (\u03a0 A B) \u03bb f \u2192 \u03a0 A (F \u02e2 f))\n dep-choice-iso = inverses (\u21d2) (uncurry <_,_>) (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n where\n \u21d2 = \u03bb f \u2192 (\u03bb x \u2192 proj\u2081 (f x)) , (\u03bb x \u2192 proj\u2082 (f x))\n\nMaybe-injective : \u2200 {A B : Set} \u2192 Maybe A \u2194 Maybe B \u2192 A \u2194 B\nMaybe-injective f = Iso.iso (g f) (g-empty f)\n module Maybe-injective where\n open Inverse using (injective; left-inverse-of; right-inverse-of)\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (tof-tot : \u2200 x \u2192 to f (just x) \u2262 nothing)\n (fof-tot : \u2200 x \u2192 from f (just x) \u2262 nothing) where\n\n CancelMaybe' : A \u2194 B\n CancelMaybe' = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n\n \u21d2 : _ \u2192 _\n \u21d2 x with to f (just x) | tof-tot x\n \u21d2 x | just y | _ = y\n \u21d2 x | nothing | p = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d0 : _ \u2192 _\n \u21d0 x with from f (just x) | fof-tot x\n \u21d0 x | just y | p = y\n \u21d0 x | nothing | p = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 x with to f (just x)\n | tof-tot x\n | from f (just (\u21d2 x))\n | fof-tot (\u21d2 x)\n | left-inverse-of f (just x)\n | right-inverse-of f (just (\u21d2 x))\n \u21d0\u21d2 x | just x\u2081 | p | just x\u2082 | q | b | c = just-injective (\u2261.trans (\u2261.sym (left-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong (from f) c) b))\n \u21d0\u21d2 x | just x\u2081 | p | nothing | q | _ | _ = \ud835\udfd8-elim (q \u2261.refl)\n \u21d0\u21d2 x | nothing | p | z | q | _ | _ = \ud835\udfd8-elim (p \u2261.refl)\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 x with from f (just x)\n | fof-tot x\n | to f (just (\u21d0 x))\n | tof-tot (\u21d0 x)\n | right-inverse-of f (just x)\n | left-inverse-of f (just (\u21d0 x))\n \u21d2\u21d0 x | just x\u2081 | p | just x\u2082 | q | b | c = just-injective (\u2261.trans (\u2261.sym (right-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong (to f) c) b))\n \u21d2\u21d0 x | just x\u2081 | p | nothing | q | _ | _ = \ud835\udfd8-elim (q \u2261.refl)\n \u21d2\u21d0 x | nothing | p | z | q | _ | _ = \ud835\udfd8-elim (p \u2261.refl)\n\n module Iso {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (f-empty : to f nothing \u2261 nothing) where\n\n tof-tot : \u2200 x \u2192 to f (just x) \u2262 nothing\n tof-tot x eq with injective f (\u2261.trans eq (\u2261.sym f-empty))\n ... | ()\n\n f-empty' : from f nothing \u2261 nothing\n f-empty' = \u2261.trans (\u2261.sym (\u2261.cong (from f) f-empty)) (left-inverse-of f nothing)\n\n fof-tot : \u2200 x \u2192 from f (just x) \u2262 nothing\n fof-tot x eq with injective (sym f) (\u2261.trans eq (\u2261.sym f-empty'))\n ... | ()\n\n iso : A \u2194 B\n iso = CancelMaybe' f tof-tot fof-tot\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B) where\n\n g : Maybe A \u2194 Maybe B\n g = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n\n \u21d2 : Maybe A \u2192 Maybe B\n \u21d2 (just x) with to f (just x)\n ... | nothing = to f nothing\n ... | just y = just y\n \u21d2 nothing = nothing\n\n \u21d0 : Maybe B \u2192 Maybe A\n \u21d0 (just x) with from f (just x)\n ... | nothing = from f nothing\n ... | just y = just y\n \u21d0 nothing = nothing\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (just x) with to f (just x) | left-inverse-of f (just x)\n \u21d0\u21d2 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d0\u21d2 (just x) | nothing | p with to f nothing | left-inverse-of f nothing\n \u21d0\u21d2 (just x) | nothing | p | just _ | q rewrite q = p\n \u21d0\u21d2 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d0\u21d2 nothing = \u2261.refl\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (just x) with from f (just x) | right-inverse-of f (just x)\n \u21d2\u21d0 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d2\u21d0 (just x) | nothing | p with from f nothing | right-inverse-of f nothing\n \u21d2\u21d0 (just x) | nothing | p | just _ | q rewrite q = p\n \u21d2\u21d0 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d2\u21d0 nothing = \u2261.refl\n\n g-empty : to g nothing \u2261 nothing\n g-empty = \u2261.refl\n\nprivate\n Setoid\u2080 : \u2605 _\n Setoid\u2080 = Setoid \u2080 \u2080\n\n\u03a3\u2261\u2194\ud835\udfd9 : \u2200 {a} {A : \u2605 a} x \u2192 \u03a3 A (_\u2261_ x) \u2194 \ud835\udfd9\n\u03a3\u2261\u2194\ud835\udfd9 x = inverses (F.const _) (\u03bb _ \u2192 _ , \u2261.refl)\n helper (\u03bb _ \u2192 \u2261.refl)\n where helper : (y : \u03a3 _ (_\u2261_ x)) \u2192 (x , \u2261.refl) \u2261 y\n helper (.x , \u2261.refl) = \u2261.refl\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : \u03a3 A B \u2192 \u2605 c} where\n curried : \u03a0 (\u03a3 A B) C \u2194 \u03a0 A \u03bb a \u2192 \u03a0 (B a) \u03bb b \u2192 C (a , b)\n curried = inverses curry uncurry (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u2192 \u2605 c} (f : A \u2194 B) where\n private\n left-f = Inverse.left-inverse-of f\n right-f = Inverse.right-inverse-of f\n coe : \u2200 x \u2192 C x \u2192 C (from f (to f x))\n coe x = \u2261.subst C (\u2261.sym (left-f x))\n \u21d2 : \u03a3 A C \u2192 \u03a3 B (C F.\u2218 from f)\n \u21d2 (x , p) = to f x , coe x p\n \u21d0 : \u03a3 B (C F.\u2218 from f) \u2192 \u03a3 A C\n \u21d0 = first (from f)\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , p) rewrite left-f x = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 p = mk\u03a3\u2261 (C F.\u2218 from f) (right-f (proj\u2081 p)) (helper p)\n where\n helper : \u2200 p \u2192 \u2261.subst (C F.\u2218 from f) (right-f (proj\u2081 p)) (coe (proj\u2081 (\u21d0 p)) (proj\u2082 (\u21d0 p))) \u2261 proj\u2082 p\n helper p with to f (from f (proj\u2081 p)) | right-f (proj\u2081 p) | left-f (from f (proj\u2081 p))\n helper _ | ._ | \u2261.refl | \u2261.refl = \u2261.refl\n first-iso : \u03a3 A C \u2194 \u03a3 B (C F.\u2218 from f)\n first-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : B \u2192 \u2605 c} (f : A \u2194 B) where\n sym-first-iso : \u03a3 A (C F.\u2218 to f) \u2194 \u03a3 B C\n sym-first-iso = sym (first-iso (sym f))\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} (f : \u2200 x \u2192 B x \u2194 C x) where\n private\n left-f = Inverse.left-inverse-of F.\u2218 f\n right-f = Inverse.right-inverse-of F.\u2218 f\n \u21d2 : \u03a3 A B \u2192 \u03a3 A C\n \u21d2 = second (to (f _))\n \u21d0 : \u03a3 A C \u2192 \u03a3 A B\n \u21d0 = second (from (f _))\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , y) rewrite left-f x y = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (x , y) rewrite right-f x y = \u2261.refl\n second-iso : \u03a3 A B \u2194 \u03a3 A C\n second-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b} {A : \u2605 a} {B : \u2605 b} (f\u2080 f\u2081 : A \u2194 B) where\n \ud835\udfda\u00d7-second-iso : (\ud835\udfda \u00d7 A) \u2194 (\ud835\udfda \u00d7 B)\n \ud835\udfda\u00d7-second-iso = second-iso {A = \ud835\udfda} {B = const A} {C = const B} (proj (f\u2080 , f\u2081))\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a3 (A \u228e B) C\n T = \u03a3 A (C F.\u2218 inj\u2081) \u228e \u03a3 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 (inj\u2081 x , y) = inj\u2081 (x , y)\n \u21d2 (inj\u2082 x , y) = inj\u2082 (x , y)\n \u21d0 : T \u2192 S\n \u21d0 (inj\u2081 (x , y)) = inj\u2081 x , y\n \u21d0 (inj\u2082 (x , y)) = inj\u2082 x , y\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (inj\u2081 _ , _) = \u2261.refl\n \u21d0\u21d2 (inj\u2082 _ , _) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n \u03a3\u228e-distrib : (\u03a3 (A \u228e B) C) \u2194 (\u03a3 A (C F.\u2218 inj\u2081) \u228e \u03a3 B (C F.\u2218 inj\u2082))\n \u03a3\u228e-distrib = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} where\n private\n S = \u03a3 A (B \u228e\u00b0 C)\n T = \u03a3 A B \u228e \u03a3 A C\n \u21d2 : S \u2192 T\n \u21d2 (x , inj\u2081 y) = inj\u2081 (x , y)\n \u21d2 (x , inj\u2082 y) = inj\u2082 (x , y)\n \u21d0 : T \u2192 S\n \u21d0 (inj\u2081 (x , y)) = x , inj\u2081 y\n \u21d0 (inj\u2082 (x , y)) = x , inj\u2082 y\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (_ , inj\u2081 _) = \u2261.refl\n \u21d0\u21d2 (_ , inj\u2082 _) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n \u03a3-\u228e-hom : \u03a3 A (B \u228e\u00b0 C) \u2194 (\u03a3 A B \u228e \u03a3 A C)\n \u03a3-\u228e-hom = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\n{- requires extensional equality\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a0 (A \u228e B) C\n T = \u03a0 A (C F.\u2218 inj\u2081) \u00d7 \u03a0 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 f = f F.\u2218 inj\u2081 , f F.\u2218 inj\u2082\n \u21d0 : T \u2192 S\n \u21d0 (f , g) = [ f , g ]\n \u21d0\u21d2 : \u2200 f x \u2192 \u21d0 (\u21d2 f) x \u2261 f x\n \u21d0\u21d2 f (inj\u2081 x) = \u2261.refl\n \u21d0\u21d2 f (inj\u2082 y) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (f , g) = \u2261.refl\n\n \u03a0\u00d7-distrib : S \u2194 T\n \u03a0\u00d7-distrib = inverses (\u21d2) (\u21d0) {!\u21d0\u21d2!} \u21d2\u21d0\n-}\n\n\u228e-ICommutativeMonoid : CommutativeMonoid _ _\n\u228e-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u228e-setoid_;\n \u03b5 = \u2261.setoid \ud835\udfd8;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u228e-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = \u2080}{f\u2082 = \u2080})\n\n left-identity : LeftIdentity (\u2261.setoid \ud835\udfd8) _\u228e-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = [ (\u03bb ()) , F.id ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = inj\u2082\n ; cong = \u2082\u223c\u2082\n }\n ; inverse-of = record\n { left-inverse-of = [ (\u03bb ()) , (\u03bb x \u2192 \u2082\u223c\u2082 refl) ]\n ; right-inverse-of = \u03bb x \u2192 refl\n }\n } where open Setoid A\n cong-to : Setoid._\u2248_ (\u2261.setoid \ud835\udfd8 \u228e-setoid A) =[ _ ]\u21d2 _\u2248_\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y) rewrite x\u223c\u2081y = refl\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = x\u223c\u2082y\n\n assoc : Associative _\u228e-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = [ [ inj\u2081 , inj\u2082 F.\u2218 inj\u2081 ] , inj\u2082 F.\u2218 inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = [ inj\u2081 F.\u2218 inj\u2081 , [ inj\u2081 F.\u2218 inj\u2082 , inj\u2082 ] ]\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = [ [ (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2081\u223c\u2081 (refl A))) , (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2082\u223c\u2082 (refl B)) ) ] , (\u03bb _ \u2192 \u2082\u223c\u2082 (refl C)) ]\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (refl A)) , [ (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2081\u223c\u2081 (refl B))) , (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2082\u223c\u2082 (refl C))) ] ]\n }\n } where\n open Setoid\n cong-to : _\u2248_ ((A \u228e-setoid B) \u228e-setoid C) =[ _ ]\u21d2 _\u2248_ (A \u228e-setoid (B \u228e-setoid C))\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2082 ()))\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 x\u223c\u2081y\n cong-to (\u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2082y)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = \u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)\n\n cong-from : _\u2248_ (A \u228e-setoid (B \u228e-setoid C)) =[ _ ]\u21d2 _\u2248_ ((A \u228e-setoid B) \u228e-setoid C)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 x\u223c\u2081y) = \u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2082 ()))\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 x\u223c\u2082y\n\n comm : Commutative _\u228e-setoid_\n comm A B = record\n { to = swap'\n ; from = swap'\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u228e-setoid B FE.\u27f6 B \u228e-setoid A\n swap' {A} {B} = record\n { _\u27e8$\u27e9_ = [ inj\u2082 , inj\u2081 ]\n ; cong = cong\n } where\n cong : Setoid._\u2248_ (A \u228e-setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (B \u228e-setoid A)\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2082\u223c\u2082 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2081\u223c\u2081 x\u223c\u2082y\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u228e-setoid B) (swap' FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (Setoid.refl A)) , (\u03bb _ \u2192 \u2082\u223c\u2082 (Setoid.refl B)) ]\n\n\u00d7-ICommutativeMonoid : CommutativeMonoid _ _\n\u00d7-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u00d7-setoid_;\n \u03b5 = \u2261.setoid \ud835\udfd9;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u00d7-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = \u2080}{f\u2082 = \u2080})\n\n left-identity : LeftIdentity (\u2261.setoid \ud835\udfd9) _\u00d7-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2082\n ; cong = proj\u2082\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 _ , x\n ; cong = \u03bb x \u2192 \u2261.refl , x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u2261.refl , (Setoid.refl A)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl A\n }\n }\n\n assoc : Associative _\u00d7-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n ; cong = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n }\n ; from = record\n { _\u27e8$\u27e9_ = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n ; cong = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb _ \u2192 Setoid.refl ((A \u00d7-setoid B) \u00d7-setoid C)\n ; right-inverse-of = \u03bb _ \u2192 Setoid.refl (A \u00d7-setoid (B \u00d7-setoid C))\n }\n }\n\n comm : Commutative _\u00d7-setoid_\n comm A B = record\n { to = swap' {A} {B}\n ; from = swap' {B} {A}\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u00d7-setoid B FE.\u27f6 B \u00d7-setoid A\n swap' {A}{B} = record { _\u27e8$\u27e9_ = swap; cong = swap }\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u00d7-setoid B) (swap' {B} {A} FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = \u03bb x \u2192 Setoid.refl (A \u00d7-setoid B)\n\n\u00d7\u228e-ICommutativeSemiring : CommutativeSemiring _ _\n\u00d7\u228e-ICommutativeSemiring = record\n { _\u2248_ = Inv.Inverse\n ; _+_ = _\u228e-setoid_\n ; _*_ = _\u00d7-setoid_\n ; 0# = \u2261.setoid \ud835\udfd8\n ; 1# = \u2261.setoid \ud835\udfd9\n ; isCommutativeSemiring = record\n { +-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u228e-ICommutativeMonoid\n ; *-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u00d7-ICommutativeMonoid\n ; distrib\u02b3 = distrib\u02b3\n ; zero\u02e1 = zero\u02e1\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = \u2080}{f\u2082 = \u2080})\n\n distrib\u02b3 : _\u00d7-setoid_ DistributesOver\u02b3 _\u228e-setoid_\n distrib\u02b3 A B C = record\n { to = record\n { _\u27e8$\u27e9_ = uncurry [ curry inj\u2081 , curry inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = frm\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = uncurry [ (\u03bb x y \u2192 (\u2081\u223c\u2081 (refl B)) , (refl A)) , (\u03bb x y \u2192 (\u2082\u223c\u2082 (refl C)) , (refl A)) ]\n ; right-inverse-of = [ uncurry (\u03bb x y \u2192 \u2081\u223c\u2081 ((refl B {x}) , (refl A {y})))\n , uncurry (\u03bb x y \u2192 \u2082\u223c\u2082 ((refl C {x}) , (refl A {y}))) ]\n }\n } where\n open Setoid\n A' = Carrier A\n B' = Carrier B\n C' = Carrier C\n\n cong-to : _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A) =[ _ ]\u21d2 _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A))\n cong-to (\u2081\u223c\u2082 () , _)\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y , A-rel) = \u2081\u223c\u2081 (x\u223c\u2081y , A-rel)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y , A-rel) = \u2082\u223c\u2082 (x\u223c\u2082y , A-rel)\n\n frm : B' \u00d7 A' \u228e C' \u00d7 A' \u2192 (B' \u228e C') \u00d7 A'\n frm = [ map\u00d7 inj\u2081 F.id , map\u00d7 inj\u2082 F.id ]\n\n cong-from : _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A)) =[ _ ]\u21d2 _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 (B-rel , A-rel)) = \u2081\u223c\u2081 B-rel , A-rel\n cong-from (\u2082\u223c\u2082 (C-rel , A-rel)) = \u2082\u223c\u2082 C-rel , A-rel\n\n zero\u02e1 : LeftZero (\u2261.setoid \ud835\udfd8) _\u00d7-setoid_\n zero\u02e1 A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2081\n ; cong = proj\u2081\n }\n ; from = record\n { _\u27e8$\u27e9_ = \ud835\udfd8-elim\n ; cong = \u03bb { {()} x }\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \ud835\udfd8-elim (proj\u2081 x)\n ; right-inverse-of = \u03bb x \u2192 \ud835\udfd8-elim x\n }\n }\n\nmodule \u00d7-CMon {a} = CommutativeMonoid (\u00d7-CommutativeMonoid FR.bijection a)\nmodule \u228e-CMon {a} = CommutativeMonoid (\u228e-CommutativeMonoid FR.bijection a)\nmodule \u00d7\u228e\u00b0 {a} = CommutativeSemiring (\u00d7\u228e-CommutativeSemiring FR.bijection a)\n\nmodule \u00d7-ICMon = CommutativeMonoid \u00d7-ICommutativeMonoid\nmodule \u228e-ICMon = CommutativeMonoid \u228e-ICommutativeMonoid\nmodule \u00d7\u228e\u00b0I = CommutativeSemiring \u00d7\u228e-ICommutativeSemiring\n\nlift-\u228e : \u2200 {A B : Set} \u2192 Inv.Inverse ((\u2261.setoid A) \u228e-setoid (\u2261.setoid B)) (\u2261.setoid (A \u228e B))\nlift-\u228e {A}{B} = record\n { to = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = cong\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = \u03bb x \u2192 Setoid.reflexive (\u2261.setoid A \u228e-setoid \u2261.setoid B) x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid A \u228e-setoid \u2261.setoid B)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid (A \u228e B))\n }\n } where\n cong : Setoid._\u2248_ (\u2261.setoid A \u228e-setoid \u2261.setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (\u2261.setoid (A \u228e B))\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2261.cong inj\u2081 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2261.cong inj\u2082 x\u223c\u2082y\n\nswap-iso : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 (A \u00d7 B) \u2194 (B \u00d7 A)\nswap-iso = inverses swap swap (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u00d7 B \u2192 \u2605 c} where\n \u03a3\u00d7-swap : \u03a3 (A \u00d7 B) C \u2194 \u03a3 (B \u00d7 A) (C F.\u2218 swap)\n \u03a3\u00d7-swap = first-iso swap-iso\n\nMaybe\u2194Lift\ud835\udfd9\u228e : \u2200 {\u2113 a} {A : \u2605 a} \u2192 Maybe A \u2194 (Lift {\u2113 = \u2113} \ud835\udfd9 \u228e A)\nMaybe\u2194Lift\ud835\udfd9\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe\u2194\ud835\udfd9\u228e : \u2200 {a} {A : \u2605 a} \u2192 Maybe A \u2194 (\ud835\udfd9 \u228e A)\nMaybe\u2194\ud835\udfd9\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 A \u2194 B \u2192 Maybe A \u2194 Maybe B\nMaybe-cong A\u2194B = sym Maybe\u2194\ud835\udfd9\u228e \u2218 id \u228e-cong A\u2194B \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe : \u2200 {a} {A : \u2605 a} n \u2192 Maybe (Maybe^ n A) \u2194 Maybe^ n (Maybe A)\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe zero = id\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe (suc n) = Maybe-cong (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n)\n\nMaybe^-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} n \u2192 A \u2194 B \u2192 Maybe^ n A \u2194 Maybe^ n B\nMaybe^-cong zero = F.id\nMaybe^-cong (suc n) = Maybe-cong F.\u2218 Maybe^-cong n\n\nVec0\u2194Lift\ud835\udfd9 : \u2200 {a \u2113} {A : \u2605 a} \u2192 Vec A 0 \u2194 Lift {_} {\u2113} \ud835\udfd9\nVec0\u2194Lift\ud835\udfd9 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec0\u2194\ud835\udfd9 : \u2200 {a} {A : \u2605 a} \u2192 Vec A 0 \u2194 \ud835\udfd9\nVec0\u2194\ud835\udfd9 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec\u2218suc\u2194A\u00d7Vec : \u2200 {a} {A : \u2605 a} {n} \u2192 Vec A (suc n) \u2194 (A \u00d7 Vec A n)\nVec\u2218suc\u2194A\u00d7Vec\n = inverses uncons (uncurry _\u2237_) \u2237-uncons (\u03bb _ \u2192 \u2261.refl)\n\ninfix 8 _^_\n\n_^_ : \u2200 {a} \u2192 \u2605 a \u2192 \u2115 \u2192 \u2605 a\nA ^ 0 = Lift \ud835\udfd9\nA ^ suc n = A \u00d7 A ^ n\n\n^\u2194Vec : \u2200 {a} {A : \u2605 a} n \u2192 (A ^ n) \u2194 Vec A n\n^\u2194Vec zero = sym Vec0\u2194Lift\ud835\udfd9\n^\u2194Vec (suc n) = sym Vec\u2218suc\u2194A\u00d7Vec \u2218 (id \u00d7-cong (^\u2194Vec n))\n\nFin0\u2194\ud835\udfd8 : Fin 0 \u2194 \ud835\udfd8\nFin0\u2194\ud835\udfd8 = inverses (\u03bb()) (\u03bb()) (\u03bb()) (\u03bb())\n\nFin1\u2194\ud835\udfd9 : Fin 1 \u2194 \ud835\udfd9\nFin1\u2194\ud835\udfd9 = inverses _ (\u03bb _ \u2192 zero) \u21d0\u21d2 (\u03bb _ \u2192 \u2261.refl)\n where \u21d0\u21d2 : (_ : Fin 1) \u2192 _\n \u21d0\u21d2 zero = \u2261.refl\n \u21d0\u21d2 (suc ())\n\nFin\u2218suc\u2194Maybe\u2218Fin : \u2200 {n} \u2192 Fin (suc n) \u2194 Maybe (Fin n)\nFin\u2218suc\u2194Maybe\u2218Fin {n}\n = inverses to' (maybe suc zero)\n (\u03bb { zero \u2192 \u2261.refl ; (suc n) \u2192 \u2261.refl })\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n where to' : Fin (suc n) \u2192 Maybe (Fin n)\n to' zero = nothing\n to' (suc n) = just n\n\nFin-injective : \u2200 {m n} \u2192 Fin m \u2194 Fin n \u2192 m \u2261 n\nFin-injective = go _ _ where\n go : \u2200 m n \u2192 Fin m \u2194 Fin n \u2192 m \u2261 n\n go zero zero iso = \u2261.refl\n go zero (suc n) iso with from iso zero\n ... | ()\n go (suc m) zero iso with to iso zero\n ... | ()\n go (suc m) (suc n) iso = \u2261.cong suc (go m n (Maybe-injective (Fin\u2218suc\u2194Maybe\u2218Fin \u2218 iso \u2218 sym Fin\u2218suc\u2194Maybe\u2218Fin)))\n\nLift\u2194id : \u2200 {a} {A : \u2605 a} \u2192 Lift {a} {a} A \u2194 A\nLift\u2194id = inverses lower lift (\u03bb { (lift x) \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\n\ud835\udfd9\u00d7A\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd9 \u00d7 A) \u2194 A\n\ud835\udfd9\u00d7A\u2194A = proj\u2081 \u00d7-CMon.identity _ \u2218 sym Lift\u2194id \u00d7-cong id\n\nA\u00d7\ud835\udfd9\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u00d7 \ud835\udfd9) \u2194 A\nA\u00d7\ud835\udfd9\u2194A = proj\u2082 \u00d7-CMon.identity _ \u2218 id \u00d7-cong sym Lift\u2194id\n\n\u03a0\ud835\udfd9F\u2194F : \u2200 {\u2113} {F : \ud835\udfd9 \u2192 \u2605_ \u2113} \u2192 \u03a0 \ud835\udfd9 F \u2194 F _\n\u03a0\ud835\udfd9F\u2194F = inverses (\u03bb x \u2192 x _) const (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\n\ud835\udfd9\u2192A\u2194A : \u2200 {\u2113} {A : \u2605_ \u2113} \u2192 (\ud835\udfd9 \u2192 A) \u2194 A\n\ud835\udfd9\u2192A\u2194A = \u03a0\ud835\udfd9F\u2194F\n\nnot-\ud835\udfda\u2194\ud835\udfda : \ud835\udfda \u2194 \ud835\udfda\nnot-\ud835\udfda\u2194\ud835\udfda = inverses not not not-involutive not-involutive\n\n\u2261-iso : \u2200 {\u2113 \u2113'}{A : \u2605_ \u2113}{B : \u2605_ \u2113'}{x y : A} \u2192 (\u03c0 : A \u2194 B) \u2192 (x \u2261 y) \u2194 (to \u03c0 x \u2261 to \u03c0 y)\n\u2261-iso {x = x}{y} \u03c0 = inverses (\u2261.cong (to \u03c0))\n (\u03bb p \u2192 \u2261.trans (\u2261.sym (Inverse.left-inverse-of \u03c0 x))\n (\u2261.trans (\u2261.cong (from \u03c0) p)\n (Inverse.left-inverse-of \u03c0 y)))\n (\u03bb x \u2192 \u2261.proof-irrelevance _ x) (\u03bb x \u2192 \u2261.proof-irrelevance _ x)\n\n-- requires extensionality\nmodule _ {a} {A : \u2605_ a} (ext\ud835\udfd8 : (f g : \ud835\udfd8 \u2192 A) \u2192 f \u2261 g) where\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 : (\ud835\udfd8 \u2192 A) \u2194 \ud835\udfd9\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 = inverses _ (\u03bb _ ()) (\u03bb h \u2192 ext\ud835\udfd8 _ h) (\u03bb _ \u2192 \u2261.refl)\n\n-- requires extensionality\nmodule _ {\u2113} {F : \ud835\udfd8 \u2192 \u2605_ \u2113} (ext\ud835\udfd8 : (f g : \u03a0 \ud835\udfd8 F) \u2192 f \u2261 g) where\n \u03a0\ud835\udfd8\u2194\ud835\udfd9 : \u03a0 \ud835\udfd8 F \u2194 \ud835\udfd9\n \u03a0\ud835\udfd8\u2194\ud835\udfd9 = inverses _ (\u03bb _ ()) (\u03bb h \u2192 ext\ud835\udfd8 _ h) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {F : \ud835\udfda \u2192 \u2605_ \u2113} (ext\ud835\udfda : {f g : \u03a0 \ud835\udfda F} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 : \u03a0 \ud835\udfda F \u2194 (F 0\u2082 \u00d7 F 1\u2082)\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 = inverses (\u03bb f \u2192 f 0\u2082 , f 1\u2082) proj\n (\u03bb f \u2192 ext\ud835\udfda (\u03bb { 0\u2082 \u2192 \u2261.refl ; 1\u2082 \u2192 \u2261.refl }))\n (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {\u2113} {A : \u2605_ \u2113} (ext\ud835\udfda : {f g : \ud835\udfda \u2192 A} \u2192 f \u2257 g \u2192 f \u2261 g) where\n \ud835\udfda\u2192A\u2194A\u00d7A : (\ud835\udfda \u2192 A) \u2194 (A \u00d7 A)\n \ud835\udfda\u2192A\u2194A\u00d7A = \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 ext\ud835\udfda\n\n\ud835\udfd8\u228eA\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd8 \u228e A) \u2194 A\n\ud835\udfd8\u228eA\u2194A = proj\u2081 \u228e-CMon.identity _ \u2218 sym Lift\u2194id \u228e-cong id\n\nA\u228e\ud835\udfd8\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u228e \ud835\udfd8) \u2194 A\nA\u228e\ud835\udfd8\u2194A = proj\u2082 \u228e-CMon.identity _ \u2218 id \u228e-cong sym Lift\u2194id\n\n\ud835\udfd8\u00d7A\u2194\ud835\udfd8 : \u2200 {A : \u2605\u2080} \u2192 (\ud835\udfd8 \u00d7 A) \u2194 \ud835\udfd8\n\ud835\udfd8\u00d7A\u2194\ud835\udfd8 = Lift\u2194id \u2218 proj\u2081 \u00d7\u228e\u00b0.zero _ \u2218 sym (Lift\u2194id \u00d7-cong id)\n\nMaybe\ud835\udfd8\u2194\ud835\udfd9 : Maybe \ud835\udfd8 \u2194 \ud835\udfd9\nMaybe\ud835\udfd8\u2194\ud835\udfd9 = A\u228e\ud835\udfd8\u2194A \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe^\ud835\udfd8\u2194Fin : \u2200 n \u2192 Maybe^ n \ud835\udfd8 \u2194 Fin n\nMaybe^\ud835\udfd8\u2194Fin zero = sym Fin0\u2194\ud835\udfd8\nMaybe^\ud835\udfd8\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\ud835\udfd8\u2194Fin n)\n\nMaybe^\ud835\udfd9\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \ud835\udfd9 \u2194 Fin (suc n)\nMaybe^\ud835\udfd9\u2194Fin1+ n = Maybe^\ud835\udfd8\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\ud835\udfd8\u2194\ud835\udfd9)\n\nMaybe-\u228e : \u2200 {a} {A B : \u2605 a} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e {a} = sym Maybe\u2194Lift\ud835\udfd9\u228e \u2218 \u228e-CMon.assoc (Lift {_} {a} \ud835\udfd9) _ _ \u2218 (Maybe\u2194Lift\ud835\udfd9\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \ud835\udfd8 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \ud835\udfd8\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n\n\u03a3\ud835\udfd8\u2194\ud835\udfd8 : \u2200 {a} (F : \ud835\udfd8 \u2192 \u2605_ a) \u2192 \u03a3 \ud835\udfd8 F \u2194 \ud835\udfd8\n\u03a3\ud835\udfd8\u2194\ud835\udfd8 F = inverses proj\u2081 (\u03bb ()) (\u03bb { ((), _) }) (\u03bb ())\n\n\u03a3\ud835\udfda\u2194\u228e : \u2200 {a} (F : \ud835\udfda \u2192 \u2605_ a) \u2192 \u03a3 \ud835\udfda F \u2194 (F 0\u2082 \u228e F 1\u2082)\n\u03a3\ud835\udfda\u2194\u228e F = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d2 : (x : \u03a3 _ _) \u2192 _\n \u21d2 (0\u2082 , p) = inj\u2081 p\n \u21d2 (1\u2082 , p) = inj\u2082 p\n \u21d0 : (x : _ \u228e _) \u2192 _\n \u21d0 (inj\u2081 x) = 0\u2082 , x\n \u21d0 (inj\u2082 y) = 1\u2082 , y\n\n \u21d0\u21d2 : (_ : \u03a3 _ _) \u2192 _\n \u21d0\u21d2 (0\u2082 , p) = \u2261.refl\n \u21d0\u21d2 (1\u2082 , p) = \u2261.refl\n \u21d2\u21d0 : (_ : _ \u228e _) \u2192 _\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n\u228e\u21ff\u03a32 : \u2200 {\u2113} {A B : \u2605 \u2113} \u2192 (A \u228e B) \u2194 \u03a3 \ud835\udfda [0: A 1: B ]\n\u228e\u21ff\u03a32 {A = A} {B} = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d2 : A \u228e B \u2192 _\n \u21d2 (inj\u2081 x) = 0\u2082 , x\n \u21d2 (inj\u2082 x) = 1\u2082 , x\n \u21d0 : \u03a3 _ _ \u2192 A \u228e B\n \u21d0 (0\u2082 , x) = inj\u2081 x\n \u21d0 (1\u2082 , x) = inj\u2082 x\n \u21d0\u21d2 : (_ : _ \u228e _) \u2192 _\n \u21d0\u21d2 (inj\u2081 x) = \u2261.refl\n \u21d0\u21d2 (inj\u2082 x) = \u2261.refl\n \u21d2\u21d0 : (_ : \u03a3 _ _) \u2192 _\n \u21d2\u21d0 (0\u2082 , x) = \u2261.refl\n \u21d2\u21d0 (1\u2082 , x) = \u2261.refl\n\n\ud835\udfda\u2194\ud835\udfd9\u228e\ud835\udfd9 : \ud835\udfda \u2194 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2194\ud835\udfd9\u228e\ud835\udfd9 = inverses (proj (inj\u2081 _ , inj\u2082 _)) [ F.const 0\u2082 , F.const 1\u2082 ] \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d0\u21d2 : (_ : \ud835\udfda) \u2192 _\n \u21d0\u21d2 0\u2082 = \u2261.refl\n \u21d0\u21d2 1\u2082 = \u2261.refl\n \u21d2\u21d0 : (_ : \ud835\udfd9 \u228e \ud835\udfd9) \u2192 _\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\nFin-\u228e-+ : \u2200 m n \u2192 (Fin m \u228e Fin n) \u2194 Fin (m + n)\nFin-\u228e-+ m n = Maybe^\ud835\udfd8\u2194Fin (m + n)\n \u2218 Maybe^-\u228e-+ m n\n \u2218 sym (Maybe^\ud835\udfd8\u2194Fin m \u228e-cong Maybe^\ud835\udfd8\u2194Fin n)\n\nFin\u2218suc\u2194\ud835\udfd9\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2194 (\ud835\udfd9 \u228e Fin n)\nFin\u2218suc\u2194\ud835\udfd9\u228eFin = Maybe\u2194\ud835\udfd9\u228e \u2218 Fin\u2218suc\u2194Maybe\u2218Fin\n\nFin\u2194\ud835\udfd9\u228e^\ud835\udfd8 : \u2200 n \u2192 Fin n \u2194 \ud835\udfd9\u228e^ n\nFin\u2194\ud835\udfd9\u228e^\ud835\udfd8 zero = Fin0\u2194\ud835\udfd8\nFin\u2194\ud835\udfd9\u228e^\ud835\udfd8 (suc n) = Inv.id \u228e-cong Fin\u2194\ud835\udfd9\u228e^\ud835\udfd8 n Inv.\u2218 Fin\u2218suc\u2194\ud835\udfd9\u228eFin\n\nFin-\u00d7-* : \u2200 m n \u2192 (Fin m \u00d7 Fin n) \u2194 Fin (m * n)\nFin-\u00d7-* zero n = (Fin 0 \u00d7 Fin n) \u2194\u27e8 Fin0\u2194\ud835\udfd8 \u00d7-cong id \u27e9\n (\ud835\udfd8 \u00d7 Fin n) \u2194\u27e8 \ud835\udfd8\u00d7A\u2194\ud835\udfd8 \u27e9\n \ud835\udfd8 \u2194\u27e8 sym Fin0\u2194\ud835\udfd8 \u27e9\n Fin 0 \u220e\n where open EquationalReasoning hiding (sym)\nFin-\u00d7-* (suc m) n = (Fin (suc m) \u00d7 Fin n) \u2194\u27e8 Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u00d7-cong id \u27e9\n ((\ud835\udfd9 \u228e Fin m) \u00d7 Fin n) \u2194\u27e8 \u00d7\u228e\u00b0.distrib\u02b3 (Fin n) \ud835\udfd9 (Fin m) \u27e9\n ((\ud835\udfd9 \u00d7 Fin n) \u228e (Fin m \u00d7 Fin n)) \u2194\u27e8 \ud835\udfd9\u00d7A\u2194A \u228e-cong Fin-\u00d7-* m n \u27e9\n (Fin n \u228e Fin (m * n)) \u2194\u27e8 Fin-\u228e-+ n (m * n) \u27e9\n Fin (suc m * n) \u220e\n where open EquationalReasoning hiding (sym)\n\nFin\u228e-injective : \u2200 {A B : Set} n \u2192 (Fin n \u228e A) \u2194 (Fin n \u228e B) \u2192 A \u2194 B\nFin\u228e-injective zero f = \ud835\udfd8\u228eA\u2194A \u2218 Fin0\u2194\ud835\udfd8 \u228e-cong id \u2218 f \u2218 sym Fin0\u2194\ud835\udfd8 \u228e-cong id \u2218 sym \ud835\udfd8\u228eA\u2194A\nFin\u228e-injective (suc n) f =\n Fin\u228e-injective n\n (Maybe-injective\n (sym Maybe\u2194\ud835\udfd9\u228e \u2218\n \u228e-CMon.assoc _ _ _ \u2218\n Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u228e-cong id \u2218\n f \u2218\n sym Fin\u2218suc\u2194\ud835\udfd9\u228eFin \u228e-cong id \u2218\n sym (\u228e-CMon.assoc _ _ _) \u2218\n Maybe\u2194\ud835\udfd9\u228e))\n\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"142187d4f24336fe4c321504b51b961590896d1b","subject":"Cosmetic changes.","message":"Cosmetic changes.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Program\/Mirror\/Type.agda","new_file":"src\/fot\/FOTC\/Program\/Mirror\/Type.agda","new_contents":"------------------------------------------------------------------------------\n-- The types used by the mirror function\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Program.Mirror.Type where\n\nopen import FOTC.Base\nopen FOTC.Base.BList\nopen import FOTC.Data.List\n\n------------------------------------------------------------------------------\n-- Tree terms.\npostulate node : D \u2192 D \u2192 D\n\n-- The mutually totality predicates\n\ndata Forest : D \u2192 Set -- The list of rose trees (called forest).\ndata Tree : D \u2192 Set -- The rose tree type.\n\ndata Forest where\n fnil : Forest []\n fcons : \u2200 {t ts} \u2192 Tree t \u2192 Forest ts \u2192 Forest (t \u2237 ts)\n{-# ATP axiom fnil fcons #-}\n\ndata Tree where\n tree : \u2200 d {ts} \u2192 Forest ts \u2192 Tree (node d ts)\n{-# ATP axiom tree #-}\n\n------------------------------------------------------------------------------\n-- Mutual induction for Tree and Forest\n\n-- Adapted from the mutual induction principles generate from Coq 8.4\n-- using the command:\n--\n-- Scheme Tree_mutual_ind :=\n-- Minimality for Tree Sort Prop\n-- with Forest_mutual_ind :=\n-- Minimality for Forest Sort Prop.\n\nTree-ind :\n {A B : D \u2192 Set} \u2192\n (\u2200 d {ts} \u2192 Forest ts \u2192 B ts \u2192 A (node d ts)) \u2192\n B [] \u2192\n (\u2200 {t ts} \u2192 Tree t \u2192 A t \u2192 Forest ts \u2192 B ts \u2192 B (t \u2237 ts)) \u2192\n \u2200 {t} \u2192 Tree t \u2192 A t\n\nForest-ind :\n {P B : D \u2192 Set} \u2192\n (\u2200 d {ts} \u2192 Forest ts \u2192 B ts \u2192 P (node d ts)) \u2192\n B [] \u2192\n (\u2200 {t ts} \u2192 Tree t \u2192 P t \u2192 Forest ts \u2192 B ts \u2192 B (t \u2237 ts)) \u2192\n \u2200 {ts} \u2192 Forest ts \u2192 B ts\n\nTree-ind ihA B[] _ (tree d fnil) = ihA d fnil B[]\nTree-ind ihA B[] ihB (tree d (fcons Tt Fts)) =\n ihA d (fcons Tt Fts) (ihB Tt (Tree-ind ihA B[] ihB Tt)\n Fts (Forest-ind ihA B[] ihB Fts))\n\nForest-ind _ B[] _ fnil = B[]\nForest-ind ihP B[] ihB (fcons Tt Fts) =\n ihB Tt (Tree-ind ihP B[] ihB Tt) Fts (Forest-ind ihP B[] ihB Fts)\n","old_contents":"------------------------------------------------------------------------------\n-- The types used by the mirror function\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Program.Mirror.Type where\n\nopen import FOTC.Base\nopen FOTC.Base.BList\nopen import FOTC.Data.List\n\n------------------------------------------------------------------------------\n-- Tree terms.\npostulate\n node : D \u2192 D \u2192 D\n\n-- The mutually totality predicates\n\ndata Forest : D \u2192 Set -- The list of rose trees (called forest).\ndata Tree : D \u2192 Set -- The rose tree type.\n\ndata Forest where\n fnil : Forest []\n fcons : \u2200 {t ts} \u2192 Tree t \u2192 Forest ts \u2192 Forest (t \u2237 ts)\n{-# ATP axiom fnil fcons #-}\n\ndata Tree where\n tree : \u2200 d {ts} \u2192 Forest ts \u2192 Tree (node d ts)\n{-# ATP axiom tree #-}\n\n------------------------------------------------------------------------------\n-- Mutual induction for Tree and Forest\n\n-- Adapted from the mutual induction principles generate from Coq 8.4\n-- using the command:\n--\n-- Scheme Tree_mutual_ind :=\n-- Minimality for Tree Sort Prop\n-- with Forest_mutual_ind :=\n-- Minimality for Forest Sort Prop.\n\nTree-ind :\n {A B : D \u2192 Set} \u2192\n (\u2200 d {ts} \u2192 Forest ts \u2192 B ts \u2192 A (node d ts)) \u2192\n B [] \u2192\n (\u2200 {t ts} \u2192 Tree t \u2192 A t \u2192 Forest ts \u2192 B ts \u2192 B (t \u2237 ts)) \u2192\n \u2200 {t} \u2192 Tree t \u2192 A t\n\nForest-ind :\n {P B : D \u2192 Set} \u2192\n (\u2200 d {ts} \u2192 Forest ts \u2192 B ts \u2192 P (node d ts)) \u2192\n B [] \u2192\n (\u2200 {t ts} \u2192 Tree t \u2192 P t \u2192 Forest ts \u2192 B ts \u2192 B (t \u2237 ts)) \u2192\n \u2200 {ts} \u2192 Forest ts \u2192 B ts\n\nTree-ind ihA B[] _ (tree d fnil) = ihA d fnil B[]\nTree-ind ihA B[] ihB (tree d (fcons Tt Fts)) =\n ihA d (fcons Tt Fts) (ihB Tt (Tree-ind ihA B[] ihB Tt)\n Fts (Forest-ind ihA B[] ihB Fts))\n\nForest-ind _ B[] _ fnil = B[]\nForest-ind ihP B[] ihB (fcons Tt Fts) =\n ihB Tt (Tree-ind ihP B[] ihB Tt) Fts (Forest-ind ihP B[] ihB Fts)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"0ab0b0012d2db6e26ec2716c2d95f3743111df4d","subject":"MType: Fix arity of _v\u00d7_","message":"MType: Fix arity of _v\u00d7_\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Syntax\/MType.agda","new_file":"Parametric\/Syntax\/MType.agda","new_contents":"import Parametric.Syntax.Type as Type\n\nmodule Parametric.Syntax.MType where\n\nmodule Structure (Base : Type.Structure) where\n open Type.Structure Base\n\n mutual\n -- Derived from CBPV\n data ValType : Set where\n U : (c : CompType) \u2192 ValType\n B : (\u03b9 : Base) \u2192 ValType\n vUnit : ValType\n _v\u00d7_ : (\u03c4\u2081 : ValType) \u2192 (\u03c4\u2082 : ValType) \u2192 ValType\n _v+_ : (\u03c4\u2081 : ValType) \u2192 (\u03c4\u2082 : ValType) \u2192 ValType\n\n -- Same associativity as the standard _\u00d7_\n infixr 2 _v\u00d7_\n\n data CompType : Set where\n F : ValType \u2192 CompType\n _\u21db_ : ValType \u2192 CompType \u2192 CompType\n -- We did not use this in CBPV, so dropped.\n -- _\u03a0_ : CompType \u2192 CompType \u2192 CompType\n\n cbnToCompType : Type \u2192 CompType\n cbnToCompType (base \u03b9) = F (B \u03b9)\n cbnToCompType (\u03c3 \u21d2 \u03c4) = U (cbnToCompType \u03c3) \u21db cbnToCompType \u03c4\n\n cbvToValType : Type \u2192 ValType\n cbvToValType (base \u03b9) = B \u03b9\n cbvToValType (\u03c3 \u21d2 \u03c4) = U (cbvToValType \u03c3 \u21db F (cbvToValType \u03c4))\n\n open import Base.Syntax.Context ValType public\n using ()\n renaming\n ( \u2205 to \u2205\u2205\n ; _\u2022_ to _\u2022\u2022_\n ; mapContext to mapValCtx\n ; Var to ValVar\n ; Context to ValContext\n ; this to vThis; that to vThat)\n\n cbnToValType : Type \u2192 ValType\n cbnToValType \u03c4 = U (cbnToCompType \u03c4)\n\n cbvToCompType : Type \u2192 CompType\n cbvToCompType \u03c4 = F (cbvToValType \u03c4)\n\n fromCBNCtx : Context \u2192 ValContext\n fromCBNCtx \u0393 = mapValCtx cbnToValType \u0393\n\n fromCBVCtx : Context \u2192 ValContext\n fromCBVCtx \u0393 = mapValCtx cbvToValType \u0393\n\n open import Data.List\n open Data.List using (List) public\n fromCBVToCompList : Context \u2192 List CompType\n fromCBVToCompList \u0393 = mapValCtx cbvToCompType \u0393\n\n fromVar : \u2200 {\u0393 \u03c4} \u2192 (f : Type \u2192 ValType) \u2192 Var \u0393 \u03c4 \u2192 ValVar (mapValCtx f \u0393) (f \u03c4)\n fromVar {x \u2022 \u0393} f this = vThis\n fromVar {x \u2022 \u0393} f (that v) = vThat (fromVar f v)\n","old_contents":"import Parametric.Syntax.Type as Type\n\nmodule Parametric.Syntax.MType where\n\nmodule Structure (Base : Type.Structure) where\n open Type.Structure Base\n\n mutual\n -- Derived from CBPV\n data ValType : Set where\n U : (c : CompType) \u2192 ValType\n B : (\u03b9 : Base) \u2192 ValType\n vUnit : ValType\n _v\u00d7_ : (\u03c4\u2081 : ValType) \u2192 (\u03c4\u2082 : ValType) \u2192 ValType\n _v+_ : (\u03c4\u2081 : ValType) \u2192 (\u03c4\u2082 : ValType) \u2192 ValType\n\n data CompType : Set where\n F : ValType \u2192 CompType\n _\u21db_ : ValType \u2192 CompType \u2192 CompType\n -- We did not use this in CBPV, so dropped.\n -- _\u03a0_ : CompType \u2192 CompType \u2192 CompType\n\n cbnToCompType : Type \u2192 CompType\n cbnToCompType (base \u03b9) = F (B \u03b9)\n cbnToCompType (\u03c3 \u21d2 \u03c4) = U (cbnToCompType \u03c3) \u21db cbnToCompType \u03c4\n\n cbvToValType : Type \u2192 ValType\n cbvToValType (base \u03b9) = B \u03b9\n cbvToValType (\u03c3 \u21d2 \u03c4) = U (cbvToValType \u03c3 \u21db F (cbvToValType \u03c4))\n\n open import Base.Syntax.Context ValType public\n using ()\n renaming\n ( \u2205 to \u2205\u2205\n ; _\u2022_ to _\u2022\u2022_\n ; mapContext to mapValCtx\n ; Var to ValVar\n ; Context to ValContext\n ; this to vThis; that to vThat)\n\n cbnToValType : Type \u2192 ValType\n cbnToValType \u03c4 = U (cbnToCompType \u03c4)\n\n cbvToCompType : Type \u2192 CompType\n cbvToCompType \u03c4 = F (cbvToValType \u03c4)\n\n fromCBNCtx : Context \u2192 ValContext\n fromCBNCtx \u0393 = mapValCtx cbnToValType \u0393\n\n fromCBVCtx : Context \u2192 ValContext\n fromCBVCtx \u0393 = mapValCtx cbvToValType \u0393\n\n open import Data.List\n open Data.List using (List) public\n fromCBVToCompList : Context \u2192 List CompType\n fromCBVToCompList \u0393 = mapValCtx cbvToCompType \u0393\n\n fromVar : \u2200 {\u0393 \u03c4} \u2192 (f : Type \u2192 ValType) \u2192 Var \u0393 \u03c4 \u2192 ValVar (mapValCtx f \u0393) (f \u03c4)\n fromVar {x \u2022 \u0393} f this = vThis\n fromVar {x \u2022 \u0393} f (that v) = vThat (fromVar f v)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"fa5b698a20a02c32894d577d7a1ff7c70cd2b395","subject":"adding a small note about preservation","message":"adding a small note about preservation\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"preservation.agda","new_file":"preservation.agda","new_contents":"open import Nat\nopen import Prelude\nopen import core\nopen import contexts\n\nopen import lemmas-consistency\nopen import type-assignment-unicity\nopen import binders-disjoint-checks\n\nopen import lemmas-subst-ta\n\nmodule preservation where\n -- if d and d' both result from filling the hole in \u03b5 with terms of the\n -- same type, they too have the same type.\n wt-different-fill : \u2200{ \u0394 \u0393 d \u03b5 d1 d2 d' \u03c4 \u03c41 } \u2192\n d == \u03b5 \u27e6 d1 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n d' == \u03b5 \u27e6 d2 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n wt-different-fill FHOuter D1 D2 D3 FHOuter\n with type-assignment-unicity D1 D2\n ... | refl = D3\n wt-different-fill (FHAp1 eps) (TAAp D1 D2) D3 D4 (FHAp1 D5) = TAAp (wt-different-fill eps D1 D3 D4 D5) D2\n wt-different-fill (FHAp2 eps) (TAAp D1 D2) D3 D4 (FHAp2 D5) = TAAp D1 (wt-different-fill eps D2 D3 D4 D5)\n wt-different-fill (FHNEHole eps) (TANEHole x D1 x\u2081) D2 D3 (FHNEHole D4) = TANEHole x (wt-different-fill eps D1 D2 D3 D4) x\u2081\n wt-different-fill (FHCast eps) (TACast D1 x) D2 D3 (FHCast D4) = TACast (wt-different-fill eps D1 D2 D3 D4) x\n wt-different-fill (FHFailedCast x) (TAFailedCast y x\u2081 x\u2082 x\u2083) D3 D4 (FHFailedCast eps) = TAFailedCast (wt-different-fill x y D3 D4 eps) x\u2081 x\u2082 x\u2083\n\n -- if a well typed term results from filling the hole in \u03b5, then the term\n -- that filled the hole is also well typed\n wt-filling : \u2200{ \u03b5 \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u03a3[ \u03c4' \u2208 htyp ] (\u0394 , \u0393 \u22a2 d' :: \u03c4')\n wt-filling TAConst FHOuter = _ , TAConst\n wt-filling (TAVar x\u2081) FHOuter = _ , TAVar x\u2081\n wt-filling (TALam f ta) FHOuter = _ , TALam f ta\n\n wt-filling (TAAp ta ta\u2081) FHOuter = _ , TAAp ta ta\u2081\n wt-filling (TAAp ta ta\u2081) (FHAp1 eps) = wt-filling ta eps\n wt-filling (TAAp ta ta\u2081) (FHAp2 eps) = wt-filling ta\u2081 eps\n\n wt-filling (TAEHole x x\u2081) FHOuter = _ , TAEHole x x\u2081\n wt-filling (TANEHole x ta x\u2081) FHOuter = _ , TANEHole x ta x\u2081\n wt-filling (TANEHole x ta x\u2081) (FHNEHole eps) = wt-filling ta eps\n wt-filling (TACast ta x) FHOuter = _ , TACast ta x\n wt-filling (TACast ta x) (FHCast eps) = wt-filling ta eps\n wt-filling (TAFailedCast x y z w) FHOuter = _ , TAFailedCast x y z w\n wt-filling (TAFailedCast x x\u2081 x\u2082 x\u2083) (FHFailedCast y) = wt-filling x y\n\n -- instruction transitions preserve type\n preserve-trans : \u2200{ \u0394 \u0393 d \u03c4 d' } \u2192\n binders-unique d \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u2192> d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n preserve-trans bd TAConst ()\n preserve-trans bd (TAVar x\u2081) ()\n preserve-trans bd (TALam _ ta) ()\n preserve-trans (BUAp (BULam bd x\u2081) bd\u2081 (BDLam x\u2082 x\u2083)) (TAAp (TALam apt ta) ta\u2081) ITLam = lem-subst apt x\u2082 bd\u2081 ta ta\u2081\n preserve-trans bd (TAAp (TACast ta TCRefl) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 TCRefl)) TCRefl\n preserve-trans bd (TAAp (TACast ta (TCArr x x\u2081)) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 (~sym x))) x\u2081\n preserve-trans bd (TAEHole x x\u2081) ()\n preserve-trans bd (TANEHole x ta x\u2081) ()\n preserve-trans bd (TACast ta x) (ITCastID) = ta\n preserve-trans bd (TACast (TACast ta x) x\u2081) (ITCastSucceed x\u2082) = ta\n preserve-trans bd (TACast ta x) (ITGround (MGArr x\u2081)) = TACast (TACast ta (TCArr TCHole1 TCHole1)) TCHole1\n preserve-trans bd (TACast ta TCHole2) (ITExpand (MGArr x\u2081)) = TACast (TACast ta TCHole2) (TCArr TCHole2 TCHole2)\n preserve-trans bd (TACast (TACast ta x) x\u2081) (ITCastFail w y z) = TAFailedCast ta w y z\n preserve-trans bd (TAFailedCast x y z q) ()\n\n lem-bd-\u03b51 : \u2200{ d \u03b5 d0} \u2192 d == \u03b5 \u27e6 d0 \u27e7 \u2192 binders-unique d \u2192 binders-unique d0\n lem-bd-\u03b51 FHOuter bd = bd\n lem-bd-\u03b51 (FHAp1 eps) (BUAp bd bd\u2081 x) = lem-bd-\u03b51 eps bd\n lem-bd-\u03b51 (FHAp2 eps) (BUAp bd bd\u2081 x) = lem-bd-\u03b51 eps bd\u2081\n lem-bd-\u03b51 (FHNEHole eps) (BUNEHole bd x) = lem-bd-\u03b51 eps bd\n lem-bd-\u03b51 (FHCast eps) (BUCast bd) = lem-bd-\u03b51 eps bd\n lem-bd-\u03b51 (FHFailedCast eps) (BUFailedCast bd) = lem-bd-\u03b51 eps bd\n\n -- this is the main preservation theorem, gluing together the above\n preservation : {\u0394 : hctx} {d d' : ihexp} {\u03c4 : htyp} {\u0393 : tctx} \u2192\n binders-unique d \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u21a6 d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n preservation bd D (Step x x\u2081 x\u2082)\n with wt-filling D x\n ... | (_ , wt) = wt-different-fill x D wt (preserve-trans (lem-bd-\u03b51 x bd) wt x\u2081) x\u2082\n\n -- note that the exact statement of preservation in the paper, where \u0393 is\n -- empty indicating that the terms are closed, is an immediate corrolary\n -- of the slightly more general statement above.\n preservation' : {\u0394 : hctx} {d d' : ihexp} {\u03c4 : htyp} \u2192\n binders-unique d \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d \u21a6 d' \u2192\n \u0394 , \u2205 \u22a2 d' :: \u03c4\n preservation' = preservation\n","old_contents":"open import Nat\nopen import Prelude\nopen import core\nopen import contexts\n\nopen import lemmas-consistency\nopen import type-assignment-unicity\nopen import binders-disjoint-checks\n\nopen import lemmas-subst-ta\n\nmodule preservation where\n -- if d and d' both result from filling the hole in \u03b5 with terms of the\n -- same type, they too have the same type.\n wt-different-fill : \u2200{ \u0394 \u0393 d \u03b5 d1 d2 d' \u03c4 \u03c41 } \u2192\n d == \u03b5 \u27e6 d1 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n d' == \u03b5 \u27e6 d2 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n wt-different-fill FHOuter D1 D2 D3 FHOuter\n with type-assignment-unicity D1 D2\n ... | refl = D3\n wt-different-fill (FHAp1 eps) (TAAp D1 D2) D3 D4 (FHAp1 D5) = TAAp (wt-different-fill eps D1 D3 D4 D5) D2\n wt-different-fill (FHAp2 eps) (TAAp D1 D2) D3 D4 (FHAp2 D5) = TAAp D1 (wt-different-fill eps D2 D3 D4 D5)\n wt-different-fill (FHNEHole eps) (TANEHole x D1 x\u2081) D2 D3 (FHNEHole D4) = TANEHole x (wt-different-fill eps D1 D2 D3 D4) x\u2081\n wt-different-fill (FHCast eps) (TACast D1 x) D2 D3 (FHCast D4) = TACast (wt-different-fill eps D1 D2 D3 D4) x\n wt-different-fill (FHFailedCast x) (TAFailedCast y x\u2081 x\u2082 x\u2083) D3 D4 (FHFailedCast eps) = TAFailedCast (wt-different-fill x y D3 D4 eps) x\u2081 x\u2082 x\u2083\n\n -- if a well typed term results from filling the hole in \u03b5, then the term\n -- that filled the hole is also well typed\n wt-filling : \u2200{ \u03b5 \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u03a3[ \u03c4' \u2208 htyp ] (\u0394 , \u0393 \u22a2 d' :: \u03c4')\n wt-filling TAConst FHOuter = _ , TAConst\n wt-filling (TAVar x\u2081) FHOuter = _ , TAVar x\u2081\n wt-filling (TALam f ta) FHOuter = _ , TALam f ta\n\n wt-filling (TAAp ta ta\u2081) FHOuter = _ , TAAp ta ta\u2081\n wt-filling (TAAp ta ta\u2081) (FHAp1 eps) = wt-filling ta eps\n wt-filling (TAAp ta ta\u2081) (FHAp2 eps) = wt-filling ta\u2081 eps\n\n wt-filling (TAEHole x x\u2081) FHOuter = _ , TAEHole x x\u2081\n wt-filling (TANEHole x ta x\u2081) FHOuter = _ , TANEHole x ta x\u2081\n wt-filling (TANEHole x ta x\u2081) (FHNEHole eps) = wt-filling ta eps\n wt-filling (TACast ta x) FHOuter = _ , TACast ta x\n wt-filling (TACast ta x) (FHCast eps) = wt-filling ta eps\n wt-filling (TAFailedCast x y z w) FHOuter = _ , TAFailedCast x y z w\n wt-filling (TAFailedCast x x\u2081 x\u2082 x\u2083) (FHFailedCast y) = wt-filling x y\n\n -- instruction transitions preserve type\n preserve-trans : \u2200{ \u0394 \u0393 d \u03c4 d' } \u2192\n binders-unique d \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u2192> d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n preserve-trans bd TAConst ()\n preserve-trans bd (TAVar x\u2081) ()\n preserve-trans bd (TALam _ ta) ()\n preserve-trans (BUAp (BULam bd x\u2081) bd\u2081 (BDLam x\u2082 x\u2083)) (TAAp (TALam apt ta) ta\u2081) ITLam = lem-subst apt x\u2082 bd\u2081 ta ta\u2081\n preserve-trans bd (TAAp (TACast ta TCRefl) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 TCRefl)) TCRefl\n preserve-trans bd (TAAp (TACast ta (TCArr x x\u2081)) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 (~sym x))) x\u2081\n preserve-trans bd (TAEHole x x\u2081) ()\n preserve-trans bd (TANEHole x ta x\u2081) ()\n preserve-trans bd (TACast ta x) (ITCastID) = ta\n preserve-trans bd (TACast (TACast ta x) x\u2081) (ITCastSucceed x\u2082) = ta\n preserve-trans bd (TACast ta x) (ITGround (MGArr x\u2081)) = TACast (TACast ta (TCArr TCHole1 TCHole1)) TCHole1\n preserve-trans bd (TACast ta TCHole2) (ITExpand (MGArr x\u2081)) = TACast (TACast ta TCHole2) (TCArr TCHole2 TCHole2)\n preserve-trans bd (TACast (TACast ta x) x\u2081) (ITCastFail w y z) = TAFailedCast ta w y z\n preserve-trans bd (TAFailedCast x y z q) ()\n\n lem-bd-\u03b51 : \u2200{ d \u03b5 d0} \u2192 d == \u03b5 \u27e6 d0 \u27e7 \u2192 binders-unique d \u2192 binders-unique d0\n lem-bd-\u03b51 FHOuter bd = bd\n lem-bd-\u03b51 (FHAp1 eps) (BUAp bd bd\u2081 x) = lem-bd-\u03b51 eps bd\n lem-bd-\u03b51 (FHAp2 eps) (BUAp bd bd\u2081 x) = lem-bd-\u03b51 eps bd\u2081\n lem-bd-\u03b51 (FHNEHole eps) (BUNEHole bd x) = lem-bd-\u03b51 eps bd\n lem-bd-\u03b51 (FHCast eps) (BUCast bd) = lem-bd-\u03b51 eps bd\n lem-bd-\u03b51 (FHFailedCast eps) (BUFailedCast bd) = lem-bd-\u03b51 eps bd\n\n -- this is the main preservation theorem, gluing together the above\n preservation : {\u0394 : hctx} {d d' : ihexp} {\u03c4 : htyp} {\u0393 : tctx} \u2192\n binders-unique d \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u21a6 d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n preservation bd D (Step x x\u2081 x\u2082)\n with wt-filling D x\n ... | (_ , wt) = wt-different-fill x D wt (preserve-trans (lem-bd-\u03b51 x bd) wt x\u2081) x\u2082\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d864c1b18db6945f3011ab052b5aa8b980202dd8","subject":"Still a few holes","message":"Still a few holes\n","repos":"crypto-agda\/crypto-agda","old_file":"sum.agda","new_file":"sum.agda","new_contents":"import Level as L\nopen L using (Lift; lift)\nopen import Type hiding (\u2605)\nopen import Function.NP\nopen import Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat.NP hiding (_^_)\nopen import Data.Nat.Properties\nopen import Data.Unit hiding (_\u2264_)\nopen import Data.Sum\nopen import Data.Maybe.NP\nopen import Data.Product\nopen import Data.Bits\nopen import Data.Bool.NP as Bool\nopen import Data.Fin using (Fin)\nopen import Function.NP\nopen import Function.Equality using (_\u27e8$\u27e9_)\nimport Function.Inverse.NP as FI\nopen FI using (_\u2194_; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nimport Function.Related as FR\nopen import Function.Related.TypeIsomorphisms.NP\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_)\nopen import Search.Type\nopen import Search.Searchable renaming (Searchable to Searchable)\nopen import Search.Searchable.Product\nopen import Search.Searchable.Sum\nopen import Search.Derived\n\nmodule sum where\n\n_\u2248Sum_ : \u2200 {A} \u2192 (sum\u2080 sum\u2081 : Sum A) \u2192 \u2605 _\nsum\u2080 \u2248Sum sum\u2081 = \u2200 f \u2192 sum\u2080 f \u2261 sum\u2081 f\n\n_\u2248Search_ : \u2200 {A} \u2192 (s\u2080 s\u2081 : Search _ A) \u2192 \u2605\u2081\ns\u2080 \u2248Search s\u2081 = \u2200 {B} (op : Op\u2082 B) f \u2192 s\u2080 op f \u2261 s\u2081 op f\n\n\u03bc-iso : \u2200 {A B} \u2192 (A \u2194 B) \u2192 Searchable A \u2192 Searchable B\n\u03bc-iso {A}{B} A\u2194B \u03bcA = mk search\u1d2e ind ade\n where\n A\u2192B = to A\u2194B\n\n search\u1d2e : Search _ B\n search\u1d2e m f = search \u03bcA m (f \u2218 A\u2192B)\n\n sum\u1d2e = search\u1d2e _+_\n\n ind : SearchInd _ search\u1d2e\n ind P P\u2219 Pf = search-ind \u03bcA (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 A\u2192B))) P\u2219 (Pf \u2218 A\u2192B)\n\n ade : AdequateSum sum\u1d2e\n ade f = sym-first-iso A\u2194B FI.\u2218 adequate-sum \u03bcA (f \u2218 A\u2192B)\n\n-- I guess this could be more general\n\u03bc-iso-preserve : \u2200 {A B} (A\u2194B : A \u2194 B) f (\u03bcA : Searchable A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-iso A\u2194B \u03bcA) (f \u2218 from A\u2194B)\n\u03bc-iso-preserve A\u2194B f \u03bcA = sum-ext \u03bcA (\u2261.cong f \u2218 \u2261.sym \u2218 Inverse.left-inverse-of A\u2194B)\n\n\u03bcLift : \u2200 {A} \u2192 Searchable A \u2192 Searchable (Lift A)\n\u03bcLift = \u03bc-iso (FI.sym Lift\u2194id)\n\n\u03bc\u22a4 : Searchable \u22a4\n\u03bc\u22a4 = mk _ ind ade\n where\n srch : Search _ \u22a4\n srch _ f = f _\n\n ind : SearchInd _ srch\n ind _ _ Pf = Pf _\n\n ade : AdequateSum (srch _+_)\n ade x = FI.sym \u22a4\u00d7A\u2194A\n\nsearchBit : \u2200 {m} \u2192 Search m Bit\nsearchBit _\u2219_ f = f 0b \u2219 f 1b\n\nsearchBit-ind : \u2200 {m p} \u2192 SearchInd p {m} searchBit\nsearchBit-ind _ _P\u2219_ Pf = Pf 0b P\u2219 Pf 1b\n\n\u03bcBit : Searchable Bit\n\u03bcBit = \u03bc-iso (FI.sym Bit\u2194\u22a4\u228e\u22a4) (\u03bc\u22a4 \u228e-\u03bc \u03bc\u22a4)\n\nfocusBit : \u2200 {a} \u2192 Focus {a} searchBit\nfocusBit (false , x) = inj\u2081 x\nfocusBit (true , x) = inj\u2082 x\n\nfocusedBit : Focused {L.zero} searchBit\nfocusedBit {B} = inverses focusBit unfocus (\u21d2) (\u21d0)\n where open Searchable\u2081\u2081 searchBit-ind\n \u21d2 : (x : \u03a3 Bit B) \u2192 _\n \u21d2 (false , x) = \u2261.refl\n \u21d2 (true , x) = \u2261.refl\n \u21d0 : (x : B 0b \u228e B 1b) \u2192 _\n \u21d0 (inj\u2081 x) = \u2261.refl\n \u21d0 (inj\u2082 x) = \u2261.refl\n\nlookupBit : \u2200 {a} \u2192 Lookup {a} searchBit\nlookupBit = proj\n\n_\u228e'_ : \u2605\u2080 \u2192 \u2605\u2080 \u2192 \u2605\u2080\nA \u228e' B = \u03a3 Bool (cond A B)\n\n_\u03bc\u228e'_ : \u2200 {A B} \u2192 Searchable A \u2192 Searchable B \u2192 Searchable (A \u228e' B)\n\u03bcA \u03bc\u228e' \u03bcB = \u03bc\u03a3 \u03bcBit (\u03bb { {true} \u2192 \u03bcA ; {false} \u2192 \u03bcB })\n\nprivate -- unused\n S\u03a0\u03a3\u207b : \u2200 {m A} {B : A \u2192 \u2605 _} {C : \u03a3 A B \u2192 \u2605 _}\n \u2192 Search m ((x : A) (y : B x) \u2192 C (x , y))\n \u2192 Search m (\u03a0 (\u03a3 A B) C)\n S\u03a0\u03a3\u207b s _\u2219_ f = s _\u2219_ (f \u2218 uncurry)\n\n S\u03a0\u03a3\u207b-ind : \u2200 {m p A} {B : A \u2192 \u2605 _} {C : \u03a3 A B \u2192 \u2605 _}\n \u2192 {s : Search m ((x : A) (y : B x) \u2192 C (x , y))}\n \u2192 SearchInd p s\n \u2192 SearchInd p (S\u03a0\u03a3\u207b s)\n S\u03a0\u03a3\u207b-ind ind P P\u2219 Pf = ind (P \u2218 S\u03a0\u03a3\u207b) P\u2219 (Pf \u2218 uncurry)\n\n S\u00d7\u207b : \u2200 {m A B C} \u2192 Search m (A \u2192 B \u2192 C) \u2192 Search m (A \u00d7 B \u2192 C)\n S\u00d7\u207b = S\u03a0\u03a3\u207b\n\n S\u00d7\u207b-ind : \u2200 {m p A B C}\n \u2192 {s : Search m (A \u2192 B \u2192 C)}\n \u2192 SearchInd p s\n \u2192 SearchInd p (S\u00d7\u207b s)\n S\u00d7\u207b-ind = S\u03a0\u03a3\u207b-ind\n\n S\u03a0\u228e\u207b : \u2200 {m A B} {C : A \u228e B \u2192 \u2605 _}\n \u2192 Search m (\u03a0 A (C \u2218 inj\u2081) \u00d7 \u03a0 B (C \u2218 inj\u2082))\n \u2192 Search m (\u03a0 (A \u228e B) C)\n S\u03a0\u228e\u207b s _\u2219_ f = s _\u2219_ (f \u2218 uncurry [_,_])\n\n S\u03a0\u228e\u207b-ind : \u2200 {m p A B} {C : A \u228e B \u2192 \u2605 _}\n {s : Search m (\u03a0 A (C \u2218 inj\u2081) \u00d7 \u03a0 B (C \u2218 inj\u2082))}\n (i : SearchInd p s)\n \u2192 SearchInd p (S\u03a0\u228e\u207b {C = C} s) -- A sB)\n S\u03a0\u228e\u207b-ind i P P\u2219 Pf = i (P \u2218 S\u03a0\u228e\u207b) P\u2219 (Pf \u2218 uncurry [_,_])\n\n {- For each A\u2192C function\n and each B\u2192C function\n an A\u228eB\u2192C function is yield\n -}\n S\u228e\u207b : \u2200 {m A B C} \u2192 Search m (A \u2192 C) \u2192 Search m (B \u2192 C)\n \u2192 Search m (A \u228e B \u2192 C)\n S\u228e\u207b sA sB = S\u03a0\u228e\u207b (sA \u00d7-search sB)\n\n\u03bc\u03a0\u03a3\u207b : \u2200 {A B}{C : \u03a3 A B \u2192 \u2605\u2080} \u2192 Searchable ((x : A)(y : B x) \u2192 C (x , y)) \u2192 Searchable (\u03a0 (\u03a3 A B) C)\n\u03bc\u03a0\u03a3\u207b = \u03bc-iso (FI.sym curried)\n\n\u03a3-Fun : \u2200 {A B} \u2192 Funable A \u2192 Funable B \u2192 Funable (A \u00d7 B)\n\u03a3-Fun (\u03bcA , \u03bcA\u2192) FB = \u03bc\u03a3 \u03bcA (searchable FB) , (\u03bb x \u2192 \u03bc\u03a0\u03a3\u207b (\u03bcA\u2192 (negative FB x)))\n where open Funable\n\n\u03bc\u03a0\u228e\u207b : \u2200 {A B}{C : A \u228e B \u2192 \u2605 _} \u2192 Searchable (\u03a0 A (C \u2218 inj\u2081) \u00d7 \u03a0 B (C \u2218 inj\u2082))\n \u2192 Searchable (\u03a0 (A \u228e B) C)\n\u03bc\u03a0\u228e\u207b = \u03bc-iso {!!}\n\n_\u228e-Fun_ : \u2200 {A B} \u2192 Funable A \u2192 Funable B \u2192 Funable (A \u228e B)\n_\u228e-Fun_ (\u03bcA , \u03bcA\u2192) (\u03bcB , \u03bcB\u2192) = (\u03bcA \u228e-\u03bc \u03bcB) , (\u03bb X \u2192 \u03bc\u03a0\u228e\u207b (\u03bcA\u2192 X \u00d7-\u03bc \u03bcB\u2192 X))\n\nS\u22a4 : \u2200 {m A} \u2192 Search m A \u2192 Search m (\u22a4 \u2192 A)\nS\u22a4 sA _\u2219_ f = sA _\u2219_ (f \u2218 const)\n\nS\u03a0Bit : \u2200 {m A} \u2192 Search m (A 0b) \u2192 Search m (A 1b)\n \u2192 Search m (\u03a0 Bit A)\nS\u03a0Bit sA\u2080 sA\u2081 _\u2219_ f = sA\u2080 _\u2219_ \u03bb x \u2192 sA\u2081 _\u2219_ \u03bb y \u2192 f \u03bb {true \u2192 y; false \u2192 x}\n\nsum-const : \u2200 {A} (\u03bcA : Searchable A) \u2192 \u2200 k \u2192 sum \u03bcA (const k) \u2261 Card \u03bcA * k\nsum-const \u03bcA k\n rewrite \u2115\u00b0.*-comm (Card \u03bcA) k\n | \u2261.sym (sum-lin \u03bcA (const 1) k)\n | proj\u2082 \u2115\u00b0.*-identity k = \u2261.refl\n\ninfixr 4 _\u00d7Sum-proj\u2081_ _\u00d7Sum-proj\u2081'_ _\u00d7Sum-proj\u2082_ _\u00d7Sum-proj\u2082'_\n\n_\u00d7Sum-proj\u2081_ : \u2200 {A B}\n (\u03bcA : Searchable A)\n (\u03bcB : Searchable B)\n f \u2192\n sum (\u03bcA \u00d7-\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 Card \u03bcB * sum \u03bcA f\n(\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n rewrite sum-ext \u03bcA (sum-const \u03bcB \u2218 f)\n = sum-lin \u03bcA f (Card \u03bcB)\n\n_\u00d7Sum-proj\u2082_ : \u2200 {A B}\n (\u03bcA : Searchable A)\n (\u03bcB : Searchable B)\n f \u2192\n sum (\u03bcA \u00d7-\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 Card \u03bcA * sum \u03bcB f\n(\u03bcA \u00d7Sum-proj\u2082 \u03bcB) f = sum-const \u03bcA (sum \u03bcB f)\n\n_\u00d7Sum-proj\u2081'_ : \u2200 {A B}\n (\u03bcA : Searchable A) (\u03bcB : Searchable B)\n {f} {g} \u2192\n sum \u03bcA f \u2261 sum \u03bcA g \u2192\n sum (\u03bcA \u00d7-\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 sum (\u03bcA \u00d7-\u03bc \u03bcB) (g \u2218 proj\u2081)\n(\u03bcA \u00d7Sum-proj\u2081' \u03bcB) {f} {g} sumf\u2261sumg\n rewrite (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n | (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) g\n | sumf\u2261sumg = \u2261.refl\n\n_\u00d7Sum-proj\u2082'_ : \u2200 {A B}\n (\u03bcA : Searchable A) (\u03bcB : Searchable B)\n {f} {g} \u2192\n sum \u03bcB f \u2261 sum \u03bcB g \u2192\n sum (\u03bcA \u00d7-\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 sum (\u03bcA \u00d7-\u03bc \u03bcB) (g \u2218 proj\u2082)\n(\u03bcA \u00d7Sum-proj\u2082' \u03bcB) sumf\u2261sumg = sum-ext \u03bcA (const sumf\u2261sumg)\n\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Vec.NP as Vec using (Vec; tabulate; _++_) renaming (map to vmap; sum to vsum; foldr to vfoldr; foldr\u2081 to vfoldr\u2081)\n\nvmsum : \u2200 {c \u2113} (m : Monoid c \u2113) {n} \u2192\n let open Mon m in\n Vec C n \u2192 C\nvmsum m = vfoldr _ _\u2219_ \u03b5\n where open Mon m\n\nvsgsum : \u2200 {c \u2113} (sg : Semigroup c \u2113) {n} \u2192\n let open Sgrp sg in\n Vec C (suc n) \u2192 C\nvsgsum sg = vfoldr\u2081 _\u2219_\n where open Sgrp sg\n\n-- let's recall that: tabulate f \u2257 vmap f (allFin n)\n\n-- searchMonFin : \u2200 n \u2192 SearchMon (Fin n)\n-- searchMonFin n m f = vmsum m (tabulate f)\n\nsearchFinSuc : \u2200 {m} n \u2192 Search m (Fin (suc n))\nsearchFinSuc n _\u2219_ f = vfoldr\u2081 _\u2219_ (tabulate f)\n\n\u03bcMaybe : \u2200 {A} \u2192 Searchable A \u2192 Searchable (Maybe A)\n\u03bcMaybe \u03bcA = \u03bc-iso (FI.sym Maybe\u2194\u22a4\u228e) (\u03bc\u22a4 \u228e-\u03bc \u03bcA)\n\n\u03bcMaybe^ : \u2200 {A} n \u2192 Searchable A \u2192 Searchable (Maybe^ n A)\n\u03bcMaybe^ zero \u03bcA = \u03bcA\n\u03bcMaybe^ (suc n) \u03bcA = \u03bcMaybe (\u03bcMaybe^ n \u03bcA)\n\n{-\n\u03bcFinSuc : \u2200 n \u2192 Searchable (Fin (suc n))\n\u03bcFinSuc n = mk _ (ind n) {!!}\n where ind : \u2200 n \u2192 SearchInd _ (searchFinSuc n)\n ind zero P P\u2219 Pf = Pf zero\n ind (suc n) P P\u2219 Pf = P\u2219 (Pf zero) (ind n (\u03bb s \u2192 P (\u03bb op f \u2192 s op (f \u2218 suc))) P\u2219 (Pf \u2218 suc))\n-}\n\n\u03bcFinSuc : \u2200 n \u2192 Searchable (Fin (suc n))\n\u03bcFinSuc n = \u03bc-iso (Maybe^\u22a4\u2194Fin1+ n) (\u03bcMaybe^ n \u03bc\u22a4)\n\n\u03bc^ : \u2200 {A} (\u03bcA : Searchable A) n \u2192 Searchable (A ^ n)\n\u03bc^ \u03bcA zero = \u03bcLift \u03bc\u22a4\n\u03bc^ \u03bcA (suc n) = \u03bcA \u00d7-\u03bc \u03bc^ \u03bcA n\n\n\u03bcVec : \u2200 {A} (\u03bcA : Searchable A) n \u2192 Searchable (Vec A n)\n\u03bcVec \u03bcA n = \u03bc-iso (^\u2194Vec n) (\u03bc^ \u03bcA n)\n\nsearchVec : \u2200 {m A} n \u2192 Search m A \u2192 Search m (Vec A n)\nsearchVec zero search\u1d2c op f = f []\nsearchVec (suc n) search\u1d2c op f = search\u1d2c op (\u03bb x \u2192 searchVec n search\u1d2c op (f \u2218 _\u2237_ x))\n\nsearchVec-spec : \u2200 {A} (\u03bcA : Searchable A) n \u2192 searchVec n (search \u03bcA) \u2248Search search (\u03bcVec \u03bcA n)\nsearchVec-spec \u03bcA zero op f = \u2261.refl\nsearchVec-spec \u03bcA (suc n) op f = search-ext \u03bcA op (\u03bb x \u2192 searchVec-spec \u03bcA n op (f \u2218 _\u2237_ x))\n\nsearchVec-++ : \u2200 {A} n {m} (\u03bcA : Searchable A) sg\n \u2192 let open Sgrp sg in\n (f : Vec A (n + m) \u2192 C)\n \u2192 search (\u03bcVec \u03bcA (n + m)) _\u2219_ f\n \u2248 search (\u03bcVec \u03bcA n) _\u2219_ (\u03bb xs \u2192\n search (\u03bcVec \u03bcA m) _\u2219_ (\u03bb ys \u2192\n f (xs ++ ys)))\nsearchVec-++ zero \u03bcA sg f = Sgrp.refl sg\nsearchVec-++ (suc n) \u03bcA sg f = search-sg-ext \u03bcA sg (\u03bb x \u2192\n searchVec-++ n \u03bcA sg (f \u2218 _\u2237_ x))\n\nsumVec-swap : \u2200 {A} n {m} (\u03bcA : Searchable A)(f : Vec A (n + m) \u2192 \u2115)\n \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 f (xs ++ ys)))\nsumVec-swap n {m} \u03bcA f = sum-swap (\u03bcVec \u03bcA n) (\u03bcVec \u03bcA m) (\u03bb xs ys \u2192 f (xs ++ ys))\n\nswapS : \u2200 {A B} \u2192 Searchable (A \u00d7 B) \u2192 Searchable (B \u00d7 A)\nswapS = \u03bc-iso swap-iso\n\nswapS-preserve : \u2200 {A B} f (\u03bcA\u00d7B : Searchable (A \u00d7 B)) \u2192 sum \u03bcA\u00d7B f \u2261 sum (swapS \u03bcA\u00d7B) (f \u2218 swap)\nswapS-preserve = \u03bc-iso-preserve swap-iso\n\nmodule _ {A : Set}(\u03bcA : Searchable A) where\n\n private\n sA = search \u03bcA\n\n extend : \u2200 {n} \u2192 A \u2192 (Fin n \u2192 A) \u2192 Fin (suc n) \u2192 A\n extend x g zero = x\n extend x g (suc i) = g i\n\n abs : Fin 0 \u2192 A\n abs ()\n\n -- There is one function Fin 0 \u2192 A (called abs) so this should be fine\n -- if not there is a version below that forces the domain to be non-empty\n sFun : \u2200 n \u2192 Search _ (Fin n \u2192 A)\n sFun zero op f = f abs\n sFun (suc n) op f = sA op (\u03bb x \u2192 sFun n op (f \u2218 extend x))\n\n ind : \u2200 n \u2192 SearchInd _ (sFun n)\n ind zero P P\u2219 Pf = Pf abs\n ind (suc n) P P\u2219 Pf =\n search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (\u03bb x \u2192 sFun n op (f \u2218 extend x))))\n P\u2219\n (\u03bb x \u2192 ind n (\u03bb sf \u2192 P (\u03bb op f \u2192 sf op (f \u2218 extend x)))\n P\u2219 (Pf \u2218 extend x))\n\n \u03bcFun : \u2200 {n} \u2192 Searchable (Fin n \u2192 A)\n \u03bcFun = mk _ (ind _) {!!}\n\nmodule BigDistr\n {A}(\u03bcA : Searchable A)\n (cm : CommutativeMonoid L.zero L.zero)\n -- we want (open CMon cm) !!!\n (_\u25ce_ : let open CMon cm in C \u2192 C \u2192 C)\n (distrib : let open CMon cm in _DistributesOver_ _\u2248_ _\u25ce_ _\u2219_)\n (_\u25ce-cong_ : let open CMon cm in _\u25ce_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_) where\n\n open CMon cm\n\n \u03bcF\u2192A = \u03bcFun \u03bcA\n\n -- Sum over A\n \u03a3\u1d2c = search \u03bcA _\u2219_\n\n -- Sum over (Fin(1+I)\u2192A) functions\n \u03a3' : \u2200 {I} \u2192 ((Fin (suc I) \u2192 A) \u2192 C) \u2192 C\n \u03a3' = search \u03bcF\u2192A _\u2219_\n\n -- Product over Fin(1+I) values\n \u03a0' = \u03bb I \u2192 search (\u03bcFinSuc I) _\u25ce_\n\n bigDistr : \u2200 I F \u2192 \u03a0' I (\u03a3\u1d2c \u2218 F) \u2248 \u03a3' (\u03a0' I \u2218 _\u02e2_ F)\n bigDistr zero _ = refl\n bigDistr (suc I) F\n = \u03a3\u1d2c (F zero) \u25ce \u03a0' I (\u03a3\u1d2c \u2218 F \u2218 suc)\n \u2248\u27e8 refl \u25ce-cong bigDistr I (F \u2218 suc) \u27e9\n \u03a3\u1d2c (F zero) \u25ce \u03a3' (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc))\n \u2248\u27e8 sym (search-lin\u02b3 \u03bcA monoid _\u25ce_ (F zero) (\u03a3' (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc))) (proj\u2082 distrib)) \u27e9\n \u03a3\u1d2c (\u03bb j \u2192 F zero j \u25ce \u03a3' (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc)))\n \u2248\u27e8 search-sg-ext \u03bcA semigroup (\u03bb j \u2192 sym (search-lin\u02e1 \u03bcF\u2192A monoid _\u25ce_ (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc)) (F zero j) (proj\u2081 distrib))) \u27e9\n (\u03a3\u1d2c \u03bb j \u2192 \u03a3' \u03bb f \u2192 F zero j \u25ce \u03a0' I ((F \u2218 suc) \u02e2 f))\n \u220e\n\nFinDist : \u2200 {n} \u2192 DistFun (\u03bcFinSuc n) (\u03bb \u03bcX \u2192 \u03bcFun \u03bcX)\nFinDist \u03bcB c \u25ce distrib \u25ce-cong f = BigDistr.bigDistr \u03bcB c \u25ce distrib \u25ce-cong _ f\n\nsimple : \u2200 {A : Set}{P : A \u2192 A \u2192 Set} \u2192 (\u2200 x \u2192 P x x) \u2192 {x y : A} \u2192 x \u2261 y \u2192 P x y\nsimple r \u2261.refl = r _\n\n\u00d7-Dist : \u2200 {A B} FA FB \u2192 DistFunable {A} FA \u2192 DistFunable {B} FB \u2192 DistFunable (\u03a3-Fun FA FB)\n\u00d7-Dist FA FB FA-dist FB-dist \u03bcX c _\u2299_ distrib _\u2299-cong_ f\n = \u03a0\u1d2c (\u03bb x \u2192 \u03a0\u1d2e (\u03bb y \u2192 \u03a3' (f (x , y))))\n \u2248\u27e8 \u27e6search\u27e7 (searchable FA){_\u2261_} \u2261.refl _\u2248_ (\u03bb x y \u2192 x \u2299-cong y)\n (\u03bb { {x} {.x} \u2261.refl \u2192 FB-dist \u03bcX c _\u2299_ distrib _\u2299-cong_ (curry f x)}) \u27e9\n \u03a0\u1d2c (\u03bb x \u2192 \u03a3\u1d2e (\u03bb fb \u2192 \u03a0\u1d2e (\u03bb y \u2192 f (x , y) (fb y))))\n \u2248\u27e8 FA-dist (negative FB \u03bcX) c _\u2299_ distrib _\u2299-cong_\n (\u03bb x fb \u2192 search (searchable FB) _\u2299_ (\u03bb y \u2192 f (x , y) (fb y))) \u27e9\n \u03a3\u1d2c\u1d2e (\u03bb fab \u2192 \u03a0\u1d2c (\u03bb x \u2192 \u03a0\u1d2e (\u03bb y \u2192 f (x , y) (fab x y))))\n \u220e\n where\n open CMon c\n open Funable\n\n \u03a3' = search \u03bcX _\u2219_\n\n \u03a0\u1d2c = search (searchable FA) _\u2299_\n \u03a0\u1d2e = search (searchable FB) _\u2299_\n\n \u03a3\u1d2c\u1d2e = search (negative FA (negative FB \u03bcX)) _\u2219_\n \u03a3\u1d2e = search (negative FB \u03bcX) _\u2219_\n\n\u228e-Dist : \u2200 {A B} FA FB \u2192 DistFunable {A} FA \u2192 DistFunable {B} FB \u2192 DistFunable (FA \u228e-Fun FB)\n\u228e-Dist FA FB FA-dist FB-dist \u03bcX c _\u25ce_ distrib _\u25ce-cong_ f\n = \u03a0\u1d2c (\u03a3' \u2218 f \u2218 inj\u2081) \u25ce \u03a0\u1d2e (\u03a3' \u2218 f \u2218 inj\u2082)\n \u2248\u27e8 FA-dist \u03bcX c _\u25ce_ distrib _\u25ce-cong_ (f \u2218 inj\u2081) \u25ce-cong FB-dist \u03bcX c _\u25ce_ distrib _\u25ce-cong_ (f \u2218 inj\u2082) \u27e9\n \u03a3\u1d2c (\u03bb fa \u2192 \u03a0\u1d2c (\u03bb i \u2192 f (inj\u2081 i) (fa i))) \u25ce \u03a3\u1d2e (\u03bb fb \u2192 \u03a0\u1d2e (\u03bb i \u2192 f (inj\u2082 i) (fb i)))\n \u2248\u27e8 sym (search-lin\u02b3 (negative FA \u03bcX) monoid _\u25ce_ _ _ (proj\u2082 distrib)) \u27e9\n \u03a3\u1d2c (\u03bb fa \u2192 \u03a0\u1d2c (\u03bb i \u2192 f (inj\u2081 i) (fa i)) \u25ce \u03a3\u1d2e (\u03bb fb \u2192 \u03a0\u1d2e (\u03bb i \u2192 f (inj\u2082 i) (fb i))))\n \u2248\u27e8 search-sg-ext (negative FA \u03bcX) semigroup (\u03bb fa \u2192 sym (search-lin\u02e1 (negative FB \u03bcX) monoid _\u25ce_ _ _ (proj\u2081 distrib))) \u27e9\n (\u03a3\u1d2c \u03bb fa \u2192 \u03a3\u1d2e \u03bb fb \u2192 \u03a0\u1d2c ((f \u2218 inj\u2081) \u02e2 fa) \u25ce \u03a0\u1d2e ((f \u2218 inj\u2082) \u02e2 fb))\n \u220e\n where\n open CMon c\n open Funable\n\n \u03a3' = search \u03bcX _\u2219_\n\n \u03a0\u1d2c = search (searchable FA) _\u25ce_\n \u03a0\u1d2e = search (searchable FB) _\u25ce_\n\n \u03a3\u1d2c = search (negative FA \u03bcX) _\u2219_\n \u03a3\u1d2e = search (negative FB \u03bcX) _\u2219_\n\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"import Level as L\nopen L using (Lift; lift)\nopen import Type hiding (\u2605)\nopen import Function.NP\nopen import Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat.NP hiding (_^_)\nopen import Data.Nat.Properties\nopen import Data.Unit hiding (_\u2264_)\nopen import Data.Sum\nopen import Data.Maybe.NP\nopen import Data.Product\nopen import Data.Bits\nopen import Data.Bool.NP as Bool\nopen import Data.Fin using (Fin)\nopen import Function.NP\nopen import Function.Equality using (_\u27e8$\u27e9_)\nimport Function.Inverse.NP as FI\nopen FI using (_\u2194_; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nimport Function.Related as FR\nopen import Function.Related.TypeIsomorphisms.NP\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_)\nopen import Search.Type\nopen import Search.Searchable renaming (Searchable to Searchable)\nopen import Search.Searchable.Product\nopen import Search.Searchable.Sum\nopen import Search.Derived\n\nmodule sum where\n\n_\u2248Sum_ : \u2200 {A} \u2192 (sum\u2080 sum\u2081 : Sum A) \u2192 \u2605 _\nsum\u2080 \u2248Sum sum\u2081 = \u2200 f \u2192 sum\u2080 f \u2261 sum\u2081 f\n\n_\u2248Search_ : \u2200 {A} \u2192 (s\u2080 s\u2081 : Search _ A) \u2192 \u2605\u2081\ns\u2080 \u2248Search s\u2081 = \u2200 {B} (op : Op\u2082 B) f \u2192 s\u2080 op f \u2261 s\u2081 op f\n\n\u03bc-iso : \u2200 {A B} \u2192 (A \u2194 B) \u2192 Searchable A \u2192 Searchable B\n\u03bc-iso {A}{B} A\u2194B \u03bcA = mk search\u1d2e ind ade\n where\n A\u2192B = to A\u2194B\n\n search\u1d2e : Search _ B\n search\u1d2e m f = search \u03bcA m (f \u2218 A\u2192B)\n\n sum\u1d2e = search\u1d2e _+_\n\n ind : SearchInd _ search\u1d2e\n ind P P\u2219 Pf = search-ind \u03bcA (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 A\u2192B))) P\u2219 (Pf \u2218 A\u2192B)\n\n ade : AdequateSum sum\u1d2e\n ade f = sym-first-iso A\u2194B FI.\u2218 adequate-sum \u03bcA (f \u2218 A\u2192B)\n\n-- I guess this could be more general\n\u03bc-iso-preserve : \u2200 {A B} (A\u2194B : A \u2194 B) f (\u03bcA : Searchable A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-iso A\u2194B \u03bcA) (f \u2218 from A\u2194B)\n\u03bc-iso-preserve A\u2194B f \u03bcA = sum-ext \u03bcA (\u2261.cong f \u2218 \u2261.sym \u2218 Inverse.left-inverse-of A\u2194B)\n\n\u03bcLift : \u2200 {A} \u2192 Searchable A \u2192 Searchable (Lift A)\n\u03bcLift = \u03bc-iso (FI.sym Lift\u2194id)\n\n\u03bc\u22a4 : Searchable \u22a4\n\u03bc\u22a4 = mk _ ind ade\n where\n srch : Search _ \u22a4\n srch _ f = f _\n\n ind : SearchInd _ srch\n ind _ _ Pf = Pf _\n\n ade : AdequateSum (srch _+_)\n ade x = FI.sym \u22a4\u00d7A\u2194A\n\nsearchBit : \u2200 {m} \u2192 Search m Bit\nsearchBit _\u2219_ f = f 0b \u2219 f 1b\n\nsearchBit-ind : \u2200 {m p} \u2192 SearchInd p {m} searchBit\nsearchBit-ind _ _P\u2219_ Pf = Pf 0b P\u2219 Pf 1b\n\n\u03bcBit : Searchable Bit\n\u03bcBit = \u03bc-iso (FI.sym Bit\u2194\u22a4\u228e\u22a4) (\u03bc\u22a4 \u228e-\u03bc \u03bc\u22a4)\n\nfocusBit : \u2200 {a} \u2192 Focus {a} searchBit\nfocusBit (false , x) = inj\u2081 x\nfocusBit (true , x) = inj\u2082 x\n\nfocusedBit : Focused {L.zero} searchBit\nfocusedBit {B} = inverses focusBit unfocus (\u21d2) (\u21d0)\n where open Searchable\u2081\u2081 searchBit-ind\n \u21d2 : (x : \u03a3 Bit B) \u2192 _\n \u21d2 (false , x) = \u2261.refl\n \u21d2 (true , x) = \u2261.refl\n \u21d0 : (x : B 0b \u228e B 1b) \u2192 _\n \u21d0 (inj\u2081 x) = \u2261.refl\n \u21d0 (inj\u2082 x) = \u2261.refl\n\nlookupBit : \u2200 {a} \u2192 Lookup {a} searchBit\nlookupBit = {!proj!}\n\n_\u228e'_ : \u2605\u2080 \u2192 \u2605\u2080 \u2192 \u2605\u2080\nA \u228e' B = \u03a3 Bool (cond A B)\n\n_\u03bc\u228e'_ : \u2200 {A B} \u2192 Searchable A \u2192 Searchable B \u2192 Searchable (A \u228e' B)\n\u03bcA \u03bc\u228e' \u03bcB = \u03bc\u03a3 \u03bcBit (\u03bb { {true} \u2192 \u03bcA ; {false} \u2192 \u03bcB })\n\nS\u03a0\u03a3\u207b : \u2200 {m A} {B : A \u2192 \u2605 _} {C : \u03a3 A B \u2192 \u2605 _}\n \u2192 Search m ((x : A) (y : B x) \u2192 C (x , y))\n \u2192 Search m (\u03a0 (\u03a3 A B) C)\nS\u03a0\u03a3\u207b s _\u2219_ f = s _\u2219_ (f \u2218 uncurry)\n\nS\u03a0\u03a3\u207b-ind : \u2200 {m p A} {B : A \u2192 \u2605 _} {C : \u03a3 A B \u2192 \u2605 _}\n \u2192 {s : Search m ((x : A) (y : B x) \u2192 C (x , y))}\n \u2192 SearchInd p s\n \u2192 SearchInd p (S\u03a0\u03a3\u207b s)\nS\u03a0\u03a3\u207b-ind ind P P\u2219 Pf = ind (P \u2218 S\u03a0\u03a3\u207b) P\u2219 (Pf \u2218 uncurry)\n\n\u03bc\u03a0\u03a3\u207b : \u2200 {A B}{C : \u03a3 A B \u2192 \u2605\u2080} \u2192 Searchable ((x : A)(y : B x) \u2192 C (x , y)) \u2192 Searchable ((p : \u03a3 A B) \u2192 C p)\n\u03bc\u03a0\u03a3\u207b \u03bc = mk (S\u03a0\u03a3\u207b (search \u03bc)) (S\u03a0\u03a3\u207b-ind (search-ind \u03bc)) {!!}\n\n\u03a3-Fun : \u2200 {A B} \u2192 Funable A \u2192 (Funable B) \u2192 Funable (A \u00d7 B)\n\u03a3-Fun (\u03bcA , \u03bcA\u2192) FB = \u03bc\u03a3 \u03bcA (searchable FB) , (\u03bb x \u2192 \u03bc\u03a0\u03a3\u207b (\u03bcA\u2192 (negative FB x)))\n where open Funable\n\nS\u00d7\u207b : \u2200 {m A B C} \u2192 Search m (A \u2192 B \u2192 C) \u2192 Search m (A \u00d7 B \u2192 C)\nS\u00d7\u207b = S\u03a0\u03a3\u207b\n\nS\u00d7\u207b-ind : \u2200 {m p A B C}\n \u2192 {s : Search m (A \u2192 B \u2192 C)}\n \u2192 SearchInd p s\n \u2192 SearchInd p (S\u00d7\u207b s)\nS\u00d7\u207b-ind = S\u03a0\u03a3\u207b-ind\n\nS\u03a0\u228e\u207b : \u2200 {m A B} {C : A \u228e B \u2192 \u2605 _}\n -- \u2192 Search m (\u03a0 A (C \u2218 inj\u2081)) \u2192 Search m (\u03a0 B (C \u2218 inj\u2082))\n \u2192 Search m (\u03a0 A (C \u2218 inj\u2081) \u00d7 \u03a0 B (C \u2218 inj\u2082))\n \u2192 Search m (\u03a0 (A \u228e B) C)\nS\u03a0\u228e\u207b s _\u2219_ f = s _\u2219_ (f \u2218 uncurry [_,_])\n\nS\u03a0\u228e\u207b-ind : \u2200 {m p A B} {C : A \u228e B \u2192 \u2605 _}\n {- {sA : Search m (\u03a0 A (C \u2218 inj\u2081))}\n (iA : SearchInd p sA)\n {sB : Search m (\u03a0 B (C \u2218 inj\u2082))}\n (iB : SearchInd p sB) -}\n {s : Search m (\u03a0 A (C \u2218 inj\u2081) \u00d7 \u03a0 B (C \u2218 inj\u2082))}\n (i : SearchInd p s)\n \u2192 SearchInd p (S\u03a0\u228e\u207b {C = C} s) -- A sB)\nS\u03a0\u228e\u207b-ind i P P\u2219 Pf = i (P \u2218 S\u03a0\u228e\u207b) P\u2219 (Pf \u2218 uncurry [_,_])\n\n\u03bc\u03a0\u228e\u207b : \u2200 {A B}{C : A \u228e B \u2192 \u2605 _} \u2192 Searchable (\u03a0 A (C \u2218 inj\u2081) \u00d7 \u03a0 B (C \u2218 inj\u2082))\n \u2192 Searchable (\u03a0 (A \u228e B) C)\n\u03bc\u03a0\u228e\u207b (mk s s-ind s-ade) = mk (S\u03a0\u228e\u207b s) (S\u03a0\u228e\u207b-ind s-ind) {!!}\n\n{- For each A\u2192C function\n and each B\u2192C function\n an A\u228eB\u2192C function is yield\n -}\nS\u228e\u207b : \u2200 {m A B C} \u2192 Search m (A \u2192 C) \u2192 Search m (B \u2192 C)\n \u2192 Search m (A \u228e B \u2192 C)\nS\u228e\u207b sA sB = S\u03a0\u228e\u207b (sA \u00d7-search sB)\n\n_\u228e-Fun_ : \u2200 {A B} \u2192 Funable A \u2192 Funable B \u2192 Funable (A \u228e B)\n_\u228e-Fun_ (\u03bcA , \u03bcA\u2192) (\u03bcB , \u03bcB\u2192) = (\u03bcA \u228e-\u03bc \u03bcB) , (\u03bb X \u2192 \u03bc\u03a0\u228e\u207b (\u03bcA\u2192 X \u00d7-\u03bc \u03bcB\u2192 X))\n\nS\u22a4 : \u2200 {m A} \u2192 Search m A \u2192 Search m (\u22a4 \u2192 A)\nS\u22a4 sA _\u2219_ f = sA _\u2219_ (f \u2218 const)\n\nS\u03a0Bit : \u2200 {m A} \u2192 Search m (A 0b) \u2192 Search m (A 1b)\n \u2192 Search m (\u03a0 Bit A)\nS\u03a0Bit sA\u2080 sA\u2081 _\u2219_ f = sA\u2080 _\u2219_ \u03bb x \u2192 sA\u2081 _\u2219_ \u03bb y \u2192 f \u03bb {true \u2192 y; false \u2192 x}\n\nsum-const : \u2200 {A} (\u03bcA : Searchable A) \u2192 \u2200 k \u2192 sum \u03bcA (const k) \u2261 Card \u03bcA * k\nsum-const \u03bcA k\n rewrite \u2115\u00b0.*-comm (Card \u03bcA) k\n | \u2261.sym (sum-lin \u03bcA (const 1) k)\n | proj\u2082 \u2115\u00b0.*-identity k = \u2261.refl\n\ninfixr 4 _\u00d7Sum-proj\u2081_ _\u00d7Sum-proj\u2081'_ _\u00d7Sum-proj\u2082_ _\u00d7Sum-proj\u2082'_\n\n_\u00d7Sum-proj\u2081_ : \u2200 {A B}\n (\u03bcA : Searchable A)\n (\u03bcB : Searchable B)\n f \u2192\n sum (\u03bcA \u00d7-\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 Card \u03bcB * sum \u03bcA f\n(\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n rewrite sum-ext \u03bcA (sum-const \u03bcB \u2218 f)\n = sum-lin \u03bcA f (Card \u03bcB)\n\n_\u00d7Sum-proj\u2082_ : \u2200 {A B}\n (\u03bcA : Searchable A)\n (\u03bcB : Searchable B)\n f \u2192\n sum (\u03bcA \u00d7-\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 Card \u03bcA * sum \u03bcB f\n(\u03bcA \u00d7Sum-proj\u2082 \u03bcB) f = sum-const \u03bcA (sum \u03bcB f)\n\n_\u00d7Sum-proj\u2081'_ : \u2200 {A B}\n (\u03bcA : Searchable A) (\u03bcB : Searchable B)\n {f} {g} \u2192\n sum \u03bcA f \u2261 sum \u03bcA g \u2192\n sum (\u03bcA \u00d7-\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 sum (\u03bcA \u00d7-\u03bc \u03bcB) (g \u2218 proj\u2081)\n(\u03bcA \u00d7Sum-proj\u2081' \u03bcB) {f} {g} sumf\u2261sumg\n rewrite (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n | (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) g\n | sumf\u2261sumg = \u2261.refl\n\n_\u00d7Sum-proj\u2082'_ : \u2200 {A B}\n (\u03bcA : Searchable A) (\u03bcB : Searchable B)\n {f} {g} \u2192\n sum \u03bcB f \u2261 sum \u03bcB g \u2192\n sum (\u03bcA \u00d7-\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 sum (\u03bcA \u00d7-\u03bc \u03bcB) (g \u2218 proj\u2082)\n(\u03bcA \u00d7Sum-proj\u2082' \u03bcB) sumf\u2261sumg = sum-ext \u03bcA (const sumf\u2261sumg)\n\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Vec.NP as Vec using (Vec; tabulate; _++_) renaming (map to vmap; sum to vsum; foldr to vfoldr; foldr\u2081 to vfoldr\u2081)\n\nvmsum : \u2200 {c \u2113} (m : Monoid c \u2113) {n} \u2192\n let open Mon m in\n Vec C n \u2192 C\nvmsum m = vfoldr _ _\u2219_ \u03b5\n where open Mon m\n\nvsgsum : \u2200 {c \u2113} (sg : Semigroup c \u2113) {n} \u2192\n let open Sgrp sg in\n Vec C (suc n) \u2192 C\nvsgsum sg = vfoldr\u2081 _\u2219_\n where open Sgrp sg\n\n-- let's recall that: tabulate f \u2257 vmap f (allFin n)\n\n-- searchMonFin : \u2200 n \u2192 SearchMon (Fin n)\n-- searchMonFin n m f = vmsum m (tabulate f)\n\nsearchFinSuc : \u2200 {m} n \u2192 Search m (Fin (suc n))\nsearchFinSuc n _\u2219_ f = vfoldr\u2081 _\u2219_ (tabulate f)\n\n\u03bcMaybe : \u2200 {A} \u2192 Searchable A \u2192 Searchable (Maybe A)\n\u03bcMaybe \u03bcA = \u03bc-iso (FI.sym Maybe\u2194\u22a4\u228e) (\u03bc\u22a4 \u228e-\u03bc \u03bcA)\n\n\u03bcMaybe^ : \u2200 {A} n \u2192 Searchable A \u2192 Searchable (Maybe^ n A)\n\u03bcMaybe^ zero \u03bcA = \u03bcA\n\u03bcMaybe^ (suc n) \u03bcA = \u03bcMaybe (\u03bcMaybe^ n \u03bcA)\n\n\u03bcFinSuc : \u2200 n \u2192 Searchable (Fin (suc n))\n\u03bcFinSuc n = mk _ (ind n) {!!}\n where ind : \u2200 n \u2192 SearchInd _ (searchFinSuc n)\n ind zero P P\u2219 Pf = Pf zero\n ind (suc n) P P\u2219 Pf = P\u2219 (Pf zero) (ind n (\u03bb s \u2192 P (\u03bb op f \u2192 s op (f \u2218 suc))) P\u2219 (Pf \u2218 suc))\n\n\u03bcFinSucIso : \u2200 n \u2192 Searchable (Fin (suc n))\n\u03bcFinSucIso n = \u03bc-iso (Maybe^\u22a4\u2194Fin1+ n) (\u03bcMaybe^ n \u03bc\u22a4)\n\n\u03bc^ : \u2200 {A} (\u03bcA : Searchable A) n \u2192 Searchable (A ^ n)\n\u03bc^ \u03bcA zero = \u03bcLift \u03bc\u22a4\n\u03bc^ \u03bcA (suc n) = \u03bcA \u00d7-\u03bc \u03bc^ \u03bcA n\n\n\u03bcVec : \u2200 {A} (\u03bcA : Searchable A) n \u2192 Searchable (Vec A n)\n\u03bcVec \u03bcA n = \u03bc-iso (^\u2194Vec n) (\u03bc^ \u03bcA n)\n\nsearchVec : \u2200 {m A} n \u2192 Search m A \u2192 Search m (Vec A n)\nsearchVec zero search\u1d2c op f = f []\nsearchVec (suc n) search\u1d2c op f = search\u1d2c op (\u03bb x \u2192 searchVec n search\u1d2c op (f \u2218 _\u2237_ x))\n\nsearchVec-spec : \u2200 {A} (\u03bcA : Searchable A) n \u2192 searchVec n (search \u03bcA) \u2248Search search (\u03bcVec \u03bcA n)\nsearchVec-spec \u03bcA zero op f = \u2261.refl\nsearchVec-spec \u03bcA (suc n) op f = search-ext \u03bcA op (\u03bb x \u2192 searchVec-spec \u03bcA n op (f \u2218 _\u2237_ x))\n\nsearchVec-++ : \u2200 {A} n {m} (\u03bcA : Searchable A) sg\n \u2192 let open Sgrp sg in\n (f : Vec A (n + m) \u2192 C)\n \u2192 search (\u03bcVec \u03bcA (n + m)) _\u2219_ f\n \u2248 search (\u03bcVec \u03bcA n) _\u2219_ (\u03bb xs \u2192\n search (\u03bcVec \u03bcA m) _\u2219_ (\u03bb ys \u2192\n f (xs ++ ys)))\nsearchVec-++ zero \u03bcA sg f = Sgrp.refl sg\nsearchVec-++ (suc n) \u03bcA sg f = search-sg-ext \u03bcA sg (\u03bb x \u2192\n searchVec-++ n \u03bcA sg (f \u2218 _\u2237_ x))\n\nsumVec-swap : \u2200 {A} n {m} (\u03bcA : Searchable A)(f : Vec A (n + m) \u2192 \u2115)\n \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 f (xs ++ ys)))\nsumVec-swap n {m} \u03bcA f = sum-swap (\u03bcVec \u03bcA n) (\u03bcVec \u03bcA m) (\u03bb xs ys \u2192 f (xs ++ ys))\n\nswapS : \u2200 {A B} \u2192 Searchable (A \u00d7 B) \u2192 Searchable (B \u00d7 A)\nswapS = \u03bc-iso swap-iso\n\nswapS-preserve : \u2200 {A B} f (\u03bcA\u00d7B : Searchable (A \u00d7 B)) \u2192 sum \u03bcA\u00d7B f \u2261 sum (swapS \u03bcA\u00d7B) (f \u2218 swap)\nswapS-preserve = \u03bc-iso-preserve swap-iso\n\nmodule _ {A : Set}(\u03bcA : Searchable A) where\n\n private\n sA = search \u03bcA\n\n extend : \u2200 {n} \u2192 A \u2192 (Fin n \u2192 A) \u2192 Fin (suc n) \u2192 A\n extend x g zero = x\n extend x g (suc i) = g i\n\n abs : Fin 0 \u2192 A\n abs ()\n\n -- There is one function Fin 0 \u2192 A (called abs) so this should be fine\n -- if not there is a version below that forces the domain to be non-empty\n sFun : \u2200 n \u2192 Search _ (Fin n \u2192 A)\n sFun zero op f = f abs\n sFun (suc n) op f = sA op (\u03bb x \u2192 sFun n op (f \u2218 extend x))\n\n ind : \u2200 n \u2192 SearchInd _ (sFun n)\n ind zero P P\u2219 Pf = Pf abs\n ind (suc n) P P\u2219 Pf =\n search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (\u03bb x \u2192 sFun n op (f \u2218 extend x))))\n P\u2219\n (\u03bb x \u2192 ind n (\u03bb sf \u2192 P (\u03bb op f \u2192 sf op (f \u2218 extend x)))\n P\u2219 (Pf \u2218 extend x))\n\n \u03bcFun : \u2200 {n} \u2192 Searchable (Fin n \u2192 A)\n \u03bcFun = mk _ (ind _) {!!}\n\nmodule BigDistr\n {A}(\u03bcA : Searchable A)\n (cm : CommutativeMonoid L.zero L.zero)\n -- we want (open CMon cm) !!!\n (_\u25ce_ : let open CMon cm in C \u2192 C \u2192 C)\n (distrib : let open CMon cm in _DistributesOver_ _\u2248_ _\u25ce_ _\u2219_)\n (_\u25ce-cong_ : let open CMon cm in _\u25ce_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_) where\n\n open CMon cm\n\n \u03bcF\u2192A = \u03bcFun \u03bcA\n\n -- Sum over A\n \u03a3\u1d2c = search \u03bcA _\u2219_\n\n -- Sum over (Fin(1+I)\u2192A) functions\n \u03a3' : \u2200 {I} \u2192 ((Fin (suc I) \u2192 A) \u2192 C) \u2192 C\n \u03a3' = search \u03bcF\u2192A _\u2219_\n\n -- Product over Fin(1+I) values\n \u03a0' = \u03bb I \u2192 search (\u03bcFinSuc I) _\u25ce_\n\n bigDistr : \u2200 I F \u2192 \u03a0' I (\u03a3\u1d2c \u2218 F) \u2248 \u03a3' (\u03a0' I \u2218 _\u02e2_ F)\n bigDistr zero _ = refl\n bigDistr (suc I) F\n = \u03a3\u1d2c (F zero) \u25ce \u03a0' I (\u03a3\u1d2c \u2218 F \u2218 suc)\n \u2248\u27e8 refl \u25ce-cong bigDistr I (F \u2218 suc) \u27e9\n \u03a3\u1d2c (F zero) \u25ce \u03a3' (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc))\n \u2248\u27e8 sym (search-lin\u02b3 \u03bcA monoid _\u25ce_ (F zero) (\u03a3' (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc))) (proj\u2082 distrib)) \u27e9\n \u03a3\u1d2c (\u03bb j \u2192 F zero j \u25ce \u03a3' (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc)))\n \u2248\u27e8 search-sg-ext \u03bcA semigroup (\u03bb j \u2192 sym (search-lin\u02e1 \u03bcF\u2192A monoid _\u25ce_ (\u03a0' I \u2218 _\u02e2_ (F \u2218 suc)) (F zero j) (proj\u2081 distrib))) \u27e9\n (\u03a3\u1d2c \u03bb j \u2192 \u03a3' \u03bb f \u2192 F zero j \u25ce \u03a0' I ((F \u2218 suc) \u02e2 f))\n \u220e\n\nFinDist : \u2200 {n} \u2192 DistFun (\u03bcFinSuc n) (\u03bb \u03bcX \u2192 \u03bcFun \u03bcX)\nFinDist \u03bcB c \u25ce distrib \u25ce-cong f = BigDistr.bigDistr \u03bcB c \u25ce distrib \u25ce-cong _ f\n\nsimple : \u2200 {A : Set}{P : A \u2192 A \u2192 Set} \u2192 (\u2200 x \u2192 P x x) \u2192 {x y : A} \u2192 x \u2261 y \u2192 P x y\nsimple r \u2261.refl = r _\n\n\u00d7-Dist : \u2200 {A B} FA FB \u2192 DistFunable {A} FA \u2192 DistFunable {B} FB \u2192 DistFunable (\u03a3-Fun FA FB)\n\u00d7-Dist FA FB FA-dist FB-dist \u03bcX c _\u2299_ distrib _\u2299-cong_ f\n = \u03a0\u1d2c (\u03bb x \u2192 \u03a0\u1d2e (\u03bb y \u2192 \u03a3' (f (x , y))))\n \u2248\u27e8 \u27e6search\u27e7 (searchable FA){_\u2261_} \u2261.refl _\u2248_ (\u03bb x y \u2192 x \u2299-cong y)\n (\u03bb { {x} {.x} \u2261.refl \u2192 FB-dist \u03bcX c _\u2299_ distrib _\u2299-cong_ (curry f x)}) \u27e9\n \u03a0\u1d2c (\u03bb x \u2192 \u03a3\u1d2e (\u03bb fb \u2192 \u03a0\u1d2e (\u03bb y \u2192 f (x , y) (fb y))))\n \u2248\u27e8 FA-dist (negative FB \u03bcX) c _\u2299_ distrib _\u2299-cong_\n (\u03bb x fb \u2192 search (searchable FB) _\u2299_ (\u03bb y \u2192 f (x , y) (fb y))) \u27e9\n \u03a3\u1d2c\u1d2e (\u03bb fab \u2192 \u03a0\u1d2c (\u03bb x \u2192 \u03a0\u1d2e (\u03bb y \u2192 f (x , y) (fab x y))))\n \u220e\n where\n open CMon c\n open Funable\n\n \u03a3' = search \u03bcX _\u2219_\n\n \u03a0\u1d2c = search (searchable FA) _\u2299_\n \u03a0\u1d2e = search (searchable FB) _\u2299_\n\n \u03a3\u1d2c\u1d2e = search (negative FA (negative FB \u03bcX)) _\u2219_\n \u03a3\u1d2e = search (negative FB \u03bcX) _\u2219_\n\n\u228e-Dist : \u2200 {A B} FA FB \u2192 DistFunable {A} FA \u2192 DistFunable {B} FB \u2192 DistFunable (FA \u228e-Fun FB)\n\u228e-Dist FA FB FA-dist FB-dist \u03bcX c _\u25ce_ distrib _\u25ce-cong_ f\n = \u03a0\u1d2c (\u03a3' \u2218 f \u2218 inj\u2081) \u25ce \u03a0\u1d2e (\u03a3' \u2218 f \u2218 inj\u2082)\n \u2248\u27e8 FA-dist \u03bcX c _\u25ce_ distrib _\u25ce-cong_ (f \u2218 inj\u2081) \u25ce-cong FB-dist \u03bcX c _\u25ce_ distrib _\u25ce-cong_ (f \u2218 inj\u2082) \u27e9\n \u03a3\u1d2c (\u03bb fa \u2192 \u03a0\u1d2c (\u03bb i \u2192 f (inj\u2081 i) (fa i))) \u25ce \u03a3\u1d2e (\u03bb fb \u2192 \u03a0\u1d2e (\u03bb i \u2192 f (inj\u2082 i) (fb i)))\n \u2248\u27e8 sym (search-lin\u02b3 (negative FA \u03bcX) monoid _\u25ce_ _ _ (proj\u2082 distrib)) \u27e9\n \u03a3\u1d2c (\u03bb fa \u2192 \u03a0\u1d2c (\u03bb i \u2192 f (inj\u2081 i) (fa i)) \u25ce \u03a3\u1d2e (\u03bb fb \u2192 \u03a0\u1d2e (\u03bb i \u2192 f (inj\u2082 i) (fb i))))\n \u2248\u27e8 search-sg-ext (negative FA \u03bcX) semigroup (\u03bb fa \u2192 sym (search-lin\u02e1 (negative FB \u03bcX) monoid _\u25ce_ _ _ (proj\u2081 distrib))) \u27e9\n (\u03a3\u1d2c \u03bb fa \u2192 \u03a3\u1d2e \u03bb fb \u2192 \u03a0\u1d2c ((f \u2218 inj\u2081) \u02e2 fa) \u25ce \u03a0\u1d2e ((f \u2218 inj\u2082) \u02e2 fb))\n \u220e\n where\n open CMon c\n open Funable\n\n \u03a3' = search \u03bcX _\u2219_\n\n \u03a0\u1d2c = search (searchable FA) _\u25ce_\n \u03a0\u1d2e = search (searchable FB) _\u25ce_\n\n \u03a3\u1d2c = search (negative FA \u03bcX) _\u2219_\n \u03a3\u1d2e = search (negative FB \u03bcX) _\u2219_\n\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"5625dccd05d45bee2c7eab37e8e5842f11ca1173","subject":"Desc model: simplify proof-psi-phi","message":"Desc model: simplify proof-psi-phi","repos":"kwangkim\/pigment,kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram","old_file":"models\/IDesc_type_type.agda","new_file":"models\/IDesc_type_type.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {I : Set} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi (pi S T) = cong (pi S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n\n-- From embedding to embedding\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\\ _ -> descD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> descD I)) -> Set\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (descD I) (IDescl0 I))\n (hs : desc (box (descD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\\ s -> psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s -> psi (phi (T s)))\n T\n hs)","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {I : Set} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi (pi S T) = cong (pi S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n\n-- From embedding to embedding\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\\ _ -> descD I)\n P\n proof-psi-phi-casesW\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> descD I)) -> Set\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n P)\n \u2192 P (Void , con xs)\n proof-psi-phi-cases (lvar , i) hs = refl\n proof-psi-phi-cases (lconst , x) hs = refl\n proof-psi-phi-cases (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\u03bb s \u2192 psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s \u2192 psi (phi (T s)))\n T\n hs)\n proof-psi-phi-casesW : (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n P)\n \u2192 P (i , con xs)\n proof-psi-phi-casesW Void = proof-psi-phi-cases\n \n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f8c71f27e3ad30e3903adbc4857985df75a686bc","subject":"Cosmetic changes.","message":"Cosmetic changes.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Data\/Stream\/PropertiesI.agda","new_file":"src\/fot\/FOTC\/Data\/Stream\/PropertiesI.agda","new_contents":"------------------------------------------------------------------------------\n-- Streams properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n-- {-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Stream.PropertiesI where\n\nopen import FOTC.Base\nopen FOTC.Base.BList\nopen import FOTC.Base.PropertiesI\nopen import FOTC.Data.Conat\nopen import FOTC.Data.Conat.Equality\nopen import FOTC.Data.List\nopen import FOTC.Data.Stream\nopen import FOTC.Relation.Binary.Bisimilarity\n\n-----------------------------------------------------------------------------\n\ntailS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\ntailS {x} {xs} h with (Stream-unf h)\n... | x' , xs' , Sxs' , h\u2081 = subst Stream (sym (\u2227-proj\u2082 (\u2237-injective h\u2081))) Sxs'\n\n\u2248\u2192Stream : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Stream xs \u2227 Stream ys\n\u2248\u2192Stream {xs} {ys} h = Stream-coind P\u2081 h\u2081 (ys , h) , Stream-coind P\u2082 h\u2082 (xs , h)\n where\n P\u2081 : D \u2192 Set\n P\u2081 ws = \u2203[ zs ] ws \u2248 zs\n\n h\u2081 : \u2200 {ws} \u2192 P\u2081 ws \u2192 \u2203[ w' ] \u2203[ ws' ] P\u2081 ws' \u2227 ws \u2261 w' \u2237 ws'\n h\u2081 {ws} (zs , h\u2081) with \u2248-unf h\u2081\n ... | w' , ws' , zs' , prf\u2081 , prf\u2082 , _ = w' , ws' , (zs' , prf\u2081) , prf\u2082\n\n P\u2082 : D \u2192 Set\n P\u2082 zs = \u2203[ ws ] ws \u2248 zs\n\n h\u2082 : \u2200 {zs} \u2192 P\u2082 zs \u2192 \u2203[ z' ] \u2203[ zs' ] P\u2082 zs' \u2227 zs \u2261 z' \u2237 zs'\n h\u2082 {zs} (ws , h\u2081) with \u2248-unf h\u2081\n ... | w' , ws' , zs' , prf\u2081 , _ , prf\u2082 = w' , zs' , (ws' , prf\u2081) , prf\u2082\n\n-- Requires K.\nlengthStream : \u2200 {xs} \u2192 Stream xs \u2192 length xs \u2248N \u221e\nlengthStream {xs} Sxs = \u2248N-coind _R_ h\u2081 h\u2082\n where\n _R_ : D \u2192 D \u2192 Set\n m R n = \u2203[ xs ] Stream xs \u2227 m \u2261 length xs \u2227 n \u2261 \u221e\n\n h\u2081 : \u2200 {m n} \u2192 m R n \u2192\n m \u2261 zero \u2227 n \u2261 zero \u2228\n (\u2203[ m' ] \u2203[ n' ] m' R n' \u2227 m \u2261 succ\u2081 m' \u2227 n \u2261 succ\u2081 n')\n h\u2081 (_ , Sxs' , _ ) with Stream-unf Sxs'\n h\u2081 {m} {n} (.(x'' \u2237 xs'') , Sxs' , aux , n\u2261\u221e) | x'' , xs'' , Sxs'' , refl =\n inj\u2082 (length xs'' , \u221e , (xs'' , Sxs'' , refl , refl) , helper\u2081 , helper\u2082)\n where\n helper\u2081 : m \u2261 succ\u2081 (length xs'')\n helper\u2081 = trans aux (length-\u2237 x'' xs'')\n\n helper\u2082 : n \u2261 succ\u2081 \u221e\n helper\u2082 = trans n\u2261\u221e \u221e-eq\n\n h\u2082 : length xs R \u221e\n h\u2082 = xs , Sxs , refl , refl\n","old_contents":"------------------------------------------------------------------------------\n-- Streams properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n-- {-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Stream.PropertiesI where\n\nopen import FOTC.Base\nopen FOTC.Base.BList\nopen import FOTC.Base.PropertiesI\nopen import FOTC.Data.Conat\nopen import FOTC.Data.Conat.Equality\nopen import FOTC.Data.List\nopen import FOTC.Data.Stream\nopen import FOTC.Relation.Binary.Bisimilarity\n\n-----------------------------------------------------------------------------\n\ntailS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\ntailS {x} {xs} h with (Stream-unf h)\n... | x' , xs' , Sxs' , h\u2081 = subst Stream (sym (\u2227-proj\u2082 (\u2237-injective h\u2081))) Sxs'\n\n\u2248\u2192Stream : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Stream xs \u2227 Stream ys\n\u2248\u2192Stream {xs} {ys} h = Stream-coind P\u2081 h\u2081 (ys , h) , Stream-coind P\u2082 h\u2082 (xs , h)\n where\n P\u2081 : D \u2192 Set\n P\u2081 ws = \u2203[ zs ] ws \u2248 zs\n\n h\u2081 : \u2200 {ws} \u2192 P\u2081 ws \u2192 \u2203[ w' ] \u2203[ ws' ] P\u2081 ws' \u2227 ws \u2261 w' \u2237 ws'\n h\u2081 {ws} (zs , h\u2081) with \u2248-unf h\u2081\n ... | w' , ws' , zs' , prf\u2081 , prf\u2082 , _ = w' , ws' , (zs' , prf\u2081) , prf\u2082\n\n P\u2082 : D \u2192 Set\n P\u2082 zs = \u2203[ ws ] ws \u2248 zs\n\n h\u2082 : \u2200 {zs} \u2192 P\u2082 zs \u2192 \u2203[ z' ] \u2203[ zs' ] P\u2082 zs' \u2227 zs \u2261 z' \u2237 zs'\n h\u2082 {zs} (ws , h\u2081) with \u2248-unf h\u2081\n ... | w' , ws' , zs' , prf\u2081 , _ , prf\u2082 = w' , zs' , (ws' , prf\u2081) , prf\u2082\n\n-- Requires K.\nlengthStream : \u2200 {xs} \u2192 Stream xs \u2192 length xs \u2248N \u221e\nlengthStream {xs} Sxs = \u2248N-coind _R_ h\u2081 h\u2082\n where\n _R_ : D \u2192 D \u2192 Set\n m R n = \u2203[ xs ] Stream xs \u2227 m \u2261 length xs \u2227 n \u2261 \u221e\n\n h\u2081 : \u2200 {m n} \u2192 m R n \u2192\n m \u2261 zero \u2227 n \u2261 zero \u2228\n (\u2203[ m' ] \u2203[ n' ] m' R n' \u2227 m \u2261 succ\u2081 m' \u2227 n \u2261 succ\u2081 n')\n h\u2081 (_ , Sxs' , _ ) with Stream-unf Sxs'\n h\u2081 (.(x'' \u2237 xs'') , Sxs' , m\u2261length-x''\u2237xs'' , n\u2261\u221e)\n | x'' , xs'' , Sxs'' , refl =\n inj\u2082 (length xs'' , \u221e , (xs'' , Sxs'' , refl , refl)\n , trans m\u2261length-x''\u2237xs'' (length-\u2237 x'' xs'') , trans n\u2261\u221e \u221e-eq\n )\n\n h\u2082 : length xs R \u221e\n h\u2082 = xs , Sxs , refl , refl\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"24244df1568a8f505be675e6d92c0427c9e962cb","subject":"IDesc in IDesc, stratified, with a 'lift'ing axiom","message":"IDesc in IDesc, stratified, with a 'lift'ing axiom","repos":"brixen\/Epigram,kwangkim\/pigment,kwangkim\/pigment,kwangkim\/pigment","old_file":"models\/IDesc.agda","new_file":"models\/IDesc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift = {!!}\n\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (l : Level)(I : Set l) : Set (suc l) where\n var : I -> IDesc l I\n const : Set l -> IDesc l I\n prod : IDesc l I -> IDesc l I -> IDesc l I\n sigma : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n pi : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : (I : Set) -> IDesc zero I -> (I -> Set) -> Set\ndesc I (var i) P = P i\ndesc I (const X) P = X\ndesc I (prod D D') P = desc I D P * desc I D' P\ndesc I (sigma S T) P = Sigma S (\\s -> desc I (T s) P)\ndesc I (pi S T) P = (s : S) -> desc I (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\n{-\ndata IMu (I : Set)(R : I -> IDesc I) : IDesc I -> Set1 where\n rec : (i : I) -> IMu I R (R i) -> IMu I R (var i)\n lambda : (S : Set)(D : S -> IDesc I) -> ((s : S) -> IMu I R (D s)) -> IMu I R (pi S D)\n pair : (S : Set)(D : S -> IDesc I) -> (Sigma S (\\s -> IMu I R (D s))) -> IMu I R (sigma S D)\n-}\n\ndata IMu (I : Set)(R : I -> IDesc zero I)(i : I) : Set where\n con : desc I (R i) (\\j -> IMu I R j) -> IMu I R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : (I : Set)(D : IDesc zero I)(P : I -> Set) -> desc I D P -> IDesc zero (Sigma I P)\nbox I (var i) P x = var (i , x)\nbox I (const X) P x = const X\nbox I (prod D D') P (d , d') = prod (box I D P d) (box I D' P d')\nbox I (sigma S T) P (a , b) = box I (T a) P b\nbox I (pi S T) P f = pi S (\\s -> box I (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (I : Set)\n (R : I -> IDesc zero I)\n (P : Sigma I (IMu I R) -> Set)\n (m : (i : I)(xs : desc I (R i) (IMu I R))(hs : desc (Sigma I (IMu I R)) (box I (R i) (IMu I R) xs) P) -> P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu I R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc zero I) -> (xs : desc I D (IMu I R)) -> desc (Sigma I (IMu I R)) (box I D (IMu I R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (I : Set)(R : I -> IDesc zero I)(P : Sigma I (IMu I R) -> Set)\n (m : (i : I)\n (xs : desc I (R i) (IMu I R))\n (hs : desc (Sigma I (IMu I R)) (box I (R i) (IMu I R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu I R i) -> P ( i , x )\ninduction = Elim.induction\n\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set1 where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc (suc zero) Unit\ndescDChoice I lvar = const (lift I)\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc (suc zero) Unit\ndescD I = sigma DescDConst (descDChoice I)\n\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\nrecord One {i : Level} : Set i where\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set1 where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : (I : Set) -> IDesc I -> (I -> Set) -> Set\ndesc I (var i) P = P i\ndesc I (const X) P = X\ndesc I (prod D D') P = desc I D P * desc I D' P\ndesc I (sigma S T) P = Sigma S (\\s -> desc I (T s) P)\ndesc I (pi S T) P = (s : S) -> desc I (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\n{-\ndata IMu (I : Set)(R : I -> IDesc I) : IDesc I -> Set1 where\n rec : (i : I) -> IMu I R (R i) -> IMu I R (var i)\n lambda : (S : Set)(D : S -> IDesc I) -> ((s : S) -> IMu I R (D s)) -> IMu I R (pi S D)\n pair : (S : Set)(D : S -> IDesc I) -> (Sigma S (\\s -> IMu I R (D s))) -> IMu I R (sigma S D)\n-}\n\ndata IMu (I : Set)(R : I -> IDesc I)(i : I) : Set where\n con : desc I (R i) (\\j -> IMu I R j) -> IMu I R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : (I : Set)(D : IDesc I)(P : I -> Set) -> desc I D P -> IDesc (Sigma I P)\nbox I (var i) P x = var (i , x)\nbox I (const X) P x = const X\nbox I (prod D D') P (d , d') = prod (box I D P d) (box I D' P d')\nbox I (sigma S T) P (a , b) = box I (T a) P b\nbox I (pi S T) P f = pi S (\\s -> box I (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu I R) -> Set)\n (m : (i : I)(xs : desc I (R i) (IMu I R))(hs : desc (Sigma I (IMu I R)) (box I (R i) (IMu I R) xs) P) -> P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu I R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> (xs : desc I D (IMu I R)) -> desc (Sigma I (IMu I R)) (box I D (IMu I R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (I : Set)(R : I -> IDesc I)(P : Sigma I (IMu I R) -> Set)\n (m : (i : I)\n (xs : desc I (R i) (IMu I R))\n (hs : desc (Sigma I (IMu I R)) (box I (R i) (IMu I R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu I R i) -> P ( i , x )\ninduction = Elim.induction","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"948451ac92f2f059429dba6effbfd6f9aad6ebea","subject":"Working on the transpose map.","message":"Working on the transpose map.\n","repos":"heades\/AUGL","old_file":"dialectica-cats\/DCBSets.agda","new_file":"dialectica-cats\/DCBSets.agda","new_contents":"module DCBSets where\n\nopen import prelude\n\n-- The objects:\nObj : Set\u2081\nObj = \u03a3[ U \u2208 Set ] (\u03a3[ X \u2208 Set ] (\u03a3[ m \u2208 (\u22a4 \u2192 U) ] (\u03a3[ n \u2208 (\u22a4 \u2192 X) ] (U \u2192 X \u2192 Set))))\n\n-- The morphisms:\nHom : Obj \u2192 Obj \u2192 Set\nHom (U , X , _ , _ , \u03b1) (V , Y , _ , _ , \u03b2) =\n \u03a3[ f \u2208 (U \u2192 V) ]\n (\u03a3[ F \u2208 (U \u2192 Y \u2192 X) ] (\u2200{u : U}{y : Y} \u2192 \u03b1 u (F u y) \u2192 \u03b2 (f u) y))\n\n-- Composition:\ncomp : {A B C : Obj} \u2192 Hom A B \u2192 Hom B C \u2192 Hom A C\ncomp {(U , X , m\u2081 , n\u2081 , \u03b1)} {(V , Y , m\u2082 , n\u2082 , \u03b2)} {(W , Z , m\u2083 , n\u2083 , \u03b3)} (f , F , p\u2081) (g , G , p\u2082) =\n (g \u2218 f , (\u03bb u z \u2192 F u (G (f u) z)), (\u03bb {u} {y} p\u2083 \u2192 p\u2082 (p\u2081 p\u2083)))\n\ninfixl 5 _\u25cb_\n\n_\u25cb_ = comp\n\n-- The contravariant hom-functor:\nHom\u2090 : {A' A B B' : Obj} \u2192 Hom A' A \u2192 Hom B B' \u2192 Hom A B \u2192 Hom A' B'\nHom\u2090 f h g = comp f (comp g h)\n\n-- The identity function:\nid : {A : Obj} \u2192 Hom A A \nid {(U , V , m , n , \u03b1)} = (id-set , curry snd , id-set)\n\n-- In this formalization we will only worry about proving that the\n-- data of morphisms are equivalent, and not worry about the morphism\n-- conditions. This will make proofs shorter and faster.\n--\n-- If we have parallel morphisms (f,F) and (g,G) in which we know that\n-- f = g and F = G, then the condition for (f,F) will imply the\n-- condition of (g,G) and vice versa. Thus, we can safely ignore it.\ninfix 4 _\u2261h_\n\n_\u2261h_ : {A B : Obj} \u2192 (f g : Hom A B) \u2192 Set\n_\u2261h_ {(U , X , _ , _ , \u03b1)}{(V , Y , _ , _ , \u03b2)} (f , F , p\u2081) (g , G , p\u2082) = f \u2261 g \u00d7 F \u2261 G\n\n\u2261h-refl : {A B : Obj}{f : Hom A B} \u2192 f \u2261h f\n\u2261h-refl {U , X , _ , _ , \u03b1}{V , Y , _ , _ , \u03b2}{f , F , _} = refl , refl\n\n\u2261h-trans : \u2200{A B}{f g h : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h h \u2192 f \u2261h h\n\u2261h-trans {U , X , _ , _ , \u03b1}{V , Y , _ , _ , \u03b2}{f , F , _}{g , G , _}{h , H , _} (p\u2081 , p\u2082) (p\u2083 , p\u2084) rewrite p\u2081 | p\u2082 | p\u2083 | p\u2084 = refl , refl\n\n\u2261h-sym : \u2200{A B}{f g : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h f\n\u2261h-sym {U , X , _ , _ , \u03b1}{V , Y , _ , _ , \u03b2}{f , F , _}{g , G , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\n\u2261h-subst-\u25cb : \u2200{A B C}{f\u2081 f\u2082 : Hom A B}{g\u2081 g\u2082 : Hom B C}{j : Hom A C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 g\u2081 \u2261h g\u2082\n \u2192 f\u2082 \u25cb g\u2082 \u2261h j\n \u2192 f\u2081 \u25cb g\u2081 \u2261h j\n\u2261h-subst-\u25cb {U , X , _ , _ , \u03b1}\n {V , Y , _ , _ , \u03b2}\n {W , Z , _ , _ , \u03b3}\n {f\u2081 , F\u2081 , _}\n {f\u2082 , F\u2082 , _}\n {g\u2081 , G\u2081 , _}\n {g\u2082 , G\u2082 , _}\n {j , J , _}\n (p\u2085 , p\u2086) (p\u2087 , p\u2088) (p\u2089 , p\u2081\u2080) rewrite p\u2085 | p\u2086 | p\u2087 | p\u2088 | p\u2089 | p\u2081\u2080 = refl , refl\n\n\u25cb-assoc : \u2200{A B C D}{f : Hom A B}{g : Hom B C}{h : Hom C D}\n \u2192 f \u25cb (g \u25cb h) \u2261h (f \u25cb g) \u25cb h\n\u25cb-assoc {U , X , _ , _ , \u03b1}{V , Y , _ , _ , \u03b2}{W , Z , _ , _ , \u03b3}{S , T , _ , _ , \u03b9}\n {f , F , _}{g , G , _}{h , H , _} = refl , refl\n\n\u25cb-idl : \u2200{A B}{f : Hom A B} \u2192 id \u25cb f \u2261h f\n\u25cb-idl {U , X , _ , _ , _}{V , Y , _ , _ , _}{f , F , _} = refl , refl\n\n\u25cb-idr : \u2200{A B}{f : Hom A B} \u2192 f \u25cb id \u2261h f\n\u25cb-idr {U , X , _ , _ , _}{V , Y , _ , _ , _}{f , F , _} = refl , refl\n\n-- The tensor functor: \u2297\n_\u2297\u1d63_ : \u2200{U X V Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 ((U \u00d7 V) \u2192 (X \u00d7 Y) \u2192 Set)\n_\u2297\u1d63_ \u03b1 \u03b2 (u , v) (x , y) = (\u03b1 u x) \u00d7 (\u03b2 v y)\n\n_\u2297\u2092_ : (A B : Obj) \u2192 Obj\n(U , X , m\u2081 , n\u2081 , \u03b1) \u2297\u2092 (V , Y , m\u2082 , n\u2082 , \u03b2) = ((U \u00d7 V) , (X \u00d7 Y) , trans-\u00d7 m\u2081 m\u2082 , trans-\u00d7 n\u2081 n\u2082 , \u03b1 \u2297\u1d63 \u03b2)\n\nF\u2297 : \u2200{Z T V X U Y : Set}{F : U \u2192 Z \u2192 X}{G : V \u2192 T \u2192 Y} \u2192 (U \u00d7 V) \u2192 (Z \u00d7 T) \u2192 (X \u00d7 Y)\nF\u2297 {F = F}{G} (u , v) (z , t) = F u z , G v t\n \n_\u2297\u2090_ : {A B C D : Obj} \u2192 Hom A C \u2192 Hom B D \u2192 Hom (A \u2297\u2092 B) (C \u2297\u2092 D)\n_\u2297\u2090_ {(U , X , _ , _ , \u03b1)}{(V , Y , _ , _ , \u03b2)}{(W , Z , _ , _ , \u03b3)}{(S , T , _ , _ , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) = \u27e8 f , g \u27e9 , F\u2297 {F = F}{G} , p\u2297\n where\n p\u2297 : {u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Z (\u03bb x \u2192 T)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (F\u2297 {F = F}{G} u y) \u2192 (\u03b3 \u2297\u1d63 \u03b4) (\u27e8 f , g \u27e9 u) y\n p\u2297 {u , v}{z , t} (p\u2083 , p\u2084) = p\u2081 p\u2083 , p\u2082 p\u2084\n\n\u03c0\u2081 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {m\u2081 : \u22a4 \u2192 U}\n \u2192 {n\u2081 : \u22a4 \u2192 X}\n \u2192 {m\u2082 : \u22a4 \u2192 V}\n \u2192 {n\u2082 : \u22a4 \u2192 Y} \n \u2192 Hom ((U , X , m\u2081 , n\u2081 , \u03b1) \u2297\u2092 (V , Y , m\u2082 , n\u2082 , \u03b2)) (U , X , m\u2081 , n\u2081 , \u03b1)\n\u03c0\u2081 {U}{X}{V}{Y}{\u03b1}{\u03b2}{m\u2081}{n\u2081}{m\u2082}{n\u2082} = fst , (\u03bb r x \u2192 x , n\u2082 triv) , cond\n where\n cond : {u : \u03a3 U (\u03bb x \u2192 V)} {y : X} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (y , n\u2082 triv) \u2192 \u03b1 (fst u) y\n cond {u , v}{x} (p\u2081 , p\u2082) = p\u2081\n\n\u03c0\u2082 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {m\u2081 : \u22a4 \u2192 U}\n \u2192 {n\u2081 : \u22a4 \u2192 X}\n \u2192 {m\u2082 : \u22a4 \u2192 V}\n \u2192 {n\u2082 : \u22a4 \u2192 Y} \n \u2192 Hom ((U , X , m\u2081 , n\u2081 , \u03b1) \u2297\u2092 (V , Y , m\u2082 , n\u2082 , \u03b2)) (V , Y , m\u2082 , n\u2082 , \u03b2)\n\u03c0\u2082 {U}{X}{V}{Y}{\u03b1}{\u03b2}{m\u2081}{n\u2081}{m\u2082}{n\u2082} = snd , (\u03bb r y \u2192 n\u2081 triv , y) , cond\n where\n cond : {u : \u03a3 U (\u03bb x \u2192 V)} {y : Y} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (n\u2081 triv , y) \u2192 \u03b2 (snd u) y\n cond {u , v}{y} (p\u2081 , p\u2082) = p\u2082\n\ncart-ar : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {m\u2081 : \u22a4 \u2192 U}\n \u2192 {n\u2081 : \u22a4 \u2192 X}\n \u2192 {m\u2082 : \u22a4 \u2192 V}\n \u2192 {n\u2082 : \u22a4 \u2192 Y}\n \u2192 {m\u2083 : \u22a4 \u2192 W}\n \u2192 {n\u2083 : \u22a4 \u2192 Z} \n \u2192 Hom (W , Z , m\u2083 , n\u2083 , \u03b3) (U , X , m\u2081 , n\u2081 , \u03b1)\n \u2192 Hom (W , Z , m\u2083 , n\u2083 , \u03b3) (V , Y , m\u2082 , n\u2082 , \u03b2)\n \u2192 Hom (W , Z , m\u2083 , n\u2083 , \u03b3) ((U , X , m\u2081 , n\u2081 , \u03b1) \u2297\u2092 (V , Y , m\u2082 , n\u2082 , \u03b2))\ncart-ar {U}{X}{V}{Y}{W}{Z}{\u03b1}{\u03b2}{\u03b3}{m\u2081}{n\u2081}{m\u2082}{n\u2082}{m\u2083}{n\u2083} (f , F) (g , G) = trans-\u00d7 f g , {!!} , {!!}\n","old_contents":"module DCBSets where\n\nopen import prelude\n\n-- The objects:\nObj : Set\u2081\nObj = \u03a3[ U \u2208 Set ] (\u03a3[ X \u2208 Set ] (\u03a3[ m \u2208 (\u22a4 \u2192 U) ] (\u03a3[ n \u2208 (\u22a4 \u2192 X) ] (U \u2192 X \u2192 Set))))\n\n-- The morphisms:\nHom : Obj \u2192 Obj \u2192 Set\nHom (U , X , _ , _ , \u03b1) (V , Y , _ , _ , \u03b2) =\n \u03a3[ f \u2208 (U \u2192 V) ]\n (\u03a3[ F \u2208 (U \u2192 Y \u2192 X) ] (\u2200{u : U}{y : Y} \u2192 \u03b1 u (F u y) \u2192 \u03b2 (f u) y))\n\n-- Composition:\ncomp : {A B C : Obj} \u2192 Hom A B \u2192 Hom B C \u2192 Hom A C\ncomp {(U , X , m\u2081 , n\u2081 , \u03b1)} {(V , Y , m\u2082 , n\u2082 , \u03b2)} {(W , Z , m\u2083 , n\u2083 , \u03b3)} (f , F , p\u2081) (g , G , p\u2082) =\n (g \u2218 f , (\u03bb u z \u2192 F u (G (f u) z)), (\u03bb {u} {y} p\u2083 \u2192 p\u2082 (p\u2081 p\u2083)))\n\ninfixl 5 _\u25cb_\n\n_\u25cb_ = comp\n\n-- The contravariant hom-functor:\nHom\u2090 : {A' A B B' : Obj} \u2192 Hom A' A \u2192 Hom B B' \u2192 Hom A B \u2192 Hom A' B'\nHom\u2090 f h g = comp f (comp g h)\n\n-- The identity function:\nid : {A : Obj} \u2192 Hom A A \nid {(U , V , m , n , \u03b1)} = (id-set , curry snd , id-set)\n\n-- In this formalization we will only worry about proving that the\n-- data of morphisms are equivalent, and not worry about the morphism\n-- conditions. This will make proofs shorter and faster.\n--\n-- If we have parallel morphisms (f,F) and (g,G) in which we know that\n-- f = g and F = G, then the condition for (f,F) will imply the\n-- condition of (g,G) and vice versa. Thus, we can safely ignore it.\ninfix 4 _\u2261h_\n\n_\u2261h_ : {A B : Obj} \u2192 (f g : Hom A B) \u2192 Set\n_\u2261h_ {(U , X , _ , _ , \u03b1)}{(V , Y , _ , _ , \u03b2)} (f , F , p\u2081) (g , G , p\u2082) = f \u2261 g \u00d7 F \u2261 G\n\n\u2261h-refl : {A B : Obj}{f : Hom A B} \u2192 f \u2261h f\n\u2261h-refl {U , X , _ , _ , \u03b1}{V , Y , _ , _ , \u03b2}{f , F , _} = refl , refl\n\n\u2261h-trans : \u2200{A B}{f g h : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h h \u2192 f \u2261h h\n\u2261h-trans {U , X , _ , _ , \u03b1}{V , Y , _ , _ , \u03b2}{f , F , _}{g , G , _}{h , H , _} (p\u2081 , p\u2082) (p\u2083 , p\u2084) rewrite p\u2081 | p\u2082 | p\u2083 | p\u2084 = refl , refl\n\n\u2261h-sym : \u2200{A B}{f g : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h f\n\u2261h-sym {U , X , _ , _ , \u03b1}{V , Y , _ , _ , \u03b2}{f , F , _}{g , G , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\n\u2261h-subst-\u25cb : \u2200{A B C}{f\u2081 f\u2082 : Hom A B}{g\u2081 g\u2082 : Hom B C}{j : Hom A C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 g\u2081 \u2261h g\u2082\n \u2192 f\u2082 \u25cb g\u2082 \u2261h j\n \u2192 f\u2081 \u25cb g\u2081 \u2261h j\n\u2261h-subst-\u25cb {U , X , _ , _ , \u03b1}\n {V , Y , _ , _ , \u03b2}\n {W , Z , _ , _ , \u03b3}\n {f\u2081 , F\u2081 , _}\n {f\u2082 , F\u2082 , _}\n {g\u2081 , G\u2081 , _}\n {g\u2082 , G\u2082 , _}\n {j , J , _}\n (p\u2085 , p\u2086) (p\u2087 , p\u2088) (p\u2089 , p\u2081\u2080) rewrite p\u2085 | p\u2086 | p\u2087 | p\u2088 | p\u2089 | p\u2081\u2080 = refl , refl\n\n\u25cb-assoc : \u2200{A B C D}{f : Hom A B}{g : Hom B C}{h : Hom C D}\n \u2192 f \u25cb (g \u25cb h) \u2261h (f \u25cb g) \u25cb h\n\u25cb-assoc {U , X , _ , _ , \u03b1}{V , Y , _ , _ , \u03b2}{W , Z , _ , _ , \u03b3}{S , T , _ , _ , \u03b9}\n {f , F , _}{g , G , _}{h , H , _} = refl , refl\n\n\u25cb-idl : \u2200{A B}{f : Hom A B} \u2192 id \u25cb f \u2261h f\n\u25cb-idl {U , X , _ , _ , _}{V , Y , _ , _ , _}{f , F , _} = refl , refl\n\n\u25cb-idr : \u2200{A B}{f : Hom A B} \u2192 f \u25cb id \u2261h f\n\u25cb-idr {U , X , _ , _ , _}{V , Y , _ , _ , _}{f , F , _} = refl , refl\n\n-- The tensor functor: \u2297\n_\u2297\u1d63_ : \u2200{U X V Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 ((U \u00d7 V) \u2192 (X \u00d7 Y) \u2192 Set)\n_\u2297\u1d63_ \u03b1 \u03b2 (u , v) (x , y) = (\u03b1 u x) \u00d7 (\u03b2 v y)\n\n_\u2297\u2092_ : (A B : Obj) \u2192 Obj\n(U , X , m\u2081 , n\u2081 , \u03b1) \u2297\u2092 (V , Y , m\u2082 , n\u2082 , \u03b2) = ((U \u00d7 V) , (X \u00d7 Y) , trans-\u00d7 m\u2081 m\u2082 , trans-\u00d7 n\u2081 n\u2082 , \u03b1 \u2297\u1d63 \u03b2)\n\nF\u2297 : \u2200{Z T V X U Y : Set}{F : U \u2192 Z \u2192 X}{G : V \u2192 T \u2192 Y} \u2192 (U \u00d7 V) \u2192 (Z \u00d7 T) \u2192 (X \u00d7 Y)\nF\u2297 {F = F}{G} (u , v) (z , t) = F u z , G v t\n \n_\u2297\u2090_ : {A B C D : Obj} \u2192 Hom A C \u2192 Hom B D \u2192 Hom (A \u2297\u2092 B) (C \u2297\u2092 D)\n_\u2297\u2090_ {(U , X , _ , _ , \u03b1)}{(V , Y , _ , _ , \u03b2)}{(W , Z , _ , _ , \u03b3)}{(S , T , _ , _ , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) = \u27e8 f , g \u27e9 , F\u2297 {F = F}{G} , p\u2297\n where\n p\u2297 : {u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Z (\u03bb x \u2192 T)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (F\u2297 {F = F}{G} u y) \u2192 (\u03b3 \u2297\u1d63 \u03b4) (\u27e8 f , g \u27e9 u) y\n p\u2297 {u , v}{z , t} (p\u2083 , p\u2084) = p\u2081 p\u2083 , p\u2082 p\u2084\n\n\u03c0\u2081 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {m\u2081 : \u22a4 \u2192 U}\n \u2192 {n\u2081 : \u22a4 \u2192 X}\n \u2192 {m\u2082 : \u22a4 \u2192 V}\n \u2192 {n\u2082 : \u22a4 \u2192 Y} \n \u2192 Hom ((U , X , m\u2081 , n\u2081 , \u03b1) \u2297\u2092 (V , Y , m\u2082 , n\u2082 , \u03b2)) (U , X , m\u2081 , n\u2081 , \u03b1)\n\u03c0\u2081 {U}{X}{V}{Y}{\u03b1}{\u03b2}{m\u2081}{n\u2081}{m\u2082}{n\u2082} = fst , (\u03bb r x \u2192 x , n\u2082 triv) , cond\n where\n cond : {u : \u03a3 U (\u03bb x \u2192 V)} {y : X} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (y , n\u2082 triv) \u2192 \u03b1 (fst u) y\n cond {u , v}{x} (p\u2081 , p\u2082) = p\u2081\n\n\u03c0\u2082 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {m\u2081 : \u22a4 \u2192 U}\n \u2192 {n\u2081 : \u22a4 \u2192 X}\n \u2192 {m\u2082 : \u22a4 \u2192 V}\n \u2192 {n\u2082 : \u22a4 \u2192 Y} \n \u2192 Hom ((U , X , m\u2081 , n\u2081 , \u03b1) \u2297\u2092 (V , Y , m\u2082 , n\u2082 , \u03b2)) (V , Y , m\u2082 , n\u2082 , \u03b2)\n\u03c0\u2082 {U}{X}{V}{Y}{\u03b1}{\u03b2}{m\u2081}{n\u2081}{m\u2082}{n\u2082} = snd , (\u03bb r y \u2192 n\u2081 triv , y) , cond\n where\n cond : {u : \u03a3 U (\u03bb x \u2192 V)} {y : Y} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (n\u2081 triv , y) \u2192 \u03b2 (snd u) y\n cond {u , v}{y} (p\u2081 , p\u2082) = p\u2082\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"609618e2cc51a1aa0090d436f7f82aa096f048d3","subject":"bit-guessing-game...","message":"bit-guessing-game...\n","repos":"crypto-agda\/crypto-agda","old_file":"bit-guessing-game.agda","new_file":"bit-guessing-game.agda","new_contents":"open import Data.Nat.NP\nopen import Data.Nat.Properties\nopen import Data.Product using (\u2203)\nopen import Relation.Nullary using (\u00ac_)\nopen import Function\nopen import Data.Bool.NP\n\nopen import Data.Bits\n\nopen import flipbased-implem using (Coins; \u21ba; \u2141) -- ; #\u27e8_\u27e9) renaming (count\u21ba to #\u27e8_\u27e9)\nopen import program-distance using (PrgDist; module PrgDist)\nopen import Relation.Binary.PropositionalEquality\n\nmodule bit-guessing-game (prgDist : PrgDist) where\n\nopen PrgDist prgDist\n\nGuessAdv : Coins \u2192 Set\nGuessAdv = \u2141\n\nrunGuess\u2141 : \u2200 {ca} (A : GuessAdv ca) (b : Bit) \u2192 \u2141 ca\nrunGuess\u2141 A _ = A\n\n-- An oracle: an adversary who can break the guessing game.\nOracle : Coins \u2192 Set\nOracle ca = \u2203 (\u03bb (A : GuessAdv ca) \u2192 breaks (runGuess\u2141 A))\n\n-- Any adversary cannot do better than a random guess.\nGuessSec : Coins \u2192 Set\nGuessSec ca = \u2200 (A : GuessAdv ca) \u2192 \u00ac(breaks (runGuess\u2141 A))\n\n#\u1d47 : Bool \u2192 \u2115\n#\u1d47 b = if b then 1 else 0\n\n#\u1d47\u22641 : \u2200 b \u2192 #\u1d47 b \u2264 1\n#\u1d47\u22641 true = s\u2264s z\u2264n\n#\u1d47\u22641 false = z\u2264n\n\n#-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n#-bound zero f = #\u1d47\u22641 (f [])\n#-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n#-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n#-\u2218vnot zero f = refl\n#-\u2218vnot (suc c) f\n rewrite #-\u2218vnot c (f \u2218 0\u2237_)\n | #-\u2218vnot c (f \u2218 1\u2237_) = \u2115\u00b0.+-comm #\u27e8 f \u2218 0\u2237_ \u2218 vnot \u27e9 _\n\n#-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n#-not\u2218 zero f with f []\n... | true = refl\n... | false = refl\n#-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = helper c #\u27e8 not \u2218 f \u2218 0\u2237_ \u27e9 _ (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n where helper : \u2200 c x y \u2192 x \u2264 2^ c \u2192 y \u2264 2^ c \u2192 (2^ c \u2238 x) + (2^ c \u2238 y) \u2261 2^(1 + c) \u2238 (x + y)\n helper zero x y x\u2264 y\u2264 = {!!}\n helper (suc c\u2081) x y x\u2264 y\u2264 = {!helper c\u2081 x y ? ?!}\n","old_contents":"open import Data.Product using (\u2203)\nopen import Relation.Nullary using (\u00ac_)\n\nopen import Data.Bits using (Bit)\n\nopen import flipbased-implem using (Coins; \u21ba)\nopen import program-distance using (PrgDist; module PrgDist)\n\nmodule bit-guessing-game (prgDist : PrgDist) where\n open PrgDist prgDist\n\n GuessAdv : Coins \u2192 Set\n GuessAdv c = \u21ba c Bit\n\n runGuess\u2141 : \u2200 {ca} (A : GuessAdv ca) (b : Bit) \u2192 \u21ba ca Bit\n runGuess\u2141 A _ = A\n\n -- An oracle: an adversary who can break the guessing game.\n Oracle : Coins \u2192 Set\n Oracle ca = \u2203 (\u03bb (A : GuessAdv ca) \u2192 breaks (runGuess\u2141 A))\n\n -- Any adversary cannot do better than a random guess.\n GuessSec : Coins \u2192 Set\n GuessSec ca = \u2200 (A : GuessAdv ca) \u2192 \u00ac(breaks (runGuess\u2141 A))\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"0528ae0789f2799e87305842a858d9c55b1f63da","subject":"Proved the induction principles from\/to data to\/from postulates.","message":"Proved the induction principles from\/to data to\/from postulates.\n\nIgnore-this: ebc245f9526e42279cdf22228edf7ff1\n\ndarcs-hash:20110930172638-3bd4e-1edcece07d36236fa14fd2108032d8a4e808ce4e.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/DataPostulate.agda","new_file":"Draft\/DataPostulate.agda","new_contents":"------------------------------------------------------------------------------\n-- Data and postulates\n------------------------------------------------------------------------------\n\n-- Are the FOTC natural numbers defined by data and postulates the\n-- same?\n\nmodule DataPostulate where\n\n------------------------------------------------------------------------------\npostulate\n D : Set\n zero : D\n succ : D \u2192 D\n\n-- The FOTC natural numbers using data.\ndata N : D \u2192 Set where\n zN : N zero\n sN : \u2200 {n} \u2192 N n \u2192 N (succ n)\n\nindN : (P : D \u2192 Set) \u2192\n P zero \u2192\n (\u2200 {n} \u2192 N n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 N n \u2192 P n\nindN P P0 h zN = P0\nindN P P0 h (sN Nn) = h Nn (indN P P0 h Nn)\n\n-- The FOTC natural numbers using postulates (we choose 'M' by 'Model').\npostulate\n M : D \u2192 Set\n zM : M zero\n sM : \u2200 {n} \u2192 M n \u2192 M (succ n)\n indM : (P : D \u2192 Set) \u2192\n P zero \u2192\n (\u2200 {n} \u2192 M n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 M n \u2192 P n\n\n------------------------------------------------------------------------------\n-- The predicates\n\n-- From the data predicate to the postulate predicate: Using the\n-- induction principle.\nnat-D2P : \u2200 {n} \u2192 N n \u2192 M n\nnat-D2P = indN M zM (\u03bb _ Mn \u2192 sM Mn)\n\n-- From the data predicate to the postulate predicate: Using pattern matching.\nnat-D2P' : \u2200 {n} \u2192 N n \u2192 M n\nnat-D2P' zN = zM\nnat-D2P' (sN Nn) = sM (nat-D2P' Nn)\n\n-- From the postulate predicate to the data predicate\nnat-P2D : \u2200 {n} \u2192 M n \u2192 N n\nnat-P2D = indM N zN (\u03bb _ Nn \u2192 sN Nn)\n\n------------------------------------------------------------------------------\n-- The induction principles\n\n-- The postulate inductive principle from the data inductive principle.\nindD2P : (P : D \u2192 Set) \u2192 P zero \u2192\n (\u2200 {n} \u2192 M n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 M n \u2192 P n\nindD2P P P0 ih Mn = indN P P0 (\u03bb {n} Nn \u2192 ih (nat-D2P Nn)) (nat-P2D Mn)\n\n-- The data inductive principle from the postulate predicate.\nindP2D : (P : D \u2192 Set) \u2192 P zero \u2192\n (\u2200 {n} \u2192 N n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 N n \u2192 P n\nindP2D P P0 ih Nn = indM P P0 (\u03bb {n} Mn \u2192 ih (nat-P2D Mn)) (nat-D2P Nn)\n","old_contents":"------------------------------------------------------------------------------\n-- Data and postulates\n------------------------------------------------------------------------------\n\n-- Are the FOTC natural numbers defined by data and postulates the\n-- same?\n\nmodule DataPostulate where\n\n------------------------------------------------------------------------------\npostulate\n D : Set\n zero : D\n succ : D \u2192 D\n\n-- The FOTC natural numbers using data.\ndata N : D \u2192 Set where\n zN : N zero\n sN : \u2200 {n} \u2192 N n \u2192 N (succ n)\n\nindN : (P : D \u2192 Set) \u2192\n P zero \u2192\n (\u2200 {n} \u2192 N n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 N n \u2192 P n\nindN P P0 h zN = P0\nindN P P0 h (sN Nn) = h Nn (indN P P0 h Nn)\n\n-- The FOTC natural numbers using postulates (we choose 'M' by 'Model').\npostulate\n M : D \u2192 Set\n zM : M zero\n sM : \u2200 {n} \u2192 M n \u2192 M (succ n)\n indM : (P : D \u2192 Set) \u2192\n P zero \u2192\n (\u2200 {n} \u2192 M n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 M n \u2192 P n\n\n------------------------------------------------------------------------------\n-- From data to postulates\n\n-- The predicate: Using the induction principle.\nnat-D2P : \u2200 {n} \u2192 N n \u2192 M n\nnat-D2P = indN M zM (\u03bb _ Mn \u2192 sM Mn)\n\n-- The predicate: Using pattern matching.\nnat-D2P' : \u2200 {n} \u2192 N n \u2192 M n\nnat-D2P' zN = zM\nnat-D2P' (sN Nn) = sM (nat-D2P' Nn)\n\n------------------------------------------------------------------------------\n-- From postulates to data\n\n-- The predicate.\nnat-P2D : \u2200 {n} \u2192 M n \u2192 N n\nnat-P2D = indM N zN (\u03bb _ Nn \u2192 sN Nn)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"49294beab1d87f1535f454d4d3fed622b78f0da0","subject":"Vec: refactor","message":"Vec: refactor\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Vec\/NP.agda","new_file":"lib\/Data\/Vec\/NP.agda","new_contents":"{-# OPTIONS --copatterns #-}\n-- NOTE with-K\nmodule Data.Vec.NP where\n\nopen import Algebra\nopen import Algebra.Structures\nimport Algebra.FunctionProperties\nimport Algebra.FunctionProperties.Eq\nopen import Type hiding (\u2605)\nimport Level as L\nopen import Category.Applicative\nopen import Data.Nat.NP using (\u2115; suc; zero; _+_; _*_; 2*_; module \u2115\u00b0 ; +-interchange ; _\u2264_)\nopen import Data.Nat.Properties using (_+-mono_)\nopen import Data.Fin hiding (_\u2264_) renaming (_+_ to _+\u1da0_)\nopen import Data.Vec using (Vec; []; _\u2237_; head; tail; replicate; tabulate; foldr; _++_; lookup; splitAt; take; drop; sum; _\u2237\u02b3_; concat)\nimport Data.Fin.Properties as F\nopen import Data.Bit\nopen import Data.Bool using (Bool; if_then_else_)\nopen import Data.Product hiding (map; zip; swap) renaming (proj\u2081 to fst; proj\u2082 to snd)\nopen import Function.NP\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_; ap; ap\u2082; idp; _\u2219_; !_)\nimport Data.Vec.Equality\n\nmodule FunVec {a} {A : \u2605 a} where\n _\u2192\u1d5b_ : \u2115 \u2192 \u2115 \u2192 \u2605 a\n i \u2192\u1d5b o = Vec A i \u2192 Vec A o\n\nap-\u2237 : \u2200 {a} {A : \u2605 a} {n}\n {x y : A} {xs ys : Vec A n} \u2192 x \u2261 y \u2192 xs \u2261 ys \u2192 x \u2237 xs \u2261 y \u2237 ys\nap-\u2237 = ap\u2082 _\u2237_\n\nmodule waiting-for-a-fix-in-the-stdlib where\n\n infixl 4 _\u229b_\n\n _\u229b_ : \u2200 {a b n} {A : \u2605 a} {B : \u2605 b} \u2192\n Vec (A \u2192 B) n \u2192 Vec A n \u2192 Vec B n\n _\u229b_ {n = zero} fs xs = []\n _\u229b_ {n = suc n} fs xs = head fs (head xs) \u2237 (tail fs \u229b tail xs)\n\n applicative : \u2200 {a n} \u2192 RawApplicative (\u03bb (A : \u2605 a) \u2192 Vec A n)\n applicative = record\n { pure = replicate\n ; _\u229b_ = _\u229b_\n }\n\n map : \u2200 {a b n} {A : \u2605 a} {B : \u2605 b} \u2192\n (A \u2192 B) \u2192 Vec A n \u2192 Vec B n\n map f xs = replicate f \u229b xs\n\n zipWith : \u2200 {a b c n} {A : \u2605 a} {B : \u2605 b} {C : \u2605 c} \u2192\n (A \u2192 B \u2192 C) \u2192 Vec A n \u2192 Vec B n \u2192 Vec C n\n zipWith _\u2295_ xs ys = replicate _\u2295_ \u229b xs \u229b ys\n\n zip : \u2200 {a b n} {A : \u2605 a} {B : \u2605 b} \u2192\n Vec A n \u2192 Vec B n \u2192 Vec (A \u00d7 B) n\n zip = zipWith _,_\n\n tabulate-\u2218 : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b}\n (f : A \u2192 B) (g : Fin n \u2192 A) \u2192\n tabulate (f \u2218 g) \u2261 map f (tabulate g)\n tabulate-\u2218 {zero} f g = idp\n tabulate-\u2218 {suc n} f g = ap (_\u2237_ _) (tabulate-\u2218 f (g \u2218 suc))\n\n tabulate-ext : \u2200 {n a}{A : \u2605 a}{f g : Fin n \u2192 A} \u2192 f \u2257 g \u2192 tabulate f \u2261 tabulate g\n tabulate-ext {zero} f\u2257g = idp\n tabulate-ext {suc n} f\u2257g = ap-\u2237 (f\u2257g zero) (tabulate-ext (f\u2257g \u2218 suc))\n\n -- map is functorial.\n\n map-id : \u2200 {a n} {A : \u2605 a} \u2192 map id \u2257 id {A = Vec A n}\n map-id [] = idp\n map-id (x \u2237 xs) = ap (_\u2237_ x) (map-id xs)\n\n map-\u2218 : \u2200 {a b c n} {A : \u2605 a} {B : \u2605 b} {C : \u2605 c}\n (f : B \u2192 C) (g : A \u2192 B) \u2192\n _\u2257_ {A = Vec A n} (map (f \u2218 g)) (map f \u2218 map g)\n map-\u2218 f g [] = idp\n map-\u2218 f g (x \u2237 xs) = ap (_\u2237_ (f (g x))) (map-\u2218 f g xs)\n\n map-ext : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} {f g : A \u2192 B} {n} \u2192 f \u2257 g \u2192 map f \u2257 map {n = n} g\n map-ext f\u2257g [] = idp\n map-ext f\u2257g (x \u2237 xs) = ap-\u2237 (f\u2257g x) (map-ext f\u2257g xs)\n\nopen waiting-for-a-fix-in-the-stdlib public\n\nmodule With\u2248\n {a \u2113 \u2113'}{A : \u2605 a}\n (_\u2248_ : A \u2192 A \u2192 \u2605 \u2113)\n {_\u2248\u1d5b_ : \u2200 {n}(xs ys : Vec A n) \u2192 \u2605 \u2113'}\n ([]-cong : [] \u2248\u1d5b [])\n (_\u2237-cong_ : \u2200 {n} {x\u00b9} {xs\u00b9 : Vec A n} {x\u00b2} {xs\u00b2 : Vec A n}\n (x\u00b9\u2248x\u00b2 : x\u00b9 \u2248 x\u00b2) (xs\u00b9\u2248xs\u00b2 : xs\u00b9 \u2248\u1d5b xs\u00b2) \u2192\n (x\u00b9 \u2237 xs\u00b9) \u2248\u1d5b (x\u00b2 \u2237 xs\u00b2))\n where\n open Algebra.FunctionProperties\n\n module _ {_\u2219_ _\u2219\u2219_ : A \u2192 A \u2192 A}\n (assoc : \u2200 x y z \u2192 ((x \u2219 y) \u2219\u2219 z) \u2248 (x \u2219 (y \u2219\u2219 z))) where\n private\n _\u2219\u1d5b_ _\u2219\u2219\u1d5b_ : \u2200 {n}(xs ys : Vec A n) \u2192 Vec A n\n _\u2219\u1d5b_ = zipWith _\u2219_\n _\u2219\u2219\u1d5b_ = zipWith _\u2219\u2219_\n\n zipWith-assoc : \u2200 {n} (xs ys zs : Vec A n) \u2192 ((xs \u2219\u1d5b ys) \u2219\u2219\u1d5b zs) \u2248\u1d5b (xs \u2219\u1d5b (ys \u2219\u2219\u1d5b zs))\n zipWith-assoc [] [] [] = []-cong\n zipWith-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs)\n = assoc x y z \u2237-cong (zipWith-assoc xs ys zs)\n\n module _ {_\u2219_ : A \u2192 A \u2192 A} where\n module _ (\u2219-comm : Commutative _\u2248_ _\u2219_) where\n zipWith-comm : \u2200 {n} \u2192 Commutative _\u2248\u1d5b_ (zipWith {n = n} _\u2219_)\n zipWith-comm [] [] = []-cong\n zipWith-comm (x \u2237 xs) (y \u2237 ys) = \u2219-comm x y \u2237-cong zipWith-comm xs ys\n\n module _ {\u03b5 : A} where\n module _ (\u2219-id-left : LeftIdentity _\u2248_ \u03b5 _\u2219_) where\n zipWith-id-left : \u2200 {n} \u2192 LeftIdentity _\u2248\u1d5b_ (replicate \u03b5) (zipWith {n = n} _\u2219_)\n zipWith-id-left [] = []-cong\n zipWith-id-left (x \u2237 xs) = \u2219-id-left x \u2237-cong zipWith-id-left xs\n\n module _ (\u2219-id-right : RightIdentity _\u2248_ \u03b5 _\u2219_) where\n zipWith-id-right : \u2200 {n} \u2192 RightIdentity _\u2248\u1d5b_ (replicate \u03b5) (zipWith {n = n} _\u2219_)\n zipWith-id-right [] = []-cong\n zipWith-id-right (x \u2237 xs) = \u2219-id-right x \u2237-cong zipWith-id-right xs\n\n module _ {_\u207b\u00b9 : A \u2192 A}\n (inverse : LeftInverse _\u2248_ \u03b5 _\u207b\u00b9 _\u2219_) where\n zipWith-left-inverse : \u2200 {n} \u2192 LeftInverse _\u2248\u1d5b_ (replicate \u03b5) (map _\u207b\u00b9) (zipWith {n = n} _\u2219_)\n zipWith-left-inverse [] = []-cong\n zipWith-left-inverse (x \u2237 xs) = inverse x \u2237-cong zipWith-left-inverse xs\n\n module _ {_\u207b\u00b9 : A \u2192 A}\n (inverse : RightInverse _\u2248_ \u03b5 _\u207b\u00b9 _\u2219_) where\n zipWith-right-inverse : \u2200 {n} \u2192 RightInverse _\u2248\u1d5b_ (replicate \u03b5) (map _\u207b\u00b9) (zipWith {n = n} _\u2219_)\n zipWith-right-inverse [] = []-cong\n zipWith-right-inverse (x \u2237 xs) = inverse x \u2237-cong zipWith-right-inverse xs\n\nmodule WithSetoid {c \u2113} (S : Setoid c \u2113) where\n A = Setoid.Carrier S\n open Setoid S\n V = Vec A\n module V\u2248 = Data.Vec.Equality.Equality S\n open Algebra.FunctionProperties\n open V\u2248 hiding (_\u2248_)\n _\u2248\u1d5b_ : \u2200 {n} \u2192 V n \u2192 V n \u2192 \u2605 _\n xs \u2248\u1d5b ys = V\u2248._\u2248_ xs ys\n\n open With\u2248 _\u2248_ {_\u2248\u1d5b_} []-cong (\u03bb x y \u2192 x \u2237-cong y) public\n\n module _ {f : A \u2192 A} (f-cong : f Preserves _\u2248_ \u27f6 _\u2248_) where\n map-cong : \u2200 {n} \u2192 map {n = n} f Preserves _\u2248\u1d5b_ \u27f6 _\u2248\u1d5b_\n map-cong []-cong = []-cong\n map-cong (x\u2248y \u2237-cong xs\u2248ys) = f-cong x\u2248y \u2237-cong map-cong xs\u2248ys\n\n module _ {f : A \u2192 A \u2192 A} (f-cong : f Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_) where\n zipWith-cong : \u2200 {n} \u2192 zipWith {n = n} f Preserves\u2082 _\u2248\u1d5b_ \u27f6 _\u2248\u1d5b_ \u27f6 _\u2248\u1d5b_\n zipWith-cong []-cong []-cong = []-cong\n zipWith-cong (x\u2248y \u2237-cong xs\u2248ys) (z\u2248t \u2237-cong zs\u2248ts) = f-cong x\u2248y z\u2248t \u2237-cong zipWith-cong xs\u2248ys zs\u2248ts\n\n\u2237= : \u2200 {a}{A : \u2605 a}{n x} {xs : Vec A n} {y} {ys : Vec A n}\n (p : x \u2261 y) (q : xs \u2261 ys) \u2192\n x \u2237 xs \u2261 y \u2237 ys\n\u2237= \u2261.refl \u2261.refl = \u2261.refl\n\nmodule With\u2261 {a}{A : \u2605 a} where\n open With\u2248 (_\u2261_ {A = A}) {_\u2261_} idp (\u03bb x\u00b9\u2248x\u00b2 xs\u00b9\u2248xs\u00b2 \u2192 \u2237= x\u00b9\u2248x\u00b2 xs\u00b9\u2248xs\u00b2) public\n\nmodule LiftSemigroup {c \u2113} (Sg : Semigroup c \u2113) where\n module Sg = Semigroup Sg\n open WithSetoid Sg.setoid public\n\n _\u2219\u1d5b_ : \u2200 {n} \u2192 V n \u2192 V n \u2192 V n\n _\u2219\u1d5b_ = zipWith Sg._\u2219_\n\n -- this should be in Data.Vec.Equality\n isEquivalence : \u2200 {n} \u2192 IsEquivalence (_\u2248\u1d5b_ {n})\n isEquivalence = record { refl = \u03bb {xs} \u2192 V\u2248.refl xs\n ; sym = V\u2248.sym ; trans = V\u2248.trans }\n\n isSemigroup : \u2200 {n} \u2192 IsSemigroup (_\u2248\u1d5b_ {n}) _\u2219\u1d5b_\n isSemigroup = record { isEquivalence = isEquivalence\n ; assoc = zipWith-assoc Sg.assoc\n ; \u2219-cong = zipWith-cong Sg.\u2219-cong }\n\n semigroup : \u2200 n \u2192 Semigroup c (\u2113 L.\u2294 c)\n semigroup n = record { isSemigroup = isSemigroup {n} }\n\nmodule LiftMonoid {c \u2113} (M : Monoid c \u2113) where\n module M = Monoid M\n open LiftSemigroup M.semigroup public\n\n \u03b5\u1d5b : \u2200 {n} \u2192 V n\n \u03b5\u1d5b = replicate M.\u03b5\n\n isMonoid : \u2200 {n} \u2192 IsMonoid (_\u2248\u1d5b_ {n}) _\u2219\u1d5b_ \u03b5\u1d5b\n isMonoid = record { isSemigroup = isSemigroup\n ; identity = zipWith-id-left (fst M.identity)\n , zipWith-id-right (snd M.identity) }\n\n monoid : \u2115 \u2192 Monoid c (\u2113 L.\u2294 c)\n monoid n = record { isMonoid = isMonoid {n} }\n\nmodule LiftCommutativeMonoid {c \u2113} (CM : CommutativeMonoid c \u2113) where\n module CM = CommutativeMonoid CM\n open LiftMonoid CM.monoid public\n\n isCommutativeMonoid : \u2200 {n} \u2192 IsCommutativeMonoid (_\u2248\u1d5b_ {n}) _\u2219\u1d5b_ \u03b5\u1d5b\n isCommutativeMonoid =\n record { isSemigroup = isSemigroup\n ; identity\u02e1 = zipWith-id-left CM.identity\u02e1\n ; comm = zipWith-comm CM.comm }\n\n commutative-monoid : \u2115 \u2192 CommutativeMonoid c (\u2113 L.\u2294 c)\n commutative-monoid n = record { isCommutativeMonoid = isCommutativeMonoid {n} }\n\nmodule LiftGroup {c \u2113} (G : Group c \u2113) where\n module G = Group G\n open LiftMonoid G.monoid public\n\n _\u207b\u00b9\u1d5b : \u2200 {n} \u2192 V n \u2192 V n\n _\u207b\u00b9\u1d5b = map G._\u207b\u00b9\n\n isGroup : \u2200 {n} \u2192 IsGroup (_\u2248\u1d5b_ {n}) _\u2219\u1d5b_ \u03b5\u1d5b _\u207b\u00b9\u1d5b\n isGroup = record { isMonoid = isMonoid\n ; inverse = (zipWith-left-inverse (fst G.inverse))\n , (zipWith-right-inverse (snd G.inverse))\n ; \u207b\u00b9-cong = map-cong G.\u207b\u00b9-cong }\n\n group : \u2115 \u2192 Group c _\n group n = record { isGroup = isGroup {n} }\n\nmodule LiftAbelianGroup {c \u2113} (AG : AbelianGroup c \u2113) where\n module AG = AbelianGroup AG\n open LiftGroup AG.group public\n\n isAbelianGroup : \u2200 {n} \u2192 IsAbelianGroup (_\u2248\u1d5b_ {n}) _\u2219\u1d5b_ \u03b5\u1d5b _\u207b\u00b9\u1d5b\n isAbelianGroup = record { isGroup = isGroup ; comm = zipWith-comm AG.comm }\n\n abelianGroup : \u2115 \u2192 AbelianGroup c _\n abelianGroup n = record { isAbelianGroup = isAbelianGroup {n} }\n\nopen Algebra.FunctionProperties.Eq.Implicits\n\n-- Trying to get rid of the foldl in the definition of reverse and\n-- without using equations on natural numbers.\n-- In the end that's not very convincing.\nmodule Alternative-Reverse where\n rev-+ : \u2115 \u2192 \u2115 \u2192 \u2115\n rev-+ zero = id\n rev-+ (suc x) = rev-+ x \u2218 suc\n\n rev-app : \u2200 {a} {A : \u2605 a} {m n} \u2192\n Vec A n \u2192 Vec A m \u2192 Vec A (rev-+ n m)\n rev-app [] = id\n rev-app (x \u2237 xs) = rev-app xs \u2218 _\u2237_ x\n\n rev-aux : \u2200 {a} {A : \u2605 a} {m} n \u2192\n Vec A (rev-+ n zero) \u2192\n (\u2200 {m} \u2192 A \u2192 Vec A (rev-+ n m) \u2192 Vec A (rev-+ n (suc m))) \u2192\n Vec A m \u2192 Vec A (rev-+ n m)\n rev-aux m acc op [] = acc\n rev-aux m acc op (x \u2237 xs) = rev-aux (suc m) (op x acc) op xs\n\n alt-reverse : \u2200 {a n} {A : \u2605 a} \u2192 Vec A n \u2192 Vec A n\n alt-reverse = rev-aux 0 [] _\u2237_\n\nvuncurry : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b} (f : A \u2192 Vec A n \u2192 B) \u2192 Vec A (1 + n) \u2192 B\nvuncurry f (x \u2237 xs) = f x xs\n\ncount\u1da0 : \u2200 {n a} {A : \u2605 a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 Fin (suc n)\ncount\u1da0 pred = foldr (Fin \u2218 suc) (\u03bb x \u2192 if pred x then suc else inject\u2081) zero\n\ncount : \u2200 {n a} {A : \u2605 a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 \u2115\ncount pred = to\u2115 \u2218 count\u1da0 pred\n\ncount-\u2218 : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b} (f : A \u2192 B) (pred : B \u2192 Bool) \u2192\n count {n} (pred \u2218 f) \u2257 count pred \u2218 map f\ncount-\u2218 f pred [] = idp\ncount-\u2218 f pred (x \u2237 xs) with pred (f x)\n... | 1b rewrite count-\u2218 f pred xs = idp\n... | 0b rewrite F.inject\u2081-lemma (count\u1da0 pred (map f xs))\n | F.inject\u2081-lemma (count\u1da0 (pred \u2218 f) xs)\n | count-\u2218 f pred xs = idp\n\nRewireTbl : (i o : \u2115) \u2192 \u2605\u2080\nRewireTbl i o = Vec (Fin i) o\n\nsum-\u2237\u02b3 : \u2200 {n} x (xs : Vec \u2115 n) \u2192 sum (xs \u2237\u02b3 x) \u2261 sum xs + x\nsum-\u2237\u02b3 x [] = \u2115\u00b0.+-comm x 0\nsum-\u2237\u02b3 x (x\u2081 \u2237 xs) = ap (_+_ x\u2081) (sum-\u2237\u02b3 x xs) \u2219 !(\u2115\u00b0.+-assoc x\u2081 (sum xs) x)\n\nsum-distrib\u02e1 : \u2200 {A : \u2605\u2080} {n} f k (xs : Vec A n) \u2192 sum (map (\u03bb x \u2192 k * f x) xs) \u2261 k * sum (map f xs)\nsum-distrib\u02e1 f k [] = \u2115\u00b0.*-comm 0 k\nsum-distrib\u02e1 f k (x \u2237 xs) rewrite sum-distrib\u02e1 f k xs = !(fst \u2115\u00b0.distrib k _ _)\n\nsum-linear : \u2200 {A : \u2605\u2080} {n} f g (xs : Vec A n) \u2192 sum (map (\u03bb x \u2192 f x + g x) xs) \u2261 sum (map f xs) + sum (map g xs)\nsum-linear f g [] = idp\nsum-linear f g (x \u2237 xs) rewrite sum-linear f g xs = +-interchange (f x) (g x) (sum (map f xs)) (sum (map g xs))\n\nsum-mono : \u2200 {A : \u2605\u2080} {n f g} (mono : \u2200 x \u2192 f x \u2264 g x)(xs : Vec A n) \u2192 sum (map f xs) \u2264 sum (map g xs)\nsum-mono f\u2264\u00b0g [] = Data.Nat.NP.z\u2264n\nsum-mono f\u2264\u00b0g (x \u2237 xs) = f\u2264\u00b0g x +-mono sum-mono f\u2264\u00b0g xs\n\nmodule _ {a} {A : \u2605 a} where\n -- Exchange elements at positions 0 and 1 of a given vector\n -- (this only apply if the vector is long enough).\n 0\u21941 : \u2200 {n} \u2192 Vec A n \u2192 Vec A n\n 0\u21941 (x\u2080 \u2237 x\u2081 \u2237 xs) = x\u2081 \u2237 x\u2080 \u2237 xs\n 0\u21941 xs = xs\n\nmodule _ {a} {A : \u2605 a} where\n count-++ : \u2200 {m n a} {A : \u2605 a} (pred : A \u2192 Bool) (xs : Vec A m) (ys : Vec A n)\n \u2192 count pred (xs ++ ys) \u2261 count pred xs + count pred ys\n count-++ pred [] ys = idp\n count-++ pred (x \u2237 xs) ys with pred x\n ... | 1b rewrite count-++ pred xs ys = idp\n ... | 0b rewrite F.inject\u2081-lemma (count\u1da0 pred (xs ++ ys))\n | F.inject\u2081-lemma (count\u1da0 pred xs) | count-++ pred xs ys = idp\n\n ext-count\u1da0 : \u2200 {n a} {A : \u2605 a} {f g : A \u2192 Bool} \u2192 f \u2257 g \u2192 (xs : Vec A n) \u2192 count\u1da0 f xs \u2261 count\u1da0 g xs\n ext-count\u1da0 f\u2257g [] = idp\n ext-count\u1da0 f\u2257g (x \u2237 xs) rewrite ext-count\u1da0 f\u2257g xs | f\u2257g x = idp\n\n filter : \u2200 {n a} {A : \u2605 a} (pred : A \u2192 Bool) (xs : Vec A n) \u2192 Vec A (count pred xs)\n filter pred [] = []\n filter pred (x \u2237 xs) with pred x\n ... | 1b = x \u2237 filter pred xs\n ... | 0b rewrite F.inject\u2081-lemma (count\u1da0 pred xs) = filter pred xs\n\n transpose : \u2200 {m n a} {A : \u2605 a} \u2192 Vec (Vec A m) n \u2192 Vec (Vec A n) m\n transpose [] = replicate []\n transpose (xs \u2237 xss) = zipWith _\u2237_ xs (transpose xss)\n\n vap : \u2200 {m a b} {A : \u2605 a} {B : \u2605 b} (f : Vec A m \u2192 B)\n \u2192 \u2200 {n} \u2192 Vec (Vec A n) m \u2192 Vec B n\n vap f = map f \u2218 transpose\n\n infixl 2 _\u203c_\n _\u203c_ : \u2200 {n a} {A : \u2605 a} \u2192 Vec A n \u2192 Fin n \u2192 A\n _\u203c_ = flip lookup\n\n \u03b7 : \u2200 {n a} {A : \u2605 a} \u2192 Vec A n \u2192 Vec A n\n \u03b7 = tabulate \u2218 _\u203c_\n\n \u03b7\u2032 : \u2200 {n a} {A : \u2605 a} \u2192 Vec A n \u2192 Vec A n\n \u03b7\u2032 {zero} = \u03bb _ \u2192 []\n \u03b7\u2032 {suc n} = \u03bb xs \u2192 head xs \u2237 \u03b7 (tail xs)\n\n shallow-\u03b7 : \u2200 {n a} {A : \u2605 a} (xs : Vec A (1 + n)) \u2192 xs \u2261 head xs \u2237 tail xs\n shallow-\u03b7 (x \u2237 xs) = idp\n\n uncons : \u2200 {n a} {A : \u2605 a} \u2192 Vec A (1 + n) \u2192 (A \u00d7 Vec A n)\n uncons (x \u2237 xs) = x , xs\n\n \u2237-uncons : \u2200 {n a} {A : \u2605 a} (xs : Vec A (1 + n)) \u2192 uncurry _\u2237_ (uncons xs) \u2261 xs\n \u2237-uncons (x \u2237 xs) = idp\n\n splitAt\u2032 : \u2200 m {n} \u2192 Vec A (m + n) \u2192 Vec A m \u00d7 Vec A n\n splitAt\u2032 m xs = case splitAt m xs of \u03bb { (ys , zs , _) \u2192 (ys , zs) }\n\n group : \u2200 n k \u2192 Vec A (n * k) \u2192 Vec (Vec A k) n\n group zero k v = []\n group (suc n) k v = take k v \u2237 group n k (drop k v)\n\n map2* : \u2200 m n (f : Vec A m \u2192 Vec A n) \u2192 Vec A (2* m) \u2192 Vec A (2* n)\n map2* m _ f xs = f (take m xs) ++ f (drop m xs)\n\n map* : \u2200 k {m n}(f : Vec A m \u2192 Vec A n) \u2192 Vec A (k * m) \u2192 Vec A (k * n)\n map* k f xs = concat (map f (group k _ xs))\n\n take2* : \u2200 n \u2192 Bit \u2192 Vec A (2* n) \u2192 Vec A n\n take2* n 0b v = take n v\n take2* n 1b v = drop n v\n\n ++-decomp : \u2200 {m n} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2261 (zs ++ ts) \u2192 (xs \u2261 zs \u00d7 ys \u2261 ts)\n ++-decomp {zero} {xs = []} {[]} p = idp , p\n ++-decomp {suc m} {xs = x \u2237 xs} {z \u2237 zs} eq with ++-decomp {m} {xs = xs} {zs} (ap tail eq)\n ... | (q\u2081 , q\u2082) = (ap-\u2237 (ap head eq) q\u2081) , q\u2082\n\n ++-inj\u2081 : \u2200 {m n} {xs ys : Vec A m} {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 xs \u2261 ys\n ++-inj\u2081 eq = fst (++-decomp eq)\n\n ++-inj\u2082 : \u2200 {m n} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 zs \u2261 ts\n ++-inj\u2082 xs ys eq = snd (++-decomp {xs = xs} {zs = ys} eq)\n\n take-\u2237 : \u2200 {m} n x (xs : Vec A (n + m)) \u2192 take (suc n) (x \u2237 xs) \u2261 x \u2237 take n xs\n take-\u2237 n x xs with splitAt n xs\n take-\u2237 _ _ ._ | _ , _ , \u2261.refl = \u2261.refl\n\n drop-\u2237 : \u2200 {m} n x (xs : Vec A (n + m)) \u2192 drop (suc n) (x \u2237 xs) \u2261 drop n xs\n drop-\u2237 n x xs with splitAt n xs\n drop-\u2237 _ _ ._ | _ , _ , \u2261.refl = \u2261.refl\n\n take-++ : \u2200 m {n} (xs : Vec A m) (ys : Vec A n) \u2192 take m (xs ++ ys) \u2261 xs\n take-++ m xs ys with xs ++ ys | \u2261.inspect (_++_ xs) ys\n ... | zs | eq with splitAt m zs\n take-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | \u2261.[ eq ] | xs , ys , \u2261.refl = !(++-inj\u2081 eq)\n\n drop-++ : \u2200 m {n} (xs : Vec A m) (ys : Vec A n) \u2192 drop m (xs ++ ys) \u2261 ys\n drop-++ m xs ys with xs ++ ys | \u2261.inspect (_++_ xs) ys\n ... | zs | eq with splitAt m zs\n drop-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | \u2261.[ eq ] | xs , ys , \u2261.refl = !(++-inj\u2082 xs\u2081 xs eq)\n\n take-drop-lem : \u2200 m {n} (xs : Vec A (m + n)) \u2192 take m xs ++ drop m xs \u2261 xs\n take-drop-lem m xs with splitAt m xs\n take-drop-lem m .(ys ++ zs) | ys , zs , \u2261.refl = \u2261.refl\n\n take-them-all : \u2200 n (xs : Vec A (n + 0)) \u2192 take n xs ++ [] \u2261 xs\n take-them-all n xs with splitAt n xs\n take-them-all n .(ys ++ []) | ys , [] , \u2261.refl = \u2261.refl\n\n drop\u2032 : \u2200 m {n} \u2192 Vec A (m + n) \u2192 Vec A n\n drop\u2032 zero = id\n drop\u2032 (suc m) = drop\u2032 m \u2218 tail\n\n drop\u2032-spec : \u2200 m {n} \u2192 drop\u2032 m {n} \u2257 drop m {n}\n drop\u2032-spec zero _ = idp\n drop\u2032-spec (suc m) (x \u2237 xs) = drop\u2032-spec m xs \u2219 !(drop-\u2237 m x xs)\n\n take\u2032 : \u2200 m {n} \u2192 Vec A (m + n) \u2192 Vec A m\n take\u2032 zero _ = []\n take\u2032 (suc m) xs = head xs \u2237 take\u2032 m (tail xs)\n\n take\u2032-spec : \u2200 m {n} \u2192 take\u2032 m {n} \u2257 take m {n}\n take\u2032-spec zero xs = idp\n take\u2032-spec (suc m) (x \u2237 xs) = ap (_\u2237_ x) (take\u2032-spec m xs) \u2219 !(take-\u2237 m x xs)\n\n swap : \u2200 m {n} \u2192 Vec A (m + n) \u2192 Vec A (n + m)\n swap m xs = drop m xs ++ take m xs\n\n swap-++ : \u2200 m {n} (xs : Vec A m) (ys : Vec A n) \u2192 swap m (xs ++ ys) \u2261 ys ++ xs\n swap-++ m xs ys = ap\u2082 _++_ (drop-++ m xs ys) (take-++ m xs ys)\n\n rewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 Vec A i \u2192 Vec A o\n rewire f v = tabulate (_\u203c_ v \u2218 f)\n\n take-drop= : \u2200 m {n} (xs ys : Vec A (m + n))\n \u2192 take m xs \u2261 take m ys\n \u2192 drop m xs \u2261 drop m ys\n \u2192 xs \u2261 ys\n take-drop= m xs ys take= drop= =\n ! take-drop-lem m xs \u2219 ap\u2082 _++_ take= drop= \u2219 take-drop-lem m ys\n\n rewireTbl : \u2200 {a i o} {A : \u2605 a} \u2192 RewireTbl i o \u2192 Vec A i \u2192 Vec A o\n rewireTbl tbl v = map (_\u203c_ v) tbl\n\n on\u1d62 : \u2200 {a} {A : \u2605 a} (f : A \u2192 A) {n} (i : Fin n) \u2192 Vec A n \u2192 Vec A n\n on\u1d62 f zero (x \u2237 xs) = f x \u2237 xs\n on\u1d62 f (suc i) (x \u2237 xs) = x \u2237 on\u1d62 f i xs\n\n \u229b-dist-0\u21941 : \u2200 {n}(fs : Vec (Endo A) n) xs \u2192 0\u21941 fs \u229b 0\u21941 xs \u2261 0\u21941 (fs \u229b xs)\n \u229b-dist-0\u21941 _ [] = idp\n \u229b-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = idp\n \u229b-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = idp\n\n map-tail : \u2200 {m n} \u2192 (Vec A m \u2192 Vec A n) \u2192 Vec A (suc m) \u2192 Vec A (suc n)\n map-tail f (x \u2237 xs) = x \u2237 f xs\n\n map-tail-id : \u2200 {n} \u2192 map-tail id \u2257 id {A = Vec A (suc n)}\n map-tail-id (x \u2237 xs) = idp\n\n map-tail\u2218map-tail : \u2200 {m n o}\n (f : Vec A o \u2192 Vec A m)\n (g : Vec A n \u2192 Vec A o)\n \u2192 map-tail f \u2218 map-tail g \u2257 map-tail (f \u2218 g)\n map-tail\u2218map-tail f g (x \u2237 xs) = idp\n\n map-tail-\u2257 : \u2200 {m n}(f g : Vec A m \u2192 Vec A n) \u2192 f \u2257 g \u2192 map-tail f \u2257 map-tail g\n map-tail-\u2257 f g f\u2257g (x \u2237 xs) = ap (_\u2237_ x) (f\u2257g xs)\n\n dup : \u2200 {n} \u2192 Vec A n \u2192 Vec A (n + n)\n dup xs = xs ++ xs\n\n dup-inj : \u2200 {n} \u2192 Injective (dup {n})\n dup-inj = ++-inj\u2081\n\n rot\u2081 : \u2200 {n} \u2192 Vec A n \u2192 Vec A n\n rot\u2081 [] = []\n rot\u2081 (x \u2237 xs) = xs \u2237\u02b3 x\n\n rot : \u2200 {n} \u2192 \u2115 \u2192 Vec A n \u2192 Vec A n\n rot zero xs = xs\n rot (suc n) xs = rot n (rot\u2081 xs)\n\n map-\u2237\u02b3 : \u2200 {n}(f : A \u2192 \u2115) x (xs : Vec A n) \u2192 map f (xs \u2237\u02b3 x) \u2261 map f xs \u2237\u02b3 f x\n map-\u2237\u02b3 f x [] = idp\n map-\u2237\u02b3 f x (_ \u2237 xs) = ap (_\u2237_ _) (map-\u2237\u02b3 f x xs)\n\n sum-map-rot\u2081 : \u2200 {n}(f : A \u2192 \u2115) (xs : Vec A n) \u2192 sum (map f (rot\u2081 xs)) \u2261 sum (map f xs)\n sum-map-rot\u2081 f [] = idp\n sum-map-rot\u2081 f (x \u2237 xs) = ap sum (map-\u2237\u02b3 f x xs)\n \u2219 sum-\u2237\u02b3 (f x) (map f xs)\n \u2219 \u2115\u00b0.+-comm (sum (map f xs)) (f x)\n\nsum-rot\u2081 : \u2200 {n} (xs : Vec \u2115 n) \u2192 sum xs \u2261 sum (rot\u2081 xs)\nsum-rot\u2081 [] = idp\nsum-rot\u2081 (x \u2237 xs) = \u2115\u00b0.+-comm x (sum xs) \u2219 !(sum-\u2237\u02b3 x xs)\n\nopen import Data.Vec public hiding (_\u229b_; zipWith; zip; map; applicative; group)\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --copatterns #-}\n-- NOTE with-K\nmodule Data.Vec.NP where\n\nopen import Algebra\nopen import Algebra.Structures\nimport Algebra.FunctionProperties\nimport Algebra.FunctionProperties.Eq\nopen import Type hiding (\u2605)\nimport Level as L\nopen import Category.Applicative\nopen import Data.Nat.NP using (\u2115; suc; zero; _+_; _*_; module \u2115\u00b0 ; +-interchange ; _\u2264_)\nopen import Data.Nat.Properties using (_+-mono_)\nopen import Data.Fin hiding (_\u2264_) renaming (_+_ to _+\u1da0_)\nopen import Data.Vec using (Vec; []; _\u2237_; head; tail; replicate; tabulate; foldr; _++_; lookup; splitAt; take; drop; sum; _\u2237\u02b3_)\nimport Data.Fin.Properties as F\nopen import Data.Bool\nopen import Data.Product hiding (map; zip; swap) renaming (proj\u2081 to fst; proj\u2082 to snd)\nopen import Function.NP\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_; ap; idp; _\u2219_; !_)\nimport Data.Vec.Equality\n\nmodule FunVec {a} {A : \u2605 a} where\n _\u2192\u1d5b_ : \u2115 \u2192 \u2115 \u2192 \u2605 a\n i \u2192\u1d5b o = Vec A i \u2192 Vec A o\n\nap-\u2237 : \u2200 {a} {A : \u2605 a} {n}\n {x y : A} {xs ys : Vec A n} \u2192 x \u2261 y \u2192 xs \u2261 ys \u2192 x \u2237 xs \u2261 y \u2237 ys\nap-\u2237 = \u2261.ap\u2082 _\u2237_\n\nmodule waiting-for-a-fix-in-the-stdlib where\n\n infixl 4 _\u229b_\n\n _\u229b_ : \u2200 {a b n} {A : \u2605 a} {B : \u2605 b} \u2192\n Vec (A \u2192 B) n \u2192 Vec A n \u2192 Vec B n\n _\u229b_ {n = zero} fs xs = []\n _\u229b_ {n = suc n} fs xs = head fs (head xs) \u2237 (tail fs \u229b tail xs)\n\n applicative : \u2200 {a n} \u2192 RawApplicative (\u03bb (A : \u2605 a) \u2192 Vec A n)\n applicative = record\n { pure = replicate\n ; _\u229b_ = _\u229b_\n }\n\n map : \u2200 {a b n} {A : \u2605 a} {B : \u2605 b} \u2192\n (A \u2192 B) \u2192 Vec A n \u2192 Vec B n\n map f xs = replicate f \u229b xs\n\n zipWith : \u2200 {a b c n} {A : \u2605 a} {B : \u2605 b} {C : \u2605 c} \u2192\n (A \u2192 B \u2192 C) \u2192 Vec A n \u2192 Vec B n \u2192 Vec C n\n zipWith _\u2295_ xs ys = replicate _\u2295_ \u229b xs \u229b ys\n\n zip : \u2200 {a b n} {A : \u2605 a} {B : \u2605 b} \u2192\n Vec A n \u2192 Vec B n \u2192 Vec (A \u00d7 B) n\n zip = zipWith _,_\n\n tabulate-\u2218 : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b}\n (f : A \u2192 B) (g : Fin n \u2192 A) \u2192\n tabulate (f \u2218 g) \u2261 map f (tabulate g)\n tabulate-\u2218 {zero} f g = idp\n tabulate-\u2218 {suc n} f g = ap (_\u2237_ _) (tabulate-\u2218 f (g \u2218 suc))\n\n tabulate-ext : \u2200 {n a}{A : \u2605 a}{f g : Fin n \u2192 A} \u2192 f \u2257 g \u2192 tabulate f \u2261 tabulate g\n tabulate-ext {zero} f\u2257g = idp\n tabulate-ext {suc n} f\u2257g = ap-\u2237 (f\u2257g zero) (tabulate-ext (f\u2257g \u2218 suc))\n\n -- map is functorial.\n\n map-id : \u2200 {a n} {A : \u2605 a} \u2192 map id \u2257 id {A = Vec A n}\n map-id [] = idp\n map-id (x \u2237 xs) = ap (_\u2237_ x) (map-id xs)\n\n map-\u2218 : \u2200 {a b c n} {A : \u2605 a} {B : \u2605 b} {C : \u2605 c}\n (f : B \u2192 C) (g : A \u2192 B) \u2192\n _\u2257_ {A = Vec A n} (map (f \u2218 g)) (map f \u2218 map g)\n map-\u2218 f g [] = idp\n map-\u2218 f g (x \u2237 xs) = ap (_\u2237_ (f (g x))) (map-\u2218 f g xs)\n\n map-ext : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} {f g : A \u2192 B} {n} \u2192 f \u2257 g \u2192 map f \u2257 map {n = n} g\n map-ext f\u2257g [] = idp\n map-ext f\u2257g (x \u2237 xs) = ap-\u2237 (f\u2257g x) (map-ext f\u2257g xs)\n\nopen waiting-for-a-fix-in-the-stdlib public\n\nmodule With\u2248\n {a \u2113 \u2113'}{A : \u2605 a}\n (_\u2248_ : A \u2192 A \u2192 \u2605 \u2113)\n {_\u2248\u1d5b_ : \u2200 {n}(xs ys : Vec A n) \u2192 \u2605 \u2113'}\n ([]-cong : [] \u2248\u1d5b [])\n (_\u2237-cong_ : \u2200 {n} {x\u00b9} {xs\u00b9 : Vec A n} {x\u00b2} {xs\u00b2 : Vec A n}\n (x\u00b9\u2248x\u00b2 : x\u00b9 \u2248 x\u00b2) (xs\u00b9\u2248xs\u00b2 : xs\u00b9 \u2248\u1d5b xs\u00b2) \u2192\n (x\u00b9 \u2237 xs\u00b9) \u2248\u1d5b (x\u00b2 \u2237 xs\u00b2))\n where\n open Algebra.FunctionProperties\n\n module _ {_\u2219_ _\u2219\u2219_ : A \u2192 A \u2192 A}\n (assoc : \u2200 x y z \u2192 ((x \u2219 y) \u2219\u2219 z) \u2248 (x \u2219 (y \u2219\u2219 z))) where\n private\n _\u2219\u1d5b_ _\u2219\u2219\u1d5b_ : \u2200 {n}(xs ys : Vec A n) \u2192 Vec A n\n _\u2219\u1d5b_ = zipWith _\u2219_\n _\u2219\u2219\u1d5b_ = zipWith _\u2219\u2219_\n\n zipWith-assoc : \u2200 {n} (xs ys zs : Vec A n) \u2192 ((xs \u2219\u1d5b ys) \u2219\u2219\u1d5b zs) \u2248\u1d5b (xs \u2219\u1d5b (ys \u2219\u2219\u1d5b zs))\n zipWith-assoc [] [] [] = []-cong\n zipWith-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs)\n = assoc x y z \u2237-cong (zipWith-assoc xs ys zs)\n\n module _ {_\u2219_ : A \u2192 A \u2192 A} where\n module _ (\u2219-comm : Commutative _\u2248_ _\u2219_) where\n zipWith-comm : \u2200 {n} \u2192 Commutative _\u2248\u1d5b_ (zipWith {n = n} _\u2219_)\n zipWith-comm [] [] = []-cong\n zipWith-comm (x \u2237 xs) (y \u2237 ys) = \u2219-comm x y \u2237-cong zipWith-comm xs ys\n\n module _ {\u03b5 : A} where\n module _ (\u2219-id-left : LeftIdentity _\u2248_ \u03b5 _\u2219_) where\n zipWith-id-left : \u2200 {n} \u2192 LeftIdentity _\u2248\u1d5b_ (replicate \u03b5) (zipWith {n = n} _\u2219_)\n zipWith-id-left [] = []-cong\n zipWith-id-left (x \u2237 xs) = \u2219-id-left x \u2237-cong zipWith-id-left xs\n\n module _ (\u2219-id-right : RightIdentity _\u2248_ \u03b5 _\u2219_) where\n zipWith-id-right : \u2200 {n} \u2192 RightIdentity _\u2248\u1d5b_ (replicate \u03b5) (zipWith {n = n} _\u2219_)\n zipWith-id-right [] = []-cong\n zipWith-id-right (x \u2237 xs) = \u2219-id-right x \u2237-cong zipWith-id-right xs\n\n module _ {_\u207b\u00b9 : A \u2192 A}\n (inverse : LeftInverse _\u2248_ \u03b5 _\u207b\u00b9 _\u2219_) where\n zipWith-left-inverse : \u2200 {n} \u2192 LeftInverse _\u2248\u1d5b_ (replicate \u03b5) (map _\u207b\u00b9) (zipWith {n = n} _\u2219_)\n zipWith-left-inverse [] = []-cong\n zipWith-left-inverse (x \u2237 xs) = inverse x \u2237-cong zipWith-left-inverse xs\n\n module _ {_\u207b\u00b9 : A \u2192 A}\n (inverse : RightInverse _\u2248_ \u03b5 _\u207b\u00b9 _\u2219_) where\n zipWith-right-inverse : \u2200 {n} \u2192 RightInverse _\u2248\u1d5b_ (replicate \u03b5) (map _\u207b\u00b9) (zipWith {n = n} _\u2219_)\n zipWith-right-inverse [] = []-cong\n zipWith-right-inverse (x \u2237 xs) = inverse x \u2237-cong zipWith-right-inverse xs\n\nmodule WithSetoid {c \u2113} (S : Setoid c \u2113) where\n A = Setoid.Carrier S\n open Setoid S\n V = Vec A\n module V\u2248 = Data.Vec.Equality.Equality S\n open Algebra.FunctionProperties\n open V\u2248 hiding (_\u2248_)\n _\u2248\u1d5b_ : \u2200 {n} \u2192 V n \u2192 V n \u2192 \u2605 _\n xs \u2248\u1d5b ys = V\u2248._\u2248_ xs ys\n\n open With\u2248 _\u2248_ {_\u2248\u1d5b_} []-cong (\u03bb x y \u2192 x \u2237-cong y) public\n\n module _ {f : A \u2192 A} (f-cong : f Preserves _\u2248_ \u27f6 _\u2248_) where\n map-cong : \u2200 {n} \u2192 map {n = n} f Preserves _\u2248\u1d5b_ \u27f6 _\u2248\u1d5b_\n map-cong []-cong = []-cong\n map-cong (x\u2248y \u2237-cong xs\u2248ys) = f-cong x\u2248y \u2237-cong map-cong xs\u2248ys\n\n module _ {f : A \u2192 A \u2192 A} (f-cong : f Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_) where\n zipWith-cong : \u2200 {n} \u2192 zipWith {n = n} f Preserves\u2082 _\u2248\u1d5b_ \u27f6 _\u2248\u1d5b_ \u27f6 _\u2248\u1d5b_\n zipWith-cong []-cong []-cong = []-cong\n zipWith-cong (x\u2248y \u2237-cong xs\u2248ys) (z\u2248t \u2237-cong zs\u2248ts) = f-cong x\u2248y z\u2248t \u2237-cong zipWith-cong xs\u2248ys zs\u2248ts\n\n\u2237= : \u2200 {a}{A : \u2605 a}{n x} {xs : Vec A n} {y} {ys : Vec A n}\n (p : x \u2261 y) (q : xs \u2261 ys) \u2192\n x \u2237 xs \u2261 y \u2237 ys\n\u2237= \u2261.refl \u2261.refl = \u2261.refl\n\nmodule With\u2261 {a}{A : \u2605 a} where\n open With\u2248 (_\u2261_ {A = A}) {_\u2261_} idp (\u03bb x\u00b9\u2248x\u00b2 xs\u00b9\u2248xs\u00b2 \u2192 \u2237= x\u00b9\u2248x\u00b2 xs\u00b9\u2248xs\u00b2) public\n\nmodule LiftSemigroup {c \u2113} (Sg : Semigroup c \u2113) where\n module Sg = Semigroup Sg\n open WithSetoid Sg.setoid public\n\n _\u2219\u1d5b_ : \u2200 {n} \u2192 V n \u2192 V n \u2192 V n\n _\u2219\u1d5b_ = zipWith Sg._\u2219_\n\n -- this should be in Data.Vec.Equality\n isEquivalence : \u2200 {n} \u2192 IsEquivalence (_\u2248\u1d5b_ {n})\n isEquivalence = record { refl = \u03bb {xs} \u2192 V\u2248.refl xs\n ; sym = V\u2248.sym ; trans = V\u2248.trans }\n\n isSemigroup : \u2200 {n} \u2192 IsSemigroup (_\u2248\u1d5b_ {n}) _\u2219\u1d5b_\n isSemigroup = record { isEquivalence = isEquivalence\n ; assoc = zipWith-assoc Sg.assoc\n ; \u2219-cong = zipWith-cong Sg.\u2219-cong }\n\n semigroup : \u2200 n \u2192 Semigroup c (\u2113 L.\u2294 c)\n semigroup n = record { isSemigroup = isSemigroup {n} }\n\nmodule LiftMonoid {c \u2113} (M : Monoid c \u2113) where\n module M = Monoid M\n open LiftSemigroup M.semigroup public\n\n \u03b5\u1d5b : \u2200 {n} \u2192 V n\n \u03b5\u1d5b = replicate M.\u03b5\n\n isMonoid : \u2200 {n} \u2192 IsMonoid (_\u2248\u1d5b_ {n}) _\u2219\u1d5b_ \u03b5\u1d5b\n isMonoid = record { isSemigroup = isSemigroup\n ; identity = zipWith-id-left (fst M.identity)\n , zipWith-id-right (snd M.identity) }\n\n monoid : \u2115 \u2192 Monoid c (\u2113 L.\u2294 c)\n monoid n = record { isMonoid = isMonoid {n} }\n\nmodule LiftCommutativeMonoid {c \u2113} (CM : CommutativeMonoid c \u2113) where\n module CM = CommutativeMonoid CM\n open LiftMonoid CM.monoid public\n\n isCommutativeMonoid : \u2200 {n} \u2192 IsCommutativeMonoid (_\u2248\u1d5b_ {n}) _\u2219\u1d5b_ \u03b5\u1d5b\n isCommutativeMonoid =\n record { isSemigroup = isSemigroup\n ; identity\u02e1 = zipWith-id-left CM.identity\u02e1\n ; comm = zipWith-comm CM.comm }\n\n commutative-monoid : \u2115 \u2192 CommutativeMonoid c (\u2113 L.\u2294 c)\n commutative-monoid n = record { isCommutativeMonoid = isCommutativeMonoid {n} }\n\nmodule LiftGroup {c \u2113} (G : Group c \u2113) where\n module G = Group G\n open LiftMonoid G.monoid public\n\n _\u207b\u00b9\u1d5b : \u2200 {n} \u2192 V n \u2192 V n\n _\u207b\u00b9\u1d5b = map G._\u207b\u00b9\n\n isGroup : \u2200 {n} \u2192 IsGroup (_\u2248\u1d5b_ {n}) _\u2219\u1d5b_ \u03b5\u1d5b _\u207b\u00b9\u1d5b\n isGroup = record { isMonoid = isMonoid\n ; inverse = (zipWith-left-inverse (fst G.inverse))\n , (zipWith-right-inverse (snd G.inverse))\n ; \u207b\u00b9-cong = map-cong G.\u207b\u00b9-cong }\n\n group : \u2115 \u2192 Group c _\n group n = record { isGroup = isGroup {n} }\n\nmodule LiftAbelianGroup {c \u2113} (AG : AbelianGroup c \u2113) where\n module AG = AbelianGroup AG\n open LiftGroup AG.group public\n\n isAbelianGroup : \u2200 {n} \u2192 IsAbelianGroup (_\u2248\u1d5b_ {n}) _\u2219\u1d5b_ \u03b5\u1d5b _\u207b\u00b9\u1d5b\n isAbelianGroup = record { isGroup = isGroup ; comm = zipWith-comm AG.comm }\n\n abelianGroup : \u2115 \u2192 AbelianGroup c _\n abelianGroup n = record { isAbelianGroup = isAbelianGroup {n} }\n\n-- Trying to get rid of the foldl in the definition of reverse and\n-- without using equations on natural numbers.\n-- In the end that's not very convincing.\nmodule Alternative-Reverse where\n rev-+ : \u2115 \u2192 \u2115 \u2192 \u2115\n rev-+ zero = id\n rev-+ (suc x) = rev-+ x \u2218 suc\n\n rev-app : \u2200 {a} {A : \u2605 a} {m n} \u2192\n Vec A n \u2192 Vec A m \u2192 Vec A (rev-+ n m)\n rev-app [] = id\n rev-app (x \u2237 xs) = rev-app xs \u2218 _\u2237_ x\n\n rev-aux : \u2200 {a} {A : \u2605 a} {m} n \u2192\n Vec A (rev-+ n zero) \u2192\n (\u2200 {m} \u2192 A \u2192 Vec A (rev-+ n m) \u2192 Vec A (rev-+ n (suc m))) \u2192\n Vec A m \u2192 Vec A (rev-+ n m)\n rev-aux m acc op [] = acc\n rev-aux m acc op (x \u2237 xs) = rev-aux (suc m) (op x acc) op xs\n\n alt-reverse : \u2200 {a n} {A : \u2605 a} \u2192 Vec A n \u2192 Vec A n\n alt-reverse = rev-aux 0 [] _\u2237_\n\nvuncurry : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b} (f : A \u2192 Vec A n \u2192 B) \u2192 Vec A (1 + n) \u2192 B\nvuncurry f (x \u2237 xs) = f x xs\n\ncount\u1da0 : \u2200 {n a} {A : \u2605 a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 Fin (suc n)\ncount\u1da0 pred = foldr (Fin \u2218 suc) (\u03bb x \u2192 if pred x then suc else inject\u2081) zero\n\ncount : \u2200 {n a} {A : \u2605 a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 \u2115\ncount pred = to\u2115 \u2218 count\u1da0 pred\n\ncount-\u2218 : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b} (f : A \u2192 B) (pred : B \u2192 Bool) \u2192\n count {n} (pred \u2218 f) \u2257 count pred \u2218 map f\ncount-\u2218 f pred [] = idp\ncount-\u2218 f pred (x \u2237 xs) with pred (f x)\n... | true rewrite count-\u2218 f pred xs = idp\n... | false rewrite F.inject\u2081-lemma (count\u1da0 pred (map f xs))\n | F.inject\u2081-lemma (count\u1da0 (pred \u2218 f) xs)\n | count-\u2218 f pred xs = idp\n\ncount-++ : \u2200 {m n a} {A : \u2605 a} (pred : A \u2192 Bool) (xs : Vec A m) (ys : Vec A n)\n \u2192 count pred (xs ++ ys) \u2261 count pred xs + count pred ys\ncount-++ pred [] ys = idp\ncount-++ pred (x \u2237 xs) ys with pred x\n... | true rewrite count-++ pred xs ys = idp\n... | false rewrite F.inject\u2081-lemma (count\u1da0 pred (xs ++ ys))\n | F.inject\u2081-lemma (count\u1da0 pred xs) | count-++ pred xs ys = idp\n\next-count\u1da0 : \u2200 {n a} {A : \u2605 a} {f g : A \u2192 Bool} \u2192 f \u2257 g \u2192 (xs : Vec A n) \u2192 count\u1da0 f xs \u2261 count\u1da0 g xs\next-count\u1da0 f\u2257g [] = idp\next-count\u1da0 f\u2257g (x \u2237 xs) rewrite ext-count\u1da0 f\u2257g xs | f\u2257g x = idp\n\nfilter : \u2200 {n a} {A : \u2605 a} (pred : A \u2192 Bool) (xs : Vec A n) \u2192 Vec A (count pred xs)\nfilter pred [] = []\nfilter pred (x \u2237 xs) with pred x\n... | true = x \u2237 filter pred xs\n... | false rewrite F.inject\u2081-lemma (count\u1da0 pred xs) = filter pred xs\n\ntranspose : \u2200 {m n a} {A : \u2605 a} \u2192 Vec (Vec A m) n \u2192 Vec (Vec A n) m\ntranspose [] = replicate []\ntranspose (xs \u2237 xss) = zipWith _\u2237_ xs (transpose xss)\n\nvap : \u2200 {m a b} {A : \u2605 a} {B : \u2605 b} (f : Vec A m \u2192 B)\n \u2192 \u2200 {n} \u2192 Vec (Vec A n) m \u2192 Vec B n\nvap f = map f \u2218 transpose\n\ninfixl 2 _\u203c_\n_\u203c_ : \u2200 {n a} {A : \u2605 a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_\u203c_ = flip lookup\n\n\u03b7 : \u2200 {n a} {A : \u2605 a} \u2192 Vec A n \u2192 Vec A n\n\u03b7 = tabulate \u2218 _\u203c_\n\n\u03b7\u2032 : \u2200 {n a} {A : \u2605 a} \u2192 Vec A n \u2192 Vec A n\n\u03b7\u2032 {zero} = \u03bb _ \u2192 []\n\u03b7\u2032 {suc n} = \u03bb xs \u2192 head xs \u2237 \u03b7 (tail xs)\n\nshallow-\u03b7 : \u2200 {n a} {A : \u2605 a} (xs : Vec A (1 + n)) \u2192 xs \u2261 head xs \u2237 tail xs\nshallow-\u03b7 (x \u2237 xs) = idp\n\nuncons : \u2200 {n a} {A : \u2605 a} \u2192 Vec A (1 + n) \u2192 (A \u00d7 Vec A n)\nuncons (x \u2237 xs) = x , xs\n\n\u2237-uncons : \u2200 {n a} {A : \u2605 a} (xs : Vec A (1 + n)) \u2192 uncurry _\u2237_ (uncons xs) \u2261 xs\n\u2237-uncons (x \u2237 xs) = idp\n\nsplitAt\u2032 : \u2200 {a} {A : \u2605 a} m {n} \u2192 Vec A (m + n) \u2192 Vec A m \u00d7 Vec A n\nsplitAt\u2032 m xs = case splitAt m xs of \u03bb { (ys , zs , _) \u2192 (ys , zs) }\n\n++-decomp : \u2200 {m n a} {A : \u2605 a} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2261 (zs ++ ts) \u2192 (xs \u2261 zs \u00d7 ys \u2261 ts)\n++-decomp {zero} {xs = []} {[]} p = idp , p\n++-decomp {suc m} {xs = x \u2237 xs} {z \u2237 zs} eq with ++-decomp {m} {xs = xs} {zs} (ap tail eq)\n... | (q\u2081 , q\u2082) = (ap-\u2237 (ap head eq) q\u2081) , q\u2082\n\n{-\n\nmodule Here {a} {A : \u2605 a} where\n open Data.Vec.Equality.Equality (\u2261.setoid A)\n \u2248-splitAt : \u2200 {m n} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2248 (zs ++ ts) \u2192 (xs \u2248 zs \u00d7 ys \u2248 ts)\n \u2248-splitAt {zero} {xs = []} {[]} p = []-cong , p\n \u2248-splitAt {suc m} {xs = x \u2237 xs} {z \u2237 zs} (x\u00b9\u2248x\u00b2 \u2237-cong p) with \u2248-splitAt {m} {xs = xs} {zs} p\n ... | (q\u2081 , q\u2082) = x\u00b9\u2248x\u00b2 \u2237-cong q\u2081 , q\u2082\n-}\n\n++-inj\u2081 : \u2200 {m n} {a} {A : \u2605 a} {xs ys : Vec A m} {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 xs \u2261 ys\n++-inj\u2081 eq = fst (++-decomp eq)\n\n++-inj\u2082 : \u2200 {m n} {a} {A : \u2605 a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 zs \u2261 ts\n++-inj\u2082 xs ys eq = snd (++-decomp {xs = xs} {zs = ys} eq)\n\ntake-\u2237 : \u2200 {m a} {A : \u2605 a} n x (xs : Vec A (n + m)) \u2192 take (suc n) (x \u2237 xs) \u2261 x \u2237 take n xs\ntake-\u2237 n x xs with splitAt n xs\ntake-\u2237 _ _ ._ | _ , _ , \u2261.refl = \u2261.refl\n\ndrop-\u2237 : \u2200 {m a} {A : \u2605 a} n x (xs : Vec A (n + m)) \u2192 drop (suc n) (x \u2237 xs) \u2261 drop n xs\ndrop-\u2237 n x xs with splitAt n xs\ndrop-\u2237 _ _ ._ | _ , _ , \u2261.refl = \u2261.refl\n\ntake-++ : \u2200 m {n} {a} {A : \u2605 a} (xs : Vec A m) (ys : Vec A n) \u2192 take m (xs ++ ys) \u2261 xs\ntake-++ m xs ys with xs ++ ys | \u2261.inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ntake-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | \u2261.[ eq ] | xs , ys , \u2261.refl = !(++-inj\u2081 eq)\n\ndrop-++ : \u2200 m {n} {a} {A : \u2605 a} (xs : Vec A m) (ys : Vec A n) \u2192 drop m (xs ++ ys) \u2261 ys\ndrop-++ m xs ys with xs ++ ys | \u2261.inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ndrop-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | \u2261.[ eq ] | xs , ys , \u2261.refl = !(++-inj\u2082 xs\u2081 xs eq)\n\ntake-drop-lem : \u2200 m {n} {a} {A : \u2605 a} (xs : Vec A (m + n)) \u2192 take m xs ++ drop m xs \u2261 xs\ntake-drop-lem m xs with splitAt m xs\ntake-drop-lem m .(ys ++ zs) | ys , zs , \u2261.refl = \u2261.refl\n\ntake-them-all : \u2200 n {a} {A : \u2605 a} (xs : Vec A (n + 0)) \u2192 take n xs ++ [] \u2261 xs\ntake-them-all n xs with splitAt n xs\ntake-them-all n .(ys ++ []) | ys , [] , \u2261.refl = \u2261.refl\n\ndrop\u2032 : \u2200 {a} {A : \u2605 a} m {n} \u2192 Vec A (m + n) \u2192 Vec A n\ndrop\u2032 zero = id\ndrop\u2032 (suc m) = drop\u2032 m \u2218 tail\n\ndrop\u2032-spec : \u2200 {a} {A : \u2605 a} m {n} \u2192 drop\u2032 {A = A} m {n} \u2257 drop m {n}\ndrop\u2032-spec zero _ = idp\ndrop\u2032-spec (suc m) (x \u2237 xs) = drop\u2032-spec m xs \u2219 !(drop-\u2237 m x xs)\n\ntake\u2032 : \u2200 {a} {A : \u2605 a} m {n} \u2192 Vec A (m + n) \u2192 Vec A m\ntake\u2032 zero _ = []\ntake\u2032 (suc m) xs = head xs \u2237 take\u2032 m (tail xs)\n\ntake\u2032-spec : \u2200 {a} {A : \u2605 a} m {n} \u2192 take\u2032 {A = A} m {n} \u2257 take m {n}\ntake\u2032-spec zero xs = idp\ntake\u2032-spec (suc m) (x \u2237 xs) = ap (_\u2237_ x) (take\u2032-spec m xs) \u2219 !(take-\u2237 m x xs)\n\nswap : \u2200 m {n} {a} {A : \u2605 a} \u2192 Vec A (m + n) \u2192 Vec A (n + m)\nswap m xs = drop m xs ++ take m xs\n\nswap-++ : \u2200 m {n} {a} {A : \u2605 a} (xs : Vec A m) (ys : Vec A n) \u2192 swap m (xs ++ ys) \u2261 ys ++ xs\nswap-++ m xs ys = \u2261.ap\u2082 _++_ (drop-++ m xs ys) (take-++ m xs ys)\n\nrewire : \u2200 {a i o} {A : \u2605 a} \u2192 (Fin o \u2192 Fin i) \u2192 Vec A i \u2192 Vec A o\nrewire f v = tabulate (_\u203c_ v \u2218 f)\n\nRewireTbl : (i o : \u2115) \u2192 \u2605\u2080\nRewireTbl i o = Vec (Fin i) o\n\nrewireTbl : \u2200 {a i o} {A : \u2605 a} \u2192 RewireTbl i o \u2192 Vec A i \u2192 Vec A o\nrewireTbl tbl v = map (_\u203c_ v) tbl\n\non\u1d62 : \u2200 {a} {A : \u2605 a} (f : A \u2192 A) {n} (i : Fin n) \u2192 Vec A n \u2192 Vec A n\non\u1d62 f zero (x \u2237 xs) = f x \u2237 xs\non\u1d62 f (suc i) (x \u2237 xs) = x \u2237 on\u1d62 f i xs\n\n-- Exchange elements at positions 0 and 1 of a given vector\n-- (this only apply if the vector is long enough).\n0\u21941 : \u2200 {n a} {A : \u2605 a} \u2192 Vec A n \u2192 Vec A n\n0\u21941 (x\u2080 \u2237 x\u2081 \u2237 xs) = x\u2081 \u2237 x\u2080 \u2237 xs\n0\u21941 xs = xs\n\n\u229b-dist-0\u21941 : \u2200 {n a} {A : \u2605 a} (fs : Vec (Endo A) n) xs \u2192 0\u21941 fs \u229b 0\u21941 xs \u2261 0\u21941 (fs \u229b xs)\n\u229b-dist-0\u21941 _ [] = idp\n\u229b-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = idp\n\u229b-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = idp\n\nmap-tail : \u2200 {m n a} {A : \u2605 a} \u2192 (Vec A m \u2192 Vec A n) \u2192 Vec A (suc m) \u2192 Vec A (suc n)\nmap-tail f (x \u2237 xs) = x \u2237 f xs\n\nmap-tail-id : \u2200 {n a} {A : \u2605 a} \u2192 map-tail id \u2257 id {A = Vec A (suc n)}\nmap-tail-id (x \u2237 xs) = idp\n\nmap-tail\u2218map-tail : \u2200 {m n o a} {A : \u2605 a}\n (f : Vec A o \u2192 Vec A m)\n (g : Vec A n \u2192 Vec A o)\n \u2192 map-tail f \u2218 map-tail g \u2257 map-tail (f \u2218 g)\nmap-tail\u2218map-tail f g (x \u2237 xs) = idp\n\nmap-tail-\u2257 : \u2200 {m n a} {A : \u2605 a} (f g : Vec A m \u2192 Vec A n) \u2192 f \u2257 g \u2192 map-tail f \u2257 map-tail g\nmap-tail-\u2257 f g f\u2257g (x \u2237 xs) = ap (_\u2237_ x) (f\u2257g xs)\n\nsum-\u2237\u02b3 : \u2200 {n} x (xs : Vec \u2115 n) \u2192 sum (xs \u2237\u02b3 x) \u2261 sum xs + x\nsum-\u2237\u02b3 x [] = \u2115\u00b0.+-comm x 0\nsum-\u2237\u02b3 x (x\u2081 \u2237 xs) = ap (_+_ x\u2081) (sum-\u2237\u02b3 x xs) \u2219 !(\u2115\u00b0.+-assoc x\u2081 (sum xs) x)\n\nrot\u2081 : \u2200 {n a} {A : \u2605 a} \u2192 Vec A n \u2192 Vec A n\nrot\u2081 [] = []\nrot\u2081 (x \u2237 xs) = xs \u2237\u02b3 x\n\nrot : \u2200 {n a} {A : \u2605 a} \u2192 \u2115 \u2192 Vec A n \u2192 Vec A n\nrot zero xs = xs\nrot (suc n) xs = rot n (rot\u2081 xs)\n\nsum-distrib\u02e1 : \u2200 {A : \u2605\u2080} {n} f k (xs : Vec A n) \u2192 sum (map (\u03bb x \u2192 k * f x) xs) \u2261 k * sum (map f xs)\nsum-distrib\u02e1 f k [] = \u2115\u00b0.*-comm 0 k\nsum-distrib\u02e1 f k (x \u2237 xs) rewrite sum-distrib\u02e1 f k xs = !(fst \u2115\u00b0.distrib k _ _)\n\nsum-linear : \u2200 {A : \u2605\u2080} {n} f g (xs : Vec A n) \u2192 sum (map (\u03bb x \u2192 f x + g x) xs) \u2261 sum (map f xs) + sum (map g xs)\nsum-linear f g [] = idp\nsum-linear f g (x \u2237 xs) rewrite sum-linear f g xs = +-interchange (f x) (g x) (sum (map f xs)) (sum (map g xs))\n\nsum-mono : \u2200 {A : \u2605\u2080} {n f g} (mono : \u2200 x \u2192 f x \u2264 g x)(xs : Vec A n) \u2192 sum (map f xs) \u2264 sum (map g xs)\nsum-mono f\u2264\u00b0g [] = Data.Nat.NP.z\u2264n\nsum-mono f\u2264\u00b0g (x \u2237 xs) = f\u2264\u00b0g x +-mono sum-mono f\u2264\u00b0g xs\n\nsum-rot\u2081 : \u2200 {n} (xs : Vec \u2115 n) \u2192 sum xs \u2261 sum (rot\u2081 xs)\nsum-rot\u2081 [] = idp\nsum-rot\u2081 (x \u2237 xs) = \u2115\u00b0.+-comm x (sum xs) \u2219 !(sum-\u2237\u02b3 x xs)\n\nmap-\u2237\u02b3 : \u2200 {n a} {A : \u2605 a} (f : A \u2192 \u2115) x (xs : Vec A n) \u2192 map f (xs \u2237\u02b3 x) \u2261 map f xs \u2237\u02b3 f x\nmap-\u2237\u02b3 f x [] = idp\nmap-\u2237\u02b3 f x (_ \u2237 xs) = ap (_\u2237_ _) (map-\u2237\u02b3 f x xs)\n\nsum-map-rot\u2081 : \u2200 {n a} {A : \u2605 a} (f : A \u2192 \u2115) (xs : Vec A n) \u2192 sum (map f (rot\u2081 xs)) \u2261 sum (map f xs)\nsum-map-rot\u2081 f [] = idp\nsum-map-rot\u2081 f (x \u2237 xs) = ap sum (map-\u2237\u02b3 f x xs)\n \u2219 sum-\u2237\u02b3 (f x) (map f xs)\n \u2219 \u2115\u00b0.+-comm (sum (map f xs)) (f x)\n\nopen import Data.Vec public hiding (_\u229b_; zipWith; zip; map; applicative)\nopen Algebra.FunctionProperties.Eq.Implicits\n\nmodule _ {a} {A : \u2605 a} where\n dup : \u2200 {n} \u2192 Vec A n \u2192 Vec A (n + n)\n dup xs = xs ++ xs\n\n dup-inj : \u2200 {n} \u2192 Injective (dup {n})\n dup-inj = ++-inj\u2081\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"bb40a5ccf2b5b8b4d1ef8c518f9c6a7c60094079","subject":"cleaning up some names and easy todos","message":"cleaning up some names and easy todos\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"preservation.agda","new_file":"preservation.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nopen import lemmas-consistency\nopen import type-assignment-unicity\n\nmodule preservation where\n -- if d and d' both result from filling the hole in \u03b5 with terms of the\n -- same type, they too have the same type.\n wt-different-fill : \u2200{ \u0394 \u0393 d \u03b5 d1 d2 d' \u03c4 \u03c41 } \u2192\n d == \u03b5 \u27e6 d1 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n d' == \u03b5 \u27e6 d2 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n wt-different-fill FHOuter D1 D2 D3 FHOuter\n with type-assignment-unicity D1 D2\n ... | refl = D3\n wt-different-fill (FHAp1 eps) (TAAp D1 D2) D3 D4 (FHAp1 D5) = TAAp (wt-different-fill eps D1 D3 D4 D5) D2\n wt-different-fill (FHAp2 eps) (TAAp D1 D2) D3 D4 (FHAp2 D5) = TAAp D1 (wt-different-fill eps D2 D3 D4 D5)\n wt-different-fill (FHNEHole eps) (TANEHole x D1 x\u2081) D2 D3 (FHNEHole D4) = TANEHole x (wt-different-fill eps D1 D2 D3 D4) x\u2081\n wt-different-fill (FHCast eps) (TACast D1 x) D2 D3 (FHCast D4) = TACast (wt-different-fill eps D1 D2 D3 D4) x\n wt-different-fill (FHFailedCast x) (TAFailedCast y x\u2081 x\u2082 x\u2083) D3 D4 (FHFailedCast eps) = TAFailedCast (wt-different-fill x y D3 D4 eps) x\u2081 x\u2082 x\u2083\n\n -- if a well typed term results from filling the hole in \u03b5, then the term\n -- that filled the hole is also well typed\n wt-filling : \u2200{ \u03b5 \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u03a3[ \u03c4' \u2208 htyp ] (\u0394 , \u0393 \u22a2 d' :: \u03c4')\n wt-filling TAConst FHOuter = _ , TAConst\n wt-filling (TAVar x\u2081) FHOuter = _ , TAVar x\u2081\n wt-filling (TALam ta) FHOuter = _ , TALam ta\n\n wt-filling (TAAp ta ta\u2081) FHOuter = _ , TAAp ta ta\u2081\n wt-filling (TAAp ta ta\u2081) (FHAp1 eps) = wt-filling ta eps\n wt-filling (TAAp ta ta\u2081) (FHAp2 eps) = wt-filling ta\u2081 eps\n\n wt-filling (TAEHole x x\u2081) FHOuter = _ , TAEHole x x\u2081\n wt-filling (TANEHole x ta x\u2081) FHOuter = _ , TANEHole x ta x\u2081\n wt-filling (TANEHole x ta x\u2081) (FHNEHole eps) = wt-filling ta eps\n wt-filling (TACast ta x) FHOuter = _ , TACast ta x\n wt-filling (TACast ta x) (FHCast eps) = wt-filling ta eps\n wt-filling (TAFailedCast x y z w) FHOuter = _ , TAFailedCast x y z w\n wt-filling (TAFailedCast x x\u2081 x\u2082 x\u2083) (FHFailedCast y) = wt-filling x y\n\n -- replacing a variable in a term with contents of the appropriate type\n -- preserves type and contracts the context\n lem-subst : \u2200{\u0394 \u0393 x \u03c41 d1 \u03c4 d2 } \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 d1 :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 [ d2 \/ x ] d1 :: \u03c4\n lem-subst TAConst D2 = TAConst\n lem-subst {\u0393 = \u0393} {x = x'} (TAVar {x = x} x\u2082) D2 with natEQ x x'\n lem-subst {\u0393 = \u0393} {x = x} {\u03c41 = \u03c41} (TAVar x\u2083) D2 | Inl refl = {!!}\n lem-subst {\u0393 = \u0393} {x = x} {\u03c41 = \u03c41} (TAVar {x = x'} x\u2083) D2 | Inr x\u2082 = {!x\u2208sing!}\n -- ... | qq = TAVar {!!}\n -- with \u0393 x\n -- lem-subst {x = x} (TAVar {x = x'} x\u2083) D2 | Some x\u2081 = {!!}\n -- with natEQ x' x\n -- lem-subst (TAVar xin) D2 | Some x\u2083 | Inl refl = {!!}\n -- lem-subst (TAVar refl) D2 | Some x\u2083 | Inr x\u2082 = {!!}\n -- lem-subst {x = x} (TAVar {x = x'} x\u2082) D2 | None with natEQ x' x\n --lem-subst {x = x} (TAVar x\u2083) D2 | None | Inl refl with natEQ x x\n -- lem-subst (TAVar refl) D2 | None | Inl refl | Inl refl = D2\n -- lem-subst (TAVar x\u2083) D2 | None | Inl refl | Inr x\u2081 = abort (somenotnone (! x\u2083))\n --lem-subst {x = x} (TAVar {x = x'} x\u2083) D2 | None | Inr x\u2082 with natEQ x x'\n -- lem-subst (TAVar x\u2084) D2 | None | Inr x\u2083 | Inl x\u2082 = abort ((flip x\u2083) x\u2082)\n -- lem-subst (TAVar x\u2084) D2 | None | Inr x\u2083 | Inr x\u2082 = abort (somenotnone (! x\u2084))\n lem-subst {\u0393 = \u0393} {x = x} (TALam {x = x'} D1) D2 = TALam {!!}\n lem-subst (TAAp D1 D2) D3 = TAAp (lem-subst D1 D3) (lem-subst D2 D3)\n lem-subst (TAEHole x\u2081 x\u2082) D2 = TAEHole x\u2081 {!!}\n lem-subst (TANEHole x\u2081 D1 x\u2082) D2 = TANEHole x\u2081 (lem-subst D1 D2) {!!}\n lem-subst (TACast D1 x\u2081) D2 = TACast (lem-subst D1 D2) x\u2081\n lem-subst (TAFailedCast D1 x\u2081 x\u2082 x\u2083) D2 = TAFailedCast (lem-subst D1 D2) x\u2081 x\u2082 x\u2083\n\n -- instruction transitions preserve type\n preserve-trans : \u2200{ \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u2192> d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n preserve-trans TAConst ()\n preserve-trans (TAVar x\u2081) ()\n preserve-trans (TALam ta) ()\n preserve-trans (TAAp (TALam ta) ta\u2081) ITLam = lem-subst ta ta\u2081\n preserve-trans (TAAp (TACast ta TCRefl) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 TCRefl)) TCRefl\n preserve-trans (TAAp (TACast ta (TCArr x x\u2081)) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 (~sym x))) x\u2081\n preserve-trans (TAEHole x x\u2081) ()\n preserve-trans (TANEHole x ta x\u2081) ()\n preserve-trans (TACast ta x) (ITCastID) = ta\n preserve-trans (TACast (TACast ta x) x\u2081) (ITCastSucceed x\u2082) = ta\n preserve-trans (TACast ta x) (ITGround (MGArr x\u2081)) = TACast (TACast ta (TCArr TCHole1 TCHole1)) TCHole1\n preserve-trans (TACast ta TCHole2) (ITExpand (MGArr x\u2081)) = TACast (TACast ta TCHole2) (TCArr TCHole2 TCHole2)\n preserve-trans (TACast (TACast ta x) x\u2081) (ITCastFail w y z) = TAFailedCast ta w y z\n preserve-trans (TAFailedCast x y z q) ()\n\n -- this is the main preservation theorem, gluing together the above\n preservation : {\u0394 : hctx} {d d' : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d \u21a6 d' \u2192\n \u0394 , \u2205 \u22a2 d' :: \u03c4\n preservation D (Step x x\u2081 x\u2082)\n with wt-filling D x\n ... | (_ , wt) = wt-different-fill x D wt (preserve-trans wt x\u2081) x\u2082\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nopen import htype-decidable\nopen import lemmas-consistency\nopen import type-assignment-unicity\n\nmodule preservation where\n -- todo: rename\n pres-lem : \u2200{ \u0394 \u0393 d \u03b5 d1 d2 d3 \u03c4 \u03c41 } \u2192\n d == \u03b5 \u27e6 d1 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n d3 == \u03b5 \u27e6 d2 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d3 :: \u03c4\n pres-lem FHOuter D1 D2 D3 FHOuter\n with type-assignment-unicity D1 D2\n ... | refl = D3\n pres-lem (FHAp1 eps) (TAAp D1 D2) D3 D4 (FHAp1 D5) = TAAp (pres-lem eps D1 D3 D4 D5) D2\n pres-lem (FHAp2 eps) (TAAp D1 D2) D3 D4 (FHAp2 D5) = TAAp D1 (pres-lem eps D2 D3 D4 D5)\n pres-lem (FHNEHole eps) (TANEHole x D1 x\u2081) D2 D3 (FHNEHole D4) = TANEHole x (pres-lem eps D1 D2 D3 D4) x\u2081\n pres-lem (FHCast eps) (TACast D1 x) D2 D3 (FHCast D4) = TACast (pres-lem eps D1 D2 D3 D4) x\n pres-lem (FHFailedCast x) (TAFailedCast y x\u2081 x\u2082 x\u2083) D3 D4 (FHFailedCast eps) = TAFailedCast (pres-lem x y D3 D4 eps) x\u2081 x\u2082 x\u2083\n\n -- todo: rename\n pres-lem2 : \u2200{ \u03b5 \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u03a3[ \u03c4' \u2208 htyp ] (\u0394 , \u0393 \u22a2 d' :: \u03c4')\n pres-lem2 TAConst FHOuter = _ , TAConst\n pres-lem2 (TAVar x\u2081) FHOuter = _ , TAVar x\u2081\n pres-lem2 (TALam ta) FHOuter = _ , TALam ta\n\n pres-lem2 (TAAp ta ta\u2081) FHOuter = _ , TAAp ta ta\u2081\n pres-lem2 (TAAp ta ta\u2081) (FHAp1 eps) = pres-lem2 ta eps\n pres-lem2 (TAAp ta ta\u2081) (FHAp2 eps) = pres-lem2 ta\u2081 eps\n\n pres-lem2 (TAEHole x x\u2081) FHOuter = _ , TAEHole x x\u2081\n pres-lem2 (TANEHole x ta x\u2081) FHOuter = _ , TANEHole x ta x\u2081\n pres-lem2 (TANEHole x ta x\u2081) (FHNEHole eps) = pres-lem2 ta eps\n pres-lem2 (TACast ta x) FHOuter = _ , TACast ta x\n pres-lem2 (TACast ta x) (FHCast eps) = pres-lem2 ta eps\n pres-lem2 (TAFailedCast x y z w) FHOuter = _ , TAFailedCast x y z w\n pres-lem2 (TAFailedCast x x\u2081 x\u2082 x\u2083) (FHFailedCast y) = pres-lem2 x y\n\n -- this is the literal contents of the hole in lem3; it might not go\n -- through exactly like this.\n lem-subst : \u2200{\u0394 \u0393 x \u03c41 d1 \u03c4 d2 } \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 d1 :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 [ d2 \/ x ] d1 :: \u03c4\n lem-subst TAConst D2 = TAConst\n lem-subst {\u0393 = \u0393} {x = x'} (TAVar {x = x} x\u2082) D2 with natEQ x x'\n lem-subst {\u0393 = \u0393} {x = x} {\u03c41 = \u03c41} (TAVar x\u2083) D2 | Inl refl = {!!}\n lem-subst {\u0393 = \u0393} {x = x} {\u03c41 = \u03c41} (TAVar {x = x'} x\u2083) D2 | Inr x\u2082 = {!x\u2208sing!}\n -- ... | qq = TAVar {!!}\n -- with \u0393 x\n -- lem-subst {x = x} (TAVar {x = x'} x\u2083) D2 | Some x\u2081 = {!!}\n -- with natEQ x' x\n -- lem-subst (TAVar xin) D2 | Some x\u2083 | Inl refl = {!!}\n -- lem-subst (TAVar refl) D2 | Some x\u2083 | Inr x\u2082 = {!!}\n -- lem-subst {x = x} (TAVar {x = x'} x\u2082) D2 | None with natEQ x' x\n --lem-subst {x = x} (TAVar x\u2083) D2 | None | Inl refl with natEQ x x\n -- lem-subst (TAVar refl) D2 | None | Inl refl | Inl refl = D2\n -- lem-subst (TAVar x\u2083) D2 | None | Inl refl | Inr x\u2081 = abort (somenotnone (! x\u2083))\n --lem-subst {x = x} (TAVar {x = x'} x\u2083) D2 | None | Inr x\u2082 with natEQ x x'\n -- lem-subst (TAVar x\u2084) D2 | None | Inr x\u2083 | Inl x\u2082 = abort ((flip x\u2083) x\u2082)\n -- lem-subst (TAVar x\u2084) D2 | None | Inr x\u2083 | Inr x\u2082 = abort (somenotnone (! x\u2084))\n lem-subst {\u0393 = \u0393} {x = x} (TALam {x = x'} D1) D2 = TALam {!lem-subst D1!}\n lem-subst (TAAp D1 D2) D3 = TAAp (lem-subst D1 D3) (lem-subst D2 D3)\n lem-subst (TAEHole x\u2081 x\u2082) D2 = TAEHole x\u2081 {!!}\n lem-subst (TANEHole x\u2081 D1 x\u2082) D2 = TANEHole x\u2081 (lem-subst D1 D2) {!!}\n lem-subst (TACast D1 x\u2081) D2 = TACast (lem-subst D1 D2) x\u2081\n lem-subst (TAFailedCast D1 x\u2081 x\u2082 x\u2083) D2 = TAFailedCast (lem-subst D1 D2) x\u2081 x\u2082 x\u2083\n\n -- todo: rename\n pres-lem3 : \u2200{ \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u2192> d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n pres-lem3 TAConst ()\n pres-lem3 (TAVar x\u2081) ()\n pres-lem3 (TALam ta) ()\n pres-lem3 (TAAp (TALam ta) ta\u2081) ITLam = lem-subst ta ta\u2081\n pres-lem3 (TAAp (TACast ta TCRefl) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 TCRefl)) TCRefl\n pres-lem3 (TAAp (TACast ta (TCArr x x\u2081)) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 (~sym x))) x\u2081\n pres-lem3 (TAEHole x x\u2081) ()\n pres-lem3 (TANEHole x ta x\u2081) ()\n pres-lem3 (TACast ta x) (ITCastID) = ta\n pres-lem3 (TACast (TACast ta x) x\u2081) (ITCastSucceed x\u2082) = ta\n pres-lem3 (TACast ta x) (ITGround (MGArr x\u2081)) = TACast (TACast ta (TCArr TCHole1 TCHole1)) TCHole1\n pres-lem3 (TACast ta TCHole2) (ITExpand (MGArr x\u2081)) = TACast (TACast ta TCHole2) (TCArr TCHole2 TCHole2)\n pres-lem3 (TACast (TACast ta x) x\u2081) (ITCastFail w y z) = TAFailedCast ta w y z\n pres-lem3 (TAFailedCast x y z q) ()\n\n preservation : {\u0394 : hctx} {d d' : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d \u21a6 d' \u2192\n \u0394 , \u2205 \u22a2 d' :: \u03c4\n preservation D (Step x x\u2081 x\u2082)\n with pres-lem2 D x\n ... | (_ , wt) = pres-lem x D wt (pres-lem3 wt x\u2081) x\u2082\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"dd73b5a36235448b400cd9d02d7983b1155617bc","subject":"Reorder definitions of Term and Terms.","message":"Reorder definitions of Term and Terms.\n\nThe order of definitions should follow the order of declaration,\nand this code seems easier to understand when one looks at Term\nfirst.\n\nOld-commit-hash: 517547b8eebe1b8803d9f30fa3945f02685b6cc5\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Term\/Plotkin.agda","new_file":"Syntax\/Term\/Plotkin.agda","new_contents":"import Syntax.Type.Plotkin as Type\nimport Syntax.Context as Context\n\nmodule Syntax.Term.Plotkin\n {B : Set {- of base types -}}\n {C : Context.Context {Type.Type B} \u2192 Type.Type B \u2192 Set {- of constants -}}\n where\n\n-- Terms of languages described in Plotkin style\n\nopen import Function using (_\u2218_)\nopen import Data.Product\n\nopen Type B\nopen Context {Type}\n\nopen import Denotation.Environment Type\nopen import Syntax.Context.Plotkin B\n\ndata Term\n (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set\n\ndata Terms\n (\u0393 : Context) :\n (\u03a3 : Context) \u2192 Set\n\ndata Term \u0393 where\n const : \u2200 {\u03a3 \u03c4} \u2192\n (c : C \u03a3 \u03c4) \u2192\n Terms \u0393 \u03a3 \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Terms \u0393 where\n \u2205 : Terms \u0393 \u2205\n _\u2022_ : \u2200 {\u03c4 \u03a3} \u2192\n Term \u0393 \u03c4 \u2192\n Terms \u0393 \u03a3 \u2192\n Terms \u0393 (\u03c4 \u2022 \u03a3)\n\ninfixr 9 _\u2022_\n\n-- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n-- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\nlift-diff-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4) \u00d7 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-diff-apply diff apply {base \u03b9} = diff , apply\nlift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\nlift-diff : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4)\n\nlift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\nlift-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n-- Weakening\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\n\nweakenAll : \u2200 {\u0393\u2081 \u0393\u2082 \u03a3} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Terms \u0393\u2081 \u03a3 \u2192\n Terms \u0393\u2082 \u03a3\n\nweaken \u0393\u2081\u227c\u0393\u2082 (const c ts) = const c (weakenAll \u0393\u2081\u227c\u0393\u2082 ts)\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (lift \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\nweakenAll \u0393\u2081\u227c\u0393\u2082 \u2205 = \u2205\nweakenAll \u0393\u2081\u227c\u0393\u2082 (t \u2022 ts) = weaken \u0393\u2081\u227c\u0393\u2082 t \u2022 weakenAll \u0393\u2081\u227c\u0393\u2082 ts\n\n-- Specialized weakening\nweaken\u2081 : \u2200 {\u0393 \u03c3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4\nweaken\u2081 t = weaken (drop _ \u2022 \u227c-refl) t\n\nweaken\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u0393) \u03c4\nweaken\u2082 t = weaken (drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\nweaken\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u03b3 \u2022 \u0393) \u03c4\nweaken\u2083 t = weaken (drop _ \u2022 drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n-- Shorthands for nested applications\napp\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3\napp\u2082 f x = app (app f x)\n\napp\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4\napp\u2083 f x = app\u2082 (app f x)\n\napp\u2084 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5\napp\u2084 f x = app\u2083 (app f x)\n\napp\u2085 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6\napp\u2085 f x = app\u2084 (app f x)\n\napp\u2086 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6 \u21d2 \u03b7) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6 \u2192 Term \u0393 \u03b7\napp\u2086 f x = app\u2085 (app f x)\n\nTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nTermConstructor \u0393 \u2205 \u03c4\u2032 = Term \u0393 \u03c4\u2032\nTermConstructor \u0393 (\u03c4 \u2022 \u03a3) \u03c4\u2032 = Term \u0393 \u03c4 \u2192 TermConstructor \u0393 \u03a3 \u03c4\u2032\n\n-- helper for lift-\u03b7-const, don't try to understand at home\nlift-\u03b7-const-rec : \u2200 {\u03a3 \u0393 \u03c4} \u2192 (Terms \u0393 \u03a3 \u2192 Term \u0393 \u03c4) \u2192 TermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const-rec {\u2205} k = k \u2205\nlift-\u03b7-const-rec {\u03c4 \u2022 \u03a3} k = \u03bb t \u2192 lift-\u03b7-const-rec (\u03bb ts \u2192 k (t \u2022 ts))\n\nlift-\u03b7-const : \u2200 {\u03a3 \u03c4} \u2192 C \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 TermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const constant = lift-\u03b7-const-rec (const constant)\n","old_contents":"import Syntax.Type.Plotkin as Type\nimport Syntax.Context as Context\n\nmodule Syntax.Term.Plotkin\n {B : Set {- of base types -}}\n {C : Context.Context {Type.Type B} \u2192 Type.Type B \u2192 Set {- of constants -}}\n where\n\n-- Terms of languages described in Plotkin style\n\nopen import Function using (_\u2218_)\nopen import Data.Product\n\nopen Type B\nopen Context {Type}\n\nopen import Denotation.Environment Type\nopen import Syntax.Context.Plotkin B\n\ndata Term\n (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set\n\ndata Terms\n (\u0393 : Context) :\n (\u03a3 : Context) \u2192 Set\n\ndata Terms \u0393 where\n \u2205 : Terms \u0393 \u2205\n _\u2022_ : \u2200 {\u03c4 \u03a3} \u2192\n Term \u0393 \u03c4 \u2192\n Terms \u0393 \u03a3 \u2192\n Terms \u0393 (\u03c4 \u2022 \u03a3)\n\ninfixr 9 _\u2022_\n\ndata Term \u0393 where\n const : \u2200 {\u03a3 \u03c4} \u2192\n (c : C \u03a3 \u03c4) \u2192\n Terms \u0393 \u03a3 \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\n-- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n-- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\nlift-diff-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4) \u00d7 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-diff-apply diff apply {base \u03b9} = diff , apply\nlift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\nlift-diff : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4)\n\nlift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\nlift-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n-- Weakening\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\n\nweakenAll : \u2200 {\u0393\u2081 \u0393\u2082 \u03a3} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Terms \u0393\u2081 \u03a3 \u2192\n Terms \u0393\u2082 \u03a3\n\nweaken \u0393\u2081\u227c\u0393\u2082 (const c ts) = const c (weakenAll \u0393\u2081\u227c\u0393\u2082 ts)\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (lift \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\nweakenAll \u0393\u2081\u227c\u0393\u2082 \u2205 = \u2205\nweakenAll \u0393\u2081\u227c\u0393\u2082 (t \u2022 ts) = weaken \u0393\u2081\u227c\u0393\u2082 t \u2022 weakenAll \u0393\u2081\u227c\u0393\u2082 ts\n\n-- Specialized weakening\nweaken\u2081 : \u2200 {\u0393 \u03c3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4\nweaken\u2081 t = weaken (drop _ \u2022 \u227c-refl) t\n\nweaken\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u0393) \u03c4\nweaken\u2082 t = weaken (drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\nweaken\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u03b3 \u2022 \u0393) \u03c4\nweaken\u2083 t = weaken (drop _ \u2022 drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n-- Shorthands for nested applications\napp\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3\napp\u2082 f x = app (app f x)\n\napp\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4\napp\u2083 f x = app\u2082 (app f x)\n\napp\u2084 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5\napp\u2084 f x = app\u2083 (app f x)\n\napp\u2085 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6\napp\u2085 f x = app\u2084 (app f x)\n\napp\u2086 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6 \u21d2 \u03b7) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6 \u2192 Term \u0393 \u03b7\napp\u2086 f x = app\u2085 (app f x)\n\nTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nTermConstructor \u0393 \u2205 \u03c4\u2032 = Term \u0393 \u03c4\u2032\nTermConstructor \u0393 (\u03c4 \u2022 \u03a3) \u03c4\u2032 = Term \u0393 \u03c4 \u2192 TermConstructor \u0393 \u03a3 \u03c4\u2032\n\n-- helper for lift-\u03b7-const, don't try to understand at home\nlift-\u03b7-const-rec : \u2200 {\u03a3 \u0393 \u03c4} \u2192 (Terms \u0393 \u03a3 \u2192 Term \u0393 \u03c4) \u2192 TermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const-rec {\u2205} k = k \u2205\nlift-\u03b7-const-rec {\u03c4 \u2022 \u03a3} k = \u03bb t \u2192 lift-\u03b7-const-rec (\u03bb ts \u2192 k (t \u2022 ts))\n\nlift-\u03b7-const : \u2200 {\u03a3 \u03c4} \u2192 C \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 TermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const constant = lift-\u03b7-const-rec (const constant)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"8d03eb08a3d8dc237fc96cbdf430c73571ead581","subject":"Add new lemmas on change equivalence","message":"Add new lemmas on change equivalence\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Equivalence.agda","new_file":"Base\/Change\/Equivalence.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\nopen import Function\n\nopen import Base.Change.Equivalence.Base public\nopen import Base.Change.Equivalence.EqReasoning public\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- By update-nil, if dx = nil x, then x \u229e dx \u2261 x.\n -- As a consequence, if dx \u2259 nil x, then x \u229e dx \u2261 x\n nil-is-\u229e-unit : \u2200 (dx : \u0394 {{ca}} x) \u2192 dx \u2259 nil x \u2192 x \u229e dx \u2261 x\n nil-is-\u229e-unit dx dx\u2259nil-x =\n begin\n x \u229e dx\n \u2261\u27e8 proof dx\u2259nil-x \u27e9\n x \u229e (nil {{ca}} x)\n \u2261\u27e8 update-nil {{ca}} x \u27e9\n x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Here we prove the inverse:\n \u229e-unit-is-nil : \u2200 (dx : \u0394 {{ca}} x) \u2192 x \u229e dx \u2261 x \u2192 _\u2259_ {{ca}} dx (nil {{ca}} x)\n \u229e-unit-is-nil dx x\u229edx\u2261x = doe $\n begin\n x \u229e dx\n \u2261\u27e8 x\u229edx\u2261x \u27e9\n x\n \u2261\u27e8 sym (update-nil {{ca}} x) \u27e9\n x \u229e nil {{ca}} x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Let's show that nil x is d.o.e. to x \u229f x\n nil-x-is-x\u229fx : nil x \u2259 x \u229f x\n nil-x-is-x\u229fx = \u2259-sym (\u229e-unit-is-nil (x \u229f x) (update-diff {{ca}} x x))\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by\n -- equiv-fun-changes-respect, and its corollaries fun-change-respects and\n -- equiv-fun-changes-funs.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n -- This results pairs with update-diff.\n diff-update : \u2200 {dx : \u0394 {{ca}} x} \u2192 (x \u229e dx) \u229f x \u2259 dx\n diff-update {dx} = doe lemma\n where\n lemma : _\u229e_ {{ca}} x (x \u229e dx \u229f x) \u2261 x \u229e dx\n lemma = update-diff {{ca}} (x \u229e dx) x\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization; DerivativeAsChange)\n open FC.FunctionChange\n\n equiv-fun-changes-respect : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n _\u2259_ {{changeAlgebra}} df\u2081 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n equiv-fun-changes-respect {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = doe lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 \u2259-cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 \u2259-cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} (df : \u0394 f) \u2192\n dx\u2081 \u2259 dx\u2082 \u2192 apply df x dx\u2081 \u2259 apply df x dx\u2082\n fun-change-respects df dx\u2081\u2259dx\u2082 = equiv-fun-changes-respect (\u2259-refl {dx = df}) dx\u2081\u2259dx\u2082\n\n -- D.o.e. function changes behave like the same function (up to d.o.e.).\n equiv-fun-changes-funs : \u2200 {x : A} {dx : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082 : \u0394 f} \u2192\n _\u2259_ {{changeAlgebra}} df\u2081 df\u2082 \u2192 apply df\u2081 x dx \u2259 apply df\u2082 x dx\n equiv-fun-changes-funs {dx = dx} df\u2081\u2259df\u2082 = equiv-fun-changes-respect df\u2081\u2259df\u2082 (\u2259-refl {dx = dx})\n\n derivative-doe-characterization : \u2200 {a : A} {da : \u0394 a}\n {f : A \u2192 B} {df : RawChange f} (is-derivative : IsDerivative f df) \u2192\n _\u2259_ {{CB}} (df a da) (f (a \u229e da) \u229f f a)\n derivative-doe-characterization {a} {da} {f} {df} is-derivative = doe lemma\n where\n open \u2261-Reasoning\n lemma : f a \u229e df a da \u2261 f a \u229e (f (a \u229e da) \u229f f a)\n lemma =\n begin\n (f a \u229e df a da)\n \u2261\u27e8 is-derivative a da \u27e9\n (f (a \u229e da))\n \u2261\u27e8 sym (update-diff (f (a \u229e da)) (f a)) \u27e9\n (f a \u229e (f (a \u229e da) \u229f f a))\n \u220e\n\n derivative-respects-doe : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x}\n {f : A \u2192 B} {df : RawChange f} (is-derivative : IsDerivative f df) \u2192\n _\u2259_ {{CA}} dx\u2081 dx\u2082 \u2192 _\u2259_ {{CB}} (df x dx\u2081) (df x dx\u2082)\n derivative-respects-doe {x} {dx\u2081} {dx\u2082} {f} {df} is-derivative dx\u2081\u2259dx\u2082 =\n begin\n df x dx\u2081\n \u2259\u27e8 derivative-doe-characterization is-derivative \u27e9\n f (x \u229e dx\u2081) \u229f f x\n \u2261\u27e8 cong (\u03bb v \u2192 f v \u229f f x) (proof dx\u2081\u2259dx\u2082) \u27e9\n f (x \u229e dx\u2082) \u229f f x\n \u2259\u27e8 \u2259-sym (derivative-doe-characterization is-derivative) \u27e9\n df x dx\u2082\n \u220e\n where\n open \u2259-Reasoning\n\n -- This is also a corollary of fun-changes-respect\n derivative-respects-doe-alt : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x}\n {f : A \u2192 B} {df : RawChange f} (is-derivative : IsDerivative f df) \u2192\n _\u2259_ {{CA}} dx\u2081 dx\u2082 \u2192 _\u2259_ {{CB}} (df x dx\u2081) (df x dx\u2082)\n derivative-respects-doe-alt {x} {dx\u2081} {dx\u2082} {f} {df} is-derivative dx\u2081\u2259dx\u2082 =\n fun-change-respects (DerivativeAsChange is-derivative) dx\u2081\u2259dx\u2082\n\n open import Postulate.Extensionality\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} df-x-dx\u2259dg-x-dx = doe lemma\u2082\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 x dx = proof $ df-x-dx\u2259dg-x-dx x dx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n\n -- You could think that the function should relate equivalent changes, but\n -- that's a stronger hypothesis, which doesn't give you extra guarantees. But\n -- here's the statement and proof, for completeness:\n\n delta-ext\u2082 : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx\u2081 dx\u2082 \u2192 _\u2259_ {{CA}} dx\u2081 dx\u2082 \u2192 apply df x dx\u2081 \u2259 apply dg x dx\u2082) \u2192 df \u2259 dg\n delta-ext\u2082 {f} {df} {dg} df-x-dx\u2259dg-x-dx = delta-ext (\u03bb x dx \u2192 df-x-dx\u2259dg-x-dx x dx dx \u2259-refl)\n\n -- We know that IsDerivative f (apply (nil f)) (by nil-is-derivative).\n -- That is, df = nil f -> IsDerivative f (apply df).\n -- Now, we try to prove that if IsDerivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n IsDerivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil {{CA}} x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil {{CA}} x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n IsDerivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n derivative-is-nil-alternative : \u2200 {f : A \u2192 B} df \u2192\n (IsDerivative-f-df : IsDerivative f df) \u2192 DerivativeAsChange IsDerivative-f-df \u2259 nil f\n derivative-is-nil-alternative df IsDerivative-f-df = derivative-is-nil (DerivativeAsChange IsDerivative-f-df) IsDerivative-f-df\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 IsDerivative f (apply df) \u2192 IsDerivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 \u2259-sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n\n -- This is Lemma 2.5 in the paper. Note that the statement in the paper uses\n -- (incorrectly) normal equality instead of delta-observational equivalence.\n deriv-zero :\n \u2200 (f : A \u2192 B) df \u2192 IsDerivative f df \u2192\n \u2200 v \u2192 df v (nil {{CA}} v) \u2259 nil {{CB}} (f v)\n deriv-zero f df proof v = doe lemma\n where\n open \u2261-Reasoning\n lemma : f v \u229e df v (nil v) \u2261 f v \u229e nil {{CB}} (f v)\n lemma =\n begin\n f v \u229e df v (nil {{CA}} v)\n \u2261\u27e8 proof v (nil {{CA}} v) \u27e9\n f (v \u229e (nil {{CA}} v))\n \u2261\u27e8 cong f (update-nil {{CA}} v) \u27e9\n f v\n \u2261\u27e8 sym (update-nil {{CB}} (f v)) \u27e9\n f v \u229e nil {{CB}} (f v)\n \u220e\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\nopen import Function\n\nopen import Base.Change.Equivalence.Base public\nopen import Base.Change.Equivalence.EqReasoning public\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- By update-nil, if dx = nil x, then x \u229e dx \u2261 x.\n -- As a consequence, if dx \u2259 nil x, then x \u229e dx \u2261 x\n nil-is-\u229e-unit : \u2200 (dx : \u0394 {{ca}} x) \u2192 dx \u2259 nil x \u2192 x \u229e dx \u2261 x\n nil-is-\u229e-unit dx dx\u2259nil-x =\n begin\n x \u229e dx\n \u2261\u27e8 proof dx\u2259nil-x \u27e9\n x \u229e (nil {{ca}} x)\n \u2261\u27e8 update-nil {{ca}} x \u27e9\n x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Here we prove the inverse:\n \u229e-unit-is-nil : \u2200 (dx : \u0394 {{ca}} x) \u2192 x \u229e dx \u2261 x \u2192 _\u2259_ {{ca}} dx (nil {{ca}} x)\n \u229e-unit-is-nil dx x\u229edx\u2261x = doe $\n begin\n x \u229e dx\n \u2261\u27e8 x\u229edx\u2261x \u27e9\n x\n \u2261\u27e8 sym (update-nil {{ca}} x) \u27e9\n x \u229e nil {{ca}} x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Let's show that nil x is d.o.e. to x \u229f x\n nil-x-is-x\u229fx : nil x \u2259 x \u229f x\n nil-x-is-x\u229fx = \u2259-sym (\u229e-unit-is-nil (x \u229f x) (update-diff {{ca}} x x))\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by\n -- equiv-fun-changes-respect, and its corollaries fun-change-respects and\n -- equiv-fun-changes-funs.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n -- This results pairs with update-diff.\n diff-update : \u2200 {dx : \u0394 {{ca}} x} \u2192 (x \u229e dx) \u229f x \u2259 dx\n diff-update {dx} = doe lemma\n where\n lemma : _\u229e_ {{ca}} x (x \u229e dx \u229f x) \u2261 x \u229e dx\n lemma = update-diff {{ca}} (x \u229e dx) x\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization; DerivativeAsChange)\n open FC.FunctionChange\n\n equiv-fun-changes-respect : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n _\u2259_ {{changeAlgebra}} df\u2081 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n equiv-fun-changes-respect {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = doe lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 \u2259-cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 \u2259-cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} (df : \u0394 f) \u2192\n dx\u2081 \u2259 dx\u2082 \u2192 apply df x dx\u2081 \u2259 apply df x dx\u2082\n fun-change-respects df dx\u2081\u2259dx\u2082 = equiv-fun-changes-respect (\u2259-refl {dx = df}) dx\u2081\u2259dx\u2082\n\n -- D.o.e. function changes behave like the same function (up to d.o.e.).\n equiv-fun-changes-funs : \u2200 {x : A} {dx : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082 : \u0394 f} \u2192\n _\u2259_ {{changeAlgebra}} df\u2081 df\u2082 \u2192 apply df\u2081 x dx \u2259 apply df\u2082 x dx\n equiv-fun-changes-funs {dx = dx} df\u2081\u2259df\u2082 = equiv-fun-changes-respect df\u2081\u2259df\u2082 (\u2259-refl {dx = dx})\n\n open import Postulate.Extensionality\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} df-x-dx\u2259dg-x-dx = doe lemma\u2082\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 x dx = proof $ df-x-dx\u2259dg-x-dx x dx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n\n -- You could think that the function should relate equivalent changes, but\n -- that's a stronger hypothesis, which doesn't give you extra guarantees. But\n -- here's the statement and proof, for completeness:\n\n delta-ext\u2082 : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx\u2081 dx\u2082 \u2192 _\u2259_ {{CA}} dx\u2081 dx\u2082 \u2192 apply df x dx\u2081 \u2259 apply dg x dx\u2082) \u2192 df \u2259 dg\n delta-ext\u2082 {f} {df} {dg} df-x-dx\u2259dg-x-dx = delta-ext (\u03bb x dx \u2192 df-x-dx\u2259dg-x-dx x dx dx \u2259-refl)\n\n -- We know that IsDerivative f (apply (nil f)) (by nil-is-derivative).\n -- That is, df = nil f -> IsDerivative f (apply df).\n -- Now, we try to prove that if IsDerivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n IsDerivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil {{CA}} x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil {{CA}} x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n IsDerivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n derivative-is-nil-alternative : \u2200 {f : A \u2192 B} df \u2192\n (IsDerivative-f-df : IsDerivative f df) \u2192 DerivativeAsChange IsDerivative-f-df \u2259 nil f\n derivative-is-nil-alternative df IsDerivative-f-df = derivative-is-nil (DerivativeAsChange IsDerivative-f-df) IsDerivative-f-df\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 IsDerivative f (apply df) \u2192 IsDerivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 \u2259-sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n\n -- This is Lemma 2.5 in the paper. Note that the statement in the paper uses\n -- (incorrectly) normal equality instead of delta-observational equivalence.\n deriv-zero :\n \u2200 (f : A \u2192 B) df \u2192 IsDerivative f df \u2192\n \u2200 v \u2192 df v (nil {{CA}} v) \u2259 nil {{CB}} (f v)\n deriv-zero f df proof v = doe lemma\n where\n open \u2261-Reasoning\n lemma : f v \u229e df v (nil v) \u2261 f v \u229e nil {{CB}} (f v)\n lemma =\n begin\n f v \u229e df v (nil {{CA}} v)\n \u2261\u27e8 proof v (nil {{CA}} v) \u27e9\n f (v \u229e (nil {{CA}} v))\n \u2261\u27e8 cong f (update-nil {{CA}} v) \u27e9\n f v\n \u2261\u27e8 sym (update-nil {{CB}} (f v)) \u27e9\n f v \u229e nil {{CB}} (f v)\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"be309218c7d7b2c11388e7721ba93b5817986e9c","subject":"Moved note to agda2atp repo.","message":"Moved note to agda2atp repo.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/InternalSyntax.agda","new_file":"notes\/InternalSyntax.agda","new_contents":"","old_contents":"------------------------------------------------------------------------------\n-- Agda internal syntax\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule InternalSyntax where\n\n-- Internal types from Agda.Syntax.Internal.\n\n------------------------------------------------------------------------------\n-- Sets\n\npostulate A B C : Set\n\n-- El (Type (Max [ClosedLevel 1])) (Sort (Type (Max [])))\n\npostulate A\u2081 : Set\u2081\n\n-- El (Type (Max [ClosedLevel 2])) (Sort (Type (Max [ClosedLevel 1])))\n\n------------------------------------------------------------------------------\n-- The term is Def\n\npostulate defTerm : A\n\n-- El (Type (Max [])) (Def InternalSyntax.A [])\n\n------------------------------------------------------------------------------\n-- The term is Fun\n\npostulate funTerm\u2081 : A \u2192 B\n\n-- El (Type (Max []))\n-- (Fun r(El (Type (Max []))\n-- (Def InternalSyntax.A []))\n-- (El (Type (Max []))\n-- (Def InternalSyntax.B [])))\n\npostulate funTerm\u2082 : A \u2192 B \u2192 C\n\n-- El (Type (Max []))\n-- (Fun r(El (Type (Max []))\n-- (Def InternalSyntax.A []))\n-- (El (Type (Max []))\n-- (Fun r(El (Type (Max []))\n-- (Def InternalSyntax.B []))\n-- (El (Type (Max []))\n-- (Def InternalSyntax.C [])))))\n\n\n------------------------------------------------------------------------------\n-- The term is a (fake) Pi\n\npostulate fakePiTerm : (a : A) \u2192 B\n\n-- El (Type (Max []))\n-- (Pi r(El (Type (Max []))\n-- (Def InternalSyntax.A []))\n-- (Abs \"a\" El (Type (Max []))\n-- (Def InternalSyntax.B [])))\n\n------------------------------------------------------------------------------\n-- The term is Pi\n\npostulate P : A \u2192 Set\n\n-- El (Type (Max [ClosedLevel 1]))\n-- (Fun r(El (Type (Max []))\n-- (Def InternalSyntax.A []))\n-- (El (Type (Max [ClosedLevel 1]))\n-- (Sort (Type (Max [])))))\n\npostulate piTerm : (a : A) \u2192 P a\n\n-- El (Type (Max []))\n-- (Pi r(El (Type (Max []))\n-- (Def InternalSyntax.A []))\n-- (Abs \"a\" El (Type (Max []))\n-- (Def InternalSyntax.P [r(Var 0 [])])))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ca0490d3e647551767333fe1b8af760d98b01dfa","subject":"agda: use \u2295 and \u229d also when defining diff-term and apply-term","message":"agda: use \u2295 and \u229d also when defining diff-term and apply-term\n\nGoal: make diff-term and apply-term themselves more readable.\n\nOld-commit-hash: fc206b02d6057172e7f49c02eb521abaaafcb1e8\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Term\/Popl14.agda","new_file":"Syntax\/Term\/Popl14.agda","new_contents":"module Syntax.Term.Popl14 where\n\n-- Terms Calculus Popl14\n--\n-- Contents\n-- - Term constructors\n-- - Weakening on terms\n-- - `fit`: weaken a term to its \u0394Context\n-- - diff-term, apply-term and their syntactic sugars\n\nopen import Syntax.Context.Popl14 public\nopen import Data.Integer\n\ndata Term (\u0393 : Context) : Type -> Set where\n int : (n : \u2124) \u2192 Term \u0393 int\n add : (s : Term \u0393 int) (t : Term \u0393 int) \u2192 Term \u0393 int\n minus : (t : Term \u0393 int) \u2192 Term \u0393 int\n\n empty : Term \u0393 bag\n insert : (s : Term \u0393 int) (t : Term \u0393 bag) \u2192 Term \u0393 bag\n union : (s : Term \u0393 bag) \u2192 (t : Term \u0393 bag) \u2192 Term \u0393 bag\n negate : (t : Term \u0393 bag) \u2192 Term \u0393 bag\n\n flatmap : (s : Term \u0393 (int \u21d2 bag)) (t : Term \u0393 bag) \u2192 Term \u0393 bag\n sum : (t : Term \u0393 bag) \u2192 Term \u0393 int\n\n var : \u2200 {\u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4} \u2192 (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) (t : Term \u0393 \u03c3) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 Term \u0393 (\u03c3 \u21d2 \u03c4)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken \u0393\u2081\u227c\u0393\u2082 (int x) = int x\nweaken \u0393\u2081\u227c\u0393\u2082 (add s t) = add (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (minus t) = minus (weaken \u0393\u2081\u227c\u0393\u2082 t)\n\nweaken \u0393\u2081\u227c\u0393\u2082 empty = empty\nweaken \u0393\u2081\u227c\u0393\u2082 (insert s t) = insert (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (union s t) = union (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (negate t) = negate (weaken \u0393\u2081\u227c\u0393\u2082 t)\n\nweaken \u0393\u2081\u227c\u0393\u2082 (flatmap s t) = flatmap (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (sum t) = sum (weaken \u0393\u2081\u227c\u0393\u2082 t)\n\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (weakenVar \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\nfit : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394Context \u0393) \u03c4\nfit = weaken \u0393\u227c\u0394\u0393\n\ndiff-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\napply-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n-- Sugars for diff-term and apply-term\ninfixl 6 _\u2295_ _\u229d_\n_\u2295_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4\n_\u229d_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394Type \u03c4)\nt \u2295 \u0394t = app (app apply-term \u0394t) t\ns \u229d t = app (app diff-term s) t\n\napply-term {int} =\n let \u0394x = var (that this)\n x = var this\n in abs (abs (add x \u0394x))\napply-term {bag} =\n let \u0394x = var (that this)\n x = var this\n in abs (abs (union x \u0394x))\napply-term {\u03c3 \u21d2 \u03c4} =\n let\n \u0394f = var (that (that this))\n f = var (that this)\n x = var this\n in\n -- \u0394f f x\n abs (abs (abs\n (app f x \u2295 app (app \u0394f x) (x \u229d x))))\n\ndiff-term {int} =\n let x = var (that this)\n y = var this\n in abs (abs (add x (minus y)))\ndiff-term {bag} =\n let x = var (that this)\n y = var this\n in abs (abs (union x (negate y)))\ndiff-term {\u03c3 \u21d2 \u03c4} =\n let\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n in\n -- g f x \u0394x\n abs (abs (abs (abs\n (app g (x \u2295 \u0394x) \u229d app f x))))\n","old_contents":"module Syntax.Term.Popl14 where\n\n-- Terms Calculus Popl14\n--\n-- Contents\n-- - Term constructors\n-- - Weakening on terms\n-- - `fit`: weaken a term to its \u0394Context\n-- - diff-term, apply-term and their syntactic sugars\n\nopen import Syntax.Context.Popl14 public\nopen import Data.Integer\n\ndata Term (\u0393 : Context) : Type -> Set where\n int : (n : \u2124) \u2192 Term \u0393 int\n add : (s : Term \u0393 int) (t : Term \u0393 int) \u2192 Term \u0393 int\n minus : (t : Term \u0393 int) \u2192 Term \u0393 int\n\n empty : Term \u0393 bag\n insert : (s : Term \u0393 int) (t : Term \u0393 bag) \u2192 Term \u0393 bag\n union : (s : Term \u0393 bag) \u2192 (t : Term \u0393 bag) \u2192 Term \u0393 bag\n negate : (t : Term \u0393 bag) \u2192 Term \u0393 bag\n\n flatmap : (s : Term \u0393 (int \u21d2 bag)) (t : Term \u0393 bag) \u2192 Term \u0393 bag\n sum : (t : Term \u0393 bag) \u2192 Term \u0393 int\n\n var : \u2200 {\u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4} \u2192 (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) (t : Term \u0393 \u03c3) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 Term \u0393 (\u03c3 \u21d2 \u03c4)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken \u0393\u2081\u227c\u0393\u2082 (int x) = int x\nweaken \u0393\u2081\u227c\u0393\u2082 (add s t) = add (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (minus t) = minus (weaken \u0393\u2081\u227c\u0393\u2082 t)\n\nweaken \u0393\u2081\u227c\u0393\u2082 empty = empty\nweaken \u0393\u2081\u227c\u0393\u2082 (insert s t) = insert (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (union s t) = union (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (negate t) = negate (weaken \u0393\u2081\u227c\u0393\u2082 t)\n\nweaken \u0393\u2081\u227c\u0393\u2082 (flatmap s t) = flatmap (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (sum t) = sum (weaken \u0393\u2081\u227c\u0393\u2082 t)\n\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (weakenVar \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\nfit : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394Context \u0393) \u03c4\nfit = weaken \u0393\u227c\u0394\u0393\n\ndiff-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\napply-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\napply-term {int} =\n let \u0394x = var (that this)\n x = var this\n in abs (abs (add x \u0394x))\napply-term {bag} =\n let \u0394x = var (that this)\n x = var this\n in abs (abs (union x \u0394x))\napply-term {\u03c3 \u21d2 \u03c4} =\n let\n \u0394f = var (that (that this))\n f = var (that this)\n x = var this\n in\n -- \u0394f f x\n abs (abs (abs\n (app (app apply-term\n (app (app \u0394f x) (app (app diff-term x) x)))\n (app f x))))\n\ndiff-term {int} =\n let x = var (that this)\n y = var this\n in abs (abs (add x (minus y)))\ndiff-term {bag} =\n let x = var (that this)\n y = var this\n in abs (abs (union x (negate y)))\ndiff-term {\u03c3 \u21d2 \u03c4} =\n let\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n in\n -- g f x \u0394x\n abs (abs (abs (abs\n (app (app diff-term\n (app g (app (app apply-term \u0394x) x)))\n (app f x)))))\n\n-- Sugars for diff-term and apply-term\ninfixl 6 _\u2295_ _\u229d_\n_\u2295_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4\n_\u229d_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394Type \u03c4)\nt \u2295 \u0394t = app (app apply-term \u0394t) t\ns \u229d t = app (app diff-term s) t\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f994df78d5187cf478e9bd11ea4ef31f16894f95","subject":"NewNew.agda: Hide deducible arguments","message":"NewNew.agda: Hide deducible arguments\n","repos":"inc-lc\/ilc-agda","old_file":"New\/NewNew.agda","new_file":"New\/NewNew.agda","new_contents":"module New.NewNew where\n\nopen import New.Changes\nopen import New.LangChanges\nopen import New.Lang\nopen import New.Types\nopen import New.Derive\n\n[_]_from_to_ : \u2200 (\u03c4 : Type) \u2192 (dv : Cht \u03c4) \u2192 (v1 v2 : \u27e6 \u03c4 \u27e7Type) \u2192 Set\n[ \u03c3 \u21d2 \u03c4 ] df from f1 to f2 =\n \u2200 (da : Cht \u03c3) (a1 a2 : \u27e6 \u03c3 \u27e7Type) \u2192\n [ \u03c3 ] da from a1 to a2 \u2192 [ \u03c4 ] df a1 da from f1 a1 to f2 a2\n[ int ] dv from v1 to v2 = v2 \u2261 v1 + dv\n[ pair \u03c3 \u03c4 ] (da , db) from (a1 , b1) to (a2 , b2) = [ \u03c3 ] da from a1 to a2 \u00d7 [ \u03c4 ] db from b1 to b2\n\n-- XXX This would be more elegant as a datatype \u2014 that would avoid the need for\n-- an equality proof.\n[_]\u0393_from_to_ : \u2200 \u0393 \u2192 eCh \u0393 \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 Set\n[ \u2205 ]\u0393 \u2205 from \u2205 to \u2205 = \u22a4\n[ \u03c4 \u2022 \u0393 ]\u0393 (dv \u2022 v1' \u2022 d\u03c1) from (v1 \u2022 \u03c11) to (v2 \u2022 \u03c12) =\n [ \u03c4 ] dv from v1 to v2 \u00d7 v1 \u2261 v1' \u00d7 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12\n\n\u27e6\u0393\u227c\u0394\u0393\u27e7 : \u2200 {\u0393 \u03c11 \u03c12 d\u03c1} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n \u03c11 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7\u227c d\u03c1\n\u27e6\u0393\u227c\u0394\u0393\u27e7 {\u2205} {\u2205} {\u2205} {\u2205} tt = refl\n\u27e6\u0393\u227c\u0394\u0393\u27e7 {_ \u2022 _} {v1 \u2022 \u03c11} {v2 \u2022 \u03c12} {dv \u2022 .v1 \u2022 d\u03c1} (dvv , refl , d\u03c1\u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u0393\u227c\u0394\u0393\u27e7 d\u03c1\u03c1)\n\nfit-sound : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n \u2200 {\u03c11 \u03c12 d\u03c1} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n \u27e6 t \u27e7Term \u03c11 \u2261 \u27e6 fit t \u27e7Term d\u03c1\nfit-sound t d\u03c1\u03c1 = trans\n (cong \u27e6 t \u27e7Term (\u27e6\u0393\u227c\u0394\u0393\u27e7 d\u03c1\u03c1))\n (sym (weaken-sound t _))\n\nfromtoDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192\n (d\u03c1 : eCh \u0393) (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ] (\u27e6 x \u27e7\u0394Var \u03c11 d\u03c1) from (\u27e6 x \u27e7Var \u03c11) to (\u27e6 x \u27e7Var \u03c12)\nfromtoDeriveVar this (dv \u2022 .v1 \u2022 d\u03c1) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dvv , refl , d\u03c1\u03c1) = dvv\nfromtoDeriveVar (that x) (dv \u2022 .v1 \u2022 d\u03c1) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dvv , refl , d\u03c1\u03c1) = fromtoDeriveVar x d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1\n\nfromtoDeriveConst : \u2200 {\u03c4} c \u2192\n [ \u03c4 ] \u27e6 deriveConst c \u27e7Term \u2205 from \u27e6 c \u27e7Const to \u27e6 c \u27e7Const\nfromtoDeriveConst plus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb = mn\u00b7pq=mp\u00b7nq {a1} {da} {b1} {db}\nfromtoDeriveConst minus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb | sym (-m\u00b7-n=-mn {b1} {db}) = mn\u00b7pq=mp\u00b7nq {a1} {da} { - b1} { - db}\nfromtoDeriveConst cons da a1 a2 daa db b1 b2 dbb = daa , dbb\nfromtoDeriveConst fst (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = daa\nfromtoDeriveConst snd (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = dbb\n\nfromtoDerive : \u2200 {\u0393} \u03c4 \u2192 (t : Term \u0393 \u03c4) \u2192\n {d\u03c1 : eCh \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ] (\u27e6 t \u27e7\u0394Term \u03c11 d\u03c1) from (\u27e6 t \u27e7Term \u03c11) to (\u27e6 t \u27e7Term \u03c12)\nfromtoDerive \u03c4 (const c) {d\u03c1} {\u03c11} d\u03c1\u03c1 rewrite \u27e6 c \u27e7\u0394Const-rewrite \u03c11 d\u03c1 = fromtoDeriveConst c\nfromtoDerive \u03c4 (var x) d\u03c1\u03c1 = fromtoDeriveVar x _ _ _ d\u03c1\u03c1\nfromtoDerive \u03c4 (app {\u03c3} s t) d\u03c1\u03c1 rewrite sym (fit-sound t d\u03c1\u03c1) =\n let fromToF = fromtoDerive (\u03c3 \u21d2 \u03c4) s d\u03c1\u03c1\n in let fromToB = fromtoDerive \u03c3 t d\u03c1\u03c1 in fromToF _ _ _ fromToB\nfromtoDerive (\u03c3 \u21d2 \u03c4) (abs t) d\u03c1\u03c1 = \u03bb da a1 a2 daa \u2192\n fromtoDerive \u03c4 t (daa , refl , d\u03c1\u03c1)\n","old_contents":"module New.NewNew where\n\nopen import New.Changes\nopen import New.LangChanges\nopen import New.Lang\nopen import New.Types\nopen import New.Derive\n\n[_]_from_to_ : \u2200 (\u03c4 : Type) \u2192 (dv : Cht \u03c4) \u2192 (v1 v2 : \u27e6 \u03c4 \u27e7Type) \u2192 Set\n[ \u03c3 \u21d2 \u03c4 ] df from f1 to f2 =\n \u2200 (da : Cht \u03c3) (a1 a2 : \u27e6 \u03c3 \u27e7Type) \u2192\n [ \u03c3 ] da from a1 to a2 \u2192 [ \u03c4 ] df a1 da from f1 a1 to f2 a2\n[ int ] dv from v1 to v2 = v2 \u2261 v1 + dv\n[ pair \u03c3 \u03c4 ] (da , db) from (a1 , b1) to (a2 , b2) = [ \u03c3 ] da from a1 to a2 \u00d7 [ \u03c4 ] db from b1 to b2\n\n[_]\u0393_from_to_ : \u2200 \u0393 \u2192 eCh \u0393 \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 Set\n[ \u2205 ]\u0393 \u2205 from \u2205 to \u2205 = \u22a4\n[ \u03c4 \u2022 \u0393 ]\u0393 (dv \u2022 v1' \u2022 d\u03c1) from (v1 \u2022 \u03c11) to (v2 \u2022 \u03c12) =\n [ \u03c4 ] dv from v1 to v2 \u00d7 v1 \u2261 v1' \u00d7 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12\n\n\u27e6\u0393\u227c\u0394\u0393\u27e7 : \u2200 {\u0393} (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n \u03c11 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7\u227c d\u03c1\n\u27e6\u0393\u227c\u0394\u0393\u27e7 \u2205 \u2205 \u2205 tt = refl\n\u27e6\u0393\u227c\u0394\u0393\u27e7 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dv \u2022 .v1 \u2022 d\u03c1) (dvv , refl , d\u03c1\u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c11 \u03c12 d\u03c1 d\u03c1\u03c1)\n\nfit-sound : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : eCh \u0393) \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n \u27e6 t \u27e7Term \u03c11 \u2261 \u27e6 fit t \u27e7Term d\u03c1\nfit-sound t \u03c11 \u03c12 d\u03c1 d\u03c1\u03c1 = trans\n (cong \u27e6 t \u27e7Term (\u27e6\u0393\u227c\u0394\u0393\u27e7 \u03c11 \u03c12 d\u03c1 d\u03c1\u03c1))\n (sym (weaken-sound t _))\n\nfromtoDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192\n (d\u03c1 : eCh \u0393) (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ] (\u27e6 x \u27e7\u0394Var \u03c11 d\u03c1) from (\u27e6 x \u27e7Var \u03c11) to (\u27e6 x \u27e7Var \u03c12)\nfromtoDeriveVar this (dv \u2022 .v1 \u2022 d\u03c1) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dvv , refl , d\u03c1\u03c1) = dvv\nfromtoDeriveVar (that x) (dv \u2022 .v1 \u2022 d\u03c1) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dvv , refl , d\u03c1\u03c1) = fromtoDeriveVar x d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1\n\nfromtoDeriveConst : \u2200 {\u03c4} c \u2192\n [ \u03c4 ] \u27e6 deriveConst c \u27e7Term \u2205 from \u27e6 c \u27e7Const to \u27e6 c \u27e7Const\nfromtoDeriveConst plus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb = mn\u00b7pq=mp\u00b7nq {a1} {da} {b1} {db}\nfromtoDeriveConst minus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb | sym (-m\u00b7-n=-mn {b1} {db}) = mn\u00b7pq=mp\u00b7nq {a1} {da} { - b1} { - db}\nfromtoDeriveConst cons da a1 a2 daa db b1 b2 dbb = daa , dbb\nfromtoDeriveConst fst (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = daa\nfromtoDeriveConst snd (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = dbb\n\nfromtoDerive : \u2200 {\u0393} \u03c4 \u2192 (t : Term \u0393 \u03c4) \u2192\n {d\u03c1 : eCh \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ] (\u27e6 t \u27e7\u0394Term \u03c11 d\u03c1) from (\u27e6 t \u27e7Term \u03c11) to (\u27e6 t \u27e7Term \u03c12)\nfromtoDerive \u03c4 (const c) {d\u03c1} {\u03c11} d\u03c1\u03c1 rewrite \u27e6 c \u27e7\u0394Const-rewrite \u03c11 d\u03c1 = fromtoDeriveConst c\nfromtoDerive \u03c4 (var x) d\u03c1\u03c1 = fromtoDeriveVar x _ _ _ d\u03c1\u03c1\nfromtoDerive \u03c4 (app {\u03c3} s t) {d\u03c1} {\u03c11} {\u03c12} d\u03c1\u03c1 rewrite sym (fit-sound t \u03c11 \u03c12 d\u03c1 d\u03c1\u03c1) =\n let fromToF = fromtoDerive (\u03c3 \u21d2 \u03c4) s d\u03c1\u03c1\n in let fromToB = fromtoDerive \u03c3 t d\u03c1\u03c1 in fromToF _ _ _ fromToB\nfromtoDerive (\u03c3 \u21d2 \u03c4) (abs t) d\u03c1\u03c1 = \u03bb da a1 a2 daa \u2192\n fromtoDerive \u03c4 t (daa , refl , d\u03c1\u03c1)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"11a45f0108ff29d0e24b4ce3f2077ffe597a899f","subject":"Add 1-E_ and kill some postulates","message":"Add 1-E_ and kill some postulates\n","repos":"crypto-agda\/crypto-agda","old_file":"Prob.agda","new_file":"Prob.agda","new_contents":"module Prob where\n\nopen import Data.Bool\nopen import Data.Nat\nopen import Data.Vec\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\nopen import Data.Sum\n\nopen import Function\n\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Relation.Nullary\n\nrecord ]0,1[-ops (]0,1[ : Set) (_ : (x y : ]0,1[) \u2192 x ) \u2261 (x \u00b7E y) \/E z < \u00b7E-anti\u2082 x pf >\n\n -- proofs \u2261 maybe should be some Equivalence \u2248\n field\n +E-sym : (x y : ]0,1[) \u2192 x +E y \u2261 y +E x\n +E-assoc : (x y z : ]0,1[) \u2192 (x +E y) +E z \u2261 x +E (y +E z)\n \u00b7\/E-identity : (x : ]0,1[){y : ]0,1[} \u2192 x \u2261 (x \u00b7E y) \/E y < \u00b7E-anti\u2081 x >\n\nmodule ]0,1[-\u211a where\n data ]0,1[ : Set where\n 1+_\/2+x+_ : \u2115 \u2192 \u2115 \u2192 ]0,1[\n -- to\u211a : ]0,1] \u2192 \u211a\n -- to\u211a (1+ x \/1+x+ y) = (1 + x) \/\u211a (1 + x + y)\n\n postulate\n _ : (x y : ]0,1[) \u2192 x = 1+ x'' \/2+x+ y''\n where x'' = 1 + x' + y' + 2 * x + x * x' + x * y'\n y'' = y + x' + x' * y \u2238 1 \u2238 y' \u2238 x \u2238 x * y'\n\n postulate\n _+E_ : ]0,1[ \u2192 ]0,1[ \u2192 ]0,1[\n\n postulate\n ) \u2261 (x \u00b7E y) \/E z < \u00b7E-anti\u2082 x pf >\n\n postulate\n +E-sym : (x x\u2081 : ]0,1[) \u2192 x +E x\u2081 \u2261 x\u2081 +E x\n +E-assoc : (x x\u2081 x\u2082 : ]0,1[) \u2192 (x +E x\u2081) +E x\u2082 \u2261 x +E (x\u2081 +E x\u2082)\n \u00b7\/E-identity : (x : ]0,1[) {y : ]0,1[} \u2192 x \u2261 (x \u00b7E y) \/E y < \u00b7E-anti\u2081 x >\n\n ops : ]0,1[-ops ]0,1[ _ 1-E_ : (x : [0,1]) \u2192 Inc x \u2192 ]0,1[\n 0I < () >\n 1I < () >\n (x I) < pos > = x\n\n _+I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I +I x = x\n 1I +I _ = 1I\n x I +I 0I = x I\n x I +I 1I = 1I\n x I +I x\u2081 I = (x +E x\u2081) I\n\n _\u00b7I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I \u00b7I y = 0I\n 1I \u00b7I y = y\n (x I) \u00b7I 0I = 0I\n (x I) \u00b7I 1I = x I\n (x I) \u00b7I (x\u2081 I) = (x \u00b7E x\u2081) I\n\n -- 1\/I1+_ : \u2115 \u2192 [0,1]\n\n\n _\/I_<_,_> : (x y : [0,1]) \u2192 x \u2264I y \u2192 Pos y \u2192 [0,1]\n x \/I 0I < _ , () >\n x \/I 1I < _ , _ > = x\n 0I \/I _ < _ , _ > = 0I\n 1I \/I _ I < () , _ >\n (x I) \/I y I < E\u2264E pf , _ > = (x \/E y < pf >) I\n (x I) \/I .x I < E\u2261E , _ > = 1I\n\n\n +I-identity : (x : [0,1]) \u2192 x \u2261 0I +I x\n +I-identity x = refl\n\n *-anti : (x : [0,1]){y z : [0,1]} \u2192 y \u2264I z \u2192 x \u00b7I y \u2264I z\n *-anti 0I le = z\u2264n\n *-anti 1I le = le\n *-anti (x I) z\u2264n = z\u2264n\n *-anti (x I) n\u22641 = n\u22641\n *-anti (x I) (E\u2264E pf) = E\u2264E (\u00b7E-anti\u2082 x pf)\n *-anti (x I) E\u2261E = E\u2264E (\u00b7E-anti\u2081 x)\n\n *\/-assoc : (x y z : [0,1])(pr : y \u2264I z)(pos : Pos z) \u2192 (x \u00b7I (y \/I z < pr , pos >)) \u2261 (x \u00b7I y) \/I z < *-anti x pr , pos >\n *\/-assoc x y 0I pr ()\n *\/-assoc x y 1I pr pos = refl\n *\/-assoc 0I y (z I) pr pos = refl\n *\/-assoc 1I y (z I) pr pos = refl\n *\/-assoc (x I) 0I (z I) pr pos = refl\n *\/-assoc (x I) 1I (z I) () pos\n *\/-assoc (x I) (y I) (z I) (E\u2264E pf) pos = cong _I (\u00b7\/E-assoc x y z pf)\n *\/-assoc (x I) (y I) (.y I) E\u2261E pos = cong _I (\u00b7\/E-identity x)\n\n +I-sym : (x y : [0,1]) \u2192 x +I y \u2261 y +I x\n +I-sym 0I 0I = refl\n +I-sym 0I 1I = refl\n +I-sym 0I (x I) = refl\n +I-sym 1I 0I = refl\n +I-sym 1I 1I = refl\n +I-sym 1I (x I) = refl\n +I-sym (x I) 0I = refl\n +I-sym (x I) 1I = refl\n +I-sym (x I) (y I) = cong _I (+E-sym x y)\n\n +I-assoc : (x y z : [0,1]) \u2192 x +I y +I z \u2261 x +I (y +I z)\n +I-assoc 0I y z = refl\n +I-assoc 1I y z = refl\n +I-assoc (x I) 0I z = refl\n +I-assoc (x I) 1I z = refl\n +I-assoc (x I) (y I) 0I = refl\n +I-assoc (x I) (y I) 1I = refl\n +I-assoc (x I) (y I) (z I) = cong _I (+E-assoc x y z)\n\n \u2264I-trans : {x y z : [0,1]} \u2192 x \u2264I y \u2192 y \u2264I z \u2192 x \u2264I z\n \u2264I-trans z\u2264n le2 = z\u2264n\n \u2264I-trans n\u22641 n\u22641 = n\u22641\n \u2264I-trans (E\u2264E x\u2081) n\u22641 = n\u22641\n \u2264I-trans (E\u2264E x\u2081) (E\u2264E x\u2082) = E\u2264E ( : (A B : Event)(pf : Pos Pr[ B ]) \u2192 [0,1]\n Pr[ A \u2223 B ]< pr > = Pr[ A \u2229 B ] \/I Pr[ B ] < Pr-mono (\u2229-lem A) , pr >\n\n _ind_ : (A B : Event) \u2192 Set\n A ind B = Pr[ A ] \u00b7I Pr[ B ] \u2261 Pr[ A \u2229 B ]\n\n union-bound : (A\u2081 A\u2082 : Event) \u2192 Pr[ (A\u2081 \u222a A\u2082) ] \u2264I Pr[ A\u2081 ] +I Pr[ A\u2082 ]\n union-bound A\u2081 A\u2082 = go allU where\n sA\u2081 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2081 = \u03bb xs \u2192 sumP (pr_\u220b_ A\u2081) xs\n sA\u2082 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2082 = sumP (pr_\u220b_ A\u2082)\n go : {n : \u2115}(xs : Vec U n) \u2192 sumP (pr_\u220b_ (A\u2081 \u222a A\u2082)) xs \u2264I sumP (pr_\u220b_ A\u2081) xs +I sumP (pr_\u220b_ A\u2082) xs\n go [] = \u2264I-refl\n go (x \u2237 xs) with A\u2081 x | A\u2082 x\n ... | true | true rewrite +I-assoc (P x) (sA\u2081 xs) (P x +I sA\u2082 xs)\n | +I-sym (P x) (sA\u2082 xs)\n | sym (+I-assoc (sA\u2081 xs) (sA\u2082 xs) (P x))\n = \u2264I-pres (P x) (\u2264I-mono (P x) (go xs))\n ... | true | false rewrite +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | true rewrite sym (+I-assoc (sA\u2081 xs) (P x) (sA\u2082 xs))\n | +I-sym (sA\u2081 xs) (P x)\n | +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | false = go xs\n\n module RandomVar (V : Set) (_==V_ : V \u2192 V \u2192 Bool) where\n RV : Set\n RV = U \u2192 V\n\n _^-1_ : RV \u2192 V \u2192 Event\n RV ^-1 v = \u03bb x \u2192 RV x ==V v\n\n _\u2261r_ : RV \u2192 V \u2192 Event\n RV \u2261r v = RV ^-1 v\n\n -- sugar, socker et sucre\n Pr[_\u2261_] : RV \u2192 V \u2192 [0,1]\n Pr[ X \u2261 v ] = Pr[ X \u2261r v ]\n\n -- \u2200 (a b) . Pr[X = a and Y = b] = Pr[X = a] \u00b7 Pr[Y = b]\n _indRV_ : RV \u2192 RV \u2192 Set\n X indRV Y = (a b : V) \u2192 (X ^-1 a) ind (Y ^-1 b)\n","old_contents":"module Prob where\n\nopen import Data.Bool\nopen import Data.Nat\nopen import Data.Vec\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\nopen import Data.Sum\n\nopen import Function\n\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Relation.Nullary\n\nrecord ]0,1[-ops (]0,1[ : Set) (_ : (x y : ]0,1[) \u2192 x ) \u2261 (x \u00b7E y) \/E z < \u00b7E-anti\u2082 x pf >\n\n -- proofs \u2261 maybe should be some Equivalence \u2248\n field\n +E-sym : (x y : ]0,1[) \u2192 x +E y \u2261 y +E x\n +E-assoc : (x y z : ]0,1[) \u2192 (x +E y) +E z \u2261 x +E (y +E z)\n \u00b7\/E-identity : (x : ]0,1[){y : ]0,1[} \u2192 x \u2261 (x \u00b7E y) \/E y < \u00b7E-anti\u2081 x >\n\nmodule ]0,1[-\u211a where\n data ]0,1[ : Set where\n 1+_\/2+x+_ : \u2115 \u2192 \u2115 \u2192 ]0,1[\n -- to\u211a : ]0,1] \u2192 \u211a\n -- to\u211a (1+ x \/1+x+ y) = (1 + x) \/\u211a (1 + x + y)\n\n postulate\n _ : (x y : ]0,1[) \u2192 x ) \u2261 (x \u00b7E y) \/E z < \u00b7E-anti\u2082 x pf >\n\n postulate\n +E-sym : (x x\u2081 : ]0,1[) \u2192 x +E x\u2081 \u2261 x\u2081 +E x\n +E-assoc : (x x\u2081 x\u2082 : ]0,1[) \u2192 (x +E x\u2081) +E x\u2082 \u2261 x +E (x\u2081 +E x\u2082)\n \u00b7\/E-identity : (x : ]0,1[) {y : ]0,1[} \u2192 x \u2261 (x \u00b7E y) \/E y < \u00b7E-anti\u2081 x >\n\n ops : ]0,1[-ops ]0,1[ _ : (x : [0,1]) \u2192 Inc x \u2192 ]0,1[\n 0I < () >\n 1I < () >\n (x I) < pos > = x\n\n _+I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I +I x = x\n 1I +I _ = 1I\n x I +I 0I = x I\n x I +I 1I = 1I\n x I +I x\u2081 I = (x +E x\u2081) I\n\n _\u00b7I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I \u00b7I y = 0I\n 1I \u00b7I y = y\n (x I) \u00b7I 0I = 0I\n (x I) \u00b7I 1I = x I\n (x I) \u00b7I (x\u2081 I) = (x \u00b7E x\u2081) I\n\n -- 1\/I1+_ : \u2115 \u2192 [0,1]\n\n\n _\/I_<_,_> : (x y : [0,1]) \u2192 x \u2264I y \u2192 Pos y \u2192 [0,1]\n x \/I 0I < _ , () >\n x \/I 1I < _ , _ > = x\n 0I \/I _ < _ , _ > = 0I\n 1I \/I _ I < () , _ >\n (x I) \/I y I < E\u2264E pf , _ > = (x \/E y < pf >) I\n (x I) \/I .x I < E\u2261E , _ > = 1I\n\n\n +I-identity : (x : [0,1]) \u2192 x \u2261 0I +I x\n +I-identity x = refl\n\n *-anti : (x : [0,1]){y z : [0,1]} \u2192 y \u2264I z \u2192 x \u00b7I y \u2264I z\n *-anti 0I le = z\u2264n\n *-anti 1I le = le\n *-anti (x I) z\u2264n = z\u2264n\n *-anti (x I) n\u22641 = n\u22641\n *-anti (x I) (E\u2264E pf) = E\u2264E (\u00b7E-anti\u2082 x pf)\n *-anti (x I) E\u2261E = E\u2264E (\u00b7E-anti\u2081 x)\n\n *\/-assoc : (x y z : [0,1])(pr : y \u2264I z)(pos : Pos z) \u2192 (x \u00b7I (y \/I z < pr , pos >)) \u2261 (x \u00b7I y) \/I z < *-anti x pr , pos >\n *\/-assoc x y 0I pr ()\n *\/-assoc x y 1I pr pos = refl\n *\/-assoc 0I y (z I) pr pos = refl\n *\/-assoc 1I y (z I) pr pos = refl\n *\/-assoc (x I) 0I (z I) pr pos = refl\n *\/-assoc (x I) 1I (z I) () pos\n *\/-assoc (x I) (y I) (z I) (E\u2264E pf) pos = cong _I (\u00b7\/E-assoc x y z pf)\n *\/-assoc (x I) (y I) (.y I) E\u2261E pos = cong _I (\u00b7\/E-identity x)\n\n +I-sym : (x y : [0,1]) \u2192 x +I y \u2261 y +I x\n +I-sym 0I 0I = refl\n +I-sym 0I 1I = refl\n +I-sym 0I (x I) = refl\n +I-sym 1I 0I = refl\n +I-sym 1I 1I = refl\n +I-sym 1I (x I) = refl\n +I-sym (x I) 0I = refl\n +I-sym (x I) 1I = refl\n +I-sym (x I) (y I) = cong _I (+E-sym x y)\n\n +I-assoc : (x y z : [0,1]) \u2192 x +I y +I z \u2261 x +I (y +I z)\n +I-assoc 0I y z = refl\n +I-assoc 1I y z = refl\n +I-assoc (x I) 0I z = refl\n +I-assoc (x I) 1I z = refl\n +I-assoc (x I) (y I) 0I = refl\n +I-assoc (x I) (y I) 1I = refl\n +I-assoc (x I) (y I) (z I) = cong _I (+E-assoc x y z)\n\n \u2264I-trans : {x y z : [0,1]} \u2192 x \u2264I y \u2192 y \u2264I z \u2192 x \u2264I z\n \u2264I-trans z\u2264n le2 = z\u2264n\n \u2264I-trans n\u22641 n\u22641 = n\u22641\n \u2264I-trans (E\u2264E x\u2081) n\u22641 = n\u22641\n \u2264I-trans (E\u2264E x\u2081) (E\u2264E x\u2082) = E\u2264E ( : (A B : Event)(pf : Pos Pr[ B ]) \u2192 [0,1]\n Pr[ A \u2223 B ]< pr > = Pr[ A \u2229 B ] \/I Pr[ B ] < Pr-mono (\u2229-lem A) , pr >\n\n _ind_ : (A B : Event) \u2192 Set\n A ind B = Pr[ A ] \u00b7I Pr[ B ] \u2261 Pr[ A \u2229 B ]\n\n union-bound : (A\u2081 A\u2082 : Event) \u2192 Pr[ (A\u2081 \u222a A\u2082) ] \u2264I Pr[ A\u2081 ] +I Pr[ A\u2082 ]\n union-bound A\u2081 A\u2082 = go allU where\n sA\u2081 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2081 = \u03bb xs \u2192 sumP (pr_\u220b_ A\u2081) xs\n sA\u2082 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2082 = sumP (pr_\u220b_ A\u2082)\n go : {n : \u2115}(xs : Vec U n) \u2192 sumP (pr_\u220b_ (A\u2081 \u222a A\u2082)) xs \u2264I sumP (pr_\u220b_ A\u2081) xs +I sumP (pr_\u220b_ A\u2082) xs\n go [] = \u2264I-refl\n go (x \u2237 xs) with A\u2081 x | A\u2082 x\n ... | true | true rewrite +I-assoc (P x) (sA\u2081 xs) (P x +I sA\u2082 xs)\n | +I-sym (P x) (sA\u2082 xs)\n | sym (+I-assoc (sA\u2081 xs) (sA\u2082 xs) (P x))\n = \u2264I-pres (P x) (\u2264I-mono (P x) (go xs))\n ... | true | false rewrite +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | true rewrite sym (+I-assoc (sA\u2081 xs) (P x) (sA\u2082 xs))\n | +I-sym (sA\u2081 xs) (P x)\n | +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | false = go xs\n\n module RandomVar (V : Set) (_==V_ : V \u2192 V \u2192 Bool) where\n RV : Set\n RV = U \u2192 V\n\n _^-1_ : RV \u2192 V \u2192 Event\n RV ^-1 v = \u03bb x \u2192 RV x ==V v\n\n _\u2261r_ : RV \u2192 V \u2192 Event\n RV \u2261r v = RV ^-1 v\n\n -- sugar, socker et sucre\n Pr[_\u2261_] : RV \u2192 V \u2192 [0,1]\n Pr[ X \u2261 v ] = Pr[ X \u2261r v ]\n\n -- \u2200 (a b) . Pr[X = a and Y = b] = Pr[X = a] \u00b7 Pr[Y = b]\n _indRV_ : RV \u2192 RV \u2192 Set\n X indRV Y = (a b : V) \u2192 (X ^-1 a) ind (Y ^-1 b)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"0270576411d565e7daa6f53f108806d14fdc6582","subject":"Finished defining the SMC structure.","message":"Finished defining the SMC structure.\n","repos":"heades\/AUGL","old_file":"dialectica-cats\/DC2Sets.agda","new_file":"dialectica-cats\/DC2Sets.agda","new_contents":"-----------------------------------------------------------------------\n-- This file defines DC\u2082(Sets) and its SMC structure. --\n-----------------------------------------------------------------------\nmodule DC2Sets where\n\nopen import prelude\n\n-- The objects:\nObj : Set\u2081\nObj = \u03a3[ U \u2208 Set ] (\u03a3[ X \u2208 Set ] (U \u2192 X \u2192 Set))\n\n-- The morphisms:\nHom : Obj \u2192 Obj \u2192 Set\nHom (U , X , \u03b1) (V , Y , \u03b2) =\n \u03a3[ f \u2208 (U \u2192 V) ]\n (\u03a3[ F \u2208 (U \u2192 Y \u2192 X) ] (\u2200{u : U}{y : Y} \u2192 \u03b1 u (F u y) \u2192 \u03b2 (f u) y))\n\n-- Composition:\ncomp : {A B C : Obj} \u2192 Hom A B \u2192 Hom B C \u2192 Hom A C\ncomp {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)} (f , F , p\u2081) (g , G , p\u2082) =\n (g \u2218 f , (\u03bb u z \u2192 F u (G (f u) z)), (\u03bb {u} {y} p\u2083 \u2192 p\u2082 (p\u2081 p\u2083)))\n\ninfixl 5 _\u25cb_\n\n_\u25cb_ = comp\n\n-- The contravariant hom-functor:\nHom\u2090 : {A' A B B' : Obj} \u2192 Hom A' A \u2192 Hom B B' \u2192 Hom A B \u2192 Hom A' B'\nHom\u2090 f h g = comp f (comp g h)\n\n-- The identity function:\nid : {A : Obj} \u2192 Hom A A \nid {(U , V , \u03b1)} = (id-set , curry snd , id-set)\n\n-- In this formalization we will only worry about proving that the\n-- data of morphisms are equivalent, and not worry about the morphism\n-- conditions. This will make proofs shorter and faster.\n--\n-- If we have parallel morphisms (f,F) and (g,G) in which we know that\n-- f = g and F = G, then the condition for (f,F) will imply the\n-- condition of (g,G) and vice versa. Thus, we can safely ignore it.\ninfix 4 _\u2261h_\n\n_\u2261h_ : {A B : Obj} \u2192 (f g : Hom A B) \u2192 Set\n_\u2261h_ {(U , X , \u03b1)}{(V , Y , \u03b2)} (f , F , p\u2081) (g , G , p\u2082) = f \u2261 g \u00d7 F \u2261 G\n\n\u2261h-refl : {A B : Obj}{f : Hom A B} \u2192 f \u2261h f\n\u2261h-refl {U , X , \u03b1}{V , Y , \u03b2}{f , F , _} = refl , refl\n\n\u2261h-trans : \u2200{A B}{f g h : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h h \u2192 f \u2261h h\n\u2261h-trans {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _}{h , H , _} (p\u2081 , p\u2082) (p\u2083 , p\u2084) rewrite p\u2081 | p\u2082 | p\u2083 | p\u2084 = refl , refl\n\n\u2261h-sym : \u2200{A B}{f g : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h f\n\u2261h-sym {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\n\u2261h-subst-\u25cb : \u2200{A B C}{f\u2081 f\u2082 : Hom A B}{g\u2081 g\u2082 : Hom B C}{j : Hom A C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 g\u2081 \u2261h g\u2082\n \u2192 f\u2082 \u25cb g\u2082 \u2261h j\n \u2192 f\u2081 \u25cb g\u2081 \u2261h j\n\u2261h-subst-\u25cb {U , X , \u03b1}\n {V , Y , \u03b2}\n {W , Z , \u03b3}\n {f\u2081 , F\u2081 , _}\n {f\u2082 , F\u2082 , _}\n {g\u2081 , G\u2081 , _}\n {g\u2082 , G\u2082 , _}\n {j , J , _}\n (p\u2085 , p\u2086) (p\u2087 , p\u2088) (p\u2089 , p\u2081\u2080) rewrite p\u2085 | p\u2086 | p\u2087 | p\u2088 | p\u2089 | p\u2081\u2080 = refl , refl\n\n\u25cb-assoc : \u2200{A B C D}{f : Hom A B}{g : Hom B C}{h : Hom C D}\n \u2192 f \u25cb (g \u25cb h) \u2261h (f \u25cb g) \u25cb h\n\u25cb-assoc {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{S , T , \u03b9}\n {f , F , _}{g , G , _}{h , H , _} = refl , refl\n\n\u25cb-idl : \u2200{A B}{f : Hom A B} \u2192 id \u25cb f \u2261h f\n\u25cb-idl {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\u25cb-idr : \u2200{A B}{f : Hom A B} \u2192 f \u25cb id \u2261h f\n\u25cb-idr {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\n-----------------------------------------------------------------------\n-- DC\u2082(Sets) is a SMC --\n-----------------------------------------------------------------------\n\n-- The tensor functor: \u2297\n_\u2297\u1d63_ : \u2200{U X V Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 ((U \u00d7 V) \u2192 (X \u00d7 Y) \u2192 Set)\n_\u2297\u1d63_ \u03b1 \u03b2 (u , v) (x , y) = (\u03b1 u x) \u00d7 (\u03b2 v y)\n\n_\u2297\u2092_ : (A B : Obj) \u2192 Obj\n(U , X , \u03b1) \u2297\u2092 (V , Y , \u03b2) = ((U \u00d7 V) , (X \u00d7 Y) , \u03b1 \u2297\u1d63 \u03b2)\n\nF\u2297 : \u2200{Z T V X U Y : Set}{F : U \u2192 Z \u2192 X}{G : V \u2192 T \u2192 Y} \u2192 (U \u00d7 V) \u2192 (Z \u00d7 T) \u2192 (X \u00d7 Y)\nF\u2297 {F = F}{G} (u , v) (z , t) = F u z , G v t\n \n_\u2297\u2090_ : {A B C D : Obj} \u2192 Hom A C \u2192 Hom B D \u2192 Hom (A \u2297\u2092 B) (C \u2297\u2092 D)\n_\u2297\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) = \u27e8 f , g \u27e9 , F\u2297 {F = F}{G} , p\u2297\n where\n p\u2297 : {u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Z (\u03bb x \u2192 T)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (F\u2297 {F = F}{G} u y) \u2192 (\u03b3 \u2297\u1d63 \u03b4) (\u27e8 f , g \u27e9 u) y\n p\u2297 {u , v}{z , t} (p\u2083 , p\u2084) = p\u2081 p\u2083 , p\u2082 p\u2084\n\n\n-- The unit for tensor:\n\u03b9 : \u22a4 \u2192 \u22a4 \u2192 Set\n\u03b9 triv triv = \u22a4\n\nI : Obj\nI = (\u22a4 , \u22a4 , \u03b9)\n\nJ : Obj\nJ = (\u22a4 , \u22a4 , (\u03bb x y \u2192 \u22a5))\n\n\n-- The left-unitor:\n\u03bb\u2297-p : \u2200{U X \u03b1}{u : \u03a3 \u22a4 (\u03bb x \u2192 U)} {y : X} \u2192 (\u03b9 \u2297\u1d63 \u03b1) u (triv , y) \u2192 \u03b1 (snd u) y\n\u03bb\u2297-p {U}{X}{\u03b1}{(triv , u)}{x} = snd\n \n\u03bb\u2297 : \u2200{A : Obj} \u2192 Hom (I \u2297\u2092 A) A\n\u03bb\u2297 {(U , X , \u03b1)} = snd , (\u03bb _ x \u2192 triv , x) , \u03bb\u2297-p\n\n\u03bb\u2297-inv : \u2200{A : Obj} \u2192 Hom A (I \u2297\u2092 A)\n\u03bb\u2297-inv {(U , X , \u03b1)} = (\u03bb u \u2192 triv , u) , (\u03bb _ r \u2192 snd r) , \u03bb\u2297-inv-p\n where\n \u03bb\u2297-inv-p : \u2200{U X \u03b1}{u : U} {y : \u03a3 \u22a4 (\u03bb x \u2192 X)} \u2192 \u03b1 u (snd y) \u2192 (\u03b9 \u2297\u1d63 \u03b1) (triv , u) y\n \u03bb\u2297-inv-p {U}{X}{\u03b1}{u}{triv , x} p = triv , p\n\n-- The right-unitor:\n\u03c1\u2297 : \u2200{A : Obj} \u2192 Hom (A \u2297\u2092 I) A\n\u03c1\u2297 {(U , X , \u03b1)} = fst , (\u03bb r x \u2192 x , triv) , \u03c1\u2297-p\n where\n \u03c1\u2297-p : \u2200{U X \u03b1}{u : \u03a3 U (\u03bb x \u2192 \u22a4)} {y : X} \u2192 (\u03b1 \u2297\u1d63 \u03b9) u (y , triv) \u2192 \u03b1 (fst u) y\n \u03c1\u2297-p {U}{X}{\u03b1}{(u , _)}{x} (p , _) = p\n\n\n\u03c1\u2297-inv : \u2200{A : Obj} \u2192 Hom A (A \u2297\u2092 I)\n\u03c1\u2297-inv {(U , X , \u03b1)} = (\u03bb u \u2192 u , triv) , (\u03bb u r \u2192 fst r) , \u03c1\u2297-p-inv\n where\n \u03c1\u2297-p-inv : \u2200{U X \u03b1}{u : U} {y : \u03a3 X (\u03bb x \u2192 \u22a4)} \u2192 \u03b1 u (fst y) \u2192 (\u03b1 \u2297\u1d63 \u03b9) (u , triv) y\n \u03c1\u2297-p-inv {U}{X}{\u03b1}{u}{x , triv} p = p , triv\n\n-- Symmetry:\n\u03b2\u2297 : \u2200{A B : Obj} \u2192 Hom (A \u2297\u2092 B) (B \u2297\u2092 A)\n\u03b2\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)} = twist-\u00d7 , (\u03bb r\u2081 r\u2082 \u2192 twist-\u00d7 r\u2082) , \u03b2\u2297-p\n where\n \u03b2\u2297-p : \u2200{U V Y X \u03b1 \u03b2}{u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Y (\u03bb x \u2192 X)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (twist-\u00d7 y) \u2192 (\u03b2 \u2297\u1d63 \u03b1) (twist-\u00d7 u) y\n \u03b2\u2297-p {U}{V}{Y}{X}{\u03b1}{\u03b2}{u , v}{y , x} = twist-\u00d7\n\n\n-- The associator:\n\u03b1\u2297-inv : \u2200{A B C : Obj} \u2192 Hom (A \u2297\u2092 (B \u2297\u2092 C)) ((A \u2297\u2092 B) \u2297\u2092 C)\n\u03b1\u2297-inv {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = rl-assoc-\u00d7 , F\u03b1-inv , \u03b1-inv-cond\n where\n F\u03b1-inv : (U \u00d7 (V \u00d7 W)) \u2192 ((X \u00d7 Y) \u00d7 Z) \u2192 (X \u00d7 (Y \u00d7 Z))\n F\u03b1-inv (u , (v , w)) ((x , y) , z) = x , y , z\n \u03b1-inv-cond : {u : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))}{y : \u03a3 (\u03a3 X (\u03bb x \u2192 Y)) (\u03bb x \u2192 Z)}\n \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) u (F\u03b1-inv u y)\n \u2192 ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) (rl-assoc-\u00d7 u) y\n \u03b1-inv-cond {u , (v , w)}{(x , y) , z} (p\u2081 , (p\u2082 , p\u2083)) = (p\u2081 , p\u2082) , p\u2083\n\n\nF\u03b1 : \u2200{V W X Y U Z : Set} \u2192 ((U \u00d7 V) \u00d7 W) \u2192 (X \u00d7 (Y \u00d7 Z)) \u2192 ((X \u00d7 Y) \u00d7 Z)\nF\u03b1 {V}{W}{X}{Y}{U}{Z} ((u , v) , w) (x , (y , z)) = (x , y) , z\n\n\u03b1\u2297 : \u2200{A B C : Obj} \u2192 Hom ((A \u2297\u2092 B) \u2297\u2092 C) (A \u2297\u2092 (B \u2297\u2092 C)) \n\u03b1\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = (lr-assoc-\u00d7 , F\u03b1 , \u03b1-cond)\n where\n \u03b1-cond : {u : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)}\n {y : \u03a3 X (\u03bb x \u2192 \u03a3 Y (\u03bb x\u2081 \u2192 Z))} \u2192\n ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) u (F\u03b1 u y) \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) (lr-assoc-\u00d7 u) y\n \u03b1-cond {(u , v) , w}{x , (y , z)} ((p\u2081 , p\u2082) , p\u2083) = p\u2081 , p\u2082 , p\u2083\n{-\n\u03b1\u2297-id\u2081 : \u2200{A B C} \u2192 (\u03b1\u2297 {A}{B}{C}) \u25cb \u03b1\u2297-inv \u2261h id\n\u03b1\u2297-id\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)} \u2192 rl-assoc-\u00d7 (lr-assoc-\u00d7 a) \u2261 a\n aux {(u , v) , w} = refl\n\n aux' : {a : \u03a3 (W \u2192 \u03a3 (V \u2192 X) (\u03bb x \u2192 U \u2192 Y)) (\u03bb x \u2192 \u03a3 U (\u03bb x\u2081 \u2192 V) \u2192 Z)}\n \u2192 ((\u03bb x \u2192 (\u03bb x\u2081 \u2192 fst (fst a x) x\u2081) , (\u03bb x\u2081 \u2192 snd (fst a x) x\u2081)) , (\u03bb x \u2192 snd a (fst x , snd x))) \u2261 a\n aux' {j\u2081 , j\u2082} = eq-\u00d7 (ext-set aux'') (ext-set aux''')\n where\n aux'' : {a : W} \u2192 (fst (j\u2081 a) , snd (j\u2081 a)) \u2261 j\u2081 a\n aux'' {w} with j\u2081 w\n ... | h\u2081 , h\u2082 = refl\n\n aux''' : {a : \u03a3 U (\u03bb x\u2081 \u2192 V)} \u2192 j\u2082 (fst a , snd a) \u2261 j\u2082 a\n aux''' {u , v} = refl\n\n\u03b1\u2297-id\u2082 : \u2200{A B C} \u2192 (\u03b1\u2297-inv {A}{B}{C}) \u25cb \u03b1\u2297 \u2261h id\n\u03b1\u2297-id\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))} \u2192 lr-assoc-\u00d7 (rl-assoc-\u00d7 a) \u2261 a\n aux {u , (v , w)} = refl\n aux' : {a\n : \u03a3 (\u03a3 V (\u03bb x \u2192 W) \u2192 X) (\u03bb x \u2192 U \u2192 \u03a3 (W \u2192 Y) (\u03bb x\u2081 \u2192 V \u2192 Z))} \u2192\n ((\u03bb p' \u2192 fst (fst (F\u03b1 {V} {W} {X} {Y} {U} {V} {Z} a) (snd p')) (fst p')) ,\n (\u03bb u \u2192 (\u03bb w \u2192 snd (fst (F\u03b1 {V} {W} {X} {Y} {U} {V} {Z} a) w) u) , (\u03bb v \u2192 snd (F\u03b1 {V} {W} {X} {Y} {U} {V} {Z} a) (u , v))))\n \u2261 a\n aux' {j\u2081 , j\u2082} = eq-\u00d7 (ext-set aux'') (ext-set aux''')\n where\n aux'' : {a : \u03a3 V (\u03bb x \u2192 W)} \u2192 j\u2081 (fst a , snd a) \u2261 j\u2081 a\n aux'' {v , w} = refl\n aux''' : {a : U} \u2192 ((\u03bb w \u2192 fst (j\u2082 a) w) , (\u03bb v \u2192 snd (j\u2082 a) v)) \u2261 j\u2082 a\n aux''' {u} with j\u2082 u\n ... | h\u2081 , h\u2082 = refl\n \n-- Internal hom:\n\u22b8-cond : \u2200{U V X Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 (U \u2192 V) \u00d7 (Y \u2192 X) \u2192 U \u00d7 Y \u2192 Set\n\u22b8-cond \u03b1 \u03b2 (f , g) (u , y) = \u03b1 u (g y) \u2192 \u03b2 (f u) y\n\n_\u22b8\u2092_ : Obj \u2192 Obj \u2192 Obj\n(U , X , \u03b1) \u22b8\u2092 (V , Y , \u03b2) = ((U \u2192 V) \u00d7 (Y \u2192 X)) , (U \u00d7 Y) , \u22b8-cond \u03b1 \u03b2\n\n_\u22b8\u2090_ : {A B C D : Obj} \u2192 Hom C A \u2192 Hom B D \u2192 Hom (A \u22b8\u2092 B) (C \u22b8\u2092 D)\n_\u22b8\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) =\n h , H , p\u2083\n where\n h : \u03a3 (U \u2192 V) (\u03bb x \u2192 Y \u2192 X) \u2192 \u03a3 (W \u2192 S) (\u03bb x \u2192 T \u2192 Z)\n h (h\u2081 , h\u2082) = (\u03bb w \u2192 g (h\u2081 (f w))) , (\u03bb t \u2192 F (h\u2082 (G t)))\n H : \u03a3 W (\u03bb x \u2192 T) \u2192 \u03a3 U (\u03bb x \u2192 Y)\n H (w , t) = f w , G t\n p\u2083 : {u : \u03a3 (U \u2192 V) (\u03bb x \u2192 Y \u2192 X)} {y : \u03a3 W (\u03bb x \u2192 T)} \u2192 \u22b8-cond \u03b1 \u03b2 u (H y) \u2192 \u22b8-cond \u03b3 \u03b4 (h u) y\n p\u2083 {h\u2081 , h\u2082}{w , t} c c' = p\u2082 (c (p\u2081 c'))\n\ncur : {A B C : Obj}\n \u2192 Hom (A \u2297\u2092 B) C\n \u2192 Hom A (B \u22b8\u2092 C)\ncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb u \u2192 (\u03bb v \u2192 f (u , v)) , (\u03bb z \u2192 snd (F z) u)) , (\u03bb p \u2192 fst (F (snd p)) (fst p)) , cur-cond\n where\n cur-cond : \u2200{u : U}{y : \u03a3 V (\u03bb x \u2192 Z)}\n \u2192 \u03b1 u (fst (F (snd y)) (fst y))\n \u2192 \u22b8-cond \u03b2 \u03b3 ((\u03bb v \u2192 f (u , v)) , (\u03bb z \u2192 snd (F z) u)) y\n cur-cond {u}{v , z} p\u2082 p\u2083 with p\u2081 {u , v}{z} \n ... | p\u2081' with F z\n ... | (j\u2081 , j\u2082) = p\u2081' (p\u2082 , p\u2083)\n\ncur-\u2261h : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-\u2261h {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}\n {f\u2081 , F\u2081 , p\u2081}{f\u2082 , F\u2082 , p\u2082} (p\u2083 , p\u2084)\n rewrite p\u2083 | p\u2084 = refl , refl\n\ncur-cong : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C} \u2192 f\u2081 \u2261h f\u2082 \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-cong {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)}{f\u2081 , F\u2081 , _}{f\u2082 , F\u2082 , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\nuncur : {A B C : Obj}\n \u2192 Hom A (B \u22b8\u2092 C)\n \u2192 Hom (A \u2297\u2092 B) C\nuncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb p \u2192 fst (f (fst p)) (snd p)) , (\u03bb z \u2192 (\u03bb v \u2192 F (v , z)) , (\u03bb u \u2192 snd (f u) z)) , uncur-cond\n where\n uncur-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : Z}\n \u2192 (\u03b1 \u2297\u1d63 \u03b2) u ((\u03bb v \u2192 F (v , y)) , (\u03bb u\u2081 \u2192 snd (f u\u2081) y))\n \u2192 \u03b3 (fst (f (fst u)) (snd u)) y\n uncur-cond {u , v}{z} (p\u2082 , p\u2083) with p\u2081 {u}{v , z} p\u2082\n ... | p\u2081' with f u\n ... | (j\u2081 , j\u2082) = p\u2081' p\u2083\n\ncur-uncur-bij\u2081 : \u2200{A B C}{f : Hom (A \u2297\u2092 B) C}\n \u2192 uncur (cur f) \u2261h f\ncur-uncur-bij\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{f , F , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : \u03a3 U (\u03bb x \u2192 V)} \u2192 f (fst a , snd a) \u2261 f a\n aux\u2081 {u , v} = refl\n \n aux\u2082 : {a : Z} \u2192 ((\u03bb v \u2192 fst (F a) v) , (\u03bb u \u2192 snd (F a) u)) \u2261 F a\n aux\u2082 {z} with F z\n ... | j\u2081 , j\u2082 = refl\n\ncur-uncur-bij\u2082 : \u2200{A B C}{g : Hom A (B \u22b8\u2092 C)}\n \u2192 cur (uncur g) \u2261h g\ncur-uncur-bij\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{g , G , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : U} \u2192 ((\u03bb v \u2192 fst (g a) v) , (\u03bb z \u2192 snd (g a) z)) \u2261 g a\n aux\u2081 {u} with g u\n ... | (j\u2081 , j\u2082) = refl\n\n aux\u2082 : {a : \u03a3 V (\u03bb x \u2192 Z)} \u2192 G (fst a , snd a) \u2261 G a\n aux\u2082 {v , z} = refl\n \n-- The of-course exponential:\n!\u2092-cond : \u2200{U X : Set}\n \u2192 (U \u2192 X \u2192 Set)\n \u2192 U\n \u2192 (U \u2192 X *)\n \u2192 Set\n!\u2092-cond \u03b1 u f = all-pred (\u03b1 u) (f u)\n \n!\u2092 : Obj \u2192 Obj\n!\u2092 (U , X , \u03b1) = U , (U \u2192 X *) , !\u2092-cond \u03b1\n\n!-cta : {V Y U X : Set}\n \u2192 (Y \u2192 X)\n \u2192 (U \u2192 V)\n \u2192 (V \u2192 Y *)\n \u2192 (U \u2192 X *)\n!-cta F f g = \u03bb u \u2192 list-funct F (g (f u))\n\n!\u2090-cond : \u2200{U V Y X : Set}{F : Y \u2192 X}{f : U \u2192 V}\n \u2192 (\u03b1 : U \u2192 X \u2192 Set)\n \u2192 (\u03b2 : V \u2192 Y \u2192 Set)\n \u2192 (p : {u : U} {y : Y} \u2192 \u03b1 u (F y) \u2192 \u03b2 (f u) y)\n {u : U}{l : Y *}\n \u2192 all-pred (\u03b1 u) (list-funct F l)\n \u2192 all-pred (\u03b2 (f u)) l\n!\u2090-cond _ _ _ {l = []} _ = triv\n!\u2090-cond \u03b1 \u03b2 p {u}{x :: xs} (p' , p'') = p p' , !\u2090-cond \u03b1 \u03b2 p p'' \n \n!\u2090 : {A B : Obj} \u2192 Hom A B \u2192 Hom (!\u2092 A) (!\u2092 B)\n!\u2090 {U , X , \u03b1}{V , Y , \u03b2} (f , F , p) = f , !-cta F f , !\u2090-cond \u03b1 \u03b2 p\n\n-- Of-course is a comonad:\n\u03b5 : \u2200{A} \u2192 Hom (!\u2092 A) A\n\u03b5 {U , X , \u03b1} = id-set , (\u03bb x y \u2192 [ x ]) , fst\n\n\u03b4-cta : {U X : Set} \u2192 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X)) \u2192 U \u2192 \ud835\udd43 X\n\u03b4-cta g u = foldr (\u03bb f rest \u2192 (f u) ++ rest) [] (g u)\n \n\u03b4 : \u2200{A} \u2192 Hom (!\u2092 A) (!\u2092 (!\u2092 A))\n\u03b4 {U , X , \u03b1} = id-set , \u03b4-cta , \u03b4-cond\n where\n \u03b4-cond : {u : U} {l : \ud835\udd43 (U \u2192 \ud835\udd43 X)}\n \u2192 all-pred (\u03b1 u) (foldr (\u03bb f \u2192 _++_ (f u)) [] l)\n \u2192 all-pred (\u03bb f\n \u2192 all-pred (\u03b1 u) (f u)) l\n \u03b4-cond {l = []} _ = triv\n \u03b4-cond {u}{l = x :: l'} p with\n all-pred-append {X}{\u03b1 u}\n {x u}\n {foldr (\u03bb f \u2192 _++_ (f u)) [] l'}\n \u2227-unit \u2227-assoc\n ... | p' rewrite p' = fst p , \u03b4-cond {u} {l'} (snd p)\n\n-- These diagrams can be found on page 22 of the report.\ncomonand-diag\u2081 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (!\u2090 (\u03b4 {A})) \u2261h (\u03b4 {A}) \u25cb (\u03b4 { !\u2092 A})\ncomonand-diag\u2081 {U , X , \u03b1} =\n refl , ext-set (\u03bb {a} \u2192 ext-set (\u03bb {a\u2081} \u2192 aux {a\u2081}{a a\u2081}))\n where\n aux : \u2200{a\u2081 : U}{l : \ud835\udd43 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X))} \u2192\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) []\n (map (\u03bb g u \u2192 foldr (\u03bb f \u2192 _++_ (f u)) [] (g u)) l)\n \u2261\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] (foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] l)\n aux {a}{[]} = refl \n aux {a}{x :: l} rewrite\n sym (foldr-append {l\u2081 = x a}{foldr (\u03bb f \u2192 _++_ (f a)) [] l}{a})\n = cong2 {a = foldr (\u03bb f \u2192 _++_ (f a)) [] (x a)}\n _++_\n refl\n (aux {a}{l})\n\ncomonand-diag\u2082 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (\u03b5 { !\u2092 A}) \u2261h (\u03b4 {A}) \u25cb (!\u2090 (\u03b5 {A}))\ncomonand-diag\u2082 {U , X , \u03b1} =\n refl , ext-set (\u03bb {f} \u2192 ext-set (\u03bb {a} \u2192 aux {a}{f a}))\n where\n aux : \u2200{a : U}{l : X *}\n \u2192 l ++ [] \u2261 foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x y \u2192 x :: []) l)\n aux {a}{[]} = refl\n aux {a}{x :: l} with aux {a}{l}\n ... | IH rewrite ++[] l =\n cong2 {a = x} {x} {l}\n {foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x\u2081 y \u2192 x\u2081 :: []) l)} _::_ refl\n IH\n \nmodule Cartesian where\n \u03c0\u2081 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (U , X , \u03b1))\n \u03c0\u2081 {U}{X}{V}{Y}{\u03b1}{\u03b2} = fst , (\u03bb f \u2192 (\u03bb v u \u2192 f u) , (\u03bb u v \u2192 [])) , \u03c0\u2081-cond\n where\n \u03c0\u2081-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : U \u2192 \ud835\udd43 X} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 y u\u2081) , (\u03bb u\u2081 v \u2192 [])) \u2192\n all-pred (\u03b1 (fst u)) (y (fst u))\n \u03c0\u2081-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2081\n\n \u03c0\u2082 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (V , Y , \u03b2))\n \u03c0\u2082 {U}{X}{V}{Y}{\u03b1}{\u03b2} = snd , (\u03bb f \u2192 (\u03bb v u \u2192 []) , (\u03bb u v \u2192 f v)) , \u03c0\u2082-cond\n where\n \u03c0\u2082-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : V \u2192 \ud835\udd43 Y} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 []) , (\u03bb u\u2081 v \u2192 y v)) \u2192\n all-pred (\u03b2 (snd u)) (y (snd u))\n \u03c0\u2082-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2082\n\n cart-ar-crt : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y) \u2192 W \u2192 \ud835\udd43 Z\n cart-ar-crt (f , F , p\u2081) (g , G , p\u2082) (j\u2081 , j\u2082) w = F (j\u2081 (g w)) w ++ G (j\u2082 (f w)) w\n\n cart-ar : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))\n cart-ar {U}{X}{V}{Y}{W}{Z}{\u03b1}{\u03b2}{\u03b3} (f , F , p\u2081) (g , G , p\u2082)\n = (\u03bb w \u2192 f w , g w) , cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) , cart-ar-cond\n where\n cart-ar-cond : \u2200{u : W} {y : \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y)} \u2192\n all-pred (\u03b3 u) (cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) y u) \u2192\n ((\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b1 u\u2081) (f\u2081 u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b2 u\u2081) (f\u2081 u\u2081)))\n (f u , g u) y\n cart-ar-cond {w}{j\u2081 , j\u2082} p\n rewrite\n all-pred-append {f = \u03b3 w}{F (j\u2081 (g w)) w}{G (j\u2082 (f w)) w} \u2227-unit \u2227-assoc with p\n ... | (a , b) = p\u2081 a , p\u2082 b\n\n cart-diag\u2081 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (U , X , \u03b1)}\n (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (U , X , \u03b1)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2081)\n cart-diag\u2081 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 sym (++[] (map F (j (f w))))))\n\n cart-diag\u2082 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (V , Y , \u03b2)}\n (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (V , Y , \u03b2)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2082)\n cart-diag\u2082 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 refl))\n-}\n","old_contents":"-----------------------------------------------------------------------\n-- This file defines DC\u2082(Sets) and its SMC structure. --\n-----------------------------------------------------------------------\nmodule DC2Sets where\n\nopen import prelude\n\n-- The objects:\nObj : Set\u2081\nObj = \u03a3[ U \u2208 Set ] (\u03a3[ X \u2208 Set ] (U \u2192 X \u2192 Set))\n\n-- The morphisms:\nHom : Obj \u2192 Obj \u2192 Set\nHom (U , X , \u03b1) (V , Y , \u03b2) =\n \u03a3[ f \u2208 (U \u2192 V) ]\n (\u03a3[ F \u2208 (U \u2192 Y \u2192 X) ] (\u2200{u : U}{y : Y} \u2192 \u03b1 u (F u y) \u2192 \u03b2 (f u) y))\n\n-- Composition:\ncomp : {A B C : Obj} \u2192 Hom A B \u2192 Hom B C \u2192 Hom A C\ncomp {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)} (f , F , p\u2081) (g , G , p\u2082) =\n (g \u2218 f , (\u03bb u z \u2192 F u (G (f u) z)), (\u03bb {u} {y} p\u2083 \u2192 p\u2082 (p\u2081 p\u2083)))\n\ninfixl 5 _\u25cb_\n\n_\u25cb_ = comp\n\n-- The contravariant hom-functor:\nHom\u2090 : {A' A B B' : Obj} \u2192 Hom A' A \u2192 Hom B B' \u2192 Hom A B \u2192 Hom A' B'\nHom\u2090 f h g = comp f (comp g h)\n\n-- The identity function:\nid : {A : Obj} \u2192 Hom A A \nid {(U , V , \u03b1)} = (id-set , curry snd , id-set)\n\n-- In this formalization we will only worry about proving that the\n-- data of morphisms are equivalent, and not worry about the morphism\n-- conditions. This will make proofs shorter and faster.\n--\n-- If we have parallel morphisms (f,F) and (g,G) in which we know that\n-- f = g and F = G, then the condition for (f,F) will imply the\n-- condition of (g,G) and vice versa. Thus, we can safely ignore it.\ninfix 4 _\u2261h_\n\n_\u2261h_ : {A B : Obj} \u2192 (f g : Hom A B) \u2192 Set\n_\u2261h_ {(U , X , \u03b1)}{(V , Y , \u03b2)} (f , F , p\u2081) (g , G , p\u2082) = f \u2261 g \u00d7 F \u2261 G\n\n\u2261h-refl : {A B : Obj}{f : Hom A B} \u2192 f \u2261h f\n\u2261h-refl {U , X , \u03b1}{V , Y , \u03b2}{f , F , _} = refl , refl\n\n\u2261h-trans : \u2200{A B}{f g h : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h h \u2192 f \u2261h h\n\u2261h-trans {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _}{h , H , _} (p\u2081 , p\u2082) (p\u2083 , p\u2084) rewrite p\u2081 | p\u2082 | p\u2083 | p\u2084 = refl , refl\n\n\u2261h-sym : \u2200{A B}{f g : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h f\n\u2261h-sym {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\n\u2261h-subst-\u25cb : \u2200{A B C}{f\u2081 f\u2082 : Hom A B}{g\u2081 g\u2082 : Hom B C}{j : Hom A C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 g\u2081 \u2261h g\u2082\n \u2192 f\u2082 \u25cb g\u2082 \u2261h j\n \u2192 f\u2081 \u25cb g\u2081 \u2261h j\n\u2261h-subst-\u25cb {U , X , \u03b1}\n {V , Y , \u03b2}\n {W , Z , \u03b3}\n {f\u2081 , F\u2081 , _}\n {f\u2082 , F\u2082 , _}\n {g\u2081 , G\u2081 , _}\n {g\u2082 , G\u2082 , _}\n {j , J , _}\n (p\u2085 , p\u2086) (p\u2087 , p\u2088) (p\u2089 , p\u2081\u2080) rewrite p\u2085 | p\u2086 | p\u2087 | p\u2088 | p\u2089 | p\u2081\u2080 = refl , refl\n\n\u25cb-assoc : \u2200{A B C D}{f : Hom A B}{g : Hom B C}{h : Hom C D}\n \u2192 f \u25cb (g \u25cb h) \u2261h (f \u25cb g) \u25cb h\n\u25cb-assoc {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{S , T , \u03b9}\n {f , F , _}{g , G , _}{h , H , _} = refl , refl\n\n\u25cb-idl : \u2200{A B}{f : Hom A B} \u2192 id \u25cb f \u2261h f\n\u25cb-idl {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\u25cb-idr : \u2200{A B}{f : Hom A B} \u2192 f \u25cb id \u2261h f\n\u25cb-idr {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\n-----------------------------------------------------------------------\n-- DC\u2082(Sets) is a SMC --\n-----------------------------------------------------------------------\n\n-- The tensor functor: \u2297\n_\u2297\u1d63_ : \u2200{U X V Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 ((U \u00d7 V) \u2192 (X \u00d7 Y) \u2192 Set)\n_\u2297\u1d63_ \u03b1 \u03b2 (u , v) (x , y) = (\u03b1 u x) \u00d7 (\u03b2 v y)\n\n_\u2297\u2092_ : (A B : Obj) \u2192 Obj\n(U , X , \u03b1) \u2297\u2092 (V , Y , \u03b2) = ((U \u00d7 V) , (X \u00d7 Y) , \u03b1 \u2297\u1d63 \u03b2)\n\nF\u2297 : \u2200{Z T V X U Y : Set}{F : U \u2192 Z \u2192 X}{G : V \u2192 T \u2192 Y} \u2192 (U \u00d7 V) \u2192 (Z \u00d7 T) \u2192 (X \u00d7 Y)\nF\u2297 {F = F}{G} (u , v) (z , t) = F u z , G v t\n \n_\u2297\u2090_ : {A B C D : Obj} \u2192 Hom A C \u2192 Hom B D \u2192 Hom (A \u2297\u2092 B) (C \u2297\u2092 D)\n_\u2297\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) = \u27e8 f , g \u27e9 , F\u2297 {F = F}{G} , p\u2297\n where\n p\u2297 : {u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Z (\u03bb x \u2192 T)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (F\u2297 {F = F}{G} u y) \u2192 (\u03b3 \u2297\u1d63 \u03b4) (\u27e8 f , g \u27e9 u) y\n p\u2297 {u , v}{z , t} (p\u2083 , p\u2084) = p\u2081 p\u2083 , p\u2082 p\u2084\n\n{-\n-- The unit for tensor:\n\u03b9 : \u22a4 \u2192 \u22a4 \u2192 Set\n\u03b9 triv triv = \u22a4\n\nI : Obj\nI = (\u22a4 , \u22a4 , \u03b9)\n\nJ : Obj\nJ = (\u22a4 , \u22a4 , (\u03bb x y \u2192 \u22a5))\n\n-- The left-unitor:\n\u03bb\u2297-p : \u2200{U X \u03b1}{u : \u22a4 \u00d7 U} {x : X} \u2192 (\u03b9 \u2297\u1d63 \u03b1) u ((\u03bb _ \u2192 triv) , (\u03bb _ \u2192 x)) \u2192 \u03b1 (snd u) x\n\u03bb\u2297-p {U}{X}{\u03b1}{(triv , u)}{x} (triv , p-\u03b1) = p-\u03b1\n \n\u03bb\u2297 : \u2200{A : Obj} \u2192 Hom (I \u2297\u2092 A) A\n\u03bb\u2297 {(U , X , \u03b1)} = snd , (\u03bb x \u2192 (\u03bb _ \u2192 triv) , (\u03bb _ \u2192 x)) , \u03bb\u2297-p\n\n\u03bb\u207b\u00b9\u2297 : \u2200{A : Obj} \u2192 Hom A (I \u2297\u2092 A)\n\u03bb\u207b\u00b9\u2297 {(U , X , \u03b1)} = (\u03bb u \u2192 triv , u) , ((\u03bb x \u2192 snd x triv) , \u03bb\u207b\u00b9\u2297-p) \n where\n \u03bb\u207b\u00b9\u2297-p : \u2200{U X \u03b1} \u2192 {u : U} {y : (U \u2192 \u22a4) \u00d7 (\u22a4 \u2192 X)} \u2192 \u03b1 u (snd y triv) \u2192 (\u03b9 \u2297\u1d63 \u03b1) (triv , u) y\n \u03bb\u207b\u00b9\u2297-p {U}{X}{\u03b1}{u}{(h\u2081 , h\u2082)} p-\u03b1 with h\u2081 u\n ... | triv = triv , p-\u03b1\n\n-- The right-unitor:\n\u03c1\u2297 : \u2200{A : Obj} \u2192 Hom (A \u2297\u2092 I) A\n\u03c1\u2297 {(U , X , \u03b1)} = fst , (\u03bb x \u2192 (\u03bb x\u2081 \u2192 x) , (\u03bb x\u2081 \u2192 triv)) , \u03c1\u2297-p\n where\n \u03c1\u2297-p : \u2200{U X \u03b1}{u : U \u00d7 \u22a4}{x : X} \u2192 (\u03b1 \u2297\u1d63 \u03b9) u ((\u03bb _ \u2192 x) , (\u03bb _ \u2192 triv)) \u2192 \u03b1 (fst u) x\n \u03c1\u2297-p {U}{X}{\u03b1}{(u , triv)}{x} (p-\u03b1 , triv) = p-\u03b1\n\n\u03c1\u2297-inv : \u2200{A : Obj} \u2192 Hom A (A \u2297\u2092 I)\n\u03c1\u2297-inv {(U , X , \u03b1)} = (\u03bb x \u2192 x , triv) , (\u03bb x \u2192 fst x triv) , \u03c1\u2297-p-inv\n where\n \u03c1\u2297-p-inv : \u2200{U X \u03b1}{u : U} {y : \u03a3 (\u22a4 \u2192 X) (\u03bb x \u2192 U \u2192 \u22a4)} \u2192 \u03b1 u (fst y triv) \u2192 (\u03b1 \u2297\u1d63 \u03b9) (u , triv) y\n \u03c1\u2297-p-inv {U}{X}{\u03b1}{u}{(f , g)} p-\u03b1 rewrite single-range {g = g}{u} = p-\u03b1 , triv\n\n-- Symmetry:\n\u03b2\u2297 : \u2200{A B : Obj} \u2192 Hom (A \u2297\u2092 B) (B \u2297\u2092 A)\n\u03b2\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)} = twist-\u00d7 , twist-\u00d7 , \u03b2\u2297-p\n where\n \u03b2\u2297-p : \u2200{U V Y X \u03b1 \u03b2} \u2192 {u : U \u00d7 V} {y : (U \u2192 Y) \u00d7 (V \u2192 X)} \u2192\n (\u03b1 \u2297\u1d63 \u03b2) u (twist-\u00d7 y) \u2192 (\u03b2 \u2297\u1d63 \u03b1) (twist-\u00d7 u) y\n \u03b2\u2297-p {U}{V}{Y}{X}{\u03b1}{\u03b2}{(u , v)}{(h\u2081 , h\u2082)} p-\u03b1 = twist-\u00d7 p-\u03b1\n\n-- The associator:\n\u03b1\u2297-inv : \u2200{A B C : Obj} \u2192 Hom (A \u2297\u2092 (B \u2297\u2092 C)) ((A \u2297\u2092 B) \u2297\u2092 C)\n\u03b1\u2297-inv {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = rl-assoc-\u00d7 , F\u03b1-inv , \u03b1-inv-cond\n where\n F\u03b1-inv : (W \u2192 (V \u2192 X) \u00d7 (U \u2192 Y)) \u00d7 (U \u00d7 V \u2192 Z) \u2192 (V \u00d7 W \u2192 X) \u00d7 (U \u2192 (W \u2192 Y) \u00d7 (V \u2192 Z))\n F\u03b1-inv = (\u03bb p \u2192 (\u03bb p' \u2192 fst ((fst p) (snd p')) (fst p')) , (\u03bb u \u2192 (\u03bb w \u2192 snd (fst p w) u) , (\u03bb v \u2192 (snd p) (u , v))))\n \u03b1-inv-cond : \u2200{u : U \u00d7 V \u00d7 W} {y : (W \u2192 (V \u2192 X) \u00d7 (U \u2192 Y)) \u00d7 (U \u00d7 V \u2192 Z)} \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) u (F\u03b1-inv y) \u2192 ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) (rl-assoc-\u00d7 u) y\n \u03b1-inv-cond {(u , v , w)} {(h\u2081 , h\u2082)} (p\u2081 , p\u2082 , p\u2083) with h\u2081 w\n ... | (a , b) = (p\u2081 , p\u2082) , p\u2083\n\nF\u03b1 : \u2200{V W X Y U V Z : Set} \u2192 \u03a3 (\u03a3 V (\u03bb x \u2192 W) \u2192 X) (\u03bb x \u2192 U \u2192 \u03a3 (W \u2192 Y) (\u03bb x\u2081 \u2192 V \u2192 Z))\n \u2192 \u03a3 (W \u2192 \u03a3 (V \u2192 X) (\u03bb x \u2192 U \u2192 Y)) (\u03bb x \u2192 \u03a3 U (\u03bb x\u2081 \u2192 V) \u2192 Z)\nF\u03b1 (f , g) = (\u03bb x \u2192 (\u03bb x\u2081 \u2192 f ((x\u2081 , x))) , (\u03bb x\u2081 \u2192 fst (g x\u2081) x)) , (\u03bb x \u2192 snd (g (fst x)) (snd x))\n\n\u03b1\u2297 : \u2200{A B C : Obj} \u2192 Hom ((A \u2297\u2092 B) \u2297\u2092 C) (A \u2297\u2092 (B \u2297\u2092 C)) \n\u03b1\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = (lr-assoc-\u00d7 , F\u03b1 {V} , \u03b1-cond)\n where\n \u03b1-cond : {u : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)}{y : \u03a3 (\u03a3 V (\u03bb x \u2192 W) \u2192 X) (\u03bb x \u2192 U \u2192 \u03a3 (W \u2192 Y) (\u03bb x\u2081 \u2192 V \u2192 Z))} \u2192\n ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) u (F\u03b1 {V} y) \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) (lr-assoc-\u00d7 u) y\n \u03b1-cond {(u , v) , w}{(f , g)} ((p\u2081 , p\u2082) , p\u2083) with g u\n ... | (h\u2081 , h\u2082) = p\u2081 , p\u2082 , p\u2083\n\n\u03b1\u2297-id\u2081 : \u2200{A B C} \u2192 (\u03b1\u2297 {A}{B}{C}) \u25cb \u03b1\u2297-inv \u2261h id\n\u03b1\u2297-id\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)} \u2192 rl-assoc-\u00d7 (lr-assoc-\u00d7 a) \u2261 a\n aux {(u , v) , w} = refl\n\n aux' : {a : \u03a3 (W \u2192 \u03a3 (V \u2192 X) (\u03bb x \u2192 U \u2192 Y)) (\u03bb x \u2192 \u03a3 U (\u03bb x\u2081 \u2192 V) \u2192 Z)}\n \u2192 ((\u03bb x \u2192 (\u03bb x\u2081 \u2192 fst (fst a x) x\u2081) , (\u03bb x\u2081 \u2192 snd (fst a x) x\u2081)) , (\u03bb x \u2192 snd a (fst x , snd x))) \u2261 a\n aux' {j\u2081 , j\u2082} = eq-\u00d7 (ext-set aux'') (ext-set aux''')\n where\n aux'' : {a : W} \u2192 (fst (j\u2081 a) , snd (j\u2081 a)) \u2261 j\u2081 a\n aux'' {w} with j\u2081 w\n ... | h\u2081 , h\u2082 = refl\n\n aux''' : {a : \u03a3 U (\u03bb x\u2081 \u2192 V)} \u2192 j\u2082 (fst a , snd a) \u2261 j\u2082 a\n aux''' {u , v} = refl\n\n\u03b1\u2297-id\u2082 : \u2200{A B C} \u2192 (\u03b1\u2297-inv {A}{B}{C}) \u25cb \u03b1\u2297 \u2261h id\n\u03b1\u2297-id\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))} \u2192 lr-assoc-\u00d7 (rl-assoc-\u00d7 a) \u2261 a\n aux {u , (v , w)} = refl\n aux' : {a\n : \u03a3 (\u03a3 V (\u03bb x \u2192 W) \u2192 X) (\u03bb x \u2192 U \u2192 \u03a3 (W \u2192 Y) (\u03bb x\u2081 \u2192 V \u2192 Z))} \u2192\n ((\u03bb p' \u2192 fst (fst (F\u03b1 {V} {W} {X} {Y} {U} {V} {Z} a) (snd p')) (fst p')) ,\n (\u03bb u \u2192 (\u03bb w \u2192 snd (fst (F\u03b1 {V} {W} {X} {Y} {U} {V} {Z} a) w) u) , (\u03bb v \u2192 snd (F\u03b1 {V} {W} {X} {Y} {U} {V} {Z} a) (u , v))))\n \u2261 a\n aux' {j\u2081 , j\u2082} = eq-\u00d7 (ext-set aux'') (ext-set aux''')\n where\n aux'' : {a : \u03a3 V (\u03bb x \u2192 W)} \u2192 j\u2081 (fst a , snd a) \u2261 j\u2081 a\n aux'' {v , w} = refl\n aux''' : {a : U} \u2192 ((\u03bb w \u2192 fst (j\u2082 a) w) , (\u03bb v \u2192 snd (j\u2082 a) v)) \u2261 j\u2082 a\n aux''' {u} with j\u2082 u\n ... | h\u2081 , h\u2082 = refl\n \n-- Internal hom:\n\u22b8-cond : \u2200{U V X Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 (U \u2192 V) \u00d7 (Y \u2192 X) \u2192 U \u00d7 Y \u2192 Set\n\u22b8-cond \u03b1 \u03b2 (f , g) (u , y) = \u03b1 u (g y) \u2192 \u03b2 (f u) y\n\n_\u22b8\u2092_ : Obj \u2192 Obj \u2192 Obj\n(U , X , \u03b1) \u22b8\u2092 (V , Y , \u03b2) = ((U \u2192 V) \u00d7 (Y \u2192 X)) , (U \u00d7 Y) , \u22b8-cond \u03b1 \u03b2\n\n_\u22b8\u2090_ : {A B C D : Obj} \u2192 Hom C A \u2192 Hom B D \u2192 Hom (A \u22b8\u2092 B) (C \u22b8\u2092 D)\n_\u22b8\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) =\n h , H , p\u2083\n where\n h : \u03a3 (U \u2192 V) (\u03bb x \u2192 Y \u2192 X) \u2192 \u03a3 (W \u2192 S) (\u03bb x \u2192 T \u2192 Z)\n h (h\u2081 , h\u2082) = (\u03bb w \u2192 g (h\u2081 (f w))) , (\u03bb t \u2192 F (h\u2082 (G t)))\n H : \u03a3 W (\u03bb x \u2192 T) \u2192 \u03a3 U (\u03bb x \u2192 Y)\n H (w , t) = f w , G t\n p\u2083 : {u : \u03a3 (U \u2192 V) (\u03bb x \u2192 Y \u2192 X)} {y : \u03a3 W (\u03bb x \u2192 T)} \u2192 \u22b8-cond \u03b1 \u03b2 u (H y) \u2192 \u22b8-cond \u03b3 \u03b4 (h u) y\n p\u2083 {h\u2081 , h\u2082}{w , t} c c' = p\u2082 (c (p\u2081 c'))\n\ncur : {A B C : Obj}\n \u2192 Hom (A \u2297\u2092 B) C\n \u2192 Hom A (B \u22b8\u2092 C)\ncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb u \u2192 (\u03bb v \u2192 f (u , v)) , (\u03bb z \u2192 snd (F z) u)) , (\u03bb p \u2192 fst (F (snd p)) (fst p)) , cur-cond\n where\n cur-cond : \u2200{u : U}{y : \u03a3 V (\u03bb x \u2192 Z)}\n \u2192 \u03b1 u (fst (F (snd y)) (fst y))\n \u2192 \u22b8-cond \u03b2 \u03b3 ((\u03bb v \u2192 f (u , v)) , (\u03bb z \u2192 snd (F z) u)) y\n cur-cond {u}{v , z} p\u2082 p\u2083 with p\u2081 {u , v}{z} \n ... | p\u2081' with F z\n ... | (j\u2081 , j\u2082) = p\u2081' (p\u2082 , p\u2083)\n\ncur-\u2261h : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-\u2261h {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}\n {f\u2081 , F\u2081 , p\u2081}{f\u2082 , F\u2082 , p\u2082} (p\u2083 , p\u2084)\n rewrite p\u2083 | p\u2084 = refl , refl\n\ncur-cong : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C} \u2192 f\u2081 \u2261h f\u2082 \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-cong {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)}{f\u2081 , F\u2081 , _}{f\u2082 , F\u2082 , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\nuncur : {A B C : Obj}\n \u2192 Hom A (B \u22b8\u2092 C)\n \u2192 Hom (A \u2297\u2092 B) C\nuncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb p \u2192 fst (f (fst p)) (snd p)) , (\u03bb z \u2192 (\u03bb v \u2192 F (v , z)) , (\u03bb u \u2192 snd (f u) z)) , uncur-cond\n where\n uncur-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : Z}\n \u2192 (\u03b1 \u2297\u1d63 \u03b2) u ((\u03bb v \u2192 F (v , y)) , (\u03bb u\u2081 \u2192 snd (f u\u2081) y))\n \u2192 \u03b3 (fst (f (fst u)) (snd u)) y\n uncur-cond {u , v}{z} (p\u2082 , p\u2083) with p\u2081 {u}{v , z} p\u2082\n ... | p\u2081' with f u\n ... | (j\u2081 , j\u2082) = p\u2081' p\u2083\n\ncur-uncur-bij\u2081 : \u2200{A B C}{f : Hom (A \u2297\u2092 B) C}\n \u2192 uncur (cur f) \u2261h f\ncur-uncur-bij\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{f , F , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : \u03a3 U (\u03bb x \u2192 V)} \u2192 f (fst a , snd a) \u2261 f a\n aux\u2081 {u , v} = refl\n \n aux\u2082 : {a : Z} \u2192 ((\u03bb v \u2192 fst (F a) v) , (\u03bb u \u2192 snd (F a) u)) \u2261 F a\n aux\u2082 {z} with F z\n ... | j\u2081 , j\u2082 = refl\n\ncur-uncur-bij\u2082 : \u2200{A B C}{g : Hom A (B \u22b8\u2092 C)}\n \u2192 cur (uncur g) \u2261h g\ncur-uncur-bij\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{g , G , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : U} \u2192 ((\u03bb v \u2192 fst (g a) v) , (\u03bb z \u2192 snd (g a) z)) \u2261 g a\n aux\u2081 {u} with g u\n ... | (j\u2081 , j\u2082) = refl\n\n aux\u2082 : {a : \u03a3 V (\u03bb x \u2192 Z)} \u2192 G (fst a , snd a) \u2261 G a\n aux\u2082 {v , z} = refl\n \n-- The of-course exponential:\n!\u2092-cond : \u2200{U X : Set}\n \u2192 (U \u2192 X \u2192 Set)\n \u2192 U\n \u2192 (U \u2192 X *)\n \u2192 Set\n!\u2092-cond \u03b1 u f = all-pred (\u03b1 u) (f u)\n \n!\u2092 : Obj \u2192 Obj\n!\u2092 (U , X , \u03b1) = U , (U \u2192 X *) , !\u2092-cond \u03b1\n\n!-cta : {V Y U X : Set}\n \u2192 (Y \u2192 X)\n \u2192 (U \u2192 V)\n \u2192 (V \u2192 Y *)\n \u2192 (U \u2192 X *)\n!-cta F f g = \u03bb u \u2192 list-funct F (g (f u))\n\n!\u2090-cond : \u2200{U V Y X : Set}{F : Y \u2192 X}{f : U \u2192 V}\n \u2192 (\u03b1 : U \u2192 X \u2192 Set)\n \u2192 (\u03b2 : V \u2192 Y \u2192 Set)\n \u2192 (p : {u : U} {y : Y} \u2192 \u03b1 u (F y) \u2192 \u03b2 (f u) y)\n {u : U}{l : Y *}\n \u2192 all-pred (\u03b1 u) (list-funct F l)\n \u2192 all-pred (\u03b2 (f u)) l\n!\u2090-cond _ _ _ {l = []} _ = triv\n!\u2090-cond \u03b1 \u03b2 p {u}{x :: xs} (p' , p'') = p p' , !\u2090-cond \u03b1 \u03b2 p p'' \n \n!\u2090 : {A B : Obj} \u2192 Hom A B \u2192 Hom (!\u2092 A) (!\u2092 B)\n!\u2090 {U , X , \u03b1}{V , Y , \u03b2} (f , F , p) = f , !-cta F f , !\u2090-cond \u03b1 \u03b2 p\n\n-- Of-course is a comonad:\n\u03b5 : \u2200{A} \u2192 Hom (!\u2092 A) A\n\u03b5 {U , X , \u03b1} = id-set , (\u03bb x y \u2192 [ x ]) , fst\n\n\u03b4-cta : {U X : Set} \u2192 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X)) \u2192 U \u2192 \ud835\udd43 X\n\u03b4-cta g u = foldr (\u03bb f rest \u2192 (f u) ++ rest) [] (g u)\n \n\u03b4 : \u2200{A} \u2192 Hom (!\u2092 A) (!\u2092 (!\u2092 A))\n\u03b4 {U , X , \u03b1} = id-set , \u03b4-cta , \u03b4-cond\n where\n \u03b4-cond : {u : U} {l : \ud835\udd43 (U \u2192 \ud835\udd43 X)}\n \u2192 all-pred (\u03b1 u) (foldr (\u03bb f \u2192 _++_ (f u)) [] l)\n \u2192 all-pred (\u03bb f\n \u2192 all-pred (\u03b1 u) (f u)) l\n \u03b4-cond {l = []} _ = triv\n \u03b4-cond {u}{l = x :: l'} p with\n all-pred-append {X}{\u03b1 u}\n {x u}\n {foldr (\u03bb f \u2192 _++_ (f u)) [] l'}\n \u2227-unit \u2227-assoc\n ... | p' rewrite p' = fst p , \u03b4-cond {u} {l'} (snd p)\n\n-- These diagrams can be found on page 22 of the report.\ncomonand-diag\u2081 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (!\u2090 (\u03b4 {A})) \u2261h (\u03b4 {A}) \u25cb (\u03b4 { !\u2092 A})\ncomonand-diag\u2081 {U , X , \u03b1} =\n refl , ext-set (\u03bb {a} \u2192 ext-set (\u03bb {a\u2081} \u2192 aux {a\u2081}{a a\u2081}))\n where\n aux : \u2200{a\u2081 : U}{l : \ud835\udd43 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X))} \u2192\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) []\n (map (\u03bb g u \u2192 foldr (\u03bb f \u2192 _++_ (f u)) [] (g u)) l)\n \u2261\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] (foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] l)\n aux {a}{[]} = refl \n aux {a}{x :: l} rewrite\n sym (foldr-append {l\u2081 = x a}{foldr (\u03bb f \u2192 _++_ (f a)) [] l}{a})\n = cong2 {a = foldr (\u03bb f \u2192 _++_ (f a)) [] (x a)}\n _++_\n refl\n (aux {a}{l})\n\ncomonand-diag\u2082 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (\u03b5 { !\u2092 A}) \u2261h (\u03b4 {A}) \u25cb (!\u2090 (\u03b5 {A}))\ncomonand-diag\u2082 {U , X , \u03b1} =\n refl , ext-set (\u03bb {f} \u2192 ext-set (\u03bb {a} \u2192 aux {a}{f a}))\n where\n aux : \u2200{a : U}{l : X *}\n \u2192 l ++ [] \u2261 foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x y \u2192 x :: []) l)\n aux {a}{[]} = refl\n aux {a}{x :: l} with aux {a}{l}\n ... | IH rewrite ++[] l =\n cong2 {a = x} {x} {l}\n {foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x\u2081 y \u2192 x\u2081 :: []) l)} _::_ refl\n IH\n \nmodule Cartesian where\n \u03c0\u2081 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (U , X , \u03b1))\n \u03c0\u2081 {U}{X}{V}{Y}{\u03b1}{\u03b2} = fst , (\u03bb f \u2192 (\u03bb v u \u2192 f u) , (\u03bb u v \u2192 [])) , \u03c0\u2081-cond\n where\n \u03c0\u2081-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : U \u2192 \ud835\udd43 X} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 y u\u2081) , (\u03bb u\u2081 v \u2192 [])) \u2192\n all-pred (\u03b1 (fst u)) (y (fst u))\n \u03c0\u2081-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2081\n\n \u03c0\u2082 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (V , Y , \u03b2))\n \u03c0\u2082 {U}{X}{V}{Y}{\u03b1}{\u03b2} = snd , (\u03bb f \u2192 (\u03bb v u \u2192 []) , (\u03bb u v \u2192 f v)) , \u03c0\u2082-cond\n where\n \u03c0\u2082-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : V \u2192 \ud835\udd43 Y} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 []) , (\u03bb u\u2081 v \u2192 y v)) \u2192\n all-pred (\u03b2 (snd u)) (y (snd u))\n \u03c0\u2082-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2082\n\n cart-ar-crt : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y) \u2192 W \u2192 \ud835\udd43 Z\n cart-ar-crt (f , F , p\u2081) (g , G , p\u2082) (j\u2081 , j\u2082) w = F (j\u2081 (g w)) w ++ G (j\u2082 (f w)) w\n\n cart-ar : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))\n cart-ar {U}{X}{V}{Y}{W}{Z}{\u03b1}{\u03b2}{\u03b3} (f , F , p\u2081) (g , G , p\u2082)\n = (\u03bb w \u2192 f w , g w) , cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) , cart-ar-cond\n where\n cart-ar-cond : \u2200{u : W} {y : \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y)} \u2192\n all-pred (\u03b3 u) (cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) y u) \u2192\n ((\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b1 u\u2081) (f\u2081 u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b2 u\u2081) (f\u2081 u\u2081)))\n (f u , g u) y\n cart-ar-cond {w}{j\u2081 , j\u2082} p\n rewrite\n all-pred-append {f = \u03b3 w}{F (j\u2081 (g w)) w}{G (j\u2082 (f w)) w} \u2227-unit \u2227-assoc with p\n ... | (a , b) = p\u2081 a , p\u2082 b\n\n cart-diag\u2081 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (U , X , \u03b1)}\n (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (U , X , \u03b1)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2081)\n cart-diag\u2081 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 sym (++[] (map F (j (f w))))))\n\n cart-diag\u2082 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (V , Y , \u03b2)}\n (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (V , Y , \u03b2)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2082)\n cart-diag\u2082 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 refl))\n-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"1ae2c016233ad8fa076a9b194e880387b889b8b0","subject":"Searchable.Sum: add bit","message":"Searchable.Sum: add bit\n","repos":"crypto-agda\/crypto-agda","old_file":"Search\/Searchable\/Sum.agda","new_file":"Search\/Searchable\/Sum.agda","new_contents":"open import Function.NP\nopen import Data.Nat using (_+_)\nimport Level as L\nimport Function.Inverse.NP as FI\nimport Function.Related as FR\nopen FI using (_\u2194_; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nopen import Function.Related.TypeIsomorphisms.NP\nopen import Data.Product.NP\nopen import Data.Sum\nopen import Data.Bits\nopen import Data.Fin using (Fin)\nopen import Search.Type\nopen import Search.Searchable\n\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_ ; module \u2261-Reasoning; cong)\n\nmodule Search.Searchable.Sum where\n\ninfixr 4 _\u228e-search_\n\n_\u228e-search_ : \u2200 {m A B} \u2192 Search m A \u2192 Search m B \u2192 Search m (A \u228e B)\n(search\u1d2c \u228e-search search\u1d2e) _\u2219_ f = search\u1d2c _\u2219_ (f \u2218 inj\u2081) \u2219 search\u1d2e _\u2219_ (f \u2218 inj\u2082)\n\n_\u228e-search-ind_ : \u2200 {m p A B} {s\u1d2c : Search m A} {s\u1d2e : Search m B}\n \u2192 SearchInd p s\u1d2c \u2192 SearchInd p s\u1d2e \u2192 SearchInd p (s\u1d2c \u228e-search s\u1d2e)\n(Ps\u1d2c \u228e-search-ind Ps\u1d2e) P P\u2219 Pf\n = P\u2219 (Ps\u1d2c (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2081))) P\u2219 (Pf \u2218 inj\u2081))\n (Ps\u1d2e (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2082))) P\u2219 (Pf \u2218 inj\u2082))\n\ninfixr 4 _\u228e-sum_\n\n_\u228e-sum_ : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u228e B)\n(sum\u1d2c \u228e-sum sum\u1d2e) f = sum\u1d2c (f \u2218 inj\u2081) + sum\u1d2e (f \u2218 inj\u2082)\n\nmodule _ {A B} {sum\u1d2c : Sum A} {sum\u1d2e : Sum B} where\n\n sum\u1d2c\u1d2e = sum\u1d2c \u228e-sum sum\u1d2e\n\n _\u228e-adequate-sum_ : AdequateSum sum\u1d2c \u2192 AdequateSum sum\u1d2e \u2192 AdequateSum sum\u1d2c\u1d2e\n (asum\u1d2c \u228e-adequate-sum asum\u1d2e) f = (Fin (sum\u1d2c (f \u2218 inj\u2081) + sum\u1d2e (f \u2218 inj\u2082)))\n \u2194\u27e8 FI.sym (Fin-\u228e-+ _ _) \u27e9\n (Fin (sum\u1d2c (f \u2218 inj\u2081)) \u228e Fin (sum\u1d2e (f \u2218 inj\u2082)))\n \u2194\u27e8 asum\u1d2c (f \u2218 inj\u2081) \u228e-cong asum\u1d2e (f \u2218 inj\u2082) \u27e9\n (\u03a3 A (Fin \u2218 f \u2218 inj\u2081) \u228e \u03a3 B (Fin \u2218 f \u2218 inj\u2082))\n \u2194\u27e8 FI.sym \u03a3\u228e-distrib \u27e9\n \u03a3 (A \u228e B) (Fin \u2218 f)\n \u220e\n where open FR.EquationalReasoning\n\n_\u228e-\u03bc_ : \u2200 {A B} \u2192 Searchable A \u2192 Searchable B \u2192 Searchable (A \u228e B)\n\u03bcA \u228e-\u03bc \u03bcB = mk _ (search-ind \u03bcA \u228e-search-ind search-ind \u03bcB)\n (adequate-sum \u03bcA \u228e-adequate-sum adequate-sum \u03bcB)\n\nmodule _ {A B} {s\u1d2c : Search\u2081 A} {s\u1d2e : Search\u2081 B} where\n s\u1d2c\u207a\u1d2e = s\u1d2c \u228e-search s\u1d2e\n _\u228e-focus_ : Focus s\u1d2c \u2192 Focus s\u1d2e \u2192 Focus s\u1d2c\u207a\u1d2e\n (f\u1d2c \u228e-focus f\u1d2e) (inj\u2081 x , y) = inj\u2081 (f\u1d2c (x , y))\n (f\u1d2c \u228e-focus f\u1d2e) (inj\u2082 x , y) = inj\u2082 (f\u1d2e (x , y))\n\n _\u228e-unfocus_ : Unfocus s\u1d2c \u2192 Unfocus s\u1d2e \u2192 Unfocus s\u1d2c\u207a\u1d2e\n _\u228e-unfocus_ f\u1d2c f\u1d2e (inj\u2081 x) = first inj\u2081 (f\u1d2c x)\n _\u228e-unfocus_ f\u1d2c f\u1d2e (inj\u2082 y) = first inj\u2082 (f\u1d2e y)\n\n {-\n _\u228e-focused_ : Focused s\u1d2c \u2192 Focused s\u1d2e \u2192 Focused {L.zero} s\u1d2c\u207a\u1d2e\n _\u228e-focused_ f\u1d2c f\u1d2e {B} = inverses (to f\u1d2c \u228e-focus to f\u1d2e) (from f\u1d2c \u228e-unfocus from f\u1d2e) (\u21d2) (\u21d0)\n where\n \u21d2 : (x : \u03a3 (A \u228e {!!}) {!!}) \u2192 _\n \u21d2 (x , y) = {!!}\n \u21d0 : (x : s\u1d2c _\u228e_ (B \u2218 inj\u2081) \u228e s\u1d2e _\u228e_ (B \u2218 inj\u2082)) \u2192 _\n \u21d0 (inj\u2081 x) = cong inj\u2081 {!!}\n \u21d0 (inj\u2082 x) = cong inj\u2082 {!!}\n -}\n\n _\u228e-lookup_ : Lookup s\u1d2c \u2192 Lookup s\u1d2e \u2192 Lookup (s\u1d2c \u228e-search s\u1d2e)\n (lookup\u1d2c \u228e-lookup lookup\u1d2e) (x , y) = [ lookup\u1d2c x , lookup\u1d2e y ]\n\n _\u228e-reify_ : Reify s\u1d2c \u2192 Reify s\u1d2e \u2192 Reify (s\u1d2c \u228e-search s\u1d2e)\n (reify\u1d2c \u228e-reify reify\u1d2e) f = (reify\u1d2c (f \u2218 inj\u2081)) , (reify\u1d2e (f \u2218 inj\u2082))\n\nsearchBit : \u2200 {m} \u2192 Search m Bit\nsearchBit _\u2219_ f = f 0b \u2219 f 1b\n\nsearchBit-ind : \u2200 {m p} \u2192 SearchInd p {m} searchBit\nsearchBit-ind _ _P\u2219_ Pf = Pf 0b P\u2219 Pf 1b\n\n\u03bcBit : Searchable Bit\n\u03bcBit = \u03bc-iso (FI.sym Bit\u2194\u22a4\u228e\u22a4) (\u03bc\u22a4 \u228e-\u03bc \u03bc\u22a4)\n\nfocusBit : \u2200 {a} \u2192 Focus {a} searchBit\nfocusBit (false , x) = inj\u2081 x\nfocusBit (true , x) = inj\u2082 x\n\nfocusedBit : Focused {L.zero} searchBit\nfocusedBit {B} = inverses focusBit unfocus (\u21d2) (\u21d0)\n where open Searchable\u2081\u2081 searchBit-ind\n \u21d2 : (x : \u03a3 Bit B) \u2192 _\n \u21d2 (false , x) = \u2261.refl\n \u21d2 (true , x) = \u2261.refl\n \u21d0 : (x : B 0b \u228e B 1b) \u2192 _\n \u21d0 (inj\u2081 x) = \u2261.refl\n \u21d0 (inj\u2082 x) = \u2261.refl\n\nlookupBit : \u2200 {a} \u2192 Lookup {a} searchBit\nlookupBit = proj\n -- -}\n -- -}\n -- -}\n","old_contents":"open import Function.NP\nopen import Data.Nat using (_+_)\nimport Level as L\nimport Function.Inverse.NP as FI\nimport Function.Related as FR\nopen FI using (_\u2194_; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nopen import Function.Related.TypeIsomorphisms.NP\nopen import Data.Product.NP\nopen import Data.Sum\nopen import Data.Fin using (Fin)\nopen import Search.Type\nopen import Search.Searchable\n\nopen import Relation.Binary.Sum\nopen import Relation.Binary.PropositionalEquality.NP using (_\u2261_ ; module \u2261-Reasoning; cong)\n\nmodule Search.Searchable.Sum where\n\ninfixr 4 _\u228e-search_\n\n_\u228e-search_ : \u2200 {m A B} \u2192 Search m A \u2192 Search m B \u2192 Search m (A \u228e B)\n(search\u1d2c \u228e-search search\u1d2e) _\u2219_ f = search\u1d2c _\u2219_ (f \u2218 inj\u2081) \u2219 search\u1d2e _\u2219_ (f \u2218 inj\u2082)\n\n_\u228e-search-ind_ : \u2200 {m p A B} {s\u1d2c : Search m A} {s\u1d2e : Search m B}\n \u2192 SearchInd p s\u1d2c \u2192 SearchInd p s\u1d2e \u2192 SearchInd p (s\u1d2c \u228e-search s\u1d2e)\n(Ps\u1d2c \u228e-search-ind Ps\u1d2e) P P\u2219 Pf\n = P\u2219 (Ps\u1d2c (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2081))) P\u2219 (Pf \u2218 inj\u2081))\n (Ps\u1d2e (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2082))) P\u2219 (Pf \u2218 inj\u2082))\n\ninfixr 4 _\u228e-sum_\n\n_\u228e-sum_ : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u228e B)\n(sum\u1d2c \u228e-sum sum\u1d2e) f = sum\u1d2c (f \u2218 inj\u2081) + sum\u1d2e (f \u2218 inj\u2082)\n\nmodule _ {A B} {sum\u1d2c : Sum A} {sum\u1d2e : Sum B} where\n\n sum\u1d2c\u1d2e = sum\u1d2c \u228e-sum sum\u1d2e\n\n _\u228e-adequate-sum_ : AdequateSum sum\u1d2c \u2192 AdequateSum sum\u1d2e \u2192 AdequateSum sum\u1d2c\u1d2e\n (asum\u1d2c \u228e-adequate-sum asum\u1d2e) f = (Fin (sum\u1d2c (f \u2218 inj\u2081) + sum\u1d2e (f \u2218 inj\u2082)))\n \u2194\u27e8 FI.sym (Fin-\u228e-+ _ _) \u27e9\n (Fin (sum\u1d2c (f \u2218 inj\u2081)) \u228e Fin (sum\u1d2e (f \u2218 inj\u2082)))\n \u2194\u27e8 asum\u1d2c (f \u2218 inj\u2081) \u228e-cong asum\u1d2e (f \u2218 inj\u2082) \u27e9\n (\u03a3 A (Fin \u2218 f \u2218 inj\u2081) \u228e \u03a3 B (Fin \u2218 f \u2218 inj\u2082))\n \u2194\u27e8 FI.sym \u03a3\u228e-distrib \u27e9\n \u03a3 (A \u228e B) (Fin \u2218 f)\n \u220e\n where open FR.EquationalReasoning\n\n_\u228e-\u03bc_ : \u2200 {A B} \u2192 Searchable A \u2192 Searchable B \u2192 Searchable (A \u228e B)\n\u03bcA \u228e-\u03bc \u03bcB = mk _ (search-ind \u03bcA \u228e-search-ind search-ind \u03bcB)\n (adequate-sum \u03bcA \u228e-adequate-sum adequate-sum \u03bcB)\n\nmodule _ {A B} {s\u1d2c : Search\u2081 A} {s\u1d2e : Search\u2081 B} where\n s\u1d2c\u207a\u1d2e = s\u1d2c \u228e-search s\u1d2e\n _\u228e-focus_ : Focus s\u1d2c \u2192 Focus s\u1d2e \u2192 Focus s\u1d2c\u207a\u1d2e\n (f\u1d2c \u228e-focus f\u1d2e) (inj\u2081 x , y) = inj\u2081 (f\u1d2c (x , y))\n (f\u1d2c \u228e-focus f\u1d2e) (inj\u2082 x , y) = inj\u2082 (f\u1d2e (x , y))\n\n _\u228e-unfocus_ : Unfocus s\u1d2c \u2192 Unfocus s\u1d2e \u2192 Unfocus s\u1d2c\u207a\u1d2e\n _\u228e-unfocus_ f\u1d2c f\u1d2e (inj\u2081 x) = first inj\u2081 (f\u1d2c x)\n _\u228e-unfocus_ f\u1d2c f\u1d2e (inj\u2082 y) = first inj\u2082 (f\u1d2e y)\n\n {-\n _\u228e-focused_ : Focused s\u1d2c \u2192 Focused s\u1d2e \u2192 Focused {L.zero} s\u1d2c\u207a\u1d2e\n _\u228e-focused_ f\u1d2c f\u1d2e {B} = inverses (to f\u1d2c \u228e-focus to f\u1d2e) (from f\u1d2c \u228e-unfocus from f\u1d2e) (\u21d2) (\u21d0)\n where\n \u21d2 : (x : \u03a3 (A \u228e {!!}) {!!}) \u2192 _\n \u21d2 (x , y) = {!!}\n \u21d0 : (x : s\u1d2c _\u228e_ (B \u2218 inj\u2081) \u228e s\u1d2e _\u228e_ (B \u2218 inj\u2082)) \u2192 _\n \u21d0 (inj\u2081 x) = cong inj\u2081 {!!}\n \u21d0 (inj\u2082 x) = cong inj\u2082 {!!}\n -}\n\n _\u228e-lookup_ : Lookup s\u1d2c \u2192 Lookup s\u1d2e \u2192 Lookup (s\u1d2c \u228e-search s\u1d2e)\n (lookup\u1d2c \u228e-lookup lookup\u1d2e) (x , y) = [ lookup\u1d2c x , lookup\u1d2e y ]\n\n _\u228e-reify_ : Reify s\u1d2c \u2192 Reify s\u1d2e \u2192 Reify (s\u1d2c \u228e-search s\u1d2e)\n (reify\u1d2c \u228e-reify reify\u1d2e) f = (reify\u1d2c (f \u2218 inj\u2081)) , (reify\u1d2e (f \u2218 inj\u2082))\n\n -- -}\n -- -}\n -- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"d3c86de1ab251011a4ee5b6de0be901bedf0c9fe","subject":"Type Isos","message":"Type Isos\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Function\/Related\/TypeIsomorphisms\/NP.agda","new_file":"lib\/Function\/Related\/TypeIsomorphisms\/NP.agda","new_contents":"module Function.Related.TypeIsomorphisms.NP where\n\nimport Level as L\nopen import Algebra\nimport Algebra.FunctionProperties as FP\nopen L using (Lift; lower; lift)\nopen import Type hiding (\u2605)\n\nopen import Data.Fin using (Fin; zero; suc; pred)\nopen import Data.Vec using (Vec; []; _\u2237_)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_) renaming (_^_ to _**_)\nopen import Data.Maybe.NP\nopen import Data.Product.NP renaming (map to map\u00d7)\n--open import Data.Product.N-ary\nopen import Data.Sum renaming (map to map\u228e)\nopen import Data.Unit\nopen import Data.Empty\nopen import Data.Bits using (Bit; 0b; 1b; false; true; proj)\n\nimport Function.NP as F\nopen F using (\u03a0)\nimport Function.Equality as FE\nopen FE using (_\u27e8$\u27e9_)\nopen import Function.Related as FR\nopen import Function.Related.TypeIsomorphisms public\nimport Function.Inverse.NP as Inv\nopen Inv using (_\u2194_; _\u2218_; sym; id; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nopen import Relation.Binary\nopen import Relation.Binary.Product.Pointwise\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_ ; _\u2262_)\n\nMaybe-injective : \u2200 {A B : Set} \u2192 Maybe A \u2194 Maybe B \u2192 A \u2194 B\nMaybe-injective f = Iso.iso (g f) (g-empty f)\n module Maybe-injective where\n open Inverse using (injective; left-inverse-of; right-inverse-of)\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (tof-tot : \u2200 x \u2192 to f (just x) \u2262 nothing)\n (fof-tot : \u2200 x \u2192 from f (just x) \u2262 nothing) where\n\n CancelMaybe' : A \u2194 B\n CancelMaybe' = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n\n \u21d2 : _ \u2192 _\n \u21d2 x with to f (just x) | tof-tot x\n \u21d2 x | just y | _ = y\n \u21d2 x | nothing | p = \u22a5-elim (p \u2261.refl)\n\n \u21d0 : _ \u2192 _\n \u21d0 x with from f (just x) | fof-tot x\n \u21d0 x | just y | p = y\n \u21d0 x | nothing | p = \u22a5-elim (p \u2261.refl)\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 x with to f (just x)\n | tof-tot x\n | from f (just (\u21d2 x))\n | fof-tot (\u21d2 x)\n | left-inverse-of f (just x)\n | right-inverse-of f (just (\u21d2 x))\n \u21d0\u21d2 x | just x\u2081 | p | just x\u2082 | q | b | c = just-injective (\u2261.trans (\u2261.sym (left-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong (from f) c) b))\n \u21d0\u21d2 x | just x\u2081 | p | nothing | q | _ | _ = \u22a5-elim (q \u2261.refl)\n \u21d0\u21d2 x | nothing | p | z | q | _ | _ = \u22a5-elim (p \u2261.refl)\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 x with from f (just x)\n | fof-tot x\n | to f (just (\u21d0 x))\n | tof-tot (\u21d0 x)\n | right-inverse-of f (just x)\n | left-inverse-of f (just (\u21d0 x))\n \u21d2\u21d0 x | just x\u2081 | p | just x\u2082 | q | b | c = just-injective (\u2261.trans (\u2261.sym (right-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong (to f) c) b))\n \u21d2\u21d0 x | just x\u2081 | p | nothing | q | _ | _ = \u22a5-elim (q \u2261.refl)\n \u21d2\u21d0 x | nothing | p | z | q | _ | _ = \u22a5-elim (p \u2261.refl)\n\n module Iso {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (f-empty : to f nothing \u2261 nothing) where\n\n tof-tot : \u2200 x \u2192 to f (just x) \u2262 nothing\n tof-tot x eq with injective f (\u2261.trans eq (\u2261.sym f-empty))\n ... | ()\n\n f-empty' : from f nothing \u2261 nothing\n f-empty' = \u2261.trans (\u2261.sym (\u2261.cong (from f) f-empty)) (left-inverse-of f nothing)\n\n fof-tot : \u2200 x \u2192 from f (just x) \u2262 nothing\n fof-tot x eq with injective (sym f) (\u2261.trans eq (\u2261.sym f-empty'))\n ... | ()\n\n iso : A \u2194 B\n iso = CancelMaybe' f tof-tot fof-tot\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B) where\n\n g : Maybe A \u2194 Maybe B\n g = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n\n \u21d2 : Maybe A \u2192 Maybe B\n \u21d2 (just x) with to f (just x)\n ... | nothing = to f nothing\n ... | just y = just y\n \u21d2 nothing = nothing\n\n \u21d0 : Maybe B \u2192 Maybe A\n \u21d0 (just x) with from f (just x)\n ... | nothing = from f nothing\n ... | just y = just y\n \u21d0 nothing = nothing\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (just x) with to f (just x) | left-inverse-of f (just x)\n \u21d0\u21d2 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d0\u21d2 (just x) | nothing | p with to f nothing | left-inverse-of f nothing\n \u21d0\u21d2 (just x) | nothing | p | just _ | q rewrite q = p\n \u21d0\u21d2 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d0\u21d2 nothing = \u2261.refl\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (just x) with from f (just x) | right-inverse-of f (just x)\n \u21d2\u21d0 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d2\u21d0 (just x) | nothing | p with from f nothing | right-inverse-of f nothing\n \u21d2\u21d0 (just x) | nothing | p | just _ | q rewrite q = p\n \u21d2\u21d0 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d2\u21d0 nothing = \u2261.refl\n\n g-empty : to g nothing \u2261 nothing\n g-empty = \u2261.refl\n\nprivate\n Setoid\u2080 : \u2605 _\n Setoid\u2080 = Setoid L.zero L.zero\n\n\u03a3\u2261\u2194\u22a4 : \u2200 {a} {A : \u2605 a} x \u2192 \u03a3 A (_\u2261_ x) \u2194 \u22a4\n\u03a3\u2261\u2194\u22a4 x = inverses (F.const _) (\u03bb _ \u2192 _ , \u2261.refl)\n helper (\u03bb _ \u2192 \u2261.refl)\n where helper : (y : \u03a3 _ (_\u2261_ x)) \u2192 (x , \u2261.refl) \u2261 y\n helper (.x , \u2261.refl) = \u2261.refl\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : \u03a3 A B \u2192 \u2605 c} where\n curried : \u03a0 (\u03a3 A B) C \u2194 \u03a0 A \u03bb a \u2192 \u03a0 (B a) \u03bb b \u2192 C (a , b)\n curried = inverses curry uncurry (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u2192 \u2605 c} (f : A \u2194 B) where\n private\n left-f = Inverse.left-inverse-of f\n right-f = Inverse.right-inverse-of f\n coe : \u2200 x \u2192 C x \u2192 C (from f (to f x))\n coe x = \u2261.subst C (\u2261.sym (left-f x))\n \u21d2 : \u03a3 A C \u2192 \u03a3 B (C F.\u2218 from f)\n \u21d2 (x , p) = to f x , coe x p\n \u21d0 : \u03a3 B (C F.\u2218 from f) \u2192 \u03a3 A C\n \u21d0 = first (from f)\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , p) rewrite left-f x = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 p = mk\u03a3\u2261 (C F.\u2218 from f) (right-f (proj\u2081 p)) (helper p)\n where\n helper : \u2200 p \u2192 \u2261.subst (C F.\u2218 from f) (right-f (proj\u2081 p)) (coe (proj\u2081 (\u21d0 p)) (proj\u2082 (\u21d0 p))) \u2261 proj\u2082 p\n helper p with to f (from f (proj\u2081 p)) | right-f (proj\u2081 p) | left-f (from f (proj\u2081 p))\n helper _ | ._ | \u2261.refl | \u2261.refl = \u2261.refl\n first-iso : \u03a3 A C \u2194 \u03a3 B (C F.\u2218 from f)\n first-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : B \u2192 \u2605 c} (f : A \u2194 B) where\n sym-first-iso : \u03a3 A (C F.\u2218 to f) \u2194 \u03a3 B C\n sym-first-iso = sym (first-iso (sym f))\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} (f : \u2200 x \u2192 B x \u2194 C x) where\n private\n left-f = Inverse.left-inverse-of F.\u2218 f\n right-f = Inverse.right-inverse-of F.\u2218 f\n \u21d2 : \u03a3 A B \u2192 \u03a3 A C\n \u21d2 = second (to (f _))\n \u21d0 : \u03a3 A C \u2192 \u03a3 A B\n \u21d0 = second (from (f _))\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , y) rewrite left-f x y = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (x , y) rewrite right-f x y = \u2261.refl\n second-iso : \u03a3 A B \u2194 \u03a3 A C\n second-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a3 (A \u228e B) C\n T = \u03a3 A (C F.\u2218 inj\u2081) \u228e \u03a3 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 (inj\u2081 x , y) = inj\u2081 (x , y)\n \u21d2 (inj\u2082 x , y) = inj\u2082 (x , y)\n \u21d0 : T \u2192 S\n \u21d0 (inj\u2081 (x , y)) = inj\u2081 x , y\n \u21d0 (inj\u2082 (x , y)) = inj\u2082 x , y\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (inj\u2081 _ , _) = \u2261.refl\n \u21d0\u21d2 (inj\u2082 _ , _) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n \u03a3\u228e-distrib : S \u2194 T\n \u03a3\u228e-distrib = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\n{- requires extensional equality\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a0 (A \u228e B) C\n T = \u03a0 A (C F.\u2218 inj\u2081) \u00d7 \u03a0 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 f = f F.\u2218 inj\u2081 , f F.\u2218 inj\u2082\n \u21d0 : T \u2192 S\n \u21d0 (f , g) = [ f , g ]\n \u21d0\u21d2 : \u2200 f x \u2192 \u21d0 (\u21d2 f) x \u2261 f x\n \u21d0\u21d2 f (inj\u2081 x) = \u2261.refl\n \u21d0\u21d2 f (inj\u2082 y) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (f , g) = \u2261.refl\n\n \u03a0\u00d7-distrib : S \u2194 T\n \u03a0\u00d7-distrib = inverses (\u21d2) (\u21d0) {!\u21d0\u21d2!} \u21d2\u21d0\n-}\n\n\u228e-ICommutativeMonoid : CommutativeMonoid _ _\n\u228e-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u228e-setoid_;\n \u03b5 = \u2261.setoid \u22a5;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u228e-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \u22a5) _\u228e-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = [ (\u03bb ()) , F.id ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = inj\u2082\n ; cong = \u2082\u223c\u2082\n }\n ; inverse-of = record\n { left-inverse-of = [ (\u03bb ()) , (\u03bb x \u2192 \u2082\u223c\u2082 refl) ]\n ; right-inverse-of = \u03bb x \u2192 refl\n }\n } where open Setoid A\n cong-to : Setoid._\u2248_ (\u2261.setoid \u22a5 \u228e-setoid A) =[ _ ]\u21d2 _\u2248_\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y) rewrite x\u223c\u2081y = refl\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = x\u223c\u2082y\n\n assoc : Associative _\u228e-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = [ [ inj\u2081 , inj\u2082 F.\u2218 inj\u2081 ] , inj\u2082 F.\u2218 inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = [ inj\u2081 F.\u2218 inj\u2081 , [ inj\u2081 F.\u2218 inj\u2082 , inj\u2082 ] ]\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = [ [ (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2081\u223c\u2081 (refl A))) , (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2082\u223c\u2082 (refl B)) ) ] , (\u03bb _ \u2192 \u2082\u223c\u2082 (refl C)) ]\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (refl A)) , [ (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2081\u223c\u2081 (refl B))) , (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2082\u223c\u2082 (refl C))) ] ]\n }\n } where\n open Setoid\n cong-to : _\u2248_ ((A \u228e-setoid B) \u228e-setoid C) =[ _ ]\u21d2 _\u2248_ (A \u228e-setoid (B \u228e-setoid C))\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2082 ()))\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 x\u223c\u2081y\n cong-to (\u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2082y)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = \u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)\n\n cong-from : _\u2248_ (A \u228e-setoid (B \u228e-setoid C)) =[ _ ]\u21d2 _\u2248_ ((A \u228e-setoid B) \u228e-setoid C)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 x\u223c\u2081y) = \u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2082 ()))\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 x\u223c\u2082y\n\n comm : Commutative _\u228e-setoid_\n comm A B = record\n { to = swap'\n ; from = swap'\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u228e-setoid B FE.\u27f6 B \u228e-setoid A\n swap' {A} {B} = record\n { _\u27e8$\u27e9_ = [ inj\u2082 , inj\u2081 ]\n ; cong = cong\n } where\n cong : Setoid._\u2248_ (A \u228e-setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (B \u228e-setoid A)\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2082\u223c\u2082 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2081\u223c\u2081 x\u223c\u2082y\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u228e-setoid B) (swap' FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (Setoid.refl A)) , (\u03bb _ \u2192 \u2082\u223c\u2082 (Setoid.refl B)) ]\n\n\u00d7-ICommutativeMonoid : CommutativeMonoid _ _\n\u00d7-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u00d7-setoid_;\n \u03b5 = \u2261.setoid \u22a4;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u00d7-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \u22a4) _\u00d7-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2082\n ; cong = proj\u2082\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 _ , x\n ; cong = \u03bb x \u2192 \u2261.refl , x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u2261.refl , (Setoid.refl A)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl A\n }\n }\n\n assoc : Associative _\u00d7-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n ; cong = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n }\n ; from = record\n { _\u27e8$\u27e9_ = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n ; cong = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb _ \u2192 Setoid.refl ((A \u00d7-setoid B) \u00d7-setoid C)\n ; right-inverse-of = \u03bb _ \u2192 Setoid.refl (A \u00d7-setoid (B \u00d7-setoid C))\n }\n }\n\n comm : Commutative _\u00d7-setoid_\n comm A B = record\n { to = swap' {A} {B}\n ; from = swap' {B} {A}\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u00d7-setoid B FE.\u27f6 B \u00d7-setoid A\n swap' {A}{B} = record { _\u27e8$\u27e9_ = swap; cong = swap }\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u00d7-setoid B) (swap' {B} {A} FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = \u03bb x \u2192 Setoid.refl (A \u00d7-setoid B)\n\n\u00d7\u228e-ICommutativeSemiring : CommutativeSemiring _ _\n\u00d7\u228e-ICommutativeSemiring = record\n { _\u2248_ = Inv.Inverse\n ; _+_ = _\u228e-setoid_\n ; _*_ = _\u00d7-setoid_\n ; 0# = \u2261.setoid \u22a5\n ; 1# = \u2261.setoid \u22a4\n ; isCommutativeSemiring = record\n { +-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u228e-ICommutativeMonoid\n ; *-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u00d7-ICommutativeMonoid\n ; distrib\u02b3 = distrib\u02b3\n ; zero\u02e1 = zero\u02e1\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n distrib\u02b3 : _\u00d7-setoid_ DistributesOver\u02b3 _\u228e-setoid_\n distrib\u02b3 A B C = record\n { to = record\n { _\u27e8$\u27e9_ = uncurry [ curry inj\u2081 , curry inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = frm\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = uncurry [ (\u03bb x y \u2192 (\u2081\u223c\u2081 (refl B)) , (refl A)) , (\u03bb x y \u2192 (\u2082\u223c\u2082 (refl C)) , (refl A)) ]\n ; right-inverse-of = [ uncurry (\u03bb x y \u2192 \u2081\u223c\u2081 ((refl B {x}) , (refl A {y})))\n , uncurry (\u03bb x y \u2192 \u2082\u223c\u2082 ((refl C {x}) , (refl A {y}))) ]\n }\n } where\n open Setoid\n A' = Carrier A\n B' = Carrier B\n C' = Carrier C\n\n cong-to : _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A) =[ _ ]\u21d2 _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A))\n cong-to (\u2081\u223c\u2082 () , _)\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y , A-rel) = \u2081\u223c\u2081 (x\u223c\u2081y , A-rel)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y , A-rel) = \u2082\u223c\u2082 (x\u223c\u2082y , A-rel)\n\n frm : B' \u00d7 A' \u228e C' \u00d7 A' \u2192 (B' \u228e C') \u00d7 A'\n frm = [ map\u00d7 inj\u2081 F.id , map\u00d7 inj\u2082 F.id ]\n\n cong-from : _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A)) =[ _ ]\u21d2 _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 (B-rel , A-rel)) = \u2081\u223c\u2081 B-rel , A-rel\n cong-from (\u2082\u223c\u2082 (C-rel , A-rel)) = \u2082\u223c\u2082 C-rel , A-rel\n\n zero\u02e1 : LeftZero (\u2261.setoid \u22a5) _\u00d7-setoid_\n zero\u02e1 A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2081\n ; cong = proj\u2081\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u22a5-elim\n ; cong = \u03bb { {()} x }\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u22a5-elim (proj\u2081 x)\n ; right-inverse-of = \u03bb x \u2192 \u22a5-elim x\n }\n }\n\nmodule \u00d7-CMon {a} = CommutativeMonoid (\u00d7-CommutativeMonoid FR.bijection a)\nmodule \u228e-CMon {a} = CommutativeMonoid (\u228e-CommutativeMonoid FR.bijection a)\nmodule \u00d7\u228e\u00b0 {a} = CommutativeSemiring (\u00d7\u228e-CommutativeSemiring FR.bijection a)\n\nmodule \u00d7-ICMon = CommutativeMonoid \u00d7-ICommutativeMonoid\nmodule \u228e-ICMon = CommutativeMonoid \u228e-ICommutativeMonoid\nmodule \u00d7\u228e\u00b0I = CommutativeSemiring \u00d7\u228e-ICommutativeSemiring\n\nlift-\u228e : \u2200 {A B : Set} \u2192 Inv.Inverse ((\u2261.setoid A) \u228e-setoid (\u2261.setoid B)) (\u2261.setoid (A \u228e B))\nlift-\u228e {A}{B} = record\n { to = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = cong\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = \u03bb x \u2192 Setoid.reflexive (\u2261.setoid A \u228e-setoid \u2261.setoid B) x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid A \u228e-setoid \u2261.setoid B)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid (A \u228e B))\n }\n } where\n cong : Setoid._\u2248_ (\u2261.setoid A \u228e-setoid \u2261.setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (\u2261.setoid (A \u228e B))\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2261.cong inj\u2081 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2261.cong inj\u2082 x\u223c\u2082y\n\nswap-iso : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 (A \u00d7 B) \u2194 (B \u00d7 A)\nswap-iso = inverses swap swap (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u00d7 B \u2192 \u2605 c} where\n \u03a3\u00d7-swap : \u03a3 (A \u00d7 B) C \u2194 \u03a3 (B \u00d7 A) (C F.\u2218 swap)\n \u03a3\u00d7-swap = first-iso swap-iso\n\nMaybe\u2194Lift\u22a4\u228e : \u2200 {\u2113 a} {A : \u2605 a} \u2192 Maybe A \u2194 (Lift {\u2113 = \u2113} \u22a4 \u228e A)\nMaybe\u2194Lift\u22a4\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe\u2194\u22a4\u228e : \u2200 {a} {A : \u2605 a} \u2192 Maybe A \u2194 (\u22a4 \u228e A)\nMaybe\u2194\u22a4\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 A \u2194 B \u2192 Maybe A \u2194 Maybe B\nMaybe-cong A\u2194B = sym Maybe\u2194\u22a4\u228e \u2218 id \u228e-cong A\u2194B \u2218 Maybe\u2194\u22a4\u228e\n\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe : \u2200 {a} {A : \u2605 a} n \u2192 Maybe (Maybe^ n A) \u2194 Maybe^ n (Maybe A)\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe zero = id\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe (suc n) = Maybe-cong (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n)\n\nMaybe^-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} n \u2192 A \u2194 B \u2192 Maybe^ n A \u2194 Maybe^ n B\nMaybe^-cong zero = F.id\nMaybe^-cong (suc n) = Maybe-cong F.\u2218 Maybe^-cong n\n\nVec0\u2194Lift\u22a4 : \u2200 {a \u2113} {A : \u2605 a} \u2192 Vec A 0 \u2194 Lift {_} {\u2113} \u22a4\nVec0\u2194Lift\u22a4 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec0\u2194\u22a4 : \u2200 {a} {A : \u2605 a} \u2192 Vec A 0 \u2194 \u22a4\nVec0\u2194\u22a4 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec\u2218suc\u2194A\u00d7Vec : \u2200 {a} {A : \u2605 a} {n} \u2192 Vec A (suc n) \u2194 (A \u00d7 Vec A n)\nVec\u2218suc\u2194A\u00d7Vec\n = inverses (\u03bb { (x \u2237 xs) \u2192 x , xs }) (uncurry _\u2237_)\n (\u03bb { (x \u2237 xs) \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\ninfix 8 _^_\n\n_^_ : \u2200 {a} \u2192 \u2605 a \u2192 \u2115 \u2192 \u2605 a\nA ^ 0 = Lift \u22a4\nA ^ suc n = A \u00d7 A ^ n\n\n^\u2194Vec : \u2200 {a} {A : \u2605 a} n \u2192 (A ^ n) \u2194 Vec A n\n^\u2194Vec zero = sym Vec0\u2194Lift\u22a4\n^\u2194Vec (suc n) = sym Vec\u2218suc\u2194A\u00d7Vec \u2218 (id \u00d7-cong (^\u2194Vec n))\n\nFin0\u2194\u22a5 : Fin 0 \u2194 \u22a5\nFin0\u2194\u22a5 = inverses (\u03bb()) (\u03bb()) (\u03bb()) (\u03bb())\n\nFin\u2218suc\u2194Maybe\u2218Fin : \u2200 {n} \u2192 Fin (suc n) \u2194 Maybe (Fin n)\nFin\u2218suc\u2194Maybe\u2218Fin {n}\n = inverses to' (maybe suc zero)\n (\u03bb { zero \u2192 \u2261.refl ; (suc n) \u2192 \u2261.refl })\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n where to' : Fin (suc n) \u2192 Maybe (Fin n)\n to' zero = nothing\n to' (suc n) = just n\n\nFin-injective : \u2200 {m n} \u2192 Fin m \u2194 Fin n \u2192 m \u2261 n\nFin-injective = go _ _ where\n go : \u2200 m n \u2192 Fin m \u2194 Fin n \u2192 m \u2261 n\n go zero zero iso = \u2261.refl\n go zero (suc n) iso with from iso zero\n ... | ()\n go (suc m) zero iso with to iso zero\n ... | ()\n go (suc m) (suc n) iso = \u2261.cong suc (go m n (Maybe-injective (Fin\u2218suc\u2194Maybe\u2218Fin \u2218 iso \u2218 sym Fin\u2218suc\u2194Maybe\u2218Fin)))\n\nLift\u2194id : \u2200 {a} {A : \u2605 a} \u2192 Lift {a} {a} A \u2194 A\nLift\u2194id = inverses lower lift (\u03bb { (lift x) \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\n\u22a4\u00d7A\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\u22a4 \u00d7 A) \u2194 A\n\u22a4\u00d7A\u2194A = proj\u2081 \u00d7-CMon.identity _ \u2218 sym Lift\u2194id \u00d7-cong id\n\nA\u00d7\u22a4\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u00d7 \u22a4) \u2194 A\nA\u00d7\u22a4\u2194A = proj\u2082 \u00d7-CMon.identity _ \u2218 id \u00d7-cong sym Lift\u2194id\n\n\u22a5\u228eA\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\u22a5 \u228e A) \u2194 A\n\u22a5\u228eA\u2194A = proj\u2081 \u228e-CMon.identity _ \u2218 sym Lift\u2194id \u228e-cong id\n\nA\u228e\u22a5\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u228e \u22a5) \u2194 A\nA\u228e\u22a5\u2194A = proj\u2082 \u228e-CMon.identity _ \u2218 id \u228e-cong sym Lift\u2194id\n\n\u22a5\u00d7A\u2194\u22a5 : \u2200 {A : \u2605\u2080} \u2192 (\u22a5 \u00d7 A) \u2194 \u22a5\n\u22a5\u00d7A\u2194\u22a5 = Lift\u2194id \u2218 proj\u2081 \u00d7\u228e\u00b0.zero _ \u2218 sym (Lift\u2194id \u00d7-cong id)\n\nMaybe\u22a5\u2194\u22a4 : Maybe \u22a5 \u2194 \u22a4\nMaybe\u22a5\u2194\u22a4 = A\u228e\u22a5\u2194A \u2218 Maybe\u2194\u22a4\u228e\n\nMaybe^\u22a5\u2194Fin : \u2200 n \u2192 Maybe^ n \u22a5 \u2194 Fin n\nMaybe^\u22a5\u2194Fin zero = sym Fin0\u2194\u22a5\nMaybe^\u22a5\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\u22a5\u2194Fin n)\n\nMaybe^\u22a4\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \u22a4 \u2194 Fin (suc n)\nMaybe^\u22a4\u2194Fin1+ n = Maybe^\u22a5\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\u22a5\u2194\u22a4)\n\nMaybe-\u228e : \u2200 {a} {A B : \u2605 a} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e {a} = sym Maybe\u2194Lift\u22a4\u228e \u2218 \u228e-CMon.assoc (Lift {_} {a} \u22a4) _ _ \u2218 (Maybe\u2194Lift\u22a4\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \u22a5 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \u22a5\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n\nBit\u2194\u22a4\u228e\u22a4 : Bit \u2194 (\u22a4 \u228e \u22a4)\nBit\u2194\u22a4\u228e\u22a4 = inverses (proj (inj\u2081 _ , inj\u2082 _)) [ F.const 0b , F.const 1b ] \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d0\u21d2 : (_ : Bit) \u2192 _\n \u21d0\u21d2 true = \u2261.refl\n \u21d0\u21d2 false = \u2261.refl\n \u21d2\u21d0 : (_ : \u22a4 \u228e \u22a4) \u2192 _\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\nFin-\u228e-+ : \u2200 m n \u2192 (Fin m \u228e Fin n) \u2194 Fin (m + n)\nFin-\u228e-+ m n = Maybe^\u22a5\u2194Fin (m + n)\n \u2218 Maybe^-\u228e-+ m n\n \u2218 sym (Maybe^\u22a5\u2194Fin m \u228e-cong Maybe^\u22a5\u2194Fin n)\n\nFin\u2218suc\u2194\u22a4\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2194 (\u22a4 \u228e Fin n)\nFin\u2218suc\u2194\u22a4\u228eFin = Maybe\u2194\u22a4\u228e \u2218 Fin\u2218suc\u2194Maybe\u2218Fin\n\nFin-\u00d7-* : \u2200 m n \u2192 (Fin m \u00d7 Fin n) \u2194 Fin (m * n)\nFin-\u00d7-* zero n = (Fin 0 \u00d7 Fin n) \u2194\u27e8 Fin0\u2194\u22a5 \u00d7-cong id \u27e9\n (\u22a5 \u00d7 Fin n) \u2194\u27e8 \u22a5\u00d7A\u2194\u22a5 \u27e9\n \u22a5 \u2194\u27e8 sym Fin0\u2194\u22a5 \u27e9\n Fin 0 \u220e\n where open EquationalReasoning hiding (sym)\nFin-\u00d7-* (suc m) n = (Fin (suc m) \u00d7 Fin n) \u2194\u27e8 Fin\u2218suc\u2194\u22a4\u228eFin \u00d7-cong id \u27e9\n ((\u22a4 \u228e Fin m) \u00d7 Fin n) \u2194\u27e8 \u00d7\u228e\u00b0.distrib\u02b3 (Fin n) \u22a4 (Fin m) \u27e9\n ((\u22a4 \u00d7 Fin n) \u228e (Fin m \u00d7 Fin n)) \u2194\u27e8 \u22a4\u00d7A\u2194A \u228e-cong Fin-\u00d7-* m n \u27e9\n (Fin n \u228e Fin (m * n)) \u2194\u27e8 Fin-\u228e-+ n (m * n) \u27e9\n Fin (suc m * n) \u220e\n where open EquationalReasoning hiding (sym)\n-- -}\n","old_contents":"module Function.Related.TypeIsomorphisms.NP where\n\nimport Level as L\nopen import Algebra\nimport Algebra.FunctionProperties as FP\nopen L using (Lift; lower; lift)\nopen import Type hiding (\u2605)\n\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Vec using (Vec; []; _\u2237_)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_) renaming (_^_ to _**_)\nopen import Data.Maybe.NP\nopen import Data.Product.NP renaming (map to map\u00d7)\n--open import Data.Product.N-ary\nopen import Data.Sum renaming (map to map\u228e)\nopen import Data.Unit\nopen import Data.Empty\nopen import Data.Bits using (Bit; 0b; 1b; false; true; proj)\n\nimport Function.NP as F\nopen F using (\u03a0)\nimport Function.Equality as FE\nopen FE using (_\u27e8$\u27e9_)\nopen import Function.Related as FR\nopen import Function.Related.TypeIsomorphisms public\nimport Function.Inverse.NP as Inv\nopen Inv using (_\u2194_; _\u2218_; sym; id; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nopen import Relation.Binary\nopen import Relation.Binary.Product.Pointwise\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_ ; _\u2262_)\n\nmodule CancelMaybe where\n private\n module _ {A : Set} where\n just-inj : {x y : A} \u2192 Maybe.just x \u2261 just y \u2192 x \u2261 y\n just-inj \u2261.refl = \u2261.refl\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (tof-tot : \u2200 x \u2192 Inverse.to f \u27e8$\u27e9 just x \u2262 nothing)\n (fof-tot : \u2200 x \u2192 Inverse.from f \u27e8$\u27e9 just x \u2262 nothing) where\n\n CancelMaybe' : A \u2194 B\n CancelMaybe' = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n to = \u03bb x \u2192 Inverse.to f \u27e8$\u27e9 x\n from = \u03bb x \u2192 Inverse.from f \u27e8$\u27e9 x\n\n \u21d2 : _ \u2192 _\n \u21d2 x with to (just x) | tof-tot x\n \u21d2 x | just x\u2081 | _ = x\u2081\n \u21d2 x | nothing | p = \u22a5-elim (p \u2261.refl)\n\n \u21d0 : _ \u2192 _\n \u21d0 x with from (just x) | fof-tot x\n \u21d0 x | just x\u2081 | p = x\u2081\n \u21d0 x | nothing | p = \u22a5-elim (p \u2261.refl)\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 x with to (just x)\n | tof-tot x\n | from (just (\u21d2 x))\n | fof-tot (\u21d2 x)\n | Inverse.left-inverse-of f (just x)\n | Inverse.right-inverse-of f (just (\u21d2 x))\n \u21d0\u21d2 x | just x\u2081 | p | just x\u2082 | q | b | c = just-inj (\u2261.trans (\u2261.sym (Inverse.left-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong from c) b))\n \u21d0\u21d2 x | just x\u2081 | p | nothing | q | _ | _ = \u22a5-elim (q \u2261.refl)\n \u21d0\u21d2 x | nothing | p | z | q | _ | _ = \u22a5-elim (p \u2261.refl)\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 x with from (just x)\n | fof-tot x\n | to (just (\u21d0 x))\n | tof-tot (\u21d0 x)\n | Inverse.right-inverse-of f (just x)\n | Inverse.left-inverse-of f (just (\u21d0 x))\n \u21d2\u21d0 x | just x\u2081 | p | just x\u2082 | q | b | c = just-inj (\u2261.trans (\u2261.sym (Inverse.right-inverse-of f (just x\u2082))) (\u2261.trans (\u2261.cong to c) b))\n \u21d2\u21d0 x | just x\u2081 | p | nothing | q | _ | _ = \u22a5-elim (q \u2261.refl)\n \u21d2\u21d0 x | nothing | p | z | q | _ | _ = \u22a5-elim (p \u2261.refl)\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B)\n (f-empty : Inverse.to f \u27e8$\u27e9 nothing \u2261 nothing) where\n\n tof-tot : \u2200 x \u2192 Inverse.to f \u27e8$\u27e9 just x \u2262 nothing\n tof-tot x eq with Inverse.injective f (\u2261.trans eq (\u2261.sym f-empty))\n ... | ()\n\n f-empty' : Inverse.from f \u27e8$\u27e9 nothing \u2261 nothing\n f-empty' = \u2261.trans (\u2261.sym (\u2261.cong (\u03bb x \u2192 Inverse.from f \u27e8$\u27e9 x) f-empty)) (Inverse.left-inverse-of f nothing)\n\n fof-tot : \u2200 x \u2192 Inverse.from f \u27e8$\u27e9 just x \u2262 nothing\n fof-tot x eq with Inverse.injective (sym f) (\u2261.trans eq (\u2261.sym f-empty'))\n ... | ()\n\n iso : A \u2194 B\n iso = CancelMaybe' f tof-tot fof-tot\n\n\n module _ {A B : Set}\n (f : Maybe A \u2194 Maybe B) where\n\n g : Maybe A \u2194 Maybe B\n g = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0 where\n to = \u03bb x \u2192 Inverse.to f \u27e8$\u27e9 x\n from = \u03bb x \u2192 Inverse.from f \u27e8$\u27e9 x\n\n \u21d2 : Maybe A \u2192 Maybe B\n \u21d2 (just x) with to (just x)\n ... | nothing = to nothing\n ... | just y = just y\n \u21d2 nothing = nothing\n\n \u21d0 : Maybe B \u2192 Maybe A\n \u21d0 (just x) with from (just x)\n ... | nothing = from nothing\n ... | just y = just y\n \u21d0 nothing = nothing\n\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (just x) with to (just x) | Inverse.left-inverse-of f (just x)\n \u21d0\u21d2 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d0\u21d2 (just x) | nothing | p with to nothing | Inverse.left-inverse-of f nothing\n \u21d0\u21d2 (just x\u2081) | nothing | p | just x | q rewrite q = p\n \u21d0\u21d2 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d0\u21d2 nothing = \u2261.refl\n\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (just x) with from (just x) | Inverse.right-inverse-of f (just x)\n \u21d2\u21d0 (just x) | just x\u2081 | p rewrite p = \u2261.refl\n \u21d2\u21d0 (just x) | nothing | p with from nothing | Inverse.right-inverse-of f nothing\n \u21d2\u21d0 (just x\u2081) | nothing | p | just x | q rewrite q = p\n \u21d2\u21d0 (just x) | nothing | p | nothing | q = \u2261.trans (\u2261.sym q) p\n \u21d2\u21d0 nothing = \u2261.refl\n\n g-empty : Inverse.to g \u27e8$\u27e9 nothing \u2261 nothing\n g-empty = \u2261.refl\n\n iso' : A \u2194 B\n iso' = iso g g-empty\n CancelMaybe : \u2200 {A B : Set} \u2192 Maybe A \u2194 Maybe B \u2192 A \u2194 B\n CancelMaybe f = iso' f\n\nprivate\n Setoid\u2080 : \u2605 _\n Setoid\u2080 = Setoid L.zero L.zero\n\n\u03a3\u2261\u2194\u22a4 : \u2200 {a} {A : \u2605 a} x \u2192 \u03a3 A (_\u2261_ x) \u2194 \u22a4\n\u03a3\u2261\u2194\u22a4 x = inverses (F.const _) (\u03bb _ \u2192 _ , \u2261.refl)\n helper (\u03bb _ \u2192 \u2261.refl)\n where helper : (y : \u03a3 _ (_\u2261_ x)) \u2192 (x , \u2261.refl) \u2261 y\n helper (.x , \u2261.refl) = \u2261.refl\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : \u03a3 A B \u2192 \u2605 c} where\n curried : \u03a0 (\u03a3 A B) C \u2194 \u03a0 A \u03bb a \u2192 \u03a0 (B a) \u03bb b \u2192 C (a , b)\n curried = inverses curry uncurry (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u2192 \u2605 c} (f : A \u2194 B) where\n private\n left-f = Inv.Inverse.left-inverse-of f\n right-f = Inv.Inverse.right-inverse-of f\n coe : \u2200 x \u2192 C x \u2192 C (from f (to f x))\n coe x = \u2261.subst C (\u2261.sym (left-f x))\n \u21d2 : \u03a3 A C \u2192 \u03a3 B (C F.\u2218 from f)\n \u21d2 (x , p) = to f x , coe x p\n \u21d0 : \u03a3 B (C F.\u2218 from f) \u2192 \u03a3 A C\n \u21d0 = first (from f)\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , p) rewrite left-f x = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 p = mk\u03a3\u2261 (C F.\u2218 from f) (right-f (proj\u2081 p)) (helper p)\n where\n helper : \u2200 p \u2192 \u2261.subst (C F.\u2218 from f) (right-f (proj\u2081 p)) (coe (proj\u2081 (\u21d0 p)) (proj\u2082 (\u21d0 p))) \u2261 proj\u2082 p\n helper p with to f (from f (proj\u2081 p)) | right-f (proj\u2081 p) | left-f (from f (proj\u2081 p))\n helper _ | ._ | \u2261.refl | \u2261.refl = \u2261.refl\n first-iso : \u03a3 A C \u2194 \u03a3 B (C F.\u2218 from f)\n first-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : B \u2192 \u2605 c} (f : A \u2194 B) where\n sym-first-iso : \u03a3 A (C F.\u2218 to f) \u2194 \u03a3 B C\n sym-first-iso = sym (first-iso (sym f))\n\nmodule _ {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} (f : \u2200 x \u2192 B x \u2194 C x) where\n private\n left-f = Inv.Inverse.left-inverse-of F.\u2218 f\n right-f = Inv.Inverse.right-inverse-of F.\u2218 f\n \u21d2 : \u03a3 A B \u2192 \u03a3 A C\n \u21d2 = second (to (f _))\n \u21d0 : \u03a3 A C \u2192 \u03a3 A B\n \u21d0 = second (from (f _))\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (x , y) rewrite left-f x y = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (x , y) rewrite right-f x y = \u2261.refl\n second-iso : \u03a3 A B \u2194 \u03a3 A C\n second-iso = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a3 (A \u228e B) C\n T = \u03a3 A (C F.\u2218 inj\u2081) \u228e \u03a3 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 (inj\u2081 x , y) = inj\u2081 (x , y)\n \u21d2 (inj\u2082 x , y) = inj\u2082 (x , y)\n \u21d0 : T \u2192 S\n \u21d0 (inj\u2081 (x , y)) = inj\u2081 x , y\n \u21d0 (inj\u2082 (x , y)) = inj\u2082 x , y\n \u21d0\u21d2 : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n \u21d0\u21d2 (inj\u2081 _ , _) = \u2261.refl\n \u21d0\u21d2 (inj\u2082 _ , _) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\n \u03a3\u228e-distrib : S \u2194 T\n \u03a3\u228e-distrib = inverses (\u21d2) (\u21d0) \u21d0\u21d2 \u21d2\u21d0\n\n{- requires extensional equality\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u228e B \u2192 \u2605 c} where\n private\n S = \u03a0 (A \u228e B) C\n T = \u03a0 A (C F.\u2218 inj\u2081) \u00d7 \u03a0 B (C F.\u2218 inj\u2082)\n \u21d2 : S \u2192 T\n \u21d2 f = f F.\u2218 inj\u2081 , f F.\u2218 inj\u2082\n \u21d0 : T \u2192 S\n \u21d0 (f , g) = [ f , g ]\n \u21d0\u21d2 : \u2200 f x \u2192 \u21d0 (\u21d2 f) x \u2261 f x\n \u21d0\u21d2 f (inj\u2081 x) = \u2261.refl\n \u21d0\u21d2 f (inj\u2082 y) = \u2261.refl\n \u21d2\u21d0 : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n \u21d2\u21d0 (f , g) = \u2261.refl\n\n \u03a0\u00d7-distrib : S \u2194 T\n \u03a0\u00d7-distrib = inverses (\u21d2) (\u21d0) {!\u21d0\u21d2!} \u21d2\u21d0\n-}\n\n\u228e-ICommutativeMonoid : CommutativeMonoid _ _\n\u228e-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u228e-setoid_;\n \u03b5 = \u2261.setoid \u22a5;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u228e-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \u22a5) _\u228e-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = [ (\u03bb ()) , F.id ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = inj\u2082\n ; cong = \u2082\u223c\u2082\n }\n ; inverse-of = record\n { left-inverse-of = [ (\u03bb ()) , (\u03bb x \u2192 \u2082\u223c\u2082 refl) ]\n ; right-inverse-of = \u03bb x \u2192 refl\n }\n } where open Setoid A\n cong-to : Setoid._\u2248_ (\u2261.setoid \u22a5 \u228e-setoid A) =[ _ ]\u21d2 _\u2248_\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y) rewrite x\u223c\u2081y = refl\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = x\u223c\u2082y\n\n assoc : Associative _\u228e-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = [ [ inj\u2081 , inj\u2082 F.\u2218 inj\u2081 ] , inj\u2082 F.\u2218 inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = [ inj\u2081 F.\u2218 inj\u2081 , [ inj\u2081 F.\u2218 inj\u2082 , inj\u2082 ] ]\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = [ [ (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2081\u223c\u2081 (refl A))) , (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2082\u223c\u2082 (refl B)) ) ] , (\u03bb _ \u2192 \u2082\u223c\u2082 (refl C)) ]\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (refl A)) , [ (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2081\u223c\u2081 (refl B))) , (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2082\u223c\u2082 (refl C))) ] ]\n }\n } where\n open Setoid\n cong-to : _\u2248_ ((A \u228e-setoid B) \u228e-setoid C) =[ _ ]\u21d2 _\u2248_ (A \u228e-setoid (B \u228e-setoid C))\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2082 ()))\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 x\u223c\u2081y\n cong-to (\u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2082y)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = \u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)\n\n cong-from : _\u2248_ (A \u228e-setoid (B \u228e-setoid C)) =[ _ ]\u21d2 _\u2248_ ((A \u228e-setoid B) \u228e-setoid C)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 x\u223c\u2081y) = \u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2082 ()))\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 x\u223c\u2082y\n\n comm : Commutative _\u228e-setoid_\n comm A B = record\n { to = swap'\n ; from = swap'\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u228e-setoid B FE.\u27f6 B \u228e-setoid A\n swap' {A} {B} = record\n { _\u27e8$\u27e9_ = [ inj\u2082 , inj\u2081 ]\n ; cong = cong\n } where\n cong : Setoid._\u2248_ (A \u228e-setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (B \u228e-setoid A)\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2082\u223c\u2082 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2081\u223c\u2081 x\u223c\u2082y\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u228e-setoid B) (swap' FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (Setoid.refl A)) , (\u03bb _ \u2192 \u2082\u223c\u2082 (Setoid.refl B)) ]\n\n\u00d7-ICommutativeMonoid : CommutativeMonoid _ _\n\u00d7-ICommutativeMonoid = record {\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u00d7-setoid_;\n \u03b5 = \u2261.setoid \u22a4;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv.isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u00d7-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \u22a4) _\u00d7-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2082\n ; cong = proj\u2082\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 _ , x\n ; cong = \u03bb x \u2192 \u2261.refl , x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u2261.refl , (Setoid.refl A)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl A\n }\n }\n\n assoc : Associative _\u00d7-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n ; cong = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n }\n ; from = record\n { _\u27e8$\u27e9_ = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n ; cong = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb _ \u2192 Setoid.refl ((A \u00d7-setoid B) \u00d7-setoid C)\n ; right-inverse-of = \u03bb _ \u2192 Setoid.refl (A \u00d7-setoid (B \u00d7-setoid C))\n }\n }\n\n comm : Commutative _\u00d7-setoid_\n comm A B = record\n { to = swap' {A} {B}\n ; from = swap' {B} {A}\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B} \u2192 A \u00d7-setoid B FE.\u27f6 B \u00d7-setoid A\n swap' {A}{B} = record { _\u27e8$\u27e9_ = swap; cong = swap }\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u00d7-setoid B) (swap' {B} {A} FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = \u03bb x \u2192 Setoid.refl (A \u00d7-setoid B)\n\n\u00d7\u228e-ICommutativeSemiring : CommutativeSemiring _ _\n\u00d7\u228e-ICommutativeSemiring = record\n { _\u2248_ = Inv.Inverse\n ; _+_ = _\u228e-setoid_\n ; _*_ = _\u00d7-setoid_\n ; 0# = \u2261.setoid \u22a5\n ; 1# = \u2261.setoid \u22a4\n ; isCommutativeSemiring = record\n { +-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u228e-ICommutativeMonoid\n ; *-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u00d7-ICommutativeMonoid\n ; distrib\u02b3 = distrib\u02b3\n ; zero\u02e1 = zero\u02e1\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n distrib\u02b3 : _\u00d7-setoid_ DistributesOver\u02b3 _\u228e-setoid_\n distrib\u02b3 A B C = record\n { to = record\n { _\u27e8$\u27e9_ = uncurry [ curry inj\u2081 , curry inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = frm\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = uncurry [ (\u03bb x y \u2192 (\u2081\u223c\u2081 (refl B)) , (refl A)) , (\u03bb x y \u2192 (\u2082\u223c\u2082 (refl C)) , (refl A)) ]\n ; right-inverse-of = [ uncurry (\u03bb x y \u2192 \u2081\u223c\u2081 ((refl B {x}) , (refl A {y})))\n , uncurry (\u03bb x y \u2192 \u2082\u223c\u2082 ((refl C {x}) , (refl A {y}))) ]\n }\n } where\n open Setoid\n A' = Carrier A\n B' = Carrier B\n C' = Carrier C\n\n cong-to : _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A) =[ _ ]\u21d2 _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A))\n cong-to (\u2081\u223c\u2082 () , _)\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y , A-rel) = \u2081\u223c\u2081 (x\u223c\u2081y , A-rel)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y , A-rel) = \u2082\u223c\u2082 (x\u223c\u2082y , A-rel)\n\n frm : B' \u00d7 A' \u228e C' \u00d7 A' \u2192 (B' \u228e C') \u00d7 A'\n frm = [ map\u00d7 inj\u2081 F.id , map\u00d7 inj\u2082 F.id ]\n\n cong-from : _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A)) =[ _ ]\u21d2 _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 (B-rel , A-rel)) = \u2081\u223c\u2081 B-rel , A-rel\n cong-from (\u2082\u223c\u2082 (C-rel , A-rel)) = \u2082\u223c\u2082 C-rel , A-rel\n\n zero\u02e1 : LeftZero (\u2261.setoid \u22a5) _\u00d7-setoid_\n zero\u02e1 A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2081\n ; cong = proj\u2081\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u22a5-elim\n ; cong = \u03bb { {()} x }\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u22a5-elim (proj\u2081 x)\n ; right-inverse-of = \u03bb x \u2192 \u22a5-elim x\n }\n }\n\nmodule \u00d7-CMon {a} = CommutativeMonoid (\u00d7-CommutativeMonoid FR.bijection a)\nmodule \u228e-CMon {a} = CommutativeMonoid (\u228e-CommutativeMonoid FR.bijection a)\nmodule \u00d7\u228e\u00b0 {a} = CommutativeSemiring (\u00d7\u228e-CommutativeSemiring FR.bijection a)\n\nmodule \u00d7-ICMon = CommutativeMonoid \u00d7-ICommutativeMonoid\nmodule \u228e-ICMon = CommutativeMonoid \u228e-ICommutativeMonoid\nmodule \u00d7\u228e\u00b0I = CommutativeSemiring \u00d7\u228e-ICommutativeSemiring\n\nlift-\u228e : \u2200 {A B : Set} \u2192 Inv.Inverse ((\u2261.setoid A) \u228e-setoid (\u2261.setoid B)) (\u2261.setoid (A \u228e B))\nlift-\u228e {A}{B} = record\n { to = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = cong\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 x\n ; cong = \u03bb x \u2192 Setoid.reflexive (\u2261.setoid A \u228e-setoid \u2261.setoid B) x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid A \u228e-setoid \u2261.setoid B)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl (\u2261.setoid (A \u228e B))\n }\n } where\n cong : Setoid._\u2248_ (\u2261.setoid A \u228e-setoid \u2261.setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (\u2261.setoid (A \u228e B))\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2261.cong inj\u2081 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2261.cong inj\u2082 x\u223c\u2082y\n\nswap-iso : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 (A \u00d7 B) \u2194 (B \u00d7 A)\nswap-iso = inverses swap swap (\u03bb _ \u2192 \u2261.refl) (\u03bb _ \u2192 \u2261.refl)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u00d7 B \u2192 \u2605 c} where\n \u03a3\u00d7-swap : \u03a3 (A \u00d7 B) C \u2194 \u03a3 (B \u00d7 A) (C F.\u2218 swap)\n \u03a3\u00d7-swap = first-iso swap-iso\n\nMaybe\u2194Lift\u22a4\u228e : \u2200 {\u2113 a} {A : \u2605 a} \u2192 Maybe A \u2194 (Lift {\u2113 = \u2113} \u22a4 \u228e A)\nMaybe\u2194Lift\u22a4\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe\u2194\u22a4\u228e : \u2200 {a} {A : \u2605 a} \u2192 Maybe A \u2194 (\u22a4 \u228e A)\nMaybe\u2194\u22a4\u228e\n = inverses (maybe inj\u2082 (inj\u2081 _))\n [ F.const nothing , just ]\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ]\n\nMaybe-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} \u2192 A \u2194 B \u2192 Maybe A \u2194 Maybe B\nMaybe-cong A\u2194B = sym Maybe\u2194\u22a4\u228e \u2218 id \u228e-cong A\u2194B \u2218 Maybe\u2194\u22a4\u228e\n\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe : \u2200 {a} {A : \u2605 a} n \u2192 Maybe (Maybe^ n A) \u2194 Maybe^ n (Maybe A)\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe zero = id\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe (suc n) = Maybe-cong (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n)\n\nMaybe^-cong : \u2200 {a b} {A : \u2605 a} {B : \u2605 b} n \u2192 A \u2194 B \u2192 Maybe^ n A \u2194 Maybe^ n B\nMaybe^-cong zero = F.id\nMaybe^-cong (suc n) = Maybe-cong F.\u2218 Maybe^-cong n\n\nVec0\u2194Lift\u22a4 : \u2200 {a \u2113} {A : \u2605 a} \u2192 Vec A 0 \u2194 Lift {_} {\u2113} \u22a4\nVec0\u2194Lift\u22a4 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec0\u2194\u22a4 : \u2200 {a} {A : \u2605 a} \u2192 Vec A 0 \u2194 \u22a4\nVec0\u2194\u22a4 = inverses _ (F.const []) (\u03bb { [] \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\nVec\u2218suc\u2194A\u00d7Vec : \u2200 {a} {A : \u2605 a} {n} \u2192 Vec A (suc n) \u2194 (A \u00d7 Vec A n)\nVec\u2218suc\u2194A\u00d7Vec\n = inverses (\u03bb { (x \u2237 xs) \u2192 x , xs }) (uncurry _\u2237_)\n (\u03bb { (x \u2237 xs) \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\ninfix 8 _^_\n\n_^_ : \u2200 {a} \u2192 \u2605 a \u2192 \u2115 \u2192 \u2605 a\nA ^ 0 = Lift \u22a4\nA ^ suc n = A \u00d7 A ^ n\n\n^\u2194Vec : \u2200 {a} {A : \u2605 a} n \u2192 (A ^ n) \u2194 Vec A n\n^\u2194Vec zero = sym Vec0\u2194Lift\u22a4\n^\u2194Vec (suc n) = sym Vec\u2218suc\u2194A\u00d7Vec \u2218 (id \u00d7-cong (^\u2194Vec n))\n\nFin0\u2194\u22a5 : Fin 0 \u2194 \u22a5\nFin0\u2194\u22a5 = inverses (\u03bb()) (\u03bb()) (\u03bb()) (\u03bb())\n\nFin\u2218suc\u2194Maybe\u2218Fin : \u2200 {n} \u2192 Fin (suc n) \u2194 Maybe (Fin n)\nFin\u2218suc\u2194Maybe\u2218Fin {n}\n = inverses to' (maybe suc zero)\n (\u03bb { zero \u2192 \u2261.refl ; (suc n) \u2192 \u2261.refl })\n (maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl)\n where to' : Fin (suc n) \u2192 Maybe (Fin n)\n to' zero = nothing\n to' (suc n) = just n\n\nLift\u2194id : \u2200 {a} {A : \u2605 a} \u2192 Lift {a} {a} A \u2194 A\nLift\u2194id = inverses lower lift (\u03bb { (lift x) \u2192 \u2261.refl }) (\u03bb _ \u2192 \u2261.refl)\n\n\u22a4\u00d7A\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\u22a4 \u00d7 A) \u2194 A\n\u22a4\u00d7A\u2194A = proj\u2081 \u00d7-CMon.identity _ \u2218 sym Lift\u2194id \u00d7-cong id\n\nA\u00d7\u22a4\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u00d7 \u22a4) \u2194 A\nA\u00d7\u22a4\u2194A = proj\u2082 \u00d7-CMon.identity _ \u2218 id \u00d7-cong sym Lift\u2194id\n\n\u22a5\u228eA\u2194A : \u2200 {A : \u2605\u2080} \u2192 (\u22a5 \u228e A) \u2194 A\n\u22a5\u228eA\u2194A = proj\u2081 \u228e-CMon.identity _ \u2218 sym Lift\u2194id \u228e-cong id\n\nA\u228e\u22a5\u2194A : \u2200 {A : \u2605\u2080} \u2192 (A \u228e \u22a5) \u2194 A\nA\u228e\u22a5\u2194A = proj\u2082 \u228e-CMon.identity _ \u2218 id \u228e-cong sym Lift\u2194id\n\n\u22a5\u00d7A\u2194\u22a5 : \u2200 {A : \u2605\u2080} \u2192 (\u22a5 \u00d7 A) \u2194 \u22a5\n\u22a5\u00d7A\u2194\u22a5 = Lift\u2194id \u2218 proj\u2081 \u00d7\u228e\u00b0.zero _ \u2218 sym (Lift\u2194id \u00d7-cong id)\n\nMaybe\u22a5\u2194\u22a4 : Maybe \u22a5 \u2194 \u22a4\nMaybe\u22a5\u2194\u22a4 = A\u228e\u22a5\u2194A \u2218 Maybe\u2194\u22a4\u228e\n\nMaybe^\u22a5\u2194Fin : \u2200 n \u2192 Maybe^ n \u22a5 \u2194 Fin n\nMaybe^\u22a5\u2194Fin zero = sym Fin0\u2194\u22a5\nMaybe^\u22a5\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\u22a5\u2194Fin n)\n\nMaybe^\u22a4\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \u22a4 \u2194 Fin (suc n)\nMaybe^\u22a4\u2194Fin1+ n = Maybe^\u22a5\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\u22a5\u2194\u22a4)\n\nMaybe-\u228e : \u2200 {a} {A B : \u2605 a} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e {a} = sym Maybe\u2194Lift\u22a4\u228e \u2218 \u228e-CMon.assoc (Lift {_} {a} \u22a4) _ _ \u2218 (Maybe\u2194Lift\u22a4\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \u22a5 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \u22a5\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n\nBit\u2194\u22a4\u228e\u22a4 : Bit \u2194 (\u22a4 \u228e \u22a4)\nBit\u2194\u22a4\u228e\u22a4 = inverses (proj (inj\u2081 _ , inj\u2082 _)) [ F.const 0b , F.const 1b ] \u21d0\u21d2 \u21d2\u21d0\n where\n \u21d0\u21d2 : (_ : Bit) \u2192 _\n \u21d0\u21d2 true = \u2261.refl\n \u21d0\u21d2 false = \u2261.refl\n \u21d2\u21d0 : (_ : \u22a4 \u228e \u22a4) \u2192 _\n \u21d2\u21d0 (inj\u2081 _) = \u2261.refl\n \u21d2\u21d0 (inj\u2082 _) = \u2261.refl\n\nFin-\u228e-+ : \u2200 m n \u2192 (Fin m \u228e Fin n) \u2194 Fin (m + n)\nFin-\u228e-+ m n = Maybe^\u22a5\u2194Fin (m + n)\n \u2218 Maybe^-\u228e-+ m n\n \u2218 sym (Maybe^\u22a5\u2194Fin m \u228e-cong Maybe^\u22a5\u2194Fin n)\n\nFin\u2218suc\u2194\u22a4\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2194 (\u22a4 \u228e Fin n)\nFin\u2218suc\u2194\u22a4\u228eFin = Maybe\u2194\u22a4\u228e \u2218 Fin\u2218suc\u2194Maybe\u2218Fin\n\nFin-\u00d7-* : \u2200 m n \u2192 (Fin m \u00d7 Fin n) \u2194 Fin (m * n)\nFin-\u00d7-* zero n = (Fin 0 \u00d7 Fin n) \u2194\u27e8 Fin0\u2194\u22a5 \u00d7-cong id \u27e9\n (\u22a5 \u00d7 Fin n) \u2194\u27e8 \u22a5\u00d7A\u2194\u22a5 \u27e9\n \u22a5 \u2194\u27e8 sym Fin0\u2194\u22a5 \u27e9\n Fin 0 \u220e\n where open EquationalReasoning hiding (sym)\nFin-\u00d7-* (suc m) n = (Fin (suc m) \u00d7 Fin n) \u2194\u27e8 Fin\u2218suc\u2194\u22a4\u228eFin \u00d7-cong id \u27e9\n ((\u22a4 \u228e Fin m) \u00d7 Fin n) \u2194\u27e8 \u00d7\u228e\u00b0.distrib\u02b3 (Fin n) \u22a4 (Fin m) \u27e9\n ((\u22a4 \u00d7 Fin n) \u228e (Fin m \u00d7 Fin n)) \u2194\u27e8 \u22a4\u00d7A\u2194A \u228e-cong Fin-\u00d7-* m n \u27e9\n (Fin n \u228e Fin (m * n)) \u2194\u27e8 Fin-\u228e-+ n (m * n) \u27e9\n Fin (suc m * n) \u220e\n where open EquationalReasoning hiding (sym)\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"c97b0136bc51d35c39ad49db8bc8aaec3f77f724","subject":"Prove that application preserves change equivalence","message":"Prove that application preserves change equivalence\n\nAs demanded by Tillmann in\nhttps:\/\/github.com\/ps-mr\/ilc\/commit\/46789e08b7a8bbd95738f9bf14b3ff8644151772#commitcomment-5742787.\n\nOld-commit-hash: c6ca7c1a871e61d203684d8c4f36a91e24a0222d\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Equivalence.agda","new_file":"Parametric\/Change\/Equivalence.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Parametric.Change.Equivalence where\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\n-- Extension Point: None (currently). Do we need to allow plugins to customize\n-- this concept?\nStructure : Set\nStructure = Unit\n\nmodule Structure (unused : Structure) where\n\n module _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set a\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n module _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = lemma\n where\n open \u2261-Reasoning\n open import Postulate.Extensionality\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Parametric.Change.Equivalence where\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\n-- Extension Point: None (currently). Do we need to allow plugins to customize\n-- this concept?\nStructure : Set\nStructure = Unit\n\nmodule Structure (unused : Structure) where\n\n module _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set a\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely. That should be true for functions\n -- using changes parametrically, for derivatives and function changes, and\n -- for functions using only the interface to changes (including the fact\n -- that function changes are functions). Stating the general result, though,\n -- seems hard, we should rather have lemmas proving that certain classes of\n -- functions respect this equivalence.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"1ceac6499ba8089373bde5f40cdf695d3b78b462","subject":"Removed unused code.","message":"Removed unused code.\n\nIgnore-this: 3d26992a8909f6f5c83b3145ec136c54\n\ndarcs-hash:20100719033523-3bd4e-9c455816cf14148a738f6c8f20ee26a9b593bc8a.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Test\/Succeed\/LogicalConstants.agda","new_file":"Test\/Succeed\/LogicalConstants.agda","new_contents":"module Test.Succeed.LogicalConstants where\n\ninfix 4 _\u2261_\n\npostulate\n D : Set\n _\u2261_ : D \u2192 D \u2192 Set\n\n------------------------------------------------------------------------------\n-- The conjuction data type\n\nmodule Conjunction where\n\n -- N.B. It is not necessary to define neither the data constructor\n -- _,_, nor the projections -- because the ATPs implement it.\n data _\u2227_ (A B : Set) : Set where\n\n -- Testing the data constructor and the projections.\n postulate\n A B : Set\n _,_ : A \u2192 B \u2192 A \u2227 B\n \u2227-proj\u2081 : A \u2227 B \u2192 A\n \u2227-proj\u2082 : A \u2227 B \u2192 B\n {-# ATP prove _,_ #-}\n {-# ATP prove \u2227-proj\u2081 #-}\n {-# ATP prove \u2227-proj\u2082 #-}\n\n------------------------------------------------------------------------------\n-- The negation\n\nmodule Negation where\n\n infix 3 \u00ac\n\n data \u22a5 : Set where\n\n \u00ac : Set \u2192 Set\n \u00ac A = A \u2192 \u22a5\n\n postulate\n true false : D\n\n postulate\n true\u2260false : \u00ac (true \u2261 false)\n {-# ATP axiom true\u2260false #-}\n\n postulate\n testContradiction : (d : D) \u2192 true \u2261 false \u2192 d \u2261 true\n {-# ATP prove testContradiction #-}\n\n------------------------------------------------------------------------------\n-- The disjunction data type\n\nmodule Disjunction where\n infixr 1 _\u2228_\n\n -- N.B. It is not necessary to define neither the data constructors\n -- inj\u2081 and inj\u2082 nor the disyunction elimination because the ATP\n -- implements them.\n data _\u2228_ (A B : Set) : Set where\n\n -- Testing the data constructors and the elimination.\n postulate\n A B C : Set\n inj\u2081 : A \u2192 A \u2228 B\n inj\u2082 : B \u2192 A \u2228 B\n [_,_] : (A \u2192 C) \u2192 (B \u2192 C) \u2192 A \u2228 B \u2192 C\n {-# ATP prove inj\u2081 #-}\n {-# ATP prove inj\u2082 #-}\n {-# ATP prove [_,_] #-}\n\n------------------------------------------------------------------------------\n-- The existential type of D\n\nmodule ExistentialQuantifier where\n\n data \u2203D (P : D \u2192 Set) : Set where\n\n postulate\n test : (d : D) \u2192 \u2203D (\u03bb e \u2192 e \u2261 d)\n {-# ATP prove test #-}\n","old_contents":"module Test.Succeed.LogicalConstants where\n\ninfix 4 _\u2261_\n\npostulate\n D : Set\n _\u2261_ : D \u2192 D \u2192 Set\n\n------------------------------------------------------------------------------\n-- The conjuction data type\n\nmodule Conjunction where\n\n -- N.B. It is not necessary to define neither the data constructor\n -- _,_, nor the projections -- because the ATPs implement it.\n data _\u2227_ (A B : Set) : Set where\n\n -- Testing the data constructor and the projections.\n postulate\n A B : Set\n d e : D\n _,_ : A \u2192 B \u2192 A \u2227 B\n \u2227-proj\u2081 : A \u2227 B \u2192 A\n \u2227-proj\u2082 : A \u2227 B \u2192 B\n {-# ATP prove _,_ #-}\n {-# ATP prove \u2227-proj\u2081 #-}\n {-# ATP prove \u2227-proj\u2082 #-}\n\n------------------------------------------------------------------------------\n-- The negation\n\nmodule Negation where\n\n infix 3 \u00ac\n\n data \u22a5 : Set where\n\n \u00ac : Set \u2192 Set\n \u00ac A = A \u2192 \u22a5\n\n postulate\n true false : D\n\n postulate\n true\u2260false : \u00ac (true \u2261 false)\n {-# ATP axiom true\u2260false #-}\n\n postulate\n testContradiction : (d : D) \u2192 true \u2261 false \u2192 d \u2261 true\n {-# ATP prove testContradiction #-}\n\n------------------------------------------------------------------------------\n-- The disjunction data type\n\nmodule Disjunction where\n infixr 1 _\u2228_\n\n -- N.B. It is not necessary to define neither the data constructors\n -- inj\u2081 and inj\u2082 nor the disyunction elimination because the ATP\n -- implements them.\n data _\u2228_ (A B : Set) : Set where\n\n -- Testing the data constructors and the elimination.\n postulate\n A B C : Set\n inj\u2081 : A \u2192 A \u2228 B\n inj\u2082 : B \u2192 A \u2228 B\n [_,_] : (A \u2192 C) \u2192 (B \u2192 C) \u2192 A \u2228 B \u2192 C\n {-# ATP prove inj\u2081 #-}\n {-# ATP prove inj\u2082 #-}\n {-# ATP prove [_,_] #-}\n\n------------------------------------------------------------------------------\n-- The existential type of D\n\nmodule ExistentialQuantifier where\n\n data \u2203D (P : D \u2192 Set) : Set where\n\n postulate\n test : (d : D) \u2192 \u2203D (\u03bb e \u2192 e \u2261 d)\n {-# ATP prove test #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d99326e90618c5ea852a5fad09f240f5d2d6675d","subject":"Only renaming.","message":"Only renaming.\n\nIgnore-this: c0e0e712590c149fdb1dd4ebdd4efff0\n\ndarcs-hash:20100804165205-3bd4e-9ff2eb215b46180dcd0688fb7cf46f72a2769e14.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Examples\/Logic\/Predicate.agda","new_file":"Examples\/Logic\/Predicate.agda","new_contents":"------------------------------------------------------------------------------\n-- Predicate logic examples\n------------------------------------------------------------------------------\n\n-- This module contains some examples showing the use of the ATPs to\n-- prove theorems from predicate logic.\n\nmodule Examples.Logic.Predicate where\n\nopen import Examples.Logic.Constants\n\n------------------------------------------------------------------------------\n-- We postulate some predicate symbols.\npostulate\n P\u2070 : Set\n P\u00b9 Q\u00b9 : D \u2192 Set\n P\u00b2 : D \u2192 D \u2192 Set\n\n-- The introduction and elimination rules for the quantifiers are theorems.\npostulate\n \u2200DI : ((x : D) \u2192 P\u00b9 x) \u2192 \u2200D P\u00b9\n \u2200DE : \u2200D P\u00b9 \u2192 (t : D) \u2192 P\u00b9 t\n -- This elimination rule cannot prove in Coq because in Coq we can\n -- have empty domains. We do not have this problem because the ATPs\n -- assume a non-empty domain.\n \u2203DI : ((t : D) \u2192 P\u00b9 t) \u2192 \u2203D P\u00b9\n -- TODO: \u2203E : (x : D) \u2192 \u2203D P\u00b9 \u2192 (P\u00b9 x \u2192 Q\u00b9 x) \u2192 Q\u00b9 x\n{-# ATP prove \u2200DI #-}\n{-# ATP prove \u2200DE #-}\n{-# ATP prove \u2203DI #-}\n-- {-# ATP prove \u2203DE #-}\n\n-- Generalization of De Morgan's laws.\npostulate\n gDM\u2081 : \u00ac (\u2200D P\u00b9) \u2194 \u2203D (\u03bb x \u2192 \u00ac (P\u00b9 x))\n gDM\u2082 : \u00ac (\u2203D P\u00b9) \u2194 \u2200D (\u03bb x \u2192 \u00ac (P\u00b9 x))\n gDM\u2083 : \u2200D P\u00b9 \u2194 \u00ac (\u2203D (\u03bb x \u2192 \u00ac (P\u00b9 x)))\n gDM\u2084 : \u2203D P\u00b9 \u2194 \u00ac (\u2200D (\u03bb x \u2192 \u00ac (P\u00b9 x)))\n{-# ATP prove gDM\u2081 #-}\n{-# ATP prove gDM\u2082 #-}\n{-# ATP prove gDM\u2083 #-}\n{-# ATP prove gDM\u2084 #-}\n\n-- The order of quantifiers of the same sort is irrelevant.\npostulate\n ord\u2081 : \u2200D (\u03bb x \u2192 \u2200D (\u03bb y \u2192 P\u00b2 x y)) \u2194 \u2200D (\u03bb y \u2192 \u2200D (\u03bb x \u2192 P\u00b2 x y))\n ord\u2082 : \u2203D (\u03bb x \u2192 \u2203D (\u03bb y \u2192 P\u00b2 x y)) \u2194 \u2203D (\u03bb y \u2192 \u2203D (\u03bb x \u2192 P\u00b2 x y))\n{-# ATP prove ord\u2081 #-}\n{-# ATP prove ord\u2082 #-}\n\n-- Quantification over a variable that does not occur can be delete.\npostulate\n erase\u2081 : \u2200D (\u03bb _ \u2192 P\u2070) \u2194 P\u2070\n erase\u2082 : \u2203D (\u03bb _ \u2192 P\u2070) \u2194 P\u2070\n{-# ATP prove erase\u2081 #-}\n{-# ATP prove erase\u2082 #-}\n\n-- Distributes laws for the quantifiers.\npostulate\n dist\u2081 : \u2200D (\u03bb x \u2192 P\u00b9 x \u2227 Q\u00b9 x) \u2194 (\u2200D P\u00b9 \u2227 \u2200D Q\u00b9)\n dist\u2082 : \u2203D (\u03bb x \u2192 P\u00b9 x \u2228 Q\u00b9 x) \u2194 (\u2203D P\u00b9 \u2228 \u2203D Q\u00b9)\n{-# ATP prove dist\u2081 #-}\n{-# ATP prove dist\u2082 #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Predicate logic examples\n------------------------------------------------------------------------------\n\n-- This module contains some examples showing the use of the ATPs to\n-- prove theorems from predicate logic.\n\nmodule Examples.Logic.Predicate where\n\nopen import Examples.Logic.Constants\n\n------------------------------------------------------------------------------\n-- We postulate some predicate symbols.\npostulate\n P\u2070 : Set\n P\u00b9 Q\u00b9 : D \u2192 Set\n P\u00b2 : D \u2192 D \u2192 Set\n\n-- The introduction and elimination rules for the quantifiers are theorems.\npostulate\n \u2200I : ((x : D) \u2192 P\u00b9 x) \u2192 \u2200D P\u00b9\n \u2200E : \u2200D P\u00b9 \u2192 (t : D) \u2192 P\u00b9 t\n -- This elimination rule cannot prove in Coq because in Coq we can\n -- have empty domains. We do not have this problem because the ATPs\n -- assume a non-empty domain.\n \u2203I : ((t : D) \u2192 P\u00b9 t) \u2192 \u2203D P\u00b9\n -- TODO: \u2203E : (x : D) \u2192 \u2203D P\u00b9 \u2192 (P\u00b9 x \u2192 Q\u00b9 x) \u2192 Q\u00b9 x\n{-# ATP prove \u2200I #-}\n{-# ATP prove \u2200E #-}\n{-# ATP prove \u2203I #-}\n-- {-# ATP prove \u2203E #-}\n\n-- Generalization of De Morgan's laws.\npostulate\n gDM\u2081 : \u00ac (\u2200D P\u00b9) \u2194 \u2203D (\u03bb x \u2192 \u00ac (P\u00b9 x))\n gDM\u2082 : \u00ac (\u2203D P\u00b9) \u2194 \u2200D (\u03bb x \u2192 \u00ac (P\u00b9 x))\n gDM\u2083 : \u2200D P\u00b9 \u2194 \u00ac (\u2203D (\u03bb x \u2192 \u00ac (P\u00b9 x)))\n gDM\u2084 : \u2203D P\u00b9 \u2194 \u00ac (\u2200D (\u03bb x \u2192 \u00ac (P\u00b9 x)))\n{-# ATP prove gDM\u2081 #-}\n{-# ATP prove gDM\u2082 #-}\n{-# ATP prove gDM\u2083 #-}\n{-# ATP prove gDM\u2084 #-}\n\n-- The order of quantifiers of the same sort is irrelevant.\npostulate\n ord\u2081 : \u2200D (\u03bb x \u2192 \u2200D (\u03bb y \u2192 P\u00b2 x y)) \u2194 \u2200D (\u03bb y \u2192 \u2200D (\u03bb x \u2192 P\u00b2 x y))\n ord\u2082 : \u2203D (\u03bb x \u2192 \u2203D (\u03bb y \u2192 P\u00b2 x y)) \u2194 \u2203D (\u03bb y \u2192 \u2203D (\u03bb x \u2192 P\u00b2 x y))\n{-# ATP prove ord\u2081 #-}\n{-# ATP prove ord\u2082 #-}\n\n-- Quantification over a variable that does not occur can be delete.\npostulate\n erase\u2081 : \u2200D (\u03bb _ \u2192 P\u2070) \u2194 P\u2070\n erase\u2082 : \u2203D (\u03bb _ \u2192 P\u2070) \u2194 P\u2070\n{-# ATP prove erase\u2081 #-}\n{-# ATP prove erase\u2082 #-}\n\n-- Distributes laws for the quantifiers.\npostulate\n dist\u2081 : \u2200D (\u03bb x \u2192 P\u00b9 x \u2227 Q\u00b9 x) \u2194 (\u2200D P\u00b9 \u2227 \u2200D Q\u00b9)\n dist\u2082 : \u2203D (\u03bb x \u2192 P\u00b9 x \u2228 Q\u00b9 x) \u2194 (\u2203D P\u00b9 \u2228 \u2203D Q\u00b9)\n{-# ATP prove dist\u2081 #-}\n{-# ATP prove dist\u2082 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"de31830f9c7597f3c590f9fcf6dde952b54dd49a","subject":"Cosmetic changes.","message":"Cosmetic changes.\n\nIgnore-this: 3f66058c406361406bb3a5ee91414be3\n\ndarcs-hash:20110720040502-3bd4e-75c92a13a9ca594309249684d6305b39fd088f13.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Data\/Stream\/PropertiesI.agda","new_file":"src\/FOTC\/Data\/Stream\/PropertiesI.agda","new_contents":"------------------------------------------------------------------------------\n-- Streams properties\n------------------------------------------------------------------------------\n\nmodule FOTC.Data.Stream.PropertiesI where\n\nopen import FOTC.Base\n\nopen import FOTC.Base.PropertiesI\n\nopen import FOTC.Data.Stream.Type\n\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n\ntailS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\ntailS {x} {xs} h = subst Stream (sym (\u2227-proj\u2082 (\u2237-injective x\u2237xs\u2261e\u2237es))) Ses\n where\n unfold : \u2203 \u03bb e \u2192 \u2203 \u03bb es \u2192 x \u2237 xs \u2261 e \u2237 es \u2227 Stream es\n unfold = Stream-gfp\u2081 h\n\n e : D\n e = \u2203-proj\u2081 unfold\n\n es : D\n es = \u2203-proj\u2081 (\u2203-proj\u2082 unfold)\n\n x\u2237xs\u2261e\u2237es : x \u2237 xs \u2261 e \u2237 es\n x\u2237xs\u2261e\u2237es = \u2227-proj\u2081 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold))\n\n Ses : Stream es\n Ses = \u2227-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold))\n\n\u2248\u2192Stream : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Stream xs \u2227 Stream ys\n\u2248\u2192Stream {xs} {ys} xs\u2248ys = Stream-gfp\u2082 P\u2081 helper\u2081 (ys , xs\u2248ys)\n , Stream-gfp\u2082 P\u2082 helper\u2082 (xs , xs\u2248ys)\n where\n P\u2081 : D \u2192 Set\n P\u2081 ws = \u2203 \u03bb zs \u2192 ws \u2248 zs\n\n helper\u2081 : \u2200 {ws} \u2192 P\u2081 ws \u2192\n \u2203 (\u03bb w' \u2192 \u2203 (\u03bb ws' \u2192 ws \u2261 w' \u2237 ws' \u2227 P\u2081 ws'))\n helper\u2081 {ws} (zs , ws\u2248zs) = w' , ws' , ws\u2261w'\u2237ws' , (zs' , ws'\u2248zs')\n where\n unfold-\u2248 : \u2203 \u03bb w' \u2192 \u2203 \u03bb ws' \u2192 \u2203 \u03bb zs' \u2192\n ws' \u2248 zs' \u2227 ws \u2261 w' \u2237 ws' \u2227 zs \u2261 w' \u2237 zs'\n\n unfold-\u2248 = \u2248-gfp\u2081 ws\u2248zs\n\n w' : D\n w' = \u2203-proj\u2081 unfold-\u2248\n\n ws' : D\n ws' = \u2203-proj\u2081 (\u2203-proj\u2082 unfold-\u2248)\n\n zs' : D\n zs' = \u2203-proj\u2081 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold-\u2248))\n\n ws'\u2248zs' : ws' \u2248 zs'\n ws'\u2248zs' = \u2227-proj\u2081 (\u2203-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold-\u2248)))\n\n ws\u2261w'\u2237ws' : ws \u2261 w' \u2237 ws'\n ws\u2261w'\u2237ws' = \u2227-proj\u2081 (\u2227-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold-\u2248))))\n\n P\u2082 : D \u2192 Set\n P\u2082 zs = \u2203 \u03bb ws \u2192 ws \u2248 zs\n\n helper\u2082 : \u2200 {zs} \u2192 P\u2082 zs \u2192 \u2203 (\u03bb z' \u2192 \u2203 (\u03bb zs' \u2192 zs \u2261 z' \u2237 zs' \u2227 P\u2082 zs'))\n helper\u2082 {zs} (ws , ws\u2248zs) = w' , zs' , zs\u2261w'\u2237zs' , ws' , ws'\u2248zs'\n where\n unfold-\u2248 : \u2203 \u03bb w' \u2192 \u2203 \u03bb ws' \u2192 \u2203 \u03bb zs' \u2192\n ws' \u2248 zs' \u2227 ws \u2261 w' \u2237 ws' \u2227 zs \u2261 w' \u2237 zs'\n\n unfold-\u2248 = \u2248-gfp\u2081 ws\u2248zs\n\n w' : D\n w' = \u2203-proj\u2081 unfold-\u2248\n\n ws' : D\n ws' = \u2203-proj\u2081 (\u2203-proj\u2082 unfold-\u2248)\n\n zs' : D\n zs' = \u2203-proj\u2081 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold-\u2248))\n\n ws'\u2248zs' : ws' \u2248 zs'\n ws'\u2248zs' = \u2227-proj\u2081 (\u2203-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold-\u2248)))\n\n zs\u2261w'\u2237zs' : zs \u2261 w' \u2237 zs'\n zs\u2261w'\u2237zs' = \u2227-proj\u2082 (\u2227-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold-\u2248))))\n","old_contents":"------------------------------------------------------------------------------\n-- Streams properties\n------------------------------------------------------------------------------\n\nmodule FOTC.Data.Stream.PropertiesI where\n\nopen import FOTC.Base\n\nopen import FOTC.Base.PropertiesI\n\nopen import FOTC.Data.Stream.Type\n\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n\ntailS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\ntailS {x} {xs} h = subst Stream (sym (\u2227-proj\u2082 (\u2237-injective x\u2237xs\u2261e\u2237es))) Ses\n where\n unfold : \u2203 \u03bb e \u2192 \u2203 \u03bb es \u2192 x \u2237 xs \u2261 e \u2237 es \u2227 Stream es\n unfold = Stream-gfp\u2081 h\n\n e : D\n e = \u2203-proj\u2081 unfold\n\n es : D\n es = \u2203-proj\u2081 (\u2203-proj\u2082 unfold)\n\n x\u2237xs\u2261e\u2237es : x \u2237 xs \u2261 e \u2237 es\n x\u2237xs\u2261e\u2237es = \u2227-proj\u2081 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold))\n\n Ses : Stream es\n Ses = \u2227-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold))\n\n\u2248\u2192Stream : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Stream xs \u2227 Stream ys\n\u2248\u2192Stream {xs} {ys} xs\u2248ys = Stream-gfp\u2082 P\u2081 helper\u2081 (ys , xs\u2248ys)\n , Stream-gfp\u2082 P\u2082 helper\u2082 (xs , xs\u2248ys)\n where\n P\u2081 : D \u2192 Set\n P\u2081 ws = \u2203 \u03bb zs \u2192 ws \u2248 zs\n\n helper\u2081 : \u2200 {ws} \u2192 P\u2081 ws \u2192\n \u2203 (\u03bb w' \u2192 \u2203 (\u03bb ws' \u2192 ws \u2261 w' \u2237 ws' \u2227 P\u2081 ws'))\n helper\u2081 {ws} (zs , ws\u2248zs) = w' , ws' , ws\u2261w'\u2237ws' , (zs' , ws'\u2248zs')\n where\n unfold-\u2248 : \u2203 \u03bb w' \u2192 \u2203 \u03bb ws' \u2192 \u2203 \u03bb zs' \u2192\n ws' \u2248 zs' \u2227 ws \u2261 w' \u2237 ws' \u2227 zs \u2261 w' \u2237 zs'\n\n unfold-\u2248 = \u2248-gfp\u2081 ws\u2248zs\n\n w' : D\n w' = \u2203-proj\u2081 unfold-\u2248\n\n ws' : D\n ws' = \u2203-proj\u2081 (\u2203-proj\u2082 unfold-\u2248)\n\n zs' : D\n zs' = \u2203-proj\u2081 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold-\u2248))\n\n ws'\u2248zs' : ws' \u2248 zs'\n ws'\u2248zs' = \u2227-proj\u2081 (\u2203-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold-\u2248)))\n\n ws\u2261w'\u2237ws' : ws \u2261 w' \u2237 ws'\n ws\u2261w'\u2237ws' = \u2227-proj\u2081 (\u2227-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold-\u2248))))\n\n P\u2082 : D \u2192 Set\n P\u2082 zs = \u2203 \u03bb ws \u2192 ws \u2248 zs\n\n helper\u2082 : \u2200 {zs} \u2192 P\u2082 zs \u2192 \u2203 (\u03bb z' \u2192 \u2203 (\u03bb zs' \u2192 zs \u2261 z' \u2237 zs' \u2227 P\u2082 zs'))\n helper\u2082 {zs} (ws , ws\u2248zs) = w' , zs' , zs\u2261w'\u2237zs' , ws' , ws'\u2248zs'\n where\n unfold-\u2248 : \u2203 \u03bb w' \u2192 \u2203 \u03bb ws' \u2192 \u2203 \u03bb zs' \u2192\n ws' \u2248 zs' \u2227 ws \u2261 w' \u2237 ws' \u2227 zs \u2261 w' \u2237 zs'\n\n unfold-\u2248 = \u2248-gfp\u2081 ws\u2248zs\n\n w' : D\n w' = \u2203-proj\u2081 unfold-\u2248\n\n ws' : D\n ws' = \u2203-proj\u2081 (\u2203-proj\u2082 unfold-\u2248)\n\n zs' : D\n zs' = \u2203-proj\u2081 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold-\u2248))\n\n ws'\u2248zs' : ws' \u2248 zs'\n ws'\u2248zs' = \u2227-proj\u2081 (\u2203-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold-\u2248)))\n\n zs\u2261w'\u2237zs' : zs \u2261 w' \u2237 zs'\n zs\u2261w'\u2237zs' = \u2227-proj\u2082 (\u2227-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 (\u2203-proj\u2082 unfold-\u2248))))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7540b79d7380f6d1c7ca7db983d990a2aab84b00","subject":"refactor grouphomomorphism","message":"refactor grouphomomorphism\n","repos":"crypto-agda\/explore","old_file":"lib\/Explore\/GroupHomomorphism.agda","new_file":"lib\/Explore\/GroupHomomorphism.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule Explore.GroupHomomorphism where\n\nopen import Level\nopen import Algebra.FunctionProperties\nopen import Data.Product\nopen import Function using (_\u2218_ ; flip)\nopen import Function.Inverse as Inv using (_\u2194_; module Inverse)\nopen import Relation.Binary.PropositionalEquality.NP hiding (_\u2219_)\n\nopen import Explore.Core\nopen import Explore.Properties\nopen import Explore.Sum\n\n{-\n I had some problems with using the standard library definiton of Groups\n so I rolled my own, therefor I need some boring proofs first\n\n-}\nrecord Group (G : Set) : Set where\n field\n _\u2219_ : G \u2192 G \u2192 G\n \u03b5 : G\n -_ : G \u2192 G\n\n -- laws\n field\n assoc : Associative _\u2261_ _\u2219_\n identity : Identity _\u2261_ \u03b5 _\u2219_\n inverse : Inverse _\u2261_ \u03b5 -_ _\u2219_\n\n -- derived property\n help : \u2200 x y \u2192 x \u2261 (x \u2219 y) \u2219 - y\n help x y = x\n \u2261\u27e8 sym (proj\u2082 identity x) \u27e9\n x \u2219 \u03b5\n \u2261\u27e8 cong (_\u2219_ x) (sym (proj\u2082 inverse y)) \u27e9\n x \u2219 (y \u2219 - y)\n \u2261\u27e8 sym (assoc x y (- y)) \u27e9\n (x \u2219 y) \u2219 (- y)\n \u220e\n where open \u2261-Reasoning\n\n unique-1g : \u2200 x y \u2192 x \u2219 y \u2261 y \u2192 x \u2261 \u03b5\n unique-1g x y eq = x\n \u2261\u27e8 help x y \u27e9\n (x \u2219 y) \u2219 - y\n \u2261\u27e8 cong (flip _\u2219_ (- y)) eq \u27e9\n y \u2219 - y\n \u2261\u27e8 proj\u2082 inverse y \u27e9\n \u03b5\n \u220e\n where open \u2261-Reasoning\n\n unique-\/ : \u2200 x y \u2192 x \u2219 y \u2261 \u03b5 \u2192 x \u2261 - y\n unique-\/ x y eq = x\n \u2261\u27e8 help x y \u27e9\n (x \u2219 y) \u2219 - y\n \u2261\u27e8 cong (flip _\u2219_ (- y)) eq \u27e9\n \u03b5 \u2219 - y\n \u2261\u27e8 proj\u2081 identity (- y) \u27e9\n - y\n \u220e\n where open \u2261-Reasoning\n\nmodule _ {A B : Set}(GA : Group A)(GB : Group B) where\n open Group GA using (-_) renaming (_\u2219_ to _+_; \u03b5 to 0g)\n open Group GB using (unique-1g ; unique-\/) renaming (_\u2219_ to _*_; \u03b5 to 1g; -_ to 1\/_)\n\n GroupHomomorphism : (A \u2192 B) \u2192 Set\n GroupHomomorphism f = \u2200 x y \u2192 f (x + y) \u2261 f x * f y\n\n module GroupHomomorphismProp {f}(f-homo : GroupHomomorphism f) where\n f-pres-\u03b5 : f 0g \u2261 1g\n f-pres-\u03b5 = unique-1g (f 0g) (f 0g) part\n where open \u2261-Reasoning\n open Group GA using (identity)\n part = f 0g * f 0g\n \u2261\u27e8 sym (f-homo 0g 0g) \u27e9\n f (0g + 0g)\n \u2261\u27e8 cong f (proj\u2081 identity 0g) \u27e9\n f 0g\n \u220e\n\n f-pres-inv : \u2200 x \u2192 f (- x) \u2261 1\/ f x\n f-pres-inv x = unique-\/ (f (- x)) (f x) part\n where open \u2261-Reasoning\n open Group GA using (inverse)\n part = f (- x) * f x\n \u2261\u27e8 sym (f-homo (- x) x) \u27e9\n f (- x + x)\n \u2261\u27e8 cong f (proj\u2081 inverse x) \u27e9\n f 0g\n \u2261\u27e8 f-pres-\u03b5 \u27e9\n 1g\n \u220e\n\nmodule _ {A B}(GA : Group A)(GB : Group B)\n (f : A \u2192 B)\n (exploreA : Explore zero A)(f-homo : GroupHomomorphism GA GB f)\n ([f] : B \u2192 A)(f-sur : \u2200 b \u2192 f ([f] b) \u2261 b)\n (explore-ext : ExploreExt exploreA)\n where\n open Group GA using (-_) renaming (_\u2219_ to _+_ ; \u03b5 to 0g)\n open Group GB using () renaming (_\u2219_ to _*_ ; \u03b5 to 1g ; -_ to 1\/_)\n open GroupHomomorphismProp GA GB f-homo\n\n {- How all this is related to elgamal\n\n the Group GA is \u2124q with modular addition as operation\n the Group GB is the cyclic group with order q\n\n f is g^, the proof only need that it is a group homomorphism\n and that it has a right inverse\n\n we require that the explore (for type A) function (should work with only summation)\n is Stable under addition of GA (notice that we have flip in there that is so that\n we don't need commutativity\n\n finally we require that the explore function respects extensionality\n -}\n\n {-\n While this proof looks complicated it basically just adds inverse of m\u2080 and then adds m\u2081 (from image of f)\n we need the homomorphic property to pull out the values.\n\n -}\n\n module _ {X}(z : X)(op : X \u2192 X \u2192 X)\n (sui : \u2200 k \u2192 StableUnder' exploreA z op (flip (Group._\u2219_ GA) k))\n where\n -- this proof isn't actually any hard..\n thm : \u2200 (O : B \u2192 X) m\u2080 m\u2081 \u2192 exploreA z op (\u03bb x \u2192 O (f x * m\u2080)) \u2261 exploreA z op (\u03bb x \u2192 O (f x * m\u2081))\n thm O m\u2080 m\u2081 = explore (\u03bb x \u2192 O (f x * m\u2080))\n \u2261\u27e8 sui (- [f] m\u2080) (\u03bb x \u2192 O (f x * m\u2080)) \u27e9\n explore (\u03bb x \u2192 O (f (x + - [f] m\u2080) * m\u2080))\n \u2261\u27e8 explore-ext z op (\u03bb x \u2192 cong O (lemma1 x)) \u27e9\n explore (\u03bb x \u2192 O (f x ))\n \u2261\u27e8 sui ([f] m\u2081) (O \u2218 f) \u27e9\n explore (\u03bb x \u2192 O (f (x + [f] m\u2081)))\n \u2261\u27e8 explore-ext z op (\u03bb x \u2192 cong O (lemma2 x)) \u27e9\n explore (\u03bb x \u2192 O (f x * m\u2081))\n \u220e\n where\n open \u2261-Reasoning\n explore = exploreA z op\n\n lemma1 : \u2200 x \u2192 f (x + - [f] m\u2080) * m\u2080 \u2261 f x\n lemma1 x rewrite f-homo x (- [f] m\u2080)\n | f-pres-inv ([f] m\u2080)\n | f-sur m\u2080\n | Group.assoc GB (f x) (1\/ m\u2080) m\u2080\n | proj\u2081 (Group.inverse GB) m\u2080\n | proj\u2082 (Group.identity GB) (f x) = refl\n\n lemma2 : \u2200 x \u2192 f (x + [f] m\u2081) \u2261 f x * m\u2081\n lemma2 x rewrite f-homo x ([f] m\u2081)\n | f-sur m\u2081 = refl\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule Explore.GroupHomomorphism where\n\nopen import Level\nopen import Algebra.FunctionProperties\nopen import Data.Product\nopen import Function using (_\u2218_ ; flip)\nopen import Function.Inverse as Inv using (_\u2194_; module Inverse)\nopen import Relation.Binary.PropositionalEquality.NP hiding (_\u2219_)\n\nopen import Explore.Core\nopen import Explore.Properties\nopen import Explore.Sum\n\nrecord Group (G : Set) : Set where\n field\n _\u2219_ : G \u2192 G \u2192 G\n \u03b5 : G\n -_ : G \u2192 G\n\n -- laws\n field\n assoc : Associative _\u2261_ _\u2219_\n identity : Identity _\u2261_ \u03b5 _\u2219_\n inverse : Inverse _\u2261_ \u03b5 -_ _\u2219_\n\n\nGroupHomomorphism : \u2200 {A B : Set} \u2192 Group A \u2192 Group B \u2192(A \u2192 B) \u2192 Set\nGroupHomomorphism GA GB f = \u2200 x y \u2192 f (x + y) \u2261 f x * f y\n where\n open Group GA renaming (_\u2219_ to _+_)\n open Group GB renaming (_\u2219_ to _*_)\n\nmodule _ {A B}(GA : Group A)(GB : Group B)\n (f : A \u2192 B)\n (exploreA : Explore zero A)(f-homo : GroupHomomorphism GA GB f)\n ([f] : B \u2192 A)(f-sur : \u2200 b \u2192 f ([f] b) \u2261 b)\n (explore-ext : ExploreExt exploreA)\n where\n open Group GA using (-_) renaming (_\u2219_ to _+_ ; \u03b5 to 0g)\n open Group GB using () renaming (_\u2219_ to _*_ ; \u03b5 to 1g ; -_ to 1\/_)\n\n {- How all this is related to elgamal\n\n the Group GA is \u2124q with modular addition as operation\n the Group GB is the cyclic group with order q\n\n f is g^, the proof only need that it is a group homomorphism\n and that it has a right inverse\n\n we require that the explore (for type A) function (should work with only summation)\n is Stable under addition of GA (notice that we have flip in there that is so that\n we don't need commutativity\n\n finally we require that the explore function respects extensionality\n -}\n\n {-\n I had some problems with using the standard library definiton of Groups\n so I rolled my own, therefor I need some boring proofs first\n\n -}\n\n help : \u2200 x y \u2192 x \u2261 (x * y) * 1\/ y\n help x y = x\n \u2261\u27e8 sym (proj\u2082 identity x) \u27e9\n x * 1g\n \u2261\u27e8 cong (_*_ x) (sym (proj\u2082 inverse y)) \u27e9\n x * (y * 1\/ y)\n \u2261\u27e8 sym (assoc x y (1\/ y)) \u27e9\n (x * y) * (1\/ y)\n \u220e\n where open \u2261-Reasoning\n open Group GB\n\n unique-1g : \u2200 x y \u2192 x * y \u2261 y \u2192 x \u2261 1g\n unique-1g x y eq = x\n \u2261\u27e8 help x y \u27e9\n (x * y) * 1\/ y\n \u2261\u27e8 cong (flip _*_ (1\/ y)) eq \u27e9\n y * 1\/ y\n \u2261\u27e8 proj\u2082 inverse y \u27e9\n 1g\n \u220e\n where open \u2261-Reasoning\n open Group GB\n\n unique-\/ : \u2200 x y \u2192 x * y \u2261 1g \u2192 x \u2261 1\/ y\n unique-\/ x y eq = x\n \u2261\u27e8 help x y \u27e9\n (x * y) * 1\/ y\n \u2261\u27e8 cong (flip _*_ (1\/ y)) eq \u27e9\n 1g * 1\/ y\n \u2261\u27e8 proj\u2081 identity (1\/ y) \u27e9\n 1\/ y\n \u220e\n where open \u2261-Reasoning\n open Group GB\n\n f-pres-\u03b5 : f 0g \u2261 1g\n f-pres-\u03b5 = unique-1g (f 0g) (f 0g) part\n where open \u2261-Reasoning\n open Group GA\n part = f 0g * f 0g\n \u2261\u27e8 sym (f-homo 0g 0g) \u27e9\n f (0g + 0g)\n \u2261\u27e8 cong f (proj\u2081 identity 0g) \u27e9\n f 0g\n \u220e\n\n f-pres-inv : \u2200 x \u2192 f (- x) \u2261 1\/ f x\n f-pres-inv x = unique-\/ (f (- x)) (f x) part\n where open \u2261-Reasoning\n open Group GA hiding (-_)\n part = f (- x) * f x\n \u2261\u27e8 sym (f-homo (- x) x) \u27e9\n f (- x + x)\n \u2261\u27e8 cong f (proj\u2081 inverse x) \u27e9\n f 0g\n \u2261\u27e8 f-pres-\u03b5 \u27e9\n 1g\n \u220e\n {-\n While this proof looks complicated it basically just adds inverse of m\u2080 and then adds m\u2081 (from image of f)\n we need the homomorphic property to pull out the values.\n\n -}\n\n module _ {X}(z : X)(op : X \u2192 X \u2192 X)\n (sui : \u2200 k \u2192 StableUnder' exploreA z op (flip (Group._\u2219_ GA) k))\n where\n -- this proof isn't actually any hard..\n thm : \u2200 (O : B \u2192 X) m\u2080 m\u2081 \u2192 exploreA z op (\u03bb x \u2192 O (f x * m\u2080)) \u2261 exploreA z op (\u03bb x \u2192 O (f x * m\u2081))\n thm O m\u2080 m\u2081 = explore (\u03bb x \u2192 O (f x * m\u2080))\n \u2261\u27e8 sui (- [f] m\u2080) (\u03bb x \u2192 O (f x * m\u2080)) \u27e9\n explore (\u03bb x \u2192 O (f (x + - [f] m\u2080) * m\u2080))\n \u2261\u27e8 explore-ext z op (\u03bb x \u2192 cong O (lemma1 x)) \u27e9\n explore (\u03bb x \u2192 O (f x ))\n \u2261\u27e8 sui ([f] m\u2081) (O \u2218 f) \u27e9\n explore (\u03bb x \u2192 O (f (x + [f] m\u2081)))\n \u2261\u27e8 explore-ext z op (\u03bb x \u2192 cong O (lemma2 x)) \u27e9\n explore (\u03bb x \u2192 O (f x * m\u2081))\n \u220e\n where\n open \u2261-Reasoning\n explore = exploreA z op\n\n lemma1 : \u2200 x \u2192 f (x + - [f] m\u2080) * m\u2080 \u2261 f x\n lemma1 x rewrite f-homo x (- [f] m\u2080)\n | f-pres-inv ([f] m\u2080)\n | f-sur m\u2080\n | Group.assoc GB (f x) (1\/ m\u2080) m\u2080\n | proj\u2081 (Group.inverse GB) m\u2080\n | proj\u2082 (Group.identity GB) (f x) = refl\n\n lemma2 : \u2200 x \u2192 f (x + [f] m\u2081) \u2261 f x * m\u2081\n lemma2 x rewrite f-homo x ([f] m\u2081)\n | f-sur m\u2081 = refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"0a11660ef086b316b40c304a2804311fade09a32","subject":"Minor changes.","message":"Minor changes.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/FOT\/FOTC\/IJK.agda","new_file":"notes\/FOT\/FOTC\/IJK.agda","new_contents":"------------------------------------------------------------------------------\n-- Conversion functions i, j, and k.\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.IJK where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat.Type\n\n------------------------------------------------------------------------------\n\ndata \u2115 : Set where\n z : \u2115\n s : \u2115 \u2192 \u2115\n\n-- Conversion functions from\/to \u2115 and N.\ni : \u2115 \u2192 D\ni z = zero\ni (s n) = succ\u2081 (i n)\n\nj : (n : \u2115) \u2192 N (i n)\nj z = nzero\nj (s n) = nsucc (j n)\n\nk : {n : D} \u2192 N n \u2192 \u2115\nk nzero = z\nk (nsucc Nn) = s (k Nn)\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nz + n = n\ns m + n = s (m + n)\n","old_contents":"------------------------------------------------------------------------------\n-- Conversion functions i, j, and k.\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.IJK where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat.Type\n\n------------------------------------------------------------------------------\n\n-- The inductive natural numbers.\ndata \u2115 : Set where\n z : \u2115\n s : \u2115 \u2192 \u2115\n\n-- Conversion functions\ni : \u2115 \u2192 D\ni z = zero\ni (s n) = succ\u2081 (i n)\n\nj : (n : \u2115) \u2192 N (i n)\nj z = nzero\nj (s n) = nsucc (j n)\n\nk : {n : D} \u2192 N n \u2192 \u2115\nk nzero = z\nk (nsucc Nn) = s (k Nn)\n\n-- Addition for \u2115\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nz + n = n\ns m + n = s (m + n)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7fde932b68b4d631c9007721d6ea856d68865033","subject":"ExplicitNil: extend correctness of optimized derivation to support bag union and difference","message":"ExplicitNil: extend correctness of optimized derivation\nto support bag union and difference\n\nOld-commit-hash: 40f74c6acb6b271d0654bbc62228aba5fbfc8edf\n","repos":"inc-lc\/ilc-agda","old_file":"experimental\/ExplicitNil.agda","new_file":"experimental\/ExplicitNil.agda","new_contents":"{-\nCommunicate to derivatives that changes to certain arguments\nare always nil (i. e., certain arguments are stable).\n-}\n\nmodule ExplicitNil where\n\nopen import TaggedDeltaTypes\n\nopen import Data.Nat\nopen import Data.Bool\nopen import Data.Sum hiding (map)\nopen import Data.Product hiding (map)\nopen import Data.Unit using (\u22a4 ; tt)\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\nopen import Relation.Binary.Core using (Decidable)\nopen import Relation.Nullary.Core using (yes ; no)\n\next\u00b3 : \u2200\n {A : Set}\n {B : A \u2192 Set}\n {C : (a : A) \u2192 B a \u2192 Set }\n {D : (a : A) \u2192 (b : B a) \u2192 C a b \u2192 Set}\n {f g : (a : A) \u2192 (b : B a) \u2192 (c : C a b) \u2192 D a b c} \u2192\n ((a : A) (b : B a) (c : C a b) \u2192 f a b c \u2261 g a b c) \u2192 f \u2261 g\n\next\u00b3 fabc=gabc = ext (\u03bb a \u2192 ext (\u03bb b \u2192 ext (\u03bb c \u2192 fabc=gabc a b c)))\n where ext = extensionality\n\nproj-H : \u2200 {\u0393 : Context} {\u03c1 : \u0394Env \u0393} {us vs} \u2192\n Honest \u03c1 (FV-union us vs) \u2192 Honest \u03c1 us \u00d7 Honest \u03c1 vs\nproj-H {\u2205} {us = \u2205} {vs = \u2205} clearly = clearly , clearly\nproj-H {us = alter us} {alter vs} (alter H) =\n let uss , vss = proj-H H in alter uss , alter vss\nproj-H {us = alter us} {abide vs} (abide eq H) =\n let uss , vss = proj-H H in\n alter uss , abide eq vss\nproj-H {us = abide us} {alter vs} (abide eq H) =\n let uss , vss = proj-H H in\n abide eq uss , alter vss\nproj-H {us = abide us} {abide vs} (abide eq H) =\n let uss , vss = proj-H H in\n abide eq uss , abide eq vss\n\n-- Equivalence proofs are unique\nequivalence-unique : \u2200 {A : Set} {a b : A} \u2192 \u2200 {p q : a \u2261 b} \u2192 p \u2261 q\nequivalence-unique {p = refl} {refl} = refl\n\n-- Product of singletons are singletons (for example)\nproduct-unique : \u2200 {A : Set} {B : A \u2192 Set} \u2192\n (\u2200 {a b : A} \u2192 a \u2261 b) \u2192\n (\u2200 {a : A} {c d : B a} \u2192 c \u2261 d) \u2192\n (\u2200 {p q : \u03a3 A B} \u2192 p \u2261 q)\nproduct-unique {A} {B} lhs rhs {a , c} {b , d}\n rewrite lhs {a} {b} = cong (_,_ b) rhs\n\n-- Validity proofs are (extensionally) unique (as functions)\nvalidity-unique : \u2200 {\u03c4} {v : \u27e6 \u03c4 \u27e7} {dv : \u0394Val \u03c4} \u2192\n \u2200 {p q : valid v dv} \u2192 p \u2261 q\nvalidity-unique {nats} = equivalence-unique\nvalidity-unique {bags} = refl\nvalidity-unique {\u03c3 \u21d2 \u03c4} = ext\u00b3 (\u03bb v dv R[v,dv] \u2192\n product-unique validity-unique equivalence-unique)\n\nstabilityVar : \u2200 {\u03c4 \u0393} {x : Var \u0393 \u03c4} \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} (H : Honest \u03c1 (select-just x)) \u2192\n \u27e6 x \u27e7 (ignore \u03c1) \u2295 \u27e6 x \u27e7\u0394Var \u03c1 \u2261 \u27e6 x \u27e7 (ignore \u03c1)\n\nstabilityVar {x = this} (abide proof _) = proof\nstabilityVar {x = that y} (alter H) = stabilityVar {x = y} H\n\nstability : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} (H : Honest \u03c1 (FV t)) \u2192\n \u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n \u2261 \u27e6 t \u27e7 (ignore \u03c1)\n\n-- Boilerplate begins\nstabilityAbs : \u2200 {\u03c3 \u03c4 \u0393} {t : Term (\u03c3 \u2022 \u0393) \u03c4} \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} (H : Honest \u03c1 (FV (abs t))) \u2192\n (v : \u27e6 \u03c3 \u27e7) \u2192\n \u27e6 t \u27e7 (v \u2022 ignore \u03c1) \u2295\n \u27e6 derive t \u27e7\u0394 (cons v (v \u229d v) R[v,u\u229dv] \u03c1) (unrestricted t)\n \u2261 \u27e6 t \u27e7 (v \u2022 ignore \u03c1)\nstabilityAbs {t = t} {\u03c1} H v with FV t | inspect FV t\n... | abide vars | [ case0 ] = stability {t = t} Ht\n where\n Ht : Honest (cons v (v \u229d v) R[v,u\u229dv] \u03c1) (FV t)\n Ht rewrite case0 = abide v\u2295[u\u229dv]=u H\n... | alter vars | [ case1 ] = stability {t = t} Ht\n where\n Ht : Honest (cons v (v \u229d v) R[v,u\u229dv] \u03c1) (FV t)\n Ht rewrite case1 = alter H\n-- Boilerplate ends\n\nstability {t = nat n} H = refl\nstability {t = bag b} H = b++\u2205=b\nstability {t = union s t} {\u03c1} H =\n let\n a = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n da = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n db = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n Hs , Ht = proj-H H\n in\n begin\n (a ++ b) ++ (da ++ db)\n \u2261\u27e8 [a++b]++[c++d]=[a++c]++[b++d] \u27e9\n (a ++ da) ++ (b ++ db)\n \u2261\u27e8 cong\u2082 _++_ (stability {t = s} Hs) (stability {t = t} Ht) \u27e9\n a ++ b\n \u220e where open \u2261-Reasoning\nstability {t = diff s t} {\u03c1} H =\n let\n a = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n da = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n db = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n Hs , Ht = proj-H H\n in\n begin\n (a \\\\ b) ++ (da \\\\ db)\n \u2261\u27e8 [a\\\\b]++[c\\\\d]=[a++c]\\\\[b++d] \u27e9\n (a ++ da) \\\\ (b ++ db)\n \u2261\u27e8 cong\u2082 _\\\\_ (stability {t = s} Hs) (stability {t = t} Ht) \u27e9\n a \\\\ b\n \u220e where open \u2261-Reasoning\nstability {t = var x} H = stabilityVar H\nstability {t = abs t} {\u03c1} H = extensionality (stabilityAbs {t = t} H)\nstability {t = app s t} {\u03c1} H =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n v = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n dv = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n Hs , Ht = proj-H H\n in\n begin\n f v \u2295 df v dv (validity {t = t})\n \u2261\u27e8 sym (corollary s t) \u27e9\n (f \u2295 df) (v \u2295 dv)\n \u2261\u27e8 stability {t = s} Hs \u27e8$\u27e9 stability {t = t} Ht \u27e9\n f v\n \u220e where open \u2261-Reasoning\nstability {t = add s t} H =\n let Hs , Ht = proj-H H\n in cong\u2082 _+_ (stability {t = s} Hs) (stability {t = t} Ht)\nstability {t = map s t} {\u03c1} H =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n db = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n Hs , Ht = proj-H H\n map = mapBag\n in\n begin\n map f b \u2295 (map (f \u2295 df) (b \u2295 db) \u229d map f b)\n \u2261\u27e8 b++[d\\\\b]=d \u27e9\n map (f \u2295 df) (b \u2295 db)\n \u2261\u27e8 cong\u2082 map (stability {t = s} Hs) (stability {t = t} Ht) \u27e9\n map f b\n \u220e where open \u2261-Reasoning\n\neq-map : \u2200 {\u0393}\n (s : Term \u0393 (nats \u21d2 nats))\n (t : Term \u0393 bags)\n (\u03c1 : \u0394Env \u0393)\n (H : Honest \u03c1 (FV s)) \u2192\n \u27e6 \u0394map\u2081 s (derive t) \u27e7\u0394 \u03c1 (cons (unrestricted t) H tt tt)\n \u2261 \u27e6 \u0394map\u2080 s (derive s) t (derive t) \u27e7\u0394 \u03c1 (unrestricted (map s t))\n\neq-map s t \u03c1 H =\n let\n ds = derive s\n dt = derive t\n f = \u27e6 s \u27e7 (ignore \u03c1)\n v = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 ds \u27e7\u0394 \u03c1 (unrestricted s)\n dv = \u27e6 dt \u27e7\u0394 \u03c1 (unrestricted t)\n\n eq1 : \u27e6 \u0394map\u2080 s ds t dt \u27e7\u0394 \u03c1 (unrestricted (map s t))\n \u2261 mapBag (f \u2295 df) (v \u2295 dv) \u229d mapBag f v\n eq1 = refl\n\n eq2 : mapBag (f \u2295 df) (v \u2295 dv) \u229d mapBag f v\n \u2261 mapBag f dv\n eq2 = trans\n (cong (\u03bb hole \u2192 hole \u229d mapBag f v) (trans\n (cong (\u03bb hole \u2192 mapBag hole (v \u2295 dv)) (stability {t = s} H))\n map-over-++))\n [b++d]\\\\b=d\n\n eq3 : mapBag f dv\n \u2261 \u27e6 \u0394map\u2081 s (derive t) \u27e7\u0394 \u03c1 (cons (unrestricted t) H tt tt)\n eq3 = refl\n\n in sym (trans eq1 (trans eq2 eq3))\n\n-- Vars test\nnone-selected? : \u2200 {\u0393} \u2192 (vs : Vars \u0393) \u2192 (vs \u2261 select-none) \u228e \u22a4\nnone-selected? \u2205 = inj\u2081 refl\nnone-selected? (abide vs) = inj\u2082 tt\nnone-selected? (alter vs) with none-selected? vs\n... | inj\u2081 vs=\u2205 rewrite vs=\u2205 = inj\u2081 refl\n... | inj\u2082 _ = inj\u2082 tt\n\nclosed? : \u2200 {\u03c4 \u0393} \u2192 (t : Term \u0393 \u03c4) \u2192 (FV t \u2261 select-none) \u228e \u22a4\nclosed? t = none-selected? (FV t)\n\nvacuous-honesty : \u2200 {\u0393} {\u03c1 : \u0394Env \u0393} \u2192 Honest \u03c1 select-none\nvacuous-honesty {\u2205} {\u2205} = clearly\nvacuous-honesty {\u03c4 \u2022 \u0393} {cons _ _ _ \u03c1} = alter (vacuous-honesty {\u03c1 = \u03c1})\n\n-- Immunity of closed terms to dishonest environments\nimmune : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} \u2192\n (FV t \u2261 select-none) \u2192 \u2200 {\u03c1} \u2192 Honest \u03c1 (FV t)\nimmune {t = t} eq rewrite eq = vacuous-honesty\n\n-- Ineffectual first optimization step\nderive1 : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394Term \u0393 \u03c4\nderive1 (map s t) with closed? s\n... | inj\u2081 is-closed = \u0394map\u2081 s (derive t)\n... | inj\u2082 tt = derive (map s t)\nderive1 others = derive others\n\nvalid1 : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) {\u03c1 : \u0394Env \u0393} \u2192 derive1 t is-valid-for \u03c1\nvalid1 (nat n) = tt\nvalid1 (bag b) = tt\nvalid1 (union s t) = cons (unrestricted s) (unrestricted t) tt tt\nvalid1 (diff s t) = cons (unrestricted s) (unrestricted t) tt tt\nvalid1 (var x) = tt\nvalid1 (abs t) = \u03bb _ _ _ \u2192 unrestricted t\nvalid1 (app s t) =\n cons (unrestricted s) (unrestricted t) (validity {t = t}) tt\nvalid1 (add s t) =\n cons (unrestricted s) (unrestricted t) tt tt\nvalid1 (map s t) {\u03c1} with closed? s\n... | inj\u2082 tt = cons (unrestricted s) (unrestricted t) tt tt\n... | inj\u2081 if-closed =\n cons (unrestricted t) (immune {t = s} if-closed) tt tt\n\ncorrect1 : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n \u27e6 derive1 t \u27e7\u0394 \u03c1 (valid1 t) \u2261 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n\ncorrect1 {t = nat n} = refl\ncorrect1 {t = bag b} = refl\ncorrect1 {t = var x} = refl\ncorrect1 {t = abs t} = refl\ncorrect1 {t = app s t} = refl\ncorrect1 {t = union s t} = refl\ncorrect1 {t = diff s t} = refl\ncorrect1 {t = add s t} = refl\ncorrect1 {t = map s t} {\u03c1} with closed? s\n... | inj\u2082 tt = refl\n... | inj\u2081 if-closed = eq-map s t \u03c1 (immune {t = s} if-closed)\n\n-- derive2 = derive1 + congruence\nderive2 : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394Term \u0393 \u03c4\nderive2 (abs t) = \u0394abs (derive2 t)\nderive2 (app s t) = \u0394app (derive2 s) t (derive2 t)\nderive2 (add s t) = \u0394add (derive2 s) (derive2 t)\nderive2 (union s t) = \u0394union (derive2 s) (derive2 t)\nderive2 (diff s t) = \u0394diff (derive2 s) (derive2 t)\nderive2 (map s t) with closed? s\n... | inj\u2081 is-closed = \u0394map\u2081 s (derive2 t)\n... | inj\u2082 tt = \u0394map\u2080 s (derive2 s) t (derive2 t)\nderive2 others = derive others\n\nvalid2 : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) {\u03c1 : \u0394Env \u0393} \u2192 derive2 t is-valid-for \u03c1\ncorrect2 : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n \u27e6 derive2 t \u27e7\u0394 \u03c1 (valid2 t) \u2261 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n\nvalid2 (nat n) = tt\nvalid2 (bag b) = tt\nvalid2 (var x) = tt\nvalid2 (abs t) = \u03bb _ _ _ \u2192 valid2 t\nvalid2 (app s t) {\u03c1} = cons (valid2 s) (valid2 t) V tt\n where\n V : valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive2 t \u27e7\u0394 \u03c1 (valid2 t))\n V rewrite correct2 {t = t} {\u03c1} = validity {t = t} {\u03c1}\nvalid2 (add s t) = cons (valid2 s) (valid2 t) tt tt\nvalid2 (union s t) = cons (valid2 s) (valid2 t) tt tt\nvalid2 (diff s t) = cons (valid2 s) (valid2 t) tt tt\nvalid2 (map s t) {\u03c1} with closed? s\n... | inj\u2082 tt = cons (valid2 s) (valid2 t) tt tt\n... | inj\u2081 if-closed =\n cons (valid2 t) (immune {t = s} if-closed) tt tt\n\ncorrect2 {t = nat n} = refl\ncorrect2 {t = bag b} = refl\ncorrect2 {t = var x} = refl\ncorrect2 {t = abs t} = ext\u00b3 (\u03bb _ _ _ \u2192 correct2 {t = t})\ncorrect2 {t = app {\u03c3} {\u03c4} s t} {\u03c1} = \u2245-to-\u2261 eq-all where\n open import Relation.Binary.HeterogeneousEquality hiding (cong\u2082)\n import Relation.Binary.HeterogeneousEquality as HET\n df0 : \u0394Val (\u03c3 \u21d2 \u03c4)\n df0 = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n df2 : \u0394Val (\u03c3 \u21d2 \u03c4)\n df2 = \u27e6 derive2 s \u27e7\u0394 \u03c1 (valid2 s)\n dv0 : \u0394Val \u03c3\n dv0 = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n dv2 : \u0394Val \u03c3\n dv2 = \u27e6 derive2 t \u27e7\u0394 \u03c1 (valid2 t)\n v : \u27e6 \u03c3 \u27e7\n v = \u27e6 t \u27e7 (ignore \u03c1)\n eq-df : df2 \u2261 df0\n eq-df = correct2 {t = s}\n eq-dv : dv2 \u2261 dv0\n eq-dv = correct2 {t = t}\n eq-all : df2 v dv2 (caddr (valid2 (app s t) {\u03c1}))\n \u2245 df0 v dv0 (validity {t = t} {\u03c1})\n eq-all rewrite eq-dv = HET.cong\n (\u03bb hole \u2192 hole v dv0 (validity {t = t} {\u03c1})) (\u2261-to-\u2245 eq-df)\n -- found without intuition by trial-and-error\ncorrect2 {t = add s t} {\u03c1} =\n let\n cs = correct2 {t = s} {\u03c1}\n ct = correct2 {t = t} {\u03c1}\n in cong\u2082 _,_ (cong\u2082 _+_ (cong proj\u2081 cs) (cong proj\u2081 ct))\n (cong\u2082 _+_ (cong proj\u2082 cs) (cong proj\u2082 ct))\ncorrect2 {t = union s t} {\u03c1} =\n cong\u2082 _++_ (correct2 {t = s} {\u03c1}) (correct2 {t = t} {\u03c1})\ncorrect2 {t = diff s t} {\u03c1} =\n cong\u2082 _\\\\_ (correct2 {t = s} {\u03c1}) (correct2 {t = t} {\u03c1})\ncorrect2 {t = map s t} {\u03c1} with closed? s\n... | inj\u2082 tt =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n in\n cong\u2082 (\u03bb h1 h2 \u2192 mapBag (f \u2295 h1) (b \u2295 h2) \\\\ mapBag f b)\n (correct2 {t = s}) (correct2 {t = t})\n... | inj\u2081 if-closed = trans\n (cong (mapBag (\u27e6 s \u27e7 (ignore \u03c1))) (correct2 {t = t}))\n (eq-map s t \u03c1 (immune {t = s} if-closed))\n\n---------------\n-- Example 3 --\n---------------\n\n-- [+1] = \u03bb x \u2192 x + 1\n[+1] : \u2200 {\u0393} \u2192 Term \u0393 (nats \u21d2 nats)\n[+1] = (abs (add (var this) (nat 1)))\n\n-- inc = \u03bb bag \u2192 map [+1] bag\ninc : Term \u2205 (bags \u21d2 bags)\ninc = abs (map [+1] (var this))\n\n-- Program transformation in example 3\n-- Sadly, `inc` is not executable, for `Bag` is an abstract type\n-- whose existence we postulated.\n--\n-- derive2 inc = \u03bb bag dbag \u2192 map [+1] dbag\nexample-3 : derive2 inc \u2261 \u0394abs (\u0394map\u2081 [+1] (\u0394var this))\nexample-3 = refl\n\n-- Correctness of optimized derivation on `inc`\n--\n-- \u27e6 derive2 inc \u27e7 = \u27e6 derive inc \u27e7 \nexample-3-correct :\n \u27e6 derive2 inc \u27e7\u0394 \u2205 (valid2 inc) \u2261 \u27e6 derive inc \u27e7\u0394 \u2205 (unrestricted inc)\nexample-3-correct = correct2 {t = inc} {\u2205}\n","old_contents":"{-\nCommunicate to derivatives that changes to certain arguments\nare always nil (i. e., certain arguments are stable).\n-}\n\nmodule ExplicitNil where\n\nopen import TaggedDeltaTypes\n\nopen import Data.Nat\nopen import Data.Bool\nopen import Data.Sum hiding (map)\nopen import Data.Product hiding (map)\nopen import Data.Unit using (\u22a4 ; tt)\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\nopen import Relation.Binary.Core using (Decidable)\nopen import Relation.Nullary.Core using (yes ; no)\n\next\u00b3 : \u2200\n {A : Set}\n {B : A \u2192 Set}\n {C : (a : A) \u2192 B a \u2192 Set }\n {D : (a : A) \u2192 (b : B a) \u2192 C a b \u2192 Set}\n {f g : (a : A) \u2192 (b : B a) \u2192 (c : C a b) \u2192 D a b c} \u2192\n ((a : A) (b : B a) (c : C a b) \u2192 f a b c \u2261 g a b c) \u2192 f \u2261 g\n\next\u00b3 fabc=gabc = ext (\u03bb a \u2192 ext (\u03bb b \u2192 ext (\u03bb c \u2192 fabc=gabc a b c)))\n where ext = extensionality\n\nproj-H : \u2200 {\u0393 : Context} {\u03c1 : \u0394Env \u0393} {us vs} \u2192\n Honest \u03c1 (FV-union us vs) \u2192 Honest \u03c1 us \u00d7 Honest \u03c1 vs\nproj-H {\u2205} {us = \u2205} {vs = \u2205} clearly = clearly , clearly\nproj-H {us = alter us} {alter vs} (alter H) =\n let uss , vss = proj-H H in alter uss , alter vss\nproj-H {us = alter us} {abide vs} (abide eq H) =\n let uss , vss = proj-H H in\n alter uss , abide eq vss\nproj-H {us = abide us} {alter vs} (abide eq H) =\n let uss , vss = proj-H H in\n abide eq uss , alter vss\nproj-H {us = abide us} {abide vs} (abide eq H) =\n let uss , vss = proj-H H in\n abide eq uss , abide eq vss\n\n-- Equivalence proofs are unique\nequivalence-unique : \u2200 {A : Set} {a b : A} \u2192 \u2200 {p q : a \u2261 b} \u2192 p \u2261 q\nequivalence-unique {p = refl} {refl} = refl\n\n-- Product of singletons are singletons (for example)\nproduct-unique : \u2200 {A : Set} {B : A \u2192 Set} \u2192\n (\u2200 {a b : A} \u2192 a \u2261 b) \u2192\n (\u2200 {a : A} {c d : B a} \u2192 c \u2261 d) \u2192\n (\u2200 {p q : \u03a3 A B} \u2192 p \u2261 q)\nproduct-unique {A} {B} lhs rhs {a , c} {b , d}\n rewrite lhs {a} {b} = cong (_,_ b) rhs\n\n-- Validity proofs are (extensionally) unique (as functions)\nvalidity-unique : \u2200 {\u03c4} {v : \u27e6 \u03c4 \u27e7} {dv : \u0394Val \u03c4} \u2192\n \u2200 {p q : valid v dv} \u2192 p \u2261 q\nvalidity-unique {nats} = equivalence-unique\nvalidity-unique {bags} = refl\nvalidity-unique {\u03c3 \u21d2 \u03c4} = ext\u00b3 (\u03bb v dv R[v,dv] \u2192\n product-unique validity-unique equivalence-unique)\n\nstabilityVar : \u2200 {\u03c4 \u0393} {x : Var \u0393 \u03c4} \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} (H : Honest \u03c1 (select-just x)) \u2192\n \u27e6 x \u27e7 (ignore \u03c1) \u2295 \u27e6 x \u27e7\u0394Var \u03c1 \u2261 \u27e6 x \u27e7 (ignore \u03c1)\n\nstabilityVar {x = this} (abide proof _) = proof\nstabilityVar {x = that y} (alter H) = stabilityVar {x = y} H\n\nstability : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} (H : Honest \u03c1 (FV t)) \u2192\n \u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n \u2261 \u27e6 t \u27e7 (ignore \u03c1)\n\n-- Boilerplate begins\nstabilityAbs : \u2200 {\u03c3 \u03c4 \u0393} {t : Term (\u03c3 \u2022 \u0393) \u03c4} \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} (H : Honest \u03c1 (FV (abs t))) \u2192\n (v : \u27e6 \u03c3 \u27e7) \u2192\n \u27e6 t \u27e7 (v \u2022 ignore \u03c1) \u2295\n \u27e6 derive t \u27e7\u0394 (cons v (v \u229d v) R[v,u\u229dv] \u03c1) (unrestricted t)\n \u2261 \u27e6 t \u27e7 (v \u2022 ignore \u03c1)\nstabilityAbs {t = t} {\u03c1} H v with FV t | inspect FV t\n... | abide vars | [ case0 ] = stability {t = t} Ht\n where\n Ht : Honest (cons v (v \u229d v) R[v,u\u229dv] \u03c1) (FV t)\n Ht rewrite case0 = abide v\u2295[u\u229dv]=u H\n... | alter vars | [ case1 ] = stability {t = t} Ht\n where\n Ht : Honest (cons v (v \u229d v) R[v,u\u229dv] \u03c1) (FV t)\n Ht rewrite case1 = alter H\n-- Boilerplate ends\n\nstability {t = nat n} H = refl\nstability {t = bag b} H = b++\u2205=b\nstability {t = var x} H = stabilityVar H\nstability {t = abs t} {\u03c1} H = extensionality (stabilityAbs {t = t} H)\nstability {t = app s t} {\u03c1} H =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n v = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n dv = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n Hs , Ht = proj-H H\n in\n begin\n f v \u2295 df v dv (validity {t = t})\n \u2261\u27e8 sym (corollary s t) \u27e9\n (f \u2295 df) (v \u2295 dv)\n \u2261\u27e8 stability {t = s} Hs \u27e8$\u27e9 stability {t = t} Ht \u27e9\n f v\n \u220e where open \u2261-Reasoning\nstability {t = add s t} H =\n let Hs , Ht = proj-H H\n in cong\u2082 _+_ (stability {t = s} Hs) (stability {t = t} Ht)\nstability {t = map s t} {\u03c1} H =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n db = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n Hs , Ht = proj-H H\n map = mapBag\n in\n begin\n map f b \u2295 (map (f \u2295 df) (b \u2295 db) \u229d map f b)\n \u2261\u27e8 b++[d\\\\b]=d \u27e9\n map (f \u2295 df) (b \u2295 db)\n \u2261\u27e8 cong\u2082 map (stability {t = s} Hs) (stability {t = t} Ht) \u27e9\n map f b\n \u220e where open \u2261-Reasoning\n\neq-map : \u2200 {\u0393}\n (s : Term \u0393 (nats \u21d2 nats))\n (t : Term \u0393 bags)\n (\u03c1 : \u0394Env \u0393)\n (H : Honest \u03c1 (FV s)) \u2192\n \u27e6 \u0394map\u2081 s (derive t) \u27e7\u0394 \u03c1 (cons (unrestricted t) H tt tt)\n \u2261 \u27e6 \u0394map\u2080 s (derive s) t (derive t) \u27e7\u0394 \u03c1 (unrestricted (map s t))\n\neq-map s t \u03c1 H =\n let\n ds = derive s\n dt = derive t\n f = \u27e6 s \u27e7 (ignore \u03c1)\n v = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 ds \u27e7\u0394 \u03c1 (unrestricted s)\n dv = \u27e6 dt \u27e7\u0394 \u03c1 (unrestricted t)\n\n eq1 : \u27e6 \u0394map\u2080 s ds t dt \u27e7\u0394 \u03c1 (unrestricted (map s t))\n \u2261 mapBag (f \u2295 df) (v \u2295 dv) \u229d mapBag f v\n eq1 = refl\n\n eq2 : mapBag (f \u2295 df) (v \u2295 dv) \u229d mapBag f v\n \u2261 mapBag f dv\n eq2 = trans\n (cong (\u03bb hole \u2192 hole \u229d mapBag f v) (trans\n (cong (\u03bb hole \u2192 mapBag hole (v \u2295 dv)) (stability {t = s} H))\n map-over-++))\n [b++d]\\\\b=d\n\n eq3 : mapBag f dv\n \u2261 \u27e6 \u0394map\u2081 s (derive t) \u27e7\u0394 \u03c1 (cons (unrestricted t) H tt tt)\n eq3 = refl\n\n in sym (trans eq1 (trans eq2 eq3))\n\n-- Vars test\nnone-selected? : \u2200 {\u0393} \u2192 (vs : Vars \u0393) \u2192 (vs \u2261 select-none) \u228e \u22a4\nnone-selected? \u2205 = inj\u2081 refl\nnone-selected? (abide vs) = inj\u2082 tt\nnone-selected? (alter vs) with none-selected? vs\n... | inj\u2081 vs=\u2205 rewrite vs=\u2205 = inj\u2081 refl\n... | inj\u2082 _ = inj\u2082 tt\n\nclosed? : \u2200 {\u03c4 \u0393} \u2192 (t : Term \u0393 \u03c4) \u2192 (FV t \u2261 select-none) \u228e \u22a4\nclosed? t = none-selected? (FV t)\n\nvacuous-honesty : \u2200 {\u0393} {\u03c1 : \u0394Env \u0393} \u2192 Honest \u03c1 select-none\nvacuous-honesty {\u2205} {\u2205} = clearly\nvacuous-honesty {\u03c4 \u2022 \u0393} {cons _ _ _ \u03c1} = alter (vacuous-honesty {\u03c1 = \u03c1})\n\n-- Immunity of closed terms to dishonest environments\nimmune : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} \u2192\n (FV t \u2261 select-none) \u2192 \u2200 {\u03c1} \u2192 Honest \u03c1 (FV t)\nimmune {t = t} eq rewrite eq = vacuous-honesty\n\n-- Ineffectual first optimization step\nderive1 : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394Term \u0393 \u03c4\nderive1 (map s t) with closed? s\n... | inj\u2081 is-closed = \u0394map\u2081 s (derive t)\n... | inj\u2082 tt = derive (map s t)\nderive1 others = derive others\n\nvalid1 : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) {\u03c1 : \u0394Env \u0393} \u2192 derive1 t is-valid-for \u03c1\nvalid1 (nat n) = tt\nvalid1 (bag b) = tt\nvalid1 (var x) = tt\nvalid1 (abs t) = \u03bb _ _ _ \u2192 unrestricted t\nvalid1 (app s t) =\n cons (unrestricted s) (unrestricted t) (validity {t = t}) tt\nvalid1 (add s t) =\n cons (unrestricted s) (unrestricted t) tt tt\nvalid1 (map s t) {\u03c1} with closed? s\n... | inj\u2082 tt = cons (unrestricted s) (unrestricted t) tt tt\n... | inj\u2081 if-closed =\n cons (unrestricted t) (immune {t = s} if-closed) tt tt\n\ncorrect1 : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n \u27e6 derive1 t \u27e7\u0394 \u03c1 (valid1 t) \u2261 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n\ncorrect1 {t = nat n} = refl\ncorrect1 {t = bag b} = refl\ncorrect1 {t = var x} = refl\ncorrect1 {t = abs t} = refl\ncorrect1 {t = app s t} = refl\ncorrect1 {t = add s t} = refl\ncorrect1 {t = map s t} {\u03c1} with closed? s\n... | inj\u2082 tt = refl\n... | inj\u2081 if-closed = eq-map s t \u03c1 (immune {t = s} if-closed)\n\n-- derive2 = derive1 + congruence\nderive2 : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394Term \u0393 \u03c4\nderive2 (abs t) = \u0394abs (derive2 t)\nderive2 (app s t) = \u0394app (derive2 s) t (derive2 t)\nderive2 (add s t) = \u0394add (derive2 s) (derive2 t)\nderive2 (map s t) with closed? s\n... | inj\u2081 is-closed = \u0394map\u2081 s (derive2 t)\n... | inj\u2082 tt = \u0394map\u2080 s (derive2 s) t (derive2 t)\nderive2 others = derive others\n\nvalid2 : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) {\u03c1 : \u0394Env \u0393} \u2192 derive2 t is-valid-for \u03c1\ncorrect2 : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n \u27e6 derive2 t \u27e7\u0394 \u03c1 (valid2 t) \u2261 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n\nvalid2 (nat n) = tt\nvalid2 (bag b) = tt\nvalid2 (var x) = tt\nvalid2 (abs t) = \u03bb _ _ _ \u2192 valid2 t\nvalid2 (app s t) {\u03c1} = cons (valid2 s) (valid2 t) V tt\n where\n V : valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive2 t \u27e7\u0394 \u03c1 (valid2 t))\n V rewrite correct2 {t = t} {\u03c1} = validity {t = t} {\u03c1}\nvalid2 (add s t) = cons (valid2 s) (valid2 t) tt tt\nvalid2 (map s t) {\u03c1} with closed? s\n... | inj\u2082 tt = cons (valid2 s) (valid2 t) tt tt\n... | inj\u2081 if-closed =\n cons (valid2 t) (immune {t = s} if-closed) tt tt\n\ncorrect2 {t = nat n} = refl\ncorrect2 {t = bag b} = refl\ncorrect2 {t = var x} = refl\ncorrect2 {t = abs t} = ext\u00b3 (\u03bb _ _ _ \u2192 correct2 {t = t})\ncorrect2 {t = app {\u03c3} {\u03c4} s t} {\u03c1} = \u2245-to-\u2261 eq-all where\n open import Relation.Binary.HeterogeneousEquality hiding (cong\u2082)\n import Relation.Binary.HeterogeneousEquality as HET\n df0 : \u0394Val (\u03c3 \u21d2 \u03c4)\n df0 = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n df2 : \u0394Val (\u03c3 \u21d2 \u03c4)\n df2 = \u27e6 derive2 s \u27e7\u0394 \u03c1 (valid2 s)\n dv0 : \u0394Val \u03c3\n dv0 = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n dv2 : \u0394Val \u03c3\n dv2 = \u27e6 derive2 t \u27e7\u0394 \u03c1 (valid2 t)\n v : \u27e6 \u03c3 \u27e7\n v = \u27e6 t \u27e7 (ignore \u03c1)\n eq-df : df2 \u2261 df0\n eq-df = correct2 {t = s}\n eq-dv : dv2 \u2261 dv0\n eq-dv = correct2 {t = t}\n eq-all : df2 v dv2 (caddr (valid2 (app s t) {\u03c1}))\n \u2245 df0 v dv0 (validity {t = t} {\u03c1})\n eq-all rewrite eq-dv = HET.cong\n (\u03bb hole \u2192 hole v dv0 (validity {t = t} {\u03c1})) (\u2261-to-\u2245 eq-df)\n -- found without intuition by trial-and-error\ncorrect2 {t = add s t} {\u03c1} =\n let\n cs = correct2 {t = s} {\u03c1}\n ct = correct2 {t = t} {\u03c1}\n in cong\u2082 _,_ (cong\u2082 _+_ (cong proj\u2081 cs) (cong proj\u2081 ct))\n (cong\u2082 _+_ (cong proj\u2082 cs) (cong proj\u2082 ct))\ncorrect2 {t = map s t} {\u03c1} with closed? s\n... | inj\u2082 tt = \n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n in\n cong\u2082 (\u03bb h1 h2 \u2192 mapBag (f \u2295 h1) (b \u2295 h2) \\\\ mapBag f b)\n (correct2 {t = s}) (correct2 {t = t})\n... | inj\u2081 if-closed = trans\n (cong (mapBag (\u27e6 s \u27e7 (ignore \u03c1))) (correct2 {t = t}))\n (eq-map s t \u03c1 (immune {t = s} if-closed))\n\n---------------\n-- Example 3 --\n---------------\n\n-- [+1] = \u03bb x \u2192 x + 1\n[+1] : \u2200 {\u0393} \u2192 Term \u0393 (nats \u21d2 nats)\n[+1] = (abs (add (var this) (nat 1)))\n\n-- inc = \u03bb bag \u2192 map [+1] bag\ninc : Term \u2205 (bags \u21d2 bags)\ninc = abs (map [+1] (var this))\n\n-- Program transformation in example 3\n-- Sadly, `inc` is not executable, for `Bag` is an abstract type\n-- whose existence we postulated.\n--\n-- derive2 inc = \u03bb bag dbag \u2192 map [+1] dbag\nexample-3 : derive2 inc \u2261 \u0394abs (\u0394map\u2081 [+1] (\u0394var this))\nexample-3 = refl\n\n-- Correctness of optimized derivation on `inc`\n--\n-- \u27e6 derive2 inc \u27e7 = \u27e6 derive inc \u27e7 \nexample-3-correct :\n \u27e6 derive2 inc \u27e7\u0394 \u2205 (valid2 inc) \u2261 \u27e6 derive inc \u27e7\u0394 \u2205 (unrestricted inc)\nexample-3-correct = correct2 {t = inc} {\u2205}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d3f497c90ef2cbc4c034e571f3eb1595410f1084","subject":"fix spaces","message":"fix spaces\n","repos":"crypto-agda\/crypto-agda","old_file":"Prob.agda","new_file":"Prob.agda","new_contents":"module Prob where\n\nopen import Data.Bool\nopen import Data.Nat\nopen import Data.Vec\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\nopen import Data.Sum\n\nopen import Function\n\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Relation.Nullary\n\nrecord ]0,1[-ops (]0,1[ : Set) (_ : (x y : ]0,1[) \u2192 x ) \u2261 (x \u00b7E y) \/E z < \u00b7E-anti\u2082 x pf >\n\n -- proofs \u2261 maybe should be some Equivalence \u2248\n field\n +E-sym : (x y : ]0,1[) \u2192 x +E y \u2261 y +E x\n +E-assoc : (x y z : ]0,1[) \u2192 (x +E y) +E z \u2261 x +E (y +E z)\n \u00b7\/E-identity : (x : ]0,1[){y : ]0,1[} \u2192 x \u2261 (x \u00b7E y) \/E y < \u00b7E-anti\u2081 x >\n\nmodule ]0,1[-\u211a where\n data ]0,1[ : Set where\n 1+_\/2+x+_ : \u2115 \u2192 \u2115 \u2192 ]0,1[\n -- to\u211a : ]0,1] \u2192 \u211a\n -- to\u211a (1+ x \/1+x+ y) = (1 + x) \/\u211a (1 + x + y)\n\n postulate\n _ : (x y : ]0,1[) \u2192 x ) \u2261 (x \u00b7E y) \/E z < \u00b7E-anti\u2082 x pf >\n\n postulate\n +E-sym : (x x\u2081 : ]0,1[) \u2192 x +E x\u2081 \u2261 x\u2081 +E x\n +E-assoc : (x x\u2081 x\u2082 : ]0,1[) \u2192 (x +E x\u2081) +E x\u2082 \u2261 x +E (x\u2081 +E x\u2082)\n \u00b7\/E-identity : (x : ]0,1[) {y : ]0,1[} \u2192 x \u2261 (x \u00b7E y) \/E y < \u00b7E-anti\u2081 x >\n\n ops : ]0,1[-ops ]0,1[ _ : (x : [0,1]) \u2192 Inc x \u2192 ]0,1[\n 0I < () >\n 1I < () >\n (x I) < pos > = x\n\n _+I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I +I x = x\n 1I +I _ = 1I\n x I +I 0I = x I\n x I +I 1I = 1I\n x I +I x\u2081 I = (x +E x\u2081) I\n\n _\u00b7I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I \u00b7I y = 0I\n 1I \u00b7I y = y\n (x I) \u00b7I 0I = 0I\n (x I) \u00b7I 1I = x I\n (x I) \u00b7I (x\u2081 I) = (x \u00b7E x\u2081) I\n\n -- 1\/I1+_ : \u2115 \u2192 [0,1]\n\n\n _\/I_<_,_> : (x y : [0,1]) \u2192 x \u2264I y \u2192 Pos y \u2192 [0,1]\n x \/I 0I < _ , () >\n x \/I 1I < _ , _ > = x\n 0I \/I _ < _ , _ > = 0I\n 1I \/I _ I < () , _ >\n (x I) \/I y I < E\u2264E pf , _ > = (x \/E y < pf >) I\n (x I) \/I .x I < E\u2261E , _ > = 1I\n\n\n +I-identity : (x : [0,1]) \u2192 x \u2261 0I +I x\n +I-identity x = refl\n\n *-anti : (x : [0,1]){y z : [0,1]} \u2192 y \u2264I z \u2192 x \u00b7I y \u2264I z\n *-anti 0I le = z\u2264n\n *-anti 1I le = le\n *-anti (x I) z\u2264n = z\u2264n\n *-anti (x I) n\u22641 = n\u22641\n *-anti (x I) (E\u2264E pf) = E\u2264E (\u00b7E-anti\u2082 x pf)\n *-anti (x I) E\u2261E = E\u2264E (\u00b7E-anti\u2081 x)\n\n *\/-assoc : (x y z : [0,1])(pr : y \u2264I z)(pos : Pos z) \u2192 (x \u00b7I (y \/I z < pr , pos >)) \u2261 (x \u00b7I y) \/I z < *-anti x pr , pos >\n *\/-assoc x y 0I pr ()\n *\/-assoc x y 1I pr pos = refl\n *\/-assoc 0I y (z I) pr pos = refl\n *\/-assoc 1I y (z I) pr pos = refl\n *\/-assoc (x I) 0I (z I) pr pos = refl\n *\/-assoc (x I) 1I (z I) () pos\n *\/-assoc (x I) (y I) (z I) (E\u2264E pf) pos = cong _I (\u00b7\/E-assoc x y z pf)\n *\/-assoc (x I) (y I) (.y I) E\u2261E pos = cong _I (\u00b7\/E-identity x)\n\n +I-sym : (x y : [0,1]) \u2192 x +I y \u2261 y +I x\n +I-sym 0I 0I = refl\n +I-sym 0I 1I = refl\n +I-sym 0I (x I) = refl\n +I-sym 1I 0I = refl\n +I-sym 1I 1I = refl\n +I-sym 1I (x I) = refl\n +I-sym (x I) 0I = refl\n +I-sym (x I) 1I = refl\n +I-sym (x I) (y I) = cong _I (+E-sym x y)\n\n +I-assoc : (x y z : [0,1]) \u2192 x +I y +I z \u2261 x +I (y +I z)\n +I-assoc 0I y z = refl\n +I-assoc 1I y z = refl\n +I-assoc (x I) 0I z = refl\n +I-assoc (x I) 1I z = refl\n +I-assoc (x I) (y I) 0I = refl\n +I-assoc (x I) (y I) 1I = refl\n +I-assoc (x I) (y I) (z I) = cong _I (+E-assoc x y z)\n\n \u2264I-trans : {x y z : [0,1]} \u2192 x \u2264I y \u2192 y \u2264I z \u2192 x \u2264I z\n \u2264I-trans z\u2264n le2 = z\u2264n\n \u2264I-trans n\u22641 n\u22641 = n\u22641\n \u2264I-trans (E\u2264E x\u2081) n\u22641 = n\u22641\n \u2264I-trans (E\u2264E x\u2081) (E\u2264E x\u2082) = E\u2264E ( : (A B : Event)(pf : Pos Pr[ B ]) \u2192 [0,1]\n Pr[ A \u2223 B ]< pr > = Pr[ A \u2229 B ] \/I Pr[ B ] < Pr-mono (\u2229-lem A) , pr >\n\n _ind_ : (A B : Event) \u2192 Set\n A ind B = Pr[ A ] \u00b7I Pr[ B ] \u2261 Pr[ A \u2229 B ]\n\n union-bound : (A\u2081 A\u2082 : Event) \u2192 Pr[ (A\u2081 \u222a A\u2082) ] \u2264I Pr[ A\u2081 ] +I Pr[ A\u2082 ]\n union-bound A\u2081 A\u2082 = go allU where\n sA\u2081 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2081 = \u03bb xs \u2192 sumP (pr_\u220b_ A\u2081) xs\n sA\u2082 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2082 = sumP (pr_\u220b_ A\u2082)\n go : {n : \u2115}(xs : Vec U n) \u2192 sumP (pr_\u220b_ (A\u2081 \u222a A\u2082)) xs \u2264I sumP (pr_\u220b_ A\u2081) xs +I sumP (pr_\u220b_ A\u2082) xs\n go [] = \u2264I-refl\n go (x \u2237 xs) with A\u2081 x | A\u2082 x\n ... | true | true rewrite +I-assoc (P x) (sA\u2081 xs) (P x +I sA\u2082 xs)\n | +I-sym (P x) (sA\u2082 xs)\n | sym (+I-assoc (sA\u2081 xs) (sA\u2082 xs) (P x))\n = \u2264I-pres (P x) (\u2264I-mono (P x) (go xs))\n ... | true | false rewrite +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | true rewrite sym (+I-assoc (sA\u2081 xs) (P x) (sA\u2082 xs))\n | +I-sym (sA\u2081 xs) (P x)\n | +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | false = go xs\n\n module RandomVar (V : Set) (_==V_ : V \u2192 V \u2192 Bool) where\n RV : Set\n RV = U \u2192 V\n\n _^-1_ : RV \u2192 V \u2192 Event\n RV ^-1 v = \u03bb x \u2192 RV x ==V v\n\n _\u2261r_ : RV \u2192 V \u2192 Event\n RV \u2261r v = RV ^-1 v\n\n -- sugar, socker et sucre\n Pr[_\u2261_] : RV \u2192 V \u2192 [0,1]\n Pr[ X \u2261 v ] = Pr[ X \u2261r v ]\n\n -- \u2200 (a b) . Pr[X = a and Y = b] = Pr[X = a] \u00b7 Pr[Y = b]\n _indRV_ : RV \u2192 RV \u2192 Set\n X indRV Y = (a b : V) \u2192 (X ^-1 a) ind (Y ^-1 b)\n","old_contents":"module Prob where\n\nopen import Data.Bool\nopen import Data.Nat\nopen import Data.Vec\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\nopen import Data.Sum\n\nopen import Function\n\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Relation.Nullary\n\nrecord ]0,1[-ops (]0,1[ : Set) (_ : (x y : ]0,1[) \u2192 x ) \u2261 (x \u00b7E y) \/E z < \u00b7E-anti\u2082 x pf >\n\n -- proofs \u2261 maybe should be some Equivalence \u2248 \n field\n +E-sym : (x y : ]0,1[) \u2192 x +E y \u2261 y +E x\n +E-assoc : (x y z : ]0,1[) \u2192 (x +E y) +E z \u2261 x +E (y +E z)\n \u00b7\/E-identity : (x : ]0,1[){y : ]0,1[} \u2192 x \u2261 (x \u00b7E y) \/E y < \u00b7E-anti\u2081 x >\n\nmodule ]0,1[-\u211a where\n data ]0,1[ : Set where\n 1+_\/2+x+_ : \u2115 \u2192 \u2115 \u2192 ]0,1[\n -- to\u211a : ]0,1] \u2192 \u211a\n -- to\u211a (1+ x \/1+x+ y) = (1 + x) \/\u211a (1 + x + y)\n \n postulate\n _ : (x y : ]0,1[) \u2192 x ) \u2261 (x \u00b7E y) \/E z < \u00b7E-anti\u2082 x pf >\n\n postulate\n +E-sym : (x x\u2081 : ]0,1[) \u2192 x +E x\u2081 \u2261 x\u2081 +E x\n +E-assoc : (x x\u2081 x\u2082 : ]0,1[) \u2192 (x +E x\u2081) +E x\u2082 \u2261 x +E (x\u2081 +E x\u2082)\n \u00b7\/E-identity : (x : ]0,1[) {y : ]0,1[} \u2192 x \u2261 (x \u00b7E y) \/E y < \u00b7E-anti\u2081 x >\n\n ops : ]0,1[-ops ]0,1[ _ : (x : [0,1]) \u2192 Inc x \u2192 ]0,1[ \n 0I < () >\n 1I < () >\n (x I) < pos > = x\n\n _+I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I +I x = x\n 1I +I _ = 1I\n x I +I 0I = x I\n x I +I 1I = 1I\n x I +I x\u2081 I = (x +E x\u2081) I\n\n _\u00b7I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I \u00b7I y = 0I\n 1I \u00b7I y = y\n (x I) \u00b7I 0I = 0I\n (x I) \u00b7I 1I = x I\n (x I) \u00b7I (x\u2081 I) = (x \u00b7E x\u2081) I\n\n -- 1\/I1+_ : \u2115 \u2192 [0,1]\n\n \n _\/I_<_,_> : (x y : [0,1]) \u2192 x \u2264I y \u2192 Pos y \u2192 [0,1]\n x \/I 0I < _ , () >\n x \/I 1I < _ , _ > = x\n 0I \/I _ < _ , _ > = 0I\n 1I \/I _ I < () , _ > \n (x I) \/I y I < E\u2264E pf , _ > = (x \/E y < pf >) I\n (x I) \/I .x I < E\u2261E , _ > = 1I\n\n\n +I-identity : (x : [0,1]) \u2192 x \u2261 0I +I x\n +I-identity x = refl\n\n *-anti : (x : [0,1]){y z : [0,1]} \u2192 y \u2264I z \u2192 x \u00b7I y \u2264I z\n *-anti 0I le = z\u2264n\n *-anti 1I le = le\n *-anti (x I) z\u2264n = z\u2264n\n *-anti (x I) n\u22641 = n\u22641\n *-anti (x I) (E\u2264E pf) = E\u2264E (\u00b7E-anti\u2082 x pf)\n *-anti (x I) E\u2261E = E\u2264E (\u00b7E-anti\u2081 x)\n\n *\/-assoc : (x y z : [0,1])(pr : y \u2264I z)(pos : Pos z) \u2192 (x \u00b7I (y \/I z < pr , pos >)) \u2261 (x \u00b7I y) \/I z < *-anti x pr , pos >\n *\/-assoc x y 0I pr ()\n *\/-assoc x y 1I pr pos = refl\n *\/-assoc 0I y (z I) pr pos = refl\n *\/-assoc 1I y (z I) pr pos = refl\n *\/-assoc (x I) 0I (z I) pr pos = refl\n *\/-assoc (x I) 1I (z I) () pos\n *\/-assoc (x I) (y I) (z I) (E\u2264E pf) pos = cong _I (\u00b7\/E-assoc x y z pf)\n *\/-assoc (x I) (y I) (.y I) E\u2261E pos = cong _I (\u00b7\/E-identity x)\n\n +I-sym : (x y : [0,1]) \u2192 x +I y \u2261 y +I x\n +I-sym 0I 0I = refl\n +I-sym 0I 1I = refl\n +I-sym 0I (x I) = refl\n +I-sym 1I 0I = refl\n +I-sym 1I 1I = refl\n +I-sym 1I (x I) = refl\n +I-sym (x I) 0I = refl\n +I-sym (x I) 1I = refl\n +I-sym (x I) (y I) = cong _I (+E-sym x y)\n\n +I-assoc : (x y z : [0,1]) \u2192 x +I y +I z \u2261 x +I (y +I z)\n +I-assoc 0I y z = refl\n +I-assoc 1I y z = refl\n +I-assoc (x I) 0I z = refl\n +I-assoc (x I) 1I z = refl\n +I-assoc (x I) (y I) 0I = refl\n +I-assoc (x I) (y I) 1I = refl\n +I-assoc (x I) (y I) (z I) = cong _I (+E-assoc x y z)\n\n \u2264I-trans : {x y z : [0,1]} \u2192 x \u2264I y \u2192 y \u2264I z \u2192 x \u2264I z\n \u2264I-trans z\u2264n le2 = z\u2264n\n \u2264I-trans n\u22641 n\u22641 = n\u22641\n \u2264I-trans (E\u2264E x\u2081) n\u22641 = n\u22641\n \u2264I-trans (E\u2264E x\u2081) (E\u2264E x\u2082) = E\u2264E ( : (A B : Event)(pf : Pos Pr[ B ]) \u2192 [0,1] \n Pr[ A \u2223 B ]< pr > = Pr[ A \u2229 B ] \/I Pr[ B ] < Pr-mono (\u2229-lem A) , pr >\n\n _ind_ : (A B : Event) \u2192 Set\n A ind B = Pr[ A ] \u00b7I Pr[ B ] \u2261 Pr[ A \u2229 B ]\n\n union-bound : (A\u2081 A\u2082 : Event) \u2192 Pr[ (A\u2081 \u222a A\u2082) ] \u2264I Pr[ A\u2081 ] +I Pr[ A\u2082 ]\n union-bound A\u2081 A\u2082 = go allU where\n sA\u2081 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2081 = \u03bb xs \u2192 sumP (pr_\u220b_ A\u2081) xs\n sA\u2082 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2082 = sumP (pr_\u220b_ A\u2082)\n go : {n : \u2115}(xs : Vec U n) \u2192 sumP (pr_\u220b_ (A\u2081 \u222a A\u2082)) xs \u2264I sumP (pr_\u220b_ A\u2081) xs +I sumP (pr_\u220b_ A\u2082) xs\n go [] = \u2264I-refl\n go (x \u2237 xs) with A\u2081 x | A\u2082 x \n ... | true | true rewrite +I-assoc (P x) (sA\u2081 xs) (P x +I sA\u2082 xs)\n | +I-sym (P x) (sA\u2082 xs)\n | sym (+I-assoc (sA\u2081 xs) (sA\u2082 xs) (P x))\n = \u2264I-pres (P x) (\u2264I-mono (P x) (go xs)) \n ... | true | false rewrite +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | true rewrite sym (+I-assoc (sA\u2081 xs) (P x) (sA\u2082 xs))\n | +I-sym (sA\u2081 xs) (P x)\n | +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | false = go xs \n\n module RandomVar (V : Set) (_==V_ : V \u2192 V \u2192 Bool) where\n RV : Set\n RV = U \u2192 V\n\n _^-1_ : RV \u2192 V \u2192 Event\n RV ^-1 v = \u03bb x \u2192 RV x ==V v \n\n _\u2261r_ : RV \u2192 V \u2192 Event \n RV \u2261r v = RV ^-1 v\n \n -- sugar, socker et sucre\n Pr[_\u2261_] : RV \u2192 V \u2192 [0,1]\n Pr[ X \u2261 v ] = Pr[ X \u2261r v ]\n \n -- \u2200 (a b) . Pr[X = a and Y = b] = Pr[X = a] \u00b7 Pr[Y = b]\n _indRV_ : RV \u2192 RV \u2192 Set\n X indRV Y = (a b : V) \u2192 (X ^-1 a) ind (Y ^-1 b)","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"e4762e8af8591526fcb5395b9c63888ed89cadd2","subject":"agda: Remove code duplication","message":"agda: Remove code duplication\n\nThe current code defines two overloads of the same function independently, with\nduplicated documentation. However, one can be defined in terms of the other.\n\nNote: I could not fully test this code, since it is not yet clear which file is\nthe main one. I typechecked Syntax.Language.Atlas, I hope this was enough.\n\nI couldn't test this change because of #41: without fixing it, either I risk\nbreaking code, or I can't fix issues I spot.\n\nOld-commit-hash: b481a293cbc77ca4b2981761c15e7771d95216af\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Type\/Plotkin.agda","new_file":"Syntax\/Type\/Plotkin.agda","new_contents":"module Syntax.Type.Plotkin where\n\n-- Types for language description \u00e0 la Plotkin (LCF as PL)\n--\n-- Given base types, we build function types.\n\ninfixr 5 _\u21d2_\n\ndata Type (B : Set {- of base types -}) : Set where\n base : (\u03b9 : B) \u2192 Type B\n _\u21d2_ : (\u03c3 : Type B) \u2192 (\u03c4 : Type B) \u2192 Type B\n\n-- Lift (\u0394 : B \u2192 Type B) to (\u0394type : Type B \u2192 Type B)\n-- according to \u0394 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394 \u03c3 \u21d2 \u0394 \u03c4\nlift\u2081 : \u2200 {B} \u2192 (B \u2192 Type B) \u2192 (Type B \u2192 Type B)\nlift\u2081 f (base \u03b9) = f \u03b9\nlift\u2081 f (\u03c3 \u21d2 \u03c4) = let \u0394 = lift\u2081 f in \u03c3 \u21d2 \u0394 \u03c3 \u21d2 \u0394 \u03c4\n\n-- Note: the above is monadic bind with a different argument order.\n\nopen import Function\n\n-- Variant of lift\u2081 for (\u0394 : B \u2192 B).\nlift\u2080 : \u2200 {B} \u2192 (B \u2192 B) \u2192 (Type B \u2192 Type B)\nlift\u2080 f = lift\u2081 $ base \u2218 f\n-- If lift\u2081 is a monadic bind, this is fmap,\n-- and base is return.\n--\n-- Similarly, for collections map can be defined from flatMap, like lift\u2080 can be\n-- defined in terms of lift\u2081.\n","old_contents":"module Syntax.Type.Plotkin where\n\n-- Types for language description \u00e0 la Plotkin (LCF as PL)\n--\n-- Given base types, we build function types.\n\ninfixr 5 _\u21d2_\n\ndata Type (B : Set {- of base types -}) : Set where\n base : (\u03b9 : B) \u2192 Type B\n _\u21d2_ : (\u03c3 : Type B) \u2192 (\u03c4 : Type B) \u2192 Type B\n\n-- Lift (\u0394 : B \u2192 B) to (\u0394type : Type B \u2192 Type B)\n-- according to \u0394 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394 \u03c3 \u21d2 \u0394 \u03c4\nlift\u2080 : \u2200 {B} \u2192 (B \u2192 B) \u2192 (Type B \u2192 Type B)\nlift\u2080 f (base \u03b9) = base (f \u03b9)\nlift\u2080 f (\u03c3 \u21d2 \u03c4) = let \u0394 = lift\u2080 f in \u03c3 \u21d2 \u0394 \u03c3 \u21d2 \u0394 \u03c4\n\n-- Lift (\u0394 : B \u2192 Type B) to (\u0394type : Type B \u2192 Type B)\n-- according to \u0394 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394 \u03c3 \u21d2 \u0394 \u03c4\nlift\u2081 : \u2200 {B} \u2192 (B \u2192 Type B) \u2192 (Type B \u2192 Type B)\nlift\u2081 f (base \u03b9) = f \u03b9\nlift\u2081 f (\u03c3 \u21d2 \u03c4) = let \u0394 = lift\u2081 f in \u03c3 \u21d2 \u0394 \u03c3 \u21d2 \u0394 \u03c4\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"5cd1142501fcdb87d0821cc80e8dfa92f1d8618d","subject":"If distance between x y are zero, then x = y","message":"If distance between x y are zero, then x = y\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Nat\/Distance.agda","new_file":"lib\/Data\/Nat\/Distance.agda","new_contents":"open import Data.Nat.NP\nopen import Data.Nat.Properties as Props\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Data.Nat.Distance where\n\ndist : \u2115 \u2192 \u2115 \u2192 \u2115\ndist zero y = y\ndist x zero = x\ndist (suc x) (suc y) = dist x y\n\ndist-refl : \u2200 x \u2192 dist x x \u2261 0\ndist-refl zero = idp\ndist-refl (suc x) rewrite dist-refl x = idp\n\ndist-0\u2261id : \u2200 x \u2192 dist 0 x \u2261 x\ndist-0\u2261id _ = idp\n\ndist-x-x+y\u2261y : \u2200 x y \u2192 dist x (x + y) \u2261 y\ndist-x-x+y\u2261y zero y = idp\ndist-x-x+y\u2261y (suc x) y = dist-x-x+y\u2261y x y\n\ndist-sym : \u2200 x y \u2192 dist x y \u2261 dist y x\ndist-sym zero zero = idp\ndist-sym zero (suc y) = idp\ndist-sym (suc x) zero = idp\ndist-sym (suc x) (suc y) = dist-sym x y\n\ndist-x+ : \u2200 x y z \u2192 dist (x + y) (x + z) \u2261 dist y z\ndist-x+ zero y z = idp\ndist-x+ (suc x) y z = dist-x+ x y z\n\ndist-2* : \u2200 x y \u2192 dist (2* x) (2* y) \u2261 2* dist x y\ndist-2* zero y = idp\ndist-2* (suc x) zero = idp\ndist-2* (suc x) (suc y) rewrite +-assoc-comm {x} {1} {x} | +-assoc-comm {y} {1} {y} = dist-2* x y\n\ndist-asym-def : \u2200 {x y} \u2192 x \u2264 y \u2192 x + dist x y \u2261 y\ndist-asym-def z\u2264n = idp\ndist-asym-def (s\u2264s pf) = ap suc (dist-asym-def pf)\n\ndist-sym-wlog : \u2200 (f : \u2115 \u2192 \u2115) \u2192 (\u2200 x k \u2192 dist (f x) (f (x + k)) \u2261 f k) \u2192 \u2200 x y \u2192 dist (f x) (f y) \u2261 f (dist x y)\ndist-sym-wlog f pf x y with compare x y\ndist-sym-wlog f pf x .(suc (x + k)) | less .x k with pf x (suc k)\n... | q rewrite +-assoc-comm {x} {1} {k} | q | ! +-assoc-comm {x} {1} {k} | dist-x-x+y\u2261y x (suc k) = idp\ndist-sym-wlog f pf .y y | equal .y with pf y 0\n... | q rewrite \u2115\u00b0.+-comm y 0 | dist-refl y = q\ndist-sym-wlog f pf .(suc (y + k)) y | greater .y k with pf y (suc k)\n... | q rewrite +-assoc-comm {1} {y} {k} | dist-sym (y + suc k) y | dist-x-x+y\u2261y y (suc k) | dist-sym (f (y + suc k)) (f y) = q\n\ndist-x* : \u2200 x y z \u2192 dist (x * y) (x * z) \u2261 x * dist y z\ndist-x* x = dist-sym-wlog (_*_ x) pf\n where pf : \u2200 a k \u2192 dist (x * a) (x * (a + k)) \u2261 x * k\n pf a k rewrite proj\u2081 \u2115\u00b0.distrib x a k = dist-x-x+y\u2261y (x * a) _\n\ndist-2^* : \u2200 x y z \u2192 dist \u27e82^ x * y \u27e9 \u27e82^ x * z \u27e9 \u2261 \u27e82^ x * dist y z \u27e9\ndist-2^* x = dist-sym-wlog (2^\u27e8 x \u27e9*) pf\n where pf : \u2200 a k \u2192 dist \u27e82^ x * a \u27e9 \u27e82^ x * (a + k) \u27e9 \u2261 \u27e82^ x * k \u27e9\n pf a k rewrite 2^*-distrib x a k = dist-x-x+y\u2261y \u27e82^ x * a \u27e9 \u27e82^ x * k \u27e9\n\ndist-bounded : \u2200 {x y f} \u2192 x \u2264 f \u2192 y \u2264 f \u2192 dist x y \u2264 f\ndist-bounded z\u2264n y\u2264f = y\u2264f\ndist-bounded (s\u2264s x\u2264f) z\u2264n = s\u2264s x\u2264f\ndist-bounded (s\u2264s x\u2264f) (s\u2264s y\u2264f) = \u2264-step (dist-bounded x\u2264f y\u2264f)\n\n-- Triangular inequality\ndist-sum : \u2200 x y z \u2192 dist x z \u2264 dist x y + dist y z\ndist-sum zero zero z = \u2115\u2264.refl\ndist-sum zero (suc y) zero = z\u2264n\ndist-sum zero (suc y) (suc z) = s\u2264s (dist-sum zero y z)\ndist-sum (suc x) zero zero = s\u2264s (\u2115\u2264.reflexive (\u2115\u00b0.+-comm 0 x))\ndist-sum (suc x) (suc y) zero\n rewrite \u2115\u00b0.+-comm (dist x y) (suc y)\n | dist-sym x y = s\u2264s (dist-sum zero y x)\ndist-sum (suc x) zero (suc z) = dist-sum x zero z\n \u2219\u2264 \u2115\u2264.reflexive (ap\u2082 _+_ (dist-sym x 0) idp)\n \u2219\u2264 \u2264-step (\u2115\u2264.refl {x} +-mono \u2264-step \u2115\u2264.refl)\ndist-sum (suc x) (suc y) (suc z) = dist-sum x y z\n\ndist\u22610 : \u2200 x y \u2192 dist x y \u2261 0 \u2192 x \u2261 y\ndist\u22610 zero zero d\u22610 = refl\ndist\u22610 zero (suc y) ()\ndist\u22610 (suc x) zero ()\ndist\u22610 (suc x) (suc y) d\u22610 = ap suc (dist\u22610 x y d\u22610)\n{-\npost--ulate\n dist-\u2264 : \u2200 x y \u2192 dist x y \u2264 x\n dist-mono\u2081 : \u2200 x y z t \u2192 x \u2264 y \u2192 dist z t \u2264 dist (x + z) (y + t)\n-}\n\n-- Haskell\n-- let dist x y = abs (x - y)\n-- quickCheck (forAll (replicateM 3 (choose (0,100))) (\\ [x,y,z] -> dist (x * y) (x * z) == x * dist y z))\n","old_contents":"open import Data.Nat.NP\nopen import Data.Nat.Properties as Props\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Data.Nat.Distance where\n\ndist : \u2115 \u2192 \u2115 \u2192 \u2115\ndist zero y = y\ndist x zero = x\ndist (suc x) (suc y) = dist x y\n\ndist-refl : \u2200 x \u2192 dist x x \u2261 0\ndist-refl zero = idp\ndist-refl (suc x) rewrite dist-refl x = idp\n\ndist-0\u2261id : \u2200 x \u2192 dist 0 x \u2261 x\ndist-0\u2261id _ = idp\n\ndist-x-x+y\u2261y : \u2200 x y \u2192 dist x (x + y) \u2261 y\ndist-x-x+y\u2261y zero y = idp\ndist-x-x+y\u2261y (suc x) y = dist-x-x+y\u2261y x y\n\ndist-sym : \u2200 x y \u2192 dist x y \u2261 dist y x\ndist-sym zero zero = idp\ndist-sym zero (suc y) = idp\ndist-sym (suc x) zero = idp\ndist-sym (suc x) (suc y) = dist-sym x y\n\ndist-x+ : \u2200 x y z \u2192 dist (x + y) (x + z) \u2261 dist y z\ndist-x+ zero y z = idp\ndist-x+ (suc x) y z = dist-x+ x y z\n\ndist-2* : \u2200 x y \u2192 dist (2* x) (2* y) \u2261 2* dist x y\ndist-2* zero y = idp\ndist-2* (suc x) zero = idp\ndist-2* (suc x) (suc y) rewrite +-assoc-comm {x} {1} {x} | +-assoc-comm {y} {1} {y} = dist-2* x y\n\ndist-asym-def : \u2200 {x y} \u2192 x \u2264 y \u2192 x + dist x y \u2261 y\ndist-asym-def z\u2264n = idp\ndist-asym-def (s\u2264s pf) = ap suc (dist-asym-def pf)\n\ndist-sym-wlog : \u2200 (f : \u2115 \u2192 \u2115) \u2192 (\u2200 x k \u2192 dist (f x) (f (x + k)) \u2261 f k) \u2192 \u2200 x y \u2192 dist (f x) (f y) \u2261 f (dist x y)\ndist-sym-wlog f pf x y with compare x y\ndist-sym-wlog f pf x .(suc (x + k)) | less .x k with pf x (suc k)\n... | q rewrite +-assoc-comm {x} {1} {k} | q | ! +-assoc-comm {x} {1} {k} | dist-x-x+y\u2261y x (suc k) = idp\ndist-sym-wlog f pf .y y | equal .y with pf y 0\n... | q rewrite \u2115\u00b0.+-comm y 0 | dist-refl y = q\ndist-sym-wlog f pf .(suc (y + k)) y | greater .y k with pf y (suc k)\n... | q rewrite +-assoc-comm {1} {y} {k} | dist-sym (y + suc k) y | dist-x-x+y\u2261y y (suc k) | dist-sym (f (y + suc k)) (f y) = q\n\ndist-x* : \u2200 x y z \u2192 dist (x * y) (x * z) \u2261 x * dist y z\ndist-x* x = dist-sym-wlog (_*_ x) pf\n where pf : \u2200 a k \u2192 dist (x * a) (x * (a + k)) \u2261 x * k\n pf a k rewrite proj\u2081 \u2115\u00b0.distrib x a k = dist-x-x+y\u2261y (x * a) _\n\ndist-2^* : \u2200 x y z \u2192 dist \u27e82^ x * y \u27e9 \u27e82^ x * z \u27e9 \u2261 \u27e82^ x * dist y z \u27e9\ndist-2^* x = dist-sym-wlog (2^\u27e8 x \u27e9*) pf\n where pf : \u2200 a k \u2192 dist \u27e82^ x * a \u27e9 \u27e82^ x * (a + k) \u27e9 \u2261 \u27e82^ x * k \u27e9\n pf a k rewrite 2^*-distrib x a k = dist-x-x+y\u2261y \u27e82^ x * a \u27e9 \u27e82^ x * k \u27e9\n\ndist-bounded : \u2200 {x y f} \u2192 x \u2264 f \u2192 y \u2264 f \u2192 dist x y \u2264 f\ndist-bounded z\u2264n y\u2264f = y\u2264f\ndist-bounded (s\u2264s x\u2264f) z\u2264n = s\u2264s x\u2264f\ndist-bounded (s\u2264s x\u2264f) (s\u2264s y\u2264f) = \u2264-step (dist-bounded x\u2264f y\u2264f)\n\n-- Triangular inequality\ndist-sum : \u2200 x y z \u2192 dist x z \u2264 dist x y + dist y z\ndist-sum zero zero z = \u2115\u2264.refl\ndist-sum zero (suc y) zero = z\u2264n\ndist-sum zero (suc y) (suc z) = s\u2264s (dist-sum zero y z)\ndist-sum (suc x) zero zero = s\u2264s (\u2115\u2264.reflexive (\u2115\u00b0.+-comm 0 x))\ndist-sum (suc x) (suc y) zero\n rewrite \u2115\u00b0.+-comm (dist x y) (suc y)\n | dist-sym x y = s\u2264s (dist-sum zero y x)\ndist-sum (suc x) zero (suc z) = dist-sum x zero z\n \u2219\u2264 \u2115\u2264.reflexive (ap\u2082 _+_ (dist-sym x 0) idp)\n \u2219\u2264 \u2264-step (\u2115\u2264.refl {x} +-mono \u2264-step \u2115\u2264.refl)\ndist-sum (suc x) (suc y) (suc z) = dist-sum x y z\n\n\n{-\npost--ulate\n dist-\u2264 : \u2200 x y \u2192 dist x y \u2264 x\n dist-mono\u2081 : \u2200 x y z t \u2192 x \u2264 y \u2192 dist z t \u2264 dist (x + z) (y + t)\n-}\n\n-- Haskell\n-- let dist x y = abs (x - y)\n-- quickCheck (forAll (replicateM 3 (choose (0,100))) (\\ [x,y,z] -> dist (x * y) (x * z) == x * dist y z))\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"b2e8244277ed8fc4aa948d2ec5cd7ec553e600b8","subject":"Mention third claim of the Agda code (for #275).","message":"Mention third claim of the Agda code (for #275).\n\nOld-commit-hash: 3506fbc56e5ce8c777e14da362a2aa440bcd62bb\n","repos":"inc-lc\/ilc-agda","old_file":"README.agda","new_file":"README.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Machine-checked formalization of the theoretical results presented\n-- in the paper:\n--\n-- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann.\n-- A Theory of Changes for Higher-Order Languages:\n-- Incrementalizing \u03bb-Calculi by Static Differentiation.\n-- To appear at PLDI, ACM 2014.\n--\n-- We claim that this formalization\n--\n-- (1) proves every lemma and theorem in Sec. 2 and 3 of the paper,\n-- (2) formally specifies the interface between our incrementalization\n-- framework and potential plugins, and\n-- (3) shows that the plugin interface can be instantiated.\n--\n-- The first claim is the main reason for a machine-checked\n-- proof: We want to be sure that we got the proofs right.\n--\n-- The second claim is about reusability and applicability: Only\n-- a clearly defined interface allows other researchers to\n-- provide plugins for our framework.\n--\n-- The third claim is to show that the plugin interface is\n-- consistent: An inconsistent plugin interface would allow to\n-- prove arbitrary results in the framework.\n------------------------------------------------------------------------\n\nmodule README where\n\n-- We know two good ways to read this code base. You can either\n-- use Emacs with agda2-mode to interact with the source files,\n-- or you can use a web browser to view the pretty-printed and\n-- hyperlinked source files. For Agda power users, we also\n-- include basic setup information for your own machine.\n\n\n-- IF YOU WANT TO USE A BROWSER\n-- ============================\n--\n-- Start with *HTML* version of this readme. On the AEC\n-- submission website (online or inside the VM), follow the link\n-- \"view the Agda code in their browser\" to the file\n-- agda\/README.html.\n--\n-- The source code is syntax highlighted and hyperlinked. You can\n-- click on module names to open the corresponding files, or you\n-- can click on identifiers to jump to their definition. In\n-- general, a Agda file with name `Foo\/Bar\/Baz.agda` contains a\n-- module `Foo.Bar.Baz` and is shown in an HTML file\n-- `Foo.Bar.Baz.html`.\n--\n-- Note that we also include the HTML files generated for our\n-- transitive dependencies from the Agda standard library. This\n-- allows you to follow hyperlinks to the Agda standard\n-- library. It is the default behavior of `agda --html` which we\n-- used to generate the HTML.\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE EMACS\n-- ========================\n--\n-- Open this file in Emacs with agda2-mode installed. See below\n-- for which Agda version you need to install. On the VM image,\n-- everything is setup for you and you can just open this file in\n-- Emacs.\n--\n-- C-c C-l Load code. Type checks and syntax highlights. Use\n-- this if the code is not syntax highlighted, or\n-- after you changed something that you want to type\n-- check.\n--\n-- M-. Jump to definition of identifier at the point.\n-- M-* Jump back.\n--\n-- Note that README.agda imports Everything.agda which imports\n-- every Agda file in our formalization. So if README.agda type\n-- checks successfully, everything does. If you want to type\n-- check everything from scratch, delete the *.agdai files to\n-- disable separate compilation. You can use \"find . -name\n-- '*.agdai' | xargs rm\" to do that.\n--\n-- More information on the Agda mode is available on the Agda wiki:\n--\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Docs.EmacsModeKeyCombinations\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE YOUR OWN SETUP\n-- =================================\n--\n-- To typecheck this formalization, you need to install the appropriate version\n-- of Agda, the Agda standard library (version 0.7), generate Everything.agda\n-- with the attached Haskell helper, and finally run Agda on this file.\n--\n-- Given a Unix-like environment (including Cygwin), running the .\/agdaCheck.sh\n-- script and following instructions given on output will eventually generate\n-- Everything.agda and proceed to type check everything on command line.\n--\n-- We use Agda HEAD from September 2013; Agda 2.3.2.1 might happen to work, but\n-- has some bugs with serialization of code using some recent syntactic sugar\n-- which we use (https:\/\/code.google.com\/p\/agda\/issues\/detail?id=756), so it\n-- might work or not. When it does not, removing Agda caches (.agdai files)\n-- appears to often help. You can use \"find . -name '*.agdai' | xargs rm\" to do\n-- that.\n--\n-- If you're not an Agda power user, it is probably easier to use\n-- the VM image or look at the pretty-printed and hyperlinked\n-- HTML files, see above.\n\n\n\n-- WHERE TO START READING?\n-- =======================\n--\n-- modules.pdf\n-- The graph of dependencies between Agda modules.\n-- Good if you want to get a broad overview.\n--\n-- README.agda\n-- This file. A coarse-grained introduction to the Agda\n-- formalization. Good if you want to begin at the beginning\n-- and understand the structure of our code.\n--\n-- PLDI14-List-of-Theorems.agda\n-- Pointers to the Agda formalizations of all theorems, lemmas\n-- or definitions in the PLDI paper. Good if you want to read\n-- the paper and the Agda code side by side.\n--\n-- Here is an import of this file, so you can jump to it\n-- directly (use M-. in Emacs or click the module name in the\n-- Browser):\n\nimport PLDI14-List-of-Theorems\n\n-- Everything.agda\n-- Imports every Agda module in our formalization. Good if you\n-- want to make sure you don't miss anything.\n--\n-- Again, here's is an import of this file so you can navigate\n-- there:\n\nimport Everything\n\n-- (This import is also important to ensure that if we typecheck\n-- README.agda, we also typecheck every other file of our\n-- formalization).\n\n\n\n-- THE AGDA CODE\n-- =============\n\nimport Postulate.Extensionality\n\nimport Base.Data.DependentList\n\n-- Variables and contexts\nimport Base.Syntax.Context\n\n-- Sets of variables\nimport Base.Syntax.Vars\n\nimport Base.Denotation.Notation\n\n-- Environments\nimport Base.Denotation.Environment\n\n-- Change contexts\nimport Base.Change.Context\n\n-- # Base, parametric proof.\n--\n-- This is for a parametric calculus where:\n-- types are parametric in base types\n-- terms are parametric in constants\n--\n--\n-- Modules are ordered and grouped according to what they represent.\n\n-- ## Definitions\n\nimport Parametric.Syntax.Type\nimport Parametric.Syntax.Term\n\nimport Parametric.Denotation.Value\nimport Parametric.Denotation.Evaluation\n\nimport Parametric.Change.Type\nimport Parametric.Change.Term\n\nimport Parametric.Change.Derive\n\nimport Parametric.Change.Value\nimport Parametric.Change.Evaluation\n\n-- ## Proofs\n\nimport Parametric.Change.Validity\nimport Parametric.Change.Specification\nimport Parametric.Change.Implementation\nimport Parametric.Change.Correctness\n\n-- # Nehemiah plugin\n--\n-- The structure is the same as the parametric proof (down to the\n-- order and the grouping of modules), except for the postulate module.\n\n-- Postulate an abstract data type for integer Bags.\nimport Postulate.Bag-Nehemiah\n\n-- ## Definitions\nimport Nehemiah.Syntax.Type\nimport Nehemiah.Syntax.Term\n\nimport Nehemiah.Denotation.Value\nimport Nehemiah.Denotation.Evaluation\n\nimport Nehemiah.Change.Term\nimport Nehemiah.Change.Type\n\nimport Nehemiah.Change.Derive\n\nimport Nehemiah.Change.Value\nimport Nehemiah.Change.Evaluation\n\n-- ## Proofs\n\nimport Nehemiah.Change.Validity\nimport Nehemiah.Change.Specification\nimport Nehemiah.Change.Implementation\nimport Nehemiah.Change.Correctness\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Machine-checked formalization of the theoretical results presented\n-- in the paper:\n--\n-- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann.\n-- A Theory of Changes for Higher-Order Languages:\n-- Incrementalizing \u03bb-Calculi by Static Differentiation.\n-- To appear at PLDI, ACM 2014.\n--\n-- We claim that this formalization\n--\n-- (1) proves every lemma and theorem in Sec. 2 and 3 of the paper,\n-- (2) and formally specifies the interface between our incrementalization\n-- framework and potential plugins.\n--\n-- The first claim is the main reason for a machine-checked\n-- proof: We want to be sure that we got the proofs right.\n--\n-- The second claim is about reusability and applicability: Only\n-- a clearly defined interface allows other researchers to\n-- provide plugins for our framework.\n------------------------------------------------------------------------\n\nmodule README where\n\n-- We know two good ways to read this code base. You can either\n-- use Emacs with agda2-mode to interact with the source files,\n-- or you can use a web browser to view the pretty-printed and\n-- hyperlinked source files. For Agda power users, we also\n-- include basic setup information for your own machine.\n\n\n-- IF YOU WANT TO USE A BROWSER\n-- ============================\n--\n-- Start with *HTML* version of this readme. On the AEC\n-- submission website (online or inside the VM), follow the link\n-- \"view the Agda code in their browser\" to the file\n-- agda\/README.html.\n--\n-- The source code is syntax highlighted and hyperlinked. You can\n-- click on module names to open the corresponding files, or you\n-- can click on identifiers to jump to their definition. In\n-- general, a Agda file with name `Foo\/Bar\/Baz.agda` contains a\n-- module `Foo.Bar.Baz` and is shown in an HTML file\n-- `Foo.Bar.Baz.html`.\n--\n-- Note that we also include the HTML files generated for our\n-- transitive dependencies from the Agda standard library. This\n-- allows you to follow hyperlinks to the Agda standard\n-- library. It is the default behavior of `agda --html` which we\n-- used to generate the HTML.\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE EMACS\n-- ========================\n--\n-- Open this file in Emacs with agda2-mode installed. See below\n-- for which Agda version you need to install. On the VM image,\n-- everything is setup for you and you can just open this file in\n-- Emacs.\n--\n-- C-c C-l Load code. Type checks and syntax highlights. Use\n-- this if the code is not syntax highlighted, or\n-- after you changed something that you want to type\n-- check.\n--\n-- M-. Jump to definition of identifier at the point.\n-- M-* Jump back.\n--\n-- Note that README.agda imports Everything.agda which imports\n-- every Agda file in our formalization. So if README.agda type\n-- checks successfully, everything does. If you want to type\n-- check everything from scratch, delete the *.agdai files to\n-- disable separate compilation. You can use \"find . -name\n-- '*.agdai' | xargs rm\" to do that.\n--\n-- More information on the Agda mode is available on the Agda wiki:\n--\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Docs.EmacsModeKeyCombinations\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE YOUR OWN SETUP\n-- =================================\n--\n-- To typecheck this formalization, you need to install the appropriate version\n-- of Agda, the Agda standard library (version 0.7), generate Everything.agda\n-- with the attached Haskell helper, and finally run Agda on this file.\n--\n-- Given a Unix-like environment (including Cygwin), running the .\/agdaCheck.sh\n-- script and following instructions given on output will eventually generate\n-- Everything.agda and proceed to type check everything on command line.\n--\n-- We use Agda HEAD from September 2013; Agda 2.3.2.1 might happen to work, but\n-- has some bugs with serialization of code using some recent syntactic sugar\n-- which we use (https:\/\/code.google.com\/p\/agda\/issues\/detail?id=756), so it\n-- might work or not. When it does not, removing Agda caches (.agdai files)\n-- appears to often help. You can use \"find . -name '*.agdai' | xargs rm\" to do\n-- that.\n--\n-- If you're not an Agda power user, it is probably easier to use\n-- the VM image or look at the pretty-printed and hyperlinked\n-- HTML files, see above.\n\n\n\n-- WHERE TO START READING?\n-- =======================\n--\n-- modules.pdf\n-- The graph of dependencies between Agda modules.\n-- Good if you want to get a broad overview.\n--\n-- README.agda\n-- This file. A coarse-grained introduction to the Agda\n-- formalization. Good if you want to begin at the beginning\n-- and understand the structure of our code.\n--\n-- PLDI14-List-of-Theorems.agda\n-- Pointers to the Agda formalizations of all theorems, lemmas\n-- or definitions in the PLDI paper. Good if you want to read\n-- the paper and the Agda code side by side.\n--\n-- Here is an import of this file, so you can jump to it\n-- directly (use M-. in Emacs or click the module name in the\n-- Browser):\n\nimport PLDI14-List-of-Theorems\n\n-- Everything.agda\n-- Imports every Agda module in our formalization. Good if you\n-- want to make sure you don't miss anything.\n--\n-- Again, here's is an import of this file so you can navigate\n-- there:\n\nimport Everything\n\n-- (This import is also important to ensure that if we typecheck\n-- README.agda, we also typecheck every other file of our\n-- formalization).\n\n\n\n-- THE AGDA CODE\n-- =============\n\nimport Postulate.Extensionality\n\nimport Base.Data.DependentList\n\n-- Variables and contexts\nimport Base.Syntax.Context\n\n-- Sets of variables\nimport Base.Syntax.Vars\n\nimport Base.Denotation.Notation\n\n-- Environments\nimport Base.Denotation.Environment\n\n-- Change contexts\nimport Base.Change.Context\n\n-- # Base, parametric proof.\n--\n-- This is for a parametric calculus where:\n-- types are parametric in base types\n-- terms are parametric in constants\n--\n--\n-- Modules are ordered and grouped according to what they represent.\n\n-- ## Definitions\n\nimport Parametric.Syntax.Type\nimport Parametric.Syntax.Term\n\nimport Parametric.Denotation.Value\nimport Parametric.Denotation.Evaluation\n\nimport Parametric.Change.Type\nimport Parametric.Change.Term\n\nimport Parametric.Change.Derive\n\nimport Parametric.Change.Value\nimport Parametric.Change.Evaluation\n\n-- ## Proofs\n\nimport Parametric.Change.Validity\nimport Parametric.Change.Specification\nimport Parametric.Change.Implementation\nimport Parametric.Change.Correctness\n\n-- # Nehemiah plugin\n--\n-- The structure is the same as the parametric proof (down to the\n-- order and the grouping of modules), except for the postulate module.\n\n-- Postulate an abstract data type for integer Bags.\nimport Postulate.Bag-Nehemiah\n\n-- ## Definitions\nimport Nehemiah.Syntax.Type\nimport Nehemiah.Syntax.Term\n\nimport Nehemiah.Denotation.Value\nimport Nehemiah.Denotation.Evaluation\n\nimport Nehemiah.Change.Term\nimport Nehemiah.Change.Type\n\nimport Nehemiah.Change.Derive\n\nimport Nehemiah.Change.Value\nimport Nehemiah.Change.Evaluation\n\n-- ## Proofs\n\nimport Nehemiah.Change.Validity\nimport Nehemiah.Change.Specification\nimport Nehemiah.Change.Implementation\nimport Nehemiah.Change.Correctness\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"23339dd5a9e56c3c31d42252c4020b716ec77725","subject":"Update references to paper","message":"Update references to paper\n\nDuring revision, I added Lemma 2.5 to the paper, changing the numbering.\nThis commits updates the numbering in the formalization.\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Algebra.agda","new_file":"Base\/Change\/Algebra.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Change Structures.\n--\n-- This module defines the notion of change structures,\n-- as well as change structures for groups, functions and lists.\n--\n-- This module corresponds to Section 2 of the PLDI paper.\n------------------------------------------------------------------------\n\nmodule Base.Change.Algebra where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Change Algebras\n-- ===============\n--\n-- In the paper, change algebras are called \"change structures\"\n-- and they are described in Section 2.1. We follow the design of\n-- the Agda standard library and define change algebras as two\n-- records, so Definition 2.1 from the PLDI paper maps to the\n-- records IsChangeAlgebra and ChangeAlgebra.\n--\n-- A value of type (IsChangeAlgebra Change update diff) proves that\n-- Change, update and diff together form a change algebra.\n--\n-- A value of type (ChangeAlgebra Carrier) contains the necessary\n-- ingredients to create a change algebra for a carrier set.\n--\n-- In the paper, Carrier is called V (Def. 2.1a),\n-- Change is called \u0394 (Def. 2.1b),\n-- update is written as infix \u2295 (Def. 2.1c),\n-- diff is written as infix \u229d (Def. 2.1d),\n-- and update-diff is specified in Def. 2.1e.\n\nrecord IsChangeAlgebra\n {c} {d}\n {Carrier : Set c}\n (Change : Carrier \u2192 Set d)\n (update : (v : Carrier) (dv : Change v) \u2192 Carrier)\n (diff : (u v : Carrier) \u2192 Change v) : Set (c \u2294 d) where\n field\n update-diff : \u2200 u v \u2192 update v (diff u v) \u2261 u\n\n -- In the paper, this is Def. 2.2.\n nil : \u2200 v \u2192 Change v\n nil v = diff v v\n\n -- In the paper, this is Lemma 2.3.\n update-nil : \u2200 v \u2192 update v (nil v) \u2261 v\n update-nil v = update-diff v v\n\n -- abbreviations\n before : \u2200 {v} \u2192 Change v \u2192 Carrier\n before {v} _ = v\n\n after : \u2200 {v} \u2192 Change v \u2192 Carrier\n after {v} dv = update v dv\n\nrecord ChangeAlgebra {c} \u2113\n (Carrier : Set c) : Set (c \u2294 suc \u2113) where\n field\n Change : Carrier \u2192 Set \u2113\n update : (v : Carrier) (dv : Change v) \u2192 Carrier\n diff : (u v : Carrier) \u2192 Change v\n\n isChangeAlgebra : IsChangeAlgebra Change update diff\n\n\n infixl 6 update diff\n\n open IsChangeAlgebra isChangeAlgebra public\n\n-- The following open ... public statement lets us use infix \u229e\n-- and \u229f for update and diff. In the paper, we use infix \u2295 and\n-- \u229d.\n\nopen ChangeAlgebra {{...}} public\n using\n ( update-diff\n ; update-nil\n ; nil\n ; before\n ; after\n )\n renaming\n ( Change to \u0394\n ; update to _\u229e_\n ; diff to _\u229f_\n )\n\n-- Families of Change Algebras\n-- ===========================\n--\n-- This is some Agda machinery to allow subscripting change\n-- algebra operations to avoid ambiguity. In the paper,\n-- we simply write (in the paragraph after Def. 2.1):\n--\n-- We overload operators \u2206, \u229d and \u2295 to refer to the\n-- corresponding operations of different change structures; we\n-- will subscript these symbols when needed to prevent\n-- ambiguity.\n--\n-- The following definitions implement this idea formally.\n\nrecord ChangeAlgebraFamily {a p} \u2113 {A : Set a} (P : A \u2192 Set p): Set (suc \u2113 \u2294 p \u2294 a) where\n constructor\n family\n field\n change-algebra : \u2200 x \u2192 ChangeAlgebra \u2113 (P x)\n\n module _ x where\n open ChangeAlgebra (change-algebra x) public\n\nmodule Family = ChangeAlgebraFamily {{...}}\n\nopen Family public\n using\n (\n )\n renaming\n ( Change to \u0394\u208d_\u208e\n ; nil to nil\u208d_\u208e\n ; update-diff to update-diff\u208d_\u208e\n ; update-nil to update-nil\u208d_\u208e\n ; change-algebra to change-algebra\u208d_\u208e\n ; before to before\u208d_\u208e\n ; after to after\u208d_\u208e\n )\n\ninfixl 6 update\u2032 diff\u2032\n\nupdate\u2032 = Family.update\nsyntax update\u2032 x v dv = v \u229e\u208d x \u208e dv\n\ndiff\u2032 = Family.diff\nsyntax diff\u2032 x u v = u \u229f\u208d x \u208e v\n\n-- Derivatives\n-- ===========\n--\n-- This corresponds to Def. 2.4 in the paper.\n\nDerivative : \u2200 {a b c d} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra c A}} \u2192\n {{CB : ChangeAlgebra d B}} \u2192\n (f : A \u2192 B) \u2192\n (df : (a : A) (da : \u0394 a) \u2192 \u0394 (f a)) \u2192\n Set (a \u2294 b \u2294 c)\nDerivative f df = \u2200 a da \u2192 f a \u229e df a da \u2261 f (a \u229e da)\n\n-- This is a variant of Derivative for change algebra families.\nDerivative\u208d_,_\u208e : \u2200 {a b p q c d} {A : Set a} {B : Set b} {P : A \u2192 Set p} {Q : B \u2192 Set q} \u2192\n {{CP : ChangeAlgebraFamily c P}} \u2192\n {{CQ : ChangeAlgebraFamily d Q}} \u2192\n (x : A) \u2192\n (y : B) \u2192\n (f : P x \u2192 Q y) \u2192\n (df : (px : P x) (dpx : \u0394\u208d x \u208e px) \u2192 \u0394\u208d y \u208e (f px)) \u2192\n Set (p \u2294 q \u2294 c)\nDerivative\u208d x , y \u208e f df = Derivative f df where\n CPx = change-algebra\u208d x \u208e\n CQy = change-algebra\u208d y \u208e\n\n-- Abelian Groups Induce Change Algebras\n-- =====================================\n--\n-- In the paper, as the first example for change structures after\n-- Def. 2.1, we mention that \"each abelian group ... induces a\n-- change structure\". This is the formalization of this result.\n--\n-- The module GroupChanges below takes a proof that A forms an\n-- abelian group and provides a changeAlgebra for A. The proof of\n-- Def 2.1e is by equational reasoning using the group axioms, in\n-- the definition of changeAlgebra.isChangeAlgebra.update-diff.\n\nopen import Algebra.Structures\nopen import Data.Product\nopen import Function\n\nmodule GroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isAbelianGroup : IsAbelianGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsAbelianGroup isAbelianGroup\n hiding\n ( refl\n )\n renaming\n ( _-_ to _\u229d_\n ; \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n changeAlgebra : ChangeAlgebra a A\n changeAlgebra = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8 comm _ _ \u27e9\n (u \u229d v ) \u2295 v\n \u2261\u27e8\u27e9\n (u \u2295 (v \u207b\u00b9)) \u2295 v\n \u2261\u27e8 assoc _ _ _ \u27e9\n u \u2295 ((v \u207b\u00b9) \u2295 v)\n \u2261\u27e8 refl \u27e8\u2295\u27e9 proj\u2081 inverse v \u27e9\n u \u2295 \u03b5\n \u2261\u27e8 proj\u2082 identity u \u27e9\n u\n \u220e\n }\n }\n\n-- Function Changes\n-- ================\n--\n-- This is one of our most important results: Change structures\n-- can be lifted to function spaces. We formalize this as a module\n-- FunctionChanges that takes the change algebras for A and B as\n-- arguments, and provides a changeAlgebra for (A \u2192 B). The proofs\n-- are by equational reasoning using 2.1e for A and B.\n\nmodule FunctionChanges\n {a} {b} {c} {d} (A : Set a) (B : Set b) {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}}\n where\n -- This corresponds to Definition 2.6 in the paper.\n record FunctionChange (f : A \u2192 B) : Set (a \u2294 b \u2294 c \u2294 d) where\n constructor\n cons\n field\n -- Definition 2.6a\n apply : (a : A) (da : \u0394 a) \u2192\n \u0394 (f a)\n\n -- Definition 2.6b.\n -- (for some reason, the version in the paper has the arguments of \u2261\n -- flipped. Since \u2261 is symmetric, this doesn't matter).\n correct : (a : A) (da : \u0394 a) \u2192\n f (a \u229e da) \u229e apply (a \u229e da) (nil (a \u229e da)) \u2261 f a \u229e apply a da\n\n open FunctionChange public\n open \u2261-Reasoning\n open import Postulate.Extensionality\n\n changeAlgebra : ChangeAlgebra (a \u2294 b \u2294 c \u2294 d) (A \u2192 B)\n changeAlgebra = record\n { Change = FunctionChange\n -- in the paper, update and diff below are in Def. 2.7\n ; update = \u03bb f df a \u2192 f a \u229e apply df a (nil a)\n ; diff = \u03bb g f \u2192 record\n { apply = \u03bb a da \u2192 g (a \u229e da) \u229f f a\n -- the proof of correct is the first half of what we\n -- have to prove for Theorem 2.7.\n ; correct = \u03bb a da \u2192\n begin\n f (a \u229e da) \u229e (g ((a \u229e da) \u229e nil (a \u229e da)) \u229f f (a \u229e da))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f (a \u229e da) \u229e (g \u25a1 \u229f f (a \u229e da)))\n (update-nil (a \u229e da)) \u27e9\n f (a \u229e da) \u229e (g (a \u229e da) \u229f f (a \u229e da))\n \u2261\u27e8 update-diff (g (a \u229e da)) (f (a \u229e da)) \u27e9\n g (a \u229e da)\n \u2261\u27e8 sym (update-diff (g (a \u229e da)) (f a)) \u27e9\n f a \u229e (g (a \u229e da) \u229f f a)\n \u220e\n }\n ; isChangeAlgebra = record\n -- the proof of update-diff is the second half of what\n -- we have to prove for Theorem 2.8.\n { update-diff = \u03bb g f \u2192 ext (\u03bb a \u2192\n begin\n f a \u229e (g (a \u229e nil a) \u229f f a)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f a \u229e (g \u25a1 \u229f f a)) (update-nil a) \u27e9\n f a \u229e (g a \u229f f a)\n \u2261\u27e8 update-diff (g a) (f a) \u27e9\n g a\n \u220e)\n }\n }\n\n -- This is Theorem 2.9 in the paper.\n incrementalization : \u2200 (f : A \u2192 B) df a da \u2192\n (f \u229e df) (a \u229e da) \u2261 f a \u229e apply df a da\n incrementalization f df a da = correct df a da\n\n -- This is Theorem 2.10 in the paper.\n nil-is-derivative : \u2200 (f : A \u2192 B) \u2192\n Derivative f (apply (nil f))\n nil-is-derivative f a da =\n begin\n f a \u229e apply (nil f) a da\n \u2261\u27e8 sym (incrementalization f (nil f) a da) \u27e9\n (f \u229e nil f) (a \u229e da)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (a \u229e da))\n (update-nil f) \u27e9\n f (a \u229e da)\n \u220e\n\n-- List (== Environment) Changes\n-- =============================\n--\n-- Here, we define a change structure on environments, given a\n-- change structure on the values in the environments. In the\n-- paper, we describe this in Definition 3.5. But note that this\n-- Agda formalization uses de Bruijn indices instead of names, so\n-- environments are just lists. Therefore, when we use Definition\n-- 3.5 in the paper, in this formalization, we use the list-like\n-- change structure defined here.\n\nopen import Data.List\nopen import Data.List.All\n\ndata All\u2032 {a p q} {A : Set a}\n {P : A \u2192 Set p}\n (Q : {x : A} \u2192 P x \u2192 Set q)\n : {xs : List A} (pxs : All P xs) \u2192 Set (p \u2294 q \u2294 a) where\n [] : All\u2032 Q []\n _\u2237_ : \u2200 {x xs} {px : P x} {pxs : All P xs} (qx : Q px) (qxs : All\u2032 Q pxs) \u2192 All\u2032 Q (px \u2237 pxs)\n\nmodule ListChanges\n {a} {c} {A : Set a} (P : A \u2192 Set) {{C : ChangeAlgebraFamily c P}}\n where\n update-all : \u2200 {xs} \u2192 (pxs : All P xs) \u2192 All\u2032 (\u0394\u208d _ \u208e) pxs \u2192 All P xs\n update-all {[]} [] [] = []\n update-all {x \u2237 xs} (px \u2237 pxs) (dpx \u2237 dpxs) = (px \u229e\u208d x \u208e dpx) \u2237 update-all pxs dpxs\n\n diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 All\u2032 (\u0394\u208d _ \u208e) pxs\n diff-all [] [] = []\n diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = (px\u2032 \u229f\u208d _ \u208e px) \u2237 diff-all pxs\u2032 pxs\n\n update-diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 update-all pxs (diff-all pxs\u2032 pxs) \u2261 pxs\u2032\n update-diff-all [] [] = refl\n update-diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = cong\u2082 _\u2237_ (update-diff\u208d _ \u208e px\u2032 px) (update-diff-all pxs\u2032 pxs)\n\n changeAlgebra : ChangeAlgebraFamily (c \u2294 a) (All P)\n changeAlgebra = record\n { change-algebra = \u03bb xs \u2192 record\n { Change = All\u2032 \u0394\u208d _ \u208e\n ; update = update-all\n ; diff = diff-all\n ; isChangeAlgebra = record\n { update-diff = update-diff-all\n }\n }\n }\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Change Structures.\n--\n-- This module defines the notion of change structures,\n-- as well as change structures for groups, functions and lists.\n--\n-- This module corresponds to Section 2 of the PLDI paper.\n------------------------------------------------------------------------\n\nmodule Base.Change.Algebra where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Change Algebras\n-- ===============\n--\n-- In the paper, change algebras are called \"change structures\"\n-- and they are described in Section 2.1. We follow the design of\n-- the Agda standard library and define change algebras as two\n-- records, so Definition 2.1 from the PLDI paper maps to the\n-- records IsChangeAlgebra and ChangeAlgebra.\n--\n-- A value of type (IsChangeAlgebra Change update diff) proves that\n-- Change, update and diff together form a change algebra.\n--\n-- A value of type (ChangeAlgebra Carrier) contains the necessary\n-- ingredients to create a change algebra for a carrier set.\n--\n-- In the paper, Carrier is called V (Def. 2.1a),\n-- Change is called \u0394 (Def. 2.1b),\n-- update is written as infix \u2295 (Def. 2.1c),\n-- diff is written as infix \u229d (Def. 2.1d),\n-- and update-diff is specified in Def. 2.1e.\n\nrecord IsChangeAlgebra\n {c} {d}\n {Carrier : Set c}\n (Change : Carrier \u2192 Set d)\n (update : (v : Carrier) (dv : Change v) \u2192 Carrier)\n (diff : (u v : Carrier) \u2192 Change v) : Set (c \u2294 d) where\n field\n update-diff : \u2200 u v \u2192 update v (diff u v) \u2261 u\n\n -- In the paper, this is Def. 2.2.\n nil : \u2200 v \u2192 Change v\n nil v = diff v v\n\n -- In the paper, this is Lemma 2.3.\n update-nil : \u2200 v \u2192 update v (nil v) \u2261 v\n update-nil v = update-diff v v\n\n -- abbrevitations\n before : \u2200 {v} \u2192 Change v \u2192 Carrier\n before {v} _ = v\n\n after : \u2200 {v} \u2192 Change v \u2192 Carrier\n after {v} dv = update v dv\n\nrecord ChangeAlgebra {c} \u2113\n (Carrier : Set c) : Set (c \u2294 suc \u2113) where\n field\n Change : Carrier \u2192 Set \u2113\n update : (v : Carrier) (dv : Change v) \u2192 Carrier\n diff : (u v : Carrier) \u2192 Change v\n\n isChangeAlgebra : IsChangeAlgebra Change update diff\n\n\n infixl 6 update diff\n\n open IsChangeAlgebra isChangeAlgebra public\n\n-- The following open ... public statement lets us use infix \u229e\n-- and \u229f for update and diff. In the paper, we use infix \u2295 and\n-- \u229d.\n\nopen ChangeAlgebra {{...}} public\n using\n ( update-diff\n ; update-nil\n ; nil\n ; before\n ; after\n )\n renaming\n ( Change to \u0394\n ; update to _\u229e_\n ; diff to _\u229f_\n )\n\n-- Families of Change Algebras\n-- ===========================\n--\n-- This is some Agda machinery to allow subscripting change\n-- algebra operations to avoid ambiguity. In the paper,\n-- we simply write (in the paragraph after Def. 2.1):\n--\n-- We overload operators \u2206, \u229d and \u2295 to refer to the\n-- corresponding operations of different change structures; we\n-- will subscript these symbols when needed to prevent\n-- ambiguity.\n--\n-- The following definitions implement this idea formally.\n\nrecord ChangeAlgebraFamily {a p} \u2113 {A : Set a} (P : A \u2192 Set p): Set (suc \u2113 \u2294 p \u2294 a) where\n constructor\n family\n field\n change-algebra : \u2200 x \u2192 ChangeAlgebra \u2113 (P x)\n\n module _ x where\n open ChangeAlgebra (change-algebra x) public\n\nmodule Family = ChangeAlgebraFamily {{...}}\n\nopen Family public\n using\n (\n )\n renaming\n ( Change to \u0394\u208d_\u208e\n ; nil to nil\u208d_\u208e\n ; update-diff to update-diff\u208d_\u208e\n ; update-nil to update-nil\u208d_\u208e\n ; change-algebra to change-algebra\u208d_\u208e\n ; before to before\u208d_\u208e\n ; after to after\u208d_\u208e\n )\n\ninfixl 6 update\u2032 diff\u2032\n\nupdate\u2032 = Family.update\nsyntax update\u2032 x v dv = v \u229e\u208d x \u208e dv\n\ndiff\u2032 = Family.diff\nsyntax diff\u2032 x u v = u \u229f\u208d x \u208e v\n\n-- Derivatives\n-- ===========\n--\n-- This corresponds to Def. 2.4 in the paper.\n\nDerivative : \u2200 {a b c d} {A : Set a} {B : Set b} \u2192\n {{CA : ChangeAlgebra c A}} \u2192\n {{CB : ChangeAlgebra d B}} \u2192\n (f : A \u2192 B) \u2192\n (df : (a : A) (da : \u0394 a) \u2192 \u0394 (f a)) \u2192\n Set (a \u2294 b \u2294 c)\nDerivative f df = \u2200 a da \u2192 f a \u229e df a da \u2261 f (a \u229e da)\n\n-- This is a variant of Derivative for change algebra families.\nDerivative\u208d_,_\u208e : \u2200 {a b p q c d} {A : Set a} {B : Set b} {P : A \u2192 Set p} {Q : B \u2192 Set q} \u2192\n {{CP : ChangeAlgebraFamily c P}} \u2192\n {{CQ : ChangeAlgebraFamily d Q}} \u2192\n (x : A) \u2192\n (y : B) \u2192\n (f : P x \u2192 Q y) \u2192\n (df : (px : P x) (dpx : \u0394\u208d x \u208e px) \u2192 \u0394\u208d y \u208e (f px)) \u2192\n Set (p \u2294 q \u2294 c)\nDerivative\u208d x , y \u208e f df = Derivative f df where\n CPx = change-algebra\u208d x \u208e\n CQy = change-algebra\u208d y \u208e\n\n-- Abelian Groups Induce Change Algebras\n-- =====================================\n--\n-- In the paper, as the first example for change structures after\n-- Def. 2.1, we mention that \"each abelian group ... induces a\n-- change structure\". This is the formalization of this result.\n--\n-- The module GroupChanges below takes a proof that A forms an\n-- abelian group and provides a changeAlgebra for A. The proof of\n-- Def 2.1e is by equational reasoning using the group axioms, in\n-- the definition of changeAlgebra.isChangeAlgebra.update-diff.\n\nopen import Algebra.Structures\nopen import Data.Product\nopen import Function\n\nmodule GroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isAbelianGroup : IsAbelianGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsAbelianGroup isAbelianGroup\n hiding\n ( refl\n )\n renaming\n ( _-_ to _\u229d_\n ; \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n changeAlgebra : ChangeAlgebra a A\n changeAlgebra = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8 comm _ _ \u27e9\n (u \u229d v ) \u2295 v\n \u2261\u27e8\u27e9\n (u \u2295 (v \u207b\u00b9)) \u2295 v\n \u2261\u27e8 assoc _ _ _ \u27e9\n u \u2295 ((v \u207b\u00b9) \u2295 v)\n \u2261\u27e8 refl \u27e8\u2295\u27e9 proj\u2081 inverse v \u27e9\n u \u2295 \u03b5\n \u2261\u27e8 proj\u2082 identity u \u27e9\n u\n \u220e\n }\n }\n\n-- Function Changes\n-- ================\n--\n-- This is one of our most important results: Change structures\n-- can be lifted to function spaces. We formalize this as a module\n-- FunctionChanges that takes the change algebras for A and B as\n-- arguments, and provides a changeAlgebra for (A \u2192 B). The proofs\n-- are by equational reasoning using 2.1e for A and B.\n\nmodule FunctionChanges\n {a} {b} {c} {d} (A : Set a) (B : Set b) {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}}\n where\n -- This corresponds to Definition 2.5 in the paper.\n record FunctionChange (f : A \u2192 B) : Set (a \u2294 b \u2294 c \u2294 d) where\n constructor\n cons\n field\n -- Definition 2.5a\n apply : (a : A) (da : \u0394 a) \u2192\n \u0394 (f a)\n\n -- Definition 2.5b.\n -- (for some reason, the version in the paper has the arguments of \u2261\n -- flipped. Since \u2261 is symmetric, this doesn't matter).\n correct : (a : A) (da : \u0394 a) \u2192\n f (a \u229e da) \u229e apply (a \u229e da) (nil (a \u229e da)) \u2261 f a \u229e apply a da\n\n open FunctionChange public\n open \u2261-Reasoning\n open import Postulate.Extensionality\n\n changeAlgebra : ChangeAlgebra (a \u2294 b \u2294 c \u2294 d) (A \u2192 B)\n changeAlgebra = record\n { Change = FunctionChange\n -- in the paper, update and diff below are in Def. 2.6\n ; update = \u03bb f df a \u2192 f a \u229e apply df a (nil a)\n ; diff = \u03bb g f \u2192 record\n { apply = \u03bb a da \u2192 g (a \u229e da) \u229f f a\n -- the proof of correct is the first half of what we\n -- have to prove for Theorem 2.7.\n ; correct = \u03bb a da \u2192\n begin\n f (a \u229e da) \u229e (g ((a \u229e da) \u229e nil (a \u229e da)) \u229f f (a \u229e da))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f (a \u229e da) \u229e (g \u25a1 \u229f f (a \u229e da)))\n (update-nil (a \u229e da)) \u27e9\n f (a \u229e da) \u229e (g (a \u229e da) \u229f f (a \u229e da))\n \u2261\u27e8 update-diff (g (a \u229e da)) (f (a \u229e da)) \u27e9\n g (a \u229e da)\n \u2261\u27e8 sym (update-diff (g (a \u229e da)) (f a)) \u27e9\n f a \u229e (g (a \u229e da) \u229f f a)\n \u220e\n }\n ; isChangeAlgebra = record\n -- the proof of update-diff is the second half of what\n -- we have to prove for Theorem 2.7.\n { update-diff = \u03bb g f \u2192 ext (\u03bb a \u2192\n begin\n f a \u229e (g (a \u229e nil a) \u229f f a)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f a \u229e (g \u25a1 \u229f f a)) (update-nil a) \u27e9\n f a \u229e (g a \u229f f a)\n \u2261\u27e8 update-diff (g a) (f a) \u27e9\n g a\n \u220e)\n }\n }\n\n -- This is Lemma 2.8 in the paper.\n incrementalization : \u2200 (f : A \u2192 B) df a da \u2192\n (f \u229e df) (a \u229e da) \u2261 f a \u229e apply df a da\n incrementalization f df a da = correct df a da\n\n -- This is Theorem 2.9 in the paper.\n nil-is-derivative : \u2200 (f : A \u2192 B) \u2192\n Derivative f (apply (nil f))\n nil-is-derivative f a da =\n begin\n f a \u229e apply (nil f) a da\n \u2261\u27e8 sym (incrementalization f (nil f) a da) \u27e9\n (f \u229e nil f) (a \u229e da)\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 \u25a1 (a \u229e da))\n (update-nil f) \u27e9\n f (a \u229e da)\n \u220e\n\n-- List (== Environment) Changes\n-- =============================\n--\n-- Here, we define a change structure on environments, given a\n-- change structure on the values in the environments. In the\n-- paper, we describe this in Definition 3.5. But note that this\n-- Agda formalization uses de Bruijn indices instead of names, so\n-- environments are just lists. Therefore, when we use Definition\n-- 3.5 in the paper, in this formalization, we use the list-like\n-- change structure defined here.\n\nopen import Data.List\nopen import Data.List.All\n\ndata All\u2032 {a p q} {A : Set a}\n {P : A \u2192 Set p}\n (Q : {x : A} \u2192 P x \u2192 Set q)\n : {xs : List A} (pxs : All P xs) \u2192 Set (p \u2294 q \u2294 a) where\n [] : All\u2032 Q []\n _\u2237_ : \u2200 {x xs} {px : P x} {pxs : All P xs} (qx : Q px) (qxs : All\u2032 Q pxs) \u2192 All\u2032 Q (px \u2237 pxs)\n\nmodule ListChanges\n {a} {c} {A : Set a} (P : A \u2192 Set) {{C : ChangeAlgebraFamily c P}}\n where\n update-all : \u2200 {xs} \u2192 (pxs : All P xs) \u2192 All\u2032 (\u0394\u208d _ \u208e) pxs \u2192 All P xs\n update-all {[]} [] [] = []\n update-all {x \u2237 xs} (px \u2237 pxs) (dpx \u2237 dpxs) = (px \u229e\u208d x \u208e dpx) \u2237 update-all pxs dpxs\n\n diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 All\u2032 (\u0394\u208d _ \u208e) pxs\n diff-all [] [] = []\n diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = (px\u2032 \u229f\u208d _ \u208e px) \u2237 diff-all pxs\u2032 pxs\n\n update-diff-all : \u2200 {xs} \u2192 (pxs\u2032 pxs : All P xs) \u2192 update-all pxs (diff-all pxs\u2032 pxs) \u2261 pxs\u2032\n update-diff-all [] [] = refl\n update-diff-all (px\u2032 \u2237 pxs\u2032) (px \u2237 pxs) = cong\u2082 _\u2237_ (update-diff\u208d _ \u208e px\u2032 px) (update-diff-all pxs\u2032 pxs)\n\n changeAlgebra : ChangeAlgebraFamily (c \u2294 a) (All P)\n changeAlgebra = record\n { change-algebra = \u03bb xs \u2192 record\n { Change = All\u2032 \u0394\u208d _ \u208e\n ; update = update-all\n ; diff = diff-all\n ; isChangeAlgebra = record\n { update-diff = update-diff-all\n }\n }\n }\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3ee50b10e841b511ed0ac49f411f32075c8abc00","subject":"boolean and function composition","message":"boolean and function composition\n","repos":"shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps","old_file":"agda-tutorial\/basics.agda","new_file":"agda-tutorial\/basics.agda","new_contents":"data Bool : Set where\n true : Bool\n false : Bool\n\n\nnot : Bool \u2192 Bool\nnot true = false\nnot false = true\n\n\ndata \u2115 : Set where\n O : \u2115\n S : \u2115 \u2192 \u2115\n\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nO + a = a\nS a + b = S (a + b)\n\n_*_ : \u2115 \u2192 \u2115 \u2192 \u2115\nO * a = O\nS a * b = a + (a * b)\n\n_or_ : Bool \u2192 Bool \u2192 Bool\ntrue or _ = true\nfalse or b = b\n\n\nif_then_else_ : {A : Set} \u2192 Bool \u2192 A \u2192 A \u2192 A\nif true then x else y = x\nif false then x else y = y\n\n\ninfixl 60 _*_\ninfixl 40 _+_\ninfixr 20 _or_\ninfix 5 if_then_else_\n\n\ninfixr 40 _::_\ndata List (A : Set) : Set where\n [] : List A\n _::_ : A -> List A -> List A\n\n\n_\u2218_ : {A : Set} -> {B : A -> Set} -> {C : (x : A) -> B x -> Set} ->\n (f : {x : A} -> (y : B x) -> C x y) -> (g : (x : A) -> B x) ->\n (x : A) -> C x (g x)\n_\u2218_ f g a = f (g a)\n\n\nplus-two = S \u2218 S\n","old_contents":"data Bool : Set where\n true : Bool\n false : Bool\n\n\nnot : Bool \u2192 Bool\nnot true = false\nnot false = true\n\n\ndata \u2115 : Set where\n O : \u2115\n S : \u2115 \u2192 \u2115\n\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nO + a = a\nS a + b = S (a + b)\n\n_*_ : \u2115 \u2192 \u2115 \u2192 \u2115\nO * a = O\nS a * b = a + (a * b)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"85137cba040f5becff87d5865659e66d93a6216c","subject":"[ doc ] Added reference for non-redundant group theory axioms.","message":"[ doc ] Added reference for non-redundant group theory axioms.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/GroupTheory\/Base.agda","new_file":"src\/fot\/GroupTheory\/Base.agda","new_contents":"------------------------------------------------------------------------------\n-- Group theory base\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule GroupTheory.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfix 11 _\u207b\u00b9\ninfixl 10 _\u00b7_ -- The symbol is '\\cdot'.\n\n------------------------------------------------------------------------------\n-- First-order logic with equality.\nopen import Common.FOL.FOL-Eq public renaming ( D to G )\n\n-- Group theory axioms\npostulate\n \u03b5 : G -- The identity element.\n _\u00b7_ : G \u2192 G \u2192 G -- The binary operation.\n _\u207b\u00b9 : G \u2192 G -- The inverse function.\n\n -- We choose a non-redundant set of axioms. See for example (Mac\n -- Lane and Garret 1999, exercises 5-7, p. 50-51, or Hodges 1993,\n -- p. 37).\n assoc : \u2200 a b c \u2192 a \u00b7 b \u00b7 c \u2261 a \u00b7 (b \u00b7 c)\n leftIdentity : \u2200 a \u2192 \u03b5 \u00b7 a \u2261 a\n leftInverse : \u2200 a \u2192 a \u207b\u00b9 \u00b7 a \u2261 \u03b5\n{-# ATP axiom assoc leftIdentity leftInverse #-}\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Hodges, W. (1993). Model Theory. Vol. 42. Encyclopedia of\n-- Mathematics and its Applications. Cambridge University Press.\n--\n-- Mac Lane, S. and Birkhof, G. (1999). Algebra. 3rd ed. AMS Chelsea\n-- Publishing.\n","old_contents":"------------------------------------------------------------------------------\n-- Group theory base\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule GroupTheory.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfix 11 _\u207b\u00b9\ninfixl 10 _\u00b7_ -- The symbol is '\\cdot'.\n\n------------------------------------------------------------------------------\n-- First-order logic with equality.\nopen import Common.FOL.FOL-Eq public renaming ( D to G )\n\n-- Group theory axioms\npostulate\n \u03b5 : G -- The identity element.\n _\u00b7_ : G \u2192 G \u2192 G -- The binary operation.\n _\u207b\u00b9 : G \u2192 G -- The inverse function.\n\n -- We choose a mininal set of axioms. See for example Saunders Mac\n -- Lane and Garret Birkhoff. Algebra. AMS Chelsea Publishing, 3rd\n -- edition, 1999. exercises 5-7, p. 50-51.\n assoc : \u2200 a b c \u2192 a \u00b7 b \u00b7 c \u2261 a \u00b7 (b \u00b7 c)\n leftIdentity : \u2200 a \u2192 \u03b5 \u00b7 a \u2261 a\n leftInverse : \u2200 a \u2192 a \u207b\u00b9 \u00b7 a \u2261 \u03b5\n{-# ATP axiom assoc leftIdentity leftInverse #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"75b597ce205190597a67bec4c3fe0525c78d9c71","subject":"Type: cleanup","message":"Type: cleanup\n","repos":"crypto-agda\/explore","old_file":"lib\/Explore\/Type.agda","new_file":"lib\/Explore\/Type.agda","new_contents":"{-# OPTIONS --without-K #-}\n-- The core types behind exploration functions\nmodule Explore.Type where\n\nopen import Level.NP\nopen import Type hiding (\u2605)\nopen import Function.NP\nopen import Function.Inverse using (_\u2194_)\nopen import Data.Nat.NP hiding (_\u2294_)\nopen import Data.Bit\nopen import Data.Bits\nopen import Data.Indexed\nopen import Algebra\nopen import Relation.Binary.NP\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Two using (\ud835\udfda; \u2713)\nopen import Data.Maybe.NP using (_\u2192?_)\nopen import Data.Fin using (Fin)\nimport Algebra.FunctionProperties.NP as FP\nopen FP using (Op\u2082)\nopen import Relation.Unary.Logical\nopen import Relation.Binary.Logical\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_; _\u2257_)\n\nmodule SgrpExtra {c \u2113} (sg : Semigroup c \u2113) where\n open Semigroup sg\n open Setoid-Reasoning (Semigroup.setoid sg) public\n C : \u2605 _\n C = Carrier\n _\u2248\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 C) \u2192 \u2605 _\n f \u2248\u00b0 g = \u2200 x \u2192 f x \u2248 g x\n _\u2219\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 C) \u2192 A \u2192 C\n (f \u2219\u00b0 g) x = f x \u2219 g x\n infixl 7 _-\u2219-_\n _-\u2219-_ : _\u2219_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_\n _-\u2219-_ = \u2219-cong\n\nmodule Sgrp {c \u2113} (sg : Semigroup c \u2113) where\n open Semigroup sg public\n open SgrpExtra sg public\n\nmodule RawMon {c} {C : \u2605 c} (rawMon : C \u00d7 Op\u2082 C) where\n \u03b5 = proj\u2081 rawMon\n _\u2219_ = proj\u2082 rawMon\n\nmodule Mon {c \u2113} (m : Monoid c \u2113) where\n open Monoid m public\n sg = semigroup\n open SgrpExtra semigroup public\n RawMon = C \u00d7 Op\u2082 C\n rawMon : RawMon\n rawMon = \u03b5 , _\u2219_\n\nmodule CMon {c \u2113} (cm : CommutativeMonoid c \u2113) where\n open CommutativeMonoid cm public\n sg = semigroup\n m = monoid\n open SgrpExtra sg public\n open FP _\u2248_\n\n \u2219-interchange : Interchange _\u2219_ _\u2219_\n \u2219-interchange = InterchangeFromAssocCommCong.\u2219-interchange\n isEquivalence\n _\u2219_ assoc comm (\u03bb _ \u2192 flip \u2219-cong refl)\n\nExplore : \u2200 \u2113 \u2192 \u2605\u2080 \u2192 \u2605 \u209b \u2113\nExplore \u2113 A = \u2200 {M : \u2605 \u2113} \u2192 (_\u2219_ : M \u2192 M \u2192 M) \u2192 (A \u2192 M) \u2192 M\n\nExplore\u2080 : \u2605\u2080 \u2192 \u2605\u2081\nExplore\u2080 = Explore _\n\nExplore\u2081 : \u2605\u2080 \u2192 \u2605\u2082\nExplore\u2081 = Explore _\n\n[Explore] : ([\u2605\u2080] [\u2192] [\u2605\u2081]) (Explore _)\n[Explore] A\u209a = \u2200\u27e8 M\u209a \u2236 [\u2605\u2080] \u27e9[\u2192] [Op\u2082] M\u209a [\u2192] (A\u209a [\u2192] M\u209a) [\u2192] M\u209a\n\n\u27e6Explore\u27e7 : (\u27e6\u2605\u2080\u27e7 \u27e6\u2192\u27e7 \u27e6\u2605\u2081\u27e7) (Explore _) (Explore _)\n\u27e6Explore\u27e7 A\u1d63 = \u2200\u27e8 M\u1d63 \u2236 \u27e6\u2605\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Op\u2082\u27e7 M\u1d63 \u27e6\u2192\u27e7 (A\u1d63 \u27e6\u2192\u27e7 M\u1d63) \u27e6\u2192\u27e7 M\u1d63\n\n\u27e6Explore\u27e7\u1d64 : \u2200 {\u2113} \u2192 (\u27e6\u2605\u2080\u27e7 \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 (\u209b \u2113)) (Explore \u2113) (Explore \u2113)\n\u27e6Explore\u27e7\u1d64 {\u2113} A\u1d63 = \u2200\u27e8 M\u1d63 \u2236 \u27e6\u2605\u27e7 \u2113 \u27e9\u27e6\u2192\u27e7 \u27e6Op\u2082\u27e7 M\u1d63 \u27e6\u2192\u27e7 (A\u1d63 \u27e6\u2192\u27e7 M\u1d63) \u27e6\u2192\u27e7 M\u1d63\n\n-- Trimmed down version of \u27e6Explore\u27e7\n\u27e6Explore\u27e7\u2081 : \u2200 {A : \u2605_ _} (A\u1d63 : A \u2192 A \u2192 \u2605_ _) \u2192 Explore _ A \u2192 \u2605\u2081\n\u27e6Explore\u27e7\u2081 A\u1d63 s = \u27e6Explore\u27e7 A\u1d63 s s\n\n_\u2219-explore_ : \u2200 {\u2113 A} \u2192 Explore \u2113 A \u2192 Explore \u2113 A \u2192 Explore \u2113 A\n(s\u2080 \u2219-explore s\u2081) _\u2219_ f = s\u2080 _\u2219_ f \u2219 s\u2081 _\u2219_ f\n\nconst-explore : \u2200 {\u2113 A} \u2192 A \u2192 Explore \u2113 A\nconst-explore x _ f = f x\n\nExploreInd : \u2200 p {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreInd p {\u2113} {A} srch =\n \u2200 (P : Explore \u2113 A \u2192 \u2605 p)\n (P\u2219 : \u2200 {s\u2080 s\u2081 : Explore \u2113 A} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb _\u2219_ f \u2192 s\u2080 _\u2219_ f \u2219 s\u2081 _\u2219_ f))\n (Pf : \u2200 x \u2192 P (\u03bb _ f \u2192 f x))\n \u2192 P srch\n\nconst-explore-ind : \u2200 {\u2113 p A} (x : A) \u2192 ExploreInd p (const-explore {\u2113} x)\nconst-explore-ind x _ _ Pf = Pf x\n\n{-\n_\u2219ExploreInd_ : \u2200 {\u2113 p A} {s\u2080 s\u2081 : Explore \u2113 A}\n \u2192 ExploreInd p s\u2080 \u2192 ExploreInd p s\u2081\n \u2192 ExploreInd p (s\u2080 \u2219Explore s\u2081)\n_\u2219ExploreInd_ {s\u2080 = s\u2080} {s\u2081} Ps\u2080 Ps\u2081 P P\u2219 Pf = Ps\u2080 (\u03bb s \u2192 P s\u2081 \u2192 P (s \u2219Explore s\u2081)) (\u03bb {s\u2082} {s\u2083} Ps\u2082 Ps\u2083 Ps\u2081' \u2192 {!!}) (P\u2219 \u2218 Pf) (Ps\u2081 P P\u2219 Pf)\n-}\n\nrecord ExploreIndKit p {\u2113 A} (P : Explore \u2113 A \u2192 \u2605 p) : \u2605 (\u209b \u2113 \u2294 p) where\n constructor _,_\n field\n P\u2219 : \u2200 {s\u2080 s\u2081 : Explore \u2113 A} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (s\u2080 \u2219-explore s\u2081)\n Pf : \u2200 x \u2192 P (const-explore x)\n\n_$kit_ : \u2200 {p \u2113 A} {P : Explore \u2113 A \u2192 \u2605 p} {s : Explore \u2113 A}\n \u2192 ExploreInd p s \u2192 ExploreIndKit p P \u2192 P s\n_$kit_ {P = P} ind (P\u2219 , Pf) = ind P P\u2219 Pf\n\nExploreInd\u2080 : \u2200 {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreInd\u2080 = ExploreInd \u2080\n\nExploreInd\u2081 : \u2200 {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreInd\u2081 = ExploreInd \u2081\n\nExploreMon : \u2200 {c \u2113} \u2192 Monoid c \u2113 \u2192 \u2605\u2080 \u2192 \u2605 _\nExploreMon M A = (A \u2192 C) \u2192 C\n where open Mon M\n\nExploreMonInd : \u2200 p {c \u2113} {A} (M : Monoid c \u2113) \u2192 ExploreMon M A \u2192 \u2605 _\nExploreMonInd p {c} {\u2113} {A} M srch =\n \u2200 (P : ExploreMon M A \u2192 \u2605 p)\n (P\u2219 : \u2200 {s\u2080 s\u2081 : ExploreMon M A} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb f \u2192 s\u2080 f \u2219 s\u2081 f))\n (Pf : \u2200 x \u2192 P (\u03bb f \u2192 f x))\n (P\u2248 : \u2200 {s s'} \u2192 s \u2248\u00b0 s' \u2192 P s \u2192 P s')\n \u2192 P srch\n where open Mon M\n\nexplore\u2218FromExplore : \u2200 {m A} \u2192 Explore m A\n \u2192 \u2200 {M : \u2605 m} \u2192 (M \u2192 M \u2192 M) \u2192 (A \u2192 M) \u2192 (M \u2192 M)\nexplore\u2218FromExplore explore op f = explore _\u2218\u2032_ (op \u2218 f)\n\nExplorePlug : \u2200 {m \u2113 A} (M : Monoid m \u2113) (s : Explore _ A) \u2192 \u2605 _\nExplorePlug M s = \u2200 f x \u2192 s\u2218 _\u2219_ f \u03b5 \u2219 x \u2248 s\u2218 _\u2219_ f x\n where open Mon M\n s\u2218 = explore\u2218FromExplore s\n\n {-\nExploreMon : \u2200 m \u2192 \u2605\u2080 \u2192 \u2605 _\nExploreMon m A = \u2200 {M : \u2605 m} \u2192 M \u00d7 Op\u2082 M \u2192 (A \u2192 M) \u2192 M\n\nExploreMonInd : \u2200 p {\u2113} {A} \u2192 ExploreMon \u2113 A \u2192 \u2605 _\nExploreMonInd p {\u2113} {A} srch =\n \u2200 (P : ExploreMon _ A \u2192 \u2605 p)\n (P\u2219 : \u2200 {s\u2080 s\u2081 : ExploreMon _ A} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb M f \u2192 let _\u2219_ = proj\u2082 M in\n s\u2080 M f \u2219 s\u2081 M f))\n (Pf : \u2200 x \u2192 P (\u03bb _ f \u2192 f x))\n \u2192 P srch\n\nexploreMonFromExplore : \u2200 {\u2113 A}\n \u2192 Explore \u2113 A \u2192 ExploreMon \u2113 A\nexploreMonFromExplore s = s \u2218 proj\u2082\n -}\n\nexploreMonFromExplore : \u2200 {c \u2113 A}\n \u2192 Explore c A \u2192 (M : Monoid c \u2113) \u2192 ExploreMon M A\nexploreMonFromExplore s M = s _\u2219_ where open Mon M\n\nSum : \u2605\u2080 \u2192 \u2605\u2080\nSum A = (A \u2192 \u2115) \u2192 \u2115\n\nProduct : \u2605\u2080 \u2192 \u2605\u2080\nProduct A = (A \u2192 \u2115) \u2192 \u2115\n\nAdequateSum : \u2200 {A} \u2192 Sum A \u2192 \u2605\u2080\nAdequateSum {A} sum\u1d2c = \u2200 f \u2192 Fin (sum\u1d2c f) \u2194 \u03a3 A (Fin \u2218 f)\n\nAdequateProduct : \u2200 {A} \u2192 Product A \u2192 \u2605\u2080\nAdequateProduct {A} product\u1d2c = \u2200 f \u2192 Fin (product\u1d2c f) \u2194 \u03a0 A (Fin \u2218 f)\n\nCount : \u2605\u2080 \u2192 \u2605\u2080\nCount A = (A \u2192 Bit) \u2192 \u2115\n\nFind? : \u2605\u2080 \u2192 \u2605\u2081\nFind? A = \u2200 {B : \u2605\u2080} \u2192 (A \u2192? B) \u2192? B\n\nFindKey : \u2605\u2080 \u2192 \u2605\u2080\nFindKey A = (A \u2192 \ud835\udfda) \u2192? A\n\n_,-kit_ : \u2200 {m p A} {P : Explore m A \u2192 \u2605 p}{Q : Explore m A \u2192 \u2605 p}\n \u2192 ExploreIndKit p P \u2192 ExploreIndKit p Q \u2192 ExploreIndKit p (P \u00d7\u00b0 Q)\nPk ,-kit Qk = (\u03bb x y \u2192 P\u2219 Pk (proj\u2081 x) (proj\u2081 y) , P\u2219 Qk (proj\u2082 x) (proj\u2082 y))\n , (\u03bb x \u2192 Pf Pk x , Pf Qk x)\n where open ExploreIndKit\n\nExploreInd-Extra : \u2200 p {m A} \u2192 Explore m A \u2192 \u2605 _\nExploreInd-Extra p {m} {A} srch =\n \u2200 (Q : Explore m A \u2192 \u2605 p)\n (Q-kit : ExploreIndKit p Q)\n (P : Explore m A \u2192 \u2605 p)\n (P\u2219 : \u2200 {s\u2080 s\u2081 : Explore m A} \u2192 Q s\u2080 \u2192 Q s\u2081 \u2192 P s\u2080 \u2192 P s\u2081\n \u2192 P (s\u2080 \u2219-explore s\u2081))\n (Pf : \u2200 x \u2192 P (const-explore x))\n \u2192 P srch\n\nto-extra : \u2200 {p m A} {s : Explore m A} \u2192 ExploreInd p s \u2192 ExploreInd-Extra p s\nto-extra s-ind Q Q-kit P P\u2219 Pf =\n proj\u2082 (s-ind (Q \u00d7\u00b0 P)\n (\u03bb { (a , b) (c , d) \u2192 Q\u2219 a c , P\u2219 a c b d })\n (\u03bb x \u2192 Qf x , Pf x))\n where open ExploreIndKit Q-kit renaming (P\u2219 to Q\u2219; Pf to Qf)\n\nStableUnder : \u2200 {\u2113 A} \u2192 Explore \u2113 A \u2192 (A \u2192 A) \u2192 \u2605 _\nStableUnder explore p = \u2200 {M} op (f : _ \u2192 M) \u2192 explore op f \u2261 explore op (f \u2218 p)\n\nSumStableUnder : \u2200 {A} \u2192 Sum A \u2192 (A \u2192 A) \u2192 \u2605 _\nSumStableUnder sum p = \u2200 f \u2192 sum f \u2261 sum (f \u2218 p)\n\nCountStableUnder : \u2200 {A} \u2192 Count A \u2192 (A \u2192 A) \u2192 \u2605 _\nCountStableUnder count p = \u2200 f \u2192 count f \u2261 count (f \u2218 p)\n\n-- TODO: remove the hard-wired \u2261\nInjective : \u2200 {a b}{A : \u2605 a}{B : \u2605 b}(f : A \u2192 B) \u2192 \u2605 _\nInjective f = \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y\n\nSumStableUnderInjection : \u2200 {A} \u2192 Sum A \u2192 \u2605 _\nSumStableUnderInjection sum = \u2200 p \u2192 Injective p \u2192 SumStableUnder sum p\n\nSumInd : \u2200 {A} \u2192 Sum A \u2192 \u2605\u2081\nSumInd {A} sum = \u2200 (P : Sum A \u2192 \u2605\u2080)\n (P+ : \u2200 {s\u2080 s\u2081 : Sum A} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb f \u2192 s\u2080 f + s\u2081 f))\n (Pf : \u2200 x \u2192 P (\u03bb f \u2192 f x))\n \u2192 P sum\n\nExploreMono : \u2200 r {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreMono r s\u1d2c = \u2200 {C} (_\u2286_ : C \u2192 C \u2192 \u2605 r)\n {_\u2219_} (\u2219-mono : _\u2219_ Preserves\u2082 _\u2286_ \u27f6 _\u2286_ \u27f6 _\u2286_)\n {f g} \u2192\n (\u2200 x \u2192 f x \u2286 g x) \u2192 s\u1d2c _\u2219_ f \u2286 s\u1d2c _\u2219_ g\n\n\nExploreExtFun : \u2200 {A B} \u2192 Explore _ (A \u2192 B) \u2192 \u2605\u2081\nExploreExtFun {A}{B} s\u1d2c\u1d2e = \u2200 {M} op {f g : (A \u2192 B) \u2192 M} \u2192 (\u2200 {\u03c6 \u03c8} \u2192 \u03c6 \u2257 \u03c8 \u2192 f \u03c6 \u2261 g \u03c8) \u2192 s\u1d2c\u1d2e op f \u2261 s\u1d2c\u1d2e op g\n\nExploreSgExt : \u2200 r {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreSgExt r {\u2113} s\u1d2c = \u2200 (sg : Semigroup \u2113 r) {f g}\n \u2192 let open Sgrp sg in\n f \u2248\u00b0 g \u2192 s\u1d2c _\u2219_ f \u2248 s\u1d2c _\u2219_ g\n\nExploreExt : \u2200 {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreExt {\u2113} {A} s\u1d2c = \u2200 {M} op {f g : A \u2192 M} \u2192 f \u2257 g \u2192 s\u1d2c op f \u2261 s\u1d2c op g\n\nSumExt : \u2200 {A} \u2192 Sum A \u2192 \u2605 _\nSumExt sum\u1d2c = \u2200 {f g} \u2192 f \u2257 g \u2192 sum\u1d2c f \u2261 sum\u1d2c g\n\nCountExt : \u2200 {A} \u2192 Count A \u2192 \u2605 _\nCountExt count\u1d2c = \u2200 {f g} \u2192 f \u2257 g \u2192 count\u1d2c f \u2261 count\u1d2c g\n\nExplore\u03b5 : \u2200 \u2113 r {A} \u2192 Explore _ A \u2192 \u2605 _\nExplore\u03b5 \u2113 r s\u1d2c = \u2200 (m : Monoid \u2113 r) \u2192\n let open Mon m in\n s\u1d2c _\u2219_ (const \u03b5) \u2248 \u03b5\n\nSumZero : \u2200 {A} \u2192 Sum A \u2192 \u2605 _\nSumZero sum\u1d2c = sum\u1d2c (\u03bb _ \u2192 0) \u2261 0\n\nExploreLin\u02e1 : \u2200 \u2113 r {A} \u2192 Explore _ A \u2192 \u2605 _\nExploreLin\u02e1 \u2113 r s\u1d2c = \u2200 m _\u25ce_ f k \u2192\n let open Mon {\u2113} {r} m\n open FP _\u2248_ in\n _\u25ce_ DistributesOver\u02e1 _\u2219_ \u2192\n s\u1d2c _\u2219_ (\u03bb x \u2192 k \u25ce f x) \u2248 k \u25ce s\u1d2c _\u2219_ f\n\nExploreLin\u02b3 : \u2200 \u2113 r {A} \u2192 Explore _ A \u2192 \u2605 _\nExploreLin\u02b3 \u2113 r s\u1d2c =\n \u2200 m _\u25ce_ f k \u2192\n let open Mon {\u2113} {r} m\n open FP _\u2248_ in\n _\u25ce_ DistributesOver\u02b3 _\u2219_ \u2192\n s\u1d2c _\u2219_ (\u03bb x \u2192 f x \u25ce k) \u2248 s\u1d2c _\u2219_ f \u25ce k\n\nSumLin : \u2200 {A} \u2192 Sum A \u2192 \u2605 _\nSumLin sum\u1d2c = \u2200 f k \u2192 sum\u1d2c (\u03bb x \u2192 k * f x) \u2261 k * sum\u1d2c f\n\nExploreHom : \u2200 r {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreHom r s\u1d2c = \u2200 sg f g \u2192 let open Sgrp {_} {r} sg in\n s\u1d2c _\u2219_ (f \u2219\u00b0 g) \u2248 s\u1d2c _\u2219_ f \u2219 s\u1d2c _\u2219_ g\n\nExploreMonHom : \u2200 \u2113 r {A} \u2192 Explore _ A \u2192 \u2605 _\nExploreMonHom \u2113 r s\u1d2c =\n \u2200 cm f g \u2192\n let open CMon {\u2113} {r} cm in\n s\u1d2c _\u2219_ (f \u2219\u00b0 g) \u2248 s\u1d2c _\u2219_ f \u2219 s\u1d2c _\u2219_ g\n\nSumHom : \u2200 {A} \u2192 Sum A \u2192 \u2605 _\nSumHom sum\u1d2c = \u2200 f g \u2192 sum\u1d2c (\u03bb x \u2192 f x + g x) \u2261 sum\u1d2c f + sum\u1d2c g\n\nSumMono : \u2200 {A} \u2192 Sum A \u2192 \u2605 _\nSumMono sum\u1d2c = \u2200 {f g} \u2192 (\u2200 x \u2192 f x \u2264 g x) \u2192 sum\u1d2c f \u2264 sum\u1d2c g\n\nExploreSwap : \u2200 r {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreSwap r {\u2113} {A} s\u1d2c = \u2200 {B : \u2605\u2080} sg f \u2192\n let open Sgrp {_} {r} sg in\n \u2200 {s\u1d2e : (B \u2192 C) \u2192 C}\n \u2192 (hom : \u2200 f g \u2192 s\u1d2e (f \u2219\u00b0 g) \u2248 s\u1d2e f \u2219 s\u1d2e g)\n \u2192 s\u1d2c _\u2219_ (s\u1d2e \u2218 f) \u2248 s\u1d2e (s\u1d2c _\u2219_ \u2218 flip f)\n\nUnique : \u2200 {A} \u2192 Cmp A \u2192 Count A \u2192 \u2605 _\nUnique cmp count = \u2200 x \u2192 count (cmp x) \u2261 1\n\nmodule _ {\u2113 A} (A\u1d49 : Explore (\u209b \u2113) A) where\n Data\u03a0 : (A \u2192 \u2605 \u2113) \u2192 \u2605 \u2113\n Data\u03a0 = A\u1d49 _\u00d7_\n\n \u03a3Point : (A \u2192 \u2605 \u2113) \u2192 \u2605 \u2113\n \u03a3Point = A\u1d49 _\u228e_\n\nmodule _ {\u2113 A} (A\u1d49 : Explore (\u209b \u2113) A) where\n Lookup : \u2605 (\u209b \u2113)\n Lookup = \u2200 {P : A \u2192 \u2605 \u2113} \u2192 Data\u03a0 A\u1d49 P \u2192 \u03a0 A P\n\n Reify : \u2605 (\u209b \u2113)\n Reify = \u2200 {P : A \u2192 \u2605 \u2113} \u2192 \u03a0 A P \u2192 Data\u03a0 A\u1d49 P\n\n Reified : \u2605 (\u209b \u2113)\n Reified = \u2200 {P : A \u2192 \u2605 \u2113} \u2192 \u03a0 A P \u2194 Data\u03a0 A\u1d49 P\n\n Unfocus : \u2605 (\u209b \u2113)\n Unfocus = \u2200 {P : A \u2192 \u2605 \u2113} \u2192 \u03a3Point A\u1d49 P \u2192 \u03a3 A P\n\n Focus : \u2605 (\u209b \u2113)\n Focus = \u2200 {P : A \u2192 \u2605 \u2113} \u2192 \u03a3 A P \u2192 \u03a3Point A\u1d49 P\n\n Focused : \u2605 (\u209b \u2113)\n Focused = \u2200 {P : A \u2192 \u2605 \u2113} \u2192 \u03a3 A P \u2194 \u03a3Point A\u1d49 P\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\n-- The core types behind exploration functions\nmodule Explore.Type where\n\nopen import Level using (_\u2294_) renaming (zero to \u2080; suc to \u209b)\nopen import Type hiding (\u2605)\nopen import Function.NP\nopen import Function.Inverse using (_\u2194_)\nopen import Data.Nat.NP hiding (_\u2294_)\nopen import Data.Bit\nopen import Data.Bits\nopen import Data.Indexed\nopen import Algebra\nopen import Relation.Binary.NP\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Two using (\ud835\udfda; \u2713)\nopen import Data.Maybe.NP using (_\u2192?_)\nopen import Data.Fin using (Fin)\nimport Algebra.FunctionProperties.NP as FP\nopen FP using (Op\u2082)\nopen import Relation.Unary.Logical\nopen import Relation.Binary.Logical\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_; _\u2257_)\n\nmodule SgrpExtra {c \u2113} (sg : Semigroup c \u2113) where\n open Semigroup sg\n open Setoid-Reasoning (Semigroup.setoid sg) public\n C : \u2605 _\n C = Carrier\n _\u2248\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 C) \u2192 \u2605 _\n f \u2248\u00b0 g = \u2200 x \u2192 f x \u2248 g x\n _\u2219\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 C) \u2192 A \u2192 C\n (f \u2219\u00b0 g) x = f x \u2219 g x\n infixl 7 _-\u2219-_\n _-\u2219-_ : _\u2219_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_\n _-\u2219-_ = \u2219-cong\n\nmodule Sgrp {c \u2113} (sg : Semigroup c \u2113) where\n open Semigroup sg public\n open SgrpExtra sg public\n\nmodule RawMon {c} {C : \u2605 c} (rawMon : C \u00d7 Op\u2082 C) where\n \u03b5 = proj\u2081 rawMon\n _\u2219_ = proj\u2082 rawMon\n\nmodule Mon {c \u2113} (m : Monoid c \u2113) where\n open Monoid m public\n sg = semigroup\n open SgrpExtra semigroup public\n RawMon = C \u00d7 Op\u2082 C\n rawMon : RawMon\n rawMon = \u03b5 , _\u2219_\n\nmodule CMon {c \u2113} (cm : CommutativeMonoid c \u2113) where\n open CommutativeMonoid cm public\n sg = semigroup\n m = monoid\n open SgrpExtra sg public\n open FP _\u2248_\n\n \u2219-interchange : Interchange _\u2219_ _\u2219_\n \u2219-interchange = InterchangeFromAssocCommCong.\u2219-interchange\n isEquivalence\n _\u2219_ assoc comm (\u03bb _ \u2192 flip \u2219-cong refl)\n\nExplore : \u2200 \u2113 \u2192 \u2605\u2080 \u2192 \u2605 \u209b \u2113\nExplore \u2113 A = \u2200 {M : \u2605 \u2113} \u2192 (_\u2219_ : M \u2192 M \u2192 M) \u2192 (A \u2192 M) \u2192 M\n\nExplore\u2080 : \u2605\u2080 \u2192 \u2605\u2081\nExplore\u2080 = Explore _\n\nExplore\u2081 : \u2605\u2080 \u2192 \u2605\u2082\nExplore\u2081 = Explore _\n\n[Explore] : ([\u2605\u2080] [\u2192] [\u2605\u2081]) (Explore _)\n[Explore] A\u209a = \u2200\u27e8 M\u209a \u2236 [\u2605\u2080] \u27e9[\u2192] [Op\u2082] M\u209a [\u2192] (A\u209a [\u2192] M\u209a) [\u2192] M\u209a\n\n\u27e6Explore\u27e7 : (\u27e6\u2605\u2080\u27e7 \u27e6\u2192\u27e7 \u27e6\u2605\u2081\u27e7) (Explore _) (Explore _)\n\u27e6Explore\u27e7 A\u1d63 = \u2200\u27e8 M\u1d63 \u2236 \u27e6\u2605\u2080\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Op\u2082\u27e7 M\u1d63 \u27e6\u2192\u27e7 (A\u1d63 \u27e6\u2192\u27e7 M\u1d63) \u27e6\u2192\u27e7 M\u1d63\n\n\u27e6Explore\u27e7\u1d64 : \u2200 {\u2113} \u2192 (\u27e6\u2605\u2080\u27e7 \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 (\u209b \u2113)) (Explore \u2113) (Explore \u2113)\n\u27e6Explore\u27e7\u1d64 {\u2113} A\u1d63 = \u2200\u27e8 M\u1d63 \u2236 \u27e6\u2605\u27e7 \u2113 \u27e9\u27e6\u2192\u27e7 \u27e6Op\u2082\u27e7 M\u1d63 \u27e6\u2192\u27e7 (A\u1d63 \u27e6\u2192\u27e7 M\u1d63) \u27e6\u2192\u27e7 M\u1d63\n\n-- Trimmed down version of \u27e6Explore\u27e7\n\u27e6Explore\u27e7\u2081 : \u2200 {A : \u2605_ _} (A\u1d63 : A \u2192 A \u2192 \u2605_ _) \u2192 Explore _ A \u2192 \u2605\u2081\n\u27e6Explore\u27e7\u2081 A\u1d63 s = \u27e6Explore\u27e7 A\u1d63 s s\n\n_\u2219-explore_ : \u2200 {\u2113 A} \u2192 Explore \u2113 A \u2192 Explore \u2113 A \u2192 Explore \u2113 A\n(s\u2080 \u2219-explore s\u2081) _\u2219_ f = s\u2080 _\u2219_ f \u2219 s\u2081 _\u2219_ f\n\nconst-explore : \u2200 {\u2113 A} \u2192 A \u2192 Explore \u2113 A\nconst-explore x _ f = f x\n\nExploreInd : \u2200 p {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreInd p {\u2113} {A} srch =\n \u2200 (P : Explore \u2113 A \u2192 \u2605 p)\n (P\u2219 : \u2200 {s\u2080 s\u2081 : Explore \u2113 A} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb _\u2219_ f \u2192 s\u2080 _\u2219_ f \u2219 s\u2081 _\u2219_ f))\n (Pf : \u2200 x \u2192 P (\u03bb _ f \u2192 f x))\n \u2192 P srch\n\nconst-explore-ind : \u2200 {\u2113 p A} (x : A) \u2192 ExploreInd p (const-explore {\u2113} x)\nconst-explore-ind x _ _ Pf = Pf x\n\n{-\n_\u2219ExploreInd_ : \u2200 {\u2113 p A} {s\u2080 s\u2081 : Explore \u2113 A}\n \u2192 ExploreInd p s\u2080 \u2192 ExploreInd p s\u2081\n \u2192 ExploreInd p (s\u2080 \u2219Explore s\u2081)\n_\u2219ExploreInd_ {s\u2080 = s\u2080} {s\u2081} Ps\u2080 Ps\u2081 P P\u2219 Pf = Ps\u2080 (\u03bb s \u2192 P s\u2081 \u2192 P (s \u2219Explore s\u2081)) (\u03bb {s\u2082} {s\u2083} Ps\u2082 Ps\u2083 Ps\u2081' \u2192 {!!}) (P\u2219 \u2218 Pf) (Ps\u2081 P P\u2219 Pf)\n-}\n\nrecord ExploreIndKit p {\u2113 A} (P : Explore \u2113 A \u2192 \u2605 p) : \u2605 (\u209b \u2113 \u2294 p) where\n constructor _,_\n field\n P\u2219 : \u2200 {s\u2080 s\u2081 : Explore \u2113 A} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (s\u2080 \u2219-explore s\u2081)\n Pf : \u2200 x \u2192 P (const-explore x)\n\n_$kit_ : \u2200 {p \u2113 A} {P : Explore \u2113 A \u2192 \u2605 p} {s : Explore \u2113 A}\n \u2192 ExploreInd p s \u2192 ExploreIndKit p P \u2192 P s\n_$kit_ {P = P} ind (P\u2219 , Pf) = ind P P\u2219 Pf\n\nExploreInd\u2080 : \u2200 {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreInd\u2080 = ExploreInd \u2080\n\nExploreMon : \u2200 {c \u2113} \u2192 Monoid c \u2113 \u2192 \u2605\u2080 \u2192 \u2605 _\nExploreMon M A = (A \u2192 C) \u2192 C\n where open Mon M\n\nExploreMonInd : \u2200 p {c \u2113} {A} (M : Monoid c \u2113) \u2192 ExploreMon M A \u2192 \u2605 _\nExploreMonInd p {c} {\u2113} {A} M srch =\n \u2200 (P : ExploreMon M A \u2192 \u2605 p)\n (P\u2219 : \u2200 {s\u2080 s\u2081 : ExploreMon M A} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb f \u2192 s\u2080 f \u2219 s\u2081 f))\n (Pf : \u2200 x \u2192 P (\u03bb f \u2192 f x))\n (P\u2248 : \u2200 {s s'} \u2192 s \u2248\u00b0 s' \u2192 P s \u2192 P s')\n \u2192 P srch\n where open Mon M\n\nexplore\u2218FromExplore : \u2200 {m A} \u2192 Explore m A\n \u2192 \u2200 {M : \u2605 m} \u2192 (M \u2192 M \u2192 M) \u2192 (A \u2192 M) \u2192 (M \u2192 M)\nexplore\u2218FromExplore explore op f = explore _\u2218\u2032_ (op \u2218 f)\n\nExplorePlug : \u2200 {m \u2113 A} (M : Monoid m \u2113) (s : Explore _ A) \u2192 \u2605 _\nExplorePlug M s = \u2200 f x \u2192 s\u2218 _\u2219_ f \u03b5 \u2219 x \u2248 s\u2218 _\u2219_ f x\n where open Mon M\n s\u2218 = explore\u2218FromExplore s\n\n {-\nExploreMon : \u2200 m \u2192 \u2605\u2080 \u2192 \u2605 _\nExploreMon m A = \u2200 {M : \u2605 m} \u2192 M \u00d7 Op\u2082 M \u2192 (A \u2192 M) \u2192 M\n\nExploreMonInd : \u2200 p {\u2113} {A} \u2192 ExploreMon \u2113 A \u2192 \u2605 _\nExploreMonInd p {\u2113} {A} srch =\n \u2200 (P : ExploreMon _ A \u2192 \u2605 p)\n (P\u2219 : \u2200 {s\u2080 s\u2081 : ExploreMon _ A} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb M f \u2192 let _\u2219_ = proj\u2082 M in\n s\u2080 M f \u2219 s\u2081 M f))\n (Pf : \u2200 x \u2192 P (\u03bb _ f \u2192 f x))\n \u2192 P srch\n\nexploreMonFromExplore : \u2200 {\u2113 A}\n \u2192 Explore \u2113 A \u2192 ExploreMon \u2113 A\nexploreMonFromExplore s = s \u2218 proj\u2082\n -}\n\nexploreMonFromExplore : \u2200 {c \u2113 A}\n \u2192 Explore c A \u2192 (M : Monoid c \u2113) \u2192 ExploreMon M A\nexploreMonFromExplore s M = s _\u2219_ where open Mon M\n\nSum : \u2605\u2080 \u2192 \u2605\u2080\nSum A = (A \u2192 \u2115) \u2192 \u2115\n\nProduct : \u2605\u2080 \u2192 \u2605\u2080\nProduct A = (A \u2192 \u2115) \u2192 \u2115\n\nAdequateSum : \u2200 {A} \u2192 Sum A \u2192 \u2605\u2080\nAdequateSum {A} sum\u1d2c = \u2200 f \u2192 Fin (sum\u1d2c f) \u2194 \u03a3 A (Fin \u2218 f)\n\nAdequateProduct : \u2200 {A} \u2192 Product A \u2192 \u2605\u2080\nAdequateProduct {A} product\u1d2c = \u2200 f \u2192 Fin (product\u1d2c f) \u2194 \u03a0 A (Fin \u2218 f)\n\nCount : \u2605\u2080 \u2192 \u2605\u2080\nCount A = (A \u2192 Bit) \u2192 \u2115\n\nFind? : \u2605\u2080 \u2192 \u2605\u2081\nFind? A = \u2200 {B : \u2605\u2080} \u2192 (A \u2192? B) \u2192? B\n\nFindKey : \u2605\u2080 \u2192 \u2605\u2080\nFindKey A = (A \u2192 \ud835\udfda) \u2192? A\n\n_,-kit_ : \u2200 {m p A} {P : Explore m A \u2192 \u2605 p}{Q : Explore m A \u2192 \u2605 p}\n \u2192 ExploreIndKit p P \u2192 ExploreIndKit p Q \u2192 ExploreIndKit p (P \u00d7\u00b0 Q)\nPk ,-kit Qk = (\u03bb x y \u2192 P\u2219 Pk (proj\u2081 x) (proj\u2081 y) , P\u2219 Qk (proj\u2082 x) (proj\u2082 y))\n , (\u03bb x \u2192 Pf Pk x , Pf Qk x)\n where open ExploreIndKit\n\nExploreInd-Extra : \u2200 p {m A} \u2192 Explore m A \u2192 \u2605 _\nExploreInd-Extra p {m} {A} srch =\n \u2200 (Q : Explore m A \u2192 \u2605 p)\n (Q-kit : ExploreIndKit p Q)\n (P : Explore m A \u2192 \u2605 p)\n (P\u2219 : \u2200 {s\u2080 s\u2081 : Explore m A} \u2192 Q s\u2080 \u2192 Q s\u2081 \u2192 P s\u2080 \u2192 P s\u2081\n \u2192 P (s\u2080 \u2219-explore s\u2081))\n (Pf : \u2200 x \u2192 P (const-explore x))\n \u2192 P srch\n\nto-extra : \u2200 {p m A} {s : Explore m A} \u2192 ExploreInd p s \u2192 ExploreInd-Extra p s\nto-extra s-ind Q Q-kit P P\u2219 Pf =\n proj\u2082 (s-ind (Q \u00d7\u00b0 P)\n (\u03bb { (a , b) (c , d) \u2192 Q\u2219 a c , P\u2219 a c b d })\n (\u03bb x \u2192 Qf x , Pf x))\n where open ExploreIndKit Q-kit renaming (P\u2219 to Q\u2219; Pf to Qf)\n\nStableUnder : \u2200 {\u2113 A} \u2192 Explore \u2113 A \u2192 (A \u2192 A) \u2192 \u2605 _\nStableUnder explore p = \u2200 {M} op (f : _ \u2192 M) \u2192 explore op f \u2261 explore op (f \u2218 p)\n\nSumStableUnder : \u2200 {A} \u2192 Sum A \u2192 (A \u2192 A) \u2192 \u2605 _\nSumStableUnder sum p = \u2200 f \u2192 sum f \u2261 sum (f \u2218 p)\n\nCountStableUnder : \u2200 {A} \u2192 Count A \u2192 (A \u2192 A) \u2192 \u2605 _\nCountStableUnder count p = \u2200 f \u2192 count f \u2261 count (f \u2218 p)\n\n-- TODO: remove the hard-wired \u2261\nInjective : \u2200 {a b}{A : \u2605 a}{B : \u2605 b}(f : A \u2192 B) \u2192 \u2605 _\nInjective f = \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y\n\nSumStableUnderInjection : \u2200 {A} \u2192 Sum A \u2192 \u2605 _\nSumStableUnderInjection sum = \u2200 p \u2192 Injective p \u2192 SumStableUnder sum p\n\nSumInd : \u2200 {A} \u2192 Sum A \u2192 \u2605\u2081\nSumInd {A} sum = \u2200 (P : Sum A \u2192 \u2605\u2080)\n (P+ : \u2200 {s\u2080 s\u2081 : Sum A} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb f \u2192 s\u2080 f + s\u2081 f))\n (Pf : \u2200 x \u2192 P (\u03bb f \u2192 f x))\n \u2192 P sum\n\nExploreMono : \u2200 r {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreMono r s\u1d2c = \u2200 {C} (_\u2286_ : C \u2192 C \u2192 \u2605 r)\n {_\u2219_} (\u2219-mono : _\u2219_ Preserves\u2082 _\u2286_ \u27f6 _\u2286_ \u27f6 _\u2286_)\n {f g} \u2192\n (\u2200 x \u2192 f x \u2286 g x) \u2192 s\u1d2c _\u2219_ f \u2286 s\u1d2c _\u2219_ g\n\n\nExploreExtFun : \u2200 {A B} \u2192 Explore _ (A \u2192 B) \u2192 \u2605\u2081\nExploreExtFun {A}{B} s\u1d2c\u1d2e = \u2200 {M} op {f g : (A \u2192 B) \u2192 M} \u2192 (\u2200 {\u03c6 \u03c8} \u2192 \u03c6 \u2257 \u03c8 \u2192 f \u03c6 \u2261 g \u03c8) \u2192 s\u1d2c\u1d2e op f \u2261 s\u1d2c\u1d2e op g\n\nExploreSgExt : \u2200 r {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreSgExt r {\u2113} s\u1d2c = \u2200 (sg : Semigroup \u2113 r) {f g}\n \u2192 let open Sgrp sg in\n f \u2248\u00b0 g \u2192 s\u1d2c _\u2219_ f \u2248 s\u1d2c _\u2219_ g\n\nExploreExt : \u2200 {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreExt {\u2113} {A} s\u1d2c = \u2200 {M} op {f g : A \u2192 M} \u2192 f \u2257 g \u2192 s\u1d2c op f \u2261 s\u1d2c op g\n\nSumExt : \u2200 {A} \u2192 Sum A \u2192 \u2605 _\nSumExt sum\u1d2c = \u2200 {f g} \u2192 f \u2257 g \u2192 sum\u1d2c f \u2261 sum\u1d2c g\n\nCountExt : \u2200 {A} \u2192 Count A \u2192 \u2605 _\nCountExt count\u1d2c = \u2200 {f g} \u2192 f \u2257 g \u2192 count\u1d2c f \u2261 count\u1d2c g\n\nExplore\u03b5 : \u2200 \u2113 r {A} \u2192 Explore _ A \u2192 \u2605 _\nExplore\u03b5 \u2113 r s\u1d2c = \u2200 (m : Monoid \u2113 r) \u2192\n let open Mon m in\n s\u1d2c _\u2219_ (const \u03b5) \u2248 \u03b5\n\nSumZero : \u2200 {A} \u2192 Sum A \u2192 \u2605 _\nSumZero sum\u1d2c = sum\u1d2c (\u03bb _ \u2192 0) \u2261 0\n\nExploreLin\u02e1 : \u2200 \u2113 r {A} \u2192 Explore _ A \u2192 \u2605 _\nExploreLin\u02e1 \u2113 r s\u1d2c = \u2200 m _\u25ce_ f k \u2192\n let open Mon {\u2113} {r} m\n open FP _\u2248_ in\n _\u25ce_ DistributesOver\u02e1 _\u2219_ \u2192\n s\u1d2c _\u2219_ (\u03bb x \u2192 k \u25ce f x) \u2248 k \u25ce s\u1d2c _\u2219_ f\n\nExploreLin\u02b3 : \u2200 \u2113 r {A} \u2192 Explore _ A \u2192 \u2605 _\nExploreLin\u02b3 \u2113 r s\u1d2c =\n \u2200 m _\u25ce_ f k \u2192\n let open Mon {\u2113} {r} m\n open FP _\u2248_ in\n _\u25ce_ DistributesOver\u02b3 _\u2219_ \u2192\n s\u1d2c _\u2219_ (\u03bb x \u2192 f x \u25ce k) \u2248 s\u1d2c _\u2219_ f \u25ce k\n\nSumLin : \u2200 {A} \u2192 Sum A \u2192 \u2605 _\nSumLin sum\u1d2c = \u2200 f k \u2192 sum\u1d2c (\u03bb x \u2192 k * f x) \u2261 k * sum\u1d2c f\n\nExploreHom : \u2200 r {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreHom r s\u1d2c = \u2200 sg f g \u2192 let open Sgrp {_} {r} sg in\n s\u1d2c _\u2219_ (f \u2219\u00b0 g) \u2248 s\u1d2c _\u2219_ f \u2219 s\u1d2c _\u2219_ g\n\nExploreMonHom : \u2200 \u2113 r {A} \u2192 Explore _ A \u2192 \u2605 _\nExploreMonHom \u2113 r s\u1d2c =\n \u2200 cm f g \u2192\n let open CMon {\u2113} {r} cm in\n s\u1d2c _\u2219_ (f \u2219\u00b0 g) \u2248 s\u1d2c _\u2219_ f \u2219 s\u1d2c _\u2219_ g\n\nSumHom : \u2200 {A} \u2192 Sum A \u2192 \u2605 _\nSumHom sum\u1d2c = \u2200 f g \u2192 sum\u1d2c (\u03bb x \u2192 f x + g x) \u2261 sum\u1d2c f + sum\u1d2c g\n\nSumMono : \u2200 {A} \u2192 Sum A \u2192 \u2605 _\nSumMono sum\u1d2c = \u2200 {f g} \u2192 (\u2200 x \u2192 f x \u2264 g x) \u2192 sum\u1d2c f \u2264 sum\u1d2c g\n\nExploreSwap : \u2200 r {\u2113 A} \u2192 Explore \u2113 A \u2192 \u2605 _\nExploreSwap r {\u2113} {A} s\u1d2c = \u2200 {B : \u2605\u2080} sg f \u2192\n let open Sgrp {_} {r} sg in\n \u2200 {s\u1d2e : (B \u2192 C) \u2192 C}\n \u2192 (hom : \u2200 f g \u2192 s\u1d2e (f \u2219\u00b0 g) \u2248 s\u1d2e f \u2219 s\u1d2e g)\n \u2192 s\u1d2c _\u2219_ (s\u1d2e \u2218 f) \u2248 s\u1d2e (s\u1d2c _\u2219_ \u2218 flip f)\n\nUnique : \u2200 {A} \u2192 Cmp A \u2192 Count A \u2192 \u2605 _\nUnique cmp count = \u2200 x \u2192 count (cmp x) \u2261 1\n\nData\u03a0 : \u2200 {b A} \u2192 Explore _ A \u2192 (A \u2192 \u2605 b) \u2192 \u2605 b\nData\u03a0 sA = sA _\u00d7_\n\nLookup : \u2200 {b A} \u2192 Explore _ A \u2192 \u2605 _\nLookup {b} {A} sA = \u2200 {B : A \u2192 \u2605 b} \u2192 Data\u03a0 sA B \u2192 \u03a0 A B\n\nReify : \u2200 {b A} \u2192 Explore _ A \u2192 \u2605 _\nReify {b} {A} sA = \u2200 {B : A \u2192 \u2605 b} \u2192 \u03a0 A B \u2192 Data\u03a0 sA B\n\nReified : \u2200 {b A} \u2192 Explore _ A \u2192 \u2605 _\nReified {b} {A} sA = \u2200 {B : A \u2192 \u2605 b} \u2192 \u03a0 A B \u2194 Data\u03a0 sA B\n\n\u03a3Point : \u2200 {b A} \u2192 Explore _ A \u2192 (A \u2192 \u2605 b) \u2192 \u2605 b\n\u03a3Point sA = sA _\u228e_\n\nUnfocus : \u2200 {b A} \u2192 Explore _ A \u2192 \u2605 _\nUnfocus {b} {A} sA = \u2200 {B : A \u2192 \u2605 b} \u2192 \u03a3Point sA B \u2192 \u03a3 A B\n\nFocus : \u2200 {b A} \u2192 Explore _ A \u2192 \u2605 _\nFocus {b} {A} sA = \u2200 {B : A \u2192 \u2605 b} \u2192 \u03a3 A B \u2192 \u03a3Point sA B\n\nFocused : \u2200 {b A} \u2192 Explore _ A \u2192 \u2605 _\nFocused {b} {A} sA = \u2200 {B : A \u2192 \u2605 b} \u2192 \u03a3 A B \u2194 \u03a3Point sA B\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"7462fd6d4414a70a0c18898feef13e9b44d95589","subject":"CCA2\u2020 more intermediate names","message":"CCA2\u2020 more intermediate names\n","repos":"crypto-agda\/crypto-agda","old_file":"Game\/IND-CCA2-dagger.agda","new_file":"Game\/IND-CCA2-dagger.agda","new_contents":"\nopen import Type\nopen import Data.Bit\nopen import Data.Maybe\nopen import Data.Product\n\nopen import Data.Nat.NP\n--open import Rat\n\nopen import Explore.Core\nopen import Explore.Explorable\nopen import Explore.Product\nopen Operators\nopen import Control.Strategy renaming (run to runStrategy)\nimport Game.IND-CPA-utils\n\nopen import Relation.Binary.PropositionalEquality\n\nmodule Game.IND-CCA2-dagger\n (PubKey : \u2605)\n (SecKey : \u2605)\n (Message : \u2605)\n (CipherText : \u2605)\n\n -- randomness supply for, encryption, key-generation, adversary, adversary state\n (R\u2091 R\u2096 R\u2090 : \u2605)\n (KeyGen : R\u2096 \u2192 PubKey \u00d7 SecKey)\n (Enc : PubKey \u2192 Message \u2192 R\u2091 \u2192 CipherText)\n (Dec : SecKey \u2192 CipherText \u2192 Message)\n\nwhere\n\nopen Game.IND-CPA-utils Message CipherText\nopen CPAAdversary\n\nAdversary : \u2605\nAdversary = R\u2090 \u2192 PubKey \u2192\n DecRound -- first round of decryption queries\n (CPAAdversary -- choosen plaintext attack\n (CipherText \u2192 -- in which a second ciphertext is provided\n DecRound Bit)) -- second round of decryption queries\n\n{-\nValid-Adv : Adv \u2192 Set\nValid-Adv (m , d) = \u2200 {r\u2090 r\u2093 pk c c'} \u2192 Valid (\u03bb x \u2192 x \u2262 c \u00d7 x \u2262 c') (d r\u2090 r\u2093 pk c c')\n-}\n\nR : \u2605\nR = R\u2090 \u00d7 R\u2096 \u00d7 R\u2091 \u00d7 R\u2091\n\nExperiment : \u2605\nExperiment = Adversary \u2192 R \u2192 Bit\n\nmodule EXP (b : Bit) (A : Adversary) (r\u2090 : R\u2090) (pk : PubKey) (sk : SecKey) (r\u2091\u2080 r\u2091\u2081 : R\u2091) where\n decRound = runStrategy (Dec sk)\n A1 = A r\u2090 pk\n cpaA = decRound A1\n mb = proj\u2032 (get-m cpaA)\n c\u2080 = Enc pk (mb b) r\u2091\u2080\n c\u2081 = Enc pk (mb (not b)) r\u2091\u2081\n A2 = put-c cpaA c\u2080 c\u2081\n b\u2032 = decRound A2\n\n ct = proj\u2032 (c\u2080 , c\u2081)\n\nEXP : Bit \u2192 Experiment\nEXP b A (r\u2090 , r\u2096 , r\u2091\u2080 , r\u2091\u2081) with KeyGen r\u2096\n... | pk , sk = EXP.b\u2032 b A r\u2090 pk sk r\u2091\u2080 r\u2091\u2081\n\nmodule Advantage\n (\u03bc\u2091 : Explore\u2080 R\u2091)\n (\u03bc\u2096 : Explore\u2080 R\u2096)\n (\u03bc\u2090 : Explore\u2080 R\u2090)\n where\n \u03bcR : Explore\u2080 R\n \u03bcR = \u03bc\u2090 \u00d7\u1d49 \u03bc\u2096 \u00d7\u1d49 \u03bc\u2091 \u00d7\u1d49 \u03bc\u2091\n \n module \u03bcR = FromExplore\u2080 \u03bcR\n \n run : Bit \u2192 Adversary \u2192 \u2115\n run b adv = \u03bcR.count (EXP b adv)\n \n {-\n Advantage : Adv \u2192 \u211a\n Advantage adv = dist (run 0b adv) (run 1b adv) \/ \u03bcR.Card\n -}\n","old_contents":"\nopen import Type\nopen import Data.Bit\nopen import Data.Maybe\nopen import Data.Product\n\nopen import Data.Nat.NP\n--open import Rat\n\nopen import Explore.Core\nopen import Explore.Explorable\nopen import Explore.Product\nopen Operators\nopen import Control.Strategy renaming (run to runStrategy)\nimport Game.IND-CPA-utils\n\nopen import Relation.Binary.PropositionalEquality\n\nmodule Game.IND-CCA2-dagger\n (PubKey : \u2605)\n (SecKey : \u2605)\n (Message : \u2605)\n (CipherText : \u2605)\n\n -- randomness supply for, encryption, key-generation, adversary, adversary state\n (R\u2091 R\u2096 R\u2090 : \u2605)\n (KeyGen : R\u2096 \u2192 PubKey \u00d7 SecKey)\n (Enc : PubKey \u2192 Message \u2192 R\u2091 \u2192 CipherText)\n (Dec : SecKey \u2192 CipherText \u2192 Message)\n\nwhere\n\nopen Game.IND-CPA-utils Message CipherText\nopen CPAAdversary\n\nAdversary : \u2605\nAdversary = R\u2090 \u2192 PubKey \u2192\n DecRound -- first round of decryption queries\n (CPAAdversary -- choosen plaintext attack\n (CipherText \u2192 -- in which a second ciphertext is provided\n DecRound Bit)) -- second round of decryption queries\n\n{-\nValid-Adv : Adv \u2192 Set\nValid-Adv (m , d) = \u2200 {r\u2090 r\u2093 pk c c'} \u2192 Valid (\u03bb x \u2192 x \u2262 c \u00d7 x \u2262 c') (d r\u2090 r\u2093 pk c c')\n-}\n\nR : \u2605\nR = R\u2090 \u00d7 R\u2096 \u00d7 R\u2091 \u00d7 R\u2091\n\nExperiment : \u2605\nExperiment = Adversary \u2192 R \u2192 Bit\n\nmodule EXP (b : Bit) (A : Adversary) (r\u2090 : R\u2090) (pk : PubKey) (sk : SecKey) (r\u2091\u2080 r\u2091\u2081 : R\u2091) where\n decRound = runStrategy (Dec sk)\n cpaA = decRound (A r\u2090 pk)\n mb = proj\u2032 (get-m cpaA)\n c\u2080 = Enc pk (mb b) r\u2091\u2080\n c\u2081 = Enc pk (mb (not b)) r\u2091\u2081\n b\u2032 = decRound (put-c cpaA c\u2080 c\u2081)\n\nEXP : Bit \u2192 Experiment\nEXP b A (r\u2090 , r\u2096 , r\u2091\u2080 , r\u2091\u2081) with KeyGen r\u2096\n... | pk , sk = EXP.b\u2032 b A r\u2090 pk sk r\u2091\u2080 r\u2091\u2081\n\nmodule Advantage\n (\u03bc\u2091 : Explore\u2080 R\u2091)\n (\u03bc\u2096 : Explore\u2080 R\u2096)\n (\u03bc\u2090 : Explore\u2080 R\u2090)\n where\n \u03bcR : Explore\u2080 R\n \u03bcR = \u03bc\u2090 \u00d7\u1d49 \u03bc\u2096 \u00d7\u1d49 \u03bc\u2091 \u00d7\u1d49 \u03bc\u2091\n \n module \u03bcR = FromExplore\u2080 \u03bcR\n \n run : Bit \u2192 Adversary \u2192 \u2115\n run b adv = \u03bcR.count (EXP b adv)\n \n {-\n Advantage : Adv \u2192 \u211a\n Advantage adv = dist (run 0b adv) (run 1b adv) \/ \u03bcR.Card\n -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"2f337fa6c912b4b71f8a6539911608241ebc8524","subject":"Use helpers for nested applications.","message":"Use helpers for nested applications.\n\nOld-commit-hash: e4c7562ec14ff04d00f64f84b95725baa11a85ca\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Term.agda","new_file":"Parametric\/Change\/Term.agda","new_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Constant\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\nDiffStructure : Set\nDiffStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))\n\nApplyStructure : Set\nApplyStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\n\nmodule Structure\n (diff-base : DiffStructure)\n (apply-base : ApplyStructure)\n where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n lift-diff-apply :\n \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4) \u00d7 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-diff-apply {base \u03b9} = diff-base , apply-base\n lift-diff-apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app\u2082 diff\u03c3 s t\n _\u229d\u03c4_ = \u03bb s t \u2192 app\u2082 diff\u03c4 s t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app\u2082 apply\u03c3 \u0394t t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app\u2082 apply\u03c4 \u0394t t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\n lift-diff :\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n\n lift-diff = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply {\u03c4} {\u0393})\n\n lift-apply :\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply {\u03c4} {\u0393})\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Constant\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\nDiffStructure : Set\nDiffStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))\n\nApplyStructure : Set\nApplyStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\n\nmodule Structure\n (diff-base : DiffStructure)\n (apply-base : ApplyStructure)\n where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n lift-diff-apply :\n \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4) \u00d7 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-diff-apply {base \u03b9} = diff-base , apply-base\n lift-diff-apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\n lift-diff :\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n\n lift-diff = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply {\u03c4} {\u0393})\n\n lift-apply :\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply {\u03c4} {\u0393})\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"49a7a00a3832689c0045cb3cae94640175267a08","subject":"Removed unnecessary arguments.","message":"Removed unnecessary arguments.\n\nIgnore-this: a39d6ebeef4c07ea14c5405619bc8d59\n\ndarcs-hash:20111218163729-3bd4e-c8488225fefcfde5c7622ca2471e2ae95fba08c7.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Program\/ABP\/Fair\/PropertiesATP.agda","new_file":"src\/FOTC\/Program\/ABP\/Fair\/PropertiesATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Fair properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Program.ABP.Fair.PropertiesATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.List\nopen import FOTC.Program.ABP.Fair\nopen import FOTC.Program.ABP.Terms\n\n------------------------------------------------------------------------------\n\nhead-tail-Fair-helper : \u2200 {os ol os'} \u2192 O*L ol \u2192 os \u2261 ol ++ os' \u2192\n os \u2261 L \u2237 tail\u2081 os \u2228 os \u2261 O \u2237 tail\u2081 os\nhead-tail-Fair-helper {os} nilO*L h = prf\n where\n postulate prf : os \u2261 L \u2237 tail\u2081 os \u2228 os \u2261 O \u2237 tail\u2081 os\n {-# ATP prove prf #-}\n\nhead-tail-Fair-helper {os} (consO*L OLol) h = prf\n where\n postulate prf : os \u2261 L \u2237 tail\u2081 os \u2228 os \u2261 O \u2237 tail\u2081 os\n {-# ATP prove prf #-}\n\nhead-tail-Fair : \u2200 {os} \u2192 Fair os \u2192 os \u2261 L \u2237 tail\u2081 os \u2228 os \u2261 O \u2237 tail\u2081 os\nhead-tail-Fair {os} Fos = prf\n where\n postulate prf : os \u2261 L \u2237 tail\u2081 os \u2228 os \u2261 O \u2237 tail\u2081 os\n {-# ATP prove prf head-tail-Fair-helper #-}\n\ntail-Fair-helper : \u2200 {os ol os'} \u2192 O*L ol \u2192 os \u2261 ol ++ os' \u2192 Fair os' \u2192\n Fair (tail\u2081 os)\ntail-Fair-helper {os} nilO*L h Fos' = prf\n where\n postulate prf : Fair (tail\u2081 os)\n {-# ATP prove prf #-}\n\ntail-Fair-helper {os} (consO*L OLol) h Fos' = prf\n where\n postulate prf : Fair (tail\u2081 os)\n {-# ATP prove prf Fair-gfp\u2083 #-}\n\ntail-Fair : \u2200 {os} \u2192 Fair os \u2192 Fair (tail\u2081 os)\ntail-Fair {os} Fos = prf\n where\n postulate prf : Fair (tail\u2081 os)\n {-# ATP prove prf tail-Fair-helper #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Fair properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Program.ABP.Fair.PropertiesATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.List\nopen import FOTC.Program.ABP.Fair\nopen import FOTC.Program.ABP.Terms\n\n------------------------------------------------------------------------------\n\nhead-tail-Fair-helper : \u2200 {os ol os'} \u2192 O*L ol \u2192 os \u2261 ol ++ os' \u2192\n os \u2261 L \u2237 tail\u2081 os \u2228 os \u2261 O \u2237 tail\u2081 os\nhead-tail-Fair-helper {os} {os' = os'} nilO*L h = prf\n where\n postulate prf : os \u2261 L \u2237 tail\u2081 os \u2228 os \u2261 O \u2237 tail\u2081 os\n {-# ATP prove prf #-}\n\nhead-tail-Fair-helper {os} {os' = os'} (consO*L OLol) h = prf\n where\n postulate prf : os \u2261 L \u2237 tail\u2081 os \u2228 os \u2261 O \u2237 tail\u2081 os\n {-# ATP prove prf #-}\n\nhead-tail-Fair : \u2200 {os} \u2192 Fair os \u2192 os \u2261 L \u2237 tail\u2081 os \u2228 os \u2261 O \u2237 tail\u2081 os\nhead-tail-Fair {os} Fos = prf\n where\n postulate prf : os \u2261 L \u2237 tail\u2081 os \u2228 os \u2261 O \u2237 tail\u2081 os\n {-# ATP prove prf head-tail-Fair-helper #-}\n\ntail-Fair-helper : \u2200 {os ol os'} \u2192 O*L ol \u2192 os \u2261 ol ++ os' \u2192 Fair os' \u2192\n Fair (tail\u2081 os)\ntail-Fair-helper {os} {os' = os'} nilO*L h Fos' = prf\n where\n postulate prf : Fair (tail\u2081 os)\n {-# ATP prove prf #-}\n\ntail-Fair-helper {os} {os' = os'} (consO*L OLol) h Fos' = prf\n where\n postulate prf : Fair (tail\u2081 os)\n {-# ATP prove prf Fair-gfp\u2083 #-}\n\ntail-Fair : \u2200 {os} \u2192 Fair os \u2192 Fair (tail\u2081 os)\ntail-Fair {os} Fos = prf\n where\n postulate prf : Fair (tail\u2081 os)\n {-# ATP prove prf tail-Fair-helper #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"561e54d6fa752c3e41e0261fcb95b5fe132461b5","subject":"Rename lift-\u03b7-const-rec to curryTermConstructor.","message":"Rename lift-\u03b7-const-rec to curryTermConstructor.\n\nAlso delete the frivolous comment, which is no longer necessary\nnow that curryTermConstructor has a descriptive name and the\nmatching type signature.\n\nOld-commit-hash: bcd82c9b32ab89ebe5c1badbec2d5714d0fce9bf\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Term\/Plotkin.agda","new_file":"Syntax\/Term\/Plotkin.agda","new_contents":"import Syntax.Type.Plotkin as Type\nimport Syntax.Context as Context\n\nmodule Syntax.Term.Plotkin\n {B : Set {- of base types -}}\n {C : Context.Context {Type.Type B} \u2192 Type.Type B \u2192 Set {- of constants -}}\n where\n\n-- Terms of languages described in Plotkin style\n\nopen import Function using (_\u2218_)\nopen import Data.Product\n\nopen Type B\nopen Context {Type}\n\nopen import Denotation.Environment Type\nopen import Syntax.Context.Plotkin B\n\n-- Declarations of Term and Terms to enable mutual recursion\ndata Term\n (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set\n\ndata Terms\n (\u0393 : Context) :\n (\u03a3 : Context) \u2192 Set\n\n-- (Term \u0393 \u03c4) represents a term of type \u03c4\n-- with free variables bound in \u0393.\ndata Term \u0393 where\n const : \u2200 {\u03a3 \u03c4} \u2192\n (c : C \u03a3 \u03c4) \u2192\n Terms \u0393 \u03a3 \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\n-- (Terms \u0393 \u03a3) represents a list of terms with types from \u03a3\n-- with free variables bound in \u0393.\ndata Terms \u0393 where\n \u2205 : Terms \u0393 \u2205\n _\u2022_ : \u2200 {\u03c4 \u03a3} \u2192\n Term \u0393 \u03c4 \u2192\n Terms \u0393 \u03a3 \u2192\n Terms \u0393 (\u03c4 \u2022 \u03a3)\n\ninfixr 9 _\u2022_\n\n-- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n-- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\nlift-diff-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4) \u00d7 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-diff-apply diff apply {base \u03b9} = diff , apply\nlift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\nlift-diff : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4)\n\nlift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\nlift-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n-- Weakening\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\n\nweakenAll : \u2200 {\u0393\u2081 \u0393\u2082 \u03a3} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Terms \u0393\u2081 \u03a3 \u2192\n Terms \u0393\u2082 \u03a3\n\nweaken \u0393\u2081\u227c\u0393\u2082 (const c ts) = const c (weakenAll \u0393\u2081\u227c\u0393\u2082 ts)\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (lift \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\nweakenAll \u0393\u2081\u227c\u0393\u2082 \u2205 = \u2205\nweakenAll \u0393\u2081\u227c\u0393\u2082 (t \u2022 ts) = weaken \u0393\u2081\u227c\u0393\u2082 t \u2022 weakenAll \u0393\u2081\u227c\u0393\u2082 ts\n\n-- Specialized weakening\nweaken\u2081 : \u2200 {\u0393 \u03c3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4\nweaken\u2081 t = weaken (drop _ \u2022 \u227c-refl) t\n\nweaken\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u0393) \u03c4\nweaken\u2082 t = weaken (drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\nweaken\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u03b3 \u2022 \u0393) \u03c4\nweaken\u2083 t = weaken (drop _ \u2022 drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n-- Shorthands for nested applications\napp\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3\napp\u2082 f x = app (app f x)\n\napp\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4\napp\u2083 f x = app\u2082 (app f x)\n\napp\u2084 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5\napp\u2084 f x = app\u2083 (app f x)\n\napp\u2085 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6\napp\u2085 f x = app\u2084 (app f x)\n\napp\u2086 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6 \u21d2 \u03b7) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6 \u2192 Term \u0393 \u03b7\napp\u2086 f x = app\u2085 (app f x)\n\nUncurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nUncurriedTermConstructor \u0393 \u03a3 \u03c4 = Terms \u0393 \u03a3 \u2192 Term \u0393 \u03c4\n\nCurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nCurriedTermConstructor \u0393 \u2205 \u03c4\u2032 = Term \u0393 \u03c4\u2032\nCurriedTermConstructor \u0393 (\u03c4 \u2022 \u03a3) \u03c4\u2032 = Term \u0393 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\u2032\n\ncurryTermConstructor : \u2200 {\u03a3 \u0393 \u03c4} \u2192 UncurriedTermConstructor \u0393 \u03a3 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\ncurryTermConstructor {\u2205} k = k \u2205\ncurryTermConstructor {\u03c4 \u2022 \u03a3} k = \u03bb t \u2192 curryTermConstructor (\u03bb ts \u2192 k (t \u2022 ts))\n\nlift-\u03b7-const : \u2200 {\u03a3 \u03c4} \u2192 C \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const constant = curryTermConstructor (const constant)\n","old_contents":"import Syntax.Type.Plotkin as Type\nimport Syntax.Context as Context\n\nmodule Syntax.Term.Plotkin\n {B : Set {- of base types -}}\n {C : Context.Context {Type.Type B} \u2192 Type.Type B \u2192 Set {- of constants -}}\n where\n\n-- Terms of languages described in Plotkin style\n\nopen import Function using (_\u2218_)\nopen import Data.Product\n\nopen Type B\nopen Context {Type}\n\nopen import Denotation.Environment Type\nopen import Syntax.Context.Plotkin B\n\n-- Declarations of Term and Terms to enable mutual recursion\ndata Term\n (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set\n\ndata Terms\n (\u0393 : Context) :\n (\u03a3 : Context) \u2192 Set\n\n-- (Term \u0393 \u03c4) represents a term of type \u03c4\n-- with free variables bound in \u0393.\ndata Term \u0393 where\n const : \u2200 {\u03a3 \u03c4} \u2192\n (c : C \u03a3 \u03c4) \u2192\n Terms \u0393 \u03a3 \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\n-- (Terms \u0393 \u03a3) represents a list of terms with types from \u03a3\n-- with free variables bound in \u0393.\ndata Terms \u0393 where\n \u2205 : Terms \u0393 \u2205\n _\u2022_ : \u2200 {\u03c4 \u03a3} \u2192\n Term \u0393 \u03c4 \u2192\n Terms \u0393 \u03a3 \u2192\n Terms \u0393 (\u03c4 \u2022 \u03a3)\n\ninfixr 9 _\u2022_\n\n-- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n-- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\nlift-diff-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4) \u00d7 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-diff-apply diff apply {base \u03b9} = diff , apply\nlift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\nlift-diff : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4)\n\nlift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\nlift-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n-- Weakening\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\n\nweakenAll : \u2200 {\u0393\u2081 \u0393\u2082 \u03a3} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Terms \u0393\u2081 \u03a3 \u2192\n Terms \u0393\u2082 \u03a3\n\nweaken \u0393\u2081\u227c\u0393\u2082 (const c ts) = const c (weakenAll \u0393\u2081\u227c\u0393\u2082 ts)\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (lift \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\nweakenAll \u0393\u2081\u227c\u0393\u2082 \u2205 = \u2205\nweakenAll \u0393\u2081\u227c\u0393\u2082 (t \u2022 ts) = weaken \u0393\u2081\u227c\u0393\u2082 t \u2022 weakenAll \u0393\u2081\u227c\u0393\u2082 ts\n\n-- Specialized weakening\nweaken\u2081 : \u2200 {\u0393 \u03c3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4\nweaken\u2081 t = weaken (drop _ \u2022 \u227c-refl) t\n\nweaken\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u0393) \u03c4\nweaken\u2082 t = weaken (drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\nweaken\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u03b3 \u2022 \u0393) \u03c4\nweaken\u2083 t = weaken (drop _ \u2022 drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n-- Shorthands for nested applications\napp\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3\napp\u2082 f x = app (app f x)\n\napp\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4\napp\u2083 f x = app\u2082 (app f x)\n\napp\u2084 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5\napp\u2084 f x = app\u2083 (app f x)\n\napp\u2085 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6\napp\u2085 f x = app\u2084 (app f x)\n\napp\u2086 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6 \u21d2 \u03b7) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6 \u2192 Term \u0393 \u03b7\napp\u2086 f x = app\u2085 (app f x)\n\nUncurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nUncurriedTermConstructor \u0393 \u03a3 \u03c4 = Terms \u0393 \u03a3 \u2192 Term \u0393 \u03c4\n\nCurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nCurriedTermConstructor \u0393 \u2205 \u03c4\u2032 = Term \u0393 \u03c4\u2032\nCurriedTermConstructor \u0393 (\u03c4 \u2022 \u03a3) \u03c4\u2032 = Term \u0393 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\u2032\n\n-- helper for lift-\u03b7-const, don't try to understand at home\nlift-\u03b7-const-rec : \u2200 {\u03a3 \u0393 \u03c4} \u2192 UncurriedTermConstructor \u0393 \u03a3 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const-rec {\u2205} k = k \u2205\nlift-\u03b7-const-rec {\u03c4 \u2022 \u03a3} k = \u03bb t \u2192 lift-\u03b7-const-rec (\u03bb ts \u2192 k (t \u2022 ts))\n\nlift-\u03b7-const : \u2200 {\u03a3 \u03c4} \u2192 C \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const constant = lift-\u03b7-const-rec (const constant)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"00497b63c37149f1b209984e58341824b3174de3","subject":"Complete proof (up to termination)","message":"Complete proof (up to termination)\n\nThis completes the proof of the fundamental lemma for this step-indexed relation\nup to a few missing termination proofs, which are left external.\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/FunBigStepSILR2.agda","new_file":"Thesis\/FunBigStepSILR2.agda","new_contents":"-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\" (ISAC below), POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\n-- CHEATS:\n-- \"Fuctional big-step semantics\" requires an external termination proof for the\n-- semantics. There it is also mechanized, here it isn't. Worse, the same\n-- termination problem affects some lemmas about the semantics.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen import Data.Nat.Properties.Simple\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n\n-- WARNING: ISAC's big-step semantics produces a step count as \"output\". But\n-- that would not help Agda establish termination. That's only a problem for a\n-- functional big-step semantics, not for a relational semantics.\n--\n-- So, instead, I tried to use a sort of writer monad: the interpreter gets fuel\n-- and returns the remaining fuel. That's the same trick as in \"functional\n-- big-step semantics\". That *makes* the function terminating, even though Agda\n-- cannot see this because it does not know that the returned fuel is no bigger.\n\n-- Since we focus for now on STLC, unlike that\n-- paper, we could avoid error values because we keep types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) zero = TimeOut\nevalConst (lit v) (suc n) = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\neval (app s t) \u03c1 (suc n) = (eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)) n\n\neval-const-dec : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-const-dec (lit v) zero n1 ()\neval-const-dec (lit v) (suc n0) .n0 refl = \u2264-step \u2264-refl\n\n{-# TERMINATING #-}\neval-dec : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-dec (const c) \u03c1 v n0 n1 eq = eval-const-dec c n0 n1 eq\neval-dec (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (app s t) \u03c1 v zero n1 ()\neval-dec (app s t) \u03c1 v (suc n0) n3 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv sn1 | [ seq ] with eval t \u03c1 sn1 | inspect (eval t \u03c1) sn1\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv@(closure st s\u03c1) sn1 | [ seq ] | (Done tv tn2) | [ teq ] = \u2264-step (\u2264-trans (\u2264-trans (eval-dec st _ _ _ _ eq) (eval-dec t _ _ _ _ teq)) (eval-dec s _ _ _ _ seq))\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | Error | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | TimeOut | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Error | [ seq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | TimeOut | [ seq ]\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1)\neval-const-mono (lit v) zero n1 ()\neval-const-mono (lit v) (suc n0) .n0 refl = refl\n\n-- ARGH\n{-# TERMINATING #-}\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1)\neval-mono (const c) \u03c1 v n0 n1 eq = eval-const-mono c n0 n1 eq\neval-mono (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\neval-mono (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\neval-mono (app s t) \u03c1 v zero n1 ()\neval-mono (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-mono t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n\neval-adjust-plus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1)\neval-adjust-plus zero t \u03c1 v n0 n1 eq = eq\neval-adjust-plus (suc d) t \u03c1 v n0 n1 eq = eval-mono t \u03c1 v (d + n0) (d + n1) (eval-adjust-plus d t \u03c1 v n0 n1 eq)\n\neval-const-strengthen : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1) \u2192 evalConst c n0 \u2261 Done v n1\neval-const-strengthen (lit v) zero n1 ()\neval-const-strengthen (lit v) (suc n0) .n0 refl = refl\n\n-- {-# TERMINATING #-}\n-- eval-strengthen : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-strengthen (const c) \u03c1 v n0 n1 eq = eval-const-strengthen c n0 n1 eq\n-- eval-strengthen (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq = {!!}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 (suc n0) | inspect (eval s \u03c1) (suc n0)\n-- -- eval-strengthen (app s t) \u03c1 v\u2081 (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 n0 = {!eval-strengthen s \u03c1 v n0 n1 seq !}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-strengthen t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | Error | [ seq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | TimeOut | [ seq ] = {!!}\n\n-- eval-adjust-minus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 {\u03c1 v} n0 n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-adjust-minus zero t n0 n1 eq = eq\n-- eval-adjust-minus (suc d) t n0 n1 eq = eval-adjust-minus d t n0 n1 (eval-strengthen t _ _ (d + n0) (d + n1) eq)\n\nmutual\n -- This is not the same definition of relT, but it is equivalent.\n relT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\n -- This equation is a lemma in the original definition.\n relT {\u03c4} t1 t2 \u03c11 \u03c12 zero = \u22a4\n -- To compare this definition, note that the original k is suc n here.\n relT {\u03c4} t1 t2 \u03c11 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n -- Originally we have 0 \u2264 j < k, so j < suc n, so k - j = suc n - j.\n -- It follows that 0 < k - j \u2264 k, hence suc n - j \u2264 suc n, or n - j \u2264 n.\n -- Here, instead of binding j we bind n-j = n - j, require n - j \u2264 n, and\n -- use suc n-j instead of k - j.\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n -- The next assumption is important. This still says that evaluation consumes j steps.\n -- Since j \u2264 n, it is OK to start evaluation with n steps.\n -- Starting with (suc n) and getting suc n-j is equivalent, per eval-mono\n -- and eval-strengthen. But in practice this version is easier to use.\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 0 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n -- Here, computing t2 is allowed to take an unbounded number of steps. Having to write a number at all is annoying.\n\n relV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n relV nat v1 v2 n = v1 \u2261 v2\n relV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) n =\n \u2200 (k : \u2115) (k\u2264n : k < n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\n -- Here, in the conclusion, I'm not relating app (closure t1 \u03c11) v1 with app\n -- (closure t2 \u03c12) v2 (or some encoding of that that actually works), but the\n -- result of taking a step from that configuration. That is important, because\n -- both Pitts' \"Step-Indexed Biorthogonality: a Tutorial Example\" and\n -- \"Imperative Self-Adjusting Computation\" do the same thing (and point out it's\n -- important).\n\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono m n m\u2264n nat v1 v2 vv = vv\nrelV-mono m n m\u2264n (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) ff k k\u2264m = ff k (\u2264-trans k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 .n n-j\u2264n refl = v2 , zero , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\nlt1 : \u2200 {k n} \u2192 k < n \u2192 k \u2264 n\nlt1 (s\u2264s p) = \u2264-step p\n\n-- relV-apply : \u2200 {\u03c3 \u03c4 \u0393} (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t v1 \u03c12 n-j\n-- n1 sv1 sv2\n-- (svv : relV (\u03c3 \u21d2 \u03c4) sv1 sv2 n1)\n-- n2 tv1 tv2\n-- (tvv : relV \u03c3 tv1 tv2 (suc (suc n2)))\n-- (eq : apply sv1 tv1 n2 \u2261 Done v1 n-j) \u2192\n-- -- (tvv)\n-- -- (eqv1)\n-- \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval (app s t) \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n-- relV-apply s t v1 \u03c1 n-j n1 (closure st \u03c11) (closure st2 \u03c12) svv n2 tv1 tv2 tvv eq = {! !}\n-- -- relV-apply s t v1 \u03c12 n-j _ _ sv2 svv _ tv1 tv2 tvv eq\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\nfundamental (const (lit v)) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (const (lit v)) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 .(intV v) .n n-j\u2264n refl = intV v , suc zero , refl , refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(closure t \u03c11) .n n-j\u2264n refl = closure t \u03c12 , zero , refl , (\u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k (suc n) (lt1 k\u2264n) _ _ _ \u03c1\u03c1))\nfundamental (app s t) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq with eval s \u03c11 n | inspect (eval s \u03c11) n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done sv1 n1 | [ s1eq ] with eval-dec s _ _ n n1 s1eq | eval t \u03c11 n1 | inspect (eval t \u03c11) n1\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done (closure st1 s\u03c11) n1 | [ s1eq ] | n1\u2264n | Done tv1 n2 | [ t1eq ] with eval-dec t _ _ n1 n2 t1eq\n... | n2\u2264n1 with fundamental s (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) (suc n1) (s\u2264s n1\u2264n) (eval-mono s \u03c11 (closure st1 s\u03c11) n n1 s1eq)\n | fundamental t (suc (suc n1)) \u03c11 \u03c12 (rel\u03c1-mono (suc (suc n1)) (suc (suc n)) (s\u2264s (s\u2264s n1\u2264n)) _ _ _ \u03c1\u03c1) tv1 (suc n2) (s\u2264s n2\u2264n1) (eval-mono t \u03c11 tv1 n1 n2 t1eq)\n... | sv2@(closure st2 s\u03c12) , sn3 , s2eq , svv | tv2 , tn3 , t2eq , tvv with svv (suc n2) (s\u2264s (s\u2264s n2\u2264n1)) tv1 tv2 (relV-mono _ _ (s\u2264s (n\u22641+n n2)) _ _ _ tvv ) v1 n-j (eval-dec st1 _ _ _ _ eq) eq\n... | v2 , n3 , eq2 , vv = v2 , suc (sn3 + (tn3 + n3)) , comp , vv\n where\n s2eq-adj : eval s \u03c12 (sn3 + (tn3 + n3)) \u2261 Done (closure st2 s\u03c12) (tn3 + n3)\n s2eq-adj rewrite +-comm sn3 (tn3 + n3)| cong (Done (closure st2 s\u03c12)) (sym (+-right-identity (tn3 + n3))) = eval-adjust-plus (tn3 + n3) s _ sv2 _ _ s2eq\n t2eq-adj : eval t \u03c12 (tn3 + n3) \u2261 Done tv2 n3\n t2eq-adj rewrite +-comm tn3 n3 | cong (Done tv2) (sym (+-right-identity n3)) = eval-adjust-plus n3 t _ tv2 _ _ t2eq\n\n comp : (eval s \u03c12 >>= (\u03bb sv \u2192 eval t \u03c12 >>= apply sv))\n (sn3 + (tn3 + n3))\n \u2261 Done v2 0\n comp rewrite s2eq-adj | t2eq-adj = eq2\n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | Error | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | TimeOut | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Error | [ s1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | TimeOut | [ s1eq ]\n","old_contents":"{-# OPTIONS --allow-unsolved-metas #-}\n-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\" (ISAC below), POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n\n-- WARNING: ISAC's big-step semantics produces a step count as \"output\". But\n-- that would not help Agda establish termination. That's only a problem for a\n-- functional big-step semantics, not for a relational semantics.\n--\n-- So, instead, I tried to use a sort of writer monad: the interpreter gets fuel\n-- and returns the remaining fuel. That's the same trick as in \"functional\n-- big-step semantics\". However that doesn't help termination either,\n-- since Agda doesn't see that the returned fuel is no bigger.\n\n-- Since we focus for now on STLC, unlike that\n-- paper, we can avoid error values by keeping types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) zero = TimeOut\nevalConst (lit v) (suc n) = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\neval (app s t) \u03c1 (suc n) = (eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)) n\n\neval-const-dec : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-const-dec (lit v) zero n1 ()\neval-const-dec (lit v) (suc n0) .n0 refl = \u2264-step \u2264-refl\n\n{-# TERMINATING #-}\neval-dec : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-dec (const c) \u03c1 v n0 n1 eq = eval-const-dec c n0 n1 eq\neval-dec (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (app s t) \u03c1 v zero n1 ()\neval-dec (app s t) \u03c1 v (suc n0) n3 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv sn1 | [ seq ] with eval t \u03c1 sn1 | inspect (eval t \u03c1) sn1\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv@(closure st s\u03c1) sn1 | [ seq ] | (Done tv tn2) | [ teq ] = \u2264-step (\u2264-trans (\u2264-trans (eval-dec st _ _ _ _ eq) (eval-dec t _ _ _ _ teq)) (eval-dec s _ _ _ _ seq))\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | Error | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | TimeOut | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Error | [ seq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | TimeOut | [ seq ]\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1)\neval-const-mono (lit v) zero n1 ()\neval-const-mono (lit v) (suc n0) .n0 refl = refl\n\n-- ARGH\n{-# TERMINATING #-}\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1)\neval-mono (const c) \u03c1 v n0 n1 eq = eval-const-mono c n0 n1 eq\neval-mono (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\neval-mono (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\neval-mono (app s t) \u03c1 v zero n1 ()\neval-mono (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-mono t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n\nmodule Alt where\n mutual\n relT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\n relT {\u03c4} t1 t2 \u03c11 \u03c12 n =\n (v1 : Val \u03c4) \u2192\n \u2200 n-j (n-j\u2264n : n-j < n) \u2192\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n -- Here, computing t2 is allowed to take an unbounded number of steps. Having to write a number at all is annoying.\n\n relV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n -- Seems the proof for abs would go through even if here we do not step down.\n -- However, that only works as long as we use a typed language; not stepping\n -- down here, in an untyped language, gives a non-well-founded definition.\n relV nat v1 v2 n = v1 \u2261 v2\n relV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) n =\n \u2200 (k : \u2115) (k\u2264n : k < n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\nmutual\n relT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\n relT {\u03c4} t1 t2 \u03c11 \u03c12 zero = \u22a4\n relT {\u03c4} t1 t2 \u03c11 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n -- Here, computing t2 is allowed to take an unbounded number of steps. Having to write a number at all is annoying.\n\n relV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n relV \u03c4 v1 v2 zero = \u22a4\n -- Seems the proof for abs would go through even if here we do not step down.\n -- However, that only works as long as we use a typed language; not stepping\n -- down here, in an untyped language, gives a non-well-founded definition.\n relV nat v1 v2 (suc n) = v1 \u2261 v2\n relV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) =\n \u2200 (k : \u2115) (k\u2264n : k \u2264 n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\n -- Here, in the conclusion, I'm not relating app (closure t1 \u03c11) v1 with app\n -- (closure t2 \u03c12) v2 (or some encoding of that that actually works), but the\n -- result of taking a step from that configuration. That is important, because\n -- both Pitts' \"Step-Indexed Biorthogonality: a Tutorial Example\" and\n -- \"Imperative Self-Adjusting Computation\" do the same thing (and point out it's\n -- important).\n\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono zero n m\u2264n \u03c4 v1 v2 vv = tt\nrelV-mono (suc m) (suc n) (s\u2264s m\u2264n) nat v1 v2 vv = vv\nrelV-mono (suc m) (suc n) (s\u2264s m\u2264n) (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) ff k k\u2264m = ff k (\u2264-trans k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 .n n-j\u2264n refl = v2 , zero , zero , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\n-- relT (app s t) (app s t)\n\nrelV-apply : \u2200 {\u03c3 \u03c4 \u0393} (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t v1 \u03c12 n-j\n n1 sv1 sv2\n (svv : relV (\u03c3 \u21d2 \u03c4) sv1 sv2 n1)\n n2 tv1 tv2\n (tvv : relV \u03c3 tv1 tv2 (suc (suc n2)))\n (eq : apply sv1 tv1 n2 \u2261 Done v1 n-j) \u2192\n -- (tvv)\n -- (eqv1)\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval (app s t) \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\nrelV-apply s t v1 \u03c1 n-j n1 sv1 sv2 svv n2 tv1 tv2 tvv eq = {!!}\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\nfundamental (const (lit v)) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (const (lit v)) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 .(intV v) .n n-j\u2264n refl = intV v , suc zero , zero , refl , refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(closure t \u03c11) .n n-j\u2264n refl = closure t \u03c12 , n , n , refl , (\u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k (suc n) (\u2264-step k\u2264n) _ _ _ \u03c1\u03c1))\nfundamental (app s t) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq with eval s \u03c11 n | inspect (eval s \u03c11) n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done sv1 n1 | [ s1eq ] with eval t \u03c11 n1 | inspect (eval t \u03c11) n1\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done (closure st1 s\u03c11) n1 | [ s1eq ] | Done tv1 n2 | [ t1eq ] with fundamental s (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) (suc n1) (s\u2264s (eval-dec s \u03c11 _ n n1 s1eq)) (eval-mono s \u03c11 (closure st1 s\u03c11) n n1 s1eq) | fundamental t (suc (suc n1)) \u03c11 \u03c12 (rel\u03c1-mono (suc (suc n1)) (suc (suc n)) (s\u2264s (s\u2264s (eval-dec s \u03c11 _ n n1 s1eq))) _ _ _ \u03c1\u03c1) tv1 (suc n2) (s\u2264s (eval-dec t \u03c11 _ n1 n2 t1eq)) (eval-mono t \u03c11 tv1 n1 n2 t1eq)\n... | sv2@(closure st2 s\u03c12) , sn3 , sn4 , s2eq , svv | tv2 , tn3 , tn4 , t2eq , tvv = {! relV-apply s t v1 \u03c12 n-j _ _ sv2 svv _ tv1 tv2 tvv eq!}\n--\n-- {!eval s \u03c12 !}\n-- fundamental s (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) ? ?\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | Error | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | TimeOut | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Error | [ s1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | TimeOut | [ s1eq ]\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"55154fffeaa270b4adfd6d4cc6cb42a3f47df6a6","subject":"Cosmetic changes.","message":"Cosmetic changes.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Program\/ABP\/ProofSpecificationI.agda","new_file":"src\/fot\/FOTC\/Program\/ABP\/ProofSpecificationI.agda","new_contents":"------------------------------------------------------------------------------\n-- The alternating bit protocol (ABP) satisfies the specification\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module proves the correctness of the ABP following the\n-- formalization in [1].\n\n-- [1] Peter Dybjer and Herbert Sander. A functional programming\n-- approach to the specification and verification of concurrent\n-- systems. Formal Aspects of Computing, 1:303\u2013319, 1989.\n\nmodule FOTC.Program.ABP.ProofSpecificationI where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Data.Bool\nopen import FOTC.Data.Bool.PropertiesI\nopen import FOTC.Data.Stream\nopen import FOTC.Program.ABP.ABP\nopen import FOTC.Program.ABP.Fair\nopen import FOTC.Program.ABP.Lemma1I\nopen import FOTC.Program.ABP.Lemma2I\nopen import FOTC.Program.ABP.Terms\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n-- Main theorem.\nspec : \u2200 {b is os\u2080 os\u2081} \u2192 Bit b \u2192 Stream is \u2192 Fair os\u2080 \u2192 Fair os\u2081 \u2192\n is \u2248 transfer b os\u2080 os\u2081 is\nspec {b} {is} {os\u2080} {os\u2081} Bb Sis Fos\u2080 Fos\u2081 = \u2248-coind B prf\u2081 prf\u2082\n where\n prf\u2081 : \u2200 {is js} \u2192 B is js \u2192\n \u2203[ i' ] \u2203[ is' ] \u2203[ js' ] is \u2261 i' \u2237 is' \u2227 js \u2261 i' \u2237 js' \u2227 B is' js'\n prf\u2081 {is} {js} (b , os\u2080 , os\u2081 , as , bs , cs , ds , Sis , Bb , Fos\u2080 , Fos\u2081 , h)\n with Stream-unf Sis\n ... | (i' , is' , is\u2261i'\u2237is , Sis') =\n i' , is' , js' , is\u2261i'\u2237is , js\u2261i'\u2237js' , Bis'js'\n where\n ABP-helper : is \u2261 i' \u2237 is' \u2192\n ABP b is os\u2080 os\u2081 as bs cs ds js \u2192\n ABP b (i' \u2237 is') os\u2080 os\u2081 as bs cs ds js\n ABP-helper h\u2081 h\u2082 = subst (\u03bb t \u2192 ABP b t os\u2080 os\u2081 as bs cs ds js) h\u2081 h\u2082\n\n ABP'-lemma\u2081 : \u2203[ os\u2080' ] \u2203[ os\u2081' ] \u2203[ as' ] \u2203[ bs' ] \u2203[ cs' ] \u2203[ ds' ] \u2203[ js' ]\n Fair os\u2080'\n \u2227 Fair os\u2081'\n \u2227 ABP' b i' is' os\u2080' os\u2081' as' bs' cs' ds' js'\n \u2227 js \u2261 i' \u2237 js'\n ABP'-lemma\u2081 = lemma\u2081 Bb Fos\u2080 Fos\u2081 (ABP-helper is\u2261i'\u2237is h)\n\n -- Following Martin Escardo advice (see Agda mailing list, heap\n -- mistery) we use pattern matching instead of \u2203 eliminators to\n -- project the elements of the existentials.\n\n -- 2011-08-25 update: It does not seems strictly necessary because\n -- the Agda issue 415 was fixed.\n\n js' : D\n js' with ABP'-lemma\u2081\n ... | _ , _ , _ , _ , _ , _ , js' , _ = js'\n\n js\u2261i'\u2237js' : js \u2261 i' \u2237 js'\n js\u2261i'\u2237js' with ABP'-lemma\u2081\n ... | _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , h = h\n\n ABP-lemma\u2082 : \u2203[ os\u2080'' ] \u2203[ os\u2081'' ] \u2203[ as'' ] \u2203[ bs'' ] \u2203[ cs'' ] \u2203[ ds'' ]\n Fair os\u2080''\n \u2227 Fair os\u2081''\n \u2227 ABP (not b) is' os\u2080'' os\u2081'' as'' bs'' cs'' ds'' js'\n ABP-lemma\u2082 with ABP'-lemma\u2081\n ABP-lemma\u2082 | _ , _ , _ , _ , _ , _ , _ , Fos\u2080' , Fos\u2081' , abp' , _ =\n lemma\u2082 Bb Fos\u2080' Fos\u2081' abp'\n\n Bis'js' : B is' js'\n Bis'js' with ABP-lemma\u2082\n ... | os\u2080'' , os\u2081'' , as'' , bs'' , cs'' , ds'' , Fos\u2080'' , Fos\u2081'' , abp =\n not b , os\u2080'' , os\u2081'' , as'' , bs'' , cs'' , ds''\n , Sis' , not-Bool Bb , Fos\u2080'' , Fos\u2081'' , abp\n\n prf\u2082 : B is (transfer b os\u2080 os\u2081 is)\n prf\u2082 = b\n , os\u2080\n , os\u2081\n , has a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , hbs a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , hcs a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , hds a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , Sis\n , Bb\n , Fos\u2080\n , Fos\u2081\n , has-eq a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , hbs-eq a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , hcs-eq a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , hds-eq a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , trans (transfer-eq b os\u2080 os\u2081 is) (genTransfer-eq a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is)\n where\n a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 : D\n a\u2081 = send \u00b7 b\n a\u2082 = ack \u00b7 b\n a\u2083 = out \u00b7 b\n a\u2084 = corrupt \u00b7 os\u2080\n a\u2085 = corrupt \u00b7 os\u2081\n","old_contents":"------------------------------------------------------------------------------\n-- The alternating bit protocol (ABP) satisfies the specification\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module proves the correctness of the ABP following the\n-- formalization in [1].\n\n-- [1] Peter Dybjer and Herbert Sander. A functional programming\n-- approach to the specification and verification of concurrent\n-- systems. Formal Aspects of Computing, 1:303\u2013319, 1989.\n\nmodule FOTC.Program.ABP.ProofSpecificationI where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Data.Bool\nopen import FOTC.Data.Bool.PropertiesI\nopen import FOTC.Data.Stream\nopen import FOTC.Program.ABP.ABP\nopen import FOTC.Program.ABP.Fair\nopen import FOTC.Program.ABP.Lemma1I\nopen import FOTC.Program.ABP.Lemma2I\nopen import FOTC.Program.ABP.Terms\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n-- Main theorem.\nspec : \u2200 {b is os\u2080 os\u2081} \u2192 Bit b \u2192 Stream is \u2192 Fair os\u2080 \u2192 Fair os\u2081 \u2192\n is \u2248 transfer b os\u2080 os\u2081 is\nspec {b} {is} {os\u2080} {os\u2081} Bb Sis Fos\u2080 Fos\u2081 = \u2248-coind B prf\u2081 prf\u2082\n where\n prf\u2081 : \u2200 {is js} \u2192 B is js \u2192\n \u2203[ i' ] \u2203[ is' ] \u2203[ js' ] is \u2261 i' \u2237 is' \u2227 js \u2261 i' \u2237 js' \u2227 B is' js'\n prf\u2081 {is} {js} (b , os\u2080 , os\u2081 , as , bs , cs , ds , Sis , Bb , Fos\u2080 , Fos\u2081 , h)\n with Stream-unf Sis\n ... | (i' , is' , is\u2261i'\u2237is , Sis') =\n i' , is' , js' , is\u2261i'\u2237is , js\u2261i'\u2237js' , Bis'js'\n where\n ABP-helper : is \u2261 i' \u2237 is' \u2192\n ABP b is os\u2080 os\u2081 as bs cs ds js \u2192\n ABP b (i' \u2237 is') os\u2080 os\u2081 as bs cs ds js\n ABP-helper h\u2081 h\u2082 = subst (\u03bb t \u2192 ABP b t os\u2080 os\u2081 as bs cs ds js) h\u2081 h\u2082\n\n ABP'-lemma\u2081 : \u2203[ os\u2080' ] \u2203[ os\u2081' ] \u2203[ as' ] \u2203[ bs' ] \u2203[ cs' ] \u2203[ ds' ] \u2203[ js' ]\n Fair os\u2080'\n \u2227 Fair os\u2081'\n \u2227 ABP' b i' is' os\u2080' os\u2081' as' bs' cs' ds' js'\n \u2227 js \u2261 i' \u2237 js'\n ABP'-lemma\u2081 = lemma\u2081 Bb Fos\u2080 Fos\u2081 (ABP-helper is\u2261i'\u2237is h)\n\n -- Following Martin Escardo advice (see Agda mailing list, heap\n -- mistery) we use pattern matching instead of \u2203 eliminators to\n -- project the elements of the existentials.\n\n -- 2011-08-25 update: It does not seems strictly necessary because\n -- the Agda issue 415 was fixed.\n\n js' : D\n js' with ABP'-lemma\u2081\n ... | _ , _ , _ , _ , _ , _ , js' , _ = js'\n\n js\u2261i'\u2237js' : js \u2261 i' \u2237 js'\n js\u2261i'\u2237js' with ABP'-lemma\u2081\n ... | _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , h = h\n\n ABP-lemma\u2082 : \u2203[ os\u2080'' ] \u2203[ os\u2081'' ] \u2203[ as'' ] \u2203[ bs'' ] \u2203[ cs'' ] \u2203[ ds'' ]\n Fair os\u2080''\n \u2227 Fair os\u2081''\n \u2227 ABP (not b) is' os\u2080'' os\u2081'' as'' bs'' cs'' ds'' js'\n ABP-lemma\u2082 with ABP'-lemma\u2081\n ABP-lemma\u2082 | _ , _ , _ , _ , _ , _ , _ , Fos\u2080' , Fos\u2081' , abp' , _ =\n lemma\u2082 Bb Fos\u2080' Fos\u2081' abp'\n\n Bis'js' : B is' js'\n Bis'js' with ABP-lemma\u2082\n ... | os\u2080'' , os\u2081'' , as'' , bs'' , cs'' , ds'' , Fos\u2080'' , Fos\u2081'' , abp =\n not b , os\u2080'' , os\u2081'' , as'' , bs'' , cs'' , ds''\n , Sis' , not-Bool Bb , Fos\u2080'' , Fos\u2081'' , abp\n\n prf\u2082 : B is (transfer b os\u2080 os\u2081 is)\n prf\u2082 = b\n , os\u2080\n , os\u2081\n , has a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , hbs a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , hcs a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , hds a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , Sis\n , Bb\n , Fos\u2080\n , Fos\u2081\n , has-eq a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , hbs-eq a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , hcs-eq a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , hds-eq a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is\n , trans (transfer-eq b os\u2080 os\u2081 is) (genTransfer-eq a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 is)\n where\n a\u2081 a\u2082 a\u2083 a\u2084 a\u2085 : D\n a\u2081 = send \u00b7 b\n a\u2082 = ack \u00b7 b\n a\u2083 = out \u00b7 b\n a\u2084 = corrupt \u00b7 os\u2080\n a\u2085 = corrupt \u00b7 os\u2081\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d99b8dbc1d8b11506fe41c1f369efa0c13dc01e3","subject":"Adapt correctness framework to validity-proof embedded \u0394-terms.","message":"Adapt correctness framework to validity-proof embedded \u0394-terms.\n\nOld-commit-hash: 3a38c2651dcf938367d84ffa6dee36838a4b4fb8\n","repos":"inc-lc\/ilc-agda","old_file":"experimental\/TaggedDeltaTypes.agda","new_file":"experimental\/TaggedDeltaTypes.agda","new_contents":"{-\nThe goal of this file is to make the domain of \u0394 types smaller\nso as to be nearer to full abstraction and hopefully close\nenough for the purpose of having explicit nil changes.\n-}\n\nmodule TaggedDeltaTypes where\n\nopen import Data.NatBag renaming\n (map to mapBag ; empty to emptyBag ; update to updateBag)\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Nat\nopen import Data.Unit using (\u22a4 ; tt)\nimport Data.Integer as \u2124\nimport Data.Product as Product\nopen import Data.Product using (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\nimport Level\n\n-- Postulates: Extensionality and bag properties (#55)\npostulate extensionality : Extensionality Level.zero Level.zero\n--\n-- open import Data.NatBag.Properties\npostulate b\\\\b=\u2205 : \u2200 {{b : Bag}} \u2192 b \\\\ b \u2261 emptyBag\npostulate b++\u2205=b : \u2200 {{b : Bag}} \u2192 b ++ emptyBag \u2261 b\npostulate \u2205++b=b : \u2200 {{b : Bag}} \u2192 emptyBag ++ b \u2261 b\npostulate b++[d\\\\b]=d : \u2200 {b d} \u2192 b ++ (d \\\\ b) \u2261 d\npostulate [b++d]\\\\b=d : \u2200 {b d} \u2192 (b ++ d) \\\\ b \u2261 d\npostulate\n [a++b]\\\\[c++d]=[a\\\\c]++[b\\\\d] : \u2200 {a b c d} \u2192\n (a ++ b) \\\\ (c ++ d) \u2261 (a \\\\ c) ++ (b \\\\ d)\npostulate\n [a\\\\b]\\\\[c\\\\d]=[a\\\\c]\\\\[b\\\\d] : \u2200 {a b c d} \u2192\n (a \\\\ b) \\\\ (c \\\\ d) \u2261 (a \\\\ c) \\\\ (b \\\\ d)\n\n------------------------\n-- Syntax of programs --\n------------------------\n\ndata Type : Set where\n nats : Type\n bags : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u03c4 \u0393} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u03c4 \u03c4\u2032 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n bag : \u2200 {\u0393} \u2192 (b : Bag) \u2192 Term \u0393 bags\n\n var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (s : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n add : \u2200 {\u0393} \u2192 (s : Term \u0393 nats) \u2192 (t : Term \u0393 nats) \u2192 Term \u0393 nats\n map : \u2200 {\u0393} \u2192 (f : Term \u0393 (nats \u21d2 nats)) \u2192 (b : Term \u0393 bags) \u2192 Term \u0393 bags\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u0393\u227c\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0393\n\u0393\u227c\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0393 {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u0393\u227c\u0393 {\u0393}\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\nweaken subctx (bag b) = bag b\nweaken subctx (add t\u2081 t\u2082) = add (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (map f b) = map (weaken subctx f) (weaken subctx b)\n\n---------------------------\n-- Semantics of programs --\n---------------------------\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 nats \u27e7Type = \u2115\n\u27e6 bags \u27e7Type = Bag\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nidentity-weakening : \u2200 {\u0393} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192 \u27e6 \u0393\u227c\u0393 {\u0393} \u27e7 \u03c1 \u2261 \u03c1\nidentity-weakening {\u2205} {\u2205} = refl\nidentity-weakening {\u03c4 \u2022 \u0393} {v \u2022 \u03c1} =\n cong\u2082 _\u2022_ {x = v} refl (identity-weakening {\u0393} {\u03c1})\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\u27e6 bag b \u27e7Term \u03c1 = b\n\u27e6 add m n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 map f b \u27e7Term \u03c1 = mapBag (\u27e6 f \u27e7Term \u03c1) (\u27e6 b \u27e7Term \u03c1)\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n_\u27e8$\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\n\n-- infix 0 $ in Haskell\ninfixl 0 _\u27e8$\u27e9_\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = weaken-sound t\u2081 \u03c1 \u27e8$\u27e9 weaken-sound t\u2082 \u03c1\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (bag b) \u03c1 = refl\nweaken-sound (add m n) \u03c1 = cong\u2082 _+_ (weaken-sound m \u03c1) (weaken-sound n \u03c1)\nweaken-sound (map f b) \u03c1 =\n cong\u2082 mapBag (weaken-sound f \u03c1) (weaken-sound b \u03c1)\n\n----------------------------------------------------------\n-- Syntax and semantics of changes (they are entangled) --\n----------------------------------------------------------\n\ndata \u0394-Type : Set where\n \u0394 : (\u03c4 : Type) \u2192 \u0394-Type\n\ndata \u0394-Context : Set where\n \u0394 : (\u0393 : Context) \u2192 \u0394-Context\n\nEnv : Context \u2192 Set\nEnv \u0393 = \u27e6 \u0393 \u27e7Context\n\ndata \u0394-Term : \u0394-Context \u2192 \u0394-Type \u2192 Set\n\n-- Syntax of \u0394-Types\n-- ... is mutually recursive with semantics of \u0394-Terms,\n-- (because it embeds validity proofs)\n-- which is mutually recursive with validity and \u0394-environments,\n-- which is mutually recursive with \u2295,\n-- which is mutually recursive with \u229d and R[v,v\u229dv],\n-- which is mutually recursive with v\u2295[u\u229dv]=v, et cetera.\n\u27e6_\u27e7\u0394\u03c4 : \u0394-Type \u2192 Set\n\u0394-Env : Context \u2192 Set\n\u27e6_\u27e7\u0394 : \u2200 {\u03c4 : Type} {\u0393 : Context}\n \u2192 \u0394-Term (\u0394 \u0393) (\u0394 \u03c4) \u2192 \u0394-Env \u0393 \u2192 \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4\n_\u2295_ : \u2200 {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4 \u2192 \u27e6 \u03c4 \u27e7\n_\u229d_ : \u2200 {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4\nvalid : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4 \u2192 Set\nR[v,u\u229dv] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229d v)\nv\u2295[u\u229dv]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 v \u2295 (u \u229d v) \u2261 u\nignore : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394-Env \u0393) \u2192 \u27e6 \u0393 \u27e7\nupdate : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394-Env \u0393) \u2192 \u27e6 \u0393 \u27e7\n\ninfixl 6 _\u2295_ _\u229d_ -- as with + - in GHC.Num\n\ndata \u0394-Term where\n -- changes to numbers are replacement pairs\n \u0394nat : \u2200 {\u0393} \u2192 (old : \u2115) \u2192 (new : \u2115) \u2192 \u0394-Term (\u0394 \u0393) (\u0394 nats)\n -- changes to bags are bags\n \u0394bag : \u2200 {\u0393} \u2192 (db : Bag) \u2192 \u0394-Term (\u0394 \u0393) (\u0394 bags)\n -- changes to variables are variables\n \u0394var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 \u0394-Term (\u0394 \u0393) (\u0394 \u03c4)\n -- changes to abstractions are binders of x and dx\n \u0394abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : \u0394-Term (\u0394 (\u03c4\u2081 \u2022 \u0393)) (\u0394 \u03c4\u2082)) \u2192\n \u0394-Term (\u0394 \u0393) (\u0394 (\u03c4\u2081 \u21d2 \u03c4\u2082))\n -- changes to applications are applications of a value and a change\n \u0394app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192(ds : \u0394-Term (\u0394 \u0393) (\u0394 (\u03c4\u2081 \u21d2 \u03c4\u2082)))\n \u2192 ( t : Term \u0393 \u03c4\u2081)\n \u2192 (dt : \u0394-Term (\u0394 \u0393) (\u0394 \u03c4\u2081))\n \u2192 (R[t,dt] : {\u03c1 : \u0394-Env \u0393} \u2192 -- 'Tis but a proof.\n valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1)) \u2192\n \u0394-Term (\u0394 \u0393) (\u0394 \u03c4\u2082)\n -- changes to addition are changes to their components\n \u0394add : \u2200 {\u0393} \u2192 (ds : \u0394-Term (\u0394 \u0393) (\u0394 nats))\n \u2192 (dt : \u0394-Term (\u0394 \u0393) (\u0394 nats)) \u2192\n \u0394-Term (\u0394 \u0393) (\u0394 nats)\n -- There are two kinds of changes to maps:\n -- 0. recomputation,\n -- 1. mapping over changes,\n -- the latter used only with some form of isNil available.\n \u0394map\u2080 : \u2200 {\u0393} \u2192 ( f : Term \u0393 (nats \u21d2 nats))\n \u2192 (df : \u0394-Term (\u0394 \u0393) (\u0394 (nats \u21d2 nats)))\n \u2192 ( b : Term \u0393 bags)\n \u2192 (db : \u0394-Term (\u0394 \u0393) (\u0394 bags))\n \u2192 \u0394-Term (\u0394 \u0393) (\u0394 bags)\n \u0394map\u2081 : \u2200 {\u0393} \u2192 ( f : Term \u0393 (nats \u21d2 nats))\n \u2192 (db : \u0394-Term (\u0394 \u0393) (\u0394 bags))\n \u2192 \u0394-Term (\u0394 \u0393) (\u0394 bags)\n\n-- \u27e6_\u27e7\u0394\u03c4 : \u0394-Type \u2192 Set\n\u27e6 \u0394 nats \u27e7\u0394\u03c4 = \u2115 \u00d7 \u2115\n\u27e6 \u0394 bags \u27e7\u0394\u03c4 = Bag\n\u27e6 \u0394 (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\u0394\u03c4 =\n (v : \u27e6 \u03c4\u2081 \u27e7) \u2192 (dv : \u27e6 \u0394 \u03c4\u2081 \u27e7\u0394\u03c4) \u2192 valid v dv \u2192 \u27e6 \u0394 \u03c4\u2082 \u27e7\u0394\u03c4\n\nmeaning-\u0394\u03c4 : Meaning \u0394-Type\nmeaning-\u0394\u03c4 = meaning \u27e6_\u27e7\u0394\u03c4\n\n_\u2295_ {nats} n dn = proj\u2082 dn\n_\u2295_ {bags} b db = b ++ db\n_\u2295_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u2295 df v (v \u229d v) R[v,u\u229dv]\n\n_\u229d_ {nats} m n = (n , m)\n_\u229d_ {bags} b d = b \\\\ d\n_\u229d_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb v dv R[v,dv] \u2192 f (v \u2295 dv) \u229d g v\n\n-- valid : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid {nats} n dn = n \u2261 proj\u2081 dn\nvalid {bags} b db = \u22a4\nvalid {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (v : \u27e6 \u03c4\u2081 \u27e7) (dv : \u27e6 \u0394 \u03c4\u2081 \u27e7\u0394\u03c4) (R[v,dv] : valid v dv)\n \u2192 valid (f v) (df v dv R[v,dv])\n \u00d7 (f \u2295 df) (v \u2295 dv) \u2261 f v \u2295 df v dv R[v,dv]\n\neq1 : \u2200 {\u03c4\u2081 \u03c4\u2082 : Type} {f : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {df} {x : \u27e6 \u03c4\u2081 \u27e7} {dx} \u2192\n valid f df \u2192 (R[x,dx] : valid x dx) \u2192\n (f \u2295 df) (x \u2295 dx) \u2261 f x \u2295 df x dx R[x,dx]\n\neq1 R[f,df] R[x,dx] = proj\u2082 (R[f,df] _ _ R[x,dx])\n\nv\u2295[u\u229dv]=u {nats} {u} {v} = refl\nv\u2295[u\u229dv]=u {bags} {u} {v} = b++[d\\\\b]=d {v} {u}\nv\u2295[u\u229dv]=u {\u03c4\u2081 \u21d2 \u03c4\u2082} {u} {v} = extensionality (\u03bb w \u2192\n begin\n (v \u2295 (u \u229d v)) w\n \u2261\u27e8 refl \u27e9 -- for clarity\n v w \u2295 (u (w \u2295 (w \u229d w)) \u229d v w)\n \u2261\u27e8 cong (\u03bb hole \u2192 v w \u2295 (u hole \u229d v w)) v\u2295[u\u229dv]=u \u27e9\n v w \u2295 (u w \u229d v w)\n \u2261\u27e8 v\u2295[u\u229dv]=u \u27e9\n u w\n \u220e) where open \u2261-Reasoning\n\nR[v,u\u229dv] {nats} {u} {v} = refl\nR[v,u\u229dv] {bags} {u} {v} = tt\nR[v,u\u229dv] {\u03c4\u2081 \u21d2 \u03c4\u2082} {u} {v} = \u03bb w dw R[w,dw] \u2192\n let\n w\u2032 = w \u2295 dw\n in\n R[v,u\u229dv] -- NOT a self recursion: implicit arguments are different.\n ,\n (begin\n (v \u2295 (u \u229d v)) w\u2032\n \u2261\u27e8 cong (\u03bb hole \u2192 hole w\u2032) (v\u2295[u\u229dv]=u {_} {u} {v}) \u27e9\n u w\u2032\n \u2261\u27e8 sym (v\u2295[u\u229dv]=u {_} {u w\u2032} {v w}) \u27e9\n v w \u2295 (u \u229d v) w dw R[w,dw]\n \u220e) where open \u2261-Reasoning\n\nrecord Quadruple\n (A : Set) (B : A \u2192 Set) (C : (a : A) \u2192 B a \u2192 Set)\n (D : (a : A) \u2192 (b : B a) \u2192 (c : C a b) \u2192 Set): Set where\n constructor cons\n field\n car : A\n cadr : B car\n caddr : C car cadr\n cadddr : D car cadr caddr\n\nopen Quadruple public\n\n-- The type of environments ensures their consistency.\n-- \u0394-Env : Context \u2192 Set\n\u0394-Env \u2205 = EmptySet\n\u0394-Env (\u03c4 \u2022 \u0393) = Quadruple\n \u27e6 \u03c4 \u27e7\n (\u03bb v \u2192 \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4)\n (\u03bb v dv \u2192 valid v dv)\n (\u03bb v dv R[v,dv] \u2192 \u0394-Env \u0393)\n\n-- ignore : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394-Env \u0393) \u2192 Env \u0393\nignore {\u2205} \u03c1 = \u2205\nignore {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) = v \u2022 ignore \u03c1\n\n-- update : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394-Env \u0393) \u2192 Env \u0393\nupdate {\u2205} \u03c1 = \u2205\nupdate {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) = (v \u2295 dv) \u2022 update \u03c1\n\n\u27e6_\u27e7\u0394Var : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 \u0394-Env \u0393 \u2192 \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4\n\u27e6 this \u27e7\u0394Var (cons v dv R[v,dv] \u03c1) = dv\n\u27e6 that x \u27e7\u0394Var (cons v dv R[v,dv] \u03c1) = \u27e6 x \u27e7\u0394Var \u03c1\n\n-- \u27e6_\u27e7\u0394Var is not put into the Meaning type class\n-- because its argument is identical to that of \u27e6_\u27e7Var.\n\n-- \u27e6_\u27e7\u0394 : \u2200 {\u03c4 \u0393} \u2192 \u0394-Term (\u0394 \u0393) (\u0394 \u03c4) \u2192 \u0394-Env \u0393 \u2192 \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4\n\u27e6 \u0394nat old new \u27e7\u0394 \u03c1 = (old , new)\n\u27e6 \u0394bag db \u27e7\u0394 \u03c1 = db\n\u27e6 \u0394var x \u27e7\u0394 \u03c1 = \u27e6 x \u27e7\u0394Var \u03c1\n\u27e6 \u0394abs t \u27e7\u0394 \u03c1 = \u03bb v dv R[v,dv] \u2192 \u27e6 t \u27e7\u0394 (cons v dv R[v,dv] \u03c1)\n\u27e6 \u0394app ds t dt R[dt,t] \u27e7\u0394 \u03c1 =\n \u27e6 ds \u27e7\u0394 \u03c1 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1) R[dt,t]\n\u27e6 \u0394add ds dt \u27e7\u0394 \u03c1 =\n let\n (old-s , new-s) = \u27e6 ds \u27e7\u0394 \u03c1\n (old-t , new-t) = \u27e6 dt \u27e7\u0394 \u03c1\n in\n (old-s + old-t , new-s + new-t)\n\u27e6 \u0394map\u2080 f df b db \u27e7\u0394 \u03c1 =\n let\n v = \u27e6 b \u27e7 (ignore \u03c1)\n h = \u27e6 f \u27e7 (ignore \u03c1)\n dv = \u27e6 db \u27e7\u0394 \u03c1\n dh = \u27e6 df \u27e7\u0394 \u03c1\n in\n mapBag (h \u2295 dh) (v \u2295 dv) \\\\ mapBag h v\n\u27e6 \u0394map\u2081 f db \u27e7\u0394 \u03c1 = mapBag (\u27e6 f \u27e7 (ignore \u03c1)) (\u27e6 db \u27e7\u0394 \u03c1)\n\nmeaning-\u0394Term : \u2200 {\u03c4 \u0393} \u2192 Meaning (\u0394-Term (\u0394 \u0393) (\u0394 \u03c4))\nmeaning-\u0394Term = meaning \u27e6_\u27e7\u0394\n\n--------------------------------------------------------\n-- Program transformation and correctness (entangled) --\n--------------------------------------------------------\n\n\u0394-equiv : \u2200 {\u03c4 : Type} \u2192 (du : \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4) (dv : \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4) \u2192 Set\n\u0394-equiv {\u03c4} du dv =\n \u2200 {v : \u27e6 \u03c4 \u27e7} (R[v,du] : valid v du) (R[v,dv] : valid v dv) \u2192\n v \u2295 du \u2261 v \u2295 dv\n\nderive : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394-Term (\u0394 \u0393) (\u0394 \u03c4)\n\nvalidity : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394-Env \u0393} \u2192\n valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\ncorrectness : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394-Env \u0393} \u2192\n \u0394-equiv (\u27e6 derive t \u27e7 \u03c1) (\u27e6 t \u27e7 (update \u03c1) \u229d \u27e6 t \u27e7 (ignore \u03c1))\n\ncorollary : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394-Env \u0393} \u2192\n \u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (update \u03c1)\n\ncorollary {\u03c4} {\u0393} {t} {\u03c1} =\n let\n v = \u27e6 t \u27e7 (ignore \u03c1)\n v\u2032 = \u27e6 t \u27e7 (update \u03c1)\n in\n begin\n v \u2295 \u27e6 derive t \u27e7 \u03c1\n \u2261\u27e8 correctness {\u03c4} {\u0393} {t} {\u03c1} {v} validity R[v,u\u229dv] \u27e9\n v \u2295 (v\u2032 \u229d v)\n \u2261\u27e8 v\u2295[u\u229dv]=u \u27e9\n v\u2032\n \u220e where open \u2261-Reasoning\n\n-- derive : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394-Term (\u0394 \u0393) (\u0394 \u03c4)\nderive (nat n) = \u0394nat n n\nderive (bag b) = \u0394bag b\nderive (var x) = \u0394var x\nderive (abs t) = \u0394abs (derive t)\nderive (app s t) = \u0394app (derive s) t (derive t) validity\nderive (add s t) = \u0394add (derive s) (derive t)\nderive (map f b) = \u0394map\u2080 f (derive f) b (derive b)\n\nvalidity-var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192\n \u2200 {\u03c1 : \u0394-Env \u0393} \u2192 valid (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 x \u27e7\u0394Var \u03c1)\n\nvalidity-var this {cons v dv R[v,dv] \u03c1} = R[v,dv]\nvalidity-var (that x) {cons v dv R[v,dv] \u03c1} = validity-var x\n\nvalidity {nats} {\u0393} {nat n} = refl\nvalidity {bags} {\u0393} {bag b} = tt\nvalidity {\u03c4} {\u0393} {var x} = validity-var x\nvalidity {nats} {\u0393} {add s t} = cong\u2082 _+_ R[s,ds] R[t,dt]\n where R[s,ds] = validity {nats} {\u0393} {s}\n R[t,dt] = validity {nats} {\u0393} {t}\nvalidity {bags} {\u0393} {map f b} = tt\n\nvalidity {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u0393} {abs t} {\u03c1} = \u03bb v dv R[v,dv] \u2192\n let\n v\u2032 = v \u2295 dv\n dv\u2032 = v\u2032 \u229d v\u2032\n \u03c1\u2081 = cons v dv R[v,dv] \u03c1\n \u03c1\u2082 = cons v\u2032 dv\u2032 R[v,u\u229dv] \u03c1\n in\n validity {_} {_} {t} {\u03c1\u2081}\n ,\n (begin\n \u27e6 t \u27e7 (ignore \u03c1\u2082) \u2295 \u27e6 derive t \u27e7 \u03c1\u2082\n \u2261\u27e8 corollary {_} {_} {t} {\u03c1\u2082} \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2082)\n \u2261\u27e8 {!!} \u27e9\n \u27e6 t \u27e7 (ignore \u03c1\u2081) \u2295 \u27e6 derive t \u27e7 \u03c1\u2081\n \u220e) where open \u2261-Reasoning\n\n\nvalidity {\u03c4} {\u0393} {app s t} = {!!}\n\ncorrectness = {!!}\n","old_contents":"{-\nThe goal of this file is to make the domain of \u0394 types smaller\nso as to be nearer to full abstraction and hopefully close\nenough for the purpose of having explicit nil changes.\n-}\n\nmodule TaggedDeltaTypes where\n\nopen import Data.NatBag renaming\n (map to mapBag ; empty to emptyBag ; update to updateBag)\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Nat\nopen import Data.Unit using (\u22a4 ; tt)\nimport Data.Integer as \u2124\nimport Data.Product as Product\nopen import Data.Product using (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\nimport Level\n\n-- Postulates: Extensionality and bag properties (#55)\npostulate extensionality : Extensionality Level.zero Level.zero\n--\n-- open import Data.NatBag.Properties\npostulate b\\\\b=\u2205 : \u2200 {{b : Bag}} \u2192 b \\\\ b \u2261 emptyBag\npostulate b++\u2205=b : \u2200 {{b : Bag}} \u2192 b ++ emptyBag \u2261 b\npostulate \u2205++b=b : \u2200 {{b : Bag}} \u2192 emptyBag ++ b \u2261 b\npostulate b++[d\\\\b]=d : \u2200 {b d} \u2192 b ++ (d \\\\ b) \u2261 d\npostulate [b++d]\\\\b=d : \u2200 {b d} \u2192 (b ++ d) \\\\ b \u2261 d\npostulate\n [a++b]\\\\[c++d]=[a\\\\c]++[b\\\\d] : \u2200 {a b c d} \u2192\n (a ++ b) \\\\ (c ++ d) \u2261 (a \\\\ c) ++ (b \\\\ d)\npostulate\n [a\\\\b]\\\\[c\\\\d]=[a\\\\c]\\\\[b\\\\d] : \u2200 {a b c d} \u2192\n (a \\\\ b) \\\\ (c \\\\ d) \u2261 (a \\\\ c) \\\\ (b \\\\ d)\n\n------------------------\n-- Syntax of programs --\n------------------------\n\ndata Type : Set where\n nats : Type\n bags : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u03c4 \u0393} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u03c4 \u03c4\u2032 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n bag : \u2200 {\u0393} \u2192 (b : Bag) \u2192 Term \u0393 bags\n\n var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (s : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n add : \u2200 {\u0393} \u2192 (s : Term \u0393 nats) \u2192 (t : Term \u0393 nats) \u2192 Term \u0393 nats\n map : \u2200 {\u0393} \u2192 (f : Term \u0393 (nats \u21d2 nats)) \u2192 (b : Term \u0393 bags) \u2192 Term \u0393 bags\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u0393\u227c\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0393\n\u0393\u227c\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0393 {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u0393\u227c\u0393 {\u0393}\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\nweaken subctx (bag b) = bag b\nweaken subctx (add t\u2081 t\u2082) = add (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (map f b) = map (weaken subctx f) (weaken subctx b)\n\n---------------------------\n-- Semantics of programs --\n---------------------------\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 nats \u27e7Type = \u2115\n\u27e6 bags \u27e7Type = Bag\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nidentity-weakening : \u2200 {\u0393} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192 \u27e6 \u0393\u227c\u0393 {\u0393} \u27e7 \u03c1 \u2261 \u03c1\nidentity-weakening {\u2205} {\u2205} = refl\nidentity-weakening {\u03c4 \u2022 \u0393} {v \u2022 \u03c1} =\n cong\u2082 _\u2022_ {x = v} refl (identity-weakening {\u0393} {\u03c1})\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\u27e6 bag b \u27e7Term \u03c1 = b\n\u27e6 add m n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 map f b \u27e7Term \u03c1 = mapBag (\u27e6 f \u27e7Term \u03c1) (\u27e6 b \u27e7Term \u03c1)\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n_\u27e8$\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\n\n-- infix 0 $ in Haskell\ninfixl 0 _\u27e8$\u27e9_\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = weaken-sound t\u2081 \u03c1 \u27e8$\u27e9 weaken-sound t\u2082 \u03c1\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (bag b) \u03c1 = refl\nweaken-sound (add m n) \u03c1 = cong\u2082 _+_ (weaken-sound m \u03c1) (weaken-sound n \u03c1)\nweaken-sound (map f b) \u03c1 =\n cong\u2082 mapBag (weaken-sound f \u03c1) (weaken-sound b \u03c1)\n\n----------------------------------------------------------\n-- Syntax and semantics of changes (they are entangled) --\n----------------------------------------------------------\n\ndata \u0394-Type : Set where\n \u0394 : (\u03c4 : Type) \u2192 \u0394-Type\n\ndata \u0394-Context : Set where\n \u0394 : (\u0393 : Context) \u2192 \u0394-Context\n\nEnv : Context \u2192 Set\nEnv \u0393 = \u27e6 \u0393 \u27e7Context\n\nprivate data \u0394-Term : \u0394-Context \u2192 \u0394-Type \u2192 Set\n\n-- Syntax of \u0394-Types\n-- ... is mutually recursive with semantics of \u0394-Terms,\n-- (because it embeds validity proofs)\n-- which is mutually recursive with validity and \u0394-environments,\n-- which is mutually recursive with \u2295,\n-- which is mutually recursive with \u229d and R[v,v\u229dv],\n-- which is mutually recursive with v\u2295[u\u229dv]=v, et cetera.\n\u27e6_\u27e7\u0394\u03c4 : \u0394-Type \u2192 Set\n\u0394-Env : Context \u2192 Set\nprivate\n \u27e6_\u27e7\u0394 : \u2200 {\u03c4 : Type} {\u0393 : Context}\n \u2192 \u0394-Term (\u0394 \u0393) (\u0394 \u03c4) \u2192 \u0394-Env \u0393 \u2192 \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4\n_\u2295_ : \u2200 {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4 \u2192 \u27e6 \u03c4 \u27e7\n_\u229d_ : \u2200 {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4\nvalid : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4 \u2192 Set\nR[v,u\u229dv] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229d v)\nv\u2295[u\u229dv]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 v \u2295 (u \u229d v) \u2261 u\nignore : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394-Env \u0393) \u2192 \u27e6 \u0393 \u27e7\nupdate : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394-Env \u0393) \u2192 \u27e6 \u0393 \u27e7\n\ninfixl 6 _\u2295_ _\u229d_ -- as with + - in GHC.Num\n\nabstract\n data \u0394-Term where\n -- changes to numbers are replacement pairs\n \u0394nat : \u2200 {\u0393} \u2192 (old : \u2115) \u2192 (new : \u2115) \u2192 \u0394-Term (\u0394 \u0393) (\u0394 nats)\n -- changes to bags are bags\n \u0394bag : \u2200 {\u0393} \u2192 (db : Bag) \u2192 \u0394-Term (\u0394 \u0393) (\u0394 bags)\n -- changes to variables are variables\n \u0394var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 \u0394-Term (\u0394 \u0393) (\u0394 \u03c4)\n -- changes to abstractions are binders of x and dx\n \u0394abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : \u0394-Term (\u0394 (\u03c4\u2081 \u2022 \u0393)) (\u0394 \u03c4\u2082)) \u2192\n \u0394-Term (\u0394 \u0393) (\u0394 (\u03c4\u2081 \u21d2 \u03c4\u2082))\n -- changes to applications are applications of a value and a change\n \u0394app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192(ds : \u0394-Term (\u0394 \u0393) (\u0394 (\u03c4\u2081 \u21d2 \u03c4\u2082)))\n \u2192 ( t : Term \u0393 \u03c4\u2081)\n \u2192 (dt : \u0394-Term (\u0394 \u0393) (\u0394 \u03c4\u2081))\n \u2192 (R[t,dt] : {\u03c1 : \u0394-Env \u0393} \u2192 -- 'Tis but a proof.\n valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1)) \u2192\n \u0394-Term (\u0394 \u0393) (\u0394 \u03c4\u2082)\n -- changes to addition are changes to their components\n \u0394add : \u2200 {\u0393} \u2192 (ds : \u0394-Term (\u0394 \u0393) (\u0394 nats))\n \u2192 (dt : \u0394-Term (\u0394 \u0393) (\u0394 nats)) \u2192\n \u0394-Term (\u0394 \u0393) (\u0394 nats)\n -- There are two kinds of changes to maps:\n -- 0. recomputation,\n -- 1. mapping over changes,\n -- the latter used only with some form of isNil available.\n \u0394map\u2080 : \u2200 {\u0393} \u2192 ( f : Term \u0393 (nats \u21d2 nats))\n \u2192 (df : \u0394-Term (\u0394 \u0393) (\u0394 (nats \u21d2 nats)))\n \u2192 ( b : Term \u0393 bags)\n \u2192 (db : \u0394-Term (\u0394 \u0393) (\u0394 bags))\n \u2192 \u0394-Term (\u0394 \u0393) (\u0394 bags)\n \u0394map\u2081 : \u2200 {\u0393} \u2192 ( f : Term \u0393 (nats \u21d2 nats))\n \u2192 (db : \u0394-Term (\u0394 \u0393) (\u0394 bags))\n \u2192 \u0394-Term (\u0394 \u0393) (\u0394 bags)\n\n-- \u27e6_\u27e7\u0394\u03c4 : \u0394-Type \u2192 Set\n\u27e6 \u0394 nats \u27e7\u0394\u03c4 = \u2115 \u00d7 \u2115\n\u27e6 \u0394 bags \u27e7\u0394\u03c4 = Bag\n\u27e6 \u0394 (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\u0394\u03c4 =\n (v : \u27e6 \u03c4\u2081 \u27e7) \u2192 (dv : \u27e6 \u0394 \u03c4\u2081 \u27e7\u0394\u03c4) \u2192 valid v dv \u2192 \u27e6 \u0394 \u03c4\u2082 \u27e7\u0394\u03c4\n\nmeaning-\u0394\u03c4 : Meaning \u0394-Type\nmeaning-\u0394\u03c4 = meaning \u27e6_\u27e7\u0394\u03c4\n\n_\u2295_ {nats} n dn = proj\u2082 dn\n_\u2295_ {bags} b db = b ++ db\n_\u2295_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u2295 df v (v \u229d v) R[v,u\u229dv]\n\n_\u229d_ {nats} m n = (n , m)\n_\u229d_ {bags} b d = b \\\\ d\n_\u229d_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb v dv R[v,dv] \u2192 f (v \u2295 dv) \u229d g v\n\n-- valid : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid {nats} n dn = n \u2261 proj\u2081 dn\nvalid {bags} b db = \u22a4\nvalid {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (v : \u27e6 \u03c4\u2081 \u27e7) (dv : \u27e6 \u0394 \u03c4\u2081 \u27e7\u0394\u03c4) (R[v,dv] : valid v dv)\n \u2192 valid (f v) (df v dv R[v,dv])\n \u00d7 (f \u2295 df) (v \u2295 dv) \u2261 f v \u2295 df v dv R[v,dv]\n\neq1 : \u2200 {\u03c4\u2081 \u03c4\u2082 : Type} {f : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {df} {x : \u27e6 \u03c4\u2081 \u27e7} {dx} \u2192\n valid f df \u2192 (R[x,dx] : valid x dx) \u2192\n (f \u2295 df) (x \u2295 dx) \u2261 f x \u2295 df x dx R[x,dx]\n\neq1 R[f,df] R[x,dx] = proj\u2082 (R[f,df] _ _ R[x,dx])\n\nv\u2295[u\u229dv]=u {nats} {u} {v} = refl\nv\u2295[u\u229dv]=u {bags} {u} {v} = b++[d\\\\b]=d {v} {u}\nv\u2295[u\u229dv]=u {\u03c4\u2081 \u21d2 \u03c4\u2082} {u} {v} = extensionality (\u03bb w \u2192\n begin\n (v \u2295 (u \u229d v)) w\n \u2261\u27e8 refl \u27e9 -- for clarity\n v w \u2295 (u (w \u2295 (w \u229d w)) \u229d v w)\n \u2261\u27e8 cong (\u03bb hole \u2192 v w \u2295 (u hole \u229d v w)) v\u2295[u\u229dv]=u \u27e9\n v w \u2295 (u w \u229d v w)\n \u2261\u27e8 v\u2295[u\u229dv]=u \u27e9\n u w\n \u220e) where open \u2261-Reasoning\n\nR[v,u\u229dv] {nats} {u} {v} = refl\nR[v,u\u229dv] {bags} {u} {v} = tt\nR[v,u\u229dv] {\u03c4\u2081 \u21d2 \u03c4\u2082} {u} {v} = \u03bb w dw R[w,dw] \u2192\n let\n w\u2032 = w \u2295 dw\n in\n R[v,u\u229dv] -- NOT a self recursion: implicit arguments are different.\n ,\n (begin\n (v \u2295 (u \u229d v)) w\u2032\n \u2261\u27e8 cong (\u03bb hole \u2192 hole w\u2032) (v\u2295[u\u229dv]=u {_} {u} {v}) \u27e9\n u w\u2032\n \u2261\u27e8 sym (v\u2295[u\u229dv]=u {_} {u w\u2032} {v w}) \u27e9\n v w \u2295 (u \u229d v) w dw R[w,dw]\n \u220e) where open \u2261-Reasoning\n\nrecord Quadruple\n (A : Set) (B : A \u2192 Set) (C : (a : A) \u2192 B a \u2192 Set)\n (D : (a : A) \u2192 (b : B a) \u2192 (c : C a b) \u2192 Set): Set where\n constructor cons\n field\n car : A\n cadr : B car\n caddr : C car cadr\n cadddr : D car cadr caddr\n\nopen Quadruple public\n\n-- The type of environments ensures their consistency.\n-- \u0394-Env : Context \u2192 Set\n\u0394-Env \u2205 = EmptySet\n\u0394-Env (\u03c4 \u2022 \u0393) = Quadruple\n \u27e6 \u03c4 \u27e7\n (\u03bb v \u2192 \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4)\n (\u03bb v dv \u2192 valid v dv)\n (\u03bb v dv R[v,dv] \u2192 \u0394-Env \u0393)\n\n-- ignore : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394-Env \u0393) \u2192 Env \u0393\nignore {\u2205} \u03c1 = \u2205\nignore {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) = v \u2022 ignore \u03c1\n\n-- update : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394-Env \u0393) \u2192 Env \u0393\nupdate {\u2205} \u03c1 = \u2205\nupdate {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) = (v \u2295 dv) \u2022 update \u03c1\n\n\u27e6_\u27e7\u0394Var : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 \u0394-Env \u0393 \u2192 \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4\n\u27e6 this \u27e7\u0394Var (cons v dv R[v,dv] \u03c1) = dv\n\u27e6 that x \u27e7\u0394Var (cons v dv R[v,dv] \u03c1) = \u27e6 x \u27e7\u0394Var \u03c1\n\n-- \u27e6_\u27e7\u0394Var is not put into the Meaning type class\n-- because its argument is identical to that of \u27e6_\u27e7Var.\n\n-- \u27e6_\u27e7\u0394 : \u2200 {\u03c4 \u0393} \u2192 \u0394-Term (\u0394 \u0393) (\u0394 \u03c4) \u2192 \u0394-Env \u0393 \u2192 \u27e6 \u0394 \u03c4 \u27e7\u0394\u03c4\nabstract\n \u27e6 \u0394nat old new \u27e7\u0394 \u03c1 = (old , new)\n \u27e6 \u0394bag db \u27e7\u0394 \u03c1 = db\n \u27e6 \u0394var x \u27e7\u0394 \u03c1 = \u27e6 x \u27e7\u0394Var \u03c1\n \u27e6 \u0394abs t \u27e7\u0394 \u03c1 = \u03bb v dv R[v,dv] \u2192 \u27e6 t \u27e7\u0394 (cons v dv R[v,dv] \u03c1)\n \u27e6 \u0394app ds t dt R[dt,t] \u27e7\u0394 \u03c1 =\n \u27e6 ds \u27e7\u0394 \u03c1 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1) R[dt,t]\n \u27e6 \u0394add ds dt \u27e7\u0394 \u03c1 =\n let\n (old-s , new-s) = \u27e6 ds \u27e7\u0394 \u03c1\n (old-t , new-t) = \u27e6 dt \u27e7\u0394 \u03c1\n in\n (old-s + old-t , new-s + new-t)\n \u27e6 \u0394map\u2080 f df b db \u27e7\u0394 \u03c1 =\n let\n v = \u27e6 b \u27e7 (ignore \u03c1)\n h = \u27e6 f \u27e7 (ignore \u03c1)\n dv = \u27e6 db \u27e7\u0394 \u03c1\n dh = \u27e6 df \u27e7\u0394 \u03c1\n in\n mapBag (h \u2295 dh) (v \u2295 dv) \\\\ mapBag h v\n \u27e6 \u0394map\u2081 f db \u27e7\u0394 \u03c1 = mapBag (\u27e6 f \u27e7 (ignore \u03c1)) (\u27e6 db \u27e7\u0394 \u03c1)\n\nmeaning-\u0394Term : \u2200 {\u03c4 \u0393} \u2192 Meaning (\u0394-Term (\u0394 \u0393) (\u0394 \u03c4))\nmeaning-\u0394Term = meaning \u27e6_\u27e7\u0394\n\n--------------------------------------------------------\n-- Program transformation and correctness (entangled) --\n--------------------------------------------------------\n\nderive : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394-Term (\u0394 \u0393) (\u0394 \u03c4)\n\nvalidity : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394-Env \u0393} \u2192\n valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\n-- derive : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394-Term (\u0394 \u0393) (\u0394 \u03c4)\nderive (nat n) = \u0394nat n n\nderive (bag b) = \u0394bag b\nderive (var x) = \u0394var x\nderive (abs t) = \u0394abs (derive t)\nderive (app s t) = \u0394app (derive s) t (derive t) validity\nderive (add s t) = \u0394add (derive s) (derive t)\nderive (map f b) = \u0394map\u2080 f (derive f) b (derive b)\n\nvalidity-var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192\n \u2200 {\u03c1 : \u0394-Env \u0393} \u2192 valid (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 x \u27e7\u0394Var \u03c1)\n\nvalidity-var this {cons v dv R[v,dv] \u03c1} = R[v,dv]\nvalidity-var (that x) {cons v dv R[v,dv] \u03c1} = validity-var x\n\nvalidity {nats} {\u0393} {nat n} {\u03c1} = {!!}\n\nvalidity {bags} {\u0393} {bag b} = {!!}\nvalidity {\u03c4} {\u0393} {var x} = {!!}\nvalidity {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u0393} {abs t} = {!!}\nvalidity {\u03c4} {\u0393} {app t t\u2081} = {!!}\nvalidity {nats} {\u0393} {add t t\u2081} = {!!}\nvalidity {bags} {\u0393} {map t t\u2081} = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"8a0a4851fc2641c8c2555a37e8c04c5ba5d85659","subject":"Regenerate README.agda","message":"Regenerate README.agda\n\nOld-commit-hash: fcba64e4846b84014cac184f5b0c1db45cd742bb\n","repos":"inc-lc\/ilc-agda","old_file":"README.agda","new_file":"README.agda","new_contents":"module README where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * multiple calculi\n\nimport Denotation.Change.Popl14\n{- Correctness theorem for canonical derivation of Calc. Popl14 -}\nimport Denotation.Derive.Canon-Popl14\n{- Correctness theorem for optimized derivation of Calc. Popl14 -}\nimport Denotation.Derive.Optimized-Popl14\nimport Denotation.Environment.Popl14\nimport Denotation.Environment\nimport Denotation.Evaluation.Popl14\nimport Denotation.FreeVars.Popl14\n{- The idea of implementing a denotational specification for Calc. Popl14 -}\nimport Denotation.Implementation.Popl14\nimport Denotation.Notation\n{- Denotation-as-specification for canonical derivation of Calc. Popl14 -}\nimport Denotation.Specification.Canon-Popl14\nimport Denotation.Specification.Optimized-Popl14\nimport Denotation.Value.Popl14\nimport experimental.DecidableEq\nimport experimental.FoldableBag\nimport experimental.FoldableBagParametric\nimport experimental.NormalizationByEvaluation\nimport experimental.OrdBag\nimport experimental.Sorting\nimport Postulate.Bag-Popl14\nimport Postulate.Extensionality\nimport Property.Uniqueness\nimport Structure.Bag.Popl14\nimport Structure.Tuples\nimport Syntax.Constant.Popl14\nimport Syntax.Context.Plotkin\nimport Syntax.Context.Popl14\nimport Syntax.Context\nimport Syntax.DeltaContext\nimport Syntax.Derive.Canon-Popl14\nimport Syntax.Derive.Optimized-Popl14\nimport Syntax.FreeVars.Popl14\n{- Language definition of Calc. Atlas -}\nimport Syntax.Language.Atlas\nimport Syntax.Language.Calculus\nimport Syntax.Language.Popl14\n{- Terms of a calculus described in Plotkin style\n - types are parametric in base types\n - terms are parametric in constants\n This style of language description is employed in:\n G. D. Plotkin. \"LCF considered as a programming language.\"\n Theoretical Computer Science 5(3) pp. 223--255, 1997.\n http:\/\/dx.doi.org\/10.1016\/0304-3975(77)90044-5 -}\nimport Syntax.Term.Plotkin\nimport Syntax.Term.Popl14\nimport Syntax.Type.Atlas\nimport Syntax.Type.Plotkin\nimport Syntax.Type.Popl14\nimport Syntax.Vars\nimport Theorem.CongApp\nimport Theorem.EqualityUnique\nimport Theorem.Groups-Popl14\nimport Theorem.IrrelevanceUnique-Popl14\nimport Theorem.ProductUnique\nimport Theorem.ValidityUnique-Popl14\nimport UNDEFINED\n","old_contents":"module README where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * multiple calculi\n\nimport Denotation.Change.Popl14\n{- Correctness theorem for canonical derivation of Calc. Popl14 -}\nimport Denotation.Derive.Canon-Popl14\n{- Correctness theorem for optimized derivation of Calc. Popl14 -}\nimport Denotation.Derive.Optimized-Popl14\nimport Denotation.Environment.Popl14\nimport Denotation.Environment\nimport Denotation.Evaluation.Popl14\nimport Denotation.FreeVars.Popl14\n{- The idea of implementing a denotational specification for Calc. Popl14 -}\nimport Denotation.Implementation.Popl14\nimport Denotation.Notation\n{- Denotation-as-specification for canonical derivation of Calc. Popl14 -}\nimport Denotation.Specification.Canon-Popl14\nimport Denotation.Specification.Optimized-Popl14\nimport Denotation.Value.Popl14\nimport Postulate.Bag-Popl14\nimport Postulate.Extensionality\nimport Property.Uniqueness\nimport Structure.Bag.Popl14\nimport Structure.Tuples\nimport Syntax.Context.Popl14\nimport Syntax.Context\nimport Syntax.Derive.Canon-Popl14\nimport Syntax.Derive.Optimized-Popl14\nimport Syntax.FreeVars.Popl14\n{- Language definition of Calc. Atlas -}\nimport Syntax.Language.Atlas\nimport Syntax.Language.Calculus\n{- Terms of a calculus described in Plotkin style\n - types are parametric in base types\n - terms are parametric in constants\n This style of language description is employed in:\n G. D. Plotkin. \"LCF considered as a programming language.\"\n Theoretical Computer Science 5(3) pp. 223--255, 1997.\n http:\/\/dx.doi.org\/10.1016\/0304-3975(77)90044-5 -}\nimport Syntax.Term.Plotkin\nimport Syntax.Term.Popl14\nimport Syntax.Type.Plotkin\nimport Syntax.Type.Popl14\nimport Syntax.Vars\nimport Theorem.CongApp\nimport Theorem.EqualityUnique\nimport Theorem.Groups-Popl14\nimport Theorem.IrrelevanceUnique-Popl14\nimport Theorem.ProductUnique\nimport Theorem.ValidityUnique-Popl14\nimport UNDEFINED\nimport experimental.DecidableEq\nimport experimental.FoldableBag\nimport experimental.FoldableBagParametric\nimport experimental.NormalizationByEvaluation\nimport experimental.OrdBag\nimport experimental.Sorting\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"6e6a81d4cd5e6747138da920db56cf5c044c974c","subject":"agda: rename parameter","message":"agda: rename parameter\n\nAs suggested by Tillmann.\n\nOld-commit-hash: 3b2f1506d9fc2595f06fb0d8e57a526bc30b03ac\n","repos":"inc-lc\/ilc-agda","old_file":"total.agda","new_file":"total.agda","new_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Note that \u0394 is *not* the same as the \u2202 operator in\n-- definition\/intro.tex. See discussion at:\n--\n-- https:\/\/github.com\/ps-mr\/ilc\/pull\/34#discussion_r4290325\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nopen import Data.Product\nopen import Data.Unit\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Changes\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\n\nopen import Changes\nopen import ChangeContexts\n\n-- DEFINITION of valid changes via a logical relation\n\n{-\nWhat I wanted to write:\n\ndata Valid\u0394 : {T : Type} \u2192 (v : \u27e6 T \u27e7) \u2192 (dv : \u27e6 \u0394-Type T \u27e7) \u2192 Set where\n base : (v : \u27e6 bool \u27e7) \u2192 (dv : \u27e6 \u0394-Type bool \u27e7) \u2192 Valid\u0394 v dv\n fun : \u2200 {S T} \u2192 (f : \u27e6 S \u21d2 T \u27e7) \u2192 (df : \u27e6 \u0394-Type (S \u21d2 T) \u27e7) \u2192\n (\u2200 (s : \u27e6 S \u27e7) ds (valid : Valid\u0394 s ds) \u2192 (Valid\u0394 (f s) (df s ds)) \u00d7 ((apply df f) (apply ds s) \u2261 apply (df s ds) (f s))) \u2192 \n Valid\u0394 f df\n-}\n-- What I had to write:\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {bool} v dv = \u22a4\nvalid-\u0394 {S \u21d2 T} f df =\n \u2200 (s : \u27e6 S \u27e7) ds (valid-w : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7\n (apply df f) (apply ds s) \u2261 apply (df s ds) (f s)\n\n-- LIFTING terms into \u0394-Contexts\n\nlift-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nlift-term {\u0393\u2081} {\u0393\u2082} {{\u0393\u2032}} = weaken (\u227c-trans \u227c-\u0394-Context \u0393\u2032)\n\n-- PROPERTIES of lift-term\n\nlift-term-ignore : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} {\u03c1 : \u27e6 \u0393\u2082 \u27e7} (t : Term \u0393\u2081 \u03c4) \u2192\n \u27e6 lift-term {{\u0393\u2032}} t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1))\nlift-term-ignore {{\u0393\u2032}} {\u03c1} t = let \u0393\u2033 = \u227c-trans \u227c-\u0394-Context \u0393\u2032 in\n begin\n \u27e6 lift-term {{\u0393\u2032}} t \u27e7 \u03c1\n \u2261\u27e8\u27e9\n \u27e6 weaken \u0393\u2033 t \u27e7 \u03c1\n \u2261\u27e8 weaken-sound t \u03c1 \u27e9\n \u27e6 t \u27e7 (\u27e6 \u227c-trans \u227c-\u0394-Context \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 cong (\u03bb x \u2192 \u27e6 t \u27e7 x) (\u27e6\u27e7-\u227c-trans \u227c-\u0394-Context \u0393\u2032 \u03c1) \u27e9\n \u27e6 t \u27e7Term (\u27e6 \u227c-\u0394-Context \u27e7\u227c (\u27e6 \u0393\u2032 \u27e7\u227c \u03c1))\n \u2261\u27e8\u27e9\n \u27e6 t \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1))\n \u220e where open \u2261-Reasoning\n\n-- PROPERTIES of \u0394\n\n\u0394-abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} (t : Term (\u03c4\u2081 \u2022 \u0393\u2081) \u03c4\u2082) \u2192\n let \u0393\u2033 = keep \u0394-Type \u03c4\u2081 \u2022 keep \u03c4\u2081 \u2022 \u0393\u2032 in\n \u0394 {{\u0393\u2032}} (abs t) \u2248 abs (abs (\u0394 {\u03c4\u2081 \u2022 \u0393\u2081} t))\n\u0394-abs t = ext-t (\u03bb \u03c1 \u2192 refl)\n\n\u0394-app : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4\u2081 \u03c4\u2082} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} (t\u2081 : Term \u0393\u2081 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393\u2081 \u03c4\u2081) \u2192\n \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082) \u2248 app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n\u0394-app {{\u0393\u2032}} t\u2081 t\u2082 = \u2248-sym (ext-t (\u03bb \u03c1\u2032 \u2192 let \u03c1 = \u27e6 \u0393\u2032 \u27e7 \u03c1\u2032 in\n begin\n \u27e6 app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082) \u27e7 \u03c1\u2032\n \u2261\u27e8\u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 lift-term {{\u0393\u2032}} t\u2082 \u27e7 \u03c1\u2032)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 lift-term {{\u0393\u2032}} t\u2082 \u27e7 \u03c1\u2032))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))) x))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) x))\n (lift-term-ignore {{\u0393\u2032}} t\u2082) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff (\u27e6 t\u2081 \u27e7 (update \u03c1) x) (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (apply-diff (\u27e6 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 t\u2082 \u27e7 (update \u03c1))) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8\u27e9\n \u27e6 \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082) \u27e7 \u03c1\u2032\n \u220e)) where open \u2261-Reasoning\n\n-- SYMBOLIC DERIVATION\n\nderive-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-var {\u03c4 \u2022 \u0393} this = this\nderive-var {\u03c4 \u2022 \u0393} (that x) = that (that (derive-var x))\n\nderive-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 (\u0394-Type \u03c4)\nderive-term {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) = abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t))\n where \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term {{\u0393\u2032}} (app t\u2081 t\u2082) = app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\nderive-term {{\u0393\u2032}} (var x) = var (lift \u0393\u2032 (derive-var x))\nderive-term {{\u0393\u2032}} true = false\nderive-term {{\u0393\u2032}} false = false\nderive-term {{\u0393\u2032}} (if c t e) =\n if ((derive-term {{\u0393\u2032}} c) and (lift-term {{\u0393\u2032}} c))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} e) (lift-term {{\u0393\u2032}} e)) (lift-term {{\u0393\u2032}} t))\n (if ((derive-term {{\u0393\u2032}} c) and (lift-term {{\u0393\u2032}} (! c)))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} t) (lift-term {{\u0393\u2032}} t)) (lift-term {{\u0393\u2032}} e))\n (if (lift-term {{\u0393\u2032}} c)\n (derive-term {{\u0393\u2032}} t)\n (derive-term {{\u0393\u2032}} e)))\nderive-term {{\u0393\u2032}} (\u0394 {{\u0393\u2033}} t) = \u0394 {{\u0393\u2032}} (derive-term {{\u0393\u2033}} t)\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 (t : Term \u0393\u2081 \u03c4) \u2192\n \u0394 {{\u0393\u2032}} t \u2248 derive-term {{\u0393\u2032}} t\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 {\u03c4 \u2022 \u0393\u2081} t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct {\u03c4 \u2022 \u0393\u2081} t)) \u27e9\n abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} t))\n \u2261\u27e8\u27e9\n derive-term (abs t)\n \u220e where\n open \u2248-Reasoning\n \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct {{\u0393\u2032}} t\u2081) \u2248-refl) (derive-term-correct {{\u0393\u2032}} t\u2082) \u27e9\n app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (var x) = ext-t (\u03bb \u03c1 \u2192\n begin\n \u27e6 \u0394 {{\u0393\u2032}} (var x) \u27e7 \u03c1\n \u2261\u27e8\u27e9\n diff\n (\u27e6 x \u27e7 (update (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n (\u27e6 x \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n \u2261\u27e8 derive-var-correct {\u0393\u2081} (\u27e6 \u0393\u2032 \u27e7 \u03c1) x \u27e9\n \u27e6 derive-var x \u27e7Var (\u27e6 \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 sym (lift-sound \u0393\u2032 (derive-var x) \u03c1) \u27e9\n \u27e6 lift \u0393\u2032 (derive-var x) \u27e7Var \u03c1\n \u220e) where open \u2261-Reasoning\nderive-term-correct true = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\n","old_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Note that \u0394 is *not* the same as the \u2202 operator in\n-- definition\/intro.tex. See discussion at:\n--\n-- https:\/\/github.com\/ps-mr\/ilc\/pull\/34#discussion_r4290325\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nopen import Data.Product\nopen import Data.Unit\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Changes\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\n\nopen import Changes\nopen import ChangeContexts\n\n-- DEFINITION of valid changes via a logical relation\n\n{-\nWhat I wanted to write:\n\ndata Valid\u0394 : {T : Type} \u2192 (v : \u27e6 T \u27e7) \u2192 (dv : \u27e6 \u0394-Type T \u27e7) \u2192 Set where\n base : (v : \u27e6 bool \u27e7) \u2192 (dv : \u27e6 \u0394-Type bool \u27e7) \u2192 Valid\u0394 v dv\n fun : \u2200 {S T} \u2192 (f : \u27e6 S \u21d2 T \u27e7) \u2192 (df : \u27e6 \u0394-Type (S \u21d2 T) \u27e7) \u2192\n (\u2200 (s : \u27e6 S \u27e7) ds (valid : Valid\u0394 s ds) \u2192 (Valid\u0394 (f s) (df s ds)) \u00d7 ((apply df f) (apply ds s) \u2261 apply (df s ds) (f s))) \u2192 \n Valid\u0394 f df\n-}\n-- What I had to write:\nvalid-\u0394 : {T : Type} \u2192 \u27e6 T \u27e7 \u2192 \u27e6 \u0394-Type T \u27e7 \u2192 Set\nvalid-\u0394 {bool} v dv = \u22a4\nvalid-\u0394 {S \u21d2 T} f df =\n \u2200 (s : \u27e6 S \u27e7) ds (valid-w : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7\n (apply df f) (apply ds s) \u2261 apply (df s ds) (f s)\n\n-- LIFTING terms into \u0394-Contexts\n\nlift-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nlift-term {\u0393\u2081} {\u0393\u2082} {{\u0393\u2032}} = weaken (\u227c-trans \u227c-\u0394-Context \u0393\u2032)\n\n-- PROPERTIES of lift-term\n\nlift-term-ignore : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} {\u03c1 : \u27e6 \u0393\u2082 \u27e7} (t : Term \u0393\u2081 \u03c4) \u2192\n \u27e6 lift-term {{\u0393\u2032}} t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1))\nlift-term-ignore {{\u0393\u2032}} {\u03c1} t = let \u0393\u2033 = \u227c-trans \u227c-\u0394-Context \u0393\u2032 in\n begin\n \u27e6 lift-term {{\u0393\u2032}} t \u27e7 \u03c1\n \u2261\u27e8\u27e9\n \u27e6 weaken \u0393\u2033 t \u27e7 \u03c1\n \u2261\u27e8 weaken-sound t \u03c1 \u27e9\n \u27e6 t \u27e7 (\u27e6 \u227c-trans \u227c-\u0394-Context \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 cong (\u03bb x \u2192 \u27e6 t \u27e7 x) (\u27e6\u27e7-\u227c-trans \u227c-\u0394-Context \u0393\u2032 \u03c1) \u27e9\n \u27e6 t \u27e7Term (\u27e6 \u227c-\u0394-Context \u27e7\u227c (\u27e6 \u0393\u2032 \u27e7\u227c \u03c1))\n \u2261\u27e8\u27e9\n \u27e6 t \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1))\n \u220e where open \u2261-Reasoning\n\n-- PROPERTIES of \u0394\n\n\u0394-abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} (t : Term (\u03c4\u2081 \u2022 \u0393\u2081) \u03c4\u2082) \u2192\n let \u0393\u2033 = keep \u0394-Type \u03c4\u2081 \u2022 keep \u03c4\u2081 \u2022 \u0393\u2032 in\n \u0394 {{\u0393\u2032}} (abs t) \u2248 abs (abs (\u0394 {\u03c4\u2081 \u2022 \u0393\u2081} t))\n\u0394-abs t = ext-t (\u03bb \u03c1 \u2192 refl)\n\n\u0394-app : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4\u2081 \u03c4\u2082} {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} (t\u2081 : Term \u0393\u2081 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393\u2081 \u03c4\u2081) \u2192\n \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082) \u2248 app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n\u0394-app {{\u0393\u2032}} t\u2081 t\u2082 = \u2248-sym (ext-t (\u03bb \u03c1\u2032 \u2192 let \u03c1 = \u27e6 \u0393\u2032 \u27e7 \u03c1\u2032 in\n begin\n \u27e6 app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082) \u27e7 \u03c1\u2032\n \u2261\u27e8\u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 lift-term {{\u0393\u2032}} t\u2082 \u27e7 \u03c1\u2032)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 lift-term {{\u0393\u2032}} t\u2082 \u27e7 \u03c1\u2032))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))) x))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) x))\n (lift-term-ignore {{\u0393\u2032}} t\u2082) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff (\u27e6 t\u2081 \u27e7 (update \u03c1) x) (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (apply-diff (\u27e6 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 t\u2082 \u27e7 (update \u03c1))) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8\u27e9\n \u27e6 \u0394 {{\u0393\u2032}} (app t\u2081 t\u2082) \u27e7 \u03c1\u2032\n \u220e)) where open \u2261-Reasoning\n\n-- SYMBOLIC DERIVATION\n\nderive-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-var {\u03c4 \u2022 \u0393} this = this\nderive-var {\u03c4 \u2022 \u0393} (that x) = that (that (derive-var x))\n\nderive-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 (\u0394-Type \u03c4)\nderive-term {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) = abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} {{\u0393\u2033}} t))\n where \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term {{\u0393\u2032}} (app t\u2081 t\u2082) = app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\nderive-term {{\u0393\u2032}} (var x) = var (lift \u0393\u2032 (derive-var x))\nderive-term {{\u0393\u2032}} true = false\nderive-term {{\u0393\u2032}} false = false\nderive-term {{\u0393\u2032}} (if c t e) =\n if ((derive-term {{\u0393\u2032}} c) and (lift-term {{\u0393\u2032}} c))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} e) (lift-term {{\u0393\u2032}} e)) (lift-term {{\u0393\u2032}} t))\n (if ((derive-term {{\u0393\u2032}} c) and (lift-term {{\u0393\u2032}} (! c)))\n (diff-term (apply-term (derive-term {{\u0393\u2032}} t) (lift-term {{\u0393\u2032}} t)) (lift-term {{\u0393\u2032}} e))\n (if (lift-term {{\u0393\u2032}} c)\n (derive-term {{\u0393\u2032}} t)\n (derive-term {{\u0393\u2032}} e)))\nderive-term {{\u0393\u2032}} (\u0394 {{\u0393\u2033}} t) = \u0394 {{\u0393\u2032}} (derive-term {{\u0393\u2033}} t)\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 {{\u0393\u2032 : \u0394-Context \u0393\u2081 \u227c \u0393\u2082}} \u2192 (t : Term \u0393\u2081 \u03c4) \u2192\n \u0394 {{\u0393\u2032}} t \u2248 derive-term {{\u0393\u2032}} t\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (abs {\u03c4} t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 {\u03c4 \u2022 \u0393\u2081} t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct {\u03c4 \u2022 \u0393\u2081} t)) \u27e9\n abs (abs (derive-term {\u03c4 \u2022 \u0393\u2081} t))\n \u2261\u27e8\u27e9\n derive-term (abs t)\n \u220e where\n open \u2248-Reasoning\n \u0393\u2033 = keep \u0394-Type \u03c4 \u2022 keep \u03c4 \u2022 \u0393\u2032\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (\u0394 {{\u0393\u2032}} t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct {{\u0393\u2032}} t\u2081) \u2248-refl) (derive-term-correct {{\u0393\u2032}} t\u2082) \u27e9\n app (app (derive-term {{\u0393\u2032}} t\u2081) (lift-term {{\u0393\u2032}} t\u2082)) (derive-term {{\u0393\u2032}} t\u2082)\n \u2261\u27e8\u27e9\n derive-term {{\u0393\u2032}} (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct {\u0393\u2081} {{\u0393\u2032}} (var x) = ext-t (\u03bb \u03c1 \u2192\n begin\n \u27e6 \u0394 {{\u0393\u2032}} (var x) \u27e7 \u03c1\n \u2261\u27e8\u27e9\n diff\n (\u27e6 x \u27e7 (update (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n (\u27e6 x \u27e7 (ignore (\u27e6 \u0393\u2032 \u27e7 \u03c1)))\n \u2261\u27e8 derive-var-correct {\u0393\u2081} (\u27e6 \u0393\u2032 \u27e7 \u03c1) x \u27e9\n \u27e6 derive-var x \u27e7Var (\u27e6 \u0393\u2032 \u27e7 \u03c1)\n \u2261\u27e8 sym (lift-sound \u0393\u2032 (derive-var x) \u03c1) \u27e9\n \u27e6 lift \u0393\u2032 (derive-var x) \u27e7Var \u03c1\n \u220e) where open \u2261-Reasoning\nderive-term-correct true = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext-t (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7602ff1910998465653413456eb858d7f47a4949","subject":"flipbased-tree: Ongoing xor proofs...","message":"flipbased-tree: Ongoing xor proofs...\n","repos":"crypto-agda\/crypto-agda","old_file":"flipbased-tree.agda","new_file":"flipbased-tree.agda","new_contents":"module flipbased-tree where\n\nopen import Function\nopen import Data.Bits\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\nopen import bintree\nopen import Data.Product\n\nimport flipbased\n\n-- \u201c\u21ba n A\u201d reads like: \u201ctoss n coins and then return a value of type A\u201d\n\u21ba : \u2200 {a} n (A : Set a) \u2192 Set a\n\u21ba = flip Tree\n\nreturn\u21ba : \u2200 {c a} {A : Set a} \u2192 A \u2192 \u21ba c A\nreturn\u21ba = leaf\n\nrunDet : \u2200 {a} {A : Set a} \u2192 \u21ba 0 A \u2192 A\nrunDet (leaf x) = x\n\ntoss : \u21ba 1 Bit\ntoss = fork (leaf 0b) (leaf 1b)\n\nweaken\u2264 : \u2200 {m c a} {A : Set a} \u2192 m \u2264 c \u2192 \u21ba m A \u2192 \u21ba c A\nweaken\u2264 p (leaf x) = leaf x\nweaken\u2264 (s\u2264s p) (fork left right) = fork (weaken\u2264 p left) (weaken\u2264 p right)\n\nm\u2264n+m : \u2200 m n \u2192 m \u2264 n + m\nm\u2264n+m m n = \u2115\u2264.trans (m\u2264m+n m n) (\u2115\u2264.reflexive (\u2115\u00b0.+-comm m n))\n\nweaken+ : \u2200 c {m a} {A : Set a} \u2192 \u21ba m A \u2192 \u21ba (c + m) A\nweaken+ c = weaken\u2264 (m\u2264n+m _ c)\n\nmap\u21ba : \u2200 {c a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 \u21ba c A \u2192 \u21ba c B\nmap\u21ba f (leaf x) = leaf (f x)\nmap\u21ba f (fork left right) = fork (map\u21ba f left) (map\u21ba f right)\n\njoin\u21ba : \u2200 {c\u2081 c\u2082 a} {A : Set a} \u2192 \u21ba c\u2081 (\u21ba c\u2082 A) \u2192 \u21ba (c\u2081 + c\u2082) A\njoin\u21ba {c} (leaf x) = weaken+ c x\njoin\u21ba (fork left right) = fork (join\u21ba left) (join\u21ba right)\n\nopen flipbased \u21ba toss weaken\u2264 leaf map\u21ba join\u21ba public\n\nopen import Data.Bool\nopen import Data.Bits\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\n\ninfix 4 _\/2+_\/2\ninfix 6 _\/2\npostulate\n [0,1] : Set\n 0\/1 : [0,1]\n 1\/1 : [0,1]\n _\/2 : [0,1] \u2192 [0,1]\n _\/2+_\/2 : [0,1] \u2192 [0,1] \u2192 [0,1]\n _*\/_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 1-_ : [0,1] \u2192 [0,1]\n-- sym _\/2+_\/2\n-- 1 \/2+ 1 \/2 = 1\/1\n-- p \/2+ p \/2 = p\n-- p \/2+ (1- p) \/2 = 1\/2\n\n -- \u00b7\/1+_ : \u2115 \u2192 Set\n -- \/+\/ : \u2115 \u2192 [0,1] \u2192 [0,1] \u2192 [0,1]\n\n 0\/2+p\/2\u2261p\/2 : \u2200 p \u2192 (0\/1 \/2+ p \/2) \u2261 p \/2\n _\/2+_\/2-comm : \u2200 x y \u2192 x \/2+ y \/2 \u2261 y \/2+ x \/2\n 1*q\u2261q : \u2200 q \u2192 1\/1 *\/ q \u2261 q\n 0*q\u2261q : \u2200 q \u2192 0\/1 *\/ q \u2261 0\/1\n distr-*-\/2+\/2 : \u2200 x y z \u2192 (x *\/ z) \/2+ (y *\/ z) \/2 \u2261 (x \/2+ y \/2) *\/ z\n distr-\/2-* : \u2200 p q \u2192 (p \/2) *\/ (q \/2) \u2261 ((p *\/ q) \/2) \/2\n 0\/2\u22610 : 0\/1 \/2 \u2261 0\/1\n 1-_\/2+1-_\/2 : \u2200 p q \u2192 (1- p) \/2+ (1- q) \/2 \u2261 1- (p \/2+ q \/2)\n\n1\/2^_ : \u2115 \u2192 [0,1]\n1\/2^ zero = 1\/1\n1\/2^ suc n = (1\/2^ n)\/2\n\n1\/2 : [0,1]\n1\/2 = 1\/2^ 1\n1\/4 : [0,1]\n1\/4 = 1\/2^ 2\n\ndata Pr[return\u21ba_\u2261_]\u2261_ {a} {A : Set a} (x y : A) : [0,1] \u2192 Set a where\n Pr-\u2261 : x \u2261 y \u2192 Pr[return\u21ba x \u2261 y ]\u2261 1\/1\n Pr-\u2262 : x \u2262 y \u2192 Pr[return\u21ba x \u2261 y ]\u2261 0\/1\n\ninfix 2 Pr[_\u2261_]\u2261_\ndata Pr[_\u2261_]\u2261_ {a} {A : Set a} : \u2200 {c} \u2192 \u21ba c A \u2192 A \u2192 [0,1] \u2192 Set a where\n Pr-return : \u2200 {c x y pr} (pf : Pr[return\u21ba x \u2261 y ]\u2261 pr) \u2192 Pr[ return\u21ba {c = c} x \u2261 y ]\u2261 pr\n\n Pr-fork : \u2200 {c} {left right : \u21ba c A} {x p q r}\n (eq : p \/2+ q \/2 \u2261 r)\n (pf\u2080 : Pr[ left \u2261 x ]\u2261 p)\n (pf\u2081 : Pr[ right \u2261 x ]\u2261 q)\n \u2192 Pr[ fork left right \u2261 x ]\u2261 r\n\nPr-fork\u2032 : \u2200 {c a} {A : Set a} {left right : \u21ba c A} {x p q}\n \u2192 Pr[ left \u2261 x ]\u2261 p\n \u2192 Pr[ right \u2261 x ]\u2261 q\n \u2192 Pr[ fork left right \u2261 x ]\u2261 (p \/2+ q \/2)\nPr-fork\u2032 = Pr-fork refl\n\nPr-return-\u2261 : \u2200 {c a} {A : Set a} {x : A} \u2192 Pr[ return\u21ba {c = c} x \u2261 x ]\u2261 1\/1\nPr-return-\u2261 = Pr-return (Pr-\u2261 refl)\n\nPr-return-\u2262 : \u2200 {c a} {A : Set a} {x y : A} \u2192 x \u2262 y \u2192 Pr[ return\u21ba {c = c} x \u2261 y ]\u2261 0\/1\nPr-return-\u2262 = Pr-return \u2218 Pr-\u2262\n\nimport Function.Equality as F\u2261\nimport Function.Equivalence as F\u2248\n\n_\u2248\u21d2_ : \u2200 {c a} {A : Set a} (p\u2081 p\u2082 : \u21ba c A) \u2192 Set a\np\u2081 \u2248\u21d2 p\u2082 = \u2200 {x pr} \u2192 Pr[ p\u2081 \u2261 x ]\u2261 pr \u2192 Pr[ p\u2082 \u2261 x ]\u2261 pr\n\n_\u2248_ : \u2200 {c a} {A : Set a} (p\u2081 p\u2082 : \u21ba c A) \u2192 Set a\np\u2081 \u2248 p\u2082 = \u2200 {x pr} \u2192 (Pr[ p\u2081 \u2261 x ]\u2261 pr) F\u2248.\u21d4 (Pr[ p\u2082 \u2261 x ]\u2261 pr)\n\n\u2248-refl : \u2200 {c a} {A : Set a} \u2192 Reflexive {A = \u21ba c A} _\u2248_\n\u2248-refl = F\u2248.id\n\n\u2248-sym : \u2200 {c a} {A : Set a} {p\u2081 p\u2082 : \u21ba c A} \u2192 p\u2081 \u2248 p\u2082 \u2192 p\u2082 \u2248 p\u2081\n\u2248-sym \u03b7 = F\u2248.sym \u03b7\n\n\u2248-trans : \u2200 {c a} {A : Set a} \u2192 Transitive {A = \u21ba c A} _\u2248_\n\u2248-trans f g = g F\u2248.\u2218 f\n\nfork-sym\u21d2 : \u2200 {c a} {A : Set a} {p\u2081 p\u2082 : \u21ba c A} \u2192 fork p\u2081 p\u2082 \u2248\u21d2 fork p\u2082 p\u2081\nfork-sym\u21d2 (Pr-fork {p = p} {q} refl pf\u2081 pf\u2080) rewrite p \/2+ q \/2-comm = Pr-fork\u2032 pf\u2080 pf\u2081\n\nfork-sym : \u2200 {c a} {A : Set a} {p\u2081 p\u2082 : \u21ba c A} \u2192 fork p\u2081 p\u2082 \u2248 fork p\u2082 p\u2081\nfork-sym = F\u2248.equivalence fork-sym\u21d2 fork-sym\u21d2\n\nPr-fork-0 : \u2200 {c a} {A : Set a} {left right : \u21ba c A} {x : A} {p}\n \u2192 Pr[ left \u2261 x ]\u2261 0\/1\n \u2192 Pr[ right \u2261 x ]\u2261 p\n \u2192 Pr[ fork left right \u2261 x ]\u2261 p \/2\nPr-fork-0 {p = p} eq\u2081 eq\u2082 rewrite sym (0\/2+p\/2\u2261p\/2 p) = Pr-fork\u2032 eq\u2081 eq\u2082\n\nex\u2081 : \u2200 x \u2192 Pr[ toss \u2261 x ]\u2261 1\/2\nex\u2081 1b = Pr-fork-0 (Pr-return-\u2262 (\u03bb ())) Pr-return-\u2261\nex\u2081 0b = F\u2248.Equivalence.to fork-sym F\u2261.\u27e8$\u27e9 (Pr-fork-0 (Pr-return-\u2262 (\u03bb ())) Pr-return-\u2261)\n\nPr-map : \u2200 {c a b} {A : Set a} {B : Set b} {Alg : \u21ba c A} {x pr} {f : A \u2192 B} \u2192\n (f-inj : \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y) \u2192\n Pr[ Alg \u2261 x ]\u2261 pr \u2192\n Pr[ \u27ea f \u00b7 Alg \u27eb \u2261 f x ]\u2261 pr\nPr-map f-inj (Pr-return (Pr-\u2261 refl)) = Pr-return (Pr-\u2261 refl)\nPr-map f-inj (Pr-return (Pr-\u2262 x\u2262y)) = Pr-return (Pr-\u2262 (x\u2262y \u2218 f-inj))\nPr-map f-inj (Pr-fork eq pf\u2080 pf\u2081) = Pr-fork eq (Pr-map f-inj pf\u2080) (Pr-map f-inj pf\u2081)\n\nPr-same : \u2200 {c a} {A : Set a} {Alg : \u21ba c A} {x pr\u2080 pr\u2081} \u2192\n pr\u2080 \u2261 pr\u2081 \u2192\n Pr[ Alg \u2261 x ]\u2261 pr\u2080 \u2192\n Pr[ Alg \u2261 x ]\u2261 pr\u2081\nPr-same refl = id\n\nPr-weaken\u2264 : \u2200 {c\u2080 c\u2081 a} {A : Set a} {Alg : \u21ba c\u2080 A} {x pr} \u2192\n (c\u2080\u2264c\u2081 : c\u2080 \u2264 c\u2081) \u2192\n Pr[ Alg \u2261 x ]\u2261 pr \u2192\n Pr[ weaken\u2264 c\u2080\u2264c\u2081 Alg \u2261 x ]\u2261 pr\nPr-weaken\u2264 p (Pr-return pf) = Pr-return pf\nPr-weaken\u2264 (s\u2264s c\u2080\u2264c\u2081) (Pr-fork eq pf\u2080 pf\u2081)\n = Pr-fork eq (Pr-weaken\u2264 c\u2080\u2264c\u2081 pf\u2080) (Pr-weaken\u2264 c\u2080\u2264c\u2081 pf\u2081)\n\nPr-weaken+ : \u2200 {c\u2080} c\u2081 {a} {A : Set a} {Alg : \u21ba c\u2080 A} {x pr} \u2192\n Pr[ Alg \u2261 x ]\u2261 pr \u2192\n Pr[ weaken+ c\u2081 Alg \u2261 x ]\u2261 pr\nPr-weaken+ c\u2081 = Pr-weaken\u2264 (m\u2264n+m _ c\u2081)\n\nPr-map-0 : \u2200 {c a b} {A : Set a} {B : Set b} {Alg : \u21ba c A} {f : A \u2192 B} {x} \u2192 (\u2200 y \u2192 f y \u2262 x)\n \u2192 Pr[ map\u21ba f Alg \u2261 x ]\u2261 0\/1\nPr-map-0 {Alg = leaf x} f-prop = Pr-return (Pr-\u2262 (f-prop x))\nPr-map-0 {Alg = fork Alg Alg\u2081} f-prop = Pr-fork (trans (0\/2+p\/2\u2261p\/2 0\/1) 0\/2\u22610)\n (Pr-map-0 f-prop) (Pr-map-0 f-prop)\n\nPr-zip : \u2200 {c\u2080 c\u2081 a b} {A : Set a} {B : Set b} {Alg\u2080 : \u21ba c\u2080 A} {Alg\u2081 : \u21ba c\u2081 B} {x y pr\u2081 pr\u2082} \u2192\n Pr[ Alg\u2080 \u2261 x ]\u2261 pr\u2081 \u2192\n Pr[ Alg\u2081 \u2261 y ]\u2261 pr\u2082 \u2192\n Pr[ zip\u21ba Alg\u2080 Alg\u2081 \u2261 (x , y) ]\u2261 (pr\u2081 *\/ pr\u2082)\nPr-zip {c\u2080} {pr\u2082 = pr\u2082} (Pr-return {x = x} (Pr-\u2261 refl)) pf\u2082\n rewrite 1*q\u2261q pr\u2082 = Pr-weaken+ c\u2080 (Pr-map {f = _,_ x} (cong proj\u2082) pf\u2082)\nPr-zip {c\u2080} {pr\u2082 = pr\u2082} (Pr-return {x = x} (Pr-\u2262 pf)) pf\u2082\n rewrite 0*q\u2261q pr\u2082 = Pr-weaken+ c\u2080 (Pr-map-0 (\u03bb y x\u2081 \u2192 pf (cong proj\u2081 x\u2081)))\nPr-zip (Pr-fork refl pf\u2081 pf\u2082) pf\u2083 = Pr-fork (distr-*-\/2+\/2 _ _ _) (Pr-zip pf\u2081 pf\u2083) (Pr-zip pf\u2082 pf\u2083)\n\nex\u2082 : \u2200 x y \u2192 Pr[ toss \u27e8,\u27e9 toss \u2261 (x , y) ]\u2261 1\/4\nex\u2082 x y = Pr-same (trans (distr-\/2-* _ _) (cong (_\/2 \u2218 _\/2) (1*q\u2261q _)))\n (Pr-zip {Alg\u2080 = toss} {Alg\u2081 = toss} (ex\u2081 x) (ex\u2081 y))\n\npr-choose : \u2200 {c a} {A : Set a} {p\u2080 p\u2081 : \u21ba c A} {pr\u2080 pr\u2081 x}\n \u2192 Pr[ p\u2080 \u2261 x ]\u2261 pr\u2080\n \u2192 Pr[ p\u2081 \u2261 x ]\u2261 pr\u2081\n \u2192 Pr[ choose p\u2080 p\u2081 \u2261 x ]\u2261 pr\u2080 \/2+ pr\u2081 \/2\npr-choose {c} {pr\u2080 = pr\u2080} {pr\u2081} pf\u2080 pf\u2081 =\n Pr-fork (pr\u2081 \/2+ pr\u2080 \/2-comm) (Pr-weaken+ 0 pf\u2081) (Pr-weaken+ 0 pf\u2080)\n\npostulate\n pr-ret-xor : \u2200 {x y pr} b\n \u2192 Pr[return\u21ba x \u2261 y ]\u2261 pr\n \u2192 Pr[return\u21ba x \u2261 b xor y ]\u2261 pr\n-- pr-ret-xor b pf = {!!}\n\npr-xor'' : \u2200 {c} {Alg : \u21ba c Bit} {x pr} b\n \u2192 Pr[ Alg \u2261 x ]\u2261 pr\n \u2192 Pr[ Alg \u2261 b xor x ]\u2261 pr\npr-xor'' b (Pr-return pf) = Pr-return (pr-ret-xor b pf)\npr-xor'' b (Pr-fork refl pf\u2080 pf\u2081) = Pr-fork refl (pr-xor'' b pf\u2080) (pr-xor'' b pf\u2081)\n\npr-xor' : \u2200 {c} {Alg : \u21ba c Bit} {x pr b}\n \u2192 Pr[ Alg \u2261 x ]\u2261 pr\n \u2192 Pr[ \u27ea _xor_ b \u00b7 Alg \u27eb \u2261 b xor x ]\u2261 pr\npr-xor' {b = b} = Pr-map (xor-inj {b})\n where\n -- move it!\n not-inj : \u2200 {x y} \u2192 not x \u2261 not y \u2192 x \u2261 y\n not-inj {true} {true} = \u03bb _ \u2192 refl\n not-inj {true} {false} = \u03bb ()\n not-inj {false} {true} = \u03bb ()\n not-inj {false} {false} = \u03bb _ \u2192 refl\n xor-inj : \u2200 {b x y} \u2192 b xor x \u2261 b xor y \u2192 x \u2261 y\n xor-inj {true} = not-inj\n xor-inj {false} = id\n\npostulate\n pr-ret-not : \u2200 {x y pr}\n \u2192 Pr[return\u21ba x \u2261 y ]\u2261 pr\n \u2192 Pr[return\u21ba x \u2261 not y ]\u2261 1- pr\n-- pr-ret-not = {!!}\n\npr-not : \u2200 {c} {Alg : \u21ba c Bit} {x pr}\n \u2192 Pr[ Alg \u2261 x ]\u2261 pr\n \u2192 Pr[ Alg \u2261 not x ]\u2261 1- pr\npr-not (Pr-return pf) = Pr-return (pr-ret-not pf)\npr-not (Pr-fork refl pf pf\u2081) = Pr-fork (1- _ \/2+1- _ \/2) (pr-not pf) (pr-not pf\u2081)\n\npostulate\n pr-xor : \u2200 {c} {Alg : \u21ba c Bit} {x pr b}\n \u2192 Pr[ Alg \u2261 x ]\u2261 pr\n \u2192 Pr[ \u27ea _xor_ b \u00b7 Alg \u27eb \u2261 x ]\u2261 pr\n-- pr-xor {b = b} = {!pr-not!}\n\npostulate\n pr-toss-xor-toss : \u2200 x \u2192 Pr[ toss \u27e8xor\u27e9 toss \u2261 x ]\u2261 1\/2\n-- pr-toss-xor-toss x = {!!}\n\npostulate\n ex\u2083 : \u2200 {n} (x : Bits n) \u2192 Pr[ random \u2261 x ]\u2261 1\/2^ n\n","old_contents":"module flipbased-tree where\n\nopen import Function\nopen import Data.Bits\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\nopen import bintree\nopen import Data.Product\n\nimport flipbased\n\n-- \u201c\u21ba n A\u201d reads like: \u201ctoss n coins and then return a value of type A\u201d\n\u21ba : \u2200 {a} n (A : Set a) \u2192 Set a\n\u21ba = flip Tree\n\nreturn\u21ba : \u2200 {c a} {A : Set a} \u2192 A \u2192 \u21ba c A\nreturn\u21ba = leaf\n\nrunDet : \u2200 {a} {A : Set a} \u2192 \u21ba 0 A \u2192 A\nrunDet (leaf x) = x\n\ntoss : \u21ba 1 Bit\ntoss = fork (leaf 0b) (leaf 1b)\n\nweaken\u2264 : \u2200 {m c a} {A : Set a} \u2192 m \u2264 c \u2192 \u21ba m A \u2192 \u21ba c A\nweaken\u2264 p (leaf x) = leaf x\nweaken\u2264 (s\u2264s p) (fork left right) = fork (weaken\u2264 p left) (weaken\u2264 p right)\n\nm\u2264n+m : \u2200 m n \u2192 m \u2264 n + m\nm\u2264n+m m n = \u2115\u2264.trans (m\u2264m+n m n) (\u2115\u2264.reflexive (\u2115\u00b0.+-comm m n))\n\nweaken+ : \u2200 c {m a} {A : Set a} \u2192 \u21ba m A \u2192 \u21ba (c + m) A\nweaken+ c = weaken\u2264 (m\u2264n+m _ c)\n\nmap\u21ba : \u2200 {c a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 \u21ba c A \u2192 \u21ba c B\nmap\u21ba f (leaf x) = leaf (f x)\nmap\u21ba f (fork left right) = fork (map\u21ba f left) (map\u21ba f right)\n\njoin\u21ba : \u2200 {c\u2081 c\u2082 a} {A : Set a} \u2192 \u21ba c\u2081 (\u21ba c\u2082 A) \u2192 \u21ba (c\u2081 + c\u2082) A\njoin\u21ba {c} (leaf x) = weaken+ c x\njoin\u21ba (fork left right) = fork (join\u21ba left) (join\u21ba right)\n\nopen flipbased \u21ba toss weaken\u2264 leaf map\u21ba join\u21ba public\n\nopen import Data.Bool\nopen import Data.Bits\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\n\ninfix 4 _\/2+_\/2\ninfix 6 _\/2\npostulate\n [0,1] : Set\n 0\/1 : [0,1]\n 1\/1 : [0,1]\n _\/2 : [0,1] \u2192 [0,1]\n _\/2+_\/2 : [0,1] \u2192 [0,1] \u2192 [0,1]\n _*\/_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n-- sym _\/2+_\/2\n-- 1 \/2+ 1 \/2 = 1\/1\n-- p \/2+ p \/2 = p\n-- p \/2+ (1- p) \/2 = 1\/2\n\n -- \u00b7\/1+_ : \u2115 \u2192 Set\n -- \/+\/ : \u2115 \u2192 [0,1] \u2192 [0,1] \u2192 [0,1]\n\n 0\/2+p\/2\u2261p\/2 : \u2200 p \u2192 (0\/1 \/2+ p \/2) \u2261 p \/2\n _\/2+_\/2-comm : \u2200 x y \u2192 x \/2+ y \/2 \u2261 y \/2+ x \/2\n 1*q\u2261q : \u2200 q \u2192 1\/1 *\/ q \u2261 q\n 0*q\u2261q : \u2200 q \u2192 0\/1 *\/ q \u2261 0\/1\n distr-*-\/2+\/2 : \u2200 x y z \u2192 (x *\/ z) \/2+ (y *\/ z) \/2 \u2261 (x \/2+ y \/2) *\/ z\n distr-\/2-* : \u2200 p q \u2192 (p \/2) *\/ (q \/2) \u2261 ((p *\/ q) \/2) \/2\n 0\/2\u22610 : 0\/1 \/2 \u2261 0\/1\n\n1\/2^_ : \u2115 \u2192 [0,1]\n1\/2^ zero = 1\/1\n1\/2^ suc n = (1\/2^ n)\/2\n\n1\/2 : [0,1]\n1\/2 = 1\/2^ 1\n1\/4 : [0,1]\n1\/4 = 1\/2^ 2\n\ndata Pr[return\u21ba_\u2261_]\u2261_ {a} {A : Set a} (x y : A) : [0,1] \u2192 Set a where\n Pr-\u2261 : x \u2261 y \u2192 Pr[return\u21ba x \u2261 y ]\u2261 1\/1\n Pr-\u2262 : x \u2262 y \u2192 Pr[return\u21ba x \u2261 y ]\u2261 0\/1\n\ninfix 2 Pr[_\u2261_]\u2261_\ndata Pr[_\u2261_]\u2261_ {a} {A : Set a} : \u2200 {c} \u2192 \u21ba c A \u2192 A \u2192 [0,1] \u2192 Set a where\n Pr-return : \u2200 {c x y pr} (pf : Pr[return\u21ba x \u2261 y ]\u2261 pr) \u2192 Pr[ return\u21ba {c = c} x \u2261 y ]\u2261 pr\n\n Pr-fork : \u2200 {c} {left right : \u21ba c A} {x p q r}\n (eq : p \/2+ q \/2 \u2261 r)\n (pf\u2080 : Pr[ left \u2261 x ]\u2261 p)\n (pf\u2081 : Pr[ right \u2261 x ]\u2261 q)\n \u2192 Pr[ fork left right \u2261 x ]\u2261 r\n\nPr-fork\u2032 : \u2200 {c a} {A : Set a} {left right : \u21ba c A} {x p q}\n \u2192 Pr[ left \u2261 x ]\u2261 p\n \u2192 Pr[ right \u2261 x ]\u2261 q\n \u2192 Pr[ fork left right \u2261 x ]\u2261 (p \/2+ q \/2)\nPr-fork\u2032 = Pr-fork refl\n\nPr-return-\u2261 : \u2200 {c a} {A : Set a} {x : A} \u2192 Pr[ return\u21ba {c = c} x \u2261 x ]\u2261 1\/1\nPr-return-\u2261 = Pr-return (Pr-\u2261 refl)\n\nPr-return-\u2262 : \u2200 {c a} {A : Set a} {x y : A} \u2192 x \u2262 y \u2192 Pr[ return\u21ba {c = c} x \u2261 y ]\u2261 0\/1\nPr-return-\u2262 = Pr-return \u2218 Pr-\u2262\n\nimport Function.Equality as F\u2261\nimport Function.Equivalence as F\u2248\n\n_\u2248\u21d2_ : \u2200 {c a} {A : Set a} (p\u2081 p\u2082 : \u21ba c A) \u2192 Set a\np\u2081 \u2248\u21d2 p\u2082 = \u2200 {x pr} \u2192 Pr[ p\u2081 \u2261 x ]\u2261 pr \u2192 Pr[ p\u2082 \u2261 x ]\u2261 pr\n\n_\u2248_ : \u2200 {c a} {A : Set a} (p\u2081 p\u2082 : \u21ba c A) \u2192 Set a\np\u2081 \u2248 p\u2082 = \u2200 {x pr} \u2192 (Pr[ p\u2081 \u2261 x ]\u2261 pr) F\u2248.\u21d4 (Pr[ p\u2082 \u2261 x ]\u2261 pr)\n\n\u2248-refl : \u2200 {c a} {A : Set a} \u2192 Reflexive {A = \u21ba c A} _\u2248_\n\u2248-refl = F\u2248.id\n\n\u2248-sym : \u2200 {c a} {A : Set a} {p\u2081 p\u2082 : \u21ba c A} \u2192 p\u2081 \u2248 p\u2082 \u2192 p\u2082 \u2248 p\u2081\n\u2248-sym \u03b7 = F\u2248.sym \u03b7\n\n\u2248-trans : \u2200 {c a} {A : Set a} \u2192 Transitive {A = \u21ba c A} _\u2248_\n\u2248-trans f g = g F\u2248.\u2218 f\n\nfork-sym\u21d2 : \u2200 {c a} {A : Set a} {p\u2081 p\u2082 : \u21ba c A} \u2192 fork p\u2081 p\u2082 \u2248\u21d2 fork p\u2082 p\u2081\nfork-sym\u21d2 (Pr-fork {p = p} {q} refl pf\u2081 pf\u2080) rewrite p \/2+ q \/2-comm = Pr-fork\u2032 pf\u2080 pf\u2081\n\nfork-sym : \u2200 {c a} {A : Set a} {p\u2081 p\u2082 : \u21ba c A} \u2192 fork p\u2081 p\u2082 \u2248 fork p\u2082 p\u2081\nfork-sym = F\u2248.equivalence fork-sym\u21d2 fork-sym\u21d2\n\nPr-fork-0 : \u2200 {c a} {A : Set a} {left right : \u21ba c A} {x : A} {p}\n \u2192 Pr[ left \u2261 x ]\u2261 0\/1\n \u2192 Pr[ right \u2261 x ]\u2261 p\n \u2192 Pr[ fork left right \u2261 x ]\u2261 p \/2\nPr-fork-0 {p = p} eq\u2081 eq\u2082 rewrite sym (0\/2+p\/2\u2261p\/2 p) = Pr-fork\u2032 eq\u2081 eq\u2082\n\nex\u2081 : \u2200 x \u2192 Pr[ toss \u2261 x ]\u2261 1\/2\nex\u2081 1b = Pr-fork-0 (Pr-return-\u2262 (\u03bb ())) Pr-return-\u2261\nex\u2081 0b = F\u2248.Equivalence.to fork-sym F\u2261.\u27e8$\u27e9 (Pr-fork-0 (Pr-return-\u2262 (\u03bb ())) Pr-return-\u2261)\n\nPr-map : \u2200 {c a b} {A : Set a} {B : Set b} {Alg : \u21ba c A} {x pr} {f : A \u2192 B} \u2192\n (f-inj : \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y) \u2192\n Pr[ Alg \u2261 x ]\u2261 pr \u2192\n Pr[ \u27ea f \u00b7 Alg \u27eb \u2261 f x ]\u2261 pr\nPr-map f-inj (Pr-return (Pr-\u2261 refl)) = Pr-return (Pr-\u2261 refl)\nPr-map f-inj (Pr-return (Pr-\u2262 x\u2262y)) = Pr-return (Pr-\u2262 (x\u2262y \u2218 f-inj))\nPr-map f-inj (Pr-fork eq pf\u2080 pf\u2081) = Pr-fork eq (Pr-map f-inj pf\u2080) (Pr-map f-inj pf\u2081)\n\nPr-same : \u2200 {c a} {A : Set a} {Alg : \u21ba c A} {x pr\u2080 pr\u2081} \u2192\n pr\u2080 \u2261 pr\u2081 \u2192\n Pr[ Alg \u2261 x ]\u2261 pr\u2080 \u2192\n Pr[ Alg \u2261 x ]\u2261 pr\u2081\nPr-same refl = id\n\nPr-weaken\u2264 : \u2200 {c\u2080 c\u2081 a} {A : Set a} {Alg : \u21ba c\u2080 A} {x pr} \u2192\n (c\u2080\u2264c\u2081 : c\u2080 \u2264 c\u2081) \u2192\n Pr[ Alg \u2261 x ]\u2261 pr \u2192\n Pr[ weaken\u2264 c\u2080\u2264c\u2081 Alg \u2261 x ]\u2261 pr\nPr-weaken\u2264 p (Pr-return pf) = Pr-return pf\nPr-weaken\u2264 (s\u2264s c\u2080\u2264c\u2081) (Pr-fork eq pf\u2080 pf\u2081)\n = Pr-fork eq (Pr-weaken\u2264 c\u2080\u2264c\u2081 pf\u2080) (Pr-weaken\u2264 c\u2080\u2264c\u2081 pf\u2081)\n\nPr-weaken+ : \u2200 {c\u2080} c\u2081 {a} {A : Set a} {Alg : \u21ba c\u2080 A} {x pr} \u2192\n Pr[ Alg \u2261 x ]\u2261 pr \u2192\n Pr[ weaken+ c\u2081 Alg \u2261 x ]\u2261 pr\nPr-weaken+ c\u2081 = Pr-weaken\u2264 (m\u2264n+m _ c\u2081)\n\nPr-map-0 : \u2200 {c a b} {A : Set a} {B : Set b} {Alg : \u21ba c A} {f : A \u2192 B} {x} \u2192 (\u2200 y \u2192 f y \u2262 x)\n \u2192 Pr[ map\u21ba f Alg \u2261 x ]\u2261 0\/1\nPr-map-0 {Alg = leaf x} f-prop = Pr-return (Pr-\u2262 (f-prop x))\nPr-map-0 {Alg = fork Alg Alg\u2081} f-prop = Pr-fork (trans (0\/2+p\/2\u2261p\/2 0\/1) 0\/2\u22610)\n (Pr-map-0 f-prop) (Pr-map-0 f-prop)\n\nPr-zip : \u2200 {c\u2080 c\u2081 a b} {A : Set a} {B : Set b} {Alg\u2080 : \u21ba c\u2080 A} {Alg\u2081 : \u21ba c\u2081 B} {x y pr\u2081 pr\u2082} \u2192\n Pr[ Alg\u2080 \u2261 x ]\u2261 pr\u2081 \u2192\n Pr[ Alg\u2081 \u2261 y ]\u2261 pr\u2082 \u2192\n Pr[ zip\u21ba Alg\u2080 Alg\u2081 \u2261 (x , y) ]\u2261 (pr\u2081 *\/ pr\u2082)\nPr-zip {c\u2080} {pr\u2082 = pr\u2082} (Pr-return {x = x} (Pr-\u2261 refl)) pf\u2082\n rewrite 1*q\u2261q pr\u2082 = Pr-weaken+ c\u2080 (Pr-map {f = _,_ x} (cong proj\u2082) pf\u2082)\nPr-zip {c\u2080} {pr\u2082 = pr\u2082} (Pr-return {x = x} (Pr-\u2262 pf)) pf\u2082\n rewrite 0*q\u2261q pr\u2082 = Pr-weaken+ c\u2080 (Pr-map-0 (\u03bb y x\u2081 \u2192 pf (cong proj\u2081 x\u2081)))\nPr-zip (Pr-fork refl pf\u2081 pf\u2082) pf\u2083 = Pr-fork (distr-*-\/2+\/2 _ _ _) (Pr-zip pf\u2081 pf\u2083) (Pr-zip pf\u2082 pf\u2083)\n\nex\u2082 : \u2200 x y \u2192 Pr[ toss \u27e8,\u27e9 toss \u2261 (x , y) ]\u2261 1\/4\nex\u2082 x y = Pr-same (trans (distr-\/2-* _ _) (cong (_\/2 \u2218 _\/2) (1*q\u2261q _)))\n (Pr-zip {Alg\u2080 = toss} {Alg\u2081 = toss} (ex\u2081 x) (ex\u2081 y))\n\npostulate\n ex\u2083 : \u2200 {n} (x : Bits n) \u2192 Pr[ random \u2261 x ]\u2261 1\/2^ n\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"acb7dfad8b6ef7aeaa2800916aa6da9df5bf1c30","subject":"Reemphasize the importance of \"import Everything\".","message":"Reemphasize the importance of \"import Everything\".\n\nOld-commit-hash: d6200d73c289f7ab4460589159b8380be47e083b\n","repos":"inc-lc\/ilc-agda","old_file":"README.agda","new_file":"README.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Machine-checked formalization of the theoretical results presented\n-- in the paper:\n--\n-- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann.\n-- A Theory of Changes for Higher-Order Languages:\n-- Incrementalizing \u03bb-Calculi by Static Differentiation.\n-- To appear at PLDI, ACM 2014.\n--\n------------------------------------------------------------------------\n\nmodule README where\n\n-- We know two good ways to read this code base. You can either\n-- use Emacs with agda2-mode to interact with the source files,\n-- or you can use a web browser to view the pretty-printed and\n-- hyperlinked source files. For Agda power users, we also\n-- include basic setup information for your own machine.\n\n\n-- IF YOU WANT TO USE A BROWSER\n-- ============================\n--\n-- Start with *HTML* version of this readme. On the AEC\n-- submission website (online or inside the VM), follow the link\n-- \"view the Agda code in their browser\" to the file\n-- agda\/README.html.\n--\n-- The source code is syntax highlighted and hyperlinked. You can\n-- click on module names to open the corresponding files, or you\n-- can click on identifiers to jump to their definition. In\n-- general, a Agda file with name `Foo\/Bar\/Baz.agda` contains a\n-- module `Foo.Bar.Baz` and is shown in an HTML file\n-- `Foo.Bar.Baz.html`.\n--\n-- Note that we also include the HTML files generated for our\n-- transitive dependencies from the Agda standard library. This\n-- allows you to follow hyperlinks to the Agda standard\n-- library. It is the default behavior of `agda --html` which we\n-- used to generate the HTML.\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE EMACS\n-- ========================\n--\n-- Open this file in Emacs with agda2-mode installed. See below\n-- for which Agda version you need to install. On the VM image,\n-- everything is setup for you and you can just open this file in\n-- Emacs.\n--\n-- C-c C-l Load code. Type checks and syntax highlights. Use\n-- this if the code is not syntax highlighted, or\n-- after you changed something that you want to type\n-- check.\n--\n-- M-. Jump to definition of identifier at the point.\n-- M-* Jump back.\n--\n-- Note that README.agda imports Everything.agda which imports\n-- every Agda file in our formalization. So if README.agda type\n-- checks successfully, everything does. If you want to type\n-- check everything from scratch, delete the *.agdai files to\n-- disable separate compilation. You can use \"find . -name\n-- '*.agdai' | xargs rm\" to do that.\n--\n-- More information on the Agda mode is available on the Agda wiki:\n--\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Docs.EmacsModeKeyCombinations\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE YOUR OWN SETUP\n-- =================================\n--\n-- To typecheck this formalization, you need to install the appropriate version\n-- of Agda, the Agda standard library (version 0.7), generate Everything.agda\n-- with the attached Haskell helper, and finally run Agda on this file.\n--\n-- Given a Unix-like environment (including Cygwin), running the .\/agdaCheck.sh\n-- script and following instructions given on output will eventually generate\n-- Everything.agda and proceed to type check everything on command line.\n--\n-- We use Agda HEAD from September 2013; Agda 2.3.2.1 might happen to work, but\n-- has some bugs with serialization of code using some recent syntactic sugar\n-- which we use (https:\/\/code.google.com\/p\/agda\/issues\/detail?id=756), so it\n-- might work or not. When it does not, removing Agda caches (.agdai files)\n-- appears to often help. You can use \"find . -name '*.agdai' | xargs rm\" to do\n-- that.\n--\n-- If you're not an Agda power user, it is probably easier to use\n-- the VM image or look at the pretty-printed and hyperlinked\n-- HTML files, see above.\n\n\n\n-- WHERE TO START READING?\n-- =======================\n--\n-- modules.pdf\n-- The graph of dependencies between Agda modules.\n-- Good if you want to get a broad overview.\n--\n-- README.agda\n-- This file. A coarse-grained introduction to the Agda\n-- formalization. Good if you want to begin at the beginning\n-- and understand the structure of our code.\n--\n-- PLDI14-List-of-Theorems.agda\n-- Pointers to the Agda formalizations of all theorems, lemmas\n-- or definitions in the PLDI paper. Good if you want to read\n-- the paper and the Agda code side by side.\n--\n-- Here is an import of this file, so you can jump to it\n-- directly (use M-. in Emacs or click the module name in the\n-- Browser):\n\nimport PLDI14-List-of-Theorems\n\n-- Everything.agda\n-- Imports every Agda module in our formalization. Good if you\n-- want to make sure you don't miss anything.\n--\n-- Again, here's is an import of this file so you can navigate\n-- there:\n\nimport Everything\n\n-- (This import is also important to ensure that if we typecheck\n-- README.agda, we also typecheck every other file of our\n-- formalization).\n\n\n\n-- THE AGDA CODE\n-- =============\n\nimport Postulate.Extensionality\n\nimport Base.Data.DependentList\n\n-- Variables and contexts\nimport Base.Syntax.Context\n\n-- Sets of variables\nimport Base.Syntax.Vars\n\nimport Base.Denotation.Notation\n\n-- Environments\nimport Base.Denotation.Environment\n\n-- Change contexts\nimport Base.Change.Context\n\n-- # Base, parametric proof.\n--\n-- This is for a parametric calculus where:\n-- types are parametric in base types\n-- terms are parametric in constants\n--\n--\n-- Modules are ordered and grouped according to what they represent.\n\n-- ## Definitions\n\nimport Parametric.Syntax.Type\nimport Parametric.Syntax.Term\n\nimport Parametric.Denotation.Value\nimport Parametric.Denotation.Evaluation\n\nimport Parametric.Change.Type\nimport Parametric.Change.Term\n\nimport Parametric.Change.Derive\n\nimport Parametric.Change.Value\nimport Parametric.Change.Evaluation\n\n-- ## Proofs\n\nimport Parametric.Change.Validity\nimport Parametric.Change.Specification\nimport Parametric.Change.Implementation\nimport Parametric.Change.Correctness\n\n-- # Nehemiah plugin\n--\n-- The structure is the same as the parametric proof (down to the\n-- order and the grouping of modules), except for the postulate module.\n\n-- Postulate an abstract data type for integer Bags.\nimport Postulate.Bag-Nehemiah\n\n-- ## Definitions\nimport Nehemiah.Syntax.Type\nimport Nehemiah.Syntax.Term\n\nimport Nehemiah.Denotation.Value\nimport Nehemiah.Denotation.Evaluation\n\nimport Nehemiah.Change.Term\nimport Nehemiah.Change.Type\n\nimport Nehemiah.Change.Derive\n\nimport Nehemiah.Change.Value\nimport Nehemiah.Change.Evaluation\n\n-- ## Proofs\n\nimport Nehemiah.Change.Validity\nimport Nehemiah.Change.Specification\nimport Nehemiah.Change.Implementation\nimport Nehemiah.Change.Correctness\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Machine-checked formalization of the theoretical results presented\n-- in the paper:\n--\n-- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann.\n-- A Theory of Changes for Higher-Order Languages:\n-- Incrementalizing \u03bb-Calculi by Static Differentiation.\n-- To appear at PLDI, ACM 2014.\n--\n------------------------------------------------------------------------\n\nmodule README where\n\n-- We know two good ways to read this code base. You can either\n-- use Emacs with agda2-mode to interact with the source files,\n-- or you can use a web browser to view the pretty-printed and\n-- hyperlinked source files. For Agda power users, we also\n-- include basic setup information for your own machine.\n\n\n-- IF YOU WANT TO USE A BROWSER\n-- ============================\n--\n-- Start with *HTML* version of this readme. On the AEC\n-- submission website (online or inside the VM), follow the link\n-- \"view the Agda code in their browser\" to the file\n-- agda\/README.html.\n--\n-- The source code is syntax highlighted and hyperlinked. You can\n-- click on module names to open the corresponding files, or you\n-- can click on identifiers to jump to their definition. In\n-- general, a Agda file with name `Foo\/Bar\/Baz.agda` contains a\n-- module `Foo.Bar.Baz` and is shown in an HTML file\n-- `Foo.Bar.Baz.html`.\n--\n-- Note that we also include the HTML files generated for our\n-- transitive dependencies from the Agda standard library. This\n-- allows you to follow hyperlinks to the Agda standard\n-- library. It is the default behavior of `agda --html` which we\n-- used to generate the HTML.\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE EMACS\n-- ========================\n--\n-- Open this file in Emacs with agda2-mode installed. See below\n-- for which Agda version you need to install. On the VM image,\n-- everything is setup for you and you can just open this file in\n-- Emacs.\n--\n-- C-c C-l Load code. Type checks and syntax highlights. Use\n-- this if the code is not syntax highlighted, or\n-- after you changed something that you want to type\n-- check.\n--\n-- M-. Jump to definition of identifier at the point.\n-- M-* Jump back.\n--\n-- Note that README.agda imports Everything.agda which imports\n-- every Agda file in our formalization. So if README.agda type\n-- checks successfully, everything does. If you want to type\n-- check everything from scratch, delete the *.agdai files to\n-- disable separate compilation. You can use \"find . -name\n-- '*.agdai' | xargs rm\" to do that.\n--\n-- More information on the Agda mode is available on the Agda wiki:\n--\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Docs.EmacsModeKeyCombinations\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE YOUR OWN SETUP\n-- =================================\n--\n-- To typecheck this formalization, you need to install the appropriate version\n-- of Agda, the Agda standard library (version 0.7), generate Everything.agda\n-- with the attached Haskell helper, and finally run Agda on this file.\n--\n-- Given a Unix-like environment (including Cygwin), running the .\/agdaCheck.sh\n-- script and following instructions given on output will eventually generate\n-- Everything.agda and proceed to type check everything on command line.\n--\n-- We use Agda HEAD from September 2013; Agda 2.3.2.1 might happen to work, but\n-- has some bugs with serialization of code using some recent syntactic sugar\n-- which we use (https:\/\/code.google.com\/p\/agda\/issues\/detail?id=756), so it\n-- might work or not. When it does not, removing Agda caches (.agdai files)\n-- appears to often help. You can use \"find . -name '*.agdai' | xargs rm\" to do\n-- that.\n--\n-- If you're not an Agda power user, it is probably easier to use\n-- the VM image or look at the pretty-printed and hyperlinked\n-- HTML files, see above.\n\n\n\n-- WHERE TO START READING?\n-- =======================\n--\n-- modules.pdf\n-- The graph of dependencies between Agda modules.\n-- Good if you want to get a broad overview.\n--\n-- README.agda\n-- This file. A coarse-grained introduction to the Agda\n-- formalization. Good if you want to begin at the beginning\n-- and understand the structure of our code.\n--\n-- PLDI14-List-of-Theorems.agda\n-- Pointers to the Agda formalizations of all theorems, lemmas\n-- or definitions in the PLDI paper. Good if you want to read\n-- the paper and the Agda code side by side.\n--\n-- Here is an import of this file, so you can jump to it\n-- directly (use M-. in Emacs or click the module name in the\n-- Browser):\n\nimport PLDI14-List-of-Theorems\n\n-- Everything.agda\n-- Imports every Agda module in our formalization. Good if you\n-- want to make sure you don't miss anything.\n--\n-- Again, here's is an import of this file so you can navigate\n-- there:\n\nimport Everything\n\n\n\n-- THE AGDA CODE\n-- =============\n\nimport Postulate.Extensionality\n\nimport Base.Data.DependentList\n\n-- Variables and contexts\nimport Base.Syntax.Context\n\n-- Sets of variables\nimport Base.Syntax.Vars\n\nimport Base.Denotation.Notation\n\n-- Environments\nimport Base.Denotation.Environment\n\n-- Change contexts\nimport Base.Change.Context\n\n-- # Base, parametric proof.\n--\n-- This is for a parametric calculus where:\n-- types are parametric in base types\n-- terms are parametric in constants\n--\n--\n-- Modules are ordered and grouped according to what they represent.\n\n-- ## Definitions\n\nimport Parametric.Syntax.Type\nimport Parametric.Syntax.Term\n\nimport Parametric.Denotation.Value\nimport Parametric.Denotation.Evaluation\n\nimport Parametric.Change.Type\nimport Parametric.Change.Term\n\nimport Parametric.Change.Derive\n\nimport Parametric.Change.Value\nimport Parametric.Change.Evaluation\n\n-- ## Proofs\n\nimport Parametric.Change.Validity\nimport Parametric.Change.Specification\nimport Parametric.Change.Implementation\nimport Parametric.Change.Correctness\n\n-- # Nehemiah plugin\n--\n-- The structure is the same as the parametric proof (down to the\n-- order and the grouping of modules), except for the postulate module.\n\n-- Postulate an abstract data type for integer Bags.\nimport Postulate.Bag-Nehemiah\n\n-- ## Definitions\nimport Nehemiah.Syntax.Type\nimport Nehemiah.Syntax.Term\n\nimport Nehemiah.Denotation.Value\nimport Nehemiah.Denotation.Evaluation\n\nimport Nehemiah.Change.Term\nimport Nehemiah.Change.Type\n\nimport Nehemiah.Change.Derive\n\nimport Nehemiah.Change.Value\nimport Nehemiah.Change.Evaluation\n\n-- ## Proofs\n\nimport Nehemiah.Change.Validity\nimport Nehemiah.Change.Specification\nimport Nehemiah.Change.Implementation\nimport Nehemiah.Change.Correctness\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"83d6259ce1197a7fe5da67c77747c8df330e7d7c","subject":"ToNat: + \u2264-\u2294","message":"ToNat: + \u2264-\u2294\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Relation\/Binary\/ToNat.agda","new_file":"lib\/Relation\/Binary\/ToNat.agda","new_contents":"import Data.Nat.NP as \u2115\nopen \u2115 using (\u2115; zero; suc; module \u2115\u2264)\nopen import Data.Bool\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Sum renaming (map to \u228e-map)\nopen import Function\nopen import Relation.Binary\nopen import Algebra.FunctionProperties\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_)\n\nmodule Relation.Binary.ToNat {a} {A : Set a} (f : A \u2192 \u2115) (f-inj : \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y) where\n\n_<=_ : A \u2192 A \u2192 Bool\n_<=_ = \u2115._<=_ on f \n\n_\u2264_ : A \u2192 A \u2192 Set\nx \u2264 y = T (x <= y)\n\n_\u2293_ : A \u2192 A \u2192 A\nx \u2293 y = if x <= y then x else y\n\n_\u2294_ : A \u2192 A \u2192 A\nx \u2294 y = if x <= y then y else x\n\nisPreorder : IsPreorder _\u2261_ _\u2264_\nisPreorder = record { isEquivalence = \u2261.isEquivalence\n ; reflexive = reflexive\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n where open \u2115.<=\n reflexive : \u2200 {i j} \u2192 i \u2261 j \u2192 i \u2264 j\n reflexive {i} \u2261.refl = complete (\u2115\u2264.refl {f i})\n trans : Transitive _\u2264_\n trans {x} {y} {z} p q = complete (\u2115\u2264.trans (sound (f x) (f y) p) (sound (f y) (f z) q))\n\nisPartialOrder : IsPartialOrder _\u2261_ _\u2264_\nisPartialOrder = record { isPreorder = isPreorder; antisym = antisym }\n where open \u2115.<= using (sound)\n antisym : Antisymmetric _\u2261_ _\u2264_\n antisym {x} {y} p q = f-inj (\u2115\u2264.antisym (sound (f x) (f y) p) (sound (f y) (f x) q))\n\nisTotalOrder : IsTotalOrder _\u2261_ _\u2264_\nisTotalOrder = record { isPartialOrder = isPartialOrder; total = \u03bb x y \u2192 \u228e-map complete complete (\u2115\u2264.total (f x) (f y)) }\n where open \u2115.<= using (complete)\n\nopen IsTotalOrder isTotalOrder\n\n\u2294-spec : \u2200 {x y} \u2192 x \u2264 y \u2192 x \u2294 y \u2261 y\n\u2294-spec {x} {y} p with x <= y\n\u2294-spec _ | true = \u2261.refl\n\u2294-spec () | false\n\n\u2293-spec : \u2200 {x y} \u2192 x \u2264 y \u2192 x \u2293 y \u2261 x\n\u2293-spec {x} {y} p with x <= y\n\u2293-spec _ | true = \u2261.refl\n\u2293-spec () | false\n\n\u2293-comm : Commutative _\u2261_ _\u2293_\n\u2293-comm x y with x <= y | y <= x | antisym {x} {y} | total x y\n... | true | true | p | _ = p _ _\n... | true | false | _ | _ = \u2261.refl\n... | false | true | _ | _ = \u2261.refl\n... | false | false | _ | p = \u22a5-elim ([ id , id ] p)\n\n\u2294-comm : Commutative _\u2261_ _\u2294_\n\u2294-comm x y with x <= y | y <= x | antisym {y} {x} | total x y\n... | true | true | p | _ = p _ _\n... | true | false | _ | _ = \u2261.refl\n... | false | true | _ | _ = \u2261.refl\n... | false | false | _ | p = \u22a5-elim ([ id , id ] p)\n\n\u2293-\u2264 : \u2200 x y \u2192 (x \u2293 y) \u2264 y\n\u2293-\u2264 x y with total x y\n\u2293-\u2264 x y | inj\u2081 p rewrite \u2293-spec p = p\n\u2293-\u2264 x y | inj\u2082 p rewrite \u2293-comm x y | \u2293-spec p = refl\n\n\u2264-\u2294 : \u2200 x y \u2192 x \u2264 (y \u2294 x)\n\u2264-\u2294 x y with total x y\n\u2264-\u2294 x y | inj\u2081 p rewrite \u2294-comm y x | \u2294-spec p = p\n\u2264-\u2294 x y | inj\u2082 p rewrite \u2294-spec p = refl\n\n\u2264-<_,_> : \u2200 {x y z} \u2192 x \u2264 y \u2192 x \u2264 z \u2192 x \u2264 (y \u2293 z)\n\u2264-<_,_> {x} {y} {z} x\u2264y y\u2264z with total y z\n... | inj\u2081 p rewrite \u2293-spec p = x\u2264y\n... | inj\u2082 p rewrite \u2293-comm y z | \u2293-spec p = y\u2264z\n\n\u2264-[_,_] : \u2200 {x y z} \u2192 x \u2264 z \u2192 y \u2264 z \u2192 (x \u2294 y) \u2264 z\n\u2264-[_,_] {x} {y} {z} x\u2264z y\u2264z with total x y\n... | inj\u2081 p rewrite \u2294-spec p = y\u2264z\n... | inj\u2082 p rewrite \u2294-comm x y | \u2294-spec p = x\u2264z\n\n\u2264-\u2293\u2080 : \u2200 {x y z} \u2192 x \u2264 (y \u2293 z) \u2192 x \u2264 y\n\u2264-\u2293\u2080 {x} {y} {z} with total y z\n... | inj\u2081 p rewrite \u2293-spec p = id\n... | inj\u2082 p rewrite \u2293-comm y z | \u2293-spec p = flip trans p\n\n\u2264-\u2293\u2081 : \u2200 {x y z} \u2192 x \u2264 (y \u2293 z) \u2192 x \u2264 z\n\u2264-\u2293\u2081 {x} {y} {z} with total y z\n... | inj\u2081 p rewrite \u2293-spec p = flip trans p\n... | inj\u2082 p rewrite \u2293-comm y z | \u2293-spec p = id\n\n\u2264-\u2294\u2080 : \u2200 {x y z} \u2192 (x \u2294 y) \u2264 z \u2192 x \u2264 z\n\u2264-\u2294\u2080 {x} {y} {z} with total x y\n... | inj\u2081 p rewrite \u2294-spec p = trans p\n... | inj\u2082 p rewrite \u2294-comm x y | \u2294-spec p = id\n\n\u2264-\u2294\u2081 : \u2200 {x y z} \u2192 (x \u2294 y) \u2264 z \u2192 y \u2264 z\n\u2264-\u2294\u2081 {x} {y} {z} with total x y\n... | inj\u2081 p rewrite \u2294-spec p = id\n... | inj\u2082 p rewrite \u2294-comm x y | \u2294-spec p = trans p\n","old_contents":"import Data.Nat.NP as \u2115\nopen \u2115 using (\u2115; zero; suc; module \u2115\u2264)\nopen import Data.Bool\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Sum renaming (map to \u228e-map)\nopen import Function\nopen import Relation.Binary\nopen import Algebra.FunctionProperties\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_)\n\nmodule Relation.Binary.ToNat {a} {A : Set a} (f : A \u2192 \u2115) (f-inj : \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y) where\n\n_<=_ : A \u2192 A \u2192 Bool\n_<=_ = \u2115._<=_ on f \n\n_\u2264_ : A \u2192 A \u2192 Set\nx \u2264 y = T (x <= y)\n\n_\u2293_ : A \u2192 A \u2192 A\nx \u2293 y = if x <= y then x else y\n\n_\u2294_ : A \u2192 A \u2192 A\nx \u2294 y = if x <= y then y else x\n\nisPreorder : IsPreorder _\u2261_ _\u2264_\nisPreorder = record { isEquivalence = \u2261.isEquivalence\n ; reflexive = reflexive\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n where open \u2115.<=\n reflexive : \u2200 {i j} \u2192 i \u2261 j \u2192 i \u2264 j\n reflexive {i} \u2261.refl = complete (\u2115\u2264.refl {f i})\n trans : Transitive _\u2264_\n trans {x} {y} {z} p q = complete (\u2115\u2264.trans (sound (f x) (f y) p) (sound (f y) (f z) q))\n\nisPartialOrder : IsPartialOrder _\u2261_ _\u2264_\nisPartialOrder = record { isPreorder = isPreorder; antisym = antisym }\n where open \u2115.<= using (sound)\n antisym : Antisymmetric _\u2261_ _\u2264_\n antisym {x} {y} p q = f-inj (\u2115\u2264.antisym (sound (f x) (f y) p) (sound (f y) (f x) q))\n\nisTotalOrder : IsTotalOrder _\u2261_ _\u2264_\nisTotalOrder = record { isPartialOrder = isPartialOrder; total = \u03bb x y \u2192 \u228e-map complete complete (\u2115\u2264.total (f x) (f y)) }\n where open \u2115.<= using (complete)\n\nopen IsTotalOrder isTotalOrder\n\n\u2294-spec : \u2200 {x y} \u2192 x \u2264 y \u2192 x \u2294 y \u2261 y\n\u2294-spec {x} {y} p with x <= y\n\u2294-spec _ | true = \u2261.refl\n\u2294-spec () | false\n\n\u2293-spec : \u2200 {x y} \u2192 x \u2264 y \u2192 x \u2293 y \u2261 x\n\u2293-spec {x} {y} p with x <= y\n\u2293-spec _ | true = \u2261.refl\n\u2293-spec () | false\n\n\u2293-comm : Commutative _\u2261_ _\u2293_\n\u2293-comm x y with x <= y | y <= x | antisym {x} {y} | total x y\n... | true | true | p | _ = p _ _\n... | true | false | _ | _ = \u2261.refl\n... | false | true | _ | _ = \u2261.refl\n... | false | false | _ | p = \u22a5-elim ([ id , id ] p)\n\n\u2294-comm : Commutative _\u2261_ _\u2294_\n\u2294-comm x y with x <= y | y <= x | antisym {y} {x} | total x y\n... | true | true | p | _ = p _ _\n... | true | false | _ | _ = \u2261.refl\n... | false | true | _ | _ = \u2261.refl\n... | false | false | _ | p = \u22a5-elim ([ id , id ] p)\n\n\u2293-\u2264 : \u2200 x y \u2192 (x \u2293 y) \u2264 y\n\u2293-\u2264 x y with total x y\n\u2293-\u2264 x y | inj\u2081 p rewrite \u2293-spec p = p\n\u2293-\u2264 x y | inj\u2082 p rewrite \u2293-comm x y | \u2293-spec p = refl\n\n\u2264-<_,_> : \u2200 {x y z} \u2192 x \u2264 y \u2192 x \u2264 z \u2192 x \u2264 (y \u2293 z)\n\u2264-<_,_> {x} {y} {z} x\u2264y y\u2264z with total y z\n... | inj\u2081 p rewrite \u2293-spec p = x\u2264y\n... | inj\u2082 p rewrite \u2293-comm y z | \u2293-spec p = y\u2264z\n\n\u2264-[_,_] : \u2200 {x y z} \u2192 x \u2264 z \u2192 y \u2264 z \u2192 (x \u2294 y) \u2264 z\n\u2264-[_,_] {x} {y} {z} x\u2264z y\u2264z with total x y\n... | inj\u2081 p rewrite \u2294-spec p = y\u2264z\n... | inj\u2082 p rewrite \u2294-comm x y | \u2294-spec p = x\u2264z\n\n\u2264-\u2293\u2080 : \u2200 {x y z} \u2192 x \u2264 (y \u2293 z) \u2192 x \u2264 y\n\u2264-\u2293\u2080 {x} {y} {z} with total y z\n... | inj\u2081 p rewrite \u2293-spec p = id\n... | inj\u2082 p rewrite \u2293-comm y z | \u2293-spec p = flip trans p\n\n\u2264-\u2293\u2081 : \u2200 {x y z} \u2192 x \u2264 (y \u2293 z) \u2192 x \u2264 z\n\u2264-\u2293\u2081 {x} {y} {z} with total y z\n... | inj\u2081 p rewrite \u2293-spec p = flip trans p\n... | inj\u2082 p rewrite \u2293-comm y z | \u2293-spec p = id\n\n\u2264-\u2294\u2080 : \u2200 {x y z} \u2192 (x \u2294 y) \u2264 z \u2192 x \u2264 z\n\u2264-\u2294\u2080 {x} {y} {z} with total x y\n... | inj\u2081 p rewrite \u2294-spec p = trans p\n... | inj\u2082 p rewrite \u2294-comm x y | \u2294-spec p = id\n\n\u2264-\u2294\u2081 : \u2200 {x y z} \u2192 (x \u2294 y) \u2264 z \u2192 y \u2264 z\n\u2264-\u2294\u2081 {x} {y} {z} with total x y\n... | inj\u2081 p rewrite \u2294-spec p = id\n... | inj\u2082 p rewrite \u2294-comm x y | \u2294-spec p = trans p\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"ef8b8ce4e463417ddc66f1c9d64f5f7497d0389e","subject":"Desc model: implicit box","message":"Desc model: implicit box","repos":"kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram,kwangkim\/pigment","old_file":"models\/IDesc_type_type.agda","new_file":"models\/IDesc_type_type.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : (I : Set)(i : I) -> IDescl I\nvarl I i = con (lvar , i)\n\nconstl : (I : Set)(X : Set) -> IDescl I\nconstl I X = con (lconst , X)\n\nprodl : (I : Set)(D D' : IDescl I) -> IDescl I\nprodl I D D' = con (lprod , (D , D'))\n\npil : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\npil I S T = con (lpi , ( S , T))\n\nsigmal : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal I S T = con (lsigma , ( S , T))\n\ncases : (I : Set) \n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases I ( lvar , i ) hs = var i\ncases I ( lconst , X ) hs = const X\ncases I ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases I ( lpi , ( S , T ) ) hs = pi S hs\ncases I ( lsigma , ( S , T ) ) hs = sigma S hs\n\niso1 : (I : Set) -> IDescl I -> IDesc I\niso1 I d = induction Unit (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases I) Void d\n\niso2 : (I : Set) -> IDesc I -> IDescl I\niso2 I (var i) = varl I i\niso2 I (const X) = constl I X\niso2 I (prod D D') = prodl I (iso2 I D) (iso2 I D')\niso2 I (pi S T) = pil I S (\\s -> iso2 I (T s))\niso2 I (sigma S T) = sigmal I S (\\s -> iso2 I (T s))\n\n\n\nproof-iso1-iso2 : (I : Set) -> (D : IDesc I) -> iso1 I (iso2 I D) == D\nproof-iso1-iso2 I (var i) = refl\nproof-iso1-iso2 I (const x) = refl\nproof-iso1-iso2 I (prod D D') with proof-iso1-iso2 I D | proof-iso1-iso2 I D'\n... | p | q = cong2 prod p q\nproof-iso1-iso2 I (pi S T) = cong (pi S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\nproof-iso1-iso2 I (sigma S T) = cong (sigma S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\n\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = iso2 I (iso1 I D) == D\n\n\nproof-iso2-iso1 : (I : Set) -> (D : IDescl I) -> iso2 I (iso1 I D) == D\nproof-iso2-iso1 I D = induction Unit \n (\u03bb _ \u2192 descD I)\n (P I)\n (proof-iso2-iso1-casesW I) \n Void\n D\n where proof-iso2-iso1-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-iso2-iso1-cases I (lvar , i) hs = refl\n proof-iso2-iso1-cases I (lconst , x) hs = refl\n proof-iso2-iso1-cases I (lprod , ( D , D' )) ( p , q ) = cong2 (prodl I) p q \n proof-iso2-iso1-cases I (lpi , ( S , T )) hs = cong (pil I S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-cases I (lsigma , ( S , T )) hs = cong (sigmal I S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-iso2-iso1-casesW I Void = proof-iso2-iso1-cases I\n \n\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : (I : Set)(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox I (var i) P x = var (i , x)\nbox I (const X) P x = const X\nbox I (prod D D') P (d , d') = prod (box I D P d) (box I D' P d')\nbox I (sigma S T) P (a , b) = box I (T a) P b\nbox I (pi S T) P f = pi S (\\s -> box I (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box I (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box I D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (I : Set)\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box I (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : (I : Set)(i : I) -> IDescl I\nvarl I i = con (lvar , i)\n\nconstl : (I : Set)(X : Set) -> IDescl I\nconstl I X = con (lconst , X)\n\nprodl : (I : Set)(D D' : IDescl I) -> IDescl I\nprodl I D D' = con (lprod , (D , D'))\n\npil : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\npil I S T = con (lpi , ( S , T))\n\nsigmal : (I : Set)(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal I S T = con (lsigma , ( S , T))\n\ncases : (I : Set) \n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box Unit (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases I ( lvar , i ) hs = var i\ncases I ( lconst , X ) hs = const X\ncases I ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases I ( lpi , ( S , T ) ) hs = pi S hs\ncases I ( lsigma , ( S , T ) ) hs = sigma S hs\n\niso1 : (I : Set) -> IDescl I -> IDesc I\niso1 I d = induction Unit (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases I) Void d\n\niso2 : (I : Set) -> IDesc I -> IDescl I\niso2 I (var i) = varl I i\niso2 I (const X) = constl I X\niso2 I (prod D D') = prodl I (iso2 I D) (iso2 I D')\niso2 I (pi S T) = pil I S (\\s -> iso2 I (T s))\niso2 I (sigma S T) = sigmal I S (\\s -> iso2 I (T s))\n\n\n\nproof-iso1-iso2 : (I : Set) -> (D : IDesc I) -> iso1 I (iso2 I D) == D\nproof-iso1-iso2 I (var i) = refl\nproof-iso1-iso2 I (const x) = refl\nproof-iso1-iso2 I (prod D D') with proof-iso1-iso2 I D | proof-iso1-iso2 I D'\n... | p | q = cong2 prod p q\nproof-iso1-iso2 I (pi S T) = cong (pi S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\nproof-iso1-iso2 I (sigma S T) = cong (sigma S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\n\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = iso2 I (iso1 I D) == D\n\n\nproof-iso2-iso1 : (I : Set) -> (D : IDescl I) -> iso2 I (iso1 I D) == D\nproof-iso2-iso1 I D = induction Unit \n (\u03bb _ \u2192 descD I)\n (P I)\n (proof-iso2-iso1-casesW I) \n Void\n D\n where proof-iso2-iso1-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box Unit (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-iso2-iso1-cases I (lvar , i) hs = refl\n proof-iso2-iso1-cases I (lconst , x) hs = refl\n proof-iso2-iso1-cases I (lprod , ( D , D' )) ( p , q ) = cong2 (prodl I) p q \n proof-iso2-iso1-cases I (lpi , ( S , T )) hs = cong (pil I S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-cases I (lsigma , ( S , T )) hs = cong (sigmal I S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box Unit (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-iso2-iso1-casesW I Void = proof-iso2-iso1-cases I\n \n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d186c9c55549e759484d079ec26e88aa5dd4bfef","subject":"Removed general hints.","message":"Removed general hints.\n\nIgnore-this: 2f7102a575b4c2fdb80e99fde9cc97f0\n\ndarcs-hash:20100710142946-3bd4e-567a84c7ee48f426f69c53a0b41c8fb40007e53b.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Data\/Bool\/Properties.agda","new_file":"LTC\/Data\/Bool\/Properties.agda","new_contents":"------------------------------------------------------------------------------\n-- The booleans properties\n------------------------------------------------------------------------------\n\nmodule LTC.Data.Bool.Properties where\n\nopen import LTC.Minimal\nopen import LTC.Data.Bool\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.Properties using ( \u2264-SS ; S\u22700 )\n\n------------------------------------------------------------------------------\n-- Basic properties.\n\n&&-Bool : {b\u2081 b\u2082 : D} \u2192 Bool b\u2081 \u2192 Bool b\u2082 \u2192 Bool (b\u2081 && b\u2082)\n&&-Bool tB tB = prf\n where\n postulate prf : Bool (true && true)\n {-# ATP prove prf #-}\n&&-Bool tB fB = prf\n where\n postulate prf : Bool (true && false)\n {-# ATP prove prf #-}\n&&-Bool fB tB = prf\n where\n postulate prf : Bool (false && true)\n {-# ATP prove prf #-}\n&&-Bool fB fB = prf\n where\n postulate prf : Bool (false && false)\n {-# ATP prove prf #-}\n\nx&&y\u2261true\u2192x\u2261true : {b\u2081 b\u2082 : D} \u2192 Bool b\u2081 \u2192 Bool b\u2082 \u2192 b\u2081 && b\u2082 \u2261 true \u2192\n b\u2081 \u2261 true\nx&&y\u2261true\u2192x\u2261true tB _ _ = refl\nx&&y\u2261true\u2192x\u2261true fB tB prf = \u22a5-elim (true\u2260false (trans (sym prf) &&-ft))\nx&&y\u2261true\u2192x\u2261true fB fB prf = \u22a5-elim (true\u2260false (trans (sym prf) &&-ff))\n\nx&&y\u2261true\u2192y\u2261true : {b\u2081 b\u2082 : D} \u2192 Bool b\u2081 \u2192 Bool b\u2082 \u2192 b\u2081 && b\u2082 \u2261 true \u2192\n b\u2082 \u2261 true\nx&&y\u2261true\u2192y\u2261true _ tB _ = refl\nx&&y\u2261true\u2192y\u2261true tB fB prf = \u22a5-elim (true\u2260false (trans (sym prf) &&-tf))\nx&&y\u2261true\u2192y\u2261true fB fB prf = \u22a5-elim (true\u2260false (trans (sym prf) &&-ff))\n\n------------------------------------------------------------------------------\n-- Properties with inequalities\n\n\u2264-Bool : {m n : D} \u2192 N m \u2192 N n \u2192 Bool (m \u2264 n)\n\u2264-Bool {n = n} zN Nn = prf\n where\n postulate prf : Bool (zero \u2264 n)\n {-# ATP prove prf #-}\n\u2264-Bool (sN {m} Nm) zN = prf\n where\n postulate prf : Bool (succ m \u2264 zero)\n {-# ATP prove prf S\u22700 #-}\n\u2264-Bool (sN {m} Nm) (sN {n} Nn) = prf (\u2264-Bool Nm Nn)\n where\n postulate prf : Bool (m \u2264 n) \u2192 Bool (succ m \u2264 succ n)\n {-# ATP prove prf #-}\n","old_contents":"------------------------------------------------------------------------------\n-- The booleans properties\n------------------------------------------------------------------------------\n\nmodule LTC.Data.Bool.Properties where\n\nopen import LTC.Minimal\nopen import LTC.Data.Bool\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.Properties using ( \u2264-SS ; S\u22700 )\n\n------------------------------------------------------------------------------\n-- Basic properties.\n\n-- This function is a general hint.\n&&-Bool : {b\u2081 b\u2082 : D} \u2192 Bool b\u2081 \u2192 Bool b\u2082 \u2192 Bool (b\u2081 && b\u2082)\n&&-Bool tB tB = prf\n where\n postulate prf : Bool (true && true)\n {-# ATP prove prf #-}\n&&-Bool tB fB = prf\n where\n postulate prf : Bool (true && false)\n {-# ATP prove prf #-}\n&&-Bool fB tB = prf\n where\n postulate prf : Bool (false && true)\n {-# ATP prove prf #-}\n&&-Bool fB fB = prf\n where\n postulate prf : Bool (false && false)\n {-# ATP prove prf #-}\n{-# ATP hint &&-Bool #-}\n\nx&&y\u2261true\u2192x\u2261true : {b\u2081 b\u2082 : D} \u2192 Bool b\u2081 \u2192 Bool b\u2082 \u2192 b\u2081 && b\u2082 \u2261 true \u2192\n b\u2081 \u2261 true\nx&&y\u2261true\u2192x\u2261true tB _ _ = refl\nx&&y\u2261true\u2192x\u2261true fB tB prf = \u22a5-elim (true\u2260false (trans (sym prf) &&-ft))\nx&&y\u2261true\u2192x\u2261true fB fB prf = \u22a5-elim (true\u2260false (trans (sym prf) &&-ff))\n\nx&&y\u2261true\u2192y\u2261true : {b\u2081 b\u2082 : D} \u2192 Bool b\u2081 \u2192 Bool b\u2082 \u2192 b\u2081 && b\u2082 \u2261 true \u2192\n b\u2082 \u2261 true\nx&&y\u2261true\u2192y\u2261true _ tB _ = refl\nx&&y\u2261true\u2192y\u2261true tB fB prf = \u22a5-elim (true\u2260false (trans (sym prf) &&-tf))\nx&&y\u2261true\u2192y\u2261true fB fB prf = \u22a5-elim (true\u2260false (trans (sym prf) &&-ff))\n\n------------------------------------------------------------------------------\n-- Properties with inequalities\n\n-- This function is a general hint.\n\u2264-Bool : {m n : D} \u2192 N m \u2192 N n \u2192 Bool (m \u2264 n)\n\u2264-Bool {n = n} zN Nn = prf\n where\n postulate prf : Bool (zero \u2264 n)\n {-# ATP prove prf #-}\n\u2264-Bool (sN {m} Nm) zN = prf\n where\n postulate prf : Bool (succ m \u2264 zero)\n {-# ATP prove prf S\u22700 #-}\n\u2264-Bool (sN {m} Nm) (sN {n} Nn) = prf (\u2264-Bool Nm Nn)\n where\n postulate prf : Bool (m \u2264 n) \u2192 Bool (succ m \u2264 succ n)\n {-# ATP prove prf #-}\n{-# ATP hint \u2264-Bool #-}\n\n\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"6fe83e1d6365cf25bee0bdc823a5212aa8024ac7","subject":"Modified a test.","message":"Modified a test.\n\nIgnore-this: e571f29238bfd5b15fa8eb93e8a0edc4\n\ndarcs-hash:20100803142440-3bd4e-1d6f95a0094c35a9ec7f3ad30fa19b594c8fe390.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Test\/Succeed\/DefinitionsInsideWhereClauses.agda","new_file":"Test\/Succeed\/DefinitionsInsideWhereClauses.agda","new_contents":"module Test.Succeed.DefinitionsInsideWhereClauses where\n\ninfixl 6 _+_\ninfix 4 _\u2261_\n\npostulate\n D : Set\n zero : D\n succ : D \u2192 D\n _\u2261_ : D \u2192 D \u2192 Set\n\n-- The LTC natural numbers type.\ndata N : D \u2192 Set where\n zN : N zero\n sN : {n : D} \u2192 N n \u2192 N (succ n)\n\n-- Induction principle for N (elimination rule).\nindN : (P : D \u2192 Set) \u2192\n P zero \u2192\n ({n : D} \u2192 N n \u2192 P n \u2192 P (succ n)) \u2192\n {n : D} \u2192 N n \u2192 P n\nindN P p0 h zN = p0\nindN P p0 h (sN Nn) = h Nn (indN P p0 h Nn)\n\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : (d : D) \u2192 zero + d \u2261 d\n +-Sx : (d e : D) \u2192 succ d + e \u2261 succ (d + e)\n{-# ATP axiom +-0x #-}\n{-# ATP axiom +-Sx #-}\n\n+-rightIdentity : {n : D} \u2192 N n \u2192 n + zero \u2261 n\n+-rightIdentity {n} Nn = indN P P0 iStep Nn\n where\n P : D \u2192 Set\n P i = i + zero \u2261 i\n {-# ATP definition P #-}\n\n postulate\n P0 : P zero\n {-# ATP prove P0 #-}\n\n postulate\n iStep : {i : D} \u2192 N i \u2192 P i \u2192 P (succ i)\n {-# ATP prove iStep #-}\n","old_contents":"module Test.Succeed.DefinitionsInsideWhereClauses where\n\ninfixl 6 _+_\ninfix 4 _\u2261_\n\npostulate\n D : Set\n zero : D\n succ : D \u2192 D\n _\u2261_ : D \u2192 D \u2192 Set\n\n-- The LTC natural numbers type.\ndata N : D \u2192 Set where\n zN : N zero\n sN : {n : D} \u2192 N n \u2192 N (succ n)\n\n-- Induction principle for N (elimination rule).\nindN : (P : D \u2192 Set) \u2192\n P zero \u2192\n ({n : D} \u2192 N n \u2192 P n \u2192 P (succ n)) \u2192\n {n : D} \u2192 N n \u2192 P n\nindN P p0 h zN = p0\nindN P p0 h (sN Nn) = h Nn (indN P p0 h Nn)\n\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : (d : D) \u2192 zero + d \u2261 d\n +-Sx : (d e : D) \u2192 succ d + e \u2261 succ (d + e)\n{-# ATP axiom +-0x #-}\n{-# ATP axiom +-Sx #-}\n\n+-rightIdentity : {n : D} \u2192 N n \u2192 n + zero \u2261 n\n+-rightIdentity {n} Nn = indN Q Q0 iStep Nn\n where\n Q : D \u2192 Set\n Q i = i + zero \u2261 i\n {-# ATP definition Q #-}\n\n postulate\n Q0 : Q zero\n {-# ATP prove Q0 #-}\n\n postulate\n iStep : {i : D} \u2192 N i \u2192 Q i \u2192 Q (succ i)\n {-# ATP prove iStep #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"4b8af6242399a26626161c4b8f6ed956f115c0d6","subject":"Avoid const in favor of lift-\u03b7-const.","message":"Avoid const in favor of lift-\u03b7-const.\n\nThis commit prepares for changing the type of const.\n\nOld-commit-hash: d7ebdbeb22d70fa60c6ce08ebf4832f56797b59a\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Language\/Atlas.agda","new_file":"Syntax\/Language\/Atlas.agda","new_contents":"module Syntax.Language.Atlas where\n\n-- Base types of the calculus Atlas\n-- to be used with Plotkin-style language description\n--\n-- Atlas supports maps with neutral elements. The change type to\n-- `Map \u03ba \u03b9` is `Map \u03ba \u0394\u03b9`, where \u0394\u03b9 is the change type of the\n-- ground type \u03b9. Such a change to maps can support insertions\n-- and deletions as well: Inserting `k -> v` means mapping `k` to\n-- the change from the neutral element to `v`, and deleting\n-- `k -> v` means mapping `k` to the change from `v` to the\n-- neutral element.\n\ndata Atlas-type : Set where\n Bool : Atlas-type\n Map : (\u03ba : Atlas-type) (\u03b9 : Atlas-type) \u2192 Atlas-type\n\nopen import Syntax.Type.Plotkin Atlas-type\n\ndata Atlas-const : Type \u2192 Set where\n true : Atlas-const\n (base Bool)\n\n false : Atlas-const\n (base Bool)\n\n xor : Atlas-const\n (base Bool \u21d2 base Bool \u21d2 base Bool)\n\n empty : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base (Map \u03ba \u03b9))\n\n -- `update key val my-map` would\n -- - insert if `key` is not present in `my-map`\n -- - delete if `val` is the neutral element\n -- - make an update otherwise\n\n update : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base \u03ba \u21d2 base \u03b9 \u21d2 base (Map \u03ba \u03b9) \u21d2 base (Map \u03ba \u03b9))\n\n lookup : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base \u03ba \u21d2 base (Map \u03ba \u03b9) \u21d2 base \u03b9)\n\n -- Model of zip = Haskell Data.List.zipWith\n --\n -- zipWith :: (a \u2192 b \u2192 c) \u2192 [a] \u2192 [b] \u2192 [c]\n --\n -- Behavioral difference: all key-value pairs present\n -- in *either* (m\u2081 : Map \u03ba a) *or* (m\u2082 : Map \u03ba b) will\n -- be iterated over. Neutral element of type `a` or `b`\n -- will be supplied if the key is missing in the\n -- corresponding map.\n\n zip : \u2200 {\u03ba a b c : Atlas-type} \u2192 Atlas-const\n ((base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u21d2\n base (Map \u03ba a) \u21d2 base (Map \u03ba b) \u21d2 base (Map \u03ba c))\n\n -- Model of fold = Haskell Data.Map.foldWithKey\n --\n -- foldWithKey :: (k \u2192 a \u2192 b \u2192 b) \u2192 b \u2192 Map k a \u2192 b\n\n fold : \u2200 {\u03ba a b : Atlas-type} \u2192 Atlas-const\n ((base \u03ba \u21d2 base a \u21d2 base b \u21d2 base b) \u21d2\n base b \u21d2 base (Map \u03ba a) \u21d2 base b)\n\nAtlas-\u0394base : Atlas-type \u2192 Atlas-type\n-- change to a boolean is a xor-rand\nAtlas-\u0394base Bool = Bool\n-- change to a map is change to its values\nAtlas-\u0394base (Map key val) = (Map key (Atlas-\u0394base val))\n\nAtlas-\u0394type : Type \u2192 Type\nAtlas-\u0394type = lift-\u0394type\u2080 Atlas-\u0394base\n\nopen import Syntax.Context {Type}\nopen import Syntax.Term.Plotkin {Atlas-type} {Atlas-const}\n\n-- Shorthands of constants\ntrue! : \u2200 {\u0393} \u2192\n Term \u0393 (base Bool)\ntrue! = lift-\u03b7-const true\n\nfalse! : \u2200 {\u0393} \u2192\n Term \u0393 (base Bool)\nfalse! = lift-\u03b7-const false\n\nxor! : \u2200 {\u0393} \u2192\n Term \u0393 (base Bool) \u2192 Term \u0393 (base Bool) \u2192\n Term \u0393 (base Bool)\nxor! = lift-\u03b7-const xor\n\nempty! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base (Map \u03ba \u03b9))\nempty! = lift-\u03b7-const empty\n\nupdate! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base \u03b9) \u2192\n Term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Term \u0393 (base (Map \u03ba \u03b9))\nupdate! = lift-\u03b7-const update\n\nlookup! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Term \u0393 (base \u03b9)\nlookup! = lift-\u03b7-const lookup\n\nzip! : \u2200 {\u03ba a b c \u0393} \u2192\n Term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u2192\n Term \u0393 (base (Map \u03ba a)) \u2192 Term \u0393 (base (Map \u03ba b)) \u2192\n Term \u0393 (base (Map \u03ba c))\nzip! = lift-\u03b7-const zip\n\nfold! : \u2200 {\u03ba a b \u0393} \u2192\n Term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base b) \u2192\n Term \u0393 (base b) \u2192 Term \u0393 (base (Map \u03ba a)) \u2192\n Term \u0393 (base b)\nfold! = lift-\u03b7-const fold\n\n-- Every base type has a known nil-change.\n-- The nil-change of \u03b9 is also the neutral element of Map \u03ba \u0394\u03b9.\n\nneutral : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const (base \u03b9)\nneutral {Bool} = false\nneutral {Map \u03ba \u03b9} = empty {\u03ba} {\u03b9}\n\nneutral-term : \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9)\nneutral-term {Bool} = lift-\u03b7-const (neutral {Bool})\nneutral-term {Map \u03ba \u03b9} = lift-\u03b7-const (neutral {Map \u03ba \u03b9})\n\nnil-const : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const (base (Atlas-\u0394base \u03b9))\nnil-const {\u03b9} = neutral {Atlas-\u0394base \u03b9}\n\nnil-term : \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base (Atlas-\u0394base \u03b9))\nnil-term {Bool} = lift-\u03b7-const (nil-const {Bool})\nnil-term {Map \u03ba \u03b9} = lift-\u03b7-const (nil-const {Map \u03ba \u03b9})\n\n-- Nonfunctional products can be encoded.\n-- The incremental behavior of products thus encoded is weird:\n-- \u0394(\u03b1 \u00d7 \u03b2) = \u03b1 \u00d7 \u0394\u03b2\nPair : Atlas-type \u2192 Atlas-type \u2192 Atlas-type\nPair \u03b1 \u03b2 = Map \u03b1 \u03b2\n\npair : \u2200 {\u03b1 \u03b2 \u0393} \u2192\n Term \u0393 (base \u03b1) \u2192 Term \u0393 (base \u03b2) \u2192\n Term \u0393 (base (Pair \u03b1 \u03b2))\npair s t = update! s t empty!\n\npair-term : \u2200 {\u03b1 \u03b2 \u0393} \u2192\n Term \u0393 (base \u03b1 \u21d2 base \u03b2 \u21d2 base (Pair \u03b1 \u03b2))\npair-term = abs (abs (pair (var (that this)) (var this)))\n\nuncurry : \u2200 {\u03b1 \u03b2 \u03b3 \u0393} \u2192\n Term \u0393 (base \u03b1 \u21d2 base \u03b2 \u21d2 base \u03b3) \u2192\n Term \u0393 (base (Pair \u03b1 \u03b2)) \u2192\n Term \u0393 (base \u03b3)\nuncurry f p =\n let\n a = var (that (that this))\n b = var (that this)\n g = abs (abs (abs (app\u2082 (weaken\u2083 f) a b)))\n in\n fold! g neutral-term p\n\nzip-pair : \u2200 {\u03ba a b \u0393} \u2192\n Term \u0393 (base (Map \u03ba a)) \u2192 Term \u0393 (base (Map \u03ba b)) \u2192\n Term \u0393 (base (Map \u03ba (Pair a b)))\nzip-pair = zip! (abs pair-term)\n\n-- diff-term and apply-term\n\n-- b\u2080 \u229d b\u2081 = b\u2080 xor b\u2081\n-- m\u2080 \u229d m\u2081 = zip _\u229d_ m\u2080 m\u2081\n\nAtlas-diff : \u2200 {\u03b9 \u0393} \u2192\n Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 Atlas-\u0394type (base \u03b9))\nAtlas-diff {Bool} = abs (abs (lift-\u03b7-const xor (var (that this)) (var this)))\nAtlas-diff {Map \u03ba \u03b9} = abs (abs (lift-\u03b7-const zip (abs Atlas-diff) (var (that this)) (var this)))\n\n-- b \u2295 \u0394b = b xor \u0394b\n-- m \u2295 \u0394m = zip _\u2295_ m \u0394m\n\nAtlas-apply : \u2200 {\u03b9 \u0393} \u2192\n Term \u0393 (Atlas-\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\nAtlas-apply {Bool} = abs (abs (lift-\u03b7-const xor (var (that this)) (var this)))\nAtlas-apply {Map \u03ba \u03b9} = abs (abs (lift-\u03b7-const zip (abs Atlas-apply) (var (that this)) (var this)))\n\n-- Shorthands for working with diff-term and apply-term\n\ndiff : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 (Atlas-\u0394type \u03c4)\ndiff = app\u2082 (lift-diff Atlas-diff Atlas-apply)\n\napply : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (Atlas-\u0394type \u03c4) \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\napply = app\u2082 (lift-apply Atlas-diff Atlas-apply)\n\n-- Shorthands for creating changes corresponding to\n-- insertion\/deletion.\n\ninsert : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base \u03b9) \u2192\n -- last argument is the change accumulator\n Term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ndelete : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base \u03b9) \u2192\n Term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ninsert k v acc = update! k (diff v neutral-term) acc\ndelete k v acc = update! k (diff neutral-term v) acc\n\n-- Shorthand for 4-way zip\nzip4! : \u2200 {\u03ba a b c d e \u0393} \u2192\n let\n t:_ = \u03bb \u03b9 \u2192 Term \u0393 (base \u03b9)\n in\n Term \u0393\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c \u21d2 base d \u21d2 base e) \u2192\n t: Map \u03ba a \u2192 t: Map \u03ba b \u2192 t: Map \u03ba c \u2192 t: Map \u03ba d \u2192 t: Map \u03ba e\n\n-- zip\u2084 f m\u2081 m\u2082 m\u2083 m\u2084 =\n-- zip (\u03bb k p\u2081\u2082 p\u2083\u2084 \u2192 uncurry (\u03bb v\u2081 v\u2082 \u2192 uncurry (\u03bb v\u2083 v\u2084 \u2192\n-- f k v\u2081 v\u2082 v\u2083 v\u2084)\n-- p\u2083\u2084) p\u2081\u2082)\n-- (zip-pair m\u2081 m\u2082) (zip-pair m\u2083 m\u2084)\n\nzip4! f m\u2081 m\u2082 m\u2083 m\u2084 =\n let\n f\u2032 = weaken\u2083 (weaken\u2083 (weaken\u2081 f))\n k = var (that (that (that (that (that (that this))))))\n p\u2081\u2082 = var (that this)\n p\u2083\u2084 = var (that (that this))\n v\u2081 = var (that (that (that this)))\n v\u2082 = var (that (that this))\n v\u2083 = var (that this)\n v\u2084 = var this\n g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs\n (app\u2085 f\u2032 k v\u2081 v\u2082 v\u2083 v\u2084)))\n p\u2083\u2084))) p\u2081\u2082)))\n in\n zip! g (zip-pair m\u2081 m\u2082) (zip-pair m\u2083 m\u2084)\n\n-- Type signature of Atlas-\u0394const is boilerplate.\nAtlas-\u0394const : \u2200 {\u0393 \u03c4} \u2192 (c : Atlas-const \u03c4) \u2192\n Term \u0393 (Atlas-\u0394type \u03c4)\n\nAtlas-\u0394const true = false!\nAtlas-\u0394const false = false!\n\n-- \u0394xor = \u03bb x \u0394x y \u0394y \u2192 \u0394x xor \u0394y\nAtlas-\u0394const xor =\n let\n \u0394x = var (that (that this))\n \u0394y = var this\n in abs (abs (abs (abs (xor! \u0394x \u0394y))))\n\nAtlas-\u0394const empty = empty!\n\n-- If k \u2295 \u0394k \u2261 k, then\n-- \u0394update k \u0394k v \u0394v m \u0394m = update k \u0394v \u0394m\n-- else it is a deletion followed by insertion:\n-- \u0394update k \u0394k v \u0394v m \u0394m =\n-- insert (k \u2295 \u0394k) (v \u2295 \u0394v) (delete k v \u0394m)\n--\n-- We implement the else-branch only for the moment.\nAtlas-\u0394const update =\n let\n k = var (that (that (that (that (that this)))))\n \u0394k = var (that (that (that (that this))))\n v = var (that (that (that this)))\n \u0394v = var (that (that this))\n -- m = var (that this) -- unused parameter\n \u0394m = var this\n in\n abs (abs (abs (abs (abs (abs\n (insert (apply \u0394k k) (apply \u0394v v)\n (delete k v \u0394m)))))))\n\n-- \u0394lookup k \u0394k m \u0394m | true? (k \u2295 \u0394k \u2261 k)\n-- ... | true = lookup k \u0394m\n-- ... | false =\n-- (lookup (k \u2295 \u0394k) m \u2295 lookup (k \u2295 \u0394k) \u0394m)\n-- \u229d lookup k m\n--\n-- Only the false-branch is implemented.\nAtlas-\u0394const lookup =\n let\n k = var (that (that (that this)))\n \u0394k = var (that (that this))\n m = var (that this)\n \u0394m = var this\n k\u2032 = apply \u0394k k\n in\n abs (abs (abs (abs\n (diff (apply (lookup! k\u2032 \u0394m) (lookup! k\u2032 m))\n (lookup! k m)))))\n\n-- \u0394zip f \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082 | true? (f \u2295 \u0394f \u2261 f)\n--\n-- ... | true =\n-- zip (\u03bb k \u0394v\u2081 \u0394v\u2082 \u2192 \u0394f (lookup k m\u2081) \u0394v\u2081 (lookup k m\u2082) \u0394v\u2082)\n-- \u0394m\u2081 \u0394m\u2082\n--\n-- ... | false = zip\u2084 \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082\n--\n-- we implement the false-branch for the moment.\nAtlas-\u0394const zip =\n let\n \u0394f = var (that (that (that (that this))))\n m\u2081 = var (that (that (that this)))\n \u0394m\u2081 = var (that (that this))\n m\u2082 = var (that this)\n \u0394m\u2082 = var this\n g = abs (app\u2082 (weaken\u2081 \u0394f) (var this) nil-term)\n in\n abs (abs (abs (abs (abs (abs (zip4! g m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082))))))\n\n-- \u0394fold f \u0394f z \u0394z m \u0394m = proj\u2082\n-- (fold (\u03bb k [a,\u0394a] [b,\u0394b] \u2192\n-- uncurry (\u03bb a \u0394a \u2192 uncurry (\u03bb b \u0394b \u2192\n-- pair (f k a b) (\u0394f k nil a \u0394a b \u0394b))\n-- [b,\u0394b]) [a,\u0394a])\n-- (pair z \u0394z)\n-- (zip-pair m \u0394m))\n--\n-- \u0394fold is efficient only if evaluation is lazy and \u0394f is\n-- self-maintainable: it doesn't look at the argument\n-- (b = fold f k a b\u2080) at all.\nAtlas-\u0394const (fold {\u03ba} {\u03b1} {\u03b2}) =\n let -- TODO (tedius): write weaken\u2087\n f = weaken\u2083 (weaken\u2083 (weaken\u2081\n (var (that (that (that (that (that this))))))))\n \u0394f = weaken\u2083 (weaken\u2083 (weaken\u2081\n (var (that (that (that (that this)))))))\n z = var (that (that (that this)))\n \u0394z = var (that (that this))\n m = var (that this)\n \u0394m = var this\n k = weaken\u2083 (weaken\u2081 (var (that (that this))))\n [a,\u0394a] = var (that this)\n [b,\u0394b] = var this\n a = var (that (that (that this)))\n \u0394a = var (that (that this))\n b = var (that this)\n \u0394b = var this\n g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs\n (pair (app\u2083 f k a b)\n (app\u2086 \u0394f k nil-term a \u0394a b \u0394b))))\n (weaken\u2082 [b,\u0394b])))) [a,\u0394a])))\n proj\u2082 = uncurry (abs (abs (var this)))\n in\n abs (abs (abs (abs (abs (abs\n (proj\u2082 (fold! g (pair z \u0394z) (zip-pair m \u0394m))))))))\n\nopen import Syntax.Language.Calculus\n\nAtlas = calculus-with\n Atlas-type\n Atlas-const\n Atlas-\u0394type\n Atlas-\u0394const\n","old_contents":"module Syntax.Language.Atlas where\n\n-- Base types of the calculus Atlas\n-- to be used with Plotkin-style language description\n--\n-- Atlas supports maps with neutral elements. The change type to\n-- `Map \u03ba \u03b9` is `Map \u03ba \u0394\u03b9`, where \u0394\u03b9 is the change type of the\n-- ground type \u03b9. Such a change to maps can support insertions\n-- and deletions as well: Inserting `k -> v` means mapping `k` to\n-- the change from the neutral element to `v`, and deleting\n-- `k -> v` means mapping `k` to the change from `v` to the\n-- neutral element.\n\ndata Atlas-type : Set where\n Bool : Atlas-type\n Map : (\u03ba : Atlas-type) (\u03b9 : Atlas-type) \u2192 Atlas-type\n\nopen import Syntax.Type.Plotkin Atlas-type\n\ndata Atlas-const : Type \u2192 Set where\n true : Atlas-const\n (base Bool)\n\n false : Atlas-const\n (base Bool)\n\n xor : Atlas-const\n (base Bool \u21d2 base Bool \u21d2 base Bool)\n\n empty : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base (Map \u03ba \u03b9))\n\n -- `update key val my-map` would\n -- - insert if `key` is not present in `my-map`\n -- - delete if `val` is the neutral element\n -- - make an update otherwise\n\n update : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base \u03ba \u21d2 base \u03b9 \u21d2 base (Map \u03ba \u03b9) \u21d2 base (Map \u03ba \u03b9))\n\n lookup : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base \u03ba \u21d2 base (Map \u03ba \u03b9) \u21d2 base \u03b9)\n\n -- Model of zip = Haskell Data.List.zipWith\n --\n -- zipWith :: (a \u2192 b \u2192 c) \u2192 [a] \u2192 [b] \u2192 [c]\n --\n -- Behavioral difference: all key-value pairs present\n -- in *either* (m\u2081 : Map \u03ba a) *or* (m\u2082 : Map \u03ba b) will\n -- be iterated over. Neutral element of type `a` or `b`\n -- will be supplied if the key is missing in the\n -- corresponding map.\n\n zip : \u2200 {\u03ba a b c : Atlas-type} \u2192 Atlas-const\n ((base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u21d2\n base (Map \u03ba a) \u21d2 base (Map \u03ba b) \u21d2 base (Map \u03ba c))\n\n -- Model of fold = Haskell Data.Map.foldWithKey\n --\n -- foldWithKey :: (k \u2192 a \u2192 b \u2192 b) \u2192 b \u2192 Map k a \u2192 b\n\n fold : \u2200 {\u03ba a b : Atlas-type} \u2192 Atlas-const\n ((base \u03ba \u21d2 base a \u21d2 base b \u21d2 base b) \u21d2\n base b \u21d2 base (Map \u03ba a) \u21d2 base b)\n\nAtlas-\u0394base : Atlas-type \u2192 Atlas-type\n-- change to a boolean is a xor-rand\nAtlas-\u0394base Bool = Bool\n-- change to a map is change to its values\nAtlas-\u0394base (Map key val) = (Map key (Atlas-\u0394base val))\n\nAtlas-\u0394type : Type \u2192 Type\nAtlas-\u0394type = lift-\u0394type\u2080 Atlas-\u0394base\n\nopen import Syntax.Context {Type}\nopen import Syntax.Term.Plotkin {Atlas-type} {Atlas-const}\n\n-- Shorthands of constants\ntrue! : \u2200 {\u0393} \u2192\n Term \u0393 (base Bool)\ntrue! = lift-\u03b7-const true\n\nfalse! : \u2200 {\u0393} \u2192\n Term \u0393 (base Bool)\nfalse! = lift-\u03b7-const false\n\nxor! : \u2200 {\u0393} \u2192\n Term \u0393 (base Bool) \u2192 Term \u0393 (base Bool) \u2192\n Term \u0393 (base Bool)\nxor! = lift-\u03b7-const xor\n\nempty! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base (Map \u03ba \u03b9))\nempty! = lift-\u03b7-const empty\n\nupdate! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base \u03b9) \u2192\n Term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Term \u0393 (base (Map \u03ba \u03b9))\nupdate! = lift-\u03b7-const update\n\nlookup! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Term \u0393 (base \u03b9)\nlookup! = lift-\u03b7-const lookup\n\nzip! : \u2200 {\u03ba a b c \u0393} \u2192\n Term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u2192\n Term \u0393 (base (Map \u03ba a)) \u2192 Term \u0393 (base (Map \u03ba b)) \u2192\n Term \u0393 (base (Map \u03ba c))\nzip! = lift-\u03b7-const zip\n\nfold! : \u2200 {\u03ba a b \u0393} \u2192\n Term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base b) \u2192\n Term \u0393 (base b) \u2192 Term \u0393 (base (Map \u03ba a)) \u2192\n Term \u0393 (base b)\nfold! = lift-\u03b7-const fold\n\n-- Every base type has a known nil-change.\n-- The nil-change of \u03b9 is also the neutral element of Map \u03ba \u0394\u03b9.\n\nneutral : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const (base \u03b9)\nneutral {Bool} = false\nneutral {Map \u03ba \u03b9} = empty {\u03ba} {\u03b9}\n\nneutral-term : \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9)\nneutral-term {Bool} = const (neutral {Bool})\nneutral-term {Map \u03ba \u03b9} = const (neutral {Map \u03ba \u03b9})\n\nnil-const : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const (base (Atlas-\u0394base \u03b9))\nnil-const {\u03b9} = neutral {Atlas-\u0394base \u03b9}\n\nnil-term : \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base (Atlas-\u0394base \u03b9))\nnil-term {Bool} = const (nil-const {Bool})\nnil-term {Map \u03ba \u03b9} = const (nil-const {Map \u03ba \u03b9})\n\n-- Nonfunctional products can be encoded.\n-- The incremental behavior of products thus encoded is weird:\n-- \u0394(\u03b1 \u00d7 \u03b2) = \u03b1 \u00d7 \u0394\u03b2\nPair : Atlas-type \u2192 Atlas-type \u2192 Atlas-type\nPair \u03b1 \u03b2 = Map \u03b1 \u03b2\n\npair : \u2200 {\u03b1 \u03b2 \u0393} \u2192\n Term \u0393 (base \u03b1) \u2192 Term \u0393 (base \u03b2) \u2192\n Term \u0393 (base (Pair \u03b1 \u03b2))\npair s t = update! s t empty!\n\npair-term : \u2200 {\u03b1 \u03b2 \u0393} \u2192\n Term \u0393 (base \u03b1 \u21d2 base \u03b2 \u21d2 base (Pair \u03b1 \u03b2))\npair-term = abs (abs (pair (var (that this)) (var this)))\n\nuncurry : \u2200 {\u03b1 \u03b2 \u03b3 \u0393} \u2192\n Term \u0393 (base \u03b1 \u21d2 base \u03b2 \u21d2 base \u03b3) \u2192\n Term \u0393 (base (Pair \u03b1 \u03b2)) \u2192\n Term \u0393 (base \u03b3)\nuncurry f p =\n let\n a = var (that (that this))\n b = var (that this)\n g = abs (abs (abs (app\u2082 (weaken\u2083 f) a b)))\n in\n fold! g neutral-term p\n\nzip-pair : \u2200 {\u03ba a b \u0393} \u2192\n Term \u0393 (base (Map \u03ba a)) \u2192 Term \u0393 (base (Map \u03ba b)) \u2192\n Term \u0393 (base (Map \u03ba (Pair a b)))\nzip-pair = zip! (abs pair-term)\n\n-- diff-term and apply-term\n\n-- b\u2080 \u229d b\u2081 = b\u2080 xor b\u2081\n-- m\u2080 \u229d m\u2081 = zip _\u229d_ m\u2080 m\u2081\n\nAtlas-diff : \u2200 {\u03b9 \u0393} \u2192\n Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 Atlas-\u0394type (base \u03b9))\nAtlas-diff {Bool} = const xor\nAtlas-diff {Map \u03ba \u03b9} = app (const zip) (abs Atlas-diff)\n\n-- b \u2295 \u0394b = b xor \u0394b\n-- m \u2295 \u0394m = zip _\u2295_ m \u0394m\n\nAtlas-apply : \u2200 {\u03b9 \u0393} \u2192\n Term \u0393 (Atlas-\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\nAtlas-apply {Bool} = const xor\nAtlas-apply {Map \u03ba \u03b9} = app (const zip) (abs Atlas-apply)\n\n-- Shorthands for working with diff-term and apply-term\n\ndiff : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 (Atlas-\u0394type \u03c4)\ndiff = app\u2082 (lift-diff Atlas-diff Atlas-apply)\n\napply : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (Atlas-\u0394type \u03c4) \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\napply = app\u2082 (lift-apply Atlas-diff Atlas-apply)\n\n-- Shorthands for creating changes corresponding to\n-- insertion\/deletion.\n\ninsert : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base \u03b9) \u2192\n -- last argument is the change accumulator\n Term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ndelete : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base \u03b9) \u2192\n Term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ninsert k v acc = update! k (diff v neutral-term) acc\ndelete k v acc = update! k (diff neutral-term v) acc\n\n-- Shorthand for 4-way zip\nzip4! : \u2200 {\u03ba a b c d e \u0393} \u2192\n let\n t:_ = \u03bb \u03b9 \u2192 Term \u0393 (base \u03b9)\n in\n Term \u0393\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c \u21d2 base d \u21d2 base e) \u2192\n t: Map \u03ba a \u2192 t: Map \u03ba b \u2192 t: Map \u03ba c \u2192 t: Map \u03ba d \u2192 t: Map \u03ba e\n\n-- zip\u2084 f m\u2081 m\u2082 m\u2083 m\u2084 =\n-- zip (\u03bb k p\u2081\u2082 p\u2083\u2084 \u2192 uncurry (\u03bb v\u2081 v\u2082 \u2192 uncurry (\u03bb v\u2083 v\u2084 \u2192\n-- f k v\u2081 v\u2082 v\u2083 v\u2084)\n-- p\u2083\u2084) p\u2081\u2082)\n-- (zip-pair m\u2081 m\u2082) (zip-pair m\u2083 m\u2084)\n\nzip4! f m\u2081 m\u2082 m\u2083 m\u2084 =\n let\n f\u2032 = weaken\u2083 (weaken\u2083 (weaken\u2081 f))\n k = var (that (that (that (that (that (that this))))))\n p\u2081\u2082 = var (that this)\n p\u2083\u2084 = var (that (that this))\n v\u2081 = var (that (that (that this)))\n v\u2082 = var (that (that this))\n v\u2083 = var (that this)\n v\u2084 = var this\n g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs\n (app\u2085 f\u2032 k v\u2081 v\u2082 v\u2083 v\u2084)))\n p\u2083\u2084))) p\u2081\u2082)))\n in\n zip! g (zip-pair m\u2081 m\u2082) (zip-pair m\u2083 m\u2084)\n\n-- Type signature of Atlas-\u0394const is boilerplate.\nAtlas-\u0394const : \u2200 {\u0393 \u03c4} \u2192 (c : Atlas-const \u03c4) \u2192\n Term \u0393 (Atlas-\u0394type \u03c4)\n\nAtlas-\u0394const true = false!\nAtlas-\u0394const false = false!\n\n-- \u0394xor = \u03bb x \u0394x y \u0394y \u2192 \u0394x xor \u0394y\nAtlas-\u0394const xor =\n let\n \u0394x = var (that (that this))\n \u0394y = var this\n in abs (abs (abs (abs (xor! \u0394x \u0394y))))\n\nAtlas-\u0394const empty = empty!\n\n-- If k \u2295 \u0394k \u2261 k, then\n-- \u0394update k \u0394k v \u0394v m \u0394m = update k \u0394v \u0394m\n-- else it is a deletion followed by insertion:\n-- \u0394update k \u0394k v \u0394v m \u0394m =\n-- insert (k \u2295 \u0394k) (v \u2295 \u0394v) (delete k v \u0394m)\n--\n-- We implement the else-branch only for the moment.\nAtlas-\u0394const update =\n let\n k = var (that (that (that (that (that this)))))\n \u0394k = var (that (that (that (that this))))\n v = var (that (that (that this)))\n \u0394v = var (that (that this))\n -- m = var (that this) -- unused parameter\n \u0394m = var this\n in\n abs (abs (abs (abs (abs (abs\n (insert (apply \u0394k k) (apply \u0394v v)\n (delete k v \u0394m)))))))\n\n-- \u0394lookup k \u0394k m \u0394m | true? (k \u2295 \u0394k \u2261 k)\n-- ... | true = lookup k \u0394m\n-- ... | false =\n-- (lookup (k \u2295 \u0394k) m \u2295 lookup (k \u2295 \u0394k) \u0394m)\n-- \u229d lookup k m\n--\n-- Only the false-branch is implemented.\nAtlas-\u0394const lookup =\n let\n k = var (that (that (that this)))\n \u0394k = var (that (that this))\n m = var (that this)\n \u0394m = var this\n k\u2032 = apply \u0394k k\n in\n abs (abs (abs (abs\n (diff (apply (lookup! k\u2032 \u0394m) (lookup! k\u2032 m))\n (lookup! k m)))))\n\n-- \u0394zip f \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082 | true? (f \u2295 \u0394f \u2261 f)\n--\n-- ... | true =\n-- zip (\u03bb k \u0394v\u2081 \u0394v\u2082 \u2192 \u0394f (lookup k m\u2081) \u0394v\u2081 (lookup k m\u2082) \u0394v\u2082)\n-- \u0394m\u2081 \u0394m\u2082\n--\n-- ... | false = zip\u2084 \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082\n--\n-- we implement the false-branch for the moment.\nAtlas-\u0394const zip =\n let\n \u0394f = var (that (that (that (that this))))\n m\u2081 = var (that (that (that this)))\n \u0394m\u2081 = var (that (that this))\n m\u2082 = var (that this)\n \u0394m\u2082 = var this\n g = abs (app\u2082 (weaken\u2081 \u0394f) (var this) nil-term)\n in\n abs (abs (abs (abs (abs (abs (zip4! g m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082))))))\n\n-- \u0394fold f \u0394f z \u0394z m \u0394m = proj\u2082\n-- (fold (\u03bb k [a,\u0394a] [b,\u0394b] \u2192\n-- uncurry (\u03bb a \u0394a \u2192 uncurry (\u03bb b \u0394b \u2192\n-- pair (f k a b) (\u0394f k nil a \u0394a b \u0394b))\n-- [b,\u0394b]) [a,\u0394a])\n-- (pair z \u0394z)\n-- (zip-pair m \u0394m))\n--\n-- \u0394fold is efficient only if evaluation is lazy and \u0394f is\n-- self-maintainable: it doesn't look at the argument\n-- (b = fold f k a b\u2080) at all.\nAtlas-\u0394const (fold {\u03ba} {\u03b1} {\u03b2}) =\n let -- TODO (tedius): write weaken\u2087\n f = weaken\u2083 (weaken\u2083 (weaken\u2081\n (var (that (that (that (that (that this))))))))\n \u0394f = weaken\u2083 (weaken\u2083 (weaken\u2081\n (var (that (that (that (that this)))))))\n z = var (that (that (that this)))\n \u0394z = var (that (that this))\n m = var (that this)\n \u0394m = var this\n k = weaken\u2083 (weaken\u2081 (var (that (that this))))\n [a,\u0394a] = var (that this)\n [b,\u0394b] = var this\n a = var (that (that (that this)))\n \u0394a = var (that (that this))\n b = var (that this)\n \u0394b = var this\n g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs\n (pair (app\u2083 f k a b)\n (app\u2086 \u0394f k nil-term a \u0394a b \u0394b))))\n (weaken\u2082 [b,\u0394b])))) [a,\u0394a])))\n proj\u2082 = uncurry (abs (abs (var this)))\n in\n abs (abs (abs (abs (abs (abs\n (proj\u2082 (fold! g (pair z \u0394z) (zip-pair m \u0394m))))))))\n\nopen import Syntax.Language.Calculus\n\nAtlas = calculus-with\n Atlas-type\n Atlas-const\n Atlas-\u0394type\n Atlas-\u0394const\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7dbb7878763fef3587923b0005e33195c0135014","subject":"Add tagged descriptions to model.","message":"Add tagged descriptions to model.\n\nThis lets elim just take a single \"Desc\" arg.\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/Examples\/PropositionalDesc.agda","new_file":"formalization\/agda\/Spire\/Examples\/PropositionalDesc.agda","new_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nmodule Spire.Examples.PropositionalDesc where\n\n----------------------------------------------------------------------\n\nelimEq : (A : Set) (x : A) (P : (y : A) \u2192 x \u2261 y \u2192 Set)\n \u2192 P x refl\n \u2192 (y : A) (p : x \u2261 y) \u2192 P y p\nelimEq A .x P prefl x refl = prefl\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nCases : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nCases [] P = \u22a4\nCases (l \u2237 E) P = P here \u00d7 Cases E \u03bb t \u2192 P (there t)\n\ncase : (E : Enum) (P : Tag E \u2192 Set) (cs : Cases E P) (t : Tag E) \u2192 P t\ncase (l \u2237 E) P (c , cs) here = c\ncase (l \u2237 E) P (c , cs) (there t) = case E (\u03bb t \u2192 P (there t)) cs t\n\nUncurriedCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedCases E P X = Cases E P \u2192 X\n\nCurriedCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedCases [] P X = X\nCurriedCases (l \u2237 E) P X = P here \u2192 CurriedCases E (\u03bb t \u2192 P (there t)) X\n\ncurryCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n (f : UncurriedCases E P X) \u2192 CurriedCases E P X\ncurryCases [] P X f = f tt\ncurryCases (l \u2237 E) P X f = \u03bb c \u2192 curryCases E (\u03bb t \u2192 P (there t)) X (\u03bb cs \u2192 f (c , cs))\n\nuncurryCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n (f : CurriedCases E P X) \u2192 UncurriedCases E P X\nuncurryCases [] P X x tt = x\nuncurryCases (l \u2237 E) P X f (c , cs) = uncurryCases E (\u03bb t \u2192 P (there t)) X (f c) cs\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n `End : (i : I) \u2192 Desc I\n `Rec : (i : I) (D : Desc I) \u2192 Desc I\n `Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n `RecFun : (A : Set) (B : A \u2192 I) (D : Desc I) \u2192 Desc I\n\nISet : Set \u2192 Set\u2081\nISet I = I \u2192 Set\n\nEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 ISet I\nEl I (`End j) X i = j \u2261 i\nEl I (`Rec j D) X i = X j \u00d7 El I D X i\nEl I (`Arg A B) X i = \u03a3 A (\u03bb a \u2192 El I (B a) X i)\nEl I (`RecFun A B D) X i = ((a : A) \u2192 X (B a)) \u00d7 El I D X i\n\nAll : (I : Set) (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El I D X i) \u2192 Set\nAll I (`End j) X P i q = \u22a4\nAll I (`Rec j D) X P i (x , xs) = P j x \u00d7 All I D X P i xs\nAll I (`Arg A B) X P i (a , b) = All I (B a) X P i b\nAll I (`RecFun A B D) X P i (f , xs) = ((a : A) \u2192 P (B a) (f a)) \u00d7 All I D X P i xs\n\ncaseD : (E : Enum) (I : Set) (cs : Cases E (\u03bb _ \u2192 Desc I)) (t : Tag E) \u2192 Desc I\ncaseD E I cs t = case E (\u03bb _ \u2192 Desc I) cs t\n\n----------------------------------------------------------------------\n\nTagDesc : (I : Set) \u2192 Set\nTagDesc I = \u03a3 Enum (\u03bb E \u2192 Cases E (\u03bb _ \u2192 Desc I))\n\ntoCase : (I : Set) (E,cs : TagDesc I) \u2192 Tag (proj\u2081 E,cs) \u2192 Desc I\ntoCase I (E , cs) = case E (\u03bb _ \u2192 Desc I) cs\n\ntoDesc : (I : Set) \u2192 TagDesc I \u2192 Desc I\ntoDesc I (E , cs) = `Arg (Tag E) (toCase I (E , cs))\n\n----------------------------------------------------------------------\n\nUncurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl I D X = {i : I} \u2192 El I D X i \u2192 X i\n\nCurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl I (`End i) X = X i\nCurriedEl I (`Rec j D) X = (x : X j) \u2192 CurriedEl I D X\nCurriedEl I (`Arg A B) X = (a : A) \u2192 CurriedEl I (B a) X\nCurriedEl I (`RecFun A B D) X = ((a : A) \u2192 X (B a)) \u2192 CurriedEl I D X\n\ncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : UncurriedEl I D X) \u2192 CurriedEl I D X\ncurryEl I (`End i) X cn = cn refl\ncurryEl I (`Rec i D) X cn = \u03bb x \u2192 curryEl I D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl I (`Arg A B) X cn = \u03bb a \u2192 curryEl I (B a) X (\u03bb xs \u2192 cn (a , xs))\ncurryEl I (`RecFun A B D) X cn = \u03bb f \u2192 curryEl I D X (\u03bb xs \u2192 cn (f , xs))\n\nuncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : CurriedEl I D X) \u2192 UncurriedEl I D X\nuncurryEl I (`End i) X cn refl = cn\nuncurryEl I (`Rec i D) X cn (x , xs) = uncurryEl I D X (cn x) xs\nuncurryEl I (`Arg A B) X cn (a , xs) = uncurryEl I (B a) X (cn a) xs\nuncurryEl I (`RecFun A B D) X cn (f , xs) = uncurryEl I D X (cn f) xs\n\ndata \u03bc (I : Set) (D : Desc I) : I \u2192 Set where\n con : UncurriedEl I D (\u03bc I D)\n\ncon2 : (I : Set) (D : Desc I) \u2192 CurriedEl I D (\u03bc I D)\ncon2 I D = curryEl I D (\u03bc I D) con\n\n----------------------------------------------------------------------\n\nUncurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : {i : I} \u2192 El I D X i \u2192 X i)\n \u2192 Set\nUncurriedAll I D X P cn =\n (i : I) (xs : El I D X i) \u2192 All I D X P i xs \u2192 P i (cn xs)\n\nCurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n \u2192 Set\nCurriedAll I (`End i) X P cn =\n P i (cn refl)\nCurriedAll I (`Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedAll I (`Arg A B) X P cn =\n (a : A) \u2192 CurriedAll I (B a) X P (\u03bb xs \u2192 cn (a , xs))\nCurriedAll I (`RecFun A B D) X P cn =\n (f : (a : A) \u2192 X (B a)) (ihf : (a : A) \u2192 P (B a) (f a)) \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (f , xs))\n\ncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : UncurriedAll I D X P cn)\n \u2192 CurriedAll I D X P cn\ncurryAll I (`End i) X P cn pf =\n pf i refl tt\ncurryAll I (`Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryAll I D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryAll I (`Arg A B) X P cn pf =\n \u03bb a \u2192 curryAll I (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\ncurryAll I (`RecFun A B D) X P cn pf =\n \u03bb f ihf \u2192 curryAll I D X P (\u03bb xs \u2192 cn (f , xs)) (\u03bb i xs ihs \u2192 pf i (f , xs) (ihf , ihs))\n\nuncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : CurriedAll I D X P cn)\n \u2192 UncurriedAll I D X P cn\nuncurryAll I (`End .i) X P cn pf i refl tt =\n pf\nuncurryAll I (`Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryAll I (`Arg A B) X P cn pf i (a , xs) ihs =\n uncurryAll I (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\nuncurryAll I (`RecFun A B D) X P cn pf i (f , xs) (ihf , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (f , ys)) (pf f ihf) i xs ihs\n\n----------------------------------------------------------------------\n\nind :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : UncurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\n\nhyps :\n (I : Set)\n (D\u2081 : Desc I)\n (P : (i : I) \u2192 \u03bc I D\u2081 i \u2192 Set)\n (pcon : UncurriedAll I D\u2081 (\u03bc I D\u2081) P con)\n (D\u2082 : Desc I)\n (i : I)\n (xs : El I D\u2082 (\u03bc I D\u2081) i)\n \u2192 All I D\u2082 (\u03bc I D\u2081) P i xs\n\nind I D P pcon i (con xs) = pcon i xs (hyps I D P pcon D i xs)\n\nhyps I D P pcon (`End j) i q = tt\nhyps I D P pcon (`Rec j A) i (x , xs) = ind I D P pcon j x , hyps I D P pcon A i xs\nhyps I D P pcon (`Arg A B) i (a , b) = hyps I D P pcon (B a) i b\nhyps I D P pcon (`RecFun A B E) i (f , xs) = (\u03bb a \u2192 ind I D P pcon (B a) (f a)) , hyps I D P pcon E i xs\n\n----------------------------------------------------------------------\n\nind2 :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : CurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\nind2 I D P pcon i x = ind I D P (uncurryAll I D (\u03bc I D) P con pcon) i x\n\nelim :\n (I : Set)\n (TD : TagDesc I)\n \u2192 let\n D = toDesc I TD\n E = proj\u2081 TD\n Cs = toCase I TD\n in (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n \u2192 let\n Q = \u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))\n X = (i : I) (x : \u03bc I D i) \u2192 P i x\n in UncurriedCases E Q X\nelim I TD P cs i x =\n let\n D = toDesc I TD\n E = proj\u2081 TD\n Cs = toCase I TD\n p = case E (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))) cs\n in ind2 I D P p i x\n\nelim2 :\n (I : Set)\n (TD : TagDesc I)\n \u2192 let\n D = toDesc I TD\n E = proj\u2081 TD\n Cs = toCase I TD\n in (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n \u2192 let\n Q = \u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))\n X = (i : I) (x : \u03bc I D i) \u2192 P i x\n in CurriedCases E Q X\nelim2 I TD P =\n let\n D = toDesc I TD\n E = proj\u2081 TD\n Cs = toCase I TD\n Q = \u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))\n X = (i : I) (x : \u03bc I D i) \u2192 P i x\n in curryCases E Q X (elim I TD P)\n\n----------------------------------------------------------------------\n\nmodule Sugared where\n\n data \u2115T : Set where `zero `suc : \u2115T\n data VecT : Set where `nil `cons : VecT\n\n \u2115D : Desc \u22a4\n \u2115D = `Arg \u2115T \u03bb\n { `zero \u2192 `End tt\n ; `suc \u2192 `Rec tt (`End tt)\n }\n\n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n\n zero : \u2115 tt\n zero = con (`zero , refl)\n\n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (`suc , n , refl)\n\n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg VecT \u03bb\n { `nil \u2192 `End zero\n ; `cons \u2192 `Arg (\u2115 tt) \u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n))\n }\n\n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n\n nil : (A : Set) \u2192 Vec A zero\n nil A = con (`nil , refl)\n\n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (`cons , n , x , xs , refl)\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 n\n ; tt (`suc , m , q) (ih , tt) n \u2192 suc (ih n)\n }\n )\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 zero\n ; tt (`suc , m , q) (ih , tt) n \u2192 add n (ih n)\n }\n )\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) ih n ys \u2192 ys\n ; .(con (`suc , m , refl)) (`cons , m , x , xs , refl) (ih , tt) n ys \u2192 cons A (add m n) x (ih n ys)\n }\n )\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) tt \u2192 nil A\n ; .(con (`suc , n , refl)) (`cons , n , xs , xss , refl) (ih , tt) \u2192 append A m xs (mult n m) ih\n }\n )\n\n----------------------------------------------------------------------\n\nmodule Desugared where\n\n \u2115T : Enum\n \u2115T = \"zero\" \u2237 \"suc\" \u2237 []\n \n VecT : Enum\n VecT = \"nil\" \u2237 \"cons\" \u2237 []\n\n \u2115TD : TagDesc \u22a4\n \u2115TD = \u2115T\n , `End tt\n , `Rec tt (`End tt)\n , tt\n \n \u2115C : Tag \u2115T \u2192 Desc \u22a4\n \u2115C = toCase \u22a4 \u2115TD\n \n \u2115D : Desc \u22a4\n \u2115D = toDesc \u22a4 \u2115TD\n \n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n \n zero : \u2115 tt\n zero = con (here , refl)\n \n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (there here , n , refl)\n \n zero2 : \u2115 tt\n zero2 = con2 \u22a4 \u2115D here\n\n suc2 : \u2115 tt \u2192 \u2115 tt\n suc2 = con2 \u22a4 \u2115D (there here)\n\n VecTD : (A : Set) \u2192 TagDesc (\u2115 tt)\n VecTD A = VecT\n , `End zero\n , `Arg (\u2115 tt) (\u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n)))\n , tt\n\n VecC : (A : Set) \u2192 Tag VecT \u2192 Desc (\u2115 tt)\n VecC A = toCase (\u2115 tt) (VecTD A)\n \n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = toDesc (\u2115 tt) (VecTD A)\n \n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n \n nil : (A : Set) \u2192 Vec A zero\n nil A = con (here , refl)\n \n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (there here , n , x , xs , refl)\n \n nil2 : (A : Set) \u2192 Vec A zero\n nil2 A = con2 (\u2115 tt) (VecD A) here\n\n cons2 : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons2 A = con2 (\u2115 tt) (VecD A) (there here)\n\n----------------------------------------------------------------------\n\n module Induction where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 n)\n , (\u03bb m,q ih,tt n \u2192 suc (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 zero)\n , (\u03bb m,q ih,tt n \u2192 add n (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb m t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) m)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)) m (t , c))\n (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n )\n ( (\u03bb q ih n ys \u2192 subst (\u03bb m \u2192 Vec A (add m n)) q ys)\n , (\u03bb m',x,xs,q ih,tt n ys \u2192\n let m' = proj\u2081 m',x,xs,q\n x = proj\u2081 (proj\u2082 m',x,xs,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 m',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb m \u2192 Vec A (add m n)) q (cons A (add m' n) x (ih n ys))\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC (Vec A m) t) (Vec (Vec A m)) n)\n (ih : All (\u2115 tt) (VecD (Vec A m)) (Vec (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m)) n (t , c))\n \u2192 Vec A (mult n m)\n )\n ( (\u03bb q ih \u2192 subst (\u03bb n \u2192 Vec A (mult n m)) q (nil A))\n , (\u03bb n',xs,xss,q ih,tt \u2192\n let n' = proj\u2081 n',xs,xss,q\n xs = proj\u2081 (proj\u2082 n',xs,xss,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 n',xs,xss,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb n \u2192 Vec A (mult n m)) q (append A m xs (mult n' m) ih)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n module Eliminator where\n\n elim\u2115 : (P : (\u2115 tt) \u2192 Set)\n (pzero : P zero)\n (psuc : (m : \u2115 tt) \u2192 P m \u2192 P (suc m))\n (n : \u2115 tt)\n \u2192 P n\n elim\u2115 P pzero psuc = ind \u22a4 \u2115D (\u03bb u n \u2192 P n)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 P n) u (t , c))\n \u2192 P (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (here , q)))\n pzero\n u q\n )\n , (\u03bb n,q ih,tt \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (there here , proj\u2081 n,q , q)))\n (psuc (proj\u2081 n,q) (proj\u2081 ih,tt))\n u (proj\u2082 n,q)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n\n elimVec : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set)\n (pnil : P zero (nil A))\n (pcons : (n : \u2115 tt) (a : A) (xs : Vec A n) \u2192 P n xs \u2192 P (suc n) (cons A n a xs))\n (n : \u2115 tt)\n (xs : Vec A n)\n \u2192 P n xs\n elimVec A P pnil pcons = ind (\u2115 tt) (VecD A) (\u03bb n xs \u2192 P n xs)\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) n)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb n xs \u2192 P n xs) n (t , c))\n \u2192 P n (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq (\u2115 tt) zero (\u03bb n q \u2192 P n (con (here , q)))\n pnil\n n q\n )\n , (\u03bb n',x,xs,q ih,tt \u2192\n let n' = proj\u2081 n',x,xs,q\n x = proj\u2081 (proj\u2082 n',x,xs,q)\n xs = proj\u2081 (proj\u2082 (proj\u2082 n',x,xs,q))\n q = proj\u2082 (proj\u2082 (proj\u2082 n',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n elimEq (\u2115 tt) (suc n') (\u03bb n q \u2192 P n (con (there here , n' , x , xs , q)))\n (pcons n' x xs ih )\n n q\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elimVec A (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elimVec (Vec A m) (\u03bb n xss \u2192 Vec A (mult n m))\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n\n module GenericEliminator where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim2 \u22a4 \u2115TD _\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim2 \u22a4 \u2115TD _\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elim2 (\u2115 tt) (VecTD A) _\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elim2 (\u2115 tt) (VecTD (Vec A m)) _\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n","old_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nmodule Spire.Examples.PropositionalDesc where\n\n----------------------------------------------------------------------\n\nelimEq : (A : Set) (x : A) (P : (y : A) \u2192 x \u2261 y \u2192 Set)\n \u2192 P x refl\n \u2192 (y : A) (p : x \u2261 y) \u2192 P y p\nelimEq A .x P prefl x refl = prefl\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nCases : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nCases [] P = \u22a4\nCases (l \u2237 E) P = P here \u00d7 Cases E \u03bb t \u2192 P (there t)\n\ncase : (E : Enum) (P : Tag E \u2192 Set) (cs : Cases E P) (t : Tag E) \u2192 P t\ncase (l \u2237 E) P (c , cs) here = c\ncase (l \u2237 E) P (c , cs) (there t) = case E (\u03bb t \u2192 P (there t)) cs t\n\nUncurriedCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedCases E P X = Cases E P \u2192 X\n\nCurriedCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedCases [] P X = X\nCurriedCases (l \u2237 E) P X = P here \u2192 CurriedCases E (\u03bb t \u2192 P (there t)) X\n\ncurryCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n (f : UncurriedCases E P X) \u2192 CurriedCases E P X\ncurryCases [] P X f = f tt\ncurryCases (l \u2237 E) P X f = \u03bb c \u2192 curryCases E (\u03bb t \u2192 P (there t)) X (\u03bb cs \u2192 f (c , cs))\n\nuncurryCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n (f : CurriedCases E P X) \u2192 UncurriedCases E P X\nuncurryCases [] P X x tt = x\nuncurryCases (l \u2237 E) P X f (c , cs) = uncurryCases E (\u03bb t \u2192 P (there t)) X (f c) cs\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n `End : (i : I) \u2192 Desc I\n `Rec : (i : I) (D : Desc I) \u2192 Desc I\n `Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n `RecFun : (A : Set) (B : A \u2192 I) (D : Desc I) \u2192 Desc I\n\nISet : Set \u2192 Set\u2081\nISet I = I \u2192 Set\n\nEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 ISet I\nEl I (`End j) X i = j \u2261 i\nEl I (`Rec j D) X i = X j \u00d7 El I D X i\nEl I (`Arg A B) X i = \u03a3 A (\u03bb a \u2192 El I (B a) X i)\nEl I (`RecFun A B D) X i = ((a : A) \u2192 X (B a)) \u00d7 El I D X i\n\nAll : (I : Set) (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El I D X i) \u2192 Set\nAll I (`End j) X P i q = \u22a4\nAll I (`Rec j D) X P i (x , xs) = P j x \u00d7 All I D X P i xs\nAll I (`Arg A B) X P i (a , b) = All I (B a) X P i b\nAll I (`RecFun A B D) X P i (f , xs) = ((a : A) \u2192 P (B a) (f a)) \u00d7 All I D X P i xs\n\ncaseD : (E : Enum) (I : Set) (cs : Cases E (\u03bb _ \u2192 Desc I)) (t : Tag E) \u2192 Desc I\ncaseD E I cs t = case E (\u03bb _ \u2192 Desc I) cs t\n\n----------------------------------------------------------------------\n\nUncurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl I D X = {i : I} \u2192 El I D X i \u2192 X i\n\nCurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl I (`End i) X = X i\nCurriedEl I (`Rec j D) X = (x : X j) \u2192 CurriedEl I D X\nCurriedEl I (`Arg A B) X = (a : A) \u2192 CurriedEl I (B a) X\nCurriedEl I (`RecFun A B D) X = ((a : A) \u2192 X (B a)) \u2192 CurriedEl I D X\n\ncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : UncurriedEl I D X) \u2192 CurriedEl I D X\ncurryEl I (`End i) X cn = cn refl\ncurryEl I (`Rec i D) X cn = \u03bb x \u2192 curryEl I D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl I (`Arg A B) X cn = \u03bb a \u2192 curryEl I (B a) X (\u03bb xs \u2192 cn (a , xs))\ncurryEl I (`RecFun A B D) X cn = \u03bb f \u2192 curryEl I D X (\u03bb xs \u2192 cn (f , xs))\n\nuncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : CurriedEl I D X) \u2192 UncurriedEl I D X\nuncurryEl I (`End i) X cn refl = cn\nuncurryEl I (`Rec i D) X cn (x , xs) = uncurryEl I D X (cn x) xs\nuncurryEl I (`Arg A B) X cn (a , xs) = uncurryEl I (B a) X (cn a) xs\nuncurryEl I (`RecFun A B D) X cn (f , xs) = uncurryEl I D X (cn f) xs\n\ndata \u03bc (I : Set) (D : Desc I) : I \u2192 Set where\n con : UncurriedEl I D (\u03bc I D)\n\ncon2 : (I : Set) (D : Desc I) \u2192 CurriedEl I D (\u03bc I D)\ncon2 I D = curryEl I D (\u03bc I D) con\n\n----------------------------------------------------------------------\n\nUncurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : {i : I} \u2192 El I D X i \u2192 X i)\n \u2192 Set\nUncurriedAll I D X P cn =\n (i : I) (xs : El I D X i) \u2192 All I D X P i xs \u2192 P i (cn xs)\n\nCurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n \u2192 Set\nCurriedAll I (`End i) X P cn =\n P i (cn refl)\nCurriedAll I (`Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedAll I (`Arg A B) X P cn =\n (a : A) \u2192 CurriedAll I (B a) X P (\u03bb xs \u2192 cn (a , xs))\nCurriedAll I (`RecFun A B D) X P cn =\n (f : (a : A) \u2192 X (B a)) (ihf : (a : A) \u2192 P (B a) (f a)) \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (f , xs))\n\ncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : UncurriedAll I D X P cn)\n \u2192 CurriedAll I D X P cn\ncurryAll I (`End i) X P cn pf =\n pf i refl tt\ncurryAll I (`Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryAll I D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryAll I (`Arg A B) X P cn pf =\n \u03bb a \u2192 curryAll I (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\ncurryAll I (`RecFun A B D) X P cn pf =\n \u03bb f ihf \u2192 curryAll I D X P (\u03bb xs \u2192 cn (f , xs)) (\u03bb i xs ihs \u2192 pf i (f , xs) (ihf , ihs))\n\nuncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : CurriedAll I D X P cn)\n \u2192 UncurriedAll I D X P cn\nuncurryAll I (`End .i) X P cn pf i refl tt =\n pf\nuncurryAll I (`Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryAll I (`Arg A B) X P cn pf i (a , xs) ihs =\n uncurryAll I (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\nuncurryAll I (`RecFun A B D) X P cn pf i (f , xs) (ihf , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (f , ys)) (pf f ihf) i xs ihs\n\n----------------------------------------------------------------------\n\nind :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : UncurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\n\nhyps :\n (I : Set)\n (D\u2081 : Desc I)\n (P : (i : I) \u2192 \u03bc I D\u2081 i \u2192 Set)\n (pcon : UncurriedAll I D\u2081 (\u03bc I D\u2081) P con)\n (D\u2082 : Desc I)\n (i : I)\n (xs : El I D\u2082 (\u03bc I D\u2081) i)\n \u2192 All I D\u2082 (\u03bc I D\u2081) P i xs\n\nind I D P pcon i (con xs) = pcon i xs (hyps I D P pcon D i xs)\n\nhyps I D P pcon (`End j) i q = tt\nhyps I D P pcon (`Rec j A) i (x , xs) = ind I D P pcon j x , hyps I D P pcon A i xs\nhyps I D P pcon (`Arg A B) i (a , b) = hyps I D P pcon (B a) i b\nhyps I D P pcon (`RecFun A B E) i (f , xs) = (\u03bb a \u2192 ind I D P pcon (B a) (f a)) , hyps I D P pcon E i xs\n\n----------------------------------------------------------------------\n\nind2 :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : CurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\nind2 I D P pcon i x = ind I D P (uncurryAll I D (\u03bc I D) P con pcon) i x\n\nelim :\n (I : Set)\n (E : Enum)\n (Cs : Tag E \u2192 Desc I)\n \u2192 let D = `Arg (Tag E) Cs in\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n \u2192 UncurriedCases E\n (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))) \n ((i : I) (x : \u03bc I D i) \u2192 P i x)\nelim I E Cs P cs i x =\n let D = `Arg (Tag E) Cs in\n ind2 I D P (case E (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))) cs) i x\n\nelim2 :\n (I : Set)\n (E : Enum)\n (Cs : Tag E \u2192 Desc I)\n \u2192 let D = `Arg (Tag E) Cs in\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n \u2192 let Q = (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs)))\n X = ((i : I) (x : \u03bc I D i) \u2192 P i x)\n in CurriedCases E Q X\nelim2 I E Cs P =\n let D = `Arg (Tag E) Cs\n Q = (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs)))\n X = ((i : I) (x : \u03bc I D i) \u2192 P i x)\n in curryCases E Q X (elim I E Cs P)\n\n----------------------------------------------------------------------\n\nmodule Sugared where\n\n data \u2115T : Set where `zero `suc : \u2115T\n data VecT : Set where `nil `cons : VecT\n\n \u2115D : Desc \u22a4\n \u2115D = `Arg \u2115T \u03bb\n { `zero \u2192 `End tt\n ; `suc \u2192 `Rec tt (`End tt)\n }\n\n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n\n zero : \u2115 tt\n zero = con (`zero , refl)\n\n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (`suc , n , refl)\n\n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg VecT \u03bb\n { `nil \u2192 `End zero\n ; `cons \u2192 `Arg (\u2115 tt) \u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n))\n }\n\n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n\n nil : (A : Set) \u2192 Vec A zero\n nil A = con (`nil , refl)\n\n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (`cons , n , x , xs , refl)\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 n\n ; tt (`suc , m , q) (ih , tt) n \u2192 suc (ih n)\n }\n )\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 zero\n ; tt (`suc , m , q) (ih , tt) n \u2192 add n (ih n)\n }\n )\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) ih n ys \u2192 ys\n ; .(con (`suc , m , refl)) (`cons , m , x , xs , refl) (ih , tt) n ys \u2192 cons A (add m n) x (ih n ys)\n }\n )\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) tt \u2192 nil A\n ; .(con (`suc , n , refl)) (`cons , n , xs , xss , refl) (ih , tt) \u2192 append A m xs (mult n m) ih\n }\n )\n\n----------------------------------------------------------------------\n\nmodule Desugared where\n\n \u2115T : Enum\n \u2115T = \"zero\" \u2237 \"suc\" \u2237 []\n \n VecT : Enum\n VecT = \"nil\" \u2237 \"cons\" \u2237 []\n \n \u2115C : Tag \u2115T \u2192 Desc \u22a4\n \u2115C = caseD \u2115T \u22a4\n ( `End tt\n , `Rec tt (`End tt)\n , tt\n )\n \n \u2115D : Desc \u22a4\n \u2115D = `Arg (Tag \u2115T) \u2115C\n \n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n \n zero : \u2115 tt\n zero = con (here , refl)\n \n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (there here , n , refl)\n \n zero2 : \u2115 tt\n zero2 = con2 \u22a4 \u2115D here\n\n suc2 : \u2115 tt \u2192 \u2115 tt\n suc2 = con2 \u22a4 \u2115D (there here)\n\n VecC : (A : Set) \u2192 Tag VecT \u2192 Desc (\u2115 tt)\n VecC A = caseD VecT (\u2115 tt)\n ( `End zero\n , `Arg (\u2115 tt) (\u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n)))\n , tt\n )\n \n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg (Tag VecT) (VecC A)\n \n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n \n nil : (A : Set) \u2192 Vec A zero\n nil A = con (here , refl)\n \n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (there here , n , x , xs , refl)\n \n nil2 : (A : Set) \u2192 Vec A zero\n nil2 A = con2 (\u2115 tt) (VecD A) here\n\n cons2 : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons2 A = con2 (\u2115 tt) (VecD A) (there here)\n\n----------------------------------------------------------------------\n\n module Induction where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 n)\n , (\u03bb m,q ih,tt n \u2192 suc (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 zero)\n , (\u03bb m,q ih,tt n \u2192 add n (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb m t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) m)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)) m (t , c))\n (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n )\n ( (\u03bb q ih n ys \u2192 subst (\u03bb m \u2192 Vec A (add m n)) q ys)\n , (\u03bb m',x,xs,q ih,tt n ys \u2192\n let m' = proj\u2081 m',x,xs,q\n x = proj\u2081 (proj\u2082 m',x,xs,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 m',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb m \u2192 Vec A (add m n)) q (cons A (add m' n) x (ih n ys))\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC (Vec A m) t) (Vec (Vec A m)) n)\n (ih : All (\u2115 tt) (VecD (Vec A m)) (Vec (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m)) n (t , c))\n \u2192 Vec A (mult n m)\n )\n ( (\u03bb q ih \u2192 subst (\u03bb n \u2192 Vec A (mult n m)) q (nil A))\n , (\u03bb n',xs,xss,q ih,tt \u2192\n let n' = proj\u2081 n',xs,xss,q\n xs = proj\u2081 (proj\u2082 n',xs,xss,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 n',xs,xss,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb n \u2192 Vec A (mult n m)) q (append A m xs (mult n' m) ih)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n module Eliminator where\n\n elim\u2115 : (P : (\u2115 tt) \u2192 Set)\n (pzero : P zero)\n (psuc : (m : \u2115 tt) \u2192 P m \u2192 P (suc m))\n (n : \u2115 tt)\n \u2192 P n\n elim\u2115 P pzero psuc = ind \u22a4 \u2115D (\u03bb u n \u2192 P n)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 P n) u (t , c))\n \u2192 P (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (here , q)))\n pzero\n u q\n )\n , (\u03bb n,q ih,tt \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (there here , proj\u2081 n,q , q)))\n (psuc (proj\u2081 n,q) (proj\u2081 ih,tt))\n u (proj\u2082 n,q)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n\n elimVec : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set)\n (pnil : P zero (nil A))\n (pcons : (n : \u2115 tt) (a : A) (xs : Vec A n) \u2192 P n xs \u2192 P (suc n) (cons A n a xs))\n (n : \u2115 tt)\n (xs : Vec A n)\n \u2192 P n xs\n elimVec A P pnil pcons = ind (\u2115 tt) (VecD A) (\u03bb n xs \u2192 P n xs)\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) n)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb n xs \u2192 P n xs) n (t , c))\n \u2192 P n (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq (\u2115 tt) zero (\u03bb n q \u2192 P n (con (here , q)))\n pnil\n n q\n )\n , (\u03bb n',x,xs,q ih,tt \u2192\n let n' = proj\u2081 n',x,xs,q\n x = proj\u2081 (proj\u2082 n',x,xs,q)\n xs = proj\u2081 (proj\u2082 (proj\u2082 n',x,xs,q))\n q = proj\u2082 (proj\u2082 (proj\u2082 n',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n elimEq (\u2115 tt) (suc n') (\u03bb n q \u2192 P n (con (there here , n' , x , xs , q)))\n (pcons n' x xs ih )\n n q\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elimVec A (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elimVec (Vec A m) (\u03bb n xss \u2192 Vec A (mult n m))\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n\n module GenericEliminator where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim2 \u22a4 \u2115T \u2115C _\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim2 \u22a4 \u2115T \u2115C _\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elim2 (\u2115 tt) VecT (VecC A) _\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elim2 (\u2115 tt) VecT (VecC (Vec A m)) _\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"be764a2fe0b6c2a23e9f4f348a8a152d3071d5ae","subject":"Cosmetic change.","message":"Cosmetic change.\n\nIgnore-this: 2a9374401357d23b6287d5d86a825749\n\ndarcs-hash:20120323134058-3bd4e-217b454a1a805ea13e62f9a24a8e6663c006a0c0.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Relation\/Binary\/Bisimilarity.agda","new_file":"src\/FOTC\/Relation\/Binary\/Bisimilarity.agda","new_contents":"------------------------------------------------------------------------------\n-- Bisimilarity relation\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Relation.Binary.Bisimilarity where\n\nopen import FOTC.Base\n\n-- We add 3 to the fixities of the standard library.\ninfix 7 _\u2248_\n\n------------------------------------------------------------------------------\n-- The bisimilarity relation _\u2248_ is the greatest fixed point (by\n-- \u2248-gfp\u2081 and \u2248-gfp\u2082) of the bisimulation functional (see below).\n\n-- The bisimilarity relation.\npostulate\n _\u2248_ : D \u2192 D \u2192 Set\n\n-- The bisimilarity relation _\u2248_ is a post-fixed point of the\n-- bisimulation functional (see below).\npostulate\n \u2248-gfp\u2081 : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n{-# ATP axiom \u2248-gfp\u2081 #-}\n\n-- The bisimilarity relation _\u2248_ is the greatest post-fixed point of\n-- the bisimulation functional (see below).\n--\n-- N.B. This is an axiom schema. Because in the automatic proofs we\n-- *must* use an instance, we do not add this postulate as an ATP\n-- axiom.\npostulate\n \u2248-gfp\u2082 : (_R_ : D \u2192 D \u2192 Set) \u2192\n -- R is a post-fixed point of the bisimulation functional.\n (\u2200 {xs ys} \u2192 xs R ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys') \u2192\n -- _\u2248_ is greater than R.\n \u2200 {xs ys} \u2192 xs R ys \u2192 xs \u2248 ys\n\n-- Because a greatest post-fixed point is a fixed-point, the\n-- bisimilarity relation _\u2248_ is also a pre-fixed point of the\n-- bisimulation functional (see below).\n\u2248-gfp\u2083 : \u2200 {xs ys} \u2192\n (\u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys') \u2192\n xs \u2248 ys\n\u2248-gfp\u2083 h = \u2248-gfp\u2082 _R_ helper h\n where\n _R_ : D \u2192 D \u2192 Set\n _R_ xs ys = \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n\n helper : \u2200 {xs ys} \u2192 xs R ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n helper (_ , _ , _ , xs'\u2248ys' , prf) = _ , _ , _ , \u2248-gfp\u2081 xs'\u2248ys' , prf\n\nprivate\n module Bisimulation where\n -- In FOTC we won't use the bisimulation functional. This module is\n -- only for illustrative purposes.\n\n -- References:\n --\n -- \u2022 Peter Dybjer and Herbert Sander. A functional programming\n -- approach to the specification and verification of concurrent\n -- systems. Formal Aspects of Computing, 1:303\u2013319, 1989.\n --\n -- \u2022 Bart Jacobs and Jan Rutten. (Co)algebras and\n -- (co)induction. EATCS Bulletin, 62:222\u2013259, 1997.\n\n ----------------------------------------------------------------------------\n -- Adapted from (Dybjer and Sander 1989, p. 310). In this paper, the\n -- authors use the name\n\n -- as (R :: R') bs'\n\n -- for the bisimulation functional.\n\n -- The bisimilarity relation _\u2248_ is the greatest post-fixed point of\n -- Bisimulation (by post-fp and gpfp).\n\n -- The bisimulation functional (Jacobs and Rutten 1997, p. 30).\n BisimulationF : (D \u2192 D \u2192 Set) \u2192 D \u2192 D \u2192 Set\n BisimulationF _R_ xs ys =\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n\n -- The bisimilarity relation _\u2248_ is a post-fixed point of\n -- Bisimulation, i.e,\n --\n -- _\u2248_ \u2264 Bisimulation _\u2248_.\n post-fp : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 BisimulationF _\u2248_ xs ys\n post-fp = \u2248-gfp\u2081\n\n -- The bisimilarity relation _\u2248_ is the greatest post-fixed point of\n -- Bisimulation, i.e\n --\n -- \u2200 R. R \u2264 Bisimulation R \u21d2 R \u2264 _\u2248_.\n gpfp : (_R_ : D \u2192 D \u2192 Set) \u2192\n -- R is a post-fixed point of Bisimulation.\n (\u2200 {xs ys} \u2192 xs R ys \u2192 BisimulationF _R_ xs ys) \u2192\n -- _\u2248_ is greater than R.\n \u2200 {xs ys} \u2192 xs R ys \u2192 xs \u2248 ys\n gpfp = \u2248-gfp\u2082\n\n -- Because a greatest post-fixed point is a fixed-point, the\n -- bisimilarity relation _\u2248_ is also a pre-fixed point of\n -- Bisimulation, i.e.\n --\n -- Bisimulation _\u2248_ \u2264 _\u2248_.\n pre-fp : \u2200 {xs ys} \u2192 BisimulationF _\u2248_ xs ys \u2192 xs \u2248 ys\n pre-fp = \u2248-gfp\u2083\n","old_contents":"------------------------------------------------------------------------------\n-- Bisimilarity relation\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Relation.Binary.Bisimilarity where\n\nopen import FOTC.Base\n\n-- We add 3 to the fixities of the standard library.\ninfix 7 _\u2248_\n\n------------------------------------------------------------------------------\n-- The bisimilarity relation _\u2248_ is the greatest fixed point (by\n-- \u2248-gfp\u2081 and \u2248-gfp\u2082) of the bisimulation functional (see below).\n\n-- The bisimilarity relation.\npostulate\n _\u2248_ : D \u2192 D \u2192 Set\n\n-- The bisimilarity relation _\u2248_ is a post-fixed point of the\n-- bisimulation functional (see below).\npostulate\n \u2248-gfp\u2081 : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n{-# ATP axiom \u2248-gfp\u2081 #-}\n\n-- The bisimilarity relation _\u2248_ is the greatest post-fixed point of\n-- the bisimulation functional (see below).\n--\n-- N.B. This is an axiom schema. Because in the automatic proofs we\n-- *must* use an instance, we do not add this postulate as an ATP\n-- axiom.\npostulate\n \u2248-gfp\u2082 : (_R_ : D \u2192 D \u2192 Set) \u2192\n -- R is a post-fixed point of the bisimulation functional.\n (\u2200 {xs ys} \u2192 xs R ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys') \u2192\n -- _\u2248_ is greater than R.\n \u2200 {xs ys} \u2192 xs R ys \u2192 xs \u2248 ys\n\n-- Because a greatest post-fixed point is a fixed-point, the\n-- bisimilarity relation _\u2248_ is also a pre-fixed point of the\n-- bisimulation functional (see below).\n\u2248-gfp\u2083 : \u2200 {xs ys} \u2192\n (\u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys') \u2192\n xs \u2248 ys\n\u2248-gfp\u2083 h = \u2248-gfp\u2082 _R_ helper h\n where\n _R_ : D \u2192 D \u2192 Set\n _R_ xs ys = \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n\n helper : \u2200 {xs ys} \u2192 xs R ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n helper (_ , _ , _ , xs'\u2248ys' , prf) = _ , _ , _ , \u2248-gfp\u2081 xs'\u2248ys' , prf\n\nprivate\n module Bisimulation where\n -- In FOTC we won't use the bisimulation functional. This module is\n -- only for illustrative purposes.\n\n -- References:\n --\n -- \u2022 Peter Dybjer and Herbert Sander. A functional programming\n -- approach to the specification and verification of concurrent\n -- systems. Formal Aspects of Computing, 1:303\u2013319, 1989.\n --\n -- \u2022 Bart Jacobs and Jan Rutten. (Co)algebras and\n -- (co)induction. EATCS Bulletin, 62:222\u2013259, 1997.\n\n ----------------------------------------------------------------------------\n -- Adapted from (Dybjer and Sander 1989, p. 310). In this paper, the\n -- authors use the name\n\n -- as (R :: R') bs'\n\n -- for the bisimulation functional.\n\n -- The bisimilarity relation _\u2248_ is the greatest post-fixed point of\n -- Bisimulation (by post-fp and gpfp).\n\n -- The bisimulation functional (Jacobs and Rutten 1997, p. 30).\n Bisimulation : (D \u2192 D \u2192 Set) \u2192 D \u2192 D \u2192 Set\n Bisimulation _R_ xs ys =\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n\n -- The bisimilarity relation _\u2248_ is a post-fixed point of\n -- Bisimulation, i.e,\n --\n -- _\u2248_ \u2264 Bisimulation _\u2248_.\n post-fp : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Bisimulation _\u2248_ xs ys\n post-fp = \u2248-gfp\u2081\n\n -- The bisimilarity relation _\u2248_ is the greatest post-fixed point of\n -- Bisimulation, i.e\n --\n -- \u2200 R. R \u2264 Bisimulation R \u21d2 R \u2264 _\u2248_.\n gpfp : (_R_ : D \u2192 D \u2192 Set) \u2192\n -- R is a post-fixed point of Bisimulation.\n (\u2200 {xs ys} \u2192 xs R ys \u2192 Bisimulation _R_ xs ys) \u2192\n -- _\u2248_ is greater than R.\n \u2200 {xs ys} \u2192 xs R ys \u2192 xs \u2248 ys\n gpfp = \u2248-gfp\u2082\n\n -- Because a greatest post-fixed point is a fixed-point, the\n -- bisimilarity relation _\u2248_ is also a pre-fixed point of\n -- Bisimulation, i.e.\n --\n -- Bisimulation _\u2248_ \u2264 _\u2248_.\n pre-fp : \u2200 {xs ys} \u2192 Bisimulation _\u2248_ xs ys \u2192 xs \u2248 ys\n pre-fp = \u2248-gfp\u2083\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d798e80276e0926b00be478453bef02b5735eae4","subject":"Agda's positive checker is looking for occurrences in the indexes.","message":"Agda's positive checker is looking for occurrences in the indexes.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/strictly-positive-inductive-types\/StrictlyPositive.agda","new_file":"notes\/strictly-positive-inductive-types\/StrictlyPositive.agda","new_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule StrictlyPositive where\n\ndata \u2115 : Set where\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n\n------------------------------------------------------------------------------\n-- Parametric stability constraint (Coq Art, p. 378).\n\n-- Error: B != A of type Set when checking the constructor c in the\n-- declaration of T\n\n-- data T (A : Set) : Set where\n-- c : (B : Set) \u2192 T B\n\n------------------------------------------------------------------------------\n-- Head type constraint (Coq Art, section 14.1.2.1).\n\n-- Error: T is not strictly positive, because it occurs in an index of\n-- the target type of the constructor c in the definition of T.\n-- data T : Set \u2192 Set where\n-- c : T (T \u2115)\n\n------------------------------------------------------------------------------\n-- Strictly positive constraints (Coq Art, section 14.1.2.2).\n\n-- Error: A is not strictly positive, because it occurs to the left of\n-- an arrow in the type of the constructor c in the definition of A.\n--\n-- data A : Set where\n-- c : (A \u2192 A) \u2192 A\n\n------------------------------------------------------------------------------\n-- Universe constraints (Coq Art, section 14.1.2.3).\n\nrecord R : Set\u2081 where\n field\n A : Set\n op : A \u2192 A \u2192 A\n e : A\n\n-- Nils (Agda mailing list): According to Voevodsky this type is\n-- incompatible with his univalent model.\ndata _\u2261_ (A : Set) : Set \u2192 Set where\n refl : A \u2261 A\n\ndata A : Set \u2192 Set where\n c : A \u2115\n","old_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule StrictlyPositive where\n\ndata \u2115 : Set where\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n\n------------------------------------------------------------------------------\n-- Parametric stability constraint (Coq Art, p. 378).\n\n-- Error: B != A of type Set when checking the constructor c in the\n-- declaration of T\n\n-- data T (A : Set) : Set where\n-- c : (B : Set) \u2192 T B\n\n------------------------------------------------------------------------------\n-- Head type constraint (Coq Art, section 14.1.2.1).\n\n-- NB. This type is not accepted by Coq.\ndata T : Set \u2192 Set where\n c : T (T \u2115)\n\n------------------------------------------------------------------------------\n-- Strictly positive constraints (Coq Art, section 14.1.2.2).\n\n-- Error: A is not strictly positive, because it occurs to the left of\n-- an arrow in the type of the constructor c in the definition of A.\n--\n-- data A : Set where\n-- c : (A \u2192 A) \u2192 A\n\n------------------------------------------------------------------------------\n-- Universe constraints (Coq Art, section 14.1.2.3).\n\nrecord R : Set\u2081 where\n field\n A : Set\n op : A \u2192 A \u2192 A\n e : A\n\n-- Nils (Agda mailing list): According to Voevodsky this type is\n-- incompatible with his univalent model.\ndata _\u2261_ (A : Set) : Set \u2192 Set where\n refl : A \u2261 A\n\ndata A : Set \u2192 Set where\n c : A \u2115\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b50b0f77764d63042aa5a88075d4320129b3681a","subject":"Change order of implicit arguments of term constructors.","message":"Change order of implicit arguments of term constructors.\n\nThis simplifies extracting the argument type from a term\nof function type.\n\nOld-commit-hash: 2742a31a130e9d01c2283f3e7cb7fae6102a4062\n","repos":"inc-lc\/ilc-agda","old_file":"Syntactic\/Terms\/Total.agda","new_file":"Syntactic\/Terms\/Total.agda","new_contents":"module Syntactic.Terms.Total where\n\n-- TERMS with a primitive for TOTAL DERIVATIVES\n--\n-- This module defines the syntax of terms that support a\n-- primitive (\u0394 e) for computing the total derivative according\n-- to all free variables in e and all future arguments of e if e\n-- is a function.\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Changes\nopen import ChangeContexts\n\nopen import Relation.Binary.PropositionalEquality\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n true false : \u2200 {\u0393} \u2192 Term \u0393 bool\n if : \u2200 {\u0393 \u03c4} \u2192 (t\u2081 : Term \u0393 bool) (t\u2082 t\u2083 : Term \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n -- `\u0394 t` describes how t changes if its free variables or arguments change\n \u0394 : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n weakenOne : \u2200 \u0393\u2081 \u03c4\u2082 {\u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce (\u03c4\u2082 \u2022 \u0393\u2083)) \u03c4\n\nsubstTerm : \u2200 {\u03c4 \u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nsubstTerm {\u03c4} {\u0393\u2081} {\u0393\u2082} \u2261\u2081 t = subst (\u03bb \u0393 \u2192 Term \u0393 \u03c4) \u2261\u2081 t\n","old_contents":"module Syntactic.Terms.Total where\n\n-- TERMS with a primitive for TOTAL DERIVATIVES\n--\n-- This module defines the syntax of terms that support a\n-- primitive (\u0394 e) for computing the total derivative according\n-- to all free variables in e and all future arguments of e if e\n-- is a function.\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Changes\nopen import ChangeContexts\n\nopen import Relation.Binary.PropositionalEquality\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n true false : \u2200 {\u0393} \u2192 Term \u0393 bool\n if : \u2200 {\u0393 \u03c4} \u2192 (t\u2081 : Term \u0393 bool) (t\u2082 t\u2083 : Term \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n -- `\u0394 t` describes how t changes if its free variables or arguments change\n \u0394 : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n weakenOne : \u2200 \u0393\u2081 \u03c4\u2082 {\u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce (\u03c4\u2082 \u2022 \u0393\u2083)) \u03c4\n\nsubstTerm : \u2200 {\u03c4 \u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nsubstTerm {\u03c4} {\u0393\u2081} {\u0393\u2082} \u2261\u2081 t = subst (\u03bb \u0393 \u2192 Term \u0393 \u03c4) \u2261\u2081 t\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c16de318c104447ae09330656978df89645b8aad","subject":"IDesc example: for fun, add pair (but no projection)","message":"IDesc example: for fun, add pair (but no projection)","repos":"kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram,kwangkim\/pigment","old_file":"models\/IDescTT.agda","new_file":"models\/IDescTT.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n pair : Type -> Type -> Type\n\n--********************************\n-- Typed expressions\n--********************************\n\n-- Fix menu:\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty), -- Val t\n (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void)))\n\n-- Indexed menu:\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\nchoiceMenu (pair x y) = nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\nchoiceDessert (pair x y) = Void\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\n\n-- Expression:\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\nVal (pair x y) = (Val x) * (Val y)\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), \n Void))\n\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\nchoiceFreeMenu (pair x y) = nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void)\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void )\nchoiceFreeDessert (pair x y) = Void\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (exprFree ** Val)\n\n--********************************\n-- Closed term' evaluation\n--********************************\n\neval' : {ty : Type} -> IMu closeTerm' ty -> Val ty\neval' {ty} term = cata Type closeTerm' Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm' ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n\n--********************************\n-- Open terms\n--********************************\n\ndata Vec (A : Set) : Nat -> Set where\n vnil : Vec A ze\n vcons : {n : Nat} -> A -> Vec A n -> Vec A (su n)\n\ndata Fin : Nat -> Set where\n fze : {n : Nat} -> Fin (su n)\n fsu : {n : Nat} -> Fin n -> Fin (su n)\n\nContext : Nat -> Set\nContext n = Vec (Sigma Type (\\ty -> IMu closeTerm' ty)) n\n\ntypeAt : {n : Nat}(c : Context n) -> Fin n -> Type\ntypeAt {ze} c ()\ntypeAt {.(su n)} (vcons x xs) (fze {n}) = fst x\ntypeAt {.(su n)} (vcons x xs) (fsu {n} y) = typeAt xs y\n\nlookup : {n : Nat}(c : Context n)(i : Fin n) -> IMu closeTerm' (typeAt c i)\nlookup {ze} c ()\nlookup {su _} (vcons x _) fze = snd x\nlookup {su _} (vcons _ xs) (fsu y) = lookup xs y\n\n\nVar : {n : Nat} -> Context n -> Type -> Set\nVar {n} c ty = Sigma (Fin n) (\\i -> typeAt c i == ty)\n\nopenTerm : {n : Nat} -> Context n -> Type -> IDesc Type\nopenTerm c = toIDesc Type (exprFree ** (\\ty -> Val ty + Var c ty))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : {n : Nat}\n {context : Context n}\n (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm' ty\ndischarge ty (l value) = con (EZe , value)\ndischarge {n} {c} ty (r variable) = subst (cong (IMu closeTerm') (snd variable)) (lookup c (fst variable))\n\nsubstExpr : {n : Nat}\n {ty : Type}\n (context : Context n)\n (sigma : (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm' ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm' ty\nsubstExpr {n} {ty} c sig term = \n substI (\\ty -> Val ty + Var c ty) Val exprFree sig ty term\n\nevalOpen : {n : Nat}{ty : Type}\n (context : Context n) ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen context tm = eval' (substExpr context discharge tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- V 0 :-> true, V 1 :-> 2, V 2 :-> ( false , 1 )\ntestContext : Context _\ntestContext = vcons (bool , con (EZe , true )) \n (vcons (nat , con (EZe , su (su ze)) ) \n (vcons (pair bool nat , con (EZe , ( false , su ze )))\n vnil))\n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , r ( fsu fze , refl ) )\n\ntestSubst1 : IMu closeTerm' nat\ntestSubst1 = substExpr testContext \n discharge \n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con (ESu (ESu EZe) , (con (EZe , l (su ze)) , con ( EZe , r (fsu fze , refl) )) )\n\ntestSubst2 : IMu closeTerm' nat\ntestSubst2 = substExpr testContext \n discharge \n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext test2\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu EZe , (con (EZe , r (fze , refl)) ,\n (con (EZe , r (fsu fze , refl)) ,\n con (EZe , l ze))))\n\ntestSubst3 : IMu closeTerm' nat\ntestSubst3 = substExpr testContext \n discharge \n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext test3\n\n-- V 2\ntest4 : IMu (openTerm testContext) (pair bool nat)\ntest4 = con (EZe , r ( fsu (fsu fze) , refl ) )\n\ntestSubst4 : IMu closeTerm' (pair bool nat)\ntestSubst4 = substExpr testContext \n discharge \n test4\n-- = (false , 1)\ntestEval4 : Val (pair bool nat)\ntestEval4 = evalOpen testContext test4\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n\n\n--********************************\n-- Typed expressions\n--********************************\n\n-- Fix menu:\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty), -- Val t\n (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void)))\n\n-- Indexed menu:\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\n\n-- Expression:\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), \n Void))\n\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void)\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void )\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (exprFree ** Val)\n\n--********************************\n-- Closed term' evaluation\n--********************************\n\neval' : {ty : Type} -> IMu closeTerm' ty -> Val ty\neval' {ty} term = cata Type closeTerm' Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm' ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n\n--********************************\n-- Open terms\n--********************************\n\ndata Vec (A : Set) : Nat -> Set where\n vnil : Vec A ze\n vcons : {n : Nat} -> A -> Vec A n -> Vec A (su n)\n\ndata Fin : Nat -> Set where\n fze : {n : Nat} -> Fin (su n)\n fsu : {n : Nat} -> Fin n -> Fin (su n)\n\nContext : Nat -> Set\nContext n = Vec (Sigma Type (\\ty -> IMu closeTerm' ty)) n\n\ntypeAt : {n : Nat}(c : Context n) -> Fin n -> Type\ntypeAt {ze} c ()\ntypeAt {.(su n)} (vcons x xs) (fze {n}) = fst x\ntypeAt {.(su n)} (vcons x xs) (fsu {n} y) = typeAt xs y\n\nlookup : {n : Nat}(c : Context n)(i : Fin n) -> IMu closeTerm' (typeAt c i)\nlookup {ze} c ()\nlookup {su _} (vcons x _) fze = snd x\nlookup {su _} (vcons _ xs) (fsu y) = lookup xs y\n\n\nVar : {n : Nat} -> Context n -> Type -> Set\nVar {n} c ty = Sigma (Fin n) (\\i -> typeAt c i == ty)\n\nopenTerm : {n : Nat} -> Context n -> Type -> IDesc Type\nopenTerm c = toIDesc Type (exprFree ** (\\ty -> Val ty + Var c ty))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : {n : Nat}\n {context : Context n}\n (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm' ty\ndischarge ty (l value) = con (EZe , value)\ndischarge {n} {c} ty (r variable) = subst (cong (IMu closeTerm') (snd variable)) (lookup c (fst variable))\n\nsubstExpr : {n : Nat}\n {ty : Type}\n (context : Context n)\n (sigma : (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm' ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm' ty\nsubstExpr {n} {ty} c sig term = \n substI (\\ty -> Val ty + Var c ty) Val exprFree sig ty term\n\nevalOpen : {n : Nat}{ty : Type}\n (context : Context n) ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen context tm = eval' (substExpr context discharge tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- V 0 :-> true, V 1 :-> 2\ntestContext : Context _\ntestContext = vcons (bool , con (EZe , true )) (vcons (nat , con (EZe , su (su ze)) ) vnil)\n\n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , r ( fsu fze , refl ) )\n\ntestSubst1 : IMu closeTerm' nat\ntestSubst1 = substExpr testContext \n discharge \n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con (ESu (ESu EZe) , (con (EZe , l (su ze)) , con ( EZe , r (fsu fze , refl) )) )\n\ntestSubst2 : IMu closeTerm' nat\ntestSubst2 = substExpr testContext \n discharge \n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext test2\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu EZe , (con (EZe , r (fze , refl)) ,\n (con (EZe , r (fsu fze , refl)) ,\n con (EZe , l ze))))\n\ntestSubst3 : IMu closeTerm' nat\ntestSubst3 = substExpr testContext \n discharge \n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext test3","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ff8df0e78b5c026b9f0c8643e8b27ac0b03d8491","subject":"Cleanup README.agda (partially)","message":"Cleanup README.agda (partially)\n\nReorder modules and express a bit of the structure.\nSome other changes are proposed in comments, because I'm not fully sure\nabout them yet.\n\nOld-commit-hash: 0bf53e9e29581399c12fb7e3f5a8ff3dc6879e98\n","repos":"inc-lc\/ilc-agda","old_file":"README.agda","new_file":"README.agda","new_contents":"module README where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * multiple calculi\n--\n-- To typecheck this formalization, you need to install the appropriate version\n-- of Agda, the Agda standard library (version 0.7), generate Everything.agda\n-- with the attached Haskell helper, and finally run Agda on this file.\n--\n-- Given a Unix-like environment (including Cygwin), running the .\/agdaCheck.sh\n-- script and following instructions given on output will help with the\n-- \"generate Everything.agda\" part.\n--\n-- We use Agda HEAD from September 2013; Agda 2.3.2.1 might happen to work, but\n-- has some bugs with serialization of code using some recent syntactic sugar\n-- which we use (https:\/\/code.google.com\/p\/agda\/issues\/detail?id=756), so it\n-- might work or not. When it does not, removing Agda caches (.agdai files)\n-- appears to often help.\n\nimport Postulate.Extensionality\n\nimport Base.Data.DependentList\n\n-- Variables and contexts\nimport Base.Syntax.Context\n\n-- Sets of variables\nimport Base.Syntax.Vars\n\nimport Base.Denotation.Notation\n\n-- Environments\nimport Base.Denotation.Environment\n\n-- Change contexts\nimport Base.Change.Context\n\n-- # Base, parametric proof.\n--\n-- This is for a parametric calculus where:\n-- types are parametric in base types\n-- terms are parametric in constants\n--\n--\n-- Modules are ordered and grouped according to what they represent.\n\n-- ## Definitions\n\nimport Parametric.Syntax.Type\nimport Parametric.Syntax.Term\n\nimport Parametric.Denotation.Value\nimport Parametric.Denotation.Evaluation\n\nimport Parametric.Change.Type\nimport Parametric.Change.Term\n\nimport Parametric.Change.Derive\n\nimport Parametric.Change.Value\nimport Parametric.Change.Evaluation\n\n-- ## Proofs\n\nimport Parametric.Change.Validity\nimport Parametric.Change.Specification\nimport Parametric.Change.Implementation\nimport Parametric.Change.Correctness\n\n-- # Popl14 plugin\n--\n-- The structure is the same as the parametric proof (down to the\n-- order and the grouping of modules), except for the postulate module.\n\n-- Postulate an abstract data type for integer Bags.\nimport Postulate.Bag-Popl14\n\n-- ## Definitions\nimport Popl14.Syntax.Type\nimport Popl14.Syntax.Term\n\nimport Popl14.Denotation.Value\nimport Popl14.Denotation.Evaluation\n\nimport Popl14.Change.Term\nimport Popl14.Change.Type\n\nimport Popl14.Change.Derive\n\nimport Popl14.Change.Value\nimport Popl14.Change.Evaluation\n\n-- ## Proofs\n\nimport Popl14.Change.Validity\nimport Popl14.Change.Specification\nimport Popl14.Change.Implementation\nimport Popl14.Change.Correctness\n\n-- Some other calculus (remove from here?)\n\nimport Atlas.Syntax.Type\nimport Atlas.Syntax.Term\nimport Atlas.Change.Type\nimport Atlas.Change.Term\nimport Atlas.Change.Derive\n\n-- Import everything else.\nimport Everything\n\n-- XXX: Do we still care about these?\n\nimport Property.Uniqueness\n\nimport Structure.Bag.Popl14\nimport Structure.Tuples\n\nimport Theorem.CongApp\nimport Theorem.EqualityUnique\nimport Theorem.Groups-Popl14\nimport Theorem.Irrelevance-Popl14\nimport Theorem.IrrelevanceUnique-Popl14\nimport Theorem.ProductUnique\n\n-- Old stuff.\n-- XXX Update and integrate those descriptions where appropriate.\n\n{-\n-- Correctness theorem for canonical derivation\nimport Popl14.Change.Correctness\n\n-- Denotation-as-specification for canonical derivation\nimport Popl14.Change.Specification\n\n-- The idea of implementing a denotational specification\nimport Popl14.Change.Implementation\n-}\n","old_contents":"module README where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * multiple calculi\n--\n-- To typecheck this formalization, you need to install the appropriate version\n-- of Agda, the Agda standard library (version 0.7), generate Everything.agda\n-- with the attached Haskell helper, and finally run Agda on this file.\n--\n-- Given a Unix-like environment (including Cygwin), running the .\/agdaCheck.sh\n-- script and following instructions given on output will help with the\n-- \"generate Everything.agda\" part.\n--\n-- We use Agda HEAD from September 2013; Agda 2.3.2.1 might happen to work, but\n-- has some bugs with serialization of code using some recent syntactic sugar\n-- which we use (https:\/\/code.google.com\/p\/agda\/issues\/detail?id=756), so it\n-- might work or not. When it does not, removing Agda caches (.agdai files)\n-- appears to often help.\n\n\nimport Popl14.Syntax.Term\nimport Base.Syntax.Context\nimport Base.Change.Context\nimport Popl14.Change.Derive\n{- Language definition of Calc. Atlas -}\nimport Atlas.Syntax.Term\n{- Terms of a calculus described in Plotkin style\n - types are parametric in base types\n - terms are parametric in constants\n This style of language description is employed in:\n G. D. Plotkin. \"LCF considered as a programming language.\"\n Theoretical Computer Science 5(3) pp. 223--255, 1997.\n http:\/\/dx.doi.org\/10.1016\/0304-3975(77)90044-5 -}\nimport Parametric.Syntax.Term\nimport Popl14.Change.Term\nimport Atlas.Syntax.Type\nimport Parametric.Syntax.Type\nimport Popl14.Syntax.Type\nimport Base.Syntax.Vars\n\nimport Popl14.Change.Validity\n{- Correctness theorem for canonical derivation of Calc. Popl14 -}\nimport Popl14.Change.Correctness\nimport Theorem.Irrelevance-Popl14\nimport Base.Denotation.Environment\nimport Popl14.Denotation.Evaluation\n{- The idea of implementing a denotational specification for Calc. Popl14 -}\nimport Popl14.Change.Implementation\nimport Base.Denotation.Notation\n{- Denotation-as-specification for canonical derivation of Calc. Popl14 -}\nimport Popl14.Change.Specification\nimport Popl14.Denotation.Value\nimport experimental.DecidableEq\nimport experimental.FoldableBag\nimport experimental.FoldableBagParametric\nimport experimental.NormalizationByEvaluation\nimport experimental.OrdBag\nimport experimental.Sorting\nimport Postulate.Bag-Popl14\nimport Postulate.Extensionality\nimport Property.Uniqueness\nimport Structure.Bag.Popl14\nimport Structure.Tuples\n\nimport Theorem.CongApp\nimport Theorem.EqualityUnique\nimport Theorem.Groups-Popl14\nimport Theorem.IrrelevanceUnique-Popl14\nimport Theorem.ProductUnique\nimport UNDEFINED\n\nimport Everything\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"53ffa41592e8de74ad057626e193d26cc8e3b78b","subject":"Groups-Nehemiah: reorder theorems","message":"Groups-Nehemiah: reorder theorems\n","repos":"inc-lc\/ilc-agda","old_file":"Theorem\/Groups-Nehemiah.agda","new_file":"Theorem\/Groups-Nehemiah.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- About the group structure of integers and bags for Nehemiah plugin.\n------------------------------------------------------------------------\n\nmodule Theorem.Groups-Nehemiah where\n\nopen import Structure.Bag.Nehemiah public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Algebra.Structures\n\n4-way-shuffle : \u2200 {A : Set} {f} {z a b c d : A}\n {{comm-monoid : IsCommutativeMonoid _\u2261_ f z}} \u2192\n f (f a b) (f c d) \u2261 f (f a c) (f b d)\n4-way-shuffle {f = f} {z = z} {a} {b} {c} {d} {{comm-monoid}} =\n let\n assoc = associative comm-monoid\n cmute = commutative comm-monoid\n in\n begin\n f (f a b) (f c d)\n \u2261\u27e8 assoc a b (f c d) \u27e9\n f a (f b (f c d))\n \u2261\u27e8 cong (f a) (sym (assoc b c d)) \u27e9\n f a (f (f b c) d)\n \u2261\u27e8 cong (\u03bb hole \u2192 f a (f hole d)) (cmute b c) \u27e9\n f a (f (f c b) d)\n \u2261\u27e8 cong (f a) (assoc c b d) \u27e9\n f a (f c (f b d))\n \u2261\u27e8 sym (assoc a c (f b d)) \u27e9\n f (f a c) (f b d)\n \u220e where open \u2261-Reasoning\n\nopen import Data.Integer\n\nn+[m-n]=m : \u2200 {n m} \u2192 n + (m - n) \u2261 m\nn+[m-n]=m {n} {m} =\n begin\n n + (m - n)\n \u2261\u27e8 cong (\u03bb hole \u2192 n + hole) (commutative-int m (- n)) \u27e9\n n + (- n + m)\n \u2261\u27e8 sym (associative-int n (- n) m) \u27e9\n (n - n) + m\n \u2261\u27e8 cong (\u03bb hole \u2192 hole + m) (right-inv-int n) \u27e9\n (+ 0) + m\n \u2261\u27e8 left-id-int m \u27e9\n m\n \u220e where open \u2261-Reasoning\n\na++[b\\\\a]=b : \u2200 {a b} \u2192 a ++ (b \\\\ a) \u2261 b\na++[b\\\\a]=b {b} {d} = trans\n (cong (\u03bb hole \u2192 b ++ hole) (commutative-bag d (negateBag b))) (trans\n (sym (associative-bag b (negateBag b) d)) (trans\n (cong (\u03bb hole \u2192 hole ++ d) (right-inv-bag b))\n (left-id-bag d)))\n\nab\u00b7cd=ac\u00b7bd : \u2200 {a b c d : Bag} \u2192\n (a ++ b) ++ (c ++ d) \u2261 (a ++ c) ++ (b ++ d)\nab\u00b7cd=ac\u00b7bd {a} {b} {c} {d} =\n 4-way-shuffle {a = a} {b} {c} {d} {{comm-monoid-bag}}\n\nmn\u00b7pq=mp\u00b7nq : \u2200 {m n p q : \u2124} \u2192\n (m + n) + (p + q) \u2261 (m + p) + (n + q)\nmn\u00b7pq=mp\u00b7nq {m} {n} {p} {q} =\n 4-way-shuffle {a = m} {n} {p} {q} {{comm-monoid-int}}\n\ninverse-unique : \u2200 {A : Set} {f neg} {z a b : A}\n {{abelian : IsAbelianGroup _\u2261_ f z neg}} \u2192\n f a b \u2261 z \u2192 b \u2261 neg a\n\ninverse-unique {f = f} {neg} {z} {a} {b} {{abelian}} ab=z =\n let\n assoc = associative (IsAbelianGroup.isCommutativeMonoid abelian)\n cmute = commutative (IsAbelianGroup.isCommutativeMonoid abelian)\n in\n begin\n b\n \u2261\u27e8 sym (left-identity abelian b) \u27e9\n f z b\n \u2261\u27e8 cong (\u03bb hole \u2192 f hole b) (sym (left-inverse abelian a)) \u27e9\n f (f (neg a) a) b\n \u2261\u27e8 assoc (neg a) a b \u27e9\n f (neg a) (f a b)\n \u2261\u27e8 cong (f (neg a)) ab=z \u27e9\n f (neg a) z\n \u2261\u27e8 right-identity abelian (neg a) \u27e9\n neg a\n \u220e where\n open \u2261-Reasoning\n eq1 : f (neg a) (f a b) \u2261 f (neg a) z\n eq1 rewrite ab=z = cong (f (neg a)) refl\n\ndistribute-neg : \u2200 {A : Set} {f neg} {z a b : A}\n {{abelian : IsAbelianGroup _\u2261_ f z neg}} \u2192\n f (neg a) (neg b) \u2261 neg (f a b)\ndistribute-neg {f = f} {neg} {z} {a} {b} {{abelian}} = inverse-unique\n {{abelian}}\n (begin\n f (f a b) (f (neg a) (neg b))\n \u2261\u27e8 4-way-shuffle {{IsAbelianGroup.isCommutativeMonoid abelian}} \u27e9\n f (f a (neg a)) (f b (neg b))\n \u2261\u27e8 cong\u2082 f (inverse a) (inverse b) \u27e9\n f z z\n \u2261\u27e8 left-identity abelian z \u27e9\n z\n \u220e) where\n open \u2261-Reasoning\n inverse = right-inverse abelian\n\n-a\u00b7-b=-ab : \u2200 {a b : Bag} \u2192\n negateBag a ++ negateBag b \u2261 negateBag (a ++ b)\n-a\u00b7-b=-ab {a} {b} = distribute-neg {a = a} {b} {{abelian-bag}}\n\n-m\u00b7-n=-mn : \u2200 {m n : \u2124} \u2192\n (- m) + (- n) \u2261 - (m + n)\n-m\u00b7-n=-mn {m} {n} = distribute-neg {a = m} {n} {{abelian-int}}\n\n[a++b]\\\\a=b : \u2200 {a b} \u2192 (a ++ b) \\\\ a \u2261 b\n[a++b]\\\\a=b {b} {d} =\n begin\n (b ++ d) \\\\ b\n \u2261\u27e8 cong (\u03bb hole \u2192 hole \\\\ b) (commutative-bag b d) \u27e9\n (d ++ b) \\\\ b\n \u2261\u27e8 associative-bag d b (negateBag b) \u27e9\n d ++ (b \\\\ b)\n \u2261\u27e8 cong (_++_ d) (right-inv-bag b) \u27e9\n d ++ emptyBag\n \u2261\u27e8 right-id-bag d \u27e9\n d\n \u220e where open \u2261-Reasoning\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- About the group structure of integers and bags for Nehemiah plugin.\n------------------------------------------------------------------------\n\nmodule Theorem.Groups-Nehemiah where\n\nopen import Structure.Bag.Nehemiah public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Integer\nopen import Algebra.Structures\n\nn+[m-n]=m : \u2200 {n m} \u2192 n + (m - n) \u2261 m\nn+[m-n]=m {n} {m} =\n begin\n n + (m - n)\n \u2261\u27e8 cong (\u03bb hole \u2192 n + hole) (commutative-int m (- n)) \u27e9\n n + (- n + m)\n \u2261\u27e8 sym (associative-int n (- n) m) \u27e9\n (n - n) + m\n \u2261\u27e8 cong (\u03bb hole \u2192 hole + m) (right-inv-int n) \u27e9\n (+ 0) + m\n \u2261\u27e8 left-id-int m \u27e9\n m\n \u220e where open \u2261-Reasoning\n\na++[b\\\\a]=b : \u2200 {a b} \u2192 a ++ (b \\\\ a) \u2261 b\na++[b\\\\a]=b {b} {d} = trans\n (cong (\u03bb hole \u2192 b ++ hole) (commutative-bag d (negateBag b))) (trans\n (sym (associative-bag b (negateBag b) d)) (trans\n (cong (\u03bb hole \u2192 hole ++ d) (right-inv-bag b))\n (left-id-bag d)))\n\n4-way-shuffle : \u2200 {A : Set} {f} {z a b c d : A}\n {{comm-monoid : IsCommutativeMonoid _\u2261_ f z}} \u2192\n f (f a b) (f c d) \u2261 f (f a c) (f b d)\n4-way-shuffle {f = f} {z = z} {a} {b} {c} {d} {{comm-monoid}} =\n let\n assoc = associative comm-monoid\n cmute = commutative comm-monoid\n in\n begin\n f (f a b) (f c d)\n \u2261\u27e8 assoc a b (f c d) \u27e9\n f a (f b (f c d))\n \u2261\u27e8 cong (f a) (sym (assoc b c d)) \u27e9\n f a (f (f b c) d)\n \u2261\u27e8 cong (\u03bb hole \u2192 f a (f hole d)) (cmute b c) \u27e9\n f a (f (f c b) d)\n \u2261\u27e8 cong (f a) (assoc c b d) \u27e9\n f a (f c (f b d))\n \u2261\u27e8 sym (assoc a c (f b d)) \u27e9\n f (f a c) (f b d)\n \u220e where open \u2261-Reasoning\n\nab\u00b7cd=ac\u00b7bd : \u2200 {a b c d : Bag} \u2192\n (a ++ b) ++ (c ++ d) \u2261 (a ++ c) ++ (b ++ d)\nab\u00b7cd=ac\u00b7bd {a} {b} {c} {d} =\n 4-way-shuffle {a = a} {b} {c} {d} {{comm-monoid-bag}}\n\nmn\u00b7pq=mp\u00b7nq : \u2200 {m n p q : \u2124} \u2192\n (m + n) + (p + q) \u2261 (m + p) + (n + q)\nmn\u00b7pq=mp\u00b7nq {m} {n} {p} {q} =\n 4-way-shuffle {a = m} {n} {p} {q} {{comm-monoid-int}}\n\ninverse-unique : \u2200 {A : Set} {f neg} {z a b : A}\n {{abelian : IsAbelianGroup _\u2261_ f z neg}} \u2192\n f a b \u2261 z \u2192 b \u2261 neg a\n\ninverse-unique {f = f} {neg} {z} {a} {b} {{abelian}} ab=z =\n let\n assoc = associative (IsAbelianGroup.isCommutativeMonoid abelian)\n cmute = commutative (IsAbelianGroup.isCommutativeMonoid abelian)\n in\n begin\n b\n \u2261\u27e8 sym (left-identity abelian b) \u27e9\n f z b\n \u2261\u27e8 cong (\u03bb hole \u2192 f hole b) (sym (left-inverse abelian a)) \u27e9\n f (f (neg a) a) b\n \u2261\u27e8 assoc (neg a) a b \u27e9\n f (neg a) (f a b)\n \u2261\u27e8 cong (f (neg a)) ab=z \u27e9\n f (neg a) z\n \u2261\u27e8 right-identity abelian (neg a) \u27e9\n neg a\n \u220e where\n open \u2261-Reasoning\n eq1 : f (neg a) (f a b) \u2261 f (neg a) z\n eq1 rewrite ab=z = cong (f (neg a)) refl\n\ndistribute-neg : \u2200 {A : Set} {f neg} {z a b : A}\n {{abelian : IsAbelianGroup _\u2261_ f z neg}} \u2192\n f (neg a) (neg b) \u2261 neg (f a b)\ndistribute-neg {f = f} {neg} {z} {a} {b} {{abelian}} = inverse-unique\n {{abelian}}\n (begin\n f (f a b) (f (neg a) (neg b))\n \u2261\u27e8 4-way-shuffle {{IsAbelianGroup.isCommutativeMonoid abelian}} \u27e9\n f (f a (neg a)) (f b (neg b))\n \u2261\u27e8 cong\u2082 f (inverse a) (inverse b) \u27e9\n f z z\n \u2261\u27e8 left-identity abelian z \u27e9\n z\n \u220e) where\n open \u2261-Reasoning\n inverse = right-inverse abelian\n\n-a\u00b7-b=-ab : \u2200 {a b : Bag} \u2192\n negateBag a ++ negateBag b \u2261 negateBag (a ++ b)\n-a\u00b7-b=-ab {a} {b} = distribute-neg {a = a} {b} {{abelian-bag}}\n\n-m\u00b7-n=-mn : \u2200 {m n : \u2124} \u2192\n (- m) + (- n) \u2261 - (m + n)\n-m\u00b7-n=-mn {m} {n} = distribute-neg {a = m} {n} {{abelian-int}}\n\n[a++b]\\\\a=b : \u2200 {a b} \u2192 (a ++ b) \\\\ a \u2261 b\n[a++b]\\\\a=b {b} {d} =\n begin\n (b ++ d) \\\\ b\n \u2261\u27e8 cong (\u03bb hole \u2192 hole \\\\ b) (commutative-bag b d) \u27e9\n (d ++ b) \\\\ b\n \u2261\u27e8 associative-bag d b (negateBag b) \u27e9\n d ++ (b \\\\ b)\n \u2261\u27e8 cong (_++_ d) (right-inv-bag b) \u27e9\n d ++ emptyBag\n \u2261\u27e8 right-id-bag d \u27e9\n d\n \u220e where open \u2261-Reasoning\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"46d4925ee9668c8d90cf14f4daa40bc575c33cbd","subject":"Added x\u2238yy\u2192yy\u2192yn = \u22a5-elim $ 0>x\u2192\u22a5 Nn 0>n\nx>y\u2192yy\u2192ySn =\n trans (<-SS n m) (x>y\u2192ySn))\n\nx\u2265y\u2192x\u226ey : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GE m n \u2192 NLT m n\nx\u2265y\u2192x\u226ey zN zN _ = x\u2270x zN\nx\u2265y\u2192x\u226ey zN (sN Nn) 0\u2265Sn = \u22a5-elim $ 0\u2265S\u2192\u22a5 Nn 0\u2265Sn\nx\u2265y\u2192x\u226ey (sN {m} Nm) zN _ = <-S0 m\nx\u2265y\u2192x\u226ey (sN {m} Nm) (sN {n} Nn) Sm\u2265Sn =\n trans (<-SS m n) (x\u2265y\u2192x\u226ey Nm Nn (trans (sym $ <-SS n (succ m)) Sm\u2265Sn))\n\nx>y\u2192x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 NLE m n\nx>y\u2192x\u2270y zN Nn 0>m = \u22a5-elim $ 0>x\u2192\u22a5 Nn 0>m\nx>y\u2192x\u2270y (sN Nm) zN _ = S\u22700 Nm\nx>y\u2192x\u2270y (sN {m} Nm) (sN {n} Nn) Sm>Sn =\n x\u2270y\u2192Sx\u2270Sy m n (x>y\u2192x\u2270y Nm Nn (trans (sym $ <-SS n m) Sm>Sn))\n\npostulate\n x>y\u2192x\u2264y\u2192\u22a5 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 LE m n \u2192 \u22a5\n{-# ATP prove x>y\u2192x\u2264y\u2192\u22a5 x>y\u2192x\u2270y #-}\n\nx>y\u2228x\u2264y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2228 LE m n\nx>y\u2228x\u2264y zN Nn = inj\u2082 $ x\u22650 Nn\nx>y\u2228x\u2264y (sN {m} Nm) zN = inj\u2081 $ <-0S m\nx>y\u2228x\u2264y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m>n \u2192 inj\u2081 (trans (<-SS n m) m>n))\n , (\u03bb m\u2264n \u2192 inj\u2082 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n ] (x>y\u2228x\u2264y Nm Nn)\n\nxy\u2228x\u2264y Nn Nm\n\nx\u2264y\u2228x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2228 NLE m n\nx\u2264y\u2228x\u2270y zN Nn = inj\u2081 (0\u2264x Nn)\nx\u2264y\u2228x\u2270y (sN Nm) zN = inj\u2082 (S\u22700 Nm)\nx\u2264y\u2228x\u2270y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m\u2264n \u2192 inj\u2081 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n , (\u03bb m\u2270n \u2192 inj\u2082 (x\u2270y\u2192Sx\u2270Sy m n m\u2270n))\n ] (x\u2264y\u2228x\u2270y Nm Nn)\n\nx\u2261y\u2192x\u2264y : \u2200 {m n} {Nm : N m} {Nn : N n} \u2192 m \u2261 n \u2192 LE m n\nx\u2261y\u2192x\u2264y {Nm = Nm} refl = x\u2264x Nm\n\nxy\u2192x\u2238y+y\u2261x : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 (m \u2238 n) + n \u2261 m\nx>y\u2192x\u2238y+y\u2261x zN Nn 0>n = \u22a5-elim $ 0>x\u2192\u22a5 Nn 0>n\nx>y\u2192x\u2238y+y\u2261x (sN {m} Nm) zN Sm>0 = prf\n where\n postulate prf : (succ m \u2238 zero) + zero \u2261 succ m\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx>y\u2192x\u2238y+y\u2261x (sN {m} Nm) (sN {n} Nn) Sm>Sn = prf $ x>y\u2192x\u2238y+y\u2261x Nm Nn m>n\n where\n postulate m>n : GT m n\n {-# ATP prove m>n #-}\n\n postulate prf : (m \u2238 n) + n \u2261 m \u2192 -- IH.\n (succ m \u2238 succ n) + succ n \u2261 succ m\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx\u2264y\u2192y\u2238x+x\u2261y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2192 (n \u2238 m) + m \u2261 n\nx\u2264y\u2192y\u2238x+x\u2261y {n = n} zN Nn 0\u2264n = prf\n where\n postulate prf : (n \u2238 zero) + zero \u2261 n\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx\u2264y\u2192y\u2238x+x\u2261y (sN Nm) zN Sm\u22640 = \u22a5-elim $ S\u22640\u2192\u22a5 Nm Sm\u22640\n\nx\u2264y\u2192y\u2238x+x\u2261y (sN {m} Nm) (sN {n} Nn) Sm\u2264Sn = prf $ x\u2264y\u2192y\u2238x+x\u2261y Nm Nn m\u2264n\n where\n postulate m\u2264n : LE m n\n {-# ATP prove m\u2264n #-}\n\n postulate prf : (n \u2238 m) + m \u2261 n \u2192 -- IH.\n (succ n \u2238 succ m) + succ m \u2261 succ n\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx0\u2192x\u2238y0\u2192x\u2238y0 = \u22a5-elim $ x>x\u2192\u22a5 zN 0>0\nx\u2265y\u2192y>0\u2192x\u2238y0\u2192x\u2238y0 = prf\n where\n postulate prf : LT (succ m \u2238 succ n) (succ m)\n {-# ATP prove prf x\u2238yy\u2192yy\u2192yn = \u22a5-elim $ 0>x\u2192\u22a5 Nn 0>n\nx>y\u2192yy\u2192ySn =\n trans (<-SS n m) (x>y\u2192ySn))\n\nx\u2265y\u2192x\u226ey : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GE m n \u2192 NLT m n\nx\u2265y\u2192x\u226ey zN zN _ = x\u2270x zN\nx\u2265y\u2192x\u226ey zN (sN Nn) 0\u2265Sn = \u22a5-elim $ 0\u2265S\u2192\u22a5 Nn 0\u2265Sn\nx\u2265y\u2192x\u226ey (sN {m} Nm) zN _ = <-S0 m\nx\u2265y\u2192x\u226ey (sN {m} Nm) (sN {n} Nn) Sm\u2265Sn =\n trans (<-SS m n) (x\u2265y\u2192x\u226ey Nm Nn (trans (sym $ <-SS n (succ m)) Sm\u2265Sn))\n\nx>y\u2192x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 NLE m n\nx>y\u2192x\u2270y zN Nn 0>m = \u22a5-elim $ 0>x\u2192\u22a5 Nn 0>m\nx>y\u2192x\u2270y (sN Nm) zN _ = S\u22700 Nm\nx>y\u2192x\u2270y (sN {m} Nm) (sN {n} Nn) Sm>Sn =\n x\u2270y\u2192Sx\u2270Sy m n (x>y\u2192x\u2270y Nm Nn (trans (sym $ <-SS n m) Sm>Sn))\n\npostulate\n x>y\u2192x\u2264y\u2192\u22a5 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 LE m n \u2192 \u22a5\n{-# ATP prove x>y\u2192x\u2264y\u2192\u22a5 x>y\u2192x\u2270y #-}\n\nx>y\u2228x\u2264y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2228 LE m n\nx>y\u2228x\u2264y zN Nn = inj\u2082 $ x\u22650 Nn\nx>y\u2228x\u2264y (sN {m} Nm) zN = inj\u2081 $ <-0S m\nx>y\u2228x\u2264y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m>n \u2192 inj\u2081 (trans (<-SS n m) m>n))\n , (\u03bb m\u2264n \u2192 inj\u2082 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n ] (x>y\u2228x\u2264y Nm Nn)\n\nxy\u2228x\u2264y Nn Nm\n\nx\u2264y\u2228x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2228 NLE m n\nx\u2264y\u2228x\u2270y zN Nn = inj\u2081 (0\u2264x Nn)\nx\u2264y\u2228x\u2270y (sN Nm) zN = inj\u2082 (S\u22700 Nm)\nx\u2264y\u2228x\u2270y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m\u2264n \u2192 inj\u2081 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n , (\u03bb m\u2270n \u2192 inj\u2082 (x\u2270y\u2192Sx\u2270Sy m n m\u2270n))\n ] (x\u2264y\u2228x\u2270y Nm Nn)\n\nx\u2261y\u2192x\u2264y : \u2200 {m n} {Nm : N m} {Nn : N n} \u2192 m \u2261 n \u2192 LE m n\nx\u2261y\u2192x\u2264y {Nm = Nm} refl = x\u2264x Nm\n\nxy\u2192x\u2238y+y\u2261x : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 (m \u2238 n) + n \u2261 m\nx>y\u2192x\u2238y+y\u2261x zN Nn 0>n = \u22a5-elim $ 0>x\u2192\u22a5 Nn 0>n\nx>y\u2192x\u2238y+y\u2261x (sN {m} Nm) zN Sm>0 = prf\n where\n postulate prf : (succ m \u2238 zero) + zero \u2261 succ m\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx>y\u2192x\u2238y+y\u2261x (sN {m} Nm) (sN {n} Nn) Sm>Sn = prf $ x>y\u2192x\u2238y+y\u2261x Nm Nn m>n\n where\n postulate m>n : GT m n\n {-# ATP prove m>n #-}\n\n postulate prf : (m \u2238 n) + n \u2261 m \u2192 -- IH.\n (succ m \u2238 succ n) + succ n \u2261 succ m\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx\u2264y\u2192y\u2238x+x\u2261y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2192 (n \u2238 m) + m \u2261 n\nx\u2264y\u2192y\u2238x+x\u2261y {n = n} zN Nn 0\u2264n = prf\n where\n postulate prf : (n \u2238 zero) + zero \u2261 n\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx\u2264y\u2192y\u2238x+x\u2261y (sN Nm) zN Sm\u22640 = \u22a5-elim $ S\u22640\u2192\u22a5 Nm Sm\u22640\n\nx\u2264y\u2192y\u2238x+x\u2261y (sN {m} Nm) (sN {n} Nn) Sm\u2264Sn = prf $ x\u2264y\u2192y\u2238x+x\u2261y Nm Nn m\u2264n\n where\n postulate m\u2264n : LE m n\n {-# ATP prove m\u2264n #-}\n\n postulate prf : (n \u2238 m) + m \u2261 n \u2192 -- IH.\n (succ n \u2238 succ m) + succ m \u2261 succ n\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx0\u2192x\u2238y0\u2192x\u2238y0 = \u22a5-elim $ x>x\u2192\u22a5 zN 0>0\nx\u2265y\u2192y>0\u2192x\u2238y0\u2192x\u2238y0 = prf\n where\n postulate prf : LT (succ m \u2238 succ n) (succ m)\n {-# ATP prove prf x\u2238y = 0#; <1b> = 0#; cond = 1#; fork = 1+_+_D; tt = 0#;\n <_,_> = _\u2218\u2032_; fst = 0#; snd = 0#;\n dup = 0#; first = F.id; swap = 0#; assoc = 0#;\n = 0#; snd = 0#;\n <_\u00d7_> = _\u2218\u2032_; second = F.id;\n nil = 0#; cons = 0#; uncons = 0# }\n where open D\n\nmodule SeqTimeOpsD where\n Time = Diff\u2115\n open D\n open FlatFunsOps seqTimeOpsD public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2257 0#\n snoc\u22610 zero x = \u2261.refl\n snoc\u22610 (suc n) x rewrite snoc\u22610 n x = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2257 0#\n reverse\u22610 zero x = \u2261.refl\n reverse\u22610 (suc n) x rewrite reverse\u22610 n x | snoc\u22610 n x = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2257 0#\n replicate\u22610 zero = \u03bb _ \u2192 \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2257 0#\n constBit\u22610 true {-1b-} _ = \u2261.refl\n constBit\u22610 false {-0b-} _ = \u2261.refl\n\n sum\u2032 : \u2200 {n} \u2192 Vec Diff\u2115 n \u2192 Diff\u2115\n sum\u2032 = V.foldr (const Diff\u2115) _\u2218\u2032_ id\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec {n} f xs \u2257 sum\u2032 (V.map f xs)\n constVec\u2261sum f [] x = \u2261.refl\n constVec\u2261sum {suc n} f (b \u2237 bs) x rewrite \u2115\u00b0.+-comm (f b x \u2294 constVec f bs x) 0\n | constVec\u2261sum f bs x = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2257 0#\n constBits\u22610 [] x = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) x rewrite constBits\u22610 bs x\n | constBit\u22610 b x = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2257 m *# n\n foldl\u2261* zero n x = \u2261.refl\n foldl\u2261* (suc m) n x rewrite foldl\u2261* m n (n x) = \u2261.refl\n\n foldr\u2261* : \u2200 m n \u2192 foldr {m} n \u2257 m *#\u2032 n\n foldr\u2261* zero n x = \u2261.refl\n foldr\u2261* (suc m) n x rewrite foldr\u2261* m n x = \u2261.refl\n\n #nodes : \u2200 i \u2192 Diff\u2115\n #nodes zero = 0#\n #nodes (suc i) = 1# +# #nodes i +# #nodes i\n\n fromBitsFun\u2261 : \u2200 {i o} (f : Bits i \u2192 Bits o) \u2192 fromBitsFun f \u2257 #nodes i\n fromBitsFun\u2261 {zero} f x = constBits\u22610 (f []) x\n fromBitsFun\u2261 {suc i} f x rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_) x\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_) (#nodes i x) = \u2261.refl\n\nseqTimeOps : FlatFunsOps (constFuns \u2115)\nseqTimeOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 0; <1b> = 0; cond = 1; fork = 1+_+_; tt = 0;\n <_,_> = _+_; fst = 0; snd = 0;\n dup = 0; first = F.id; swap = 0; assoc = 0;\n = 0; snd = 0;\n <_\u00d7_> = _+_; second = F.id;\n nil = 0; cons = 0; uncons = 0 }\n\ntimeOps : FlatFunsOps (constFuns \u2115)\ntimeOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 0; <1b> = 0; cond = 1; fork = 1+_\u2294_; tt = 0;\n <_,_> = _\u2294_; fst = 0; snd = 0;\n dup = 0; first = F.id; swap = 0; assoc = 0;\n = 0; snd = 0;\n <_\u00d7_> = _\u2294_; second = F.id;\n nil = 0; cons = 0; uncons = 0 }\n\n\ntimeOps\u2261seqTimeOps : timeOps \u2261 record seqTimeOps {\n <_,_> = _\u2294_; <_\u00d7_> = _\u2294_; fork = 1+_\u2294_\n ; cons = 0; uncons = 0 } -- Without cons = 0... this definition makes\n -- the FlatFunsOps record def yellow\ntimeOps\u2261seqTimeOps = \u2261.refl\n\n{-\nopen import maxsemiring\nopen \u2294-+-SemiringSolver\ntimeOps' : \u2200 n \u2192 FlatFunsOps (constFuns (Polynomial n))\ntimeOps' n = record {\n id = con 0; _\u2218_ = _:*_;\n <0b> = con 0; <1b> = con 0; cond = con 1; fork = 1+_+_; tt = con 0;\n <_,_> = _:*_; fst = con 0; snd = con 0;\n dup = con 0; <_\u00d7_> = _:+_; swap = con 0; assoc = con 0;\n = con 0; snd = con 0;\n nil = con 0; cons = con 0; uncons = con 0 }\n where 1+_+_ : Polynomial n \u2192 Polynomial n \u2192 Polynomial n\n 1+ x + y = con 1 :* (x :* y)\n\nModule TimeOps' where\n Time = \u2115\n open FlatFunsOps (timeOps' 1) public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} := con 0\n snoc\u22610 zero = ?\n snoc\u22610 (suc n) = ?\n-}\n\nmodule TimeOps where\n Time = \u2115\n open FlatFunsOps timeOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2294\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2261 0\n replicate\u22610 zero = \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2261 0\n constBit\u22610 true = \u2261.refl\n constBit\u22610 false = \u2261.refl\n\n maximum : \u2200 {n} \u2192 Vec \u2115 n \u2192 \u2115\n maximum = V.foldr (const \u2115) (\u03bb {_} x y \u2192 x \u2294 y) 0\n\n constVec\u2261maximum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec {n} f xs \u2261 maximum (V.map f xs)\n constVec\u2261maximum f [] = \u2261.refl\n constVec\u2261maximum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b \u2294 constVec f bs) 0\n | constVec\u2261maximum f bs = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2261 0\n constBits\u22610 [] = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) rewrite constBit\u22610 b\n | constBits\u22610 bs = \u2261.refl\n\n fromBitsFun\u2261i : \u2200 {i o} (f : Bits i \u2192 Bits o) \u2192 fromBitsFun f \u2261 i\n fromBitsFun\u2261i {zero} f rewrite constBits\u22610 (f []) = \u2261.refl\n fromBitsFun\u2261i {suc i} f rewrite fromBitsFun\u2261i {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261i {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (i \u2294 i) 0\n = \u2261.cong suc (i\u2294i\u2261i i)\n\nspaceOps : FlatFunsOps (constFuns \u2115)\nspaceOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 1; <1b> = 1; cond = 1; fork = 1+_+_; tt = 0;\n <_,_> = 1+_+_; fst = 0; snd = 0;\n dup = 1; first = F.id; swap = 0; assoc = 0;\n = 0; snd = 0;\n <_\u00d7_> = _+_; second = F.id;\n nil = 0; cons = 0; uncons = 0 }\n\n-- So far the space cost model is like the sequential time cost model but makes <0b>,<1b>,dup\n-- cost one unit of space.\nspaceOps\u2261seqTimeOps : spaceOps \u2261 record seqTimeOps { <0b> = 1; <1b> = 1; dup = 1; <_,_> = 1+_+_\n ; cons = 0; uncons = 0 } -- same bug here\nspaceOps\u2261seqTimeOps = \u2261.refl\n\nmodule SpaceOps where\n Space = \u2115\n open FlatFunsOps spaceOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2115\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u2261n : \u2200 n \u2192 replicate {n} \u2261 n\n replicate\u2261n zero = \u2261.refl\n replicate\u2261n (suc n) rewrite replicate\u2261n n = \u2261.refl\n\n constBit\u22611 : \u2200 b \u2192 constBit b \u2261 1\n constBit\u22611 true = \u2261.refl\n constBit\u22611 false = \u2261.refl\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Space) xs \u2192 constVec {n} f xs \u2261 V.sum (V.map f xs)\n constVec\u2261sum f [] = \u2261.refl\n constVec\u2261sum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b + constVec f bs) 0\n | constVec\u2261sum f bs = \u2261.refl\n\n constBits\u2261n : \u2200 {n} xs \u2192 constBits {n} xs \u2261 n\n constBits\u2261n [] = \u2261.refl\n constBits\u2261n {suc n} (b \u2237 bs) rewrite constBit\u22611 b\n | constBits\u2261n bs\n | \u2115\u00b0.+-comm n 0 = \u2261.refl\n\n fromBitsFun-cost : \u2200 (i o : \u2115) \u2192 \u2115\n fromBitsFun-cost zero o = o\n fromBitsFun-cost (suc i) o = 1 + 2*(fromBitsFun-cost i o)\n\n fromBitsFun\u2261 : \u2200 {i o} (f : Bits i \u2192 Bits o) \u2192 fromBitsFun f \u2261 fromBitsFun-cost i o\n fromBitsFun\u2261 {zero} {o} f rewrite constBits\u2261n (f []) | \u2115\u00b0.+-comm o 0 = \u2261.refl\n fromBitsFun\u2261 {suc i} {o} f rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (2* (fromBitsFun-cost i o)) 0\n = \u2261.refl\n\ntime\u00d7spaceOps : FlatFunsOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-\u266dOps timeOps spaceOps\n\nmodule Time\u00d7SpaceOps = FlatFunsOps time\u00d7spaceOps","old_contents":"module flat-funs-cost where\n\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2*_; 2^_; _^_; _\u2294_; module \u2115\u00b0; module \u2294\u00b0; 2*\u2032_)\nopen import Data.Bool using (true; false)\nimport Data.DifferenceNat\nimport Data.Vec.NP as V\nimport Function as F\nimport Data.Product as \u00d7\nimport Relation.Binary.PropositionalEquality as \u2261\nopen F using (const; _\u2218\u2032_)\nopen V using (Vec; []; _\u2237_; _++_; [_])\nopen \u00d7 using (_\u00d7_)\nopen \u2261 using (_\u2261_; _\u2257_)\n\nopen import Data.Bits using (Bits; 0\u2237_; 1\u2237_)\n\nopen import flat-funs\n\nmodule D where\n open Data.DifferenceNat public renaming (suc to suc#; _+_ to _+#_)\n _*#_ : \u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n zero *# d = 0#\n suc n *# d = (n *# d) +# d\n _*#\u2032_ : \u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n zero *#\u2032 d = 0#\n suc n *#\u2032 d = d +# (n *#\u2032 d)\nopen D using (Diff\u2115)\n\nprivate\n 1+_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n 1+ x + y = 1 + (x + y)\n 1+_\u2294_ : \u2115 \u2192 \u2115 \u2192 \u2115\n 1+ x \u2294 y = 1 + (x \u2294 y)\n i\u2294i\u2261i : \u2200 i \u2192 i \u2294 i \u2261 i\n i\u2294i\u2261i zero = \u2261.refl\n i\u2294i\u2261i (suc i) = \u2261.cong suc (i\u2294i\u2261i i)\n\nseqTimeOpsD : FlatFunsOps (constFuns Diff\u2115)\nseqTimeOpsD = record {\n id = 0#; _\u2218_ = _\u2218\u2032_;\n <0b> = 0#; <1b> = 0#; cond = 1#; fork = 1+_+_D; tt = 0#;\n <_,_> = _\u2218\u2032_; fst = 0#; snd = 0#;\n dup = 0#; first = F.id; swap = 0#; assoc = 0#;\n = 0#; snd = 0#;\n <_\u00d7_> = _\u2218\u2032_; second = F.id;\n nil = 0#; cons = 0#; uncons = 0# }\n where open D\n 1+_+_D : Diff\u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n 1+ x + y D = 1# \u2218\u2032 (x \u2218\u2032 y)\n\nmodule SeqTimeOpsD where\n Time = Diff\u2115\n open D\n open FlatFunsOps seqTimeOpsD public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2257 0#\n snoc\u22610 zero x = \u2261.refl\n snoc\u22610 (suc n) x rewrite snoc\u22610 n x = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2257 0#\n reverse\u22610 zero x = \u2261.refl\n reverse\u22610 (suc n) x rewrite reverse\u22610 n x | snoc\u22610 n x = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2257 0#\n replicate\u22610 zero = \u03bb _ \u2192 \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2257 0#\n constBit\u22610 true {-1b-} _ = \u2261.refl\n constBit\u22610 false {-0b-} _ = \u2261.refl\n\n sum\u2032 : \u2200 {n} \u2192 Vec Diff\u2115 n \u2192 Diff\u2115\n sum\u2032 = V.foldr (const Diff\u2115) _\u2218\u2032_ id\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec {n} f xs \u2257 sum\u2032 (V.map f xs)\n constVec\u2261sum f [] x = \u2261.refl\n constVec\u2261sum {suc n} f (b \u2237 bs) x rewrite \u2115\u00b0.+-comm (f b x \u2294 constVec f bs x) 0\n | constVec\u2261sum f bs x = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2257 0#\n constBits\u22610 [] x = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) x rewrite constBits\u22610 bs x\n | constBit\u22610 b x = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2257 m *# n\n foldl\u2261* zero n x = \u2261.refl\n foldl\u2261* (suc m) n x rewrite foldl\u2261* m n (n x) = \u2261.refl\n\n foldr\u2261* : \u2200 m n \u2192 foldr {m} n \u2257 m *#\u2032 n\n foldr\u2261* zero n x = \u2261.refl\n foldr\u2261* (suc m) n x rewrite foldr\u2261* m n x = \u2261.refl\n\n #nodes : \u2200 i \u2192 Diff\u2115\n #nodes zero = 0#\n #nodes (suc i) = 1# +# #nodes i +# #nodes i\n\n fromBitsFun\u2261 : \u2200 {i o} (f : Bits i \u2192 Bits o) \u2192 fromBitsFun f \u2257 #nodes i\n fromBitsFun\u2261 {zero} f x = constBits\u22610 (f []) x\n fromBitsFun\u2261 {suc i} f x rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_) x\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_) (#nodes i x) = \u2261.refl\n\nseqTimeOps : FlatFunsOps (constFuns \u2115)\nseqTimeOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 0; <1b> = 0; cond = 1; fork = 1+_+_; tt = 0;\n <_,_> = _+_; fst = 0; snd = 0;\n dup = 0; first = F.id; swap = 0; assoc = 0;\n = 0; snd = 0;\n <_\u00d7_> = _+_; second = F.id;\n nil = 0; cons = 0; uncons = 0 }\n\ntimeOps : FlatFunsOps (constFuns \u2115)\ntimeOps = record seqTimeOps {<_,_> = _\u2294_; <_\u00d7_> = _\u2294_; fork = 1+_\u2294_; cons = 0; uncons = 0 }\n-- Without cons = 0... this definition makes the FlatFunsOps record def yellow\n\n{-\nopen import maxsemiring\nopen \u2294-+-SemiringSolver\ntimeOps' : \u2200 n \u2192 FlatFunsOps (constFuns (Polynomial n))\ntimeOps' n = record {\n id = con 0; _\u2218_ = _:*_;\n <0b> = con 0; <1b> = con 0; cond = con 1; fork = 1+_+_; tt = con 0;\n <_,_> = _:*_; fst = con 0; snd = con 0;\n dup = con 0; <_\u00d7_> = _:+_; swap = con 0; assoc = con 0;\n = con 0; snd = con 0;\n nil = con 0; cons = con 0; uncons = con 0 }\n where 1+_+_ : Polynomial n \u2192 Polynomial n \u2192 Polynomial n\n 1+ x + y = con 1 :* (x :* y)\n\nModule TimeOps' where\n Time = \u2115\n open FlatFunsOps (timeOps' 1) public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} := con 0\n snoc\u22610 zero = ?\n snoc\u22610 (suc n) = ?\n-}\n\nmodule TimeOps where\n Time = \u2115\n open FlatFunsOps timeOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2294\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2261 0\n replicate\u22610 zero = \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2261 0\n constBit\u22610 true = \u2261.refl\n constBit\u22610 false = \u2261.refl\n\n maximum : \u2200 {n} \u2192 Vec \u2115 n \u2192 \u2115\n maximum = V.foldr (const \u2115) (\u03bb {_} x y \u2192 x \u2294 y) 0\n\n constVec\u2261maximum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec {n} f xs \u2261 maximum (V.map f xs)\n constVec\u2261maximum f [] = \u2261.refl\n constVec\u2261maximum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b \u2294 constVec f bs) 0\n | constVec\u2261maximum f bs = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2261 0\n constBits\u22610 [] = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) rewrite constBit\u22610 b\n | constBits\u22610 bs = \u2261.refl\n\n fromBitsFun\u2261i : \u2200 {i o} (f : Bits i \u2192 Bits o) \u2192 fromBitsFun f \u2261 i\n fromBitsFun\u2261i {zero} f rewrite constBits\u22610 (f []) = \u2261.refl\n fromBitsFun\u2261i {suc i} f rewrite fromBitsFun\u2261i {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261i {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (i \u2294 i) 0\n = \u2261.cong suc (i\u2294i\u2261i i)\n\nspaceOps : FlatFunsOps (constFuns \u2115)\nspaceOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 1; <1b> = 1; cond = 1; fork = 1+_+_; tt = 0;\n <_,_> = 1+_+_; fst = 0; snd = 0;\n dup = 1; first = F.id; swap = 0; assoc = 0;\n = 0; snd = 0;\n <_\u00d7_> = _+_; second = F.id;\n nil = 0; cons = 0; uncons = 0 }\n\n-- So far the space cost model is like the sequential time cost model but makes <0b>,<1b>,dup\n-- cost one unit of space.\nspaceOps\u2261seqTimeOps : spaceOps \u2261 record seqTimeOps { <0b> = 1; <1b> = 1; dup = 1; <_,_> = 1+_+_\n ; cons = 0; uncons = 0 } -- same bug here\nspaceOps\u2261seqTimeOps = \u2261.refl\n\nmodule SpaceOps where\n Space = \u2115\n open FlatFunsOps spaceOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2115\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u2261n : \u2200 n \u2192 replicate {n} \u2261 n\n replicate\u2261n zero = \u2261.refl\n replicate\u2261n (suc n) rewrite replicate\u2261n n = \u2261.refl\n\n constBit\u22611 : \u2200 b \u2192 constBit b \u2261 1\n constBit\u22611 true = \u2261.refl\n constBit\u22611 false = \u2261.refl\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Space) xs \u2192 constVec {n} f xs \u2261 V.sum (V.map f xs)\n constVec\u2261sum f [] = \u2261.refl\n constVec\u2261sum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b + constVec f bs) 0\n | constVec\u2261sum f bs = \u2261.refl\n\n constBits\u2261n : \u2200 {n} xs \u2192 constBits {n} xs \u2261 n\n constBits\u2261n [] = \u2261.refl\n constBits\u2261n {suc n} (b \u2237 bs) rewrite constBit\u22611 b\n | constBits\u2261n bs\n | \u2115\u00b0.+-comm n 0 = \u2261.refl\n\n fromBitsFun-cost : \u2200 (i o : \u2115) \u2192 \u2115\n fromBitsFun-cost zero o = o\n fromBitsFun-cost (suc i) o = 1 + 2*(fromBitsFun-cost i o)\n\n fromBitsFun\u2261 : \u2200 {i o} (f : Bits i \u2192 Bits o) \u2192 fromBitsFun f \u2261 fromBitsFun-cost i o\n fromBitsFun\u2261 {zero} {o} f rewrite constBits\u2261n (f []) | \u2115\u00b0.+-comm o 0 = \u2261.refl\n fromBitsFun\u2261 {suc i} {o} f rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (2* (fromBitsFun-cost i o)) 0\n = \u2261.refl\n\ntime\u00d7spaceOps : FlatFunsOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-\u266dOps timeOps spaceOps\n\nmodule Time\u00d7SpaceOps = FlatFunsOps time\u00d7spaceOps","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"8bb712cd1a8c9c9cc0885ef29918d9cc47588189","subject":"Document P.D.Evaluation.","message":"Document P.D.Evaluation.\n\nOld-commit-hash: a346d6ffd091c3041ecb8d087a4171aeffa63576\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Denotation\/Evaluation.agda","new_file":"Parametric\/Denotation\/Evaluation.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Standard evaluation (Def. 3.3 and Fig. 4i)\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\n\nmodule Parametric.Denotation.Evaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\n\nopen import Base.Denotation.Notation\n\nopen import Relation.Binary.PropositionalEquality\nopen import Theorem.CongApp\nopen import Postulate.Extensionality\n\n-- Extension Point: Evaluation of fully applied constants.\nStructure : Set\nStructure = \u2200 {\u03a3 \u03c4} \u2192 Const \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\nmodule Structure (\u27e6_\u27e7Const : Structure) where\n \u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\n \u27e6_\u27e7Terms : \u2200 {\u0393 \u03a3} \u2192 Terms \u0393 \u03a3 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03a3 \u27e7\n\n -- We provide: Evaluation of arbitrary terms.\n \u27e6 const c args \u27e7Term \u03c1 = \u27e6 c \u27e7Const (\u27e6 args \u27e7Terms \u03c1)\n \u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n \u27e6 app s t \u27e7Term \u03c1 = (\u27e6 s \u27e7Term \u03c1) (\u27e6 t \u27e7Term \u03c1)\n \u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\n -- this is what we'd like to write.\n -- unfortunately termination checker complains.\n --\n -- \u27e6 terms \u27e7Terms \u03c1 = map-IVT (\u03bb t \u2192 \u27e6 t \u27e7Term \u03c1) terms\n --\n -- so we do explicit pattern matching instead.\n \u27e6 \u2205 \u27e7Terms \u03c1 = \u2205\n \u27e6 s \u2022 terms \u27e7Terms \u03c1 = \u27e6 s \u27e7Term \u03c1 \u2022 \u27e6 terms \u27e7Terms \u03c1\n \n meaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\n meaningOfTerm = meaning \u27e6_\u27e7Term\n\n weaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082}\n (t : Term \u0393\u2081 \u03c4) (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken \u0393\u2081\u227c\u0393\u2082 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 \u0393\u2081\u227c\u0393\u2082 \u27e7 \u03c1)\n\n weaken-terms-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03a3} {\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082}\n (terms : Terms \u0393\u2081 \u03a3) (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192\n \u27e6 weaken-terms \u0393\u2081\u227c\u0393\u2082 terms \u27e7Terms \u03c1 \u2261 \u27e6 terms \u27e7Terms (\u27e6 \u0393\u2081\u227c\u0393\u2082 \u27e7 \u03c1)\n\n weaken-terms-sound \u2205 \u03c1 = refl\n weaken-terms-sound (t \u2022 terms) \u03c1 =\n cong\u2082 _\u2022_ (weaken-sound t \u03c1) (weaken-terms-sound terms \u03c1)\n\n weaken-sound {\u0393\u2081\u227c\u0393\u2082 = \u0393\u2081\u227c\u0393\u2082} (var x) \u03c1 = weaken-var-sound \u0393\u2081\u227c\u0393\u2082 x \u03c1\n weaken-sound (app s t) \u03c1 = weaken-sound s \u03c1 \u27e8$\u27e9 weaken-sound t \u03c1\n weaken-sound (abs t) \u03c1 = ext (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\n weaken-sound {\u0393\u2081} {\u0393\u2082} {\u0393\u2081\u227c\u0393\u2082 = \u0393\u2081\u227c\u0393\u2082} (const {\u03a3} {\u03c4} c args) \u03c1 =\n cong \u27e6 c \u27e7Const (weaken-terms-sound args \u03c1)\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Evaluation for languages described in Plotkin style\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\n\nmodule Parametric.Denotation.Evaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\n\nopen import Base.Denotation.Notation\n\nopen import Relation.Binary.PropositionalEquality\nopen import Theorem.CongApp\nopen import Postulate.Extensionality\n\nStructure : Set\nStructure = \u2200 {\u03a3 \u03c4} \u2192 Const \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\nmodule Structure (\u27e6_\u27e7Const : Structure) where\n \u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\n \u27e6_\u27e7Terms : \u2200 {\u0393 \u03a3} \u2192 Terms \u0393 \u03a3 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03a3 \u27e7\n\n \u27e6 const c args \u27e7Term \u03c1 = \u27e6 c \u27e7Const (\u27e6 args \u27e7Terms \u03c1)\n \u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n \u27e6 app s t \u27e7Term \u03c1 = (\u27e6 s \u27e7Term \u03c1) (\u27e6 t \u27e7Term \u03c1)\n \u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\n -- this is what we'd like to write.\n -- unfortunately termination checker complains.\n --\n -- \u27e6 terms \u27e7Terms \u03c1 = map-IVT (\u03bb t \u2192 \u27e6 t \u27e7Term \u03c1) terms\n --\n -- so we do explicit pattern matching instead.\n \u27e6 \u2205 \u27e7Terms \u03c1 = \u2205\n \u27e6 s \u2022 terms \u27e7Terms \u03c1 = \u27e6 s \u27e7Term \u03c1 \u2022 \u27e6 terms \u27e7Terms \u03c1\n \n meaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\n meaningOfTerm = meaning \u27e6_\u27e7Term\n\n weaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082}\n (t : Term \u0393\u2081 \u03c4) (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken \u0393\u2081\u227c\u0393\u2082 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 \u0393\u2081\u227c\u0393\u2082 \u27e7 \u03c1)\n\n weaken-terms-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03a3} {\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082}\n (terms : Terms \u0393\u2081 \u03a3) (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192\n \u27e6 weaken-terms \u0393\u2081\u227c\u0393\u2082 terms \u27e7Terms \u03c1 \u2261 \u27e6 terms \u27e7Terms (\u27e6 \u0393\u2081\u227c\u0393\u2082 \u27e7 \u03c1)\n\n weaken-terms-sound \u2205 \u03c1 = refl\n weaken-terms-sound (t \u2022 terms) \u03c1 =\n cong\u2082 _\u2022_ (weaken-sound t \u03c1) (weaken-terms-sound terms \u03c1)\n\n weaken-sound {\u0393\u2081\u227c\u0393\u2082 = \u0393\u2081\u227c\u0393\u2082} (var x) \u03c1 = weaken-var-sound \u0393\u2081\u227c\u0393\u2082 x \u03c1\n weaken-sound (app s t) \u03c1 = weaken-sound s \u03c1 \u27e8$\u27e9 weaken-sound t \u03c1\n weaken-sound (abs t) \u03c1 = ext (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\n weaken-sound {\u0393\u2081} {\u0393\u2082} {\u0393\u2081\u227c\u0393\u2082 = \u0393\u2081\u227c\u0393\u2082} (const {\u03a3} {\u03c4} c args) \u03c1 =\n cong \u27e6 c \u27e7Const (weaken-terms-sound args \u03c1)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"df3db95515c4144eec20758b26fd5ef6c4c2f54a","subject":"Desc stratified model: I hate yellow. Almost done with the proof, but stuck on yellow.","message":"Desc stratified model: I hate yellow. Almost done with the proof, but stuck on yellow.","repos":"kwangkim\/pigment,brixen\/Epigram,kwangkim\/pigment,kwangkim\/pigment","old_file":"models\/IDesc.agda","new_file":"models\/IDesc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\nunlift : {l : Level}{A : Set l} -> Lifted A -> A\nunlift (lifter a) = a\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l m : Level}{A : Set l}{B : Set m}\n (f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l m n : Level}{A : Set l}{B : Set m}{C : Set n}\n (f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n-- Intensionally extensional\npostulate \n reflFun : {l m : Level}{A : Set l}{B : Set m}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set l) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set l} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set l}(R : I -> IDesc I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set l}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set l -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const (lift I)\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : {l : Level}(I : Set l) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\n\nIDescl0 : {l : Level}(I : Set l) -> Unit -> Set (suc l)\nIDescl0 I = IMu (\\_ -> descD I)\n\nIDescl : {l : Level}(I : Set l) -> Set (suc l)\nIDescl I = IDescl0 I Void\n\nvarl : {l : Level}{I : Set l}(i : I) -> IDescl I\nvarl {x} i = con (lv , lifter i) \n where lv : DescDConst {l = suc x}\n lv = lvar\n\nconstl : {l : Level}{I : Set l}(X : Set l) -> IDescl I\nconstl {x} X = con (lc , X)\n where lc : DescDConst {l = suc x}\n lc = lconst\n\nprodl : {l : Level}{I : Set l}(D D' : IDescl I) -> IDescl I\nprodl {x} D D' = con (lp , (D , D'))\n where lp : DescDConst {l = suc x}\n lp = lprod\n\n\npil : {l : Level}{I : Set l}(S : Set l)(T : S -> IDescl I) -> IDescl I\npil {x} S T = con (lp , ( S , Tl))\n where lp : DescDConst {l = suc x}\n lp = lpi\n Tl : Lifted S -> IDescl _\n Tl (lifter s) = T s\n\nsigmal : {l : Level}{I : Set l}(S : Set l)(T : S -> IDescl I) -> IDescl I\nsigmal {x} S T = con (ls , ( S , Tl))\n where ls : DescDConst {l = suc x}\n ls = lsigma\n Tl : Lifted S -> IDescl _\n Tl (lifter s) = T s\n \n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {l : Level}\n {I : Set l}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var (unlift i)\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S (\\s -> hs (lifter s))\ncases ( lsigma , ( S , T ) ) hs = sigma S (\\s -> hs (lifter s))\n\nphi : {l : Level}{I : Set l} -> IDescl I -> IDesc I\nphi {x} {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {l : Level}{I : Set l} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {l : Level}{I : Set l} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi {x} (pi S T) = cong (pi S) \n (reflFun (\\s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S)\n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n-- From embedding to embedding\n\nproof-lift-unlift-eq : {l : Level}{A : Set l}(x : Lifted A) -> lifter (unlift x) == x\nproof-lift-unlift-eq (lifter a) = refl\n\nproof-psi-phi : {l : Level}(I : Set l) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi {x} I D = induction (\\ _ -> descD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> descD I)) -> Set (suc x)\n P ( Void , D ) = psi (phi D) == D\n lvar' : DescDConst {l = suc x}\n lvar' = lvar\n lpi' : DescDConst {l = suc x}\n lpi' = lpi\n lsigma' : DescDConst {l = suc x}\n lsigma' = lsigma\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (descD I) (IDescl0 I))\n (hs : desc (box (descD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = cong (\\t -> con (lvar' , t)) \n (proof-lift-unlift-eq i)\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (\\t -> con ((lpi' , ((S , \\ s -> t s))))) \n (reflFun (\\s -> psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = {!!} {- cong (\\t -> con (lsigma' , ( S , \\s -> t (unlift s) ))) \n (reflFun (\\s -> psi (phi (T (lifter s)))) \n (\\s -> T (lifter s)) \n (\\s -> hs (lifter s))) -}","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\nunlift : {l : Level}{A : Set l} -> Lifted A -> A\nunlift (lifter a) = a\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l : Level}{A B : Set l}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l : Level}{A B C : Set l}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {l : Level}{A B : Set l}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set l) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set l} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set l}(R : I -> IDesc I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set l}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set l -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const (lift I)\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : {l : Level}(I : Set l) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : {l : Level}(I : Set l) -> Set (suc l)\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : {l : Level}{I : Set l}(i : I) -> IDescl I\nvarl {x} i = con (lv , lifter i) \n where lv : DescDConst {l = suc x}\n lv = lvar\n\nconstl : {l : Level}{I : Set l}(X : Set l) -> IDescl I\nconstl {x} X = con (lc , X)\n where lc : DescDConst {l = suc x}\n lc = lconst\n\nprodl : {l : Level}{I : Set l}(D D' : IDescl I) -> IDescl I\nprodl {x} D D' = con (lp , (D , D'))\n where lp : DescDConst {l = suc x}\n lp = lprod\n\n\npil : {l : Level}{I : Set l}(S : Set l)(T : S -> IDescl I) -> IDescl I\npil {x} S T = con (lp , ( S , Tl))\n where lp : DescDConst {l = suc x}\n lp = lpi\n Tl : Lifted S -> IDescl _\n Tl (lifter s) = T s\n\nsigmal : {l : Level}{I : Set l}(S : Set l)(T : S -> IDescl I) -> IDescl I\nsigmal {x} S T = con (ls , ( S , Tl))\n where ls : DescDConst {l = suc x}\n ls = lsigma\n Tl : Lifted S -> IDescl _\n Tl (lifter s) = T s\n \n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {l : Level}\n {I : Set l}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var (unlift i)\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S (\\s -> hs (lifter s))\ncases ( lsigma , ( S , T ) ) hs = sigma S (\\s -> hs (lifter s))\n\nphi : {l : Level}{I : Set l} -> IDescl I -> IDesc I\nphi {x} {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {l : Level}{I : Set l} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"4ed02027800055ba1e71c3b4722146c588db6259","subject":"Instantiate (Type Atlas-type) at module import.","message":"Instantiate (Type Atlas-type) at module import.\n\nOld-commit-hash: 37cd45916c0d36a09e68e7828338aab353d4f404\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Language\/Atlas.agda","new_file":"Syntax\/Language\/Atlas.agda","new_contents":"module Syntax.Language.Atlas where\n\n-- Base types of the calculus Atlas\n-- to be used with Plotkin-style language description\n--\n-- Atlas supports maps with neutral elements. The change type to\n-- `Map \u03ba \u03b9` is `Map \u03ba \u0394\u03b9`, where \u0394\u03b9 is the change type of the\n-- ground type \u03b9. Such a change to maps can support insertions\n-- and deletions as well: Inserting `k -> v` means mapping `k` to\n-- the change from the neutral element to `v`, and deleting\n-- `k -> v` means mapping `k` to the change from `v` to the\n-- neutral element.\n\ndata Atlas-type : Set where\n Bool : Atlas-type\n Map : (\u03ba : Atlas-type) (\u03b9 : Atlas-type) \u2192 Atlas-type\n\nopen import Syntax.Type.Plotkin Atlas-type\n\ndata Atlas-const : Type \u2192 Set where\n true : Atlas-const\n (base Bool)\n\n false : Atlas-const\n (base Bool)\n\n xor : Atlas-const\n (base Bool \u21d2 base Bool \u21d2 base Bool)\n\n empty : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base (Map \u03ba \u03b9))\n\n -- `update key val my-map` would\n -- - insert if `key` is not present in `my-map`\n -- - delete if `val` is the neutral element\n -- - make an update otherwise\n\n update : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base \u03ba \u21d2 base \u03b9 \u21d2 base (Map \u03ba \u03b9) \u21d2 base (Map \u03ba \u03b9))\n\n lookup : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base \u03ba \u21d2 base (Map \u03ba \u03b9) \u21d2 base \u03b9)\n\n -- Model of zip = Haskell Data.List.zipWith\n --\n -- zipWith :: (a \u2192 b \u2192 c) \u2192 [a] \u2192 [b] \u2192 [c]\n --\n -- Behavioral difference: all key-value pairs present\n -- in *either* (m\u2081 : Map \u03ba a) *or* (m\u2082 : Map \u03ba b) will\n -- be iterated over. Neutral element of type `a` or `b`\n -- will be supplied if the key is missing in the\n -- corresponding map.\n\n zip : \u2200 {\u03ba a b c : Atlas-type} \u2192 Atlas-const\n ((base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u21d2\n base (Map \u03ba a) \u21d2 base (Map \u03ba b) \u21d2 base (Map \u03ba c))\n\n -- Model of fold = Haskell Data.Map.foldWithKey\n --\n -- foldWithKey :: (k \u2192 a \u2192 b \u2192 b) \u2192 b \u2192 Map k a \u2192 b\n\n fold : \u2200 {\u03ba a b : Atlas-type} \u2192 Atlas-const\n ((base \u03ba \u21d2 base a \u21d2 base b \u21d2 base b) \u21d2\n base b \u21d2 base (Map \u03ba a) \u21d2 base b)\n\nAtlas-\u0394base : Atlas-type \u2192 Atlas-type\n-- change to a boolean is a xor-rand\nAtlas-\u0394base Bool = Bool\n-- change to a map is change to its values\nAtlas-\u0394base (Map key val) = (Map key (Atlas-\u0394base val))\n\nAtlas-\u0394type : Type \u2192 Type\nAtlas-\u0394type = lift-\u0394type\u2080 Atlas-\u0394base\n\nopen import Syntax.Context\n\nAtlas-context : Set\nAtlas-context = Context {Type}\n\nopen import Syntax.Term.Plotkin\n\nAtlas-term : Atlas-context \u2192 Type \u2192 Set\nAtlas-term = Term {Atlas-type} {Atlas-const}\n\n-- Shorthands of constants\n--\n-- There's probably a uniform way to lift constants\n-- into term constructors.\n\ntrue! : \u2200 {\u0393} \u2192\n Atlas-term \u0393 (base Bool)\ntrue! = const true\n\nfalse! : \u2200 {\u0393} \u2192\n Atlas-term \u0393 (base Bool)\nfalse! = const false\n\nxor! : \u2200 {\u0393} \u2192\n Atlas-term \u0393 (base Bool) \u2192 Atlas-term \u0393 (base Bool) \u2192\n Atlas-term \u0393 (base Bool)\nxor! = app\u2082 (const xor)\n\nempty! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9))\nempty! = const empty\n\nupdate! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9))\nupdate! = app\u2083 (const update)\n\nlookup! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Atlas-term \u0393 (base \u03b9)\nlookup! = app\u2082 (const lookup)\n\nzip! : \u2200 {\u03ba a b c \u0393} \u2192\n Atlas-term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u2192\n Atlas-term \u0393 (base (Map \u03ba a)) \u2192 Atlas-term \u0393 (base (Map \u03ba b)) \u2192\n Atlas-term \u0393 (base (Map \u03ba c))\nzip! = app\u2083 (const zip)\n\nfold! : \u2200 {\u03ba a b \u0393} \u2192\n Atlas-term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base b) \u2192\n Atlas-term \u0393 (base b) \u2192 Atlas-term \u0393 (base (Map \u03ba a)) \u2192\n Atlas-term \u0393 (base b)\nfold! = app\u2083 (const fold)\n\n-- Every base type has a known nil-change.\n-- The nil-change of \u03b9 is also the neutral element of Map \u03ba \u0394\u03b9.\n\nneutral : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const (base \u03b9)\nneutral {Bool} = false\nneutral {Map \u03ba \u03b9} = empty {\u03ba} {\u03b9}\n\nneutral-term : \u2200 {\u03b9 \u0393} \u2192 Atlas-term \u0393 (base \u03b9)\nneutral-term {Bool} = const (neutral {Bool})\nneutral-term {Map \u03ba \u03b9} = const (neutral {Map \u03ba \u03b9})\n\nnil-const : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const (base (Atlas-\u0394base \u03b9))\nnil-const {\u03b9} = neutral {Atlas-\u0394base \u03b9}\n\nnil-term : \u2200 {\u03b9 \u0393} \u2192 Atlas-term \u0393 (base (Atlas-\u0394base \u03b9))\nnil-term {Bool} = const (nil-const {Bool})\nnil-term {Map \u03ba \u03b9} = const (nil-const {Map \u03ba \u03b9})\n\n-- Nonfunctional products can be encoded.\n-- The incremental behavior of products thus encoded is weird:\n-- \u0394(\u03b1 \u00d7 \u03b2) = \u03b1 \u00d7 \u0394\u03b2\nPair : Atlas-type \u2192 Atlas-type \u2192 Atlas-type\nPair \u03b1 \u03b2 = Map \u03b1 \u03b2\n\npair : \u2200 {\u03b1 \u03b2 \u0393} \u2192\n Atlas-term \u0393 (base \u03b1) \u2192 Atlas-term \u0393 (base \u03b2) \u2192\n Atlas-term \u0393 (base (Pair \u03b1 \u03b2))\npair s t = update! s t empty!\n\npair-term : \u2200 {\u03b1 \u03b2 \u0393} \u2192\n Atlas-term \u0393 (base \u03b1 \u21d2 base \u03b2 \u21d2 base (Pair \u03b1 \u03b2))\npair-term = abs (abs (pair (var (that this)) (var this)))\n\nuncurry : \u2200 {\u03b1 \u03b2 \u03b3 \u0393} \u2192\n Atlas-term \u0393 (base \u03b1 \u21d2 base \u03b2 \u21d2 base \u03b3) \u2192\n Atlas-term \u0393 (base (Pair \u03b1 \u03b2)) \u2192\n Atlas-term \u0393 (base \u03b3)\nuncurry f p =\n let\n a = var (that (that this))\n b = var (that this)\n g = abs (abs (abs (app\u2082 (weaken\u2083 f) a b)))\n in\n fold! g neutral-term p\n\nzip-pair : \u2200 {\u03ba a b \u0393} \u2192\n Atlas-term \u0393 (base (Map \u03ba a)) \u2192 Atlas-term \u0393 (base (Map \u03ba b)) \u2192\n Atlas-term \u0393 (base (Map \u03ba (Pair a b)))\nzip-pair = zip! (abs pair-term)\n\n-- diff-term and apply-term\n\n-- b\u2080 \u229d b\u2081 = b\u2080 xor b\u2081\n-- m\u2080 \u229d m\u2081 = zip _\u229d_ m\u2080 m\u2081\n\nAtlas-diff : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 Atlas-\u0394type (base \u03b9))\nAtlas-diff {Bool} = const xor\nAtlas-diff {Map \u03ba \u03b9} = app (const zip) (abs Atlas-diff)\n\n-- b \u2295 \u0394b = b xor \u0394b\n-- m \u2295 \u0394m = zip _\u2295_ m \u0394m\n\nAtlas-apply : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\nAtlas-apply {Bool} = const xor\nAtlas-apply {Map \u03ba \u03b9} = app (const zip) (abs Atlas-apply)\n\n-- Shorthands for working with diff-term and apply-term\n\ndiff : \u2200 {\u03c4 \u0393} \u2192\n Atlas-term \u0393 \u03c4 \u2192 Atlas-term \u0393 \u03c4 \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4)\ndiff = app\u2082 (lift-diff Atlas-diff Atlas-apply)\n\napply : \u2200 {\u03c4 \u0393} \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4) \u2192 Atlas-term \u0393 \u03c4 \u2192\n Atlas-term \u0393 \u03c4\napply = app\u2082 (lift-apply Atlas-diff Atlas-apply)\n\n-- Shorthands for creating changes corresponding to\n-- insertion\/deletion.\n\ninsert : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n -- last argument is the change accumulator\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ndelete : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ninsert k v acc = update! k (diff v neutral-term) acc\ndelete k v acc = update! k (diff neutral-term v) acc\n\n-- Shorthand for 4-way zip\nzip4! : \u2200 {\u03ba a b c d e \u0393} \u2192\n let\n t:_ = \u03bb \u03b9 \u2192 Atlas-term \u0393 (base \u03b9)\n in\n Atlas-term \u0393\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c \u21d2 base d \u21d2 base e) \u2192\n t: Map \u03ba a \u2192 t: Map \u03ba b \u2192 t: Map \u03ba c \u2192 t: Map \u03ba d \u2192 t: Map \u03ba e\n\n-- zip\u2084 f m\u2081 m\u2082 m\u2083 m\u2084 =\n-- zip (\u03bb k p\u2081\u2082 p\u2083\u2084 \u2192 uncurry (\u03bb v\u2081 v\u2082 \u2192 uncurry (\u03bb v\u2083 v\u2084 \u2192\n-- f k v\u2081 v\u2082 v\u2083 v\u2084)\n-- p\u2083\u2084) p\u2081\u2082)\n-- (zip-pair m\u2081 m\u2082) (zip-pair m\u2083 m\u2084)\n\nzip4! f m\u2081 m\u2082 m\u2083 m\u2084 =\n let\n f\u2032 = weaken\u2083 (weaken\u2083 (weaken\u2081 f))\n k = var (that (that (that (that (that (that this))))))\n p\u2081\u2082 = var (that this)\n p\u2083\u2084 = var (that (that this))\n v\u2081 = var (that (that (that this)))\n v\u2082 = var (that (that this))\n v\u2083 = var (that this)\n v\u2084 = var this\n g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs\n (app\u2085 f\u2032 k v\u2081 v\u2082 v\u2083 v\u2084)))\n p\u2083\u2084))) p\u2081\u2082)))\n in\n zip! g (zip-pair m\u2081 m\u2082) (zip-pair m\u2083 m\u2084)\n\n-- Type signature of Atlas-\u0394const is boilerplate.\nAtlas-\u0394const : \u2200 {\u0393 \u03c4} \u2192 (c : Atlas-const \u03c4) \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4)\n\nAtlas-\u0394const true = false!\nAtlas-\u0394const false = false!\n\n-- \u0394xor = \u03bb x \u0394x y \u0394y \u2192 \u0394x xor \u0394y\nAtlas-\u0394const xor =\n let\n \u0394x = var (that (that this))\n \u0394y = var this\n in abs (abs (abs (abs (xor! \u0394x \u0394y))))\n\nAtlas-\u0394const empty = empty!\n\n-- If k \u2295 \u0394k \u2261 k, then\n-- \u0394update k \u0394k v \u0394v m \u0394m = update k \u0394v \u0394m\n-- else it is a deletion followed by insertion:\n-- \u0394update k \u0394k v \u0394v m \u0394m =\n-- insert (k \u2295 \u0394k) (v \u2295 \u0394v) (delete k v \u0394m)\n--\n-- We implement the else-branch only for the moment.\nAtlas-\u0394const update =\n let\n k = var (that (that (that (that (that this)))))\n \u0394k = var (that (that (that (that this))))\n v = var (that (that (that this)))\n \u0394v = var (that (that this))\n -- m = var (that this) -- unused parameter\n \u0394m = var this\n in\n abs (abs (abs (abs (abs (abs\n (insert (apply \u0394k k) (apply \u0394v v)\n (delete k v \u0394m)))))))\n\n-- \u0394lookup k \u0394k m \u0394m | true? (k \u2295 \u0394k \u2261 k)\n-- ... | true = lookup k \u0394m\n-- ... | false =\n-- (lookup (k \u2295 \u0394k) m \u2295 lookup (k \u2295 \u0394k) \u0394m)\n-- \u229d lookup k m\n--\n-- Only the false-branch is implemented.\nAtlas-\u0394const lookup =\n let\n k = var (that (that (that this)))\n \u0394k = var (that (that this))\n m = var (that this)\n \u0394m = var this\n k\u2032 = apply \u0394k k\n in\n abs (abs (abs (abs\n (diff (apply (lookup! k\u2032 \u0394m) (lookup! k\u2032 m))\n (lookup! k m)))))\n\n-- \u0394zip f \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082 | true? (f \u2295 \u0394f \u2261 f)\n--\n-- ... | true =\n-- zip (\u03bb k \u0394v\u2081 \u0394v\u2082 \u2192 \u0394f (lookup k m\u2081) \u0394v\u2081 (lookup k m\u2082) \u0394v\u2082)\n-- \u0394m\u2081 \u0394m\u2082\n--\n-- ... | false = zip\u2084 \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082\n--\n-- we implement the false-branch for the moment.\nAtlas-\u0394const zip =\n let\n \u0394f = var (that (that (that (that this))))\n m\u2081 = var (that (that (that this)))\n \u0394m\u2081 = var (that (that this))\n m\u2082 = var (that this)\n \u0394m\u2082 = var this\n g = abs (app\u2082 (weaken\u2081 \u0394f) (var this) nil-term)\n in\n abs (abs (abs (abs (abs (abs (zip4! g m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082))))))\n\n-- \u0394fold f \u0394f z \u0394z m \u0394m = proj\u2082\n-- (fold (\u03bb k [a,\u0394a] [b,\u0394b] \u2192\n-- uncurry (\u03bb a \u0394a \u2192 uncurry (\u03bb b \u0394b \u2192\n-- pair (f k a b) (\u0394f k nil a \u0394a b \u0394b))\n-- [b,\u0394b]) [a,\u0394a])\n-- (pair z \u0394z)\n-- (zip-pair m \u0394m))\n--\n-- \u0394fold is efficient only if evaluation is lazy and \u0394f is\n-- self-maintainable: it doesn't look at the argument\n-- (b = fold f k a b\u2080) at all.\nAtlas-\u0394const (fold {\u03ba} {\u03b1} {\u03b2}) =\n let -- TODO (tedius): write weaken\u2087\n f = weaken\u2083 (weaken\u2083 (weaken\u2081\n (var (that (that (that (that (that this))))))))\n \u0394f = weaken\u2083 (weaken\u2083 (weaken\u2081\n (var (that (that (that (that this)))))))\n z = var (that (that (that this)))\n \u0394z = var (that (that this))\n m = var (that this)\n \u0394m = var this\n k = weaken\u2083 (weaken\u2081 (var (that (that this))))\n [a,\u0394a] = var (that this)\n [b,\u0394b] = var this\n a = var (that (that (that this)))\n \u0394a = var (that (that this))\n b = var (that this)\n \u0394b = var this\n g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs\n (pair (app\u2083 f k a b)\n (app\u2086 \u0394f k nil-term a \u0394a b \u0394b))))\n (weaken\u2082 [b,\u0394b])))) [a,\u0394a])))\n proj\u2082 = uncurry (abs (abs (var this)))\n in\n abs (abs (abs (abs (abs (abs\n (proj\u2082 (fold! g (pair z \u0394z) (zip-pair m \u0394m))))))))\n\nopen import Syntax.Language.Calculus\n\nAtlas = calculus-with\n Atlas-type\n Atlas-const\n Atlas-\u0394type\n Atlas-\u0394const\n","old_contents":"module Syntax.Language.Atlas where\n\n-- Base types of the calculus Atlas\n-- to be used with Plotkin-style language description\n--\n-- Atlas supports maps with neutral elements. The change type to\n-- `Map \u03ba \u03b9` is `Map \u03ba \u0394\u03b9`, where \u0394\u03b9 is the change type of the\n-- ground type \u03b9. Such a change to maps can support insertions\n-- and deletions as well: Inserting `k -> v` means mapping `k` to\n-- the change from the neutral element to `v`, and deleting\n-- `k -> v` means mapping `k` to the change from `v` to the\n-- neutral element.\n\ndata Atlas-type : Set where\n Bool : Atlas-type\n Map : (\u03ba : Atlas-type) (\u03b9 : Atlas-type) \u2192 Atlas-type\n\nopen import Syntax.Type.Plotkin\n\ndata Atlas-const : Type Atlas-type \u2192 Set where\n true : Atlas-const\n (base Bool)\n\n false : Atlas-const\n (base Bool)\n\n xor : Atlas-const\n (base Bool \u21d2 base Bool \u21d2 base Bool)\n\n empty : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base (Map \u03ba \u03b9))\n\n -- `update key val my-map` would\n -- - insert if `key` is not present in `my-map`\n -- - delete if `val` is the neutral element\n -- - make an update otherwise\n\n update : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base \u03ba \u21d2 base \u03b9 \u21d2 base (Map \u03ba \u03b9) \u21d2 base (Map \u03ba \u03b9))\n\n lookup : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base \u03ba \u21d2 base (Map \u03ba \u03b9) \u21d2 base \u03b9)\n\n -- Model of zip = Haskell Data.List.zipWith\n --\n -- zipWith :: (a \u2192 b \u2192 c) \u2192 [a] \u2192 [b] \u2192 [c]\n --\n -- Behavioral difference: all key-value pairs present\n -- in *either* (m\u2081 : Map \u03ba a) *or* (m\u2082 : Map \u03ba b) will\n -- be iterated over. Neutral element of type `a` or `b`\n -- will be supplied if the key is missing in the\n -- corresponding map.\n\n zip : \u2200 {\u03ba a b c : Atlas-type} \u2192 Atlas-const\n ((base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u21d2\n base (Map \u03ba a) \u21d2 base (Map \u03ba b) \u21d2 base (Map \u03ba c))\n\n -- Model of fold = Haskell Data.Map.foldWithKey\n --\n -- foldWithKey :: (k \u2192 a \u2192 b \u2192 b) \u2192 b \u2192 Map k a \u2192 b\n\n fold : \u2200 {\u03ba a b : Atlas-type} \u2192 Atlas-const\n ((base \u03ba \u21d2 base a \u21d2 base b \u21d2 base b) \u21d2\n base b \u21d2 base (Map \u03ba a) \u21d2 base b)\n\nAtlas-\u0394base : Atlas-type \u2192 Atlas-type\n-- change to a boolean is a xor-rand\nAtlas-\u0394base Bool = Bool\n-- change to a map is change to its values\nAtlas-\u0394base (Map key val) = (Map key (Atlas-\u0394base val))\n\nAtlas-\u0394type : Type Atlas-type \u2192 Type Atlas-type\nAtlas-\u0394type = lift-\u0394type\u2080 _ Atlas-\u0394base\n\nopen import Syntax.Context\n\nAtlas-context : Set\nAtlas-context = Context {Type Atlas-type}\n\nopen import Syntax.Term.Plotkin\n\nAtlas-term : Atlas-context \u2192 Type Atlas-type \u2192 Set\nAtlas-term = Term {Atlas-type} {Atlas-const}\n\n-- Shorthands of constants\n--\n-- There's probably a uniform way to lift constants\n-- into term constructors.\n\ntrue! : \u2200 {\u0393} \u2192\n Atlas-term \u0393 (base Bool)\ntrue! = const true\n\nfalse! : \u2200 {\u0393} \u2192\n Atlas-term \u0393 (base Bool)\nfalse! = const false\n\nxor! : \u2200 {\u0393} \u2192\n Atlas-term \u0393 (base Bool) \u2192 Atlas-term \u0393 (base Bool) \u2192\n Atlas-term \u0393 (base Bool)\nxor! = app\u2082 (const xor)\n\nempty! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9))\nempty! = const empty\n\nupdate! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9))\nupdate! = app\u2083 (const update)\n\nlookup! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Atlas-term \u0393 (base \u03b9)\nlookup! = app\u2082 (const lookup)\n\nzip! : \u2200 {\u03ba a b c \u0393} \u2192\n Atlas-term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u2192\n Atlas-term \u0393 (base (Map \u03ba a)) \u2192 Atlas-term \u0393 (base (Map \u03ba b)) \u2192\n Atlas-term \u0393 (base (Map \u03ba c))\nzip! = app\u2083 (const zip)\n\nfold! : \u2200 {\u03ba a b \u0393} \u2192\n Atlas-term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base b) \u2192\n Atlas-term \u0393 (base b) \u2192 Atlas-term \u0393 (base (Map \u03ba a)) \u2192\n Atlas-term \u0393 (base b)\nfold! = app\u2083 (const fold)\n\n-- Every base type has a known nil-change.\n-- The nil-change of \u03b9 is also the neutral element of Map \u03ba \u0394\u03b9.\n\nneutral : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const (base \u03b9)\nneutral {Bool} = false\nneutral {Map \u03ba \u03b9} = empty {\u03ba} {\u03b9}\n\nneutral-term : \u2200 {\u03b9 \u0393} \u2192 Atlas-term \u0393 (base \u03b9)\nneutral-term {Bool} = const (neutral {Bool})\nneutral-term {Map \u03ba \u03b9} = const (neutral {Map \u03ba \u03b9})\n\nnil-const : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const (base (Atlas-\u0394base \u03b9))\nnil-const {\u03b9} = neutral {Atlas-\u0394base \u03b9}\n\nnil-term : \u2200 {\u03b9 \u0393} \u2192 Atlas-term \u0393 (base (Atlas-\u0394base \u03b9))\nnil-term {Bool} = const (nil-const {Bool})\nnil-term {Map \u03ba \u03b9} = const (nil-const {Map \u03ba \u03b9})\n\n-- Nonfunctional products can be encoded.\n-- The incremental behavior of products thus encoded is weird:\n-- \u0394(\u03b1 \u00d7 \u03b2) = \u03b1 \u00d7 \u0394\u03b2\nPair : Atlas-type \u2192 Atlas-type \u2192 Atlas-type\nPair \u03b1 \u03b2 = Map \u03b1 \u03b2\n\npair : \u2200 {\u03b1 \u03b2 \u0393} \u2192\n Atlas-term \u0393 (base \u03b1) \u2192 Atlas-term \u0393 (base \u03b2) \u2192\n Atlas-term \u0393 (base (Pair \u03b1 \u03b2))\npair s t = update! s t empty!\n\npair-term : \u2200 {\u03b1 \u03b2 \u0393} \u2192\n Atlas-term \u0393 (base \u03b1 \u21d2 base \u03b2 \u21d2 base (Pair \u03b1 \u03b2))\npair-term = abs (abs (pair (var (that this)) (var this)))\n\nuncurry : \u2200 {\u03b1 \u03b2 \u03b3 \u0393} \u2192\n Atlas-term \u0393 (base \u03b1 \u21d2 base \u03b2 \u21d2 base \u03b3) \u2192\n Atlas-term \u0393 (base (Pair \u03b1 \u03b2)) \u2192\n Atlas-term \u0393 (base \u03b3)\nuncurry f p =\n let\n a = var (that (that this))\n b = var (that this)\n g = abs (abs (abs (app\u2082 (weaken\u2083 f) a b)))\n in\n fold! g neutral-term p\n\nzip-pair : \u2200 {\u03ba a b \u0393} \u2192\n Atlas-term \u0393 (base (Map \u03ba a)) \u2192 Atlas-term \u0393 (base (Map \u03ba b)) \u2192\n Atlas-term \u0393 (base (Map \u03ba (Pair a b)))\nzip-pair = zip! (abs pair-term)\n\n-- diff-term and apply-term\n\n-- b\u2080 \u229d b\u2081 = b\u2080 xor b\u2081\n-- m\u2080 \u229d m\u2081 = zip _\u229d_ m\u2080 m\u2081\n\nAtlas-diff : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 Atlas-\u0394type (base \u03b9))\nAtlas-diff {Bool} = const xor\nAtlas-diff {Map \u03ba \u03b9} = app (const zip) (abs Atlas-diff)\n\n-- b \u2295 \u0394b = b xor \u0394b\n-- m \u2295 \u0394m = zip _\u2295_ m \u0394m\n\nAtlas-apply : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\nAtlas-apply {Bool} = const xor\nAtlas-apply {Map \u03ba \u03b9} = app (const zip) (abs Atlas-apply)\n\n-- Shorthands for working with diff-term and apply-term\n\ndiff : \u2200 {\u03c4 \u0393} \u2192\n Atlas-term \u0393 \u03c4 \u2192 Atlas-term \u0393 \u03c4 \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4)\ndiff = app\u2082 (lift-diff Atlas-diff Atlas-apply)\n\napply : \u2200 {\u03c4 \u0393} \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4) \u2192 Atlas-term \u0393 \u03c4 \u2192\n Atlas-term \u0393 \u03c4\napply = app\u2082 (lift-apply Atlas-diff Atlas-apply)\n\n-- Shorthands for creating changes corresponding to\n-- insertion\/deletion.\n\ninsert : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n -- last argument is the change accumulator\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ndelete : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ninsert k v acc = update! k (diff v neutral-term) acc\ndelete k v acc = update! k (diff neutral-term v) acc\n\n-- Shorthand for 4-way zip\nzip4! : \u2200 {\u03ba a b c d e \u0393} \u2192\n let\n t:_ = \u03bb \u03b9 \u2192 Atlas-term \u0393 (base \u03b9)\n in\n Atlas-term \u0393\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c \u21d2 base d \u21d2 base e) \u2192\n t: Map \u03ba a \u2192 t: Map \u03ba b \u2192 t: Map \u03ba c \u2192 t: Map \u03ba d \u2192 t: Map \u03ba e\n\n-- zip\u2084 f m\u2081 m\u2082 m\u2083 m\u2084 =\n-- zip (\u03bb k p\u2081\u2082 p\u2083\u2084 \u2192 uncurry (\u03bb v\u2081 v\u2082 \u2192 uncurry (\u03bb v\u2083 v\u2084 \u2192\n-- f k v\u2081 v\u2082 v\u2083 v\u2084)\n-- p\u2083\u2084) p\u2081\u2082)\n-- (zip-pair m\u2081 m\u2082) (zip-pair m\u2083 m\u2084)\n\nzip4! f m\u2081 m\u2082 m\u2083 m\u2084 =\n let\n f\u2032 = weaken\u2083 (weaken\u2083 (weaken\u2081 f))\n k = var (that (that (that (that (that (that this))))))\n p\u2081\u2082 = var (that this)\n p\u2083\u2084 = var (that (that this))\n v\u2081 = var (that (that (that this)))\n v\u2082 = var (that (that this))\n v\u2083 = var (that this)\n v\u2084 = var this\n g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs\n (app\u2085 f\u2032 k v\u2081 v\u2082 v\u2083 v\u2084)))\n p\u2083\u2084))) p\u2081\u2082)))\n in\n zip! g (zip-pair m\u2081 m\u2082) (zip-pair m\u2083 m\u2084)\n\n-- Type signature of Atlas-\u0394const is boilerplate.\nAtlas-\u0394const : \u2200 {\u0393 \u03c4} \u2192 (c : Atlas-const \u03c4) \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4)\n\nAtlas-\u0394const true = false!\nAtlas-\u0394const false = false!\n\n-- \u0394xor = \u03bb x \u0394x y \u0394y \u2192 \u0394x xor \u0394y\nAtlas-\u0394const xor =\n let\n \u0394x = var (that (that this))\n \u0394y = var this\n in abs (abs (abs (abs (xor! \u0394x \u0394y))))\n\nAtlas-\u0394const empty = empty!\n\n-- If k \u2295 \u0394k \u2261 k, then\n-- \u0394update k \u0394k v \u0394v m \u0394m = update k \u0394v \u0394m\n-- else it is a deletion followed by insertion:\n-- \u0394update k \u0394k v \u0394v m \u0394m =\n-- insert (k \u2295 \u0394k) (v \u2295 \u0394v) (delete k v \u0394m)\n--\n-- We implement the else-branch only for the moment.\nAtlas-\u0394const update =\n let\n k = var (that (that (that (that (that this)))))\n \u0394k = var (that (that (that (that this))))\n v = var (that (that (that this)))\n \u0394v = var (that (that this))\n -- m = var (that this) -- unused parameter\n \u0394m = var this\n in\n abs (abs (abs (abs (abs (abs\n (insert (apply \u0394k k) (apply \u0394v v)\n (delete k v \u0394m)))))))\n\n-- \u0394lookup k \u0394k m \u0394m | true? (k \u2295 \u0394k \u2261 k)\n-- ... | true = lookup k \u0394m\n-- ... | false =\n-- (lookup (k \u2295 \u0394k) m \u2295 lookup (k \u2295 \u0394k) \u0394m)\n-- \u229d lookup k m\n--\n-- Only the false-branch is implemented.\nAtlas-\u0394const lookup =\n let\n k = var (that (that (that this)))\n \u0394k = var (that (that this))\n m = var (that this)\n \u0394m = var this\n k\u2032 = apply \u0394k k\n in\n abs (abs (abs (abs\n (diff (apply (lookup! k\u2032 \u0394m) (lookup! k\u2032 m))\n (lookup! k m)))))\n\n-- \u0394zip f \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082 | true? (f \u2295 \u0394f \u2261 f)\n--\n-- ... | true =\n-- zip (\u03bb k \u0394v\u2081 \u0394v\u2082 \u2192 \u0394f (lookup k m\u2081) \u0394v\u2081 (lookup k m\u2082) \u0394v\u2082)\n-- \u0394m\u2081 \u0394m\u2082\n--\n-- ... | false = zip\u2084 \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082\n--\n-- we implement the false-branch for the moment.\nAtlas-\u0394const zip =\n let\n \u0394f = var (that (that (that (that this))))\n m\u2081 = var (that (that (that this)))\n \u0394m\u2081 = var (that (that this))\n m\u2082 = var (that this)\n \u0394m\u2082 = var this\n g = abs (app\u2082 (weaken\u2081 \u0394f) (var this) nil-term)\n in\n abs (abs (abs (abs (abs (abs (zip4! g m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082))))))\n\n-- \u0394fold f \u0394f z \u0394z m \u0394m = proj\u2082\n-- (fold (\u03bb k [a,\u0394a] [b,\u0394b] \u2192\n-- uncurry (\u03bb a \u0394a \u2192 uncurry (\u03bb b \u0394b \u2192\n-- pair (f k a b) (\u0394f k nil a \u0394a b \u0394b))\n-- [b,\u0394b]) [a,\u0394a])\n-- (pair z \u0394z)\n-- (zip-pair m \u0394m))\n--\n-- \u0394fold is efficient only if evaluation is lazy and \u0394f is\n-- self-maintainable: it doesn't look at the argument\n-- (b = fold f k a b\u2080) at all.\nAtlas-\u0394const (fold {\u03ba} {\u03b1} {\u03b2}) =\n let -- TODO (tedius): write weaken\u2087\n f = weaken\u2083 (weaken\u2083 (weaken\u2081\n (var (that (that (that (that (that this))))))))\n \u0394f = weaken\u2083 (weaken\u2083 (weaken\u2081\n (var (that (that (that (that this)))))))\n z = var (that (that (that this)))\n \u0394z = var (that (that this))\n m = var (that this)\n \u0394m = var this\n k = weaken\u2083 (weaken\u2081 (var (that (that this))))\n [a,\u0394a] = var (that this)\n [b,\u0394b] = var this\n a = var (that (that (that this)))\n \u0394a = var (that (that this))\n b = var (that this)\n \u0394b = var this\n g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs\n (pair (app\u2083 f k a b)\n (app\u2086 \u0394f k nil-term a \u0394a b \u0394b))))\n (weaken\u2082 [b,\u0394b])))) [a,\u0394a])))\n proj\u2082 = uncurry (abs (abs (var this)))\n in\n abs (abs (abs (abs (abs (abs\n (proj\u2082 (fold! g (pair z \u0394z) (zip-pair m \u0394m))))))))\n\nopen import Syntax.Language.Calculus\n\nAtlas = calculus-with\n Atlas-type\n Atlas-const\n Atlas-\u0394type\n Atlas-\u0394const\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"61a5152115a91d3d1aa7aae91b383785619dd89e","subject":"scraps","message":"scraps\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"complete-progress.agda","new_file":"complete-progress.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nopen import progress\nopen import htype-decidable\nopen import lemmas-complete\n\nmodule complete-progress where\n\n -- todo: explain this\n data okc : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n V : \u2200{d \u0394} \u2192 d val \u2192 okc d \u0394\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 okc d \u0394\n\n -- todo: lemma file if an arrow is disequal, it disagrees in the first or second argument\n ne-factor : \u2200{\u03c41 \u03c42 \u03c43 \u03c44} \u2192 (\u03c41 ==> \u03c42) \u2260 (\u03c43 ==> \u03c44) \u2192 (\u03c41 \u2260 \u03c43) + (\u03c42 \u2260 \u03c44)\n ne-factor {\u03c41} {\u03c42} {\u03c43} {\u03c44} ne with htype-dec \u03c41 \u03c43 | htype-dec \u03c42 \u03c44\n ne-factor ne | Inl refl | Inl refl = Inl (\u03bb x \u2192 ne refl)\n ne-factor ne | Inl x | Inr x\u2081 = Inr x\u2081\n ne-factor ne | Inr x | Inl x\u2081 = Inl x\n ne-factor ne | Inr x | Inr x\u2081 = Inl x\n\n complete-progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d dcomplete \u2192\n okc d \u0394\n complete-progress wt comp with progress wt\n complete-progress wt comp | I x = abort (lem-ind-comp comp x)\n complete-progress wt comp | S x = S x\n complete-progress wt comp | BV (BVVal x) = V x\n complete-progress wt (DCCast comp x\u2082 ()) | BV (BVHoleCast x x\u2081)\n complete-progress (TACast wt x) (DCCast comp x\u2083 x\u2084) | BV (BVArrCast x\u2081 x\u2082) = abort (x\u2081 (eq-complete-consist x\u2083 x\u2084 x))\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nopen import progress\nopen import htype-decidable\nopen import lemmas-complete\n\nmodule complete-progress where\n\n data okc : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n V : \u2200{d \u0394} \u2192 d val \u2192 okc d \u0394\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 okc d \u0394\n\n -- if an arrow is disequal, it disagrees in the first or second argument\n ne-factor : \u2200{\u03c41 \u03c42 \u03c43 \u03c44} \u2192 (\u03c41 ==> \u03c42) \u2260 (\u03c43 ==> \u03c44) \u2192 (\u03c41 \u2260 \u03c43) + (\u03c42 \u2260 \u03c44)\n ne-factor {\u03c41} {\u03c42} {\u03c43} {\u03c44} ne with htype-dec \u03c41 \u03c43 | htype-dec \u03c42 \u03c44\n ne-factor ne | Inl refl | Inl refl = Inl (\u03bb x \u2192 ne refl)\n ne-factor ne | Inl x | Inr x\u2081 = Inr x\u2081\n ne-factor ne | Inr x | Inl x\u2081 = Inl x\n ne-factor ne | Inr x | Inr x\u2081 = Inl x\n\n complete-progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d dcomplete \u2192\n okc d \u0394\n complete-progress wt comp with progress wt\n complete-progress wt comp | I x = abort (lem-ind-comp comp x)\n complete-progress wt comp | S x = S x\n complete-progress wt comp | BV (BVVal x) = V x\n complete-progress wt (DCCast comp x\u2082 ()) | BV (BVHoleCast x x\u2081)\n complete-progress (TACast wt x) (DCCast comp x\u2083 x\u2084) | BV (BVArrCast x\u2081 x\u2082) = abort (x\u2081 (eq-complete-consist x\u2083 x\u2084 x))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"45b8ba0a0ef2036716a3ecc0d5ba60c89c0b5209","subject":"README: also show flipbased-tree","message":"README: also show flipbased-tree\n","repos":"crypto-agda\/crypto-agda","old_file":"README.agda","new_file":"README.agda","new_contents":"module README where\n\nopen import composition-sem-sec-reduction\n\nopen import one-time-semantic-security\n\nopen import flat-funs\nopen import composable\nopen import vcomp\nopen import forkable\n\nopen import program-distance\nopen import diff\n\nopen import flipbased\nopen import flipbased-implem\nopen import flipbased-tree\n\nopen import bit-guessing-game\n\nopen import circuit\n","old_contents":"module README where\n\nopen import composition-sem-sec-reduction\n\nopen import one-time-semantic-security\n\nopen import flat-funs\nopen import composable\nopen import vcomp\nopen import forkable\n\nopen import program-distance\nopen import diff\n\nopen import flipbased\nopen import flipbased-implem\n\nopen import bit-guessing-game\n\nopen import circuit\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"dab62d30d5d25c4f6b0f1826882c53b0cc5440ae","subject":"Typo.","message":"Typo.\n\nIgnore-this: 9da2ea6ef1e7200ec7366497f70e13c\n\ndarcs-hash:20110212174745-3bd4e-7324e4e35a5f0260910ab9c48669f01a570ccb03.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"README.agda","new_file":"README.agda","new_contents":"------------------------------------------------------------------------------\n-- FOT (First Order Theories)\n------------------------------------------------------------------------------\n\n-- Code accompanying the paper \"Combining Automatic and Interactive\n-- Proof in First Order Theories of Combinators\" by Ana Bove,\n-- Peter Dybjer, and Andr\u00e9s Sicard-Ram\u00edrez.\n\n------------------------------------------------------------------------------\n\nmodule README where\n\n------------------------------------------------------------------------------\n-- Description\n------------------------------------------------------------------------------\n\n-- Examples of the formalization of first order theories showing the\n-- combination of interactive proofs with automatics proofs carried\n-- out by first order automatic theorem provers (ATPs).\n\n------------------------------------------------------------------------------\n-- Prerequisites\n------------------------------------------------------------------------------\n\n-- You can download the agda2atp tool (described in above paper) using\n-- darcs with the following command:\n\n-- $ darcs get http:\/\/patch-tag.com\/r\/asr\/agda2atp\n\n-- The agda2atp tool and the FOT code require a modified version of\n-- Agda. See agda2atp\/README.txt for the related instructions.\n\n------------------------------------------------------------------------------\n-- Use\n------------------------------------------------------------------------------\n\n-- Let's suppose you want to use the Metis ATP to prove the group\n-- theory properties stated in the module\n-- GroupTheory.PropertiesATP. First, you should type-check the module using\n-- Agda\n\n-- $ agda -isrc src\/GroupTheory\/PropertiesATP.agda\n\n-- Second, you call the agda2tool using the Metis ATP\n\n-- $ agda2atp -isrc --atp=metis src\/GroupTheory\/PropertiesATP.agda\n\n------------------------------------------------------------------------------\n-- First order theories\n------------------------------------------------------------------------------\n\n-- Conventions\n\n-- The following modules show the formalization of some first order\n-- theories. If the module's name ends in 'I' the module contains\n-- interactive proofs, if it ends in 'ATP' the module contains\n-- combined proofs, otherwise the module contains interactive proofs\n-- that are used by the combined proofs.\n\n------------------------------------------------------------------------------\n-- Distributive laws on a binary operation\n\n-- We prove the proposition 2 (task B) of [1]. Let _\u00b7_ be a\n-- left-associative binary operation, the task B consist in given the\n-- left and right distributive axioms\n\n-- \u2200 x y z \u2192 x \u2219 (y \u2219 z) \u2261 (x \u2219 y) \u2219 (x \u2219 z)\n-- \u2200 x y z \u2192 (x \u2219 y) \u2219 z \u2261 (x \u2219 z) \u2219 (y \u2219 z)\n\n-- to prove the theorem\n\n-- \u2200 u x y z \u2192 (x \u2219 y \u2219 (z \u2219 u)) \u2219\n-- (( x \u2219 y \u2219 ( z \u2219 u)) \u2219 (x \u2219 z \u2219 (y \u2219 u))) \u2261\n-- x \u2219 z \u2219 (y \u2219 u)\n\n-- [1] David Stanovsk\u00fd. Distributive groupoids are symmetrical-by-medial:\n-- An elementary proof. Commentations Mathematicae Universitatis\n-- Carolinae, 49(4):541\u2013546, 2008.\n\nopen import DistributiveLaws.TaskB-HalvedStepsATP\nopen import DistributiveLaws.TaskB-I\nopen import DistributiveLaws.TaskB-TopDownATP\n\n------------------------------------------------------------------------------\n-- Group theory\n\n-- We formalize the theory of groups using Agda postulates for\n-- the group axioms.\n\n-- Basic properties\nopen import GroupTheory.PropertiesATP\nopen import GroupTheory.PropertiesI\n\n-- Commutator properties\nopen import GroupTheory.Commutator.PropertiesATP\nopen import GroupTheory.Commutator.PropertiesI\n\n-- Abelian groups\nopen import GroupTheory.AbelianGroup.PropertiesATP\n\n------------------------------------------------------------------------------\n-- Logic\n\n-- Propositional logic\nopen import Logic.Propositional.PropertiesATP\nopen import Logic.Predicate.PropertiesI\n\n-- Predicate logic\nopen import Logic.Predicate.PropertiesATP\nopen import Logic.Predicate.PropertiesI\n\n------------------------------------------------------------------------------\n-- LTC\n\n-- Formalization of (a version of) Azcel's Logical Theory of constructions.\n\n-- LTC base\nopen import LTC.Base.Properties\nopen import LTC.Base.PropertiesATP\nopen import LTC.Base.PropertiesI\n\n-- Booleans\nopen import LTC.Data.Bool.PropertiesATP\nopen import LTC.Data.Bool.PropertiesI\n\n-- Lists\nopen import LTC.Data.List.PropertiesATP\nopen import LTC.Data.List.PropertiesI\n\n-- Naturals numbers: Common properties\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.PropertiesI\n\nopen import LTC.Data.Nat.PropertiesByInductionATP\nopen import LTC.Data.Nat.PropertiesByInductionI\n\n-- Naturals numbers: Divisibility relation\nopen import LTC.Data.Nat.Divisibility.PropertiesATP\nopen import LTC.Data.Nat.Divisibility.PropertiesI\n\n-- Naturals numbers: Induction\nopen import LTC.Data.Nat.Induction.LexicographicATP\nopen import LTC.Data.Nat.Induction.LexicographicI\nopen import LTC.Data.Nat.Induction.WellFoundedATP\nopen import LTC.Data.Nat.Induction.WellFoundedI\n\n-- Naturals numbers: Inequalites\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.Inequalities.PropertiesI\n\n-- Naturals numbers: List\nopen import LTC.Data.Nat.List.PropertiesATP\nopen import LTC.Data.Nat.List.PropertiesI\n\n-- Naturals numbers: Unary numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\nopen import LTC.Data.Nat.Unary.Inequalities.PropertiesATP\n\n-- The GCD algorithm\nopen import LTC.Program.GCD.ProofSpecificationATP\nopen import LTC.Program.GCD.ProofSpecificationI\n\n-- Burstall's sort list algorithm\nopen import LTC.Program.SortList.ProofSpecificationATP\nopen import LTC.Program.SortList.ProofSpecificationI\n\n------------------------------------------------------------------------------\n-- LTC-PCF\n\n-- Formalization of a version of Azcel's Logical Theory of constructions.\n\n-- Naturals numbers: Common properties\nopen import LTC-PCF.Data.Nat.PropertiesATP\nopen import LTC-PCF.Data.Nat.PropertiesI\n\n-- Naturals numbers: Divisibility relation\nopen import LTC-PCF.Data.Nat.Divisibility.Properties\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesATP\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesI\n\n-- Naturals numbers: Induction\nopen import LTC-PCF.Data.Nat.Induction.LexicographicATP\nopen import LTC-PCF.Data.Nat.Induction.LexicographicI\nopen import LTC-PCF.Data.Nat.Induction.WellFoundedATP\nopen import LTC-PCF.Data.Nat.Induction.WellFoundedI\n\n-- Naturals numbers: Inequalites\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesATP\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesI\n\n-- Naturals numbers: The recursive operator\nopen import LTC-PCF.Data.Nat.Rec.EquationsATP\nopen import LTC-PCF.Data.Nat.Rec.EquationsI\n\n-- The division algorithm\nopen import LTC-PCF.Program.Division.ProofSpecificationATP\nopen import LTC-PCF.Program.Division.ProofSpecificationI\n\n-- The GCD algorithm\nopen import LTC-PCF.Program.GCD.ProofSpecificationATP\nopen import LTC-PCF.Program.GCD.ProofSpecificationI\n\n------------------------------------------------------------------------------\n-- Peano arithmetic (PA)\n\n-- We write two formalizations of PA. In the axiomatic formalization\n-- we use Agda postulates for Peano's axioms. In the inductive\n-- formalization, we use Agda data types and primitive recursive\n-- functions.\n\n-- Axiomatic PA\nopen import PA.Axiomatic.PropertiesATP\nopen import PA.Axiomatic.PropertiesI\n\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityATP\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityI\n\n-- Inductive PA\nopen import PA.Inductive.Properties\nopen import PA.Inductive.PropertiesATP\nopen import PA.Inductive.PropertiesI\n\nopen import PA.Inductive.PropertiesByInduction\nopen import PA.Inductive.PropertiesByInductionATP\nopen import PA.Inductive.PropertiesByInductionI\n\n------------------------------------------------------------------------------\n-- ATPs failures\n------------------------------------------------------------------------------\n\n-- The ATPs (E, Equinox, Metis and Vampire) could not prove some\n-- theorems.\n\nopen import DistributiveLaws.TaskB-ATP\nopen import LTC-PCF.Program.GCD.EquationsATP\nopen import PA.Axiomatic.PropertiesATP\n\n------------------------------------------------------------------------------\n-- Agsy examples\n------------------------------------------------------------------------------\n\n-- We cannot import the Agsy examples because some modules contain\n-- unsolved metas, so see src\/Agsy\/README.txt\n","old_contents":"------------------------------------------------------------------------------\n-- FOT (First Order Theories)\n------------------------------------------------------------------------------\n\n-- Code accompanying the paper \"Combining Automatic and Interactive\n-- Proof in First Order Theories of Combinators\" by Ana Bove,\n-- Peter Dybjer, and Andr\u00e9s Sicard-Ram\u00edrez.\n\n------------------------------------------------------------------------------\n\nmodule README where\n\n------------------------------------------------------------------------------\n-- Description\n------------------------------------------------------------------------------\n\n-- Examples of the formalization of first order theories showing the\n-- combination of interactive proofs with automatics proofs carried\n-- out by first order automatic theorem provers (ATPs).\n\n------------------------------------------------------------------------------\n-- Prerequisites\n------------------------------------------------------------------------------\n\n-- You can download agda2atp tool (described in above paper) using darcs\n-- with the following command:\n\n-- $ darcs get http:\/\/patch-tag.com\/r\/asr\/agda2atp\n\n-- The agda2atp tool and the FOT code require a modified version of\n-- Agda. See agda2atp\/README.txt for the related instructions.\n\n------------------------------------------------------------------------------\n-- Use\n------------------------------------------------------------------------------\n\n-- Let's suppose you want to use the Metis ATP to prove the group\n-- theory properties stated in the module\n-- GroupTheory.PropertiesATP. First, you should type-check the module using\n-- Agda\n\n-- $ agda -isrc src\/GroupTheory\/PropertiesATP.agda\n\n-- Second, you call the agda2tool using the Metis ATP\n\n-- $ agda2atp -isrc --atp=metis src\/GroupTheory\/PropertiesATP.agda\n\n------------------------------------------------------------------------------\n-- First order theories\n------------------------------------------------------------------------------\n\n-- Conventions\n\n-- The following modules show the formalization of some first order\n-- theories. If the module's name ends in 'I' the module contains\n-- interactive proofs, if it ends in 'ATP' the module contains\n-- combined proofs, otherwise the module contains interactive proofs\n-- that are used by the combined proofs.\n\n------------------------------------------------------------------------------\n-- Distributive laws on a binary operation\n\n-- We prove the proposition 2 (task B) of [1]. Let _\u00b7_ be a\n-- left-associative binary operation, the task B consist in given the\n-- left and right distributive axioms\n\n-- \u2200 x y z \u2192 x \u2219 (y \u2219 z) \u2261 (x \u2219 y) \u2219 (x \u2219 z)\n-- \u2200 x y z \u2192 (x \u2219 y) \u2219 z \u2261 (x \u2219 z) \u2219 (y \u2219 z)\n\n-- to prove the theorem\n\n-- \u2200 u x y z \u2192 (x \u2219 y \u2219 (z \u2219 u)) \u2219\n-- (( x \u2219 y \u2219 ( z \u2219 u)) \u2219 (x \u2219 z \u2219 (y \u2219 u))) \u2261\n-- x \u2219 z \u2219 (y \u2219 u)\n\n-- [1] David Stanovsk\u00fd. Distributive groupoids are symmetrical-by-medial:\n-- An elementary proof. Commentations Mathematicae Universitatis\n-- Carolinae, 49(4):541\u2013546, 2008.\n\nopen import DistributiveLaws.TaskB-HalvedStepsATP\nopen import DistributiveLaws.TaskB-I\nopen import DistributiveLaws.TaskB-TopDownATP\n\n------------------------------------------------------------------------------\n-- Group theory\n\n-- We formalize the theory of groups using Agda postulates for\n-- the group axioms.\n\n-- Basic properties\nopen import GroupTheory.PropertiesATP\nopen import GroupTheory.PropertiesI\n\n-- Commutator properties\nopen import GroupTheory.Commutator.PropertiesATP\nopen import GroupTheory.Commutator.PropertiesI\n\n-- Abelian groups\nopen import GroupTheory.AbelianGroup.PropertiesATP\n\n------------------------------------------------------------------------------\n-- Logic\n\n-- Propositional logic\nopen import Logic.Propositional.PropertiesATP\nopen import Logic.Predicate.PropertiesI\n\n-- Predicate logic\nopen import Logic.Predicate.PropertiesATP\nopen import Logic.Predicate.PropertiesI\n\n------------------------------------------------------------------------------\n-- LTC\n\n-- Formalization of (a version of) Azcel's Logical Theory of constructions.\n\n-- LTC base\nopen import LTC.Base.Properties\nopen import LTC.Base.PropertiesATP\nopen import LTC.Base.PropertiesI\n\n-- Booleans\nopen import LTC.Data.Bool.PropertiesATP\nopen import LTC.Data.Bool.PropertiesI\n\n-- Lists\nopen import LTC.Data.List.PropertiesATP\nopen import LTC.Data.List.PropertiesI\n\n-- Naturals numbers: Common properties\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.PropertiesI\n\nopen import LTC.Data.Nat.PropertiesByInductionATP\nopen import LTC.Data.Nat.PropertiesByInductionI\n\n-- Naturals numbers: Divisibility relation\nopen import LTC.Data.Nat.Divisibility.PropertiesATP\nopen import LTC.Data.Nat.Divisibility.PropertiesI\n\n-- Naturals numbers: Induction\nopen import LTC.Data.Nat.Induction.LexicographicATP\nopen import LTC.Data.Nat.Induction.LexicographicI\nopen import LTC.Data.Nat.Induction.WellFoundedATP\nopen import LTC.Data.Nat.Induction.WellFoundedI\n\n-- Naturals numbers: Inequalites\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.Inequalities.PropertiesI\n\n-- Naturals numbers: List\nopen import LTC.Data.Nat.List.PropertiesATP\nopen import LTC.Data.Nat.List.PropertiesI\n\n-- Naturals numbers: Unary numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\nopen import LTC.Data.Nat.Unary.Inequalities.PropertiesATP\n\n-- The GCD algorithm\nopen import LTC.Program.GCD.ProofSpecificationATP\nopen import LTC.Program.GCD.ProofSpecificationI\n\n-- Burstall's sort list algorithm\nopen import LTC.Program.SortList.ProofSpecificationATP\nopen import LTC.Program.SortList.ProofSpecificationI\n\n------------------------------------------------------------------------------\n-- LTC-PCF\n\n-- Formalization of a version of Azcel's Logical Theory of constructions.\n\n-- Naturals numbers: Common properties\nopen import LTC-PCF.Data.Nat.PropertiesATP\nopen import LTC-PCF.Data.Nat.PropertiesI\n\n-- Naturals numbers: Divisibility relation\nopen import LTC-PCF.Data.Nat.Divisibility.Properties\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesATP\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesI\n\n-- Naturals numbers: Induction\nopen import LTC-PCF.Data.Nat.Induction.LexicographicATP\nopen import LTC-PCF.Data.Nat.Induction.LexicographicI\nopen import LTC-PCF.Data.Nat.Induction.WellFoundedATP\nopen import LTC-PCF.Data.Nat.Induction.WellFoundedI\n\n-- Naturals numbers: Inequalites\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesATP\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesI\n\n-- Naturals numbers: The recursive operator\nopen import LTC-PCF.Data.Nat.Rec.EquationsATP\nopen import LTC-PCF.Data.Nat.Rec.EquationsI\n\n-- The division algorithm\nopen import LTC-PCF.Program.Division.ProofSpecificationATP\nopen import LTC-PCF.Program.Division.ProofSpecificationI\n\n-- The GCD algorithm\nopen import LTC-PCF.Program.GCD.ProofSpecificationATP\nopen import LTC-PCF.Program.GCD.ProofSpecificationI\n\n------------------------------------------------------------------------------\n-- Peano arithmetic (PA)\n\n-- We write two formalizations of PA. In the axiomatic formalization\n-- we use Agda postulates for Peano's axioms. In the inductive\n-- formalization, we use Agda data types and primitive recursive\n-- functions.\n\n-- Axiomatic PA\nopen import PA.Axiomatic.PropertiesATP\nopen import PA.Axiomatic.PropertiesI\n\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityATP\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityI\n\n-- Inductive PA\nopen import PA.Inductive.Properties\nopen import PA.Inductive.PropertiesATP\nopen import PA.Inductive.PropertiesI\n\nopen import PA.Inductive.PropertiesByInduction\nopen import PA.Inductive.PropertiesByInductionATP\nopen import PA.Inductive.PropertiesByInductionI\n\n------------------------------------------------------------------------------\n-- ATPs failures\n------------------------------------------------------------------------------\n\n-- The ATPs (E, Equinox, Metis and Vampire) could not prove some\n-- theorems.\n\nopen import DistributiveLaws.TaskB-ATP\nopen import LTC-PCF.Program.GCD.EquationsATP\nopen import PA.Axiomatic.PropertiesATP\n\n------------------------------------------------------------------------------\n-- Agsy examples\n------------------------------------------------------------------------------\n\n-- We cannot import the Agsy examples because some modules contain\n-- unsolved metas, so see src\/Agsy\/README.txt\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"509e5de975b6eee17da4b6baeb32823e1688e23c","subject":"Added x+1+y\u22611+x+y (ER version).","message":"Added x+1+y\u22611+x+y (ER version).\n\nIgnore-this: e099f22302e7b85516d7b432f9309d04\n\ndarcs-hash:20100531140940-3bd4e-8dfca741032e39e7dabeb662da649d5f5a74d439.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Function\/Arithmetic\/PropertiesER.agda","new_file":"LTC\/Function\/Arithmetic\/PropertiesER.agda","new_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties using equational reasoning\n------------------------------------------------------------------------------\n\nmodule LTC.Function.Arithmetic.PropertiesER where\n\nopen import LTC.Minimal\nopen import LTC.MinimalER\n\nopen import LTC.Data.N\nopen import LTC.Function.Arithmetic\nopen import LTC.Function.Arithmetic.Properties using ( +-comm )\nopen import LTC.Relation.Equalities.PropertiesER\n\nopen import MyStdLib.Function\nimport MyStdLib.Relation.Binary.EqReasoning\nopen module APER = MyStdLib.Relation.Binary.EqReasoning.StdLib _\u2261_ refl trans\n\n------------------------------------------------------------------------------\n\nminus-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m - n)\nminus-N {m} Nm zN = subst (\u03bb t \u2192 N t) (sym (minus-x0 m)) Nm\nminus-N zN (sN {n} Nn) = subst (\u03bb t \u2192 N t) (sym (minus-0S n)) zN\nminus-N (sN {m} Nm) (sN {n} Nn) = subst (\u03bb t \u2192 N t)\n (sym (minus-SS m n))\n (minus-N Nm Nn)\n\nminus-0x : {n : D} \u2192 N n \u2192 zero - n \u2261 zero\nminus-0x zN = minus-x0 zero\nminus-0x (sN {n} Nn) = minus-0S n\n\n+-leftIdentity : {n : D} \u2192 N n \u2192 zero + n \u2261 n\n+-leftIdentity {n} _ = +-0x n\n\n+-rightIdentity : {n : D} \u2192 N n \u2192 n + zero \u2261 n\n+-rightIdentity zN = +-leftIdentity zN\n+-rightIdentity (sN {n} Nn) =\n trans (+-Sx n zero)\n (subst (\u03bb t \u2192 succ (n + zero) \u2261 succ t)\n (+-rightIdentity Nn)\n refl\n )\n\n+-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N zN Nn = subst (\u03bb t \u2192 N t) (sym (+-leftIdentity Nn)) Nn\n+-N {n = n} (sN {m} Nm ) Nn =\n subst (\u03bb t \u2192 N t) (sym (+-Sx m n)) (sN (+-N Nm Nn))\n\n+-assoc : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc {n = n} {o = o} zN Nn No =\n begin\n zero + n + o \u2261\u27e8 subst (\u03bb t \u2192 zero + n + o \u2261 t + o)\n (+-leftIdentity Nn)\n refl\n \u27e9\n n + o \u2261\u27e8 sym (+-leftIdentity (+-N Nn No)) \u27e9\n zero + (n + o)\n \u220e\n\n+-assoc {n = n} {o = o} (sN {m} Nm) Nn No =\n begin\n succ m + n + o \u2261\u27e8 subst (\u03bb t \u2192 succ m + n + o \u2261 t + o)\n (+-Sx m n)\n refl\n \u27e9\n succ (m + n) + o \u2261\u27e8 +-Sx (m + n) o \u27e9\n succ (m + n + o) \u2261\u27e8 subst (\u03bb t \u2192 succ (m + n + o) \u2261 succ t)\n (+-assoc Nm Nn No)\n refl\n \u27e9\n succ (m + (n + o)) \u2261\u27e8 sym (+-Sx m (n + o)) \u27e9\n succ m + (n + o)\n \u220e\n\nx+1+y\u22611+x+y : {m n : D} \u2192 N m \u2192 N n \u2192 m + succ n \u2261 succ (m + n)\nx+1+y\u22611+x+y {n = n} zN Nn =\n begin\n zero + succ n \u2261\u27e8 +-0x (succ n ) \u27e9\n succ n \u2261\u27e8 subst (\u03bb t \u2192 succ n \u2261 succ t)\n (sym (+-leftIdentity Nn))\n refl\n \u27e9\n succ (zero + n)\n \u220e\n\nx+1+y\u22611+x+y {n = n} (sN {m} Nm) Nn =\n begin\n succ m + succ n \u2261\u27e8 +-Sx m (succ n) \u27e9\n succ (m + succ n) \u2261\u27e8 subst (\u03bb t \u2192 succ (m + succ n) \u2261 succ t)\n (x+1+y\u22611+x+y Nm Nn)\n refl\n \u27e9\n succ (succ (m + n)) \u2261\u27e8 subst (\u03bb t \u2192 succ (succ (m + n)) \u2261 succ t)\n (sym (+-Sx m n))\n refl\n \u27e9\n succ (succ m + n)\n \u220e\n\n[x+y]-[x+z]\u2261y-z : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192\n (m + n) - (m + o) \u2261 n - o\n[x+y]-[x+z]\u2261y-z {n = n} {o} zN Nn No =\n begin\n (zero + n) - (zero + o) \u2261\u27e8 subst (\u03bb t \u2192 (zero + n) - (zero + o) \u2261\n t - (zero + o))\n (+-0x n) refl\n \u27e9\n n - (zero + o) \u2261\u27e8 subst (\u03bb t \u2192 n - (zero + o) \u2261 n - t)\n (+-0x o)\n refl \u27e9\n n - o\n \u220e\n\n[x+y]-[x+z]\u2261y-z {n = n} {o} (sN {m} Nm) Nn No =\n begin\n (succ m + n) - (succ m + o) \u2261\u27e8 subst (\u03bb t \u2192 succ m + n - (succ m + o) \u2261\n t - (succ m + o))\n (+-Sx m n)\n refl\n \u27e9\n succ (m + n) - (succ m + o) \u2261\u27e8 subst (\u03bb t \u2192 succ (m + n) - (succ m + o) \u2261\n succ (m + n) - t)\n (+-Sx m o)\n refl \u27e9\n succ (m + n) - succ (m + o) \u2261\u27e8 minus-SS (m + n) (m + o) \u27e9\n (m + n) - (m + o) \u2261\u27e8 [x+y]-[x+z]\u2261y-z Nm Nn No \u27e9\n n - o\n \u220e\n\n*-leftZero : {n : D} \u2192 N n \u2192 zero * n \u2261 zero\n*-leftZero {n} _ = *-0x n\n\n*-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m * n)\n*-N zN Nn = subst (\u03bb t \u2192 N t) (sym (*-leftZero Nn)) zN\n*-N {n = n} (sN {m} Nm) Nn =\n subst (\u03bb t \u2192 N t) (sym (*-Sx m n)) (+-N Nn (*-N Nm Nn))\n\n*-rightZero : {n : D} \u2192 N n \u2192 n * zero \u2261 zero\n*-rightZero zN = *-leftZero zN\n*-rightZero (sN {n} Nn) =\n trans (*-Sx n zero)\n (trans (+-leftIdentity (*-N Nn zN)) (*-rightZero Nn))\n\nx*1+y\u2261x+xy : {m n : D} \u2192 N m \u2192 N n \u2192 m * succ n \u2261 m + m * n\nx*1+y\u2261x+xy {n = n} zN Nn = sym\n (\n begin\n zero + zero * n \u2261\u27e8 subst (\u03bb t \u2192 zero + zero * n \u2261 zero + t)\n (*-leftZero Nn)\n refl\n \u27e9\n zero + zero \u2261\u27e8 +-leftIdentity zN \u27e9\n zero \u2261\u27e8 sym (*-leftZero (sN Nn)) \u27e9\n zero * succ n\n \u220e\n )\n\nx*1+y\u2261x+xy {n = n} (sN {m} Nm) Nn =\n begin\n succ m * succ n \u2261\u27e8 *-Sx m (succ n) \u27e9\n succ n + m * succ n \u2261\u27e8 subst (\u03bb t \u2192 succ n + m * succ n \u2261 succ n + t)\n (x*1+y\u2261x+xy Nm Nn)\n refl\n \u27e9\n succ n + (m + m * n) \u2261\u27e8 +-Sx n (m + m * n) \u27e9\n succ (n + (m + m * n)) \u2261\u27e8 subst (\u03bb t \u2192 succ (n + (m + m * n)) \u2261 succ t)\n (sym (+-assoc Nn Nm (*-N Nm Nn)))\n refl\n \u27e9\n succ (n + m + m * n) \u2261\u27e8 subst (\u03bb t \u2192 succ (n + m + m * n) \u2261\n succ (t + m * n))\n (+-comm Nn Nm)\n refl\n \u27e9\n succ (m + n + m * n) \u2261\u27e8 subst (\u03bb t \u2192 succ (m + n + m * n) \u2261 succ t)\n (+-assoc Nm Nn (*-N Nm Nn))\n refl\n \u27e9\n\n succ (m + (n + m * n)) \u2261\u27e8 sym (+-Sx m (n + m * n)) \u27e9\n succ m + (n + m * n) \u2261\u27e8 subst (\u03bb t \u2192 succ m + (n + m * n) \u2261 succ m + t)\n (sym (*-Sx m n))\n refl\n \u27e9\n succ m + succ m * n\n \u220e\n\n*-comm : {m n : D} \u2192 N m \u2192 N n \u2192 m * n \u2261 n * m\n*-comm zN Nn = trans (*-leftZero Nn) (sym (*-rightZero Nn))\n*-comm {n = n} (sN {m} Nm) Nn =\n begin\n succ m * n \u2261\u27e8 *-Sx m n \u27e9\n n + m * n \u2261\u27e8 subst (\u03bb t \u2192 n + m * n \u2261 n + t)\n (*-comm Nm Nn)\n refl\n \u27e9\n n + n * m \u2261\u27e8 sym (x*1+y\u2261x+xy Nn Nm) \u27e9\n n * succ m\n \u220e\n\n[x-y]z\u2261xz*yz : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 (m - n) * o \u2261 m * o - n * o\n[x-y]z\u2261xz*yz {m} {o = o} Nm zN No =\n begin\n (m - zero) * o \u2261\u27e8 subst (\u03bb t \u2192 (m - zero) * o \u2261 t * o)\n (minus-x0 m)\n refl\n \u27e9\n m * o \u2261\u27e8 sym (minus-x0 (m * o)) \u27e9\n m * o - zero \u2261\u27e8 subst (\u03bb t \u2192 m * o - zero \u2261 m * o - t)\n (sym (*-0x o))\n refl\n \u27e9\n m * o - zero * o\n \u220e\n\n[x-y]z\u2261xz*yz {o = o} zN (sN {n} Nn) No =\n begin\n (zero - succ n) * o \u2261\u27e8 subst (\u03bb t \u2192 (zero - succ n) * o \u2261 t * o)\n (minus-0S n)\n refl\n \u27e9\n zero * o \u2261\u27e8 *-0x o \u27e9\n zero \u2261\u27e8 sym (minus-0x (*-N (sN Nn) No)) \u27e9\n zero - succ n * o \u2261\u27e8 subst (\u03bb t \u2192 zero - succ n * o \u2261 t - succ n * o)\n (sym (*-0x o))\n refl\n \u27e9\n zero * o - succ n * o\n \u220e\n\n[x-y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) zN =\n begin\n (succ m - succ n) * zero \u2261\u27e8 *-comm (minus-N (sN Nm) (sN Nn)) zN \u27e9\n zero * (succ m - succ n) \u2261\u27e8 *-0x (succ m - succ n) \u27e9\n zero \u2261\u27e8 sym (minus-0x (*-N (sN Nn) zN)) \u27e9\n zero - succ n * zero \u2261\u27e8 subst (\u03bb t \u2192 zero - succ n * zero \u2261\n t - succ n * zero)\n (sym (*-0x (succ m)))\n refl\n \u27e9\n zero * succ m - succ n * zero \u2261\u27e8 subst\n (\u03bb t \u2192 zero * succ m - succ n * zero \u2261\n t - succ n * zero)\n (*-comm zN (sN Nm))\n refl\n \u27e9\n succ m * zero - succ n * zero\n \u220e\n\n[x-y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) (sN {o} No) =\n begin\n (succ m - succ n) * succ o \u2261\u27e8 subst (\u03bb t \u2192 (succ m - succ n) * succ o \u2261\n t * succ o)\n (minus-SS m n)\n refl\n \u27e9\n (m - n) * succ o \u2261\u27e8 [x-y]z\u2261xz*yz Nm Nn (sN No) \u27e9\n m * succ o - n * succ o \u2261\u27e8 sym ([x+y]-[x+z]\u2261y-z (sN No)\n (*-N Nm (sN No))\n (*-N Nn (sN No)))\n \u27e9\n (succ o + m * succ o) - (succ o + n * succ o)\n \u2261\u27e8 subst (\u03bb t \u2192 (succ o + m * succ o) - (succ o + n * succ o) \u2261\n t - (succ o + n * succ o))\n (sym (*-Sx m (succ o)))\n refl\n \u27e9\n (succ m * succ o) - (succ o + n * succ o)\n \u2261\u27e8 subst (\u03bb t \u2192 (succ m * succ o) - (succ o + n * succ o) \u2261\n (succ m * succ o) - t)\n (sym (*-Sx n (succ o)))\n refl\n \u27e9\n (succ m * succ o) - (succ n * succ o)\n \u220e\n\n[x+y]z\u2261xz*yz : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 (m + n) * o \u2261 m * o + n * o\n[x+y]z\u2261xz*yz {m} {n} Nm Nn zN =\n begin\n (m + n) * zero \u2261\u27e8 *-comm (+-N Nm Nn) zN \u27e9\n zero * (m + n) \u2261\u27e8 *-0x (m + n) \u27e9\n zero \u2261\u27e8 sym (*-0x m) \u27e9\n zero * m \u2261\u27e8 *-comm zN Nm \u27e9\n m * zero \u2261\u27e8 sym (+-rightIdentity (*-N Nm zN)) \u27e9\n m * zero + zero \u2261\u27e8 subst (\u03bb t \u2192 m * zero + zero \u2261 m * zero + t)\n (trans (sym (*-0x n)) (*-comm zN Nn))\n refl\n \u27e9\n m * zero + n * zero\n \u220e\n\n[x+y]z\u2261xz*yz {n = n} zN Nn (sN {o} No) =\n begin\n (zero + n) * succ o \u2261\u27e8 subst (\u03bb t \u2192 (zero + n) * succ o \u2261 t * succ o)\n (+-leftIdentity Nn)\n refl\n \u27e9\n n * succ o \u2261\u27e8 sym (+-leftIdentity (*-N Nn (sN No))) \u27e9\n zero + n * succ o \u2261\u27e8 subst (\u03bb t \u2192 zero + n * succ o \u2261 t + n * succ o)\n (sym (*-0x (succ o)))\n refl\n \u27e9\n zero * succ o + n * succ o\n \u220e\n\n[x+y]z\u2261xz*yz (sN {m} Nm) zN (sN {o} No) =\n begin\n (succ m + zero) * succ o \u2261\u27e8 subst (\u03bb t \u2192 (succ m + zero) * succ o \u2261\n t * succ o)\n (+-rightIdentity (sN Nm))\n refl\n \u27e9\n succ m * succ o \u2261\u27e8 sym (+-rightIdentity (*-N (sN Nm) (sN No))) \u27e9\n succ m * succ o + zero \u2261\u27e8 subst (\u03bb t \u2192 succ m * succ o + zero \u2261\n succ m * succ o + t)\n (sym (*-leftZero (sN No)))\n refl\n \u27e9\n succ m * succ o + zero * succ o\n \u220e\n\n[x+y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) (sN {o} No) =\n begin\n (succ m + succ n) * succ o\n \u2261\u27e8 subst (\u03bb t \u2192 (succ m + succ n) * succ o \u2261 t * succ o)\n (+-Sx m (succ n))\n refl\n \u27e9\n succ ( m + succ n) * succ o \u2261\u27e8 *-Sx (m + succ n) (succ o) \u27e9\n succ o + (m + succ n) * succ o\n \u2261\u27e8 subst (\u03bb t \u2192 succ o + (m + succ n) * succ o \u2261 succ o + t)\n ([x+y]z\u2261xz*yz Nm (sN Nn) (sN No))\n refl\n \u27e9\n succ o + (m * succ o + succ n * succ o)\n \u2261\u27e8 sym (+-assoc (sN No) (*-N Nm (sN No)) (*-N (sN Nn) (sN No))) \u27e9\n succ o + m * succ o + succ n * succ o\n \u2261\u27e8 subst (\u03bb t \u2192 succ o + m * succ o + succ n * succ o \u2261\n t + succ n * succ o)\n (sym (*-Sx m (succ o)))\n refl\n \u27e9\n succ m * succ o + succ n * succ o\n \u220e","old_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties using equational reasoning\n------------------------------------------------------------------------------\n\nmodule LTC.Function.Arithmetic.PropertiesER where\n\nopen import LTC.Minimal\nopen import LTC.MinimalER\n\nopen import LTC.Data.N\nopen import LTC.Function.Arithmetic\nopen import LTC.Function.Arithmetic.Properties using ( +-comm )\nopen import LTC.Relation.Equalities.PropertiesER\n\nopen import MyStdLib.Function\nimport MyStdLib.Relation.Binary.EqReasoning\nopen module APER = MyStdLib.Relation.Binary.EqReasoning.StdLib _\u2261_ refl trans\n\n------------------------------------------------------------------------------\n\nminus-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m - n)\nminus-N {m} Nm zN = subst (\u03bb t \u2192 N t) (sym (minus-x0 m)) Nm\nminus-N zN (sN {n} Nn) = subst (\u03bb t \u2192 N t) (sym (minus-0S n)) zN\nminus-N (sN {m} Nm) (sN {n} Nn) = subst (\u03bb t \u2192 N t)\n (sym (minus-SS m n))\n (minus-N Nm Nn)\n\nminus-0x : {n : D} \u2192 N n \u2192 zero - n \u2261 zero\nminus-0x zN = minus-x0 zero\nminus-0x (sN {n} Nn) = minus-0S n\n\n+-leftIdentity : {n : D} \u2192 N n \u2192 zero + n \u2261 n\n+-leftIdentity {n} _ = +-0x n\n\n+-rightIdentity : {n : D} \u2192 N n \u2192 n + zero \u2261 n\n+-rightIdentity zN = +-leftIdentity zN\n+-rightIdentity (sN {n} Nn) =\n trans (+-Sx n zero)\n (subst (\u03bb t \u2192 succ (n + zero) \u2261 succ t)\n (+-rightIdentity Nn)\n refl\n )\n\n+-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N zN Nn = subst (\u03bb t \u2192 N t) (sym (+-leftIdentity Nn)) Nn\n+-N {n = n} (sN {m} Nm ) Nn =\n subst (\u03bb t \u2192 N t) (sym (+-Sx m n)) (sN (+-N Nm Nn))\n\n+-assoc : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc {n = n} {o = o} zN Nn No =\n begin\n zero + n + o \u2261\u27e8 subst (\u03bb t \u2192 zero + n + o \u2261 t + o)\n (+-leftIdentity Nn)\n refl\n \u27e9\n n + o \u2261\u27e8 sym (+-leftIdentity (+-N Nn No)) \u27e9\n zero + (n + o)\n \u220e\n\n+-assoc {n = n} {o = o} (sN {m} Nm) Nn No =\n begin\n succ m + n + o \u2261\u27e8 subst (\u03bb t \u2192 succ m + n + o \u2261 t + o)\n (+-Sx m n)\n refl\n \u27e9\n succ (m + n) + o \u2261\u27e8 +-Sx (m + n) o \u27e9\n succ (m + n + o) \u2261\u27e8 subst (\u03bb t \u2192 succ (m + n + o) \u2261 succ t)\n (+-assoc Nm Nn No)\n refl\n \u27e9\n succ (m + (n + o)) \u2261\u27e8 sym (+-Sx m (n + o)) \u27e9\n succ m + (n + o)\n \u220e\n\n[x+y]-[x+z]\u2261y-z : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192\n (m + n) - (m + o) \u2261 n - o\n[x+y]-[x+z]\u2261y-z {n = n} {o} zN Nn No =\n begin\n (zero + n) - (zero + o) \u2261\u27e8 subst (\u03bb t \u2192 (zero + n) - (zero + o) \u2261\n t - (zero + o))\n (+-0x n) refl\n \u27e9\n n - (zero + o) \u2261\u27e8 subst (\u03bb t \u2192 n - (zero + o) \u2261 n - t)\n (+-0x o)\n refl \u27e9\n n - o\n \u220e\n\n[x+y]-[x+z]\u2261y-z {n = n} {o} (sN {m} Nm) Nn No =\n begin\n (succ m + n) - (succ m + o) \u2261\u27e8 subst (\u03bb t \u2192 succ m + n - (succ m + o) \u2261\n t - (succ m + o))\n (+-Sx m n)\n refl\n \u27e9\n succ (m + n) - (succ m + o) \u2261\u27e8 subst (\u03bb t \u2192 succ (m + n) - (succ m + o) \u2261\n succ (m + n) - t)\n (+-Sx m o)\n refl \u27e9\n succ (m + n) - succ (m + o) \u2261\u27e8 minus-SS (m + n) (m + o) \u27e9\n (m + n) - (m + o) \u2261\u27e8 [x+y]-[x+z]\u2261y-z Nm Nn No \u27e9\n n - o\n \u220e\n\n*-leftZero : {n : D} \u2192 N n \u2192 zero * n \u2261 zero\n*-leftZero {n} _ = *-0x n\n\n*-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m * n)\n*-N zN Nn = subst (\u03bb t \u2192 N t) (sym (*-leftZero Nn)) zN\n*-N {n = n} (sN {m} Nm) Nn =\n subst (\u03bb t \u2192 N t) (sym (*-Sx m n)) (+-N Nn (*-N Nm Nn))\n\n*-rightZero : {n : D} \u2192 N n \u2192 n * zero \u2261 zero\n*-rightZero zN = *-leftZero zN\n*-rightZero (sN {n} Nn) =\n trans (*-Sx n zero)\n (trans (+-leftIdentity (*-N Nn zN)) (*-rightZero Nn))\n\nx*1+y\u2261x+xy : {m n : D} \u2192 N m \u2192 N n \u2192 m * succ n \u2261 m + m * n\nx*1+y\u2261x+xy {n = n} zN Nn = sym\n (\n begin\n zero + zero * n \u2261\u27e8 subst (\u03bb t \u2192 zero + zero * n \u2261 zero + t)\n (*-leftZero Nn)\n refl\n \u27e9\n zero + zero \u2261\u27e8 +-leftIdentity zN \u27e9\n zero \u2261\u27e8 sym (*-leftZero (sN Nn)) \u27e9\n zero * succ n\n \u220e\n )\n\nx*1+y\u2261x+xy {n = n} (sN {m} Nm) Nn =\n begin\n succ m * succ n \u2261\u27e8 *-Sx m (succ n) \u27e9\n succ n + m * succ n \u2261\u27e8 subst (\u03bb t \u2192 succ n + m * succ n \u2261 succ n + t)\n (x*1+y\u2261x+xy Nm Nn)\n refl\n \u27e9\n succ n + (m + m * n) \u2261\u27e8 +-Sx n (m + m * n) \u27e9\n succ (n + (m + m * n)) \u2261\u27e8 subst (\u03bb t \u2192 succ (n + (m + m * n)) \u2261 succ t)\n (sym (+-assoc Nn Nm (*-N Nm Nn)))\n refl\n \u27e9\n succ (n + m + m * n) \u2261\u27e8 subst (\u03bb t \u2192 succ (n + m + m * n) \u2261\n succ (t + m * n))\n (+-comm Nn Nm)\n refl\n \u27e9\n succ (m + n + m * n) \u2261\u27e8 subst (\u03bb t \u2192 succ (m + n + m * n) \u2261 succ t)\n (+-assoc Nm Nn (*-N Nm Nn))\n refl\n \u27e9\n\n succ (m + (n + m * n)) \u2261\u27e8 sym (+-Sx m (n + m * n)) \u27e9\n succ m + (n + m * n) \u2261\u27e8 subst (\u03bb t \u2192 succ m + (n + m * n) \u2261 succ m + t)\n (sym (*-Sx m n))\n refl\n \u27e9\n succ m + succ m * n\n \u220e\n\n*-comm : {m n : D} \u2192 N m \u2192 N n \u2192 m * n \u2261 n * m\n*-comm zN Nn = trans (*-leftZero Nn) (sym (*-rightZero Nn))\n*-comm {n = n} (sN {m} Nm) Nn =\n begin\n succ m * n \u2261\u27e8 *-Sx m n \u27e9\n n + m * n \u2261\u27e8 subst (\u03bb t \u2192 n + m * n \u2261 n + t)\n (*-comm Nm Nn)\n refl\n \u27e9\n n + n * m \u2261\u27e8 sym (x*1+y\u2261x+xy Nn Nm) \u27e9\n n * succ m\n \u220e\n\n[x-y]z\u2261xz*yz : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 (m - n) * o \u2261 m * o - n * o\n[x-y]z\u2261xz*yz {m} {o = o} Nm zN No =\n begin\n (m - zero) * o \u2261\u27e8 subst (\u03bb t \u2192 (m - zero) * o \u2261 t * o)\n (minus-x0 m)\n refl\n \u27e9\n m * o \u2261\u27e8 sym (minus-x0 (m * o)) \u27e9\n m * o - zero \u2261\u27e8 subst (\u03bb t \u2192 m * o - zero \u2261 m * o - t)\n (sym (*-0x o))\n refl\n \u27e9\n m * o - zero * o\n \u220e\n\n[x-y]z\u2261xz*yz {o = o} zN (sN {n} Nn) No =\n begin\n (zero - succ n) * o \u2261\u27e8 subst (\u03bb t \u2192 (zero - succ n) * o \u2261 t * o)\n (minus-0S n)\n refl\n \u27e9\n zero * o \u2261\u27e8 *-0x o \u27e9\n zero \u2261\u27e8 sym (minus-0x (*-N (sN Nn) No)) \u27e9\n zero - succ n * o \u2261\u27e8 subst (\u03bb t \u2192 zero - succ n * o \u2261 t - succ n * o)\n (sym (*-0x o))\n refl\n \u27e9\n zero * o - succ n * o\n \u220e\n\n[x-y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) zN =\n begin\n (succ m - succ n) * zero \u2261\u27e8 *-comm (minus-N (sN Nm) (sN Nn)) zN \u27e9\n zero * (succ m - succ n) \u2261\u27e8 *-0x (succ m - succ n) \u27e9\n zero \u2261\u27e8 sym (minus-0x (*-N (sN Nn) zN)) \u27e9\n zero - succ n * zero \u2261\u27e8 subst (\u03bb t \u2192 zero - succ n * zero \u2261\n t - succ n * zero)\n (sym (*-0x (succ m)))\n refl\n \u27e9\n zero * succ m - succ n * zero \u2261\u27e8 subst\n (\u03bb t \u2192 zero * succ m - succ n * zero \u2261\n t - succ n * zero)\n (*-comm zN (sN Nm))\n refl\n \u27e9\n succ m * zero - succ n * zero\n \u220e\n\n[x-y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) (sN {o} No) =\n begin\n (succ m - succ n) * succ o \u2261\u27e8 subst (\u03bb t \u2192 (succ m - succ n) * succ o \u2261\n t * succ o)\n (minus-SS m n)\n refl\n \u27e9\n (m - n) * succ o \u2261\u27e8 [x-y]z\u2261xz*yz Nm Nn (sN No) \u27e9\n m * succ o - n * succ o \u2261\u27e8 sym ([x+y]-[x+z]\u2261y-z (sN No)\n (*-N Nm (sN No))\n (*-N Nn (sN No)))\n \u27e9\n (succ o + m * succ o) - (succ o + n * succ o)\n \u2261\u27e8 subst (\u03bb t \u2192 (succ o + m * succ o) - (succ o + n * succ o) \u2261\n t - (succ o + n * succ o))\n (sym (*-Sx m (succ o)))\n refl\n \u27e9\n (succ m * succ o) - (succ o + n * succ o)\n \u2261\u27e8 subst (\u03bb t \u2192 (succ m * succ o) - (succ o + n * succ o) \u2261\n (succ m * succ o) - t)\n (sym (*-Sx n (succ o)))\n refl\n \u27e9\n (succ m * succ o) - (succ n * succ o)\n \u220e\n\n[x+y]z\u2261xz*yz : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 (m + n) * o \u2261 m * o + n * o\n[x+y]z\u2261xz*yz {m} {n} Nm Nn zN =\n begin\n (m + n) * zero \u2261\u27e8 *-comm (+-N Nm Nn) zN \u27e9\n zero * (m + n) \u2261\u27e8 *-0x (m + n) \u27e9\n zero \u2261\u27e8 sym (*-0x m) \u27e9\n zero * m \u2261\u27e8 *-comm zN Nm \u27e9\n m * zero \u2261\u27e8 sym (+-rightIdentity (*-N Nm zN)) \u27e9\n m * zero + zero \u2261\u27e8 subst (\u03bb t \u2192 m * zero + zero \u2261 m * zero + t)\n (trans (sym (*-0x n)) (*-comm zN Nn))\n refl\n \u27e9\n m * zero + n * zero\n \u220e\n\n[x+y]z\u2261xz*yz {n = n} zN Nn (sN {o} No) =\n begin\n (zero + n) * succ o \u2261\u27e8 subst (\u03bb t \u2192 (zero + n) * succ o \u2261 t * succ o)\n (+-leftIdentity Nn)\n refl\n \u27e9\n n * succ o \u2261\u27e8 sym (+-leftIdentity (*-N Nn (sN No))) \u27e9\n zero + n * succ o \u2261\u27e8 subst (\u03bb t \u2192 zero + n * succ o \u2261 t + n * succ o)\n (sym (*-0x (succ o)))\n refl\n \u27e9\n zero * succ o + n * succ o\n \u220e\n\n[x+y]z\u2261xz*yz (sN {m} Nm) zN (sN {o} No) =\n begin\n (succ m + zero) * succ o \u2261\u27e8 subst (\u03bb t \u2192 (succ m + zero) * succ o \u2261\n t * succ o)\n (+-rightIdentity (sN Nm))\n refl\n \u27e9\n succ m * succ o \u2261\u27e8 sym (+-rightIdentity (*-N (sN Nm) (sN No))) \u27e9\n succ m * succ o + zero \u2261\u27e8 subst (\u03bb t \u2192 succ m * succ o + zero \u2261\n succ m * succ o + t)\n (sym (*-leftZero (sN No)))\n refl\n \u27e9\n succ m * succ o + zero * succ o\n \u220e\n\n[x+y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) (sN {o} No) =\n begin\n (succ m + succ n) * succ o\n \u2261\u27e8 subst (\u03bb t \u2192 (succ m + succ n) * succ o \u2261 t * succ o)\n (+-Sx m (succ n))\n refl\n \u27e9\n succ ( m + succ n) * succ o \u2261\u27e8 *-Sx (m + succ n) (succ o) \u27e9\n succ o + (m + succ n) * succ o\n \u2261\u27e8 subst (\u03bb t \u2192 succ o + (m + succ n) * succ o \u2261 succ o + t)\n ([x+y]z\u2261xz*yz Nm (sN Nn) (sN No))\n refl\n \u27e9\n succ o + (m * succ o + succ n * succ o)\n \u2261\u27e8 sym (+-assoc (sN No) (*-N Nm (sN No)) (*-N (sN Nn) (sN No))) \u27e9\n succ o + m * succ o + succ n * succ o\n \u2261\u27e8 subst (\u03bb t \u2192 succ o + m * succ o + succ n * succ o \u2261\n t + succ n * succ o)\n (sym (*-Sx m (succ o)))\n refl\n \u27e9\n succ m * succ o + succ n * succ o\n \u220e","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"8dcb8c60821890604e6bd92f3acf8aaa9b58a6f9","subject":"Follow-up question to commit message 3367105","message":"Follow-up question to commit message 3367105\n\nOld-commit-hash: d0b179f01726529fe06b0707c242472fce315998\n","repos":"inc-lc\/ilc-agda","old_file":"Data\/NatBag\/Properties.agda","new_file":"Data\/NatBag\/Properties.agda","new_contents":"module Data.NatBag.Properties where\n\nimport Data.Nat as \u2115\nopen import Relation.Binary.PropositionalEquality\nopen import Data.NatBag\nopen import Data.Integer\nopen import Data.Sum hiding (map)\nopen import Data.Product hiding (map)\n\n-- This import is too slow.\n-- It causes Agda 2.3.2 to use so much memory that cai's\n-- computer with 4GB RAM begins to thresh.\n--\n-- open import Data.Integer.Properties using (n\u2296n\u22610)\nn\u2296n\u22610 : \u2200 n \u2192 n \u2296 n \u2261 + 0\nn\u2296n\u22610 \u2115.zero = refl\nn\u2296n\u22610 (\u2115.suc n) = n\u2296n\u22610 n\n\n\n----------------\n-- Statements --\n----------------\n\n-- Caution: Please convert all implicit bag argument to\n-- instance bag argument in the next iteration so that\n-- using the lemmas here incurs less typing overhead.\n-- Leave the implicit arguments on multivariate lemmas,\n-- though.\n--\n-- TODO: Convert bags from a sum to an ADT so that\n-- constructor names give hints to the bag type,\n-- and a set-theoretic development can write things\n-- like \u2200 {b} \u2192 \u2205 \u2286 b.\n\n\nb\\\\b=\u2205 : \u2200 {b} \u2192 b \\\\ b \u2261 empty\n\n\u2205++b=b : \u2200 {b} \u2192 empty ++ b \u2261 b\n\nb\\\\\u2205=b : \u2200 {b} \u2192 b \\\\ empty \u2261 b\n\nb++\u2205=b : \u2200 {{b}} \u2192 b ++ empty \u2261 b\n\nb++[d\\\\b]=d : \u2200 {b d} \u2192 b ++ (d \\\\ b) \u2261 d\n\n[b++d]\\\\b=d : \u2200 {b d} \u2192 (b ++ d) \\\\ b \u2261 d\n\n------------\n-- Proofs --\n------------\n\nb++\u2205=b = {!!}\n\ni-i=0 : \u2200 {i : \u2124} \u2192 (i - i) \u2261 (+ 0)\ni-i=0 {+ \u2115.zero} = refl\ni-i=0 {+ \u2115.suc n} = n\u2296n\u22610 n\ni-i=0 { -[1+ n ]} = n\u2296n\u22610 n\n\n-- Debug tool\n-- Lets you try out inhabitance of any type anywhere\nabsurd! : \u2200 {B C : Set} \u2192 0 \u2261 1 \u2192 B \u2192 {x : B} \u2192 C\nabsurd! ()\n\n-- Specialized absurdity needed to type check.\n-- \u03bb () hasn't enough information sometimes.\nabsurd : Nonzero (+ 0) \u2192 \u2200 {A : Set} \u2192 A\nabsurd ()\n\n-- Here to please the termination checker.\nneb\\\\neb=\u2205 : \u2200 {neb : NonemptyBag} \u2192 zipNonempty _-_ neb neb \u2261 empty\nneb\\\\neb=\u2205 {singleton i i\u22600} with nonzero? (i - i)\n... | inj\u2081 _ = refl\n... | inj\u2082 0\u22600 rewrite i-i=0 {i} = absurd 0\u22600\nneb\\\\neb=\u2205 {i \u2237 neb} with nonzero? (i - i)\n... | inj\u2081 _ rewrite neb\\\\neb=\u2205 {neb} = refl\n... | inj\u2082 0\u22600 rewrite neb\\\\neb=\u2205 {neb} | i-i=0 {i} = absurd 0\u22600\n\nb\\\\b=\u2205 {inj\u2081 \u2205} = refl\nb\\\\b=\u2205 {inj\u2082 neb} = neb\\\\neb=\u2205 {neb}\n\n\u2205++b=b {b} = {!!}\n\nb\\\\\u2205=b {b} = {!!}\n\nnegate : \u2200 {i} \u2192 Nonzero i \u2192 Nonzero (- i)\nnegate (negative n) = positive n\nnegate (positive n) = negative n\n\nnegate\u2032 : \u2200 {i} \u2192 (i\u22600 : Nonzero i) \u2192 Nonzero (+ 0 - i)\nnegate\u2032 { -[1+ n ]} (negative .n) = positive n\nnegate\u2032 {+ .(\u2115.suc n)} (positive n) = negative n\n\n0-i=-i : \u2200 {i} \u2192 + 0 - i \u2261 - i\n0-i=-i { -[1+ n ]} = refl -- cases are split, for arguments to\n0-i=-i {+ \u2115.zero} = refl -- refl are different.\n0-i=-i {+ \u2115.suc n} = refl\n\nrewrite-singleton :\n \u2200 (i : \u2124) (0-i\u22600 : Nonzero (+ 0 - i)) ( -i\u22600 : Nonzero (- i)) \u2192\n singleton (+ 0 - i) 0-i\u22600 \u2261 singleton (- i) -i\u22600\nrewrite-singleton (+ \u2115.zero) () ()\nrewrite-singleton (+ \u2115.suc n) (negative .n) (negative .n) = refl\nrewrite-singleton ( -[1+ n ]) (positive .n) (positive .n) = refl\n\nnegateSingleton : \u2200 {i i\u22600} \u2192\n mapNonempty\u2082 (\u03bb j \u2192 + 0 - j) (singleton i i\u22600)\n \u2261\n inj\u2082 (singleton (- i) (negate i\u22600))\n\n-- Fun fact:\n-- Pattern-match on implicit parameters in the first two\n-- cases results in rejection by Agda.\nnegateSingleton {i} {i\u22600} with nonzero? i | nonzero? (+ 0 - i)\nnegateSingleton | inj\u2082 (negative n) | inj\u2081 ()\nnegateSingleton | inj\u2082 (positive n) | inj\u2081 ()\nnegateSingleton {_} {i\u22600} | inj\u2081 i=0 | _ rewrite i=0 = absurd i\u22600\nnegateSingleton {i} {i\u22600} | inj\u2082 _ | inj\u2082 0-i\u22600 =\n begin -- Reasoning done in 1 step. Included for clarity only.\n inj\u2082 (singleton (+ 0 + - i) 0-i\u22600)\n \u2261\u27e8 cong inj\u2082 (rewrite-singleton i 0-i\u22600 (negate i\u22600)) \u27e9\n inj\u2082 (singleton (- i) (negate i\u22600))\n \u220e where open \u2261-Reasoning\n\nabsurd[i-i\u22600] : \u2200 {i} \u2192 Nonzero (i - i) \u2192 \u2200 {A : Set} \u2192 A\nabsurd[i-i\u22600] {+ \u2115.zero} = absurd\nabsurd[i-i\u22600] {+ \u2115.suc n} = absurd[i-i\u22600] { -[1+ n ]}\nabsurd[i-i\u22600] { -[1+ \u2115.zero ]} = absurd\nabsurd[i-i\u22600] { -[1+ \u2115.suc n ]} = absurd[i-i\u22600] { -[1+ n ]}\n\nannihilate : \u2200 {i i\u22600} \u2192\n inj\u2082 (singleton i i\u22600) ++ inj\u2082 (singleton (- i) (negate i\u22600)) \u2261 inj\u2081 \u2205\n\nannihilate {i} with nonzero? (i - i)\n... | inj\u2081 i-i=0 = \u03bb {i\u22600} \u2192 refl\n... | inj\u2082 i-i\u22600 = absurd[i-i\u22600] {i} i-i\u22600\n{-\nFollow-up question to\nhttps:\/\/github.com\/ps-mr\/ilc\/commit\/978e3f94e70762904e077bb4d51d6b7b17695103#commitcomment-3367105\n\nMysterious error message when the case above is replaced by the one\nbelow.\n\nannihilate {i} with i - i\n... | w = ?\n-}\n\nb++[\u2205\\\\b]=\u2205 : \u2200 {b} \u2192 b ++ (empty \\\\ b) \u2261 empty\nb++[\u2205\\\\b]=\u2205 {inj\u2081 \u2205} = refl\nb++[\u2205\\\\b]=\u2205 {inj\u2082 (singleton i i\u22600)} =\n begin\n inj\u2082 (singleton i i\u22600) ++\n mapNonempty\u2082 (\u03bb j \u2192 + 0 - j) (singleton i i\u22600)\n \u2261\u27e8 cong\u2082 _++_ {x = inj\u2082 (singleton i i\u22600)} refl\n (negateSingleton {i} {i\u22600}) \u27e9\n inj\u2082 (singleton i i\u22600) ++ inj\u2082 (singleton (- i) (negate i\u22600))\n \u2261\u27e8 annihilate {i} {i\u22600} \u27e9\n inj\u2081 \u2205\n \u220e where open \u2261-Reasoning\n\nb++[\u2205\\\\b]=\u2205 {inj\u2082 (i \u2237 y)} = ?\n\nb++[d\\\\b]=d {inj\u2081 \u2205} {d} rewrite b\\\\\u2205=b {d} | \u2205++b=b {d} = refl\nb++[d\\\\b]=d {b} {inj\u2081 \u2205} = b++[\u2205\\\\b]=\u2205 {b}\nb++[d\\\\b]=d {inj\u2082 b} {inj\u2082 d} = {!!}\n\n[b++d]\\\\b=d = {!!}\n","old_contents":"module Data.NatBag.Properties where\n\nimport Data.Nat as \u2115\nopen import Relation.Binary.PropositionalEquality\nopen import Data.NatBag\nopen import Data.Integer\nopen import Data.Sum hiding (map)\nopen import Data.Product hiding (map)\n\n-- This import is too slow.\n-- It causes Agda 2.3.2 to use so much memory that cai's\n-- computer with 4GB RAM begins to thresh.\n--\n-- open import Data.Integer.Properties using (n\u2296n\u22610)\nn\u2296n\u22610 : \u2200 n \u2192 n \u2296 n \u2261 + 0\nn\u2296n\u22610 \u2115.zero = refl\nn\u2296n\u22610 (\u2115.suc n) = n\u2296n\u22610 n\n\n\n----------------\n-- Statements --\n----------------\n\n\nb\\\\b=\u2205 : \u2200 {b : Bag} \u2192 b \\\\ b \u2261 empty\n\n\u2205++b=b : \u2200 {b : Bag} \u2192 empty ++ b \u2261 b\n\nb\\\\\u2205=b : \u2200 {b : Bag} \u2192 b \\\\ empty \u2261 b\n\n\u2205\\\\b=-b : \u2200 {b : Bag} \u2192 empty \\\\ b \u2261 map\u2082 -_ b\n\nb++[d\\\\b]=d : \u2200 {b d : Bag} \u2192 b ++ (d \\\\ b) \u2261 d\n\n\n------------\n-- Proofs --\n------------\n\ni-i=0 : \u2200 {i : \u2124} \u2192 (i - i) \u2261 (+ 0)\ni-i=0 {+ \u2115.zero} = refl\ni-i=0 {+ \u2115.suc n} = n\u2296n\u22610 n\ni-i=0 { -[1+ n ]} = n\u2296n\u22610 n\n\n-- Debug tool\n-- Lets you try out inhabitance of any type anywhere\nabsurd! : \u2200 {B C : Set} \u2192 0 \u2261 1 \u2192 B \u2192 {x : B} \u2192 C\nabsurd! ()\n\n-- Specialized absurdity needed to type check.\n-- \u03bb () hasn't enough information sometimes.\nabsurd : Nonzero (+ 0) \u2192 \u2200 {A : Set} \u2192 A\nabsurd ()\n\n-- Here to please the termination checker.\nneb\\\\neb=\u2205 : \u2200 {neb : NonemptyBag} \u2192 zipNonempty _-_ neb neb \u2261 empty\nneb\\\\neb=\u2205 {singleton i i\u22600} with nonzero? (i - i)\n... | inj\u2081 _ = refl\n... | inj\u2082 0\u22600 rewrite i-i=0 {i} = absurd 0\u22600\nneb\\\\neb=\u2205 {i \u2237 neb} with nonzero? (i - i)\n... | inj\u2081 _ rewrite neb\\\\neb=\u2205 {neb} = refl\n... | inj\u2082 0\u22600 rewrite neb\\\\neb=\u2205 {neb} | i-i=0 {i} = absurd 0\u22600\n\nb\\\\b=\u2205 {inj\u2081 \u2205} = refl\nb\\\\b=\u2205 {inj\u2082 neb} = neb\\\\neb=\u2205 {neb}\n\n\u2205++b=b {b} = {!!}\n\nb\\\\\u2205=b {b} = {!!}\n\n\u2205\\\\b=-b {b} = {!!}\n\nnegate : \u2200 {i} \u2192 Nonzero i \u2192 Nonzero (- i)\nnegate (negative n) = positive n\nnegate (positive n) = negative n\n\nnegate\u2032 : \u2200 {i} \u2192 (i\u22600 : Nonzero i) \u2192 Nonzero (+ 0 - i)\nnegate\u2032 { -[1+ n ]} (negative .n) = positive n\nnegate\u2032 {+ .(\u2115.suc n)} (positive n) = negative n\n\n0-i=-i : \u2200 {i} \u2192 + 0 - i \u2261 - i\n0-i=-i { -[1+ n ]} = refl -- cases are split, for arguments to\n0-i=-i {+ \u2115.zero} = refl -- refl are different.\n0-i=-i {+ \u2115.suc n} = refl\n\nrewrite-singleton :\n \u2200 (i : \u2124) (0-i\u22600 : Nonzero (+ 0 - i)) ( -i\u22600 : Nonzero (- i)) \u2192\n singleton (+ 0 - i) 0-i\u22600 \u2261 singleton (- i) -i\u22600\nrewrite-singleton (+ \u2115.zero) () ()\nrewrite-singleton (+ \u2115.suc n) (negative .n) (negative .n) = refl\nrewrite-singleton ( -[1+ n ]) (positive .n) (positive .n) = refl\n\nnegateSingleton : \u2200 {i i\u22600} \u2192\n mapNonempty\u2082 (\u03bb j \u2192 + 0 - j) (singleton i i\u22600)\n \u2261\n inj\u2082 (singleton (- i) (negate i\u22600))\n\n-- Fun fact:\n-- Pattern-match on implicit parameters in the first two\n-- cases results in rejection by Agda.\nnegateSingleton {i} {i\u22600} with nonzero? i | nonzero? (+ 0 - i)\nnegateSingleton | inj\u2082 (negative n) | inj\u2081 ()\nnegateSingleton | inj\u2082 (positive n) | inj\u2081 ()\nnegateSingleton {_} {i\u22600} | inj\u2081 i=0 | _ rewrite i=0 = absurd i\u22600\nnegateSingleton {i} {i\u22600} | inj\u2082 _ | inj\u2082 0-i\u22600 =\n begin -- Reasoning done in 1 step. Included for clarity only.\n inj\u2082 (singleton (+ 0 + - i) 0-i\u22600)\n \u2261\u27e8 cong inj\u2082 (rewrite-singleton i 0-i\u22600 (negate i\u22600)) \u27e9\n inj\u2082 (singleton (- i) (negate i\u22600))\n \u220e where open \u2261-Reasoning\n\nabsurd[i-i\u22600] : \u2200 {i} \u2192 Nonzero (i - i) \u2192 \u2200 {A : Set} \u2192 A\nabsurd[i-i\u22600] {+ \u2115.zero} = absurd\nabsurd[i-i\u22600] {+ \u2115.suc n} = absurd[i-i\u22600] { -[1+ n ]}\nabsurd[i-i\u22600] { -[1+ \u2115.zero ]} = absurd\nabsurd[i-i\u22600] { -[1+ \u2115.suc n ]} = absurd[i-i\u22600] { -[1+ n ]}\n\nannihilate : \u2200 {i i\u22600} \u2192\n inj\u2082 (singleton i i\u22600) ++ inj\u2082 (singleton (- i) (negate i\u22600)) \u2261 inj\u2081 \u2205\nannihilate {i} with nonzero? (i - i)\n... | inj\u2081 i-i=0 = \u03bb {i\u22600} \u2192 refl\n... | inj\u2082 i-i\u22600 = absurd[i-i\u22600] {i} i-i\u22600\n\nb++[\u2205\\\\b]=\u2205 : \u2200 {b} \u2192 b ++ (empty \\\\ b) \u2261 empty\nb++[\u2205\\\\b]=\u2205 {inj\u2081 \u2205} = refl\nb++[\u2205\\\\b]=\u2205 {inj\u2082 (singleton i i\u22600)} =\n begin\n inj\u2082 (singleton i i\u22600) ++\n mapNonempty\u2082 (\u03bb j \u2192 + 0 - j) (singleton i i\u22600)\n \u2261\u27e8 cong\u2082 _++_ {x = inj\u2082 (singleton i i\u22600)} refl\n (negateSingleton {i} {i\u22600}) \u27e9\n inj\u2082 (singleton i i\u22600) ++ inj\u2082 (singleton (- i) (negate i\u22600))\n \u2261\u27e8 annihilate {i} {i\u22600} \u27e9\n inj\u2081 \u2205\n \u220e where open \u2261-Reasoning\nb++[\u2205\\\\b]=\u2205 {inj\u2082 (i \u2237 y)} = {!!}\n\nb++[d\\\\b]=d {inj\u2081 \u2205} {d} rewrite b\\\\\u2205=b {d} | \u2205++b=b {d} = refl\nb++[d\\\\b]=d {b} {inj\u2081 \u2205} = b++[\u2205\\\\b]=\u2205 {b}\nb++[d\\\\b]=d {inj\u2082 b} {inj\u2082 d} = {!!}\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"dda828f86a18387618d4b27b17c1325955ae135d","subject":"StepIndexedRelBigStepTypedAnfIlcCorrect: Prove SOS sound","message":"StepIndexedRelBigStepTypedAnfIlcCorrect: Prove SOS sound\n\nThat's soundness wrt. denotational semantics. No completeness though, that would\nbe tricky (and `succ` violates it right now).\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/StepIndexedRelBigStepTypedAnfIlcCorrect.agda","new_file":"Thesis\/StepIndexedRelBigStepTypedAnfIlcCorrect.agda","new_contents":"-- Step-indexed logical relations based on relational big-step semantics\n-- for ILC-based incremental computation.\n\n-- Goal: prove the fundamental lemma for a ternary logical relation (change\n-- validity) across t1, dt and t2. The fundamnetal lemma relates t, derive t and\n-- t. That is, we relate a term evaluated relative to an original environment,\n-- its derivative evaluated relative to a valid environment change, and the\n-- original term evaluated relative to an updated environment.\n--\n-- Missing goal: here \u2295 isn't defined and wouldn't yet agree with change\n-- validity.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\" (ISAC below), POPL'08, including the choice of using ANF syntax\n-- to simplify some step-indexing proofs.\n--\n-- In fact, this development is typed, hence some parts of the model are closer\n-- to Ahmed (ESOP 2006), \"Step-Indexed Syntactic Logical Relations for Recursive\n-- and Quantified Types\". But for many relevant aspects, the two papers are\n-- very similar. In fact, I first defined similar logical relations\n-- without types, but they require a trickier recursion scheme for well-founded\n-- recursion, and I failed to do any proof in that setting.\n--\n-- The original inspiration came from Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but we ended up looking\n-- at their source.\n--\n-- The main insight from the ISAC paper missing from the other one is how to\n-- step-index a big-step semantics correctly: just ensure that the steps in the\n-- big-step semantics agree with the ones in the small-step semantics. *Then*\n-- everything just works with big-step semantics. Quite a few other details are\n-- fiddly, but those are the same in small-step semantics.\n--\n-- The crucial novelty here is that we relate two computations on different\n-- inputs. So we only conclude their results are related if both terminate; the\n-- relation for computations does not prove that if the first computation\n-- terminates, then the second terminates as well.\n--\n-- Instead, e1, de and e2 are related at k steps if, whenever e1 terminates in j\n-- < k steps and e2 terminates with any step count, then de terminates (with any\n-- step count) and their results are related at k - j steps.\n--\n-- Even when e1 terminates in j steps implies that e2 terminates, e2 gets no\n-- bound. Similarly, we do not bound in how many steps de terminates, since on\n-- big inputs it might take long.\n\nmodule Thesis.StepIndexedRelBigStepTypedAnfIlcCorrect where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen import Data.Nat.Properties.Simple\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\ninfixr 20 _\u21d2_\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\n-- Decidable equivalence for types and contexts. Needed later for \u2295 on closures.\n\n\u21d2-inj : \u2200 {\u03c41 \u03c42 \u03c43 \u03c44 : Type} \u2192 _\u2261_ {A = Type} (\u03c41 \u21d2 \u03c42) (\u03c43 \u21d2 \u03c44) \u2192 \u03c41 \u2261 \u03c43 \u00d7 \u03c42 \u2261 \u03c44\n\u21d2-inj refl = refl , refl\n\n_\u225fType_ : (\u03c41 \u03c42 : Type) \u2192 Dec (\u03c41 \u2261 \u03c42)\n(\u03c41 \u21d2 \u03c42) \u225fType (\u03c43 \u21d2 \u03c44) with \u03c41 \u225fType \u03c43 | \u03c42 \u225fType \u03c44\n(\u03c41 \u21d2 \u03c42) \u225fType (.\u03c41 \u21d2 .\u03c42) | yes refl | yes refl = yes refl\n(\u03c41 \u21d2 \u03c42) \u225fType (.\u03c41 \u21d2 \u03c44) | yes refl | no \u00acq = no (\u03bb x \u2192 \u00acq (proj\u2082 (\u21d2-inj x)))\n(\u03c41 \u21d2 \u03c42) \u225fType (\u03c43 \u21d2 \u03c44) | no \u00acp | q = no (\u03bb x \u2192 \u00acp (proj\u2081 (\u21d2-inj x)))\n(\u03c41 \u21d2 \u03c42) \u225fType nat = no (\u03bb ())\nnat \u225fType (\u03c42 \u21d2 \u03c43) = no (\u03bb ())\nnat \u225fType nat = yes refl\n\n\u2022-inj : \u2200 {\u03c41 \u03c42 : Type} {\u03931 \u03932 : Context} \u2192 _\u2261_ {A = Context} (\u03c41 \u2022 \u03931) (\u03c42 \u2022 \u03932) \u2192 \u03c41 \u2261 \u03c42 \u00d7 \u03931 \u2261 \u03932\n\u2022-inj refl = refl , refl\n\n_\u225fCtx_ : (\u03931 \u03932 : Context) \u2192 Dec (\u03931 \u2261 \u03932)\n\u2205 \u225fCtx \u2205 = yes refl\n\u2205 \u225fCtx (\u03c42 \u2022 \u03932) = no (\u03bb ())\n(\u03c41 \u2022 \u03931) \u225fCtx \u2205 = no (\u03bb ())\n(\u03c41 \u2022 \u03931) \u225fCtx (\u03c42 \u2022 \u03932) with \u03c41 \u225fType \u03c42 | \u03931 \u225fCtx \u03932\n(\u03c41 \u2022 \u03931) \u225fCtx (.\u03c41 \u2022 .\u03931) | yes refl | yes refl = yes refl\n(\u03c41 \u2022 \u03931) \u225fCtx (.\u03c41 \u2022 \u03932) | yes refl | no \u00acq = no (\u03bb x \u2192 \u00acq (proj\u2082 (\u2022-inj x)))\n(\u03c41 \u2022 \u03931) \u225fCtx (\u03c42 \u2022 \u03932) | no \u00acp | q = no (\u03bb x \u2192 \u00acp (proj\u2081 (\u2022-inj x)))\n\n\u225fCtx-refl : \u2200 \u0393 \u2192 \u0393 \u225fCtx \u0393 \u2261 yes refl\n\u225fCtx-refl \u0393 with \u0393 \u225fCtx \u0393\n\u225fCtx-refl \u0393 | yes refl = refl\n\u225fCtx-refl \u0393 | no \u00acp = \u22a5-elim (\u00acp refl)\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- Adding this changes nothing without changes to the semantics.\n succ : Const (nat \u21d2 nat)\n\ndata Term (\u0393 : Context) (\u03c4 : Type) : Set\n-- Source values\ndata SVal (\u0393 : Context) : (\u03c4 : Type) \u2192 Set where\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n SVal \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n SVal \u0393 (\u03c3 \u21d2 \u03c4)\ndata Term (\u0393 : Context) (\u03c4 : Type) where\n val :\n SVal \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\n -- constants aka. primitives\n const :\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n app : \u2200 {\u03c3}\n (vs : SVal \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (vt : SVal \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n lett : \u2200 {\u03c3}\n (s : Term \u0393 \u03c3) \u2192\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 \u03c4\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\ndata Val : Type \u2192 Set\n\nimport Base.Denotation.Environment\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\nopen Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\n\u27e6_\u27e7Const : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 lit n \u27e7Const = n\n\u27e6 succ \u27e7Const = suc\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Den.\u27e6 \u0393 \u27e7Context \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6_\u27e7SVal : \u2200 {\u0393 \u03c4} \u2192 SVal \u0393 \u03c4 \u2192 Den.\u27e6 \u0393 \u27e7Context \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 var x \u27e7SVal \u03c1 = Den.\u27e6 x \u27e7Var \u03c1\n\u27e6 abs t \u27e7SVal \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 const c \u27e7Term \u03c1 = \u27e6 c \u27e7Const\n\u27e6 val sv \u27e7Term \u03c1 = \u27e6 sv \u27e7SVal \u03c1\n\u27e6 app s t \u27e7Term \u03c1 = \u27e6 s \u27e7SVal \u03c1 (\u27e6 t \u27e7SVal \u03c1)\n\u27e6 lett s t \u27e7Term \u03c1 = \u27e6 t \u27e7Term ((\u27e6 s \u27e7Term \u03c1) \u2022 \u03c1)\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n natV : \u2200 (n : \u2115) \u2192 Val nat\n\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7Context \u2192 Den.\u27e6 \u0393 \u27e7Context\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 closure t \u03c1 \u27e7Val = \u03bb v \u2192 (\u27e6 t \u27e7Term) (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\u27e6 natV n \u27e7Val = n\n\n\u21a6-sound : \u2200 {\u0393 \u03c4} \u03c1 (x : Var \u0393 \u03c4) \u2192\n Den.\u27e6 x \u27e7Var \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 \u27e6 x \u27e7Var \u03c1 \u27e7Val\n\u21a6-sound (px \u2022 \u03c1) this = refl\n\u21a6-sound (px \u2022 \u03c1) (that x) = \u21a6-sound \u03c1 x\n\nimport Data.Integer as I\nopen I using (\u2124)\n\n-- Yann's idea.\ndata HasIdx : Set where\n true : HasIdx\n false : HasIdx\ndata Idx : HasIdx \u2192 Set where\n i' : \u2115 \u2192 Idx true\n no : Idx false\n\ni : {hasIdx : HasIdx} \u2192 \u2115 \u2192 Idx hasIdx\ni {false} j = no\ni {true} j = i' j\n\nmodule _ {hasIdx : HasIdx} where\n data _\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) : \u2200 {\u03c4} \u2192 Term \u0393 \u03c4 \u2192 Idx hasIdx \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u03c3 \u03c4} {t : Term (\u03c3 \u2022 \u0393) \u03c4} \u2192\n \u03c1 \u22a2 val (abs t) \u2193[ i 1 ] closure t \u03c1\n var : \u2200 {\u03c4} (x : Var \u0393 \u03c4) \u2192\n \u03c1 \u22a2 val (var x) \u2193[ i 1 ] (\u27e6 x \u27e7Var \u03c1)\n app : \u2200 n {\u0393\u2032 \u03c3 \u03c4 \u03c1\u2032} vtv {v} {vs : SVal \u0393 (\u03c3 \u21d2 \u03c4)} {vt : SVal \u0393 \u03c3} {t : Term (\u03c3 \u2022 \u0393\u2032) \u03c4} \u2192\n \u03c1 \u22a2 val vs \u2193[ i 0 ] closure t \u03c1\u2032 \u2192\n \u03c1 \u22a2 val vt \u2193[ i 0 ] vtv \u2192\n (vtv \u2022 \u03c1\u2032) \u22a2 t \u2193[ i n ] v \u2192\n \u03c1 \u22a2 app vs vt \u2193[ i (suc n) ] v\n lett :\n \u2200 n1 n2 {\u03c3 \u03c4} vsv {v} (s : Term \u0393 \u03c3) (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n \u03c1 \u22a2 s \u2193[ i n1 ] vsv \u2192\n (vsv \u2022 \u03c1) \u22a2 t \u2193[ i n2 ] v \u2192\n \u03c1 \u22a2 lett s t \u2193[ i (suc n1 + n2) ] v\n lit : \u2200 n \u2192\n \u03c1 \u22a2 const (lit n) \u2193[ i 1 ] natV n\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v hasIdx} {n : Idx hasIdx} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193[ n ] v \u2192\n \u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 v \u27e7Val\n\u2193-sound abs = refl\n\u2193-sound (app _ _ \u2193\u2081 \u2193\u2082 \u2193\u2032) rewrite \u2193-sound \u2193\u2081 | \u2193-sound \u2193\u2082 | \u2193-sound \u2193\u2032 = refl\n\u2193-sound (var x) = \u21a6-sound _ x\n\u2193-sound (lit n) = refl\n\u2193-sound (lett n1 n2 vsv s t \u2193 \u2193\u2081) rewrite \u2193-sound \u2193 | \u2193-sound \u2193\u2081 = refl\n-- \u2193-sound (add \u2193\u2081 \u2193\u2082) rewrite \u2193-sound \u2193\u2081 | \u2193-sound \u2193\u2082 = refl\n-- \u2193-sound (minus \u2193\u2081 \u2193\u2082) rewrite \u2193-sound \u2193\u2081 | \u2193-sound \u2193\u2082 = refl\n\n-- No proof of completeness yet: the statement does not hold here.\n\n-- data DType : Set where\n-- _\u21d2_ : (\u03c3 \u03c4 : DType) \u2192 DType\n-- int : DType\nDType = Type\n\nimport Base.Syntax.Context DType as DC\n\n\u0394\u03c4 : Type \u2192 DType\n\u0394\u03c4 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394\u03c4 \u03c3 \u21d2 \u0394\u03c4 \u03c4\n\u0394\u03c4 nat = nat\n\n\u0394\u0394 : Context \u2192 DC.Context\n\u0394\u0394 \u2205 = \u2205\n\u0394\u0394 (\u03c4 \u2022 \u0393) = \u0394\u03c4 \u03c4 \u2022 \u0394\u0394 \u0393\n--\u0394\u0394 \u0393 = \u0393\n\n-- A DTerm evaluates in normal context \u0394, change context (\u0394\u0394 \u0394), and produces\n-- a result of type (\u0394t \u03c4).\ndata DTerm (\u0394 : Context) (\u03c4 : DType) : Set\ndata DSVal (\u0394 : Context) : (\u03c4 : DType) \u2192 Set where\n dvar : \u2200 {\u03c4} \u2192\n (x : Var (\u0394\u0394 \u0394) (\u0394\u03c4 \u03c4)) \u2192\n DSVal \u0394 \u03c4\n dabs : \u2200 {\u03c3 \u03c4}\n (dt : DTerm (\u03c3 \u2022 \u0394) \u03c4) \u2192\n DSVal \u0394 (\u03c3 \u21d2 \u03c4)\n\ndata DTerm (\u0394 : Context) (\u03c4 : DType) where\n dval :\n DSVal \u0394 \u03c4 \u2192\n DTerm \u0394 \u03c4\n -- constants aka. primitives\n dconst :\n (c : Const \u03c4) \u2192\n DTerm \u0394 \u03c4\n dapp : \u2200 {\u03c3}\n (dvs : DSVal \u0394 (\u03c3 \u21d2 \u03c4)) \u2192\n (vt : SVal \u0394 \u03c3) \u2192\n (dvt : DSVal \u0394 \u03c3) \u2192\n DTerm \u0394 \u03c4\n dlett : \u2200 {\u03c3}\n (s : Term \u0394 \u03c3) \u2192\n (ds : DTerm \u0394 \u03c3) \u2192\n (dt : DTerm (\u03c3 \u2022 \u0394) \u03c4) \u2192\n DTerm \u0394 \u03c4\n\nderive-dvar : \u2200 {\u0394 \u03c3} \u2192 (x : Var \u0394 \u03c3) \u2192 Var (\u0394\u0394 \u0394) (\u0394\u03c4 \u03c3)\nderive-dvar this = this\nderive-dvar (that x) = that (derive-dvar x)\n\nderive-dterm : \u2200 {\u0394 \u03c3} \u2192 (t : Term \u0394 \u03c3) \u2192 DTerm \u0394 \u03c3\n\nderive-dsval : \u2200 {\u0394 \u03c3} \u2192 (t : SVal \u0394 \u03c3) \u2192 DSVal \u0394 \u03c3\nderive-dsval (var x) = dvar (derive-dvar x)\n\nderive-dsval (abs t) = dabs (derive-dterm t)\n\nderive-dterm (val x) = dval (derive-dsval x)\nderive-dterm (const c) = dconst c\nderive-dterm (app vs vt) = dapp (derive-dsval vs) vt (derive-dsval vt)\nderive-dterm (lett s t) = dlett s (derive-dterm s) (derive-dterm t)\n\n-- Nontrivial because of unification problems in pattern matching. I wanted to\n-- use it to define \u2295 on closures purely on terms of the closure change.\n\n-- Instead, I decided to use decidable equality on contexts: that's a lot of\n-- tedious boilerplate, but not too hard, but the proof that validity and \u2295\n-- agree becomes easier.\n-- -- Define a DVar and be done?\n-- underive-dvar : \u2200 {\u0394 \u03c3} \u2192 Var (\u0394\u0394 \u0394) (\u0394\u03c4 \u03c3) \u2192 Var \u0394 \u03c3\n-- underive-dvar {\u2205} ()\n-- underive-dvar {\u03c4 \u2022 \u0394} x = {!!}\n\n--underive-dvar {\u03c3 \u2022 \u0394} (that x) = that (underive-dvar x)\n\ndata DVal : Type \u2192 Set\nimport Base.Denotation.Environment DType DVal as D\n\nCh\u0394 : \u2200 (\u0394 : Context) \u2192 Set\nCh\u0394 \u0394 = D.\u27e6 \u0394 \u27e7Context\n\ndata DVal where\n bang : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 DVal \u03c4\n dclosure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (dt : DTerm (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 (d\u03c1 : Ch\u0394 \u0393) \u2192 DVal (\u03c3 \u21d2 \u03c4)\n dnatV : \u2200 (n : \u2115) \u2192 DVal nat\n\n_\u2295_ : \u2200 {\u03c4} \u2192 (v1 : Val \u03c4) (dv : DVal \u03c4) \u2192 Val \u03c4\n\n_\u2295\u03c1_ : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7Context \u2192 Ch\u0394 \u0393 \u2192 \u27e6 \u0393 \u27e7Context\n\u2205 \u2295\u03c1 \u2205 = \u2205\n(v \u2022 \u03c11) \u2295\u03c1 (dv \u2022 d\u03c1) = v \u2295 dv \u2022 \u03c11 \u2295\u03c1 d\u03c1\n\nv1 \u2295 bang v2 = v2\nclosure {\u0393} t \u03c1 \u2295 dclosure {\u03931} dt \u03c1\u2081 d\u03c1 with \u0393 \u225fCtx \u03931\nclosure {\u0393} t \u03c1 \u2295 dclosure {.\u0393} dt \u03c1\u2081 d\u03c1 | yes refl = closure t (\u03c1 \u2295\u03c1 d\u03c1)\n... | no \u00acp = closure t \u03c1\n_\u2295_ (natV n) (dnatV dn) = natV (n + dn)\n\ndata _D_\u22a2_\u2193_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) : \u2200 {\u03c4} \u2192 DTerm \u0393 \u03c4 \u2192 DVal \u03c4 \u2192 Set where\n dabs : \u2200 {\u03c3 \u03c4} {dt : DTerm (\u03c3 \u2022 \u0393) \u03c4} \u2192\n \u03c1 D d\u03c1 \u22a2 dval (dabs dt) \u2193 dclosure dt \u03c1 d\u03c1\n dvar : \u2200 {\u03c4} (x : DC.Var \u0393 \u03c4) \u2192\n \u03c1 D d\u03c1 \u22a2 dval (dvar (derive-dvar x)) \u2193 D.\u27e6 x \u27e7Var d\u03c1\n dlit : \u2200 n \u2192\n \u03c1 D d\u03c1 \u22a2 dconst (lit n) \u2193 dnatV 0\n dapp : \u2200 {hasIdx} {n : Idx hasIdx}\n {\u0393\u2032 \u03c3 \u03c4 \u03c1\u2032 d\u03c1\u2032}\n {dvs} {vt} {dvt}\n {vtv} {dvtv}\n {dt : DTerm (\u03c3 \u2022 \u0393\u2032) \u03c4} {dv} \u2192\n \u03c1 D d\u03c1 \u22a2 dval dvs \u2193 dclosure dt \u03c1\u2032 d\u03c1\u2032 \u2192\n \u03c1 \u22a2 val vt \u2193[ n ] vtv \u2192\n \u03c1 D d\u03c1 \u22a2 dval dvt \u2193 dvtv \u2192\n (vtv \u2022 \u03c1\u2032) D (dvtv \u2022 d\u03c1\u2032) \u22a2 dt \u2193 dv \u2192\n \u03c1 D d\u03c1 \u22a2 dapp dvs vt dvt \u2193 dv\n dlett : \u2200 {hasIdx} {n : Idx hasIdx}\n {\u03c3 \u03c4} {s : Term \u0393 \u03c3} {ds} {dt : DTerm (\u03c3 \u2022 \u0393) \u03c4}\n {vsv dvsv dv} \u2192\n \u03c1 \u22a2 s \u2193[ n ] vsv \u2192\n \u03c1 D d\u03c1 \u22a2 ds \u2193 dvsv \u2192\n (vsv \u2022 \u03c1) D (dvsv \u2022 d\u03c1) \u22a2 dt \u2193 dv \u2192\n \u03c1 D d\u03c1 \u22a2 dlett s ds dt \u2193 dv\n bangapp : \u2200 {hasIdx} {n1 n2 : Idx hasIdx}\n {\u0393\u2032 \u03c3 \u03c4 \u03c1\u2032}\n {dvs} {vt} {dvt}\n {vtv2}\n {t : Term (\u03c3 \u2022 \u0393\u2032) \u03c4} {v2} \u2192\n \u03c1 D d\u03c1 \u22a2 dval dvs \u2193 bang (closure t \u03c1\u2032) \u2192\n (\u03c1 \u2295\u03c1 d\u03c1) \u22a2 val vt \u2193[ n1 ] vtv2 \u2192\n (vtv2 \u2022 \u03c1\u2032) \u22a2 t \u2193[ n2 ] v2 \u2192\n \u03c1 D d\u03c1 \u22a2 dapp dvs vt dvt \u2193 bang v2\n\nmutual\n rrelT3 : \u2200 {\u03c4 \u0393} (t1 : Term \u0393 \u03c4) (dt : DTerm \u0393 \u03c4) (t2 : Term \u0393 \u03c4) (\u03c11 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) (\u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\n rrelT3 {\u03c4} t1 dt t2 \u03c11 d\u03c1 \u03c12 k =\n (v1 v2 : Val \u03c4) \u2192\n \u2200 j (j v` means mapping `k` to\n-- the change from the neutral element to `v`, and deleting\n-- `k -> v` means mapping `k` to the change from `v` to the\n-- neutral element.\n\ndata Atlas-type : Set where\n Bool : Atlas-type\n Map : (\u03ba : Atlas-type) (\u03b9 : Atlas-type) \u2192 Atlas-type\n\nopen import Syntax.Type.Plotkin Atlas-type\n\ndata Atlas-const : Type \u2192 Set where\n true : Atlas-const\n (base Bool)\n\n false : Atlas-const\n (base Bool)\n\n xor : Atlas-const\n (base Bool \u21d2 base Bool \u21d2 base Bool)\n\n empty : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base (Map \u03ba \u03b9))\n\n -- `update key val my-map` would\n -- - insert if `key` is not present in `my-map`\n -- - delete if `val` is the neutral element\n -- - make an update otherwise\n\n update : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base \u03ba \u21d2 base \u03b9 \u21d2 base (Map \u03ba \u03b9) \u21d2 base (Map \u03ba \u03b9))\n\n lookup : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base \u03ba \u21d2 base (Map \u03ba \u03b9) \u21d2 base \u03b9)\n\n -- Model of zip = Haskell Data.List.zipWith\n --\n -- zipWith :: (a \u2192 b \u2192 c) \u2192 [a] \u2192 [b] \u2192 [c]\n --\n -- Behavioral difference: all key-value pairs present\n -- in *either* (m\u2081 : Map \u03ba a) *or* (m\u2082 : Map \u03ba b) will\n -- be iterated over. Neutral element of type `a` or `b`\n -- will be supplied if the key is missing in the\n -- corresponding map.\n\n zip : \u2200 {\u03ba a b c : Atlas-type} \u2192 Atlas-const\n ((base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u21d2\n base (Map \u03ba a) \u21d2 base (Map \u03ba b) \u21d2 base (Map \u03ba c))\n\n -- Model of fold = Haskell Data.Map.foldWithKey\n --\n -- foldWithKey :: (k \u2192 a \u2192 b \u2192 b) \u2192 b \u2192 Map k a \u2192 b\n\n fold : \u2200 {\u03ba a b : Atlas-type} \u2192 Atlas-const\n ((base \u03ba \u21d2 base a \u21d2 base b \u21d2 base b) \u21d2\n base b \u21d2 base (Map \u03ba a) \u21d2 base b)\n\nAtlas-\u0394base : Atlas-type \u2192 Atlas-type\n-- change to a boolean is a xor-rand\nAtlas-\u0394base Bool = Bool\n-- change to a map is change to its values\nAtlas-\u0394base (Map key val) = (Map key (Atlas-\u0394base val))\n\nAtlas-\u0394type : Type \u2192 Type\nAtlas-\u0394type = lift-\u0394type\u2080 Atlas-\u0394base\n\nopen import Syntax.Context {Type}\nopen import Syntax.Term.Plotkin {Atlas-type} {Atlas-const}\n\n-- Shorthands of constants\n--\n-- There's probably a uniform way to lift constants\n-- into term constructors.\n\ntrue! : \u2200 {\u0393} \u2192\n Term \u0393 (base Bool)\ntrue! = const true\n\nfalse! : \u2200 {\u0393} \u2192\n Term \u0393 (base Bool)\nfalse! = const false\n\nxor! : \u2200 {\u0393} \u2192\n Term \u0393 (base Bool) \u2192 Term \u0393 (base Bool) \u2192\n Term \u0393 (base Bool)\nxor! = app\u2082 (const xor)\n\nempty! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base (Map \u03ba \u03b9))\nempty! = const empty\n\nupdate! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base \u03b9) \u2192\n Term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Term \u0393 (base (Map \u03ba \u03b9))\nupdate! = app\u2083 (const update)\n\nlookup! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Term \u0393 (base \u03b9)\nlookup! = app\u2082 (const lookup)\n\nzip! : \u2200 {\u03ba a b c \u0393} \u2192\n Term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u2192\n Term \u0393 (base (Map \u03ba a)) \u2192 Term \u0393 (base (Map \u03ba b)) \u2192\n Term \u0393 (base (Map \u03ba c))\nzip! = app\u2083 (const zip)\n\nfold! : \u2200 {\u03ba a b \u0393} \u2192\n Term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base b) \u2192\n Term \u0393 (base b) \u2192 Term \u0393 (base (Map \u03ba a)) \u2192\n Term \u0393 (base b)\nfold! = app\u2083 (const fold)\n\n-- Every base type has a known nil-change.\n-- The nil-change of \u03b9 is also the neutral element of Map \u03ba \u0394\u03b9.\n\nneutral : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const (base \u03b9)\nneutral {Bool} = false\nneutral {Map \u03ba \u03b9} = empty {\u03ba} {\u03b9}\n\nneutral-term : \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9)\nneutral-term {Bool} = const (neutral {Bool})\nneutral-term {Map \u03ba \u03b9} = const (neutral {Map \u03ba \u03b9})\n\nnil-const : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const (base (Atlas-\u0394base \u03b9))\nnil-const {\u03b9} = neutral {Atlas-\u0394base \u03b9}\n\nnil-term : \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base (Atlas-\u0394base \u03b9))\nnil-term {Bool} = const (nil-const {Bool})\nnil-term {Map \u03ba \u03b9} = const (nil-const {Map \u03ba \u03b9})\n\n-- Nonfunctional products can be encoded.\n-- The incremental behavior of products thus encoded is weird:\n-- \u0394(\u03b1 \u00d7 \u03b2) = \u03b1 \u00d7 \u0394\u03b2\nPair : Atlas-type \u2192 Atlas-type \u2192 Atlas-type\nPair \u03b1 \u03b2 = Map \u03b1 \u03b2\n\npair : \u2200 {\u03b1 \u03b2 \u0393} \u2192\n Term \u0393 (base \u03b1) \u2192 Term \u0393 (base \u03b2) \u2192\n Term \u0393 (base (Pair \u03b1 \u03b2))\npair s t = update! s t empty!\n\npair-term : \u2200 {\u03b1 \u03b2 \u0393} \u2192\n Term \u0393 (base \u03b1 \u21d2 base \u03b2 \u21d2 base (Pair \u03b1 \u03b2))\npair-term = abs (abs (pair (var (that this)) (var this)))\n\nuncurry : \u2200 {\u03b1 \u03b2 \u03b3 \u0393} \u2192\n Term \u0393 (base \u03b1 \u21d2 base \u03b2 \u21d2 base \u03b3) \u2192\n Term \u0393 (base (Pair \u03b1 \u03b2)) \u2192\n Term \u0393 (base \u03b3)\nuncurry f p =\n let\n a = var (that (that this))\n b = var (that this)\n g = abs (abs (abs (app\u2082 (weaken\u2083 f) a b)))\n in\n fold! g neutral-term p\n\nzip-pair : \u2200 {\u03ba a b \u0393} \u2192\n Term \u0393 (base (Map \u03ba a)) \u2192 Term \u0393 (base (Map \u03ba b)) \u2192\n Term \u0393 (base (Map \u03ba (Pair a b)))\nzip-pair = zip! (abs pair-term)\n\n-- diff-term and apply-term\n\n-- b\u2080 \u229d b\u2081 = b\u2080 xor b\u2081\n-- m\u2080 \u229d m\u2081 = zip _\u229d_ m\u2080 m\u2081\n\nAtlas-diff : \u2200 {\u03b9 \u0393} \u2192\n Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 Atlas-\u0394type (base \u03b9))\nAtlas-diff {Bool} = const xor\nAtlas-diff {Map \u03ba \u03b9} = app (const zip) (abs Atlas-diff)\n\n-- b \u2295 \u0394b = b xor \u0394b\n-- m \u2295 \u0394m = zip _\u2295_ m \u0394m\n\nAtlas-apply : \u2200 {\u03b9 \u0393} \u2192\n Term \u0393 (Atlas-\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\nAtlas-apply {Bool} = const xor\nAtlas-apply {Map \u03ba \u03b9} = app (const zip) (abs Atlas-apply)\n\n-- Shorthands for working with diff-term and apply-term\n\ndiff : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 (Atlas-\u0394type \u03c4)\ndiff = app\u2082 (lift-diff Atlas-diff Atlas-apply)\n\napply : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (Atlas-\u0394type \u03c4) \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\napply = app\u2082 (lift-apply Atlas-diff Atlas-apply)\n\n-- Shorthands for creating changes corresponding to\n-- insertion\/deletion.\n\ninsert : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base \u03b9) \u2192\n -- last argument is the change accumulator\n Term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ndelete : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base \u03b9) \u2192\n Term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ninsert k v acc = update! k (diff v neutral-term) acc\ndelete k v acc = update! k (diff neutral-term v) acc\n\n-- Shorthand for 4-way zip\nzip4! : \u2200 {\u03ba a b c d e \u0393} \u2192\n let\n t:_ = \u03bb \u03b9 \u2192 Term \u0393 (base \u03b9)\n in\n Term \u0393\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c \u21d2 base d \u21d2 base e) \u2192\n t: Map \u03ba a \u2192 t: Map \u03ba b \u2192 t: Map \u03ba c \u2192 t: Map \u03ba d \u2192 t: Map \u03ba e\n\n-- zip\u2084 f m\u2081 m\u2082 m\u2083 m\u2084 =\n-- zip (\u03bb k p\u2081\u2082 p\u2083\u2084 \u2192 uncurry (\u03bb v\u2081 v\u2082 \u2192 uncurry (\u03bb v\u2083 v\u2084 \u2192\n-- f k v\u2081 v\u2082 v\u2083 v\u2084)\n-- p\u2083\u2084) p\u2081\u2082)\n-- (zip-pair m\u2081 m\u2082) (zip-pair m\u2083 m\u2084)\n\nzip4! f m\u2081 m\u2082 m\u2083 m\u2084 =\n let\n f\u2032 = weaken\u2083 (weaken\u2083 (weaken\u2081 f))\n k = var (that (that (that (that (that (that this))))))\n p\u2081\u2082 = var (that this)\n p\u2083\u2084 = var (that (that this))\n v\u2081 = var (that (that (that this)))\n v\u2082 = var (that (that this))\n v\u2083 = var (that this)\n v\u2084 = var this\n g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs\n (app\u2085 f\u2032 k v\u2081 v\u2082 v\u2083 v\u2084)))\n p\u2083\u2084))) p\u2081\u2082)))\n in\n zip! g (zip-pair m\u2081 m\u2082) (zip-pair m\u2083 m\u2084)\n\n-- Type signature of Atlas-\u0394const is boilerplate.\nAtlas-\u0394const : \u2200 {\u0393 \u03c4} \u2192 (c : Atlas-const \u03c4) \u2192\n Term \u0393 (Atlas-\u0394type \u03c4)\n\nAtlas-\u0394const true = false!\nAtlas-\u0394const false = false!\n\n-- \u0394xor = \u03bb x \u0394x y \u0394y \u2192 \u0394x xor \u0394y\nAtlas-\u0394const xor =\n let\n \u0394x = var (that (that this))\n \u0394y = var this\n in abs (abs (abs (abs (xor! \u0394x \u0394y))))\n\nAtlas-\u0394const empty = empty!\n\n-- If k \u2295 \u0394k \u2261 k, then\n-- \u0394update k \u0394k v \u0394v m \u0394m = update k \u0394v \u0394m\n-- else it is a deletion followed by insertion:\n-- \u0394update k \u0394k v \u0394v m \u0394m =\n-- insert (k \u2295 \u0394k) (v \u2295 \u0394v) (delete k v \u0394m)\n--\n-- We implement the else-branch only for the moment.\nAtlas-\u0394const update =\n let\n k = var (that (that (that (that (that this)))))\n \u0394k = var (that (that (that (that this))))\n v = var (that (that (that this)))\n \u0394v = var (that (that this))\n -- m = var (that this) -- unused parameter\n \u0394m = var this\n in\n abs (abs (abs (abs (abs (abs\n (insert (apply \u0394k k) (apply \u0394v v)\n (delete k v \u0394m)))))))\n\n-- \u0394lookup k \u0394k m \u0394m | true? (k \u2295 \u0394k \u2261 k)\n-- ... | true = lookup k \u0394m\n-- ... | false =\n-- (lookup (k \u2295 \u0394k) m \u2295 lookup (k \u2295 \u0394k) \u0394m)\n-- \u229d lookup k m\n--\n-- Only the false-branch is implemented.\nAtlas-\u0394const lookup =\n let\n k = var (that (that (that this)))\n \u0394k = var (that (that this))\n m = var (that this)\n \u0394m = var this\n k\u2032 = apply \u0394k k\n in\n abs (abs (abs (abs\n (diff (apply (lookup! k\u2032 \u0394m) (lookup! k\u2032 m))\n (lookup! k m)))))\n\n-- \u0394zip f \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082 | true? (f \u2295 \u0394f \u2261 f)\n--\n-- ... | true =\n-- zip (\u03bb k \u0394v\u2081 \u0394v\u2082 \u2192 \u0394f (lookup k m\u2081) \u0394v\u2081 (lookup k m\u2082) \u0394v\u2082)\n-- \u0394m\u2081 \u0394m\u2082\n--\n-- ... | false = zip\u2084 \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082\n--\n-- we implement the false-branch for the moment.\nAtlas-\u0394const zip =\n let\n \u0394f = var (that (that (that (that this))))\n m\u2081 = var (that (that (that this)))\n \u0394m\u2081 = var (that (that this))\n m\u2082 = var (that this)\n \u0394m\u2082 = var this\n g = abs (app\u2082 (weaken\u2081 \u0394f) (var this) nil-term)\n in\n abs (abs (abs (abs (abs (abs (zip4! g m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082))))))\n\n-- \u0394fold f \u0394f z \u0394z m \u0394m = proj\u2082\n-- (fold (\u03bb k [a,\u0394a] [b,\u0394b] \u2192\n-- uncurry (\u03bb a \u0394a \u2192 uncurry (\u03bb b \u0394b \u2192\n-- pair (f k a b) (\u0394f k nil a \u0394a b \u0394b))\n-- [b,\u0394b]) [a,\u0394a])\n-- (pair z \u0394z)\n-- (zip-pair m \u0394m))\n--\n-- \u0394fold is efficient only if evaluation is lazy and \u0394f is\n-- self-maintainable: it doesn't look at the argument\n-- (b = fold f k a b\u2080) at all.\nAtlas-\u0394const (fold {\u03ba} {\u03b1} {\u03b2}) =\n let -- TODO (tedius): write weaken\u2087\n f = weaken\u2083 (weaken\u2083 (weaken\u2081\n (var (that (that (that (that (that this))))))))\n \u0394f = weaken\u2083 (weaken\u2083 (weaken\u2081\n (var (that (that (that (that this)))))))\n z = var (that (that (that this)))\n \u0394z = var (that (that this))\n m = var (that this)\n \u0394m = var this\n k = weaken\u2083 (weaken\u2081 (var (that (that this))))\n [a,\u0394a] = var (that this)\n [b,\u0394b] = var this\n a = var (that (that (that this)))\n \u0394a = var (that (that this))\n b = var (that this)\n \u0394b = var this\n g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs\n (pair (app\u2083 f k a b)\n (app\u2086 \u0394f k nil-term a \u0394a b \u0394b))))\n (weaken\u2082 [b,\u0394b])))) [a,\u0394a])))\n proj\u2082 = uncurry (abs (abs (var this)))\n in\n abs (abs (abs (abs (abs (abs\n (proj\u2082 (fold! g (pair z \u0394z) (zip-pair m \u0394m))))))))\n\nopen import Syntax.Language.Calculus\n\nAtlas = calculus-with\n Atlas-type\n Atlas-const\n Atlas-\u0394type\n Atlas-\u0394const\n","old_contents":"module Syntax.Language.Atlas where\n\n-- Base types of the calculus Atlas\n-- to be used with Plotkin-style language description\n--\n-- Atlas supports maps with neutral elements. The change type to\n-- `Map \u03ba \u03b9` is `Map \u03ba \u0394\u03b9`, where \u0394\u03b9 is the change type of the\n-- ground type \u03b9. Such a change to maps can support insertions\n-- and deletions as well: Inserting `k -> v` means mapping `k` to\n-- the change from the neutral element to `v`, and deleting\n-- `k -> v` means mapping `k` to the change from `v` to the\n-- neutral element.\n\ndata Atlas-type : Set where\n Bool : Atlas-type\n Map : (\u03ba : Atlas-type) (\u03b9 : Atlas-type) \u2192 Atlas-type\n\nopen import Syntax.Type.Plotkin Atlas-type\n\ndata Atlas-const : Type \u2192 Set where\n true : Atlas-const\n (base Bool)\n\n false : Atlas-const\n (base Bool)\n\n xor : Atlas-const\n (base Bool \u21d2 base Bool \u21d2 base Bool)\n\n empty : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base (Map \u03ba \u03b9))\n\n -- `update key val my-map` would\n -- - insert if `key` is not present in `my-map`\n -- - delete if `val` is the neutral element\n -- - make an update otherwise\n\n update : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base \u03ba \u21d2 base \u03b9 \u21d2 base (Map \u03ba \u03b9) \u21d2 base (Map \u03ba \u03b9))\n\n lookup : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n (base \u03ba \u21d2 base (Map \u03ba \u03b9) \u21d2 base \u03b9)\n\n -- Model of zip = Haskell Data.List.zipWith\n --\n -- zipWith :: (a \u2192 b \u2192 c) \u2192 [a] \u2192 [b] \u2192 [c]\n --\n -- Behavioral difference: all key-value pairs present\n -- in *either* (m\u2081 : Map \u03ba a) *or* (m\u2082 : Map \u03ba b) will\n -- be iterated over. Neutral element of type `a` or `b`\n -- will be supplied if the key is missing in the\n -- corresponding map.\n\n zip : \u2200 {\u03ba a b c : Atlas-type} \u2192 Atlas-const\n ((base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u21d2\n base (Map \u03ba a) \u21d2 base (Map \u03ba b) \u21d2 base (Map \u03ba c))\n\n -- Model of fold = Haskell Data.Map.foldWithKey\n --\n -- foldWithKey :: (k \u2192 a \u2192 b \u2192 b) \u2192 b \u2192 Map k a \u2192 b\n\n fold : \u2200 {\u03ba a b : Atlas-type} \u2192 Atlas-const\n ((base \u03ba \u21d2 base a \u21d2 base b \u21d2 base b) \u21d2\n base b \u21d2 base (Map \u03ba a) \u21d2 base b)\n\nAtlas-\u0394base : Atlas-type \u2192 Atlas-type\n-- change to a boolean is a xor-rand\nAtlas-\u0394base Bool = Bool\n-- change to a map is change to its values\nAtlas-\u0394base (Map key val) = (Map key (Atlas-\u0394base val))\n\nAtlas-\u0394type : Type \u2192 Type\nAtlas-\u0394type = lift-\u0394type\u2080 Atlas-\u0394base\n\nopen import Syntax.Context {Type}\n\nopen import Syntax.Term.Plotkin\n\nAtlas-term : Context \u2192 Type \u2192 Set\nAtlas-term = Term {Atlas-type} {Atlas-const}\n\n-- Shorthands of constants\n--\n-- There's probably a uniform way to lift constants\n-- into term constructors.\n\ntrue! : \u2200 {\u0393} \u2192\n Atlas-term \u0393 (base Bool)\ntrue! = const true\n\nfalse! : \u2200 {\u0393} \u2192\n Atlas-term \u0393 (base Bool)\nfalse! = const false\n\nxor! : \u2200 {\u0393} \u2192\n Atlas-term \u0393 (base Bool) \u2192 Atlas-term \u0393 (base Bool) \u2192\n Atlas-term \u0393 (base Bool)\nxor! = app\u2082 (const xor)\n\nempty! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9))\nempty! = const empty\n\nupdate! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9))\nupdate! = app\u2083 (const update)\n\nlookup! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Atlas-term \u0393 (base \u03b9)\nlookup! = app\u2082 (const lookup)\n\nzip! : \u2200 {\u03ba a b c \u0393} \u2192\n Atlas-term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u2192\n Atlas-term \u0393 (base (Map \u03ba a)) \u2192 Atlas-term \u0393 (base (Map \u03ba b)) \u2192\n Atlas-term \u0393 (base (Map \u03ba c))\nzip! = app\u2083 (const zip)\n\nfold! : \u2200 {\u03ba a b \u0393} \u2192\n Atlas-term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base b) \u2192\n Atlas-term \u0393 (base b) \u2192 Atlas-term \u0393 (base (Map \u03ba a)) \u2192\n Atlas-term \u0393 (base b)\nfold! = app\u2083 (const fold)\n\n-- Every base type has a known nil-change.\n-- The nil-change of \u03b9 is also the neutral element of Map \u03ba \u0394\u03b9.\n\nneutral : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const (base \u03b9)\nneutral {Bool} = false\nneutral {Map \u03ba \u03b9} = empty {\u03ba} {\u03b9}\n\nneutral-term : \u2200 {\u03b9 \u0393} \u2192 Atlas-term \u0393 (base \u03b9)\nneutral-term {Bool} = const (neutral {Bool})\nneutral-term {Map \u03ba \u03b9} = const (neutral {Map \u03ba \u03b9})\n\nnil-const : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const (base (Atlas-\u0394base \u03b9))\nnil-const {\u03b9} = neutral {Atlas-\u0394base \u03b9}\n\nnil-term : \u2200 {\u03b9 \u0393} \u2192 Atlas-term \u0393 (base (Atlas-\u0394base \u03b9))\nnil-term {Bool} = const (nil-const {Bool})\nnil-term {Map \u03ba \u03b9} = const (nil-const {Map \u03ba \u03b9})\n\n-- Nonfunctional products can be encoded.\n-- The incremental behavior of products thus encoded is weird:\n-- \u0394(\u03b1 \u00d7 \u03b2) = \u03b1 \u00d7 \u0394\u03b2\nPair : Atlas-type \u2192 Atlas-type \u2192 Atlas-type\nPair \u03b1 \u03b2 = Map \u03b1 \u03b2\n\npair : \u2200 {\u03b1 \u03b2 \u0393} \u2192\n Atlas-term \u0393 (base \u03b1) \u2192 Atlas-term \u0393 (base \u03b2) \u2192\n Atlas-term \u0393 (base (Pair \u03b1 \u03b2))\npair s t = update! s t empty!\n\npair-term : \u2200 {\u03b1 \u03b2 \u0393} \u2192\n Atlas-term \u0393 (base \u03b1 \u21d2 base \u03b2 \u21d2 base (Pair \u03b1 \u03b2))\npair-term = abs (abs (pair (var (that this)) (var this)))\n\nuncurry : \u2200 {\u03b1 \u03b2 \u03b3 \u0393} \u2192\n Atlas-term \u0393 (base \u03b1 \u21d2 base \u03b2 \u21d2 base \u03b3) \u2192\n Atlas-term \u0393 (base (Pair \u03b1 \u03b2)) \u2192\n Atlas-term \u0393 (base \u03b3)\nuncurry f p =\n let\n a = var (that (that this))\n b = var (that this)\n g = abs (abs (abs (app\u2082 (weaken\u2083 f) a b)))\n in\n fold! g neutral-term p\n\nzip-pair : \u2200 {\u03ba a b \u0393} \u2192\n Atlas-term \u0393 (base (Map \u03ba a)) \u2192 Atlas-term \u0393 (base (Map \u03ba b)) \u2192\n Atlas-term \u0393 (base (Map \u03ba (Pair a b)))\nzip-pair = zip! (abs pair-term)\n\n-- diff-term and apply-term\n\n-- b\u2080 \u229d b\u2081 = b\u2080 xor b\u2081\n-- m\u2080 \u229d m\u2081 = zip _\u229d_ m\u2080 m\u2081\n\nAtlas-diff : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 Atlas-\u0394type (base \u03b9))\nAtlas-diff {Bool} = const xor\nAtlas-diff {Map \u03ba \u03b9} = app (const zip) (abs Atlas-diff)\n\n-- b \u2295 \u0394b = b xor \u0394b\n-- m \u2295 \u0394m = zip _\u2295_ m \u0394m\n\nAtlas-apply : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\nAtlas-apply {Bool} = const xor\nAtlas-apply {Map \u03ba \u03b9} = app (const zip) (abs Atlas-apply)\n\n-- Shorthands for working with diff-term and apply-term\n\ndiff : \u2200 {\u03c4 \u0393} \u2192\n Atlas-term \u0393 \u03c4 \u2192 Atlas-term \u0393 \u03c4 \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4)\ndiff = app\u2082 (lift-diff Atlas-diff Atlas-apply)\n\napply : \u2200 {\u03c4 \u0393} \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4) \u2192 Atlas-term \u0393 \u03c4 \u2192\n Atlas-term \u0393 \u03c4\napply = app\u2082 (lift-apply Atlas-diff Atlas-apply)\n\n-- Shorthands for creating changes corresponding to\n-- insertion\/deletion.\n\ninsert : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n -- last argument is the change accumulator\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ndelete : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ninsert k v acc = update! k (diff v neutral-term) acc\ndelete k v acc = update! k (diff neutral-term v) acc\n\n-- Shorthand for 4-way zip\nzip4! : \u2200 {\u03ba a b c d e \u0393} \u2192\n let\n t:_ = \u03bb \u03b9 \u2192 Atlas-term \u0393 (base \u03b9)\n in\n Atlas-term \u0393\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c \u21d2 base d \u21d2 base e) \u2192\n t: Map \u03ba a \u2192 t: Map \u03ba b \u2192 t: Map \u03ba c \u2192 t: Map \u03ba d \u2192 t: Map \u03ba e\n\n-- zip\u2084 f m\u2081 m\u2082 m\u2083 m\u2084 =\n-- zip (\u03bb k p\u2081\u2082 p\u2083\u2084 \u2192 uncurry (\u03bb v\u2081 v\u2082 \u2192 uncurry (\u03bb v\u2083 v\u2084 \u2192\n-- f k v\u2081 v\u2082 v\u2083 v\u2084)\n-- p\u2083\u2084) p\u2081\u2082)\n-- (zip-pair m\u2081 m\u2082) (zip-pair m\u2083 m\u2084)\n\nzip4! f m\u2081 m\u2082 m\u2083 m\u2084 =\n let\n f\u2032 = weaken\u2083 (weaken\u2083 (weaken\u2081 f))\n k = var (that (that (that (that (that (that this))))))\n p\u2081\u2082 = var (that this)\n p\u2083\u2084 = var (that (that this))\n v\u2081 = var (that (that (that this)))\n v\u2082 = var (that (that this))\n v\u2083 = var (that this)\n v\u2084 = var this\n g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs\n (app\u2085 f\u2032 k v\u2081 v\u2082 v\u2083 v\u2084)))\n p\u2083\u2084))) p\u2081\u2082)))\n in\n zip! g (zip-pair m\u2081 m\u2082) (zip-pair m\u2083 m\u2084)\n\n-- Type signature of Atlas-\u0394const is boilerplate.\nAtlas-\u0394const : \u2200 {\u0393 \u03c4} \u2192 (c : Atlas-const \u03c4) \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4)\n\nAtlas-\u0394const true = false!\nAtlas-\u0394const false = false!\n\n-- \u0394xor = \u03bb x \u0394x y \u0394y \u2192 \u0394x xor \u0394y\nAtlas-\u0394const xor =\n let\n \u0394x = var (that (that this))\n \u0394y = var this\n in abs (abs (abs (abs (xor! \u0394x \u0394y))))\n\nAtlas-\u0394const empty = empty!\n\n-- If k \u2295 \u0394k \u2261 k, then\n-- \u0394update k \u0394k v \u0394v m \u0394m = update k \u0394v \u0394m\n-- else it is a deletion followed by insertion:\n-- \u0394update k \u0394k v \u0394v m \u0394m =\n-- insert (k \u2295 \u0394k) (v \u2295 \u0394v) (delete k v \u0394m)\n--\n-- We implement the else-branch only for the moment.\nAtlas-\u0394const update =\n let\n k = var (that (that (that (that (that this)))))\n \u0394k = var (that (that (that (that this))))\n v = var (that (that (that this)))\n \u0394v = var (that (that this))\n -- m = var (that this) -- unused parameter\n \u0394m = var this\n in\n abs (abs (abs (abs (abs (abs\n (insert (apply \u0394k k) (apply \u0394v v)\n (delete k v \u0394m)))))))\n\n-- \u0394lookup k \u0394k m \u0394m | true? (k \u2295 \u0394k \u2261 k)\n-- ... | true = lookup k \u0394m\n-- ... | false =\n-- (lookup (k \u2295 \u0394k) m \u2295 lookup (k \u2295 \u0394k) \u0394m)\n-- \u229d lookup k m\n--\n-- Only the false-branch is implemented.\nAtlas-\u0394const lookup =\n let\n k = var (that (that (that this)))\n \u0394k = var (that (that this))\n m = var (that this)\n \u0394m = var this\n k\u2032 = apply \u0394k k\n in\n abs (abs (abs (abs\n (diff (apply (lookup! k\u2032 \u0394m) (lookup! k\u2032 m))\n (lookup! k m)))))\n\n-- \u0394zip f \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082 | true? (f \u2295 \u0394f \u2261 f)\n--\n-- ... | true =\n-- zip (\u03bb k \u0394v\u2081 \u0394v\u2082 \u2192 \u0394f (lookup k m\u2081) \u0394v\u2081 (lookup k m\u2082) \u0394v\u2082)\n-- \u0394m\u2081 \u0394m\u2082\n--\n-- ... | false = zip\u2084 \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082\n--\n-- we implement the false-branch for the moment.\nAtlas-\u0394const zip =\n let\n \u0394f = var (that (that (that (that this))))\n m\u2081 = var (that (that (that this)))\n \u0394m\u2081 = var (that (that this))\n m\u2082 = var (that this)\n \u0394m\u2082 = var this\n g = abs (app\u2082 (weaken\u2081 \u0394f) (var this) nil-term)\n in\n abs (abs (abs (abs (abs (abs (zip4! g m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082))))))\n\n-- \u0394fold f \u0394f z \u0394z m \u0394m = proj\u2082\n-- (fold (\u03bb k [a,\u0394a] [b,\u0394b] \u2192\n-- uncurry (\u03bb a \u0394a \u2192 uncurry (\u03bb b \u0394b \u2192\n-- pair (f k a b) (\u0394f k nil a \u0394a b \u0394b))\n-- [b,\u0394b]) [a,\u0394a])\n-- (pair z \u0394z)\n-- (zip-pair m \u0394m))\n--\n-- \u0394fold is efficient only if evaluation is lazy and \u0394f is\n-- self-maintainable: it doesn't look at the argument\n-- (b = fold f k a b\u2080) at all.\nAtlas-\u0394const (fold {\u03ba} {\u03b1} {\u03b2}) =\n let -- TODO (tedius): write weaken\u2087\n f = weaken\u2083 (weaken\u2083 (weaken\u2081\n (var (that (that (that (that (that this))))))))\n \u0394f = weaken\u2083 (weaken\u2083 (weaken\u2081\n (var (that (that (that (that this)))))))\n z = var (that (that (that this)))\n \u0394z = var (that (that this))\n m = var (that this)\n \u0394m = var this\n k = weaken\u2083 (weaken\u2081 (var (that (that this))))\n [a,\u0394a] = var (that this)\n [b,\u0394b] = var this\n a = var (that (that (that this)))\n \u0394a = var (that (that this))\n b = var (that this)\n \u0394b = var this\n g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs\n (pair (app\u2083 f k a b)\n (app\u2086 \u0394f k nil-term a \u0394a b \u0394b))))\n (weaken\u2082 [b,\u0394b])))) [a,\u0394a])))\n proj\u2082 = uncurry (abs (abs (var this)))\n in\n abs (abs (abs (abs (abs (abs\n (proj\u2082 (fold! g (pair z \u0394z) (zip-pair m \u0394m))))))))\n\nopen import Syntax.Language.Calculus\n\nAtlas = calculus-with\n Atlas-type\n Atlas-const\n Atlas-\u0394type\n Atlas-\u0394const\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"42ae1a35bb1ac9d1c07108e9ae8061964838aae2","subject":"Add elimDesc to formalization","message":"Add elimDesc to formalization\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/Examples\/PropLev.agda","new_file":"formalization\/agda\/Spire\/Examples\/PropLev.agda","new_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nopen import Function\nmodule Spire.Examples.PropLev where\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nBranches : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nBranches [] P = \u22a4\nBranches (l \u2237 E) P = P here \u00d7 Branches E (\u03bb t \u2192 P (there t))\n\ncase : {E : Enum} (P : Tag E \u2192 Set) (cs : Branches E P) (t : Tag E) \u2192 P t\ncase P (c , cs) here = c\ncase P (c , cs) (there t) = case (\u03bb t \u2192 P (there t)) cs t\n\nUncurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedBranches E P X = Branches E P \u2192 X\n\nCurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedBranches [] P X = X\nCurriedBranches (l \u2237 E) P X = P here \u2192 CurriedBranches E (\u03bb t \u2192 P (there t)) X\n\ncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 UncurriedBranches E P X \u2192 CurriedBranches E P X\ncurryBranches {[]} f = f tt\ncurryBranches {l \u2237 E} f = \u03bb c \u2192 curryBranches (\u03bb cs \u2192 f (c , cs))\n\nuncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 CurriedBranches E P X \u2192 UncurriedBranches E P X\nuncurryBranches {[]} x tt = x\nuncurryBranches {l \u2237 E} f (c , cs) = uncurryBranches (f c) cs\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n End : (i : I) \u2192 Desc I\n Rec : (i : I) (D : Desc I) \u2192 Desc I\n Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n\nelimDesc : {I : Set} (P : Desc I \u2192 Set)\n (pend : (i : I) \u2192 P (End i))\n (prec : (i : I) (D : Desc I) (pd : P D) \u2192 P (Rec i D))\n (parg : (A : Set) (B : A \u2192 Desc I) (pb : (a : A) \u2192 P (B a)) \u2192 P (Arg A B))\n (D : Desc I) \u2192 P D\nelimDesc P pend prec parg (End i) = pend i\nelimDesc P pend prec parg (Rec i D) = prec i D (elimDesc P pend prec parg D)\nelimDesc P pend prec parg (Arg A B) = parg A B (\u03bb a \u2192 elimDesc P pend prec parg (B a))\n\n----------------------------------------------------------------------\n\nISet : Set \u2192 Set\u2081\nISet I = I \u2192 Set\n\nEl : {I : Set} (D : Desc I) \u2192 ISet I \u2192 ISet I\nEl (End j) X i = j \u2261 i\nEl (Rec j D) X i = X j \u00d7 El D X i\nEl (Arg A B) X i = \u03a3 A (\u03bb a \u2192 El (B a) X i)\n\nHyps : {I : Set} (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El D X i) \u2192 Set\nHyps (End j) X P i q = \u22a4\nHyps (Rec j D) X P i (x , xs) = P j x \u00d7 Hyps D X P i xs\nHyps (Arg A B) X P i (a , b) = Hyps (B a) X P i b\n\n----------------------------------------------------------------------\n\nBranchesD : (I : Set) (E : Enum) \u2192 Set\nBranchesD I E = Branches E (\u03bb _ \u2192 Desc I)\n\ncaseD : {I : Set} {E : Enum} (cs : BranchesD I E) (t : Tag E) \u2192 Desc I\ncaseD = case (\u03bb _ \u2192 Desc _)\n\n----------------------------------------------------------------------\n\nUncurriedEl : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl D X = \u2200{i} \u2192 El D X i \u2192 X i\n\nCurriedEl : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl (End i) X = X i\nCurriedEl (Rec i D) X = (x : X i) \u2192 CurriedEl D X\nCurriedEl (Arg A B) X = (a : A) \u2192 CurriedEl (B a) X\n\nCurriedEl' : {I : Set} (D : Desc I) (X : ISet I) (i : I) \u2192 Set\nCurriedEl' (End j) X i = j \u2261 i \u2192 X i\nCurriedEl' (Rec j D) X i = (x : X j) \u2192 CurriedEl' D X i\nCurriedEl' (Arg A B) X i = (a : A) \u2192 CurriedEl' (B a) X i\n\ncurryEl : {I : Set} (D : Desc I) (X : ISet I)\n \u2192 UncurriedEl D X \u2192 CurriedEl D X\ncurryEl (End i) X cn = cn refl\ncurryEl (Rec i D) X cn = \u03bb x \u2192 curryEl D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl (Arg A B) X cn = \u03bb a \u2192 curryEl (B a) X (\u03bb xs \u2192 cn (a , xs))\n\nuncurryEl : {I : Set} (D : Desc I) (X : ISet I)\n \u2192 CurriedEl D X \u2192 UncurriedEl D X\nuncurryEl (End i) X cn refl = cn\nuncurryEl (Rec i D) X cn (x , xs) = uncurryEl D X (cn x) xs\nuncurryEl (Arg A B) X cn (a , xs) = uncurryEl (B a) X (cn a) xs\n\n----------------------------------------------------------------------\n\nUncurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl D X)\n \u2192 Set\nUncurriedHyps D X P cn =\n \u2200 i (xs : El D X i) (ihs : Hyps D X P i xs) \u2192 P i (cn xs)\n\nCurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl D X)\n \u2192 Set\nCurriedHyps (End i) X P cn =\n P i (cn refl)\nCurriedHyps (Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedHyps D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedHyps (Arg A B) X P cn =\n (a : A) \u2192 CurriedHyps (B a) X P (\u03bb xs \u2192 cn (a , xs))\n\nCurriedHyps' : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (i : I)\n (cn : El D X i \u2192 X i)\n \u2192 Set\nCurriedHyps' (End j) X P i cn = (q : j \u2261 i) \u2192 P i (cn q)\nCurriedHyps' (Rec j D) X P i cn =\n (x : X j) \u2192 P j x \u2192 CurriedHyps' D X P i (\u03bb xs \u2192 cn (x , xs))\nCurriedHyps' (Arg A B) X P i cn =\n (a : A) \u2192 CurriedHyps' (B a) X P i (\u03bb xs \u2192 cn (a , xs))\n\ncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl D X)\n \u2192 UncurriedHyps D X P cn\n \u2192 CurriedHyps D X P cn\ncurryHyps (End i) X P cn pf =\n pf i refl tt\ncurryHyps (Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryHyps D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryHyps (Arg A B) X P cn pf =\n \u03bb a \u2192 curryHyps (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\n\nuncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl D X)\n \u2192 CurriedHyps D X P cn\n \u2192 UncurriedHyps D X P cn\nuncurryHyps (End .i) X P cn pf i refl tt =\n pf\nuncurryHyps (Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryHyps D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryHyps (Arg A B) X P cn pf i (a , xs) ihs =\n uncurryHyps (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\n\n----------------------------------------------------------------------\n\ndata \u03bc {I : Set} (E : Enum) (C : Tag E \u2192 Desc I) : ISet I where\n init : (t : Tag E) \u2192 UncurriedEl (C t) (\u03bc E C)\n\ninj : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I) (t : Tag E) \u2192 CurriedEl (C t) (\u03bc E C)\ninj E C t = curryEl (C t) (\u03bc E C) (init t)\n\n----------------------------------------------------------------------\n\nhyps : {I : Set} (D : Desc I) (X : I \u2192 Set)\n (P : (i : I) \u2192 X i \u2192 Set)\n (\u03b1 : (i : I) (x : X i) \u2192 P i x)\n (i : I) (xs : El D X i) \u2192 Hyps D X P i xs\nhyps (End j) X P \u03b1 i xs = tt\nhyps (Rec j D) X P \u03b1 i x,xs = \u03b1 j (proj\u2081 x,xs) , hyps D X P \u03b1 i (proj\u2082 x,xs)\nhyps (Arg A B) X P \u03b1 i a,xs = hyps (B (proj\u2081 a,xs)) X P \u03b1 i (proj\u2082 a,xs)\n\n{-# NO_TERMINATION_CHECK #-}\nind : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n (\u03b1 : (t : Tag E) \u2192 UncurriedHyps (C t) (\u03bc E C) P (init t))\n (i : I)\n (x : \u03bc E C i)\n \u2192 P i x\nind E C P \u03b1 i (init t xs) = \u03b1 t i xs $\n hyps (C t) (\u03bc E C) P (ind E C P \u03b1) i xs\n\n----------------------------------------------------------------------\n\nindCurried : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n (f : (t : Tag E) \u2192 CurriedHyps (C t) (\u03bc E C) P (init t))\n (i : I)\n (x : \u03bc E C i)\n \u2192 P i x\nindCurried E C P f i x = ind E C P (\u03bb t \u2192 uncurryHyps (C t) (\u03bc E C) P (init t) (f t)) i x\n\nSummer : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (X : ISet I) (cn : (t : Tag E) \u2192 UncurriedEl (C t) X)\n (P : (i : I) \u2192 X i \u2192 Set)\n \u2192 Tag E \u2192 Set\nSummer E C X cn P t = CurriedHyps (C t) X P (cn t)\n\nSumCurriedHyps : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n \u2192 Tag E \u2192 Set\nSumCurriedHyps E C P t = Summer E C (\u03bc E C) init P t\n\nelimUncurried : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n \u2192 Branches E (SumCurriedHyps E C P)\n \u2192 (i : I) (x : \u03bc E C i) \u2192 P i x\nelimUncurried E C P cs i x =\n indCurried E C P\n (case (SumCurriedHyps E C P) cs)\n i x\n\nelim : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n \u2192 CurriedBranches E\n (SumCurriedHyps E C P)\n ((i : I) (x : \u03bc E C i) \u2192 P i x)\nelim E C P = curryBranches (elimUncurried E C P)\n\n----------------------------------------------------------------------\n\nSoundness : Set\u2081\nSoundness = {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n (cs : Branches E (SumCurriedHyps E C P))\n (i : I) (x : \u03bc E C i)\n \u2192 \u2203 \u03bb \u03b1\n \u2192 elimUncurried E C P cs i x \u2261 ind E C P \u03b1 i x\n\nsound : Soundness\nsound E C P cs i xs =\n let D = Arg (Tag E) C in\n (\u03bb t \u2192 uncurryHyps (C t) (\u03bc E C) P (init t) (case (SumCurriedHyps E C P) cs t)) , refl\n\nCompleteness : Set\u2081\nCompleteness = {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n (\u03b1 : (t : Tag E) \u2192 UncurriedHyps (C t) (\u03bc E C) P (init t))\n (i : I) (x : \u03bc E C i)\n \u2192 \u2203 \u03bb cs\n \u2192 ind E C P \u03b1 i x \u2261 elimUncurried E C P cs i x\n\nuncurryHypsIdent : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl D X)\n (\u03b1 : UncurriedHyps D X P cn)\n (i : I) (xs : El D X i) (ihs : Hyps D X P i xs)\n \u2192 \u03b1 i xs ihs \u2261 uncurryHyps D X P cn (curryHyps D X P cn \u03b1) i xs ihs\nuncurryHypsIdent (End .i) X P cn \u03b1 i refl tt = refl\nuncurryHypsIdent (Rec j D) X P cn \u03b1 i (x , xs) (p , ps) =\n uncurryHypsIdent D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb k ys rs \u2192 \u03b1 k (x , ys) (p , rs)) i xs ps\nuncurryHypsIdent (Arg A B) X P cn \u03b1 i (a , xs) ps =\n uncurryHypsIdent (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb j ys \u2192 \u03b1 j (a , ys)) i xs ps\n\npostulate\n ext4 : {A : Set} {B : A \u2192 Set} {C : (a : A) \u2192 B a \u2192 Set}\n {D : (a : A) (b : B a) \u2192 C a b \u2192 Set}\n {Z : (a : A) (b : B a) (c : C a b) \u2192 D a b c \u2192 Set}\n (f g : (a : A) (b : B a) (c : C a b) (d : D a b c) \u2192 Z a b c d)\n \u2192 ((a : A) (b : B a) (c : C a b) (d : D a b c) \u2192 f a b c d \u2261 g a b c d)\n \u2192 f \u2261 g\n\ntoBranches : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (X : ISet I) (cn : (t : Tag E) \u2192 UncurriedEl (C t) X)\n (P : (i : I) \u2192 X i \u2192 Set)\n (\u03b1 : (t : Tag E) \u2192 UncurriedHyps (C t) X P (cn t))\n \u2192 Branches E (Summer E C X cn P)\ntoBranches [] C X cn P \u03b1 = tt\ntoBranches (l \u2237 E) C X cn P \u03b1 =\n curryHyps (C here) X P (\u03bb xs \u2192 cn here xs) (\u03bb i xs \u2192 \u03b1 here i xs)\n , toBranches E (\u03bb t \u2192 C (there t)) X\n (\u03bb t \u2192 cn (there t))\n P (\u03bb t i xs ih \u2192 \u03b1 (there t) i xs ih)\n\nToBranches : {I : Set} {E : Enum} (C : Tag E \u2192 Desc I)\n (X : ISet I) (cn : (t : Tag E) \u2192 UncurriedEl (C t) X)\n (P : (i : I) \u2192 X i \u2192 Set)\n (\u03b1 : (t : Tag E) \u2192 UncurriedHyps (C t) X P (cn t))\n (t : Tag E)\n \u2192 let \u03b2 = toBranches E C X cn P \u03b1 in\n case (Summer E C X cn P) \u03b2 t \u2261 curryHyps (C t) X P (cn t) (\u03b1 t)\nToBranches C X cn P \u03b1 here = refl\nToBranches C X cn P \u03b1 (there t)\n with ToBranches (\u03bb t \u2192 C (there t)) X\n (\u03bb t xs \u2192 cn (there t) xs)\n P (\u03bb t i xs ih \u2192 \u03b1 (there t) i xs ih) t\n... | ih rewrite ih = refl\n\ncomplete\u03b1 : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n (\u03b1 : (t : Tag E) \u2192 UncurriedHyps (C t) (\u03bc E C) P (init t))\n (t : Tag E) (i : I) (xs : El (C t) (\u03bc E C) i) (ihs : Hyps (C t) (\u03bc E C) P i xs)\n \u2192 let \u03b2 = toBranches E C (\u03bc E C) init P \u03b1 in\n \u03b1 t i xs ihs \u2261 uncurryHyps (C t) (\u03bc E C) P (init t) (case (SumCurriedHyps E C P) \u03b2 t) i xs ihs\ncomplete\u03b1 E C P \u03b1 t i xs ihs\n with ToBranches C (\u03bc E C) init P \u03b1 t\n... | q rewrite q = uncurryHypsIdent (C t) (\u03bc E C) P (init t) (\u03b1 t) i xs ihs\n\ncomplete' : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n (\u03b1 : (t : Tag E) \u2192 UncurriedHyps (C t) (\u03bc E C) P (init t))\n (i : I) (x : \u03bc E C i)\n \u2192 let \u03b2 = toBranches E C (\u03bc E C) init P \u03b1 in\n ind E C P \u03b1 i x \u2261 elimUncurried E C P \u03b2 i x\ncomplete' E C P \u03b1 i (init t xs) = cong\n (\u03bb f \u2192 ind E C P f i (init t xs))\n (ext4 \u03b1\n (\u03bb t \u2192 uncurryHyps (C t) (\u03bc E C) P (init t) (case (SumCurriedHyps E C P) \u03b2 t))\n (complete\u03b1 E C P \u03b1)\n )\n where \u03b2 = toBranches E C (\u03bc E C) init P \u03b1\n\ncomplete : Completeness\ncomplete E C P \u03b1 i x =\n let D = Arg (Tag E) C in\n toBranches E C (\u03bc E C) init P \u03b1\n , complete' E C P \u03b1 i x\n\n----------------------------------------------------------------------\n\n\u2115E : Enum\n\u2115E = \"zero\" \u2237 \"suc\" \u2237 []\n\nVecE : Enum\nVecE = \"nil\" \u2237 \"cons\" \u2237 []\n\n\u2115T : Set\n\u2115T = Tag \u2115E\n\nVecT : Set\nVecT = Tag VecE\n\nzeroT : \u2115T\nzeroT = here\n\nsucT : \u2115T\nsucT = there here\n\nnilT : VecT\nnilT = here\n\nconsT : VecT\nconsT = there here\n\n\u2115C : \u2115T \u2192 Desc \u22a4\n\u2115C = caseD $\n End tt\n , Rec tt (End tt)\n , tt\n\n\u2115D : Desc \u22a4\n\u2115D = Arg \u2115T \u2115C\n\n\u2115 : \u22a4 \u2192 Set\n\u2115 = \u03bc \u2115E \u2115C\n\nzero : \u2115 tt\nzero = init zeroT refl\n\nsuc : \u2115 tt \u2192 \u2115 tt\nsuc n = init sucT (n , refl)\n\nVecC : (A : Set) \u2192 VecT \u2192 Desc (\u2115 tt)\nVecC A = caseD $\n End zero\n , Arg (\u2115 tt) (\u03bb n \u2192 Arg A \u03bb _ \u2192 Rec n (End (suc n)))\n , tt\n\nnilD : (A : Set) \u2192 Desc (\u2115 tt)\nnilD A = End zero\n\nconsD : (A : Set) \u2192 Desc (\u2115 tt)\nconsD A = Arg (\u2115 tt) (\u03bb n \u2192 Arg A (\u03bb _ \u2192 Rec n (End (suc n))))\n\nVecD : (A : Set) \u2192 Desc (\u2115 tt)\nVecD A = Arg VecT (VecC A)\n\nVec : (A : Set) \u2192 \u2115 tt \u2192 Set\nVec A = \u03bc VecE (VecC A)\n\nNilEl : (A : Set) (n : \u2115 tt) \u2192 Set\nNilEl A n = El (nilD A) (Vec A) n\n\nConsEl : (A : Set) \u2192 \u2115 tt \u2192 Set\nConsEl A n = El (consD A) (Vec A) n\n\nVecEl : (A : Set) \u2192 \u2115 tt \u2192 Set\nVecEl A n = El (VecD A) (Vec A) n\n\nNilHyps : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set) (n : \u2115 tt) (xs : NilEl A n) \u2192 Set\nNilHyps A P n xs = Hyps (nilD A) (Vec A) P n xs\n\nConsHyps : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set) (n : \u2115 tt) (xs : ConsEl A n) \u2192 Set\nConsHyps A P n xs = Hyps (consD A) (Vec A) P n xs\n\nVecHyps : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set) (n : \u2115 tt) (xs : VecEl A n) \u2192 Set\nVecHyps A P n xs = Hyps (VecD A) (Vec A) P n xs\n\nConsUncurriedHyps : (A : Set)\n (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set)\n (cn : UncurriedEl (consD A) (Vec A)) \u2192 Set\nConsUncurriedHyps A P cn = UncurriedHyps (consD A) (Vec A) P cn\n\nnil : (A : Set) \u2192 Vec A zero\nnil A = init nilT refl\n\ncons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\ncons A n x xs = init consT (n , x , xs , refl)\n\nnil2 : (A : Set) \u2192 Vec A zero\nnil2 A = inj VecE (VecC A) nilT\n\ncons2 : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\ncons2 A = inj VecE (VecC A) consT\n\n----------------------------------------------------------------------\n\nmodule Induction where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u2115E \u2115C _\n (case (\u03bb t \u2192 UncurriedHyps (\u2115C t) \u2115 _ (init t))\n ( (\u03bb u q ih n \u2192 n)\n , (\u03bb u m,q ih,tt n \u2192 suc (proj\u2081 ih,tt n))\n , tt\n )\n )\n tt\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u2115E \u2115C _\n (case (\u03bb t \u2192 UncurriedHyps (\u2115C t) \u2115 _ (init t))\n ( (\u03bb u q ih n \u2192 zero)\n , (\u03bb u m,q ih,tt n \u2192 add n (proj\u2081 ih,tt n))\n , tt\n )\n )\n tt\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind VecE (VecC A) _\n (case (\u03bb t \u2192 UncurriedHyps (VecC A t) (Vec A) _ (init t))\n ( (\u03bb m q ih n ys \u2192 subst (\u03bb m \u2192 Vec A (add m n)) q ys)\n , (\u03bb m m',x,xs,q ih,tt n ys \u2192\n let m' = proj\u2081 m',x,xs,q\n x = proj\u2081 (proj\u2082 m',x,xs,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 m',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb m \u2192 Vec A (add m n)) q (cons A (add m' n) x (ih n ys))\n )\n , tt\n )\n )\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind VecE (VecC (Vec A m)) _\n (case (\u03bb t \u2192 UncurriedHyps (VecC (Vec A m) t) (Vec (Vec A m)) _ (init t))\n ( (\u03bb n q ih \u2192 subst (\u03bb n \u2192 Vec A (mult n m)) q (nil A))\n , (\u03bb n n',x,xs,q ih,tt \u2192\n let n' = proj\u2081 n',x,xs,q\n xs = proj\u2081 (proj\u2082 n',x,xs,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 n',x,xs,q))\n ih = proj\u2081 ih,tt\n in subst (\u03bb n \u2192 Vec A (mult n m)) q (append A m xs (mult n' m) ih)\n )\n , tt\n )\n )\n\n----------------------------------------------------------------------\n\nmodule GenericElim where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim \u2115E \u2115C _\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim \u2115E \u2115C _\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elim VecE (VecC A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elim VecE (VecC (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n","old_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nopen import Function\nmodule Spire.Examples.PropLev where\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nBranches : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nBranches [] P = \u22a4\nBranches (l \u2237 E) P = P here \u00d7 Branches E (\u03bb t \u2192 P (there t))\n\ncase : {E : Enum} (P : Tag E \u2192 Set) (cs : Branches E P) (t : Tag E) \u2192 P t\ncase P (c , cs) here = c\ncase P (c , cs) (there t) = case (\u03bb t \u2192 P (there t)) cs t\n\nUncurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedBranches E P X = Branches E P \u2192 X\n\nCurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedBranches [] P X = X\nCurriedBranches (l \u2237 E) P X = P here \u2192 CurriedBranches E (\u03bb t \u2192 P (there t)) X\n\ncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 UncurriedBranches E P X \u2192 CurriedBranches E P X\ncurryBranches {[]} f = f tt\ncurryBranches {l \u2237 E} f = \u03bb c \u2192 curryBranches (\u03bb cs \u2192 f (c , cs))\n\nuncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 CurriedBranches E P X \u2192 UncurriedBranches E P X\nuncurryBranches {[]} x tt = x\nuncurryBranches {l \u2237 E} f (c , cs) = uncurryBranches (f c) cs\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n End : (i : I) \u2192 Desc I\n Rec : (i : I) (D : Desc I) \u2192 Desc I\n Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n\nISet : Set \u2192 Set\u2081\nISet I = I \u2192 Set\n\nEl : {I : Set} (D : Desc I) \u2192 ISet I \u2192 ISet I\nEl (End j) X i = j \u2261 i\nEl (Rec j D) X i = X j \u00d7 El D X i\nEl (Arg A B) X i = \u03a3 A (\u03bb a \u2192 El (B a) X i)\n\nHyps : {I : Set} (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El D X i) \u2192 Set\nHyps (End j) X P i q = \u22a4\nHyps (Rec j D) X P i (x , xs) = P j x \u00d7 Hyps D X P i xs\nHyps (Arg A B) X P i (a , b) = Hyps (B a) X P i b\n\n----------------------------------------------------------------------\n\nBranchesD : (I : Set) (E : Enum) \u2192 Set\nBranchesD I E = Branches E (\u03bb _ \u2192 Desc I)\n\ncaseD : {I : Set} {E : Enum} (cs : BranchesD I E) (t : Tag E) \u2192 Desc I\ncaseD = case (\u03bb _ \u2192 Desc _)\n\n----------------------------------------------------------------------\n\nUncurriedEl : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl D X = \u2200{i} \u2192 El D X i \u2192 X i\n\nCurriedEl : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl (End i) X = X i\nCurriedEl (Rec i D) X = (x : X i) \u2192 CurriedEl D X\nCurriedEl (Arg A B) X = (a : A) \u2192 CurriedEl (B a) X\n\nCurriedEl' : {I : Set} (D : Desc I) (X : ISet I) (i : I) \u2192 Set\nCurriedEl' (End j) X i = j \u2261 i \u2192 X i\nCurriedEl' (Rec j D) X i = (x : X j) \u2192 CurriedEl' D X i\nCurriedEl' (Arg A B) X i = (a : A) \u2192 CurriedEl' (B a) X i\n\ncurryEl : {I : Set} (D : Desc I) (X : ISet I)\n \u2192 UncurriedEl D X \u2192 CurriedEl D X\ncurryEl (End i) X cn = cn refl\ncurryEl (Rec i D) X cn = \u03bb x \u2192 curryEl D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl (Arg A B) X cn = \u03bb a \u2192 curryEl (B a) X (\u03bb xs \u2192 cn (a , xs))\n\nuncurryEl : {I : Set} (D : Desc I) (X : ISet I)\n \u2192 CurriedEl D X \u2192 UncurriedEl D X\nuncurryEl (End i) X cn refl = cn\nuncurryEl (Rec i D) X cn (x , xs) = uncurryEl D X (cn x) xs\nuncurryEl (Arg A B) X cn (a , xs) = uncurryEl (B a) X (cn a) xs\n\n----------------------------------------------------------------------\n\nUncurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl D X)\n \u2192 Set\nUncurriedHyps D X P cn =\n \u2200 i (xs : El D X i) (ihs : Hyps D X P i xs) \u2192 P i (cn xs)\n\nCurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl D X)\n \u2192 Set\nCurriedHyps (End i) X P cn =\n P i (cn refl)\nCurriedHyps (Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedHyps D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedHyps (Arg A B) X P cn =\n (a : A) \u2192 CurriedHyps (B a) X P (\u03bb xs \u2192 cn (a , xs))\n\nCurriedHyps' : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (i : I)\n (cn : El D X i \u2192 X i)\n \u2192 Set\nCurriedHyps' (End j) X P i cn = (q : j \u2261 i) \u2192 P i (cn q)\nCurriedHyps' (Rec j D) X P i cn =\n (x : X j) \u2192 P j x \u2192 CurriedHyps' D X P i (\u03bb xs \u2192 cn (x , xs))\nCurriedHyps' (Arg A B) X P i cn =\n (a : A) \u2192 CurriedHyps' (B a) X P i (\u03bb xs \u2192 cn (a , xs))\n\ncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl D X)\n \u2192 UncurriedHyps D X P cn\n \u2192 CurriedHyps D X P cn\ncurryHyps (End i) X P cn pf =\n pf i refl tt\ncurryHyps (Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryHyps D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryHyps (Arg A B) X P cn pf =\n \u03bb a \u2192 curryHyps (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\n\nuncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl D X)\n \u2192 CurriedHyps D X P cn\n \u2192 UncurriedHyps D X P cn\nuncurryHyps (End .i) X P cn pf i refl tt =\n pf\nuncurryHyps (Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryHyps D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryHyps (Arg A B) X P cn pf i (a , xs) ihs =\n uncurryHyps (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\n\n----------------------------------------------------------------------\n\ndata \u03bc {I : Set} (E : Enum) (C : Tag E \u2192 Desc I) : ISet I where\n init : (t : Tag E) \u2192 UncurriedEl (C t) (\u03bc E C)\n\ninj : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I) (t : Tag E) \u2192 CurriedEl (C t) (\u03bc E C)\ninj E C t = curryEl (C t) (\u03bc E C) (init t)\n\n----------------------------------------------------------------------\n\nhyps : {I : Set} (D : Desc I) (X : I \u2192 Set)\n (P : (i : I) \u2192 X i \u2192 Set)\n (\u03b1 : (i : I) (x : X i) \u2192 P i x)\n (i : I) (xs : El D X i) \u2192 Hyps D X P i xs\nhyps (End j) X P \u03b1 i xs = tt\nhyps (Rec j D) X P \u03b1 i x,xs = \u03b1 j (proj\u2081 x,xs) , hyps D X P \u03b1 i (proj\u2082 x,xs)\nhyps (Arg A B) X P \u03b1 i a,xs = hyps (B (proj\u2081 a,xs)) X P \u03b1 i (proj\u2082 a,xs)\n\n{-# NO_TERMINATION_CHECK #-}\nind : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n (\u03b1 : (t : Tag E) \u2192 UncurriedHyps (C t) (\u03bc E C) P (init t))\n (i : I)\n (x : \u03bc E C i)\n \u2192 P i x\nind E C P \u03b1 i (init t xs) = \u03b1 t i xs $\n hyps (C t) (\u03bc E C) P (ind E C P \u03b1) i xs\n\n----------------------------------------------------------------------\n\nindCurried : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n (f : (t : Tag E) \u2192 CurriedHyps (C t) (\u03bc E C) P (init t))\n (i : I)\n (x : \u03bc E C i)\n \u2192 P i x\nindCurried E C P f i x = ind E C P (\u03bb t \u2192 uncurryHyps (C t) (\u03bc E C) P (init t) (f t)) i x\n\nSummer : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (X : ISet I) (cn : (t : Tag E) \u2192 UncurriedEl (C t) X)\n (P : (i : I) \u2192 X i \u2192 Set)\n \u2192 Tag E \u2192 Set\nSummer E C X cn P t = CurriedHyps (C t) X P (cn t)\n\nSumCurriedHyps : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n \u2192 Tag E \u2192 Set\nSumCurriedHyps E C P t = Summer E C (\u03bc E C) init P t\n\nelimUncurried : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n \u2192 Branches E (SumCurriedHyps E C P)\n \u2192 (i : I) (x : \u03bc E C i) \u2192 P i x\nelimUncurried E C P cs i x =\n indCurried E C P\n (case (SumCurriedHyps E C P) cs)\n i x\n\nelim : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n \u2192 CurriedBranches E\n (SumCurriedHyps E C P)\n ((i : I) (x : \u03bc E C i) \u2192 P i x)\nelim E C P = curryBranches (elimUncurried E C P)\n\n----------------------------------------------------------------------\n\nSoundness : Set\u2081\nSoundness = {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n (cs : Branches E (SumCurriedHyps E C P))\n (i : I) (x : \u03bc E C i)\n \u2192 \u2203 \u03bb \u03b1\n \u2192 elimUncurried E C P cs i x \u2261 ind E C P \u03b1 i x\n\nsound : Soundness\nsound E C P cs i xs =\n let D = Arg (Tag E) C in\n (\u03bb t \u2192 uncurryHyps (C t) (\u03bc E C) P (init t) (case (SumCurriedHyps E C P) cs t)) , refl\n\nCompleteness : Set\u2081\nCompleteness = {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n (\u03b1 : (t : Tag E) \u2192 UncurriedHyps (C t) (\u03bc E C) P (init t))\n (i : I) (x : \u03bc E C i)\n \u2192 \u2203 \u03bb cs\n \u2192 ind E C P \u03b1 i x \u2261 elimUncurried E C P cs i x\n\nuncurryHypsIdent : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl D X)\n (\u03b1 : UncurriedHyps D X P cn)\n (i : I) (xs : El D X i) (ihs : Hyps D X P i xs)\n \u2192 \u03b1 i xs ihs \u2261 uncurryHyps D X P cn (curryHyps D X P cn \u03b1) i xs ihs\nuncurryHypsIdent (End .i) X P cn \u03b1 i refl tt = refl\nuncurryHypsIdent (Rec j D) X P cn \u03b1 i (x , xs) (p , ps) =\n uncurryHypsIdent D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb k ys rs \u2192 \u03b1 k (x , ys) (p , rs)) i xs ps\nuncurryHypsIdent (Arg A B) X P cn \u03b1 i (a , xs) ps =\n uncurryHypsIdent (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb j ys \u2192 \u03b1 j (a , ys)) i xs ps\n\npostulate\n ext4 : {A : Set} {B : A \u2192 Set} {C : (a : A) \u2192 B a \u2192 Set}\n {D : (a : A) (b : B a) \u2192 C a b \u2192 Set}\n {Z : (a : A) (b : B a) (c : C a b) \u2192 D a b c \u2192 Set}\n (f g : (a : A) (b : B a) (c : C a b) (d : D a b c) \u2192 Z a b c d)\n \u2192 ((a : A) (b : B a) (c : C a b) (d : D a b c) \u2192 f a b c d \u2261 g a b c d)\n \u2192 f \u2261 g\n\ntoBranches : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (X : ISet I) (cn : (t : Tag E) \u2192 UncurriedEl (C t) X)\n (P : (i : I) \u2192 X i \u2192 Set)\n (\u03b1 : (t : Tag E) \u2192 UncurriedHyps (C t) X P (cn t))\n \u2192 Branches E (Summer E C X cn P)\ntoBranches [] C X cn P \u03b1 = tt\ntoBranches (l \u2237 E) C X cn P \u03b1 =\n curryHyps (C here) X P (\u03bb xs \u2192 cn here xs) (\u03bb i xs \u2192 \u03b1 here i xs)\n , toBranches E (\u03bb t \u2192 C (there t)) X\n (\u03bb t \u2192 cn (there t))\n P (\u03bb t i xs ih \u2192 \u03b1 (there t) i xs ih)\n\nToBranches : {I : Set} {E : Enum} (C : Tag E \u2192 Desc I)\n (X : ISet I) (cn : (t : Tag E) \u2192 UncurriedEl (C t) X)\n (P : (i : I) \u2192 X i \u2192 Set)\n (\u03b1 : (t : Tag E) \u2192 UncurriedHyps (C t) X P (cn t))\n (t : Tag E)\n \u2192 let \u03b2 = toBranches E C X cn P \u03b1 in\n case (Summer E C X cn P) \u03b2 t \u2261 curryHyps (C t) X P (cn t) (\u03b1 t)\nToBranches C X cn P \u03b1 here = refl\nToBranches C X cn P \u03b1 (there t)\n with ToBranches (\u03bb t \u2192 C (there t)) X\n (\u03bb t xs \u2192 cn (there t) xs)\n P (\u03bb t i xs ih \u2192 \u03b1 (there t) i xs ih) t\n... | ih rewrite ih = refl\n\ncomplete\u03b1 : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n (\u03b1 : (t : Tag E) \u2192 UncurriedHyps (C t) (\u03bc E C) P (init t))\n (t : Tag E) (i : I) (xs : El (C t) (\u03bc E C) i) (ihs : Hyps (C t) (\u03bc E C) P i xs)\n \u2192 let \u03b2 = toBranches E C (\u03bc E C) init P \u03b1 in\n \u03b1 t i xs ihs \u2261 uncurryHyps (C t) (\u03bc E C) P (init t) (case (SumCurriedHyps E C P) \u03b2 t) i xs ihs\ncomplete\u03b1 E C P \u03b1 t i xs ihs\n with ToBranches C (\u03bc E C) init P \u03b1 t\n... | q rewrite q = uncurryHypsIdent (C t) (\u03bc E C) P (init t) (\u03b1 t) i xs ihs\n\ncomplete' : {I : Set} (E : Enum) (C : Tag E \u2192 Desc I)\n (P : (i : I) \u2192 \u03bc E C i \u2192 Set)\n (\u03b1 : (t : Tag E) \u2192 UncurriedHyps (C t) (\u03bc E C) P (init t))\n (i : I) (x : \u03bc E C i)\n \u2192 let \u03b2 = toBranches E C (\u03bc E C) init P \u03b1 in\n ind E C P \u03b1 i x \u2261 elimUncurried E C P \u03b2 i x\ncomplete' E C P \u03b1 i (init t xs) = cong\n (\u03bb f \u2192 ind E C P f i (init t xs))\n (ext4 \u03b1\n (\u03bb t \u2192 uncurryHyps (C t) (\u03bc E C) P (init t) (case (SumCurriedHyps E C P) \u03b2 t))\n (complete\u03b1 E C P \u03b1)\n )\n where \u03b2 = toBranches E C (\u03bc E C) init P \u03b1\n\ncomplete : Completeness\ncomplete E C P \u03b1 i x =\n let D = Arg (Tag E) C in\n toBranches E C (\u03bc E C) init P \u03b1\n , complete' E C P \u03b1 i x\n\n----------------------------------------------------------------------\n\n\u2115E : Enum\n\u2115E = \"zero\" \u2237 \"suc\" \u2237 []\n\nVecE : Enum\nVecE = \"nil\" \u2237 \"cons\" \u2237 []\n\n\u2115T : Set\n\u2115T = Tag \u2115E\n\nVecT : Set\nVecT = Tag VecE\n\nzeroT : \u2115T\nzeroT = here\n\nsucT : \u2115T\nsucT = there here\n\nnilT : VecT\nnilT = here\n\nconsT : VecT\nconsT = there here\n\n\u2115C : \u2115T \u2192 Desc \u22a4\n\u2115C = caseD $\n End tt\n , Rec tt (End tt)\n , tt\n\n\u2115D : Desc \u22a4\n\u2115D = Arg \u2115T \u2115C\n\n\u2115 : \u22a4 \u2192 Set\n\u2115 = \u03bc \u2115E \u2115C\n\nzero : \u2115 tt\nzero = init zeroT refl\n\nsuc : \u2115 tt \u2192 \u2115 tt\nsuc n = init sucT (n , refl)\n\nVecC : (A : Set) \u2192 VecT \u2192 Desc (\u2115 tt)\nVecC A = caseD $\n End zero\n , Arg (\u2115 tt) (\u03bb n \u2192 Arg A \u03bb _ \u2192 Rec n (End (suc n)))\n , tt\n\nnilD : (A : Set) \u2192 Desc (\u2115 tt)\nnilD A = End zero\n\nconsD : (A : Set) \u2192 Desc (\u2115 tt)\nconsD A = Arg (\u2115 tt) (\u03bb n \u2192 Arg A (\u03bb _ \u2192 Rec n (End (suc n))))\n\nVecD : (A : Set) \u2192 Desc (\u2115 tt)\nVecD A = Arg VecT (VecC A)\n\nVec : (A : Set) \u2192 \u2115 tt \u2192 Set\nVec A = \u03bc VecE (VecC A)\n\nNilEl : (A : Set) (n : \u2115 tt) \u2192 Set\nNilEl A n = El (nilD A) (Vec A) n\n\nConsEl : (A : Set) \u2192 \u2115 tt \u2192 Set\nConsEl A n = El (consD A) (Vec A) n\n\nVecEl : (A : Set) \u2192 \u2115 tt \u2192 Set\nVecEl A n = El (VecD A) (Vec A) n\n\nNilHyps : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set) (n : \u2115 tt) (xs : NilEl A n) \u2192 Set\nNilHyps A P n xs = Hyps (nilD A) (Vec A) P n xs\n\nConsHyps : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set) (n : \u2115 tt) (xs : ConsEl A n) \u2192 Set\nConsHyps A P n xs = Hyps (consD A) (Vec A) P n xs\n\nVecHyps : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set) (n : \u2115 tt) (xs : VecEl A n) \u2192 Set\nVecHyps A P n xs = Hyps (VecD A) (Vec A) P n xs\n\nConsUncurriedHyps : (A : Set)\n (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set)\n (cn : UncurriedEl (consD A) (Vec A)) \u2192 Set\nConsUncurriedHyps A P cn = UncurriedHyps (consD A) (Vec A) P cn\n\nnil : (A : Set) \u2192 Vec A zero\nnil A = init nilT refl\n\ncons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\ncons A n x xs = init consT (n , x , xs , refl)\n\nnil2 : (A : Set) \u2192 Vec A zero\nnil2 A = inj VecE (VecC A) nilT\n\ncons2 : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\ncons2 A = inj VecE (VecC A) consT\n\n----------------------------------------------------------------------\n\nmodule Induction where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u2115E \u2115C _\n (case (\u03bb t \u2192 UncurriedHyps (\u2115C t) \u2115 _ (init t))\n ( (\u03bb u q ih n \u2192 n)\n , (\u03bb u m,q ih,tt n \u2192 suc (proj\u2081 ih,tt n))\n , tt\n )\n )\n tt\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u2115E \u2115C _\n (case (\u03bb t \u2192 UncurriedHyps (\u2115C t) \u2115 _ (init t))\n ( (\u03bb u q ih n \u2192 zero)\n , (\u03bb u m,q ih,tt n \u2192 add n (proj\u2081 ih,tt n))\n , tt\n )\n )\n tt\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind VecE (VecC A) _\n (case (\u03bb t \u2192 UncurriedHyps (VecC A t) (Vec A) _ (init t))\n ( (\u03bb m q ih n ys \u2192 subst (\u03bb m \u2192 Vec A (add m n)) q ys)\n , (\u03bb m m',x,xs,q ih,tt n ys \u2192\n let m' = proj\u2081 m',x,xs,q\n x = proj\u2081 (proj\u2082 m',x,xs,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 m',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb m \u2192 Vec A (add m n)) q (cons A (add m' n) x (ih n ys))\n )\n , tt\n )\n )\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind VecE (VecC (Vec A m)) _\n (case (\u03bb t \u2192 UncurriedHyps (VecC (Vec A m) t) (Vec (Vec A m)) _ (init t))\n ( (\u03bb n q ih \u2192 subst (\u03bb n \u2192 Vec A (mult n m)) q (nil A))\n , (\u03bb n n',x,xs,q ih,tt \u2192\n let n' = proj\u2081 n',x,xs,q\n xs = proj\u2081 (proj\u2082 n',x,xs,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 n',x,xs,q))\n ih = proj\u2081 ih,tt\n in subst (\u03bb n \u2192 Vec A (mult n m)) q (append A m xs (mult n' m) ih)\n )\n , tt\n )\n )\n\n----------------------------------------------------------------------\n\nmodule GenericElim where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim \u2115E \u2115C _\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim \u2115E \u2115C _\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elim VecE (VecC A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elim VecE (VecC (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"d3b24ab0dab8b2f1fb47722d3ac214bc1561629b","subject":"Minor example change.","message":"Minor example change.\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/DarkwingDuck\/Examples.agda","new_file":"formalization\/agda\/Spire\/DarkwingDuck\/Examples.agda","new_contents":"{-# OPTIONS --type-in-type #-}\nopen import Spire.DarkwingDuck.Primitive\nopen import Spire.DarkwingDuck.Derived\nmodule Spire.DarkwingDuck.Examples where\n\n----------------------------------------------------------------------\n\n\u2115E : Enum\n\u2115E = \"zero\" \u2237 \"suc\" \u2237 []\n\nVecE : Enum\nVecE = \"nil\" \u2237 \"cons\" \u2237 []\n\n\u2115T : Set\n\u2115T = Tag \u2115E\n\nVecT : Set\nVecT = Tag VecE\n\nzeroT : \u2115T\nzeroT = here\n\nsucT : \u2115T\nsucT = there here\n\nnilT : VecT\nnilT = here\n\nconsT : VecT\nconsT = there here\n\n----------------------------------------------------------------------\n\n\u2115R : Data\n\u2115R = record\n { P = End\n ; I = \u03bb _ \u2192 End\n ; E = \u2115E\n ; B = \u03bb _ \u2192\n End tt\n , Rec tt (End tt)\n , tt\n }\n\n\u2115 : Set\n\u2115 = Form \u2115R\n\nzero : \u2115\nzero = inj \u2115R zeroT\n\nsuc : \u2115 \u2192 \u2115\nsuc = inj \u2115R sucT\n\nVecR : Data\nVecR = record\n { P = Arg Set (\u03bb _ \u2192 End)\n ; I = \u03bb _ \u2192 Arg \u2115 (\u03bb _ \u2192 End)\n ; E = VecE\n ; B = \u03bb A \u2192\n End (zero , tt)\n , IArg \u2115 (\u03bb n \u2192 Arg (proj\u2081 A) \u03bb _ \u2192 Rec (n , tt) (End (suc n , tt)))\n , tt\n }\n\nVec : (A : Set) \u2192 \u2115 \u2192 Set\nVec = Form VecR\n\nnil : {A : Set} \u2192 Vec A zero\nnil = inj VecR nilT\n\ncons : {A : Set} {n : \u2115} (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\ncons = inj VecR consT\n\n----------------------------------------------------------------------\n\nadd : \u2115 \u2192 \u2115 \u2192 \u2115\nadd = elim \u2115R\n (\u03bb n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n\nmult : \u2115 \u2192 \u2115 \u2192 \u2115\nmult = elim \u2115R\n (\u03bb n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n\nappend : {A : Set} {m n : \u2115} (xs : Vec A m) (ys : Vec A n) \u2192 Vec A (add m n)\nappend {A} {m} {n} = elim VecR\n (\u03bb m xs \u2192 (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb ys \u2192 ys)\n (\u03bb x xs ih ys \u2192 cons x (ih ys))\n m\n\nconcat : {A : Set} {m n : \u2115} (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\nconcat {A} {m} {n} = elim VecR\n (\u03bb n xss \u2192 Vec A (mult n m))\n nil\n (\u03bb xs xss ih \u2192 append xs ih)\n n\n\n----------------------------------------------------------------------\n\none : \u2115\none = suc zero\n\ntwo : \u2115\ntwo = suc one\n\nthree : \u2115\nthree = suc two\n\n[1] : Vec \u2115 one\n[1] = cons one nil\n\n[2,3] : Vec \u2115 two\n[2,3] = cons two (cons three nil)\n\n[1,2,3] : Vec \u2115 three\n[1,2,3] = cons one (cons two (cons three nil))\n\ntest-append : [1,2,3] \u2261 append [1] [2,3]\ntest-append = refl\n\n----------------------------------------------------------------------\n","old_contents":"{-# OPTIONS --type-in-type #-}\nopen import Spire.DarkwingDuck.Primitive\nopen import Spire.DarkwingDuck.Derived\nmodule Spire.DarkwingDuck.Examples where\n\n----------------------------------------------------------------------\n\n\u2115E : Enum\n\u2115E = \"zero\" \u2237 \"suc\" \u2237 []\n\nVecE : Enum\nVecE = \"nil\" \u2237 \"cons\" \u2237 []\n\n\u2115T : Set\n\u2115T = Tag \u2115E\n\nVecT : Set\nVecT = Tag VecE\n\nzeroT : \u2115T\nzeroT = here\n\nsucT : \u2115T\nsucT = there here\n\nnilT : VecT\nnilT = here\n\nconsT : VecT\nconsT = there here\n\n----------------------------------------------------------------------\n\n\u2115R : Data\n\u2115R = record\n { P = End\n ; I = \u03bb _ \u2192 End\n ; E = \u2115E\n ; B = \u03bb _ \u2192\n End tt\n , Rec tt (End tt)\n , tt\n }\n\n\u2115 : Set\n\u2115 = Form \u2115R\n\nzero : \u2115\nzero = inj \u2115R zeroT\n\nsuc : \u2115 \u2192 \u2115\nsuc = inj \u2115R sucT\n\nVecR : Data\nVecR = record\n { P = Arg Set (\u03bb _ \u2192 End)\n ; I = \u03bb _ \u2192 Arg \u2115 (\u03bb _ \u2192 End)\n ; E = VecE\n ; B = \u03bb A \u2192\n End (zero , tt)\n , IArg \u2115 (\u03bb n \u2192 Arg (proj\u2081 A) \u03bb _ \u2192 Rec (n , tt) (End (suc n , tt)))\n , tt\n }\n\nVec : (A : Set) \u2192 \u2115 \u2192 Set\nVec = Form VecR\n\nnil : {A : Set} \u2192 Vec A zero\nnil = inj VecR nilT\n\ncons : {A : Set} {n : \u2115} (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\ncons = inj VecR consT\n\n----------------------------------------------------------------------\n\nadd : \u2115 \u2192 \u2115 \u2192 \u2115\nadd = elim \u2115R\n (\u03bb n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n\nmult : \u2115 \u2192 \u2115 \u2192 \u2115\nmult = elim \u2115R\n (\u03bb n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n\nappend : {A : Set} {m : \u2115} (xs : Vec A m) {n : \u2115} (ys : Vec A n) \u2192 Vec A (add m n)\nappend {A} {m} = elim VecR\n (\u03bb m xs \u2192 {n : \u2115} (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb ys \u2192 ys)\n (\u03bb x xs ih ys \u2192 cons x (ih ys))\n m\n\nconcat : {A : Set} {m n : \u2115} (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\nconcat {A} {m} {n} = elim VecR\n (\u03bb n xss \u2192 Vec A (mult n m))\n nil\n (\u03bb xs xss ih \u2192 append xs ih)\n n\n\n----------------------------------------------------------------------\n\none : \u2115\none = suc zero\n\ntwo : \u2115\ntwo = suc one\n\nthree : \u2115\nthree = suc two\n\n[1] : Vec \u2115 one\n[1] = cons one nil\n\n[2,3] : Vec \u2115 two\n[2,3] = cons two (cons three nil)\n\n[1,2,3] : Vec \u2115 three\n[1,2,3] = cons one (cons two (cons three nil))\n\ntest-append : [1,2,3] \u2261 append [1] [2,3]\ntest-append = refl\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"1da8d765d3b45cbc385435bbd6c113c680b6f896","subject":"Generalize helper methods.","message":"Generalize helper methods.\n\nBefore this commit, the helper methods worked for one particular context\nthat was inferred from the helper's use sites. I want to change the\nhelper's use suite in the next commit so that the context would no\nlonger get inferred automatically, so I have to generalize the helper\nmethods for arbitrary contexts first.\n\nOld-commit-hash: 331ecddb578d8491bb6b65d1312b789d9260ff31\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Term.agda","new_file":"Parametric\/Change\/Term.agda","new_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Constant\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\nDiffStructure : Set\nDiffStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))\n\nApplyStructure : Set\nApplyStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\n\nmodule Structure\n (diff-base : DiffStructure)\n (apply-base : ApplyStructure)\n where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n lift-diff-apply :\n \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4) \u00d7 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-diff-apply {base \u03b9} = diff-base , apply-base\n lift-diff-apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff\u03c3 {\u0393}) s t\n _\u229d\u03c4_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff\u03c4 {\u0393}) s t\n _\u2295\u03c3_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply\u03c3 {\u0393}) \u0394t t\n _\u2295\u03c4_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply\u03c4 {\u0393}) \u0394t t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\n lift-diff :\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n\n lift-diff = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply {\u03c4} {\u0393})\n\n lift-apply :\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply {\u03c4} {\u0393})\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Constant\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\nDiffStructure : Set\nDiffStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))\n\nApplyStructure : Set\nApplyStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\n\nmodule Structure\n (diff-base : DiffStructure)\n (apply-base : ApplyStructure)\n where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n lift-diff-apply :\n \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4) \u00d7 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-diff-apply {base \u03b9} = diff-base , apply-base\n lift-diff-apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app\u2082 diff\u03c3 s t\n _\u229d\u03c4_ = \u03bb s t \u2192 app\u2082 diff\u03c4 s t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app\u2082 apply\u03c3 \u0394t t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app\u2082 apply\u03c4 \u0394t t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\n lift-diff :\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n\n lift-diff = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply {\u03c4} {\u0393})\n\n lift-apply :\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply {\u03c4} {\u0393})\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"91c228f51658b4d57015ba27b291178a7cbfb4ee","subject":"Desc model: proof-phi-psi implicit.","message":"Desc model: proof-phi-psi implicit.","repos":"brixen\/Epigram,kwangkim\/pigment,kwangkim\/pigment,kwangkim\/pigment","old_file":"models\/IDesc_type_type.agda","new_file":"models\/IDesc_type_type.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {I : Set} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi (pi S T) = cong (pi S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n\n-- From embedding to embedding\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = psi (phi D) == D\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\u03bb _ \u2192 descD I)\n (P I)\n (proof-psi-phi-casesW I) \n Void\n D\n where proof-psi-phi-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-psi-phi-cases I (lvar , i) hs = refl\n proof-psi-phi-cases I (lconst , x) hs = refl\n proof-psi-phi-cases I (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases I (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\u03bb s \u2192 psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases I (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s \u2192 psi (phi (T s)))\n T\n hs)\n proof-psi-phi-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-psi-phi-casesW I Void = proof-psi-phi-cases I\n \n\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : (I : Set) -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi I (var i) = refl\nproof-phi-psi I (const x) = refl\nproof-phi-psi I (prod D D') with proof-phi-psi I D | proof-phi-psi I D'\n... | p | q = cong2 prod p q\nproof-phi-psi I (pi S T) = cong (pi S) \n (reflFun (\u03bb s \u2192 phi (psi (T s)))\n T\n (\\s -> proof-phi-psi I (T s)))\nproof-phi-psi I (sigma S T) = cong (sigma S) \n (reflFun (\u03bb s \u2192 phi (psi (T s)))\n T\n (\\s -> proof-phi-psi I (T s)))\n\n\n-- From embedding to embedding\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = psi (phi D) == D\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\u03bb _ \u2192 descD I)\n (P I)\n (proof-psi-phi-casesW I) \n Void\n D\n where proof-psi-phi-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-psi-phi-cases I (lvar , i) hs = refl\n proof-psi-phi-cases I (lconst , x) hs = refl\n proof-psi-phi-cases I (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases I (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\u03bb s \u2192 psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases I (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s \u2192 psi (phi (T s)))\n T\n hs)\n proof-psi-phi-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-psi-phi-casesW I Void = proof-psi-phi-cases I\n \n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"65a027f17f89f3f1babdbfeb27d53029aeef353a","subject":"Fixed notes.","message":"Fixed notes.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/thesis\/CombinedProofs\/AddRecursiveEquation.agda","new_file":"notes\/thesis\/CombinedProofs\/AddRecursiveEquation.agda","new_contents":"------------------------------------------------------------------------------\n-- Definition of addition using a recursive equation\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule CombinedProofs.AddRecursiveEquation where\n\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n\npostulate\n _+_ : D \u2192 D \u2192 D\n +-eq : \u2200 m n \u2192 m + n \u2261 if (iszero\u2081 m) then n else succ\u2081 (pred\u2081 m + n)\n{-# ATP axiom +-eq #-}\n\npostulate\n +-0x : \u2200 n \u2192 zero + n \u2261 n\n +-Sx : \u2200 m n \u2192 succ\u2081 m + n \u2261 succ\u2081 (m + n)\n{-# ATP prove +-0x #-}\n{-# ATP prove +-Sx #-}\n\n-- $ agda2atp -i. -i ~\/fotc\/fot\/ FOT\/FOTC\/Data\/Nat\/AddRecursiveEquation.agda\n-- Proving the conjecture in \/tmp\/FOT\/FOTC\/Data\/Nat\/AddRecursiveEquation\/23-43-Sx.tptp ...\n-- Vampire 0.6 (revision 903) proved the conjecture in \/tmp\/FOT\/FOTC\/Data\/Nat\/AddRecursiveEquation\/23-43-Sx.tptp\n-- Proving the conjecture in \/tmp\/FOT\/FOTC\/Data\/Nat\/AddRecursiveEquation\/22-43-0x.tptp ...\n-- Vampire 0.6 (revision 903) proved the conjecture in \/tmp\/FOT\/FOTC\/Data\/Nat\/AddRecursiveEquation\/22-43-0x.tptp\n","old_contents":"------------------------------------------------------------------------------\n-- Definition of addition using a recursive equation\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.AddRecursiveEquation where\n\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n\npostulate\n _+_ : D \u2192 D \u2192 D\n +-eq : \u2200 m n \u2192 m + n \u2261 if (iszero\u2081 m) then n else succ\u2081 (pred\u2081 m + n)\n{-# ATP axiom +-eq #-}\n\npostulate\n +-0x : \u2200 n \u2192 zero + n \u2261 n\n +-Sx : \u2200 m n \u2192 succ\u2081 m + n \u2261 succ\u2081 (m + n)\n{-# ATP prove +-0x #-}\n{-# ATP prove +-Sx #-}\n\n-- $ agda2atp -i. -i ~\/fotc\/fot\/ FOT\/FOTC\/Data\/Nat\/AddRecursiveEquation.agda\n-- Proving the conjecture in \/tmp\/FOT\/FOTC\/Data\/Nat\/AddRecursiveEquation\/23-43-Sx.tptp ...\n-- Vampire 0.6 (revision 903) proved the conjecture in \/tmp\/FOT\/FOTC\/Data\/Nat\/AddRecursiveEquation\/23-43-Sx.tptp\n-- Proving the conjecture in \/tmp\/FOT\/FOTC\/Data\/Nat\/AddRecursiveEquation\/22-43-0x.tptp ...\n-- Vampire 0.6 (revision 903) proved the conjecture in \/tmp\/FOT\/FOTC\/Data\/Nat\/AddRecursiveEquation\/22-43-0x.tptp\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"fa53dc9c768d14c58160f019f07c795b973f0043","subject":"bintree: sort is sorting!","message":"bintree: sort is sorting!\n","repos":"crypto-agda\/crypto-agda","old_file":"prefect-bintree.agda","new_file":"prefect-bintree.agda","new_contents":"module prefect-bintree where\n\nimport Level as L\nopen import Function.NP\nimport Data.Nat.NP as Nat\nopen Nat using (\u2115; zero; suc; 2^_; _+_; module \u2115\u00b0)\nopen import Data.Bool\nopen import Data.Sum\nopen import Data.Bits\nopen import Data.Unit using (\u22a4)\nopen import Data.Product using (_\u00d7_; _,_; proj\u2081; proj\u2082; \u2203)\nopen import Data.Vec.NP using (Vec; _++_; module Alternative-Reverse)\nopen import Relation.Nullary\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_)\nopen import Algebra.FunctionProperties\n\ndata Tree {a} (A : Set a) : \u2115 \u2192 Set a where\n leaf : (x : A) \u2192 Tree A zero\n fork : \u2200 {n} (left right : Tree A n) \u2192 Tree A (suc n)\n\nfromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Tree A n\nfromFun {zero} f = leaf (f [])\nfromFun {suc n} f = fork (fromFun (f \u2218 0\u2237_)) (fromFun (f \u2218 1\u2237_))\n\ntoFun : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Bits n \u2192 A\ntoFun (leaf x) _ = x\ntoFun (fork left right) (b \u2237 bs) = toFun (if b then right else left) bs\n\ntoFun\u2218fromFun : \u2200 {n a} {A : Set a} (f : Bits n \u2192 A) \u2192 toFun (fromFun f) \u2257 f\ntoFun\u2218fromFun {zero} f [] = \u2261.refl\ntoFun\u2218fromFun {suc n} f (false \u2237 xs)\n rewrite toFun\u2218fromFun (f \u2218 0\u2237_) xs = \u2261.refl\ntoFun\u2218fromFun {suc n} f (true \u2237 xs)\n rewrite toFun\u2218fromFun (f \u2218 1\u2237_) xs = \u2261.refl\n\nfromFun\u2218toFun : \u2200 {n a} {A : Set a} (t : Tree A n) \u2192 fromFun (toFun t) \u2261 t\nfromFun\u2218toFun (leaf x) = \u2261.refl\nfromFun\u2218toFun (fork t\u2080 t\u2081)\n rewrite fromFun\u2218toFun t\u2080\n | fromFun\u2218toFun t\u2081 = \u2261.refl\n\ntoFun\u2192fromFun : \u2200 {n a} {A : Set a} (t : Tree A n) (f : Bits n \u2192 A) \u2192 toFun t \u2257 f \u2192 t \u2261 fromFun f\ntoFun\u2192fromFun (leaf x) f t\u2257f = \u2261.cong leaf (t\u2257f [])\ntoFun\u2192fromFun (fork t\u2080 t\u2081) f t\u2257f\n rewrite toFun\u2192fromFun t\u2080 (f \u2218 0\u2237_) (t\u2257f \u2218 0\u2237_)\n | toFun\u2192fromFun t\u2081 (f \u2218 1\u2237_) (t\u2257f \u2218 1\u2237_) = \u2261.refl\n\nfromFun\u2192toFun : \u2200 {n a} {A : Set a} (t : Tree A n) (f : Bits n \u2192 A) \u2192 t \u2261 fromFun f \u2192 toFun t \u2257 f\nfromFun\u2192toFun ._ _ \u2261.refl = toFun\u2218fromFun _\n\nlookup : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Tree A n \u2192 A\nlookup = flip toFun\n\n{-\nmodule Fold {a b i} {I : Set i} (ze : I) (su : I \u2192 I)\n {A : Set a} {B : I \u2192 Set b}\n (f : A \u2192 B ze) (_\u00b7_ : \u2200 {n} \u2192 B n \u2192 B n \u2192 B (su n)) where\n\n `_ : \u2115 \u2192 I\n `_ = Nat.fold ze su\n\n fold : \u2200 {n} \u2192 Tree A n \u2192 B(` n)\n fold (leaf x) = f x\n fold (fork t\u2080 t\u2081) = fold t\u2080 \u00b7 fold t\u2081\n\nfold : \u2200 {n a} {A : Set a} (op : A \u2192 A \u2192 A) \u2192 Tree A n \u2192 A\nfold {A = A} op = Fold.fold 0 suc {B = const A} id op\n\nsearch\u2261fold\u2218fromFun : \u2200 {n a} {A : Set a} op (f : Bits n \u2192 A) \u2192 search op f \u2261 fold op (fromFun f)\nsearch\u2261fold\u2218fromFun {zero} op f = \u2261.refl\nsearch\u2261fold\u2218fromFun {suc n} op f\n rewrite search\u2261fold\u2218fromFun op (f \u2218 0\u2237_)\n | search\u2261fold\u2218fromFun op (f \u2218 1\u2237_) = \u2261.refl\n\n-- Returns the flat vector of leaves underlying the perfect binary tree.\ntoVec : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Vec A (2^ n)\ntoVec (leaf x) = x \u2237 []\ntoVec (fork t\u2080 t\u2081) = toVec t\u2080 ++ toVec t\u2081\n\nlookup' : \u2200 {m n a} {A : Set a} \u2192 Bits m \u2192 Tree A (m + n) \u2192 Tree A n\nlookup' [] t = t\nlookup' (b \u2237 bs) (fork t t\u2081) = lookup' bs (if b then t\u2081 else t)\n\n\nupdate' : \u2200 {m n a} {A : Set a} \u2192 Bits m \u2192 Tree A n \u2192 Tree A (m + n) \u2192 Tree A (m + n)\nupdate' [] val tree = val\nupdate' (b \u2237 key) val (fork tree tree\u2081) = if b then fork tree (update' key val tree\u2081)\n else fork (update' key val tree) tree\u2081\n\nmap : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 Tree A n \u2192 Tree B n\nmap f (leaf x) = leaf (f x)\nmap f (fork t\u2080 t\u2081) = fork (map f t\u2080) (map f t\u2081)\n\nopen import Relation.Binary\nopen import Data.Star using (Star; \u03b5; _\u25c5_)\n\ndata Swp {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n left : \u2200 {n} {left\u2080 left\u2081 right : Tree A n} \u2192\n Swp left\u2080 left\u2081 \u2192\n Swp (fork left\u2080 right) (fork left\u2081 right)\n right : \u2200 {n} {left right\u2080 right\u2081 : Tree A n} \u2192\n Swp right\u2080 right\u2081 \u2192\n Swp (fork left right\u2080) (fork left right\u2081)\n swp\u2081 : \u2200 {n} {left right : Tree A n} \u2192\n Swp (fork left right) (fork right left)\n swp\u2082 : \u2200 {n} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Swp (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2081\u2081 t\u2080\u2081) (fork t\u2081\u2080 t\u2080\u2080))\n\nSwp\u2605 : \u2200 {n a} {A : Set a} (left right : Tree A n) \u2192 Set a\nSwp\u2605 = Star Swp\n\nSwp-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Swp {A = A} {n})\nSwp-sym (left s) = left (Swp-sym s)\nSwp-sym (right s) = right (Swp-sym s)\nSwp-sym swp\u2081 = swp\u2081\nSwp-sym swp\u2082 = swp\u2082\n\ndata Rot {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n leaf : \u2200 x \u2192 Rot (leaf x) (leaf x)\n fork : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 left\u2081 \u2192\n Rot right\u2080 right\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n krof : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 right\u2081 \u2192\n Rot right\u2080 left\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n\nRot-refl : \u2200 {n a} {A : Set a} \u2192 Reflexive (Rot {A = A} {n})\nRot-refl {x = leaf x} = leaf x\nRot-refl {x = fork _ _} = fork Rot-refl Rot-refl\n\nRot-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Rot {A = A} {n})\nRot-sym (leaf x) = leaf x\nRot-sym (fork p\u2080 p\u2081) = fork (Rot-sym p\u2080) (Rot-sym p\u2081)\nRot-sym (krof p\u2080 p\u2081) = krof (Rot-sym p\u2081) (Rot-sym p\u2080)\n\nRot-trans : \u2200 {n a} {A : Set a} \u2192 Transitive (Rot {A = A} {n})\nRot-trans (leaf x) q = q\nRot-trans (fork p\u2080 p\u2081) (fork q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (fork p\u2080 p\u2081) (krof q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (krof p\u2080 p\u2081) (fork q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\nRot-trans (krof p\u2080 p\u2081) (krof q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\n\ndata SwpOp : \u2115 \u2192 Set where\n \u03b5 : \u2200 {n} \u2192 SwpOp n\n\n _\u204f_ : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp n \u2192 SwpOp n\n\n first : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp (suc n)\n\n swp : \u2200 {n} \u2192 SwpOp (suc n)\n\n swp-seconds : \u2200 {n} \u2192 SwpOp (2 + n)\n\ndata Perm {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n \u03b5 : \u2200 {n} {t : Tree A n} \u2192 Perm t t\n\n _\u204f_ : \u2200 {n} {t u v : Tree A n} \u2192 Perm t u \u2192 Perm u v \u2192 Perm t v\n\n first : \u2200 {n} {tA tB tC : Tree A n} \u2192\n Perm tA tB \u2192\n Perm (fork tA tC) (fork tB tC)\n\n swp : \u2200 {n} {tA tB : Tree A n} \u2192\n Perm (fork tA tB) (fork tB tA)\n\n swp-seconds : \u2200 {n} {tA tB tC tD : Tree A n} \u2192\n Perm (fork (fork tA tB) (fork tC tD))\n (fork (fork tA tD) (fork tC tB))\n\ndata Perm0\u2194 {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n \u03b5 : \u2200 {n} {t : Tree A n} \u2192 Perm0\u2194 t t\n\n swp : \u2200 {n} {tA tB : Tree A n} \u2192\n Perm0\u2194 (fork tA tB) (fork tB tA)\n\n first : \u2200 {n} {tA tB tC : Tree A n} \u2192\n Perm0\u2194 tA tB \u2192\n Perm0\u2194 (fork tA tC) (fork tB tC)\n\n firsts : \u2200 {n} {tA tB tC tD tE tF : Tree A n} \u2192\n Perm0\u2194 (fork tA tC) (fork tE tF) \u2192\n Perm0\u2194 (fork (fork tA tB) (fork tC tD))\n (fork (fork tE tB) (fork tF tD))\n\n extremes : \u2200 {n} {tA tB tC tD tE tF : Tree A n} \u2192\n Perm0\u2194 (fork tA tD) (fork tE tF) \u2192\n Perm0\u2194 (fork (fork tA tB) (fork tC tD))\n (fork (fork tE tB) (fork tC tF))\n\n-- Star Perm0\u2194 can then model any permutation\n\ninfixr 1 _\u204f_\n\nsecond-perm : \u2200 {a} {A : Set a} {n} {left right\u2080 right\u2081 : Tree A n} \u2192\n Perm right\u2080 right\u2081 \u2192\n Perm (fork left right\u2080) (fork left right\u2081)\nsecond-perm f = swp \u204f first f \u204f swp\n\nsecond-swpop : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp (suc n)\nsecond-swpop f = swp \u204f first f \u204f swp\n\n<_\u00d7_>-perm : \u2200 {a} {A : Set a} {n} {left\u2080 right\u2080 left\u2081 right\u2081 : Tree A n} \u2192\n Perm left\u2080 left\u2081 \u2192\n Perm right\u2080 right\u2081 \u2192\n Perm (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n< f \u00d7 g >-perm = first f \u204f second-perm g\n\nswp\u2082-perm : \u2200 {a n} {A : Set a} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Perm (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2081\u2081 t\u2080\u2081) (fork t\u2081\u2080 t\u2080\u2080))\nswp\u2082-perm = first swp \u204f swp-seconds \u204f first swp\n\nswp\u2083-perm : \u2200 {a n} {A : Set a} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Perm (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2080\u2080 t\u2081\u2080) (fork t\u2080\u2081 t\u2081\u2081))\nswp\u2083-perm = second-perm swp \u204f swp-seconds \u204f second-perm swp\n\nswp-firsts-perm : \u2200 {n a} {A : Set a} {tA tB tC tD : Tree A n} \u2192\n Perm (fork (fork tA tB) (fork tC tD))\n (fork (fork tC tB) (fork tA tD))\nswp-firsts-perm = < swp \u00d7 swp >-perm \u204f swp-seconds \u204f < swp \u00d7 swp >-perm\n\nSwp\u21d2Perm : \u2200 {n a} {A : Set a} \u2192 Swp {a} {A} {n} \u21d2 Perm {n = n}\nSwp\u21d2Perm (left pf) = first (Swp\u21d2Perm pf)\nSwp\u21d2Perm (right pf) = second-perm (Swp\u21d2Perm pf)\nSwp\u21d2Perm swp\u2081 = swp\nSwp\u21d2Perm swp\u2082 = swp\u2082-perm\n\nSwp\u2605\u21d2Perm : \u2200 {n a} {A : Set a} \u2192 Swp\u2605 {n} {a} {A} \u21d2 Perm {n = n}\nSwp\u2605\u21d2Perm \u03b5 = \u03b5\nSwp\u2605\u21d2Perm (x \u25c5 xs) = Swp\u21d2Perm x \u204f Swp\u2605\u21d2Perm xs\n\nswp-inners : \u2200 {n} \u2192 SwpOp (2 + n)\nswp-inners = second-swpop swp \u204f swp-seconds \u204f second-swpop swp\n\non-extremes : \u2200 {n} \u2192 SwpOp (1 + n) \u2192 SwpOp (2 + n)\non-extremes f = swp-seconds \u204f first f \u204f swp-seconds\n\non-firsts : \u2200 {n} \u2192 SwpOp (1 + n) \u2192 SwpOp (2 + n)\non-firsts f = swp-inners \u204f first f \u204f swp-inners\n\n0\u2194_ : \u2200 {m n} \u2192 Bits m \u2192 SwpOp (m + n)\n0\u2194 [] = \u03b5\n0\u2194 (false{-0-} \u2237 p) = first (0\u2194 p)\n0\u2194 (true{-1-} \u2237 []) = swp\n0\u2194 (true{-1-} \u2237 true {-1-} \u2237 p) = on-extremes (0\u2194 (1b \u2237 p))\n0\u2194 (true{-1-} \u2237 false{-0-} \u2237 p) = on-firsts (0\u2194 (1b \u2237 p))\n\ncommSwpOp : \u2200 m n \u2192 SwpOp (m + n) \u2192 SwpOp (n + m)\ncommSwpOp m n x rewrite \u2115\u00b0.+-comm m n = x\n\n[_\u2194_] : \u2200 {m n} (p q : Bits m) \u2192 SwpOp (m + n)\n[ p \u2194 q ] = 0\u2194 p \u204f 0\u2194 q \u204f 0\u2194 p\n\n[_\u2194\u2032_] : \u2200 {n} (p q : Bits n) \u2192 SwpOp n\n[ p \u2194\u2032 q ] = commSwpOp _ 0 [ p \u2194 q ]\n\n_$swp_ : \u2200 {n a} {A : Set a} \u2192 SwpOp n \u2192 Tree A n \u2192 Tree A n\n\u03b5 $swp t = t\n(f \u204f g) $swp t = g $swp (f $swp t)\n(first f) $swp (fork t\u2080 t\u2081) = fork (f $swp t\u2080) t\u2081\nswp $swp (fork t\u2080 t\u2081) = fork t\u2081 t\u2080\nswp-seconds $swp (fork (fork t\u2080 t\u2081) (fork t\u2082 t\u2083)) = fork (fork t\u2080 t\u2083) (fork t\u2082 t\u2081)\n\nswpRel : \u2200 {n a} {A : Set a} (f : SwpOp n) (t : Tree A n) \u2192 Perm t (f $swp t)\nswpRel \u03b5 _ = \u03b5\nswpRel (f \u204f g) _ = swpRel f _ \u204f swpRel g _\nswpRel (first f) (fork _ _) = first (swpRel f _)\nswpRel swp (fork _ _) = swp\nswpRel swp-seconds\n (fork (fork _ _) (fork _ _)) = swp-seconds\n\n[0\u2194_]-Rel : \u2200 {m n a} {A : Set a} (p : Bits m) (t : Tree A (m + n)) \u2192 Perm t ((0\u2194 p) $swp t)\n[0\u2194 p ]-Rel = swpRel (0\u2194 p)\n\nswpOp' : \u2200 {n a} {A : Set a} {t u : Tree A n} \u2192 Perm0\u2194 t u \u2192 SwpOp n\nswpOp' \u03b5 = \u03b5\nswpOp' (first f) = first (swpOp' f)\nswpOp' swp = swp\nswpOp' (firsts f) = on-firsts (swpOp' f)\nswpOp' (extremes f) = on-extremes (swpOp' f)\n\nswpOp : \u2200 {n a} {A : Set a} {t u : Tree A n} \u2192 Perm t u \u2192 SwpOp n\nswpOp \u03b5 = \u03b5\nswpOp (f \u204f g) = swpOp f \u204f swpOp g\nswpOp (first f) = first (swpOp f)\nswpOp swp = swp\nswpOp swp-seconds = swp-seconds\n\nswpOp-sym : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp n\nswpOp-sym \u03b5 = \u03b5\nswpOp-sym (f \u204f g) = swpOp-sym g \u204f swpOp-sym f\nswpOp-sym (first f) = first (swpOp-sym f)\nswpOp-sym swp = swp\nswpOp-sym swp-seconds = swp-seconds\n\nswpOp-sym-involutive : \u2200 {n} (f : SwpOp n) \u2192 swpOp-sym (swpOp-sym f) \u2261 f\nswpOp-sym-involutive \u03b5 = \u2261.refl\nswpOp-sym-involutive (f \u204f g) rewrite swpOp-sym-involutive f | swpOp-sym-involutive g = \u2261.refl\nswpOp-sym-involutive (first f) rewrite swpOp-sym-involutive f = \u2261.refl\nswpOp-sym-involutive swp = \u2261.refl\nswpOp-sym-involutive swp-seconds = \u2261.refl\n\nswpOp-sym-sound : \u2200 {n a} {A : Set a} (f : SwpOp n) (t : Tree A n) \u2192 swpOp-sym f $swp (f $swp t) \u2261 t\nswpOp-sym-sound \u03b5 t = \u2261.refl\nswpOp-sym-sound (f \u204f g) t rewrite swpOp-sym-sound g (f $swp t) | swpOp-sym-sound f t = \u2261.refl\nswpOp-sym-sound (first f) (fork t _) rewrite swpOp-sym-sound f t = \u2261.refl\nswpOp-sym-sound swp (fork _ _) = \u2261.refl\nswpOp-sym-sound swp-seconds (fork (fork _ _) (fork _ _)) = \u2261.refl\n\nmodule \u00acswp-comm where\n data X : Set where\n A B C D E F G H : X\n n : \u2115\n n = 3\n t : Tree X n\n t = fork (fork (fork (leaf A) (leaf B))(fork (leaf C) (leaf D))) (fork (fork (leaf E) (leaf F))(fork (leaf G) (leaf H)))\n f : SwpOp n\n f = swp\n g : SwpOp n\n g = first swp\n pf : f $swp (g $swp t) \u2262 g $swp (f $swp t)\n pf ()\n\nswp-leaf : \u2200 {a} {A : Set a} (f : SwpOp 0) (x : A) \u2192 f $swp (leaf x) \u2261 leaf x\nswp-leaf \u03b5 x = refl\nswp-leaf (f \u204f g) x rewrite swp-leaf f x | swp-leaf g x = refl\n\nswpOp-sound : \u2200 {n a} {A : Set a} {t u : Tree A n} (perm : Perm t u) \u2192 (swpOp perm $swp t \u2261 u)\nswpOp-sound \u03b5 = refl\nswpOp-sound (f \u204f f\u2081) rewrite swpOp-sound f | swpOp-sound f\u2081 = refl\nswpOp-sound (first f) rewrite swpOp-sound f = refl\nswpOp-sound swp = refl\nswpOp-sound swp-seconds = refl\n\nopen import Relation.Nullary using (Dec ; yes ; no)\nopen import Relation.Nullary.Negation\n-}\n\nmodule new-approach where\n\n open import Data.Empty\n\n import Function.Inverse as FI\n open FI using (_\u2194_; module Inverse; _InverseOf_)\n open import Function.Related\n import Function.Equality\n import Relation.Binary.PropositionalEquality as P\n\n data _\u2208_ {a}{A : Set a}(x : A) : {n : \u2115} \u2192 Tree A n \u2192 Set a where\n here : x \u2208 leaf x\n left : {n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 t\u2081 \u2192 x \u2208 fork t\u2081 t\u2082\n right : {n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 t\u2082 \u2192 x \u2208 fork t\u2081 t\u2082\n\n toBits : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t : Tree A n} \u2192 x \u2208 t \u2192 Bits n\n toBits here = []\n toBits (left key) = 0b \u2237 toBits key\n toBits (right key) = 1b \u2237 toBits key\n\n \u2208-lookup : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t : Tree A n}(path : x \u2208 t) \u2192 lookup (toBits path) t \u2261 x\n \u2208-lookup here = \u2261.refl\n \u2208-lookup (left path) = \u2208-lookup path\n \u2208-lookup (right path) = \u2208-lookup path\n\n lookup-\u2208 : \u2200 {a}{A : Set a}{n : \u2115}(key : Bits n)(t : Tree A n) \u2192 lookup key t \u2208 t\n lookup-\u2208 [] (leaf x) = here\n lookup-\u2208 (true \u2237 key) (fork tree tree\u2081) = right (lookup-\u2208 key tree\u2081)\n lookup-\u2208 (false \u2237 key) (fork tree tree\u2081) = left (lookup-\u2208 key tree)\n\n {-\n _\u2248_ : \u2200 {a}{A : Set a}{n : \u2115} \u2192 Tree A n \u2192 Tree A n \u2192 Set _\n t\u2081 \u2248 t\u2082 = \u2200 x \u2192 (x \u2208 t\u2081) \u2194 (x \u2208 t\u2082)\n\n \u2248-refl : {a : _}{A : Set a}{n : \u2115}{t : Tree A n} \u2192 t \u2248 t\n \u2248-refl _ = FI.id\n\n \u2248-trans : {a : _}{A : Set a}{n : \u2115}{t u v : Tree A n} \u2192 t \u2248 u \u2192 u \u2248 v \u2192 t \u2248 v\n \u2248-trans f g x = g x FI.\u2218 f x\n\n move : \u2200 {a}{A : Set a}{n : \u2115}{t s : Tree A n}{x : A} \u2192 t \u2248 s \u2192 x \u2208 t \u2192 x \u2208 s\n move t\u2248s x\u2208t = Inverse.to (t\u2248s _) Function.Equality.\u27e8$\u27e9 x\u2208t\n\n swap\u2080 : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 fork t\u2081 t\u2082 \u2248 fork t\u2082 t\u2081\n swap\u2080 _ = record\n { to = \u2192-to-\u27f6 swap\n ; from = \u2192-to-\u27f6 swap\n ; inverse-of = record { left-inverse-of = swap-inv\n ; right-inverse-of = swap-inv }\n } where\n swap : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 fork t\u2081 t\u2082 \u2192 x \u2208 fork t\u2082 t\u2081\n swap (left path) = right path\n swap (right path) = left path\n\n swap-inv : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t\u2081 t\u2082 : Tree A n}(p : x \u2208 fork t\u2081 t\u2082) \u2192 swap (swap p) \u2261 p\n swap-inv (left p) = refl\n swap-inv (right p) = refl\n\n swap\u2082 : \u2200 {a}{A : Set a}{n : \u2115}{tA tB tC tD : Tree A n}\n \u2192 fork (fork tA tB) (fork tC tD) \u2248 fork (fork tA tD) (fork tC tB)\n swap\u2082 _ = record\n { to = \u2192-to-\u27f6 fun\n ; from = \u2192-to-\u27f6 fun\n ; inverse-of = record { left-inverse-of = inv\n ; right-inverse-of = inv }\n } where\n fun : \u2200 {a}{A : Set a}{x n}{tA tB tC tD : Tree A n}\n \u2192 x \u2208 fork (fork tA tB) (fork tC tD) \u2192 x \u2208 fork (fork tA tD) (fork tC tB)\n fun (left (left path)) = left (left path)\n fun (left (right path)) = right (right path)\n fun (right (left path)) = right (left path)\n fun (right (right path)) = left (right path)\n\n inv : \u2200 {a}{A : Set a}{x n}{tA tB tC tD : Tree A n}\n \u2192 (p : x \u2208 fork (fork tA tB) (fork tC tD)) \u2192 fun (fun p) \u2261 p\n inv (left (left p)) = refl\n inv (left (right p)) = refl\n inv (right (left p)) = refl\n inv (right (right p)) = refl\n\n _\u27e8fork\u27e9_ : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 s\u2081 s\u2082 : Tree A n} \u2192 t\u2081 \u2248 s\u2081 \u2192 t\u2082 \u2248 s\u2082 \u2192 fork t\u2081 t\u2082 \u2248 fork s\u2081 s\u2082\n (t1\u2248s1 \u27e8fork\u27e9 t2\u2248s2) y = record\n { to = to\n ; from = from\n ; inverse-of = record { left-inverse-of = frk-linv\n ; right-inverse-of = frk-rinv }\n } where\n\n frk : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 s\u2081 s\u2082 : Tree A n}{x : A} \u2192 t\u2081 \u2248 s\u2081 \u2192 t\u2082 \u2248 s\u2082 \u2192 x \u2208 fork t\u2081 t\u2082 \u2192 x \u2208 fork s\u2081 s\u2082\n frk t1\u2248s1 t2\u2248s2 (left x\u2208t1) = left (move t1\u2248s1 x\u2208t1)\n frk t1\u2248s1 t2\u2248s2 (right x\u2208t2) = right (move t2\u2248s2 x\u2208t2)\n\n to = \u2192-to-\u27f6 (frk t1\u2248s1 t2\u2248s2)\n from = \u2192-to-\u27f6 (frk (\u03bb x \u2192 FI.sym (t1\u2248s1 x)) (\u03bb x \u2192 FI.sym (t2\u2248s2 x)))\n\n\n open Function.Equality using (_\u27e8$\u27e9_)\n open import Function.LeftInverse\n\n frk-linv : from LeftInverseOf to\n frk-linv (left x) = \u2261.cong left (_InverseOf_.left-inverse-of (Inverse.inverse-of (t1\u2248s1 y)) x)\n frk-linv (right x) = \u2261.cong right (_InverseOf_.left-inverse-of (Inverse.inverse-of (t2\u2248s2 y)) x)\n\n frk-rinv : from RightInverseOf to -- \u2200 x \u2192 to \u27e8$\u27e9 (from \u27e8$\u27e9 x) \u2261 x\n frk-rinv (left x) = \u2261.cong left (_InverseOf_.right-inverse-of (Inverse.inverse-of (t1\u2248s1 y)) x)\n frk-rinv (right x) = \u2261.cong right (_InverseOf_.right-inverse-of (Inverse.inverse-of (t2\u2248s2 y)) x)\n\n \u2248-first : \u2200 {a}{A : Set a}{n : \u2115}{t u v : Tree A n} \u2192 t \u2248 u \u2192 fork t v \u2248 fork u v\n \u2248-first f = f \u27e8fork\u27e9 \u2248-refl\n\n \u2248-second : \u2200 {a}{A : Set a}{n : \u2115}{t u v : Tree A n} \u2192 t \u2248 u \u2192 fork v t \u2248 fork v u\n \u2248-second f = \u2248-refl \u27e8fork\u27e9 f\n\n swap-inner : \u2200 {a}{A : Set a}{n : \u2115}{tA tB tC tD : Tree A n}\n \u2192 fork (fork tA tB) (fork tC tD) \u2248 fork (fork tA tC) (fork tB tD)\n swap-inner = \u2248-trans (\u2248-second swap\u2080) (\u2248-trans swap\u2082 (\u2248-second swap\u2080))\n\n Rot\u27f6\u2248 : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 Rot t\u2081 t\u2082 \u2192 t\u2081 \u2248 t\u2082\n Rot\u27f6\u2248 (leaf x) = \u2248-refl\n Rot\u27f6\u2248 (fork rot rot\u2081) = Rot\u27f6\u2248 rot \u27e8fork\u27e9 Rot\u27f6\u2248 rot\u2081\n Rot\u27f6\u2248 (krof {_} {l} {l'} {r} {r'} rot rot\u2081) = \u03bb y \u2192\n y \u2208 fork l r \u2194\u27e8 (Rot\u27f6\u2248 rot \u27e8fork\u27e9 Rot\u27f6\u2248 rot\u2081) y \u27e9\n y \u2208 fork r' l' \u2194\u27e8 swap\u2080 y \u27e9\n y \u2208 fork l' r' \u220e\n where open EquationalReasoning\n\n Perm\u27f6\u2248 : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 Perm t\u2081 t\u2082 \u2192 t\u2081 \u2248 t\u2082\n Perm\u27f6\u2248 \u03b5 = \u2248-refl\n Perm\u27f6\u2248 (f \u204f g) = \u2248-trans (Perm\u27f6\u2248 f) (Perm\u27f6\u2248 g)\n Perm\u27f6\u2248 (first f) = \u2248-first (Perm\u27f6\u2248 f)\n Perm\u27f6\u2248 swp = swap\u2080\n Perm\u27f6\u2248 swp-seconds = swap\u2082\n\n Perm0\u2194\u27f6\u2248 : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 Perm0\u2194 t\u2081 t\u2082 \u2192 t\u2081 \u2248 t\u2082\n Perm0\u2194\u27f6\u2248 \u03b5 = \u2248-refl\n Perm0\u2194\u27f6\u2248 swp = swap\u2080\n Perm0\u2194\u27f6\u2248 (first t) = \u2248-first (Perm0\u2194\u27f6\u2248 t)\n Perm0\u2194\u27f6\u2248 (firsts t) = \u2248-trans swap-inner (\u2248-trans (\u2248-first (Perm0\u2194\u27f6\u2248 t)) swap-inner)\n Perm0\u2194\u27f6\u2248 (extremes t) = \u2248-trans swap\u2082 (\u2248-trans (\u2248-first (Perm0\u2194\u27f6\u2248 t)) swap\u2082)\n\n put : {a : _}{A : Set a}{n : \u2115} \u2192 Bits n \u2192 A \u2192 Tree A n \u2192 Tree A n\n put [] val tree = leaf val\n put (x \u2237 key) val (fork tree tree\u2081) = if x then fork tree (put key val tree\u2081)\n else fork (put key val tree) tree\u2081\n\n -- move-me\n _\u2237\u2262_ : {n : \u2115}{xs ys : Bits n}(x : Bit) \u2192 x \u2237 xs \u2262 x \u2237 ys \u2192 xs \u2262 ys\n _\u2237\u2262_ x = contraposition $ \u2261.cong $ _\u2237_ x\n\n \u2208-put : {a : _}{A : Set a}{n : \u2115}(p : Bits n){x : A}(t : Tree A n) \u2192 x \u2208 put p x t\n \u2208-put [] t = here\n \u2208-put (true \u2237 p) (fork t t\u2081) = right (\u2208-put p t\u2081)\n \u2208-put (false \u2237 p) (fork t t\u2081) = left (\u2208-put p t)\n\n \u2208-put-\u2262 : {a : _}{A : Set a}{n : \u2115}(p : Bits n){x y : A}{t : Tree A n}(path : x \u2208 t)\n \u2192 p \u2262 toBits path \u2192 x \u2208 put p y t\n \u2208-put-\u2262 [] here neg = \u22a5-elim (neg refl)\n \u2208-put-\u2262 (true \u2237 p) (left path) neg = left path\n \u2208-put-\u2262 (false \u2237 p) (left path) neg = left (\u2208-put-\u2262 p path (false \u2237\u2262 neg))\n \u2208-put-\u2262 (true \u2237 p) (right path) neg = right (\u2208-put-\u2262 p path (true \u2237\u2262 neg))\n \u2208-put-\u2262 (false \u2237 p) (right path) neg = right path\n\n swap : {a : _}{A : Set a}{n : \u2115} \u2192 (p\u2081 p\u2082 : Bits n) \u2192 Tree A n \u2192 Tree A n\n swap p\u2081 p\u2082 t = put p\u2081 a\u2082 (put p\u2082 a\u2081 t)\n where\n a\u2081 = lookup p\u2081 t\n a\u2082 = lookup p\u2082 t\n\n swap-perm\u2081 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p : x \u2208 t) \u2192 t \u2248 swap (toBits p) (toBits p) t\n swap-perm\u2081 here = \u2248-refl\n swap-perm\u2081 (left path) = \u2248-first (swap-perm\u2081 path)\n swap-perm\u2081 (right path) = \u2248-second (swap-perm\u2081 path)\n\n swap-comm : {a : _}{A : Set a}{n : \u2115} (p\u2081 p\u2082 : Bits n)(t : Tree A n) \u2192 swap p\u2082 p\u2081 t \u2261 swap p\u2081 p\u2082 t\n swap-comm [] [] (leaf x) = refl\n swap-comm (true \u2237 p\u2081) (true \u2237 p\u2082) (fork t t\u2081) = \u2261.cong (fork t) (swap-comm p\u2081 p\u2082 t\u2081)\n swap-comm (true \u2237 p\u2081) (false \u2237 p\u2082) (fork t t\u2081) = refl\n swap-comm (false \u2237 p\u2081) (true \u2237 p\u2082) (fork t t\u2081) = refl\n swap-comm (false \u2237 p\u2081) (false \u2237 p\u2082) (fork t t\u2081) = \u2261.cong (flip fork t\u2081) (swap-comm p\u2081 p\u2082 t)\n\n swap-perm\u2082 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p' : Bits n)(p : x \u2208 t)\n \u2192 x \u2208 swap (toBits p) p' t\n swap-perm\u2082 _ here = here\n swap-perm\u2082 (true \u2237 p) (left path) rewrite \u2208-lookup path = right (\u2208-put p _)\n swap-perm\u2082 (false \u2237 p) (left path) = left (swap-perm\u2082 p path)\n swap-perm\u2082 (true \u2237 p) (right path) = right (swap-perm\u2082 p path)\n swap-perm\u2082 (false \u2237 p) (right path) rewrite \u2208-lookup path = left (\u2208-put p _)\n\n swap-perm\u2083 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p\u2081 p\u2082 : Bits n)(p : x \u2208 t)\n \u2192 p\u2081 \u2262 toBits p \u2192 p\u2082 \u2262 toBits p \u2192 x \u2208 swap p\u2081 p\u2082 t\n swap-perm\u2083 [] [] here neg\u2081 neg\u2082 = here\n swap-perm\u2083 (true \u2237 p\u2081) (true \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left path\n swap-perm\u2083 (true \u2237 p\u2081) (false \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left (\u2208-put-\u2262 _ path (false \u2237\u2262 neg\u2082))\n swap-perm\u2083 (false \u2237 p\u2081) (true \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left (\u2208-put-\u2262 _ path (false \u2237\u2262 neg\u2081))\n swap-perm\u2083 (false \u2237 p\u2081) (false \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left\n (swap-perm\u2083 p\u2081 p\u2082 path (false \u2237\u2262 neg\u2081) (false \u2237\u2262 neg\u2082))\n swap-perm\u2083 (true \u2237 p\u2081) (true \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right\n (swap-perm\u2083 p\u2081 p\u2082 path (true \u2237\u2262 neg\u2081) (true \u2237\u2262 neg\u2082))\n swap-perm\u2083 (true \u2237 p\u2081) (false \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right (\u2208-put-\u2262 _ path (true \u2237\u2262 neg\u2081))\n swap-perm\u2083 (false \u2237 p\u2081) (true \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right (\u2208-put-\u2262 _ path (true \u2237\u2262 neg\u2082))\n swap-perm\u2083 (false \u2237 p\u2081) (false \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right path\n\n \u2208-swp : \u2200 {n a} {A : Set a} (f : SwpOp n) {x : A} {t : Tree A n} \u2192 x \u2208 t \u2192 x \u2208 (f $swp t)\n \u2208-swp \u03b5 pf = pf\n \u2208-swp (f \u204f g) pf = \u2208-swp g (\u2208-swp f pf)\n \u2208-swp (first f) {t = fork _ _} (left pf) = left (\u2208-swp f pf)\n \u2208-swp (first f) {t = fork _ _} (right pf) = right pf\n \u2208-swp swp {t = fork t u} (left pf) = right pf\n \u2208-swp swp {t = fork t u} (right pf) = left pf\n \u2208-swp swp-seconds {t = fork (fork _ _) (fork _ _)} (left (left pf)) = left (left pf)\n \u2208-swp swp-seconds {t = fork (fork _ _) (fork _ _)} (left (right pf)) = right (right pf)\n \u2208-swp swp-seconds {t = fork (fork _ _) (fork _ _)} (right (left pf)) = right (left pf)\n \u2208-swp swp-seconds {t = fork (fork _ _) (fork _ _)} (right (right pf)) = left (right pf)\n\nmodule FoldProp {a} {A : Set a} (_\u00b7_ : Op\u2082 A) (op-comm : Commutative _\u2261_ _\u00b7_) (op-assoc : Associative _\u2261_ _\u00b7_) where\n\n \u27ea_\u27eb : \u2200 {n} \u2192 Tree A n \u2192 A\n \u27ea_\u27eb = fold _\u00b7_\n\n _=[fold]\u21d2\u2032_ : \u2200 {\u2113\u2081 \u2113\u2082} \u2192 (\u2200 {m n} \u2192 REL (Tree A m) (Tree A n) \u2113\u2081) \u2192 Rel A \u2113\u2082 \u2192 Set _\n -- _\u223c\u2080_ =[fold]\u21d2 _\u223c\u2081_ = \u2200 {m n} \u2192 _\u223c\u2080_ {m} {n} =[ fold {n} _\u00b7_ ]\u21d2 _\u223c\u2081_\n _\u223c\u2080_ =[fold]\u21d2\u2032 _\u223c\u2081_ = \u2200 {m n} {t : Tree A m} {u : Tree A n} \u2192 t \u223c\u2080 u \u2192 \u27ea t \u27eb \u223c\u2081 \u27ea u \u27eb\n\n _=[fold]\u21d2_ : \u2200 {\u2113\u2081 \u2113\u2082} \u2192 (\u2200 {n} \u2192 Rel (Tree A n) \u2113\u2081) \u2192 Rel A \u2113\u2082 \u2192 Set _\n _\u223c\u2080_ =[fold]\u21d2 _\u223c\u2081_ = \u2200 {n} \u2192 _\u223c\u2080_ =[ fold {n} _\u00b7_ ]\u21d2 _\u223c\u2081_\n\n fold-rot : Rot =[fold]\u21d2 _\u2261_\n fold-rot (leaf x) = refl\n fold-rot (fork rot rot\u2081) = cong\u2082 _\u00b7_ (fold-rot rot) (fold-rot rot\u2081)\n fold-rot (krof rot rot\u2081) rewrite fold-rot rot | fold-rot rot\u2081 = op-comm _ _\n\n -- t \u223c u \u2192 fork v t \u223c fork u w\n\n lem : \u2200 x y z t \u2192 (x \u00b7 y) \u00b7 (z \u00b7 t) \u2261 (t \u00b7 y) \u00b7 (z \u00b7 x)\n lem x y z t = (x \u00b7 y) \u00b7 (z \u00b7 t)\n \u2261\u27e8 op-assoc x y _ \u27e9\n x \u00b7 (y \u00b7 (z \u00b7 t))\n \u2261\u27e8 op-comm x _ \u27e9\n (y \u00b7 (z \u00b7 t)) \u00b7 x\n \u2261\u27e8 op-assoc y (z \u00b7 t) _ \u27e9\n y \u00b7 ((z \u00b7 t) \u00b7 x)\n \u2261\u27e8 \u2261.cong (\u03bb u \u2192 y \u00b7 (u \u00b7 x)) (op-comm z t) \u27e9\n y \u00b7 ((t \u00b7 z) \u00b7 x)\n \u2261\u27e8 \u2261.cong (_\u00b7_ y) (op-assoc t z x) \u27e9\n y \u00b7 (t \u00b7 (z \u00b7 x))\n \u2261\u27e8 \u2261.sym (op-assoc y t _) \u27e9\n (y \u00b7 t) \u00b7 (z \u00b7 x)\n \u2261\u27e8 \u2261.cong (\u03bb u \u2192 u \u00b7 (z \u00b7 x)) (op-comm y t) \u27e9\n (t \u00b7 y) \u00b7 (z \u00b7 x)\n \u220e\n where open \u2261-Reasoning\n\n fold-swp : Swp =[fold]\u21d2 _\u2261_\n fold-swp (left pf) rewrite fold-swp pf = refl\n fold-swp (right pf) rewrite fold-swp pf = refl\n fold-swp swp\u2081 = op-comm _ _\n fold-swp (swp\u2082 {_} {t\u2080\u2080} {t\u2080\u2081} {t\u2081\u2080} {t\u2081\u2081}) = lem \u27ea t\u2080\u2080 \u27eb \u27ea t\u2080\u2081 \u27eb \u27ea t\u2081\u2080 \u27eb \u27ea t\u2081\u2081 \u27eb\n\n fold-swp\u2605 : Swp\u2605 =[fold]\u21d2 _\u2261_\n fold-swp\u2605 \u03b5 = refl\n fold-swp\u2605 (x \u25c5 xs) rewrite fold-swp x | fold-swp\u2605 xs = refl\n-}\n\nmodule FoldProp {a \u2113} {A : Set a} (_\u24cd_ : Set \u2113 \u2192 Set \u2113 \u2192 Set \u2113) where\n Fold : \u2200 {n} \u2192 (Bits n \u2192 A \u2192 Set \u2113) \u2192 Tree A n \u2192 Set \u2113\n Fold f (leaf x) = f [] x\n Fold f (fork t\u2080 t\u2081) = Fold (f \u2218 0\u2237_) t\u2080 \u24cd Fold (f \u2218 1\u2237_) t\u2081\n\nAll : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A \u2192 Set) \u2192 Tree A n \u2192 Set\nAll = FoldProp.Fold _\u00d7_\n\nAny : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A \u2192 Set) \u2192 Tree A n \u2192 Set\nAny = FoldProp.Fold _\u228e_\n\nopen Alternative-Reverse\n\nmodule AllBits where\n _IsRevPrefixOf_ : \u2200 {m n} \u2192 Bits m \u2192 Bits (rev-+ m n) \u2192 Set\n _IsRevPrefixOf_ {m} {n} p xs = \u2203 \u03bb (ys : Bits n) \u2192 rev-app p ys \u2261 xs\n\n RevPrefix : \u2200 {m n o} (p : Bits m) \u2192 Tree (Bits (rev-+ m n)) o \u2192 Set\n RevPrefix p = All (\u03bb _ \u2192 _IsRevPrefixOf_ p)\n\n RevPrefix-[]-\u22a4 : \u2200 {m n} (t : Tree (Bits m) n) \u2192 RevPrefix [] t\n RevPrefix-[]-\u22a4 (leaf x) = x , \u2261.refl\n RevPrefix-[]-\u22a4 (fork t u) = RevPrefix-[]-\u22a4 t , RevPrefix-[]-\u22a4 u\n\n All-fromFun : \u2200 {m} n (p : Bits m) \u2192 All (_\u2261_ \u2218 rev-app p) (fromFun {n} (rev-app p))\n All-fromFun zero p = \u2261.refl\n All-fromFun (suc n) p = All-fromFun n (0\u2237 p) , All-fromFun n (1\u2237 p)\n\n All-id : \u2200 n \u2192 All {n} _\u2261_ (fromFun id)\n All-id n = All-fromFun n []\n\nopen new-approach\n\n\u2208-fromFun : \u2200 {m n x} (f : Bits m \u2192 Bits n) (p : x \u2208 fromFun f) \u2192 f (toBits p) \u2261 x\n\u2208-fromFun f here = \u2261.refl\n\u2208-fromFun f (left p) = \u2208-fromFun (f \u2218 0\u2237_) p\n\u2208-fromFun f (right p) = \u2208-fromFun (f \u2218 1\u2237_) p\n\n\u2208-rev-app : \u2200 {m} n {x : Bits (rev-+ m n)} (q : Bits m) (p : x \u2208 fromFun (rev-app q)) \u2192 rev-app q (toBits p) \u2261 x\n\u2208-rev-app _ = \u2208-fromFun \u2218 rev-app\n\nfirst : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 A\nfirst (leaf x) = x\nfirst (fork t _) = first t\n\nlast : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 A\nlast (leaf x) = x\nlast (fork _ t) = last t\n\nmodule SortedDataIx {a \u2113} {A : Set a} (_\u2264\u1d2c_ : A \u2192 A \u2192 Set \u2113) (isPreorder : IsPreorder _\u2261_ _\u2264\u1d2c_) where\n data Sorted : \u2200 {n} \u2192 Tree A n \u2192 A \u2192 A \u2192 Set (a L.\u2294 \u2113) where\n leaf : {x : A} \u2192 Sorted (leaf x) x x\n fork : \u2200 {n} {t u : Tree A n} {low_t high_t low\u1d64 high\u1d64} \u2192\n Sorted t low_t high_t \u2192\n Sorted u low\u1d64 high\u1d64 \u2192\n (h\u2264l : high_t \u2264\u1d2c low\u1d64) \u2192\n Sorted (fork t u) low_t high\u1d64\n\n private\n module \u2264\u1d2c = IsPreorder isPreorder\n\n \u2264\u1d2c-bounds : \u2200 {n} {t : Tree A n} {l h} \u2192 Sorted t l h \u2192 l \u2264\u1d2c h\n \u2264\u1d2c-bounds leaf = \u2264\u1d2c.refl\n \u2264\u1d2c-bounds (fork s\u2080 s\u2081 pf) = \u2264\u1d2c.trans (\u2264\u1d2c-bounds s\u2080) (\u2264\u1d2c.trans pf (\u2264\u1d2c-bounds s\u2081))\n\n Sorted\u2192lb : \u2200 {n} {t : Tree A n} {l h} \u2192 Sorted t l h \u2192 \u2200 {x} \u2192 x \u2208 t \u2192 l \u2264\u1d2c x\n Sorted\u2192lb leaf here = \u2264\u1d2c.refl\n Sorted\u2192lb (fork s _ _) (left p) = Sorted\u2192lb s p\n Sorted\u2192lb (fork s\u2080 s\u2081 pf) (right p) = \u2264\u1d2c.trans (\u2264\u1d2c.trans (\u2264\u1d2c-bounds s\u2080) pf) (Sorted\u2192lb s\u2081 p)\n\n Sorted\u2192ub : \u2200 {n} {t : Tree A n} {l h} \u2192 Sorted t l h \u2192 \u2200 {x} \u2192 x \u2208 t \u2192 x \u2264\u1d2c h\n Sorted\u2192ub leaf here = \u2264\u1d2c.refl\n Sorted\u2192ub (fork _ s _) (right p) = Sorted\u2192ub s p\n Sorted\u2192ub (fork s\u2080 s\u2081 pf) (left p) = \u2264\u1d2c.trans (\u2264\u1d2c.trans (Sorted\u2192ub s\u2080 p) pf) (\u2264\u1d2c-bounds s\u2081)\n\n Bounded : \u2200 {n} \u2192 Tree A n \u2192 A \u2192 A \u2192 Set (a L.\u2294 \u2113)\n Bounded t l h = \u2200 {x} \u2192 x \u2208 t \u2192 (l \u2264\u1d2c x) \u00d7 (x \u2264\u1d2c h)\n\n Sorted\u2192Bounded : \u2200 {n} {t : Tree A n} {l h} \u2192 Sorted t l h \u2192 Bounded t l h\n Sorted\u2192Bounded s x = Sorted\u2192lb s x , Sorted\u2192ub s x\n\n first-lb : \u2200 {n} {t : Tree A n} {l h} \u2192 Sorted t l h \u2192 first t \u2261 l\n first-lb leaf = \u2261.refl\n first-lb (fork st _ _) = first-lb st\n\n last-ub : \u2200 {n} {t : Tree A n} {l h} \u2192 Sorted t l h \u2192 last t \u2261 h\n last-ub leaf = \u2261.refl\n last-ub (fork _ st _) = last-ub st\n\n uniq-lb : \u2200 {n} {t : Tree A n} {l\u2080 h\u2080 l\u2081 h\u2081}\n \u2192 Sorted t l\u2080 h\u2080 \u2192 Sorted t l\u2081 h\u2081 \u2192 l\u2080 \u2261 l\u2081\n uniq-lb leaf leaf = \u2261.refl\n uniq-lb (fork p p\u2081 h\u2264l) (fork q q\u2081 h\u2264l\u2081) = uniq-lb p q\n\n uniq-ub : \u2200 {n} {t : Tree A n} {l\u2080 h\u2080 l\u2081 h\u2081}\n \u2192 Sorted t l\u2080 h\u2080 \u2192 Sorted t l\u2081 h\u2081 \u2192 h\u2080 \u2261 h\u2081\n uniq-ub leaf leaf = \u2261.refl\n uniq-ub (fork p p\u2081 h\u2264l) (fork q q\u2081 h\u2264l\u2081) = uniq-ub p\u2081 q\u2081\n\n Sorted-trans : \u2200 {n} {t u v : Tree A n} {lt hu lu hv}\n \u2192 Sorted (fork t u) lt hu \u2192 Sorted (fork u v) lu hv \u2192 Sorted (fork t v) lt hv\n Sorted-trans {lt = lt} {hu} {lu} {hv} (fork tu tu\u2081 h\u2264l) (fork uv uv\u2081 h\u2264l\u2081)\n rewrite uniq-lb uv tu\u2081\n | uniq-ub uv tu\u2081\n = fork tu uv\u2081 (\u2264\u1d2c.trans h\u2264l (\u2264\u1d2c.trans (\u2264\u1d2c-bounds tu\u2081) h\u2264l\u2081))\n\nmodule Sorted' {a \u2113} {A : Set a} (_\u2264\u1d2c_ : A \u2192 A \u2192 Set \u2113) (isPreorder : IsPreorder _\u2261_ _\u2264\u1d2c_) where\n data _\u2264\u1d2e_ : \u2200 {n} (p q : Bits n) \u2192 Set where\n [] : [] \u2264\u1d2e []\n there : \u2200 {n} {p q : Bits n} b \u2192 p \u2264\u1d2e q \u2192 (b \u2237 p) \u2264\u1d2e (b \u2237 q)\n 0-1 : \u2200 {n} (p q : Bits n) \u2192 0\u2237 p \u2264\u1d2e 1\u2237 q\n\n _\u2264\u1d3e_ : \u2200 {n x y} {t : Tree A n} \u2192 x \u2208 t \u2192 y \u2208 t \u2192 Set\n p \u2264\u1d3e q = toBits p \u2264\u1d2e toBits q\n\n Sorted : \u2200 {n} \u2192 Tree A n \u2192 Set _\n Sorted t = \u2200 {x} (p : x \u2208 t) {y} (q : y \u2208 t) \u2192 p \u2264\u1d3e q \u2192 x \u2264\u1d2c y\n\n private\n module \u2264\u1d2c = IsPreorder isPreorder\n\n module S = SortedDataIx _\u2264\u1d2c_ isPreorder\n open S using (leaf; fork)\n Sorted\u2192Sorted' : \u2200 {n l h} {t : Tree A n} \u2192 S.Sorted t l h \u2192 Sorted t\n Sorted\u2192Sorted' leaf here here p\u2264q = \u2264\u1d2c.refl\n Sorted\u2192Sorted' (fork s _ _) (left p) (left q) (there ._ p\u2264q) = Sorted\u2192Sorted' s p q p\u2264q\n Sorted\u2192Sorted' (fork s\u2080 s\u2081 l\u2264h) (left p) (right q) p\u2264q = \u2264\u1d2c.trans (S.Sorted\u2192ub s\u2080 p) (\u2264\u1d2c.trans l\u2264h (S.Sorted\u2192lb s\u2081 q))\n Sorted\u2192Sorted' (fork _ _ _) (right _) (left _) ()\n Sorted\u2192Sorted' (fork _ s _) (right p) (right q) (there ._ p\u2264q) = Sorted\u2192Sorted' s p q p\u2264q\n\nprivate\n module Dummy {a} {A : Set a} where\n lft : \u2200 {n} \u2192 Tree A (1 + n) \u2192 Tree A n\n lft (fork t _) = t\n\n rght : \u2200 {n} \u2192 Tree A (1 + n) \u2192 Tree A n\n rght (fork _ t) = t\n\n \u03b7fork : \u2200 {n} (t : Tree A (1 + n)) \u2192 t \u2261 fork (lft t) (rght t)\n \u03b7fork (fork _ _) = \u2261.refl\n\n swap : \u2200 {n} \u2192 Tree A (1 + n) \u2192 Tree A (1 + n)\n swap (fork t u) = fork u t\n\n map-inner : \u2200 {n} \u2192 (Tree A (1 + n) \u2192 Tree A (1 + n)) \u2192 (Tree A (2 + n) \u2192 Tree A (2 + n))\n map-inner f (fork (fork t\u2080 t\u2081) (fork t\u2082 t\u2083)) =\n case f (fork t\u2081 t\u2082) of \u03bb { (fork t\u2084 t\u2085) \u2192 fork (fork t\u2080 t\u2084) (fork t\u2085 t\u2083) }\n\n map-outer : \u2200 {n} \u2192 (f g : Tree A n \u2192 Tree A n) \u2192 (Tree A (1 + n) \u2192 Tree A (1 + n))\n map-outer f g (fork t u) = fork (f t) (g u)\n\n interchange : \u2200 {n} \u2192 Tree A (2 + n) \u2192 Tree A (2 + n)\n interchange = map-inner swap\nopen Dummy public\n\nmodule Sorting {a} {A : Set a} (_\u2293\u1d2c_ _\u2294\u1d2c_ : A \u2192 A \u2192 A) where\n\n merge : \u2200 {n} \u2192 Endo (Tree A (1 + n))\n merge {zero} (fork (leaf x\u2080) (leaf x\u2081)) =\n fork (leaf (x\u2080 \u2293\u1d2c x\u2081)) (leaf (x\u2080 \u2294\u1d2c x\u2081))\n merge {suc _} t\n = (map-inner merge \u2218 map-outer merge merge \u2218 interchange) t\n\n sort : \u2200 {n} \u2192 Tree A n \u2192 Tree A n\n sort {zero} = id\n sort {suc n} = merge \u2218 map-outer sort sort\n\nmodule SortedData {a \u2113} {A : Set a} (_\u2264\u1d2c_ : A \u2192 A \u2192 Set \u2113) where\n data Sorted : \u2200 {n} \u2192 Tree A n \u2192 Set (a L.\u2294 \u2113) where\n leaf : {x : A} \u2192 Sorted (leaf x)\n fork : \u2200 {n} {t u : Tree A n} \u2192\n Sorted t \u2192\n Sorted u \u2192\n (h\u2264l : last t \u2264\u1d2c first u) \u2192\n Sorted (fork t u)\n\n PreSorted : \u2200 {n} \u2192 Tree A (1 + n) \u2192 Set _\n PreSorted t = Sorted (lft t) \u00d7 Sorted (rght t)\n\nmodule MergeSwap {a} {A : Set a}\n (_\u2293\u1d2c_ _\u2294\u1d2c_ : A \u2192 A \u2192 A)\n (\u2293-comm : Commutative _\u2261_ _\u2293\u1d2c_)\n (\u2294-comm : Commutative _\u2261_ _\u2294\u1d2c_) where\n open Sorting _\u2293\u1d2c_ _\u2294\u1d2c_\n merge-swap : \u2200 {n} (t : Tree A (1 + n)) \u2192 merge t \u2261 merge (swap t)\n merge-swap (fork (leaf x) (leaf y)) rewrite \u2294-comm x y | \u2293-comm y x = \u2261.refl\n merge-swap (fork (fork t\u2080 t\u2081) (fork u\u2080 u\u2081))\n rewrite merge-swap (fork t\u2080 u\u2080)\n | merge-swap (fork t\u2081 u\u2081) = \u2261.refl\n\nmodule SortingDataIxProperties {\u2113 a} {A : Set a} (_\u2264\u1d2c_ : A \u2192 A \u2192 Set \u2113)\n (_\u2293\u1d2c_ _\u2294\u1d2c_ : A \u2192 A \u2192 A)\n (isPreorder : IsPreorder _\u2261_ _\u2264\u1d2c_)\n (\u2294-spec : \u2200 {x y} \u2192 x \u2264\u1d2c y \u2192 x \u2294\u1d2c y \u2261 y)\n (\u2293-spec : \u2200 {x y} \u2192 x \u2264\u1d2c y \u2192 x \u2293\u1d2c y \u2261 x)\n (\u2293-comm : Commutative _\u2261_ _\u2293\u1d2c_)\n (\u2294-comm : Commutative _\u2261_ _\u2294\u1d2c_)\n where\n open MergeSwap _\u2293\u1d2c_ _\u2294\u1d2c_ \u2293-comm \u2294-comm\n module \u2264\u1d2c = IsPreorder isPreorder\n open SortedDataIx _\u2264\u1d2c_ isPreorder\n open Sorting _\u2293\u1d2c_ _\u2294\u1d2c_\n\n merge-pres : \u2200 {n} {t : Tree A (1 + n)} {l h} \u2192 Sorted t l h \u2192 merge t \u2261 t\n merge-pres (fork leaf leaf x) = \u2261.cong\u2082 (fork on leaf) (\u2293-spec x) (\u2294-spec x)\n merge-pres {t = fork (fork t\u2080 t\u2081) (fork u\u2080 u\u2081)}\n (fork (fork {low_t = lt\u2080} {ht\u2080} {lt\u2081} {ht\u2081} st\u2080 st\u2081 ht\u2080\u2264lt\u2081)\n (fork {low_t = lu\u2080} {hu\u2080} {lu\u2081} {hu\u2081} su\u2080 su\u2081 hu\u2080\u2264lu\u2081) ht\u2081\u2264lu\u2080)\n rewrite merge-pres (fork st\u2080 su\u2080 (\u2264\u1d2c.trans ht\u2080\u2264lt\u2081 (\u2264\u1d2c.trans (\u2264\u1d2c-bounds st\u2081) ht\u2081\u2264lu\u2080)))\n | merge-pres (fork st\u2081 su\u2081 (\u2264\u1d2c.trans ht\u2081\u2264lu\u2080 (\u2264\u1d2c.trans (\u2264\u1d2c-bounds su\u2080) hu\u2080\u2264lu\u2081)))\n | merge-swap (fork u\u2080 t\u2081)\n | merge-pres (fork st\u2081 su\u2080 ht\u2081\u2264lu\u2080) = \u2261.refl\n\nmodule SortingProperties {\u2113 a} {A : Set a} (_\u2264\u1d2c_ : A \u2192 A \u2192 Set \u2113)\n (_\u2293\u1d2c_ _\u2294\u1d2c_ : A \u2192 A \u2192 A)\n (isPreorder : IsPreorder _\u2261_ _\u2264\u1d2c_)\n (\u2264-\u2294 : \u2200 x y \u2192 x \u2264\u1d2c (y \u2294\u1d2c x))\n (\u2293-\u2264 : \u2200 x y \u2192 (x \u2293\u1d2c y) \u2264\u1d2c y)\n (\u2264-<_,_> : \u2200 {x y z} \u2192 x \u2264\u1d2c y \u2192 x \u2264\u1d2c z \u2192 x \u2264\u1d2c (y \u2293\u1d2c z))\n (\u2264-[_,_] : \u2200 {x y z} \u2192 x \u2264\u1d2c z \u2192 y \u2264\u1d2c z \u2192 (x \u2294\u1d2c y) \u2264\u1d2c z)\n (\u2264-\u2293\u2080 : \u2200 {x y z} \u2192 x \u2264\u1d2c (y \u2293\u1d2c z) \u2192 x \u2264\u1d2c y)\n (\u2264-\u2293\u2081 : \u2200 {x y z} \u2192 x \u2264\u1d2c (y \u2293\u1d2c z) \u2192 x \u2264\u1d2c z)\n (\u2264-\u2294\u2080 : \u2200 {x y z} \u2192 (x \u2294\u1d2c y) \u2264\u1d2c z \u2192 x \u2264\u1d2c z)\n (\u2264-\u2294\u2081 : \u2200 {x y z} \u2192 (x \u2294\u1d2c y) \u2264\u1d2c z \u2192 y \u2264\u1d2c z)\n where\n module \u2264\u1d2c = IsPreorder isPreorder\n -- open SortedDataIx _\u2264\u1d2c_ isPreorder\n open Sorting _\u2293\u1d2c_ _\u2294\u1d2c_\n module SD = SortedData _\u2264\u1d2c_\n open SD using (fork; leaf; PreSorted)\n\n first-merge : \u2200 {n} (t : Tree A (1 + n)) \u2192\n first (merge t) \u2261 first (lft t) \u2293\u1d2c first (rght t)\n first-merge (fork (leaf x) (leaf y)) = \u2261.refl\n first-merge (fork (fork t\u2080 t\u2081) (fork u\u2080 u\u2081))\n with merge (fork t\u2080 u\u2080) | first-merge (fork t\u2080 u\u2080)\n | merge (fork t\u2081 u\u2081)\n ... | fork v\u2080 w\u2080 | pf\n | fork v\u2081 w\u2081\n with merge (fork w\u2080 v\u2081)\n ... | fork a b\n = pf\n\n last-merge : \u2200 {n} (t : Tree A (1 + n)) \u2192\n last (merge t) \u2261 last (lft t) \u2294\u1d2c last (rght t)\n last-merge (fork (leaf x) (leaf y)) = \u2261.refl\n last-merge (fork (fork t\u2080 t\u2081) (fork u\u2080 u\u2081))\n with merge (fork t\u2080 u\u2080)\n | merge (fork t\u2081 u\u2081) | last-merge (fork t\u2081 u\u2081)\n ... | fork v\u2080 w\u2080\n | fork v\u2081 w\u2081 | pf\n with merge (fork w\u2080 v\u2081)\n ... | fork a b\n = pf\n\n merge-spec\u2032 : \u2200 {n} {t u : Tree A n} \u2192\n SD.Sorted t \u2192 SD.Sorted u \u2192\n let tu' = merge (fork t u) in\n SD.Sorted tu'\n \u00d7 last (lft tu') \u2264\u1d2c (last t \u2293\u1d2c last u)\n \u00d7 (first t \u2294\u1d2c first u) \u2264\u1d2c first (rght tu')\n merge-spec\u2032 (leaf {x}) (leaf {y}) = fork leaf leaf (\u2264\u1d2c.trans (\u2293-\u2264 x y) (\u2264-\u2294 y x)) , \u2264\u1d2c.refl , \u2264\u1d2c.refl\n merge-spec\u2032 {t = fork t\u2080 t\u2081} {u = fork u\u2080 u\u2081}\n (fork st\u2080 st\u2081 ht\u2080\u2264lt\u2081)\n (fork su\u2080 su\u2081 lu\u2080\u2264hu\u2081)\n with merge (fork t\u2080 u\u2080) | merge-spec\u2032 st\u2080 su\u2080 | last-merge (fork t\u2080 u\u2080)\n | merge (fork t\u2081 u\u2081) | merge-spec\u2032 st\u2081 su\u2081 | first-merge (fork t\u2081 u\u2081)\n ... | fork v\u2080 w\u2080 | (fork sv\u2080 sw\u2080 p1 , lpf1 , rpf1) | lastw\u2080\n | fork v\u2081 w\u2081 | (fork sv\u2081 sw\u2081 p2 , lpf2 , rpf2) | firstv\u2081\n with merge (fork w\u2080 v\u2081) | merge-spec\u2032 sw\u2080 sv\u2081 | first-merge (fork w\u2080 v\u2081) | last-merge (fork w\u2080 v\u2081)\n ... | fork a b | (fork sa sb p3 , lpf3 , rpf3) | firsta | lastb\n = fork (fork sv\u2080 sa pf1) (fork sb sw\u2081 pf2) p3 , lpf4 , rpf4\n where\n pf1 : last v\u2080 \u2264\u1d2c first a\n pf1 rewrite firsta | firstv\u2081 = \u2264-< p1 , \u2264-< \u2264\u1d2c.trans (\u2264-\u2293\u2080 lpf1) ht\u2080\u2264lt\u2081 , \u2264\u1d2c.trans (\u2264-\u2293\u2081 lpf1) lu\u2080\u2264hu\u2081 > >\n pf2 : last b \u2264\u1d2c first w\u2081\n pf2 rewrite lastb | lastw\u2080 = \u2264-[ \u2264-[ \u2264\u1d2c.trans ht\u2080\u2264lt\u2081 (\u2264-\u2294\u2080 rpf2) , \u2264\u1d2c.trans lu\u2080\u2264hu\u2081 (\u2264-\u2294\u2081 rpf2) ] , p2 ]\n lpf4 = \u2264-< \u2264\u1d2c.trans (\u2264-\u2293\u2081 lpf3) (\u2264-\u2293\u2080 lpf2) , \u2264\u1d2c.trans (\u2264-\u2293\u2081 lpf3) (\u2264-\u2293\u2081 lpf2) >\n rpf4 = \u2264-[ \u2264\u1d2c.trans (\u2264-\u2294\u2080 rpf1) (\u2264-\u2294\u2080 rpf3) , \u2264\u1d2c.trans (\u2264-\u2294\u2081 rpf1) (\u2264-\u2294\u2080 rpf3) ]\n\n merge-spec : \u2200 {n} {t : Tree A (1 + n)} \u2192 PreSorted t \u2192 SD.Sorted (merge t)\n merge-spec {t = fork t u} (st , su) = proj\u2081 (merge-spec\u2032 st su)\n\n sort-spec : \u2200 {n} (t : Tree A n) \u2192 SD.Sorted (sort t)\n sort-spec (leaf _) = leaf\n sort-spec (fork t u) = merge-spec (sort-spec t , sort-spec u)\n\n {-\nmodule M {n} where\n postulate\n _\u2264_ : {-\u2200 {n} \u2192-} Bits n \u2192 Bits n \u2192 Set\n _\u2294_ : Bits n \u2192 Bits n \u2192 Bits n\n _\u2293_ : Bits n \u2192 Bits n \u2192 Bits n\n isPreorder : IsPreorder _\u2261_ _\u2264_\n \u2264-\u2294 : \u2200 x y \u2192 x \u2264 (y \u2294 x)\n \u2293-\u2264 : \u2200 x y \u2192 (x \u2293 y) \u2264 y\n \u2294-spec : \u2200 {x y} \u2192 x \u2264 y \u2192 x \u2294 y \u2261 y\n \u2293-spec : \u2200 {x y} \u2192 x \u2264 y \u2192 x \u2293 y \u2261 x\n \u2293-comm : Commutative _\u2261_ _\u2293_\n \u2294-comm : Commutative _\u2261_ _\u2294_\n \u2264-<_,_> : \u2200 {x y z} \u2192 x \u2264 y \u2192 x \u2264 z \u2192 x \u2264 (y \u2293 z)\n \u2264-[_,_] : \u2200 {x y z} \u2192 x \u2264 z \u2192 y \u2264 z \u2192 (x \u2294 y) \u2264 z\n \u2264-\u2293\u2080 : \u2200 {x y z} \u2192 x \u2264 (y \u2293 z) \u2192 x \u2264 y\n \u2264-\u2293\u2081 : \u2200 {x y z} \u2192 x \u2264 (y \u2293 z) \u2192 x \u2264 z\n \u2264-\u2294\u2080 : \u2200 {x y z} \u2192 (x \u2294 y) \u2264 z \u2192 x \u2264 z\n \u2264-\u2294\u2081 : \u2200 {x y z} \u2192 (x \u2294 y) \u2264 z \u2192 y \u2264 z\n\nmodule BitsSorting {m} where\n open M {m}\n\n module S = Sorting _\u2293_ _\u2294_\n module SDP = SortingDataIxProperties _\u2264_ _\u2293_ _\u2294_ isPreorder \u2294-spec \u2293-spec \u2293-comm \u2294-comm\n module SP = SortingProperties _\u2264_ _\u2293_ _\u2294_ isPreorder \u2264-\u2294 \u2293-\u2264 \u2264-<_,_> \u2264-[_,_] \u2264-\u2293\u2080 \u2264-\u2293\u2081 \u2264-\u2294\u2080 \u2264-\u2294\u2081\n open SortedData _\u2264_\n\n merge : \u2200 {n} \u2192 Tree (Bits m) (1 + n) \u2192 Tree (Bits m) (1 + n)\n merge = S.merge\n\n sort : \u2200 {n} \u2192 Tree (Bits m) n \u2192 Tree (Bits m) n\n sort = S.sort\n\n merge-spec : \u2200 {n} {t : Tree (Bits m) (1 + n)} \u2192 PreSorted t \u2192 Sorted (merge t)\n merge-spec = SP.merge-spec\n\n sort-spec : \u2200 {n} (t : Tree (Bits m) n) \u2192 Sorted (sort t)\n sort-spec = SP.sort-spec\n\nmodule BitsSorting\u2032 where\n open BitsSorting\n open AllBits\n -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"module prefect-bintree where\n\nimport Level as L\nopen import Function.NP\nimport Data.Nat.NP as Nat\nopen Nat using (\u2115; zero; suc; 2^_; _+_; module \u2115\u00b0)\nopen import Data.Bool\nopen import Data.Sum\nopen import Data.Bits\nopen import Data.Product using (_\u00d7_; _,_; proj\u2081; proj\u2082; \u2203)\nopen import Data.Vec.NP using (Vec; _++_; module Alternative-Reverse)\nopen import Relation.Nullary\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Algebra.FunctionProperties\n\ndata Tree {a} (A : Set a) : \u2115 \u2192 Set a where\n leaf : (x : A) \u2192 Tree A zero\n fork : \u2200 {n} (left right : Tree A n) \u2192 Tree A (suc n)\n\nfromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Tree A n\nfromFun {zero} f = leaf (f [])\nfromFun {suc n} f = fork (fromFun (f \u2218 0\u2237_)) (fromFun (f \u2218 1\u2237_))\n\ntoFun : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Bits n \u2192 A\ntoFun (leaf x) _ = x\ntoFun (fork left right) (b \u2237 bs) = toFun (if b then right else left) bs\n\ntoFun\u2218fromFun : \u2200 {n a} {A : Set a} (f : Bits n \u2192 A) \u2192 toFun (fromFun f) \u2257 f\ntoFun\u2218fromFun {zero} f [] = refl\ntoFun\u2218fromFun {suc n} f (false \u2237 xs)\n rewrite toFun\u2218fromFun (f \u2218 0\u2237_) xs = refl\ntoFun\u2218fromFun {suc n} f (true \u2237 xs)\n rewrite toFun\u2218fromFun (f \u2218 1\u2237_) xs = refl\n\nfromFun\u2218toFun : \u2200 {n a} {A : Set a} (t : Tree A n) \u2192 fromFun (toFun t) \u2261 t\nfromFun\u2218toFun (leaf x) = refl\nfromFun\u2218toFun (fork t\u2080 t\u2081)\n rewrite fromFun\u2218toFun t\u2080\n | fromFun\u2218toFun t\u2081 = refl\n\ntoFun\u2192fromFun : \u2200 {n a} {A : Set a} (t : Tree A n) (f : Bits n \u2192 A) \u2192 toFun t \u2257 f \u2192 t \u2261 fromFun f\ntoFun\u2192fromFun (leaf x) f t\u2257f = cong leaf (t\u2257f [])\ntoFun\u2192fromFun (fork t\u2080 t\u2081) f t\u2257f\n rewrite toFun\u2192fromFun t\u2080 (f \u2218 0\u2237_) (t\u2257f \u2218 0\u2237_)\n | toFun\u2192fromFun t\u2081 (f \u2218 1\u2237_) (t\u2257f \u2218 1\u2237_) = refl\n\nfromFun\u2192toFun : \u2200 {n a} {A : Set a} (t : Tree A n) (f : Bits n \u2192 A) \u2192 t \u2261 fromFun f \u2192 toFun t \u2257 f\nfromFun\u2192toFun ._ _ refl = toFun\u2218fromFun _\n\nlookup : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Tree A n \u2192 A\nlookup = flip toFun\n\n{-\nmodule Fold {a b i} {I : Set i} (ze : I) (su : I \u2192 I)\n {A : Set a} {B : I \u2192 Set b}\n (f : A \u2192 B ze) (_\u00b7_ : \u2200 {n} \u2192 B n \u2192 B n \u2192 B (su n)) where\n\n `_ : \u2115 \u2192 I\n `_ = Nat.fold ze su\n\n fold : \u2200 {n} \u2192 Tree A n \u2192 B(` n)\n fold (leaf x) = f x\n fold (fork t\u2080 t\u2081) = fold t\u2080 \u00b7 fold t\u2081\n\nfold : \u2200 {n a} {A : Set a} (op : A \u2192 A \u2192 A) \u2192 Tree A n \u2192 A\nfold {A = A} op = Fold.fold 0 suc {B = const A} id op\n\nsearch\u2261fold\u2218fromFun : \u2200 {n a} {A : Set a} op (f : Bits n \u2192 A) \u2192 search op f \u2261 fold op (fromFun f)\nsearch\u2261fold\u2218fromFun {zero} op f = refl\nsearch\u2261fold\u2218fromFun {suc n} op f\n rewrite search\u2261fold\u2218fromFun op (f \u2218 0\u2237_)\n | search\u2261fold\u2218fromFun op (f \u2218 1\u2237_) = refl\n\n-- Returns the flat vector of leaves underlying the perfect binary tree.\ntoVec : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Vec A (2^ n)\ntoVec (leaf x) = x \u2237 []\ntoVec (fork t\u2080 t\u2081) = toVec t\u2080 ++ toVec t\u2081\n\nlookup' : \u2200 {m n a} {A : Set a} \u2192 Bits m \u2192 Tree A (m + n) \u2192 Tree A n\nlookup' [] t = t\nlookup' (b \u2237 bs) (fork t t\u2081) = lookup' bs (if b then t\u2081 else t)\n\n\nupdate' : \u2200 {m n a} {A : Set a} \u2192 Bits m \u2192 Tree A n \u2192 Tree A (m + n) \u2192 Tree A (m + n)\nupdate' [] val tree = val\nupdate' (b \u2237 key) val (fork tree tree\u2081) = if b then fork tree (update' key val tree\u2081)\n else fork (update' key val tree) tree\u2081\n\nmap : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 Tree A n \u2192 Tree B n\nmap f (leaf x) = leaf (f x)\nmap f (fork t\u2080 t\u2081) = fork (map f t\u2080) (map f t\u2081)\n\nopen import Relation.Binary\nopen import Data.Star using (Star; \u03b5; _\u25c5_)\n\ndata Swp {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n left : \u2200 {n} {left\u2080 left\u2081 right : Tree A n} \u2192\n Swp left\u2080 left\u2081 \u2192\n Swp (fork left\u2080 right) (fork left\u2081 right)\n right : \u2200 {n} {left right\u2080 right\u2081 : Tree A n} \u2192\n Swp right\u2080 right\u2081 \u2192\n Swp (fork left right\u2080) (fork left right\u2081)\n swp\u2081 : \u2200 {n} {left right : Tree A n} \u2192\n Swp (fork left right) (fork right left)\n swp\u2082 : \u2200 {n} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Swp (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2081\u2081 t\u2080\u2081) (fork t\u2081\u2080 t\u2080\u2080))\n\nSwp\u2605 : \u2200 {n a} {A : Set a} (left right : Tree A n) \u2192 Set a\nSwp\u2605 = Star Swp\n\nSwp-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Swp {A = A} {n})\nSwp-sym (left s) = left (Swp-sym s)\nSwp-sym (right s) = right (Swp-sym s)\nSwp-sym swp\u2081 = swp\u2081\nSwp-sym swp\u2082 = swp\u2082\n\ndata Rot {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n leaf : \u2200 x \u2192 Rot (leaf x) (leaf x)\n fork : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 left\u2081 \u2192\n Rot right\u2080 right\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n krof : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 right\u2081 \u2192\n Rot right\u2080 left\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n\nRot-refl : \u2200 {n a} {A : Set a} \u2192 Reflexive (Rot {A = A} {n})\nRot-refl {x = leaf x} = leaf x\nRot-refl {x = fork _ _} = fork Rot-refl Rot-refl\n\nRot-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Rot {A = A} {n})\nRot-sym (leaf x) = leaf x\nRot-sym (fork p\u2080 p\u2081) = fork (Rot-sym p\u2080) (Rot-sym p\u2081)\nRot-sym (krof p\u2080 p\u2081) = krof (Rot-sym p\u2081) (Rot-sym p\u2080)\n\nRot-trans : \u2200 {n a} {A : Set a} \u2192 Transitive (Rot {A = A} {n})\nRot-trans (leaf x) q = q\nRot-trans (fork p\u2080 p\u2081) (fork q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (fork p\u2080 p\u2081) (krof q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (krof p\u2080 p\u2081) (fork q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\nRot-trans (krof p\u2080 p\u2081) (krof q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\n\ndata SwpOp : \u2115 \u2192 Set where\n \u03b5 : \u2200 {n} \u2192 SwpOp n\n\n _\u204f_ : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp n \u2192 SwpOp n\n\n first : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp (suc n)\n\n swp : \u2200 {n} \u2192 SwpOp (suc n)\n\n swp-seconds : \u2200 {n} \u2192 SwpOp (2 + n)\n\ndata Perm {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n \u03b5 : \u2200 {n} {t : Tree A n} \u2192 Perm t t\n\n _\u204f_ : \u2200 {n} {t u v : Tree A n} \u2192 Perm t u \u2192 Perm u v \u2192 Perm t v\n\n first : \u2200 {n} {tA tB tC : Tree A n} \u2192\n Perm tA tB \u2192\n Perm (fork tA tC) (fork tB tC)\n\n swp : \u2200 {n} {tA tB : Tree A n} \u2192\n Perm (fork tA tB) (fork tB tA)\n\n swp-seconds : \u2200 {n} {tA tB tC tD : Tree A n} \u2192\n Perm (fork (fork tA tB) (fork tC tD))\n (fork (fork tA tD) (fork tC tB))\n\ndata Perm0\u2194 {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n \u03b5 : \u2200 {n} {t : Tree A n} \u2192 Perm0\u2194 t t\n\n swp : \u2200 {n} {tA tB : Tree A n} \u2192\n Perm0\u2194 (fork tA tB) (fork tB tA)\n\n first : \u2200 {n} {tA tB tC : Tree A n} \u2192\n Perm0\u2194 tA tB \u2192\n Perm0\u2194 (fork tA tC) (fork tB tC)\n\n firsts : \u2200 {n} {tA tB tC tD tE tF : Tree A n} \u2192\n Perm0\u2194 (fork tA tC) (fork tE tF) \u2192\n Perm0\u2194 (fork (fork tA tB) (fork tC tD))\n (fork (fork tE tB) (fork tF tD))\n\n extremes : \u2200 {n} {tA tB tC tD tE tF : Tree A n} \u2192\n Perm0\u2194 (fork tA tD) (fork tE tF) \u2192\n Perm0\u2194 (fork (fork tA tB) (fork tC tD))\n (fork (fork tE tB) (fork tC tF))\n\n-- Star Perm0\u2194 can then model any permutation\n\ninfixr 1 _\u204f_\n\nsecond-perm : \u2200 {a} {A : Set a} {n} {left right\u2080 right\u2081 : Tree A n} \u2192\n Perm right\u2080 right\u2081 \u2192\n Perm (fork left right\u2080) (fork left right\u2081)\nsecond-perm f = swp \u204f first f \u204f swp\n\nsecond-swpop : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp (suc n)\nsecond-swpop f = swp \u204f first f \u204f swp\n\n<_\u00d7_>-perm : \u2200 {a} {A : Set a} {n} {left\u2080 right\u2080 left\u2081 right\u2081 : Tree A n} \u2192\n Perm left\u2080 left\u2081 \u2192\n Perm right\u2080 right\u2081 \u2192\n Perm (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n< f \u00d7 g >-perm = first f \u204f second-perm g\n\nswp\u2082-perm : \u2200 {a n} {A : Set a} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Perm (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2081\u2081 t\u2080\u2081) (fork t\u2081\u2080 t\u2080\u2080))\nswp\u2082-perm = first swp \u204f swp-seconds \u204f first swp\n\nswp\u2083-perm : \u2200 {a n} {A : Set a} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Perm (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2080\u2080 t\u2081\u2080) (fork t\u2080\u2081 t\u2081\u2081))\nswp\u2083-perm = second-perm swp \u204f swp-seconds \u204f second-perm swp\n\nswp-firsts-perm : \u2200 {n a} {A : Set a} {tA tB tC tD : Tree A n} \u2192\n Perm (fork (fork tA tB) (fork tC tD))\n (fork (fork tC tB) (fork tA tD))\nswp-firsts-perm = < swp \u00d7 swp >-perm \u204f swp-seconds \u204f < swp \u00d7 swp >-perm\n\nSwp\u21d2Perm : \u2200 {n a} {A : Set a} \u2192 Swp {a} {A} {n} \u21d2 Perm {n = n}\nSwp\u21d2Perm (left pf) = first (Swp\u21d2Perm pf)\nSwp\u21d2Perm (right pf) = second-perm (Swp\u21d2Perm pf)\nSwp\u21d2Perm swp\u2081 = swp\nSwp\u21d2Perm swp\u2082 = swp\u2082-perm\n\nSwp\u2605\u21d2Perm : \u2200 {n a} {A : Set a} \u2192 Swp\u2605 {n} {a} {A} \u21d2 Perm {n = n}\nSwp\u2605\u21d2Perm \u03b5 = \u03b5\nSwp\u2605\u21d2Perm (x \u25c5 xs) = Swp\u21d2Perm x \u204f Swp\u2605\u21d2Perm xs\n\nswp-inners : \u2200 {n} \u2192 SwpOp (2 + n)\nswp-inners = second-swpop swp \u204f swp-seconds \u204f second-swpop swp\n\non-extremes : \u2200 {n} \u2192 SwpOp (1 + n) \u2192 SwpOp (2 + n)\non-extremes f = swp-seconds \u204f first f \u204f swp-seconds\n\non-firsts : \u2200 {n} \u2192 SwpOp (1 + n) \u2192 SwpOp (2 + n)\non-firsts f = swp-inners \u204f first f \u204f swp-inners\n\n0\u2194_ : \u2200 {m n} \u2192 Bits m \u2192 SwpOp (m + n)\n0\u2194 [] = \u03b5\n0\u2194 (false{-0-} \u2237 p) = first (0\u2194 p)\n0\u2194 (true{-1-} \u2237 []) = swp\n0\u2194 (true{-1-} \u2237 true {-1-} \u2237 p) = on-extremes (0\u2194 (1b \u2237 p))\n0\u2194 (true{-1-} \u2237 false{-0-} \u2237 p) = on-firsts (0\u2194 (1b \u2237 p))\n\ncommSwpOp : \u2200 m n \u2192 SwpOp (m + n) \u2192 SwpOp (n + m)\ncommSwpOp m n x rewrite \u2115\u00b0.+-comm m n = x\n\n[_\u2194_] : \u2200 {m n} (p q : Bits m) \u2192 SwpOp (m + n)\n[ p \u2194 q ] = 0\u2194 p \u204f 0\u2194 q \u204f 0\u2194 p\n\n[_\u2194\u2032_] : \u2200 {n} (p q : Bits n) \u2192 SwpOp n\n[ p \u2194\u2032 q ] = commSwpOp _ 0 [ p \u2194 q ]\n\n_$swp_ : \u2200 {n a} {A : Set a} \u2192 SwpOp n \u2192 Tree A n \u2192 Tree A n\n\u03b5 $swp t = t\n(f \u204f g) $swp t = g $swp (f $swp t)\n(first f) $swp (fork t\u2080 t\u2081) = fork (f $swp t\u2080) t\u2081\nswp $swp (fork t\u2080 t\u2081) = fork t\u2081 t\u2080\nswp-seconds $swp (fork (fork t\u2080 t\u2081) (fork t\u2082 t\u2083)) = fork (fork t\u2080 t\u2083) (fork t\u2082 t\u2081)\n\nswpRel : \u2200 {n a} {A : Set a} (f : SwpOp n) (t : Tree A n) \u2192 Perm t (f $swp t)\nswpRel \u03b5 _ = \u03b5\nswpRel (f \u204f g) _ = swpRel f _ \u204f swpRel g _\nswpRel (first f) (fork _ _) = first (swpRel f _)\nswpRel swp (fork _ _) = swp\nswpRel swp-seconds\n (fork (fork _ _) (fork _ _)) = swp-seconds\n\n[0\u2194_]-Rel : \u2200 {m n a} {A : Set a} (p : Bits m) (t : Tree A (m + n)) \u2192 Perm t ((0\u2194 p) $swp t)\n[0\u2194 p ]-Rel = swpRel (0\u2194 p)\n\nswpOp' : \u2200 {n a} {A : Set a} {t u : Tree A n} \u2192 Perm0\u2194 t u \u2192 SwpOp n\nswpOp' \u03b5 = \u03b5\nswpOp' (first f) = first (swpOp' f)\nswpOp' swp = swp\nswpOp' (firsts f) = on-firsts (swpOp' f)\nswpOp' (extremes f) = on-extremes (swpOp' f)\n\nswpOp : \u2200 {n a} {A : Set a} {t u : Tree A n} \u2192 Perm t u \u2192 SwpOp n\nswpOp \u03b5 = \u03b5\nswpOp (f \u204f g) = swpOp f \u204f swpOp g\nswpOp (first f) = first (swpOp f)\nswpOp swp = swp\nswpOp swp-seconds = swp-seconds\n\nswpOp-sym : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp n\nswpOp-sym \u03b5 = \u03b5\nswpOp-sym (f \u204f g) = swpOp-sym g \u204f swpOp-sym f\nswpOp-sym (first f) = first (swpOp-sym f)\nswpOp-sym swp = swp\nswpOp-sym swp-seconds = swp-seconds\n\nswpOp-sym-involutive : \u2200 {n} (f : SwpOp n) \u2192 swpOp-sym (swpOp-sym f) \u2261 f\nswpOp-sym-involutive \u03b5 = refl\nswpOp-sym-involutive (f \u204f g) rewrite swpOp-sym-involutive f | swpOp-sym-involutive g = refl\nswpOp-sym-involutive (first f) rewrite swpOp-sym-involutive f = refl\nswpOp-sym-involutive swp = refl\nswpOp-sym-involutive swp-seconds = refl\n\nswpOp-sym-sound : \u2200 {n a} {A : Set a} (f : SwpOp n) (t : Tree A n) \u2192 swpOp-sym f $swp (f $swp t) \u2261 t\nswpOp-sym-sound \u03b5 t = refl\nswpOp-sym-sound (f \u204f g) t rewrite swpOp-sym-sound g (f $swp t) | swpOp-sym-sound f t = refl\nswpOp-sym-sound (first f) (fork t _) rewrite swpOp-sym-sound f t = refl\nswpOp-sym-sound swp (fork _ _) = refl\nswpOp-sym-sound swp-seconds (fork (fork _ _) (fork _ _)) = refl\n\nmodule \u00acswp-comm where\n data X : Set where\n A B C D E F G H : X\n n : \u2115\n n = 3\n t : Tree X n\n t = fork (fork (fork (leaf A) (leaf B))(fork (leaf C) (leaf D))) (fork (fork (leaf E) (leaf F))(fork (leaf G) (leaf H)))\n f : SwpOp n\n f = swp\n g : SwpOp n\n g = first swp\n pf : f $swp (g $swp t) \u2262 g $swp (f $swp t)\n pf ()\n\nswp-leaf : \u2200 {a} {A : Set a} (f : SwpOp 0) (x : A) \u2192 f $swp (leaf x) \u2261 leaf x\nswp-leaf \u03b5 x = refl\nswp-leaf (f \u204f g) x rewrite swp-leaf f x | swp-leaf g x = refl\n\nswpOp-sound : \u2200 {n a} {A : Set a} {t u : Tree A n} (perm : Perm t u) \u2192 (swpOp perm $swp t \u2261 u)\nswpOp-sound \u03b5 = refl\nswpOp-sound (f \u204f f\u2081) rewrite swpOp-sound f | swpOp-sound f\u2081 = refl\nswpOp-sound (first f) rewrite swpOp-sound f = refl\nswpOp-sound swp = refl\nswpOp-sound swp-seconds = refl\n\nopen import Relation.Nullary using (Dec ; yes ; no)\nopen import Relation.Nullary.Negation\n-}\n\nmodule new-approach where\n\n open import Data.Empty\n\n import Function.Inverse as FI\n open FI using (_\u2194_; module Inverse; _InverseOf_)\n open import Function.Related\n import Function.Equality\n import Relation.Binary.PropositionalEquality as P\n\n data _\u2208_ {a}{A : Set a}(x : A) : {n : \u2115} \u2192 Tree A n \u2192 Set a where\n here : x \u2208 leaf x\n left : {n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 t\u2081 \u2192 x \u2208 fork t\u2081 t\u2082\n right : {n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 t\u2082 \u2192 x \u2208 fork t\u2081 t\u2082\n\n toBits : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t : Tree A n} \u2192 x \u2208 t \u2192 Bits n\n toBits here = []\n toBits (left key) = 0b \u2237 toBits key\n toBits (right key) = 1b \u2237 toBits key\n\n \u2208-lookup : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t : Tree A n}(path : x \u2208 t) \u2192 lookup (toBits path) t \u2261 x\n \u2208-lookup here = refl\n \u2208-lookup (left path) = \u2208-lookup path\n \u2208-lookup (right path) = \u2208-lookup path\n\n lookup-\u2208 : \u2200 {a}{A : Set a}{n : \u2115}(key : Bits n)(t : Tree A n) \u2192 lookup key t \u2208 t\n lookup-\u2208 [] (leaf x) = here\n lookup-\u2208 (true \u2237 key) (fork tree tree\u2081) = right (lookup-\u2208 key tree\u2081)\n lookup-\u2208 (false \u2237 key) (fork tree tree\u2081) = left (lookup-\u2208 key tree)\n\n {-\n _\u2248_ : \u2200 {a}{A : Set a}{n : \u2115} \u2192 Tree A n \u2192 Tree A n \u2192 Set _\n t\u2081 \u2248 t\u2082 = \u2200 x \u2192 (x \u2208 t\u2081) \u2194 (x \u2208 t\u2082)\n\n \u2248-refl : {a : _}{A : Set a}{n : \u2115}{t : Tree A n} \u2192 t \u2248 t\n \u2248-refl _ = FI.id\n\n \u2248-trans : {a : _}{A : Set a}{n : \u2115}{t u v : Tree A n} \u2192 t \u2248 u \u2192 u \u2248 v \u2192 t \u2248 v\n \u2248-trans f g x = g x FI.\u2218 f x\n\n move : \u2200 {a}{A : Set a}{n : \u2115}{t s : Tree A n}{x : A} \u2192 t \u2248 s \u2192 x \u2208 t \u2192 x \u2208 s\n move t\u2248s x\u2208t = Inverse.to (t\u2248s _) Function.Equality.\u27e8$\u27e9 x\u2208t\n\n swap\u2080 : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 fork t\u2081 t\u2082 \u2248 fork t\u2082 t\u2081\n swap\u2080 _ = record\n { to = \u2192-to-\u27f6 swap\n ; from = \u2192-to-\u27f6 swap\n ; inverse-of = record { left-inverse-of = swap-inv\n ; right-inverse-of = swap-inv }\n } where\n swap : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 fork t\u2081 t\u2082 \u2192 x \u2208 fork t\u2082 t\u2081\n swap (left path) = right path\n swap (right path) = left path\n\n swap-inv : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t\u2081 t\u2082 : Tree A n}(p : x \u2208 fork t\u2081 t\u2082) \u2192 swap (swap p) \u2261 p\n swap-inv (left p) = refl\n swap-inv (right p) = refl\n\n swap\u2082 : \u2200 {a}{A : Set a}{n : \u2115}{tA tB tC tD : Tree A n}\n \u2192 fork (fork tA tB) (fork tC tD) \u2248 fork (fork tA tD) (fork tC tB)\n swap\u2082 _ = record\n { to = \u2192-to-\u27f6 fun\n ; from = \u2192-to-\u27f6 fun\n ; inverse-of = record { left-inverse-of = inv\n ; right-inverse-of = inv }\n } where\n fun : \u2200 {a}{A : Set a}{x n}{tA tB tC tD : Tree A n}\n \u2192 x \u2208 fork (fork tA tB) (fork tC tD) \u2192 x \u2208 fork (fork tA tD) (fork tC tB)\n fun (left (left path)) = left (left path)\n fun (left (right path)) = right (right path)\n fun (right (left path)) = right (left path)\n fun (right (right path)) = left (right path)\n\n inv : \u2200 {a}{A : Set a}{x n}{tA tB tC tD : Tree A n}\n \u2192 (p : x \u2208 fork (fork tA tB) (fork tC tD)) \u2192 fun (fun p) \u2261 p\n inv (left (left p)) = refl\n inv (left (right p)) = refl\n inv (right (left p)) = refl\n inv (right (right p)) = refl\n\n _\u27e8fork\u27e9_ : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 s\u2081 s\u2082 : Tree A n} \u2192 t\u2081 \u2248 s\u2081 \u2192 t\u2082 \u2248 s\u2082 \u2192 fork t\u2081 t\u2082 \u2248 fork s\u2081 s\u2082\n (t1\u2248s1 \u27e8fork\u27e9 t2\u2248s2) y = record\n { to = to\n ; from = from\n ; inverse-of = record { left-inverse-of = frk-linv\n ; right-inverse-of = frk-rinv }\n } where\n\n frk : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 s\u2081 s\u2082 : Tree A n}{x : A} \u2192 t\u2081 \u2248 s\u2081 \u2192 t\u2082 \u2248 s\u2082 \u2192 x \u2208 fork t\u2081 t\u2082 \u2192 x \u2208 fork s\u2081 s\u2082\n frk t1\u2248s1 t2\u2248s2 (left x\u2208t1) = left (move t1\u2248s1 x\u2208t1)\n frk t1\u2248s1 t2\u2248s2 (right x\u2208t2) = right (move t2\u2248s2 x\u2208t2)\n\n to = \u2192-to-\u27f6 (frk t1\u2248s1 t2\u2248s2)\n from = \u2192-to-\u27f6 (frk (\u03bb x \u2192 FI.sym (t1\u2248s1 x)) (\u03bb x \u2192 FI.sym (t2\u2248s2 x)))\n\n\n open Function.Equality using (_\u27e8$\u27e9_)\n open import Function.LeftInverse\n\n frk-linv : from LeftInverseOf to\n frk-linv (left x) = cong left (_InverseOf_.left-inverse-of (Inverse.inverse-of (t1\u2248s1 y)) x)\n frk-linv (right x) = cong right (_InverseOf_.left-inverse-of (Inverse.inverse-of (t2\u2248s2 y)) x)\n\n frk-rinv : from RightInverseOf to -- \u2200 x \u2192 to \u27e8$\u27e9 (from \u27e8$\u27e9 x) \u2261 x\n frk-rinv (left x) = cong left (_InverseOf_.right-inverse-of (Inverse.inverse-of (t1\u2248s1 y)) x)\n frk-rinv (right x) = cong right (_InverseOf_.right-inverse-of (Inverse.inverse-of (t2\u2248s2 y)) x)\n\n \u2248-first : \u2200 {a}{A : Set a}{n : \u2115}{t u v : Tree A n} \u2192 t \u2248 u \u2192 fork t v \u2248 fork u v\n \u2248-first f = f \u27e8fork\u27e9 \u2248-refl\n\n \u2248-second : \u2200 {a}{A : Set a}{n : \u2115}{t u v : Tree A n} \u2192 t \u2248 u \u2192 fork v t \u2248 fork v u\n \u2248-second f = \u2248-refl \u27e8fork\u27e9 f\n\n swap-inner : \u2200 {a}{A : Set a}{n : \u2115}{tA tB tC tD : Tree A n}\n \u2192 fork (fork tA tB) (fork tC tD) \u2248 fork (fork tA tC) (fork tB tD)\n swap-inner = \u2248-trans (\u2248-second swap\u2080) (\u2248-trans swap\u2082 (\u2248-second swap\u2080))\n\n Rot\u27f6\u2248 : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 Rot t\u2081 t\u2082 \u2192 t\u2081 \u2248 t\u2082\n Rot\u27f6\u2248 (leaf x) = \u2248-refl\n Rot\u27f6\u2248 (fork rot rot\u2081) = Rot\u27f6\u2248 rot \u27e8fork\u27e9 Rot\u27f6\u2248 rot\u2081\n Rot\u27f6\u2248 (krof {_} {l} {l'} {r} {r'} rot rot\u2081) = \u03bb y \u2192\n y \u2208 fork l r \u2194\u27e8 (Rot\u27f6\u2248 rot \u27e8fork\u27e9 Rot\u27f6\u2248 rot\u2081) y \u27e9\n y \u2208 fork r' l' \u2194\u27e8 swap\u2080 y \u27e9\n y \u2208 fork l' r' \u220e\n where open EquationalReasoning\n\n Perm\u27f6\u2248 : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 Perm t\u2081 t\u2082 \u2192 t\u2081 \u2248 t\u2082\n Perm\u27f6\u2248 \u03b5 = \u2248-refl\n Perm\u27f6\u2248 (f \u204f g) = \u2248-trans (Perm\u27f6\u2248 f) (Perm\u27f6\u2248 g)\n Perm\u27f6\u2248 (first f) = \u2248-first (Perm\u27f6\u2248 f)\n Perm\u27f6\u2248 swp = swap\u2080\n Perm\u27f6\u2248 swp-seconds = swap\u2082\n\n Perm0\u2194\u27f6\u2248 : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 Perm0\u2194 t\u2081 t\u2082 \u2192 t\u2081 \u2248 t\u2082\n Perm0\u2194\u27f6\u2248 \u03b5 = \u2248-refl\n Perm0\u2194\u27f6\u2248 swp = swap\u2080\n Perm0\u2194\u27f6\u2248 (first t) = \u2248-first (Perm0\u2194\u27f6\u2248 t)\n Perm0\u2194\u27f6\u2248 (firsts t) = \u2248-trans swap-inner (\u2248-trans (\u2248-first (Perm0\u2194\u27f6\u2248 t)) swap-inner)\n Perm0\u2194\u27f6\u2248 (extremes t) = \u2248-trans swap\u2082 (\u2248-trans (\u2248-first (Perm0\u2194\u27f6\u2248 t)) swap\u2082)\n\n put : {a : _}{A : Set a}{n : \u2115} \u2192 Bits n \u2192 A \u2192 Tree A n \u2192 Tree A n\n put [] val tree = leaf val\n put (x \u2237 key) val (fork tree tree\u2081) = if x then fork tree (put key val tree\u2081)\n else fork (put key val tree) tree\u2081\n\n -- move-me\n _\u2237\u2262_ : {n : \u2115}{xs ys : Bits n}(x : Bit) \u2192 x \u2237 xs \u2262 x \u2237 ys \u2192 xs \u2262 ys\n _\u2237\u2262_ x = contraposition $ cong $ _\u2237_ x\n\n \u2208-put : {a : _}{A : Set a}{n : \u2115}(p : Bits n){x : A}(t : Tree A n) \u2192 x \u2208 put p x t\n \u2208-put [] t = here\n \u2208-put (true \u2237 p) (fork t t\u2081) = right (\u2208-put p t\u2081)\n \u2208-put (false \u2237 p) (fork t t\u2081) = left (\u2208-put p t)\n\n \u2208-put-\u2262 : {a : _}{A : Set a}{n : \u2115}(p : Bits n){x y : A}{t : Tree A n}(path : x \u2208 t)\n \u2192 p \u2262 toBits path \u2192 x \u2208 put p y t\n \u2208-put-\u2262 [] here neg = \u22a5-elim (neg refl)\n \u2208-put-\u2262 (true \u2237 p) (left path) neg = left path\n \u2208-put-\u2262 (false \u2237 p) (left path) neg = left (\u2208-put-\u2262 p path (false \u2237\u2262 neg))\n \u2208-put-\u2262 (true \u2237 p) (right path) neg = right (\u2208-put-\u2262 p path (true \u2237\u2262 neg))\n \u2208-put-\u2262 (false \u2237 p) (right path) neg = right path\n\n swap : {a : _}{A : Set a}{n : \u2115} \u2192 (p\u2081 p\u2082 : Bits n) \u2192 Tree A n \u2192 Tree A n\n swap p\u2081 p\u2082 t = put p\u2081 a\u2082 (put p\u2082 a\u2081 t)\n where\n a\u2081 = lookup p\u2081 t\n a\u2082 = lookup p\u2082 t\n\n swap-perm\u2081 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p : x \u2208 t) \u2192 t \u2248 swap (toBits p) (toBits p) t\n swap-perm\u2081 here = \u2248-refl\n swap-perm\u2081 (left path) = \u2248-first (swap-perm\u2081 path)\n swap-perm\u2081 (right path) = \u2248-second (swap-perm\u2081 path)\n\n swap-comm : {a : _}{A : Set a}{n : \u2115} (p\u2081 p\u2082 : Bits n)(t : Tree A n) \u2192 swap p\u2082 p\u2081 t \u2261 swap p\u2081 p\u2082 t\n swap-comm [] [] (leaf x) = refl\n swap-comm (true \u2237 p\u2081) (true \u2237 p\u2082) (fork t t\u2081) = cong (fork t) (swap-comm p\u2081 p\u2082 t\u2081)\n swap-comm (true \u2237 p\u2081) (false \u2237 p\u2082) (fork t t\u2081) = refl\n swap-comm (false \u2237 p\u2081) (true \u2237 p\u2082) (fork t t\u2081) = refl\n swap-comm (false \u2237 p\u2081) (false \u2237 p\u2082) (fork t t\u2081) = cong (flip fork t\u2081) (swap-comm p\u2081 p\u2082 t)\n\n swap-perm\u2082 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p' : Bits n)(p : x \u2208 t)\n \u2192 x \u2208 swap (toBits p) p' t\n swap-perm\u2082 _ here = here\n swap-perm\u2082 (true \u2237 p) (left path) rewrite \u2208-lookup path = right (\u2208-put p _)\n swap-perm\u2082 (false \u2237 p) (left path) = left (swap-perm\u2082 p path)\n swap-perm\u2082 (true \u2237 p) (right path) = right (swap-perm\u2082 p path)\n swap-perm\u2082 (false \u2237 p) (right path) rewrite \u2208-lookup path = left (\u2208-put p _)\n\n swap-perm\u2083 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p\u2081 p\u2082 : Bits n)(p : x \u2208 t)\n \u2192 p\u2081 \u2262 toBits p \u2192 p\u2082 \u2262 toBits p \u2192 x \u2208 swap p\u2081 p\u2082 t\n swap-perm\u2083 [] [] here neg\u2081 neg\u2082 = here\n swap-perm\u2083 (true \u2237 p\u2081) (true \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left path\n swap-perm\u2083 (true \u2237 p\u2081) (false \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left (\u2208-put-\u2262 _ path (false \u2237\u2262 neg\u2082))\n swap-perm\u2083 (false \u2237 p\u2081) (true \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left (\u2208-put-\u2262 _ path (false \u2237\u2262 neg\u2081))\n swap-perm\u2083 (false \u2237 p\u2081) (false \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left\n (swap-perm\u2083 p\u2081 p\u2082 path (false \u2237\u2262 neg\u2081) (false \u2237\u2262 neg\u2082))\n swap-perm\u2083 (true \u2237 p\u2081) (true \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right\n (swap-perm\u2083 p\u2081 p\u2082 path (true \u2237\u2262 neg\u2081) (true \u2237\u2262 neg\u2082))\n swap-perm\u2083 (true \u2237 p\u2081) (false \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right (\u2208-put-\u2262 _ path (true \u2237\u2262 neg\u2081))\n swap-perm\u2083 (false \u2237 p\u2081) (true \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right (\u2208-put-\u2262 _ path (true \u2237\u2262 neg\u2082))\n swap-perm\u2083 (false \u2237 p\u2081) (false \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right path\n\n \u2208-swp : \u2200 {n a} {A : Set a} (f : SwpOp n) {x : A} {t : Tree A n} \u2192 x \u2208 t \u2192 x \u2208 (f $swp t)\n \u2208-swp \u03b5 pf = pf\n \u2208-swp (f \u204f g) pf = \u2208-swp g (\u2208-swp f pf)\n \u2208-swp (first f) {t = fork _ _} (left pf) = left (\u2208-swp f pf)\n \u2208-swp (first f) {t = fork _ _} (right pf) = right pf\n \u2208-swp swp {t = fork t u} (left pf) = right pf\n \u2208-swp swp {t = fork t u} (right pf) = left pf\n \u2208-swp swp-seconds {t = fork (fork _ _) (fork _ _)} (left (left pf)) = left (left pf)\n \u2208-swp swp-seconds {t = fork (fork _ _) (fork _ _)} (left (right pf)) = right (right pf)\n \u2208-swp swp-seconds {t = fork (fork _ _) (fork _ _)} (right (left pf)) = right (left pf)\n \u2208-swp swp-seconds {t = fork (fork _ _) (fork _ _)} (right (right pf)) = left (right pf)\n\nmodule FoldProp {a} {A : Set a} (_\u00b7_ : Op\u2082 A) (op-comm : Commutative _\u2261_ _\u00b7_) (op-assoc : Associative _\u2261_ _\u00b7_) where\n\n \u27ea_\u27eb : \u2200 {n} \u2192 Tree A n \u2192 A\n \u27ea_\u27eb = fold _\u00b7_\n\n _=[fold]\u21d2\u2032_ : \u2200 {\u2113\u2081 \u2113\u2082} \u2192 (\u2200 {m n} \u2192 REL (Tree A m) (Tree A n) \u2113\u2081) \u2192 Rel A \u2113\u2082 \u2192 Set _\n -- _\u223c\u2080_ =[fold]\u21d2 _\u223c\u2081_ = \u2200 {m n} \u2192 _\u223c\u2080_ {m} {n} =[ fold {n} _\u00b7_ ]\u21d2 _\u223c\u2081_\n _\u223c\u2080_ =[fold]\u21d2\u2032 _\u223c\u2081_ = \u2200 {m n} {t : Tree A m} {u : Tree A n} \u2192 t \u223c\u2080 u \u2192 \u27ea t \u27eb \u223c\u2081 \u27ea u \u27eb\n\n _=[fold]\u21d2_ : \u2200 {\u2113\u2081 \u2113\u2082} \u2192 (\u2200 {n} \u2192 Rel (Tree A n) \u2113\u2081) \u2192 Rel A \u2113\u2082 \u2192 Set _\n _\u223c\u2080_ =[fold]\u21d2 _\u223c\u2081_ = \u2200 {n} \u2192 _\u223c\u2080_ =[ fold {n} _\u00b7_ ]\u21d2 _\u223c\u2081_\n\n fold-rot : Rot =[fold]\u21d2 _\u2261_\n fold-rot (leaf x) = refl\n fold-rot (fork rot rot\u2081) = cong\u2082 _\u00b7_ (fold-rot rot) (fold-rot rot\u2081)\n fold-rot (krof rot rot\u2081) rewrite fold-rot rot | fold-rot rot\u2081 = op-comm _ _\n\n -- t \u223c u \u2192 fork v t \u223c fork u w\n\n lem : \u2200 x y z t \u2192 (x \u00b7 y) \u00b7 (z \u00b7 t) \u2261 (t \u00b7 y) \u00b7 (z \u00b7 x)\n lem x y z t = (x \u00b7 y) \u00b7 (z \u00b7 t)\n \u2261\u27e8 op-assoc x y _ \u27e9\n x \u00b7 (y \u00b7 (z \u00b7 t))\n \u2261\u27e8 op-comm x _ \u27e9\n (y \u00b7 (z \u00b7 t)) \u00b7 x\n \u2261\u27e8 op-assoc y (z \u00b7 t) _ \u27e9\n y \u00b7 ((z \u00b7 t) \u00b7 x)\n \u2261\u27e8 cong (\u03bb u \u2192 y \u00b7 (u \u00b7 x)) (op-comm z t) \u27e9\n y \u00b7 ((t \u00b7 z) \u00b7 x)\n \u2261\u27e8 cong (_\u00b7_ y) (op-assoc t z x) \u27e9\n y \u00b7 (t \u00b7 (z \u00b7 x))\n \u2261\u27e8 sym (op-assoc y t _) \u27e9\n (y \u00b7 t) \u00b7 (z \u00b7 x)\n \u2261\u27e8 cong (\u03bb u \u2192 u \u00b7 (z \u00b7 x)) (op-comm y t) \u27e9\n (t \u00b7 y) \u00b7 (z \u00b7 x)\n \u220e\n where open \u2261-Reasoning\n\n fold-swp : Swp =[fold]\u21d2 _\u2261_\n fold-swp (left pf) rewrite fold-swp pf = refl\n fold-swp (right pf) rewrite fold-swp pf = refl\n fold-swp swp\u2081 = op-comm _ _\n fold-swp (swp\u2082 {_} {t\u2080\u2080} {t\u2080\u2081} {t\u2081\u2080} {t\u2081\u2081}) = lem \u27ea t\u2080\u2080 \u27eb \u27ea t\u2080\u2081 \u27eb \u27ea t\u2081\u2080 \u27eb \u27ea t\u2081\u2081 \u27eb\n\n fold-swp\u2605 : Swp\u2605 =[fold]\u21d2 _\u2261_\n fold-swp\u2605 \u03b5 = refl\n fold-swp\u2605 (x \u25c5 xs) rewrite fold-swp x | fold-swp\u2605 xs = refl\n -}\n\nmodule All {a} (A : Set a) where\n\n All : \u2200 {n} \u2192 (Bits n \u2192 A \u2192 Set) \u2192 Tree A n \u2192 Set\n All f (leaf x) = f [] x\n All f (fork t\u2080 t\u2081) = All (f \u2218 0\u2237_) t\u2080 \u00d7 All (f \u2218 1\u2237_) t\u2081\n\n Any : \u2200 {n} \u2192 (Bits n \u2192 A \u2192 Set) \u2192 Tree A n \u2192 Set\n Any f (leaf x) = f [] x\n Any f (fork t\u2080 t\u2081) = Any (f \u2218 0\u2237_) t\u2080 \u228e Any (f \u2218 1\u2237_) t\u2081\n\nopen Alternative-Reverse\n\nmodule AllBits where\n module M {m} = All (Bits m)\n open M\n\n _IsRevPrefixOf_ : \u2200 {m n} \u2192 Bits m \u2192 Bits (rev-+ m n) \u2192 Set\n _IsRevPrefixOf_ {m} {n} p xs = \u2203 \u03bb (ys : Bits n) \u2192 rev-app p ys \u2261 xs\n\n RevPrefix : \u2200 {m n o} (p : Bits m) \u2192 Tree (Bits (rev-+ m n)) o \u2192 Set\n RevPrefix p = All (\u03bb _ \u2192 _IsRevPrefixOf_ p)\n\n RevPrefix-[]-\u22a4 : \u2200 {m n} (t : Tree (Bits m) n) \u2192 RevPrefix [] t\n RevPrefix-[]-\u22a4 (leaf x) = x , refl\n RevPrefix-[]-\u22a4 (fork t u) = RevPrefix-[]-\u22a4 t , RevPrefix-[]-\u22a4 u\n\n All-fromFun : \u2200 {m} n (p : Bits m) \u2192 All (_\u2261_ \u2218 rev-app p) (fromFun {n} (rev-app p))\n All-fromFun zero p = refl\n All-fromFun (suc n) p = All-fromFun n (0\u2237 p) , All-fromFun n (1\u2237 p)\n\n All-id : \u2200 n \u2192 All {n} _\u2261_ (fromFun id)\n All-id n = All-fromFun n []\n\nopen new-approach\n\n{-\n rev-app : \u2200 {a} {A : Set a} {m n} \u2192\n Vec A n \u2192 Vec A m \u2192 Vec A (rev-+ n m)\n -}\n\nbar : \u2200 {m n x} (f : Bits m \u2192 Bits n) (p : x \u2208 fromFun f) \u2192 f (toBits p) \u2261 x\nbar f here = refl\nbar f (left p) = bar (f \u2218 0\u2237_) p\nbar f (right p) = bar (f \u2218 1\u2237_) p\n\nfoo : \u2200 {m} n {x : Bits (rev-+ m n)} (q : Bits m) (p : x \u2208 fromFun (rev-app q)) \u2192 rev-app q (toBits p) \u2261 x\nfoo _ = bar \u2218 rev-app\n\nfirst : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 A\nfirst (leaf x) = x\nfirst (fork t _) = first t\n\nlast : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 A\nlast (leaf x) = x\nlast (fork _ t) = last t\n\nmodule SortedDataIx {a \u2113} {A : Set a} (_\u2264\u1d2c_ : A \u2192 A \u2192 Set \u2113) (isPreorder : IsPreorder _\u2261_ _\u2264\u1d2c_) where\n data Sorted : \u2200 {n} \u2192 Tree A n \u2192 A \u2192 A \u2192 Set (a L.\u2294 \u2113) where\n leaf : {x : A} \u2192 Sorted (leaf x) x x\n fork : \u2200 {n} {t u : Tree A n} {low_t high_t low\u1d64 high\u1d64} \u2192\n Sorted t low_t high_t \u2192\n Sorted u low\u1d64 high\u1d64 \u2192\n (h\u2264l : high_t \u2264\u1d2c low\u1d64) \u2192\n Sorted (fork t u) low_t high\u1d64\n\n private\n module \u2264\u1d2c = IsPreorder isPreorder\n\n \u2264\u1d2c-bounds : \u2200 {n} {t : Tree A n} {l h} \u2192 Sorted t l h \u2192 l \u2264\u1d2c h\n \u2264\u1d2c-bounds leaf = \u2264\u1d2c.refl\n \u2264\u1d2c-bounds (fork s\u2080 s\u2081 pf) = \u2264\u1d2c.trans (\u2264\u1d2c-bounds s\u2080) (\u2264\u1d2c.trans pf (\u2264\u1d2c-bounds s\u2081))\n\n Sorted\u2192lb : \u2200 {n} {t : Tree A n} {l h} \u2192 Sorted t l h \u2192 \u2200 {x} \u2192 x \u2208 t \u2192 l \u2264\u1d2c x\n Sorted\u2192lb leaf here = \u2264\u1d2c.refl\n Sorted\u2192lb (fork s _ _) (left p) = Sorted\u2192lb s p\n Sorted\u2192lb (fork s\u2080 s\u2081 pf) (right p) = \u2264\u1d2c.trans (\u2264\u1d2c.trans (\u2264\u1d2c-bounds s\u2080) pf) (Sorted\u2192lb s\u2081 p)\n\n Sorted\u2192ub : \u2200 {n} {t : Tree A n} {l h} \u2192 Sorted t l h \u2192 \u2200 {x} \u2192 x \u2208 t \u2192 x \u2264\u1d2c h\n Sorted\u2192ub leaf here = \u2264\u1d2c.refl\n Sorted\u2192ub (fork _ s _) (right p) = Sorted\u2192ub s p\n Sorted\u2192ub (fork s\u2080 s\u2081 pf) (left p) = \u2264\u1d2c.trans (\u2264\u1d2c.trans (Sorted\u2192ub s\u2080 p) pf) (\u2264\u1d2c-bounds s\u2081)\n\n Bounded : \u2200 {n} \u2192 Tree A n \u2192 A \u2192 A \u2192 Set (a L.\u2294 \u2113)\n Bounded t l h = \u2200 {x} \u2192 x \u2208 t \u2192 (l \u2264\u1d2c x) \u00d7 (x \u2264\u1d2c h)\n\n Sorted\u2192Bounded : \u2200 {n} {t : Tree A n} {l h} \u2192 Sorted t l h \u2192 Bounded t l h\n Sorted\u2192Bounded s x = Sorted\u2192lb s x , Sorted\u2192ub s x\n\n first-lb : \u2200 {n} {t : Tree A n} {l h} \u2192 Sorted t l h \u2192 first t \u2261 l\n first-lb leaf = refl\n first-lb (fork st _ _) = first-lb st\n\n last-ub : \u2200 {n} {t : Tree A n} {l h} \u2192 Sorted t l h \u2192 last t \u2261 h\n last-ub leaf = refl\n last-ub (fork _ st _) = last-ub st\n\n uniq-lb : \u2200 {n} {t : Tree A n} {l\u2080 h\u2080 l\u2081 h\u2081}\n \u2192 Sorted t l\u2080 h\u2080 \u2192 Sorted t l\u2081 h\u2081 \u2192 l\u2080 \u2261 l\u2081\n uniq-lb leaf leaf = refl\n uniq-lb (fork p p\u2081 h\u2264l) (fork q q\u2081 h\u2264l\u2081) = uniq-lb p q\n\n uniq-ub : \u2200 {n} {t : Tree A n} {l\u2080 h\u2080 l\u2081 h\u2081}\n \u2192 Sorted t l\u2080 h\u2080 \u2192 Sorted t l\u2081 h\u2081 \u2192 h\u2080 \u2261 h\u2081\n uniq-ub leaf leaf = refl\n uniq-ub (fork p p\u2081 h\u2264l) (fork q q\u2081 h\u2264l\u2081) = uniq-ub p\u2081 q\u2081\n\n Sorted-trans : \u2200 {n} {t u v : Tree A n} {lt hu lu hv}\n \u2192 Sorted (fork t u) lt hu \u2192 Sorted (fork u v) lu hv \u2192 Sorted (fork t v) lt hv\n Sorted-trans {lt = lt} {hu} {lu} {hv} (fork tu tu\u2081 h\u2264l) (fork uv uv\u2081 h\u2264l\u2081)\n rewrite uniq-lb uv tu\u2081\n | uniq-ub uv tu\u2081\n = fork tu uv\u2081 (\u2264\u1d2c.trans h\u2264l (\u2264\u1d2c.trans (\u2264\u1d2c-bounds tu\u2081) h\u2264l\u2081))\n\n {-\n bounded\u2192sorted : \u2200 {n} {t : Tree A n} {l h} \u2192 Bounded t l h \u2192 Sorted t l h\n bounded\u2192sorted {t = leaf x} b = {!b here!}\n bounded\u2192sorted {t = fork t\u2080 t\u2081} b = fork (bounded\u2192sorted {t = t\u2080} {!!}) (bounded\u2192sorted {t = t\u2081} {!!}) {!!}\n -}\n\nmodule Sorted' {a \u2113} {A : Set a} (_\u2264\u1d2c_ : A \u2192 A \u2192 Set \u2113) (isPreorder : IsPreorder _\u2261_ _\u2264\u1d2c_) where\n{-\n data _\u2264\u1d3e_ : \u2200 {x y t} \u2192 x \u2208 t \u2192 y \u2208 t \u2192 Set where\n here-here : here \u2264\u1d3e here\n left-left : \u2200 {x y t} {p : x \u2208 t} {q : y \u2208 t} \u2192 p \u2264\u1d3e q \u2192 left p \u2264\u1d3e left q\n right-right : \u2200 {x y t} {p : x \u2208 t} {q : y \u2208 t} \u2192 p \u2264\u1d3e q \u2192 right p \u2264\u1d3e right q\n left-right : \u2200 {x y t} {p : x \u2208 t} {q : y \u2208 t} \u2192 left p \u2264\u1d3e right q\n -}\n data _\u2264\u1d2e_ : \u2200 {n} (p q : Bits n) \u2192 Set where\n [] : [] \u2264\u1d2e []\n there : \u2200 {n} {p q : Bits n} b \u2192 p \u2264\u1d2e q \u2192 (b \u2237 p) \u2264\u1d2e (b \u2237 q)\n 0-1 : \u2200 {n} (p q : Bits n) \u2192 0\u2237 p \u2264\u1d2e 1\u2237 q\n\n _\u2264\u1d3e_ : \u2200 {n x y} {t : Tree A n} \u2192 x \u2208 t \u2192 y \u2208 t \u2192 Set\n p \u2264\u1d3e q = toBits p \u2264\u1d2e toBits q\n\n Sorted : \u2200 {n} \u2192 Tree A n \u2192 Set _\n Sorted t = \u2200 {x} (p : x \u2208 t) {y} (q : y \u2208 t) \u2192 p \u2264\u1d3e q \u2192 x \u2264\u1d2c y\n\n private\n module \u2264\u1d2c = IsPreorder isPreorder\n\n module S = SortedDataIx _\u2264\u1d2c_ isPreorder\n open S using (leaf; fork)\n Sorted\u2192Sorted' : \u2200 {n l h} {t : Tree A n} \u2192 S.Sorted t l h \u2192 Sorted t\n Sorted\u2192Sorted' leaf here here p\u2264q = \u2264\u1d2c.refl\n Sorted\u2192Sorted' (fork s _ _) (left p) (left q) (there ._ p\u2264q) = Sorted\u2192Sorted' s p q p\u2264q\n Sorted\u2192Sorted' (fork s\u2080 s\u2081 l\u2264h) (left p) (right q) p\u2264q = \u2264\u1d2c.trans (S.Sorted\u2192ub s\u2080 p) (\u2264\u1d2c.trans l\u2264h (S.Sorted\u2192lb s\u2081 q))\n Sorted\u2192Sorted' (fork _ _ _) (right _) (left _) ()\n Sorted\u2192Sorted' (fork _ s _) (right p) (right q) (there ._ p\u2264q) = Sorted\u2192Sorted' s p q p\u2264q\n\nmodule SortedData {a \u2113} {A : Set a} (_\u2264\u1d2c_ : A \u2192 A \u2192 Set \u2113) (isPreorder : IsPreorder _\u2261_ _\u2264\u1d2c_) where\n data Sorted : \u2200 {n} \u2192 Tree A n \u2192 Set (a L.\u2294 \u2113) where\n leaf : {x : A} \u2192 Sorted (leaf x)\n fork : \u2200 {n} {t u : Tree A n} \u2192\n Sorted t \u2192\n Sorted u \u2192\n (h\u2264l : last t \u2264\u1d2c first u) \u2192\n Sorted (fork t u)\n\nmodule Sorting {a} {A : Set a} -- (sort\u1d2c : A \u00d7 A \u2192 A \u00d7 A)\n (_\u2293\u1d2c_ _\u2294\u1d2c_ : A \u2192 A \u2192 A) where\n\n swap : \u2200 {n} \u2192 Tree A (1 + n) \u2192 Tree A (1 + n)\n swap (fork t u) = fork u t\n\n map-inner : \u2200 {n} \u2192 (Tree A (1 + n) \u2192 Tree A (1 + n)) \u2192 (Tree A (2 + n) \u2192 Tree A (2 + n))\n map-inner f (fork (fork t\u2080 t\u2081) (fork t\u2082 t\u2083)) =\n case f (fork t\u2081 t\u2082) of \u03bb { (fork t\u2084 t\u2085) \u2192 fork (fork t\u2080 t\u2084) (fork t\u2085 t\u2083) }\n\n map-outer : \u2200 {n} \u2192 (f g : Tree A n \u2192 Tree A n) \u2192 (Tree A (1 + n) \u2192 Tree A (1 + n))\n map-outer f g (fork t u) = fork (f t) (g u)\n\n interchange : \u2200 {n} \u2192 Tree A (2 + n) \u2192 Tree A (2 + n)\n interchange = map-inner swap\n\n merge : \u2200 {n} \u2192 Endo (Tree A (1 + n))\n merge {zero} (fork (leaf x\u2080) (leaf x\u2081)) =\n fork (leaf (x\u2080 \u2293\u1d2c x\u2081)) (leaf (x\u2080 \u2294\u1d2c x\u2081))\n merge {suc _} t\n = (map-inner merge \u2218 map-outer merge merge \u2218 interchange) t\n\n {-\n merge : \u2200 {n} \u2192 (t u : Tree A n) \u2192 Tree A (1 + n)\n merge (leaf x\u2080) (leaf x\u2081) =\n fork (leaf (x\u2080 \u2293\u1d2c x\u2081)) (leaf (x\u2080 \u2294\u1d2c x\u2081))\n merge (fork t\u2080 t\u2081) (fork u\u2080 u\u2081)\n with merge t\u2080 u\u2080 | merge t\u2081 u\u2081\n ... | fork l m\u2080 | fork m\u2081 h with merge m\u2080 m\u2081\n ... | fork m\u2080\u2032 m\u2081\u2032 = fork (fork l m\u2080\u2032) (fork m\u2081\u2032 h)\n\n sort : \u2200 {n} \u2192 Tree A n \u2192 Tree A n\n sort (leaf x) = leaf x\n sort (fork t\u2080 t\u2081) = merge (sort t\u2080) (sort t\u2081)\n\n open new-approach\n InjTree : \u2200 {n} \u2192 Tree A n \u2192 Set _\n InjTree t = \u2200 x \u2192 (p q : x \u2208 t) \u2192 p \u2261 q\n\n InjTree-\u00d7 : \u2200 {n} (t u : Tree A n) \u2192 InjTree (fork t u) \u2192 InjTree t \u00d7 InjTree u\n InjTree-\u00d7 t u pf = pf\u2080 , pf\u2081\n where pf\u2080 : InjTree t\n pf\u2080 x p q with pf x (left p) (left q)\n pf\u2080 x p .p | refl = refl\n pf\u2081 : InjTree u\n pf\u2081 x p q with pf x (right p) (right q)\n pf\u2081 x p .p | refl = refl\n\n _\u2257T_ : \u2200 {n} (t u : Tree A n) \u2192 Set _\n t \u2257T u = toFun t \u2257 toFun u\n\n {-\nmodule SortingProperties {\u2113 a} {A : Set a} (_\u2264\u1d2c_ : A \u2192 A \u2192 Set \u2113)\n (_\u2293\u1d2c_ _\u2294\u1d2c_ : A \u2192 A \u2192 A)\n (isPreorder : IsPreorder _\u2261_ _\u2264\u1d2c_)\n (\u2264-\u2294 : \u2200 x y \u2192 x \u2264\u1d2c (y \u2294\u1d2c x))\n (\u2293-\u2264 : \u2200 x y \u2192 (x \u2293\u1d2c y) \u2264\u1d2c y)\n (\u2264-\u2293 : \u2200 {x y z} \u2192 x \u2264\u1d2c y \u2192 x \u2264\u1d2c z \u2192 x \u2264\u1d2c (y \u2293\u1d2c z))\n (\u2294-\u2264 : \u2200 {x y z} \u2192 x \u2264\u1d2c z \u2192 y \u2264\u1d2c z \u2192 (x \u2294\u1d2c y) \u2264\u1d2c z)\n where\n module \u2264\u1d2c = IsPreorder isPreorder\n open Sorted' _\u2264\u1d2c_ isPreorder\n open Sorting _\u2293\u1d2c_ _\u2294\u1d2c_\n\n postulate dec-\u2264 : \u2200 x y \u2192 Dec (x \u2264\u1d2c y)\n postulate dec-\u2294 : \u2200 x y \u2192 x \u2294\u1d2c y \u2261 x \u228e x \u2294\u1d2c y \u2261 y\n postulate dec-\u2293 : \u2200 x y \u2192 x \u2293\u1d2c y \u2261 x \u228e x \u2293\u1d2c y \u2261 y\n\n merge-spec : \u2200 {n} (t u : Tree A n) \u2192\n Sorted t \u2192 Sorted u \u2192 Sorted (merge t u)\n merge-spec (leaf x) (leaf y) sx sy (left here) (left here) pf = \u2264\u1d2c.refl\n merge-spec (leaf x) (leaf y) sx sy (left here) (right here) (0-1 ._ ._) = \u2264\u1d2c.trans (\u2293-\u2264 x y) (\u2264-\u2294 y x)\n merge-spec (leaf x) (leaf y) sx sy (right p) (left q) ()\n merge-spec (leaf x) (leaf y) sx sy (right here) (right here) pf = \u2264\u1d2c.refl\n merge-spec (fork t\u2080 t\u2081) (fork u\u2080 u\u2081) st su p q p\u2264q\n with merge t\u2080 u\u2080 | merge t\u2081 u\u2081 -- | merge-spec t\u2080 u\u2080 ? ? | merge-spec t\u2081 u\u2081 ? ?\n ... | fork l m\u2080 | fork m\u2081 h\n with merge m\u2080 m\u2081 -- | merge-spec sm\u2080 sm\u2081\n ... | fork m\u2080\u2032 m\u2081\u2032 -- | fork {high_t = hm\u2080} {lm\u2081} sm\u2080\u2032 sm\u2081\u2032 pf3\n with p | q | p\u2264q\n ... | left pp | left qq | there ._ pp\u2264qq = {!merge-spec t\u2080 u\u2080 !}\n ... | left pp | right qq | 0-1 ._ ._ = {!!}\n ... | right pp | left qq | ()\n ... | right pp | right qq | there ._ pp\u2264qq = {!!}\n -}\n -}\n\nmodule SortingProperties {\u2113 a} {A : Set a} (_\u2264\u1d2c_ : A \u2192 A \u2192 Set \u2113)\n (_\u2293\u1d2c_ _\u2294\u1d2c_ : A \u2192 A \u2192 A)\n (isPreorder : IsPreorder _\u2261_ _\u2264\u1d2c_)\n (\u2264-\u2294 : \u2200 x y \u2192 x \u2264\u1d2c (y \u2294\u1d2c x))\n (\u2293-\u2264 : \u2200 x y \u2192 (x \u2293\u1d2c y) \u2264\u1d2c y)\n (\u2294-spec : \u2200 {x y} \u2192 x \u2264\u1d2c y \u2192 x \u2294\u1d2c y \u2261 y)\n (\u2293-spec : \u2200 {x y} \u2192 x \u2264\u1d2c y \u2192 x \u2293\u1d2c y \u2261 x)\n (\u2293-comm : Commutative _\u2261_ _\u2293\u1d2c_)\n (\u2294-comm : Commutative _\u2261_ _\u2294\u1d2c_)\n (\u2264-<_,_> : \u2200 {x y z} \u2192 x \u2264\u1d2c y \u2192 x \u2264\u1d2c z \u2192 x \u2264\u1d2c (y \u2293\u1d2c z))\n (\u2264-[_,_] : \u2200 {x y z} \u2192 x \u2264\u1d2c z \u2192 y \u2264\u1d2c z \u2192 (x \u2294\u1d2c y) \u2264\u1d2c z)\n where\n module \u2264\u1d2c = IsPreorder isPreorder\n open SortedDataIx _\u2264\u1d2c_ isPreorder\n open Sorting _\u2293\u1d2c_ _\u2294\u1d2c_\n module SD = SortedData _\u2264\u1d2c_ isPreorder\n open SD using (fork; leaf)\n merge-swap : \u2200 {n} (t : Tree A (1 + n)) \u2192 merge t \u2261 merge (swap t)\n merge-swap (fork (leaf x) (leaf y)) rewrite \u2294-comm x y | \u2293-comm y x = refl\n merge-swap (fork (fork t\u2080 t\u2081) (fork u\u2080 u\u2081))\n rewrite merge-swap (fork t\u2080 u\u2080)\n | merge-swap (fork t\u2081 u\u2081) = refl\n\n merge-pres : \u2200 {n} {t : Tree A (1 + n)} {l h} \u2192 Sorted t l h \u2192 merge t \u2261 t\n merge-pres (fork leaf leaf x) = cong\u2082 (fork on leaf) (\u2293-spec x) (\u2294-spec x)\n merge-pres {t = fork (fork t\u2080 t\u2081) (fork u\u2080 u\u2081)}\n (fork (fork {low_t = lt\u2080} {ht\u2080} {lt\u2081} {ht\u2081} st\u2080 st\u2081 ht\u2080\u2264lt\u2081)\n (fork {low_t = lu\u2080} {hu\u2080} {lu\u2081} {hu\u2081} su\u2080 su\u2081 hu\u2080\u2264lu\u2081) ht\u2081\u2264lu\u2080)\n rewrite merge-pres (fork st\u2080 su\u2080 (\u2264\u1d2c.trans ht\u2080\u2264lt\u2081 (\u2264\u1d2c.trans (\u2264\u1d2c-bounds st\u2081) ht\u2081\u2264lu\u2080)))\n | merge-pres (fork st\u2081 su\u2081 (\u2264\u1d2c.trans ht\u2081\u2264lu\u2080 (\u2264\u1d2c.trans (\u2264\u1d2c-bounds su\u2080) hu\u2080\u2264lu\u2081)))\n | merge-swap (fork u\u2080 t\u2081)\n | merge-pres (fork st\u2081 su\u2080 ht\u2081\u2264lu\u2080) = refl\n\n {-\n \u2203Sorted : \u2200 {n} \u2192 Tree A n \u2192 Set _\n \u2203Sorted t = \u2203 \u03bb l \u2192 \u2203 \u03bb h \u2192 Sorted t l h\n\n PreSorted : \u2200 {n} \u2192 Tree A (1 + n) \u2192 Set _\n PreSorted (fork t u) = \u2203Sorted t \u00d7 \u2203Sorted u\n -}\n\n PreSorted : \u2200 {n} \u2192 Tree A (1 + n) \u2192 Set _\n PreSorted (fork t u) = SD.Sorted t \u00d7 SD.Sorted u\n\n lft : \u2200 {n} \u2192 Tree A (1 + n) \u2192 Tree A n\n lft (fork t _) = t\n\n rght : \u2200 {n} \u2192 Tree A (1 + n) \u2192 Tree A n\n rght (fork _ t) = t\n\n \u03b7fork : \u2200 {n} (t : Tree A (1 + n)) \u2192 t \u2261 fork (lft t) (rght t)\n \u03b7fork (fork t t\u2081) = refl\n\n {-\n record MergeInnerHyp {n} (t : Tree A (2 + n)) : Set (a L.\u2294 \u2113) where\n constructor mk\n field\n st\u2080 : SD.Sorted (lft t)\n st\u2081 : SD.Sorted (rght t)\n u = interchange t\n field\n su\u2080 : SD.Sorted (lft u)\n su\u2081 : SD.Sorted (rght u)\n\n merge-inner : \u2200 {n} {t : Tree A (2 + n)} \u2192\n MergeInnerHyp t \u2192 SD.Sorted (map-inner merge t)\n merge-inner {t = fork (fork t\u2080 t\u2081) (fork u\u2080 u\u2081)}\n (mk (fork st\u2080 st\u2081 ht\u2080\u2264lt\u2081)\n (fork su\u2080 su\u2081 lu\u2080\u2264hu\u2081)\n (fork sv\u2080 sv\u2081 hv\u2080\u2264lv\u2081)\n (fork sw\u2080 sw\u2081 lw\u2080\u2264hw\u2081)) = {!!}\n -}\n\n first-merge : \u2200 {n} (t : Tree A (1 + n)) \u2192\n first (merge t) \u2261 first (lft t) \u2293\u1d2c first (rght t)\n first-merge (fork (leaf x) (leaf y)) = refl\n first-merge (fork (fork t\u2080 t\u2081) (fork u\u2080 u\u2081))\n with merge (fork t\u2080 u\u2080) | first-merge (fork t\u2080 u\u2080)\n | merge (fork t\u2081 u\u2081)\n ... | fork v\u2080 w\u2080 | pf\n | fork v\u2081 w\u2081\n with merge (fork w\u2080 v\u2081)\n ... | fork a b\n = pf\n\n last-merge : \u2200 {n} (t : Tree A (1 + n)) \u2192\n last (merge t) \u2261 last (lft t) \u2294\u1d2c last (rght t)\n last-merge (fork (leaf x) (leaf y)) = refl\n last-merge (fork (fork t\u2080 t\u2081) (fork u\u2080 u\u2081))\n with merge (fork t\u2080 u\u2080)\n | merge (fork t\u2081 u\u2081) | last-merge (fork t\u2081 u\u2081)\n ... | fork v\u2080 w\u2080\n | fork v\u2081 w\u2081 | pf\n with merge (fork w\u2080 v\u2081)\n ... | fork a b\n = pf\n\n -- last (merge t u) \u2261 last t \u2293\u1d2c last u\n merge-spec : \u2200 {n} {t u : Tree A n} \u2192\n SD.Sorted t \u2192 SD.Sorted u \u2192 SD.Sorted (merge (fork t u))\n merge-spec (leaf {x}) (leaf {y}) = fork leaf leaf (\u2264\u1d2c.trans (\u2293-\u2264 x y) (\u2264-\u2294 y x))\n merge-spec {t = fork t\u2080 t\u2081} {u = fork u\u2080 u\u2081}\n (fork st\u2080 st\u2081 ht\u2080\u2264lt\u2081)\n (fork su\u2080 su\u2081 lu\u2080\u2264hu\u2081)\n with merge (fork t\u2080 u\u2080) | merge-spec st\u2080 su\u2080 | first-merge (fork t\u2080 u\u2080)\n | merge (fork t\u2081 u\u2081) | merge-spec st\u2081 su\u2081\n ... | fork v\u2080 w\u2080 | fork sv\u2080 sw\u2080 p1 | fpf1\n | fork v\u2081 w\u2081 | fork sv\u2081 sw\u2081 p2\n with merge (fork w\u2080 v\u2081) | merge-spec sw\u2080 sv\u2081\n ... | fork a b | fork sa sb p3\n = fork (fork sv\u2080 sa pf1) (fork sb sw\u2081 pf2) p3\n where\n postulate\n pf3 : last v\u2080 \u2264\u1d2c first t\u2081\n pf4 : last v\u2080 \u2264\u1d2c first u\u2081\n -- Sorted (merge t u) \u2192\n pf1 : last v\u2080 \u2264\u1d2c first a\n pf1 = {!first-merge !}\n pf2 : last b \u2264\u1d2c first w\u2081\n pf2 = {!!}\n {-\n -}\n\n\n-- map-outer merge id \u2218 interchange\n\n {-\n\n merge-spec : \u2200 {n lt ht lu hu} {t u : Tree A n} \u2192\n Sorted t lt ht \u2192 Sorted u lu hu \u2192 Sorted (merge t u) (lt \u2293\u1d2c lu) (ht \u2294\u1d2c hu)\n merge-spec (leaf x) (leaf y) = fork (leaf _) (leaf _) (\u2264\u1d2c.trans (\u2293-\u2264 x y) (\u2264-\u2294 y x))\n merge-spec {t = fork t\u2080 t\u2081} {u = fork u\u2080 u\u2081} (fork {low_t = lt\u2080} {ht\u2080} {lt\u2081} {ht\u2081} st\u2080 st\u2081 ht\u2080\u2264lt\u2081)\n (fork {low_t = lu\u2080} {hu\u2080} {lu\u2081} {hu\u2081} su\u2080 su\u2081 lu\u2080\u2264hu\u2081)\n with merge t\u2080 u\u2080 | merge t\u2081 u\u2081 | merge-spec st\u2080 su\u2080 | merge-spec st\u2081 su\u2081\n ... | fork l m\u2080 | fork m\u2081 h | fork {high_t = hl} {lm\u2080} sl sm\u2080 pf1\n | fork {high_t = hm\u2081} {lh} sm\u2081 sh pf2\n with merge m\u2080 m\u2081 | merge-spec sm\u2080 sm\u2081\n ... | fork m\u2080\u2032 m\u2081\u2032 | fork {high_t = hm\u2080} {lm\u2081} sm\u2080\u2032 sm\u2081\u2032 pf3\n {-with \u2264\u1d2c-bounds st\u2080 | \u2264\u1d2c-bounds st\u2081\n | \u2264\u1d2c-bounds su\u2080 | \u2264\u1d2c-bounds su\u2081\n | \u2264\u1d2c-bounds sm\u2080 | \u2264\u1d2c-bounds sm\u2081\n | \u2264\u1d2c-bounds sm\u2080\u2032 | \u2264\u1d2c-bounds sm\u2081\u2032\n | \u2264\u1d2c-bounds sh | \u2264\u1d2c-bounds sl\n ... | lt\u2080\u2264ht\u2080 | lt\u2081\u2264ht\u2081\n | lu\u2080\u2264hu\u2081 | lu\u2081\u2264hu\u2081\n | lm\u2080\u2264\u2605 | \u2605\u2264hm\u2081\n | \u2605\u2264hm\u2080 | lm\u2081\u2264\u2605\n | lh\u2264\u2605 | \u2605\u2264hl-} =\n fork\n (fork sl sm\u2080\u2032 (proj\u2081 pf))\n (fork sm\u2081\u2032 sh (proj\u2082 pf)) pf3\n module M where\n hl\u2264lt\u2081 : hl \u2264\u1d2c lt\u2081\n hl\u2264lt\u2081 = {!!}\n hl\u2264lu\u2081 : hl \u2264\u1d2c lu\u2081\n hl\u2264lu\u2081 = {!!}\n ht\u2080\u2264lh : ht\u2080 \u2264\u1d2c lh\n ht\u2080\u2264lh = {!!}\n hu\u2080\u2264lh : hu\u2080 \u2264\u1d2c lh\n hu\u2080\u2264lh = {!!}\n\n pf : (hl \u2264\u1d2c (lm\u2080 \u2293\u1d2c (lt\u2081 \u2293\u1d2c lu\u2081))) \u00d7 (((ht\u2080 \u2294\u1d2c hu\u2080) \u2294\u1d2c hm\u2081) \u2264\u1d2c lh)\n pf = \u2264-\u2293 pf1 (\u2264-\u2293 hl\u2264lt\u2081 hl\u2264lu\u2081) , \u2294-\u2264 (\u2294-\u2264 ht\u2080\u2264lh hu\u2080\u2264lh) pf2\n\npostulate\n _\u2294_ : \u2200 {n} \u2192 Bits n \u2192 Bits n \u2192 Bits n\n _\u2293_ : \u2200 {n} \u2192 Bits n \u2192 Bits n \u2192 Bits n\n\nmodule BitsSorting {m} where\n\n module S = Sorting (_\u2293_ {m}) (_\u2294_ {m})\n open S public using (InjTree; InjTree-\u00d7)\n\n merge : \u2200 {n} \u2192 (t u : Tree (Bits m) n) \u2192 Tree (Bits m) (1 + n)\n merge = S.merge\n\n sort : \u2200 {n} \u2192 Tree (Bits m) n \u2192 Tree (Bits m) n\n sort = S.sort\n\nmodule BitsSorting\u2032 where\n open BitsSorting\n open AllBits\n -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"ef0b3de030e69c13f6abe2f657976fd6056b39d2","subject":"Update example1","message":"Update example1\n","repos":"crypto-agda\/agda-libjs","old_file":"example1.agda","new_file":"example1.agda","new_contents":"module example1 where\n\nopen import Data.String.Base using (String)\nopen import Data.Product\nopen import Data.List.Base using (List; []; _\u2237_)\nopen import Data.Bool.Base\nopen import Function\n\nopen import Control.Process.Type\n\nopen import FFI.JS as JS\nopen import FFI.JS.Proc\nimport FFI.JS.Console as Console\nimport FFI.JS.Process as Process\nimport FFI.JS.FS as FS\n\ntake-half : String \u2192 String\ntake-half s = substring s 0N (length s \/ 2N)\n\ndrop-half : String \u2192 String\ndrop-half s = substring1 s (length s \/ 2N)\n\ntest-value : Value\ntest-value = object ((\"array\" , array (array [] \u2237 array (array [] \u2237 []) \u2237 [])) \u2237\n (\"object\" , array (object [] \u2237 object ((\"a\", string \"b\") \u2237 []) \u2237 [])) \u2237\n (\"string\" , array (string \"\" \u2237 string \"a\" \u2237 [])) \u2237\n (\"number\" , array (number 0N \u2237 number 1N \u2237 [])) \u2237\n (\"bool\" , array (bool true \u2237 bool false \u2237 [])) \u2237\n (\"null\" , array (null \u2237 [])) \u2237 [])\n\ntest =\n fromString (JSON-stringify (fromValue test-value))\n ===\n fromString \"{\\\"array\\\":[[],[[]]],\\\"object\\\":[{},{\\\"a\\\":\\\"b\\\"}],\\\"string\\\":[\\\"\\\",\\\"a\\\"],\\\"number\\\":[0,1],\\\"bool\\\":[true,false],\\\"null\\\":[null]}\"\n\n\nmodule _ {A : Set} (_\u2264_ : A \u2192 A \u2192 Bool) where\n\n merge-sort-list : (l\u2080 l\u2081 : List A) \u2192 List A\n merge-sort-list [] l\u2081 = l\u2081\n merge-sort-list l\u2080 [] = l\u2080\n merge-sort-list (x\u2080 \u2237 l\u2080) (x\u2081 \u2237 l\u2081) with x\u2080 \u2264 x\u2081\n ... | true = x\u2080 \u2237 merge-sort-list l\u2080 (x\u2081 \u2237 l\u2081)\n ... | false = x\u2081 \u2237 merge-sort-list (x\u2080 \u2237 l\u2080) l\u2081\n\nmerge-sort-string : String \u2192 String \u2192 String\nmerge-sort-string s\u2080 s\u2081 = List\u25b9String (merge-sort-list _\u2264Char_ (String\u25b9List s\u2080) (String\u25b9List s\u2081))\n\nmapJSArray : (JSArray String \u2192 JSArray String) \u2192 JSValue \u2192 JSValue\nmapJSArray f v = fromString (join \"\" \u2218 f \u2218 split \"\" \u2218 castString $ v)\n\nreverser : URI \u2192 JSProc\nreverser d = recv d \u03bb s \u2192 send d (mapJSArray JS.reverse s) end\n\nadder : URI \u2192 JSProc\nadder d = recv d \u03bb s\u2080 \u2192 recv d \u03bb s\u2081 \u2192 send d (s\u2080 +JS s\u2081) end\n\nadder-client : URI \u2192 JSValue \u2192 JSValue \u2192 JSProc\nadder-client d s\u2080 s\u2081 = send d s\u2080 (send d s\u2081 (recv d \u03bb _ \u2192 end))\n\nmodule _ (adder-addr reverser-addr : URI)(s : JSValue) where\n adder-reverser-client : JSProc\n adder-reverser-client =\n send reverser-addr s $\n send adder-addr s $\n recv reverser-addr \u03bb rs \u2192\n send adder-addr rs $\n recv adder-addr \u03bb res \u2192\n end\n\nstr-sorter\u2080 : URI \u2192 JSProc\nstr-sorter\u2080 d = recv d \u03bb s \u2192 send d (mapJSArray sort s) end\n\nstr-sorter-client : \u2200 {D} \u2192 D \u2192 JSValue \u2192 Proc D JSValue\nstr-sorter-client d s = send d s $ recv d \u03bb _ \u2192 end\n\nmodule _ (upstream helper\u2080 helper\u2081 : URI) where\n str-merger : JSProc\n str-merger =\n recv upstream \u03bb s \u2192\n send helper\u2080 (fromString (take-half (castString s))) $\n send helper\u2081 (fromString (drop-half (castString s))) $\n recv helper\u2080 \u03bb ss\u2080 \u2192\n recv helper\u2081 \u03bb ss\u2081 \u2192\n send upstream (fromString (merge-sort-string (castString ss\u2080) (castString ss\u2081)))\n end\n\ndyn-merger : URI \u2192 (URI \u2192 JSProc) \u2192 JSProc\ndyn-merger upstream helper =\n spawn helper \u03bb helper\u2080 \u2192\n spawn helper \u03bb helper\u2081 \u2192\n str-merger upstream helper\u2080 helper\u2081\n\nstr-sorter\u2081 : URI \u2192 JSProc\nstr-sorter\u2081 upstream = dyn-merger upstream str-sorter\u2080\n\nstr-sorter\u2082 : URI \u2192 JSProc\nstr-sorter\u2082 upstream = dyn-merger upstream str-sorter\u2081\n\nstringifier : URI \u2192 JSProc\nstringifier d = recv d \u03bb v \u2192 send d (fromString (JSON-stringify v)) end\n\nstringifier-client : \u2200 {D} \u2192 D \u2192 JSValue \u2192 Proc D JSValue\nstringifier-client d v = send d v $ recv d \u03bb _ \u2192 end\n\nmain : JS!\nmain =\n Console.log \"Hey!\" >> assert test >>\n Process.argv !\u2081 \u03bb argv \u2192 Console.log (\"argv=\" ++ join \" \" argv) >>\n Console.log \"server(adder):\" >> server \"127.0.0.1\" \"1337\" adder !\u2081 \u03bb adder-uri \u2192\n Console.log \"client(adderclient):\" >>\n client (adder-client adder-uri (fromString \"Hello \") (fromString \"World!\")) >>\n client (adder-client adder-uri (fromString \"Bonjour \") (fromString \"monde!\")) >>\n Console.log \"server(reverser):\" >>\n server \"127.0.0.1\" \"1338\" reverser !\u2081 \u03bb reverser-uri \u2192\n Console.log \"client(adder-reverser-client):\" >>\n client (adder-reverser-client adder-uri reverser-uri (fromString \"red\")) >>\n\n server \"127.0.0.1\" \"1339\" str-sorter\u2080 !\u2081 \u03bb str-sorter\u2080-uri \u2192\n Console.log \"str-sorter-client for str-sorter\u2080:\" >>\n client (str-sorter-client str-sorter\u2080-uri (fromString \"Something to be sorted!\")) >>\n\n server \"127.0.0.1\" \"1342\" str-sorter\u2082 !\u2081 \u03bb str-sorter\u2082-uri \u2192\n Console.log \"str-sorter-client:\" >>\n client (str-sorter-client str-sorter\u2082-uri (fromString \"Something to be sorted!\")) >>\n\n server \"127.0.0.1\" \"1343\" stringifier !\u2081 \u03bb stringifier-uri \u2192\n client (stringifier-client stringifier-uri (fromValue test-value)) >>\n FS.readFile \"README.md\" nullJS !\u2082 \u03bb err dat \u2192\n Console.log (\"README.md, length is \" ++ Number\u25b9String (length (toString dat))) >>\n Console.log \"Bye!\"\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"module example1 where\n\nopen import Data.String.Base using (String)\nopen import Data.Product\nopen import Data.List.Base using (List; []; _\u2237_)\nopen import Data.Bool.Base\nopen import Function\n\nopen import Control.Process.Type\n\nopen import FFI.JS as JS\nopen import FFI.JS.Proc\nimport FFI.JS.Console as Console\nimport FFI.JS.Process as Process\nimport FFI.JS.FS as FS\n\npostulate take-half : String \u2192 String\n{-# COMPILED_JS take-half function(x) { return x.substring(0,x.length\/2); } #-}\npostulate drop-half : String \u2192 String\n{-# COMPILED_JS drop-half function(x) { return x.substring(x.length\/2); } #-}\n\ntest-value : Value\ntest-value = object ((\"array\" , array (array [] \u2237 array (array [] \u2237 []) \u2237 [])) \u2237\n (\"object\" , array (object [] \u2237 object ((\"a\", string \"b\") \u2237 []) \u2237 [])) \u2237\n (\"string\" , array (string \"\" \u2237 string \"a\" \u2237 [])) \u2237\n (\"number\" , array (number zero \u2237 number one \u2237 [])) \u2237\n (\"bool\" , array (bool true \u2237 bool false \u2237 [])) \u2237\n (\"null\" , array (null \u2237 [])) \u2237 [])\n\ntest =\n fromString (JSON-stringify (fromValue test-value))\n ===\n fromString \"{\\\"array\\\":[[],[[]]],\\\"object\\\":[{},{\\\"a\\\":\\\"b\\\"}],\\\"string\\\":[\\\"\\\",\\\"a\\\"],\\\"number\\\":[0,1],\\\"bool\\\":[true,false],\\\"null\\\":[null]}\"\n\n\nmodule _ {A : Set} (_\u2264_ : A \u2192 A \u2192 Bool) where\n\n merge-sort-list : (l\u2080 l\u2081 : List A) \u2192 List A\n merge-sort-list [] l\u2081 = l\u2081\n merge-sort-list l\u2080 [] = l\u2080\n merge-sort-list (x\u2080 \u2237 l\u2080) (x\u2081 \u2237 l\u2081) with x\u2080 \u2264 x\u2081\n ... | true = x\u2080 \u2237 merge-sort-list l\u2080 (x\u2081 \u2237 l\u2081)\n ... | false = x\u2081 \u2237 merge-sort-list (x\u2080 \u2237 l\u2080) l\u2081\n\nmerge-sort-string : String \u2192 String \u2192 String\nmerge-sort-string s\u2080 s\u2081 = List\u25b9String (merge-sort-list _\u2264Char_ (String\u25b9List s\u2080) (String\u25b9List s\u2081))\n\nmapJSArray : (JSArray String \u2192 JSArray String) \u2192 JSValue \u2192 JSValue\nmapJSArray f v = fromString (onString (join \"\" \u2218 f \u2218 split \"\") v)\n\nreverser : URI \u2192 JSProc\nreverser d = recv d \u03bb s \u2192 send d (mapJSArray JS.reverse s) end\n\nadder : URI \u2192 JSProc\nadder d = recv d \u03bb s\u2080 \u2192 recv d \u03bb s\u2081 \u2192 send d (s\u2080 +JS s\u2081) end\n\nadder-client : URI \u2192 JSValue \u2192 JSValue \u2192 JSProc\nadder-client d s\u2080 s\u2081 = send d s\u2080 (send d s\u2081 (recv d \u03bb _ \u2192 end))\n\nmodule _ (adder-addr reverser-addr : URI)(s : JSValue) where\n adder-reverser-client : JSProc\n adder-reverser-client =\n send reverser-addr s $\n send adder-addr s $\n recv reverser-addr \u03bb rs \u2192\n send adder-addr rs $\n recv adder-addr \u03bb res \u2192\n end\n\nstr-sorter\u2080 : URI \u2192 JSProc\nstr-sorter\u2080 d = recv d \u03bb s \u2192 send d (mapJSArray sort s) end\n\nstr-sorter-client : \u2200 {D} \u2192 D \u2192 JSValue \u2192 Proc D JSValue\nstr-sorter-client d s = send d s $ recv d \u03bb _ \u2192 end\n\nmodule _ (upstream helper\u2080 helper\u2081 : URI) where\n str-merger : JSProc\n str-merger =\n recv upstream \u03bb s \u2192\n send helper\u2080 (fromString (onString take-half s)) $\n send helper\u2081 (fromString (onString drop-half s)) $\n recv helper\u2080 \u03bb ss\u2080 \u2192\n recv helper\u2081 \u03bb ss\u2081 \u2192\n send upstream (fromString (onString (onString merge-sort-string ss\u2080) ss\u2081))\n end\n\ndyn-merger : URI \u2192 (URI \u2192 JSProc) \u2192 JSProc\ndyn-merger upstream helper =\n spawn helper \u03bb helper\u2080 \u2192\n spawn helper \u03bb helper\u2081 \u2192\n str-merger upstream helper\u2080 helper\u2081\n\nstr-sorter\u2081 : URI \u2192 JSProc\nstr-sorter\u2081 upstream = dyn-merger upstream str-sorter\u2080\n\nstr-sorter\u2082 : URI \u2192 JSProc\nstr-sorter\u2082 upstream = dyn-merger upstream str-sorter\u2081\n\nstringifier : URI \u2192 JSProc\nstringifier d = recv d \u03bb v \u2192 send d (fromString (JSON-stringify v)) end\n\nstringifier-client : \u2200 {D} \u2192 D \u2192 JSValue \u2192 Proc D JSValue\nstringifier-client d v = send d v $ recv d \u03bb _ \u2192 end\n\nmain : JS!\nmain =\n Console.log \"Hey!\" >> assert test >>\n Process.argv !\u2081 \u03bb argv \u2192 Console.log (\"argv=\" ++ join \" \" argv) >>\n Console.log \"server(adder):\" >> server \"127.0.0.1\" \"1337\" adder !\u2081 \u03bb adder-uri \u2192\n Console.log \"client(adderclient):\" >>\n client (adder-client adder-uri (fromString \"Hello \") (fromString \"World!\")) >>\n client (adder-client adder-uri (fromString \"Bonjour \") (fromString \"monde!\")) >>\n Console.log \"server(reverser):\" >>\n server \"127.0.0.1\" \"1338\" reverser !\u2081 \u03bb reverser-uri \u2192\n Console.log \"client(adder-reverser-client):\" >>\n client (adder-reverser-client adder-uri reverser-uri (fromString \"red\")) >>\n\n server \"127.0.0.1\" \"1342\" str-sorter\u2082 !\u2081 \u03bb str-sorter\u2082-uri \u2192\n Console.log \"str-sorter-client:\" >>\n client (str-sorter-client str-sorter\u2082-uri (fromString \"Something to be sorted!\")) >>\n\n server \"127.0.0.1\" \"1343\" stringifier !\u2081 \u03bb stringifier-uri \u2192\n client (stringifier-client stringifier-uri (fromValue test-value)) >>\n FS.readFile \"README.md\" nullJS !\u2082 \u03bb err dat \u2192\n Console.log (\"README.md, length is \" ++ Number\u25b9String (length (castString dat))) >>\n Console.log \"Bye!\" >>\n end\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"fb1d9843b7b5fef2c931b969b8e78f25726b0909","subject":"IDesc: tagged indexed description","message":"IDesc: tagged indexed description","repos":"brixen\/Epigram,kwangkim\/pigment,kwangkim\/pigment,kwangkim\/pigment","old_file":"models\/IDesc_type_type.agda","new_file":"models\/IDesc_type_type.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {I : Set} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi (pi S T) = cong (pi S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n\n-- From embedding to embedding\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\\ _ -> descD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> descD I)) -> Set\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (descD I) (IDescl0 I))\n (hs : desc (box (descD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\\ s -> psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s -> psi (phi (T s)))\n T\n hs)","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {I : Set} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi (pi S T) = cong (pi S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n\n-- From embedding to embedding\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\\ _ -> descD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> descD I)) -> Set\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (descD I) (IDescl0 I))\n (hs : desc (box (descD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\\ s -> psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s -> psi (phi (T s)))\n T\n hs)","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b3651f102b65a31ef5dd4d541002915b3e5cc9fe","subject":"Improve commentary in Base.Data.DependentList.","message":"Improve commentary in Base.Data.DependentList.\n\nOld-commit-hash: 7a4d9aea2f3574b1b3f21022bced5f9ef112264c\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Data\/DependentList.agda","new_file":"Base\/Data\/DependentList.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Reexport Data.List.All from the standard library.\n--\n-- At one point, we reinvented Data.List.All from the Agda\n-- standard library, under the name dependent list. We later\n-- replaced our reinvention by this adapter module that just\n-- exports the standard library's version with partly different\n-- names.\n------------------------------------------------------------------------\n\nmodule Base.Data.DependentList where\n\nopen import Data.List.All public\n using\n ( head\n ; tail\n ; map\n ; tabulate\n )\n renaming\n ( All to DependentList\n ; _\u2237_ to _\u2022_\n ; [] to \u2205\n )\n\n-- Maps a binary function over two dependent lists.\n-- Should this be in the Agda standard library?\nzipWith : \u2200 {a p q r} {A : Set a} {P : A \u2192 Set p} {Q : A \u2192 Set q} {R : A \u2192 Set r} \u2192\n (f : {a : A} \u2192 P a \u2192 Q a \u2192 R a) \u2192\n \u2200 {xs} \u2192 DependentList P xs \u2192 DependentList Q xs \u2192 DependentList R xs\nzipWith f \u2205 \u2205 = \u2205\nzipWith f (p \u2022 ps) (q \u2022 qs) = f p q \u2022 zipWith f ps qs\n","old_contents":"module Base.Data.DependentList where\n\nopen import Data.List.All public\n using\n ( head\n ; tail\n ; map\n ; tabulate\n )\n renaming\n ( All to DependentList\n ; _\u2237_ to _\u2022_\n ; [] to \u2205\n )\n\nzipWith : \u2200 {a p q r} {A : Set a} {P : A \u2192 Set p} {Q : A \u2192 Set q} {R : A \u2192 Set r} \u2192\n (f : {a : A} \u2192 P a \u2192 Q a \u2192 R a) \u2192\n \u2200 {xs} \u2192 DependentList P xs \u2192 DependentList Q xs \u2192 DependentList R xs\nzipWith f \u2205 \u2205 = \u2205\nzipWith f (p \u2022 ps) (q \u2022 qs) = f p q \u2022 zipWith f ps qs\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f36c392d31fa25a5e11981c52c34ff11f7b2d2de","subject":"Erased unnecessary parenthesis.","message":"Erased unnecessary parenthesis.\n\nIgnore-this: f72b1f73b49c371bb5ae4bdf44e761c\n\ndarcs-hash:20100501002912-3bd4e-c556b26bbefb1e09c60f2decd251473a241a4c66.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Function\/Arithmetic\/Properties.agda","new_file":"LTC\/Function\/Arithmetic\/Properties.agda","new_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties on total natural numbers\n------------------------------------------------------------------------------\n\nmodule LTC.Function.Arithmetic.Properties where\n\nopen import LTC.Minimal\n\nopen import LTC.Data.N\nopen import LTC.Function.Arithmetic\n\nopen import MyStdLib.Function\n\n------------------------------------------------------------------------------\n-- Closure properties\n\npred-N : {n : D} \u2192 N n \u2192 N (pred n)\npred-N zN = prf\n where\n postulate prf : N (pred zero)\n {-# ATP prove prf zN #-}\n\npred-N (sN {n} Nn) = prf\n where\n -- TODO: The postulate N (pred $ succ n) is not proved by the ATP.\n postulate prf : N (pred (succ n))\n {-# ATP prove prf #-}\n-- {-# ATP hint pred-N #-}\n\nminus-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m - n)\nminus-N {m} Nm zN = prf\n where\n postulate prf : N (m - zero)\n {-# ATP prove prf #-}\n\nminus-N zN (sN {n} Nn) = prf\n where\n postulate prf : N (zero - succ n)\n {-# ATP prove prf zN #-}\n\nminus-N (sN {m} Nm) (sN {n} Nn) = prf $ minus-N Nm Nn\n where\n postulate prf : N (m - n) \u2192 N (succ m - succ n)\n {-# ATP prove prf #-}\n\n+-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {n = n} zN Nn = prf\n where\n postulate prf : N (zero + n)\n {-# ATP prove prf #-}\n+-N {n = n} (sN {m} Nm ) Nn = prf (+-N Nm Nn)\n where\n postulate prf : N (m + n) \u2192 N (succ m + n)\n {-# ATP prove prf sN #-}\n\n*-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m * n)\n*-N {n = n} zN Nn = prf\n where\n postulate prf : N (zero * n)\n {-# ATP prove prf zN #-}\n*-N {n = n} (sN {m} Nm) Nn = prf (*-N Nm Nn)\n where\n postulate prf : N (m * n) \u2192 N (succ m * n)\n {-# ATP prove prf +-N #-}\n\n------------------------------------------------------------------------------\n\n-- Some proofs are based on the proofs in the standard library\n\n+-leftIdentity : {n : D} \u2192 N n \u2192 zero + n \u2261 n\n+-leftIdentity {n} _ = +-0x n\n\n+-rightIdentity : {n : D} \u2192 N n \u2192 n + zero \u2261 n\n+-rightIdentity zN = +-leftIdentity zN\n+-rightIdentity (sN {n} Nn) = prf $ +-rightIdentity Nn\n where\n postulate prf : n + zero \u2261 n \u2192 succ n + zero \u2261 succ n\n {-# ATP prove prf #-}\n\n+-assoc : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 m + n + o \u2261 m + (n + o)\n+-assoc {n = n} {o = o} zN Nn No = prf\n where\n postulate prf : zero + n + o \u2261 zero + (n + o)\n {-# ATP prove prf #-}\n+-assoc {n = n} {o = o} (sN {m} Nm) Nn No = prf $ +-assoc Nm Nn No\n where\n postulate prf : m + n + o \u2261 m + (n + o) \u2192\n succ m + n + o \u2261 succ m + (n + o)\n {-# ATP prove prf #-}\n\nx+1+y\u22611+x+y : {m n : D} \u2192 N m \u2192 N n \u2192 m + succ n \u2261 succ (m + n)\nx+1+y\u22611+x+y {n = n} zN Nn = prf\n where\n postulate prf : zero + succ n \u2261 succ (zero + n)\n {-# ATP prove prf #-}\nx+1+y\u22611+x+y {n = n} (sN {m} Nm) Nn = prf (x+1+y\u22611+x+y Nm Nn)\n where\n postulate prf : m + succ n \u2261 succ (m + n) \u2192\n succ m + succ n \u2261 succ (succ m + n)\n {-# ATP prove prf #-}\n\n+-comm : {m n : D} \u2192 N m \u2192 N n \u2192 m + n \u2261 n + m\n+-comm {n = n} zN Nn = prf\n where\n postulate prf : zero + n \u2261 n + zero\n {-# ATP prove prf +-rightIdentity #-}\n+-comm {n = n} (sN {m} Nm) Nn = prf (+-comm Nm Nn)\n where\n postulate prf : m + n \u2261 n + m \u2192 succ m + n \u2261 n + succ m\n {-# ATP prove prf x+1+y\u22611+x+y #-}\n\nminus-0x : {n : D} \u2192 N n \u2192 zero - n \u2261 zero\nminus-0x zN = minus-x0 zero\nminus-0x (sN {n} Nn) = minus-0S n\n\n[x+y]-[x+z]\u2261y-z : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192\n (m + n) - (m + o) \u2261 n - o\n[x+y]-[x+z]\u2261y-z {n = n} {o = o} zN Nn No = prf\n where\n postulate prf : (zero + n) - (zero + o) \u2261 n - o\n {-# ATP prove prf #-}\n\n-- Nice proof by the ATP.\n[x+y]-[x+z]\u2261y-z {n = n} {o = o} (sN {m} Nm) Nn No =\n prf ([x+y]-[x+z]\u2261y-z Nm Nn No)\n where\n postulate prf : (m + n) - (m + o) \u2261 n - o \u2192\n (succ m + n) - (succ m + o) \u2261 n - o\n {-# ATP prove prf #-}\n\n*-leftZero : {n : D} \u2192 N n \u2192 zero * n \u2261 zero\n*-leftZero {n} _ = *-0x n\n\n*-rightZero : {n : D} \u2192 N n \u2192 n * zero \u2261 zero\n*-rightZero zN = *-leftZero zN\n*-rightZero (sN {n} Nn) = prf (*-rightZero Nn)\n where\n postulate prf : n * zero \u2261 zero \u2192 succ n * zero \u2261 zero\n {-# ATP prove prf #-}\n\npostulate *-leftIdentity : {n : D} \u2192 N n \u2192 succ zero * n \u2261 n\n{-# ATP prove *-leftIdentity +-rightIdentity #-}\n\nx*1+y\u2261x+xy : {m n : D} \u2192 N m \u2192 N n \u2192 m * succ n \u2261 m + m * n\nx*1+y\u2261x+xy {n = n} zN Nn = prf\n where\n postulate prf : zero * succ n \u2261 zero + zero * n\n {-# ATP prove prf #-}\nx*1+y\u2261x+xy {n = n} (sN {m} Nm) Nn = prf (x*1+y\u2261x+xy Nm Nn)\n (+-assoc Nn Nm (*-N Nm Nn))\n (+-assoc Nm Nn (*-N Nm Nn))\n where\n -- N.B. We had to feed the ATP with the instances of the associate law\n postulate prf : m * succ n \u2261 m + m * n \u2192 -- IH\n (n + m) + (m * n) \u2261 n + (m + (m * n)) \u2192 -- Associative law\n (m + n) + (m * n) \u2261 m + (n + (m * n)) \u2192 -- Associateve law\n succ m * succ n \u2261 succ m + succ m * n\n {-# ATP prove prf +-comm #-}\n\n*-comm : {m n : D} \u2192 N m \u2192 N n \u2192 m * n \u2261 n * m\n*-comm {n = n} zN Nn = prf\n where\n postulate prf : zero * n \u2261 n * zero\n {-# ATP prove prf *-rightZero #-}\n*-comm {n = n} (sN {m} Nm) Nn = prf (*-comm Nm Nn)\n where\n postulate prf : m * n \u2261 n * m \u2192\n succ m * n \u2261 n * succ m\n {-# ATP prove prf x*1+y\u2261x+xy #-}\n\n[x-y]z\u2261xz*yz : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 (m - n) * o \u2261 m * o - n * o\n[x-y]z\u2261xz*yz {m} .{zero} {o} Nm zN No = prf\n where\n postulate prf : (m - zero) * o \u2261 m * o - zero * o\n {-# ATP prove prf #-}\n\n[x-y]z\u2261xz*yz {o = o} zN (sN {n} Nn) No = prf (minus-0x (*-N (sN Nn) No))\n where\n postulate prf : zero - succ n * o \u2261 zero \u2192\n (zero - succ n) * o \u2261 zero * o - succ n * o\n {-# ATP prove prf #-}\n\n[x-y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) zN = prf\n where\n postulate prf : (succ m - succ n) * zero \u2261 succ m * zero - succ n * zero\n {-# ATP prove prf *-comm minus-N zN #-}\n\n[x-y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) (sN {o} No) =\n prf ([x-y]z\u2261xz*yz Nm Nn (sN No))\n where\n postulate prf : (m - n) * succ o \u2261 m * succ o - n * succ o \u2192 -- IH\n (succ m - succ n) * succ o \u2261\n succ m * succ o - succ n * succ o\n {-# ATP prove prf sN *-N [x+y]-[x+z]\u2261y-z #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties on total natural numbers\n------------------------------------------------------------------------------\n\nmodule LTC.Function.Arithmetic.Properties where\n\nopen import LTC.Minimal\n\nopen import LTC.Data.N\nopen import LTC.Function.Arithmetic\n\nopen import MyStdLib.Function\n\n------------------------------------------------------------------------------\n-- Closure properties\n\npred-N : {n : D} \u2192 N n \u2192 N (pred n)\npred-N zN = prf\n where\n postulate prf : N (pred zero)\n {-# ATP prove prf zN #-}\n\npred-N (sN {n} Nn) = prf\n where\n -- TODO: The postulate N (pred $ succ n) is not proved by the ATP.\n postulate prf : N (pred (succ n))\n {-# ATP prove prf #-}\n-- {-# ATP hint pred-N #-}\n\nminus-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m - n)\nminus-N {m} Nm zN = prf\n where\n postulate prf : N (m - zero)\n {-# ATP prove prf #-}\n\nminus-N zN (sN {n} Nn) = prf\n where\n postulate prf : N (zero - succ n)\n {-# ATP prove prf zN #-}\n\nminus-N (sN {m} Nm) (sN {n} Nn) = prf $ minus-N Nm Nn\n where\n postulate prf : N (m - n) \u2192 N (succ m - succ n)\n {-# ATP prove prf #-}\n\n+-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {n = n} zN Nn = prf\n where\n postulate prf : N (zero + n)\n {-# ATP prove prf #-}\n+-N {n = n} (sN {m} Nm ) Nn = prf (+-N Nm Nn)\n where\n postulate prf : N (m + n) \u2192 N (succ m + n)\n {-# ATP prove prf sN #-}\n\n*-N : {m n : D} \u2192 N m \u2192 N n \u2192 N (m * n)\n*-N {n = n} zN Nn = prf\n where\n postulate prf : N (zero * n)\n {-# ATP prove prf zN #-}\n*-N {n = n} (sN {m} Nm) Nn = prf (*-N Nm Nn)\n where\n postulate prf : N (m * n) \u2192 N (succ m * n)\n {-# ATP prove prf +-N #-}\n\n------------------------------------------------------------------------------\n\n-- Some proofs are based on the proofs in the standard library\n\n+-leftIdentity : {n : D} \u2192 N n \u2192 zero + n \u2261 n\n+-leftIdentity {n} _ = +-0x n\n\n+-rightIdentity : {n : D} \u2192 N n \u2192 n + zero \u2261 n\n+-rightIdentity zN = +-leftIdentity zN\n+-rightIdentity (sN {n} Nn) = prf $ +-rightIdentity Nn\n where\n postulate prf : n + zero \u2261 n \u2192 succ n + zero \u2261 succ n\n {-# ATP prove prf #-}\n\n+-assoc : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 (m + n) + o \u2261 m + (n + o)\n+-assoc {n = n} {o = o} zN Nn No = prf\n where\n postulate prf : zero + n + o \u2261 zero + (n + o)\n {-# ATP prove prf #-}\n+-assoc {n = n} {o = o} (sN {m} Nm) Nn No = prf $ +-assoc Nm Nn No\n where\n postulate prf : m + n + o \u2261 m + (n + o) \u2192\n succ m + n + o \u2261 succ m + (n + o)\n {-# ATP prove prf #-}\n\nx+1+y\u22611+x+y : {m n : D} \u2192 N m \u2192 N n \u2192 m + succ n \u2261 succ (m + n)\nx+1+y\u22611+x+y {n = n} zN Nn = prf\n where\n postulate prf : zero + succ n \u2261 succ (zero + n)\n {-# ATP prove prf #-}\nx+1+y\u22611+x+y {n = n} (sN {m} Nm) Nn = prf (x+1+y\u22611+x+y Nm Nn)\n where\n postulate prf : m + succ n \u2261 succ (m + n) \u2192\n succ m + succ n \u2261 succ (succ m + n)\n {-# ATP prove prf #-}\n\n+-comm : {m n : D} \u2192 N m \u2192 N n \u2192 m + n \u2261 n + m\n+-comm {n = n} zN Nn = prf\n where\n postulate prf : zero + n \u2261 n + zero\n {-# ATP prove prf +-rightIdentity #-}\n+-comm {n = n} (sN {m} Nm) Nn = prf (+-comm Nm Nn)\n where\n postulate prf : m + n \u2261 n + m \u2192 succ m + n \u2261 n + succ m\n {-# ATP prove prf x+1+y\u22611+x+y #-}\n\nminus-0x : {n : D} \u2192 N n \u2192 zero - n \u2261 zero\nminus-0x zN = minus-x0 zero\nminus-0x (sN {n} Nn) = minus-0S n\n\n[x+y]-[x+z]\u2261y-z : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192\n (m + n) - (m + o) \u2261 n - o\n[x+y]-[x+z]\u2261y-z {n = n} {o = o} zN Nn No = prf\n where\n postulate prf : (zero + n) - (zero + o) \u2261 n - o\n {-# ATP prove prf #-}\n\n-- Nice proof by the ATP.\n[x+y]-[x+z]\u2261y-z {n = n} {o = o} (sN {m} Nm) Nn No =\n prf ([x+y]-[x+z]\u2261y-z Nm Nn No)\n where\n postulate prf : (m + n) - (m + o) \u2261 n - o \u2192\n (succ m + n) - (succ m + o) \u2261 n - o\n {-# ATP prove prf #-}\n\n*-leftZero : {n : D} \u2192 N n \u2192 zero * n \u2261 zero\n*-leftZero {n} _ = *-0x n\n\n*-rightZero : {n : D} \u2192 N n \u2192 n * zero \u2261 zero\n*-rightZero zN = *-leftZero zN\n*-rightZero (sN {n} Nn) = prf (*-rightZero Nn)\n where\n postulate prf : n * zero \u2261 zero \u2192 succ n * zero \u2261 zero\n {-# ATP prove prf #-}\n\npostulate *-leftIdentity : {n : D} \u2192 N n \u2192 succ zero * n \u2261 n\n{-# ATP prove *-leftIdentity +-rightIdentity #-}\n\nx*1+y\u2261x+xy : {m n : D} \u2192 N m \u2192 N n \u2192 m * succ n \u2261 m + m * n\nx*1+y\u2261x+xy {n = n} zN Nn = prf\n where\n postulate prf : zero * succ n \u2261 zero + zero * n\n {-# ATP prove prf #-}\nx*1+y\u2261x+xy {n = n} (sN {m} Nm) Nn = prf (x*1+y\u2261x+xy Nm Nn)\n (+-assoc Nn Nm (*-N Nm Nn))\n (+-assoc Nm Nn (*-N Nm Nn))\n where\n -- N.B. We had to feed the ATP with the instances of the associate law\n postulate prf : m * succ n \u2261 m + m * n \u2192 -- IH\n (n + m) + (m * n) \u2261 n + (m + (m * n)) \u2192 -- Associative law\n (m + n) + (m * n) \u2261 m + (n + (m * n)) \u2192 -- Associateve law\n succ m * succ n \u2261 succ m + succ m * n\n {-# ATP prove prf +-comm #-}\n\n*-comm : {m n : D} \u2192 N m \u2192 N n \u2192 m * n \u2261 n * m\n*-comm {n = n} zN Nn = prf\n where\n postulate prf : zero * n \u2261 n * zero\n {-# ATP prove prf *-rightZero #-}\n*-comm {n = n} (sN {m} Nm) Nn = prf (*-comm Nm Nn)\n where\n postulate prf : m * n \u2261 n * m \u2192\n succ m * n \u2261 n * succ m\n {-# ATP prove prf x*1+y\u2261x+xy #-}\n\n[x-y]z\u2261xz*yz : {m n o : D} \u2192 N m \u2192 N n \u2192 N o \u2192 (m - n) * o \u2261 m * o - n * o\n[x-y]z\u2261xz*yz {m} .{zero} {o} Nm zN No = prf\n where\n postulate prf : (m - zero) * o \u2261 m * o - zero * o\n {-# ATP prove prf #-}\n\n[x-y]z\u2261xz*yz {o = o} zN (sN {n} Nn) No = prf (minus-0x (*-N (sN Nn) No))\n where\n postulate prf : zero - succ n * o \u2261 zero \u2192\n (zero - succ n) * o \u2261 zero * o - succ n * o\n {-# ATP prove prf #-}\n\n[x-y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) zN = prf\n where\n postulate prf : (succ m - succ n) * zero \u2261 succ m * zero - succ n * zero\n {-# ATP prove prf *-comm minus-N zN #-}\n\n[x-y]z\u2261xz*yz (sN {m} Nm) (sN {n} Nn) (sN {o} No) =\n prf ([x-y]z\u2261xz*yz Nm Nn (sN No))\n where\n postulate prf : (m - n) * succ o \u2261 m * succ o - n * succ o \u2192 -- IH\n (succ m - succ n) * succ o \u2261\n succ m * succ o - succ n * succ o\n {-# ATP prove prf sN *-N [x+y]-[x+z]\u2261y-z #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"57b3c49f6694f8e55a607d041782d114dabf89b4","subject":"adapt bintree to composable and vcomposable","message":"adapt bintree to composable and vcomposable\n","repos":"crypto-agda\/crypto-agda","old_file":"bintree.agda","new_file":"bintree.agda","new_contents":"module bintree where\n\nopen import Function\nopen import Data.Nat.NP using (\u2115; zero; suc; _\u2264_; s\u2264s; _+_; module \u2115\u2264; module \u2115\u00b0)\nopen import Data.Nat.Properties\nopen import Data.Bool\nopen import Data.Vec using (_++_)\nopen import Data.Bits\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\nopen import composable\nopen import vcomp\nopen import forkable\n\ndata Tree {a} (A : Set a) : \u2115 \u2192 Set a where\n leaf : \u2200 {n} \u2192 A \u2192 Tree A n\n fork : \u2200 {n} (left right : Tree A n) \u2192 Tree A (suc n)\n\nfromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Tree A n\nfromFun {zero} f = leaf (f [])\nfromFun {suc n} f = fork (fromFun (f \u2218 0\u2237_)) (fromFun (f \u2218 1\u2237_))\n\ntoFun : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Bits n \u2192 A\ntoFun (leaf x) _ = x\ntoFun (fork left right) (b \u2237 bs) = toFun (if b then right else left) bs\n\ntoFun\u2218fromFun : \u2200 {n a} {A : Set a} (f : Bits n \u2192 A) \u2192 toFun (fromFun f) \u2257 f\ntoFun\u2218fromFun {zero} f [] = refl\ntoFun\u2218fromFun {suc n} f (0b \u2237 bs) = toFun\u2218fromFun {n} (f \u2218 0\u2237_) bs\ntoFun\u2218fromFun {suc n} f (1b \u2237 bs) = toFun\u2218fromFun {n} (f \u2218 1\u2237_) bs\n\nleaf\u207f : \u2200 {n a} {A : Set a} \u2192 A \u2192 Tree A n\nleaf\u207f {zero} x = leaf x\nleaf\u207f {suc n} x = fork t t where t = leaf\u207f x\n\nexpand : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Tree A n\nexpand (leaf x) = leaf\u207f x\nexpand (fork t\u2080 t\u2081) = fork (expand t\u2080) (expand t\u2081)\n\nfromConst\u2261leaf\u207f : \u2200 {n a} {A : Set a} (x : A) \u2192 fromFun (const x) \u2261 leaf\u207f {n} x\nfromConst\u2261leaf\u207f {zero} _ = refl\nfromConst\u2261leaf\u207f {suc n} x rewrite fromConst\u2261leaf\u207f {n} x = refl\n\nfromFun\u2218toFun : \u2200 {n a} {A : Set a} (t : Tree A n) \u2192 fromFun (toFun t) \u2261 expand t\nfromFun\u2218toFun (leaf x) = fromConst\u2261leaf\u207f x\nfromFun\u2218toFun (fork t\u2080 t\u2081) = cong\u2082 fork (fromFun\u2218toFun t\u2080) (fromFun\u2218toFun t\u2081)\n\nlookup : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Tree A n \u2192 A\nlookup = flip toFun\n\nmap : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 Tree A n \u2192 Tree B n\nmap f (leaf x) = leaf (f x)\nmap f (fork t\u2080 t\u2081) = fork (map f t\u2080) (map f t\u2081)\n\nweaken\u2264 : \u2200 {m n a} {A : Set a} \u2192 m \u2264 n \u2192 Tree A m \u2192 Tree A n\nweaken\u2264 _ (leaf x) = leaf x\nweaken\u2264 (s\u2264s p) (fork left right) = fork (weaken\u2264 p left) (weaken\u2264 p right)\n\nm\u2264n+m : \u2200 m n \u2192 m \u2264 n + m\nm\u2264n+m m n = \u2115\u2264.trans (m\u2264m+n m n) (\u2115\u2264.reflexive (\u2115\u00b0.+-comm m n))\n\nweaken+ : \u2200 n {m a} {A : Set a} \u2192 Tree A m \u2192 Tree A (n + m)\nweaken+ n = weaken\u2264 (m\u2264n+m _ n)\n\njoin : \u2200 {c\u2081 c\u2082 a} {A : Set a} \u2192 Tree (Tree A c\u2082) c\u2081 \u2192 Tree A (c\u2081 + c\u2082)\njoin {c} (leaf x) = weaken+ c x\njoin (fork left right) = fork (join left) (join right)\n\n_>>>_ : \u2200 {m n a} {A : Set a} \u2192 Tree (Bits m) n \u2192 Tree A m \u2192 Tree A n\nf >>> g = map (flip lookup g) f\n\n_\u2192\u1d57_ : (i o : \u2115) \u2192 Set\ni \u2192\u1d57 o = Tree (Bits o) i\n\ncomposable : Composable _\u2192\u1d57_\ncomposable = mk _>>>_\n\n_***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 i\u2080 \u2192\u1d57 o\u2080 \u2192 i\u2081 \u2192\u1d57 o\u2081 \u2192 (i\u2080 + i\u2081) \u2192\u1d57 (o\u2080 + o\u2081)\n(f *** g) = join (map (\u03bb xs \u2192 map (_++_ xs) g) f)\n\nvcomposable : VComposable _+_ _\u2192\u1d57_\nvcomposable = mk _***_\n\nforkable : Forkable suc _\u2192\u1d57_\nforkable = mk fork\n\ndata Rot {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n leaf : \u2200 {n} x \u2192 Rot {n = n} (leaf x) (leaf x)\n fork : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 left\u2081 \u2192\n Rot right\u2080 right\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n krof : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 right\u2081 \u2192\n Rot right\u2080 left\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n\nRot-refl : \u2200 {n a} {A : Set a} \u2192 Reflexive (Rot {A = A} {n})\nRot-refl {x = leaf x} = leaf x\nRot-refl {x = fork left right} = fork Rot-refl Rot-refl\n\nRot-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Rot {A = A} {n})\nRot-sym (leaf x) = leaf x\nRot-sym (fork p\u2080 p\u2081) = fork (Rot-sym p\u2080) (Rot-sym p\u2081)\nRot-sym (krof p\u2080 p\u2081) = krof (Rot-sym p\u2081) (Rot-sym p\u2080)\n\nRot-trans : \u2200 {n a} {A : Set a} \u2192 Transitive (Rot {A = A} {n})\nRot-trans (leaf x) q = q\nRot-trans (fork p\u2080 p\u2081) (fork q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (fork p\u2080 p\u2081) (krof q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (krof p\u2080 p\u2081) (fork q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\nRot-trans (krof p\u2080 p\u2081) (krof q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\n","old_contents":"module bintree where\n\nopen import Function\nopen import Data.Nat\nopen import Data.Bool\nopen import Data.Bits\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\n\ndata Tree {a} (A : Set a) : \u2115 \u2192 Set a where\n leaf : \u2200 {n} \u2192 A \u2192 Tree A n\n fork : \u2200 {n} (left right : Tree A n) \u2192 Tree A (suc n)\n\nfromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Tree A n\nfromFun {zero} f = leaf (f [])\nfromFun {suc n} f = fork (fromFun (f \u2218 0\u2237_)) (fromFun (f \u2218 1\u2237_))\n\ntoFun : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Bits n \u2192 A\ntoFun (leaf x) _ = x\ntoFun (fork left right) (b \u2237 bs) = toFun (if b then right else left) bs\n\ntoFun\u2218fromFun : \u2200 {n a} {A : Set a} (f : Bits n \u2192 A) \u2192 toFun (fromFun f) \u2257 f\ntoFun\u2218fromFun {zero} f [] = refl\ntoFun\u2218fromFun {suc n} f (0b \u2237 bs) = toFun\u2218fromFun {n} (f \u2218 0\u2237_) bs\ntoFun\u2218fromFun {suc n} f (1b \u2237 bs) = toFun\u2218fromFun {n} (f \u2218 1\u2237_) bs\n\nleaf\u207f : \u2200 {n a} {A : Set a} \u2192 A \u2192 Tree A n\nleaf\u207f {zero} x = leaf x\nleaf\u207f {suc n} x = fork t t where t = leaf\u207f x\n\nexpand : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Tree A n\nexpand (leaf x) = leaf\u207f x\nexpand (fork t\u2080 t\u2081) = fork (expand t\u2080) (expand t\u2081)\n\nfromConst\u2261leaf\u207f : \u2200 {n a} {A : Set a} (x : A) \u2192 fromFun (const x) \u2261 leaf\u207f {n} x\nfromConst\u2261leaf\u207f {zero} _ = refl\nfromConst\u2261leaf\u207f {suc n} x rewrite fromConst\u2261leaf\u207f {n} x = refl\n\nfromFun\u2218toFun : \u2200 {n a} {A : Set a} (t : Tree A n) \u2192 fromFun (toFun t) \u2261 expand t\nfromFun\u2218toFun (leaf x) = fromConst\u2261leaf\u207f x\nfromFun\u2218toFun (fork t\u2080 t\u2081) = cong\u2082 fork (fromFun\u2218toFun t\u2080) (fromFun\u2218toFun t\u2081)\n\nlookup : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Tree A n \u2192 A\nlookup = flip toFun\n\nmap : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 Tree A n \u2192 Tree B n\nmap f (leaf x) = leaf (f x)\nmap f (fork t\u2080 t\u2081) = fork (map f t\u2080) (map f t\u2081)\n\ndata Rot {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n leaf : \u2200 {n} x \u2192 Rot {n = n} (leaf x) (leaf x)\n fork : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 left\u2081 \u2192\n Rot right\u2080 right\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n krof : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 right\u2081 \u2192\n Rot right\u2080 left\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n\nRot-refl : \u2200 {n a} {A : Set a} \u2192 Reflexive (Rot {A = A} {n})\nRot-refl {x = leaf x} = leaf x\nRot-refl {x = fork left right} = fork Rot-refl Rot-refl\n\nRot-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Rot {A = A} {n})\nRot-sym (leaf x) = leaf x\nRot-sym (fork p\u2080 p\u2081) = fork (Rot-sym p\u2080) (Rot-sym p\u2081)\nRot-sym (krof p\u2080 p\u2081) = krof (Rot-sym p\u2081) (Rot-sym p\u2080)\n\nRot-trans : \u2200 {n a} {A : Set a} \u2192 Transitive (Rot {A = A} {n})\nRot-trans (leaf x) q = q\nRot-trans (fork p\u2080 p\u2081) (fork q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (fork p\u2080 p\u2081) (krof q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (krof p\u2080 p\u2081) (fork q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\nRot-trans (krof p\u2080 p\u2081) (krof q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"362f857ed3f64793c31edc314bd464b3913cc72b","subject":"updating progress checks for #13","message":"updating progress checks for #13\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress-checks.agda","new_file":"progress-checks.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import canonical-forms\nopen import type-assignment-unicity\n\n\n-- taken together, the theorems in this file argue that for any expression\n-- d, at most one summand of the labeled sum that results from progress may\n-- be true at any time, i.e. that values, indeterminates, errors, and\n-- expressions that step are pairwise disjoint. (note that as a consequence\n-- of currying and comutativity of products, this means that there are six\n-- theorems to prove)\nmodule progress-checks where\n -- values and indeterminates are disjoint\n vi : \u2200{d} \u2192 d val \u2192 d indet \u2192 \u22a5\n vi VConst ()\n vi VLam ()\n\n -- values and errors are disjoint\n ve : \u2200{d \u0394} \u2192 d val \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n ve VConst ()\n ve VLam ()\n\n -- values and expressions that step are disjoint\n vs : \u2200{d \u0394} \u2192 d val \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n vs VConst (_ , Step (FHFinal _) () _)\n vs VLam (_ , Step (FHFinal _) () _)\n\n mutual\n -- indeterminates and errors are disjoint\n ie : \u2200{d \u0394} \u2192 d indet \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n ie IEHole ()\n ie (INEHole x) (ENEHole e) = fe x e\n ie (IAp i x) (EAp1 e) = ie i e\n ie (IAp i x) (EAp2 y e) = fe x e\n\n -- final expressions are not errors (not one of the 6 cases for progress, just a convenience)\n fe : \u2200{d \u0394} \u2192 d final \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n fe (FVal x) er = ve x er\n fe (FIndet x) er = ie x er\n\n -- todo: these are bad names\n lem2 : \u2200{d \u0394 d'} \u2192 d indet \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem2 IEHole ()\n lem2 (INEHole f) ()\n lem2 (IAp () _) (ITLam _)\n\n lem3 : \u2200{d \u0394 d'} \u2192 d val \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem3 VConst ()\n lem3 VLam ()\n\n lem1 : \u2200{d \u0394 d'} \u2192 d final \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem1 (FVal x) st = lem3 x st\n lem1 (FIndet x) st = lem2 x st\n\n lem4 : \u2200{d \u03b5 x} \u2192 d final \u2192 d == \u03b5 \u27e6 x \u27e7 \u2192 x final\n lem4 f (FHFinal x) = x\n lem4 (FVal ()) (FHAp1 x\u2082 sub)\n lem4 (FIndet (IAp x\u2081 x\u2082)) (FHAp1 x\u2083 sub) = lem4 x\u2082 sub\n lem4 (FVal ()) (FHAp2 sub)\n lem4 (FIndet (IAp x\u2081 x\u2082)) (FHAp2 sub) = lem4 (FIndet x\u2081) sub\n lem4 f FHEHole = f\n lem4 f (FHNEHoleFinal x) = f\n lem4 (FVal ()) (FHCast sub)\n lem4 (FIndet ()) (FHCast sub)\n lem4 f (FHCastFinal x) = f\n lem4 (FVal ()) (FHNEHole y)\n lem4 (FIndet (INEHole x\u2081)) (FHNEHole y) = lem4 x\u2081 y\n\n lem5 : \u2200{d \u0394 d' d'' \u03b5} \u2192 d final \u2192 d == \u03b5 \u27e6 d' \u27e7 \u2192 \u0394 \u22a2 d' \u2192> d'' \u2192 \u22a5\n lem5 f sub step = lem1 (lem4 f sub) step\n\n -- indeterminates and expressions that step are disjoint\n is : \u2200{d \u0394} \u2192 d indet \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n is IEHole (_ , Step (FHFinal x) q _) = lem1 x q\n is IEHole (_ , Step FHEHole () _)\n is (INEHole _) (_ , Step (FHFinal x\u2081) q _) = lem1 x\u2081 q\n is (INEHole x) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = lem5 x x\u2081 x\u2082\n is (INEHole x) (_ , Step (FHNEHoleFinal x\u2081) () FHEHole)\n is (INEHole x) (d , Step (FHNEHoleFinal x\u2081) () (FHFinal x\u2083))\n is (INEHole x) (_ , Step (FHNEHoleFinal x\u2081) () (FHNEHoleFinal x\u2083))\n is (INEHole x) (_ , Step (FHNEHoleFinal x\u2081) () (FHCastFinal x\u2083))\n is (IAp _ _) (_ , Step (FHFinal x\u2081) q _) = lem1 x\u2081 q\n is (IAp _ (FVal x)) (_ , Step (FHAp1 _ p) q (FHAp1 _ r)) = vs x (_ , Step p q r)\n is (IAp _ (FIndet x)) (_ , Step (FHAp1 _ p) q (FHAp1 _ r)) = is x (_ , Step p q r)\n is (IAp i x) (_ , Step (FHAp2 p) q (FHAp2 r)) = is i (_ , (Step p q r))\n\n -- errors and expressions that step are disjoint\n es : \u2200{d \u0394} \u2192 \u0394 \u22a2 d err \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n -- cast error cases\n es (ECastError x x\u2081) (d' , Step (FHFinal x\u2082) x\u2083 x\u2084) = lem1 x\u2082 x\u2083\n es (ECastError x x\u2081) (_ , Step (FHCast x\u2082) x\u2083 (FHCast x\u2084)) = {!!}\n es (ECastError x x\u2081) (d' , Step (FHCastFinal x\u2082) (ITCast x\u2083 x\u2084 x\u2085) x\u2086)\n with type-assignment-unicity x x\u2084\n ... | refl = ~apart x\u2081 x\u2085\n\n -- ap1 cases\n es (EAp1 er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (EAp1 er) (_ , Step (FHAp1 x x\u2081) x\u2082 (FHAp1 x\u2083 x\u2084)) = fe x er\n es (EAp1 er) (_ , Step (FHAp2 x) x\u2081 (FHAp2 x\u2082)) = es er (_ , Step x x\u2081 x\u2082)\n\n -- ap2 cases\n es (EAp2 a er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (EAp2 a er) (_ , Step (FHAp1 x x\u2081) x\u2082 (FHAp1 x\u2083 x\u2084)) = es er (_ , Step x\u2081 x\u2082 x\u2084)\n es (EAp2 a er) (_ , Step (FHAp2 x) x\u2081 (FHAp2 x\u2082)) = lem5 a x x\u2081\n\n -- nehole cases\n es (ENEHole er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (ENEHole er) (_ , Step (FHNEHole a) x (FHNEHole x\u2082)) = es er (_ , Step a x x\u2082)\n es (ENEHole er) (d' , Step (FHNEHoleFinal x) x\u2081 x\u2082) = fe x er\n\n -- castprop cases\n es (ECastProp er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (ECastProp er) (_ , Step (FHCast x) x\u2081 (FHCast x\u2082)) = es er (_ , Step x x\u2081 x\u2082)\n es (ECastProp er) (d' , Step (FHCastFinal x) x\u2081 x\u2082) = fe x er\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import canonical-forms\nopen import type-assignment-unicity\n\n\n-- taken together, the theorems in this file argue that for any expression\n-- d, at most one summand of the labeled sum that results from progress may\n-- be true at any time, i.e. that values, indeterminates, errors, and\n-- expressions that step are pairwise disjoint. (note that as a consequence\n-- of currying and comutativity of products, this means that there are six\n-- theorems to prove)\nmodule progress-checks where\n -- values and indeterminates are disjoint\n vi : \u2200{d} \u2192 d val \u2192 d indet \u2192 \u22a5\n vi VConst ()\n vi VLam ()\n\n -- values and errors are disjoint\n ve : \u2200{d \u0394} \u2192 d val \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n ve VConst ()\n ve VLam ()\n\n -- values and expressions that step are disjoint\n vs : \u2200{d \u0394} \u2192 d val \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n vs VConst (_ , Step (FHFinal _) () (FHFinal _))\n vs VConst (_ , Step (FHFinal _) () FHEHole)\n vs VConst (_ , Step (FHFinal _) () FHNEHoleEvaled)\n vs VConst (_ , Step (FHFinal _) () (FHNEHoleFinal _))\n vs VConst (_ , Step (FHFinal _) () (FHCastFinal _))\n vs VLam (_ , Step (FHFinal _) () (FHFinal _))\n vs VLam (_ , Step (FHFinal _) () FHEHole)\n vs VLam (_ , Step (FHFinal _) () FHNEHoleEvaled)\n vs VLam (_ , Step (FHFinal _) () (FHNEHoleFinal _))\n vs VLam (_ , Step (FHFinal _) () (FHCastFinal _))\n\n mutual\n -- indeterminates and errors are disjoint\n ie : \u2200{d \u0394} \u2192 d indet \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n ie IEHole ()\n ie (INEHole x) (ENEHole e) = fe x e\n ie (IAp i x) (EAp1 e) = ie i e\n ie (IAp i x) (EAp2 y e) = fe x e\n\n -- final expressions are not errors (not one of the 6 cases for progress, just a convenience)\n fe : \u2200{d \u0394} \u2192 d final \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n fe (FVal x) er = ve x er\n fe (FIndet x) er = ie x er\n\n -- todo: these are bad names\n lem2 : \u2200{d \u0394 d'} \u2192 d indet \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem2 IEHole ()\n lem2 (INEHole _) ()\n lem2 (IAp () _) (ITLam _)\n\n lem3 : \u2200{d \u0394 d'} \u2192 d val \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem3 VConst ()\n lem3 VLam ()\n\n lem1 : \u2200{d \u0394 d'} \u2192 d final \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem1 (FVal x) st = lem3 x st\n lem1 (FIndet x) st = lem2 x st\n\n -- indeterminates and expressions that step are disjoint\n is : \u2200{d \u0394} \u2192 d indet \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n is IEHole (_ , Step (FHFinal x) q _) = lem1 x q\n is IEHole (_ , Step FHEHole () (FHFinal _))\n is IEHole (_ , Step FHEHole () FHEHole)\n is IEHole (_ , Step FHEHole () FHNEHoleEvaled)\n is IEHole (_ , Step FHEHole () (FHNEHoleFinal _))\n is IEHole (_ , Step FHEHole () (FHCastFinal _))\n is (INEHole _) (_ , Step (FHFinal x\u2081) q _) = lem1 x\u2081 q\n is (INEHole _) (_ , Step FHNEHoleEvaled () (FHFinal _))\n is (INEHole _) (_ , Step FHNEHoleEvaled () FHEHole)\n is (INEHole _) (_ , Step FHNEHoleEvaled () FHNEHoleEvaled)\n is (INEHole _) (_ , Step FHNEHoleEvaled () (FHNEHoleFinal _))\n is (INEHole _) (_ , Step FHNEHoleEvaled () (FHCastFinal _))\n is (IAp _ _) (_ , Step (FHFinal x\u2081) q _) = lem1 x\u2081 q\n is (IAp _ (FVal x)) (_ , Step (FHAp1 _ p) q (FHAp1 _ r)) = vs x (_ , Step p q r)\n is (IAp _ (FIndet x)) (_ , Step (FHAp1 _ p) q (FHAp1 _ r)) = is x (_ , Step p q r)\n is (IAp i x) (_ , Step (FHAp2 p) q (FHAp2 r)) = is i (_ , (Step p q r))\n\n lem4 : \u2200{d \u03b5 x} \u2192 d final \u2192 d == \u03b5 \u27e6 x \u27e7 \u2192 x final\n lem4 f (FHFinal x) = x\n lem4 (FVal ()) (FHAp1 x\u2082 sub)\n lem4 (FIndet (IAp x\u2081 x\u2082)) (FHAp1 x\u2083 sub) = lem4 x\u2082 sub\n lem4 (FVal ()) (FHAp2 sub)\n lem4 (FIndet (IAp x\u2081 x\u2082)) (FHAp2 sub) = lem4 (FIndet x\u2081) sub\n lem4 f FHEHole = f\n lem4 f FHNEHoleEvaled = f\n lem4 (FVal ()) (FHNEHoleInside sub)\n lem4 (FIndet ()) (FHNEHoleInside sub)\n lem4 f (FHNEHoleFinal x) = f\n lem4 (FVal ()) (FHCast sub)\n lem4 (FIndet ()) (FHCast sub)\n lem4 f (FHCastFinal x) = f\n\n -- errors and expressions that step are disjoint\n es : \u2200{d \u0394} \u2192 \u0394 \u22a2 d err \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n -- cast error cases\n es (ECastError x x\u2081) (d' , Step (FHFinal x\u2082) x\u2083 x\u2084) = lem1 x\u2082 x\u2083\n es (ECastError x x\u2081) (_ , Step (FHCast x\u2082) x\u2083 (FHCast x\u2084)) = {!!}\n es (ECastError x x\u2081) (d' , Step (FHCastFinal x\u2082) (ITCast x\u2083 x\u2084 x\u2085) x\u2086)\n with type-assignment-unicity x x\u2084\n ... | refl = ~apart x\u2081 x\u2085\n\n -- ap1 cases\n es (EAp1 er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (EAp1 er) (_ , Step (FHAp1 x x\u2081) x\u2082 (FHAp1 x\u2083 x\u2084)) = fe x er\n es (EAp1 er) (_ , Step (FHAp2 x) x\u2081 (FHAp2 x\u2082)) = es er (_ , Step x x\u2081 x\u2082)\n\n -- ap2 cases\n es (EAp2 a er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (EAp2 a er) (_ , Step (FHAp1 x x\u2081) x\u2082 (FHAp1 x\u2083 x\u2084)) = es er (_ , Step x\u2081 x\u2082 x\u2084)\n es (EAp2 a er) (_ , Step (FHAp2 x) x\u2081 (FHAp2 x\u2082)) = lem1 (lem4 a x) x\u2081\n\n -- nehole cases\n es (ENEHole er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (ENEHole er) (d' , Step FHNEHoleEvaled () x\u2082)\n es (ENEHole er) (_ , Step (FHNEHoleInside x) x\u2081 (FHNEHoleInside x\u2082)) = es er (_ , Step x x\u2081 x\u2082)\n es (ENEHole er) (d' , Step (FHNEHoleFinal x) x\u2081 x\u2082) = fe x er\n\n -- castprop cases\n es (ECastProp er) (d' , Step (FHFinal x) x\u2081 x\u2082) = lem1 x x\u2081\n es (ECastProp er) (_ , Step (FHCast x) x\u2081 (FHCast x\u2082)) = es er (_ , Step x x\u2081 x\u2082)\n es (ECastProp er) (d' , Step (FHCastFinal x) x\u2081 x\u2082) = fe x er\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"69f6068e034334d162dcf8e4f4cf795c1e52c652","subject":"Fixed doc.","message":"Fixed doc.\n\nIgnore-this: 38884787069d54d1e3561ca859c8a953\n\ndarcs-hash:20120615045929-3bd4e-a154492ae826893a3747789a3772973a10b352f0.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/PA\/Inductive\/Base.agda","new_file":"src\/PA\/Inductive\/Base.agda","new_contents":"------------------------------------------------------------------------------\n-- Inductive Peano arithmetic base\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule PA.Inductive.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfixl 10 _*_\ninfixl 9 _+_\n\n------------------------------------------------------------------------------\n-- PA universe\nopen import PA.Inductive.Base.Core public\n\n-- FOL (without equality)\n--\nopen import Common.FOL.FOL public hiding ( _,_ ; \u2203 )\n-- 2012-04-24. Agda bug? Why it is necessary to use the modifier\n-- @using@ in the following importation?\nopen import PA.Inductive.Existential public using ( _,_ ; \u2203 )\n\n-- The induction principle on the PA universe\n\n-- TODO: 19 May 2012. We don't use an implicit argument for the\n-- inductive step, because it yields some unsolved meta-variables (see\n-- notes\/FOT.PA.Inductive.ImplicitArgumentInductionSL).\nPA-ind : (A : M \u2192 Set) \u2192 A zero \u2192 (\u2200 n \u2192 A n \u2192 A (succ n)) \u2192 \u2200 n \u2192 A n\nPA-ind A A0 h zero = A0\nPA-ind A A0 h (succ n) = h n (PA-ind A A0 h n)\n\n-- The identity type on the PA universe\nopen import PA.Inductive.Relation.Binary.PropositionalEquality public\n\n-- PA primitive recursive functions\n_+_ : M \u2192 M \u2192 M\nzero + n = n\nsucc m + n = succ (m + n)\n\n_*_ : M \u2192 M \u2192 M\nzero * n = zero\nsucc m * n = n + m * n\n\n------------------------------------------------------------------------------\n-- ATPs helper\n-- We don't traslate the body of functions, only the types. Therefore\n-- we need to feed the ATPs with the functions' equations.\n\n-- Addition axioms\n+-0x : \u2200 n \u2192 zero + n \u2261 n\n+-0x n = refl\n-- {-# ATP hint +-0x #-}\n\n+-Sx : \u2200 m n \u2192 succ m + n \u2261 succ (m + n)\n+-Sx m n = refl\n{-# ATP hint +-Sx #-}\n\n-- Multiplication axioms\n*-0x : \u2200 n \u2192 zero * n \u2261 zero\n*-0x n = refl\n-- {-# ATP hint *-0x #-}\n\n*-Sx : \u2200 m n \u2192 succ m * n \u2261 n + m * n\n*-Sx m n = refl\n-- {-# ATP hint *-Sx #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Inductive Peano arithmetic base\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule PA.Inductive.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfixl 10 _*_\ninfixl 9 _+_\n\n------------------------------------------------------------------------------\n-- PA universe\nopen import PA.Inductive.Base.Core public\n\n-- FOL (without equality)\n--\nopen import Common.FOL.FOL public hiding ( _,_ ; \u2203 )\n-- 2012-04-24. Agda bug? Why it is necessary to use the modifier\n-- @using@ in the following importation?\nopen import PA.Inductive.Existential public using ( _,_ ; \u2203 )\n\n-- The induction principle on the PA universe\n\n-- TODO: 19 May 2012. We don't use an implicit argument for the\n-- inductive step, because it yields some unsolved meta-variables (see\n-- Draft.PA.Inductive.ImplicitArgumentInductionSL).\nPA-ind : (A : M \u2192 Set) \u2192 A zero \u2192 (\u2200 n \u2192 A n \u2192 A (succ n)) \u2192 \u2200 n \u2192 A n\nPA-ind A A0 h zero = A0\nPA-ind A A0 h (succ n) = h n (PA-ind A A0 h n)\n\n-- The identity type on the PA universe\nopen import PA.Inductive.Relation.Binary.PropositionalEquality public\n\n-- PA primitive recursive functions\n_+_ : M \u2192 M \u2192 M\nzero + n = n\nsucc m + n = succ (m + n)\n\n_*_ : M \u2192 M \u2192 M\nzero * n = zero\nsucc m * n = n + m * n\n\n------------------------------------------------------------------------------\n-- ATPs helper\n-- We don't traslate the body of functions, only the types. Therefore\n-- we need to feed the ATPs with the functions' equations.\n\n-- Addition axioms\n+-0x : \u2200 n \u2192 zero + n \u2261 n\n+-0x n = refl\n-- {-# ATP hint +-0x #-}\n\n+-Sx : \u2200 m n \u2192 succ m + n \u2261 succ (m + n)\n+-Sx m n = refl\n{-# ATP hint +-Sx #-}\n\n-- Multiplication axioms\n*-0x : \u2200 n \u2192 zero * n \u2261 zero\n*-0x n = refl\n-- {-# ATP hint *-0x #-}\n\n*-Sx : \u2200 m n \u2192 succ m * n \u2261 n + m * n\n*-Sx m n = refl\n-- {-# ATP hint *-Sx #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"eea966329ff9f953b7330cd87ef1188ffe40f0f0","subject":"Correct caching for constants (by delegation)","message":"Correct caching for constants (by delegation)\n\nThis defines the plugin interface for constants (with a type that makes\nsemantic sense), and delegates to that, while stipulating:\n* what are the appropriate types;\n* that constant arguments needn't be cached because they are values.\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Denotation\/CachingEvaluation.agda","new_file":"Parametric\/Denotation\/CachingEvaluation.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Caching evaluation\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\n\nimport Parametric.Syntax.MType as MType\nimport Parametric.Syntax.MTerm as MTerm\n\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Denotation.MValue as MValue\nimport Parametric.Denotation.CachingMValue as CachingMValue\nimport Parametric.Denotation.MEvaluation as MEvaluation\n\nmodule Parametric.Denotation.CachingEvaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (ValConst : MTerm.ValConstStructure Const)\n (CompConst : MTerm.CompConstStructure Const)\n -- I should really switch to records - can it get sillier than this?\n (\u27e6_\u27e7ValBase : MEvaluation.ValStructure Const \u27e6_\u27e7Base ValConst CompConst)\n (\u27e6_\u27e7CompBase : MEvaluation.CompStructure Const \u27e6_\u27e7Base ValConst CompConst)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\n\nopen MType.Structure Base\nopen MTerm.Structure Const ValConst CompConst\n\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen MValue.Structure Base \u27e6_\u27e7Base\nopen CachingMValue.Structure Base \u27e6_\u27e7Base\nopen MEvaluation.Structure Const \u27e6_\u27e7Base ValConst CompConst \u27e6_\u27e7ValBase \u27e6_\u27e7CompBase\n\nopen import Base.Denotation.Notation\n\n-- Extension Point: Evaluation of fully applied constants.\nValStructure : Set\nValStructure = \u2200 {\u03a3 \u03c4} \u2192 ValConst \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n\nCompStructure : Set\nCompStructure = \u2200 {\u03a3 \u03c4} \u2192 CompConst \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n\nmodule Structure\n (\u27e6_\u27e7ValBaseTermCache : ValStructure)\n (\u27e6_\u27e7CompBaseTermCache : CompStructure)\n where\n\n {-\n -- Prototype here the type-correctness of a simple non-standard semantics.\n -- This describes a simplified version of the transformation by Liu and\n -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language\n -- terms, we produce host language terms to take advantage of the richer type\n -- system of the host language (in particular, here we need the unit type,\n -- product types and *existentials*).\n --\n -- As usual, we'll later switch to a real term transformation.\n -}\n open import Data.Product hiding (map)\n open import Data.Unit\n\n -- Defining a caching semantics for Term proves to be hard, requiring to\n -- insert and remove caches where we apply constants.\n\n -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.\n\n -- The solution is to distinguish among different kinds of constants. Some are\n -- value constructors (and thus do not return caches), while others are\n -- computation constructors (and thus should return caches). For products, I\n -- believe we will only use the products which are values, not computations\n -- (XXX check CBPV paper for the name).\n \u27e6_\u27e7CompTermCache : \u2200 {\u03c4 \u0393} \u2192 Comp \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n \u27e6_\u27e7ValTermCache : \u2200 {\u03c4 \u0393} \u2192 Val \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n \u27e6_\u27e7ValsTermCache : \u2200 {\u0393 \u03a3} \u2192 Vals \u0393 \u03a3 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache\n\n open import Base.Denotation.Environment ValType \u27e6_\u27e7ValTypeHidCache public\n using ()\n renaming (\u27e6_\u27e7Var to \u27e6_\u27e7ValVarHidCache)\n\n -- This says that the environment does not contain caches... sounds wrong!\n -- Either we add extra variables for the caches, or we store computations in\n -- the environment (but that does not make sense), or we store caches in\n -- values, by acting not on F but on something else (U?).\n\n open import UNDEFINED\n\n -- Copy of \u27e6_\u27e7Vals\n \u27e6 \u2205 \u27e7ValsTermCache \u03c1 = \u2205\n \u27e6 vt \u2022 valtms \u27e7ValsTermCache \u03c1 = \u27e6 vt \u27e7ValTermCache \u03c1 \u2022 \u27e6 valtms \u27e7ValsTermCache \u03c1\n\n -- I suspect the plan was to use extra variables; that's annoying to model in\n -- Agda but easier in implementations.\n\n \u27e6 vVar x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7ValVarHidCache \u03c1\n \u27e6 vThunk x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7CompTermCache \u03c1\n -- No caching, because the arguments are values, so evaluating them does not\n -- produce intermediate results.\n \u27e6 vConst c args \u27e7ValTermCache \u03c1 = \u27e6 c \u27e7ValBaseTermCache (\u27e6 args \u27e7ValsTermCache \u03c1)\n\n -- The only caching is done by the interpretation of the constant (because the\n -- arguments are values so need no caching).\n \u27e6_\u27e7CompTermCache (cConst c args) \u03c1 = \u27e6 c \u27e7CompBaseTermCache (\u27e6 args \u27e7ValsTermCache \u03c1)\n\n -- XXX constants are still a slight mess because I'm abusing CBPV...\n -- (Actually, I just forgot the difference, and believe I had too little clue\n -- when I wrote these constructors... but some of them did make sense).\n \u27e6_\u27e7CompTermCache (cConstB c args) \u03c1 = reveal UNDEFINED\n \u27e6_\u27e7CompTermCache (cConstVB c args) \u03c1 = reveal UNDEFINED\n \u27e6_\u27e7CompTermCache (cConstVB2 c args) \u03c1 = reveal UNDEFINED\n\n -- Also, where are introduction forms for pairs and sums among values? With\n -- them, we should see that we can interpret them without adding a cache.\n\n -- Thunks keep seeming noops.\n \u27e6_\u27e7CompTermCache (cForce x) \u03c1 = \u27e6 x \u27e7ValTermCache \u03c1\n\n -- Here, in an actual implementation, we would return the actual cache with\n -- all variables.\n --\n -- The effect of F is a writer monad of cached values, where the monoid is\n -- (isomorphic to) the free monoid over (\u2203 \u03c4 . \u03c4), but we push the\n -- existentials up when pairing things!\n\n -- That's what we're interpreting computations in. XXX maybe consider using\n -- monads directly. But that doesn't deal with arity.\n \u27e6_\u27e7CompTermCache (cReturn v) \u03c1 = vUnit , (\u27e6 v \u27e7ValTermCache \u03c1 , tt)\n\n -- For this to be enough, a lambda needs to return a produce, not to forward\n -- the underlying one (unless there are no intermediate results). The correct\n -- requirements can maybe be enforced through a linear typing discipline.\n\n {-\n -- Here we'd have a problem with the original into from CBPV, because it does\n -- not require converting expressions to the \"CBPV A-normal form\".\n\n \u27e6_\u27e7CompTermCache (v\u2081 into v\u2082) \u03c1 =\n -- Sequence commands and combine their caches.\n {-\n let (_ , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (_ , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in , (r\u2082 , (c\u2081 ,\u2032 c\u2082))\n -}\n\n -- The code above does not work, because we only guarantee that v\u2082 is\n -- a computation, not that it's an F-computation - v\u2082 could also be function\n -- type or a computation product.\n\n -- We need a restricted CBPV, where these two possibilities are forbidden. If\n -- you want to save something between different lambdas, you need to add an F\n -- U to reflect that. (Double-check the papers which show how to encode arity\n -- using CBPV, it seems that they should find the same problem --- XXX they\n -- don't).\n\n -- Instead, just drop the first cache (XXX WRONG).\n \u27e6 v\u2082 \u27e7CompTermCache (proj\u2081 (proj\u2082 (\u27e6 v\u2081 \u27e7CompTermCache \u03c1)) \u2022 \u03c1)\n -}\n\n -- But if we alter _into_ as described above, composing the caches works!\n -- However, we should not forget we also need to save the new intermediate\n -- result, that is the one produced by the first part of the let.\n \u27e6_\u27e7CompTermCache (_into_ {\u03c3} {\u03c4} v\u2081 v\u2082) \u03c1 =\n let (\u03c4\u2081 , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (\u03c4\u2082 , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in (\u03c3 v\u00d7 \u03c4\u2081 v\u00d7 \u03c4\u2082) , (r\u2082 ,\u2032 ( r\u2081 ,\u2032 c\u2081 ,\u2032 c\u2082))\n\n -- Note the compositionality and luck: we don't need to do anything at the\n -- cReturn time, we just need the nested into to do their job, because as I\n -- said intermediate results are a writer monad.\n --\n -- Q: But then, do we still need to do all the other stuff? IOW, do we still\n -- need to forbid (\u03bb x . y <- f args; g args') and replace it with (\u03bb x . y <-\n -- f args; z <- g args'; z)?\n --\n -- A: One thing we still need is using the monadic version of into for the\n -- same reasons - which makes sense, since it has the type of monadic bind.\n --\n -- Maybe not: if we use a monad, which respects left and right identity, the\n -- two above forms are equivalent. But what about associativity? We don't have\n -- associativity with nested tuples in the middle. That's why the monad uses\n -- lists! We can also use nested tuple, as long as in the into case we don't\n -- do (a, b) but append a b (ahem, where?), which decomposes the first list\n -- and prepends it to the second. To this end, we need to know the type of the\n -- first element, or to ensure it's always a pair. Maybe we just want to reuse\n -- an HList.\n\n -- In abstractions, we should start collecting all variables...\n\n -- Here, unlike in \u27e6_\u27e7TermCache, we don't need to invent an empty cache,\n -- that's moved into the handling of cReturn. This makes *the* difference for\n -- nested lambdas, where we don't need to create caches multiple times!\n\n \u27e6_\u27e7CompTermCache (cAbs v) \u03c1 = \u03bb x \u2192 \u27e6 v \u27e7CompTermCache (x \u2022 \u03c1)\n\n -- Here we see that we are in a sort of A-normal form, because the argument is\n -- a value (not quite ANF though, since values can be thunks - that is,\n -- computations which haven't been run yet, I guess. Do we have an use for\n -- that? That allows passing lambdas as arguments directly - which is fine,\n -- because producing a closure indeed does not have intermediate results!).\n \u27e6_\u27e7CompTermCache (cApp t v) \u03c1 = \u27e6 t \u27e7CompTermCache \u03c1 (\u27e6 v \u27e7ValTermCache \u03c1)\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Caching evaluation\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\n\nimport Parametric.Syntax.MType as MType\nimport Parametric.Syntax.MTerm as MTerm\n\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Denotation.MValue as MValue\nimport Parametric.Denotation.CachingMValue as CachingMValue\nimport Parametric.Denotation.MEvaluation as MEvaluation\n\nmodule Parametric.Denotation.CachingEvaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (ValConst : MTerm.ValConstStructure Const)\n (CompConst : MTerm.CompConstStructure Const)\n -- I should really switch to records - can it get sillier than this?\n (\u27e6_\u27e7ValBase : MEvaluation.ValStructure Const \u27e6_\u27e7Base ValConst CompConst)\n (\u27e6_\u27e7CompBase : MEvaluation.CompStructure Const \u27e6_\u27e7Base ValConst CompConst)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\n\nopen MType.Structure Base\nopen MTerm.Structure Const ValConst CompConst\n\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen MValue.Structure Base \u27e6_\u27e7Base\nopen CachingMValue.Structure Base \u27e6_\u27e7Base\nopen MEvaluation.Structure Const \u27e6_\u27e7Base ValConst CompConst \u27e6_\u27e7ValBase \u27e6_\u27e7CompBase\n\nopen import Base.Denotation.Notation\n\nopen import Relation.Binary.PropositionalEquality\n\nmodule Structure where\n {-\n -- Prototype here the type-correctness of a simple non-standard semantics.\n -- This describes a simplified version of the transformation by Liu and\n -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language\n -- terms, we produce host language terms to take advantage of the richer type\n -- system of the host language (in particular, here we need the unit type,\n -- product types and *existentials*).\n --\n -- As usual, we'll later switch to a real term transformation.\n -}\n open import Data.Product hiding (map)\n open import Data.Unit\n\n -- Defining a caching semantics for Term proves to be hard, requiring to\n -- insert and remove caches where we apply constants.\n\n -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.\n\n -- The solution is to distinguish among different kinds of constants. Some are\n -- value constructors (and thus do not return caches), while others are\n -- computation constructors (and thus should return caches). For products, I\n -- believe we will only use the products which are values, not computations\n -- (XXX check CBPV paper for the name).\n \u27e6_\u27e7CompTermCache : \u2200 {\u03c4 \u0393} \u2192 Comp \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n \u27e6_\u27e7ValTermCache : \u2200 {\u03c4 \u0393} \u2192 Val \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n \u27e6_\u27e7ValsTermCache : \u2200 {\u0393 \u03a3} \u2192 Vals \u0393 \u03a3 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache\n\n open import Base.Denotation.Environment ValType \u27e6_\u27e7ValTypeHidCache public\n using ()\n renaming (\u27e6_\u27e7Var to \u27e6_\u27e7ValVarHidCache)\n\n -- This says that the environment does not contain caches... sounds wrong!\n -- Either we add extra variables for the caches, or we store computations in\n -- the environment (but that does not make sense), or we store caches in\n -- values, by acting not on F but on something else (U?).\n\n open import UNDEFINED\n\n -- Copy of \u27e6_\u27e7Vals\n \u27e6 \u2205 \u27e7ValsTermCache \u03c1 = \u2205\n \u27e6 vt \u2022 valtms \u27e7ValsTermCache \u03c1 = \u27e6 vt \u27e7ValTermCache \u03c1 \u2022 \u27e6 valtms \u27e7ValsTermCache \u03c1\n\n -- I suspect the plan was to use extra variables; that's annoying to model in\n -- Agda but easier in implementations.\n\n \u27e6 vVar x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7ValVarHidCache \u03c1\n \u27e6 vThunk x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7CompTermCache \u03c1\n -- TODO No caching. Uh??? What about the intermediate results of the\n -- arguments? Ah, they're values, so they aren't interesting (or they're\n -- already cached). Yu-uh!\n\n -- XXX We need a caching ValBase, so the caching type semantics needs to move.\n \u27e6 vConst c args \u27e7ValTermCache \u03c1 = reveal UNDEFINED -- {!\u27e6 c \u27e7ValBase !}\n\n -- The real deal, finally.\n -- TODO Do caching. No! Delegate to caching version of the base semantics!\n \u27e6_\u27e7CompTermCache (cConst c args) \u03c1 = reveal UNDEFINED -- {!\u27e6 c \u27e7CompBase (\u27e6 args \u27e7Vals ?)!}\n\n -- XXX constants are still a slight mess because I'm abusing CBPV...\n -- (Actually, I just forgot the difference, and believe I had too little clue\n -- when I wrote these constructors... but some of them did make sense).\n \u27e6_\u27e7CompTermCache (cConstB c args) \u03c1 = reveal UNDEFINED\n \u27e6_\u27e7CompTermCache (cConstVB c args) \u03c1 = reveal UNDEFINED\n \u27e6_\u27e7CompTermCache (cConstVB2 c args) \u03c1 = reveal UNDEFINED\n\n -- Also, where are introduction forms for pairs and sums among values? With\n -- them, we should see that we can interpret them without adding a cache.\n\n -- Thunks keep seeming noops.\n \u27e6_\u27e7CompTermCache (cForce x) \u03c1 = \u27e6 x \u27e7ValTermCache \u03c1\n\n -- Here, in an actual implementation, we would return the actual cache with\n -- all variables.\n --\n -- The effect of F is a writer monad of cached values, where the monoid is\n -- (isomorphic to) the free monoid over (\u2203 \u03c4 . \u03c4), but we push the\n -- existentials up when pairing things!\n\n -- That's what we're interpreting computations in. XXX maybe consider using\n -- monads directly. But that doesn't deal with arity.\n \u27e6_\u27e7CompTermCache (cReturn v) \u03c1 = vUnit , (\u27e6 v \u27e7ValTermCache \u03c1 , tt)\n\n -- For this to be enough, a lambda needs to return a produce, not to forward\n -- the underlying one (unless there are no intermediate results). The correct\n -- requirements can maybe be enforced through a linear typing discipline.\n\n {-\n -- Here we'd have a problem with the original into from CBPV, because it does\n -- not require converting expressions to the \"CBPV A-normal form\".\n\n \u27e6_\u27e7CompTermCache (v\u2081 into v\u2082) \u03c1 =\n -- Sequence commands and combine their caches.\n {-\n let (_ , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (_ , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in , (r\u2082 , (c\u2081 ,\u2032 c\u2082))\n -}\n\n -- The code above does not work, because we only guarantee that v\u2082 is\n -- a computation, not that it's an F-computation - v\u2082 could also be function\n -- type or a computation product.\n\n -- We need a restricted CBPV, where these two possibilities are forbidden. If\n -- you want to save something between different lambdas, you need to add an F\n -- U to reflect that. (Double-check the papers which show how to encode arity\n -- using CBPV, it seems that they should find the same problem --- XXX they\n -- don't).\n\n -- Instead, just drop the first cache (XXX WRONG).\n \u27e6 v\u2082 \u27e7CompTermCache (proj\u2081 (proj\u2082 (\u27e6 v\u2081 \u27e7CompTermCache \u03c1)) \u2022 \u03c1)\n -}\n\n -- But if we alter _into_ as described above, composing the caches works!\n -- However, we should not forget we also need to save the new intermediate\n -- result, that is the one produced by the first part of the let.\n \u27e6_\u27e7CompTermCache (_into_ {\u03c3} {\u03c4} v\u2081 v\u2082) \u03c1 =\n let (\u03c4\u2081 , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (\u03c4\u2082 , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in (\u03c3 v\u00d7 \u03c4\u2081 v\u00d7 \u03c4\u2082) , (r\u2082 ,\u2032 ( r\u2081 ,\u2032 c\u2081 ,\u2032 c\u2082))\n\n -- Note the compositionality and luck: we don't need to do anything at the\n -- cReturn time, we just need the nested into to do their job, because as I\n -- said intermediate results are a writer monad.\n --\n -- Q: But then, do we still need to do all the other stuff? IOW, do we still\n -- need to forbid (\u03bb x . y <- f args; g args') and replace it with (\u03bb x . y <-\n -- f args; z <- g args'; z)?\n --\n -- A: One thing we still need is using the monadic version of into for the\n -- same reasons - which makes sense, since it has the type of monadic bind.\n --\n -- Maybe not: if we use a monad, which respects left and right identity, the\n -- two above forms are equivalent. But what about associativity? We don't have\n -- associativity with nested tuples in the middle. That's why the monad uses\n -- lists! We can also use nested tuple, as long as in the into case we don't\n -- do (a, b) but append a b (ahem, where?), which decomposes the first list\n -- and prepends it to the second. To this end, we need to know the type of the\n -- first element, or to ensure it's always a pair. Maybe we just want to reuse\n -- an HList.\n\n -- In abstractions, we should start collecting all variables...\n\n -- Here, unlike in \u27e6_\u27e7TermCache, we don't need to invent an empty cache,\n -- that's moved into the handling of cReturn. This makes *the* difference for\n -- nested lambdas, where we don't need to create caches multiple times!\n\n \u27e6_\u27e7CompTermCache (cAbs v) \u03c1 = \u03bb x \u2192 \u27e6 v \u27e7CompTermCache (x \u2022 \u03c1)\n\n -- Here we see that we are in a sort of A-normal form, because the argument is\n -- a value (not quite ANF though, since values can be thunks - that is,\n -- computations which haven't been run yet, I guess. Do we have an use for\n -- that? That allows passing lambdas as arguments directly - which is fine,\n -- because producing a closure indeed does not have intermediate results!).\n \u27e6_\u27e7CompTermCache (cApp t v) \u03c1 = \u27e6 t \u27e7CompTermCache \u03c1 (\u27e6 v \u27e7ValTermCache \u03c1)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"308d57f744384b21a2811f278b455dfb952dd324","subject":"Define \u0394Const for Popl14 and fill in calculus-with","message":"Define \u0394Const for Popl14 and fill in calculus-with\n\nOld-commit-hash: c1cdbc71d45afdb85da9ac22b3207d1cb03e0bf7\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Language\/Popl14.agda","new_file":"Syntax\/Language\/Popl14.agda","new_contents":"module Syntax.Language.Popl14 where\n\nopen import Syntax.Term.Popl14\nopen import Syntax.Context.Plotkin Popl14-type\n\nopen import Data.Integer\n\nimport Syntax.Language.Calculus as Calc\n\n\u0394Const : \u2200 {\u0393 \u03a3 \u03c4} \u2192\n Const \u03a3 \u03c4 \u2192\n Term \u0393\n (internalizeContext\n (Calc.\u0394Context\u2032 (Calc.Type Popl14-type) \u0394Type \u03a3) (\u0394Type \u03c4))\n\n-- These helpers hide deBrujin indexes, providing an interface which is as\n-- comfortable as HOAS. This should be generalized and moved to Syntax.Term.Plotkin\n\n\u0394Const (intlit-c n) = intlit (+ 0)\n\u0394Const add-c = abs\u2084 (\u03bb x \u0394x y \u0394y \u2192 add \u0394x \u0394y)\n\u0394Const minus-c = abs\u2082 (\u03bb x \u0394x \u2192 minus \u0394x)\n\u0394Const empty-c = empty\n\u0394Const insert-c = abs\u2084 (\u03bb x \u0394x y \u0394y \u2192 insert (x \u2295 \u0394x) (y \u2295 \u0394y) \u229d insert x y)\n\u0394Const union-c = abs\u2084 (\u03bb x \u0394x y \u0394y \u2192 union \u0394x \u0394y)\n\u0394Const negate-c = abs\u2082 (\u03bb x \u0394x \u2192 negate \u0394x)\n\u0394Const flatmap-c = abs\u2084 (\u03bb x \u0394x y \u0394y \u2192 flatmap (x \u2295 \u0394x) (y \u2295 \u0394y) \u229d flatmap x y)\n\u0394Const sum-c = abs\u2082 (\u03bb x \u0394x \u2192 sum \u0394x)\n\nPopl14 = Calc.calculus-with\n Popl14-type\n Const\n \u0394Type\n \u0394Const\n","old_contents":"module Syntax.Language.Popl14 where\n\nopen import Syntax.Term.Popl14\nopen import Syntax.Context.Plotkin Popl14-type\n\nopen import Data.Integer\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"450dbdd83ddf95d4b2095ee7d8ea56a92b5c2302","subject":"Cleaning.","message":"Cleaning.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/Functors.agda","new_file":"notes\/fixed-points\/Functors.agda","new_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Based on (Vene, 2000).\n\nmodule Functors where\n\ninfixr 1 _+_\ninfixr 2 _\u00d7_\n\ndata Bool : Set where\n false true : Bool\n\ndata _+_ (A B : Set) : Set where\n inl : A \u2192 A + B\n inr : B \u2192 A + B\n\ndata _\u00d7_ (A B : Set) : Set where\n _,_ : A \u2192 B \u2192 A \u00d7 B\n\n-- The terminal object.\ndata \u22a4 : Set where\n <> : \u22a4\n\npostulate\n -- The least fixed-point.\n\n -- Haskell definitions:\n\n -- data Mu f = In (f (Mu f))\n\n -- unIn :: Mu f \u2192 f (Mu f)\n -- unIn (In x) = x\n\n \u03bc : (Set \u2192 Set) \u2192 Set\n In : {F : Set \u2192 Set} \u2192 F (\u03bc F) \u2192 \u03bc F\n unIn : {F : Set \u2192 Set} \u2192 \u03bc F \u2192 F (\u03bc F)\n\npostulate\n -- The greatest fixed-point.\n\n -- Haskell definitions:\n\n -- data Nu f = Wrap (f (Nu f))\n\n -- out :: Nu f \u2192 (f (Nu f))\n -- out (Wrap x) = x\n\n \u03bd : (Set \u2192 Set) \u2192 Set\n Wrap : {F : Set \u2192 Set} \u2192 F (\u03bd F) \u2192 \u03bd F\n out : {F : Set \u2192 Set} \u2192 \u03bd F \u2192 F (\u03bd F)\n\n------------------------------------------------------------------------------\n-- Functors\n\n-- The identity functor (the functor for the empty and unit types).\nIdF : Set \u2192 Set\nIdF X = X\n\n-- The (co)natural numbers functor.\nNatF : Set \u2192 Set\nNatF X = \u22a4 + X\n\n-- The (co)list functor.\nListF : Set \u2192 Set \u2192 Set\nListF A X = \u22a4 + A \u00d7 X\n\n-- The stream functor.\nStreamF : Set \u2192 Set \u2192 Set\nStreamF A X = A \u00d7 X\n\n------------------------------------------------------------------------------\n-- Types as least fixed-points\n\n-- The empty type is a least fixed-point.\n\u22a5 : Set\n\u22a5 = \u03bc IdF\n\n-- The natural numbers type is a least fixed-point.\nN : Set\nN = \u03bc NatF\n\n-- The data constructors for the natural numbers.\nzero : N\nzero = In (inl <>)\n\nsucc : N \u2192 N\nsucc n = In (inr n)\n\n-- The list type is a least fixed-point.\nList : Set \u2192 Set\nList A = \u03bc (ListF A)\n\n-- The data constructors for List.\nnil : {A : Set} \u2192 List A\nnil = In (inl <>)\n\ncons : {A : Set} \u2192 A \u2192 List A \u2192 List A\ncons x xs = In (inr (x , xs))\n\n------------------------------------------------------------------------------\n-- Types as greatest fixed-points\n\n-- The unit type is a greatest fixed-point.\nUnit : Set\nUnit = \u03bd IdF\n\n-- Non-structural recursion\n-- unit : Nu IdF\n-- unit = Wrap IdF {!unit!}\n\n-- The conat type is a greatest fixed-point.\nConat : Set\nConat = \u03bd NatF\n\nzeroC : Conat\nzeroC = Wrap (inl <>)\n\nsuccC : Conat \u2192 Conat\nsuccC cn = Wrap (inr cn)\n\n-- Non-structural recursion for the definition of \u221eC.\n-- \u221eC : Conat\n-- \u221eC = succC {!\u221eC!}\n\n-- The pred function is the conat destructor.\npred : Conat \u2192 \u22a4 + Conat\npred cn with out cn\n... | inl _ = inl <>\n... | inr x = inr x\n\n-- The colist type is a greatest fixed-point.\nColist : Set \u2192 Set\nColist A = \u03bd (ListF A)\n\n-- The colist data constructors.\nnilCL : {A : Set} \u2192 Colist A\nnilCL = Wrap (inl <>)\n\nconsCL : {A : Set} \u2192 A \u2192 Colist A \u2192 Colist A\nconsCL x xs = Wrap (inr (x , xs))\n\n-- The colist destructors.\nnullCL : {A : Set} \u2192 Colist A \u2192 Bool\nnullCL xs with out xs\n... | inl _ = true\n... | inr _ = false\n\n-- headCL : {A : Set} \u2192 Colist A \u2192 A\n-- headCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (x , _) = x\n\n-- tailCL : {A : Set} \u2192 Colist A \u2192 Colist A\n-- tailCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (_ , xs') = xs'\n\n-- The stream type is a greatest fixed-point.\nStream : Set \u2192 Set\nStream A = \u03bd (StreamF A)\n\n-- The stream data constructor.\nconsS : {A : Set} \u2192 A \u2192 Stream A \u2192 Stream A\nconsS x xs = Wrap (x , xs)\n\n-- The stream destructors.\nheadS : {A : Set} \u2192 Stream A \u2192 A\nheadS xs with out xs\n... | x , _ = x\n\ntailS : {A : Set} \u2192 Stream A \u2192 Stream A\ntailS xs with out xs\n... | _ , xs' = xs'\n\n-- From (Leclerc and Paulin-Mohring 1994, p. 195).\n--\n-- TODO (07 January 2014): Agda doesn't accept the definition of\n-- Stream-build.\n{-# NO_TERMINATION_CHECK #-}\nStream-build :\n {A X : Set} \u2192\n (X \u2192 StreamF A X) \u2192\n X \u2192 Stream A\nStream-build h x with h x\n... | a , x' = Wrap (a , Stream-build h x')\n\n-- From (Gim\u00e9nez, 1995, p. 40).\n--\n-- TODO (07 January 2014): Agda doesn't accept the definition of\n-- Stream-corec.\n{-# NO_TERMINATION_CHECK #-}\nStream-corec :\n {A X : Set} \u2192\n (X \u2192 (A \u00d7 (Stream A + X))) \u2192\n X \u2192 Stream A\nStream-corec h x with h x\n... | a , inl xs = Wrap (a , xs)\n... | a , inr x' = Wrap (a , (Stream-corec h x'))\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Gim\u00e9nez, E. (1995). Codifying guarded de\ufb01nitions with recursive\n-- schemes. In: Types for Proofs and Programs (TYPES \u201994). Ed. by\n-- Dybjer, P., Nordstr\u00f6m, B. and Smith, J. Vol. 996. LNCS. Springer,\n-- pp. 39\u201359.\n--\n-- Leclerc, F. and Paulin-Mohring, C. (1994). Programming with Streams\n-- in Coq. A case study : the Sieve of Eratosthenes. In: Types for\n-- Proofs and Programs (TYPES \u201993). Ed. by Barendregt, H. and Nipkow,\n-- T. Vol. 806. LNCS. Springer, pp. 191\u2013212.\n--\n-- Vene, Varmo (2000). Categorical programming with inductive and\n-- coinductive types. PhD thesis. Faculty of Mathematics: University\n-- of Tartu.\n","old_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Based on (Vene, 2000).\n\nmodule Functors where\n\ninfixr 1 _+_\ninfixr 2 _\u00d7_\n\ndata Bool : Set where\n false true : Bool\n\ndata _+_ (A B : Set) : Set where\n inl : A \u2192 A + B\n inr : B \u2192 A + B\n\ndata _\u00d7_ (A B : Set) : Set where\n _,_ : A \u2192 B \u2192 A \u00d7 B\n\n-- The terminal object.\ndata \u22a4 : Set where\n <> : \u22a4\n\npostulate\n -- The least fixed-point.\n\n -- Haskell definitions:\n\n -- data Mu f = In (f (Mu f))\n\n -- unIn :: Mu f \u2192 f (Mu f)\n -- unIn (In x) = x\n\n \u03bc : (Set \u2192 Set) \u2192 Set\n In : {F : Set \u2192 Set} \u2192 F (\u03bc F) \u2192 \u03bc F\n unIn : {F : Set \u2192 Set} \u2192 \u03bc F \u2192 F (\u03bc F)\n\npostulate\n -- The greatest fixed-point.\n\n -- Haskell definitions:\n\n -- data Nu f = Wrap (f (Nu f))\n\n -- out :: Nu f \u2192 (f (Nu f))\n -- out (Wrap x) = x\n\n \u03bd : (Set \u2192 Set) \u2192 Set\n Wrap : {F : Set \u2192 Set} \u2192 F (\u03bd F) \u2192 \u03bd F\n out : {F : Set \u2192 Set} \u2192 \u03bd F \u2192 F (\u03bd F)\n\n------------------------------------------------------------------------------\n-- Functors\n\n-- The identity functor (the functor for the empty and unit types).\nIdF : Set \u2192 Set\nIdF X = X\n\n-- The (co)natural numbers functor.\nNatF : Set \u2192 Set\nNatF X = \u22a4 + X\n\n-- The (co)list functor.\nListF : Set \u2192 Set \u2192 Set\nListF A X = \u22a4 + A \u00d7 X\n\n-- The stream functor.\nStreamF : Set \u2192 Set \u2192 Set\nStreamF A X = A \u00d7 X\n\n------------------------------------------------------------------------------\n-- Types as least fixed-points\n\n-- The empty type is a least fixed-point.\n\u22a5 : Set\n\u22a5 = \u03bc IdF\n\n-- The natural numbers type is a least fixed-point.\nN : Set\nN = \u03bc NatF\n\n-- The data constructors for the natural numbers.\nzero : N\nzero = In (inl <>)\n\nsucc : N \u2192 N\nsucc n = In (inr n)\n\nN-ind : (A : N \u2192 Set) \u2192\n A zero \u2192\n (\u2200 n \u2192 A n \u2192 A (succ n)) \u2192\n \u2200 n \u2192 A n\nN-ind A A0 h n = {!!}\n\n-- The list type is a least fixed-point.\nList : Set \u2192 Set\nList A = \u03bc (ListF A)\n\n-- The data constructors for List.\nnil : {A : Set} \u2192 List A\nnil = In (inl <>)\n\ncons : {A : Set} \u2192 A \u2192 List A \u2192 List A\ncons x xs = In (inr (x , xs))\n\nList-ind : {A : Set}(B : List A \u2192 Set) \u2192\n B nil \u2192\n (\u2200 {x} (xs : List A) \u2192 B xs \u2192 B (cons x xs)) \u2192\n (xs : List A) \u2192 B xs\nList-ind B Bnil h xs = {!!}\n\n------------------------------------------------------------------------------\n-- Types as greatest fixed-points\n\n-- The unit type is a greatest fixed-point.\nUnit : Set\nUnit = \u03bd IdF\n\n-- Non-structural recursion\n-- unit : Nu IdF\n-- unit = Wrap IdF {!unit!}\n\n-- The conat type is a greatest fixed-point.\nConat : Set\nConat = \u03bd NatF\n\nzeroC : Conat\nzeroC = Wrap (inl <>)\n\nsuccC : Conat \u2192 Conat\nsuccC cn = Wrap (inr cn)\n\n-- Non-structural recursion for the definition of \u221eC.\n-- \u221eC : Conat\n-- \u221eC = succC {!\u221eC!}\n\n-- The pred function is the conat destructor.\npred : Conat \u2192 \u22a4 + Conat\npred cn with out cn\n... | inl _ = inl <>\n... | inr x = inr x\n\n-- The colist type is a greatest fixed-point.\nColist : Set \u2192 Set\nColist A = \u03bd (ListF A)\n\n-- The colist data constructors.\nnilCL : {A : Set} \u2192 Colist A\nnilCL = Wrap (inl <>)\n\nconsCL : {A : Set} \u2192 A \u2192 Colist A \u2192 Colist A\nconsCL x xs = Wrap (inr (x , xs))\n\n-- The colist destructors.\nnullCL : {A : Set} \u2192 Colist A \u2192 Bool\nnullCL xs with out xs\n... | inl _ = true\n... | inr _ = false\n\n-- headCL : {A : Set} \u2192 Colist A \u2192 A\n-- headCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (x , _) = x\n\n-- tailCL : {A : Set} \u2192 Colist A \u2192 Colist A\n-- tailCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (_ , xs') = xs'\n\n-- The stream type is a greatest fixed-point.\nStream : Set \u2192 Set\nStream A = \u03bd (StreamF A)\n\n-- The stream data constructor.\nconsS : {A : Set} \u2192 A \u2192 Stream A \u2192 Stream A\nconsS x xs = Wrap (x , xs)\n\n-- The stream destructors.\nheadS : {A : Set} \u2192 Stream A \u2192 A\nheadS xs with out xs\n... | x , _ = x\n\ntailS : {A : Set} \u2192 Stream A \u2192 Stream A\ntailS xs with out xs\n... | _ , xs' = xs'\n\n-- From (Leclerc and Paulin-Mohring 1994, p. 195).\n--\n-- TODO (07 January 2014): Agda doesn't accept the definition of\n-- Stream-build.\n{-# NO_TERMINATION_CHECK #-}\nStream-build :\n {A X : Set} \u2192\n (X \u2192 StreamF A X) \u2192\n X \u2192 Stream A\nStream-build h x with h x\n... | a , x' = Wrap (a , Stream-build h x')\n\n-- From (Gim\u00e9nez, 1995, p. 40).\n--\n-- TODO (07 January 2014): Agda doesn't accept the definition of\n-- Stream-corec.\n{-# NO_TERMINATION_CHECK #-}\nStream-corec :\n {A X : Set} \u2192\n (X \u2192 (A \u00d7 (Stream A + X))) \u2192\n X \u2192 Stream A\nStream-corec h x with h x\n... | a , inl xs = Wrap (a , xs)\n... | a , inr x' = Wrap (a , (Stream-corec h x'))\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Gim\u00e9nez, E. (1995). Codifying guarded de\ufb01nitions with recursive\n-- schemes. In: Types for Proofs and Programs (TYPES \u201994). Ed. by\n-- Dybjer, P., Nordstr\u00f6m, B. and Smith, J. Vol. 996. LNCS. Springer,\n-- pp. 39\u201359.\n--\n-- Leclerc, F. and Paulin-Mohring, C. (1994). Programming with Streams\n-- in Coq. A case study : the Sieve of Eratosthenes. In: Types for\n-- Proofs and Programs (TYPES \u201993). Ed. by Barendregt, H. and Nipkow,\n-- T. Vol. 806. LNCS. Springer, pp. 191\u2013212.\n--\n-- Vene, Varmo (2000). Categorical programming with inductive and\n-- coinductive types. PhD thesis. Faculty of Mathematics: University\n-- of Tartu.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d3a0501a305802a2bdcd4ab9568ea4a2ac6d6410","subject":"Obligatory ascii art for this next-level research project.","message":"Obligatory ascii art for this next-level research project.\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/Examples\/DarkwingDuck.agda","new_file":"formalization\/agda\/Spire\/Examples\/DarkwingDuck.agda","new_contents":"{-\n\nProject Darkwing Duck:\n\nTowards the ultimate Agda model of the Spire canonical\ntype theory and its associated derived generic operations\nlibrary.\n\n ____\n \/ `.\n \/-----.| ____\n ___\/___.---`--.__.---' `--.\n _______.-----' __.--' )\n ,--'---.______________..----'( __ __.-'\n `---.___,-.|(a (a) \/-' )___.---'\n `-.>------<__.-'\n ______ _____..--' \/\/\n __.----' `---._ `._.--._______.-'\/))\n,--'---.__ -_ _.-(`-.____.'\/\/ \\\n `-._ `---.________.---' >\\ \/< \\\n \\_ `--.___ \\ \\-__-\/ \/ \\\n \\_ `----._______\\ \\ \/ \/__ \\\n \\ \/ |,-------'-'\\ `-.__\\\n \\ ( || \\ )\n `\\ \\ || \/\\ \/\n \\ >-|| @) @) \/\\ \/\n \\ ((_|| \\ \\_.'|\n \\ || `-' |\n \\ || \/ |\n \\ || ( '|\n \\ || @) @) \\ |\n \\ || \\ )\n `\\_ `|__ ____\\ |\n \\_ | ``----''' \\|\n \\_ \\ .--___ |)\n `-.__ \\ | \\ |\n `----.___ \\^\/| \\\/\\|\n `--\\ \\-._ \/ | |\n \\ \\ `' \\ \\\n __...--' ) ( `-._\n (_ \/ `. `-.__\n `--.__.' `. )\n `.__.-'\n-}\n\n----------------------------------------------------------------------\n\n{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nopen import Function\nmodule Spire.Examples.DarkwingDuck where\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nBranches : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nBranches [] P = \u22a4\nBranches (l \u2237 E) P = P here \u00d7 Branches E (\u03bb t \u2192 P (there t))\n\ncase : {E : Enum} (P : Tag E \u2192 Set) (cs : Branches E P) (t : Tag E) \u2192 P t\ncase P (c , cs) here = c\ncase P (c , cs) (there t) = case (\u03bb t \u2192 P (there t)) cs t\n\n----------------------------------------------------------------------\n\ndata Tel : Set where\n End : Tel\n Arg : (A : Set) (B : A \u2192 Tel) \u2192 Tel\n\nEl\u1d40 : Tel \u2192 Set\nEl\u1d40 End = \u22a4\nEl\u1d40 (Arg A B) = \u03a3 A (\u03bb a \u2192 El\u1d40 (B a))\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set where\n End : (i : I) \u2192 Desc I\n Rec : (i : I) (D : Desc I) \u2192 Desc I\n Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n\n----------------------------------------------------------------------\n\nISet : Set \u2192 Set\nISet I = I \u2192 Set\n\nEl\u1d30 : {I : Set} (D : Desc I) \u2192 ISet I \u2192 ISet I\nEl\u1d30 (End j) X i = j \u2261 i\nEl\u1d30 (Rec j D) X i = X j \u00d7 El\u1d30 D X i\nEl\u1d30 (Arg A B) X i = \u03a3 A (\u03bb a \u2192 El\u1d30 (B a) X i)\n\nHyps : {I : Set} (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El\u1d30 D X i) \u2192 Set\nHyps (End j) X P i q = \u22a4\nHyps (Rec j D) X P i (x , xs) = P j x \u00d7 Hyps D X P i xs\nHyps (Arg A B) X P i (a , b) = Hyps (B a) X P i b\n\n----------------------------------------------------------------------\n\ndata \u03bc {I : Set} (D : Desc I) (i : I) : Set where\n init : El\u1d30 D (\u03bc D) i \u2192 \u03bc D i\n\n----------------------------------------------------------------------\n\nind : {I : Set} (D : Desc I)\n (M : (i : I) \u2192 \u03bc D i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 D (\u03bc D) i) (ihs : Hyps D (\u03bc D) M i xs) \u2192 M i (init xs))\n (i : I)\n (x : \u03bc D i)\n \u2192 M i x\n\nprove : {I : Set} (D E : Desc I)\n (M : (i : I) \u2192 \u03bc E i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 E (\u03bc E) i) (ihs : Hyps E (\u03bc E) M i xs) \u2192 M i (init xs))\n (i : I) (xs : El\u1d30 D (\u03bc E) i) \u2192 Hyps D (\u03bc E) M i xs\n\nind D M \u03b1 i (init xs) = \u03b1 i xs (prove D D M \u03b1 i xs)\n\nprove (End j) E M \u03b1 i q = tt\nprove (Rec j D) E M \u03b1 i (x , xs) = ind E M \u03b1 j x , prove D E M \u03b1 i xs\nprove (Arg A B) E M \u03b1 i (a , xs) = prove (B a) E M \u03b1 i xs\n\n----------------------------------------------------------------------\n\nrecord Data : Set where\n field\n P : Tel\n I : El\u1d40 P \u2192 Tel\n E : Enum\n B : (A : El\u1d40 P) \u2192 Branches E (\u03bb _ \u2192 Desc (El\u1d40 (I A)))\n\n C : (A : El\u1d40 P) \u2192 Tag E \u2192 Desc (El\u1d40 (I A))\n C A = case (\u03bb _ \u2192 Desc (El\u1d40 (I A))) (B A)\n\n D : (A : El\u1d40 P) \u2192 Desc (El\u1d40 (I A))\n D A = Arg (Tag E) (C A)\n\n----------------------------------------------------------------------\n\nUncurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedBranches E P X = Branches E P \u2192 X\n\nCurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedBranches [] P X = X\nCurriedBranches (l \u2237 E) P X = P here \u2192 CurriedBranches E (\u03bb t \u2192 P (there t)) X\n\ncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 UncurriedBranches E P X \u2192 CurriedBranches E P X\ncurryBranches {[]} f = f tt\ncurryBranches {l \u2237 E} f = \u03bb c \u2192 curryBranches (\u03bb cs \u2192 f (c , cs))\n\nuncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 CurriedBranches E P X \u2192 UncurriedBranches E P X\nuncurryBranches {[]} x tt = x\nuncurryBranches {l \u2237 E} f (c , cs) = uncurryBranches (f c) cs\n\n----------------------------------------------------------------------\n\nUncurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nUncurriedEl\u1d40 T X = (xs : El\u1d40 T) \u2192 X xs\n\nCurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nCurriedEl\u1d40 End X = X tt\nCurriedEl\u1d40 (Arg A B) X = (a : A) \u2192 CurriedEl\u1d40 (B a) (\u03bb b \u2192 X (a , b))\n\ncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 UncurriedEl\u1d40 T X \u2192 CurriedEl\u1d40 T X\ncurryEl\u1d40 End X f = f tt\ncurryEl\u1d40 (Arg A B) X f = \u03bb a \u2192 curryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (\u03bb b \u2192 f (a , b))\n\nuncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 CurriedEl\u1d40 T X \u2192 UncurriedEl\u1d40 T X\nuncurryEl\u1d40 End X x tt = x\nuncurryEl\u1d40 (Arg A B) X f (a , b) = uncurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (f a) b\n\nICurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nICurriedEl\u1d40 End X = X tt\nICurriedEl\u1d40 (Arg A B) X = {a : A} \u2192 ICurriedEl\u1d40 (B a) (\u03bb b \u2192 X (a , b))\n\nicurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 UncurriedEl\u1d40 T X \u2192 ICurriedEl\u1d40 T X\nicurryEl\u1d40 End X f = f tt\nicurryEl\u1d40 (Arg A B) X f = \u03bb {a} \u2192 icurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (\u03bb b \u2192 f (a , b))\n\niuncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 ICurriedEl\u1d40 T X \u2192 UncurriedEl\u1d40 T X\niuncurryEl\u1d40 End X x tt = x\niuncurryEl\u1d40 (Arg A B) X f (a , b) = iuncurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) f b\n\n----------------------------------------------------------------------\n\nUncurriedEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl\u1d30 D X = \u2200{i} \u2192 El\u1d30 D X i \u2192 X i\n\nCurriedEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl\u1d30 (End i) X = X i\nCurriedEl\u1d30 (Rec i D) X = (x : X i) \u2192 CurriedEl\u1d30 D X\nCurriedEl\u1d30 (Arg A B) X = (a : A) \u2192 CurriedEl\u1d30 (B a) X\n\ncurryEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I)\n \u2192 UncurriedEl\u1d30 D X \u2192 CurriedEl\u1d30 D X\ncurryEl\u1d30 (End i) X cn = cn refl\ncurryEl\u1d30 (Rec i D) X cn = \u03bb x \u2192 curryEl\u1d30 D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl\u1d30 (Arg A B) X cn = \u03bb a \u2192 curryEl\u1d30 (B a) X (\u03bb xs \u2192 cn (a , xs))\n\n----------------------------------------------------------------------\n\nUncurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 Set\nUncurriedHyps D X P cn =\n \u2200 i (xs : El\u1d30 D X i) (ihs : Hyps D X P i xs) \u2192 P i (cn xs)\n\nCurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 Set\nCurriedHyps (End i) X P cn =\n P i (cn refl)\nCurriedHyps (Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedHyps D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedHyps (Arg A B) X P cn =\n (a : A) \u2192 CurriedHyps (B a) X P (\u03bb xs \u2192 cn (a , xs))\n\ncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 UncurriedHyps D X P cn\n \u2192 CurriedHyps D X P cn\ncurryHyps (End i) X P cn pf =\n pf i refl tt\ncurryHyps (Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryHyps D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryHyps (Arg A B) X P cn pf =\n \u03bb a \u2192 curryHyps (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\n\nuncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 CurriedHyps D X P cn\n \u2192 UncurriedHyps D X P cn\nuncurryHyps (End .i) X P cn pf i refl tt =\n pf\nuncurryHyps (Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryHyps D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryHyps (Arg A B) X P cn pf i (a , xs) ihs =\n uncurryHyps (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\n\n----------------------------------------------------------------------\n\nFormUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 UncurriedEl\u1d40 (Data.I R p) \u03bb i\n \u2192 Set\nFormUncurried R p i = \u03bc (Data.D R p) i\n\nForm : (R : Data)\n \u2192 CurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 CurriedEl\u1d40 (Data.I R p) \u03bb i\n \u2192 Set\nForm R =\n curryEl\u1d40 (Data.P R) (\u03bb p \u2192 CurriedEl\u1d40 (Data.I R p) \u03bb i \u2192 Set) \u03bb p \u2192\n curryEl\u1d40 (Data.I R p) (\u03bb i \u2192 Set) \u03bb i \u2192\n FormUncurried R p i\n\n----------------------------------------------------------------------\n\ninjUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p\n in CurriedEl\u1d30 D (\u03bc D)\ninjUncurried R p t = curryEl\u1d30 (Data.C R p t)\n (\u03bc (Data.D R p))\n (\u03bb xs \u2192 init (t , xs))\n\ninj : (R : Data)\n \u2192 ICurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p\n in CurriedEl\u1d30 D (\u03bc D)\ninj R = icurryEl\u1d40 (Data.P R)\n (\u03bb p \u2192 let D = Data.D R p in CurriedEl\u1d30 D (\u03bc D))\n (injUncurried R)\n\n----------------------------------------------------------------------\n\nindCurried : {I : Set} (D : Desc I)\n (M : (i : I) \u2192 \u03bc D i \u2192 Set)\n (f : CurriedHyps D (\u03bc D) M init)\n (i : I)\n (x : \u03bc D i)\n \u2192 M i x\nindCurried D M f i x =\n ind D M (uncurryHyps D (\u03bc D) M init f) i x\n\nSumCurriedHyps : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 Tag (Data.E R) \u2192 Set\nSumCurriedHyps R p M t =\n let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc (Data.D R p) i \u2192 Set) M in\n CurriedHyps (Data.C R p t) (\u03bc (Data.D R p)) unM (\u03bb xs \u2192 init (t , xs))\n\nelimUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in UncurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 (x : \u03bc D i) \u2192 unM i x))\nelimUncurried R p M cs =\n let D = Data.D R p\n unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in\n curryEl\u1d40 (Data.I R p) (\u03bb i \u2192 (x : \u03bc D i) \u2192 unM i x) \u03bb i x \u2192\n indCurried (Data.D R p) unM\n (case (SumCurriedHyps R p M) cs)\n i x\n\nelim : (R : Data)\n \u2192 ICurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in CurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 (x : \u03bc D i) \u2192 unM i x))\nelim R = icurryEl\u1d40 (Data.P R)\n (\u03bb p \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in CurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 (x : \u03bc D i) \u2192 unM i x)))\n (\u03bb p M \u2192 curryBranches\n (elimUncurried R p M))\n\n----------------------------------------------------------------------\n\n\u2115E : Enum\n\u2115E = \"zero\" \u2237 \"suc\" \u2237 []\n\nVecE : Enum\nVecE = \"nil\" \u2237 \"cons\" \u2237 []\n\n\u2115T : Set\n\u2115T = Tag \u2115E\n\nVecT : Set\nVecT = Tag VecE\n\nzeroT : \u2115T\nzeroT = here\n\nsucT : \u2115T\nsucT = there here\n\nnilT : VecT\nnilT = here\n\nconsT : VecT\nconsT = there here\n\n----------------------------------------------------------------------\n\n\u2115R : Data\n\u2115R = record\n { P = End\n ; I = \u03bb _ \u2192 End\n ; E = \u2115E\n ; B = \u03bb _ \u2192\n End tt\n , Rec tt (End tt)\n , tt\n }\n\n\u2115 : Set\n\u2115 = Form \u2115R\n\nzero : \u2115\nzero = inj \u2115R zeroT\n\nsuc : \u2115 \u2192 \u2115\nsuc = inj \u2115R sucT\n\nVecR : Data\nVecR = record\n { P = Arg Set (\u03bb _ \u2192 End)\n ; I = \u03bb _ \u2192 Arg \u2115 (\u03bb _ \u2192 End)\n ; E = VecE\n ; B = \u03bb A \u2192\n End (zero , tt)\n , Arg \u2115 (\u03bb n \u2192 Arg (proj\u2081 A) \u03bb _ \u2192 Rec (n , tt) (End (suc n , tt)))\n , tt\n }\n\nVec : (A : Set) \u2192 \u2115 \u2192 Set\nVec = Form VecR\n\nnil : {A : Set} \u2192 Vec A zero\nnil = inj VecR nilT\n\ncons : {A : Set} (n : \u2115) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\ncons = inj VecR consT\n\n----------------------------------------------------------------------\n\nadd : \u2115 \u2192 \u2115 \u2192 \u2115\nadd = elim \u2115R (\u03bb n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n\nmult : \u2115 \u2192 \u2115 \u2192 \u2115\nmult = elim \u2115R (\u03bb n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n\nappend : {A : Set} (m : \u2115) (xs : Vec A m) (n : \u2115) (ys : Vec A n) \u2192 Vec A (add m n)\nappend {A} = elim VecR (\u03bb m xs \u2192 (n : \u2115) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons (add m n) x (ih n ys))\n\nconcat : {A : Set} (m n : \u2115) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\nconcat {A} m = elim VecR (\u03bb n xss \u2192 Vec A (mult n m))\n nil\n (\u03bb n xs xss ih \u2192 append m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n","old_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nopen import Function\nmodule Spire.Examples.DarkwingDuck where\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nBranches : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nBranches [] P = \u22a4\nBranches (l \u2237 E) P = P here \u00d7 Branches E (\u03bb t \u2192 P (there t))\n\ncase : {E : Enum} (P : Tag E \u2192 Set) (cs : Branches E P) (t : Tag E) \u2192 P t\ncase P (c , cs) here = c\ncase P (c , cs) (there t) = case (\u03bb t \u2192 P (there t)) cs t\n\n----------------------------------------------------------------------\n\ndata Tel : Set where\n End : Tel\n Arg : (A : Set) (B : A \u2192 Tel) \u2192 Tel\n\nEl\u1d40 : Tel \u2192 Set\nEl\u1d40 End = \u22a4\nEl\u1d40 (Arg A B) = \u03a3 A (\u03bb a \u2192 El\u1d40 (B a))\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set where\n End : (i : I) \u2192 Desc I\n Rec : (i : I) (D : Desc I) \u2192 Desc I\n Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n\n----------------------------------------------------------------------\n\nISet : Set \u2192 Set\nISet I = I \u2192 Set\n\nEl\u1d30 : {I : Set} (D : Desc I) \u2192 ISet I \u2192 ISet I\nEl\u1d30 (End j) X i = j \u2261 i\nEl\u1d30 (Rec j D) X i = X j \u00d7 El\u1d30 D X i\nEl\u1d30 (Arg A B) X i = \u03a3 A (\u03bb a \u2192 El\u1d30 (B a) X i)\n\nHyps : {I : Set} (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El\u1d30 D X i) \u2192 Set\nHyps (End j) X P i q = \u22a4\nHyps (Rec j D) X P i (x , xs) = P j x \u00d7 Hyps D X P i xs\nHyps (Arg A B) X P i (a , b) = Hyps (B a) X P i b\n\n----------------------------------------------------------------------\n\ndata \u03bc {I : Set} (D : Desc I) (i : I) : Set where\n init : El\u1d30 D (\u03bc D) i \u2192 \u03bc D i\n\n----------------------------------------------------------------------\n\nind : {I : Set} (D : Desc I)\n (M : (i : I) \u2192 \u03bc D i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 D (\u03bc D) i) (ihs : Hyps D (\u03bc D) M i xs) \u2192 M i (init xs))\n (i : I)\n (x : \u03bc D i)\n \u2192 M i x\n\nprove : {I : Set} (D E : Desc I)\n (M : (i : I) \u2192 \u03bc E i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 E (\u03bc E) i) (ihs : Hyps E (\u03bc E) M i xs) \u2192 M i (init xs))\n (i : I) (xs : El\u1d30 D (\u03bc E) i) \u2192 Hyps D (\u03bc E) M i xs\n\nind D M \u03b1 i (init xs) = \u03b1 i xs (prove D D M \u03b1 i xs)\n\nprove (End j) E M \u03b1 i q = tt\nprove (Rec j D) E M \u03b1 i (x , xs) = ind E M \u03b1 j x , prove D E M \u03b1 i xs\nprove (Arg A B) E M \u03b1 i (a , xs) = prove (B a) E M \u03b1 i xs\n\n----------------------------------------------------------------------\n\nrecord Data : Set where\n field\n P : Tel\n I : El\u1d40 P \u2192 Tel\n E : Enum\n B : (A : El\u1d40 P) \u2192 Branches E (\u03bb _ \u2192 Desc (El\u1d40 (I A)))\n\n C : (A : El\u1d40 P) \u2192 Tag E \u2192 Desc (El\u1d40 (I A))\n C A = case (\u03bb _ \u2192 Desc (El\u1d40 (I A))) (B A)\n\n D : (A : El\u1d40 P) \u2192 Desc (El\u1d40 (I A))\n D A = Arg (Tag E) (C A)\n\n----------------------------------------------------------------------\n\nUncurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedBranches E P X = Branches E P \u2192 X\n\nCurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedBranches [] P X = X\nCurriedBranches (l \u2237 E) P X = P here \u2192 CurriedBranches E (\u03bb t \u2192 P (there t)) X\n\ncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 UncurriedBranches E P X \u2192 CurriedBranches E P X\ncurryBranches {[]} f = f tt\ncurryBranches {l \u2237 E} f = \u03bb c \u2192 curryBranches (\u03bb cs \u2192 f (c , cs))\n\nuncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 CurriedBranches E P X \u2192 UncurriedBranches E P X\nuncurryBranches {[]} x tt = x\nuncurryBranches {l \u2237 E} f (c , cs) = uncurryBranches (f c) cs\n\n----------------------------------------------------------------------\n\nUncurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nUncurriedEl\u1d40 T X = (xs : El\u1d40 T) \u2192 X xs\n\nCurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nCurriedEl\u1d40 End X = X tt\nCurriedEl\u1d40 (Arg A B) X = (a : A) \u2192 CurriedEl\u1d40 (B a) (\u03bb b \u2192 X (a , b))\n\ncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 UncurriedEl\u1d40 T X \u2192 CurriedEl\u1d40 T X\ncurryEl\u1d40 End X f = f tt\ncurryEl\u1d40 (Arg A B) X f = \u03bb a \u2192 curryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (\u03bb b \u2192 f (a , b))\n\nuncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 CurriedEl\u1d40 T X \u2192 UncurriedEl\u1d40 T X\nuncurryEl\u1d40 End X x tt = x\nuncurryEl\u1d40 (Arg A B) X f (a , b) = uncurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (f a) b\n\nICurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nICurriedEl\u1d40 End X = X tt\nICurriedEl\u1d40 (Arg A B) X = {a : A} \u2192 ICurriedEl\u1d40 (B a) (\u03bb b \u2192 X (a , b))\n\nicurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 UncurriedEl\u1d40 T X \u2192 ICurriedEl\u1d40 T X\nicurryEl\u1d40 End X f = f tt\nicurryEl\u1d40 (Arg A B) X f = \u03bb {a} \u2192 icurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (\u03bb b \u2192 f (a , b))\n\niuncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 ICurriedEl\u1d40 T X \u2192 UncurriedEl\u1d40 T X\niuncurryEl\u1d40 End X x tt = x\niuncurryEl\u1d40 (Arg A B) X f (a , b) = iuncurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) f b\n\n----------------------------------------------------------------------\n\nUncurriedEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl\u1d30 D X = \u2200{i} \u2192 El\u1d30 D X i \u2192 X i\n\nCurriedEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl\u1d30 (End i) X = X i\nCurriedEl\u1d30 (Rec i D) X = (x : X i) \u2192 CurriedEl\u1d30 D X\nCurriedEl\u1d30 (Arg A B) X = (a : A) \u2192 CurriedEl\u1d30 (B a) X\n\ncurryEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I)\n \u2192 UncurriedEl\u1d30 D X \u2192 CurriedEl\u1d30 D X\ncurryEl\u1d30 (End i) X cn = cn refl\ncurryEl\u1d30 (Rec i D) X cn = \u03bb x \u2192 curryEl\u1d30 D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl\u1d30 (Arg A B) X cn = \u03bb a \u2192 curryEl\u1d30 (B a) X (\u03bb xs \u2192 cn (a , xs))\n\n----------------------------------------------------------------------\n\nUncurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 Set\nUncurriedHyps D X P cn =\n \u2200 i (xs : El\u1d30 D X i) (ihs : Hyps D X P i xs) \u2192 P i (cn xs)\n\nCurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 Set\nCurriedHyps (End i) X P cn =\n P i (cn refl)\nCurriedHyps (Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedHyps D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedHyps (Arg A B) X P cn =\n (a : A) \u2192 CurriedHyps (B a) X P (\u03bb xs \u2192 cn (a , xs))\n\ncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 UncurriedHyps D X P cn\n \u2192 CurriedHyps D X P cn\ncurryHyps (End i) X P cn pf =\n pf i refl tt\ncurryHyps (Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryHyps D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryHyps (Arg A B) X P cn pf =\n \u03bb a \u2192 curryHyps (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\n\nuncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 CurriedHyps D X P cn\n \u2192 UncurriedHyps D X P cn\nuncurryHyps (End .i) X P cn pf i refl tt =\n pf\nuncurryHyps (Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryHyps D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryHyps (Arg A B) X P cn pf i (a , xs) ihs =\n uncurryHyps (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\n\n----------------------------------------------------------------------\n\nFormUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 UncurriedEl\u1d40 (Data.I R p) \u03bb i\n \u2192 Set\nFormUncurried R p i = \u03bc (Data.D R p) i\n\nForm : (R : Data)\n \u2192 CurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 CurriedEl\u1d40 (Data.I R p) \u03bb i\n \u2192 Set\nForm R =\n curryEl\u1d40 (Data.P R) (\u03bb p \u2192 CurriedEl\u1d40 (Data.I R p) \u03bb i \u2192 Set) \u03bb p \u2192\n curryEl\u1d40 (Data.I R p) (\u03bb i \u2192 Set) \u03bb i \u2192\n FormUncurried R p i\n\n----------------------------------------------------------------------\n\ninjUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p\n in CurriedEl\u1d30 D (\u03bc D)\ninjUncurried R p t = curryEl\u1d30 (Data.C R p t)\n (\u03bc (Data.D R p))\n (\u03bb xs \u2192 init (t , xs))\n\ninj : (R : Data)\n \u2192 ICurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p\n in CurriedEl\u1d30 D (\u03bc D)\ninj R = icurryEl\u1d40 (Data.P R)\n (\u03bb p \u2192 let D = Data.D R p in CurriedEl\u1d30 D (\u03bc D))\n (injUncurried R)\n\n----------------------------------------------------------------------\n\nindCurried : {I : Set} (D : Desc I)\n (M : (i : I) \u2192 \u03bc D i \u2192 Set)\n (f : CurriedHyps D (\u03bc D) M init)\n (i : I)\n (x : \u03bc D i)\n \u2192 M i x\nindCurried D M f i x =\n ind D M (uncurryHyps D (\u03bc D) M init f) i x\n\nSumCurriedHyps : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 Tag (Data.E R) \u2192 Set\nSumCurriedHyps R p M t =\n let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc (Data.D R p) i \u2192 Set) M in\n CurriedHyps (Data.C R p t) (\u03bc (Data.D R p)) unM (\u03bb xs \u2192 init (t , xs))\n\nelimUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in UncurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 (x : \u03bc D i) \u2192 unM i x))\nelimUncurried R p M cs =\n let D = Data.D R p\n unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in\n curryEl\u1d40 (Data.I R p) (\u03bb i \u2192 (x : \u03bc D i) \u2192 unM i x) \u03bb i x \u2192\n indCurried (Data.D R p) unM\n (case (SumCurriedHyps R p M) cs)\n i x\n\nelim : (R : Data)\n \u2192 ICurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in CurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 (x : \u03bc D i) \u2192 unM i x))\nelim R = icurryEl\u1d40 (Data.P R)\n (\u03bb p \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in CurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 (x : \u03bc D i) \u2192 unM i x)))\n (\u03bb p M \u2192 curryBranches\n (elimUncurried R p M))\n\n----------------------------------------------------------------------\n\n\u2115E : Enum\n\u2115E = \"zero\" \u2237 \"suc\" \u2237 []\n\nVecE : Enum\nVecE = \"nil\" \u2237 \"cons\" \u2237 []\n\n\u2115T : Set\n\u2115T = Tag \u2115E\n\nVecT : Set\nVecT = Tag VecE\n\nzeroT : \u2115T\nzeroT = here\n\nsucT : \u2115T\nsucT = there here\n\nnilT : VecT\nnilT = here\n\nconsT : VecT\nconsT = there here\n\n----------------------------------------------------------------------\n\n\u2115R : Data\n\u2115R = record\n { P = End\n ; I = \u03bb _ \u2192 End\n ; E = \u2115E\n ; B = \u03bb _ \u2192\n End tt\n , Rec tt (End tt)\n , tt\n }\n\n\u2115 : Set\n\u2115 = Form \u2115R\n\nzero : \u2115\nzero = inj \u2115R zeroT\n\nsuc : \u2115 \u2192 \u2115\nsuc = inj \u2115R sucT\n\nVecR : Data\nVecR = record\n { P = Arg Set (\u03bb _ \u2192 End)\n ; I = \u03bb _ \u2192 Arg \u2115 (\u03bb _ \u2192 End)\n ; E = VecE\n ; B = \u03bb A \u2192\n End (zero , tt)\n , Arg \u2115 (\u03bb n \u2192 Arg (proj\u2081 A) \u03bb _ \u2192 Rec (n , tt) (End (suc n , tt)))\n , tt\n }\n\nVec : (A : Set) \u2192 \u2115 \u2192 Set\nVec = Form VecR\n\nnil : {A : Set} \u2192 Vec A zero\nnil = inj VecR nilT\n\ncons : {A : Set} (n : \u2115) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\ncons = inj VecR consT\n\n----------------------------------------------------------------------\n\nadd : \u2115 \u2192 \u2115 \u2192 \u2115\nadd = elim \u2115R (\u03bb n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n\nmult : \u2115 \u2192 \u2115 \u2192 \u2115\nmult = elim \u2115R (\u03bb n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n\nappend : {A : Set} (m : \u2115) (xs : Vec A m) (n : \u2115) (ys : Vec A n) \u2192 Vec A (add m n)\nappend {A} = elim VecR (\u03bb m xs \u2192 (n : \u2115) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons (add m n) x (ih n ys))\n\nconcat : {A : Set} (m n : \u2115) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\nconcat {A} m = elim VecR (\u03bb n xss \u2192 Vec A (mult n m))\n nil\n (\u03bb n xs xss ih \u2192 append m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"5ea0220b999f1d1ef9f072ca4bedfb82dc747ca1","subject":"Use a record for doe, as suggested by Tillmann","message":"Use a record for doe, as suggested by Tillmann\n\nOld-commit-hash: f1a0e47b6fd1c171f165e2a9ba96fd258042a8ae\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Equivalence.agda","new_file":"Base\/Change\/Equivalence.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\nopen import Function\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n\n -- To avoid unification problems, use a one-field record.\n record _\u2259_ dx dy : Set a where\n -- doe = Delta-Observational Equivalence.\n constructor doe\n field\n proof : x \u229e dx \u2261 x \u229e dy\n\n open _\u2259_ public\n\n -- Same priority as \u2261\n infix 4 _\u2259_\n\n open import Relation.Binary\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = doe refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = doe $ sym $ proof \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = doe $ trans (proof \u2259\u2081) (proof \u2259\u2082)\n\n \u2259-isEquivalence : IsEquivalence (_\u2259_)\n \u2259-isEquivalence = record\n { refl = \u2259-refl\n ; sym = \u2259-symm\n ; trans = \u2259-trans\n }\n\n \u2259-setoid : Setoid \u2113 a\n \u2259-setoid = record\n { Carrier = \u0394 x\n ; _\u2248_ = _\u2259_\n ; isEquivalence = \u2259-isEquivalence\n }\n\n\n ------------------------------------------------------------------------\n -- Convenient syntax for equational reasoning\n\n import Relation.Binary.EqReasoning as EqR\n\n module \u2259-Reasoning where\n open EqR \u2259-setoid public\n renaming (_\u2248\u27e8_\u27e9_ to _\u2259\u27e8_\u27e9_)\n\n -- By update-nil, if dx = nil x, then x \u229e dx \u2261 x.\n -- As a consequence, if dx \u2259 nil x, then x \u229e dx \u2261 x\n nil-is-\u229e-unit : \u2200 dx \u2192 dx \u2259 nil x \u2192 x \u229e dx \u2261 x\n nil-is-\u229e-unit dx dx\u2259nil-x =\n begin\n x \u229e dx\n \u2261\u27e8 proof dx\u2259nil-x \u27e9\n x \u229e (nil x)\n \u2261\u27e8 update-nil x \u27e9\n x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Here we prove the inverse:\n \u229e-unit-is-nil : \u2200 dx \u2192 x \u229e dx \u2261 x \u2192 dx \u2259 nil x\n \u229e-unit-is-nil dx x\u229edx\u2261x = doe $\n begin\n x \u229e dx\n \u2261\u27e8 x\u229edx\u2261x \u27e9\n x\n \u2261\u27e8 sym (update-nil x) \u27e9\n x \u229e nil x\n \u220e\n where\n open \u2261-Reasoning\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n -- This results pairs with update-diff.\n diff-update : \u2200 {dx} \u2192 (x \u229e dx) \u229f x \u2259 dx\n diff-update {dx} = doe lemma\n where\n lemma : x \u229e (x \u229e dx \u229f x) \u2261 x \u229e dx\n lemma = update-diff (x \u229e dx) x\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = doe lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 cong (f \u229e df\u2081) $ proof dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 cong (\u03bb f \u2192 f (x \u229e dx\u2082)) $ proof df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n open import Postulate.Extensionality\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} df-x-dx\u2259dg-x-dx = doe lemma\u2082\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 x dx = proof $ df-x-dx\u2259dg-x-dx x dx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n\n -- We know that Derivative f (apply (nil f)) (by nil-is-derivative).\n -- That is, df = nil f -> Derivative f (apply df).\n -- Now, we try to prove that if Derivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 Derivative f (apply df) \u2192 Derivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 \u2259-symm (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n -- Unused, but just to test that inference works.\n lemma : nil f \u2259 dg\n lemma = \u2259-symm (derivative-is-nil dg fdg)\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set a\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n -- Same priority as \u2261\n infix 4 _\u2259_\n\n open import Relation.Binary\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n \u2259-isEquivalence : IsEquivalence (_\u2259_)\n \u2259-isEquivalence = record\n { refl = \u2259-refl\n ; sym = \u2259-symm\n ; trans = \u2259-trans\n }\n\n \u2259-setoid : Setoid \u2113 a\n \u2259-setoid = record\n { Carrier = \u0394 x\n ; _\u2248_ = _\u2259_\n ; isEquivalence = \u2259-isEquivalence\n }\n\n\n ------------------------------------------------------------------------\n -- Convenient syntax for equational reasoning\n\n import Relation.Binary.EqReasoning as EqR\n\n module \u2259-Reasoning where\n open EqR \u2259-setoid public\n renaming (_\u2248\u27e8_\u27e9_ to _\u2259\u27e8_\u27e9_)\n\n -- By update-nil, if dx = nil x, then x \u229e dx \u2261 x.\n -- As a consequence, if dx \u2259 nil x, then x \u229e dx \u2261 x\n nil-is-\u229e-unit : \u2200 dx \u2192 dx \u2259 nil x \u2192 x \u229e dx \u2261 x\n nil-is-\u229e-unit dx dx\u2259nil-x =\n begin\n x \u229e dx\n \u2261\u27e8 dx\u2259nil-x \u27e9\n x \u229e (nil x)\n \u2261\u27e8 update-nil x \u27e9\n x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Here we prove the inverse:\n \u229e-unit-is-nil : \u2200 dx \u2192 x \u229e dx \u2261 x \u2192 dx \u2259 nil x\n \u229e-unit-is-nil dx x\u229edx\u2261x =\n begin\n x \u229e dx\n \u2261\u27e8 x\u229edx\u2261x \u27e9\n x\n \u2261\u27e8 sym (update-nil x) \u27e9\n x \u229e nil x\n \u220e\n where\n open \u2261-Reasoning\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n -- This results pairs with update-diff.\n diff-update : \u2200 {dx} \u2192 (x \u229e dx) \u229f x \u2259 dx\n diff-update {dx} = lemma\n where\n lemma : x \u229e (x \u229e dx \u229f x) \u2261 x \u229e dx\n lemma = update-diff (x \u229e dx) x\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n open import Postulate.Extensionality\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} df-x-dx\u2259dg-x-dx = lemma\u2082\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 = df-x-dx\u2259dg-x-dx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n\n -- We know that Derivative f (apply (nil f)) (by nil-is-derivative).\n -- That is, df = nil f -> Derivative f (apply df).\n -- Now, we try to prove that if Derivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n Derivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 Derivative f (apply df) \u2192 Derivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n{-\n lemma : nil f \u2259 dg\n -- Goes through\n --lemma = sym (derivative-is-nil dg fdg)\n -- Generates tons of crazy yellow ambiguities of equality type. Apparently, unifying against \u2259 does not work so well.\n lemma = \u2259-symm {{changeAlgebra}} {f} {dg} {nil f} (derivative-is-nil dg fdg)\n-}\n\n -- We could also use derivative-is-\u229e-unit, but the proof above matches better\n -- with the text above.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ed9cb7b49d88102a5aa3017140c748073cf6cd6a","subject":"Neglible: ~ invariant under equivalence","message":"Neglible: ~ invariant under equivalence\n","repos":"crypto-agda\/crypto-agda","old_file":"Neglible.agda","new_file":"Neglible.agda","new_contents":"{-# OPTIONS --copatterns #-}\nopen import Algebra\n\nopen import Function\nopen import Function.Extensionality\n\nopen import Data.Nat.NP\nopen import Data.Nat.Distance\nopen import Data.Nat.Properties\nopen import Data.Two\nopen import Data.Zero\nopen import Data.Product\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\n\nopen import HoTT\nopen Equivalences\n\nopen import Explore.Core\nopen import Explore.Universe.Type {\ud835\udfd8}\nopen import Explore.Universe.Base\n\nmodule Neglible where\n\nmodule prop = CommutativeSemiring commutativeSemiring\nmodule OR = Poset (DecTotalOrder.poset decTotalOrder)\n\n\u2264-*-cancel : \u2200 {x m n} \u2192 1 \u2264 x \u2192 x * m \u2264 x * n \u2192 m \u2264 n\n\u2264-*-cancel {suc x} {m} {n} (s\u2264s le) mn\n rewrite prop.*-comm (suc x) m | prop.*-comm (suc x) n = cancel-*-right-\u2264 _ _ _ mn\n\nrecord \u2115\u2192\u211a : Set where\n constructor _\/_[_]\n field\n \u03b5N : (n : \u2115) \u2192 \u2115\n \u03b5D : (n : \u2115) \u2192 \u2115\n \u03b5D-pos : \u2200 n \u2192 \u03b5D n > 0\n\nrecord Is-Neg (\u03b5 : \u2115\u2192\u211a) : Set where\n constructor mk\n open \u2115\u2192\u211a \u03b5\n field\n c\u2099 : (c : \u2115) \u2192 \u2115\n prf : \u2200(c n : \u2115) \u2192 n > c\u2099 n \u2192 n ^ c * \u03b5N n \u2264 \u03b5D n\nopen Is-Neg\n\n0\u2115\u211a : \u2115\u2192\u211a\n\u2115\u2192\u211a.\u03b5N 0\u2115\u211a _ = 0\n\u2115\u2192\u211a.\u03b5D 0\u2115\u211a _ = 1\n\u2115\u2192\u211a.\u03b5D-pos 0\u2115\u211a _ = s\u2264s z\u2264n\n\n0\u2115\u211a-neg : Is-Neg 0\u2115\u211a\nc\u2099 0\u2115\u211a-neg _ = 0\nprf 0\u2115\u211a-neg c n x = OR.trans (OR.reflexive (proj\u2082 prop.zero (n ^ c))) z\u2264n\n\n_+\u2115\u211a_ : \u2115\u2192\u211a \u2192 \u2115\u2192\u211a \u2192 \u2115\u2192\u211a\n\u2115\u2192\u211a.\u03b5N ((\u03b5N \/ \u03b5D [ _ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ _ ])) n = \u03b5N n * \u03bcD n + \u03bcN n * \u03b5D n\n\u2115\u2192\u211a.\u03b5D ((\u03b5N \/ \u03b5D [ _ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ _ ])) n = \u03b5D n * \u03bcD n\n\u2115\u2192\u211a.\u03b5D-pos ((\u03b5N \/ \u03b5D [ \u03b5D+ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ \u03bcD+ ])) n = \u03b5D+ n *-mono \u03bcD+ n\n\n\n+\u2115\u211a-neg : {\u03b5 \u03bc : \u2115\u2192\u211a} \u2192 Is-Neg \u03b5 \u2192 Is-Neg \u03bc \u2192 Is-Neg (\u03b5 +\u2115\u211a \u03bc)\nc\u2099 (+\u2115\u211a-neg \u03b5 \u03bc) n = 1 + c\u2099 \u03b5 n + c\u2099 \u03bc n\nprf (+\u2115\u211a-neg {\u03b5M} {\u03bcM} \u03b5 \u03bc) c n n>nc = \u2264-*-cancel {x = n} (OR.trans (s\u2264s z\u2264n) n>nc) lemma\n where\n\n open \u2264-Reasoning\n open \u2115\u2192\u211a \u03b5M\n open \u2115\u2192\u211a \u03bcM renaming (\u03b5N to \u03bcN; \u03b5D to \u03bcD; \u03b5D-pos to \u03bcD-pos)\n\n lemma = n * (n ^ c * (\u03b5N n * \u03bcD n + \u03bcN n * \u03b5D n))\n \u2261\u27e8 ! prop.*-assoc n (n ^ c) _\n \u2219 proj\u2081 prop.distrib (n ^ (1 + c)) (\u03b5N n * \u03bcD n) (\u03bcN n * \u03b5D n)\n \u2219 ap\u2082 _+_ (! prop.*-assoc (n ^ (1 + c)) (\u03b5N n) (\u03bcD n))\n (! (prop.*-assoc (n ^ (1 + c)) (\u03bcN n) (\u03b5D n))) \u27e9\n n ^ (1 + c) * \u03b5N n * \u03bcD n + n ^ (1 + c) * \u03bcN n * \u03b5D n\n \u2264\u27e8 (prf \u03b5 (1 + c) n (OR.trans (s\u2264s (\u2264-step (m\u2264m+n (c\u2099 \u03b5 n) (c\u2099 \u03bc n)))) n>nc) *-mono (\u03bcD n \u220e))\n +-mono (prf \u03bc (1 + c) n (OR.trans (s\u2264s (\u2264-step (n\u2264m+n (c\u2099 \u03b5 n) (c\u2099 \u03bc n)))) n>nc) *-mono (\u03b5D n \u220e)) \u27e9\n \u03b5D n * \u03bcD n + \u03bcD n * \u03b5D n\n \u2261\u27e8 ap\u2082 _+_ (refl {x = \u03b5D n * \u03bcD n}) (prop.*-comm (\u03bcD n) (\u03b5D n) \u2219 ! proj\u2082 prop.+-identity (\u03b5D n * \u03bcD n)) \u27e9\n 2 * (\u03b5D n * \u03bcD n)\n \u2264\u27e8 OR.trans (s\u2264s (s\u2264s z\u2264n)) n>nc *-mono (\u03b5D n * \u03bcD n \u220e) \u27e9\n n * (\u03b5D n * \u03bcD n)\n \u220e\n\ninfix 4 _\u2264\u2192_\nrecord _\u2264\u2192_ (f g : \u2115\u2192\u211a) : Set where\n constructor mk\n open \u2115\u2192\u211a f renaming (\u03b5N to fN; \u03b5D to fD)\n open \u2115\u2192\u211a g renaming (\u03b5N to gN; \u03b5D to gD)\n field\n -- fN k \/ fD k \u2264 gN k \/ gD k\n \u2264\u2192 : \u2200 k \u2192 fN k * gD k \u2264 gN k * fD k\n\n\u2264\u2192-refl : \u2200 {f} \u2192 f \u2264\u2192 f\n_\u2264\u2192_.\u2264\u2192 \u2264\u2192-refl k = OR.refl\n\n\u2264\u2192-trans : \u2200 {f g h} \u2192 f \u2264\u2192 g \u2192 g \u2264\u2192 h \u2192 f \u2264\u2192 h\n_\u2264\u2192_.\u2264\u2192 (\u2264\u2192-trans {fN \/ fD [ fD-pos ]} {gN \/ gD [ gD-pos ]} {hN \/ hD [ hD-pos ]} (mk fg) (mk gh)) k\n = \u2264-*-cancel (gD-pos k) lemma\n where\n open \u2264-Reasoning\n lemma : gD k * (fN k * hD k) \u2264 gD k * (hN k * fD k)\n lemma = gD k * (fN k * hD k)\n \u2261\u27e8 ! prop.*-assoc (gD k) (fN k) (hD k)\n \u2219 ap (flip _*_ (hD k)) (prop.*-comm (gD k) (fN k))\n \u27e9\n (fN k * gD k) * hD k\n \u2264\u27e8 fg k *-mono OR.refl \u27e9\n (gN k * fD k) * hD k\n \u2261\u27e8 prop.*-assoc (gN k) (fD k) (hD k)\n \u2219 ap (_*_ (gN k)) (prop.*-comm (fD k) (hD k))\n \u2219 ! prop.*-assoc (gN k) (hD k) (fD k)\n \u27e9\n (gN k * hD k) * fD k\n \u2264\u27e8 gh k *-mono OR.refl \u27e9\n (hN k * gD k) * fD k\n \u2261\u27e8 ap (flip _*_ (fD k)) (prop.*-comm (hN k) (gD k))\n \u2219 prop.*-assoc (gD k) (hN k) (fD k)\n \u27e9\n gD k * (hN k * fD k)\n \u220e\n\n+\u2115\u211a-mono : \u2200 {f f' g g'} \u2192 f \u2264\u2192 f' \u2192 g \u2264\u2192 g' \u2192 f +\u2115\u211a g \u2264\u2192 f' +\u2115\u211a g'\n_\u2264\u2192_.\u2264\u2192 (+\u2115\u211a-mono {fN \/ fD [ _ ]} {f'N \/ f'D [ _ ]} {gN \/ gD [ _ ]} {g'N \/ g'D [ _ ]} (mk ff) (mk gg)) k\n = (fN k * gD k + gN k * fD k) * (f'D k * g'D k)\n \u2261\u27e8 proj\u2082 prop.distrib (f'D k * g'D k) (fN k * gD k) (gN k * fD k) \u27e9\n fN k * gD k * (f'D k * g'D k) + gN k * fD k * (f'D k * g'D k)\n \u2261\u27e8 ap\u2082 _+_ (*-interchange {fN k} {gD k} {f'D k} {g'D k} \u2219 ap (_*_ (fN k * f'D k)) (prop.*-comm (gD k) (g'D k)))\n (ap (_*_ (gN k * fD k)) (prop.*-comm (f'D k) (g'D k)) \u2219 *-interchange {gN k} {fD k} {g'D k} {f'D k})\n \u27e9\n fN k * f'D k * (g'D k * gD k) + gN k * g'D k * (fD k * f'D k)\n \u2264\u27e8 (ff k *-mono OR.refl) +-mono (gg k *-mono OR.refl) \u27e9\n f'N k * fD k * (g'D k * gD k) + g'N k * gD k * (fD k * f'D k)\n \u2261\u27e8 ap\u2082 _+_ (*-interchange {f'N k} {fD k} {g'D k} {gD k})\n (ap (_*_ (g'N k * gD k)) (prop.*-comm (fD k) (f'D k))\n \u2219 *-interchange {g'N k} {gD k} {f'D k} {fD k}\n \u2219 ap (_*_ (g'N k * f'D k)) (prop.*-comm (gD k) (fD k)))\n \u27e9\n f'N k * g'D k * (fD k * gD k) + g'N k * f'D k * (fD k * gD k)\n \u2261\u27e8 ! proj\u2082 prop.distrib (fD k * gD k) (f'N k * g'D k) (g'N k * f'D k) \u27e9\n (f'N k * g'D k + g'N k * f'D k) * (fD k * gD k)\n \u220e\n where\n open \u2264-Reasoning\n\nrecord NegBounded (f : \u2115\u2192\u211a) : Set where\n constructor mk\n field\n \u03b5 : \u2115\u2192\u211a\n \u03b5-neg : Is-Neg \u03b5\n bounded : f \u2264\u2192 \u03b5\n\nmodule _ where\n open NegBounded\n\n fromNeg : {f : \u2115\u2192\u211a} \u2192 Is-Neg f \u2192 NegBounded f\n \u03b5 (fromNeg f-neg) = _\n \u03b5-neg (fromNeg f-neg) = f-neg\n bounded (fromNeg f-neg) = \u2264\u2192-refl\n\n \u2264-NB : {f g : \u2115\u2192\u211a} \u2192 f \u2264\u2192 g \u2192 NegBounded g \u2192 NegBounded f\n \u03b5 (\u2264-NB le nb) = \u03b5 nb\n \u03b5-neg (\u2264-NB le nb) = \u03b5-neg nb\n bounded (\u2264-NB le nb) = \u2264\u2192-trans le (bounded nb)\n\n _+NB_ : {f g : \u2115\u2192\u211a} \u2192 NegBounded f \u2192 NegBounded g \u2192 NegBounded (f +\u2115\u211a g)\n \u03b5 (fNB +NB gNB) = \u03b5 fNB +\u2115\u211a \u03b5 gNB\n \u03b5-neg (fNB +NB gNB) = +\u2115\u211a-neg (\u03b5-neg fNB) (\u03b5-neg gNB)\n bounded (fNB +NB gNB) = +\u2115\u211a-mono (bounded fNB) (bounded gNB)\n\nmodule ~-NegBounded (R\u1d41 : \u2115 \u2192 U)(let R = \u03bb n \u2192 El (R\u1d41 n))(inh : \u2200 x \u2192 0 < Card (R\u1d41 x)) where\n\n # : \u2200 {n} \u2192 Count (R n)\n # {n} = count (R\u1d41 n)\n\n ~dist : (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) \u2192 \u2115\u2192\u211a\n \u2115\u2192\u211a.\u03b5N (~dist f g) n = dist (# (f n)) (# (g n))\n \u2115\u2192\u211a.\u03b5D (~dist f g) n = Card (R\u1d41 n)\n \u2115\u2192\u211a.\u03b5D-pos (~dist f g) n = inh n\n\n ~dist-sum : \u2200 f g h \u2192 ~dist f h \u2264\u2192 ~dist f g +\u2115\u211a ~dist g h\n _\u2264\u2192_.\u2264\u2192 (~dist-sum f g h) k\n = #fh * (|R| * |R|)\n \u2264\u27e8 dist-sum #f #g #h *-mono OR.refl \u27e9\n (#fg + #gh) * (|R| * |R|)\n \u2261\u27e8 ! prop.*-assoc (#fg + #gh) |R| |R| \u2219 ap (flip _*_ |R|) (proj\u2082 prop.distrib |R| #fg #gh) \u27e9\n (#fg * |R| + #gh * |R|) * |R|\n \u220e\n where\n open \u2264-Reasoning\n |R| = Card (R\u1d41 k)\n #f = # (f k)\n #g = # (g k)\n #h = # (h k)\n #fh = dist #f #h\n #fg = dist #f #g\n #gh = dist #g #h\n\n record _~_ (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) : Set where\n constructor mk\n field\n ~ : NegBounded (~dist f g)\n\n ~-trans : Transitive _~_\n _~_.~ (~-trans {f}{g}{h} (mk fg) (mk gh)) = \u2264-NB (~dist-sum f g h) (fg +NB gh)\n\n ~-Inv : {{_ : FunExt}}{{_ : UA}}(\u03c0 : \u2200 n \u2192 R n \u2243 R n)(f g : \u2200 x \u2192 R x \u2192 \ud835\udfda)\n (eq : \u2200 x (r : R x) \u2192 f x r \u2261 g x (proj\u2081 (\u03c0 x) r)) \u2192 f ~ g\n _~_.~ (~-Inv \u03c0 f g eq) = \u2264-NB lemma (fromNeg 0\u2115\u211a-neg)\n where\n open \u2264-Reasoning\n lemma : ~dist f g \u2264\u2192 0\u2115\u211a\n _\u2264\u2192_.\u2264\u2192 lemma k = dist (# (f k)) (# (g k)) * 1\n \u2261\u27e8 proj\u2082 prop.*-identity _ \u27e9\n dist (# (f k)) (# (g k))\n \u2261\u27e8 ap (flip dist (# (g k))) (count-ext (R\u1d41 k) (eq k)) \u27e9\n dist (# (g k \u2218 proj\u2081 (\u03c0 k))) (# (g k))\n \u2261\u27e8 ap (flip dist (# (g k))) (sumStableUnder (R\u1d41 k) (\u03c0 k) (\ud835\udfda\u25b9\u2115 \u2218 g k)) \u27e9\n dist (# (g k)) (# (g k))\n \u2261\u27e8 dist-refl (# (g k)) \u27e9\n 0\n \u220e\n\nmodule ~-Inlined (R\u1d41 : \u2115 \u2192 U)(let R = \u03bb n \u2192 El (R\u1d41 n)) where\n\n # : \u2200 {n} \u2192 Count (R n)\n # {n} = count (R\u1d41 n)\n\n record _~_ (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) : Set where\n constructor mk\n field\n \u03b5 : \u2115\u2192\u211a\n open \u2115\u2192\u211a \u03b5\n field\n \u03b5-neg : Is-Neg \u03b5\n bounded : \u2200 k \u2192 \u03b5D k * dist (# (f k)) (# (g k)) \u2264 Card (R\u1d41 k) * \u03b5N k\n\n\n ~-trans : Transitive _~_\n _~_.\u03b5 (~-trans x x\u2081) = _\n _~_.\u03b5-neg (~-trans x x\u2081) = +\u2115\u211a-neg (_~_.\u03b5-neg x) (_~_.\u03b5-neg x\u2081)\n _~_.bounded (~-trans {f}{g}{h}(mk \u03b5\u2080 \u03b5\u2080-neg fg) (mk \u03b5\u2081 \u03b5\u2081-neg gh)) k\n = (b * d) * dist #f #h\n \u2264\u27e8 (b * d \u220e) *-mono dist-sum #f #g #h \u27e9\n (b * d) * (dist #f #g + dist #g #h)\n \u2261\u27e8 proj\u2081 prop.distrib (b * d) (dist #f #g) (dist #g #h)\n \u2219 ap\u2082 _+_ (ap\u2082 _*_ (prop.*-comm b d) refl\n \u2219 prop.*-assoc d b (dist #f #g)) (prop.*-assoc b d (dist #g #h))\n \u27e9\n d * (b * dist #f #g) + b * (d * dist #g #h)\n \u2264\u27e8 ((d \u220e) *-mono fg k) +-mono ((b \u220e) *-mono gh k) \u27e9\n d * (|R| * a) + b * (|R| * c)\n \u2261\u27e8 ap\u2082 _+_ (rot d |R| a) (rot b |R| c) \u2219 ! proj\u2081 prop.distrib |R| (a * d) (c * b) \u27e9\n |R| * \u2115\u2192\u211a.\u03b5N (\u03b5\u2080 +\u2115\u211a \u03b5\u2081) k\n \u220e\n where\n open \u2264-Reasoning\n rot : \u2200 x y z \u2192 x * (y * z) \u2261 y * (z * x)\n rot x y z = prop.*-comm x (y * z) \u2219 prop.*-assoc y z x\n |R| = Card (R\u1d41 k)\n #f = # (f k)\n #g = # (g k)\n #h = # (h k)\n a = \u2115\u2192\u211a.\u03b5N \u03b5\u2080 k\n b = \u2115\u2192\u211a.\u03b5D \u03b5\u2080 k\n c = \u2115\u2192\u211a.\u03b5N \u03b5\u2081 k\n d = \u2115\u2192\u211a.\u03b5D \u03b5\u2081 k\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --copatterns #-}\nopen import Algebra\n\nopen import Function\n\nopen import Data.Nat.NP\nopen import Data.Nat.Distance\nopen import Data.Nat.Properties\nopen import Data.Two\nopen import Data.Zero\nopen import Data.Product\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\n\nopen import HoTT\nopen Equivalences\n\nopen import Explore.Core\nopen import Explore.Universe.Type {\ud835\udfd8}\nopen import Explore.Universe.Base\n\nmodule Neglible where\n\nmodule prop = CommutativeSemiring commutativeSemiring\nmodule OR = Poset (DecTotalOrder.poset decTotalOrder)\n\n\u2264-*-cancel : \u2200 {x m n} \u2192 1 \u2264 x \u2192 x * m \u2264 x * n \u2192 m \u2264 n\n\u2264-*-cancel {suc x} {m} {n} (s\u2264s le) mn\n rewrite prop.*-comm (suc x) m | prop.*-comm (suc x) n = cancel-*-right-\u2264 _ _ _ mn\n\nrecord \u2115\u2192\u211a : Set where\n constructor _\/_[_]\n field\n \u03b5N : (n : \u2115) \u2192 \u2115\n \u03b5D : (n : \u2115) \u2192 \u2115\n \u03b5D-pos : \u2200 n \u2192 \u03b5D n > 0\n\nrecord Is-Neg (\u03b5 : \u2115\u2192\u211a) : Set where\n constructor mk\n open \u2115\u2192\u211a \u03b5\n field\n c\u2099 : (c : \u2115) \u2192 \u2115\n prf : \u2200(c n : \u2115) \u2192 n > c\u2099 n \u2192 n ^ c * \u03b5N n \u2264 \u03b5D n\nopen Is-Neg\n\n0\u2115\u211a : \u2115\u2192\u211a\n\u2115\u2192\u211a.\u03b5N 0\u2115\u211a _ = 0\n\u2115\u2192\u211a.\u03b5D 0\u2115\u211a _ = 1\n\u2115\u2192\u211a.\u03b5D-pos 0\u2115\u211a _ = s\u2264s z\u2264n\n\n0\u2115\u211a-neg : Is-Neg 0\u2115\u211a\nc\u2099 0\u2115\u211a-neg _ = 0\nprf 0\u2115\u211a-neg c n x = OR.trans (OR.reflexive (proj\u2082 prop.zero (n ^ c))) z\u2264n\n\n_+\u2115\u211a_ : \u2115\u2192\u211a \u2192 \u2115\u2192\u211a \u2192 \u2115\u2192\u211a\n\u2115\u2192\u211a.\u03b5N ((\u03b5N \/ \u03b5D [ _ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ _ ])) n = \u03b5N n * \u03bcD n + \u03bcN n * \u03b5D n\n\u2115\u2192\u211a.\u03b5D ((\u03b5N \/ \u03b5D [ _ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ _ ])) n = \u03b5D n * \u03bcD n\n\u2115\u2192\u211a.\u03b5D-pos ((\u03b5N \/ \u03b5D [ \u03b5D+ ]) +\u2115\u211a (\u03bcN \/ \u03bcD [ \u03bcD+ ])) n = \u03b5D+ n *-mono \u03bcD+ n\n\n\n+\u2115\u211a-neg : {\u03b5 \u03bc : \u2115\u2192\u211a} \u2192 Is-Neg \u03b5 \u2192 Is-Neg \u03bc \u2192 Is-Neg (\u03b5 +\u2115\u211a \u03bc)\nc\u2099 (+\u2115\u211a-neg \u03b5 \u03bc) n = 1 + c\u2099 \u03b5 n + c\u2099 \u03bc n\nprf (+\u2115\u211a-neg {\u03b5M} {\u03bcM} \u03b5 \u03bc) c n n>nc = \u2264-*-cancel {x = n} (OR.trans (s\u2264s z\u2264n) n>nc) lemma\n where\n\n open \u2264-Reasoning\n open \u2115\u2192\u211a \u03b5M\n open \u2115\u2192\u211a \u03bcM renaming (\u03b5N to \u03bcN; \u03b5D to \u03bcD; \u03b5D-pos to \u03bcD-pos)\n\n lemma = n * (n ^ c * (\u03b5N n * \u03bcD n + \u03bcN n * \u03b5D n))\n \u2261\u27e8 ! prop.*-assoc n (n ^ c) _\n \u2219 proj\u2081 prop.distrib (n ^ (1 + c)) (\u03b5N n * \u03bcD n) (\u03bcN n * \u03b5D n)\n \u2219 ap\u2082 _+_ (! prop.*-assoc (n ^ (1 + c)) (\u03b5N n) (\u03bcD n))\n (! (prop.*-assoc (n ^ (1 + c)) (\u03bcN n) (\u03b5D n))) \u27e9\n n ^ (1 + c) * \u03b5N n * \u03bcD n + n ^ (1 + c) * \u03bcN n * \u03b5D n\n \u2264\u27e8 (prf \u03b5 (1 + c) n (OR.trans (s\u2264s (\u2264-step (m\u2264m+n (c\u2099 \u03b5 n) (c\u2099 \u03bc n)))) n>nc) *-mono (\u03bcD n \u220e))\n +-mono (prf \u03bc (1 + c) n (OR.trans (s\u2264s (\u2264-step (n\u2264m+n (c\u2099 \u03b5 n) (c\u2099 \u03bc n)))) n>nc) *-mono (\u03b5D n \u220e)) \u27e9\n \u03b5D n * \u03bcD n + \u03bcD n * \u03b5D n\n \u2261\u27e8 ap\u2082 _+_ (refl {x = \u03b5D n * \u03bcD n}) (prop.*-comm (\u03bcD n) (\u03b5D n) \u2219 ! proj\u2082 prop.+-identity (\u03b5D n * \u03bcD n)) \u27e9\n 2 * (\u03b5D n * \u03bcD n)\n \u2264\u27e8 OR.trans (s\u2264s (s\u2264s z\u2264n)) n>nc *-mono (\u03b5D n * \u03bcD n \u220e) \u27e9\n n * (\u03b5D n * \u03bcD n)\n \u220e\n\ninfix 4 _\u2264\u2192_\nrecord _\u2264\u2192_ (f g : \u2115\u2192\u211a) : Set where\n constructor mk\n open \u2115\u2192\u211a f renaming (\u03b5N to fN; \u03b5D to fD)\n open \u2115\u2192\u211a g renaming (\u03b5N to gN; \u03b5D to gD)\n field\n -- fN k \/ fD k \u2264 gN k \/ gD k\n \u2264\u2192 : \u2200 k \u2192 fN k * gD k \u2264 gN k * fD k\n\n\u2264\u2192-trans : \u2200 {f g h} \u2192 f \u2264\u2192 g \u2192 g \u2264\u2192 h \u2192 f \u2264\u2192 h\n_\u2264\u2192_.\u2264\u2192 (\u2264\u2192-trans {fN \/ fD [ fD-pos ]} {gN \/ gD [ gD-pos ]} {hN \/ hD [ hD-pos ]} (mk fg) (mk gh)) k\n = \u2264-*-cancel (gD-pos k) lemma\n where\n open \u2264-Reasoning\n lemma : gD k * (fN k * hD k) \u2264 gD k * (hN k * fD k)\n lemma = gD k * (fN k * hD k)\n \u2261\u27e8 ! prop.*-assoc (gD k) (fN k) (hD k)\n \u2219 ap (flip _*_ (hD k)) (prop.*-comm (gD k) (fN k))\n \u27e9\n (fN k * gD k) * hD k\n \u2264\u27e8 fg k *-mono OR.refl \u27e9\n (gN k * fD k) * hD k\n \u2261\u27e8 prop.*-assoc (gN k) (fD k) (hD k)\n \u2219 ap (_*_ (gN k)) (prop.*-comm (fD k) (hD k))\n \u2219 ! prop.*-assoc (gN k) (hD k) (fD k)\n \u27e9\n (gN k * hD k) * fD k\n \u2264\u27e8 gh k *-mono OR.refl \u27e9\n (hN k * gD k) * fD k\n \u2261\u27e8 ap (flip _*_ (fD k)) (prop.*-comm (hN k) (gD k))\n \u2219 prop.*-assoc (gD k) (hN k) (fD k)\n \u27e9\n gD k * (hN k * fD k)\n \u220e\n\n+\u2115\u211a-mono : \u2200 {f f' g g'} \u2192 f \u2264\u2192 f' \u2192 g \u2264\u2192 g' \u2192 f +\u2115\u211a g \u2264\u2192 f' +\u2115\u211a g'\n_\u2264\u2192_.\u2264\u2192 (+\u2115\u211a-mono {fN \/ fD [ _ ]} {f'N \/ f'D [ _ ]} {gN \/ gD [ _ ]} {g'N \/ g'D [ _ ]} (mk ff) (mk gg)) k\n = (fN k * gD k + gN k * fD k) * (f'D k * g'D k)\n \u2261\u27e8 proj\u2082 prop.distrib (f'D k * g'D k) (fN k * gD k) (gN k * fD k) \u27e9\n fN k * gD k * (f'D k * g'D k) + gN k * fD k * (f'D k * g'D k)\n \u2261\u27e8 ap\u2082 _+_ (*-interchange {fN k} {gD k} {f'D k} {g'D k} \u2219 ap (_*_ (fN k * f'D k)) (prop.*-comm (gD k) (g'D k)))\n (ap (_*_ (gN k * fD k)) (prop.*-comm (f'D k) (g'D k)) \u2219 *-interchange {gN k} {fD k} {g'D k} {f'D k})\n \u27e9\n fN k * f'D k * (g'D k * gD k) + gN k * g'D k * (fD k * f'D k)\n \u2264\u27e8 (ff k *-mono OR.refl) +-mono (gg k *-mono OR.refl) \u27e9\n f'N k * fD k * (g'D k * gD k) + g'N k * gD k * (fD k * f'D k)\n \u2261\u27e8 ap\u2082 _+_ (*-interchange {f'N k} {fD k} {g'D k} {gD k})\n (ap (_*_ (g'N k * gD k)) (prop.*-comm (fD k) (f'D k))\n \u2219 *-interchange {g'N k} {gD k} {f'D k} {fD k}\n \u2219 ap (_*_ (g'N k * f'D k)) (prop.*-comm (gD k) (fD k)))\n \u27e9\n f'N k * g'D k * (fD k * gD k) + g'N k * f'D k * (fD k * gD k)\n \u2261\u27e8 ! proj\u2082 prop.distrib (fD k * gD k) (f'N k * g'D k) (g'N k * f'D k) \u27e9\n (f'N k * g'D k + g'N k * f'D k) * (fD k * gD k)\n \u220e\n where\n open \u2264-Reasoning\n\nrecord NegBounded (f : \u2115\u2192\u211a) : Set where\n constructor mk\n field\n \u03b5 : \u2115\u2192\u211a\n \u03b5-neg : Is-Neg \u03b5\n bounded : f \u2264\u2192 \u03b5\n\nmodule _ where\n open NegBounded\n \u2264-NB : {f g : \u2115\u2192\u211a} \u2192 f \u2264\u2192 g \u2192 NegBounded g \u2192 NegBounded f\n \u03b5 (\u2264-NB le nb) = \u03b5 nb\n \u03b5-neg (\u2264-NB le nb) = \u03b5-neg nb\n bounded (\u2264-NB le nb) = \u2264\u2192-trans le (bounded nb)\n\n _+NB_ : {f g : \u2115\u2192\u211a} \u2192 NegBounded f \u2192 NegBounded g \u2192 NegBounded (f +\u2115\u211a g)\n \u03b5 (fNB +NB gNB) = \u03b5 fNB +\u2115\u211a \u03b5 gNB\n \u03b5-neg (fNB +NB gNB) = +\u2115\u211a-neg (\u03b5-neg fNB) (\u03b5-neg gNB)\n bounded (fNB +NB gNB) = +\u2115\u211a-mono (bounded fNB) (bounded gNB)\n\nmodule ~-NegBounded (R\u1d41 : \u2115 \u2192 U)(let R = \u03bb n \u2192 El (R\u1d41 n))(inh : \u2200 x \u2192 0 < Card (R\u1d41 x)) where\n\n # : \u2200 {n} \u2192 Count (R n)\n # {n} = count (R\u1d41 n)\n\n ~dist : (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) \u2192 \u2115\u2192\u211a\n \u2115\u2192\u211a.\u03b5N (~dist f g) n = dist (# (f n)) (# (g n))\n \u2115\u2192\u211a.\u03b5D (~dist f g) n = Card (R\u1d41 n)\n \u2115\u2192\u211a.\u03b5D-pos (~dist f g) n = inh n\n\n ~dist-sum : \u2200 f g h \u2192 ~dist f h \u2264\u2192 ~dist f g +\u2115\u211a ~dist g h\n _\u2264\u2192_.\u2264\u2192 (~dist-sum f g h) k\n = #fh * (|R| * |R|)\n \u2264\u27e8 dist-sum #f #g #h *-mono OR.refl \u27e9\n (#fg + #gh) * (|R| * |R|)\n \u2261\u27e8 ! prop.*-assoc (#fg + #gh) |R| |R| \u2219 ap (flip _*_ |R|) (proj\u2082 prop.distrib |R| #fg #gh) \u27e9\n (#fg * |R| + #gh * |R|) * |R|\n \u220e\n where\n open \u2264-Reasoning\n |R| = Card (R\u1d41 k)\n #f = # (f k)\n #g = # (g k)\n #h = # (h k)\n #fh = dist #f #h\n #fg = dist #f #g\n #gh = dist #g #h\n\n record _~_ (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) : Set where\n constructor mk\n field\n ~ : NegBounded (~dist f g)\n\n ~-trans : Transitive _~_\n _~_.~ (~-trans {f}{g}{h} (mk fg) (mk gh)) = \u2264-NB (~dist-sum f g h) (fg +NB gh)\n\nmodule ~-Inlined (R\u1d41 : \u2115 \u2192 U)(let R = \u03bb n \u2192 El (R\u1d41 n)) where\n\n # : \u2200 {n} \u2192 Count (R n)\n # {n} = count (R\u1d41 n)\n\n record _~_ (f g : (x : \u2115) \u2192 R x \u2192 \ud835\udfda) : Set where\n constructor mk\n field\n \u03b5 : \u2115\u2192\u211a\n open \u2115\u2192\u211a \u03b5\n field\n \u03b5-neg : Is-Neg \u03b5\n bounded : \u2200 k \u2192 \u03b5D k * dist (# (f k)) (# (g k)) \u2264 Card (R\u1d41 k) * \u03b5N k\n\n\n ~-trans : Transitive _~_\n _~_.\u03b5 (~-trans x x\u2081) = _\n _~_.\u03b5-neg (~-trans x x\u2081) = +\u2115\u211a-neg (_~_.\u03b5-neg x) (_~_.\u03b5-neg x\u2081)\n _~_.bounded (~-trans {f}{g}{h}(mk \u03b5\u2080 \u03b5\u2080-neg fg) (mk \u03b5\u2081 \u03b5\u2081-neg gh)) k\n = (b * d) * dist #f #h\n \u2264\u27e8 (b * d \u220e) *-mono dist-sum #f #g #h \u27e9\n (b * d) * (dist #f #g + dist #g #h)\n \u2261\u27e8 proj\u2081 prop.distrib (b * d) (dist #f #g) (dist #g #h)\n \u2219 ap\u2082 _+_ (ap\u2082 _*_ (prop.*-comm b d) refl\n \u2219 prop.*-assoc d b (dist #f #g)) (prop.*-assoc b d (dist #g #h))\n \u27e9\n d * (b * dist #f #g) + b * (d * dist #g #h)\n \u2264\u27e8 ((d \u220e) *-mono fg k) +-mono ((b \u220e) *-mono gh k) \u27e9\n d * (|R| * a) + b * (|R| * c)\n \u2261\u27e8 ap\u2082 _+_ (rot d |R| a) (rot b |R| c) \u2219 ! proj\u2081 prop.distrib |R| (a * d) (c * b) \u27e9\n |R| * \u2115\u2192\u211a.\u03b5N (\u03b5\u2080 +\u2115\u211a \u03b5\u2081) k\n \u220e\n where\n open \u2264-Reasoning\n rot : \u2200 x y z \u2192 x * (y * z) \u2261 y * (z * x)\n rot x y z = prop.*-comm x (y * z) \u2219 prop.*-assoc y z x\n |R| = Card (R\u1d41 k)\n #f = # (f k)\n #g = # (g k)\n #h = # (h k)\n a = \u2115\u2192\u211a.\u03b5N \u03b5\u2080 k\n b = \u2115\u2192\u211a.\u03b5D \u03b5\u2080 k\n c = \u2115\u2192\u211a.\u03b5N \u03b5\u2081 k\n d = \u2115\u2192\u211a.\u03b5D \u03b5\u2081 k\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"de617d2ad76260052f9d6ecbf3c054fc7eb183ef","subject":"Agda: Remove unused imports violating architectural constraints","message":"Agda: Remove unused imports violating architectural constraints\n\nThe parametric formalization shouldn't import stuff specific to the\nPOPL14 calculus.\n\nOld-commit-hash: 30c3071e33a99422b2c878f7bca7937e8b1c216f\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Value.agda","new_file":"Parametric\/Change\/Value.agda","new_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Value\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\nopen ChangeType.Structure Base \u0394Base\n\nopen import Base.Denotation.Notation\n\n-- `diff` and `apply`, without validity proofs\n\nApplyStructure : Set\nApplyStructure = \u2200 \u03b9 \u2192 \u27e6 \u03b9 \u27e7Base \u2192 \u27e6 \u0394Base \u03b9 \u27e7Base \u2192 \u27e6 \u03b9 \u27e7Base\n\nDiffStructure : Set\nDiffStructure = \u2200 \u03b9 \u2192 \u27e6 \u03b9 \u27e7Base \u2192 \u27e6 \u03b9 \u27e7Base \u2192 \u27e6 \u0394Base \u03b9 \u27e7Base\n\nmodule Structure\n (\u27e6apply-base\u27e7 : ApplyStructure)\n (\u27e6diff-base\u27e7 : DiffStructure)\n where\n\n \u27e6apply\u27e7 : \u2200 \u03c4 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n \u27e6diff\u27e7 : \u2200 \u03c4 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7\n\n infixl 6 \u27e6apply\u27e7 \u27e6diff\u27e7\n syntax \u27e6apply\u27e7 \u03c4 v dv = v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 dv\n syntax \u27e6diff\u27e7 \u03c4 u v = u \u27e6\u229d\u208d \u03c4 \u208e\u27e7 v\n\n v \u27e6\u2295\u208d base \u03b9 \u208e\u27e7 \u0394v = \u27e6apply-base\u27e7 \u03b9 v \u0394v\n f \u27e6\u2295\u208d \u03c3 \u21d2 \u03c4 \u208e\u27e7 \u0394f = \u03bb v \u2192 f v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u0394f v (v \u27e6\u229d\u208d \u03c3 \u208e\u27e7 v)\n\n u \u27e6\u229d\u208d base \u03b9 \u208e\u27e7 v = \u27e6diff-base\u27e7 \u03b9 u v\n g \u27e6\u229d\u208d \u03c3 \u21d2 \u03c4 \u208e\u27e7 f = \u03bb v \u0394v \u2192 (g (v \u27e6\u2295\u208d \u03c3 \u208e\u27e7 \u0394v)) \u27e6\u229d\u208d \u03c4 \u208e\u27e7 (f v)\n\n _\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n _\u27e6\u2295\u27e7_ {\u03c4} = \u27e6apply\u27e7 \u03c4\n\n _\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7\n _\u27e6\u229d\u27e7_ {\u03c4} = \u27e6diff\u27e7 \u03c4\n\n alternate : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 mapContext \u0394Type \u0393 \u27e7 \u2192 \u27e6 \u0394Context \u0393 \u27e7\n alternate {\u2205} \u2205 \u2205 = \u2205\n alternate {\u03c4 \u2022 \u0393} (v \u2022 \u03c1) (dv \u2022 d\u03c1) = dv \u2022 v \u2022 alternate \u03c1 d\u03c1\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Denotation.Value as Value\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Value\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\nopen Value.Structure Base \u27e6_\u27e7Base\nopen ChangeType.Structure Base \u0394Base\n\nopen import Base.Denotation.Notation\n\nopen import Data.Integer\nopen import Structure.Bag.Popl14\n\n-- `diff` and `apply`, without validity proofs\n\nApplyStructure : Set\nApplyStructure = \u2200 \u03b9 \u2192 \u27e6 \u03b9 \u27e7Base \u2192 \u27e6 \u0394Base \u03b9 \u27e7Base \u2192 \u27e6 \u03b9 \u27e7Base\n\nDiffStructure : Set\nDiffStructure = \u2200 \u03b9 \u2192 \u27e6 \u03b9 \u27e7Base \u2192 \u27e6 \u03b9 \u27e7Base \u2192 \u27e6 \u0394Base \u03b9 \u27e7Base\n\nmodule Structure\n (\u27e6apply-base\u27e7 : ApplyStructure)\n (\u27e6diff-base\u27e7 : DiffStructure)\n where\n\n \u27e6apply\u27e7 : \u2200 \u03c4 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n \u27e6diff\u27e7 : \u2200 \u03c4 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7\n\n infixl 6 \u27e6apply\u27e7 \u27e6diff\u27e7\n syntax \u27e6apply\u27e7 \u03c4 v dv = v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 dv\n syntax \u27e6diff\u27e7 \u03c4 u v = u \u27e6\u229d\u208d \u03c4 \u208e\u27e7 v\n\n v \u27e6\u2295\u208d base \u03b9 \u208e\u27e7 \u0394v = \u27e6apply-base\u27e7 \u03b9 v \u0394v\n f \u27e6\u2295\u208d \u03c3 \u21d2 \u03c4 \u208e\u27e7 \u0394f = \u03bb v \u2192 f v \u27e6\u2295\u208d \u03c4 \u208e\u27e7 \u0394f v (v \u27e6\u229d\u208d \u03c3 \u208e\u27e7 v)\n\n u \u27e6\u229d\u208d base \u03b9 \u208e\u27e7 v = \u27e6diff-base\u27e7 \u03b9 u v\n g \u27e6\u229d\u208d \u03c3 \u21d2 \u03c4 \u208e\u27e7 f = \u03bb v \u0394v \u2192 (g (v \u27e6\u2295\u208d \u03c3 \u208e\u27e7 \u0394v)) \u27e6\u229d\u208d \u03c4 \u208e\u27e7 (f v)\n\n _\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n _\u27e6\u2295\u27e7_ {\u03c4} = \u27e6apply\u27e7 \u03c4\n\n _\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394Type \u03c4 \u27e7\n _\u27e6\u229d\u27e7_ {\u03c4} = \u27e6diff\u27e7 \u03c4\n\n alternate : \u2200 {\u0393} \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 mapContext \u0394Type \u0393 \u27e7 \u2192 \u27e6 \u0394Context \u0393 \u27e7\n alternate {\u2205} \u2205 \u2205 = \u2205\n alternate {\u03c4 \u2022 \u0393} (v \u2022 \u03c1) (dv \u2022 d\u03c1) = dv \u2022 v \u2022 alternate \u03c1 d\u03c1\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3eec36d839aa4ec0250dbd55c9767a1d968d0649","subject":"proof of associativity and commutativity hard way","message":"proof of associativity and commutativity hard way\n","repos":"piyush-kurur\/sample-code","old_file":"agda\/Nat.agda","new_file":"agda\/Nat.agda","new_contents":"module Nat where\n\ndata \u2115 : Set where\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero + b = b\nsucc a + b = succ (a + b)\n\n_\u00d7_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero \u00d7 b = zero\nsucc a \u00d7 b = (a \u00d7 b) + b\n\n\nopen import Relation.Binary.PropositionalEquality\n\n0-is-right-identity-of-+ : \u2200 (n : \u2115) \u2192 n + zero \u2261 n\n0-is-right-identity-of-+ zero = refl\n0-is-right-identity-of-+ (succ n) = cong succ (0-is-right-identity-of-+ n)\n\n\n+-is-associative : \u2200 (a b c : \u2115) \u2192 a + (b + c) \u2261 (a + b) + c\n+-is-associative zero b c = refl\n+-is-associative (succ a) b c = cong succ (+-is-associative a b c)\n\n\nlemma : \u2200 (a b : \u2115) \u2192 a + succ b \u2261 succ (a + b)\nlemma zero b = refl\nlemma (succ a) b = cong succ (lemma a b)\n\n+-is-commutative : \u2200 (a b : \u2115) \u2192 a + b \u2261 b + a\n+-is-commutative a zero = 0-is-right-identity-of-+ a\n+-is-commutative a (succ b)\n = trans (lemma a b) (cong succ (+-is-commutative a b))\n","old_contents":"module Nat where\n\ndata \u2115 : Set where\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero + b = b\nsucc a + b = succ (a + b)\n\n_\u00d7_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero \u00d7 b = zero\nsucc a \u00d7 b = (a \u00d7 b) + b\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"19985bd94f56f9d826d6eae344261fe20f4b7f4d","subject":"Removed empty line.","message":"Removed empty line.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Relation\/Binary\/Bisimilarity\/PropertiesI.agda","new_file":"src\/fot\/FOTC\/Relation\/Binary\/Bisimilarity\/PropertiesI.agda","new_contents":"------------------------------------------------------------------------------\n-- Properties for the bisimilarity relation\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Relation.Binary.Bisimilarity.PropertiesI where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Base.List.PropertiesI\nopen import FOTC.Base.PropertiesI\nopen import FOTC.Data.Stream\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n\n\u2248-refl : \u2200 {xs} \u2192 Stream xs \u2192 xs \u2248 xs\n\u2248-refl {xs} Sxs = \u2248-coind R h\u2081 h\u2082\n where\n R : D \u2192 D \u2192 Set\n R xs ys = Stream xs \u2227 xs \u2261 ys\n\n h\u2081 : \u2200 {xs ys} \u2192 R xs ys \u2192 \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys' \u2227 R xs' ys'\n h\u2081 (Sxs , refl) with Stream-unf Sxs\n ... | x' , xs' , prf , Sxs' =\n x' , xs' , xs' , prf , prf , (Sxs' , refl)\n\n h\u2082 : R xs xs\n h\u2082 = Sxs , refl\n\n\u2248-sym : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 ys \u2248 xs\n\u2248-sym {xs} {ys} xs\u2248ys = \u2248-coind R h\u2081 h\u2082\n where\n R : D \u2192 D \u2192 Set\n R xs ys = ys \u2248 xs\n\n h\u2081 : R ys xs \u2192\n \u2203[ y' ] \u2203[ ys' ] \u2203[ xs' ]\n ys \u2261 y' \u2237 ys' \u2227 xs \u2261 y' \u2237 xs' \u2227 R ys' xs'\n h\u2081 Rxsys with \u2248-unf Rxsys\n ... | y' , ys' , xs' , prf\u2081 , prf\u2082 , ys'\u2248xs' =\n y' , xs' , ys' , prf\u2082 , prf\u2081 , ys'\u2248xs'\n\n h\u2082 : R ys xs\n h\u2082 = xs\u2248ys\n\n\u2248-trans : \u2200 {xs ys zs} \u2192 xs \u2248 ys \u2192 ys \u2248 zs \u2192 xs \u2248 zs\n\u2248-trans {xs} {ys} {zs} xs\u2248ys ys\u2248zs = \u2248-coind R h\u2081 h\u2082\n where\n R : D \u2192 D \u2192 Set\n R xs zs = \u2203[ ys ] xs \u2248 ys \u2227 ys \u2248 zs\n\n h\u2081 : R xs zs \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ zs' ]\n xs \u2261 x' \u2237 xs' \u2227 zs \u2261 x' \u2237 zs' \u2227 R xs' zs'\n h\u2081 (ys , xs\u2248ys , ys\u2248zs) with \u2248-unf xs\u2248ys\n ... | x' , xs' , ys' , prf\u2081 , prf\u2082 , xs'\u2248ys' with \u2248-unf ys\u2248zs\n ... | y' , ys'' , zs' , prf\u2083 , prf\u2084 , ys''\u2248zs' =\n x'\n , xs'\n , zs'\n , prf\u2081\n , subst (\u03bb t \u2192 zs \u2261 t \u2237 zs') y'\u2261x' prf\u2084\n , (ys' , (xs'\u2248ys' , (subst (\u03bb t \u2192 t \u2248 zs') ys''\u2261ys' ys''\u2248zs')))\n\n where\n y'\u2261x' : y' \u2261 x'\n y'\u2261x' = \u2227-proj\u2081 (\u2237-injective (trans (sym prf\u2083) prf\u2082))\n\n ys''\u2261ys' : ys'' \u2261 ys'\n ys''\u2261ys' = \u2227-proj\u2082 (\u2237-injective (trans (sym prf\u2083) prf\u2082))\n\n h\u2082 : R xs zs\n h\u2082 = ys , (xs\u2248ys , ys\u2248zs)\n\n\u2237-injective\u2248 : \u2200 {x xs ys} \u2192 x \u2237 xs \u2248 x \u2237 ys \u2192 xs \u2248 ys\n\u2237-injective\u2248 {x} {xs} {ys} h with \u2248-unf h\n... | x' , xs' , ys' , prf\u2081 , prf\u2082 , prf\u2083 = xs\u2248ys\n where\n xs\u2261xs' : xs \u2261 xs'\n xs\u2261xs' = \u2227-proj\u2082 (\u2237-injective prf\u2081)\n\n ys\u2261ys' : ys \u2261 ys'\n ys\u2261ys' = \u2227-proj\u2082 (\u2237-injective prf\u2082)\n\n xs\u2248ys : xs \u2248 ys\n xs\u2248ys = subst (\u03bb t \u2192 t \u2248 ys)\n (sym xs\u2261xs')\n (subst (\u03bb t \u2192 xs' \u2248 t) (sym ys\u2261ys') prf\u2083)\n\n\u2237-rightCong\u2248 : \u2200 {x xs ys} \u2192 xs \u2248 ys \u2192 x \u2237 xs \u2248 x \u2237 ys\n\u2237-rightCong\u2248 {x} {xs} {ys} h = \u2248-pre-fixed (x , xs , ys , refl , refl , h)\n","old_contents":"------------------------------------------------------------------------------\n-- Properties for the bisimilarity relation\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Relation.Binary.Bisimilarity.PropertiesI where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Base.List.PropertiesI\nopen import FOTC.Base.PropertiesI\nopen import FOTC.Data.Stream\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n\n\u2248-refl : \u2200 {xs} \u2192 Stream xs \u2192 xs \u2248 xs\n\u2248-refl {xs} Sxs = \u2248-coind R h\u2081 h\u2082\n where\n R : D \u2192 D \u2192 Set\n R xs ys = Stream xs \u2227 xs \u2261 ys\n\n h\u2081 : \u2200 {xs ys} \u2192 R xs ys \u2192 \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys' \u2227 R xs' ys'\n h\u2081 (Sxs , refl) with Stream-unf Sxs\n ... | x' , xs' , prf , Sxs' =\n x' , xs' , xs' , prf , prf , (Sxs' , refl)\n\n h\u2082 : R xs xs\n h\u2082 = Sxs , refl\n\n\n\u2248-sym : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 ys \u2248 xs\n\u2248-sym {xs} {ys} xs\u2248ys = \u2248-coind R h\u2081 h\u2082\n where\n R : D \u2192 D \u2192 Set\n R xs ys = ys \u2248 xs\n\n h\u2081 : R ys xs \u2192\n \u2203[ y' ] \u2203[ ys' ] \u2203[ xs' ]\n ys \u2261 y' \u2237 ys' \u2227 xs \u2261 y' \u2237 xs' \u2227 R ys' xs'\n h\u2081 Rxsys with \u2248-unf Rxsys\n ... | y' , ys' , xs' , prf\u2081 , prf\u2082 , ys'\u2248xs' =\n y' , xs' , ys' , prf\u2082 , prf\u2081 , ys'\u2248xs'\n\n h\u2082 : R ys xs\n h\u2082 = xs\u2248ys\n\n\u2248-trans : \u2200 {xs ys zs} \u2192 xs \u2248 ys \u2192 ys \u2248 zs \u2192 xs \u2248 zs\n\u2248-trans {xs} {ys} {zs} xs\u2248ys ys\u2248zs = \u2248-coind R h\u2081 h\u2082\n where\n R : D \u2192 D \u2192 Set\n R xs zs = \u2203[ ys ] xs \u2248 ys \u2227 ys \u2248 zs\n\n h\u2081 : R xs zs \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ zs' ]\n xs \u2261 x' \u2237 xs' \u2227 zs \u2261 x' \u2237 zs' \u2227 R xs' zs'\n h\u2081 (ys , xs\u2248ys , ys\u2248zs) with \u2248-unf xs\u2248ys\n ... | x' , xs' , ys' , prf\u2081 , prf\u2082 , xs'\u2248ys' with \u2248-unf ys\u2248zs\n ... | y' , ys'' , zs' , prf\u2083 , prf\u2084 , ys''\u2248zs' =\n x'\n , xs'\n , zs'\n , prf\u2081\n , subst (\u03bb t \u2192 zs \u2261 t \u2237 zs') y'\u2261x' prf\u2084\n , (ys' , (xs'\u2248ys' , (subst (\u03bb t \u2192 t \u2248 zs') ys''\u2261ys' ys''\u2248zs')))\n\n where\n y'\u2261x' : y' \u2261 x'\n y'\u2261x' = \u2227-proj\u2081 (\u2237-injective (trans (sym prf\u2083) prf\u2082))\n\n ys''\u2261ys' : ys'' \u2261 ys'\n ys''\u2261ys' = \u2227-proj\u2082 (\u2237-injective (trans (sym prf\u2083) prf\u2082))\n\n h\u2082 : R xs zs\n h\u2082 = ys , (xs\u2248ys , ys\u2248zs)\n\n\u2237-injective\u2248 : \u2200 {x xs ys} \u2192 x \u2237 xs \u2248 x \u2237 ys \u2192 xs \u2248 ys\n\u2237-injective\u2248 {x} {xs} {ys} h with \u2248-unf h\n... | x' , xs' , ys' , prf\u2081 , prf\u2082 , prf\u2083 = xs\u2248ys\n where\n xs\u2261xs' : xs \u2261 xs'\n xs\u2261xs' = \u2227-proj\u2082 (\u2237-injective prf\u2081)\n\n ys\u2261ys' : ys \u2261 ys'\n ys\u2261ys' = \u2227-proj\u2082 (\u2237-injective prf\u2082)\n\n xs\u2248ys : xs \u2248 ys\n xs\u2248ys = subst (\u03bb t \u2192 t \u2248 ys)\n (sym xs\u2261xs')\n (subst (\u03bb t \u2192 xs' \u2248 t) (sym ys\u2261ys') prf\u2083)\n\n\u2237-rightCong\u2248 : \u2200 {x xs ys} \u2192 xs \u2248 ys \u2192 x \u2237 xs \u2248 x \u2237 ys\n\u2237-rightCong\u2248 {x} {xs} {ys} h = \u2248-pre-fixed (x , xs , ys , refl , refl , h)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"1cbcb26f8da55497f03917175c94772edb7077cd","subject":"Cosmetic change.","message":"Cosmetic change.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Program\/Collatz\/ConversionRulesATP.agda","new_file":"src\/fot\/FOTC\/Program\/Collatz\/ConversionRulesATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Conversion rules for the Collatz function\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Program.Collatz.ConversionRulesATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Program.Collatz.Collatz\nopen import FOTC.Program.Collatz.Data.Nat\n\n------------------------------------------------------------------------------\n-- Conversion rules for the Collatz function.\npostulate\n collatz-0 : collatz zero \u2261 [1]\n collatz-1 : collatz [1] \u2261 [1]\n collatz-even : \u2200 {n} \u2192 Even (succ\u2081 (succ\u2081 n)) \u2192\n collatz (succ\u2081 (succ\u2081 n)) \u2261\n collatz (div (succ\u2081 (succ\u2081 n)) [2])\n collatz-noteven : \u2200 {n} \u2192 NotEven (succ\u2081 (succ\u2081 n)) \u2192\n collatz (succ\u2081 (succ\u2081 n)) \u2261\n collatz ([3] * (succ\u2081 (succ\u2081 n)) + [1])\n{-# ATP prove collatz-0 #-}\n{-# ATP prove collatz-1 #-}\n{-# ATP prove collatz-even #-}\n{-# ATP prove collatz-noteven #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Conversion rules for the Collatz function\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Program.Collatz.ConversionRulesATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Program.Collatz.Collatz\nopen import FOTC.Program.Collatz.Data.Nat\n\n------------------------------------------------------------------------------\n-- Conversion rules for the Collatz function.\npostulate\n collatz-0 : collatz zero \u2261 [1]\n collatz-1 : collatz [1] \u2261 [1]\n collatz-even : \u2200 {n} \u2192 Even (succ\u2081 (succ\u2081 n)) \u2192\n collatz (succ\u2081 (succ\u2081 n)) \u2261 collatz (div (succ\u2081 (succ\u2081 n)) [2])\n collatz-noteven : \u2200 {n} \u2192 NotEven (succ\u2081 (succ\u2081 n)) \u2192\n collatz (succ\u2081 (succ\u2081 n)) \u2261\n collatz ([3] * (succ\u2081 (succ\u2081 n)) + [1])\n{-# ATP prove collatz-0 #-}\n{-# ATP prove collatz-1 #-}\n{-# ATP prove collatz-even #-}\n{-# ATP prove collatz-noteven #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b0acfde8e76ff27fb9d7b8609c3d7acf1ccf1ef2","subject":"update PLDI14-List-of-Theorems.agda for consistency with #4","message":"update PLDI14-List-of-Theorems.agda for consistency with #4\n\nCamera-ready version inserts a \"lemma 2.5\", which means math items\n2.5--2.9 should be renumbered 2.6--2.10.\n\nRelevant commits:\n23339dd5a9e56c3c31d42252c4020b716ec77725\n0c1e97fbf3b62bddf23559545a4c801b4189cb29\n","repos":"inc-lc\/ilc-agda","old_file":"PLDI14-List-of-Theorems.agda","new_file":"PLDI14-List-of-Theorems.agda","new_contents":"module PLDI14-List-of-Theorems where\n\n-- List of theorems in PLDI submission\n--\n-- For hints about installation and execution, please refer\n-- to README.agda.\n--\n-- Agda modules corresponding to definitions, lemmas and theorems\n-- are listed here with the most important names. For example,\n-- after this file type checks (C-C C-L), placing the cursor\n-- on the purple \"Base.Change.Algebra\" and pressing M-. will\n-- bring you to the file where change structures are defined.\n-- The name for change structures in that file is\n-- \"ChangeAlgebra\", given in the using-clause.\n\n-- Definition 2.1 (Change structures)\nopen import Base.Change.Algebra using (ChangeAlgebra)\n---- Carrier in record ChangeAlgebra --(a)\nopen Base.Change.Algebra.ChangeAlgebra using (Change) --(b)\nopen Base.Change.Algebra.ChangeAlgebra using (update) --(c)\nopen Base.Change.Algebra.ChangeAlgebra using (diff) --(d)\nopen Base.Change.Algebra.IsChangeAlgebra using (update-diff)--(e)\n\n-- Definition 2.2 (Nil change)\n-- IsChangeAlgebra.nil\nopen Base.Change.Algebra using (IsChangeAlgebra)\n\n-- Lemma 2.3 (Behavior of nil)\n-- IsChangeAlgebra.update-nil\nopen Base.Change.Algebra using (IsChangeAlgebra)\n\n-- Lemma 2.5 (Behavior of derivatives on nil)\nopen import Base.Change.Equivalence using (deriv-zero)\n\n-- Definition 2.4 (Derivatives)\nopen Base.Change.Algebra using (Derivative)\n\n-- Definition 2.6 (Carrier set of function changes)\nopen Base.Change.Algebra.FunctionChanges\n\n-- Definition 2.7 (Operations on function changes)\n-- ChangeAlgebra.update FunctionChanges.changeAlgebra\n-- ChangeAlgebra.diff FunctionChanges.changeAlgebra\nopen Base.Change.Algebra.FunctionChanges using (changeAlgebra)\n\n-- Theorem 2.8 (Function changes form a change structure)\n-- (In Agda, the proof of Theorem 2.8 has to be included in the\n-- definition of function changes, here\n-- FunctionChanges.changeAlgebra.)\nopen Base.Change.Algebra.FunctionChanges using (changeAlgebra)\n\n-- Theorem 2.9 (Incrementalization)\nopen Base.Change.Algebra.FunctionChanges using (incrementalization)\n\n-- Theorem 2.10 (Nil changes are derivatives)\nopen Base.Change.Algebra.FunctionChanges using (nil-is-derivative)\n\n-- Definition 3.1 (Domains)\nimport Parametric.Denotation.Value\nopen Parametric.Denotation.Value.Structure using (\u27e6_\u27e7Type)\n\n-- Definition 3.2 (Environments)\nopen import Base.Denotation.Environment using (\u27e6_\u27e7Context)\n\n-- Definition 3.3 (Evaluation)\nimport Parametric.Denotation.Evaluation\nopen Parametric.Denotation.Evaluation.Structure using (\u27e6_\u27e7Term)\n\n-- Definition 3.4 (Changes)\n-- Definition 3.5 (Change environments)\nimport Parametric.Change.Validity\nopen Parametric.Change.Validity.Structure using (change-algebra)\nopen Parametric.Change.Validity.Structure using (environment-changes)\n\n-- Definition 3.6 (Change semantics)\n-- Lemma 3.7 (Change semantics is the derivative of semantics)\nimport Parametric.Change.Specification\nopen Parametric.Change.Specification.Structure using (\u27e6_\u27e7\u0394)\nopen Parametric.Change.Specification.Structure using (correctness)\n\n-- Definition 3.8 (Erasure)\n-- Lemma 3.9 (The erased version of a change is almost the same)\nimport Parametric.Change.Implementation\nopen Parametric.Change.Implementation.Structure using (_\u2248_)\nopen Parametric.Change.Implementation.Structure using (carry-over)\n\n-- Lemma 3.10 (\u27e6 t \u27e7\u0394 erases to Derive(t))\n-- Theorem 3.11 (Correctness of differentiation)\nimport Parametric.Change.Correctness\nopen Parametric.Change.Correctness.Structure using (derive-correct-closed)\nopen Parametric.Change.Correctness.Structure using (main-theorem)\n","old_contents":"module PLDI14-List-of-Theorems where\n\n-- List of theorems in PLDI submission\n--\n-- For hints about installation and execution, please refer\n-- to README.agda.\n--\n-- Agda modules corresponding to definitions, lemmas and theorems\n-- are listed here with the most important names. For example,\n-- after this file type checks (C-C C-L), placing the cursor\n-- on the purple \"Base.Change.Algebra\" and pressing M-. will\n-- bring you to the file where change structures are defined.\n-- The name for change structures in that file is\n-- \"ChangeAlgebra\", given in the using-clause.\n\n-- Definition 2.1 (Change structures)\nopen import Base.Change.Algebra using (ChangeAlgebra)\n---- Carrier in record ChangeAlgebra --(a)\nopen Base.Change.Algebra.ChangeAlgebra using (Change) --(b)\nopen Base.Change.Algebra.ChangeAlgebra using (update) --(c)\nopen Base.Change.Algebra.ChangeAlgebra using (diff) --(d)\nopen Base.Change.Algebra.IsChangeAlgebra using (update-diff)--(e)\n\n-- Definition 2.2 (Nil change)\n-- IsChangeAlgebra.nil\nopen Base.Change.Algebra using (IsChangeAlgebra)\n\n-- Lemma 2.3 (Behavior of nil)\n-- IsChangeAlgebra.update-nil\nopen Base.Change.Algebra using (IsChangeAlgebra)\n\n-- Definition 2.4 (Derivatives)\nopen Base.Change.Algebra using (Derivative)\n\n-- Definition 2.5 (Carrier set of function changes)\nopen Base.Change.Algebra.FunctionChanges\n\n-- Definition 2.6 (Operations on function changes)\n-- ChangeAlgebra.update FunctionChanges.changeAlgebra\n-- ChangeAlgebra.diff FunctionChanges.changeAlgebra\nopen Base.Change.Algebra.FunctionChanges using (changeAlgebra)\n\n-- Theorem 2.7 (Function changes form a change structure)\n-- (In Agda, the proof of Theorem 2.7 has to be included in the\n-- definition of function changes, here\n-- FunctionChanges.changeAlgebra.)\nopen Base.Change.Algebra.FunctionChanges using (changeAlgebra)\n\n-- Lemma 2.8 (Incrementalization)\nopen Base.Change.Algebra.FunctionChanges using (incrementalization)\n\n-- Theorem 2.9 (Nil changes are derivatives)\nopen Base.Change.Algebra.FunctionChanges using (nil-is-derivative)\n\n-- Definition 3.1 (Domains)\nimport Parametric.Denotation.Value\nopen Parametric.Denotation.Value.Structure using (\u27e6_\u27e7Type)\n\n-- Definition 3.2 (Environments)\nopen import Base.Denotation.Environment using (\u27e6_\u27e7Context)\n\n-- Definition 3.3 (Evaluation)\nimport Parametric.Denotation.Evaluation\nopen Parametric.Denotation.Evaluation.Structure using (\u27e6_\u27e7Term)\n\n-- Definition 3.4 (Changes)\n-- Definition 3.5 (Change environments)\nimport Parametric.Change.Validity\nopen Parametric.Change.Validity.Structure using (change-algebra)\nopen Parametric.Change.Validity.Structure using (environment-changes)\n\n-- Definition 3.6 (Change semantics)\n-- Lemma 3.7 (Change semantics is the derivative of semantics)\nimport Parametric.Change.Specification\nopen Parametric.Change.Specification.Structure using (\u27e6_\u27e7\u0394)\nopen Parametric.Change.Specification.Structure using (correctness)\n\n-- Definition 3.8 (Erasure)\n-- Lemma 3.9 (The erased version of a change is almost the same)\nimport Parametric.Change.Implementation\nopen Parametric.Change.Implementation.Structure using (_\u2248_)\nopen Parametric.Change.Implementation.Structure using (carry-over)\n\n-- Lemma 3.10 (\u27e6 t \u27e7\u0394 erases to Derive(t))\n-- Theorem 3.11 (Correctness of differentiation)\nimport Parametric.Change.Correctness\nopen Parametric.Change.Correctness.Structure using (derive-correct-closed)\nopen Parametric.Change.Correctness.Structure using (main-theorem)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a5f10ca08fbc34a5cea8a019fefc59bf379d19a6","subject":"flipbased.agda","message":"flipbased.agda\n","repos":"crypto-agda\/crypto-agda","old_file":"flipbased.agda","new_file":"flipbased.agda","new_contents":"open import Algebra\nimport Level as L\nopen L using () renaming (_\u2294_ to _L\u2294_)\nopen import Function hiding (_\u27e8_\u27e9_)\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Bool\nopen import Data.Nat.Properties\nopen import Data.Product using (proj\u2081; proj\u2082; _,_; swap; _\u00d7_)\nopen import Data.Bits\nopen import Data.Bool\nopen import Data.Vec using (Vec; []; _\u2237_; take; drop; head; tail) renaming (map to vmap)\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_)\n\nmodule flipbased\n (\u21ba : \u2200 {a} \u2192 \u2115 \u2192 Set a \u2192 Set a)\n (toss : \u21ba 1 Bit)\n (return\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A)\n (map\u21ba : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B)\n (join\u21ba : \u2200 {n\u2081 n\u2082 a} {A : Set a} \u2192 \u21ba n\u2081 (\u21ba n\u2082 A) \u2192 \u21ba (n\u2081 + n\u2082) A)\n where\n\nCoins = \u2115\n\n-- If you are not allowed to toss any coin, then you are deterministic.\nDet : \u2200 {a} \u2192 Set a \u2192 Set a\nDet = \u21ba 0\n\n-- An experiment\nEXP : \u2115 \u2192 Set\nEXP n = \u21ba n Bit\n\n-- A guessing game\n\u2141? : \u2200 c \u2192 Set\n\u2141? c = Bit \u2192 EXP c\n\nreturn\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\nreturn\u1d30 = return\u21ba\n\npure\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\npure\u1d30 = return\u1d30\n\ncoerce : \u2200 {m n a} {A : Set a} \u2192 m \u2261 n \u2192 \u21ba m A \u2192 \u21ba n A\ncoerce \u2261.refl = id\n\n_>>=_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 A \u2192 (A \u2192 \u21ba n\u2082 B) \u2192 \u21ba (n\u2081 + n\u2082) B\n_>>=_ x f = join\u21ba (map\u21ba f x)\n\n_=<<_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n (A \u2192 \u21ba n\u2081 B) \u2192 \u21ba n\u2082 A \u2192 \u21ba (n\u2081 + n\u2082) B\n_=<<_ {n\u2081} {n\u2082} rewrite \u2115\u00b0.+-comm n\u2081 n\u2082 = flip _>>=_\n\n_>>_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 A \u2192 \u21ba n\u2082 B \u2192 \u21ba (n\u2081 + n\u2082) B\n_>>_ {n\u2081} x y = x >>= const y\n\n_>=>_ : \u2200 {n\u2081 n\u2082 a b c} {A : Set a} {B : Set b} {C : Set c}\n \u2192 (A \u2192 \u21ba n\u2081 B) \u2192 (B \u2192 \u21ba n\u2082 C) \u2192 A \u2192 \u21ba (n\u2081 + n\u2082) C\n(f >=> g) x = f x >>= g\n\nweaken : \u2200 m {n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba (m + n) A\nweaken m x = return\u21ba {m} 0 >> x\n\nweaken\u2032 : \u2200 {m n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba (n + m) A\nweaken\u2032 x = x >>= return\u21ba\n\nweaken\u2264 : \u2200 {m n a} {A : Set a} \u2192 m \u2264 n \u2192 \u21ba m A \u2192 \u21ba n A\nweaken\u2264 pf x with \u2264\u21d2\u2203 pf\n... | k , \u2261.refl = weaken\u2032 x\n\npure\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A\npure\u21ba = return\u21ba\n\n-- Weakened version of toss\ntoss\u1d42 : \u2200 {n} \u2192 \u21ba (1 + n) Bit\ntoss\u1d42 = toss >>= return\u21ba\n\n_\u25b9\u21ba_ : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 \u21ba n A \u2192 (A \u2192 B) \u2192 \u21ba n B\nx \u25b9\u21ba f = map\u21ba f x\n\n\u27ea_\u27eb : \u2200 {n} {a} {A : Set a} \u2192 A \u2192 \u21ba n A\n\u27ea_\u27eb = pure\u21ba\n\n\u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n\u27ea_\u27eb\u1d30 = pure\u1d30\n\n\u27ea_\u00b7_\u27eb : \u2200 {a b} {A : Set a} {B : Set b} {n} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B\n\u27ea f \u00b7 x \u27eb = map\u21ba f x\n\ninfixl 4 _\u229b_\n_\u229b_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 (A \u2192 B) \u2192 \u21ba n\u2082 A \u2192 \u21ba (n\u2081 + n\u2082) B\n_\u229b_ {n\u2081} mf mx = mf >>= \u03bb f \u2192 \u27ea f \u00b7 mx \u27eb \n\n\u27ea_\u00b7_\u00b7_\u27eb : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba (m + n) C\n\u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n\u27ea_\u00b7_\u00b7_\u00b7_\u27eb : \u2200 {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} {m n o} \u2192\n (A \u2192 B \u2192 C \u2192 D) \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba o C \u2192 \u21ba (m + n + o) D\n\u27ea f \u00b7 x \u00b7 y \u00b7 z \u27eb = map\u21ba f x \u229b y \u229b z\n\nchoose : \u2200 {n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba n A \u2192 \u21ba (suc n) A\nchoose x y = toss >>= \u03bb b \u2192 if b then x else y\n\nzip\u21ba : \u2200 {c\u2080 c\u2081 a b} {A : Set a} {B : Set b} \u2192 \u21ba c\u2080 A \u2192 \u21ba c\u2081 B \u2192 \u21ba (c\u2080 + c\u2081) (A \u00d7 B)\nzip\u21ba x y = \u27ea _,_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8,\u27e9_ : \u2200 {a b} {A : Set a} {B : Set b} {m n} \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba (m + n) (A \u00d7 B)\n_\u27e8,\u27e9_ = zip\u21ba\n\n_\u27e8xor\u27e9_ : \u2200 {n\u2081 n\u2082} \u2192 \u21ba n\u2081 Bit \u2192 \u21ba n\u2082 Bit \u2192 \u21ba (n\u2081 + n\u2082) Bit\nx \u27e8xor\u27e9 y = \u27ea _xor_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8\u2295\u27e9_ : \u2200 {n\u2081 n\u2082 m} \u2192 \u21ba n\u2081 (Bits m) \u2192 \u21ba n\u2082 (Bits m) \u2192 \u21ba (n\u2081 + n\u2082) (Bits m)\nx \u27e8\u2295\u27e9 y = \u27ea _\u2295_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8\u2237\u27e9_ : \u2200 {n\u2081 n\u2082 m a} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 (Vec A m) \u2192 \u21ba (n\u2081 + n\u2082) (Vec A (suc m))\nx \u27e8\u2237\u27e9 xs = \u27ea _\u2237_ \u00b7 x \u00b7 xs \u27eb\n\n_\u27e8==\u27e9_ : \u2200 {n\u2081 n\u2082 m} \u2192 \u21ba n\u2081 (Bits m) \u2192 \u21ba n\u2082 (Bits m) \u2192 \u21ba (n\u2081 + n\u2082) Bit\nx \u27e8==\u27e9 y = \u27ea _==_ \u00b7 x \u00b7 y \u27eb\n\nT\u21ba : \u2200 {k} \u2192 \u21ba k Bit \u2192 \u21ba k Set\nT\u21ba p = \u27ea T \u00b7 p \u27eb\n\nreplicate\u21ba : \u2200 {n m} {a} {A : Set a} \u2192 \u21ba m A \u2192 \u21ba (n * m) (Vec A n)\nreplicate\u21ba {zero} _ = \u27ea [] \u27eb\nreplicate\u21ba {suc _} x = x \u27e8\u2237\u27e9 replicate\u21ba x\n\nnot\u21ba : \u2200 {n} \u2192 EXP n \u2192 EXP n\nnot\u21ba = map\u21ba not\n\nrandom : \u2200 {n} \u2192 \u21ba n (Bits n)\n-- random = coerce ? (replicate\u21ba toss) -- specialized version for now to avoid coerce\nrandom {zero} = \u27ea [] \u27eb\nrandom {suc _} = \u27ea _\u2237_ \u00b7 toss \u00b7 random \u27eb\n\nrandomTbl : \u2200 m n \u2192 \u21ba (2 ^ m * n) (Vec (Bits n) (2 ^ m))\nrandomTbl m n = replicate\u21ba random\n\nrandomFun : \u2200 m n \u2192 \u21ba (2 ^ m * n) (Bits m \u2192 Bits n)\nrandomFun m n = \u27ea funFromTbl \u00b7 randomTbl m n \u27eb\n\nrandomFunExt : \u2200 {n k a} {A : Set a} \u2192 \u21ba k (Bits n \u2192 A) \u2192 \u21ba (k + k) (Bits (suc n) \u2192 A)\nrandomFunExt f = \u27ea comb \u00b7 f \u00b7 f \u27eb where comb = \u03bb g\u2081 g\u2082 xs \u2192 (if head xs then g\u2081 else g\u2082) (tail xs)\n\ncostRndFun : \u2115 \u2192 \u2115 \u2192 \u2115\ncostRndFun zero n = n\ncostRndFun (suc m) n = 2* (costRndFun m n)\n\ncostRndFun-lem : \u2200 m n \u2192 costRndFun m n \u2261 2 ^ m * n\ncostRndFun-lem zero n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\ncostRndFun-lem (suc m) n rewrite costRndFun-lem m n | \u2115\u00b0.*-assoc 2 (2 ^ m) n | \u2115\u00b0.+-comm (2 ^ m * n) 0 = \u2261.refl\n\nrandomFun\u2032 : \u2200 {m n} \u2192 \u21ba (costRndFun m n) (Bits m \u2192 Bits n)\nrandomFun\u2032 {zero} = \u27ea const \u00b7 random \u27eb\nrandomFun\u2032 {suc m} = randomFunExt (randomFun\u2032 {m})\n\nrecord ProgEquiv a \u2113 : Set (L.suc \u2113 L\u2294 L.suc a) where\n infix 2 _\u2248_ _\u224b_\n field\n _\u2248_ : \u2200 {n} {A : Set a} \u2192 Rel (\u21ba n A) \u2113\n\n refl : \u2200 {n A} \u2192 Reflexive {A = \u21ba n A} _\u2248_\n sym : \u2200 {n A} \u2192 Symmetric {A = \u21ba n A} _\u2248_\n\n -- not strictly transitive\n\n reflexive : \u2200 {n A} \u2192 _\u2261_ \u21d2 _\u2248_ {n} {A}\n reflexive \u2261.refl = refl\n\n _\u224b_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 A \u2192 Set \u2113\n _\u224b_ {n\u2081} {n\u2082} p\u2081 p\u2082 = _\u2248_ {n = n\u2081 \u2294 n\u2082} (weaken\u2264 (m\u2264m\u2294n _ _) p\u2081) (weaken\u2264 (m\u2264n\u2294m _ n\u2081) p\u2082)\n where m\u2264n\u2294m : \u2200 m n \u2192 m \u2264 n \u2294 m\n m\u2264n\u2294m m n rewrite \u2294\u00b0.+-comm n m = m\u2264m\u2294n m n\n\n -- Another name for _\u224b_\n _looks_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 A \u2192 Set \u2113\n _looks_ = _\u224b_\n\nmodule WithEquiv (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n\n SecPRG : \u2200 {k n} (prg : (key : Bits k) \u2192 Bits n) \u2192 Set\n SecPRG prg = this looks random where this = \u27ea prg \u00b7 random \u27eb\n\n record PRG k n : Set where\n constructor _,_\n field\n prg : Bits k \u2192 Bits n\n sec : SecPRG prg\n\n OneTimeSecPRF : \u2200 {k m n} (prf : (key : Bits k) (msg : Bits m) \u2192 Bits n) \u2192 Set\n OneTimeSecPRF prf = \u2200 {xs} \u2192 let this = \u27ea prf \u00b7 random \u00b7 \u27ea xs \u27eb\u1d30 \u27eb in\n this looks random\n\n record PRF k m n : Set where\n constructor _,_\n field\n prf : Bits k \u2192 Bits m \u2192 Bits n\n sec : OneTimeSecPRF prf\n\nOTP : \u2200 {n} \u2192 Bits n \u2192 Bits n \u2192 Bits n\nOTP key msg = key \u2295 msg\n\ninit : \u2200 {k a} {A : Set a} \u2192 (Bits k \u2192 A) \u2192 \u21ba k A\ninit f = \u27ea f \u00b7 random \u27eb\n\nmodule Examples (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n open WithEquiv progEq\n\n left-unit-law = \u2200 {A B : Set} {n} {x : A} {f : A \u2192 \u21ba n B} \u2192 return\u1d30 x >>= f \u2248 f x\n\n right-unit-law = \u2200 {A : Set} {n} {x : \u21ba n A} \u2192 return\u1d30 =<< x \u2248 x\n\n assoc-law = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : \u21ba n\u2081 A} {f : A \u2192 \u21ba n\u2082 B} {g : B \u2192 \u21ba n\u2083 C}\n \u2192 (x >>= f) >>= g \u224b x >>= (\u03bb x \u2192 f x >>= g)\n\n assoc-law\u2032 = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : \u21ba n\u2081 A} {f : A \u2192 \u21ba n\u2082 B} {g : B \u2192 \u21ba n\u2083 C}\n \u2192 (x >>= f) >>= g \u2248 coerce (\u2261.sym (\u2115\u00b0.+-assoc n\u2081 n\u2082 n\u2083)) (x >>= (\u03bb x \u2192 f x >>= g))\n\n ex\u2081 = \u2200 {x} \u2192 toss \u27e8xor\u27e9 \u27ea x \u27eb\u1d30 \u2248 \u27ea x \u27eb\n\n ex\u2082 = p \u2248 map\u21ba swap p where p = toss \u27e8,\u27e9 toss\n\n ex\u2083 = \u2200 {n} \u2192 OneTimeSecPRF {n} OTP\n\n ex\u2084 = \u2200 {k n} (prg : PRG k n) \u2192 OneTimeSecPRF (\u03bb key xs \u2192 xs \u2295 PRG.prg prg key)\n\n ex\u2085 = \u2200 {k n} \u2192 PRG k n \u2192 PRF k n n\n","old_contents":"open import Algebra\nimport Level as L\nopen L using () renaming (_\u2294_ to _L\u2294_)\nopen import Function hiding (_\u27e8_\u27e9_)\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Bool\nopen import Data.Nat.Properties\nopen import Data.Product using (proj\u2081; proj\u2082; _,_; swap; _\u00d7_)\nopen import Data.Bits\nopen import Data.Bool\nopen import Data.Vec using (Vec; []; _\u2237_; take; drop; head; tail) renaming (map to vmap)\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_)\n\nmodule flipbased\n (\u21ba : \u2200 {a} \u2192 \u2115 \u2192 Set a \u2192 Set a)\n (toss : \u21ba 1 Bit)\n (return\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A)\n (map\u21ba : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B)\n (join\u21ba : \u2200 {n\u2081 n\u2082 a} {A : Set a} \u2192 \u21ba n\u2081 (\u21ba n\u2082 A) \u2192 \u21ba (n\u2081 + n\u2082) A)\n where\n\nCoins = \u2115\n\n-- If you are not allowed to toss any coin, then you are deterministic.\nDet : \u2200 {a} \u2192 Set a \u2192 Set a\nDet = \u21ba 0\n\n-- An experiment\nEXP : \u2115 \u2192 Set\nEXP n = \u21ba n Bit\n\n-- A guessing game\n\u2141? : \u2200 c \u2192 Set\n\u2141? c = Bit \u2192 EXP c\n\nreturn\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\nreturn\u1d30 = return\u21ba\n\npure\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\npure\u1d30 = return\u1d30\n\ncoerce : \u2200 {m n a} {A : Set a} \u2192 m \u2261 n \u2192 \u21ba m A \u2192 \u21ba n A\ncoerce \u2261.refl = id\n\n_>>=_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 A \u2192 (A \u2192 \u21ba n\u2082 B) \u2192 \u21ba (n\u2081 + n\u2082) B\n_>>=_ x f = join\u21ba (map\u21ba f x)\n\n_=<<_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n (A \u2192 \u21ba n\u2081 B) \u2192 \u21ba n\u2082 A \u2192 \u21ba (n\u2081 + n\u2082) B\n_=<<_ {n\u2081} {n\u2082} rewrite \u2115\u00b0.+-comm n\u2081 n\u2082 = flip _>>=_\n\n_>>_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 A \u2192 \u21ba n\u2082 B \u2192 \u21ba (n\u2081 + n\u2082) B\n_>>_ {n\u2081} x y = x >>= const y\n\n_>=>_ : \u2200 {n\u2081 n\u2082 a b c} {A : Set a} {B : Set b} {C : Set c}\n \u2192 (A \u2192 \u21ba n\u2081 B) \u2192 (B \u2192 \u21ba n\u2082 C) \u2192 A \u2192 \u21ba (n\u2081 + n\u2082) C\n(f >=> g) x = f x >>= g\n\nweaken : \u2200 m {n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba (m + n) A\nweaken m x = return\u21ba {m} 0 >> x\n\nweaken\u2032 : \u2200 {m n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba (n + m) A\nweaken\u2032 x = x >>= return\u21ba\n\nweaken\u2264 : \u2200 {m n a} {A : Set a} \u2192 m \u2264 n \u2192 \u21ba m A \u2192 \u21ba n A\nweaken\u2264 pf x with \u2264\u21d2\u2203 pf\n... | k , \u2261.refl = weaken\u2032 x\n\npure\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A\npure\u21ba = return\u21ba\n\n-- Weakened version of toss\ntoss\u1d42 : \u2200 {n} \u2192 \u21ba (1 + n) Bit\ntoss\u1d42 = toss >>= return\u21ba\n\n_\u25b9\u21ba_ : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 \u21ba n A \u2192 (A \u2192 B) \u2192 \u21ba n B\nx \u25b9\u21ba f = map\u21ba f x\n\n\u27ea_\u27eb : \u2200 {n} {a} {A : Set a} \u2192 A \u2192 \u21ba n A\n\u27ea_\u27eb = pure\u21ba\n\n\u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n\u27ea_\u27eb\u1d30 = pure\u1d30\n\n\u27ea_\u00b7_\u27eb : \u2200 {a b} {A : Set a} {B : Set b} {n} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B\n\u27ea f \u00b7 x \u27eb = map\u21ba f x\n\ninfixl 4 _\u229b_\n_\u229b_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 (A \u2192 B) \u2192 \u21ba n\u2082 A \u2192 \u21ba (n\u2081 + n\u2082) B\n_\u229b_ {n\u2081} mf mx = mf >>= \u03bb f \u2192 \u27ea f \u00b7 mx \u27eb \n\n\u27ea_\u00b7_\u00b7_\u27eb : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba (m + n) C\n\u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n\u27ea_\u00b7_\u00b7_\u00b7_\u27eb : \u2200 {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} {m n o} \u2192\n (A \u2192 B \u2192 C \u2192 D) \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba o C \u2192 \u21ba (m + n + o) D\n\u27ea f \u00b7 x \u00b7 y \u00b7 z \u27eb = map\u21ba f x \u229b y \u229b z\n\nchoose : \u2200 {n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba n A \u2192 \u21ba (suc n) A\nchoose x y = toss >>= \u03bb b \u2192 if b then x else y\n\nzip\u21ba : \u2200 {c\u2080 c\u2081 a b} {A : Set a} {B : Set b} \u2192 \u21ba c\u2080 A \u2192 \u21ba c\u2081 B \u2192 \u21ba (c\u2080 + c\u2081) (A \u00d7 B)\nzip\u21ba x y = \u27ea _,_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8,\u27e9_ : \u2200 {a b} {A : Set a} {B : Set b} {m n} \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba (m + n) (A \u00d7 B)\n_\u27e8,\u27e9_ = zip\u21ba\n\n_\u27e8xor\u27e9_ : \u2200 {n\u2081 n\u2082} \u2192 \u21ba n\u2081 Bit \u2192 \u21ba n\u2082 Bit \u2192 \u21ba (n\u2081 + n\u2082) Bit\nx \u27e8xor\u27e9 y = \u27ea _xor_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8\u2295\u27e9_ : \u2200 {n\u2081 n\u2082 m} \u2192 \u21ba n\u2081 (Bits m) \u2192 \u21ba n\u2082 (Bits m) \u2192 \u21ba (n\u2081 + n\u2082) (Bits m)\nx \u27e8\u2295\u27e9 y = \u27ea _\u2295_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8==\u27e9_ : \u2200 {n\u2081 n\u2082 m} \u2192 \u21ba n\u2081 (Bits m) \u2192 \u21ba n\u2082 (Bits m) \u2192 \u21ba (n\u2081 + n\u2082) Bit\nx \u27e8==\u27e9 y = \u27ea _==_ \u00b7 x \u00b7 y \u27eb\n\nT\u21ba : \u2200 {k} \u2192 \u21ba k Bit \u2192 \u21ba k Set\nT\u21ba p = \u27ea T \u00b7 p \u27eb\n\nreplicate\u21ba : \u2200 {n m} {a} {A : Set a} \u2192 \u21ba m A \u2192 \u21ba (n * m) (Vec A n)\nreplicate\u21ba {zero} _ = \u27ea [] \u27eb\nreplicate\u21ba {suc _} x = \u27ea _\u2237_ \u00b7 x \u00b7 replicate\u21ba x \u27eb\n\nrandom : \u2200 {n} \u2192 \u21ba n (Bits n)\n-- random = coerce ? (replicate\u21ba toss) -- specialized version for now to avoid coerce\nrandom {zero} = \u27ea [] \u27eb\nrandom {suc _} = \u27ea _\u2237_ \u00b7 toss \u00b7 random \u27eb\n\nrandomTbl : \u2200 m n \u2192 \u21ba (2 ^ m * n) (Vec (Bits n) (2 ^ m))\nrandomTbl m n = replicate\u21ba random\n\nrandomFun : \u2200 m n \u2192 \u21ba (2 ^ m * n) (Bits m \u2192 Bits n)\nrandomFun m n = \u27ea funFromTbl \u00b7 randomTbl m n \u27eb\n\nrandomFunExt : \u2200 {n k a} {A : Set a} \u2192 \u21ba k (Bits n \u2192 A) \u2192 \u21ba (k + k) (Bits (suc n) \u2192 A)\nrandomFunExt f = \u27ea comb \u00b7 f \u00b7 f \u27eb where comb = \u03bb g\u2081 g\u2082 xs \u2192 (if head xs then g\u2081 else g\u2082) (tail xs)\n\ncostRndFun : \u2115 \u2192 \u2115 \u2192 \u2115\ncostRndFun zero n = n\ncostRndFun (suc m) n = 2* (costRndFun m n)\n\ncostRndFun-lem : \u2200 m n \u2192 costRndFun m n \u2261 2 ^ m * n\ncostRndFun-lem zero n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\ncostRndFun-lem (suc m) n rewrite costRndFun-lem m n | \u2115\u00b0.*-assoc 2 (2 ^ m) n | \u2115\u00b0.+-comm (2 ^ m * n) 0 = \u2261.refl\n\nrandomFun\u2032 : \u2200 {m n} \u2192 \u21ba (costRndFun m n) (Bits m \u2192 Bits n)\nrandomFun\u2032 {zero} = \u27ea const \u00b7 random \u27eb\nrandomFun\u2032 {suc m} = randomFunExt (randomFun\u2032 {m})\n\nrecord ProgEquiv a \u2113 : Set (L.suc \u2113 L\u2294 L.suc a) where\n infix 2 _\u2248_ _\u224b_\n field\n _\u2248_ : \u2200 {n} {A : Set a} \u2192 Rel (\u21ba n A) \u2113\n\n refl : \u2200 {n A} \u2192 Reflexive {A = \u21ba n A} _\u2248_\n sym : \u2200 {n A} \u2192 Symmetric {A = \u21ba n A} _\u2248_\n\n -- not strictly transitive\n\n reflexive : \u2200 {n A} \u2192 _\u2261_ \u21d2 _\u2248_ {n} {A}\n reflexive \u2261.refl = refl\n\n _\u224b_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 A \u2192 Set \u2113\n _\u224b_ {n\u2081} {n\u2082} p\u2081 p\u2082 = _\u2248_ {n = n\u2081 \u2294 n\u2082} (weaken\u2264 (m\u2264m\u2294n _ _) p\u2081) (weaken\u2264 (m\u2264n\u2294m _ n\u2081) p\u2082)\n where m\u2264n\u2294m : \u2200 m n \u2192 m \u2264 n \u2294 m\n m\u2264n\u2294m m n rewrite \u2294\u00b0.+-comm n m = m\u2264m\u2294n m n\n\n -- Another name for _\u224b_\n _looks_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 A \u2192 Set \u2113\n _looks_ = _\u224b_\n\nmodule WithEquiv (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n\n SecPRG : \u2200 {k n} (prg : (key : Bits k) \u2192 Bits n) \u2192 Set\n SecPRG prg = this looks random where this = \u27ea prg \u00b7 random \u27eb\n\n record PRG k n : Set where\n constructor _,_\n field\n prg : Bits k \u2192 Bits n\n sec : SecPRG prg\n\n OneTimeSecPRF : \u2200 {k m n} (prf : (key : Bits k) (msg : Bits m) \u2192 Bits n) \u2192 Set\n OneTimeSecPRF prf = \u2200 {xs} \u2192 let this = \u27ea prf \u00b7 random \u00b7 \u27ea xs \u27eb\u1d30 \u27eb in\n this looks random\n\n record PRF k m n : Set where\n constructor _,_\n field\n prf : Bits k \u2192 Bits m \u2192 Bits n\n sec : OneTimeSecPRF prf\n\nOTP : \u2200 {n} \u2192 Bits n \u2192 Bits n \u2192 Bits n\nOTP key msg = key \u2295 msg\n\ninit : \u2200 {k a} {A : Set a} \u2192 (Bits k \u2192 A) \u2192 \u21ba k A\ninit f = \u27ea f \u00b7 random \u27eb\n\nmodule Examples (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n open WithEquiv progEq\n\n left-unit-law = \u2200 {A B : Set} {n} {x : A} {f : A \u2192 \u21ba n B} \u2192 return\u1d30 x >>= f \u2248 f x\n\n right-unit-law = \u2200 {A : Set} {n} {x : \u21ba n A} \u2192 return\u1d30 =<< x \u2248 x\n\n assoc-law = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : \u21ba n\u2081 A} {f : A \u2192 \u21ba n\u2082 B} {g : B \u2192 \u21ba n\u2083 C}\n \u2192 (x >>= f) >>= g \u224b x >>= (\u03bb x \u2192 f x >>= g)\n\n assoc-law\u2032 = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : \u21ba n\u2081 A} {f : A \u2192 \u21ba n\u2082 B} {g : B \u2192 \u21ba n\u2083 C}\n \u2192 (x >>= f) >>= g \u2248 coerce (\u2261.sym (\u2115\u00b0.+-assoc n\u2081 n\u2082 n\u2083)) (x >>= (\u03bb x \u2192 f x >>= g))\n\n ex\u2081 = \u2200 {x} \u2192 toss \u27e8xor\u27e9 \u27ea x \u27eb\u1d30 \u2248 \u27ea x \u27eb\n\n ex\u2082 = p \u2248 map\u21ba swap p where p = toss \u27e8,\u27e9 toss\n\n ex\u2083 = \u2200 {n} \u2192 OneTimeSecPRF {n} OTP\n\n ex\u2084 = \u2200 {k n} (prg : PRG k n) \u2192 OneTimeSecPRF (\u03bb key xs \u2192 xs \u2295 PRG.prg prg key)\n\n ex\u2085 = \u2200 {k n} \u2192 PRG k n \u2192 PRF k n n\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"5e8b1aa79d7d28b80b01e29591028ad21f2751b4","subject":"Prove new lemma from thesis","message":"Prove new lemma from thesis\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Equivalence.agda","new_file":"Base\/Change\/Equivalence.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\nopen import Function\n\nopen import Base.Change.Equivalence.Base public\nopen import Base.Change.Equivalence.EqReasoning public\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- By update-nil, if dx = nil x, then x \u229e dx \u2261 x.\n -- As a consequence, if dx \u2259 nil x, then x \u229e dx \u2261 x\n nil-is-\u229e-unit : \u2200 (dx : \u0394 {{ca}} x) \u2192 dx \u2259 nil x \u2192 x \u229e dx \u2261 x\n nil-is-\u229e-unit dx dx\u2259nil-x =\n begin\n x \u229e dx\n \u2261\u27e8 proof dx\u2259nil-x \u27e9\n x \u229e (nil {{ca}} x)\n \u2261\u27e8 update-nil {{ca}} x \u27e9\n x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Here we prove the inverse:\n \u229e-unit-is-nil : \u2200 (dx : \u0394 {{ca}} x) \u2192 x \u229e dx \u2261 x \u2192 _\u2259_ {{ca}} dx (nil {{ca}} x)\n \u229e-unit-is-nil dx x\u229edx\u2261x = doe $\n begin\n x \u229e dx\n \u2261\u27e8 x\u229edx\u2261x \u27e9\n x\n \u2261\u27e8 sym (update-nil {{ca}} x) \u27e9\n x \u229e nil {{ca}} x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Let's show that nil x is d.o.e. to x \u229f x\n nil-x-is-x\u229fx : nil x \u2259 x \u229f x\n nil-x-is-x\u229fx = \u2259-sym (\u229e-unit-is-nil (x \u229f x) (update-diff {{ca}} x x))\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by\n -- equiv-fun-changes-respect, and its corollaries fun-change-respects and\n -- equiv-fun-changes-funs.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n -- This results pairs with update-diff.\n diff-update : \u2200 {dx : \u0394 {{ca}} x} \u2192 (x \u229e dx) \u229f x \u2259 dx\n diff-update {dx} = doe lemma\n where\n lemma : _\u229e_ {{ca}} x (x \u229e dx \u229f x) \u2261 x \u229e dx\n lemma = update-diff {{ca}} (x \u229e dx) x\n\n -- \\begin{lemma}[Equivalence cancellation]\n -- |v2 `ominus` v1 `doe` dv| holds if and only if |v2 = v1 `oplus`\n -- dv|, for any |v1, v2 `elem` V| and |dv `elem` Dt ^ v1|.\n -- \\end{lemma}\n\n equiv-cancel-1 : \u2200 x' dx \u2192 _\u2259_ {{ca}} (x' \u229f x) dx \u2192 x' \u2261 x \u229e dx\n equiv-cancel-1 x' dx (doe x'\u229fx\u2259dx) = trans (sym (update-diff x' x)) x'\u229fx\u2259dx\n equiv-cancel-2 : \u2200 x' dx \u2192 x' \u2261 x \u229e dx \u2192 _\u2259_ {{ca}} (x' \u229f x) dx\n equiv-cancel-2 _ dx refl = diff-update\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization; DerivativeAsChange)\n open FC.FunctionChange\n\n equiv-fun-changes-respect : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n _\u2259_ {{changeAlgebra}} df\u2081 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n equiv-fun-changes-respect {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = doe lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 \u2259-cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 \u2259-cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} (df : \u0394 f) \u2192\n dx\u2081 \u2259 dx\u2082 \u2192 apply df x dx\u2081 \u2259 apply df x dx\u2082\n fun-change-respects df dx\u2081\u2259dx\u2082 = equiv-fun-changes-respect (\u2259-refl {dx = df}) dx\u2081\u2259dx\u2082\n\n -- D.o.e. function changes behave like the same function (up to d.o.e.).\n equiv-fun-changes-funs : \u2200 {x : A} {dx : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082 : \u0394 f} \u2192\n _\u2259_ {{changeAlgebra}} df\u2081 df\u2082 \u2192 apply df\u2081 x dx \u2259 apply df\u2082 x dx\n equiv-fun-changes-funs {dx = dx} df\u2081\u2259df\u2082 = equiv-fun-changes-respect df\u2081\u2259df\u2082 (\u2259-refl {dx = dx})\n\n derivative-doe-characterization : \u2200 {a : A} {da : \u0394 a}\n {f : A \u2192 B} {df : RawChange f} (is-derivative : IsDerivative f df) \u2192\n _\u2259_ {{CB}} (df a da) (f (a \u229e da) \u229f f a)\n derivative-doe-characterization {a} {da} {f} {df} is-derivative = doe lemma\n where\n open \u2261-Reasoning\n lemma : f a \u229e df a da \u2261 f a \u229e (f (a \u229e da) \u229f f a)\n lemma =\n begin\n (f a \u229e df a da)\n \u2261\u27e8 is-derivative a da \u27e9\n (f (a \u229e da))\n \u2261\u27e8 sym (update-diff (f (a \u229e da)) (f a)) \u27e9\n (f a \u229e (f (a \u229e da) \u229f f a))\n \u220e\n\n derivative-respects-doe : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x}\n {f : A \u2192 B} {df : RawChange f} (is-derivative : IsDerivative f df) \u2192\n _\u2259_ {{CA}} dx\u2081 dx\u2082 \u2192 _\u2259_ {{CB}} (df x dx\u2081) (df x dx\u2082)\n derivative-respects-doe {x} {dx\u2081} {dx\u2082} {f} {df} is-derivative dx\u2081\u2259dx\u2082 =\n begin\n df x dx\u2081\n \u2259\u27e8 derivative-doe-characterization is-derivative \u27e9\n f (x \u229e dx\u2081) \u229f f x\n \u2261\u27e8 cong (\u03bb v \u2192 f v \u229f f x) (proof dx\u2081\u2259dx\u2082) \u27e9\n f (x \u229e dx\u2082) \u229f f x\n \u2259\u27e8 \u2259-sym (derivative-doe-characterization is-derivative) \u27e9\n df x dx\u2082\n \u220e\n where\n open \u2259-Reasoning\n\n -- This is also a corollary of fun-changes-respect\n derivative-respects-doe-alt : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x}\n {f : A \u2192 B} {df : RawChange f} (is-derivative : IsDerivative f df) \u2192\n _\u2259_ {{CA}} dx\u2081 dx\u2082 \u2192 _\u2259_ {{CB}} (df x dx\u2081) (df x dx\u2082)\n derivative-respects-doe-alt {x} {dx\u2081} {dx\u2082} {f} {df} is-derivative dx\u2081\u2259dx\u2082 =\n fun-change-respects (DerivativeAsChange is-derivative) dx\u2081\u2259dx\u2082\n\n open import Postulate.Extensionality\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} df-x-dx\u2259dg-x-dx = doe lemma\u2082\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 x dx = proof $ df-x-dx\u2259dg-x-dx x dx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n\n -- You could think that the function should relate equivalent changes, but\n -- that's a stronger hypothesis, which doesn't give you extra guarantees. But\n -- here's the statement and proof, for completeness:\n\n delta-ext\u2082 : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx\u2081 dx\u2082 \u2192 _\u2259_ {{CA}} dx\u2081 dx\u2082 \u2192 apply df x dx\u2081 \u2259 apply dg x dx\u2082) \u2192 df \u2259 dg\n delta-ext\u2082 {f} {df} {dg} df-x-dx\u2259dg-x-dx = delta-ext (\u03bb x dx \u2192 df-x-dx\u2259dg-x-dx x dx dx \u2259-refl)\n\n -- We know that IsDerivative f (apply (nil f)) (by nil-is-derivative).\n -- That is, df = nil f -> IsDerivative f (apply df).\n -- Now, we try to prove that if IsDerivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n IsDerivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil {{CA}} x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil {{CA}} x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n IsDerivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n derivative-is-nil-alternative : \u2200 {f : A \u2192 B} df \u2192\n (IsDerivative-f-df : IsDerivative f df) \u2192 DerivativeAsChange IsDerivative-f-df \u2259 nil f\n derivative-is-nil-alternative df IsDerivative-f-df = derivative-is-nil (DerivativeAsChange IsDerivative-f-df) IsDerivative-f-df\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 IsDerivative f (apply df) \u2192 IsDerivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 \u2259-sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n\n -- This is Lemma 2.5 in the paper. Note that the statement in the paper uses\n -- (incorrectly) normal equality instead of delta-observational equivalence.\n deriv-zero :\n \u2200 (f : A \u2192 B) df \u2192 IsDerivative f df \u2192\n \u2200 v \u2192 df v (nil {{CA}} v) \u2259 nil {{CB}} (f v)\n deriv-zero f df proof v = doe lemma\n where\n open \u2261-Reasoning\n lemma : f v \u229e df v (nil v) \u2261 f v \u229e nil {{CB}} (f v)\n lemma =\n begin\n f v \u229e df v (nil {{CA}} v)\n \u2261\u27e8 proof v (nil {{CA}} v) \u27e9\n f (v \u229e (nil {{CA}} v))\n \u2261\u27e8 cong f (update-nil {{CA}} v) \u27e9\n f v\n \u2261\u27e8 sym (update-nil {{CB}} (f v)) \u27e9\n f v \u229e nil {{CB}} (f v)\n \u220e\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\nopen import Function\n\nopen import Base.Change.Equivalence.Base public\nopen import Base.Change.Equivalence.EqReasoning public\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- By update-nil, if dx = nil x, then x \u229e dx \u2261 x.\n -- As a consequence, if dx \u2259 nil x, then x \u229e dx \u2261 x\n nil-is-\u229e-unit : \u2200 (dx : \u0394 {{ca}} x) \u2192 dx \u2259 nil x \u2192 x \u229e dx \u2261 x\n nil-is-\u229e-unit dx dx\u2259nil-x =\n begin\n x \u229e dx\n \u2261\u27e8 proof dx\u2259nil-x \u27e9\n x \u229e (nil {{ca}} x)\n \u2261\u27e8 update-nil {{ca}} x \u27e9\n x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Here we prove the inverse:\n \u229e-unit-is-nil : \u2200 (dx : \u0394 {{ca}} x) \u2192 x \u229e dx \u2261 x \u2192 _\u2259_ {{ca}} dx (nil {{ca}} x)\n \u229e-unit-is-nil dx x\u229edx\u2261x = doe $\n begin\n x \u229e dx\n \u2261\u27e8 x\u229edx\u2261x \u27e9\n x\n \u2261\u27e8 sym (update-nil {{ca}} x) \u27e9\n x \u229e nil {{ca}} x\n \u220e\n where\n open \u2261-Reasoning\n\n -- Let's show that nil x is d.o.e. to x \u229f x\n nil-x-is-x\u229fx : nil x \u2259 x \u229f x\n nil-x-is-x\u229fx = \u2259-sym (\u229e-unit-is-nil (x \u229f x) (update-diff {{ca}} x x))\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by\n -- equiv-fun-changes-respect, and its corollaries fun-change-respects and\n -- equiv-fun-changes-funs.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n -- This results pairs with update-diff.\n diff-update : \u2200 {dx : \u0394 {{ca}} x} \u2192 (x \u229e dx) \u229f x \u2259 dx\n diff-update {dx} = doe lemma\n where\n lemma : _\u229e_ {{ca}} x (x \u229e dx \u229f x) \u2261 x \u229e dx\n lemma = update-diff {{ca}} (x \u229e dx) x\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization; DerivativeAsChange)\n open FC.FunctionChange\n\n equiv-fun-changes-respect : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n _\u2259_ {{changeAlgebra}} df\u2081 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n equiv-fun-changes-respect {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = doe lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 \u2259-cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 \u2259-cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} (df : \u0394 f) \u2192\n dx\u2081 \u2259 dx\u2082 \u2192 apply df x dx\u2081 \u2259 apply df x dx\u2082\n fun-change-respects df dx\u2081\u2259dx\u2082 = equiv-fun-changes-respect (\u2259-refl {dx = df}) dx\u2081\u2259dx\u2082\n\n -- D.o.e. function changes behave like the same function (up to d.o.e.).\n equiv-fun-changes-funs : \u2200 {x : A} {dx : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082 : \u0394 f} \u2192\n _\u2259_ {{changeAlgebra}} df\u2081 df\u2082 \u2192 apply df\u2081 x dx \u2259 apply df\u2082 x dx\n equiv-fun-changes-funs {dx = dx} df\u2081\u2259df\u2082 = equiv-fun-changes-respect df\u2081\u2259df\u2082 (\u2259-refl {dx = dx})\n\n derivative-doe-characterization : \u2200 {a : A} {da : \u0394 a}\n {f : A \u2192 B} {df : RawChange f} (is-derivative : IsDerivative f df) \u2192\n _\u2259_ {{CB}} (df a da) (f (a \u229e da) \u229f f a)\n derivative-doe-characterization {a} {da} {f} {df} is-derivative = doe lemma\n where\n open \u2261-Reasoning\n lemma : f a \u229e df a da \u2261 f a \u229e (f (a \u229e da) \u229f f a)\n lemma =\n begin\n (f a \u229e df a da)\n \u2261\u27e8 is-derivative a da \u27e9\n (f (a \u229e da))\n \u2261\u27e8 sym (update-diff (f (a \u229e da)) (f a)) \u27e9\n (f a \u229e (f (a \u229e da) \u229f f a))\n \u220e\n\n derivative-respects-doe : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x}\n {f : A \u2192 B} {df : RawChange f} (is-derivative : IsDerivative f df) \u2192\n _\u2259_ {{CA}} dx\u2081 dx\u2082 \u2192 _\u2259_ {{CB}} (df x dx\u2081) (df x dx\u2082)\n derivative-respects-doe {x} {dx\u2081} {dx\u2082} {f} {df} is-derivative dx\u2081\u2259dx\u2082 =\n begin\n df x dx\u2081\n \u2259\u27e8 derivative-doe-characterization is-derivative \u27e9\n f (x \u229e dx\u2081) \u229f f x\n \u2261\u27e8 cong (\u03bb v \u2192 f v \u229f f x) (proof dx\u2081\u2259dx\u2082) \u27e9\n f (x \u229e dx\u2082) \u229f f x\n \u2259\u27e8 \u2259-sym (derivative-doe-characterization is-derivative) \u27e9\n df x dx\u2082\n \u220e\n where\n open \u2259-Reasoning\n\n -- This is also a corollary of fun-changes-respect\n derivative-respects-doe-alt : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x}\n {f : A \u2192 B} {df : RawChange f} (is-derivative : IsDerivative f df) \u2192\n _\u2259_ {{CA}} dx\u2081 dx\u2082 \u2192 _\u2259_ {{CB}} (df x dx\u2081) (df x dx\u2082)\n derivative-respects-doe-alt {x} {dx\u2081} {dx\u2082} {f} {df} is-derivative dx\u2081\u2259dx\u2082 =\n fun-change-respects (DerivativeAsChange is-derivative) dx\u2081\u2259dx\u2082\n\n open import Postulate.Extensionality\n\n -- An extensionality principle for delta-observational equivalence: if\n -- applying two function changes to the same base value and input change gives\n -- a d.o.e. result, then the two function changes are d.o.e. themselves.\n\n delta-ext : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx \u2192 apply df x dx \u2259 apply dg x dx) \u2192 df \u2259 dg\n delta-ext {f} {df} {dg} df-x-dx\u2259dg-x-dx = doe lemma\u2082\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma\u2081 : \u2200 x dx \u2192 f x \u229e apply df x dx \u2261 f x \u229e apply dg x dx\n lemma\u2081 x dx = proof $ df-x-dx\u2259dg-x-dx x dx\n lemma\u2082 : f \u229e df \u2261 f \u229e dg\n lemma\u2082 = ext (\u03bb x \u2192 lemma\u2081 x (nil x))\n\n -- You could think that the function should relate equivalent changes, but\n -- that's a stronger hypothesis, which doesn't give you extra guarantees. But\n -- here's the statement and proof, for completeness:\n\n delta-ext\u2082 : \u2200 {f : A \u2192 B} \u2192 \u2200 {df dg : \u0394 f} \u2192 (\u2200 x dx\u2081 dx\u2082 \u2192 _\u2259_ {{CA}} dx\u2081 dx\u2082 \u2192 apply df x dx\u2081 \u2259 apply dg x dx\u2082) \u2192 df \u2259 dg\n delta-ext\u2082 {f} {df} {dg} df-x-dx\u2259dg-x-dx = delta-ext (\u03bb x dx \u2192 df-x-dx\u2259dg-x-dx x dx dx \u2259-refl)\n\n -- We know that IsDerivative f (apply (nil f)) (by nil-is-derivative).\n -- That is, df = nil f -> IsDerivative f (apply df).\n -- Now, we try to prove that if IsDerivative f (apply df) -> df = nil f.\n -- But first, we prove that f \u229e df = f.\n derivative-is-\u229e-unit : \u2200 {f : A \u2192 B} df \u2192\n IsDerivative f (apply df) \u2192 f \u229e df \u2261 f\n derivative-is-\u229e-unit {f} df fdf =\n begin\n f \u229e df\n \u2261\u27e8\u27e9\n (\u03bb x \u2192 f x \u229e apply df x (nil x))\n \u2261\u27e8 ext (\u03bb x \u2192 fdf x (nil x)) \u27e9\n (\u03bb x \u2192 f (x \u229e nil {{CA}} x))\n \u2261\u27e8 ext (\u03bb x \u2192 cong f (update-nil {{CA}} x)) \u27e9\n (\u03bb x \u2192 f x)\n \u2261\u27e8\u27e9\n f\n \u220e\n where\n open \u2261-Reasoning\n\n -- We can restate the above as \"df is a nil change\".\n\n derivative-is-nil : \u2200 {f : A \u2192 B} df \u2192\n IsDerivative f (apply df) \u2192 df \u2259 nil f\n derivative-is-nil df fdf = \u229e-unit-is-nil df (derivative-is-\u229e-unit df fdf)\n\n derivative-is-nil-alternative : \u2200 {f : A \u2192 B} df \u2192\n (IsDerivative-f-df : IsDerivative f df) \u2192 DerivativeAsChange IsDerivative-f-df \u2259 nil f\n derivative-is-nil-alternative df IsDerivative-f-df = derivative-is-nil (DerivativeAsChange IsDerivative-f-df) IsDerivative-f-df\n\n -- If we have two derivatives, they're both nil, hence they're equal.\n derivative-unique : \u2200 {f : A \u2192 B} {df dg : \u0394 f} \u2192 IsDerivative f (apply df) \u2192 IsDerivative f (apply dg) \u2192 df \u2259 dg\n derivative-unique {f} {df} {dg} fdf fdg =\n begin\n df\n \u2259\u27e8 derivative-is-nil df fdf \u27e9\n nil f\n \u2259\u27e8 \u2259-sym (derivative-is-nil dg fdg) \u27e9\n dg\n \u220e\n where\n open \u2259-Reasoning\n\n -- This is Lemma 2.5 in the paper. Note that the statement in the paper uses\n -- (incorrectly) normal equality instead of delta-observational equivalence.\n deriv-zero :\n \u2200 (f : A \u2192 B) df \u2192 IsDerivative f df \u2192\n \u2200 v \u2192 df v (nil {{CA}} v) \u2259 nil {{CB}} (f v)\n deriv-zero f df proof v = doe lemma\n where\n open \u2261-Reasoning\n lemma : f v \u229e df v (nil v) \u2261 f v \u229e nil {{CB}} (f v)\n lemma =\n begin\n f v \u229e df v (nil {{CA}} v)\n \u2261\u27e8 proof v (nil {{CA}} v) \u27e9\n f (v \u229e (nil {{CA}} v))\n \u2261\u27e8 cong f (update-nil {{CA}} v) \u27e9\n f v\n \u2261\u27e8 sym (update-nil {{CB}} (f v)) \u27e9\n f v \u229e nil {{CB}} (f v)\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f0468dc61e6551228ac0e70f23b50c06ad9c2a14","subject":"Cosmetic changes.","message":"Cosmetic changes.\n\nIgnore-this: 1264a77944a1d117ad1b3633d2b311d2\n\ndarcs-hash:20110212194846-3bd4e-f2ad17ea1c00d9c7b31f0ff4056470b7cbe98384.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/LTC\/Data\/Nat.agda","new_file":"src\/LTC\/Data\/Nat.agda","new_contents":"------------------------------------------------------------------------------\n-- LTC natural numbers\n------------------------------------------------------------------------------\n\nmodule LTC.Data.Nat where\n\nopen import LTC.Base\n\n-- We add 3 to the fixities of the standard library.\ninfixl 10 _*_\ninfixl 9 _+_ _\u2238_\n\n------------------------------------------------------------------------------\n-- The LTC natural numbers type.\nopen import LTC.Data.Nat.Type public\n\n------------------------------------------------------------------------------\n-- Arithmetic operations\n\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ d + e \u2261 succ (d + e)\n{-# ATP axiom +-0x #-}\n{-# ATP axiom +-Sx #-}\n\npostulate\n _\u2238_ : D \u2192 D \u2192 D\n \u2238-x0 : \u2200 d \u2192 d \u2238 zero \u2261 d\n \u2238-0S : \u2200 d \u2192 zero \u2238 succ d \u2261 zero\n \u2238-SS : \u2200 d e \u2192 succ d \u2238 succ e \u2261 d \u2238 e\n{-# ATP axiom \u2238-x0 #-}\n{-# ATP axiom \u2238-0S #-}\n{-# ATP axiom \u2238-SS #-}\n\npostulate\n _*_ : D \u2192 D \u2192 D\n *-0x : \u2200 d \u2192 zero * d \u2261 zero\n *-Sx : \u2200 d e \u2192 succ d * e \u2261 e + d * e\n{-# ATP axiom *-0x #-}\n{-# ATP axiom *-Sx #-}\n","old_contents":"------------------------------------------------------------------------------\n-- LTC natural numbers\n------------------------------------------------------------------------------\n\nmodule LTC.Data.Nat where\n\nopen import LTC.Base\n\n-- We add 3 to the fixities of the standard library.\ninfixl 10 _*_\ninfixl 9 _+_ _\u2238_\n\n------------------------------------------------------------------------------\n-- The LTC natural numbers type.\nopen import LTC.Data.Nat.Type public\n\n------------------------------------------------------------------------------\n-- Arithmetic operations\n\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ d + e \u2261 succ (d + e)\n{-# ATP axiom +-0x #-}\n{-# ATP axiom +-Sx #-}\n\npostulate\n _\u2238_ : D \u2192 D \u2192 D\n \u2238-x0 : \u2200 d \u2192 d \u2238 zero \u2261 d\n \u2238-0S : \u2200 d \u2192 zero \u2238 succ d \u2261 zero\n \u2238-SS : \u2200 d e \u2192 succ d \u2238 succ e \u2261 d \u2238 e\n{-# ATP axiom \u2238-x0 #-}\n{-# ATP axiom \u2238-0S #-}\n{-# ATP axiom \u2238-SS #-}\n\npostulate\n _*_ : D \u2192 D \u2192 D\n *-0x : \u2200 d \u2192 zero * d \u2261 zero\n *-Sx : \u2200 d e \u2192 succ d * e \u2261 e + d * e\n{-# ATP axiom *-0x #-}\n{-# ATP axiom *-Sx #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"2525e7d2c808f4a14be0656c9bf760dc0db42f6d","subject":"The agda2atp are using the Koen's approach for the translation of predicate symbols.","message":"The agda2atp are using the Koen's approach for the translation of predicate symbols.\n\nIgnore-this: bf6dda1de53baa440b98cd82a80cd1af\n\ndarcs-hash:20110318170057-3bd4e-bebcaa3fbf61993e0433fb96ed3f5d961f78a1a1.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/FOTC\/Data\/Bool\/TestATP.agda","new_file":"Draft\/FOTC\/Data\/Bool\/TestATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Testing\n------------------------------------------------------------------------------\n\nmodule Draft.FOTC.Data.Bool.TestATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Bool.Type\n\n------------------------------------------------------------------------------\n\npostulate\n thm : \u2200 {b}(P : D \u2192 Set) \u2192 (Bool b \u2227 P true \u2227 P false) \u2192 P b\n-- The ATPs couldn't prove this postulate.\n-- {-# ATP prove thm #-}\n\npostulate\n thm\u2081 : \u2200 {P : D \u2192 Set}{x y z} \u2192 Bool x \u2192 P y \u2192 P z \u2192 P (if x then y else z)\n-- The ATPs couldn't prove this postulate.\n-- {-# ATP prove thm\u2081 #-}\n\nBool-elim : \u2200 (P : D \u2192 Set){x y z} \u2192 Bool x \u2192 P y \u2192 P z \u2192\n P (if x then y else z)\nBool-elim P {y = y} tB Py Pz = subst P (sym (if-true y)) Py\nBool-elim P {z = z} fB Py Pz = subst P (sym (if-false z)) Pz\n\n_&&_ : D \u2192 D \u2192 D\nx && y = if x then y else false\n{-# ATP definition _&&_ #-}\n\npostulate\n &&-Bool : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n{-# ATP prove &&-Bool Bool-elim #-}\n\n&&-Bool\u2081 : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n&&-Bool\u2081 {y = y} tB By = prf\n where\n postulate prf : Bool (true && y)\n {-# ATP prove prf Bool-elim #-}\n&&-Bool\u2081 {y = y} fB By = prf\n where\n postulate prf : Bool (false && y)\n {-# ATP prove prf Bool-elim #-}\n\n&&-Bool\u2082 : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n&&-Bool\u2082 tB By = Bool-elim Bool tB By fB\n&&-Bool\u2082 fB By = Bool-elim Bool fB By fB\n","old_contents":"------------------------------------------------------------------------------\n-- Testing\n------------------------------------------------------------------------------\n\nmodule Draft.FOTC.Data.Bool.TestATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Bool.Type\n\n------------------------------------------------------------------------------\n\npostulate\n thm : \u2200 {b}(P : D \u2192 Set) \u2192 (Bool b \u2227 P true \u2227 P false) \u2192 P b\n-- The ATPs couldn't prove this postulate.\n-- {-# ATP prove thm #-}\n\npostulate\n thm\u2081 : \u2200 {P : D \u2192 Set}{x y z} \u2192 Bool x \u2192 P y \u2192 P z \u2192 P (if x then y else z)\n-- The ATPs couldn't prove this postulate.\n-- {-# ATP prove thm\u2081 #-}\n\nBool-elim : \u2200 (P : D \u2192 Set){x y z} \u2192 Bool x \u2192 P y \u2192 P z \u2192\n P (if x then y else z)\nBool-elim P {y = y} tB Py Pz = subst P (sym (if-true y)) Py\nBool-elim P {z = z} fB Py Pz = subst P (sym (if-false z)) Pz\n\n_&&_ : D \u2192 D \u2192 D\nx && y = if x then y else false\n{-# ATP definition _&&_ #-}\n\npostulate\n &&-Bool : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n-- The ATPs couldn't prove this postulate.\n-- {-# ATP prove &&-Bool Bool-elim #-}\n\n&&-Bool\u2081 : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n&&-Bool\u2081 {y = y} tB By = prf\n where\n postulate prf : Bool (true && y)\n {-# ATP prove prf Bool-elim #-}\n&&-Bool\u2081 {y = y} fB By = prf\n where\n postulate prf : Bool (false && y)\n {-# ATP prove prf Bool-elim #-}\n\n&&-Bool\u2082 : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n&&-Bool\u2082 tB By = Bool-elim Bool tB By fB\n&&-Bool\u2082 fB By = Bool-elim Bool fB By fB\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"0be3dd74597a8a36c9a931238927ac17095c3522","subject":"Added draft about the FOTC booleans.","message":"Added draft about the FOTC booleans.\n\nIgnore-this: 917f7ddaa04101914e11938deb91f45b\n\ndarcs-hash:20110316145903-3bd4e-9d4543d873379bde71fa7e85c33b9b1fe709c320.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/FOTC\/Data\/Bool\/TestATP.agda","new_file":"Draft\/FOTC\/Data\/Bool\/TestATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Testing\n------------------------------------------------------------------------------\n\nmodule Draft.FOTC.Data.Bool.TestATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Bool.Type\n\n------------------------------------------------------------------------------\n\npostulate\n thm : \u2200 {b}(P : D \u2192 Set) \u2192 (Bool b \u2227 P true \u2227 P false) \u2192 P b\n-- The ATPs couldn't prove this postulate.\n-- {-# ATP prove thm #-}\n\npostulate\n thm\u2081 : \u2200 {P : D \u2192 Set}{x y z} \u2192 Bool x \u2192 P y \u2192 P z \u2192 P (if x then y else z)\n-- The ATPs couldn't prove this postulate.\n-- {-# ATP prove thm\u2081 #-}\n\nBool-elim : \u2200 (P : D \u2192 Set){x y z} \u2192 Bool x \u2192 P y \u2192 P z \u2192\n P (if x then y else z)\nBool-elim P {y = y} tB Py Pz = subst P (sym (if-true y)) Py\nBool-elim P {z = z} fB Py Pz = subst P (sym (if-false z)) Pz\n\n_&&_ : D \u2192 D \u2192 D\nx && y = if x then y else false\n{-# ATP definition _&&_ #-}\n\npostulate\n &&-Bool : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n-- The ATPs couldn't prove this postulate.\n-- {-# ATP prove &&-Bool Bool-elim #-}\n\n&&-Bool\u2081 : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n&&-Bool\u2081 {y = y} tB By = prf\n where\n postulate prf : Bool (true && y)\n {-# ATP prove prf Bool-elim #-}\n&&-Bool\u2081 {y = y} fB By = prf\n where\n postulate prf : Bool (false && y)\n {-# ATP prove prf Bool-elim #-}\n\n&&-Bool\u2082 : \u2200 {x y} \u2192 Bool x \u2192 Bool y \u2192 Bool (x && y)\n&&-Bool\u2082 tB By = Bool-elim Bool tB By fB\n&&-Bool\u2082 fB By = Bool-elim Bool fB By fB\n","old_contents":"------------------------------------------------------------------------------\n-- Testing\n------------------------------------------------------------------------------\n\nmodule Draft.FOTC.Data.Bool.TestATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Bool.Type\n\n------------------------------------------------------------------------------\n\npostulate\n thm : \u2200 {b}(P : D \u2192 Set) \u2192 (Bool b \u2227 P true \u2227 P false) \u2192 P b\n-- The ATPs couldn't prove this postulate.\n-- {-# ATP prove thm #-}\n\npostulate\n thm\u2081 : \u2200 {P : D \u2192 Set}{x y z} \u2192 Bool x \u2192 P y \u2192 P z \u2192 P (if x then y else z)\n-- The ATPs couldn't prove this postulate.\n-- {-# ATP prove thm\u2081 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"acd256774e7cff7d41fddcb59d4ee1a166592477","subject":"Removed MCR-prop","message":"Removed MCR-prop\n\nIgnore-this: ba33703ab5552046fafa46f8d50d370\n\ndarcs-hash:20110213041239-3bd4e-02db08fc7032c1d17a5c545da14dc167ebab8d9a.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/McCarthy91\/RelationATP.agda","new_file":"Draft\/McCarthy91\/RelationATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Properties for some relations\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.RelationATP where\n\nopen import LTC.Base\n\nopen import Draft.McCarthy91.McCarthy91\nopen import Draft.McCarthy91.ArithmeticATP\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\n\n------------------------------------------------------------------------------\n","old_contents":"------------------------------------------------------------------------------\n-- Properties for some relations\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.RelationATP where\n\nopen import LTC.Base\n\nopen import Draft.McCarthy91.McCarthy91\nopen import Draft.McCarthy91.ArithmeticATP\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\n\n------------------------------------------------------------------------------\n\nMCR-prop : \u2200 {n} \u2192 N n \u2192 LE n one-hundred \u2192 MCR (n + eleven) n\nMCR-prop zN 0\u2264100 = prf\n where\n postulate\n prf : (hundred-one \u2238 (zero + eleven)) < (hundred-one \u2238 zero) \u2261 true\n {-# ATP prove prf #-}\nMCR-prop (sN {n} Nn) Sn\u2264100 = prf (MCR-prop Nn n\u2264100)\n where\n n\u2264100 : LE n one-hundred\n n\u2264100 = \u2264-trans Nn (sN Nn) N100 (x \u03c4') \u2192\n d1 indet \u2192\n ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8 \u03c41 ==> \u03c42 \u21d2 \u03c43 ==> \u03c44 \u27e9))\n lem (TACast wt TCRefl) (ICastArr x\u2083 ind) \u03c41 \u03c42 .\u03c41 .\u03c42 d refl = x\u2083 refl\n lem (TACast wt (TCArr x\u2082 x\u2083)) (ICastArr x\u2084 ind) \u03c41 \u03c42 \u03c43 \u03c4' d refl = {!!}\n progress (TAAp wt1 wt2) | I x | V x\u2081 = I (IAp {!!} x (FBoxed x\u2081)) -- cyrus\n -- if the left is a boxed value, inspect the right\n progress (TAAp wt1 wt2) | V v | S (_ , Step x y z) = S (_ , Step (FHAp2 (FBoxed v) x) y (FHAp2 (FBoxed v) z))\n progress (TAAp wt1 wt2) | V v | E e = E (CECong (FHAp2 (FBoxed v) FHOuter) e)\n progress (TAAp wt1 wt2) | V v | I i = I (IAp {!!} {!!} (FIndet i)) -- cyrus (issue from above, and also i think missing a rule for indet applications)\n progress (TAAp wt1 wt2) | V v | V v\u2082 = {!!} -- {!canonical-boxed-forms-arr wt1 x !}\n\n -- empty holes\n progress (TAEHole x x\u2081) = I IEHole\n\n -- nonempty holes\n progress (TANEHole xin wt x\u2081)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHNEHole x) y (FHNEHole z))\n ... | E x = E (CECong (FHNEHole FHOuter) x)\n ... | I x = I (INEHole (FIndet x))\n ... | V x = I (INEHole (FBoxed x))\n\n -- casts\n progress (TACast wt con)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHCast x) y (FHCast z))\n ... | E x = E (CECong (FHCast FHOuter) x)\n ... | I x = I {!!}\n ... | V x = V {!!}\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\n\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d boxedval + d indet + d casterr[ \u0394 ] + \u03a3[ d' \u2208 dhexp ] (d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 ok d \u0394\n E : \u2200{d \u0394} \u2192 d casterr \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n V : \u2200{d \u0394} \u2192 d boxedval \u2192 ok d \u0394\n\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = V (BVVal VConst)\n\n -- variables\n progress (TAVar x\u2081) = abort (somenotnone (! x\u2081))\n\n -- lambdas\n progress (TALam wt) = V (BVVal VLam)\n\n -- applications\n progress (TAAp wt1 wt2)\n with progress wt1 | progress wt2\n progress (TAAp wt1 wt2) | S x | ih2 = {!!}\n progress (TAAp wt1 wt2) | E x | _ = E (CECong (FHAp1 FHOuter) x)\n progress (TAAp wt1 wt2) | I x | ih2 = {!!}\n progress (TAAp wt1 wt2) | V x | ih2 = {!!}\n\n -- empty holes\n progress (TAEHole x x\u2081) = I IEHole\n\n -- nonempty holes\n progress (TANEHole xin wt x\u2081)\n with progress wt\n ... | S (d' , Step x y z) = S (_ , Step {!!} {!!} {!!})\n ... | E x = E (CECong (FHNEHole FHOuter) x)\n ... | I x = I (INEHole (FIndet x))\n ... | V x = I (INEHole (FBoxed x))\n\n -- casts\n progress (TACast wt y)\n with progress wt\n ... | S x = {!!}\n ... | E x = E (CECong (FHCast FHOuter) x)\n ... | I x = {!x!}\n ... | V x = {!!}\n\n\n -- -- applications\n -- progress (TAAp D1 x D2)\n -- with progress D1 | progress D2\n -- -- left applicand value\n -- progress (TAAp TAConst () D2) | V VConst | _\n -- progress (TAAp {d2 = d2} D1 MAArr D2) | V (VLam {x = x} {d = d}) | V x\u2081 = S ([ d2 \/ x ] d , Step (FHAp1 (FVal VLam) {!!}) (ITLam (FVal x\u2081)) {!!} )\n -- progress (TAAp {d2 = d2} D1 MAArr D2) | V (VLam {x = x} {d = d}) | I x\u2081 = S ([ d2 \/ x ] d , Step (FHAp1 (FVal VLam) {!!}) (ITLam (FIndet x\u2081)) {!!} )\n -- -- errors propagate\n -- progress (TAAp D1 x\u2082 D2) | V x | E x\u2081 = E (EAp2 (FVal x) x\u2081)\n -- progress (TAAp D1 x\u2082 D2) | I x | E x\u2081 = E (EAp2 (FIndet x) x\u2081)\n -- progress (TAAp D1 x\u2082 D2) | E x | E x\u2081 = E (EAp1 x) -- NB: could have picked the other one here, too; this is a source of non-determinism\n -- progress (TAAp D1 x\u2082 D2) | S x | E x\u2081 = S {!!}\n -- progress (TAAp D1 x\u2082 D2) | E x | _ = E (EAp1 x)\n -- -- indeterminates\n -- progress (TAAp D1 x\u2082 D2) | I i | V v = I (IAp i (FVal v))\n -- progress (TAAp D1 x\u2082 D2) | I x | I x\u2081 = I (IAp x (FIndet x\u2081))\n -- -- either applicand steps\n -- progress (TAAp D1 x\u2083 D2) | S (d1' , Step x x\u2081 x\u2082) | _ = S (_ , Step (FHAp2 x) x\u2081 (FHAp2 x\u2082))\n -- progress (TAAp D1 x\u2084 D2) | _ | S (d2' , Step x\u2081 x\u2082 x\u2083) = S (_ , Step (FHAp1 {!!} x\u2081) x\u2082 (FHAp1 {!!} x\u2083))\n\n\n -- -- non-empty holes\n -- progress (TANEHole x D x\u2081)\n -- with progress D\n -- progress (TANEHole x\u2081 D x\u2082) | V v = I (INEHole (FVal v))\n -- progress (TANEHole x\u2081 D x\u2082) | I x = I (INEHole (FIndet x))\n -- progress (TANEHole x\u2081 D x\u2082) | E x = E (ENEHole x)\n -- progress (TANEHole x\u2083 D x\u2084) | S (_ , Step x x\u2081 x\u2082) = S (_ , Step (FHNEHole x) x\u2081 (FHNEHole x\u2082))\n\n -- -- casts\n -- progress (TACast D x)\n -- with progress D\n -- progress (TACast TAConst con) | V VConst = S (c , Step (FHCastFinal (FVal VConst)) (ITCast (FVal VConst) TAConst con) (FHFinal (FVal VConst)))\n -- progress (TACast D m) | V VLam = S (_ , Step (FHCastFinal (FVal VLam)) (ITCast (FVal VLam) D m) (FHFinal (FVal VLam)))\n -- progress (TACast D x\u2081) | I x = I (ICast x)\n -- progress (TACast D x\u2081) | E x = E (ECastProp x)\n -- progress (TACast D x\u2083) | S (d , Step x x\u2081 x\u2082) = S (_ , Step (FHCast x) x\u2081 (FHCast x\u2082))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f8bd15f415762af0fce2117302889b7e97a25936","subject":"Rename diff to diff-base.","message":"Rename diff to diff-base.\n\nI want to move code between Parametric.Syntax.Term and\nAtlas.Syntax.Term, but I have to unify the naming conventions first.\n\nOld-commit-hash: 73a829a379ce12d2c01dbaf184ae2d380ad9abb1\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Term.agda","new_file":"Parametric\/Change\/Term.agda","new_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Constant\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\nDiffStructure : Set\nDiffStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))\n\nApplyStructure : Set\nApplyStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\n\nmodule Structure where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n lift-diff-apply :\n DiffStructure \u2192\n ApplyStructure \u2192\n \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4) \u00d7 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-diff-apply diff-base apply {base \u03b9} = diff-base , apply\n lift-diff-apply diff-base apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff-base apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff-base apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff-base apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff-base apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\n lift-diff :\n DiffStructure \u2192\n ApplyStructure \u2192\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n\n lift-diff diff-base apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff-base apply {\u03c4} {\u0393})\n\n lift-apply :\n DiffStructure \u2192\n ApplyStructure \u2192\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-apply diff-base apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff-base apply {\u03c4} {\u0393})\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Constant\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\nDiffStructure : Set\nDiffStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))\n\nApplyStructure : Set\nApplyStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\n\nmodule Structure where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n lift-diff-apply :\n DiffStructure \u2192\n ApplyStructure \u2192\n \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4) \u00d7 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-diff-apply diff apply {base \u03b9} = diff , apply\n lift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\n lift-diff :\n DiffStructure \u2192\n ApplyStructure \u2192\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n\n lift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n lift-apply :\n DiffStructure \u2192\n ApplyStructure \u2192\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"fcfc151932c7801c0c8b94a5b4653842199a6f6c","subject":"Finish Greek Unicode 0370-03FF (partial).","message":"Finish Greek Unicode 0370-03FF (partial).\n","repos":"scott-fleischman\/greek-grammar,scott-fleischman\/greek-grammar","old_file":"agda\/Text\/Greek\/Script.agda","new_file":"agda\/Text\/Greek\/Script.agda","new_contents":"module Text.Greek.Script where\n\nopen import Data.Maybe\nopen import Data.Vec\nopen import Relation.Nullary using (\u00ac_)\nopen import Relation.Binary.PropositionalEquality using (_\u2262_)\n\ndata Case : Set where\n lower upper : Case\n\ndata Letter : Set where\n \u03b1\u2032 \u03b2\u2032 \u03b3\u2032 \u03b4\u2032 \u03b5\u2032 \u03b6\u2032 \u03b7\u2032 \u03b8\u2032 \u03b9\u2032 \u03ba\u2032 \u03bb\u2032 \u03bc\u2032 \u03bd\u2032 \u03be\u2032 \u03bf\u2032 \u03c0\u2032 \u03c1\u2032 \u03c3\u2032 \u03c4\u2032 \u03c5\u2032 \u03c6\u2032 \u03c7\u2032 \u03c8\u2032 \u03c9\u2032 : Letter\n\nvowels : Vec _ _\nvowels = \u03b1\u2032 \u2237 \u03b5\u2032 \u2237 \u03b7\u2032 \u2237 \u03b9\u2032 \u2237 \u03bf\u2032 \u2237 \u03c5\u2032 \u2237 \u03c9\u2032 \u2237 []\n\nalways-short-letters : Vec _ _\nalways-short-letters = \u03b5\u2032 \u2237 \u03bf\u2032 \u2237 []\n\ndiaeresis-letters : Vec _ _\ndiaeresis-letters = \u03b9\u2032 \u2237 \u03c5\u2032 \u2237 []\n\niota-subscript-letters : Vec _ _\niota-subscript-letters = \u03b1\u2032 \u2237 \u03b7\u2032 \u2237 \u03c9\u2032 \u2237 []\n\ndata _vowel : Letter \u2192 Set where\n is-vowel : \u2200 {v} \u2192 v \u2208 vowels \u2192 v vowel\n\ndata _always-short : Letter \u2192 Set where\n is-always-short : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 always-short-letters \u2192 v always-short\n\ndata _diaeresis : Letter \u2192 Set where\n add-diaeresis : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 diaeresis-letters \u2192 v diaeresis\n\ndata _\u27e6_\u27e7-final : Letter \u2192 Case \u2192 Set where\n make-final : \u03c3\u2032 \u27e6 lower \u27e7-final\n\ndata _\u27e6_\u27e7-smooth : Letter \u2192 Case \u2192 Set where\n add-smooth-lower-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u27e6 lower \u27e7-smooth\n add-smooth-\u03c1 : \u03c1\u2032 \u27e6 lower \u27e7-smooth\n add-smooth-upper-vowel-not-\u03a5 : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2262 \u03c5\u2032 \u2192 v \u27e6 upper \u27e7-smooth\n\ndata _with-rough : Letter \u2192 Set where\n add-rough-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v with-rough\n add-rough-\u03c1 : \u03c1\u2032 with-rough\n\ndata _iota-subscript : Letter \u2192 Set where\n add-iota-subscript : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 iota-subscript-letters \u2192 v iota-subscript\n\ndata _\u27e6_\u27e7-breathing : Letter \u2192 Case \u2192 Set where\n smooth : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 \u27e6 c \u27e7-smooth \u2984 \u2192 \u2113 \u27e6 c \u27e7-breathing\n rough : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 with-rough \u2984 \u2192 \u2113 \u27e6 c \u27e7-breathing\n\ndata _long-vowel : Letter \u2192 Set where\n make-long-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 \u00ac v always-short \u2192 v long-vowel\n\ndata _accent : Letter \u2192 Set where\n acute : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n grave : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n circumflex : \u2200 {\u2113} \u2192 \u2983 p : \u2113 long-vowel \u2984 \u2192 \u2113 accent\n\ndata Token : Letter \u2192 Case \u2192 Set where\n unmarked : \u2200 {\u2113 c} \u2192 Token \u2113 c\n with-accent : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 Token \u2113 c\n with-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-diaeresis : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 diaeresis \u2984 \u2192 Token \u2113 c\n with-accent-diaeresis : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2983 p : \u2113 diaeresis \u2984 \u2192 Token \u2113 c\n with-accent-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-iota : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n final : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 \u27e6 c \u27e7-final \u2984 \u2192 Token \u2113 c\n\n-- Constructions\n\n-- \u0391 \u03b1\ninstance \u03b1-vowel : \u03b1\u2032 vowel\n\u03b1-vowel = is-vowel here\n\n\u00ac\u03b1-always-short : \u00ac \u03b1\u2032 always-short\n\u00ac\u03b1-always-short (is-always-short (there (there ())))\n\ninstance \u03b1-long-vowel : \u03b1\u2032 long-vowel\n\u03b1-long-vowel = make-long-vowel \u00ac\u03b1-always-short\n\ninstance \u03b1-smooth : \u03b1\u2032 \u27e6 lower \u27e7-smooth\n\u03b1-smooth = add-smooth-lower-vowel\n\ninstance \u0391-smooth : \u03b1\u2032 \u27e6 upper \u27e7-smooth\n\u0391-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b1-rough : \u03b1\u2032 with-rough\n\u03b1-rough = add-rough-vowel\n\ninstance \u03b1-iota-subscript : \u03b1\u2032 iota-subscript\n\u03b1-iota-subscript = add-iota-subscript here\n\n-- \u0395 \u03b5\ninstance \u03b5-vowel : \u03b5\u2032 vowel\n\u03b5-vowel = is-vowel (there here)\n\n\u03b5-always-short : \u03b5\u2032 always-short\n\u03b5-always-short = is-always-short here\n\ninstance \u03b5-smooth : \u03b5\u2032 \u27e6 lower \u27e7-smooth\n\u03b5-smooth = add-smooth-lower-vowel\n\ninstance \u0395-smooth : \u03b5\u2032 \u27e6 upper \u27e7-smooth\n\u0395-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b5-rough : \u03b5\u2032 with-rough\n\u03b5-rough = add-rough-vowel\n\n-- \u0397 \u03b7\ninstance \u03b7-vowel : \u03b7\u2032 vowel\n\u03b7-vowel = is-vowel (there (there here))\n\n\u00ac\u03b7-always-short : \u00ac \u03b7\u2032 always-short\n\u00ac\u03b7-always-short (is-always-short (there (there ())))\n\ninstance \u03b7-long-vowel : \u03b7\u2032 long-vowel\n\u03b7-long-vowel = make-long-vowel \u00ac\u03b7-always-short\n\ninstance \u03b7-smooth : \u03b7\u2032 \u27e6 lower \u27e7-smooth\n\u03b7-smooth = add-smooth-lower-vowel\n\ninstance \u0397-smooth : \u03b7\u2032 \u27e6 upper \u27e7-smooth\n\u0397-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b7-rough : \u03b7\u2032 with-rough\n\u03b7-rough = add-rough-vowel\n\ninstance \u03b7-iota-subscript : \u03b7\u2032 iota-subscript\n\u03b7-iota-subscript = add-iota-subscript (there here)\n\n-- \u0399 \u03b9\n\ninstance \u03b9-vowel : \u03b9\u2032 vowel\n\u03b9-vowel = is-vowel (there (there (there here)))\n\n\u00ac\u03b9-always-short : \u00ac \u03b9\u2032 always-short\n\u00ac\u03b9-always-short (is-always-short (there (there ())))\n\ninstance \u03b9-long-vowel : \u03b9\u2032 long-vowel\n\u03b9-long-vowel = make-long-vowel \u00ac\u03b9-always-short\n\ninstance \u03b9-smooth : \u03b9\u2032 \u27e6 lower \u27e7-smooth\n\u03b9-smooth = add-smooth-lower-vowel\n\ninstance \u0399-smooth : \u03b9\u2032 \u27e6 upper \u27e7-smooth\n\u0399-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b9-rough : \u03b9\u2032 with-rough\n\u03b9-rough = add-rough-vowel\n\ninstance \u03b9-diaeresis : \u03b9\u2032 diaeresis\n\u03b9-diaeresis = add-diaeresis here\n\n-- \u039f \u03bf\ninstance \u03bf-vowel : \u03bf\u2032 vowel\n\u03bf-vowel = is-vowel (there (there (there (there here))))\n\n\u03bf-always-short : \u03bf\u2032 always-short\n\u03bf-always-short = is-always-short (there here)\n\ninstance \u03bf-smooth : \u03bf\u2032 \u27e6 lower \u27e7-smooth\n\u03bf-smooth = add-smooth-lower-vowel\n\ninstance \u039f-smooth : \u03bf\u2032 \u27e6 upper \u27e7-smooth\n\u039f-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03bf-rough : \u03bf\u2032 with-rough\n\u03bf-rough = add-rough-vowel\n\n-- \u03a1 \u03c1\ninstance \u03c1-smooth : \u03c1\u2032 \u27e6 lower \u27e7-smooth\n\u03c1-smooth = add-smooth-\u03c1\n\ninstance \u03c1-rough : \u03c1\u2032 with-rough\n\u03c1-rough = add-rough-\u03c1\n\n-- \u03a3 \u03c3\ninstance \u03c3-final : \u03c3\u2032 \u27e6 lower \u27e7-final\n\u03c3-final = make-final\n\n-- \u03a5 \u03c5\ninstance \u03c5-vowel : \u03c5\u2032 vowel\n\u03c5-vowel = is-vowel (there (there (there (there (there here)))))\n\n\u00ac\u03c5-always-short : \u00ac \u03c5\u2032 always-short\n\u00ac\u03c5-always-short (is-always-short (there (there ())))\n\ninstance \u03c5-long-vowel : \u03c5\u2032 long-vowel\n\u03c5-long-vowel = make-long-vowel \u00ac\u03c5-always-short\n\ninstance \u03c5-smooth : \u03c5\u2032 \u27e6 lower \u27e7-smooth\n\u03c5-smooth = add-smooth-lower-vowel\n\ninstance \u03c5-rough : \u03c5\u2032 with-rough\n\u03c5-rough = add-rough-vowel\n\ninstance \u03c5-diaeresis : \u03c5\u2032 diaeresis\n\u03c5-diaeresis = add-diaeresis (there here)\n\n-- \u03a9 \u03c9\ninstance \u03c9-vowel : \u03c9\u2032 vowel\n\u03c9-vowel = is-vowel (there (there (there (there (there (there here))))))\n\n\u00ac\u03c9-always-short : \u00ac \u03c9\u2032 always-short\n\u00ac\u03c9-always-short (is-always-short (there (there ())))\n\ninstance \u03c9-long-vowel : \u03c9\u2032 long-vowel\n\u03c9-long-vowel = make-long-vowel \u00ac\u03c9-always-short\n\ninstance \u03c9-smooth : \u03c9\u2032 \u27e6 lower \u27e7-smooth\n\u03c9-smooth = add-smooth-lower-vowel\n\ninstance \u03a9-smooth : \u03c9\u2032 \u27e6 upper \u27e7-smooth\n\u03a9-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03c9-rough : \u03c9\u2032 with-rough\n\u03c9-rough = add-rough-vowel\n\ninstance \u03c9-iota-subscript : \u03c9\u2032 iota-subscript\n\u03c9-iota-subscript = add-iota-subscript (there (there here))\n\n\n-- Unicode\n-- U+0390 - U+03CE\n\u0390 : Token \u03b9\u2032 lower -- U+0390 GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS\n\u0390 = with-accent-diaeresis acute\n\u0391 : Token \u03b1\u2032 upper -- U+0391 GREEK CAPITAL LETTER ALPHA\n\u0391 = unmarked\n\u0392 : Token \u03b2\u2032 upper -- U+0392 GREEK CAPITAL LETTER BETA\n\u0392 = unmarked\n\u0393 : Token \u03b3\u2032 upper -- U+0393 GREEK CAPITAL LETTER GAMMA\n\u0393 = unmarked\n\u0394 : Token \u03b4\u2032 upper -- U+0394 GREEK CAPITAL LETTER DELTA\n\u0394 = unmarked\n\u0395 : Token \u03b5\u2032 upper -- U+0395 GREEK CAPITAL LETTER EPSILON\n\u0395 = unmarked\n\u0396 : Token \u03b6\u2032 upper -- U+0396 GREEK CAPITAL LETTER ZETA\n\u0396 = unmarked\n\u0397 : Token \u03b7\u2032 upper -- U+0397 GREEK CAPITAL LETTER ETA\n\u0397 = unmarked\n\u0398 : Token \u03b8\u2032 upper -- U+0398 GREEK CAPITAL LETTER THETA\n\u0398 = unmarked\n\u0399 : Token \u03b9\u2032 upper -- U+0399 GREEK CAPITAL LETTER IOTA\n\u0399 = unmarked\n\u039a : Token \u03ba\u2032 upper -- U+039A GREEK CAPITAL LETTER KAPPA\n\u039a = unmarked\n\u039b : Token \u03bb\u2032 upper -- U+039B GREEK CAPITAL LETTER LAMDA\n\u039b = unmarked\n\u039c : Token \u03bc\u2032 upper -- U+039C GREEK CAPITAL LETTER MU\n\u039c = unmarked\n\u039d : Token \u03bd\u2032 upper -- U+039D GREEK CAPITAL LETTER NU\n\u039d = unmarked\n\u039e : Token \u03be\u2032 upper -- U+039E GREEK CAPITAL LETTER XI\n\u039e = unmarked\n\u039f : Token \u03bf\u2032 upper -- U+039F GREEK CAPITAL LETTER OMICRON\n\u039f = unmarked\n\u03a0 : Token \u03c0\u2032 upper -- U+03A0 GREEK CAPITAL LETTER PI\n\u03a0 = unmarked\n\u03a1 : Token \u03c1\u2032 upper -- U+03A1 GREEK CAPITAL LETTER RHO\n\u03a1 = unmarked\n -- U+03A2 \n\u03a3 : Token \u03c3\u2032 upper -- U+03A3 GREEK CAPITAL LETTER SIGMA\n\u03a3 = unmarked\n\u03a4 : Token \u03c4\u2032 upper -- U+03A4 GREEK CAPITAL LETTER TAU\n\u03a4 = unmarked\n\u03a5 : Token \u03c5\u2032 upper -- U+03A5 GREEK CAPITAL LETTER UPSILON\n\u03a5 = unmarked\n\u03a6 : Token \u03c6\u2032 upper -- U+03A6 GREEK CAPITAL LETTER PHI\n\u03a6 = unmarked\n\u03a7 : Token \u03c7\u2032 upper -- U+03A7 GREEK CAPITAL LETTER CHI\n\u03a7 = unmarked\n\u03a8 : Token \u03c8\u2032 upper -- U+03A8 GREEK CAPITAL LETTER PSI\n\u03a8 = unmarked\n\u03a9 : Token \u03c9\u2032 upper -- U+03A9 GREEK CAPITAL LETTER OMEGA\n\u03a9 = unmarked\n\u03aa : Token \u03b9\u2032 upper -- U+03AA GREEK CAPITAL LETTER IOTA WITH DIALYTIKA\n\u03aa = with-diaeresis\n\u03ab : Token \u03c5\u2032 upper -- U+03AB GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA\n\u03ab = with-diaeresis\n\u03ac : Token \u03b1\u2032 lower -- U+03AC GREEK SMALL LETTER ALPHA WITH TONOS\n\u03ac = with-accent acute\n\u03ad : Token \u03b5\u2032 lower -- U+03AD GREEK SMALL LETTER EPSILON WITH TONOS\n\u03ad = with-accent acute\n\u03ae : Token \u03b7\u2032 lower -- U+03AE GREEK SMALL LETTER ETA WITH TONOS\n\u03ae = with-accent acute\n\u03af : Token \u03b9\u2032 lower -- U+03AF GREEK SMALL LETTER IOTA WITH TONOS\n\u03af = with-accent acute\n\u03b0 : Token \u03c5\u2032 lower -- U+03B0 GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS\n\u03b0 = with-accent-diaeresis acute\n\u03b1 : Token \u03b1\u2032 lower -- U+03B1 GREEK SMALL LETTER ALPHA\n\u03b1 = unmarked\n\u03b2 : Token \u03b2\u2032 lower -- U+03B2 GREEK SMALL LETTER BETA\n\u03b2 = unmarked\n\u03b3 : Token \u03b3\u2032 lower -- U+03B3 GREEK SMALL LETTER GAMMA\n\u03b3 = unmarked\n\u03b4 : Token \u03b4\u2032 lower -- U+03B4 GREEK SMALL LETTER DELTA\n\u03b4 = unmarked\n\u03b5 : Token \u03b5\u2032 lower -- U+03B5 GREEK SMALL LETTER EPSILON\n\u03b5 = unmarked\n\u03b6 : Token \u03b6\u2032 lower -- U+03B6 GREEK SMALL LETTER ZETA\n\u03b6 = unmarked\n\u03b7 : Token \u03b7\u2032 lower -- U+03B7 GREEK SMALL LETTER ETA\n\u03b7 = unmarked\n\u03b8 : Token \u03b8\u2032 lower -- U+03B8 GREEK SMALL LETTER THETA\n\u03b8 = unmarked\n\u03b9 : Token \u03b9\u2032 lower -- U+03B9 GREEK SMALL LETTER IOTA\n\u03b9 = unmarked\n\u03ba : Token \u03ba\u2032 lower -- U+03BA GREEK SMALL LETTER KAPPA\n\u03ba = unmarked\n\u2219\u03bb : Token \u03bb\u2032 lower -- U+03BB GREEK SMALL LETTER LAMDA\n\u2219\u03bb = unmarked\n\u03bc : Token \u03bc\u2032 lower -- U+03BC GREEK SMALL LETTER MU\n\u03bc = unmarked\n\u03bd : Token \u03bd\u2032 lower -- U+03BD GREEK SMALL LETTER NU\n\u03bd = unmarked\n\u03be : Token \u03be\u2032 lower -- U+03BE GREEK SMALL LETTER XI\n\u03be = unmarked\n\u03bf : Token \u03bf\u2032 lower -- U+03BF GREEK SMALL LETTER OMICRON\n\u03bf = unmarked\n\u03c0 : Token \u03c0\u2032 lower -- U+03C0 GREEK SMALL LETTER PI\n\u03c0 = unmarked\n\u03c1 : Token \u03c1\u2032 lower -- U+03C1 GREEK SMALL LETTER RHO\n\u03c1 = unmarked\n\u03c2 : Token \u03c3\u2032 lower -- U+03C2 GREEK SMALL LETTER FINAL SIGMA\n\u03c2 = final\n\u03c3 : Token \u03c3\u2032 lower -- U+03C3 GREEK SMALL LETTER SIGMA\n\u03c3 = unmarked\n\u03c4 : Token \u03c4\u2032 lower -- U+03C4 GREEK SMALL LETTER TAU\n\u03c4 = unmarked\n\u03c5 : Token \u03c5\u2032 lower -- U+03C5 GREEK SMALL LETTER UPSILON\n\u03c5 = unmarked\n\u03c6 : Token \u03c6\u2032 lower -- U+03C6 GREEK SMALL LETTER PHI\n\u03c6 = unmarked\n\u03c7 : Token \u03c7\u2032 lower -- U+03C7 GREEK SMALL LETTER CHI\n\u03c7 = unmarked\n\u03c8 : Token \u03c8\u2032 lower -- U+03C8 GREEK SMALL LETTER PSI\n\u03c8 = unmarked\n\u03c9 : Token \u03c9\u2032 lower -- U+03C9 GREEK SMALL LETTER OMEGA\n\u03c9 = unmarked\n\u03ca : Token \u03b9\u2032 lower -- U+03CA GREEK SMALL LETTER IOTA WITH DIALYTIKA\n\u03ca = with-diaeresis\n\u03cb : Token \u03c5\u2032 lower -- U+03CB GREEK SMALL LETTER UPSILON WITH DIALYTIKA\n\u03cb = with-diaeresis\n\u03cc : Token \u03bf\u2032 lower -- U+03CC GREEK SMALL LETTER OMICRON WITH TONOS\n\u03cc = with-accent acute\n\u03cd : Token \u03c5\u2032 lower -- U+03CD GREEK SMALL LETTER UPSILON WITH TONOS\n\u03cd = with-accent acute\n\u03ce : Token \u03c9\u2032 lower -- U+03CE GREEK SMALL LETTER OMEGA WITH TONOS\n\u03ce = with-accent acute\n\n\n\u1f00 : Token \u03b1\u2032 lower\n\u1f00 = with-breathing smooth\n\u1f01 : Token \u03b1\u2032 lower\n\u1f01 = with-breathing rough\n\u1f02 : Token \u03b1\u2032 lower\n\u1f02 = with-accent-breathing grave smooth\n\u1f03 : Token \u03b1\u2032 lower\n\u1f03 = with-accent-breathing grave rough\n\u1f04 : Token \u03b1\u2032 lower\n\u1f04 = with-accent-breathing acute smooth\n\u1f05 : Token \u03b1\u2032 lower\n\u1f05 = with-accent-breathing acute rough\n\u1f06 : Token \u03b1\u2032 lower\n\u1f06 = with-accent-breathing circumflex smooth\n\u1f07 : Token \u03b1\u2032 lower\n\u1f07 = with-accent-breathing circumflex rough\n\u1f08 : Token \u03b1\u2032 upper\n\u1f08 = with-breathing smooth\n\u1f09 : Token \u03b1\u2032 upper\n\u1f09 = with-breathing rough\n\u1f0a : Token \u03b1\u2032 upper\n\u1f0a = with-accent-breathing grave smooth\n\u1f0b : Token \u03b1\u2032 upper\n\u1f0b = with-accent-breathing grave rough\n\u1f0c : Token \u03b1\u2032 upper\n\u1f0c = with-accent-breathing acute smooth\n\u1f0d : Token \u03b1\u2032 upper\n\u1f0d = with-accent-breathing acute rough\n\u1f0e : Token \u03b1\u2032 upper\n\u1f0e = with-accent-breathing circumflex smooth\n\u1f0f : Token \u03b1\u2032 upper\n\u1f0f = with-accent-breathing circumflex rough\n\n-- U+1F7x\n\u1f70 : Token \u03b1\u2032 lower\n\u1f70 = with-accent grave\n\u1f71 : Token \u03b1\u2032 lower\n\u1f71 = with-accent acute\n\n-- U+1F8x\n\u1f80 : Token \u03b1\u2032 lower\n\u1f80 = with-breathing-iota smooth\n\u1f81 : Token \u03b1\u2032 lower\n\u1f81 = with-breathing-iota rough\n\u1f82 : Token \u03b1\u2032 lower\n\u1f82 = with-accent-breathing-iota grave smooth\n\u1f83 : Token \u03b1\u2032 lower\n\u1f83 = with-accent-breathing-iota grave rough\n\u1f84 : Token \u03b1\u2032 lower\n\u1f84 = with-accent-breathing-iota acute smooth\n\u1f85 : Token \u03b1\u2032 lower\n\u1f85 = with-accent-breathing-iota acute rough\n\u1f86 : Token \u03b1\u2032 lower\n\u1f86 = with-accent-breathing-iota circumflex smooth\n\u1f87 : Token \u03b1\u2032 lower\n\u1f87 = with-accent-breathing-iota circumflex rough\n\u1f88 : Token \u03b1\u2032 upper\n\u1f88 = with-breathing-iota smooth\n\u1f89 : Token \u03b1\u2032 upper\n\u1f89 = with-breathing-iota rough\n\u1f8a : Token \u03b1\u2032 upper\n\u1f8a = with-accent-breathing-iota grave smooth\n\u1f8b : Token \u03b1\u2032 upper\n\u1f8b = with-accent-breathing-iota grave rough\n\u1f8c : Token \u03b1\u2032 upper\n\u1f8c = with-accent-breathing-iota acute smooth\n\u1f8d : Token \u03b1\u2032 upper\n\u1f8d = with-accent-breathing-iota acute rough\n\u1f8e : Token \u03b1\u2032 upper\n\u1f8e = with-accent-breathing-iota circumflex smooth\n\u1f8f : Token \u03b1\u2032 upper\n\u1f8f = with-accent-breathing-iota circumflex rough\n\n-- U+1FBx\n-- \u1fb0\n-- \u1fb1\n\u1fb2 : Token \u03b1\u2032 lower\n\u1fb2 = with-accent-iota grave\n\u1fb3 : Token \u03b1\u2032 lower\n\u1fb3 = with-iota\n\u1fb4 : Token \u03b1\u2032 lower\n\u1fb4 = with-accent-iota acute\n\u1fb6 : Token \u03b1\u2032 lower\n\u1fb6 = with-accent circumflex\n\u1fb7 : Token \u03b1\u2032 lower\n\u1fb7 = with-accent-iota circumflex\n-- \u1fb8\n-- \u1fb9\n\u1fba : Token \u03b1\u2032 upper\n\u1fba = with-accent grave\n\u1fbb : Token \u03b1\u2032 upper\n\u1fbb = with-accent acute\n\u1fbc : Token \u03b1\u2032 upper\n\u1fbc = with-iota\n\n-- Mapping\n\ndata Accent : Set where\n acute-mark grave-mark circumflex-mark : Accent\n\nletter-to-accent : \u2200 {v} \u2192 v accent \u2192 Accent\nletter-to-accent acute = acute-mark\nletter-to-accent grave = grave-mark\nletter-to-accent circumflex = circumflex-mark\n\nget-accent : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Accent\nget-accent (with-accent a) = just (letter-to-accent a)\nget-accent (with-accent-breathing a _) = just (letter-to-accent a)\nget-accent (with-accent-breathing-iota a _) = just (letter-to-accent a)\nget-accent (with-accent-diaeresis a) = just (letter-to-accent a)\nget-accent (with-accent-iota a) = just (letter-to-accent a)\nget-accent _ = nothing\n\ndata Breathing : Set where\n smooth-mark rough-mark : Breathing\n\nletter-to-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Breathing\nletter-to-breathing smooth = smooth-mark\nletter-to-breathing rough = rough-mark\n\nget-breathing : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Breathing\nget-breathing (with-breathing x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing _ x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing-iota _ x) = just (letter-to-breathing x)\nget-breathing (with-breathing-iota x) = just (letter-to-breathing x)\nget-breathing _ = nothing\n\ndata IotaSubscript : Set where\n iota-subscript-mark : IotaSubscript\n\nget-iota-subscript : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe IotaSubscript\nget-iota-subscript (with-accent-breathing-iota _ _) = just iota-subscript-mark\nget-iota-subscript (with-accent-iota _) = just iota-subscript-mark\nget-iota-subscript (with-breathing-iota _) = just iota-subscript-mark\nget-iota-subscript _ = nothing\n\ndata Diaeresis : Set where\n diaeresis-mark : Diaeresis\n\nget-diaeresis : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Diaeresis\nget-diaeresis with-diaeresis = just diaeresis-mark\nget-diaeresis (with-accent-diaeresis _) = just diaeresis-mark\nget-diaeresis _ = nothing\n\ndata FinalForm : Set where\n final-form : FinalForm\n\nget-final-form : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe FinalForm\nget-final-form final = just final-form\nget-final-form _ = nothing\n","old_contents":"module Text.Greek.Script where\n\nopen import Data.Maybe\nopen import Data.Vec\nopen import Relation.Nullary using (\u00ac_)\nopen import Relation.Binary.PropositionalEquality using (_\u2262_)\n\ndata Case : Set where\n lower upper : Case\n\ndata Letter : Set where\n \u03b1\u2032 \u03b2\u2032 \u03b3\u2032 \u03b4\u2032 \u03b5\u2032 \u03b6\u2032 \u03b7\u2032 \u03b8\u2032 \u03b9\u2032 \u03ba\u2032 \u03bb\u2032 \u03bc\u2032 \u03bd\u2032 \u03be\u2032 \u03bf\u2032 \u03c0\u2032 \u03c1\u2032 \u03c3\u2032 \u03c4\u2032 \u03c5\u2032 \u03c6\u2032 \u03c7\u2032 \u03c8\u2032 \u03c9\u2032 : Letter\n\nvowels : Vec _ _\nvowels = \u03b1\u2032 \u2237 \u03b5\u2032 \u2237 \u03b7\u2032 \u2237 \u03b9\u2032 \u2237 \u03bf\u2032 \u2237 \u03c5\u2032 \u2237 \u03c9\u2032 \u2237 []\n\nalways-short-letters : Vec _ _\nalways-short-letters = \u03b5\u2032 \u2237 \u03bf\u2032 \u2237 []\n\ndiaeresis-letters : Vec _ _\ndiaeresis-letters = \u03b9\u2032 \u2237 \u03c5\u2032 \u2237 []\n\niota-subscript-letters : Vec _ _\niota-subscript-letters = \u03b1\u2032 \u2237 \u03b7\u2032 \u2237 \u03c9\u2032 \u2237 []\n\ndata _vowel : Letter \u2192 Set where\n is-vowel : \u2200 {v} \u2192 v \u2208 vowels \u2192 v vowel\n\ndata _always-short : Letter \u2192 Set where\n is-always-short : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 always-short-letters \u2192 v always-short\n\ndata _diaeresis : Letter \u2192 Set where\n add-diaeresis : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 diaeresis-letters \u2192 v diaeresis\n\ndata _\u27e6_\u27e7-final : Letter \u2192 Case \u2192 Set where\n make-final : \u03c3\u2032 \u27e6 lower \u27e7-final\n\ndata _\u27e6_\u27e7-smooth : Letter \u2192 Case \u2192 Set where\n add-smooth-lower-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u27e6 lower \u27e7-smooth\n add-smooth-\u03c1 : \u03c1\u2032 \u27e6 lower \u27e7-smooth\n add-smooth-upper-vowel-not-\u03a5 : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2262 \u03c5\u2032 \u2192 v \u27e6 upper \u27e7-smooth\n\ndata _with-rough : Letter \u2192 Set where\n add-rough-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v with-rough\n add-rough-\u03c1 : \u03c1\u2032 with-rough\n\ndata _iota-subscript : Letter \u2192 Set where\n add-iota-subscript : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 iota-subscript-letters \u2192 v iota-subscript\n\ndata _\u27e6_\u27e7-breathing : Letter \u2192 Case \u2192 Set where\n smooth : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 \u27e6 c \u27e7-smooth \u2984 \u2192 \u2113 \u27e6 c \u27e7-breathing\n rough : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 with-rough \u2984 \u2192 \u2113 \u27e6 c \u27e7-breathing\n\ndata _long-vowel : Letter \u2192 Set where\n make-long-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 \u00ac v always-short \u2192 v long-vowel\n\ndata _accent : Letter \u2192 Set where\n acute : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n grave : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n circumflex : \u2200 {\u2113} \u2192 \u2983 p : \u2113 long-vowel \u2984 \u2192 \u2113 accent\n\ndata Token : Letter \u2192 Case \u2192 Set where\n unmarked : \u2200 {\u2113 c} \u2192 Token \u2113 c\n with-accent : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 Token \u2113 c\n with-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-diaeresis : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 diaeresis \u2984 \u2192 Token \u2113 c\n with-accent-diaeresis : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2983 p : \u2113 diaeresis \u2984 \u2192 Token \u2113 c\n with-accent-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-iota : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n final : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 \u27e6 c \u27e7-final \u2984 \u2192 Token \u2113 c\n\n-- Constructions\n\n-- \u0391 \u03b1\ninstance \u03b1-vowel : \u03b1\u2032 vowel\n\u03b1-vowel = is-vowel here\n\n\u00ac\u03b1-always-short : \u00ac \u03b1\u2032 always-short\n\u00ac\u03b1-always-short (is-always-short (there (there ())))\n\ninstance \u03b1-long-vowel : \u03b1\u2032 long-vowel\n\u03b1-long-vowel = make-long-vowel \u00ac\u03b1-always-short\n\ninstance \u03b1-smooth : \u03b1\u2032 \u27e6 lower \u27e7-smooth\n\u03b1-smooth = add-smooth-lower-vowel\n\ninstance \u0391-smooth : \u03b1\u2032 \u27e6 upper \u27e7-smooth\n\u0391-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b1-rough : \u03b1\u2032 with-rough\n\u03b1-rough = add-rough-vowel\n\ninstance \u03b1-iota-subscript : \u03b1\u2032 iota-subscript\n\u03b1-iota-subscript = add-iota-subscript here\n\n-- \u0395 \u03b5\ninstance \u03b5-vowel : \u03b5\u2032 vowel\n\u03b5-vowel = is-vowel (there here)\n\n\u03b5-always-short : \u03b5\u2032 always-short\n\u03b5-always-short = is-always-short here\n\ninstance \u03b5-smooth : \u03b5\u2032 \u27e6 lower \u27e7-smooth\n\u03b5-smooth = add-smooth-lower-vowel\n\ninstance \u0395-smooth : \u03b5\u2032 \u27e6 upper \u27e7-smooth\n\u0395-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b5-rough : \u03b5\u2032 with-rough\n\u03b5-rough = add-rough-vowel\n\n-- \u0397 \u03b7\ninstance \u03b7-vowel : \u03b7\u2032 vowel\n\u03b7-vowel = is-vowel (there (there here))\n\n\u00ac\u03b7-always-short : \u00ac \u03b7\u2032 always-short\n\u00ac\u03b7-always-short (is-always-short (there (there ())))\n\ninstance \u03b7-long-vowel : \u03b7\u2032 long-vowel\n\u03b7-long-vowel = make-long-vowel \u00ac\u03b7-always-short\n\ninstance \u03b7-smooth : \u03b7\u2032 \u27e6 lower \u27e7-smooth\n\u03b7-smooth = add-smooth-lower-vowel\n\ninstance \u0397-smooth : \u03b7\u2032 \u27e6 upper \u27e7-smooth\n\u0397-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b7-rough : \u03b7\u2032 with-rough\n\u03b7-rough = add-rough-vowel\n\n\u03b7-iota-subscript : \u03b7\u2032 iota-subscript\n\u03b7-iota-subscript = add-iota-subscript (there here)\n\n-- \u0399 \u03b9\n\ninstance \u03b9-vowel : \u03b9\u2032 vowel\n\u03b9-vowel = is-vowel (there (there (there here)))\n\n\u00ac\u03b9-always-short : \u00ac \u03b9\u2032 always-short\n\u00ac\u03b9-always-short (is-always-short (there (there ())))\n\ninstance \u03b9-long-vowel : \u03b9\u2032 long-vowel\n\u03b9-long-vowel = make-long-vowel \u00ac\u03b9-always-short\n\n\u03b9-not-\u03c5 : \u03b9\u2032 \u2262 \u03c5\u2032\n\u03b9-not-\u03c5 ()\n\ninstance \u03b9-smooth : \u03b9\u2032 \u27e6 lower \u27e7-smooth\n\u03b9-smooth = add-smooth-lower-vowel\n\ninstance \u0399-smooth : \u03b9\u2032 \u27e6 upper \u27e7-smooth\n\u0399-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b9-rough : \u03b9\u2032 with-rough\n\u03b9-rough = add-rough-vowel\n\n-- \u039f \u03bf\n\n-- Unicode\n-- U+0390 - U+03CE\n\u0390 : Token \u03b9\u2032 lower -- U+0390 GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS\n\u0390 = unmarked\n\u0391 : Token \u03b1\u2032 upper -- U+0391 GREEK CAPITAL LETTER ALPHA\n\u0391 = unmarked\n\u0392 : Token \u03b2\u2032 upper -- U+0392 GREEK CAPITAL LETTER BETA\n\u0392 = unmarked\n\u0393 : Token \u03b3\u2032 upper -- U+0393 GREEK CAPITAL LETTER GAMMA\n\u0393 = unmarked\n\u0394 : Token \u03b4\u2032 upper -- U+0394 GREEK CAPITAL LETTER DELTA\n\u0394 = unmarked\n\u0395 : Token \u03b5\u2032 upper -- U+0395 GREEK CAPITAL LETTER EPSILON\n\u0395 = unmarked\n\u0396 : Token \u03b6\u2032 upper -- U+0396 GREEK CAPITAL LETTER ZETA\n\u0396 = unmarked\n\u0397 : Token \u03b7\u2032 upper -- U+0397 GREEK CAPITAL LETTER ETA\n\u0397 = unmarked\n\u0398 : Token \u03b8\u2032 upper -- U+0398 GREEK CAPITAL LETTER THETA\n\u0398 = unmarked\n\u0399 : Token \u03b9\u2032 upper -- U+0399 GREEK CAPITAL LETTER IOTA\n\u0399 = unmarked\n\u039a : Token \u03ba\u2032 upper -- U+039A GREEK CAPITAL LETTER KAPPA\n\u039a = unmarked\n\u039b : Token \u03bb\u2032 upper -- U+039B GREEK CAPITAL LETTER LAMDA\n\u039b = unmarked\n\u039c : Token \u03bc\u2032 upper -- U+039C GREEK CAPITAL LETTER MU\n\u039c = unmarked\n\u039d : Token \u03bd\u2032 upper -- U+039D GREEK CAPITAL LETTER NU\n\u039d = unmarked\n\u039e : Token \u03be\u2032 upper -- U+039E GREEK CAPITAL LETTER XI\n\u039e = unmarked\n\u039f : Token \u03bf\u2032 upper -- U+039F GREEK CAPITAL LETTER OMICRON\n\u039f = unmarked\n\u03a0 : Token \u03c0\u2032 upper -- U+03A0 GREEK CAPITAL LETTER PI\n\u03a0 = unmarked\n\u03a1 : Token \u03c1\u2032 upper -- U+03A1 GREEK CAPITAL LETTER RHO\n\u03a1 = unmarked\n -- U+03A2 \n\u03a3 : Token \u03c3\u2032 upper -- U+03A3 GREEK CAPITAL LETTER SIGMA\n\u03a3 = unmarked\n\u03a4 : Token \u03c4\u2032 upper -- U+03A4 GREEK CAPITAL LETTER TAU\n\u03a4 = unmarked\n\u03a5 : Token \u03c5\u2032 upper -- U+03A5 GREEK CAPITAL LETTER UPSILON\n\u03a5 = unmarked\n\u03a6 : Token \u03c6\u2032 upper -- U+03A6 GREEK CAPITAL LETTER PHI\n\u03a6 = unmarked\n\u03a7 : Token \u03c7\u2032 upper -- U+03A7 GREEK CAPITAL LETTER CHI\n\u03a7 = unmarked\n\u03a8 : Token \u03c8\u2032 upper -- U+03A8 GREEK CAPITAL LETTER PSI\n\u03a8 = unmarked\n\u03a9 : Token \u03c9\u2032 upper -- U+03A9 GREEK CAPITAL LETTER OMEGA\n\u03a9 = unmarked\n\u03aa : Token \u03b9\u2032 upper -- U+03AA GREEK CAPITAL LETTER IOTA WITH DIALYTIKA\n\u03aa = unmarked\n\u03ab : Token \u03c5\u2032 upper -- U+03AB GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA\n\u03ab = unmarked\n\u03ac : Token \u03b1\u2032 lower -- U+03AC GREEK SMALL LETTER ALPHA WITH TONOS\n\u03ac = unmarked\n\u03ad : Token \u03b5\u2032 lower -- U+03AD GREEK SMALL LETTER EPSILON WITH TONOS\n\u03ad = unmarked\n\u03ae : Token \u03b7\u2032 lower -- U+03AE GREEK SMALL LETTER ETA WITH TONOS\n\u03ae = unmarked\n\u03af : Token \u03b9\u2032 lower -- U+03AF GREEK SMALL LETTER IOTA WITH TONOS\n\u03af = unmarked\n\u03b0 : Token \u03c5\u2032 lower -- U+03B0 GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS\n\u03b0 = unmarked\n\u03b1 : Token \u03b1\u2032 lower -- U+03B1 GREEK SMALL LETTER ALPHA\n\u03b1 = unmarked\n\u03b2 : Token \u03b2\u2032 lower -- U+03B2 GREEK SMALL LETTER BETA\n\u03b2 = unmarked\n\u03b3 : Token \u03b3\u2032 lower -- U+03B3 GREEK SMALL LETTER GAMMA\n\u03b3 = unmarked\n\u03b4 : Token \u03b4\u2032 lower -- U+03B4 GREEK SMALL LETTER DELTA\n\u03b4 = unmarked\n\u03b5 : Token \u03b5\u2032 lower -- U+03B5 GREEK SMALL LETTER EPSILON\n\u03b5 = unmarked\n\u03b6 : Token \u03b6\u2032 lower -- U+03B6 GREEK SMALL LETTER ZETA\n\u03b6 = unmarked\n\u03b7 : Token \u03b7\u2032 lower -- U+03B7 GREEK SMALL LETTER ETA\n\u03b7 = unmarked\n\u03b8 : Token \u03b8\u2032 lower -- U+03B8 GREEK SMALL LETTER THETA\n\u03b8 = unmarked\n\u03b9 : Token \u03b9\u2032 lower -- U+03B9 GREEK SMALL LETTER IOTA\n\u03b9 = unmarked\n\u03ba : Token \u03ba\u2032 lower -- U+03BA GREEK SMALL LETTER KAPPA\n\u03ba = unmarked\n\u2219\u03bb : Token \u03bb\u2032 lower -- U+03BB GREEK SMALL LETTER LAMDA\n\u2219\u03bb = unmarked\n\u03bc : Token \u03bc\u2032 lower -- U+03BC GREEK SMALL LETTER MU\n\u03bc = unmarked\n\u03bd : Token \u03bd\u2032 lower -- U+03BD GREEK SMALL LETTER NU\n\u03bd = unmarked\n\u03be : Token \u03be\u2032 lower -- U+03BE GREEK SMALL LETTER XI\n\u03be = unmarked\n\u03bf : Token \u03bf\u2032 lower -- U+03BF GREEK SMALL LETTER OMICRON\n\u03bf = unmarked\n\u03c0 : Token \u03c0\u2032 lower -- U+03C0 GREEK SMALL LETTER PI\n\u03c0 = unmarked\n\u03c1 : Token \u03c1\u2032 lower -- U+03C1 GREEK SMALL LETTER RHO\n\u03c1 = unmarked\n\u03c2 : Token \u03c3\u2032 lower -- U+03C2 GREEK SMALL LETTER FINAL SIGMA\n\u03c2 = unmarked\n\u03c3 : Token \u03c3\u2032 lower -- U+03C3 GREEK SMALL LETTER SIGMA\n\u03c3 = unmarked\n\u03c4 : Token \u03c4\u2032 lower -- U+03C4 GREEK SMALL LETTER TAU\n\u03c4 = unmarked\n\u03c5 : Token \u03c5\u2032 lower -- U+03C5 GREEK SMALL LETTER UPSILON\n\u03c5 = unmarked\n\u03c6 : Token \u03c6\u2032 lower -- U+03C6 GREEK SMALL LETTER PHI\n\u03c6 = unmarked\n\u03c7 : Token \u03c7\u2032 lower -- U+03C7 GREEK SMALL LETTER CHI\n\u03c7 = unmarked\n\u03c8 : Token \u03c8\u2032 lower -- U+03C8 GREEK SMALL LETTER PSI\n\u03c8 = unmarked\n\u03c9 : Token \u03c9\u2032 lower -- U+03C9 GREEK SMALL LETTER OMEGA\n\u03c9 = unmarked\n\u03ca : Token \u03b9\u2032 lower -- U+03CA GREEK SMALL LETTER IOTA WITH DIALYTIKA\n\u03ca = unmarked\n\u03cb : Token \u03c5\u2032 lower -- U+03CB GREEK SMALL LETTER UPSILON WITH DIALYTIKA\n\u03cb = unmarked\n\u03cc : Token \u03bf\u2032 lower -- U+03CC GREEK SMALL LETTER OMICRON WITH TONOS\n\u03cc = unmarked\n\u03cd : Token \u03c5\u2032 lower -- U+03CD GREEK SMALL LETTER UPSILON WITH TONOS\n\u03cd = unmarked\n\u03ce : Token \u03c9\u2032 lower -- U+03CE GREEK SMALL LETTER OMEGA WITH TONOS\n\u03ce = unmarked\n\n\n\u1f00 : Token \u03b1\u2032 lower\n\u1f00 = with-breathing smooth\n\u1f01 : Token \u03b1\u2032 lower\n\u1f01 = with-breathing rough\n\u1f02 : Token \u03b1\u2032 lower\n\u1f02 = with-accent-breathing grave smooth\n\u1f03 : Token \u03b1\u2032 lower\n\u1f03 = with-accent-breathing grave rough\n\u1f04 : Token \u03b1\u2032 lower\n\u1f04 = with-accent-breathing acute smooth\n\u1f05 : Token \u03b1\u2032 lower\n\u1f05 = with-accent-breathing acute rough\n\u1f06 : Token \u03b1\u2032 lower\n\u1f06 = with-accent-breathing circumflex smooth\n\u1f07 : Token \u03b1\u2032 lower\n\u1f07 = with-accent-breathing circumflex rough\n\u1f08 : Token \u03b1\u2032 upper\n\u1f08 = with-breathing smooth\n\u1f09 : Token \u03b1\u2032 upper\n\u1f09 = with-breathing rough\n\u1f0a : Token \u03b1\u2032 upper\n\u1f0a = with-accent-breathing grave smooth\n\u1f0b : Token \u03b1\u2032 upper\n\u1f0b = with-accent-breathing grave rough\n\u1f0c : Token \u03b1\u2032 upper\n\u1f0c = with-accent-breathing acute smooth\n\u1f0d : Token \u03b1\u2032 upper\n\u1f0d = with-accent-breathing acute rough\n\u1f0e : Token \u03b1\u2032 upper\n\u1f0e = with-accent-breathing circumflex smooth\n\u1f0f : Token \u03b1\u2032 upper\n\u1f0f = with-accent-breathing circumflex rough\n\n-- U+1F7x\n\u1f70 : Token \u03b1\u2032 lower\n\u1f70 = with-accent grave\n\u1f71 : Token \u03b1\u2032 lower\n\u1f71 = with-accent acute\n\n-- U+1F8x\n\u1f80 : Token \u03b1\u2032 lower\n\u1f80 = with-breathing-iota smooth\n\u1f81 : Token \u03b1\u2032 lower\n\u1f81 = with-breathing-iota rough\n\u1f82 : Token \u03b1\u2032 lower\n\u1f82 = with-accent-breathing-iota grave smooth\n\u1f83 : Token \u03b1\u2032 lower\n\u1f83 = with-accent-breathing-iota grave rough\n\u1f84 : Token \u03b1\u2032 lower\n\u1f84 = with-accent-breathing-iota acute smooth\n\u1f85 : Token \u03b1\u2032 lower\n\u1f85 = with-accent-breathing-iota acute rough\n\u1f86 : Token \u03b1\u2032 lower\n\u1f86 = with-accent-breathing-iota circumflex smooth\n\u1f87 : Token \u03b1\u2032 lower\n\u1f87 = with-accent-breathing-iota circumflex rough\n\u1f88 : Token \u03b1\u2032 upper\n\u1f88 = with-breathing-iota smooth\n\u1f89 : Token \u03b1\u2032 upper\n\u1f89 = with-breathing-iota rough\n\u1f8a : Token \u03b1\u2032 upper\n\u1f8a = with-accent-breathing-iota grave smooth\n\u1f8b : Token \u03b1\u2032 upper\n\u1f8b = with-accent-breathing-iota grave rough\n\u1f8c : Token \u03b1\u2032 upper\n\u1f8c = with-accent-breathing-iota acute smooth\n\u1f8d : Token \u03b1\u2032 upper\n\u1f8d = with-accent-breathing-iota acute rough\n\u1f8e : Token \u03b1\u2032 upper\n\u1f8e = with-accent-breathing-iota circumflex smooth\n\u1f8f : Token \u03b1\u2032 upper\n\u1f8f = with-accent-breathing-iota circumflex rough\n\n-- U+1FBx\n-- \u1fb0\n-- \u1fb1\n\u1fb2 : Token \u03b1\u2032 lower\n\u1fb2 = with-accent-iota grave\n\u1fb3 : Token \u03b1\u2032 lower\n\u1fb3 = with-iota\n\u1fb4 : Token \u03b1\u2032 lower\n\u1fb4 = with-accent-iota acute\n\u1fb6 : Token \u03b1\u2032 lower\n\u1fb6 = with-accent circumflex\n\u1fb7 : Token \u03b1\u2032 lower\n\u1fb7 = with-accent-iota circumflex\n-- \u1fb8\n-- \u1fb9\n\u1fba : Token \u03b1\u2032 upper\n\u1fba = with-accent grave\n\u1fbb : Token \u03b1\u2032 upper\n\u1fbb = with-accent acute\n\u1fbc : Token \u03b1\u2032 upper\n\u1fbc = with-iota\n\n-- Mapping\n\ndata Accent : Set where\n acute-mark grave-mark circumflex-mark : Accent\n\nletter-to-accent : \u2200 {v} \u2192 v accent \u2192 Accent\nletter-to-accent acute = acute-mark\nletter-to-accent grave = grave-mark\nletter-to-accent circumflex = circumflex-mark\n\nget-accent : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Accent\nget-accent (with-accent a) = just (letter-to-accent a)\nget-accent (with-accent-breathing a _) = just (letter-to-accent a)\nget-accent (with-accent-breathing-iota a _) = just (letter-to-accent a)\nget-accent (with-accent-diaeresis a) = just (letter-to-accent a)\nget-accent (with-accent-iota a) = just (letter-to-accent a)\nget-accent _ = nothing\n\ndata Breathing : Set where\n smooth-mark rough-mark : Breathing\n\nletter-to-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Breathing\nletter-to-breathing smooth = smooth-mark\nletter-to-breathing rough = rough-mark\n\nget-breathing : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Breathing\nget-breathing (with-breathing x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing _ x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing-iota _ x) = just (letter-to-breathing x)\nget-breathing (with-breathing-iota x) = just (letter-to-breathing x)\nget-breathing _ = nothing\n\ndata IotaSubscript : Set where\n iota-subscript-mark : IotaSubscript\n\nget-iota-subscript : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe IotaSubscript\nget-iota-subscript (with-accent-breathing-iota _ _) = just iota-subscript-mark\nget-iota-subscript (with-accent-iota _) = just iota-subscript-mark\nget-iota-subscript (with-breathing-iota _) = just iota-subscript-mark\nget-iota-subscript _ = nothing\n\ndata Diaeresis : Set where\n diaeresis-mark : Diaeresis\n\nget-diaeresis : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Diaeresis\nget-diaeresis with-diaeresis = just diaeresis-mark\nget-diaeresis (with-accent-diaeresis _) = just diaeresis-mark\nget-diaeresis _ = nothing\n\ndata FinalForm : Set where\n final-form : FinalForm\n\nget-final-form : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe FinalForm\nget-final-form final = just final-form\nget-final-form _ = nothing\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7b98f7162bfda73ebd29d9b3e00a96ffe8354b0c","subject":"Add the remainder of polytonic Greek Unicode.","message":"Add the remainder of polytonic Greek Unicode.\n","repos":"scott-fleischman\/greek-grammar,scott-fleischman\/greek-grammar","old_file":"agda\/Text\/Greek\/Script.agda","new_file":"agda\/Text\/Greek\/Script.agda","new_contents":"module Text.Greek.Script where\n\nopen import Data.Maybe\nopen import Data.Vec\nopen import Relation.Nullary using (\u00ac_)\nopen import Relation.Binary.PropositionalEquality using (_\u2262_)\n\ndata Case : Set where\n lower upper : Case\n\ndata Letter : Set where\n \u03b1\u2032 \u03b2\u2032 \u03b3\u2032 \u03b4\u2032 \u03b5\u2032 \u03b6\u2032 \u03b7\u2032 \u03b8\u2032 \u03b9\u2032 \u03ba\u2032 \u03bb\u2032 \u03bc\u2032 \u03bd\u2032 \u03be\u2032 \u03bf\u2032 \u03c0\u2032 \u03c1\u2032 \u03c3\u2032 \u03c4\u2032 \u03c5\u2032 \u03c6\u2032 \u03c7\u2032 \u03c8\u2032 \u03c9\u2032 : Letter\n\nvowels : Vec _ _\nvowels = \u03b1\u2032 \u2237 \u03b5\u2032 \u2237 \u03b7\u2032 \u2237 \u03b9\u2032 \u2237 \u03bf\u2032 \u2237 \u03c5\u2032 \u2237 \u03c9\u2032 \u2237 []\n\nalways-short-letters : Vec _ _\nalways-short-letters = \u03b5\u2032 \u2237 \u03bf\u2032 \u2237 []\n\ndiaeresis-letters : Vec _ _\ndiaeresis-letters = \u03b9\u2032 \u2237 \u03c5\u2032 \u2237 []\n\niota-subscript-letters : Vec _ _\niota-subscript-letters = \u03b1\u2032 \u2237 \u03b7\u2032 \u2237 \u03c9\u2032 \u2237 []\n\ndata _vowel : Letter \u2192 Set where\n is-vowel : \u2200 {v} \u2192 v \u2208 vowels \u2192 v vowel\n\ndata _always-short : Letter \u2192 Set where\n is-always-short : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 always-short-letters \u2192 v always-short\n\ndata _diaeresis : Letter \u2192 Set where\n add-diaeresis : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 diaeresis-letters \u2192 v diaeresis\n\ndata _\u27e6_\u27e7-final : Letter \u2192 Case \u2192 Set where\n make-final : \u03c3\u2032 \u27e6 lower \u27e7-final\n\ndata _\u27e6_\u27e7-smooth : Letter \u2192 Case \u2192 Set where\n add-smooth-lower-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u27e6 lower \u27e7-smooth\n add-smooth-\u03c1 : \u03c1\u2032 \u27e6 lower \u27e7-smooth\n add-smooth-upper-vowel-not-\u03a5 : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2262 \u03c5\u2032 \u2192 v \u27e6 upper \u27e7-smooth\n\ndata _with-rough : Letter \u2192 Set where\n add-rough-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v with-rough\n add-rough-\u03c1 : \u03c1\u2032 with-rough\n\ndata _iota-subscript : Letter \u2192 Set where\n add-iota-subscript : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 iota-subscript-letters \u2192 v iota-subscript\n\ndata _\u27e6_\u27e7-breathing : Letter \u2192 Case \u2192 Set where\n smooth : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 \u27e6 c \u27e7-smooth \u2984 \u2192 \u2113 \u27e6 c \u27e7-breathing\n rough : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 with-rough \u2984 \u2192 \u2113 \u27e6 c \u27e7-breathing\n\ndata _long-vowel : Letter \u2192 Set where\n make-long-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 \u00ac v always-short \u2192 v long-vowel\n\ndata _accent : Letter \u2192 Set where\n acute : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n grave : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n circumflex : \u2200 {\u2113} \u2192 \u2983 p : \u2113 long-vowel \u2984 \u2192 \u2113 accent\n\ndata Token : Letter \u2192 Case \u2192 Set where\n unmarked : \u2200 {\u2113 c} \u2192 Token \u2113 c\n with-accent : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 Token \u2113 c\n with-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-diaeresis : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 diaeresis \u2984 \u2192 Token \u2113 c\n with-accent-diaeresis : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2983 p : \u2113 diaeresis \u2984 \u2192 Token \u2113 c\n with-accent-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-iota : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n final : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 \u27e6 c \u27e7-final \u2984 \u2192 Token \u2113 c\n\n-- Constructions\n\n-- \u0391 \u03b1\ninstance \u03b1-vowel : \u03b1\u2032 vowel\n\u03b1-vowel = is-vowel here\n\n\u00ac\u03b1-always-short : \u00ac \u03b1\u2032 always-short\n\u00ac\u03b1-always-short (is-always-short (there (there ())))\n\ninstance \u03b1-long-vowel : \u03b1\u2032 long-vowel\n\u03b1-long-vowel = make-long-vowel \u00ac\u03b1-always-short\n\ninstance \u03b1-smooth : \u03b1\u2032 \u27e6 lower \u27e7-smooth\n\u03b1-smooth = add-smooth-lower-vowel\n\ninstance \u0391-smooth : \u03b1\u2032 \u27e6 upper \u27e7-smooth\n\u0391-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b1-rough : \u03b1\u2032 with-rough\n\u03b1-rough = add-rough-vowel\n\ninstance \u03b1-iota-subscript : \u03b1\u2032 iota-subscript\n\u03b1-iota-subscript = add-iota-subscript here\n\n-- \u0395 \u03b5\ninstance \u03b5-vowel : \u03b5\u2032 vowel\n\u03b5-vowel = is-vowel (there here)\n\n\u03b5-always-short : \u03b5\u2032 always-short\n\u03b5-always-short = is-always-short here\n\ninstance \u03b5-smooth : \u03b5\u2032 \u27e6 lower \u27e7-smooth\n\u03b5-smooth = add-smooth-lower-vowel\n\ninstance \u0395-smooth : \u03b5\u2032 \u27e6 upper \u27e7-smooth\n\u0395-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b5-rough : \u03b5\u2032 with-rough\n\u03b5-rough = add-rough-vowel\n\n-- \u0397 \u03b7\ninstance \u03b7-vowel : \u03b7\u2032 vowel\n\u03b7-vowel = is-vowel (there (there here))\n\n\u00ac\u03b7-always-short : \u00ac \u03b7\u2032 always-short\n\u00ac\u03b7-always-short (is-always-short (there (there ())))\n\ninstance \u03b7-long-vowel : \u03b7\u2032 long-vowel\n\u03b7-long-vowel = make-long-vowel \u00ac\u03b7-always-short\n\ninstance \u03b7-smooth : \u03b7\u2032 \u27e6 lower \u27e7-smooth\n\u03b7-smooth = add-smooth-lower-vowel\n\ninstance \u0397-smooth : \u03b7\u2032 \u27e6 upper \u27e7-smooth\n\u0397-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b7-rough : \u03b7\u2032 with-rough\n\u03b7-rough = add-rough-vowel\n\ninstance \u03b7-iota-subscript : \u03b7\u2032 iota-subscript\n\u03b7-iota-subscript = add-iota-subscript (there here)\n\n-- \u0399 \u03b9\n\ninstance \u03b9-vowel : \u03b9\u2032 vowel\n\u03b9-vowel = is-vowel (there (there (there here)))\n\n\u00ac\u03b9-always-short : \u00ac \u03b9\u2032 always-short\n\u00ac\u03b9-always-short (is-always-short (there (there ())))\n\ninstance \u03b9-long-vowel : \u03b9\u2032 long-vowel\n\u03b9-long-vowel = make-long-vowel \u00ac\u03b9-always-short\n\ninstance \u03b9-smooth : \u03b9\u2032 \u27e6 lower \u27e7-smooth\n\u03b9-smooth = add-smooth-lower-vowel\n\ninstance \u0399-smooth : \u03b9\u2032 \u27e6 upper \u27e7-smooth\n\u0399-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b9-rough : \u03b9\u2032 with-rough\n\u03b9-rough = add-rough-vowel\n\ninstance \u03b9-diaeresis : \u03b9\u2032 diaeresis\n\u03b9-diaeresis = add-diaeresis here\n\n-- \u039f \u03bf\ninstance \u03bf-vowel : \u03bf\u2032 vowel\n\u03bf-vowel = is-vowel (there (there (there (there here))))\n\n\u03bf-always-short : \u03bf\u2032 always-short\n\u03bf-always-short = is-always-short (there here)\n\ninstance \u03bf-smooth : \u03bf\u2032 \u27e6 lower \u27e7-smooth\n\u03bf-smooth = add-smooth-lower-vowel\n\ninstance \u039f-smooth : \u03bf\u2032 \u27e6 upper \u27e7-smooth\n\u039f-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03bf-rough : \u03bf\u2032 with-rough\n\u03bf-rough = add-rough-vowel\n\n-- \u03a1 \u03c1\ninstance \u03c1-smooth : \u03c1\u2032 \u27e6 lower \u27e7-smooth\n\u03c1-smooth = add-smooth-\u03c1\n\ninstance \u03c1-rough : \u03c1\u2032 with-rough\n\u03c1-rough = add-rough-\u03c1\n\n-- \u03a3 \u03c3\ninstance \u03c3-final : \u03c3\u2032 \u27e6 lower \u27e7-final\n\u03c3-final = make-final\n\n-- \u03a5 \u03c5\ninstance \u03c5-vowel : \u03c5\u2032 vowel\n\u03c5-vowel = is-vowel (there (there (there (there (there here)))))\n\n\u00ac\u03c5-always-short : \u00ac \u03c5\u2032 always-short\n\u00ac\u03c5-always-short (is-always-short (there (there ())))\n\ninstance \u03c5-long-vowel : \u03c5\u2032 long-vowel\n\u03c5-long-vowel = make-long-vowel \u00ac\u03c5-always-short\n\ninstance \u03c5-smooth : \u03c5\u2032 \u27e6 lower \u27e7-smooth\n\u03c5-smooth = add-smooth-lower-vowel\n\ninstance \u03c5-rough : \u03c5\u2032 with-rough\n\u03c5-rough = add-rough-vowel\n\ninstance \u03c5-diaeresis : \u03c5\u2032 diaeresis\n\u03c5-diaeresis = add-diaeresis (there here)\n\n-- \u03a9 \u03c9\ninstance \u03c9-vowel : \u03c9\u2032 vowel\n\u03c9-vowel = is-vowel (there (there (there (there (there (there here))))))\n\n\u00ac\u03c9-always-short : \u00ac \u03c9\u2032 always-short\n\u00ac\u03c9-always-short (is-always-short (there (there ())))\n\ninstance \u03c9-long-vowel : \u03c9\u2032 long-vowel\n\u03c9-long-vowel = make-long-vowel \u00ac\u03c9-always-short\n\ninstance \u03c9-smooth : \u03c9\u2032 \u27e6 lower \u27e7-smooth\n\u03c9-smooth = add-smooth-lower-vowel\n\ninstance \u03a9-smooth : \u03c9\u2032 \u27e6 upper \u27e7-smooth\n\u03a9-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03c9-rough : \u03c9\u2032 with-rough\n\u03c9-rough = add-rough-vowel\n\ninstance \u03c9-iota-subscript : \u03c9\u2032 iota-subscript\n\u03c9-iota-subscript = add-iota-subscript (there (there here))\n\n\n-- Unicode\n-- U+0390 - U+03CE\n\u0390 : Token \u03b9\u2032 lower -- U+0390 GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS\n\u0390 = with-accent-diaeresis acute\n\u0391 : Token \u03b1\u2032 upper -- U+0391 GREEK CAPITAL LETTER ALPHA\n\u0391 = unmarked\n\u0392 : Token \u03b2\u2032 upper -- U+0392 GREEK CAPITAL LETTER BETA\n\u0392 = unmarked\n\u0393 : Token \u03b3\u2032 upper -- U+0393 GREEK CAPITAL LETTER GAMMA\n\u0393 = unmarked\n\u0394 : Token \u03b4\u2032 upper -- U+0394 GREEK CAPITAL LETTER DELTA\n\u0394 = unmarked\n\u0395 : Token \u03b5\u2032 upper -- U+0395 GREEK CAPITAL LETTER EPSILON\n\u0395 = unmarked\n\u0396 : Token \u03b6\u2032 upper -- U+0396 GREEK CAPITAL LETTER ZETA\n\u0396 = unmarked\n\u0397 : Token \u03b7\u2032 upper -- U+0397 GREEK CAPITAL LETTER ETA\n\u0397 = unmarked\n\u0398 : Token \u03b8\u2032 upper -- U+0398 GREEK CAPITAL LETTER THETA\n\u0398 = unmarked\n\u0399 : Token \u03b9\u2032 upper -- U+0399 GREEK CAPITAL LETTER IOTA\n\u0399 = unmarked\n\u039a : Token \u03ba\u2032 upper -- U+039A GREEK CAPITAL LETTER KAPPA\n\u039a = unmarked\n\u039b : Token \u03bb\u2032 upper -- U+039B GREEK CAPITAL LETTER LAMDA\n\u039b = unmarked\n\u039c : Token \u03bc\u2032 upper -- U+039C GREEK CAPITAL LETTER MU\n\u039c = unmarked\n\u039d : Token \u03bd\u2032 upper -- U+039D GREEK CAPITAL LETTER NU\n\u039d = unmarked\n\u039e : Token \u03be\u2032 upper -- U+039E GREEK CAPITAL LETTER XI\n\u039e = unmarked\n\u039f : Token \u03bf\u2032 upper -- U+039F GREEK CAPITAL LETTER OMICRON\n\u039f = unmarked\n\u03a0 : Token \u03c0\u2032 upper -- U+03A0 GREEK CAPITAL LETTER PI\n\u03a0 = unmarked\n\u03a1 : Token \u03c1\u2032 upper -- U+03A1 GREEK CAPITAL LETTER RHO\n\u03a1 = unmarked\n -- U+03A2 \n\u03a3 : Token \u03c3\u2032 upper -- U+03A3 GREEK CAPITAL LETTER SIGMA\n\u03a3 = unmarked\n\u03a4 : Token \u03c4\u2032 upper -- U+03A4 GREEK CAPITAL LETTER TAU\n\u03a4 = unmarked\n\u03a5 : Token \u03c5\u2032 upper -- U+03A5 GREEK CAPITAL LETTER UPSILON\n\u03a5 = unmarked\n\u03a6 : Token \u03c6\u2032 upper -- U+03A6 GREEK CAPITAL LETTER PHI\n\u03a6 = unmarked\n\u03a7 : Token \u03c7\u2032 upper -- U+03A7 GREEK CAPITAL LETTER CHI\n\u03a7 = unmarked\n\u03a8 : Token \u03c8\u2032 upper -- U+03A8 GREEK CAPITAL LETTER PSI\n\u03a8 = unmarked\n\u03a9 : Token \u03c9\u2032 upper -- U+03A9 GREEK CAPITAL LETTER OMEGA\n\u03a9 = unmarked\n\u03aa : Token \u03b9\u2032 upper -- U+03AA GREEK CAPITAL LETTER IOTA WITH DIALYTIKA\n\u03aa = with-diaeresis\n\u03ab : Token \u03c5\u2032 upper -- U+03AB GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA\n\u03ab = with-diaeresis\n\u03ac : Token \u03b1\u2032 lower -- U+03AC GREEK SMALL LETTER ALPHA WITH TONOS\n\u03ac = with-accent acute\n\u03ad : Token \u03b5\u2032 lower -- U+03AD GREEK SMALL LETTER EPSILON WITH TONOS\n\u03ad = with-accent acute\n\u03ae : Token \u03b7\u2032 lower -- U+03AE GREEK SMALL LETTER ETA WITH TONOS\n\u03ae = with-accent acute\n\u03af : Token \u03b9\u2032 lower -- U+03AF GREEK SMALL LETTER IOTA WITH TONOS\n\u03af = with-accent acute\n\u03b0 : Token \u03c5\u2032 lower -- U+03B0 GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS\n\u03b0 = with-accent-diaeresis acute\n\u03b1 : Token \u03b1\u2032 lower -- U+03B1 GREEK SMALL LETTER ALPHA\n\u03b1 = unmarked\n\u03b2 : Token \u03b2\u2032 lower -- U+03B2 GREEK SMALL LETTER BETA\n\u03b2 = unmarked\n\u03b3 : Token \u03b3\u2032 lower -- U+03B3 GREEK SMALL LETTER GAMMA\n\u03b3 = unmarked\n\u03b4 : Token \u03b4\u2032 lower -- U+03B4 GREEK SMALL LETTER DELTA\n\u03b4 = unmarked\n\u03b5 : Token \u03b5\u2032 lower -- U+03B5 GREEK SMALL LETTER EPSILON\n\u03b5 = unmarked\n\u03b6 : Token \u03b6\u2032 lower -- U+03B6 GREEK SMALL LETTER ZETA\n\u03b6 = unmarked\n\u03b7 : Token \u03b7\u2032 lower -- U+03B7 GREEK SMALL LETTER ETA\n\u03b7 = unmarked\n\u03b8 : Token \u03b8\u2032 lower -- U+03B8 GREEK SMALL LETTER THETA\n\u03b8 = unmarked\n\u03b9 : Token \u03b9\u2032 lower -- U+03B9 GREEK SMALL LETTER IOTA\n\u03b9 = unmarked\n\u03ba : Token \u03ba\u2032 lower -- U+03BA GREEK SMALL LETTER KAPPA\n\u03ba = unmarked\n\u2219\u03bb : Token \u03bb\u2032 lower -- U+03BB GREEK SMALL LETTER LAMDA\n\u2219\u03bb = unmarked\n\u03bc : Token \u03bc\u2032 lower -- U+03BC GREEK SMALL LETTER MU\n\u03bc = unmarked\n\u03bd : Token \u03bd\u2032 lower -- U+03BD GREEK SMALL LETTER NU\n\u03bd = unmarked\n\u03be : Token \u03be\u2032 lower -- U+03BE GREEK SMALL LETTER XI\n\u03be = unmarked\n\u03bf : Token \u03bf\u2032 lower -- U+03BF GREEK SMALL LETTER OMICRON\n\u03bf = unmarked\n\u03c0 : Token \u03c0\u2032 lower -- U+03C0 GREEK SMALL LETTER PI\n\u03c0 = unmarked\n\u03c1 : Token \u03c1\u2032 lower -- U+03C1 GREEK SMALL LETTER RHO\n\u03c1 = unmarked\n\u03c2 : Token \u03c3\u2032 lower -- U+03C2 GREEK SMALL LETTER FINAL SIGMA\n\u03c2 = final\n\u03c3 : Token \u03c3\u2032 lower -- U+03C3 GREEK SMALL LETTER SIGMA\n\u03c3 = unmarked\n\u03c4 : Token \u03c4\u2032 lower -- U+03C4 GREEK SMALL LETTER TAU\n\u03c4 = unmarked\n\u03c5 : Token \u03c5\u2032 lower -- U+03C5 GREEK SMALL LETTER UPSILON\n\u03c5 = unmarked\n\u03c6 : Token \u03c6\u2032 lower -- U+03C6 GREEK SMALL LETTER PHI\n\u03c6 = unmarked\n\u03c7 : Token \u03c7\u2032 lower -- U+03C7 GREEK SMALL LETTER CHI\n\u03c7 = unmarked\n\u03c8 : Token \u03c8\u2032 lower -- U+03C8 GREEK SMALL LETTER PSI\n\u03c8 = unmarked\n\u03c9 : Token \u03c9\u2032 lower -- U+03C9 GREEK SMALL LETTER OMEGA\n\u03c9 = unmarked\n\u03ca : Token \u03b9\u2032 lower -- U+03CA GREEK SMALL LETTER IOTA WITH DIALYTIKA\n\u03ca = with-diaeresis\n\u03cb : Token \u03c5\u2032 lower -- U+03CB GREEK SMALL LETTER UPSILON WITH DIALYTIKA\n\u03cb = with-diaeresis\n\u03cc : Token \u03bf\u2032 lower -- U+03CC GREEK SMALL LETTER OMICRON WITH TONOS\n\u03cc = with-accent acute\n\u03cd : Token \u03c5\u2032 lower -- U+03CD GREEK SMALL LETTER UPSILON WITH TONOS\n\u03cd = with-accent acute\n\u03ce : Token \u03c9\u2032 lower -- U+03CE GREEK SMALL LETTER OMEGA WITH TONOS\n\u03ce = with-accent acute\n\n-- U+1F00 - U+1FFF\n\u1f00 : Token \u03b1\u2032 lower -- U+1F00 \u1f00 GREEK SMALL LETTER ALPHA WITH PSILI\n\u1f00 = with-breathing smooth\n\u1f01 : Token \u03b1\u2032 lower -- U+1F01 \u1f01 GREEK SMALL LETTER ALPHA WITH DASIA\n\u1f01 = with-breathing rough\n\u1f02 : Token \u03b1\u2032 lower -- U+1F02 \u1f02 GREEK SMALL LETTER ALPHA WITH PSILI AND VARIA\n\u1f02 = with-accent-breathing grave smooth\n\u1f03 : Token \u03b1\u2032 lower -- U+1F03 \u1f03 GREEK SMALL LETTER ALPHA WITH DASIA AND VARIA\n\u1f03 = with-accent-breathing grave rough\n\u1f04 : Token \u03b1\u2032 lower -- U+1F04 \u1f04 GREEK SMALL LETTER ALPHA WITH PSILI AND OXIA\n\u1f04 = with-accent-breathing acute smooth\n\u1f05 : Token \u03b1\u2032 lower -- U+1F05 \u1f05 GREEK SMALL LETTER ALPHA WITH DASIA AND OXIA\n\u1f05 = with-accent-breathing acute rough\n\u1f06 : Token \u03b1\u2032 lower -- U+1F06 \u1f06 GREEK SMALL LETTER ALPHA WITH PSILI AND PERISPOMENI\n\u1f06 = with-accent-breathing circumflex smooth\n\u1f07 : Token \u03b1\u2032 lower -- U+1F07 \u1f07 GREEK SMALL LETTER ALPHA WITH DASIA AND PERISPOMENI\n\u1f07 = with-accent-breathing circumflex rough\n\u1f08 : Token \u03b1\u2032 upper -- U+1F08 \u1f08 GREEK CAPITAL LETTER ALPHA WITH PSILI\n\u1f08 = with-breathing smooth\n\u1f09 : Token \u03b1\u2032 upper -- U+1F09 \u1f09 GREEK CAPITAL LETTER ALPHA WITH DASIA\n\u1f09 = with-breathing rough\n\u1f0a : Token \u03b1\u2032 upper -- U+1F0A \u1f0a GREEK CAPITAL LETTER ALPHA WITH PSILI AND VARIA\n\u1f0a = with-accent-breathing grave smooth\n\u1f0b : Token \u03b1\u2032 upper -- U+1F0B \u1f0b GREEK CAPITAL LETTER ALPHA WITH DASIA AND VARIA\n\u1f0b = with-accent-breathing grave rough\n\u1f0c : Token \u03b1\u2032 upper -- U+1F0C \u1f0c GREEK CAPITAL LETTER ALPHA WITH PSILI AND OXIA\n\u1f0c = with-accent-breathing acute smooth\n\u1f0d : Token \u03b1\u2032 upper -- U+1F0D \u1f0d GREEK CAPITAL LETTER ALPHA WITH DASIA AND OXIA\n\u1f0d = with-accent-breathing acute rough\n\u1f0e : Token \u03b1\u2032 upper -- U+1F0E \u1f0e GREEK CAPITAL LETTER ALPHA WITH PSILI AND PERISPOMENI\n\u1f0e = with-accent-breathing circumflex smooth\n\u1f0f : Token \u03b1\u2032 upper -- U+1F0F \u1f0f GREEK CAPITAL LETTER ALPHA WITH DASIA AND PERISPOMENI\n\u1f0f = with-accent-breathing circumflex rough\n\u1f10 : Token \u03b5\u2032 lower -- U+1F10 \u1f10 GREEK SMALL LETTER EPSILON WITH PSILI\n\u1f10 = with-breathing smooth\n\u1f11 : Token \u03b5\u2032 lower -- U+1F11 \u1f11 GREEK SMALL LETTER EPSILON WITH DASIA\n\u1f11 = with-breathing rough\n\u1f12 : Token \u03b5\u2032 lower -- U+1F12 \u1f12 GREEK SMALL LETTER EPSILON WITH PSILI AND VARIA\n\u1f12 = with-accent-breathing grave smooth\n\u1f13 : Token \u03b5\u2032 lower -- U+1F13 \u1f13 GREEK SMALL LETTER EPSILON WITH DASIA AND VARIA\n\u1f13 = with-accent-breathing grave rough\n\u1f14 : Token \u03b5\u2032 lower -- U+1F14 \u1f14 GREEK SMALL LETTER EPSILON WITH PSILI AND OXIA\n\u1f14 = with-accent-breathing acute smooth\n\u1f15 : Token \u03b5\u2032 lower -- U+1F15 \u1f15 GREEK SMALL LETTER EPSILON WITH DASIA AND OXIA\n\u1f15 = with-accent-breathing acute rough\n-- U+1F16\n-- U+1F17\n\u1f18 : Token \u03b5\u2032 upper -- U+1F18 \u1f18 GREEK CAPITAL LETTER EPSILON WITH PSILI\n\u1f18 = with-breathing smooth\n\u1f19 : Token \u03b5\u2032 upper -- U+1F19 \u1f19 GREEK CAPITAL LETTER EPSILON WITH DASIA\n\u1f19 = with-breathing rough\n\u1f1a : Token \u03b5\u2032 upper -- U+1F1A \u1f1a GREEK CAPITAL LETTER EPSILON WITH PSILI AND VARIA\n\u1f1a = with-accent-breathing grave smooth\n\u1f1b : Token \u03b5\u2032 upper -- U+1F1B \u1f1b GREEK CAPITAL LETTER EPSILON WITH DASIA AND VARIA\n\u1f1b = with-accent-breathing grave rough\n\u1f1c : Token \u03b5\u2032 upper -- U+1F1C \u1f1c GREEK CAPITAL LETTER EPSILON WITH PSILI AND OXIA\n\u1f1c = with-accent-breathing acute smooth\n\u1f1d : Token \u03b5\u2032 upper -- U+1F1D \u1f1d GREEK CAPITAL LETTER EPSILON WITH DASIA AND OXIA\n\u1f1d = with-accent-breathing acute rough\n-- U+1F1E\n-- U+1F1F\n\u1f20 : Token \u03b7\u2032 lower -- U+1F20 \u1f20 GREEK SMALL LETTER ETA WITH PSILI\n\u1f20 = with-breathing smooth\n\u1f21 : Token \u03b7\u2032 lower -- U+1F21 \u1f21 GREEK SMALL LETTER ETA WITH DASIA\n\u1f21 = with-breathing rough\n\u1f22 : Token \u03b7\u2032 lower -- U+1F22 \u1f22 GREEK SMALL LETTER ETA WITH PSILI AND VARIA\n\u1f22 = with-accent-breathing grave smooth\n\u1f23 : Token \u03b7\u2032 lower -- U+1F23 \u1f23 GREEK SMALL LETTER ETA WITH DASIA AND VARIA\n\u1f23 = with-accent-breathing grave rough\n\u1f24 : Token \u03b7\u2032 lower -- U+1F24 \u1f24 GREEK SMALL LETTER ETA WITH PSILI AND OXIA\n\u1f24 = with-accent-breathing acute smooth\n\u1f25 : Token \u03b7\u2032 lower -- U+1F25 \u1f25 GREEK SMALL LETTER ETA WITH DASIA AND OXIA\n\u1f25 = with-accent-breathing acute rough\n\u1f26 : Token \u03b7\u2032 lower -- U+1F26 \u1f26 GREEK SMALL LETTER ETA WITH PSILI AND PERISPOMENI\n\u1f26 = with-accent-breathing circumflex smooth\n\u1f27 : Token \u03b7\u2032 lower -- U+1F27 \u1f27 GREEK SMALL LETTER ETA WITH DASIA AND PERISPOMENI\n\u1f27 = with-accent-breathing circumflex rough\n\u1f28 : Token \u03b7\u2032 upper -- U+1F28 \u1f28 GREEK CAPITAL LETTER ETA WITH PSILI\n\u1f28 = with-breathing smooth\n\u1f29 : Token \u03b7\u2032 upper -- U+1F29 \u1f29 GREEK CAPITAL LETTER ETA WITH DASIA\n\u1f29 = with-breathing rough\n\u1f2a : Token \u03b7\u2032 upper -- U+1F2A \u1f2a GREEK CAPITAL LETTER ETA WITH PSILI AND VARIA\n\u1f2a = with-accent-breathing grave smooth\n\u1f2b : Token \u03b7\u2032 upper -- U+1F2B \u1f2b GREEK CAPITAL LETTER ETA WITH DASIA AND VARIA\n\u1f2b = with-accent-breathing grave rough\n\u1f2c : Token \u03b7\u2032 upper -- U+1F2C \u1f2c GREEK CAPITAL LETTER ETA WITH PSILI AND OXIA\n\u1f2c = with-accent-breathing acute smooth\n\u1f2d : Token \u03b7\u2032 upper -- U+1F2D \u1f2d GREEK CAPITAL LETTER ETA WITH DASIA AND OXIA\n\u1f2d = with-accent-breathing acute rough\n\u1f2e : Token \u03b7\u2032 upper -- U+1F2E \u1f2e GREEK CAPITAL LETTER ETA WITH PSILI AND PERISPOMENI\n\u1f2e = with-accent-breathing circumflex smooth\n\u1f2f : Token \u03b7\u2032 upper -- U+1F2F \u1f2f GREEK CAPITAL LETTER ETA WITH DASIA AND PERISPOMENI\n\u1f2f = with-accent-breathing circumflex rough\n\u1f30 : Token \u03b9\u2032 lower -- U+1F30 \u1f30 GREEK SMALL LETTER IOTA WITH PSILI\n\u1f30 = with-breathing smooth\n\u1f31 : Token \u03b9\u2032 lower -- U+1F31 \u1f31 GREEK SMALL LETTER IOTA WITH DASIA\n\u1f31 = with-breathing rough\n\u1f32 : Token \u03b9\u2032 lower -- U+1F32 \u1f32 GREEK SMALL LETTER IOTA WITH PSILI AND VARIA\n\u1f32 = with-accent-breathing grave smooth\n\u1f33 : Token \u03b9\u2032 lower -- U+1F33 \u1f33 GREEK SMALL LETTER IOTA WITH DASIA AND VARIA\n\u1f33 = with-accent-breathing grave rough\n\u1f34 : Token \u03b9\u2032 lower -- U+1F34 \u1f34 GREEK SMALL LETTER IOTA WITH PSILI AND OXIA\n\u1f34 = with-accent-breathing acute smooth\n\u1f35 : Token \u03b9\u2032 lower -- U+1F35 \u1f35 GREEK SMALL LETTER IOTA WITH DASIA AND OXIA\n\u1f35 = with-accent-breathing acute rough\n\u1f36 : Token \u03b9\u2032 lower -- U+1F36 \u1f36 GREEK SMALL LETTER IOTA WITH PSILI AND PERISPOMENI\n\u1f36 = with-accent-breathing circumflex smooth\n\u1f37 : Token \u03b9\u2032 lower -- U+1F37 \u1f37 GREEK SMALL LETTER IOTA WITH DASIA AND PERISPOMENI\n\u1f37 = with-accent-breathing circumflex rough\n\u1f38 : Token \u03b9\u2032 upper -- U+1F38 \u1f38 GREEK CAPITAL LETTER IOTA WITH PSILI\n\u1f38 = with-breathing smooth\n\u1f39 : Token \u03b9\u2032 upper -- U+1F39 \u1f39 GREEK CAPITAL LETTER IOTA WITH DASIA\n\u1f39 = with-breathing rough\n\u1f3a : Token \u03b9\u2032 upper -- U+1F3A \u1f3a GREEK CAPITAL LETTER IOTA WITH PSILI AND VARIA\n\u1f3a = with-accent-breathing grave smooth\n\u1f3b : Token \u03b9\u2032 upper -- U+1F3B \u1f3b GREEK CAPITAL LETTER IOTA WITH DASIA AND VARIA\n\u1f3b = with-accent-breathing grave rough\n\u1f3c : Token \u03b9\u2032 upper -- U+1F3C \u1f3c GREEK CAPITAL LETTER IOTA WITH PSILI AND OXIA\n\u1f3c = with-accent-breathing acute smooth\n\u1f3d : Token \u03b9\u2032 upper -- U+1F3D \u1f3d GREEK CAPITAL LETTER IOTA WITH DASIA AND OXIA\n\u1f3d = with-accent-breathing acute rough\n\u1f3e : Token \u03b9\u2032 upper -- U+1F3E \u1f3e GREEK CAPITAL LETTER IOTA WITH PSILI AND PERISPOMENI\n\u1f3e = with-accent-breathing circumflex smooth\n\u1f3f : Token \u03b9\u2032 upper -- U+1F3F \u1f3f GREEK CAPITAL LETTER IOTA WITH DASIA AND PERISPOMENI\n\u1f3f = with-accent-breathing circumflex rough\n\u1f40 : Token \u03bf\u2032 lower -- U+1F40 \u1f40 GREEK SMALL LETTER OMICRON WITH PSILI\n\u1f40 = with-breathing smooth\n\u1f41 : Token \u03bf\u2032 lower -- U+1F41 \u1f41 GREEK SMALL LETTER OMICRON WITH DASIA\n\u1f41 = with-breathing rough\n\u1f42 : Token \u03bf\u2032 lower -- U+1F42 \u1f42 GREEK SMALL LETTER OMICRON WITH PSILI AND VARIA\n\u1f42 = with-accent-breathing grave smooth\n\u1f43 : Token \u03bf\u2032 lower -- U+1F43 \u1f43 GREEK SMALL LETTER OMICRON WITH DASIA AND VARIA\n\u1f43 = with-accent-breathing grave rough\n\u1f44 : Token \u03bf\u2032 lower -- U+1F44 \u1f44 GREEK SMALL LETTER OMICRON WITH PSILI AND OXIA\n\u1f44 = with-accent-breathing acute smooth\n\u1f45 : Token \u03bf\u2032 lower -- U+1F45 \u1f45 GREEK SMALL LETTER OMICRON WITH DASIA AND OXIA\n\u1f45 = with-accent-breathing acute rough\n-- U+1F46\n-- U+1F47\n\u1f48 : Token \u03bf\u2032 upper -- U+1F48 \u1f48 GREEK CAPITAL LETTER OMICRON WITH PSILI\n\u1f48 = with-breathing smooth\n\u1f49 : Token \u03bf\u2032 upper -- U+1F49 \u1f49 GREEK CAPITAL LETTER OMICRON WITH DASIA\n\u1f49 = with-breathing rough\n\u1f4a : Token \u03bf\u2032 upper -- U+1F4A \u1f4a GREEK CAPITAL LETTER OMICRON WITH PSILI AND VARIA\n\u1f4a = with-accent-breathing grave smooth\n\u1f4b : Token \u03bf\u2032 upper -- U+1F4B \u1f4b GREEK CAPITAL LETTER OMICRON WITH DASIA AND VARIA\n\u1f4b = with-accent-breathing grave rough\n\u1f4c : Token \u03bf\u2032 upper -- U+1F4C \u1f4c GREEK CAPITAL LETTER OMICRON WITH PSILI AND OXIA\n\u1f4c = with-accent-breathing acute smooth\n\u1f4d : Token \u03bf\u2032 upper -- U+1F4D \u1f4d GREEK CAPITAL LETTER OMICRON WITH DASIA AND OXIA\n\u1f4d = with-accent-breathing acute rough\n-- U+1F4E\n-- U+1F4F\n\u1f50 : Token \u03c5\u2032 lower -- U+1F50 \u1f50 GREEK SMALL LETTER UPSILON WITH PSILI\n\u1f50 = with-breathing smooth\n\u1f51 : Token \u03c5\u2032 lower -- U+1F51 \u1f51 GREEK SMALL LETTER UPSILON WITH DASIA\n\u1f51 = with-breathing rough\n\u1f52 : Token \u03c5\u2032 lower -- U+1F52 \u1f52 GREEK SMALL LETTER UPSILON WITH PSILI AND VARIA\n\u1f52 = with-accent-breathing grave smooth\n\u1f53 : Token \u03c5\u2032 lower -- U+1F53 \u1f53 GREEK SMALL LETTER UPSILON WITH DASIA AND VARIA\n\u1f53 = with-accent-breathing grave rough\n\u1f54 : Token \u03c5\u2032 lower -- U+1F54 \u1f54 GREEK SMALL LETTER UPSILON WITH PSILI AND OXIA\n\u1f54 = with-accent-breathing acute smooth\n\u1f55 : Token \u03c5\u2032 lower -- U+1F55 \u1f55 GREEK SMALL LETTER UPSILON WITH DASIA AND OXIA\n\u1f55 = with-accent-breathing acute rough\n\u1f56 : Token \u03c5\u2032 lower -- U+1F56 \u1f56 GREEK SMALL LETTER UPSILON WITH PSILI AND PERISPOMENI\n\u1f56 = with-accent-breathing circumflex smooth\n\u1f57 : Token \u03c5\u2032 lower -- U+1F57 \u1f57 GREEK SMALL LETTER UPSILON WITH DASIA AND PERISPOMENI\n\u1f57 = with-accent-breathing circumflex rough\n-- U+1F58\n\u1f59 : Token \u03c5\u2032 upper -- U+1F59 \u1f59 GREEK CAPITAL LETTER UPSILON WITH DASIA\n\u1f59 = with-breathing rough\n-- U+1F5A\n\u1f5b : Token \u03c5\u2032 upper -- U+1F5B \u1f5b GREEK CAPITAL LETTER UPSILON WITH DASIA AND VARIA\n\u1f5b = with-accent-breathing grave rough\n-- U+1F5C\n\u1f5d : Token \u03c5\u2032 upper -- U+1F5D \u1f5d GREEK CAPITAL LETTER UPSILON WITH DASIA AND OXIA\n\u1f5d = with-accent-breathing acute rough\n-- U+1F5E\n\u1f5f : Token \u03c5\u2032 upper -- U+1F5F \u1f5f GREEK CAPITAL LETTER UPSILON WITH DASIA AND PERISPOMENI\n\u1f5f = with-accent-breathing circumflex rough\n\u1f60 : Token \u03c9\u2032 lower -- U+1F60 \u1f60 GREEK SMALL LETTER OMEGA WITH PSILI\n\u1f60 = with-breathing smooth\n\u1f61 : Token \u03c9\u2032 lower -- U+1F61 \u1f61 GREEK SMALL LETTER OMEGA WITH DASIA\n\u1f61 = with-breathing rough\n\u1f62 : Token \u03c9\u2032 lower -- U+1F62 \u1f62 GREEK SMALL LETTER OMEGA WITH PSILI AND VARIA\n\u1f62 = with-accent-breathing grave smooth\n\u1f63 : Token \u03c9\u2032 lower -- U+1F63 \u1f63 GREEK SMALL LETTER OMEGA WITH DASIA AND VARIA\n\u1f63 = with-accent-breathing grave rough\n\u1f64 : Token \u03c9\u2032 lower -- U+1F64 \u1f64 GREEK SMALL LETTER OMEGA WITH PSILI AND OXIA\n\u1f64 = with-accent-breathing acute smooth\n\u1f65 : Token \u03c9\u2032 lower -- U+1F65 \u1f65 GREEK SMALL LETTER OMEGA WITH DASIA AND OXIA\n\u1f65 = with-accent-breathing acute rough\n\u1f66 : Token \u03c9\u2032 lower -- U+1F66 \u1f66 GREEK SMALL LETTER OMEGA WITH PSILI AND PERISPOMENI\n\u1f66 = with-accent-breathing circumflex smooth\n\u1f67 : Token \u03c9\u2032 lower -- U+1F67 \u1f67 GREEK SMALL LETTER OMEGA WITH DASIA AND PERISPOMENI\n\u1f67 = with-accent-breathing circumflex rough\n\u1f68 : Token \u03c9\u2032 upper -- U+1F68 \u1f68 GREEK CAPITAL LETTER OMEGA WITH PSILI\n\u1f68 = with-breathing smooth\n\u1f69 : Token \u03c9\u2032 upper -- U+1F69 \u1f69 GREEK CAPITAL LETTER OMEGA WITH DASIA\n\u1f69 = with-breathing rough\n\u1f6a : Token \u03c9\u2032 upper -- U+1F6A \u1f6a GREEK CAPITAL LETTER OMEGA WITH PSILI AND VARIA\n\u1f6a = with-accent-breathing grave smooth\n\u1f6b : Token \u03c9\u2032 upper -- U+1F6B \u1f6b GREEK CAPITAL LETTER OMEGA WITH DASIA AND VARIA\n\u1f6b = with-accent-breathing grave rough\n\u1f6c : Token \u03c9\u2032 upper -- U+1F6C \u1f6c GREEK CAPITAL LETTER OMEGA WITH PSILI AND OXIA\n\u1f6c = with-accent-breathing acute smooth\n\u1f6d : Token \u03c9\u2032 upper -- U+1F6D \u1f6d GREEK CAPITAL LETTER OMEGA WITH DASIA AND OXIA\n\u1f6d = with-accent-breathing acute rough\n\u1f6e : Token \u03c9\u2032 upper -- U+1F6E \u1f6e GREEK CAPITAL LETTER OMEGA WITH PSILI AND PERISPOMENI\n\u1f6e = with-accent-breathing circumflex smooth\n\u1f6f : Token \u03c9\u2032 upper -- U+1F6F \u1f6f GREEK CAPITAL LETTER OMEGA WITH DASIA AND PERISPOMENI\n\u1f6f = with-accent-breathing circumflex rough\n\u1f70 : Token \u03b1\u2032 lower -- U+1F70 \u1f70 GREEK SMALL LETTER ALPHA WITH VARIA\n\u1f70 = with-accent grave\n\u1f71 : Token \u03b1\u2032 lower -- U+1F71 \u1f71 GREEK SMALL LETTER ALPHA WITH OXIA\n\u1f71 = with-accent acute\n\u1f72 : Token \u03b5\u2032 lower -- U+1F72 \u1f72 GREEK SMALL LETTER EPSILON WITH VARIA\n\u1f72 = with-accent grave\n\u1f73 : Token \u03b5\u2032 lower -- U+1F73 \u1f73 GREEK SMALL LETTER EPSILON WITH OXIA\n\u1f73 = with-accent acute\n\u1f74 : Token \u03b7\u2032 lower -- U+1F74 \u1f74 GREEK SMALL LETTER ETA WITH VARIA\n\u1f74 = with-accent grave\n\u1f75 : Token \u03b7\u2032 lower -- U+1F75 \u1f75 GREEK SMALL LETTER ETA WITH OXIA\n\u1f75 = with-accent acute\n\u1f76 : Token \u03b9\u2032 lower -- U+1F76 \u1f76 GREEK SMALL LETTER IOTA WITH VARIA\n\u1f76 = with-accent grave\n\u1f77 : Token \u03b9\u2032 lower -- U+1F77 \u1f77 GREEK SMALL LETTER IOTA WITH OXIA\n\u1f77 = with-accent acute\n\u1f78 : Token \u03bf\u2032 lower -- U+1F78 \u1f78 GREEK SMALL LETTER OMICRON WITH VARIA\n\u1f78 = with-accent grave\n\u1f79 : Token \u03bf\u2032 lower -- U+1F79 \u1f79 GREEK SMALL LETTER OMICRON WITH OXIA\n\u1f79 = with-accent acute\n\u1f7a : Token \u03c5\u2032 lower -- U+1F7A \u1f7a GREEK SMALL LETTER UPSILON WITH VARIA\n\u1f7a = with-accent grave\n\u1f7b : Token \u03c5\u2032 lower -- U+1F7B \u1f7b GREEK SMALL LETTER UPSILON WITH OXIA\n\u1f7b = with-accent acute\n\u1f7c : Token \u03c9\u2032 lower -- U+1F7C \u1f7c GREEK SMALL LETTER OMEGA WITH VARIA\n\u1f7c = with-accent grave\n\u1f7d : Token \u03c9\u2032 lower -- U+1F7D \u1f7d GREEK SMALL LETTER OMEGA WITH OXIA\n\u1f7d = with-accent acute\n-- U+1F7E\n-- U+1F7F\n\u1f80 : Token \u03b1\u2032 lower -- U+1F80 \u1f80 GREEK SMALL LETTER ALPHA WITH PSILI AND YPOGEGRAMMENI\n\u1f80 = with-breathing-iota smooth\n\u1f81 : Token \u03b1\u2032 lower -- U+1F81 \u1f81 GREEK SMALL LETTER ALPHA WITH DASIA AND YPOGEGRAMMENI\n\u1f81 = with-breathing-iota rough\n\u1f82 : Token \u03b1\u2032 lower -- U+1F82 \u1f82 GREEK SMALL LETTER ALPHA WITH PSILI AND VARIA AND YPOGEGRAMMENI\n\u1f82 = with-accent-breathing-iota grave smooth\n\u1f83 : Token \u03b1\u2032 lower -- U+1F83 \u1f83 GREEK SMALL LETTER ALPHA WITH DASIA AND VARIA AND YPOGEGRAMMENI\n\u1f83 = with-accent-breathing grave rough\n\u1f84 : Token \u03b1\u2032 lower -- U+1F84 \u1f84 GREEK SMALL LETTER ALPHA WITH PSILI AND OXIA AND YPOGEGRAMMENI\n\u1f84 = with-accent-breathing-iota acute smooth\n\u1f85 : Token \u03b1\u2032 lower -- U+1F85 \u1f85 GREEK SMALL LETTER ALPHA WITH DASIA AND OXIA AND YPOGEGRAMMENI\n\u1f85 = with-accent-breathing-iota acute rough\n\u1f86 : Token \u03b1\u2032 lower -- U+1F86 \u1f86 GREEK SMALL LETTER ALPHA WITH PSILI AND PERISPOMENI AND YPOGEGRAMMENI\n\u1f86 = with-accent-breathing-iota circumflex smooth\n\u1f87 : Token \u03b1\u2032 lower -- U+1F87 \u1f87 GREEK SMALL LETTER ALPHA WITH DASIA AND PERISPOMENI AND YPOGEGRAMMENI\n\u1f87 = with-accent-breathing-iota circumflex rough\n\u1f88 : Token \u03b1\u2032 upper -- U+1F88 \u1f88 GREEK CAPITAL LETTER ALPHA WITH PSILI AND PROSGEGRAMMENI\n\u1f88 = with-breathing-iota smooth\n\u1f89 : Token \u03b1\u2032 upper -- U+1F89 \u1f89 GREEK CAPITAL LETTER ALPHA WITH DASIA AND PROSGEGRAMMENI\n\u1f89 = with-breathing-iota rough\n\u1f8a : Token \u03b1\u2032 upper -- U+1F8A \u1f8a GREEK CAPITAL LETTER ALPHA WITH PSILI AND VARIA AND PROSGEGRAMMENI\n\u1f8a = with-accent-breathing-iota grave smooth\n\u1f8b : Token \u03b1\u2032 upper -- U+1F8B \u1f8b GREEK CAPITAL LETTER ALPHA WITH DASIA AND VARIA AND PROSGEGRAMMENI\n\u1f8b = with-accent-breathing-iota grave rough\n\u1f8c : Token \u03b1\u2032 upper -- U+1F8C \u1f8c GREEK CAPITAL LETTER ALPHA WITH PSILI AND OXIA AND PROSGEGRAMMENI\n\u1f8c = with-accent-breathing-iota acute smooth\n\u1f8d : Token \u03b1\u2032 upper -- U+1F8D \u1f8d GREEK CAPITAL LETTER ALPHA WITH DASIA AND OXIA AND PROSGEGRAMMENI\n\u1f8d = with-accent-breathing-iota acute rough\n\u1f8e : Token \u03b1\u2032 upper -- U+1F8E \u1f8e GREEK CAPITAL LETTER ALPHA WITH PSILI AND PERISPOMENI AND PROSGEGRAMMENI\n\u1f8e = with-accent-breathing-iota circumflex smooth\n\u1f8f : Token \u03b1\u2032 upper -- U+1F8F \u1f8f GREEK CAPITAL LETTER ALPHA WITH DASIA AND PERISPOMENI AND PROSGEGRAMMENI\n\u1f8f = with-accent-breathing-iota circumflex rough\n\u1f90 : Token \u03b7\u2032 lower -- U+1F90 \u1f90 GREEK SMALL LETTER ETA WITH PSILI AND YPOGEGRAMMENI\n\u1f90 = with-breathing-iota smooth\n\u1f91 : Token \u03b7\u2032 lower -- U+1F91 \u1f91 GREEK SMALL LETTER ETA WITH DASIA AND YPOGEGRAMMENI\n\u1f91 = with-breathing-iota rough\n\u1f92 : Token \u03b7\u2032 lower -- U+1F92 \u1f92 GREEK SMALL LETTER ETA WITH PSILI AND VARIA AND YPOGEGRAMMENI\n\u1f92 = with-accent-breathing-iota grave smooth\n\u1f93 : Token \u03b7\u2032 lower -- U+1F93 \u1f93 GREEK SMALL LETTER ETA WITH DASIA AND VARIA AND YPOGEGRAMMENI\n\u1f93 = with-accent-breathing-iota grave rough\n\u1f94 : Token \u03b7\u2032 lower -- U+1F94 \u1f94 GREEK SMALL LETTER ETA WITH PSILI AND OXIA AND YPOGEGRAMMENI\n\u1f94 = with-accent-breathing-iota acute smooth\n\u1f95 : Token \u03b7\u2032 lower -- U+1F95 \u1f95 GREEK SMALL LETTER ETA WITH DASIA AND OXIA AND YPOGEGRAMMENI\n\u1f95 = with-accent-breathing-iota acute rough\n\u1f96 : Token \u03b7\u2032 lower -- U+1F96 \u1f96 GREEK SMALL LETTER ETA WITH PSILI AND PERISPOMENI AND YPOGEGRAMMENI\n\u1f96 = with-accent-breathing-iota circumflex smooth\n\u1f97 : Token \u03b7\u2032 lower -- U+1F97 \u1f97 GREEK SMALL LETTER ETA WITH DASIA AND PERISPOMENI AND YPOGEGRAMMENI\n\u1f97 = with-accent-breathing-iota circumflex rough\n\u1f98 : Token \u03b7\u2032 upper -- U+1F98 \u1f98 GREEK CAPITAL LETTER ETA WITH PSILI AND PROSGEGRAMMENI\n\u1f98 = with-breathing-iota smooth\n\u1f99 : Token \u03b7\u2032 upper -- U+1F99 \u1f99 GREEK CAPITAL LETTER ETA WITH DASIA AND PROSGEGRAMMENI\n\u1f99 = with-breathing-iota rough\n\u1f9a : Token \u03b7\u2032 upper -- U+1F9A \u1f9a GREEK CAPITAL LETTER ETA WITH PSILI AND VARIA AND PROSGEGRAMMENI\n\u1f9a = with-accent-breathing-iota grave smooth\n\u1f9b : Token \u03b7\u2032 upper -- U+1F9B \u1f9b GREEK CAPITAL LETTER ETA WITH DASIA AND VARIA AND PROSGEGRAMMENI\n\u1f9b = with-accent-breathing-iota grave rough\n\u1f9c : Token \u03b7\u2032 upper -- U+1F9C \u1f9c GREEK CAPITAL LETTER ETA WITH PSILI AND OXIA AND PROSGEGRAMMENI\n\u1f9c = with-accent-breathing-iota acute smooth\n\u1f9d : Token \u03b7\u2032 upper -- U+1F9D \u1f9d GREEK CAPITAL LETTER ETA WITH DASIA AND OXIA AND PROSGEGRAMMENI\n\u1f9d = with-accent-breathing-iota acute rough\n\u1f9e : Token \u03b7\u2032 upper -- U+1F9E \u1f9e GREEK CAPITAL LETTER ETA WITH PSILI AND PERISPOMENI AND PROSGEGRAMMENI\n\u1f9e = with-accent-breathing-iota circumflex smooth\n\u1f9f : Token \u03b7\u2032 upper -- U+1F9F \u1f9f GREEK CAPITAL LETTER ETA WITH DASIA AND PERISPOMENI AND PROSGEGRAMMENI\n\u1f9f = with-accent-breathing-iota circumflex rough\n\u1fa0 : Token \u03c9\u2032 lower -- U+1FA0 \u1fa0 GREEK SMALL LETTER OMEGA WITH PSILI AND YPOGEGRAMMENI\n\u1fa0 = with-breathing-iota smooth\n\u1fa1 : Token \u03c9\u2032 lower -- U+1FA1 \u1fa1 GREEK SMALL LETTER OMEGA WITH DASIA AND YPOGEGRAMMENI\n\u1fa1 = with-breathing-iota rough\n\u1fa2 : Token \u03c9\u2032 lower -- U+1FA2 \u1fa2 GREEK SMALL LETTER OMEGA WITH PSILI AND VARIA AND YPOGEGRAMMENI\n\u1fa2 = with-accent-breathing-iota grave smooth\n\u1fa3 : Token \u03c9\u2032 lower -- U+1FA3 \u1fa3 GREEK SMALL LETTER OMEGA WITH DASIA AND VARIA AND YPOGEGRAMMENI\n\u1fa3 = with-accent-breathing-iota grave rough\n\u1fa4 : Token \u03c9\u2032 lower -- U+1FA4 \u1fa4 GREEK SMALL LETTER OMEGA WITH PSILI AND OXIA AND YPOGEGRAMMENI\n\u1fa4 = with-accent-breathing-iota acute smooth\n\u1fa5 : Token \u03c9\u2032 lower -- U+1FA5 \u1fa5 GREEK SMALL LETTER OMEGA WITH DASIA AND OXIA AND YPOGEGRAMMENI\n\u1fa5 = with-accent-breathing-iota acute rough\n\u1fa6 : Token \u03c9\u2032 lower -- U+1FA6 \u1fa6 GREEK SMALL LETTER OMEGA WITH PSILI AND PERISPOMENI AND YPOGEGRAMMENI\n\u1fa6 = with-accent-breathing-iota circumflex smooth\n\u1fa7 : Token \u03c9\u2032 lower -- U+1FA7 \u1fa7 GREEK SMALL LETTER OMEGA WITH DASIA AND PERISPOMENI AND YPOGEGRAMMENI\n\u1fa7 = with-accent-breathing-iota circumflex rough\n\u1fa8 : Token \u03c9\u2032 upper -- U+1FA8 \u1fa8 GREEK CAPITAL LETTER OMEGA WITH PSILI AND PROSGEGRAMMENI\n\u1fa8 = with-breathing-iota smooth\n\u1fa9 : Token \u03c9\u2032 upper -- U+1FA9 \u1fa9 GREEK CAPITAL LETTER OMEGA WITH DASIA AND PROSGEGRAMMENI\n\u1fa9 = with-breathing-iota rough\n\u1faa : Token \u03c9\u2032 upper -- U+1FAA \u1faa GREEK CAPITAL LETTER OMEGA WITH PSILI AND VARIA AND PROSGEGRAMMENI\n\u1faa = with-accent-breathing-iota grave smooth\n\u1fab : Token \u03c9\u2032 upper -- U+1FAB \u1fab GREEK CAPITAL LETTER OMEGA WITH DASIA AND VARIA AND PROSGEGRAMMENI\n\u1fab = with-accent-breathing-iota grave rough\n\u1fac : Token \u03c9\u2032 upper -- U+1FAC \u1fac GREEK CAPITAL LETTER OMEGA WITH PSILI AND OXIA AND PROSGEGRAMMENI\n\u1fac = with-accent-breathing-iota acute smooth\n\u1fad : Token \u03c9\u2032 upper -- U+1FAD \u1fad GREEK CAPITAL LETTER OMEGA WITH DASIA AND OXIA AND PROSGEGRAMMENI\n\u1fad = with-accent-breathing-iota acute rough\n\u1fae : Token \u03c9\u2032 upper -- U+1FAE \u1fae GREEK CAPITAL LETTER OMEGA WITH PSILI AND PERISPOMENI AND PROSGEGRAMMENI\n\u1fae = with-accent-breathing-iota circumflex smooth\n\u1faf : Token \u03c9\u2032 upper -- U+1FAF \u1faf GREEK CAPITAL LETTER OMEGA WITH DASIA AND PERISPOMENI AND PROSGEGRAMMENI\n\u1faf = with-accent-breathing-iota circumflex rough\n-- U+1FB0 \u1fb0 GREEK SMALL LETTER ALPHA WITH VRACHY\n-- U+1FB1 \u1fb1 GREEK SMALL LETTER ALPHA WITH MACRON\n\u1fb2 : Token \u03b1\u2032 lower -- U+1FB2 \u1fb2 GREEK SMALL LETTER ALPHA WITH VARIA AND YPOGEGRAMMENI\n\u1fb2 = with-accent-iota grave\n\u1fb3 : Token \u03b1\u2032 lower -- U+1FB3 \u1fb3 GREEK SMALL LETTER ALPHA WITH YPOGEGRAMMENI\n\u1fb3 = with-iota\n\u1fb4 : Token \u03b1\u2032 lower -- U+1FB4 \u1fb4 GREEK SMALL LETTER ALPHA WITH OXIA AND YPOGEGRAMMENI\n\u1fb4 = with-accent-iota acute\n-- U+1FB5\n\u1fb6 : Token \u03b1\u2032 lower -- U+1FB6 \u1fb6 GREEK SMALL LETTER ALPHA WITH PERISPOMENI\n\u1fb6 = with-accent-iota circumflex\n\u1fb7 : Token \u03b1\u2032 lower -- U+1FB7 \u1fb7 GREEK SMALL LETTER ALPHA WITH PERISPOMENI AND YPOGEGRAMMENI\n\u1fb7 = with-accent-iota circumflex\n-- U+1FB8 \u1fb8 GREEK CAPITAL LETTER ALPHA WITH VRACHY\n-- U+1FB9 \u1fb9 GREEK CAPITAL LETTER ALPHA WITH MACRON\n\u1fba : Token \u03b1\u2032 upper -- U+1FBA \u1fba GREEK CAPITAL LETTER ALPHA WITH VARIA\n\u1fba = with-accent grave\n\u1fbb : Token \u03b1\u2032 upper -- U+1FBB \u1fbb GREEK CAPITAL LETTER ALPHA WITH OXIA\n\u1fbb = with-accent acute\n\u1fbc : Token \u03b1\u2032 upper -- U+1FBC \u1fbc GREEK CAPITAL LETTER ALPHA WITH PROSGEGRAMMENI\n\u1fbc = with-iota\n-- U+1FBD \u1fbd GREEK KORONIS\n-- U+1FBE \u1fbe GREEK PROSGEGRAMMENI\n-- U+1FBF \u1fbf GREEK PSILI\n-- U+1FC0 \u1fc0 GREEK PERISPOMENI\n-- U+1FC1 \u1fc1 GREEK DIALYTIKA AND PERISPOMENI\n\u1fc2 : Token \u03b7\u2032 lower -- U+1FC2 \u1fc2 GREEK SMALL LETTER ETA WITH VARIA AND YPOGEGRAMMENI accent circumflex\n\u1fc2 = with-accent-iota grave\n\u1fc3 : Token \u03b7\u2032 lower -- U+1FC3 \u1fc3 GREEK SMALL LETTER ETA WITH YPOGEGRAMMENI\n\u1fc3 = with-iota\n\u1fc4 : Token \u03b7\u2032 lower -- U+1FC4 \u1fc4 GREEK SMALL LETTER ETA WITH OXIA AND YPOGEGRAMMENI\n\u1fc4 = with-accent-iota acute\n-- U+1FC5\n\u1fc6 : Token \u03b7\u2032 lower -- U+1FC6 \u1fc6 GREEK SMALL LETTER ETA WITH PERISPOMENI\n\u1fc6 = with-accent circumflex\n\u1fc7 : Token \u03b7\u2032 lower -- U+1FC7 \u1fc7 GREEK SMALL LETTER ETA WITH PERISPOMENI AND YPOGEGRAMMENI\n\u1fc7 = with-accent-iota circumflex\n\u1fc8 : Token \u03b5\u2032 upper -- U+1FC8 \u1fc8 GREEK CAPITAL LETTER EPSILON WITH VARIA\n\u1fc8 = with-accent grave\n\u1fc9 : Token \u03b5\u2032 upper -- U+1FC9 \u1fc9 GREEK CAPITAL LETTER EPSILON WITH OXIA\n\u1fc9 = with-accent acute\n\u1fca : Token \u03b7\u2032 upper -- U+1FCA \u1fca GREEK CAPITAL LETTER ETA WITH VARIA\n\u1fca = with-accent grave\n\u1fcb : Token \u03b7\u2032 upper -- U+1FCB \u1fcb GREEK CAPITAL LETTER ETA WITH OXIA\n\u1fcb = with-accent acute\n\u1fcc : Token \u03b7\u2032 upper -- U+1FCC \u1fcc GREEK CAPITAL LETTER ETA WITH PROSGEGRAMMENI\n\u1fcc = with-iota\n-- U+1FCD \u1fcd GREEK PSILI AND VARIA\n-- U+1FCE \u1fce GREEK PSILI AND OXIA\n-- U+1FCF \u1fcf GREEK PSILI AND PERISPOMENI\n-- U+1FD0 \u1fd0 GREEK SMALL LETTER IOTA WITH VRACHY\n-- U+1FD1 \u1fd1 GREEK SMALL LETTER IOTA WITH MACRON\n\u1fd2 : Token \u03b9\u2032 lower -- U+1FD2 \u1fd2 GREEK SMALL LETTER IOTA WITH DIALYTIKA AND VARIA\n\u1fd2 = with-accent-diaeresis grave\n\u1fd3 : Token \u03b9\u2032 lower -- U+1FD3 \u1fd3 GREEK SMALL LETTER IOTA WITH DIALYTIKA AND OXIA\n\u1fd3 = with-accent-diaeresis acute\n-- U+1FD4\n-- U+1FD5\n\u1fd6 : Token \u03b9\u2032 lower -- U+1FD6 \u1fd6 GREEK SMALL LETTER IOTA WITH PERISPOMENI\n\u1fd6 = with-accent circumflex\n\u1fd7 : Token \u03b9\u2032 lower -- U+1FD7 \u1fd7 GREEK SMALL LETTER IOTA WITH DIALYTIKA AND PERISPOMENI\n\u1fd7 = with-accent-diaeresis circumflex\n-- U+1FD8 \u1fd8 GREEK CAPITAL LETTER IOTA WITH VRACHY\n-- U+1FD9 \u1fd9 GREEK CAPITAL LETTER IOTA WITH MACRON\n\u1fda : Token \u03b9\u2032 upper -- U+1FDA \u1fda GREEK CAPITAL LETTER IOTA WITH VARIA\n\u1fda = with-accent grave\n\u1fdb : Token \u03b9\u2032 upper -- U+1FDB \u1fdb GREEK CAPITAL LETTER IOTA WITH OXIA\n\u1fdb = with-accent acute\n-- U+1FDC\n-- U+1FDD \u1fdd GREEK DASIA AND VARIA\n-- U+1FDE \u1fde GREEK DASIA AND OXIA\n-- U+1FDF \u1fdf GREEK DASIA AND PERISPOMENI\n-- U+1FE0 \u1fe0 GREEK SMALL LETTER UPSILON WITH VRACHY\n-- U+1FE1 \u1fe1 GREEK SMALL LETTER UPSILON WITH MACRON\n\u1fe2 : Token \u03c5\u2032 lower -- U+1FE2 \u1fe2 GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND VARIA\n\u1fe2 = with-accent-diaeresis grave\n\u1fe3 : Token \u03c5\u2032 lower -- U+1FE3 \u1fe3 GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND OXIA\n\u1fe3 = with-accent-diaeresis acute\n\u1fe4 : Token \u03c1\u2032 lower -- U+1FE4 \u1fe4 GREEK SMALL LETTER RHO WITH PSILI\n\u1fe4 = with-breathing smooth\n\u1fe5 : Token \u03c1\u2032 lower -- U+1FE5 \u1fe5 GREEK SMALL LETTER RHO WITH DASIA\n\u1fe5 = with-breathing rough\n\u1fe6 : Token \u03c5\u2032 lower -- U+1FE6 \u1fe6 GREEK SMALL LETTER UPSILON WITH PERISPOMENI\n\u1fe6 = with-accent circumflex\n\u1fe7 : Token \u03c5\u2032 lower -- U+1FE7 \u1fe7 GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND PERISPOMENI\n\u1fe7 = with-accent-diaeresis circumflex\n-- U+1FE8 \u1fe8 GREEK CAPITAL LETTER UPSILON WITH VRACHY\n-- U+1FE9 \u1fe9 GREEK CAPITAL LETTER UPSILON WITH MACRON\n\u1fea : Token \u03c5\u2032 upper -- U+1FEA \u1fea GREEK CAPITAL LETTER UPSILON WITH VARIA\n\u1fea = with-accent grave\n\u1feb : Token \u03c5\u2032 upper -- U+1FEB \u1feb GREEK CAPITAL LETTER UPSILON WITH OXIA\n\u1feb = with-accent acute\n\u1fec : Token \u03c1\u2032 upper -- U+1FEC \u1fec GREEK CAPITAL LETTER RHO WITH DASIA\n\u1fec = with-breathing rough\n-- U+1FED \u1fed GREEK DIALYTIKA AND VARIA\n-- U+1FEE \u1fee GREEK DIALYTIKA AND OXIA\n-- U+1FEF \u1fef GREEK VARIA\n-- U+1FF0\n-- U+1FF1\n\u1ff2 : Token \u03c9\u2032 lower -- U+1FF2 \u1ff2 GREEK SMALL LETTER OMEGA WITH VARIA AND YPOGEGRAMMENI\n\u1ff2 = with-accent-iota grave\n\u1ff3 : Token \u03c9\u2032 lower -- U+1FF3 \u1ff3 GREEK SMALL LETTER OMEGA WITH YPOGEGRAMMENI\n\u1ff3 = with-iota\n\u1ff4 : Token \u03c9\u2032 lower -- U+1FF4 \u1ff4 GREEK SMALL LETTER OMEGA WITH OXIA AND YPOGEGRAMMENI\n\u1ff4 = with-accent-iota acute\n-- U+1FF5\n\u1ff6 : Token \u03c9\u2032 lower -- U+1FF6 \u1ff6 GREEK SMALL LETTER OMEGA WITH PERISPOMENI\n\u1ff6 = with-accent circumflex\n\u1ff7 : Token \u03c9\u2032 lower -- U+1FF7 \u1ff7 GREEK SMALL LETTER OMEGA WITH PERISPOMENI AND YPOGEGRAMMENI\n\u1ff7 = with-accent-iota circumflex\n\u1ff8 : Token \u03bf\u2032 upper -- U+1FF8 \u1ff8 GREEK CAPITAL LETTER OMICRON WITH VARIA\n\u1ff8 = with-accent grave\n\u1ff9 : Token \u03bf\u2032 upper -- U+1FF9 \u1ff9 GREEK CAPITAL LETTER OMICRON WITH OXIA\n\u1ff9 = with-accent acute\n\u1ffa : Token \u03c9\u2032 upper -- U+1FFA \u1ffa GREEK CAPITAL LETTER OMEGA WITH VARIA\n\u1ffa = with-accent grave\n\u1ffb : Token \u03c9\u2032 upper -- U+1FFB \u1ffb GREEK CAPITAL LETTER OMEGA WITH OXIA\n\u1ffb = with-accent acute\n\u1ffc : Token \u03c9\u2032 upper -- U+1FFC \u1ffc GREEK CAPITAL LETTER OMEGA WITH PROSGEGRAMMENI\n\u1ffc = with-iota\n-- U+1FFD \u1ffd GREEK OXIA\n-- U+1FFE \u1ffe GREEK DASIA\n-- U+1FFF\n\n-- Mapping\n\ndata Accent : Set where\n acute-mark grave-mark circumflex-mark : Accent\n\nletter-to-accent : \u2200 {v} \u2192 v accent \u2192 Accent\nletter-to-accent acute = acute-mark\nletter-to-accent grave = grave-mark\nletter-to-accent circumflex = circumflex-mark\n\nget-accent : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Accent\nget-accent (with-accent a) = just (letter-to-accent a)\nget-accent (with-accent-breathing a _) = just (letter-to-accent a)\nget-accent (with-accent-breathing-iota a _) = just (letter-to-accent a)\nget-accent (with-accent-diaeresis a) = just (letter-to-accent a)\nget-accent (with-accent-iota a) = just (letter-to-accent a)\nget-accent _ = nothing\n\ndata Breathing : Set where\n smooth-mark rough-mark : Breathing\n\nletter-to-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Breathing\nletter-to-breathing smooth = smooth-mark\nletter-to-breathing rough = rough-mark\n\nget-breathing : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Breathing\nget-breathing (with-breathing x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing _ x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing-iota _ x) = just (letter-to-breathing x)\nget-breathing (with-breathing-iota x) = just (letter-to-breathing x)\nget-breathing _ = nothing\n\ndata IotaSubscript : Set where\n iota-subscript-mark : IotaSubscript\n\nget-iota-subscript : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe IotaSubscript\nget-iota-subscript (with-accent-breathing-iota _ _) = just iota-subscript-mark\nget-iota-subscript (with-accent-iota _) = just iota-subscript-mark\nget-iota-subscript (with-breathing-iota _) = just iota-subscript-mark\nget-iota-subscript _ = nothing\n\ndata Diaeresis : Set where\n diaeresis-mark : Diaeresis\n\nget-diaeresis : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Diaeresis\nget-diaeresis with-diaeresis = just diaeresis-mark\nget-diaeresis (with-accent-diaeresis _) = just diaeresis-mark\nget-diaeresis _ = nothing\n\ndata FinalForm : Set where\n final-form : FinalForm\n\nget-final-form : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe FinalForm\nget-final-form final = just final-form\nget-final-form _ = nothing\n","old_contents":"module Text.Greek.Script where\n\nopen import Data.Maybe\nopen import Data.Vec\nopen import Relation.Nullary using (\u00ac_)\nopen import Relation.Binary.PropositionalEquality using (_\u2262_)\n\ndata Case : Set where\n lower upper : Case\n\ndata Letter : Set where\n \u03b1\u2032 \u03b2\u2032 \u03b3\u2032 \u03b4\u2032 \u03b5\u2032 \u03b6\u2032 \u03b7\u2032 \u03b8\u2032 \u03b9\u2032 \u03ba\u2032 \u03bb\u2032 \u03bc\u2032 \u03bd\u2032 \u03be\u2032 \u03bf\u2032 \u03c0\u2032 \u03c1\u2032 \u03c3\u2032 \u03c4\u2032 \u03c5\u2032 \u03c6\u2032 \u03c7\u2032 \u03c8\u2032 \u03c9\u2032 : Letter\n\nvowels : Vec _ _\nvowels = \u03b1\u2032 \u2237 \u03b5\u2032 \u2237 \u03b7\u2032 \u2237 \u03b9\u2032 \u2237 \u03bf\u2032 \u2237 \u03c5\u2032 \u2237 \u03c9\u2032 \u2237 []\n\nalways-short-letters : Vec _ _\nalways-short-letters = \u03b5\u2032 \u2237 \u03bf\u2032 \u2237 []\n\ndiaeresis-letters : Vec _ _\ndiaeresis-letters = \u03b9\u2032 \u2237 \u03c5\u2032 \u2237 []\n\niota-subscript-letters : Vec _ _\niota-subscript-letters = \u03b1\u2032 \u2237 \u03b7\u2032 \u2237 \u03c9\u2032 \u2237 []\n\ndata _vowel : Letter \u2192 Set where\n is-vowel : \u2200 {v} \u2192 v \u2208 vowels \u2192 v vowel\n\ndata _always-short : Letter \u2192 Set where\n is-always-short : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 always-short-letters \u2192 v always-short\n\ndata _diaeresis : Letter \u2192 Set where\n add-diaeresis : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 diaeresis-letters \u2192 v diaeresis\n\ndata _\u27e6_\u27e7-final : Letter \u2192 Case \u2192 Set where\n make-final : \u03c3\u2032 \u27e6 lower \u27e7-final\n\ndata _\u27e6_\u27e7-smooth : Letter \u2192 Case \u2192 Set where\n add-smooth-lower-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u27e6 lower \u27e7-smooth\n add-smooth-\u03c1 : \u03c1\u2032 \u27e6 lower \u27e7-smooth\n add-smooth-upper-vowel-not-\u03a5 : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2262 \u03c5\u2032 \u2192 v \u27e6 upper \u27e7-smooth\n\ndata _with-rough : Letter \u2192 Set where\n add-rough-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v with-rough\n add-rough-\u03c1 : \u03c1\u2032 with-rough\n\ndata _iota-subscript : Letter \u2192 Set where\n add-iota-subscript : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 v \u2208 iota-subscript-letters \u2192 v iota-subscript\n\ndata _\u27e6_\u27e7-breathing : Letter \u2192 Case \u2192 Set where\n smooth : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 \u27e6 c \u27e7-smooth \u2984 \u2192 \u2113 \u27e6 c \u27e7-breathing\n rough : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 with-rough \u2984 \u2192 \u2113 \u27e6 c \u27e7-breathing\n\ndata _long-vowel : Letter \u2192 Set where\n make-long-vowel : \u2200 {v} \u2192 \u2983 p : v vowel \u2984 \u2192 \u00ac v always-short \u2192 v long-vowel\n\ndata _accent : Letter \u2192 Set where\n acute : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n grave : \u2200 {\u2113} \u2192 \u2983 p : \u2113 vowel \u2984 \u2192 \u2113 accent\n circumflex : \u2200 {\u2113} \u2192 \u2983 p : \u2113 long-vowel \u2984 \u2192 \u2113 accent\n\ndata Token : Letter \u2192 Case \u2192 Set where\n unmarked : \u2200 {\u2113 c} \u2192 Token \u2113 c\n with-accent : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 Token \u2113 c\n with-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Token \u2113 c\n with-accent-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-diaeresis : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 diaeresis \u2984 \u2192 Token \u2113 c\n with-accent-diaeresis : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2983 p : \u2113 diaeresis \u2984 \u2192 Token \u2113 c\n with-accent-iota : \u2200 {\u2113 c} \u2192 \u2113 accent \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-breathing-iota : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n with-iota : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 iota-subscript \u2984 \u2192 Token \u2113 c\n final : \u2200 {\u2113 c} \u2192 \u2983 p : \u2113 \u27e6 c \u27e7-final \u2984 \u2192 Token \u2113 c\n\n-- Constructions\n\n-- \u0391 \u03b1\ninstance \u03b1-vowel : \u03b1\u2032 vowel\n\u03b1-vowel = is-vowel here\n\n\u00ac\u03b1-always-short : \u00ac \u03b1\u2032 always-short\n\u00ac\u03b1-always-short (is-always-short (there (there ())))\n\ninstance \u03b1-long-vowel : \u03b1\u2032 long-vowel\n\u03b1-long-vowel = make-long-vowel \u00ac\u03b1-always-short\n\ninstance \u03b1-smooth : \u03b1\u2032 \u27e6 lower \u27e7-smooth\n\u03b1-smooth = add-smooth-lower-vowel\n\ninstance \u0391-smooth : \u03b1\u2032 \u27e6 upper \u27e7-smooth\n\u0391-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b1-rough : \u03b1\u2032 with-rough\n\u03b1-rough = add-rough-vowel\n\ninstance \u03b1-iota-subscript : \u03b1\u2032 iota-subscript\n\u03b1-iota-subscript = add-iota-subscript here\n\n-- \u0395 \u03b5\ninstance \u03b5-vowel : \u03b5\u2032 vowel\n\u03b5-vowel = is-vowel (there here)\n\n\u03b5-always-short : \u03b5\u2032 always-short\n\u03b5-always-short = is-always-short here\n\ninstance \u03b5-smooth : \u03b5\u2032 \u27e6 lower \u27e7-smooth\n\u03b5-smooth = add-smooth-lower-vowel\n\ninstance \u0395-smooth : \u03b5\u2032 \u27e6 upper \u27e7-smooth\n\u0395-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b5-rough : \u03b5\u2032 with-rough\n\u03b5-rough = add-rough-vowel\n\n-- \u0397 \u03b7\ninstance \u03b7-vowel : \u03b7\u2032 vowel\n\u03b7-vowel = is-vowel (there (there here))\n\n\u00ac\u03b7-always-short : \u00ac \u03b7\u2032 always-short\n\u00ac\u03b7-always-short (is-always-short (there (there ())))\n\ninstance \u03b7-long-vowel : \u03b7\u2032 long-vowel\n\u03b7-long-vowel = make-long-vowel \u00ac\u03b7-always-short\n\ninstance \u03b7-smooth : \u03b7\u2032 \u27e6 lower \u27e7-smooth\n\u03b7-smooth = add-smooth-lower-vowel\n\ninstance \u0397-smooth : \u03b7\u2032 \u27e6 upper \u27e7-smooth\n\u0397-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b7-rough : \u03b7\u2032 with-rough\n\u03b7-rough = add-rough-vowel\n\ninstance \u03b7-iota-subscript : \u03b7\u2032 iota-subscript\n\u03b7-iota-subscript = add-iota-subscript (there here)\n\n-- \u0399 \u03b9\n\ninstance \u03b9-vowel : \u03b9\u2032 vowel\n\u03b9-vowel = is-vowel (there (there (there here)))\n\n\u00ac\u03b9-always-short : \u00ac \u03b9\u2032 always-short\n\u00ac\u03b9-always-short (is-always-short (there (there ())))\n\ninstance \u03b9-long-vowel : \u03b9\u2032 long-vowel\n\u03b9-long-vowel = make-long-vowel \u00ac\u03b9-always-short\n\ninstance \u03b9-smooth : \u03b9\u2032 \u27e6 lower \u27e7-smooth\n\u03b9-smooth = add-smooth-lower-vowel\n\ninstance \u0399-smooth : \u03b9\u2032 \u27e6 upper \u27e7-smooth\n\u0399-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03b9-rough : \u03b9\u2032 with-rough\n\u03b9-rough = add-rough-vowel\n\ninstance \u03b9-diaeresis : \u03b9\u2032 diaeresis\n\u03b9-diaeresis = add-diaeresis here\n\n-- \u039f \u03bf\ninstance \u03bf-vowel : \u03bf\u2032 vowel\n\u03bf-vowel = is-vowel (there (there (there (there here))))\n\n\u03bf-always-short : \u03bf\u2032 always-short\n\u03bf-always-short = is-always-short (there here)\n\ninstance \u03bf-smooth : \u03bf\u2032 \u27e6 lower \u27e7-smooth\n\u03bf-smooth = add-smooth-lower-vowel\n\ninstance \u039f-smooth : \u03bf\u2032 \u27e6 upper \u27e7-smooth\n\u039f-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03bf-rough : \u03bf\u2032 with-rough\n\u03bf-rough = add-rough-vowel\n\n-- \u03a1 \u03c1\ninstance \u03c1-smooth : \u03c1\u2032 \u27e6 lower \u27e7-smooth\n\u03c1-smooth = add-smooth-\u03c1\n\ninstance \u03c1-rough : \u03c1\u2032 with-rough\n\u03c1-rough = add-rough-\u03c1\n\n-- \u03a3 \u03c3\ninstance \u03c3-final : \u03c3\u2032 \u27e6 lower \u27e7-final\n\u03c3-final = make-final\n\n-- \u03a5 \u03c5\ninstance \u03c5-vowel : \u03c5\u2032 vowel\n\u03c5-vowel = is-vowel (there (there (there (there (there here)))))\n\n\u00ac\u03c5-always-short : \u00ac \u03c5\u2032 always-short\n\u00ac\u03c5-always-short (is-always-short (there (there ())))\n\ninstance \u03c5-long-vowel : \u03c5\u2032 long-vowel\n\u03c5-long-vowel = make-long-vowel \u00ac\u03c5-always-short\n\ninstance \u03c5-smooth : \u03c5\u2032 \u27e6 lower \u27e7-smooth\n\u03c5-smooth = add-smooth-lower-vowel\n\ninstance \u03c5-rough : \u03c5\u2032 with-rough\n\u03c5-rough = add-rough-vowel\n\ninstance \u03c5-diaeresis : \u03c5\u2032 diaeresis\n\u03c5-diaeresis = add-diaeresis (there here)\n\n-- \u03a9 \u03c9\ninstance \u03c9-vowel : \u03c9\u2032 vowel\n\u03c9-vowel = is-vowel (there (there (there (there (there (there here))))))\n\n\u00ac\u03c9-always-short : \u00ac \u03c9\u2032 always-short\n\u00ac\u03c9-always-short (is-always-short (there (there ())))\n\ninstance \u03c9-long-vowel : \u03c9\u2032 long-vowel\n\u03c9-long-vowel = make-long-vowel \u00ac\u03c9-always-short\n\ninstance \u03c9-smooth : \u03c9\u2032 \u27e6 lower \u27e7-smooth\n\u03c9-smooth = add-smooth-lower-vowel\n\ninstance \u03a9-smooth : \u03c9\u2032 \u27e6 upper \u27e7-smooth\n\u03a9-smooth = add-smooth-upper-vowel-not-\u03a5 (\u03bb ())\n\ninstance \u03c9-rough : \u03c9\u2032 with-rough\n\u03c9-rough = add-rough-vowel\n\ninstance \u03c9-iota-subscript : \u03c9\u2032 iota-subscript\n\u03c9-iota-subscript = add-iota-subscript (there (there here))\n\n\n-- Unicode\n-- U+0390 - U+03CE\n\u0390 : Token \u03b9\u2032 lower -- U+0390 GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS\n\u0390 = with-accent-diaeresis acute\n\u0391 : Token \u03b1\u2032 upper -- U+0391 GREEK CAPITAL LETTER ALPHA\n\u0391 = unmarked\n\u0392 : Token \u03b2\u2032 upper -- U+0392 GREEK CAPITAL LETTER BETA\n\u0392 = unmarked\n\u0393 : Token \u03b3\u2032 upper -- U+0393 GREEK CAPITAL LETTER GAMMA\n\u0393 = unmarked\n\u0394 : Token \u03b4\u2032 upper -- U+0394 GREEK CAPITAL LETTER DELTA\n\u0394 = unmarked\n\u0395 : Token \u03b5\u2032 upper -- U+0395 GREEK CAPITAL LETTER EPSILON\n\u0395 = unmarked\n\u0396 : Token \u03b6\u2032 upper -- U+0396 GREEK CAPITAL LETTER ZETA\n\u0396 = unmarked\n\u0397 : Token \u03b7\u2032 upper -- U+0397 GREEK CAPITAL LETTER ETA\n\u0397 = unmarked\n\u0398 : Token \u03b8\u2032 upper -- U+0398 GREEK CAPITAL LETTER THETA\n\u0398 = unmarked\n\u0399 : Token \u03b9\u2032 upper -- U+0399 GREEK CAPITAL LETTER IOTA\n\u0399 = unmarked\n\u039a : Token \u03ba\u2032 upper -- U+039A GREEK CAPITAL LETTER KAPPA\n\u039a = unmarked\n\u039b : Token \u03bb\u2032 upper -- U+039B GREEK CAPITAL LETTER LAMDA\n\u039b = unmarked\n\u039c : Token \u03bc\u2032 upper -- U+039C GREEK CAPITAL LETTER MU\n\u039c = unmarked\n\u039d : Token \u03bd\u2032 upper -- U+039D GREEK CAPITAL LETTER NU\n\u039d = unmarked\n\u039e : Token \u03be\u2032 upper -- U+039E GREEK CAPITAL LETTER XI\n\u039e = unmarked\n\u039f : Token \u03bf\u2032 upper -- U+039F GREEK CAPITAL LETTER OMICRON\n\u039f = unmarked\n\u03a0 : Token \u03c0\u2032 upper -- U+03A0 GREEK CAPITAL LETTER PI\n\u03a0 = unmarked\n\u03a1 : Token \u03c1\u2032 upper -- U+03A1 GREEK CAPITAL LETTER RHO\n\u03a1 = unmarked\n -- U+03A2 \n\u03a3 : Token \u03c3\u2032 upper -- U+03A3 GREEK CAPITAL LETTER SIGMA\n\u03a3 = unmarked\n\u03a4 : Token \u03c4\u2032 upper -- U+03A4 GREEK CAPITAL LETTER TAU\n\u03a4 = unmarked\n\u03a5 : Token \u03c5\u2032 upper -- U+03A5 GREEK CAPITAL LETTER UPSILON\n\u03a5 = unmarked\n\u03a6 : Token \u03c6\u2032 upper -- U+03A6 GREEK CAPITAL LETTER PHI\n\u03a6 = unmarked\n\u03a7 : Token \u03c7\u2032 upper -- U+03A7 GREEK CAPITAL LETTER CHI\n\u03a7 = unmarked\n\u03a8 : Token \u03c8\u2032 upper -- U+03A8 GREEK CAPITAL LETTER PSI\n\u03a8 = unmarked\n\u03a9 : Token \u03c9\u2032 upper -- U+03A9 GREEK CAPITAL LETTER OMEGA\n\u03a9 = unmarked\n\u03aa : Token \u03b9\u2032 upper -- U+03AA GREEK CAPITAL LETTER IOTA WITH DIALYTIKA\n\u03aa = with-diaeresis\n\u03ab : Token \u03c5\u2032 upper -- U+03AB GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA\n\u03ab = with-diaeresis\n\u03ac : Token \u03b1\u2032 lower -- U+03AC GREEK SMALL LETTER ALPHA WITH TONOS\n\u03ac = with-accent acute\n\u03ad : Token \u03b5\u2032 lower -- U+03AD GREEK SMALL LETTER EPSILON WITH TONOS\n\u03ad = with-accent acute\n\u03ae : Token \u03b7\u2032 lower -- U+03AE GREEK SMALL LETTER ETA WITH TONOS\n\u03ae = with-accent acute\n\u03af : Token \u03b9\u2032 lower -- U+03AF GREEK SMALL LETTER IOTA WITH TONOS\n\u03af = with-accent acute\n\u03b0 : Token \u03c5\u2032 lower -- U+03B0 GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS\n\u03b0 = with-accent-diaeresis acute\n\u03b1 : Token \u03b1\u2032 lower -- U+03B1 GREEK SMALL LETTER ALPHA\n\u03b1 = unmarked\n\u03b2 : Token \u03b2\u2032 lower -- U+03B2 GREEK SMALL LETTER BETA\n\u03b2 = unmarked\n\u03b3 : Token \u03b3\u2032 lower -- U+03B3 GREEK SMALL LETTER GAMMA\n\u03b3 = unmarked\n\u03b4 : Token \u03b4\u2032 lower -- U+03B4 GREEK SMALL LETTER DELTA\n\u03b4 = unmarked\n\u03b5 : Token \u03b5\u2032 lower -- U+03B5 GREEK SMALL LETTER EPSILON\n\u03b5 = unmarked\n\u03b6 : Token \u03b6\u2032 lower -- U+03B6 GREEK SMALL LETTER ZETA\n\u03b6 = unmarked\n\u03b7 : Token \u03b7\u2032 lower -- U+03B7 GREEK SMALL LETTER ETA\n\u03b7 = unmarked\n\u03b8 : Token \u03b8\u2032 lower -- U+03B8 GREEK SMALL LETTER THETA\n\u03b8 = unmarked\n\u03b9 : Token \u03b9\u2032 lower -- U+03B9 GREEK SMALL LETTER IOTA\n\u03b9 = unmarked\n\u03ba : Token \u03ba\u2032 lower -- U+03BA GREEK SMALL LETTER KAPPA\n\u03ba = unmarked\n\u2219\u03bb : Token \u03bb\u2032 lower -- U+03BB GREEK SMALL LETTER LAMDA\n\u2219\u03bb = unmarked\n\u03bc : Token \u03bc\u2032 lower -- U+03BC GREEK SMALL LETTER MU\n\u03bc = unmarked\n\u03bd : Token \u03bd\u2032 lower -- U+03BD GREEK SMALL LETTER NU\n\u03bd = unmarked\n\u03be : Token \u03be\u2032 lower -- U+03BE GREEK SMALL LETTER XI\n\u03be = unmarked\n\u03bf : Token \u03bf\u2032 lower -- U+03BF GREEK SMALL LETTER OMICRON\n\u03bf = unmarked\n\u03c0 : Token \u03c0\u2032 lower -- U+03C0 GREEK SMALL LETTER PI\n\u03c0 = unmarked\n\u03c1 : Token \u03c1\u2032 lower -- U+03C1 GREEK SMALL LETTER RHO\n\u03c1 = unmarked\n\u03c2 : Token \u03c3\u2032 lower -- U+03C2 GREEK SMALL LETTER FINAL SIGMA\n\u03c2 = final\n\u03c3 : Token \u03c3\u2032 lower -- U+03C3 GREEK SMALL LETTER SIGMA\n\u03c3 = unmarked\n\u03c4 : Token \u03c4\u2032 lower -- U+03C4 GREEK SMALL LETTER TAU\n\u03c4 = unmarked\n\u03c5 : Token \u03c5\u2032 lower -- U+03C5 GREEK SMALL LETTER UPSILON\n\u03c5 = unmarked\n\u03c6 : Token \u03c6\u2032 lower -- U+03C6 GREEK SMALL LETTER PHI\n\u03c6 = unmarked\n\u03c7 : Token \u03c7\u2032 lower -- U+03C7 GREEK SMALL LETTER CHI\n\u03c7 = unmarked\n\u03c8 : Token \u03c8\u2032 lower -- U+03C8 GREEK SMALL LETTER PSI\n\u03c8 = unmarked\n\u03c9 : Token \u03c9\u2032 lower -- U+03C9 GREEK SMALL LETTER OMEGA\n\u03c9 = unmarked\n\u03ca : Token \u03b9\u2032 lower -- U+03CA GREEK SMALL LETTER IOTA WITH DIALYTIKA\n\u03ca = with-diaeresis\n\u03cb : Token \u03c5\u2032 lower -- U+03CB GREEK SMALL LETTER UPSILON WITH DIALYTIKA\n\u03cb = with-diaeresis\n\u03cc : Token \u03bf\u2032 lower -- U+03CC GREEK SMALL LETTER OMICRON WITH TONOS\n\u03cc = with-accent acute\n\u03cd : Token \u03c5\u2032 lower -- U+03CD GREEK SMALL LETTER UPSILON WITH TONOS\n\u03cd = with-accent acute\n\u03ce : Token \u03c9\u2032 lower -- U+03CE GREEK SMALL LETTER OMEGA WITH TONOS\n\u03ce = with-accent acute\n\n\n\u1f00 : Token \u03b1\u2032 lower\n\u1f00 = with-breathing smooth\n\u1f01 : Token \u03b1\u2032 lower\n\u1f01 = with-breathing rough\n\u1f02 : Token \u03b1\u2032 lower\n\u1f02 = with-accent-breathing grave smooth\n\u1f03 : Token \u03b1\u2032 lower\n\u1f03 = with-accent-breathing grave rough\n\u1f04 : Token \u03b1\u2032 lower\n\u1f04 = with-accent-breathing acute smooth\n\u1f05 : Token \u03b1\u2032 lower\n\u1f05 = with-accent-breathing acute rough\n\u1f06 : Token \u03b1\u2032 lower\n\u1f06 = with-accent-breathing circumflex smooth\n\u1f07 : Token \u03b1\u2032 lower\n\u1f07 = with-accent-breathing circumflex rough\n\u1f08 : Token \u03b1\u2032 upper\n\u1f08 = with-breathing smooth\n\u1f09 : Token \u03b1\u2032 upper\n\u1f09 = with-breathing rough\n\u1f0a : Token \u03b1\u2032 upper\n\u1f0a = with-accent-breathing grave smooth\n\u1f0b : Token \u03b1\u2032 upper\n\u1f0b = with-accent-breathing grave rough\n\u1f0c : Token \u03b1\u2032 upper\n\u1f0c = with-accent-breathing acute smooth\n\u1f0d : Token \u03b1\u2032 upper\n\u1f0d = with-accent-breathing acute rough\n\u1f0e : Token \u03b1\u2032 upper\n\u1f0e = with-accent-breathing circumflex smooth\n\u1f0f : Token \u03b1\u2032 upper\n\u1f0f = with-accent-breathing circumflex rough\n\n-- U+1F7x\n\u1f70 : Token \u03b1\u2032 lower\n\u1f70 = with-accent grave\n\u1f71 : Token \u03b1\u2032 lower\n\u1f71 = with-accent acute\n\n-- U+1F8x\n\u1f80 : Token \u03b1\u2032 lower\n\u1f80 = with-breathing-iota smooth\n\u1f81 : Token \u03b1\u2032 lower\n\u1f81 = with-breathing-iota rough\n\u1f82 : Token \u03b1\u2032 lower\n\u1f82 = with-accent-breathing-iota grave smooth\n\u1f83 : Token \u03b1\u2032 lower\n\u1f83 = with-accent-breathing-iota grave rough\n\u1f84 : Token \u03b1\u2032 lower\n\u1f84 = with-accent-breathing-iota acute smooth\n\u1f85 : Token \u03b1\u2032 lower\n\u1f85 = with-accent-breathing-iota acute rough\n\u1f86 : Token \u03b1\u2032 lower\n\u1f86 = with-accent-breathing-iota circumflex smooth\n\u1f87 : Token \u03b1\u2032 lower\n\u1f87 = with-accent-breathing-iota circumflex rough\n\u1f88 : Token \u03b1\u2032 upper\n\u1f88 = with-breathing-iota smooth\n\u1f89 : Token \u03b1\u2032 upper\n\u1f89 = with-breathing-iota rough\n\u1f8a : Token \u03b1\u2032 upper\n\u1f8a = with-accent-breathing-iota grave smooth\n\u1f8b : Token \u03b1\u2032 upper\n\u1f8b = with-accent-breathing-iota grave rough\n\u1f8c : Token \u03b1\u2032 upper\n\u1f8c = with-accent-breathing-iota acute smooth\n\u1f8d : Token \u03b1\u2032 upper\n\u1f8d = with-accent-breathing-iota acute rough\n\u1f8e : Token \u03b1\u2032 upper\n\u1f8e = with-accent-breathing-iota circumflex smooth\n\u1f8f : Token \u03b1\u2032 upper\n\u1f8f = with-accent-breathing-iota circumflex rough\n\n-- U+1FBx\n-- \u1fb0\n-- \u1fb1\n\u1fb2 : Token \u03b1\u2032 lower\n\u1fb2 = with-accent-iota grave\n\u1fb3 : Token \u03b1\u2032 lower\n\u1fb3 = with-iota\n\u1fb4 : Token \u03b1\u2032 lower\n\u1fb4 = with-accent-iota acute\n\u1fb6 : Token \u03b1\u2032 lower\n\u1fb6 = with-accent circumflex\n\u1fb7 : Token \u03b1\u2032 lower\n\u1fb7 = with-accent-iota circumflex\n-- \u1fb8\n-- \u1fb9\n\u1fba : Token \u03b1\u2032 upper\n\u1fba = with-accent grave\n\u1fbb : Token \u03b1\u2032 upper\n\u1fbb = with-accent acute\n\u1fbc : Token \u03b1\u2032 upper\n\u1fbc = with-iota\n\n-- Mapping\n\ndata Accent : Set where\n acute-mark grave-mark circumflex-mark : Accent\n\nletter-to-accent : \u2200 {v} \u2192 v accent \u2192 Accent\nletter-to-accent acute = acute-mark\nletter-to-accent grave = grave-mark\nletter-to-accent circumflex = circumflex-mark\n\nget-accent : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Accent\nget-accent (with-accent a) = just (letter-to-accent a)\nget-accent (with-accent-breathing a _) = just (letter-to-accent a)\nget-accent (with-accent-breathing-iota a _) = just (letter-to-accent a)\nget-accent (with-accent-diaeresis a) = just (letter-to-accent a)\nget-accent (with-accent-iota a) = just (letter-to-accent a)\nget-accent _ = nothing\n\ndata Breathing : Set where\n smooth-mark rough-mark : Breathing\n\nletter-to-breathing : \u2200 {\u2113 c} \u2192 \u2113 \u27e6 c \u27e7-breathing \u2192 Breathing\nletter-to-breathing smooth = smooth-mark\nletter-to-breathing rough = rough-mark\n\nget-breathing : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Breathing\nget-breathing (with-breathing x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing _ x) = just (letter-to-breathing x)\nget-breathing (with-accent-breathing-iota _ x) = just (letter-to-breathing x)\nget-breathing (with-breathing-iota x) = just (letter-to-breathing x)\nget-breathing _ = nothing\n\ndata IotaSubscript : Set where\n iota-subscript-mark : IotaSubscript\n\nget-iota-subscript : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe IotaSubscript\nget-iota-subscript (with-accent-breathing-iota _ _) = just iota-subscript-mark\nget-iota-subscript (with-accent-iota _) = just iota-subscript-mark\nget-iota-subscript (with-breathing-iota _) = just iota-subscript-mark\nget-iota-subscript _ = nothing\n\ndata Diaeresis : Set where\n diaeresis-mark : Diaeresis\n\nget-diaeresis : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe Diaeresis\nget-diaeresis with-diaeresis = just diaeresis-mark\nget-diaeresis (with-accent-diaeresis _) = just diaeresis-mark\nget-diaeresis _ = nothing\n\ndata FinalForm : Set where\n final-form : FinalForm\n\nget-final-form : \u2200 {\u2113 c} \u2192 Token \u2113 c \u2192 Maybe FinalForm\nget-final-form final = just final-form\nget-final-form _ = nothing\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"59a112037f5ddcf24c9663b9399e5b60b19d1198","subject":"IDesc: tagged indexed description","message":"IDesc: tagged indexed description\n","repos":"mietek\/epigram2,larrytheliquid\/pigit,mietek\/epigram2","old_file":"models\/IDesc_type_type.agda","new_file":"models\/IDesc_type_type.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {I : Set} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi (pi S T) = cong (pi S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n\n-- From embedding to embedding\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\\ _ -> descD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> descD I)) -> Set\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (descD I) (IDescl0 I))\n (hs : desc (box (descD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\\ s -> psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s -> psi (phi (T s)))\n T\n hs)","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {I : Set} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi (pi S T) = cong (pi S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n\n-- From embedding to embedding\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\\ _ -> descD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> descD I)) -> Set\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (descD I) (IDescl0 I))\n (hs : desc (box (descD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\\ s -> psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s -> psi (phi (T s)))\n T\n hs)","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"8062ff2e3a4db2f2405fcecd8ecd3687f9574da1","subject":"Added doc.","message":"Added doc.\n\nIgnore-this: d943a6359f1fa72487ab808788c3bd0e\n\ndarcs-hash:20110227043610-3bd4e-0cb5a48fd55778fc07d4e4e5389bf3e2d2648b58.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Program\/McCarthy91\/MCR\/WellFoundedInductionATP.agda","new_file":"src\/FOTC\/Program\/McCarthy91\/MCR\/WellFoundedInductionATP.agda","new_contents":"----------------------------------------------------------------------------\n-- Well-founded induction on the relation MCR\n----------------------------------------------------------------------------\n\n{-# OPTIONS --no-termination-check #-}\n\n-- N.B This module does not contain combined proofs, but it imports\n-- modules which contain combined proofs.\n\nmodule FOTC.Program.McCarthy91.MCR.WellFoundedInductionATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Nat\n\nopen import FOTC.Program.McCarthy91.McCarthy91\nopen import FOTC.Program.McCarthy91.MCR\nopen import FOTC.Program.McCarthy91.MCR.PropertiesATP\n\n----------------------------------------------------------------------------\n\n-- Adapted from FOTC.Data.Nat.Induction.WellFoundedI.wfInd-LT\u2081.\nwfInd-MCR : (P : D \u2192 Set) \u2192\n (\u2200 {m} \u2192 N m \u2192 (\u2200 {n} \u2192 N n \u2192 MCR n m \u2192 P n) \u2192 P m) \u2192\n \u2200 {n} \u2192 N n \u2192 P n\nwfInd-MCR P accH Nn = accH Nn (helper Nn)\n where\n helper : \u2200 {m n} \u2192 N m \u2192 N n \u2192 MCR n m \u2192 P n\n helper Nm zN 0\u00abm = \u22a5-elim (0\u00abx\u2192\u22a5 Nm 0\u00abm)\n\n -- This equation does not pass the termination check.\n helper zN (sN {n} Nn) Sn\u00ab0 = accH (sN Nn)\n (\u03bb {n'} Nn' n'\u00abSn \u2192\n let n'\u00ab0 : MCR n' zero\n n'\u00ab0 = \u00ab-trans Nn' (sN Nn) zN n'\u00abSn Sn\u00ab0\n\n in helper zN Nn' n'\u00ab0\n )\n\n -- Other version of the previous equation (this version neither\n -- pass the termination check).\n -- helper zN (sN {n} Nn) Sn\u00ab0 = accH (sN Nn)\n -- (\u03bb {n'} Nn' n'\u00abSn \u2192\n -- let n'\u00abn : MCR n' n\n -- n'\u00abn = x\u00abSy\u2192x\u00aby Nn' Nn n'\u00abSn\n\n -- in helper Nn Nn' n'\u00abn\n -- )\n\n -- Other version of the previous equation (this version neither\n -- pass the termination check).\n -- helper zN Nn n\u00ab0 = accH Nn\n -- (\u03bb {n'} Nn' n'\u00abn \u2192\n -- let n'\u00ab0 : MCR n' zero\n -- n'\u00ab0 = \u00ab-trans Nn' Nn zN n'\u00abn n\u00ab0\n\n -- in helper zN Nn' n'\u00ab0\n -- )\n\n -- Other version of the previous equation (this version neither\n -- pass the termination check).\n -- helper {n = n} zN Nn n\u00ab0 =\n -- accH Nn (\u03bb {n'} Nn' n'\u00abn \u2192 helper Nn Nn' n'\u00abn)\n\n helper (sN {m} Nm) (sN {n} Nn) Sn\u00abSm = helper Nm (sN Nn) Sn\u00abm\n where\n Sn\u00abm : MCR (succ n) m\n Sn\u00abm = x\u00abSy\u2192x\u00aby (sN Nn) Nm Sn\u00abSm\n","old_contents":"----------------------------------------------------------------------------\n-- Well-founded induction on the relation MCR\n----------------------------------------------------------------------------\n\n{-# OPTIONS --no-termination-check #-}\n\n-- N.B This module does not contain combined proofs, but it imports\n-- modules which contain combined proofs.\n\nmodule FOTC.Program.McCarthy91.MCR.WellFoundedInductionATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Nat\n\nopen import FOTC.Program.McCarthy91.McCarthy91\nopen import FOTC.Program.McCarthy91.MCR\nopen import FOTC.Program.McCarthy91.MCR.PropertiesATP\n\n----------------------------------------------------------------------------\n\n-- Adapted from FOTC.Data.Nat.Induction.WellFoundedI.wfInd-LT\u2081.\nwfInd-MCR : (P : D \u2192 Set) \u2192\n (\u2200 {m} \u2192 N m \u2192 (\u2200 {n} \u2192 N n \u2192 MCR n m \u2192 P n) \u2192 P m) \u2192\n \u2200 {n} \u2192 N n \u2192 P n\nwfInd-MCR P accH Nn = accH Nn (helper Nn)\n where\n helper : \u2200 {m n} \u2192 N m \u2192 N n \u2192 MCR n m \u2192 P n\n helper Nm zN 0\u00abm = \u22a5-elim (0\u00abx\u2192\u22a5 Nm 0\u00abm)\n\n -- This equation does not pass the termination check.\n helper zN Nn n\u00ab0 = accH Nn\n (\u03bb {n'} Nn' n'\u00abn \u2192\n let n'\u00ab0 : MCR n' zero\n n'\u00ab0 = \u00ab-trans Nn' Nn zN n'\u00abn n\u00ab0\n\n in helper zN Nn' n'\u00ab0\n )\n\n helper (sN {m} Nm) (sN {n} Nn) Sn\u00abSm = helper Nm (sN Nn) Sn\u00abm\n where\n Sn\u00abm : MCR (succ n) m\n Sn\u00abm = x\u00abSy\u2192x\u00aby (sN Nn) Nm Sn\u00abSm\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3d513b19df5421df9f336ebcdb620a5af4aa0ce4","subject":"progress but stuck...","message":"progress but stuck...\n","repos":"crypto-agda\/crypto-agda","old_file":"Control\/Protocol\/Choreography.agda","new_file":"Control\/Protocol\/Choreography.agda","new_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map; proj\u2081 to fst; proj\u2082 to snd)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_]) hiding ([_,_]\u2032)\nopen import Data.One hiding (_\u225f_)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP as \u2261\n\nmodule Control.Protocol.Choreography where\n\npostulate\n FunExt : \u2605\n funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 {{fe : FunExt}} \u2192 f \u2261 g\n\nContractible : \u2200 {a}{A : \u2605_ a}(x : A) \u2192 \u2605_ a\nContractible x = \u2200 y \u2192 x \u2261 y\n\nmodule Equivalences where\n\n module _ {A B : \u2605} where\n _LeftInverseOf_ : (B \u2192 A) \u2192 (A \u2192 B) \u2192 \u2605\n linv LeftInverseOf f = \u2200 x \u2192 linv (f x) \u2261 x\n\n _RightInverseOf_ : (B \u2192 A) \u2192 (A \u2192 B) \u2192 \u2605\n rinv RightInverseOf f = \u2200 x \u2192 f (rinv x) \u2261 x\n\n record Equiv {A B : \u2605}(f : A \u2192 B) : \u2605 where\n field\n linv : B \u2192 A\n is-linv : \u2200 x \u2192 linv (f x) \u2261 x\n rinv : B \u2192 A\n is-rinv : \u2200 x \u2192 f (rinv x) \u2261 x\n\n module _ {A B : \u2605}{f : A \u2192 B}(f\u1d31 : Equiv f) where\n open Equiv f\u1d31\n inv : B \u2192 A\n inv = linv \u2218 f \u2218 rinv\n\n inv-equiv : Equiv inv\n inv-equiv = record { linv = f\n ; is-linv = \u03bb x \u2192 cong f (is-linv (rinv x)) \u2219 is-rinv x\n ; rinv = f\n ; is-rinv = \u03bb x \u2192 cong linv (is-rinv (f x)) \u2219 is-linv x }\n\n id\u1d31 : \u2200 {A} \u2192 Equiv {A} id\n id\u1d31 = record\n { linv = id\n ; is-linv = \u03bb _ \u2192 refl\n ; rinv = id\n ; is-rinv = \u03bb _ \u2192 refl\n }\n\n module _ {A B C}{g : B \u2192 C}{f : A \u2192 B} where\n _\u2218\u1d31_ : Equiv g \u2192 Equiv f \u2192 Equiv (g \u2218 f)\n g\u1d31 \u2218\u1d31 f\u1d31 = record { linv = F.linv \u2218 G.linv ; is-linv = \u03bb x \u2192 cong F.linv (G.is-linv (f x)) \u2219 F.is-linv x\n ; rinv = F.rinv \u2218 G.rinv ; is-rinv = \u03bb x \u2192 cong g (F.is-rinv _) \u2219 G.is-rinv x }\n where\n module G = Equiv g\u1d31\n module F = Equiv f\u1d31\n\n infix 0 _\u2243_\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = \u03a3 (A \u2192 B) Equiv\n\n \u2243-refl : Reflexive _\u2243_\n \u2243-refl = _ , id\u1d31\n\n \u2243-sym : Symmetric _\u2243_\n \u2243-sym (_ , f\u1d31) = _ , inv-equiv f\u1d31\n\n \u2243-trans : Transitive _\u2243_\n \u2243-trans (_ , p) (_ , q) = _ , q \u2218\u1d31 p\n\n \u2243-! = \u2243-sym\n _\u2243-\u2219_ = \u2243-trans\n\n module Contractible\u2192Equiv {A : \u2605}{x : A}(x-contr : Contractible x) where\n const-equiv : Equiv {\ud835\udfd9} (\u03bb _ \u2192 x)\n const-equiv = record { linv = _ ; is-linv = \u03bb _ \u2192 refl ; rinv = _ ; is-rinv = x-contr }\n \ud835\udfd9\u2243 : \ud835\udfd9 \u2243 A\n \ud835\udfd9\u2243 = _ , const-equiv\n module Equiv\u2192Contractible {A : \u2605}(f : \ud835\udfd9 \u2192 A)(f-equiv : Equiv f) where\n open Equiv f-equiv\n A-contr : Contractible (f _)\n A-contr = is-rinv\n\n module _ {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b} where\n \u03a3-ext : \u2200 {x y : \u03a3 A B} \u2192 (p : fst x \u2261 fst y) \u2192 subst B p (snd x) \u2261 snd y \u2192 x \u2261 y\n \u03a3-ext refl = cong (_,_ _)\nopen Equivalences\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\n {-\ndata S<_> {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com_ \u2113 : \u2605_(\u209b \u2113) where\n constructor mk\n field\n io : InOut\n M : \u2605_ \u2113\n P : M \u2192 Proto_ \u2113\n\n data Proto_ \u2113 : \u2605_(\u209b \u2113) where\n end : Proto_ \u2113\n com : Com_ \u2113 \u2192 Proto_ \u2113\n\nProto : \u2605\u2081\nProto = Proto_ \u2080\nProto\u2080 = Proto\nProto\u2081 = Proto_ \u2081\nCom : \u2605\u2081\nCom = Com_ \u2080\n\n{-\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n-}\npattern com' q M P = com (mk q M P)\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\n\nmodule ProtoRelImplicit {_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605} = ProtoRel _\u2248\u1d35\u1d3c_\nopen ProtoRelImplicit hiding (_\u2248\u1d3e_)\nopen ProtoRel _\u2261_ public renaming (_\u2248\u1d3e_ to _\u2261\u1d3e_) using ()\n\ndata View-\u2261\u1d3e : (P Q : Proto) \u2192 P \u2261\u1d3e Q \u2192 \u2605\u2081 where\n end : View-\u2261\u1d3e end end end\n \u2261-\u03a3 : \u2200 {M P Q} (p\u2261q : \u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 View-\u2261\u1d3e (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (com refl M p\u2261q)\n \u2261-\u03a0 : \u2200 {M P Q} (p\u2261q : \u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 View-\u2261\u1d3e (\u03a0\u1d3e _ P) (\u03a0\u1d3e _ Q) (com refl M p\u2261q)\n\nview-\u2261\u1d3e : \u2200 {P Q} (p\u2261q : P \u2261\u1d3e Q) \u2192 View-\u2261\u1d3e P Q p\u2261q\nview-\u2261\u1d3e end = end\nview-\u2261\u1d3e (com {In} refl _ _) = \u2261-\u03a0 _\nview-\u2261\u1d3e (com {Out} refl _ _) = \u2261-\u03a3 _\n\n{-\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n -}\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n source-of : Proto \u2192 Proto\n source-of end = end\n source-of (com c) = com (source-of\u1d9c c)\n\n source-of\u1d9c : Com \u2192 Com\n source-of\u1d9c (mk _ M P) = \u03a3\u1d9c M \u03bb m \u2192 source-of (P m)\n\n {-\ndual : Proto \u2192 Proto\ndual end = end\ndual (\u03a3\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 dual (P m)\ndual (\u03a0\u1d3e M P) = \u03a3\u1d3e M \u03bb m \u2192 dual (P m)\n-}\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c (mk io M P) = mk (dual\u1d35\u1d3c io) M \u03bb m \u2192 dual (P m)\n\ndata IsSource : Proto \u2192 \u2605\u2081 where\n end : IsSource end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSource (P m)) \u2192 IsSource (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\nrecord End_ \u2113 : \u2605_ \u2113 where\n constructor end\nEnd : \u2200 {\u2113} \u2192 \u2605_ \u2113\nEnd = End_ _\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 \u2200{\u2113}(M : \u2605_ \u2113) (P : M \u2192 \u2605_ \u2113) \u2192 \u2605_ \u2113\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : \u2200 {\u2113} \u2192 Proto_ \u2113 \u2192 \u2605_ \u2113\n\u27e6 end \u27e7 = End\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n\u211b\u27e6_\u27e7 : \u2200{\u2113}(P : Proto_ \u2113) (p q : \u27e6 P \u27e7) \u2192 \u2605_ \u2113\n\u211b\u27e6 end \u27e7 p q = End\n\u211b\u27e6 \u03a0\u1d3e M P \u27e7 p q = (m : M) \u2192 \u211b\u27e6 P m \u27e7 (p m) (q m)\n\u211b\u27e6 \u03a3\u1d3e M P \u27e7 p q = \u03a3 (fst p \u2261 fst q) \u03bb e \u2192 \u211b\u27e6 P (fst q) \u27e7 (subst (\u27e6_\u27e7 \u2218 P) e (snd p)) (snd q)\n\n\u211b\u27e6_\u27e7-refl : \u2200 {\u2113}(P : Proto_ \u2113) \u2192 Reflexive \u211b\u27e6 P \u27e7\n\u211b\u27e6 end \u27e7-refl = end\n\u211b\u27e6 \u03a0\u1d3e M P \u27e7-refl = \u03bb m \u2192 \u211b\u27e6 P m \u27e7-refl\n\u211b\u27e6 \u03a3\u1d3e M P \u27e7-refl {x} = refl , \u211b\u27e6 P (fst x) \u27e7-refl\n\n\u211b\u27e6_\u27e7-sym : \u2200 {\u2113}(P : Proto_ \u2113) \u2192 Symmetric \u211b\u27e6 P \u27e7\n\u211b\u27e6 end \u27e7-sym p = end\n\u211b\u27e6 \u03a0\u1d3e M P \u27e7-sym p = \u03bb m \u2192 \u211b\u27e6 P m \u27e7-sym (p m)\n\u211b\u27e6 \u03a3\u1d3e M P \u27e7-sym (refl , q) = refl , \u211b\u27e6 P _ \u27e7-sym q -- TODO HoTT\n\n\u211b\u27e6_\u27e7-trans : \u2200 {\u2113}(P : Proto_ \u2113) \u2192 Transitive \u211b\u27e6 P \u27e7\n\u211b\u27e6 end \u27e7-trans p q = end\n\u211b\u27e6 \u03a0\u1d3e M P \u27e7-trans p q = \u03bb m \u2192 \u211b\u27e6 P m \u27e7-trans (p m) (q m)\n\u211b\u27e6 \u03a3\u1d3e M P \u27e7-trans (refl , p) (refl , q) = refl , \u211b\u27e6 P _ \u27e7-trans p q -- TODO HoTT\n\ndata ViewProc {\u2113} : \u2200 (P : Proto_ \u2113) \u2192 \u27e6 P \u27e7 \u2192 \u2605_(\u209b \u2113) where\n send : \u2200 M(P : M \u2192 Proto_ \u2113)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewProc (\u03a3\u1d3e M P) (m , p)\n recv : \u2200 M(P : M \u2192 Proto_ \u2113)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewProc (\u03a0\u1d3e M P) p\n end : ViewProc end _\n\nview-proc : \u2200 {\u2113} (P : Proto_ \u2113) (p : \u27e6 P \u27e7) \u2192 ViewProc P p\nview-proc end _ = end\nview-proc (\u03a0\u1d3e M P) p = recv _ _ p\nview-proc (\u03a3\u1d3e M P) (m , p) = send _ _ m p\n\ndata ViewCom : \u2200 P \u2192 \u27e6 com P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200{M}(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewCom (\u03a3\u1d9c M P) (m , p)\n recv : \u2200{M}(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewCom (\u03a0\u1d9c M P) p\n\nview-com : \u2200 P (p : \u27e6 com P \u27e7) \u2192 ViewCom P p\nview-com (\u03a0\u1d9c M P) p = recv _ p\nview-com (\u03a3\u1d9c M P) (m , p) = send _ m p\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com refl M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\n\u2261\u1d3e-reflexive : \u2200 {P Q} \u2192 P \u2261 Q \u2192 P \u2261\u1d3e Q\n\u2261\u1d3e-reflexive refl = \u2261\u1d3e-refl _\n\n\u2261\u1d3e-trans : Transitive _\u2261\u1d3e_\n\u2261\u1d3e-trans end qr = qr\n\u2261\u1d3e-trans (com refl M x) (com refl .M x\u2081) = com refl M (\u03bb m \u2192 \u2261\u1d3e-trans (x m) (x\u2081 m))\n\n_\u2219\u1d3e_ = \u2261\u1d3e-trans\n\nmodule _ {{_ : FunExt}} where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com refl M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n \u2261\u1d3e-cong : \u2200 {P Q} (f : Proto \u2192 Proto) \u2192 P \u2261\u1d3e Q \u2192 f P \u2261\u1d3e f Q\n \u2261\u1d3e-cong f P\u2261Q = \u2261\u1d3e-reflexive (cong f (\u2261\u1d3e-sound P\u2261Q))\n\ndual\u1d35\u1d3c-involutive : \u2200 io \u2192 dual\u1d35\u1d3c (dual\u1d35\u1d3c io) \u2261 io\ndual\u1d35\u1d3c-involutive In = refl\ndual\u1d35\u1d3c-involutive Out = refl\n\ndual-involutive : \u2200 P \u2192 dual (dual P) \u2261\u1d3e P\ndual-involutive end = end\ndual-involutive (com' q M P) = com (dual\u1d35\u1d3c-involutive q) M (\u03bb m \u2192 dual-involutive (P m))\n\ndual-inj : \u2200 P Q \u2192 dual P \u2261\u1d3e dual Q \u2192 P \u2261\u1d3e Q\ndual-inj end end end = end\ndual-inj end (com x) ()\ndual-inj (com x) end ()\ndual-inj (com (mk In M P\u2081)) (com (mk In .M P)) (ProtoRel.com q .M x) = ProtoRel.com refl M (\u03bb m \u2192 dual-inj (P\u2081 m) (P m) (x m))\ndual-inj (com (mk In M P\u2081)) (com (mk Out .M P)) (ProtoRel.com () .M x)\ndual-inj (com (mk Out M P)) (com (mk In .M Q)) (ProtoRel.com () .M x)\ndual-inj (com (mk Out M P)) (com (mk Out .M Q)) (ProtoRel.com refl .M x) = ProtoRel.com refl M (\u03bb m \u2192 dual-inj (P m) (Q m) (x m))\n\nsource-of-idempotent : \u2200 P \u2192 source-of (source-of P) \u2261\u1d3e source-of P\nsource-of-idempotent end = end\nsource-of-idempotent (com' _ M P) = com refl M \u03bb m \u2192 source-of-idempotent (P m)\n\nsource-of-dual-oblivious : \u2200 P \u2192 source-of (dual P) \u2261\u1d3e source-of P\nsource-of-dual-oblivious end = end\nsource-of-dual-oblivious (com' _ M P) = com refl M \u03bb m \u2192 source-of-dual-oblivious (P m)\n\nsink-of : Proto \u2192 Proto\nsink-of = dual \u2218 source-of\n\nSink : Proto \u2192 \u2605\nSink P = \u27e6 sink-of P \u27e7\n\nsink : \u2200 P \u2192 Sink P\nsink end = _\nsink (com' _ M P) x = sink (P x)\n\nmodule _ {{_ : FunExt}} where\n sink-contr : \u2200 P \u2192 Contractible (sink P)\n sink-contr end s = refl\n sink-contr (com' _ _ P) s = funExt \u03bb m \u2192 sink-contr (P m) (s m)\n\n \ud835\udfd9\u2243Sink : \u2200 P \u2192 \ud835\udfd9 \u2243 Sink P\n \ud835\udfd9\u2243Sink P = Contractible\u2192Equiv.\ud835\udfd9\u2243 (sink-contr P)\n\nLog : Proto \u2192 \u2605\nLog P = \u27e6 source-of P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Log P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n++Log : \u2200 (P : Proto){Q : Log P \u2192 Proto} (xs : Log P) \u2192 Log (Q xs) \u2192 Log (P >>= Q)\n++Log end _ ys = ys\n++Log (com' q M P) (x , xs) ys = x , ++Log (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com refl M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Log P \u2192 Proto)(R : Log (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Log P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com refl M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [inl: (\u03bb m \u2192 add\u03a3\u1d3e (P m)) ,inr: A\u1d3e ]\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e (M \u228e A) [inl: (\u03bb m \u2192 add\u03a0\u1d3e (P m)) ,inr: A\u1d3e ]\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n dual-add\u03a3\u1d3e : \u2200 P \u2192 dual (add\u03a3\u1d3e A\u1d3e P) \u2261\u1d3e add\u03a0\u1d3e (dual \u2218 A\u1d3e) (dual P)\n dual-add\u03a3\u1d3e end = end\n dual-add\u03a3\u1d3e (\u03a0\u1d3e M P) = com refl M (\u03bb m \u2192 dual-add\u03a3\u1d3e (P m))\n dual-add\u03a3\u1d3e (\u03a3\u1d3e M P) = com refl (M \u228e A) [inl: (\u03bb m \u2192 dual-add\u03a3\u1d3e (P m))\n ,inr: (\u03bb x \u2192 \u2261\u1d3e-refl (dual (A\u1d3e x))) ]\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Log P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n\nlift\u1d3e : \u2200 a {\u2113} \u2192 Proto_ \u2113 \u2192 Proto_ (a \u2294 \u2113)\nlift\u1d3e a end = end\nlift\u1d3e a (com' io M P) = com' io (Lift {_} {a} M) \u03bb m \u2192 lift\u1d3e a (P (lower m))\n\nlift-proc : \u2200 a {\u2113} (P : Proto_ \u2113) \u2192 \u27e6 P \u27e7 \u2192 \u27e6 lift\u1d3e a P \u27e7\nlift-proc a {\u2113} P0 p0 = lift-view (view-proc P0 p0)\n where\n lift-view : \u2200 {P : Proto_ \u2113}{p : \u27e6 P \u27e7} \u2192 ViewProc P p \u2192 \u27e6 lift\u1d3e a P \u27e7\n lift-view (send M P m p) = lift m , lift-proc _ (P m) p\n lift-view (recv M P x) = \u03bb { (lift m) \u2192 lift-proc _ (P m) (x m) }\n lift-view end = end\n\nmodule MonoMobility (P : Proto\u2080) where\n Com\u1d3e : Proto\u2080\n Com\u1d3e = \u03a0\u1d3e \u27e6 P \u27e7 \u03bb p \u2192\n \u03a0\u1d3e \u27e6 P \u22a5\u27e7 \u03bb p\u22a5 \u2192\n \u03a3\u1d3e (Log P) \u03bb log \u2192\n end\n com-proc : \u27e6 Com\u1d3e \u27e7\n com-proc p p\u22a5 = tele-com P p p\u22a5 , _\n\nmodule PolyMobility where\n Com\u1d3e : Proto_ \u2081\n Com\u1d3e = \u03a0\u1d3e Proto\u2080 \u03bb P \u2192\n lift\u1d3e \u2081 (MonoMobility.Com\u1d3e P)\n com-proc : \u27e6 Com\u1d3e \u27e7\n com-proc P = lift-proc \u2081 (MonoMobility.Com\u1d3e P) (MonoMobility.com-proc P)\n\nmodule PolyMobility' where\n Com\u1d3e : Proto_ \u2081\n Com\u1d3e = \u03a0\u1d3e Proto\u2080 \u03bb P \u2192\n \u03a0\u1d3e (Lift \u27e6 P \u27e7) \u03bb p \u2192\n \u03a0\u1d3e (Lift \u27e6 P \u22a5\u27e7) \u03bb p\u22a5 \u2192\n \u03a3\u1d3e (Lift (Log P)) \u03bb log \u2192\n end\n com-proc : \u27e6 Com\u1d3e \u27e7\n com-proc P (lift p) (lift p\u22a5) = lift (tele-com P p p\u22a5) , _\n\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Log : Choreo I \u2192 Proto\n \u2102Log \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Log-IsSource : \u2200 \u2102 \u2192 IsSource (\u2102Log \u2102)\n \u2102Log-IsSource (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Log-IsSource (\u2102 m)\n \u2102Log-IsSource (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Log-IsSource (\u2102 m)\n \u2102Log-IsSource end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Log \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Log \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Log \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Log \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n\n {-\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\ndata Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n-}\n\n{-\nEl : (P : Proto) \u2192 (Log P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind {{_ : FunExt}} where\n\n El->>= : (P : Proto){Q : Log P \u2192 Proto}{X : Log (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Log P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n-}\n\n>>=-com : (P : Proto){Q : Log P \u2192 Proto}{R : Log P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Log P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Log P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\nmodule ClientServerV1 (Query : \u2605\u2080) (Resp : Query \u2192 \u2605\u2080) (P : Proto) where\n Client : \u2115 \u2192 Proto\n Client zero = P\n Client (suc n) = \u03a3\u1d3e Query \u03bb q \u2192 \u03a0\u1d3e (Resp q) \u03bb r \u2192 Client n\n\n Server : \u2115 \u2192 Proto\n Server zero = P\n Server (suc n) = \u03a0\u1d3e Query \u03bb q \u2192 \u03a3\u1d3e (Resp q) \u03bb r \u2192 Server n\n\nmodule ClientServerV2 (Query : \u2605\u2080) (Resp : Query \u2192 \u2605\u2080) where\n ClientRound ServerRound : Proto\n ClientRound = \u03a3\u1d3e Query \u03bb q \u2192 \u03a0\u1d3e (Resp q) \u03bb r \u2192 end\n ServerRound = dual ClientRound\n\n Client Server : \u2115 \u2192 Proto\n Client n = replicate\u1d3e n ClientRound\n Server = dual \u2218 Client\n\n DynamicServer StaticServer : Proto\n DynamicServer = \u03a0\u1d3e \u2115 \u03bb n \u2192\n Server n\n StaticServer = \u03a3\u1d3e \u2115 \u03bb n \u2192\n Server n\n\n module PureServer (serve : \u03a0 Query Resp) where\n server : \u2200 n \u2192 \u27e6 Server n \u27e7\n server zero = _\n server (suc n) q = serve q , server n\n\nmodule _ {{_ : FunExt}} where\n dual-Log : \u2200 P \u2192 Log (dual P) \u2261 Log P\n dual-Log P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound (source-of-dual-oblivious P))\n\ndual->> : \u2200 P Q \u2192 dual (P >> Q) \u2261\u1d3e dual P >> dual Q\ndual->> end Q = \u2261\u1d3e-refl _\ndual->> (\u03a0\u1d3e M P) Q = com refl M (\u03bb m \u2192 dual->> (P m) Q)\ndual->> (\u03a3\u1d3e M P) Q = com refl M (\u03bb m \u2192 dual->> (P m) Q)\n\n {- ohoh!\n dual->>= : \u2200 P (Q : Log P \u2192 Proto) \u2192 dual (P >>= Q) \u2261\u1d3e dual P >>= (dual \u2218 Q \u2218 subst id (dual-Log P))\n dual->>= end Q = \u2261\u1d3e-refl _\n dual->>= (\u03a0\u1d3e M P) Q = ProtoRel.com refl M (\u03bb m \u2192 {!dual->>= (P m) (Q \u2218 _,_ m)!})\n dual->>= (\u03a3\u1d3e M P) Q = ProtoRel.com refl M (\u03bb m \u2192 {!!})\n -}\n\nmodule _ {{_ : FunExt}} (P : Proto) where\n dual-replicate\u1d3e : \u2200 n \u2192 dual (replicate\u1d3e n P) \u2261\u1d3e replicate\u1d3e n (dual P)\n dual-replicate\u1d3e zero = end\n dual-replicate\u1d3e (suc n) = dual->> P (replicate\u1d3e n P) \u2219\u1d3e \u2261\u1d3e-cong (_>>_ (dual P)) (dual-replicate\u1d3e n)\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecv\u2610 : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecv\u2610 = recv \u2218\u2032 un\u2610\n\nsend\u2610 : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsend\u2610 m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\n{-\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n-}\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : LR \u2192 \u2605_ a} (l : A `L)(r : A `R) \u2192 (lr : LR) \u2192 A lr\n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a0\u1d3e LR [L: l R: r ]\n\nmodule _ {P Q R S} where\n \u2295\u1d3e-map : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map f g (`L , pr) = `L , f pr\n \u2295\u1d3e-map f g (`R , pr) = `R , g pr\n\n &\u1d3e-map : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map f g p `L = f (p `L)\n &\u1d3e-map f g p `R = g (p `R)\n\nmodule _ {P Q} where\n \u2295\u1d3e\u2192\u228e : \u27e6 P \u2295\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u228e \u27e6 Q \u27e7\n \u2295\u1d3e\u2192\u228e (`L , p) = inl p\n \u2295\u1d3e\u2192\u228e (`R , q) = inr q\n\n \u228e\u2192\u2295\u1d3e : \u27e6 P \u27e7 \u228e \u27e6 Q \u27e7 \u2192 \u27e6 P \u2295\u1d3e Q \u27e7\n \u228e\u2192\u2295\u1d3e (inl p) = `L , p\n \u228e\u2192\u2295\u1d3e (inr q) = `R , q\n\n \u228e\u2192\u2295\u1d3e\u2192\u228e : \u2200 x \u2192 \u228e\u2192\u2295\u1d3e (\u2295\u1d3e\u2192\u228e x) \u2261 x\n \u228e\u2192\u2295\u1d3e\u2192\u228e (`L , _) = refl\n \u228e\u2192\u2295\u1d3e\u2192\u228e (`R , _) = refl\n\n \u2295\u1d3e\u2192\u228e\u2192\u2295\u1d3e : \u2200 x \u2192 \u2295\u1d3e\u2192\u228e (\u228e\u2192\u2295\u1d3e x) \u2261 x\n \u2295\u1d3e\u2192\u228e\u2192\u2295\u1d3e (inl _) = refl\n \u2295\u1d3e\u2192\u228e\u2192\u2295\u1d3e (inr _) = refl\n\n \u2295\u1d3e\u2243\u228e : \u27e6 P \u2295\u1d3e Q \u27e7 \u2243 \u27e6 P \u27e7 \u228e \u27e6 Q \u27e7\n \u2295\u1d3e\u2243\u228e = \u2295\u1d3e\u2192\u228e , record { linv = \u228e\u2192\u2295\u1d3e ; is-linv = \u228e\u2192\u2295\u1d3e\u2192\u228e ; rinv = \u228e\u2192\u2295\u1d3e ; is-rinv = \u2295\u1d3e\u2192\u228e\u2192\u2295\u1d3e }\n\n &\u1d3e\u2192\u00d7 : \u27e6 P &\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n &\u1d3e\u2192\u00d7 p = p `L , p `R\n\n \u00d7\u2192&\u1d3e : \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P &\u1d3e Q \u27e7\n \u00d7\u2192&\u1d3e (p , q) `L = p\n \u00d7\u2192&\u1d3e (p , q) `R = q\n\n &\u1d3e\u2192\u00d7\u2192&\u1d3e : \u2200 x \u2192 &\u1d3e\u2192\u00d7 (\u00d7\u2192&\u1d3e x) \u2261 x\n &\u1d3e\u2192\u00d7\u2192&\u1d3e (p , q) = refl\n\n module _ {{_ : FunExt}} where\n \u00d7\u2192&\u1d3e\u2192\u00d7 : \u2200 x \u2192 \u00d7\u2192&\u1d3e (&\u1d3e\u2192\u00d7 x) \u2261 x\n \u00d7\u2192&\u1d3e\u2192\u00d7 p = funExt \u03bb { `L \u2192 refl ; `R \u2192 refl }\n\n &\u1d3e\u2243\u00d7 : \u27e6 P &\u1d3e Q \u27e7 \u2243 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n &\u1d3e\u2243\u00d7 = &\u1d3e\u2192\u00d7 , record { linv = \u00d7\u2192&\u1d3e ; is-linv = \u00d7\u2192&\u1d3e\u2192\u00d7 ; rinv = \u00d7\u2192&\u1d3e ; is-rinv = &\u1d3e\u2192\u00d7\u2192&\u1d3e }\n\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP\u1d9c >>\u1d9c S = record P\u1d9c { P = \u03bb m \u2192 S (P m) }\n where open Com_ P\u1d9c\n\nmodule _ where\n\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n \u03a0\u1d3e M P \u214b\u1d3e Q = \u03a0\u1d3e M \u03bb m \u2192 P m \u214b\u1d3e Q\n P \u214b\u1d3e end = P\n P \u214b\u1d3e \u03a0\u1d3e M Q = \u03a0\u1d3e M \u03bb m \u2192 P \u214b\u1d3e Q m\n \u03a3\u1d3e M P \u214b\u1d3e \u03a3\u1d3e M' Q = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 P m \u214b\u1d3e \u03a3\u1d3e M' Q)\n ,inr: (\u03bb m' \u2192 \u03a3\u1d3e M P \u214b\u1d3e Q m') ]\n _\u2297\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u2297\u1d3e Q = Q\n \u03a3\u1d3e M P \u2297\u1d3e Q = \u03a3\u1d3e M \u03bb m \u2192 P m \u2297\u1d3e Q\n P \u2297\u1d3e end = P\n P \u2297\u1d3e \u03a3\u1d3e M Q = \u03a3\u1d3e M \u03bb m \u2192 P \u2297\u1d3e Q m\n \u03a0\u1d3e M P \u2297\u1d3e \u03a0\u1d3e M' Q = \u03a0\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 P m \u2297\u1d3e \u03a0\u1d3e M' Q)\n ,inr: (\u03bb m' \u2192 \u03a0\u1d3e M P \u2297\u1d3e Q m') ]\n\n \u2297\u214b-dual : \u2200 P Q \u2192 dual (P \u214b\u1d3e Q) \u2261\u1d3e dual P \u2297\u1d3e dual Q\n \u2297\u214b-dual end Q = \u2261\u1d3e-refl _\n \u2297\u214b-dual (\u03a0\u1d3e M P) Q = com refl M \u03bb m \u2192 \u2297\u214b-dual (P m) _\n \u2297\u214b-dual (\u03a3\u1d3e M P) end = \u2261\u1d3e-refl _\n \u2297\u214b-dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) = com refl M' \u03bb m' \u2192 \u2297\u214b-dual (\u03a3\u1d3e M P) (Q m')\n \u2297\u214b-dual (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) = com refl (M \u228e M')\n [inl: (\u03bb m \u2192 \u2297\u214b-dual (P m) (\u03a3\u1d3e M' Q))\n ,inr: (\u03bb m' \u2192 \u2297\u214b-dual (\u03a3\u1d3e M P) (Q m'))\n ]\n\n data View-\u214b-proto : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end-X : \u2200 Q \u2192 View-\u214b-proto end Q\n recv-X : \u2200 {M}(P : M \u2192 Proto)Q \u2192 View-\u214b-proto (\u03a0\u1d3e M P) Q\n send-send : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto) \u2192 View-\u214b-proto (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q)\n send-recv : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto) \u2192 View-\u214b-proto (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q)\n send-end : \u2200 {M}(P : M \u2192 Proto) \u2192 View-\u214b-proto (\u03a3\u1d3e M P) end\n\n view-\u214b-proto : \u2200 P Q \u2192 View-\u214b-proto P Q\n view-\u214b-proto end Q = end-X Q\n view-\u214b-proto (\u03a0\u1d3e _ P) Q = recv-X P Q\n view-\u214b-proto (\u03a3\u1d3e _ P) end = send-end P\n view-\u214b-proto (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) = send-recv P Q\n view-\u214b-proto (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) = send-send P Q\n\n data View-\u2297-proto : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end-X : \u2200 Q \u2192 View-\u2297-proto end Q\n send-X : \u2200 {M}(P : M \u2192 Proto)Q \u2192 View-\u2297-proto (\u03a3\u1d3e M P) Q\n recv-recv : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto) \u2192 View-\u2297-proto (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q)\n recv-send : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto) \u2192 View-\u2297-proto (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q)\n recv-end : \u2200 {M}(P : M \u2192 Proto) \u2192 View-\u2297-proto (\u03a0\u1d3e M P) end\n\n view-\u2297-proto : \u2200 P Q \u2192 View-\u2297-proto P Q\n view-\u2297-proto end Q = end-X Q\n view-\u2297-proto (\u03a3\u1d3e _ P) Q = send-X P Q\n view-\u2297-proto (\u03a0\u1d3e _ P) end = recv-end P\n view-\u2297-proto (\u03a0\u1d3e _ P) (\u03a0\u1d3e _ Q) = recv-recv P Q\n view-\u2297-proto (\u03a0\u1d3e _ P) (\u03a3\u1d3e _ Q) = recv-send P Q\n\n -- the terminology used for the constructor follows the behavior of the combined process\n data View-\u214b : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n sendL' : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto)(m : M )(p : \u27e6 P m \u214b\u1d3e \u03a3\u1d3e M' Q \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p)\n sendR' : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto)(m' : M')(p : \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q m' \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m' , p)\n recvL' : \u2200 {M} (P : M \u2192 Proto) Q (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7)) \u2192 View-\u214b (\u03a0\u1d3e M P) Q p\n recvR' : \u2200 {M M'} (P : M \u2192 Proto) (Q : M' \u2192 Proto)(p : (m' : M') \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q m' \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p\n endL : \u2200 Q (p : \u27e6 Q \u27e7) \u2192 View-\u214b end Q p\n send : \u2200 {M}(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) end (m , p)\n\n view-\u214b : \u2200 P Q (p : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 View-\u214b P Q p\n view-\u214b end Q p = endL Q p\n view-\u214b (\u03a0\u1d3e M P) Q p = recvL' P Q p\n view-\u214b (\u03a3\u1d3e M P) end (m , p) = send P m p\n view-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = recvR' P Q p\n view-\u214b (com (mk Out M P)) (com (mk Out M' Q)) (inl x , p) = sendL' P Q x p\n view-\u214b (com (mk Out M P)) (com (mk Out M' Q)) (inr y , p) = sendR' P Q y p\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7 \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7\n \u214b\u1d3e-assoc end Q R s = s\n \u214b\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = \u214b\u1d3e-assoc (P m) _ _ (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , \u214b\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end p = p\n \u214b\u1d3e-rend (\u03a0\u1d3e _ P) p = \u03bb m \u2192 \u214b\u1d3e-rend (P m) (p m)\n \u214b\u1d3e-rend (\u03a3\u1d3e _ _) p = p\n\n \u214b\u1d3e-rend' : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u214b\u1d3e end \u27e7\n \u214b\u1d3e-rend' end p = p\n \u214b\u1d3e-rend' (\u03a0\u1d3e _ P) p = \u03bb m \u2192 \u214b\u1d3e-rend' (P m) (p m)\n \u214b\u1d3e-rend' (\u03a3\u1d3e _ _) p = p\n\n {-\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n -}\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (\u03a3\u1d3e M P) m p = inr m , p\n \u214b\u1d3e-sendR (\u03a0\u1d3e M P) m p = \u03bb x \u2192 \u214b\u1d3e-sendR (P x) m (p x)\n\n \u214b\u1d3e-isendR : \u2200 {M'} P Q \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e M' Q \u27e7 \u2192 (m' : M') \u2192 \u27e6 P \u214b\u1d3e Q m' \u27e7\n \u214b\u1d3e-isendR end Q s m' = s m'\n \u214b\u1d3e-isendR (\u03a0\u1d3e M P) Q s m' = \u03bb m \u2192 \u214b\u1d3e-isendR (P m) Q (s m) m'\n \u214b\u1d3e-isendR (\u03a3\u1d3e M P) Q s m' = s m'\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL {P = P} (\u03a0\u1d3e M Q) m p = \u03bb m' \u2192 \u214b\u1d3e-sendL (Q m') m (\u214b\u1d3e-isendR (P m) _ p m')\n \u214b\u1d3e-sendL (\u03a3\u1d3e M Q) m p = inl m , p\n\n\n \u214b\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e M Q \u27e7\n \u214b\u1d3e-recvR end Q s = s\n \u214b\u1d3e-recvR (\u03a0\u1d3e M P) Q s = \u03bb x \u2192 \u214b\u1d3e-recvR (P x) Q (\u03bb m \u2192 s m x)\n \u214b\u1d3e-recvR (\u03a3\u1d3e M P) Q s = s\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id end = end\n \u214b\u1d3e-id (\u03a0\u1d3e M P) = \u03bb x \u2192 \u214b\u1d3e-sendL (P x) x (\u214b\u1d3e-id (P x))\n \u214b\u1d3e-id (\u03a3\u1d3e M P) = \u03bb x \u2192 \u214b\u1d3e-sendR (dual (P x)) x (\u214b\u1d3e-id (P x))\n\n{- Useless\n \u214b\u1d3e-recvL : \u2200 {M}{P : M \u2192 Proto}Q \u2192 ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7) \u2192 \u27e6 \u03a0\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-recvL Q f x = f x\n-}\n\n data View-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u2605\u2081 where\n sendLL : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto) R (m : M)(p : \u27e6 P m \u214b\u1d3e \u03a3\u1d3e _ Q \u27e7)(q : \u27e6 dual (\u03a3\u1d3e _ Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e M P) (\u03a3\u1d3e _ Q) R (inl m , p) q\n recvLL : \u2200 {M} (P : M \u2192 Proto) Q R\n (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7))(q : \u27e6 dual Q \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a0\u1d3e M P) Q R p q\n recvR-sendR : \u2200 {M M'}P(Q : M \u2192 Proto)(R : M' \u2192 Proto)(m : M')(p : \u27e6 com P \u214b\u1d3e \u03a0\u1d3e _ Q \u27e7)(q : \u27e6 dual (\u03a0\u1d3e _ Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (\u03a0\u1d3e _ Q) (\u03a3\u1d3e _ R) p (inr m , q)\n recvRR : \u2200 {MP MQ MR}(P : MP \u2192 Proto)(Q : MQ \u2192 Proto)(R : MR \u2192 Proto)\n (p : \u27e6 \u03a3\u1d3e _ P \u214b\u1d3e \u03a0\u1d3e _ Q \u27e7)(q : (m : MR) \u2192 \u27e6 dual (\u03a0\u1d3e _ Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) (\u03a0\u1d3e _ R) p q\n sendR-recvL : \u2200 {MP MQ}(P : MP \u2192 Proto)(Q : MQ \u2192 Proto)R(m : MQ)\n (p : \u27e6 \u03a3\u1d3e _ P \u214b\u1d3e Q m \u27e7)(q : (m : MQ) \u2192 \u27e6 dual (Q m) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) R (inr m , p) q\n recvR-sendL : \u2200 {MP MQ MR}(P : MP \u2192 Proto)(Q : MQ \u2192 Proto)(R : MR \u2192 Proto)\n (p : (m : MQ) \u2192 \u27e6 \u03a3\u1d3e _ P \u214b\u1d3e Q m \u27e7)(m : MQ)(q : \u27e6 dual (Q m) \u214b\u1d3e \u03a3\u1d3e _ R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) (\u03a3\u1d3e _ R) p (inl m , q)\n endL : \u2200 Q R\n \u2192 (q : \u27e6 Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 end Q R q qr\n sendLM : \u2200 {MP}(P : MP \u2192 Proto)R\n (m : MP)(p : \u27e6 P m \u27e7)(r : \u27e6 R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e _ P) end R (m , p) r\n recvL-sendR : \u2200 {MP MQ}(P : MP \u2192 Proto)(Q : MQ \u2192 Proto)\n (m : MQ)(p : \u2200 m \u2192 \u27e6 \u03a3\u1d3e _ P \u214b\u1d3e Q m \u27e7)(q : \u27e6 dual (Q m) \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) end p (m , q)\n\n view-\u2218 : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7) \u2192 View-\u2218 P Q R pq qr\n view-\u2218 P Q R pq qr = view-\u2218-view (view-\u214b P Q pq) (view-\u214b (dual Q) R qr)\n where\n view-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u214b P Q pq \u2192 View-\u214b (dual Q) R qr \u2192 View-\u2218 P Q R pq qr\n view-\u2218-view (sendL' _ _ _ _) _ = sendLL _ _ _ _ _ _\n view-\u2218-view (recvL' _ _ _) _ = recvLL _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) _ = sendR-recvL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendL' ._ _ _ _) = recvR-sendL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendR' ._ _ _ _) = recvR-sendR _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (send ._ _ _) = recvL-sendR _ _ _ _ _\n view-\u2218-view (endL _ _) _ = endL _ _ _ _\n view-\u2218-view (send _ _ _) _ = sendLM _ _ _ _ _\n\n \u214b\u1d3e-apply : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply end Q s p = s\n \u214b\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = \u214b\u1d3e-apply (P m) Q (s m) p\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = \u214b\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = \u214b\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , \u214b\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n\n dist-\u214b-fst : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e (Q &\u1d3e R) \u27e7 \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n dist-\u214b-fst (\u03a0\u1d3e _ P) Q R p = \u03bb m \u2192 dist-\u214b-fst (P m) Q R (p m)\n dist-\u214b-fst (\u03a3\u1d3e _ P) Q R p = p `L\n dist-\u214b-fst end Q R p = p `L\n\n dist-\u214b-snd : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e (Q &\u1d3e R) \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n dist-\u214b-snd (\u03a0\u1d3e _ P) Q R p = \u03bb m \u2192 dist-\u214b-snd (P m) Q R (p m)\n dist-\u214b-snd (\u03a3\u1d3e _ P) Q R p = p `R\n dist-\u214b-snd end Q R p = p `R\n\n dist-\u214b-\u00d7 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e (Q &\u1d3e R) \u27e7 \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u00d7 \u27e6 P \u214b\u1d3e R \u27e7\n dist-\u214b-\u00d7 P Q R p = dist-\u214b-fst P Q R p , dist-\u214b-snd P Q R p\n\n dist-\u214b-& : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e (Q &\u1d3e R) \u27e7 \u2192 \u27e6 (P \u214b\u1d3e Q) &\u1d3e (P \u214b\u1d3e R) \u27e7\n dist-\u214b-& P Q R p = \u00d7\u2192&\u1d3e (dist-\u214b-\u00d7 P Q R p)\n\n factor-,-\u214b : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q &\u1d3e R) \u27e7\n factor-,-\u214b end Q R pq pr = \u00d7\u2192&\u1d3e (pq , pr)\n factor-,-\u214b (\u03a0\u1d3e _ P) Q R pq pr = \u03bb m \u2192 factor-,-\u214b (P m) Q R (pq m) (pr m)\n factor-,-\u214b (\u03a3\u1d3e _ P) Q R pq pr = [L: pq R: pr ]\n\n factor-\u00d7-\u214b : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u00d7 \u27e6 P \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q &\u1d3e R) \u27e7\n factor-\u00d7-\u214b P Q R (p , q) = factor-,-\u214b P Q R p q\n\n factor-&-\u214b : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) &\u1d3e (P \u214b\u1d3e R) \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q &\u1d3e R) \u27e7\n factor-&-\u214b P Q R p = factor-\u00d7-\u214b P Q R (&\u1d3e\u2192\u00d7 p)\n\n module _ {{_ : FunExt}} where\n dist-\u214b-fst-factor-&-, : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7)(pr : \u27e6 P \u214b\u1d3e R \u27e7)\n \u2192 dist-\u214b-fst P Q R (factor-,-\u214b P Q R pq pr) \u2261 pq\n dist-\u214b-fst-factor-&-, (\u03a0\u1d3e _ P) Q R pq pr = funExt \u03bb m \u2192 dist-\u214b-fst-factor-&-, (P m) Q R (pq m) (pr m)\n dist-\u214b-fst-factor-&-, (\u03a3\u1d3e _ P) Q R pq pr = refl\n dist-\u214b-fst-factor-&-, end Q R pq pr = refl\n\n dist-\u214b-snd-factor-&-, : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7)(pr : \u27e6 P \u214b\u1d3e R \u27e7)\n \u2192 dist-\u214b-snd P Q R (factor-,-\u214b P Q R pq pr) \u2261 pr\n dist-\u214b-snd-factor-&-, (\u03a0\u1d3e _ P) Q R pq pr = funExt \u03bb m \u2192 dist-\u214b-snd-factor-&-, (P m) Q R (pq m) (pr m)\n dist-\u214b-snd-factor-&-, (\u03a3\u1d3e _ P) Q R pq pr = refl\n dist-\u214b-snd-factor-&-, end Q R pq pr = refl\n\n factor-\u00d7-\u214b-linv-dist-\u214b-\u00d7 : \u2200 P Q R \u2192 (factor-\u00d7-\u214b P Q R) LeftInverseOf (dist-\u214b-\u00d7 P Q R)\n factor-\u00d7-\u214b-linv-dist-\u214b-\u00d7 (\u03a0\u1d3e _ P) Q R p = funExt \u03bb m \u2192 factor-\u00d7-\u214b-linv-dist-\u214b-\u00d7 (P m) Q R (p m)\n factor-\u00d7-\u214b-linv-dist-\u214b-\u00d7 (\u03a3\u1d3e _ P) Q R p = funExt \u03bb { `L \u2192 refl ; `R \u2192 refl }\n factor-\u00d7-\u214b-linv-dist-\u214b-\u00d7 end Q R p = funExt \u03bb { `L \u2192 refl ; `R \u2192 refl }\n\n module _ P Q R where\n factor-\u00d7-\u214b-rinv-dist-\u214b-\u00d7 : (factor-\u00d7-\u214b P Q R) RightInverseOf (dist-\u214b-\u00d7 P Q R)\n factor-\u00d7-\u214b-rinv-dist-\u214b-\u00d7 (x , y) = cong\u2082 _,_ (dist-\u214b-fst-factor-&-, P Q R x y) (dist-\u214b-snd-factor-&-, P Q R x y)\n\n dist-\u214b-\u00d7-\u2243 : \u27e6 P \u214b\u1d3e (Q &\u1d3e R) \u27e7 \u2243 \u27e6 P \u214b\u1d3e Q \u27e7 \u00d7 \u27e6 P \u214b\u1d3e R \u27e7\n dist-\u214b-\u00d7-\u2243 = dist-\u214b-\u00d7 P Q R\n , record { linv = factor-\u00d7-\u214b P Q R; is-linv = factor-\u00d7-\u214b-linv-dist-\u214b-\u00d7 P Q R\n ; rinv = factor-\u00d7-\u214b P Q R; is-rinv = factor-\u00d7-\u214b-rinv-dist-\u214b-\u00d7 }\n\n dist-\u214b-&-\u2243 : \u27e6 P \u214b\u1d3e (Q &\u1d3e R) \u27e7 \u2243 \u27e6 (P \u214b\u1d3e Q) &\u1d3e (P \u214b\u1d3e R) \u27e7\n dist-\u214b-&-\u2243 = dist-\u214b-\u00d7-\u2243 \u2243-\u2219 \u2243-! &\u1d3e\u2243\u00d7\n\nmodule _ {{_ : FunExt}} where\n \u214b\u1d3e-apply' : \u2200 {P Q} \u2192 \u27e6 dual P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply' {P} {Q} pq p = \u214b\u1d3e-apply (dual P) Q pq (subst \u27e6_\u27e7 (\u2261.sym (\u2261\u1d3e-sound (dual-involutive P))) p)\n\n -- left-biased \u201cstrategy\u201d\n par : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par P Q p q = par-view (view-proc P p)\n where par-view : \u2200 {P} {p : \u27e6 P \u27e7} \u2192 ViewProc P p \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par-view (send M P m p) = \u214b\u1d3e-sendL Q m (par (P m) Q p q)\n par-view (recv M P p) = \u03bb m \u2192 par (P m) Q (p m) q\n par-view end = q\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = \u214b\u1d3e-\u2218-view (view-\u2218 P Q R pq qr)\n where\n \u214b\u1d3e-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u2218 P Q R pq qr \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218-view (sendLL P Q R m p qr) = \u214b\u1d3e-sendL R m (\u214b\u1d3e-\u2218 (P m) (\u03a3\u1d3e _ Q) R p qr)\n \u214b\u1d3e-\u2218-view (recvLL P Q R p qr) = \u03bb m \u2192 \u214b\u1d3e-\u2218 (P m) Q R (p m) qr\n \u214b\u1d3e-\u2218-view (recvR-sendR P Q R m pq q) = \u214b\u1d3e-sendR (com P) m (\u214b\u1d3e-\u2218 (com P) (\u03a0\u1d3e _ Q) (R m) pq q)\n \u214b\u1d3e-\u2218-view (recvRR P Q R pq q) = \u03bb m \u2192 \u214b\u1d3e-\u2218 (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) (R m) pq (q m)\n \u214b\u1d3e-\u2218-view (sendR-recvL P Q R m p q) = \u214b\u1d3e-\u2218 (\u03a3\u1d3e _ P) (Q m) R p (q m)\n \u214b\u1d3e-\u2218-view (recvR-sendL P Q R p m q) = \u214b\u1d3e-\u2218 (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) (p m) q\n \u214b\u1d3e-\u2218-view (endL Q R pq qr) = \u214b\u1d3e-apply' {Q} {R} qr pq\n \u214b\u1d3e-\u2218-view (sendLM P R m pq qr) = \u214b\u1d3e-sendL R m (par (P m) R pq qr)\n \u214b\u1d3e-\u2218-view (recvL-sendR P Q m pq qr) = \u214b\u1d3e-\u2218 (\u03a3\u1d3e _ P) (Q m) end (pq m) (\u214b\u1d3e-rend' (dual (Q m)) qr)\n\n mutual\n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm P Q p = \u214b\u1d3e-comm-view (view-\u214b P Q p)\n\n {-\n \u214b\u1d3e-comm-\u03a0 : \u2200 P {N}(Q : N \u2192 Proto) \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e N Q \u27e7 \u2192 \u03a0 N \u03bb n \u2192 \u27e6 Q n \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm-\u03a0 end Q p n = \u214b\u1d3e-rend' (Q n) (p n)\n \u214b\u1d3e-comm-\u03a0 (\u03a0\u1d3e _ P) Q p n = ?\n \u214b\u1d3e-comm-\u03a0 (\u03a3\u1d3e _ P) Q p n = \u214b\u1d3e-comm (\u03a3\u1d3e _ P) (Q n) (p n)\n -}\n\n \u214b\u1d3e-comm-view : \u2200 {P Q} {pq : \u27e6 P \u214b\u1d3e Q \u27e7} \u2192 View-\u214b P Q pq \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm-view (sendL' P Q m p) = \u214b\u1d3e-sendR (\u03a3\u1d3e _ Q) m (\u214b\u1d3e-comm (P m) (com (mk Out _ Q)) p)\n \u214b\u1d3e-comm-view (sendR' P Q m' p) = inl m' , \u214b\u1d3e-comm (\u03a3\u1d3e _ P) (Q m') p\n \u214b\u1d3e-comm-view (recvL' P Q pq) = \u214b\u1d3e-recvR Q P \u03bb m \u2192 \u214b\u1d3e-comm (P m) Q (pq m)\n \u214b\u1d3e-comm-view (recvR' P Q pq) = \u03bb m' \u2192 \u214b\u1d3e-comm (\u03a3\u1d3e _ P) (Q m') (pq m')\n \u214b\u1d3e-comm-view (endL Q pq) = \u214b\u1d3e-rend' Q pq\n \u214b\u1d3e-comm-view (send P m pq) = m , pq\n\n \u214b\u1d3e-comm-equiv : \u2200 P Q \u2192 Equiv (\u214b\u1d3e-comm P Q)\n \u214b\u1d3e-comm-equiv P Q = record { linv = \u214b\u1d3e-comm Q P ; is-linv = {!!} ; rinv = \u214b\u1d3e-comm Q P ; is-rinv = {!!} }\n where\n toto : \u2200 {P Q}{pq : \u27e6 P \u214b\u1d3e Q \u27e7} \u2192 View-\u214b P Q pq \u2192 \u214b\u1d3e-comm Q P (\u214b\u1d3e-comm P Q pq) \u2261 pq\n toto (sendL' P Q m p) = cong (_,_ (inl m)) (toto (view-\u214b (P m) (\u03a3\u1d3e _ Q) p))\n toto (sendR' P Q m' p) = cong (_,_ (inr m')) (toto (view-\u214b (\u03a3\u1d3e _ P) (Q m') p))\n toto (recvL' P Q pq) = funExt \u03bb m \u2192 {!MAKE A LEMMA!}\n toto (recvR' P Q pq) = funExt \u03bb m \u2192 toto (view-\u214b (\u03a3\u1d3e _ P) (Q m) (pq m))\n toto (endL end p) = refl\n toto (endL (\u03a0\u1d3e _ Q) p) = funExt \u03bb m \u2192 toto (view-\u214b end (Q m) (p m))\n toto (endL (\u03a3\u1d3e _ Q) p) = refl\n toto (send P m p) = refl\n\n \u214b\u1d3e-comm-\u2248 : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm-\u2248 P Q = _ , \u214b\u1d3e-comm-equiv P Q\n\n comma\u1d3e : \u2200 {P Q} \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P \u2297\u1d3e Q \u27e7\n comma\u1d3e {end} {Q} p q = q\n comma\u1d3e {\u03a3\u1d3e M P} {Q} (m , p) q = m , comma\u1d3e {P m} p q\n comma\u1d3e {\u03a0\u1d3e M P} {end} p end = p\n comma\u1d3e {\u03a0\u1d3e M P} {\u03a3\u1d3e _ Q} p (m , q) = m , comma\u1d3e {\u03a0\u1d3e M P} {Q m} p q\n {-\n comma\u1d3e {\u03a0\u1d3e M P} {\u03a0\u1d3e N Q} p q (inl m) = comma\u1d3e (P m) (\u03a0\u1d3e _ Q) (p m) q\n comma\u1d3e {\u03a0\u1d3e M P} {\u03a0\u1d3e N Q} p q (inr m') = comma\u1d3e (\u03a0\u1d3e _ P) (Q m') p (q m')\n -}\n comma\u1d3e {\u03a0\u1d3e M P} {\u03a0\u1d3e N Q} p q = [inl: (\u03bb m \u2192 comma\u1d3e {P m} {\u03a0\u1d3e _ Q} (p m) q)\n ,inr: (\u03bb m' \u2192 comma\u1d3e {\u03a0\u1d3e _ P} {Q m'} p (q m')) ]\n\n \u2297\u1d3e-fst : \u2200 P Q \u2192 \u27e6 P \u2297\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7\n \u2297\u1d3e-fst end Q pq = _\n \u2297\u1d3e-fst (\u03a3\u1d3e M P) Q (m , pq) = m , \u2297\u1d3e-fst (P m) Q pq\n \u2297\u1d3e-fst (\u03a0\u1d3e M P) end pq = pq\n \u2297\u1d3e-fst (\u03a0\u1d3e M P) (\u03a3\u1d3e _ Q) (_ , pq) = \u2297\u1d3e-fst (\u03a0\u1d3e M P) (Q _) pq\n \u2297\u1d3e-fst (\u03a0\u1d3e M P) (\u03a0\u1d3e N Q) pq = \u03bb m \u2192 \u2297\u1d3e-fst (P m) (\u03a0\u1d3e N Q) (pq (inl m))\n\n \u2297\u1d3e-snd : \u2200 P Q \u2192 \u27e6 P \u2297\u1d3e Q \u27e7 \u2192 \u27e6 Q \u27e7\n \u2297\u1d3e-snd end Q pq = pq\n \u2297\u1d3e-snd (\u03a3\u1d3e M P) Q (_ , pq) = \u2297\u1d3e-snd (P _) Q pq\n \u2297\u1d3e-snd (\u03a0\u1d3e M P) end pq = end\n \u2297\u1d3e-snd (\u03a0\u1d3e M P) (\u03a3\u1d3e _ Q) (m , pq) = m , \u2297\u1d3e-snd (\u03a0\u1d3e M P) (Q m) pq\n \u2297\u1d3e-snd (\u03a0\u1d3e M P) (\u03a0\u1d3e N Q) pq = \u03bb m \u2192 \u2297\u1d3e-snd (\u03a0\u1d3e M P) (Q m) (pq (inr m))\n\n \u2297\u1d3e-comma-fst : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 \u2297\u1d3e-fst P Q (comma\u1d3e {P} {Q} p q) \u2261 p\n \u2297\u1d3e-comma-fst end Q p q = refl\n \u2297\u1d3e-comma-fst (\u03a3\u1d3e M P) Q (m , p) q = \u03a3-ext refl (\u2297\u1d3e-comma-fst (P m) Q p q)\n \u2297\u1d3e-comma-fst (\u03a0\u1d3e M P) end p q = refl\n \u2297\u1d3e-comma-fst (\u03a0\u1d3e M P) (\u03a3\u1d3e _ Q) p (m , q) = \u2297\u1d3e-comma-fst (\u03a0\u1d3e _ P) (Q m) p q\n \u2297\u1d3e-comma-fst (\u03a0\u1d3e M P) (\u03a0\u1d3e N Q) p q = funExt \u03bb m \u2192 \u2297\u1d3e-comma-fst (P m) (\u03a0\u1d3e _ Q) (p m) q\n\n \u2297\u1d3e-comma-snd : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 \u2297\u1d3e-snd P Q (comma\u1d3e {P} {Q} p q) \u2261 q\n \u2297\u1d3e-comma-snd end Q p q = refl\n \u2297\u1d3e-comma-snd (\u03a3\u1d3e M P) Q (m , p) q = \u2297\u1d3e-comma-snd (P m) Q p q\n \u2297\u1d3e-comma-snd (\u03a0\u1d3e M P) end p q = refl\n \u2297\u1d3e-comma-snd (\u03a0\u1d3e M P) (\u03a3\u1d3e _ Q) p (m , q) = \u03a3-ext refl (\u2297\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p q)\n \u2297\u1d3e-comma-snd (\u03a0\u1d3e M P) (\u03a0\u1d3e N Q) p q = funExt \u03bb m \u2192 \u2297\u1d3e-comma-snd (\u03a0\u1d3e M P) (Q m) p (q m)\n\n module _ P Q where\n \u2297\u2192\u00d7 : \u27e6 P \u2297\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n \u2297\u2192\u00d7 pq = \u2297\u1d3e-fst P Q pq , \u2297\u1d3e-snd P Q pq\n\n \u00d7\u2192\u2297 : \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P \u2297\u1d3e Q \u27e7\n \u00d7\u2192\u2297 (p , q) = comma\u1d3e {P} {Q} p q\n\n \u2297\u2192\u00d7\u2192\u2297 : \u2200 P Q \u2192 (\u00d7\u2192\u2297 P Q) LeftInverseOf (\u2297\u2192\u00d7 P Q)\n foo : \u2200 M N P Q (x : M \u228e N)(pq : \u27e6 \u03a0\u1d3e M P \u2297\u1d3e \u03a0\u1d3e N Q \u27e7) \u2192\n \u00d7\u2192\u2297 (\u03a0\u1d3e M P) (\u03a0\u1d3e N Q) (\u2297\u2192\u00d7 (\u03a0\u1d3e M P) (\u03a0\u1d3e N Q) pq) x \u2261 pq x\n fooL : \u2200 (M : \u2605) N (P : M \u2192 Proto) Q (m : M) (pq : \u27e6 P m \u2297\u1d3e \u03a0\u1d3e N Q \u27e7) \u2192\n \u00d7\u2192\u2297 (P m) (\u03a0\u1d3e N Q) (\u2297\u2192\u00d7 (P m) (\u03a0\u1d3e N Q) pq) \u2261 pq\n\n \u2297\u2192\u00d7\u2192\u2297 end Q pq = refl\n \u2297\u2192\u00d7\u2192\u2297 (\u03a3\u1d3e M P) Q (m , pq) = cong (_,_ m) (\u2297\u2192\u00d7\u2192\u2297 (P m) Q pq)\n \u2297\u2192\u00d7\u2192\u2297 (\u03a0\u1d3e M P) end pq = refl\n \u2297\u2192\u00d7\u2192\u2297 (\u03a0\u1d3e M P) (\u03a3\u1d3e _ Q) (m , pq) = cong (_,_ m) (\u2297\u2192\u00d7\u2192\u2297 (\u03a0\u1d3e _ P) (Q m) pq)\n \u2297\u2192\u00d7\u2192\u2297 (\u03a0\u1d3e M P) (\u03a0\u1d3e N Q) pq = funExt (\u03bb m \u2192 foo M N P Q m pq) {-funExt \u03bb { (inl x) \u2192 {!!} \u2219 \u2297\u2192\u00d7\u2192\u2297 (P x) (\u03a0\u1d3e _ Q) (pq (inl x))\n ; (inr x) \u2192 {!!} \u2219 \u2297\u2192\u00d7\u2192\u2297 (\u03a0\u1d3e _ P) (Q x) (pq (inr x)) }-}\n\n fooL M N P Q m pq = \u2297\u2192\u00d7\u2192\u2297 (P m) (\u03a0\u1d3e _ Q) pq\n\n foo M N P Q (inl m) pq = {!STUCK!} \u2219 fooL M N P Q m (pq (inl m))\n foo M N P Q (inr m) pq = {!!}\n\n module _ P Q where\n \u2297\u2243\u00d7 : \u27e6 P \u2297\u1d3e Q \u27e7 \u2243 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n \u2297\u2243\u00d7 = \u2297\u2192\u00d7 P Q\n , record { linv = \u00d7\u2192\u2297 P Q ; is-linv = \u2297\u2192\u00d7\u2192\u2297 P Q\n ; rinv = \u00d7\u2192\u2297 P Q ; is-rinv = \u03bb { (x , y) \u2192 cong\u2082 _,_ (\u2297\u1d3e-comma-fst P Q x y) (\u2297\u1d3e-comma-snd P Q x y) } }\n\n {-\n end Q p = \u214b\u1d3e-rend' Q p\n \u214b\u1d3e-! end Q p = \u214b\u1d3e-rend' Q p\n \u214b\u1d3e-! (\u03a0\u1d3e M P) end p x = \u214b\u1d3e-! (P x) end (p x)\n \u214b\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 {!\u214b\u1d3e-! (\u03a0 !}\n \u214b\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 \u214b\u1d3e-! (P m) (com (mk Out M' Q)) (p m)\n \u214b\u1d3e-! (\u03a3\u1d3e M P) end p = p\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 \u214b\u1d3e-! (com (mk Out M P)) (Q m') (p m')\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (\u214b\u1d3e-! (P m) (com (mk Out M' Q)) p)\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (\u214b\u1d3e-! (com (mk Out M P)) (Q m) p)\n -}\n\nmodule _ {{_ : FunExt}} where\n \u03a0\ud835\udfd8-uniq : \u2200 (F G : \ud835\udfd8 \u2192 \u2605) \u2192 \u03a0 \ud835\udfd8 F \u2261 \u03a0 \ud835\udfd8 G\n \u03a0\ud835\udfd8-uniq F G = cong (\u03a0 \ud835\udfd8) (funExt (\u03bb()))\n\n{-\nA `\u2297 B 'times', context chooses how A and B are used\nA `\u214b B 'par', \"we\" chooses how A and B are used\nA `\u2295 B 'plus', select from A or B\nA `& B 'with', offer choice of A or B\n`! A 'of course!', server accept\n`? A 'why not?', client request\n`1 unit for `\u2297\n`\u22a4 unit for `\u214b\n`0 unit for `\u2295\n`\u22a5 unit for `&\n-}\ndata CLL : \u2605 where\n `1 `\u22a4 `0 `\u22a5 : CLL\n _`\u2297_ _`\u214b_ _`\u2295_ _`&_ : (A B : CLL) \u2192 CLL\n -- `!_ `?_ : (A : CLL) \u2192 CLL\n\n_\u22a5 : CLL \u2192 CLL\n`1 \u22a5 = `\u22a4\n`\u22a4 \u22a5 = `1\n`0 \u22a5 = `\u22a5\n`\u22a5 \u22a5 = `0\n(A `\u2297 B)\u22a5 = A \u22a5 `\u214b B \u22a5\n(A `\u214b B)\u22a5 = A \u22a5 `\u2297 B \u22a5\n(A `\u2295 B)\u22a5 = A \u22a5 `& B \u22a5\n(A `& B)\u22a5 = A \u22a5 `\u2295 B \u22a5\n{-\n(`! A)\u22a5 = `?(A \u22a5)\n(`? A)\u22a5 = `!(A \u22a5)\n-}\n\nCLL-proto : CLL \u2192 Proto\nCLL-proto `1 = end -- TODO\nCLL-proto `\u22a4 = end -- TODO\nCLL-proto `0 = \u03a0\u1d3e \ud835\udfd8 \u03bb()\nCLL-proto `\u22a5 = \u03a3\u1d3e \ud835\udfd8 \u03bb()\nCLL-proto (A `\u2297 B) = CLL-proto A \u2297\u1d3e CLL-proto B\nCLL-proto (A `\u214b B) = CLL-proto A \u214b\u1d3e CLL-proto B\nCLL-proto (A `\u2295 B) = CLL-proto A \u2295\u1d3e CLL-proto B\nCLL-proto (A `& B) = CLL-proto A &\u1d3e CLL-proto B\n\n{- The point of this could be to devise a particular equivalence\n relation for processes. It could properly deal with \u214b. -}\n\n {-\nmodule V4 {{_ : FunExt}} where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n P \u214b\u1d3e end = P\n com P \u214b\u1d3e com Q = \u03a3\u1d3e LR (P \u214b\u1d9c Q)\n\n _\u214b\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 \u214b\u1d9c P\u1d3f) `L = P\u1d38 \u214b\u1d9cL P\u1d3f\n (P\u1d38 \u214b\u1d9c P\u1d3f) `R = P\u1d38 \u214b\u1d9cR P\u1d3f\n\n _\u214b\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) \u214b\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m \u214b\u1d3e com Q)\n\n _\u214b\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P \u214b\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P \u214b\u1d3e P\u1d3f m)\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end ox\u1d3e Q = Q\n P ox\u1d3e end = P\n com P\u1d38 ox\u1d3e com P\u1d3f = \u03a0\u1d3e LR (P\u1d38 ox\u1d9c P\u1d3f)\n\n _ox\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 ox\u1d9c P\u1d3f) `L = P\u1d38 ox\u1d9cL P\u1d3f\n (P\u1d38 ox\u1d9c P\u1d3f) `R = P\u1d38 ox\u1d9cR P\u1d3f\n\n _ox\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) ox\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m ox\u1d3e com Q)\n\n _ox\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P ox\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P ox\u1d3e P\u1d3f m)\n\n data Viewox : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n com : \u2200{P Q}(p : \u27e6 \u03a0\u1d3e LR (P ox\u1d9c Q) \u27e7) \u2192 Viewox (com P) (com Q) p\n endL : \u2200{Q}(p : \u27e6 Q \u27e7) \u2192 Viewox end Q p\n endR : \u2200{P}(p : \u27e6 com P \u27e7) \u2192 Viewox (com P) end p\n\n viewox : \u2200 P Q (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 Viewox P Q p\n viewox end Q p = endL p\n viewox (com P) end p = endR p\n viewox (com P) (com Q) p = com p\n\n data View-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u2605\u2081 where\n sendLL : \u2200 {M} (P : M \u2192 Proto) Q R (m : M)(p : \u27e6 P m \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e M P) (com Q) R (`L , m , p) q\n recvLL : \u2200 {M} (P : M \u2192 Proto) Q R\n (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e com Q \u27e7))(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a0\u1d3e M P) (com Q) R (`L , p) q\n sendRR : \u2200 {M} P Q (R : M \u2192 Proto)(m : M)(p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a3\u1d3e M R) p (`R , m , q)\n recvRR : \u2200 {M} P Q (R : M \u2192 Proto)\n (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : (m : M) \u2192 \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a0\u1d3e M R) p (`R , q)\n\n sendR-recvL : \u2200 {M} P (Q : M \u2192 Proto) R (m : M)(p : \u27e6 com P \u214b\u1d3e Q m \u27e7)(q : (m : M) \u2192 \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a3\u1d3e M Q) (com R) (`R , m , p) (`L , q)\n recvR-sendL : \u2200 {M} P (Q : M \u2192 Proto) R (p : (m : M) \u2192 \u27e6 com P \u214b\u1d3e Q m \u27e7)(m : M)(q : \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a0\u1d3e M Q) (com R) (`R , p) (`L , m , q)\n\n endL : \u2200 Q R\n \u2192 (q : \u27e6 Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 end Q R q qr\n\n endM : \u2200 P R\n \u2192 (p : \u27e6 com P \u27e7)(r : \u27e6 R \u27e7)\n \u2192 View-\u2218 (com P) end R p r\n endR : \u2200 P Q\n \u2192 (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u27e7)\n \u2192 View-\u2218 (com P) (com Q) end p q\n\n view-\u2218 : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7) \u2192 View-\u2218 P Q R pq qr\n view-\u2218 P Q R pq qr = view-\u2218-view (view-\u214b P Q pq) (view-\u214b (dual Q) R qr)\n where\n view-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u214b P Q pq \u2192 View-\u214b (dual Q) R qr \u2192 View-\u2218 P Q R pq qr\n view-\u2218-view (sendL' _ _ _ _) _ = sendLL _ _ _ _ _ _\n view-\u2218-view (recvL' _ _ _) _ = recvLL _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvL' ._ _ _) = sendR-recvL _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (endR ._ _) = endR _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendL' ._ _ _ _) = recvR-sendL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (endR ._ ._) = endR _ _ _ _\n view-\u2218-view (endL _ _) _ = endL _ _ _ _\n view-\u2218-view (endR _ _) _ = endM _ _ _ _\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end = id\n \u214b\u1d3e-rend (com _) = id\n\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (com P) m p = `R , (m , p)\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL (com _) m p = `L , (m , p)\n\n \u214b\u1d3e-recvR : \u2200 {M}P{Q : M \u2192 Proto} \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e com' In M Q \u27e7\n \u214b\u1d3e-recvR end f = f\n \u214b\u1d3e-recvR (com _) f = `R , f\n\n \u214b\u1d3e-recvL : \u2200 {M}{P : M \u2192 Proto}Q \u2192 ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7) \u2192 \u27e6 \u03a0\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-recvL {P = P} end f x = \u214b\u1d3e-rend (P x) (f x)\n \u214b\u1d3e-recvL (com _) f = `L , f\n\n ox\u1d3e-rend : \u2200 P \u2192 \u27e6 P ox\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-rend end = id\n ox\u1d3e-rend (com x) = id\n\n ox\u1d3e-sendR : \u2200 {M P}{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n ox\u1d3e-sendR {P = end} m p = m , p\n ox\u1d3e-sendR {P = com x} m p = `R , (m , p)\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id end = _\n \u214b\u1d3e-id (com (mk In M P)) = `R , \u03bb m \u2192 \u214b\u1d3e-sendL (P m) m (\u214b\u1d3e-id (P m))\n \u214b\u1d3e-id (com (mk Out M P)) = `L , \u03bb m \u2192 \u214b\u1d3e-sendR (dual (P m)) m (\u214b\u1d3e-id (P m))\n\n module _ where\n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm = \u03bb P Q \u2192 to P Q , equiv P Q\n where\n to : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n to end end pq = pq\n to end (com x) pq = pq\n to (com x) end pq = pq\n to (com (mk In M P)) (com x\u2081) (`L , pq) = `R , (\u03bb m \u2192 to (P m) (com x\u2081) (pq m))\n to (com (mk Out M P)) (com x\u2081) (`L , m , pq) = `R , m , to (P m) (com x\u2081) pq\n to (com x) (com (mk In M P)) (`R , pq) = `L , (\u03bb m \u2192 to (com x) (P m) (pq m))\n to (com x) (com (mk Out M P)) (`R , m , pq) = `L , m , to (com x) (P m) pq\n\n toto : \u2200 P Q (x : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 to Q P (to P Q x) \u2261 x\n toto end end x = refl\n toto end (com (mk io M P)) x\u2081 = refl\n toto (com (mk io M P)) end x\u2081 = refl\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' In M\u2081 P\u2081) (pq x))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' Out M\u2081 P\u2081) (pq x))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' In M\u2081 P\u2081) pq))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' Out M\u2081 P\u2081) pq))\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' In M P) (P\u2081 x) (pq x)))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' In M P) (P\u2081 m) pq))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' Out M P) (P\u2081 x) (pq x)))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' Out M P) (P\u2081 m) pq))\n\n equiv : \u2200 P Q \u2192 Equiv (to P Q)\n equiv P Q = record { linv = to Q P ; is-linv = toto P Q ; rinv = to Q P ; is-rinv = toto Q P }\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e end Q p q = q\n comma\u1d3e (com _) end p q = p\n comma\u1d3e (\u03a3\u1d3e M P) (com Q) (m , p) q `L = m , comma\u1d3e (P m) (com Q) p q\n comma\u1d3e (\u03a0\u1d3e M P) (com Q) p q `L = \u03bb m \u2192 comma\u1d3e (P m) (com Q) (p m) q\n comma\u1d3e (com P) (\u03a3\u1d3e M Q) p (m , q) `R = m , comma\u1d3e (com P) (Q m) p q\n comma\u1d3e (com P) (\u03a0\u1d3e M Q) p q `R = \u03bb m \u2192 comma\u1d3e (com P) (Q m) p (q m)\n\n \u00d7\u2192ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n \u00d7\u2192ox\u1d3e P Q (p , q) = comma\u1d3e P Q p q\n\n ox\u1d3e-fst : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-fst end Q pq = _\n ox\u1d3e-fst (com _) end pq = pq\n ox\u1d3e-fst (\u03a0\u1d3e M P) (com Q) pq = \u03bb m \u2192 ox\u1d3e-fst (P m) (com Q) (pq `L m)\n ox\u1d3e-fst (\u03a3\u1d3e M P) (com Q) pq = \u00d7-map id (\u03bb {x} \u2192 ox\u1d3e-fst (P x) (com Q)) (pq `L)\n\n ox\u1d3e-snd : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 Q \u27e7\n ox\u1d3e-snd end Q pq = pq\n ox\u1d3e-snd (com _) end pq = _\n ox\u1d3e-snd (com P) (\u03a0\u1d3e M' Q) pq = \u03bb m' \u2192 ox\u1d3e-snd (com P) (Q m') (pq `R m')\n ox\u1d3e-snd (com P) (\u03a3\u1d3e M' Q) pq = \u00d7-map id (\u03bb {m'} \u2192 ox\u1d3e-snd (com P) (Q m')) (pq `R)\n\n ox\u1d3e\u2192\u00d7 : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n ox\u1d3e\u2192\u00d7 P Q p = ox\u1d3e-fst P Q p , ox\u1d3e-snd P Q p\n\n ox\u1d3e-comma-fst : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-fst P Q (comma\u1d3e P Q p q) \u2261 p\n ox\u1d3e-comma-fst end Q p q = refl\n ox\u1d3e-comma-fst (com P) end p q = refl\n ox\u1d3e-comma-fst (com P) (com Q) p q with view-com P p\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | send P m p = cong (_,_ m) (ox\u1d3e-comma-fst (P m) (com Q) p q)\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | recv P p = funExt \u03bb m \u2192 ox\u1d3e-comma-fst (P m) (com Q) (p m) q\n\n ox\u1d3e-comma-snd : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-snd P Q (comma\u1d3e P Q p q) \u2261 q\n ox\u1d3e-comma-snd end Q p q = refl\n ox\u1d3e-comma-snd (com P) end p q = refl\n ox\u1d3e-comma-snd (com P) (com Q) p q with view-com Q q\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p (q m)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p (q m)\n\n {-\n ox\u1d3e\u2192\u00d7-rinv : \u2200 {P Q} (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 \u00d7\u2192ox\u1d3e P Q (ox\u1d3e\u2192\u00d7 P Q p) \u2261 p\n ox\u1d3e\u2192\u00d7-rinv {P} {Q} p with viewox P Q p\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk In M\u2081 P\u2081} .p = funExt \u03bb { `L \u2192 funExt \u03bb m \u2192 {!ox\u1d3e\u2192\u00d7-rinv {P m} {\u03a0\u1d3e _ P\u2081} ?!} ; `R \u2192 {!!} }\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk In M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | endL .p = refl\n ox\u1d3e\u2192\u00d7-rinv p | endR {P} .p = refl\n\n \u00d7\u2192ox\u1d3e-equiv : \u2200 P Q \u2192 Equiv (\u00d7\u2192ox\u1d3e P Q)\n \u00d7\u2192ox\u1d3e-equiv P Q = record { linv = ox\u1d3e\u2192\u00d7 P Q\n ; is-linv = \u03bb { (x , y) \u2192 cong\u2082 _,_ (ox\u1d3e-comma-fst P Q x y) (ox\u1d3e-comma-snd P Q x y) }\n ; rinv = ox\u1d3e\u2192\u00d7 P Q\n ; is-rinv = ox\u1d3e\u2192\u00d7-rinv {P} {Q} }\n -}\n\n \u214b\u1d3e-apply : \u2200 {P Q} \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply {P} {Q} pq p with view-\u214b P Q pq\n \u214b\u1d3e-apply ._ p | sendL' P Q m pq = \u214b\u1d3e-apply {P m} pq (p m)\n \u214b\u1d3e-apply ._ p | sendR' P Q m pq = m , \u214b\u1d3e-apply {com P} {Q m} pq p\n \u214b\u1d3e-apply ._ (m , p) | recvL' P Q pq = \u214b\u1d3e-apply {P m} (pq m) p\n \u214b\u1d3e-apply ._ p | recvR' P Q pq = \u03bb m \u2192 \u214b\u1d3e-apply {com P} {Q m} (pq m) p\n \u214b\u1d3e-apply pq p | endL Q .pq = pq\n \u214b\u1d3e-apply pq p | endR P .pq = _\n\n \u214b\u1d3e-apply' : \u2200 {P Q} \u2192 \u27e6 dual P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply' {P} {Q} pq p = \u214b\u1d3e-apply {dual P} {Q} pq (subst \u27e6_\u27e7 (\u2261.sym (\u2261\u1d3e-sound funExt (dual-involutive P))) p)\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = \u214b\u1d3e-\u2218-view (view-\u2218 P Q R pq qr)\n where\n \u214b\u1d3e-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u2218 P Q R pq qr \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218-view (sendLL P Q R m p qr) = \u214b\u1d3e-sendL R m (\u214b\u1d3e-\u2218 (P m) (com Q) R p qr)\n \u214b\u1d3e-\u2218-view (recvLL P Q R p qr) = \u214b\u1d3e-recvL R (\u03bb m \u2192 \u214b\u1d3e-\u2218 (P m) (com Q) R (p m) qr)\n \u214b\u1d3e-\u2218-view (sendRR P Q R m pq q) = \u214b\u1d3e-sendR (com P) m (\u214b\u1d3e-\u2218 (com P) (com Q) (R m) pq q)\n \u214b\u1d3e-\u2218-view (recvRR P Q R pq q) = \u214b\u1d3e-recvR (com P) (\u03bb m\u2081 \u2192 \u214b\u1d3e-\u2218 (com P) (com Q) (R m\u2081) pq (q m\u2081))\n \u214b\u1d3e-\u2218-view (sendR-recvL P Q R m p q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) p (q m)\n \u214b\u1d3e-\u2218-view (recvR-sendL P Q R p m q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) (p m) q\n \u214b\u1d3e-\u2218-view (endL Q R pq qr) = \u214b\u1d3e-apply' {Q} {R} qr pq\n \u214b\u1d3e-\u2218-view (endM P R pq qr) = par (com P) R pq qr\n \u214b\u1d3e-\u2218-view (endR P Q pq qr) = \u214b\u1d3e-apply {com Q} {com P} (fst (\u214b\u1d3e-comm (com P) (com Q)) pq) qr\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map P Q R S f g p = comma\u1d3e Q S (f (ox\u1d3e-fst P R p)) (g (ox\u1d3e-snd P R p))\n\n {-\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = switchL' P Q R (ox\u1d3e-fst (P \u214b\u1d3e Q) R pqr) (ox\u1d3e-snd (P \u214b\u1d3e Q) R pqr)\n -}\n\n {-\n switchL' : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7) (r : \u27e6 R \u27e7) \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL' P Q R pq r = {!switchL-view {!!}!}\n where\n switchL-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{r : \u27e6 R \u27e7} \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL-view {P} {Q} {R} {p\u214bq} {r} with view-\u214b P Q p\u214bq | view-proc R r\n switchL-view | sendL' P\u2081 Q\u2081 m p | vr = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | send M\u2081 P\u2082 m\u2081 p\u2081 = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | end = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | send M\u2081 P\u2082 m p\u2081 = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | end = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | send M\u2081 P\u2082 m p\u2081 = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | end = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | send M P\u2081 m p = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | recv M P\u2081 r\u2081 = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | end = {!!}\n switchL-view | endR P\u2081 pq\u2081 | send M P\u2082 m p = {!!}\n switchL-view | endR P\u2081 pq\u2081 | recv M P\u2082 r\u2081 = {!!}\n switchL-view | endR P\u2081 pq\u2081 | end = {!!}\n {-\n switchL-view {R = R}{r = r} | sendL' P Q m p = \u214b\u1d3e-sendL {!!} m (switchL' {!!} {!!} {!!} p r)\n switchL-view {R = R}{r = r} | sendR' P Q m p with view-proc R r\n switchL-view | sendR' {M\u2081} P\u2081 Q m\u2081 p\u2081 | send M P m p = {!!}\n switchL-view | sendR' {M\u2081} P\u2081 Q m p | recv M P r = {!!}\n switchL-view | sendR' {M} P Q m p | end = {!!}\n -- {!\u214b\u1d3e-map (com P) (com P) (Q m ox\u1d3e R) (\u03a3\u1d3e M Q ox\u1d3e R) id (ox\u1d3e-map (Q m) (\u03a3\u1d3e M Q) R R (_,_ m) id) (switchL-view (com P) (Q m) R p r)!}\n switchL-view {R = R}{r = r} | recvL' P Q p = \u214b\u1d3e-recvL (com Q ox\u1d3e R) \u03bb m \u2192 switchL' (P m) (com Q) R (p m) r\n switchL-view {R = R}{r = r} | recvR' (\u03a0\u1d9c M' P) Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL-view {R = R}{r = r} | recvR' (\u03a3\u1d9c M' P) Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL-view {R = R}{r = r} | endL Q p\u214bq = comma\u1d3e Q R p\u214bq r\n switchL-view {R = R}{r = r} | endR P p\u214bq = par (com P) R p\u214bq r\n -}\n -}\n -}\n\n {-\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Log P \u00d7 Log Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Log B \u00d7 Log E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _ {{_ : FunExt}} where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _ {{_ : FunExt}} where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _ {{_ : FunExt}}\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map; proj\u2081 to fst; proj\u2082 to snd)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_]) hiding ([_,_]\u2032)\nopen import Data.One hiding (_\u225f_)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP as \u2261\n\nmodule Control.Protocol.Choreography where\n\npostulate\n FunExt : \u2605\n funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 {{fe : FunExt}} \u2192 f \u2261 g\n\nContractible : \u2200 {a}{A : \u2605_ a}(x : A) \u2192 \u2605_ a\nContractible x = \u2200 y \u2192 x \u2261 y\n\nmodule Equivalences where\n\n record Equiv {A B : \u2605}(f : A \u2192 B) : \u2605 where\n field\n linv : B \u2192 A\n is-linv : \u2200 x \u2192 linv (f x) \u2261 x\n rinv : B \u2192 A\n is-rinv : \u2200 x \u2192 f (rinv x) \u2261 x\n\n module _ {A B : \u2605}{f : A \u2192 B}(f\u1d31 : Equiv f) where\n open Equiv f\u1d31\n inv : B \u2192 A\n inv = linv \u2218 f \u2218 rinv\n\n inv-equiv : Equiv inv\n inv-equiv = record { linv = f\n ; is-linv = \u03bb x \u2192 cong f (is-linv (rinv x)) \u2219 is-rinv x\n ; rinv = f\n ; is-rinv = \u03bb x \u2192 cong linv (is-rinv (f x)) \u2219 is-linv x }\n\n id\u1d31 : \u2200 {A} \u2192 Equiv {A} id\n id\u1d31 = record\n { linv = id\n ; is-linv = \u03bb _ \u2192 refl\n ; rinv = id\n ; is-rinv = \u03bb _ \u2192 refl\n }\n\n module _ {A B C}{g : B \u2192 C}{f : A \u2192 B} where\n _\u2218\u1d31_ : Equiv g \u2192 Equiv f \u2192 Equiv (g \u2218 f)\n g\u1d31 \u2218\u1d31 f\u1d31 = record { linv = F.linv \u2218 G.linv ; is-linv = \u03bb x \u2192 cong F.linv (G.is-linv (f x)) \u2219 F.is-linv x\n ; rinv = F.rinv \u2218 G.rinv ; is-rinv = \u03bb x \u2192 cong g (F.is-rinv _) \u2219 G.is-rinv x }\n where\n module G = Equiv g\u1d31\n module F = Equiv f\u1d31\n\n infix 0 _\u2243_\n _\u2243_ : \u2605 \u2192 \u2605 \u2192 \u2605\n A \u2243 B = \u03a3 (A \u2192 B) Equiv\n\n \u2243-refl : Reflexive _\u2243_\n \u2243-refl = _ , id\u1d31\n\n \u2243-sym : Symmetric _\u2243_\n \u2243-sym (_ , f\u1d31) = _ , inv-equiv f\u1d31\n\n \u2243-trans : Transitive _\u2243_\n \u2243-trans (_ , p) (_ , q) = _ , q \u2218\u1d31 p\n\n module Contractible\u2192Equiv {A : \u2605}{x : A}(x-contr : Contractible x) where\n const-equiv : Equiv {\ud835\udfd9} (\u03bb _ \u2192 x)\n const-equiv = record { linv = _ ; is-linv = \u03bb _ \u2192 refl ; rinv = _ ; is-rinv = x-contr }\n \ud835\udfd9\u2243 : \ud835\udfd9 \u2243 A\n \ud835\udfd9\u2243 = _ , const-equiv\n module Equiv\u2192Contractible {A : \u2605}(f : \ud835\udfd9 \u2192 A)(f-equiv : Equiv f) where\n open Equiv f-equiv\n A-contr : Contractible (f _)\n A-contr = is-rinv\n\n module _ {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b} where\n \u03a3-ext : \u2200 {x y : \u03a3 A B} \u2192 (p : fst x \u2261 fst y) \u2192 subst B p (snd x) \u2261 snd y \u2192 x \u2261 y\n \u03a3-ext refl = cong (_,_ _)\nopen Equivalences\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata \u2610 {a}(A : \u2605_ a) : \u2605_ a where\n [_] : ..(x : A) \u2192 \u2610 A\n\nun\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} \u2192 (..(x : A) \u2192 B [ x ]) \u2192 \u03a0 (\u2610 A) B\nun\u2610 f [ x ] = f x\n\n {-\ndata S<_> {a} {A : \u2605_ a} : ..(x : A) \u2192 \u2605_ a where\n S[_] : \u2200 x \u2192 S< x >\n\nunS : \u2200 {a} {A : \u2605_ a} ..{x : A} \u2192 S< x > \u2192 A\nunS S[ y ] = y\n-}\n\ndata _\u2261\u2610_ {a} {A : \u2605_ a} (x : A) : ..(y : A) \u2192 \u2605_ a where\n refl : x \u2261\u2610 x\n\nrecord S<_> {a} {A : \u2605_ a} ..(x : A) : \u2605_ a where\n constructor S[_\u2225_]\n field\n unS : A\n isS : unS \u2261\u2610 x\nopen S<_> public\n\nS[_] : \u2200 {a}{A : \u2605_ a} (x : A) \u2192 S< x >\nS[ x ] = S[ x \u2225 refl ]\n\n_>>\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2610 A \u2192 \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 B [ x ]) \u2192 B x\n[ x ] >>\u2610 f = f x\n\n-- This is not a proper map since the function takes a ..A.\nmap\u2610 : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} \u2192 (..(x : A) \u2192 B) \u2192 \u2610 A \u2192 \u2610 B\nmap\u2610 f [ x ] = [ f x ]\n\n-- This does not work since a \u2610 has to be relevant when eliminated.\n-- join\u2610 : \u2200 {a}{A : \u2605_ a} \u2192 \u2610 (\u2610 A) \u2192 \u2610 A\n\n{- This is not a proper bind either.\n_>>=\u2610_ : \u2200 {a b}{A : \u2605_ a}{B : \u2605_ b} (x : \u2610 A) \u2192 (..(x : A) \u2192 \u2610 B) \u2192 \u2610 B\n_>>=\u2610_ = _>>\u2610_\n-}\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\nmutual\n record Com_ \u2113 : \u2605_(\u209b \u2113) where\n constructor mk\n field\n io : InOut\n M : \u2605_ \u2113\n P : M \u2192 Proto_ \u2113\n\n data Proto_ \u2113 : \u2605_(\u209b \u2113) where\n end : Proto_ \u2113\n com : Com_ \u2113 \u2192 Proto_ \u2113\n\nProto : \u2605\u2081\nProto = Proto_ \u2080\nProto\u2080 = Proto\nProto\u2081 = Proto_ \u2081\nCom : \u2605\u2081\nCom = Com_ \u2080\n\n{-\nmutual\n record Com : \u2605\u2081 where\n constructor mk\n field\n io : InOut\n M : \u2605\n P : M \u2192 Proto\n\n data Proto : \u2605\u2081 where\n end : Proto\n com : Com \u2192 Proto\n-}\npattern com' q M P = com (mk q M P)\n\npattern \u03a0\u1d9c M P = mk In M P\npattern \u03a3\u1d9c M P = mk Out M P\n\npattern \u03a0\u1d3e M P = com (mk In M P)\npattern \u03a3\u1d3e M P = com (mk Out M P)\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {q\u2080 q\u2081} (q : q\u2080 \u2248\u1d35\u1d3c q\u2081) M {P Q} \u2192 (\u2200 m \u2192 P m \u2248\u1d3e Q m) \u2192 com' q\u2080 M P \u2248\u1d3e com' q\u2081 M Q\n\nmodule ProtoRelImplicit {_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605} = ProtoRel _\u2248\u1d35\u1d3c_\nopen ProtoRelImplicit hiding (_\u2248\u1d3e_)\nopen ProtoRel _\u2261_ public renaming (_\u2248\u1d3e_ to _\u2261\u1d3e_) using ()\n\ndata View-\u2261\u1d3e : (P Q : Proto) \u2192 P \u2261\u1d3e Q \u2192 \u2605\u2081 where\n end : View-\u2261\u1d3e end end end\n \u2261-\u03a3 : \u2200 {M P Q} (p\u2261q : \u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 View-\u2261\u1d3e (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (com refl M p\u2261q)\n \u2261-\u03a0 : \u2200 {M P Q} (p\u2261q : \u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 View-\u2261\u1d3e (\u03a0\u1d3e _ P) (\u03a0\u1d3e _ Q) (com refl M p\u2261q)\n\nview-\u2261\u1d3e : \u2200 {P Q} (p\u2261q : P \u2261\u1d3e Q) \u2192 View-\u2261\u1d3e P Q p\u2261q\nview-\u2261\u1d3e end = end\nview-\u2261\u1d3e (com {In} refl _ _) = \u2261-\u03a0 _\nview-\u2261\u1d3e (com {Out} refl _ _) = \u2261-\u03a3 _\n\n{-\ninfix 0 _\u2261\u1d3e_\ndata _\u2261\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2261\u1d3e end\n com : \u2200 q M {P Q} \u2192 (\u2200 m \u2192 P m \u2261\u1d3e Q m) \u2192 com' q M P \u2261\u1d3e com' q M Q\n -}\n{-\n\u03a0' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a0' M P = com In M P\n\n\u03a3' : (M : \u2605)(P : M \u2192 Proto) \u2192 Proto\n\u03a3' M P = com Out M P\n-}\n\n\u03a0\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a0\u2610\u1d3e M P = \u03a0\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\n\u03a3\u2610\u1d3e : (M : \u2605)(P : ..(_ : M) \u2192 Proto) \u2192 Proto\n\u03a3\u2610\u1d3e M P = \u03a3\u1d3e (\u2610 M) (\u03bb { [ m ] \u2192 P m })\n\nmutual\n source-of : Proto \u2192 Proto\n source-of end = end\n source-of (com c) = com (source-of\u1d9c c)\n\n source-of\u1d9c : Com \u2192 Com\n source-of\u1d9c (mk _ M P) = \u03a3\u1d9c M \u03bb m \u2192 source-of (P m)\n\n {-\ndual : Proto \u2192 Proto\ndual end = end\ndual (\u03a3\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 dual (P m)\ndual (\u03a0\u1d3e M P) = \u03a3\u1d3e M \u03bb m \u2192 dual (P m)\n-}\n\nmutual\n dual : Proto \u2192 Proto\n dual end = end\n dual (com c) = com (dual\u1d9c c)\n\n dual\u1d9c : Com \u2192 Com\n dual\u1d9c (mk io M P) = mk (dual\u1d35\u1d3c io) M \u03bb m \u2192 dual (P m)\n\ndata IsSource : Proto \u2192 \u2605\u2081 where\n end : IsSource end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSource (P m)) \u2192 IsSource (\u03a3\u1d3e M P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 M {P} (PT : \u2200 m \u2192 IsSink (P m)) \u2192 IsSink (\u03a0\u1d3e M P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q M {P} (P\u2610 : \u2200 m \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com' q (\u2610 M) P)\n\nrecord End_ \u2113 : \u2605_ \u2113 where\n constructor end\nEnd : \u2200 {\u2113} \u2192 \u2605_ \u2113\nEnd = End_ _\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 \u2200{\u2113}(M : \u2605_ \u2113) (P : M \u2192 \u2605_ \u2113) \u2192 \u2605_ \u2113\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : \u2200 {\u2113} \u2192 Proto_ \u2113 \u2192 \u2605_ \u2113\n\u27e6 end \u27e7 = End\n\u27e6 com' q M P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n\u211b\u27e6_\u27e7 : \u2200{\u2113}(P : Proto_ \u2113) (p q : \u27e6 P \u27e7) \u2192 \u2605_ \u2113\n\u211b\u27e6 end \u27e7 p q = End\n\u211b\u27e6 \u03a0\u1d3e M P \u27e7 p q = (m : M) \u2192 \u211b\u27e6 P m \u27e7 (p m) (q m)\n\u211b\u27e6 \u03a3\u1d3e M P \u27e7 p q = \u03a3 (fst p \u2261 fst q) \u03bb e \u2192 \u211b\u27e6 P (fst q) \u27e7 (subst (\u27e6_\u27e7 \u2218 P) e (snd p)) (snd q)\n\n\u211b\u27e6_\u27e7-refl : \u2200 {\u2113}(P : Proto_ \u2113) \u2192 Reflexive \u211b\u27e6 P \u27e7\n\u211b\u27e6 end \u27e7-refl = end\n\u211b\u27e6 \u03a0\u1d3e M P \u27e7-refl = \u03bb m \u2192 \u211b\u27e6 P m \u27e7-refl\n\u211b\u27e6 \u03a3\u1d3e M P \u27e7-refl {x} = refl , \u211b\u27e6 P (fst x) \u27e7-refl\n\n\u211b\u27e6_\u27e7-sym : \u2200 {\u2113}(P : Proto_ \u2113) \u2192 Symmetric \u211b\u27e6 P \u27e7\n\u211b\u27e6 end \u27e7-sym p = end\n\u211b\u27e6 \u03a0\u1d3e M P \u27e7-sym p = \u03bb m \u2192 \u211b\u27e6 P m \u27e7-sym (p m)\n\u211b\u27e6 \u03a3\u1d3e M P \u27e7-sym (refl , q) = refl , \u211b\u27e6 P _ \u27e7-sym q -- TODO HoTT\n\n\u211b\u27e6_\u27e7-trans : \u2200 {\u2113}(P : Proto_ \u2113) \u2192 Transitive \u211b\u27e6 P \u27e7\n\u211b\u27e6 end \u27e7-trans p q = end\n\u211b\u27e6 \u03a0\u1d3e M P \u27e7-trans p q = \u03bb m \u2192 \u211b\u27e6 P m \u27e7-trans (p m) (q m)\n\u211b\u27e6 \u03a3\u1d3e M P \u27e7-trans (refl , p) (refl , q) = refl , \u211b\u27e6 P _ \u27e7-trans p q -- TODO HoTT\n\ndata ViewProc {\u2113} : \u2200 (P : Proto_ \u2113) \u2192 \u27e6 P \u27e7 \u2192 \u2605_(\u209b \u2113) where\n send : \u2200 M(P : M \u2192 Proto_ \u2113)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewProc (\u03a3\u1d3e M P) (m , p)\n recv : \u2200 M(P : M \u2192 Proto_ \u2113)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewProc (\u03a0\u1d3e M P) p\n end : ViewProc end _\n\nview-proc : \u2200 {\u2113} (P : Proto_ \u2113) (p : \u27e6 P \u27e7) \u2192 ViewProc P p\nview-proc end _ = end\nview-proc (\u03a0\u1d3e M P) p = recv _ _ p\nview-proc (\u03a3\u1d3e M P) (m , p) = send _ _ m p\n\ndata ViewCom : \u2200 P \u2192 \u27e6 com P \u27e7 \u2192 \u2605\u2081 where\n send : \u2200{M}(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 ViewCom (\u03a3\u1d9c M P) (m , p)\n recv : \u2200{M}(P : M \u2192 Proto)(p : ((m : M) \u2192 \u27e6 P m \u27e7)) \u2192 ViewCom (\u03a0\u1d9c M P) p\n\nview-com : \u2200 P (p : \u27e6 com P \u27e7) \u2192 ViewCom P p\nview-com (\u03a0\u1d9c M P) p = recv _ p\nview-com (\u03a3\u1d9c M P) (m , p) = send _ m p\n\n_\u00d7'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u00d7' P = \u03a3\u1d3e M \u03bb _ \u2192 P\n\n_\u2192'_ : \u2605 \u2192 Proto \u2192 Proto\nM \u2192' P = \u03a0\u1d3e M \u03bb _ \u2192 P\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com' q M P) = com refl M \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\n\u2261\u1d3e-reflexive : \u2200 {P Q} \u2192 P \u2261 Q \u2192 P \u2261\u1d3e Q\n\u2261\u1d3e-reflexive refl = \u2261\u1d3e-refl _\n\n\u2261\u1d3e-trans : Transitive _\u2261\u1d3e_\n\u2261\u1d3e-trans end qr = qr\n\u2261\u1d3e-trans (com refl M x) (com refl .M x\u2081) = com refl M (\u03bb m \u2192 \u2261\u1d3e-trans (x m) (x\u2081 m))\n\n_\u2219\u1d3e_ = \u2261\u1d3e-trans\n\nmodule _ {{_ : FunExt}} where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com refl M P\u2261Q) = cong (com' _ M) (funExt \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n \u2261\u1d3e-cong : \u2200 {P Q} (f : Proto \u2192 Proto) \u2192 P \u2261\u1d3e Q \u2192 f P \u2261\u1d3e f Q\n \u2261\u1d3e-cong f P\u2261Q = \u2261\u1d3e-reflexive (cong f (\u2261\u1d3e-sound P\u2261Q))\n\ndual\u1d35\u1d3c-involutive : \u2200 io \u2192 dual\u1d35\u1d3c (dual\u1d35\u1d3c io) \u2261 io\ndual\u1d35\u1d3c-involutive In = refl\ndual\u1d35\u1d3c-involutive Out = refl\n\ndual-involutive : \u2200 P \u2192 dual (dual P) \u2261\u1d3e P\ndual-involutive end = end\ndual-involutive (com' q M P) = com (dual\u1d35\u1d3c-involutive q) M (\u03bb m \u2192 dual-involutive (P m))\n\ndual-inj : \u2200 P Q \u2192 dual P \u2261\u1d3e dual Q \u2192 P \u2261\u1d3e Q\ndual-inj end end end = end\ndual-inj end (com x) ()\ndual-inj (com x) end ()\ndual-inj (com (mk In M P\u2081)) (com (mk In .M P)) (ProtoRel.com q .M x) = ProtoRel.com refl M (\u03bb m \u2192 dual-inj (P\u2081 m) (P m) (x m))\ndual-inj (com (mk In M P\u2081)) (com (mk Out .M P)) (ProtoRel.com () .M x)\ndual-inj (com (mk Out M P)) (com (mk In .M Q)) (ProtoRel.com () .M x)\ndual-inj (com (mk Out M P)) (com (mk Out .M Q)) (ProtoRel.com refl .M x) = ProtoRel.com refl M (\u03bb m \u2192 dual-inj (P m) (Q m) (x m))\n\nsource-of-idempotent : \u2200 P \u2192 source-of (source-of P) \u2261\u1d3e source-of P\nsource-of-idempotent end = end\nsource-of-idempotent (com' _ M P) = com refl M \u03bb m \u2192 source-of-idempotent (P m)\n\nsource-of-dual-oblivious : \u2200 P \u2192 source-of (dual P) \u2261\u1d3e source-of P\nsource-of-dual-oblivious end = end\nsource-of-dual-oblivious (com' _ M P) = com refl M \u03bb m \u2192 source-of-dual-oblivious (P m)\n\nsink-of : Proto \u2192 Proto\nsink-of = dual \u2218 source-of\n\nSink : Proto \u2192 \u2605\nSink P = \u27e6 sink-of P \u27e7\n\nsink : \u2200 P \u2192 Sink P\nsink end = _\nsink (com' _ M P) x = sink (P x)\n\nmodule _ {{_ : FunExt}} where\n sink-contr : \u2200 P \u2192 Contractible (sink P)\n sink-contr end s = refl\n sink-contr (com' _ _ P) s = funExt \u03bb m \u2192 sink-contr (P m) (s m)\n\n \ud835\udfd9\u2243Sink : \u2200 P \u2192 \ud835\udfd9 \u2243 Sink P\n \ud835\udfd9\u2243Sink P = Contractible\u2192Equiv.\ud835\udfd9\u2243 (sink-contr P)\n\nLog : Proto \u2192 \u2605\nLog P = \u27e6 source-of P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Log P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom' q M P >>= Q = com' q M \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n++Log : \u2200 (P : Proto){Q : Log P \u2192 Proto} (xs : Log P) \u2192 Log (Q xs) \u2192 Log (P >>= Q)\n++Log end _ ys = ys\n++Log (com' q M P) (x , xs) ys = x , ++Log (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com' q M P) = com refl M \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Log P \u2192 Proto)(R : Log (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Log P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com' q M P) Q R = com refl M \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\n-- foo : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 Sim (dual P) Q\n-- foo can stop interacting with P as soon as Q is done\n\ndata End? : \u2605 where\n end continue : End?\n\nEnd?\u1d3e : Proto \u2192 Proto\nEnd?\u1d3e P = \u03a3\u1d3e End? \u03bb { end \u2192 end ; continue \u2192 P }\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n add\u03a3\u1d3e : Proto \u2192 Proto\n add\u03a3\u1d3e end = end\n add\u03a3\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e (M \u228e A) [inl: (\u03bb m \u2192 add\u03a3\u1d3e (P m)) ,inr: A\u1d3e ]\n add\u03a3\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e M \u03bb m \u2192 add\u03a3\u1d3e (P m)\n\n add\u03a0\u1d3e : Proto \u2192 Proto\n add\u03a0\u1d3e end = end\n add\u03a0\u1d3e (\u03a0\u1d3e M P) = \u03a0\u1d3e (M \u228e A) [inl: (\u03bb m \u2192 add\u03a0\u1d3e (P m)) ,inr: A\u1d3e ]\n add\u03a0\u1d3e (\u03a3\u1d3e M P) = \u03a3\u1d3e M \u03bb m \u2192 add\u03a0\u1d3e (P m)\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n dual-add\u03a3\u1d3e : \u2200 P \u2192 dual (add\u03a3\u1d3e A\u1d3e P) \u2261\u1d3e add\u03a0\u1d3e (dual \u2218 A\u1d3e) (dual P)\n dual-add\u03a3\u1d3e end = end\n dual-add\u03a3\u1d3e (\u03a0\u1d3e M P) = com refl M (\u03bb m \u2192 dual-add\u03a3\u1d3e (P m))\n dual-add\u03a3\u1d3e (\u03a3\u1d3e M P) = com refl (M \u228e A) [inl: (\u03bb m \u2192 dual-add\u03a3\u1d3e (P m))\n ,inr: (\u03bb x \u2192 \u2261\u1d3e-refl (dual (A\u1d3e x))) ]\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = add\u03a3\u1d3e Abort\u1d3e\n\ntele-com : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Log P\ntele-com end _ _ = _\ntele-com (\u03a0\u1d3e M P) p (m , q) = m , tele-com (P m) (p m) q\ntele-com (\u03a3\u1d3e M P) (m , p) q = m , tele-com (P m) p (q m)\n\nlift\u1d3e : \u2200 a {\u2113} \u2192 Proto_ \u2113 \u2192 Proto_ (a \u2294 \u2113)\nlift\u1d3e a end = end\nlift\u1d3e a (com' io M P) = com' io (Lift {_} {a} M) \u03bb m \u2192 lift\u1d3e a (P (lower m))\n\nlift-proc : \u2200 a {\u2113} (P : Proto_ \u2113) \u2192 \u27e6 P \u27e7 \u2192 \u27e6 lift\u1d3e a P \u27e7\nlift-proc a {\u2113} P0 p0 = lift-view (view-proc P0 p0)\n where\n lift-view : \u2200 {P : Proto_ \u2113}{p : \u27e6 P \u27e7} \u2192 ViewProc P p \u2192 \u27e6 lift\u1d3e a P \u27e7\n lift-view (send M P m p) = lift m , lift-proc _ (P m) p\n lift-view (recv M P x) = \u03bb { (lift m) \u2192 lift-proc _ (P m) (x m) }\n lift-view end = end\n\nmodule MonoMobility (P : Proto\u2080) where\n Com\u1d3e : Proto\u2080\n Com\u1d3e = \u03a0\u1d3e \u27e6 P \u27e7 \u03bb p \u2192\n \u03a0\u1d3e \u27e6 P \u22a5\u27e7 \u03bb p\u22a5 \u2192\n \u03a3\u1d3e (Log P) \u03bb log \u2192\n end\n com-proc : \u27e6 Com\u1d3e \u27e7\n com-proc p p\u22a5 = tele-com P p p\u22a5 , _\n\nmodule PolyMobility where\n Com\u1d3e : Proto_ \u2081\n Com\u1d3e = \u03a0\u1d3e Proto\u2080 \u03bb P \u2192\n lift\u1d3e \u2081 (MonoMobility.Com\u1d3e P)\n com-proc : \u27e6 Com\u1d3e \u27e7\n com-proc P = lift-proc \u2081 (MonoMobility.Com\u1d3e P) (MonoMobility.com-proc P)\n\nmodule PolyMobility' where\n Com\u1d3e : Proto_ \u2081\n Com\u1d3e = \u03a0\u1d3e Proto\u2080 \u03bb P \u2192\n \u03a0\u1d3e (Lift \u27e6 P \u27e7) \u03bb p \u2192\n \u03a0\u1d3e (Lift \u27e6 P \u22a5\u27e7) \u03bb p\u22a5 \u2192\n \u03a3\u1d3e (Lift (Log P)) \u03bb log \u2192\n end\n com-proc : \u27e6 Com\u1d3e \u27e7\n com-proc P (lift p) (lift p\u22a5) = lift (tele-com P p p\u22a5) , _\n\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I) (M : \u2605) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = com (case p A\n 0: case p B\n 0: \u03a0\u1d9c (\u2610 M) (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: \u03a0\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: \u03a3\u1d9c M (\u03bb m \u2192 \u2102 m \/\/ p))\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com' (case p A 0: In 1: Out) M \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Log : Choreo I \u2192 Proto\n \u2102Log \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Log-IsSource : \u2200 \u2102 \u2192 IsSource (\u2102Log \u2102)\n \u2102Log-IsSource (A -[ M ]\u2192 B \u204f \u2102) = com M \u03bb m \u2192 \u2102Log-IsSource (\u2102 m)\n \u2102Log-IsSource (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Log-IsSource (\u2102 m)\n \u2102Log-IsSource end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com (\u2610 M) \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com M \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Log \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Log \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Log \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Log \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n\n {-\nmutual\n data [_&_\u2261_]\u1d9c : Com \u2192 Com \u2192 Com \u2192 \u2605\u2081 where\n \u03a0& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c M P & mk q M Q \u2261 mk q M R ]\u1d9c\n \u03a0\u2610& : \u2200 q {M P Q R}(P& : \u2200 m \u2192 [ P [ m ] & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d9c (\u2610 M) P & mk q M Q \u2261 mk q M R ]\u1d9c\n\n data [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n &-comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end : \u2200 {P} \u2192 [ end & P \u2261 P ]\n com : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ]\u1d9c \u2192 [ com P & com Q \u2261 com R ]\n\n-- pattern \u03a0&\u03a3 P = \u03a0& \u03a3' P\n\u03a0&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a0\u1d3e M R ]\n\u03a0&\u03a0 P& = com (\u03a0& In P&)\n\u03a0&\u03a3 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a0\u1d3e M P & \u03a3\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a0&\u03a3 P& = com (\u03a0& Out P&)\n\u03a3&\u03a0 : \u2200 {M P Q R}(P& : \u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ \u03a3\u1d3e M P & \u03a0\u1d3e M Q \u2261 \u03a3\u1d3e M R ]\n\u03a3&\u03a0 P& = &-comm (\u03a0&\u03a3 (\u03bb m \u2192 &-comm (P& m)))\n\n&-dual : \u2200 P \u2192 [ P & dual P \u2261 Trace P ]\n&-dual end = end\n&-dual (\u03a3\u1d3e M P) = \u03a3&\u03a0 \u03bb m \u2192 &-dual (P m)\n&-dual (\u03a0\u1d3e M P) = \u03a0&\u03a3 \u03bb m \u2192 &-dual (P m)\n\ndata Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u00b7\u03a3 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a0\u1d3e M P) (\u03a3\u1d3e M Q)\n \u03a3\u00b7\u03a0 : \u2200 {M P Q} \u2192 (\u2200 x \u2192 Dual (P x) (Q x)) \u2192 Dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M Q)\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u00b7\u03a3 f) = \u03a3\u00b7\u03a0 (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u00b7\u03a0 f) = \u03a0\u00b7\u03a3 (\u03bb x \u2192 Dual-sym (f x))\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0\u1d3e M P) = \u03a0\u00b7\u03a3 \u03bb m \u2192 Dual-spec (P m)\nDual-spec (\u03a3\u1d3e M P) = \u03a3\u00b7\u03a0 \u03bb m \u2192 Dual-spec (P m)\n-}\n\n{-\nEl : (P : Proto) \u2192 (Log P \u2192 \u2605) \u2192 \u2605\nEl end X = X _\nEl (com' q M P) X = \u27e6 q \u27e7\u1d35\u1d3c M \u03bb x \u2192 El (P x) (\u03bb y \u2192 X (x , y))\n\nmodule ElBind {{_ : FunExt}} where\n\n El->>= : (P : Proto){Q : Log P \u2192 Proto}{X : Log (P >>= Q) \u2192 \u2605} \u2192 El (P >>= Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Log P x y)))\n El->>= end = refl\n El->>= (com' q M P) = cong (\u27e6 q \u27e7\u1d35\u1d3c M) (funExt \u03bb m \u2192 El->>= (P m))\n-}\n\n>>=-com : (P : Proto){Q : Log P \u2192 Proto}{R : Log P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Log P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (\u03a3\u1d3e M P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (\u03a0\u1d3e M P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Log P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\nmodule ClientServerV1 (Query : \u2605\u2080) (Resp : Query \u2192 \u2605\u2080) (P : Proto) where\n Client : \u2115 \u2192 Proto\n Client zero = P\n Client (suc n) = \u03a3\u1d3e Query \u03bb q \u2192 \u03a0\u1d3e (Resp q) \u03bb r \u2192 Client n\n\n Server : \u2115 \u2192 Proto\n Server zero = P\n Server (suc n) = \u03a0\u1d3e Query \u03bb q \u2192 \u03a3\u1d3e (Resp q) \u03bb r \u2192 Server n\n\nmodule ClientServerV2 (Query : \u2605\u2080) (Resp : Query \u2192 \u2605\u2080) where\n ClientRound ServerRound : Proto\n ClientRound = \u03a3\u1d3e Query \u03bb q \u2192 \u03a0\u1d3e (Resp q) \u03bb r \u2192 end\n ServerRound = dual ClientRound\n\n Client Server : \u2115 \u2192 Proto\n Client n = replicate\u1d3e n ClientRound\n Server = dual \u2218 Client\n\n DynamicServer StaticServer : Proto\n DynamicServer = \u03a0\u1d3e \u2115 \u03bb n \u2192\n Server n\n StaticServer = \u03a3\u1d3e \u2115 \u03bb n \u2192\n Server n\n\n module PureServer (serve : \u03a0 Query Resp) where\n server : \u2200 n \u2192 \u27e6 Server n \u27e7\n server zero = _\n server (suc n) q = serve q , server n\n\nmodule _ {{_ : FunExt}} where\n dual-Log : \u2200 P \u2192 Log (dual P) \u2261 Log P\n dual-Log P = cong \u27e6_\u27e7 (\u2261\u1d3e-sound (source-of-dual-oblivious P))\n\ndual->> : \u2200 P Q \u2192 dual (P >> Q) \u2261\u1d3e dual P >> dual Q\ndual->> end Q = \u2261\u1d3e-refl _\ndual->> (\u03a0\u1d3e M P) Q = com refl M (\u03bb m \u2192 dual->> (P m) Q)\ndual->> (\u03a3\u1d3e M P) Q = com refl M (\u03bb m \u2192 dual->> (P m) Q)\n\n {- ohoh!\n dual->>= : \u2200 P (Q : Log P \u2192 Proto) \u2192 dual (P >>= Q) \u2261\u1d3e dual P >>= (dual \u2218 Q \u2218 subst id (dual-Log P))\n dual->>= end Q = \u2261\u1d3e-refl _\n dual->>= (\u03a0\u1d3e M P) Q = ProtoRel.com refl M (\u03bb m \u2192 {!dual->>= (P m) (Q \u2218 _,_ m)!})\n dual->>= (\u03a3\u1d3e M P) Q = ProtoRel.com refl M (\u03bb m \u2192 {!!})\n -}\n\nmodule _ {{_ : FunExt}} (P : Proto) where\n dual-replicate\u1d3e : \u2200 n \u2192 dual (replicate\u1d3e n P) \u2261\u1d3e replicate\u1d3e n (dual P)\n dual-replicate\u1d3e zero = end\n dual-replicate\u1d3e (suc n) = dual->> P (replicate\u1d3e n P) \u2219\u1d3e \u2261\u1d3e-cong (_>>_ (dual P)) (dual-replicate\u1d3e n)\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Com \u2192 \u2605\u2081 where\n recv : \u2200 {M P} (s : (m : M) \u2192 this (P m)) \u2192 ProcessF this (\u03a0\u1d9c M P)\n send : \u2200 {M P} (m : M) (s : this (P m)) \u2192 ProcessF this (\u03a3\u1d9c M P)\n\nrecv\u2610 : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} \u2192 (..(m : M) \u2192 this (P [ m ])) \u2192 ProcessF this (\u03a0\u1d9c (\u2610 M) P)\nrecv\u2610 = recv \u2218\u2032 un\u2610\n\nsend\u2610 : \u2200 {this : Proto \u2192 \u2605\u2081}{M}{P : \u2610 M \u2192 Proto} ..(m : M) \u2192 this (P [ m ]) \u2192 ProcessF this (\u03a3\u1d9c (\u2610 M) P)\nsend\u2610 m = send [ m ]\n\ndata Process : Proto \u2192 \u2605\u2081 where\n end : Process end\n com : \u2200 {P} \u2192 ProcessF Process P \u2192 Process (com P)\n\n{-\nmutual\n SimL : Com \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Com \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n comL : \u2200 {P Q} (sL : SimL P Q) \u2192 Sim (com P) Q\n comR : \u2200 {P Q} (sR : SimR P Q) \u2192 Sim P (com Q)\n end : Sim end end\n\nsendL : \u2200 {M P Q} (m : M) \u2192 Sim (P m) Q \u2192 Sim (\u03a3\u1d3e M P) Q\nsendL m s = comL (send m s)\n\nsendR : \u2200 {M P Q} (m : M) \u2192 Sim P (Q m) \u2192 Sim P (\u03a3\u1d3e M Q)\nsendR m s = comR (send m s)\n\nrecvL : \u2200 {M P Q} (s : (m : M) \u2192 Sim (P m) Q) \u2192 Sim (\u03a0\u1d3e M P) Q\nrecvL s = comL (recv s)\n\nrecvR : \u2200 {M P Q} (s : (m : M) \u2192 Sim P (Q m)) \u2192 Sim P (\u03a0\u1d3e M Q)\nrecvR s = comR (recv s)\n\ndata _\u2248\u02e2_ : \u2200 {P Q} (s\u2080 s\u2081 : Sim P Q) \u2192 \u2605\u2081 where\n \u2248-end : end \u2248\u02e2 end\n \u2248-sendL : \u2200 {M} {P : M \u2192 Proto} {Q} (m : M) {s\u2080 s\u2081 : Sim (P m) Q}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} m s\u2080 \u2248\u02e2 sendL m s\u2081\n \u2248-sendR : \u2200 {M P} {Q : M \u2192 Proto} (m : M) {s\u2080 s\u2081 : Sim P (Q m)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR {Q = Q} m s\u2080 \u2248\u02e2 sendR m s\u2081\n \u2248-recvL : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim (P m) Q}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvL {P = P} s\u2080 \u2248\u02e2 recvL s\u2081\n \u2248-recvR : \u2200 {M P Q} {s\u2080 s\u2081 : (m : M) \u2192 Sim P (Q m)}\n \u2192 (p : \u2200 m \u2192 s\u2080 m \u2248\u02e2 s\u2081 m)\n \u2192 recvR {Q = Q} s\u2080 \u2248\u02e2 recvR s\u2081\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080} {s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 sendR {Q = Q} r (sendL {P = P} \u2113 s\u2081)\n \u2192 s\u2080 \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\n {-\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2080) \u2248\u02e2 sendR r (sendL \u2113 s\u2081)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s\u2080 s\u2081 : Sim (P \u2113) (Q r)}\n \u2192 s\u2080 \u2248\u02e2 s\u2081\n \u2192 sendR r (sendL \u2113 s\u2080) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s\u2081)\n -}\npostulate\n \u2248-sendLR : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendL {P = P} \u2113 (sendR {Q = Q} r s) \u2248\u02e2 sendR r (sendL \u2113 s)\n \u2248-sendRL : \u2200 {M\u2113 Mr P Q} (\u2113 : M\u2113) (r : Mr) {s : Sim (P \u2113) (Q r)}\n \u2192 sendR r (sendL \u2113 s) \u2248\u02e2 sendL {P = P} \u2113 (sendR {Q = Q} r s)\n \u2248-sendR-recvL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 sendR r (recvL s) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113))\n \u2248-recvR-sendL : \u2200 {M\u2113 Mr P Q} (r : Mr) {s : (\u2113 : M\u2113) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 sendR {Q = Q} r (s \u2113)) \u2248\u02e2 sendR r (recvL s)\n \u2248-recvRL : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r)) \u2248\u02e2 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113))\n \u2248-recvLR : \u2200 {M\u2113 Mr P Q} {s : (\u2113 : M\u2113) (r : Mr) \u2192 Sim (P \u2113) (Q r)}\n \u2192 recvL {P = P} (\u03bb \u2113 \u2192 recvR {Q = Q} (s \u2113)) \u2248\u02e2 recvR (\u03bb r \u2192 recvL (\u03bb \u2113 \u2192 s \u2113 r))\n\n\u2248\u02e2-refl : \u2200 {P Q} (s : Sim P Q) \u2192 s \u2248\u02e2 s\n\u2248\u02e2-refl (comL (recv s)) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comL (send m s)) = \u2248-sendL m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl (comR (recv s)) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-refl (s m))\n\u2248\u02e2-refl (comR (send m s)) = \u2248-sendR m (\u2248\u02e2-refl s)\n\u2248\u02e2-refl end = \u2248-end\n\n\u2248\u02e2-sym : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 s\u2081 \u2248\u02e2 s\u2080\n\u2248\u02e2-sym \u2248-end = \u2248-end\n\u2248\u02e2-sym (\u2248-sendL m p) = \u2248-sendL m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendR m p) = \u2248-sendR m (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-recvL x) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n\u2248\u02e2-sym (\u2248-recvR x) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-sym (x m))\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = {!\u2248-sendRL \u2113 r ?!}\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = {!\u2248-sendLR \u2113 r!}\n-}\n{-\n\u2248\u02e2-sym (\u2248-sendLR \u2113 r p) = \u2248-sendRL \u2113 r (\u2248\u02e2-sym p)\n\u2248\u02e2-sym (\u2248-sendRL \u2113 r p) = \u2248-sendLR \u2113 r (\u2248\u02e2-sym p)\n-}\n\n\u2248\u02e2-trans : \u2200 {P Q} \u2192 Transitive (_\u2248\u02e2_ {P} {Q})\n\u2248\u02e2-trans \u2248-end q = q\n\u2248\u02e2-trans (\u2248-sendL m x) (\u2248-sendL .m x\u2081) = \u2248-sendL m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-sendR m x) (\u2248-sendR .m x\u2081) = \u2248-sendR m (\u2248\u02e2-trans x x\u2081)\n\u2248\u02e2-trans (\u2248-recvL x) (\u2248-recvL x\u2081) = \u2248-recvL (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n\u2248\u02e2-trans (\u2248-recvR x) (\u2248-recvR x\u2081) = \u2248-recvR (\u03bb m \u2192 \u2248\u02e2-trans (x m) (x\u2081 m))\n-}\n\ndata LR : \u2605 where\n `L `R : LR\n\n[L:_R:_] : \u2200 {a} {A : \u2605_ a} (l r : A) \u2192 LR \u2192 A \n[L: l R: r ] `L = l\n[L: l R: r ] `R = r\n\n_\u2295\u1d3e_ : (l r : Proto) \u2192 Proto\nl \u2295\u1d3e r = \u03a3\u1d3e LR [L: l R: r ]\n\n_&\u1d3e_ : (l r : Proto) \u2192 Proto\nl &\u1d3e r = \u03a0\u1d3e LR [L: l R: r ]\n\nmodule _ {P Q R S} where\n \u2295\u1d3e-map : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295\u1d3e R \u27e7 \u2192 \u27e6 Q \u2295\u1d3e S \u27e7\n \u2295\u1d3e-map f g (`L , pr) = `L , f pr\n \u2295\u1d3e-map f g (`R , pr) = `R , g pr\n\n &\u1d3e-map : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P &\u1d3e R \u27e7 \u2192 \u27e6 Q &\u1d3e S \u27e7\n &\u1d3e-map f g p `L = f (p `L)\n &\u1d3e-map f g p `R = g (p `R)\n\nmodule _ {P Q} where\n \u2295\u1d3e\u2192\u228e : \u27e6 P \u2295\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u228e \u27e6 Q \u27e7\n \u2295\u1d3e\u2192\u228e (`L , p) = inl p\n \u2295\u1d3e\u2192\u228e (`R , q) = inr q\n\n \u228e\u2192\u2295\u1d3e : \u27e6 P \u27e7 \u228e \u27e6 Q \u27e7 \u2192 \u27e6 P \u2295\u1d3e Q \u27e7\n \u228e\u2192\u2295\u1d3e (inl p) = `L , p\n \u228e\u2192\u2295\u1d3e (inr q) = `R , q\n\n \u228e\u2192\u2295\u1d3e\u2192\u228e : \u2200 x \u2192 \u228e\u2192\u2295\u1d3e (\u2295\u1d3e\u2192\u228e x) \u2261 x\n \u228e\u2192\u2295\u1d3e\u2192\u228e (`L , _) = refl\n \u228e\u2192\u2295\u1d3e\u2192\u228e (`R , _) = refl\n\n \u2295\u1d3e\u2192\u228e\u2192\u2295\u1d3e : \u2200 x \u2192 \u2295\u1d3e\u2192\u228e (\u228e\u2192\u2295\u1d3e x) \u2261 x\n \u2295\u1d3e\u2192\u228e\u2192\u2295\u1d3e (inl _) = refl\n \u2295\u1d3e\u2192\u228e\u2192\u2295\u1d3e (inr _) = refl\n\n \u2295\u1d3e\u2243\u228e : \u27e6 P \u2295\u1d3e Q \u27e7 \u2243 \u27e6 P \u27e7 \u228e \u27e6 Q \u27e7\n \u2295\u1d3e\u2243\u228e = \u2295\u1d3e\u2192\u228e , record { linv = \u228e\u2192\u2295\u1d3e ; is-linv = \u228e\u2192\u2295\u1d3e\u2192\u228e ; rinv = \u228e\u2192\u2295\u1d3e ; is-rinv = \u2295\u1d3e\u2192\u228e\u2192\u2295\u1d3e }\n\n &\u1d3e\u2192\u00d7 : \u27e6 P &\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n &\u1d3e\u2192\u00d7 p = p `L , p `R\n\n \u00d7\u2192&\u1d3e : \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P &\u1d3e Q \u27e7\n \u00d7\u2192&\u1d3e (p , q) `L = p\n \u00d7\u2192&\u1d3e (p , q) `R = q\n\n &\u1d3e\u2192\u00d7\u2192&\u1d3e : \u2200 x \u2192 &\u1d3e\u2192\u00d7 (\u00d7\u2192&\u1d3e x) \u2261 x\n &\u1d3e\u2192\u00d7\u2192&\u1d3e (p , q) = refl\n\n module _ {{_ : FunExt}} where\n \u00d7\u2192&\u1d3e\u2192\u00d7 : \u2200 x \u2192 \u00d7\u2192&\u1d3e (&\u1d3e\u2192\u00d7 x) \u2261 x\n \u00d7\u2192&\u1d3e\u2192\u00d7 p = funExt \u03bb { `L \u2192 refl ; `R \u2192 refl }\n\n &\u1d3e\u2243\u00d7 : \u27e6 P &\u1d3e Q \u27e7 \u2243 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n &\u1d3e\u2243\u00d7 = &\u1d3e\u2192\u00d7 , record { linv = \u00d7\u2192&\u1d3e ; is-linv = \u00d7\u2192&\u1d3e\u2192\u00d7 ; rinv = \u00d7\u2192&\u1d3e ; is-rinv = &\u1d3e\u2192\u00d7\u2192&\u1d3e }\n\n\n_>>\u1d9c_ : (P : Com) \u2192 (Proto \u2192 Proto) \u2192 Com\nP\u1d9c >>\u1d9c S = record P\u1d9c { P = \u03bb m \u2192 S (P m) }\n where open Com_ P\u1d9c\n\nmodule _ where\n\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n \u03a0\u1d3e M P \u214b\u1d3e Q = \u03a0\u1d3e M \u03bb m \u2192 P m \u214b\u1d3e Q\n P \u214b\u1d3e end = P\n P \u214b\u1d3e \u03a0\u1d3e M Q = \u03a0\u1d3e M \u03bb m \u2192 P \u214b\u1d3e Q m\n \u03a3\u1d3e M P \u214b\u1d3e \u03a3\u1d3e M' Q = \u03a3\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 P m \u214b\u1d3e \u03a3\u1d3e M' Q)\n ,inr: (\u03bb m' \u2192 \u03a3\u1d3e M P \u214b\u1d3e Q m') ]\n _\u2297\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u2297\u1d3e Q = Q\n \u03a3\u1d3e M P \u2297\u1d3e Q = \u03a3\u1d3e M \u03bb m \u2192 P m \u2297\u1d3e Q\n P \u2297\u1d3e end = P\n P \u2297\u1d3e \u03a3\u1d3e M Q = \u03a3\u1d3e M \u03bb m \u2192 P \u2297\u1d3e Q m\n \u03a0\u1d3e M P \u2297\u1d3e \u03a0\u1d3e M' Q = \u03a0\u1d3e (M \u228e M') [inl: (\u03bb m \u2192 P m \u2297\u1d3e \u03a0\u1d3e M' Q)\n ,inr: (\u03bb m' \u2192 \u03a0\u1d3e M P \u2297\u1d3e Q m') ]\n\n \u2297\u214b-dual : \u2200 P Q \u2192 dual (P \u214b\u1d3e Q) \u2261\u1d3e dual P \u2297\u1d3e dual Q\n \u2297\u214b-dual end Q = \u2261\u1d3e-refl _\n \u2297\u214b-dual (\u03a0\u1d3e M P) Q = com refl M \u03bb m \u2192 \u2297\u214b-dual (P m) _\n \u2297\u214b-dual (\u03a3\u1d3e M P) end = \u2261\u1d3e-refl _\n \u2297\u214b-dual (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) = com refl M' \u03bb m' \u2192 \u2297\u214b-dual (\u03a3\u1d3e M P) (Q m')\n \u2297\u214b-dual (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) = com refl (M \u228e M')\n [inl: (\u03bb m \u2192 \u2297\u214b-dual (P m) (\u03a3\u1d3e M' Q))\n ,inr: (\u03bb m' \u2192 \u2297\u214b-dual (\u03a3\u1d3e M P) (Q m'))\n ]\n\n -- the terminology used for the constructor follows the behavior of the combined process\n data View-\u214b : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n sendL' : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto)(m : M )(p : \u27e6 P m \u214b\u1d3e \u03a3\u1d3e M' Q \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p)\n sendR' : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto)(m' : M')(p : \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q m' \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m' , p)\n recvL' : \u2200 {M} (P : M \u2192 Proto) Q (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7)) \u2192 View-\u214b (\u03a0\u1d3e M P) Q p\n recvR' : \u2200 {M M'} (P : M \u2192 Proto) (Q : M' \u2192 Proto)(p : (m' : M') \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q m' \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p\n endL : \u2200 Q (p : \u27e6 Q \u27e7) \u2192 View-\u214b end Q p\n send : \u2200 {M}(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 View-\u214b (\u03a3\u1d3e M P) end (m , p)\n\n view-\u214b : \u2200 P Q (p : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 View-\u214b P Q p\n view-\u214b end Q p = endL Q p\n view-\u214b (\u03a0\u1d3e M P) Q p = recvL' P Q p\n view-\u214b (\u03a3\u1d3e M P) end (m , p) = send P m p\n view-\u214b (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = recvR' P Q p\n view-\u214b (com (mk Out M P)) (com (mk Out M' Q)) (inl x , p) = sendL' P Q x p\n view-\u214b (com (mk Out M P)) (com (mk Out M' Q)) (inr y , p) = sendR' P Q y p\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7 \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7\n \u214b\u1d3e-assoc end Q R s = s\n \u214b\u1d3e-assoc (\u03a0\u1d3e _ P) Q R s m = \u214b\u1d3e-assoc (P m) _ _ (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) end R s = s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) R s m = \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) _ (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) end s = s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a0\u1d3e M R) s m = \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) (s m)\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inl m , s) = inl (inl m) , \u214b\u1d3e-assoc (P m) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e _ R) s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inl m) , s) = inl (inr m) , \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) s\n \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (\u03a3\u1d3e Mr R) (inr (inr m) , s) = inr m , \u214b\u1d3e-assoc (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) (R m) s\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end p = p\n \u214b\u1d3e-rend (\u03a0\u1d3e _ P) p = \u03bb m \u2192 \u214b\u1d3e-rend (P m) (p m)\n \u214b\u1d3e-rend (\u03a3\u1d3e _ _) p = p\n\n \u214b\u1d3e-rend' : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u214b\u1d3e end \u27e7\n \u214b\u1d3e-rend' end p = p\n \u214b\u1d3e-rend' (\u03a0\u1d3e _ P) p = \u03bb m \u2192 \u214b\u1d3e-rend' (P m) (p m)\n \u214b\u1d3e-rend' (\u03a3\u1d3e _ _) p = p\n\n {-\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n -}\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (\u03a3\u1d3e M P) m p = inr m , p\n \u214b\u1d3e-sendR (\u03a0\u1d3e M P) m p = \u03bb x \u2192 \u214b\u1d3e-sendR (P x) m (p x)\n\n \u214b\u1d3e-isendR : \u2200 {M'} P Q \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e M' Q \u27e7 \u2192 (m' : M') \u2192 \u27e6 P \u214b\u1d3e Q m' \u27e7\n \u214b\u1d3e-isendR end Q s m' = s m'\n \u214b\u1d3e-isendR (\u03a0\u1d3e M P) Q s m' = \u03bb m \u2192 \u214b\u1d3e-isendR (P m) Q (s m) m'\n \u214b\u1d3e-isendR (\u03a3\u1d3e M P) Q s m' = s m'\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL {P = P} (\u03a0\u1d3e M Q) m p = \u03bb m' \u2192 \u214b\u1d3e-sendL (Q m') m (\u214b\u1d3e-isendR (P m) _ p m')\n \u214b\u1d3e-sendL (\u03a3\u1d3e M Q) m p = inl m , p\n\n\n \u214b\u1d3e-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e \u03a0\u1d3e M Q \u27e7\n \u214b\u1d3e-recvR end Q s = s\n \u214b\u1d3e-recvR (\u03a0\u1d3e M P) Q s = \u03bb x \u2192 \u214b\u1d3e-recvR (P x) Q (\u03bb m \u2192 s m x)\n \u214b\u1d3e-recvR (\u03a3\u1d3e M P) Q s = s\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id end = end\n \u214b\u1d3e-id (\u03a0\u1d3e M P) = \u03bb x \u2192 \u214b\u1d3e-sendL (P x) x (\u214b\u1d3e-id (P x))\n \u214b\u1d3e-id (\u03a3\u1d3e M P) = \u03bb x \u2192 \u214b\u1d3e-sendR (dual (P x)) x (\u214b\u1d3e-id (P x))\n\n{- Useless\n \u214b\u1d3e-recvL : \u2200 {M}{P : M \u2192 Proto}Q \u2192 ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7) \u2192 \u27e6 \u03a0\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-recvL Q f x = f x\n-}\n\n data View-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u2605\u2081 where\n sendLL : \u2200 {M M'}(P : M \u2192 Proto)(Q : M' \u2192 Proto) R (m : M)(p : \u27e6 P m \u214b\u1d3e \u03a3\u1d3e _ Q \u27e7)(q : \u27e6 dual (\u03a3\u1d3e _ Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e M P) (\u03a3\u1d3e _ Q) R (inl m , p) q\n recvLL : \u2200 {M} (P : M \u2192 Proto) Q R\n (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7))(q : \u27e6 dual Q \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a0\u1d3e M P) Q R p q\n recvR-sendR : \u2200 {M M'}P(Q : M \u2192 Proto)(R : M' \u2192 Proto)(m : M')(p : \u27e6 com P \u214b\u1d3e \u03a0\u1d3e _ Q \u27e7)(q : \u27e6 dual (\u03a0\u1d3e _ Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (\u03a0\u1d3e _ Q) (\u03a3\u1d3e _ R) p (inr m , q)\n recvRR : \u2200 {MP MQ MR}(P : MP \u2192 Proto)(Q : MQ \u2192 Proto)(R : MR \u2192 Proto)\n (p : \u27e6 \u03a3\u1d3e _ P \u214b\u1d3e \u03a0\u1d3e _ Q \u27e7)(q : (m : MR) \u2192 \u27e6 dual (\u03a0\u1d3e _ Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) (\u03a0\u1d3e _ R) p q\n sendR-recvL : \u2200 {MP MQ}(P : MP \u2192 Proto)(Q : MQ \u2192 Proto)R(m : MQ)\n (p : \u27e6 \u03a3\u1d3e _ P \u214b\u1d3e Q m \u27e7)(q : (m : MQ) \u2192 \u27e6 dual (Q m) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e _ P) (\u03a3\u1d3e _ Q) R (inr m , p) q\n recvR-sendL : \u2200 {MP MQ MR}(P : MP \u2192 Proto)(Q : MQ \u2192 Proto)(R : MR \u2192 Proto)\n (p : (m : MQ) \u2192 \u27e6 \u03a3\u1d3e _ P \u214b\u1d3e Q m \u27e7)(m : MQ)(q : \u27e6 dual (Q m) \u214b\u1d3e \u03a3\u1d3e _ R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) (\u03a3\u1d3e _ R) p (inl m , q)\n endL : \u2200 Q R\n \u2192 (q : \u27e6 Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 end Q R q qr\n sendLM : \u2200 {MP}(P : MP \u2192 Proto)R\n (m : MP)(p : \u27e6 P m \u27e7)(r : \u27e6 R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e _ P) end R (m , p) r\n recvL-sendR : \u2200 {MP MQ}(P : MP \u2192 Proto)(Q : MQ \u2192 Proto)\n (m : MQ)(p : \u2200 m \u2192 \u27e6 \u03a3\u1d3e _ P \u214b\u1d3e Q m \u27e7)(q : \u27e6 dual (Q m) \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) end p (m , q)\n\n view-\u2218 : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7) \u2192 View-\u2218 P Q R pq qr\n view-\u2218 P Q R pq qr = view-\u2218-view (view-\u214b P Q pq) (view-\u214b (dual Q) R qr)\n where\n view-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u214b P Q pq \u2192 View-\u214b (dual Q) R qr \u2192 View-\u2218 P Q R pq qr\n view-\u2218-view (sendL' _ _ _ _) _ = sendLL _ _ _ _ _ _\n view-\u2218-view (recvL' _ _ _) _ = recvLL _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) _ = sendR-recvL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendL' ._ _ _ _) = recvR-sendL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendR' ._ _ _ _) = recvR-sendR _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (send ._ _ _) = recvL-sendR _ _ _ _ _\n view-\u2218-view (endL _ _) _ = endL _ _ _ _\n view-\u2218-view (send _ _ _) _ = sendLM _ _ _ _ _\n\n \u214b\u1d3e-apply : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply end Q s p = s\n \u214b\u1d3e-apply (\u03a0\u1d3e M P) Q s (m , p) = \u214b\u1d3e-apply (P m) Q (s m) p\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) end s p = _\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) s p m' = \u214b\u1d3e-apply (\u03a3\u1d3e M P) (Q m') (s m') p\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , s) p = \u214b\u1d3e-apply (P m) (\u03a3\u1d3e M' Q) s (p m)\n \u214b\u1d3e-apply (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , s) p = m , \u214b\u1d3e-apply (\u03a3\u1d3e M P) (Q m) s p\n\nmodule _ {{_ : FunExt}} where\n \u214b\u1d3e-apply' : \u2200 {P Q} \u2192 \u27e6 dual P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply' {P} {Q} pq p = \u214b\u1d3e-apply (dual P) Q pq (subst \u27e6_\u27e7 (\u2261.sym (\u2261\u1d3e-sound (dual-involutive P))) p)\n\n -- left-biased \u201cstrategy\u201d\n par : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par P Q p q = par-view (view-proc P p)\n where par-view : \u2200 {P} {p : \u27e6 P \u27e7} \u2192 ViewProc P p \u2192 \u27e6 P \u214b\u1d3e Q \u27e7\n par-view (send M P m p) = \u214b\u1d3e-sendL Q m (par (P m) Q p q)\n par-view (recv M P p) = \u03bb m \u2192 par (P m) Q (p m) q\n par-view end = q\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = \u214b\u1d3e-\u2218-view (view-\u2218 P Q R pq qr)\n where\n \u214b\u1d3e-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u2218 P Q R pq qr \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218-view (sendLL P Q R m p qr) = \u214b\u1d3e-sendL R m (\u214b\u1d3e-\u2218 (P m) (\u03a3\u1d3e _ Q) R p qr)\n \u214b\u1d3e-\u2218-view (recvLL P Q R p qr) = \u03bb m \u2192 \u214b\u1d3e-\u2218 (P m) Q R (p m) qr\n \u214b\u1d3e-\u2218-view (recvR-sendR P Q R m pq q) = \u214b\u1d3e-sendR (com P) m (\u214b\u1d3e-\u2218 (com P) (\u03a0\u1d3e _ Q) (R m) pq q)\n \u214b\u1d3e-\u2218-view (recvRR P Q R pq q) = \u03bb m \u2192 \u214b\u1d3e-\u2218 (\u03a3\u1d3e _ P) (\u03a0\u1d3e _ Q) (R m) pq (q m)\n \u214b\u1d3e-\u2218-view (sendR-recvL P Q R m p q) = \u214b\u1d3e-\u2218 (\u03a3\u1d3e _ P) (Q m) R p (q m)\n \u214b\u1d3e-\u2218-view (recvR-sendL P Q R p m q) = \u214b\u1d3e-\u2218 (\u03a3\u1d3e _ P) (Q m) (\u03a3\u1d3e _ R) (p m) q\n \u214b\u1d3e-\u2218-view (endL Q R pq qr) = \u214b\u1d3e-apply' {Q} {R} qr pq\n \u214b\u1d3e-\u2218-view (sendLM P R m pq qr) = \u214b\u1d3e-sendL R m (par (P m) R pq qr)\n \u214b\u1d3e-\u2218-view (recvL-sendR P Q m pq qr) = \u214b\u1d3e-\u2218 (\u03a3\u1d3e _ P) (Q m) end (pq m) (\u214b\u1d3e-rend' (dual (Q m)) qr)\n \n mutual\n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm P Q p = \u214b\u1d3e-comm-view (view-\u214b P Q p)\n\n \u214b\u1d3e-comm-view : \u2200 {P Q} {pq : \u27e6 P \u214b\u1d3e Q \u27e7} \u2192 View-\u214b P Q pq \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm-view (sendL' P Q m p) = \u214b\u1d3e-sendR (\u03a3\u1d3e _ Q) m (\u214b\u1d3e-comm (P m) (com (mk Out _ Q)) p)\n \u214b\u1d3e-comm-view (sendR' P Q m' p) = inl m' , \u214b\u1d3e-comm (\u03a3\u1d3e _ P) (Q m') p\n \u214b\u1d3e-comm-view (recvL' P Q pq) = \u214b\u1d3e-recvR Q P \u03bb m \u2192 \u214b\u1d3e-comm (P m) Q (pq m)\n \u214b\u1d3e-comm-view (recvR' P Q pq) = \u03bb m' \u2192 \u214b\u1d3e-comm (\u03a3\u1d3e _ P) (Q m') (pq m')\n \u214b\u1d3e-comm-view (endL Q pq) = \u214b\u1d3e-rend' Q pq\n \u214b\u1d3e-comm-view (send P m pq) = m , pq\n\n \u214b\u1d3e-comm-equiv : \u2200 P Q \u2192 Equiv (\u214b\u1d3e-comm P Q)\n \u214b\u1d3e-comm-equiv P Q = record { linv = \u214b\u1d3e-comm Q P ; is-linv = {!!} ; rinv = \u214b\u1d3e-comm Q P ; is-rinv = {!!} }\n {-\n where\n toto : \u2200 P Q (x : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 \u214b\u1d3e-comm Q P (\u214b\u1d3e-comm P Q x) \u2261 x\n toto end end x = refl\n toto end (com (mk io M P)) x\u2081 = refl\n toto (com (mk io M P)) end x\u2081 = refl\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' In M\u2081 P\u2081) (pq x))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' Out M\u2081 P\u2081) (pq x))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' In M\u2081 P\u2081) pq))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' Out M\u2081 P\u2081) pq))\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' In M P) (P\u2081 x) (pq x)))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' In M P) (P\u2081 m) pq))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' Out M P) (P\u2081 x) (pq x)))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' Out M P) (P\u2081 m) pq))\n -}\n \u214b\u1d3e-comm-\u2248 : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm-\u2248 P Q = _ , \u214b\u1d3e-comm-equiv P Q\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P \u2297\u1d3e Q \u27e7\n comma\u1d3e P Q p q with view-proc P p | view-proc Q q\n comma\u1d3e .(com (mk Out M P)) Q .(m , p) q | send M P m p | q' = m , comma\u1d3e (P m) Q p q\n comma\u1d3e .(com (mk In M P)) .(com (mk Out M\u2081 P\u2081)) p .(m , p\u2081) | recv M P .p | send M\u2081 P\u2081 m p\u2081 = m , comma\u1d3e (\u03a0\u1d3e M P) (P\u2081 m) p p\u2081\n comma\u1d3e ._ ._ ._ ._ | recv M P p | recv M\u2081 P\u2081 x = [inl: (\u03bb m \u2192 comma\u1d3e (P m) (\u03a0\u1d3e _ P\u2081) (p m) x)\n ,inr: (\u03bb m' \u2192 comma\u1d3e (\u03a0\u1d3e _ P) (P\u2081 m') p (x m') ) ]\n comma\u1d3e ._ ._ ._ ._ | recv M P x | end = x\n comma\u1d3e .end Q .end q | end | q' = q\n\n \u2297\u1d3e-fst : \u2200 P Q \u2192 \u27e6 P \u2297\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7\n \u2297\u1d3e-fst end Q pq = _\n \u2297\u1d3e-fst (\u03a3\u1d3e M P) Q (m , pq) = m , \u2297\u1d3e-fst (P m) Q pq\n \u2297\u1d3e-fst (\u03a0\u1d3e M P) end pq = pq\n \u2297\u1d3e-fst (\u03a0\u1d3e M P) (\u03a3\u1d3e _ Q) (_ , pq) = \u2297\u1d3e-fst (\u03a0\u1d3e M P) (Q _) pq\n \u2297\u1d3e-fst (\u03a0\u1d3e M P) (\u03a0\u1d3e N Q) pq = \u03bb m \u2192 \u2297\u1d3e-fst (P m) (\u03a0\u1d3e N Q) (pq (inl m))\n\n \u2297\u1d3e-snd : \u2200 P Q \u2192 \u27e6 P \u2297\u1d3e Q \u27e7 \u2192 \u27e6 Q \u27e7\n \u2297\u1d3e-snd end Q pq = pq\n \u2297\u1d3e-snd (\u03a3\u1d3e M P) Q (_ , pq) = \u2297\u1d3e-snd (P _) Q pq\n \u2297\u1d3e-snd (\u03a0\u1d3e M P) end pq = end\n \u2297\u1d3e-snd (\u03a0\u1d3e M P) (\u03a3\u1d3e _ Q) (m , pq) = m , \u2297\u1d3e-snd (\u03a0\u1d3e M P) (Q m) pq\n \u2297\u1d3e-snd (\u03a0\u1d3e M P) (\u03a0\u1d3e N Q) pq = \u03bb m \u2192 \u2297\u1d3e-snd (\u03a0\u1d3e M P) (Q m) (pq (inr m))\n\n \u00d7\u2192\u2297\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P \u2297\u1d3e Q \u27e7\n \u00d7\u2192\u2297\u1d3e P Q (p , q) = comma\u1d3e P Q p q\n\n \u2297\u1d3e\u2192\u00d7 : \u2200 P Q \u2192 \u27e6 P \u2297\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n \u2297\u1d3e\u2192\u00d7 P Q p = \u2297\u1d3e-fst P Q p , \u2297\u1d3e-snd P Q p\n\n \u2297\u1d3e-comma-fst : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 \u2297\u1d3e-fst P Q (comma\u1d3e P Q p q) \u2261 p\n \u2297\u1d3e-comma-fst end Q p q = refl\n \u2297\u1d3e-comma-fst (\u03a3\u1d3e M P) Q (m , p) q = \u03a3-ext refl (\u2297\u1d3e-comma-fst (P m) Q p q)\n \u2297\u1d3e-comma-fst (\u03a0\u1d3e M P) end p q = refl\n \u2297\u1d3e-comma-fst (\u03a0\u1d3e M P) (\u03a3\u1d3e _ Q) p (m , q) = {!\u03a3-ext!}\n \u2297\u1d3e-comma-fst (\u03a0\u1d3e M P) (\u03a0\u1d3e N Q) p q = {!!}\n {-\n \u2297\u1d3e-comma-fst end Q p q = refl\n \u2297\u1d3e-comma-fst (com P) end p q = refl\n \u2297\u1d3e-comma-fst (com P) (com Q) p q with view-com P p\n \u2297\u1d3e-comma-fst (com ._) (com Q) ._ q | send P m p = cong (_,_ m) (\u2297\u1d3e-comma-fst (P m) (com Q) p q)\n \u2297\u1d3e-comma-fst (com ._) (com Q) ._ q | recv P p = funExt \u03bb m \u2192 \u2297\u1d3e-comma-fst (P m) (com Q) (p m) q\n\n \u2297\u1d3e-comma-snd : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 \u2297\u1d3e-snd P Q (comma\u1d3e P Q p q) \u2261 q\n \u2297\u1d3e-comma-snd end Q p q = refl\n \u2297\u1d3e-comma-snd (com P) end p q = refl\n \u2297\u1d3e-comma-snd (com P) (com Q) p q with view-com Q q\n \u2297\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (\u2297\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p q)\n \u2297\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (\u2297\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p q)\n \u2297\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 \u2297\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p (q m)\n \u2297\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 \u2297\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p (q m)\n -}\n\n {-\n end Q p = \u214b\u1d3e-rend' Q p\n \u214b\u1d3e-! end Q p = \u214b\u1d3e-rend' Q p\n \u214b\u1d3e-! (\u03a0\u1d3e M P) end p x = \u214b\u1d3e-! (P x) end (p x)\n \u214b\u1d3e-! (\u03a0\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 {!\u214b\u1d3e-! (\u03a0 !}\n \u214b\u1d3e-! (\u03a0\u1d3e M P) (\u03a3\u1d3e M' Q) p = \u03bb m \u2192 \u214b\u1d3e-! (P m) (com (mk Out M' Q)) (p m)\n \u214b\u1d3e-! (\u03a3\u1d3e M P) end p = p\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a0\u1d3e M' Q) p = \u03bb m' \u2192 \u214b\u1d3e-! (com (mk Out M P)) (Q m') (p m')\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inl m , p) = inr m , (\u214b\u1d3e-! (P m) (com (mk Out M' Q)) p)\n \u214b\u1d3e-! (\u03a3\u1d3e M P) (\u03a3\u1d3e M' Q) (inr m , p) = inl m , (\u214b\u1d3e-! (com (mk Out M P)) (Q m) p)\n -}\n\nmodule _ {{_ : FunExt}} where\n \u03a0\ud835\udfd8-uniq : \u2200 (F G : \ud835\udfd8 \u2192 \u2605) \u2192 \u03a0 \ud835\udfd8 F \u2261 \u03a0 \ud835\udfd8 G\n \u03a0\ud835\udfd8-uniq F G = cong (\u03a0 \ud835\udfd8) (funExt (\u03bb()))\n\n{-\nA `\u2297 B 'times', context chooses how A and B are used\nA `\u214b B 'par', \"we\" chooses how A and B are used\nA `\u2295 B 'plus', select from A or B\nA `& B 'with', offer choice of A or B\n`! A 'of course!', server accept\n`? A 'why not?', client request\n`1 unit for `\u2297\n`\u22a4 unit for `\u214b\n`0 unit for `\u2295\n`\u22a5 unit for `&\n-}\ndata CLL : \u2605 where\n `1 `\u22a4 `0 `\u22a5 : CLL\n _`\u2297_ _`\u214b_ _`\u2295_ _`&_ : (A B : CLL) \u2192 CLL\n -- `!_ `?_ : (A : CLL) \u2192 CLL\n\n_\u22a5 : CLL \u2192 CLL\n`1 \u22a5 = `\u22a4\n`\u22a4 \u22a5 = `1\n`0 \u22a5 = `\u22a5\n`\u22a5 \u22a5 = `0\n(A `\u2297 B)\u22a5 = A \u22a5 `\u214b B \u22a5\n(A `\u214b B)\u22a5 = A \u22a5 `\u2297 B \u22a5\n(A `\u2295 B)\u22a5 = A \u22a5 `& B \u22a5\n(A `& B)\u22a5 = A \u22a5 `\u2295 B \u22a5\n{-\n(`! A)\u22a5 = `?(A \u22a5)\n(`? A)\u22a5 = `!(A \u22a5)\n-}\n\nCLL-proto : CLL \u2192 Proto\nCLL-proto `1 = end -- TODO\nCLL-proto `\u22a4 = end -- TODO\nCLL-proto `0 = \u03a0\u1d3e \ud835\udfd8 \u03bb()\nCLL-proto `\u22a5 = \u03a3\u1d3e \ud835\udfd8 \u03bb()\nCLL-proto (A `\u2297 B) = CLL-proto A \u2297\u1d3e CLL-proto B\nCLL-proto (A `\u214b B) = CLL-proto A \u214b\u1d3e CLL-proto B\nCLL-proto (A `\u2295 B) = CLL-proto A \u2295\u1d3e CLL-proto B\nCLL-proto (A `& B) = CLL-proto A &\u1d3e CLL-proto B\n\n{- The point of this could be to devise a particular equivalence\n relation for processes. It could properly deal with \u214b. -}\n\n {-\nmodule V4 {{_ : FunExt}} where\n mutual\n _\u214b\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end \u214b\u1d3e Q = Q\n P \u214b\u1d3e end = P\n com P \u214b\u1d3e com Q = \u03a3\u1d3e LR (P \u214b\u1d9c Q)\n\n _\u214b\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 \u214b\u1d9c P\u1d3f) `L = P\u1d38 \u214b\u1d9cL P\u1d3f\n (P\u1d38 \u214b\u1d9c P\u1d3f) `R = P\u1d38 \u214b\u1d9cR P\u1d3f\n\n _\u214b\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) \u214b\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m \u214b\u1d3e com Q)\n\n _\u214b\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P \u214b\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P \u214b\u1d3e P\u1d3f m)\n\n mutual\n _ox\u1d3e_ : Proto \u2192 Proto \u2192 Proto\n end ox\u1d3e Q = Q\n P ox\u1d3e end = P\n com P\u1d38 ox\u1d3e com P\u1d3f = \u03a0\u1d3e LR (P\u1d38 ox\u1d9c P\u1d3f)\n\n _ox\u1d9c_ : Com \u2192 Com \u2192 LR \u2192 Proto\n (P\u1d38 ox\u1d9c P\u1d3f) `L = P\u1d38 ox\u1d9cL P\u1d3f\n (P\u1d38 ox\u1d9c P\u1d3f) `R = P\u1d38 ox\u1d9cR P\u1d3f\n\n _ox\u1d9cL_ : Com \u2192 Com \u2192 Proto\n (mk q\u1d38 M\u1d38 P\u1d38) ox\u1d9cL Q = com' q\u1d38 M\u1d38 (\u03bb m \u2192 P\u1d38 m ox\u1d3e com Q)\n\n _ox\u1d9cR_ : Com \u2192 Com \u2192 Proto\n P ox\u1d9cR (mk q\u1d3f M\u1d3f P\u1d3f) = com' q\u1d3f M\u1d3f (\u03bb m \u2192 com P ox\u1d3e P\u1d3f m)\n\n data Viewox : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u2605\u2081 where\n com : \u2200{P Q}(p : \u27e6 \u03a0\u1d3e LR (P ox\u1d9c Q) \u27e7) \u2192 Viewox (com P) (com Q) p\n endL : \u2200{Q}(p : \u27e6 Q \u27e7) \u2192 Viewox end Q p\n endR : \u2200{P}(p : \u27e6 com P \u27e7) \u2192 Viewox (com P) end p\n\n viewox : \u2200 P Q (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 Viewox P Q p\n viewox end Q p = endL p\n viewox (com P) end p = endR p\n viewox (com P) (com Q) p = com p\n\n data View-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u2605\u2081 where\n sendLL : \u2200 {M} (P : M \u2192 Proto) Q R (m : M)(p : \u27e6 P m \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a3\u1d3e M P) (com Q) R (`L , m , p) q\n recvLL : \u2200 {M} (P : M \u2192 Proto) Q R\n (p : ((m : M) \u2192 \u27e6 P m \u214b\u1d3e com Q \u27e7))(q : \u27e6 dual (com Q) \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 (\u03a0\u1d3e M P) (com Q) R (`L , p) q\n sendRR : \u2200 {M} P Q (R : M \u2192 Proto)(m : M)(p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a3\u1d3e M R) p (`R , m , q)\n recvRR : \u2200 {M} P Q (R : M \u2192 Proto)\n (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : (m : M) \u2192 \u27e6 dual (com Q) \u214b\u1d3e R m \u27e7)\n \u2192 View-\u2218 (com P) (com Q) (\u03a0\u1d3e M R) p (`R , q)\n\n sendR-recvL : \u2200 {M} P (Q : M \u2192 Proto) R (m : M)(p : \u27e6 com P \u214b\u1d3e Q m \u27e7)(q : (m : M) \u2192 \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a3\u1d3e M Q) (com R) (`R , m , p) (`L , q)\n recvR-sendL : \u2200 {M} P (Q : M \u2192 Proto) R (p : (m : M) \u2192 \u27e6 com P \u214b\u1d3e Q m \u27e7)(m : M)(q : \u27e6 dual (Q m) \u214b\u1d3e com R \u27e7)\n \u2192 View-\u2218 (com P) (\u03a0\u1d3e M Q) (com R) (`R , p) (`L , m , q)\n\n endL : \u2200 Q R\n \u2192 (q : \u27e6 Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7)\n \u2192 View-\u2218 end Q R q qr\n\n endM : \u2200 P R\n \u2192 (p : \u27e6 com P \u27e7)(r : \u27e6 R \u27e7)\n \u2192 View-\u2218 (com P) end R p r\n endR : \u2200 P Q\n \u2192 (p : \u27e6 com P \u214b\u1d3e com Q \u27e7)(q : \u27e6 dual (com Q) \u27e7)\n \u2192 View-\u2218 (com P) (com Q) end p q\n\n view-\u2218 : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7)(qr : \u27e6 dual Q \u214b\u1d3e R \u27e7) \u2192 View-\u2218 P Q R pq qr\n view-\u2218 P Q R pq qr = view-\u2218-view (view-\u214b P Q pq) (view-\u214b (dual Q) R qr)\n where\n view-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u214b P Q pq \u2192 View-\u214b (dual Q) R qr \u2192 View-\u2218 P Q R pq qr\n view-\u2218-view (sendL' _ _ _ _) _ = sendLL _ _ _ _ _ _\n view-\u2218-view (recvL' _ _ _) _ = recvLL _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvL' ._ _ _) = sendR-recvL _ _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) (endR ._ _) = endR _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendL' ._ _ _ _) = recvR-sendL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendR' ._ _ _ _) = sendRR _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (endR ._ ._) = endR _ _ _ _\n view-\u2218-view (endL _ _) _ = endL _ _ _ _\n view-\u2218-view (endR _ _) _ = endM _ _ _ _\n\n \u214b\u1d3e-rend : \u2200 P \u2192 \u27e6 P \u214b\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b\u1d3e-rend end = id\n \u214b\u1d3e-rend (com _) = id\n\n \u214b\u1d3e-rend-equiv : \u2200 P \u2192 Equiv (\u214b\u1d3e-rend P)\n \u214b\u1d3e-rend-equiv end = id\u1d31\n \u214b\u1d3e-rend-equiv (com _) = id\u1d31\n\n \u214b\u1d3e-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n \u214b\u1d3e-sendR end m p = m , p\n \u214b\u1d3e-sendR (com P) m p = `R , (m , p)\n\n \u214b\u1d3e-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7 \u2192 \u27e6 \u03a3\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-sendL {P = P} end m p = m , \u214b\u1d3e-rend (P m) p\n \u214b\u1d3e-sendL (com _) m p = `L , (m , p)\n\n \u214b\u1d3e-recvR : \u2200 {M}P{Q : M \u2192 Proto} \u2192 ((m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7) \u2192 \u27e6 P \u214b\u1d3e com' In M Q \u27e7\n \u214b\u1d3e-recvR end f = f\n \u214b\u1d3e-recvR (com _) f = `R , f\n\n \u214b\u1d3e-recvL : \u2200 {M}{P : M \u2192 Proto}Q \u2192 ((m : M) \u2192 \u27e6 P m \u214b\u1d3e Q \u27e7) \u2192 \u27e6 \u03a0\u1d3e M P \u214b\u1d3e Q \u27e7\n \u214b\u1d3e-recvL {P = P} end f x = \u214b\u1d3e-rend (P x) (f x)\n \u214b\u1d3e-recvL (com _) f = `L , f\n\n ox\u1d3e-rend : \u2200 P \u2192 \u27e6 P ox\u1d3e end \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-rend end = id\n ox\u1d3e-rend (com x) = id\n\n ox\u1d3e-sendR : \u2200 {M P}{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b\u1d3e Q m \u27e7 \u2192 \u27e6 P \u214b\u1d3e com' Out M Q \u27e7\n ox\u1d3e-sendR {P = end} m p = m , p\n ox\u1d3e-sendR {P = com x} m p = `R , (m , p)\n\n \u214b\u1d3e-id : \u2200 P \u2192 \u27e6 dual P \u214b\u1d3e P \u27e7\n \u214b\u1d3e-id end = _\n \u214b\u1d3e-id (com (mk In M P)) = `R , \u03bb m \u2192 \u214b\u1d3e-sendL (P m) m (\u214b\u1d3e-id (P m))\n \u214b\u1d3e-id (com (mk Out M P)) = `L , \u03bb m \u2192 \u214b\u1d3e-sendR (dual (P m)) m (\u214b\u1d3e-id (P m))\n\n module _ where\n \u214b\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 Q \u214b\u1d3e P \u27e7\n \u214b\u1d3e-comm = \u03bb P Q \u2192 to P Q , equiv P Q\n where\n to : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 Q \u214b\u1d3e P \u27e7\n to end end pq = pq\n to end (com x) pq = pq\n to (com x) end pq = pq\n to (com (mk In M P)) (com x\u2081) (`L , pq) = `R , (\u03bb m \u2192 to (P m) (com x\u2081) (pq m))\n to (com (mk Out M P)) (com x\u2081) (`L , m , pq) = `R , m , to (P m) (com x\u2081) pq\n to (com x) (com (mk In M P)) (`R , pq) = `L , (\u03bb m \u2192 to (com x) (P m) (pq m))\n to (com x) (com (mk Out M P)) (`R , m , pq) = `L , m , to (com x) (P m) pq\n\n toto : \u2200 P Q (x : \u27e6 P \u214b\u1d3e Q \u27e7) \u2192 to Q P (to P Q x) \u2261 x\n toto end end x = refl\n toto end (com (mk io M P)) x\u2081 = refl\n toto (com (mk io M P)) end x\u2081 = refl\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' In M\u2081 P\u2081) (pq x))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`L , pq) = \u03a3-ext refl (funExt \u03bb x \u2192 toto (P x) (com' Out M\u2081 P\u2081) (pq x))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' In M\u2081 P\u2081) pq))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`L , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (P m) (com' Out M\u2081 P\u2081) pq))\n toto (com (mk In M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' In M P) (P\u2081 x) (pq x)))\n toto (com (mk In M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' In M P) (P\u2081 m) pq))\n toto (com (mk Out M P)) (com (mk In M\u2081 P\u2081)) (`R , pq) = \u03a3-ext refl (funExt (\u03bb x \u2192 toto (com' Out M P) (P\u2081 x) (pq x)))\n toto (com (mk Out M P)) (com (mk Out M\u2081 P\u2081)) (`R , m , pq) = \u03a3-ext refl (\u03a3-ext refl (toto (com' Out M P) (P\u2081 m) pq))\n\n equiv : \u2200 P Q \u2192 Equiv (to P Q)\n equiv P Q = record { linv = to Q P ; is-linv = toto P Q ; rinv = to Q P ; is-rinv = toto Q P }\n\n \u214b\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) \u214b\u1d3e R \u27e7 \u2243 \u27e6 P \u214b\u1d3e (Q \u214b\u1d3e R) \u27e7\n \u214b\u1d3e-assoc P Q R = {!!}\n\n \u214b\u1d3e-ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2243 \u27e6 dual P ox\u1d3e dual Q \u22a5\u27e7\n \u214b\u1d3e-ox\u1d3e P Q = {!!}\n \n ox\u1d3e-comm : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2243 \u27e6 Q ox\u1d3e P \u27e7\n ox\u1d3e-comm P Q = {!!}\n\n ox\u1d3e-assoc : \u2200 P Q R \u2192 \u27e6 (P ox\u1d3e Q) ox\u1d3e R \u27e7 \u2243 \u27e6 P ox\u1d3e (Q ox\u1d3e R) \u27e7\n ox\u1d3e-assoc P Q R = {!!}\n\n comma\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n comma\u1d3e end Q p q = q\n comma\u1d3e (com _) end p q = p\n comma\u1d3e (\u03a3\u1d3e M P) (com Q) (m , p) q `L = m , comma\u1d3e (P m) (com Q) p q\n comma\u1d3e (\u03a0\u1d3e M P) (com Q) p q `L = \u03bb m \u2192 comma\u1d3e (P m) (com Q) (p m) q\n comma\u1d3e (com P) (\u03a3\u1d3e M Q) p (m , q) `R = m , comma\u1d3e (com P) (Q m) p q\n comma\u1d3e (com P) (\u03a0\u1d3e M Q) p q `R = \u03bb m \u2192 comma\u1d3e (com P) (Q m) p (q m)\n\n \u00d7\u2192ox\u1d3e : \u2200 P Q \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P ox\u1d3e Q \u27e7\n \u00d7\u2192ox\u1d3e P Q (p , q) = comma\u1d3e P Q p q\n\n ox\u1d3e-fst : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7\n ox\u1d3e-fst end Q pq = _\n ox\u1d3e-fst (com _) end pq = pq\n ox\u1d3e-fst (\u03a0\u1d3e M P) (com Q) pq = \u03bb m \u2192 ox\u1d3e-fst (P m) (com Q) (pq `L m)\n ox\u1d3e-fst (\u03a3\u1d3e M P) (com Q) pq = \u00d7-map id (\u03bb {x} \u2192 ox\u1d3e-fst (P x) (com Q)) (pq `L)\n\n ox\u1d3e-snd : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 Q \u27e7\n ox\u1d3e-snd end Q pq = pq\n ox\u1d3e-snd (com _) end pq = _\n ox\u1d3e-snd (com P) (\u03a0\u1d3e M' Q) pq = \u03bb m' \u2192 ox\u1d3e-snd (com P) (Q m') (pq `R m')\n ox\u1d3e-snd (com P) (\u03a3\u1d3e M' Q) pq = \u00d7-map id (\u03bb {m'} \u2192 ox\u1d3e-snd (com P) (Q m')) (pq `R)\n\n ox\u1d3e\u2192\u00d7 : \u2200 P Q \u2192 \u27e6 P ox\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n ox\u1d3e\u2192\u00d7 P Q p = ox\u1d3e-fst P Q p , ox\u1d3e-snd P Q p\n\n ox\u1d3e-comma-fst : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-fst P Q (comma\u1d3e P Q p q) \u2261 p\n ox\u1d3e-comma-fst end Q p q = refl\n ox\u1d3e-comma-fst (com P) end p q = refl\n ox\u1d3e-comma-fst (com P) (com Q) p q with view-com P p\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | send P m p = cong (_,_ m) (ox\u1d3e-comma-fst (P m) (com Q) p q)\n ox\u1d3e-comma-fst (com ._) (com Q) ._ q | recv P p = funExt \u03bb m \u2192 ox\u1d3e-comma-fst (P m) (com Q) (p m) q\n\n ox\u1d3e-comma-snd : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 ox\u1d3e-snd P Q (comma\u1d3e P Q p q) \u2261 q\n ox\u1d3e-comma-snd end Q p q = refl\n ox\u1d3e-comma-snd (com P) end p q = refl\n ox\u1d3e-comma-snd (com P) (com Q) p q with view-com Q q\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | send Q m q = cong (_,_ m) (ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p q)\n ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a0\u1d3e _ P) (Q m) p (q m)\n ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (com ._) p ._ | recv Q q = funExt \u03bb m \u2192 ox\u1d3e-comma-snd (\u03a3\u1d3e _ P) (Q m) p (q m)\n\n {-\n ox\u1d3e\u2192\u00d7-rinv : \u2200 {P Q} (p : \u27e6 P ox\u1d3e Q \u27e7) \u2192 \u00d7\u2192ox\u1d3e P Q (ox\u1d3e\u2192\u00d7 P Q p) \u2261 p\n ox\u1d3e\u2192\u00d7-rinv {P} {Q} p with viewox P Q p\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk In M\u2081 P\u2081} .p = funExt \u03bb { `L \u2192 funExt \u03bb m \u2192 {!ox\u1d3e\u2192\u00d7-rinv {P m} {\u03a0\u1d3e _ P\u2081} ?!} ; `R \u2192 {!!} }\n ox\u1d3e\u2192\u00d7-rinv p | com {mk In M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk In M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | com {mk Out M P} {mk Out M\u2081 P\u2081} .p = {!!}\n ox\u1d3e\u2192\u00d7-rinv p | endL .p = refl\n ox\u1d3e\u2192\u00d7-rinv p | endR {P} .p = refl\n\n \u00d7\u2192ox\u1d3e-equiv : \u2200 P Q \u2192 Equiv (\u00d7\u2192ox\u1d3e P Q)\n \u00d7\u2192ox\u1d3e-equiv P Q = record { linv = ox\u1d3e\u2192\u00d7 P Q\n ; is-linv = \u03bb { (x , y) \u2192 cong\u2082 _,_ (ox\u1d3e-comma-fst P Q x y) (ox\u1d3e-comma-snd P Q x y) }\n ; rinv = ox\u1d3e\u2192\u00d7 P Q\n ; is-rinv = ox\u1d3e\u2192\u00d7-rinv {P} {Q} }\n -}\n\n \u214b\u1d3e-apply : \u2200 {P Q} \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply {P} {Q} pq p with view-\u214b P Q pq\n \u214b\u1d3e-apply ._ p | sendL' P Q m pq = \u214b\u1d3e-apply {P m} pq (p m)\n \u214b\u1d3e-apply ._ p | sendR' P Q m pq = m , \u214b\u1d3e-apply {com P} {Q m} pq p\n \u214b\u1d3e-apply ._ (m , p) | recvL' P Q pq = \u214b\u1d3e-apply {P m} (pq m) p\n \u214b\u1d3e-apply ._ p | recvR' P Q pq = \u03bb m \u2192 \u214b\u1d3e-apply {com P} {Q m} (pq m) p\n \u214b\u1d3e-apply pq p | endL Q .pq = pq\n \u214b\u1d3e-apply pq p | endR P .pq = _\n\n \u214b\u1d3e-apply' : \u2200 {P Q} \u2192 \u27e6 dual P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b\u1d3e-apply' {P} {Q} pq p = \u214b\u1d3e-apply {dual P} {Q} pq (subst \u27e6_\u27e7 (\u2261.sym (\u2261\u1d3e-sound funExt (dual-involutive P))) p)\n\n \u214b\u1d3e-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b\u1d3e Q \u27e7 \u2192 \u27e6 dual Q \u214b\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218 P Q R pq qr = \u214b\u1d3e-\u2218-view (view-\u2218 P Q R pq qr)\n where\n \u214b\u1d3e-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{qr : \u27e6 dual Q \u214b\u1d3e R \u27e7} \u2192 View-\u2218 P Q R pq qr \u2192 \u27e6 P \u214b\u1d3e R \u27e7\n \u214b\u1d3e-\u2218-view (sendLL P Q R m p qr) = \u214b\u1d3e-sendL R m (\u214b\u1d3e-\u2218 (P m) (com Q) R p qr)\n \u214b\u1d3e-\u2218-view (recvLL P Q R p qr) = \u214b\u1d3e-recvL R (\u03bb m \u2192 \u214b\u1d3e-\u2218 (P m) (com Q) R (p m) qr)\n \u214b\u1d3e-\u2218-view (sendRR P Q R m pq q) = \u214b\u1d3e-sendR (com P) m (\u214b\u1d3e-\u2218 (com P) (com Q) (R m) pq q)\n \u214b\u1d3e-\u2218-view (recvRR P Q R pq q) = \u214b\u1d3e-recvR (com P) (\u03bb m\u2081 \u2192 \u214b\u1d3e-\u2218 (com P) (com Q) (R m\u2081) pq (q m\u2081))\n \u214b\u1d3e-\u2218-view (sendR-recvL P Q R m p q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) p (q m)\n \u214b\u1d3e-\u2218-view (recvR-sendL P Q R p m q) = \u214b\u1d3e-\u2218 (com P) (Q m) (com R) (p m) q\n \u214b\u1d3e-\u2218-view (endL Q R pq qr) = \u214b\u1d3e-apply' {Q} {R} qr pq\n \u214b\u1d3e-\u2218-view (endM P R pq qr) = par (com P) R pq qr\n \u214b\u1d3e-\u2218-view (endR P Q pq qr) = \u214b\u1d3e-apply {com Q} {com P} (fst (\u214b\u1d3e-comm (com P) (com Q)) pq) qr\n\n ox\u1d3e-map : \u2200 P Q R S \u2192 (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P ox\u1d3e R \u27e7 \u2192 \u27e6 Q ox\u1d3e S \u27e7\n ox\u1d3e-map P Q R S f g p = comma\u1d3e Q S (f (ox\u1d3e-fst P R p)) (g (ox\u1d3e-snd P R p))\n\n {-\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b\u1d3e Q) ox\u1d3e R \u27e7 \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL P Q R pqr = switchL' P Q R (ox\u1d3e-fst (P \u214b\u1d3e Q) R pqr) (ox\u1d3e-snd (P \u214b\u1d3e Q) R pqr)\n -}\n\n {-\n switchL' : \u2200 P Q R (pq : \u27e6 P \u214b\u1d3e Q \u27e7) (r : \u27e6 R \u27e7) \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL' P Q R pq r = {!switchL-view {!!}!}\n where\n switchL-view : \u2200 {P Q R}{pq : \u27e6 P \u214b\u1d3e Q \u27e7}{r : \u27e6 R \u27e7} \u2192 \u27e6 P \u214b\u1d3e (Q ox\u1d3e R) \u27e7\n switchL-view {P} {Q} {R} {p\u214bq} {r} with view-\u214b P Q p\u214bq | view-proc R r\n switchL-view | sendL' P\u2081 Q\u2081 m p | vr = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | send M\u2081 P\u2082 m\u2081 p\u2081 = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | sendR' P\u2081 Q\u2081 m p | end = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | send M\u2081 P\u2082 m p\u2081 = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | recvL' P\u2081 Q\u2081 p | end = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | send M\u2081 P\u2082 m p\u2081 = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | recv M\u2081 P\u2082 r\u2081 = {!!}\n switchL-view | recvR' P\u2081 Q\u2081 p | end = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | send M P\u2081 m p = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | recv M P\u2081 r\u2081 = {!!}\n switchL-view | endL Q\u2081 pq\u2081 | end = {!!}\n switchL-view | endR P\u2081 pq\u2081 | send M P\u2082 m p = {!!}\n switchL-view | endR P\u2081 pq\u2081 | recv M P\u2082 r\u2081 = {!!}\n switchL-view | endR P\u2081 pq\u2081 | end = {!!}\n {-\n switchL-view {R = R}{r = r} | sendL' P Q m p = \u214b\u1d3e-sendL {!!} m (switchL' {!!} {!!} {!!} p r)\n switchL-view {R = R}{r = r} | sendR' P Q m p with view-proc R r\n switchL-view | sendR' {M\u2081} P\u2081 Q m\u2081 p\u2081 | send M P m p = {!!}\n switchL-view | sendR' {M\u2081} P\u2081 Q m p | recv M P r = {!!}\n switchL-view | sendR' {M} P Q m p | end = {!!}\n -- {!\u214b\u1d3e-map (com P) (com P) (Q m ox\u1d3e R) (\u03a3\u1d3e M Q ox\u1d3e R) id (ox\u1d3e-map (Q m) (\u03a3\u1d3e M Q) R R (_,_ m) id) (switchL-view (com P) (Q m) R p r)!}\n switchL-view {R = R}{r = r} | recvL' P Q p = \u214b\u1d3e-recvL (com Q ox\u1d3e R) \u03bb m \u2192 switchL' (P m) (com Q) R (p m) r\n switchL-view {R = R}{r = r} | recvR' (\u03a0\u1d9c M' P) Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL-view {R = R}{r = r} | recvR' (\u03a3\u1d9c M' P) Q p = {!!} -- \u214b\u1d3e-map (com P) (com P) {!!} (\u03a0\u1d3e M Q ox\u1d3e R) id {!!} {!!}\n switchL-view {R = R}{r = r} | endL Q p\u214bq = comma\u1d3e Q R p\u214bq r\n switchL-view {R = R}{r = r} | endR P p\u214bq = par (com P) R p\u214bq r\n -}\n -}\n -}\n\n {-\n\n {-\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = Process (dual P) \u2192 Process Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ]\u1d9c \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& end PA PB = PB\nsim& (com P&) (comR s\u2080) (comR s\u2081) = comR (sim&R P& s\u2080 s\u2081)\nsim& (&-comm P&) PA PB = sim& P& PB PA\n\nsim&R (\u03a0& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 m) (s\u2081 m)\nsim&R (\u03a0& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 m) s\u2081)\nsim&R (\u03a0\u2610& In P&) (recv s\u2080) (recv s\u2081) = recv \u03bb m \u2192 sim& (P& m) (s\u2080 [ m ]) (s\u2081 m)\nsim&R (\u03a0\u2610& Out P&) (recv s\u2080) (send m s\u2081) = send m (sim& (P& m) (s\u2080 [ m ]) s\u2081)\n-}\n{-\nsim&-assoc : \u2200 {PA PB PC PAB PBC PABC}\n (PAB& : [ PA & PB \u2261 PAB ])\n (PAB&PC : [ PAB & PC \u2261 PABC ])\n (PBC& : [ PB & PC \u2261 PBC ])\n (PA&PBC : [ PA & PBC \u2261 PABC ])\n (sA : Sim end PA)(sB : Sim end PB)(sC : Sim end PC)\n \u2192 sim& PA&PBC sA (sim& PBC& sB sC) \u2261 sim& PAB&PC (sim& PAB& sA sB) sC\nsim&-assoc PAB& PAB&PC PBC& PA&PBC sA sB sC = {!!}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0\u1d3e M P) = comR (recv \u03bb m \u2192 comL (send m (sim-id (P m))))\nsim-id (\u03a3\u1d3e M P) = comL (recv \u03bb m \u2192 comR (send m (sim-id (P m))))\n\nid\u02e2 : \u2200 {P P'} \u2192 Dual P P' \u2192 Sim P P'\nid\u02e2 end = end\nid\u02e2 (\u03a0\u00b7\u03a3 x) = comL (recv (\u03bb m \u2192 comR (send m (id\u02e2 (x m)))))\nid\u02e2 (\u03a3\u00b7\u03a0 x) = comR (recv (\u03bb m \u2192 comL (send m (id\u02e2 (x m)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual (com Q) (com Q') \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\n--sim-compRL : \u2200 {P Q R} \u2192 SimR P Q \u2192 SimL (dual\u1d9c Q) R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (comL PQ) QR = comL (sim-compL Q-Q' PQ QR)\nsim-comp Q-Q' (comR PQ) (comL QR) = sim-compRL Q-Q' PQ QR\nsim-comp Q-Q' (comR PQ) (comR QR) = comR (sim-compR Q-Q' (comR PQ) QR)\nsim-comp () (comR PQ) end\nsim-comp end end QR = QR\n\nsim-compRL (\u03a0\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ m) QR\nsim-compRL (\u03a3\u00b7\u03a0 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR m)\n{-\nsim-compRL (\u03a0\u2610\u00b7\u03a3 Q-Q') (recv PQ) (send m QR) = sim-comp (Q-Q' m) (PQ [ m ]) QR\nsim-compRL (\u03a3\u00b7\u03a0\u2610 Q-Q') (send m PQ) (recv QR) = sim-comp (Q-Q' m) PQ (QR [ m ])\n-}\n\nsim-compL Q-Q' (recv PQ) QR = recv \u03bb m \u2192 sim-comp Q-Q' (PQ m) QR\nsim-compL Q-Q' (send m PQ) QR = send m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recv QR) = recv \u03bb m \u2192 sim-comp Q-Q' PQ (QR m)\nsim-compR Q-Q' PQ (send m QR) = send m (sim-comp Q-Q' PQ QR)\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (comL x) = comR (sim-symL x)\n!\u02e2 (comR x) = comL (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (send m PQ) = send m (!\u02e2 PQ)\n\nsim-symR (recv PQ) = recv (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (send m PQ) = send m (!\u02e2 PQ)\n\n!\u02e2-cong : \u2200 {P Q} {s\u2080 s\u2081 : Sim P Q} \u2192 s\u2080 \u2248\u02e2 s\u2081 \u2192 !\u02e2 s\u2080 \u2248\u02e2 !\u02e2 s\u2081\n!\u02e2-cong \u2248-end = \u2248-end\n!\u02e2-cong (\u2248-sendL m p) = \u2248-sendR m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-sendR m p) = \u2248-sendL m (!\u02e2-cong p)\n!\u02e2-cong (\u2248-recvL x) = \u2248-recvR (\u03bb m \u2192 !\u02e2-cong (x m))\n!\u02e2-cong (\u2248-recvR x) = \u2248-recvL (\u03bb m \u2192 !\u02e2-cong (x m))\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process P\nsim-unit (comR (recv P)) = com (recv (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (comR (send m P)) = com (send m (sim-unit P))\nsim-unit end = end\n\nmutual\n Sim\u1d3e\u2192SimR : \u2200 {P Q} \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P)) \u2192 SimR (com P) Q\n Sim\u1d3e\u2192SimR (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimR (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192SimL : \u2200 {P Q} \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q)) \u2192 SimL P (com Q)\n Sim\u1d3e\u2192SimL (recv s) = recv \u03bb m \u2192 Sim\u1d3e\u2192Sim (s m)\n Sim\u1d3e\u2192SimL (send m s) = send m (Sim\u1d3e\u2192Sim s)\n\n Sim\u1d3e\u2192Sim : \u2200 {P Q} \u2192 Sim end (Sim\u1d3e P Q) \u2192 Sim P Q\n Sim\u1d3e\u2192Sim {end} s = s\n Sim\u1d3e\u2192Sim {com _} {end} s = !\u02e2 s\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `L (comR s))) = comL (Sim\u1d3e\u2192SimL s)\n Sim\u1d3e\u2192Sim {com _} {com _} (comR (send `R (comR s))) = comR (Sim\u1d3e\u2192SimR s)\n\nmutual\n Sim\u2192Sim\u1d3eR : \u2200 {P Q} \u2192 SimR (com P) Q \u2192 ProcessF (Sim end) (Q >>\u1d9c Sim\u1d3e (com P))\n Sim\u2192Sim\u1d3eR (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eR (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3eL : \u2200 {P Q} \u2192 SimL P (com Q) \u2192 ProcessF (Sim end) (P >>\u1d9c flip Sim\u1d3e (com Q))\n Sim\u2192Sim\u1d3eL (recv s) = recv \u03bb m \u2192 Sim\u2192Sim\u1d3e (s m)\n Sim\u2192Sim\u1d3eL (send m s) = send m (Sim\u2192Sim\u1d3e s)\n\n Sim\u2192Sim\u1d3e : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim end (Sim\u1d3e P Q)\n Sim\u2192Sim\u1d3e {end} s = s\n Sim\u2192Sim\u1d3e {com _} {end} s = !\u02e2 s\n Sim\u2192Sim\u1d3e {com _} {com _} (comL s) = comR (send `L (comR (Sim\u2192Sim\u1d3eL s)))\n Sim\u2192Sim\u1d3e {com _} {com _} (comR s) = comR (send `R (comR (Sim\u2192Sim\u1d3eR s)))\n\n {-\nmutual\n Sim\u1d3e-assocR : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cR Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocR {end}{Q}{R} s = {!!}\n Sim\u1d3e-assocR {\u03a0\u1d3e M P}{Q}{R}(comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assocR {\u03a3\u1d3e M P}{Q}{R}(comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assocR {com (mk io M P)} {Q} {mk .In M\u2082 R} (comR (recv s)) = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = com Q} {R m} (s m)))\n Sim\u1d3e-assocR {com (mk io M P)} {mk io\u2081 M\u2081 Q} {mk .Out M\u2082 R} (comR (send m s)) = comR (send m (Sim\u1d3e-assoc {com (mk io M P)} {com (mk io\u2081 M\u2081 Q)} {R m} s))\n\n Sim\u1d3e-assocL : \u2200 {P Q R} \u2192 Sim P (Sim\u1d9cL Q R) \u2192 Sim (Sim\u1d3e P (com Q)) (com R)\n Sim\u1d3e-assocL s = {!!}\n\n Sim\u1d3e-assoc : \u2200 {P Q R} \u2192 Sim P (Sim\u1d3e Q R) \u2192 Sim (Sim\u1d3e P Q) R\n Sim\u1d3e-assoc {end} s = Sim\u1d3e\u2192Sim s\n Sim\u1d3e-assoc {com _} {end} s = s\n Sim\u1d3e-assoc {com _} {com _} {end} s = !\u02e2 {!s!}\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (recv s)) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comL (send m s)) = comL (send `L (comL (send m (Sim\u1d3e-assoc s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `L (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocL (s m)))))\n Sim\u1d3e-assoc {com ._} {com Q} {com R} (comR (send `L (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocL s))))\n Sim\u1d3e-assoc {com P} {\u03a0\u1d3e M Q} {com R} (comR (send `L (comR (recv s))))\n = comL (send `R (comL (recv (\u03bb m \u2192 Sim\u1d3e-assoc {Q = Q m} (s m)))))\n Sim\u1d3e-assoc {com P} {\u03a3\u1d3e M Q} {com R} (comR (send `L (comR (send m s))))\n = comL (send `R (comL (send m (Sim\u1d3e-assoc {Q = Q m} s))))\n Sim\u1d3e-assoc {\u03a0\u1d3e M P} {com Q} {com R} (comR (send `R (comL (recv s)))) = comL (send `L (comL (recv (\u03bb m \u2192 Sim\u1d3e-assocR (s m)))))\n Sim\u1d3e-assoc {\u03a3\u1d3e M P} {com Q} {com R} (comR (send `R (comL (send m s)))) = comL (send `L (comL (send m (Sim\u1d3e-assocR s))))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a0\u1d3e M R} (comR (send `R (comR (recv s))))\n = comR (recv (\u03bb m \u2192 Sim\u1d3e-assoc {com P} {com Q} {R m} (s m)))\n Sim\u1d3e-assoc {com P} {com Q} {\u03a3\u1d3e M R} (comR (send `R (comR (send m s))))\n = comR (send m (Sim\u1d3e-assoc {com P} {com Q} {R m} s))\n-}\n{-\nSim\u1d3e-assoc : \u2200 {P Q R} \u2192 \u27e6 Sim\u1d3e P (Sim\u1d3e Q R) \u27e7 \u2192 \u27e6 Sim\u1d3e (Sim\u1d3e P Q) R \u27e7\nSim\u1d3e-assoc {end} s = s\nSim\u1d3e-assoc {com _} {end} s = s\nSim\u1d3e-assoc {com _} {com _} {end} s = s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`L , s) = `L , `L , Sim\u1d3e-assoc {com ?} {com Q} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `L , s) = `L , `R , Sim\u1d3e-assoc {com P} {{!com ?!}} {com R} s\nSim\u1d3e-assoc {com P} {com Q} {com R} (`R , `R , s) = {!!}\n-}\n\n{-\nmodule 3-way-trace where\n trace : \u2200 {P P' Q Q'} \u2192 Dual P P' \u2192 Dual Q Q' \u2192 Sim end P' \u2192 Sim P Q \u2192 Sim Q' end\n \u2192 Log P \u00d7 Log Q\n trace (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)\n trace (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)\n {-\n trace (\u03a0\u2610\u00b7\u03a3 x\u2081) Q-Q' (comR (send x x\u2082)) (comL (recv x\u2083)) Q\u00b7 = {!first (_,_ x) (trace (x\u2081 x) Q-Q' x\u2082 (x\u2083 x) Q\u00b7)!}\n trace (\u03a3\u00b7\u03a0\u2610 x\u2081) Q-Q' (comR (recv x)) (comL (send x\u2082 x\u2083)) Q\u00b7 = {!first (_,_ x\u2082) (trace (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 Q\u00b7)!}\n -}\n trace P-P' (\u03a0\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)\n trace P-P' (\u03a3\u00b7\u03a0 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))\n {-\n trace P-P' (\u03a0\u2610\u00b7\u03a3 x\u2081) \u00b7P (comR (recv x)) (comL (send x\u2082 x\u2083)) = {!second (_,_ x\u2082) (trace P-P' (x\u2081 x\u2082) \u00b7P (x x\u2082) x\u2083)!}\n trace P-P' (\u03a3\u00b7\u03a0\u2610 x\u2081) \u00b7P (comR (send x x\u2082)) (comL (recv x\u2083)) = {!second (_,_ x) (trace P-P' (x\u2081 x) \u00b7P x\u2082 (x\u2083 x))!}\n -}\n trace P-P' Q-Q' \u00b7P end Q\u00b7 = _\n\n module _ {P Q : Proto} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {P' Q'}(P-P' : Dual P P')(Q-Q' : Dual Q Q') \u2192 (\u00b7P : Sim end P')(Q\u00b7 : Sim Q' end)\n \u2192 trace P-P' Q-Q' \u00b7P PQ Q\u00b7 \u2261 trace P-P' Q-Q' \u00b7P PQ' Q\u00b7\n\nmodule _ where\n trace : \u2200 {B E} \u2192 Sim (Trace B) (Trace E) \u2192 Log B \u00d7 Log E\n trace {end} {end} end = _\n trace {com _} {end} (comL (send m s)) = first (_,_ m) (trace s)\n trace {end} {com _} (comR (send m s)) = second (_,_ m) (trace s)\n trace {com _} {com c} (comL (send m s)) = first (_,_ m) (trace {E = com c} s)\n trace {com c} {com _} (comR (send m s)) = second (_,_ m) (trace {com c} s)\n\n module _ {P Q} where\n _\u2248_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u2248 PQ' = \u2200 {B P' Q' E} \u2192 (P'-P : Dual P' P)(Q-Q' : Dual Q Q')(BP : Sim (Trace B) P')(QE : Sim Q' (Trace E))\n \u2192 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ QE)) \u2261 trace (sim-comp P'-P BP (sim-comp Q-Q' PQ' QE))\n\nmodule _ {{_ : FunExt}} where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u00b7\u03a3 x) = cong \u03a0\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0 x) = cong \u03a3\u00b7\u03a0 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n {-\n Dual-sym-sym (\u03a0\u2610\u00b7\u03a3 x) = cong \u03a0\u2610\u00b7\u03a3 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u00b7\u03a0\u2610 x) = cong \u03a3\u00b7\u03a0\u2610 (funExt (\u03bb y \u2192 Dual-sym-sym (x y)))\n -}\n\nmodule _ {{_ : FunExt}} where\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u2261 sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' (comL (recv x)) PQ QR = cong (comL \u2218 recv) (funExt (\u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (x m) PQ QR))\n sim-comp-assoc P-P' Q-Q' (comL (send m x)) PQ QR = cong (comL \u2218 send m) (sim-comp-assoc P-P' Q-Q' x PQ QR)\n sim-comp-assoc (\u03a0\u00b7\u03a3 x\u2081) Q-Q' (comR (recv x)) (comL (send m x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' (x m) x\u2082 QR\n sim-comp-assoc (\u03a3\u00b7\u03a0 x\u2081) Q-Q' (comR (send m x)) (comL (recv x\u2082)) QR = sim-comp-assoc (x\u2081 m) Q-Q' x (x\u2082 m) QR\n sim-comp-assoc P-P' (\u03a0\u00b7\u03a3 x\u2082) (comR x) (comR (recv x\u2081)) (comL (send m x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) (x\u2081 m) x\u2083\n sim-comp-assoc P-P' (\u03a3\u00b7\u03a0 x\u2082) (comR x) (comR (send m x\u2081)) (comL (recv x\u2083)) = sim-comp-assoc P-P' (x\u2082 m) (comR x) x\u2081 (x\u2083 m)\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (recv x\u2082)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (x\u2082 m))\n sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) (comR (send m x\u2082)) = cong (comR \u2218 send m) (sim-comp-assoc P-P' Q-Q' (comR x) (comR x\u2081) x\u2082)\n sim-comp-assoc end Q-Q' end PQ QR = refl\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u2261 WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n {-\n sim-id-comp : \u2200 {P P' Q}(P-P' : Dual P P')(s : Sim P' Q) \u2192 sim-comp P-P' (id\u02e2 (Dual-sym P-P')) s \u2261 s\n sim-id-comp end s = refl\n sim-id-comp (\u03a0\u00b7\u03a3 x) s = {!!}\n sim-id-comp (\u03a3\u00b7\u03a0 x) s = {!!}\n\n module _ (A : \u2605) where\n Test : Proto\n Test = A \u00d7' end\n\n s : A \u2192 Sim Test Test\n s m = comR (send m (comL (send m end)))\n\n s' : Sim (dual Test) (dual Test)\n s' = comR (recv (\u03bb m \u2192 comL (recv (\u03bb m\u2081 \u2192 end))))\n\n prf : \u2200 x \u2192 s x \u2666 sim-id _ \u2261 s x\n prf x = {!!}\n\n c-prf : \u2200 x \u2192 sim-id _ \u2666 s x \u2261 s x\n c-prf x = {!!}\n\n c-prf' : sim-id _ \u2666 s' \u2261 s'\n c-prf' = {!!}\n\n prf' : s' \u2666 sim-id _ \u2261 s'\n prf' = {!!}\n\n sim-comp-id : \u2200 {P Q}(s : Sim P Q) \u2192 s \u2666 (sim-id Q) \u2261 s\n sim-comp-id (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comL (send m x)) = cong (comL \u2218 send m) (sim-comp-id x)\n sim-comp-id (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-comp-id (x m))\n sim-comp-id (comR (send m x)) = {!cong (comR \u2218 send m) (sim-comp-id x)!}\n sim-comp-id end = refl\n -}\n\nmodule _ {{_ : FunExt}}\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (comL (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comL (send x x\u2081)) = cong (comL \u2218 send x) (sim-!! x\u2081)\n sim-!! (comR (recv x)) = cong (comR \u2218 recv) (funExt \u03bb m \u2192 sim-!! (x m))\n sim-!! (comR (send x x\u2081)) = cong (comR \u2218 send x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u2248\u02e2 !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' (comL (recv s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (recv s)) (comR (send m s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (recv s\u2081)) = TODO where postulate TODO : _\n sim-comp-! Q-Q' (comL (send m s)) (comR (send m\u2081 s\u2081)) = \u2248\u02e2-trans (\u2248\u02e2-trans (\u2248-sendL m\u2081 (\u2248\u02e2-trans (sim-comp-! Q-Q' (sendL m s) s\u2081) (\u2248-sendR m (\u2248\u02e2-sym (sim-comp-! Q-Q' s s\u2081))))) (\u2248-sendLR m\u2081 m)) (\u2248-sendR m (sim-comp-! Q-Q' s (comR (send m\u2081 s\u2081))))\n sim-comp-! Q-Q' (comL (recv s)) (comL (recv s\u2081)) = \u2248-recvR \u03bb m \u2192 sim-comp-! Q-Q' (s m) (comL (recv s\u2081))\n sim-comp-! Q-Q' (comL (send m s)) (comL (recv s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (recv s\u2081)))\n sim-comp-! Q-Q' (comL (send m s)) (comL (send m\u2081 s\u2081)) = \u2248-sendR m (sim-comp-! Q-Q' s (comL (send m\u2081 s\u2081)))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comL (send m s\u2081)) = sim-comp-! (Q-Q' m) (s m) s\u2081\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comL (recv s)) (comL (send m s\u2081)) = \u2248-recvR \u03bb m\u2081 \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (s m\u2081) (comL (send m s\u2081))\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m \u2192 sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (s\u2081 m)\n sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) (comR (send m s\u2081)) = \u2248-sendL m (sim-comp-! (\u03a0\u00b7\u03a3 Q-Q') (comR (recv s)) s\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comL (recv s\u2081)) = sim-comp-! (Q-Q' m) s (s\u2081 m)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (recv s\u2081)) = \u2248-recvL \u03bb m\u2081 \u2192 sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (s\u2081 m\u2081)\n sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) (comR (send m\u2081 s\u2081)) = \u2248-sendL m\u2081 (sim-comp-! (\u03a3\u00b7\u03a0 Q-Q') (comR (send m s)) s\u2081)\n sim-comp-! end (comL (recv s)) end = \u2248-recvR \u03bb m \u2192 sim-comp-! end (s m) end\n sim-comp-! end (comL (send m s)) end = \u2248-sendR m (sim-comp-! end s end)\n sim-comp-! end end (comR (recv s)) = \u2248-recvL \u03bb m \u2192 sim-comp-! end end (s m)\n sim-comp-! end end (comR (send m s)) = \u2248-sendL m (sim-comp-! end end s)\n sim-comp-! end end end = \u2248-end\n\n {-\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (\u03a0\u00b7\u03a3 x\u2081) (comR (recv x)) (comL (send x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u00b7\u03a0 x) (comR (send x\u2081 x\u2082)) (comL (recv x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (comR x) (comR (recv x\u2081))\n = cong (comL \u2218 recv) (funExt (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (comR x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (comR x) (comR (send x\u2081 x\u2082))\n = cong (comL \u2218 send x\u2081) (sim-comp-!-end Q-Q' (comR x) x\u2082)\n sim-comp-!-end end end (comR (recv x)) = cong (comL \u2218 recv) (funExt \u03bb m \u2192 sim-comp-!-end end end (x m))\n sim-comp-!-end end end (comR (send m x)) = cong (comL \u2218 send m) (sim-comp-!-end end end x)\n sim-comp-!-end end end end = refl\n\n open \u2261-Reasoning\n\n module _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc funExt R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExt Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc funExt Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n-}\n\n {-\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n -}\n -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = \u03a3\u2610\u1d3e Secret \u03bb s \u2192\n \u03a0\u1d3e Guess \u03bb g \u2192\n \u03a3\u1d3e S< s > \u03bb _ \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = \u03a3\u1d3e G \u03bb g\u02b3 \u2192 -- commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e \u2124q \u03bb s \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover x y\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb g\u02b3 \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb s \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u03bb _ \u2192\n end\n\n Honest-Prover' : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Prover' x S[ y \u2225 _ ]\n = \u03a3\u2610\u1d3e \u2124q \u03bb r \u2192 -- ideal commitment\n \u03a3\u1d3e S< g ^ r > \u03bb { S[ g\u02b3 \u2225 _ ] \u2192 -- real commitment\n \u03a0\u1d3e \u2124q \u03bb c \u2192 -- challenge\n \u03a3\u1d3e S< r + (c * x) > \u03bb { S[ s \u2225 _ ] \u2192 -- response\n \u03a0\u1d3e (Dec ((g ^ s) \u2261 (g\u02b3 \u00b7 (y ^ c)))) \u03bb _ \u2192\n end } }\n\n Honest-Verifier : ..(x : \u2124q) (y : S< g ^ x >) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S< g ^ x >) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S< g ^ x >) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"c9be4e22888383844a7b653557e9463330618caa","subject":"more startx proofs","message":"more startx proofs\n","repos":"crypto-agda\/crypto-agda","old_file":"Control\/Protocol\/Choreography.agda","new_file":"Control\/Protocol\/Choreography.agda","new_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level\nopen import Data.Product\nopen import Data.One\n\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Control.Protocol.Choreography where\nopen import Control.Strategy renaming (Strategy to Client) public\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata Mod : \u2605 where S D : Mod\n\n\u2192M : \u2200 {a b} \u2192 Mod \u2192 \u2605_ a \u2192 \u2605_ b \u2192 \u2605_ (a \u2294 b)\n\u2192M S A B = ..(_ : A) \u2192 B\n\u2192M D A B = A \u2192 B\n\n\u03a0M : \u2200 {a b}(m : Mod) \u2192 (A : \u2605_ a) \u2192 (B : \u2192M m A (\u2605_ b)) \u2192 \u2605_ (a \u2294 b)\n\u03a0M S A B = \u03a0\u00b7 A B\n\u03a0M D A B = \u03a0 A B\n\n-- appM : \u2200 {a b}(m : Mod){A : \u2605_ a}{B : \u2192M m A (\u2605_ b)}(P : \u03a0M m A B)(x : A) \u2192 B\n\ndata Proto : \u2605\u2081 where\n end : Proto\n \u03a0' \u03a3' : (f : Mod)(A : \u2605)(B : \u2192M f A Proto) \u2192 Proto\n\n{-\nTele : Proto \u2192 \u2605\nTele end = \ud835\udfd9\nTele (\u03a0' A B) = \u03a3 A \u03bb x \u2192 Tele (B x)\nTele (\u03a3' A B) = \u03a3 A \u03bb x \u2192 Tele (B x)\nTele (later i P) = ?\n\n_>>\u2261_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>\u2261 Q = Q _\n\u03a0' A B >>\u2261 Q = \u03a0' A (\u03bb x \u2192 B x >>\u2261 (\u03bb xs \u2192 Q (x , xs)))\n\u03a3' A B >>\u2261 Q = \u03a3' A (\u03bb x \u2192 B x >>\u2261 (\u03bb xs \u2192 Q (x , xs)))\nlater i P >>\u2261 Q = ?\n\n++Tele : \u2200 (P : Proto)(Q : Tele P \u2192 Proto) \u2192 (x : Tele P) \u2192 Tele (Q x) \u2192 Tele (P >>\u2261 Q)\n++Tele end Q x y = y\n++Tele (\u03a0' M C) Q (m , x) y = m , ++Tele (C m) (\u03bb x\u2081 \u2192 Q (m , x\u2081)) x y\n++Tele (\u03a3' M C) Q (m , x) y = m , ++Tele (C m) _ x y\n++Tele (later i P) Q x y = ?\n\nmodule _ (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : \u2605_ b}{f g : A \u2192 B} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n right-unit : \u2200 (P : Proto) \u2192 (P >>\u2261 \u03bb x \u2192 end) \u2261 P\n right-unit end = refl\n right-unit (\u03a0' M C) = let p = funExt (\u03bb x \u2192 right-unit (C x)) in cong (\u03a0' M) p\n right-unit (\u03a3' M C) = cong (\u03a3' M) (funExt (\u03bb x \u2192 right-unit (C x)))\n right-unit (later i P) = ?\n\n assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>\u2261 Q) \u2192 Proto)\n \u2192 P >>\u2261 (\u03bb x \u2192 Q x >>\u2261 (\u03bb y \u2192 R (++Tele P Q x y))) \u2261 ((P >>\u2261 Q) >>\u2261 R)\n assoc end Q R = refl\n assoc (\u03a0' M C\u2081) Q R = cong (\u03a0' M) (funExt (\u03bb x \u2192 assoc (C\u2081 x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb xs \u2192 R (x , xs))))\n assoc (\u03a3' M C\u2081) Q R = cong (\u03a3' M) (funExt (\u03bb x \u2192 assoc (C\u2081 x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb xs \u2192 R (x , xs))))\n assoc (later i P) Q R = ?\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>\u2261 \u03bb _ \u2192 Q\n-}\n\n_\u00d7'_ : Set \u2192 Proto \u2192 Proto\nA \u00d7' B = \u03a3' D A \u03bb _ \u2192 B\n\n_\u2192'_ : Set \u2192 Proto \u2192 Proto\nA \u2192' B = \u03a0' D A \u03bb _ \u2192 B\n\ndual : Proto \u2192 Proto\ndual end = end\ndual (\u03a0' S A B) = \u03a3' S A (\u03bb x \u2192 dual (B x))\ndual (\u03a0' D A B) = \u03a3' D A (\u03bb x \u2192 dual (B x))\ndual (\u03a3' S A B) = \u03a0' S A (\u03bb x \u2192 dual (B x))\ndual (\u03a3' D A B) = \u03a0' D A (\u03bb x \u2192 dual (B x))\n\ndata Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u03a3'S : \u2200 {A B B'} \u2192 (\u2200 ..x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a0' S A B) (\u03a3' S A B')\n \u03a0\u03a3'D : \u2200 {A B B'} \u2192 (\u2200 x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a0' D A B) (\u03a3' D A B')\n \u03a3\u03a0'S : \u2200 {A B B'} \u2192 (\u2200 ..x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a3' S A B) (\u03a0' S A B')\n \u03a3\u03a0'D : \u2200 {A B B'} \u2192 (\u2200 x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a3' D A B) (\u03a0' D A B')\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u03a3'S f) = \u03a3\u03a0'S (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a0\u03a3'D f) = \u03a3\u03a0'D (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u03a0'S f) = \u03a0\u03a3'S (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u03a0'D f) = \u03a0\u03a3'D (\u03bb x \u2192 Dual-sym (f x))\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0' S A B) = \u03a0\u03a3'S (\u03bb x \u2192 Dual-spec (B x))\nDual-spec (\u03a0' D A B) = \u03a0\u03a3'D (\u03bb x \u2192 Dual-spec (B x))\nDual-spec (\u03a3' S A B) = \u03a3\u03a0'S (\u03bb x \u2192 Dual-spec (B x))\nDual-spec (\u03a3' D A B) = \u03a3\u03a0'D (\u03bb x \u2192 Dual-spec (B x))\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele P \u2261 Tele (dual P)\n dual-Tele end = refl\n dual-Tele (\u03a0' A B) = cong (\u03a3 A) (funExt (\u03bb x \u2192 dual-Tele (B x)))\n dual-Tele (\u03a3' A B) = cong (\u03a3 A) (funExt (\u03bb x \u2192 dual-Tele (B x)))\n dual-Tele (later i P) = ?\n-}{-\nmodule _ X where\n El : Proto \u2192 \u2605\n El end = X\n El (\u03a0' A B) = \u03a0 A \u03bb x \u2192 El (B x)\n El (\u03a3' A B) = \u03a3 A \u03bb x \u2192 El (B x)\nmodule _ where\n El : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\n El end X = X _\n El (\u03a0' A B) X = \u03a0 A \u03bb x \u2192 El (B x) (\u03bb y \u2192 X (x , y))\n El (\u03a3' A B) X = \u03a3 A \u03bb x \u2192 El (B x) (\u03bb y \u2192 X (x , y))\n El (later i P) = ?\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n bind-spec : (P : Proto)(Q : Tele P \u2192 Proto)(X : Tele (P >>\u2261 Q) \u2192 \u2605) \u2192 El (P >>\u2261 Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P Q x y)))\n bind-spec end Q X = refl\n bind-spec (\u03a0' A B) Q X = cong (\u03a0 A) (funExt (\u03bb x \u2192 bind-spec (B x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb y \u2192 X (x , y))))\n bind-spec (\u03a3' A B) Q X = cong (\u03a3 A) (funExt (\u03bb x \u2192 bind-spec (B x) _ _))\n bind-spec (later i p) Q X = ?\n\n\nmodule _ {A B} where\n com : (P : Proto) \u2192 El P (const A) \u2192 El (dual P) (const B) \u2192 A \u00d7 B\n com end a b = a , b\n com (\u03a0' A B) f (x , y) = com (B x) (f x) y\n com (\u03a3' A B) (x , y) f = com (B x) y (f x)\n com (later i P) x y = ?\n\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n com-cont : (P : Proto){A : Tele P \u2192 \u2605}{B : Tele (dual P) \u2192 \u2605} \u2192 El P A \u2192 El (dual P) B \u2192 \u03a3 (Tele P) A \u00d7 \u03a3 (Tele (dual P)) B\n com-cont end p q = (_ , p) , (_ , q)\n com-cont (\u03a0' A B) p (m , q) with com-cont (B m) (p m) q\n ... | (X , a) , (Y , b) = ((m , X) , a) , (m , Y) , b\n com-cont (\u03a3' A B) (m , p) q with com-cont (B m) p (q m)\n ... | (X , a) , (Y , b) = ((m , X) , a) , (m , Y) , b\n com-cont (later i P) p q = ?\n-}\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Proto \u2192 \u2605\u2081 where\n recvD : \u2200 {M P} \u2192 (\u03a0M D M \u03bb m \u2192 this (P m)) \u2192 ProcessF this (\u03a0' D M P)\n recvS : \u2200 {M P} \u2192 (\u03a0M S M \u03bb m \u2192 this (P m)) \u2192 ProcessF this (\u03a0' S M P)\n sendD : \u2200 {M P} \u2192 \u03a0M D M (\u03bb m \u2192 this (P m) \u2192 ProcessF this (\u03a3' D M P))\n sendS : \u2200 {M P} \u2192 \u03a0M S M (\u03bb m \u2192 this (P m) \u2192 ProcessF this (\u03a3' S M P))\n\ndata Process (A : \u2605) : Proto \u2192 \u2605\u2081 where\n do : \u2200 {P} \u2192 ProcessF (Process A) P \u2192 Process A P\n end : A \u2192 Process A end\n\nmutual\n SimL : Proto \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Proto \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n left : \u2200 {P Q} \u2192 SimL P Q \u2192 Sim P Q\n right : \u2200 {P Q} \u2192 SimR P Q \u2192 Sim P Q\n end : Sim end end\n\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = \u2200 {A} \u2192 Process A (dual P) \u2192 Process A Q\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0' S A B) = right (recvS (\u03bb x \u2192 left (sendS x (sim-id (B x)))))\nsim-id (\u03a0' D A B) = right (recvD (\u03bb x \u2192 left (sendD x (sim-id (B x)))))\nsim-id (\u03a3' S A B) = left (recvS (\u03bb x \u2192 right (sendS x (sim-id (B x)))))\nsim-id (\u03a3' D A B) = left (recvD (\u03bb x \u2192 right (sendD x (sim-id (B x)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (left x) QR = left (sim-compL Q-Q' x QR)\nsim-comp Q-Q' (right x) (left x\u2081) = sim-compRL Q-Q' x x\u2081\nsim-comp Q-Q' (right x) (right x\u2081) = right (sim-compR Q-Q' (right x) x\u2081)\nsim-comp end (right x) end = right x\nsim-comp end end QR = QR\n\nsim-compRL end () QR\nsim-compRL (\u03a0\u03a3'S x\u2081) (recvS x) (sendS x\u2082 x\u2083) = sim-comp (x\u2081 x\u2082) (x x\u2082) x\u2083\nsim-compRL (\u03a0\u03a3'D x\u2081) (recvD x) (sendD x\u2082 x\u2083) = sim-comp (x\u2081 x\u2082) (x x\u2082) x\u2083\nsim-compRL (\u03a3\u03a0'S x) (sendS x\u2081 x\u2082) (recvS x\u2083) = sim-comp (x x\u2081) x\u2082 (x\u2083 x\u2081)\nsim-compRL (\u03a3\u03a0'D x) (sendD x\u2081 x\u2082) (recvD x\u2083) = sim-comp (x x\u2081) x\u2082 (x\u2083 x\u2081)\n\nsim-compL Q-Q' (recvD PQ) QR = recvD (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR)\nsim-compL Q-Q' (recvS PQ) QR = recvS (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR)\nsim-compL Q-Q' (sendD m PQ) QR = sendD m (sim-comp Q-Q' PQ QR)\nsim-compL Q-Q' (sendS m PQ) QR = sendS m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recvD QR) = recvD (\u03bb m \u2192 sim-comp Q-Q' PQ (QR m))\nsim-compR Q-Q' PQ (recvS QR) = recvS (\u03bb x \u2192 sim-comp Q-Q' PQ (QR x))\nsim-compR Q-Q' PQ (sendD m QR) = sendD m (sim-comp Q-Q' PQ QR)\nsim-compR Q-Q' PQ (sendS m QR) = sendS m (sim-comp Q-Q' PQ QR)\n\n{-\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-comp Q-Q' (left (recvD PQ)) QR = left (recvD (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR))\nsim-comp Q-Q' (left (recvS PQ)) QR = left (recvS (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR))\nsim-comp Q-Q' (left (sendD m PQ)) QR = left (sendD m (sim-comp Q-Q' PQ QR))\nsim-comp Q-Q' (left (sendS m PQ)) QR = left (sendS m (sim-comp Q-Q' PQ QR))\nsim-comp end (right ()) (left x\u2081)\nsim-comp end end QR = QR\nsim-comp end PQ end = PQ\nsim-comp (\u03a0\u03a3'S Q-Q') (right (recvS PQ)) (left (sendS m QR)) = sim-comp (Q-Q' m) (PQ m) QR\nsim-comp (\u03a0\u03a3'D Q-Q') (right (recvD PQ)) (left (sendD m QR)) = sim-comp (Q-Q' m) (PQ m) QR\nsim-comp (\u03a3\u03a0'S Q-Q') (right (sendS m PQ)) (left (recvS QR)) = sim-comp (Q-Q' m) PQ (QR m)\nsim-comp (\u03a3\u03a0'D Q-Q') (right (sendD m PQ)) (left (recvD QR)) = sim-comp (Q-Q' m) PQ (QR m)\nsim-comp Q-Q' PQ (right (recvD QR)) = right (recvD (\u03bb m \u2192 sim-comp Q-Q' PQ (QR m)))\nsim-comp Q-Q' PQ (right (recvS QR)) = right (recvS (\u03bb x \u2192 sim-comp Q-Q' PQ (QR x)))\nsim-comp Q-Q' PQ (right (sendD m QR)) = right (sendD m (sim-comp Q-Q' PQ QR))\nsim-comp Q-Q' PQ (right (sendS m QR)) = right (sendS m (sim-comp Q-Q' PQ QR))\n-}\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (left x) = right (sim-symL x)\n!\u02e2 (right x) = left (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recvD PQ) = recvD (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (recvS PQ) = recvS (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (sendD m PQ) = sendD m (!\u02e2 PQ)\nsim-symL (sendS m PQ) = sendS m (!\u02e2 PQ)\n\nsim-symR (recvD PQ) = recvD (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (recvS PQ) = recvS (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (sendD m PQ) = sendD m (!\u02e2 PQ)\nsim-symR (sendS m PQ) = sendS m (!\u02e2 PQ)\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process \ud835\udfd9 P\nsim-unit (left ())\nsim-unit (right (recvD P)) = do (recvD (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (right (recvS P)) = do (recvS (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (right (sendD m P)) = do (sendD m (sim-unit P))\nsim-unit (right (sendS m P)) = do (sendS m (sim-unit P))\nsim-unit end = end 0\u2081\n\nmodule _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\nmodule _\n (funExtD : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n (funExtS : \u2200 {a}{b}{A : \u2605_ a}{B : ..(_ : A) \u2192 \u2605_ b}{f g : ..(x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u03a3'S x) = cong \u03a0\u03a3'S (funExtS (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a0\u03a3'D x) = cong \u03a0\u03a3'D (funExtD (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u03a0'S x) = cong \u03a3\u03a0'S (funExtS (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u03a0'D x) = cong \u03a3\u03a0'D (funExtD (\u03bb y \u2192 Dual-sym-sym (x y)))\n\nmodule _\n (funExtD : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n (funExtS : \u2200 {a}{b}{A : \u2605_ a}{B : ..(_ : A) \u2192 \u2605_ b}{f g : ..(x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n sim-comp-assoc-end : \u2200 {P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (\u00f8P : Sim end P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' \u00f8P PQ) QR\n \u2261 sim-comp P-P' \u00f8P (sim-comp Q-Q' PQ QR)\n sim-comp-assoc-end P-P' Q-Q' (left ()) PQ QR\n sim-comp-assoc-end end Q-Q' (right ()) (left PQ) QR\n sim-comp-assoc-end (\u03a0\u03a3'S x\u2081) Q-Q' (right (recvS x)) (left (sendS x\u2082 x\u2083)) QR\n = sim-comp-assoc-end (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 QR\n sim-comp-assoc-end (\u03a0\u03a3'D x\u2081) Q-Q' (right (recvD x)) (left (sendD x\u2082 x\u2083)) QR\n = sim-comp-assoc-end (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 QR\n sim-comp-assoc-end (\u03a3\u03a0'S x) Q-Q' (right (sendS x\u2081 x\u2082)) (left (recvS x\u2083)) QR\n = sim-comp-assoc-end (x x\u2081) Q-Q' x\u2082 (x\u2083 x\u2081) QR\n sim-comp-assoc-end (\u03a3\u03a0'D x) Q-Q' (right (sendD x\u2081 x\u2082)) (left (recvD x\u2083)) QR\n = sim-comp-assoc-end (x x\u2081) Q-Q' x\u2082 (x\u2083 x\u2081) QR\n sim-comp-assoc-end P-P' end (right \u00f8P) (right ()) (left x\u2081)\n sim-comp-assoc-end P-P' (\u03a0\u03a3'S x\u2081) (right \u00f8P) (right (recvS x)) (left (sendS x\u2082 x\u2083))\n = sim-comp-assoc-end P-P' (x\u2081 x\u2082) (right \u00f8P) (x x\u2082) x\u2083\n sim-comp-assoc-end P-P' (\u03a0\u03a3'D x\u2081) (right \u00f8P) (right (recvD x)) (left (sendD x\u2082 x\u2083))\n = sim-comp-assoc-end P-P' (x\u2081 x\u2082) (right \u00f8P) (x x\u2082) x\u2083\n sim-comp-assoc-end P-P' (\u03a3\u03a0'S x) (right \u00f8P) (right (sendS x\u2081 x\u2082)) (left (recvS x\u2083))\n = sim-comp-assoc-end P-P' (x x\u2081) (right \u00f8P) x\u2082 (x\u2083 x\u2081)\n sim-comp-assoc-end P-P' (\u03a3\u03a0'D x) (right \u00f8P) (right (sendD x\u2081 x\u2082)) (left (recvD x\u2083))\n = sim-comp-assoc-end P-P' (x x\u2081) (right \u00f8P) x\u2082 (x\u2083 x\u2081)\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (recvD x\u2081))\n = cong (right \u2218 recvD) (funExtD (\u03bb m \u2192 sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (x\u2081 m)))\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (recvS x\u2081))\n = cong (right \u2218 recvS) (funExtS (\u03bb m \u2192 sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (x\u2081 m)))\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (sendD x\u2081 x\u2082))\n = cong (right \u2218 sendD x\u2081) (sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) x\u2082)\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (sendS x\u2081 x\u2082))\n = cong (right \u2218 sendS x\u2081) (sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) x\u2082)\n sim-comp-assoc-end P-P' end (right \u00f8P) (right ()) end\n sim-comp-assoc-end end end (right \u00f8P) end QR = refl\n sim-comp-assoc-end end Q-Q' end PQ QR = refl\n\n \u2666-assoc-end : \u2200 {P Q R}(\u00f8P : Sim end P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (\u00f8P \u2666 PQ) \u2666 QR \u2261 \u00f8P \u2666 (PQ \u2666 QR)\n \u2666-assoc-end = sim-comp-assoc-end (Dual-spec _) (Dual-spec _)\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u223c sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' WP PQ QR {W'} W'-W \u00f8W'\n = sim-comp W'-W \u00f8W' (sim-comp Q-Q' (sim-comp P-P' WP PQ) QR)\n \u2261\u27e8 sym (sim-comp-assoc-end W'-W Q-Q' \u00f8W' (sim-comp P-P' WP PQ) QR) \u27e9\n sim-comp Q-Q' (sim-comp W'-W \u00f8W' (sim-comp P-P' WP PQ)) QR\n \u2261\u27e8 cong (\u03bb X \u2192 sim-comp Q-Q' X QR) (sym (sim-comp-assoc-end W'-W P-P' \u00f8W' WP PQ)) \u27e9\n sim-comp Q-Q' (sim-comp P-P' (sim-comp W'-W \u00f8W' WP) PQ) QR\n \u2261\u27e8 sim-comp-assoc-end P-P' Q-Q' (sim-comp W'-W \u00f8W' WP) PQ QR \u27e9\n sim-comp P-P' (sim-comp W'-W \u00f8W' WP) (sim-comp Q-Q' PQ QR)\n \u2261\u27e8 sim-comp-assoc-end W'-W P-P' \u00f8W' WP (sim-comp Q-Q' PQ QR) \u27e9\n sim-comp W'-W \u00f8W' (sim-comp P-P' WP (sim-comp Q-Q' PQ QR))\n \u220e\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u223c WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n\n\u223c-\u00f8 : \u2200 {P}{s s' : Sim end P} \u2192 s \u223c s' \u2192 s \u2261 s'\n\u223c-\u00f8 s\u223cs' = s\u223cs' end end\n\nmodule _\n (funExtD : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n (funExtS : \u2200 {a}{b}{A : \u2605_ a}{B : ..(_ : A) \u2192 \u2605_ b}{f g : ..(x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (left (recvD x)) = cong (left \u2218 recvD) (funExtD \u03bb m \u2192 sim-!! (x m))\n sim-!! (left (recvS x)) = cong (left \u2218 recvS) (funExtS \u03bb m \u2192 sim-!! (x m))\n sim-!! (left (sendD x x\u2081)) = cong (left \u2218 sendD x) (sim-!! x\u2081)\n sim-!! (left (sendS x x\u2081)) = cong (left \u2218 sendS x) (sim-!! x\u2081)\n sim-!! (right (recvD x)) = cong (right \u2218 recvD) (funExtD \u03bb m \u2192 sim-!! (x m))\n sim-!! (right (recvS x)) = cong (right \u2218 recvS) (funExtS \u03bb m \u2192 sim-!! (x m))\n sim-!! (right (sendD x x\u2081)) = cong (right \u2218 sendD x) (sim-!! x\u2081)\n sim-!! (right (sendS x x\u2081)) = cong (right \u2218 sendS x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end Q-Q' (left ()) QR\n sim-comp-!-end end (right ()) (left x\u2081)\n sim-comp-!-end (\u03a0\u03a3'S x\u2081) (right (recvS x)) (left (sendS x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a0\u03a3'D x\u2081) (right (recvD x)) (left (sendD x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u03a0'S x) (right (sendS x\u2081 x\u2082)) (left (recvS x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end (\u03a3\u03a0'D x) (right (sendD x\u2081 x\u2082)) (left (recvD x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (right x) (right (recvD x\u2081))\n = cong (left \u2218 recvD) (funExtD (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (right x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (right x) (right (recvS x\u2081))\n = cong (left \u2218 recvS) (funExtS (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (right x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (right x) (right (sendD x\u2081 x\u2082))\n = cong (left \u2218 sendD x\u2081) (sim-comp-!-end Q-Q' (right x) x\u2082)\n sim-comp-!-end Q-Q' (right x) (right (sendS x\u2081 x\u2082))\n = cong (left \u2218 sendS x\u2081) (sim-comp-!-end Q-Q' (right x) x\u2082)\n sim-comp-!-end end (right x) end = refl\n sim-comp-!-end end end QR = {!!}\n\n open \u2261-Reasoning\n module _ {P Q}{s s' : Sim P Q} where\n !\u02e2-cong : s \u223c s' \u2192 !\u02e2 s \u223c !\u02e2 s'\n !\u02e2-cong s\u223cs' Q'-Q \u00f8Q'\n = sim-comp Q'-Q \u00f8Q' (!\u02e2 s)\n \u2261\u27e8 {!!} \u27e9\n sim-comp Q'-Q \u00f8Q' (!\u02e2 (sim-comp (Dual-spec Q) s (sim-id _)))\n \u2261\u27e8 {!!} \u27e9\n sim-comp Q'-Q \u00f8Q' (!\u02e2 s')\n \u220e\n\n postulate\n sim-comp-assoc-end' : \u2200 {P Q Q' R R'}(Q-Q' : Dual Q Q')(R-R' : Dual R R')\n (PQ : Sim P Q)(QR : Sim Q' R )(R\u00f8 : Sim R' end)\n \u2192 sim-comp R-R' (sim-comp Q-Q' PQ QR) R\u00f8\n \u2261 sim-comp Q-Q' PQ (sim-comp R-R' QR R\u00f8)\n\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc-end funExtD funExtS R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExtD funExtS Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n -- \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc-end' Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n \u2261\u27e8 \u223c-\u00f8 {!!}\u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n{-\n\nunit-sim : \u2200 {P} \u2192 Process \ud835\udfd9 P \u2192 Sim end P\nunit-sim (do (send m x)) = right (send m (unit-sim x))\nunit-sim (do (recv x)) = right (recv (\u03bb m \u2192 unit-sim (x m)))\nunit-sim (end x) = end\n\n{-\ntoEl : \u2200 {P A} \u2192 Process A P \u2192 El P (const A)\ntoEl (end x) = x\ntoEl (do (recv f)) = \u03bb x \u2192 toEl (f x)\ntoEl (do (send m x)) = m , toEl x\n-}\n\nidP : \u2200 {A} \u2192 Process A (\u03a0' A (const end))\nidP = do (recv end)\n\ndual\u00b2 : \u2200 {P A} \u2192 Process A P \u2192 Process A (dual (dual P))\ndual\u00b2 (end x) = end x\ndual\u00b2 (do (recv x)) = do (recv (\u03bb m \u2192 dual\u00b2 (x m)))\ndual\u00b2 (do (send m x)) = do (send m (dual\u00b2 x))\n\napply-sim : \u2200 {P Q} \u2192 Sim P Q \u2192 P \u27f9\u1d3e Q\napply-sim (left (send m x)) (do (recv x\u2081)) = apply-sim x (x\u2081 m)\napply-sim (left (recv x)) (do (send m x\u2081)) = apply-sim (x m) x\u2081\napply-sim (right (send m x)) P\u2082 = do (send m (apply-sim x P\u2082))\napply-sim (right (recv x)) P\u2082 = do (recv (\u03bb m \u2192 apply-sim (x m) P\u2082))\napply-sim end P = P\n\napply-sim' : \u2200 {P Q} \u2192 Sim P Q \u2192 Q \u27f9\u1d3e P -- \u2200 {A} \u2192 Process A Q \u2192 Process A (dual P)\napply-sim' PQ P = apply-sim (sim-sym PQ) P\n\napply : \u2200 {P Q A} \u2192 P \u27f9 Q \u2192 Process A P \u2192 Process A Q\napply PQ P = apply-sim PQ (dual\u00b2 P)\n\nmodule _ (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n apply-comp : \u2200 {P Q R A}(PQ : Sim P Q)(QR : Sim (dual Q) R)(p : Process A (dual P)) \u2192 apply-sim (sim-comp PQ QR) p \u2261 apply QR (apply-sim PQ p)\n apply-comp (left (send m x)) QR (do (recv x\u2081)) = apply-comp x QR (x\u2081 m)\n apply-comp (left (recv x)) QR (do (send m x\u2081)) = apply-comp (x m) QR x\u2081\n apply-comp (right (send m x)) (left (recv x\u2081)) p = apply-comp x (x\u2081 m) p\n apply-comp (right (send m x)) (right (recv x\u2081)) p = cong (\u03bb X \u2192 do (recv X))\n (funExt (\u03bb m' \u2192 apply-comp (right (send m x)) (x\u2081 m') p))\n apply-comp (right (send m x)) (right (send m\u2081 x\u2081)) p\n rewrite apply-comp (right (send m x)) x\u2081 p = refl\n apply-comp (right (recv x)) (left (send m x\u2081)) p = apply-comp (x m) x\u2081 p\n apply-comp (right (recv x)) (right (send m x\u2081)) p\n rewrite apply-comp (right (recv x)) x\u2081 p = refl\n apply-comp (right (recv x)) (right (recv x\u2081)) p = cong (\u03bb X \u2192 do (recv X))\n (funExt (\u03bb m \u2192 apply-comp (right (recv x)) (x\u2081 m) p))\n apply-comp end QR (do ())\n apply-comp end QR (end x) = refl\n\n{-\n_>>=P_ : \u2200 {A B P}{Q : Tele P \u2192 Proto} \u2192 Process A P \u2192 ((p : Tele P) \u2192 A \u2192 Process B (Q p)) \u2192 Process B (P >>\u2261 Q)\nsend m p >>=P k = send m (p >>=P (\u03bb p\u2081 \u2192 k (m , p\u2081)))\nrecv x >>=P k = recv (\u03bb m \u2192 x m >>=P (\u03bb p \u2192 k (m , p)))\nend x >>=P k = k 0\u2081 x\n\n\n {-\nmodule _ where\n bind-com : (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (dual P) \u2192 Proto)\n (X : Tele (P >>\u2261 Q) \u2192 \u2605)(Y : Tele (dual P >>\u2261 R) \u2192 \u2605)\n \u2192 El (P >>\u2261 Q) X \u2192 El (dual P >>\u2261 R) Y \u2192 ? \u00d7 ?\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level\nopen import Data.Product\nopen import Data.One\n\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Control.Protocol.Choreography where\nopen import Control.Strategy renaming (Strategy to Client) public\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata Mod : \u2605 where S D : Mod\n\n\u2192M : \u2200 {a b} \u2192 Mod \u2192 \u2605_ a \u2192 \u2605_ b \u2192 \u2605_ (a \u2294 b)\n\u2192M S A B = ..(_ : A) \u2192 B\n\u2192M D A B = A \u2192 B\n\n\u03a0M : \u2200 {a b}(m : Mod) \u2192 (A : \u2605_ a) \u2192 (B : \u2192M m A (\u2605_ b)) \u2192 \u2605_ (a \u2294 b)\n\u03a0M S A B = \u03a0\u00b7 A B\n\u03a0M D A B = \u03a0 A B\n\n-- appM : \u2200 {a b}(m : Mod){A : \u2605_ a}{B : \u2192M m A (\u2605_ b)}(P : \u03a0M m A B)(x : A) \u2192 B\n\ndata Proto : \u2605\u2081 where\n end : Proto\n \u03a0' \u03a3' : (f : Mod)(A : \u2605)(B : \u2192M f A Proto) \u2192 Proto\n\n{-\nTele : Proto \u2192 \u2605\nTele end = \ud835\udfd9\nTele (\u03a0' A B) = \u03a3 A \u03bb x \u2192 Tele (B x)\nTele (\u03a3' A B) = \u03a3 A \u03bb x \u2192 Tele (B x)\nTele (later i P) = ?\n\n_>>\u2261_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>\u2261 Q = Q _\n\u03a0' A B >>\u2261 Q = \u03a0' A (\u03bb x \u2192 B x >>\u2261 (\u03bb xs \u2192 Q (x , xs)))\n\u03a3' A B >>\u2261 Q = \u03a3' A (\u03bb x \u2192 B x >>\u2261 (\u03bb xs \u2192 Q (x , xs)))\nlater i P >>\u2261 Q = ?\n\n++Tele : \u2200 (P : Proto)(Q : Tele P \u2192 Proto) \u2192 (x : Tele P) \u2192 Tele (Q x) \u2192 Tele (P >>\u2261 Q)\n++Tele end Q x y = y\n++Tele (\u03a0' M C) Q (m , x) y = m , ++Tele (C m) (\u03bb x\u2081 \u2192 Q (m , x\u2081)) x y\n++Tele (\u03a3' M C) Q (m , x) y = m , ++Tele (C m) _ x y\n++Tele (later i P) Q x y = ?\n\nmodule _ (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : \u2605_ b}{f g : A \u2192 B} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n right-unit : \u2200 (P : Proto) \u2192 (P >>\u2261 \u03bb x \u2192 end) \u2261 P\n right-unit end = refl\n right-unit (\u03a0' M C) = let p = funExt (\u03bb x \u2192 right-unit (C x)) in cong (\u03a0' M) p\n right-unit (\u03a3' M C) = cong (\u03a3' M) (funExt (\u03bb x \u2192 right-unit (C x)))\n right-unit (later i P) = ?\n\n assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>\u2261 Q) \u2192 Proto)\n \u2192 P >>\u2261 (\u03bb x \u2192 Q x >>\u2261 (\u03bb y \u2192 R (++Tele P Q x y))) \u2261 ((P >>\u2261 Q) >>\u2261 R)\n assoc end Q R = refl\n assoc (\u03a0' M C\u2081) Q R = cong (\u03a0' M) (funExt (\u03bb x \u2192 assoc (C\u2081 x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb xs \u2192 R (x , xs))))\n assoc (\u03a3' M C\u2081) Q R = cong (\u03a3' M) (funExt (\u03bb x \u2192 assoc (C\u2081 x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb xs \u2192 R (x , xs))))\n assoc (later i P) Q R = ?\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>\u2261 \u03bb _ \u2192 Q\n-}\n\n_\u00d7'_ : Set \u2192 Proto \u2192 Proto\nA \u00d7' B = \u03a3' D A \u03bb _ \u2192 B\n\n_\u2192'_ : Set \u2192 Proto \u2192 Proto\nA \u2192' B = \u03a0' D A \u03bb _ \u2192 B\n\ndual : Proto \u2192 Proto\ndual end = end\ndual (\u03a0' S A B) = \u03a3' S A (\u03bb x \u2192 dual (B x))\ndual (\u03a0' D A B) = \u03a3' D A (\u03bb x \u2192 dual (B x))\ndual (\u03a3' S A B) = \u03a0' S A (\u03bb x \u2192 dual (B x))\ndual (\u03a3' D A B) = \u03a0' D A (\u03bb x \u2192 dual (B x))\n\ndata Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u03a3'S : \u2200 {A B B'} \u2192 (\u2200 ..x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a0' S A B) (\u03a3' S A B')\n \u03a0\u03a3'D : \u2200 {A B B'} \u2192 (\u2200 x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a0' D A B) (\u03a3' D A B')\n \u03a3\u03a0'S : \u2200 {A B B'} \u2192 (\u2200 ..x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a3' S A B) (\u03a0' S A B')\n \u03a3\u03a0'D : \u2200 {A B B'} \u2192 (\u2200 x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a3' D A B) (\u03a0' D A B')\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u03a3'S f) = \u03a3\u03a0'S (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a0\u03a3'D f) = \u03a3\u03a0'D (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u03a0'S f) = \u03a0\u03a3'S (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u03a0'D f) = \u03a0\u03a3'D (\u03bb x \u2192 Dual-sym (f x))\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0' S A B) = \u03a0\u03a3'S (\u03bb x \u2192 Dual-spec (B x))\nDual-spec (\u03a0' D A B) = \u03a0\u03a3'D (\u03bb x \u2192 Dual-spec (B x))\nDual-spec (\u03a3' S A B) = \u03a3\u03a0'S (\u03bb x \u2192 Dual-spec (B x))\nDual-spec (\u03a3' D A B) = \u03a3\u03a0'D (\u03bb x \u2192 Dual-spec (B x))\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele P \u2261 Tele (dual P)\n dual-Tele end = refl\n dual-Tele (\u03a0' A B) = cong (\u03a3 A) (funExt (\u03bb x \u2192 dual-Tele (B x)))\n dual-Tele (\u03a3' A B) = cong (\u03a3 A) (funExt (\u03bb x \u2192 dual-Tele (B x)))\n dual-Tele (later i P) = ?\n-}{-\nmodule _ X where\n El : Proto \u2192 \u2605\n El end = X\n El (\u03a0' A B) = \u03a0 A \u03bb x \u2192 El (B x)\n El (\u03a3' A B) = \u03a3 A \u03bb x \u2192 El (B x)\nmodule _ where\n El : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\n El end X = X _\n El (\u03a0' A B) X = \u03a0 A \u03bb x \u2192 El (B x) (\u03bb y \u2192 X (x , y))\n El (\u03a3' A B) X = \u03a3 A \u03bb x \u2192 El (B x) (\u03bb y \u2192 X (x , y))\n El (later i P) = ?\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n bind-spec : (P : Proto)(Q : Tele P \u2192 Proto)(X : Tele (P >>\u2261 Q) \u2192 \u2605) \u2192 El (P >>\u2261 Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P Q x y)))\n bind-spec end Q X = refl\n bind-spec (\u03a0' A B) Q X = cong (\u03a0 A) (funExt (\u03bb x \u2192 bind-spec (B x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb y \u2192 X (x , y))))\n bind-spec (\u03a3' A B) Q X = cong (\u03a3 A) (funExt (\u03bb x \u2192 bind-spec (B x) _ _))\n bind-spec (later i p) Q X = ?\n\n\nmodule _ {A B} where\n com : (P : Proto) \u2192 El P (const A) \u2192 El (dual P) (const B) \u2192 A \u00d7 B\n com end a b = a , b\n com (\u03a0' A B) f (x , y) = com (B x) (f x) y\n com (\u03a3' A B) (x , y) f = com (B x) y (f x)\n com (later i P) x y = ?\n\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n com-cont : (P : Proto){A : Tele P \u2192 \u2605}{B : Tele (dual P) \u2192 \u2605} \u2192 El P A \u2192 El (dual P) B \u2192 \u03a3 (Tele P) A \u00d7 \u03a3 (Tele (dual P)) B\n com-cont end p q = (_ , p) , (_ , q)\n com-cont (\u03a0' A B) p (m , q) with com-cont (B m) (p m) q\n ... | (X , a) , (Y , b) = ((m , X) , a) , (m , Y) , b\n com-cont (\u03a3' A B) (m , p) q with com-cont (B m) p (q m)\n ... | (X , a) , (Y , b) = ((m , X) , a) , (m , Y) , b\n com-cont (later i P) p q = ?\n-}\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Proto \u2192 \u2605\u2081 where\n recvD : \u2200 {M P} \u2192 (\u03a0M D M \u03bb m \u2192 this (P m)) \u2192 ProcessF this (\u03a0' D M P)\n recvS : \u2200 {M P} \u2192 (\u03a0M S M \u03bb m \u2192 this (P m)) \u2192 ProcessF this (\u03a0' S M P)\n sendD : \u2200 {M P} \u2192 \u03a0M D M (\u03bb m \u2192 this (P m) \u2192 ProcessF this (\u03a3' D M P))\n sendS : \u2200 {M P} \u2192 \u03a0M S M (\u03bb m \u2192 this (P m) \u2192 ProcessF this (\u03a3' S M P))\n\ndata Process (A : \u2605) : Proto \u2192 \u2605\u2081 where\n do : \u2200 {P} \u2192 ProcessF (Process A) P \u2192 Process A P\n end : A \u2192 Process A end\n\nmutual\n SimL : Proto \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Proto \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n left : \u2200 {P Q} \u2192 SimL P Q \u2192 Sim P Q\n right : \u2200 {P Q} \u2192 SimR P Q \u2192 Sim P Q\n end : Sim end end\n\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = \u2200 {A} \u2192 Process A (dual P) \u2192 Process A Q\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0' S A B) = right (recvS (\u03bb x \u2192 left (sendS x (sim-id (B x)))))\nsim-id (\u03a0' D A B) = right (recvD (\u03bb x \u2192 left (sendD x (sim-id (B x)))))\nsim-id (\u03a3' S A B) = left (recvS (\u03bb x \u2192 right (sendS x (sim-id (B x)))))\nsim-id (\u03a3' D A B) = left (recvD (\u03bb x \u2192 right (sendD x (sim-id (B x)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (left x) QR = left (sim-compL Q-Q' x QR)\nsim-comp Q-Q' (right x) (left x\u2081) = sim-compRL Q-Q' x x\u2081\nsim-comp Q-Q' (right x) (right x\u2081) = right (sim-compR Q-Q' (right x) x\u2081)\nsim-comp end (right x) end = right x\nsim-comp end end QR = QR\n\nsim-compRL end () QR\nsim-compRL (\u03a0\u03a3'S x\u2081) (recvS x) (sendS x\u2082 x\u2083) = sim-comp (x\u2081 x\u2082) (x x\u2082) x\u2083\nsim-compRL (\u03a0\u03a3'D x\u2081) (recvD x) (sendD x\u2082 x\u2083) = sim-comp (x\u2081 x\u2082) (x x\u2082) x\u2083\nsim-compRL (\u03a3\u03a0'S x) (sendS x\u2081 x\u2082) (recvS x\u2083) = sim-comp (x x\u2081) x\u2082 (x\u2083 x\u2081)\nsim-compRL (\u03a3\u03a0'D x) (sendD x\u2081 x\u2082) (recvD x\u2083) = sim-comp (x x\u2081) x\u2082 (x\u2083 x\u2081)\n\nsim-compL Q-Q' (recvD PQ) QR = recvD (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR)\nsim-compL Q-Q' (recvS PQ) QR = recvS (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR)\nsim-compL Q-Q' (sendD m PQ) QR = sendD m (sim-comp Q-Q' PQ QR)\nsim-compL Q-Q' (sendS m PQ) QR = sendS m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recvD QR) = recvD (\u03bb m \u2192 sim-comp Q-Q' PQ (QR m))\nsim-compR Q-Q' PQ (recvS QR) = recvS (\u03bb x \u2192 sim-comp Q-Q' PQ (QR x))\nsim-compR Q-Q' PQ (sendD m QR) = sendD m (sim-comp Q-Q' PQ QR)\nsim-compR Q-Q' PQ (sendS m QR) = sendS m (sim-comp Q-Q' PQ QR)\n\n{-\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-comp Q-Q' (left (recvD PQ)) QR = left (recvD (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR))\nsim-comp Q-Q' (left (recvS PQ)) QR = left (recvS (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR))\nsim-comp Q-Q' (left (sendD m PQ)) QR = left (sendD m (sim-comp Q-Q' PQ QR))\nsim-comp Q-Q' (left (sendS m PQ)) QR = left (sendS m (sim-comp Q-Q' PQ QR))\nsim-comp end (right ()) (left x\u2081)\nsim-comp end end QR = QR\nsim-comp end PQ end = PQ\nsim-comp (\u03a0\u03a3'S Q-Q') (right (recvS PQ)) (left (sendS m QR)) = sim-comp (Q-Q' m) (PQ m) QR\nsim-comp (\u03a0\u03a3'D Q-Q') (right (recvD PQ)) (left (sendD m QR)) = sim-comp (Q-Q' m) (PQ m) QR\nsim-comp (\u03a3\u03a0'S Q-Q') (right (sendS m PQ)) (left (recvS QR)) = sim-comp (Q-Q' m) PQ (QR m)\nsim-comp (\u03a3\u03a0'D Q-Q') (right (sendD m PQ)) (left (recvD QR)) = sim-comp (Q-Q' m) PQ (QR m)\nsim-comp Q-Q' PQ (right (recvD QR)) = right (recvD (\u03bb m \u2192 sim-comp Q-Q' PQ (QR m)))\nsim-comp Q-Q' PQ (right (recvS QR)) = right (recvS (\u03bb x \u2192 sim-comp Q-Q' PQ (QR x)))\nsim-comp Q-Q' PQ (right (sendD m QR)) = right (sendD m (sim-comp Q-Q' PQ QR))\nsim-comp Q-Q' PQ (right (sendS m QR)) = right (sendS m (sim-comp Q-Q' PQ QR))\n-}\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\nsim-sym : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\nsim-sym (left x) = right (sim-symL x)\nsim-sym (right x) = left (sim-symR x)\nsim-sym end = end\n\nsim-symL (recvD PQ) = recvD (\u03bb m \u2192 sim-sym (PQ m))\nsim-symL (recvS PQ) = recvS (\u03bb m \u2192 sim-sym (PQ m))\nsim-symL (sendD m PQ) = sendD m (sim-sym PQ)\nsim-symL (sendS m PQ) = sendS m (sim-sym PQ)\n\nsim-symR (recvD PQ) = recvD (\u03bb m \u2192 sim-sym (PQ m))\nsim-symR (recvS PQ) = recvS (\u03bb m \u2192 sim-sym (PQ m))\nsim-symR (sendD m PQ) = sendD m (sim-sym PQ)\nsim-symR (sendS m PQ) = sendS m (sim-sym PQ)\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process \ud835\udfd9 P\nsim-unit (left ())\nsim-unit (right (recvD P)) = do (recvD (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (right (recvS P)) = do (recvS (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (right (sendD m P)) = do (sendD m (sim-unit P))\nsim-unit (right (sendS m P)) = do (sendS m (sim-unit P))\nsim-unit end = end 0\u2081\n\nmodule _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\nmodule _\n (funExtD : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n (funExtS : \u2200 {a}{b}{A : \u2605_ a}{B : ..(_ : A) \u2192 \u2605_ b}{f g : ..(x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n sim-comp-assoc-end : \u2200 {P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (\u00f8P : Sim end P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' \u00f8P PQ) QR\n \u2261 sim-comp P-P' \u00f8P (sim-comp Q-Q' PQ QR)\n sim-comp-assoc-end P-P' Q-Q' (left ()) PQ QR\n sim-comp-assoc-end end Q-Q' (right ()) (left PQ) QR\n sim-comp-assoc-end (\u03a0\u03a3'S x\u2081) Q-Q' (right (recvS x)) (left (sendS x\u2082 x\u2083)) QR\n = sim-comp-assoc-end (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 QR\n sim-comp-assoc-end (\u03a0\u03a3'D x\u2081) Q-Q' (right (recvD x)) (left (sendD x\u2082 x\u2083)) QR\n = sim-comp-assoc-end (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 QR\n sim-comp-assoc-end (\u03a3\u03a0'S x) Q-Q' (right (sendS x\u2081 x\u2082)) (left (recvS x\u2083)) QR\n = sim-comp-assoc-end (x x\u2081) Q-Q' x\u2082 (x\u2083 x\u2081) QR\n sim-comp-assoc-end (\u03a3\u03a0'D x) Q-Q' (right (sendD x\u2081 x\u2082)) (left (recvD x\u2083)) QR\n = sim-comp-assoc-end (x x\u2081) Q-Q' x\u2082 (x\u2083 x\u2081) QR\n sim-comp-assoc-end P-P' end (right \u00f8P) (right ()) (left x\u2081)\n sim-comp-assoc-end P-P' (\u03a0\u03a3'S x\u2081) (right \u00f8P) (right (recvS x)) (left (sendS x\u2082 x\u2083))\n = sim-comp-assoc-end P-P' (x\u2081 x\u2082) (right \u00f8P) (x x\u2082) x\u2083\n sim-comp-assoc-end P-P' (\u03a0\u03a3'D x\u2081) (right \u00f8P) (right (recvD x)) (left (sendD x\u2082 x\u2083))\n = sim-comp-assoc-end P-P' (x\u2081 x\u2082) (right \u00f8P) (x x\u2082) x\u2083\n sim-comp-assoc-end P-P' (\u03a3\u03a0'S x) (right \u00f8P) (right (sendS x\u2081 x\u2082)) (left (recvS x\u2083))\n = sim-comp-assoc-end P-P' (x x\u2081) (right \u00f8P) x\u2082 (x\u2083 x\u2081)\n sim-comp-assoc-end P-P' (\u03a3\u03a0'D x) (right \u00f8P) (right (sendD x\u2081 x\u2082)) (left (recvD x\u2083))\n = sim-comp-assoc-end P-P' (x x\u2081) (right \u00f8P) x\u2082 (x\u2083 x\u2081)\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (recvD x\u2081))\n = cong (right \u2218 recvD) (funExtD (\u03bb m \u2192 sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (x\u2081 m)))\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (recvS x\u2081))\n = cong (right \u2218 recvS) (funExtS (\u03bb m \u2192 sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (x\u2081 m)))\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (sendD x\u2081 x\u2082))\n = cong (right \u2218 sendD x\u2081) (sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) x\u2082)\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (sendS x\u2081 x\u2082))\n = cong (right \u2218 sendS x\u2081) (sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) x\u2082)\n sim-comp-assoc-end P-P' end (right \u00f8P) (right ()) end\n sim-comp-assoc-end end end (right \u00f8P) end QR = refl\n sim-comp-assoc-end end Q-Q' end PQ QR = refl\n\n \u2666-assoc-end : \u2200 {P Q R}(\u00f8P : Sim end P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (\u00f8P \u2666 PQ) \u2666 QR \u2261 \u00f8P \u2666 (PQ \u2666 QR)\n \u2666-assoc-end = sim-comp-assoc-end (Dual-spec _) (Dual-spec _)\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u223c sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' WP PQ QR {W'} W'-W \u00f8W'\n = sim-comp W'-W \u00f8W' (sim-comp Q-Q' (sim-comp P-P' WP PQ) QR)\n \u2261\u27e8 sym (sim-comp-assoc-end W'-W Q-Q' \u00f8W' (sim-comp P-P' WP PQ) QR) \u27e9\n sim-comp Q-Q' (sim-comp W'-W \u00f8W' (sim-comp P-P' WP PQ)) QR\n \u2261\u27e8 cong (\u03bb X \u2192 sim-comp Q-Q' X QR) (sym (sim-comp-assoc-end W'-W P-P' \u00f8W' WP PQ)) \u27e9\n sim-comp Q-Q' (sim-comp P-P' (sim-comp W'-W \u00f8W' WP) PQ) QR\n \u2261\u27e8 sim-comp-assoc-end P-P' Q-Q' (sim-comp W'-W \u00f8W' WP) PQ QR \u27e9\n sim-comp P-P' (sim-comp W'-W \u00f8W' WP) (sim-comp Q-Q' PQ QR)\n \u2261\u27e8 sim-comp-assoc-end W'-W P-P' \u00f8W' WP (sim-comp Q-Q' PQ QR) \u27e9\n sim-comp W'-W \u00f8W' (sim-comp P-P' WP (sim-comp Q-Q' PQ QR))\n \u220e\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u223c WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n\nsim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (sim-sym Q'R) (sim-sym PQ) \u2261 sim-sym (sim-comp Q-Q' PQ Q'R)\nsim-comp-! Q-Q' (left (recvD x)) (left x\u2081) = {!!}\nsim-comp-! Q-Q' (left (recvS x)) (left x\u2081) = {!!}\nsim-comp-! Q-Q' (left (sendD x x\u2081)) (left x\u2082) = cong (right \u2218 sendD x) (sim-comp-! Q-Q' x\u2081 (left x\u2082))\nsim-comp-! Q-Q' (left (sendS x x\u2081)) (left x\u2082) = cong (right \u2218 sendS x) (sim-comp-! Q-Q' x\u2081 (left x\u2082))\nsim-comp-! Q-Q' (left x) (right x\u2081) = {!!}\nsim-comp-! Q-Q' (left x) end = {!!}\nsim-comp-! Q-Q' (right x) QR = {!!}\nsim-comp-! end end (left x) = refl\nsim-comp-! end end (right x) = {!!}\nsim-comp-! end end end = refl\n{-\n\nunit-sim : \u2200 {P} \u2192 Process \ud835\udfd9 P \u2192 Sim end P\nunit-sim (do (send m x)) = right (send m (unit-sim x))\nunit-sim (do (recv x)) = right (recv (\u03bb m \u2192 unit-sim (x m)))\nunit-sim (end x) = end\n\n{-\ntoEl : \u2200 {P A} \u2192 Process A P \u2192 El P (const A)\ntoEl (end x) = x\ntoEl (do (recv f)) = \u03bb x \u2192 toEl (f x)\ntoEl (do (send m x)) = m , toEl x\n-}\n\nidP : \u2200 {A} \u2192 Process A (\u03a0' A (const end))\nidP = do (recv end)\n\ndual\u00b2 : \u2200 {P A} \u2192 Process A P \u2192 Process A (dual (dual P))\ndual\u00b2 (end x) = end x\ndual\u00b2 (do (recv x)) = do (recv (\u03bb m \u2192 dual\u00b2 (x m)))\ndual\u00b2 (do (send m x)) = do (send m (dual\u00b2 x))\n\napply-sim : \u2200 {P Q} \u2192 Sim P Q \u2192 P \u27f9\u1d3e Q\napply-sim (left (send m x)) (do (recv x\u2081)) = apply-sim x (x\u2081 m)\napply-sim (left (recv x)) (do (send m x\u2081)) = apply-sim (x m) x\u2081\napply-sim (right (send m x)) P\u2082 = do (send m (apply-sim x P\u2082))\napply-sim (right (recv x)) P\u2082 = do (recv (\u03bb m \u2192 apply-sim (x m) P\u2082))\napply-sim end P = P\n\napply-sim' : \u2200 {P Q} \u2192 Sim P Q \u2192 Q \u27f9\u1d3e P -- \u2200 {A} \u2192 Process A Q \u2192 Process A (dual P)\napply-sim' PQ P = apply-sim (sim-sym PQ) P\n\napply : \u2200 {P Q A} \u2192 P \u27f9 Q \u2192 Process A P \u2192 Process A Q\napply PQ P = apply-sim PQ (dual\u00b2 P)\n\nmodule _ (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n apply-comp : \u2200 {P Q R A}(PQ : Sim P Q)(QR : Sim (dual Q) R)(p : Process A (dual P)) \u2192 apply-sim (sim-comp PQ QR) p \u2261 apply QR (apply-sim PQ p)\n apply-comp (left (send m x)) QR (do (recv x\u2081)) = apply-comp x QR (x\u2081 m)\n apply-comp (left (recv x)) QR (do (send m x\u2081)) = apply-comp (x m) QR x\u2081\n apply-comp (right (send m x)) (left (recv x\u2081)) p = apply-comp x (x\u2081 m) p\n apply-comp (right (send m x)) (right (recv x\u2081)) p = cong (\u03bb X \u2192 do (recv X))\n (funExt (\u03bb m' \u2192 apply-comp (right (send m x)) (x\u2081 m') p))\n apply-comp (right (send m x)) (right (send m\u2081 x\u2081)) p\n rewrite apply-comp (right (send m x)) x\u2081 p = refl\n apply-comp (right (recv x)) (left (send m x\u2081)) p = apply-comp (x m) x\u2081 p\n apply-comp (right (recv x)) (right (send m x\u2081)) p\n rewrite apply-comp (right (recv x)) x\u2081 p = refl\n apply-comp (right (recv x)) (right (recv x\u2081)) p = cong (\u03bb X \u2192 do (recv X))\n (funExt (\u03bb m \u2192 apply-comp (right (recv x)) (x\u2081 m) p))\n apply-comp end QR (do ())\n apply-comp end QR (end x) = refl\n\n{-\n_>>=P_ : \u2200 {A B P}{Q : Tele P \u2192 Proto} \u2192 Process A P \u2192 ((p : Tele P) \u2192 A \u2192 Process B (Q p)) \u2192 Process B (P >>\u2261 Q)\nsend m p >>=P k = send m (p >>=P (\u03bb p\u2081 \u2192 k (m , p\u2081)))\nrecv x >>=P k = recv (\u03bb m \u2192 x m >>=P (\u03bb p \u2192 k (m , p)))\nend x >>=P k = k 0\u2081 x\n\n\n {-\nmodule _ where\n bind-com : (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (dual P) \u2192 Proto)\n (X : Tele (P >>\u2261 Q) \u2192 \u2605)(Y : Tele (dual P >>\u2261 R) \u2192 \u2605)\n \u2192 El (P >>\u2261 Q) X \u2192 El (dual P >>\u2261 R) Y \u2192 ? \u00d7 ?\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"4dd4448f43898fbd80877ba687ffc2fbc1558a5d","subject":"Readd instance","message":"Readd instance\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Validity.agda","new_file":"Parametric\/Change\/Validity.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e)\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Denotation.Value as Value\nopen import Base.Change.Algebra as CA\n using (ChangeAlgebraFamily)\n\nmodule Parametric.Change.Validity\n {Base : Type.Structure}\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Extension Point: Change algebras for base types\nStructure : Set\u2081\nStructure = ChangeAlgebraFamily zero \u27e6_\u27e7Base\n\nmodule Structure (change-algebra-base : Structure) where\n -- change algebras\n\n open CA public renaming\n ( [] to \u2205\n ; _\u2237_ to _\u2022_\n )\n\n -- We provide: change algebra for every type\n instance\n change-algebra : \u2200 \u03c4 \u2192 ChangeAlgebra zero \u27e6 \u03c4 \u27e7Type\n change-algebra (base \u03b9) = change-algebra\u208d_\u208e {{change-algebra-base}} \u03b9\n change-algebra (\u03c4\u2081 \u21d2 \u03c4\u2082) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}}\n\n change-algebra-family : ChangeAlgebraFamily zero \u27e6_\u27e7Type\n change-algebra-family = family change-algebra\n\n -- function changes\n\n open FunctionChanges public using (cons)\n module _ {\u03c4\u2081 \u03c4\u2082 : Type} where\n open FunctionChanges.FunctionChange _ _ {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}} public\n renaming\n ( correct to is-valid\n ; apply to call-change\n )\n\n -- We also provide: change environments (aka. environment changes).\n\n open ListChanges \u27e6_\u27e7Type {{change-algebra-family}} public using () renaming\n ( changeAlgebra to environment-changes\n )\n\n after-env : \u2200 {\u0393 : Context} \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} (d\u03c1 : \u0394\u208d_\u208e {{environment-changes}} \u0393 \u03c1) \u2192 \u27e6 \u0393 \u27e7\n after-env {\u0393} = after\u208d_\u208e \u0393\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e)\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Denotation.Value as Value\nopen import Base.Change.Algebra as CA\n using (ChangeAlgebraFamily)\n\nmodule Parametric.Change.Validity\n {Base : Type.Structure}\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Extension Point: Change algebras for base types\nStructure : Set\u2081\nStructure = ChangeAlgebraFamily zero \u27e6_\u27e7Base\n\nmodule Structure (change-algebra-base : Structure) where\n -- change algebras\n\n open CA public renaming\n ( [] to \u2205\n ; _\u2237_ to _\u2022_\n )\n\n -- We provide: change algebra for every type\n instance\n change-algebra : \u2200 \u03c4 \u2192 ChangeAlgebra zero \u27e6 \u03c4 \u27e7Type\n change-algebra (base \u03b9) = change-algebra\u208d_\u208e {{change-algebra-base}} \u03b9\n change-algebra (\u03c4\u2081 \u21d2 \u03c4\u2082) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}}\n\n change-algebra-family : ChangeAlgebraFamily zero \u27e6_\u27e7Type\n change-algebra-family = family change-algebra\n\n -- function changes\n\n open FunctionChanges public using (cons)\n module _ {\u03c4\u2081 \u03c4\u2082 : Type} where\n open FunctionChanges.FunctionChange _ _ {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}} public\n renaming\n ( correct to is-valid\n ; apply to call-change\n )\n\n -- We also provide: change environments (aka. environment changes).\n\n open ListChanges \u27e6_\u27e7Type {{change-algebra-family}} public using () renaming\n ( changeAlgebra to environment-changes\n )\n\n after-env : \u2200 {\u0393 : Context} \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} (d\u03c1 : \u0394\u208d_\u208e {{environment-changes}} \u0393 \u03c1) \u2192 \u27e6 \u0393 \u27e7\n after-env {\u0393} = after\u208d_\u208e \u0393\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9f49923507a898888e19681f5d20c3540f5cf834","subject":"Added foldr.","message":"Added foldr.\n\nIgnore-this: ea015214744665e73d11c17cfab71321\n\ndarcs-hash:20100702160458-3bd4e-fbf3e5957dd6548bc9fe93f1963cdf972226d6c0.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Data\/List.agda","new_file":"LTC\/Data\/List.agda","new_contents":"------------------------------------------------------------------------------\n-- Lists\n------------------------------------------------------------------------------\n\nmodule LTC.Data.List where\n\nopen import LTC.Minimal\n\n------------------------------------------------------------------------------\n\ninfixr 5 _\u2237_ _++_\n\n-- List terms\npostulate\n [] : D\n _\u2237_ : D \u2192 D \u2192 D\n\n-- Basic functions\n\npostulate\n length : D \u2192 D\n length-[] : length [] \u2261 zero\n length-\u2237 : ( d ds : D) \u2192 length (d \u2237 ds) \u2261 succ (length ds)\n{-# ATP axiom length-[] #-}\n{-# ATP axiom length-\u2237 #-}\n\npostulate\n _++_ : D \u2192 D \u2192 D\n ++-[] : (ds : D) \u2192 [] ++ ds \u2261 ds\n ++-\u2237 : (d ds es : D) \u2192 (d \u2237 ds) ++ es \u2261 d \u2237 (ds ++ es)\n{-# ATP axiom ++-[] #-}\n{-# ATP axiom ++-\u2237 #-}\n\n-- List transformations\n\npostulate\n map : D \u2192 D \u2192 D\n map-[] : (f : D ) \u2192 map f [] \u2261 []\n map-\u2237 : (f d ds : D) \u2192 map f (d \u2237 ds) \u2261 f \u2219 d \u2237 map f ds\n{-# ATP axiom map-[] #-}\n{-# ATP axiom map-\u2237 #-}\n\npostulate\n reverse : D \u2192 D\n reverse-[] : reverse [] \u2261 []\n reverse-\u2237 : (d ds : D) \u2192 reverse (d \u2237 ds) \u2261 reverse ds ++ d \u2237 []\n{-# ATP axiom reverse-[] #-}\n{-# ATP axiom reverse-\u2237 #-}\n\npostulate\n replicate : D \u2192 D \u2192 D\n replicate-0 : (d : D) \u2192 replicate zero d \u2261 []\n replicate-S : (d e : D) \u2192 replicate (succ e) d \u2261 d \u2237 replicate e d\n{-# ATP axiom replicate-0 #-}\n{-# ATP axiom replicate-S #-}\n\n-- Reducing lists\n\npostulate\n foldr : D \u2192 D \u2192 D \u2192 D\n foldr-[] : (f n : D) \u2192 foldr f n [] \u2261 n\n foldr-\u2237 : (f n d ds : D) \u2192 foldr f n (d \u2237 ds) \u2261 f \u2219 d \u2219 (foldr f n ds)\n{-# ATP axiom foldr-[] #-}\n{-# ATP axiom foldr-\u2237 #-}\n\n------------------------------------------------------------------------------\n\n-- The LTC list data type\ndata List : D \u2192 Set where\n nil : List []\n cons : (d : D){ds : D} \u2192 (dsL : List ds) \u2192 List (d \u2237 ds)\n\n-- Induction principle for List\nindList : (P : D \u2192 Set) \u2192\n P [] \u2192\n ((d : D){ds : D} \u2192 List ds \u2192 P ds \u2192 P (d \u2237 ds)) \u2192\n {ds : D} \u2192 List ds \u2192 P ds\nindList P p[] iStep nil = p[]\nindList P p[] iStep (cons d {ds} dsL) = iStep d dsL (indList P p[] iStep dsL)\n","old_contents":"------------------------------------------------------------------------------\n-- Lists\n------------------------------------------------------------------------------\n\nmodule LTC.Data.List where\n\nopen import LTC.Minimal\n\n------------------------------------------------------------------------------\n\ninfixr 5 _\u2237_ _++_\n\n-- List terms\npostulate\n [] : D\n _\u2237_ : D \u2192 D \u2192 D\n\n-- Basic functions\n\npostulate\n length : D \u2192 D\n length-[] : length [] \u2261 zero\n length-\u2237 : ( d ds : D) \u2192 length (d \u2237 ds) \u2261 succ (length ds)\n{-# ATP axiom length-[] #-}\n{-# ATP axiom length-\u2237 #-}\n\npostulate\n _++_ : D \u2192 D \u2192 D\n ++-[] : (ds : D) \u2192 [] ++ ds \u2261 ds\n ++-\u2237 : (d ds es : D) \u2192 (d \u2237 ds) ++ es \u2261 d \u2237 (ds ++ es)\n{-# ATP axiom ++-[] #-}\n{-# ATP axiom ++-\u2237 #-}\n\n-- List transformations\n\npostulate\n map : D \u2192 D \u2192 D\n map-[] : (f : D ) \u2192 map f [] \u2261 []\n map-\u2237 : (f d ds : D) \u2192 map f (d \u2237 ds) \u2261 f \u2219 d \u2237 map f ds\n{-# ATP axiom map-[] #-}\n{-# ATP axiom map-\u2237 #-}\n\npostulate\n reverse : D \u2192 D\n reverse-[] : reverse [] \u2261 []\n reverse-\u2237 : (d ds : D) \u2192 reverse (d \u2237 ds) \u2261 reverse ds ++ d \u2237 []\n{-# ATP axiom reverse-[] #-}\n{-# ATP axiom reverse-\u2237 #-}\n\npostulate\n replicate : D \u2192 D \u2192 D\n replicate-0 : (d : D) \u2192 replicate zero d \u2261 []\n replicate-S : (d e : D) \u2192 replicate (succ e) d \u2261 d \u2237 replicate e d\n{-# ATP axiom replicate-0 #-}\n{-# ATP axiom replicate-S #-}\n\n------------------------------------------------------------------------------\n\n-- The LTC list data type\ndata List : D \u2192 Set where\n nil : List []\n cons : (d : D){ds : D} \u2192 (dsL : List ds) \u2192 List (d \u2237 ds)\n\n-- Induction principle for List\nindList : (P : D \u2192 Set) \u2192\n P [] \u2192\n ((d : D){ds : D} \u2192 List ds \u2192 P ds \u2192 P (d \u2237 ds)) \u2192\n {ds : D} \u2192 List ds \u2192 P ds\nindList P p[] iStep nil = p[]\nindList P p[] iStep (cons d {ds} dsL) = iStep d dsL (indList P p[] iStep dsL)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"48205df178d2e57970bb42813b33f76e3c63c5ed","subject":"Minor changes.","message":"Minor changes.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/Functors.agda","new_file":"notes\/fixed-points\/Functors.agda","new_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Based on (Vene, 2000).\n\nmodule Functors where\n\ninfixr 1 _+_\ninfixr 2 _\u00d7_\n\ndata Bool : Set where\n false true : Bool\n\ndata _+_ (A B : Set) : Set where\n inl : A \u2192 A + B\n inr : B \u2192 A + B\n\ndata _\u00d7_ (A B : Set) : Set where\n _,_ : A \u2192 B \u2192 A \u00d7 B\n\n-- The terminal object.\ndata \u22a4 : Set where\n <> : \u22a4\n\npostulate\n -- The least fixed-point.\n\n -- Haskell definitions:\n\n -- data Mu f = In (f (Mu f))\n\n -- unIn :: Mu f \u2192 f (Mu f)\n -- unIn (In x) = x\n\n \u03bc : (Set \u2192 Set) \u2192 Set\n In : {F : Set \u2192 Set} \u2192 F (\u03bc F) \u2192 \u03bc F\n unIn : {F : Set \u2192 Set} \u2192 \u03bc F \u2192 F (\u03bc F)\n\npostulate\n -- The greatest fixed-point.\n\n -- Haskell definitions:\n\n -- data Nu f = Wrap (f (Nu f))\n\n -- out :: Nu f \u2192 (f (Nu f))\n -- out (Wrap x) = x\n\n \u03bd : (Set \u2192 Set) \u2192 Set\n Wrap : {F : Set \u2192 Set} \u2192 F (\u03bd F) \u2192 \u03bd F\n out : {F : Set \u2192 Set} \u2192 \u03bd F \u2192 F (\u03bd F)\n\n------------------------------------------------------------------------------\n-- Functors\n\n-- The identity functor (the functor for the empty and unit types).\nIdF : Set \u2192 Set\nIdF X = X\n\n-- The (co)natural numbers functor.\nNatF : Set \u2192 Set\nNatF X = \u22a4 + X\n\n-- The (co)list functor.\nListF : Set \u2192 Set \u2192 Set\nListF A X = \u22a4 + A \u00d7 X\n\n-- The stream functor.\nStreamF : Set \u2192 Set \u2192 Set\nStreamF A X = A \u00d7 X\n\n------------------------------------------------------------------------------\n-- Types as least fixed-points\n\n-- The empty type is a least fixed-point.\n\u22a5 : Set\n\u22a5 = \u03bc IdF\n\n-- The natural numbers type is a least fixed-point.\nN : Set\nN = \u03bc NatF\n\n-- The data constructors for the natural numbers.\nzero : N\nzero = In (inl <>)\n\nsucc : N \u2192 N\nsucc n = In (inr n)\n\n-- The list type is a least fixed-point.\nList : Set \u2192 Set\nList A = \u03bc (ListF A)\n\n-- The data constructors for List.\nnil : {A : Set} \u2192 List A\nnil = In (inl <>)\n\ncons : {A : Set} \u2192 A \u2192 List A \u2192 List A\ncons x xs = In (inr (x , xs))\n\n------------------------------------------------------------------------------\n-- Types as greatest fixed-points\n\n-- The unit type is a greatest fixed-point.\nUnit : Set\nUnit = \u03bd IdF\n\n-- Non-structural recursion\n-- unit : Nu IdF\n-- unit = Wrap IdF {!unit!}\n\n-- The conaturals type is a greatest fixed-point.\nConat : Set\nConat = \u03bd NatF\n\nzeroC : Conat\nzeroC = Wrap (inl <>)\n\nsuccC : Conat \u2192 Conat\nsuccC cn = Wrap (inr cn)\n\n-- Non-structural recursion for the definition of \u221eC.\n-- \u221eC : Conat\n-- \u221eC = succC {!\u221eC!}\n\n-- TODO: The conat destructor.\npred : Conat \u2192 \u22a4 + Conat\npred cn with out cn\n... | inl _ = inl <>\n... | inr x = inr x\n\n-- The colist type is a greatest fixed-point.\nColist : Set \u2192 Set\nColist A = \u03bd (ListF A)\n\n-- The colist data constructors.\nnilCL : {A : Set} \u2192 Colist A\nnilCL = Wrap (inl <>)\n\nconsCL : {A : Set} \u2192 A \u2192 Colist A \u2192 Colist A\nconsCL x xs = Wrap (inr (x , xs))\n\n-- The colist destructors.\nnullCL : {A : Set} \u2192 Colist A \u2192 Bool\nnullCL xs with out xs\n... | inl _ = true\n... | inr _ = false\n\n-- headCL : {A : Set} \u2192 Colist A \u2192 A\n-- headCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (x , _) = x\n\n-- tailCL : {A : Set} \u2192 Colist A \u2192 Colist A\n-- tailCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (_ , xs') = xs'\n\n-- The stream type is a greatest fixed-point.\nStream : Set \u2192 Set\nStream A = \u03bd (StreamF A)\n\n-- The stream data constructor.\nconsS : {A : Set} \u2192 A \u2192 Stream A \u2192 Stream A\nconsS x xs = Wrap (x , xs)\n\n-- The stream destructors.\nheadS : {A : Set} \u2192 Stream A \u2192 A\nheadS xs with out xs\n... | x , _ = x\n\ntailS : {A : Set} \u2192 Stream A \u2192 Stream A\ntailS xs with out xs\n... | _ , xs' = xs'\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Vene, Varmo (2000). Categorical programming with inductive and\n-- coinductive types. PhD thesis. Faculty of Mathematics: University\n-- of Tartu.\n","old_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Functors where\n\n-- The carrier of the initial algebra is (up to isomorphism) a\n-- fixed-point of the functor (Vene 2000, p).\n\n------------------------------------------------------------------------------\n\ndata Bool : Set where\n false true : Bool\n\ndata _\u228e_ (A B : Set) : Set where\n inl : A \u2192 A \u228e B\n inr : B \u2192 A \u228e B\n\ndata _\u00d7_ (A B : Set) : Set where\n _,_ : A \u2192 B \u2192 A \u00d7 B\n\n-- The terminal object.\ndata One : Set where\n one : One\n\npostulate\n -- The least fixed-point.\n\n -- Haskell definitions:\n\n -- data Mu f = In (f (Mu f))\n\n -- unIn :: Mu f \u2192 f (Mu f)\n -- unIn (In x) = x\n\n \u03bc : (Set \u2192 Set) \u2192 Set\n In : {F : Set \u2192 Set} \u2192 F (\u03bc F) \u2192 \u03bc F\n unIn : {F : Set \u2192 Set} \u2192 \u03bc F \u2192 F (\u03bc F)\n\npostulate\n -- The greatest fixed-point.\n\n -- Haskell definitions:\n\n -- data Nu f = Wrap (f (Nu f))\n\n -- out :: Nu f \u2192 (f (Nu f))\n -- out (Wrap x) = x\n\n \u03bd : (Set \u2192 Set) \u2192 Set\n Wrap : {F : Set \u2192 Set} \u2192 F (\u03bd F) \u2192 \u03bd F\n out : {F : Set \u2192 Set} \u2192 \u03bd F \u2192 F (\u03bd F)\n\n------------------------------------------------------------------------------\n-- Functors\n\n-- The identity functor (the functor for the empty and unit types).\nIdF : Set \u2192 Set\nIdF X = X\n\n-- The (co)natural numbers functor.\nNatF : Set \u2192 Set\nNatF X = One \u228e X\n\n-- The (co)list functor.\nListF : Set \u2192 Set \u2192 Set\nListF A X = One \u228e (A \u00d7 X)\n\n-- The stream functor.\nStreamF : Set \u2192 Set \u2192 Set\nStreamF A X = A \u00d7 X\n\n------------------------------------------------------------------------------\n-- Types as least fixed-points\n\n-- The empty type is a least fixed-point.\n\u22a5 : Set\n\u22a5 = \u03bc IdF\n\n-- The natural numbers type is a least fixed-point.\nN : Set\nN = \u03bc NatF\n\n-- The data constructors for the natural numbers.\nzero : N\nzero = In (inl one)\n\nsucc : N \u2192 N\nsucc n = In (inr n)\n\n-- The list type is a least fixed-point.\nList : Set \u2192 Set\nList A = \u03bc (ListF A)\n\n-- The data constructors for List.\nnil : {A : Set} \u2192 List A\nnil = In (inl one)\n\ncons : {A : Set} \u2192 A \u2192 List A \u2192 List A\ncons x xs = In (inr (x , xs))\n\n------------------------------------------------------------------------------\n-- Types as greatest fixed-points\n\n-- The unit type is a greatest fixed-point.\nUnit : Set\nUnit = \u03bd IdF\n\n-- Non-structural recursion\n-- unit : Nu IdF\n-- unit = Wrap IdF {!unit!}\n\n-- The conaturals type is a greatest fixed-point.\nConat : Set\nConat = \u03bd NatF\n\nzeroC : Conat\nzeroC = Wrap (inl one)\n\nsuccC : Conat \u2192 Conat\nsuccC cn = Wrap (inr cn)\n\n-- Non-structural recursion for the definition of \u221eC.\n-- \u221eC : Conat\n-- \u221eC = succC {!\u221eC!}\n\n-- TODO: The conat destructor.\npred : Conat \u2192 One \u228e Conat\npred cn with out cn\n... | inl _ = inl one\n... | inr x = inr x\n\n-- The colist type is a greatest fixed-point.\nColist : Set \u2192 Set\nColist A = \u03bd (ListF A)\n\n-- The colist data constructors.\nnilCL : {A : Set} \u2192 Colist A\nnilCL = Wrap (inl one)\n\nconsCL : {A : Set} \u2192 A \u2192 Colist A \u2192 Colist A\nconsCL x xs = Wrap (inr (x , xs))\n\n-- The colist destructors.\nnullCL : {A : Set} \u2192 Colist A \u2192 Bool\nnullCL xs with out xs\n... | inl _ = true\n... | inr _ = false\n\n-- headCL : {A : Set} \u2192 Colist A \u2192 A\n-- headCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (x , _) = x\n\n-- tailCL : {A : Set} \u2192 Colist A \u2192 Colist A\n-- tailCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (_ , xs') = xs'\n\n-- The stream type is a greatest fixed-point.\nStream : Set \u2192 Set\nStream A = \u03bd (StreamF A)\n\n-- The stream data constructor.\nconsS : {A : Set} \u2192 A \u2192 Stream A \u2192 Stream A\nconsS x xs = Wrap (x , xs)\n\n-- The stream destructors.\nheadS : {A : Set} \u2192 Stream A \u2192 A\nheadS xs with out xs\n... | x , _ = x\n\ntailS : {A : Set} \u2192 Stream A \u2192 Stream A\ntailS xs with out xs\n... | _ , xs' = xs'\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Vene, Varmo (2000). Categorical programming with inductive and\n-- coinductive types. PhD thesis. Faculty of Mathematics: University\n-- of Tartu.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a03a22bdb93489d665eb5cd0150477b68a74b8a7","subject":"Inline Structure module","message":"Inline Structure module\n\nOld-commit-hash: a1f90eeec3bbf0ee39c738ae1cb8b8557443544c\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Equivalence.agda","new_file":"Base\/Change\/Equivalence.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\nmodule _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set a\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\nmodule _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Delta-observational equivalence\n------------------------------------------------------------------------\nmodule Base.Change.Equivalence where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra\nopen import Level\nopen import Data.Unit\n\nmodule Structure where\n\n module _ {a \u2113} {A : Set a} {{ca : ChangeAlgebra \u2113 A}} {x : A} where\n -- Delta-observational equivalence: these asserts that two changes\n -- give the same result when applied to a base value.\n _\u2259_ : \u2200 dx dy \u2192 Set a\n dx \u2259 dy = x \u229e dx \u2261 x \u229e dy\n\n -- _\u2259_ is indeed an equivalence relation:\n \u2259-refl : \u2200 {dx} \u2192 dx \u2259 dx\n \u2259-refl = refl\n\n \u2259-symm : \u2200 {dx dy} \u2192 dx \u2259 dy \u2192 dy \u2259 dx\n \u2259-symm \u2259 = sym \u2259\n\n \u2259-trans : \u2200 {dx dy dz} \u2192 dx \u2259 dy \u2192 dy \u2259 dz \u2192 dx \u2259 dz\n \u2259-trans \u2259\u2081 \u2259\u2082 = trans \u2259\u2081 \u2259\u2082\n\n -- TODO: we want to show that all functions of interest respect\n -- delta-observational equivalence, so that two d.o.e. changes can be\n -- substituted for each other freely.\n --\n -- * That should be be true for\n -- functions using changes parametrically.\n --\n -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];\n -- this is proved below on both contexts at once by fun-change-respects.\n --\n -- * Finally, change algebra operations should respect d.o.e. But \u229e respects\n -- it by definition, and \u229f doesn't take change arguments - we will only\n -- need a proof for compose, when we define it.\n --\n -- Stating the general result, though, seems hard, we should\n -- rather have lemmas proving that certain classes of functions respect this\n -- equivalence.\n\n module _ {a} {b} {c} {d} {A : Set a} {B : Set b}\n {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where\n\n module FC = FunctionChanges A B {{CA}} {{CB}}\n open FC using (changeAlgebra; incrementalization)\n open FC.FunctionChange\n\n fun-change-respects : \u2200 {x : A} {dx\u2081 dx\u2082 : \u0394 x} {f : A \u2192 B} {df\u2081 df\u2082} \u2192\n df\u2081 \u2259 df\u2082 \u2192 dx\u2081 \u2259 dx\u2082 \u2192 apply df\u2081 x dx\u2081 \u2259 apply df\u2082 x dx\u2082\n fun-change-respects {x} {dx\u2081} {dx\u2082} {f} {df\u2081} {df\u2082} df\u2081\u2259df\u2082 dx\u2081\u2259dx\u2082 = lemma\n where\n open \u2261-Reasoning\n -- This type signature just expands the goal manually a bit.\n lemma : f x \u229e apply df\u2081 x dx\u2081 \u2261 f x \u229e apply df\u2082 x dx\u2082\n -- Informally: use incrementalization on both sides and then apply\n -- congruence.\n lemma =\n begin\n f x \u229e apply df\u2081 x dx\u2081\n \u2261\u27e8 sym (incrementalization f df\u2081 x dx\u2081) \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2081)\n \u2261\u27e8 cong (f \u229e df\u2081) dx\u2081\u2259dx\u2082 \u27e9\n (f \u229e df\u2081) (x \u229e dx\u2082)\n \u2261\u27e8 cong (\u03bb f \u2192 f (x \u229e dx\u2082)) df\u2081\u2259df\u2082 \u27e9\n (f \u229e df\u2082) (x \u229e dx\u2082)\n \u2261\u27e8 incrementalization f df\u2082 x dx\u2082 \u27e9\n f x \u229e apply df\u2082 x dx\u2082\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"be6f1fa3a0adbb680ac349286c28d45c17722184","subject":"Fix some concrete non-sense about the division","message":"Fix some concrete non-sense about the division\n","repos":"crypto-agda\/crypto-agda","old_file":"Prob.agda","new_file":"Prob.agda","new_contents":"module Prob where\n\nopen import Data.Bool\nopen import Data.Nat\nopen import Data.Vec\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\n\nopen import Function\n\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Relation.Nullary\n\nrecord ]0,1]-ops (]0,1] : Set) (_\u2264E_ : ]0,1] \u2192 ]0,1] \u2192 Set) : Set where\n constructor mk\n field \n 1E : ]0,1]\n _+E_ : ]0,1] \u2192 ]0,1] \u2192 ]0,1]\n _\u00b7E_ : ]0,1] \u2192 ]0,1] \u2192 ]0,1]\n _\/E_<_> : (x y : ]0,1]) \u2192 x \u2264E y \u2192 ]0,1]\n\nmodule [0,1] {]0,1] _\u2264E_} (]0,1]R : ]0,1]-ops ]0,1] _\u2264E_) where\n\n open ]0,1]-ops ]0,1]R public\n\n infixl 6 _+I_\n infix 4 _\u2264I_\n\n data [0,1] : Set where\n 0I : [0,1]\n _I : ]0,1] \u2192 [0,1]\n\n data _\u2264I_ : [0,1] \u2192 [0,1] \u2192 Set where\n z\u2264n : \u2200 {n} \u2192 0I \u2264I n\n E\u2264E : \u2200 {x y} \u2192 x \u2264E y \u2192 (x I) \u2264I (y I)\n\n 1I : [0,1]\n 1I = 1E I\n\n Pos : [0,1] \u2192 Set\n Pos 0I = \u22a5\n Pos (_ I) = \u22a4\n\n _<_> : (x : [0,1]) \u2192 Pos x \u2192 ]0,1] \n 0I < () >\n (x I) < pos > = x\n\n _+I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I +I x = x\n x I +I 0I = x I\n x I +I x\u2081 I = (x +E x\u2081) I\n\n _\u00b7I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I \u00b7I y = 0I\n (x I) \u00b7I 0I = 0I\n (x I) \u00b7I (x\u2081 I) = (x \u00b7E x\u2081) I\n\n -- 1\/I1+_ : \u2115 \u2192 [0,1]\n\n _\/I_<_,_> : (x y : [0,1]) \u2192 x \u2264I y \u2192 Pos y \u2192 [0,1]\n _ \/I 0I < _ , () >\n 0I \/I _ < _ , _ > = 0I\n (x I) \/I (y I) < E\u2264E pf , _ > = (x \/E y < pf >)I\n\n +I-identity : (x : [0,1]) \u2192 x \u2261 0I +I x\n +I-identity x = refl\n\n postulate\n +I-sym : (x y : [0,1]) \u2192 x +I y \u2261 y +I x\n +I-assoc : (x y z : [0,1]) \u2192 x +I y +I z \u2261 x +I (y +I z)\n\n \u2264I-refl : {x : [0,1]} \u2192 x \u2264I x\n\n \u2264I-trans : {x y z : [0,1]} \u2192 x \u2264I y \u2192 y \u2264I z \u2192 x \u2264I z\n \u2264I-mono : (x : [0,1]){y z : [0,1]} \u2192 y \u2264I z \u2192 y \u2264I z +I x\n \u2264I-pres : (x : [0,1]){y z : [0,1]} \u2192 y \u2264I z \u2192 x +I y \u2264I x +I z\n\nmodule Univ {]0,1] _\u2264E_} (]0,1]R : ]0,1]-ops ]0,1] _\u2264E_)\n (U : Set)\n (size-1 : \u2115) \n (allU : Vec U (suc size-1))\n (x\u2208allU : (x : U) \u2192 x \u2208 allU) where \n\n open [0,1] ]0,1]R\n\n sumP : {n : \u2115} \u2192 (U \u2192 [0,1]) \u2192 Vec U n \u2192 [0,1]\n sumP P [] = 0I\n sumP P (x \u2237 xs) = (P x) +I (sumP P xs)\n\n module Prob (P : U \u2192 [0,1])\n (sumP\u22611 : sumP P allU \u2261 1I) where\n \n Event : Set\n Event = U \u2192 Bool \n \n pr_\u220b_ : Event \u2192 U \u2192 [0,1]\n pr A \u220b x = if A x then P x else 0I\n\n _\u222a_ : Event \u2192 Event \u2192 Event\n A\u2081 \u222a A\u2082 = \u03bb x \u2192 A\u2081 x \u2228 A\u2082 x\n\n _\u2229_ : Event \u2192 Event \u2192 Event\n A\u2081 \u2229 A\u2082 = \u03bb x \u2192 A\u2081 x \u2227 A\u2082 x\n\n _\u2286_ : Event \u2192 Event \u2192 Set\n A \u2286 B = \u2200 x \u2192 T(A x) \u2192 T(B x)\n\n \u2102[_] : Event \u2192 Event\n \u2102[ A ] = not \u2218 A\n\n Pr[_] : Event \u2192 [0,1]\n Pr[ A ] = sumP (pr_\u220b_ A) allU\n\n postulate\n Pr-mono : \u2200 {A B} \u2192 A \u2286 B \u2192 Pr[ A ] \u2264I Pr[ B ]\n\n \u222a-lem : \u2200 {A} B \u2192 A \u2286 (A \u222a B)\n \u222a-lem {A} _ x with A x\n ... | true = id\n ... | false = \u03bb()\n\n \u2229-lem : \u2200 A {B} \u2192 (A \u2229 B) \u2286 B\n \u2229-lem A x with A x\n ... | true = id\n ... | false = \u03bb()\n\n Pr[_\u2223_]<_> : (A B : Event)(pf : Pos Pr[ B ]) \u2192 [0,1] \n Pr[ A \u2223 B ]< pr > = Pr[ A \u2229 B ] \/I Pr[ B ] < Pr-mono (\u2229-lem A) , pr >\n\n _ind_ : (A B : Event) \u2192 Set\n A ind B = Pr[ A ] \u00b7I Pr[ B ] \u2261 Pr[ A \u2229 B ]\n\n union-bound : (A\u2081 A\u2082 : Event) \u2192 Pr[ (A\u2081 \u222a A\u2082) ] \u2264I Pr[ A\u2081 ] +I Pr[ A\u2082 ]\n union-bound A\u2081 A\u2082 = go allU where\n sA\u2081 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2081 = \u03bb xs \u2192 sumP (pr_\u220b_ A\u2081) xs\n sA\u2082 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2082 = sumP (pr_\u220b_ A\u2082)\n go : {n : \u2115}(xs : Vec U n) \u2192 sumP (pr_\u220b_ (A\u2081 \u222a A\u2082)) xs \u2264I sumP (pr_\u220b_ A\u2081) xs +I sumP (pr_\u220b_ A\u2082) xs\n go [] = \u2264I-refl\n go (x \u2237 xs) with A\u2081 x | A\u2082 x \n ... | true | true rewrite +I-assoc (P x) (sA\u2081 xs) (P x +I sA\u2082 xs)\n | +I-sym (P x) (sA\u2082 xs)\n | sym (+I-assoc (sA\u2081 xs) (sA\u2082 xs) (P x))\n = \u2264I-pres (P x) (\u2264I-mono (P x) (go xs)) \n ... | true | false rewrite +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | true rewrite sym (+I-assoc (sA\u2081 xs) (P x) (sA\u2082 xs))\n | +I-sym (sA\u2081 xs) (P x)\n | +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | false = go xs \n\n module RandomVar (V : Set) (_==V_ : V \u2192 V \u2192 Bool) where\n RV : Set\n RV = U \u2192 V\n\n _^-1_ : RV \u2192 V \u2192 Event\n RV ^-1 v = \u03bb x \u2192 RV x ==V v \n\n _\u2261r_ : RV \u2192 V \u2192 Event \n RV \u2261r v = RV ^-1 v\n \n -- sugar, socker et sucre\n Pr[_\u2261_] : RV \u2192 V \u2192 [0,1]\n Pr[ X \u2261 v ] = Pr[ X \u2261r v ]\n \n -- \u2200 (a b) . Pr[X = a and Y = b] = Pr[X = a] \u00b7 Pr[Y = b]\n _indRV_ : RV \u2192 RV \u2192 Set\n X indRV Y = (a b : V) \u2192 (X ^-1 a) ind (Y ^-1 b)\n","old_contents":"module Prob where\n\nopen import Data.Bool\nopen import Data.Nat\nopen import Data.Vec\n\nopen import Function\n\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Relation.Nullary\n\nrecord ]0,1]-ops (]0,1] : Set) : Set where\n field \n 1E : ]0,1]\n _+E_ : ]0,1] \u2192 ]0,1] \u2192 ]0,1]\n _\u00b7E_ : ]0,1] \u2192 ]0,1] \u2192 ]0,1]\n 1\/E_ : ]0,1] \u2192 ]0,1]\n\nmodule [0,1] {]0,1]}(]0,1]R : ]0,1]-ops ]0,1]) where\n\n open ]0,1]-ops ]0,1]R public\n\n infixl 6 _+I_\n infix 4 _\u2264I_\n\n data [0,1] : Set where\n 0I : [0,1]\n _I : ]0,1] \u2192 [0,1]\n\n 1I : [0,1]\n 1I = 1E I\n\n Pos : [0,1] \u2192 Set\n Pos x = \u00ac (x \u2261 0I)\n\n _<_> : (x : [0,1]) \u2192 Pos x \u2192 ]0,1] \n 0I < pos > with pos refl\n ... | ()\n (x I) < pos > = x\n\n \n\n _+I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I +I x = x\n x I +I 0I = x I\n x I +I x\u2081 I = (x +E x\u2081) I\n\n _\u00b7I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I \u00b7I y = 0I\n (x I) \u00b7I 0I = 0I\n (x I) \u00b7I (x\u2081 I) = (x \u00b7E x\u2081) I\n\n 1\/I_ : ]0,1] \u2192 [0,1]\n 1\/I x = (1\/E x) I\n\n _\/I_ : [0,1] \u2192 ]0,1] \u2192 [0,1]\n x \/I y = x \u00b7I (1\/I y)\n\n +I-identity : (x : [0,1]) \u2192 x \u2261 0I +I x\n +I-identity x = refl\n\n postulate\n +I-sym : (x y : [0,1]) \u2192 x +I y \u2261 y +I x\n +I-assoc : (x y z : [0,1]) \u2192 x +I y +I z \u2261 x +I (y +I z)\n\n _\u2264I_ : [0,1] \u2192 [0,1] \u2192 Set\n \u2264I-refl : {x : [0,1]} \u2192 x \u2264I x\n\n \u2264I-trans : {x y z : [0,1]} \u2192 x \u2264I y \u2192 y \u2264I z \u2192 x \u2264I z\n \u2264I-mono : (x : [0,1]){y z : [0,1]} \u2192 y \u2264I z \u2192 y \u2264I z +I x\n \u2264I-pres : (x : [0,1]){y z : [0,1]} \u2192 y \u2264I z \u2192 x +I y \u2264I x +I z\n\nmodule Univ ( ]0,1] : Set)(]0,1]R : ]0,1]-ops ]0,1])\n (U : Set)\n (size-1 : \u2115) \n (allU : Vec U (suc size-1))\n (x\u2208allU : (x : U) \u2192 x \u2208 allU) where \n\n open [0,1] ]0,1]R\n\n sumP : {n : \u2115} \u2192 (U \u2192 [0,1]) \u2192 Vec U n \u2192 [0,1]\n sumP P [] = 0I\n sumP P (x \u2237 xs) = (P x) +I (sumP P xs)\n\n module Prob (P : U \u2192 [0,1])\n (sumP\u22611 : sumP P allU \u2261 1I) where\n \n Event : Set\n Event = U \u2192 Bool \n \n pr_\u220b_ : Event \u2192 U \u2192 [0,1]\n pr A \u220b x = if A x then P x else 0I\n\n _\u222a_ : Event \u2192 Event \u2192 Event\n A\u2081 \u222a A\u2082 = \u03bb x \u2192 A\u2081 x \u2228 A\u2082 x\n\n _\u2229_ : Event \u2192 Event \u2192 Event\n A\u2081 \u2229 A\u2082 = \u03bb x \u2192 A\u2081 x \u2227 A\u2082 x\n\n \u2102[_] : Event \u2192 Event\n \u2102[ A ] = not \u2218 A\n\n Pr[_] : Event \u2192 [0,1]\n Pr[ A ] = sumP (pr_\u220b_ A) allU\n\n Pr[_\u2223_]<_> : (A B : Event)(pf : Pos Pr[ B ]) \u2192 [0,1] \n Pr[ A \u2223 B ]< pr > = Pr[ A \u2229 B ] \/I Pr[ B ] < pr > \n\n _ind_ : (A B : Event) \u2192 Set\n A ind B = Pr[ A ] \u00b7I Pr[ B ] \u2261 Pr[ A \u2229 B ]\n\n union-bound : (A\u2081 A\u2082 : Event) \u2192 Pr[ (A\u2081 \u222a A\u2082) ] \u2264I Pr[ A\u2081 ] +I Pr[ A\u2082 ]\n union-bound A\u2081 A\u2082 = go allU where\n sA\u2081 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2081 = \u03bb xs \u2192 sumP (pr_\u220b_ A\u2081) xs\n sA\u2082 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2082 = sumP (pr_\u220b_ A\u2082)\n go : {n : \u2115}(xs : Vec U n) \u2192 sumP (pr_\u220b_ (A\u2081 \u222a A\u2082)) xs \u2264I sumP (pr_\u220b_ A\u2081) xs +I sumP (pr_\u220b_ A\u2082) xs\n go [] = \u2264I-refl\n go (x \u2237 xs) with A\u2081 x | A\u2082 x \n ... | true | true rewrite +I-assoc (P x) (sA\u2081 xs) (P x +I sA\u2082 xs)\n | +I-sym (P x) (sA\u2082 xs)\n | sym (+I-assoc (sA\u2081 xs) (sA\u2082 xs) (P x))\n = \u2264I-pres (P x) (\u2264I-mono (P x) (go xs)) \n ... | true | false rewrite +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | true rewrite sym (+I-assoc (sA\u2081 xs) (P x) (sA\u2082 xs))\n | +I-sym (sA\u2081 xs) (P x)\n | +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | false = go xs \n\n module RandomVar (V : Set) (_==V_ : V \u2192 V \u2192 Bool) where\n RV : Set\n RV = U \u2192 V\n\n _^-1_ : RV \u2192 V \u2192 Event\n RV ^-1 v = \u03bb x \u2192 RV x ==V v \n\n _\u2261r_ : RV \u2192 V \u2192 Event \n RV \u2261r v = RV ^-1 v\n \n -- sugar, socker et sucre\n Pr[_\u2261_] : RV \u2192 V \u2192 [0,1]\n Pr[ X \u2261 v ] = Pr[ X \u2261r v ]\n \n -- \u2200 (a b) . Pr[X = a and Y = b] = Pr[X = a] \u00b7 Pr[Y = b]\n _indRV_ : RV \u2192 RV \u2192 Set\n X indRV Y = (a b : V) \u2192 (X ^-1 a) ind (Y ^-1 b) ","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"d2408aa967f85d327836b63f65e967e844774910","subject":"Cleaning","message":"Cleaning\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/DarkwingDuck\/Derived.agda","new_file":"formalization\/agda\/Spire\/DarkwingDuck\/Derived.agda","new_contents":"{-# OPTIONS --type-in-type --no-pattern-matching #-}\nopen import Spire.DarkwingDuck.Primitive\nmodule Spire.DarkwingDuck.Derived where\n\n----------------------------------------------------------------------\n\nISet : Set \u2192 Set\nISet I = I \u2192 Set\n\nEnum : Set\nEnum = List String\n\nTag : Enum \u2192 Set\nTag xs = Elem String xs\n\nproj\u2081 : \u2200{A B} \u2192 \u03a3 A B \u2192 A\nproj\u2081 = elimPair _ (\u03bb a b \u2192 a)\n\nproj\u2082 : \u2200{A B} (ab : \u03a3 A B) \u2192 B (proj\u2081 ab)\nproj\u2082 = elimPair _ (\u03bb a b \u2192 b)\n\nBranchesM : Enum \u2192 Set\nBranchesM E = (P : Tag E \u2192 Set) \u2192 Set\n\nBranches : (E : Enum) \u2192 BranchesM E\nBranches = elimList BranchesM\n (\u03bb P \u2192 \u22a4)\n (\u03bb l E ih P \u2192 \u03a3 (P here) (\u03bb _ \u2192 ih (\u03bb t \u2192 P (there t))))\n\nCase : (E : Enum) \u2192 Tag E \u2192 Set\nCase E t = (P : Tag E \u2192 Set) (cs : Branches E P) \u2192 P t\n\ncase' : (E : Enum) (t : Tag E) \u2192 Case E t\ncase' = elimElem String Case\n (\u03bb l E P c,cs \u2192 elimPair (\u03bb _ \u2192 P here) (\u03bb a b \u2192 a) c,cs)\n (\u03bb l E t ih P c,cs \u2192 ih (\u03bb t \u2192 P (there t)) (elimPair (\u03bb _ \u2192 Branches E (\u03bb t \u2192 P (there t))) (\u03bb a b \u2192 b) c,cs))\n\ncase : (E : Enum) (P : Tag E \u2192 Set) (cs : Branches E P) (t : Tag E) \u2192 P t\ncase E P cs t = case' E t P cs\n\nScope : Tel \u2192 Set\nScope = elimTel (\u03bb _ \u2192 Set) \u22a4 (\u03bb A B ih \u2192 \u03a3 A ih)\n\n----------------------------------------------------------------------\n\nUncurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedBranches E P X = Branches E P \u2192 X\n\nCurriedBranchesM : Enum \u2192 Set\nCurriedBranchesM E = (P : Tag E \u2192 Set) (X : Set) \u2192 Set\n\nCurriedBranches : (E : Enum) \u2192 CurriedBranchesM E\nCurriedBranches = elimList CurriedBranchesM\n (\u03bb P X \u2192 X)\n (\u03bb l E ih P X \u2192 P here \u2192 ih (\u03bb t \u2192 P (there t)) X)\n\nCurryBranches : Enum \u2192 Set\nCurryBranches E = (P : Tag E \u2192 Set) (X : Set) \u2192 UncurriedBranches E P X \u2192 CurriedBranches E P X\n\ncurryBranches : (E : Enum) \u2192 CurryBranches E\ncurryBranches = elimList CurryBranches\n (\u03bb P X f \u2192 f tt)\n (\u03bb l E ih P X f c \u2192 ih (\u03bb t \u2192 P (there t)) X (\u03bb cs \u2192 f (c , cs)))\n\n----------------------------------------------------------------------\n\nUncurriedScope : (T : Tel) (X : Scope T \u2192 Set) \u2192 Set\nUncurriedScope T X = (xs : Scope T) \u2192 X xs\n\nCurriedScope : (T : Tel) (X : Scope T \u2192 Set) \u2192 Set\nCurriedScope = elimTel\n (\u03bb T \u2192 (X : Scope T \u2192 Set) \u2192 Set)\n (\u03bb X \u2192 X tt)\n (\u03bb A B ih X \u2192 (a : A) \u2192 ih a (\u03bb b \u2192 X (a , b)))\n\nCurryScope : Tel \u2192 Set\nCurryScope T = (X : Scope T \u2192 Set) \u2192 UncurriedScope T X \u2192 CurriedScope T X\n\ncurryScope : (T : Tel) \u2192 CurryScope T\ncurryScope = elimTel CurryScope\n (\u03bb X f \u2192 f tt)\n (\u03bb A B ih X f a \u2192 ih a (\u03bb b \u2192 X (a , b)) (\u03bb b \u2192 f (a , b)))\n\nUncurryScope : Tel \u2192 Set\nUncurryScope T = (X : Scope T \u2192 Set) \u2192 CurriedScope T X \u2192 UncurriedScope T X\n\nuncurryScope : (T : Tel) \u2192 UncurryScope T\nuncurryScope = elimTel UncurryScope\n (\u03bb X x \u2192 elimUnit X x)\n (\u03bb A B ih X f \u2192 elimPair X (\u03bb a b \u2192 ih a (\u03bb b \u2192 X (a , b)) (f a) b))\n\n----------------------------------------------------------------------\n\nUncurriedFunc : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedFunc I D X = (i : I) \u2192 Func I D X i \u2192 X i\n\nCurriedFunc : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nCurriedFunc I = elimDesc\n (\u03bb _ \u2192 (X : ISet I) \u2192 Set)\n (\u03bb i X \u2192 X i)\n (\u03bb i D ih X \u2192 (x : X i) \u2192 ih X )\n (\u03bb A B ih X \u2192 (a : A) \u2192 ih a X)\n\ncurryFunc : (I : Set) (D : Desc I) (X : ISet I)\n \u2192 UncurriedFunc I D X \u2192 CurriedFunc I D X\ncurryFunc I = elimDesc\n (\u03bb D \u2192 (X : ISet I) \u2192 UncurriedFunc I D X \u2192 CurriedFunc I D X)\n (\u03bb i X cn \u2192 cn i refl)\n (\u03bb i D ih X cn x \u2192 ih X (\u03bb j xs \u2192 cn j (x , xs)))\n (\u03bb A B ih X cn a \u2192 ih a X (\u03bb j xs \u2192 cn j (a , xs)))\n\n----------------------------------------------------------------------\n\nUncurriedHyps : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedFunc I D X)\n \u2192 Set\nUncurriedHyps I D X P cn =\n (i : I) (xs : Func I D X i) (ihs : Hyps I D X P i xs) \u2192 P i (cn i xs)\n\nCurriedHypsM : (I : Set) (D : Desc I) \u2192 Set\nCurriedHypsM I D = (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (cn : UncurriedFunc I D X) \u2192 Set\n\nCurriedHyps : (I : Set) (D : Desc I) \u2192 CurriedHypsM I D\nCurriedHyps I = elimDesc (CurriedHypsM I)\n (\u03bb i X P cn \u2192 P i (cn i refl))\n (\u03bb i D ih X P cn \u2192 (x : X i) \u2192 P i x \u2192 ih X P (\u03bb j xs \u2192 cn j (x , xs)))\n (\u03bb A B ih X P cn \u2192 (a : A) \u2192 ih a X P (\u03bb j xs \u2192 cn j (a , xs)))\n\nUncurryHyps : (I : Set) (D : Desc I) \u2192 Set\nUncurryHyps I D = (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (cn : UncurriedFunc I D X)\n \u2192 CurriedHyps I D X P cn \u2192 UncurriedHyps I D X P cn\n\nuncurryHyps : (I : Set) (D : Desc I) \u2192 UncurryHyps I D\nuncurryHyps I = elimDesc\n (UncurryHyps I)\n (\u03bb j X P cn pf \u2192\n elimEq _ (\u03bb u \u2192 pf))\n (\u03bb j D ih X P cn pf i \u2192\n elimPair _ (\u03bb x xs ih,ihs \u2192\n ih X P (\u03bb j ys \u2192 cn j (x , ys)) (pf x (proj\u2081 ih,ihs)) i xs (proj\u2082 ih,ihs)))\n (\u03bb A B ih X P cn pf i \u2192\n elimPair _ (\u03bb a xs \u2192 ih a X P (\u03bb j ys \u2192 cn j (a , ys)) (pf a) i xs))\n\n----------------------------------------------------------------------\n\nrecord Data : Set where\n field\n N : String\n P : Tel\n I : Scope P \u2192 Tel\n E : Enum\n B : (p : Scope P) \u2192 Branches E (\u03bb _ \u2192 Desc (Scope (I p)))\n\n PS : Set\n PS = Scope P\n\n IS : PS \u2192 Set\n IS p = Scope (I p)\n\n C : (p : PS) \u2192 Tag E \u2192 Desc (IS p)\n C p = case E (\u03bb _ \u2192 Desc (IS p)) (B p)\n\n D : (p : PS) \u2192 Desc (IS p)\n D p = Arg (Tag E) (C p)\n\n F : (p : PS) \u2192 IS p \u2192 Set\n F p = \u03bc N PS IS p (D p)\n\n----------------------------------------------------------------------\n\nDecl :\n (N : String)\n (P : Tel)\n (I : CurriedScope P (\u03bb _ \u2192 Tel))\n (E : Enum)\n (B : let I = uncurryScope P (\u03bb _ \u2192 Tel) I\n in CurriedScope P \u03bb A \u2192 Branches E (\u03bb _ \u2192 Desc (Scope (I A))))\n \u2192 Data\nDecl N P I E B = record\n { N = N\n ; P = P\n ; I = uncurryScope P _ I\n ; E = E\n ; B = uncurryScope P _ B\n }\n\n----------------------------------------------------------------------\n\nEnd[_] : (I : Tel)\n \u2192 CurriedScope I (\u03bb _ \u2192 Desc (Scope I))\nEnd[_] I = curryScope I _ End\n\nRec[_] : (I : Tel)\n \u2192 CurriedScope I (\u03bb _ \u2192 Desc (Scope I) \u2192 Desc (Scope I))\nRec[_] I = curryScope I _ Rec\n\n----------------------------------------------------------------------\n\nFormUncurried : (R : Data)\n \u2192 UncurriedScope (Data.P R) \u03bb p\n \u2192 UncurriedScope (Data.I R p) \u03bb i\n \u2192 Set\nFormUncurried = Data.F\n\nForm : (R : Data)\n \u2192 CurriedScope (Data.P R) \u03bb p\n \u2192 CurriedScope (Data.I R p) \u03bb i\n \u2192 Set\nForm R =\n curryScope (Data.P R) (\u03bb p \u2192 CurriedScope (Data.I R p) \u03bb i \u2192 Set) \u03bb p \u2192\n curryScope (Data.I R p) (\u03bb i \u2192 Set) \u03bb i \u2192\n FormUncurried R p i\n\n----------------------------------------------------------------------\n\ninjUncurried : (R : Data)\n \u2192 UncurriedScope (Data.P R) \u03bb p\n \u2192 CurriedFunc (Data.IS R p) (Data.D R p) (Data.F R p)\ninjUncurried R p t = curryFunc (Data.IS R p) (Data.C R p t)\n (Data.F R p)\n (\u03bb i xs \u2192 init (t , xs))\n\ninj : (R : Data)\n \u2192 CurriedScope (Data.P R) \u03bb p\n \u2192 CurriedFunc (Data.IS R p) (Data.D R p) (Data.F R p)\ninj R = curryScope (Data.P R)\n (\u03bb p \u2192 CurriedFunc (Data.IS R p) (Data.D R p) (Data.F R p))\n (injUncurried R)\n\n----------------------------------------------------------------------\n\nindCurried : (\u2113 : String) (P : Set) (I : P \u2192 Set) (p : P) (D : Desc (I p))\n (M : (i : I p) \u2192 \u03bc \u2113 P I p D i \u2192 Set)\n (f : CurriedHyps (I p) D (\u03bc \u2113 P I p D) M (\u03bb _ \u2192 init))\n (i : I p)\n (x : \u03bc \u2113 P I p D i)\n \u2192 M i x\nindCurried \u2113 P I p D M f i x =\n ind \u2113 P I p D M (uncurryHyps (I p) D (\u03bc \u2113 P I p D) M (\u03bb _ \u2192 init) f) i x\n\nSumCurriedHyps : (R : Data)\n \u2192 UncurriedScope (Data.P R) \u03bb p\n \u2192 (M : CurriedScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set))\n \u2192 Tag (Data.E R) \u2192 Set\nSumCurriedHyps R p M t =\n let unM = uncurryScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set) M in\n CurriedHyps (Data.IS R p) (Data.C R p t) (Data.F R p) unM (\u03bb i xs \u2192 init (t , xs))\n\nelimUncurried : (R : Data)\n \u2192 UncurriedScope (Data.P R) \u03bb p\n \u2192 (M : CurriedScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set))\n \u2192 let unM = uncurryScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set) M\n in UncurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedScope (Data.I R p) (\u03bb i \u2192 (x : Data.F R p i) \u2192 unM i x))\nelimUncurried R p M cs = let\n unM = uncurryScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set) M\n in\n curryScope (Data.I R p) (\u03bb i \u2192 (x : Data.F R p i) \u2192 unM i x) \u03bb i x \u2192\n indCurried (Data.N R) (Data.PS R) (Data.IS R) p (Data.D R p) unM\n (case (Data.E R) (SumCurriedHyps R p M) cs)\n i x\n\nelim : (R : Data)\n \u2192 CurriedScope (Data.P R) \u03bb p\n \u2192 (M : CurriedScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set))\n \u2192 let unM = uncurryScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set) M\n in CurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedScope (Data.I R p) (\u03bb i \u2192 (x : Data.F R p i) \u2192 unM i x))\nelim R = curryScope (Data.P R)\n (\u03bb p \u2192 (M : CurriedScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set))\n \u2192 let unM = uncurryScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set) M\n in CurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedScope (Data.I R p) (\u03bb i \u2192 (x : Data.F R p i) \u2192 unM i x)))\n (\u03bb p M \u2192 curryBranches (Data.E R) _ _\n (elimUncurried R p M))\n\n----------------------------------------------------------------------\n","old_contents":"{-# OPTIONS --type-in-type --no-pattern-matching #-}\nopen import Spire.DarkwingDuck.Primitive\nmodule Spire.DarkwingDuck.Derived where\n\n----------------------------------------------------------------------\n\nISet : Set \u2192 Set\nISet I = I \u2192 Set\n\nEnum : Set\nEnum = List String\n\nTag : Enum \u2192 Set\nTag xs = Elem String xs\n\nproj\u2081 : \u2200{A B} \u2192 \u03a3 A B \u2192 A\nproj\u2081 = elimPair _ (\u03bb a b \u2192 a)\n\nproj\u2082 : \u2200{A B} (ab : \u03a3 A B) \u2192 B (proj\u2081 ab)\nproj\u2082 = elimPair _ (\u03bb a b \u2192 b)\n\nBranches : (E : List String) (P : Tag E \u2192 Set) \u2192 Set\nBranches = elimList (\u03bb E \u2192 (P : Tag E \u2192 Set) \u2192 Set)\n (\u03bb P \u2192 \u22a4)\n (\u03bb l E ih P \u2192 \u03a3 (P here) (\u03bb _ \u2192 ih (\u03bb t \u2192 P (there t))))\n\ncase' : (E : List String) (t : Tag E) (P : Tag E \u2192 Set) (cs : Branches E P) \u2192 P t\ncase' = elimElem String (\u03bb E t \u2192 (P : Tag E \u2192 Set) (cs : Branches E P) \u2192 P t)\n (\u03bb l E P c,cs \u2192 elimPair (\u03bb _ \u2192 P here) (\u03bb a b \u2192 a) c,cs)\n (\u03bb l E t ih P c,cs \u2192 ih (\u03bb t \u2192 P (there t)) (elimPair (\u03bb _ \u2192 Branches E (\u03bb t \u2192 P (there t))) (\u03bb a b \u2192 b) c,cs))\n\ncase : {E : List String} (P : Tag E \u2192 Set) (cs : Branches E P) (t : Tag E) \u2192 P t\ncase P cs t = case' _ t P cs\n\nScope : Tel \u2192 Set\nScope = elimTel (\u03bb _ \u2192 Set) \u22a4 (\u03bb A B ih \u2192 \u03a3 A ih)\n\n----------------------------------------------------------------------\n\nUncurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedBranches E P X = Branches E P \u2192 X\n\nCurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedBranches = elimList\n (\u03bb E \u2192 (P : Tag E \u2192 Set) (X : Set) \u2192 Set)\n (\u03bb P X \u2192 X)\n (\u03bb l E ih P X \u2192 P here \u2192 ih (\u03bb t \u2192 P (there t)) X)\n\nCurryBranches : Enum \u2192 Set\nCurryBranches E = (P : Tag E \u2192 Set) (X : Set) \u2192 UncurriedBranches E P X \u2192 CurriedBranches E P X\n\ncurryBranches : (E : Enum) \u2192 CurryBranches E\ncurryBranches = elimList CurryBranches\n (\u03bb P X f \u2192 f tt)\n (\u03bb l E ih P X f c \u2192 ih (\u03bb t \u2192 P (there t)) X (\u03bb cs \u2192 f (c , cs)))\n\n----------------------------------------------------------------------\n\nUncurriedScope : (T : Tel) (X : Scope T \u2192 Set) \u2192 Set\nUncurriedScope T X = (xs : Scope T) \u2192 X xs\n\nCurriedScope : (T : Tel) (X : Scope T \u2192 Set) \u2192 Set\nCurriedScope = elimTel\n (\u03bb T \u2192 (X : Scope T \u2192 Set) \u2192 Set)\n (\u03bb X \u2192 X tt)\n (\u03bb A B ih X \u2192 (a : A) \u2192 ih a (\u03bb b \u2192 X (a , b)))\n\nCurryScope : Tel \u2192 Set\nCurryScope T = (X : Scope T \u2192 Set) \u2192 UncurriedScope T X \u2192 CurriedScope T X\n\ncurryScope : (T : Tel) \u2192 CurryScope T\ncurryScope = elimTel CurryScope\n (\u03bb X f \u2192 f tt)\n (\u03bb A B ih X f a \u2192 ih a (\u03bb b \u2192 X (a , b)) (\u03bb b \u2192 f (a , b)))\n\nUncurryScope : Tel \u2192 Set\nUncurryScope T = (X : Scope T \u2192 Set) \u2192 CurriedScope T X \u2192 UncurriedScope T X\n\nuncurryScope : (T : Tel) \u2192 UncurryScope T\nuncurryScope = elimTel UncurryScope\n (\u03bb X x \u2192 elimUnit X x)\n (\u03bb A B ih X f \u2192 elimPair X (\u03bb a b \u2192 ih a (\u03bb b \u2192 X (a , b)) (f a) b))\n\n----------------------------------------------------------------------\n\nUncurriedFunc : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedFunc I D X = (i : I) \u2192 Func I D X i \u2192 X i\n\nCurriedFunc : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nCurriedFunc I = elimDesc\n (\u03bb _ \u2192 (X : ISet I) \u2192 Set)\n (\u03bb i X \u2192 X i)\n (\u03bb i D ih X \u2192 (x : X i) \u2192 ih X )\n (\u03bb A B ih X \u2192 (a : A) \u2192 ih a X)\n\ncurryFunc : (I : Set) (D : Desc I) (X : ISet I)\n \u2192 UncurriedFunc I D X \u2192 CurriedFunc I D X\ncurryFunc I = elimDesc\n (\u03bb D \u2192 (X : ISet I) \u2192 UncurriedFunc I D X \u2192 CurriedFunc I D X)\n (\u03bb i X cn \u2192 cn i refl)\n (\u03bb i D ih X cn x \u2192 ih X (\u03bb j xs \u2192 cn j (x , xs)))\n (\u03bb A B ih X cn a \u2192 ih a X (\u03bb j xs \u2192 cn j (a , xs)))\n\n----------------------------------------------------------------------\n\nUncurriedHyps : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedFunc I D X)\n \u2192 Set\nUncurriedHyps I D X P cn =\n (i : I) (xs : Func I D X i) (ihs : Hyps I D X P i xs) \u2192 P i (cn i xs)\n\nCurriedHyps : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedFunc I D X)\n \u2192 Set\nCurriedHyps I = elimDesc\n (\u03bb D \u2192 (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (cn : UncurriedFunc I D X) \u2192 Set)\n (\u03bb i X P cn \u2192 P i (cn i refl))\n (\u03bb i D ih X P cn \u2192 (x : X i) \u2192 P i x \u2192 ih X P (\u03bb j xs \u2192 cn j (x , xs)))\n (\u03bb A B ih X P cn \u2192 (a : A) \u2192 ih a X P (\u03bb j xs \u2192 cn j (a , xs)))\n\nUncurryHyps : (I : Set) (D : Desc I) \u2192 Set\nUncurryHyps I D = (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (cn : UncurriedFunc I D X)\n \u2192 CurriedHyps I D X P cn \u2192 UncurriedHyps I D X P cn\n\nuncurryHyps : (I : Set) (D : Desc I) \u2192 UncurryHyps I D\nuncurryHyps I = elimDesc\n (UncurryHyps I)\n (\u03bb j X P cn pf \u2192\n elimEq _ (\u03bb u \u2192 pf))\n (\u03bb j D ih X P cn pf i \u2192\n elimPair _ (\u03bb x xs ih,ihs \u2192\n ih X P (\u03bb j ys \u2192 cn j (x , ys)) (pf x (proj\u2081 ih,ihs)) i xs (proj\u2082 ih,ihs)))\n (\u03bb A B ih X P cn pf i \u2192\n elimPair _ (\u03bb a xs \u2192 ih a X P (\u03bb j ys \u2192 cn j (a , ys)) (pf a) i xs))\n\n----------------------------------------------------------------------\n\nrecord Data : Set where\n field\n N : String\n P : Tel\n I : Scope P \u2192 Tel\n E : Enum\n B : (p : Scope P) \u2192 Branches E (\u03bb _ \u2192 Desc (Scope (I p)))\n\n PS : Set\n PS = Scope P\n\n IS : PS \u2192 Set\n IS p = Scope (I p)\n\n C : (p : PS) \u2192 Tag E \u2192 Desc (IS p)\n C p = case (\u03bb _ \u2192 Desc (IS p)) (B p)\n\n D : (p : PS) \u2192 Desc (IS p)\n D p = Arg (Tag E) (C p)\n\n F : (p : PS) \u2192 IS p \u2192 Set\n F p = \u03bc N PS IS p (D p)\n\n----------------------------------------------------------------------\n\nDecl :\n (N : String)\n (P : Tel)\n (I : CurriedScope P (\u03bb _ \u2192 Tel))\n (E : Enum)\n (B : let I = uncurryScope P (\u03bb _ \u2192 Tel) I\n in CurriedScope P \u03bb A \u2192 Branches E (\u03bb _ \u2192 Desc (Scope (I A))))\n \u2192 Data\nDecl N P I E B = record\n { N = N\n ; P = P\n ; I = uncurryScope P _ I\n ; E = E\n ; B = uncurryScope P _ B\n }\n\n----------------------------------------------------------------------\n\nEnd[_] : (I : Tel)\n \u2192 CurriedScope I (\u03bb _ \u2192 Desc (Scope I))\nEnd[_] I = curryScope I _ End\n\nRec[_] : (I : Tel)\n \u2192 CurriedScope I (\u03bb _ \u2192 Desc (Scope I) \u2192 Desc (Scope I))\nRec[_] I = curryScope I _ Rec\n\n----------------------------------------------------------------------\n\nFormUncurried : (R : Data)\n \u2192 UncurriedScope (Data.P R) \u03bb p\n \u2192 UncurriedScope (Data.I R p) \u03bb i\n \u2192 Set\nFormUncurried = Data.F\n\nForm : (R : Data)\n \u2192 CurriedScope (Data.P R) \u03bb p\n \u2192 CurriedScope (Data.I R p) \u03bb i\n \u2192 Set\nForm R =\n curryScope (Data.P R) (\u03bb p \u2192 CurriedScope (Data.I R p) \u03bb i \u2192 Set) \u03bb p \u2192\n curryScope (Data.I R p) (\u03bb i \u2192 Set) \u03bb i \u2192\n FormUncurried R p i\n\n----------------------------------------------------------------------\n\ninjUncurried : (R : Data)\n \u2192 UncurriedScope (Data.P R) \u03bb p\n \u2192 CurriedFunc (Data.IS R p) (Data.D R p) (Data.F R p)\ninjUncurried R p t = curryFunc (Data.IS R p) (Data.C R p t)\n (Data.F R p)\n (\u03bb i xs \u2192 init (t , xs))\n\ninj : (R : Data)\n \u2192 CurriedScope (Data.P R) \u03bb p\n \u2192 CurriedFunc (Data.IS R p) (Data.D R p) (Data.F R p)\ninj R = curryScope (Data.P R)\n (\u03bb p \u2192 CurriedFunc (Data.IS R p) (Data.D R p) (Data.F R p))\n (injUncurried R)\n\n----------------------------------------------------------------------\n\nindCurried : (\u2113 : String) (P : Set) (I : P \u2192 Set) (p : P) (D : Desc (I p))\n (M : (i : I p) \u2192 \u03bc \u2113 P I p D i \u2192 Set)\n (f : CurriedHyps (I p) D (\u03bc \u2113 P I p D) M (\u03bb _ \u2192 init))\n (i : I p)\n (x : \u03bc \u2113 P I p D i)\n \u2192 M i x\nindCurried \u2113 P I p D M f i x =\n ind \u2113 P I p D M (uncurryHyps (I p) D (\u03bc \u2113 P I p D) M (\u03bb _ \u2192 init) f) i x\n\nSumCurriedHyps : (R : Data)\n \u2192 UncurriedScope (Data.P R) \u03bb p\n \u2192 (M : CurriedScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set))\n \u2192 Tag (Data.E R) \u2192 Set\nSumCurriedHyps R p M t =\n let unM = uncurryScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set) M in\n CurriedHyps (Data.IS R p) (Data.C R p t) (Data.F R p) unM (\u03bb i xs \u2192 init (t , xs))\n\nelimUncurried : (R : Data)\n \u2192 UncurriedScope (Data.P R) \u03bb p\n \u2192 (M : CurriedScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set))\n \u2192 let unM = uncurryScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set) M\n in UncurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedScope (Data.I R p) (\u03bb i \u2192 (x : Data.F R p i) \u2192 unM i x))\nelimUncurried R p M cs = let\n unM = uncurryScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set) M\n in\n curryScope (Data.I R p) (\u03bb i \u2192 (x : Data.F R p i) \u2192 unM i x) \u03bb i x \u2192\n indCurried (Data.N R) (Data.PS R) (Data.IS R) p (Data.D R p) unM\n (case (SumCurriedHyps R p M) cs)\n i x\n\nelim : (R : Data)\n \u2192 CurriedScope (Data.P R) \u03bb p\n \u2192 (M : CurriedScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set))\n \u2192 let unM = uncurryScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set) M\n in CurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedScope (Data.I R p) (\u03bb i \u2192 (x : Data.F R p i) \u2192 unM i x))\nelim R = curryScope (Data.P R)\n (\u03bb p \u2192 (M : CurriedScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set))\n \u2192 let unM = uncurryScope (Data.I R p) (\u03bb i \u2192 Data.F R p i \u2192 Set) M\n in CurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedScope (Data.I R p) (\u03bb i \u2192 (x : Data.F R p i) \u2192 unM i x)))\n (\u03bb p M \u2192 curryBranches (Data.E R) _ _\n (elimUncurried R p M))\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"4d607ac9e5770bdf5c6387421b0deb44c4fe3c1d","subject":"Fixed indentation.","message":"Fixed indentation.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Program\/ABP\/ABP.agda","new_file":"src\/fot\/FOTC\/Program\/ABP\/ABP.agda","new_contents":"------------------------------------------------------------------------------\n-- The alternating bit protocol (ABP)\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module define the ABP following the presentation in Dybjer and\n-- Sander (1989).\n--\n-- References:\n--\n-- \u2022 Dybjer, Peter and Sander, Herbert P. (1989). A Functional\n-- Programming Approach to the Speci\ufb01cation and Veri\ufb01cation of\n-- Concurrent Systems. In: Formal Aspects of Computing 1,\n-- pp. 303\u2013319.\n\nmodule FOTC.Program.ABP.ABP where\n\nopen import FOTC.Base\nopen import FOTC.Base.Loop\nopen import FOTC.Base.List\nopen import FOTC.Data.Bool\nopen import FOTC.Data.Stream\nopen import FOTC.Program.ABP.Fair\nopen import FOTC.Program.ABP.Terms\n\n------------------------------------------------------------------------------\n-- ABP equations\n\npostulate\n send ack out corrupt : D \u2192 D\n await : D \u2192 D \u2192 D \u2192 D \u2192 D\n\npostulate send-eq : \u2200 b i is ds \u2192\n send b \u00b7 (i \u2237 is) \u00b7 ds \u2261 < i , b > \u2237 await b i is ds\n{-# ATP axiom send-eq #-}\n\npostulate\n await-ok\u2261 : \u2200 b b' i is ds \u2192\n b \u2261 b' \u2192\n await b i is (ok b' \u2237 ds) \u2261 send (not b) \u00b7 is \u00b7 ds\n\n await-ok\u2262 : \u2200 b b' i is ds \u2192\n b \u2262 b' \u2192\n await b i is (ok b' \u2237 ds) \u2261 < i , b > \u2237 await b i is ds\n\n await-error : \u2200 b i is ds \u2192\n await b i is (error \u2237 ds) \u2261 < i , b > \u2237 await b i is ds\n{-# ATP axiom await-ok\u2261 await-ok\u2262 await-error #-}\n\npostulate\n ack-ok\u2261 : \u2200 b b' i bs \u2192\n b \u2261 b' \u2192\n ack b \u00b7 (ok < i , b' > \u2237 bs) \u2261 b \u2237 ack (not b) \u00b7 bs\n\n ack-ok\u2262 : \u2200 b b' i bs \u2192\n b \u2262 b' \u2192\n ack b \u00b7 (ok < i , b' > \u2237 bs) \u2261 not b \u2237 ack b \u00b7 bs\n\n ack-error : \u2200 b bs \u2192 ack b \u00b7 (error \u2237 bs) \u2261 not b \u2237 ack b \u00b7 bs\n{-# ATP axiom ack-ok\u2261 ack-ok\u2262 ack-error #-}\n\npostulate\n out-ok\u2261 : \u2200 b b' i bs \u2192\n b \u2261 b' \u2192\n out b \u00b7 (ok < i , b' > \u2237 bs) \u2261 i \u2237 out (not b) \u00b7 bs\n\n out-ok\u2262 : \u2200 b b' i bs \u2192\n b \u2262 b' \u2192\n out b \u00b7 (ok < i , b' > \u2237 bs) \u2261 out b \u00b7 bs\n\n out-error : \u2200 b bs \u2192 out b \u00b7 (error \u2237 bs) \u2261 out b \u00b7 bs\n{-# ATP axiom out-ok\u2261 out-ok\u2262 out-error #-}\n\npostulate\n corrupt-T : \u2200 os x xs \u2192\n corrupt (T \u2237 os) \u00b7 (x \u2237 xs) \u2261 ok x \u2237 corrupt os \u00b7 xs\n corrupt-F : \u2200 os x xs \u2192\n corrupt (F \u2237 os) \u00b7 (x \u2237 xs) \u2261 error \u2237 corrupt os \u00b7 xs\n{-# ATP axiom corrupt-T corrupt-F #-}\n\npostulate has hbs hcs hds : D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D\n\npostulate has-eq : \u2200 f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2192\n has f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2261 f\u2081 \u00b7 is \u00b7 (hds f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is)\n{-# ATP axiom has-eq #-}\n\npostulate hbs-eq : \u2200 f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2192\n hbs f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2261 g\u2081 \u00b7 (has f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is)\n{-# ATP axiom hbs-eq #-}\n\npostulate hcs-eq : \u2200 f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2192\n hcs f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2261 f\u2082 \u00b7 (hbs f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is)\n{-# ATP axiom hcs-eq #-}\n\npostulate hds-eq : \u2200 f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2192\n hds f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2261 g\u2082 \u00b7 (hcs f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is)\n{-# ATP axiom hds-eq #-}\n\npostulate\n transfer : D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D\n transfer-eq : \u2200 f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2192\n transfer f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2261 f\u2083 \u00b7 (hbs f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is)\n{-# ATP axiom transfer-eq #-}\n\npostulate\n abpTransfer : D \u2192 D \u2192 D \u2192 D \u2192 D\n abpTransfer-eq :\n \u2200 b os\u2081 os\u2082 is \u2192\n abpTransfer b os\u2081 os\u2082 is \u2261\n transfer (send b) (ack b) (out b) (corrupt os\u2081) (corrupt os\u2082) is\n{-# ATP axiom abpTransfer-eq #-}\n\n------------------------------------------------------------------------------\n-- ABP relations\n\n-- Start state for the ABP.\nS : D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 Set\nS b is os\u2081 os\u2082 as bs cs ds js =\n as \u2261 send b \u00b7 is \u00b7 ds\n \u2227 bs \u2261 corrupt os\u2081 \u00b7 as\n \u2227 cs \u2261 ack b \u00b7 bs\n \u2227 ds \u2261 corrupt os\u2082 \u00b7 cs\n \u2227 js \u2261 out b \u00b7 bs\n{-# ATP definition S #-}\n\n-- State for the ABP.\nS' : D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 Set\nS' b i' is' os\u2081' os\u2082' as' bs' cs' ds' js' =\n as' \u2261 await b i' is' ds' -- Typo in ds'.\n \u2227 bs' \u2261 corrupt os\u2081' \u00b7 as'\n \u2227 cs' \u2261 ack (not b) \u00b7 bs'\n \u2227 ds' \u2261 corrupt os\u2082' \u00b7 (b \u2237 cs')\n \u2227 js' \u2261 out (not b) \u00b7 bs'\n{-# ATP definition S' #-}\n\n-- Auxiliary bisimulation.\nB : D \u2192 D \u2192 Set\nB is js = \u2203[ b ] \u2203[ os\u2081 ] \u2203[ os\u2082 ] \u2203[ as ] \u2203[ bs ] \u2203[ cs ] \u2203[ ds ]\n Stream is\n \u2227 Bit b\n \u2227 Fair os\u2081\n \u2227 Fair os\u2082\n \u2227 S b is os\u2081 os\u2082 as bs cs ds js\n{-# ATP definition B #-}\n","old_contents":"------------------------------------------------------------------------------\n-- The alternating bit protocol (ABP)\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module define the ABP following the presentation in Dybjer and\n-- Sander (1989).\n--\n-- References:\n--\n-- \u2022 Dybjer, Peter and Sander, Herbert P. (1989). A Functional\n-- Programming Approach to the Speci\ufb01cation and Veri\ufb01cation of\n-- Concurrent Systems. In: Formal Aspects of Computing 1,\n-- pp. 303\u2013319.\n\nmodule FOTC.Program.ABP.ABP where\n\nopen import FOTC.Base\nopen import FOTC.Base.Loop\nopen import FOTC.Base.List\nopen import FOTC.Data.Bool\nopen import FOTC.Data.Stream\nopen import FOTC.Program.ABP.Fair\nopen import FOTC.Program.ABP.Terms\n\n------------------------------------------------------------------------------\n-- ABP equations\n\npostulate\n send ack out corrupt : D \u2192 D\n await : D \u2192 D \u2192 D \u2192 D \u2192 D\n\npostulate send-eq : \u2200 b i is ds \u2192\n send b \u00b7 (i \u2237 is) \u00b7 ds \u2261 < i , b > \u2237 await b i is ds\n{-# ATP axiom send-eq #-}\n\npostulate\n await-ok\u2261 : \u2200 b b' i is ds \u2192\n b \u2261 b' \u2192\n await b i is (ok b' \u2237 ds) \u2261 send (not b) \u00b7 is \u00b7 ds\n\n await-ok\u2262 : \u2200 b b' i is ds \u2192\n b \u2262 b' \u2192\n await b i is (ok b' \u2237 ds) \u2261 < i , b > \u2237 await b i is ds\n\n await-error : \u2200 b i is ds \u2192\n await b i is (error \u2237 ds) \u2261 < i , b > \u2237 await b i is ds\n{-# ATP axiom await-ok\u2261 await-ok\u2262 await-error #-}\n\npostulate\n ack-ok\u2261 : \u2200 b b' i bs \u2192\n b \u2261 b' \u2192\n ack b \u00b7 (ok < i , b' > \u2237 bs) \u2261 b \u2237 ack (not b) \u00b7 bs\n\n ack-ok\u2262 : \u2200 b b' i bs \u2192\n b \u2262 b' \u2192\n ack b \u00b7 (ok < i , b' > \u2237 bs) \u2261 not b \u2237 ack b \u00b7 bs\n\n ack-error : \u2200 b bs \u2192 ack b \u00b7 (error \u2237 bs) \u2261 not b \u2237 ack b \u00b7 bs\n{-# ATP axiom ack-ok\u2261 ack-ok\u2262 ack-error #-}\n\npostulate\n out-ok\u2261 : \u2200 b b' i bs \u2192\n b \u2261 b' \u2192\n out b \u00b7 (ok < i , b' > \u2237 bs) \u2261 i \u2237 out (not b) \u00b7 bs\n\n out-ok\u2262 : \u2200 b b' i bs \u2192\n b \u2262 b' \u2192\n out b \u00b7 (ok < i , b' > \u2237 bs) \u2261 out b \u00b7 bs\n\n out-error : \u2200 b bs \u2192 out b \u00b7 (error \u2237 bs) \u2261 out b \u00b7 bs\n{-# ATP axiom out-ok\u2261 out-ok\u2262 out-error #-}\n\npostulate\n corrupt-T : \u2200 os x xs \u2192\n corrupt (T \u2237 os) \u00b7 (x \u2237 xs) \u2261 ok x \u2237 corrupt os \u00b7 xs\n corrupt-F : \u2200 os x xs \u2192\n corrupt (F \u2237 os) \u00b7 (x \u2237 xs) \u2261 error \u2237 corrupt os \u00b7 xs\n{-# ATP axiom corrupt-T corrupt-F #-}\n\npostulate has hbs hcs hds : D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D\n\npostulate has-eq : \u2200 f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2192\n has f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2261 f\u2081 \u00b7 is \u00b7 (hds f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is)\n{-# ATP axiom has-eq #-}\n\npostulate hbs-eq : \u2200 f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2192\n hbs f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2261 g\u2081 \u00b7 (has f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is)\n{-# ATP axiom hbs-eq #-}\n\npostulate hcs-eq : \u2200 f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2192\n hcs f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2261 f\u2082 \u00b7 (hbs f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is)\n{-# ATP axiom hcs-eq #-}\n\npostulate hds-eq : \u2200 f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2192\n hds f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2261 g\u2082 \u00b7 (hcs f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is)\n{-# ATP axiom hds-eq #-}\n\npostulate\n transfer : D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D\n transfer-eq : \u2200 f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2192\n transfer f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is \u2261 f\u2083 \u00b7 (hbs f\u2081 f\u2082 f\u2083 g\u2081 g\u2082 is)\n{-# ATP axiom transfer-eq #-}\n\npostulate\n abpTransfer : D \u2192 D \u2192 D \u2192 D \u2192 D\n abpTransfer-eq :\n \u2200 b os\u2081 os\u2082 is \u2192\n abpTransfer b os\u2081 os\u2082 is \u2261\n transfer (send b) (ack b) (out b) (corrupt os\u2081) (corrupt os\u2082) is\n{-# ATP axiom abpTransfer-eq #-}\n\n------------------------------------------------------------------------------\n-- ABP relations\n\n-- Start state for the ABP.\nS : D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 Set\nS b is os\u2081 os\u2082 as bs cs ds js =\n as \u2261 send b \u00b7 is \u00b7 ds\n \u2227 bs \u2261 corrupt os\u2081 \u00b7 as\n \u2227 cs \u2261 ack b \u00b7 bs\n \u2227 ds \u2261 corrupt os\u2082 \u00b7 cs\n \u2227 js \u2261 out b \u00b7 bs\n{-# ATP definition S #-}\n\n-- State for the ABP.\nS' : D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 D \u2192 Set\nS' b i' is' os\u2081' os\u2082' as' bs' cs' ds' js' =\n as' \u2261 await b i' is' ds' -- Typo in ds'.\n \u2227 bs' \u2261 corrupt os\u2081' \u00b7 as'\n \u2227 cs' \u2261 ack (not b) \u00b7 bs'\n \u2227 ds' \u2261 corrupt os\u2082' \u00b7 (b \u2237 cs')\n \u2227 js' \u2261 out (not b) \u00b7 bs'\n{-# ATP definition S' #-}\n\n-- Auxiliary bisimulation.\nB : D \u2192 D \u2192 Set\nB is js = \u2203[ b ] \u2203[ os\u2081 ] \u2203[ os\u2082 ] \u2203[ as ] \u2203[ bs ] \u2203[ cs ] \u2203[ ds ]\n Stream is\n \u2227 Bit b\n \u2227 Fair os\u2081\n \u2227 Fair os\u2082\n \u2227 S b is os\u2081 os\u2082 as bs cs ds js\n{-# ATP definition B #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3852931ffc56b9e51f939e2c7f10aafb8889360e","subject":"updating comments","message":"updating comments\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"core.agda","new_file":"core.agda","new_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- external expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n -- the type of type contexts, i.e. \u0393s in the judegments below\n tctx : Set\n tctx = htyp ctx\n\n mutual\n data env : Set where\n Id : (\u0393 : tctx) \u2192 env\n Subst : (d : dhexp) \u2192 (y : Nat) \u2192 env \u2192 env\n\n -- internal expressions\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 env) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 env) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n _\u27e8_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 dhexp\n _\u27e8_\u21d2\u2987\u2988\u21d2\u0338_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 dhexp\n\n -- convenient notation for chaining together two agreeable casts\n _\u27e8_\u21d2_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 htyp \u2192 dhexp\n d \u27e8 t1 \u21d2 t2 \u21d2 t3 \u27e9 = d \u27e8 t1 \u21d2 t2 \u27e9 \u27e8 t2 \u21d2 t3 \u27e9\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- the type of hole contexts, i.e. \u0394s in the judgements\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n -- notation for a triple to match the CMTT syntax\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 (tctx \u00d7 htyp))\n u ::[ \u0393 ] \u03c4 = u , (\u0393 , \u03c4)\n\n -- the hole name u does not appear in the term e\n data hole-name-new : (e : hexp) (u : Nat) \u2192 Set where\n HNConst : \u2200{u} \u2192 hole-name-new c u\n HNAsc : \u2200{e \u03c4 u} \u2192\n hole-name-new e u \u2192\n hole-name-new (e \u00b7: \u03c4) u\n HNVar : \u2200{x u} \u2192 hole-name-new (X x) u\n HNLam1 : \u2200{x e u} \u2192\n hole-name-new e u \u2192\n hole-name-new (\u00b7\u03bb x e) u\n HNLam2 : \u2200{x e u \u03c4} \u2192\n hole-name-new e u \u2192\n hole-name-new (\u00b7\u03bb x [ \u03c4 ] e) u\n HNHole : \u2200{u u'} \u2192\n u' \u2260 u \u2192\n hole-name-new (\u2987\u2988[ u' ]) u\n HNNEHole : \u2200{u u' e} \u2192\n u' \u2260 u \u2192\n hole-name-new e u \u2192\n hole-name-new (\u2987 e \u2988[ u' ]) u\n HNAp : \u2200{ u e1 e2 } \u2192\n hole-name-new e1 u \u2192\n hole-name-new e2 u \u2192\n hole-name-new (e1 \u2218 e2) u\n\n -- two terms that do not share any hole names\n data holes-disjoint : (e1 : hexp) \u2192 (e2 : hexp) \u2192 Set where\n HDConst : \u2200{e} \u2192 holes-disjoint c e\n HDAsc : \u2200{e1 e2 \u03c4} \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint (e1 \u00b7: \u03c4) e2\n HDVar : \u2200{x e} \u2192 holes-disjoint (X x) e\n HDLam1 : \u2200{x e1 e2} \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint (\u00b7\u03bb x e1) e2\n HDLam2 : \u2200{x e1 e2 \u03c4} \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint (\u00b7\u03bb x [ \u03c4 ] e1) e2\n HDHole : \u2200{u e2} \u2192 hole-name-new e2 u \u2192 holes-disjoint (\u2987\u2988[ u ]) e2\n HDNEHole : \u2200{u e1 e2} \u2192 hole-name-new e2 u \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint (\u2987 e1 \u2988[ u ]) e2\n HDAp : \u2200{e1 e2 e3} \u2192 holes-disjoint e1 e3 \u2192 holes-disjoint e2 e3 \u2192 holes-disjoint (e1 \u2218 e2) e3\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {x : Nat} \u2192\n (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n holes-disjoint e1 e2 \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- those types without holes\n data _tcomplete : htyp \u2192 Set where\n TCBase : b tcomplete\n TCArr : \u2200{\u03c41 \u03c42} \u2192 \u03c41 tcomplete \u2192 \u03c42 tcomplete \u2192 (\u03c41 ==> \u03c42) tcomplete\n\n -- those external expressions without holes\n data _ecomplete : hexp \u2192 Set where\n ECConst : c ecomplete\n ECAsc : \u2200{\u03c4 e} \u2192 \u03c4 tcomplete \u2192 e ecomplete \u2192 (e \u00b7: \u03c4) ecomplete\n ECVar : \u2200{x} \u2192 (X x) ecomplete\n ECLam1 : \u2200{x e} \u2192 e ecomplete \u2192 (\u00b7\u03bb x e) ecomplete\n ECLam2 : \u2200{x e \u03c4} \u2192 e ecomplete \u2192 \u03c4 tcomplete \u2192 (\u00b7\u03bb x [ \u03c4 ] e) ecomplete\n ECAp : \u2200{e1 e2} \u2192 e1 ecomplete \u2192 e2 ecomplete \u2192 (e1 \u2218 e2) ecomplete\n\n -- those internal expressions without holes\n data _dcomplete : dhexp \u2192 Set where\n DCVar : \u2200{x} \u2192 (X x) dcomplete\n DCConst : c dcomplete\n DCLam : \u2200{x \u03c4 d} \u2192 d dcomplete \u2192 \u03c4 tcomplete \u2192 (\u00b7\u03bb x [ \u03c4 ] d) dcomplete\n DCAp : \u2200{d1 d2} \u2192 d1 dcomplete \u2192 d2 dcomplete \u2192 (d1 \u2218 d2) dcomplete\n DCCast : \u2200{d \u03c41 \u03c42} \u2192 d dcomplete \u2192 \u03c41 tcomplete \u2192 \u03c42 tcomplete \u2192 (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) dcomplete\n\n -- contexts that only produce complete types\n _gcomplete : tctx \u2192 Set\n \u0393 gcomplete = (x : Nat) (\u03c4 : htyp) \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u03c4 tcomplete\n\n -- those internal expressions where every cast is the identity cast and\n -- there are no failed casts\n data cast-id : dhexp \u2192 Set where\n CIConst : cast-id c\n CIVar : \u2200{x} \u2192 cast-id (X x)\n CILam : \u2200{x \u03c4 d} \u2192 cast-id d \u2192 cast-id (\u00b7\u03bb x [ \u03c4 ] d)\n CIHole : \u2200{u} \u2192 cast-id (\u2987\u2988\u27e8 u \u27e9)\n CINEHole : \u2200{d u} \u2192 cast-id d \u2192 cast-id (\u2987 d \u2988\u27e8 u \u27e9)\n CIAp : \u2200{d1 d2} \u2192 cast-id d1 \u2192 cast-id d2 \u2192 cast-id (d1 \u2218 d2)\n CICast : \u2200{d \u03c4} \u2192 cast-id d \u2192 cast-id (d \u27e8 \u03c4 \u21d2 \u03c4 \u27e9)\n\n -- expansion\n mutual\n -- synthesis\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp : \u2200{\u0393 e1 \u03c4 \u03c41 \u03c41' \u03c42 \u03c42' d1 \u03941 e2 d2 \u03942 } \u2192\n holes-disjoint e1 e2 \u2192\n \u03941 ## \u03942 \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u03c4) ~> d1 :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> (d1 \u27e8 \u03c41' \u21d2 \u03c42 ==> \u03c4 \u27e9) \u2218 (d2 \u27e8 \u03c42' \u21d2 \u03c42 \u27e9) \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , Id \u0393 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0394 ## (\u25a0 (u , \u0393 , \u2987\u2988)) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , Id \u0393 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u27e8 \u03c4' \u21d2 \u03c4 \u27e9 \u22a3 \u0394\n\n -- analysis\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c4 \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c4 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 e \u2260 \u2987\u2988[ u ]) \u2192\n ((e' : hexp) (u : Nat) \u2192 e \u2260 \u2987 e' \u2988[ u ]) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , Id \u0393 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0394 ## (\u25a0 (u , \u0393 , \u03c4)) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , Id \u0393 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n -- ground types\n data _ground : (\u03c4 : htyp) \u2192 Set where\n GBase : b ground\n GHole : \u2987\u2988 ==> \u2987\u2988 ground\n\n mutual\n -- todo: clean up above, dom?\n data _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 env \u2192 tctx \u2192 Set where\n STAId : \u2200{\u0393 \u0393' \u0394} \u2192\n ((x : Nat) (\u03c4 : htyp) \u2192 (x , \u03c4) \u2208 \u0393' \u2192 (x , \u03c4) \u2208 \u0393) \u2192\n \u0394 , \u0393 \u22a2 Id \u0393' :s: \u0393'\n STASubst : \u2200{\u0393 \u0394 \u03c3 y \u0393' d \u03c4 } \u2192\n \u0394 , \u0393 ,, (y , \u03c4) \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 Subst d y \u03c3 :s: \u0393'\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n x # \u0393 \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 ==> \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 } \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c41 \u03c42} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u03c41 ~ \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 :: \u03c42\n TAFailedCast : \u2200{\u0394 \u0393 d \u03c41 \u03c42} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 :: \u03c42\n\n -- substitution\n --\n -- todo: if substitution lemma is hard to prove, maybe get a premise that\n -- it's final; analagous to \"value substitution\". or define it\n -- judgementally instead of as a function.\n [_\/_]_ : dhexp \u2192 Nat \u2192 dhexp \u2192 dhexp\n [ d \/ y ] c = c\n [ d \/ y ] X x\n with natEQ x y\n [ d \/ y ] X .y | Inl refl = d\n [ d \/ y ] X x | Inr neq = X x\n [ d \/ y ] (\u00b7\u03bb x [ x\u2081 ] d')\n with natEQ x y\n [ d \/ y ] (\u00b7\u03bb .y [ \u03c4 ] d') | Inl refl = \u00b7\u03bb y [ \u03c4 ] d'\n [ d \/ y ] (\u00b7\u03bb x [ \u03c4 ] d') | Inr x\u2081 = \u00b7\u03bb x [ \u03c4 ] ( [ d \/ y ] d')\n [ d \/ y ] \u2987\u2988\u27e8 u , \u03c3 \u27e9 = \u2987\u2988\u27e8 u , Subst d y \u03c3 \u27e9\n [ d \/ y ] \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9 = \u2987 [ d \/ y ] d' \u2988\u27e8 u , Subst d y \u03c3 \u27e9\n [ d \/ y ] (d1 \u2218 d2) = ([ d \/ y ] d1) \u2218 ([ d \/ y ] d2)\n [ d \/ y ] (d' \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 ) = ([ d \/ y ] d') \u27e8 \u03c41 \u21d2 \u03c42 \u27e9\n [ d \/ y ] (d' \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 ) = ([ d \/ y ] d') \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9\n\n -- applying an environment to an expression\n apply-env : env \u2192 dhexp \u2192 dhexp\n apply-env (Id \u0393) d = d\n apply-env (Subst d y \u03c3) d' = [ d \/ y ] ( apply-env \u03c3 d')\n\n -- values\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n -- boxed values\n data _boxedval : (d : dhexp) \u2192 Set where\n BVVal : \u2200{d} \u2192 d val \u2192 d boxedval\n BVArrCast : \u2200{ d \u03c41 \u03c42 \u03c43 \u03c44 } \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d boxedval \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 boxedval\n BVHoleCast : \u2200{ \u03c4 d } \u2192 \u03c4 ground \u2192 d boxedval \u2192 d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 boxedval\n\n mutual\n -- indeterminate forms\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192\n d1 \u2260 (d1' \u27e8(\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44)\u27e9)) \u2192\n d1 indet \u2192\n d2 final \u2192\n (d1 \u2218 d2) indet\n ICastArr : \u2200{d \u03c41 \u03c42 \u03c43 \u03c44} \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d indet \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 indet\n ICastGroundHole : \u2200{ \u03c4 d } \u2192\n \u03c4 ground \u2192\n d indet \u2192\n d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 indet\n ICastHoleGround : \u2200 { d \u03c4 } \u2192\n ((d' : dhexp) (\u03c4' : htyp) \u2192 d \u2260 (d' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9)) \u2192\n d indet \u2192\n \u03c4 ground \u2192\n d \u27e8 \u2987\u2988 \u21d2 \u03c4 \u27e9 indet\n IFailedCast : \u2200{ d \u03c41 \u03c42 } \u2192\n d final \u2192\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 indet\n\n -- final expressions\n data _final : (d : dhexp) \u2192 Set where\n FBoxed : \u2200{d} \u2192 d boxedval \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n\n -- contextual dynamics\n\n -- evaluation contexts\n data ectx : Set where\n \u2299 : ectx\n _\u2218\u2081_ : ectx \u2192 dhexp \u2192 ectx\n _\u2218\u2082_ : dhexp \u2192 ectx \u2192 ectx\n \u2987_\u2988\u27e8_\u27e9 : ectx \u2192 (Nat \u00d7 env ) \u2192 ectx\n _\u27e8_\u21d2_\u27e9 : ectx \u2192 htyp \u2192 htyp \u2192 ectx\n _\u27e8_\u21d2\u2987\u2988\u21d2\u0338_\u27e9 : ectx \u2192 htyp \u2192 htyp \u2192 ectx\n\n -- note: this judgement is redundant: in the absence of the premises in\n -- the red brackets, all syntactically well formed ectxs are valid. with\n -- finality premises, that's not true, and that would propagate through\n -- additions to the calculus. so we leave it here for clarity but note\n -- that, as written, in any use case its either trival to prove or\n -- provides no additional information\n\n --\u03b5 is an evaluation context\n data _evalctx : (\u03b5 : ectx) \u2192 Set where\n ECDot : \u2299 evalctx\n ECAp1 : \u2200{d \u03b5} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u2218\u2081 d) evalctx\n ECAp2 : \u2200{d \u03b5} \u2192\n -- d final \u2192 -- red brackets\n \u03b5 evalctx \u2192\n (d \u2218\u2082 \u03b5) evalctx\n ECNEHole : \u2200{\u03b5 u \u03c3} \u2192\n \u03b5 evalctx \u2192\n \u2987 \u03b5 \u2988\u27e8 u , \u03c3 \u27e9 evalctx\n ECCast : \u2200{ \u03b5 \u03c41 \u03c42} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) evalctx\n ECFailedCast : \u2200{ \u03b5 \u03c41 \u03c42 } \u2192\n \u03b5 evalctx \u2192\n \u03b5 \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 evalctx\n\n -- d is the result of filling the hole in \u03b5 with d'\n data _==_\u27e6_\u27e7 : (d : dhexp) (\u03b5 : ectx) (d' : dhexp) \u2192 Set where\n FHOuter : \u2200{d} \u2192 d == \u2299 \u27e6 d \u27e7\n FHAp1 : \u2200{d1 d1' d2 \u03b5} \u2192\n d1 == \u03b5 \u27e6 d1' \u27e7 \u2192\n (d1 \u2218 d2) == (\u03b5 \u2218\u2081 d2) \u27e6 d1' \u27e7\n FHAp2 : \u2200{d1 d2 d2' \u03b5} \u2192\n -- d1 final \u2192 -- red brackets\n d2 == \u03b5 \u27e6 d2' \u27e7 \u2192\n (d1 \u2218 d2) == (d1 \u2218\u2082 \u03b5) \u27e6 d2' \u27e7\n FHNEHole : \u2200{ d d' \u03b5 u \u03c3} \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u2987 d \u2988\u27e8 (u , \u03c3 ) \u27e9 == \u2987 \u03b5 \u2988\u27e8 (u , \u03c3 ) \u27e9 \u27e6 d' \u27e7\n FHCast : \u2200{ d d' \u03b5 \u03c41 \u03c42 } \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 == \u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 \u27e6 d' \u27e7\n FHFailedCast : \u2200{ d d' \u03b5 \u03c41 \u03c42} \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9) == (\u03b5 \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9) \u27e6 d' \u27e7\n\n -- matched ground types\n data _\u25b8gnd_ : htyp \u2192 htyp \u2192 Set where\n MGArr : \u2200{\u03c41 \u03c42} \u2192\n (\u03c41 ==> \u03c42) \u2260 (\u2987\u2988 ==> \u2987\u2988) \u2192\n (\u03c41 ==> \u03c42) \u25b8gnd (\u2987\u2988 ==> \u2987\u2988)\n\n -- instruction transition judgement\n data _\u2192>_ : (d d' : dhexp) \u2192 Set where\n ITLam : \u2200{ x \u03c4 d1 d2 } \u2192\n -- d2 final \u2192 -- red brackets\n ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u2192> ([ d2 \/ x ] d1)\n ITCastID : \u2200{d \u03c4 } \u2192\n -- d final \u2192 -- red brackets\n (d \u27e8 \u03c4 \u21d2 \u03c4 \u27e9) \u2192> d\n ITCastSucceed : \u2200{d \u03c4 } \u2192\n -- d final \u2192 -- red brackets\n \u03c4 ground \u2192\n (d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u21d2 \u03c4 \u27e9) \u2192> d\n ITCastFail : \u2200{ d \u03c41 \u03c42} \u2192\n -- d final \u2192 -- red brackets\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n (d \u27e8 \u03c41 \u21d2 \u2987\u2988 \u21d2 \u03c42 \u27e9) \u2192> (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n ITApCast : \u2200{d1 d2 \u03c41 \u03c42 \u03c41' \u03c42' } \u2192\n -- d1 final \u2192 -- red brackets\n -- d2 final \u2192 -- red brackets\n ((d1 \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c41' ==> \u03c42')\u27e9) \u2218 d2) \u2192> ((d1 \u2218 (d2 \u27e8 \u03c41' \u21d2 \u03c41 \u27e9)) \u27e8 \u03c42 \u21d2 \u03c42' \u27e9)\n ITGround : \u2200{ d \u03c4 \u03c4'} \u2192\n -- d final \u2192 -- red brackets\n \u03c4 \u25b8gnd \u03c4' \u2192\n (d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9) \u2192> (d \u27e8 \u03c4 \u21d2 \u03c4' \u21d2 \u2987\u2988 \u27e9)\n ITExpand : \u2200{d \u03c4 \u03c4' } \u2192\n -- d final \u2192 -- red brackets\n \u03c4 \u25b8gnd \u03c4' \u2192\n (d \u27e8 \u2987\u2988 \u21d2 \u03c4 \u27e9) \u2192> (d \u27e8 \u2987\u2988 \u21d2 \u03c4' \u21d2 \u03c4 \u27e9)\n\n -- single step (in contextual evaluation sense)\n data _\u21a6_ : (d d' : dhexp) \u2192 Set where\n Step : \u2200{ d d0 d' d0' \u03b5} \u2192\n d == \u03b5 \u27e6 d0 \u27e7 \u2192\n d0 \u2192> d0' \u2192\n d' == \u03b5 \u27e6 d0' \u27e7 \u2192\n d \u21a6 d'\n\n -- reflexive transitive closure of single steps into multi steps\n data _\u21a6*_ : (d d' : dhexp) \u2192 Set where\n MSRefl : \u2200{d} \u2192 d \u21a6* d\n MSStep : \u2200{d d' d''} \u2192\n d \u21a6 d' \u2192\n d' \u21a6* d'' \u2192\n d \u21a6* d''\n\n -- freshness\n mutual\n -- ... with respect to a hole context\n data envfresh : Nat \u2192 env \u2192 Set where\n EFId : \u2200{x \u0393} \u2192 x # \u0393 \u2192 envfresh x (Id \u0393)\n EFSubst : \u2200{x d \u03c3 y} \u2192 fresh x d\n \u2192 envfresh x \u03c3\n \u2192 x \u2260 y\n \u2192 envfresh x (Subst d y \u03c3)\n\n -- ... for inernal expressions\n data fresh : Nat \u2192 dhexp \u2192 Set where\n FConst : \u2200{x} \u2192 fresh x c\n FVar : \u2200{x y} \u2192 x \u2260 y \u2192 fresh x (X y)\n FLam : \u2200{x y \u03c4 d} \u2192 x \u2260 y \u2192 fresh x d \u2192 fresh x (\u00b7\u03bb y [ \u03c4 ] d)\n FHole : \u2200{x u \u03c3} \u2192 envfresh x \u03c3 \u2192 fresh x (\u2987\u2988\u27e8 u , \u03c3 \u27e9)\n FNEHole : \u2200{x d u \u03c3} \u2192 envfresh x \u03c3 \u2192 fresh x d \u2192 fresh x (\u2987 d \u2988\u27e8 u , \u03c3 \u27e9)\n FAp : \u2200{x d1 d2} \u2192 fresh x d1 \u2192 fresh x d2 \u2192 fresh x (d1 \u2218 d2)\n FCast : \u2200{x d \u03c41 \u03c42} \u2192 fresh x d \u2192 fresh x (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9)\n FFailedCast : \u2200{x d \u03c41 \u03c42} \u2192 fresh x d \u2192 fresh x (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n\n -- ... for external expressions\n data freshh : Nat \u2192 hexp \u2192 Set where\n FRHConst : \u2200{x} \u2192 freshh x c\n FRHAsc : \u2200{x e \u03c4} \u2192 freshh x e \u2192 freshh x (e \u00b7: \u03c4)\n FRHVar : \u2200{x y} \u2192 x \u2260 y \u2192 freshh x (X y)\n FRHLam1 : \u2200{x y e} \u2192 x \u2260 y \u2192 freshh x e \u2192 freshh x (\u00b7\u03bb y e)\n FRHLam2 : \u2200{x \u03c4 e y} \u2192 x \u2260 y \u2192 freshh x e \u2192 freshh x (\u00b7\u03bb y [ \u03c4 ] e)\n FRHEHole : \u2200{x u} \u2192 freshh x (\u2987\u2988[ u ])\n FRHNEHole : \u2200{x u e} \u2192 freshh x e \u2192 freshh x (\u2987 e \u2988[ u ])\n FRHAp : \u2200{x e1 e2} \u2192 freshh x e1 \u2192 freshh x e2 \u2192 freshh x (e1 \u2218 e2)\n","old_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n -- the type of type contexts, i.e. \u0393s in the judegments below\n tctx : Set\n tctx = htyp ctx\n\n mutual\n data env : Set where\n Id : (\u0393 : tctx) \u2192 env\n Subst : (d : dhexp) \u2192 (y : Nat) \u2192 env \u2192 env\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 env) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 env) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n _\u27e8_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 dhexp\n _\u27e8_\u21d2\u2987\u2988\u21d2\u0338_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 dhexp\n\n -- convenient notation for chaining together two agreeable casts\n _\u27e8_\u21d2_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 htyp \u2192 dhexp\n d \u27e8 t1 \u21d2 t2 \u21d2 t3 \u27e9 = d \u27e8 t1 \u21d2 t2 \u27e9 \u27e8 t2 \u21d2 t3 \u27e9\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- the type of hole contexts, i.e. \u0394s in the judgements\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n -- notation for a triple to match the CMTT syntax\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 (tctx \u00d7 htyp))\n u ::[ \u0393 ] \u03c4 = u , (\u0393 , \u03c4)\n\n -- the hole name u does not appear in the term e\n data hole-name-new : (e : hexp) (u : Nat) \u2192 Set where\n HNConst : \u2200{u} \u2192 hole-name-new c u\n HNAsc : \u2200{e \u03c4 u} \u2192\n hole-name-new e u \u2192\n hole-name-new (e \u00b7: \u03c4) u\n HNVar : \u2200{x u} \u2192 hole-name-new (X x) u\n HNLam1 : \u2200{x e u} \u2192\n hole-name-new e u \u2192\n hole-name-new (\u00b7\u03bb x e) u\n HNLam2 : \u2200{x e u \u03c4} \u2192\n hole-name-new e u \u2192\n hole-name-new (\u00b7\u03bb x [ \u03c4 ] e) u\n HNHole : \u2200{u u'} \u2192\n u' \u2260 u \u2192\n hole-name-new (\u2987\u2988[ u' ]) u\n HNNEHole : \u2200{u u' e} \u2192\n u' \u2260 u \u2192\n hole-name-new e u \u2192\n hole-name-new (\u2987 e \u2988[ u' ]) u\n HNAp : \u2200{ u e1 e2 } \u2192\n hole-name-new e1 u \u2192\n hole-name-new e2 u \u2192\n hole-name-new (e1 \u2218 e2) u\n\n -- two terms that do not share any hole names\n data holes-disjoint : (e1 : hexp) \u2192 (e2 : hexp) \u2192 Set where\n HDConst : \u2200{e} \u2192 holes-disjoint c e\n HDAsc : \u2200{e1 e2 \u03c4} \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint (e1 \u00b7: \u03c4) e2\n HDVar : \u2200{x e} \u2192 holes-disjoint (X x) e\n HDLam1 : \u2200{x e1 e2} \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint (\u00b7\u03bb x e1) e2\n HDLam2 : \u2200{x e1 e2 \u03c4} \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint (\u00b7\u03bb x [ \u03c4 ] e1) e2\n HDHole : \u2200{u e2} \u2192 hole-name-new e2 u \u2192 holes-disjoint (\u2987\u2988[ u ]) e2\n HDNEHole : \u2200{u e1 e2} \u2192 hole-name-new e2 u \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint (\u2987 e1 \u2988[ u ]) e2\n HDAp : \u2200{e1 e2 e3} \u2192 holes-disjoint e1 e3 \u2192 holes-disjoint e2 e3 \u2192 holes-disjoint (e1 \u2218 e2) e3\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {x : Nat} \u2192\n (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n holes-disjoint e1 e2 \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- those types without holes\n data _tcomplete : htyp \u2192 Set where\n TCBase : b tcomplete\n TCArr : \u2200{\u03c41 \u03c42} \u2192 \u03c41 tcomplete \u2192 \u03c42 tcomplete \u2192 (\u03c41 ==> \u03c42) tcomplete\n\n -- those external expressions without holes\n data _ecomplete : hexp \u2192 Set where\n ECConst : c ecomplete\n ECAsc : \u2200{\u03c4 e} \u2192 \u03c4 tcomplete \u2192 e ecomplete \u2192 (e \u00b7: \u03c4) ecomplete\n ECVar : \u2200{x} \u2192 (X x) ecomplete\n ECLam1 : \u2200{x e} \u2192 e ecomplete \u2192 (\u00b7\u03bb x e) ecomplete\n ECLam2 : \u2200{x e \u03c4} \u2192 e ecomplete \u2192 \u03c4 tcomplete \u2192 (\u00b7\u03bb x [ \u03c4 ] e) ecomplete\n ECAp : \u2200{e1 e2} \u2192 e1 ecomplete \u2192 e2 ecomplete \u2192 (e1 \u2218 e2) ecomplete\n\n -- those internal expressions without holes\n data _dcomplete : dhexp \u2192 Set where\n DCVar : \u2200{x} \u2192 (X x) dcomplete\n DCConst : c dcomplete\n DCLam : \u2200{x \u03c4 d} \u2192 d dcomplete \u2192 \u03c4 tcomplete \u2192 (\u00b7\u03bb x [ \u03c4 ] d) dcomplete\n DCAp : \u2200{d1 d2} \u2192 d1 dcomplete \u2192 d2 dcomplete \u2192 (d1 \u2218 d2) dcomplete\n DCCast : \u2200{d \u03c41 \u03c42} \u2192 d dcomplete \u2192 \u03c41 tcomplete \u2192 \u03c42 tcomplete \u2192 (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) dcomplete\n\n -- contexts that only produce complete types\n _gcomplete : tctx \u2192 Set\n \u0393 gcomplete = (x : Nat) (\u03c4 : htyp) \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u03c4 tcomplete\n\n -- those internal expressions where every cast is the identity cast and\n -- there are no failed casts\n data cast-id : dhexp \u2192 Set where\n CIConst : cast-id c\n CIVar : \u2200{x} \u2192 cast-id (X x)\n CILam : \u2200{x \u03c4 d} \u2192 cast-id d \u2192 cast-id (\u00b7\u03bb x [ \u03c4 ] d)\n CIHole : \u2200{u} \u2192 cast-id (\u2987\u2988\u27e8 u \u27e9)\n CINEHole : \u2200{d u} \u2192 cast-id d \u2192 cast-id (\u2987 d \u2988\u27e8 u \u27e9)\n CIAp : \u2200{d1 d2} \u2192 cast-id d1 \u2192 cast-id d2 \u2192 cast-id (d1 \u2218 d2)\n CICast : \u2200{d \u03c4} \u2192 cast-id d \u2192 cast-id (d \u27e8 \u03c4 \u21d2 \u03c4 \u27e9)\n\n -- expansion\n mutual\n -- synthesis\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp : \u2200{\u0393 e1 \u03c4 \u03c41 \u03c41' \u03c42 \u03c42' d1 \u03941 e2 d2 \u03942 } \u2192\n holes-disjoint e1 e2 \u2192\n \u03941 ## \u03942 \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u03c4) ~> d1 :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> (d1 \u27e8 \u03c41' \u21d2 \u03c42 ==> \u03c4 \u27e9) \u2218 (d2 \u27e8 \u03c42' \u21d2 \u03c42 \u27e9) \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , Id \u0393 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0394 ## (\u25a0 (u , \u0393 , \u2987\u2988)) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , Id \u0393 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u27e8 \u03c4' \u21d2 \u03c4 \u27e9 \u22a3 \u0394\n\n -- analysis\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c4 \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c4 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 e \u2260 \u2987\u2988[ u ]) \u2192\n ((e' : hexp) (u : Nat) \u2192 e \u2260 \u2987 e' \u2988[ u ]) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , Id \u0393 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0394 ## (\u25a0 (u , \u0393 , \u03c4)) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , Id \u0393 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n -- ground types\n data _ground : (\u03c4 : htyp) \u2192 Set where\n GBase : b ground\n GHole : \u2987\u2988 ==> \u2987\u2988 ground\n\n mutual\n -- todo: clean up above, dom?\n data _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 env \u2192 tctx \u2192 Set where\n STAId : \u2200{\u0393 \u0393' \u0394} \u2192\n ((x : Nat) (\u03c4 : htyp) \u2192 (x , \u03c4) \u2208 \u0393' \u2192 (x , \u03c4) \u2208 \u0393) \u2192\n \u0394 , \u0393 \u22a2 Id \u0393' :s: \u0393'\n STASubst : \u2200{\u0393 \u0394 \u03c3 y \u0393' d \u03c4 } \u2192\n \u0394 , \u0393 ,, (y , \u03c4) \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 Subst d y \u03c3 :s: \u0393'\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n x # \u0393 \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 ==> \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 } \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c41 \u03c42} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u03c41 ~ \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 :: \u03c42\n TAFailedCast : \u2200{\u0394 \u0393 d \u03c41 \u03c42} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 :: \u03c42\n\n -- substitution\n --\n -- todo: if substitution lemma is hard to prove, maybe get a premise that\n -- it's final; analagous to \"value substitution\". or define it\n -- judgementally instead of as a function.\n [_\/_]_ : dhexp \u2192 Nat \u2192 dhexp \u2192 dhexp\n [ d \/ y ] c = c\n [ d \/ y ] X x\n with natEQ x y\n [ d \/ y ] X .y | Inl refl = d\n [ d \/ y ] X x | Inr neq = X x\n [ d \/ y ] (\u00b7\u03bb x [ x\u2081 ] d')\n with natEQ x y\n [ d \/ y ] (\u00b7\u03bb .y [ \u03c4 ] d') | Inl refl = \u00b7\u03bb y [ \u03c4 ] d'\n [ d \/ y ] (\u00b7\u03bb x [ \u03c4 ] d') | Inr x\u2081 = \u00b7\u03bb x [ \u03c4 ] ( [ d \/ y ] d')\n [ d \/ y ] \u2987\u2988\u27e8 u , \u03c3 \u27e9 = \u2987\u2988\u27e8 u , Subst d y \u03c3 \u27e9\n [ d \/ y ] \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9 = \u2987 [ d \/ y ] d' \u2988\u27e8 u , Subst d y \u03c3 \u27e9\n [ d \/ y ] (d1 \u2218 d2) = ([ d \/ y ] d1) \u2218 ([ d \/ y ] d2)\n [ d \/ y ] (d' \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 ) = ([ d \/ y ] d') \u27e8 \u03c41 \u21d2 \u03c42 \u27e9\n [ d \/ y ] (d' \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 ) = ([ d \/ y ] d') \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9\n\n -- applying an environment to an expression\n apply-env : env \u2192 dhexp \u2192 dhexp\n apply-env (Id \u0393) d = d\n apply-env (Subst d y \u03c3) d' = [ d \/ y ] ( apply-env \u03c3 d')\n\n -- values\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n -- boxed values\n data _boxedval : (d : dhexp) \u2192 Set where\n BVVal : \u2200{d} \u2192 d val \u2192 d boxedval\n BVArrCast : \u2200{ d \u03c41 \u03c42 \u03c43 \u03c44 } \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d boxedval \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 boxedval\n BVHoleCast : \u2200{ \u03c4 d } \u2192 \u03c4 ground \u2192 d boxedval \u2192 d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 boxedval\n\n mutual\n -- indeterminate forms\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192\n d1 \u2260 (d1' \u27e8(\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44)\u27e9)) \u2192\n d1 indet \u2192\n d2 final \u2192\n (d1 \u2218 d2) indet\n ICastArr : \u2200{d \u03c41 \u03c42 \u03c43 \u03c44} \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d indet \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 indet\n ICastGroundHole : \u2200{ \u03c4 d } \u2192\n \u03c4 ground \u2192\n d indet \u2192\n d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 indet\n ICastHoleGround : \u2200 { d \u03c4 } \u2192\n ((d' : dhexp) (\u03c4' : htyp) \u2192 d \u2260 (d' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9)) \u2192\n d indet \u2192\n \u03c4 ground \u2192\n d \u27e8 \u2987\u2988 \u21d2 \u03c4 \u27e9 indet\n IFailedCast : \u2200{ d \u03c41 \u03c42 } \u2192\n d final \u2192\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 indet\n\n -- final expressions\n data _final : (d : dhexp) \u2192 Set where\n FBoxed : \u2200{d} \u2192 d boxedval \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n\n -- contextual dynamics\n\n -- evaluation contexts\n data ectx : Set where\n \u2299 : ectx\n _\u2218\u2081_ : ectx \u2192 dhexp \u2192 ectx\n _\u2218\u2082_ : dhexp \u2192 ectx \u2192 ectx\n \u2987_\u2988\u27e8_\u27e9 : ectx \u2192 (Nat \u00d7 env ) \u2192 ectx\n _\u27e8_\u21d2_\u27e9 : ectx \u2192 htyp \u2192 htyp \u2192 ectx\n _\u27e8_\u21d2\u2987\u2988\u21d2\u0338_\u27e9 : ectx \u2192 htyp \u2192 htyp \u2192 ectx\n\n -- note: this judgement is redundant: in the absence of the premises in\n -- the red brackets, all syntactically well formed ectxs are valid. with\n -- finality premises, that's not true, and that would propagate through\n -- additions to the calculus. so we leave it here for clarity but note\n -- that, as written, in any use case its either trival to prove or\n -- provides no additional information\n\n --\u03b5 is an evaluation context\n data _evalctx : (\u03b5 : ectx) \u2192 Set where\n ECDot : \u2299 evalctx\n ECAp1 : \u2200{d \u03b5} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u2218\u2081 d) evalctx\n ECAp2 : \u2200{d \u03b5} \u2192\n -- d final \u2192 -- red brackets\n \u03b5 evalctx \u2192\n (d \u2218\u2082 \u03b5) evalctx\n ECNEHole : \u2200{\u03b5 u \u03c3} \u2192\n \u03b5 evalctx \u2192\n \u2987 \u03b5 \u2988\u27e8 u , \u03c3 \u27e9 evalctx\n ECCast : \u2200{ \u03b5 \u03c41 \u03c42} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) evalctx\n ECFailedCast : \u2200{ \u03b5 \u03c41 \u03c42 } \u2192\n \u03b5 evalctx \u2192\n \u03b5 \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 evalctx\n\n -- d is the result of filling the hole in \u03b5 with d'\n data _==_\u27e6_\u27e7 : (d : dhexp) (\u03b5 : ectx) (d' : dhexp) \u2192 Set where\n FHOuter : \u2200{d} \u2192 d == \u2299 \u27e6 d \u27e7\n FHAp1 : \u2200{d1 d1' d2 \u03b5} \u2192\n d1 == \u03b5 \u27e6 d1' \u27e7 \u2192\n (d1 \u2218 d2) == (\u03b5 \u2218\u2081 d2) \u27e6 d1' \u27e7\n FHAp2 : \u2200{d1 d2 d2' \u03b5} \u2192\n -- d1 final \u2192 -- red brackets\n d2 == \u03b5 \u27e6 d2' \u27e7 \u2192\n (d1 \u2218 d2) == (d1 \u2218\u2082 \u03b5) \u27e6 d2' \u27e7\n FHNEHole : \u2200{ d d' \u03b5 u \u03c3} \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u2987 d \u2988\u27e8 (u , \u03c3 ) \u27e9 == \u2987 \u03b5 \u2988\u27e8 (u , \u03c3 ) \u27e9 \u27e6 d' \u27e7\n FHCast : \u2200{ d d' \u03b5 \u03c41 \u03c42 } \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 == \u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 \u27e6 d' \u27e7\n FHFailedCast : \u2200{ d d' \u03b5 \u03c41 \u03c42} \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9) == (\u03b5 \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9) \u27e6 d' \u27e7\n\n -- matched ground types\n data _\u25b8gnd_ : htyp \u2192 htyp \u2192 Set where\n MGArr : \u2200{\u03c41 \u03c42} \u2192\n (\u03c41 ==> \u03c42) \u2260 (\u2987\u2988 ==> \u2987\u2988) \u2192\n (\u03c41 ==> \u03c42) \u25b8gnd (\u2987\u2988 ==> \u2987\u2988)\n\n -- instruction transition judgement\n data _\u2192>_ : (d d' : dhexp) \u2192 Set where\n ITLam : \u2200{ x \u03c4 d1 d2 } \u2192\n -- d2 final \u2192 -- red brackets\n ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u2192> ([ d2 \/ x ] d1)\n ITCastID : \u2200{d \u03c4 } \u2192\n -- d final \u2192 -- red brackets\n (d \u27e8 \u03c4 \u21d2 \u03c4 \u27e9) \u2192> d\n ITCastSucceed : \u2200{d \u03c4 } \u2192\n -- d final \u2192 -- red brackets\n \u03c4 ground \u2192\n (d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u21d2 \u03c4 \u27e9) \u2192> d\n ITCastFail : \u2200{ d \u03c41 \u03c42} \u2192\n -- d final \u2192 -- red brackets\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n (d \u27e8 \u03c41 \u21d2 \u2987\u2988 \u21d2 \u03c42 \u27e9) \u2192> (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n ITApCast : \u2200{d1 d2 \u03c41 \u03c42 \u03c41' \u03c42' } \u2192\n -- d1 final \u2192 -- red brackets\n -- d2 final \u2192 -- red brackets\n ((d1 \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c41' ==> \u03c42')\u27e9) \u2218 d2) \u2192> ((d1 \u2218 (d2 \u27e8 \u03c41' \u21d2 \u03c41 \u27e9)) \u27e8 \u03c42 \u21d2 \u03c42' \u27e9)\n ITGround : \u2200{ d \u03c4 \u03c4'} \u2192\n -- d final \u2192 -- red brackets\n \u03c4 \u25b8gnd \u03c4' \u2192\n (d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9) \u2192> (d \u27e8 \u03c4 \u21d2 \u03c4' \u21d2 \u2987\u2988 \u27e9)\n ITExpand : \u2200{d \u03c4 \u03c4' } \u2192\n -- d final \u2192 -- red brackets\n \u03c4 \u25b8gnd \u03c4' \u2192\n (d \u27e8 \u2987\u2988 \u21d2 \u03c4 \u27e9) \u2192> (d \u27e8 \u2987\u2988 \u21d2 \u03c4' \u21d2 \u03c4 \u27e9)\n\n -- single step (in contextual evaluation sense)\n data _\u21a6_ : (d d' : dhexp) \u2192 Set where\n Step : \u2200{ d d0 d' d0' \u03b5} \u2192\n d == \u03b5 \u27e6 d0 \u27e7 \u2192\n d0 \u2192> d0' \u2192\n d' == \u03b5 \u27e6 d0' \u27e7 \u2192\n d \u21a6 d'\n\n -- reflexive transitive closure of single steps into multi steps\n data _\u21a6*_ : (d d' : dhexp) \u2192 Set where\n MSRefl : \u2200{d} \u2192 d \u21a6* d\n MSStep : \u2200{d d' d''} \u2192\n d \u21a6 d' \u2192\n d' \u21a6* d'' \u2192\n d \u21a6* d''\n\n -- freshness\n mutual\n -- ... with respect to a hole context\n data envfresh : Nat \u2192 env \u2192 Set where\n EFId : \u2200{x \u0393} \u2192 x # \u0393 \u2192 envfresh x (Id \u0393)\n EFSubst : \u2200{x d \u03c3 y} \u2192 fresh x d\n \u2192 envfresh x \u03c3\n \u2192 x \u2260 y\n \u2192 envfresh x (Subst d y \u03c3)\n\n -- ... for inernal expressions\n data fresh : Nat \u2192 dhexp \u2192 Set where\n FConst : \u2200{x} \u2192 fresh x c\n FVar : \u2200{x y} \u2192 x \u2260 y \u2192 fresh x (X y)\n FLam : \u2200{x y \u03c4 d} \u2192 x \u2260 y \u2192 fresh x d \u2192 fresh x (\u00b7\u03bb y [ \u03c4 ] d)\n FHole : \u2200{x u \u03c3} \u2192 envfresh x \u03c3 \u2192 fresh x (\u2987\u2988\u27e8 u , \u03c3 \u27e9)\n FNEHole : \u2200{x d u \u03c3} \u2192 envfresh x \u03c3 \u2192 fresh x d \u2192 fresh x (\u2987 d \u2988\u27e8 u , \u03c3 \u27e9)\n FAp : \u2200{x d1 d2} \u2192 fresh x d1 \u2192 fresh x d2 \u2192 fresh x (d1 \u2218 d2)\n FCast : \u2200{x d \u03c41 \u03c42} \u2192 fresh x d \u2192 fresh x (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9)\n FFailedCast : \u2200{x d \u03c41 \u03c42} \u2192 fresh x d \u2192 fresh x (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n\n -- ... for external expressions\n data freshh : Nat \u2192 hexp \u2192 Set where\n FRHConst : \u2200{x} \u2192 freshh x c\n FRHAsc : \u2200{x e \u03c4} \u2192 freshh x e \u2192 freshh x (e \u00b7: \u03c4)\n FRHVar : \u2200{x y} \u2192 x \u2260 y \u2192 freshh x (X y)\n FRHLam1 : \u2200{x y e} \u2192 x \u2260 y \u2192 freshh x e \u2192 freshh x (\u00b7\u03bb y e)\n FRHLam2 : \u2200{x \u03c4 e y} \u2192 x \u2260 y \u2192 freshh x e \u2192 freshh x (\u00b7\u03bb y [ \u03c4 ] e)\n FRHEHole : \u2200{x u} \u2192 freshh x (\u2987\u2988[ u ])\n FRHNEHole : \u2200{x u e} \u2192 freshh x e \u2192 freshh x (\u2987 e \u2988[ u ])\n FRHAp : \u2200{x e1 e2} \u2192 freshh x e1 \u2192 freshh x e2 \u2192 freshh x (e1 \u2218 e2)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"4d4cc54595eef42ea3f87392acc4712358dc360b","subject":"SynGrp: updated but not used anymore so far","message":"SynGrp: updated but not used anymore so far\n","repos":"crypto-agda\/crypto-agda","old_file":"SynGrp.agda","new_file":"SynGrp.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Type.Eq\nopen import Function.Base\nopen import Data.Product.NP renaming (map to <_\u00d7_>)\nopen import FFI.JS.BigI\nopen import Crypto.JS.BigI.CyclicGroup\nopen import Crypto.JS.BigI.FiniteField\n hiding (_*_; _^I_)\nopen import Algebra.Group\nopen import Algebra.Group.Constructions\nopen import Algebra.Group.Homomorphism\nopen import Relation.Binary.PropositionalEquality\n\nmodule SynGrp where\n\ndata SynGrp : Set where\n `\u2124[_]+ : (q : BigI) \u2192 SynGrp\n `\u2124[_]\u2605 : (p : BigI) \u2192 SynGrp\n _`\u00d7_ : (\ud835\udd38 \ud835\udd39 : SynGrp) \u2192 SynGrp\n\nElGrp : SynGrp \u2192 Set\nElGrp `\u2124[ q ]+ = \u2124[ q ]\nElGrp `\u2124[ p ]\u2605 = \u2124[ p ]\u2605\nElGrp (`\ud835\udd38 `\u00d7 `\ud835\udd39) = ElGrp `\ud835\udd38 \u00d7 ElGrp `\ud835\udd39\n\nEl\ud835\udd3erp : \u2200 `\ud835\udd3e \u2192 Group (ElGrp `\ud835\udd3e)\nEl\ud835\udd3erp `\u2124[ q ]+ = \u2124[ q ]+-grp\nEl\ud835\udd3erp `\u2124[ p ]\u2605 = \u2124[ p ]\u2605-grp\nEl\ud835\udd3erp (`\ud835\udd38 `\u00d7 `\ud835\udd39) = Product.\u00d7-grp (El\ud835\udd3erp `\ud835\udd38) (El\ud835\udd3erp `\ud835\udd39)\n\nexp-\u00d7 : \u2200 {a b c}{A : Set a}{B : Set b}{C : Set c}\n (expA : A \u2192 C \u2192 A)\n (expB : B \u2192 C \u2192 B)\n \u2192 A \u00d7 B \u2192 C \u2192 A \u00d7 B\nexp-\u00d7 expA expB (b0 , b1) e = expA b0 e , expB b1 e\n\n-- This iterate the group operation of \ud835\udd38 based on the given\n-- BigI value. Calling this operations exp(onential) makes\n-- sense when the group is \u2124p\u2605, but for \u2124q+ this corresponds\n-- to multiplication.\nexpI : \u2200 \ud835\udd38 \u2192 ElGrp \ud835\udd38 \u2192 BigI \u2192 ElGrp \ud835\udd38\nexpI `\u2124[ q ]+ = _\u2297I_ _\nexpI `\u2124[ p ]\u2605 = _^I_ _\nexpI (\ud835\udd38 `\u00d7 \ud835\udd39) = exp-\u00d7 (expI \ud835\udd38) (expI \ud835\udd39)\n\n-- See the remark on expI\nexp : \u2200 {q} \ud835\udd38 \u2192 ElGrp \ud835\udd38 \u2192 \u2124[ q ] \u2192 ElGrp \ud835\udd38\nexp \ud835\udd38 b e = expI \ud835\udd38 b (\u2124q\u25b9BigI _ e)\n\nmodule _ {q q'} where\n\n _*_ : \u2124[ q ] \u2192 \u2124[ q' ] \u2192 \u2124[ q ]\n x * y = _\u2297I_ _ x (\u2124q\u25b9BigI _ y)\n\n -- TODO check on the assumptions on q,q'\n -- x * (y +{q'} z) = x * y +{q} x * z\n -- mod (x * (mod (y + z) q')) q = ((x * y) mod q + (x * z) mod q) mod q\n postulate *-hom : \u2200 x \u2192 GroupHomomorphism \u2124[ q' ]+-grp \u2124[ q ]+-grp (_*_ x)\n\nopen module ^-hom-p-q {p q} = ^-hom p {q}\n\nexp-hom : \u2200 {q} \ud835\udd38 (b : ElGrp \ud835\udd38)\n \u2192 GroupHomomorphism \u2124[ q ]+-grp (El\ud835\udd3erp \ud835\udd38) (exp \ud835\udd38 b)\nexp-hom `\u2124[ q ]+ b = *-hom b\nexp-hom `\u2124[ p ]\u2605 b = ^-hom b\nexp-hom (\ud835\udd38 `\u00d7 \ud835\udd39) (b0 , b1) = < exp-hom \ud835\udd38 b0 , exp-hom \ud835\udd39 b1 >-hom\n\ndata SynHom : (\ud835\udd38 \ud835\udd39 : SynGrp) \u2192 Set where\n `id : \u2200{\ud835\udd38} \u2192 SynHom \ud835\udd38 \ud835\udd38 \n _`\u2218_ : \u2200{\ud835\udd38 \ud835\udd39 \u2102}(f : SynHom \ud835\udd39 \u2102)(g : SynHom \ud835\udd38 \ud835\udd39) \u2192 SynHom \ud835\udd38 \u2102\n `<_\u00d7_> : \u2200{\ud835\udd38\u2080 \ud835\udd38\u2081 \ud835\udd39\u2080 \ud835\udd39\u2081}\n (f\u2080 : SynHom \ud835\udd38\u2080 \ud835\udd39\u2080)(f\u2081 : SynHom \ud835\udd38\u2081 \ud835\udd39\u2081)\n \u2192 SynHom (\ud835\udd38\u2080 `\u00d7 \ud835\udd38\u2081) (\ud835\udd39\u2080 `\u00d7 \ud835\udd39\u2081)\n `\u0394 : \u2200{\ud835\udd38} \u2192 SynHom \ud835\udd38 (\ud835\udd38 `\u00d7 \ud835\udd38) \n _`^_ : \u2200 {q \ud835\udd38} \u2192 ElGrp \ud835\udd38 \u2192 SynHom `\u2124[ q ]+ \ud835\udd38\n\n`<_,_> : \u2200{\ud835\udd38 \ud835\udd39\u2080 \ud835\udd39\u2081}\n (f\u2080 : SynHom \ud835\udd38 \ud835\udd39\u2080)\n (f\u2081 : SynHom \ud835\udd38 \ud835\udd39\u2081)\n \u2192 SynHom \ud835\udd38 (\ud835\udd39\u2080 `\u00d7 \ud835\udd39\u2081)\n`< f\u2080 , f\u2081 > = `< f\u2080 \u00d7 f\u2081 > `\u2218 `\u0394\n\nElHom : \u2200{\ud835\udd38 \ud835\udd39 : SynGrp} \u2192 SynHom \ud835\udd38 \ud835\udd39 \u2192 ElGrp \ud835\udd38 \u2192 ElGrp \ud835\udd39\nElHom `id = id\nElHom (f `\u2218 g) = ElHom f \u2218 ElHom g\nElHom `< f \u00d7 g > = < ElHom f \u00d7 ElHom g >\nElHom `\u0394 = \u0394\nElHom (_`^_ {\ud835\udd38 = \ud835\udd38} b) = exp \ud835\udd38 b\n\nEl\u210dom : \u2200{\ud835\udd38 \ud835\udd39 : SynGrp}(\u03c6 : SynHom \ud835\udd38 \ud835\udd39) \u2192 GroupHomomorphism (El\ud835\udd3erp \ud835\udd38) (El\ud835\udd3erp \ud835\udd39) (ElHom \u03c6)\nEl\u210dom `id = Identity.id-hom _\nEl\u210dom (\u03c6 `\u2218 \u03c8) = El\u210dom \u03c6 \u2218-hom El\u210dom \u03c8\nEl\u210dom `< \u03c6 \u00d7 \u03c8 > = < El\u210dom \u03c6 \u00d7 El\u210dom \u03c8 >-hom\nEl\u210dom `\u0394 = Delta.\u0394-hom _\nEl\u210dom (_`^_ {\ud835\udd38 = \ud835\udd38} x) = exp-hom \ud835\udd38 x\n\nSynGrp-Eq? : (\ud835\udd38 : SynGrp) \u2192 Eq? (ElGrp \ud835\udd38)\nSynGrp-Eq? `\u2124[ q ]+ = \u2124[ q ]-Eq?\nSynGrp-Eq? `\u2124[ p ]\u2605 = \u2124[ p ]\u2605-Eq?\nSynGrp-Eq? (\ud835\udd38 `\u00d7 \ud835\udd39) = \u00d7-Eq? {{SynGrp-Eq? \ud835\udd38}} {{SynGrp-Eq? \ud835\udd39}}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Type.Eq\nopen import Function.NP\nopen import Data.Product.NP renaming (map to <_\u00d7_>)\nopen import FFI.JS.BigI\nimport Crypto.JS.BigI.CyclicGroup as \ud835\udd3e\nimport Crypto.JS.BigI.FiniteField as \ud835\udd3d\nopen import Algebra.Group\nopen import Algebra.Group.Constructions\nopen import Algebra.Group.Homomorphism\n-- open import Relation.Binary.PropositionalEquality\n\nmodule SynGrp where\n\ndata SynGrp : Set where\n `\u2124[_]+ : (q : BigI) \u2192 SynGrp\n `\u2124[_]\u2605 : (p : BigI) \u2192 SynGrp\n _`\u00d7_ : (\ud835\udd38 \ud835\udd39 : SynGrp) \u2192 SynGrp\n\n\u2124[_] = \ud835\udd3d.\ud835\udd3d\n\u2124[_]\u2605 = \ud835\udd3e.\ud835\udd3e\n\n\u2124[_]+-grp = \ud835\udd3d.fld.+-grp\n\u2124[_]\u2605-grp = \ud835\udd3e.grp\n\nElGrp : SynGrp \u2192 Set\nElGrp `\u2124[ q ]+ = \u2124[ q ]\nElGrp `\u2124[ p ]\u2605 = \u2124[ p ]\u2605\nElGrp (`\ud835\udd38 `\u00d7 `\ud835\udd39) = ElGrp `\ud835\udd38 \u00d7 ElGrp `\ud835\udd39\n\nEl\ud835\udd3erp : \u2200 `\ud835\udd3e \u2192 Group (ElGrp `\ud835\udd3e)\nEl\ud835\udd3erp `\u2124[ q ]+ = \u2124[ q ]+-grp\nEl\ud835\udd3erp `\u2124[ p ]\u2605 = \u2124[ p ]\u2605-grp\nEl\ud835\udd3erp (`\ud835\udd38 `\u00d7 `\ud835\udd39) = Product.\u00d7-grp (El\ud835\udd3erp `\ud835\udd38) (El\ud835\udd3erp `\ud835\udd39)\n\n-- This iterate the group operation of \ud835\udd38 based on the given\n-- BigI value. Calling this operations exp(onential) makes\n-- sense when the group is \u2124p\u2605, but for \u2124q+ this corresponds\n-- to multiplication.\nexpI : \u2200 \ud835\udd38 \u2192 ElGrp \ud835\udd38 \u2192 BigI \u2192 ElGrp \ud835\udd38\nexpI `\u2124[ q ]+ b e = \ud835\udd3d._\u2297_ _ b e\nexpI `\u2124[ p ]\u2605 b e = \ud835\udd3e._^_ _ b e\nexpI (\ud835\udd38 `\u00d7 \ud835\udd39) (b0 , b1) e = expI \ud835\udd38 b0 e , expI \ud835\udd39 b1 e\n\n-- See the remark on expI\nexp : \u2200 {q} \ud835\udd38 \u2192 ElGrp \ud835\udd38 \u2192 \u2124[ q ] \u2192 ElGrp \ud835\udd38\nexp \ud835\udd38 b e = expI \ud835\udd38 b (\ud835\udd3d.repr _ e)\n\nmodule _ {q q'} where\n\n _*_ : \u2124[ q ] \u2192 \u2124[ q' ] \u2192 \u2124[ q ]\n x * y = \ud835\udd3d._\u2297_ _ x (\ud835\udd3d.repr _ y)\n\n -- TODO check on the assumptions on q,q'\n postulate\n *-hom : \u2200 x \u2192 GroupHomomorphism \u2124[ q' ]+-grp \u2124[ q ]+-grp (_*_ x)\n\nmodule _ {q p} where\n _^_ : \u2124[ p ]\u2605 \u2192 \u2124[ q ] \u2192 \u2124[ p ]\u2605\n b ^ e = \ud835\udd3e._^_ _ b (\ud835\udd3d.repr _ e)\n\n -- TODO check on the assumptions on p,q\n postulate\n ^-hom : \u2200 b \u2192 GroupHomomorphism \u2124[ q ]+-grp \u2124[ p ]\u2605-grp (_^_ b)\n\n -- ^-comm : \u2200 {\u03b1 x y} \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x\n\nexp-hom : \u2200 {q} \ud835\udd38 (b : ElGrp \ud835\udd38)\n \u2192 GroupHomomorphism \u2124[ q ]+-grp (El\ud835\udd3erp \ud835\udd38) (exp \ud835\udd38 b)\nexp-hom `\u2124[ q ]+ b = *-hom b\nexp-hom `\u2124[ p ]\u2605 b = ^-hom b\nexp-hom (\ud835\udd38 `\u00d7 \ud835\udd39) (b0 , b1) = < exp-hom \ud835\udd38 b0 , exp-hom \ud835\udd39 b1 >-hom\n\ndata SynHom : (\ud835\udd38 \ud835\udd39 : SynGrp) \u2192 Set where\n `id : \u2200{\ud835\udd38} \u2192 SynHom \ud835\udd38 \ud835\udd38 \n _`\u2218_ : \u2200{\ud835\udd38 \ud835\udd39 \u2102}(f : SynHom \ud835\udd39 \u2102)(g : SynHom \ud835\udd38 \ud835\udd39) \u2192 SynHom \ud835\udd38 \u2102\n `<_\u00d7_> : \u2200{\ud835\udd38\u2080 \ud835\udd38\u2081 \ud835\udd39\u2080 \ud835\udd39\u2081}\n (f\u2080 : SynHom \ud835\udd38\u2080 \ud835\udd39\u2080)(f\u2081 : SynHom \ud835\udd38\u2081 \ud835\udd39\u2081)\n \u2192 SynHom (\ud835\udd38\u2080 `\u00d7 \ud835\udd38\u2081) (\ud835\udd39\u2080 `\u00d7 \ud835\udd39\u2081)\n `\u0394 : \u2200{\ud835\udd38} \u2192 SynHom \ud835\udd38 (\ud835\udd38 `\u00d7 \ud835\udd38) \n _`^_ : \u2200 {q \ud835\udd38} \u2192 ElGrp \ud835\udd38 \u2192 SynHom `\u2124[ q ]+ \ud835\udd38\n\n`<_,_> : \u2200{\ud835\udd38 \ud835\udd39\u2080 \ud835\udd39\u2081}\n (f\u2080 : SynHom \ud835\udd38 \ud835\udd39\u2080)\n (f\u2081 : SynHom \ud835\udd38 \ud835\udd39\u2081)\n \u2192 SynHom \ud835\udd38 (\ud835\udd39\u2080 `\u00d7 \ud835\udd39\u2081)\n`< f\u2080 , f\u2081 > = `< f\u2080 \u00d7 f\u2081 > `\u2218 `\u0394\n\nElHom : \u2200{\ud835\udd38 \ud835\udd39 : SynGrp} \u2192 SynHom \ud835\udd38 \ud835\udd39 \u2192 ElGrp \ud835\udd38 \u2192 ElGrp \ud835\udd39\nElHom `id = id\nElHom (f `\u2218 g) = ElHom f \u2218 ElHom g\nElHom `< f \u00d7 g > = < ElHom f \u00d7 ElHom g >\nElHom `\u0394 = \u0394\nElHom (_`^_ {\ud835\udd38 = \ud835\udd38} b) = exp \ud835\udd38 b\n\nEl\u210dom : \u2200{\ud835\udd38 \ud835\udd39 : SynGrp}(\u03c6 : SynHom \ud835\udd38 \ud835\udd39) \u2192 GroupHomomorphism (El\ud835\udd3erp \ud835\udd38) (El\ud835\udd3erp \ud835\udd39) (ElHom \u03c6)\nEl\u210dom `id = Identity.id-hom _\nEl\u210dom (\u03c6 `\u2218 \u03c8) = El\u210dom \u03c6 \u2218-hom El\u210dom \u03c8\nEl\u210dom `< \u03c6 \u00d7 \u03c8 > = < El\u210dom \u03c6 \u00d7 El\u210dom \u03c8 >-hom\nEl\u210dom `\u0394 = Delta.\u0394-hom _\nEl\u210dom (_`^_ {\ud835\udd38 = \ud835\udd38} x) = exp-hom \ud835\udd38 x\n\nSynGrp-Eq? : (\ud835\udd38 : SynGrp) \u2192 Eq? (ElGrp \ud835\udd38)\nSynGrp-Eq? `\u2124[ q ]+ = \ud835\udd3d.\ud835\udd3d-Eq? q\nSynGrp-Eq? `\u2124[ p ]\u2605 = \ud835\udd3e.\ud835\udd3e-Eq? p\nSynGrp-Eq? (\ud835\udd38 `\u00d7 \ud835\udd39) = \u00d7-Eq? {{SynGrp-Eq? \ud835\udd38}} {{SynGrp-Eq? \ud835\udd39}}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"eff34c6c08c12aa06efeaea8b12668e9a2418ce1","subject":"Fixed a definition of Stream-coind.","message":"Fixed a definition of Stream-coind.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/FOT\/FOTC\/Data\/Stream\/StreamSL.agda","new_file":"notes\/FOT\/FOTC\/Data\/Stream\/StreamSL.agda","new_contents":"------------------------------------------------------------------------------\n-- Definition of FOTC streams using the Agda co-inductive combinators\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Data.Stream.StreamSL where\n\nopen import Data.Product renaming ( _\u00d7_ to _\u2227_ )\nopen import Coinduction\nopen import Relation.Binary.PropositionalEquality\n\n------------------------------------------------------------------------------\n\ndata D : Set where\n _\u2237_ : D \u2192 D \u2192 D\n\ndata Stream : D \u2192 Set where\n consS : \u2200 x {xs} \u2192 \u221e (Stream xs) \u2192 Stream (x \u2237 xs)\n\nStream-unf : \u2200 {xs} \u2192 Stream xs \u2192\n \u2203 \u03bb x' \u2192 \u2203 \u03bb xs' \u2192 Stream xs' \u2227 xs \u2261 x' \u2237 xs'\nStream-unf (consS x' {xs'} Sxs') = x' , xs' , \u266d Sxs' , refl\n\n{-# NO_TERMINATION_CHECK #-}\nStream-coind :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192 \u2203 \u03bb x' \u2192 \u2203 \u03bb xs' \u2192 xs \u2261 x' \u2237 xs' \u2227 A xs') \u2192\n \u2200 {xs} \u2192 A xs \u2192 Stream xs\nStream-coind A h Axs with h Axs\n... | x' , xs' , prf\u2081 , Axs' = subst Stream (sym prf\u2081) prf\u2082\n where\n prf\u2082 : Stream (x' \u2237 xs')\n prf\u2082 = consS x' (\u266f Stream-coind A h Axs')\n","old_contents":"------------------------------------------------------------------------------\n-- Definition of FOTC streams using the Agda co-inductive combinators\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Data.Stream.StreamSL where\n\nopen import Data.Product renaming ( _\u00d7_ to _\u2227_ )\nopen import Coinduction\nopen import Relation.Binary.PropositionalEquality\n\n------------------------------------------------------------------------------\n\ndata D : Set where\n _\u2237_ : D \u2192 D \u2192 D\n\ndata Stream : D \u2192 Set where\n consS : \u2200 x {xs} \u2192 \u221e (Stream xs) \u2192 Stream (x \u2237 xs)\n\nStream-unf : \u2200 {xs} \u2192 Stream xs \u2192\n \u2203 \u03bb x' \u2192 \u2203 \u03bb xs' \u2192 Stream xs' \u2227 xs \u2261 x' \u2237 xs'\nStream-unf (consS x' {xs'} Sxs') = x' , xs' , \u266d Sxs' , refl\n\n{-# NO_TERMINATION_CHECK #-}\nStream-coind : \u2200 (A : D \u2192 Set) {xs} \u2192\n -- A is post-fixed point of StreamF.\n (A xs \u2192 \u2203 \u03bb x' \u2192 \u2203 \u03bb xs' \u2192 A xs' \u2227 xs \u2261 x' \u2237 xs') \u2192\n -- Stream is greater than A.\n A xs \u2192 Stream xs\nStream-coind A {xs} h Axs = subst Stream (sym xs\u2261x'\u2237xs') prf\n where\n x' : D\n x' = proj\u2081 (h Axs)\n\n xs' : D\n xs' = proj\u2081 (proj\u2082 (h Axs))\n\n Axs' : A xs'\n Axs' = proj\u2081 (proj\u2082 (proj\u2082 (h Axs)))\n\n xs\u2261x'\u2237xs' : xs \u2261 x' \u2237 xs'\n xs\u2261x'\u2237xs' = proj\u2082 (proj\u2082 (proj\u2082 (h Axs)))\n\n prf : Stream (x' \u2237 xs')\n prf = consS x' (\u266f (Stream-coind A {!!} Axs'))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"4b68efdc89a5dc5bad6ad5ef71ae18bb262c1c86","subject":"Prove that equivalences don't equate everything (fix #23).","message":"Prove that equivalences don't equate everything (fix #23).\n\nOld-commit-hash: 9c2bf11b7532012cd196e4eecd4cc3a7b5b5586a\n","repos":"inc-lc\/ilc-agda","old_file":"total.agda","new_file":"total.agda","new_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nimport Relation.Binary as B\n\nopen import Relation.Binary using\n (IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)\nimport Relation.Binary.EqReasoning as EqR\n\nopen import Relation.Nullary using (\u00ac_)\n\nopen import meaning\n\n-- SIMPLE TYPES\n\n-- Syntax\n\ndata Type : Set where\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n bool : Type\n\ninfixr 5 _\u21d2_\n\n-- Denotational Semantics\n\ndata Bool : Set where\n true false : Bool\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 bool \u27e7Type = Bool\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\n-- Value Equivalence\n\ndata _\u2261_ : \u2200 {\u03c4} \u2192 (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192 Set where\n ext : \u2200 {\u03c4\u2081 \u03c4\u2082} {f\u2081 f\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} \u2192\n (\u2200 v \u2192 f\u2081 v \u2261 f\u2082 v) \u2192\n f\u2081 \u2261 f\u2082\n bool : \u2200 {b : Bool} \u2192\n b \u2261 b\n\n\u2261-refl : \u2200 {\u03c4} {v : \u27e6 \u03c4 \u27e7} \u2192\n v \u2261 v\n\u2261-refl {\u03c4\u2081 \u21d2 \u03c4\u2082} = ext (\u03bb v \u2192 \u2261-refl)\n\u2261-refl {bool} = bool\n\n\u2261-sym : \u2200 {\u03c4} {v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2081\n\u2261-sym {\u03c4\u2081 \u21d2 \u03c4\u2082} (ext \u2261) = ext (\u03bb v \u2192 \u2261-sym (\u2261 v))\n\u2261-sym {bool} bool = bool\n\n\u2261-trans : \u2200 {\u03c4} {v\u2081 v\u2082 v\u2083 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2083 \u2192 v\u2081 \u2261 v\u2083\n\u2261-trans {\u03c4\u2081 \u21d2 \u03c4\u2082} {f} (ext \u2261\u2081) (ext \u2261\u2082) =\n ext (\u03bb v \u2192 \u2261-trans (\u2261\u2081 v) (\u2261\u2082 v))\n\u2261-trans {bool} bool bool = bool\n\n\u2261-cong : \u2200 {\u03c4\u2082 \u03c4\u2081 v\u2081 v\u2082} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 f v\u2081 \u2261 f v\u2082\n\u2261-cong {\u03c4\u2081 \u21d2 \u03c4\u2082} f \u2261 = ext (\u03bb v \u2192 \u2261-cong (\u03bb x \u2192 f x v) \u2261)\n\u2261-cong {bool} f \u2261 = {!!}\n\n\u2261-cong\u2082 : \u2200 {\u03c4\u2083 \u03c4\u2081 \u03c4\u2082 v\u2081 v\u2082 v\u2083 v\u2084} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7 \u2192 \u27e6 \u03c4\u2083 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 f v\u2081 v\u2083 \u2261 f v\u2082 v\u2084\n\u2261-cong\u2082 {\u03c4\u2081 \u21d2 \u03c4\u2082} f \u2261\u2081 \u2261\u2082 = ext (\u03bb v \u2192 \u2261-cong\u2082 (\u03bb x y \u2192 f x y v) \u2261\u2081 \u2261\u2082)\n\u2261-cong\u2082 {bool} f \u2261\u2081 \u2261\u2082 = {!!}\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = \u2261-cong\u2082 (\u03bb x y \u2192 x y)\n\n\u2261-isEquivalence : \u2200 {\u03c4} \u2192 IsEquivalence (_\u2261_ {\u03c4})\n\u2261-isEquivalence = record\n { refl = \u2261-refl\n ; sym = \u2261-sym\n ; trans = \u2261-trans\n }\n\n\u2261-setoid : Type \u2192 Setoid _ _\n\u2261-setoid \u03c4 = record\n { Carrier = \u27e6 \u03c4 \u27e7\n ; _\u2248_ = _\u2261_\n ; isEquivalence = \u2261-isEquivalence\n }\n\nmodule \u2261-Reasoning where\n module _ {\u03c4 : Type} where\n open EqR (\u2261-setoid \u03c4) public\n hiding (_\u2261\u27e8_\u27e9_) renaming (_\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_)\n\n\u2261-consistent : \u00ac (\u2200 (\u03c4 : Type) \u2192 (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192 v\u2081 \u2261 v\u2082)\n\u2261-consistent H with H bool true false\n... | ()\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\u0394-Type (bool) = bool -- true means negate, false means nil change\n\nderive : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\napply : \u2200 {\u03c4} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\ndiff : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ndiff {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 diff (f\u2081 (apply dv v)) (f\u2082 v)\ndiff {bool} true true = false\ndiff {bool} true false = true\ndiff {bool} false true = true\ndiff {bool} false false = false\n\nderive {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 diff (f (apply dv v)) (f v)\nderive {bool} b = false\n\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} df f = \u03bb v \u2192 apply (df v (derive v)) (f v)\napply {bool} true true = false\napply {bool} true false = true\napply {bool} false true = true\napply {bool} false false = false\n\ncompose : \u2200 {\u03c4} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} df\u2081 df\u2082 = \u03bb v dv \u2192 compose (df\u2081 v dv) (df\u2082 v dv)\ncompose {bool} true true = false\ncompose {bool} true false = true\ncompose {bool} false true = true\ncompose {bool} false false = false\n\n-- CONGRUENCE rules for change operations\n\n\u2261-diff : \u2200 {\u03c4 : Type} {v\u2081 v\u2082 v\u2083 v\u2084 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 diff v\u2081 v\u2083 \u2261 diff v\u2082 v\u2084\n\u2261-diff = \u2261-cong\u2082 diff\n\n\u2261-apply : \u2200 {\u03c4 : Type} {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} {v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 v\u2081 \u2261 v\u2082 \u2192 apply dv\u2081 v\u2081 \u2261 apply dv\u2082 v\u2082\n\u2261-apply = \u2261-cong\u2082 apply\n\n-- PROPERTIES of changes\n\ndiff-derive : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192\n diff v v \u2261 derive v\ndiff-derive {\u03c4\u2081 \u21d2 \u03c4\u2082} v = \u2261-refl\ndiff-derive {bool} true = bool\ndiff-derive {bool} false = bool\n\ndiff-apply : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n diff (apply dv v) v \u2261 dv\ndiff-apply {\u03c4\u2081 \u21d2 \u03c4\u2082} df f = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n {!!}))\ndiff-apply {bool} true true = bool\ndiff-apply {bool} true false = bool\ndiff-apply {bool} false true = bool\ndiff-apply {bool} false false = bool\n\napply-diff : \u2200 {\u03c4} (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192\n apply (diff v\u2082 v\u2081) v\u2081 \u2261 v\u2082\n\napply-derive : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192\n apply (derive v) v \u2261 v\n\napply-diff {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = ext (\u03bb v \u2192\n begin\n apply (diff f\u2082 f\u2081) f\u2081 v\n \u2261\u27e8 \u2261-refl \u27e9\n apply (diff (f\u2082 (apply (derive v) v)) (f\u2081 v)) (f\u2081 v)\n \u2261\u27e8 \u2261-apply (\u2261-diff (\u2261-cong f\u2082 (apply-derive v)) \u2261-refl) \u2261-refl \u27e9\n apply (diff (f\u2082 v) (f\u2081 v)) (f\u2081 v)\n \u2261\u27e8 apply-diff (f\u2081 v) (f\u2082 v) \u27e9\n f\u2082 v\n \u220e) where open \u2261-Reasoning\napply-diff {bool} true true = bool\napply-diff {bool} true false = bool\napply-diff {bool} false true = bool\napply-diff {bool} false false = bool\n\napply-derive {\u03c4\u2081 \u21d2 \u03c4\u2082} f = ext (\u03bb v \u2192\n begin\n apply (derive f) f v\n \u2261\u27e8 \u2261-refl \u27e9\n apply (diff (f (apply (derive v) v)) (f v)) (f v)\n \u2261\u27e8 \u2261-cong (\u03bb x \u2192 apply (diff (f x) (f v)) (f v)) (apply-derive v) \u27e9\n apply (diff (f v) (f v)) (f v)\n \u2261\u27e8 apply-diff (f v) (f v)\u27e9\n f v\n \u220e) where open \u2261-Reasoning\napply-derive {bool} true = bool\napply-derive {bool} false = bool\n\napply-compose : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) (dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n apply (compose dv\u2081 dv\u2082) v \u2261 apply dv\u2081 (apply dv\u2082 v)\napply-compose {\u03c4\u2081 \u21d2 \u03c4\u2082} f df\u2081 df\u2082 = ext (\u03bb v \u2192\n apply-compose (f v) (df\u2081 v (derive v)) (df\u2082 v (derive v)))\napply-compose {bool} true true true = bool\napply-compose {bool} true true false = bool\napply-compose {bool} true false true = bool\napply-compose {bool} true false false = bool\napply-compose {bool} false true true = bool\napply-compose {bool} false true false = bool\napply-compose {bool} false false true = bool\napply-compose {bool} false false false = bool\n\ncompose-assoc : \u2200 {\u03c4} (dv\u2081 dv\u2082 dv\u2083 : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n compose dv\u2081 (compose dv\u2082 dv\u2083) \u2261 compose (compose dv\u2081 dv\u2082) dv\u2083\ncompose-assoc {\u03c4\u2081 \u21d2 \u03c4\u2082} df\u2081 df\u2082 df\u2083 = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n compose-assoc (df\u2081 v dv) (df\u2082 v dv) (df\u2083 v dv)))\ncompose-assoc {bool} true true true = bool\ncompose-assoc {bool} true true false = bool\ncompose-assoc {bool} true false true = bool\ncompose-assoc {bool} true false false = bool\ncompose-assoc {bool} false true true = bool\ncompose-assoc {bool} false true false = bool\ncompose-assoc {bool} false false true = bool\ncompose-assoc {bool} false false false = bool\n\n-- TYPING CONTEXTS, VARIABLES and WEAKENING\n\nopen import binding Type \u27e6_\u27e7Type\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393\n\nupdate : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 \u03c1) = apply dv v \u2022 update \u03c1\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore {\u2205} \u2205 = \u2205\nignore {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 \u03c1) = v \u2022 ignore \u03c1\n\n\u0394-Context\u2032 : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\n\u0394-Context\u2032 \u0393 \u2205 = \u0394-Context \u0393\n\u0394-Context\u2032 (.\u03c4 \u2022 \u0393) (\u03c4 \u2022 \u0393\u2032) = \u03c4 \u2022 \u0394-Context\u2032 \u0393 \u0393\u2032\n\nupdate\u2032 : \u2200 {\u0393} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nupdate\u2032 \u2205 \u03c1 = update \u03c1\nupdate\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) = v \u2022 update\u2032 \u0393\u2032 \u03c1\n\nignore\u2032 : \u2200 {\u0393} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore\u2032 \u2205 \u03c1 = ignore \u03c1\nignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) = v \u2022 ignore\u2032 \u0393\u2032 \u03c1\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n true false : \u2200 {\u0393} \u2192 Term \u0393 bool\n if : \u2200 {\u0393 \u03c4} \u2192 (t\u2081 : Term \u0393 bool) (t\u2082 t\u2083 : Term \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n -- `\u0394 t` describes how t changes if its free variables or arguments change\n \u0394 : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 true \u27e7Term \u03c1 = true\n\u27e6 false \u27e7Term \u03c1 = false\n\u27e6 if t\u2081 t\u2082 t\u2083 \u27e7Term \u03c1 with \u27e6 t\u2081 \u27e7Term \u03c1\n... | true = \u27e6 t\u2082 \u27e7Term \u03c1\n... | false = \u27e6 t\u2083 \u27e7Term \u03c1\n\u27e6 \u0394 t \u27e7Term \u03c1 = diff (\u27e6 t \u27e7Term (update \u03c1)) (\u27e6 t \u27e7Term (ignore \u03c1))\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n-- Term Equivalence\n\nmodule _ {\u0393} {\u03c4} where\n data _\u2248_ (t\u2081 t\u2082 : Term \u0393 \u03c4) : Set where\n ext :\n (\u2200 \u03c1 \u2192 \u27e6 t\u2081 \u27e7 \u03c1 \u2261 \u27e6 t\u2082 \u27e7 \u03c1) \u2192\n t\u2081 \u2248 t\u2082\n\n \u2248-refl : Reflexive _\u2248_\n \u2248-refl = ext (\u03bb \u03c1 \u2192 \u2261-refl)\n\n \u2248-sym : Symmetric _\u2248_\n \u2248-sym (ext \u2248) = ext (\u03bb \u03c1 \u2192 \u2261-sym (\u2248 \u03c1))\n\n \u2248-trans : Transitive _\u2248_\n \u2248-trans (ext \u2248\u2081) (ext \u2248\u2082) = ext (\u03bb \u03c1 \u2192 \u2261-trans (\u2248\u2081 \u03c1) (\u2248\u2082 \u03c1))\n\n \u2248-isEquivalence : IsEquivalence _\u2248_\n \u2248-isEquivalence = record\n { refl = \u2248-refl\n ; sym = \u2248-sym\n ; trans = \u2248-trans\n }\n\n\u2248-setoid : Context \u2192 Type \u2192 Setoid _ _\n\u2248-setoid \u0393 \u03c4 = record\n { Carrier = Term \u0393 \u03c4\n ; _\u2248_ = _\u2248_\n ; isEquivalence = \u2248-isEquivalence\n }\n\n\u2248-app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} {t\u2081 t\u2082 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2083 t\u2084 : Term \u0393 \u03c4\u2081} \u2192\n t\u2081 \u2248 t\u2082 \u2192 t\u2083 \u2248 t\u2084 \u2192 app t\u2081 t\u2083 \u2248 app t\u2082 t\u2084\n\u2248-app (ext \u2248\u2081) (ext \u2248\u2082) = ext (\u03bb \u03c1 \u2192\n \u2261-cong\u2082 (\u03bb x y \u2192 x y) (\u2248\u2081 \u03c1) (\u2248\u2082 \u03c1))\n\n\u2248-abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} {t\u2081 t\u2082 : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n t\u2081 \u2248 t\u2082 \u2192 abs t\u2081 \u2248 abs t\u2082\n\u2248-abs (ext \u2248) = ext (\u03bb \u03c1 \u2192\n ext (\u03bb v \u2192 \u2248 (v \u2022 \u03c1)))\n\n\u2248-\u0394 : \u2200 {\u03c4 \u0393} {t\u2081 t\u2082 : Term \u0393 \u03c4} \u2192\n t\u2081 \u2248 t\u2082 \u2192 \u0394 t\u2081 \u2248 \u0394 t\u2082\n\u2248-\u0394 (ext \u2248) = ext (\u03bb \u03c1 \u2192 \u2261-diff (\u2248 (update \u03c1)) (\u2248 (ignore \u03c1)))\n\nmodule \u2248-Reasoning where\n module _ {\u0393 : Context} {\u03c4 : Type} where\n open EqR (\u2248-setoid \u0393 \u03c4) public\n hiding (_\u2261\u27e8_\u27e9_)\n\n\u2248-consistent : \u00ac (\u2200 {\u0393 \u03c4} (t\u2081 t\u2082 : Term \u0393 \u03c4) \u2192 t\u2081 \u2248 t\u2082)\n\u2248-consistent H with H {\u2205} true false\n... | ext x with x \u2205\n... | ()\n\n-- LIFTING terms into \u0394-Contexts\n\nlift-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) \u03c4\nlift-var this = that this\nlift-var (that x) = that (that (lift-var x))\n\nlift-var\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-var\u2032 \u2205 x = lift-var x\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) this = this\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) (that x) = that (lift-var\u2032 \u0393\u2032 x)\n\nlift-term\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-term\u2032 \u0393\u2032 (abs t) = abs (lift-term\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term\u2032 \u0393\u2032 (app t\u2081 t\u2082) = app (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082)\nlift-term\u2032 \u0393\u2032 (var x) = var (lift-var\u2032 \u0393\u2032 x)\nlift-term\u2032 \u0393\u2032 true = true\nlift-term\u2032 \u0393\u2032 false = false\nlift-term\u2032 \u0393\u2032 (if t\u2081 t\u2082 t\u2083) = if (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082) (lift-term\u2032 \u0393\u2032 t\u2083)\nlift-term\u2032 \u0393\u2032 (\u0394 t) = {!!}\n\nlift-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) \u03c4\nlift-term = lift-term\u2032 \u2205\n\n-- PROPERTIES of lift-term\n\nlift-var-ignore : \u2200 {\u0393 \u03c4} (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore \u03c1)\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) (that x) = lift-var-ignore \u03c1 x\n\nlift-var-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) (\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var\u2032 \u0393\u2032 x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-var-ignore\u2032 \u2205 \u03c1 x = lift-var-ignore \u03c1 x\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) (that x) = lift-var-ignore\u2032 \u0393\u2032 \u03c1 x\n\nlift-term-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) {\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term\u2032 \u0393\u2032 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-term-ignore\u2032 \u0393\u2032 (abs t) =\n ext (\u03bb v \u2192 lift-term-ignore\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term-ignore\u2032 \u0393\u2032 (app t\u2081 t\u2082) =\n \u2261-app (lift-term-ignore\u2032 \u0393\u2032 t\u2081) (lift-term-ignore\u2032 \u0393\u2032 t\u2082)\nlift-term-ignore\u2032 \u0393\u2032 (var x) = lift-var-ignore\u2032 \u0393\u2032 _ x\nlift-term-ignore\u2032 \u0393\u2032 true = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 false = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 {\u03c1} (if t\u2081 t\u2082 t\u2083)\n with \u27e6 lift-term\u2032 \u0393\u2032 t\u2081 \u27e7 \u03c1\n | \u27e6 t\u2081 \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\n | lift-term-ignore\u2032 \u0393\u2032 {\u03c1} t\u2081\n... | true | true | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2082\n... | false | false | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2083\nlift-term-ignore\u2032 \u0393\u2032 (\u0394 t) = {!!}\n\nlift-term-ignore : \u2200 {\u0393 \u03c4} {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore \u03c1)\nlift-term-ignore = lift-term-ignore\u2032 \u2205\n\n\n-- PROPERTIES of \u0394\n\n\u0394-abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192\n \u0394 (abs t) \u2248 abs (abs (\u0394 t))\n\u0394-abs t = ext (\u03bb \u03c1 \u2192 \u2261-refl)\n\n\u0394-app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192\n \u0394 (app t\u2081 t\u2082) \u2248 app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n\u0394-app t\u2081 t\u2082 = \u2248-sym (ext (\u03bb \u03c1 \u2192\n begin\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 lift-term t\u2082 \u27e7 \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 lift-term t\u2082 \u27e7 \u03c1))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))) x))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) x))\n (lift-term-ignore {\u03c1 = \u03c1} t\u2082) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff (\u27e6 t\u2081 \u27e7 (update \u03c1) x) (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (apply-diff (\u27e6 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 t\u2082 \u27e7 (update \u03c1))) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u220e)) where open \u2261-Reasoning\n\n-- SYMBOLIC DERIVATION\n\nderive-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-var this = this\nderive-var (that x) = that (that (derive-var x))\n\nderive-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-term (abs t) = abs (abs (derive-term t))\nderive-term (app t\u2081 t\u2082) = app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\nderive-term (var x) = var (derive-var x)\nderive-term true = false\nderive-term false = false\nderive-term (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term (\u0394 t) = \u0394 (derive-term t)\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n \u0394 t \u2248 derive-term t\nderive-term-correct {\u0393} (abs t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct t)) \u27e9\n abs (abs (derive-term t))\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (abs t)\n \u220e where open \u2248-Reasoning\nderive-term-correct (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct t\u2081) \u2248-refl) (derive-term-correct t\u2082) \u27e9\n app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct (var x) = ext (\u03bb \u03c1 \u2192 derive-var-correct \u03c1 x)\nderive-term-correct true = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\n\n-- NATURAL SEMANTICS\n\n-- (without support for \u0394 for now)\n\n-- Syntax\n\ndata Env : Context \u2192 Set\ndata Val : Type \u2192 Set\n\ndata Val where\n \u27e8abs_,_\u27e9 : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) (\u03c1 : Env \u0393) \u2192 Val (\u03c4\u2081 \u21d2 \u03c4\u2082)\n\ndata Env where\n \u2205 : Env \u2205\n _\u2022_ : \u2200 {\u0393 \u03c4} \u2192 Val \u03c4 \u2192 Env \u0393 \u2192 Env (\u03c4 \u2022 \u0393)\n\n-- Lookup\n\ninfixr 8 _\u22a2_\u2193_ _\u22a2_\u21a6_\n\ndata _\u22a2_\u21a6_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Var \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n this : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {v : Val \u03c4} \u2192\n v \u2022 \u03c1 \u22a2 this \u21a6 v\n that : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 x} {\u03c1 : Env \u0393} {v\u2081 : Val \u03c4\u2081} {v\u2082 : Val \u03c4\u2082} \u2192\n \u03c1 \u22a2 x \u21a6 v\u2082 \u2192\n v\u2081 \u2022 \u03c1 \u22a2 that x \u21a6 v\u2082\n\n-- Reduction\n\ndata _\u22a2_\u2193_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Term \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c1} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n \u03c1 \u22a2 abs t \u2193 \u27e8abs t , \u03c1 \u27e9\n app : \u2200 {\u0393 \u0393\u2032 \u03c4\u2081 \u03c4\u2082 \u03c1 \u03c1\u2032 v\u2082 v\u2032} {t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2082 : Term \u0393 \u03c4\u2081} {t\u2032 : Term (\u03c4\u2081 \u2022 \u0393\u2032) \u03c4\u2082} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 \u27e8abs t\u2032 , \u03c1\u2032 \u27e9 \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n v\u2082 \u2022 \u03c1\u2032 \u22a2 t\u2032 \u2193 v\u2032 \u2192\n \u03c1 \u22a2 app t\u2081 t\u2082 \u2193 v\u2032\n var : \u2200 {\u0393 \u03c4 x} {\u03c1 : Env \u0393} {v : Val \u03c4}\u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u03c1 \u22a2 var x \u2193 v\n\n-- SOUNDNESS of natural semantics\n\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 Env \u0393 \u2192 \u27e6 \u0393 \u27e7\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 \u27e8abs t , \u03c1 \u27e9 \u27e7Val = \u03bb v \u2192 \u27e6 t \u27e7 (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\nmeaningOfEnv : \u2200 {\u0393} \u2192 Meaning (Env \u0393)\nmeaningOfEnv = meaning \u27e6_\u27e7Env\n\nmeaningOfVal : \u2200 {\u03c4} \u2192 Meaning (Val \u03c4)\nmeaningOfVal = meaning \u27e6_\u27e7Val\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = \u2261-refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = \u2261-refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) =\n \u2261-trans\n (\u2261-cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082))\n (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n\n-- WEAKENING\n\n-- Weaken a term to a super context\n\n{-\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken {\u0393\u2081} {\u0393\u2082} (abs {\u03c4\u2081 = \u03c4} t) = abs (weaken {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} t)\nweaken {\u0393\u2081} {\u0393\u2082} (app t\u2081 t\u2082) = app (weaken {\u0393\u2081} {\u0393\u2082} t\u2081) (weaken {\u0393\u2081} {\u0393\u2082} t\u2082)\nweaken {\u0393\u2081} {\u0393\u2082} (var x) = var (lift {\u0393\u2081} {\u0393\u2082} x)\nweaken {\u0393\u2081} {\u0393\u2082} (\u0394 t) = ?\n-}\n","old_contents":"module total where\n\n-- INCREMENTAL \u03bb-CALCULUS\n-- with total derivatives\n--\n-- Features:\n-- * changes and derivatives are unified (following Cai)\n-- * \u0394 e describes how e changes when its free variables or its arguments change\n-- * denotational semantics including semantics of changes\n--\n-- Work in Progress:\n-- * lemmas about behavior of changes\n-- * lemmas about behavior of \u0394\n-- * correctness proof for symbolic derivation\n\nimport Relation.Binary as B\n\nopen import Relation.Binary using\n (IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)\nimport Relation.Binary.EqReasoning as EqR\n\nopen import meaning\n\n-- SIMPLE TYPES\n\n-- Syntax\n\ndata Type : Set where\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n bool : Type\n\ninfixr 5 _\u21d2_\n\n-- Denotational Semantics\n\ndata Bool : Set where\n true false : Bool\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 bool \u27e7Type = Bool\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\n-- Value Equivalence\n\ndata _\u2261_ : \u2200 {\u03c4} \u2192 (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192 Set where\n ext : \u2200 {\u03c4\u2081 \u03c4\u2082} {f\u2081 f\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} \u2192\n (\u2200 v \u2192 f\u2081 v \u2261 f\u2082 v) \u2192\n f\u2081 \u2261 f\u2082\n bool : \u2200 {b : Bool} \u2192\n b \u2261 b\n\n\u2261-refl : \u2200 {\u03c4} {v : \u27e6 \u03c4 \u27e7} \u2192\n v \u2261 v\n\u2261-refl {\u03c4\u2081 \u21d2 \u03c4\u2082} = ext (\u03bb v \u2192 \u2261-refl)\n\u2261-refl {bool} = bool\n\n\u2261-sym : \u2200 {\u03c4} {v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2081\n\u2261-sym {\u03c4\u2081 \u21d2 \u03c4\u2082} (ext \u2261) = ext (\u03bb v \u2192 \u2261-sym (\u2261 v))\n\u2261-sym {bool} bool = bool\n\n\u2261-trans : \u2200 {\u03c4} {v\u2081 v\u2082 v\u2083 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2083 \u2192 v\u2081 \u2261 v\u2083\n\u2261-trans {\u03c4\u2081 \u21d2 \u03c4\u2082} {f} (ext \u2261\u2081) (ext \u2261\u2082) =\n ext (\u03bb v \u2192 \u2261-trans (\u2261\u2081 v) (\u2261\u2082 v))\n\u2261-trans {bool} bool bool = bool\n\n\u2261-cong : \u2200 {\u03c4\u2082 \u03c4\u2081 v\u2081 v\u2082} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 f v\u2081 \u2261 f v\u2082\n\u2261-cong {\u03c4\u2081 \u21d2 \u03c4\u2082} f \u2261 = ext (\u03bb v \u2192 \u2261-cong (\u03bb x \u2192 f x v) \u2261)\n\u2261-cong {bool} f \u2261 = {!!}\n\n\u2261-cong\u2082 : \u2200 {\u03c4\u2083 \u03c4\u2081 \u03c4\u2082 v\u2081 v\u2082 v\u2083 v\u2084} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7 \u2192 \u27e6 \u03c4\u2083 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 f v\u2081 v\u2083 \u2261 f v\u2082 v\u2084\n\u2261-cong\u2082 {\u03c4\u2081 \u21d2 \u03c4\u2082} f \u2261\u2081 \u2261\u2082 = ext (\u03bb v \u2192 \u2261-cong\u2082 (\u03bb x y \u2192 f x y v) \u2261\u2081 \u2261\u2082)\n\u2261-cong\u2082 {bool} f \u2261\u2081 \u2261\u2082 = {!!}\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = \u2261-cong\u2082 (\u03bb x y \u2192 x y)\n\n\u2261-isEquivalence : \u2200 {\u03c4} \u2192 IsEquivalence (_\u2261_ {\u03c4})\n\u2261-isEquivalence = record\n { refl = \u2261-refl\n ; sym = \u2261-sym\n ; trans = \u2261-trans\n }\n\n\u2261-setoid : Type \u2192 Setoid _ _\n\u2261-setoid \u03c4 = record\n { Carrier = \u27e6 \u03c4 \u27e7\n ; _\u2248_ = _\u2261_\n ; isEquivalence = \u2261-isEquivalence\n }\n\nmodule \u2261-Reasoning where\n module _ {\u03c4 : Type} where\n open EqR (\u2261-setoid \u03c4) public\n hiding (_\u2261\u27e8_\u27e9_) renaming (_\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_)\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\u0394-Type (bool) = bool -- true means negate, false means nil change\n\nderive : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\napply : \u2200 {\u03c4} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\ndiff : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ndiff {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 diff (f\u2081 (apply dv v)) (f\u2082 v)\ndiff {bool} true true = false\ndiff {bool} true false = true\ndiff {bool} false true = true\ndiff {bool} false false = false\n\nderive {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 diff (f (apply dv v)) (f v)\nderive {bool} b = false\n\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} df f = \u03bb v \u2192 apply (df v (derive v)) (f v)\napply {bool} true true = false\napply {bool} true false = true\napply {bool} false true = true\napply {bool} false false = false\n\ncompose : \u2200 {\u03c4} \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} df\u2081 df\u2082 = \u03bb v dv \u2192 compose (df\u2081 v dv) (df\u2082 v dv)\ncompose {bool} true true = false\ncompose {bool} true false = true\ncompose {bool} false true = true\ncompose {bool} false false = false\n\n-- CONGRUENCE rules for change operations\n\n\u2261-diff : \u2200 {\u03c4 : Type} {v\u2081 v\u2082 v\u2083 v\u2084 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 diff v\u2081 v\u2083 \u2261 diff v\u2082 v\u2084\n\u2261-diff = \u2261-cong\u2082 diff\n\n\u2261-apply : \u2200 {\u03c4 : Type} {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} {v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 v\u2081 \u2261 v\u2082 \u2192 apply dv\u2081 v\u2081 \u2261 apply dv\u2082 v\u2082\n\u2261-apply = \u2261-cong\u2082 apply\n\n-- PROPERTIES of changes\n\ndiff-derive : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192\n diff v v \u2261 derive v\ndiff-derive {\u03c4\u2081 \u21d2 \u03c4\u2082} v = \u2261-refl\ndiff-derive {bool} true = bool\ndiff-derive {bool} false = bool\n\ndiff-apply : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n diff (apply dv v) v \u2261 dv\ndiff-apply {\u03c4\u2081 \u21d2 \u03c4\u2082} df f = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n {!!}))\ndiff-apply {bool} true true = bool\ndiff-apply {bool} true false = bool\ndiff-apply {bool} false true = bool\ndiff-apply {bool} false false = bool\n\napply-diff : \u2200 {\u03c4} (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192\n apply (diff v\u2082 v\u2081) v\u2081 \u2261 v\u2082\n\napply-derive : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192\n apply (derive v) v \u2261 v\n\napply-diff {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = ext (\u03bb v \u2192\n begin\n apply (diff f\u2082 f\u2081) f\u2081 v\n \u2261\u27e8 \u2261-refl \u27e9\n apply (diff (f\u2082 (apply (derive v) v)) (f\u2081 v)) (f\u2081 v)\n \u2261\u27e8 \u2261-apply (\u2261-diff (\u2261-cong f\u2082 (apply-derive v)) \u2261-refl) \u2261-refl \u27e9\n apply (diff (f\u2082 v) (f\u2081 v)) (f\u2081 v)\n \u2261\u27e8 apply-diff (f\u2081 v) (f\u2082 v) \u27e9\n f\u2082 v\n \u220e) where open \u2261-Reasoning\napply-diff {bool} true true = bool\napply-diff {bool} true false = bool\napply-diff {bool} false true = bool\napply-diff {bool} false false = bool\n\napply-derive {\u03c4\u2081 \u21d2 \u03c4\u2082} f = ext (\u03bb v \u2192\n begin\n apply (derive f) f v\n \u2261\u27e8 \u2261-refl \u27e9\n apply (diff (f (apply (derive v) v)) (f v)) (f v)\n \u2261\u27e8 \u2261-cong (\u03bb x \u2192 apply (diff (f x) (f v)) (f v)) (apply-derive v) \u27e9\n apply (diff (f v) (f v)) (f v)\n \u2261\u27e8 apply-diff (f v) (f v)\u27e9\n f v\n \u220e) where open \u2261-Reasoning\napply-derive {bool} true = bool\napply-derive {bool} false = bool\n\napply-compose : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) (dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n apply (compose dv\u2081 dv\u2082) v \u2261 apply dv\u2081 (apply dv\u2082 v)\napply-compose {\u03c4\u2081 \u21d2 \u03c4\u2082} f df\u2081 df\u2082 = ext (\u03bb v \u2192\n apply-compose (f v) (df\u2081 v (derive v)) (df\u2082 v (derive v)))\napply-compose {bool} true true true = bool\napply-compose {bool} true true false = bool\napply-compose {bool} true false true = bool\napply-compose {bool} true false false = bool\napply-compose {bool} false true true = bool\napply-compose {bool} false true false = bool\napply-compose {bool} false false true = bool\napply-compose {bool} false false false = bool\n\ncompose-assoc : \u2200 {\u03c4} (dv\u2081 dv\u2082 dv\u2083 : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n compose dv\u2081 (compose dv\u2082 dv\u2083) \u2261 compose (compose dv\u2081 dv\u2082) dv\u2083\ncompose-assoc {\u03c4\u2081 \u21d2 \u03c4\u2082} df\u2081 df\u2082 df\u2083 = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n compose-assoc (df\u2081 v dv) (df\u2082 v dv) (df\u2083 v dv)))\ncompose-assoc {bool} true true true = bool\ncompose-assoc {bool} true true false = bool\ncompose-assoc {bool} true false true = bool\ncompose-assoc {bool} true false false = bool\ncompose-assoc {bool} false true true = bool\ncompose-assoc {bool} false true false = bool\ncompose-assoc {bool} false false true = bool\ncompose-assoc {bool} false false false = bool\n\n-- TYPING CONTEXTS, VARIABLES and WEAKENING\n\nopen import binding Type \u27e6_\u27e7Type\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393\n\nupdate : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 \u03c1) = apply dv v \u2022 update \u03c1\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore {\u2205} \u2205 = \u2205\nignore {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 \u03c1) = v \u2022 ignore \u03c1\n\n\u0394-Context\u2032 : (\u0393 : Context) \u2192 Prefix \u0393 \u2192 Context\n\u0394-Context\u2032 \u0393 \u2205 = \u0394-Context \u0393\n\u0394-Context\u2032 (.\u03c4 \u2022 \u0393) (\u03c4 \u2022 \u0393\u2032) = \u03c4 \u2022 \u0394-Context\u2032 \u0393 \u0393\u2032\n\nupdate\u2032 : \u2200 {\u0393} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nupdate\u2032 \u2205 \u03c1 = update \u03c1\nupdate\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) = v \u2022 update\u2032 \u0393\u2032 \u03c1\n\nignore\u2032 : \u2200 {\u0393} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore\u2032 \u2205 \u03c1 = ignore \u03c1\nignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) = v \u2022 ignore\u2032 \u0393\u2032 \u03c1\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n true false : \u2200 {\u0393} \u2192 Term \u0393 bool\n if : \u2200 {\u0393 \u03c4} \u2192 (t\u2081 : Term \u0393 bool) (t\u2082 t\u2083 : Term \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n -- `\u0394 t` describes how t changes if its free variables or arguments change\n \u0394 : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 true \u27e7Term \u03c1 = true\n\u27e6 false \u27e7Term \u03c1 = false\n\u27e6 if t\u2081 t\u2082 t\u2083 \u27e7Term \u03c1 with \u27e6 t\u2081 \u27e7Term \u03c1\n... | true = \u27e6 t\u2082 \u27e7Term \u03c1\n... | false = \u27e6 t\u2083 \u27e7Term \u03c1\n\u27e6 \u0394 t \u27e7Term \u03c1 = diff (\u27e6 t \u27e7Term (update \u03c1)) (\u27e6 t \u27e7Term (ignore \u03c1))\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n-- Term Equivalence\n\nmodule _ {\u0393} {\u03c4} where\n data _\u2248_ (t\u2081 t\u2082 : Term \u0393 \u03c4) : Set where\n ext :\n (\u2200 \u03c1 \u2192 \u27e6 t\u2081 \u27e7 \u03c1 \u2261 \u27e6 t\u2082 \u27e7 \u03c1) \u2192\n t\u2081 \u2248 t\u2082\n\n \u2248-refl : Reflexive _\u2248_\n \u2248-refl = ext (\u03bb \u03c1 \u2192 \u2261-refl)\n\n \u2248-sym : Symmetric _\u2248_\n \u2248-sym (ext \u2248) = ext (\u03bb \u03c1 \u2192 \u2261-sym (\u2248 \u03c1))\n\n \u2248-trans : Transitive _\u2248_\n \u2248-trans (ext \u2248\u2081) (ext \u2248\u2082) = ext (\u03bb \u03c1 \u2192 \u2261-trans (\u2248\u2081 \u03c1) (\u2248\u2082 \u03c1))\n\n \u2248-isEquivalence : IsEquivalence _\u2248_\n \u2248-isEquivalence = record\n { refl = \u2248-refl\n ; sym = \u2248-sym\n ; trans = \u2248-trans\n }\n\n\u2248-setoid : Context \u2192 Type \u2192 Setoid _ _\n\u2248-setoid \u0393 \u03c4 = record\n { Carrier = Term \u0393 \u03c4\n ; _\u2248_ = _\u2248_\n ; isEquivalence = \u2248-isEquivalence\n }\n\n\u2248-app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} {t\u2081 t\u2082 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2083 t\u2084 : Term \u0393 \u03c4\u2081} \u2192\n t\u2081 \u2248 t\u2082 \u2192 t\u2083 \u2248 t\u2084 \u2192 app t\u2081 t\u2083 \u2248 app t\u2082 t\u2084\n\u2248-app (ext \u2248\u2081) (ext \u2248\u2082) = ext (\u03bb \u03c1 \u2192\n \u2261-cong\u2082 (\u03bb x y \u2192 x y) (\u2248\u2081 \u03c1) (\u2248\u2082 \u03c1))\n\n\u2248-abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} {t\u2081 t\u2082 : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n t\u2081 \u2248 t\u2082 \u2192 abs t\u2081 \u2248 abs t\u2082\n\u2248-abs (ext \u2248) = ext (\u03bb \u03c1 \u2192\n ext (\u03bb v \u2192 \u2248 (v \u2022 \u03c1)))\n\n\u2248-\u0394 : \u2200 {\u03c4 \u0393} {t\u2081 t\u2082 : Term \u0393 \u03c4} \u2192\n t\u2081 \u2248 t\u2082 \u2192 \u0394 t\u2081 \u2248 \u0394 t\u2082\n\u2248-\u0394 (ext \u2248) = ext (\u03bb \u03c1 \u2192 \u2261-diff (\u2248 (update \u03c1)) (\u2248 (ignore \u03c1)))\n\nmodule \u2248-Reasoning where\n module _ {\u0393 : Context} {\u03c4 : Type} where\n open EqR (\u2248-setoid \u0393 \u03c4) public\n hiding (_\u2261\u27e8_\u27e9_)\n\n-- LIFTING terms into \u0394-Contexts\n\nlift-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) \u03c4\nlift-var this = that this\nlift-var (that x) = that (that (lift-var x))\n\nlift-var\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-var\u2032 \u2205 x = lift-var x\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) this = this\nlift-var\u2032 (\u03c4 \u2022 \u0393\u2032) (that x) = that (lift-var\u2032 \u0393\u2032 x)\n\nlift-term\u2032 : \u2200 {\u0393 \u03c4} \u2192 (\u0393\u2032 : Prefix \u0393) \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context\u2032 \u0393 \u0393\u2032) \u03c4\nlift-term\u2032 \u0393\u2032 (abs t) = abs (lift-term\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term\u2032 \u0393\u2032 (app t\u2081 t\u2082) = app (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082)\nlift-term\u2032 \u0393\u2032 (var x) = var (lift-var\u2032 \u0393\u2032 x)\nlift-term\u2032 \u0393\u2032 true = true\nlift-term\u2032 \u0393\u2032 false = false\nlift-term\u2032 \u0393\u2032 (if t\u2081 t\u2082 t\u2083) = if (lift-term\u2032 \u0393\u2032 t\u2081) (lift-term\u2032 \u0393\u2032 t\u2082) (lift-term\u2032 \u0393\u2032 t\u2083)\nlift-term\u2032 \u0393\u2032 (\u0394 t) = {!!}\n\nlift-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) \u03c4\nlift-term = lift-term\u2032 \u2205\n\n-- PROPERTIES of lift-term\n\nlift-var-ignore : \u2200 {\u0393 \u03c4} (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore \u03c1)\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore (v \u2022 dv \u2022 \u03c1) (that x) = lift-var-ignore \u03c1 x\n\nlift-var-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) (\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7) (x : Var \u0393 \u03c4) \u2192\n \u27e6 lift-var\u2032 \u0393\u2032 x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-var-ignore\u2032 \u2205 \u03c1 x = lift-var-ignore \u03c1 x\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) this = \u2261-refl\nlift-var-ignore\u2032 (\u03c4 \u2022 \u0393\u2032) (v \u2022 \u03c1) (that x) = lift-var-ignore\u2032 \u0393\u2032 \u03c1 x\n\nlift-term-ignore\u2032 : \u2200 {\u0393 \u03c4} \u2192\n (\u0393\u2032 : Prefix \u0393) {\u03c1 : \u27e6 \u0394-Context\u2032 \u0393 \u0393\u2032 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term\u2032 \u0393\u2032 t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\nlift-term-ignore\u2032 \u0393\u2032 (abs t) =\n ext (\u03bb v \u2192 lift-term-ignore\u2032 (_ \u2022 \u0393\u2032) t)\nlift-term-ignore\u2032 \u0393\u2032 (app t\u2081 t\u2082) =\n \u2261-app (lift-term-ignore\u2032 \u0393\u2032 t\u2081) (lift-term-ignore\u2032 \u0393\u2032 t\u2082)\nlift-term-ignore\u2032 \u0393\u2032 (var x) = lift-var-ignore\u2032 \u0393\u2032 _ x\nlift-term-ignore\u2032 \u0393\u2032 true = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 false = \u2261-refl\nlift-term-ignore\u2032 \u0393\u2032 {\u03c1} (if t\u2081 t\u2082 t\u2083)\n with \u27e6 lift-term\u2032 \u0393\u2032 t\u2081 \u27e7 \u03c1\n | \u27e6 t\u2081 \u27e7 (ignore\u2032 \u0393\u2032 \u03c1)\n | lift-term-ignore\u2032 \u0393\u2032 {\u03c1} t\u2081\n... | true | true | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2082\n... | false | false | bool = lift-term-ignore\u2032 \u0393\u2032 t\u2083\nlift-term-ignore\u2032 \u0393\u2032 (\u0394 t) = {!!}\n\nlift-term-ignore : \u2200 {\u0393 \u03c4} {\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} (t : Term \u0393 \u03c4) \u2192\n \u27e6 lift-term t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (ignore \u03c1)\nlift-term-ignore = lift-term-ignore\u2032 \u2205\n\n\n-- PROPERTIES of \u0394\n\n\u0394-abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192\n \u0394 (abs t) \u2248 abs (abs (\u0394 t))\n\u0394-abs t = ext (\u03bb \u03c1 \u2192 \u2261-refl)\n\n\u0394-app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192\n \u0394 (app t\u2081 t\u2082) \u2248 app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n\u0394-app t\u2081 t\u2082 = \u2248-sym (ext (\u03bb \u03c1 \u2192\n begin\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 lift-term t\u2082 \u27e7 \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 lift-term t\u2082 \u27e7 \u03c1))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))) x))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) x))\n (lift-term-ignore {\u03c1 = \u03c1} t\u2082) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1)\n (apply\n (diff (\u27e6 t\u2082 \u27e7 (update \u03c1)) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192\n diff (\u27e6 t\u2081 \u27e7 (update \u03c1) x) (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1))))\n (apply-diff (\u27e6 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 t\u2082 \u27e7 (update \u03c1))) \u27e9\n diff\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1)))\n (\u27e6 t\u2081 \u27e7 (ignore \u03c1) (\u27e6 t\u2082 \u27e7 (ignore \u03c1)))\n \u220e)) where open \u2261-Reasoning\n\n-- SYMBOLIC DERIVATION\n\nderive-var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-var this = this\nderive-var (that x) = that (that (derive-var x))\n\nderive-term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderive-term (abs t) = abs (abs (derive-term t))\nderive-term (app t\u2081 t\u2082) = app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\nderive-term (var x) = var (derive-var x)\nderive-term true = false\nderive-term false = false\nderive-term (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term (\u0394 t) = \u0394 (derive-term t)\n\n-- CORRECTNESS of derivation\n\nderive-var-correct : \u2200 {\u0393 \u03c4} \u2192 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 (x : Var \u0393 \u03c4) \u2192\n diff (\u27e6 x \u27e7 (update \u03c1)) (\u27e6 x \u27e7 (ignore \u03c1)) \u2261\n \u27e6 derive-var x \u27e7 \u03c1\nderive-var-correct (dv \u2022 v \u2022 \u03c1) this = diff-apply dv v\nderive-var-correct (dv \u2022 v \u2022 \u03c1) (that x) = derive-var-correct \u03c1 x\n\nderive-term-correct : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n \u0394 t \u2248 derive-term t\nderive-term-correct {\u0393} (abs t) =\n begin\n \u0394 (abs t)\n \u2248\u27e8 \u0394-abs t \u27e9\n abs (abs (\u0394 t))\n \u2248\u27e8 \u2248-abs (\u2248-abs (derive-term-correct t)) \u27e9\n abs (abs (derive-term t))\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (abs t)\n \u220e where open \u2248-Reasoning\nderive-term-correct (app t\u2081 t\u2082) =\n begin\n \u0394 (app t\u2081 t\u2082)\n \u2248\u27e8 \u0394-app t\u2081 t\u2082 \u27e9\n app (app (\u0394 t\u2081) (lift-term t\u2082)) (\u0394 t\u2082)\n \u2248\u27e8 \u2248-app (\u2248-app (derive-term-correct t\u2081) \u2248-refl) (derive-term-correct t\u2082) \u27e9\n app (app (derive-term t\u2081) (lift-term t\u2082)) (derive-term t\u2082)\n \u2248\u27e8 \u2248-refl \u27e9\n derive-term (app t\u2081 t\u2082)\n \u220e where open \u2248-Reasoning\nderive-term-correct (var x) = ext (\u03bb \u03c1 \u2192 derive-var-correct \u03c1 x)\nderive-term-correct true = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct false = ext (\u03bb \u03c1 \u2192 \u2261-refl)\nderive-term-correct (if t\u2081 t\u2082 t\u2083) = {!!}\nderive-term-correct (\u0394 t) = \u2248-\u0394 (derive-term-correct t)\n\n-- NATURAL SEMANTICS\n\n-- (without support for \u0394 for now)\n\n-- Syntax\n\ndata Env : Context \u2192 Set\ndata Val : Type \u2192 Set\n\ndata Val where\n \u27e8abs_,_\u27e9 : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) (\u03c1 : Env \u0393) \u2192 Val (\u03c4\u2081 \u21d2 \u03c4\u2082)\n\ndata Env where\n \u2205 : Env \u2205\n _\u2022_ : \u2200 {\u0393 \u03c4} \u2192 Val \u03c4 \u2192 Env \u0393 \u2192 Env (\u03c4 \u2022 \u0393)\n\n-- Lookup\n\ninfixr 8 _\u22a2_\u2193_ _\u22a2_\u21a6_\n\ndata _\u22a2_\u21a6_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Var \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n this : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {v : Val \u03c4} \u2192\n v \u2022 \u03c1 \u22a2 this \u21a6 v\n that : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 x} {\u03c1 : Env \u0393} {v\u2081 : Val \u03c4\u2081} {v\u2082 : Val \u03c4\u2082} \u2192\n \u03c1 \u22a2 x \u21a6 v\u2082 \u2192\n v\u2081 \u2022 \u03c1 \u22a2 that x \u21a6 v\u2082\n\n-- Reduction\n\ndata _\u22a2_\u2193_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Term \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c1} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n \u03c1 \u22a2 abs t \u2193 \u27e8abs t , \u03c1 \u27e9\n app : \u2200 {\u0393 \u0393\u2032 \u03c4\u2081 \u03c4\u2082 \u03c1 \u03c1\u2032 v\u2082 v\u2032} {t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2082 : Term \u0393 \u03c4\u2081} {t\u2032 : Term (\u03c4\u2081 \u2022 \u0393\u2032) \u03c4\u2082} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 \u27e8abs t\u2032 , \u03c1\u2032 \u27e9 \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n v\u2082 \u2022 \u03c1\u2032 \u22a2 t\u2032 \u2193 v\u2032 \u2192\n \u03c1 \u22a2 app t\u2081 t\u2082 \u2193 v\u2032\n var : \u2200 {\u0393 \u03c4 x} {\u03c1 : Env \u0393} {v : Val \u03c4}\u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u03c1 \u22a2 var x \u2193 v\n\n-- SOUNDNESS of natural semantics\n\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 Env \u0393 \u2192 \u27e6 \u0393 \u27e7\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 \u27e8abs t , \u03c1 \u27e9 \u27e7Val = \u03bb v \u2192 \u27e6 t \u27e7 (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\nmeaningOfEnv : \u2200 {\u0393} \u2192 Meaning (Env \u0393)\nmeaningOfEnv = meaning \u27e6_\u27e7Env\n\nmeaningOfVal : \u2200 {\u03c4} \u2192 Meaning (Val \u03c4)\nmeaningOfVal = meaning \u27e6_\u27e7Val\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = \u2261-refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = \u2261-refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) =\n \u2261-trans\n (\u2261-cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082))\n (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n\n-- WEAKENING\n\n-- Weaken a term to a super context\n\n{-\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken {\u0393\u2081} {\u0393\u2082} (abs {\u03c4\u2081 = \u03c4} t) = abs (weaken {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} t)\nweaken {\u0393\u2081} {\u0393\u2082} (app t\u2081 t\u2082) = app (weaken {\u0393\u2081} {\u0393\u2082} t\u2081) (weaken {\u0393\u2081} {\u0393\u2082} t\u2082)\nweaken {\u0393\u2081} {\u0393\u2082} (var x) = var (lift {\u0393\u2081} {\u0393\u2082} x)\nweaken {\u0393\u2081} {\u0393\u2082} (\u0394 t) = ?\n-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"53075c15ffec0c11417aecdf27b9920b9b88a56c","subject":"otp: +lem-flip$-\u2295","message":"otp: +lem-flip$-\u2295\n","repos":"crypto-agda\/crypto-agda","old_file":"single-bit-one-time-pad.agda","new_file":"single-bit-one-time-pad.agda","new_contents":"module single-bit-one-time-pad where\n\nopen import Function\nopen import Data.Bool.NP as Bool\nopen import Data.Product renaming (map to <_\u00d7_>)\nopen import Data.Nat.NP\nimport Data.Vec.NP as V\nopen V using (Vec; take; drop; drop\u2032; take\u2032; _++_) renaming (swap to vswap)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_; module \u2261-Reasoning)\n\nopen import Data.Bits\nopen import flipbased-implem\nopen import program-distance\n\nK = Bit\nM = Bit\nC = Bit\n\nrecord Adv (S\u2080 S\u2081 S\u2082 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n step\u2081 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2082 : C \u00d7 S\u2081 \u2192 S\u2082\n step\u2083 : S\u2082 \u2192 Bit\n\nrecord Adv\u2081 (S\u2080 S\u2081 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n -- step\u2081 s\u2080 = id , s\u2080\n step\u2082 : C \u00d7 S\u2080 \u2192 S\u2081\n step\u2083 : S\u2081 \u2192 Bit\n\nAdv\u2082 : \u2200 ca \u2192 Set\nAdv\u2082 ca = \u21ba ca (C \u2192 Bit)\n\nmodule Run\u2141\u2032 {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open Adv A\n E : M \u2192 \u21ba 1 C\n E m = toss >>= \u03bb k \u2192 return\u21ba (m xor k)\n run\u2141\u2032 : \u2141? (ca + 1)\n run\u2141\u2032 b = step\u2080 >>= \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n E (m b) >>= \u03bb c \u2192\n return\u21ba (step\u2083 (step\u2082 (c , s\u2081)))}\n\nmodule Run\u2141 {S\u2080 S\u2081 S\u2082 ca} (E : K \u2192 M \u2192 C) (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Adv A\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n let c = E k (m b) in\n step\u2083 (step\u2082 (c , s\u2081))}\n\n run\u2141 : EXP (1 + ca)\n run\u2141 = toss >>= kont\u2080\n\n {- looks wrong\nmodule Run\u2141-Properties {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b k : Bit) where\n open Run\u2141 A\n kont\u2080-not : kont\u2080 b k \u2261 kont\u2080 (not b) (not k)\n kont\u2080-not rewrite xor-not-not b k = {!refl!}\n -}\n\nmodule SymAdv (homPrgDist : HomPrgDist) {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n step\u2081\u2032 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2081\u2032 s\u2080 = case step\u2081 s\u2080 of \u03bb { (m , s\u2081) \u2192 (m \u2218 not , s\u2081) }\n symA : Adv S\u2080 S\u2081 S\u2082 ca\n symA = mk step\u2080 step\u2081\u2032 step\u2082 (not \u2218 step\u2083)\n\n symA\u2032 : Adv S\u2080 S\u2081 S\u2082 ca\n symA\u2032 = mk step\u2080 step\u2081\u2032 step\u2082 step\u2083\n\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n open Run\u2141 _xor_ symA renaming (run\u2141 to runSymA)\n open Run\u2141 _xor_ symA\u2032 renaming (run\u2141 to runSymA\u2032)\n {-\n helper : \u2200 {n} (g\u2080 g\u2081 : EXP n) \u2192 g\u2080 ]-[ g\u2081 \u2192 not\u21ba g\u2080 ]-[ not\u21ba g\u2081\n helper = {!!}\n lem : runA \u21d3 runSymA\n lem A-breaks-E = {!helper (uunSymA\u2032 0b) (runSymA\u2032 1)!}\n where pf : breaks runSymA\n pf = {!!}\n -}\n\nmodule Run\u2141\u2082 {ca} (A : Adv\u2082 ca) (b : Bit) where\n E : Bit \u2192 M \u2192 C\n E k m = m xor k\n\n m : Bit \u2192 Bit\n m = id\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n\n {-\n kont\u2080\u2032 : \u2141? _\n kont\u2080\u2032 k =\n conv-Adv A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n -}\n\n run\u2141\u2082 : EXP (1 + ca)\n run\u2141\u2082 = toss >>= kont\u2080\n\nmodule Run\u2141\u2082-Properties {ca} (A : Adv\u2082 ca) where\n open Run\u2141\u2082 A renaming (run\u2141\u2082 to runA)\n kont\u2080-not : \u2200 b k \u2192 kont\u2080 b k \u2261 kont\u2080 (not b) (not k)\n kont\u2080-not b k rewrite xor-not-not b k = \u2261.refl\n\n open \u2261-Reasoning\n\n lem\u2082 : \u2200 b \u2192 count\u21ba (runA b) \u2261 count\u21ba (runA (not b))\n lem\u2082 b = count\u21ba (runA b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 \u2261.cong\u2082 (_+_ on count\u21ba) (kont\u2080-not b 0b) (kont\u2080-not b 1b) \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (runA (not b)) \u220e\n\n lem\u2083 : Safe\u2141? runA\n lem\u2083 = lem\u2082 0b\n\n -- A specialized version of lem\u2082 (\u2248lem\u2083)\n lem\u2084 : Safe\u2141? (Run\u2141\u2082.run\u2141\u2082 A)\n lem\u2084 = count\u21ba (runA 0b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (kont\u2080 0b 0b) + count\u21ba (kont\u2080 0b 1b)\n \u2261\u27e8 \u2261.cong\u2082 (_+_ on count\u21ba) (kont\u2080-not 0b 0b) (kont\u2080-not 0b 1b) \u27e9\n count\u21ba (kont\u2080 1b 1b) + count\u21ba (kont\u2080 1b 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 1b 1b)) _ \u27e9\n count\u21ba (kont\u2080 1b 0b) + count\u21ba (kont\u2080 1b 1b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (runA 1b) \u220e\n\nconv-Adv : \u2200 {ca S\u2080 S\u2081 S\u2082} \u2192 Adv S\u2080 S\u2081 S\u2082 ca \u2192 Adv\u2082 ca\nconv-Adv A = step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n \u03bb c \u2192 m (step\u2083 (step\u2082 (c , s\u2081)))}\n where open Adv A\n\nmodule Conv-Adv-Props (homPrgDist : HomPrgDist) {ca S\u2080 S\u2081 S\u2082} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n -- open Run\u2141-Properties A\n\n A\u2032 : Adv\u2082 ca\n A\u2032 = conv-Adv A\n open Run\u2141\u2082 A\u2032 using () renaming (kont\u2080 to kont\u2080\u2032; run\u2141\u2082 to runA\u2032)\n\n kont\u2080\u2032\u2032 : \u2200 b k \u2192 EXP ca\n kont\u2080\u2032\u2032 b k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 ((m b) xor k , s\u2081)))}\n\n kont\u2080\u2032\u2032\u2032 : \u2200 b\u2032 \u2192 EXP ca\n kont\u2080\u2032\u2032\u2032 b\u2032 =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 (b\u2032 , s\u2081)))}\n\n {-\n kont\u2032\u2032-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032\u2032 b k)\n kont\u2032\u2032-lem true true = {!!}\n kont\u2032\u2032-lem false true = {!!}\n kont\u2032\u2032-lem true false = {!!}\n kont\u2032\u2032-lem false false = {!!}\n\n kont-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032 b k)\n kont-lem b true = {!!}\n kont-lem b false = {!!}\n\n conv-Adv-lem : runA \u2248\u2141? runA\u2032\n conv-Adv-lem b = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 _+_ (kont-lem b 0b) (kont-lem b 1b) \u27e9\n count\u21ba (kont\u2080\u2032 b 0b) + count\u21ba (kont\u2080\u2032 b 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA\u2032 b) \u220e\n\n conv-Adv-sound : runA \u21d3 runA\u2032\n conv-Adv-sound = ]-[-cong-\u2248\u21ba (conv-Adv-lem 0b) (conv-Adv-lem 1b)\n -}\n-- Cute fact: this is true by computation!\ncount\u21ba-toss->>= : \u2200 {c} (f : \u2141? c) \u2192 count\u21ba (toss >>= f) \u2261 count\u21ba (f 0b) + count\u21ba (f 1b)\ncount\u21ba-toss->>= f = \u2261.refl\n\n{-\nmodule Run\u2141-Properties' {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n lem : count\u21ba (runA b) \u2261 count\u21ba (runA (not b))\n lem = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 (_+_ on count\u21ba) {x = kont\u2080 b 0b} {kont\u2080 (not b) 1b} {kont\u2080 b 1b} {kont\u2080 (not b) 0b} {!!} {!!} \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA (not b)) \u220e\n-}\nopen import program-distance\nopen import Relation.Nullary\n\n\u2047 : \u2200 {n} \u2192 \u21ba n (Bits n)\n\u2047 = random\n\n\nlem'' : \u2200 {k} (f : Bits k \u2192 Bit) \u2192 #\u27e8 f \u2218 tail \u27e9 \u2261 2* #\u27e8 f \u27e9\nlem'' f = \u2261.refl\n\nlem' : \u2200 {k} (f g : Bits k \u2192 Bit) \u2192 #\u27e8 f \u2218 tail \u27e9 \u2261 #\u27e8 g \u2218 tail \u27e9 \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\nlem' f g pf = 2*-inj (\u2261.trans (lem'' f) (\u2261.trans pf (\u2261.sym (lem'' g))))\n\ndrop-tail : \u2200 k {n a} {A : Set a} \u2192 drop (suc k) {n} \u2257 drop k \u2218 tail {A = A}\ndrop-tail k (x \u2237 xs) = V.drop-\u2237 k x xs\n\nlemdrop\u2032 : \u2200 {k n} (f : Bits n \u2192 Bit) \u2192 #\u27e8 f \u2218 drop\u2032 k \u27e9 \u2261 \u27e82^ k * #\u27e8 f \u27e9 \u27e9\nlemdrop\u2032 {zero} f = \u2261.refl\nlemdrop\u2032 {suc k} f = #\u27e8 f \u2218 drop\u2032 k \u2218 tail \u27e9\n \u2261\u27e8 lem'' (f \u2218 drop\u2032 k) \u27e9\n 2* #\u27e8 f \u2218 drop\u2032 k \u27e9\n \u2261\u27e8 \u2261.cong 2*_ (lemdrop\u2032 {k} f) \u27e9\n 2* \u27e82^ k * #\u27e8 f \u27e9 \u27e9 \u220e\n where open \u2261-Reasoning\n\n\n\n\n\n\n\n-- exchange to independant statements\nlem-flip$-\u2295 : \u2200 {m n a} {A : Set a} (f : \u21ba m (A \u2192 Bit)) (x : \u21ba n A) \u2192\n count\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb \u2261 count\u21ba (f \u229b x)\nlem-flip$-\u2295 {m} {n} f x = \u2261.sym (\n count\u21ba fx\n \u2261\u27e8 #-+ {m} {n} (run\u21ba fx) \u27e9\n sum {m} (\u03bb xs \u2192 #\u27e8_\u27e9 {n} (\u03bb ys \u2192 run\u21ba fx (xs ++ ys)))\n \u2261\u27e8 sum-sum {m} {n} (Bool.to\u2115 \u2218 run\u21ba fx) \u27e9\n sum {n} (\u03bb ys \u2192 #\u27e8_\u27e9 {m} (\u03bb xs \u2192 run\u21ba fx (xs ++ ys)))\n \u2261\u27e8 sum-\u2257\u2082 (\u03bb ys xs \u2192 Bool.to\u2115 (run\u21ba fx (xs ++ ys)))\n (\u03bb ys xs \u2192 Bool.to\u2115 (run\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb (ys ++ xs)))\n (\u03bb ys xs \u2192 \u2261.cong Bool.to\u2115 (lem\u2081 xs ys)) \u27e9\n sum {n} (\u03bb ys \u2192 #\u27e8_\u27e9 {m} (\u03bb xs \u2192 run\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb (ys ++ xs)))\n \u2261\u27e8 \u2261.sym (#-+ {n} {m} (run\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb)) \u27e9\n count\u21ba \u27ea flip _$_ \u00b7 x \u00b7 f \u27eb\n \u220e )\n where open \u2261-Reasoning\n fx = f \u229b x\n lem\u2081 : \u2200 xs ys \u2192 run\u21ba f (take m (xs ++ ys)) (run\u21ba x (drop m (xs ++ ys)))\n \u2261 run\u21ba f (drop n (ys ++ xs)) (run\u21ba x (take n (ys ++ xs)))\n lem\u2081 xs ys rewrite V.take-++ m xs ys | V.drop-++ m xs ys\n | V.take-++ n ys xs | V.drop-++ n ys xs = \u2261.refl\n\n\u2248\u1d2c\u2032-toss : \u2200 b \u2192 \u27ea b \u27eb\u1d30 \u27e8xor\u27e9 toss \u2248\u1d2c\u2032 toss\n\u2248\u1d2c\u2032-toss true Adv = \u2115\u00b0.+-comm (count\u21ba (Adv true)) _\n\u2248\u1d2c\u2032-toss false Adv = \u2261.refl\n\n\u2248\u1d2c-toss : \u2200 b \u2192 \u27ea b \u27eb\u1d30 \u27e8xor\u27e9 toss \u2248\u1d2c toss\n\u2248\u1d2c-toss b Adv = \u2248\u1d2c\u2032-toss b (return\u1d30 \u2218 Adv)\n\n-- should be equivalent to #-comm if \u27ea m \u27eb\u1d30 \u27e8\u2295\u27e9 x were convertible to \u27ea _\u2295_ m \u00b7 x \u27eb\n\u2248\u1d2c-\u2047 : \u2200 {k} (m : Bits k) \u2192 \u27ea m \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u2047\n\u2248\u1d2c-\u2047 {zero} _ _ = \u2261.refl\n\u2248\u1d2c-\u2047 {suc k} (h \u2237 m) Adv\n rewrite \u2248\u1d2c-\u2047 m (Adv \u2218 _\u2237_ (h xor 0b))\n | \u2248\u1d2c-\u2047 m (Adv \u2218 _\u2237_ (h xor 1b))\n = \u2248\u1d2c\u2032-toss h (\u03bb x \u2192 \u27ea Adv \u2218 _\u2237_ x \u00b7 \u2047 \u27eb)\n\n\u2248\u1d2c-\u2047\u2082 : \u2200 {k} (m\u2080 m\u2081 : Bits k) \u2192 \u27ea m\u2080 \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea m\u2081 \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2082 {k} m\u2080 m\u2081 = \u2248\u1d2c.trans {k} (\u2248\u1d2c-\u2047 m\u2080) (\u2248\u1d2c.sym {k} (\u2248\u1d2c-\u2047 m\u2081))\n\n\u2248\u1d2c-\u2047\u2083 : \u2200 {k} (m : Bit \u2192 Bits k) (b : Bit) \u2192 \u27ea m b \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea m (not b) \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2083 m b = \u2248\u1d2c-\u2047\u2082 (m b) (m (not b))\n\n\u2248\u1d2c-\u2047\u2084 : \u2200 {k} (m : Bits k \u00d7 Bits k) (b : Bit) \u2192 \u27ea proj m b \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea proj m (not b) \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2084 = \u2248\u1d2c-\u2047\u2083 \u2218 proj\n","old_contents":"module single-bit-one-time-pad where\n\nopen import Function\nopen import Data.Bool.NP\nopen import Data.Product renaming (map to <_\u00d7_>)\nopen import Data.Nat.NP\nimport Data.Vec.NP as V\nopen V using (Vec; take; drop; drop\u2032; take\u2032; _++_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_; module \u2261-Reasoning)\n\nopen import Data.Bits\nopen import flipbased-implem\nopen import program-distance\n\nK = Bit\nM = Bit\nC = Bit\n\nrecord Adv (S\u2080 S\u2081 S\u2082 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n step\u2081 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2082 : C \u00d7 S\u2081 \u2192 S\u2082\n step\u2083 : S\u2082 \u2192 Bit\n\nrecord Adv\u2081 (S\u2080 S\u2081 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n -- step\u2081 s\u2080 = id , s\u2080\n step\u2082 : C \u00d7 S\u2080 \u2192 S\u2081\n step\u2083 : S\u2081 \u2192 Bit\n\nAdv\u2082 : \u2200 ca \u2192 Set\nAdv\u2082 ca = \u21ba ca (C \u2192 Bit)\n\nmodule Run\u2141\u2032 {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open Adv A\n E : M \u2192 \u21ba 1 C\n E m = toss >>= \u03bb k \u2192 return\u21ba (m xor k)\n run\u2141\u2032 : \u2141? (ca + 1)\n run\u2141\u2032 b = step\u2080 >>= \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n E (m b) >>= \u03bb c \u2192\n return\u21ba (step\u2083 (step\u2082 (c , s\u2081)))}\n\nmodule Run\u2141 {S\u2080 S\u2081 S\u2082 ca} (E : K \u2192 M \u2192 C) (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Adv A\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n let c = E k (m b) in\n step\u2083 (step\u2082 (c , s\u2081))}\n\n run\u2141 : EXP (1 + ca)\n run\u2141 = toss >>= kont\u2080\n\n {- looks wrong\nmodule Run\u2141-Properties {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b k : Bit) where\n open Run\u2141 A\n kont\u2080-not : kont\u2080 b k \u2261 kont\u2080 (not b) (not k)\n kont\u2080-not rewrite xor-not-not b k = {!refl!}\n -}\n\nmodule SymAdv (homPrgDist : HomPrgDist) {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n step\u2081\u2032 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2081\u2032 s\u2080 = case step\u2081 s\u2080 of \u03bb { (m , s\u2081) \u2192 (m \u2218 not , s\u2081) }\n symA : Adv S\u2080 S\u2081 S\u2082 ca\n symA = mk step\u2080 step\u2081\u2032 step\u2082 (not \u2218 step\u2083)\n\n symA\u2032 : Adv S\u2080 S\u2081 S\u2082 ca\n symA\u2032 = mk step\u2080 step\u2081\u2032 step\u2082 step\u2083\n\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n open Run\u2141 _xor_ symA renaming (run\u2141 to runSymA)\n open Run\u2141 _xor_ symA\u2032 renaming (run\u2141 to runSymA\u2032)\n {-\n helper : \u2200 {n} (g\u2080 g\u2081 : EXP n) \u2192 g\u2080 ]-[ g\u2081 \u2192 not\u21ba g\u2080 ]-[ not\u21ba g\u2081\n helper = {!!}\n lem : runA \u21d3 runSymA\n lem A-breaks-E = {!helper (uunSymA\u2032 0b) (runSymA\u2032 1)!}\n where pf : breaks runSymA\n pf = {!!}\n -}\n\nmodule Run\u2141\u2082 {ca} (A : Adv\u2082 ca) (b : Bit) where\n E : Bit \u2192 M \u2192 C\n E k m = m xor k\n\n m : Bit \u2192 Bit\n m = id\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n\n {-\n kont\u2080\u2032 : \u2141? _\n kont\u2080\u2032 k =\n conv-Adv A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n -}\n\n run\u2141\u2082 : EXP (1 + ca)\n run\u2141\u2082 = toss >>= kont\u2080\n\nmodule Run\u2141\u2082-Properties {ca} (A : Adv\u2082 ca) where\n open Run\u2141\u2082 A renaming (run\u2141\u2082 to runA)\n kont\u2080-not : \u2200 b k \u2192 kont\u2080 b k \u2261 kont\u2080 (not b) (not k)\n kont\u2080-not b k rewrite xor-not-not b k = \u2261.refl\n\n open \u2261-Reasoning\n\n lem\u2082 : \u2200 b \u2192 count\u21ba (runA b) \u2261 count\u21ba (runA (not b))\n lem\u2082 b = count\u21ba (runA b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 \u2261.cong\u2082 (_+_ on count\u21ba) (kont\u2080-not b 0b) (kont\u2080-not b 1b) \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (runA (not b)) \u220e\n\n lem\u2083 : Safe\u2141? runA\n lem\u2083 = lem\u2082 0b\n\n -- A specialized version of lem\u2082 (\u2248lem\u2083)\n lem\u2084 : Safe\u2141? (Run\u2141\u2082.run\u2141\u2082 A)\n lem\u2084 = count\u21ba (runA 0b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (kont\u2080 0b 0b) + count\u21ba (kont\u2080 0b 1b)\n \u2261\u27e8 \u2261.cong\u2082 (_+_ on count\u21ba) (kont\u2080-not 0b 0b) (kont\u2080-not 0b 1b) \u27e9\n count\u21ba (kont\u2080 1b 1b) + count\u21ba (kont\u2080 1b 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 1b 1b)) _ \u27e9\n count\u21ba (kont\u2080 1b 0b) + count\u21ba (kont\u2080 1b 1b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (runA 1b) \u220e\n\nconv-Adv : \u2200 {ca S\u2080 S\u2081 S\u2082} \u2192 Adv S\u2080 S\u2081 S\u2082 ca \u2192 Adv\u2082 ca\nconv-Adv A = step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n \u03bb c \u2192 m (step\u2083 (step\u2082 (c , s\u2081)))}\n where open Adv A\n\nmodule Conv-Adv-Props (homPrgDist : HomPrgDist) {ca S\u2080 S\u2081 S\u2082} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n -- open Run\u2141-Properties A\n\n A\u2032 : Adv\u2082 ca\n A\u2032 = conv-Adv A\n open Run\u2141\u2082 A\u2032 using () renaming (kont\u2080 to kont\u2080\u2032; run\u2141\u2082 to runA\u2032)\n\n kont\u2080\u2032\u2032 : \u2200 b k \u2192 EXP ca\n kont\u2080\u2032\u2032 b k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 ((m b) xor k , s\u2081)))}\n\n kont\u2080\u2032\u2032\u2032 : \u2200 b\u2032 \u2192 EXP ca\n kont\u2080\u2032\u2032\u2032 b\u2032 =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 (b\u2032 , s\u2081)))}\n\n {-\n kont\u2032\u2032-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032\u2032 b k)\n kont\u2032\u2032-lem true true = {!!}\n kont\u2032\u2032-lem false true = {!!}\n kont\u2032\u2032-lem true false = {!!}\n kont\u2032\u2032-lem false false = {!!}\n\n kont-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032 b k)\n kont-lem b true = {!!}\n kont-lem b false = {!!}\n\n conv-Adv-lem : runA \u2248\u2141? runA\u2032\n conv-Adv-lem b = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 _+_ (kont-lem b 0b) (kont-lem b 1b) \u27e9\n count\u21ba (kont\u2080\u2032 b 0b) + count\u21ba (kont\u2080\u2032 b 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA\u2032 b) \u220e\n\n conv-Adv-sound : runA \u21d3 runA\u2032\n conv-Adv-sound = ]-[-cong-\u2248\u21ba (conv-Adv-lem 0b) (conv-Adv-lem 1b)\n -}\n-- Cute fact: this is true by computation!\ncount\u21ba-toss->>= : \u2200 {c} (f : \u2141? c) \u2192 count\u21ba (toss >>= f) \u2261 count\u21ba (f 0b) + count\u21ba (f 1b)\ncount\u21ba-toss->>= f = \u2261.refl\n\n{-\nmodule Run\u2141-Properties' {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n lem : count\u21ba (runA b) \u2261 count\u21ba (runA (not b))\n lem = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 (_+_ on count\u21ba) {x = kont\u2080 b 0b} {kont\u2080 (not b) 1b} {kont\u2080 b 1b} {kont\u2080 (not b) 0b} {!!} {!!} \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA (not b)) \u220e\n-}\nopen import program-distance\nopen import Relation.Nullary\n\n\u2047 : \u2200 {n} \u2192 \u21ba n (Bits n)\n\u2047 = random\n\n\nlem'' : \u2200 {k} (f : Bits k \u2192 Bit) \u2192 #\u27e8 f \u2218 tail \u27e9 \u2261 2* #\u27e8 f \u27e9\nlem'' f = \u2261.refl\n\nlem' : \u2200 {k} (f g : Bits k \u2192 Bit) \u2192 #\u27e8 f \u2218 tail \u27e9 \u2261 #\u27e8 g \u2218 tail \u27e9 \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\nlem' f g pf = 2*-inj (\u2261.trans (lem'' f) (\u2261.trans pf (\u2261.sym (lem'' g))))\n\ndrop-tail : \u2200 k {n a} {A : Set a} \u2192 drop (suc k) {n} \u2257 drop k \u2218 tail {A = A}\ndrop-tail k (x \u2237 xs) = V.drop-\u2237 k x xs\n\nlemdrop\u2032 : \u2200 {k n} (f : Bits n \u2192 Bit) \u2192 #\u27e8 f \u2218 drop\u2032 k \u27e9 \u2261 \u27e82^ k * #\u27e8 f \u27e9 \u27e9\nlemdrop\u2032 {zero} f = \u2261.refl\nlemdrop\u2032 {suc k} f = #\u27e8 f \u2218 drop\u2032 k \u2218 tail \u27e9\n \u2261\u27e8 lem'' (f \u2218 drop\u2032 k) \u27e9\n 2* #\u27e8 f \u2218 drop\u2032 k \u27e9\n \u2261\u27e8 \u2261.cong 2*_ (lemdrop\u2032 {k} f) \u27e9\n 2* \u27e82^ k * #\u27e8 f \u27e9 \u27e9 \u220e\n where open \u2261-Reasoning\n\nvswap : \u2200 m {n} {a} {A : Set a} \u2192 Vec A (m + n) \u2192 Vec A (n + m)\nvswap m xs = drop\u2032 m xs ++ take\u2032 m xs\n\n\n\n\n\n\n\u2248\u1d2c\u2032-toss : \u2200 b \u2192 \u27ea b \u27eb\u1d30 \u27e8xor\u27e9 toss \u2248\u1d2c\u2032 toss\n\u2248\u1d2c\u2032-toss true Adv = \u2115\u00b0.+-comm (count\u21ba (Adv true)) _\n\u2248\u1d2c\u2032-toss false Adv = \u2261.refl\n\n\u2248\u1d2c-toss : \u2200 b \u2192 \u27ea b \u27eb\u1d30 \u27e8xor\u27e9 toss \u2248\u1d2c toss\n\u2248\u1d2c-toss b Adv = \u2248\u1d2c\u2032-toss b (return\u1d30 \u2218 Adv)\n\n-- should be equivalent to #-comm if \u27ea m \u27eb\u1d30 \u27e8\u2295\u27e9 x were convertible to \u27ea _\u2295_ m \u00b7 x \u27eb\n\u2248\u1d2c-\u2047 : \u2200 {k} (m : Bits k) \u2192 \u27ea m \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u2047\n\u2248\u1d2c-\u2047 {zero} _ _ = \u2261.refl\n\u2248\u1d2c-\u2047 {suc k} (h \u2237 m) Adv\n rewrite \u2248\u1d2c-\u2047 m (Adv \u2218 _\u2237_ (h xor 0b))\n | \u2248\u1d2c-\u2047 m (Adv \u2218 _\u2237_ (h xor 1b))\n = \u2248\u1d2c\u2032-toss h (\u03bb x \u2192 \u27ea Adv \u2218 _\u2237_ x \u00b7 \u2047 \u27eb)\n\n\u2248\u1d2c-\u2047\u2082 : \u2200 {k} (m\u2080 m\u2081 : Bits k) \u2192 \u27ea m\u2080 \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea m\u2081 \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2082 {k} m\u2080 m\u2081 = \u2248\u1d2c.trans {k} (\u2248\u1d2c-\u2047 m\u2080) (\u2248\u1d2c.sym {k} (\u2248\u1d2c-\u2047 m\u2081))\n\n\u2248\u1d2c-\u2047\u2083 : \u2200 {k} (m : Bit \u2192 Bits k) (b : Bit) \u2192 \u27ea m b \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea m (not b) \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2083 m b = \u2248\u1d2c-\u2047\u2082 (m b) (m (not b))\n\n\u2248\u1d2c-\u2047\u2084 : \u2200 {k} (m : Bits k \u00d7 Bits k) (b : Bit) \u2192 \u27ea proj m b \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea proj m (not b) \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2084 = \u2248\u1d2c-\u2047\u2083 \u2218 proj\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"17f8e744461ddd3f5ba4efa2b67bc86b88e65197","subject":"ExplicitNils: stability of terms, reworked (Reminder: Stability of terms means that the value denoted by a term is unchanging if all its free variables are unchanging in an environment.)","message":"ExplicitNils: stability of terms, reworked\n(Reminder: Stability of terms means that the value denoted by\na term is unchanging if all its free variables are unchanging\nin an environment.)\n\nOld-commit-hash: 82fcad297e8d931c8f855fa0ba0c31f1db0f5d3f\n","repos":"inc-lc\/ilc-agda","old_file":"experimental\/ExplicitNil.agda","new_file":"experimental\/ExplicitNil.agda","new_contents":"{-\nCommunicate to derivatives that changes to certain arguments\nare always nil (i. e., certain arguments are stable).\n-}\n\nmodule ExplicitNil where\n\nopen import TaggedDeltaTypes\n\nopen import Data.Nat\nopen import Data.Bool\nopen import Data.Product hiding (map)\nopen import Data.Unit using (\u22a4 ; tt)\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\n-----------------------------------\n-- Describing the lack of change --\n-----------------------------------\n\ndata Args : (\u03c4 : Type) \u2192 Set where\n \u2205-nat : Args nats\n \u2205-bag : Args bags\n alter : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192 (args : Args \u03c4\u2082) \u2192 Args (\u03c4\u2081 \u21d2 \u03c4\u2082)\n abide : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192 (args : Args \u03c4\u2082) \u2192 Args (\u03c4\u2081 \u21d2 \u03c4\u2082)\n\n-- Totally like subcontext relation _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set\ndata Vars : Context \u2192 Set where\n \u2205 : Vars \u2205\n alter : \u2200 {\u03c4 \u0393} \u2192 Vars \u0393 \u2192 Vars (\u03c4 \u2022 \u0393)\n abide : \u2200 {\u03c4 \u0393} \u2192 Vars \u0393 \u2192 Vars (\u03c4 \u2022 \u0393)\n\nstableVar : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Bool\nstableVar this (abide _) = true\nstableVar this (alter _) = false\nstableVar (that x) (abide vars) = stableVar x vars\nstableVar (that x) (alter vars) = stableVar x vars\n\n-- A term is stable if all its free variables are unchanging\n-- Alternative definition:\n--\n-- stable t vars = isNil t (derive t)\n--\nstable : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Bool\nstable (nat n) vars = true\nstable (bag b) vars = true\nstable (var x) vars = stableVar x vars\nstable (abs t) vars = stable t (abide vars)\nstable (app f x) vars = stable f vars \u2227 stable x vars\nstable (add m n) vars = stable m vars \u2227 stable n vars\nstable (map f b) vars = stable f vars \u2227 stable b vars\n\nexpect-volatility : {\u03c4 : Type} \u2192 Args \u03c4\nexpect-volatility {\u03c4\u2081 \u21d2 \u03c4\u2082} = alter expect-volatility\nexpect-volatility {nats} = \u2205-nat\nexpect-volatility {bags} = \u2205-bag\n\nproj-\u2227 : \u2200 {a b} \u2192 a \u2227 b \u2261 true \u2192 a \u2261 true \u00d7 b \u2261 true\nproj-\u2227 {false} {_} ()\nproj-\u2227 {true} {false} ()\nproj-\u2227 {true} {true} truth = refl , refl\n\n--------------------------\n-- Optimized derivation --\n--------------------------\n\nderive' : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 \u03c4 \u2192 {args : Args \u03c4} \u2192 {vars : Vars \u0393} \u2192 \u0394Term \u0393 \u03c4\n\nderive' (nat n) = derive (nat n)\nderive' (bag b) = derive (bag b)\nderive' (var x) = derive (var x)\n\nderive' (add s t) {\u2205-nat} {vars} =\n \u0394add (derive' s {\u2205-nat} {vars}) (derive' t {\u2205-nat} {vars})\n\nderive' (abs t) {alter args} {vars} = \u0394abs (derive' t {args} {alter vars})\nderive' (abs t) {abide args} {vars} = \u0394abs (derive' t {args} {abide vars})\n\nderive' (app s t) {args} {vars} =\n if stable t vars\n then \u0394app (derive' s {abide args} {vars})\n t (derive' t {expect-volatility} {vars})\n else \u0394app (derive' s {alter args} {vars})\n t (derive' t {expect-volatility} {vars})\n\nderive' (map s t) {\u2205-bag} {vars} =\n if stable s vars\n then \u0394map\u2081 s (derive' t {\u2205-bag} {vars})\n else \u0394map\u2080 s (derive' s {abide \u2205-nat} {vars})\n t (derive' t {\u2205-bag} {vars})\n\n-----------------------------------------------------\n-- A program equivalence preserved by optimization --\n-----------------------------------------------------\n\n-- A description of variables is honest w.r.t. a \u0394-environment\n-- if every variable described as stable receives the nil change.\ndata Honest : \u2200 {\u0393} \u2192 \u0394Env \u0393 \u2192 Vars \u0393 \u2192 Set where\n clearly : Honest \u2205 \u2205\n alter : \u2200 {\u0393 \u03c4} {v : \u27e6 \u03c4 \u27e7} {dv R[v,dv] vars \u03c1} \u2192\n Honest {\u0393} \u03c1 vars \u2192\n Honest {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) (alter vars)\n abide : \u2200 {\u0393 \u03c4} {v : \u27e6 \u03c4 \u27e7} {dv R[v,dv] vars \u03c1} \u2192\n v \u2295 dv \u2261 v \u2192\n Honest {\u0393} \u03c1 vars \u2192\n Honest {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) (abide vars)\n\n-- Two \u0394-values are close enough w.r.t. a set of arguments if they\n-- behave the same when fully applied (cf. extensionality) given\n-- that each argument declared stable receives the nil change.\n--\n-- du \u2248 dv wrt args\n--\nclose-enough : \u2200 {\u03c4 : Type} \u2192 \u0394Val \u03c4 \u2192 \u0394Val \u03c4 \u2192 Args \u03c4 \u2192 Set\nclose-enough {nats} du dv args = du \u2261 dv -- extensionally\nclose-enough {bags} du dv args = du \u2261 dv -- literally\nclose-enough {\u03c3 \u21d2 \u03c4} df dg (alter args) = \u2200 {v dv R[v,dv]} \u2192\n close-enough (df v dv R[v,dv]) (dg v dv R[v,dv]) args\nclose-enough {\u03c3 \u21d2 \u03c4} df dg (abide args) = \u2200 {v dv R[v,dv]} \u2192\n v \u2295 dv \u2261 v \u2192 close-enough (df v dv R[v,dv]) (dg v dv R[v,dv]) args\n\nsyntax close-enough du dv args = du \u2248 dv wrt args\n\next\u00b3 : \u2200\n {A : Set}\n {B : A \u2192 Set}\n {C : (a : A) \u2192 B a \u2192 Set }\n {D : (a : A) \u2192 (b : B a) \u2192 C a b \u2192 Set}\n {f g : (a : A) \u2192 (b : B a) \u2192 (c : C a b) \u2192 D a b c} \u2192\n ((a : A) (b : B a) (c : C a b) \u2192 f a b c \u2261 g a b c) \u2192 f \u2261 g\n\next\u00b3 fabc=gabc = ext (\u03bb a \u2192 ext (\u03bb b \u2192 ext (\u03bb c \u2192 fabc=gabc a b c)))\n where ext = extensionality\n\n\u2261to\u2248 : \u2200 {\u03c4 args} {df dg : \u0394Val \u03c4} \u2192\n df \u2261 dg \u2192 df \u2248 dg wrt args\n\n\u2261to\u2248 {nats} df\u2261dg = df\u2261dg\n\u2261to\u2248 {bags} df\u2261dg = df\u2261dg\n\u2261to\u2248 {\u03c3 \u21d2 \u03c4} {alter args} df\u2261dg = \u03bb {v} {dv} {R[v,dv]} \u2192\n \u2261to\u2248 (cong (\u03bb hole \u2192 hole v dv R[v,dv]) df\u2261dg)\n\u2261to\u2248 {\u03c3 \u21d2 \u03c4} {abide args} df\u2261dg = \u03bb {v} {dv} {R[v,dv]} v\u2295dv=v \u2192\n \u2261to\u2248 (cong (\u03bb hole \u2192 hole v dv R[v,dv]) df\u2261dg)\n\n\u2248to\u2261 : \u2200 {\u03c4} {df dg : \u0394Val \u03c4} \u2192\n df \u2248 dg wrt (expect-volatility {\u03c4}) \u2192 df \u2261 dg\n\n\u2248to\u2261 {nats} df\u2248dg = df\u2248dg\n\u2248to\u2261 {bags} df\u2248dg = df\u2248dg\n\u2248to\u2261 {\u03c3 \u21d2 \u03c4} {df} {dg} df\u2248dg =\n ext\u00b3 (\u03bb v dv R[v,dv] \u2192 \u2248to\u2261 {\u03c4} (df\u2248dg {v} {dv} {R[v,dv]}))\n\n------------------------\n-- Stability of terms --\n------------------------\n\n-- A variable does not change if its value is unchanging.\n\nstabilityVar : \u2200 {\u03c4 \u0393} {x : Var \u0393 \u03c4} {vars} \u2192\n (S : stableVar x vars \u2261 true) \u2192 \u2200 {\u03c1 : \u0394Env \u0393}\n (H : Honest \u03c1 vars) \u2192\n \u27e6 x \u27e7 (ignore \u03c1) \u2295 \u27e6 x \u27e7\u0394Var \u03c1 \u2261 \u27e6 x \u27e7 (ignore \u03c1)\n\nstabilityVar {x = this} {alter vars} () _\nstabilityVar {x = this} {abide vars} refl (abide proof _) = proof\nstabilityVar {x = that y} {alter vars} S (alter H) =\n stabilityVar {x = y} {vars} S H\nstabilityVar {x = that y} {abide vars} S (abide _ H) =\n stabilityVar {x = y} {vars} S H\n\n-- A term does not change if its free variables are unchanging.\n\nstability : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {vars} \u2192\n (S : stable t vars \u2261 true) \u2192 \u2200 {\u03c1 : \u0394Env \u0393}\n (H : Honest \u03c1 vars) \u2192\n \u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n \u2261 \u27e6 t \u27e7 (ignore \u03c1)\n\nstability {t = nat n} _ _ = refl\nstability {t = bag b} _ _ = b++\u2205=b\nstability {t = var x} {vars} S H = stabilityVar {x = x} {vars} S H\n\nstability {t = abs t} {vars} S {\u03c1} H = extensionality\n (\u03bb w \u2192 stability {t = t} {abide vars} S (abide v\u2295[u\u229dv]=u H))\n\nstability {t = app s t} {vars} S {\u03c1} H =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n v = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n dv = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n Ss , St = proj-\u2227 S\n in\n begin\n f v \u2295 df v dv (validity {t = t})\n \u2261\u27e8 sym (corollary s t) \u27e9\n (f \u2295 df) (v \u2295 dv)\n \u2261\u27e8 stability {t = s} Ss H \u27e8$\u27e9 stability {t = t} St H \u27e9\n f v\n \u220e where open \u2261-Reasoning\n\nstability {t = add s t} {vars} S {\u03c1} H =\n let\n Ss , St = proj-\u2227 S\n in cong\u2082 _+_ (stability {t = s} Ss H) (stability {t = t} St H)\n\nstability {t = map s t} {vars} S {\u03c1} H =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n db = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n Ss , St = proj-\u2227 S\n map = mapBag\n in\n begin\n map f b \u2295 (map (f \u2295 df) (b \u2295 db) \u229d map f b)\n \u2261\u27e8 b++[d\\\\b]=d \u27e9\n map (f \u2295 df) (b \u2295 db)\n \u2261\u27e8 cong\u2082 map (stability {t = s} Ss H) (stability {t = t} St H) \u27e9\n map f b\n \u220e where open \u2261-Reasoning\n\n\n","old_contents":"{-\nCommunicate to derivatives that changes to certain arguments\nare always nil (i. e., certain arguments are stable).\n-}\n\nmodule ExplicitNil where\n\nopen import TaggedDeltaTypes\n\nopen import Data.Bool\nopen import Data.Product hiding (map)\nopen import Data.Nat using (\u2115)\nopen import Data.Unit using (\u22a4 ; tt)\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\n-- Debug tool\nabsurd! : \u2200 {A B : Set} \u2192 A \u2192 A \u2192 B \u2192 B\nabsurd! _ _ b = b\n\n-- A term is stable if all its free variables are unchanging\n-- Alternative definition:\n--\n-- stable t vars = isNil t (derive t) bzgl. vars\n\nstableVar : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Bool\nstableVar this (abide _) = true\nstableVar this (alter _) = false\nstableVar (that x) (abide vars) = stableVar x vars\nstableVar (that x) (alter vars) = stableVar x vars\n\nstable : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Vars \u0393 \u2192 Bool\nstable (nat n) vars = true\nstable (bag b) vars = true\nstable (var x) vars = stableVar x vars\nstable (abs t) vars = stable t (abide vars)\nstable (app f x) vars = stable f vars \u2227 stable x vars\nstable (add m n) vars = stable m vars \u2227 stable n vars\nstable (map f b) vars = stable f vars \u2227 stable b vars\n\n-- Optimized derivation\n\nderive' : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 \u03c4 \u2192 {args : Args \u03c4} \u2192 {vars : Vars \u0393} \u2192 \u0394-Term (\u0394 \u0393) (\u0394 \u03c4)\n\nvalidity' : \u2200 {\u03c4 \u0393 vars}\n {t : Term \u0393 \u03c4}\n {\u03c1 : \u0394-Env \u0393} {honesty : Honest \u03c1 vars} \u2192\n valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive' t {fickle-args} {vars} \u27e7 \u03c1)\n\nderive' (nat n) = derive (nat n)\nderive' (bag b) = derive (bag b)\nderive' (var x) = derive (var x)\n\nderive' (add s t) {\u2205-nat} {vars} =\n \u0394add (derive' s {\u2205-nat} {vars}) (derive' t {\u2205-nat} {vars})\n\nderive' (map s t) {\u2205-bag} {vars} =\n if stable s vars\n then \u0394map\u2081 s (derive' t {\u2205-bag} {vars})\n else \u0394map\u2080 s (derive' s {abide \u2205-nat} {vars})\n t (derive' t {\u2205-bag} {vars})\n\nderive' (app s t) {args} {vars} =\n if stable t vars\n then \u0394app (derive' s {abide args} {vars})\n t (derive' t {fickle-args} {vars})\n (\u03bb {\u03c1 vars honesty} \u2192\n validity' {vars = vars} {t = t} {\u03c1}\n {honesty = ?})\n else \u0394app (derive' s {alter args} {vars})\n t (derive' t {fickle-args} {vars}) validity'\n\nderive' t = {!!}\n\nvalidity' = {!!}\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"26d1b2d6a8dde3981ebe9617d7c3e4cf600a9368","subject":"patching type-assignment-unicity after rewrite","message":"patching type-assignment-unicity after rewrite\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"type-assignment-unicity.agda","new_file":"type-assignment-unicity.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nmodule type-assignment-unicity where\n type-assignment-unicity : {\u0393 : tctx} {d : dhexp} {\u03c4' \u03c4 : htyp} {\u0394 : hctx} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u03c4 == \u03c4'\n type-assignment-unicity TAConst TAConst = refl\n type-assignment-unicity {\u0393 = \u0393} (TAVar x\u2081) (TAVar x\u2082) = ctxunicity {\u0393 = \u0393} x\u2081 x\u2082\n type-assignment-unicity (TALam d1) (TALam d2)\n with type-assignment-unicity d1 d2\n ... | refl = refl\n type-assignment-unicity (TAAp x x\u2081) (TAAp y y\u2081)\n with type-assignment-unicity x y\n ... | refl = refl\n type-assignment-unicity (TAEHole {\u0394 = \u0394} x y) (TAEHole x\u2081 x\u2082)\n with ctxunicity {\u0393 = \u0394} x x\u2081\n ... | refl = refl\n type-assignment-unicity (TANEHole {\u0394 = \u0394} x d1 y) (TANEHole x\u2081 d2 x\u2082)\n with ctxunicity {\u0393 = \u0394} x\u2081 x\n ... | refl = refl\n type-assignment-unicity (TACast d1 x) (TACast d2 x\u2081)\n with type-assignment-unicity d1 d2\n ... | refl = refl\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nmodule type-assignment-unicity where\n type-assignment-unicity : {\u0393 : tctx} {d : dhexp} {\u03c4' \u03c4 : htyp} {\u0394 : hctx} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u03c4 == \u03c4'\n type-assignment-unicity TAConst TAConst = refl\n type-assignment-unicity {\u0393 = \u0393} (TAVar x\u2081) (TAVar x\u2082) = ctxunicity {\u0393 = \u0393} x\u2081 x\u2082\n type-assignment-unicity (TALam d1) (TALam d2)\n with type-assignment-unicity d1 d2\n ... | refl = refl\n type-assignment-unicity (TAAp d3 MAHole d4) (TAAp d5 MAHole d6) = refl\n type-assignment-unicity (TAAp d3 MAHole d4) (TAAp d5 MAArr d6)\n with type-assignment-unicity d3 d5\n ... | ()\n type-assignment-unicity (TAAp d3 MAArr d4) (TAAp d5 MAHole d6)\n with type-assignment-unicity d3 d5\n ... | ()\n type-assignment-unicity (TAAp d3 MAArr d4) (TAAp d5 MAArr d6)\n with type-assignment-unicity d3 d5 | type-assignment-unicity d4 d6\n ... | refl | refl = refl\n type-assignment-unicity (TAEHole {\u0394 = \u0394} x y) (TAEHole x\u2081 x\u2082)\n with ctxunicity {\u0393 = \u0394} x x\u2081\n ... | refl = refl\n type-assignment-unicity (TANEHole {\u0394 = \u0394} x d1 y) (TANEHole x\u2081 d2 x\u2082)\n with ctxunicity {\u0393 = \u0394} x\u2081 x\n ... | refl = refl\n type-assignment-unicity (TACast d1 x) (TACast d2 x\u2081)\n with type-assignment-unicity d1 d2\n ... | refl = refl\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b04d285696190406b19d3bead396447ab71f875b","subject":"removing a postulate; feels good, man","message":"removing a postulate; feels good, man\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"finality.agda","new_file":"finality.agda","new_contents":"open import Prelude\nopen import core\n\nopen import progress-checks\n\nmodule finality where\n finality : \u2200{d d'} \u2192 d final \u2192 d \u21a6* d' \u2192 d == d'\n finality fin MSRefl = refl\n finality fin (MSStep x ms) = abort (final-not-step fin (_ , x))\n","old_contents":"open import Prelude\nopen import core\n\nmodule finality where\n -- todo: this will come from progress checks, once that's all squared away\n postulate\n fs : \u2200{d} \u2192 d final \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 \u22a5\n\n finality : \u2200{d d'} \u2192 d final \u2192 d \u21a6* d' \u2192 d == d'\n finality fin MSRefl = refl\n finality fin (MSStep x ms) = abort (fs fin (_ , x))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"abfc56ed18fd208d29e65400cf629b952508aa35","subject":"Reordered a module.","message":"Reordered a module.\n\nIgnore-this: 56bcd35741bf120b61fd36f3a9149101\n\ndarcs-hash:20110301052416-3bd4e-83667c713d5a74ccfb53ef3ed0b22dd170580915.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Data\/Nat\/Induction\/Acc\/WellFoundedInduction.agda","new_file":"src\/FOTC\/Data\/Nat\/Induction\/Acc\/WellFoundedInduction.agda","new_contents":"----------------------------------------------------------------------------\n-- Well-founded induction on the natural numbers\n----------------------------------------------------------------------------\n\nmodule FOTC.Data.Nat.Induction.Acc.WellFoundedInduction where\n\nopen import FOTC.Base\n\nopen import Common.Function\n\nopen import FOTC.Data.Nat.Induction.Acc.WellFounded\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Type\n\n------------------------------------------------------------------------------\n-- The relation LT is well-founded.\nmodule WF-LT\u2081\n ( x<0\u2192\u22a5 : \u2200 {n} \u2192 N n \u2192 \u00ac (LT n zero)\n ; Sx Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n\n\n--********************************\n-- Typed expressions\n--********************************\n\n-- Fix menu:\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty), -- Val t\n (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void)))\n\n-- Indexed menu:\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\n\n-- Expression:\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), \n Void))\n\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void)\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void )\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (exprFree ** Val)\n\n--********************************\n-- Closed term' evaluation\n--********************************\n\neval' : {ty : Type} -> IMu closeTerm' ty -> Val ty\neval' {ty} term = cata Type closeTerm' Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm' ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n\n--********************************\n-- Open terms\n--********************************\n\ndata Vec (A : Set) : Nat -> Set where\n vnil : Vec A ze\n vcons : {n : Nat} -> A -> Vec A n -> Vec A (su n)\n\ndata Fin : Nat -> Set where\n fze : {n : Nat} -> Fin (su n)\n fsu : {n : Nat} -> Fin n -> Fin (su n)\n\nContext : Nat -> Set\nContext n = Vec (Sigma Type (\\ty -> IMu closeTerm' ty)) n\n\ntypeAt : {n : Nat}(c : Context n) -> Fin n -> Type\ntypeAt {ze} c ()\ntypeAt {.(su n)} (vcons x xs) (fze {n}) = fst x\ntypeAt {.(su n)} (vcons x xs) (fsu {n} y) = typeAt xs y\n\nlookup : {n : Nat}(c : Context n)(i : Fin n) -> IMu closeTerm' (typeAt c i)\nlookup {ze} c ()\nlookup {su _} (vcons x _) fze = snd x\nlookup {su _} (vcons _ xs) (fsu y) = lookup xs y\n\n\nVar : {n : Nat} -> Context n -> Type -> Set\nVar {n} c ty = Sigma (Fin n) (\\i -> typeAt c i == ty)\n\nopenTerm : {n : Nat} -> Context n -> Type -> IDesc Type\nopenTerm c = toIDesc Type (exprFree ** (\\ty -> Val ty + Var c ty))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : {n : Nat}\n {context : Context n}\n (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm' ty\ndischarge ty (l value) = con (EZe , value)\ndischarge {n} {c} ty (r variable) = subst (cong (IMu closeTerm') (snd variable)) (lookup c (fst variable))\n\nsubstExpr : {n : Nat}\n {ty : Type}\n (context : Context n)\n (sigma : (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm' ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm' ty\nsubstExpr {n} {ty} c sig term = \n substI (\\ty -> Val ty + Var c ty) Val exprFree sig ty term\n\nevalOpen : {n : Nat}{ty : Type}\n (context : Context n) ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen context tm = eval' (substExpr context discharge tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- V 0 :-> true, V 1 :-> 2\ntestContext : Context _\ntestContext = vcons (bool , con (EZe , true )) (vcons (nat , con (EZe , su (su ze)) ) vnil)\n\n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , r ( fsu fze , refl ) )\n\ntestSubst1 : IMu closeTerm' nat\ntestSubst1 = substExpr testContext \n discharge \n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con (ESu (ESu EZe) , (con (EZe , l (su ze)) , con ( EZe , r (fsu fze , refl) )) )\n\ntestSubst2 : IMu closeTerm' nat\ntestSubst2 = substExpr testContext \n discharge \n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext test2\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu EZe , (con (EZe , r (fze , refl)) ,\n (con (EZe , r (fsu fze , refl)) ,\n con (EZe , l ze))))\n\ntestSubst3 : IMu closeTerm' nat\ntestSubst3 = substExpr testContext \n discharge \n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext test3","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n\n\n--********************************\n-- Typed expressions\n--********************************\n\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty),\n (prod (var bool) (prod (var ty) (var ty)), \n Void)))\n\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n\n--********************************\n-- Open terms\n--********************************\n\nVar : EnumU -> Type -> Set\nVar dom _ = EnumT dom\n\nopenTerm : EnumU -> Type -> IDesc Type\nopenTerm dom = exprIDesc (\\ty -> Val ty + Var dom ty) expr\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm ty\ndischarge ty vars context (l value) = con ( EZe , value )\ndischarge ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm ty) context variable \n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), \n Void))\n\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void)\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void )\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (exprFree ** Val)\n\nopenTerm' : EnumU -> Type -> IDesc Type\nopenTerm' dom = toIDesc Type (exprFree ** (\\ty -> Val ty + Var dom ty))\n\n--********************************\n-- Evaluation of open terms'\n--********************************\n\ndischarge' : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm' ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm' ty\ndischarge' ty vars context (l value) = con (EZe , value) \ndischarge' ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm' ty) context variable\n\nchooseDom : (ty : Type)(domNat domBool : EnumU) -> EnumU\nchooseDom nat domNat _ = domNat\nchooseDom bool _ domBool = domBool\n\nchooseGamma : (ty : Type)\n (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool)) ->\n spi dom (\\_ -> IMu closeTerm' ty)\nchooseGamma nat dom gammaNat gammaBool = gammaNat\nchooseGamma bool dom gammaNat gammaBool = gammaBool\n\nsubstExpr : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (sigma : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var dom ty) ->\n IMu closeTerm' ty)\n (ty : Type) ->\n IMu (openTerm' dom) ty ->\n IMu closeTerm' ty\nsubstExpr dom gammaNat gammaBool sig ty term = \n substI (\\ty -> Val ty + Var dom ty)\n Val\n exprFree\n (sig dom gammaNat gammaBool)\n ty\n term\n\n\nsigmaExpr : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var dom ty) ->\n IMu closeTerm' ty\nsigmaExpr dom gammaNat gammaBool ty v = \n discharge' ty\n dom\n (chooseGamma ty dom gammaNat gammaBool)\n v\n\ntest : IMu (openTerm' (consE (consE nilE))) nat ->\n IMu closeTerm' nat\ntest term = substExpr (consE (consE nilE)) \n ( con ( EZe , ze ) , ( con (EZe , su ze ) , Void )) \n ( con ( EZe , true ) , ( con ( EZe , false ) , Void ))\n sigmaExpr nat term\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a0c26d1d2731a3ccca7b00c77068c7bb3491ecab","subject":"FiniteField.JS: rename \u2124q to \ud835\udd3d","message":"FiniteField.JS: rename \u2124q to \ud835\udd3d\n","repos":"crypto-agda\/crypto-agda","old_file":"FiniteField\/JS.agda","new_file":"FiniteField\/JS.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Function.NP\nopen import FFI.JS using (\ud835\udfd9; Number; Bool; true; false; String; warn-check; check; trace-call; _++_)\nopen import FFI.JS.BigI\nopen import Data.List.Base hiding (sum; _++_)\n{-\nopen import Algebra.Raw\nopen import Algebra.Field\n-}\n\nmodule FiniteField.JS (q : BigI) where\n\nabstract\n \ud835\udd3d : Set\n \ud835\udd3d = BigI\n\n private\n mod-q : BigI \u2192 \ud835\udd3d\n mod-q x = mod x q\n\n check' : {A : Set}(pred : Bool)(errmsg : \ud835\udfd9 \u2192 String)(input : A) \u2192 A\n -- check' = warn-check\n check' = check\n\n -- There is two ways to go from BigI to \u2124q: check and mod-q\n -- Use check for untrusted input data and mod-q for internal\n -- computation.\n fromBigI : BigI \u2192 \ud835\udd3d\n fromBigI = -- trace-call \"BigI\u25b9\u2124q \"\n \u03bb x \u2192\n (check' (x I 0I) (\u03bb _ \u2192 \"Should be non zero\") x\n\n repr : \ud835\udd3d \u2192 BigI\n repr x = x\n\n 0# 1# : \ud835\udd3d\n 0# = 0I\n 1# = 1I\n\n 1\/_ : Op\u2081 \ud835\udd3d\n 1\/ x = modInv (check-non-zero x) q\n\n _^_ : Op\u2082 \ud835\udd3d\n x ^ y = modPow (repr x) (repr y) q\n\n_+_ _\u2212_ _*_ _\/_ : Op\u2082 \ud835\udd3d\n\nx + y = mod-q (add (repr x) (repr y))\nx \u2212 y = mod-q (subtract (repr x) (repr y))\nx * y = mod-q (multiply (repr x) (repr y))\nx \/ y = x * 1\/ y\n\n0\u2212_ : Op\u2081 \ud835\udd3d\n0\u2212 x = mod-q (negate (repr x))\n\n_==_ : (x y : \ud835\udd3d) \u2192 Bool\nx == y = equals (repr x) (repr y)\n\nsum prod : List \ud835\udd3d \u2192 \ud835\udd3d\nsum = foldr _+_ 0#\nprod = foldr _*_ 1#\n\n{-\n+-mon-ops : Monoid-Ops \ud835\udd3d\n+-mon-ops = _+_ , 0#\n\n+-grp-ops : Group-Ops \ud835\udd3d\n+-grp-ops = +-mon-ops , 0\u2212_\n\n*-mon-ops : Monoid-Ops \ud835\udd3d\n*-mon-ops = _*_ , 1#\n\n*-grp-ops : Group-Ops \ud835\udd3d\n*-grp-ops = *-mon-ops , 1\/_\n\nfld-ops : Field-Ops \ud835\udd3d\nfld-ops = +-grp-ops , *-grp-ops\n\npostulate\n fld-struct : Field-Struct fld-ops\n\nfld : Field \ud835\udd3d\nfld = fld-ops , fld-struct\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Function.NP\nopen import FFI.JS using (Number; Bool; true; false; String; warn-check; check; trace-call; _++_)\nopen import FFI.JS.BigI\nopen import Data.List.Base hiding (sum; _++_)\n{-\nopen import Algebra.Raw\nopen import Algebra.Field\n-}\n\nmodule FiniteField.JS (q : BigI) where\n\nabstract\n \u2124q : Set\n \u2124q = BigI\n\n private\n mod-q : BigI \u2192 \u2124q\n mod-q x = mod x q\n\n -- There is two ways to go from BigI to \u2124q: check and mod-q\n -- Use check for untrusted input data and mod-q for internal\n -- computation.\n BigI\u25b9\u2124q : BigI \u2192 \u2124q\n BigI\u25b9\u2124q = -- trace-call \"BigI\u25b9\u2124q \"\n \u03bb x \u2192\n mod-q\n (warn-check (x I 0I) (\u03bb _ \u2192 \"Should be non zero\") x\n\n repr : \u2124q \u2192 BigI\n repr x = x\n\n 0# 1# : \u2124q\n 0# = 0I\n 1# = 1I\n\n 1\/_ : Op\u2081 \u2124q\n 1\/ x = modInv (check-non-zero x) q\n\n _^_ : Op\u2082 \u2124q\n x ^ y = modPow (repr x) (repr y) q\n\n_+_ _\u2212_ _*_ _\/_ : Op\u2082 \u2124q\n\nx + y = mod-q (add (repr x) (repr y))\nx \u2212 y = mod-q (subtract (repr x) (repr y))\nx * y = mod-q (multiply (repr x) (repr y))\nx \/ y = x * 1\/ y\n\n0\u2212_ : Op\u2081 \u2124q\n0\u2212 x = mod-q (negate (repr x))\n\n_==_ : (x y : \u2124q) \u2192 Bool\nx == y = equals (repr x) (repr y)\n\nsum prod : List \u2124q \u2192 \u2124q\nsum = foldr _+_ 0#\nprod = foldr _*_ 1#\n\n{-\n+-mon-ops : Monoid-Ops \u2124q\n+-mon-ops = _+_ , 0#\n\n+-grp-ops : Group-Ops \u2124q\n+-grp-ops = +-mon-ops , 0\u2212_\n\n*-mon-ops : Monoid-Ops \u2124q\n*-mon-ops = _*_ , 1#\n\n*-grp-ops : Group-Ops \u2124q\n*-grp-ops = *-mon-ops , 1\/_\n\nfld-ops : Field-Ops \u2124q\nfld-ops = +-grp-ops , *-grp-ops\n\npostulate\n fld-struct : Field-Struct fld-ops\n\nfld : Field \u2124q\nfld = fld-ops , fld-struct\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"0f09d9f4ac44a07ba808622a5206a04b696a6f76","subject":"agda\/derivation.agda: correct \u0394-term, more comments","message":"agda\/derivation.agda: correct \u0394-term, more comments\n\nIn the previous commit I forgot some comments and I forgot applying some\nchanges, something the typechecker unfortunately would not detect. Fix that.\n\nOld-commit-hash: 9a72bde5058103929f5d3c0eda97b9850a5d7c9c\n","repos":"inc-lc\/ilc-agda","old_file":"derivation.agda","new_file":"derivation.agda","new_contents":"module derivation where\n\nopen import lambda\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\u0394-Type bool = {!!}\n\napply : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbf. \u03bbx. apply ( df x ) ( f x )\napply {bool} = {!!}\n\ncompose : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4)\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app (compose {\u03c4\u2082}) (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbdg. \u03bbx. compose ( df x ) ( dg x )\ncompose {bool} = {!!}\n\nnil : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4)\nnil {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (nil {\u03c4\u2082})\n -- \u03bbx. nil\nnil {bool} = {!!}\n\n-- Hey, apply is \u03b1-equivalent to compose, what's going on?\n-- Oh, and `\u0394-Type` is the identity function?\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u03c4 \u2022 \u0394-Type \u03c4 \u2022 \u0394-Context \u0393\n\n-- CHANGE VARIABLES\n\n-- changes 'x' to 'dx'\n\nrename : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nrename this = that this\nrename (that x) = that (that (rename x))\n\n-- Weakening of a term needed during derivation - change x to x in a context which also includes dx.\n-- The actual specification is more complicated, study the type signature.\nadaptVar : \u2200 {\u03c4} \u0393\u2081 \u0393\u2082 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) \u03c4\nadaptVar \u2205 (\u03c4\u2082 \u2022 \u0393\u2082) this = this\nadaptVar \u2205 (\u03c4\u2082 \u2022 \u0393\u2082) (that x) = that (that (adaptVar \u2205 \u0393\u2082 x))\nadaptVar (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 this = this\nadaptVar (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 (that x) = that (adaptVar \u0393\u2081 \u0393\u2082 x)\n\nadapt : \u2200 {\u03c4} \u0393\u2081 \u0393\u2082 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) \u03c4\nadapt {\u03c4\u2081 \u21d2 \u03c4\u2082} \u0393\u2081 \u0393\u2082 (abs t) = abs (adapt (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 t)\nadapt \u0393\u2081 \u0393\u2082 (app t\u2081 t\u2082) = app (adapt \u0393\u2081 \u0393\u2082 t\u2081) (adapt \u0393\u2081 \u0393\u2082 t\u2082)\nadapt \u0393\u2081 \u0393\u2082 (var t) = var (adaptVar \u0393\u2081 \u0393\u2082 t)\nadapt \u0393\u2081 \u0393\u2082 true = true\nadapt \u0393\u2081 \u0393\u2082 false = false\nadapt \u0393\u2081 \u0393\u2082 (cond tc t\u2081 t\u2082) = cond (adapt \u0393\u2081 \u0393\u2082 tc) (adapt \u0393\u2081 \u0393\u2082 t\u2081) (adapt \u0393\u2081 \u0393\u2082 t\u2082)\n\n-- changes 'x' to 'nil' (if internally bound) or 'dx' (if externally bound)\n\n\u0394-var : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-var {\u2205} x = var (rename x)\n\u0394-var {\u03c4 \u2022 \u0393} this = nil {\u03c4}\n\u0394-var {\u03c4 \u2022 \u0393} (that x) = \u0394-var {\u0393} x\n\n-- Note: this should be the derivative with respect to the first variable.\nnabla : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2080 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082)\nnabla = {!!}\n\n-- CHANGE TERMS\n\u0394-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-term {\u0393} (abs {\u03c4\u2081 = \u03c4} t) = abs (\u0394-term {\u03c4 \u2022 \u0393} t)\n\n-- To recheck: which is the order in which to apply the changes? When I did my live proof to Klaus, I came up with this order:\n-- (\u0394-term t\u2081) (t\u2082 \u2295 (\u0394-term t\u2082)) \u2218 (\u2207 t\u2081) t\u2082 (\u0394-term t\u2082)\n-- corresponding to:\n\u0394-term {\u0393\u2081} {\u0393\u2082} {\u03c4} (app t\u2081 t\u2082) = app (app (compose {\u03c4}) (app (\u0394-term {\u0393\u2081} t\u2081) (app (app apply (\u0394-term {\u0393\u2081} {\u0393\u2082} t\u2082)) (adapt \u0393\u2081 \u0393\u2082 t\u2082))))\n (app (adapt \u0393\u2081 \u0393\u2082 (app (nabla {\u0393\u2081 \u22ce \u0393\u2082} t\u2081) t\u2082)) (\u0394-term {\u0393\u2081} {\u0393\u2082} t\u2082))\n\n\u0394-term {\u0393} (var x) = weaken {\u2205} {\u0393} (\u0394-var {\u0393} x)\n\u0394-term {\u0393} true = {!!}\n\u0394-term {\u0393} false = {!!}\n\u0394-term {\u0393} (cond t\u2081 t\u2082 t\u2083) = {!!}\n","old_contents":"module derivation where\n\nopen import lambda\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\u0394-Type bool = {!!}\n\napply : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbf. \u03bbx. apply ( df x ) ( f x )\napply {bool} = {!!}\n\ncompose : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4)\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app (compose {\u03c4\u2082}) (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbdg. \u03bbx. compose ( df x ) ( dg x )\ncompose {bool} = {!!}\n\nnil : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4)\nnil {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (nil {\u03c4\u2082})\n -- \u03bbx. nil\nnil {bool} = {!!}\n\n-- Hey, apply is \u03b1-equivalent to compose, what's going on?\n-- Oh, and `\u0394-Type` is the identity function?\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u03c4 \u2022 \u0394-Type \u03c4 \u2022 \u0394-Context \u0393\n\n-- CHANGE VARIABLES\n\n-- changes 'x' to 'dx'\n\nrename : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nrename this = that this\nrename (that x) = that (that (rename x))\n\nadaptVar : \u2200 {\u03c4} \u0393\u2081 \u0393\u2082 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) \u03c4\nadaptVar \u2205 (\u03c4\u2082 \u2022 \u0393\u2082) this = this\nadaptVar \u2205 (\u03c4\u2082 \u2022 \u0393\u2082) (that x) = that (that (adaptVar \u2205 \u0393\u2082 x))\nadaptVar (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 this = this\nadaptVar (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 (that x) = that (adaptVar \u0393\u2081 \u0393\u2082 x)\n\nadapt : \u2200 {\u03c4} \u0393\u2081 \u0393\u2082 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) \u03c4\nadapt {\u03c4\u2081 \u21d2 \u03c4\u2082} \u0393\u2081 \u0393\u2082 (abs t) = abs (adapt (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 t)\nadapt \u0393\u2081 \u0393\u2082 (app t\u2081 t\u2082) = app (adapt \u0393\u2081 \u0393\u2082 t\u2081) (adapt \u0393\u2081 \u0393\u2082 t\u2082)\nadapt \u0393\u2081 \u0393\u2082 (var t) = var (adaptVar \u0393\u2081 \u0393\u2082 t)\nadapt \u0393\u2081 \u0393\u2082 true = true\nadapt \u0393\u2081 \u0393\u2082 false = false\nadapt \u0393\u2081 \u0393\u2082 (cond tc t\u2081 t\u2082) = cond (adapt \u0393\u2081 \u0393\u2082 tc) (adapt \u0393\u2081 \u0393\u2082 t\u2081) (adapt \u0393\u2081 \u0393\u2082 t\u2082)\n\n-- changes 'x' to 'nil' (if internally bound) or 'dx' (if externally bound)\n\n\u0394-var : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-var {\u2205} x = var (rename x)\n\u0394-var {\u03c4 \u2022 \u0393} this = nil {\u03c4}\n\u0394-var {\u03c4 \u2022 \u0393} (that x) = \u0394-var {\u0393} x\n\n-- Note: this should be the derivative with respect to the first variable.\nnabla : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2080 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082)\nnabla = {!!}\n\n-- CHANGE TERMS\n\u0394-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-term {\u0393} (abs {\u03c4\u2081 = \u03c4} t) = abs (\u0394-term {\u03c4 \u2022 \u0393} t)\n\n\u0394-term {\u0393\u2081} {\u0393\u2082} {\u03c4} (app t\u2081 t\u2082) = app (app (compose {\u03c4}) (app (\u0394-term {\u0393\u2081} t\u2081) (adapt \u0393\u2081 \u0393\u2082 t\u2082)))\n (app (adapt \u0393\u2081 \u0393\u2082 (app (nabla {\u0393\u2081 \u22ce \u0393\u2082} t\u2081) t\u2082)) (\u0394-term {\u0393\u2081} {\u0393\u2082} t\u2082))\n\n\u0394-term {\u0393} (var x) = weaken {\u2205} {\u0393} (\u0394-var {\u0393} x)\n\u0394-term {\u0393} true = {!!}\n\u0394-term {\u0393} false = {!!}\n\u0394-term {\u0393} (cond t\u2081 t\u2082 t\u2083) = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"8f895d5906dc2d743ba0dc39c4f907e17931b4f9","subject":"agda: implement proof-decorated environments (#33)","message":"agda: implement proof-decorated environments (#33)\n\nOld-commit-hash: eb0097a135a39c572cfb62c967a388632b42f839\n","repos":"inc-lc\/ilc-agda","old_file":"Denotational\/ValidChanges.agda","new_file":"Denotational\/ValidChanges.agda","new_contents":"module Denotational.ValidChanges where\n\nopen import Data.Product\nopen import Data.Unit\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Equivalence\n\nopen import Changes\n\n-- DEFINITION of valid changes via a logical relation\n\n{-\nWhat I wanted to write:\n\ndata Valid-\u0394 : {T : Type} \u2192 (v : \u27e6 T \u27e7) \u2192 (dv : \u27e6 \u0394-Type T \u27e7) \u2192 Set where\n base : (v : \u27e6 bool \u27e7) \u2192 (dv : \u27e6 \u0394-Type bool \u27e7) \u2192 Valid\u0394 v dv\n fun : \u2200 {S T} \u2192 (f : \u27e6 S \u21d2 T \u27e7) \u2192 (df : \u27e6 \u0394-Type (S \u21d2 T) \u27e7) \u2192\n (\u2200 (s : \u27e6 S \u27e7) ds \u2192 (Valid\u0394 (f s) (df s ds)) \u00d7 ((apply df f) (apply ds s) \u2261 apply (df s ds) (f s))) \u2192 \n Valid\u0394 f df\n-}\n\n-- What I had to write:\n-- Note: now I could go back to using a datatype, since the datatype is now strictly positive.\nValid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nValid-\u0394 {bool} v dv = \u22a4\nValid-\u0394 {S \u21d2 T} f df =\n \u2200 (s : \u27e6 S \u27e7) ds {- (valid-w : Valid-\u0394 s ds) -} \u2192\n Valid-\u0394 (f s) (df s ds) \u00d7\n (apply df f) (apply ds s) \u2261 apply (df s ds) (f s)\n\ninvalid-changes-exist : \u00ac (\u2200 {\u03c4} v dv \u2192 Valid-\u0394 {\u03c4} v dv)\ninvalid-changes-exist k with k (\u03bb x \u2192 x) (\u03bb x dx \u2192 false) false true\n... | _ , ()\n\ndiff-is-valid : \u2200 {\u03c4} (v\u2032 v : \u27e6 \u03c4 \u27e7) \u2192 Valid-\u0394 {\u03c4} v (diff v\u2032 v)\ndiff-is-valid {bool} v\u2032 v = tt\ndiff-is-valid {\u03c4 \u21d2 \u03c4\u2081} v\u2032 v =\n \u03bb s ds \u2192\n diff-is-valid (v\u2032 (apply ds s)) (v s) , (\n begin\n apply (diff v\u2032 v) v (apply ds s)\n \u2261\u27e8 refl \u27e9\n apply\n (diff (v\u2032 (apply (derive (apply ds s)) (apply ds s))) (v (apply ds s)))\n (v (apply ds s))\n \u2261\u27e8 \u2261-cong (\u03bb x \u2192 apply (diff (v\u2032 x) (v (apply ds s))) (v (apply ds s))) (apply-derive (apply ds s)) \u27e9\n apply (diff (v\u2032 (apply ds s)) (v (apply ds s))) (v (apply ds s))\n \u2261\u27e8 apply-diff (v (apply ds s)) (v\u2032 (apply ds s)) \u27e9\n v\u2032 (apply ds s)\n \u2261\u27e8 sym (apply-diff (v s) (v\u2032 (apply ds s))) \u27e9\n apply ((diff v\u2032 v) s ds) (v s)\n \u220e) where open \u2261-Reasoning\n\nderive-is-valid : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192 Valid-\u0394 {\u03c4} v (derive v)\nderive-is-valid v rewrite sym (diff-derive v) = diff-is-valid v v\n\n-- This is a postulate elsewhere, but here I provide a proper proof.\n\ndiff-apply-proof : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n (Valid-\u0394 v dv) \u2192 diff (apply dv v) v \u2261 dv\n\ndiff-apply-proof {\u03c4\u2081 \u21d2 \u03c4\u2082} df f df-valid = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n begin\n diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (sym (proj\u2082 (df-valid (apply dv v) (derive (apply dv v))))) \u27e9\n diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff (apply df f x) (f v))\n (apply-derive (apply dv v)) \u27e9\n diff ((apply df f) (apply dv v)) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (proj\u2082 (df-valid v dv)) \u27e9\n diff (apply (df v dv) (f v)) (f v)\n \u2261\u27e8 diff-apply-proof {\u03c4\u2082} (df v dv) (f v) (proj\u2081 (df-valid v dv)) \u27e9\n df v dv\n \u220e)) where open \u2261-Reasoning\n\ndiff-apply-proof {bool} db b _ = xor-cancellative b db\n\nopen import Syntactic.Contexts Type\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Changes\nopen import ChangeContexts\n\nProofVal : Type \u2192 Set\nProofVal \u03c4 = \u03a3[ v \u2208 \u27e6 \u03c4 \u27e7 ] (\u03a3[ dv \u2208 \u27e6 \u0394-Type \u03c4 \u27e7 ] Valid-\u0394 v dv)\n\nimport Denotational.Environments Type ProofVal as ProofEnv\n\neraseVal : \u2200 {\u03c4} \u2192 ProofVal \u03c4 \u2192 \u27e6 \u03c4 \u27e7\neraseVal (v , dv , dv-valid) = v\n\n-- Specification: eraseEnv = map eraseVal\neraseEnv : \u2200 {\u0393} \u2192 ProofEnv.\u27e6 \u0393 \u27e7Context \u2192 \u27e6 \u0393 \u27e7\neraseEnv {\u2205} \u2205 = \u2205\neraseEnv {\u03c4 \u2022 \u0393} (v \u2022 \u03c1) = eraseVal v \u2022 eraseEnv \u03c1\n\neraseProofs : \u2200 {\u0393} \u2192 ProofEnv.\u27e6 \u0393 \u27e7Context \u2192 \u27e6 \u0394-Context \u0393 \u27e7\neraseProofs {\u2205} \u2205 = \u2205\neraseProofs {\u03c4 \u2022 \u0393} ((v , dv , dv-valid) \u2022 \u03c1) = dv \u2022 v \u2022 eraseProofs \u03c1\n","old_contents":"module Denotational.ValidChanges where\n\nopen import Data.Product\nopen import Data.Unit\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Equivalence\n\nopen import Changes\n\n-- DEFINITION of valid changes via a logical relation\n\n{-\nWhat I wanted to write:\n\ndata Valid-\u0394 : {T : Type} \u2192 (v : \u27e6 T \u27e7) \u2192 (dv : \u27e6 \u0394-Type T \u27e7) \u2192 Set where\n base : (v : \u27e6 bool \u27e7) \u2192 (dv : \u27e6 \u0394-Type bool \u27e7) \u2192 Valid\u0394 v dv\n fun : \u2200 {S T} \u2192 (f : \u27e6 S \u21d2 T \u27e7) \u2192 (df : \u27e6 \u0394-Type (S \u21d2 T) \u27e7) \u2192\n (\u2200 (s : \u27e6 S \u27e7) ds \u2192 (Valid\u0394 (f s) (df s ds)) \u00d7 ((apply df f) (apply ds s) \u2261 apply (df s ds) (f s))) \u2192 \n Valid\u0394 f df\n-}\n\n-- What I had to write:\n-- Note: now I could go back to using a datatype, since the datatype is now strictly positive.\nValid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nValid-\u0394 {bool} v dv = \u22a4\nValid-\u0394 {S \u21d2 T} f df =\n \u2200 (s : \u27e6 S \u27e7) ds {- (valid-w : Valid-\u0394 s ds) -} \u2192\n Valid-\u0394 (f s) (df s ds) \u00d7\n (apply df f) (apply ds s) \u2261 apply (df s ds) (f s)\n\ninvalid-changes-exist : \u00ac (\u2200 {\u03c4} v dv \u2192 Valid-\u0394 {\u03c4} v dv)\ninvalid-changes-exist k with k (\u03bb x \u2192 x) (\u03bb x dx \u2192 false) false true\n... | _ , ()\n\ndiff-is-valid : \u2200 {\u03c4} (v\u2032 v : \u27e6 \u03c4 \u27e7) \u2192 Valid-\u0394 {\u03c4} v (diff v\u2032 v)\ndiff-is-valid {bool} v\u2032 v = tt\ndiff-is-valid {\u03c4 \u21d2 \u03c4\u2081} v\u2032 v =\n \u03bb s ds \u2192\n diff-is-valid (v\u2032 (apply ds s)) (v s) , (\n begin\n apply (diff v\u2032 v) v (apply ds s)\n \u2261\u27e8 refl \u27e9\n apply\n (diff (v\u2032 (apply (derive (apply ds s)) (apply ds s))) (v (apply ds s)))\n (v (apply ds s))\n \u2261\u27e8 \u2261-cong (\u03bb x \u2192 apply (diff (v\u2032 x) (v (apply ds s))) (v (apply ds s))) (apply-derive (apply ds s)) \u27e9\n apply (diff (v\u2032 (apply ds s)) (v (apply ds s))) (v (apply ds s))\n \u2261\u27e8 apply-diff (v (apply ds s)) (v\u2032 (apply ds s)) \u27e9\n v\u2032 (apply ds s)\n \u2261\u27e8 sym (apply-diff (v s) (v\u2032 (apply ds s))) \u27e9\n apply ((diff v\u2032 v) s ds) (v s)\n \u220e) where open \u2261-Reasoning\n\nderive-is-valid : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192 Valid-\u0394 {\u03c4} v (derive v)\nderive-is-valid v rewrite sym (diff-derive v) = diff-is-valid v v\n\n-- This is a postulate elsewhere, but here I provide a proper proof.\n\ndiff-apply-proof : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n (Valid-\u0394 v dv) \u2192 diff (apply dv v) v \u2261 dv\n\ndiff-apply-proof {\u03c4\u2081 \u21d2 \u03c4\u2082} df f df-valid = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n begin\n diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (sym (proj\u2082 (df-valid (apply dv v) (derive (apply dv v))))) \u27e9\n diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff (apply df f x) (f v))\n (apply-derive (apply dv v)) \u27e9\n diff ((apply df f) (apply dv v)) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (proj\u2082 (df-valid v dv)) \u27e9\n diff (apply (df v dv) (f v)) (f v)\n \u2261\u27e8 diff-apply-proof {\u03c4\u2082} (df v dv) (f v) (proj\u2081 (df-valid v dv)) \u27e9\n df v dv\n \u220e)) where open \u2261-Reasoning\n\ndiff-apply-proof {bool} db b _ = xor-cancellative b db\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"e758586a59fdf8218ce20e2fc52ba3b20db11401","subject":"Changed base type from ]0,1] to ]0,1[ and <= to <","message":"Changed base type from ]0,1] to ]0,1[ and <= to <\n\nAlso added more proof obligations to ]0,1[-ops so that the [0,1]\nmodule doesn't contain any postulates.\n\n1 + x = 1 -- by definition now. More properties regarding 1 are\nsimpler now aswell.\n","repos":"crypto-agda\/crypto-agda","old_file":"Prob.agda","new_file":"Prob.agda","new_contents":"module Prob where\n\nopen import Data.Bool\nopen import Data.Nat\nopen import Data.Vec\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\nopen import Data.Sum\n\nopen import Function\n\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Relation.Nullary\n\nrecord ]0,1[-ops (]0,1[ : Set) (_ : (x y : ]0,1[) \u2192 x ) \u2261 (x \u00b7E y) \/E z < \u00b7E-anti\u2082 x pf >\n\n -- proofs \u2261 maybe should be some Equivalence \u2248 \n field\n +E-sym : (x y : ]0,1[) \u2192 x +E y \u2261 y +E x\n +E-assoc : (x y z : ]0,1[) \u2192 (x +E y) +E z \u2261 x +E (y +E z)\n \u00b7\/E-identity : (x : ]0,1[){y : ]0,1[} \u2192 x \u2261 (x \u00b7E y) \/E y < \u00b7E-anti\u2081 x >\n\nmodule ]0,1[-\u211a where\n data ]0,1[ : Set where\n 1+_\/2+x+_ : \u2115 \u2192 \u2115 \u2192 ]0,1[\n -- to\u211a : ]0,1] \u2192 \u211a\n -- to\u211a (1+ x \/1+x+ y) = (1 + x) \/\u211a (1 + x + y)\n \n postulate\n _ : (x y : ]0,1[) \u2192 x ) \u2261 (x \u00b7E y) \/E z < \u00b7E-anti\u2082 x pf >\n\n postulate\n +E-sym : (x x\u2081 : ]0,1[) \u2192 x +E x\u2081 \u2261 x\u2081 +E x\n +E-assoc : (x x\u2081 x\u2082 : ]0,1[) \u2192 (x +E x\u2081) +E x\u2082 \u2261 x +E (x\u2081 +E x\u2082)\n \u00b7\/E-identity : (x : ]0,1[) {y : ]0,1[} \u2192 x \u2261 (x \u00b7E y) \/E y < \u00b7E-anti\u2081 x >\n\n ops : ]0,1[-ops ]0,1[ _ : (x : [0,1]) \u2192 Inc x \u2192 ]0,1[ \n 0I < () >\n 1I < () >\n (x I) < pos > = x\n\n _+I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I +I x = x\n 1I +I _ = 1I\n x I +I 0I = x I\n x I +I 1I = 1I\n x I +I x\u2081 I = (x +E x\u2081) I\n\n _\u00b7I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I \u00b7I y = 0I\n 1I \u00b7I y = y\n (x I) \u00b7I 0I = 0I\n (x I) \u00b7I 1I = x I\n (x I) \u00b7I (x\u2081 I) = (x \u00b7E x\u2081) I\n\n -- 1\/I1+_ : \u2115 \u2192 [0,1]\n\n \n _\/I_<_,_> : (x y : [0,1]) \u2192 x \u2264I y \u2192 Pos y \u2192 [0,1]\n x \/I 0I < _ , () >\n x \/I 1I < _ , _ > = x\n 0I \/I _ < _ , _ > = 0I\n 1I \/I _ I < () , _ > \n (x I) \/I y I < E\u2264E pf , _ > = (x \/E y < pf >) I\n (x I) \/I .x I < E\u2261E , _ > = 1I\n\n\n +I-identity : (x : [0,1]) \u2192 x \u2261 0I +I x\n +I-identity x = refl\n\n *-anti : (x : [0,1]){y z : [0,1]} \u2192 y \u2264I z \u2192 x \u00b7I y \u2264I z\n *-anti 0I le = z\u2264n\n *-anti 1I le = le\n *-anti (x I) z\u2264n = z\u2264n\n *-anti (x I) n\u22641 = n\u22641\n *-anti (x I) (E\u2264E pf) = E\u2264E (\u00b7E-anti\u2082 x pf)\n *-anti (x I) E\u2261E = E\u2264E (\u00b7E-anti\u2081 x)\n\n *\/-assoc : (x y z : [0,1])(pr : y \u2264I z)(pos : Pos z) \u2192 (x \u00b7I (y \/I z < pr , pos >)) \u2261 (x \u00b7I y) \/I z < *-anti x pr , pos >\n *\/-assoc x y 0I pr ()\n *\/-assoc x y 1I pr pos = refl\n *\/-assoc 0I y (z I) pr pos = refl\n *\/-assoc 1I y (z I) pr pos = refl\n *\/-assoc (x I) 0I (z I) pr pos = refl\n *\/-assoc (x I) 1I (z I) () pos\n *\/-assoc (x I) (y I) (z I) (E\u2264E pf) pos = cong _I (\u00b7\/E-assoc x y z pf)\n *\/-assoc (x I) (y I) (.y I) E\u2261E pos = cong _I (\u00b7\/E-identity x)\n\n +I-sym : (x y : [0,1]) \u2192 x +I y \u2261 y +I x\n +I-sym 0I 0I = refl\n +I-sym 0I 1I = refl\n +I-sym 0I (x I) = refl\n +I-sym 1I 0I = refl\n +I-sym 1I 1I = refl\n +I-sym 1I (x I) = refl\n +I-sym (x I) 0I = refl\n +I-sym (x I) 1I = refl\n +I-sym (x I) (y I) = cong _I (+E-sym x y)\n\n +I-assoc : (x y z : [0,1]) \u2192 x +I y +I z \u2261 x +I (y +I z)\n +I-assoc 0I y z = refl\n +I-assoc 1I y z = refl\n +I-assoc (x I) 0I z = refl\n +I-assoc (x I) 1I z = refl\n +I-assoc (x I) (y I) 0I = refl\n +I-assoc (x I) (y I) 1I = refl\n +I-assoc (x I) (y I) (z I) = cong _I (+E-assoc x y z)\n\n \u2264I-trans : {x y z : [0,1]} \u2192 x \u2264I y \u2192 y \u2264I z \u2192 x \u2264I z\n \u2264I-trans z\u2264n le2 = z\u2264n\n \u2264I-trans n\u22641 n\u22641 = n\u22641\n \u2264I-trans (E\u2264E x\u2081) n\u22641 = n\u22641\n \u2264I-trans (E\u2264E x\u2081) (E\u2264E x\u2082) = E\u2264E ( : (A B : Event)(pf : Pos Pr[ B ]) \u2192 [0,1] \n Pr[ A \u2223 B ]< pr > = Pr[ A \u2229 B ] \/I Pr[ B ] < Pr-mono (\u2229-lem A) , pr >\n\n _ind_ : (A B : Event) \u2192 Set\n A ind B = Pr[ A ] \u00b7I Pr[ B ] \u2261 Pr[ A \u2229 B ]\n\n union-bound : (A\u2081 A\u2082 : Event) \u2192 Pr[ (A\u2081 \u222a A\u2082) ] \u2264I Pr[ A\u2081 ] +I Pr[ A\u2082 ]\n union-bound A\u2081 A\u2082 = go allU where\n sA\u2081 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2081 = \u03bb xs \u2192 sumP (pr_\u220b_ A\u2081) xs\n sA\u2082 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2082 = sumP (pr_\u220b_ A\u2082)\n go : {n : \u2115}(xs : Vec U n) \u2192 sumP (pr_\u220b_ (A\u2081 \u222a A\u2082)) xs \u2264I sumP (pr_\u220b_ A\u2081) xs +I sumP (pr_\u220b_ A\u2082) xs\n go [] = \u2264I-refl\n go (x \u2237 xs) with A\u2081 x | A\u2082 x \n ... | true | true rewrite +I-assoc (P x) (sA\u2081 xs) (P x +I sA\u2082 xs)\n | +I-sym (P x) (sA\u2082 xs)\n | sym (+I-assoc (sA\u2081 xs) (sA\u2082 xs) (P x))\n = \u2264I-pres (P x) (\u2264I-mono (P x) (go xs)) \n ... | true | false rewrite +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | true rewrite sym (+I-assoc (sA\u2081 xs) (P x) (sA\u2082 xs))\n | +I-sym (sA\u2081 xs) (P x)\n | +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | false = go xs \n\n module RandomVar (V : Set) (_==V_ : V \u2192 V \u2192 Bool) where\n RV : Set\n RV = U \u2192 V\n\n _^-1_ : RV \u2192 V \u2192 Event\n RV ^-1 v = \u03bb x \u2192 RV x ==V v \n\n _\u2261r_ : RV \u2192 V \u2192 Event \n RV \u2261r v = RV ^-1 v\n \n -- sugar, socker et sucre\n Pr[_\u2261_] : RV \u2192 V \u2192 [0,1]\n Pr[ X \u2261 v ] = Pr[ X \u2261r v ]\n \n -- \u2200 (a b) . Pr[X = a and Y = b] = Pr[X = a] \u00b7 Pr[Y = b]\n _indRV_ : RV \u2192 RV \u2192 Set\n X indRV Y = (a b : V) \u2192 (X ^-1 a) ind (Y ^-1 b)","old_contents":"module Prob where\n\nopen import Data.Bool\nopen import Data.Nat\nopen import Data.Vec\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\n\nopen import Function\n\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Relation.Nullary\n\nrecord ]0,1]-ops (]0,1] : Set) (_\u2264E_ : ]0,1] \u2192 ]0,1] \u2192 Set) : Set where\n constructor mk\n field \n 1E : ]0,1]\n _+E_ : ]0,1] \u2192 ]0,1] \u2192 ]0,1]\n _\u00b7E_ : ]0,1] \u2192 ]0,1] \u2192 ]0,1]\n _\/E_<_> : (x y : ]0,1]) \u2192 x \u2264E y \u2192 ]0,1]\n\nmodule ]0,1]-\u211a where\n data ]0,1] : Set where\n 1+_\/1+x+_ : \u2115 \u2192 \u2115 \u2192 ]0,1]\n -- to\u211a : ]0,1] \u2192 \u211a\n -- to\u211a (1+ x \/1+x+ y) = (1 + x) \/\u211a (1 + x + y)\n 1E : ]0,1]\n 1E = 1+ 0 \/1+x+ 0\n postulate\n _\u2264E_ : ]0,1] \u2192 ]0,1] \u2192 Set\n -- (1+ x \/1+x+ y) \u2264E (1+ x' \/1+x+ y') = ?\n -- (1 + x' + y') * (1 + x) \u2264E (1 + x + y) * (1 + x') = ?\n postulate\n _+E_ : ]0,1] \u2192 ]0,1] \u2192 ]0,1]\n _\u00b7E_ : ]0,1] \u2192 ]0,1] \u2192 ]0,1]\n _\/E_<_> : (x y : ]0,1]) \u2192 x \u2264E y \u2192 ]0,1]\n ops : ]0,1]-ops ]0,1] _\u2264E_\n ops = mk 1E _+E_ _\u00b7E_ _\/E_<_>\n\nmodule [0,1] {]0,1] _\u2264E_} (]0,1]R : ]0,1]-ops ]0,1] _\u2264E_) where\n\n open ]0,1]-ops ]0,1]R public\n\n infixl 6 _+I_\n infix 4 _\u2264I_\n\n data [0,1] : Set where\n 0I : [0,1]\n _I : ]0,1] \u2192 [0,1]\n\n data _\u2264I_ : [0,1] \u2192 [0,1] \u2192 Set where\n z\u2264n : \u2200 {n} \u2192 0I \u2264I n\n E\u2264E : \u2200 {x y} \u2192 x \u2264E y \u2192 (x I) \u2264I (y I)\n\n 1I : [0,1]\n 1I = 1E I\n\n Pos : [0,1] \u2192 Set\n Pos 0I = \u22a5\n Pos (_ I) = \u22a4\n\n _<_> : (x : [0,1]) \u2192 Pos x \u2192 ]0,1] \n 0I < () >\n (x I) < pos > = x\n\n _+I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I +I x = x\n x I +I 0I = x I\n x I +I x\u2081 I = (x +E x\u2081) I\n\n _\u00b7I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I \u00b7I y = 0I\n (x I) \u00b7I 0I = 0I\n (x I) \u00b7I (x\u2081 I) = (x \u00b7E x\u2081) I\n\n -- 1\/I1+_ : \u2115 \u2192 [0,1]\n\n _\/I_<_,_> : (x y : [0,1]) \u2192 x \u2264I y \u2192 Pos y \u2192 [0,1]\n _ \/I 0I < _ , () >\n 0I \/I _ < _ , _ > = 0I\n (x I) \/I (y I) < E\u2264E pf , _ > = (x \/E y < pf >)I\n\n +I-identity : (x : [0,1]) \u2192 x \u2261 0I +I x\n +I-identity x = refl\n\n postulate\n +I-sym : (x y : [0,1]) \u2192 x +I y \u2261 y +I x\n +I-assoc : (x y z : [0,1]) \u2192 x +I y +I z \u2261 x +I (y +I z)\n\n \u2264I-refl : {x : [0,1]} \u2192 x \u2264I x\n\n \u2264I-trans : {x y z : [0,1]} \u2192 x \u2264I y \u2192 y \u2264I z \u2192 x \u2264I z\n \u2264I-mono : (x : [0,1]){y z : [0,1]} \u2192 y \u2264I z \u2192 y \u2264I z +I x\n \u2264I-pres : (x : [0,1]){y z : [0,1]} \u2192 y \u2264I z \u2192 x +I y \u2264I x +I z\n\nmodule Univ {]0,1] _\u2264E_} (]0,1]R : ]0,1]-ops ]0,1] _\u2264E_)\n (U : Set)\n (size-1 : \u2115) \n (allU : Vec U (suc size-1))\n (x\u2208allU : (x : U) \u2192 x \u2208 allU) where \n\n open [0,1] ]0,1]R\n\n sumP : {n : \u2115} \u2192 (U \u2192 [0,1]) \u2192 Vec U n \u2192 [0,1]\n sumP P [] = 0I\n sumP P (x \u2237 xs) = (P x) +I (sumP P xs)\n\n module Prob (P : U \u2192 [0,1])\n (sumP\u22611 : sumP P allU \u2261 1I) where\n \n Event : Set\n Event = U \u2192 Bool \n \n pr_\u220b_ : Event \u2192 U \u2192 [0,1]\n pr A \u220b x = if A x then P x else 0I\n\n _\u222a_ : Event \u2192 Event \u2192 Event\n A\u2081 \u222a A\u2082 = \u03bb x \u2192 A\u2081 x \u2228 A\u2082 x\n\n _\u2229_ : Event \u2192 Event \u2192 Event\n A\u2081 \u2229 A\u2082 = \u03bb x \u2192 A\u2081 x \u2227 A\u2082 x\n\n _\u2286_ : Event \u2192 Event \u2192 Set\n A \u2286 B = \u2200 x \u2192 T(A x) \u2192 T(B x)\n\n \u2102[_] : Event \u2192 Event\n \u2102[ A ] = not \u2218 A\n\n Pr[_] : Event \u2192 [0,1]\n Pr[ A ] = sumP (pr_\u220b_ A) allU\n\n postulate\n Pr-mono : \u2200 {A B} \u2192 A \u2286 B \u2192 Pr[ A ] \u2264I Pr[ B ]\n\n \u222a-lem : \u2200 {A} B \u2192 A \u2286 (A \u222a B)\n \u222a-lem {A} _ x with A x\n ... | true = id\n ... | false = \u03bb()\n\n \u2229-lem : \u2200 A {B} \u2192 (A \u2229 B) \u2286 B\n \u2229-lem A x with A x\n ... | true = id\n ... | false = \u03bb()\n\n Pr[_\u2223_]<_> : (A B : Event)(pf : Pos Pr[ B ]) \u2192 [0,1] \n Pr[ A \u2223 B ]< pr > = Pr[ A \u2229 B ] \/I Pr[ B ] < Pr-mono (\u2229-lem A) , pr >\n\n _ind_ : (A B : Event) \u2192 Set\n A ind B = Pr[ A ] \u00b7I Pr[ B ] \u2261 Pr[ A \u2229 B ]\n\n union-bound : (A\u2081 A\u2082 : Event) \u2192 Pr[ (A\u2081 \u222a A\u2082) ] \u2264I Pr[ A\u2081 ] +I Pr[ A\u2082 ]\n union-bound A\u2081 A\u2082 = go allU where\n sA\u2081 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2081 = \u03bb xs \u2192 sumP (pr_\u220b_ A\u2081) xs\n sA\u2082 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2082 = sumP (pr_\u220b_ A\u2082)\n go : {n : \u2115}(xs : Vec U n) \u2192 sumP (pr_\u220b_ (A\u2081 \u222a A\u2082)) xs \u2264I sumP (pr_\u220b_ A\u2081) xs +I sumP (pr_\u220b_ A\u2082) xs\n go [] = \u2264I-refl\n go (x \u2237 xs) with A\u2081 x | A\u2082 x \n ... | true | true rewrite +I-assoc (P x) (sA\u2081 xs) (P x +I sA\u2082 xs)\n | +I-sym (P x) (sA\u2082 xs)\n | sym (+I-assoc (sA\u2081 xs) (sA\u2082 xs) (P x))\n = \u2264I-pres (P x) (\u2264I-mono (P x) (go xs)) \n ... | true | false rewrite +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | true rewrite sym (+I-assoc (sA\u2081 xs) (P x) (sA\u2082 xs))\n | +I-sym (sA\u2081 xs) (P x)\n | +I-assoc (P x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (P x) (go xs)\n ... | false | false = go xs \n\n module RandomVar (V : Set) (_==V_ : V \u2192 V \u2192 Bool) where\n RV : Set\n RV = U \u2192 V\n\n _^-1_ : RV \u2192 V \u2192 Event\n RV ^-1 v = \u03bb x \u2192 RV x ==V v \n\n _\u2261r_ : RV \u2192 V \u2192 Event \n RV \u2261r v = RV ^-1 v\n \n -- sugar, socker et sucre\n Pr[_\u2261_] : RV \u2192 V \u2192 [0,1]\n Pr[ X \u2261 v ] = Pr[ X \u2261r v ]\n \n -- \u2200 (a b) . Pr[X = a and Y = b] = Pr[X = a] \u00b7 Pr[Y = b]\n _indRV_ : RV \u2192 RV \u2192 Set\n X indRV Y = (a b : V) \u2192 (X ^-1 a) ind (Y ^-1 b)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"f40291968fc13e8adb20c12fff5d457c41d78279","subject":"Bits: Get rid of one duplicate","message":"Bits: Get rid of one duplicate\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (_\u229b_; rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n where open RawMonad L.monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u00b7_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u00b7 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u00b7 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u00b7-comm : \u2200 {m} (x y : A m) \u2192 x \u00b7 y \u2261 y \u00b7 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u00b7-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nmodule SimpleSearch {a} {A : Set a} (_\u00b7_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u00b7_ public\n\n search-\u00b7-\u03b5\u2261\u03b5 : \u2200 \u03b5 (\u03b5\u00b7\u03b5 : \u03b5 \u00b7 \u03b5 \u2261 \u03b5) n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-\u00b7-\u03b5\u2261\u03b5 \u03b5 \u03b5\u00b7\u03b5 = go\n where\n go : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n go zero = refl\n go (suc n) rewrite go n = \u03b5\u00b7\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n module Interchange (\u00b7-interchange : \u2200 x y z t \u2192 (x \u00b7 y) \u00b7 (z \u00b7 t) \u2261 (x \u00b7 z) \u00b7 (y \u00b7 t)) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u00b7 f\u2081 x) \u2261 search f\u2080 \u00b7 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u00b7-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e\n where open \u2261-Reasoning\n\nopen SimpleSearch public\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nsum : \u2200 {n} \u2192 (Bits n \u2192 \u2115) \u2192 \u2115\nsum = search _+_\n\nsum-\u2257 : \u2200 {n} (f g : Bits n \u2192 \u2115) \u2192 f \u2257 g \u2192 sum f \u2261 sum g\nsum-\u2257 = search-\u2257 _+_\n\nsum-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 \u2115) \u2192 sum f \u2261 sum (f \u2218 _\u2295_ pad)\nsum-comm = search\u2032-comm _+_ \u2115\u00b0.+-comm\n\nsum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\nsum-const zero _ = refl\nsum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n#\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n-- #-ext\n#-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n#-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n#-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n#-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n#-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n#-\u2295 = #-comm\n\n#-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n#-const n b = sum-const n (Bool.to\u2115 b)\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n#never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n#never\u22610 = search-\u00b7-\u03b5\u2261\u03b5 _ _ refl\n\n#always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n#always\u22612^ n = sum-const n 1\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\ncount\u1d47 : Bit \u2192 \u2115\ncount\u1d47 b = if b then 1 else 0\n\n#\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n#\u27e8== [] \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n\u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n\u2257-cong-# f g f\u2257g = search-\u2257 _+_ _ _ (cong count\u1d47 \u2218 f\u2257g)\n\n#-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n#-+ f f0 f1 rewrite f0 | f1 = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\n#-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-take-drop zero n f g with f []\n... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n... | false = #never\u22610 n\n#-take-drop (suc m) n f g = trans (#-+ {a = #\u27e8 f \u2218 0\u2237_ \u27e9 * #\u27e8 g \u27e9} ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)))\n (trans (\u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x)))\n (#-take-drop m n (f \u2218 0\u2237_) g))\n (trans (\u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x)))\n (#-take-drop m n (f \u2218 1\u2237_) g)))\n (sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9))\n\n#-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n#\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n#\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n#\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n... | true | true | _ = s\u2264s z\u2264n\n... | true | false | p = \u22a5-elim (p _)\n... | false | _ | _ = z\u2264n\n#\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n#-\u2227-\u2228\u1d47 : \u2200 x y \u2192 count\u1d47 (x \u2227 y) + count\u1d47 (x \u2228 y) \u2261 count\u1d47 x + count\u1d47 y\n#-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (count\u1d47 y) 1 = refl\n#-\u2227-\u2228\u1d47 false y = refl\n\n#-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n#-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#\u2228' {zero} f g with f []\n... | true = s\u2264s z\u2264n\n... | false = \u2115\u2264.refl\n#\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n#\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n#\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n#\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n#-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n#-bound zero f = Bool.to\u2115\u22641 (f [])\n#-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n#-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n#-\u2218vnot _ f = #-\u2295 1\u207f f\n\n#-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n#-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n#-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n#-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n#-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n#-not\u2218 zero f with f []\n... | true = \u2261.refl\n... | false = \u2261.refl\n#-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n#-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n#-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192 search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = search-\u2257 op _ _ (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _+_ _*_ f p hom = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom {n} _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n\n\nopen Defs public\n","old_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (_\u229b_; rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n where open RawMonad L.monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u00b7_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u00b7 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u00b7 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u00b7-comm : \u2200 {m} (x y : A m) \u2192 x \u00b7 y \u2261 y \u00b7 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u00b7-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nmodule SimpleSearch {a} {A : Set a} (_\u00b7_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u00b7_ public\n\n search-\u00b7-\u03b5\u2261\u03b5 : \u2200 \u03b5 (\u03b5\u00b7\u03b5 : \u03b5 \u00b7 \u03b5 \u2261 \u03b5) n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-\u00b7-\u03b5\u2261\u03b5 \u03b5 \u03b5\u00b7\u03b5 = go\n where\n go : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n go zero = refl\n go (suc n) rewrite go n = \u03b5\u00b7\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n module Interchange (\u00b7-interchange : \u2200 x y z t \u2192 (x \u00b7 y) \u00b7 (z \u00b7 t) \u2261 (x \u00b7 z) \u00b7 (y \u00b7 t)) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u00b7 f\u2081 x) \u2261 search f\u2080 \u00b7 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u00b7-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e\n where open \u2261-Reasoning\n\nopen SimpleSearch public\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nsum : \u2200 {n} \u2192 (Bits n \u2192 \u2115) \u2192 \u2115\nsum = search _+_\n\nsum-\u2257 : \u2200 {n} (f g : Bits n \u2192 \u2115) \u2192 f \u2257 g \u2192 sum f \u2261 sum g\nsum-\u2257 = search-\u2257 _+_\n\nsum-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 \u2115) \u2192 sum f \u2261 sum (f \u2218 _\u2295_ pad)\nsum-comm = search\u2032-comm _+_ \u2115\u00b0.+-comm\n\nsum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\nsum-const zero _ = refl\nsum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n#\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n-- #-ext\n#-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n#-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n#-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n#-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n#-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n#-\u2295 = #-comm\n\n#-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n#-const n b = sum-const n (Bool.to\u2115 b)\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n#never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n#never\u22610 = search-\u00b7-\u03b5\u2261\u03b5 _ _ refl\n\n#always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n#always\u22612^ n = sum-const n 1\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\ncount\u1d47 : Bit \u2192 \u2115\ncount\u1d47 b = if b then 1 else 0\n\n#\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n#\u27e8== [] \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n\u2257-cong-search : \u2200 {n a} {A : Set a} op {f g : Bits n \u2192 A} \u2192 f \u2257 g \u2192 search op f \u2261 search op g\n\u2257-cong-search {zero} op f\u2257g = f\u2257g []\n\u2257-cong-search {suc n} op f\u2257g = cong\u2082 op (\u2257-cong-search op (f\u2257g \u2218 0\u2237_))\n (\u2257-cong-search op (f\u2257g \u2218 1\u2237_))\n\n\u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n\u2257-cong-# f g f\u2257g = \u2257-cong-search _+_ (cong count\u1d47 \u2218 f\u2257g)\n\n#-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n#-+ f f0 f1 rewrite f0 | f1 = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\n#-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-take-drop zero n f g with f []\n... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n... | false = #never\u22610 n\n#-take-drop (suc m) n f g = trans (#-+ {a = #\u27e8 f \u2218 0\u2237_ \u27e9 * #\u27e8 g \u27e9} ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)))\n (trans (\u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x)))\n (#-take-drop m n (f \u2218 0\u2237_) g))\n (trans (\u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x)))\n (#-take-drop m n (f \u2218 1\u2237_) g)))\n (sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9))\n\n#-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n#\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n#\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n#\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n... | true | true | _ = s\u2264s z\u2264n\n... | true | false | p = \u22a5-elim (p _)\n... | false | _ | _ = z\u2264n\n#\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n#-\u2227-\u2228\u1d47 : \u2200 x y \u2192 count\u1d47 (x \u2227 y) + count\u1d47 (x \u2228 y) \u2261 count\u1d47 x + count\u1d47 y\n#-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (count\u1d47 y) 1 = refl\n#-\u2227-\u2228\u1d47 false y = refl\n\n#-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n#-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#\u2228' {zero} f g with f []\n... | true = s\u2264s z\u2264n\n... | false = \u2115\u2264.refl\n#\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n#\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n#\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n#\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n#-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n#-bound zero f = Bool.to\u2115\u22641 (f [])\n#-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n#-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n#-\u2218vnot _ f = #-\u2295 1\u207f f\n\n#-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n#-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n#-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n#-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n#-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n#-not\u2218 zero f with f []\n... | true = \u2261.refl\n... | false = \u2261.refl\n#-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n#-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n#-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192 search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = \u2257-cong-search op (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _+_ _*_ f p hom = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom {n} _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n\n\nopen Defs public\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"e1bda97e68132c63d228bd4154434843f6d59c0c","subject":"circuit: restore ipC-spec","message":"circuit: restore ipC-spec\n","repos":"crypto-agda\/crypto-agda","old_file":"circuit.agda","new_file":"circuit.agda","new_contents":"module circuit where\n\nopen import Function\nopen import Data.Nat.NP hiding (_\u225f_; compare)\nopen import Data.Bits\nopen import Data.Bits.Bits2\nopen import Data.Bool hiding (_\u225f_)\nopen import Data.Product hiding (swap; map)\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc; inject+; raise; #_)\nopen import Data.List using (List; []; _\u2237_)\nopen import Data.Vec using (Vec; []; _\u2237_; foldr; _[_]\u2254_; lookup; _++_; splitAt; tabulate; allFin; concat) renaming (map to vmap)\nopen import Data.Vec.Properties\nopen import Relation.Nullary.Decidable hiding (map)\nopen import Relation.Binary.PropositionalEquality\n\nCircuitType : Set\u2081\nCircuitType = (i o : \u2115) \u2192 Set\n\nRunCircuit : CircuitType \u2192 Set\nRunCircuit C = \u2200 {i o} \u2192 C i o \u2192 Bits i \u2192 Bits o\n\nmodule Rewire where\n RewireFun : CircuitType\n RewireFun i o = Fin o \u2192 Fin i\n\n RewireTbl : CircuitType\n RewireTbl i o = Vec (Fin i) o\n\n rewire : \u2200 {a i o} {A : Set a} \u2192 (Fin o \u2192 Fin i) \u2192 Vec A i \u2192 Vec A o\n rewire f v = tabulate (flip lookup v \u2218 f)\n\n rewireTbl : \u2200 {a i o} {A : Set a} \u2192 RewireTbl i o \u2192 Vec A i \u2192 Vec A o\n rewireTbl tbl v = vmap (flip lookup v) tbl\n\n runRewireFun : RunCircuit RewireFun\n runRewireFun = rewire\n\n runRewireTbl : RunCircuit RewireTbl\n runRewireTbl = rewireTbl\n\nopen Rewire using (RewireTbl; RewireFun)\n\nrecord RewiringBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n\n infixr 1 _>>>_\n infixr 3 _***_\n\n field\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n\n rewireWithTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 C i o\n rewireWithTbl = rewire \u2218 flip lookup\n\n idCDefault : \u2200 {i} \u2192 C i i\n idCDefault = rewire id\n\n field\n idC : \u2200 {i} \u2192 C i i\n\n field\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) is \u2192 is =[ rewire r ]= Rewire.rewire r is\n\n{-\n _>>>-spec_ : \u2200 {i m o} {c\u2080 : C i m} {c\u2081 : C m o} {is ms os} \u2192\n is =[ c\u2080 ]= ms \u2192 ms =[ c\u2081 ]= os \u2192 is =[ c\u2080 >>> c\u2081 ]= os\n\n _***-spec_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} {c\u2080 : C i\u2080 o\u2080} {c\u2081 : C i\u2081 o\u2081} {is\u2080 is\u2081 os\u2080 os\u2081} \u2192\n is\u2080 =[ c\u2080 ]= os\u2080 \u2192 is\u2081 =[ c\u2081 ]= os\u2081 \u2192 (is\u2080 ++ is\u2081) =[ c\u2080 *** c\u2081 ]= (os\u2080 ++ os\u2081)\n\n-}\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idC ]= bs\n{-\n rewireWithTbl-spec : \u2200 {i o} (t : RewireTbl i o) is\n \u2192 is =[ rewireWithTbl t ]= Rewire.rewireTbl t is\n rewireWithTbl-spec t is = {!rewire-spec ? ?!}\n-}\n\n idCDefault-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idCDefault ]= bs\n idCDefault-spec bs\n = subst (\u03bb bs' \u2192 bs =[ idCDefault ]= bs') (tabulate\u2218lookup bs) (rewire-spec id bs)\n\n sink : \u2200 i \u2192 C i 0\n sink _ = rewire (\u03bb())\n-- sink-spec : bs =[ sink i ]= []\n\n dup\u2081 : \u2200 o \u2192 C 1 o\n dup\u2081 _ = rewire (const zero)\n-- dup\u2081-spec : (b \u2237 []) =[ dup o ]= replicate o\n\n dup\u2081\u00b2 : C 1 2\n dup\u2081\u00b2 = dup\u2081 2\n-- dup\u2081\u00b2-spec : (b \u2237 []) =[ dup\u2081\u00b2 ]= (b \u2237 b \u2237 [])\n\n vcat : \u2200 {i o n} \u2192 Vec (C i o) n \u2192 C (n * i) (n * o)\n vcat [] = idC\n vcat (x \u2237 xs) = x *** vcat xs\n\n coerce : \u2200 {i o} \u2192 i \u2261 o \u2192 C i o\n coerce refl = idC\n\n{-\n coerce-spec : \u2200 {i o} {i\u2261o : i \u2261 o} {is} \u2192\n is =[ coerce i\u2261o ]= subst Bits i\u2261o is\n coerce-spec = {!!}\n-}\n\n {-\n Sends inputs 0,1,2,...,n to outputs 0,1,2,...,n,0,1,2,...,n,0,1,2,...,n,...\n -}\n dup\u207f : \u2200 {i} k \u2192 C i (k * i)\n dup\u207f {i} k = rewireWithTbl (concat (replicate {n = k} (allFin i)))\n\n{-\n dup\u207f-spec : \u2200 {i} {is : Bits i} k \u2192 is =[ dup\u207f k ]= concat (replicate {n = k} is)\n dup\u207f-spec {i} {is} k = {!rewireWithTbl-spec (concat (replicate {n = k} (allFin _))) ?!}\n-}\n\n {-\n Sends inputs 0,1,2,...,n to outputs 0,0,0,...,1,1,1,...,2,2,2,...,n,n,n,...\n -}\n dup\u207f\u2032 : \u2200 {i} k \u2192 C i (i * k)\n dup\u207f\u2032 {i} k = rewireWithTbl (concat (vmap replicate (allFin i)))\n{-\n dup\u207f\u2032 {i} k = coerce (proj\u2082 \u2115\u00b0.*-identity i) >>>\n (vcat {n = i} (replicate (dup\u2081 _)))\n-}\n\n dup\u00b2 : \u2200 {i} \u2192 C i (i + i)\n dup\u00b2 {i} = dup\u207f 2 >>> coerce (cong (_+_ i) (sym (\u2115\u00b0.+-comm 0 i)))\n\n{-\n dup\u00b2-spec : \u2200 {n} {is : Bits n} \u2192 is =[ dup\u00b2 ]= (is ++ is)\n dup\u00b2-spec = coerce-spec (rewireWithTbl-spec {!!} {!!})\n-}\n\n _&&&_ : \u2200 {i o\u2080 o\u2081} \u2192 C i o\u2080 \u2192 C i o\u2081 \u2192 C i (o\u2080 + o\u2081)\n c\u2080 &&& c\u2081 = dup\u00b2 >>> c\u2080 *** c\u2081\n\n{-\n _&&&-spec_ : \u2200 {i o\u2080 o\u2081} {c\u2080 : C i o\u2080} {c\u2081 : C i o\u2081} {is os\u2080 os\u2081}\n \u2192 is =[ c\u2080 ]= os\u2080 \u2192 is =[ c\u2081 ]= os\u2081 \u2192 is =[ c\u2080 &&& c\u2081 ]= (os\u2080 ++ os\u2081)\n pf\u2080 &&&-spec pf\u2081 = dup\u00b2-spec >>>-spec (pf\u2080 ***-spec pf\u2081)\n-}\n\n ext-before : \u2200 {k i o} \u2192 C i o \u2192 C (k + i) (k + o)\n ext-before {k} c = idC {k} *** c\n\n ext-after : \u2200 {k i o} \u2192 C i o \u2192 C (i + k) (o + k)\n ext-after c = c *** idC\n\n commC : \u2200 m n \u2192 C (m + n) (n + m)\n commC m n = rewireWithTbl (vmap (raise m) (allFin n) ++ vmap (inject+ n) (allFin m))\n\n dropC : \u2200 {i} k \u2192 C (k + i) i\n dropC k = sink k *** idC\n\n takeC : \u2200 {i} k \u2192 C (k + i) k\n takeC {i} k = commC k _ >>> dropC i\n\n swap : \u2200 {i} (x y : Fin i) \u2192 C i i\n -- swap x y = arr (\u03bb xs \u2192 (xs [ x ]\u2254 (lookup y xs)) [ y ]\u2254 (lookup x xs))\n swap x y = rewire (Fin.swap x y)\n\n rev : \u2200 {i} \u2192 C i i\n rev = rewire Fin.reverse\n\n swap\u2082 : C 2 2\n swap\u2082 = rev\n -- swap\u2082 = swap (# 0) (# 1)\n\n Perm : \u2115 \u2192 Set\n Perm n = List (Fin n \u00d7 Fin n)\n\n perm : \u2200 {i} \u2192 Perm i \u2192 C i i\n perm [] = idC\n perm ((x , y) \u2237 \u03c0) = swap x y >>> perm \u03c0\n\n headC : \u2200 {i} \u2192 C (1 + i) 1\n headC = takeC 1\n\n tailC : \u2200 {i} \u2192 C (1 + i) i\n tailC = dropC 1\n\nrecord CircuitBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n field\n isRewiringBuilder : RewiringBuilder C\n arr : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n leafC : \u2200 {o} \u2192 Bits o \u2192 C 0 o\n forkC : \u2200 {i o} (c\u2080 c\u2081 : C i o) \u2192 C (1 + i) o\n\n open RewiringBuilder isRewiringBuilder\n\n{-\n field\n leafC-spec : \u2200 {o} (os : Bits o) \u2192 [] =[ leafC os ]= os\n forkC-left-spec : \u2200 {i o} {c\u2080 c\u2081 : C i o} {is os}\n \u2192 is =[ c\u2080 ]= os \u2192 (0\u2237 is) =[ forkC c\u2080 c\u2081 ]= os\n forkC-right-spec : \u2200 {i o} {c\u2080 c\u2081 : C i o} {is os}\n \u2192 is =[ c\u2081 ]= os \u2192 (1\u2237 is) =[ forkC c\u2080 c\u2081 ]= os\n-}\n bit : Bit \u2192 C 0 1\n bit b = leafC (b \u2237 [])\n\n{-\n bit-spec : \u2200 b \u2192 [] =[ bit b ]= (b \u2237 [])\n bit-spec b = leafC-spec (b \u2237 [])\n-}\n\n 0\u02b7 : C 0 1\n 0\u02b7 = bit 0b\n\n 0\u02b7\u207f : \u2200 {o} \u2192 C 0 o\n 0\u02b7\u207f = leafC 0\u207f\n\n{-\n 0\u02b7-spec : [] =[ 0\u02b7 ]= 0\u2237 []\n 0\u02b7-spec = bit-spec 0b\n-}\n\n 1\u02b7 : C 0 1\n 1\u02b7 = bit 1b\n\n 1\u02b7\u207f : \u2200 {o} \u2192 C 0 o\n 1\u02b7\u207f = leafC 1\u207f\n\n{-\n 1\u02b7-spec : [] =[ 1\u02b7 ]= 1\u2237 []\n 1\u02b7-spec = bit-spec 1b\n-}\n\n padL : \u2200 {i} k \u2192 C i (k + i)\n padL k = 0\u02b7\u207f {k} *** idC\n\n padR : \u2200 {i} k \u2192 C i (i + k)\n padR k = padL k >>> commC k _\n\n arr' : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n arr' {zero} f = leafC (f [])\n arr' {suc i} f = forkC (arr' {i} (f \u2218 0\u2237_)) (arr' (f \u2218 1\u2237_))\n\n unOp : (Bit \u2192 Bit) \u2192 C 1 1\n unOp op = arr (\u03bb { (x \u2237 []) \u2192 (op x) \u2237 [] })\n\n notC : C 1 1\n notC = unOp not\n\n binOp : (Bit \u2192 Bit \u2192 Bit) \u2192 C 2 1\n binOp op = arr (\u03bb { (x \u2237 y \u2237 []) \u2192 (op x y) \u2237 [] })\n\n terOp : (Bit \u2192 Bit \u2192 Bit \u2192 Bit) \u2192 C 3 1\n terOp op = arr (\u03bb { (x \u2237 y \u2237 z \u2237 []) \u2192 (op x y z) \u2237 [] })\n\n xorC : C 2 1\n xorC = binOp _xor_\n\n eqC : C 2 1\n eqC = binOp _==\u1d47_\n\n orC : C 2 1\n orC = binOp _\u2228_\n\n andC : C 2 1\n andC = binOp _\u2227_\n\n norC : C 2 1\n norC = binOp (\u03bb x y \u2192 not (x \u2228 y))\n\n nandC : C 2 1\n nandC = binOp (\u03bb x y \u2192 not (x \u2227 y))\n\n if\u27e8head=0\u27e9then_else_ : \u2200 {i o} (c\u2080 c\u2081 : C i o) \u2192 C (1 + i) o\n if\u27e8head=0\u27e9then_else_ = forkC\n\n -- Base addition with carry:\n -- * Any input can be used as the carry\n -- * First output is the carry\n --\n -- This can also be seen as the addition of\n -- three bits which result is between 0 and 3\n -- and thus fits in two bit word in binary\n -- representation.\n --\n -- Alternatively you can see it as counting the\n -- number of 1s in a three bits vector.\n add\u2082 : C 3 2\n add\u2082 = carry &&& result\n where carry : C 3 1\n carry = terOp (\u03bb x y z \u2192 x xor (y \u2227 z))\n result : C 3 1\n result = terOp (\u03bb x y z \u2192 x xor (y xor z))\n\n open RewiringBuilder isRewiringBuilder public\n\n_\u2192\u1d47_ : CircuitType\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n_\u2192\u1da0_ : CircuitType\ni \u2192\u1da0 o = Fin o \u2192 Fin i\n\nfinFunRewiringBuilder : RewiringBuilder _\u2192\u1da0_\nfinFunRewiringBuilder = mk id _\u2218\u2032_ _***_ id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1da0_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = Rewire.rewire f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec = tabulate\u2218lookup\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n _***_ {o\u2080 = o\u2080} f g x with Fin.cmp o\u2080 _ x\n _***_ f _ ._ | Fin.bound x = inject+ _ (f x)\n _***_ {i\u2080} _ g ._ | Fin.free x = raise i\u2080 (g x)\n\ntblRewiringBuilder : RewiringBuilder RewireTbl\ntblRewiringBuilder = mk tabulate _>>>_ _***_ (allFin _) _=[_]=_ rewire-spec idC-spec\n where\n open Rewire\n C = RewireTbl\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tbl ]= output = Rewire.rewireTbl tbl input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ tabulate r ]= Rewire.rewire r bs\n rewire-spec r bs = sym (tabulate-\u2218 (flip lookup bs) r)\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ allFin _ ]= bs\n -- idC-spec = map-lookup-allFin\n idC-spec bs rewrite rewire-spec id bs = tabulate\u2218lookup bs\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n c\u2080 >>> c\u2081 = rewireTbl c\u2081 c\u2080\n -- c\u2080 >>> c\u2081 = tabulate (flip lookup c\u2080 \u2218 flip lookup c\u2081)\n -- c\u2080 >>> c\u2081 = vmap (flip lookup c\u2080) c\u2081\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n _***_ {i\u2080} c\u2080 c\u2081 = vmap (inject+ _) c\u2080 ++ vmap (raise i\u2080) c\u2081\n\nbitsFunRewiringBuilder : RewiringBuilder _\u2192\u1d47_\nbitsFunRewiringBuilder = mk Rewire.rewire _>>>_ _***_ id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1d47_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ Rewire.rewire r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec bs = refl\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n f >>> g = g \u2218 f\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n (f *** g) xs with splitAt _ xs\n ... | ys , zs , _ = f ys ++ g zs\n\nbitsFunCircuitBuilder : CircuitBuilder _\u2192\u1d47_\nbitsFunCircuitBuilder = mk bitsFunRewiringBuilder id (\u03bb { bs [] \u2192 bs }) (\u03bb { f g (b \u2237 bs) \u2192 (if b then f else g) bs })\n\nopen import bintree\nopen import flipbased-tree\n\nTreeBits : CircuitType\nTreeBits i o = Tree (Bits o) i\n\nmodule moretree where\n _>>>_ : \u2200 {m n a} {A : Set a} \u2192 Tree (Bits m) n \u2192 Tree A m \u2192 Tree A n\n f >>> g = map (flip bintree.lookup g) f\n\ntreeBitsRewiringBuilder : RewiringBuilder TreeBits\ntreeBitsRewiringBuilder = mk rewire moretree._>>>_ _***_ (rewire id) _=[_]=_ rewire-spec idC-spec\n where\n C = TreeBits\n\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n rewire f = fromFun (Rewire.rewire f)\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tree ]= output = toFun tree input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ rewire r ]= Rewire.rewire r bs\n rewire-spec r = toFun\u2218fromFun (tabulate \u2218 flip (Data.Vec.lookup \u2218 r))\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ rewire id ]= bs\n idC-spec bs rewrite toFun\u2218fromFun (tabulate \u2218 flip Data.Vec.lookup) bs | tabulate\u2218lookup bs = refl\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n (f *** g) = f >>= \u03bb xs \u2192 map (_++_ xs) g\n\ntreeBitsCircuitBuilder : CircuitBuilder TreeBits\ntreeBitsCircuitBuilder = mk treeBitsRewiringBuilder arr leaf fork\n where\n C = TreeBits\n\n arr : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n arr = fromFun\n\nRewiringTree : CircuitType\nRewiringTree i o = Tree (Fin i) o\n\nmodule RewiringWith2^Outputs where\n C_\u27e82^_\u27e9 = RewiringTree\n\n rewire : \u2200 {i o} \u2192 RewireFun i (2 ^ o) \u2192 C i \u27e82^ o \u27e9\n rewire f = fromFun (f \u2218 toFin)\n\n lookupFin : \u2200 {i o} \u2192 C i \u27e82^ o \u27e9 \u2192 Fin (2 ^ o) \u2192 Fin i\n lookupFin c x = bintree.lookup (fromFin x) c\n\n _>>>_ : \u2200 {i m o} \u2192 C i \u27e82^ m \u27e9 \u2192 C (2 ^ m) \u27e82^ o \u27e9 \u2192 C i \u27e82^ o \u27e9\n f >>> g = rewire (lookupFin f \u2218 lookupFin g)\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 \u27e82^ o\u2080 \u27e9 \u2192 C i\u2081 \u27e82^ o\u2081 \u27e9 \u2192 C (i\u2080 + i\u2081) \u27e82^ (o\u2080 + o\u2081) \u27e9\n f *** g = f >>= \u03bb x \u2192 map (Fin._+\u2032_ x) g\n\nmodule Test where\n open import Data.Bits.Bits4\n\n tbl : RewireTbl 4 4\n tbl = # 1 \u2237 # 0 \u2237 # 2 \u2237 # 2 \u2237 []\n\n fun : RewireFun 4 4\n fun zero = # 1\n fun (suc zero) = # 0\n fun (suc (suc zero)) = # 2\n fun (suc (suc (suc x))) = # 2\n\n-- swap x y \u2248 swap y x\n-- reverse \u2218 reverse \u2248 id\n\n abs : \u2200 {C} \u2192 RewiringBuilder C \u2192 C 4 4\n abs builder = swap\u2082 *** dup\u2081\u00b2 *** sink 1\n where open RewiringBuilder builder\n\n tinytree : Tree (Fin 4) 2\n tinytree = fork (fork (leaf (# 1)) (leaf (# 0))) (fork (leaf (# 2)) (leaf (# 2)))\n\n bigtree : Tree (Bits 4) 4\n bigtree = fork (fork (fork (same 0000b) (same 0011b)) (fork (same 1000b) (same 1011b)))\n (fork (fork (same 0100b) (same 0111b)) (fork (same 1100b) (same 1111b)))\n where same : \u2200 {n} {A : Set} \u2192 A \u2192 Tree A (suc n)\n same x = fork (leaf x) (leaf x)\n\n test\u2081 : tbl \u2261 tabulate fun\n test\u2081 = refl\n\n test\u2082 : tbl \u2261 abs tblRewiringBuilder\n test\u2082 = refl\n\n test\u2083 : tabulate fun \u2261 tabulate (abs finFunRewiringBuilder)\n test\u2083 = refl\n\n test\u2084 : bigtree \u2261 abs treeBitsRewiringBuilder\n test\u2084 = refl\n\n -- open RewiringWith2^Outputs\n -- test\u2085 : tabulate (lookupFin tinytree) \u2261 tbl\n -- test\u2085 = refl\n","old_contents":"module circuit where\n\nopen import Function\nopen import Data.Nat.NP hiding (_\u225f_; compare)\nopen import Data.Bits\nopen import Data.Bits.Bits2\nopen import Data.Bool hiding (_\u225f_)\nopen import Data.Product hiding (swap; map)\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc; inject+; raise; #_)\nopen import Data.List using (List; []; _\u2237_)\nopen import Data.Vec using (Vec; []; _\u2237_; foldr; _[_]\u2254_; lookup; _++_; splitAt; tabulate; allFin; concat) renaming (map to vmap)\nopen import Data.Vec.Properties\nopen import Relation.Nullary.Decidable hiding (map)\nopen import Relation.Binary.PropositionalEquality\n\nCircuitType : Set\u2081\nCircuitType = (i o : \u2115) \u2192 Set\n\nRunCircuit : CircuitType \u2192 Set\nRunCircuit C = \u2200 {i o} \u2192 C i o \u2192 Bits i \u2192 Bits o\n\nmodule Rewire where\n RewireFun : CircuitType\n RewireFun i o = Fin o \u2192 Fin i\n\n RewireTbl : CircuitType\n RewireTbl i o = Vec (Fin i) o\n\n rewire : \u2200 {a i o} {A : Set a} \u2192 (Fin o \u2192 Fin i) \u2192 Vec A i \u2192 Vec A o\n rewire f v = tabulate (flip lookup v \u2218 f)\n\n rewireTbl : \u2200 {a i o} {A : Set a} \u2192 RewireTbl i o \u2192 Vec A i \u2192 Vec A o\n rewireTbl tbl v = vmap (flip lookup v) tbl\n\n runRewireFun : RunCircuit RewireFun\n runRewireFun = rewire\n\n runRewireTbl : RunCircuit RewireTbl\n runRewireTbl = rewireTbl\n\nopen Rewire using (RewireTbl; RewireFun)\n\nrecord RewiringBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n\n infixr 1 _>>>_\n infixr 3 _***_\n\n field\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n\n rewireWithTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 C i o\n rewireWithTbl = rewire \u2218 flip lookup\n\n idCDefault : \u2200 {i} \u2192 C i i\n idCDefault = rewire id\n\n field\n idC : \u2200 {i} \u2192 C i i\n\n field\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) is \u2192 is =[ rewire r ]= Rewire.rewire r is\n\n{-\n _>>>-spec_ : \u2200 {i m o} {c\u2080 : C i m} {c\u2081 : C m o} {is ms os} \u2192\n is =[ c\u2080 ]= ms \u2192 ms =[ c\u2081 ]= os \u2192 is =[ c\u2080 >>> c\u2081 ]= os\n\n _***-spec_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} {c\u2080 : C i\u2080 o\u2080} {c\u2081 : C i\u2081 o\u2081} {is\u2080 is\u2081 os\u2080 os\u2081} \u2192\n is\u2080 =[ c\u2080 ]= os\u2080 \u2192 is\u2081 =[ c\u2081 ]= os\u2081 \u2192 (is\u2080 ++ is\u2081) =[ c\u2080 *** c\u2081 ]= (os\u2080 ++ os\u2081)\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idC ]= bs\n-}\n{-\n rewireWithTbl-spec : \u2200 {i o} (t : RewireTbl i o) is\n \u2192 is =[ rewireWithTbl t ]= Rewire.rewireTbl t is\n rewireWithTbl-spec t is = {!rewire-spec ? ?!}\n-}\n\n idCDefault-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idCDefault ]= bs\n idCDefault-spec bs\n = subst (\u03bb bs' \u2192 bs =[ idCDefault ]= bs') (tabulate\u2218lookup bs) (rewire-spec id bs)\n\n sink : \u2200 i \u2192 C i 0\n sink _ = rewire (\u03bb())\n-- sink-spec : bs =[ sink i ]= []\n\n dup\u2081 : \u2200 o \u2192 C 1 o\n dup\u2081 _ = rewire (const zero)\n-- dup\u2081-spec : (b \u2237 []) =[ dup o ]= replicate o\n\n dup\u2081\u00b2 : C 1 2\n dup\u2081\u00b2 = dup\u2081 2\n-- dup\u2081\u00b2-spec : (b \u2237 []) =[ dup\u2081\u00b2 ]= (b \u2237 b \u2237 [])\n\n vcat : \u2200 {i o n} \u2192 Vec (C i o) n \u2192 C (n * i) (n * o)\n vcat [] = idC\n vcat (x \u2237 xs) = x *** vcat xs\n\n coerce : \u2200 {i o} \u2192 i \u2261 o \u2192 C i o\n coerce refl = idC\n\n{-\n coerce-spec : \u2200 {i o} {i\u2261o : i \u2261 o} {is} \u2192\n is =[ coerce i\u2261o ]= subst Bits i\u2261o is\n coerce-spec = {!!}\n-}\n\n {-\n Sends inputs 0,1,2,...,n to outputs 0,1,2,...,n,0,1,2,...,n,0,1,2,...,n,...\n -}\n dup\u207f : \u2200 {i} k \u2192 C i (k * i)\n dup\u207f {i} k = rewireWithTbl (concat (replicate {n = k} (allFin i)))\n\n{-\n dup\u207f-spec : \u2200 {i} {is : Bits i} k \u2192 is =[ dup\u207f k ]= concat (replicate {n = k} is)\n dup\u207f-spec {i} {is} k = {!rewireWithTbl-spec (concat (replicate {n = k} (allFin _))) ?!}\n-}\n\n {-\n Sends inputs 0,1,2,...,n to outputs 0,0,0,...,1,1,1,...,2,2,2,...,n,n,n,...\n -}\n dup\u207f\u2032 : \u2200 {i} k \u2192 C i (i * k)\n dup\u207f\u2032 {i} k = rewireWithTbl (concat (vmap replicate (allFin i)))\n{-\n dup\u207f\u2032 {i} k = coerce (proj\u2082 \u2115\u00b0.*-identity i) >>>\n (vcat {n = i} (replicate (dup\u2081 _)))\n-}\n\n dup\u00b2 : \u2200 {i} \u2192 C i (i + i)\n dup\u00b2 {i} = dup\u207f 2 >>> coerce (cong (_+_ i) (sym (\u2115\u00b0.+-comm 0 i)))\n\n{-\n dup\u00b2-spec : \u2200 {n} {is : Bits n} \u2192 is =[ dup\u00b2 ]= (is ++ is)\n dup\u00b2-spec = coerce-spec (rewireWithTbl-spec {!!} {!!})\n-}\n\n _&&&_ : \u2200 {i o\u2080 o\u2081} \u2192 C i o\u2080 \u2192 C i o\u2081 \u2192 C i (o\u2080 + o\u2081)\n c\u2080 &&& c\u2081 = dup\u00b2 >>> c\u2080 *** c\u2081\n\n{-\n _&&&-spec_ : \u2200 {i o\u2080 o\u2081} {c\u2080 : C i o\u2080} {c\u2081 : C i o\u2081} {is os\u2080 os\u2081}\n \u2192 is =[ c\u2080 ]= os\u2080 \u2192 is =[ c\u2081 ]= os\u2081 \u2192 is =[ c\u2080 &&& c\u2081 ]= (os\u2080 ++ os\u2081)\n pf\u2080 &&&-spec pf\u2081 = dup\u00b2-spec >>>-spec (pf\u2080 ***-spec pf\u2081)\n-}\n\n ext-before : \u2200 {k i o} \u2192 C i o \u2192 C (k + i) (k + o)\n ext-before {k} c = idC {k} *** c\n\n ext-after : \u2200 {k i o} \u2192 C i o \u2192 C (i + k) (o + k)\n ext-after c = c *** idC\n\n commC : \u2200 m n \u2192 C (m + n) (n + m)\n commC m n = rewireWithTbl (vmap (raise m) (allFin n) ++ vmap (inject+ n) (allFin m))\n\n dropC : \u2200 {i} k \u2192 C (k + i) i\n dropC k = sink k *** idC\n\n takeC : \u2200 {i} k \u2192 C (k + i) k\n takeC {i} k = commC k _ >>> dropC i\n\n swap : \u2200 {i} (x y : Fin i) \u2192 C i i\n -- swap x y = arr (\u03bb xs \u2192 (xs [ x ]\u2254 (lookup y xs)) [ y ]\u2254 (lookup x xs))\n swap x y = rewire (Fin.swap x y)\n\n rev : \u2200 {i} \u2192 C i i\n rev = rewire Fin.reverse\n\n swap\u2082 : C 2 2\n swap\u2082 = rev\n -- swap\u2082 = swap (# 0) (# 1)\n\n Perm : \u2115 \u2192 Set\n Perm n = List (Fin n \u00d7 Fin n)\n\n perm : \u2200 {i} \u2192 Perm i \u2192 C i i\n perm [] = idC\n perm ((x , y) \u2237 \u03c0) = swap x y >>> perm \u03c0\n\n headC : \u2200 {i} \u2192 C (1 + i) 1\n headC = takeC 1\n\n tailC : \u2200 {i} \u2192 C (1 + i) i\n tailC = dropC 1\n\nrecord CircuitBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n field\n isRewiringBuilder : RewiringBuilder C\n arr : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n leafC : \u2200 {o} \u2192 Bits o \u2192 C 0 o\n forkC : \u2200 {i o} (c\u2080 c\u2081 : C i o) \u2192 C (1 + i) o\n\n open RewiringBuilder isRewiringBuilder\n\n{-\n field\n leafC-spec : \u2200 {o} (os : Bits o) \u2192 [] =[ leafC os ]= os\n forkC-left-spec : \u2200 {i o} {c\u2080 c\u2081 : C i o} {is os}\n \u2192 is =[ c\u2080 ]= os \u2192 (0\u2237 is) =[ forkC c\u2080 c\u2081 ]= os\n forkC-right-spec : \u2200 {i o} {c\u2080 c\u2081 : C i o} {is os}\n \u2192 is =[ c\u2081 ]= os \u2192 (1\u2237 is) =[ forkC c\u2080 c\u2081 ]= os\n-}\n bit : Bit \u2192 C 0 1\n bit b = leafC (b \u2237 [])\n\n{-\n bit-spec : \u2200 b \u2192 [] =[ bit b ]= (b \u2237 [])\n bit-spec b = leafC-spec (b \u2237 [])\n-}\n\n 0\u02b7 : C 0 1\n 0\u02b7 = bit 0b\n\n 0\u02b7\u207f : \u2200 {o} \u2192 C 0 o\n 0\u02b7\u207f = leafC 0\u207f\n\n{-\n 0\u02b7-spec : [] =[ 0\u02b7 ]= 0\u2237 []\n 0\u02b7-spec = bit-spec 0b\n-}\n\n 1\u02b7 : C 0 1\n 1\u02b7 = bit 1b\n\n 1\u02b7\u207f : \u2200 {o} \u2192 C 0 o\n 1\u02b7\u207f = leafC 1\u207f\n\n{-\n 1\u02b7-spec : [] =[ 1\u02b7 ]= 1\u2237 []\n 1\u02b7-spec = bit-spec 1b\n-}\n\n padL : \u2200 {i} k \u2192 C i (k + i)\n padL k = 0\u02b7\u207f {k} *** idC\n\n padR : \u2200 {i} k \u2192 C i (i + k)\n padR k = padL k >>> commC k _\n\n arr' : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n arr' {zero} f = leafC (f [])\n arr' {suc i} f = forkC (arr' {i} (f \u2218 0\u2237_)) (arr' (f \u2218 1\u2237_))\n\n unOp : (Bit \u2192 Bit) \u2192 C 1 1\n unOp op = arr (\u03bb { (x \u2237 []) \u2192 (op x) \u2237 [] })\n\n notC : C 1 1\n notC = unOp not\n\n binOp : (Bit \u2192 Bit \u2192 Bit) \u2192 C 2 1\n binOp op = arr (\u03bb { (x \u2237 y \u2237 []) \u2192 (op x y) \u2237 [] })\n\n terOp : (Bit \u2192 Bit \u2192 Bit \u2192 Bit) \u2192 C 3 1\n terOp op = arr (\u03bb { (x \u2237 y \u2237 z \u2237 []) \u2192 (op x y z) \u2237 [] })\n\n xorC : C 2 1\n xorC = binOp _xor_\n\n eqC : C 2 1\n eqC = binOp _==\u1d47_\n\n orC : C 2 1\n orC = binOp _\u2228_\n\n andC : C 2 1\n andC = binOp _\u2227_\n\n norC : C 2 1\n norC = binOp (\u03bb x y \u2192 not (x \u2228 y))\n\n nandC : C 2 1\n nandC = binOp (\u03bb x y \u2192 not (x \u2227 y))\n\n if\u27e8head=0\u27e9then_else_ : \u2200 {i o} (c\u2080 c\u2081 : C i o) \u2192 C (1 + i) o\n if\u27e8head=0\u27e9then_else_ = forkC\n\n -- Base addition with carry:\n -- * Any input can be used as the carry\n -- * First output is the carry\n --\n -- This can also be seen as the addition of\n -- three bits which result is between 0 and 3\n -- and thus fits in two bit word in binary\n -- representation.\n --\n -- Alternatively you can see it as counting the\n -- number of 1s in a three bits vector.\n add\u2082 : C 3 2\n add\u2082 = carry &&& result\n where carry : C 3 1\n carry = terOp (\u03bb x y z \u2192 x xor (y \u2227 z))\n result : C 3 1\n result = terOp (\u03bb x y z \u2192 x xor (y xor z))\n\n open RewiringBuilder isRewiringBuilder public\n\n_\u2192\u1d47_ : CircuitType\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n_\u2192\u1da0_ : CircuitType\ni \u2192\u1da0 o = Fin o \u2192 Fin i\n\nfinFunRewiringBuilder : RewiringBuilder _\u2192\u1da0_\nfinFunRewiringBuilder = mk id _\u2218\u2032_ _***_ id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1da0_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = Rewire.rewire f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec = tabulate\u2218lookup\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n _***_ {o\u2080 = o\u2080} f g x with Fin.cmp o\u2080 _ x\n _***_ f _ ._ | Fin.bound x = inject+ _ (f x)\n _***_ {i\u2080} _ g ._ | Fin.free x = raise i\u2080 (g x)\n\ntblRewiringBuilder : RewiringBuilder RewireTbl\ntblRewiringBuilder = mk tabulate _>>>_ _***_ (allFin _) _=[_]=_ rewire-spec idC-spec\n where\n open Rewire\n C = RewireTbl\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tbl ]= output = Rewire.rewireTbl tbl input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ tabulate r ]= Rewire.rewire r bs\n rewire-spec r bs = sym (tabulate-\u2218 (flip lookup bs) r)\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ allFin _ ]= bs\n -- idC-spec = map-lookup-allFin\n idC-spec bs rewrite rewire-spec id bs = tabulate\u2218lookup bs\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n c\u2080 >>> c\u2081 = rewireTbl c\u2081 c\u2080\n -- c\u2080 >>> c\u2081 = tabulate (flip lookup c\u2080 \u2218 flip lookup c\u2081)\n -- c\u2080 >>> c\u2081 = vmap (flip lookup c\u2080) c\u2081\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n _***_ {i\u2080} c\u2080 c\u2081 = vmap (inject+ _) c\u2080 ++ vmap (raise i\u2080) c\u2081\n\nbitsFunRewiringBuilder : RewiringBuilder _\u2192\u1d47_\nbitsFunRewiringBuilder = mk Rewire.rewire _>>>_ _***_ id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1d47_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ Rewire.rewire r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec bs = refl\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n f >>> g = g \u2218 f\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n (f *** g) xs with splitAt _ xs\n ... | ys , zs , _ = f ys ++ g zs\n\nbitsFunCircuitBuilder : CircuitBuilder _\u2192\u1d47_\nbitsFunCircuitBuilder = mk bitsFunRewiringBuilder id (\u03bb { bs [] \u2192 bs }) (\u03bb { f g (b \u2237 bs) \u2192 (if b then f else g) bs })\n\nopen import bintree\nopen import flipbased-tree\n\nTreeBits : CircuitType\nTreeBits i o = Tree (Bits o) i\n\nmodule moretree where\n _>>>_ : \u2200 {m n a} {A : Set a} \u2192 Tree (Bits m) n \u2192 Tree A m \u2192 Tree A n\n f >>> g = map (flip bintree.lookup g) f\n\ntreeBitsRewiringBuilder : RewiringBuilder TreeBits\ntreeBitsRewiringBuilder = mk rewire moretree._>>>_ _***_ (rewire id) _=[_]=_ rewire-spec idC-spec\n where\n C = TreeBits\n\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n rewire f = fromFun (Rewire.rewire f)\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tree ]= output = toFun tree input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ rewire r ]= Rewire.rewire r bs\n rewire-spec r = toFun\u2218fromFun (tabulate \u2218 flip (Data.Vec.lookup \u2218 r))\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ rewire id ]= bs\n idC-spec bs rewrite toFun\u2218fromFun (tabulate \u2218 flip Data.Vec.lookup) bs | tabulate\u2218lookup bs = refl\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n (f *** g) = f >>= \u03bb xs \u2192 map (_++_ xs) g\n\ntreeBitsCircuitBuilder : CircuitBuilder TreeBits\ntreeBitsCircuitBuilder = mk treeBitsRewiringBuilder arr leaf fork\n where\n C = TreeBits\n\n arr : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n arr = fromFun\n\nRewiringTree : CircuitType\nRewiringTree i o = Tree (Fin i) o\n\nmodule RewiringWith2^Outputs where\n C_\u27e82^_\u27e9 = RewiringTree\n\n rewire : \u2200 {i o} \u2192 RewireFun i (2 ^ o) \u2192 C i \u27e82^ o \u27e9\n rewire f = fromFun (f \u2218 toFin)\n\n lookupFin : \u2200 {i o} \u2192 C i \u27e82^ o \u27e9 \u2192 Fin (2 ^ o) \u2192 Fin i\n lookupFin c x = bintree.lookup (fromFin x) c\n\n _>>>_ : \u2200 {i m o} \u2192 C i \u27e82^ m \u27e9 \u2192 C (2 ^ m) \u27e82^ o \u27e9 \u2192 C i \u27e82^ o \u27e9\n f >>> g = rewire (lookupFin f \u2218 lookupFin g)\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 \u27e82^ o\u2080 \u27e9 \u2192 C i\u2081 \u27e82^ o\u2081 \u27e9 \u2192 C (i\u2080 + i\u2081) \u27e82^ (o\u2080 + o\u2081) \u27e9\n f *** g = f >>= \u03bb x \u2192 map (Fin._+\u2032_ x) g\n\nmodule Test where\n open import Data.Bits.Bits4\n\n tbl : RewireTbl 4 4\n tbl = # 1 \u2237 # 0 \u2237 # 2 \u2237 # 2 \u2237 []\n\n fun : RewireFun 4 4\n fun zero = # 1\n fun (suc zero) = # 0\n fun (suc (suc zero)) = # 2\n fun (suc (suc (suc x))) = # 2\n\n-- swap x y \u2248 swap y x\n-- reverse \u2218 reverse \u2248 id\n\n abs : \u2200 {C} \u2192 RewiringBuilder C \u2192 C 4 4\n abs builder = swap\u2082 *** dup\u2081\u00b2 *** sink 1\n where open RewiringBuilder builder\n\n tinytree : Tree (Fin 4) 2\n tinytree = fork (fork (leaf (# 1)) (leaf (# 0))) (fork (leaf (# 2)) (leaf (# 2)))\n\n bigtree : Tree (Bits 4) 4\n bigtree = fork (fork (fork (same 0000b) (same 0011b)) (fork (same 1000b) (same 1011b)))\n (fork (fork (same 0100b) (same 0111b)) (fork (same 1100b) (same 1111b)))\n where same : \u2200 {n} {A : Set} \u2192 A \u2192 Tree A (suc n)\n same x = fork (leaf x) (leaf x)\n\n test\u2081 : tbl \u2261 tabulate fun\n test\u2081 = refl\n\n test\u2082 : tbl \u2261 abs tblRewiringBuilder\n test\u2082 = refl\n\n test\u2083 : tabulate fun \u2261 tabulate (abs finFunRewiringBuilder)\n test\u2083 = refl\n\n test\u2084 : bigtree \u2261 abs treeBitsRewiringBuilder\n test\u2084 = refl\n\n open RewiringWith2^Outputs\n test\u2085 : tabulate (lookupFin tinytree) \u2261 tbl\n test\u2085 = refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"7c497fbc194526d5d3f1a4335a6f74574050c633","subject":"Fix TotalNaturalSemantics.agda (fix #32).","message":"Fix TotalNaturalSemantics.agda (fix #32).\n\nOld-commit-hash: 3665667d355c2d049d92115cd27f98cc9dc4f3a2\n","repos":"inc-lc\/ilc-agda","old_file":"TotalNaturalSemantics.agda","new_file":"TotalNaturalSemantics.agda","new_contents":"module TotalNaturalSemantics where\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import meaning\nopen import Model\n--open import Changes\n--open import ChangeContexts\nopen import binding Type \u27e6_\u27e7Type\nopen import TotalTerms\n\nopen import Relation.Binary.PropositionalEquality\n\n-- NATURAL SEMANTICS\n\n-- (without support for \u0394 for now)\n\n-- Syntax\n\ndata Env : Context \u2192 Set\ndata Val : Type \u2192 Set\n\ndata Val where\n \u27e8abs_,_\u27e9 : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) (\u03c1 : Env \u0393) \u2192 Val (\u03c4\u2081 \u21d2 \u03c4\u2082)\n\ndata Env where\n \u2205 : Env \u2205\n _\u2022_ : \u2200 {\u0393 \u03c4} \u2192 Val \u03c4 \u2192 Env \u0393 \u2192 Env (\u03c4 \u2022 \u0393)\n\n-- Lookup\n\ninfixr 8 _\u22a2_\u2193_ _\u22a2_\u21a6_\n\ndata _\u22a2_\u21a6_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Var \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n this : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {v : Val \u03c4} \u2192\n v \u2022 \u03c1 \u22a2 this \u21a6 v\n that : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 x} {\u03c1 : Env \u0393} {v\u2081 : Val \u03c4\u2081} {v\u2082 : Val \u03c4\u2082} \u2192\n \u03c1 \u22a2 x \u21a6 v\u2082 \u2192\n v\u2081 \u2022 \u03c1 \u22a2 that x \u21a6 v\u2082\n\n-- Reduction\n\ndata _\u22a2_\u2193_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Term \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c1} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n \u03c1 \u22a2 abs t \u2193 \u27e8abs t , \u03c1 \u27e9\n app : \u2200 {\u0393 \u0393\u2032 \u03c4\u2081 \u03c4\u2082 \u03c1 \u03c1\u2032 v\u2082 v\u2032} {t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2082 : Term \u0393 \u03c4\u2081} {t\u2032 : Term (\u03c4\u2081 \u2022 \u0393\u2032) \u03c4\u2082} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 \u27e8abs t\u2032 , \u03c1\u2032 \u27e9 \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n v\u2082 \u2022 \u03c1\u2032 \u22a2 t\u2032 \u2193 v\u2032 \u2192\n \u03c1 \u22a2 app t\u2081 t\u2082 \u2193 v\u2032\n var : \u2200 {\u0393 \u03c4 x} {\u03c1 : Env \u0393} {v : Val \u03c4}\u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u03c1 \u22a2 var x \u2193 v\n\n-- SOUNDNESS of natural semantics\n\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 Env \u0393 \u2192 \u27e6 \u0393 \u27e7\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 \u27e8abs t , \u03c1 \u27e9 \u27e7Val = \u03bb v \u2192 \u27e6 t \u27e7 (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\nmeaningOfEnv : \u2200 {\u0393} \u2192 Meaning (Env \u0393)\nmeaningOfEnv = meaning \u27e6_\u27e7Env\n\nmeaningOfVal : \u2200 {\u03c4} \u2192 Meaning (Val \u03c4)\nmeaningOfVal = meaning \u27e6_\u27e7Val\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = \u2261-refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = \u2261-refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) =\n \u2261-trans\n (\u2261-cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082))\n (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n","old_contents":"module TotalNaturalSemantics where\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import meaning\nopen import Model\n--open import Changes\n--open import ChangeContexts\nopen import binding Type \u27e6_\u27e7Type\nopen import TotalTerms\n\n-- NATURAL SEMANTICS\n\n-- (without support for \u0394 for now)\n\n-- Syntax\n\ndata Env : Context \u2192 Set\ndata Val : Type \u2192 Set\n\ndata Val where\n \u27e8abs_,_\u27e9 : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) (\u03c1 : Env \u0393) \u2192 Val (\u03c4\u2081 \u21d2 \u03c4\u2082)\n\ndata Env where\n \u2205 : Env \u2205\n _\u2022_ : \u2200 {\u0393 \u03c4} \u2192 Val \u03c4 \u2192 Env \u0393 \u2192 Env (\u03c4 \u2022 \u0393)\n\n-- Lookup\n\ninfixr 8 _\u22a2_\u2193_ _\u22a2_\u21a6_\n\ndata _\u22a2_\u21a6_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Var \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n this : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {v : Val \u03c4} \u2192\n v \u2022 \u03c1 \u22a2 this \u21a6 v\n that : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 x} {\u03c1 : Env \u0393} {v\u2081 : Val \u03c4\u2081} {v\u2082 : Val \u03c4\u2082} \u2192\n \u03c1 \u22a2 x \u21a6 v\u2082 \u2192\n v\u2081 \u2022 \u03c1 \u22a2 that x \u21a6 v\u2082\n\n-- Reduction\n\ndata _\u22a2_\u2193_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Term \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c1} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n \u03c1 \u22a2 abs t \u2193 \u27e8abs t , \u03c1 \u27e9\n app : \u2200 {\u0393 \u0393\u2032 \u03c4\u2081 \u03c4\u2082 \u03c1 \u03c1\u2032 v\u2082 v\u2032} {t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2082 : Term \u0393 \u03c4\u2081} {t\u2032 : Term (\u03c4\u2081 \u2022 \u0393\u2032) \u03c4\u2082} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 \u27e8abs t\u2032 , \u03c1\u2032 \u27e9 \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n v\u2082 \u2022 \u03c1\u2032 \u22a2 t\u2032 \u2193 v\u2032 \u2192\n \u03c1 \u22a2 app t\u2081 t\u2082 \u2193 v\u2032\n var : \u2200 {\u0393 \u03c4 x} {\u03c1 : Env \u0393} {v : Val \u03c4}\u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u03c1 \u22a2 var x \u2193 v\n\n-- SOUNDNESS of natural semantics\n\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 Env \u0393 \u2192 \u27e6 \u0393 \u27e7\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 \u27e8abs t , \u03c1 \u27e9 \u27e7Val = \u03bb v \u2192 \u27e6 t \u27e7 (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\nmeaningOfEnv : \u2200 {\u0393} \u2192 Meaning (Env \u0393)\nmeaningOfEnv = meaning \u27e6_\u27e7Env\n\nmeaningOfVal : \u2200 {\u03c4} \u2192 Meaning (Val \u03c4)\nmeaningOfVal = meaning \u27e6_\u27e7Val\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = \u2261-refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = \u2261-refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) =\n \u2261-trans\n (\u2261-cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082))\n (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d9a070a53dc596858137ebe6d0a29d1de2b7d461","subject":"updating statement of finality","message":"updating statement of finality\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"finality.agda","new_file":"finality.agda","new_contents":"open import Prelude\nopen import core\n\nopen import progress-checks\n\nmodule finality where\n finality : \u03a3[ d \u2208 dhexp ] (d final \u00d7 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d'))) \u2192 \u22a5\n finality (\u03c01 , \u03c02 , \u03c03 , \u03c04) = final-not-step \u03c02 (\u03c03 , \u03c04)\n\n -- a slight restatement of the above, generalizing it to the\n -- multistep judgement\n finality* : \u2200{d d'} \u2192 d final \u2192 d \u21a6* d' \u2192 d == d'\n finality* fin MSRefl = refl\n finality* fin (MSStep x ms) = abort (final-not-step fin (_ , x))\n","old_contents":"open import Prelude\nopen import core\n\nopen import progress-checks\n\nmodule finality where\n finality : \u2200{d d'} \u2192 d final \u2192 d \u21a6* d' \u2192 d == d'\n finality fin MSRefl = refl\n finality fin (MSStep x ms) = abort (final-not-step fin (_ , x))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3ecae75aaafb233597792507020b6d74bee0ecfe","subject":"Implement map, union, difference on bags of natural numbers (#51)","message":"Implement map, union, difference on bags of natural numbers (#51)\n\nTODO before closing the issue:\n- Introduce bags and prove the stupid derivative of map correct.\n\nOld-commit-hash: 76c8ff03739579a6839431ba637557395ccbf8b2\n","repos":"inc-lc\/ilc-agda","old_file":"Data\/NatBag.agda","new_file":"Data\/NatBag.agda","new_contents":"-- An agda type isomorphic to bags of natural numbers\n\n-- How to import this file from anywhere in the world\n--\n-- 1. M-x customize-group RET agda2 RET\n-- 2. Look for the option `Agda2 Include Dirs`\n-- 3. Insert \"$ILC\/agda\", where $ILC is path to ILC repo on your computer\n-- 4. \"Save for future sessions\"\n-- 5. open import Data.NatBag\n\nmodule Data.NatBag where\n\nopen import Data.Nat hiding (zero) renaming (_+_ to plus)\nopen import Data.Integer renaming (suc to +1 ; pred to -1)\nopen import Data.Maybe.Core\nopen import Data.Sum hiding (map)\n\n-----------\n-- Types --\n-----------\n\ndata Nonzero : \u2124 \u2192 Set where\n negative : (n : \u2115) \u2192 Nonzero -[1+ n ]\n positive : (n : \u2115) \u2192 Nonzero (+ (suc n))\n\ndata EmptyBag : Set where\n \u2205 : EmptyBag\n\ndata NonemptyBag : Set where\n singleton : (i : \u2124) \u2192 (i\u22600 : Nonzero i) \u2192 NonemptyBag\n _\u2237_ : (i : \u2124) \u2192 (bag : NonemptyBag) \u2192 NonemptyBag\n\ninfixr 5 _\u2237_\n\nBag : Set\nBag = EmptyBag \u228e NonemptyBag\n\n-------------------\n-- Bag interface --\n-------------------\n\nempty : Bag\n\ninsert : \u2115 \u2192 Bag \u2192 Bag\n\ndelete : \u2115 \u2192 Bag \u2192 Bag\n\nlookup : \u2115 \u2192 Bag \u2192 \u2124\n\nupdate : \u2115 \u2192 \u2124 \u2192 Bag \u2192 Bag\n\nupdateWith : \u2115 \u2192 (\u2124 \u2192 \u2124) \u2192 Bag \u2192 Bag\n\nmap : (\u2115 \u2192 \u2115) \u2192 Bag \u2192 Bag -- map on bags, mapKeys on maps\n\nmap\u2082 : (\u2124 \u2192 \u2124) \u2192 Bag \u2192 Bag -- mapValues on maps\n\nzipWith : (\u2124 \u2192 \u2124 \u2192 \u2124) \u2192 Bag \u2192 Bag \u2192 Bag\n\n_++_ : Bag \u2192 Bag \u2192 Bag\ninfixr 5 _++_ -- right-associativity follows Haskell, for efficiency\n\n_\\\\_ : Bag \u2192 Bag \u2192 Bag\ninfixl 9 _\\\\_ -- fixity follows Haskell's Data.Map.(\\\\)\n\n--------------------\n-- Implementation --\n--------------------\n\ninsert n bag = updateWith n +1 bag\n\ndelete n bag = updateWith n -1 bag\n\nupdate n i bag = updateWith n (\u03bb _ \u2192 i) bag\n\n_++_ = zipWith _+_\n\n_\\\\_ = zipWith _-_\n\nzero : \u2124\nzero = + 0\n\n-- It is very easy to compute a proof that an integer is\n-- nonzero whenever such a thing exists.\nnonzero? : (i : \u2124) \u2192 Maybe (Nonzero i)\nnonzero? (+ 0) = nothing\nnonzero? (+ (suc n)) = just (positive n)\nnonzero? -[1+ n ] = just (negative n)\n\nempty = inj\u2081 \u2205\n\nmakeSingleton : \u2115 \u2192 (i : \u2124) \u2192 Nonzero i \u2192 NonemptyBag\nmakeSingleton 0 i i\u22600 = singleton i i\u22600\nmakeSingleton (suc n) i i\u22600 = (+ 0) \u2237 makeSingleton n i i\u22600\n\nupdateWith n f (inj\u2081 \u2205) with nonzero? (f zero)\n... | nothing = empty\n... | just i\u22600 = inj\u2082 (makeSingleton n (f zero) i\u22600)\nupdateWith 0 f (inj\u2082 (singleton i i\u22600)) with nonzero? (f i)\n... | nothing = empty\n... | just j\u22600 = inj\u2082 (singleton (f i) j\u22600)\nupdateWith (suc n) f (inj\u2082 (singleton i i\u22600)) with nonzero? (f zero)\n... | nothing = inj\u2082 (singleton i i\u22600)\n... | just j\u22600 = inj\u2082 (i \u2237 makeSingleton n (f zero) j\u22600)\nupdateWith 0 f (inj\u2082 (i \u2237 bag)) = inj\u2082 (f i \u2237 bag)\nupdateWith (suc n) f (inj\u2082 (i \u2237 y))\n with updateWith n f (inj\u2082 y) | nonzero? i\n... | inj\u2081 \u2205 | nothing = empty\n... | inj\u2081 \u2205 | just i\u22600 = inj\u2082 (singleton i i\u22600)\n... | inj\u2082 bag | _ = inj\u2082 (i \u2237 bag)\n\nlookupNonempty : \u2115 \u2192 NonemptyBag \u2192 \u2124\nlookupNonempty 0 (singleton i i\u22600) = i\nlookupNonempty (suc n) (singleton i i\u22600) = zero\nlookupNonempty 0 (i \u2237 bag) = i\nlookupNonempty (suc n) (i \u2237 bag) = lookupNonempty n bag\n\nlookup n (inj\u2081 \u2205) = zero\nlookup n (inj\u2082 bag) = lookupNonempty n bag\n\n-- It is possible to get empty bags by mapping over a nonempty bag:\n-- map (\u03bb _ \u2192 3) { 1 \u21d2 5 , 2 \u21d2 -5 }\nmapKeysFrom : \u2115 \u2192 (\u2115 \u2192 \u2115) \u2192 NonemptyBag \u2192 Bag\nmapKeysFrom n f (singleton i i\u22600) = inj\u2082 (makeSingleton (f n) i i\u22600)\nmapKeysFrom n f (i \u2237 bag) with nonzero? i\n... | nothing = mapKeysFrom (suc n) f bag\n... | just i\u22600 = updateWith (f n) (\u03bb j \u2192 j + i) (mapKeysFrom (suc n) f bag)\n\nmap f (inj\u2081 \u2205) = empty\nmap f (inj\u2082 bag) = mapKeysFrom 0 f bag\n\nmapNonempty\u2082 : (\u2124 \u2192 \u2124) \u2192 NonemptyBag \u2192 Bag\nmapNonempty\u2082 f (singleton i i\u22600) with nonzero? (f i)\n... | nothing = empty\n... | just j\u22600 = inj\u2082 (singleton (f i) j\u22600)\nmapNonempty\u2082 f (i \u2237 bag\u2080)\n with mapNonempty\u2082 f bag\u2080 | nonzero? (f i)\n... | inj\u2081 \u2205 | nothing = empty\n... | inj\u2081 \u2205 | just j\u22600 = inj\u2082 (singleton (f i) j\u22600)\n... | inj\u2082 bag | _ = inj\u2082 (f i \u2237 bag)\n\nmap\u2082 f (inj\u2081 \u2205) = empty\nmap\u2082 f (inj\u2082 b) = mapNonempty\u2082 f b\n\nzipLeft : (\u2124 \u2192 \u2124 \u2192 \u2124) \u2192 NonemptyBag \u2192 Bag\nzipLeft f b\u2081 = mapNonempty\u2082 (\u03bb x \u2192 f x zero) b\u2081\n\nzipRight : (\u2124 \u2192 \u2124 \u2192 \u2124) \u2192 NonemptyBag \u2192 Bag\nzipRight f b\u2082 = mapNonempty\u2082 (\u03bb y \u2192 f zero y) b\u2082\n\nzipNonempty : (\u2124 \u2192 \u2124 \u2192 \u2124) \u2192 NonemptyBag \u2192 NonemptyBag \u2192 Bag\nzipNonempty f (singleton i i\u22600) (singleton j j\u22600) with nonzero? (f i j)\n... | nothing = empty\n... | just k\u22600 = inj\u2082 (singleton (f i j) k\u22600)\nzipNonempty f (singleton i i\u22600) (j \u2237 b\u2082)\n with zipRight f b\u2082 | nonzero? (f i j)\n... | inj\u2081 \u2205 | nothing = empty\n... | inj\u2081 \u2205 | just k\u22600 = inj\u2082 (singleton (f i j) k\u22600)\n... | inj\u2082 bag | _ = inj\u2082 (f i j \u2237 bag)\nzipNonempty f (i \u2237 b\u2081) (singleton j j\u22600)\n with zipLeft f b\u2081 | nonzero? (f i j)\n... | inj\u2081 \u2205 | nothing = empty\n... | inj\u2081 \u2205 | just k\u22600 = inj\u2082 (singleton (f i j) k\u22600)\n... | inj\u2082 bag | _ = inj\u2082 (f i j \u2237 bag)\nzipNonempty f (i \u2237 b\u2081) (j \u2237 b\u2082)\n with zipNonempty f b\u2081 b\u2082 | nonzero? (f i j)\n... | inj\u2081 \u2205 | nothing = empty\n... | inj\u2081 \u2205 | just k\u22600 = inj\u2082 (singleton (f i j) k\u22600)\n... | inj\u2082 bag | _ = inj\u2082 (f i j \u2237 bag)\n\nzipWith f (inj\u2081 \u2205) (inj\u2081 \u2205) = empty\nzipWith f (inj\u2081 \u2205) (inj\u2082 b\u2082) = zipRight f b\u2082\nzipWith f (inj\u2082 b\u2081) (inj\u2081 \u2205) = zipLeft f b\u2081\nzipWith f (inj\u2082 b\u2081) (inj\u2082 b\u2082) = zipNonempty f b\u2081 b\u2082\n","old_contents":"-- An agda type isomorphic to bags of natural numbers\n\n-- How to import this file from anywhere in the world\n--\n-- 1. M-x customize-group RET agda2 RET\n-- 2. Look for the option `Agda2 Include Dirs`\n-- 3. Insert \"$ILC\/agda\", where $ILC is path to ILC repo on your computer\n-- 4. \"Save for future sessions\"\n-- 5. open import Data.NatBag\n\nmodule Data.NatBag where\n\nopen import Data.Nat hiding (zero) renaming (_+_ to plus)\nopen import Data.Integer renaming (suc to +1 ; pred to -1)\nopen import Data.Maybe.Core\nopen import Data.Sum\n\n-----------\n-- Types --\n-----------\n\ndata Nonzero : \u2124 \u2192 Set where\n negative : (n : \u2115) \u2192 Nonzero -[1+ n ]\n positive : (n : \u2115) \u2192 Nonzero (+ (suc n))\n\ndata EmptyBag : Set where\n \u2205 : EmptyBag\n\ndata NonemptyBag : Set where\n singleton : (i : \u2124) \u2192 (i\u22600 : Nonzero i) \u2192 NonemptyBag\n _\u2237_ : (i : \u2124) \u2192 (bag : NonemptyBag) \u2192 NonemptyBag\n\ninfixr 5 _\u2237_\n\nBag : Set\nBag = EmptyBag \u228e NonemptyBag\n\n-------------------\n-- Bag interface --\n-------------------\n\nempty : Bag\n\ninsert : \u2115 \u2192 Bag \u2192 Bag\n\ndelete : \u2115 \u2192 Bag \u2192 Bag\n\nlookup : \u2115 \u2192 Bag \u2192 \u2124\n\nupdate : \u2115 \u2192 \u2124 \u2192 Bag \u2192 Bag\n\nupdateWith : \u2115 \u2192 (\u2124 \u2192 \u2124) \u2192 Bag \u2192 Bag\n\nmap\u2081 : (\u2115 \u2192 \u2115) \u2192 Bag \u2192 Bag -- map on bags, mapKeys on maps\n\nmap\u2082 : (\u2124 \u2192 \u2124) \u2192 Bag \u2192 Bag -- mapValues on maps\n\n{- TODO: Wait for map.\nzipWith : (\u2124 \u2192 \u2124 \u2192 \u2124) \u2192 Bag \u2192 Bag \u2192 Bag\n\n_++_ : Bag \u2192 Bag \u2192 Bag\ninfixr 5 _++_ -- right-associativity follows Haskell, for efficiency\n\n_\\\\_ : Bag \u2192 Bag \u2192 Bag\ninfixl 9 _\\\\_ -- fixity follows Haskell's Data.Map.(\\\\)\n-}\n\n--------------------\n-- Implementation --\n--------------------\n\ninsert n bag = updateWith n +1 bag\n\ndelete n bag = updateWith n -1 bag\n\nupdate n i bag = updateWith n (\u03bb _ \u2192 i) bag\n\nzero : \u2124\nzero = + 0\n\n-- It is very easy to compute a proof that an integer is\n-- nonzero whenever such a thing exists.\nnonzero? : (i : \u2124) \u2192 Maybe (Nonzero i)\nnonzero? (+ 0) = nothing\nnonzero? (+ (suc n)) = just (positive n)\nnonzero? -[1+ n ] = just (negative n)\n\nempty = inj\u2081 \u2205\n\nmakeSingleton : \u2115 \u2192 (i : \u2124) \u2192 Nonzero i \u2192 NonemptyBag\nmakeSingleton 0 i i\u22600 = singleton i i\u22600\nmakeSingleton (suc n) i i\u22600 = (+ 0) \u2237 makeSingleton n i i\u22600\n\nupdateWith n f (inj\u2081 \u2205) with nonzero? (f zero)\n... | nothing = empty\n... | just i\u22600 = inj\u2082 (makeSingleton n (f zero) i\u22600)\nupdateWith 0 f (inj\u2082 (singleton i i\u22600)) with nonzero? (f i)\n... | nothing = empty\n... | just j\u22600 = inj\u2082 (singleton (f i) j\u22600)\nupdateWith (suc n) f (inj\u2082 (singleton i i\u22600)) with nonzero? (f zero)\n... | nothing = inj\u2082 (singleton i i\u22600)\n... | just j\u22600 = inj\u2082 (i \u2237 makeSingleton n (f zero) j\u22600)\nupdateWith 0 f (inj\u2082 (i \u2237 bag)) = inj\u2082 (f i \u2237 bag)\nupdateWith (suc n) f (inj\u2082 (i \u2237 y))\n with updateWith n f (inj\u2082 y) | nonzero? i\n... | inj\u2081 \u2205 | nothing = empty\n... | inj\u2081 \u2205 | just i\u22600 = inj\u2082 (singleton i i\u22600)\n... | inj\u2082 bag | _ = inj\u2082 (i \u2237 bag)\n\nlookupNonempty : \u2115 \u2192 NonemptyBag \u2192 \u2124\nlookupNonempty 0 (singleton i i\u22600) = i\nlookupNonempty (suc n) (singleton i i\u22600) = zero\nlookupNonempty 0 (i \u2237 bag) = i\nlookupNonempty (suc n) (i \u2237 bag) = lookupNonempty n bag\n\nlookup n (inj\u2081 \u2205) = zero\nlookup n (inj\u2082 bag) = lookupNonempty n bag\n\n-- It is possible to get empty bags by mapping over a nonempty bag:\n-- map\u2081 (\u03bb _ \u2192 3) { 1 \u21d2 5 , 2 \u21d2 -5 }\nmapKeysFrom : \u2115 \u2192 (\u2115 \u2192 \u2115) \u2192 NonemptyBag \u2192 Bag\nmapKeysFrom n f (singleton i i\u22600) = inj\u2082 (makeSingleton (f n) i i\u22600)\nmapKeysFrom n f (i \u2237 bag) with nonzero? i\n... | nothing = mapKeysFrom (suc n) f bag\n... | just i\u22600 = updateWith (f n) (\u03bb j \u2192 j + i) (mapKeysFrom (suc n) f bag)\n\nmap\u2081 f (inj\u2081 \u2205) = empty\nmap\u2081 f (inj\u2082 bag) = mapKeysFrom 0 f bag\n\nmap\u2082 f (inj\u2081 \u2205) = empty\nmap\u2082 f (inj\u2082 (singleton i i\u22600)) = {!!}\nmap\u2082 f (inj\u2082 (i \u2237 bag\u2080)) with map\u2082 f (inj\u2082 bag\u2080) | nonzero? i | nonzero? (f i)\n-- If an element has multiplicity 0, it should not be mapped over.\n-- Conceptually, we are mapping over the values of this infinite\n-- map not by f, but by {case 0 => 0}.orElse(f).\n... | inj\u2081 \u2205 | nothing | _ = empty\n... | inj\u2082 bag | nothing | _ = inj\u2082 (zero \u2237 bag)\n... | inj\u2081 \u2205 | just i\u22600 | nothing = empty\n... | inj\u2081 \u2205 | just i\u22600 | just j\u22600 = inj\u2082 (singleton (f i) j\u22600)\n... | inj\u2082 bag | just i\u22600 | _ = inj\u2082 (f i \u2237 bag)\n\n{- TODO: Redefine in terms of zipWith, which will be defined\n in terms of map.\n-- The union of nonempty bags (with possibly negative\n-- multiplicities) can be empty.\nunionNonempty : NonemptyBag \u2192 NonemptyBag \u2192 Bag\nunionNonempty (singleton i i\u22600) (singleton j j\u22600) with nonzero? (i + j)\n... | nothing = empty\n... | just k\u22600 = inj\u2082 (singleton (i + j) k\u22600)\nunionNonempty (singleton i i\u22600) (j \u2237 b\u2082) = inj\u2082 (i + j \u2237 b\u2082)\n\ninj\u2081 \u2205 ++ b\u2082 = b\u2082\nb\u2081 ++ inj\u2081 \u2205 = b\u2081\n(inj\u2082 b\u2081) ++ (inj\u2082 b\u2082) = (unionNonempty b\u2081 b\u2082)\n-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f44ff9c8dc76e1365c69137588490cc7705c8477","subject":"agda: rename datatype valid-\u0394 |-> Valid-\u0394","message":"agda: rename datatype valid-\u0394 |-> Valid-\u0394\n\nOld-commit-hash: 37b8db1e2601a886b5bcd31b170ad1560352b8a7\n","repos":"inc-lc\/ilc-agda","old_file":"Denotational\/ValidChanges.agda","new_file":"Denotational\/ValidChanges.agda","new_contents":"module Denotational.ValidChanges where\n\nopen import Data.Product\nopen import Data.Unit\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Equivalence\n\nopen import Changes\n\n-- DEFINITION of valid changes via a logical relation\n\n{-\nWhat I wanted to write:\n\ndata Valid\u0394 : {T : Type} \u2192 (v : \u27e6 T \u27e7) \u2192 (dv : \u27e6 \u0394-Type T \u27e7) \u2192 Set where\n base : (v : \u27e6 bool \u27e7) \u2192 (dv : \u27e6 \u0394-Type bool \u27e7) \u2192 Valid\u0394 v dv\n fun : \u2200 {S T} \u2192 (f : \u27e6 S \u21d2 T \u27e7) \u2192 (df : \u27e6 \u0394-Type (S \u21d2 T) \u27e7) \u2192\n (\u2200 (s : \u27e6 S \u27e7) ds (valid : Valid\u0394 s ds) \u2192 (Valid\u0394 (f s) (df s ds)) \u00d7 ((apply df f) (apply ds s) \u2261 apply (df s ds) (f s))) \u2192 \n Valid\u0394 f df\n-}\n-- What I had to write:\n-- Note: now I could go back to using a datatype, since the datatype is now strictly positive.\nValid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nValid-\u0394 {bool} v dv = \u22a4\nValid-\u0394 {S \u21d2 T} f df =\n \u2200 (s : \u27e6 S \u27e7) ds {- (valid-w : Valid-\u0394 s ds) -} \u2192\n Valid-\u0394 (f s) (df s ds) \u00d7\n (apply df f) (apply ds s) \u2261 apply (df s ds) (f s)\n\ndiff-is-valid : \u2200 {\u03c4} (v\u2032 v : \u27e6 \u03c4 \u27e7) \u2192 Valid-\u0394 {\u03c4} v (diff v\u2032 v)\ndiff-is-valid {bool} v\u2032 v = tt\ndiff-is-valid {\u03c4 \u21d2 \u03c4\u2081} v\u2032 v =\n \u03bb s ds \u2192\n diff-is-valid (v\u2032 (apply ds s)) (v s) , (\n begin\n apply (diff v\u2032 v) v (apply ds s)\n \u2261\u27e8 refl \u27e9\n apply\n (diff (v\u2032 (apply (derive (apply ds s)) (apply ds s))) (v (apply ds s)))\n (v (apply ds s))\n \u2261\u27e8 \u2261-cong (\u03bb x \u2192 apply (diff (v\u2032 x) (v (apply ds s))) (v (apply ds s))) (apply-derive (apply ds s)) \u27e9\n apply (diff (v\u2032 (apply ds s)) (v (apply ds s))) (v (apply ds s))\n \u2261\u27e8 apply-diff (v (apply ds s)) (v\u2032 (apply ds s)) \u27e9\n v\u2032 (apply ds s)\n \u2261\u27e8 sym (apply-diff (v s) (v\u2032 (apply ds s))) \u27e9\n apply ((diff v\u2032 v) s ds) (v s)\n \u220e) where open \u2261-Reasoning\n\nderive-is-valid : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192 Valid-\u0394 {\u03c4} v (derive v)\nderive-is-valid v rewrite sym (diff-derive v) = diff-is-valid v v\n\n-- This is a postulate elsewhere, but here I provide a proper proof.\n\ndiff-apply-proof : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n (Valid-\u0394 v dv) \u2192 diff (apply dv v) v \u2261 dv\n\ndiff-apply-proof {\u03c4\u2081 \u21d2 \u03c4\u2082} df f df-valid = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n begin\n diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (sym (proj\u2082 (df-valid (apply dv v) (derive (apply dv v))))) \u27e9\n diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff (apply df f x) (f v))\n (apply-derive (apply dv v)) \u27e9\n diff ((apply df f) (apply dv v)) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (proj\u2082 (df-valid v dv)) \u27e9\n diff (apply (df v dv) (f v)) (f v)\n \u2261\u27e8 diff-apply-proof {\u03c4\u2082} (df v dv) (f v) (proj\u2081 (df-valid v dv)) \u27e9\n df v dv\n \u220e)) where open \u2261-Reasoning\n\ndiff-apply-proof {bool} db b _ = xor-cancellative b db\n","old_contents":"module Denotational.ValidChanges where\n\nopen import Data.Product\nopen import Data.Unit\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Equivalence\n\nopen import Changes\n\n-- DEFINITION of valid changes via a logical relation\n\n{-\nWhat I wanted to write:\n\ndata Valid\u0394 : {T : Type} \u2192 (v : \u27e6 T \u27e7) \u2192 (dv : \u27e6 \u0394-Type T \u27e7) \u2192 Set where\n base : (v : \u27e6 bool \u27e7) \u2192 (dv : \u27e6 \u0394-Type bool \u27e7) \u2192 Valid\u0394 v dv\n fun : \u2200 {S T} \u2192 (f : \u27e6 S \u21d2 T \u27e7) \u2192 (df : \u27e6 \u0394-Type (S \u21d2 T) \u27e7) \u2192\n (\u2200 (s : \u27e6 S \u27e7) ds (valid : Valid\u0394 s ds) \u2192 (Valid\u0394 (f s) (df s ds)) \u00d7 ((apply df f) (apply ds s) \u2261 apply (df s ds) (f s))) \u2192 \n Valid\u0394 f df\n-}\n-- What I had to write:\n-- Note: now I could go back to using a datatype, since the datatype is now strictly positive.\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {bool} v dv = \u22a4\nvalid-\u0394 {S \u21d2 T} f df =\n \u2200 (s : \u27e6 S \u27e7) ds {- (valid-w : valid-\u0394 s ds) -} \u2192\n valid-\u0394 (f s) (df s ds) \u00d7\n (apply df f) (apply ds s) \u2261 apply (df s ds) (f s)\n\ndiff-is-valid : \u2200 {\u03c4} (v\u2032 v : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} v (diff v\u2032 v)\ndiff-is-valid {bool} v\u2032 v = tt\ndiff-is-valid {\u03c4 \u21d2 \u03c4\u2081} v\u2032 v =\n \u03bb s ds \u2192\n diff-is-valid (v\u2032 (apply ds s)) (v s) , (\n begin\n apply (diff v\u2032 v) v (apply ds s)\n \u2261\u27e8 refl \u27e9\n apply\n (diff (v\u2032 (apply (derive (apply ds s)) (apply ds s))) (v (apply ds s)))\n (v (apply ds s))\n \u2261\u27e8 \u2261-cong (\u03bb x \u2192 apply (diff (v\u2032 x) (v (apply ds s))) (v (apply ds s))) (apply-derive (apply ds s)) \u27e9\n apply (diff (v\u2032 (apply ds s)) (v (apply ds s))) (v (apply ds s))\n \u2261\u27e8 apply-diff (v (apply ds s)) (v\u2032 (apply ds s)) \u27e9\n v\u2032 (apply ds s)\n \u2261\u27e8 sym (apply-diff (v s) (v\u2032 (apply ds s))) \u27e9\n apply ((diff v\u2032 v) s ds) (v s)\n \u220e) where open \u2261-Reasoning\n\nderive-is-valid : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} v (derive v)\nderive-is-valid v rewrite sym (diff-derive v) = diff-is-valid v v\n\n-- This is a postulate elsewhere, but here I provide a proper proof.\n\ndiff-apply-proof : \u2200 {\u03c4} (dv : \u27e6 \u0394-Type \u03c4 \u27e7) (v : \u27e6 \u03c4 \u27e7) \u2192\n (valid-\u0394 v dv) \u2192 diff (apply dv v) v \u2261 dv\n\ndiff-apply-proof {\u03c4\u2081 \u21d2 \u03c4\u2082} df f df-valid = ext (\u03bb v \u2192 ext (\u03bb dv \u2192\n begin\n diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (sym (proj\u2082 (df-valid (apply dv v) (derive (apply dv v))))) \u27e9\n diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff (apply df f x) (f v))\n (apply-derive (apply dv v)) \u27e9\n diff ((apply df f) (apply dv v)) (f v)\n \u2261\u27e8 \u2261-cong\n (\u03bb x \u2192 diff x (f v))\n (proj\u2082 (df-valid v dv)) \u27e9\n diff (apply (df v dv) (f v)) (f v)\n \u2261\u27e8 diff-apply-proof {\u03c4\u2082} (df v dv) (f v) (proj\u2081 (df-valid v dv)) \u27e9\n df v dv\n \u220e)) where open \u2261-Reasoning\n\ndiff-apply-proof {bool} db b _ = xor-cancellative b db\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9ecbe1387ef1b739fd49c32e7766fae28250aa45","subject":"Added new versions of J to the note about the identity type.","message":"Added new versions of J to the note about the identity type.\n\nIgnore-this: 762053066cff60c961b3d681934fc31d\n\ndarcs-hash:20111001144638-3bd4e-a232ee2ee9691326ad224dcdc46d20ec88ff6132.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/IdentityType.agda","new_file":"Draft\/IdentityType.agda","new_contents":"------------------------------------------------------------------------------\n-- The identity type\n------------------------------------------------------------------------------\n\n-- We can prove the properties of equality used in the formalization\n-- of FOTC, from refl and J.\n\nmodule IdentityType where\n\ninfix 7 _\u2261_\n\npostulate\n D : Set\n _\u2261_ : D \u2192 D \u2192 Set\n refl : \u2200 {x} \u2192 x \u2261 x\n\nmodule TypeTheory where\n\n -- Using the type-theoretic eliminator for equality.\n\n postulate\n J : (C : \u2200 x y \u2192 x \u2261 y \u2192 Set) \u2192\n (\u2200 x \u2192 (C x x refl)) \u2192\n \u2200 x y \u2192 (c : x \u2261 y) \u2192 C x y c\n\n -- From Thorsten's slides: A short history of equality.\n sym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\n sym {x} {y} = J (\u03bb x' y' _ \u2192 y' \u2261 x') (\u03bb x' \u2192 refl) x y\n\n -- From Thorsten's slides: A short history of equality.\n trans : \u2200 {x y z} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n trans {x} {y} {z} = J (\u03bb x' y' _ \u2192 y' \u2261 z \u2192 x' \u2261 z) (\u03bb x' pr \u2192 pr) x y\n\n subst : \u2200 {x} {y} \u2192 (P : D \u2192 Set) \u2192 x \u2261 y \u2192 P x \u2192 P y\n subst {x} {y} P x\u2261y = J (\u03bb x' y' _ \u2192 P x' \u2192 P y') (\u03bb x' pr \u2192 pr) x y x\u2261y\n\nmodule FOL where\n\n -- Using the usual elimination schema for predicate logic.\n\n postulate\n J : (C : D \u2192 Set) \u2192 \u2200 x y \u2192 x \u2261 y \u2192 C x \u2192 C y\n\n sym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\n sym {x} {y} x\u2261y = J (\u03bb y' \u2192 y' \u2261 x) x y x\u2261y refl\n\n trans : \u2200 {x y z} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n trans {x} {y} {z} x\u2261y = J (\u03bb y' \u2192 y' \u2261 z \u2192 x \u2261 z) x y x\u2261y (\u03bb pr \u2192 pr)\n\n subst : \u2200 {x} {y} \u2192 (P : D \u2192 Set) \u2192 x \u2261 y \u2192 P x \u2192 P y\n subst {x} {y} P x\u2261y Px = J P x y x\u2261y Px\n\nmodule ML where\n\n -- Using Martin-L\u00f6f elimination (\"Hauptsatz ...\", 1971).\n\n postulate\n J : (C : D \u2192 D \u2192 Set) \u2192\n (\u2200 x \u2192 (C x x)) \u2192\n \u2200 x y \u2192 x \u2261 y \u2192 C x y\n\n sym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\n sym {x} {y} = J (\u03bb x' y' \u2192 y' \u2261 x') (\u03bb x' \u2192 refl) x y\n\n trans : \u2200 {x y z} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\n trans {x} {y} {z} = J (\u03bb x' y' \u2192 y' \u2261 z \u2192 x' \u2261 z) (\u03bb x' pr \u2192 pr) x y\n\n subst : \u2200 {x} {y} \u2192 (P : D \u2192 Set) \u2192 x \u2261 y \u2192 P x \u2192 P y\n subst {x} {y} P x\u2261y = J (\u03bb x' y' \u2192 P x' \u2192 P y') (\u03bb x' pr \u2192 pr) x y x\u2261y\n","old_contents":"------------------------------------------------------------------------------\n-- The identity type\n------------------------------------------------------------------------------\n\n-- We can prove the properties of equality used in the formalization\n-- of FOTC, from refl and J.\n\nmodule IdentityType where\n\ninfix 7 _\u2261_\n\npostulate\n D : Set\n _\u2261_ : D \u2192 D \u2192 Set\n refl : \u2200 {x} \u2192 x \u2261 x\n J : (C : \u2200 x y \u2192 x \u2261 y \u2192 Set) \u2192\n (\u2200 x \u2192 (C x x refl)) \u2192\n \u2200 x y \u2192 (c : x \u2261 y) \u2192 C x y c\n\n-- From Thorsten's slides: A short history of equality.\nsym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\nsym {x} {y} = J (\u03bb x' y' _ \u2192 y' \u2261 x') (\u03bb x' \u2192 refl) x y\n\n-- From Thorsten's slides: A short history of equality.\ntrans : \u2200 {x y z} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\ntrans {x} {y} {z} = J (\u03bb x' y' _ \u2192 y' \u2261 z \u2192 x' \u2261 z) (\u03bb x' pr \u2192 pr) x y\n\nsubst : \u2200 {x} {y} \u2192 (P : D \u2192 Set) \u2192 x \u2261 y \u2192 P x \u2192 P y\nsubst {x} {y} P x\u2261y = J (\u03bb x' y' _ \u2192 P x' \u2192 P y') (\u03bb x' pr \u2192 pr) x y x\u2261y\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"8428371dc1838756182848c20525884faef5dcf9","subject":"Levitate","message":"Levitate\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/DarkwingDuck\/Primitive.agda","new_file":"formalization\/agda\/Spire\/DarkwingDuck\/Primitive.agda","new_contents":"{-# OPTIONS --type-in-type --no-positivity-check #-}\nmodule Spire.DarkwingDuck.Primitive where\n\n----------------------------------------------------------------------\n\ninfixr 4 _,_\ninfixr 5 _\u2237_\n\n----------------------------------------------------------------------\n\npostulate String : Set\n{-# BUILTIN STRING String #-}\n\n----------------------------------------------------------------------\n\ndata \u22a5 : Set where\n\nelimBot : (P : \u22a5 \u2192 Set)\n (v : \u22a5) \u2192 P v\nelimBot P ()\n\n----------------------------------------------------------------------\n\ndata \u22a4 : Set where\n tt : \u22a4\n\nelimUnit : (P : \u22a4 \u2192 Set)\n (ptt : P tt)\n (u : \u22a4) \u2192 P u\nelimUnit P ptt tt = ptt\n\n----------------------------------------------------------------------\n\ndata \u03a3 (A : Set) (B : A \u2192 Set) : Set where\n _,_ : (a : A) (b : B a) \u2192 \u03a3 A B\n\nelimPair : {A : Set} {B : A \u2192 Set}\n (P : \u03a3 A B \u2192 Set)\n (ppair : (a : A) (b : B a) \u2192 P (a , b))\n (ab : \u03a3 A B) \u2192 P ab\nelimPair P ppair (a , b) = ppair a b\n\n----------------------------------------------------------------------\n\ndata _\u2261_ {A : Set} (x : A) : A \u2192 Set where\n refl : x \u2261 x\n\nelimEq : {A : Set} {x : A} (P : (y : A) \u2192 x \u2261 y \u2192 Set)\n (prefl : P x refl)\n (y : A) (q : x \u2261 y) \u2192 P y q\nelimEq P prefl x refl = prefl\n\n----------------------------------------------------------------------\n\ndata List (A : Set) : Set where\n [] : List A\n _\u2237_ : (x : A) (xs : List A) \u2192 List A\n\nelimList : {A : Set} (P : List A \u2192 Set)\n (pnil : P [])\n (pcons : (x : A) (xs : List A) \u2192 P xs \u2192 P (x \u2237 xs))\n (xs : List A) \u2192 P xs\nelimList P pnil pcons [] = pnil\nelimList P pnil pcons (x \u2237 xs) = pcons x xs (elimList P pnil pcons xs)\n\n----------------------------------------------------------------------\n\ndata Elem {A : Set} : List A \u2192 Set where\n here : \u2200{x xs} \u2192 Elem (x \u2237 xs)\n there : \u2200{x xs} \u2192 Elem xs \u2192 Elem (x \u2237 xs)\n\nelimElem : {A : Set} (P : (xs : List A) \u2192 Elem xs \u2192 Set)\n (phere : (x : A) (xs : List A) \u2192 P (x \u2237 xs) here)\n (pthere : (x : A) (xs : List A) (t : Elem xs) \u2192 P xs t \u2192 P (x \u2237 xs) (there t))\n (xs : List A) (t : Elem xs) \u2192 P xs t\nelimElem P phere pthere (x \u2237 xs) here = phere x xs\nelimElem P phere pthere (x \u2237 xs) (there t) = pthere x xs t (elimElem P phere pthere xs t)\n\n----------------------------------------------------------------------\n\ndata Tel : Set\u2081 where\n Emp : Tel\n Ext : (A : Set) (B : A \u2192 Tel) \u2192 Tel\n\nelimTel : (P : Tel \u2192 Set)\n (pemp : P Emp)\n (pext : (A : Set) (B : A \u2192 Tel) (pb : (a : A) \u2192 P (B a)) \u2192 P (Ext A B))\n (T : Tel) \u2192 P T\nelimTel P pemp pext Emp = pemp\nelimTel P pemp pext (Ext A B) = pext A B (\u03bb a \u2192 elimTel P pemp pext (B a))\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n End : (i : I) \u2192 Desc I\n Rec : (i : I) (D : Desc I) \u2192 Desc I\n Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n\nelimDesc : {I : Set} (P : Desc I \u2192 Set)\n (pend : (i : I) \u2192 P (End i))\n (prec : (i : I) (D : Desc I) (pd : P D) \u2192 P (Rec i D))\n (parg : (A : Set) (B : A \u2192 Desc I) (pb : (a : A) \u2192 P (B a)) \u2192 P (Arg A B))\n (D : Desc I) \u2192 P D\nelimDesc P pend prec parg (End i) = pend i\nelimDesc P pend prec parg (Rec i D) = prec i D (elimDesc P pend prec parg D)\nelimDesc P pend prec parg (Arg A B) = parg A B (\u03bb a \u2192 elimDesc P pend prec parg (B a))\n\nFunc : (I : Set) (D : Desc I) \u2192 (I \u2192 Set) \u2192 I \u2192 Set\nFunc I = elimDesc\n (\u03bb D \u2192 (I \u2192 Set) \u2192 I \u2192 Set)\n (\u03bb j X i \u2192 j \u2261 i)\n (\u03bb j D ih X i \u2192 \u03a3 (X j) (\u03bb _ \u2192 ih X i))\n (\u03bb A B ih X i \u2192 \u03a3 A (\u03bb a \u2192 ih a X i))\n\nHyps : (I : Set) (D : Desc I) (X : I \u2192 Set) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : Func I D X i) \u2192 Set\nHyps I = elimDesc\n (\u03bb D \u2192 (X : I \u2192 Set) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : Func I D X i) \u2192 Set)\n (\u03bb j X P i q \u2192 \u22a4)\n (\u03bb j D ih X P i x,xs \u2192 elimPair (\u03bb ab \u2192 Set) (\u03bb x xs \u2192 \u03a3 (P j x) (\u03bb _ \u2192 ih X P i xs)) x,xs)\n (\u03bb A B ih X P i a,xs \u2192 elimPair (\u03bb ab \u2192 Set) (\u03bb a xs \u2192 ih a X P i xs) a,xs)\n\n----------------------------------------------------------------------\n\ndata \u03bc (\u2113 : String) (P : Set) (I : P \u2192 Set) (p : P) (D : Desc (I p)) (i : I p) : Set where\n init : Func (I p) D (\u03bc \u2113 P I p D) i \u2192 \u03bc \u2113 P I p D i\n\nall : {I : Set} (D : Desc I) (X : I \u2192 Set) (P : (i : I) \u2192 X i \u2192 Set)\n (p : (i : I) (x : X i) \u2192 P i x) (i : I) (xs : Func I D X i)\n \u2192 Hyps I D X P i xs\nall (End j) X P p i q = tt\nall (Rec j D) X P p i (x , xs) = p j x , all D X P p i xs\nall (Arg A B) X P p i (a , xs) = all (B a) X P p i xs\n\n{-# NO_TERMINATION_CHECK #-}\nind : (\u2113 : String) (P : Set) (I : P \u2192 Set) (p : P) (D : Desc (I p))\n (M : (i : I p) \u2192 \u03bc \u2113 P I p D i \u2192 Set)\n (\u03b1 : \u2200 i (xs : Func (I p) D (\u03bc \u2113 P I p D) i) (ihs : Hyps (I p) D (\u03bc \u2113 P I p D) M i xs) \u2192 M i (init xs))\n (i : I p)\n (x : \u03bc \u2113 P I p D i)\n \u2192 M i x\nind \u2113 P I p D M \u03b1 i (init xs) = \u03b1 i xs (all D (\u03bc \u2113 P I p D) M (ind \u2113 P I p D M \u03b1) i xs)\n\n----------------------------------------------------------------------\n","old_contents":"module Spire.DarkwingDuck.Primitive where\n\n----------------------------------------------------------------------\n\ninfixr 4 _,_\ninfixr 5 _\u2237_\n\n----------------------------------------------------------------------\n\npostulate String : Set\n{-# BUILTIN STRING String #-}\n\n----------------------------------------------------------------------\n\ndata \u22a5 : Set where\n\nelimBot : (P : \u22a5 \u2192 Set)\n (v : \u22a5) \u2192 P v\nelimBot P ()\n\n----------------------------------------------------------------------\n\ndata \u22a4 : Set where\n tt : \u22a4\n\nelimUnit : (P : \u22a4 \u2192 Set)\n (ptt : P tt)\n (u : \u22a4) \u2192 P u\nelimUnit P ptt tt = ptt\n\n----------------------------------------------------------------------\n\ndata \u03a3 (A : Set) (B : A \u2192 Set) : Set where\n _,_ : (a : A) (b : B a) \u2192 \u03a3 A B\n\nelimPair : {A : Set} {B : A \u2192 Set}\n (P : \u03a3 A B \u2192 Set)\n (ppair : (a : A) (b : B a) \u2192 P (a , b))\n (ab : \u03a3 A B) \u2192 P ab\nelimPair P ppair (a , b) = ppair a b\n\n----------------------------------------------------------------------\n\ndata _\u2261_ {A : Set} (x : A) : A \u2192 Set where\n refl : x \u2261 x\n\nelimEq : {A : Set} {x : A} (P : (y : A) \u2192 x \u2261 y \u2192 Set)\n (prefl : P x refl)\n (y : A) (q : x \u2261 y) \u2192 P y q\nelimEq P prefl x refl = prefl\n\n----------------------------------------------------------------------\n\ndata List (A : Set) : Set where\n [] : List A\n _\u2237_ : (x : A) (xs : List A) \u2192 List A\n\nelimList : {A : Set} (P : List A \u2192 Set)\n (pnil : P [])\n (pcons : (x : A) (xs : List A) \u2192 P xs \u2192 P (x \u2237 xs))\n (xs : List A) \u2192 P xs\nelimList P pnil pcons [] = pnil\nelimList P pnil pcons (x \u2237 xs) = pcons x xs (elimList P pnil pcons xs)\n\n----------------------------------------------------------------------\n\ndata Elem {A : Set} : List A \u2192 Set where\n here : \u2200{x xs} \u2192 Elem (x \u2237 xs)\n there : \u2200{x xs} \u2192 Elem xs \u2192 Elem (x \u2237 xs)\n\nelimElem : {A : Set} (P : (xs : List A) \u2192 Elem xs \u2192 Set)\n (phere : (x : A) (xs : List A) \u2192 P (x \u2237 xs) here)\n (pthere : (x : A) (xs : List A) (t : Elem xs) \u2192 P xs t \u2192 P (x \u2237 xs) (there t))\n (xs : List A) (t : Elem xs) \u2192 P xs t\nelimElem P phere pthere (x \u2237 xs) here = phere x xs\nelimElem P phere pthere (x \u2237 xs) (there t) = pthere x xs t (elimElem P phere pthere xs t)\n\n----------------------------------------------------------------------\n\ndata Tel : Set\u2081 where\n End : Tel\n Arg : (A : Set) (B : A \u2192 Tel) \u2192 Tel\n\nelimTel : (P : Tel \u2192 Set)\n (pend : P End)\n (parg : (A : Set) (B : A \u2192 Tel) (pb : (a : A) \u2192 P (B a)) \u2192 P (Arg A B))\n (T : Tel) \u2192 P T\nelimTel P pend parg End = pend\nelimTel P pend parg (Arg A B) = parg A B (\u03bb a \u2192 elimTel P pend parg (B a))\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n End : (i : I) \u2192 Desc I\n Rec : (i : I) (D : Desc I) \u2192 Desc I\n Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n\nelimDesc : {I : Set} (P : Desc I \u2192 Set)\n (pend : (i : I) \u2192 P (End i))\n (prec : (i : I) (D : Desc I) (pd : P D) \u2192 P (Rec i D))\n (parg : (A : Set) (B : A \u2192 Desc I) (pb : (a : A) \u2192 P (B a)) \u2192 P (Arg A B))\n (D : Desc I) \u2192 P D\nelimDesc P pend prec parg (End i) = pend i\nelimDesc P pend prec parg (Rec i D) = prec i D (elimDesc P pend prec parg D)\nelimDesc P pend prec parg (Arg A B) = parg A B (\u03bb a \u2192 elimDesc P pend prec parg (B a))\n\nEl\u1d30 : {I : Set} (D : Desc I) \u2192 (I \u2192 Set) \u2192 I \u2192 Set\nEl\u1d30 (End j) X i = j \u2261 i\nEl\u1d30 (Rec j D) X i = \u03a3 (X j) (\u03bb _ \u2192 El\u1d30 D X i)\nEl\u1d30 (Arg A B) X i = \u03a3 A (\u03bb a \u2192 El\u1d30 (B a) X i)\n\nHyps : {I : Set} (D : Desc I) (X : I \u2192 Set) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El\u1d30 D X i) \u2192 Set\nHyps (End j) X P i q = \u22a4\nHyps (Rec j D) X P i (x , xs) = \u03a3 (P j x) (\u03bb _ \u2192 Hyps D X P i xs)\nHyps (Arg A B) X P i (a , xs) = Hyps (B a) X P i xs\n\n----------------------------------------------------------------------\n\ndata \u03bc (\u2113 : String) (P : Set) (I : P \u2192 Set) (p : P) (D : Desc (I p)) (i : I p) : Set where\n init : El\u1d30 D (\u03bc \u2113 P I p D) i \u2192 \u03bc \u2113 P I p D i\n\nmap : {I : Set} (D : Desc I) (X : I \u2192 Set) (P : (i : I) \u2192 X i \u2192 Set)\n (p : (i : I) (x : X i) \u2192 P i x) (i : I) (xs : El\u1d30 D X i)\n \u2192 Hyps D X P i xs\nmap (End j) X P p i q = tt\nmap (Rec j D) X P p i (x , xs) = p j x , map D X P p i xs\nmap (Arg A B) X P p i (a , xs) = map (B a) X P p i xs\n\n{-# NO_TERMINATION_CHECK #-}\nind : (\u2113 : String) (P : Set) (I : P \u2192 Set) (p : P) (D : Desc (I p))\n (M : (i : I p) \u2192 \u03bc \u2113 P I p D i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 D (\u03bc \u2113 P I p D) i) (ihs : Hyps D (\u03bc \u2113 P I p D) M i xs) \u2192 M i (init xs))\n (i : I p)\n (x : \u03bc \u2113 P I p D i)\n \u2192 M i x\nind \u2113 P I p D M \u03b1 i (init xs) = \u03b1 i xs (map D (\u03bc \u2113 P I p D) M (ind \u2113 P I p D M \u03b1) i xs)\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"52889b467ae1b2d3302590984af3fa49681bec28","subject":"elgamal: fixes","message":"elgamal: fixes\n","repos":"crypto-agda\/crypto-agda","old_file":"elgamal.agda","new_file":"elgamal.agda","new_contents":"{-# OPTIONS --copatterns #-}\nopen import Type\nopen import Function\nopen import Data.Product\nopen import Data.Bool.NP as Bool\nopen import Data.Unit\nopen import Data.Maybe.NP\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc) renaming (#_ to ##_)\nopen import Data.Nat.NP hiding (_^_)\nopen import Data.Bits\nimport Data.Vec.NP as Vec\nopen Vec using (Vec; []; _\u2237_; _\u2237\u02b3_; allFin; lookup) renaming (map to vmap)\nopen import Algebra.FunctionProperties\nopen import Relation.Binary.PropositionalEquality as \u2261\nimport cont as cont\nopen cont using (Cont; ContA)\nopen import sum\nopen import sum-properties\n\nmodule elgamal where\n\ndata `\u2605 : \u2605 where\n `\u22a4 : `\u2605\n `X : `\u2605\n _`\u00d7_ : `\u2605 \u2192 `\u2605 \u2192 `\u2605\ninfixr 2 _`\u00d7_\n\nmodule Univ (X : \u2605) where\n El : `\u2605 \u2192 \u2605\n El `\u22a4 = \u22a4\n El `X = X\n El (u\u2080 `\u00d7 u\u2081) = El u\u2080 \u00d7 El u\u2081\n\n record \u21ba (R : `\u2605) (A : \u2605) : \u2605 where\n constructor mk\n field\n run\u21ba : El R \u2192 A\n open \u21ba public\n\n EXP : (R : `\u2605) \u2192 \u2605\n EXP R = \u21ba R Bit\n\n Det : \u2605 \u2192 \u2605\n Det = \u21ba `\u22a4\n\n \u03bcU : SumProp X \u2192 \u2200 u \u2192 SumProp (El u)\n \u03bcU \u03bcX `\u22a4 = \u03bc\u22a4\n \u03bcU \u03bcX `X = \u03bcX\n \u03bcU \u03bcX (u\u2080 `\u00d7 u\u2081) = \u03bcU \u03bcX u\u2080 \u00d7\u03bc \u03bcU \u03bcX u\u2081\n\nmodule EntropySmoothing\n (M : \u2605) -- Message\n (Hash : \u2605)\n (\u210b : M \u2192 Hash) -- Hashing function\n (R\u2090 : \u2605) -- Adversary randomness\n where\n\n -- Entropy smoothing adversary\n ESAdv : \u2605\n ESAdv = R\u2090 \u2192 Hash \u2192 Bit\n\n -- The randomness universe needed for the following games\n R : \u2605\n R = M \u00d7 Hash \u00d7 R\u2090\n\n -- In this game we always use \u210b on a random message\n ES\u2141\u2080 : ESAdv \u2192 R \u2192 Bit\n ES\u2141\u2080 A (m , _ , r\u2090) = A r\u2090 (\u210b m)\n\n -- In this game we just retrun a random Hash value\n ES\u2141\u2081 : ESAdv \u2192 R \u2192 Bit\n ES\u2141\u2081 A (_ , h , r\u2090) = A r\u2090 h\n\n ES\u2141 : ESAdv \u2192 Bit \u2192 R \u2192 Bit\n ES\u2141 A b r = (case b 0\u2192 ES\u2141\u2080 1\u2192 ES\u2141\u2081) A r\n\nmodule EntropySmoothingWithKey\n (M : \u2605)\n (Key : \u2605)\n (Hash : \u2605)\n (\u210b : Key \u2192 M \u2192 Hash) -- Hashing function\n (R\u2090 : \u2605) -- Adversary randomness\n where\n\n -- Entropy smoothing adversary\n ESAdv : \u2605\n ESAdv = R\u2090 \u2192 Key \u2192 Hash \u2192 Bit\n\n -- The randomness universe needed for the following games\n R : \u2605\n R = Key \u00d7 M \u00d7 Hash \u00d7 R\u2090\n\n -- In this game we always use \u210b on a random message\n ES\u2141\u2080 : ESAdv \u2192 R \u2192 Bit\n ES\u2141\u2080 A (k , m , _ , r\u2090) = A r\u2090 k (\u210b k m)\n\n -- In this game we just retrun a random Hash value\n ES\u2141\u2081 : ESAdv \u2192 R \u2192 Bit\n ES\u2141\u2081 A (k , _ , h , r\u2090) = A r\u2090 k h\n\n ES\u2141 : ESAdv \u2192 Bit \u2192 R \u2192 Bit\n ES\u2141 A b r = (case b 0\u2192 ES\u2141\u2080 1\u2192 ES\u2141\u2081) A r\n\nmodule \u2124q-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u03bc\u2124q : SumProp \u2124q)\n (sum\u2124q-\u229e-lem : \u2200 x f \u2192 sum \u03bc\u2124q (f \u2218 _\u229e_ x) \u2261 sum \u03bc\u2124q f)\n where\n\n -- open Sum\n open Univ \u2124q public\n open `\u2605 public renaming (`X to `\u2124q)\n\n #_ : \u2200 {u} \u2192 \u21ba u Bit \u2192 \u2115\n #_ {u} f = sum (\u03bcU \u03bc\u2124q u) (Bool.to\u2115 \u2218 run\u21ba f)\n\n #q_ : Count \u2124q\n #q_ = count \u03bc\u2124q\n\n \u2047 : \u2200 R \u2192 \u21ba R (El R)\n run\u21ba (\u2047 _) = id\n\n pure\u21ba : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n run\u21ba (pure\u21ba x) r = x -- turning r to _ produce an error\n\n \u27ea_\u27eb : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n \u27ea_\u27eb = pure\u21ba\n\n {-\n \u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n \u27ea_\u27eb\u1d30 = pure\u1d30\n -}\n\n map\u21ba : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n run\u21ba (map\u21ba f x) r = f (run\u21ba x r)\n\n infixl 4 _\u229b_\n _\u229b_ : \u2200 {R S A B} \u2192 \u21ba R (A \u2192 B) \u2192 \u21ba S A \u2192 \u21ba (R `\u00d7 S) B\n run\u21ba (af \u229b ax) rs = run\u21ba af (proj\u2081 rs) (run\u21ba ax (proj\u2082 rs))\n\n \u27ea_\u00b7_\u27eb : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n \u27ea f \u00b7 x \u27eb = map\u21ba f x\n\n \u27ea_\u00b7_\u00b7_\u27eb : \u2200 {A B C} {R S} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba R A \u2192 \u21ba S B \u2192 \u21ba (R `\u00d7 S) C\n \u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n _\u27e8\u229e\u27e9_ : \u2200 {R S} \u2192 \u21ba R \u2124q \u2192 \u21ba S \u2124q \u2192 \u21ba (R `\u00d7 S) \u2124q\n x \u27e8\u229e\u27e9 y = \u27ea _\u229e_ \u00b7 x \u00b7 y \u27eb\n\n \u27e8_\u229e\u27e9_ : \u2200 {R} \u2192 \u2124q \u2192 \u21ba R \u2124q \u2192 \u21ba R \u2124q\n \u27e8 x \u229e\u27e9 y = \u27ea _\u229e_ x \u00b7 y \u27eb\n\n infix 4 _\u2248\u21ba_ _\u2248\u1d2c_\n _\u2248\u21ba_ : \u2200 {R : `\u2605} (f g : EXP R) \u2192 \u2605\n _\u2248\u21ba_ = _\u2261_ on #_\n\n _\u2248\u1d2c_ : \u2200 {A R} (f g : \u21ba R A) \u2192 Set _\n _\u2248\u1d2c_ {A} f g = \u2200 (Adv : A \u2192 Bit) \u2192 \u27ea Adv \u00b7 f \u27eb \u2248\u21ba \u27ea Adv \u00b7 g \u27eb\n\n lem : \u2200 x \u2192 \u27e8 x \u229e\u27e9 (\u2047 `\u2124q) \u2248\u1d2c \u2047 _\n lem x Adv = sum\u2124q-\u229e-lem x (Bool.to\u2115 \u2218 Adv)\n\n -- \u2200 (A : \u2124q \u2192 Bit) \u2192 # (A \u2047)\n\nopen Fin.Modulo renaming (sucmod to [suc])\n\n{-\nmodule \u2124q-implem (q-2 : \u2115) where\n q : \u2115\n q = 2 + q-2\n\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = zero\n\n [1] : \u2124q\n [1] = suc zero\n-}\nmodule \u2124q-implem (q : \u2115) ([0]' [1]' : Fin q) where\n -- open Sum\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = [0]'\n\n [1] : \u2124q\n [1] = [1]'\n\n _\u2115\u229e_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = m \u2115\u229e ([suc] n)\n {-\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = [suc] (m \u2115\u229e n)\n -}\n\n \u2115\u229e-inj : \u2200 n {x y} \u2192 n \u2115\u229e x \u2261 n \u2115\u229e y \u2192 x \u2261 y\n \u2115\u229e-inj zero eq = eq\n \u2115\u229e-inj (suc n) eq = sucmod-inj _ _ (\u2115\u229e-inj n eq)\n\n _\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u229e n = Fin.to\u2115 m \u2115\u229e n\n\n \u229e-inj : \u2200 m {x y} \u2192 m \u229e x \u2261 m \u229e y \u2192 x \u2261 y\n \u229e-inj m = \u2115\u229e-inj (Fin.to\u2115 m)\n\n _\u2115\u22a0_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u22a0 n = [0]\n suc m \u2115\u22a0 n = n \u229e (m \u2115\u22a0 n)\n\n _\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u22a0 n = Fin.to\u2115 m \u2115\u22a0 n\n\n _[^]\u2115_ : \u2124q \u2192 \u2115 \u2192 \u2124q\n m [^]\u2115 zero = [1]\n m [^]\u2115 suc n = m \u22a0 (m [^]\u2115 n)\n\n _[^]_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m [^] n = m [^]\u2115 (Fin.to\u2115 n)\n\n \u03bc\u2124q : SumProp \u2124q\n \u03bc\u2124q = \u03bcFin q\n\n all\u2124q : Vec \u2124q q\n all\u2124q = allFin q\n\n {-\n sum\u2124q : Sum \u2124q\n sum\u2124q f = Vec.sum (vmap f all\u2124q)\n -}\n\n sum\u2124q-[suc]-lem : \u2200 f \u2192 sum \u03bc\u2124q (f \u2218 [suc]) \u2261 sum \u03bc\u2124q f\n sum\u2124q-[suc]-lem f rewrite \u2261.sym (Vec.sum-map-rot\u2081 f all\u2124q)\n | Vec.map-\u2218 f [suc] all\u2124q\n | rot\u2081-map-sucmod q\n = refl\n\n -- comm-[suc]-\u2115\u229e : \u2200 m n \u2192 [suc] (m \u2115\u229e n) \u2261 m \u2115\u229e ([suc] n)\n\n sum\u2124q-\u2115\u229e-lem : \u2200 m f \u2192 sum \u03bc\u2124q (f \u2218 _\u2115\u229e_ m) \u2261 sum \u03bc\u2124q f\n sum\u2124q-\u2115\u229e-lem zero f = refl\n sum\u2124q-\u2115\u229e-lem (suc m) f rewrite sum\u2124q-[suc]-lem (f \u2218 _\u2115\u229e_ m)\n | sum\u2124q-\u2115\u229e-lem m f = refl\n\n sum\u2124q-\u229e-lem : \u2200 m f \u2192 sum \u03bc\u2124q (f \u2218 _\u229e_ m) \u2261 sum \u03bc\u2124q f\n sum\u2124q-\u229e-lem = sum\u2124q-\u2115\u229e-lem \u2218 Fin.to\u2115\n\n\nmodule G-implem (p q : \u2115) (g' : Fin p) (0[p] 1[p] : Fin p) (0[q] 1[q] : Fin q) where\n open \u2124q-implem q 0[q] 1[q] public\n open \u2124q-implem p 0[p] 1[p] public using () renaming (\u2124q to G; _\u22a0_ to _\u2219_; _[^]\u2115_ to _^[p]_)\n\n g : G\n g = g'\n\n _^_ : G \u2192 \u2124q \u2192 G\n x ^ n = x ^[p] Fin.to\u2115 n\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n {-\n g^-inj : \u2200 m n \u2192 g^ m \u2261 g^ n \u2192 m \u2261 n\n g^-inj = {!!}\n -}\n\nmodule G-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u03bc\u2124q : SumProp \u2124q)\n (sum\u2124q-\u229e-lem : \u2200 x f \u2192 sum \u03bc\u2124q (f \u2218 _\u229e_ x) \u2261 sum \u03bc\u2124q f)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n where\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n open \u2124q-count \u2124q _\u229e_ \u03bc\u2124q sum\u2124q-\u229e-lem\n\n \u2047G : \u21ba `\u2124q G\n run\u21ba \u2047G x = g^ x\n\n #G : Count G\n #G f = #q (f \u2218 g^_)\n\n {-\n #G-\u2219 : \u2200 f m \u2192 #G (f \u2218 _\u2219_ m) \u2261 #G f\n #G-\u2219 f m = {!!}\n -}\n\nmodule DDH\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g^_ : \u2124q \u2192 G)\n where\n DDHAdv : \u2605 \u2192 \u2605\n DDHAdv R = R \u2192 G \u2192 G \u2192 G \u2192 Bit\n\n DDH\u2141\u2080 : \u2200 {R} {_I : \u2605} \u2192 DDHAdv R \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n DDH\u2141\u2080 d (r , x , y , _) = d r (g^ x) (g^ y) (g^ (x \u22a0 y))\n\n DDH\u2141\u2081 : \u2200 {R} \u2192 DDHAdv R \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n DDH\u2141\u2081 d (r , x , y , z) = d r (g^ x) (g^ y) (g^ z)\n\n DDH\u2141 : \u2200 {R} \u2192 DDHAdv R \u2192 Bit \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n DDH\u2141 d b = (case b 0\u2192 DDH\u2141\u2080 1\u2192 DDH\u2141\u2081) d\n\n -- DDH\u2141\u2032 : \u2200 {R} \u2192 DDHAdv R \u2192 (Bit \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q \u00d7 R) \u2192 Bit\n -- DDH\u2141\u2032 d (b , x , y , z , r) = DDH\u2141 d b (x , y , z , r)\n\n module With\u21ba where\n open Univ \u2124q\n open `\u2605 public renaming (`X to `\u2124q)\n DDHAdv\u21ba : `\u2605 \u2192 \u2605\n DDHAdv\u21ba R = G \u2192 G \u2192 G \u2192 \u21ba R Bit\n DDH\u2141\u2080\u21ba : \u2200 {R _I} \u2192 DDHAdv\u21ba R \u2192 \u21ba (R `\u00d7 `\u2124q `\u00d7 `\u2124q `\u00d7 _I) Bit\n run\u21ba (DDH\u2141\u2080\u21ba d) = DDH\u2141\u2080 (\u03bb x y z t \u2192 run\u21ba (d y z t) x)\n\nmodule El-Gamal-Generic\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (Message : \u2605)\n (_\u2219_ : G \u2192 Message \u2192 Message)\n\n -- Required for decryption\n (_\/_ : Message \u2192 G \u2192 Message)\n\n -- Required for the correctness proof\n (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n -- Required for the security proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : SumProp \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : SumProp R\u2090)\n where\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n -- g\u02e3 is the pk\n -- x is the sk\n\n PubKey = G\n SecKey = \u2124q\n KeyPair = PubKey \u00d7 SecKey\n CipherText = G \u00d7 Message\n\n M = Message\n C = CipherText\n\n KeyGen : \u2124q \u2192 KeyPair\n KeyGen x = (g^ x , x)\n\n -- KeyGen\u21ba : \u21ba \u2124q KeyPair\n -- KeyGen\u21ba = mk KeyGen\n\n Enc : PubKey \u2192 Message \u2192 \u2124q \u2192 CipherText\n Enc g\u02e3 m y = g\u02b8 , \u03b6 where\n g\u02b8 = g^ y\n \u03b4 = g\u02e3 ^ y\n \u03b6 = \u03b4 \u2219 m\n\n -- Enc\u21ba : PubKey \u2192 Message \u2192 \u21ba \u2124q CipherText\n -- Enc\u21ba g\u02e3 m = mk (Enc g\u02e3 m)\n\n Dec : SecKey \u2192 CipherText \u2192 Message\n Dec x (g\u02b8 , \u03b6) = \u03b6 \/ (g\u02b8 ^ x)\n\n EncAdv : \u2605 \u2192 \u2605\n EncAdv R\u2090 = (R\u2090 \u2192 PubKey \u2192 Bit \u2192 M)\n \u00d7 (R\u2090 \u2192 PubKey \u2192 C \u2192 Bit)\n\n SS\u2141 : \u2200 {R\u2090 _I : \u2605} \u2192 EncAdv R\u2090 \u2192 Bit \u2192 (R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n SS\u2141 (m , d) b (r\u2090 , x , y , z) =\n let pk = proj\u2081 (KeyGen x) in\n d r\u2090 pk (Enc pk (m r\u2090 pk b) y)\n\n -- Unused\n Game : (i : Bit) \u2192 \u2200 {R\u2090} \u2192 EncAdv R\u2090 \u2192 (Bit \u00d7 R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n Game i (m , d) (b , r\u2090 , x , y , z) = b ==\u1d47 d r\u2090 g\u02e3 (g\u02b8 , \u03b6)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n \u03b4 = g\u02e3 ^ case i 0\u2192 y 1\u2192 z\n \u03b6 = \u03b4 \u2219 m r\u2090 g\u02e3 b\n\n {-\n Game-0b\u2261Game0 : \u2200 {R\u2090} \u2192 Game 0b \u2261 Game0 {R\u2090}\n Game-0b\u2261Game0 = refl\n -}\n\n open DDH \u2124q _\u22a0_ G g^_ public\n\n OTP\u2141 : \u2200 {R : \u2605} \u2192 (R \u2192 G \u2192 Message) \u2192 (R \u2192 G \u2192 G \u2192 Message \u2192 Bit)\n \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n OTP\u2141 M d (r , x , y , z) = d r g\u02e3 g\u02b8 (g\u1dbb \u2219 M r g\u02e3)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n g\u1dbb = g^ z\n\n TrA : \u2200 {R\u2090} \u2192 Bit \u2192 EncAdv R\u2090 \u2192 DDHAdv R\u2090\n TrA b (m , d) r\u2090 g\u02e3 g\u02b8 g\u02e3\u02b8 = d r\u2090 g\u02e3 (g\u02b8 , g\u02e3\u02b8 \u2219 m r\u2090 g\u02e3 b)\n\n projM : \u2200 {R\u2090} \u2192 EncAdv R\u2090 \u2192 Bit \u2192 R\u2090 \u2192 G \u2192 Message\n projM (m , _) b r\u2090 g\u02e3 = m r\u2090 g\u02e3 b\n\n projD : \u2200 {R\u2090} \u2192 EncAdv R\u2090 \u2192 R\u2090 \u2192 G \u2192 G \u2192 Message \u2192 Bit\n projD (_ , d) r\u2090 g\u02e3 g\u02b8 g\u1dbb\u2219M = d r\u2090 g\u02e3 (g\u02b8 , g\u1dbb\u2219M)\n\n like-SS\u2141 : \u2200 {R\u2090 _I : \u2605} \u2192 EncAdv R\u2090 \u2192 Bit \u2192 (R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n like-SS\u2141 (m , d) b (r\u2090 , x , y , _z) =\n d r\u2090 g\u02e3 (g\u02b8 , (g\u02e3 ^ y) \u2219 m r\u2090 g\u02e3 b)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n\n SS\u2141\u2261like-SS\u2141 : \u2200 {R _I} \u2192 SS\u2141 {R} {_I} \u2261 like-SS\u2141\n SS\u2141\u2261like-SS\u2141 = refl\n\n -- open Sum\n\n R = R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q\n \u03bcR : SumProp R\n \u03bcR = \u03bcR\u2090 \u00d7\u03bc \u03bc\u2124q \u00d7\u03bc \u03bc\u2124q \u00d7\u03bc \u03bc\u2124q\n\n #R_ : Count R\n #R_ = count \u03bcR\n\n #q_ : Count \u2124q\n #q_ = count \u03bc\u2124q\n\n _\u2248q_ : (f g : \u2124q \u2192 Bit) \u2192 \u2605\n f \u2248q g = #q f \u2261 #q g\n\n _\u2248R_ : (f g : R \u2192 Bit) \u2192 \u2605\n f \u2248R g = #R f \u2261 #R g\n\n functional-correctness : \u2200 x y m \u2192 Dec x (Enc (g^ x) m y) \u2261 m\n functional-correctness x y m rewrite comm-^ g x y | \/-\u2219 (g^ y ^ x) m = refl\n\n module Proof\n (ddh-hyp : \u2200 A \u2192 DDH\u2141 A 0b \u2248R DDH\u2141 A 1b)\n (otp-lem : \u2200 A m\u2080 m\u2081 \u2192 (\u03bb x \u2192 A (g^ x \u2219 m\u2080)) \u2248q (\u03bb x \u2192 A (g^ x \u2219 m\u2081)))\n (A : EncAdv R\u2090) (b : Bit)\n where\n\n OTP\u2141-lem : \u2200 d M\u2080 M\u2081 \u2192 OTP\u2141 M\u2080 d \u2248R OTP\u2141 M\u2081 d\n OTP\u2141-lem d M\u2080 M\u2081 = sum-ext \u03bcR\u2090 (\u03bb r \u2192\n sum-ext \u03bc\u2124q (\u03bb x \u2192\n sum-ext \u03bc\u2124q (\u03bb y \u2192\n pf r x y)))\n where\n pf : \u2200 r x y \u2192 count \u03bc\u2124q (\u03bb z \u2192 OTP\u2141 M\u2080 d (r , x , y , z))\n \u2261 count \u03bc\u2124q (\u03bb z \u2192 OTP\u2141 M\u2081 d (r , x , y , z))\n pf r x y rewrite otp-lem (d r (g^ x) (g^ y)) (M\u2080 r (g^ x)) (M\u2081 r (g^ x)) = refl\n\n A\u1d47 = TrA b A\n A\u00ac\u1d47 = TrA (not b) A\n\n pf0,5 : SS\u2141 A b \u2257 DDH\u2141 A\u1d47 0b\n pf0,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n pf1 : SS\u2141 A b \u2248R DDH\u2141 A\u1d47 0b\n pf1 = sum-ext \u03bcR (cong Bool.to\u2115 \u2218 pf0,5)\n\n pf2 : DDH\u2141 A\u1d47 0b \u2248R DDH\u2141 A\u1d47 1b\n pf2 = ddh-hyp A\u1d47\n\n pf2,5 : DDH\u2141 A\u1d47 1b \u2261 OTP\u2141 (projM A b) (projD A)\n pf2,5 = refl\n\n pf3 : DDH\u2141 A\u1d47 1b \u2248R DDH\u2141 A\u00ac\u1d47 1b\n pf3 = OTP\u2141-lem (projD A) (projM A b) (projM A (not b))\n\n pf4 : DDH\u2141 A\u00ac\u1d47 1b \u2248R DDH\u2141 A\u00ac\u1d47 0b\n pf4 = \u2261.sym (ddh-hyp A\u00ac\u1d47)\n\n pf4,5 : SS\u2141 A (not b) \u2257 DDH\u2141 A\u00ac\u1d47 0b\n pf4,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n pf5 : SS\u2141 A (not b) \u2248R DDH\u2141 A\u00ac\u1d47 0b\n pf5 = sum-ext \u03bcR (cong Bool.to\u2115 \u2218 pf4,5)\n\n final : SS\u2141 A b \u2248R SS\u2141 A (not b)\n final rewrite pf1 | pf2 | pf3 | pf4 | pf5 = refl\n\nmodule El-Gamal-Base\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n\n -- Required for decryption\n (_\/_ : G \u2192 G \u2192 G)\n\n -- Required for the correctness proof\n (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n \n {-\n (_\u207b\u00b9 : G \u2192 G)\n (\u207b\u00b9-inverse : \u2200 x \u2192 x \u207b\u00b9 \u2219 x \u2261 1G)\n -}\n\n -- Required for the proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : SumProp \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : SumProp R\u2090)\n where\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ G _\u2219_\n _\/_ \/-\u2219 comm-^\n dist-^-\u22a0 \u03bc\u2124q R\u2090 \u03bcR\u2090 public\n\n module OTP\u2141-LEM\n (otp-lem1 : \u2200 (A : G \u2192 Bit) m \u2192 (\u03bb x \u2192 A (g^ x \u2219 m)) \u2248q (\u03bb x \u2192 A (g^ x)))\n where\n\n otp-lem : \u2200 (A : G \u2192 Bit) m\u2080 m\u2081 \u2192 (\u03bb x \u2192 A (g^ x \u2219 m\u2080)) \u2248q (\u03bb x \u2192 A (g^ x \u2219 m\u2081))\n otp-lem A m\u2080 m\u2081 rewrite otp-lem1 A m\u2080 | otp-lem1 A m\u2081 = refl\n\nmodule El-Gamal-Hashed\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n -- (HKey : \u2605)\n (|M| : \u2115)\n (\u210b : {-HKey \u2192-} G \u2192 Bits |M|)\n\n -- (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n -- Required for the proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : SumProp \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : SumProp R\u2090)\n where\n\n Message = Bits |M|\n\n \u210b\u27e8_\u27e9\u2295_ : G \u2192 Message \u2192 Message\n \u210b\u27e8 \u03b4 \u27e9\u2295 m = \u210b \u03b4 \u2295 m\n\n _\/_ : Message \u2192 G \u2192 Message\n _\/_ m \u03b4 = \u210b \u03b4 \u2295 m\n\n \n \/-\u2219 : \u2200 x y \u2192 \u210b\u27e8 x \u27e9\u2295 y \/ x \u2261 y\n \/-\u2219 x y = {!!}\n {-\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ Message \u210b\u27e8_\u27e9\u2295_ _\/_ {!!} {!!}\n dist-^-\u22a0 sum\u2124q sum\u2124q-ext R\u2090 sumR\u2090 sumR\u2090-ext public\n -}\n\n {-\n OTP\u2141-lem : \u2200 d M\u2080 M\u2081 \u2192 OTP\u2141 M\u2080 d \u2248R OTP\u2141 M\u2081 d\n OTP\u2141-lem = ?\n -}\n\nmodule \u27e8\u2124p\u27e9\u2605 p-3 {- p is prime -} (`R\u2090 : `\u2605) where\n p : \u2115\n p = 3 + p-3\n\n q : \u2115\n q = p \u2238 1\n\n module G = G-implem p q (## 2) (## 0) (## 1) (## 0) (## 1)\n open G\n\n postulate\n _\u207b\u00b9 : G \u2192 G\n _\/_ : G \u2192 G \u2192 G\n \/-\u2022 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y\n comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x\n dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y\n\n open \u2124q-count \u2124q _\u229e_ \u03bc\u2124q sum\u2124q-\u229e-lem\n\n \u03bcR\u2090 : SumProp (El `R\u2090)\n \u03bcR\u2090 = \u03bcU \u03bc\u2124q `R\u2090\n\n R\u2090 = El `R\u2090\n sumR\u2090 = sum \u03bcR\u2090\n sumR\u2090-ext = sum-ext \u03bcR\u2090\n\n module EB = El-Gamal-Base _ _\u22a0_ G g _^_ _\u2219_ _\/_ \/-\u2022 comm-^ dist-^-\u22a0 \u03bc\u2124q R\u2090 \u03bcR\u2090\n open EB hiding (g^_)\n\n otp-base-lem : \u2200 (A : G \u2192 Bit) m \u2192 (A \u2218 g^_) \u2248q (A \u2218 g^_ \u2218 _\u229e_ m)\n otp-base-lem A m = #-StableUnderInjection {\u03bc = \u03bcFin _} \u03bcFinSUI\n (A \u2218 g^_) (_\u229e_ m) (\u229e-inj m)\n -- oh hai! this is: sym (sum\u2124q-\u229e-lem m (Bool.to\u2115 \u2218 A \u2218 g^_)) -- kthxbye\n\n postulate\n ddh-hyp : (A : DDHAdv R\u2090) \u2192 DDH\u2141 A 0b \u2248R DDH\u2141 A 1b\n otp-lem : \u2200 (A : G \u2192 Bit) m \u2192 (\u03bb x \u2192 A (g^ x \u2219 m)) \u2248q (\u03bb x \u2192 A (g^ x))\n\n\n open OTP\u2141-LEM otp-lem\n\n {-\n final : \u2200 A \u2192 SS\u2141 A 0b \u2248R SS\u2141 A 1b\n final A = Proof.final ddh-hyp OTP\u2141-lem A 0b\n -}\n\nmodule \u27e8\u212411\u27e9\u2605 = \u27e8\u2124p\u27e9\u2605 (11 \u2238 3)\n `X -- the amount of adversary randomness\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","old_contents":"{-# OPTIONS --copatterns #-}\nopen import Type\nopen import Function\nopen import Data.Product\nopen import Data.Bool.NP as Bool\nopen import Data.Unit\nopen import Data.Maybe.NP\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc) renaming (#_ to ##_)\nopen import Data.Nat.NP hiding (_^_)\nopen import Data.Bits\nimport Data.Vec.NP as Vec\nopen Vec using (Vec; []; _\u2237_; _\u2237\u02b3_; allFin; lookup) renaming (map to vmap)\nopen import Algebra.FunctionProperties\nopen import Relation.Binary.PropositionalEquality as \u2261\nimport cont as cont\nopen cont using (Cont; ContA)\nopen import sum\nopen import sum-properties\n\nmodule elgamal where\n\ndata `\u2605 : \u2605 where\n `\u22a4 : `\u2605\n `X : `\u2605\n _`\u00d7_ : `\u2605 \u2192 `\u2605 \u2192 `\u2605\ninfixr 2 _`\u00d7_\n\nmodule Univ (X : \u2605) where\n El : `\u2605 \u2192 \u2605\n El `\u22a4 = \u22a4\n El `X = X\n El (u\u2080 `\u00d7 u\u2081) = El u\u2080 \u00d7 El u\u2081\n\n record \u21ba (R : `\u2605) (A : \u2605) : \u2605 where\n constructor mk\n field\n run\u21ba : El R \u2192 A\n open \u21ba public\n\n EXP : (R : `\u2605) \u2192 \u2605\n EXP R = \u21ba R Bit\n\n Det : \u2605 \u2192 \u2605\n Det = \u21ba `\u22a4\n\n \u03bcU : SumProp X \u2192 \u2200 u \u2192 SumProp (El u)\n \u03bcU \u03bcX `\u22a4 = \u03bc\u22a4\n \u03bcU \u03bcX `X = \u03bcX\n \u03bcU \u03bcX (u\u2080 `\u00d7 u\u2081) = \u03bcU \u03bcX u\u2080 \u00d7\u03bc \u03bcU \u03bcX u\u2081\n\nmodule EntropySmoothing\n (M : \u2605) -- Message\n (Hash : \u2605)\n (\u210b : M \u2192 Hash) -- Hashing function\n (R\u2090 : \u2605) -- Adversary randomness\n where\n\n -- Entropy smoothing adversary\n ESAdv : \u2605\n ESAdv = R\u2090 \u2192 Hash \u2192 Bit\n\n -- The randomness universe needed for the following games\n R : \u2605\n R = M \u00d7 Hash \u00d7 R\u2090\n\n -- In this game we always use \u210b on a random message\n ES\u2141\u2080 : ESAdv \u2192 R \u2192 Bit\n ES\u2141\u2080 A (m , _ , r\u2090) = A r\u2090 (\u210b m)\n\n -- In this game we just retrun a random Hash value\n ES\u2141\u2081 : ESAdv \u2192 R \u2192 Bit\n ES\u2141\u2081 A (_ , h , r\u2090) = A r\u2090 h\n\n ES\u2141 : ESAdv \u2192 Bit \u2192 R \u2192 Bit\n ES\u2141 A b r = (case b 0\u2192 ES\u2141\u2080 1\u2192 ES\u2141\u2081) A r\n\nmodule EntropySmoothingWithKey\n (M : \u2605)\n (Key : \u2605)\n (Hash : \u2605)\n (\u210b : Key \u2192 M \u2192 Hash) -- Hashing function\n (R\u2090 : \u2605) -- Adversary randomness\n where\n\n -- Entropy smoothing adversary\n ESAdv : \u2605\n ESAdv = R\u2090 \u2192 Key \u2192 Hash \u2192 Bit\n\n -- The randomness universe needed for the following games\n R : \u2605\n R = Key \u00d7 M \u00d7 Hash \u00d7 R\u2090\n\n -- In this game we always use \u210b on a random message\n ES\u2141\u2080 : ESAdv \u2192 R \u2192 Bit\n ES\u2141\u2080 A (k , m , _ , r\u2090) = A r\u2090 k (\u210b k m)\n\n -- In this game we just retrun a random Hash value\n ES\u2141\u2081 : ESAdv \u2192 R \u2192 Bit\n ES\u2141\u2081 A (k , _ , h , r\u2090) = A r\u2090 k h\n\n ES\u2141 : ESAdv \u2192 Bit \u2192 R \u2192 Bit\n ES\u2141 A b r = (case b 0\u2192 ES\u2141\u2080 1\u2192 ES\u2141\u2081) A r\n\nmodule \u2124q-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u03bc\u2124q : SumProp \u2124q)\n (sum\u2124q-\u229e-lem : \u2200 x f \u2192 sum \u03bc\u2124q (f \u2218 _\u229e_ x) \u2261 sum \u03bc\u2124q f)\n where\n\n -- open Sum\n open Univ \u2124q public\n open `\u2605 public renaming (`X to `\u2124q)\n\n #_ : \u2200 {u} \u2192 \u21ba u Bit \u2192 \u2115\n #_ {u} f = sum (\u03bcU \u03bc\u2124q u) (Bool.to\u2115 \u2218 run\u21ba f)\n\n #q_ : Count \u2124q\n #q_ = count \u03bc\u2124q\n\n \u2047 : \u2200 R \u2192 \u21ba R (El R)\n run\u21ba (\u2047 _) = id\n\n pure\u21ba : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n run\u21ba (pure\u21ba x) r = x -- turning r to _ produce an error\n\n \u27ea_\u27eb : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n \u27ea_\u27eb = pure\u21ba\n\n {-\n \u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n \u27ea_\u27eb\u1d30 = pure\u1d30\n -}\n\n map\u21ba : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n run\u21ba (map\u21ba f x) r = f (run\u21ba x r)\n\n infixl 4 _\u229b_\n _\u229b_ : \u2200 {R S A B} \u2192 \u21ba R (A \u2192 B) \u2192 \u21ba S A \u2192 \u21ba (R `\u00d7 S) B\n run\u21ba (af \u229b ax) rs = run\u21ba af (proj\u2081 rs) (run\u21ba ax (proj\u2082 rs))\n\n \u27ea_\u00b7_\u27eb : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n \u27ea f \u00b7 x \u27eb = map\u21ba f x\n\n \u27ea_\u00b7_\u00b7_\u27eb : \u2200 {A B C} {R S} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba R A \u2192 \u21ba S B \u2192 \u21ba (R `\u00d7 S) C\n \u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n _\u27e8\u229e\u27e9_ : \u2200 {R S} \u2192 \u21ba R \u2124q \u2192 \u21ba S \u2124q \u2192 \u21ba (R `\u00d7 S) \u2124q\n x \u27e8\u229e\u27e9 y = \u27ea _\u229e_ \u00b7 x \u00b7 y \u27eb\n\n \u27e8_\u229e\u27e9_ : \u2200 {R} \u2192 \u2124q \u2192 \u21ba R \u2124q \u2192 \u21ba R \u2124q\n \u27e8 x \u229e\u27e9 y = \u27ea _\u229e_ x \u00b7 y \u27eb\n\n infix 4 _\u2248\u21ba_ _\u2248\u1d2c_\n _\u2248\u21ba_ : \u2200 {R : `\u2605} (f g : EXP R) \u2192 \u2605\n _\u2248\u21ba_ = _\u2261_ on #_\n\n _\u2248\u1d2c_ : \u2200 {A R} (f g : \u21ba R A) \u2192 Set _\n _\u2248\u1d2c_ {A} f g = \u2200 (Adv : A \u2192 Bit) \u2192 \u27ea Adv \u00b7 f \u27eb \u2248\u21ba \u27ea Adv \u00b7 g \u27eb\n\n lem : \u2200 x \u2192 \u27e8 x \u229e\u27e9 (\u2047 `\u2124q) \u2248\u1d2c \u2047 _\n lem x Adv = sum\u2124q-\u229e-lem x (Bool.to\u2115 \u2218 Adv)\n\n -- \u2200 (A : \u2124q \u2192 Bit) \u2192 # (A \u2047)\n\nopen Fin.Modulo renaming (sucmod to [suc])\n\n{-\nmodule \u2124q-implem (q-2 : \u2115) where\n q : \u2115\n q = 2 + q-2\n\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = zero\n\n [1] : \u2124q\n [1] = suc zero\n-}\nmodule \u2124q-implem (q : \u2115) ([0]' [1]' : Fin q) where\n -- open Sum\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = [0]'\n\n [1] : \u2124q\n [1] = [1]'\n\n _\u2115\u229e_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = m \u2115\u229e ([suc] n)\n {-\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = [suc] (m \u2115\u229e n)\n -}\n\n \u2115\u229e-inj : \u2200 n {x y} \u2192 n \u2115\u229e x \u2261 n \u2115\u229e y \u2192 x \u2261 y\n \u2115\u229e-inj zero eq = eq\n \u2115\u229e-inj (suc n) eq = sucmod-inj _ _ (\u2115\u229e-inj n eq)\n\n _\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u229e n = Fin.to\u2115 m \u2115\u229e n\n\n \u229e-inj : \u2200 m {x y} \u2192 m \u229e x \u2261 m \u229e y \u2192 x \u2261 y\n \u229e-inj m = \u2115\u229e-inj (Fin.to\u2115 m)\n\n _\u2115\u22a0_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u22a0 n = [0]\n suc m \u2115\u22a0 n = n \u229e (m \u2115\u22a0 n)\n\n _\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u22a0 n = Fin.to\u2115 m \u2115\u22a0 n\n\n _[^]\u2115_ : \u2124q \u2192 \u2115 \u2192 \u2124q\n m [^]\u2115 zero = [1]\n m [^]\u2115 suc n = m \u22a0 (m [^]\u2115 n)\n\n _[^]_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m [^] n = m [^]\u2115 (Fin.to\u2115 n)\n\n \u03bc\u2124q : SumProp \u2124q\n \u03bc\u2124q = \u03bcFin q\n\n all\u2124q : Vec \u2124q q\n all\u2124q = allFin q\n\n {-\n sum\u2124q : Sum \u2124q\n sum\u2124q f = Vec.sum (vmap f all\u2124q)\n -}\n\n sum\u2124q-[suc]-lem : \u2200 f \u2192 sum \u03bc\u2124q (f \u2218 [suc]) \u2261 sum \u03bc\u2124q f\n sum\u2124q-[suc]-lem f rewrite \u2261.sym (Vec.sum-map-rot\u2081 f all\u2124q)\n | Vec.map-\u2218 f [suc] all\u2124q\n | rot\u2081-map-sucmod q\n = refl\n\n -- comm-[suc]-\u2115\u229e : \u2200 m n \u2192 [suc] (m \u2115\u229e n) \u2261 m \u2115\u229e ([suc] n)\n\n sum\u2124q-\u2115\u229e-lem : \u2200 m f \u2192 sum \u03bc\u2124q (f \u2218 _\u2115\u229e_ m) \u2261 sum \u03bc\u2124q f\n sum\u2124q-\u2115\u229e-lem zero f = refl\n sum\u2124q-\u2115\u229e-lem (suc m) f rewrite sum\u2124q-[suc]-lem (f \u2218 _\u2115\u229e_ m)\n | sum\u2124q-\u2115\u229e-lem m f = refl\n\n sum\u2124q-\u229e-lem : \u2200 m f \u2192 sum \u03bc\u2124q (f \u2218 _\u229e_ m) \u2261 sum \u03bc\u2124q f\n sum\u2124q-\u229e-lem = sum\u2124q-\u2115\u229e-lem \u2218 Fin.to\u2115\n\n\nmodule G-implem (p q : \u2115) (g' : Fin p) (0[p] 1[p] : Fin p) (0[q] 1[q] : Fin q) where\n open \u2124q-implem q 0[q] 1[q] public\n open \u2124q-implem p 0[p] 1[p] public using () renaming (\u2124q to G; _\u22a0_ to _\u2219_; _[^]\u2115_ to _^[p]_)\n\n g : G\n g = g'\n\n _^_ : G \u2192 \u2124q \u2192 G\n x ^ n = x ^[p] Fin.to\u2115 n\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n {-\n g^-inj : \u2200 m n \u2192 g^ m \u2261 g^ n \u2192 m \u2261 n\n g^-inj = {!!}\n -}\n\nmodule G-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u03bc\u2124q : SumProp \u2124q)\n (sum\u2124q-\u229e-lem : \u2200 x f \u2192 sum \u03bc\u2124q (f \u2218 _\u229e_ x) \u2261 sum \u03bc\u2124q f)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n where\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n open \u2124q-count \u2124q _\u229e_ \u03bc\u2124q sum\u2124q-\u229e-lem\n\n \u2047G : \u21ba `\u2124q G\n run\u21ba \u2047G x = g^ x\n\n #G : Count G\n #G f = #q (f \u2218 g^_)\n\n {-\n #G-\u2219 : \u2200 f m \u2192 #G (f \u2218 _\u2219_ m) \u2261 #G f\n #G-\u2219 f m = {!!}\n -}\n\nmodule DDH\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g^_ : \u2124q \u2192 G)\n where\n DDHAdv : \u2605 \u2192 \u2605\n DDHAdv R = R \u2192 G \u2192 G \u2192 G \u2192 Bit\n\n DDH\u2141\u2080 : \u2200 {R} {_I : \u2605} \u2192 DDHAdv R \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n DDH\u2141\u2080 d (r , x , y , _) = d r (g^ x) (g^ y) (g^ (x \u22a0 y))\n\n DDH\u2141\u2081 : \u2200 {R} \u2192 DDHAdv R \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n DDH\u2141\u2081 d (r , x , y , z) = d r (g^ x) (g^ y) (g^ z)\n\n DDH\u2141 : \u2200 {R} \u2192 DDHAdv R \u2192 Bit \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n DDH\u2141 d b = (case b 0\u2192 DDH\u2141\u2080 1\u2192 DDH\u2141\u2081) d\n\n -- DDH\u2141\u2032 : \u2200 {R} \u2192 DDHAdv R \u2192 (Bit \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q \u00d7 R) \u2192 Bit\n -- DDH\u2141\u2032 d (b , x , y , z , r) = DDH\u2141 d b (x , y , z , r)\n\n module With\u21ba where\n open Univ \u2124q\n open `\u2605 public renaming (`X to `\u2124q)\n DDHAdv\u21ba : `\u2605 \u2192 \u2605\n DDHAdv\u21ba R = G \u2192 G \u2192 G \u2192 \u21ba R Bit\n DDH\u2141\u2080\u21ba : \u2200 {R _I} \u2192 DDHAdv\u21ba R \u2192 \u21ba (R `\u00d7 `\u2124q `\u00d7 `\u2124q `\u00d7 _I) Bit\n run\u21ba (DDH\u2141\u2080\u21ba d) = DDH\u2141\u2080 (\u03bb a b c d \u2192 run\u21ba (d b c d) a)\n\nmodule El-Gamal-Generic\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (Message : \u2605)\n (_\u2219_ : G \u2192 Message \u2192 Message)\n\n -- Required for decryption\n (_\/_ : Message \u2192 G \u2192 Message)\n\n -- Required for the correctness proof\n (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n -- Required for the security proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : SumProp \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : SumProp R\u2090)\n where\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n -- g\u02e3 is the pk\n -- x is the sk\n\n PubKey = G\n SecKey = \u2124q\n KeyPair = PubKey \u00d7 SecKey\n CipherText = G \u00d7 Message\n\n M = Message\n C = CipherText\n\n KeyGen : \u2124q \u2192 KeyPair\n KeyGen x = (g^ x , x)\n\n -- KeyGen\u21ba : \u21ba \u2124q KeyPair\n -- KeyGen\u21ba = mk KeyGen\n\n Enc : PubKey \u2192 Message \u2192 \u2124q \u2192 CipherText\n Enc g\u02e3 m y = g\u02b8 , \u03b6 where\n g\u02b8 = g^ y\n \u03b4 = g\u02e3 ^ y\n \u03b6 = \u03b4 \u2219 m\n\n -- Enc\u21ba : PubKey \u2192 Message \u2192 \u21ba \u2124q CipherText\n -- Enc\u21ba g\u02e3 m = mk (Enc g\u02e3 m)\n\n Dec : SecKey \u2192 CipherText \u2192 Message\n Dec x (g\u02b8 , \u03b6) = \u03b6 \/ (g\u02b8 ^ x)\n\n EncAdv : \u2605 \u2192 \u2605\n EncAdv R\u2090 = (R\u2090 \u2192 PubKey \u2192 Bit \u2192 M)\n \u00d7 (R\u2090 \u2192 PubKey \u2192 C \u2192 Bit)\n\n SS\u2141 : \u2200 {R\u2090 _I : \u2605} \u2192 EncAdv R\u2090 \u2192 Bit \u2192 (R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n SS\u2141 (m , d) b (r\u2090 , x , y , z) =\n let pk = proj\u2081 (KeyGen x) in\n d r\u2090 pk (Enc pk (m r\u2090 pk b) y)\n\n -- Unused\n Game : (i : Bit) \u2192 \u2200 {R\u2090} \u2192 EncAdv R\u2090 \u2192 (Bit \u00d7 R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n Game i (m , d) (b , r\u2090 , x , y , z) = b ==\u1d47 d r\u2090 g\u02e3 (g\u02b8 , \u03b6)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n \u03b4 = g\u02e3 ^ case i 0\u2192 y 1\u2192 z\n \u03b6 = \u03b4 \u2219 m r\u2090 g\u02e3 b\n\n {-\n Game-0b\u2261Game0 : \u2200 {R\u2090} \u2192 Game 0b \u2261 Game0 {R\u2090}\n Game-0b\u2261Game0 = refl\n -}\n\n open DDH \u2124q _\u22a0_ G g^_ public\n\n OTP\u2141 : \u2200 {R : \u2605} \u2192 (R \u2192 G \u2192 Message) \u2192 (R \u2192 G \u2192 G \u2192 Message \u2192 Bit)\n \u2192 (R \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n OTP\u2141 M d (r , x , y , z) = d r g\u02e3 g\u02b8 (g\u1dbb \u2219 M r g\u02e3)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n g\u1dbb = g^ z\n\n TrA : \u2200 {R\u2090} \u2192 Bit \u2192 EncAdv R\u2090 \u2192 DDHAdv R\u2090\n TrA b (m , d) r\u2090 g\u02e3 g\u02b8 g\u02e3\u02b8 = d r\u2090 g\u02e3 (g\u02b8 , g\u02e3\u02b8 \u2219 m r\u2090 g\u02e3 b)\n\n projM : \u2200 {R\u2090} \u2192 EncAdv R\u2090 \u2192 Bit \u2192 R\u2090 \u2192 G \u2192 Message\n projM (m , _) b r\u2090 g\u02e3 = m r\u2090 g\u02e3 b\n\n projD : \u2200 {R\u2090} \u2192 EncAdv R\u2090 \u2192 R\u2090 \u2192 G \u2192 G \u2192 Message \u2192 Bit\n projD (_ , d) r\u2090 g\u02e3 g\u02b8 g\u1dbb\u2219M = d r\u2090 g\u02e3 (g\u02b8 , g\u1dbb\u2219M)\n\n like-SS\u2141 : \u2200 {R\u2090 _I : \u2605} \u2192 EncAdv R\u2090 \u2192 Bit \u2192 (R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 _I) \u2192 Bit\n like-SS\u2141 (m , d) b (r\u2090 , x , y , _z) =\n d r\u2090 g\u02e3 (g\u02b8 , (g\u02e3 ^ y) \u2219 m r\u2090 g\u02e3 b)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n\n SS\u2141\u2261like-SS\u2141 : \u2200 {R _I} \u2192 SS\u2141 {R} {_I} \u2261 like-SS\u2141\n SS\u2141\u2261like-SS\u2141 = refl\n\n -- open Sum\n\n R = R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q\n \u03bcR : SumProp R\n \u03bcR = \u03bcR\u2090 \u00d7\u03bc \u03bc\u2124q \u00d7\u03bc \u03bc\u2124q \u00d7\u03bc \u03bc\u2124q\n\n #R_ : Count R\n #R_ = count \u03bcR\n\n #q_ : Count \u2124q\n #q_ = count \u03bc\u2124q\n\n _\u2248q_ : (f g : \u2124q \u2192 Bit) \u2192 \u2605\n f \u2248q g = #q f \u2261 #q g\n\n _\u2248R_ : (f g : R \u2192 Bit) \u2192 \u2605\n f \u2248R g = #R f \u2261 #R g\n\n functional-correctness : \u2200 x y m \u2192 Dec x (Enc (g^ x) m y) \u2261 m\n functional-correctness x y m rewrite comm-^ g x y | \/-\u2219 (g^ y ^ x) m = refl\n\n module Proof\n (ddh-hyp : \u2200 A \u2192 DDH\u2141 A 0b \u2248R DDH\u2141 A 1b)\n (otp-lem : \u2200 A m\u2080 m\u2081 \u2192 (\u03bb x \u2192 A (g^ x \u2219 m\u2080)) \u2248q (\u03bb x \u2192 A (g^ x \u2219 m\u2081)))\n (A : EncAdv R\u2090) (b : Bit)\n where\n\n OTP\u2141-lem : \u2200 d M\u2080 M\u2081 \u2192 OTP\u2141 M\u2080 d \u2248R OTP\u2141 M\u2081 d\n OTP\u2141-lem d M\u2080 M\u2081 = sum-ext \u03bcR\u2090 (\u03bb r \u2192\n sum-ext \u03bc\u2124q (\u03bb x \u2192\n sum-ext \u03bc\u2124q (\u03bb y \u2192\n pf r x y)))\n where\n pf : \u2200 r x y \u2192 count \u03bc\u2124q (\u03bb z \u2192 OTP\u2141 M\u2080 D (r , x , y , z))\n \u2261 count \u03bc\u2124q (\u03bb z \u2192 OTP\u2141 M\u2081 D (r , x , y , z))\n pf r x y rewrite otp-lem (D r (g^ x) (g^ y)) (M\u2080 r (g^ x)) (M\u2081 r (g^ x)) = refl\n\n A\u1d47 = TrA b A\n A\u00ac\u1d47 = TrA (not b) A\n\n pf0,5 : SS\u2141 A b \u2257 DDH\u2141 A\u1d47 0b\n pf0,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n pf1 : SS\u2141 A b \u2248R DDH\u2141 A\u1d47 0b\n pf1 = sum-ext \u03bcR (cong Bool.to\u2115 \u2218 pf0,5)\n\n pf2 : DDH\u2141 A\u1d47 0b \u2248R DDH\u2141 A\u1d47 1b\n pf2 = ddh-hyp A\u1d47\n\n pf2,5 : DDH\u2141 A\u1d47 1b \u2261 OTP\u2141 (projM A b) (projD A)\n pf2,5 = refl\n\n pf3 : DDH\u2141 A\u1d47 1b \u2248R DDH\u2141 A\u00ac\u1d47 1b\n pf3 = OTP\u2141-lem (projD A) (projM A b) (projM A (not b))\n\n pf4 : DDH\u2141 A\u00ac\u1d47 1b \u2248R DDH\u2141 A\u00ac\u1d47 0b\n pf4 = \u2261.sym (ddh-hyp A\u00ac\u1d47)\n\n pf4,5 : SS\u2141 A (not b) \u2257 DDH\u2141 A\u00ac\u1d47 0b\n pf4,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n pf5 : SS\u2141 A (not b) \u2248R DDH\u2141 A\u00ac\u1d47 0b\n pf5 = sum-ext \u03bcR (cong Bool.to\u2115 \u2218 pf4,5)\n\n final : SS\u2141 A b \u2248R SS\u2141 A (not b)\n final rewrite pf1 | pf2 | pf3 | pf4 | pf5 = refl\n\nmodule El-Gamal-Base\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n\n -- Required for decryption\n (_\/_ : G \u2192 G \u2192 G)\n\n -- Required for the correctness proof\n (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n \n {-\n (_\u207b\u00b9 : G \u2192 G)\n (\u207b\u00b9-inverse : \u2200 x \u2192 x \u207b\u00b9 \u2219 x \u2261 1G)\n -}\n\n -- Required for the proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : SumProp \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : SumProp R\u2090)\n where\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ G _\u2219_\n _\/_ \/-\u2219 comm-^\n dist-^-\u22a0 \u03bc\u2124q R\u2090 \u03bcR\u2090 public\n\n module OTP\u2141-LEM\n (otp-lem1 : \u2200 (A : G \u2192 Bit) m \u2192 (\u03bb x \u2192 A (g^ x \u2219 m)) \u2248q (\u03bb x \u2192 A (g^ x)))\n where\n\n otp-lem : \u2200 (A : G \u2192 Bit) m\u2080 m\u2081 \u2192 (\u03bb x \u2192 A (g^ x \u2219 m\u2080)) \u2248q (\u03bb x \u2192 A (g^ x \u2219 m\u2081))\n otp-lem A m\u2080 m\u2081 rewrite otp-lem1 A m\u2080 | otp-lem1 A m\u2081 = refl\n\nmodule El-Gamal-Hashed\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n -- (HKey : \u2605)\n (|M| : \u2115)\n (\u210b : {-HKey \u2192-} G \u2192 Bits |M|)\n\n -- (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n -- Required for the proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : SumProp \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : SumProp R\u2090)\n where\n\n Message = Bits |M|\n\n \u210b\u27e8_\u27e9\u2295_ : G \u2192 Message \u2192 Message\n \u210b\u27e8 \u03b4 \u27e9\u2295 m = \u210b \u03b4 \u2295 m\n\n _\/_ : Message \u2192 G \u2192 Message\n _\/_ m \u03b4 = \u210b \u03b4 \u2295 m\n\n \n \/-\u2219 : \u2200 x y \u2192 \u210b\u27e8 x \u27e9\u2295 y \/ x \u2261 y\n \/-\u2219 x y = {!!}\n {-\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ Message \u210b\u27e8_\u27e9\u2295_ _\/_ {!!} {!!}\n dist-^-\u22a0 sum\u2124q sum\u2124q-ext R\u2090 sumR\u2090 sumR\u2090-ext public\n -}\n\n {-\n OTP\u2141-lem : \u2200 d M\u2080 M\u2081 \u2192 OTP\u2141 M\u2080 d \u2248R OTP\u2141 M\u2081 d\n OTP\u2141-lem = ?\n -}\n\nmodule \u27e8\u2124p\u27e9\u2605 p-3 {- p is prime -} (`R\u2090 : `\u2605) where\n p : \u2115\n p = 3 + p-3\n\n q : \u2115\n q = p \u2238 1\n\n module G = G-implem p q (## 2) (## 0) (## 1) (## 0) (## 1)\n open G\n\n postulate\n _\u207b\u00b9 : G \u2192 G\n _\/_ : G \u2192 G \u2192 G\n \/-\u2022 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y\n comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x\n dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y\n\n open \u2124q-count \u2124q _\u229e_ \u03bc\u2124q sum\u2124q-\u229e-lem\n\n \u03bcR\u2090 : SumProp (El `R\u2090)\n \u03bcR\u2090 = \u03bcU \u03bc\u2124q `R\u2090\n\n R\u2090 = El `R\u2090\n sumR\u2090 = sum \u03bcR\u2090\n sumR\u2090-ext = sum-ext \u03bcR\u2090\n\n module EB = El-Gamal-Base _ _\u22a0_ G g _^_ _\u2219_ _\/_ \/-\u2022 comm-^ dist-^-\u22a0 \u03bc\u2124q R\u2090 \u03bcR\u2090\n open EB hiding (g^_)\n\n otp-base-lem : \u2200 (A : G \u2192 Bit) m \u2192 (A \u2218 g^_) \u2248q (A \u2218 g^_ \u2218 _\u229e_ m)\n otp-base-lem A m = #-StableUnderInjection {\u03bc = \u03bcFin _} \u03bcFinSUI\n (A \u2218 g^_) (_\u229e_ m) (\u229e-inj m)\n -- oh hai! this is: sym (sum\u2124q-\u229e-lem m (Bool.to\u2115 \u2218 A \u2218 g^_)) -- kthxbye\n\n postulate\n ddh-hyp : (A : DDHAdv R\u2090) \u2192 DDH\u2141 A 0b \u2248R DDH\u2141 A 1b\n otp-lem : \u2200 (A : G \u2192 Bit) m \u2192 (\u03bb x \u2192 A (g^ x \u2219 m)) \u2248q (\u03bb x \u2192 A (g^ x))\n\n\n open OTP\u2141-LEM otp-lem\n\n {-\n final : \u2200 A \u2192 SS\u2141 A 0b \u2248R SS\u2141 A 1b\n final A = Proof.final ddh-hyp OTP\u2141-lem A 0b\n -}\n\nmodule \u27e8\u212411\u27e9\u2605 = \u27e8\u2124p\u27e9\u2605 (11 \u2238 3)\n `X -- the amount of adversary randomness\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"c35a02e641512184761dc90196c26b6363980f5e","subject":"regen the crypto-agda toplevel file","message":"regen the crypto-agda toplevel file\n","repos":"crypto-agda\/crypto-agda","old_file":"crypto-agda.agda","new_file":"crypto-agda.agda","new_contents":"module crypto-agda where\nimport Attack.Compression\nimport Attack.Reencryption\nimport Crypto.Cipher.ElGamal.CPA-DDH\nimport Crypto.Cipher.ElGamal.Generic\nimport Crypto.Cipher.ElGamal.Group\nimport Crypto.Cipher.ElGamal.Homomorphic\nimport Composition.Forkable\nimport Composition.Horizontal\nimport Composition.Vertical\nimport Control.Beh\nimport Control.Protocol.BiSim\nimport Control.Protocol.CoreOld\nimport Control.Protocol.Reduction\nimport Control.Strategy\nimport Control.Strategy.Utils\nimport Crypto.Schemes\nimport FiniteField.FinImplem\nimport FiniteField.JS\nimport FunUniverse.Agda\nimport FunUniverse.BinTree\nimport FunUniverse.Bits\nimport FunUniverse.Category\nimport FunUniverse.Category.Op\nimport FunUniverse.Circuit\nimport FunUniverse.Const\nimport FunUniverse.Core\nimport FunUniverse.Cost\nimport FunUniverse.Data\nimport FunUniverse.Defaults.FirstPart\n-- import FunUniverse.ExnArrow\nimport FunUniverse.Fin\nimport FunUniverse.Fin.Op\nimport FunUniverse.Fin.Op.Abstract\n-- import FunUniverse.FlatFunsProd\nimport FunUniverse.Interface.Bits\nimport FunUniverse.Interface.Two\nimport FunUniverse.Interface.Vec\nimport FunUniverse.Inverse\nimport FunUniverse.Loop\nimport FunUniverse.Nand\nimport FunUniverse.Nand.Function\nimport FunUniverse.Nand.Properties\nimport FunUniverse.README\nimport FunUniverse.Rewiring.Linear\n-- import FunUniverse.State\nimport FunUniverse.Syntax\nimport FunUniverse.Types\nimport Game.Challenge\nimport Game.DDH\nimport Game.EntropySmoothing\nimport Game.EntropySmoothing.WithKey\nimport Game.Generic\nimport Game.IND-CCA\nimport Game.IND-CCA2-dagger\nimport Game.IND-CCA2-dagger.Adversary\nimport Game.IND-CCA2-dagger.Experiment\nimport Game.IND-CCA2-dagger.Protocol\nimport Game.IND-CCA2-dagger.ProtocolImplementation\nimport Game.IND-CCA2-dagger.Valid\nimport Game.IND-CCA2-gen.Protocol\nimport Game.IND-CCA2-gen.ProtocolImplementation\nimport Game.IND-CCA2\n-- import Game.IND-CCA2.Advantage\nimport Game.IND-CPA-alt\nimport Game.IND-CPA-dagger\nimport Game.IND-CPA-utils\nimport Game.IND-CPA\nimport Game.IND-CPA.Core\nimport Game.IND-NM-CPA\nimport Game.NCE\nimport Game.ReceiptFreeness\nimport Game.ReceiptFreeness.Adversary\nimport Game.ReceiptFreeness.CheatingAdversaries\nimport Game.ReceiptFreeness.Definitions\nimport Game.ReceiptFreeness.Definitions.Encryption\nimport Game.ReceiptFreeness.Definitions.Receipt\nimport Game.ReceiptFreeness.Definitions.Tally\nimport Game.ReceiptFreeness.Experiment\nimport Game.ReceiptFreeness.Protocol\nimport Game.ReceiptFreeness.ProtocolImplementation\nimport Game.ReceiptFreeness.Valid\nimport Game.ReceiptFreeness.ValidInst\nimport Game.Transformation.CCA-CPA\nimport Game.Transformation.CCA2-CCA\nimport Game.Transformation.CCA2-CCA2d\nimport Game.Transformation.CCA2d-CCA2\nimport Game.Transformation.CPA-CPAd\nimport Game.Transformation.CPAd-CPA\nimport Game.Transformation.Naor-Yung-proof\nimport Game.Transformation.Naor-Yung\nimport Game.Transformation.ReceiptFreeness-CCA2d\nimport Game.Transformation.ReceiptFreeness-CCA2d.Proof\nimport Game.Transformation.ReceiptFreeness-CCA2d.Protocol\n--TODO import Game.Transformation.ReceiptFreeness-CCA2d.ProtocolImplementation\nimport Game.Transformation.ReceiptFreeness-CCA2d.Simulator\nimport Game.Transformation.ReceiptFreeness-CCA2d.SimulatorInst\n--TODO import Game.Transformation.ReceiptFreeness-CCA2d.Valid\nimport Helios\nimport Language.Simple.Abstract\nimport Language.Simple.Free\nimport Language.Simple.Interface\nimport Language.Simple.Two.Mux\nimport Language.Simple.Two.Mux.Normalise\nimport Language.Simple.Two.Nand\nimport Negligible\nimport README\nimport Solver.AddMax\nimport Solver.Linear\nimport Solver.Linear.Examples\nimport Solver.Linear.Parser\nimport Solver.Linear.Syntax\nimport ZK.ChaumPedersen\n-- import ZK.Disjunctive\nimport ZK.GroupHom\nimport ZK.GroupHom.ElGamal\nimport ZK.GroupHom.FieldChal\nimport ZK.GroupHom.FieldChal2\nimport ZK.GroupHom.FieldChal3\nimport ZK.GroupHom.NatChal\nimport ZK.GroupHom.NumChal\nimport ZK.GroupHom.Types\nimport ZK.JSChecker\nimport ZK.Lemmas\nimport ZK.PartialHeliosVerifier\nimport ZK.Schnorr\nimport ZK.Schnorr.KnownStatement\nimport ZK.SigmaProtocol\nimport ZK.SigmaProtocol.KnownStatement\nimport ZK.SigmaProtocol.Map\nimport ZK.SigmaProtocol.Signature\nimport ZK.SigmaProtocol.Structure\nimport ZK.SigmaProtocol.Types\nimport ZK.Statement\n-- import ZK.Strong-Fiat-Shamir\nimport ZK.Types\nimport adder\nimport alea.cpo\nimport bijection-syntax.Bijection-Fin\nimport bijection-syntax.Bijection\nimport bijection-syntax.README\nimport circuits.bytecode\nimport circuits.circuit\nimport cycle-id\nimport cycle\nimport cycle3\nimport cyclic10\nimport forking-lemma\n-- import hash-param\n-- import misc.merkle-tree\n-- import misc.secret-sharing\n-- import misc.zk\n-- import rewind-on-success\nimport sha1\n","old_contents":"module crypto-agda where\n\n--import Control.Protocol\nimport Explore.README\n\nimport Attack.Compression\nimport Attack.Reencryption\nimport Crypto.Cipher.ElGamal.CPA-DDH\nimport Crypto.Cipher.ElGamal.Generic\nimport Crypto.Cipher.ElGamal.Group\nimport Crypto.Cipher.ElGamal.Homomorphic\nimport Composition.Forkable\nimport Composition.Horizontal\nimport Composition.Vertical\nimport Control.Beh\nimport Control.Protocol.BiSim\nimport Control.Protocol.CoreOld\nimport Control.Protocol.Reduction\nimport Control.Strategy\nimport Control.Strategy.Utils\nimport Crypto.Schemes\nimport FiniteField.FinImplem\nimport FunUniverse.Agda\nimport FunUniverse.BinTree\nimport FunUniverse.Bits\nimport FunUniverse.Category\nimport FunUniverse.Category.Op\nimport FunUniverse.Circuit\nimport FunUniverse.Const\nimport FunUniverse.Core\nimport FunUniverse.Cost\nimport FunUniverse.Data\nimport FunUniverse.Defaults.FirstPart\n--import FunUniverse.ExnArrow\nimport FunUniverse.Fin\nimport FunUniverse.Fin.Op\nimport FunUniverse.Fin.Op.Abstract\n--import FunUniverse.FlatFunsProd\nimport FunUniverse.Interface.Bits\nimport FunUniverse.Interface.Two\nimport FunUniverse.Interface.Vec\nimport FunUniverse.Inverse\nimport FunUniverse.Loop\nimport FunUniverse.Nand\nimport FunUniverse.Nand.Function\nimport FunUniverse.Nand.Properties\nimport FunUniverse.README\nimport FunUniverse.Rewiring.Linear\n--import FunUniverse.State\nimport FunUniverse.Syntax\nimport FunUniverse.Types\nimport Game.Challenge\nimport Game.DDH\nimport Game.EntropySmoothing\nimport Game.EntropySmoothing.WithKey\nimport Game.Generic\nimport Game.IND-CCA\nimport Game.IND-CCA2-dagger\nimport Game.IND-CCA2-dagger.Adversary\nimport Game.IND-CCA2-dagger.Experiment\nimport Game.IND-CCA2-dagger.Protocol\nimport Game.IND-CCA2-dagger.ProtocolImplementation\nimport Game.IND-CCA2-dagger.Valid\nimport Game.IND-CCA2-gen.Protocol\nimport Game.IND-CCA2-gen.ProtocolImplementation\nimport Game.IND-CCA2\nimport Game.IND-CPA-alt\nimport Game.IND-CPA-dagger\nimport Game.IND-CPA-utils\nimport Game.IND-CPA\nimport Game.NCE\nimport Game.ReceiptFreeness\nimport Game.ReceiptFreeness.Adversary\nimport Game.ReceiptFreeness.CheatingAdversaries\nimport Game.ReceiptFreeness.Definitions\nimport Game.ReceiptFreeness.Definitions.Encryption\nimport Game.ReceiptFreeness.Definitions.Receipt\nimport Game.ReceiptFreeness.Definitions.Tally\nimport Game.ReceiptFreeness.Experiment\nimport Game.ReceiptFreeness.Protocol\nimport Game.ReceiptFreeness.ProtocolImplementation\nimport Game.ReceiptFreeness.Valid\nimport Game.ReceiptFreeness.ValidInst\nimport Game.Transformation.CCA-CPA\nimport Game.Transformation.CCA2-CCA\n--import Game.Transformation.CCA2-CCA2d\nimport Game.Transformation.CCA2d-CCA2\nimport Game.Transformation.CPA-CPAd\nimport Game.Transformation.CPAd-CPA\nimport Game.Transformation.Naor-Yung-proof\nimport Game.Transformation.Naor-Yung\nimport Game.Transformation.ReceiptFreeness-CCA2d\nimport Game.Transformation.ReceiptFreeness-CCA2d.Proof\nimport Game.Transformation.ReceiptFreeness-CCA2d.Protocol\nimport Game.Transformation.ReceiptFreeness-CCA2d.ProtocolImplementation\nimport Game.Transformation.ReceiptFreeness-CCA2d.Simulator\nimport Game.Transformation.ReceiptFreeness-CCA2d.SimulatorInst\n-- import Game.Transformation.ReceiptFreeness-CCA2d.Valid\nimport Language.Simple.Abstract\nimport Language.Simple.Free\nimport Language.Simple.Interface\nimport Language.Simple.Two.Mux\nimport Language.Simple.Two.Mux.Normalise\nimport Language.Simple.Two.Nand\nimport README\nimport Solver.AddMax\nimport Solver.Linear\nimport Solver.Linear.Examples\nimport Solver.Linear.Parser\nimport Solver.Linear.Syntax\nimport ZK.PartialHeliosVerifier\nimport ZK.JSChecker\nimport ZK.GroupHom\nimport ZK.GroupHom.FieldChal\nimport ZK.GroupHom.FieldChal2\nimport ZK.GroupHom.FieldChal3\nimport ZK.GroupHom.ElGamal\nimport adder\nimport alea.cpo\nimport bijection-syntax.Bijection-Fin\nimport bijection-syntax.Bijection\nimport bijection-syntax.README\nimport circuits.bytecode\nimport circuits.circuit\nimport sha1\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"716297f35eafe17ce828bb82de3d5879b784645f","subject":"findB -> findKey","message":"findB -> findKey\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits\/Count.agda","new_file":"lib\/Data\/Bits\/Count.agda","new_contents":"\nmodule Data.Bits.Count where\n\nopen import Type hiding (\u2605)\nopen import Data.Bits\nopen import Data.Bits.OperationSyntax\nimport Data.Bits.Search as Search\nopen Search.SimpleSearch\nopen import Data.Bits.Sum\n\n\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\n\nopen import Data.Maybe.NP\n\nopen import Data.Nat.NP hiding (_==_) \nopen import Data.Nat.Properties\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\n\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\n\nopen import Function.NP\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n#\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n-- #-ext\n#-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n#-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n#-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n#-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n#-bij : \u2200 {n} f (g : Bits n \u2192 Bit) \u2192 #\u27e8 g \u2218 eval f \u27e9 \u2261 #\u27e8 g \u27e9\n#-bij f g = sum-bij f (Bool.to\u2115 \u2218 g)\n\n#-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n#-\u2295 = #-comm\n\n#-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n#-const n b = sum-const n (Bool.to\u2115 b)\n\n#never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n#never\u22610 = sum-const0\u22610\n\n#always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n#always\u22612^ n = sum-const n 1\n\n#-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 Bit) \u2192 sum (\u03bb x \u2192 Bool.to\u2115 (f\u2080 x) + Bool.to\u2115 (f\u2081 x)) \u2261 #\u27e8 f\u2080 \u27e9 + #\u27e8 f\u2081 \u27e9\n#-dist f\u2080 f\u2081 = sum-dist (Bool.to\u2115 \u2218 f\u2080) (Bool.to\u2115 \u2218 f\u2081)\n\n#-+ : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n #\u27e8 f \u27e9 \u2261 sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9 )\n#-+ {m} {n} f = sum-+ {m} {n} (Bool.to\u2115 \u2218 f)\n\n#-# : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9)\n \u2261 sum {n} (\u03bb ys \u2192 #\u27e8 (\u03bb (xs : Bits m) \u2192 f (xs ++ ys)) \u27e9)\n#-# {m} {n} f = sum-sum {m} {n} (Bool.to\u2115 \u2218 f)\n\n#-swap : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192 #\u27e8 f \u2218 vswap n {m} \u27e9 \u2261 #\u27e8 f \u27e9\n#-swap {m} {n} f = sum-swap {m} {n} (Bool.to\u2115 \u2218 f)\n\n#\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n#\u27e8== [] \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n\u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n\u2257-cong-# f g f\u2257g = sum-\u2257 _ _ (cong Bool.to\u2115 \u2218 f\u2257g)\n\n-- #-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n-- #-+ f f0 f1 rewrite f0 | f1 = refl\n\n#-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-take-drop zero n f g with f []\n... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n... | false = #never\u22610 n\n#-take-drop (suc m) n f g\n rewrite \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x))\n | #-take-drop m n (f \u2218 0\u2237_) g\n | \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x))\n | #-take-drop m n (f \u2218 1\u2237_) g\n = sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9)\n\n#-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n#\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n#\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n#\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n... | true | true | _ = s\u2264s z\u2264n\n... | true | false | p = \u22a5-elim (p _)\n... | false | _ | _ = z\u2264n\n#\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n#-\u2227-\u2228\u1d47 : \u2200 x y \u2192 Bool.to\u2115 (x \u2227 y) + Bool.to\u2115 (x \u2228 y) \u2261 Bool.to\u2115 x + Bool.to\u2115 y\n#-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (Bool.to\u2115 y) 1 = refl\n#-\u2227-\u2228\u1d47 false y = refl\n\n#-LEM : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g |\u2227| f \u27e9 + #\u27e8 |not| g |\u2227| f \u27e9\n#-LEM {zero} f g with g []\n... | false = refl\n... | true = \u2115\u00b0.+-comm 0 #\u27e8 f \u27e9\n#-LEM {suc n} f g \n rewrite #-LEM (f \u2218 0\u2237_) (g \u2218 0\u2237_)\n | #-LEM (f \u2218 1\u2237_) (g \u2218 1\u2237_)\n = +-interchange #\u27e8 (g \u2218 0\u2237_) |\u2227| (f \u2218 0\u2237_) \u27e9\n #\u27e8 |not| (g \u2218 0\u2237_) |\u2227| (f \u2218 0\u2237_) \u27e9 \n #\u27e8 (g \u2218 1\u2237_) |\u2227| (f \u2218 1\u2237_) \u27e9\n #\u27e8 |not| (g \u2218 1\u2237_) |\u2227| (f \u2218 1\u2237_) \u27e9\n\n\n#-\u2227-snd : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 #\u27e8 g \u27e9\n#-\u2227-snd {zero} f g with f [] | g []\n... | false | false = z\u2264n\n... | false | true = z\u2264n\n... | true | _ = \u2115\u2264.reflexive refl\n#-\u2227-snd {suc n} f g = #-\u2227-snd (f \u2218 0\u2237_) (g \u2218 0\u2237_) \n +-mono #-\u2227-snd (f \u2218 1\u2237_) (g \u2218 1\u2237_)\n\n#-\u2227-fst : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 #\u27e8 f \u27e9\n#-\u2227-fst f g = \n #\u27e8 f |\u2227| g \u27e9 \n \u2261\u27e8 #-\u2257 (f |\u2227| g) (g |\u2227| f) (|\u2227|-comm f g) \u27e9 \n #\u27e8 g |\u2227| f \u27e9 \n \u2264\u27e8 #-\u2227-snd g f \u27e9 \n #\u27e8 f \u27e9 \u220e\n where open \u2264-Reasoning\n\n#-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n#-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#\u2228' {zero} f g with f []\n... | true = s\u2264s z\u2264n\n... | false = \u2115\u2264.refl\n#\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 (m + n)\n#\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n#\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n#\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n#-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n#-bound zero f = Bool.to\u2115\u22641 (f [])\n#-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n#-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n#-\u2218vnot _ f = #-\u2295 1\u207f f\n\n#-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n#-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n#-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n#-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n#-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n#-not\u2218 zero f with f []\n... | true = \u2261.refl\n... | false = \u2261.refl\n#-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n#-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n#-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n\ndifference-lemma : \u2200 {n}(A B F : Bits n \u2192 Bit) \n \u2192 #\u27e8 |not| F |\u2227| A \u27e9 \u2261 #\u27e8 |not| F |\u2227| B \u27e9\n \u2192 dist #\u27e8 A \u27e9 #\u27e8 B \u27e9 \u2264 #\u27e8 F \u27e9\ndifference-lemma A B F A\u2227\u00acF\u2261B\u2227\u00acF = \n dist #\u27e8 A \u27e9 #\u27e8 B \u27e9 \n \u2261\u27e8 cong\u2082 dist (#-LEM A F) (#-LEM B F) \u27e9\n dist (#\u27e8 F |\u2227| A \u27e9 + #\u27e8 |not| F |\u2227| A \u27e9)\n (#\u27e8 F |\u2227| B \u27e9 + #\u27e8 |not| F |\u2227| B \u27e9)\n \u2261\u27e8 cong\u2082 dist (\u2115\u00b0.+-comm #\u27e8 F |\u2227| A \u27e9 #\u27e8 |not| F |\u2227| A \u27e9) \n (\u2115\u00b0.+-comm #\u27e8 F |\u2227| B \u27e9 #\u27e8 |not| F |\u2227| B \u27e9) \u27e9\n dist (#\u27e8 |not| F |\u2227| A \u27e9 + #\u27e8 F |\u2227| A \u27e9)\n (#\u27e8 |not| F |\u2227| B \u27e9 + #\u27e8 F |\u2227| B \u27e9)\n \u2261\u27e8 cong\u2082 dist (refl {x = #\u27e8 |not| F |\u2227| A \u27e9 + #\u27e8 F |\u2227| A \u27e9})\n (cong\u2082 _+_ (sym A\u2227\u00acF\u2261B\u2227\u00acF) (refl {x = #\u27e8 F |\u2227| B \u27e9})) \u27e9\n dist (#\u27e8 |not| F |\u2227| A \u27e9 + #\u27e8 F |\u2227| A \u27e9)\n (#\u27e8 |not| F |\u2227| A \u27e9 + #\u27e8 F |\u2227| B \u27e9)\n \u2261\u27e8 dist-x+ #\u27e8 |not| F |\u2227| A \u27e9 #\u27e8 F |\u2227| A \u27e9 #\u27e8 F |\u2227| B \u27e9 \u27e9\n dist #\u27e8 F |\u2227| A \u27e9 #\u27e8 F |\u2227| B \u27e9\n \u2264\u27e8 dist-bounded {#\u27e8 F |\u2227| A \u27e9} {#\u27e8 F |\u2227| B \u27e9} {#\u27e8 F \u27e9} (#-\u2227-fst F A) (#-\u2227-fst F B) \u27e9 \n #\u27e8 F \u27e9 \u220e\n where open \u2264-Reasoning\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : \u2605 a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindKey : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindKey pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n","old_contents":"\nmodule Data.Bits.Count where\n\nopen import Type hiding (\u2605)\nopen import Data.Bits\nopen import Data.Bits.OperationSyntax\nimport Data.Bits.Search as Search\nopen Search.SimpleSearch\nopen import Data.Bits.Sum\n\n\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\n\nopen import Data.Maybe.NP\n\nopen import Data.Nat.NP hiding (_==_) \nopen import Data.Nat.Properties\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\n\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\n\nopen import Function.NP\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n#\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n-- #-ext\n#-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n#-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n#-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n#-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n#-bij : \u2200 {n} f (g : Bits n \u2192 Bit) \u2192 #\u27e8 g \u2218 eval f \u27e9 \u2261 #\u27e8 g \u27e9\n#-bij f g = sum-bij f (Bool.to\u2115 \u2218 g)\n\n#-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n#-\u2295 = #-comm\n\n#-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n#-const n b = sum-const n (Bool.to\u2115 b)\n\n#never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n#never\u22610 = sum-const0\u22610\n\n#always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n#always\u22612^ n = sum-const n 1\n\n#-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 Bit) \u2192 sum (\u03bb x \u2192 Bool.to\u2115 (f\u2080 x) + Bool.to\u2115 (f\u2081 x)) \u2261 #\u27e8 f\u2080 \u27e9 + #\u27e8 f\u2081 \u27e9\n#-dist f\u2080 f\u2081 = sum-dist (Bool.to\u2115 \u2218 f\u2080) (Bool.to\u2115 \u2218 f\u2081)\n\n#-+ : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n #\u27e8 f \u27e9 \u2261 sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9 )\n#-+ {m} {n} f = sum-+ {m} {n} (Bool.to\u2115 \u2218 f)\n\n#-# : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9)\n \u2261 sum {n} (\u03bb ys \u2192 #\u27e8 (\u03bb (xs : Bits m) \u2192 f (xs ++ ys)) \u27e9)\n#-# {m} {n} f = sum-sum {m} {n} (Bool.to\u2115 \u2218 f)\n\n#-swap : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192 #\u27e8 f \u2218 vswap n {m} \u27e9 \u2261 #\u27e8 f \u27e9\n#-swap {m} {n} f = sum-swap {m} {n} (Bool.to\u2115 \u2218 f)\n\n#\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n#\u27e8== [] \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n\u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n\u2257-cong-# f g f\u2257g = sum-\u2257 _ _ (cong Bool.to\u2115 \u2218 f\u2257g)\n\n-- #-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n-- #-+ f f0 f1 rewrite f0 | f1 = refl\n\n#-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-take-drop zero n f g with f []\n... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n... | false = #never\u22610 n\n#-take-drop (suc m) n f g\n rewrite \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x))\n | #-take-drop m n (f \u2218 0\u2237_) g\n | \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x))\n | #-take-drop m n (f \u2218 1\u2237_) g\n = sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9)\n\n#-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n#\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n#\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n#\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n... | true | true | _ = s\u2264s z\u2264n\n... | true | false | p = \u22a5-elim (p _)\n... | false | _ | _ = z\u2264n\n#\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n#-\u2227-\u2228\u1d47 : \u2200 x y \u2192 Bool.to\u2115 (x \u2227 y) + Bool.to\u2115 (x \u2228 y) \u2261 Bool.to\u2115 x + Bool.to\u2115 y\n#-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (Bool.to\u2115 y) 1 = refl\n#-\u2227-\u2228\u1d47 false y = refl\n\n#-LEM : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g |\u2227| f \u27e9 + #\u27e8 |not| g |\u2227| f \u27e9\n#-LEM {zero} f g with g []\n... | false = refl\n... | true = \u2115\u00b0.+-comm 0 #\u27e8 f \u27e9\n#-LEM {suc n} f g \n rewrite #-LEM (f \u2218 0\u2237_) (g \u2218 0\u2237_)\n | #-LEM (f \u2218 1\u2237_) (g \u2218 1\u2237_)\n = +-interchange #\u27e8 (g \u2218 0\u2237_) |\u2227| (f \u2218 0\u2237_) \u27e9\n #\u27e8 |not| (g \u2218 0\u2237_) |\u2227| (f \u2218 0\u2237_) \u27e9 \n #\u27e8 (g \u2218 1\u2237_) |\u2227| (f \u2218 1\u2237_) \u27e9\n #\u27e8 |not| (g \u2218 1\u2237_) |\u2227| (f \u2218 1\u2237_) \u27e9\n\n\n#-\u2227-snd : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 #\u27e8 g \u27e9\n#-\u2227-snd {zero} f g with f [] | g []\n... | false | false = z\u2264n\n... | false | true = z\u2264n\n... | true | _ = \u2115\u2264.reflexive refl\n#-\u2227-snd {suc n} f g = #-\u2227-snd (f \u2218 0\u2237_) (g \u2218 0\u2237_) \n +-mono #-\u2227-snd (f \u2218 1\u2237_) (g \u2218 1\u2237_)\n\n#-\u2227-fst : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 #\u27e8 f \u27e9\n#-\u2227-fst f g = \n #\u27e8 f |\u2227| g \u27e9 \n \u2261\u27e8 #-\u2257 (f |\u2227| g) (g |\u2227| f) (|\u2227|-comm f g) \u27e9 \n #\u27e8 g |\u2227| f \u27e9 \n \u2264\u27e8 #-\u2227-snd g f \u27e9 \n #\u27e8 f \u27e9 \u220e\n where open \u2264-Reasoning\n\n#-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n#-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#\u2228' {zero} f g with f []\n... | true = s\u2264s z\u2264n\n... | false = \u2115\u2264.refl\n#\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 (m + n)\n#\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n#\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n#\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n#-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n#-bound zero f = Bool.to\u2115\u22641 (f [])\n#-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n#-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n#-\u2218vnot _ f = #-\u2295 1\u207f f\n\n#-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n#-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n#-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n#-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n#-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n#-not\u2218 zero f with f []\n... | true = \u2261.refl\n... | false = \u2261.refl\n#-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n#-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n#-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n\ndifference-lemma : \u2200 {n}(A B F : Bits n \u2192 Bit) \n \u2192 #\u27e8 |not| F |\u2227| A \u27e9 \u2261 #\u27e8 |not| F |\u2227| B \u27e9\n \u2192 dist #\u27e8 A \u27e9 #\u27e8 B \u27e9 \u2264 #\u27e8 F \u27e9\ndifference-lemma A B F A\u2227\u00acF\u2261B\u2227\u00acF = \n dist #\u27e8 A \u27e9 #\u27e8 B \u27e9 \n \u2261\u27e8 cong\u2082 dist (#-LEM A F) (#-LEM B F) \u27e9\n dist (#\u27e8 F |\u2227| A \u27e9 + #\u27e8 |not| F |\u2227| A \u27e9)\n (#\u27e8 F |\u2227| B \u27e9 + #\u27e8 |not| F |\u2227| B \u27e9)\n \u2261\u27e8 cong\u2082 dist (\u2115\u00b0.+-comm #\u27e8 F |\u2227| A \u27e9 #\u27e8 |not| F |\u2227| A \u27e9) \n (\u2115\u00b0.+-comm #\u27e8 F |\u2227| B \u27e9 #\u27e8 |not| F |\u2227| B \u27e9) \u27e9\n dist (#\u27e8 |not| F |\u2227| A \u27e9 + #\u27e8 F |\u2227| A \u27e9)\n (#\u27e8 |not| F |\u2227| B \u27e9 + #\u27e8 F |\u2227| B \u27e9)\n \u2261\u27e8 cong\u2082 dist (refl {x = #\u27e8 |not| F |\u2227| A \u27e9 + #\u27e8 F |\u2227| A \u27e9})\n (cong\u2082 _+_ (sym A\u2227\u00acF\u2261B\u2227\u00acF) (refl {x = #\u27e8 F |\u2227| B \u27e9})) \u27e9\n dist (#\u27e8 |not| F |\u2227| A \u27e9 + #\u27e8 F |\u2227| A \u27e9)\n (#\u27e8 |not| F |\u2227| A \u27e9 + #\u27e8 F |\u2227| B \u27e9)\n \u2261\u27e8 dist-x+ #\u27e8 |not| F |\u2227| A \u27e9 #\u27e8 F |\u2227| A \u27e9 #\u27e8 F |\u2227| B \u27e9 \u27e9\n dist #\u27e8 F |\u2227| A \u27e9 #\u27e8 F |\u2227| B \u27e9\n \u2264\u27e8 dist-bounded {#\u27e8 F |\u2227| A \u27e9} {#\u27e8 F |\u2227| B \u27e9} {#\u27e8 F \u27e9} (#-\u2227-fst F A) (#-\u2227-fst F B) \u27e9 \n #\u27e8 F \u27e9 \u220e\n where open \u2264-Reasoning\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : \u2605 a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"825b66ffe80a00ed52000825c62d346bf51ddbc1","subject":"agda : Swierstra_A_Predicate_Transformer_for_Effects","message":"agda : Swierstra_A_Predicate_Transformer_for_Effects\n","repos":"haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc","old_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/wouter-swierstra-predicate-transformers-6fe8f13\/PredicateTransformers.agda","new_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/wouter-swierstra-predicate-transformers-6fe8f13\/PredicateTransformers.agda","new_contents":"module PredicateTransformers where\n\nopen import Prelude hiding (map; all)\nopen import Level hiding (lift)\n\n------------------------------------------------------------------------------\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\n------------------------------------------------------------------------------\nABSTRACT\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nrefinement type https:\/\/en.wikipedia.org\/wiki\/Refinement_(computing)\n- type with a predicate which must hold for any element of the refined type\n- can express\n - preconditions when used as function args\n - postconditions when used as return types\n\nthe problem statement is centered around \/compositionality\/\n- pure functions enable compositional reasoning\n- effectful programs require reasoning about context\n- paper provides mechanism for reasoning about effectful programs (and for program calculation)\n\nmain idea\n- represent effectful computation as a free monad\n- write an \"interpreter\" that computes a predicate transformer\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to use this refinement relation to\n - show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\nmodule Free where\n -- Free : represents syntax of an effectful computation\n -- C : type of commands\n -- Free C R : returns an 'a' or issues command c : C\n -- For each c : C, there is a set of responses R c\n -- - R : C -> Set : is type family of responses for commands\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\" input to output\n -- input\n -- - pure function 'a\u2192b : a -> b'\n -- - postcondition 'b -> Set' (on output of a\u2192b)\n -- output\n -- - weakest precondition 'a -> Set' on input to a\u2192b that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n module WP0Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data isEven : \u2115 \u2192 Set where\n even-z : isEven Nat.zero\n even-s : {n : \u2115} \u2192 isEven n \u2192 isEven (Nat.suc (Nat.suc n))\n\n isEvenPT : \u2115 -> Set\n isEvenPT = wp0 (_+ 2) isEven\n\n isEven5 : Set\n isEven5 = isEvenPT 5\n\n isEven5' : isEvenPT 5 \u2261 isEven 7\n isEven5' = refl\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - enable via making dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n -- every element a that satisfies P is contained in the set of things that satifies Q\n --\n -- implication between predicates\n --\n -- P a says a \u2208 P,\n -- if the extents of P, Q : A \u2192 Set is taken to be\n -- the subset of A satisfying predicates P, Q\n --\n -- the extent of P : A -> Set is the subset of terms of type A satisfying P.\n -- \"extent\" indicates a kind of black-box view of things,\n -- e.g., the \"extent\" of a function is the set of its input-output pairs.\n --\n -- https:\/\/en.wikipedia.org\/wiki\/Extension_%28semantics%29\n -- the extension of a concept, idea, or sign consists of the things to which it applies\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n -- refinement relation between PTs : pt1 refined by pt2\n --\n -- (same thing as \u2286, but one level up)\n -- for every post condition, pt2 weakens the precondition\n -- \"weakest\" is the right side : pt2 P\n --\n -- pt1 , pt2 : preconditions for satisfying a program\n -- refinement : pt1 maybe be a stronger requirement than pt2\n --\n -- pt2 is a refinement because it weakens what is required by pt1\n -- e.g., pt1 pt2\n -- >= 20 >= 10\n --\n -- weakening the input assumptions gives a stronger result\n --\n -- contract says establish a pre and I will give you a post, i.e.,\n -- \"stronger\" post condition\n -- or \"weaker\" pre condition\n --\n -- refinement of predicate transformers\n --\n -- use case: relate different implementations of the \"same program\"\n -- pt1 \u2291 pt2 : pt1 is refined by pt2\n -- - if pt1 is understood as giving preconditions which satisfy\n -- postcondition P for some given program f1\n -- then his says the precondition can be \/weakened\/ and still guarantee the postcondition\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g\n -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation to\n - relate PT semantics between programs and specifications\n - show a program satisfies its specification\n - show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n - corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n note : for =Free C R b= programs means generating the same AST\n\n instead, define predicate transformers that give \/interpretations\/\n to a particular choice of command type C and response family R\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public using (_+_; _>_; _*_)\n renaming (\u2115 to Nat; zero to Zero; suc to Succ)\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n -- one command\n data C : Set where\n Abort : C -- no continuation\n\n -- response code : the program does not execute further\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n -- BIG-STEP SEMANTICS specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n module \u21d3Test where\n exv : Expr\n exv = Val 3\n exd : Expr\n exd = Div (Val 3) (Val 0)\n\n exb0 : Val 0 \u21d3 0\n exb0 = \u21d3Base\n\n exb3 : Val 3 \u21d3 3\n exb3 = \u21d3Base\n\n exs331 : Div (Val 3) (Val 3) \u21d3 1\n exs331 = \u21d3Step \u21d3Base \u21d3Base\n\n exs931 : Div (Val 9) (Val 3) \u21d3 3\n exs931 = \u21d3Step \u21d3Base \u21d3Base\n\n exs9d31 : Div (Val 9) (Div (Val 3) (Val 1)) \u21d3 3\n exs9d31 = \u21d3Step \u21d3Base (\u21d3Step \u21d3Base \u21d3Base)\n\n exsd91d31 : Div (Div (Val 9) (Val 1)) (Div (Val 3) (Val 1)) \u21d3 3\n exsd91d31 = \u21d3Step (\u21d3Step \u21d3Base \u21d3Base) (\u21d3Step \u21d3Base \u21d3Base)\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val x \u27e7 = return x\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\v1 ->\n \u27e6 er \u27e7 >>= \\v2 ->\n v1 \u00f7 v2\n\n module InterpTest where\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n evv' : evv \u2261 Pure 3\n evv' = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n evd' : evd \u2261 Pure 1\n evd' = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n evd0' : evd0 \u2261 Step Abort (\u03bb ())\n evd0' = refl\n\n {-\n to relate big step semantics to monadic interpreter\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - _\u21d3_ relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n\n to call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results P' : Partial b -> Set\n done via 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so Abort case returns empty type\n -}\n\n -- convert post-condition for a pure function 'f : (x : a) -> b x'\n -- to post-condition on partial functions 'f : (x : A) -> Partial (b x)'\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba -- P holds if operation produces a value\n mustPT _ _ (Step Abort _) = \u22a5 -- if operation fails, there is no result to apply P to\n -- note: could give \u22a4 here, but want to rule out failure\n -- (total correctness)\n\n module MustPTTest where\n bs\u21d3 : Expr -> Nat -> Set\n bs\u21d3 = _\u21d3_\n\n bs\u21d3' : Set\n bs\u21d3' = Val 1 \u21d3 1\n\n bsp\u21d3 : Expr -> Partial Nat -> Set\n bsp\u21d3 = mustPT _\u21d3_\n\n bsp\u21d3' : Set\n bsp\u21d3' = mustPT _\u21d3_ (Val 1) (Pure 1)\n\n bsp\u21d3'x : mustPT _\u21d3_ (Val 1) (Pure 1)\n bsp\u21d3'x = \u21d3Base\n\n bsp\u21d3'' : Set\n bsp\u21d3'' = mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n\n {- nothing can be constructed with this type\n bsp\u21d3''x : mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n bsp\u21d3''x = {!!}\n -}\n xxx : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb v -> Pure 3 >>= _\u00f7 v))\n xxx = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx' : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n xxx' = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx'' : Set\n xxx'' = mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n module WpPartialTest where\n exwpp : Expr -> Set\n exwpp = wpPartial \u27e6_\u27e7 _\u21d3_\n exwpp' : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n exwpp' = refl\n\n wppv1 : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n wppv1 = \u21d3Base\n\n wppd11 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n wppd11 = \u21d3Step \u21d3Base \u21d3Base\n wppd11' : Set\n wppd11' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n\n {- this type cannot be constructed\n wppd10 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n wppd10 = {!!}\n -}\n wppd10' : Set\n wppd10' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n\n {-\n wpPartial \u27e6_\u27e7 _\u21d3_ : a predicate on expressions\n - a way to express SUCCESSFUL evaluation of an Expr with intended semantics\n\n \u2200 exprs satisfying this predicate\n - monadic interpreter and\n - relational big-step specification\n must agree on result of evaluation\n\n for a given Expr, satisfaction of predicate means\n - Expr evaluates to a value by big-step operational semantics\n - if interpreter returns a value at all\n then that value agrees with big step semantics\n - if interpreter aborts\n then have a proof of \u22a5 (so, it does not abort)\n which will NOT agree with \u21d3 big-step semantics since \u21d3 does not allow div-by-0\n\n What does this say about correctness of interpreter?\n\n does not specify how many Expr that satisfy that characterization also satisfy wpPartial \u27e6_\u27e7 _\u21d3_\n\n to understand wpPartial \u27e6_\u27e7 _\u21d3_ better\n - define SafeDiv : safety criteria that we believe in\n - prove every expression that satisfies SafeDiv satisfies wpPartial \u27e6_\u27e7 _\u21d3_\n - SafeDiv refines wpPartial \u27e6_\u27e7 _\u21d3_\n - if we gave \u22a4 above, then 'correct' would not say that SafeDiv rules out undefined behavior\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val x) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n module SafeDivTest where\n exsdv3 : SafeDiv (Val 3) \u2261 \u22a4\n exsdv3 = refl\n exsdv0 : SafeDiv (Val 0) \u2261 \u22a4\n exsdv0 = refl\n exsdv0' : Set\n exsdv0' = SafeDiv (Val 0)\n\n exsd33 : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd33 = refl\n exsd33' : Set\n exsd33' = SafeDiv (Div (Val 3) (Val 3))\n\n exsd30 : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val Zero \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd30 = refl\n exsd30' : Set\n exsd30' = SafeDiv (Div (Val 3) (Val 0))\n\n {-\n Expect : any expr e for which SafeDiv e holds can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n correct (Val x) h = \u21d3Base\n correct (Div el er) (ernz , (sdel , sder))\n with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n ... | Pure _ | Pure Zero | _ | er\u21d3Zvr = magic (ernz er\u21d3Zvr)\n ... | Pure _ | Pure (Succ _) | el\u21d3vl | er\u21d3Svr = \u21d3Step el\u21d3vl er\u21d3Svr\n ... | Pure _ | Step Abort _ | _ | ()\n ... | Step Abort _ | _ | () | _\n\n {-\n Generalize above.\n Instead of manually defining SafeDiv,\n define more general predicate\n characterising the domain of a partial function using wpPartial\n -}\n\n -- HC: compare to 'mustPT'\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n --can show that the two semantics agree precisely on the domain of the interpreter:\n\n -- everything in domain of interpreter\n -- gets evaluated in agreement with big step semantics\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n -- only things interpreted in agreement with big step semantics\n -- are those that are in domain of interpreter\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on argument expression\n\n sound (Val x) _ = \u21d3Base\n sound (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div _ _) () | Pure _ | Pure Zero | _ | _\n sound (Div _ _) _ | Pure nl | Pure (Succ nr) | \u22a4'zero\u2192el\u21d3nl | \u22a4'zero\u2192er\u21d3Succnr\n = \u21d3Step (\u22a4'zero\u2192el\u21d3nl tt) (\u22a4'zero\u2192er\u21d3Succnr tt)\n sound (Div _ _) () | Pure _ | Step Abort _ | _ | _\n sound (Div _ _) () | Step Abort _ | _ | _ | _\n\n inDom : {v : Nat}\n -> (e : Expr)\n -> \u27e6 e \u27e7 == Pure v\n -> dom \u27e6_\u27e7 e\n inDom (Val _) _ = tt\n inDom (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div _ _) () | Pure _ | Pure Zero\n inDom (Div _ _) _ | Pure _ | Pure (Succ _) = tt\n inDom (Div _ _) () | Pure _ | Step Abort _\n inDom (Div _ _) () | Step Abort _ | _\n\n aux : (e : Expr) (v : Nat) -> \u27e6 e \u27e7 \u2261 Pure v -> e \u21d3 v\n aux e v eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} Diveler\u21d3_nldivSuccn\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 { _} { _} () | Pure _ | _ | Pure Zero\n wpPartial1 {el} { _} h | Pure nl | [[[ eq ]]] | Pure (Succ _) = aux el nl eq\n wpPartial1 { _} { _} () | Pure _ | _ | Step Abort _\n wpPartial1 { _} { _} () | Step Abort _ | _ | _\n\n wpPartial2 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} _ with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 { _} {er} _ | Pure _ | _ | Pure nr | [[[ eqnr ]]] = aux er nr eqnr\n wpPartial2 { _} { _} () | Pure _ | _ | Step Abort _ | _\n wpPartial2 { _} { _} () | Step Abort _ | _ | _ | _\n\n complete (Val x) h = tt\n complete (Div el er) h\n with \u27e6 el \u27e7\n | inspect \u27e6_\u27e7 el\n | \u27e6 er \u27e7\n | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div _ _) h | Pure nl | [[[ \u27e6el\u27e7\u2261Purenl ]]] | Pure Zero | [[[ \u27e6er\u27e7\u2261Pure0 ]]] | _ | _\n -- Goal\n -- mustPT (\u03bb _ _ \u2192 \u22a4' zero) (Div el er) (Pure nl >>= (\u03bb v1 \u2192 Pure Zero >>= _\u00f7_ v1))\n -- \u22a5\n -- Context\n -- h : mustPT _\u21d3_ (Div el er) (\u27e6 el \u27e7 >>= (\u03bb v1 \u2192 \u27e6 er \u27e7 >>= _\u00f7_ v1))\n rewrite\n \u27e6el\u27e7\u2261Purenl -- h : mustPT _\u21d3_ (Div el er) (\u27e6 er \u27e7 >>= _\u00f7_ nl)\n | \u27e6er\u27e7\u2261Pure0 -- h : \u22a5\n = magic h\n complete (Div _ _) _ | Pure _ | _ | Pure (Succ _) | _ | _ | _ = tt\n complete (Div _ _) _ | Pure _ | _ | Step Abort _ | _ | _ | ()\n complete (Div _ _) _ | Step Abort _ | _ | _ | _ | () | _\n\n module CompleteTest where\n {-\n xxx : mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n xxx = {!!}\n -}\n xxx' : Set\n xxx' = mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement to relate Kleisli morphisms,\n and to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n add top two elements; can fail fail if stack has too few elements\n\n below shows how to prove the definition meets its specification\n\n Define specification in terms of a pre\/post condition.\n -}\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n {-\n [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n for non-dependent examples (e.g., type b does not depend on x : a)\n -}\n\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function\n\n -- specification for addition function : describes the desired postcondition\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n\n {-\n How to relate this specification to an implementation?\n Note: 'wpPartial' assigns predicate transformer semantics to functions\n - but do not yet have a corresponding predicate transform semantics for specifications.\n wpSpec function does that:\n -}\n\n -- Given a specification, Spec a b\n -- computes the weakest precondition that satisfies an arbitrary postcondition P\n -- i.e., the spec\u2019s precondition should hold and its postcondition must imply P.\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\x -> (pre x) \u2227 (post x \u2286 P x)\n\n -- Can now formulate program\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs =\n pop xs >>= \\{(x1 , xs) ->\n pop xs >>= \\{(x2 , xs) ->\n return ((x1 + x2) :: xs)}}\n\n -- that refines the specification given by addSpec:\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd P Nil (() , _)\n correctnessAdd P (x :: Nil) (Data.Nat.s\u2264s () , _)\n correctnessAdd P (x :: y :: xs) (_ , H) = H (x + y :: xs) AddStep\n\n {-\n This example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition,\n - to its implementation.\n\n Compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n get : State s\n get = Step Get return\n\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n statePT : forall {l l'} -> {b : Set l} -> (b \u00d7 s -> Set l') -> State b -> (s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> (a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> (a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s') = (flatten t' == (seq (s) (size t))) \u2227 (s + size t == s')\n\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n correctnessRelabel : forall {a : Set} -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set} -> (c : State a) (f : a -> State b) ->\n \u2200 i P -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification,\n -- by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence,\n -- specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b} (mx : State a) (f : a -> State b) P i ->\n statePT (wpState f \\_ -> P) mx i -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b} (mx : State a) (f : a -> State b) spec ->\n \u2200 P i -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i) ->\n Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P)) ->\n statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s} -> SpecK {zero} (a \u00d7 s) (b \u00d7 s) -> a -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a) (trans\n (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr ->\n Pair (fl \u2261 seq s sl) (s + sl \u2261 s') ->\n Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'') ->\n Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s -> wpSpec relabelSpec P (t , s) -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set} ->\n ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set} -> (c : Free C R a) (f : a -> Free C R b) ->\n \u2200 P -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P =\n cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R} -> (a -> Free C R b) -> (b -> Free C R c) -> a -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set} -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c) ->\n wpCR f1 \u2291 wpCR f2 ->\n wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","old_contents":"module PredicateTransformers where\n\nopen import Prelude hiding (map; all)\nopen import Level hiding (lift)\n\n------------------------------------------------------------------------------\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\n------------------------------------------------------------------------------\nABSTRACT\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nrefinement type https:\/\/en.wikipedia.org\/wiki\/Refinement_(computing)\n- type with a predicate which must hold for any element of the refined type\n- can express\n - preconditions when used as function args\n - postconditions when used as return types\n\nthe problem statement is centered around \/compositionality\/\n- pure functions enable compositional reasoning\n- effectful programs require reasoning about context\n- paper provides mechanism for reasoning about effectful programs (and for program calculation)\n\nmain idea\n- represent effectful computation as a free monad\n- write an \"interpreter\" that computes a predicate transformer\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to use this refinement relation to\n - show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\nmodule Free where\n -- Free : represents syntax of an effectful computation\n -- C : type of commands\n -- Free C R : returns an 'a' or issues command c : C\n -- For each c : C, there is a set of responses R c\n -- - R : C -> Set : is type family of responses for commands\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\" input to output\n -- input\n -- - pure function 'a\u2192b : a -> b'\n -- - postcondition 'b -> Set' (on output of a\u2192b)\n -- output\n -- - weakest precondition 'a -> Set' on input to a\u2192b that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n module WP0Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data isEven : \u2115 \u2192 Set where\n even-z : isEven Nat.zero\n even-s : {n : \u2115} \u2192 isEven n \u2192 isEven (Nat.suc (Nat.suc n))\n\n isEvenPT : \u2115 -> Set\n isEvenPT = wp0 (_+ 2) isEven\n\n isEven5 : Set\n isEven5 = isEvenPT 5\n\n isEven5' : isEvenPT 5 \u2261 isEven 7\n isEven5' = refl\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - enable via making dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n -- every element a that satisfies P is contained in the set of things that satifies Q\n --\n -- implication between predicates\n --\n -- P a says a \u2208 P,\n -- if the extents of P, Q : A \u2192 Set is taken to be\n -- the subset of A satisfying predicates P, Q\n --\n -- the extent of P : A -> Set is the subset of terms of type A satisfying P.\n -- \"extent\" indicates a kind of black-box view of things,\n -- e.g., the \"extent\" of a function is the set of its input-output pairs.\n --\n -- https:\/\/en.wikipedia.org\/wiki\/Extension_%28semantics%29\n -- the extension of a concept, idea, or sign consists of the things to which it applies\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n -- refinement relation between PTs : pt1 refined by pt2\n --\n -- (same thing as \u2286, but one level up)\n -- for every post condition, pt2 weakens the precondition\n -- \"weakest\" is the right side : pt2 P\n --\n -- pt1 , pt2 : preconditions for satisfying a program\n -- refinement : pt1 maybe be a stronger requirement than pt2\n --\n -- pt2 is a refinement because it weakens what is required by pt1\n -- e.g., pt1 pt2\n -- >= 20 >= 10\n --\n -- weakening the input assumptions gives a stronger result\n --\n -- contract says establish a pre and I will give you a post, i.e.,\n -- \"stronger\" post condition\n -- or \"weaker\" pre condition\n --\n -- refinement of predicate transformers\n --\n -- use case: relate different implementations of the \"same program\"\n -- pt1 \u2291 pt2 : pt1 is refined by pt2\n -- - if pt1 is understood as giving preconditions which satisfy\n -- postcondition P for some given program f1\n -- then his says the precondition can be \/weakened\/ and still guarantee the postcondition\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g\n -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation to\n - relate PT semantics between programs and specifications\n - show a program satisfies its specification\n - show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n - corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n note : for =Free C R b= programs means generating the same AST\n\n instead, define predicate transformers that give \/interpretations\/\n to a particular choice of command type C and response family R\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public using (_+_; _>_; _*_)\n renaming (\u2115 to Nat; zero to Zero; suc to Succ)\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n -- one command\n data C : Set where\n Abort : C -- no continuation\n\n -- response code : the program does not execute further\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n -- BIG-STEP SEMANTICS specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n module \u21d3Test where\n exv : Expr\n exv = Val 3\n exd : Expr\n exd = Div (Val 3) (Val 0)\n\n exb0 : Val 0 \u21d3 0\n exb0 = \u21d3Base\n\n exb3 : Val 3 \u21d3 3\n exb3 = \u21d3Base\n\n exs331 : Div (Val 3) (Val 3) \u21d3 1\n exs331 = \u21d3Step \u21d3Base \u21d3Base\n\n exs931 : Div (Val 9) (Val 3) \u21d3 3\n exs931 = \u21d3Step \u21d3Base \u21d3Base\n\n exs9d31 : Div (Val 9) (Div (Val 3) (Val 1)) \u21d3 3\n exs9d31 = \u21d3Step \u21d3Base (\u21d3Step \u21d3Base \u21d3Base)\n\n exsd91d31 : Div (Div (Val 9) (Val 1)) (Div (Val 3) (Val 1)) \u21d3 3\n exsd91d31 = \u21d3Step (\u21d3Step \u21d3Base \u21d3Base) (\u21d3Step \u21d3Base \u21d3Base)\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val x \u27e7 = return x\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\v1 ->\n \u27e6 er \u27e7 >>= \\v2 ->\n v1 \u00f7 v2\n\n module InterpTest where\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n evv' : evv \u2261 Pure 3\n evv' = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n evd' : evd \u2261 Pure 1\n evd' = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n evd0' : evd0 \u2261 Step Abort (\u03bb ())\n evd0' = refl\n\n {-\n to relate big step semantics to monadic interpreter\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - _\u21d3_ relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n\n to call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results P' : Partial b -> Set\n done via 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so Abort case returns empty type\n -}\n\n -- convert post-condition for a pure function 'f : (x : a) -> b x'\n -- to post-condition on partial functions 'f : (x : A) -> Partial (b x)'\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba -- P holds if operation produces a value\n mustPT _ _ (Step Abort _) = \u22a5 -- if operation fails, there is no result to apply P to\n -- note: could give \u22a4 here, but want to rule out failure\n -- (total correctness)\n\n module MustPTTest where\n bs\u21d3 : Expr -> Nat -> Set\n bs\u21d3 = _\u21d3_\n\n bs\u21d3' : Set\n bs\u21d3' = Val 1 \u21d3 1\n\n bsp\u21d3 : Expr -> Partial Nat -> Set\n bsp\u21d3 = mustPT _\u21d3_\n\n bsp\u21d3' : Set\n bsp\u21d3' = mustPT _\u21d3_ (Val 1) (Pure 1)\n\n bsp\u21d3'x : mustPT _\u21d3_ (Val 1) (Pure 1)\n bsp\u21d3'x = \u21d3Base\n\n bsp\u21d3'' : Set\n bsp\u21d3'' = mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n\n {- nothing can be constructed with this type\n bsp\u21d3''x : mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n bsp\u21d3''x = {!!}\n -}\n xxx : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb v -> Pure 3 >>= _\u00f7 v))\n xxx = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx' : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n xxx' = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx'' : Set\n xxx'' = mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n module WpPartialTest where\n exwpp : Expr -> Set\n exwpp = wpPartial \u27e6_\u27e7 _\u21d3_\n exwpp' : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n exwpp' = refl\n\n wppv1 : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n wppv1 = \u21d3Base\n\n wppd11 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n wppd11 = \u21d3Step \u21d3Base \u21d3Base\n wppd11' : Set\n wppd11' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n\n {- this type cannot be constructed\n wppd10 : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n wppd10 = {!!}\n -}\n wppd10' : Set\n wppd10' = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n\n {-\n wpPartial \u27e6_\u27e7 _\u21d3_ : a predicate on expressions\n - a way to express SUCCESSFUL evaluation of an Expr with intended semantics\n\n \u2200 exprs satisfying this predicate\n - monadic interpreter and\n - relational big-step specification\n must agree on result of evaluation\n\n for a given Expr, satisfaction of predicate means\n - Expr evaluates to a value by big-step operational semantics\n - if interpreter returns a value at all\n then that value agrees with big step semantics\n - if interpreter aborts\n then have a proof of \u22a5 (so, it does not abort)\n which will NOT agree with \u21d3 big-step semantics since \u21d3 does not allow div-by-0\n\n What does this say about correctness of interpreter?\n\n does not specify how many Expr that satisfy that characterization also satisfy wpPartial \u27e6_\u27e7 _\u21d3_\n\n to understand wpPartial \u27e6_\u27e7 _\u21d3_ better\n - define SafeDiv : safety criteria that we believe in\n - prove every expression that satisfies SafeDiv satisfies wpPartial \u27e6_\u27e7 _\u21d3_\n - SafeDiv refines wpPartial \u27e6_\u27e7 _\u21d3_\n - if we gave \u22a4 above, then 'correct' would not say that SafeDiv rules out undefined behavior\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val x) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n module SafeDivTest where\n exsdv3 : SafeDiv (Val 3) \u2261 \u22a4\n exsdv3 = refl\n exsdv0 : SafeDiv (Val 0) \u2261 \u22a4\n exsdv0 = refl\n exsdv0' : Set\n exsdv0' = SafeDiv (Val 0)\n\n exsd33 : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd33 = refl\n exsd33' : Set\n exsd33' = SafeDiv (Div (Val 3) (Val 3))\n\n exsd30 : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val Zero \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsd30 = refl\n exsd30' : Set\n exsd30' = SafeDiv (Div (Val 3) (Val 0))\n\n {-\n Expect : any expr e for which SafeDiv e holds can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n correct (Val x) h = \u21d3Base\n correct (Div el er) ( _ , (sdel , sder))\n with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n correct (Div _ _) (ernz , ( _ , _)) | Pure _ | Pure Zero | _ | er\u21d3Zvr = magic (ernz er\u21d3Zvr)\n correct (Div _ _) ( _ , ( _ , _)) | Pure _ | Pure (Succ _) | el\u21d3vl | er\u21d3Svr = \u21d3Step el\u21d3vl er\u21d3Svr\n correct (Div _ _) ( _ , ( _ , _)) | Pure _ | Step Abort _ | _ | ()\n correct (Div _ _) ( _ , ( _ , _)) | Step Abort _ | _ | () | _\n\n {-\n Generalize above.\n Instead of manually defining SafeDiv,\n define more general predicate\n characterising the domain of a partial function using wpPartial\n -}\n\n -- HC: compare to 'mustPT'\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n --can show that the two semantics agree precisely on the domain of the interpreter:\n\n -- everything in domain of interpreter\n -- gets evaluated in agreement with big step semantics\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n -- only things interpreted in agreement with big step semantics\n -- are those that are in domain of interpreter\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on argument expression\n\n sound (Val x) _ = \u21d3Base\n sound (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div _ _) () | Pure _ | Pure Zero | _ | _\n sound (Div _ _) _ | Pure nl | Pure (Succ nr) | \u22a4'zero\u2192el\u21d3nl | \u22a4'zero\u2192er\u21d3Succnr\n = \u21d3Step (\u22a4'zero\u2192el\u21d3nl tt) (\u22a4'zero\u2192er\u21d3Succnr tt)\n sound (Div _ _) () | Pure _ | Step Abort _ | _ | _\n sound (Div _ _) () | Step Abort _ | _ | _ | _\n\n inDom : {v : Nat}\n -> (e : Expr)\n -> \u27e6 e \u27e7 == Pure v\n -> dom \u27e6_\u27e7 e\n inDom (Val _) _ = tt\n inDom (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div _ _) () | Pure _ | Pure Zero\n inDom (Div _ _) _ | Pure _ | Pure (Succ _) = tt\n inDom (Div _ _) () | Pure _ | Step Abort _\n inDom (Div _ _) () | Step Abort _ | _\n\n aux : (e : Expr) (v : Nat) -> \u27e6 e \u27e7 \u2261 Pure v -> e \u21d3 v\n aux e v eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} Diveler\u21d3_nldivSuccn\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 { _} { _} () | Pure _ | _ | Pure Zero\n wpPartial1 {el} { _} h | Pure nl | [[[ eq ]]] | Pure (Succ _) = aux el nl eq\n wpPartial1 { _} { _} () | Pure _ | _ | Step Abort _\n wpPartial1 { _} { _} () | Step Abort _ | _ | _\n\n wpPartial2 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} _ with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 { _} {er} _ | Pure _ | _ | Pure nr | [[[ eqnr ]]] = aux er nr eqnr\n wpPartial2 { _} { _} () | Pure _ | _ | Step Abort _ | _\n wpPartial2 { _} { _} () | Step Abort _ | _ | _ | _\n\n complete (Val x) h = tt\n complete (Div el er) h\n with \u27e6 el \u27e7\n | inspect \u27e6_\u27e7 el\n | \u27e6 er \u27e7\n | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div _ _) h | Pure nl | [[[ \u27e6el\u27e7\u2261Purenl ]]] | Pure Zero | [[[ \u27e6er\u27e7\u2261Pure0 ]]] | _ | _\n -- Goal\n -- mustPT (\u03bb _ _ \u2192 \u22a4' zero) (Div el er) (Pure nl >>= (\u03bb v1 \u2192 Pure Zero >>= _\u00f7_ v1))\n -- \u22a5\n -- Context\n -- h : mustPT _\u21d3_ (Div el er) (\u27e6 el \u27e7 >>= (\u03bb v1 \u2192 \u27e6 er \u27e7 >>= _\u00f7_ v1))\n rewrite\n \u27e6el\u27e7\u2261Purenl -- h : mustPT _\u21d3_ (Div el er) (\u27e6 er \u27e7 >>= _\u00f7_ nl)\n | \u27e6er\u27e7\u2261Pure0 -- h : \u22a5\n = magic h\n complete (Div _ _) _ | Pure _ | _ | Pure (Succ _) | _ | _ | _ = tt\n complete (Div _ _) _ | Pure _ | _ | Step Abort _ | _ | _ | ()\n complete (Div _ _) _ | Step Abort _ | _ | _ | _ | () | _\n\n module CompleteTest where\n {-\n xxx : mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n xxx = {!!}\n -}\n xxx' : Set\n xxx' = mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement to relate Kleisli morphisms,\n and to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n add top two elements; can fail fail if stack has too few elements\n\n below shows how to prove the definition meets its specification\n\n Define specification in terms of a pre\/post condition.\n -}\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n {-\n [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n for non-dependent examples (e.g., type b does not depend on x : a)\n -}\n\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function\n\n -- specification for addition function : describes the desired postcondition\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n\n {-\n How to relate this specification to an implementation?\n Note: 'wpPartial' assigns predicate transformer semantics to functions\n - but do not yet have a corresponding predicate transform semantics for specifications.\n wpSpec function does that:\n -}\n\n -- Given a specification, Spec a b\n -- computes the weakest precondition that satisfies an arbitrary postcondition P\n -- i.e., the spec\u2019s precondition should hold and its postcondition must imply P.\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\x -> (pre x) \u2227 (post x \u2286 P x)\n\n -- Can now formulate program\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs =\n pop xs >>= \\{(x1 , xs) ->\n pop xs >>= \\{(x2 , xs) ->\n return ((x1 + x2) :: xs)}}\n\n -- that refines the specification given by addSpec:\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd P Nil (() , _)\n correctnessAdd P (x :: Nil) (Data.Nat.s\u2264s () , _)\n correctnessAdd P (x :: y :: xs) (_ , H) = H (x + y :: xs) AddStep\n\n {-\n This example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition,\n - to its implementation.\n\n Compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n get : State s\n get = Step Get return\n\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n statePT : forall {l l'} -> {b : Set l} -> (b \u00d7 s -> Set l') -> State b -> (s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> (a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> (a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s') = (flatten t' == (seq (s) (size t))) \u2227 (s + size t == s')\n\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n correctnessRelabel : forall {a : Set} -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set} -> (c : State a) (f : a -> State b) ->\n \u2200 i P -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification,\n -- by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence,\n -- specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b} (mx : State a) (f : a -> State b) P i ->\n statePT (wpState f \\_ -> P) mx i -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b} (mx : State a) (f : a -> State b) spec ->\n \u2200 P i -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i) ->\n Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P)) ->\n statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s} -> SpecK {zero} (a \u00d7 s) (b \u00d7 s) -> a -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a) (trans\n (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr ->\n Pair (fl \u2261 seq s sl) (s + sl \u2261 s') ->\n Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'') ->\n Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s -> wpSpec relabelSpec P (t , s) -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set} ->\n ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set} -> (c : Free C R a) (f : a -> Free C R b) ->\n \u2200 P -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P =\n cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R} -> (a -> Free C R b) -> (b -> Free C R c) -> a -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set} -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c) ->\n wpCR f1 \u2291 wpCR f2 ->\n wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"2c0998148ca4d0f531c98144173c0d27ec548b8a","subject":"Update and extend ZK\/Schnorr","message":"Update and extend ZK\/Schnorr\n","repos":"crypto-agda\/crypto-agda","old_file":"ZK\/Schnorr.agda","new_file":"ZK\/Schnorr.agda","new_contents":"open import Type using (Type)\nopen import Data.Bool.Minimal using (Bool) renaming (T to \u2713)\nopen import Relation.Binary.PropositionalEquality.NP using (_\u2261_; idp; ap; ap\u2082; !_; module \u2261-Reasoning)\nopen import ZK.Types using (Cyclic-group; module Cyclic-group\n ; Cyclic-group-properties; module Cyclic-group-properties)\nimport ZK.SigmaProtocol\n\nmodule ZK.Schnorr\n {G \u2124q : Type}\n (cg : Cyclic-group G \u2124q)\n where\n open Cyclic-group cg\n\n Commitment = G\n Challenge = \u2124q\n Response = \u2124q\n\n open ZK.SigmaProtocol Commitment Challenge Response\n\n module _ (x a : \u2124q) where\n prover-commitment : Commitment\n prover-commitment = g ^ a\n\n prover-response : Challenge \u2192 Response\n prover-response c = a + (x * c)\n\n prover : Prover\n prover = prover-commitment , prover-response\n\n module _ (y : G) where\n verifier : Verifier\n verifier (mk A c s) = (g ^ s) == (A \u00b7 (y ^ c))\n\n -- This simulator shows why it is so important for the\n -- challenge to be picked once the commitment is known.\n\n -- To fake a transcript, the challenge and response can\n -- be arbitrarily chosen. However to be indistinguishable\n -- from a valid proof it they need to be picked at random.\n simulator : Simulator\n simulator c s = A\n where\n -- Compute A, such that the verifier accepts!\n A = (g ^ s) \/ (y ^ c)\n\n witness-extractor : Transcript\u00b2 verifier \u2192 \u2124q\n witness-extractor t\u00b2 = x\n module Witness-extractor where\n open Transcript\u00b2 t\u00b2\n fd = get-f\u2080 - get-f\u2081\n cd = get-c\u2080 - get-c\u2081\n x = fd * modinv cd\n\n extractor : \u2200 a \u2192 Extractor verifier\n extractor a t\u00b2 = prover (witness-extractor t\u00b2) a\n\n module Proofs (cg-props : Cyclic-group-properties cg) where\n open Cyclic-group-properties cg-props\n\n correct : \u2200 x a \u2192 Correct (prover x a) (let y = g ^ x in verifier y)\n correct x a c\n = \u2713-== (g ^(a + (x * c))\n \u2261\u27e8 ^-+ \u27e9\n g\u02b7 \u00b7 (g ^(x * c))\n \u2261\u27e8 ap (\u03bb z \u2192 g\u02b7 \u00b7 z) ^-* \u27e9\n g\u02b7 \u00b7 (y ^ c)\n \u220e)\n where\n open \u2261-Reasoning\n g\u02b7 = g ^ a\n y = g ^ x\n\n module _ (y : G) where\n shvzk : Special-Honest-Verifier-Zero-Knowledge (verifier y)\n shvzk = record { simulator = simulator y\n ; correct-simulator = \u03bb _ _ \u2192 \u2713-== \/-\u00b7 }\n\n module _ (x : \u2124q) (t\u00b2 : Transcript\u00b2 (verifier (g ^ x))) where\n private\n y = g ^ x\n x' = witness-extractor y t\u00b2\n\n open Transcript\u00b2 t\u00b2 renaming (get-A to A; get-c\u2080 to c\u2080; get-c\u2081 to c\u2081\n ;get-f\u2080 to f\u2080; get-f\u2081 to f\u2081)\n open Witness-extractor y t\u00b2 hiding (x)\n open \u2261-Reasoning\n\n g^xcd\u2261g^fd = g ^(x * cd)\n \u2261\u27e8 ^-* \u27e9\n y ^ (c\u2080 - c\u2081)\n \u2261\u27e8 ^-- \u27e9\n (y ^ c\u2080) \/ (y ^ c\u2081)\n \u2261\u27e8 ! cancels-\/ \u27e9\n (A \u00b7 (y ^ c\u2080)) \/ (A \u00b7 (y ^ c\u2081))\n \u2261\u27e8 ap\u2082 _\/_ (! ==-\u2713 verify\u2080) (! ==-\u2713 verify\u2081) \u27e9\n (g ^ f\u2080) \/ (g ^ f\u2081)\n \u2261\u27e8 ! ^-- \u27e9\n g ^ fd\n \u220e\n\n -- The extracted x is correct\n x\u2261x' : x \u2261 x'\n x\u2261x' = left-*-to-right-\/ (^-inj g^xcd\u2261g^fd)\n\n extractor-exact : \u2200 a \u2192 EqProver (extractor y a t\u00b2) (prover x a)\n extractor-exact a = idp , (\u03bb c \u2192 ap (\u03bb z \u2192 _+_ a (_*_ z c)) (! x\u2261x'))\n\n special-soundness : Special-Soundness (verifier y)\n special-soundness = record { extractor = extractor y a\n ; extractor-correct = extractor-correct }\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"module ZK.Schnorr where\n\nopen import Type\nopen import Data.Two\nopen import Relation.Binary.PropositionalEquality.NP\nimport ZK.Sigma-Protocol\n\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_\/_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_==_ : (x y : G) \u2192 \ud835\udfda)\n where\n\n Commitment = G\n Challenge = \u2124q\n Response = \u2124q\n\n open ZK.Sigma-Protocol Commitment Challenge Response\n\n module _ (x w : \u2124q) where\n prover-commitment : Commitment\n prover-commitment = g ^ w\n\n prover-response : Challenge \u2192 Response\n prover-response c = w + (x * c)\n\n prover : Prover\n prover = prover-commitment , prover-response\n\n module _ (y : G) where\n verifier : Verifier\n verifier (mk A c s) = (g ^ s) == (A \u00b7 (y ^ c))\n\n -- This simulator shows why it is so important for the\n -- challenge to be picked once the commitment is known.\n \n -- To fake a transcript, the challenge and response can\n -- be arbitrarily chosen. However to be indistinguishable\n -- from a valid proof it they need to be picked at random.\n module _ (c : Challenge) (s : Response) where\n -- Compute A, such that the verifier accepts!\n private\n A = (g ^ s) \/ (y ^ c)\n\n simulate-commitment : Commitment\n simulate-commitment = A\n\n simulate : Transcript\n simulate = mk simulate-commitment c s\n\n module Correct-simulation\n (\u2713-== : \u2200 {x y} \u2192 x \u2261 y \u2192 \u2713 (x == y))\n (\/-\u00b7 : \u2200 {P Q} \u2192 P \u2261 (P \/ Q) \u00b7 Q)\n where\n correct-simulation : \u2713(verifier simulate)\n correct-simulation = \u2713-== \/-\u00b7\n\n module Correctness-proof\n (\u2713-== : \u2200 {x y} \u2192 x \u2261 y \u2192 \u2713 (x == y))\n (^-+ : \u2200 {b x y} \u2192 b ^(x + y) \u2261 (b ^ x) \u00b7 (b ^ y))\n (^-* : \u2200 {b x y} \u2192 b ^(x * y) \u2261 (b ^ x) ^ y)\n (x r : \u2124q) where\n open \u2261-Reasoning\n g\u02b3 = g ^ r\n correctness : Correctness (prover x r) (verifier (g ^ x))\n correctness c = \u2713-== (g ^(r + (x * c))\n \u2261\u27e8 ^-+ \u27e9\n g\u02b3 \u00b7 (g ^(x * c))\n \u2261\u27e8 ap (\u03bb z \u2192 g\u02b3 \u00b7 z) ^-* \u27e9\n g\u02b3 \u00b7 ((g ^ x) ^ c)\n \u220e)\n\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"c0a1b912bb72e4c332092cdb7f26de8fcb7313fb","subject":"More robust definition for \u27e6\u2286-\u00f8\u27e7","message":"More robust definition for \u27e6\u2286-\u00f8\u27e7\n","repos":"np\/NomPa","old_file":"lib\/NomPa\/Implem\/LogicalRelation\/Internals.agda","new_file":"lib\/NomPa\/Implem\/LogicalRelation\/Internals.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nimport Level as L\nopen import Data.Nat.Logical\nopen import Data.Two hiding (_==_)\nopen import Data.Two.Logical\nopen import Data.Unit\nopen import Data.Empty\nopen import Data.Sum.NP\nopen import Data.Sum.Logical\nopen import Data.Maybe.NP\nopen import Data.List\nopen import Data.Product.NP\nopen import Relation.Nullary\nopen import Relation.Nullary.Negation using (contraposition)\nopen import Relation.Nullary.Decidable\nopen import Relation.Binary.NP as Bin\nopen import Relation.Binary.Logical\nopen import Function\nopen import Function.Equality using (_\u27e8$\u27e9_)\nopen import Function.Equivalence as \u21d4 using (_\u21d4_; equivalence; module Equivalence)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nimport Data.Nat.NP as \u2115\nopen \u2115 renaming (_==_ to _==\u2115_)\nimport Data.Nat.Properties as \u2115\nopen import NomPa.Record using (module NomPa)\nopen import NomPa.Implem\nimport NaPa\nopen NaPa using (\u2208-uniq)\nopen import NomPa.Worlds\nimport NomPa.Derived\n\nmodule NomPa.Implem.LogicalRelation.Internals where\n\nopen \u2261 using (_\u2261_; _\u2262_; \u27e6\u2261\u27e7)\nopen \u2261.\u2261-Reasoning\nopen NomPa.Implem.Internals\nopen NomPa NomPa.Implem.nomPa using (worldSym; Name\u00f8-elim; suc\u1d3a; suc\u1d3a\u2191)\n\n-- move to record\npred\u1d3a : \u2200 {\u03b1} \u2192 Name (\u03b1 +1) \u2192 Name \u03b1\npred\u1d3a = subtract\u1d3a 1\n\n\u2262\u2192\u2713-not-==\u2115 : \u2200 {x y} \u2192 x \u2262 y \u2192 \u2713 (not (x ==\u2115 y))\n\u2262\u2192\u2713-not-==\u2115 \u00acp = \u2713-\u00ac-not (\u00acp \u2218 \u2115.==.sound _ _)\n\n#\u21d2\u2209 : \u2200 {\u03b1 b} \u2192 b # \u03b1 \u2192 b \u2209 \u03b1\n#\u21d2\u2209 (_ #\u00f8) = id\n#\u21d2\u2209 (suc#\u2237 b#\u03b1) = #\u21d2\u2209 b#\u03b1\n#\u21d2\u2209 (0# _) = id\n\n#\u21d2\u2262 : \u2200 {\u03b1 b} \u2192 b # \u03b1 \u2192 (x : Name \u03b1) \u2192 binder\u1d3a x \u2262 b\n#\u21d2\u2262 b# (b , b\u2208) \u2261.refl = #\u21d2\u2209 b# b\u2208\n\nbinder\u1d3a-injective : \u2200 {\u03b1} {x y : Name \u03b1} \u2192 binder\u1d3a x \u2261 binder\u1d3a y \u2192 x \u2261 y\nbinder\u1d3a-injective {\u03b1} {_ , p\u2081} {a , p\u2082} eq rewrite eq = \u2261.cong (_,_ a) (\u2208-uniq \u03b1 p\u2081 p\u2082)\n\npf-irr\u2032 : \u2200 {\u03b1 \u03b2} (\u211b : Name \u03b1 \u2192 Name \u03b2 \u2192 Set) {x x\u2032 : Name \u03b1} {y y\u2032 : Name \u03b2}\n \u2192 binder\u1d3a x \u2261 binder\u1d3a x\u2032 \u2192 binder\u1d3a y \u2261 binder\u1d3a y\u2032 \u2192 \u211b x y \u2192 \u211b x\u2032 y\u2032\npf-irr\u2032 {\u03b1} {\u03b2} \u211b {x , x\u2208} {x\u2032 , x\u2032\u2208} {y , y\u2208} {y\u2032 , y\u2032\u2208} eq\u2081 eq\u2082 x\u1d63y\n rewrite eq\u2081 | eq\u2082 | \u2208-uniq \u03b1 x\u2208 x\u2032\u2208 | \u2208-uniq \u03b2 y\u2208 y\u2032\u2208 = x\u1d63y\n\npf-irr : \u2200 {\u03b1 \u03b2} (\u211b : Name \u03b1 \u2192 Name \u03b2 \u2192 Set) {x y x\u2208 x\u2208\u2032 y\u2208 y\u2208\u2032}\n \u2192 \u211b (x , x\u2208) (y , y\u2208) \u2192 \u211b (x , x\u2208\u2032) (y , y\u2208\u2032)\npf-irr \u211b = pf-irr\u2032 \u211b \u2261.refl \u2261.refl\n\nPreserve-\u2248 : \u2200 {a b \u2113 \u2113a \u2113b} {A : Set a} {B : Set b} \u2192 Rel A \u2113a \u2192 Rel (B) \u2113b \u2192 REL A (B) \u2113 \u2192 Set _\nPreserve-\u2248 _\u2248a_ _\u2248b_ _\u223c_ = \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u223c x\u2082 \u2192 y\u2081 \u223c y\u2082 \u2192 (x\u2081 \u2248a y\u2081) \u21d4 (x\u2082 \u2248b y\u2082)\n\n==\u2115\u21d4\u2261 : \u2200 {x y} \u2192 \u2713 (x ==\u2115 y) \u21d4 x \u2261 y\n==\u2115\u21d4\u2261 {x} {y} = equivalence (\u2115.==.sound _ _) \u2115.==.reflexive\n\n\u2261-on-name\u21d4\u2261 : \u2200 {\u03b1} {x y : Name \u03b1} \u2192 binder\u1d3a x \u2261 binder\u1d3a y \u21d4 x \u2261 y\n\u2261-on-name\u21d4\u2261 {\u03b1} {x} {y} = equivalence binder\u1d3a-injective (\u2261.cong binder\u1d3a)\n\nT\u21d4T\u2192\u2261 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 b\u2081 \u21d4 \u2713 b\u2082 \u2192 b\u2081 \u2261 b\u2082\nT\u21d4T\u2192\u2261 {1\u2082} {1\u2082} b\u2081\u21d4b\u2082 = \u2261.refl\nT\u21d4T\u2192\u2261 {1\u2082} {0\u2082} b\u2081\u21d4b\u2082 = \u22a5-elim (Equivalence.to b\u2081\u21d4b\u2082 \u27e8$\u27e9 _)\nT\u21d4T\u2192\u2261 {0\u2082} {1\u2082} b\u2081\u21d4b\u2082 = \u22a5-elim (Equivalence.from b\u2081\u21d4b\u2082 \u27e8$\u27e9 _)\nT\u21d4T\u2192\u2261 {0\u2082} {0\u2082} b\u2081\u21d4b\u2082 = \u2261.refl\n\n==\u1d3a\u21d4\u2261 : \u2200 {\u03b1} {x y : Name \u03b1} \u2192 \u2713 (x ==\u1d3a y) \u21d4 x \u2261 y\n==\u1d3a\u21d4\u2261 = \u2261-on-name\u21d4\u2261 \u27e8\u2218\u27e9 ==\u2115\u21d4\u2261 where open \u21d4 renaming (_\u2218_ to _\u27e8\u2218\u27e9_)\n\n-- Preserving the equalities also mean that the relation is functional and injective\nPreserve-\u2261 : \u2200 {a b \u2113} {A : Set a} {B : Set b} \u2192 REL A (B) \u2113 \u2192 Set _\nPreserve-\u2261 _\u223c_ = Preserve-\u2248 _\u2261_ _\u2261_ _\u223c_\n\nexport\u1d3a?-nothing : \u2200 {b \u03b1} (x : Name (b \u25c5 \u03b1)) \u2192 \u2713 (binder\u1d3a x ==\u2115 b) \u2192 export\u1d3a? {b} x \u2261 nothing\nexport\u1d3a?-nothing {b} {\u03b1} (x , x\u2208) p with x ==\u2115 b | export\u2208 \u03b1 x b\n... | 1\u2082 | _ = \u2261.refl\n... | 0\u2082 | _ = \u22a5-elim p\n\nexport\u1d3a?-just : \u2200 {\u03b1 b} (x : Name (b \u25c5 \u03b1)) {x\u2208} \u2192 \u2713 (not (binder\u1d3a x ==\u2115 b)) \u2192 export\u1d3a? {b} x \u2261 just (binder\u1d3a x , x\u2208)\nexport\u1d3a?-just {\u03b1} {b} (x , x\u2208) p with x ==\u2115 b | export\u2208 \u03b1 x b\n... | 1\u2082 | _ = \u22a5-elim p\n... | 0\u2082 | _ = \u2261.cong just (binder\u1d3a-injective \u2261.refl)\n\nrecord \u27e6World\u27e7 (\u03b1\u2081 \u03b1\u2082 : World) : Set\u2081 where\n constructor _,_\n field\n _\u223c_ : Name \u03b1\u2081 \u2192 Name \u03b1\u2082 \u2192 Set\n \u223c-pres-\u2261 : Preserve-\u2261 _\u223c_\n\n \u223c-inj : \u2200 {x y z} \u2192 x \u223c z \u2192 y \u223c z \u2192 x \u2261 y\n \u223c-inj p q = Equivalence.from (\u223c-pres-\u2261 p q) \u27e8$\u27e9 \u2261.refl\n \u223c-fun : \u2200 {x y z} \u2192 x \u223c y \u2192 x \u223c z \u2192 y \u2261 z\n \u223c-fun p q = Equivalence.to (\u223c-pres-\u2261 p q) \u27e8$\u27e9 \u2261.refl\n\n \u223c-\u2208-uniq : \u2200 {x\u2081 x\u2082} {x\u2081\u2208\u2032 x\u2082\u2208\u2032} \u2192\n x\u2081 \u223c x\u2082 \u2261 (binder\u1d3a x\u2081 , x\u2081\u2208\u2032) \u223c (binder\u1d3a x\u2082 , x\u2082\u2208\u2032)\n \u223c-\u2208-uniq {_ , x\u2081\u2208} {_ , x\u2082\u2208} {x\u2081\u2208\u2032} {x\u2082\u2208\u2032} = \u2261.ap\u2082 (\u03bb x\u2081\u2208 x\u2082\u2208 \u2192 (_ , x\u2081\u2208) \u223c (_ , x\u2082\u2208)) (\u2208-uniq \u03b1\u2081 x\u2081\u2208 x\u2081\u2208\u2032) (\u2208-uniq \u03b1\u2082 x\u2082\u2208 x\u2082\u2208\u2032)\n\n\u27e6sym\u27e7 : \u2200 {\u03b1\u2081 \u03b1\u2082} \u2192 \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082 \u2192 \u27e6World\u27e7 \u03b1\u2082 \u03b1\u2081\n\u27e6sym\u27e7 (\u211b , \u211b-pres-\u2261) = \u211b-sym , \u211b-sym-pres-\u2261\n where \u211b-sym = flip \u211b\n \u211b-sym-pres-\u2261 : Preserve-\u2261 \u211b-sym\n \u211b-sym-pres-\u2261 p q = \u21d4.sym (\u211b-pres-\u2261 p q)\n\nrecord \u27e6Binder\u27e7 (b\u2081 b\u2082 : Binder) : Set where\n\nmodule \u27e6\u25c5\u27e7 (b\u2081 b\u2082 : Binder) {\u03b1\u2081 \u03b1\u2082} (\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082) where\n open \u27e6World\u27e7 \u03b1\u1d63\n data _\u211b_ (x : Name (b\u2081 \u25c5 \u03b1\u2081)) (y : Name (b\u2082 \u25c5 \u03b1\u2082)) : Set where\n here : (x\u2261b\u2081 : binder\u1d3a x \u2261 b\u2081) (y\u2261b\u2082 : binder\u1d3a y \u2261 b\u2082) \u2192 x \u211b y\n there : \u2200 (x\u2262b\u2081 : binder\u1d3a x \u2262 b\u2081) (y\u2262b\u2082 : binder\u1d3a y \u2262 b\u2082) {x\u2208 y\u2208} (x\u223cy : (binder\u1d3a x , x\u2208) \u223c (binder\u1d3a y , y\u2208)) \u2192 x \u211b y\n\n \u211b-inj : \u2200 {x y z} \u2192 x \u211b z \u2192 y \u211b z \u2192 x \u2261 y\n \u211b-inj (here x\u2261b\u2081 _) (here y\u2261b\u2081 _) = binder\u1d3a-injective (\u2261.trans x\u2261b\u2081 (\u2261.sym y\u2261b\u2081))\n \u211b-inj (here _ z\u2261b\u2082) (there _ z\u2262b\u2082 _) = \u22a5-elim (z\u2262b\u2082 z\u2261b\u2082)\n \u211b-inj (there _ z\u2262b\u2082 _) (here _ z\u2261b\u2082) = \u22a5-elim (z\u2262b\u2082 z\u2261b\u2082)\n \u211b-inj {x} {y} {z} (there x\u2262b\u2081 z\u2262b\u2082 {x\u2208} {z\u2208} x\u223cz) (there y\u2262b\u2081 _ {y\u2208} {z\u2208\u2032} y\u223cz) =\n binder\u1d3a-injective (\u2261.cong binder\u1d3a (\u223c-inj x\u223cz (\u2261.tr (\u03bb z\u2208 \u2192 (binder\u1d3a y , y\u2208) \u223c (binder\u1d3a z , z\u2208)) (\u2208-uniq \u03b1\u2082 z\u2208\u2032 z\u2208) y\u223cz )))\n\n \u211b-fun : \u2200 {x y z} \u2192 x \u211b y \u2192 x \u211b z \u2192 y \u2261 z\n \u211b-fun (here _ y\u2261b\u2082) (here _ z\u2261b\u2082) = binder\u1d3a-injective (\u2261.trans y\u2261b\u2082 (\u2261.sym z\u2261b\u2082))\n \u211b-fun (here p _) (there \u00acp _ _) = \u22a5-elim (\u00acp p)\n \u211b-fun (there \u00acp _ _) (here p _) = \u22a5-elim (\u00acp p)\n \u211b-fun {x} {y} {z} (there x\u2262b\u2081 y\u2262b\u2082 {x\u2208} {y\u2208} x\u223cy) (there p q {x\u2208\u2032} {z\u2208} x\u223cz) =\n binder\u1d3a-injective (\u2261.cong binder\u1d3a (\u223c-fun x\u223cy (\u2261.tr (\u03bb x\u2208 \u2192 (binder\u1d3a x , x\u2208) \u223c (binder\u1d3a z , z\u2208)) (\u2208-uniq \u03b1\u2081 x\u2208\u2032 x\u2208) x\u223cz )))\n\n \u211b-pres-\u2261 : Preserve-\u2261 _\u211b_\n \u211b-pres-\u2261 {x\u2081} {x\u2082} {y\u2081} {y\u2082} x\u2081\u223cx\u2082 y\u2081\u223cy\u2082 =\n equivalence (\u03bb x\u2081=y\u2081 \u2192 \u211b-fun {y\u2081} (\u2261.tr (\u03bb x\u2081 \u2192 x\u2081 \u211b x\u2082) x\u2081=y\u2081 x\u2081\u223cx\u2082) y\u2081\u223cy\u2082)\n (\u03bb x\u2082=y\u2082 \u2192 \u211b-inj x\u2081\u223cx\u2082 (\u2261.tr (\u03bb y\u2082 \u2192 y\u2081 \u211b y\u2082) (\u2261.sym x\u2082=y\u2082) y\u2081\u223cy\u2082))\n\n \u27e6world\u27e7 : \u27e6World\u27e7 (b\u2081 \u25c5 \u03b1\u2081) (b\u2082 \u25c5 \u03b1\u2082)\n \u27e6world\u27e7 = _\u211b_ , \u211b-pres-\u2261\n\nhere\u2032 : \u2200 {\u03b1\u2081 \u03b1\u2082 b\u2081 b\u2082 pf\u2081 pf\u2082} {\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082} \u2192 \u27e6\u25c5\u27e7._\u211b_ b\u2081 b\u2082 \u03b1\u1d63 (b\u2081 , pf\u2081) (b\u2082 , pf\u2082)\nhere\u2032 = \u27e6\u25c5\u27e7.here \u2261.refl \u2261.refl\n\n_\u27e6\u25c5\u27e7_ : (\u27e6Binder\u27e7 \u27e6\u2192\u27e7 \u27e6World\u27e7 \u27e6\u2192\u27e7 \u27e6World\u27e7) _\u25c5_ _\u25c5_\n_\u27e6\u25c5\u27e7_ {b\u2081} {b\u2082} _ \u03b1\u1d63 = \u27e6\u25c5\u27e7.\u27e6world\u27e7 b\u2081 b\u2082 \u03b1\u1d63\n\n\u27e6Name\u27e7 : \u27e6Pred\u27e7 L.zero \u27e6World\u27e7 Name Name\n\u27e6Name\u27e7 (\u211b , _) x\u2081 x\u2082 = \u211b x\u2081 x\u2082\n\n\u27e6zero\u1d2e\u27e7 : \u27e6Binder\u27e7 zero\u1d2e zero\u1d2e\n\u27e6zero\u1d2e\u27e7 = _\n\n\u27e6suc\u1d2e\u27e7 : (\u27e6Binder\u27e7 \u27e6\u2192\u27e7 \u27e6Binder\u27e7) suc\u1d2e suc\u1d2e\n\u27e6suc\u1d2e\u27e7 = _\n\n\u27e6\u00f8\u27e7 : \u27e6World\u27e7 \u00f8 \u00f8\n\u27e6\u00f8\u27e7 = (\u03bb _ _ \u2192 \u22a5) , (\u03bb())\n\n_\u27e6==\u1d3a\u27e7_ : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) _==\u1d3a_ _==\u1d3a_\n_\u27e6==\u1d3a\u27e7_ \u03b1\u1d63 x\u1d63 y\u1d63 =\n \u27e6\ud835\udfda\u27e7-Props.reflexive (T\u21d4T\u2192\u2261 (sym ==\u1d3a\u21d4\u2261 \u27e8\u2218\u27e9 \u223c-pres-\u2261 x\u1d63 y\u1d63 \u27e8\u2218\u27e9 ==\u1d3a\u21d4\u2261)) where\n open \u27e6World\u27e7 \u03b1\u1d63\n open \u21d4 using (sym) renaming (_\u2218_ to _\u27e8\u2218\u27e9_)\n\n\u27e6name\u1d2e\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63)) name\u1d2e name\u1d2e\n\u27e6name\u1d2e\u27e7 _ _ = here\u2032\n\n\u27e6binder\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Binder\u27e7) binder\u1d3a binder\u1d3a\n\u27e6binder\u1d3a\u27e7 _ _ = _\n\n\u27e6nothing\u27e7\u2032 : \u2200 {a} {A\u2081 A\u2082 : Set a} {A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set} {x : Maybe A\u2081} {y : Maybe A\u2082}\n \u2192 x \u2261 nothing \u2192 y \u2261 nothing \u2192 \u27e6Maybe\u27e7 A\u1d63 x y\n\u27e6nothing\u27e7\u2032 \u2261.refl \u2261.refl = \u27e6nothing\u27e7\n\n\u27e6just\u27e7\u2032 : \u2200 {a} {A\u2081 A\u2082 : Set a} {A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set} {x : Maybe A\u2081} {y : Maybe A\u2082} {x\u2032 y\u2032}\n \u2192 x \u2261 just x\u2032 \u2192 y \u2261 just y\u2032 \u2192 A\u1d63 x\u2032 y\u2032 \u2192 \u27e6Maybe\u27e7 A\u1d63 x y\n\u27e6just\u27e7\u2032 \u2261.refl \u2261.refl = \u27e6just\u27e7\n\n\u27e6export\u1d3a?\u27e7 : (\u2200\u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) \u27e6\u2192\u27e7 \u27e6Maybe\u27e7 (\u27e6Name\u27e7 \u03b1\u1d63)) (\u03bb {b} \u2192 export\u1d3a? {b}) (\u03bb {b} \u2192 export\u1d3a? {b})\n\u27e6export\u1d3a?\u27e7 _ \u03b1\u1d63 {x\u2081} {x\u2082} (\u27e6\u25c5\u27e7.here e\u2081 e\u2082)\n = \u27e6nothing\u27e7\u2032 (export\u1d3a?-nothing x\u2081 (\u2115.==.reflexive e\u2081)) (export\u1d3a?-nothing x\u2082 (\u2115.==.reflexive e\u2082))\n\u27e6export\u1d3a?\u27e7 _ \u03b1\u1d63 {x\u2081} {x\u2082} (\u27e6\u25c5\u27e7.there x\u2081\u2262b\u2081 x\u2082\u2262b\u2082 x\u2081\u223cx\u2082)\n = \u27e6just\u27e7\u2032 (export\u1d3a?-just x\u2081 (\u2262\u2192\u2713-not-==\u2115 x\u2081\u2262b\u2081))\n (export\u1d3a?-just x\u2082 (\u2262\u2192\u2713-not-==\u2115 x\u2082\u2262b\u2082)) x\u2081\u223cx\u2082\n\n_\u27e6\u2286\u27e7b_ : \u27e6Rel\u27e7 \u27e6World\u27e7 L.zero _\u2286_ _\u2286_\n_\u27e6\u2286\u27e7b_ \u03b1\u1d63 \u03b2\u1d63 x y = (\u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b2\u1d63) (coerce\u1d3a x) (coerce\u1d3a y)\n\ndata _\u27e6\u2286\u27e7_ {\u03b1\u2081 \u03b1\u2082} (\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082) {\u03b2\u2081 \u03b2\u2082} (\u03b2\u1d63 : \u27e6World\u27e7 \u03b2\u2081 \u03b2\u2082) (x : \u03b1\u2081 \u2286 \u03b2\u2081) (y : \u03b1\u2082 \u2286 \u03b2\u2082) : Set where\n mk : _\u27e6\u2286\u27e7b_ \u03b1\u1d63 \u03b2\u1d63 x y \u2192 _\u27e6\u2286\u27e7_ \u03b1\u1d63 \u03b2\u1d63 x y\n\nun\u27e6\u2286\u27e7 : \u2200 {\u03b1\u2081 \u03b1\u2082} {\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082} {\u03b2\u2081 \u03b2\u2082} {\u03b2\u1d63 : \u27e6World\u27e7 \u03b2\u2081 \u03b2\u2082} {x y} \u2192 (\u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63) x y \u2192 (\u03b1\u1d63 \u27e6\u2286\u27e7b \u03b2\u1d63) x y\nun\u27e6\u2286\u27e7 (mk x) {a} {b} c = x {a} {b} c\n\n\u27e6\u2286-refl\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b1\u1d63) \u2286-refl \u2286-refl\n\u27e6\u2286-refl\u27e7 _ = mk (\u03bb x\u1d63 \u2192 x\u1d63)\n\n\u27e6\u2286-trans\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b3\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7\n \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63 \u27e6\u2192\u27e7 (\u03b2\u1d63 \u27e6\u2286\u27e7 \u03b3\u1d63 \u27e6\u2192\u27e7 (\u03b1\u1d63 \u27e6\u2286\u27e7 \u03b3\u1d63))) \u2286-trans \u2286-trans\n\u27e6\u2286-trans\u27e7 _ _ _ {mk f\u2081} {mk f\u2082} f {mk g\u2081} {mk g\u2082} g\n = mk (\u03bb {x\u2081} {x\u2082} x\u1d63 \u2192 un\u27e6\u2286\u27e7 g {coerce\u1d3a (mk f\u2081) x\u2081} {coerce\u1d3a (mk f\u2082) x\u2082} (un\u27e6\u2286\u27e7 f {x\u2081} {x\u2082} x\u1d63))\n\n\u27e6coerce\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63 \u27e6\u2192\u27e7 (\u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b2\u1d63)) coerce\u1d3a coerce\u1d3a\n\u27e6coerce\u1d3a\u27e7 _ _ \u03b1\u2286\u03b2\u1d63 {x\u2081} {x\u2082} x\u1d63 = un\u27e6\u2286\u27e7 \u03b1\u2286\u03b2\u1d63 {x\u2081} {x\u2082} x\u1d63\n\n\u27e6\u00acName\u00f8\u27e7 : (\u27e6\u00ac\u27e7(\u27e6Name\u27e7 \u27e6\u00f8\u27e7)) \u00acName\u00f8 \u00acName\u00f8\n\u27e6\u00acName\u00f8\u27e7 ()\n\n\u27e6\u2286-\u00f8\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6\u00f8\u27e7 \u27e6\u2286\u27e7 \u03b1\u1d63) \u2286-\u00f8 \u2286-\u00f8\n\u27e6\u2286-\u00f8\u27e7 \u03b1\u1d63 = mk \u03bb { {_ , ()} }\n\n_\u27e6#\u27e7_ : (\u27e6Binder\u27e7 \u27e6\u2192\u27e7 \u27e6World\u27e7 \u27e6\u2192\u27e7 \u27e6Set\u2080\u27e7) _#_ _#_\n_\u27e6#\u27e7_ _ _ _ _ = \u22a4\n\n_\u27e6#\u00f8\u27e7 : (\u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7 b\u1d63 \u27e6#\u27e7 \u27e6\u00f8\u27e7) _#\u00f8 _#\u00f8\n_\u27e6#\u00f8\u27e7 _ = _\n\n\u27e6suc#\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7 b\u1d63 \u27e6#\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 (\u27e6suc\u1d2e\u27e7 b\u1d63) \u27e6#\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63)) suc# suc#\n\u27e6suc#\u27e7 _ _ _ = _\n\n\u27e6\u2286-#\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7\n (b\u1d63 \u27e6#\u27e7 \u03b1\u1d63) \u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63)) \u2286-# \u2286-#\n\u27e6\u2286-#\u27e7 _ {b\u2081} {b\u2082} _ {b\u2081#\u03b1\u2081} {b\u2082#\u03b1\u2082} _ = mk (\u03bb {x\u2081} {x\u2082} \u2192 \u27e6\u25c5\u27e7.there (#\u21d2\u2262 b\u2081#\u03b1\u2081 x\u2081) (#\u21d2\u2262 b\u2082#\u03b1\u2082 x\u2082))\n\n\u27e6\u2286-\u25c5\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63 \u27e6\u2192\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) \u27e6\u2286\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b2\u1d63)) \u2286-\u25c5 \u2286-\u25c5\n\u27e6\u2286-\u25c5\u27e7 {\u03b1\u2081} {\u03b1\u2082} \u03b1\u1d63 {\u03b2\u2081} {\u03b2\u2082} \u03b2\u1d63 {b\u2081} {b\u2082} _ {\u03b1\u2286\u03b2\u2081} {\u03b1\u2286\u03b2\u2082} (mk f) = mk g where\n open \u27e6\u25c5\u27e7 b\u2081 b\u2082\n g : \u2200 {x\u2081 x\u2082} \u2192 _\u211b_ \u03b1\u1d63 x\u2081 x\u2082 \u2192 _\u211b_ \u03b2\u1d63 (coerce\u1d3a (\u2286-\u25c5 b\u2081 \u03b1\u2286\u03b2\u2081) x\u2081) (coerce\u1d3a (\u2286-\u25c5 b\u2082 \u03b1\u2286\u03b2\u2082) x\u2082)\n g (here x\u2261b\u2081 y\u2261b\u2082) = here x\u2261b\u2081 y\u2261b\u2082\n g (there x\u2262b\u2081 y\u2262b\u2082 x\u223cy) = there x\u2262b\u2081 y\u2262b\u2082 (f x\u223cy)\n\ncong\u2115 : \u2200 {\u03b1 \u03b2 : World} {x y : Name \u03b1} (f : \u2115 \u2192 \u2115) {fx\u2208 fy\u2208} \u2192 x \u2261 y \u2192 _\u2261_ {A = Name \u03b2} (f (binder\u1d3a x) , fx\u2208) (f (binder\u1d3a y) , fy\u2208)\ncong\u2115 f = binder\u1d3a-injective \u2218 \u2261.cong (f \u2218 binder\u1d3a)\n\nmodule \u27e6+1\u27e7 {\u03b1\u2081 \u03b1\u2082} (\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082) where\n open \u27e6World\u27e7 \u03b1\u1d63 renaming (_\u223c_ to \u211b; \u223c-inj to \u211b-inj; \u223c-fun to \u211b-fun)\n \u211b+1 : Name (\u03b1\u2081 +1) \u2192 Name (\u03b1\u2082 +1) \u2192 Set\n \u211b+1 x y = \u27e6Name\u27e7 \u03b1\u1d63 (pred\u1d3a x) (pred\u1d3a y)\n{-\n data \u211b+1 : Name (\u03b1\u2081 +1) \u2192 Name (\u03b1\u2082 +1) \u2192 Set where\n suc : \u2200 {x x\u2208 y y\u2208} \u2192 \u27e6Name\u27e7 \u03b1\u1d63 x y \u2192 \u211b+1 (suc x , ) (sucN y)\n data \u211b+1 : Name (\u03b1\u2081 +1) \u2192 Name (\u03b1\u2082 +1) \u2192 Set where\n suc : \u2200 {x y} \u2192 \u27e6Name\u27e7 \u03b1\u1d63 x y \u2192 \u211b+1 (sucN x) (sucN y)\n-}\n\n \u211b+1-inj : \u2200 {x y z} \u2192 \u211b+1 x z \u2192 \u211b+1 y z \u2192 x \u2261 y\n -- \u211b+1-inj {suc x , _} {suc y , _} {suc z , _} (suc a) (suc b) = {!!}\n \u211b+1-inj {suc x , _} {suc y , _} {suc z , _} a b = cong\u2115 suc (\u211b-inj a b)\n \u211b+1-inj {zero , ()} _ _\n \u211b+1-inj {_} {zero , ()} _ _\n \u211b+1-inj {_} {_} {zero , ()} _ _\n \u211b+1-fun : \u2200 {x y z} \u2192 \u211b+1 x y \u2192 \u211b+1 x z \u2192 y \u2261 z\n \u211b+1-fun {suc x , _} {suc y , _} {suc z , _} a b = cong\u2115 suc (\u211b-fun a b)\n \u211b+1-fun {zero , ()} _ _\n \u211b+1-fun {_} {zero , ()} _ _\n \u211b+1-fun {_} {_} {zero , ()} _ _\n\n \u211b+1-pres-\u2261 : Preserve-\u2261 \u211b+1\n -- \u211b+1-pres-\u2261 x y = equivalence {!\u211b+1-inj!} {!!}\n \u211b+1-pres-\u2261 {x\u2081} {x\u2082} {y\u2081} {y\u2082} x\u2081\u223cx\u2082 y\u2081\u223cy\u2082 =\n -- factor this move\n equivalence (\u03bb x\u2081=y\u2081 \u2192 \u211b+1-fun {y\u2081} (\u2261.tr (\u03bb x\u2081 \u2192 \u211b+1 x\u2081 x\u2082) x\u2081=y\u2081 x\u2081\u223cx\u2082) y\u2081\u223cy\u2082)\n (\u03bb x\u2082=y\u2082 \u2192 \u211b+1-inj {x\u2081} {y\u2081} {x\u2082} x\u2081\u223cx\u2082 (\u2261.tr (\u03bb y\u2082 \u2192 \u211b+1 y\u2081 y\u2082) (\u2261.sym x\u2082=y\u2082) y\u2081\u223cy\u2082))\n\n\n_\u27e6+1\u27e7 : (\u27e6World\u27e7 \u27e6\u2192\u27e7 \u27e6World\u27e7) _+1 _+1\n_\u27e6+1\u27e7 \u03b1\u1d63 = \u211b+1 , \u211b+1-pres-\u2261 where open \u27e6+1\u27e7 \u03b1\u1d63\n\nopen WorldOps worldSym\n\n_\u27e6\u21911\u27e7 : (\u27e6World\u27e7 \u27e6\u2192\u27e7 \u27e6World\u27e7) _\u21911 _\u21911\n_\u27e6\u21911\u27e7 \u03b1\u1d63 = \u27e6zero\u1d2e\u27e7 \u27e6\u25c5\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7)\n\n_\u27e6\u2191\u27e7_ : (\u27e6World\u27e7 \u27e6\u2192\u27e7 \u27e6\u2115\u27e7 \u27e6\u2192\u27e7 \u27e6World\u27e7) _\u2191_ _\u2191_\n_\u27e6\u2191\u27e7_ \u03b1\u1d63 zero = \u03b1\u1d63\n_\u27e6\u2191\u27e7_ \u03b1\u1d63 (suc k) = (\u03b1\u1d63 \u27e6\u2191\u27e7 k) \u27e6\u21911\u27e7\n\n_\u27e6+\u1d42\u27e7_ : (\u27e6World\u27e7 \u27e6\u2192\u27e7 \u27e6\u2115\u27e7 \u27e6\u2192\u27e7 \u27e6World\u27e7) _+\u1d42_ _+\u1d42_\n_\u27e6+\u1d42\u27e7_ \u03b1\u1d63 zero = \u03b1\u1d63\n_\u27e6+\u1d42\u27e7_ \u03b1\u1d63 (suc k) = (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k) \u27e6+1\u27e7\n\n\u27e6pred\u1d3a\u27e7-suc\u1d3a\u2191 : \u2200 {\u03b1\u2081 \u03b1\u2082} (\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082)\n {x\u2081 x\u2082} (x\u1d63 : \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7) (suc\u1d3a\u2191 x\u2081) (suc\u1d3a\u2191 x\u2082)) \u2192 \u27e6Name\u27e7 \u03b1\u1d63 x\u2081 x\u2082\n\u27e6pred\u1d3a\u27e7-suc\u1d3a\u2191 _ (\u27e6\u25c5\u27e7.here () _)\n\u27e6pred\u1d3a\u27e7-suc\u1d3a\u2191 \u03b1\u1d63 (\u27e6\u25c5\u27e7.there _ _ x\u223cy) = pf-irr (\u27e6Name\u27e7 \u03b1\u1d63) x\u223cy\n\n\u27e6\u00f8+1\u2286\u00f8\u27e7 : (\u27e6\u00f8\u27e7 \u27e6+1\u27e7 \u27e6\u2286\u27e7 \u27e6\u00f8\u27e7) \u00f8+1\u2286\u00f8 \u00f8+1\u2286\u00f8\n\u27e6\u00f8+1\u2286\u00f8\u27e7 = mk (\u03bb {x} _ \u2192 Name\u00f8-elim (pred\u1d3a x))\n\n\u27e6\u2286-cong-+1\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63 \u27e6\u2192\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7) \u27e6\u2286\u27e7 (\u03b2\u1d63 \u27e6+1\u27e7)) \u2286-cong-+1 \u2286-cong-+1\n\u27e6\u2286-cong-+1\u27e7 _ \u03b2\u1d63 \u03b1\u2286\u03b2\u1d63 = mk (pf-irr (\u27e6Name\u27e7 \u03b2\u1d63) \u2218 un\u27e6\u2286\u27e7 \u03b1\u2286\u03b2\u1d63)\n\n\u27e6\u2286-cong-\u21911\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63 \u27e6\u2192\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7) \u27e6\u2286\u27e7 (\u03b2\u1d63 \u27e6\u21911\u27e7)) \u2286-cong-\u21911 \u2286-cong-\u21911\n\u27e6\u2286-cong-\u21911\u27e7 \u03b1\u1d63 \u03b2\u1d63 {\u03b1\u2286\u03b2\u2081} {\u03b1\u2286\u03b2\u2082} \u03b1\u2286\u03b2\u1d63 = mk helper where\n open \u27e6\u25c5\u27e7 0 0\n helper : (\u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7) \u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u03b2\u1d63 \u27e6\u21911\u27e7)) (coerce\u1d3a (\u2286-cong-\u21911 \u03b1\u2286\u03b2\u2081)) (coerce\u1d3a (\u2286-cong-\u21911 \u03b1\u2286\u03b2\u2082))\n helper (here x\u2261b\u2081 y\u2261b\u2082) = here x\u2261b\u2081 y\u2261b\u2082\n helper {x\u2081 , _} {x\u2082 , _} (there x\u2262b\u2081 y\u2262b\u2082 {p\u2081} {p\u2082} x\u223cy)\n = there x\u2262b\u2081 y\u2262b\u2082 {coe (\u2286-cong-+1 \u03b1\u2286\u03b2\u2081) x\u2081 p\u2081}\n {coe (\u2286-cong-+1 \u03b1\u2286\u03b2\u2082) x\u2082 p\u2082}\n (pf-irr (\u27e6Name\u27e7 \u03b2\u1d63) (un\u27e6\u2286\u27e7 \u03b1\u2286\u03b2\u1d63 x\u223cy))\n\n\u27e6\u2286-+1-inj\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7) \u27e6\u2286\u27e7 (\u03b2\u1d63 \u27e6+1\u27e7) \u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63) \u2286-+1-inj \u2286-+1-inj\n\u27e6\u2286-+1-inj\u27e7 _ _ \u03b1+\u2286\u03b2+\u1d63 = mk (\u03bb {x\u2081} {x\u2082} \u2192 un\u27e6\u2286\u27e7 \u03b1+\u2286\u03b2+\u1d63 {add\u1d3a 1 x\u2081} {add\u1d3a 1 x\u2082}) \n\n\u22620 : \u2200 {\u03b1} (x : Name (\u03b1 +1)) \u2192 binder\u1d3a x \u2262 0\n\u22620 (0 , ()) \u2261.refl\n\u22620 (suc _ , _) ()\n\n\u27e6\u2286-+1\u21911\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7) \u27e6\u2286\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7)) \u2286-+1\u21911 \u2286-+1\u21911\n\u27e6\u2286-+1\u21911\u27e7 \u03b1\u1d63 = mk (\u03bb {x\u2081} {x\u2082} \u2192 there (\u22620 x\u2081) (\u22620 x\u2082)) where open \u27e6\u25c5\u27e7 0 0\n\n\u27e6\u2286-\u21911-inj\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7) \u27e6\u2286\u27e7 (\u03b2\u1d63 \u27e6\u21911\u27e7) \u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63) \u2286-\u21911-inj \u2286-\u21911-inj\n\u27e6\u2286-\u21911-inj\u27e7 _ \u03b2\u1d63 \u03b1\u2191\u2286\u03b2\u2191\u1d63 = mk (\u03bb {x\u2081} {x\u2082} x\u1d63 \u2192 \u27e6pred\u1d3a\u27e7-suc\u1d3a\u2191 \u03b2\u1d63 (un\u27e6\u2286\u27e7 \u03b1\u2191\u2286\u03b2\u2191\u1d63 {suc\u1d3a\u2191 x\u2081} {suc\u1d3a\u2191 x\u2082} (there (\u03bb()) (\u03bb()) x\u1d63))) where\n open \u27e6\u25c5\u27e7 0 0\n\n\u27e6\u2286-unctx-+1\u21911\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7) \u27e6\u2286\u27e7 (\u03b2\u1d63 \u27e6\u21911\u27e7) \u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63) \u2286-unctx-+1\u21911 \u2286-unctx-+1\u21911\n\u27e6\u2286-unctx-+1\u21911\u27e7 _ \u03b2\u1d63 \u03b1+\u2286\u03b2\u2191\u1d63 = mk (\u03bb {x\u2081} {x\u2082} x\u1d63 \u2192 helper (un\u27e6\u2286\u27e7 \u03b1+\u2286\u03b2\u2191\u1d63 {suc\u1d3a x\u2081} {suc\u1d3a x\u2082} x\u1d63)) where\n open \u27e6\u25c5\u27e7 0 0\n helper : \u2200 {x\u2081 x\u2082} (x\u1d63 : \u27e6Name\u27e7 (\u03b2\u1d63 \u27e6\u21911\u27e7) (suc\u1d3a\u2191 x\u2081) (suc\u1d3a\u2191 x\u2082)) \u2192 \u27e6Name\u27e7 \u03b2\u1d63 x\u2081 x\u2082\n helper (here () _)\n helper (there _ _ x\u223cy) = pf-irr (\u27e6Name\u27e7 \u03b2\u1d63) x\u223cy\n\n\u27e6suc\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7)) suc\u1d3a suc\u1d3a\n\u27e6suc\u1d3a\u27e7 \u03b1\u1d63 = pf-irr (\u27e6Name\u27e7 \u03b1\u1d63)\n\n\u27e6pred\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7) \u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63) pred\u1d3a pred\u1d3a\n\u27e6pred\u1d3a\u27e7 \u03b1\u1d63 = pf-irr (\u27e6Name\u27e7 \u03b1\u1d63)\n\n\u27e6suc\u1d3a\u2191\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7)) suc\u1d3a\u2191 suc\u1d3a\u2191\n\u27e6suc\u1d3a\u2191\u27e7 \u03b1\u1d63 x\u1d63 = \u27e6coerce\u1d3a\u27e7 _ _ (\u27e6\u2286-+1\u21911\u27e7 \u03b1\u1d63) (\u27e6suc\u1d3a\u27e7 \u03b1\u1d63 x\u1d63)\n\n\u27e6add\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 k\u1d63 \u2236 \u27e6\u2115\u27e7 \u27e9\u27e6\u2192\u27e7 (\u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63))) add\u1d3a add\u1d3a\n\u27e6add\u1d3a\u27e7 _ zero x\u1d63 = x\u1d63\n\u27e6add\u1d3a\u27e7 \u03b1\u1d63 (suc k\u1d63) x\u1d63 = pf-irr (\u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63)) (\u27e6add\u1d3a\u27e7 \u03b1\u1d63 k\u1d63 x\u1d63)\n\n\u27e6subtract\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 k\u1d63 \u2236 \u27e6\u2115\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63) \u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63) subtract\u1d3a subtract\u1d3a\n\u27e6subtract\u1d3a\u27e7 _ zero x = x\n\u27e6subtract\u1d3a\u27e7 \u03b1\u1d63 (suc {k\u2081} {k\u2082} k\u1d63) {x\u2081} {x\u2082} x\u1d63\n = pf-irr\u2032 (\u27e6Name\u27e7 \u03b1\u1d63) (\u2115.\u2238-+-assoc (binder\u1d3a x\u2081) 1 k\u2081) (\u2115.\u2238-+-assoc (binder\u1d3a x\u2082) 1 k\u2082) (\u27e6subtract\u1d3a\u27e7 \u03b1\u1d63 k\u1d63 x\u1d63)\n\n\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-0-suc : \u2200 {\u03b1\u2081 \u03b1\u2082} (\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082) {pf\u2081 : 0 \u2208 \u03b1\u2081 \u21911} {n} {pf\u2082 : suc n \u2208 \u03b1\u2082 \u21911} \u2192 \u00ac(\u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7) (0 , pf\u2081) (suc n , pf\u2082))\n\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-0-suc _ (\u27e6\u25c5\u27e7.there p _ _) = p \u2261.refl\n\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-0-suc _ (\u27e6\u25c5\u27e7.here _ ())\n\n\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-suc-0 : \u2200 {\u03b1\u2081 \u03b1\u2082} (\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082) {n} {pf\u2081 : suc n \u2208 \u03b1\u2081 \u21911} {pf\u2082 : 0 \u2208 \u03b1\u2082 \u21911} \u2192 \u00ac(\u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7) (suc n , pf\u2081) (0 , pf\u2082))\n\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-suc-0 _ (\u27e6\u25c5\u27e7.there _ p _) = p \u2261.refl\n\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-suc-0 _ (\u27e6\u25c5\u27e7.here () _)\n\n-- cmp\u1d3a-bool : \u2200 {\u03b1} \u2113 \u2192 Name (\u03b1 \u2191 \u2113) \u2192 \ud835\udfda\n-- cmp\u1d3a-bool \u2113 x = suc (binder\u1d3a x) <= \u2113\n\n\u27e6cmp\u1d3a-bool\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 k\u1d63 \u2236 \u27e6\u2115\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) NaPa.cmp\u1d3a-bool NaPa.cmp\u1d3a-bool\n\u27e6cmp\u1d3a-bool\u27e7 _ zero _ = \u27e60\u2082\u27e7\n\u27e6cmp\u1d3a-bool\u27e7 _ (suc _) {zero , _} {zero , _} _ = \u27e61\u2082\u27e7\n\u27e6cmp\u1d3a-bool\u27e7 \u03b1\u1d63 (suc k\u1d63) {suc _ , _} {suc _ , _} x\u1d63 = \u27e6cmp\u1d3a-bool\u27e7 \u03b1\u1d63 k\u1d63 (\u27e6pred\u1d3a\u27e7-suc\u1d3a\u2191 (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) x\u1d63)\n\u27e6cmp\u1d3a-bool\u27e7 \u03b1\u1d63 (suc k\u1d63) {zero , _} {suc _ , _} x\u1d63 = \u22a5-elim (\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-0-suc (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) x\u1d63)\n\u27e6cmp\u1d3a-bool\u27e7 \u03b1\u1d63 (suc k\u1d63) {suc _ , _} {zero , _} x\u1d63 = \u22a5-elim (\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-suc-0 (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) x\u1d63)\n\n\u27e6easy-cmp\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 k\u1d63 \u2236 \u27e6\u2115\u27e7 \u27e9\u27e6\u2192\u27e7\n \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) \u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u27e6\u00f8\u27e7 \u27e6\u2191\u27e7 k\u1d63) \u27e6\u228e\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63)\n ) NaPa.easy-cmp\u1d3a NaPa.easy-cmp\u1d3a\n\u27e6easy-cmp\u1d3a\u27e7 _ zero x\u1d63 = \u27e6inr\u27e7 x\u1d63\n\u27e6easy-cmp\u1d3a\u27e7 _ (suc _) {zero , _} {zero , _} x\u1d63 = \u27e6inl\u27e7 here\u2032\n\u27e6easy-cmp\u1d3a\u27e7 \u03b1\u1d63 (suc k\u1d63) {suc _ , _} {suc _ , _} x\u1d63 = \u27e6map\u27e7 _ _ _ _ (\u27e6suc\u1d3a\u2191\u27e7 (\u27e6\u00f8\u27e7 \u27e6\u2191\u27e7 k\u1d63)) (pf-irr (\u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63))) (\u27e6easy-cmp\u1d3a\u27e7 \u03b1\u1d63 k\u1d63 (\u27e6pred\u1d3a\u27e7-suc\u1d3a\u2191 (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) x\u1d63))\n\u27e6easy-cmp\u1d3a\u27e7 \u03b1\u1d63 (suc k\u1d63) {zero , _} {suc _ , _} x\u1d63 = \u22a5-elim (\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-0-suc (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) x\u1d63)\n\u27e6easy-cmp\u1d3a\u27e7 \u03b1\u1d63 (suc k\u1d63) {suc _ , _} {zero , _} x\u1d63 = \u22a5-elim (\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-suc-0 (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) x\u1d63)\n\n\u27e6cmp\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 k\u1d63 \u2236 \u27e6\u2115\u27e7 \u27e9\u27e6\u2192\u27e7\n \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) \u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u27e6\u00f8\u27e7 \u27e6\u2191\u27e7 k\u1d63) \u27e6\u228e\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63)\n ) cmp\u1d3a cmp\u1d3a\n\u27e6cmp\u1d3a\u27e7 {\u03b1\u2081} {\u03b1\u2082} \u03b1\u1d63 {k\u2081} {k\u2082} k\u1d63 {x\u2081} {x\u2082} x\u1d63 =\n \u2261.tr (\u03bb x \u2192 (\u27e6Name\u27e7 (\u27e6\u00f8\u27e7 \u27e6\u2191\u27e7 k\u1d63) \u27e6\u228e\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63)) (cmp\u1d3a {\u03b1\u2081} k\u2081 x\u2081) x)\n (NaPa.easy-cmp\u1d3a\u2257cmp\u1d3a {\u03b1\u2082} k\u2082 x\u2082)\n (\u2261.tr (\u03bb x \u2192 (\u27e6Name\u27e7 (\u27e6\u00f8\u27e7 \u27e6\u2191\u27e7 k\u1d63) \u27e6\u228e\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63)) x\n (NaPa.easy-cmp\u1d3a {\u03b1\u2082} k\u2082 x\u2082))\n (NaPa.easy-cmp\u1d3a\u2257cmp\u1d3a {\u03b1\u2081} k\u2081 x\u2081) (\u27e6easy-cmp\u1d3a\u27e7 \u03b1\u1d63 k\u1d63 {x\u2081} {x\u2082} x\u1d63))\n\n\u27e6\u2286-dist-+1-\u25c5\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7\n (((b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) \u27e6+1\u27e7) \u27e6\u2286\u27e7 ((\u27e6suc\u1d2e\u27e7 b\u1d63) \u27e6\u25c5\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7))))\n \u2286-dist-+1-\u25c5 \u2286-dist-+1-\u25c5\n\u27e6\u2286-dist-+1-\u25c5\u27e7 \u03b1\u1d63 {b\u2081} {b\u2082} b\u1d63 = mk (\u03bb {\u03b7\u2081} {\u03b7\u2082} \u03b7\u2083 \u2192 helper {\u03b7\u2081} {\u03b7\u2082} \u03b7\u2083) where\n -- open \u27e6\u25c5\u27e7 b\u2081 b\u2082\n\n hper : \u2200 {x\u2081 x\u2082} (x\u1d63 : \u27e6Name\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) x\u2081 x\u2082)\n \u2192 \u27e6Name\u27e7 ((\u27e6suc\u1d2e\u27e7 b\u1d63) \u27e6\u25c5\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7))\n (suc\u1d3a x\u2081) (suc\u1d3a x\u2082)\n hper (\u27e6\u25c5\u27e7.here x\u2261b\u2081 y\u2261b\u2082) = \u27e6\u25c5\u27e7.here (\u2261.cong suc x\u2261b\u2081) (\u2261.cong suc y\u2261b\u2082)\n hper (\u27e6\u25c5\u27e7.there x\u2262b\u2081 y\u2262b\u2082 x\u223cy) = \u27e6\u25c5\u27e7.there (x\u2262b\u2081 \u2218 \u2261.cong pred) (y\u2262b\u2082 \u2218 \u2261.cong pred) x\u223cy\n\n helper : \u2200 {x\u2081 x\u2082} (x\u1d63 : \u27e6Name\u27e7 ((b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) \u27e6+1\u27e7) x\u2081 x\u2082)\n \u2192 \u27e6Name\u27e7 ((\u27e6suc\u1d2e\u27e7 b\u1d63) \u27e6\u25c5\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7))\n (coerce\u1d3a (\u2286-dist-+1-\u25c5 b\u2081) x\u2081)\n (coerce\u1d3a (\u2286-dist-+1-\u25c5 b\u2082) x\u2082)\n helper {suc x\u2081 , pf\u2081} {suc x\u2082 , pf\u2082} x\u1d63 = hper x\u1d63\n helper {zero , ()} {_ , _} _\n helper {_ , _} {zero , ()} _\n\n\u27e6\u2286-dist-\u25c5-+1\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7\n (((\u27e6suc\u1d2e\u27e7 b\u1d63) \u27e6\u25c5\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7)) \u27e6\u2286\u27e7 ((b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) \u27e6+1\u27e7)))\n \u2286-dist-\u25c5-+1 \u2286-dist-\u25c5-+1\n\u27e6\u2286-dist-\u25c5-+1\u27e7 \u03b1\u1d63 {b\u2081} {b\u2082} b\u1d63 = mk helper where\n helper : \u2200 {x\u2081 x\u2082} (x\u1d63 : \u27e6Name\u27e7 ((\u27e6suc\u1d2e\u27e7 b\u1d63) \u27e6\u25c5\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7)) x\u2081 x\u2082)\n \u2192 \u27e6Name\u27e7 ((b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) \u27e6+1\u27e7)\n (coerce\u1d3a (\u2286-dist-\u25c5-+1 b\u2081) x\u2081)\n (coerce\u1d3a (\u2286-dist-\u25c5-+1 b\u2082) x\u2082)\n helper {suc x\u2081 , pf\u2081} {suc x\u2082 , pf\u2082} x\u1d63 with x\u1d63\n ... | \u27e6\u25c5\u27e7.here x\u2261b\u2081 y\u2261b\u2082 = \u27e6\u25c5\u27e7.here (\u2261.cong (\u03bb x \u2192 x \u2238 1) x\u2261b\u2081) (\u2261.cong (\u03bb x \u2192 x \u2238 1) y\u2261b\u2082)\n ... | \u27e6\u25c5\u27e7.there x\u2262b\u2081 y\u2262b\u2082 x\u223cy = \u27e6\u25c5\u27e7.there (x\u2262b\u2081 \u2218 \u2261.cong suc) (y\u2262b\u2082 \u2218 \u2261.cong suc) x\u223cy\n helper {zero , ()} {_ , _} _\n helper {_ , _} {zero , ()} _\n\n{-\nBinder\u2261\u2115 : Binder \u2261 \u2115\nBinder\u2261\u2115 = \u2261.refl\n\npostulate\n \u27e6Binder\u27e7\u2262\u27e6\u2115\u27e7 : \u27e6Binder\u27e7 \u2262 \u27e6\u2115\u27e7\n\n\u27e6\u2261\u27e7-lem : \u2200 {a\u2081 a\u2082 a\u1d63 A\u2081 A\u2082} {A\u1d63 : \u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 A\u2081 A\u2082} {x\u2081 x\u2082} {x\u1d63 : A\u1d63 x\u2081 x\u2082} {y\u2081 y\u2082} {y\u1d63 : A\u1d63 y\u2081 y\u2082} {pf\u2081 pf\u2082} \u2192 \u27e6\u2261\u27e7 A\u1d63 x\u1d63 y\u1d63 pf\u2081 pf\u2082 \u2192 \u2200 (P : \u2200 {z\u2081 z\u2082} \u2192 A\u1d63 z\u2081 z\u2082 \u2192 Set) \u2192 P x\u1d63 \u2192 P y\u1d63\n\u27e6\u2261\u27e7-lem = {!!}\n\n\u00ac\u27e6Binder\u2261\u2115\u27e7 : \u00ac((\u27e6\u2261\u27e7 \u27e6Set\u2080\u27e7 \u27e6Binder\u27e7 \u27e6\u2115\u27e7) Binder\u2261\u2115 Binder\u2261\u2115)\n\u00ac\u27e6Binder\u2261\u2115\u27e7 = \u03bb pf \u2192 \u27e6\u2261\u27e7-lem pf (\u03bb x \u2192 \u22a5) {!!}\n-}\n\n\u27e6binder\u1d3a\u2218name\u1d2e\u27e7 : (\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7\n \u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7\n \u27e6\u2261\u27e7 \u27e6Binder\u27e7 (\u27e6binder\u1d3a\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) (\u27e6name\u1d2e\u27e7 \u03b1\u1d63 b\u1d63)) b\u1d63) binder\u1d3a\u2218name\u1d2e binder\u1d3a\u2218name\u1d2e\n\u27e6binder\u1d3a\u2218name\u1d2e\u27e7 \u03b1\u1d63 b\u1d63 = \u2261.\u27e6refl\u27e7\n\n\u27e8_,_\u27e9\u27e6\u25c5\u27e7_ : (b\u2081 b\u2082 : Binder) \u2192 \u2200 {\u03b1\u2081 \u03b1\u2082} (\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082) \u2192 \u27e6World\u27e7 (b\u2081 \u25c5 \u03b1\u2081) (b\u2082 \u25c5 \u03b1\u2082)\n\u27e8 b\u2081 , b\u2082 \u27e9\u27e6\u25c5\u27e7 \u03b1\u1d63 = _\u27e6\u25c5\u27e7_ {b\u2081} {b\u2082} _ \u03b1\u1d63\n\nmodule Perm (m n : \u2115) (m\u2262n : m \u2262 n) where\n mn\u1d42 : World\n mn\u1d42 = m \u25c5 n \u25c5 \u00f8\n nm\u1d42 : World\n nm\u1d42 = n \u25c5 m \u25c5 \u00f8\n perm-m-n : \u27e6World\u27e7 mn\u1d42 nm\u1d42\n perm-m-n = \u27e8 m , n \u27e9\u27e6\u25c5\u27e7 (\u27e8 n , m \u27e9\u27e6\u25c5\u27e7 \u27e6\u00f8\u27e7)\n m-n : \u2200 {m\u2208 n\u2208} \u2192 \u27e6Name\u27e7 perm-m-n (m , m\u2208) (n , n\u2208)\n m-n = here\u2032\n n-m : \u2200 {m\u2208 n\u2208} \u2192 \u27e6Name\u27e7 perm-m-n (n , n\u2208) (m , m\u2208)\n n-m = \u27e6\u25c5\u27e7.there (m\u2262n \u2218 \u2261.sym) m\u2262n {b\u2208b\u25c5 n \u00f8} {b\u2208b\u25c5 m \u00f8} here\u2032\n\n\u00ac\u27e6\ud835\udfda\u27e7-1\u2082-0\u2082 : \u00ac(\u27e6\ud835\udfda\u27e7 1\u2082 0\u2082)\n\u00ac\u27e6\ud835\udfda\u27e7-1\u2082-0\u2082 ()\n\nbinder-irrelevance : \u2200 (f : Binder \u2192 \ud835\udfda)\n \u2192 (\u27e6Binder\u27e7 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) f f\n \u2192 \u2200 {b\u2081 b\u2082} \u2192 f b\u2081 \u2261 f b\u2082\nbinder-irrelevance _ f\u1d63 = \u27e6\ud835\udfda\u27e7-Props.to-propositional (f\u1d63 _)\n\ncontrab : \u2200 (f : Binder \u2192 \ud835\udfda) {b\u2081 b\u2082}\n \u2192 f b\u2081 \u2262 f b\u2082\n \u2192 \u00ac((\u27e6Binder\u27e7 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) f f)\ncontrab f = contraposition (\u03bb f\u1d63 \u2192 binder-irrelevance f (\u03bb x\u1d63 \u2192 f\u1d63 x\u1d63))\n\nmodule Single \u03b1\u2081 \u03b1\u2082 x\u2081 x\u2082 where\n data \u211b : Name \u03b1\u2081 \u2192 Name \u03b1\u2082 \u2192 Set where\n refl : \u211b x\u2081 x\u2082\n \u211b-pres-\u2261 : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 \u211b x\u2081 x\u2082 \u2192 \u211b y\u2081 y\u2082 \u2192 (x\u2081 \u2261 y\u2081) \u21d4 (x\u2082 \u2261 y\u2082)\n \u211b-pres-\u2261 refl refl = equivalence (const \u2261.refl) (const \u2261.refl)\n \u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082\n \u03b1\u1d63 = \u211b , \u211b-pres-\u2261\n\npoly-name-uniq : \u2200 (f : \u2200 {\u03b1} \u2192 Name \u03b1 \u2192 \ud835\udfda)\n (f\u1d63 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) f f)\n {\u03b1} (x : Name \u03b1) \u2192 f x \u2261 f {\u00f8 \u21911} (0 , _)\npoly-name-uniq f f\u1d63 {\u03b1} x =\n \u27e6\ud835\udfda\u27e7-Props.to-propositional (f\u1d63 {\u03b1} {\u00f8 \u21911} \u03b1\u1d63 {x} {0 , _} refl)\n where open Single \u03b1 (\u00f8 \u21911) x (0 , _)\n\npoly-name-irrelevance : \u2200 (f : \u2200 {\u03b1} \u2192 Name \u03b1 \u2192 \ud835\udfda)\n (f\u1d63 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) f f)\n {\u03b1\u2081 \u03b1\u2082} (x\u2081 : Name \u03b1\u2081) (x\u2082 : Name \u03b1\u2082)\n \u2192 f x\u2081 \u2261 f x\u2082\npoly-name-irrelevance f f\u1d63 x\u2081 x\u2082 =\n \u2261.trans (poly-name-uniq f f\u1d63 x\u2081) (\u2261.sym (poly-name-uniq f f\u1d63 x\u2082))\n\nmodule Broken where\n _<=\u1d3a_ : \u2200 {\u03b1} \u2192 Name \u03b1 \u2192 Name \u03b1 \u2192 \ud835\udfda\n (m , _) <=\u1d3a (n , _) = \u2115._<=_ m n\n\n \u00ac\u27e6<=\u1d3a\u27e7 : \u00ac((\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) _<=\u1d3a_ _<=\u1d3a_)\n \u00ac\u27e6<=\u1d3a\u27e7 \u27e6<=\u27e7 = \u00ac\u27e6\ud835\udfda\u27e7-1\u2082-0\u2082 (\u27e6<=\u27e7 perm-0-1 {0 , _} {1 , _} 0-1 {1 , _} {0 , _} 1-0)\n where open Perm 0 1 (\u03bb()) renaming (perm-m-n to perm-0-1; m-n to 0-1; n-m to 1-0)\n\n \u2286-broken : \u2200 \u03b1 b \u2192 \u03b1 \u2286 (b \u25c5 \u03b1)\n \u2286-broken \u03b1 b = mk (\u03bb b\u2032 b\u2032\u2208\u03b1 \u2192 \u2261.tr id (\u2261.sym (\u25c5-sem \u03b1 b\u2032 b)) (If\u2032 \u2115._==_ b\u2032 b then _ else b\u2032\u2208\u03b1))\n\n \u00ac\u27e6\u2286-broken\u27e7 : \u00ac((\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63))\n \u2286-broken \u2286-broken)\n \u00ac\u27e6\u2286-broken\u27e7 \u27e6\u2286-broken\u27e7 = bot\n where 0\u21940 : \u27e6Name\u27e7 (\u27e8 0 , 1 \u27e9\u27e6\u25c5\u27e7 \u27e8 0 , 0 \u27e9\u27e6\u25c5\u27e7 \u27e6\u00f8\u27e7) (0 , _) (0 , _)\n 0\u21940 = un\u27e6\u2286\u27e7 (\u27e6\u2286-broken\u27e7 (\u27e8 0 , 0 \u27e9\u27e6\u25c5\u27e7 \u27e6\u00f8\u27e7) _) {0 , _} {0 , _} here\u2032\n\n bot : \u22a5\n bot with 0\u21940\n bot | \u27e6\u25c5\u27e7.here _ ()\n bot | \u27e6\u25c5\u27e7.there 0\u22620 _ _ = 0\u22620 \u2261.refl\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nimport Level as L\nopen import Data.Nat.Logical\nopen import Data.Two hiding (_==_)\nopen import Data.Two.Logical\nopen import Data.Unit\nopen import Data.Empty\nopen import Data.Sum.NP\nopen import Data.Sum.Logical\nopen import Data.Maybe.NP\nopen import Data.List\nopen import Data.Product.NP\nopen import Relation.Nullary\nopen import Relation.Nullary.Negation using (contraposition)\nopen import Relation.Nullary.Decidable\nopen import Relation.Binary.NP as Bin\nopen import Relation.Binary.Logical\nopen import Function\nopen import Function.Equality using (_\u27e8$\u27e9_)\nopen import Function.Equivalence as \u21d4 using (_\u21d4_; equivalence; module Equivalence)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nimport Data.Nat.NP as \u2115\nopen \u2115 renaming (_==_ to _==\u2115_)\nimport Data.Nat.Properties as \u2115\nopen import NomPa.Record using (module NomPa)\nopen import NomPa.Implem\nimport NaPa\nopen NaPa using (\u2208-uniq)\nopen import NomPa.Worlds\nimport NomPa.Derived\n\nmodule NomPa.Implem.LogicalRelation.Internals where\n\nopen \u2261 using (_\u2261_; _\u2262_; \u27e6\u2261\u27e7)\nopen \u2261.\u2261-Reasoning\nopen NomPa.Implem.Internals\nopen NomPa NomPa.Implem.nomPa using (worldSym; Name\u00f8-elim; suc\u1d3a; suc\u1d3a\u2191)\n\n-- move to record\npred\u1d3a : \u2200 {\u03b1} \u2192 Name (\u03b1 +1) \u2192 Name \u03b1\npred\u1d3a = subtract\u1d3a 1\n\n\u2262\u2192\u2713-not-==\u2115 : \u2200 {x y} \u2192 x \u2262 y \u2192 \u2713 (not (x ==\u2115 y))\n\u2262\u2192\u2713-not-==\u2115 \u00acp = \u2713-\u00ac-not (\u00acp \u2218 \u2115.==.sound _ _)\n\n#\u21d2\u2209 : \u2200 {\u03b1 b} \u2192 b # \u03b1 \u2192 b \u2209 \u03b1\n#\u21d2\u2209 (_ #\u00f8) = id\n#\u21d2\u2209 (suc#\u2237 b#\u03b1) = #\u21d2\u2209 b#\u03b1\n#\u21d2\u2209 (0# _) = id\n\n#\u21d2\u2262 : \u2200 {\u03b1 b} \u2192 b # \u03b1 \u2192 (x : Name \u03b1) \u2192 binder\u1d3a x \u2262 b\n#\u21d2\u2262 b# (b , b\u2208) \u2261.refl = #\u21d2\u2209 b# b\u2208\n\nbinder\u1d3a-injective : \u2200 {\u03b1} {x y : Name \u03b1} \u2192 binder\u1d3a x \u2261 binder\u1d3a y \u2192 x \u2261 y\nbinder\u1d3a-injective {\u03b1} {_ , p\u2081} {a , p\u2082} eq rewrite eq = \u2261.cong (_,_ a) (\u2208-uniq \u03b1 p\u2081 p\u2082)\n\npf-irr\u2032 : \u2200 {\u03b1 \u03b2} (\u211b : Name \u03b1 \u2192 Name \u03b2 \u2192 Set) {x x\u2032 : Name \u03b1} {y y\u2032 : Name \u03b2}\n \u2192 binder\u1d3a x \u2261 binder\u1d3a x\u2032 \u2192 binder\u1d3a y \u2261 binder\u1d3a y\u2032 \u2192 \u211b x y \u2192 \u211b x\u2032 y\u2032\npf-irr\u2032 {\u03b1} {\u03b2} \u211b {x , x\u2208} {x\u2032 , x\u2032\u2208} {y , y\u2208} {y\u2032 , y\u2032\u2208} eq\u2081 eq\u2082 x\u1d63y\n rewrite eq\u2081 | eq\u2082 | \u2208-uniq \u03b1 x\u2208 x\u2032\u2208 | \u2208-uniq \u03b2 y\u2208 y\u2032\u2208 = x\u1d63y\n\npf-irr : \u2200 {\u03b1 \u03b2} (\u211b : Name \u03b1 \u2192 Name \u03b2 \u2192 Set) {x y x\u2208 x\u2208\u2032 y\u2208 y\u2208\u2032}\n \u2192 \u211b (x , x\u2208) (y , y\u2208) \u2192 \u211b (x , x\u2208\u2032) (y , y\u2208\u2032)\npf-irr \u211b = pf-irr\u2032 \u211b \u2261.refl \u2261.refl\n\nPreserve-\u2248 : \u2200 {a b \u2113 \u2113a \u2113b} {A : Set a} {B : Set b} \u2192 Rel A \u2113a \u2192 Rel (B) \u2113b \u2192 REL A (B) \u2113 \u2192 Set _\nPreserve-\u2248 _\u2248a_ _\u2248b_ _\u223c_ = \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u223c x\u2082 \u2192 y\u2081 \u223c y\u2082 \u2192 (x\u2081 \u2248a y\u2081) \u21d4 (x\u2082 \u2248b y\u2082)\n\n==\u2115\u21d4\u2261 : \u2200 {x y} \u2192 \u2713 (x ==\u2115 y) \u21d4 x \u2261 y\n==\u2115\u21d4\u2261 {x} {y} = equivalence (\u2115.==.sound _ _) \u2115.==.reflexive\n\n\u2261-on-name\u21d4\u2261 : \u2200 {\u03b1} {x y : Name \u03b1} \u2192 binder\u1d3a x \u2261 binder\u1d3a y \u21d4 x \u2261 y\n\u2261-on-name\u21d4\u2261 {\u03b1} {x} {y} = equivalence binder\u1d3a-injective (\u2261.cong binder\u1d3a)\n\nT\u21d4T\u2192\u2261 : \u2200 {b\u2081 b\u2082} \u2192 \u2713 b\u2081 \u21d4 \u2713 b\u2082 \u2192 b\u2081 \u2261 b\u2082\nT\u21d4T\u2192\u2261 {1\u2082} {1\u2082} b\u2081\u21d4b\u2082 = \u2261.refl\nT\u21d4T\u2192\u2261 {1\u2082} {0\u2082} b\u2081\u21d4b\u2082 = \u22a5-elim (Equivalence.to b\u2081\u21d4b\u2082 \u27e8$\u27e9 _)\nT\u21d4T\u2192\u2261 {0\u2082} {1\u2082} b\u2081\u21d4b\u2082 = \u22a5-elim (Equivalence.from b\u2081\u21d4b\u2082 \u27e8$\u27e9 _)\nT\u21d4T\u2192\u2261 {0\u2082} {0\u2082} b\u2081\u21d4b\u2082 = \u2261.refl\n\n==\u1d3a\u21d4\u2261 : \u2200 {\u03b1} {x y : Name \u03b1} \u2192 \u2713 (x ==\u1d3a y) \u21d4 x \u2261 y\n==\u1d3a\u21d4\u2261 = \u2261-on-name\u21d4\u2261 \u27e8\u2218\u27e9 ==\u2115\u21d4\u2261 where open \u21d4 renaming (_\u2218_ to _\u27e8\u2218\u27e9_)\n\n-- Preserving the equalities also mean that the relation is functional and injective\nPreserve-\u2261 : \u2200 {a b \u2113} {A : Set a} {B : Set b} \u2192 REL A (B) \u2113 \u2192 Set _\nPreserve-\u2261 _\u223c_ = Preserve-\u2248 _\u2261_ _\u2261_ _\u223c_\n\nexport\u1d3a?-nothing : \u2200 {b \u03b1} (x : Name (b \u25c5 \u03b1)) \u2192 \u2713 (binder\u1d3a x ==\u2115 b) \u2192 export\u1d3a? {b} x \u2261 nothing\nexport\u1d3a?-nothing {b} {\u03b1} (x , x\u2208) p with x ==\u2115 b | export\u2208 \u03b1 x b\n... | 1\u2082 | _ = \u2261.refl\n... | 0\u2082 | _ = \u22a5-elim p\n\nexport\u1d3a?-just : \u2200 {\u03b1 b} (x : Name (b \u25c5 \u03b1)) {x\u2208} \u2192 \u2713 (not (binder\u1d3a x ==\u2115 b)) \u2192 export\u1d3a? {b} x \u2261 just (binder\u1d3a x , x\u2208)\nexport\u1d3a?-just {\u03b1} {b} (x , x\u2208) p with x ==\u2115 b | export\u2208 \u03b1 x b\n... | 1\u2082 | _ = \u22a5-elim p\n... | 0\u2082 | _ = \u2261.cong just (binder\u1d3a-injective \u2261.refl)\n\nrecord \u27e6World\u27e7 (\u03b1\u2081 \u03b1\u2082 : World) : Set\u2081 where\n constructor _,_\n field\n _\u223c_ : Name \u03b1\u2081 \u2192 Name \u03b1\u2082 \u2192 Set\n \u223c-pres-\u2261 : Preserve-\u2261 _\u223c_\n\n \u223c-inj : \u2200 {x y z} \u2192 x \u223c z \u2192 y \u223c z \u2192 x \u2261 y\n \u223c-inj p q = Equivalence.from (\u223c-pres-\u2261 p q) \u27e8$\u27e9 \u2261.refl\n \u223c-fun : \u2200 {x y z} \u2192 x \u223c y \u2192 x \u223c z \u2192 y \u2261 z\n \u223c-fun p q = Equivalence.to (\u223c-pres-\u2261 p q) \u27e8$\u27e9 \u2261.refl\n\n \u223c-\u2208-uniq : \u2200 {x\u2081 x\u2082} {x\u2081\u2208\u2032 x\u2082\u2208\u2032} \u2192\n x\u2081 \u223c x\u2082 \u2261 (binder\u1d3a x\u2081 , x\u2081\u2208\u2032) \u223c (binder\u1d3a x\u2082 , x\u2082\u2208\u2032)\n \u223c-\u2208-uniq {_ , x\u2081\u2208} {_ , x\u2082\u2208} {x\u2081\u2208\u2032} {x\u2082\u2208\u2032} = \u2261.ap\u2082 (\u03bb x\u2081\u2208 x\u2082\u2208 \u2192 (_ , x\u2081\u2208) \u223c (_ , x\u2082\u2208)) (\u2208-uniq \u03b1\u2081 x\u2081\u2208 x\u2081\u2208\u2032) (\u2208-uniq \u03b1\u2082 x\u2082\u2208 x\u2082\u2208\u2032)\n\n\u27e6sym\u27e7 : \u2200 {\u03b1\u2081 \u03b1\u2082} \u2192 \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082 \u2192 \u27e6World\u27e7 \u03b1\u2082 \u03b1\u2081\n\u27e6sym\u27e7 (\u211b , \u211b-pres-\u2261) = \u211b-sym , \u211b-sym-pres-\u2261\n where \u211b-sym = flip \u211b\n \u211b-sym-pres-\u2261 : Preserve-\u2261 \u211b-sym\n \u211b-sym-pres-\u2261 p q = \u21d4.sym (\u211b-pres-\u2261 p q)\n\nrecord \u27e6Binder\u27e7 (b\u2081 b\u2082 : Binder) : Set where\n\nmodule \u27e6\u25c5\u27e7 (b\u2081 b\u2082 : Binder) {\u03b1\u2081 \u03b1\u2082} (\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082) where\n open \u27e6World\u27e7 \u03b1\u1d63\n data _\u211b_ (x : Name (b\u2081 \u25c5 \u03b1\u2081)) (y : Name (b\u2082 \u25c5 \u03b1\u2082)) : Set where\n here : (x\u2261b\u2081 : binder\u1d3a x \u2261 b\u2081) (y\u2261b\u2082 : binder\u1d3a y \u2261 b\u2082) \u2192 x \u211b y\n there : \u2200 (x\u2262b\u2081 : binder\u1d3a x \u2262 b\u2081) (y\u2262b\u2082 : binder\u1d3a y \u2262 b\u2082) {x\u2208 y\u2208} (x\u223cy : (binder\u1d3a x , x\u2208) \u223c (binder\u1d3a y , y\u2208)) \u2192 x \u211b y\n\n \u211b-inj : \u2200 {x y z} \u2192 x \u211b z \u2192 y \u211b z \u2192 x \u2261 y\n \u211b-inj (here x\u2261b\u2081 _) (here y\u2261b\u2081 _) = binder\u1d3a-injective (\u2261.trans x\u2261b\u2081 (\u2261.sym y\u2261b\u2081))\n \u211b-inj (here _ z\u2261b\u2082) (there _ z\u2262b\u2082 _) = \u22a5-elim (z\u2262b\u2082 z\u2261b\u2082)\n \u211b-inj (there _ z\u2262b\u2082 _) (here _ z\u2261b\u2082) = \u22a5-elim (z\u2262b\u2082 z\u2261b\u2082)\n \u211b-inj {x} {y} {z} (there x\u2262b\u2081 z\u2262b\u2082 {x\u2208} {z\u2208} x\u223cz) (there y\u2262b\u2081 _ {y\u2208} {z\u2208\u2032} y\u223cz) =\n binder\u1d3a-injective (\u2261.cong binder\u1d3a (\u223c-inj x\u223cz (\u2261.tr (\u03bb z\u2208 \u2192 (binder\u1d3a y , y\u2208) \u223c (binder\u1d3a z , z\u2208)) (\u2208-uniq \u03b1\u2082 z\u2208\u2032 z\u2208) y\u223cz )))\n\n \u211b-fun : \u2200 {x y z} \u2192 x \u211b y \u2192 x \u211b z \u2192 y \u2261 z\n \u211b-fun (here _ y\u2261b\u2082) (here _ z\u2261b\u2082) = binder\u1d3a-injective (\u2261.trans y\u2261b\u2082 (\u2261.sym z\u2261b\u2082))\n \u211b-fun (here p _) (there \u00acp _ _) = \u22a5-elim (\u00acp p)\n \u211b-fun (there \u00acp _ _) (here p _) = \u22a5-elim (\u00acp p)\n \u211b-fun {x} {y} {z} (there x\u2262b\u2081 y\u2262b\u2082 {x\u2208} {y\u2208} x\u223cy) (there p q {x\u2208\u2032} {z\u2208} x\u223cz) =\n binder\u1d3a-injective (\u2261.cong binder\u1d3a (\u223c-fun x\u223cy (\u2261.tr (\u03bb x\u2208 \u2192 (binder\u1d3a x , x\u2208) \u223c (binder\u1d3a z , z\u2208)) (\u2208-uniq \u03b1\u2081 x\u2208\u2032 x\u2208) x\u223cz )))\n\n \u211b-pres-\u2261 : Preserve-\u2261 _\u211b_\n \u211b-pres-\u2261 {x\u2081} {x\u2082} {y\u2081} {y\u2082} x\u2081\u223cx\u2082 y\u2081\u223cy\u2082 =\n equivalence (\u03bb x\u2081=y\u2081 \u2192 \u211b-fun {y\u2081} (\u2261.tr (\u03bb x\u2081 \u2192 x\u2081 \u211b x\u2082) x\u2081=y\u2081 x\u2081\u223cx\u2082) y\u2081\u223cy\u2082)\n (\u03bb x\u2082=y\u2082 \u2192 \u211b-inj x\u2081\u223cx\u2082 (\u2261.tr (\u03bb y\u2082 \u2192 y\u2081 \u211b y\u2082) (\u2261.sym x\u2082=y\u2082) y\u2081\u223cy\u2082))\n\n \u27e6world\u27e7 : \u27e6World\u27e7 (b\u2081 \u25c5 \u03b1\u2081) (b\u2082 \u25c5 \u03b1\u2082)\n \u27e6world\u27e7 = _\u211b_ , \u211b-pres-\u2261\n\nhere\u2032 : \u2200 {\u03b1\u2081 \u03b1\u2082 b\u2081 b\u2082 pf\u2081 pf\u2082} {\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082} \u2192 \u27e6\u25c5\u27e7._\u211b_ b\u2081 b\u2082 \u03b1\u1d63 (b\u2081 , pf\u2081) (b\u2082 , pf\u2082)\nhere\u2032 = \u27e6\u25c5\u27e7.here \u2261.refl \u2261.refl\n\n_\u27e6\u25c5\u27e7_ : (\u27e6Binder\u27e7 \u27e6\u2192\u27e7 \u27e6World\u27e7 \u27e6\u2192\u27e7 \u27e6World\u27e7) _\u25c5_ _\u25c5_\n_\u27e6\u25c5\u27e7_ {b\u2081} {b\u2082} _ \u03b1\u1d63 = \u27e6\u25c5\u27e7.\u27e6world\u27e7 b\u2081 b\u2082 \u03b1\u1d63\n\n\u27e6Name\u27e7 : \u27e6Pred\u27e7 L.zero \u27e6World\u27e7 Name Name\n\u27e6Name\u27e7 (\u211b , _) x\u2081 x\u2082 = \u211b x\u2081 x\u2082\n\n\u27e6zero\u1d2e\u27e7 : \u27e6Binder\u27e7 zero\u1d2e zero\u1d2e\n\u27e6zero\u1d2e\u27e7 = _\n\n\u27e6suc\u1d2e\u27e7 : (\u27e6Binder\u27e7 \u27e6\u2192\u27e7 \u27e6Binder\u27e7) suc\u1d2e suc\u1d2e\n\u27e6suc\u1d2e\u27e7 = _\n\n\u27e6\u00f8\u27e7 : \u27e6World\u27e7 \u00f8 \u00f8\n\u27e6\u00f8\u27e7 = (\u03bb _ _ \u2192 \u22a5) , (\u03bb())\n\n_\u27e6==\u1d3a\u27e7_ : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) _==\u1d3a_ _==\u1d3a_\n_\u27e6==\u1d3a\u27e7_ \u03b1\u1d63 x\u1d63 y\u1d63 =\n \u27e6\ud835\udfda\u27e7-Props.reflexive (T\u21d4T\u2192\u2261 (sym ==\u1d3a\u21d4\u2261 \u27e8\u2218\u27e9 \u223c-pres-\u2261 x\u1d63 y\u1d63 \u27e8\u2218\u27e9 ==\u1d3a\u21d4\u2261)) where\n open \u27e6World\u27e7 \u03b1\u1d63\n open \u21d4 using (sym) renaming (_\u2218_ to _\u27e8\u2218\u27e9_)\n\n\u27e6name\u1d2e\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63)) name\u1d2e name\u1d2e\n\u27e6name\u1d2e\u27e7 _ _ = here\u2032\n\n\u27e6binder\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Binder\u27e7) binder\u1d3a binder\u1d3a\n\u27e6binder\u1d3a\u27e7 _ _ = _\n\n\u27e6nothing\u27e7\u2032 : \u2200 {a} {A\u2081 A\u2082 : Set a} {A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set} {x : Maybe A\u2081} {y : Maybe A\u2082}\n \u2192 x \u2261 nothing \u2192 y \u2261 nothing \u2192 \u27e6Maybe\u27e7 A\u1d63 x y\n\u27e6nothing\u27e7\u2032 \u2261.refl \u2261.refl = \u27e6nothing\u27e7\n\n\u27e6just\u27e7\u2032 : \u2200 {a} {A\u2081 A\u2082 : Set a} {A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set} {x : Maybe A\u2081} {y : Maybe A\u2082} {x\u2032 y\u2032}\n \u2192 x \u2261 just x\u2032 \u2192 y \u2261 just y\u2032 \u2192 A\u1d63 x\u2032 y\u2032 \u2192 \u27e6Maybe\u27e7 A\u1d63 x y\n\u27e6just\u27e7\u2032 \u2261.refl \u2261.refl = \u27e6just\u27e7\n\n\u27e6export\u1d3a?\u27e7 : (\u2200\u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) \u27e6\u2192\u27e7 \u27e6Maybe\u27e7 (\u27e6Name\u27e7 \u03b1\u1d63)) (\u03bb {b} \u2192 export\u1d3a? {b}) (\u03bb {b} \u2192 export\u1d3a? {b})\n\u27e6export\u1d3a?\u27e7 _ \u03b1\u1d63 {x\u2081} {x\u2082} (\u27e6\u25c5\u27e7.here e\u2081 e\u2082)\n = \u27e6nothing\u27e7\u2032 (export\u1d3a?-nothing x\u2081 (\u2115.==.reflexive e\u2081)) (export\u1d3a?-nothing x\u2082 (\u2115.==.reflexive e\u2082))\n\u27e6export\u1d3a?\u27e7 _ \u03b1\u1d63 {x\u2081} {x\u2082} (\u27e6\u25c5\u27e7.there x\u2081\u2262b\u2081 x\u2082\u2262b\u2082 x\u2081\u223cx\u2082)\n = \u27e6just\u27e7\u2032 (export\u1d3a?-just x\u2081 (\u2262\u2192\u2713-not-==\u2115 x\u2081\u2262b\u2081))\n (export\u1d3a?-just x\u2082 (\u2262\u2192\u2713-not-==\u2115 x\u2082\u2262b\u2082)) x\u2081\u223cx\u2082\n\n_\u27e6\u2286\u27e7b_ : \u27e6Rel\u27e7 \u27e6World\u27e7 L.zero _\u2286_ _\u2286_\n_\u27e6\u2286\u27e7b_ \u03b1\u1d63 \u03b2\u1d63 x y = (\u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b2\u1d63) (coerce\u1d3a x) (coerce\u1d3a y)\n\ndata _\u27e6\u2286\u27e7_ {\u03b1\u2081 \u03b1\u2082} (\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082) {\u03b2\u2081 \u03b2\u2082} (\u03b2\u1d63 : \u27e6World\u27e7 \u03b2\u2081 \u03b2\u2082) (x : \u03b1\u2081 \u2286 \u03b2\u2081) (y : \u03b1\u2082 \u2286 \u03b2\u2082) : Set where\n mk : _\u27e6\u2286\u27e7b_ \u03b1\u1d63 \u03b2\u1d63 x y \u2192 _\u27e6\u2286\u27e7_ \u03b1\u1d63 \u03b2\u1d63 x y\n\nun\u27e6\u2286\u27e7 : \u2200 {\u03b1\u2081 \u03b1\u2082} {\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082} {\u03b2\u2081 \u03b2\u2082} {\u03b2\u1d63 : \u27e6World\u27e7 \u03b2\u2081 \u03b2\u2082} {x y} \u2192 (\u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63) x y \u2192 (\u03b1\u1d63 \u27e6\u2286\u27e7b \u03b2\u1d63) x y\nun\u27e6\u2286\u27e7 (mk x) {a} {b} c = x {a} {b} c\n\n\u27e6\u2286-refl\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b1\u1d63) \u2286-refl \u2286-refl\n\u27e6\u2286-refl\u27e7 _ = mk (\u03bb x\u1d63 \u2192 x\u1d63)\n\n\u27e6\u2286-trans\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b3\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7\n \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63 \u27e6\u2192\u27e7 (\u03b2\u1d63 \u27e6\u2286\u27e7 \u03b3\u1d63 \u27e6\u2192\u27e7 (\u03b1\u1d63 \u27e6\u2286\u27e7 \u03b3\u1d63))) \u2286-trans \u2286-trans\n\u27e6\u2286-trans\u27e7 _ _ _ {mk f\u2081} {mk f\u2082} f {mk g\u2081} {mk g\u2082} g\n = mk (\u03bb {x\u2081} {x\u2082} x\u1d63 \u2192 un\u27e6\u2286\u27e7 g {coerce\u1d3a (mk f\u2081) x\u2081} {coerce\u1d3a (mk f\u2082) x\u2082} (un\u27e6\u2286\u27e7 f {x\u2081} {x\u2082} x\u1d63))\n\n\u27e6coerce\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63 \u27e6\u2192\u27e7 (\u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b2\u1d63)) coerce\u1d3a coerce\u1d3a\n\u27e6coerce\u1d3a\u27e7 _ _ \u03b1\u2286\u03b2\u1d63 {x\u2081} {x\u2082} x\u1d63 = un\u27e6\u2286\u27e7 \u03b1\u2286\u03b2\u1d63 {x\u2081} {x\u2082} x\u1d63\n\n\u27e6\u00acName\u00f8\u27e7 : (\u27e6\u00ac\u27e7(\u27e6Name\u27e7 \u27e6\u00f8\u27e7)) \u00acName\u00f8 \u00acName\u00f8\n\u27e6\u00acName\u00f8\u27e7 ()\n\n\u27e6\u2286-\u00f8\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6\u00f8\u27e7 \u27e6\u2286\u27e7 \u03b1\u1d63) \u2286-\u00f8 \u2286-\u00f8\n\u27e6\u2286-\u00f8\u27e7 \u03b1\u1d63 = mk helper where\n helper : (\u27e6\u00f8\u27e7 \u27e6\u2286\u27e7b \u03b1\u1d63) \u2286-\u00f8 \u2286-\u00f8\n helper {_ , ()}\n\n_\u27e6#\u27e7_ : (\u27e6Binder\u27e7 \u27e6\u2192\u27e7 \u27e6World\u27e7 \u27e6\u2192\u27e7 \u27e6Set\u2080\u27e7) _#_ _#_\n_\u27e6#\u27e7_ _ _ _ _ = \u22a4\n\n_\u27e6#\u00f8\u27e7 : (\u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7 b\u1d63 \u27e6#\u27e7 \u27e6\u00f8\u27e7) _#\u00f8 _#\u00f8\n_\u27e6#\u00f8\u27e7 _ = _\n\n\u27e6suc#\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7 b\u1d63 \u27e6#\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 (\u27e6suc\u1d2e\u27e7 b\u1d63) \u27e6#\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63)) suc# suc#\n\u27e6suc#\u27e7 _ _ _ = _\n\n\u27e6\u2286-#\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7\n (b\u1d63 \u27e6#\u27e7 \u03b1\u1d63) \u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63)) \u2286-# \u2286-#\n\u27e6\u2286-#\u27e7 _ {b\u2081} {b\u2082} _ {b\u2081#\u03b1\u2081} {b\u2082#\u03b1\u2082} _ = mk (\u03bb {x\u2081} {x\u2082} \u2192 \u27e6\u25c5\u27e7.there (#\u21d2\u2262 b\u2081#\u03b1\u2081 x\u2081) (#\u21d2\u2262 b\u2082#\u03b1\u2082 x\u2082))\n\n\u27e6\u2286-\u25c5\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63 \u27e6\u2192\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) \u27e6\u2286\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b2\u1d63)) \u2286-\u25c5 \u2286-\u25c5\n\u27e6\u2286-\u25c5\u27e7 {\u03b1\u2081} {\u03b1\u2082} \u03b1\u1d63 {\u03b2\u2081} {\u03b2\u2082} \u03b2\u1d63 {b\u2081} {b\u2082} _ {\u03b1\u2286\u03b2\u2081} {\u03b1\u2286\u03b2\u2082} (mk f) = mk g where\n open \u27e6\u25c5\u27e7 b\u2081 b\u2082\n g : \u2200 {x\u2081 x\u2082} \u2192 _\u211b_ \u03b1\u1d63 x\u2081 x\u2082 \u2192 _\u211b_ \u03b2\u1d63 (coerce\u1d3a (\u2286-\u25c5 b\u2081 \u03b1\u2286\u03b2\u2081) x\u2081) (coerce\u1d3a (\u2286-\u25c5 b\u2082 \u03b1\u2286\u03b2\u2082) x\u2082)\n g (here x\u2261b\u2081 y\u2261b\u2082) = here x\u2261b\u2081 y\u2261b\u2082\n g (there x\u2262b\u2081 y\u2262b\u2082 x\u223cy) = there x\u2262b\u2081 y\u2262b\u2082 (f x\u223cy)\n\ncong\u2115 : \u2200 {\u03b1 \u03b2 : World} {x y : Name \u03b1} (f : \u2115 \u2192 \u2115) {fx\u2208 fy\u2208} \u2192 x \u2261 y \u2192 _\u2261_ {A = Name \u03b2} (f (binder\u1d3a x) , fx\u2208) (f (binder\u1d3a y) , fy\u2208)\ncong\u2115 f = binder\u1d3a-injective \u2218 \u2261.cong (f \u2218 binder\u1d3a)\n\nmodule \u27e6+1\u27e7 {\u03b1\u2081 \u03b1\u2082} (\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082) where\n open \u27e6World\u27e7 \u03b1\u1d63 renaming (_\u223c_ to \u211b; \u223c-inj to \u211b-inj; \u223c-fun to \u211b-fun)\n \u211b+1 : Name (\u03b1\u2081 +1) \u2192 Name (\u03b1\u2082 +1) \u2192 Set\n \u211b+1 x y = \u27e6Name\u27e7 \u03b1\u1d63 (pred\u1d3a x) (pred\u1d3a y)\n{-\n data \u211b+1 : Name (\u03b1\u2081 +1) \u2192 Name (\u03b1\u2082 +1) \u2192 Set where\n suc : \u2200 {x x\u2208 y y\u2208} \u2192 \u27e6Name\u27e7 \u03b1\u1d63 x y \u2192 \u211b+1 (suc x , ) (sucN y)\n data \u211b+1 : Name (\u03b1\u2081 +1) \u2192 Name (\u03b1\u2082 +1) \u2192 Set where\n suc : \u2200 {x y} \u2192 \u27e6Name\u27e7 \u03b1\u1d63 x y \u2192 \u211b+1 (sucN x) (sucN y)\n-}\n\n \u211b+1-inj : \u2200 {x y z} \u2192 \u211b+1 x z \u2192 \u211b+1 y z \u2192 x \u2261 y\n -- \u211b+1-inj {suc x , _} {suc y , _} {suc z , _} (suc a) (suc b) = {!!}\n \u211b+1-inj {suc x , _} {suc y , _} {suc z , _} a b = cong\u2115 suc (\u211b-inj a b)\n \u211b+1-inj {zero , ()} _ _\n \u211b+1-inj {_} {zero , ()} _ _\n \u211b+1-inj {_} {_} {zero , ()} _ _\n \u211b+1-fun : \u2200 {x y z} \u2192 \u211b+1 x y \u2192 \u211b+1 x z \u2192 y \u2261 z\n \u211b+1-fun {suc x , _} {suc y , _} {suc z , _} a b = cong\u2115 suc (\u211b-fun a b)\n \u211b+1-fun {zero , ()} _ _\n \u211b+1-fun {_} {zero , ()} _ _\n \u211b+1-fun {_} {_} {zero , ()} _ _\n\n \u211b+1-pres-\u2261 : Preserve-\u2261 \u211b+1\n -- \u211b+1-pres-\u2261 x y = equivalence {!\u211b+1-inj!} {!!}\n \u211b+1-pres-\u2261 {x\u2081} {x\u2082} {y\u2081} {y\u2082} x\u2081\u223cx\u2082 y\u2081\u223cy\u2082 =\n -- factor this move\n equivalence (\u03bb x\u2081=y\u2081 \u2192 \u211b+1-fun {y\u2081} (\u2261.tr (\u03bb x\u2081 \u2192 \u211b+1 x\u2081 x\u2082) x\u2081=y\u2081 x\u2081\u223cx\u2082) y\u2081\u223cy\u2082)\n (\u03bb x\u2082=y\u2082 \u2192 \u211b+1-inj {x\u2081} {y\u2081} {x\u2082} x\u2081\u223cx\u2082 (\u2261.tr (\u03bb y\u2082 \u2192 \u211b+1 y\u2081 y\u2082) (\u2261.sym x\u2082=y\u2082) y\u2081\u223cy\u2082))\n\n\n_\u27e6+1\u27e7 : (\u27e6World\u27e7 \u27e6\u2192\u27e7 \u27e6World\u27e7) _+1 _+1\n_\u27e6+1\u27e7 \u03b1\u1d63 = \u211b+1 , \u211b+1-pres-\u2261 where open \u27e6+1\u27e7 \u03b1\u1d63\n\nopen WorldOps worldSym\n\n_\u27e6\u21911\u27e7 : (\u27e6World\u27e7 \u27e6\u2192\u27e7 \u27e6World\u27e7) _\u21911 _\u21911\n_\u27e6\u21911\u27e7 \u03b1\u1d63 = \u27e6zero\u1d2e\u27e7 \u27e6\u25c5\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7)\n\n_\u27e6\u2191\u27e7_ : (\u27e6World\u27e7 \u27e6\u2192\u27e7 \u27e6\u2115\u27e7 \u27e6\u2192\u27e7 \u27e6World\u27e7) _\u2191_ _\u2191_\n_\u27e6\u2191\u27e7_ \u03b1\u1d63 zero = \u03b1\u1d63\n_\u27e6\u2191\u27e7_ \u03b1\u1d63 (suc k) = (\u03b1\u1d63 \u27e6\u2191\u27e7 k) \u27e6\u21911\u27e7\n\n_\u27e6+\u1d42\u27e7_ : (\u27e6World\u27e7 \u27e6\u2192\u27e7 \u27e6\u2115\u27e7 \u27e6\u2192\u27e7 \u27e6World\u27e7) _+\u1d42_ _+\u1d42_\n_\u27e6+\u1d42\u27e7_ \u03b1\u1d63 zero = \u03b1\u1d63\n_\u27e6+\u1d42\u27e7_ \u03b1\u1d63 (suc k) = (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k) \u27e6+1\u27e7\n\n\u27e6pred\u1d3a\u27e7-suc\u1d3a\u2191 : \u2200 {\u03b1\u2081 \u03b1\u2082} (\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082)\n {x\u2081 x\u2082} (x\u1d63 : \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7) (suc\u1d3a\u2191 x\u2081) (suc\u1d3a\u2191 x\u2082)) \u2192 \u27e6Name\u27e7 \u03b1\u1d63 x\u2081 x\u2082\n\u27e6pred\u1d3a\u27e7-suc\u1d3a\u2191 _ (\u27e6\u25c5\u27e7.here () _)\n\u27e6pred\u1d3a\u27e7-suc\u1d3a\u2191 \u03b1\u1d63 (\u27e6\u25c5\u27e7.there _ _ x\u223cy) = pf-irr (\u27e6Name\u27e7 \u03b1\u1d63) x\u223cy\n\n\u27e6\u00f8+1\u2286\u00f8\u27e7 : (\u27e6\u00f8\u27e7 \u27e6+1\u27e7 \u27e6\u2286\u27e7 \u27e6\u00f8\u27e7) \u00f8+1\u2286\u00f8 \u00f8+1\u2286\u00f8\n\u27e6\u00f8+1\u2286\u00f8\u27e7 = mk (\u03bb {x} _ \u2192 Name\u00f8-elim (pred\u1d3a x))\n\n\u27e6\u2286-cong-+1\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63 \u27e6\u2192\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7) \u27e6\u2286\u27e7 (\u03b2\u1d63 \u27e6+1\u27e7)) \u2286-cong-+1 \u2286-cong-+1\n\u27e6\u2286-cong-+1\u27e7 _ \u03b2\u1d63 \u03b1\u2286\u03b2\u1d63 = mk (pf-irr (\u27e6Name\u27e7 \u03b2\u1d63) \u2218 un\u27e6\u2286\u27e7 \u03b1\u2286\u03b2\u1d63)\n\n\u27e6\u2286-cong-\u21911\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63 \u27e6\u2192\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7) \u27e6\u2286\u27e7 (\u03b2\u1d63 \u27e6\u21911\u27e7)) \u2286-cong-\u21911 \u2286-cong-\u21911\n\u27e6\u2286-cong-\u21911\u27e7 \u03b1\u1d63 \u03b2\u1d63 {\u03b1\u2286\u03b2\u2081} {\u03b1\u2286\u03b2\u2082} \u03b1\u2286\u03b2\u1d63 = mk helper where\n open \u27e6\u25c5\u27e7 0 0\n helper : (\u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7) \u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u03b2\u1d63 \u27e6\u21911\u27e7)) (coerce\u1d3a (\u2286-cong-\u21911 \u03b1\u2286\u03b2\u2081)) (coerce\u1d3a (\u2286-cong-\u21911 \u03b1\u2286\u03b2\u2082))\n helper (here x\u2261b\u2081 y\u2261b\u2082) = here x\u2261b\u2081 y\u2261b\u2082\n helper {x\u2081 , _} {x\u2082 , _} (there x\u2262b\u2081 y\u2262b\u2082 {p\u2081} {p\u2082} x\u223cy)\n = there x\u2262b\u2081 y\u2262b\u2082 {coe (\u2286-cong-+1 \u03b1\u2286\u03b2\u2081) x\u2081 p\u2081}\n {coe (\u2286-cong-+1 \u03b1\u2286\u03b2\u2082) x\u2082 p\u2082}\n (pf-irr (\u27e6Name\u27e7 \u03b2\u1d63) (un\u27e6\u2286\u27e7 \u03b1\u2286\u03b2\u1d63 x\u223cy))\n\n\u27e6\u2286-+1-inj\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7) \u27e6\u2286\u27e7 (\u03b2\u1d63 \u27e6+1\u27e7) \u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63) \u2286-+1-inj \u2286-+1-inj\n\u27e6\u2286-+1-inj\u27e7 _ _ \u03b1+\u2286\u03b2+\u1d63 = mk (\u03bb {x\u2081} {x\u2082} \u2192 un\u27e6\u2286\u27e7 \u03b1+\u2286\u03b2+\u1d63 {add\u1d3a 1 x\u2081} {add\u1d3a 1 x\u2082}) \n\n\u22620 : \u2200 {\u03b1} (x : Name (\u03b1 +1)) \u2192 binder\u1d3a x \u2262 0\n\u22620 (0 , ()) \u2261.refl\n\u22620 (suc _ , _) ()\n\n\u27e6\u2286-+1\u21911\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7) \u27e6\u2286\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7)) \u2286-+1\u21911 \u2286-+1\u21911\n\u27e6\u2286-+1\u21911\u27e7 \u03b1\u1d63 = mk (\u03bb {x\u2081} {x\u2082} \u2192 there (\u22620 x\u2081) (\u22620 x\u2082)) where open \u27e6\u25c5\u27e7 0 0\n\n\u27e6\u2286-\u21911-inj\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7) \u27e6\u2286\u27e7 (\u03b2\u1d63 \u27e6\u21911\u27e7) \u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63) \u2286-\u21911-inj \u2286-\u21911-inj\n\u27e6\u2286-\u21911-inj\u27e7 _ \u03b2\u1d63 \u03b1\u2191\u2286\u03b2\u2191\u1d63 = mk (\u03bb {x\u2081} {x\u2082} x\u1d63 \u2192 \u27e6pred\u1d3a\u27e7-suc\u1d3a\u2191 \u03b2\u1d63 (un\u27e6\u2286\u27e7 \u03b1\u2191\u2286\u03b2\u2191\u1d63 {suc\u1d3a\u2191 x\u2081} {suc\u1d3a\u2191 x\u2082} (there (\u03bb()) (\u03bb()) x\u1d63))) where\n open \u27e6\u25c5\u27e7 0 0\n\n\u27e6\u2286-unctx-+1\u21911\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 \u03b2\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7) \u27e6\u2286\u27e7 (\u03b2\u1d63 \u27e6\u21911\u27e7) \u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 \u03b2\u1d63) \u2286-unctx-+1\u21911 \u2286-unctx-+1\u21911\n\u27e6\u2286-unctx-+1\u21911\u27e7 _ \u03b2\u1d63 \u03b1+\u2286\u03b2\u2191\u1d63 = mk (\u03bb {x\u2081} {x\u2082} x\u1d63 \u2192 helper (un\u27e6\u2286\u27e7 \u03b1+\u2286\u03b2\u2191\u1d63 {suc\u1d3a x\u2081} {suc\u1d3a x\u2082} x\u1d63)) where\n open \u27e6\u25c5\u27e7 0 0\n helper : \u2200 {x\u2081 x\u2082} (x\u1d63 : \u27e6Name\u27e7 (\u03b2\u1d63 \u27e6\u21911\u27e7) (suc\u1d3a\u2191 x\u2081) (suc\u1d3a\u2191 x\u2082)) \u2192 \u27e6Name\u27e7 \u03b2\u1d63 x\u2081 x\u2082\n helper (here () _)\n helper (there _ _ x\u223cy) = pf-irr (\u27e6Name\u27e7 \u03b2\u1d63) x\u223cy\n\n\u27e6suc\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7)) suc\u1d3a suc\u1d3a\n\u27e6suc\u1d3a\u27e7 \u03b1\u1d63 = pf-irr (\u27e6Name\u27e7 \u03b1\u1d63)\n\n\u27e6pred\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7) \u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63) pred\u1d3a pred\u1d3a\n\u27e6pred\u1d3a\u27e7 \u03b1\u1d63 = pf-irr (\u27e6Name\u27e7 \u03b1\u1d63)\n\n\u27e6suc\u1d3a\u2191\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7)) suc\u1d3a\u2191 suc\u1d3a\u2191\n\u27e6suc\u1d3a\u2191\u27e7 \u03b1\u1d63 x\u1d63 = \u27e6coerce\u1d3a\u27e7 _ _ (\u27e6\u2286-+1\u21911\u27e7 \u03b1\u1d63) (\u27e6suc\u1d3a\u27e7 \u03b1\u1d63 x\u1d63)\n\n\u27e6add\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 k\u1d63 \u2236 \u27e6\u2115\u27e7 \u27e9\u27e6\u2192\u27e7 (\u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63))) add\u1d3a add\u1d3a\n\u27e6add\u1d3a\u27e7 _ zero x\u1d63 = x\u1d63\n\u27e6add\u1d3a\u27e7 \u03b1\u1d63 (suc k\u1d63) x\u1d63 = pf-irr (\u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63)) (\u27e6add\u1d3a\u27e7 \u03b1\u1d63 k\u1d63 x\u1d63)\n\n\u27e6subtract\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 k\u1d63 \u2236 \u27e6\u2115\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63) \u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63) subtract\u1d3a subtract\u1d3a\n\u27e6subtract\u1d3a\u27e7 _ zero x = x\n\u27e6subtract\u1d3a\u27e7 \u03b1\u1d63 (suc {k\u2081} {k\u2082} k\u1d63) {x\u2081} {x\u2082} x\u1d63\n = pf-irr\u2032 (\u27e6Name\u27e7 \u03b1\u1d63) (\u2115.\u2238-+-assoc (binder\u1d3a x\u2081) 1 k\u2081) (\u2115.\u2238-+-assoc (binder\u1d3a x\u2082) 1 k\u2082) (\u27e6subtract\u1d3a\u27e7 \u03b1\u1d63 k\u1d63 x\u1d63)\n\n\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-0-suc : \u2200 {\u03b1\u2081 \u03b1\u2082} (\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082) {pf\u2081 : 0 \u2208 \u03b1\u2081 \u21911} {n} {pf\u2082 : suc n \u2208 \u03b1\u2082 \u21911} \u2192 \u00ac(\u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7) (0 , pf\u2081) (suc n , pf\u2082))\n\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-0-suc _ (\u27e6\u25c5\u27e7.there p _ _) = p \u2261.refl\n\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-0-suc _ (\u27e6\u25c5\u27e7.here _ ())\n\n\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-suc-0 : \u2200 {\u03b1\u2081 \u03b1\u2082} (\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082) {n} {pf\u2081 : suc n \u2208 \u03b1\u2081 \u21911} {pf\u2082 : 0 \u2208 \u03b1\u2082 \u21911} \u2192 \u00ac(\u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u21911\u27e7) (suc n , pf\u2081) (0 , pf\u2082))\n\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-suc-0 _ (\u27e6\u25c5\u27e7.there _ p _) = p \u2261.refl\n\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-suc-0 _ (\u27e6\u25c5\u27e7.here () _)\n\n-- cmp\u1d3a-bool : \u2200 {\u03b1} \u2113 \u2192 Name (\u03b1 \u2191 \u2113) \u2192 \ud835\udfda\n-- cmp\u1d3a-bool \u2113 x = suc (binder\u1d3a x) <= \u2113\n\n\u27e6cmp\u1d3a-bool\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 k\u1d63 \u2236 \u27e6\u2115\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) NaPa.cmp\u1d3a-bool NaPa.cmp\u1d3a-bool\n\u27e6cmp\u1d3a-bool\u27e7 _ zero _ = \u27e60\u2082\u27e7\n\u27e6cmp\u1d3a-bool\u27e7 _ (suc _) {zero , _} {zero , _} _ = \u27e61\u2082\u27e7\n\u27e6cmp\u1d3a-bool\u27e7 \u03b1\u1d63 (suc k\u1d63) {suc _ , _} {suc _ , _} x\u1d63 = \u27e6cmp\u1d3a-bool\u27e7 \u03b1\u1d63 k\u1d63 (\u27e6pred\u1d3a\u27e7-suc\u1d3a\u2191 (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) x\u1d63)\n\u27e6cmp\u1d3a-bool\u27e7 \u03b1\u1d63 (suc k\u1d63) {zero , _} {suc _ , _} x\u1d63 = \u22a5-elim (\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-0-suc (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) x\u1d63)\n\u27e6cmp\u1d3a-bool\u27e7 \u03b1\u1d63 (suc k\u1d63) {suc _ , _} {zero , _} x\u1d63 = \u22a5-elim (\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-suc-0 (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) x\u1d63)\n\n\u27e6easy-cmp\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 k\u1d63 \u2236 \u27e6\u2115\u27e7 \u27e9\u27e6\u2192\u27e7\n \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) \u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u27e6\u00f8\u27e7 \u27e6\u2191\u27e7 k\u1d63) \u27e6\u228e\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63)\n ) NaPa.easy-cmp\u1d3a NaPa.easy-cmp\u1d3a\n\u27e6easy-cmp\u1d3a\u27e7 _ zero x\u1d63 = \u27e6inr\u27e7 x\u1d63\n\u27e6easy-cmp\u1d3a\u27e7 _ (suc _) {zero , _} {zero , _} x\u1d63 = \u27e6inl\u27e7 here\u2032\n\u27e6easy-cmp\u1d3a\u27e7 \u03b1\u1d63 (suc k\u1d63) {suc _ , _} {suc _ , _} x\u1d63 = \u27e6map\u27e7 _ _ _ _ (\u27e6suc\u1d3a\u2191\u27e7 (\u27e6\u00f8\u27e7 \u27e6\u2191\u27e7 k\u1d63)) (pf-irr (\u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63))) (\u27e6easy-cmp\u1d3a\u27e7 \u03b1\u1d63 k\u1d63 (\u27e6pred\u1d3a\u27e7-suc\u1d3a\u2191 (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) x\u1d63))\n\u27e6easy-cmp\u1d3a\u27e7 \u03b1\u1d63 (suc k\u1d63) {zero , _} {suc _ , _} x\u1d63 = \u22a5-elim (\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-0-suc (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) x\u1d63)\n\u27e6easy-cmp\u1d3a\u27e7 \u03b1\u1d63 (suc k\u1d63) {suc _ , _} {zero , _} x\u1d63 = \u22a5-elim (\u00ac\u27e6Name\u27e7-\u03b1\u1d63-\u27e6\u21911\u27e7-suc-0 (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) x\u1d63)\n\n\u27e6cmp\u1d3a\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 k\u1d63 \u2236 \u27e6\u2115\u27e7 \u27e9\u27e6\u2192\u27e7\n \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6\u2191\u27e7 k\u1d63) \u27e6\u2192\u27e7 \u27e6Name\u27e7 (\u27e6\u00f8\u27e7 \u27e6\u2191\u27e7 k\u1d63) \u27e6\u228e\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63)\n ) cmp\u1d3a cmp\u1d3a\n\u27e6cmp\u1d3a\u27e7 {\u03b1\u2081} {\u03b1\u2082} \u03b1\u1d63 {k\u2081} {k\u2082} k\u1d63 {x\u2081} {x\u2082} x\u1d63 =\n \u2261.tr (\u03bb x \u2192 (\u27e6Name\u27e7 (\u27e6\u00f8\u27e7 \u27e6\u2191\u27e7 k\u1d63) \u27e6\u228e\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63)) (cmp\u1d3a {\u03b1\u2081} k\u2081 x\u2081) x)\n (NaPa.easy-cmp\u1d3a\u2257cmp\u1d3a {\u03b1\u2082} k\u2082 x\u2082)\n (\u2261.tr (\u03bb x \u2192 (\u27e6Name\u27e7 (\u27e6\u00f8\u27e7 \u27e6\u2191\u27e7 k\u1d63) \u27e6\u228e\u27e7 \u27e6Name\u27e7 (\u03b1\u1d63 \u27e6+\u1d42\u27e7 k\u1d63)) x\n (NaPa.easy-cmp\u1d3a {\u03b1\u2082} k\u2082 x\u2082))\n (NaPa.easy-cmp\u1d3a\u2257cmp\u1d3a {\u03b1\u2081} k\u2081 x\u2081) (\u27e6easy-cmp\u1d3a\u27e7 \u03b1\u1d63 k\u1d63 {x\u2081} {x\u2082} x\u1d63))\n\n\u27e6\u2286-dist-+1-\u25c5\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7\n (((b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) \u27e6+1\u27e7) \u27e6\u2286\u27e7 ((\u27e6suc\u1d2e\u27e7 b\u1d63) \u27e6\u25c5\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7))))\n \u2286-dist-+1-\u25c5 \u2286-dist-+1-\u25c5\n\u27e6\u2286-dist-+1-\u25c5\u27e7 \u03b1\u1d63 {b\u2081} {b\u2082} b\u1d63 = mk (\u03bb {\u03b7\u2081} {\u03b7\u2082} \u03b7\u2083 \u2192 helper {\u03b7\u2081} {\u03b7\u2082} \u03b7\u2083) where\n -- open \u27e6\u25c5\u27e7 b\u2081 b\u2082\n\n hper : \u2200 {x\u2081 x\u2082} (x\u1d63 : \u27e6Name\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) x\u2081 x\u2082)\n \u2192 \u27e6Name\u27e7 ((\u27e6suc\u1d2e\u27e7 b\u1d63) \u27e6\u25c5\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7))\n (suc\u1d3a x\u2081) (suc\u1d3a x\u2082)\n hper (\u27e6\u25c5\u27e7.here x\u2261b\u2081 y\u2261b\u2082) = \u27e6\u25c5\u27e7.here (\u2261.cong suc x\u2261b\u2081) (\u2261.cong suc y\u2261b\u2082)\n hper (\u27e6\u25c5\u27e7.there x\u2262b\u2081 y\u2262b\u2082 x\u223cy) = \u27e6\u25c5\u27e7.there (x\u2262b\u2081 \u2218 \u2261.cong pred) (y\u2262b\u2082 \u2218 \u2261.cong pred) x\u223cy\n\n helper : \u2200 {x\u2081 x\u2082} (x\u1d63 : \u27e6Name\u27e7 ((b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) \u27e6+1\u27e7) x\u2081 x\u2082)\n \u2192 \u27e6Name\u27e7 ((\u27e6suc\u1d2e\u27e7 b\u1d63) \u27e6\u25c5\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7))\n (coerce\u1d3a (\u2286-dist-+1-\u25c5 b\u2081) x\u2081)\n (coerce\u1d3a (\u2286-dist-+1-\u25c5 b\u2082) x\u2082)\n helper {suc x\u2081 , pf\u2081} {suc x\u2082 , pf\u2082} x\u1d63 = hper x\u1d63\n helper {zero , ()} {_ , _} _\n helper {_ , _} {zero , ()} _\n\n\u27e6\u2286-dist-\u25c5-+1\u27e7 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7\n (((\u27e6suc\u1d2e\u27e7 b\u1d63) \u27e6\u25c5\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7)) \u27e6\u2286\u27e7 ((b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) \u27e6+1\u27e7)))\n \u2286-dist-\u25c5-+1 \u2286-dist-\u25c5-+1\n\u27e6\u2286-dist-\u25c5-+1\u27e7 \u03b1\u1d63 {b\u2081} {b\u2082} b\u1d63 = mk helper where\n helper : \u2200 {x\u2081 x\u2082} (x\u1d63 : \u27e6Name\u27e7 ((\u27e6suc\u1d2e\u27e7 b\u1d63) \u27e6\u25c5\u27e7 (\u03b1\u1d63 \u27e6+1\u27e7)) x\u2081 x\u2082)\n \u2192 \u27e6Name\u27e7 ((b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) \u27e6+1\u27e7)\n (coerce\u1d3a (\u2286-dist-\u25c5-+1 b\u2081) x\u2081)\n (coerce\u1d3a (\u2286-dist-\u25c5-+1 b\u2082) x\u2082)\n helper {suc x\u2081 , pf\u2081} {suc x\u2082 , pf\u2082} x\u1d63 with x\u1d63\n ... | \u27e6\u25c5\u27e7.here x\u2261b\u2081 y\u2261b\u2082 = \u27e6\u25c5\u27e7.here (\u2261.cong (\u03bb x \u2192 x \u2238 1) x\u2261b\u2081) (\u2261.cong (\u03bb x \u2192 x \u2238 1) y\u2261b\u2082)\n ... | \u27e6\u25c5\u27e7.there x\u2262b\u2081 y\u2262b\u2082 x\u223cy = \u27e6\u25c5\u27e7.there (x\u2262b\u2081 \u2218 \u2261.cong suc) (y\u2262b\u2082 \u2218 \u2261.cong suc) x\u223cy\n helper {zero , ()} {_ , _} _\n helper {_ , _} {zero , ()} _\n\n{-\nBinder\u2261\u2115 : Binder \u2261 \u2115\nBinder\u2261\u2115 = \u2261.refl\n\npostulate\n \u27e6Binder\u27e7\u2262\u27e6\u2115\u27e7 : \u27e6Binder\u27e7 \u2262 \u27e6\u2115\u27e7\n\n\u27e6\u2261\u27e7-lem : \u2200 {a\u2081 a\u2082 a\u1d63 A\u2081 A\u2082} {A\u1d63 : \u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 A\u2081 A\u2082} {x\u2081 x\u2082} {x\u1d63 : A\u1d63 x\u2081 x\u2082} {y\u2081 y\u2082} {y\u1d63 : A\u1d63 y\u2081 y\u2082} {pf\u2081 pf\u2082} \u2192 \u27e6\u2261\u27e7 A\u1d63 x\u1d63 y\u1d63 pf\u2081 pf\u2082 \u2192 \u2200 (P : \u2200 {z\u2081 z\u2082} \u2192 A\u1d63 z\u2081 z\u2082 \u2192 Set) \u2192 P x\u1d63 \u2192 P y\u1d63\n\u27e6\u2261\u27e7-lem = {!!}\n\n\u00ac\u27e6Binder\u2261\u2115\u27e7 : \u00ac((\u27e6\u2261\u27e7 \u27e6Set\u2080\u27e7 \u27e6Binder\u27e7 \u27e6\u2115\u27e7) Binder\u2261\u2115 Binder\u2261\u2115)\n\u00ac\u27e6Binder\u2261\u2115\u27e7 = \u03bb pf \u2192 \u27e6\u2261\u27e7-lem pf (\u03bb x \u2192 \u22a5) {!!}\n-}\n\n\u27e6binder\u1d3a\u2218name\u1d2e\u27e7 : (\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7\n \u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7\n \u27e6\u2261\u27e7 \u27e6Binder\u27e7 (\u27e6binder\u1d3a\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63) (\u27e6name\u1d2e\u27e7 \u03b1\u1d63 b\u1d63)) b\u1d63) binder\u1d3a\u2218name\u1d2e binder\u1d3a\u2218name\u1d2e\n\u27e6binder\u1d3a\u2218name\u1d2e\u27e7 \u03b1\u1d63 b\u1d63 = \u2261.\u27e6refl\u27e7\n\n\u27e8_,_\u27e9\u27e6\u25c5\u27e7_ : (b\u2081 b\u2082 : Binder) \u2192 \u2200 {\u03b1\u2081 \u03b1\u2082} (\u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082) \u2192 \u27e6World\u27e7 (b\u2081 \u25c5 \u03b1\u2081) (b\u2082 \u25c5 \u03b1\u2082)\n\u27e8 b\u2081 , b\u2082 \u27e9\u27e6\u25c5\u27e7 \u03b1\u1d63 = _\u27e6\u25c5\u27e7_ {b\u2081} {b\u2082} _ \u03b1\u1d63\n\nmodule Perm (m n : \u2115) (m\u2262n : m \u2262 n) where\n mn\u1d42 : World\n mn\u1d42 = m \u25c5 n \u25c5 \u00f8\n nm\u1d42 : World\n nm\u1d42 = n \u25c5 m \u25c5 \u00f8\n perm-m-n : \u27e6World\u27e7 mn\u1d42 nm\u1d42\n perm-m-n = \u27e8 m , n \u27e9\u27e6\u25c5\u27e7 (\u27e8 n , m \u27e9\u27e6\u25c5\u27e7 \u27e6\u00f8\u27e7)\n m-n : \u2200 {m\u2208 n\u2208} \u2192 \u27e6Name\u27e7 perm-m-n (m , m\u2208) (n , n\u2208)\n m-n = here\u2032\n n-m : \u2200 {m\u2208 n\u2208} \u2192 \u27e6Name\u27e7 perm-m-n (n , n\u2208) (m , m\u2208)\n n-m = \u27e6\u25c5\u27e7.there (m\u2262n \u2218 \u2261.sym) m\u2262n {b\u2208b\u25c5 n \u00f8} {b\u2208b\u25c5 m \u00f8} here\u2032\n\n\u00ac\u27e6\ud835\udfda\u27e7-1\u2082-0\u2082 : \u00ac(\u27e6\ud835\udfda\u27e7 1\u2082 0\u2082)\n\u00ac\u27e6\ud835\udfda\u27e7-1\u2082-0\u2082 ()\n\nbinder-irrelevance : \u2200 (f : Binder \u2192 \ud835\udfda)\n \u2192 (\u27e6Binder\u27e7 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) f f\n \u2192 \u2200 {b\u2081 b\u2082} \u2192 f b\u2081 \u2261 f b\u2082\nbinder-irrelevance _ f\u1d63 = \u27e6\ud835\udfda\u27e7-Props.to-propositional (f\u1d63 _)\n\ncontrab : \u2200 (f : Binder \u2192 \ud835\udfda) {b\u2081 b\u2082}\n \u2192 f b\u2081 \u2262 f b\u2082\n \u2192 \u00ac((\u27e6Binder\u27e7 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) f f)\ncontrab f = contraposition (\u03bb f\u1d63 \u2192 binder-irrelevance f (\u03bb x\u1d63 \u2192 f\u1d63 x\u1d63))\n\nmodule Single \u03b1\u2081 \u03b1\u2082 x\u2081 x\u2082 where\n data \u211b : Name \u03b1\u2081 \u2192 Name \u03b1\u2082 \u2192 Set where\n refl : \u211b x\u2081 x\u2082\n \u211b-pres-\u2261 : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 \u211b x\u2081 x\u2082 \u2192 \u211b y\u2081 y\u2082 \u2192 (x\u2081 \u2261 y\u2081) \u21d4 (x\u2082 \u2261 y\u2082)\n \u211b-pres-\u2261 refl refl = equivalence (const \u2261.refl) (const \u2261.refl)\n \u03b1\u1d63 : \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082\n \u03b1\u1d63 = \u211b , \u211b-pres-\u2261\n\npoly-name-uniq : \u2200 (f : \u2200 {\u03b1} \u2192 Name \u03b1 \u2192 \ud835\udfda)\n (f\u1d63 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) f f)\n {\u03b1} (x : Name \u03b1) \u2192 f x \u2261 f {\u00f8 \u21911} (0 , _)\npoly-name-uniq f f\u1d63 {\u03b1} x =\n \u27e6\ud835\udfda\u27e7-Props.to-propositional (f\u1d63 {\u03b1} {\u00f8 \u21911} \u03b1\u1d63 {x} {0 , _} refl)\n where open Single \u03b1 (\u00f8 \u21911) x (0 , _)\n\npoly-name-irrelevance : \u2200 (f : \u2200 {\u03b1} \u2192 Name \u03b1 \u2192 \ud835\udfda)\n (f\u1d63 : (\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) f f)\n {\u03b1\u2081 \u03b1\u2082} (x\u2081 : Name \u03b1\u2081) (x\u2082 : Name \u03b1\u2082)\n \u2192 f x\u2081 \u2261 f x\u2082\npoly-name-irrelevance f f\u1d63 x\u2081 x\u2082 =\n \u2261.trans (poly-name-uniq f f\u1d63 x\u2081) (\u2261.sym (poly-name-uniq f f\u1d63 x\u2082))\n\nmodule Broken where\n _<=\u1d3a_ : \u2200 {\u03b1} \u2192 Name \u03b1 \u2192 Name \u03b1 \u2192 \ud835\udfda\n (m , _) <=\u1d3a (n , _) = \u2115._<=_ m n\n\n \u00ac\u27e6<=\u1d3a\u27e7 : \u00ac((\u2200\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6Name\u27e7 \u03b1\u1d63 \u27e6\u2192\u27e7 \u27e6\ud835\udfda\u27e7) _<=\u1d3a_ _<=\u1d3a_)\n \u00ac\u27e6<=\u1d3a\u27e7 \u27e6<=\u27e7 = \u00ac\u27e6\ud835\udfda\u27e7-1\u2082-0\u2082 (\u27e6<=\u27e7 perm-0-1 {0 , _} {1 , _} 0-1 {1 , _} {0 , _} 1-0)\n where open Perm 0 1 (\u03bb()) renaming (perm-m-n to perm-0-1; m-n to 0-1; n-m to 1-0)\n\n \u2286-broken : \u2200 \u03b1 b \u2192 \u03b1 \u2286 (b \u25c5 \u03b1)\n \u2286-broken \u03b1 b = mk (\u03bb b\u2032 b\u2032\u2208\u03b1 \u2192 \u2261.tr id (\u2261.sym (\u25c5-sem \u03b1 b\u2032 b)) (If\u2032 \u2115._==_ b\u2032 b then _ else b\u2032\u2208\u03b1))\n\n \u00ac\u27e6\u2286-broken\u27e7 : \u00ac((\u27e8 \u03b1\u1d63 \u2236 \u27e6World\u27e7 \u27e9\u27e6\u2192\u27e7 \u27e8 b\u1d63 \u2236 \u27e6Binder\u27e7 \u27e9\u27e6\u2192\u27e7 \u03b1\u1d63 \u27e6\u2286\u27e7 (b\u1d63 \u27e6\u25c5\u27e7 \u03b1\u1d63))\n \u2286-broken \u2286-broken)\n \u00ac\u27e6\u2286-broken\u27e7 \u27e6\u2286-broken\u27e7 = bot\n where 0\u21940 : \u27e6Name\u27e7 (\u27e8 0 , 1 \u27e9\u27e6\u25c5\u27e7 \u27e8 0 , 0 \u27e9\u27e6\u25c5\u27e7 \u27e6\u00f8\u27e7) (0 , _) (0 , _)\n 0\u21940 = un\u27e6\u2286\u27e7 (\u27e6\u2286-broken\u27e7 (\u27e8 0 , 0 \u27e9\u27e6\u25c5\u27e7 \u27e6\u00f8\u27e7) _) {0 , _} {0 , _} here\u2032\n\n bot : \u22a5\n bot with 0\u21940\n bot | \u27e6\u25c5\u27e7.here _ ()\n bot | \u27e6\u25c5\u27e7.there 0\u22620 _ _ = 0\u22620 \u2261.refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"b5c551446ac6bd8508e98e0b22480ff263a62540","subject":"implicit arguments","message":"implicit arguments\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"typed-expansion.agda","new_file":"typed-expansion.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\n\nmodule typed-expansion where\n mutual\n typed-expansion-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4\n typed-expansion-synth ESConst = TAConst\n typed-expansion-synth (ESVar x\u2081) = TAVar x\u2081\n typed-expansion-synth (ESLam x\u2081 ex) = {!!}\n typed-expansion-synth (ESAp1 x x\u2081 x\u2082 x\u2083) = {!!}\n typed-expansion-synth (ESAp2 x ex x\u2081 x\u2082) = {!!}\n typed-expansion-synth (ESAp3 x ex x\u2081) = {!!}\n typed-expansion-synth ESEHole = {!!}\n typed-expansion-synth (ESNEHole ex) = {!!}\n typed-expansion-synth (ESAsc1 x x\u2081) = {!!}\n typed-expansion-synth (ESAsc2 x) = {!!}\n\n typed-expansion-ana : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u03c4 ~ \u03c4') \u00d7 (\u0394 , \u0393 \u22a2 d :: \u03c4')\n typed-expansion-ana (EALam x\u2081 ex) = {!!}\n typed-expansion-ana (EASubsume x x\u2081 x\u2082 x\u2083) = {!!}\n typed-expansion-ana EAEHole = {!!}\n typed-expansion-ana (EANEHole x x\u2081) = {!!}\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\n\nmodule typed-expansion where\n mutual\n typed-expansion-synth : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4\n typed-expansion-synth \u0393 .c .b .c .(\u03bb _ \u2192 None) ESConst = TAConst\n typed-expansion-synth _ _ \u03c4 _ .(\u03bb _ \u2192 None) ESVar = TAVar\n typed-expansion-synth \u0393 _ _ _ .(\u03bb _ \u2192 None) (ESLam D) with typed-expansion-synth _ _ _ _ _ D\n ... | ih = TALam {!!}\n typed-expansion-synth \u0393 _ .\u2987\u2988 _ _ (ESAp1 y x x\u2081 x\u2082) = {!!}\n typed-expansion-synth \u0393 _ \u03c4 _ _ (ESAp2 y D x x\u2081) = {!!}\n typed-expansion-synth \u0393 _ \u03c4 _ _ (ESAp3 y D x) = {!!}\n typed-expansion-synth \u0393 _ .\u2987\u2988 _ _ ESEHole = TAEHole {!!}\n typed-expansion-synth \u0393 _ .\u2987\u2988 _ _ (ESNEHole D) = TANEHole {!!} {!!}\n typed-expansion-synth \u0393 _ \u03c4 _ \u0394 (ESAsc1 x x\u2081) = {!!}\n typed-expansion-synth \u0393 _ \u03c4 d \u0394 (ESAsc2 x) = {!!}\n\n typed-expansion-ana : (\u0393 : tctx) (e : hexp) (\u03c4 \u03c4' : htyp) (d : dhexp) (\u0394 : hctx) \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u03c4 ~ \u03c4') \u00d7 (\u0394 , \u0393 \u22a2 d :: \u03c4')\n typed-expansion-ana \u0393 _ _ _ _ \u0394 (EALam D) = (TCArr TCRefl {!!}) , {!!}\n typed-expansion-ana \u0393 e \u03c4 \u03c4' d \u0394 (EASubsume x x\u2081 x\u2082 x\u2083) = {!!}\n typed-expansion-ana \u0393 _ \u03c4' .\u03c4' _ _ EAEHole = TCRefl , {!!}\n typed-expansion-ana \u0393 _ \u03c4' .\u03c4' _ _ (EANEHole x x\u2081) = TCRefl , {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"5dcb5ee53faebcf8147bafebc216b8e23aeb02d8","subject":"removing comments, whitespace","message":"removing comments, whitespace\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"lemmas-subst-ta.agda","new_file":"lemmas-subst-ta.agda","new_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import lemmas-disjointness\nopen import structural\n\nmodule lemmas-subst-ta where\n lem-subst-\u03c3 : \u2200{\u0394 x \u0393 \u03c41 \u03c3 \u0393' d } \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 Subst d x \u03c3 :s: \u0393'\n lem-subst-\u03c3 s wt = STASubst s wt\n\n -- todo: i'm worried this may actually be false without knowing that x \u2260\n -- y, but that's kind of what we usually mean on paper anyway\n exchange-subst-\u0393 : \u2200{\u0394 \u0393 x y \u03c41 \u03c42 \u03c3 \u0393'} \u2192\n x \u2260 y \u2192\n \u0394 , (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) \u22a2 \u03c3 :s: \u0393'\n exchange-subst-\u0393 {\u0394} {\u0393} {x} {y} {\u03c41} {\u03c42} {\u03c3} {\u0393'} x\u2260y xy = tr (\u03bb qq \u2192 \u0394 , qq \u22a2 \u03c3 :s: \u0393') (funext swap) xy\n where\n swap : (z : Nat) \u2192 (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) z == (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) z\n swap = {!!}\n\n mutual\n data envfresh : Nat \u2192 env \u2192 Set where\n EFId : \u2200{x \u0393} \u2192 x # \u0393 \u2192 envfresh x (Id \u0393)\n EFSubst : \u2200{x d \u03c3 y} \u2192 fresh x d\n \u2192 envfresh x \u03c3\n \u2192 x \u2260 y\n \u2192 envfresh x (Subst d y \u03c3)\n\n data fresh : Nat \u2192 dhexp \u2192 Set where\n FConst : \u2200{x} \u2192 fresh x c\n FVar : \u2200{x y} \u2192 x \u2260 y \u2192 fresh x (X y)\n FLam : \u2200{x y \u03c4 d} \u2192 x \u2260 y \u2192 fresh x d \u2192 fresh x (\u00b7\u03bb y [ \u03c4 ] d)\n FHole : \u2200{x u \u03c3} \u2192 envfresh x \u03c3 \u2192 fresh x (\u2987\u2988\u27e8 u , \u03c3 \u27e9)\n FNEHole : \u2200{x d u \u03c3} \u2192 envfresh x \u03c3 \u2192 fresh x d \u2192 fresh x (\u2987 d \u2988\u27e8 u , \u03c3 \u27e9)\n FAp : \u2200{x d1 d2} \u2192 fresh x d1 \u2192 fresh x d2 \u2192 fresh x (d1 \u2218 d2)\n FCast : \u2200{x d \u03c41 \u03c42} \u2192 fresh x d \u2192 fresh x (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9)\n FFailedCast : \u2200{x d \u03c41 \u03c42} \u2192 fresh x d \u2192 fresh x (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n\n mutual\n weaken-subst-\u0393 : \u2200{ x \u0393 \u0394 \u03c3 \u0393' \u03c4} \u2192\n envfresh x \u03c3 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , (\u0393 ,, (x , \u03c4)) \u22a2 \u03c3 :s: \u0393'\n weaken-subst-\u0393 {\u0393 = \u0393} (EFId x\u2081) (STAId x\u2082) = STAId (\u03bb x \u03c4 x\u2083 \u2192 x\u2208\u222al \u0393 _ x \u03c4 (x\u2082 x \u03c4 x\u2083) )\n weaken-subst-\u0393 {x = x} {\u0393 = \u0393} (EFSubst x\u2081 efrsh x\u2082) (STASubst {y = y} {\u03c4 = \u03c4'} subst x\u2083) =\n STASubst (exchange-subst-\u0393 {\u0393 = \u0393} (flip x\u2082) (weaken-subst-\u0393 {\u0393 = \u0393 ,, (y , \u03c4')} efrsh subst))\n (weaken-ta x\u2081 x\u2083)\n\n weaken-ta : \u2200{x \u0393 \u0394 d \u03c4 \u03c4'} \u2192\n fresh x d \u2192 -- x # \u0393 ?\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 ,, (x , \u03c4') \u22a2 d :: \u03c4\n weaken-ta _ TAConst = TAConst\n weaken-ta {x} {\u0393} {_} {_} {\u03c4} {\u03c4'} (FVar x\u2082) (TAVar x\u2083) = TAVar (x\u2208\u222al \u0393 (\u25a0 (x , \u03c4')) _ _ x\u2083)\n weaken-ta {x = x} frsh (TALam {x = y} x\u2082 wt) with natEQ x y\n weaken-ta (FLam x\u2081 x\u2082) (TALam x\u2083 wt) | Inl refl = abort (x\u2081 refl)\n weaken-ta {\u0393 = \u0393} {\u03c4' = \u03c4'} (FLam x\u2081 x\u2083) (TALam {x = y} x\u2084 wt) | Inr x\u2082 = TALam (apart-parts \u0393 _ _ x\u2084 (apart-singleton (flip x\u2081))) (weaken-ta {\u0393 = {!\u25a0 (y , ? )!} \u222a \u0393} x\u2083 wt)\n weaken-ta (FAp frsh frsh\u2081) (TAAp wt wt\u2081) = TAAp (weaken-ta frsh wt) (weaken-ta frsh\u2081 wt\u2081)\n weaken-ta (FHole x\u2081) (TAEHole x\u2082 x\u2083) = TAEHole x\u2082 (weaken-subst-\u0393 x\u2081 x\u2083)\n weaken-ta (FNEHole x\u2081 frsh) (TANEHole x\u2082 wt x\u2083) = TANEHole x\u2082 (weaken-ta frsh wt) (weaken-subst-\u0393 x\u2081 x\u2083)\n weaken-ta (FCast frsh) (TACast wt x\u2081) = TACast (weaken-ta frsh wt) x\u2081\n weaken-ta (FFailedCast frsh) (TAFailedCast wt x\u2081 x\u2082 x\u2083) = TAFailedCast (weaken-ta frsh wt) x\u2081 x\u2082 x\u2083\n\n lem-subst : \u2200{\u0394 \u0393 x \u03c41 d1 \u03c4 d2 } \u2192\n x # \u0393 \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 d1 :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 [ d2 \/ x ] d1 :: \u03c4\n lem-subst apt TAConst wt2 = TAConst\n lem-subst {x = x} apt (TAVar {x = x'} x\u2082) wt2 with natEQ x' x\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inl refl with lem-apart-union-eq {\u0393 = \u0393} apt x\u2083\n lem-subst apt (TAVar x\u2083) wt2 | Inl refl | refl = wt2\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inr x\u2082 = TAVar (lem-neq-union-eq {\u0393 = \u0393} x\u2082 x\u2083)\n lem-subst {\u0393 = \u0393} {x = x} apt (TALam {x = y} x\u2082 wt1) wt2 -- = {!weaken-ta ? (TALam x\u2082 wt1)!}\n with natEQ y x\n lem-subst {\u0393 = \u0393} {x = x} apt (TALam x\u2083 wt1) wt2 | Inl refl = abort ((\u03c01(lem-union-none {\u0393 = \u0393} x\u2083)) refl)\n lem-subst {\u0393 = \u0393} apt (TALam x\u2083 wt1) wt2 | Inr x\u2082\n with lem-union-none {\u0393 = \u0393} x\u2083 -- | lem-subst apt wt1 -- probably not the straight IH; need to weaken\n ... | neq , r = {!!} -- TALam r (lem-subst {!!} (weaken-ta {!!} {!!}) (weaken-ta r wt2))\n lem-subst apt (TAAp wt1 wt2) wt3 = TAAp (lem-subst apt wt1 wt3) (lem-subst apt wt2 wt3)\n lem-subst apt (TAEHole in\u0394 sub) wt2 = TAEHole in\u0394 (lem-subst-\u03c3 sub wt2)\n lem-subst apt (TANEHole x\u2081 wt1 x\u2082) wt2 = TANEHole x\u2081 (lem-subst apt wt1 wt2) (lem-subst-\u03c3 x\u2082 wt2)\n lem-subst apt (TACast wt1 x\u2081) wt2 = TACast (lem-subst apt wt1 wt2) x\u2081\n lem-subst apt (TAFailedCast wt1 x\u2081 x\u2082 x\u2083) wt2 = TAFailedCast (lem-subst apt wt1 wt2) x\u2081 x\u2082 x\u2083\n","old_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import lemmas-disjointness\nopen import structural\n\nmodule lemmas-subst-ta where\n lem-subst-\u03c3 : \u2200{\u0394 x \u0393 \u03c41 \u03c3 \u0393' d } \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 Subst d x \u03c3 :s: \u0393'\n lem-subst-\u03c3 s wt = STASubst s wt\n\n -- todo: i'm worried this may actually be false without knowing that x \u2260\n -- y, but that's kind of what we usually mean on paper anyway\n exchange-subst-\u0393 : \u2200{\u0394 \u0393 x y \u03c41 \u03c42 \u03c3 \u0393'} \u2192\n x \u2260 y \u2192\n \u0394 , (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) \u22a2 \u03c3 :s: \u0393'\n exchange-subst-\u0393 {\u0394} {\u0393} {x} {y} {\u03c41} {\u03c42} {\u03c3} {\u0393'} x\u2260y xy = tr (\u03bb qq \u2192 \u0394 , qq \u22a2 \u03c3 :s: \u0393') (funext swap) xy\n where\n swap : (z : Nat) \u2192 (\u0393 ,, (x , \u03c41) ,, (y , \u03c42)) z == (\u0393 ,, (y , \u03c42) ,, (x , \u03c41)) z\n swap = {!!}\n\n mutual\n data envfresh : Nat \u2192 env \u2192 Set where\n EFId : \u2200{x \u0393} \u2192 x # \u0393 \u2192 envfresh x (Id \u0393)\n EFSubst : \u2200{x d \u03c3 y} \u2192 fresh x d\n \u2192 envfresh x \u03c3\n \u2192 x \u2260 y -- todo: maybe?\n \u2192 envfresh x (Subst d y \u03c3)\n\n data fresh : Nat \u2192 dhexp \u2192 Set where\n FConst : \u2200{x} \u2192 fresh x c\n FVar : \u2200{x y} \u2192 x \u2260 y \u2192 fresh x (X y)\n FLam : \u2200{x y \u03c4 d} \u2192 x \u2260 y \u2192 fresh x d \u2192 fresh x (\u00b7\u03bb y [ \u03c4 ] d)\n FHole : \u2200{x u \u03c3} \u2192 envfresh x \u03c3 \u2192 fresh x (\u2987\u2988\u27e8 u , \u03c3 \u27e9)\n FNEHole : \u2200{x d u \u03c3} \u2192 envfresh x \u03c3 \u2192 fresh x d \u2192 fresh x (\u2987 d \u2988\u27e8 u , \u03c3 \u27e9)\n FAp : \u2200{x d1 d2} \u2192 fresh x d1 \u2192 fresh x d2 \u2192 fresh x (d1 \u2218 d2)\n FCast : \u2200{x d \u03c41 \u03c42} \u2192 fresh x d \u2192 fresh x (d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9)\n FFailedCast : \u2200{x d \u03c41 \u03c42} \u2192 fresh x d \u2192 fresh x (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n\n mutual\n weaken-subst-\u0393 : \u2200{ x \u0393 \u0394 \u03c3 \u0393' \u03c4} \u2192\n envfresh x \u03c3 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , (\u0393 ,, (x , \u03c4)) \u22a2 \u03c3 :s: \u0393'\n weaken-subst-\u0393 {\u0393 = \u0393} (EFId x\u2081) (STAId x\u2082) = STAId (\u03bb x \u03c4 x\u2083 \u2192 x\u2208\u222al \u0393 _ x \u03c4 (x\u2082 x \u03c4 x\u2083) )\n weaken-subst-\u0393 {x = x} {\u0393 = \u0393} (EFSubst x\u2081 efrsh x\u2082) (STASubst {y = y} {\u03c4 = \u03c4'} subst x\u2083) =\n STASubst (exchange-subst-\u0393 {\u0393 = \u0393} (flip x\u2082) (weaken-subst-\u0393 {\u0393 = \u0393 ,, (y , \u03c4')} efrsh subst))\n (weaken-ta x\u2081 x\u2083)\n\n weaken-ta : \u2200{x \u0393 \u0394 d \u03c4 \u03c4'} \u2192\n fresh x d \u2192\n -- x # \u0393 ?\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 ,, (x , \u03c4') \u22a2 d :: \u03c4\n weaken-ta _ TAConst = TAConst\n weaken-ta {x} {\u0393} {_} {_} {\u03c4} {\u03c4'} (FVar x\u2082) (TAVar x\u2083) = TAVar (x\u2208\u222al \u0393 (\u25a0 (x , \u03c4')) _ _ x\u2083)\n weaken-ta {x = x} frsh (TALam {x = y} x\u2082 wt) with natEQ x y\n weaken-ta (FLam x\u2081 x\u2082) (TALam x\u2083 wt) | Inl refl = abort (x\u2081 refl)\n weaken-ta {\u0393 = \u0393} {\u03c4' = \u03c4'} (FLam x\u2081 x\u2083) (TALam {x = y} x\u2084 wt) | Inr x\u2082 = TALam (apart-parts \u0393 _ _ x\u2084 (apart-singleton (flip x\u2081))) (weaken-ta {\u0393 = {!\u25a0 (y , ? )!} \u222a \u0393} x\u2083 wt)\n weaken-ta (FAp frsh frsh\u2081) (TAAp wt wt\u2081) = TAAp (weaken-ta frsh wt) (weaken-ta frsh\u2081 wt\u2081)\n weaken-ta (FHole x\u2081) (TAEHole x\u2082 x\u2083) = TAEHole x\u2082 (weaken-subst-\u0393 x\u2081 x\u2083) -- second argument rel to todo above\n weaken-ta (FNEHole x\u2081 frsh) (TANEHole x\u2082 wt x\u2083) = TANEHole x\u2082 (weaken-ta frsh wt) (weaken-subst-\u0393 x\u2081 x\u2083) -- second argument rel to todo above\n weaken-ta (FCast frsh) (TACast wt x\u2081) = TACast (weaken-ta frsh wt) x\u2081\n weaken-ta (FFailedCast frsh) (TAFailedCast wt x\u2081 x\u2082 x\u2083) = TAFailedCast (weaken-ta frsh wt) x\u2081 x\u2082 x\u2083\n\n lem-subst : \u2200{\u0394 \u0393 x \u03c41 d1 \u03c4 d2 } \u2192\n x # \u0393 \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 d1 :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 [ d2 \/ x ] d1 :: \u03c4\n lem-subst apt TAConst wt2 = TAConst\n lem-subst {x = x} apt (TAVar {x = x'} x\u2082) wt2 with natEQ x' x\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inl refl with lem-apart-union-eq {\u0393 = \u0393} apt x\u2083\n lem-subst apt (TAVar x\u2083) wt2 | Inl refl | refl = wt2\n lem-subst {\u0393 = \u0393} apt (TAVar x\u2083) wt2 | Inr x\u2082 = TAVar (lem-neq-union-eq {\u0393 = \u0393} x\u2082 x\u2083)\n lem-subst {\u0393 = \u0393} {x = x} apt (TALam {x = y} x\u2082 wt1) wt2 -- = {!weaken-ta ? (TALam x\u2082 wt1)!}\n with natEQ y x\n lem-subst {\u0393 = \u0393} {x = x} apt (TALam x\u2083 wt1) wt2 | Inl refl = abort ((\u03c01(lem-union-none {\u0393 = \u0393} x\u2083)) refl)\n lem-subst {\u0393 = \u0393} apt (TALam x\u2083 wt1) wt2 | Inr x\u2082\n with lem-union-none {\u0393 = \u0393} x\u2083 -- | lem-subst apt wt1 -- probably not the straight IH; need to weaken\n ... | neq , r = {!!} -- TALam r (lem-subst {!!} (weaken-ta {!!} {!!}) (weaken-ta r wt2))\n lem-subst apt (TAAp wt1 wt2) wt3 = TAAp (lem-subst apt wt1 wt3) (lem-subst apt wt2 wt3)\n lem-subst apt (TAEHole in\u0394 sub) wt2 = TAEHole in\u0394 (lem-subst-\u03c3 sub wt2)\n lem-subst apt (TANEHole x\u2081 wt1 x\u2082) wt2 = TANEHole x\u2081 (lem-subst apt wt1 wt2) (lem-subst-\u03c3 x\u2082 wt2)\n lem-subst apt (TACast wt1 x\u2081) wt2 = TACast (lem-subst apt wt1 wt2) x\u2081\n lem-subst apt (TAFailedCast wt1 x\u2081 x\u2082 x\u2083) wt2 = TAFailedCast (lem-subst apt wt1 wt2) x\u2081 x\u2082 x\u2083\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f91b15c3d4b631cfaa10d6b9f0ec06f4e556c1d3","subject":"IDesc model: cool down on universe polymorphism on Desc","message":"IDesc model: cool down on universe polymorphism on Desc\n","repos":"larrytheliquid\/pigit,mietek\/epigram2,mietek\/epigram2","old_file":"models\/IDesc.agda","new_file":"models\/IDesc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\nrecord One {i : Level} : Set i where\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set1 where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : (I : Set) -> IDesc I -> (I -> Set) -> Set\ndesc I (var i) P = P i\ndesc I (const X) P = X\ndesc I (prod D D') P = desc I D P * desc I D' P\ndesc I (sigma S T) P = Sigma S (\\s -> desc I (T s) P)\ndesc I (pi S T) P = (s : S) -> desc I (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\n{-\ndata IMu (I : Set)(R : I -> IDesc I) : IDesc I -> Set1 where\n rec : (i : I) -> IMu I R (R i) -> IMu I R (var i)\n lambda : (S : Set i)(D : S -> IDesc I) -> ((s : S) -> IMu I R (D s)) -> IMu I R (pi S D)\n pair : (S : Set i)(D : S -> IDesc I) -> (Sigma S (\\s -> IMu I R (D s))) -> IMu I R (sigma S D)","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\nrecord One {i : Level} : Set i where\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {i : Level}(I : Set i) : Set (suc i) where\n var : I -> IDesc I\n const : Set i -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set i) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set i) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {i : Level}(I : Set i) -> IDesc I -> (I -> Set i) -> Set i\ndesc I (var i) P = P i\ndesc I (const X) P = X\ndesc I (prod D D') P = desc I D P * desc I D' P\ndesc I (sigma S T) P = Sigma S (\\s -> desc I (T s) P)\ndesc I (pi S T) P = (s : S) -> desc I (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {i : Level}(I : Set i)(R : I -> IDesc I) : IDesc I -> Set (suc i) where\n rec : (i : I) -> IMu I R (R i) -> IMu I R (var i)\n lambda : (S : Set i)(D : S -> IDesc I) -> ((s : S) -> IMu I R (D s)) -> IMu I R (pi S D)\n pair : (S : Set i)(D : S -> IDesc I) -> (Sigma S (\\s -> IMu I R (D s))) -> IMu I R (sigma S D)","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"de962511ed25886f7e30cde9537dbe0b8146958d","subject":"Boring.","message":"Boring.\n\nIgnore-this: 29b776acd29766be249fdded6f93d79f\n\ndarcs-hash:20100414150814-3bd4e-916ed4ed8f8a00a1edd98792247972840d63fb14.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Test\/Succeed\/LogicalConstants.agda","new_file":"Test\/Succeed\/LogicalConstants.agda","new_contents":"module Test.Succeed.LogicalConstants where\n\ninfix 4 _\u2261_\n\npostulate\n D : Set\n _\u2261_ : D \u2192 D \u2192 Set\n\n------------------------------------------------------------------------------\n-- The conjuction data type\n\nmodule Conjunction where\n\n infixr 4 _,_\n infixr 2 _\u00d7_\n\n -- We want to use the product type to define the conjunction type\n record _\u00d7_ (A B : Set) : Set where\n constructor _,_\n field\n proj\u2081 : A\n proj\u2082 : B\n\n -- N.B. It is not necessary to add the constructor _,_, nor the fields\n -- proj\u2081, proj\u2082 as hints, because the ATP implements it.\n\n _\u2227_ : (A B : Set) \u2192 Set\n A \u2227 B = A \u00d7 B\n\n postulate\n P : D \u2192 Set\n\n -- Testing the conjunction data constructor\n postulate\n testAndDataConstructor : {m n : D} \u2192 P m \u2192 P n \u2192 P m \u2227 P n\n {-# ATP prove testAndDataConstructor #-}\n\n -- Testing the first projection\n postulate\n testProj\u2081 : {m n : D} \u2192 P m \u2227 P n \u2192 P m\n {-# ATP prove testProj\u2081 #-}\n\n -- Testing the second projection\n postulate\n testProj\u2082 : {m n : D} \u2192 P m \u2227 P n \u2192 P n\n {-# ATP prove testProj\u2082 #-}\n\n------------------------------------------------------------------------------\n-- The negation\n\nmodule Negation where\n\n infix 3 \u00ac\n\n data \u22a5 : Set where\n\n \u00ac : Set \u2192 Set\n \u00ac A = A \u2192 \u22a5\n\n postulate\n true : D\n false : D\n\n postulate\n true\u2260false : \u00ac (true \u2261 false)\n {-# ATP axiom true\u2260false #-}\n\n postulate\n testContradiction : (d : D) \u2192 true \u2261 false \u2192 d \u2261 true\n {-# ATP prove testContradiction #-}\n","old_contents":"module Test.Succeed.LogicalConstants where\n\ninfix 4 _\u2261_\n\npostulate\n D : Set\n _\u2261_ : D \u2192 D \u2192 Set\n\n------------------------------------------------------------------------------\n-- The conjuction data type\n\nmodule Conjunction where\n\n infixr 4 _,_\n infixr 2 _\u00d7_\n\n -- We want to use the product type to define the conjunction type\n record _\u00d7_ (A B : Set) : Set where\n constructor _,_\n field\n proj\u2081 : A\n proj\u2082 : B\n\n -- N.B. It is not necessary to add the constructor _,_, nor the fields\n -- proj\u2081, proj\u2082 as hints, because the ATP implements it.\n\n _\u2227_ : (A B : Set) \u2192 Set\n A \u2227 B = A \u00d7 B\n\n postulate\n P : D \u2192 Set\n\n -- Testing the conjunction data constructor\n postulate\n testAndDataConstructor : {m n : D} \u2192 P m \u2192 P n \u2192 P m \u2227 P n\n {-# ATP prove testAndDataConstructor #-}\n\n -- Testing the first projection\n postulate\n testProj\u2081 : {m n : D} \u2192 P m \u2227 P n \u2192 P m\n {-# ATP prove testProj\u2081 #-}\n\n-- Testing the second projection\n postulate\n testProj\u2082 : {m n : D} \u2192 P m \u2227 P n \u2192 P n\n {-# ATP prove testProj\u2082 #-}\n\n------------------------------------------------------------------------------\n-- The negation\n\nmodule Negation where\n\n infix 3 \u00ac\n\n data \u22a5 : Set where\n\n \u00ac : Set \u2192 Set\n \u00ac A = A \u2192 \u22a5\n\n postulate\n true : D\n false : D\n\n postulate\n true\u2260false : \u00ac (true \u2261 false)\n {-# ATP axiom true\u2260false #-}\n\n postulate\n testContradiction : (d : D) \u2192 true \u2261 false \u2192 d \u2261 true\n {-# ATP prove testContradiction #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"72fbb40433f647484e1e61bb7541542a115c6027","subject":"sem-sec: reductions are now abstracted from the notion of function","message":"sem-sec: reductions are now abstracted from the notion of function\n","repos":"crypto-agda\/crypto-agda","old_file":"otp-sem-sec.agda","new_file":"otp-sem-sec.agda","new_contents":"module otp-sem-sec where\n\nimport Level as L\nopen import Function\nopen import Data.Nat.NP\nopen import Data.Bits\nopen import Data.Bool\nopen import Data.Bool.Properties\nopen import Data.Vec hiding (_>>=_)\nopen import Data.Product.NP hiding (_\u27e6\u00d7\u27e7_)\nopen import Relation.Nullary\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\nopen import flipbased-implem\nopen \u2261-Reasoning\nopen import Data.Unit using (\u22a4)\nopen import composable\nopen import vcomp\nopen import forkable\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nmodule FunctionExtra where\n _***_ : \u2200 {A B C D : Set} \u2192 (A \u2192 B) \u2192 (C \u2192 D) \u2192 A \u00d7 C \u2192 B \u00d7 D\n (f *** g) (x , y) = (f x , g y)\n -- Fanout\n _&&&_ : \u2200 {A B C : Set} \u2192 (A \u2192 B) \u2192 (A \u2192 C) \u2192 A \u2192 B \u00d7 C\n (f &&& g) x = (f x , g x)\n _>>>_ : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c} \u2192\n (A \u2192 B) \u2192 (B \u2192 C) \u2192 (A \u2192 C)\n f >>> g = g \u2218 f\n infixr 1 _>>>_\n\nmodule BitsExtra where\n splitAt\u2032 : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k \u00d7 Bits n\n splitAt\u2032 k xs = case splitAt k xs of \u03bb { (ys , zs , _) \u2192 (ys , zs) }\n\n vnot\u2218vnot : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\n vnot\u2218vnot [] = refl\n vnot\u2218vnot (x \u2237 xs) rewrite not-involutive x = cong (_\u2237_ x) (vnot\u2218vnot xs)\n\nopen BitsExtra\n\nCoins = \u2115\n\nrecord PrgDist : Set\u2081 where\n constructor mk\n field\n _]-[_ : \u2200 {c} (f g : \u21ba c Bit) \u2192 Set\n ]-[-cong : \u2200 {c} {f f' g g' : \u21ba c Bit} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f ]-[ f' \u2192 g ]-[ g'\n\n breaks : \u2200 {c} (EXP : Bit \u2192 \u21ba c Bit) \u2192 Set\n breaks \u2141 = \u2141 0b ]-[ \u2141 1b\n\n _\u2257\u2141_ : \u2200 {c} (\u2141\u2080 \u2141\u2081 : Bit \u2192 \u21ba c Bit) \u2192 Set\n \u2141\u2080 \u2257\u2141 \u2141\u2081 = \u2200 b \u2192 \u2141\u2080 b \u2257\u21ba \u2141\u2081 b\n\n \u2257\u2141-trans : \u2200 {c} \u2192 Transitive (_\u2257\u2141_ {c})\n \u2257\u2141-trans p q b R = trans (p b R) (q b R)\n\n -- An wining adversary for game \u2141\u2080 reduces to a wining adversary for game \u2141\u2081\n _\u21d3_ : \u2200 {c\u2080 c\u2081} (\u2141\u2080 : Bit \u2192 \u21ba c\u2080 Bit) (\u2141\u2081 : Bit \u2192 \u21ba c\u2081 Bit) \u2192 Set\n \u2141\u2080 \u21d3 \u2141\u2081 = breaks \u2141\u2080 \u2192 breaks \u2141\u2081\n\n extensional-reduction : \u2200 {c} {\u2141\u2080 \u2141\u2081 : Bit \u2192 \u21ba c Bit}\n \u2192 \u2141\u2080 \u2257\u2141 \u2141\u2081 \u2192 \u2141\u2080 \u21d3 \u2141\u2081\n extensional-reduction same-games = ]-[-cong (same-games 0b) (same-games 1b)\n\nmodule Guess (Power : Set) (coins : Power \u2192 Coins) (prgDist : PrgDist) where\n open PrgDist prgDist\n\n GuessAdv : Coins \u2192 Set\n GuessAdv c = \u21ba c Bit\n\n runGuess\u2141 : \u2200 {ca} (A : GuessAdv ca) (b : Bit) \u2192 \u21ba ca Bit\n runGuess\u2141 A _ = A\n\n -- An oracle: an adversary who can break the guessing game.\n Oracle : Power \u2192 Set\n Oracle power = \u2203 (\u03bb (A : GuessAdv (coins power)) \u2192 breaks (runGuess\u2141 A))\n\n -- Any adversary cannot do better than a random guess.\n GuessSec : Power \u2192 Set\n GuessSec power = \u2200 (A : GuessAdv (coins power)) \u2192 \u00ac(breaks (runGuess\u2141 A))\n\nrecord FlatFuns (Power : Set) {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n `Bits : \u2115 \u2192 T\n `Bit : T\n _`\u00d7_ : T \u2192 T \u2192 T\n \u27e8_\u27e9_\u219d_ : Power \u2192 T \u2192 T \u2192 Set\n infixr 2 _`\u00d7_\n infix 0 \u27e8_\u27e9_\u219d_\n\nrecord PowerOps (Power : Set) : Set where\n constructor mk\n infixr 1 _>>>\u1d56_\n infixr 3 _***\u1d56_\n field\n id\u1d56 : Power\n _>>>\u1d56_ _***\u1d56_ : Power \u2192 Power \u2192 Power\n\nconstPowerOps : PowerOps \u22a4\nconstPowerOps = _\n\nrecord FlatFunsOps {P t} {T : Set t} (powerOps : PowerOps P) (\u266dFuns : FlatFuns P T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n open PowerOps powerOps\n field\n idO : \u2200 {A p} \u2192 \u27e8 p \u27e9 A \u219d A\n isIComposable : IComposable {I = \u22a4} _>>>\u1d56_ \u27e8_\u27e9_\u219d_\n isVIComposable : VIComposable {I = \u22a4} _***\u1d56_ _`\u00d7_ \u27e8_\u27e9_\u219d_\n open FlatFuns \u266dFuns public\n open PowerOps powerOps public\n open IComposable isIComposable public\n open VIComposable isVIComposable public\n\nfun\u266dFuns : FlatFuns \u22a4 Set\nfun\u266dFuns = mk Bits Bit _\u00d7_ (\u03bb _ A B \u2192 A \u2192 B)\n\nfun\u266dOps : FlatFunsOps _ fun\u266dFuns\nfun\u266dOps = mk id funComp funVComp\n\nmodule AbsSemSec (|M| |C| : \u2115) {FunPower : Set} {t} {T : Set t}\n (\u266dFuns : FlatFuns FunPower T) where\n\n record Power : Set where\n constructor mk\n field\n p\u2080 p\u2081 : FunPower\n |R| : Coins\n coins = Power.|R|\n\n open FlatFuns \u266dFuns\n\n M = `Bits |M|\n C = `Bits |C|\n\n record AbsSemSecAdv (p : Power) : Set where\n constructor mk\n\n open Power p\n\n field\n {|S|} : \u2115\n\n S = `Bits |S|\n R = `Bits |R|\n\n field\n step\u2080 : \u27e8 p\u2080 \u27e9 R \u219d (M `\u00d7 M) `\u00d7 S\n step\u2081 : \u27e8 p\u2081 \u27e9 C `\u00d7 S \u219d `Bit\n\n SemSecReduction : \u2200 (f : Power \u2192 Power) \u2192 Set\n SemSecReduction f = \u2200 {p} \u2192 AbsSemSecAdv p \u2192 AbsSemSecAdv (f p)\n\n-- Here we use Agda functions for FlatFuns and \u22a4 for power.\nmodule FunSemSec (prgDist : PrgDist) (|M| |C| : \u2115) where\n open PrgDist prgDist\n open AbsSemSec |M| |C| fun\u266dFuns\n open PowerOps constPowerOps\n open FlatFunsOps fun\u266dOps\n\n M\u00b2 = Bit \u2192 M\n\n Enc : \u2200 cc \u2192 Set\n Enc cc = M \u2192 \u21ba cc C\n\n Tr : (cc\u2080 cc\u2081 : Coins) \u2192 Set\n Tr cc\u2080 cc\u2081 = Enc cc\u2080 \u2192 Enc cc\u2081\n\n FunSemSecAdv : Coins \u2192 Set\n FunSemSecAdv |R| = AbsSemSecAdv (mk _ _ |R|)\n\n module FunSemSecAdv {|R|} (A : FunSemSecAdv |R|) where\n open AbsSemSecAdv A public\n\n step\u2080F : R \u2192 (M\u00b2 \u00d7 S)\n step\u2080F = step\u2080 >>> proj *** idO\n\n step\u2080\u21ba : \u21ba |R| (M\u00b2 \u00d7 S)\n step\u2080\u21ba = mk step\u2080F\n\n step\u2081F : S \u2192 C \u2192 Bit\n step\u2081F s c = step\u2081 (c , s)\n\n -- Returing 0 means Chal wins, Adv looses\n -- 1 means Adv wins, Chal looses\n runSemSec : \u2200 {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b \u2192 \u21ba (ca + cc) Bit\n runSemSec E A b\n = A-step\u2080 >>= \u03bb { (m , s) \u2192 map\u21ba (A-step\u2081 s) (E (m b)) }\n where open FunSemSecAdv A renaming (step\u2080\u21ba to A-step\u2080; step\u2081F to A-step\u2081)\n\n _\u21c4_ : \u2200 {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b \u2192 \u21ba (ca + cc) Bit\n _\u21c4_ = runSemSec\n\n runAdv : \u2200 {|R|} \u2192 FunSemSecAdv |R| \u2192 C \u2192 Bits |R| \u2192 (M \u00d7 M) \u00d7 Bit\n runAdv (mk A-step\u2080 A-step\u2081) C = A-step\u2080 >>> id *** (const C &&& id >>> A-step\u2081)\n where open FunctionExtra using (_&&&_)\n\n _\u2257A_ : \u2200 {p} (A\u2081 A\u2082 : FunSemSecAdv p) \u2192 Set\n A\u2080 \u2257A A\u2081 = \u2200 C R \u2192 runAdv A\u2080 C R \u2261 runAdv A\u2081 C R\n\n change-adv : \u2200 {cc ca} {E : Enc cc} {A\u2081 A\u2082 : FunSemSecAdv ca} \u2192 A\u2081 \u2257A A\u2082 \u2192 (E \u21c4 A\u2081) \u2257\u2141 (E \u21c4 A\u2082)\n change-adv {ca = ca} {A\u2081 = _} {_} pf b R with splitAt ca R\n change-adv {E = E} {A\u2081} {A\u2082} pf b ._ | pre \u03a3., post , refl = trans (cong proj\u2082 (helper\u2080 A\u2081)) helper\u2082\n where open FunSemSecAdv\n helper\u2080 = \u03bb A \u2192 pf (run\u21ba (E (proj (proj\u2081 (step\u2080 A pre)) b)) post) pre\n helper\u2082 = cong (\u03bb m \u2192 step\u2081 A\u2082 (run\u21ba (E (proj (proj\u2081 m) b)) post , proj\u2082 (step\u2080 A\u2082 pre)))\n (helper\u2080 A\u2082)\n\n SafeSemSecReduction : \u2200 (f : Power \u2192 Power) {cc\u2080 cc\u2081} (E\u2080 : Enc cc\u2080) (E\u2081 : Enc cc\u2081) \u2192 Set\n SafeSemSecReduction f E\u2080 E\u2081 =\n \u2203 \u03bb (red : SemSecReduction f) \u2192\n \u2200 {p} A \u2192 (E\u2080 \u21c4 A) \u21d3 (E\u2081 \u21c4 red {p} A)\n\n SemSecTr : \u2200 {cc\u2080 cc\u2081} (f : Power \u2192 Power) (tr : Tr cc\u2080 cc\u2081) \u2192 Set\n SemSecTr {cc\u2080} f tr = \u2200 {E : Enc cc\u2080} \u2192 SafeSemSecReduction f (tr E) E\n\nmodule PostCompSec (prgDist : PrgDist) (|M| |C| : \u2115) where\n module PostCompRed {FunPower : Set} {t} {T : Set t}\n {\u266dFuns : FlatFuns FunPower T}\n {funPowerOps : PowerOps FunPower}\n (\u266dops : FlatFunsOps funPowerOps \u266dFuns) where\n open FlatFunsOps \u266dops\n open AbsSemSec |M| |C| \u266dFuns\n\n post-comp-red-power : FunPower \u2192 Power \u2192 Power\n post-comp-red-power p (mk p\u2080 p\u2081 |R|) = mk p\u2080 (p ***\u1d56 id\u1d56 >>>\u1d56 p\u2081) |R|\n\n post-comp-red : \u2200 {p} (post-E : \u27e8 p \u27e9 C \u219d C) \u2192 SemSecReduction (post-comp-red-power p)\n post-comp-red post-E (mk A\u2080 A\u2081) = mk A\u2080 (post-E *** idO >>> A\u2081)\n\n open PrgDist prgDist\n open PostCompRed fun\u266dOps\n open FlatFunsOps fun\u266dOps\n open FunSemSec prgDist |M| |C|\n open AbsSemSec |M| |C| fun\u266dFuns\n\n post-comp : \u2200 {cc} (post-E : C \u2192 C) \u2192 Tr cc cc\n post-comp post-E E = E >>> map\u21ba post-E\n\n post-comp-pres-sem-sec : \u2200 {cc} (post-E : C \u2192 C)\n \u2192 SemSecTr id (post-comp {cc} post-E)\n post-comp-pres-sem-sec post-E = post-comp-red post-E , (\u03bb _ \u2192 id)\n\n post-comp-pres-sem-sec' : \u2200 (post-E post-E\u207b\u00b9 : C \u2192 C)\n (post-E-inv : post-E\u207b\u00b9 \u2218 post-E \u2257 id)\n {cc} {E : Enc cc}\n \u2192 SafeSemSecReduction id E (post-comp post-E E)\n post-comp-pres-sem-sec' post-E post-E\u207b\u00b9 post-E-inv {cc} {E} = red , helper where\n E' = post-comp post-E E\n red : SemSecReduction id\n red = post-comp-red post-E\u207b\u00b9\n helper : \u2200 {p} A \u2192 (E \u21c4 A) \u21d3 (E' \u21c4 red {p} A)\n helper {p} A A-breaks-E = A'-breaks-E'\n where open FunSemSecAdv A renaming (step\u2080F to A\u2080F)\n A' = red {p} A\n same-games : (E \u21c4 A) \u2257\u2141 (E' \u21c4 A')\n same-games b R\n rewrite post-E-inv (run\u21ba (E (proj\u2081 (A\u2080F (take (coins p) R)) b))\n (drop (coins p) R)) = refl\n A'-breaks-E' : breaks (E' \u21c4 A')\n A'-breaks-E' = extensional-reduction same-games A-breaks-E\n\n post-neg : \u2200 {cc} \u2192 Tr cc cc\n post-neg = post-comp vnot\n\n post-neg-pres-sem-sec : \u2200 {cc} \u2192 SemSecTr id (post-neg {cc})\n post-neg-pres-sem-sec {cc} {E} = post-comp-pres-sem-sec vnot {E}\n\n post-neg-pres-sem-sec' : \u2200 {cc} {E : Enc cc}\n \u2192 SafeSemSecReduction id E (post-neg E)\n post-neg-pres-sem-sec' {cc} {E} = post-comp-pres-sem-sec' vnot vnot vnot\u2218vnot {cc} {E}\n\nopen import diff\nimport Data.Fin as Fin\n_]-_-[_ : \u2200 {c} (f : \u21ba c Bit) k (g : \u21ba c Bit) \u2192 Set\n_]-_-[_ {c} f k g = diff (Fin.to\u2115 #\u27e8 run\u21ba f \u27e9) (Fin.to\u2115 #\u27e8 run\u21ba g \u27e9) \u2265 2^(c \u2238 k)\n -- diff (#1 f) (#1 g) \u2265 2^(-k) * 2^ c\n -- diff (#1 f) (#1 g) \u2265 \u03b5 * 2^ c\n -- dist (#1 f) (#1 g) \u2265 \u03b5 * 2^ c\n -- where \u03b5 = 2^ -k\n -- {!dist (#1 f \/ 2^ c) (#1 g \/ 2^ c) > \u03b5 !}\n\nopen import Data.Vec.NP using (count)\n\next-count : \u2200 {n a} {A : Set a} {f g : A \u2192 Bool} \u2192 f \u2257 g \u2192 (xs : Vec A n) \u2192 count f xs \u2261 count g xs\next-count f\u2257g [] = refl\next-count f\u2257g (x \u2237 xs) rewrite ext-count f\u2257g xs | f\u2257g x = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\next-# f\u2257g = ext-count f\u2257g (allBits _)\n\n]-[-cong : \u2200 {k c} {f f' g g' : \u21ba c Bit} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f ]- k -[ f' \u2192 g ]- k -[ g'\n]-[-cong f\u2257g f'\u2257g' f]-[f' rewrite ext-# f\u2257g | ext-# f'\u2257g' = f]-[f'\n\nmodule Concrete k where\n _]-[_ : \u2200 {c} (f g : \u21ba c Bit) \u2192 Set\n _]-[_ f g = f ]- k -[ g\n cong' : \u2200 {c} {f f' g g' : \u21ba c Bit} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f ]-[ f' \u2192 g ]-[ g'\n cong' = ]-[-cong {k}\n prgDist : PrgDist\n prgDist = mk _]-[_ cong'\n module Guess' = Guess Coins id prgDist\n module FunSemSec' = FunSemSec prgDist\n module PostCompSec' = PostCompSec prgDist\n","old_contents":"module otp-sem-sec where\n\nimport Level as L\nopen import Function\nopen import Data.Nat.NP\nopen import Data.Bits\nopen import Data.Bool\nopen import Data.Bool.Properties\nopen import Data.Vec hiding (_>>=_)\nopen import Data.Product.NP hiding (_\u27e6\u00d7\u27e7_)\n-- open import circuit\nopen import Relation.Nullary\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\nopen import flipbased-implem\nopen \u2261-Reasoning\nopen import Data.Unit using (\u22a4)\nopen import composable\nopen import vcomp\nopen import forkable\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nmodule FunctionExtra where\n _***_ : \u2200 {A B C D : Set} \u2192 (A \u2192 B) \u2192 (C \u2192 D) \u2192 A \u00d7 C \u2192 B \u00d7 D\n (f *** g) (x , y) = (f x , g y)\n -- Fanout\n _&&&_ : \u2200 {A B C : Set} \u2192 (A \u2192 B) \u2192 (A \u2192 C) \u2192 A \u2192 B \u00d7 C\n (f &&& g) x = (f x , g x)\n _>>>_ : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c} \u2192\n (A \u2192 B) \u2192 (B \u2192 C) \u2192 (A \u2192 C)\n f >>> g = g \u2218 f\n infixr 1 _>>>_\n\nmodule BitsExtra where\n splitAt\u2032 : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k \u00d7 Bits n\n splitAt\u2032 k xs = case splitAt k xs of \u03bb { (ys , zs , _) \u2192 (ys , zs) }\n\n vnot\u2218vnot : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\n vnot\u2218vnot [] = refl\n vnot\u2218vnot (x \u2237 xs) rewrite not-involutive x = cong (_\u2237_ x) (vnot\u2218vnot xs)\n\nopen BitsExtra\n\nCoins = \u2115\nPorts = \u2115\nSize = \u2115\nTime = \u2115\n\nrecord Power : Set where\n constructor mk\n field\n coins : Coins\n size : Size\n time : Time\nopen Power public\n\nsame-power : Power \u2192 Power\nsame-power = id\n\n_+\u1d56_ : Power \u2192 Power \u2192 Power\n(mk c\u2080 s\u2080 t\u2080) +\u1d56 (mk c\u2081 s\u2081 t\u2081) = mk (c\u2080 + c\u2081) (s\u2080 + s\u2081) (t\u2080 + t\u2081)\n\npowerComp : Composable (ConstArr Power)\npowerComp = constComp _+\u1d56_\n\npowerVComp : VComposable _ (ConstArr Power)\npowerVComp = constVComp (\u03bb { (Power.mk c\u2080 s\u2080 t\u2080) (mk c\u2081 s\u2081 t\u2081) \u2192 mk (c\u2080 + c\u2081) (s\u2080 + s\u2081) (t\u2080 \u2294 t\u2081) })\n\nrecord PrgDist : Set\u2081 where\n constructor mk\n field\n _]-[_ : \u2200 {c} (f g : \u21ba c Bit) \u2192 Set\n ]-[-cong : \u2200 {c} {f f' g g' : \u21ba c Bit} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f ]-[ f' \u2192 g ]-[ g'\n\n breaks : \u2200 {c} (EXP : Bit \u2192 \u21ba c Bit) \u2192 Set\n breaks \u2141 = \u2141 0b ]-[ \u2141 1b\n\n _\u2257\u2141_ : \u2200 {c} (\u2141\u2080 \u2141\u2081 : Bit \u2192 \u21ba c Bit) \u2192 Set\n \u2141\u2080 \u2257\u2141 \u2141\u2081 = \u2200 b \u2192 \u2141\u2080 b \u2257\u21ba \u2141\u2081 b\n\n \u2257\u2141-trans : \u2200 {c} \u2192 Transitive (_\u2257\u2141_ {c})\n \u2257\u2141-trans p q b R = trans (p b R) (q b R)\n\nmodule Guess (prgDist : PrgDist) where\n open PrgDist prgDist\n\n GuessAdv : Coins \u2192 Set\n GuessAdv c = \u21ba c Bit\n\n runGuess\u2141 : \u2200 {ca} (A : GuessAdv ca) (b : Bit) \u2192 \u21ba ca Bit\n runGuess\u2141 A _ = A\n\n -- An oracle: an adversary who can break the guessing game.\n Oracle : Power \u2192 Set\n Oracle power = \u2203 (\u03bb (A : GuessAdv (coins power)) \u2192 breaks (runGuess\u2141 A))\n\n -- Any adversary cannot do better than a random guess.\n GuessSec : Power \u2192 Set\n GuessSec power = \u2200 (A : GuessAdv (coins power)) \u2192 \u00ac(breaks (runGuess\u2141 A))\n\nrecord FlatFuns (Power : Set) {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n `Bits : \u2115 \u2192 T\n `Bit : T\n _`\u00d7_ : T \u2192 T \u2192 T\n \u27e8_\u27e9_\u219d_ : Power \u2192 T \u2192 T \u2192 Set\n infixr 2 _`\u00d7_\n infix 0 \u27e8_\u27e9_\u219d_\n\nrecord PowerOps (Power : Set) : Set where\n constructor mk\n field\n _>>>\u1d56_ _***\u1d56_ : Power \u2192 Power \u2192 Power\n\nconstPowerOps : PowerOps \u22a4\nconstPowerOps = _\n\nrecord FlatFunsOps {P t} {T : Set t} (powerOps : PowerOps P) (\u266dFuns : FlatFuns P T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n open PowerOps powerOps\n field\n idO : \u2200 {A p} \u2192 \u27e8 p \u27e9 A \u219d A\n isIComposable : IComposable {I = \u22a4} _>>>\u1d56_ \u27e8_\u27e9_\u219d_\n isVIComposable : VIComposable {I = \u22a4} _***\u1d56_ _`\u00d7_ \u27e8_\u27e9_\u219d_\n open IComposable isIComposable public\n open VIComposable isVIComposable public\n\nfun\u266dFuns : FlatFuns \u22a4 Set\nfun\u266dFuns = mk Bits Bit _\u00d7_ (\u03bb _ A B \u2192 A \u2192 B)\n\nrecord AbsSemSecAdv |M| |C|\n {Power : Set} {t} {T : Set t} (\u266dFuns : FlatFuns Power T)\n (p\u2080 p\u2081 : Power) (|R| : Coins) : Set where\n constructor _,_\n\n open FlatFuns \u266dFuns\n\n field\n {|S|} : \u2115\n\n S = `Bits |S|\n R = `Bits |R|\n M = `Bits |M|\n C = `Bits |C|\n\n field\n step\u2080 : \u27e8 p\u2080 \u27e9 R \u219d (M `\u00d7 M) `\u00d7 S\n step\u2081 : \u27e8 p\u2081 \u27e9 C `\u00d7 S \u219d `Bit\n\nmodule FunAdv (prgDist : PrgDist) (|M| |C| : \u2115) where\n open PrgDist prgDist\n\n M = Bits |M|\n C = Bits |C|\n -- module AbsSemSecAdv' |R| = AbsSemSecAdv {|M|} {|C|} {fun\u266dFuns} |R|\n\n -- open AbsSemSecAdv' using (M; C)\n\n M\u00b2 = Bit \u2192 M\n\n Enc : \u2200 cc \u2192 Set\n Enc cc = M \u2192 \u21ba cc C\n\n record FunSemSecAdv |R| : Set where\n constructor mk\n\n field\n semSecAdv : AbsSemSecAdv |M| |C| fun\u266dFuns _ _ |R|\n\n open AbsSemSecAdv semSecAdv public hiding (M; C)\n\n step\u2080F : R \u2192 (M\u00b2 \u00d7 S)\n step\u2080F = step\u2080 >>> proj *** id\n where open FunctionExtra\n\n step\u2080\u21ba : \u21ba |R| (M\u00b2 \u00d7 S)\n step\u2080\u21ba = mk step\u2080F\n\n step\u2081F : S \u2192 C \u2192 Bit\n step\u2081F s c = step\u2081 (c , s)\n\n -- Returing 0 means Chal wins, Adv looses\n -- 1 means Adv wins, Chal looses\n runSemSec : \u2200 {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b \u2192 \u21ba (ca + cc) Bit\n runSemSec E A b\n = A-step\u2080 >>= \u03bb { (m , s) \u2192 map\u21ba (A-step\u2081 s) (E (m b)) }\n where open FunSemSecAdv A renaming (step\u2080\u21ba to A-step\u2080; step\u2081F to A-step\u2081)\n\n _\u21c4_ : \u2200 {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b \u2192 \u21ba (ca + cc) Bit\n _\u21c4_ = runSemSec\n\n runAdv : \u2200 {|R|} \u2192 FunSemSecAdv |R| \u2192 C \u2192 Bits |R| \u2192 (M \u00d7 M) \u00d7 Bit\n runAdv (mk (A-step\u2080 , A-step\u2081)) C = A-step\u2080 >>> id *** (const C &&& id >>> A-step\u2081)\n where open FunctionExtra\n\n _\u2257A_ : \u2200 {p} (A\u2081 A\u2082 : FunSemSecAdv p) \u2192 Set\n A\u2080 \u2257A A\u2081 = \u2200 C R \u2192 runAdv A\u2080 C R \u2261 runAdv A\u2081 C R\n\n change-adv : \u2200 {cc p} {E : Enc cc} {A\u2081 A\u2082 : FunSemSecAdv p} \u2192 A\u2081 \u2257A A\u2082 \u2192 (E \u21c4 A\u2081) \u2257\u2141 (E \u21c4 A\u2082)\n change-adv {cc} {ca} {E} {A\u2081} {A\u2082} pf b R with splitAt ca R\n change-adv {cc} {ca} {E} {A\u2081} {A\u2082} pf b ._ | pre , post , refl = trans (cong proj\u2082 (helper\u2080 A\u2081)) helper\u2082\n where open FunSemSecAdv\n helper\u2080 = \u03bb A \u2192 pf (run\u21ba (E (proj (proj\u2081 (step\u2080 A pre)) b)) post) pre\n helper\u2082 = cong (\u03bb m \u2192 step\u2081 A\u2082 (run\u21ba (E (proj (proj\u2081 m) b)) post , proj\u2082 (step\u2080 A\u2082 pre)))\n (helper\u2080 A\u2082)\n\n ext-as-broken : \u2200 {c} {\u2141\u2080 \u2141\u2081 : Bit \u2192 \u21ba c Bit}\n \u2192 \u2141\u2080 \u2257\u2141 \u2141\u2081 \u2192 breaks \u2141\u2080 \u2192 breaks \u2141\u2081\n ext-as-broken same-games = ]-[-cong (same-games 0b) (same-games 1b)\n\n SemBroken : \u2200 {cc} (E : Enc cc) \u2192 Power \u2192 Set\n SemBroken E power = \u2203 (\u03bb (A : FunSemSecAdv (coins power)) \u2192 breaks (E \u21c4 A))\n\n Tr : (cc\u2080 cc\u2081 : Coins) \u2192 Set\n Tr cc\u2080 cc\u2081 = Enc cc\u2080 \u2192 Enc cc\u2081\n\n -- SemSecReduction p\u2080 p\u2081 E\u2080 E\u2081:\n -- security of E\u2080 reduces to security of E\u2081\n -- breaking E\u2081 reduces to breaking E\u2080\n SemSecReduction : \u2200 (p\u2080 p\u2081 : Power) {cc\u2080 cc\u2081} (E\u2080 : Enc cc\u2080) (E\u2081 : Enc cc\u2081) \u2192 Set\n SemSecReduction p\u2080 p\u2081 E\u2080 E\u2081 = SemBroken E\u2081 p\u2081 \u2192 SemBroken E\u2080 p\u2080\n\n SemSecTr : \u2200 {cc\u2080 cc\u2081} (f : Power \u2192 Power) (tr : Tr cc\u2080 cc\u2081) \u2192 Set\n SemSecTr {cc\u2080} {cc\u2081} f tr = \u2200 {p} {E : Enc cc\u2080} \u2192 SemSecReduction (f p) p E (tr E)\n\n -- SemSecReductionToOracle : \u2200 (p\u2080 p\u2081 : Power) {cc} (E : Enc cc) \u2192 Set\n -- SemSecReductionToOracle = SemBroken E p\u2080 \u2192 Oracle p\u2081\n\n open FunctionExtra\n\n -- post-comp : \u2200 {cc k} (post-E : C \u2192 \u21ba k C) \u2192 Tr cc (cc + k)\n -- post-comp post-E E = E >=> post-E\n\n post-comp : \u2200 {cc} (post-E : C \u2192 C) \u2192 Tr cc cc\n post-comp post-E E = E >>> map\u21ba post-E\n\n post-comp-pres-sem-sec : \u2200 {cc} (post-E : C \u2192 C)\n \u2192 SemSecTr same-power (post-comp {cc} post-E)\n post-comp-pres-sem-sec post-E (mk (A'\u2080 , A'\u2081) , A'-breaks-E') = A , A'-breaks-E'\n where A = mk (A'\u2080 , (post-E *** id >>> A'\u2081))\n\n post-comp-pres-sem-sec' : \u2200 (post-E post-E\u207b\u00b9 : C \u2192 C)\n (post-E-inv : post-E\u207b\u00b9 \u2218 post-E \u2257 id)\n {cc p} {E : Enc cc}\n \u2192 SemSecReduction p p (post-comp post-E E) E\n post-comp-pres-sem-sec' post-E post-E\u207b\u00b9 post-E-inv {cc} {p} {E} (A , A-breaks-E)\n = A' , A'-breaks-E'\n where E' = post-comp post-E E\n open FunSemSecAdv A renaming (step\u2080 to A\u2080; step\u2080F to A\u2080F; step\u2081 to A\u2081)\n A' = mk (A\u2080 , (post-E\u207b\u00b9 *** id >>> A\u2081))\n same-games : (E \u21c4 A) \u2257\u2141 (E' \u21c4 A')\n same-games b R\n rewrite post-E-inv (run\u21ba (E (proj\u2081 (A\u2080F (take (coins p) R)) b))\n (drop (coins p) R)) = refl\n A'-breaks-E' : breaks (E' \u21c4 A')\n A'-breaks-E' = ext-as-broken same-games A-breaks-E\n\n post-neg : \u2200 {cc} \u2192 Tr cc cc\n post-neg E = E >>> map\u21ba vnot\n\n post-neg-pres-sem-sec : \u2200 {cc} \u2192 SemSecTr same-power (post-neg {cc})\n post-neg-pres-sem-sec {cc} {p} {E} = post-comp-pres-sem-sec vnot {p} {E}\n\n post-neg-pres-sem-sec' : \u2200 {cc p} {E : Enc cc}\n \u2192 SemSecReduction p p (post-neg E) E\n post-neg-pres-sem-sec' {cc} {p} {E} = post-comp-pres-sem-sec' vnot vnot vnot\u2218vnot {cc} {p} {E}\n\nopen import diff\nimport Data.Fin as Fin\n_]-_-[_ : \u2200 {c} (f : \u21ba c Bit) k (g : \u21ba c Bit) \u2192 Set\n_]-_-[_ {c} f k g = diff (Fin.to\u2115 #\u27e8 run\u21ba f \u27e9) (Fin.to\u2115 #\u27e8 run\u21ba g \u27e9) \u2265 2^(c \u2238 k)\n -- diff (#1 f) (#1 g) \u2265 2^(-k) * 2^ c\n -- diff (#1 f) (#1 g) \u2265 \u03b5 * 2^ c\n -- dist (#1 f) (#1 g) \u2265 \u03b5 * 2^ c\n -- where \u03b5 = 2^ -k\n -- {!dist (#1 f \/ 2^ c) (#1 g \/ 2^ c) > \u03b5 !}\n\nopen import Data.Vec.NP using (count)\n\next-count : \u2200 {n a} {A : Set a} {f g : A \u2192 Bool} \u2192 f \u2257 g \u2192 (xs : Vec A n) \u2192 count f xs \u2261 count g xs\next-count f\u2257g [] = refl\next-count f\u2257g (x \u2237 xs) rewrite ext-count f\u2257g xs | f\u2257g x = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\next-# f\u2257g = ext-count f\u2257g (allBits _)\n\n]-[-cong : \u2200 {k c} {f f' g g' : \u21ba c Bit} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f ]- k -[ f' \u2192 g ]- k -[ g'\n]-[-cong f\u2257g f'\u2257g' f]-[f' rewrite ext-# f\u2257g | ext-# f'\u2257g' = f]-[f'\n\n{-\nmodule F''\n (|M| |C| : \u2115)\n\n (Proba : Set)\n (Pr[_\u22611] : \u2200 {c} (EXP : Bits c \u2192 Bit) \u2192 Proba)\n (Pr-ext : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 Pr[ f \u22611] \u2261 Pr[ g \u22611])\n (dist : Proba \u2192 Proba \u2192 Proba)\n (negligible : Proba \u2192 Set)\n (non-negligible : Proba \u2192 Set)\n\n where\n advantage : \u2200 {c} (EXP : Bit \u2192 \u21ba c Bit) \u2192 Proba\n advantage EXP = dist Pr[ EXP 0b \u22611] Pr[ EXP 1b \u22611]\n\n breaks : \u2200 {c} (EXP : Bit \u2192 \u21ba c Bit) \u2192 Set\n breaks \u2141 = non-negligible (advantage \u2141)\n\n SemSec : \u2200 (E : M \u2192 C) \u2192 Power \u2192 Set\n SemSec E power = \u2200 (A : SemSecAdv power) \u2192 negligible (advantage (E \u21c4 A))\n-}\n\n{-\n \u2295-pres-sem-sec : \u2200 mask \u2192 SemSecReduction (_\u2218_ (_\u2295_ mask))\n \u2295-pres-sem-sec = ?\n-}\n\n-- Actually Ops + Spec\nrecord FlatFunsSpec {t} {T : Set t} (\u266dFuns : FlatFuns \u22a4 T) : Set (L.suc L.zero L.\u2294 t) where\n constructor mk\n\n open FlatFuns \u266dFuns\n Cp : T \u2192 T \u2192 Set\n Cp = \u27e8_\u27e9_\u219d_ _\n\n field\n \u27e6_\u27e7 : T \u2192 Set\n _\u27e6\u00d7\u27e7_ : \u2200 {i\u2080 i\u2081} \u2192 \u27e6 i\u2080 \u27e7 \u2192 \u27e6 i\u2081 \u27e7 \u2192 \u27e6 i\u2080 `\u00d7 i\u2081 \u27e7\n \u266dops : FlatFunsOps constPowerOps \u266dFuns\n =[]= : \u2200 {i o} \u2192 Cp i o \u2192 \u27e6 i \u27e7 \u2192 \u27e6 o \u27e7 \u2192 Set\n\n _=[_]=_ : \u2200 {i o} \u2192 \u27e6 i \u27e7 \u2192 Cp i o \u2192 \u27e6 o \u27e7 \u2192 Set\n _=[_]=_ = \u03bb is c os \u2192 =[]= c is os\n\n open FlatFunsOps \u266dops\n field\n idC-spec : \u2200 {i} (bs : \u27e6 i \u27e7) \u2192 bs =[ idO ]= bs\n >>>-spec : IComposable {_\u219d\u1d62_ = Cp} _>>>_ =[]=\n ***-spec : VIComposable {_\u219d\u1d62_ = Cp} _***_ _\u27e6\u00d7\u27e7_ =[]=\n\n runC : \u2200 {i o} \u2192 Cp i o \u2192 \u27e6 i \u27e7 \u2192 \u27e6 o \u27e7\n runC-spec : \u2200 {i o} (c : Cp i o) is \u2192 is =[ c ]= runC c is\n{-\n splitAtC : \u2200 k {n} \u2192 \u27e6 `Bits (k + n) \u27e7 \u2192 (\u27e6 `Bits k \u27e7 \u00d7 \u27e6 `Bits n \u27e7)\n splitAtC2 : \u2200 k {n} \u2192 \u27e6 `Bits (k + n) \u27e7 \u2192 (\u27e6 `Bits k `\u00d7 `Bits n \u27e7)\n splitAtC3 : \u2200 k {n} \u2192 Cp (`Bits (k + n)) (`Bits k `\u00d7 `Bits n)\n splitAtC4 : \u2200 k {n} \u2192 (\u27e6 `Bits k `\u00d7 `Bits n \u27e7) \u2192 (\u27e6 `Bits k \u27e7 \u00d7 \u27e6 `Bits n \u27e7)\n-}\n\n{-\nmodule CpAdv\n {t}\n {T : Set t}\n (\u266dFuns : FlatFuns \u22a4 T)\n (\u266dFunsSpec : FlatFunsSpec \u266dFuns)\n -- (FCp : Coins \u2192 Size \u2192 Time \u2192 Ports \u2192 Ports \u2192 Set)\n -- (toC : \u2200 {c s t i o} \u2192 FCp c s t i o \u2192 Cp (c + i) o)\n\n (prgDist : PrgDist)\n (|M| |C| : \u2115)\n\n where\n open FlatFuns \u266dFuns\n open FlatFunsSpec \u266dFunsSpec\n open FlatFunsOps \u266dops\n\n open PrgDist prgDist\n `M = `Bits |M|\n `C = `Bits |C|\n M = \u27e6 `M \u27e7\n C = \u27e6 `C \u27e7\n M\u00b2 = Bit \u2192 M\n\n -- module FunAdv' = FunAdv prgDist |M| |C|\n -- open FunAdv' public using (mk; {-module SplitSemSecAdv;-} FunSemSecAdv; M; C; M\u00b2; Enc; Tr; ext-as-broken; change-adv; _\u2257A_) renaming (_\u21c4_ to _\u21c4F_)\n\n module FE = FunctionExtra\n\n record CpSemSecAdv power : Set where\n constructor mk\n\n open Power power renaming (coins to |R|; size to s; time to t)\n\n -- open FunctionExtra\n\n field\n semSecAdv : AbsSemSecAdv |M| |C| \u266dFuns _ _ |R|\n\n open AbsSemSecAdv semSecAdv public hiding (M; C; R; S)\n\n `R = `Bits |R|\n `S = `Bits |S|\n R = \u27e6 `R \u27e7\n S = \u27e6 `S \u27e7\n\n step\u2080f : R \u2192 ((M \u00d7 M) \u00d7 S)\n step\u2080f = runC step\u2080 FE.>>> {!splitAtC4 (|M| + |M|) FE.>>> {!splitAtC |M| FE.*** id!}!}\n\n -- step\u2080f : R \u2192 ((M \u00d7 M) \u00d7 S)\n -- step\u2080f = runC step\u2080 FE.>>> {!splitAtC3 (|M| + |M|) FE.>>> {!splitAtC |M| FE.*** id!}!}\n\n step\u2080F : R \u2192 (M\u00b2 \u00d7 S)\n step\u2080F = step\u2080f >>> proj *** id\n\n step\u2080\u21ba : \u21ba |R| (M\u00b2 \u00d7 S)\n step\u2080\u21ba = mk step\u2080F\n\n step\u2081f : C \u00d7 S \u2192 Bit\n step\u2081f (c , s) = head (runC step\u2081 (c ++ s))\n\n step\u2081F : S \u2192 C \u2192 Bit\n step\u2081F s c = step\u2081f (c , s)\n\n-- funAdv : FunSemSecAdv |R|\n-- funAdv = mk (step\u2080f , step\u2081f) \n\n{-\n record SemSecAdv+FunBeh power : Set where\n constructor mk\n\n open Power power renaming (coins to c; size to s; time to t)\n\n field\n Acp : CpSemSecAdv power\n A\u21ba : FunSemSecAdv c\n\n open CpSemSecAdv Acp renaming (beh\u21ba to Acp-beh\u21ba; beh to Acp-beh; beh\u2082 to Acp-beh\u2082; beh\u2081 to Acp-beh\u2081)\n A\u21ba-beh : R \u2192 M\u00b2 \u00d7 (C \u2192 Bit)\n A\u21ba-beh = run\u21ba A\u21ba\n\n module SplitA\u21ba = SplitSemSecAdv A\u21ba\n open SplitA\u21ba using () renaming (beh\u2081 to As-beh\u2081; beh\u2082 to As-beh\u2082)\n\n field\n coh\u2081 : Acp-beh\u2081 \u2257 As-beh\u2081\n coh\u2082 : \u2200 R \u2192 Acp-beh\u2082 R \u2257 As-beh\u2082 R\n\n coh : CpSemSecAdv.adv\u21ba Acp \u2257A A\u21ba\n coh = coh\u2081 , coh\u2082\n-}\n -- Returing 0 means Chal wins, Adv looses\n -- 1 means Adv wins, Chal looses\n _\u21c4_ : \u2200 {cc p} (E : Enc cc) (A : CpSemSecAdv p) b \u2192 \u21ba (coins p + cc) Bit\n E \u21c4 A = E \u21c4F CpSemSecAdv.funAdv A\n\n SemBroken : \u2200 {cc} (E : Enc cc) \u2192 Power \u2192 Set\n SemBroken E power = \u2203 (\u03bb (A : CpSemSecAdv power) \u2192 breaks (E \u21c4 A))\n\n -- SemSecReduction p\u2080 p\u2081 E\u2080 E\u2081:\n -- security of E\u2080 reduces to security of E\u2081\n -- breaking E\u2081 reduces to breaking E\u2080\n SemSecReduction : \u2200 (p\u2080 p\u2081 : Power) {cc\u2080 cc\u2081} (E\u2080 : Enc cc\u2080) (E\u2081 : Enc cc\u2081) \u2192 Set\n SemSecReduction p\u2080 p\u2081 E\u2080 E\u2081 = SemBroken E\u2081 p\u2081 \u2192 SemBroken E\u2080 p\u2080\n\n SemSecTr : \u2200 {cc\u2080 cc\u2081} (f : Power \u2192 Power) (tr : Tr cc\u2080 cc\u2081) \u2192 Set\n SemSecTr {cc\u2080} {cc\u2081} f tr = \u2200 {p} {E : Enc cc\u2080} \u2192 SemSecReduction (f p) p E (tr E)\n\n post-comp-cp : \u2200 {cc} post-E \u2192 Tr cc cc\n post-comp-cp post-E E = E >>> map\u21ba (runC post-E)\n where open FunctionExtra\n\n post-comp-pres-sem-sec : \u2200 {cc} (post-E : Cp |C| |C|) \u2192 SemSecTr same-power (post-comp-cp {cc} post-E)\n post-comp-pres-sem-sec {cc} post-E {p} {E} (A' , A'-breaks-E') = A , A-breaks-E\n where E' : Enc cc\n E' = post-comp-cp post-E E\n open Power p renaming (coins to c)\n open CpSemSecAdv A' using (R; S; |S|; step\u2080) renaming (step\u2080f to A'-step\u2080f; step\u2081f to A'-step\u2081f; step\u2081 to A'-step\u2081)\n A-step\u2081-spec = runC post-E *** id >>> A'-step\u2081f\n where open FunctionExtra\n A-spec : FunSemSecAdv (coins p)\n A-spec = mk (A'-step\u2080f , A-step\u2081-spec)\n open FlatFunsOps \u266dops\n A-step\u2081 = post-E *** idO {|S|} >>> A'-step\u2081\n A : CpSemSecAdv p\n A = mk (step\u2080 , A-step\u2081)\n open CpSemSecAdv A using () renaming (funAdv to funA; step\u2080f to A-step\u2080f; step\u2081f to A-step\u2081f)\n\n coh\u2081 : A-step\u2081-spec \u2257 A-step\u2081f\n coh\u2081 (C , S) rewrite >>>-spec (post-E *** idO {|S|}) A'-step\u2081 (C ++ S)\n | ***-spec post-E (idO {|S|}) C {S}\n | idC-spec S = refl\n pf3 : A-spec \u2257A funA\n pf3 C R rewrite coh\u2081 (C , proj\u2082 (A-step\u2080f R)) = refl\n same-games : (E' \u21c4 A') \u2257\u2141 (E \u21c4 A)\n same-games = change-adv {E = E} {A-spec} {funA} pf3\n A-breaks-E : breaks (E \u21c4 A)\n A-breaks-E = ext-as-broken same-games A'-breaks-E'\n\n{-\n \u2295-pres-sem-sec : \u2200 mask \u2192 SemSecReduction (_\u2218_ (_\u2295_ mask))\n-}\n\n{-\nfunBits-kit : FlatFunsSpec\nfunBits-kit = mk _\u2192\u1d47_ bitsFunCircuitBuilder id idC-spec >>>-spec ***-spec where\n open CircuitBuilder bitsFunCircuitBuilder\n open BitsFunExtras\n\nmodule Concrete k where\n _]-[_ : \u2200 {c} (f g : \u21ba c Bit) \u2192 Set\n _]-[_ f g = f ]- k -[ g\n cong' : \u2200 {c} {f f' g g' : \u21ba c Bit} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f ]-[ f' \u2192 g ]-[ g'\n cong' = ]-[-cong {k}\n prgDist : PrgDist\n prgDist = mk _]-[_ cong'\n module Guess' = Guess prgDist\n module FunAdv' = FunAdv prgDist\n module CpAdv' = CpAdv funBits-kit prgDist\n-}\n\n{-\nmodule OTP (prgDist : PrgDist) where\n open FunAdv prgDist 1 1\n open Guess prgDist renaming (breaks to reads-your-mind)\n\n -- OTP\u2081 : \u2200 {c} (k : Bit) \u2192 Enc c\n -- OTP\u2081 k m = return\u21ba ((k xor (head m)) \u2237 [])\n\n OTP\u2081 : Enc 1\n OTP\u2081 m = map\u21ba (\u03bb k \u2192 (k xor (head m)) \u2237 []) toss\n\n open FunctionExtra\n\n -- \u2200 k \u2192 SemSecReductionToOracle no-power no-power (OTP\u2081 k)\n foo : \u2200 p \u2192 SemBroken OTP\u2081 p \u2192 Oracle {!(mk 1 0 0 +\u1d56 p)!}\n foo p (A , A-breaks-OTP) = O , O-reads-your-mind\n where postulate\n b : Bit\n -- b = {!!}\n k : Bit\n -- k = {!!}\n O : \u21ba (coins p + 1) Bit\n O = A >>= \u03bb { (m , kont) \u2192 map\u21ba (_xor_ (head (m b)) \u2218 kont) (OTP\u2081 (m b)) } -- (k \u2237 []))) }\n O-reads-your-mind : reads-your-mind (runGuess\u2141 O)\n O-reads-your-mind = {!!}\n{-\n A >>= \u03bb { (m , kont) \u2192 map\u21ba kont (OTP\u2081 (m b)) }\n A >>= \u03bb { (m , kont) \u2192 map\u21ba kont (map\u21ba (\u03bb k \u2192 (k xor (head (m b))) \u2237 []) toss) }\n A >>= \u03bb { (m , kont) \u2192 map\u21ba (kont \u2218 \u03bb k \u2192 (k xor (head (m b))) \u2237 []) toss) }\n A >>= \u03bb { (m , kont) \u2192 map\u21ba (\u03bb k \u2192 kont ((k xor (head (m b))) \u2237 []) toss) }\n A >>= \u03bb { (m , kont) \u2192 map\u21ba (\u03bb k \u2192 kont ((k xor (head (m b))) \u2237 []) (choose (return\u21ba 0b) (return\u21ba 1b))) }\n A >>= \u03bb { (m , kont) \u2192 choose (kont ((0b xor (head (m b))) \u2237 []))\n (kont ((1b xor (head (m b))) \u2237 [])) }\n A >>= \u03bb { (m , kont) \u2192 choose (kont (head (m b) \u2237 []))\n (kont (not (head (m b)) \u2237 [])) }\n\nkont = const b\n A >>= \u03bb { (m , kont) \u2192 map\u21ba kont (OTP\u2081 (m b)) }\n A >>= \u03bb { (m , kont) \u2192 map\u21ba (const b) (OTP\u2081 (m b)) }\n A >>= \u03bb { (m , kont) \u2192 toss >> return b }\n\nkont = const b\n A >>= \u03bb { (m , kont) \u2192 map\u21ba (\u03bb c \u2192 c xor head (m (kont c))) (OTP\u2081 (m b)) }\n A >>= \u03bb { (m , kont) \u2192 map\u21ba (_xor_ (head (m b)) \u2218 const b) (OTP\u2081 (m b)) }\n A >>= \u03bb { (m , kont) \u2192 map\u21ba (const (head (m b) xor b)) (OTP\u2081 (m b)) }\n A >>= \u03bb { (m , kont) \u2192 return\u21ba (head (m b) xor b) }\n\n---\n A >>= \u03bb { (m , kont) \u2192 map\u21ba (_xor_ (head (m b)) \u2218 kont) (OTP\u2081 (m b)) }\n A >>= \u03bb { (m , kont) \u2192 map\u21ba (_xor_ (head (m b)) \u2218 kont) (map\u21ba (\u03bb k \u2192 (k xor (head (m b))) \u2237 []) toss) }\n A >>= \u03bb { (m , kont) \u2192 map\u21ba (_xor_ (head (m b)) \u2218 kont \u2218 \u03bb k \u2192 (k xor (head (m b))) \u2237 []) toss) }\n A >>= \u03bb { (m , kont) \u2192 map\u21ba (\u03bb k \u2192 (head (m b)) xor (kont ((k xor (head (m b))) \u2237 [])) toss) }\n A >>= \u03bb { (m , kont) \u2192 choose ((head (m b)) xor (kont ((0b xor (head (m b))) \u2237 [])))\n ((head (m b)) xor (kont ((1b xor (head (m b))) \u2237 [])))\n }\n-}\n-}\n-}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"838565e1e2803a8aaf798d3dc124bda52434357b","subject":"one-time-semantic-security: plenty of minor improvments","message":"one-time-semantic-security: plenty of minor improvments\n","repos":"crypto-agda\/crypto-agda","old_file":"one-time-semantic-security.agda","new_file":"one-time-semantic-security.agda","new_contents":"","old_contents":"","returncode":0,"stderr":"unknown","license":"bsd-3-clause","lang":"Agda"} {"commit":"a9feb3a901a7d53440a0a2887ae907ada16e3f20","subject":"Type.Identities: one more","message":"Type.Identities: one more\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Type\/Identities.agda","new_file":"lib\/Type\/Identities.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Type\n\nopen import Level.NP\nopen import HoTT\nopen import Function.NP\nopen import Function.Extensionality\n\nopen import Data.Maybe.NP using (Maybe ; just ; nothing ; maybe ; maybe\u2032 ; just-injective; Maybe^)\nopen import Data.Zero using (\ud835\udfd8 ; \ud835\udfd8-elim)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Two\nopen import Data.Fin as Fin using (Fin ; suc ; zero)\nopen import Data.Nat.NP as \u2115 using (\u2115 ; suc ; zero; _+_)\nopen import Data.Product.NP renaming (map to map\u00d7)\nopen import Data.Sum using (_\u228e_) renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_]; map to map\u228e)\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_ ; ap; coe; coe!; !_; _\u2219_; J ; inspect ; Reveal_is_ ; [_]; tr; ap\u2082; apd) renaming (refl to idp; _\u2257_ to _\u223c_; J-orig to J')\n\nmodule Type.Identities where\n\nopen Equivalences\n\nmodule _ {a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 B\u2081 : \u2605_ b}(A= : A\u2080 \u2261 A\u2081)(B= : B\u2080 \u2261 B\u2081) where\n \u00d7= : (A\u2080 \u00d7 B\u2080) \u2261 (A\u2081 \u00d7 B\u2081)\n \u00d7= = ap\u2082 _\u00d7_ A= B=\n\n \u228e= : (A\u2080 \u228e B\u2080) \u2261 (A\u2081 \u228e B\u2081)\n \u228e= = ap\u2082 _\u228e_ A= B=\n\n \u2192= : (A\u2080 \u2192 B\u2080) \u2261 (A\u2081 \u2192 B\u2081)\n \u2192= = ap\u2082 -\u2192- A= B=\n\nmodule _ {a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 B\u2081 : \u2605_ b}(A\u2243 : A\u2080 \u2243 A\u2081)(B\u2243 : B\u2080 \u2243 B\u2081) where\n{-\n \u00d7\u2243 : (A\u2080 \u00d7 B\u2080) \u2243 (A\u2081 \u00d7 B\u2081)\n \u00d7\u2243 = equiv (map\u00d7 (\u2013> A\u2243) (\u2013> B\u2243)) (map\u00d7 (<\u2013 A\u2243) (<\u2013 B\u2243))\n (\u03bb y \u2192 pair= (<\u2013-inv-r A\u2243 (fst y)) ({!!} \u2219 <\u2013-inv-r B\u2243 (snd y)))\n {!!}\n -}\n\n \u228e\u2243 : (A\u2080 \u228e B\u2080) \u2243 (A\u2081 \u228e B\u2081)\n \u228e\u2243 = equiv (map\u228e (\u2013> A\u2243) (\u2013> B\u2243)) (map\u228e (<\u2013 A\u2243) (<\u2013 B\u2243))\n [inl: (\u03bb x \u2192 ap inl (<\u2013-inv-r A\u2243 x)) ,inr: ap inr \u2218 <\u2013-inv-r B\u2243 ]\n [inl: (\u03bb x \u2192 ap inl (<\u2013-inv-l A\u2243 x)) ,inr: ap inr \u2218 <\u2013-inv-l B\u2243 ]\n\n \u2192\u2243 : {{_ : FunExt}} \u2192 (A\u2080 \u2192 B\u2080) \u2243 (A\u2081 \u2192 B\u2081)\n \u2192\u2243 = equiv (\u03bb f \u2192 \u2013> B\u2243 \u2218 f \u2218 <\u2013 A\u2243)\n (\u03bb f \u2192 <\u2013 B\u2243 \u2218 f \u2218 \u2013> A\u2243)\n (\u03bb f \u2192 \u03bb= (\u03bb x \u2192 <\u2013-inv-r B\u2243 _ \u2219 ap f (<\u2013-inv-r A\u2243 x)))\n (\u03bb f \u2192 \u03bb= (\u03bb x \u2192 <\u2013-inv-l B\u2243 _ \u2219 ap f (<\u2013-inv-l A\u2243 x)))\n\nmodule _ {{_ : FunExt}}{a}(A : \u2605_ a){b}{B\u2080 B\u2081 : A \u2192 \u2605_ b}(B : (x : A) \u2192 B\u2080 x \u2261 B\u2081 x) where\n \u03a3=\u2032 : \u03a3 A B\u2080 \u2261 \u03a3 A B\u2081\n \u03a3=\u2032 = ap (\u03a3 A) (\u03bb= B)\n\n \u03a0=\u2032 : \u03a0 A B\u2080 \u2261 \u03a0 A B\u2081\n \u03a0=\u2032 = ap (\u03a0 A) (\u03bb= B)\n\nmodule _ {a b}{A\u2080 : \u2605_ a}{B\u2080 : A\u2080 \u2192 \u2605_ b}{{_ : FunExt}} where\n \u03a3= : {A\u2081 : \u2605_ a}(A= : A\u2080 \u2261 A\u2081)\n {B\u2081 : A\u2081 \u2192 \u2605_ b}(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (coe A= x))\n \u2192 \u03a3 A\u2080 B\u2080 \u2261 \u03a3 A\u2081 B\u2081\n \u03a3= = J (\u03bb A\u2081 A= \u2192 {B\u2081 : A\u2081 \u2192 \u2605_ b}(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (coe A= x))\n \u2192 \u03a3 A\u2080 B\u2080 \u2261 \u03a3 A\u2081 B\u2081) (\u03a3=\u2032 _)\n -- \u03a3= idp B= = \u03a3=\u2032 _ B=\n\n \u03a0= : \u2200 {A\u2081 : \u2605_ a}(A= : A\u2080 \u2261 A\u2081)\n {B\u2081 : A\u2081 \u2192 \u2605_ b}(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (coe A= x))\n \u2192 \u03a0 A\u2080 B\u2080 \u2261 \u03a0 A\u2081 B\u2081\n \u03a0= idp B= = \u03a0=\u2032 _ B=\n\nmodule _ {{_ : UA}}{{_ : FunExt}}{a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 : A\u2080 \u2192 \u2605_ b}{B\u2081 : A\u2081 \u2192 \u2605_ b} where\n \u03a3\u2243 : (A\u2243 : A\u2080 \u2243 A\u2081)(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (\u2013> A\u2243 x))\n \u2192 \u03a3 A\u2080 B\u2080 \u2261 \u03a3 A\u2081 B\u2081\n \u03a3\u2243 A\u2243 B= = \u03a3= (ua A\u2243) \u03bb x \u2192 B= x \u2219 ap B\u2081 (! coe-\u03b2 A\u2243 x)\n\n \u03a0\u2243 : (A : A\u2080 \u2243 A\u2081)(B : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (\u2013> A x))\n \u2192 \u03a0 A\u2080 B\u2080 \u2261 \u03a0 A\u2081 B\u2081\n \u03a0\u2243 A B = \u03a0= (ua A) \u03bb x \u2192 B x \u2219 ap B\u2081 (! coe-\u03b2 A x)\n\n {-\nmodule _ {{_ : FunExt}}{a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 : A\u2080 \u2192 \u2605_ b}{B\u2081 : A\u2081 \u2192 \u2605_ b}(A : A\u2080 \u2243 A\u2081)(B : (x : A\u2081) \u2192 B\u2080 (<\u2013 A x) \u2243 B\u2081 x) where\n \u03a0\u2243' : (\u03a0 A\u2080 B\u2080) \u2243 (\u03a0 A\u2081 B\u2081)\n \u03a0\u2243' = equiv (\u03bb f x \u2192 \u2013> (B x) (f (<\u2013 A x)))\n (\u03bb f x \u2192 tr B\u2080 (<\u2013-inv-l A x) (<\u2013 (B (\u2013> A x)) (f (\u2013> A x))))\n (\u03bb f \u2192 \u03bb= (\u03bb x \u2192 {!apd (<\u2013-inv-l A (<\u2013 A x))!}))\n {!\u03bb f \u2192 \u03bb= (\u03bb x \u2192 <\u2013-inv-l B _ \u2219 ap f (<\u2013-inv-l A x))!}\n -}\n\nmodule _ {{_ : UA}}{{_ : FunExt}}{a}{A\u2080 A\u2081 : \u2605_ a}{b} where\n \u03a3-fst\u2243 : \u2200 (A : A\u2080 \u2243 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2080 (B \u2218 \u2013> A) \u2261 \u03a3 A\u2081 B\n \u03a3-fst\u2243 A B = \u03a3\u2243 A (\u03bb x \u2192 idp)\n\n \u03a3-fst= : \u2200 (A : A\u2080 \u2261 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2080 (B \u2218 coe A) \u2261 \u03a3 A\u2081 B\n \u03a3-fst= A = \u03a3-fst\u2243 (coe-equiv A)\n\n \u03a0-dom\u2243 : \u2200 (A : A\u2080 \u2243 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2080 (B \u2218 \u2013> A) \u2261 \u03a0 A\u2081 B\n \u03a0-dom\u2243 A B = \u03a0\u2243 A (\u03bb x \u2192 idp)\n\n \u03a0-dom= : \u2200 (A : A\u2080 \u2261 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2080 (B \u2218 coe A) \u2261 \u03a0 A\u2081 B\n \u03a0-dom= A = \u03a0-dom\u2243 (coe-equiv A)\n\n -- variations where the equiv is transported backward on the right side\n\n \u03a3-fst\u2243\u2032 : (A : A\u2081 \u2243 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2081 B \u2261 \u03a3 A\u2080 (B \u2218 <\u2013 A)\n \u03a3-fst\u2243\u2032 A B = ! \u03a3-fst\u2243 (\u2243-sym A) B\n\n \u03a3-fst=\u2032 : (A : A\u2081 \u2261 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2081 B \u2261 \u03a3 A\u2080 (B \u2218 coe! A)\n \u03a3-fst=\u2032 A = \u03a3-fst\u2243\u2032 (coe-equiv A)\n\n \u03a0-dom\u2243\u2032 : (A : A\u2081 \u2243 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2081 B \u2261 \u03a0 A\u2080 (B \u2218 <\u2013 A)\n \u03a0-dom\u2243\u2032 A B = ! \u03a0-dom\u2243 (\u2243-sym A) B\n\n \u03a0-dom=\u2032 : (A : A\u2081 \u2261 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2081 B \u2261 \u03a0 A\u2080 (B \u2218 coe! A)\n \u03a0-dom=\u2032 A = \u03a0-dom\u2243\u2032 (coe-equiv A)\n\nmodule _ {a b c} {A : \u2605_ a} {B : A \u2192 \u2605_ b} {C : \u03a3 A B \u2192 \u2605_ c} where\n \u03a0\u03a3-curry-equiv : \u03a0 (\u03a3 A B) C \u2243 ((x : A) (y : B x) \u2192 C (x , y))\n \u03a0\u03a3-curry-equiv = equiv curry uncurry (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a0\u03a3-curry : {{_ : UA}} \u2192 \u03a0 (\u03a3 A B) C \u2261 ((x : A) (y : B x) \u2192 C (x , y))\n \u03a0\u03a3-curry = ua \u03a0\u03a3-curry-equiv\n\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 : \ud835\udfda \u2243 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 = equiv [0: inl _ 1: inr _ ]\n [inl: const 0\u2082 ,inr: const 1\u2082 ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n [0: idp 1: idp ]\n\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 : {{_ : UA}} \u2192 \ud835\udfda \u2261 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 = ua \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9\n\nmodule _ {{_ : UA}}{A : \u2605}{B C : A \u2192 \u2605} where\n \u03a3\u228e-split : (\u03a3 A (\u03bb x \u2192 B x \u228e C x)) \u2261 (\u03a3 A B \u228e \u03a3 A C)\n \u03a3\u228e-split = ua (equiv (\u03bb { (x , inl y) \u2192 inl (x , y)\n ; (x , inr y) \u2192 inr (x , y) })\n (\u03bb { (inl (x , y)) \u2192 x , inl y\n ; (inr (x , y)) \u2192 x , inr y })\n (\u03bb { (inl (x , y)) \u2192 idp\n ; (inr (x , y)) \u2192 idp })\n (\u03bb { (x , inl y) \u2192 idp\n ; (x , inr y) \u2192 idp }))\n\nmodule _ {{_ : UA}}{a b c}{A : \u2605_ a}{B : \u2605_ b}{C : A \u228e B \u2192 \u2605_ c} where\n dist-\u228e-\u03a3-equiv : \u03a3 (A \u228e B) C \u2243 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u03a3 : \u03a3 (A \u228e B) C \u2261 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3 = ua dist-\u228e-\u03a3-equiv\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0-equiv : \u03a0 (A \u228e B) C \u2243 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u03a0 : \u03a0 (A \u228e B) C \u2261 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0 = ua dist-\u00d7-\u03a0-equiv\n\nmodule _ {{_ : UA}}{A B : \u2605}{C : A \u2192 \u2605}{D : B \u2192 \u2605} where\n dist-\u228e-\u03a3[] : (\u03a3 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a3 A C \u228e \u03a3 B D)\n dist-\u228e-\u03a3[] = dist-\u228e-\u03a3\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0[] : (\u03a0 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a0 A C \u00d7 \u03a0 B D)\n dist-\u00d7-\u03a0[] = dist-\u00d7-\u03a0\n\nmodule _ {{_ : UA}}{A B C : \u2605} where\n dist-\u228e-\u00d7-equiv : ((A \u228e B) \u00d7 C) \u2243 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u00d7 : ((A \u228e B) \u00d7 C) \u2261 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7 = ua dist-\u228e-\u00d7-equiv\n\n module _ {{_ : FunExt}} where\n dist-\u00d7-\u2192-equiv : ((A \u228e B) \u2192 C ) \u2243 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u2192 : ((A \u228e B) \u2192 C) \u2261 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192 = ua dist-\u00d7-\u2192-equiv\n\nmodule _ {a b c}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{C : (x : A) \u2192 B x \u2192 \u2605_ c} where\n \u03a3-assoc-equiv : (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2243 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc-equiv = equiv (\u03bb x \u2192 (fst x , fst (snd x)) , snd (snd x))\n (\u03bb x \u2192 fst (fst x) , snd (fst x) , snd x)\n (\u03bb y \u2192 idp)\n (\u03bb y \u2192 idp)\n\n \u03a3-assoc : {{_ : UA}} \u2192 (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2261 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc = ua \u03a3-assoc-equiv\n\nmodule _ {a b}{A : \u2605_ a} {B : \u2605_ b} where\n \u00d7-comm-equiv : (A \u00d7 B) \u2243 (B \u00d7 A)\n \u00d7-comm-equiv = equiv swap swap (\u03bb y \u2192 idp) (\u03bb x \u2192 idp)\n\n \u00d7-comm : {{_ : UA}} \u2192 (A \u00d7 B) \u2261 (B \u00d7 A)\n \u00d7-comm = ua \u00d7-comm-equiv\n\n \u228e-comm-equiv : (A \u228e B) \u2243 (B \u228e A)\n \u228e-comm-equiv = equiv [inl: inr ,inr: inl ]\n [inl: inr ,inr: inl ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n\n \u228e-comm : {{_ : UA}} \u2192 (A \u228e B) \u2261 (B \u228e A)\n \u228e-comm = ua \u228e-comm-equiv\n\nmodule _ {A B : \u2605}{C : A \u2192 B \u2192 \u2605} where\n \u03a0\u03a0-comm-equiv : ((x : A)(y : B) \u2192 C x y) \u2243 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm-equiv = equiv flip flip (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a0\u03a0-comm : {{_ : UA}} \u2192 ((x : A)(y : B) \u2192 C x y) \u2261 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm = ua \u03a0\u03a0-comm-equiv\n\n \u03a3\u03a3-comm-equiv : (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2243 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm-equiv = equiv (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb _ \u2192 idp)\n (\u03bb _ \u2192 idp)\n\n \u03a3\u03a3-comm : {{_ : UA}} \u2192 (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2261 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm = ua \u03a3\u03a3-comm-equiv\n\nmodule _ {a b c} {A : \u2605_ a} {B : \u2605_ b} {C : \u2605_ c} where\n \u00d7-assoc : {{_ : UA}} \u2192 (A \u00d7 (B \u00d7 C)) \u2261 ((A \u00d7 B) \u00d7 C)\n \u00d7-assoc = \u03a3-assoc\n\n \u228e-assoc-equiv : (A \u228e (B \u228e C)) \u2243 ((A \u228e B) \u228e C)\n \u228e-assoc-equiv = equiv [inl: inl \u2218 inl ,inr: [inl: inl \u2218 inr ,inr: inr ] ]\n [inl: [inl: inl ,inr: inr \u2218 inl ] ,inr: inr \u2218 inr ]\n [inl: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ]\n\n \u228e-assoc : {{_ : UA}} \u2192 (A \u228e (B \u228e C)) \u2261 ((A \u228e B) \u228e C)\n \u228e-assoc = ua \u228e-assoc-equiv\n\nmodule _ {{_ : UA}}{{_ : FunExt}}{a}(A : \ud835\udfd8 \u2192 \u2605_ a) where\n \u03a0\ud835\udfd8-uniq : \u03a0 \ud835\udfd8 A \u2261 Lift \ud835\udfd9\n \u03a0\ud835\udfd8-uniq = ua (equiv _ (\u03bb _ ()) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= (\u03bb())))\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \ud835\udfd8 \u2192 \u2605\u2080) where\n \u03a0\ud835\udfd8-uniq\u2080 : \u03a0 \ud835\udfd8 A \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq\u2080 = ua (equiv _ (\u03bb _ ()) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= (\u03bb())))\n\nmodule _ {{_ : UA}}{a}(A : \ud835\udfd9 \u2192 \u2605_ a) where\n \u03a0\ud835\udfd9-uniq : \u03a0 \ud835\udfd9 A \u2261 A _\n \u03a0\ud835\udfd9-uniq = ua (equiv (\u03bb f \u2192 f _) (\u03bb x _ \u2192 x) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\nmodule _ {{_ : UA}}{a}(A : \u2605_ a) where\n \u03a0\ud835\udfd9-uniq\u2032 : (\ud835\udfd9 \u2192 A) \u2261 A\n \u03a0\ud835\udfd9-uniq\u2032 = \u03a0\ud835\udfd9-uniq (\u03bb _ \u2192 A)\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \u2605\u2080) where\n \u03a0\ud835\udfd8-uniq\u2032 : (\ud835\udfd8 \u2192 A) \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq\u2032 = \u03a0\ud835\udfd8-uniq\u2080 (\u03bb _ \u2192 A)\n\n \ud835\udfd8\u2192A\u2261\ud835\udfd9 = \u03a0\ud835\udfd8-uniq\u2032\n\nmodule _ {{_ : FunExt}}{\u2113}(F G : \ud835\udfd8 \u2192 \u2605_ \u2113) where\n -- also by \u03a0\ud835\udfd8-uniq twice\n \u03a0\ud835\udfd8-uniq' : \u03a0 \ud835\udfd8 F \u2261 \u03a0 \ud835\udfd8 G\n \u03a0\ud835\udfd8-uniq' = \u03a0=\u2032 \ud835\udfd8 (\u03bb())\n\nmodule _ {a \u2113} {A : \ud835\udfd8 \u2192 \u2605_ a} where\n \u03a3\ud835\udfd8-lift\u2218fst-equiv : \u03a3 \ud835\udfd8 A \u2243 Lift {\u2113 = \u2113} \ud835\udfd8\n \u03a3\ud835\udfd8-lift\u2218fst-equiv = equiv (lift \u2218 fst) (\u03bb { (lift ()) }) (\u03bb { (lift ()) }) (\u03bb { (() , _) })\n\nmodule _ {a} {A : \ud835\udfd8 \u2192 \u2605_ a} {{_ : UA}} where\n \u03a3\ud835\udfd8-lift\u2218fst : \u03a3 \ud835\udfd8 A \u2261 Lift {\u2113 = a} \ud835\udfd8\n \u03a3\ud835\udfd8-lift\u2218fst = ua \u03a3\ud835\udfd8-lift\u2218fst-equiv\n\nmodule _ {A : \ud835\udfd8 \u2192 \u2605} where\n \u03a3\ud835\udfd8-fst-equiv : \u03a3 \ud835\udfd8 A \u2243 \ud835\udfd8\n \u03a3\ud835\udfd8-fst-equiv = equiv fst (\u03bb()) (\u03bb()) (\u03bb { (() , _) })\n\n \u03a3\ud835\udfd8-fst : {{_ : UA}} \u2192 \u03a3 \ud835\udfd8 A \u2261 \ud835\udfd8\n \u03a3\ud835\udfd8-fst = ua \u03a3\ud835\udfd8-fst-equiv\n\nmodule _ {a}{A : \ud835\udfd9 \u2192 \u2605_ a} where\n \u03a3\ud835\udfd9-snd-equiv : \u03a3 \ud835\udfd9 A \u2243 A _\n \u03a3\ud835\udfd9-snd-equiv = equiv snd (_,_ _) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a3\ud835\udfd9-snd : {{_ : UA}} \u2192 \u03a3 \ud835\udfd9 A \u2261 A _\n \u03a3\ud835\udfd9-snd = ua \u03a3\ud835\udfd9-snd-equiv\n\nmodule _ {A : \u2605} where\n \u228e\ud835\udfd8-inl-equiv : A \u2243 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl-equiv = equiv inl [inl: id ,inr: (\u03bb()) ] [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb()) ] (\u03bb _ \u2192 idp)\n\n module _ {{_ : UA}} where\n \u228e\ud835\udfd8-inl : A \u2261 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl = ua \u228e\ud835\udfd8-inl-equiv\n\n \ud835\udfd8\u228e-inr : A \u2261 (\ud835\udfd8 \u228e A)\n \ud835\udfd8\u228e-inr = \u228e\ud835\udfd8-inl \u2219 \u228e-comm\n\n \ud835\udfd9\u00d7-snd : (\ud835\udfd9 \u00d7 A) \u2261 A\n \ud835\udfd9\u00d7-snd = \u03a3\ud835\udfd9-snd\n\n \u00d7\ud835\udfd9-fst : (A \u00d7 \ud835\udfd9) \u2261 A\n \u00d7\ud835\udfd9-fst = \u00d7-comm \u2219 \ud835\udfd9\u00d7-snd\n\n \ud835\udfd8\u00d7-fst : (\ud835\udfd8 \u00d7 A) \u2261 \ud835\udfd8\n \ud835\udfd8\u00d7-fst = \u03a3\ud835\udfd8-fst\n\n \u00d7\ud835\udfd8-snd : (A \u00d7 \ud835\udfd8) \u2261 \ud835\udfd8\n \u00d7\ud835\udfd8-snd = \u00d7-comm \u2219 \u03a3\ud835\udfd8-fst\n\n -- old names\n A\u00d7\ud835\udfd9\u2261A = \u00d7\ud835\udfd9-fst\n \ud835\udfd9\u00d7A\u2261A = \ud835\udfd9\u00d7-snd\n \ud835\udfd8\u228eA\u2261A = \ud835\udfd8\u228e-inr\n A\u228e\ud835\udfd8\u2261A = \u228e\ud835\udfd8-inl\n \ud835\udfd8\u00d7A\u2261\ud835\udfd8 = \ud835\udfd8\u00d7-fst\n A\u00d7\ud835\udfd8\u2261\ud835\udfd8 = \u00d7\ud835\udfd8-snd\n\nmodule _ {A : \u2605}{B : A \u2192 \u2605}{C : \u03a3 A B \u2192 \u2605} where\n -- AC: Dependent axiom of choice\n -- In Type Theory it happens to be neither an axiom nor to be choosing anything.\n \u03a0\u03a3-comm-equiv : (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2243 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm-equiv = equiv (\u03bb H \u2192 fst \u2218 H , snd \u2218 H) (\u03bb H \u2192 < fst H , snd H >) (\u03bb H \u2192 idp) (\u03bb H \u2192 idp)\n\n \u03a0\u03a3-comm : {{_ : UA}}\n \u2192 (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2261 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm = ua \u03a0\u03a3-comm-equiv\n\nmodule _ {\u2113}{A : \ud835\udfda \u2192 \u2605_ \u2113}{{_ : UA}}{{_ : FunExt}} where\n \u03a3\ud835\udfda-\u228e : \u03a3 \ud835\udfda A \u2261 (A 0\u2082 \u228e A 1\u2082)\n \u03a3\ud835\udfda-\u228e = \u03a3-fst\u2243\u2032 \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 _ \u2219 dist-\u228e-\u03a3 \u2219 \u228e= \u03a3\ud835\udfd9-snd \u03a3\ud835\udfd9-snd\n\n \u03a0\ud835\udfda-\u00d7 : \u03a0 \ud835\udfda A \u2261 (A 0\u2082 \u00d7 A 1\u2082)\n \u03a0\ud835\udfda-\u00d7 = \u03a0-dom\u2243\u2032 \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 _ \u2219 dist-\u00d7-\u03a0 \u2219 \u00d7= (\u03a0\ud835\udfd9-uniq _) (\u03a0\ud835\udfd9-uniq _)\n\n \u03a0\ud835\udfdaF\u2261F\u2080\u00d7F\u2081 = \u03a0\ud835\udfda-\u00d7\n\nmodule _ {A : \u2605}{{_ : UA}}{{_ : FunExt}} where\n \u03a3\ud835\udfda-\u228e\u2032 : (\ud835\udfda \u00d7 A) \u2261 (A \u228e A)\n \u03a3\ud835\udfda-\u228e\u2032 = \u03a3\ud835\udfda-\u228e\n\n \u03a0\ud835\udfda\u2192\u00d7\u2032 : (\ud835\udfda \u2192 A) \u2261 (A \u00d7 A)\n \u03a0\ud835\udfda\u2192\u00d7\u2032 = \u03a0\ud835\udfda-\u00d7\n\nmodule _ {a}{A : \u2605_ a} where\n\n \u03a3\u2261x\u2243\ud835\udfd9 : \u2200 x \u2192 (\u03a3 A (_\u2261_ x)) \u2243 \ud835\udfd9\n \u03a3\u2261x\u2243\ud835\udfd9 x = equiv (\u03bb _ \u2192 _) (\u03bb _ \u2192 x , idp) (\u03bb _ \u2192 idp) (\u03bb p \u2192 pair= (snd p) (tr-r\u2261 (snd p) idp))\n\n \u03a3x\u2261\u2243\ud835\udfd9 : \u2200 x \u2192 (\u03a3 A (flip _\u2261_ x)) \u2243 \ud835\udfd9\n \u03a3x\u2261\u2243\ud835\udfd9 x = equiv (\u03bb _ \u2192 _) (\u03bb _ \u2192 x , idp) (\u03bb _ \u2192 idp) (\u03bb p \u2192 pair= (! snd p) ( tr-l\u2261 (! snd p) idp \u2219\n \u2219-refl (! (! (snd p))) \u2219 !-inv (snd p)))\n\nmodule _ {ab c}{A B : \u2605_ ab}{C : A \u2192 B \u2192 \u2605_ c}{{_ : UA}}{{_ : FunExt}} where\n\n \u03a0\u228e-equiv : (\u03a0 (A \u228e B) [inl: (\u03bb x \u2192 \u2200 y \u2192 C x y) ,inr: (\u03bb y \u2192 \u2200 x \u2192 C x y) ]) \u2243 ((t : \ud835\udfda)(x : A)(y : B) \u2192 C x y)\n \u03a0\u228e-equiv = equiv (\u03bb f \u2192 [0: (\u03bb x y \u2192 f (inl x) y) 1: ((\u03bb x y \u2192 f (inr y) x)) ])\n (\u03bb f \u2192 [inl: f 0\u2082 ,inr: flip (f 1\u2082) ])\n (\u03bb f \u2192 \u03bb= [0: idp 1: idp ])\n (\u03bb f \u2192 \u03bb= [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ])\n\n \u03a0\u228e : (\u03a0 (A \u228e B) [inl: (\u03bb x \u2192 \u2200 y \u2192 C x y) ,inr: (\u03bb y \u2192 \u2200 x \u2192 C x y) ]) \u2261 ((t : \ud835\udfda)(x : A)(y : B) \u2192 C x y)\n \u03a0\u228e = ua \u03a0\u228e-equiv\n\nmodule _ {ab c}{A B : \u2605_ ab}{C : \u2605_ c}{{_ : UA}}{{_ : FunExt}} where\n \u03a0\u228e\u2032 : (\u03a0 (A \u228e B) [inl: const (B \u2192 C) ,inr: const (A \u2192 C) ]) \u2261 (\ud835\udfda \u2192 A \u2192 B \u2192 C)\n \u03a0\u228e\u2032 = \u03a0\u228e\n\nmodule _ where\n\n private\n maybe-elim : {X : Set}(m : Maybe X) \u2192 m \u2262 nothing \u2192 X\n maybe-elim {X} m = maybe {B = \u03bb m' \u2192 m' \u2262 _ \u2192 _} (\u03bb x _ \u2192 x) (\u03bb e \u2192 \ud835\udfd8-elim (e idp)) m\n\n maybe-elim-just : {X : Set}(m : Maybe X)(p : m \u2262 nothing)\n \u2192 just (maybe-elim m p) \u2261 m\n maybe-elim-just (just x) p = idp\n maybe-elim-just nothing p = \ud835\udfd8-elim (p idp)\n\n maybe-elim-cong : \u2200 {X : Set}{m m' : Maybe X}{p p'} \u2192 m \u2261 m'\n \u2192 maybe-elim m p \u2261 maybe-elim m' p'\n maybe-elim-cong {m = just x} idp = idp\n maybe-elim-cong {m = nothing} {p = p} idp = \ud835\udfd8-elim (p idp)\n\n not-just : {X : Set}{x : X} \u2192 _\u2262_ {A = Maybe X} (just x) nothing\n not-just ()\n\n !!-p : \u2200 {x}{X : Set x}{x y : X}(p : x \u2261 y) \u2192 ! (! p) \u2261 p\n !!-p idp = idp\n\n !\u2219 : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 ! p \u2219 p \u2261 idp\n !\u2219 idp = idp\n\n \u2219! : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 p \u2219 ! p \u2261 idp\n \u2219! idp = idp\n\n\n record PreservePoint {A B : Set}(eq : Maybe A \u2261 Maybe B) : Set where\n field\n coe-p : \u2200 x \u2192 coe eq (just x) \u2262 nothing\n coe!-p : \u2200 x \u2192 coe! eq (just x) \u2262 nothing\n open PreservePoint\n\n nothing-to-nothing : {A B : Set}(eq : Maybe A \u2261 Maybe B)\n \u2192 coe eq nothing \u2261 nothing \u2192 PreservePoint eq\n nothing-to-nothing eq n2n = record { coe-p = \u03bb x p \u2192 not-just (coe-inj eq (p \u2219 ! n2n))\n ; coe!-p = \u03bb x p \u2192 not-just (coe-same (! !\u2219 eq) (just x)\n \u2219 ! (coe\u2218coe eq (! eq) (just x)) \u2219 ap (coe eq) p \u2219 n2n) }\n\n !-PP : \u2200 {A B : Set} {e : Maybe A \u2261 Maybe B} \u2192 PreservePoint e \u2192 PreservePoint (! e)\n !-PP {e = e} e-pp = record { coe-p = coe!-p e-pp\n ; coe!-p = \u03bb x p \u2192 coe-p e-pp x ( ap (\u03bb X \u2192 coe X (just x)) (! !!-p e ) \u2219 p) }\n\n to : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e \u2192 A \u2192 B\n to e e-pp x = maybe-elim (coe e (just x)) (coe-p e-pp x)\n\n module _{A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}{ep ep'}(e'\u2219e=id : e' \u2219 e \u2261 idp)(x : B) where\n to-to : to e ep (to e' ep' x) \u2261 x\n to-to = just-injective (ap just (maybe-elim-cong\n {m = coe e (just (maybe-elim (coe e' (just x)) _))}\n {m' = coe e (coe e' (just x))}\n {p' = \u03bb q \u2192 not-just (! p \u2219 q)} (ap (coe e) (maybe-elim-just _ _)))\n \u2219 maybe-elim-just (coe e (coe e' (just x))) (\u03bb q \u2192 not-just (! p \u2219 q))\n \u2219 p)\n where\n p : coe e (coe e' (just x)) \u2261 just x\n p = coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n\n cto : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 Maybe A \u2192 Maybe B\n cto e = maybe (maybe\u2032 just (coe e nothing) \u2218 coe e \u2218 just) nothing\n\n module _ {A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}(e'\u2219e=id : e' \u2219 e \u2261 idp) where\n cto-cto : (x : Maybe B) \u2192 cto e (cto e' x) \u2261 x\n cto-cto nothing = idp\n cto-cto (just x) with coe e' (just x) | coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n cto-cto (just x) | just x\u2081 | p = ap (maybe just (coe e nothing)) p\n cto-cto (just x) | nothing | p with coe e' nothing\n | coe\u2218coe e e' nothing \u2219 coe-same e'\u2219e=id nothing\n cto-cto (just x) | nothing | p | just y | q = ap (maybe just (coe e nothing)) q \u2219 p\n cto-cto (just x) | nothing | p | nothing | q = ! q \u2219 p\n\n module _ {A B : Set} where\n create-PP-\u2243 : Maybe A \u2261 Maybe B \u2192 Maybe A \u2243 Maybe B\n create-PP-\u2243 e = equiv (cto e)\n (cto (! e))\n (cto-cto {e = e} {e' = ! e} (!\u2219 e))\n (cto-cto {e = ! e}{e' = e} (\u2219! e))\n\n create-PP : {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 \u03a3 (Maybe A \u2261 Maybe B) PreservePoint\n create-PP eq = ua (create-PP-\u2243 eq) , nothing-to-nothing _ (coe-\u03b2 (create-PP-\u2243 eq) nothing)\n\n Maybe-injective-PP : {{_ : UA}} \u2192 (e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e\n \u2192 A \u2261 B\n Maybe-injective-PP e e-pp = ua\n (equiv (to e e-pp)\n (to (! e) (!-PP e-pp))\n (to-to {e = e} { ! e} {e-pp}{ !-PP e-pp} (!\u2219 e))\n (to-to {e = ! e} {e} { !-PP e-pp} {e-pp} (\u2219! e)))\n\n Maybe-injective : \u2200 {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 A \u2261 B\n Maybe-injective e = let (e' , e'-pp) = create-PP e in Maybe-injective-PP e' e'-pp\n\n\nmodule _ {a}{A : \u2605_ a} where\n Maybe\u2243\ud835\udfd9\u228e : Maybe A \u2243 (\ud835\udfd9 \u228e A)\n Maybe\u2243\ud835\udfd9\u228e = equiv (maybe inr (inl _)) [inl: const nothing ,inr: just ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (maybe (\u03bb x \u2192 idp) idp)\n\n module _ {{_ : UA}} where\n\n Maybe\u2261\ud835\udfd9\u228e : Maybe A \u2261 (\ud835\udfd9 \u228e A)\n Maybe\u2261\ud835\udfd9\u228e = ua Maybe\u2243\ud835\udfd9\u228e\n\n Maybe\u2261Lift\ud835\udfd9\u228e : Maybe A \u2261 (Lift {\u2113 = a} \ud835\udfd9 \u228e A)\n Maybe\u2261Lift\ud835\udfd9\u228e = ua (equiv (maybe inr (inl _))\n [inl: const nothing ,inr: just ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n (maybe (\u03bb _ \u2192 idp) idp))\n\nFin0\u2243\ud835\udfd8 : Fin 0 \u2243 \ud835\udfd8\nFin0\u2243\ud835\udfd8 = equiv (\u03bb ()) (\u03bb ()) (\u03bb ()) (\u03bb ())\n\nFin1\u2243\ud835\udfd9 : Fin 1 \u2243 \ud835\udfd9\nFin1\u2243\ud835\udfd9 = equiv _ (\u03bb _ \u2192 zero) (\u03bb _ \u2192 idp) \u03b2\n where \u03b2 : (_ : Fin 1) \u2192 _\n \u03b2 zero = idp\n \u03b2 (suc ())\n\nmodule _ {{_ : UA}} where\n Fin0\u2261\ud835\udfd8 : Fin 0 \u2261 \ud835\udfd8\n Fin0\u2261\ud835\udfd8 = ua Fin0\u2243\ud835\udfd8\n\n Fin1\u2261\ud835\udfd9 : Fin 1 \u2261 \ud835\udfd9\n Fin1\u2261\ud835\udfd9 = ua Fin1\u2243\ud835\udfd9\n\nmodule _ where\n isZero? : \u2200 {n}{A : Fin (suc n) \u2192 Set} \u2192 ((i : Fin n) \u2192 A (suc i)) \u2192 A zero\n \u2192 (i : Fin (suc n)) \u2192 A i\n isZero? f x zero = x\n isZero? f x (suc i) = f i\n\n Fin\u2218suc\u2243\ud835\udfd9\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2243 (\ud835\udfd9 \u228e Fin n)\n Fin\u2218suc\u2243\ud835\udfd9\u228eFin = equiv (isZero? inr (inl _)) [inl: (\u03bb _ \u2192 zero) ,inr: suc ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n (isZero? (\u03bb _ \u2192 idp) idp)\n\n Fin\u2218suc\u2261\ud835\udfd9\u228eFin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 (\ud835\udfd9 \u228e Fin n)\n Fin\u2218suc\u2261\ud835\udfd9\u228eFin = ua Fin\u2218suc\u2243\ud835\udfd9\u228eFin\n\nFin-\u228e-+ : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u228e Fin n) \u2261 Fin (m \u2115.+ n)\nFin-\u228e-+ {zero} = \u228e= Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm \u2219 ! \u228e\ud835\udfd8-inl\nFin-\u228e-+ {suc m} = \u228e= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ap (_\u228e_ \ud835\udfd9) Fin-\u228e-+ \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\n\nFin-\u00d7-* : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u00d7 Fin n) \u2261 Fin (m \u2115.* n)\nFin-\u00d7-* {zero} = \u00d7= Fin0\u2261\ud835\udfd8 idp \u2219 \u03a3\ud835\udfd8-fst \u2219 ! Fin0\u2261\ud835\udfd8\nFin-\u00d7-* {suc m} = \u00d7= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u228e-\u00d7 \u2219 \u228e= \u03a3\ud835\udfd9-snd Fin-\u00d7-* \u2219 Fin-\u228e-+\n\nFin-\u2192-^ : \u2200 {{_ : UA}}{{_ : FunExt}}{m n} \u2192 (Fin m \u2192 Fin n) \u2261 Fin (n \u2115.^ m)\nFin-\u2192-^ {zero} = \u2192= Fin0\u2261\ud835\udfd8 idp \u2219 \u03a0\ud835\udfd8-uniq\u2032 _ \u2219 \u228e\ud835\udfd8-inl \u2219 ap (_\u228e_ \ud835\udfd9) (! Fin0\u2261\ud835\udfd8)\n \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\nFin-\u2192-^ {suc m} = \u2192= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u00d7-\u2192 \u2219 \u00d7= (\u03a0\ud835\udfd9-uniq _) Fin-\u2192-^ \u2219 Fin-\u00d7-*\n\nFin\u2218suc\u2261Maybe\u2218Fin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 Maybe (Fin n)\nFin\u2218suc\u2261Maybe\u2218Fin = Fin\u2218suc\u2261\ud835\udfd9\u228eFin \u2219 ! Maybe\u2261\ud835\udfd9\u228e\n\nFin-injective : \u2200 {{_ : UA}}{m n} \u2192 Fin m \u2261 Fin n \u2192 m \u2261 n\nFin-injective {zero} {zero} Finm\u2261Finn = idp\nFin-injective {zero} {suc n} Finm\u2261Finn = \ud835\udfd8-elim (coe (! Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {zero} Finm\u2261Finn = \ud835\udfd8-elim (coe (Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {suc n} Finm\u2261Finn\n = ap suc (Fin-injective (Maybe-injective\n (! Fin\u2218suc\u2261Maybe\u2218Fin \u2219 Finm\u2261Finn \u2219 Fin\u2218suc\u2261Maybe\u2218Fin)))\n\nFin\u228e-injective : \u2200 {{_ : UA}}{A B : Set} n \u2192 (Fin n \u228e A) \u2261 (Fin n \u228e B) \u2192 A \u2261 B\nFin\u228e-injective zero f = \u228e\ud835\udfd8-inl \u2219 \u228e-comm \u2219 \u228e= (! Fin0\u2261\ud835\udfd8) idp\n \u2219 f \u2219 (\u228e= Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm) \u2219 ! \u228e\ud835\udfd8-inl\nFin\u228e-injective (suc n) f = Fin\u228e-injective n (Maybe-injective\n (Maybe\u2261\ud835\udfd9\u228e \u2219 \u228e-assoc \u2219 \u228e= (! Fin\u2218suc\u2261\ud835\udfd9\u228eFin) idp \u2219 f\n \u2219 \u228e= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ! Maybe\u2261\ud835\udfd9\u228e))\n\nLift\u2243id : \u2200 {a} {A : \u2605_ a} \u2192 Lift {a} {a} A \u2243 A\nLift\u2243id = equiv lower lift (\u03bb _ \u2192 idp) (\u03bb { (lift x) \u2192 idp })\n\nmodule _ {{_ : UA}} where\n Fin-\u2261-\u22611\u2082 : \u2200 b \u2192 Fin (\ud835\udfda\u25b9\u2115 b) \u2261 (b \u2261 1\u2082)\n Fin-\u2261-\u22611\u2082 1\u2082 = Fin1\u2261\ud835\udfd9 \u2219 ua (Is-contr-to-Is-equiv.\ud835\udfd9\u2243 (\u03a9\u2081-set-to-contr \ud835\udfda-is-set 1\u2082))\n Fin-\u2261-\u22611\u2082 0\u2082 = ua (equiv (\u03bb ()) (\u03bb ()) (\u03bb ()) (\u03bb ()))\n\n Fin-\u2261-\u22610\u2082 : \u2200 b \u2192 Fin (\ud835\udfda\u25b9\u2115 (not b)) \u2261 (b \u2261 0\u2082)\n Fin-\u2261-\u22610\u2082 b = Fin-\u2261-\u22611\u2082 (not b) \u2219 ! \u2013>-paths-equiv twist-equiv\n\n \u2713-\u2227-\u00d7 : \u2200 x y \u2192 \u2713 (x \u2227 y) \u2261 (\u2713 x \u00d7 \u2713 y)\n \u2713-\u2227-\u00d7 1\u2082 y = ! \ud835\udfd9\u00d7-snd\n \u2713-\u2227-\u00d7 0\u2082 y = ! \ud835\udfd8\u00d7-fst\n\n count-\u2261 : \u2200 {a} {A : \u2605_ a} (p : A \u2192 \ud835\udfda) x \u2192 Fin (\ud835\udfda\u25b9\u2115 (p x)) \u2261 (p x \u2261 1\u2082)\n count-\u2261 p x = Fin-\u2261-\u22611\u2082 (p x)\n\n\n Lift\u2261id : \u2200 {a} {A : \u2605_ a} \u2192 Lift {a} {a} A \u2261 A\n Lift\u2261id = ua Lift\u2243id\n\n \u03a0\ud835\udfd9F\u2261F : \u2200 {\u2113} {F : \ud835\udfd9 \u2192 \u2605_ \u2113} \u2192 \u03a0 \ud835\udfd9 F \u2261 F _\n \u03a0\ud835\udfd9F\u2261F = ua (equiv (\u03bb x \u2192 x _) const (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\n \ud835\udfd9\u2192A\u2261A : \u2200 {\u2113} {A : \u2605_ \u2113} \u2192 (\ud835\udfd9 \u2192 A) \u2261 A\n \ud835\udfd9\u2192A\u2261A = \u03a0\ud835\udfd9F\u2261F\n\n not-\ud835\udfda\u2261\ud835\udfda : \ud835\udfda \u2261 \ud835\udfda\n not-\ud835\udfda\u2261\ud835\udfda = twist\n\n Maybe\ud835\udfd8\u2261\ud835\udfd9 : Maybe \ud835\udfd8 \u2261 \ud835\udfd9\n Maybe\ud835\udfd8\u2261\ud835\udfd9 = Maybe\u2261\ud835\udfd9\u228e \u2219 ! \u228e\ud835\udfd8-inl\n\n Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe : \u2200 {a} {A : \u2605_ a} n \u2192 Maybe (Maybe^ n A) \u2261 Maybe^ n (Maybe A)\n Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe zero = idp\n Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe (suc n) = ap Maybe (Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe n)\n\n Maybe^\ud835\udfd8\u2261Fin : \u2200 n \u2192 Maybe^ n \ud835\udfd8 \u2261 Fin n\n Maybe^\ud835\udfd8\u2261Fin zero = ! Fin0\u2261\ud835\udfd8\n Maybe^\ud835\udfd8\u2261Fin (suc n) = ap Maybe (Maybe^\ud835\udfd8\u2261Fin n) \u2219 ! Fin\u2218suc\u2261Maybe\u2218Fin\n\n Maybe^\ud835\udfd9\u2261Fin1+ : \u2200 n \u2192 Maybe^ n \ud835\udfd9 \u2261 Fin (suc n)\n Maybe^\ud835\udfd9\u2261Fin1+ n = ap (Maybe^ n) (! Maybe\ud835\udfd8\u2261\ud835\udfd9) \u2219 ! Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe n \u2219 Maybe^\ud835\udfd8\u2261Fin (suc n)\n\n Maybe-\u228e : \u2200 {a} {A B : \u2605_ a} \u2192 (Maybe A \u228e B) \u2261 Maybe (A \u228e B)\n Maybe-\u228e {a} = \u228e= Maybe\u2261Lift\ud835\udfd9\u228e idp \u2219 ! \u228e-assoc \u2219 ! Maybe\u2261Lift\ud835\udfd9\u228e\n\n Maybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \ud835\udfd8 \u228e Maybe^ n A) \u2261 Maybe^ (m + n) A\n Maybe^-\u228e-+ zero n = ! \ud835\udfd8\u228e-inr\n Maybe^-\u228e-+ (suc m) n = Maybe-\u228e \u2219 ap Maybe (Maybe^-\u228e-+ m n)\n\nmodule _ {A : Set} {p q : A \u2192 \ud835\udfda} where\n \u03a3AP\u00acQ : Set\n \u03a3AP\u00acQ = \u03a3 A (\u03bb x \u2192 p x \u2261 1\u2082 \u00d7 q x \u2261 0\u2082)\n\n \u03a3A\u00acPQ : Set\n \u03a3A\u00acPQ = \u03a3 A (\u03bb x \u2192 p x \u2261 0\u2082 \u00d7 q x \u2261 1\u2082)\n\n module EquivalentSubsets (e : \u03a3AP\u00acQ \u2261 \u03a3A\u00acPQ) where\n\n f' : \u03a3AP\u00acQ \u2192 \u03a3A\u00acPQ\n f' = coe e\n\n f-1' : \u03a3A\u00acPQ \u2192 \u03a3AP\u00acQ\n f-1' = coe! e\n\n f-1f' : \u2200 x \u2192 f-1' (f' x) \u2261 x\n f-1f' = coe!-inv-l e\n\n ff-1' : \u2200 x \u2192 f' (f-1' x) \u2261 x\n ff-1' = coe!-inv-r e\n\n f : (x : A) \u2192 p x \u2261 1\u2082 \u2192 q x \u2261 0\u2082 \u2192 \u03a3A\u00acPQ\n f x px qx = f' (x , (px , qx))\n\n f-1 : (x : A) \u2192 p x \u2261 0\u2082 \u2192 q x \u2261 1\u2082 \u2192 \u03a3AP\u00acQ\n f-1 x px qx = f-1' (x , (px , qx))\n\n f-1f : \u2200 x px nqx \u2192\n let y = snd (f x px nqx) in fst (f-1 (fst (f x px nqx)) (fst y) (snd y)) \u2261 x\n f-1f x px nqx = \u2261.cong fst (f-1f' (x , (px , nqx)))\n\n ff-1 : \u2200 x px nqx \u2192\n let y = snd (f-1 x px nqx) in fst (f (fst (f-1 x px nqx)) (fst y) (snd y)) \u2261 x\n ff-1 x px nqx = \u2261.cong fst (ff-1' (x , (px , nqx)))\n\n \u03c0' : (x : A) (px qx : \ud835\udfda) \u2192 p x \u2261 px \u2192 q x \u2261 qx \u2192 A\n \u03c0' x 1\u2082 1\u2082 px qx = x\n \u03c0' x 1\u2082 0\u2082 px qx = fst (f x px qx)\n \u03c0' x 0\u2082 1\u2082 px qx = fst (f-1 x px qx)\n \u03c0' x 0\u2082 0\u2082 px qx = x\n\n \u03c0 : A \u2192 A\n \u03c0 x = \u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n 0\u22621 : 0\u2082 \u2262 1\u2082\n 0\u22621 ()\n\n \u03c001 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px0 : p x \u2261 0\u2082) (qx1 : q x \u2261 1\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 0\u2082 1\u2082 px0 qx1\n \u03c001 x 1\u2082 _ ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym px0) ppx))\n \u03c001 x 0\u2082 1\u2082 ppx qqx px0 qx1 = \u2261.ap\u2082 (\u03bb z1 z2 \u2192 fst (f-1 x z1 z2)) (UIP-set \ud835\udfda-is-set ppx px0) (UIP-set \ud835\udfda-is-set qqx qx1)\n \u03c001 x 0\u2082 0\u2082 ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qqx) qx1))\n\n \u03c010 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px1 : p x \u2261 1\u2082) (qx0 : q x \u2261 0\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 1\u2082 0\u2082 px1 qx0\n \u03c010 x 0\u2082 _ ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym ppx) px1))\n \u03c010 x 1\u2082 0\u2082 ppx qqx px1 qx0 = \u2261.ap\u2082 (\u03bb z1 z2 \u2192 fst (f x z1 z2)) (UIP-set \ud835\udfda-is-set ppx px1) (UIP-set \ud835\udfda-is-set qqx qx0)\n \u03c010 x 1\u2082 1\u2082 ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qx0) qqx))\n\n \u03c0'bb : \u2200 {b} x (px : p x \u2261 b) (qx : q x \u2261 b) ppx qqx ([ppx] : p x \u2261 ppx) ([qqx] : q x \u2261 qqx) \u2192 \u03c0' x ppx qqx [ppx] [qqx] \u2261 x\n \u03c0'bb x px qx 1\u2082 1\u2082 [ppx] [qqx] = \u2261.refl\n \u03c0'bb x px qx 1\u2082 0\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [qqx]) (\u2261.trans qx (\u2261.trans (\u2261.sym px) [ppx]))))\n \u03c0'bb x px qx 0\u2082 1\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [ppx]) (\u2261.trans px (\u2261.trans (\u2261.sym qx) [qqx]))))\n \u03c0'bb x px qx 0\u2082 0\u2082 [ppx] [qqx] = \u2261.refl\n\n \u03c0\u03c0' : \u2200 x px qx [px] [qx] \u2192 let y = (\u03c0' x px qx [px] [qx]) in \u03c0' y (p y) (q y) \u2261.refl \u2261.refl \u2261 x\n \u03c0\u03c0' x 1\u2082 1\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n \u03c0\u03c0' x 1\u2082 0\u2082 px qx = let fx = f x px qx in let pfx = fst (snd fx) in let qfx = snd (snd fx) in \u2261.trans (\u03c001 (fst fx) (p (fst fx)) (q (fst fx)) \u2261.refl \u2261.refl pfx qfx) (f-1f x px qx)\n \u03c0\u03c0' x 0\u2082 1\u2082 px qx = let fx = f-1 x px qx in let pfx = fst (snd fx) in let qfx = snd (snd fx) in \u2261.trans (\u03c010 (fst fx) (p (fst fx)) (q (fst fx)) \u2261.refl \u2261.refl pfx qfx) (ff-1 x px qx)\n \u03c0\u03c0' x 0\u2082 0\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n\n \u03c0\u03c0 : \u2200 x \u2192 \u03c0 (\u03c0 x) \u2261 x\n \u03c0\u03c0 x = \u03c0\u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n prop' : \u2200 px qx x ([px] : p x \u2261 px) ([qx] : q x \u2261 qx) \u2192 q (\u03c0' x px qx [px] [qx]) \u2261 px\n prop' 1\u2082 1\u2082 x px qx = qx\n prop' 1\u2082 0\u2082 x px qx = snd (snd (f x px qx))\n prop' 0\u2082 1\u2082 x px qx = snd (snd (f-1 x px qx))\n prop' 0\u2082 0\u2082 x px qx = qx\n\n prop'' : \u2200 x \u2192 p x \u2261 q (\u03c0 x)\n prop'' x = \u2261.sym (prop' (p x) (q x) x \u2261.refl \u2261.refl)\n\n prop : {{_ : FunExt}} \u2192 p \u2261 q \u2218 \u03c0\n prop = \u03bb= prop''\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Type\n\nopen import Level.NP\nopen import HoTT\nopen import Function.NP\nopen import Function.Extensionality\n\nopen import Data.Maybe.NP using (Maybe ; just ; nothing ; maybe ; maybe\u2032 ; just-injective; Maybe^)\nopen import Data.Zero using (\ud835\udfd8 ; \ud835\udfd8-elim)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Two\nopen import Data.Fin as Fin using (Fin ; suc ; zero)\nopen import Data.Nat.NP as \u2115 using (\u2115 ; suc ; zero; _+_)\nopen import Data.Product.NP renaming (map to map\u00d7)\nopen import Data.Sum using (_\u228e_) renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_]; map to map\u228e)\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_ ; ap; coe; coe!; !_; _\u2219_; J ; inspect ; Reveal_is_ ; [_]; tr; ap\u2082; apd) renaming (refl to idp; _\u2257_ to _\u223c_; J-orig to J')\n\nmodule Type.Identities where\n\nopen Equivalences\n\nmodule _ {a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 B\u2081 : \u2605_ b}(A= : A\u2080 \u2261 A\u2081)(B= : B\u2080 \u2261 B\u2081) where\n \u00d7= : (A\u2080 \u00d7 B\u2080) \u2261 (A\u2081 \u00d7 B\u2081)\n \u00d7= = ap\u2082 _\u00d7_ A= B=\n\n \u228e= : (A\u2080 \u228e B\u2080) \u2261 (A\u2081 \u228e B\u2081)\n \u228e= = ap\u2082 _\u228e_ A= B=\n\n \u2192= : (A\u2080 \u2192 B\u2080) \u2261 (A\u2081 \u2192 B\u2081)\n \u2192= = ap\u2082 -\u2192- A= B=\n\nmodule _ {a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 B\u2081 : \u2605_ b}(A\u2243 : A\u2080 \u2243 A\u2081)(B\u2243 : B\u2080 \u2243 B\u2081) where\n{-\n \u00d7\u2243 : (A\u2080 \u00d7 B\u2080) \u2243 (A\u2081 \u00d7 B\u2081)\n \u00d7\u2243 = equiv (map\u00d7 (\u2013> A\u2243) (\u2013> B\u2243)) (map\u00d7 (<\u2013 A\u2243) (<\u2013 B\u2243))\n (\u03bb y \u2192 pair= (<\u2013-inv-r A\u2243 (fst y)) ({!!} \u2219 <\u2013-inv-r B\u2243 (snd y)))\n {!!}\n -}\n\n \u228e\u2243 : (A\u2080 \u228e B\u2080) \u2243 (A\u2081 \u228e B\u2081)\n \u228e\u2243 = equiv (map\u228e (\u2013> A\u2243) (\u2013> B\u2243)) (map\u228e (<\u2013 A\u2243) (<\u2013 B\u2243))\n [inl: (\u03bb x \u2192 ap inl (<\u2013-inv-r A\u2243 x)) ,inr: ap inr \u2218 <\u2013-inv-r B\u2243 ]\n [inl: (\u03bb x \u2192 ap inl (<\u2013-inv-l A\u2243 x)) ,inr: ap inr \u2218 <\u2013-inv-l B\u2243 ]\n\n \u2192\u2243 : {{_ : FunExt}} \u2192 (A\u2080 \u2192 B\u2080) \u2243 (A\u2081 \u2192 B\u2081)\n \u2192\u2243 = equiv (\u03bb f \u2192 \u2013> B\u2243 \u2218 f \u2218 <\u2013 A\u2243)\n (\u03bb f \u2192 <\u2013 B\u2243 \u2218 f \u2218 \u2013> A\u2243)\n (\u03bb f \u2192 \u03bb= (\u03bb x \u2192 <\u2013-inv-r B\u2243 _ \u2219 ap f (<\u2013-inv-r A\u2243 x)))\n (\u03bb f \u2192 \u03bb= (\u03bb x \u2192 <\u2013-inv-l B\u2243 _ \u2219 ap f (<\u2013-inv-l A\u2243 x)))\n\nmodule _ {{_ : FunExt}}{a}(A : \u2605_ a){b}{B\u2080 B\u2081 : A \u2192 \u2605_ b}(B : (x : A) \u2192 B\u2080 x \u2261 B\u2081 x) where\n \u03a3=\u2032 : \u03a3 A B\u2080 \u2261 \u03a3 A B\u2081\n \u03a3=\u2032 = ap (\u03a3 A) (\u03bb= B)\n\n \u03a0=\u2032 : \u03a0 A B\u2080 \u2261 \u03a0 A B\u2081\n \u03a0=\u2032 = ap (\u03a0 A) (\u03bb= B)\n\nmodule _ {a b}{A\u2080 : \u2605_ a}{B\u2080 : A\u2080 \u2192 \u2605_ b}{{_ : FunExt}} where\n \u03a3= : {A\u2081 : \u2605_ a}(A= : A\u2080 \u2261 A\u2081)\n {B\u2081 : A\u2081 \u2192 \u2605_ b}(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (coe A= x))\n \u2192 \u03a3 A\u2080 B\u2080 \u2261 \u03a3 A\u2081 B\u2081\n \u03a3= = J (\u03bb A\u2081 A= \u2192 {B\u2081 : A\u2081 \u2192 \u2605_ b}(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (coe A= x))\n \u2192 \u03a3 A\u2080 B\u2080 \u2261 \u03a3 A\u2081 B\u2081) (\u03a3=\u2032 _)\n -- \u03a3= idp B= = \u03a3=\u2032 _ B=\n\n \u03a0= : \u2200 {A\u2081 : \u2605_ a}(A= : A\u2080 \u2261 A\u2081)\n {B\u2081 : A\u2081 \u2192 \u2605_ b}(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (coe A= x))\n \u2192 \u03a0 A\u2080 B\u2080 \u2261 \u03a0 A\u2081 B\u2081\n \u03a0= idp B= = \u03a0=\u2032 _ B=\n\nmodule _ {{_ : UA}}{{_ : FunExt}}{a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 : A\u2080 \u2192 \u2605_ b}{B\u2081 : A\u2081 \u2192 \u2605_ b} where\n \u03a3\u2243 : (A\u2243 : A\u2080 \u2243 A\u2081)(B= : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (\u2013> A\u2243 x))\n \u2192 \u03a3 A\u2080 B\u2080 \u2261 \u03a3 A\u2081 B\u2081\n \u03a3\u2243 A\u2243 B= = \u03a3= (ua A\u2243) \u03bb x \u2192 B= x \u2219 ap B\u2081 (! coe-\u03b2 A\u2243 x)\n\n \u03a0\u2243 : (A : A\u2080 \u2243 A\u2081)(B : (x : A\u2080) \u2192 B\u2080 x \u2261 B\u2081 (\u2013> A x))\n \u2192 \u03a0 A\u2080 B\u2080 \u2261 \u03a0 A\u2081 B\u2081\n \u03a0\u2243 A B = \u03a0= (ua A) \u03bb x \u2192 B x \u2219 ap B\u2081 (! coe-\u03b2 A x)\n\n {-\nmodule _ {{_ : FunExt}}{a}{A\u2080 A\u2081 : \u2605_ a}{b}{B\u2080 : A\u2080 \u2192 \u2605_ b}{B\u2081 : A\u2081 \u2192 \u2605_ b}(A : A\u2080 \u2243 A\u2081)(B : (x : A\u2081) \u2192 B\u2080 (<\u2013 A x) \u2243 B\u2081 x) where\n \u03a0\u2243' : (\u03a0 A\u2080 B\u2080) \u2243 (\u03a0 A\u2081 B\u2081)\n \u03a0\u2243' = equiv (\u03bb f x \u2192 \u2013> (B x) (f (<\u2013 A x)))\n (\u03bb f x \u2192 tr B\u2080 (<\u2013-inv-l A x) (<\u2013 (B (\u2013> A x)) (f (\u2013> A x))))\n (\u03bb f \u2192 \u03bb= (\u03bb x \u2192 {!apd (<\u2013-inv-l A (<\u2013 A x))!}))\n {!\u03bb f \u2192 \u03bb= (\u03bb x \u2192 <\u2013-inv-l B _ \u2219 ap f (<\u2013-inv-l A x))!}\n -}\n\nmodule _ {{_ : UA}}{{_ : FunExt}}{a}{A\u2080 A\u2081 : \u2605_ a}{b} where\n \u03a3-fst\u2243 : \u2200 (A : A\u2080 \u2243 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2080 (B \u2218 \u2013> A) \u2261 \u03a3 A\u2081 B\n \u03a3-fst\u2243 A B = \u03a3\u2243 A (\u03bb x \u2192 idp)\n\n \u03a3-fst= : \u2200 (A : A\u2080 \u2261 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2080 (B \u2218 coe A) \u2261 \u03a3 A\u2081 B\n \u03a3-fst= A = \u03a3-fst\u2243 (coe-equiv A)\n\n \u03a0-dom\u2243 : \u2200 (A : A\u2080 \u2243 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2080 (B \u2218 \u2013> A) \u2261 \u03a0 A\u2081 B\n \u03a0-dom\u2243 A B = \u03a0\u2243 A (\u03bb x \u2192 idp)\n\n \u03a0-dom= : \u2200 (A : A\u2080 \u2261 A\u2081)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2080 (B \u2218 coe A) \u2261 \u03a0 A\u2081 B\n \u03a0-dom= A = \u03a0-dom\u2243 (coe-equiv A)\n\n -- variations where the equiv is transported backward on the right side\n\n \u03a3-fst\u2243\u2032 : (A : A\u2081 \u2243 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2081 B \u2261 \u03a3 A\u2080 (B \u2218 <\u2013 A)\n \u03a3-fst\u2243\u2032 A B = ! \u03a3-fst\u2243 (\u2243-sym A) B\n\n \u03a3-fst=\u2032 : (A : A\u2081 \u2261 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a3 A\u2081 B \u2261 \u03a3 A\u2080 (B \u2218 coe! A)\n \u03a3-fst=\u2032 A = \u03a3-fst\u2243\u2032 (coe-equiv A)\n\n \u03a0-dom\u2243\u2032 : (A : A\u2081 \u2243 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2081 B \u2261 \u03a0 A\u2080 (B \u2218 <\u2013 A)\n \u03a0-dom\u2243\u2032 A B = ! \u03a0-dom\u2243 (\u2243-sym A) B\n\n \u03a0-dom=\u2032 : (A : A\u2081 \u2261 A\u2080)(B : A\u2081 \u2192 \u2605_ b) \u2192 \u03a0 A\u2081 B \u2261 \u03a0 A\u2080 (B \u2218 coe! A)\n \u03a0-dom=\u2032 A = \u03a0-dom\u2243\u2032 (coe-equiv A)\n\nmodule _ {a b c} {A : \u2605_ a} {B : A \u2192 \u2605_ b} {C : \u03a3 A B \u2192 \u2605_ c} where\n \u03a0\u03a3-curry-equiv : \u03a0 (\u03a3 A B) C \u2243 ((x : A) (y : B x) \u2192 C (x , y))\n \u03a0\u03a3-curry-equiv = equiv curry uncurry (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a0\u03a3-curry : {{_ : UA}} \u2192 \u03a0 (\u03a3 A B) C \u2261 ((x : A) (y : B x) \u2192 C (x , y))\n \u03a0\u03a3-curry = ua \u03a0\u03a3-curry-equiv\n\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 : \ud835\udfda \u2243 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 = equiv [0: inl _ 1: inr _ ]\n [inl: const 0\u2082 ,inr: const 1\u2082 ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n [0: idp 1: idp ]\n\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 : {{_ : UA}} \u2192 \ud835\udfda \u2261 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 = ua \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9\n\nmodule _ {{_ : UA}}{A : \u2605}{B C : A \u2192 \u2605} where\n \u03a3\u228e-split : (\u03a3 A (\u03bb x \u2192 B x \u228e C x)) \u2261 (\u03a3 A B \u228e \u03a3 A C)\n \u03a3\u228e-split = ua (equiv (\u03bb { (x , inl y) \u2192 inl (x , y)\n ; (x , inr y) \u2192 inr (x , y) })\n (\u03bb { (inl (x , y)) \u2192 x , inl y\n ; (inr (x , y)) \u2192 x , inr y })\n (\u03bb { (inl (x , y)) \u2192 idp\n ; (inr (x , y)) \u2192 idp })\n (\u03bb { (x , inl y) \u2192 idp\n ; (x , inr y) \u2192 idp }))\n\nmodule _ {{_ : UA}}{a b c}{A : \u2605_ a}{B : \u2605_ b}{C : A \u228e B \u2192 \u2605_ c} where\n dist-\u228e-\u03a3-equiv : \u03a3 (A \u228e B) C \u2243 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u03a3 : \u03a3 (A \u228e B) C \u2261 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3 = ua dist-\u228e-\u03a3-equiv\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0-equiv : \u03a0 (A \u228e B) C \u2243 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u03a0 : \u03a0 (A \u228e B) C \u2261 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0 = ua dist-\u00d7-\u03a0-equiv\n\nmodule _ {{_ : UA}}{A B : \u2605}{C : A \u2192 \u2605}{D : B \u2192 \u2605} where\n dist-\u228e-\u03a3[] : (\u03a3 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a3 A C \u228e \u03a3 B D)\n dist-\u228e-\u03a3[] = dist-\u228e-\u03a3\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0[] : (\u03a0 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a0 A C \u00d7 \u03a0 B D)\n dist-\u00d7-\u03a0[] = dist-\u00d7-\u03a0\n\nmodule _ {{_ : UA}}{A B C : \u2605} where\n dist-\u228e-\u00d7-equiv : ((A \u228e B) \u00d7 C) \u2243 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u00d7 : ((A \u228e B) \u00d7 C) \u2261 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7 = ua dist-\u228e-\u00d7-equiv\n\n module _ {{_ : FunExt}} where\n dist-\u00d7-\u2192-equiv : ((A \u228e B) \u2192 C ) \u2243 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u2192 : ((A \u228e B) \u2192 C) \u2261 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192 = ua dist-\u00d7-\u2192-equiv\n\nmodule _ {a b c}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{C : (x : A) \u2192 B x \u2192 \u2605_ c} where\n \u03a3-assoc-equiv : (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2243 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc-equiv = equiv (\u03bb x \u2192 (fst x , fst (snd x)) , snd (snd x))\n (\u03bb x \u2192 fst (fst x) , snd (fst x) , snd x)\n (\u03bb y \u2192 idp)\n (\u03bb y \u2192 idp)\n\n \u03a3-assoc : {{_ : UA}} \u2192 (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2261 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc = ua \u03a3-assoc-equiv\n\nmodule _ {a b}{A : \u2605_ a} {B : \u2605_ b} where\n \u00d7-comm-equiv : (A \u00d7 B) \u2243 (B \u00d7 A)\n \u00d7-comm-equiv = equiv swap swap (\u03bb y \u2192 idp) (\u03bb x \u2192 idp)\n\n \u00d7-comm : {{_ : UA}} \u2192 (A \u00d7 B) \u2261 (B \u00d7 A)\n \u00d7-comm = ua \u00d7-comm-equiv\n\n \u228e-comm-equiv : (A \u228e B) \u2243 (B \u228e A)\n \u228e-comm-equiv = equiv [inl: inr ,inr: inl ]\n [inl: inr ,inr: inl ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n\n \u228e-comm : {{_ : UA}} \u2192 (A \u228e B) \u2261 (B \u228e A)\n \u228e-comm = ua \u228e-comm-equiv\n\nmodule _ {A B : \u2605}{C : A \u2192 B \u2192 \u2605} where\n \u03a0\u03a0-comm-equiv : ((x : A)(y : B) \u2192 C x y) \u2243 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm-equiv = equiv flip flip (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a0\u03a0-comm : {{_ : UA}} \u2192 ((x : A)(y : B) \u2192 C x y) \u2261 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm = ua \u03a0\u03a0-comm-equiv\n\n \u03a3\u03a3-comm-equiv : (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2243 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm-equiv = equiv (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb _ \u2192 idp)\n (\u03bb _ \u2192 idp)\n\n \u03a3\u03a3-comm : {{_ : UA}} \u2192 (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2261 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm = ua \u03a3\u03a3-comm-equiv\n\nmodule _ {a b c} {A : \u2605_ a} {B : \u2605_ b} {C : \u2605_ c} where\n \u00d7-assoc : {{_ : UA}} \u2192 (A \u00d7 (B \u00d7 C)) \u2261 ((A \u00d7 B) \u00d7 C)\n \u00d7-assoc = \u03a3-assoc\n\n \u228e-assoc-equiv : (A \u228e (B \u228e C)) \u2243 ((A \u228e B) \u228e C)\n \u228e-assoc-equiv = equiv [inl: inl \u2218 inl ,inr: [inl: inl \u2218 inr ,inr: inr ] ]\n [inl: [inl: inl ,inr: inr \u2218 inl ] ,inr: inr \u2218 inr ]\n [inl: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ]\n\n \u228e-assoc : {{_ : UA}} \u2192 (A \u228e (B \u228e C)) \u2261 ((A \u228e B) \u228e C)\n \u228e-assoc = ua \u228e-assoc-equiv\n\nmodule _ {{_ : UA}}{{_ : FunExt}}{a}(A : \ud835\udfd8 \u2192 \u2605_ a) where\n \u03a0\ud835\udfd8-uniq : \u03a0 \ud835\udfd8 A \u2261 Lift \ud835\udfd9\n \u03a0\ud835\udfd8-uniq = ua (equiv _ (\u03bb _ ()) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= (\u03bb())))\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \ud835\udfd8 \u2192 \u2605\u2080) where\n \u03a0\ud835\udfd8-uniq\u2080 : \u03a0 \ud835\udfd8 A \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq\u2080 = ua (equiv _ (\u03bb _ ()) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= (\u03bb())))\n\nmodule _ {{_ : UA}}{a}(A : \ud835\udfd9 \u2192 \u2605_ a) where\n \u03a0\ud835\udfd9-uniq : \u03a0 \ud835\udfd9 A \u2261 A _\n \u03a0\ud835\udfd9-uniq = ua (equiv (\u03bb f \u2192 f _) (\u03bb x _ \u2192 x) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\nmodule _ {{_ : UA}}{a}(A : \u2605_ a) where\n \u03a0\ud835\udfd9-uniq\u2032 : (\ud835\udfd9 \u2192 A) \u2261 A\n \u03a0\ud835\udfd9-uniq\u2032 = \u03a0\ud835\udfd9-uniq (\u03bb _ \u2192 A)\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \u2605\u2080) where\n \u03a0\ud835\udfd8-uniq\u2032 : (\ud835\udfd8 \u2192 A) \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq\u2032 = \u03a0\ud835\udfd8-uniq\u2080 (\u03bb _ \u2192 A)\n\n \ud835\udfd8\u2192A\u2261\ud835\udfd9 = \u03a0\ud835\udfd8-uniq\u2032\n\nmodule _ {{_ : FunExt}}{\u2113}(F G : \ud835\udfd8 \u2192 \u2605_ \u2113) where\n -- also by \u03a0\ud835\udfd8-uniq twice\n \u03a0\ud835\udfd8-uniq' : \u03a0 \ud835\udfd8 F \u2261 \u03a0 \ud835\udfd8 G\n \u03a0\ud835\udfd8-uniq' = \u03a0=\u2032 \ud835\udfd8 (\u03bb())\n\nmodule _ {a \u2113} {A : \ud835\udfd8 \u2192 \u2605_ a} where\n \u03a3\ud835\udfd8-lift\u2218fst-equiv : \u03a3 \ud835\udfd8 A \u2243 Lift {\u2113 = \u2113} \ud835\udfd8\n \u03a3\ud835\udfd8-lift\u2218fst-equiv = equiv (lift \u2218 fst) (\u03bb { (lift ()) }) (\u03bb { (lift ()) }) (\u03bb { (() , _) })\n\nmodule _ {a} {A : \ud835\udfd8 \u2192 \u2605_ a} {{_ : UA}} where\n \u03a3\ud835\udfd8-lift\u2218fst : \u03a3 \ud835\udfd8 A \u2261 Lift {\u2113 = a} \ud835\udfd8\n \u03a3\ud835\udfd8-lift\u2218fst = ua \u03a3\ud835\udfd8-lift\u2218fst-equiv\n\nmodule _ {A : \ud835\udfd8 \u2192 \u2605} where\n \u03a3\ud835\udfd8-fst-equiv : \u03a3 \ud835\udfd8 A \u2243 \ud835\udfd8\n \u03a3\ud835\udfd8-fst-equiv = equiv fst (\u03bb()) (\u03bb()) (\u03bb { (() , _) })\n\n \u03a3\ud835\udfd8-fst : {{_ : UA}} \u2192 \u03a3 \ud835\udfd8 A \u2261 \ud835\udfd8\n \u03a3\ud835\udfd8-fst = ua \u03a3\ud835\udfd8-fst-equiv\n\nmodule _ {a}{A : \ud835\udfd9 \u2192 \u2605_ a} where\n \u03a3\ud835\udfd9-snd-equiv : \u03a3 \ud835\udfd9 A \u2243 A _\n \u03a3\ud835\udfd9-snd-equiv = equiv snd (_,_ _) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a3\ud835\udfd9-snd : {{_ : UA}} \u2192 \u03a3 \ud835\udfd9 A \u2261 A _\n \u03a3\ud835\udfd9-snd = ua \u03a3\ud835\udfd9-snd-equiv\n\nmodule _ {A : \u2605} where\n \u228e\ud835\udfd8-inl-equiv : A \u2243 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl-equiv = equiv inl [inl: id ,inr: (\u03bb()) ] [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb()) ] (\u03bb _ \u2192 idp)\n\n module _ {{_ : UA}} where\n \u228e\ud835\udfd8-inl : A \u2261 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl = ua \u228e\ud835\udfd8-inl-equiv\n\n \ud835\udfd8\u228e-inr : A \u2261 (\ud835\udfd8 \u228e A)\n \ud835\udfd8\u228e-inr = \u228e\ud835\udfd8-inl \u2219 \u228e-comm\n\n \ud835\udfd9\u00d7-snd : (\ud835\udfd9 \u00d7 A) \u2261 A\n \ud835\udfd9\u00d7-snd = \u03a3\ud835\udfd9-snd\n\n \u00d7\ud835\udfd9-fst : (A \u00d7 \ud835\udfd9) \u2261 A\n \u00d7\ud835\udfd9-fst = \u00d7-comm \u2219 \ud835\udfd9\u00d7-snd\n\n \ud835\udfd8\u00d7-fst : (\ud835\udfd8 \u00d7 A) \u2261 \ud835\udfd8\n \ud835\udfd8\u00d7-fst = \u03a3\ud835\udfd8-fst\n\n \u00d7\ud835\udfd8-snd : (A \u00d7 \ud835\udfd8) \u2261 \ud835\udfd8\n \u00d7\ud835\udfd8-snd = \u00d7-comm \u2219 \u03a3\ud835\udfd8-fst\n\n -- old names\n A\u00d7\ud835\udfd9\u2261A = \u00d7\ud835\udfd9-fst\n \ud835\udfd9\u00d7A\u2261A = \ud835\udfd9\u00d7-snd\n \ud835\udfd8\u228eA\u2261A = \ud835\udfd8\u228e-inr\n A\u228e\ud835\udfd8\u2261A = \u228e\ud835\udfd8-inl\n \ud835\udfd8\u00d7A\u2261\ud835\udfd8 = \ud835\udfd8\u00d7-fst\n A\u00d7\ud835\udfd8\u2261\ud835\udfd8 = \u00d7\ud835\udfd8-snd\n\nmodule _ {A : \u2605}{B : A \u2192 \u2605}{C : \u03a3 A B \u2192 \u2605} where\n -- AC: Dependent axiom of choice\n -- In Type Theory it happens to be neither an axiom nor to be choosing anything.\n \u03a0\u03a3-comm-equiv : (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2243 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm-equiv = equiv (\u03bb H \u2192 fst \u2218 H , snd \u2218 H) (\u03bb H \u2192 < fst H , snd H >) (\u03bb H \u2192 idp) (\u03bb H \u2192 idp)\n\n \u03a0\u03a3-comm : {{_ : UA}}\n \u2192 (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2261 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm = ua \u03a0\u03a3-comm-equiv\n\nmodule _ {\u2113}{A : \ud835\udfda \u2192 \u2605_ \u2113}{{_ : UA}}{{_ : FunExt}} where\n \u03a3\ud835\udfda-\u228e : \u03a3 \ud835\udfda A \u2261 (A 0\u2082 \u228e A 1\u2082)\n \u03a3\ud835\udfda-\u228e = \u03a3-fst\u2243\u2032 \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 _ \u2219 dist-\u228e-\u03a3 \u2219 \u228e= \u03a3\ud835\udfd9-snd \u03a3\ud835\udfd9-snd\n\n \u03a0\ud835\udfda-\u00d7 : \u03a0 \ud835\udfda A \u2261 (A 0\u2082 \u00d7 A 1\u2082)\n \u03a0\ud835\udfda-\u00d7 = \u03a0-dom\u2243\u2032 \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 _ \u2219 dist-\u00d7-\u03a0 \u2219 \u00d7= (\u03a0\ud835\udfd9-uniq _) (\u03a0\ud835\udfd9-uniq _)\n\n \u03a0\ud835\udfdaF\u2261F\u2080\u00d7F\u2081 = \u03a0\ud835\udfda-\u00d7\n\nmodule _ {A : \u2605}{{_ : UA}}{{_ : FunExt}} where\n \u03a3\ud835\udfda-\u228e\u2032 : (\ud835\udfda \u00d7 A) \u2261 (A \u228e A)\n \u03a3\ud835\udfda-\u228e\u2032 = \u03a3\ud835\udfda-\u228e\n\n \u03a0\ud835\udfda\u2192\u00d7\u2032 : (\ud835\udfda \u2192 A) \u2261 (A \u00d7 A)\n \u03a0\ud835\udfda\u2192\u00d7\u2032 = \u03a0\ud835\udfda-\u00d7\n\nmodule _ {a}{A : \u2605_ a} where\n\n \u03a3\u2261x\u2243\ud835\udfd9 : \u2200 x \u2192 (\u03a3 A (_\u2261_ x)) \u2243 \ud835\udfd9\n \u03a3\u2261x\u2243\ud835\udfd9 x = equiv (\u03bb _ \u2192 _) (\u03bb _ \u2192 x , idp) (\u03bb _ \u2192 idp) (\u03bb p \u2192 pair= (snd p) (tr-r\u2261 (snd p) idp))\n\n \u03a3x\u2261\u2243\ud835\udfd9 : \u2200 x \u2192 (\u03a3 A (flip _\u2261_ x)) \u2243 \ud835\udfd9\n \u03a3x\u2261\u2243\ud835\udfd9 x = equiv (\u03bb _ \u2192 _) (\u03bb _ \u2192 x , idp) (\u03bb _ \u2192 idp) (\u03bb p \u2192 pair= (! snd p) ( tr-l\u2261 (! snd p) idp \u2219\n \u2219-refl (! (! (snd p))) \u2219 !-inv (snd p)))\n\nmodule _ where\n\n private\n maybe-elim : {X : Set}(m : Maybe X) \u2192 m \u2262 nothing \u2192 X\n maybe-elim {X} m = maybe {B = \u03bb m' \u2192 m' \u2262 _ \u2192 _} (\u03bb x _ \u2192 x) (\u03bb e \u2192 \ud835\udfd8-elim (e idp)) m\n\n maybe-elim-just : {X : Set}(m : Maybe X)(p : m \u2262 nothing)\n \u2192 just (maybe-elim m p) \u2261 m\n maybe-elim-just (just x) p = idp\n maybe-elim-just nothing p = \ud835\udfd8-elim (p idp)\n\n maybe-elim-cong : \u2200 {X : Set}{m m' : Maybe X}{p p'} \u2192 m \u2261 m'\n \u2192 maybe-elim m p \u2261 maybe-elim m' p'\n maybe-elim-cong {m = just x} idp = idp\n maybe-elim-cong {m = nothing} {p = p} idp = \ud835\udfd8-elim (p idp)\n\n not-just : {X : Set}{x : X} \u2192 _\u2262_ {A = Maybe X} (just x) nothing\n not-just ()\n\n !!-p : \u2200 {x}{X : Set x}{x y : X}(p : x \u2261 y) \u2192 ! (! p) \u2261 p\n !!-p idp = idp\n\n !\u2219 : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 ! p \u2219 p \u2261 idp\n !\u2219 idp = idp\n\n \u2219! : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 p \u2219 ! p \u2261 idp\n \u2219! idp = idp\n\n\n record PreservePoint {A B : Set}(eq : Maybe A \u2261 Maybe B) : Set where\n field\n coe-p : \u2200 x \u2192 coe eq (just x) \u2262 nothing\n coe!-p : \u2200 x \u2192 coe! eq (just x) \u2262 nothing\n open PreservePoint\n\n nothing-to-nothing : {A B : Set}(eq : Maybe A \u2261 Maybe B)\n \u2192 coe eq nothing \u2261 nothing \u2192 PreservePoint eq\n nothing-to-nothing eq n2n = record { coe-p = \u03bb x p \u2192 not-just (coe-inj eq (p \u2219 ! n2n))\n ; coe!-p = \u03bb x p \u2192 not-just (coe-same (! !\u2219 eq) (just x)\n \u2219 ! (coe\u2218coe eq (! eq) (just x)) \u2219 ap (coe eq) p \u2219 n2n) }\n\n !-PP : \u2200 {A B : Set} {e : Maybe A \u2261 Maybe B} \u2192 PreservePoint e \u2192 PreservePoint (! e)\n !-PP {e = e} e-pp = record { coe-p = coe!-p e-pp\n ; coe!-p = \u03bb x p \u2192 coe-p e-pp x ( ap (\u03bb X \u2192 coe X (just x)) (! !!-p e ) \u2219 p) }\n\n to : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e \u2192 A \u2192 B\n to e e-pp x = maybe-elim (coe e (just x)) (coe-p e-pp x)\n\n module _{A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}{ep ep'}(e'\u2219e=id : e' \u2219 e \u2261 idp)(x : B) where\n to-to : to e ep (to e' ep' x) \u2261 x\n to-to = just-injective (ap just (maybe-elim-cong\n {m = coe e (just (maybe-elim (coe e' (just x)) _))}\n {m' = coe e (coe e' (just x))}\n {p' = \u03bb q \u2192 not-just (! p \u2219 q)} (ap (coe e) (maybe-elim-just _ _)))\n \u2219 maybe-elim-just (coe e (coe e' (just x))) (\u03bb q \u2192 not-just (! p \u2219 q))\n \u2219 p)\n where\n p : coe e (coe e' (just x)) \u2261 just x\n p = coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n\n cto : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 Maybe A \u2192 Maybe B\n cto e = maybe (maybe\u2032 just (coe e nothing) \u2218 coe e \u2218 just) nothing\n\n module _ {A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}(e'\u2219e=id : e' \u2219 e \u2261 idp) where\n cto-cto : (x : Maybe B) \u2192 cto e (cto e' x) \u2261 x\n cto-cto nothing = idp\n cto-cto (just x) with coe e' (just x) | coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n cto-cto (just x) | just x\u2081 | p = ap (maybe just (coe e nothing)) p\n cto-cto (just x) | nothing | p with coe e' nothing\n | coe\u2218coe e e' nothing \u2219 coe-same e'\u2219e=id nothing\n cto-cto (just x) | nothing | p | just y | q = ap (maybe just (coe e nothing)) q \u2219 p\n cto-cto (just x) | nothing | p | nothing | q = ! q \u2219 p\n\n module _ {A B : Set} where\n create-PP-\u2243 : Maybe A \u2261 Maybe B \u2192 Maybe A \u2243 Maybe B\n create-PP-\u2243 e = equiv (cto e)\n (cto (! e))\n (cto-cto {e = e} {e' = ! e} (!\u2219 e))\n (cto-cto {e = ! e}{e' = e} (\u2219! e))\n\n create-PP : {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 \u03a3 (Maybe A \u2261 Maybe B) PreservePoint\n create-PP eq = ua (create-PP-\u2243 eq) , nothing-to-nothing _ (coe-\u03b2 (create-PP-\u2243 eq) nothing)\n\n Maybe-injective-PP : {{_ : UA}} \u2192 (e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e\n \u2192 A \u2261 B\n Maybe-injective-PP e e-pp = ua\n (equiv (to e e-pp)\n (to (! e) (!-PP e-pp))\n (to-to {e = e} { ! e} {e-pp}{ !-PP e-pp} (!\u2219 e))\n (to-to {e = ! e} {e} { !-PP e-pp} {e-pp} (\u2219! e)))\n\n Maybe-injective : \u2200 {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 A \u2261 B\n Maybe-injective e = let (e' , e'-pp) = create-PP e in Maybe-injective-PP e' e'-pp\n\n\nmodule _ {a}{A : \u2605_ a} where\n Maybe\u2243\ud835\udfd9\u228e : Maybe A \u2243 (\ud835\udfd9 \u228e A)\n Maybe\u2243\ud835\udfd9\u228e = equiv (maybe inr (inl _)) [inl: const nothing ,inr: just ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (maybe (\u03bb x \u2192 idp) idp)\n\n module _ {{_ : UA}} where\n\n Maybe\u2261\ud835\udfd9\u228e : Maybe A \u2261 (\ud835\udfd9 \u228e A)\n Maybe\u2261\ud835\udfd9\u228e = ua Maybe\u2243\ud835\udfd9\u228e\n\n Maybe\u2261Lift\ud835\udfd9\u228e : Maybe A \u2261 (Lift {\u2113 = a} \ud835\udfd9 \u228e A)\n Maybe\u2261Lift\ud835\udfd9\u228e = ua (equiv (maybe inr (inl _))\n [inl: const nothing ,inr: just ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n (maybe (\u03bb _ \u2192 idp) idp))\n\nFin0\u2243\ud835\udfd8 : Fin 0 \u2243 \ud835\udfd8\nFin0\u2243\ud835\udfd8 = equiv (\u03bb ()) (\u03bb ()) (\u03bb ()) (\u03bb ())\n\nFin1\u2243\ud835\udfd9 : Fin 1 \u2243 \ud835\udfd9\nFin1\u2243\ud835\udfd9 = equiv _ (\u03bb _ \u2192 zero) (\u03bb _ \u2192 idp) \u03b2\n where \u03b2 : (_ : Fin 1) \u2192 _\n \u03b2 zero = idp\n \u03b2 (suc ())\n\nmodule _ {{_ : UA}} where\n Fin0\u2261\ud835\udfd8 : Fin 0 \u2261 \ud835\udfd8\n Fin0\u2261\ud835\udfd8 = ua Fin0\u2243\ud835\udfd8\n\n Fin1\u2261\ud835\udfd9 : Fin 1 \u2261 \ud835\udfd9\n Fin1\u2261\ud835\udfd9 = ua Fin1\u2243\ud835\udfd9\n\nmodule _ where\n isZero? : \u2200 {n}{A : Fin (suc n) \u2192 Set} \u2192 ((i : Fin n) \u2192 A (suc i)) \u2192 A zero\n \u2192 (i : Fin (suc n)) \u2192 A i\n isZero? f x zero = x\n isZero? f x (suc i) = f i\n\n Fin\u2218suc\u2243\ud835\udfd9\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2243 (\ud835\udfd9 \u228e Fin n)\n Fin\u2218suc\u2243\ud835\udfd9\u228eFin = equiv (isZero? inr (inl _)) [inl: (\u03bb _ \u2192 zero) ,inr: suc ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n (isZero? (\u03bb _ \u2192 idp) idp)\n\n Fin\u2218suc\u2261\ud835\udfd9\u228eFin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 (\ud835\udfd9 \u228e Fin n)\n Fin\u2218suc\u2261\ud835\udfd9\u228eFin = ua Fin\u2218suc\u2243\ud835\udfd9\u228eFin\n\nFin-\u228e-+ : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u228e Fin n) \u2261 Fin (m \u2115.+ n)\nFin-\u228e-+ {zero} = \u228e= Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm \u2219 ! \u228e\ud835\udfd8-inl\nFin-\u228e-+ {suc m} = \u228e= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ap (_\u228e_ \ud835\udfd9) Fin-\u228e-+ \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\n\nFin-\u00d7-* : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u00d7 Fin n) \u2261 Fin (m \u2115.* n)\nFin-\u00d7-* {zero} = \u00d7= Fin0\u2261\ud835\udfd8 idp \u2219 \u03a3\ud835\udfd8-fst \u2219 ! Fin0\u2261\ud835\udfd8\nFin-\u00d7-* {suc m} = \u00d7= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u228e-\u00d7 \u2219 \u228e= \u03a3\ud835\udfd9-snd Fin-\u00d7-* \u2219 Fin-\u228e-+\n\nFin-\u2192-^ : \u2200 {{_ : UA}}{{_ : FunExt}}{m n} \u2192 (Fin m \u2192 Fin n) \u2261 Fin (n \u2115.^ m)\nFin-\u2192-^ {zero} = \u2192= Fin0\u2261\ud835\udfd8 idp \u2219 \u03a0\ud835\udfd8-uniq\u2032 _ \u2219 \u228e\ud835\udfd8-inl \u2219 ap (_\u228e_ \ud835\udfd9) (! Fin0\u2261\ud835\udfd8)\n \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\nFin-\u2192-^ {suc m} = \u2192= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u00d7-\u2192 \u2219 \u00d7= (\u03a0\ud835\udfd9-uniq _) Fin-\u2192-^ \u2219 Fin-\u00d7-*\n\nFin\u2218suc\u2261Maybe\u2218Fin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 Maybe (Fin n)\nFin\u2218suc\u2261Maybe\u2218Fin = Fin\u2218suc\u2261\ud835\udfd9\u228eFin \u2219 ! Maybe\u2261\ud835\udfd9\u228e\n\nFin-injective : \u2200 {{_ : UA}}{m n} \u2192 Fin m \u2261 Fin n \u2192 m \u2261 n\nFin-injective {zero} {zero} Finm\u2261Finn = idp\nFin-injective {zero} {suc n} Finm\u2261Finn = \ud835\udfd8-elim (coe (! Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {zero} Finm\u2261Finn = \ud835\udfd8-elim (coe (Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {suc n} Finm\u2261Finn\n = ap suc (Fin-injective (Maybe-injective\n (! Fin\u2218suc\u2261Maybe\u2218Fin \u2219 Finm\u2261Finn \u2219 Fin\u2218suc\u2261Maybe\u2218Fin)))\n\nFin\u228e-injective : \u2200 {{_ : UA}}{A B : Set} n \u2192 (Fin n \u228e A) \u2261 (Fin n \u228e B) \u2192 A \u2261 B\nFin\u228e-injective zero f = \u228e\ud835\udfd8-inl \u2219 \u228e-comm \u2219 \u228e= (! Fin0\u2261\ud835\udfd8) idp\n \u2219 f \u2219 (\u228e= Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm) \u2219 ! \u228e\ud835\udfd8-inl\nFin\u228e-injective (suc n) f = Fin\u228e-injective n (Maybe-injective\n (Maybe\u2261\ud835\udfd9\u228e \u2219 \u228e-assoc \u2219 \u228e= (! Fin\u2218suc\u2261\ud835\udfd9\u228eFin) idp \u2219 f\n \u2219 \u228e= Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ! Maybe\u2261\ud835\udfd9\u228e))\n\nLift\u2243id : \u2200 {a} {A : \u2605_ a} \u2192 Lift {a} {a} A \u2243 A\nLift\u2243id = equiv lower lift (\u03bb _ \u2192 idp) (\u03bb { (lift x) \u2192 idp })\n\nmodule _ {{_ : UA}} where\n Fin-\u2261-\u22611\u2082 : \u2200 b \u2192 Fin (\ud835\udfda\u25b9\u2115 b) \u2261 (b \u2261 1\u2082)\n Fin-\u2261-\u22611\u2082 1\u2082 = Fin1\u2261\ud835\udfd9 \u2219 ua (Is-contr-to-Is-equiv.\ud835\udfd9\u2243 (\u03a9\u2081-set-to-contr \ud835\udfda-is-set 1\u2082))\n Fin-\u2261-\u22611\u2082 0\u2082 = ua (equiv (\u03bb ()) (\u03bb ()) (\u03bb ()) (\u03bb ()))\n\n Fin-\u2261-\u22610\u2082 : \u2200 b \u2192 Fin (\ud835\udfda\u25b9\u2115 (not b)) \u2261 (b \u2261 0\u2082)\n Fin-\u2261-\u22610\u2082 b = Fin-\u2261-\u22611\u2082 (not b) \u2219 ! \u2013>-paths-equiv twist-equiv\n\n \u2713-\u2227-\u00d7 : \u2200 x y \u2192 \u2713 (x \u2227 y) \u2261 (\u2713 x \u00d7 \u2713 y)\n \u2713-\u2227-\u00d7 1\u2082 y = ! \ud835\udfd9\u00d7-snd\n \u2713-\u2227-\u00d7 0\u2082 y = ! \ud835\udfd8\u00d7-fst\n\n count-\u2261 : \u2200 {a} {A : \u2605_ a} (p : A \u2192 \ud835\udfda) x \u2192 Fin (\ud835\udfda\u25b9\u2115 (p x)) \u2261 (p x \u2261 1\u2082)\n count-\u2261 p x = Fin-\u2261-\u22611\u2082 (p x)\n\n\n Lift\u2261id : \u2200 {a} {A : \u2605_ a} \u2192 Lift {a} {a} A \u2261 A\n Lift\u2261id = ua Lift\u2243id\n\n \u03a0\ud835\udfd9F\u2261F : \u2200 {\u2113} {F : \ud835\udfd9 \u2192 \u2605_ \u2113} \u2192 \u03a0 \ud835\udfd9 F \u2261 F _\n \u03a0\ud835\udfd9F\u2261F = ua (equiv (\u03bb x \u2192 x _) const (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\n \ud835\udfd9\u2192A\u2261A : \u2200 {\u2113} {A : \u2605_ \u2113} \u2192 (\ud835\udfd9 \u2192 A) \u2261 A\n \ud835\udfd9\u2192A\u2261A = \u03a0\ud835\udfd9F\u2261F\n\n not-\ud835\udfda\u2261\ud835\udfda : \ud835\udfda \u2261 \ud835\udfda\n not-\ud835\udfda\u2261\ud835\udfda = twist\n\n Maybe\ud835\udfd8\u2261\ud835\udfd9 : Maybe \ud835\udfd8 \u2261 \ud835\udfd9\n Maybe\ud835\udfd8\u2261\ud835\udfd9 = Maybe\u2261\ud835\udfd9\u228e \u2219 ! \u228e\ud835\udfd8-inl\n\n Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe : \u2200 {a} {A : \u2605_ a} n \u2192 Maybe (Maybe^ n A) \u2261 Maybe^ n (Maybe A)\n Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe zero = idp\n Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe (suc n) = ap Maybe (Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe n)\n\n Maybe^\ud835\udfd8\u2261Fin : \u2200 n \u2192 Maybe^ n \ud835\udfd8 \u2261 Fin n\n Maybe^\ud835\udfd8\u2261Fin zero = ! Fin0\u2261\ud835\udfd8\n Maybe^\ud835\udfd8\u2261Fin (suc n) = ap Maybe (Maybe^\ud835\udfd8\u2261Fin n) \u2219 ! Fin\u2218suc\u2261Maybe\u2218Fin\n\n Maybe^\ud835\udfd9\u2261Fin1+ : \u2200 n \u2192 Maybe^ n \ud835\udfd9 \u2261 Fin (suc n)\n Maybe^\ud835\udfd9\u2261Fin1+ n = ap (Maybe^ n) (! Maybe\ud835\udfd8\u2261\ud835\udfd9) \u2219 ! Maybe\u2218Maybe^\u2261Maybe^\u2218Maybe n \u2219 Maybe^\ud835\udfd8\u2261Fin (suc n)\n\n Maybe-\u228e : \u2200 {a} {A B : \u2605_ a} \u2192 (Maybe A \u228e B) \u2261 Maybe (A \u228e B)\n Maybe-\u228e {a} = \u228e= Maybe\u2261Lift\ud835\udfd9\u228e idp \u2219 ! \u228e-assoc \u2219 ! Maybe\u2261Lift\ud835\udfd9\u228e\n\n Maybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \ud835\udfd8 \u228e Maybe^ n A) \u2261 Maybe^ (m + n) A\n Maybe^-\u228e-+ zero n = ! \ud835\udfd8\u228e-inr\n Maybe^-\u228e-+ (suc m) n = Maybe-\u228e \u2219 ap Maybe (Maybe^-\u228e-+ m n)\n\nmodule _ {A : Set} {p q : A \u2192 \ud835\udfda} where\n \u03a3AP\u00acQ : Set\n \u03a3AP\u00acQ = \u03a3 A (\u03bb x \u2192 p x \u2261 1\u2082 \u00d7 q x \u2261 0\u2082)\n\n \u03a3A\u00acPQ : Set\n \u03a3A\u00acPQ = \u03a3 A (\u03bb x \u2192 p x \u2261 0\u2082 \u00d7 q x \u2261 1\u2082)\n\n module EquivalentSubsets (e : \u03a3AP\u00acQ \u2261 \u03a3A\u00acPQ) where\n\n f' : \u03a3AP\u00acQ \u2192 \u03a3A\u00acPQ\n f' = coe e\n\n f-1' : \u03a3A\u00acPQ \u2192 \u03a3AP\u00acQ\n f-1' = coe! e\n\n f-1f' : \u2200 x \u2192 f-1' (f' x) \u2261 x\n f-1f' = coe!-inv-l e\n\n ff-1' : \u2200 x \u2192 f' (f-1' x) \u2261 x\n ff-1' = coe!-inv-r e\n\n f : (x : A) \u2192 p x \u2261 1\u2082 \u2192 q x \u2261 0\u2082 \u2192 \u03a3A\u00acPQ\n f x px qx = f' (x , (px , qx))\n\n f-1 : (x : A) \u2192 p x \u2261 0\u2082 \u2192 q x \u2261 1\u2082 \u2192 \u03a3AP\u00acQ\n f-1 x px qx = f-1' (x , (px , qx))\n\n f-1f : \u2200 x px nqx \u2192\n let y = snd (f x px nqx) in fst (f-1 (fst (f x px nqx)) (fst y) (snd y)) \u2261 x\n f-1f x px nqx = \u2261.cong fst (f-1f' (x , (px , nqx)))\n\n ff-1 : \u2200 x px nqx \u2192\n let y = snd (f-1 x px nqx) in fst (f (fst (f-1 x px nqx)) (fst y) (snd y)) \u2261 x\n ff-1 x px nqx = \u2261.cong fst (ff-1' (x , (px , nqx)))\n\n \u03c0' : (x : A) (px qx : \ud835\udfda) \u2192 p x \u2261 px \u2192 q x \u2261 qx \u2192 A\n \u03c0' x 1\u2082 1\u2082 px qx = x\n \u03c0' x 1\u2082 0\u2082 px qx = fst (f x px qx)\n \u03c0' x 0\u2082 1\u2082 px qx = fst (f-1 x px qx)\n \u03c0' x 0\u2082 0\u2082 px qx = x\n\n \u03c0 : A \u2192 A\n \u03c0 x = \u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n 0\u22621 : 0\u2082 \u2262 1\u2082\n 0\u22621 ()\n\n \u03c001 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px0 : p x \u2261 0\u2082) (qx1 : q x \u2261 1\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 0\u2082 1\u2082 px0 qx1\n \u03c001 x 1\u2082 _ ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym px0) ppx))\n \u03c001 x 0\u2082 1\u2082 ppx qqx px0 qx1 = \u2261.ap\u2082 (\u03bb z1 z2 \u2192 fst (f-1 x z1 z2)) (UIP-set \ud835\udfda-is-set ppx px0) (UIP-set \ud835\udfda-is-set qqx qx1)\n \u03c001 x 0\u2082 0\u2082 ppx qqx px0 qx1 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qqx) qx1))\n\n \u03c010 : \u2200 x px qx (ppx : p x \u2261 px) (qqx : q x \u2261 qx) (px1 : p x \u2261 1\u2082) (qx0 : q x \u2261 0\u2082) \u2192 \u03c0' x px qx ppx qqx \u2261 \u03c0' x 1\u2082 0\u2082 px1 qx0\n \u03c010 x 0\u2082 _ ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym ppx) px1))\n \u03c010 x 1\u2082 0\u2082 ppx qqx px1 qx0 = \u2261.ap\u2082 (\u03bb z1 z2 \u2192 fst (f x z1 z2)) (UIP-set \ud835\udfda-is-set ppx px1) (UIP-set \ud835\udfda-is-set qqx qx0)\n \u03c010 x 1\u2082 1\u2082 ppx qqx px1 qx0 = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym qx0) qqx))\n\n \u03c0'bb : \u2200 {b} x (px : p x \u2261 b) (qx : q x \u2261 b) ppx qqx ([ppx] : p x \u2261 ppx) ([qqx] : q x \u2261 qqx) \u2192 \u03c0' x ppx qqx [ppx] [qqx] \u2261 x\n \u03c0'bb x px qx 1\u2082 1\u2082 [ppx] [qqx] = \u2261.refl\n \u03c0'bb x px qx 1\u2082 0\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [qqx]) (\u2261.trans qx (\u2261.trans (\u2261.sym px) [ppx]))))\n \u03c0'bb x px qx 0\u2082 1\u2082 [ppx] [qqx] = \ud835\udfd8-elim (0\u22621 (\u2261.trans (\u2261.sym [ppx]) (\u2261.trans px (\u2261.trans (\u2261.sym qx) [qqx]))))\n \u03c0'bb x px qx 0\u2082 0\u2082 [ppx] [qqx] = \u2261.refl\n\n \u03c0\u03c0' : \u2200 x px qx [px] [qx] \u2192 let y = (\u03c0' x px qx [px] [qx]) in \u03c0' y (p y) (q y) \u2261.refl \u2261.refl \u2261 x\n \u03c0\u03c0' x 1\u2082 1\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n \u03c0\u03c0' x 1\u2082 0\u2082 px qx = let fx = f x px qx in let pfx = fst (snd fx) in let qfx = snd (snd fx) in \u2261.trans (\u03c001 (fst fx) (p (fst fx)) (q (fst fx)) \u2261.refl \u2261.refl pfx qfx) (f-1f x px qx)\n \u03c0\u03c0' x 0\u2082 1\u2082 px qx = let fx = f-1 x px qx in let pfx = fst (snd fx) in let qfx = snd (snd fx) in \u2261.trans (\u03c010 (fst fx) (p (fst fx)) (q (fst fx)) \u2261.refl \u2261.refl pfx qfx) (ff-1 x px qx)\n \u03c0\u03c0' x 0\u2082 0\u2082 px qx = \u03c0'bb x px qx (p x) (q x) \u2261.refl \u2261.refl\n\n \u03c0\u03c0 : \u2200 x \u2192 \u03c0 (\u03c0 x) \u2261 x\n \u03c0\u03c0 x = \u03c0\u03c0' x (p x) (q x) \u2261.refl \u2261.refl\n\n prop' : \u2200 px qx x ([px] : p x \u2261 px) ([qx] : q x \u2261 qx) \u2192 q (\u03c0' x px qx [px] [qx]) \u2261 px\n prop' 1\u2082 1\u2082 x px qx = qx\n prop' 1\u2082 0\u2082 x px qx = snd (snd (f x px qx))\n prop' 0\u2082 1\u2082 x px qx = snd (snd (f-1 x px qx))\n prop' 0\u2082 0\u2082 x px qx = qx\n\n prop'' : \u2200 x \u2192 p x \u2261 q (\u03c0 x)\n prop'' x = \u2261.sym (prop' (p x) (q x) x \u2261.refl \u2261.refl)\n\n prop : {{_ : FunExt}} \u2192 p \u2261 q \u2218 \u03c0\n prop = \u03bb= prop''\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"e725104ca30e21c58d1af1ec4d1e96783d4520c0","subject":"Removed unnecessary option.","message":"Removed unnecessary option.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/FOT\/FOTC\/Polymorphism\/List.agda","new_file":"notes\/FOT\/FOTC\/Polymorphism\/List.agda","new_contents":"------------------------------------------------------------------------------\n-- Testing polymorphic lists\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Polymorphism.List where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Data.Bool.Type\nopen import FOTC.Data.Nat.Type\n\n------------------------------------------------------------------------------\n-- Heterogeneous lists\ndata List : D \u2192 Set where\n lnil : List []\n lcons : \u2200 x {xs} \u2192 List xs \u2192 List (x \u2237 xs)\n\n-- Lists of total natural numbers\ndata ListN : D \u2192 Set where\n lnnil : ListN []\n lncons : \u2200 {n ns} \u2192 N n \u2192 ListN ns \u2192 ListN (n \u2237 ns)\n\n-- Lists of total Booleans\ndata ListB : D \u2192 Set where\n lbnil : ListB []\n lbcons : \u2200 {b bs} \u2192 Bool b \u2192 ListB bs \u2192 ListB (b \u2237 bs)\n\n-- Polymorphic lists.\n-- NB. The data type list is in *Set\u2081*.\ndata ListP (A : D \u2192 Set) : D \u2192 Set\u2081 where\n lnil : ListP A []\n lcons : \u2200 {x xs} \u2192 A x \u2192 ListP A xs \u2192 ListP A (x \u2237 xs)\n\nList\u2081 : D \u2192 Set\u2081\nList\u2081 = ListP (\u03bb d \u2192 d \u2261 d)\n\nListN\u2081 : D \u2192 Set\u2081\nListN\u2081 = ListP N\n\nListB\u2081 : D \u2192 Set\u2081\nListB\u2081 = ListP Bool\n","old_contents":"------------------------------------------------------------------------------\n-- Testing polymorphic lists\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --universal-quantified-propositional-functions #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Polymorphism.List where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Data.Bool.Type\nopen import FOTC.Data.Nat.Type\n\n------------------------------------------------------------------------------\n-- Heterogeneous lists\ndata List : D \u2192 Set where\n lnil : List []\n lcons : \u2200 x {xs} \u2192 List xs \u2192 List (x \u2237 xs)\n\n-- Lists of total natural numbers\ndata ListN : D \u2192 Set where\n lnnil : ListN []\n lncons : \u2200 {n ns} \u2192 N n \u2192 ListN ns \u2192 ListN (n \u2237 ns)\n\n-- Lists of total Booleans\ndata ListB : D \u2192 Set where\n lbnil : ListB []\n lbcons : \u2200 {b bs} \u2192 Bool b \u2192 ListB bs \u2192 ListB (b \u2237 bs)\n\n-- Polymorphic lists.\n-- NB. The data type list is in *Set\u2081*.\ndata ListP (A : D \u2192 Set) : D \u2192 Set\u2081 where\n lnil : ListP A []\n lcons : \u2200 {x xs} \u2192 A x \u2192 ListP A xs \u2192 ListP A (x \u2237 xs)\n\nList\u2081 : D \u2192 Set\u2081\nList\u2081 = ListP (\u03bb d \u2192 d \u2261 d)\n\nListN\u2081 : D \u2192 Set\u2081\nListN\u2081 = ListP N\n\nListB\u2081 : D \u2192 Set\u2081\nListB\u2081 = ListP Bool\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"920d6b2f3f692db778063b92415a07047d82dee3","subject":"Minor change to doc.","message":"Minor change to doc.\n\nIgnore-this: f157c6b20376d15e0a44e561b15fc97e\n\ndarcs-hash:20120308161712-3bd4e-db631d345a42a8d25e0c71de4d4549198275a09b.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/README.agda","new_file":"src\/FOTC\/README.agda","new_contents":"------------------------------------------------------------------------------\n-- First-Order Theory of Combinators (FOTC)\n------------------------------------------------------------------------------\n\nmodule FOTC.README where\n\n-- Formalization of (a version of) Azcel's First-Order Theory of\n-- Combinators.\n\n------------------------------------------------------------------------------\n-- The axioms\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n-- Booleans\n\n-- The axioms\nopen import FOTC.Data.Bool\n\n-- The inductive predicate\nopen import FOTC.Data.Bool.Type\n\n-- Properties\nopen import FOTC.Data.Bool.PropertiesATP\nopen import FOTC.Data.Bool.PropertiesI\n\n------------------------------------------------------------------------------\n-- Natural numbers\n\n-- The axioms\nopen import FOTC.Data.Nat\n\n-- The inductive predicate\nopen import FOTC.Data.Nat.Type\n\n-- Properties\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Data.Nat.PropertiesI\n\nopen import FOTC.Data.Nat.PropertiesByInductionATP\nopen import FOTC.Data.Nat.PropertiesByInductionI\n\n-- Divisibility relation\nopen import FOTC.Data.Nat.Divisibility.By0.Properties\nopen import FOTC.Data.Nat.Divisibility.By0.PropertiesATP\nopen import FOTC.Data.Nat.Divisibility.By0.PropertiesI\nopen import FOTC.Data.Nat.Divisibility.NotBy0.Properties\nopen import FOTC.Data.Nat.Divisibility.NotBy0.PropertiesATP\nopen import FOTC.Data.Nat.Divisibility.NotBy0.PropertiesI\n\n-- Induction\nopen import FOTC.Data.Nat.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.Nat.Induction.NonAcc.LexicographicI\n\n-- Inequalites\nopen import FOTC.Data.Nat.Inequalities.EliminationProperties\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.Inequalities.PropertiesI\n\n-- Unary numbers\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers.TotalityATP\n\n------------------------------------------------------------------------------\n-- Lists\n\n-- The axioms\nopen import FOTC.Data.List\n\n-- The inductive predicate\nopen import FOTC.Data.List.Type\n\n-- Properties\nopen import FOTC.Data.List.PropertiesATP\nopen import FOTC.Data.List.PropertiesI\n\n-- Well-founded induction\nopen import FOTC.Data.List.LT-Cons.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.List.LT-Cons.PropertiesI\nopen import FOTC.Data.List.LT-Length.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.List.LT-Length.PropertiesI\n\n------------------------------------------------------------------------------\n-- Lists of natural numbers\n\n-- The inductive predicate\nopen import FOTC.Data.Nat.List.Type\n\n-- Properties\nopen import FOTC.Data.Nat.List.PropertiesATP\nopen import FOTC.Data.Nat.List.PropertiesI\n\n------------------------------------------------------------------------------\n-- Coinductive natural numbers\n\n-- The coinductive predicate\nopen import FOTC.Data.Conat\n\n-- Equality on coinductive natural numbers\nopen import FOTC.Data.Conat.Equality\n\n-- Properties\nopen import FOTC.Data.Conat.PropertiesI\n\n------------------------------------------------------------------------------\n-- Streams\n\n-- The coinductive predicate (axioms)\nopen import FOTC.Data.Stream\n\n-- Properties\nopen import FOTC.Data.Stream.PropertiesATP\nopen import FOTC.Data.Stream.PropertiesI\n\n------------------------------------------------------------------------------\n-- Bisimilary relation\n\n-- The axioms\nopen import FOTC.Relation.Binary.Bisimilarity\n\n-- Properties\nopen import FOTC.Relation.Binary.Bisimilarity.PropertiesATP\nopen import FOTC.Relation.Binary.Bisimilarity.PropertiesI\n\n------------------------------------------------------------------------------\n-- Verification of programs\n\n-- The Collatz function: A function without a termination proof\nopen import FOTC.Program.Collatz.PropertiesATP\nopen import FOTC.Program.Collatz.PropertiesI\n\n-- The GCD algorithm: A non-structurally recursive algorithm\nopen import FOTC.Program.GCD.Partial.ProofSpecificationATP\nopen import FOTC.Program.GCD.Partial.ProofSpecificationI\nopen import FOTC.Program.GCD.Total.ProofSpecificationATP\nopen import FOTC.Program.GCD.Total.ProofSpecificationI\n\n-- The McCarthy 91 function: A function with nested recursion\nopen import FOTC.Program.McCarthy91.Properties.MainATP\n\n-- The mirror function: A function with higher-order recursion\nopen import FOTC.Program.Mirror.PropertiesATP\nopen import FOTC.Program.Mirror.PropertiesI\n\n-- Burstall's sort list algorithm: A structurally recursive algorithm\nopen import FOTC.Program.SortList.ProofSpecificationATP\nopen import FOTC.Program.SortList.ProofSpecificationI\n\n-- The division algorithm: A non-structurally recursive algorithm\nopen import FOTC.Program.Division.ProofSpecificationATP\nopen import FOTC.Program.Division.ProofSpecificationI\n\n-- The map-iterate property: A property using co-induction\nopen import FOTC.Program.MapIterate.MapIterateATP\nopen import FOTC.Program.MapIterate.MapIterateI\n\n-- The alternating bit protocol: A program using induction and co-induction\nopen import FOTC.Program.ABP.ProofSpecificationATP\nopen import FOTC.Program.ABP.ProofSpecificationI\n","old_contents":"------------------------------------------------------------------------------\n-- First-Order Theory of Combinators (FOTC)\n------------------------------------------------------------------------------\n\nmodule FOTC.README where\n\n-- Formalization of (a version of) Azcel's First-Order Theory of\n-- Combinators.\n\n------------------------------------------------------------------------------\n-- The axioms\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n-- Booleans\n\n-- The axioms\nopen import FOTC.Data.Bool\n\n-- The inductive predicate\nopen import FOTC.Data.Bool.Type\n\n-- Properties\nopen import FOTC.Data.Bool.PropertiesATP\nopen import FOTC.Data.Bool.PropertiesI\n\n------------------------------------------------------------------------------\n-- Natural numbers\n\n-- The axioms\nopen import FOTC.Data.Nat\n\n-- The inductive predicate\nopen import FOTC.Data.Nat.Type\n\n-- Properties\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Data.Nat.PropertiesI\n\nopen import FOTC.Data.Nat.PropertiesByInductionATP\nopen import FOTC.Data.Nat.PropertiesByInductionI\n\n-- Divisibility relation\nopen import FOTC.Data.Nat.Divisibility.By0.Properties\nopen import FOTC.Data.Nat.Divisibility.By0.PropertiesATP\nopen import FOTC.Data.Nat.Divisibility.By0.PropertiesI\nopen import FOTC.Data.Nat.Divisibility.NotBy0.Properties\nopen import FOTC.Data.Nat.Divisibility.NotBy0.PropertiesATP\nopen import FOTC.Data.Nat.Divisibility.NotBy0.PropertiesI\n\n-- Induction\nopen import FOTC.Data.Nat.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.Nat.Induction.NonAcc.LexicographicI\n\n-- Inequalites\nopen import FOTC.Data.Nat.Inequalities.EliminationProperties\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.Inequalities.PropertiesI\n\n-- Unary numbers\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers.TotalityATP\n\n------------------------------------------------------------------------------\n-- Lists\n\n-- The axioms\nopen import FOTC.Data.List\n\n-- The inductive predicate\nopen import FOTC.Data.List.Type\n\n-- Properties\nopen import FOTC.Data.List.PropertiesATP\nopen import FOTC.Data.List.PropertiesI\n\n-- Well-founded induction\nopen import FOTC.Data.List.LT-Cons.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.List.LT-Cons.PropertiesI\nopen import FOTC.Data.List.LT-Length.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.List.LT-Length.PropertiesI\n\n------------------------------------------------------------------------------\n-- Lists of natural numbers\n\n-- The inductive predicate\nopen import FOTC.Data.Nat.List.Type\n\n-- Properties\nopen import FOTC.Data.Nat.List.PropertiesATP\nopen import FOTC.Data.Nat.List.PropertiesI\n\n------------------------------------------------------------------------------\n-- Coinductive natural numbers\n\n-- The coinductive predicate\nopen import FOTC.Data.Conat\n\n-- Equality on coinductive natural numbers\nopen import FOTC.Data.Conat.Equality\n\n-- Properties\nopen import FOTC.Data.Conat.PropertiesI\n\n------------------------------------------------------------------------------\n-- Streams\n\n-- The coinductive predicate (axioms)\nopen import FOTC.Data.Stream\n\n-- Properties\nopen import FOTC.Data.Stream.PropertiesATP\nopen import FOTC.Data.Stream.PropertiesI\n\n------------------------------------------------------------------------------\n-- Bisimilary relation\n\n-- The axioms\nopen import FOTC.Relation.Binary.Bisimilarity\n\n-- Properties\nopen import FOTC.Relation.Binary.Bisimilarity.PropertiesATP\nopen import FOTC.Relation.Binary.Bisimilarity.PropertiesI\n\n------------------------------------------------------------------------------\n-- Verification of programs\n\n-- The Collatz function: A function without a termination proof\nopen import FOTC.Program.Collatz.PropertiesATP\nopen import FOTC.Program.Collatz.PropertiesI\n\n-- The GCD algorithm: A non-structurally recursive algorithm\nopen import FOTC.Program.GCD.Partial.ProofSpecificationATP\nopen import FOTC.Program.GCD.Partial.ProofSpecificationI\nopen import FOTC.Program.GCD.Total.ProofSpecificationATP\nopen import FOTC.Program.GCD.Total.ProofSpecificationI\n\n-- The McCarthy 91 function: A function with nested recursion\nopen import FOTC.Program.McCarthy91.Properties.MainATP\n\n-- The mirror function: A function with higher-order recursion\nopen import FOTC.Program.Mirror.PropertiesATP\nopen import FOTC.Program.Mirror.PropertiesI\n\n-- Burstall's sort list algorithm: A structurally recursive algorithm\nopen import FOTC.Program.SortList.ProofSpecificationATP\nopen import FOTC.Program.SortList.ProofSpecificationI\n\n-- The division algorithm: A non-structurally recursive algorithm\nopen import FOTC.Program.Division.ProofSpecificationATP\nopen import FOTC.Program.Division.ProofSpecificationI\n\n-- The map-iterate property: A property using coinduction\nopen import FOTC.Program.MapIterate.MapIterateATP\nopen import FOTC.Program.MapIterate.MapIterateI\n\n-- The alternating bit protocol: A program using co-inductive types\nopen import FOTC.Program.ABP.ProofSpecificationATP\nopen import FOTC.Program.ABP.ProofSpecificationI\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"636d5a56d24486b6265526052a187e78c4538067","subject":"proof of preservation with out induction -- something is rotten in the kingdom of denmark","message":"proof of preservation with out induction -- something is rotten in the kingdom of denmark\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"preservation.agda","new_file":"preservation.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nmodule preservation where\n preservation : {\u0394 : hctx} {d d' : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n \u0394 \u22a2 d \u21a6 d' \u2192\n \u0394 , \u2205 \u22a2 d' :: \u03c4\n preservation TAConst (Step _ _ _) = TAConst\n preservation (TAVar x\u2081) step = abort (somenotnone (! x\u2081))\n preservation (TALam wt) (Step x\u2082 (ITLam x\u2083) x\u2084) = TALam wt\n preservation (TALam wt) (Step x\u2081 (ITCast x\u2082 x\u2083 x\u2084) x\u2085) = TALam wt\n preservation (TAAp wt x\u2081 wt\u2081) (Step x\u2082 (ITLam x\u2083) x\u2084) = TAAp wt x\u2081 wt\u2081\n preservation (TAAp wt x wt\u2081) (Step x\u2081 (ITCast x\u2082 x\u2083 x\u2084) x\u2085) = TAAp wt x wt\u2081\n preservation (TAEHole x x\u2081) (Step x\u2082 x\u2083 x\u2084) = TAEHole x x\u2081\n preservation (TANEHole x\u2081 wt x\u2082) (Step x\u2083 (ITLam x\u2084) x\u2085) = TANEHole x\u2081 wt x\u2082\n preservation (TANEHole x wt x\u2081) (Step x\u2082 (ITCast x\u2083 x\u2084 x\u2085) x\u2086) = TANEHole x wt x\u2081\n preservation (TACast wt x\u2081) (Step x\u2082 (ITLam x\u2083) x\u2084) = TACast wt x\u2081\n preservation (TACast wt x) (Step x\u2081 (ITCast x\u2082 x\u2083 x\u2084) x\u2085) = TACast wt x\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nmodule preservation where\n preservation : {\u0394 : hctx} {d d' : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n \u0394 \u22a2 d \u21a6 d' \u2192\n \u0394 , \u2205 \u22a2 d' :: \u03c4\n preservation TAConst (Step _ _ _) = TAConst\n preservation (TAVar x\u2081) step = abort (somenotnone (! x\u2081))\n preservation (TALam wt) (Step x\u2081 x\u2082 x\u2083) = {!!}\n preservation (TAAp wt x wt\u2081) step = {!!}\n preservation (TAEHole x x\u2081) (Step x\u2082 x\u2083 x\u2084) = {!!}\n preservation (TANEHole x wt x\u2081) step = {!!}\n preservation (TACast wt x) step = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"1fdf8b41d9320f80c520ee0682a8fdfd595b10c2","subject":"Still unfinished, Chaum Pedersen.","message":"Still unfinished, Chaum Pedersen.\n","repos":"crypto-agda\/crypto-agda","old_file":"ZK\/ChaumPedersen.agda","new_file":"ZK\/ChaumPedersen.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Type\nopen import Data.Zero\nopen import Data.Two.Base\nopen import Data.ShapePolymorphism\nopen import Relation.Binary.PropositionalEquality.NP\nimport ZK.SigmaProtocol as \u03a3Proto\nopen import ZK.Types\nopen import ZK.Statement\n\nmodule ZK.ChaumPedersen\n {G \u2124q : \u2605}\n -- (cyclic-group : Cyclic-group G \u2124q)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_\/_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_==_ : (x y : G) \u2192 \ud835\udfda)\n where\n\n -- TODO: Re-use another module\n module ElGamal-encryption where\n record CipherText : \u2605 where\n constructor _,_\n field\n get-\u03b1 get-\u03b2 : G\n\n PubKey = G\n EncRnd = \u2124q {- randomness used for encryption of ct -}\n Message = G {- plain text message -}\n\n enc : PubKey \u2192 EncRnd \u2192 Message \u2192 CipherText\n enc y r M = \u03b1 , \u03b2 where\n \u03b1 = g ^ r\n \u03b2 = (y ^ r) \u00b7 M\n open ElGamal-encryption\n\n module Tmp where\n module _ (y : PubKey) (M : Message) (ct : CipherText) where\n Statement : Set\n Statement =\n -- Reads as follows:\n -- A value `r` (of type `EncRnd`) is known to be\n -- the encryption randomness used to produce the\n -- cipher-text `c` of message `M` using public-key `y`.\n ZKStatement EncRnd \u03bb { [ r ] \u2192 ct \u2261\u2610 enc y r M }\n\n -- Assume the randomness `r` is known\n module _ (y : PubKey) (M : Message) (r : EncRnd) where\n -- Then the Statement holds\n Statement-complete : Statement y M (enc y r M)\n Statement-complete = [ r ] , refl\n\n record Commitment : \u2605 where\n constructor _,_\n field\n get-A get-B : G\n\n Challenge = \u2124q\n Response = \u2124q\n Randomness = \u2124q\n Witness = EncRnd\n\n record Statement : Type where\n constructor mk\n field\n y : PubKey\n M : Message\n ct : CipherText\n open CipherText ct public renaming (get-\u03b1 to \u03b1; get-\u03b2 to \u03b2)\n\n _\u2208_ : Witness \u2192 Statement \u2192 Type\n r \u2208 (mk y M ct) = {!!}\n\n open \u03a3Proto Commitment Challenge Response Randomness Witness Statement _\u2208_ public\n\n{-\n module _ (y : PubKey) (r : EncRnd) (w : \u2124q) where\n prover : (w : \u2124q) \u2192 Prover\n prover r w = prover-commitment , prover-response\n where\n prover-commitment : Commitment\n prover-commitment = (g ^ w) , (y ^ w)\n\n prover-response : Challenge \u2192 Response\n prover-response c = w + (r * c)\n\n\n module _ (y : PubKey) (M : Message) (ct : CipherText) where\n private\n \u03b1 = CipherText.get-\u03b1 ct\n \u03b2 = CipherText.get-\u03b2 ct\n \u03b2\/M = \u03b2 \/ M\n\n verifier : Verifier\n verifier (mk (A , B) c s)\n = (g ^ s) == (A \u00b7 (\u03b1 ^ c))\n \u2227 (y ^ s) == (B \u00b7 (\u03b2\/M ^ c))\n\n -- This simulator shows why it is so important for the\n -- challenge to be picked once the commitment is known.\n\n -- To fake a transcript, the challenge and response can\n -- be arbitrarily chosen. However to be indistinguishable\n -- from a valid proof it they need to be picked at random.\n module _ (c : Challenge) (s : Response) where\n -- Compute A and B, such that the verifier accepts!\n private\n A = (g ^ s) \/ (\u03b1 ^ c)\n B = (y ^ s) \/ (\u03b2\/M ^ c)\n\n simulate-commitment : Commitment\n simulate-commitment = (A , B)\n\n simulate : Transcript\n simulate = mk simulate-commitment c s\n\n module Correct-simulation\n (\u2713-== : \u2200 {x y} \u2192 x \u2261 y \u2192 \u2713 (x == y))\n (\/-\u00b7 : \u2200 {P Q} \u2192 P \u2261 (P \/ Q) \u00b7 Q)\n where\n correct-simulation : \u2713(verifier simulate)\n correct-simulation = \u2713\u2227 (\u2713-== \/-\u00b7) (\u2713-== \/-\u00b7)\n\n module Correctness-proof\n (\u2713-== : \u2200 {x y} \u2192 x \u2261 y \u2192 \u2713 (x == y))\n (^-+ : \u2200 {b x y} \u2192 b ^(x + y) \u2261 (b ^ x) \u00b7 (b ^ y))\n (^-* : \u2200 {b x y} \u2192 b ^(x * y) \u2261 (b ^ x) ^ y)\n (\u00b7-\/ : \u2200 {P Q} \u2192 P \u2261 (P \u00b7 Q) \/ Q)\n (y : PubKey) (r : EncRnd) (w : \u2124q) (M : Message) where\n open \u2261-Reasoning\n\n correctness : Correct (prover y r w , verifier y M (enc y r M))\n correctness c = \u2713\u2227 (\u2713-== pf1) (\u2713-== pf2)\n where\n g\u02b7 = g ^ w\n pf1 = g ^(w + (r * c))\n \u2261\u27e8 ^-+ \u27e9\n g\u02b7 \u00b7 (g ^(r * c))\n \u2261\u27e8 ap (\u03bb z \u2192 g\u02b7 \u00b7 z) ^-* \u27e9\n g\u02b7 \u00b7 ((g ^ r) ^ c)\n \u220e\n pf3 = y ^ (r * c)\n \u2261\u27e8 ^-* \u27e9\n (y ^ r)^ c\n \u2261\u27e8 ap (\u03bb b \u2192 b ^ c) \u00b7-\/ \u27e9\n (((y ^ r) \u00b7 M) \/ M) ^ c\n \u220e\n pf2 = y ^(w + (r * c))\n \u2261\u27e8 ^-+ \u27e9\n (y ^ w) \u00b7 (y ^(r * c))\n \u2261\u27e8 ap (\u03bb z \u2192 (y ^ w) \u00b7 z) pf3 \u27e9\n (y ^ w) \u00b7 ((((y ^ r) \u00b7 M) \/ M) ^ c)\n \u220e\n\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Type\nopen import Data.Zero\nopen import Data.Two.Base\nopen import Data.ShapePolymorphism\nopen import Relation.Binary.PropositionalEquality.NP\nimport ZK.SigmaProtocol as \u03a3Proto\nopen import ZK.Types\nopen import ZK.Statement\n\nmodule ZK.ChaumPedersen\n {G \u2124q : \u2605}\n -- (cyclic-group : Cyclic-group G \u2124q)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_\/_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_==_ : (x y : G) \u2192 \ud835\udfda)\n where\n\n -- TODO: Re-use another module\n module ElGamal-encryption where\n record CipherText : \u2605 where\n constructor _,_\n field\n get-\u03b1 get-\u03b2 : G\n\n PubKey = G\n EncRnd = \u2124q {- randomness used for encryption of ct -}\n Message = G {- plain text message -}\n\n enc : PubKey \u2192 EncRnd \u2192 Message \u2192 CipherText\n enc y r M = \u03b1 , \u03b2 where\n \u03b1 = g ^ r\n \u03b2 = (y ^ r) \u00b7 M\n open ElGamal-encryption\n\n module _ (y : PubKey) (M : Message) (ct : CipherText) where\n Statement : Set\n Statement =\n -- Reads as follows:\n -- A value `r` (of type `EncRnd`) is known to be\n -- the encryption randomness used to produce the\n -- cipher-text `c` of message `M` using public-key `y`.\n ZKStatement EncRnd \u03bb { [ r ] \u2192 ct \u2261\u2610 enc y r M }\n\n -- Assume the randomness `r` is known\n module _ (y : PubKey) (M : Message) (r : EncRnd) where\n -- Then the Statement holds\n Statement-complete : Statement y M (enc y r M)\n Statement-complete = [ r ] , refl\n\n record Commitment : \u2605 where\n constructor _,_\n field\n get-A get-B : G\n\n Challenge = \u2124q\n Response = \u2124q\n\n open \u03a3Proto Commitment Challenge Response public\n\n module _ (y : PubKey) (r : EncRnd) (w : \u2124q) where\n prover-commitment : Commitment\n prover-commitment = (g ^ w) , (y ^ w)\n\n prover-response : Challenge \u2192 Response\n prover-response c = w + (r * c)\n\n prover : Prover\n prover = prover-commitment , prover-response\n\n module _ (y : PubKey) (M : Message) (ct : CipherText) where\n private\n \u03b1 = CipherText.get-\u03b1 ct\n \u03b2 = CipherText.get-\u03b2 ct\n \u03b2\/M = \u03b2 \/ M\n\n verifier : Verifier\n verifier (mk (A , B) c s)\n = (g ^ s) == (A \u00b7 (\u03b1 ^ c))\n \u2227 (y ^ s) == (B \u00b7 (\u03b2\/M ^ c))\n\n -- This simulator shows why it is so important for the\n -- challenge to be picked once the commitment is known.\n\n -- To fake a transcript, the challenge and response can\n -- be arbitrarily chosen. However to be indistinguishable\n -- from a valid proof it they need to be picked at random.\n module _ (c : Challenge) (s : Response) where\n -- Compute A and B, such that the verifier accepts!\n private\n A = (g ^ s) \/ (\u03b1 ^ c)\n B = (y ^ s) \/ (\u03b2\/M ^ c)\n\n simulate-commitment : Commitment\n simulate-commitment = (A , B)\n\n simulate : Transcript\n simulate = mk simulate-commitment c s\n\n module Correct-simulation\n (\u2713-== : \u2200 {x y} \u2192 x \u2261 y \u2192 \u2713 (x == y))\n (\/-\u00b7 : \u2200 {P Q} \u2192 P \u2261 (P \/ Q) \u00b7 Q)\n where\n correct-simulation : \u2713(verifier simulate)\n correct-simulation = \u2713\u2227 (\u2713-== \/-\u00b7) (\u2713-== \/-\u00b7)\n\n module Correctness-proof\n (\u2713-== : \u2200 {x y} \u2192 x \u2261 y \u2192 \u2713 (x == y))\n (^-+ : \u2200 {b x y} \u2192 b ^(x + y) \u2261 (b ^ x) \u00b7 (b ^ y))\n (^-* : \u2200 {b x y} \u2192 b ^(x * y) \u2261 (b ^ x) ^ y)\n (\u00b7-\/ : \u2200 {P Q} \u2192 P \u2261 (P \u00b7 Q) \/ Q)\n (y : PubKey) (r : EncRnd) (w : \u2124q) (M : Message) where\n open \u2261-Reasoning\n\n correctness : Correct (prover y r w , verifier y M (enc y r M))\n correctness c = \u2713\u2227 (\u2713-== pf1) (\u2713-== pf2)\n where\n g\u02b7 = g ^ w\n pf1 = g ^(w + (r * c))\n \u2261\u27e8 ^-+ \u27e9\n g\u02b7 \u00b7 (g ^(r * c))\n \u2261\u27e8 ap (\u03bb z \u2192 g\u02b7 \u00b7 z) ^-* \u27e9\n g\u02b7 \u00b7 ((g ^ r) ^ c)\n \u220e\n pf3 = y ^ (r * c)\n \u2261\u27e8 ^-* \u27e9\n (y ^ r)^ c\n \u2261\u27e8 ap (\u03bb b \u2192 b ^ c) \u00b7-\/ \u27e9\n (((y ^ r) \u00b7 M) \/ M) ^ c\n \u220e\n pf2 = y ^(w + (r * c))\n \u2261\u27e8 ^-+ \u27e9\n (y ^ w) \u00b7 (y ^(r * c))\n \u2261\u27e8 ap (\u03bb z \u2192 (y ^ w) \u00b7 z) pf3 \u27e9\n (y ^ w) \u00b7 ((((y ^ r) \u00b7 M) \/ M) ^ c)\n \u220e\n\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"d88bde24da1e5c0823a6bebbe460684813833212","subject":"Desc model: cases implicit.","message":"Desc model: cases implicit.\n","repos":"mietek\/epigram2,mietek\/epigram2,larrytheliquid\/pigit","old_file":"models\/IDesc_type_type.agda","new_file":"models\/IDesc_type_type.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\niso1 : (I : Set) -> IDescl I -> IDesc I\niso1 I d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\niso2 : (I : Set) -> IDesc I -> IDescl I\niso2 I (var i) = varl i\niso2 I (const X) = constl X\niso2 I (prod D D') = prodl (iso2 I D) (iso2 I D')\niso2 I (pi S T) = pil S (\\s -> iso2 I (T s))\niso2 I (sigma S T) = sigmal S (\\s -> iso2 I (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-iso1-iso2 : (I : Set) -> (D : IDesc I) -> iso1 I (iso2 I D) == D\nproof-iso1-iso2 I (var i) = refl\nproof-iso1-iso2 I (const x) = refl\nproof-iso1-iso2 I (prod D D') with proof-iso1-iso2 I D | proof-iso1-iso2 I D'\n... | p | q = cong2 prod p q\nproof-iso1-iso2 I (pi S T) = cong (pi S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\nproof-iso1-iso2 I (sigma S T) = cong (sigma S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\n\n\n-- From embedding to embedding\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = iso2 I (iso1 I D) == D\n\nproof-iso2-iso1 : (I : Set) -> (D : IDescl I) -> iso2 I (iso1 I D) == D\nproof-iso2-iso1 I D = induction (\u03bb _ \u2192 descD I)\n (P I)\n (proof-iso2-iso1-casesW I) \n Void\n D\n where proof-iso2-iso1-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-iso2-iso1-cases I (lvar , i) hs = refl\n proof-iso2-iso1-cases I (lconst , x) hs = refl\n proof-iso2-iso1-cases I (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-iso2-iso1-cases I (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-cases I (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-iso2-iso1-casesW I Void = proof-iso2-iso1-cases I\n \n\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl : (I : Set) -> Set\nIDescl I = IMu (\\_ -> descD I) Void\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : (I : Set) \n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases I ( lvar , i ) hs = var i\ncases I ( lconst , X ) hs = const X\ncases I ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases I ( lpi , ( S , T ) ) hs = pi S hs\ncases I ( lsigma , ( S , T ) ) hs = sigma S hs\n\niso1 : (I : Set) -> IDescl I -> IDesc I\niso1 I d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases I) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\niso2 : (I : Set) -> IDesc I -> IDescl I\niso2 I (var i) = varl i\niso2 I (const X) = constl X\niso2 I (prod D D') = prodl (iso2 I D) (iso2 I D')\niso2 I (pi S T) = pil S (\\s -> iso2 I (T s))\niso2 I (sigma S T) = sigmal S (\\s -> iso2 I (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-iso1-iso2 : (I : Set) -> (D : IDesc I) -> iso1 I (iso2 I D) == D\nproof-iso1-iso2 I (var i) = refl\nproof-iso1-iso2 I (const x) = refl\nproof-iso1-iso2 I (prod D D') with proof-iso1-iso2 I D | proof-iso1-iso2 I D'\n... | p | q = cong2 prod p q\nproof-iso1-iso2 I (pi S T) = cong (pi S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\nproof-iso1-iso2 I (sigma S T) = cong (sigma S) \n (reflFun (\u03bb s \u2192 iso1 I (iso2 I (T s)))\n T\n (\\s -> proof-iso1-iso2 I (T s)))\n\n\n-- From embedding to embedding\n\nP : (I : Set) -> Sigma Unit (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) \u2192 Set\nP I ( Void , D ) = iso2 I (iso1 I D) == D\n\nproof-iso2-iso1 : (I : Set) -> (D : IDescl I) -> iso2 I (iso1 I D) == D\nproof-iso2-iso1 I D = induction (\u03bb _ \u2192 descD I)\n (P I)\n (proof-iso2-iso1-casesW I) \n Void\n D\n where proof-iso2-iso1-cases : (I : Set)\n (xs : Sigma DescDConst \n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (Void , con xs)\n proof-iso2-iso1-cases I (lvar , i) hs = refl\n proof-iso2-iso1-cases I (lconst , x) hs = refl\n proof-iso2-iso1-cases I (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-iso2-iso1-cases I (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-cases I (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s \u2192 iso2 I (iso1 I (T s)))\n T\n hs)\n proof-iso2-iso1-casesW : (I : Set)\n (i : Unit)\n (xs : Sigma DescDConst\n (\u03bb s \u2192 desc (descDChoice I s)\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I)))))\n (hs : desc \n (box (sigma DescDConst (descDChoice I))\n (IMu (\u03bb x \u2192 sigma DescDConst (descDChoice I))) xs)\n (P I))\n \u2192 P I (i , con xs)\n proof-iso2-iso1-casesW I Void = proof-iso2-iso1-cases I\n \n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"78d5c087d7e245e7995a82da881435a72bfdf850","subject":"HoTT: \u03a0-is-prop and others","message":"HoTT: \u03a0-is-prop and others\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/HoTT.agda","new_file":"lib\/HoTT.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule HoTT where\n\nopen import Type\nopen import Level.NP\nopen import Function.NP\nopen import Function.Extensionality\nopen import Data.Zero using (\ud835\udfd8; \ud835\udfd8-elim)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Product.NP\nopen import Data.Sum using (_\u228e_) renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\nopen import Relation.Nullary.NP\nopen import Relation.Binary using (Reflexive; Symmetric; Transitive)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; ap; coe; coe!; !_; _\u2219_; J; ap\u2193; PathOver; tr; ap\u2082)\n renaming (refl to idp; _\u2257_ to _\u223c_; J-orig to J')\nopen \u2261.\u2261-Reasoning\n\nimport Function.Inverse.NP as Inv\nopen Inv using (_\u2194_; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\n\nmodule _ {a} {A : \u2605_ a} where\n idp_ : (x : A) \u2192 x \u2261 x\n idp_ _ = idp\n\n refl-\u2219 : \u2200 {x y : A} (p : x \u2261 y) \u2192 idp_ x \u2219 p \u2261 p\n refl-\u2219 _ = idp\n\n \u2219-refl : \u2200 {x y : A} (p : x \u2261 y) \u2192 p \u2219 idp_ y \u2261 p\n \u2219-refl = J' (\u03bb (x y : A) (p : x \u2261 y) \u2192 (p \u2219 idp_ y) \u2261 p) (\u03bb x \u2192 idp)\n\n -- could be derived in any groupoid\n hom-!-\u2219 : \u2200 {x y z : A} (p : x \u2261 y)(q : y \u2261 z) \u2192 !(p \u2219 q) \u2261 ! q \u2219 ! p\n hom-!-\u2219 p q = J' (\u03bb x y p \u2192 \u2200 z \u2192 (q : y \u2261 z) \u2192 !(p \u2219 q) \u2261 ! q \u2219 ! p) (\u03bb x z q \u2192 ! \u2219-refl (! q)) p _ q\n\n module _ {x y : A} where\n\n \u2219-assoc : (p : x \u2261 y) {z : A} (q : y \u2261 z) {t : A} (r : z \u2261 t) \u2192 p \u2219 q \u2219 r \u2261 (p \u2219 q) \u2219 r\n \u2219-assoc = J' (\u03bb x y p \u2192 \u2200 {z} (q : y \u2261 z) {t} (r : z \u2261 t) \u2192 p \u2219 (q \u2219 r) \u2261 (p \u2219 q) \u2219 r)\n (\u03bb x q r \u2192 idp)\n\n module _ (p : x \u2261 y) where\n -- ! is a left-inverse for _\u2219_\n !-\u2219 : ! p \u2219 p \u2261 idp_ y\n !-\u2219 = J' (\u03bb x y p \u2192 (! p \u2219 p) \u2261 idp_ y) (\u03bb x \u2192 idp) p\n\n -- ! is a right-inverse for _\u2219_\n \u2219-! : p \u2219 ! p \u2261 idp_ x\n \u2219-! = J' (\u03bb x y p \u2192 (p \u2219 ! p) \u2261 idp_ x) (\u03bb x \u2192 idp) p\n\n -- ! is involutive\n !-involutive : ! (! p) \u2261 p\n !-involutive = J' (\u03bb x y p \u2192 ! (! p) \u2261 p) (\u03bb x \u2192 idp) p\n\n !p\u2219p = !-\u2219\n p\u2219!p = \u2219-!\n\n ==-refl-\u2219 : {q : x \u2261 x} \u2192 q \u2261 idp_ x \u2192 q \u2219 p \u2261 p\n ==-refl-\u2219 = ap (flip _\u2219_ p)\n\n \u2219-==-refl : {q : y \u2261 y} \u2192 q \u2261 idp_ y \u2192 p \u2219 q \u2261 p\n \u2219-==-refl qr = ap (_\u2219_ p) qr \u2219 \u2219-refl p\n\n module _ {x y : A} where\n module _ (p : x \u2261 x)(q : x \u2261 y)(e : p \u2219 q \u2261 q) where\n unique-idp-left : p \u2261 idp\n unique-idp-left\n = p \u2261\u27e8 ! \u2219-refl p \u27e9\n p \u2219 idp \u2261\u27e8 ap (_\u2219_ p) (! \u2219-! q) \u27e9\n p \u2219 (q \u2219 ! q) \u2261\u27e8 \u2219-assoc p q (! q) \u27e9\n (p \u2219 q) \u2219 ! q \u2261\u27e8 ap (flip _\u2219_ (! q)) e \u27e9\n q \u2219 ! q \u2261\u27e8 \u2219-! q \u27e9\n idp \u220e\n\n module _ {x y z : A}{p\u2080 p\u2081 : x \u2261 y}{q\u2080 q\u2081 : y \u2261 z}(p : p\u2080 \u2261 p\u2081)(q : q\u2080 \u2261 q\u2081) where\n \u2219= : p\u2080 \u2219 q\u2080 \u2261 p\u2081 \u2219 q\u2081\n \u2219= = ap (flip _\u2219_ q\u2080) p \u2219 ap (_\u2219_ p\u2081) q\n\n module _ {x y z : A} where\n module _ (p : x \u2261 y)(q : y \u2261 z) where\n \u2219-\u2219-==-refl : {r : z \u2261 z} \u2192 r \u2261 idp_ z \u2192 p \u2219 q \u2219 r \u2261 p \u2219 q\n \u2219-\u2219-==-refl rr = \u2219-assoc p q _ \u2219 \u2219-==-refl (p \u2219 q) rr\n\n !p\u2219p\u2219q : ! p \u2219 p \u2219 q \u2261 q\n !p\u2219p\u2219q = \u2219-assoc (! p) p q \u2219 ==-refl-\u2219 q (!-\u2219 p)\n\n p\u2219!p\u2219q : (p : y \u2261 x) (q : y \u2261 z) \u2192 p \u2219 ! p \u2219 q \u2261 q\n p\u2219!p\u2219q p q = \u2219-assoc p _ q \u2219 ==-refl-\u2219 q (\u2219-! p)\n\n p\u2219!q\u2219q : (p : x \u2261 y) (q : z \u2261 y) \u2192 p \u2219 ! q \u2219 q \u2261 p\n p\u2219!q\u2219q p q = \u2219-==-refl p (!-\u2219 q)\n\n p\u2219q\u2219!q : (p : x \u2261 y) (q : y \u2261 z) \u2192 p \u2219 q \u2219 ! q \u2261 p\n p\u2219q\u2219!q p q = \u2219-==-refl p (\u2219-! q)\n\n \u2219-cancel : {p q : x \u2261 y}(r : y \u2261 z) \u2192 p \u2219 r \u2261 q \u2219 r \u2192 p \u2261 q\n \u2219-cancel {p = p} {q} r e\n = p \u2261\u27e8 ! p\u2219q\u2219!q p r \u27e9\n p \u2219 r \u2219 ! r \u2261\u27e8 \u2219-assoc p r (! r) \u27e9\n (p \u2219 r) \u2219 ! r \u2261\u27e8 \u2219= e idp \u27e9\n (q \u2219 r) \u2219 ! r \u2261\u27e8 ! \u2219-assoc q r (! r) \u27e9\n q \u2219 (r \u2219 ! r) \u2261\u27e8 p\u2219q\u2219!q q r \u27e9\n q \u220e\n\n!-ap : \u2200 {a b}{A : Set a}{B : Set b}(f : A \u2192 B){x y}(p : x \u2261 y)\n \u2192 ! (ap f p) \u2261 ap f (! p)\n!-ap f idp = idp\n\nap-id : \u2200 {a}{A : Set a}{x y : A}(p : x \u2261 y) \u2192 ap id p \u2261 p\nap-id idp = idp\n\nmodule _ {a b}{A : Set a}{B : Set b}{f g : A \u2192 B}(H : \u2200 x \u2192 f x \u2261 g x) where\n ap-nat : \u2200 {x y}(q : x \u2261 y) \u2192 ap f q \u2219 H _ \u2261 H _ \u2219 ap g q\n ap-nat idp = ! \u2219-refl _\n\nmodule _ {a}{A : Set a}{f : A \u2192 A}(H : \u2200 x \u2192 f x \u2261 x) where\n ap-nat-id : \u2200 x \u2192 ap f (H x) \u2261 H (f x)\n ap-nat-id x = \u2219-cancel (H x) (ap-nat H (H x) \u2219 ap (_\u2219_ (H (f x))) (ap-id (H x)))\n\ntr-\u2218 : \u2200 {a b p}{A : Set a}{B : Set b}(P : B \u2192 Set p)(f : A \u2192 B){x y}(p : x \u2261 y)\n \u2192 tr (P \u2218 f) p \u2261 tr P (ap f p)\ntr-\u2218 P f idp = idp\n\nmodule _ {a}{A : \u2605_ a} where\n tr-\u2219\u2032 : \u2200 {\u2113}(P : A \u2192 \u2605_ \u2113) {x y z} (p : x \u2261 y) (q : y \u2261 z) \u2192\n tr P (p \u2219 q) \u223c tr P q \u2218 tr P p\n tr-\u2219\u2032 P idp _ _ = idp\n\n tr-\u2219 : \u2200 {\u2113}(P : A \u2192 \u2605_ \u2113) {x y z} (p : x \u2261 y) (q : y \u2261 z) (pq : x \u2261 z) \u2192\n pq \u2261 p \u2219 q \u2192\n tr P pq \u223c tr P q \u2218 tr P p\n tr-\u2219 P p q ._ idp = tr-\u2219\u2032 P p q\n\nmodule _ {k} {K : \u2605_ k} {a} {A : \u2605_ a} {x y : A} (p : x \u2261 y) where\n tr-const : tr (const K) p \u2261 id\n tr-const = J (\u03bb _ p\u2081 \u2192 tr (const K) p\u2081 \u2261 id) idp p\n\n-- Contractible\nmodule _ {a}(A : \u2605_ a) where\n Is-contr : \u2605_ a\n Is-contr = \u03a3 A \u03bb x \u2192 \u2200 y \u2192 x \u2261 y\n\nmodule _ {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b} where\n pair= : \u2200 {x y : \u03a3 A B} \u2192 (p : fst x \u2261 fst y) \u2192 tr B p (snd x) \u2261 snd y \u2192 x \u2261 y\n pair= idp = ap (_,_ _)\n\n snd= : \u2200 {x : A} {y y' : B x} \u2192 y \u2261 y' \u2192 _\u2261_ {A = \u03a3 A B} (x , y) (x , y')\n snd= = pair= idp\n\n tr-snd= : \u2200 {p}(P : \u03a3 A B \u2192 \u2605_ p){x}{y\u2080 y\u2081 : B x}(y= : y\u2080 \u2261 y\u2081)\n \u2192 tr P (snd= {x = x} y=) \u223c tr (P \u2218 _,_ x) y=\n tr-snd= P idp p = idp\n\nmodule _ {a}{A : \u2605_ a} where\n tr-r\u2261 : {x y z : A}(p : y \u2261 z)(q : x \u2261 y) \u2192 tr (\u03bb v \u2192 x \u2261 v) p q \u2261 q \u2219 p\n tr-r\u2261 idp q = ! \u2219-refl q\n\n tr-l\u2261 : {x y z : A}(p : x \u2261 y)(q : x \u2261 z) \u2192 tr (\u03bb v \u2192 v \u2261 z) p q \u2261 ! p \u2219 q\n tr-l\u2261 idp q = idp\n\nmodule _ {A : \u2605}(f g : A \u2192 \u2605){x y : A}(p : x \u2261 y)(h : f x \u2192 g x) where\n tr-\u2192 : tr (\u03bb x \u2192 f x \u2192 g x) p h \u2261 (\u03bb x \u2192 tr g p (h (tr f (! p) x)))\n tr-\u2192 = J' (\u03bb x y p \u2192 (h : f x \u2192 g x) \u2192 tr (\u03bb x \u2192 f x \u2192 g x) p h \u2261 (\u03bb x \u2192 tr g p (h (tr f (! p) x))))\n (\u03bb _ _ \u2192 idp) p h\n\nmodule _ {a}{b}{A : \u2605_ a}{B : \u2605_ b} where\n pair\u00d7= : \u2200 {x x' : A}(p : x \u2261 x')\n {y y' : B}(q : y \u2261 y')\n \u2192 (x , y) \u2261 (x' , y')\n pair\u00d7= idp q = snd= q\n\nmodule _ {a b c}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{x\u2080 : A}{y\u2080 : B x\u2080}{C : \u2605_ c}\n (f : (x : A) (y : B x) \u2192 C) where\n ap\u2082\u2193 : {x\u2081 : A}(x= : x\u2080 \u2261 x\u2081)\n {y\u2081 : B x\u2081}(y= : y\u2080 \u2261 y\u2081 [ B \u2193 x= ])\n \u2192 f x\u2080 y\u2080 \u2261 f x\u2081 y\u2081\n ap\u2082\u2193 idp = ap (f x\u2080)\n {- Or with J\n ap\u2082\u2193 x= = J (\u03bb x\u2081' x=' \u2192 {y\u2081 : B x\u2081'}(y= : y\u2080 \u2261 y\u2081 [ _ \u2193 x=' ])\n \u2192 f x\u2080 y\u2080 \u2261 f x\u2081' y\u2081)\n (\u03bb y= \u2192 ap (f x\u2080) y=) x=\n -- -}\n\n apd\u2082 : {x\u2081 : A}(x= : x\u2080 \u2261 x\u2081)\n {y\u2081 : B x\u2081}(y= : tr B x= y\u2080 \u2261 y\u2081)\n \u2192 f x\u2080 y\u2080 \u2261 f x\u2081 y\u2081\n -- apd\u2082 idp = ap (f x\u2080)\n -- {- Or with J\n apd\u2082 x= = J (\u03bb x\u2081' x=' \u2192 {y\u2081 : B x\u2081'}(y= : tr B x=' y\u2080 \u2261 y\u2081) \u2192 f x\u2080 y\u2080 \u2261 f x\u2081' y\u2081)\n (\u03bb y= \u2192 ap (f x\u2080) y=) x=\n -- -}\n\nmodule _ {a b c d}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{C : \u2605_ c}{x\u2080 : A}{y\u2080 : B x\u2080 \u2192 C}{D : \u2605_ d}\n {{_ : FunExt}}\n (f : (x : A) (y : B x \u2192 C) \u2192 D) where\n apd\u2082\u207b : {x\u2081 : A}(x= : x\u2080 \u2261 x\u2081)\n {y\u2081 : B x\u2081 \u2192 C}(y= : \u2200 x \u2192 y\u2080 x \u2261 y\u2081 (tr B x= x))\n \u2192 f x\u2080 y\u2080 \u2261 f x\u2081 y\u2081\n apd\u2082\u207b idp y= = ap (f x\u2080) (\u03bb= y=)\n\nmodule Equivalences where\n\n module _ {a b}{A : \u2605_ a}{B : \u2605_ b} where\n _LeftInverseOf_ : (B \u2192 A) \u2192 (A \u2192 B) \u2192 \u2605_ a\n linv LeftInverseOf f = \u2200 x \u2192 linv (f x) \u2261 x\n\n _RightInverseOf_ : (B \u2192 A) \u2192 (A \u2192 B) \u2192 \u2605_ b\n rinv RightInverseOf f = \u2200 x \u2192 f (rinv x) \u2261 x\n\n record Linv (f : A \u2192 B) : \u2605_(a \u2294 b) where\n field\n linv : B \u2192 A\n is-linv : \u2200 x \u2192 linv (f x) \u2261 x\n\n injective : \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y\n injective p = ! is-linv _ \u2219 ap linv p \u2219 is-linv _\n\n record Rinv (f : A \u2192 B) : \u2605_(a \u2294 b) where\n field\n rinv : B \u2192 A\n is-rinv : \u2200 x \u2192 f (rinv x) \u2261 x\n\n surjective : \u2200 y \u2192 \u2203 \u03bb x \u2192 f x \u2261 y\n surjective y = rinv y , is-rinv y\n\n record Biinv (f : A \u2192 B) : \u2605_(a \u2294 b) where\n field\n has-linv : Linv f\n has-rinv : Rinv f\n\n open Linv has-linv public\n open Rinv has-rinv public\n\n module _ {f : A \u2192 B}\n (g : B \u2192 A)(g-f : (x : A) \u2192 g (f x) \u2261 x)\n (h : B \u2192 A)(f-h : (y : B) \u2192 f (h y) \u2261 y) where\n biinv : Biinv f\n biinv = record { has-linv = record { linv = g ; is-linv = g-f }\n ; has-rinv = record { rinv = h ; is-rinv = f-h } }\n\n record Qinv (f : A \u2192 B) : \u2605_(a \u2294 b) where\n field\n inv : B \u2192 A\n inv-is-linv : \u2200 x \u2192 inv (f x) \u2261 x\n inv-is-rinv : \u2200 x \u2192 f (inv x) \u2261 x\n\n has-biinv : Biinv f\n has-biinv = record { has-linv = record { linv = inv\n ; is-linv = inv-is-linv }\n ; has-rinv = record { rinv = inv\n ; is-rinv = inv-is-rinv } }\n\n open Biinv has-biinv public\n\n HAE : {f : A \u2192 B} \u2192 Qinv f \u2192 \u2605_(a \u2294 b)\n HAE {f} f-qinv = \u2200 x \u2192 ap f (F.inv-is-linv x) \u2261 F.inv-is-rinv (f x)\n where module F = Qinv f-qinv\n\n record Is-equiv (f : A \u2192 B) : \u2605_(a \u2294 b) where\n field\n has-qinv : Qinv f\n is-hae : HAE has-qinv\n open Qinv has-qinv public\n\n module _ {f : A \u2192 B}(g : B \u2192 A)\n (f-g : (y : B) \u2192 f (g y) \u2261 y)\n (g-f : (x : A) \u2192 g (f x) \u2261 x) where\n qinv : Qinv f\n qinv = record\n { inv = g\n ; inv-is-linv = g-f\n ; inv-is-rinv = f-g }\n\n module _ {f : A \u2192 B}(g : B \u2192 A)\n (f-g : (y : B) \u2192 f (g y) \u2261 y)\n (g-f : (x : A) \u2192 g (f x) \u2261 x) where\n f-g' : (x : B) \u2192 f (g x) \u2261 x\n f-g' x = ! ap (f \u2218 g) (f-g x) \u2219 ap f (g-f (g x)) \u2219 f-g x\n -- g-f' x = ap g {!f-g ?!} \u2219 {!!}\n\n postulate hae : HAE (qinv g f-g' g-f)\n\n is-equiv : Is-equiv f\n is-equiv = record\n { has-qinv = qinv g f-g' g-f\n ; is-hae = hae }\n\n module Biinv-inv {a b}{A : \u2605_ a}{B : \u2605_ b}{f : A \u2192 B}\n (f\u1d2e : Biinv f) where\n open Biinv f\u1d2e\n inv : B \u2192 A\n inv = linv \u2218 f \u2218 rinv\n\n inv-biinv : Biinv inv\n inv-biinv =\n biinv f (\u03bb x \u2192 ap f (is-linv (rinv x)) \u2219 is-rinv x)\n f (\u03bb x \u2192 ap linv (is-rinv (f x)) \u2219 is-linv x)\n\n module _ {a b}{A : \u2605_ a}{B : \u2605_ b}{f : A \u2192 B}\n (f\u1d31 : Is-equiv f) where\n open Is-equiv f\u1d31\n\n inv-is-equiv : Is-equiv inv\n inv-is-equiv = is-equiv f is-linv is-rinv\n\n module _ {a b} where\n infix 4 _\u2243_\n _\u2243_ : \u2605_ a \u2192 \u2605_ b \u2192 \u2605_(a \u2294 b)\n A \u2243 B = \u03a3 (A \u2192 B) Is-equiv\n\n module _ {a b}{A : \u2605_ a}{B : \u2605_ b}\n (f : A \u2192 B)(g : B \u2192 A)\n (f-g : (y : B) \u2192 f (g y) \u2261 y)\n (g-f : (x : A) \u2192 g (f x) \u2261 x) where\n equiv : A \u2243 B\n equiv = f , is-equiv g f-g g-f\n\n module _ {a}{A : \u2605_ a}\n (f : A \u2192 A)(f-inv : f LeftInverseOf f) where\n self-inv-is-equiv : Is-equiv f\n self-inv-is-equiv = is-equiv f f-inv f-inv\n\n self-inv-equiv : A \u2243 A\n self-inv-equiv = f , self-inv-is-equiv\n\n self-inv-biinv : Biinv f\n self-inv-biinv = biinv f f-inv f f-inv\n\n module _ {a}{A : \u2605_ a} where\n id\u1d31 : Is-equiv {A = A} id\n id\u1d31 = self-inv-is-equiv _ \u03bb _ \u2192 idp\n\n id\u1d2e : Biinv {A = A} id\n id\u1d2e = self-inv-biinv _ \u03bb _ \u2192 idp\n\n module _ {a b c}{A : \u2605_ a}{B : \u2605_ b}{C : \u2605_ c}{g : B \u2192 C}{f : A \u2192 B} where\n _\u2218\u1d31_ : Is-equiv g \u2192 Is-equiv f \u2192 Is-equiv (g \u2218 f)\n g\u1d31 \u2218\u1d31 f\u1d31 = is-equiv (F.inv \u2218 G.inv)\n (\u03bb x \u2192 ap g (F.inv-is-rinv _) \u2219 G.inv-is-rinv _)\n (\u03bb x \u2192 ap F.inv (G.inv-is-linv _) \u2219 F.inv-is-linv _)\n where\n module G = Is-equiv g\u1d31\n module F = Is-equiv f\u1d31\n\n _\u2218\u1d2e_ : Biinv g \u2192 Biinv f \u2192 Biinv (g \u2218 f)\n g\u1d2e \u2218\u1d2e f\u1d2e =\n biinv (F.linv \u2218 G.linv)\n (\u03bb x \u2192 ap F.linv (G.is-linv (f x)) \u2219 F.is-linv x)\n (F.rinv \u2218 G.rinv)\n (\u03bb x \u2192 ap g (F.is-rinv _) \u2219 G.is-rinv x)\n where\n module G = Biinv g\u1d2e\n module F = Biinv f\u1d2e\n\n module Equiv {a b}{A : \u2605_ a}{B : \u2605_ b}(e : A \u2243 B) where\n \u00b7\u2192 : A \u2192 B\n \u00b7\u2192 = fst e\n\n open Is-equiv (snd e) public\n renaming (linv to \u00b7\u2190; rinv to \u00b7\u2190\u2032; is-linv to \u00b7\u2190-inv-l; is-rinv to \u00b7\u2190-inv-r)\n\n -- Equivalences are \"injective\"\n equiv-inj : {x y : A} \u2192 (\u00b7\u2192 x \u2261 \u00b7\u2192 y \u2192 x \u2261 y)\n equiv-inj p = ! \u00b7\u2190-inv-l _ \u2219 ap \u00b7\u2190 p \u2219 \u00b7\u2190-inv-l _\n\n \u2013> = \u00b7\u2192\n <\u2013 = \u00b7\u2190\n <\u2013' = \u00b7\u2190\u2032\n\n <\u2013-inv-l = \u00b7\u2190-inv-l\n <\u2013-inv-r = \u00b7\u2190-inv-r\n\n open Equiv public\n\n module _ {\u2113} where\n \u2243-refl : Reflexive (_\u2243_ {\u2113})\n \u2243-refl = _ , id\u1d31\n\n \u2243-sym : Symmetric (_\u2243_ {\u2113})\n \u2243-sym (_ , f\u1d31) = _ , inv-is-equiv f\u1d31\n\n \u2243-trans : Transitive (_\u2243_ {\u2113})\n \u2243-trans (_ , p) (_ , q) = _ , q \u2218\u1d31 p\n\n \u2243-! = \u2243-sym\n _\u2243-\u2219_ = \u2243-trans\n\n module _ {a}(A : \u2605_ a) where\n Paths : \u2605_ a\n Paths = \u03a3 A \u03bb x \u2192 \u03a3 A \u03bb y \u2192 x \u2261 y\n\n module _ {a}{A : \u2605_ a} where\n id-path : A \u2192 Paths A\n id-path x = x , x , idp\n\n fst-rinv-id-path : \u2200 p \u2192 id-path (fst p) \u2261 p\n fst-rinv-id-path (x , y , p) = snd= (pair= p (J (\u03bb y p \u2192 tr (_\u2261_ x) p idp \u2261 p) idp p))\n\n id-path-is-equiv : Is-equiv id-path\n id-path-is-equiv = is-equiv fst fst-rinv-id-path (\u03bb x \u2192 idp)\n\n \u2243-Paths : A \u2243 Paths A\n \u2243-Paths = id-path , id-path-is-equiv\n\n module _ {a b}{A : \u2605_ a}{B : \u2605_ b}(f : A \u2192 B) where\n hfiber : (y : B) \u2192 \u2605_(a \u2294 b)\n hfiber y = \u03a3 A \u03bb x \u2192 f x \u2261 y\n\n Is-equiv-alt : \u2605_(a \u2294 b)\n Is-equiv-alt = (y : B) \u2192 Is-contr (hfiber y)\n\n module Is-contr-to-Is-equiv {a}{A : \u2605_ a}(A-contr : Is-contr A) where\n const-is-equiv : Is-equiv (\u03bb (_ : \ud835\udfd9) \u2192 fst A-contr)\n const-is-equiv = is-equiv _ (snd A-contr) (\u03bb _ \u2192 idp)\n \ud835\udfd9\u2243 : \ud835\udfd9 \u2243 A\n \ud835\udfd9\u2243 = _ , const-is-equiv\n module Is-equiv-to-Is-contr {a}{A : \u2605_ a}(f : \ud835\udfd9 \u2192 A)(f-is-equiv : Is-equiv f) where\n open Is-equiv f-is-equiv\n A-contr : Is-contr A\n A-contr = f _ , is-rinv\n\n module _ {a}{A : \u2605_ a}{b}{B : \u2605_ b} where\n iso-to-equiv : (A \u2194 B) \u2192 (A \u2243 B)\n iso-to-equiv iso = to iso , is-equiv (from iso) (Inverse.right-inverse-of iso) (Inverse.left-inverse-of iso)\n\n equiv-to-iso : (A \u2243 B) \u2192 (A \u2194 B)\n equiv-to-iso (f , f-is-equiv) = inverses f (f\u1d31.linv \u2218 f \u2218 f\u1d31.rinv)\n (\u03bb x \u2192 ap f\u1d31.linv (f\u1d31.is-rinv (f x)) \u2219 f\u1d31.is-linv x)\n (\u03bb x \u2192 ap f (f\u1d31.is-linv (f\u1d31.rinv x)) \u2219 f\u1d31.is-rinv x)\n where module f\u1d31 = Is-equiv f-is-equiv\n\n {-\n iso-to-equiv-to-iso : (iso : A \u2194 B) \u2192 equiv-to-iso (iso-to-equiv iso) \u2261 iso\n iso-to-equiv-to-iso iso = {!!}\n where module Iso = Inverse iso\n\n iso-to-equiv-is-equiv : Is-equiv iso-to-equiv\n iso-to-equiv-is-equiv = record { linv = equiv-to-iso ; is-linv = {!!} ; rinv = {!!} ; is-rinv = {!!} }\n -}\nopen Equivalences\n\ndata T-level : \u2605\u2080 where\n \u27e8-2\u27e9 : T-level\n \u27e8S_\u27e9 : (n : T-level) \u2192 T-level\n\n\u27e8-1\u27e9 \u27e80\u27e9 : T-level\n\u27e8-1\u27e9 = \u27e8S \u27e8-2\u27e9 \u27e9\n\u27e80\u27e9 = \u27e8S \u27e8-1\u27e9 \u27e9\n\u27e81\u27e9 = \u27e8S \u27e80\u27e9 \u27e9\n\u27e82\u27e9 = \u27e8S \u27e81\u27e9 \u27e9\n\n\u2115\u208b\u2082 = T-level\n\nmodule _ {a} where\n private\n U = \u2605_ a\n\n has-level : T-level \u2192 U \u2192 U\n has-level \u27e8-2\u27e9 A = Is-contr A\n has-level \u27e8S n \u27e9 A = (x y : A) \u2192 has-level n (x \u2261 y)\n\n is-prop : U \u2192 U\n is-prop A = has-level \u27e8-1\u27e9 A\n\n is-set : U \u2192 U\n is-set A = has-level \u27e80\u27e9 A\n\n has-all-paths : U \u2192 U\n has-all-paths A = (x y : A) \u2192 x \u2261 y\n\n UIP : U \u2192 U\n UIP A = {x y : A} (p q : x \u2261 y) \u2192 p \u2261 q\n\n private\n UIP-check : {A : U} \u2192 UIP A \u2261 ({x y : A} \u2192 has-all-paths (x \u2261 y))\n UIP-check = idp\n\n module _ {A : U} where\n prop-has-all-paths : is-prop A \u2192 has-all-paths A\n prop-has-all-paths A-prop x y = fst (A-prop x y)\n\n all-paths-is-prop : has-all-paths A \u2192 is-prop A\n all-paths-is-prop c x y = c x y , canon-path\n where\n lemma : {x y : A} (p : x \u2261 y) \u2192 c x y \u2261 p \u2219 c y y\n lemma = J' (\u03bb x y p \u2192 c x y \u2261 p \u2219 c y y) (\u03bb x \u2192 idp)\n\n canon-path : {x y : A} (p : x \u2261 y) \u2192 c x y \u2261 p\n canon-path = J' (\u03bb x y p \u2192 c x y \u2261 p)\n (\u03bb x \u2192 lemma (! c x x) \u2219 !-\u2219 (c x x))\n\n Is-contr\u2192is-prop : Is-contr A \u2192 is-prop A\n Is-contr\u2192is-prop (x , p) y z\n = ! p y \u2219 p z\n , J' (\u03bb y\u2081 z\u2081 q \u2192 ! p y\u2081 \u2219 p z\u2081 \u2261 q) (!-\u2219 \u2218 p)\n\n {-\n has-level-up : \u2200 {n} \u2192 has-level n A \u2192 has-level \u27e8S n \u27e9 A\n has-level-up {\u27e8-2\u27e9} = Is-contr\u2192is-prop\n has-level-up {\u27e8S n \u27e9} \u03c0 x y p q = {!!}\n\n Is-contr-is-prop : is-prop (Is-contr A)\n Is-contr-is-prop (x , p) (y , q) = ?\n\n module _ {{_ : FunExt}} where\n is-prop-is-prop : has-all-paths (has-all-paths A)\n is-prop-is-prop h0 h1 = \u03bb= \u03bb x \u2192 \u03bb= \u03bb y \u2192 {!!}\n -}\n\n\ud835\udfd8-is-prop : is-prop \ud835\udfd8\n\ud835\udfd8-is-prop () _\n\n\ud835\udfd8-has-all-paths : has-all-paths \ud835\udfd8\n\ud835\udfd8-has-all-paths () _\n\n\ud835\udfd9-is-contr : Is-contr \ud835\udfd9\n\ud835\udfd9-is-contr = _ , \u03bb _ \u2192 idp\n\n\ud835\udfd9-is-prop : is-prop \ud835\udfd9\n\ud835\udfd9-is-prop = Is-contr\u2192is-prop \ud835\udfd9-is-contr\n\n\ud835\udfd9-has-all-paths : has-all-paths \ud835\udfd9\n\ud835\udfd9-has-all-paths _ _ = idp\n\nmodule _ {{_ : FunExt}}{a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b} where\n \u03a0-has-all-paths : (\u2200 x \u2192 has-all-paths (B x)) \u2192 has-all-paths (\u03a0 A B)\n \u03a0-has-all-paths B-has-all-paths f g\n = \u03bb= \u03bb _ \u2192 B-has-all-paths _ _ _\n\n \u03a0-is-prop : (\u2200 x \u2192 is-prop (B x)) \u2192 is-prop (\u03a0 A B)\n \u03a0-is-prop B-is-prop = all-paths-is-prop (\u03a0-has-all-paths (prop-has-all-paths \u2218 B-is-prop))\n\nmodule _ {{_ : FunExt}}{a}{A : \u2605_ a} where\n \u00ac-has-all-paths : has-all-paths (\u00ac A)\n \u00ac-has-all-paths = \u03a0-has-all-paths (\u03bb _ \u2192 \ud835\udfd8-has-all-paths)\n\n \u00ac-is-prop : is-prop (\u00ac A)\n \u00ac-is-prop = \u03a0-is-prop (\u03bb _ \u2192 \ud835\udfd8-is-prop)\n\nmodule _ {a} (A : \u2605_ a) where\n has-dec-eq : \u2605_ a\n has-dec-eq = (x y : A) \u2192 Dec (x \u2261 y)\n\nmodule _ {a} {A : \u2605_ a} (d : has-dec-eq A) where\n private\n Code' : {x y : A} (dxy : Dec (x \u2261 y)) (dxx : Dec (x \u2261 x)) \u2192 x \u2261 y \u2192 \u2605_ a\n Code' {x} {y} dxy dxx p = case dxy of \u03bb\n { (no _) \u2192 Lift \ud835\udfd8\n ; (yes b) \u2192 case dxx of \u03bb\n { (no _) \u2192 Lift \ud835\udfd8\n ; (yes b') \u2192 p \u2261 ! b' \u2219 b\n }\n }\n\n Code : {x y : A} \u2192 x \u2261 y \u2192 \u2605_ a\n Code {x} {y} p = Code' (d x y) (d x x) p\n\n encode : {x y : A} \u2192 (p : x \u2261 y) -> Code p\n encode {x} = J (\u03bb y (p : x \u2261 y) \u2192 Code p) (elim-Dec (\u03bb d \u2192 Code' d d idp) (!_ \u2218 !p\u2219p) (\u03bb x\u2081 \u2192 lift (x\u2081 idp)) (d x x))\n\n UIP-dec : UIP A\n UIP-dec {x} idp q with d x x | encode q\n UIP-dec idp q | yes a | p' = ! !p\u2219p a \u2219 ! p'\n UIP-dec idp q | no r | _ = \ud835\udfd8-elim (r idp)\n\n dec-eq-is-set : is-set A\n dec-eq-is-set _ _ = all-paths-is-prop UIP-dec\n\nmodule _ {\u2113}{A : \u2605_ \u2113} where\n UIP-set : is-set A \u2192 UIP A\n UIP-set A-is-set p q = fst (A-is-set _ _ p q)\n\n UIP\u2192is-set : UIP A \u2192 is-set A\n UIP\u2192is-set A-is-set' x y = all-paths-is-prop A-is-set'\n\n \u03a9\u2081-set-to-contr : is-set A \u2192 (x : A) \u2192 Is-contr (x \u2261 x)\n \u03a9\u2081-set-to-contr A-is-set x = idp , UIP-set A-is-set idp\n\n coe!-inv-r : \u2200 {B}(p : A \u2261 B) y \u2192 coe p (coe! p y) \u2261 y\n coe!-inv-r idp y = idp\n\n coe!-inv-l : \u2200 {B}(p : A \u2261 B) x \u2192 coe! p (coe p x) \u2261 x\n coe!-inv-l idp x = idp\n\n coe-equiv : \u2200 {B} \u2192 A \u2261 B \u2192 A \u2243 B\n coe-equiv p = equiv (coe p) (coe! p) (coe!-inv-r p) (coe!-inv-l p)\n\n coe\u2218coe : \u2200 {B C}(p : B \u2261 C)(q : A \u2261 B)(m : A) \u2192 coe p (coe q m) \u2261 coe (q \u2219 p) m\n coe\u2218coe p idp m = idp\n\n coe-same : \u2200 {B}{p q : A \u2261 B}(e : p \u2261 q)(x : A) \u2192 coe p x \u2261 coe q x\n coe-same p x = ap (\u03bb X \u2192 coe X x) p\n\n coe-inj : \u2200 {B}{x y : A}(p : A \u2261 B) \u2192 coe p x \u2261 coe p y \u2192 x \u2261 y\n coe-inj idp = id\n\n module _ {B : \u2605_ \u2113}(p : A \u2261 B){x y : A} where\n coe-paths-equiv : (x \u2261 y) \u2261 (coe p x \u2261 coe p y)\n coe-paths-equiv = J (\u03bb B (p : A \u2261 B) \u2192 (x \u2261 y) \u2261 (coe p x \u2261 coe p y)) idp p\n\npostulate\n UA : \u2605\nmodule _ {\u2113}{A B : \u2605_ \u2113}{{_ : UA}} where\n postulate\n ua : (A \u2243 B) \u2192 (A \u2261 B)\n coe-equiv-\u03b2 : (e : A \u2243 B) \u2192 coe-equiv (ua e) \u2261 e\n ua-\u03b7 : (p : A \u2261 B) \u2192 ua (coe-equiv p) \u2261 p\n\n ua-equiv : (A \u2243 B) \u2243 (A \u2261 B)\n ua-equiv = equiv ua coe-equiv ua-\u03b7 coe-equiv-\u03b2\n\n coe-\u03b2 : (e : A \u2243 B) (a : A) \u2192 coe (ua e) a \u2261 \u00b7\u2192 e a\n coe-\u03b2 e a = ap (\u03bb e \u2192 \u00b7\u2192 e a) (coe-equiv-\u03b2 e)\n\n postulate\n coe!-\u03b2 : (e : A \u2243 B) (b : B) \u2192 coe! (ua e) b \u2261 \u00b7\u2190 e b\n\n module _ (e : A \u2243 B){x y : A} where\n \u00b7\u2192-paths-equiv : (x \u2261 y) \u2261 (\u00b7\u2192 e x \u2261 \u00b7\u2192 e y)\n \u00b7\u2192-paths-equiv = coe-paths-equiv (ua e) \u2219 ap\u2082 _\u2261_ (coe-\u03b2 e x) (coe-\u03b2 e y)\n\n \u2013>-paths-equiv = \u00b7\u2192-paths-equiv\n\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule HoTT where\n\nopen import Type\nopen import Level.NP\nopen import Function.NP\nopen import Function.Extensionality\nopen import Data.Zero using (\ud835\udfd8; \ud835\udfd8-elim)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Product.NP\nopen import Data.Sum using (_\u228e_) renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\nopen import Relation.Nullary.NP\nopen import Relation.Binary using (Reflexive; Symmetric; Transitive)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; ap; coe; coe!; !_; _\u2219_; J; ap\u2193; PathOver; tr; ap\u2082)\n renaming (refl to idp; _\u2257_ to _\u223c_; J-orig to J')\nopen \u2261.\u2261-Reasoning\n\nimport Function.Inverse.NP as Inv\nopen Inv using (_\u2194_; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\n\nmodule _ {a} {A : \u2605_ a} where\n idp_ : (x : A) \u2192 x \u2261 x\n idp_ _ = idp\n\n refl-\u2219 : \u2200 {x y : A} (p : x \u2261 y) \u2192 idp_ x \u2219 p \u2261 p\n refl-\u2219 _ = idp\n\n \u2219-refl : \u2200 {x y : A} (p : x \u2261 y) \u2192 p \u2219 idp_ y \u2261 p\n \u2219-refl = J' (\u03bb (x y : A) (p : x \u2261 y) \u2192 (p \u2219 idp_ y) \u2261 p) (\u03bb x \u2192 idp)\n\n -- could be derived in any groupoid\n hom-!-\u2219 : \u2200 {x y z : A} (p : x \u2261 y)(q : y \u2261 z) \u2192 !(p \u2219 q) \u2261 ! q \u2219 ! p\n hom-!-\u2219 p q = J' (\u03bb x y p \u2192 \u2200 z \u2192 (q : y \u2261 z) \u2192 !(p \u2219 q) \u2261 ! q \u2219 ! p) (\u03bb x z q \u2192 ! \u2219-refl (! q)) p _ q\n\n module _ {x y : A} where\n\n \u2219-assoc : (p : x \u2261 y) {z : A} (q : y \u2261 z) {t : A} (r : z \u2261 t) \u2192 p \u2219 q \u2219 r \u2261 (p \u2219 q) \u2219 r\n \u2219-assoc = J' (\u03bb x y p \u2192 \u2200 {z} (q : y \u2261 z) {t} (r : z \u2261 t) \u2192 p \u2219 (q \u2219 r) \u2261 (p \u2219 q) \u2219 r)\n (\u03bb x q r \u2192 idp)\n\n module _ (p : x \u2261 y) where\n -- ! is a left-inverse for _\u2219_\n !-\u2219 : ! p \u2219 p \u2261 idp_ y\n !-\u2219 = J' (\u03bb x y p \u2192 (! p \u2219 p) \u2261 idp_ y) (\u03bb x \u2192 idp) p\n\n -- ! is a right-inverse for _\u2219_\n \u2219-! : p \u2219 ! p \u2261 idp_ x\n \u2219-! = J' (\u03bb x y p \u2192 (p \u2219 ! p) \u2261 idp_ x) (\u03bb x \u2192 idp) p\n\n -- ! is involutive\n !-involutive : ! (! p) \u2261 p\n !-involutive = J' (\u03bb x y p \u2192 ! (! p) \u2261 p) (\u03bb x \u2192 idp) p\n\n !p\u2219p = !-\u2219\n p\u2219!p = \u2219-!\n\n ==-refl-\u2219 : {q : x \u2261 x} \u2192 q \u2261 idp_ x \u2192 q \u2219 p \u2261 p\n ==-refl-\u2219 = ap (flip _\u2219_ p)\n\n \u2219-==-refl : {q : y \u2261 y} \u2192 q \u2261 idp_ y \u2192 p \u2219 q \u2261 p\n \u2219-==-refl qr = ap (_\u2219_ p) qr \u2219 \u2219-refl p\n\n module _ {x y : A} where\n module _ (p : x \u2261 x)(q : x \u2261 y)(e : p \u2219 q \u2261 q) where\n unique-idp-left : p \u2261 idp\n unique-idp-left\n = p \u2261\u27e8 ! \u2219-refl p \u27e9\n p \u2219 idp \u2261\u27e8 ap (_\u2219_ p) (! \u2219-! q) \u27e9\n p \u2219 (q \u2219 ! q) \u2261\u27e8 \u2219-assoc p q (! q) \u27e9\n (p \u2219 q) \u2219 ! q \u2261\u27e8 ap (flip _\u2219_ (! q)) e \u27e9\n q \u2219 ! q \u2261\u27e8 \u2219-! q \u27e9\n idp \u220e\n\n module _ {x y z : A}{p\u2080 p\u2081 : x \u2261 y}{q\u2080 q\u2081 : y \u2261 z}(p : p\u2080 \u2261 p\u2081)(q : q\u2080 \u2261 q\u2081) where\n \u2219= : p\u2080 \u2219 q\u2080 \u2261 p\u2081 \u2219 q\u2081\n \u2219= = ap (flip _\u2219_ q\u2080) p \u2219 ap (_\u2219_ p\u2081) q\n\n module _ {x y z : A} where\n module _ (p : x \u2261 y)(q : y \u2261 z) where\n \u2219-\u2219-==-refl : {r : z \u2261 z} \u2192 r \u2261 idp_ z \u2192 p \u2219 q \u2219 r \u2261 p \u2219 q\n \u2219-\u2219-==-refl rr = \u2219-assoc p q _ \u2219 \u2219-==-refl (p \u2219 q) rr\n\n !p\u2219p\u2219q : ! p \u2219 p \u2219 q \u2261 q\n !p\u2219p\u2219q = \u2219-assoc (! p) p q \u2219 ==-refl-\u2219 q (!-\u2219 p)\n\n p\u2219!p\u2219q : (p : y \u2261 x) (q : y \u2261 z) \u2192 p \u2219 ! p \u2219 q \u2261 q\n p\u2219!p\u2219q p q = \u2219-assoc p _ q \u2219 ==-refl-\u2219 q (\u2219-! p)\n\n p\u2219!q\u2219q : (p : x \u2261 y) (q : z \u2261 y) \u2192 p \u2219 ! q \u2219 q \u2261 p\n p\u2219!q\u2219q p q = \u2219-==-refl p (!-\u2219 q)\n\n p\u2219q\u2219!q : (p : x \u2261 y) (q : y \u2261 z) \u2192 p \u2219 q \u2219 ! q \u2261 p\n p\u2219q\u2219!q p q = \u2219-==-refl p (\u2219-! q)\n\n \u2219-cancel : {p q : x \u2261 y}(r : y \u2261 z) \u2192 p \u2219 r \u2261 q \u2219 r \u2192 p \u2261 q\n \u2219-cancel {p = p} {q} r e\n = p \u2261\u27e8 ! p\u2219q\u2219!q p r \u27e9\n p \u2219 r \u2219 ! r \u2261\u27e8 \u2219-assoc p r (! r) \u27e9\n (p \u2219 r) \u2219 ! r \u2261\u27e8 \u2219= e idp \u27e9\n (q \u2219 r) \u2219 ! r \u2261\u27e8 ! \u2219-assoc q r (! r) \u27e9\n q \u2219 (r \u2219 ! r) \u2261\u27e8 p\u2219q\u2219!q q r \u27e9\n q \u220e\n\n!-ap : \u2200 {a b}{A : Set a}{B : Set b}(f : A \u2192 B){x y}(p : x \u2261 y)\n \u2192 ! (ap f p) \u2261 ap f (! p)\n!-ap f idp = idp\n\nap-id : \u2200 {a}{A : Set a}{x y : A}(p : x \u2261 y) \u2192 ap id p \u2261 p\nap-id idp = idp\n\nmodule _ {a b}{A : Set a}{B : Set b}{f g : A \u2192 B}(H : \u2200 x \u2192 f x \u2261 g x) where\n ap-nat : \u2200 {x y}(q : x \u2261 y) \u2192 ap f q \u2219 H _ \u2261 H _ \u2219 ap g q\n ap-nat idp = ! \u2219-refl _\n\nmodule _ {a}{A : Set a}{f : A \u2192 A}(H : \u2200 x \u2192 f x \u2261 x) where\n ap-nat-id : \u2200 x \u2192 ap f (H x) \u2261 H (f x)\n ap-nat-id x = \u2219-cancel (H x) (ap-nat H (H x) \u2219 ap (_\u2219_ (H (f x))) (ap-id (H x)))\n\ntr-\u2218 : \u2200 {a b p}{A : Set a}{B : Set b}(P : B \u2192 Set p)(f : A \u2192 B){x y}(p : x \u2261 y)\n \u2192 tr (P \u2218 f) p \u2261 tr P (ap f p)\ntr-\u2218 P f idp = idp\n\nmodule _ {a}{A : \u2605_ a} where\n tr-\u2219\u2032 : \u2200 {\u2113}(P : A \u2192 \u2605_ \u2113) {x y z} (p : x \u2261 y) (q : y \u2261 z) \u2192\n tr P (p \u2219 q) \u223c tr P q \u2218 tr P p\n tr-\u2219\u2032 P idp _ _ = idp\n\n tr-\u2219 : \u2200 {\u2113}(P : A \u2192 \u2605_ \u2113) {x y z} (p : x \u2261 y) (q : y \u2261 z) (pq : x \u2261 z) \u2192\n pq \u2261 p \u2219 q \u2192\n tr P pq \u223c tr P q \u2218 tr P p\n tr-\u2219 P p q ._ idp = tr-\u2219\u2032 P p q\n\nmodule _ {k} {K : \u2605_ k} {a} {A : \u2605_ a} {x y : A} (p : x \u2261 y) where\n tr-const : tr (const K) p \u2261 id\n tr-const = J (\u03bb _ p\u2081 \u2192 tr (const K) p\u2081 \u2261 id) idp p\n\n-- Contractible\nmodule _ {a}(A : \u2605_ a) where\n Is-contr : \u2605_ a\n Is-contr = \u03a3 A \u03bb x \u2192 \u2200 y \u2192 x \u2261 y\n\nmodule _ {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b} where\n pair= : \u2200 {x y : \u03a3 A B} \u2192 (p : fst x \u2261 fst y) \u2192 tr B p (snd x) \u2261 snd y \u2192 x \u2261 y\n pair= idp = ap (_,_ _)\n\n snd= : \u2200 {x : A} {y y' : B x} \u2192 y \u2261 y' \u2192 _\u2261_ {A = \u03a3 A B} (x , y) (x , y')\n snd= = pair= idp\n\n tr-snd= : \u2200 {p}(P : \u03a3 A B \u2192 \u2605_ p){x}{y\u2080 y\u2081 : B x}(y= : y\u2080 \u2261 y\u2081)\n \u2192 tr P (snd= {x = x} y=) \u223c tr (P \u2218 _,_ x) y=\n tr-snd= P idp p = idp\n\nmodule _ {a}{A : \u2605_ a} where\n tr-r\u2261 : {x y z : A}(p : y \u2261 z)(q : x \u2261 y) \u2192 tr (\u03bb v \u2192 x \u2261 v) p q \u2261 q \u2219 p\n tr-r\u2261 idp q = ! \u2219-refl q\n\n tr-l\u2261 : {x y z : A}(p : x \u2261 y)(q : x \u2261 z) \u2192 tr (\u03bb v \u2192 v \u2261 z) p q \u2261 ! p \u2219 q\n tr-l\u2261 idp q = idp\n\nmodule _ {A : \u2605}(f g : A \u2192 \u2605){x y : A}(p : x \u2261 y)(h : f x \u2192 g x) where\n tr-\u2192 : tr (\u03bb x \u2192 f x \u2192 g x) p h \u2261 (\u03bb x \u2192 tr g p (h (tr f (! p) x)))\n tr-\u2192 = J' (\u03bb x y p \u2192 (h : f x \u2192 g x) \u2192 tr (\u03bb x \u2192 f x \u2192 g x) p h \u2261 (\u03bb x \u2192 tr g p (h (tr f (! p) x))))\n (\u03bb _ _ \u2192 idp) p h\n\nmodule _ {a}{b}{A : \u2605_ a}{B : \u2605_ b} where\n pair\u00d7= : \u2200 {x x' : A}(p : x \u2261 x')\n {y y' : B}(q : y \u2261 y')\n \u2192 (x , y) \u2261 (x' , y')\n pair\u00d7= idp q = snd= q\n\nmodule _ {a b c}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{x\u2080 : A}{y\u2080 : B x\u2080}{C : \u2605_ c}\n (f : (x : A) (y : B x) \u2192 C) where\n ap\u2082\u2193 : {x\u2081 : A}(x= : x\u2080 \u2261 x\u2081)\n {y\u2081 : B x\u2081}(y= : y\u2080 \u2261 y\u2081 [ B \u2193 x= ])\n \u2192 f x\u2080 y\u2080 \u2261 f x\u2081 y\u2081\n ap\u2082\u2193 idp = ap (f x\u2080)\n {- Or with J\n ap\u2082\u2193 x= = J (\u03bb x\u2081' x=' \u2192 {y\u2081 : B x\u2081'}(y= : y\u2080 \u2261 y\u2081 [ _ \u2193 x=' ])\n \u2192 f x\u2080 y\u2080 \u2261 f x\u2081' y\u2081)\n (\u03bb y= \u2192 ap (f x\u2080) y=) x=\n -- -}\n\n apd\u2082 : {x\u2081 : A}(x= : x\u2080 \u2261 x\u2081)\n {y\u2081 : B x\u2081}(y= : tr B x= y\u2080 \u2261 y\u2081)\n \u2192 f x\u2080 y\u2080 \u2261 f x\u2081 y\u2081\n -- apd\u2082 idp = ap (f x\u2080)\n -- {- Or with J\n apd\u2082 x= = J (\u03bb x\u2081' x=' \u2192 {y\u2081 : B x\u2081'}(y= : tr B x=' y\u2080 \u2261 y\u2081) \u2192 f x\u2080 y\u2080 \u2261 f x\u2081' y\u2081)\n (\u03bb y= \u2192 ap (f x\u2080) y=) x=\n -- -}\n\nmodule _ {a b c d}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{C : \u2605_ c}{x\u2080 : A}{y\u2080 : B x\u2080 \u2192 C}{D : \u2605_ d}\n {{_ : FunExt}}\n (f : (x : A) (y : B x \u2192 C) \u2192 D) where\n apd\u2082\u207b : {x\u2081 : A}(x= : x\u2080 \u2261 x\u2081)\n {y\u2081 : B x\u2081 \u2192 C}(y= : \u2200 x \u2192 y\u2080 x \u2261 y\u2081 (tr B x= x))\n \u2192 f x\u2080 y\u2080 \u2261 f x\u2081 y\u2081\n apd\u2082\u207b idp y= = ap (f x\u2080) (\u03bb= y=)\n\nmodule Equivalences where\n\n module _ {a b}{A : \u2605_ a}{B : \u2605_ b} where\n _LeftInverseOf_ : (B \u2192 A) \u2192 (A \u2192 B) \u2192 \u2605_ a\n linv LeftInverseOf f = \u2200 x \u2192 linv (f x) \u2261 x\n\n _RightInverseOf_ : (B \u2192 A) \u2192 (A \u2192 B) \u2192 \u2605_ b\n rinv RightInverseOf f = \u2200 x \u2192 f (rinv x) \u2261 x\n\n record Linv (f : A \u2192 B) : \u2605_(a \u2294 b) where\n field\n linv : B \u2192 A\n is-linv : \u2200 x \u2192 linv (f x) \u2261 x\n\n injective : \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y\n injective p = ! is-linv _ \u2219 ap linv p \u2219 is-linv _\n\n record Rinv (f : A \u2192 B) : \u2605_(a \u2294 b) where\n field\n rinv : B \u2192 A\n is-rinv : \u2200 x \u2192 f (rinv x) \u2261 x\n\n surjective : \u2200 y \u2192 \u2203 \u03bb x \u2192 f x \u2261 y\n surjective y = rinv y , is-rinv y\n\n record Biinv (f : A \u2192 B) : \u2605_(a \u2294 b) where\n field\n has-linv : Linv f\n has-rinv : Rinv f\n\n open Linv has-linv public\n open Rinv has-rinv public\n\n module _ {f : A \u2192 B}\n (g : B \u2192 A)(g-f : (x : A) \u2192 g (f x) \u2261 x)\n (h : B \u2192 A)(f-h : (y : B) \u2192 f (h y) \u2261 y) where\n biinv : Biinv f\n biinv = record { has-linv = record { linv = g ; is-linv = g-f }\n ; has-rinv = record { rinv = h ; is-rinv = f-h } }\n\n record Qinv (f : A \u2192 B) : \u2605_(a \u2294 b) where\n field\n inv : B \u2192 A\n inv-is-linv : \u2200 x \u2192 inv (f x) \u2261 x\n inv-is-rinv : \u2200 x \u2192 f (inv x) \u2261 x\n\n has-biinv : Biinv f\n has-biinv = record { has-linv = record { linv = inv\n ; is-linv = inv-is-linv }\n ; has-rinv = record { rinv = inv\n ; is-rinv = inv-is-rinv } }\n\n open Biinv has-biinv public\n\n HAE : {f : A \u2192 B} \u2192 Qinv f \u2192 \u2605_(a \u2294 b)\n HAE {f} f-qinv = \u2200 x \u2192 ap f (F.inv-is-linv x) \u2261 F.inv-is-rinv (f x)\n where module F = Qinv f-qinv\n\n record Is-equiv (f : A \u2192 B) : \u2605_(a \u2294 b) where\n field\n has-qinv : Qinv f\n is-hae : HAE has-qinv\n open Qinv has-qinv public\n\n module _ {f : A \u2192 B}(g : B \u2192 A)\n (f-g : (y : B) \u2192 f (g y) \u2261 y)\n (g-f : (x : A) \u2192 g (f x) \u2261 x) where\n qinv : Qinv f\n qinv = record\n { inv = g\n ; inv-is-linv = g-f\n ; inv-is-rinv = f-g }\n\n module _ {f : A \u2192 B}(g : B \u2192 A)\n (f-g : (y : B) \u2192 f (g y) \u2261 y)\n (g-f : (x : A) \u2192 g (f x) \u2261 x) where\n f-g' : (x : B) \u2192 f (g x) \u2261 x\n f-g' x = ! ap (f \u2218 g) (f-g x) \u2219 ap f (g-f (g x)) \u2219 f-g x\n -- g-f' x = ap g {!f-g ?!} \u2219 {!!}\n\n postulate hae : HAE (qinv g f-g' g-f)\n\n is-equiv : Is-equiv f\n is-equiv = record\n { has-qinv = qinv g f-g' g-f\n ; is-hae = hae }\n\n module Biinv-inv {a b}{A : \u2605_ a}{B : \u2605_ b}{f : A \u2192 B}\n (f\u1d2e : Biinv f) where\n open Biinv f\u1d2e\n inv : B \u2192 A\n inv = linv \u2218 f \u2218 rinv\n\n inv-biinv : Biinv inv\n inv-biinv =\n biinv f (\u03bb x \u2192 ap f (is-linv (rinv x)) \u2219 is-rinv x)\n f (\u03bb x \u2192 ap linv (is-rinv (f x)) \u2219 is-linv x)\n\n module _ {a b}{A : \u2605_ a}{B : \u2605_ b}{f : A \u2192 B}\n (f\u1d31 : Is-equiv f) where\n open Is-equiv f\u1d31\n\n inv-is-equiv : Is-equiv inv\n inv-is-equiv = is-equiv f is-linv is-rinv\n\n module _ {a b} where\n infix 4 _\u2243_\n _\u2243_ : \u2605_ a \u2192 \u2605_ b \u2192 \u2605_(a \u2294 b)\n A \u2243 B = \u03a3 (A \u2192 B) Is-equiv\n\n module _ {a b}{A : \u2605_ a}{B : \u2605_ b}\n (f : A \u2192 B)(g : B \u2192 A)\n (f-g : (y : B) \u2192 f (g y) \u2261 y)\n (g-f : (x : A) \u2192 g (f x) \u2261 x) where\n equiv : A \u2243 B\n equiv = f , is-equiv g f-g g-f\n\n module _ {a}{A : \u2605_ a}\n (f : A \u2192 A)(f-inv : f LeftInverseOf f) where\n self-inv-is-equiv : Is-equiv f\n self-inv-is-equiv = is-equiv f f-inv f-inv\n\n self-inv-equiv : A \u2243 A\n self-inv-equiv = f , self-inv-is-equiv\n\n self-inv-biinv : Biinv f\n self-inv-biinv = biinv f f-inv f f-inv\n\n module _ {a}{A : \u2605_ a} where\n id\u1d31 : Is-equiv {A = A} id\n id\u1d31 = self-inv-is-equiv _ \u03bb _ \u2192 idp\n\n id\u1d2e : Biinv {A = A} id\n id\u1d2e = self-inv-biinv _ \u03bb _ \u2192 idp\n\n module _ {a b c}{A : \u2605_ a}{B : \u2605_ b}{C : \u2605_ c}{g : B \u2192 C}{f : A \u2192 B} where\n _\u2218\u1d31_ : Is-equiv g \u2192 Is-equiv f \u2192 Is-equiv (g \u2218 f)\n g\u1d31 \u2218\u1d31 f\u1d31 = is-equiv (F.inv \u2218 G.inv)\n (\u03bb x \u2192 ap g (F.inv-is-rinv _) \u2219 G.inv-is-rinv _)\n (\u03bb x \u2192 ap F.inv (G.inv-is-linv _) \u2219 F.inv-is-linv _)\n where\n module G = Is-equiv g\u1d31\n module F = Is-equiv f\u1d31\n\n _\u2218\u1d2e_ : Biinv g \u2192 Biinv f \u2192 Biinv (g \u2218 f)\n g\u1d2e \u2218\u1d2e f\u1d2e =\n biinv (F.linv \u2218 G.linv)\n (\u03bb x \u2192 ap F.linv (G.is-linv (f x)) \u2219 F.is-linv x)\n (F.rinv \u2218 G.rinv)\n (\u03bb x \u2192 ap g (F.is-rinv _) \u2219 G.is-rinv x)\n where\n module G = Biinv g\u1d2e\n module F = Biinv f\u1d2e\n\n module Equiv {a b}{A : \u2605_ a}{B : \u2605_ b}(e : A \u2243 B) where\n \u00b7\u2192 : A \u2192 B\n \u00b7\u2192 = fst e\n\n open Is-equiv (snd e) public\n renaming (linv to \u00b7\u2190; rinv to \u00b7\u2190\u2032; is-linv to \u00b7\u2190-inv-l; is-rinv to \u00b7\u2190-inv-r)\n\n -- Equivalences are \"injective\"\n equiv-inj : {x y : A} \u2192 (\u00b7\u2192 x \u2261 \u00b7\u2192 y \u2192 x \u2261 y)\n equiv-inj p = ! \u00b7\u2190-inv-l _ \u2219 ap \u00b7\u2190 p \u2219 \u00b7\u2190-inv-l _\n\n \u2013> = \u00b7\u2192\n <\u2013 = \u00b7\u2190\n <\u2013' = \u00b7\u2190\u2032\n\n <\u2013-inv-l = \u00b7\u2190-inv-l\n <\u2013-inv-r = \u00b7\u2190-inv-r\n\n open Equiv public\n\n module _ {\u2113} where\n \u2243-refl : Reflexive (_\u2243_ {\u2113})\n \u2243-refl = _ , id\u1d31\n\n \u2243-sym : Symmetric (_\u2243_ {\u2113})\n \u2243-sym (_ , f\u1d31) = _ , inv-is-equiv f\u1d31\n\n \u2243-trans : Transitive (_\u2243_ {\u2113})\n \u2243-trans (_ , p) (_ , q) = _ , q \u2218\u1d31 p\n\n \u2243-! = \u2243-sym\n _\u2243-\u2219_ = \u2243-trans\n\n module _ {a}(A : \u2605_ a) where\n Paths : \u2605_ a\n Paths = \u03a3 A \u03bb x \u2192 \u03a3 A \u03bb y \u2192 x \u2261 y\n\n module _ {a}{A : \u2605_ a} where\n id-path : A \u2192 Paths A\n id-path x = x , x , idp\n\n fst-rinv-id-path : \u2200 p \u2192 id-path (fst p) \u2261 p\n fst-rinv-id-path (x , y , p) = snd= (pair= p (J (\u03bb y p \u2192 tr (_\u2261_ x) p idp \u2261 p) idp p))\n\n id-path-is-equiv : Is-equiv id-path\n id-path-is-equiv = is-equiv fst fst-rinv-id-path (\u03bb x \u2192 idp)\n\n \u2243-Paths : A \u2243 Paths A\n \u2243-Paths = id-path , id-path-is-equiv\n\n module _ {a b}{A : \u2605_ a}{B : \u2605_ b}(f : A \u2192 B) where\n hfiber : (y : B) \u2192 \u2605_(a \u2294 b)\n hfiber y = \u03a3 A \u03bb x \u2192 f x \u2261 y\n\n Is-equiv-alt : \u2605_(a \u2294 b)\n Is-equiv-alt = (y : B) \u2192 Is-contr (hfiber y)\n\n module Is-contr-to-Is-equiv {a}{A : \u2605_ a}(A-contr : Is-contr A) where\n const-is-equiv : Is-equiv (\u03bb (_ : \ud835\udfd9) \u2192 fst A-contr)\n const-is-equiv = is-equiv _ (snd A-contr) (\u03bb _ \u2192 idp)\n \ud835\udfd9\u2243 : \ud835\udfd9 \u2243 A\n \ud835\udfd9\u2243 = _ , const-is-equiv\n module Is-equiv-to-Is-contr {a}{A : \u2605_ a}(f : \ud835\udfd9 \u2192 A)(f-is-equiv : Is-equiv f) where\n open Is-equiv f-is-equiv\n A-contr : Is-contr A\n A-contr = f _ , is-rinv\n\n module _ {a}{A : \u2605_ a}{b}{B : \u2605_ b} where\n iso-to-equiv : (A \u2194 B) \u2192 (A \u2243 B)\n iso-to-equiv iso = to iso , is-equiv (from iso) (Inverse.right-inverse-of iso) (Inverse.left-inverse-of iso)\n\n equiv-to-iso : (A \u2243 B) \u2192 (A \u2194 B)\n equiv-to-iso (f , f-is-equiv) = inverses f (f\u1d31.linv \u2218 f \u2218 f\u1d31.rinv)\n (\u03bb x \u2192 ap f\u1d31.linv (f\u1d31.is-rinv (f x)) \u2219 f\u1d31.is-linv x)\n (\u03bb x \u2192 ap f (f\u1d31.is-linv (f\u1d31.rinv x)) \u2219 f\u1d31.is-rinv x)\n where module f\u1d31 = Is-equiv f-is-equiv\n\n {-\n iso-to-equiv-to-iso : (iso : A \u2194 B) \u2192 equiv-to-iso (iso-to-equiv iso) \u2261 iso\n iso-to-equiv-to-iso iso = {!!}\n where module Iso = Inverse iso\n\n iso-to-equiv-is-equiv : Is-equiv iso-to-equiv\n iso-to-equiv-is-equiv = record { linv = equiv-to-iso ; is-linv = {!!} ; rinv = {!!} ; is-rinv = {!!} }\n -}\nopen Equivalences\n\ndata T-level : \u2605\u2080 where\n \u27e8-2\u27e9 : T-level\n \u27e8S_\u27e9 : (n : T-level) \u2192 T-level\n\n\u27e8-1\u27e9 \u27e80\u27e9 : T-level\n\u27e8-1\u27e9 = \u27e8S \u27e8-2\u27e9 \u27e9\n\u27e80\u27e9 = \u27e8S \u27e8-1\u27e9 \u27e9\n\u27e81\u27e9 = \u27e8S \u27e80\u27e9 \u27e9\n\u27e82\u27e9 = \u27e8S \u27e81\u27e9 \u27e9\n\n\u2115\u208b\u2082 = T-level\n\nmodule _ {a} where\n private\n U = \u2605_ a\n\n has-level : T-level \u2192 U \u2192 U\n has-level \u27e8-2\u27e9 A = Is-contr A\n has-level \u27e8S n \u27e9 A = (x y : A) \u2192 has-level n (x \u2261 y)\n\n is-prop : U \u2192 U\n is-prop A = has-level \u27e8-1\u27e9 A\n\n is-set : U \u2192 U\n is-set A = has-level \u27e80\u27e9 A\n\n has-all-paths : U \u2192 U\n has-all-paths A = (x y : A) \u2192 x \u2261 y\n\n UIP : U \u2192 U\n UIP A = {x y : A} (p q : x \u2261 y) \u2192 p \u2261 q\n\n private\n UIP-check : {A : U} \u2192 UIP A \u2261 ({x y : A} \u2192 has-all-paths (x \u2261 y))\n UIP-check = idp\n\n module _ {A : U} where\n prop-has-all-paths : is-prop A \u2192 has-all-paths A\n prop-has-all-paths A-prop x y = fst (A-prop x y)\n\n all-paths-is-prop : has-all-paths A \u2192 is-prop A\n all-paths-is-prop c x y = c x y , canon-path\n where\n lemma : {x y : A} (p : x \u2261 y) \u2192 c x y \u2261 p \u2219 c y y\n lemma = J' (\u03bb x y p \u2192 c x y \u2261 p \u2219 c y y) (\u03bb x \u2192 idp)\n\n canon-path : {x y : A} (p : x \u2261 y) \u2192 c x y \u2261 p\n canon-path = J' (\u03bb x y p \u2192 c x y \u2261 p)\n (\u03bb x \u2192 lemma (! c x x) \u2219 !-\u2219 (c x x))\n\n\nmodule _ {a} (A : \u2605_ a) where\n has-dec-eq : \u2605_ a\n has-dec-eq = (x y : A) \u2192 Dec (x \u2261 y)\n\nmodule _ {a} {A : \u2605_ a} (d : has-dec-eq A) where\n private\n Code' : {x y : A} (dxy : Dec (x \u2261 y)) (dxx : Dec (x \u2261 x)) \u2192 x \u2261 y \u2192 \u2605_ a\n Code' {x} {y} dxy dxx p = case dxy of \u03bb\n { (no _) \u2192 Lift \ud835\udfd8\n ; (yes b) \u2192 case dxx of \u03bb\n { (no _) \u2192 Lift \ud835\udfd8\n ; (yes b') \u2192 p \u2261 ! b' \u2219 b\n }\n }\n\n Code : {x y : A} \u2192 x \u2261 y \u2192 \u2605_ a\n Code {x} {y} p = Code' (d x y) (d x x) p\n\n encode : {x y : A} \u2192 (p : x \u2261 y) -> Code p\n encode {x} = J (\u03bb y (p : x \u2261 y) \u2192 Code p) (elim-Dec (\u03bb d \u2192 Code' d d idp) (!_ \u2218 !p\u2219p) (\u03bb x\u2081 \u2192 lift (x\u2081 idp)) (d x x))\n\n UIP-dec : UIP A\n UIP-dec {x} idp q with d x x | encode q\n UIP-dec idp q | yes a | p' = ! !p\u2219p a \u2219 ! p'\n UIP-dec idp q | no r | _ = \ud835\udfd8-elim (r idp)\n\n dec-eq-is-set : is-set A\n dec-eq-is-set _ _ = all-paths-is-prop UIP-dec\n\nmodule _ {\u2113}{A : \u2605_ \u2113} where\n UIP-set : is-set A \u2192 UIP A\n UIP-set A-is-set p q = fst (A-is-set _ _ p q)\n\n UIP\u2192is-set : UIP A \u2192 is-set A\n UIP\u2192is-set A-is-set' x y = all-paths-is-prop A-is-set'\n\n \u03a9\u2081-set-to-contr : is-set A \u2192 (x : A) \u2192 Is-contr (x \u2261 x)\n \u03a9\u2081-set-to-contr A-is-set x = idp , UIP-set A-is-set idp\n\n coe!-inv-r : \u2200 {B}(p : A \u2261 B) y \u2192 coe p (coe! p y) \u2261 y\n coe!-inv-r idp y = idp\n\n coe!-inv-l : \u2200 {B}(p : A \u2261 B) x \u2192 coe! p (coe p x) \u2261 x\n coe!-inv-l idp x = idp\n\n coe-equiv : \u2200 {B} \u2192 A \u2261 B \u2192 A \u2243 B\n coe-equiv p = equiv (coe p) (coe! p) (coe!-inv-r p) (coe!-inv-l p)\n\n coe\u2218coe : \u2200 {B C}(p : B \u2261 C)(q : A \u2261 B)(m : A) \u2192 coe p (coe q m) \u2261 coe (q \u2219 p) m\n coe\u2218coe p idp m = idp\n\n coe-same : \u2200 {B}{p q : A \u2261 B}(e : p \u2261 q)(x : A) \u2192 coe p x \u2261 coe q x\n coe-same p x = ap (\u03bb X \u2192 coe X x) p\n\n coe-inj : \u2200 {B}{x y : A}(p : A \u2261 B) \u2192 coe p x \u2261 coe p y \u2192 x \u2261 y\n coe-inj idp = id\n\n module _ {B : \u2605_ \u2113}(p : A \u2261 B){x y : A} where\n coe-paths-equiv : (x \u2261 y) \u2261 (coe p x \u2261 coe p y)\n coe-paths-equiv = J (\u03bb B (p : A \u2261 B) \u2192 (x \u2261 y) \u2261 (coe p x \u2261 coe p y)) idp p\n\npostulate\n UA : \u2605\nmodule _ {\u2113}{A B : \u2605_ \u2113}{{_ : UA}} where\n postulate\n ua : (A \u2243 B) \u2192 (A \u2261 B)\n coe-equiv-\u03b2 : (e : A \u2243 B) \u2192 coe-equiv (ua e) \u2261 e\n ua-\u03b7 : (p : A \u2261 B) \u2192 ua (coe-equiv p) \u2261 p\n\n ua-equiv : (A \u2243 B) \u2243 (A \u2261 B)\n ua-equiv = equiv ua coe-equiv ua-\u03b7 coe-equiv-\u03b2\n\n coe-\u03b2 : (e : A \u2243 B) (a : A) \u2192 coe (ua e) a \u2261 \u00b7\u2192 e a\n coe-\u03b2 e a = ap (\u03bb e \u2192 \u00b7\u2192 e a) (coe-equiv-\u03b2 e)\n\n postulate\n coe!-\u03b2 : (e : A \u2243 B) (b : B) \u2192 coe! (ua e) b \u2261 \u00b7\u2190 e b\n\n module _ (e : A \u2243 B){x y : A} where\n \u00b7\u2192-paths-equiv : (x \u2261 y) \u2261 (\u00b7\u2192 e x \u2261 \u00b7\u2192 e y)\n \u00b7\u2192-paths-equiv = coe-paths-equiv (ua e) \u2219 ap\u2082 _\u2261_ (coe-\u03b2 e x) (coe-\u03b2 e y)\n\n \u2013>-paths-equiv = \u00b7\u2192-paths-equiv\n\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"e583d1acadf29bc3856d3599b8082a70b3f41ca1","subject":"Add big-lift\u2227 big-lift\u2228 lift-\u2713all-\u03a0\u1d49 lift-\u2713any\u2194\u03a3\u1d49","message":"Add big-lift\u2227 big-lift\u2228 lift-\u2713all-\u03a0\u1d49 lift-\u2713any\u2194\u03a3\u1d49\n","repos":"crypto-agda\/explore","old_file":"lib\/Explore\/Explorable.agda","new_file":"lib\/Explore\/Explorable.agda","new_contents":"{-# OPTIONS --without-K #-}\n-- Constructions on top of exploration functions\n\nopen import Level.NP\nopen import Type hiding (\u2605)\nopen import Type.Identities\nopen import Function.NP\nopen import Function.Extensionality\nopen import Algebra.FunctionProperties.NP\nopen import Data.Two.Base\nopen import Data.Indexed\nopen import Data.Nat.NP hiding (_\u2294_)\nopen import Data.Nat.Properties\nopen import Data.Fin using (Fin) renaming (zero to fzero)\nopen import Data.Maybe.NP\nopen import Algebra\nopen import Data.Product.NP renaming (map to \u00d7-map) hiding (first)\nopen import Data.Sum.NP renaming (map to \u228e-map)\nopen import Data.Zero using (\ud835\udfd8)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Tree.Binary\nimport Data.List as List\nopen List using (List; _++_)\nopen import Relation.Nullary.Decidable\nopen import Relation.Nullary.NP\nopen import Relation.Binary\nopen import Relation.Binary.Sum using (_\u228e-cong_)\nopen import Relation.Binary.Product.Pointwise using (_\u00d7-cong_)\nimport Function.Related as FR\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen import HoTT\nopen Equivalences\nopen \u2261 using (_\u2261_)\n\nopen import Explore.Core\nopen import Explore.Properties\nimport Explore.Monad as EM\n\nmodule Explore.Explorable where\n\nmodule _ {m a} {A : \u2605 a} where\n open EM {a} m\n gfilter-explore : \u2200 {B} \u2192 (A \u2192? B) \u2192 Explore m A \u2192 Explore m B\n gfilter-explore f e\u1d2c = e\u1d2c >>= \u03bb x \u2192 maybe (\u03bb \u03b7 \u2192 point-explore \u03b7) empty-explore (f x)\n\n filter-explore : (A \u2192 \ud835\udfda) \u2192 Explore m A \u2192 Explore m A\n filter-explore p = gfilter-explore \u03bb x \u2192 [0: nothing 1: just x ] (p x)\n\n -- monoidal exploration: explore A with a monoid M\n explore-monoid : \u2200 {\u2113} \u2192 Explore m A \u2192 ExploreMon m \u2113 A\n explore-monoid e\u1d2c M = e\u1d2c \u03b5 _\u00b7_ where open Mon M renaming (_\u2219_ to _\u00b7_)\n\n explore-endo : Explore m A \u2192 Explore m A\n explore-endo e\u1d2c \u03b5 op f = e\u1d2c id _\u2218\u2032_ (op \u2218 f) \u03b5\n\n explore-endo-monoid : \u2200 {\u2113} \u2192 Explore m A \u2192 ExploreMon m \u2113 A\n explore-endo-monoid = explore-monoid \u2218 explore-endo\n\n explore-backward : Explore m A \u2192 Explore m A\n explore-backward e\u1d2c \u03b5 _\u2219_ f = e\u1d2c \u03b5 (flip _\u2219_) f\n\n -- explore-backward \u2218 explore-backward = id\n -- (m : a comm monoid) \u2192 explore-backward m = explore m\n\nprivate\n module FindForward {a} {A : \u2605 a} (explore : Explore a A) where\n find? : Find? A\n find? = explore nothing (M?._\u2223_ _)\n\n first : Maybe A\n first = find? just\n\n findKey : FindKey A\n findKey pred = find? (\u03bb x \u2192 [0: nothing 1: just x ] (pred x))\n\nmodule ExplorePlug {\u2113 a} {A : \u2605 a} where\n record ExploreIndKit p (P : Explore \u2113 A \u2192 \u2605 p) : \u2605 (a \u2294 \u209b \u2113 \u2294 p) where\n constructor mk\n field\n P\u03b5 : P empty-explore\n P\u2219 : \u2200 {e\u2080 e\u2081 : Explore \u2113 A} \u2192 P e\u2080 \u2192 P e\u2081 \u2192 P (merge-explore e\u2080 e\u2081)\n Pf : \u2200 x \u2192 P (point-explore x)\n\n _$kit_ : \u2200 {p} {P : Explore \u2113 A \u2192 \u2605 p} {e : Explore \u2113 A}\n \u2192 ExploreInd p e \u2192 ExploreIndKit p P \u2192 P e\n _$kit_ {P = P} ind (mk P\u03b5 P\u2219 Pf) = ind P P\u03b5 P\u2219 Pf\n\n _,-kit_ : \u2200 {p} {P : Explore \u2113 A \u2192 \u2605 p}{Q : Explore \u2113 A \u2192 \u2605 p}\n \u2192 ExploreIndKit p P \u2192 ExploreIndKit p Q \u2192 ExploreIndKit p (P \u00d7\u00b0 Q)\n Pk ,-kit Qk = mk (P\u03b5 Pk , P\u03b5 Qk)\n (\u03bb x y \u2192 P\u2219 Pk (fst x) (fst y) , P\u2219 Qk (snd x) (snd y))\n (\u03bb x \u2192 Pf Pk x , Pf Qk x)\n where open ExploreIndKit\n\n ExploreInd-Extra : \u2200 p \u2192 Explore \u2113 A \u2192 \u2605 _\n ExploreInd-Extra p exp =\n \u2200 (Q : Explore \u2113 A \u2192 \u2605 p)\n (Q-kit : ExploreIndKit p Q)\n (P : Explore \u2113 A \u2192 \u2605 p)\n (P\u03b5 : P empty-explore)\n (P\u2219 : \u2200 {e\u2080 e\u2081 : Explore \u2113 A} \u2192 Q e\u2080 \u2192 Q e\u2081 \u2192 P e\u2080 \u2192 P e\u2081\n \u2192 P (merge-explore e\u2080 e\u2081))\n (Pf : \u2200 x \u2192 P (point-explore x))\n \u2192 P exp\n\n to-extra : \u2200 {p} {e : Explore \u2113 A} \u2192 ExploreInd p e \u2192 ExploreInd-Extra p e\n to-extra e-ind Q Q-kit P P\u03b5 P\u2219 Pf =\n snd (e-ind (Q \u00d7\u00b0 P)\n (Q\u03b5 , P\u03b5)\n (\u03bb { (a , b) (c , d) \u2192 Q\u2219 a c , P\u2219 a c b d })\n (\u03bb x \u2192 Qf x , Pf x))\n where open ExploreIndKit Q-kit renaming (P\u03b5 to Q\u03b5; P\u2219 to Q\u2219; Pf to Qf)\n\n ExplorePlug : \u2200 {m} (M : Monoid \u2113 m) (e : Explore _ A) \u2192 \u2605 _\n ExplorePlug M e = \u2200 f x \u2192 e\u2218 \u03b5 _\u2219_ f \u2219 x \u2248 e\u2218 x _\u2219_ f\n where open Mon M\n e\u2218 = explore-endo e\n\n plugKit : \u2200 {m} (M : Monoid \u2113 m) \u2192 ExploreIndKit _ (ExplorePlug M)\n plugKit M = mk (\u03bb _ \u2192 fst identity)\n (\u03bb Ps Ps' f x \u2192\n trans (\u2219-cong (! Ps _ _) refl)\n (trans (assoc _ _ _)\n (trans (\u2219-cong refl (Ps' _ x)) (Ps _ _))))\n (\u03bb x f _ \u2192 \u2219-cong (snd identity (f x)) refl)\n where open Mon M\n\nmodule FromExplore\n {a} {A : \u2605 a}\n (explore : \u2200 {\u2113} \u2192 Explore \u2113 A) where\n\n module _ {\u2113} where\n with-monoid : \u2200 {m} \u2192 ExploreMon \u2113 m A\n with-monoid = explore-monoid explore\n\n with\u2218 : Explore \u2113 A\n with\u2218 = explore-endo explore\n\n with-endo-monoid : \u2200 {m} \u2192 ExploreMon \u2113 m A\n with-endo-monoid = explore-endo-monoid explore\n\n backward : Explore \u2113 A\n backward = explore-backward explore\n\n gfilter : \u2200 {B} \u2192 (A \u2192? B) \u2192 Explore \u2113 B\n gfilter f = gfilter-explore f explore\n\n filter : (A \u2192 \ud835\udfda) \u2192 Explore \u2113 A\n filter p = filter-explore p explore\n\n sum : Sum A\n sum = explore 0 _+_\n\n Card : \u2115\n Card = sum (const 1)\n\n count : Count A\n count f = sum (\ud835\udfda\u25b9\u2115 \u2218 f)\n\n product : (A \u2192 \u2115) \u2192 \u2115\n product = explore 1 _*_\n\n big-\u2227 big-\u2228 big-xor : (A \u2192 \ud835\udfda) \u2192 \ud835\udfda\n\n big-\u2227 = explore 1\u2082 _\u2227_\n and = big-\u2227\n all = big-\u2227\n\n big-\u2228 = explore 0\u2082 _\u2228_\n or = big-\u2228\n any = big-\u2228\n\n big-xor = explore 0\u2082 _xor_\n\n big-lift\u2227 big-lift\u2228 : Level \u2192 (A \u2192 \ud835\udfda) \u2192 \ud835\udfda\n big-lift\u2227 \u2113 f = lower (explore {\u2113} (lift 1\u2082) (lift-op\u2082 _\u2227_) (lift \u2218 f))\n big-lift\u2228 \u2113 f = lower (explore {\u2113} (lift 0\u2082) (lift-op\u2082 _\u2228_) (lift \u2218 f))\n\n bin-tree : BinTree A\n bin-tree = explore empty fork leaf\n\n list : List A\n list = explore List.[] _++_ List.[_]\n\n module FindBackward = FindForward backward\n\n findLast? : Find? A\n findLast? = FindBackward.find?\n\n last : Maybe A\n last = FindBackward.first\n\n findLastKey : FindKey A\n findLastKey = FindBackward.findKey\n\n open FindForward explore public\n\nmodule FromLookup\n {a} {A : \u2605 a}\n {explore : \u2200 {\u2113} \u2192 Explore \u2113 A}\n (lookup : \u2200 {\u2113} \u2192 Lookup {\u2113} explore)\n where\n\n module CheckDec! {\u2113}{P : A \u2192 \u2605 \u2113}(decP : \u2200 x \u2192 Dec (P x)) where\n CheckDec! : \u2605 _\n CheckDec! = explore (Lift \ud835\udfd9) _\u00d7_ \u03bb x \u2192 \u2713 \u230a decP x \u230b\n\n checkDec! : {p\u2713 : CheckDec!} \u2192 \u2200 x \u2192 P x\n checkDec! {p\u2713} x = toWitness (lookup p\u2713 x)\n\nmodule FromExploreInd\n {a} {A : \u2605 a}\n {explore : \u2200 {\u2113} \u2192 Explore \u2113 A}\n (explore-ind : \u2200 {p \u2113} \u2192 ExploreInd {\u2113} p explore)\n where\n\n open FromExplore explore public\n\n module _ {\u2113 p} where\n explore-mon-ext : ExploreMonExt {\u2113} p explore\n explore-mon-ext m {f} {g} f\u2248\u00b0g = explore-ind (\u03bb s \u2192 s _ _ f \u2248 s _ _ g) refl \u2219-cong f\u2248\u00b0g\n where open Mon m\n\n explore-mono : ExploreMono {\u2113} p explore\n explore-mono _\u2286_ z\u2286 _\u2219-mono_ {f} {g} f\u2286\u00b0g =\n explore-ind (\u03bb e \u2192 e _ _ f \u2286 e _ _ g) z\u2286 _\u2219-mono_ f\u2286\u00b0g\n\n open ExplorePlug {\u2113} {a} {A}\n\n explore\u2218-plug : (M : Monoid \u2113 \u2113) \u2192 ExplorePlug M explore\n explore\u2218-plug M = explore-ind $kit plugKit M\n\n module _ (M : Monoid \u2113 \u2113)\n (open Mon M)\n (f : A \u2192 C)\n where\n explore-endo-monoid-spec\u2032 : \u2200 z \u2192 explore \u03b5 _\u2219_ f \u2219 z \u2248 explore-endo explore z _\u2219_ f\n explore-endo-monoid-spec\u2032 = explore-ind (\u03bb e \u2192 \u2200 z \u2192 e \u03b5 _\u2219_ f \u2219 z \u2248 explore-endo e z _\u2219_ f)\n (fst identity) (\u03bb P\u2080 P\u2081 z \u2192 trans (assoc _ _ _) (trans (\u2219-cong refl (P\u2081 z)) (P\u2080 _))) (\u03bb _ _ \u2192 refl)\n\n explore-endo-monoid-spec : with-monoid M f \u2248 with-endo-monoid M f\n explore-endo-monoid-spec = trans (! snd identity _) (explore-endo-monoid-spec\u2032 \u03b5)\n\n explore\u2218-ind : \u2200 (M : Monoid \u2113 \u2113) \u2192 BigOpMonInd \u2113 M (with-endo-monoid M)\n explore\u2218-ind M P P\u03b5 P\u2219 Pf P\u2248 =\n snd (explore-ind (\u03bb e \u2192 ExplorePlug M e \u00d7 P (\u03bb f \u2192 e id _\u2218\u2032_ (_\u2219_ \u2218 f) \u03b5))\n (const (fst identity) , P\u03b5)\n (\u03bb {e} {s'} Ps Ps' \u2192 ExploreIndKit.P\u2219 (plugKit M) {e} {s'} (fst Ps) (fst Ps')\n , P\u2248 (\u03bb f \u2192 fst Ps f _) (P\u2219 (snd Ps) (snd Ps')))\n (\u03bb x \u2192 ExploreIndKit.Pf (plugKit M) x\n , P\u2248 (\u03bb f \u2192 ! snd identity _) (Pf x)))\n where open Mon M\n\n explore-swap : \u2200 {b} \u2192 ExploreSwap {\u2113} p explore {b}\n explore-swap mon {e\u1d2e} e\u1d2e-\u03b5 pf f =\n explore-ind (\u03bb e \u2192 e _ _ (e\u1d2e \u2218 f) \u2248 e\u1d2e (e _ _ \u2218 flip f))\n (! e\u1d2e-\u03b5)\n (\u03bb p q \u2192 trans (\u2219-cong p q) (! pf _ _))\n (\u03bb _ \u2192 refl)\n where open Mon mon\n\n explore-\u03b5 : Explore\u03b5 {\u2113} p explore\n explore-\u03b5 M = explore-ind (\u03bb e \u2192 e \u03b5 _ (const \u03b5) \u2248 \u03b5)\n refl\n (\u03bb x\u2248\u03b5 y\u2248\u03b5 \u2192 trans (\u2219-cong x\u2248\u03b5 y\u2248\u03b5) (fst identity \u03b5))\n (\u03bb _ \u2192 refl)\n where open Mon M\n\n explore-hom : ExploreHom {\u2113} p explore\n explore-hom cm f g = explore-ind (\u03bb e \u2192 e _ _ (f \u2219\u00b0 g) \u2248 e _ _ f \u2219 e _ _ g)\n (! fst identity \u03b5)\n (\u03bb p\u2080 p\u2081 \u2192 trans (\u2219-cong p\u2080 p\u2081) (\u2219-interchange _ _ _ _))\n (\u03bb _ \u2192 refl)\n where open CMon cm\n\n explore-lin\u02e1 : ExploreLin\u02e1 {\u2113} p explore\n explore-lin\u02e1 m _\u25ce_ f k ide dist = explore-ind (\u03bb e \u2192 e \u03b5 _\u2219_ (\u03bb x \u2192 k \u25ce f x) \u2248 k \u25ce e \u03b5 _\u2219_ f) (! ide) (\u03bb x x\u2081 \u2192 trans (\u2219-cong x x\u2081) (! dist k _ _)) (\u03bb x \u2192 refl)\n where open Mon m\n\n explore-lin\u02b3 : ExploreLin\u02b3 {\u2113} p explore\n explore-lin\u02b3 m _\u25ce_ f k ide dist = explore-ind (\u03bb e \u2192 e \u03b5 _\u2219_ (\u03bb x \u2192 f x \u25ce k) \u2248 e \u03b5 _\u2219_ f \u25ce k) (! ide) (\u03bb x x\u2081 \u2192 trans (\u2219-cong x x\u2081) (! dist k _ _)) (\u03bb x \u2192 refl)\n where open Mon m\n\n module ProductMonoid\n {M : \u2605\u2080} (\u03b5\u2098 : M) (_\u2295\u2098_ : Op\u2082 M)\n {N : \u2605\u2080} (\u03b5\u2099 : N) (_\u2295\u2099_ : Op\u2082 N)\n where\n \u03b5 = (\u03b5\u2098 , \u03b5\u2099)\n _\u2295_ : Op\u2082 (M \u00d7 N)\n (x\u2098 , x\u2099) \u2295 (y\u2098 , y\u2099) = (x\u2098 \u2295\u2098 y\u2098 , x\u2099 \u2295\u2099 y\u2099)\n\n explore-product-monoid :\n \u2200 f\u2098 f\u2099 \u2192 explore \u03b5 _\u2295_ < f\u2098 , f\u2099 > \u2261 (explore \u03b5\u2098 _\u2295\u2098_ f\u2098 , explore \u03b5\u2099 _\u2295\u2099_ f\u2099)\n explore-product-monoid f\u2098 f\u2099 =\n explore-ind (\u03bb e \u2192 e \u03b5 _\u2295_ < f\u2098 , f\u2099 > \u2261 (e \u03b5\u2098 _\u2295\u2098_ f\u2098 , e \u03b5\u2099 _\u2295\u2099_ f\u2099)) \u2261.refl (\u2261.ap\u2082 _\u2295_) (\u03bb _ \u2192 \u2261.refl)\n {-\n empty-explore:\n \u03b5 \u2261 (\u03b5\u2098 , \u03b5\u2099) \u2713\n point-explore (x , y):\n < f\u2098 , f\u2099 > (x , y) \u2261 (f\u2098 x , f\u2099 y) \u2713\n merge-explore e\u2080 e\u2081:\n e\u2080 \u03b5 _\u2295_ < f\u2098 , f\u2099 > \u2295 e\u2081 \u03b5 _\u2295_ < f\u2098 , f\u2099 >\n \u2261\n (e\u2080 \u03b5\u2098 _\u2295\u2098_ f\u2098 , e\u2080 \u03b5\u2099 _\u2295\u2099_ f\u2099) \u2295 (e\u2081 \u03b5\u2098 _\u2295\u2098_ f\u2098 , e\u2081 \u03b5\u2099 _\u2295\u2099_ f\u2099)\n \u2261\n (e\u2080 \u03b5\u2098 _\u2295\u2098_ f\u2098 \u2295 e\u2081 \u03b5\u2098 _\u2295\u2098_ f\u2098 , e\u2080 \u03b5\u2099 _\u2295\u2099_ f\u2099 \u2295 e\u2081 \u03b5\u2099 _\u2295\u2099_ f\u2099)\n -}\n\n module _ {\u2113} where\n reify : Reify {\u2113} explore\n reify = explore-ind (\u03bb e\u1d2c \u2192 \u03a0\u1d49 e\u1d2c _) _ _,_\n\n unfocus : Unfocus {\u2113} explore\n unfocus = explore-ind Unfocus (\u03bb{ (lift ()) }) (\u03bb P Q \u2192 [ P , Q ]) (\u03bb \u03b7 \u2192 _,_ \u03b7)\n\n module _ {\u2113\u1d63 a\u1d63} {A\u1d63 : A \u2192 A \u2192 \u2605 a\u1d63}\n (A\u1d63-refl : Reflexive A\u1d63) where\n \u27e6explore\u27e7 : \u27e6Explore\u27e7 \u2113\u1d63 A\u1d63 (explore {\u2113}) (explore {\u2113})\n \u27e6explore\u27e7 M\u1d63 z\u1d63 \u2219\u1d63 f\u1d63 = explore-ind (\u03bb e \u2192 M\u1d63 (e _ _ _) (e _ _ _)) z\u1d63 (\u03bb \u03b7 \u2192 \u2219\u1d63 \u03b7) (\u03bb \u03b7 \u2192 f\u1d63 A\u1d63-refl)\n\n explore-ext : ExploreExt {\u2113} explore\n explore-ext \u03b5 op = explore-ind (\u03bb e \u2192 e _ _ _ \u2261 e _ _ _) \u2261.refl (\u2261.ap\u2082 op)\n\n module LiftHom\n {m p}\n {S T : \u2605 m}\n (_\u2248_ : T \u2192 T \u2192 \u2605 p)\n (\u2248-refl : Reflexive _\u2248_)\n (\u2248-trans : Transitive _\u2248_)\n (zero : S)\n (_+_ : Op\u2082 S)\n (one : T)\n (_*_ : Op\u2082 T)\n (\u2248-cong-* : _*_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_)\n (f : S \u2192 T)\n (g : A \u2192 S)\n (hom-0-1 : f zero \u2248 one)\n (hom-+-* : \u2200 {x y} \u2192 (f (x + y)) \u2248 (f x * f y))\n where\n\n lift-hom : f (explore zero _+_ g) \u2248 explore one _*_ (f \u2218 g)\n lift-hom = explore-ind (\u03bb e \u2192 f (e zero _+_ g) \u2248 e one _*_ (f \u2218 g))\n hom-0-1\n (\u03bb p q \u2192 \u2248-trans hom-+-* (\u2248-cong-* p q))\n (\u03bb _ \u2192 \u2248-refl)\n\n module _ {\u2113} {P : A \u2192 \u2605_ \u2113} where\n open LiftHom {S = \u2605_ \u2113} {\u2605_ \u2113} (\u03bb A B \u2192 B \u2192 A) id _\u2218\u2032_\n (Lift \ud835\udfd8) _\u228e_ (Lift \ud835\udfd9) _\u00d7_\n (\u03bb f g \u2192 \u00d7-map f g) Dec P (const (no (\u03bb{ (lift ()) })))\n (uncurry Dec-\u228e)\n public renaming (lift-hom to lift-Dec)\n\n module FromFocus {p} (focus : Focus {p} explore) where\n Dec-\u03a3 : \u2200 {P} \u2192 \u03a0 A (Dec \u2218 P) \u2192 Dec (\u03a3 A P)\n Dec-\u03a3 = map-Dec unfocus focus \u2218 lift-Dec \u2218 reify\n\n lift-hom-\u2261 :\n \u2200 {m} {S T : \u2605 m}\n (zero : S)\n (_+_ : Op\u2082 S)\n (one : T)\n (_*_ : Op\u2082 T)\n (f : S \u2192 T)\n (g : A \u2192 S)\n (hom-0-1 : f zero \u2261 one)\n (hom-+-* : \u2200 {x y} \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (explore zero _+_ g) \u2261 explore one _*_ (f \u2218 g)\n lift-hom-\u2261 z _+_ o _*_ = LiftHom.lift-hom _\u2261_ \u2261.refl \u2261.trans z _+_ o _*_ (\u2261.ap\u2082 _*_)\n\n -- Since so far S and T should have the same level, we get this mess of resizing\n -- There is a later version based on \u27e6explore\u27e7.\n module _ (f : A \u2192 \ud835\udfda) {{_ : UA}} where\n lift-\u2713all-\u03a0\u1d49 : \u2713 (big-lift\u2227 \u2081 f) \u2261 \u03a0\u1d49 explore (\u2713 \u2218 f)\n lift-\u2713all-\u03a0\u1d49 = lift-hom-\u2261 (lift 1\u2082) (lift-op\u2082 _\u2227_) (Lift \ud835\udfd9) _\u00d7_ (\u2713 \u2218 lower) (lift \u2218 f) (\u2261.! Lift\u2261id) (\u2713-\u2227-\u00d7 _ _)\n\n module _ (f : A \u2192 \ud835\udfda) where\n lift-\u2713any\u2194\u03a3\u1d49 : \u2713 (big-lift\u2228 \u2081 f) \u2194 \u03a3\u1d49 explore (\u2713 \u2218 f)\n lift-\u2713any\u2194\u03a3\u1d49 = LiftHom.lift-hom _\u2194_ (id , id) (zip (flip _\u2218\u2032_) _\u2218\u2032_)\n (lift 0\u2082) (lift-op\u2082 _\u2228_) (Lift \ud835\udfd8) _\u228e_\n (zip \u228e-map \u228e-map) (\u2713 \u2218 lower) (lift \u2218 f)\n ((\u03bb()) , \u03bb{(lift())}) (\u2713\u2228-\u228e , \u228e-\u2713\u2228)\n\n sum-ind : SumInd sum\n sum-ind P P0 P+ Pf = explore-ind (\u03bb e \u2192 P (e 0 _+_)) P0 P+ Pf\n\n sum-ext : SumExt sum\n sum-ext = explore-ext 0 _+_\n\n sum-zero : SumZero sum\n sum-zero = explore-\u03b5 \u2115+.monoid\n\n sum-hom : SumHom sum\n sum-hom = explore-hom \u2115\u00b0.+-commutativeMonoid\n\n sum-mono : SumMono sum\n sum-mono = explore-mono _\u2264_ z\u2264n _+-mono_\n\n sum-swap' : SumSwap sum\n sum-swap' {sum\u1d2e = s\u1d2e} s\u1d2e-0 hom f =\n sum-ind (\u03bb s \u2192 s (s\u1d2e \u2218 f) \u2261 s\u1d2e (s \u2218 flip f))\n (! s\u1d2e-0)\n (\u03bb p q \u2192 (ap\u2082 _+_ p q) \u2219 (! hom _ _)) (\u03bb _ \u2192 refl)\n where open \u2261\n\n sum-lin : SumLin sum\n sum-lin f zero = sum-zero\n sum-lin f (suc k) = \u2261.trans (sum-hom f (\u03bb x \u2192 k * f x)) (\u2261.ap\u2082 _+_ (\u2261.refl {x = sum f}) (sum-lin f k))\n\n sum-const : SumConst sum\n sum-const x = sum-ext (\u03bb _ \u2192 ! snd \u2115\u00b0.*-identity x) \u2219 sum-lin (const 1) x \u2219 \u2115\u00b0.*-comm x Card\n where open \u2261\n\n exploreStableUnder\u2192sumStableUnder : \u2200 {p} \u2192 StableUnder explore p \u2192 SumStableUnder sum p\n exploreStableUnder\u2192sumStableUnder SU-p = SU-p 0 _+_\n\n count-ext : CountExt count\n count-ext f\u2257g = sum-ext (\u2261.cong \ud835\udfda\u25b9\u2115 \u2218 f\u2257g)\n\n sumStableUnder\u2192countStableUnder : \u2200 {p} \u2192 SumStableUnder sum p \u2192 CountStableUnder count p\n sumStableUnder\u2192countStableUnder sumSU-p f = sumSU-p (\ud835\udfda\u25b9\u2115 \u2218 f)\n\n diff-list = with-endo-monoid (List.monoid A) List.[_]\n\n {-\n list\u2261diff-list : list \u2261 diff-list\n list\u2261diff-list = {!explore-endo-monoid-spec (List.monoid A) List.[_]!}\n -}\n\n lift-sum : \u2200 \u2113 \u2192 Sum A\n lift-sum \u2113 f = lower {\u2080} {\u2113} (explore (lift 0) (lift-op\u2082 _+_) (lift \u2218 f))\n\n Fin-lower-sum\u2261\u03a3\u1d49-Fin : \u2200 {{_ : UA}}(f : A \u2192 \u2115) \u2192 Fin (lift-sum _ f) \u2261 \u03a3\u1d49 explore (Fin \u2218 f)\n Fin-lower-sum\u2261\u03a3\u1d49-Fin f = lift-hom-\u2261 (lift 0) (lift-op\u2082 _+_) (Lift \ud835\udfd8) _\u228e_ (Fin \u2218 lower) (lift \u2218 f) (Fin0\u2261\ud835\udfd8 \u2219 ! Lift\u2261id) (! Fin-\u228e-+)\n where open \u2261\n\nmodule FromTwoExploreInd\n {a} {A : \u2605 a}\n {e\u1d2c : \u2200 {\u2113} \u2192 Explore \u2113 A}\n (e\u1d2c-ind : \u2200 {p \u2113} \u2192 ExploreInd {\u2113} p e\u1d2c)\n {b} {B : \u2605 b}\n {e\u1d2e : \u2200 {\u2113} \u2192 Explore \u2113 B}\n (e\u1d2e-ind : \u2200 {p \u2113} \u2192 ExploreInd {\u2113} p e\u1d2e)\n where\n\n module A = FromExploreInd e\u1d2c-ind\n module B = FromExploreInd e\u1d2e-ind\n\n module _ {c \u2113}(cm : CommutativeMonoid c \u2113) where\n open CMon cm\n\n op\u1d2c = e\u1d2c \u03b5 _\u2219_\n op\u1d2e = e\u1d2e \u03b5 _\u2219_\n\n -- TODO use lift-hom\n explore-swap' : \u2200 f \u2192 op\u1d2c (op\u1d2e \u2218 f) \u2248 op\u1d2e (op\u1d2c \u2218 flip f)\n explore-swap' = A.explore-swap m (B.explore-\u03b5 m) (B.explore-hom cm)\n\n sum-swap : \u2200 f \u2192 A.sum (B.sum \u2218 f) \u2261 B.sum (A.sum \u2218 flip f)\n sum-swap = explore-swap' \u2115\u00b0.+-commutativeMonoid\n\nmodule FromTwoAdequate-sum\n {{_ : UA}}{{_ : FunExt}}\n {A}{B}\n {sum\u1d2c : Sum A}{sum\u1d2e : Sum B}\n (open Adequacy _\u2261_)\n (sum\u1d2c-adq : Adequate-sum sum\u1d2c)\n (sum\u1d2e-adq : Adequate-sum sum\u1d2e) where\n\n open \u2261\n sumStableUnder : (p : A \u2243 B)(f : B \u2192 \u2115)\n \u2192 sum\u1d2c (f \u2218 \u00b7\u2192 p) \u2261 sum\u1d2e f\n sumStableUnder p f = Fin-injective (sum\u1d2c-adq (f \u2218 \u00b7\u2192 p)\n \u2219 \u03a3-fst\u2243 p _\n \u2219 ! sum\u1d2e-adq f)\n\n sumStableUnder\u2032 : (p : A \u2243 B)(f : A \u2192 \u2115)\n \u2192 sum\u1d2c f \u2261 sum\u1d2e (f \u2218 <\u2013 p)\n sumStableUnder\u2032 p f = Fin-injective (sum\u1d2c-adq f\n \u2219 \u03a3-fst\u2243\u2032 p _\n \u2219 ! sum\u1d2e-adq (f \u2218 <\u2013 p))\n\nmodule FromAdequate-sum\n {A}\n {sum : Sum A}\n (open Adequacy _\u2261_)\n (sum-adq : Adequate-sum sum)\n {{_ : UA}}{{_ : FunExt}}\n where\n\n open FromTwoAdequate-sum sum-adq sum-adq public\n open \u2261\n\n sum-ext : SumExt sum\n sum-ext = ap sum \u2218 \u03bb=\n\n private\n count : Count A\n count f = sum (\ud835\udfda\u25b9\u2115 \u2218 f)\n\n private\n module M {p q : A \u2192 \ud835\udfda}(same-count : count p \u2261 count q) where\n private\n\n P = \u03bb x \u2192 p x \u2261 1\u2082\n Q = \u03bb x \u2192 q x \u2261 1\u2082\n \u00acP = \u03bb x \u2192 p x \u2261 0\u2082\n \u00acQ = \u03bb x \u2192 q x \u2261 0\u2082\n\n \u03c0 : \u03a3 A P \u2261 \u03a3 A Q\n \u03c0 = ! \u03a3=\u2032 _ (count-\u2261 p)\n \u2219 ! (sum-adq (\ud835\udfda\u25b9\u2115 \u2218 p))\n \u2219 ap Fin same-count\n \u2219 sum-adq (\ud835\udfda\u25b9\u2115 \u2218 q)\n \u2219 \u03a3=\u2032 _ (count-\u2261 q)\n\n lem1 : \u2200 px qx \u2192 \ud835\udfda\u25b9\u2115 qx \u2261 (\ud835\udfda\u25b9\u2115 (px \u2227 qx)) + \ud835\udfda\u25b9\u2115 (not px) * \ud835\udfda\u25b9\u2115 qx\n lem1 1\u2082 1\u2082 = \u2261.refl\n lem1 1\u2082 0\u2082 = \u2261.refl\n lem1 0\u2082 1\u2082 = \u2261.refl\n lem1 0\u2082 0\u2082 = \u2261.refl\n\n lem2 : \u2200 px qx \u2192 \ud835\udfda\u25b9\u2115 px \u2261 (\ud835\udfda\u25b9\u2115 (px \u2227 qx)) + \ud835\udfda\u25b9\u2115 px * \ud835\udfda\u25b9\u2115 (not qx)\n lem2 1\u2082 1\u2082 = \u2261.refl\n lem2 1\u2082 0\u2082 = \u2261.refl\n lem2 0\u2082 1\u2082 = \u2261.refl\n lem2 0\u2082 0\u2082 = \u2261.refl\n\n lemma1 : \u2200 px qx \u2192 (qx \u2261 1\u2082) \u2261 (Fin (\ud835\udfda\u25b9\u2115 (px \u2227 qx)) \u228e (px \u2261 0\u2082 \u00d7 qx \u2261 1\u2082))\n lemma1 px qx = ! Fin-\u2261-\u22611\u2082 qx\n \u2219 ap Fin (lem1 px qx)\n \u2219 ! Fin-\u228e-+\n \u2219 \u228e= refl (! Fin-\u00d7-* \u2219 \u00d7= (Fin-\u2261-\u22610\u2082 px) (Fin-\u2261-\u22611\u2082 qx))\n\n lemma2 : \u2200 px qx \u2192 (Fin (\ud835\udfda\u25b9\u2115 (px \u2227 qx)) \u228e (px \u2261 1\u2082 \u00d7 qx \u2261 0\u2082)) \u2261 (px \u2261 1\u2082)\n lemma2 px qx = ! \u228e= refl (! Fin-\u00d7-* \u2219 \u00d7= (Fin-\u2261-\u22611\u2082 px) (Fin-\u2261-\u22610\u2082 qx)) \u2219 Fin-\u228e-+ \u2219 ap Fin (! lem2 px qx) \u2219 Fin-\u2261-\u22611\u2082 px\n\n \u03c0' : (Fin (sum (\u03bb x \u2192 \ud835\udfda\u25b9\u2115 (p x \u2227 q x))) \u228e \u03a3 A (\u03bb x \u2192 P x \u00d7 \u00acQ x))\n \u2261 (Fin (sum (\u03bb x \u2192 \ud835\udfda\u25b9\u2115 (p x \u2227 q x))) \u228e \u03a3 A (\u03bb x \u2192 \u00acP x \u00d7 Q x))\n \u03c0' = \u228e= (sum-adq (\u03bb x \u2192 \ud835\udfda\u25b9\u2115 (p x \u2227 q x))) refl\n \u2219 ! \u03a3\u228e-split\n \u2219 \u03a3=\u2032 _ (\u03bb x \u2192 lemma2 (p x) (q x))\n \u2219 \u03c0\n \u2219 \u03a3=\u2032 _ (\u03bb x \u2192 lemma1 (p x) (q x))\n \u2219 \u03a3\u228e-split\n \u2219 ! \u228e= (sum-adq (\u03bb x \u2192 \ud835\udfda\u25b9\u2115 (p x \u2227 q x))) refl\n\n \u03c0'' : \u03a3 A (P \u00d7\u00b0 \u00acQ) \u2261 \u03a3 A (\u00acP \u00d7\u00b0 Q)\n \u03c0'' = Fin\u228e-injective (sum (\u03bb x \u2192 \ud835\udfda\u25b9\u2115 (p x \u2227 q x))) \u03c0'\n\n open EquivalentSubsets \u03c0'' public\n\n same-count\u2192iso : \u2200{p q : A \u2192 \ud835\udfda}(same-count : count p \u2261 count q) \u2192 p \u2261 q \u2218 M.\u03c0 {p} {q} same-count\n same-count\u2192iso {p} {q} sc = M.prop {p} {q} sc\n\nmodule From\u27e6Explore\u27e7\n {-a-} {A : \u2605\u2080 {- a-}}\n {explore : \u2200 {\u2113} \u2192 Explore \u2113 A}\n (\u27e6explore\u27e7 : \u2200 {\u2113\u2080 \u2113\u2081} \u2113\u1d63 \u2192 \u27e6Explore\u27e7 {\u2113\u2080} {\u2113\u2081} \u2113\u1d63 _\u2261_ explore explore)\n {{_ : UA}}\n where\n open FromExplore explore\n\n module AlsoInFromExploreInd\n {\u2113}(M : Monoid \u2113 \u2113)\n (open Mon M)\n (f : A \u2192 C)\n where\n explore-endo-monoid-spec\u2032 : \u2200 z \u2192 explore \u03b5 _\u2219_ f \u2219 z \u2248 explore-endo explore z _\u2219_ f\n explore-endo-monoid-spec\u2032 = \u27e6explore\u27e7 \u2080 {C} {C \u2192 C}\n (\u03bb r s \u2192 \u2200 z \u2192 r \u2219 z \u2248 s z)\n (fst identity)\n (\u03bb P\u2080 P\u2081 z \u2192 trans (assoc _ _ _) (trans (\u2219-cong refl (P\u2081 z)) (P\u2080 _)))\n (\u03bb x\u1d63 _ \u2192 \u2219-cong (reflexive (\u2261.ap f x\u1d63)) refl)\n\n explore-endo-monoid-spec : with-monoid M f \u2248 with-endo-monoid M f\n explore-endo-monoid-spec = trans (! snd identity _) (explore-endo-monoid-spec\u2032 \u03b5)\n\n open \u2261\n module _ (f : A \u2192 \u2115) where\n sum\u21d2\u03a3\u1d49 : Fin (explore 0 _+_ f) \u2261 explore (Lift \ud835\udfd8) _\u228e_ (Fin \u2218 f)\n sum\u21d2\u03a3\u1d49 = \u27e6explore\u27e7 {\u2080} {\u2081} \u2081\n (\u03bb n X \u2192 Fin n \u2261 X)\n (Fin0\u2261\ud835\udfd8 \u2219 ! Lift\u2261id)\n (\u03bb p q \u2192 ! Fin-\u228e-+ \u2219 \u228e= p q)\n (ap (Fin \u2218 f))\n\n product\u21d2\u03a0\u1d49 : Fin (explore 1 _*_ f) \u2261 explore (Lift \ud835\udfd9) _\u00d7_ (Fin \u2218 f)\n product\u21d2\u03a0\u1d49 = \u27e6explore\u27e7 {\u2080} {\u2081} \u2081\n (\u03bb n X \u2192 Fin n \u2261 X)\n (Fin1\u2261\ud835\udfd9 \u2219 ! Lift\u2261id)\n (\u03bb p q \u2192 ! Fin-\u00d7-* \u2219 \u00d7= p q)\n (ap (Fin \u2218 f))\n\n module _ (f : A \u2192 \ud835\udfda) where\n \u2713all-\u03a0\u1d49 : \u2713 (all f) \u2261 \u03a0\u1d49 explore (\u2713 \u2218 f)\n \u2713all-\u03a0\u1d49 = \u27e6explore\u27e7 {\u2080} {\u2081} \u2081\n (\u03bb b X \u2192 \u2713 b \u2261 X)\n (! Lift\u2261id)\n (\u03bb p q \u2192 \u2713-\u2227-\u00d7 _ _ \u2219 \u00d7= p q)\n (ap (\u2713 \u2218 f))\n\n \u2713any\u2192\u03a3\u1d49 : \u2713 (any f) \u2192 \u03a3\u1d49 explore (\u2713 \u2218 f)\n \u2713any\u2192\u03a3\u1d49 p = \u27e6explore\u27e7 {\u2080} {\u209b \u2080} \u2081\n (\u03bb b (X : \u2605\u2080) \u2192 Lift (\u2713 b) \u2192 X)\n (\u03bb x \u2192 lift (lower x))\n (\u03bb { {0\u2082} {x\u2081} x\u1d63 {y\u2080} {y\u2081} y\u1d63 z\u1d63 \u2192 inr (y\u1d63 z\u1d63)\n ; {1\u2082} {x\u2081} x\u1d63 {y\u2080} {y\u2081} y\u1d63 z\u1d63 \u2192 inl (x\u1d63 _) })\n (\u03bb x\u1d63 x \u2192 tr (\u2713 \u2218 f) x\u1d63 (lower x)) (lift p)\n\n module FromAdequate-\u03a3\u1d49\n (adequate-\u03a3\u1d49 : \u2200 {\u2113} \u2192 Adequate-\u03a3 {\u2113} (\u03a3\u1d49 explore))\n where\n open Adequacy\n\n adequate-sum : Adequate-sum _\u2261_ sum\n adequate-sum f = sum\u21d2\u03a3\u1d49 f \u2219 adequate-\u03a3\u1d49 (Fin \u2218 f)\n\n open FromAdequate-sum adequate-sum public\n\n adequate-any : Adequate-any -\u2192- any\n adequate-any f e = coe (adequate-\u03a3\u1d49 (\u2713 \u2218 f)) (\u2713any\u2192\u03a3\u1d49 f e)\n\n module FromAdequate-\u03a0\u1d49\n (adequate-\u03a0\u1d49 : \u2200 {\u2113} \u2192 Adequate-\u03a0 {\u2113} (\u03a0\u1d49 explore))\n where\n open Adequacy\n\n adequate-product : Adequate-product _\u2261_ product\n adequate-product f = product\u21d2\u03a0\u1d49 f \u2219 adequate-\u03a0\u1d49 (Fin \u2218 f)\n\n adequate-all : Adequate-all _\u2261_ all\n adequate-all f = \u2713all-\u03a0\u1d49 f \u2219 adequate-\u03a0\u1d49 _\n\n check! : (f : A \u2192 \ud835\udfda) {pf : \u2713 (all f)} \u2192 (\u2200 x \u2192 \u2713 (f x))\n check! f {pf} = coe (adequate-all f) pf\n\n{-\nmodule ExplorableRecord where\n record Explorable A : \u2605\u2081 where\n constructor mk\n field\n explore : Explore\u2080 A\n explore-ind : ExploreInd\u2080 explore\n\n open FromExploreInd explore-ind\n field\n adequate-sum : Adequate-sum sum\n -- adequate-product : AdequateProduct product\n\n open FromExploreInd explore-ind public\n\n open Explorable public\n\n ExploreForFun : \u2605\u2080 \u2192 \u2605\u2081\n ExploreForFun A = \u2200 {X} \u2192 Explorable X \u2192 Explorable (A \u2192 X)\n\n record Funable A : \u2605\u2082 where\n constructor _,_\n field\n explorable : Explorable A\n negative : ExploreForFun A\n\n module DistFun {A} (\u03bcA : Explorable A)\n (\u03bcA\u2192 : ExploreForFun A)\n {B} (\u03bcB : Explorable B){X}\n (_\u2248_ : X \u2192 X \u2192 \u2605 \u2080)\n (0\u2032 : X)\n (_+_ : X \u2192 X \u2192 X)\n (_*_ : X \u2192 X \u2192 X) where\n\n \u03a3\u1d2e = explore \u03bcB 0\u2032 _+_\n \u03a0' = explore \u03bcA 0\u2032 _*_\n \u03a3' = explore (\u03bcA\u2192 \u03bcB) 0\u2032 _+_\n\n DistFun = \u2200 f \u2192 \u03a0' (\u03a3\u1d2e \u2218 f) \u2248 \u03a3' (\u03a0' \u2218 _\u02e2_ f)\n\n DistFun : \u2200 {A} \u2192 Explorable A \u2192 ExploreForFun A \u2192 \u2605\u2081\n DistFun \u03bcA \u03bcA\u2192 = \u2200 {B} (\u03bcB : Explorable B) c \u2192 let open CMon {\u2080}{\u2080} c in\n \u2200 _*_ \u2192 Zero _\u2248_ \u03b5 _*_ \u2192 _DistributesOver_ _\u2248_ _*_ _\u2219_ \u2192 _*_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_\n \u2192 DistFun.DistFun \u03bcA \u03bcA\u2192 \u03bcB _\u2248_ \u03b5 _\u2219_ _*_\n\n DistFunable : \u2200 {A} \u2192 Funable A \u2192 \u2605\u2081\n DistFunable (\u03bcA , \u03bcA\u2192) = DistFun \u03bcA \u03bcA\u2192\n\n module _ {{_ : UA}}{{_ : FunExt}} where\n \u03bc-iso : \u2200 {A B} \u2192 (A \u2243 B) \u2192 Explorable A \u2192 Explorable B\n \u03bc-iso {A}{B} A\u2243B \u03bcA = mk (EM.map _ A\u2192B (explore \u03bcA)) (EM.map-ind _ A\u2192B (explore-ind \u03bcA)) ade\n where\n open \u2261\n A\u2192B = \u2013> A\u2243B\n ade = \u03bb f \u2192 adequate-sum \u03bcA (f \u2218 A\u2192B) \u2219 \u03a3-fst\u2243 A\u2243B _\n\n -- I guess this could be more general\n \u03bc-iso-preserve : \u2200 {A B} (A\u2243B : A \u2243 B) f (\u03bcA : Explorable A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-iso A\u2243B \u03bcA) (f \u2218 <\u2013 A\u2243B)\n \u03bc-iso-preserve A\u2243B f \u03bcA = sum-ext \u03bcA (\u03bb x \u2192 ap f (! (<\u2013-inv-l A\u2243B x)))\n where open \u2261\n\n {-\n \u03bcLift : \u2200 {A} \u2192 Explorable A \u2192 Explorable (Lift A)\n \u03bcLift = \u03bc-iso {!(! Lift\u2194id)!}\n where open \u2261\n -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\n-- Constructions on top of exploration functions\n\nopen import Level.NP\nopen import Type hiding (\u2605)\nopen import Type.Identities\nopen import Function.NP\nopen import Function.Extensionality\nopen import Algebra.FunctionProperties.NP\nopen import Data.Two.Base\nopen import Data.Indexed\nopen import Data.Nat.NP hiding (_\u2294_)\nopen import Data.Nat.Properties\nopen import Data.Fin using (Fin) renaming (zero to fzero)\nopen import Data.Maybe.NP\nopen import Algebra\nopen import Data.Product.NP renaming (map to \u00d7-map) hiding (first)\nopen import Data.Sum.NP\nopen import Data.Zero using (\ud835\udfd8)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Tree.Binary\nimport Data.List as List\nopen List using (List; _++_)\nopen import Relation.Nullary.Decidable\nopen import Relation.Nullary.NP\nopen import Relation.Binary\nopen import Relation.Binary.Sum using (_\u228e-cong_)\nopen import Relation.Binary.Product.Pointwise using (_\u00d7-cong_)\nimport Function.Related as FR\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen import HoTT\nopen Equivalences\nopen \u2261 using (_\u2261_)\n\nopen import Explore.Core\nopen import Explore.Properties\nimport Explore.Monad as EM\n\nmodule Explore.Explorable where\n\nmodule _ {m a} {A : \u2605 a} where\n open EM {a} m\n gfilter-explore : \u2200 {B} \u2192 (A \u2192? B) \u2192 Explore m A \u2192 Explore m B\n gfilter-explore f e\u1d2c = e\u1d2c >>= \u03bb x \u2192 maybe (\u03bb \u03b7 \u2192 point-explore \u03b7) empty-explore (f x)\n\n filter-explore : (A \u2192 \ud835\udfda) \u2192 Explore m A \u2192 Explore m A\n filter-explore p = gfilter-explore \u03bb x \u2192 [0: nothing 1: just x ] (p x)\n\n -- monoidal exploration: explore A with a monoid M\n explore-monoid : \u2200 {\u2113} \u2192 Explore m A \u2192 ExploreMon m \u2113 A\n explore-monoid e\u1d2c M = e\u1d2c \u03b5 _\u00b7_ where open Mon M renaming (_\u2219_ to _\u00b7_)\n\n explore-endo : Explore m A \u2192 Explore m A\n explore-endo e\u1d2c \u03b5 op f = e\u1d2c id _\u2218\u2032_ (op \u2218 f) \u03b5\n\n explore-endo-monoid : \u2200 {\u2113} \u2192 Explore m A \u2192 ExploreMon m \u2113 A\n explore-endo-monoid = explore-monoid \u2218 explore-endo\n\n explore-backward : Explore m A \u2192 Explore m A\n explore-backward e\u1d2c \u03b5 _\u2219_ f = e\u1d2c \u03b5 (flip _\u2219_) f\n\n -- explore-backward \u2218 explore-backward = id\n -- (m : a comm monoid) \u2192 explore-backward m = explore m\n\nprivate\n module FindForward {a} {A : \u2605 a} (explore : Explore a A) where\n find? : Find? A\n find? = explore nothing (M?._\u2223_ _)\n\n first : Maybe A\n first = find? just\n\n findKey : FindKey A\n findKey pred = find? (\u03bb x \u2192 [0: nothing 1: just x ] (pred x))\n\nmodule ExplorePlug {\u2113 a} {A : \u2605 a} where\n record ExploreIndKit p (P : Explore \u2113 A \u2192 \u2605 p) : \u2605 (a \u2294 \u209b \u2113 \u2294 p) where\n constructor mk\n field\n P\u03b5 : P empty-explore\n P\u2219 : \u2200 {e\u2080 e\u2081 : Explore \u2113 A} \u2192 P e\u2080 \u2192 P e\u2081 \u2192 P (merge-explore e\u2080 e\u2081)\n Pf : \u2200 x \u2192 P (point-explore x)\n\n _$kit_ : \u2200 {p} {P : Explore \u2113 A \u2192 \u2605 p} {e : Explore \u2113 A}\n \u2192 ExploreInd p e \u2192 ExploreIndKit p P \u2192 P e\n _$kit_ {P = P} ind (mk P\u03b5 P\u2219 Pf) = ind P P\u03b5 P\u2219 Pf\n\n _,-kit_ : \u2200 {p} {P : Explore \u2113 A \u2192 \u2605 p}{Q : Explore \u2113 A \u2192 \u2605 p}\n \u2192 ExploreIndKit p P \u2192 ExploreIndKit p Q \u2192 ExploreIndKit p (P \u00d7\u00b0 Q)\n Pk ,-kit Qk = mk (P\u03b5 Pk , P\u03b5 Qk)\n (\u03bb x y \u2192 P\u2219 Pk (fst x) (fst y) , P\u2219 Qk (snd x) (snd y))\n (\u03bb x \u2192 Pf Pk x , Pf Qk x)\n where open ExploreIndKit\n\n ExploreInd-Extra : \u2200 p \u2192 Explore \u2113 A \u2192 \u2605 _\n ExploreInd-Extra p exp =\n \u2200 (Q : Explore \u2113 A \u2192 \u2605 p)\n (Q-kit : ExploreIndKit p Q)\n (P : Explore \u2113 A \u2192 \u2605 p)\n (P\u03b5 : P empty-explore)\n (P\u2219 : \u2200 {e\u2080 e\u2081 : Explore \u2113 A} \u2192 Q e\u2080 \u2192 Q e\u2081 \u2192 P e\u2080 \u2192 P e\u2081\n \u2192 P (merge-explore e\u2080 e\u2081))\n (Pf : \u2200 x \u2192 P (point-explore x))\n \u2192 P exp\n\n to-extra : \u2200 {p} {e : Explore \u2113 A} \u2192 ExploreInd p e \u2192 ExploreInd-Extra p e\n to-extra e-ind Q Q-kit P P\u03b5 P\u2219 Pf =\n snd (e-ind (Q \u00d7\u00b0 P)\n (Q\u03b5 , P\u03b5)\n (\u03bb { (a , b) (c , d) \u2192 Q\u2219 a c , P\u2219 a c b d })\n (\u03bb x \u2192 Qf x , Pf x))\n where open ExploreIndKit Q-kit renaming (P\u03b5 to Q\u03b5; P\u2219 to Q\u2219; Pf to Qf)\n\n ExplorePlug : \u2200 {m} (M : Monoid \u2113 m) (e : Explore _ A) \u2192 \u2605 _\n ExplorePlug M e = \u2200 f x \u2192 e\u2218 \u03b5 _\u2219_ f \u2219 x \u2248 e\u2218 x _\u2219_ f\n where open Mon M\n e\u2218 = explore-endo e\n\n plugKit : \u2200 {m} (M : Monoid \u2113 m) \u2192 ExploreIndKit _ (ExplorePlug M)\n plugKit M = mk (\u03bb _ \u2192 fst identity)\n (\u03bb Ps Ps' f x \u2192\n trans (\u2219-cong (! Ps _ _) refl)\n (trans (assoc _ _ _)\n (trans (\u2219-cong refl (Ps' _ x)) (Ps _ _))))\n (\u03bb x f _ \u2192 \u2219-cong (snd identity (f x)) refl)\n where open Mon M\n\nmodule FromExplore\n {a} {A : \u2605 a}\n (explore : \u2200 {\u2113} \u2192 Explore \u2113 A) where\n\n module _ {\u2113} where\n with-monoid : \u2200 {m} \u2192 ExploreMon \u2113 m A\n with-monoid = explore-monoid explore\n\n with\u2218 : Explore \u2113 A\n with\u2218 = explore-endo explore\n\n with-endo-monoid : \u2200 {m} \u2192 ExploreMon \u2113 m A\n with-endo-monoid = explore-endo-monoid explore\n\n backward : Explore \u2113 A\n backward = explore-backward explore\n\n gfilter : \u2200 {B} \u2192 (A \u2192? B) \u2192 Explore \u2113 B\n gfilter f = gfilter-explore f explore\n\n filter : (A \u2192 \ud835\udfda) \u2192 Explore \u2113 A\n filter p = filter-explore p explore\n\n sum : Sum A\n sum = explore 0 _+_\n\n Card : \u2115\n Card = sum (const 1)\n\n count : Count A\n count f = sum (\ud835\udfda\u25b9\u2115 \u2218 f)\n\n product : (A \u2192 \u2115) \u2192 \u2115\n product = explore 1 _*_\n\n big-\u2227 and big-\u2228 or big-xor : (A \u2192 \ud835\udfda) \u2192 \ud835\udfda\n\n big-\u2227 = explore 1\u2082 _\u2227_\n and = big-\u2227\n all = big-\u2227\n\n big-\u2228 = explore 0\u2082 _\u2228_\n or = big-\u2228\n any = big-\u2228\n\n big-xor = explore 0\u2082 _xor_\n\n bin-tree : BinTree A\n bin-tree = explore empty fork leaf\n\n list : List A\n list = explore List.[] _++_ List.[_]\n\n module FindBackward = FindForward backward\n\n findLast? : Find? A\n findLast? = FindBackward.find?\n\n last : Maybe A\n last = FindBackward.first\n\n findLastKey : FindKey A\n findLastKey = FindBackward.findKey\n\n open FindForward explore public\n\nmodule FromLookup\n {a} {A : \u2605 a}\n {explore : \u2200 {\u2113} \u2192 Explore \u2113 A}\n (lookup : \u2200 {\u2113} \u2192 Lookup {\u2113} explore)\n where\n\n module CheckDec! {\u2113}{P : A \u2192 \u2605 \u2113}(decP : \u2200 x \u2192 Dec (P x)) where\n CheckDec! : \u2605 _\n CheckDec! = explore (Lift \ud835\udfd9) _\u00d7_ \u03bb x \u2192 \u2713 \u230a decP x \u230b\n\n checkDec! : {p\u2713 : CheckDec!} \u2192 \u2200 x \u2192 P x\n checkDec! {p\u2713} x = toWitness (lookup p\u2713 x)\n\nmodule FromExploreInd\n {a} {A : \u2605 a}\n {explore : \u2200 {\u2113} \u2192 Explore \u2113 A}\n (explore-ind : \u2200 {p \u2113} \u2192 ExploreInd {\u2113} p explore)\n where\n\n open FromExplore explore public\n\n module _ {\u2113 p} where\n explore-mon-ext : ExploreMonExt {\u2113} p explore\n explore-mon-ext m {f} {g} f\u2248\u00b0g = explore-ind (\u03bb s \u2192 s _ _ f \u2248 s _ _ g) refl \u2219-cong f\u2248\u00b0g\n where open Mon m\n\n explore-mono : ExploreMono {\u2113} p explore\n explore-mono _\u2286_ z\u2286 _\u2219-mono_ {f} {g} f\u2286\u00b0g =\n explore-ind (\u03bb e \u2192 e _ _ f \u2286 e _ _ g) z\u2286 _\u2219-mono_ f\u2286\u00b0g\n\n open ExplorePlug {\u2113} {a} {A}\n\n explore\u2218-plug : (M : Monoid \u2113 \u2113) \u2192 ExplorePlug M explore\n explore\u2218-plug M = explore-ind $kit plugKit M\n\n module _ (M : Monoid \u2113 \u2113)\n (open Mon M)\n (f : A \u2192 C)\n where\n explore-endo-monoid-spec\u2032 : \u2200 z \u2192 explore \u03b5 _\u2219_ f \u2219 z \u2248 explore-endo explore z _\u2219_ f\n explore-endo-monoid-spec\u2032 = explore-ind (\u03bb e \u2192 \u2200 z \u2192 e \u03b5 _\u2219_ f \u2219 z \u2248 explore-endo e z _\u2219_ f)\n (fst identity) (\u03bb P\u2080 P\u2081 z \u2192 trans (assoc _ _ _) (trans (\u2219-cong refl (P\u2081 z)) (P\u2080 _))) (\u03bb _ _ \u2192 refl)\n\n explore-endo-monoid-spec : with-monoid M f \u2248 with-endo-monoid M f\n explore-endo-monoid-spec = trans (! snd identity _) (explore-endo-monoid-spec\u2032 \u03b5)\n\n explore\u2218-ind : \u2200 (M : Monoid \u2113 \u2113) \u2192 BigOpMonInd \u2113 M (with-endo-monoid M)\n explore\u2218-ind M P P\u03b5 P\u2219 Pf P\u2248 =\n snd (explore-ind (\u03bb e \u2192 ExplorePlug M e \u00d7 P (\u03bb f \u2192 e id _\u2218\u2032_ (_\u2219_ \u2218 f) \u03b5))\n (const (fst identity) , P\u03b5)\n (\u03bb {e} {s'} Ps Ps' \u2192 ExploreIndKit.P\u2219 (plugKit M) {e} {s'} (fst Ps) (fst Ps')\n , P\u2248 (\u03bb f \u2192 fst Ps f _) (P\u2219 (snd Ps) (snd Ps')))\n (\u03bb x \u2192 ExploreIndKit.Pf (plugKit M) x\n , P\u2248 (\u03bb f \u2192 ! snd identity _) (Pf x)))\n where open Mon M\n\n explore-swap : \u2200 {b} \u2192 ExploreSwap {\u2113} p explore {b}\n explore-swap mon {e\u1d2e} e\u1d2e-\u03b5 pf f =\n explore-ind (\u03bb e \u2192 e _ _ (e\u1d2e \u2218 f) \u2248 e\u1d2e (e _ _ \u2218 flip f))\n (! e\u1d2e-\u03b5)\n (\u03bb p q \u2192 trans (\u2219-cong p q) (! pf _ _))\n (\u03bb _ \u2192 refl)\n where open Mon mon\n\n explore-\u03b5 : Explore\u03b5 {\u2113} p explore\n explore-\u03b5 M = explore-ind (\u03bb e \u2192 e \u03b5 _ (const \u03b5) \u2248 \u03b5)\n refl\n (\u03bb x\u2248\u03b5 y\u2248\u03b5 \u2192 trans (\u2219-cong x\u2248\u03b5 y\u2248\u03b5) (fst identity \u03b5))\n (\u03bb _ \u2192 refl)\n where open Mon M\n\n explore-hom : ExploreHom {\u2113} p explore\n explore-hom cm f g = explore-ind (\u03bb e \u2192 e _ _ (f \u2219\u00b0 g) \u2248 e _ _ f \u2219 e _ _ g)\n (! fst identity \u03b5)\n (\u03bb p\u2080 p\u2081 \u2192 trans (\u2219-cong p\u2080 p\u2081) (\u2219-interchange _ _ _ _))\n (\u03bb _ \u2192 refl)\n where open CMon cm\n\n explore-lin\u02e1 : ExploreLin\u02e1 {\u2113} p explore\n explore-lin\u02e1 m _\u25ce_ f k ide dist = explore-ind (\u03bb e \u2192 e \u03b5 _\u2219_ (\u03bb x \u2192 k \u25ce f x) \u2248 k \u25ce e \u03b5 _\u2219_ f) (! ide) (\u03bb x x\u2081 \u2192 trans (\u2219-cong x x\u2081) (! dist k _ _)) (\u03bb x \u2192 refl)\n where open Mon m\n\n explore-lin\u02b3 : ExploreLin\u02b3 {\u2113} p explore\n explore-lin\u02b3 m _\u25ce_ f k ide dist = explore-ind (\u03bb e \u2192 e \u03b5 _\u2219_ (\u03bb x \u2192 f x \u25ce k) \u2248 e \u03b5 _\u2219_ f \u25ce k) (! ide) (\u03bb x x\u2081 \u2192 trans (\u2219-cong x x\u2081) (! dist k _ _)) (\u03bb x \u2192 refl)\n where open Mon m\n\n module ProductMonoid\n {M : \u2605\u2080} (\u03b5\u2098 : M) (_\u2295\u2098_ : Op\u2082 M)\n {N : \u2605\u2080} (\u03b5\u2099 : N) (_\u2295\u2099_ : Op\u2082 N)\n where\n \u03b5 = (\u03b5\u2098 , \u03b5\u2099)\n _\u2295_ : Op\u2082 (M \u00d7 N)\n (x\u2098 , x\u2099) \u2295 (y\u2098 , y\u2099) = (x\u2098 \u2295\u2098 y\u2098 , x\u2099 \u2295\u2099 y\u2099)\n\n explore-product-monoid :\n \u2200 f\u2098 f\u2099 \u2192 explore \u03b5 _\u2295_ < f\u2098 , f\u2099 > \u2261 (explore \u03b5\u2098 _\u2295\u2098_ f\u2098 , explore \u03b5\u2099 _\u2295\u2099_ f\u2099)\n explore-product-monoid f\u2098 f\u2099 =\n explore-ind (\u03bb e \u2192 e \u03b5 _\u2295_ < f\u2098 , f\u2099 > \u2261 (e \u03b5\u2098 _\u2295\u2098_ f\u2098 , e \u03b5\u2099 _\u2295\u2099_ f\u2099)) \u2261.refl (\u2261.ap\u2082 _\u2295_) (\u03bb _ \u2192 \u2261.refl)\n {-\n empty-explore:\n \u03b5 \u2261 (\u03b5\u2098 , \u03b5\u2099) \u2713\n point-explore (x , y):\n < f\u2098 , f\u2099 > (x , y) \u2261 (f\u2098 x , f\u2099 y) \u2713\n merge-explore e\u2080 e\u2081:\n e\u2080 \u03b5 _\u2295_ < f\u2098 , f\u2099 > \u2295 e\u2081 \u03b5 _\u2295_ < f\u2098 , f\u2099 >\n \u2261\n (e\u2080 \u03b5\u2098 _\u2295\u2098_ f\u2098 , e\u2080 \u03b5\u2099 _\u2295\u2099_ f\u2099) \u2295 (e\u2081 \u03b5\u2098 _\u2295\u2098_ f\u2098 , e\u2081 \u03b5\u2099 _\u2295\u2099_ f\u2099)\n \u2261\n (e\u2080 \u03b5\u2098 _\u2295\u2098_ f\u2098 \u2295 e\u2081 \u03b5\u2098 _\u2295\u2098_ f\u2098 , e\u2080 \u03b5\u2099 _\u2295\u2099_ f\u2099 \u2295 e\u2081 \u03b5\u2099 _\u2295\u2099_ f\u2099)\n -}\n\n module _ {\u2113} where\n reify : Reify {\u2113} explore\n reify = explore-ind (\u03bb e\u1d2c \u2192 \u03a0\u1d49 e\u1d2c _) _ _,_\n\n unfocus : Unfocus {\u2113} explore\n unfocus = explore-ind Unfocus (\u03bb{ (lift ()) }) (\u03bb P Q \u2192 [ P , Q ]) (\u03bb \u03b7 \u2192 _,_ \u03b7)\n\n module _ {\u2113\u1d63 a\u1d63} {A\u1d63 : A \u2192 A \u2192 \u2605 a\u1d63}\n (A\u1d63-refl : Reflexive A\u1d63) where\n \u27e6explore\u27e7 : \u27e6Explore\u27e7 \u2113\u1d63 A\u1d63 (explore {\u2113}) (explore {\u2113})\n \u27e6explore\u27e7 M\u1d63 z\u1d63 \u2219\u1d63 f\u1d63 = explore-ind (\u03bb e \u2192 M\u1d63 (e _ _ _) (e _ _ _)) z\u1d63 (\u03bb \u03b7 \u2192 \u2219\u1d63 \u03b7) (\u03bb \u03b7 \u2192 f\u1d63 A\u1d63-refl)\n\n explore-ext : ExploreExt {\u2113} explore\n explore-ext \u03b5 op = explore-ind (\u03bb e \u2192 e _ _ _ \u2261 e _ _ _) \u2261.refl (\u2261.ap\u2082 op)\n\n module LiftHom\n {m p}\n {S T : \u2605 m}\n (_\u2248_ : T \u2192 T \u2192 \u2605 p)\n (\u2248-refl : Reflexive _\u2248_)\n (\u2248-trans : Transitive _\u2248_)\n (zero : S)\n (_+_ : Op\u2082 S)\n (one : T)\n (_*_ : Op\u2082 T)\n (\u2248-cong-* : _*_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_)\n (f : S \u2192 T)\n (g : A \u2192 S)\n (hom-0-1 : f zero \u2248 one)\n (hom-+-* : \u2200 {x y} \u2192 (f (x + y)) \u2248 (f x * f y))\n where\n\n lift-hom : f (explore zero _+_ g) \u2248 explore one _*_ (f \u2218 g)\n lift-hom = explore-ind (\u03bb e \u2192 f (e zero _+_ g) \u2248 e one _*_ (f \u2218 g))\n hom-0-1\n (\u03bb p q \u2192 \u2248-trans hom-+-* (\u2248-cong-* p q))\n (\u03bb _ \u2192 \u2248-refl)\n\n module _ {\u2113} {P : A \u2192 \u2605_ \u2113} where\n open LiftHom {S = \u2605_ \u2113} {\u2605_ \u2113} (\u03bb A B \u2192 B \u2192 A) id _\u2218\u2032_\n (Lift \ud835\udfd8) _\u228e_ (Lift \ud835\udfd9) _\u00d7_\n (\u03bb f g \u2192 \u00d7-map f g) Dec P (const (no (\u03bb{ (lift ()) })))\n (uncurry Dec-\u228e)\n public renaming (lift-hom to lift-Dec)\n\n module FromFocus {p} (focus : Focus {p} explore) where\n Dec-\u03a3 : \u2200 {P} \u2192 \u03a0 A (Dec \u2218 P) \u2192 Dec (\u03a3 A P)\n Dec-\u03a3 = map-Dec unfocus focus \u2218 lift-Dec \u2218 reify\n\n lift-hom-\u2261 :\n \u2200 {m} {S T : \u2605 m}\n (zero : S)\n (_+_ : Op\u2082 S)\n (one : T)\n (_*_ : Op\u2082 T)\n (f : S \u2192 T)\n (g : A \u2192 S)\n (hom-0-1 : f zero \u2261 one)\n (hom-+-* : \u2200 {x y} \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (explore zero _+_ g) \u2261 explore one _*_ (f \u2218 g)\n lift-hom-\u2261 z _+_ o _*_ = LiftHom.lift-hom _\u2261_ \u2261.refl \u2261.trans z _+_ o _*_ (\u2261.ap\u2082 _*_)\n\n sum-ind : SumInd sum\n sum-ind P P0 P+ Pf = explore-ind (\u03bb e \u2192 P (e 0 _+_)) P0 P+ Pf\n\n sum-ext : SumExt sum\n sum-ext = explore-ext 0 _+_\n\n sum-zero : SumZero sum\n sum-zero = explore-\u03b5 \u2115+.monoid\n\n sum-hom : SumHom sum\n sum-hom = explore-hom \u2115\u00b0.+-commutativeMonoid\n\n sum-mono : SumMono sum\n sum-mono = explore-mono _\u2264_ z\u2264n _+-mono_\n\n sum-swap' : SumSwap sum\n sum-swap' {sum\u1d2e = s\u1d2e} s\u1d2e-0 hom f =\n sum-ind (\u03bb s \u2192 s (s\u1d2e \u2218 f) \u2261 s\u1d2e (s \u2218 flip f))\n (! s\u1d2e-0)\n (\u03bb p q \u2192 (ap\u2082 _+_ p q) \u2219 (! hom _ _)) (\u03bb _ \u2192 refl)\n where open \u2261\n\n sum-lin : SumLin sum\n sum-lin f zero = sum-zero\n sum-lin f (suc k) = \u2261.trans (sum-hom f (\u03bb x \u2192 k * f x)) (\u2261.ap\u2082 _+_ (\u2261.refl {x = sum f}) (sum-lin f k))\n\n sum-const : SumConst sum\n sum-const x = sum-ext (\u03bb _ \u2192 ! snd \u2115\u00b0.*-identity x) \u2219 sum-lin (const 1) x \u2219 \u2115\u00b0.*-comm x Card\n where open \u2261\n\n exploreStableUnder\u2192sumStableUnder : \u2200 {p} \u2192 StableUnder explore p \u2192 SumStableUnder sum p\n exploreStableUnder\u2192sumStableUnder SU-p = SU-p 0 _+_\n\n count-ext : CountExt count\n count-ext f\u2257g = sum-ext (\u2261.cong \ud835\udfda\u25b9\u2115 \u2218 f\u2257g)\n\n sumStableUnder\u2192countStableUnder : \u2200 {p} \u2192 SumStableUnder sum p \u2192 CountStableUnder count p\n sumStableUnder\u2192countStableUnder sumSU-p f = sumSU-p (\ud835\udfda\u25b9\u2115 \u2218 f)\n\n diff-list = with-endo-monoid (List.monoid A) List.[_]\n\n {-\n list\u2261diff-list : list \u2261 diff-list\n list\u2261diff-list = {!explore-endo-monoid-spec (List.monoid A) List.[_]!}\n -}\n\n lift-sum : \u2200 \u2113 \u2192 Sum A\n lift-sum \u2113 f = lower {\u2080} {\u2113} (explore (lift 0) (lift-op\u2082 _+_) (lift \u2218 f))\n\n Fin-lower-sum\u2261\u03a3\u1d49-Fin : \u2200 {{_ : UA}}(f : A \u2192 \u2115) \u2192 Fin (lift-sum _ f) \u2261 \u03a3\u1d49 explore (Fin \u2218 f)\n Fin-lower-sum\u2261\u03a3\u1d49-Fin f = LiftHom.lift-hom _\u2261_ \u2261.refl \u2261.trans (lift 0) (lift-op\u2082 _+_) (Lift \ud835\udfd8) _\u228e_ \u228e= (Fin \u2218 lower) (lift \u2218 f) (Fin0\u2261\ud835\udfd8 \u2219 ! Lift\u2261id) (! Fin-\u228e-+)\n where open \u2261\n\nmodule FromTwoExploreInd\n {a} {A : \u2605 a}\n {e\u1d2c : \u2200 {\u2113} \u2192 Explore \u2113 A}\n (e\u1d2c-ind : \u2200 {p \u2113} \u2192 ExploreInd {\u2113} p e\u1d2c)\n {b} {B : \u2605 b}\n {e\u1d2e : \u2200 {\u2113} \u2192 Explore \u2113 B}\n (e\u1d2e-ind : \u2200 {p \u2113} \u2192 ExploreInd {\u2113} p e\u1d2e)\n where\n\n module A = FromExploreInd e\u1d2c-ind\n module B = FromExploreInd e\u1d2e-ind\n\n module _ {c \u2113}(cm : CommutativeMonoid c \u2113) where\n open CMon cm\n\n op\u1d2c = e\u1d2c \u03b5 _\u2219_\n op\u1d2e = e\u1d2e \u03b5 _\u2219_\n\n -- TODO use lift-hom\n explore-swap' : \u2200 f \u2192 op\u1d2c (op\u1d2e \u2218 f) \u2248 op\u1d2e (op\u1d2c \u2218 flip f)\n explore-swap' = A.explore-swap m (B.explore-\u03b5 m) (B.explore-hom cm)\n\n sum-swap : \u2200 f \u2192 A.sum (B.sum \u2218 f) \u2261 B.sum (A.sum \u2218 flip f)\n sum-swap = explore-swap' \u2115\u00b0.+-commutativeMonoid\n\nmodule FromTwoAdequate-sum\n {{_ : UA}}{{_ : FunExt}}\n {A}{B}\n {sum\u1d2c : Sum A}{sum\u1d2e : Sum B}\n (open Adequacy _\u2261_)\n (sum\u1d2c-adq : Adequate-sum sum\u1d2c)\n (sum\u1d2e-adq : Adequate-sum sum\u1d2e) where\n\n open \u2261\n sumStableUnder : (p : A \u2243 B)(f : B \u2192 \u2115)\n \u2192 sum\u1d2c (f \u2218 \u00b7\u2192 p) \u2261 sum\u1d2e f\n sumStableUnder p f = Fin-injective (sum\u1d2c-adq (f \u2218 \u00b7\u2192 p)\n \u2219 \u03a3-fst\u2243 p _\n \u2219 ! sum\u1d2e-adq f)\n\n sumStableUnder\u2032 : (p : A \u2243 B)(f : A \u2192 \u2115)\n \u2192 sum\u1d2c f \u2261 sum\u1d2e (f \u2218 <\u2013 p)\n sumStableUnder\u2032 p f = Fin-injective (sum\u1d2c-adq f\n \u2219 \u03a3-fst\u2243\u2032 p _\n \u2219 ! sum\u1d2e-adq (f \u2218 <\u2013 p))\n\nmodule FromAdequate-sum\n {A}\n {sum : Sum A}\n (open Adequacy _\u2261_)\n (sum-adq : Adequate-sum sum)\n {{_ : UA}}{{_ : FunExt}}\n where\n\n open FromTwoAdequate-sum sum-adq sum-adq public\n open \u2261\n\n sum-ext : SumExt sum\n sum-ext = ap sum \u2218 \u03bb=\n\n private\n count : Count A\n count f = sum (\ud835\udfda\u25b9\u2115 \u2218 f)\n\n private\n module M {p q : A \u2192 \ud835\udfda}(same-count : count p \u2261 count q) where\n private\n\n P = \u03bb x \u2192 p x \u2261 1\u2082\n Q = \u03bb x \u2192 q x \u2261 1\u2082\n \u00acP = \u03bb x \u2192 p x \u2261 0\u2082\n \u00acQ = \u03bb x \u2192 q x \u2261 0\u2082\n\n \u03c0 : \u03a3 A P \u2261 \u03a3 A Q\n \u03c0 = ! \u03a3=\u2032 _ (count-\u2261 p)\n \u2219 ! (sum-adq (\ud835\udfda\u25b9\u2115 \u2218 p))\n \u2219 ap Fin same-count\n \u2219 sum-adq (\ud835\udfda\u25b9\u2115 \u2218 q)\n \u2219 \u03a3=\u2032 _ (count-\u2261 q)\n\n lem1 : \u2200 px qx \u2192 \ud835\udfda\u25b9\u2115 qx \u2261 (\ud835\udfda\u25b9\u2115 (px \u2227 qx)) + \ud835\udfda\u25b9\u2115 (not px) * \ud835\udfda\u25b9\u2115 qx\n lem1 1\u2082 1\u2082 = \u2261.refl\n lem1 1\u2082 0\u2082 = \u2261.refl\n lem1 0\u2082 1\u2082 = \u2261.refl\n lem1 0\u2082 0\u2082 = \u2261.refl\n\n lem2 : \u2200 px qx \u2192 \ud835\udfda\u25b9\u2115 px \u2261 (\ud835\udfda\u25b9\u2115 (px \u2227 qx)) + \ud835\udfda\u25b9\u2115 px * \ud835\udfda\u25b9\u2115 (not qx)\n lem2 1\u2082 1\u2082 = \u2261.refl\n lem2 1\u2082 0\u2082 = \u2261.refl\n lem2 0\u2082 1\u2082 = \u2261.refl\n lem2 0\u2082 0\u2082 = \u2261.refl\n\n lemma1 : \u2200 px qx \u2192 (qx \u2261 1\u2082) \u2261 (Fin (\ud835\udfda\u25b9\u2115 (px \u2227 qx)) \u228e (px \u2261 0\u2082 \u00d7 qx \u2261 1\u2082))\n lemma1 px qx = ! Fin-\u2261-\u22611\u2082 qx\n \u2219 ap Fin (lem1 px qx)\n \u2219 ! Fin-\u228e-+\n \u2219 \u228e= refl (! Fin-\u00d7-* \u2219 \u00d7= (Fin-\u2261-\u22610\u2082 px) (Fin-\u2261-\u22611\u2082 qx))\n\n lemma2 : \u2200 px qx \u2192 (Fin (\ud835\udfda\u25b9\u2115 (px \u2227 qx)) \u228e (px \u2261 1\u2082 \u00d7 qx \u2261 0\u2082)) \u2261 (px \u2261 1\u2082)\n lemma2 px qx = ! \u228e= refl (! Fin-\u00d7-* \u2219 \u00d7= (Fin-\u2261-\u22611\u2082 px) (Fin-\u2261-\u22610\u2082 qx)) \u2219 Fin-\u228e-+ \u2219 ap Fin (! lem2 px qx) \u2219 Fin-\u2261-\u22611\u2082 px\n\n \u03c0' : (Fin (sum (\u03bb x \u2192 \ud835\udfda\u25b9\u2115 (p x \u2227 q x))) \u228e \u03a3 A (\u03bb x \u2192 P x \u00d7 \u00acQ x))\n \u2261 (Fin (sum (\u03bb x \u2192 \ud835\udfda\u25b9\u2115 (p x \u2227 q x))) \u228e \u03a3 A (\u03bb x \u2192 \u00acP x \u00d7 Q x))\n \u03c0' = \u228e= (sum-adq (\u03bb x \u2192 \ud835\udfda\u25b9\u2115 (p x \u2227 q x))) refl\n \u2219 ! \u03a3\u228e-split\n \u2219 \u03a3=\u2032 _ (\u03bb x \u2192 lemma2 (p x) (q x))\n \u2219 \u03c0\n \u2219 \u03a3=\u2032 _ (\u03bb x \u2192 lemma1 (p x) (q x))\n \u2219 \u03a3\u228e-split\n \u2219 ! \u228e= (sum-adq (\u03bb x \u2192 \ud835\udfda\u25b9\u2115 (p x \u2227 q x))) refl\n\n \u03c0'' : \u03a3 A (P \u00d7\u00b0 \u00acQ) \u2261 \u03a3 A (\u00acP \u00d7\u00b0 Q)\n \u03c0'' = Fin\u228e-injective (sum (\u03bb x \u2192 \ud835\udfda\u25b9\u2115 (p x \u2227 q x))) \u03c0'\n\n open EquivalentSubsets \u03c0'' public\n\n same-count\u2192iso : \u2200{p q : A \u2192 \ud835\udfda}(same-count : count p \u2261 count q) \u2192 p \u2261 q \u2218 M.\u03c0 {p} {q} same-count\n same-count\u2192iso {p} {q} sc = M.prop {p} {q} sc\n\nmodule From\u27e6Explore\u27e7\n {-a-} {A : \u2605\u2080 {- a-}}\n {explore : \u2200 {\u2113} \u2192 Explore \u2113 A}\n (\u27e6explore\u27e7 : \u2200 {\u2113\u2080 \u2113\u2081} \u2113\u1d63 \u2192 \u27e6Explore\u27e7 {\u2113\u2080} {\u2113\u2081} \u2113\u1d63 _\u2261_ explore explore)\n {{_ : UA}}\n where\n open FromExplore explore\n\n module AlsoInFromExploreInd\n {\u2113}(M : Monoid \u2113 \u2113)\n (open Mon M)\n (f : A \u2192 C)\n where\n explore-endo-monoid-spec\u2032 : \u2200 z \u2192 explore \u03b5 _\u2219_ f \u2219 z \u2248 explore-endo explore z _\u2219_ f\n explore-endo-monoid-spec\u2032 = \u27e6explore\u27e7 \u2080 {C} {C \u2192 C}\n (\u03bb r s \u2192 \u2200 z \u2192 r \u2219 z \u2248 s z)\n (fst identity)\n (\u03bb P\u2080 P\u2081 z \u2192 trans (assoc _ _ _) (trans (\u2219-cong refl (P\u2081 z)) (P\u2080 _)))\n (\u03bb x\u1d63 _ \u2192 \u2219-cong (reflexive (\u2261.ap f x\u1d63)) refl)\n\n explore-endo-monoid-spec : with-monoid M f \u2248 with-endo-monoid M f\n explore-endo-monoid-spec = trans (! snd identity _) (explore-endo-monoid-spec\u2032 \u03b5)\n\n open \u2261\n module _ (f : A \u2192 \u2115) where\n sum\u21d2\u03a3\u1d49 : Fin (explore 0 _+_ f) \u2261 explore (Lift \ud835\udfd8) _\u228e_ (Fin \u2218 f)\n sum\u21d2\u03a3\u1d49 = \u27e6explore\u27e7 {\u2080} {\u2081} \u2081\n (\u03bb n X \u2192 Fin n \u2261 X)\n (Fin0\u2261\ud835\udfd8 \u2219 ! Lift\u2261id)\n (\u03bb p q \u2192 ! Fin-\u228e-+ \u2219 \u228e= p q)\n (ap (Fin \u2218 f))\n\n product\u21d2\u03a0\u1d49 : Fin (explore 1 _*_ f) \u2261 explore (Lift \ud835\udfd9) _\u00d7_ (Fin \u2218 f)\n product\u21d2\u03a0\u1d49 = \u27e6explore\u27e7 {\u2080} {\u2081} \u2081\n (\u03bb n X \u2192 Fin n \u2261 X)\n (Fin1\u2261\ud835\udfd9 \u2219 ! Lift\u2261id)\n (\u03bb p q \u2192 ! Fin-\u00d7-* \u2219 \u00d7= p q)\n (ap (Fin \u2218 f))\n\n module _ (f : A \u2192 \ud835\udfda) where\n \u2713all-\u03a0\u1d49 : \u2713 (all f) \u2261 \u03a0\u1d49 explore (\u2713 \u2218 f)\n \u2713all-\u03a0\u1d49 = \u27e6explore\u27e7 {\u2080} {\u2081} \u2081\n (\u03bb b X \u2192 \u2713 b \u2261 X)\n (! Lift\u2261id)\n (\u03bb p q \u2192 \u2713-\u2227-\u00d7 _ _ \u2219 \u00d7= p q)\n (ap (\u2713 \u2218 f))\n\n \u2713any\u2192\u03a3\u1d49 : \u2713 (any f) \u2192 \u03a3\u1d49 explore (\u2713 \u2218 f)\n \u2713any\u2192\u03a3\u1d49 p = \u27e6explore\u27e7 {\u2080} {\u209b \u2080} \u2081\n (\u03bb b (X : \u2605\u2080) \u2192 Lift (\u2713 b) \u2192 X)\n (\u03bb x \u2192 lift (lower x))\n (\u03bb { {0\u2082} {x\u2081} x\u1d63 {y\u2080} {y\u2081} y\u1d63 z\u1d63 \u2192 inr (y\u1d63 z\u1d63)\n ; {1\u2082} {x\u2081} x\u1d63 {y\u2080} {y\u2081} y\u1d63 z\u1d63 \u2192 inl (x\u1d63 _) })\n (\u03bb x\u1d63 x \u2192 tr (\u2713 \u2218 f) x\u1d63 (lower x)) (lift p)\n\n module FromAdequate-\u03a3\u1d49\n (adequate-\u03a3\u1d49 : \u2200 {\u2113} \u2192 Adequate-\u03a3 {\u2113} (\u03a3\u1d49 explore))\n where\n open Adequacy\n\n adequate-sum : Adequate-sum _\u2261_ sum\n adequate-sum f = sum\u21d2\u03a3\u1d49 f \u2219 adequate-\u03a3\u1d49 (Fin \u2218 f)\n\n open FromAdequate-sum adequate-sum public\n\n adequate-any : Adequate-any -\u2192- any\n adequate-any f e = coe (adequate-\u03a3\u1d49 (\u2713 \u2218 f)) (\u2713any\u2192\u03a3\u1d49 f e)\n\n module FromAdequate-\u03a0\u1d49\n (adequate-\u03a0\u1d49 : \u2200 {\u2113} \u2192 Adequate-\u03a0 {\u2113} (\u03a0\u1d49 explore))\n where\n open Adequacy\n\n adequate-product : Adequate-product _\u2261_ product\n adequate-product f = product\u21d2\u03a0\u1d49 f \u2219 adequate-\u03a0\u1d49 (Fin \u2218 f)\n\n adequate-all : Adequate-all _\u2261_ all\n adequate-all f = \u2713all-\u03a0\u1d49 f \u2219 adequate-\u03a0\u1d49 _\n\n check! : (f : A \u2192 \ud835\udfda) {pf : \u2713 (all f)} \u2192 (\u2200 x \u2192 \u2713 (f x))\n check! f {pf} = coe (adequate-all f) pf\n\n{-\nmodule ExplorableRecord where\n record Explorable A : \u2605\u2081 where\n constructor mk\n field\n explore : Explore\u2080 A\n explore-ind : ExploreInd\u2080 explore\n\n open FromExploreInd explore-ind\n field\n adequate-sum : Adequate-sum sum\n -- adequate-product : AdequateProduct product\n\n open FromExploreInd explore-ind public\n\n open Explorable public\n\n ExploreForFun : \u2605\u2080 \u2192 \u2605\u2081\n ExploreForFun A = \u2200 {X} \u2192 Explorable X \u2192 Explorable (A \u2192 X)\n\n record Funable A : \u2605\u2082 where\n constructor _,_\n field\n explorable : Explorable A\n negative : ExploreForFun A\n\n module DistFun {A} (\u03bcA : Explorable A)\n (\u03bcA\u2192 : ExploreForFun A)\n {B} (\u03bcB : Explorable B){X}\n (_\u2248_ : X \u2192 X \u2192 \u2605 \u2080)\n (0\u2032 : X)\n (_+_ : X \u2192 X \u2192 X)\n (_*_ : X \u2192 X \u2192 X) where\n\n \u03a3\u1d2e = explore \u03bcB 0\u2032 _+_\n \u03a0' = explore \u03bcA 0\u2032 _*_\n \u03a3' = explore (\u03bcA\u2192 \u03bcB) 0\u2032 _+_\n\n DistFun = \u2200 f \u2192 \u03a0' (\u03a3\u1d2e \u2218 f) \u2248 \u03a3' (\u03a0' \u2218 _\u02e2_ f)\n\n DistFun : \u2200 {A} \u2192 Explorable A \u2192 ExploreForFun A \u2192 \u2605\u2081\n DistFun \u03bcA \u03bcA\u2192 = \u2200 {B} (\u03bcB : Explorable B) c \u2192 let open CMon {\u2080}{\u2080} c in\n \u2200 _*_ \u2192 Zero _\u2248_ \u03b5 _*_ \u2192 _DistributesOver_ _\u2248_ _*_ _\u2219_ \u2192 _*_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_\n \u2192 DistFun.DistFun \u03bcA \u03bcA\u2192 \u03bcB _\u2248_ \u03b5 _\u2219_ _*_\n\n DistFunable : \u2200 {A} \u2192 Funable A \u2192 \u2605\u2081\n DistFunable (\u03bcA , \u03bcA\u2192) = DistFun \u03bcA \u03bcA\u2192\n\n module _ {{_ : UA}}{{_ : FunExt}} where\n \u03bc-iso : \u2200 {A B} \u2192 (A \u2243 B) \u2192 Explorable A \u2192 Explorable B\n \u03bc-iso {A}{B} A\u2243B \u03bcA = mk (EM.map _ A\u2192B (explore \u03bcA)) (EM.map-ind _ A\u2192B (explore-ind \u03bcA)) ade\n where\n open \u2261\n A\u2192B = \u2013> A\u2243B\n ade = \u03bb f \u2192 adequate-sum \u03bcA (f \u2218 A\u2192B) \u2219 \u03a3-fst\u2243 A\u2243B _\n\n -- I guess this could be more general\n \u03bc-iso-preserve : \u2200 {A B} (A\u2243B : A \u2243 B) f (\u03bcA : Explorable A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-iso A\u2243B \u03bcA) (f \u2218 <\u2013 A\u2243B)\n \u03bc-iso-preserve A\u2243B f \u03bcA = sum-ext \u03bcA (\u03bb x \u2192 ap f (! (<\u2013-inv-l A\u2243B x)))\n where open \u2261\n\n {-\n \u03bcLift : \u2200 {A} \u2192 Explorable A \u2192 Explorable (Lift A)\n \u03bcLift = \u03bc-iso {!(! Lift\u2194id)!}\n where open \u2261\n -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"518e11d3c270fa1bc9465dcf6b737a977b65500a","subject":"Proof-irrelevance on paths when the type is claimed to be Is-set","message":"Proof-irrelevance on paths when the type is claimed to be Is-set\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Relation\/Binary\/PropositionalEquality\/K.agda","new_file":"lib\/Relation\/Binary\/PropositionalEquality\/K.agda","new_contents":"-- NOTE with-K\nmodule Relation.Binary.PropositionalEquality.K where\n\nopen import Type hiding (\u2605)\nopen import Relation.Binary.PropositionalEquality\n{-\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Product using (\u03a3; _,_)\nopen import Relation.Binary.Bijection\nopen import Relation.Binary.Logical\n-}\nopen import Relation.Binary.NP\nopen import Relation.Nullary\n\nmodule _ where\n postulate\n Is-set : \u2200 {a} \u2192 Set a \u2192 Set a\n\nproof-irrelevance : \u2200 {a} {A : Set a} {A-is-set : Is-set A} {x y : A} (p q : x \u2261 y) \u2192 p \u2261 q\nproof-irrelevance refl refl = refl\n\nmodule _ {a} {A : \u2605 a} where\n _\u2261\u2261_ : \u2200 {x y : A} (p q : x \u2261 y) \u2192 p \u2261 q\n _\u2261\u2261_ refl refl = refl\n\n _\u225f\u2261_ : \u2200 {i j : A} \u2192 Decidable {A = i \u2261 j} _\u2261_\n _\u225f\u2261_ refl refl = yes refl\n","old_contents":"-- NOTE with-K\nmodule Relation.Binary.PropositionalEquality.K where\n\nopen import Type hiding (\u2605)\nopen import Relation.Binary.PropositionalEquality\n{-\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Product using (\u03a3; _,_)\nopen import Relation.Binary.Bijection\nopen import Relation.Binary.Logical\n-}\nopen import Relation.Binary.NP\nopen import Relation.Nullary\n\nmodule _ {a} {A : \u2605 a} where\n _\u2261\u2261_ : \u2200 {x y : A} (p q : x \u2261 y) \u2192 p \u2261 q\n _\u2261\u2261_ refl refl = refl\n\n _\u225f\u2261_ : \u2200 {i j : A} \u2192 Decidable {A = i \u2261 j} _\u2261_\n _\u225f\u2261_ refl refl = yes refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"11ccfac008b3587ccf8f74ecc0e0da44907f22d5","subject":"adding a couple of rules after double checking with @cyrus-","message":"adding a couple of rules after double checking with @cyrus-\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"core.agda","new_file":"core.agda","new_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n -- used to mark which dhexp holes have been evaluated\n data mark : Set where\n \u2713 : mark\n \u2717 : mark\n\n mutual\n subst : Set -- todo: no idea if this is right; mutual thing is weird\n subst = dhexp ctx\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 subst \u00d7 mark) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 subst \u00d7 mark) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n <_>_ : htyp \u2192 dhexp \u2192 dhexp\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows, sums, and products\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = htyp ctx\n\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n -- todo: this probably belongs in contexts, but need to abstract it.\n id : tctx \u2192 subst\n id ctx x with ctx x\n id ctx x | Some \u03c4 = Some (X x)\n id ctx x | None = None\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 (tctx \u00d7 htyp))\n u ::[ \u0393 ] \u03c4 = u , (\u0393 , \u03c4)\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {n : Nat} \u2192\n (n , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of u?\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192 -- todo: uniqueness of u?\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- todo: do we care about completeness of hexp or e-umlauts? should this\n -- be judgemental rather than functional?\n\n -- those types without holes anywhere\n tcomplete : htyp \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (\u03c41 ==> \u03c42) = tcomplete \u03c41 \u00d7 tcomplete \u03c42\n\n -- those expressions without holes anywhere\n ecomplete : hexp \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: \u03c4) = ecomplete e1 \u00d7 tcomplete \u03c4\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ \u03c4 ] e) = tcomplete \u03c4 \u00d7 ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp1 : \u2200{\u0393 e1 e2 d2 d1 \u03941 \u03c41 \u03c42 \u03942} \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: bneed to think about disjointness and context rep\n \u0393 \u22a2 e1 => \u2987\u2988 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u2987\u2988) ~> d1 :: \u03c41 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u2987\u2988 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u2987\u2988 ~> (< \u03c42 ==> \u2987\u2988 > d1) \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESAp2 : \u2200{\u0393 e1 \u03c42 \u03c4 d1 d2 \u03941 \u03942 \u03c42' e2} \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: bneed to think about disjointness and context rep\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n (\u03c42 == \u03c42' \u2192 \u22a5) \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 (< \u03c42 > d2) \u22a3 (\u03941 \u222a \u03942)\n ESAp3 : \u2200{\u0393 e1 \u03c4 d1 \u03941 e2 \u03c42 d2 \u03942 } \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: bneed to think about disjointness and context rep\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , id \u0393 , \u2717 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , id \u0393 , \u2717 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc1 : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u03c4 == \u03c4' \u2192 \u22a5) \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> (< \u03c4 > d) \u22a3 \u0394\n ESAsc2 : \u2200{\u0393 e \u03c4 d \u0394 } \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4 \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c41 ==> \u03c42 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EALamHole : \u2200{\u0393 x e \u03c4 d \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u2987\u2988)) \u22a2 e \u21d0 \u2987\u2988 ~> d :: \u03c4 \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u2987\u2988 ~> \u00b7\u03bb x [ \u2987\u2988 ] d :: \u2987\u2988 ==> \u03c4 \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 (e == \u2987\u2988[ u ] \u2192 \u22a5)) \u2192\n ((e' : hexp) (u : Nat) \u2192 (e == \u2987 e' \u2988[ u ] \u2192 \u22a5)) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , id \u0393 , \u2717 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , id \u0393 , \u2717 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n mutual\n -- substitition type assignment\n _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 subst \u2192 tctx \u2192 Set\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' =\n (x : Nat) (d : dhexp) (xd\u2208\u03c3 : (x , d) \u2208 \u03c3) \u2192\n \u03a3[ \u03c4 \u2208 htyp ] (\u0393' x == Some \u03c4 \u00d7 \u0394 , \u0393 \u22a2 d :: \u03c4)\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c42 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u03c41 \u25b8arr (\u03c42 ==> \u03c4) \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4 m} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 , m \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 m} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 , m \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c4 \u03c4'} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0394 , \u0393 \u22a2 < \u03c4 > d :: \u03c4\n\n -- postulate -- todo: write this later\n -- substitution\n [_\/_]_ : dhexp \u2192 Nat \u2192 dhexp \u2192 dhexp\n [ d \/ y ] c = c\n [ d \/ y ] X x\n with natEQ x y\n [ d \/ y ] X .y | Inl refl = d\n [ d \/ y ] X x | Inr neq = X y\n [ d \/ y ] (\u00b7\u03bb x [ x\u2081 ] d') = \u00b7\u03bb x [ x\u2081 ] ( [ d \/ y ] d') -- TODO: i *think* barendrecht's saves us here, or at least i want it to. may need to reformulat this as a relation --> set\n [ d \/ y ] \u2987\u2988\u27e8 u , \u03c3 , m \u27e9 = \u2987\u2988\u27e8 u , \u03c3 , m \u27e9\n [ d \/ y ] \u2987 d' \u2988\u27e8 u , \u03c3 , m \u27e9 = \u2987 [ d \/ y ] d' \u2988\u27e8 u , \u03c3 , m \u27e9\n [ d \/ y ] (d1 \u2218 d2) = ([ d \/ y ] d1) \u2218 ([ d \/ y ] d2)\n [ d \/ y ] (< \u03c4 > d') = < \u03c4 > ([ d \/ y ] d')\n\n -- value\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n mutual\n -- indeterminate\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 , \u2713 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 , \u2713 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 d1 indet \u2192 d2 final \u2192 (d1 \u2218 d2) indet\n ICast : \u2200{d \u03c4} \u2192 d indet \u2192 (< \u03c4 > d) indet\n\n -- final\n data _final : (d : dhexp) \u2192 Set where\n FVal : \u2200{d} \u2192 d val \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n -- error\n data _\u22a2_err : (\u0394 : hctx) (d : dhexp) \u2192 Set where\n ECastError : \u2200{ \u0394 d \u03c41 \u03c42 } \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c42 \u2192\n \u03c41 ~\u0338 \u03c42 \u2192\n \u0394 \u22a2 (< \u03c41 > d) err\n EAp1 : \u2200{ \u0394 d1 d2} \u2192\n \u0394 \u22a2 d1 err \u2192\n \u0394 \u22a2 (d1 \u2218 d2) err\n EAp2 : \u2200{ \u0394 d1 d2} \u2192\n \u0394 \u22a2 d2 err \u2192\n \u0394 \u22a2 (d1 \u2218 d2) err\n ENEHole : \u2200{ \u0394 d u \u03c3 m} \u2192\n \u0394 \u22a2 d err \u2192\n \u0394 \u22a2 (\u2987 d \u2988\u27e8 (u , \u03c3 , m)\u27e9) err\n ECastProp : \u2200{ \u0394 d \u03c4} \u2192\n \u0394 \u22a2 d err \u2192\n \u0394 \u22a2 (< \u03c4 > d) err\n EConst : \u2200{ \u0394 \u03c4 } \u2192 \u0394 \u22a2 (< \u03c4 > c) err\n\n -- contextual dynamics\n\n -- evaluation contexts\n data ectx : Set where\n \u2299 : ectx\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 ectx \u2192 ectx\n _\u2218\u2081_ : dhexp \u2192 ectx \u2192 ectx\n _\u2218\u2082_ : ectx \u2192 dhexp \u2192 ectx\n \u2987_\u2988\u27e8_\u27e9 : ectx \u2192 (Nat \u00d7 subst \u00d7 mark) \u2192 ectx\n <_>_ : htyp \u2192 ectx \u2192 ectx\n\n -- instruction transition judgement\n data _\u22a2_\u2192>_ : (\u0394 : hctx) (d d' : dhexp) \u2192 Set where\n ITLam : \u2200{ \u0394 x \u03c4 d1 d2 } \u2192\n d2 final \u2192\n \u0394 \u22a2 ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u2192> ([ d2 \/ x ] d1) -- this is very unlikely to work long term\n ITCast : \u2200{d \u0394 \u03c41 \u03c42 } \u2192\n d final \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c42 \u2192\n \u03c41 ~ \u03c42 \u2192 -- maybe?\n \u0394 \u22a2 < \u03c41 > d \u2192> d\n ITEHole : \u2200{ \u0394 u \u03c3} \u2192\n \u0394 \u22a2 \u2987\u2988\u27e8 u , \u03c3 , \u2717 \u27e9 \u2192> \u2987\u2988\u27e8 u , \u03c3 , \u2713 \u27e9\n ITNEHole : \u2200{ \u0394 u \u03c3 d } \u2192\n d final \u2192\n \u0394 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 , \u2717 \u27e9 \u2192> \u2987 d \u2988\u27e8 u , \u03c3 , \u2713 \u27e9\n\n\n data _ectxt : (\u03b5 : ectx) \u2192 Set where\n ECDot : \u2299 ectxt\n ECLam : \u2200{\u03b5 x \u03c4} \u2192\n \u03b5 ectxt \u2192\n (\u00b7\u03bb x [ \u03c4 ] \u03b5) ectxt\n ECAp1 : \u2200{d \u03b5} \u2192\n d final \u2192\n \u03b5 ectxt \u2192\n (d \u2218\u2081 \u03b5) ectxt\n ECAp2 : \u2200{d \u03b5} \u2192\n \u03b5 ectxt \u2192\n (\u03b5 \u2218\u2082 d) ectxt\n ECNEHole : \u2200{\u03b5 m u \u03c3} \u2192\n \u03b5 ectxt \u2192\n \u2987 \u03b5 \u2988\u27e8 u , \u03c3 , m \u27e9 ectxt\n ECCast : \u2200{ \u03b5 \u03c4 } \u2192\n \u03b5 ectxt \u2192\n (< \u03c4 > \u03b5 ) ectxt\n\n data _==_\u27e6_\u27e7 : (d : dhexp) (\u03b5 : ectx) (d' : dhexp) \u2192 Set where\n FRefl : \u2200{d : dhexp} \u2192\n d == \u2299 \u27e6 d \u27e7\n FLam : \u2200{ d d1 \u03b5 x \u03c4 } \u2192\n d1 == \u03b5 \u27e6 d \u27e7 \u2192\n (\u00b7\u03bb x [ \u03c4 ] d1) == (\u00b7\u03bb x [ \u03c4 ] \u03b5) \u27e6 d \u27e7\n FAp1 : \u2200{ d1 d2 d \u03b5} \u2192\n d1 final \u2192\n d2 == \u03b5 \u27e6 d \u27e7 \u2192\n (d1 \u2218 d2) == (d1 \u2218\u2081 \u03b5) \u27e6 d \u27e7\n FAp2 : \u2200{ d1 d2 d \u03b5} \u2192\n d2 == \u03b5 \u27e6 d \u27e7 \u2192\n (d1 \u2218 d2) == (\u03b5 \u2218\u2082 d2) \u27e6 d \u27e7\n FNEHole : \u2200{ d d1 \u03b5 u \u03c3 m} \u2192\n d1 == \u03b5 \u27e6 d \u27e7 \u2192\n \u2987 d1 \u2988\u27e8 (u , \u03c3 , m) \u27e9 == \u2987 \u03b5 \u2988\u27e8 (u , \u03c3 , m) \u27e9 \u27e6 d \u27e7\n FCast : \u2200{ d d1 \u03b5 \u03c4 } \u2192\n d1 == \u03b5 \u27e6 d \u27e7 \u2192\n (< \u03c4 > d1) == < \u03c4 > \u03b5 \u27e6 d \u27e7\n\n data _\u22a2_\u21a6_ : (\u0394 : hctx) (d d' : dhexp) \u2192 Set where\n Step : \u2200{ d d0 d' d0' \u0394 \u03b5} \u2192\n d == \u03b5 \u27e6 d0 \u27e7 \u2192\n \u0394 \u22a2 d0 \u2192> d0' \u2192 -- should this \u0394 be \u2205?\n d' == \u03b5 \u27e6 d0' \u27e7 \u2192 -- why is this the same \u03b5\n \u0394 \u22a2 d \u21a6 d\n","old_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n -- used to mark which dhexp holes have been evaluated\n data mark : Set where\n \u2713 : mark\n \u2717 : mark\n\n mutual\n subst : Set -- todo: no idea if this is right; mutual thing is weird\n subst = dhexp ctx\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 subst \u00d7 mark) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 subst \u00d7 mark) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n <_>_ : htyp \u2192 dhexp \u2192 dhexp\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows, sums, and products\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = htyp ctx\n\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n -- todo: this probably belongs in contexts, but need to abstract it.\n id : tctx \u2192 subst\n id ctx x with ctx x\n id ctx x | Some \u03c4 = Some (X x)\n id ctx x | None = None\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 (tctx \u00d7 htyp))\n u ::[ \u0393 ] \u03c4 = u , (\u0393 , \u03c4)\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {n : Nat} \u2192\n (n , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of u?\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192 -- todo: uniqueness of u?\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- todo: do we care about completeness of hexp or e-umlauts? should this\n -- be judgemental rather than functional?\n\n -- those types without holes anywhere\n tcomplete : htyp \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (\u03c41 ==> \u03c42) = tcomplete \u03c41 \u00d7 tcomplete \u03c42\n\n -- those expressions without holes anywhere\n ecomplete : hexp \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: \u03c4) = ecomplete e1 \u00d7 tcomplete \u03c4\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ \u03c4 ] e) = tcomplete \u03c4 \u00d7 ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp1 : \u2200{\u0393 e1 e2 d2 d1 \u03941 \u03c41 \u03c42 \u03942} \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: bneed to think about disjointness and context rep\n \u0393 \u22a2 e1 => \u2987\u2988 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u2987\u2988) ~> d1 :: \u03c41 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u2987\u2988 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u2987\u2988 ~> (< \u03c42 ==> \u2987\u2988 > d1) \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESAp2 : \u2200{\u0393 e1 \u03c42 \u03c4 d1 d2 \u03941 \u03942 \u03c42' e2} \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: bneed to think about disjointness and context rep\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n (\u03c42 == \u03c42' \u2192 \u22a5) \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 (< \u03c42 > d2) \u22a3 (\u03941 \u222a \u03942)\n ESAp3 : \u2200{\u0393 e1 \u03c4 d1 \u03941 e2 \u03c42 d2 \u03942 } \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: bneed to think about disjointness and context rep\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , id \u0393 , \u2717 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , id \u0393 , \u2717 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc1 : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u03c4 == \u03c4' \u2192 \u22a5) \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> (< \u03c4 > d) \u22a3 \u0394\n ESAsc2 : \u2200{\u0393 e \u03c4 d \u0394 } \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4 \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c41 ==> \u03c42 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EALamHole : \u2200{\u0393 x e \u03c4 d \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u2987\u2988)) \u22a2 e \u21d0 \u2987\u2988 ~> d :: \u03c4 \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u2987\u2988 ~> \u00b7\u03bb x [ \u2987\u2988 ] d :: \u2987\u2988 ==> \u03c4 \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 (e == \u2987\u2988[ u ] \u2192 \u22a5)) \u2192\n ((e' : hexp) (u : Nat) \u2192 (e == \u2987 e' \u2988[ u ] \u2192 \u22a5)) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , id \u0393 , \u2717 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , id \u0393 , \u2717 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n mutual\n -- substitition type assignment\n _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 subst \u2192 tctx \u2192 Set\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' =\n (x : Nat) (d : dhexp) (xd\u2208\u03c3 : (x , d) \u2208 \u03c3) \u2192\n \u03a3[ \u03c4 \u2208 htyp ] (\u0393' x == Some \u03c4 \u00d7 \u0394 , \u0393 \u22a2 d :: \u03c4)\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c42 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u03c41 \u25b8arr (\u03c42 ==> \u03c4) \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4 m} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 , m \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 m} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 , m \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c4 \u03c4'} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0394 , \u0393 \u22a2 < \u03c4 > d :: \u03c4\n\n -- postulate -- todo: write this later\n -- substitution\n [_\/_]_ : dhexp \u2192 Nat \u2192 dhexp \u2192 dhexp\n [ d \/ y ] c = c\n [ d \/ y ] X x\n with natEQ x y\n [ d \/ y ] X .y | Inl refl = d\n [ d \/ y ] X x | Inr neq = X y\n [ d \/ y ] (\u00b7\u03bb x [ x\u2081 ] d') = \u00b7\u03bb x [ x\u2081 ] ( [ d \/ y ] d') -- TODO: i *think* barendrecht's saves us here, or at least i want it to. may need to reformulat this as a relation --> set\n [ d \/ y ] \u2987\u2988\u27e8 u , \u03c3 , m \u27e9 = \u2987\u2988\u27e8 u , \u03c3 , m \u27e9\n [ d \/ y ] \u2987 d' \u2988\u27e8 u , \u03c3 , m \u27e9 = \u2987 [ d \/ y ] d' \u2988\u27e8 u , \u03c3 , m \u27e9\n [ d \/ y ] (d1 \u2218 d2) = ([ d \/ y ] d1) \u2218 ([ d \/ y ] d2)\n [ d \/ y ] (< \u03c4 > d') = < \u03c4 > ([ d \/ y ] d')\n\n -- value\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n mutual\n -- indeterminate\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 , \u2713 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 , \u2713 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 d1 indet \u2192 d2 final \u2192 (d1 \u2218 d2) indet\n ICast : \u2200{d \u03c4} \u2192 d indet \u2192 (< \u03c4 > d) indet\n\n -- final\n data _final : (d : dhexp) \u2192 Set where\n FVal : \u2200{d} \u2192 d val \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n -- error\n data _\u22a2_err : (\u0394 : hctx) (d : dhexp) \u2192 Set where\n ECastError : \u2200{ \u0394 d \u03c41 \u03c42 } \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c42 \u2192\n \u03c41 ~\u0338 \u03c42 \u2192\n \u0394 \u22a2 (< \u03c41 > d) err\n EAp1 : \u2200{ \u0394 d1 d2} \u2192\n \u0394 \u22a2 d1 err \u2192\n \u0394 \u22a2 (d1 \u2218 d2) err\n EAp2 : \u2200{ \u0394 d1 d2} \u2192\n \u0394 \u22a2 d2 err \u2192\n \u0394 \u22a2 (d1 \u2218 d2) err\n ENEHole : \u2200{ \u0394 d u \u03c3 m} \u2192\n \u0394 \u22a2 d err \u2192\n \u0394 \u22a2 (\u2987 d \u2988\u27e8 (u , \u03c3 , m)\u27e9) err\n ECastProp : \u2200{ \u0394 d \u03c4} \u2192\n \u0394 \u22a2 d err \u2192\n \u0394 \u22a2 (< \u03c4 > d) err\n -- EConst : \u2200{ \u0394 \u03c4 } \u2192 \u0394 \u22a2 (< \u03c4 > c) err -- todo: is it an error to ever cast the constant?\n\n -- contextual dynamics\n\n -- evaluation contexts\n data ectx : Set where\n \u2299 : ectx\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 ectx \u2192 ectx\n _\u2218\u2081_ : dhexp \u2192 ectx \u2192 ectx\n _\u2218\u2082_ : ectx \u2192 dhexp \u2192 ectx\n \u2987_\u2988\u27e8_\u27e9 : ectx \u2192 (Nat \u00d7 subst \u00d7 mark) \u2192 ectx\n <_>_ : htyp \u2192 ectx \u2192 ectx\n\n -- instruction transition judgement\n data _\u22a2_\u2192>_ : (\u0394 : hctx) (d d' : dhexp) \u2192 Set where\n ITLam : \u2200{ \u0394 x \u03c4 d1 d2 } \u2192\n d2 final \u2192\n \u0394 \u22a2 ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u2192> ([ d2 \/ x ] d1) -- this is very unlikely to work long term\n ITCast : \u2200{d \u0394 \u03c41 \u03c42 } \u2192\n d final \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c42 \u2192\n \u03c41 ~ \u03c42 \u2192 -- maybe?\n \u0394 \u22a2 < \u03c41 > d \u2192> d -- is that the right thing to step to?\n ITEHole : \u2200{ \u0394 u \u03c3} \u2192\n \u0394 \u22a2 \u2987\u2988\u27e8 u , \u03c3 , \u2717 \u27e9 \u2192> \u2987\u2988\u27e8 u , \u03c3 , \u2713 \u27e9 -- todo: added to make progress work\n ITNEHole : \u2200{ \u0394 u \u03c3 d } \u2192 -- todo: aded this to make progress work\n d final \u2192\n \u0394 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 , \u2717 \u27e9 \u2192> \u2987 d \u2988\u27e8 u , \u03c3 , \u2713 \u27e9\n\n\n data _ectxt : (\u03b5 : ectx) \u2192 Set where\n ECDot : \u2299 ectxt\n ECLam : \u2200{\u03b5 x \u03c4} \u2192\n \u03b5 ectxt \u2192\n (\u00b7\u03bb x [ \u03c4 ] \u03b5) ectxt\n ECAp1 : \u2200{d \u03b5} \u2192\n d final \u2192\n \u03b5 ectxt \u2192\n (d \u2218\u2081 \u03b5) ectxt\n ECAp2 : \u2200{d \u03b5} \u2192\n \u03b5 ectxt \u2192\n (\u03b5 \u2218\u2082 d) ectxt\n ECNEHole : \u2200{\u03b5 m u \u03c3} \u2192\n \u03b5 ectxt \u2192\n \u2987 \u03b5 \u2988\u27e8 u , \u03c3 , m \u27e9 ectxt\n ECCast : \u2200{ \u03b5 \u03c4 } \u2192\n \u03b5 ectxt \u2192\n (< \u03c4 > \u03b5 ) ectxt\n\n data _==_\u27e6_\u27e7 : (d : dhexp) (\u03b5 : ectx) (d' : dhexp) \u2192 Set where\n FRefl : \u2200{d : dhexp} \u2192\n d == \u2299 \u27e6 d \u27e7\n FLam : \u2200{ d d1 \u03b5 x \u03c4 } \u2192\n d1 == \u03b5 \u27e6 d \u27e7 \u2192\n (\u00b7\u03bb x [ \u03c4 ] d1) == (\u00b7\u03bb x [ \u03c4 ] \u03b5) \u27e6 d \u27e7\n FAp1 : \u2200{ d1 d2 d \u03b5} \u2192\n d1 final \u2192\n d2 == \u03b5 \u27e6 d \u27e7 \u2192\n (d1 \u2218 d2) == (d1 \u2218\u2081 \u03b5) \u27e6 d \u27e7\n FAp2 : \u2200{ d1 d2 d \u03b5} \u2192\n d2 == \u03b5 \u27e6 d \u27e7 \u2192\n (d1 \u2218 d2) == (\u03b5 \u2218\u2082 d2) \u27e6 d \u27e7\n FNEHole : \u2200{ d d1 \u03b5 u \u03c3 m} \u2192\n d1 == \u03b5 \u27e6 d \u27e7 \u2192\n \u2987 d1 \u2988\u27e8 (u , \u03c3 , m) \u27e9 == \u2987 \u03b5 \u2988\u27e8 (u , \u03c3 , m) \u27e9 \u27e6 d \u27e7\n FCast : \u2200{ d d1 \u03b5 \u03c4 } \u2192\n d1 == \u03b5 \u27e6 d \u27e7 \u2192\n (< \u03c4 > d1) == < \u03c4 > \u03b5 \u27e6 d \u27e7\n\n data _\u22a2_\u21a6_ : (\u0394 : hctx) (d d' : dhexp) \u2192 Set where\n Step : \u2200{ d d0 d' d0' \u0394 \u03b5} \u2192\n d == \u03b5 \u27e6 d0 \u27e7 \u2192\n \u0394 \u22a2 d0 \u2192> d0' \u2192 -- should this \u0394 be \u2205?\n d' == \u03b5 \u27e6 d0' \u27e7 \u2192 -- why is this the same \u03b5\n \u0394 \u22a2 d \u21a6 d\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"6344e9fed9ae231dae91c77a68e2a74aa27511be","subject":"whitespace","message":"whitespace\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"core.agda","new_file":"core.agda","new_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n mutual\n subst : Set -- todo: no idea if this is right; mutual thing is weird\n subst = dhexp ctx\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 subst) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 subst) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n _\u27e8_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 dhexp\n\n -- notation for chaining together agreeable casts\n _\u27e8_\u21d2_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 htyp \u2192 dhexp\n d \u27e8 t1 \u21d2 t2 \u21d2 t3 \u27e9 = d \u27e8 t1 \u21d2 t2 \u27e9 \u27e8 t2 \u21d2 t3 \u27e9\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = htyp ctx\n\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n -- todo: this probably belongs in contexts, but need to abstract it.\n id : tctx \u2192 subst\n id ctx x with ctx x\n id ctx x | Some \u03c4 = Some (X x)\n id ctx x | None = None\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 (tctx \u00d7 htyp))\n u ::[ \u0393 ] \u03c4 = u , (\u0393 , \u03c4)\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {n : Nat} \u2192\n (n , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of u?\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192 -- todo: uniqueness of u?\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- todo: do we care about completeness of hexp or e-umlauts? should this\n -- be judgemental rather than functional?\n\n -- those types without holes anywhere\n tcomplete : htyp \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (\u03c41 ==> \u03c42) = tcomplete \u03c41 \u00d7 tcomplete \u03c42\n\n -- those expressions without holes anywhere\n ecomplete : hexp \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: \u03c4) = ecomplete e1 \u00d7 tcomplete \u03c4\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ \u03c4 ] e) = tcomplete \u03c4 \u00d7 ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp : \u2200{\u0393 e1 \u03c4 \u03c41 \u03c41' \u03c42 \u03c42' d1 \u03941 e2 d2 \u03942 } \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: need to think about disjointness and context rep\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u03c4) ~> d1 :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> (d1 \u27e8 \u03c41' \u21d2 \u03c42 ==> \u03c4 \u27e9) \u2218 (d2 \u27e8 \u03c42' \u21d2 \u03c42 \u27e9) \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u27e8 \u03c4' \u21d2 \u03c4 \u27e9 \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c4 \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c4 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 e \u2260 \u2987\u2988[ u ]) \u2192\n ((e' : hexp) (u : Nat) \u2192 e \u2260 \u2987 e' \u2988[ u ]) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n mutual\n -- substitition type assignment\n _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 subst \u2192 tctx \u2192 Set\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' =\n (x : Nat) (d : dhexp) (xd\u2208\u03c3 : (x , d) \u2208 \u03c3) \u2192\n \u03a3[ \u03c4 \u2208 htyp ] (\u0393' x == Some \u03c4 \u00d7 \u0394 , \u0393 \u22a2 d :: \u03c4)\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 ==> \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 } \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c41 \u03c42} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u03c41 ~ \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 :: \u03c42\n\n -- substitution;; todo: maybe get a premise that it's final; analagous to \"value substitution\"\n [_\/_]_ : dhexp \u2192 Nat \u2192 dhexp \u2192 dhexp\n [ d \/ y ] c = c\n [ d \/ y ] X x\n with natEQ x y\n [ d \/ y ] X .y | Inl refl = d\n [ d \/ y ] X x | Inr neq = X y\n [ d \/ y ] (\u00b7\u03bb x [ x\u2081 ] d') = \u00b7\u03bb x [ x\u2081 ] ( [ d \/ y ] d') -- TODO: i *think* barendrecht's saves us here, or at least i want it to. may need to reformulat this as a relation --> set\n [ d \/ y ] \u2987\u2988\u27e8 u , \u03c3 \u27e9 = \u2987\u2988\u27e8 u , \u03c3 \u27e9\n [ d \/ y ] \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9 = \u2987 [ d \/ y ] d' \u2988\u27e8 u , \u03c3 \u27e9\n [ d \/ y ] (d1 \u2218 d2) = ([ d \/ y ] d1) \u2218 ([ d \/ y ] d2)\n [ d \/ y ] (d' \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 ) = ([ d \/ y ] d') \u27e8 \u03c41 \u21d2 \u03c42 \u27e9\n\n -- value\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n -- ground\n data _ground : (\u03c4 : htyp) \u2192 Set where\n GBase : b ground\n GHole : \u2987\u2988 ==> \u2987\u2988 ground\n\n data _boxedval : (d : dhexp) \u2192 Set where\n BVVal : \u2200{d} \u2192 d val \u2192 d boxedval\n BVArrCast : \u2200{ d \u03c41 \u03c42 \u03c43 \u03c44 } \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d boxedval \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 boxedval\n BVHoleCast : \u2200{ \u03c4 d } \u2192 \u03c4 ground \u2192 d boxedval \u2192 d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 boxedval\n\n mutual\n -- indeterminate\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192\n d1 \u2260 (d1' \u27e8(\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44)\u27e9)) \u2192\n d1 indet \u2192\n d2 final \u2192\n (d1 \u2218 d2) indet -- todo: should there be two ap rules?\n ICastArr : \u2200{d \u03c41 \u03c42 \u03c43 \u03c44} \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d indet \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 indet\n ICastGroundHole : \u2200{ \u03c4 d } \u2192\n \u03c4 ground \u2192\n d indet \u2192\n d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 indet\n ICastHoleGround : \u2200 { d \u03c4 } \u2192\n ((d' : dhexp) (\u03c4' : htyp) \u2192 d \u2260 (d' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9)) \u2192\n d indet \u2192\n \u03c4 ground \u2192\n d \u27e8 \u2987\u2988 \u21d2 \u03c4 \u27e9 indet\n\n -- final\n data _final : (d : dhexp) \u2192 Set where\n FBoxed : \u2200{d} \u2192 d boxedval \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n\n -- -- contextual dynamics\n\n -- evaluation contexts\n data ectx : Set where\n \u2299 : ectx\n _\u2218\u2081_ : ectx \u2192 dhexp \u2192 ectx\n _\u2218\u2082_ : dhexp \u2192 ectx \u2192 ectx\n \u2987_\u2988\u27e8_\u27e9 : ectx \u2192 (Nat \u00d7 subst ) \u2192 ectx\n _\u27e8_\u21d2_\u27e9 : ectx \u2192 htyp \u2192 htyp \u2192 ectx\n\n --\u03b5 is an evaluation context\n data _evalctx : (\u03b5 : ectx) \u2192 Set where\n ECDot : \u2299 evalctx\n ECAp1 : \u2200{d \u03b5} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u2218\u2081 d) evalctx\n ECAp2 : \u2200{d \u03b5} \u2192\n d final \u2192\n \u03b5 evalctx \u2192\n (d \u2218\u2082 \u03b5) evalctx\n ECNEHole : \u2200{\u03b5 u \u03c3} \u2192\n \u03b5 evalctx \u2192\n \u2987 \u03b5 \u2988\u27e8 u , \u03c3 \u27e9 evalctx\n ECCast : \u2200{ \u03b5 \u03c41 \u03c42} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) evalctx\n\n -- d is the result of filling the hole in \u03b5 with d'\n data _==_\u27e6_\u27e7 : (d : dhexp) (\u03b5 : ectx) (d' : dhexp) \u2192 Set where\n FHOuter : \u2200{d} \u2192 d == \u2299 \u27e6 d \u27e7 -- this used to a have a premise of being final for some reason\n FHAp1 : \u2200{d1 d1' d2 \u03b5} \u2192\n d1 == \u03b5 \u27e6 d1' \u27e7 \u2192\n (d1 \u2218 d2) == (\u03b5 \u2218\u2081 d2) \u27e6 d1' \u27e7\n FHAp2 : \u2200{d1 d2 d2' \u03b5} \u2192\n d1 final \u2192\n d2 == \u03b5 \u27e6 d2' \u27e7 \u2192\n (d1 \u2218 d2) == (d1 \u2218\u2082 \u03b5) \u27e6 d2' \u27e7\n FHNEHole : \u2200{ d d' \u03b5 u \u03c3} \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u2987 d \u2988\u27e8 (u , \u03c3 ) \u27e9 == \u2987 \u03b5 \u2988\u27e8 (u , \u03c3 ) \u27e9 \u27e6 d' \u27e7\n FHCast : \u2200{ d d' \u03b5 \u03c41 \u03c42 } \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 == \u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 \u27e6 d' \u27e7\n\n -- instruction transition judgement\n data _\u2192>_ : (d d' : dhexp) \u2192 Set where\n ITLam : \u2200{ x \u03c4 d1 d2 } \u2192\n d2 final \u2192\n ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u2192> ([ d2 \/ x ] d1) -- this is very unlikely to work long term\n ITCastID : \u2200{d \u03c4 } \u2192\n d final \u2192\n (d \u27e8 \u03c4 \u21d2 \u03c4 \u27e9) \u2192> d\n ITCastSucceed : \u2200{d \u03c4 } \u2192\n d final \u2192\n \u03c4 ground \u2192\n (d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u21d2 \u03c4 \u27e9) \u2192> d\n ITApCast : \u2200{d1 d2 \u03c41 \u03c42 \u03c41' \u03c42' } \u2192\n d1 final \u2192\n d2 final \u2192\n ((d1 \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c41' ==> \u03c42')\u27e9) \u2218 d2) \u2192> ((d1 \u2218 (d2 \u27e8 \u03c41' \u21d2 \u03c41 \u27e9)) \u27e8 \u03c42 \u21d2 \u03c42' \u27e9)\n ITGround : \u2200{ d \u03c41 \u03c42 } \u2192\n d final \u2192\n \u03c41 ==> \u03c42 \u2260 \u2987\u2988 ==> \u2987\u2988 \u2192\n (d \u27e8 \u03c41 ==> \u03c42 \u21d2 \u2987\u2988 \u27e9) \u2192> (d \u27e8 \u03c41 ==> \u03c42 \u21d2 \u2987\u2988 ==> \u2987\u2988 \u21d2 \u2987\u2988 \u27e9)\n ITExpand : \u2200{d \u03c41 \u03c42 } \u2192\n d final \u2192\n \u03c41 ==> \u03c42 \u2260 \u2987\u2988 ==> \u2987\u2988 \u2192\n (d \u27e8 \u2987\u2988 \u21d2 \u03c41 ==> \u03c42 \u27e9) \u2192> (d \u27e8 \u2987\u2988 \u21d2 \u2987\u2988 ==> \u2987\u2988 \u21d2 \u03c41 ==> \u03c42 \u27e9)\n\n data _\u21a6_ : (d d' : dhexp) \u2192 Set where\n Step : \u2200{ d d0 d' d0' \u03b5} \u2192\n d == \u03b5 \u27e6 d0 \u27e7 \u2192\n d0 \u2192> d0' \u2192\n d' == \u03b5 \u27e6 d0' \u27e7 \u2192\n d \u21a6 d'\n\n data _\u21a6*_ : (d d' : dhexp) \u2192 Set where\n MSRefl : \u2200{d} \u2192 d \u21a6* d\n MSStep : \u2200{d d' d''} \u2192\n d \u21a6 d' \u2192\n d' \u21a6* d'' \u2192\n d \u21a6* d''\n\n data _casterr : (d : dhexp) \u2192 Set where\n CECastFail : \u2200 {d \u03c41 \u03c42} \u2192\n d final \u2192\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n d \u27e8 \u03c41 \u21d2 \u2987\u2988 \u21d2 \u03c42 \u27e9 casterr\n CECong : \u2200{ d \u03b5 d0} \u2192\n d == \u03b5 \u27e6 d0 \u27e7 \u2192\n d0 casterr \u2192\n d casterr\n","old_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n mutual\n subst : Set -- todo: no idea if this is right; mutual thing is weird\n subst = dhexp ctx\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 subst) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 subst) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n _\u27e8_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 dhexp\n\n -- notation for chaining together agreeable casts\n _\u27e8_\u21d2_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 htyp \u2192 dhexp\n d \u27e8 t1 \u21d2 t2 \u21d2 t3 \u27e9 = d \u27e8 t1 \u21d2 t2 \u27e9 \u27e8 t2 \u21d2 t3 \u27e9\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = htyp ctx\n\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n -- todo: this probably belongs in contexts, but need to abstract it.\n id : tctx \u2192 subst\n id ctx x with ctx x\n id ctx x | Some \u03c4 = Some (X x)\n id ctx x | None = None\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 (tctx \u00d7 htyp))\n u ::[ \u0393 ] \u03c4 = u , (\u0393 , \u03c4)\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {n : Nat} \u2192\n (n , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of u?\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192 -- todo: uniqueness of u?\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- todo: do we care about completeness of hexp or e-umlauts? should this\n -- be judgemental rather than functional?\n\n -- those types without holes anywhere\n tcomplete : htyp \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (\u03c41 ==> \u03c42) = tcomplete \u03c41 \u00d7 tcomplete \u03c42\n\n -- those expressions without holes anywhere\n ecomplete : hexp \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: \u03c4) = ecomplete e1 \u00d7 tcomplete \u03c4\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ \u03c4 ] e) = tcomplete \u03c4 \u00d7 ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp : \u2200{\u0393 e1 \u03c4 \u03c41 \u03c41' \u03c42 \u03c42' d1 \u03941 e2 d2 \u03942 } \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: need to think about disjointness and context rep\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u03c4) ~> d1 :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> (d1 \u27e8 \u03c41' \u21d2 \u03c42 ==> \u03c4 \u27e9) \u2218 (d2 \u27e8 \u03c42' \u21d2 \u03c42 \u27e9) \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u27e8 \u03c4' \u21d2 \u03c4 \u27e9 \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c4 \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c4 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 e \u2260 \u2987\u2988[ u ]) \u2192\n ((e' : hexp) (u : Nat) \u2192 e \u2260 \u2987 e' \u2988[ u ]) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n mutual\n -- substitition type assignment\n _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 subst \u2192 tctx \u2192 Set\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' =\n (x : Nat) (d : dhexp) (xd\u2208\u03c3 : (x , d) \u2208 \u03c3) \u2192\n \u03a3[ \u03c4 \u2208 htyp ] (\u0393' x == Some \u03c4 \u00d7 \u0394 , \u0393 \u22a2 d :: \u03c4)\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 ==> \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 } \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c41 \u03c42} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u03c41 ~ \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 :: \u03c42\n\n -- substitution;; todo: maybe get a premise that it's final; analagous to \"value substitution\"\n [_\/_]_ : dhexp \u2192 Nat \u2192 dhexp \u2192 dhexp\n [ d \/ y ] c = c\n [ d \/ y ] X x\n with natEQ x y\n [ d \/ y ] X .y | Inl refl = d\n [ d \/ y ] X x | Inr neq = X y\n [ d \/ y ] (\u00b7\u03bb x [ x\u2081 ] d') = \u00b7\u03bb x [ x\u2081 ] ( [ d \/ y ] d') -- TODO: i *think* barendrecht's saves us here, or at least i want it to. may need to reformulat this as a relation --> set\n [ d \/ y ] \u2987\u2988\u27e8 u , \u03c3 \u27e9 = \u2987\u2988\u27e8 u , \u03c3 \u27e9\n [ d \/ y ] \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9 = \u2987 [ d \/ y ] d' \u2988\u27e8 u , \u03c3 \u27e9\n [ d \/ y ] (d1 \u2218 d2) = ([ d \/ y ] d1) \u2218 ([ d \/ y ] d2)\n [ d \/ y ] (d' \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 ) = ([ d \/ y ] d') \u27e8 \u03c41 \u21d2 \u03c42 \u27e9\n\n -- value\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n -- ground\n data _ground : (\u03c4 : htyp) \u2192 Set where\n GBase : b ground\n GHole : \u2987\u2988 ==> \u2987\u2988 ground\n\n data _boxedval : (d : dhexp) \u2192 Set where\n BVVal : \u2200{d} \u2192 d val \u2192 d boxedval\n BVArrCast : \u2200{ d \u03c41 \u03c42 \u03c43 \u03c44 } \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d boxedval \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 boxedval\n BVHoleCast : \u2200{ \u03c4 d } \u2192 \u03c4 ground \u2192 d boxedval \u2192 d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 boxedval\n\n mutual\n -- indeterminate\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192 d1 \u2260 (d1' \u27e8(\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44)\u27e9)) \u2192\n d1 indet \u2192\n d2 final \u2192\n (d1 \u2218 d2) indet -- todo: should there be two ap rules?\n ICastArr : \u2200{d \u03c41 \u03c42 \u03c43 \u03c44} \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d indet \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 indet\n ICastGroundHole : \u2200{ \u03c4 d } \u2192\n \u03c4 ground \u2192\n d indet \u2192\n d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 indet\n ICastHoleGround : \u2200 { d \u03c4 } \u2192\n ((d' : dhexp) (\u03c4' : htyp) \u2192 d \u2260 (d' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9)) \u2192\n d indet \u2192\n \u03c4 ground \u2192\n d \u27e8 \u2987\u2988 \u21d2 \u03c4 \u27e9 indet\n\n -- final\n data _final : (d : dhexp) \u2192 Set where\n FBoxed : \u2200{d} \u2192 d boxedval \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n\n -- -- contextual dynamics\n\n -- evaluation contexts\n data ectx : Set where\n \u2299 : ectx\n _\u2218\u2081_ : ectx \u2192 dhexp \u2192 ectx\n _\u2218\u2082_ : dhexp \u2192 ectx \u2192 ectx\n \u2987_\u2988\u27e8_\u27e9 : ectx \u2192 (Nat \u00d7 subst ) \u2192 ectx\n _\u27e8_\u21d2_\u27e9 : ectx \u2192 htyp \u2192 htyp \u2192 ectx\n\n --\u03b5 is an evaluation context\n data _evalctx : (\u03b5 : ectx) \u2192 Set where\n ECDot : \u2299 evalctx\n ECAp1 : \u2200{d \u03b5} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u2218\u2081 d) evalctx\n ECAp2 : \u2200{d \u03b5} \u2192\n d final \u2192\n \u03b5 evalctx \u2192\n (d \u2218\u2082 \u03b5) evalctx\n ECNEHole : \u2200{\u03b5 u \u03c3} \u2192\n \u03b5 evalctx \u2192\n \u2987 \u03b5 \u2988\u27e8 u , \u03c3 \u27e9 evalctx\n ECCast : \u2200{ \u03b5 \u03c41 \u03c42} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) evalctx\n\n -- d is the result of filling the hole in \u03b5 with d'\n data _==_\u27e6_\u27e7 : (d : dhexp) (\u03b5 : ectx) (d' : dhexp) \u2192 Set where\n FHOuter : \u2200{d} \u2192 d == \u2299 \u27e6 d \u27e7 -- this used to a have a premise of being final for some reason\n FHAp1 : \u2200{d1 d1' d2 \u03b5} \u2192\n d1 == \u03b5 \u27e6 d1' \u27e7 \u2192\n (d1 \u2218 d2) == (\u03b5 \u2218\u2081 d2) \u27e6 d1' \u27e7\n FHAp2 : \u2200{d1 d2 d2' \u03b5} \u2192\n d1 final \u2192\n d2 == \u03b5 \u27e6 d2' \u27e7 \u2192\n (d1 \u2218 d2) == (d1 \u2218\u2082 \u03b5) \u27e6 d2' \u27e7\n FHNEHole : \u2200{ d d' \u03b5 u \u03c3} \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u2987 d \u2988\u27e8 (u , \u03c3 ) \u27e9 == \u2987 \u03b5 \u2988\u27e8 (u , \u03c3 ) \u27e9 \u27e6 d' \u27e7\n FHCast : \u2200{ d d' \u03b5 \u03c41 \u03c42 } \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 == \u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 \u27e6 d' \u27e7\n\n -- instruction transition judgement\n data _\u2192>_ : (d d' : dhexp) \u2192 Set where\n ITLam : \u2200{ x \u03c4 d1 d2 } \u2192\n d2 final \u2192\n ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u2192> ([ d2 \/ x ] d1) -- this is very unlikely to work long term\n ITCastID : \u2200{d \u03c4 } \u2192\n d final \u2192\n (d \u27e8 \u03c4 \u21d2 \u03c4 \u27e9) \u2192> d\n ITCastSucceed : \u2200{d \u03c4 } \u2192\n d final \u2192\n \u03c4 ground \u2192\n (d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u21d2 \u03c4 \u27e9) \u2192> d\n ITApCast : \u2200{d1 d2 \u03c41 \u03c42 \u03c41' \u03c42' } \u2192\n d1 final \u2192\n d2 final \u2192\n ((d1 \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c41' ==> \u03c42')\u27e9) \u2218 d2) \u2192> ((d1 \u2218 (d2 \u27e8 \u03c41' \u21d2 \u03c41 \u27e9)) \u27e8 \u03c42 \u21d2 \u03c42' \u27e9)\n ITGround : \u2200{ d \u03c41 \u03c42 } \u2192\n d final \u2192\n \u03c41 ==> \u03c42 \u2260 \u2987\u2988 ==> \u2987\u2988 \u2192\n (d \u27e8 \u03c41 ==> \u03c42 \u21d2 \u2987\u2988 \u27e9) \u2192> (d \u27e8 \u03c41 ==> \u03c42 \u21d2 \u2987\u2988 ==> \u2987\u2988 \u21d2 \u2987\u2988 \u27e9)\n ITExpand : \u2200{d \u03c41 \u03c42 } \u2192\n d final \u2192\n \u03c41 ==> \u03c42 \u2260 \u2987\u2988 ==> \u2987\u2988 \u2192\n (d \u27e8 \u2987\u2988 \u21d2 \u03c41 ==> \u03c42 \u27e9) \u2192> (d \u27e8 \u2987\u2988 \u21d2 \u2987\u2988 ==> \u2987\u2988 \u21d2 \u03c41 ==> \u03c42 \u27e9)\n\n data _\u21a6_ : (d d' : dhexp) \u2192 Set where\n Step : \u2200{ d d0 d' d0' \u03b5} \u2192\n d == \u03b5 \u27e6 d0 \u27e7 \u2192\n d0 \u2192> d0' \u2192\n d' == \u03b5 \u27e6 d0' \u27e7 \u2192\n d \u21a6 d'\n\n data _\u21a6*_ : (d d' : dhexp) \u2192 Set where\n MSRefl : \u2200{d} \u2192 d \u21a6* d\n MSStep : \u2200{d d' d''} \u2192\n d \u21a6 d' \u2192\n d' \u21a6* d'' \u2192\n d \u21a6* d''\n\n data _casterr : (d : dhexp) \u2192 Set where\n CECastFail : \u2200 {d \u03c41 \u03c42} \u2192\n d final \u2192\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n d \u27e8 \u03c41 \u21d2 \u2987\u2988 \u21d2 \u03c42 \u27e9 casterr\n CECong : \u2200{ d \u03b5 d0} \u2192\n d == \u03b5 \u27e6 d0 \u27e7 \u2192\n d0 casterr \u2192\n d casterr\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d8553018dc2fe750ba11fedbda0f3db018125324","subject":"figured out the last judgement","message":"figured out the last judgement\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"core.agda","new_file":"core.agda","new_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n -- used to mark which dhexp holes have been evaluated\n data mark : Set where\n \u2713 : mark\n \u2717 : mark\n\n mutual\n subst : Set -- todo: no idea if this is right; mutual thing is weird\n subst = dhexp ctx\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 subst \u00d7 mark) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 subst \u00d7 mark) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n <_>_ : htyp \u2192 dhexp \u2192 dhexp\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows, sums, and products\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = htyp ctx\n\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n -- todo: this probably belongs in contexts, but need to abstract it.\n id : tctx \u2192 subst\n id ctx x with ctx x\n id ctx x | Some \u03c4 = Some (X x)\n id ctx x | None = None\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 (tctx \u00d7 htyp))\n u ::[ \u0393 ] \u03c4 = u , (\u0393 , \u03c4)\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {n : Nat} \u2192\n (n , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of n?\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192 -- todo: uniqueness of n?\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- todo: do we care about completeness of hexp or e-umlauts?\n -- those types without holes anywhere\n tcomplete : htyp \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (\u03c41 ==> \u03c42) = tcomplete \u03c41 \u00d7 tcomplete \u03c42\n\n -- those expressions without holes anywhere\n ecomplete : hexp \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: \u03c4) = ecomplete e1 \u00d7 tcomplete \u03c4\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ \u03c4 ] e) = tcomplete \u03c4 \u00d7 ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp1 : \u2200{\u0393 e1 e2 d2 d1 \u03941 \u03c41 \u03c42 \u03942} \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: bneed to think about disjointness and context rep\n \u0393 \u22a2 e1 => \u2987\u2988 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u2987\u2988) ~> d1 :: \u03c41 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u2987\u2988 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u2987\u2988 ~> (< \u03c42 ==> \u2987\u2988 > d1) \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESAp2 : \u2200{\u0393 e1 \u03c42 \u03c4 d1 d2 \u03941 \u03942 \u03c42' e2} \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: bneed to think about disjointness and context rep\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n (\u03c42 == \u03c42' \u2192 \u22a5) \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 (< \u03c42 > d2) \u22a3 (\u03941 \u222a \u03942)\n ESAp3 : \u2200{\u0393 e1 \u03c4 d1 \u03941 e2 \u03c42 d2 \u03942 } \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: bneed to think about disjointness and context rep\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , id \u0393 , \u2717 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , id \u0393 , \u2717 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc1 : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u03c4 == \u03c4' \u2192 \u22a5) \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> (< \u03c4 > d) \u22a3 \u0394\n ESAsc2 : \u2200{\u0393 e \u03c4 d \u0394 } \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4 \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c41 ==> \u03c42 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EALamHole : \u2200{\u0393 x e \u03c4 d \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u2987\u2988)) \u22a2 e \u21d0 \u2987\u2988 ~> d :: \u03c4 \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u2987\u2988 ~> \u00b7\u03bb x [ \u2987\u2988 ] d :: \u2987\u2988 ==> \u03c4 \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 (e == \u2987\u2988[ u ] \u2192 \u22a5)) \u2192\n ((e' : hexp) (u : Nat) \u2192 (e == \u2987 e' \u2988[ u ] \u2192 \u22a5)) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , id \u0393 , \u2717 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , id \u0393 , \u2717 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n mutual\n -- substitition type assignment\n _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 subst \u2192 tctx \u2192 Set\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' =\n (x : Nat) (d : dhexp) (xd\u2208\u03c3 : (x , d) \u2208 \u03c3) \u2192\n \u03a3[ \u03c4 \u2208 htyp ] (\u0393' x == Some \u03c4 \u00d7 \u0394 , \u0393 \u22a2 d :: \u03c4)\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c42 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u03c41 \u25b8arr (\u03c42 ==> \u03c4) \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4 m} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 , m \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 m} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 , m \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c4 \u03c4'} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0394 , \u0393 \u22a2 < \u03c4 > d :: \u03c4\n\n postulate -- todo: write this later\n [_]_ : subst \u2192 dhexp \u2192 dhexp\n\n -- value\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n mutual\n -- indeterminate\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 d1 indet \u2192 d2 final \u2192 (d1 \u2218 d2) indet\n ICast : \u2200{d \u03c4} \u2192 d indet \u2192 (< \u03c4 > d) indet\n\n -- final\n data _final : (d : dhexp) \u2192 Set where\n FVal : \u2200{d} \u2192 d val \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n -- error -- todo\n data _err[_] : (d : dhexp) \u2192 (\u0394 : hctx) \u2192 Set where\n -- ERNEHole\n -- ERCastError\n -- ERLam\n -- ERAp1\n -- ERAp2\n -- ERCast\n\n -- contextual dynamics\n\n -- evaluation contexts\n data ectx : Set where\n \u2299 : ectx\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 ectx \u2192 ectx\n _\u2218\u2081_ : dhexp \u2192 ectx \u2192 ectx\n _\u2218\u2082_ : ectx \u2192 dhexp \u2192 ectx\n \u2987_\u2988\u27e8_\u27e9 : ectx \u2192 (Nat \u00d7 subst \u00d7 mark) \u2192 ectx\n <_>_ : htyp \u2192 ectx \u2192 ectx\n\n -- instruction transition judgement\n data _\u22a2_\u2192>_ : (\u0394 : hctx) (d d' : dhexp) \u2192 Set where\n ITLam : \u2200{ \u0394 x \u03c4 d1 d2 } \u2192\n d2 final \u2192\n \u0394 \u22a2 ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u2192> ([ \u25a0 (x , d2) ] d1) -- this is very unlikely to work long term\n ITCast : \u2200{d \u0394 \u03c41 \u03c42 } \u2192\n d final \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c42 \u2192\n \u03c41 ~ \u03c42 \u2192 -- maybe?\n \u0394 \u22a2 < \u03c41 > d \u2192> d -- is that the right thing to step to?\n\n data _ectxt : (\u03b5 : ectx) \u2192 Set where\n ECDot : \u2299 ectxt\n ECLam : \u2200{\u03b5 x \u03c4} \u2192\n \u03b5 ectxt \u2192\n (\u00b7\u03bb x [ \u03c4 ] \u03b5) ectxt\n ECAp1 : \u2200{d \u03b5} \u2192\n d final \u2192\n \u03b5 ectxt \u2192\n (d \u2218\u2081 \u03b5) ectxt\n ECAp2 : \u2200{d \u03b5} \u2192\n \u03b5 ectxt \u2192\n (\u03b5 \u2218\u2082 d) ectxt\n ECNEHole : \u2200{\u03b5 m u \u03c3} \u2192\n \u03b5 ectxt \u2192\n \u2987 \u03b5 \u2988\u27e8 u , \u03c3 , m \u27e9 ectxt\n ECCast : \u2200{ \u03b5 \u03c4 } \u2192\n \u03b5 ectxt \u2192\n (< \u03c4 > \u03b5 ) ectxt\n\n data _==_\u27e6_\u27e7 : (d : dhexp) (\u03b5 : ectx) (d' : dhexp) \u2192 Set where\n FRefl : \u2200{d : dhexp} \u2192\n d == \u2299 \u27e6 d \u27e7\n FLam : \u2200{ d d1 \u03b5 x \u03c4 } \u2192\n d1 == \u03b5 \u27e6 d \u27e7 \u2192\n (\u00b7\u03bb x [ \u03c4 ] d1) == (\u00b7\u03bb x [ \u03c4 ] \u03b5) \u27e6 d \u27e7\n FAp1 : \u2200{ d1 d2 d \u03b5} \u2192\n d1 final \u2192\n d2 == \u03b5 \u27e6 d \u27e7 \u2192\n (d1 \u2218 d2) == (d1 \u2218\u2081 \u03b5) \u27e6 d \u27e7\n FAp2 : \u2200{ d1 d2 d \u03b5} \u2192\n d2 == \u03b5 \u27e6 d \u27e7 \u2192\n (d1 \u2218 d2) == (\u03b5 \u2218\u2082 d2) \u27e6 d \u27e7\n FNEHole : \u2200{ d d1 \u03b5 u \u03c3 m} \u2192\n d1 == \u03b5 \u27e6 d \u27e7 \u2192\n \u2987 d1 \u2988\u27e8 (u , \u03c3 , m) \u27e9 == \u2987 \u03b5 \u2988\u27e8 (u , \u03c3 , m) \u27e9 \u27e6 d \u27e7\n FCast : \u2200{ d d1 \u03b5 \u03c4 } \u2192\n d1 == \u03b5 \u27e6 d \u27e7 \u2192\n (< \u03c4 > d1) == < \u03c4 > \u03b5 \u27e6 d \u27e7\n\n data _\u22a2_\u21a6_ : (\u0394 : hctx) (d d' : dhexp) \u2192 Set where\n Step : \u2200{ d d0 d' d0' \u0394 \u03b5} \u2192\n d == \u03b5 \u27e6 d0 \u27e7 \u2192\n \u0394 \u22a2 d0 \u2192> d0' \u2192 -- should this \u0394 be \u2205?\n d' == \u03b5 \u27e6 d0' \u27e7 \u2192 -- why is this the same \u03b5\n \u0394 \u22a2 d \u21a6 d\n","old_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n -- used to mark which dhexp holes have been evaluated\n data mark : Set where\n \u2713 : mark\n \u2717 : mark\n\n mutual\n subst : Set -- todo: no idea if this is right; mutual thing is weird\n subst = dhexp ctx\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 subst \u00d7 mark) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 subst \u00d7 mark) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n <_>_ : htyp \u2192 dhexp \u2192 dhexp\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows, sums, and products\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = htyp ctx\n\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n -- todo: this probably belongs in contexts, but need to abstract it.\n id : tctx \u2192 subst\n id ctx x with ctx x\n id ctx x | Some \u03c4 = Some (X x)\n id ctx x | None = None\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 (tctx \u00d7 htyp))\n u ::[ \u0393 ] \u03c4 = u , (\u0393 , \u03c4)\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {n : Nat} \u2192\n (n , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of n?\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192 -- todo: uniqueness of n?\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- todo: do we care about completeness of hexp or e-umlauts?\n -- those types without holes anywhere\n tcomplete : htyp \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (\u03c41 ==> \u03c42) = tcomplete \u03c41 \u00d7 tcomplete \u03c42\n\n -- those expressions without holes anywhere\n ecomplete : hexp \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: \u03c4) = ecomplete e1 \u00d7 tcomplete \u03c4\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ \u03c4 ] e) = tcomplete \u03c4 \u00d7 ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp1 : \u2200{\u0393 e1 e2 d2 d1 \u03941 \u03c41 \u03c42 \u03942} \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: bneed to think about disjointness and context rep\n \u0393 \u22a2 e1 => \u2987\u2988 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u2987\u2988) ~> d1 :: \u03c41 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u2987\u2988 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u2987\u2988 ~> (< \u03c42 ==> \u2987\u2988 > d1) \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESAp2 : \u2200{\u0393 e1 \u03c42 \u03c4 d1 d2 \u03941 \u03942 \u03c42' e2} \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: bneed to think about disjointness and context rep\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n (\u03c42 == \u03c42' \u2192 \u22a5) \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 (< \u03c42 > d2) \u22a3 (\u03941 \u222a \u03942)\n ESAp3 : \u2200{\u0393 e1 \u03c4 d1 \u03941 e2 \u03c42 d2 \u03942 } \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: bneed to think about disjointness and context rep\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , id \u0393 , \u2717 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , id \u0393 , \u2717 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc1 : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u03c4 == \u03c4' \u2192 \u22a5) \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> (< \u03c4 > d) \u22a3 \u0394\n ESAsc2 : \u2200{\u0393 e \u03c4 d \u0394 } \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4 \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c41 ==> \u03c42 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EALamHole : \u2200{\u0393 x e \u03c4 d \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u2987\u2988)) \u22a2 e \u21d0 \u2987\u2988 ~> d :: \u03c4 \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u2987\u2988 ~> \u00b7\u03bb x [ \u2987\u2988 ] d :: \u2987\u2988 ==> \u03c4 \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 (e == \u2987\u2988[ u ] \u2192 \u22a5)) \u2192\n ((e' : hexp) (u : Nat) \u2192 (e == \u2987 e' \u2988[ u ] \u2192 \u22a5)) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , id \u0393 , \u2717 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , id \u0393 , \u2717 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n mutual\n -- substitition type assignment\n _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 subst \u2192 tctx \u2192 Set\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' =\n (x : Nat) (d : dhexp) (xd\u2208\u03c3 : (x , d) \u2208 \u03c3) \u2192\n \u03a3[ \u03c4 \u2208 htyp ] (\u0393' x == Some \u03c4 \u00d7 \u0394 , \u0393 \u22a2 d :: \u03c4)\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c42 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u03c41 \u25b8arr (\u03c42 ==> \u03c4) \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4 m} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 , m \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 m} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 , m \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c4 \u03c4'} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0394 , \u0393 \u22a2 < \u03c4 > d :: \u03c4\n\n postulate -- todo: write this later\n [_]_ : subst \u2192 dhexp \u2192 dhexp\n\n -- value\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n mutual\n -- indeterminate\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 d1 indet \u2192 d2 final \u2192 (d1 \u2218 d2) indet\n ICast : \u2200{d \u03c4} \u2192 d indet \u2192 (< \u03c4 > d) indet\n\n -- final\n data _final : (d : dhexp) \u2192 Set where\n FVal : \u2200{d} \u2192 d val \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n------------- these two judgements are still being figured out; form\n------------- changing, etc. double check everything here once it settles\n------------- before doing anything with it\n -- error\n data _err[_] : (d : dhexp) \u2192 (\u0394 : hctx) \u2192 Set where\n -- ERNEHole\n -- ERCastError\n -- ERLam\n -- ERAp1\n -- ERAp2\n -- ERCast\n\n -- contextual dynamics\n\n -- evaluation contexts\n data ectx : Set where\n \u2299 : ectx\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 ectx \u2192 ectx\n _\u2218\u2081_ : dhexp \u2192 ectx \u2192 ectx\n _\u2218\u2082_ : ectx \u2192 dhexp \u2192 ectx\n \u2987_\u2988\u27e8_\u27e9 : ectx \u2192 (Nat \u00d7 subst \u00d7 mark) \u2192 ectx\n <_>_ : htyp \u2192 ectx \u2192 ectx\n\n -- instruction transition judgement\n data _\u22a2_\u2192>_ : (\u0394 : hctx) (d d' : dhexp) \u2192 Set where\n ITLam : \u2200{ \u0394 x \u03c4 d1 d2 } \u2192\n d2 final \u2192\n \u0394 \u22a2 ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u2192> ([ \u25a0 (x , d2) ] d1) -- this is very unlikely to work long term\n ITCast : \u2200{d \u0394 \u03c41 \u03c42 } \u2192\n d final \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c42 \u2192\n \u03c41 ~ \u03c42 \u2192 -- maybe?\n \u0394 \u22a2 < \u03c41 > d \u2192> d -- is that the right thing to step to?\n\n data _ectxt : (\u03b5 : ectx) \u2192 Set where\n ECDot : \u2299 ectxt\n ECLam : \u2200{\u03b5 x \u03c4} \u2192\n \u03b5 ectxt \u2192\n (\u00b7\u03bb x [ \u03c4 ] \u03b5) ectxt\n ECAp1 : \u2200{d \u03b5} \u2192\n d final \u2192\n \u03b5 ectxt \u2192\n (d \u2218\u2081 \u03b5) ectxt\n ECAp2 : \u2200{d \u03b5} \u2192\n \u03b5 ectxt \u2192\n (\u03b5 \u2218\u2082 d) ectxt\n ECNEHole : \u2200{\u03b5 m u \u03c3} \u2192\n \u03b5 ectxt \u2192\n (\u2987 \u03b5 \u2988\u27e8 u , \u03c3 , m \u27e9) ectxt\n ECCast : \u2200{ \u03b5 \u03c4 } \u2192\n \u03b5 ectxt \u2192\n (< \u03c4 > \u03b5 ) ectxt\n\n -- d is result of filling the hole in \u03b5 with d'\n data _==_[_] : (d : dhexp) (\u03b5 : ectx) (d' : dhexp) \u2192 Set where\n FRefl : \u2200{d : dhexp} \u2192\n d == \u2299 [ d ]\n -- FLam : \u2200{ d d1 \u03b5 x \u03c4 } \u2192\n -- d1 == \u03b5 [ d ] \u2192\n -- (\u00b7\u03bb x [ \u03c4 ] d1) == ?\n -- FAp1 :\n -- FAp2 :\n -- FNEHole : \u2200{ d d1 \u03b5 \u03c4 } \u2192\n -- d1 == \u03b5 [ d ] \u2192\n -- (\u2987 d1 \u2988\u27e8 u , \u03c3 , m\u27e9) == \u2987 d \u2988\u27e8 u , \u03c3 , m\u27e9\n FCast : \u2200{ d d1 \u03b5 \u03c4 } \u2192\n d1 == \u03b5 [ d ] \u2192\n (< \u03c4 > d1) == < \u03c4 > \u03b5 [ d ]\n\n data _\u22a2_\u21a6_ : (\u0394 : hctx) (d d' : dhexp) \u2192 Set where\n Step : \u2200{ d d0 d' d0' \u0394 \u03b5} \u2192\n d == \u03b5 [ d0 ] \u2192\n \u0394 \u22a2 d0 \u2192> d0' \u2192 -- should this \u0394 be \u2205?\n d' == \u03b5 [ d0' ] \u2192 -- why is this the same \u03b5\n \u0394 \u22a2 d \u21a6 d\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"26bbe5e6c76ff022da19d20c80116757a322d8e2","subject":"Control\/Protocol.agda","message":"Control\/Protocol.agda\n","repos":"crypto-agda\/protocols","old_file":"Control\/Protocol.agda","new_file":"Control\/Protocol.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP using (\u03a3; _\u00d7_; _,_) renaming (proj\u2081 to fst)\nopen import Data.One using (\ud835\udfd9)\nopen import Relation.Binary.PropositionalEquality.NP using (_\u2261_; !_; _\u2219_; refl; ap; coe; coe!)\nopen import Function.Extensionality\nopen import HoTT\nopen import Data.ShapePolymorphism\nopen Equivalences\n\nmodule Control.Protocol where\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\ndual\u1d35\u1d3c-involutive : \u2200 io \u2192 dual\u1d35\u1d3c (dual\u1d35\u1d3c io) \u2261 io\ndual\u1d35\u1d3c-involutive In = refl\ndual\u1d35\u1d3c-involutive Out = refl\n\ndual\u1d35\u1d3c-equiv : Is-equiv dual\u1d35\u1d3c\ndual\u1d35\u1d3c-equiv = self-inv-is-equiv dual\u1d35\u1d3c-involutive\n\ndual\u1d35\u1d3c-inj : \u2200 {x y} \u2192 dual\u1d35\u1d3c x \u2261 dual\u1d35\u1d3c y \u2192 x \u2261 y\ndual\u1d35\u1d3c-inj = Is-equiv.injective dual\u1d35\u1d3c-equiv\n\n{-\nmodule UniversalProtocols \u2113 {U : \u2605_(\u209b \u2113)}(U\u27e6_\u27e7 : U \u2192 \u2605_ \u2113) where\n-}\nmodule _ \u2113 where\n U = \u2605_ \u2113\n U\u27e6_\u27e7 = id\n data Proto_ : \u2605_(\u209b \u2113) where\n end : Proto_\n com : (io : InOut){M : U}(P : U\u27e6 M \u27e7 \u2192 Proto_) \u2192 Proto_\n{-\nmodule U\u2605 \u2113 = UniversalProtocols \u2113 {\u2605_ \u2113} id\nopen U\u2605\n-}\n\nProto : \u2605\u2081\nProto = Proto_ \u2080\nProto\u2080 = Proto\nProto\u2081 = Proto_ \u2081\n\npattern recv P = com In P\npattern send P = com Out P\n\nmodule _ {{_ : FunExt}} where\n com= : \u2200 {io\u2080 io\u2081}(io= : io\u2080 \u2261 io\u2081)\n {M\u2080 M\u2081}(M= : M\u2080 \u2261 M\u2081)\n {P\u2080 : M\u2080 \u2192 Proto}{P\u2081 : M\u2081 \u2192 Proto}(P= : \u2200 m\u2080 \u2192 P\u2080 m\u2080 \u2261 P\u2081 (coe M= m\u2080))\n \u2192 com io\u2080 P\u2080 \u2261 com io\u2081 P\u2081\n com= refl refl P= = ap (com _) (\u03bb= P=)\n\n module _ {io\u2080 io\u2081}(io= : io\u2080 \u2261 io\u2081)\n {M\u2080 M\u2081}(M\u2243 : M\u2080 \u2243 M\u2081)\n {P\u2080 : M\u2080 \u2192 Proto}{P\u2081 : M\u2081 \u2192 Proto}\n (P= : \u2200 m\u2080 \u2192 P\u2080 m\u2080 \u2261 P\u2081 (\u2013> M\u2243 m\u2080))\n {{_ : UA}} where\n com\u2243 : com io\u2080 P\u2080 \u2261 com io\u2081 P\u2081\n com\u2243 = com= io= (ua M\u2243) \u03bb m \u2192 P= m \u2219 ap P\u2081 (! coe-\u03b2 M\u2243 m)\n\n module _ io {M N}(P : M \u00d7 N \u2192 Proto)\n where\n com\u2082 : Proto\n com\u2082 = com io \u03bb m \u2192 com io \u03bb n \u2192 P (m , n)\n\n {- Proving this would be awesome...\n module _ io\n {M\u2080 M\u2081 N\u2080 N\u2081 : \u2605}\n (M\u00d7N\u2243 : (M\u2080 \u00d7 N\u2080) \u2243 (M\u2081 \u00d7 N\u2081))\n {P\u2080 : M\u2080 \u00d7 N\u2080 \u2192 Proto}{P\u2081 : M\u2081 \u00d7 N\u2081 \u2192 Proto}\n (P= : \u2200 m,n\u2080 \u2192 P\u2080 m,n\u2080 \u2261 P\u2081 (\u2013> M\u00d7N\u2243 m,n\u2080))\n {{_ : UA}} where\n com\u2082\u2243 : com\u2082 io P\u2080 \u2261 com\u2082 io P\u2081\n com\u2082\u2243 = {!com=!}\n -}\n\n send= = com= {Out} refl\n send\u2243 = com\u2243 {Out} refl\n recv= = com= {In} refl\n recv\u2243 = com\u2243 {In} refl\n\n com=\u2032 : \u2200 io {M}{P\u2080 P\u2081 : M \u2192 Proto}(P= : \u2200 m \u2192 P\u2080 m \u2261 P\u2081 m) \u2192 com io P\u2080 \u2261 com io P\u2081\n com=\u2032 io = com= refl refl\n\n send=\u2032 : \u2200 {M}{P\u2080 P\u2081 : M \u2192 Proto}(P= : \u2200 m \u2192 P\u2080 m \u2261 P\u2081 m) \u2192 send P\u2080 \u2261 send P\u2081\n send=\u2032 = send= refl\n\n recv=\u2032 : \u2200 {M}{P\u2080 P\u2081 : M \u2192 Proto}(P= : \u2200 m \u2192 P\u2080 m \u2261 P\u2081 m) \u2192 recv P\u2080 \u2261 recv P\u2081\n recv=\u2032 = recv= refl\n\npattern recvE M P = com In {M} P\npattern sendE M P = com Out {M} P\n\nrecv\u2610 : {M : \u2605}(P : ..(_ : M) \u2192 Proto) \u2192 Proto\nrecv\u2610 P = recv (\u03bb { [ m ] \u2192 P m })\n\nsend\u2610 : {M : \u2605}(P : ..(_ : M) \u2192 Proto) \u2192 Proto\nsend\u2610 P = send (\u03bb { [ m ] \u2192 P m })\n\n{-\ndual : Proto \u2192 Proto\ndual end = end\ndual (send P) = recv \u03bb m \u2192 dual (P m)\ndual (recv P) = send \u03bb m \u2192 dual (P m)\n-}\n\ndual : Proto \u2192 Proto\ndual end = end\ndual (com io P) = com (dual\u1d35\u1d3c io) \u03bb m \u2192 dual (P m)\n\nsource-of : Proto \u2192 Proto\nsource-of end = end\nsource-of (com _ P) = send \u03bb m \u2192 source-of (P m)\n\nsink-of : Proto \u2192 Proto\nsink-of = dual \u2218 source-of\n\ndata IsSource : Proto \u2192 \u2605\u2081 where\n end : IsSource end\n send' : \u2200 {M P} (PT : (m : M) \u2192 IsSource (P m)) \u2192 IsSource (send P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n recv' : \u2200 {M P} (PT : (m : M) \u2192 IsSink (P m)) \u2192 IsSink (recv P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 io {M P} (P\u2610 : \u2200 (m : \u2610 M) \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com io P)\n\nrecord End_ \u2113 : \u2605_ \u2113 where\n constructor end\n\nEnd : \u2200 {\u2113} \u2192 \u2605_ \u2113\nEnd = End_ _\n\nEnd-equiv : End \u2243 \ud835\udfd9\nEnd-equiv = equiv {\u2080} _ _ (\u03bb _ \u2192 refl) (\u03bb _ \u2192 refl)\n\nEnd-uniq : {{_ : UA}} \u2192 End \u2261 \ud835\udfd9\nEnd-uniq = ua End-equiv\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 \u2200{\u2113}(M : \u2605_ \u2113)(P : M \u2192 \u2605_ \u2113) \u2192 \u2605_ \u2113\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : \u2200 {\u2113} \u2192 Proto_ \u2113 \u2192 \u2605_ \u2113\n\u27e6 end \u27e7 = End\n\u27e6 com io P \u27e7 = \u27e6 io \u27e7\u1d35\u1d3c _ \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\nLog : Proto \u2192 \u2605\nLog P = \u27e6 source-of P \u27e7\n\nSink : Proto \u2192 \u2605\nSink P = \u27e6 sink-of P \u27e7\n\nsink : \u2200 P \u2192 Sink P\nsink end = _\nsink (com _ P) x = sink (P x)\n\ntelecom : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Log P\ntelecom end _ _ = _\ntelecom (recv P) p (m , q) = m , telecom (P m) (p m) q\ntelecom (send P) (m , p) q = m , telecom (P m) p (q m)\n\nsend\u2032 : \u2605 \u2192 Proto \u2192 Proto\nsend\u2032 M P = send \u03bb (_ : M) \u2192 P\n\nrecv\u2032 : \u2605 \u2192 Proto \u2192 Proto\nrecv\u2032 M P = recv \u03bb (_ : M) \u2192 P\n\nmodule _ {{_ : FunExt}} where\n dual-involutive : \u2200 P \u2192 dual (dual P) \u2261 P\n dual-involutive end = refl\n dual-involutive (com io P) = com= (dual\u1d35\u1d3c-involutive io) refl \u03bb m \u2192 dual-involutive (P m)\n\n dual-equiv : Is-equiv dual\n dual-equiv = self-inv-is-equiv dual-involutive\n\n dual-inj : \u2200 {P Q} \u2192 dual P \u2261 dual Q \u2192 P \u2261 Q\n dual-inj = Is-equiv.injective dual-equiv\n\n source-of-idempotent : \u2200 P \u2192 source-of (source-of P) \u2261 source-of P\n source-of-idempotent end = refl\n source-of-idempotent (com _ P) = com= refl refl \u03bb m \u2192 source-of-idempotent (P m)\n\n source-of-dual-oblivious : \u2200 P \u2192 source-of (dual P) \u2261 source-of P\n source-of-dual-oblivious end = refl\n source-of-dual-oblivious (com _ P) = com= refl refl \u03bb m \u2192 source-of-dual-oblivious (P m)\n\nmodule _ {{_ : FunExt}} where\n sink-contr : \u2200 P s \u2192 sink P \u2261 s\n sink-contr end s = refl\n sink-contr (com _ P) s = \u03bb= \u03bb m \u2192 sink-contr (P m) (s m)\n\n Sink-is-contr : \u2200 P \u2192 Is-contr (Sink P)\n Sink-is-contr P = sink P , sink-contr P\n\n \ud835\udfd9\u2243Sink : \u2200 P \u2192 \ud835\udfd9 \u2243 Sink P\n \ud835\udfd9\u2243Sink P = Is-contr-to-Is-equiv.\ud835\udfd9\u2243 (Sink-is-contr P)\n\n dual-Log : \u2200 P \u2192 Log (dual P) \u2261 Log P\n dual-Log = ap \u27e6_\u27e7 \u2218 source-of-dual-oblivious\n","old_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level.NP\nopen import Data.Product.NP renaming (map to \u00d7-map; proj\u2081 to fst; proj\u2082 to snd)\nopen import Data.Zero\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_]) hiding ([_,_]\u2032)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen import Data.LR\nopen Data.Two.Indexed\n\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; !_; _\u2219_; refl; subst; J; ap; coe; coe!; J-orig; _\u2262_)\n\nopen import Function.Extensionality\nopen import HoTT\nopen import Data.ShapePolymorphism\nopen Equivalences\n\nmodule Control.Protocol where\n\ndata InOut : \u2605 where\n In Out : InOut\n\ndual\u1d35\u1d3c : InOut \u2192 InOut\ndual\u1d35\u1d3c In = Out\ndual\u1d35\u1d3c Out = In\n\ndual\u1d35\u1d3c-involutive : \u2200 io \u2192 dual\u1d35\u1d3c (dual\u1d35\u1d3c io) \u2261 io\ndual\u1d35\u1d3c-involutive In = refl\ndual\u1d35\u1d3c-involutive Out = refl\n\ndual\u1d35\u1d3c-equiv : Is-equiv dual\u1d35\u1d3c\ndual\u1d35\u1d3c-equiv = self-inv-is-equiv dual\u1d35\u1d3c-involutive\n\ndual\u1d35\u1d3c-inj : \u2200 {x y} \u2192 dual\u1d35\u1d3c x \u2261 dual\u1d35\u1d3c y \u2192 x \u2261 y\ndual\u1d35\u1d3c-inj = Is-equiv.injective dual\u1d35\u1d3c-equiv\n\n{-\nmodule UniversalProtocols \u2113 {U : \u2605_(\u209b \u2113)}(U\u27e6_\u27e7 : U \u2192 \u2605_ \u2113) where\n-}\nmodule _ \u2113 where\n U = \u2605_ \u2113\n U\u27e6_\u27e7 = id\n data Proto_ : \u2605_(\u209b \u2113) where\n end : Proto_\n com : (io : InOut){M : U}(P : U\u27e6 M \u27e7 \u2192 Proto_) \u2192 Proto_\n{-\nmodule U\u2605 \u2113 = UniversalProtocols \u2113 {\u2605_ \u2113} id\nopen U\u2605\n-}\n\nProto : \u2605\u2081\nProto = Proto_ \u2080\nProto\u2080 = Proto\nProto\u2081 = Proto_ \u2081\n\npattern recv P = com In P\npattern send P = com Out P\n\nmodule _ {{_ : FunExt}} where\n com= : \u2200 io {M\u2080 M\u2081}(M= : M\u2080 \u2261 M\u2081)\n {P\u2080 : M\u2080 \u2192 Proto}{P\u2081 : M\u2081 \u2192 Proto}(P= : \u2200 m\u2080 \u2192 P\u2080 m\u2080 \u2261 P\u2081 (coe M= m\u2080))\n \u2192 com io P\u2080 \u2261 com io P\u2081\n com= io refl P= = ap (com io) (\u03bb= P=)\n\n module _ io {M\u2080 M\u2081}(M\u2243 : M\u2080 \u2243 M\u2081)\n {P\u2080 : M\u2080 \u2192 Proto}{P\u2081 : M\u2081 \u2192 Proto}\n (P= : \u2200 m\u2080 \u2192 P\u2080 m\u2080 \u2261 P\u2081 (\u2013> M\u2243 m\u2080))\n {{_ : UA}} where\n com\u2243 : com io P\u2080 \u2261 com io P\u2081\n com\u2243 = com= io (ua M\u2243) \u03bb m \u2192 P= m \u2219 ap P\u2081 (! coe-\u03b2 M\u2243 m)\n\n module _ io {M N}(P : M \u00d7 N \u2192 Proto)\n where\n com\u2082 : Proto\n com\u2082 = com io \u03bb m \u2192 com io \u03bb n \u2192 P (m , n)\n\n {- Proving this would be awesome...\n module _ io\n {M\u2080 M\u2081 N\u2080 N\u2081 : \u2605}\n (M\u00d7N\u2243 : (M\u2080 \u00d7 N\u2080) \u2243 (M\u2081 \u00d7 N\u2081))\n {P\u2080 : M\u2080 \u00d7 N\u2080 \u2192 Proto}{P\u2081 : M\u2081 \u00d7 N\u2081 \u2192 Proto}\n (P= : \u2200 m,n\u2080 \u2192 P\u2080 m,n\u2080 \u2261 P\u2081 (\u2013> M\u00d7N\u2243 m,n\u2080))\n {{_ : UA}} where\n com\u2082\u2243 : com\u2082 io P\u2080 \u2261 com\u2082 io P\u2081\n com\u2082\u2243 = {!com=!}\n -}\n\n -- send= : \u2200 {M\u2080 M\u2081}(M= : M\u2080 \u2261 M\u2081){P\u2080 : M\u2080 \u2192 Proto}{P\u2081 : M\u2081 \u2192 Proto}(P= : \u2200 m\u2080 \u2192 P\u2080 m\u2080 \u2261 P\u2081 (coe M= m\u2080)) \u2192 send P\u2080 \u2261 send P\u2081\n send= = com= Out\n send\u2243 = com\u2243 Out\n\n -- recv= : \u2200 {M\u2080 M\u2081}(M= : M\u2080 \u2261 M\u2081){P\u2080 : M\u2080 \u2192 Proto}{P\u2081 : M\u2081 \u2192 Proto}(P= : \u2200 m\u2080 \u2192 P\u2080 m\u2080 \u2261 P\u2081 (coe M= m\u2080)) \u2192 recv P\u2080 \u2261 recv P\u2081\n recv= = com= In\n recv\u2243 = com\u2243 In\n\n com=\u2032 : \u2200 io {M}{P\u2080 P\u2081 : M \u2192 Proto}(P= : \u2200 m \u2192 P\u2080 m \u2261 P\u2081 m) \u2192 com io P\u2080 \u2261 com io P\u2081\n com=\u2032 io = com= io refl\n\n send=\u2032 : \u2200 {M}{P\u2080 P\u2081 : M \u2192 Proto}(P= : \u2200 m \u2192 P\u2080 m \u2261 P\u2081 m) \u2192 send P\u2080 \u2261 send P\u2081\n send=\u2032 = send= refl\n\n recv=\u2032 : \u2200 {M}{P\u2080 P\u2081 : M \u2192 Proto}(P= : \u2200 m \u2192 P\u2080 m \u2261 P\u2081 m) \u2192 recv P\u2080 \u2261 recv P\u2081\n recv=\u2032 = recv= refl\n\npattern recvE M P = com In {M} P\npattern sendE M P = com Out {M} P\n\nmodule ProtoRel (_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605) where\n infix 0 _\u2248\u1d3e_\n data _\u2248\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : end \u2248\u1d3e end\n com : \u2200 {io\u2080 io\u2081} (io : io\u2080 \u2248\u1d35\u1d3c io\u2081){M P Q} \u2192 (\u2200 (m : M) \u2192 P m \u2248\u1d3e Q m) \u2192 com io\u2080 P \u2248\u1d3e com io\u2081 Q\n\nmodule ProtoRelImplicit {_\u2248\u1d35\u1d3c_ : InOut \u2192 InOut \u2192 \u2605} = ProtoRel _\u2248\u1d35\u1d3c_\nopen ProtoRelImplicit hiding (_\u2248\u1d3e_)\nopen ProtoRel _\u2261_ public renaming (_\u2248\u1d3e_ to _\u2261\u1d3e_) using ()\n\ndata View-\u2261\u1d3e : (P Q : Proto) \u2192 P \u2261\u1d3e Q \u2192 \u2605\u2081 where\n end : View-\u2261\u1d3e end end end\n \u2261-\u03a3 : \u2200 {M P Q} (p\u2261q : (m : M) \u2192 P m \u2261\u1d3e Q m) \u2192 View-\u2261\u1d3e (send P) (send Q) (com refl p\u2261q)\n \u2261-\u03a0 : \u2200 {M P Q} (p\u2261q : (m : M) \u2192 P m \u2261\u1d3e Q m) \u2192 View-\u2261\u1d3e (recv P) (recv Q) (com refl p\u2261q)\n\nview-\u2261\u1d3e : \u2200 {P Q} (p\u2261q : P \u2261\u1d3e Q) \u2192 View-\u2261\u1d3e P Q p\u2261q\nview-\u2261\u1d3e end = end\nview-\u2261\u1d3e (com {In} refl _) = \u2261-\u03a0 _\nview-\u2261\u1d3e (com {Out} refl _) = \u2261-\u03a3 _\n\nrecv\u2610 : {M : \u2605}(P : ..(_ : M) \u2192 Proto) \u2192 Proto\nrecv\u2610 P = recv (\u03bb { [ m ] \u2192 P m })\n\nsend\u2610 : {M : \u2605}(P : ..(_ : M) \u2192 Proto) \u2192 Proto\nsend\u2610 P = send (\u03bb { [ m ] \u2192 P m })\n\nsource-of : Proto \u2192 Proto\nsource-of end = end\nsource-of (com _ P) = send \u03bb m \u2192 source-of (P m)\n\n{-\ndual : Proto \u2192 Proto\ndual end = end\ndual (send P) = recv \u03bb m \u2192 dual (P m)\ndual (recv P) = send \u03bb m \u2192 dual (P m)\n-}\n\ndual : Proto \u2192 Proto\ndual end = end\ndual (com io P) = com (dual\u1d35\u1d3c io) \u03bb m \u2192 dual (P m)\n\ndata IsSource : Proto \u2192 \u2605\u2081 where\n end : IsSource end\n com : \u2200 {M P} (PT : (m : M) \u2192 IsSource (P m)) \u2192 IsSource (send P)\n\ndata IsSink : Proto \u2192 \u2605\u2081 where\n end : IsSink end\n com : \u2200 {M P} (PT : (m : M) \u2192 IsSink (P m)) \u2192 IsSink (recv P)\n\ndata Proto\u2610 : Proto \u2192 \u2605\u2081 where\n end : Proto\u2610 end\n com : \u2200 q {M P} (P\u2610 : \u2200 (m : \u2610 M) \u2192 Proto\u2610 (P m)) \u2192 Proto\u2610 (com q P)\n\nrecord End_ \u2113 : \u2605_ \u2113 where\n constructor end\n\nEnd : \u2200 {\u2113} \u2192 \u2605_ \u2113\nEnd = End_ _\n\nmodule _ {{_ : UA}} where\n End-uniq : End \u2261 \ud835\udfd9\n End-uniq = ua (equiv _ _ (\u03bb _ \u2192 refl) (\u03bb _ \u2192 refl))\n\n\u27e6_\u27e7\u1d35\u1d3c : InOut \u2192 \u2200{\u2113}(M : \u2605_ \u2113)(P : M \u2192 \u2605_ \u2113) \u2192 \u2605_ \u2113\n\u27e6_\u27e7\u1d35\u1d3c In = \u03a0\n\u27e6_\u27e7\u1d35\u1d3c Out = \u03a3\n\n\u27e6_\u27e7 : \u2200 {\u2113} \u2192 Proto_ \u2113 \u2192 \u2605_ \u2113\n\u27e6 end \u27e7 = End\n\u27e6 com q P \u27e7 = \u27e6 q \u27e7\u1d35\u1d3c _ \u03bb m \u2192 \u27e6 P m \u27e7\n\n\u27e6_\u22a5\u27e7 : Proto \u2192 \u2605\n\u27e6 P \u22a5\u27e7 = \u27e6 dual P \u27e7\n\n\u211b\u27e6_\u27e7 : \u2200{\u2113}(P : Proto_ \u2113) (p q : \u27e6 P \u27e7) \u2192 \u2605_ \u2113\n\u211b\u27e6 end \u27e7 p q = End\n\u211b\u27e6 recv P \u27e7 p q = \u2200 m \u2192 \u211b\u27e6 P m \u27e7 (p m) (q m)\n\u211b\u27e6 send P \u27e7 p q = \u03a3 (fst p \u2261 fst q) \u03bb e \u2192 \u211b\u27e6 P (fst q) \u27e7 (subst (\u27e6_\u27e7 \u2218 P) e (snd p)) (snd q)\n\n\u211b\u27e6_\u27e7-refl : \u2200 {\u2113}(P : Proto_ \u2113) \u2192 Reflexive \u211b\u27e6 P \u27e7\n\u211b\u27e6 end \u27e7-refl = end\n\u211b\u27e6 recv P \u27e7-refl = \u03bb m \u2192 \u211b\u27e6 P m \u27e7-refl\n\u211b\u27e6 send P \u27e7-refl {x} = refl , \u211b\u27e6 P (fst x) \u27e7-refl\n\n\u211b\u27e6_\u27e7-sym : \u2200 {\u2113}(P : Proto_ \u2113) \u2192 Symmetric \u211b\u27e6 P \u27e7\n\u211b\u27e6 end \u27e7-sym p = end\n\u211b\u27e6 recv P \u27e7-sym p = \u03bb m \u2192 \u211b\u27e6 P m \u27e7-sym (p m)\n\u211b\u27e6 send P \u27e7-sym (refl , q) = refl , \u211b\u27e6 P _ \u27e7-sym q -- TODO HoTT\n\n\u211b\u27e6_\u27e7-trans : \u2200 {\u2113}(P : Proto_ \u2113) \u2192 Transitive \u211b\u27e6 P \u27e7\n\u211b\u27e6 end \u27e7-trans p q = end\n\u211b\u27e6 recv P \u27e7-trans p q = \u03bb m \u2192 \u211b\u27e6 P m \u27e7-trans (p m) (q m)\n\u211b\u27e6 send P \u27e7-trans (refl , p) (refl , q) = refl , \u211b\u27e6 P _ \u27e7-trans p q -- TODO HoTT\n\nsend\u2032 : \u2605 \u2192 Proto \u2192 Proto\nsend\u2032 M P = send \u03bb (_ : M) \u2192 P\n\nrecv\u2032 : \u2605 \u2192 Proto \u2192 Proto\nrecv\u2032 M P = recv \u03bb (_ : M) \u2192 P\n\nmodule send\/recv-\ud835\udfd8 (P : \ud835\udfd8 \u2192 Proto){{_ : FunExt}}{{_ : UA}} where\n P\u22a4 : Proto\n P\u22a4 = recvE \ud835\udfd8 P\n\n P0 : Proto\n P0 = sendE \ud835\udfd8 P\n\n P0-empty : \u27e6 P0 \u27e7 \u2261 \ud835\udfd8\n P0-empty = ua (equiv fst (\u03bb()) (\u03bb()) (\u03bb { (() , _) }))\n\n P\u22a4-uniq : \u27e6 P\u22a4 \u27e7 \u2261 \ud835\udfd9\n P\u22a4-uniq = \u03a0\ud835\udfd8-uniq _\n\nopen send\/recv-\ud835\udfd8 (\u03bb _ \u2192 end) public\n\n\u2261\u1d3e-refl : \u2200 P \u2192 P \u2261\u1d3e P\n\u2261\u1d3e-refl end = end\n\u2261\u1d3e-refl (com q P) = com refl \u03bb m \u2192 \u2261\u1d3e-refl (P m)\n\n\u2261\u1d3e-reflexive : \u2200 {P Q} \u2192 P \u2261 Q \u2192 P \u2261\u1d3e Q\n\u2261\u1d3e-reflexive refl = \u2261\u1d3e-refl _\n\n\u2261\u1d3e-sym : Symmetric _\u2261\u1d3e_\n\u2261\u1d3e-sym end = end\n\u2261\u1d3e-sym (com refl r) = com refl \u03bb m \u2192 \u2261\u1d3e-sym (r m)\n\n\u2261\u1d3e-trans : Transitive _\u2261\u1d3e_\n\u2261\u1d3e-trans end qr = qr\n\u2261\u1d3e-trans (com refl x) (com refl x\u2081) = com refl (\u03bb m \u2192 \u2261\u1d3e-trans (x m) (x\u2081 m))\n\n!\u1d3e = \u2261\u1d3e-sym\n_\u2219\u1d3e_ = \u2261\u1d3e-trans\n\ndual-involutive : \u2200 P \u2192 dual (dual P) \u2261\u1d3e P\ndual-involutive end = end\ndual-involutive (com q P) = com (dual\u1d35\u1d3c-involutive q) \u03bb m \u2192 dual-involutive (P m)\n\nmodule _ {{_ : FunExt}} where\n \u2261\u1d3e-sound : \u2200 {P Q} \u2192 P \u2261\u1d3e Q \u2192 P \u2261 Q\n \u2261\u1d3e-sound end = refl\n \u2261\u1d3e-sound (com refl P\u2261Q) = ap (com _) (\u03bb= \u03bb m \u2192 \u2261\u1d3e-sound (P\u2261Q m))\n\n \u2261\u1d3e-cong : \u2200 {P Q} (f : Proto \u2192 Proto) \u2192 P \u2261\u1d3e Q \u2192 f P \u2261\u1d3e f Q\n \u2261\u1d3e-cong f P\u2261Q = \u2261\u1d3e-reflexive (ap f (\u2261\u1d3e-sound P\u2261Q))\n\n dual-equiv : Is-equiv dual\n dual-equiv = self-inv-is-equiv (\u2261\u1d3e-sound \u2218 dual-involutive)\n\n dual-inj : \u2200 {P Q} \u2192 dual P \u2261 dual Q \u2192 P \u2261 Q\n dual-inj = Is-equiv.injective dual-equiv\n\nsource-of-idempotent : \u2200 P \u2192 source-of (source-of P) \u2261\u1d3e source-of P\nsource-of-idempotent end = end\nsource-of-idempotent (com _ P) = com refl \u03bb m \u2192 source-of-idempotent (P m)\n\nsource-of-dual-oblivious : \u2200 P \u2192 source-of (dual P) \u2261\u1d3e source-of P\nsource-of-dual-oblivious end = end\nsource-of-dual-oblivious (com _ P) = com refl \u03bb m \u2192 source-of-dual-oblivious (P m)\n\nsink-of : Proto \u2192 Proto\nsink-of = dual \u2218 source-of\n\nSink : Proto \u2192 \u2605\nSink P = \u27e6 sink-of P \u27e7\n\nsink : \u2200 P \u2192 Sink P\nsink end = _\nsink (com _ P) x = sink (P x)\n\nmodule _ {{_ : FunExt}} where\n sink-contr : \u2200 P s \u2192 sink P \u2261 s\n sink-contr end s = refl\n sink-contr (com _ P) s = \u03bb= \u03bb m \u2192 sink-contr (P m) (s m)\n\n Sink-is-contr : \u2200 P \u2192 Is-contr (Sink P)\n Sink-is-contr P = sink P , sink-contr P\n\n \ud835\udfd9\u2243Sink : \u2200 P \u2192 \ud835\udfd9 \u2243 Sink P\n \ud835\udfd9\u2243Sink P = Is-contr-to-Is-equiv.\ud835\udfd9\u2243 (Sink-is-contr P)\n\nLog : Proto \u2192 \u2605\nLog P = \u27e6 source-of P \u27e7\n\n_>>=_ : (P : Proto) \u2192 (Log P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom q P >>= Q = com q \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\n++Log : \u2200 (P : Proto){Q : Log P \u2192 Proto} (xs : Log P) \u2192 Log (Q xs) \u2192 Log (P >>= Q)\n++Log end _ ys = ys\n++Log (com q P) (x , xs) ys = x , ++Log (P x) xs ys\n\n>>=-right-unit : \u2200 P \u2192 (P >> end) \u2261\u1d3e P\n>>=-right-unit end = end\n>>=-right-unit (com q P) = com refl \u03bb m \u2192 >>=-right-unit (P m)\n\n>>=-assoc : \u2200 (P : Proto)(Q : Log P \u2192 Proto)(R : Log (P >>= Q) \u2192 Proto)\n \u2192 P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Log P x y))) \u2261\u1d3e ((P >>= Q) >>= R)\n>>=-assoc end Q R = \u2261\u1d3e-refl (Q _ >>= R)\n>>=-assoc (com q P) Q R = com refl \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\ndata Accept? : \u2605 where\n accept reject : Accept?\ndata Is-accept : Accept? \u2192 \u2605 where\n accept : Is-accept accept\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n extend-send : Proto \u2192 Proto\n extend-send end = end\n extend-send (send P) = send [inl: (\u03bb m \u2192 extend-send (P m)) ,inr: A\u1d3e ]\n extend-send (recv P) = recv \u03bb m \u2192 extend-send (P m)\n\n extend-recv : Proto \u2192 Proto\n extend-recv end = end\n extend-recv (recv P) = recv [inl: (\u03bb m \u2192 extend-recv (P m)) ,inr: A\u1d3e ]\n extend-recv (send P) = send \u03bb m \u2192 extend-recv (P m)\n\nmodule _ {A : \u2605} (A\u1d3e : A \u2192 Proto) where\n dual-extend-send : \u2200 P \u2192 dual (extend-send A\u1d3e P) \u2261\u1d3e extend-recv (dual \u2218 A\u1d3e) (dual P)\n dual-extend-send end = end\n dual-extend-send (recv P) = com refl (\u03bb m \u2192 dual-extend-send (P m))\n dual-extend-send (send P) = com refl [inl: (\u03bb m \u2192 dual-extend-send (P m))\n ,inr: (\u03bb x \u2192 \u2261\u1d3e-refl (dual (A\u1d3e x))) ]\n\ndata Abort : \u2605 where abort : Abort\n\nAbort\u1d3e : Abort \u2192 Proto\nAbort\u1d3e _ = end\n\nadd-abort : Proto \u2192 Proto\nadd-abort = extend-send Abort\u1d3e\n\ntelecom : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u22a5\u27e7 \u2192 Log P\ntelecom end _ _ = _\ntelecom (recv P) p (m , q) = m , telecom (P m) (p m) q\ntelecom (send P) (m , p) q = m , telecom (P m) p (q m)\n\nlift\u1d3e : \u2200 a {\u2113} \u2192 Proto_ \u2113 \u2192 Proto_ (a \u2294 \u2113)\nlift\u1d3e a end = end\nlift\u1d3e a (com io P) = com io \u03bb m \u2192 lift\u1d3e a (P (lower {\u2113 = a} m))\n\nlift-proc : \u2200 a {\u2113} (P : Proto_ \u2113) \u2192 \u27e6 P \u27e7 \u2192 \u27e6 lift\u1d3e a P \u27e7\nlift-proc a {\u2113} end end = end\nlift-proc a {\u2113} (send P) (m , p) = lift m , lift-proc a (P m) p\nlift-proc a {\u2113} (recv P) p = \u03bb { (lift m) \u2192 lift-proc _ (P m) (p m) }\n\nmodule MonomorphicSky (P : Proto\u2080) where\n Cloud : Proto\u2080\n Cloud = recv \u03bb (t : \u27e6 P \u27e7) \u2192\n recv \u03bb (u : \u27e6 P \u22a5\u27e7) \u2192\n send \u03bb (log : Log P) \u2192\n end\n cloud : \u27e6 Cloud \u27e7\n cloud t u = telecom P t u , _\n\nmodule PolySky where\n Cloud : Proto_ \u2081\n Cloud = recv \u03bb (P : Proto\u2080) \u2192\n lift\u1d3e \u2081 (MonomorphicSky.Cloud P)\n cloud : \u27e6 Cloud \u27e7\n cloud P = lift-proc \u2081 (MonomorphicSky.Cloud P) (MonomorphicSky.cloud P)\n\nmodule PolySky' where\n Cloud : Proto_ \u2081\n Cloud = recv \u03bb (P : Proto\u2080) \u2192\n recv \u03bb (t : Lift \u27e6 P \u27e7) \u2192\n recv \u03bb (u : Lift \u27e6 P \u22a5\u27e7) \u2192\n send \u03bb (log : Lift (Log P)) \u2192\n end\n cloud : \u27e6 Cloud \u27e7\n cloud P (lift t) (lift u) = lift (telecom P t u) , _\n\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n end : Choreo I\n\nmodule _ {I : \u2605} where \n _-[_]\u2192\u00f8\u204f_ : \u2200 (A : I)(M : \u2605)(\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n A -[ M ]\u2192\u00f8\u204f \u2102 = A -[ \u2610 M ]\u2192\u2605\u204f \u03bb { [ m ] \u2192 \u2102 m }\n\n _\/\/_ : (\u2102 : Choreo I) (p : I \u2192 \ud835\udfda) \u2192 Proto\n (A -[ M ]\u2192 B \u204f \u2102) \/\/ p = case p A\n 0: case p B\n 0: recv (\u03bb { [ m ] \u2192 \u2102 m \/\/ p })\n 1: recv (\u03bb m \u2192 \u2102 m \/\/ p)\n 1: send (\u03bb m \u2192 \u2102 m \/\/ p)\n (A -[ M ]\u2192\u2605\u204f \u2102) \/\/ p = com (case p A 0: In 1: Out) \u03bb m \u2192 \u2102 m \/\/ p\n end \/\/ p = end\n\n \u2102Observer : Choreo I \u2192 Proto\n \u2102Observer \u2102 = \u2102 \/\/ \u03bb _ \u2192 0\u2082\n\n \u2102Log : Choreo I \u2192 Proto\n \u2102Log \u2102 = \u2102 \/\/ \u03bb _ \u2192 1\u2082\n\n \u2102Log-IsSource : \u2200 \u2102 \u2192 IsSource (\u2102Log \u2102)\n \u2102Log-IsSource (A -[ M ]\u2192 B \u204f \u2102) = com \u03bb m \u2192 \u2102Log-IsSource (\u2102 m)\n \u2102Log-IsSource (A -[ M ]\u2192\u2605\u204f \u2102) = com \u03bb m \u2192 \u2102Log-IsSource (\u2102 m)\n \u2102Log-IsSource end = end\n\n \u2102Observer-IsSink : \u2200 \u2102 \u2192 IsSink (\u2102Observer \u2102)\n \u2102Observer-IsSink (A -[ M ]\u2192 B \u204f \u2102) = com \u03bb { [ m ] \u2192 \u2102Observer-IsSink (\u2102 m) }\n \u2102Observer-IsSink (A -[ M ]\u2192\u2605\u204f \u2102) = com \u03bb m \u2192 \u2102Observer-IsSink (\u2102 m)\n \u2102Observer-IsSink end = end\n\n data R : (p q r : \ud835\udfda) \u2192 \u2605 where\n R011 : R 0\u2082 1\u2082 1\u2082\n R101 : R 1\u2082 0\u2082 1\u2082\n R000 : R 0\u2082 0\u2082 0\u2082\n R\u00b0 : \u2200 {I : \u2605} (p q r : I \u2192 \ud835\udfda) \u2192 \u2605\n R\u00b0 p q r = \u2200 i \u2192 R (p i) (q i) (r i)\n\n module _ {p q r : I \u2192 \ud835\udfda} where\n choreo-merge : (\u2102 : Choreo I)(pqr : R\u00b0 p q r) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ q \u27e7 \u2192 \u27e6 \u2102 \/\/ r \u27e7\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A | p B | q B | r B | pqr B\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 | 0\u2082 | _ | _ | _ = m , choreo-merge (\u2102 m) pqr (\u2102p [ m ]) \u2102q\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 | _ | 0\u2082 | _ | _ = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | .0\u2082 | 1\u2082 | .1\u2082 | R011 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q m)\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 1\u2082 | .0\u2082 | .1\u2082 | R101 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q [ m ])\n choreo-merge (A -[ M ]\u2192 B \u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 | 0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb { [ m ] \u2192 choreo-merge (\u2102 m) pqr (\u2102p [ m ]) (\u2102q [ m ]) }\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q with p A | q A | r A | pqr A\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p (m , \u2102q) | .0\u2082 | 1\u2082 | .1\u2082 | R011 = m , choreo-merge (\u2102 m) pqr (\u2102p m) \u2102q\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr (m , \u2102p) \u2102q | 1\u2082 | .0\u2082 | .1\u2082 | R101 = m , choreo-merge (\u2102 m) pqr \u2102p (\u2102q m)\n choreo-merge (A -[ M ]\u2192\u2605\u204f \u2102) pqr \u2102p \u2102q | .0\u2082 | 0\u2082 | .0\u2082 | R000 = \u03bb m \u2192 choreo-merge (\u2102 m) pqr (\u2102p m) (\u2102q m)\n choreo-merge end pqr \u2102p \u2102q = _\n\n {-\n module _ {p q r pq qr pqr : I \u2192 \ud835\udfda} where\n choreo-merge-assoc : (\u2102 : Choreo I)(Rpqr : R\u00b0 p qr pqr)(Rqr : R\u00b0 q r qr)(Rpqr' : R\u00b0 pq r pqr)(Rpq : R\u00b0 p q pq) \u2192\n (\u2102p : \u27e6 \u2102 \/\/ p \u27e7) (\u2102q : \u27e6 \u2102 \/\/ q \u27e7) (\u2102r : \u27e6 \u2102 \/\/ r \u27e7)\n \u2192 choreo-merge \u2102 Rpqr \u2102p (choreo-merge \u2102 Rqr \u2102q \u2102r) \u2261 choreo-merge \u2102 Rpqr' (choreo-merge \u2102 Rpq \u2102p \u2102q) \u2102r\n choreo-merge-assoc = {!!}\n -}\n\n R-p-\u00acp-1 : \u2200 (p : I \u2192 \ud835\udfda) i \u2192 R (p i) (not (p i)) 1\u2082\n R-p-\u00acp-1 p i with p i\n R-p-\u00acp-1 p i | 1\u2082 = R101\n R-p-\u00acp-1 p i | 0\u2082 = R011\n\n choreo-bi : {p : I \u2192 \ud835\udfda}(\u2102 : Choreo I) \u2192 \u27e6 \u2102 \/\/ p \u27e7 \u2192 \u27e6 \u2102 \/\/ (not \u2218 p) \u27e7 \u2192 \u27e6 \u2102Log \u2102 \u27e7\n choreo-bi {p} \u2102 \u2102p \u2102\u00acp = choreo-merge \u2102 (R-p-\u00acp-1 p) \u2102p \u2102\u00acp\n\nchoreo2 : (\u2102 : Choreo \ud835\udfda) \u2192 \u27e6 \u2102 \/\/ id \u27e7 \u2192 \u27e6 \u2102 \/\/ not \u27e7 \u2192 \u27e6 \u2102Log \u2102 \u27e7\nchoreo2 = choreo-bi\n\nmodule Choreo3 where\n data \ud835\udfdb : \u2605 where\n 0\u2083 1\u2083 2\u2083 : \ud835\udfdb\n 0\u2083? 1\u2083? 2\u2083? : \ud835\udfdb \u2192 \ud835\udfda\n 0\u2083? 0\u2083 = 1\u2082\n 0\u2083? _ = 0\u2082\n 1\u2083? 1\u2083 = 1\u2082\n 1\u2083? _ = 0\u2082\n 2\u2083? 2\u2083 = 1\u2082\n 2\u2083? _ = 0\u2082\n\n choreo3 : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Log \u2102 \u27e7\n choreo3 (0\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 [ m ])\n choreo3 (0\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 [ m ]) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 [ m ])\n choreo3 (1\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 [ m ]) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192 0\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 [ m ]) p2\n choreo3 (2\u2083 -[ M ]\u2192 1\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 m) p2\n choreo3 (2\u2083 -[ M ]\u2192 2\u2083 \u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 [ m ]) (p1 [ m ]) p2\n choreo3 (0\u2083 -[ M ]\u2192\u2605\u204f \u2102) (m , p0) p1 p2 = m , choreo3 (\u2102 m) p0 (p1 m) (p2 m)\n choreo3 (1\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 (m , p1) p2 = m , choreo3 (\u2102 m) (p0 m) p1 (p2 m)\n choreo3 (2\u2083 -[ M ]\u2192\u2605\u204f \u2102) p0 p1 (m , p2) = m , choreo3 (\u2102 m) (p0 m) (p1 m) p2\n choreo3 end p0 p1 p2 = _\n\n choreo3' : (\u2102 : Choreo \ud835\udfdb) \u2192 \u27e6 \u2102 \/\/ 0\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 1\u2083? \u27e7 \u2192 \u27e6 \u2102 \/\/ 2\u2083? \u27e7 \u2192 \u27e6 \u2102Log \u2102 \u27e7\n choreo3' \u2102 p0 p1 p2 = choreo-merge \u2102 (R-p-\u00acp-1 0\u2083?) p0 (choreo-merge \u2102 R-1-2-\u00ac0 p1 p2)\n where R-1-2-\u00ac0 : \u2200 i \u2192 R (1\u2083? i) (2\u2083? i) (not (0\u2083? i))\n R-1-2-\u00ac0 0\u2083 = R000\n R-1-2-\u00ac0 1\u2083 = R101\n R-1-2-\u00ac0 2\u2083 = R011\n\n>>=-com : (P : Proto){Q : Log P \u2192 Proto}{R : Log P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Log P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-com end p0 p1 = _ , p0 , p1\n>>=-com (send P) (m , p0) p1 = first (_,_ m) (>>=-com (P m) p0 (p1 m))\n>>=-com (recv P) p0 (m , p1) = first (_,_ m) (>>=-com (P m) (p0 m) p1)\n\n>>-com : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Log P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-com P p q = >>=-com P p q\n\nmodule _ {{_ : FunExt}} where\n ap->>= : \u2200 P {Q\u2080 Q\u2081} \u2192 (\u2200 {log} \u2192 \u27e6 Q\u2080 log \u27e7 \u2261 \u27e6 Q\u2081 log \u27e7) \u2192 \u27e6 P >>= Q\u2080 \u27e7 \u2261 \u27e6 P >>= Q\u2081 \u27e7\n ap->>= end Q= = Q=\n ap->>= (send P) Q= = \u03a3=\u2032 _ \u03bb m \u2192 ap->>= (P m) Q=\n ap->>= (recv P) Q= = \u03a0=\u2032 _ \u03bb m \u2192 ap->>= (P m) Q=\n\nmodule _ {{_ : FunExt}}{{_ : UA}} where\n P2 = send\u2032 \ud835\udfda end\n\n 0\u2082\u22621\u2082 : 0\u2082 \u2262 1\u2082\n 0\u2082\u22621\u2082 ()\n\n \ud835\udfd8\u2262 : \u2200 {A} (x : A) \u2192 \ud835\udfd8 \u2262 A\n \ud835\udfd8\u2262 x e = coe! e x\n\n \ud835\udfd8\u2262\ud835\udfd9 : \ud835\udfd8 \u2262 \ud835\udfd9\n \ud835\udfd8\u2262\ud835\udfd9 = \ud835\udfd8\u2262 _\n\n \ud835\udfd8\u2262\ud835\udfda : \ud835\udfd8 \u2262 \ud835\udfda\n \ud835\udfd8\u2262\ud835\udfda = \ud835\udfd8\u2262 0\u2082\n\n\nmodule ClientServerV1 (Query : \u2605\u2080) (Resp : Query \u2192 \u2605\u2080) (P : Proto) where\n Client : \u2115 \u2192 Proto\n Client zero = P\n Client (suc n) = send \u03bb (q : Query) \u2192 recv \u03bb (r : Resp q) \u2192 Client n\n\n Server : \u2115 \u2192 Proto\n Server zero = P\n Server (suc n) = recv \u03bb (q : Query) \u2192 send \u03bb (r : Resp q) \u2192 Server n\n\nmodule ClientServerV2 (Query : \u2605\u2080) (Resp : Query \u2192 \u2605\u2080) where\n ClientRound ServerRound : Proto\n ClientRound = send \u03bb (q : Query) \u2192 recv \u03bb (r : Resp q) \u2192 end\n ServerRound = dual ClientRound\n\n Client Server : \u2115 \u2192 Proto\n Client n = replicate\u1d3e n ClientRound\n Server = dual \u2218 Client\n\n DynamicServer StaticServer : Proto\n DynamicServer = recv \u03bb n \u2192\n Server n\n StaticServer = send \u03bb n \u2192\n Server n\n\n module PureServer (serve : \u03a0 Query Resp) where\n server : \u2200 n \u2192 \u27e6 Server n \u27e7\n server zero = _\n server (suc n) q = serve q , server n\n\nmodule _ {{_ : FunExt}} where\n dual-Log : \u2200 P \u2192 Log (dual P) \u2261 Log P\n dual-Log P = ap \u27e6_\u27e7 (\u2261\u1d3e-sound (source-of-dual-oblivious P))\n\ndual->> : \u2200 P Q \u2192 dual (P >> Q) \u2261\u1d3e dual P >> dual Q\ndual->> end Q = \u2261\u1d3e-refl _\ndual->> (recv P) Q = com refl \u03bb m \u2192 dual->> (P m) Q\ndual->> (send P) Q = com refl \u03bb m \u2192 dual->> (P m) Q\n\n {- ohoh!\n dual->>= : \u2200 P (Q : Log P \u2192 Proto) \u2192 dual (P >>= Q) \u2261\u1d3e dual P >>= (dual \u2218 Q \u2218 coe (dual-Log P))\n dual->>= end Q = \u2261\u1d3e-refl _\n dual->>= (recv M P) Q = ProtoRel.com refl M (\u03bb m \u2192 {!dual->>= (P m) (Q \u2218 _,_ m)!})\n dual->>= (send M P) Q = ProtoRel.com refl M (\u03bb m \u2192 {!!})\n -}\n\nmodule _ {{_ : FunExt}} (P : Proto) where\n dual-replicate\u1d3e : \u2200 n \u2192 dual (replicate\u1d3e n P) \u2261\u1d3e replicate\u1d3e n (dual P)\n dual-replicate\u1d3e zero = end\n dual-replicate\u1d3e (suc n) = dual->> P (replicate\u1d3e n P) \u2219\u1d3e \u2261\u1d3e-cong (_>>_ (dual P)) (dual-replicate\u1d3e n)\n\n_\u2295_ : (l r : Proto) \u2192 Proto\nl \u2295 r = send [L: l R: r ]\n\n_&_ : (l r : Proto) \u2192 Proto\nl & r = recv [L: l R: r ]\n\nmodule _ {{_ : FunExt}} where\n dual-\u2295 : \u2200 {P Q} \u2192 dual (P \u2295 Q) \u2261 dual P & dual Q\n dual-\u2295 = recv=\u2032 [L: refl R: refl ]\n\n dual-& : \u2200 {P Q} \u2192 dual (P & Q) \u2261 dual P \u2295 dual Q\n dual-& = send=\u2032 [L: refl R: refl ]\n\nmodule _ {{_ : FunExt}}{{_ : UA}} where\n &-comm : \u2200 P Q \u2192 P & Q \u2261 Q & P\n &-comm P Q = recv\u2243 LR!-equiv [L: refl R: refl ]\n\n \u2295-comm : \u2200 P Q \u2192 P \u2295 Q \u2261 Q \u2295 P\n \u2295-comm P Q = send\u2243 LR!-equiv [L: refl R: refl ]\n\nmodule _ {P Q R S} where\n \u2295-map : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P \u2295 R \u27e7 \u2192 \u27e6 Q \u2295 S \u27e7\n \u2295-map f g (`L , pr) = `L , f pr\n \u2295-map f g (`R , pr) = `R , g pr\n\n &-map : (\u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7) \u2192 (\u27e6 R \u27e7 \u2192 \u27e6 S \u27e7) \u2192 \u27e6 P & R \u27e7 \u2192 \u27e6 Q & S \u27e7\n &-map f g p `L = f (p `L)\n &-map f g p `R = g (p `R)\n\nmodule _ {P Q} where\n \u2295\u2192\u228e : \u27e6 P \u2295 Q \u27e7 \u2192 \u27e6 P \u27e7 \u228e \u27e6 Q \u27e7\n \u2295\u2192\u228e (`L , p) = inl p\n \u2295\u2192\u228e (`R , q) = inr q\n\n \u228e\u2192\u2295 : \u27e6 P \u27e7 \u228e \u27e6 Q \u27e7 \u2192 \u27e6 P \u2295 Q \u27e7\n \u228e\u2192\u2295 (inl p) = `L , p\n \u228e\u2192\u2295 (inr q) = `R , q\n\n \u228e\u2192\u2295\u2192\u228e : \u2200 x \u2192 \u228e\u2192\u2295 (\u2295\u2192\u228e x) \u2261 x\n \u228e\u2192\u2295\u2192\u228e (`L , _) = refl\n \u228e\u2192\u2295\u2192\u228e (`R , _) = refl\n\n \u2295\u2192\u228e\u2192\u2295 : \u2200 x \u2192 \u2295\u2192\u228e (\u228e\u2192\u2295 x) \u2261 x\n \u2295\u2192\u228e\u2192\u2295 (inl _) = refl\n \u2295\u2192\u228e\u2192\u2295 (inr _) = refl\n\n \u2295\u2243\u228e : \u27e6 P \u2295 Q \u27e7 \u2243 (\u27e6 P \u27e7 \u228e \u27e6 Q \u27e7)\n \u2295\u2243\u228e = \u2295\u2192\u228e , record { linv = \u228e\u2192\u2295 ; is-linv = \u228e\u2192\u2295\u2192\u228e ; rinv = \u228e\u2192\u2295 ; is-rinv = \u2295\u2192\u228e\u2192\u2295 }\n\n \u2295\u2261\u228e : {{_ : UA}} \u2192 \u27e6 P \u2295 Q \u27e7 \u2261 (\u27e6 P \u27e7 \u228e \u27e6 Q \u27e7)\n \u2295\u2261\u228e = ua \u2295\u2243\u228e\n\n &\u2192\u00d7 : \u27e6 P & Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n &\u2192\u00d7 p = p `L , p `R\n\n \u00d7\u2192& : \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P & Q \u27e7\n \u00d7\u2192& (p , q) `L = p\n \u00d7\u2192& (p , q) `R = q\n\n &\u2192\u00d7\u2192& : \u2200 x \u2192 &\u2192\u00d7 (\u00d7\u2192& x) \u2261 x\n &\u2192\u00d7\u2192& (p , q) = refl\n\n module _ {{_ : FunExt}} where\n \u00d7\u2192&\u2192\u00d7 : \u2200 x \u2192 \u00d7\u2192& (&\u2192\u00d7 x) \u2261 x\n \u00d7\u2192&\u2192\u00d7 p = \u03bb= \u03bb { `L \u2192 refl ; `R \u2192 refl }\n\n &\u2243\u00d7 : \u27e6 P & Q \u27e7 \u2243 (\u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7)\n &\u2243\u00d7 = &\u2192\u00d7 , record { linv = \u00d7\u2192& ; is-linv = \u00d7\u2192&\u2192\u00d7 ; rinv = \u00d7\u2192& ; is-rinv = &\u2192\u00d7\u2192& }\n\n &\u2261\u00d7 : {{_ : UA}} \u2192 \u27e6 P & Q \u27e7 \u2261 (\u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7)\n &\u2261\u00d7 = ua &\u2243\u00d7\n\nmodule _ {{_ : FunExt}}{{_ : UA}} where\n P\u22a4-& : \u2200 P \u2192 \u27e6 P\u22a4 & P \u27e7 \u2261 \u27e6 P \u27e7\n P\u22a4-& P = &\u2261\u00d7 \u2219 ap (flip _\u00d7_ \u27e6 P \u27e7) P\u22a4-uniq \u2219 \u03a3\ud835\udfd9-snd\n\n P0-\u2295 : \u2200 P \u2192 \u27e6 P0 \u2295 P \u27e7 \u2261 \u27e6 P \u27e7\n P0-\u2295 P = \u2295\u2261\u228e \u2219 ap (flip _\u228e_ \u27e6 P \u27e7) \u03a3\ud835\udfd8-fst \u2219 \u228e-comm \u2219 ! \u228e\ud835\udfd8-inl\n\n &-assoc : \u2200 P Q R \u2192 \u27e6 P & (Q & R) \u27e7 \u2261 \u27e6 (P & Q) & R \u27e7\n &-assoc P Q R = &\u2261\u00d7 \u2219 (ap (_\u00d7_ \u27e6 P \u27e7) &\u2261\u00d7 \u2219 \u00d7-assoc \u2219 ap (flip _\u00d7_ \u27e6 R \u27e7) (! &\u2261\u00d7)) \u2219 ! &\u2261\u00d7\n\n \u2295-assoc : \u2200 P Q R \u2192 \u27e6 P \u2295 (Q \u2295 R) \u27e7 \u2261 \u27e6 (P \u2295 Q) \u2295 R \u27e7\n \u2295-assoc P Q R = \u2295\u2261\u228e \u2219 (ap (_\u228e_ \u27e6 P \u27e7) \u2295\u2261\u228e \u2219 \u228e-assoc \u2219 ap (flip _\u228e_ \u27e6 R \u27e7) (! \u2295\u2261\u228e)) \u2219 ! \u2295\u2261\u228e\n\nmodule _ where\n\n _\u214b_ : Proto \u2192 Proto \u2192 Proto\n end \u214b Q = Q\n recv P \u214b Q = recv \u03bb m \u2192 P m \u214b Q\n P \u214b end = P\n P \u214b recv Q = recv \u03bb m \u2192 P \u214b Q m\n send P \u214b send Q = send [inl: (\u03bb m \u2192 P m \u214b send Q)\n ,inr: (\u03bb n \u2192 send P \u214b Q n) ]\n\n module _ {{_ : FunExt}}{{_ : UA}} where\n -- absorption\n \u22a4-\u214b : \u2200 P \u2192 \u27e6 P\u22a4 \u214b P \u27e7\n \u22a4-\u214b P = \u03bb()\n\n _\u2297_ : Proto \u2192 Proto \u2192 Proto\n end \u2297 Q = Q\n send P \u2297 Q = send \u03bb m \u2192 P m \u2297 Q\n P \u2297 end = P\n P \u2297 send Q = send \u03bb m \u2192 P \u2297 Q m\n recv P \u2297 recv Q = recv [inl: (\u03bb m \u2192 P m \u2297 recv Q)\n ,inr: (\u03bb n \u2192 recv P \u2297 Q n) ]\n\n _-o_ : (P Q : Proto) \u2192 Proto\n P -o Q = dual P \u214b Q\n\n _o-o_ : (P Q : Proto) \u2192 Proto\n P o-o Q = (P -o Q) \u2297 (Q -o P)\n\n module _ {{_ : FunExt}} where\n \u2297-endR : \u2200 P \u2192 P \u2297 end \u2261 P\n \u2297-endR end = refl\n \u2297-endR (recv _) = refl\n \u2297-endR (send P) = send=\u2032 \u03bb m \u2192 \u2297-endR (P m)\n\n \u214b-endR : \u2200 P \u2192 P \u214b end \u2261 P\n \u214b-endR end = refl\n \u214b-endR (send _) = refl\n \u214b-endR (recv P) = recv=\u2032 \u03bb m \u2192 \u214b-endR (P m)\n\n module _ {{_ : FunExt}}{{_ : UA}} where\n \u2297-sendR : \u2200 P{M}(Q : M \u2192 Proto) \u2192 \u27e6 P \u2297 send Q \u27e7 \u2261 (\u03a3 M \u03bb m \u2192 \u27e6 P \u2297 Q m \u27e7)\n \u2297-sendR end Q = refl\n \u2297-sendR (recv P) Q = refl\n \u2297-sendR (send P) Q = (\u03a3=\u2032 _ \u03bb m \u2192 \u2297-sendR (P m) Q) \u2219 \u03a3\u03a3-comm\n\n \u2297-comm : \u2200 P Q \u2192 \u27e6 P \u2297 Q \u27e7 \u2261 \u27e6 Q \u2297 P \u27e7\n \u2297-comm end Q = ! ap \u27e6_\u27e7 (\u2297-endR Q)\n \u2297-comm (send P) Q = (\u03a3=\u2032 _ \u03bb m \u2192 \u2297-comm (P m) Q) \u2219 ! \u2297-sendR Q P\n \u2297-comm (recv P) end = refl\n \u2297-comm (recv P) (send Q) = \u03a3=\u2032 _ \u03bb m \u2192 \u2297-comm (recv P) (Q m)\n \u2297-comm (recv P) (recv Q) = \u03a0\u2243 \u228e-comm-equiv [inl: (\u03bb m \u2192 \u2297-comm (P m) (recv Q))\n ,inr: (\u03bb m \u2192 \u2297-comm (recv P) (Q m)) ]\n\n \u2297-assoc : \u2200 P Q R \u2192 P \u2297 (Q \u2297 R) \u2261 (P \u2297 Q) \u2297 R\n \u2297-assoc end Q R = refl\n \u2297-assoc (send P) Q R = send=\u2032 \u03bb m \u2192 \u2297-assoc (P m) Q R\n \u2297-assoc (recv P) end R = refl\n \u2297-assoc (recv P) (send Q) R = send=\u2032 \u03bb m \u2192 \u2297-assoc (recv P) (Q m) R\n \u2297-assoc (recv P) (recv Q) end = refl\n \u2297-assoc (recv P) (recv Q) (send R) = send=\u2032 \u03bb m \u2192 \u2297-assoc (recv P) (recv Q) (R m)\n \u2297-assoc (recv P) (recv Q) (recv R) = recv\u2243 \u228e-assoc-equiv\n \u03bb { (inl m) \u2192 \u2297-assoc (P m) (recv Q) (recv R)\n ; (inr (inl m)) \u2192 \u2297-assoc (recv P) (Q m) (recv R)\n ; (inr (inr m)) \u2192 \u2297-assoc (recv P) (recv Q) (R m) }\n\n\n \u214b-recvR : \u2200 P{M}(Q : M \u2192 Proto) \u2192 \u27e6 P \u214b recv Q \u27e7 \u2261 (\u03a0 M \u03bb m \u2192 \u27e6 P \u214b Q m \u27e7)\n \u214b-recvR end Q = refl\n \u214b-recvR (send P) Q = refl\n \u214b-recvR (recv P) Q = (\u03a0=\u2032 _ \u03bb m \u2192 \u214b-recvR (P m) Q) \u2219 \u03a0\u03a0-comm\n\n \u214b-comm : \u2200 P Q \u2192 \u27e6 P \u214b Q \u27e7 \u2261 \u27e6 Q \u214b P \u27e7\n \u214b-comm end Q = ! ap \u27e6_\u27e7 (\u214b-endR Q)\n \u214b-comm (recv P) Q = (\u03a0=\u2032 _ \u03bb m \u2192 \u214b-comm (P m) Q) \u2219 ! \u214b-recvR Q P\n \u214b-comm (send P) end = refl\n \u214b-comm (send P) (recv Q) = \u03a0=\u2032 _ \u03bb m \u2192 \u214b-comm (send P) (Q m)\n \u214b-comm (send P) (send Q) = \u03a3\u2243 \u228e-comm-equiv [inl: (\u03bb m \u2192 \u214b-comm (P m) (send Q))\n ,inr: (\u03bb m \u2192 \u214b-comm (send P) (Q m)) ]\n\n \u214b-assoc : \u2200 P Q R \u2192 P \u214b (Q \u214b R) \u2261 (P \u214b Q) \u214b R\n \u214b-assoc end Q R = refl\n \u214b-assoc (recv P) Q R = recv=\u2032 \u03bb m \u2192 \u214b-assoc (P m) Q R\n \u214b-assoc (send P) end R = refl\n \u214b-assoc (send P) (recv Q) R = recv=\u2032 \u03bb m \u2192 \u214b-assoc (send P) (Q m) R\n \u214b-assoc (send P) (send Q) end = refl\n \u214b-assoc (send P) (send Q) (recv R) = recv=\u2032 \u03bb m \u2192 \u214b-assoc (send P) (send Q) (R m)\n \u214b-assoc (send P) (send Q) (send R) = send\u2243 \u228e-assoc-equiv\n \u03bb { (inl m) \u2192 \u214b-assoc (P m) (send Q) (send R)\n ; (inr (inl m)) \u2192 \u214b-assoc (send P) (Q m) (send R)\n ; (inr (inr m)) \u2192 \u214b-assoc (send P) (send Q) (R m) }\n\n module _ {P Q R}{{_ : FunExt}} where\n dist-\u2297-\u2295\u2032 : (Q \u2295 R) \u2297 P \u2261 (Q \u2297 P) \u2295 (R \u2297 P)\n dist-\u2297-\u2295\u2032 = send=\u2032 [L: refl R: refl ]\n\n dist-\u214b-&\u2032 : (Q & R) \u214b P \u2261 (Q \u214b P) & (R \u214b P)\n dist-\u214b-&\u2032 = recv=\u2032 [L: refl R: refl ]\n\n module _ {{_ : UA}} where\n dist-\u2297-\u2295 : \u27e6 P \u2297 (Q \u2295 R) \u27e7 \u2261 \u27e6 (P \u2297 Q) \u2295 (P \u2297 R) \u27e7\n dist-\u2297-\u2295 = \u2297-comm P (Q \u2295 R)\n \u2219 ap \u27e6_\u27e7 dist-\u2297-\u2295\u2032\n \u2219 \u2295\u2261\u228e\n \u2219 \u228e= (\u2297-comm Q P) (\u2297-comm R P)\n \u2219 ! \u2295\u2261\u228e\n\n dist-\u214b-& : \u27e6 P \u214b (Q & R) \u27e7 \u2261 \u27e6 (P \u214b Q) & (P \u214b R) \u27e7\n dist-\u214b-& = \u214b-comm P (Q & R)\n \u2219 ap \u27e6_\u27e7 dist-\u214b-&\u2032\n \u2219 &\u2261\u00d7\n \u2219 \u00d7= (\u214b-comm Q P) (\u214b-comm R P)\n \u2219 ! &\u2261\u00d7\n\n -- P \u27e6\u2297\u27e7 Q \u2243 \u27e6 P \u2297 Q \u27e7\n -- but potentially more convenient\n _\u27e6\u2297\u27e7_ : Proto \u2192 Proto \u2192 \u2605\n end \u27e6\u2297\u27e7 Q = \u27e6 Q \u27e7\n send P \u27e6\u2297\u27e7 Q = \u2203 \u03bb m \u2192 P m \u27e6\u2297\u27e7 Q\n P \u27e6\u2297\u27e7 end = \u27e6 P \u27e7\n P \u27e6\u2297\u27e7 send Q = \u2203 \u03bb m \u2192 P \u27e6\u2297\u27e7 Q m\n recv P \u27e6\u2297\u27e7 recv Q = (\u03a0 _ \u03bb m \u2192 P m \u27e6\u2297\u27e7 recv Q)\n \u00d7 (\u03a0 _ \u03bb n \u2192 recv P \u27e6\u2297\u27e7 Q n)\n\n module _ {{_ : FunExt}}{{_ : UA}} where\n \u27e6\u2297\u27e7-correct : \u2200 P Q \u2192 P \u27e6\u2297\u27e7 Q \u2261 \u27e6 P \u2297 Q \u27e7\n \u27e6\u2297\u27e7-correct end Q = refl\n \u27e6\u2297\u27e7-correct (send P) Q = \u03a3=\u2032 _ \u03bb m \u2192 \u27e6\u2297\u27e7-correct (P m) Q\n \u27e6\u2297\u27e7-correct (recv P) end = refl\n \u27e6\u2297\u27e7-correct (recv P) (send Q) = \u03a3=\u2032 _ \u03bb n \u2192 \u27e6\u2297\u27e7-correct (recv P) (Q n)\n \u27e6\u2297\u27e7-correct (recv P) (recv Q) = ! dist-\u00d7-\u03a0\n \u2219 \u03a0=\u2032 (_ \u228e _) \u03bb { (inl m) \u2192 \u27e6\u2297\u27e7-correct (P m) (recv Q)\n ; (inr n) \u2192 \u27e6\u2297\u27e7-correct (recv P) (Q n) }\n\n -- an alternative, potentially more convenient\n _\u27e6\u214b\u27e7_ : Proto \u2192 Proto \u2192 \u2605\n end \u27e6\u214b\u27e7 Q = \u27e6 Q \u27e7\n recv P \u27e6\u214b\u27e7 Q = \u2200 m \u2192 P m \u27e6\u214b\u27e7 Q\n P \u27e6\u214b\u27e7 end = \u27e6 P \u27e7\n P \u27e6\u214b\u27e7 recv Q = \u2200 n \u2192 P \u27e6\u214b\u27e7 Q n\n send P \u27e6\u214b\u27e7 send Q = (\u2203 \u03bb m \u2192 P m \u27e6\u214b\u27e7 send Q)\n \u228e (\u2203 \u03bb n \u2192 send P \u27e6\u214b\u27e7 Q n)\n\n module _ {{_ : FunExt}}{{_ : UA}} where\n \u27e6\u214b\u27e7-correct : \u2200 P Q \u2192 P \u27e6\u214b\u27e7 Q \u2261 \u27e6 P \u214b Q \u27e7\n \u27e6\u214b\u27e7-correct end Q = refl\n \u27e6\u214b\u27e7-correct (recv P) Q = \u03a0=\u2032 _ \u03bb m \u2192 \u27e6\u214b\u27e7-correct (P m) Q\n \u27e6\u214b\u27e7-correct (send P) end = refl\n \u27e6\u214b\u27e7-correct (send P) (recv Q) = \u03a0=\u2032 _ \u03bb n \u2192 \u27e6\u214b\u27e7-correct (send P) (Q n)\n \u27e6\u214b\u27e7-correct (send P) (send Q) = ! dist-\u228e-\u03a3\n \u2219 \u03a3=\u2032 (_ \u228e _) \u03bb { (inl m) \u2192 \u27e6\u214b\u27e7-correct (P m) (send Q)\n ; (inr n) \u2192 \u27e6\u214b\u27e7-correct (send P) (Q n) }\n\n {-\n -- sends can be pulled out of tensor\n source->>=-\u2297 : \u2200 P Q R \u2192 (source-of P >>= Q) \u2297 R \u2261 source-of P >>= \u03bb log \u2192 (Q log \u2297 R)\n source->>=-\u2297 end Q R = refl\n source->>=-\u2297 (com _ P) Q R = send=\u2032 \u03bb m \u2192 source->>=-\u2297 (P m) (Q \u2218 _,_ m) R\n\n -- consequence[Q = const end]: \u2200 P R \u2192 source-of P \u2297 R \u2261 source-of P >> R\n\n -- recvs can be pulled out of par\n sink->>=-\u214b : \u2200 P Q R \u2192 (sink-of P >>= Q) \u214b R \u2261 sink-of P >>= \u03bb log \u2192 (Q log \u214b R)\n sink->>=-\u214b end Q R = refl\n sink->>=-\u214b (com _ P) Q R = recv=\u2032 \u03bb m \u2192 sink->>=-\u214b (P m) (Q \u2218 _,_ m) R\n\n -- consequence[Q = const end]: \u2200 P R \u2192 sink-of P \u214b R \u2261 sink-of P >> R\n\n Log-\u214b-\u00d7 : \u2200 {P Q} \u2192 Log (P \u214b Q) \u2192 Log P \u00d7 Log Q\n Log-\u214b-\u00d7 {end} {Q} q = end , q\n Log-\u214b-\u00d7 {recv P}{Q} (m , p) = first (_,_ m) $ Log-\u214b-\u00d7 {P m} {Q} p\n Log-\u214b-\u00d7 {send P}{end} (m , p) = (m , p) , end\n Log-\u214b-\u00d7 {send P}{recv Q} (m , p) = second (_,_ m) $ Log-\u214b-\u00d7 {send P} {Q m} p\n Log-\u214b-\u00d7 {send P}{send Q} (inl m , p) = first (_,_ m) $ Log-\u214b-\u00d7 {P m} {send Q} p\n Log-\u214b-\u00d7 {send P}{send Q} (inr m , p) = second (_,_ m) $ Log-\u214b-\u00d7 {send P} {Q m} p\n\n module _ {{_ : FunExt}} where\n \u2297\u214b-dual : \u2200 P Q \u2192 dual (P \u214b Q) \u2261 dual P \u2297 dual Q\n \u2297\u214b-dual end Q = refl\n \u2297\u214b-dual (recv P) Q = com=\u2032 _ \u03bb m \u2192 \u2297\u214b-dual (P m) _\n \u2297\u214b-dual (send P) end = refl\n \u2297\u214b-dual (send P) (recv Q) = com=\u2032 _ \u03bb n \u2192 \u2297\u214b-dual (send P) (Q n)\n \u2297\u214b-dual (send P) (send Q) = com=\u2032 _\n [inl: (\u03bb m \u2192 \u2297\u214b-dual (P m) (send Q))\n ,inr: (\u03bb n \u2192 \u2297\u214b-dual (send P) (Q n))\n ]\n\n data View-\u214b-proto : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end-X : \u2200 Q \u2192 View-\u214b-proto end Q\n recv-X : \u2200 {M}(P : M \u2192 Proto)Q \u2192 View-\u214b-proto (recv P) Q\n send-send : \u2200 {M N}(P : M \u2192 Proto)(Q : N \u2192 Proto) \u2192 View-\u214b-proto (send P) (send Q)\n send-recv : \u2200 {M N}(P : M \u2192 Proto)(Q : N \u2192 Proto) \u2192 View-\u214b-proto (send P) (recv Q)\n send-end : \u2200 {M}(P : M \u2192 Proto) \u2192 View-\u214b-proto (send P) end\n\n view-\u214b-proto : \u2200 P Q \u2192 View-\u214b-proto P Q\n view-\u214b-proto end Q = end-X Q\n view-\u214b-proto (recv P) Q = recv-X P Q\n view-\u214b-proto (send P) end = send-end P\n view-\u214b-proto (send P) (recv Q) = send-recv P Q\n view-\u214b-proto (send P) (send Q) = send-send P Q\n\n data View-\u2297-proto : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end-X : \u2200 Q \u2192 View-\u2297-proto end Q\n send-X : \u2200 {M}(P : M \u2192 Proto)Q \u2192 View-\u2297-proto (send P) Q\n recv-recv : \u2200 {M N}(P : M \u2192 Proto)(Q : N \u2192 Proto) \u2192 View-\u2297-proto (recv P) (recv Q)\n recv-send : \u2200 {M N}(P : M \u2192 Proto)(Q : N \u2192 Proto) \u2192 View-\u2297-proto (recv P) (send Q)\n recv-end : \u2200 {M}(P : M \u2192 Proto) \u2192 View-\u2297-proto (recv P) end\n\n view-\u2297-proto : \u2200 P Q \u2192 View-\u2297-proto P Q\n view-\u2297-proto end Q = end-X Q\n view-\u2297-proto (send P) Q = send-X P Q\n view-\u2297-proto (recv P) end = recv-end P\n view-\u2297-proto (recv P) (recv Q) = recv-recv P Q\n view-\u2297-proto (recv P) (send Q) = recv-send P Q\n\n -- the terminology used for the constructor follows the behavior of the combined process\n data View-\u214b : \u2200 P Q \u2192 \u27e6 P \u214b Q \u27e7 \u2192 \u2605\u2081 where\n sendL' : \u2200 {M N}(P : M \u2192 Proto)(Q : N \u2192 Proto)(m : M )(p : \u27e6 P m \u214b send Q \u27e7) \u2192 View-\u214b (send P) (send Q) (inl m , p)\n sendR' : \u2200 {M N}(P : M \u2192 Proto)(Q : N \u2192 Proto)(n : N)(p : \u27e6 send P \u214b Q n \u27e7) \u2192 View-\u214b (send P) (send Q) (inr n , p)\n recvL' : \u2200 {M} (P : M \u2192 Proto) Q (p : ((m : M) \u2192 \u27e6 P m \u214b Q \u27e7)) \u2192 View-\u214b (recv P) Q p\n recvR' : \u2200 {M N} (P : M \u2192 Proto) (Q : N \u2192 Proto)(p : (n : N) \u2192 \u27e6 send P \u214b Q n \u27e7) \u2192 View-\u214b (send P) (recv Q) p\n endL : \u2200 Q (p : \u27e6 Q \u27e7) \u2192 View-\u214b end Q p\n send' : \u2200 {M}(P : M \u2192 Proto)(m : M)(p : \u27e6 P m \u27e7) \u2192 View-\u214b (send P) end (m , p)\n\n view-\u214b : \u2200 P Q (p : \u27e6 P \u214b Q \u27e7) \u2192 View-\u214b P Q p\n view-\u214b end Q p = endL Q p\n view-\u214b (recv P) Q p = recvL' P Q p\n view-\u214b (send P) end (m , p) = send' P m p\n view-\u214b (send P) (recv Q) p = recvR' P Q p\n view-\u214b (send P) (send Q) (inl x , p) = sendL' P Q x p\n view-\u214b (send P) (send Q) (inr y , p) = sendR' P Q y p\n\n {-\n -- use coe (... \u214b-assoc P Q R)\n \u214b-assoc : \u2200 P Q R \u2192 \u27e6 P \u214b (Q \u214b R) \u27e7 \u2192 \u27e6 (P \u214b Q) \u214b R \u27e7\n \u214b-assoc end Q R s = s\n \u214b-assoc (recv P) Q R s m = \u214b-assoc (P m) _ _ (s m)\n \u214b-assoc (send P) end R s = s\n \u214b-assoc (send P) (recv Q) R s m = \u214b-assoc (send P) (Q m) _ (s m)\n \u214b-assoc (send P) (send Q) end s = s\n \u214b-assoc (send P) (send Q) (recv R) s m = \u214b-assoc (send P) (send Q) (R m) (s m)\n \u214b-assoc (send P) (send Q) (send R) (inl m , s) = inl (inl m) , \u214b-assoc (P m) (send Q) (send R) s\n \u214b-assoc (send P) (send Q) (send R) (inr (inl m) , s) = inl (inr m) , \u214b-assoc (send P) (Q m) (send R) s\n \u214b-assoc (send P) (send Q) (send R) (inr (inr m) , s) = inr m , \u214b-assoc (send P) (send Q) (R m) s\n\n -- use coe (\u214b-endR P) instead\n \u214b-rend : \u2200 P \u2192 \u27e6 P \u214b end \u27e7 \u2192 \u27e6 P \u27e7\n \u214b-rend end p = p\n \u214b-rend (send _) p = p\n \u214b-rend (recv P) p = \u03bb m \u2192 \u214b-rend (P m) (p m)\n\n -- use coe! (\u214b-endR P) instead\n \u214b-rend! : \u2200 P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 P \u214b end \u27e7\n \u214b-rend! end p = p\n \u214b-rend! (send _) p = p\n \u214b-rend! (recv P) p = \u03bb m \u2192 \u214b-rend! (P m) (p m)\n\n -- use coe! (\u214b-recvR P Q) instead\n \u214b-isendR : \u2200 {N} P Q \u2192 \u27e6 P \u214b recv Q \u27e7 \u2192 (n : N) \u2192 \u27e6 P \u214b Q n \u27e7\n \u214b-isendR end Q s n = s n\n \u214b-isendR (recv P) Q s n = \u03bb m \u2192 \u214b-isendR (P m) Q (s m) n\n \u214b-isendR (send P) Q s n = s n\n\n\n -- see \u214b-recvR\n \u214b-recvR : \u2200 {M} P Q \u2192 ((m : M) \u2192 \u27e6 P \u214b Q m \u27e7) \u2192 \u27e6 P \u214b recv Q \u27e7\n \u214b-recvR end Q s = s\n \u214b-recvR (recv P) Q s = \u03bb x \u2192 \u214b-recvR (P x) Q (\u03bb m \u2192 s m x)\n \u214b-recvR (send P) Q s = s\n -}\n\n module _ {{_ : FunExt}}{{_ : UA}} where\n\n \u214b-sendR : \u2200 {M}P{Q : M \u2192 Proto}(m : M) \u2192 \u27e6 P \u214b Q m \u27e7 \u2192 \u27e6 P \u214b send Q \u27e7\n \u214b-sendR end m p = m , p\n \u214b-sendR (send P) m p = inr m , p\n \u214b-sendR (recv P) m p = \u03bb x \u2192 \u214b-sendR (P x) m (p x)\n\n \u214b-sendL : \u2200 {M}{P : M \u2192 Proto} Q (m : M) \u2192 \u27e6 P m \u214b Q \u27e7 \u2192 \u27e6 send P \u214b Q \u27e7\n \u214b-sendL {M} {P} Q m pq = coe (\u214b-comm Q (send P)) (\u214b-sendR Q m (coe (\u214b-comm (P m) Q) pq))\n\n \u214b-id : \u2200 P \u2192 \u27e6 dual P \u214b P \u27e7\n \u214b-id end = end\n \u214b-id (recv P) = \u03bb x \u2192 \u214b-sendL (P x) x (\u214b-id (P x))\n \u214b-id (send P) = \u03bb x \u2192 \u214b-sendR (dual (P x)) x (\u214b-id (P x))\n\n data View-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b Q \u27e7 \u2192 \u27e6 dual Q \u214b R \u27e7 \u2192 \u2605\u2081 where\n sendLL : \u2200 {M N}(P : M \u2192 Proto)(Q : N \u2192 Proto) R (m : M)(p : \u27e6 P m \u214b send Q \u27e7)(q : \u27e6 dual (send Q) \u214b R \u27e7)\n \u2192 View-\u2218 (send P) (send Q) R (inl m , p) q\n recvLL : \u2200 {M} (P : M \u2192 Proto) Q R\n (p : ((m : M) \u2192 \u27e6 P m \u214b Q \u27e7))(q : \u27e6 dual Q \u214b R \u27e7)\n \u2192 View-\u2218 (recv P) Q R p q\n recvR-sendR : \u2200 {MP MQ MR}ioP(P : MP \u2192 Proto)(Q : MQ \u2192 Proto)(R : MR \u2192 Proto)\n (mR : MR)(p : \u27e6 com ioP P \u214b recv Q \u27e7)(q : \u27e6 dual (recv Q) \u214b R mR \u27e7)\n \u2192 View-\u2218 (com ioP P) (recv Q) (send R) p (inr mR , q)\n\n recvRR : \u2200 {MP MQ MR}(P : MP \u2192 Proto)(Q : MQ \u2192 Proto)(R : MR \u2192 Proto)\n (p : \u27e6 send P \u214b recv Q \u27e7)(q : (m : MR) \u2192 \u27e6 dual (recv Q) \u214b R m \u27e7)\n \u2192 View-\u2218 (send P) (recv Q) (recv R) p q\n sendR-recvL : \u2200 {MP MQ}(P : MP \u2192 Proto)(Q : MQ \u2192 Proto)R(m : MQ)\n (p : \u27e6 send P \u214b Q m \u27e7)(q : (m : MQ) \u2192 \u27e6 dual (Q m) \u214b R \u27e7)\n \u2192 View-\u2218 (send P) (send Q) R (inr m , p) q\n recvR-sendL : \u2200 {MP MQ MR}(P : MP \u2192 Proto)(Q : MQ \u2192 Proto)(R : MR \u2192 Proto)\n (p : (m : MQ) \u2192 \u27e6 send P \u214b Q m \u27e7)(m : MQ)(q : \u27e6 dual (Q m) \u214b send R \u27e7)\n \u2192 View-\u2218 (send P) (recv Q) (send R) p (inl m , q)\n endL : \u2200 Q R\n \u2192 (q : \u27e6 Q \u27e7)(qr : \u27e6 dual Q \u214b R \u27e7)\n \u2192 View-\u2218 end Q R q qr\n sendLM : \u2200 {MP}(P : MP \u2192 Proto)R\n (m : MP)(p : \u27e6 P m \u27e7)(r : \u27e6 R \u27e7)\n \u2192 View-\u2218 (send P) end R (m , p) r\n recvL-sendR : \u2200 {MP MQ}(P : MP \u2192 Proto)(Q : MQ \u2192 Proto)\n (m : MQ)(p : \u2200 m \u2192 \u27e6 send P \u214b Q m \u27e7)(q : \u27e6 dual (Q m) \u27e7)\n \u2192 View-\u2218 (send P) (recv Q) end p (m , q)\n\n view-\u2218 : \u2200 P Q R (pq : \u27e6 P \u214b Q \u27e7)(qr : \u27e6 dual Q \u214b R \u27e7) \u2192 View-\u2218 P Q R pq qr\n view-\u2218 P Q R pq qr = view-\u2218-view (view-\u214b P Q pq) (view-\u214b (dual Q) R qr)\n where\n view-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b Q \u27e7}{qr : \u27e6 dual Q \u214b R \u27e7} \u2192 View-\u214b P Q pq \u2192 View-\u214b (dual Q) R qr \u2192 View-\u2218 P Q R pq qr\n view-\u2218-view (sendL' _ _ _ _) _ = sendLL _ _ _ _ _ _\n view-\u2218-view (recvL' _ _ _) _ = recvLL _ _ _ _ _\n view-\u2218-view (sendR' _ _ _ _) _ = sendR-recvL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendL' ._ _ _ _) = recvR-sendL _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (sendR' ._ _ _ _) = recvR-sendR _ _ _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (recvR' ._ _ _) = recvRR _ _ _ _ _\n view-\u2218-view (recvR' _ _ _) (send' ._ _ _) = recvL-sendR _ _ _ _ _\n view-\u2218-view (endL _ _) _ = endL _ _ _ _\n view-\u2218-view (send' _ _ _) _ = sendLM _ _ _ _ _\n\n \u214b-apply : \u2200 P Q \u2192 \u27e6 P \u214b Q \u27e7 \u2192 \u27e6 dual P \u27e7 \u2192 \u27e6 Q \u27e7\n \u214b-apply end Q s p = s\n \u214b-apply (recv P) Q s (m , p) = \u214b-apply (P m) Q (s m) p\n \u214b-apply (send P) end s p = _\n \u214b-apply (send P) (recv Q) s p n = \u214b-apply (send P) (Q n) (s n) p\n \u214b-apply (send P) (send Q) (inl m , s) p = \u214b-apply (P m) (send Q) s (p m)\n \u214b-apply (send P) (send Q) (inr m , s) p = m , \u214b-apply (send P) (Q m) s p\n\n {-\n -- see dist-\u214b-&\n dist-\u214b-fst : \u2200 P Q R \u2192 \u27e6 P \u214b (Q & R) \u27e7 \u2192 \u27e6 P \u214b Q \u27e7\n dist-\u214b-fst (recv P) Q R p = \u03bb m \u2192 dist-\u214b-fst (P m) Q R (p m)\n dist-\u214b-fst (send P) Q R p = p `L\n dist-\u214b-fst end Q R p = p `L\n\n -- see dist-\u214b-&\n dist-\u214b-snd : \u2200 P Q R \u2192 \u27e6 P \u214b (Q & R) \u27e7 \u2192 \u27e6 P \u214b R \u27e7\n dist-\u214b-snd (recv P) Q R p = \u03bb m \u2192 dist-\u214b-snd (P m) Q R (p m)\n dist-\u214b-snd (send P) Q R p = p `R\n dist-\u214b-snd end Q R p = p `R\n\n -- see dist-\u214b-&\n dist-\u214b-\u00d7 : \u2200 P Q R \u2192 \u27e6 P \u214b (Q & R) \u27e7 \u2192 \u27e6 P \u214b Q \u27e7 \u00d7 \u27e6 P \u214b R \u27e7\n dist-\u214b-\u00d7 P Q R p = dist-\u214b-fst P Q R p , dist-\u214b-snd P Q R p\n\n -- see dist-\u214b-&\n dist-\u214b-& : \u2200 P Q R \u2192 \u27e6 P \u214b (Q & R) \u27e7 \u2192 \u27e6 (P \u214b Q) & (P \u214b R) \u27e7\n dist-\u214b-& P Q R p = \u00d7\u2192& (dist-\u214b-\u00d7 P Q R p)\n\n -- see dist-\u214b-&\n factor-,-\u214b : \u2200 P Q R \u2192 \u27e6 P \u214b Q \u27e7 \u2192 \u27e6 P \u214b R \u27e7 \u2192 \u27e6 P \u214b (Q & R) \u27e7\n factor-,-\u214b end Q R pq pr = \u00d7\u2192& (pq , pr)\n factor-,-\u214b (recv P) Q R pq pr = \u03bb m \u2192 factor-,-\u214b (P m) Q R (pq m) (pr m)\n factor-,-\u214b (send P) Q R pq pr = [L: pq R: pr ]\n\n -- see dist-\u214b-&\n factor-\u00d7-\u214b : \u2200 P Q R \u2192 \u27e6 P \u214b Q \u27e7 \u00d7 \u27e6 P \u214b R \u27e7 \u2192 \u27e6 P \u214b (Q & R) \u27e7\n factor-\u00d7-\u214b P Q R (p , q) = factor-,-\u214b P Q R p q\n\n -- see dist-\u214b-&\n factor-&-\u214b : \u2200 P Q R \u2192 \u27e6 (P \u214b Q) & (P \u214b R) \u27e7 \u2192 \u27e6 P \u214b (Q & R) \u27e7\n factor-&-\u214b P Q R p = factor-\u00d7-\u214b P Q R (&\u2192\u00d7 p)\n\n -- see dist-\u214b-&\n module _ {{_ : FunExt}} where\n dist-\u214b-fst-factor-&-, : \u2200 P Q R (pq : \u27e6 P \u214b Q \u27e7)(pr : \u27e6 P \u214b R \u27e7)\n \u2192 dist-\u214b-fst P Q R (factor-,-\u214b P Q R pq pr) \u2261 pq\n dist-\u214b-fst-factor-&-, (recv P) Q R pq pr = \u03bb= \u03bb m \u2192 dist-\u214b-fst-factor-&-, (P m) Q R (pq m) (pr m)\n dist-\u214b-fst-factor-&-, (send P) Q R pq pr = refl\n dist-\u214b-fst-factor-&-, end Q R pq pr = refl\n\n dist-\u214b-snd-factor-&-, : \u2200 P Q R (pq : \u27e6 P \u214b Q \u27e7)(pr : \u27e6 P \u214b R \u27e7)\n \u2192 dist-\u214b-snd P Q R (factor-,-\u214b P Q R pq pr) \u2261 pr\n dist-\u214b-snd-factor-&-, (recv P) Q R pq pr = \u03bb= \u03bb m \u2192 dist-\u214b-snd-factor-&-, (P m) Q R (pq m) (pr m)\n dist-\u214b-snd-factor-&-, (send P) Q R pq pr = refl\n dist-\u214b-snd-factor-&-, end Q R pq pr = refl\n\n factor-\u00d7-\u214b-linv-dist-\u214b-\u00d7 : \u2200 P Q R \u2192 (factor-\u00d7-\u214b P Q R) LeftInverseOf (dist-\u214b-\u00d7 P Q R)\n factor-\u00d7-\u214b-linv-dist-\u214b-\u00d7 (recv P) Q R p = \u03bb= \u03bb m \u2192 factor-\u00d7-\u214b-linv-dist-\u214b-\u00d7 (P m) Q R (p m)\n factor-\u00d7-\u214b-linv-dist-\u214b-\u00d7 (send P) Q R p = \u03bb= \u03bb { `L \u2192 refl ; `R \u2192 refl }\n factor-\u00d7-\u214b-linv-dist-\u214b-\u00d7 end Q R p = \u03bb= \u03bb { `L \u2192 refl ; `R \u2192 refl }\n\n module _ P Q R where\n factor-\u00d7-\u214b-rinv-dist-\u214b-\u00d7 : (factor-\u00d7-\u214b P Q R) RightInverseOf (dist-\u214b-\u00d7 P Q R)\n factor-\u00d7-\u214b-rinv-dist-\u214b-\u00d7 (x , y) = pair\u00d7= (dist-\u214b-fst-factor-&-, P Q R x y) (dist-\u214b-snd-factor-&-, P Q R x y)\n\n dist-\u214b-\u00d7-\u2243 : \u27e6 P \u214b (Q & R) \u27e7 \u2243 (\u27e6 P \u214b Q \u27e7 \u00d7 \u27e6 P \u214b R \u27e7)\n dist-\u214b-\u00d7-\u2243 = dist-\u214b-\u00d7 P Q R\n , record { linv = factor-\u00d7-\u214b P Q R; is-linv = factor-\u00d7-\u214b-linv-dist-\u214b-\u00d7 P Q R\n ; rinv = factor-\u00d7-\u214b P Q R; is-rinv = factor-\u00d7-\u214b-rinv-dist-\u214b-\u00d7 }\n\n dist-\u214b-&-\u2243 : \u27e6 P \u214b (Q & R) \u27e7 \u2243 \u27e6 (P \u214b Q) & (P \u214b R) \u27e7\n dist-\u214b-&-\u2243 = dist-\u214b-\u00d7-\u2243 \u2243-\u2219 \u2243-! &\u2243\u00d7\n -}\n\nmodule _ {{_ : FunExt}}{{_ : UA}} where\n \u2297-pair : \u2200 {P Q} \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P \u2297 Q \u27e7\n \u2297-pair {end} {Q} p q = q\n \u2297-pair {send P} {Q} (m , p) q = m , \u2297-pair {P m} p q\n \u2297-pair {recv P} {end} p end = p\n \u2297-pair {recv P} {send Q} p (m , q) = m , \u2297-pair {recv P} {Q m} p q\n \u2297-pair {recv P} {recv Q} p q = [inl: (\u03bb m \u2192 \u2297-pair {P m} {recv Q} (p m) q)\n ,inr: (\u03bb n \u2192 \u2297-pair {recv P} {Q n} p (q n)) ]\n\n \u2297-fst : \u2200 P Q \u2192 \u27e6 P \u2297 Q \u27e7 \u2192 \u27e6 P \u27e7\n \u2297-fst end Q pq = _\n \u2297-fst (send P) Q (m , pq) = m , \u2297-fst (P m) Q pq\n \u2297-fst (recv P) end pq = pq\n \u2297-fst (recv P) (send Q) (_ , pq) = \u2297-fst (recv P) (Q _) pq\n \u2297-fst (recv P) (recv Q) pq = \u03bb m \u2192 \u2297-fst (P m) (recv Q) (pq (inl m))\n\n \u2297-snd : \u2200 P Q \u2192 \u27e6 P \u2297 Q \u27e7 \u2192 \u27e6 Q \u27e7\n \u2297-snd end Q pq = pq\n \u2297-snd (send P) Q (_ , pq) = \u2297-snd (P _) Q pq\n \u2297-snd (recv P) end pq = end\n \u2297-snd (recv P) (send Q) (m , pq) = m , \u2297-snd (recv P) (Q m) pq\n \u2297-snd (recv P) (recv Q) pq = \u03bb m \u2192 \u2297-snd (recv P) (Q m) (pq (inr m))\n\n \u2297-pair-fst : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 \u2297-fst P Q (\u2297-pair {P} {Q} p q) \u2261 p\n \u2297-pair-fst end Q p q = refl\n \u2297-pair-fst (send P) Q (m , p) q = pair= refl (\u2297-pair-fst (P m) Q p q)\n \u2297-pair-fst (recv P) end p q = refl\n \u2297-pair-fst (recv P) (send Q) p (m , q) = \u2297-pair-fst (recv P) (Q m) p q\n \u2297-pair-fst (recv P) (recv Q) p q = \u03bb= \u03bb m \u2192 \u2297-pair-fst (P m) (recv Q) (p m) q\n\n \u2297-pair-snd : \u2200 P Q (p : \u27e6 P \u27e7)(q : \u27e6 Q \u27e7) \u2192 \u2297-snd P Q (\u2297-pair {P} {Q} p q) \u2261 q\n \u2297-pair-snd end Q p q = refl\n \u2297-pair-snd (send P) Q (m , p) q = \u2297-pair-snd (P m) Q p q\n \u2297-pair-snd (recv P) end p q = refl\n \u2297-pair-snd (recv P) (send Q) p (m , q) = pair= refl (\u2297-pair-snd (recv P) (Q m) p q)\n \u2297-pair-snd (recv P) (recv Q) p q = \u03bb= \u03bb m \u2192 \u2297-pair-snd (recv P) (Q m) p (q m)\n\n module _ P Q where\n \u2297\u2192\u00d7 : \u27e6 P \u2297 Q \u27e7 \u2192 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n \u2297\u2192\u00d7 pq = \u2297-fst P Q pq , \u2297-snd P Q pq\n\n \u00d7\u2192\u2297 : \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7 \u2192 \u27e6 P \u2297 Q \u27e7\n \u00d7\u2192\u2297 (p , q) = \u2297-pair {P} {Q} p q\n\n \u00d7\u2192\u2297\u2192\u00d7 : \u00d7\u2192\u2297 RightInverseOf \u2297\u2192\u00d7\n \u00d7\u2192\u2297\u2192\u00d7 = \u03bb { (x , y) \u2192 pair\u00d7= (\u2297-pair-fst P Q x y) (\u2297-pair-snd P Q x y) }\n\n \u2297\u2192\u00d7-has-rinv : Rinv \u2297\u2192\u00d7\n \u2297\u2192\u00d7-has-rinv = record { rinv = \u00d7\u2192\u2297 ; is-rinv = \u00d7\u2192\u2297\u2192\u00d7 }\n\n {- WRONG\n \u2297\u2192\u00d7\u2192\u2297 : (\u00d7\u2192\u2297 P Q) LeftInverseOf (\u2297\u2192\u00d7 P Q)\n \u2297\u2243\u00d7 : \u27e6 P \u2297 Q \u27e7 \u2243 \u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7\n \u27e6\u2297\u27e7\u2261\u00d7 : P \u27e6\u2297\u27e7 Q \u2261 (\u27e6 P \u27e7 \u00d7 \u27e6 Q \u27e7)\n -}\n\n -o-apply : \u2200 {P Q} \u2192 \u27e6 dual P \u214b Q \u27e7 \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7\n -o-apply {P} {Q} pq p = \u214b-apply (dual P) Q pq (subst \u27e6_\u27e7 (\u2261.sym (\u2261\u1d3e-sound (dual-involutive P))) p)\n\n o-o-apply : \u2200 P Q \u2192 \u27e6 P o-o Q \u27e7 \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7\n o-o-apply P Q Po-oQ p = -o-apply {P} {Q} (\u2297-fst (P -o Q) (Q -o P) Po-oQ) p\n\n o-o-comm : \u2200 P Q \u2192 \u27e6 P o-o Q \u27e7 \u2261 \u27e6 Q o-o P \u27e7\n o-o-comm P Q = \u2297-comm (dual P \u214b Q) (dual Q \u214b P)\n\n -- left-biased \u201cstrategy\u201d\n par : \u2200 P Q \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P \u214b Q \u27e7\n par (recv P) Q p q = \u03bb m \u2192 par (P m) Q (p m) q\n par (send P) Q (m , p) q = \u214b-sendL Q m (par (P m) Q p q)\n par end Q end q = q\n\n \u214b-\u2218 : \u2200 P Q R \u2192 \u27e6 P \u214b Q \u27e7 \u2192 \u27e6 dual Q \u214b R \u27e7 \u2192 \u27e6 P \u214b R \u27e7\n \u214b-\u2218 P Q R pq qr = \u214b-\u2218-view (view-\u2218 P Q R pq qr)\n where\n \u214b-\u2218-view : \u2200 {P Q R}{pq : \u27e6 P \u214b Q \u27e7}{qr : \u27e6 dual Q \u214b R \u27e7} \u2192 View-\u2218 P Q R pq qr \u2192 \u27e6 P \u214b R \u27e7\n \u214b-\u2218-view (sendLL P Q R m p qr) = \u214b-sendL R m (\u214b-\u2218 (P m) (send Q) R p qr)\n \u214b-\u2218-view (recvLL P Q R p qr) = \u03bb m \u2192 \u214b-\u2218 (P m) Q R (p m) qr\n \u214b-\u2218-view (recvR-sendR ioP P Q R m pq q) = \u214b-sendR (com ioP P) m (\u214b-\u2218 (com ioP P) (recv Q) (R m) pq q)\n \u214b-\u2218-view (recvRR P Q R pq q) = \u03bb m \u2192 \u214b-\u2218 (send P) (recv Q) (R m) pq (q m)\n \u214b-\u2218-view (sendR-recvL P Q R m p q) = \u214b-\u2218 (send P) (Q m) R p (q m)\n \u214b-\u2218-view (recvR-sendL P Q R p m q) = \u214b-\u2218 (send P) (Q m) (send R) (p m) q\n \u214b-\u2218-view (endL Q R pq qr) = -o-apply {Q} {R} qr pq\n \u214b-\u2218-view (sendLM P R m pq qr) = \u214b-sendL R m (par (P m) R pq qr)\n \u214b-\u2218-view (recvL-sendR P Q m pq qr) = \u214b-\u2218 (send P) (Q m) end (pq m) (coe! (ap \u27e6_\u27e7 (\u214b-endR (dual (Q m)))) qr)\n\n {-\n mutual\n \u214b-comm : \u2200 P Q \u2192 \u27e6 P \u214b Q \u27e7 \u2192 \u27e6 Q \u214b P \u27e7\n \u214b-comm P Q p = \u214b-comm-view (view-\u214b P Q p)\n\n \u214b-comm-view : \u2200 {P Q} {pq : \u27e6 P \u214b Q \u27e7} \u2192 View-\u214b P Q pq \u2192 \u27e6 Q \u214b P \u27e7\n \u214b-comm-view (sendL' P Q m p) = \u214b-sendR (send Q) m (\u214b-comm (P m) (send Q) p)\n \u214b-comm-view (sendR' P Q n p) = inl n , \u214b-comm (send P) (Q n) p\n \u214b-comm-view (recvL' P Q pq) = \u214b-recvR Q P \u03bb m \u2192 \u214b-comm (P m) Q (pq m)\n \u214b-comm-view (recvR' P Q pq) = \u03bb n \u2192 \u214b-comm (send P) (Q n) (pq n)\n \u214b-comm-view (endL Q pq) = \u214b-rend! Q pq\n \u214b-comm-view (send P m pq) = m , pq\n -}\n\n switchL' : \u2200 P Q R (pq : \u27e6 P \u214b Q \u27e7) (r : \u27e6 R \u27e7) \u2192 \u27e6 P \u214b (Q \u2297 R) \u27e7\n switchL' end Q R q r = \u2297-pair {Q} {R} q r\n switchL' (send P) end R p r = par (send P) R p r\n switchL' (send P) (send Q) R (inl m , pq) r = inl m , switchL' (P m) (send Q) R pq r\n switchL' (send P) (send Q) R (inr m , pq) r = inr m , switchL' (send P) (Q m) R pq r\n switchL' (send P) (recv Q) end pq r = pq\n switchL' (send P) (recv Q) (send R) pq (m , r) = inr m , switchL' (send P) (recv Q) (R m) pq r\n switchL' (send P) (recv Q) (recv R) pq r (inl m) = switchL' (send P) (Q m) (recv R) (pq m) r\n switchL' (send P) (recv Q) (recv R) pq r (inr m) = switchL' (send P) (recv Q) (R m) pq (r m)\n switchL' (recv P) Q R pq r = \u03bb m \u2192 switchL' (P m) Q R (pq m) r\n\n switchL : \u2200 P Q R \u2192 \u27e6 (P \u214b Q) \u2297 R \u27e7 \u2192 \u27e6 P \u214b (Q \u2297 R) \u27e7\n switchL P Q R pqr = switchL' P Q R (\u2297-fst (P \u214b Q) R pqr) (\u2297-snd (P \u214b Q) R pqr)\n\n -- multiplicative mix (left-biased)\n mmix : \u2200 P Q \u2192 \u27e6 P \u2297 Q \u27e7 \u2192 \u27e6 P \u214b Q \u27e7\n mmix P Q pq = par P Q (\u2297-fst P Q pq) (\u2297-snd P Q pq)\n\n -- additive mix (left-biased)\n amix : \u2200 P Q \u2192 \u27e6 P & Q \u27e7 \u2192 \u27e6 P \u2295 Q \u27e7\n amix P Q pq = (`L , pq `L)\n\n{-\nA `\u2297 B 'times', context chooses how A and B are used\nA `\u214b B 'par', \"we\" chooses how A and B are used\nA `\u2295 B 'plus', select from A or B\nA `& B 'with', offer choice of A or B\n`! A 'of course!', server accept\n`? A 'why not?', client request\n`1 unit for `\u2297\n`\u22a5 unit for `\u214b\n`0 unit for `\u2295\n`\u22a4 unit for `&\n-}\ndata CLL : \u2605 where\n `1 `\u22a4 `0 `\u22a5 : CLL\n _`\u2297_ _`\u214b_ _`\u2295_ _`&_ : (A B : CLL) \u2192 CLL\n -- `!_ `?_ : (A : CLL) \u2192 CLL\n\n_\u22a5 : CLL \u2192 CLL\n`1 \u22a5 = `\u22a5\n`\u22a5 \u22a5 = `1\n`0 \u22a5 = `\u22a4\n`\u22a4 \u22a5 = `0\n(A `\u2297 B)\u22a5 = A \u22a5 `\u214b B \u22a5\n(A `\u214b B)\u22a5 = A \u22a5 `\u2297 B \u22a5\n(A `\u2295 B)\u22a5 = A \u22a5 `& B \u22a5\n(A `& B)\u22a5 = A \u22a5 `\u2295 B \u22a5\n{-\n(`! A)\u22a5 = `?(A \u22a5)\n(`? A)\u22a5 = `!(A \u22a5)\n-}\n\nCLL-proto : CLL \u2192 Proto\nCLL-proto `1 = end -- TODO\nCLL-proto `\u22a5 = end -- TODO\nCLL-proto `0 = send\u2032 \ud835\udfd8 end -- Alt: send \u03bb()\nCLL-proto `\u22a4 = recv\u2032 \ud835\udfd8 end -- Alt: recv \u03bb()\nCLL-proto (A `\u2297 B) = CLL-proto A \u2297 CLL-proto B\nCLL-proto (A `\u214b B) = CLL-proto A \u214b CLL-proto B\nCLL-proto (A `\u2295 B) = CLL-proto A \u2295 CLL-proto B\nCLL-proto (A `& B) = CLL-proto A & CLL-proto B\n\n{- The point of this could be to devise a particular equivalence\n relation for processes. It could properly deal with \u214b. -}\n\nmodule Commitment {Secret Guess : \u2605} {R : ..(_ : Secret) \u2192 Guess \u2192 \u2605} where\n Commit : Proto\n Commit = send\u2610 \u03bb (s : Secret) \u2192\n recv \u03bb (g : Guess) \u2192\n send \u03bb (_ : S\u27e8 s \u27e9) \u2192\n end\n\n commit : (s : Secret) \u2192 \u27e6 Commit \u27e7\n commit s = [ s ] , \u03bb g \u2192 S[ s ] , _\n\n decommit : (g : Guess) \u2192 \u27e6 dual Commit \u27e7\n decommit g = \u03bb { [ m ] \u2192 g , _ }\n\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n{-\ntest-sim : Sim (\ud835\udfd8 \u00d7' end) end\ntest-sim = end\n-}\n\n-- Prove knowledge of a discrete log\n-- Adapted here to have precise types\nmodule Shnorr-protocol\n (G \u2124q : \u2605)\n (g : G) \n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u00b7_ : G \u2192 G \u2192 G)\n (_+_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_*_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (_\u225f_ : (x y : G) \u2192 Dec (x \u2261 y))\n where\n module Real where\n Prover : Proto\n Prover = send \u03bb (g\u02b3 : G) \u2192 -- commitment\n recv \u03bb (c : \u2124q) \u2192 -- challenge\n send \u03bb (s : \u2124q) \u2192 -- response\n end\n\n Verifier : Proto\n Verifier = dual Prover\n\n -- he is honest but its type does not say it\n prover : (x r : \u2124q) \u2192 \u27e6 Prover \u27e7\n prover x r = (g ^ r) , \u03bb c \u2192 (r + (c * x)) , _\n\n Honest-Prover : ..(x : \u2124q) (y : S\u27e8 g ^ x \u27e9) \u2192 Proto\n Honest-Prover x y\n = send\u2610 \u03bb (r : \u2124q) \u2192 -- ideal commitment\n send \u03bb (g\u02b3 : S\u27e8 g ^ r \u27e9) \u2192 -- real commitment\n recv \u03bb (c : \u2124q) \u2192 -- challenge\n send \u03bb (s : S\u27e8 r + (c * x) \u27e9) \u2192 -- response\n recv \u03bb (_ : Dec ((g ^ unS s) \u2261 (unS g\u02b3 \u00b7 (unS y ^ c)))) \u2192\n end\n\n Honest-Verifier : ..(x : \u2124q) (y : S\u27e8 g ^ x \u27e9) \u2192 Proto\n Honest-Verifier x y = dual (Honest-Prover x y)\n\n honest-prover : (x r : \u2124q) \u2192 \u27e6 Honest-Prover x S[ g ^ x ] \u27e7\n honest-prover x r = [ r ] , S[ g ^ r ] , \u03bb c \u2192 S[ r + (c * x) ] , _\n -- agsy can do it\n\n honest-verifier : ..(x : \u2124q) (y : S\u27e8 g ^ x \u27e9) (c : \u2124q) \u2192 \u27e6 Honest-Verifier x y \u27e7\n honest-verifier x y c = \u03bb { [ r ] \u2192 \u03bb g\u02b3 \u2192 c , \u03bb s \u2192 (g ^ unS s) \u225f (unS g\u02b3 \u00b7 (unS y ^ c)) , _ }\n\n honest-prover\u2192prover : ..(x : \u2124q)(y : S\u27e8 g ^ x \u27e9) \u2192 \u27e6 Honest-Prover x y \u27e7 \u2192 \u27e6 Prover \u27e7\n honest-prover\u2192prover x y ([ r ] , g\u02b3 , p) = unS g\u02b3 , \u03bb c \u2192 case p c of \u03bb { (s , _) \u2192 unS s , _ }\n\n {-\n sim-honest-prover : ..(x : \u2124q)(y : S\u27e8 g ^ x \u27e9) \u2192 Sim (dual (Honest-Prover x y)) Prover\n sim-honest-prover x y = recvL (\u03bb { [ r ] \u2192\n recvL \u03bb g\u02b3 \u2192\n sendR (unS g\u02b3) (\n recvR \u03bb c \u2192\n sendL c (recvL \u03bb s \u2192 sendR (unS s) (sendL {!!} {!!}) )) })\n -}\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"69291665aee627be442146337bc1002cde0f963f","subject":"half corrected","message":"half corrected\n","repos":"siddhartha-gadgil\/LogicTypesSpaces,siddhartha-gadgil\/LogicTypesSpaces,siddhartha-gadgil\/LogicTypesSpaces,siddhartha-gadgil\/LogicTypesSpaces","old_file":"code\/GoaTest.agda","new_file":"code\/GoaTest.agda","new_contents":"open import Base\n\nmodule GoaTest where\n\ndata Bool : Type where -- finite type\n true : Bool\n false : Bool\n\nidBool : Bool \u2192 Bool -- lambda\nidBool x = x\n\nalwaysTrue : Bool \u2192 Bool\nalwaysTrue x = true\n\nnot : Bool \u2192 Bool -- case defn\nnot true = false\nnot false = true\n\nnotnot : Bool \u2192 Bool -- lambda\nnotnot x = not(not(x))\n\n_&_ : Bool \u2192 Bool \u2192 Bool --curried function\ntrue & x = x\nfalse & _ = false\n\ndata \u2115 : Type where -- infinite type\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n\neven : \u2115 \u2192 Bool -- recursive definition\neven zero = true\neven (succ x) = not (even x)\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115 \nzero + y = y\nsucc x + y = x + succ y\n\n{-# BUILTIN NATURAL \u2115 #-}\n\ndata \u2115List : Type where --list type \n [] : \u2115List -- empty list \n _::_ : \u2115 \u2192 \u2115List \u2192 \u2115List -- add number to head of list\n\nmylist : \u2115List \nmylist = 3 :: (4 :: (2 :: [])) -- the list [3, 4, 2]\n\ndata Vector : \u2115 \u2192 Type where -- type family\n [] : Vector 0\n _::_ : {n : \u2115} \u2192 \u2115 \u2192 Vector n \u2192 Vector (succ n) \n\nsum : {n : \u2115} \u2192 Vector n \u2192 \u2115\nsum [] = 0\nsum (x :: l) = x + sum l\n\n\ncountdown : (n : \u2115) \u2192 Vector n -- dependent function\ncountdown 0 = []\ncountdown (succ n) = (succ n) :: (countdown n)\n\nsumToN : \u2115 \u2192 \u2115 -- calculation\nsumToN n = sum(countdown n)\n\ndata isEven : \u2115 \u2192 Type where\n 0even : isEven 0\n +2even : (n : \u2115) \u2192 isEven n \u2192 isEven (succ(succ(n)))\n\n4even : isEven 4\n4even = +2even _ (+2even _ 0even)\n\ndata False : Type where\n\n1odd : isEven 1 \u2192 False\n1odd ()\n\n3odd : isEven 3 \u2192 False\n3odd (+2even .1 ())\n\nhalf : (n : \u2115) \u2192 isEven n \u2192 \u2115\nhalf .0 0even = 0\nhalf .(succ (succ n)) (+2even n pf) = succ(half n pf)\n\ndouble : (n : \u2115) \u2192 \u2115\ndouble 0 = 0\ndouble (succ n) = succ(succ(double(n)))\n\nstep : (n : \u2115) \u2192 (isEven (double n)) \u2192 isEven (double(succ(n)))\nstep n pf = +2even _ pf\n\nthm : (n : \u2115) \u2192 isEven (double n)\nthm zero = 0even\nthm (succ n) = step _ (thm n)\n\nhalfOfDouble : \u2115 \u2192 \u2115\nhalfOfDouble n = half (double n) (thm n)\n","old_contents":"open import Base\n\nmodule GoaTest where\n\ndata Bool : Type where -- finite type\n true : Bool\n false : Bool\n\nidBool : Bool \u2192 Bool -- lambda\nidBool x = x\n\nalwaysTrue : Bool \u2192 Bool\nalwaysTrue x = true\n\nnot : Bool \u2192 Bool -- case defn\nnot true = false\nnot false = true\n\nnotnot : Bool \u2192 Bool -- lambda\nnotnot x = not(not(x))\n\n_&_ : Bool \u2192 Bool \u2192 Bool --curried function\ntrue & x = x\nfalse & _ = false\n\ndata \u2115 : Type where -- infinite type\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n\neven : \u2115 \u2192 Bool -- recursive definition\neven zero = true\neven (succ x) = not (even x)\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115 \nzero + y = y\nsucc x + y = x + succ y\n\n{-# BUILTIN NATURAL \u2115 #-}\n\ndata \u2115List : Type where --list type \n [] : \u2115List -- empty list \n _::_ : \u2115 \u2192 \u2115List \u2192 \u2115List -- add number to head of list\n\nmylist : \u2115List \nmylist = 3 :: (4 :: (2 :: [])) -- the list [3, 4, 2]\n\ndata Vector : \u2115 \u2192 Type where -- type family\n [] : Vector 0\n _::_ : {n : \u2115} \u2192 \u2115 \u2192 Vector n \u2192 Vector (succ n) \n\nsum : {n : \u2115} \u2192 Vector n \u2192 \u2115\nsum [] = 0\nsum (x :: l) = x + sum l\n\n\ncountdown : (n : \u2115) \u2192 Vector n -- dependent function\ncountdown 0 = []\ncountdown (succ n) = (succ n) :: (countdown n)\n\nsumToN : \u2115 \u2192 \u2115 -- calculation\nsumToN n = sum(countdown n)\n\ndata isEven : \u2115 \u2192 Type where\n 0even : isEven 0\n +2even : (n : \u2115) \u2192 isEven n \u2192 isEven (succ(succ(n)))\n\n4even : isEven 4\n4even = +2even _ (+2even _ 0even)\n\ndata False : Type where\n\n1odd : isEven 1 \u2192 False\n1odd ()\n\n3odd : isEven 3 \u2192 False\n3odd (+2even .1 ())\n\nhalf : (n : \u2115) \u2192 isEven n \u2192 \u2115\nhalf .0 0even = 0\nhalf .(succ (succ n)) (+2even n pf) = half n pf\n\ndouble : (n : \u2115) \u2192 \u2115\ndouble 0 = 0\ndouble (succ n) = succ(succ(double(n)))\n\nstep : (n : \u2115) \u2192 (isEven (double n)) \u2192 isEven (double(succ(n)))\nstep n pf = +2even _ pf\n\nthm : (n : \u2115) \u2192 isEven (double n)\nthm zero = 0even\nthm (succ n) = step _ (thm n)\n\nhalfOfDouble : \u2115 \u2192 \u2115\nhalfOfDouble n = half (double n) (thm n)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"40d66dfc3f2210419c40886d095f223a2cfd8315","subject":"Typos.","message":"Typos.\n\nIgnore-this: a9f125da338e4853a6c0a6f49b4f60ad\n\ndarcs-hash:20111217061246-3bd4e-453a79b478812c0a80a233f53de72d1545fce6a8.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Program\/ABP\/Lemma1ATP\/Helper.agda","new_file":"src\/FOTC\/Program\/ABP\/Lemma1ATP\/Helper.agda","new_contents":"","old_contents":"","returncode":0,"stderr":"unknown","license":"mit","lang":"Agda"} {"commit":"484bb111868a8b921157a7822aff897305838442","subject":"cleaning up progress after canonical forms change","message":"cleaning up progress after canonical forms change\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress.agda","new_file":"progress.agda","new_contents":"open import Nat\nopen import Prelude\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import lemmas-ground\n\nopen import progress-checks\n\nopen import canonical-boxed-forms\nopen import canonical-value-forms\nopen import canonical-indeterminate-forms\n\nopen import ground-decidable\nopen import htype-decidable\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress\n data ok : (d : ihexp) (\u0394 : hctx) \u2192 Set where\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 ihexp ] (d \u21a6 d') \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n BV : \u2200{d \u0394} \u2192 d boxedval \u2192 ok d \u0394\n\n progress : {\u0394 : hctx} {d : ihexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = BV (BVVal VConst)\n\n -- variables\n progress (TAVar x\u2081) = abort (somenotnone (! x\u2081))\n\n -- lambdas\n progress (TALam _ wt) = BV (BVVal VLam)\n\n -- applications\n progress (TAAp wt1 wt2)\n with progress wt1 | progress wt2\n -- if the left steps, the whole thing steps\n progress (TAAp wt1 wt2) | S (_ , Step x y z) | _ = S (_ , Step (FHAp1 x) y (FHAp1 z))\n -- if the left is indeterminate, step the right\n progress (TAAp wt1 wt2) | I i | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n -- if they're both indeterminate, step when the cast steps and indet otherwise\n progress (TAAp wt1 wt2) | I x | I x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | I y | CIFACast (_ , _ , _ , _ , _ , refl , _ , _ ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | I y | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n -- similar if the left is indetermiante but the right is a boxed val\n progress (TAAp wt1 wt2) | I x | BV x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | BV y | CIFACast (_ , _ , _ , _ , _ , refl , _ , _ ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | BV y | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxedVal y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxedVal y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxedVal y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxedVal y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxedVal y))\n -- if the left is a boxed value, inspect the right\n progress (TAAp wt1 wt2) | BV v | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n progress (TAAp wt1 wt2) | BV v | I i\n with canonical-boxed-forms-arr wt1 v\n ... | CBFLam (_ , _ , refl , _) = S (_ , Step FHOuter ITLam FHOuter)\n ... | CBFCastArr (_ , _ , _ , refl , _ , _) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | BV v | BV v\u2082\n with canonical-boxed-forms-arr wt1 v\n ... | CBFLam (_ , _ , refl , _) = S (_ , Step FHOuter ITLam FHOuter)\n ... | CBFCastArr (_ , _ , _ , refl , _ , _) = S (_ , Step FHOuter ITApCast FHOuter)\n\n -- empty holes are indeterminate\n progress (TAEHole _ _ ) = I IEHole\n\n -- nonempty holes step if the innards step, indet otherwise\n progress (TANEHole xin wt x\u2081)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHNEHole x) y (FHNEHole z))\n ... | I x = I (INEHole (FIndet x))\n ... | BV x = I (INEHole (FBoxedVal x))\n\n -- casts\n progress (TACast wt con)\n with progress wt\n -- step if the innards step\n progress (TACast wt con) | S (_ , Step x y z) = S (_ , Step (FHCast x) y (FHCast z))\n -- if indet, inspect how the types in the cast are realted by consistency:\n -- if they're the same, step by ID\n progress (TACast wt TCRefl) | I x = S (_ , Step FHOuter ITCastID FHOuter)\n -- if first type is hole\n progress (TACast {\u03c41 = \u03c41} wt TCHole1) | I x\n with \u03c41\n progress (TACast wt TCHole1) | I x | b = I (ICastGroundHole GBase x)\n progress (TACast wt TCHole1) | I x | \u2987\u2988 = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole1) | I x | \u03c411 ==> \u03c412\n with ground-decidable (\u03c411 ==> \u03c412)\n progress (TACast wt TCHole1) | I x\u2081 | .\u2987\u2988 ==> .\u2987\u2988 | Inl GHole = I (ICastGroundHole GHole x\u2081)\n progress (TACast wt TCHole1) | I x\u2081 | \u03c411 ==> \u03c412 | Inr x = S (_ , Step FHOuter (ITGround (MGArr (ground-arr-not-hole x))) FHOuter)\n -- if second type is hole\n progress (TACast wt (TCHole2 {b})) | I x\n with canonical-indeterminate-forms-hole wt x\n progress (TACast wt (TCHole2 {b})) | I x | CIFHEHole (_ , _ , _ , refl , f) = I (ICastHoleGround (\u03bb _ _ ()) x GBase)\n progress (TACast wt (TCHole2 {b})) | I x | CIFHNEHole (_ , _ , _ , _ , _ , refl , _ ) = I (ICastHoleGround (\u03bb _ _ ()) x GBase)\n progress (TACast wt (TCHole2 {b})) | I x | CIFHAp (_ , _ , _ , refl , _ ) = I (ICastHoleGround (\u03bb _ _ ()) x GBase)\n progress (TACast wt (TCHole2 {b})) | I x | CIFHCast (_ , \u03c4 , refl , _)\n with htype-dec \u03c4 b\n progress (TACast wt (TCHole2 {b})) | I x\u2081 | CIFHCast (_ , .b , refl , _ , grn , _) | Inl refl = S (_ , Step FHOuter (ITCastSucceed grn ) FHOuter)\n progress (TACast wt (TCHole2 {b})) | I x\u2081 | CIFHCast (_ , _ , refl , \u03c02 , grn , _) | Inr x = S (_ , Step FHOuter (ITCastFail grn GBase x) FHOuter)\n progress (TACast wt (TCHole2 {\u2987\u2988}))| I x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCHole2 {\u03c411 ==> \u03c412})) | I x\n with ground-decidable (\u03c411 ==> \u03c412)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x\u2081 | Inl GHole\n with canonical-indeterminate-forms-hole wt x\u2081\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | CIFHEHole (_ , _ , _ , refl , _) = I (ICastHoleGround (\u03bb _ _ ()) x GHole)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | CIFHNEHole (_ , _ , _ , _ , _ , refl , _) = I (ICastHoleGround (\u03bb _ _ ()) x GHole)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | CIFHAp (_ , _ , _ , refl , _ ) = I (ICastHoleGround (\u03bb _ _ ()) x GHole)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | CIFHCast (_ , ._ , refl , _ , GBase , _) = S (_ , Step FHOuter (ITCastFail GBase GHole (\u03bb ())) FHOuter )\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | CIFHCast (_ , ._ , refl , _ , GHole , _) = S (_ , Step FHOuter (ITCastSucceed GHole) FHOuter)\n progress (TACast wt (TCHole2 {\u03c411 ==> \u03c412})) | I x\u2081 | Inr x = S (_ , Step FHOuter (ITExpand (MGArr (ground-arr-not-hole x))) FHOuter)\n -- if both are arrows\n progress (TACast wt (TCArr {\u03c41} {\u03c42} {\u03c41'} {\u03c42'} c1 c2)) | I x\n with htype-dec (\u03c41 ==> \u03c42) (\u03c41' ==> \u03c42')\n progress (TACast wt (TCArr c1 c2)) | I x\u2081 | Inl refl = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCArr c1 c2)) | I x\u2081 | Inr x = I (ICastArr x x\u2081)\n -- boxed value cases, inspect how the casts are realted by consistency\n -- step by ID if the casts are the same\n progress (TACast wt TCRefl) | BV x = S (_ , Step FHOuter ITCastID FHOuter)\n -- if left is hole\n progress (TACast wt (TCHole1 {\u03c4 = \u03c4})) | BV x\n with ground-decidable \u03c4\n progress (TACast wt TCHole1) | BV x\u2081 | Inl g = BV (BVHoleCast g x\u2081)\n progress (TACast wt (TCHole1 {b})) | BV x\u2081 | Inr x = abort (x GBase)\n progress (TACast wt (TCHole1 {\u2987\u2988})) | BV x\u2081 | Inr x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCHole1 {\u03c41 ==> \u03c42})) | BV x\u2081 | Inr x\n with (htype-dec (\u03c41 ==> \u03c42) (\u2987\u2988 ==> \u2987\u2988))\n progress (TACast wt (TCHole1 {.\u2987\u2988 ==> .\u2987\u2988})) | BV x\u2082 | Inr x\u2081 | Inl refl = BV (BVHoleCast GHole x\u2082)\n progress (TACast wt (TCHole1 {\u03c41 ==> \u03c42})) | BV x\u2082 | Inr x\u2081 | Inr x = S (_ , Step FHOuter (ITGround (MGArr x)) FHOuter)\n -- if right is hole\n progress {\u03c4 = \u03c4} (TACast wt TCHole2) | BV x\n with canonical-boxed-forms-hole wt x\n progress {\u03c4 = \u03c4} (TACast wt TCHole2) | BV x | d' , \u03c4' , refl , gnd , wt'\n with htype-dec \u03c4 \u03c4'\n progress (TACast wt TCHole2) | BV x\u2081 | d' , \u03c4 , refl , gnd , wt' | Inl refl = S (_ , Step FHOuter (ITCastSucceed gnd) FHOuter)\n progress {\u03c4 = \u03c4} (TACast wt TCHole2) | BV x\u2081 | _ , _ , refl , _ , _ | Inr _\n with ground-decidable \u03c4\n progress (TACast wt TCHole2) | BV x\u2082 | _ , _ , refl , gnd , _ | Inr x\u2081 | Inl x = S(_ , Step FHOuter (ITCastFail gnd x (flip x\u2081)) FHOuter)\n progress (TACast wt TCHole2) | BV x\u2082 | _ , _ , refl , _ , _ | Inr x\u2081 | Inr x\n with notground x\n progress (TACast wt TCHole2) | BV x\u2083 | _ , _ , refl , _ , _ | Inr _ | Inr _ | Inl refl = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole2) | BV x\u2083 | _ , _ , refl , _ , _ | Inr _ | Inr x | Inr (_ , _ , refl) = S(_ , Step FHOuter (ITExpand (MGArr (ground-arr-not-hole x))) FHOuter )\n -- if both arrows\n progress (TACast wt (TCArr {\u03c41} {\u03c42} {\u03c41'} {\u03c42'} c1 c2)) | BV x\n with htype-dec (\u03c41 ==> \u03c42) (\u03c41' ==> \u03c42')\n progress (TACast wt (TCArr c1 c2)) | BV x\u2081 | Inl refl = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCArr c1 c2)) | BV x\u2081 | Inr x = BV (BVArrCast x x\u2081)\n\n -- failed casts\n progress (TAFailedCast wt y z w)\n with progress wt\n progress (TAFailedCast wt y z w) | S (d' , Step x a q) = S (_ , Step (FHFailedCast x) a (FHFailedCast q))\n progress (TAFailedCast wt y z w) | I x = I (IFailedCast (FIndet x) y z w)\n progress (TAFailedCast wt y z w) | BV x = I (IFailedCast (FBoxedVal x) y z w)\n","old_contents":"open import Nat\nopen import Prelude\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import lemmas-ground\n\nopen import progress-checks\n\nopen import canonical-boxed-forms\nopen import canonical-value-forms\nopen import canonical-indeterminate-forms\n\nopen import ground-decidable\nopen import htype-decidable\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress\n data ok : (d : ihexp) (\u0394 : hctx) \u2192 Set where\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 ihexp ] (d \u21a6 d') \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n BV : \u2200{d \u0394} \u2192 d boxedval \u2192 ok d \u0394\n\n progress : {\u0394 : hctx} {d : ihexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = BV (BVVal VConst)\n\n -- variables\n progress (TAVar x\u2081) = abort (somenotnone (! x\u2081))\n\n -- lambdas\n progress (TALam _ wt) = BV (BVVal VLam)\n\n -- applications\n progress (TAAp wt1 wt2)\n with progress wt1 | progress wt2\n -- if the left steps, the whole thing steps\n progress (TAAp wt1 wt2) | S (_ , Step x y z) | _ = S (_ , Step (FHAp1 x) y (FHAp1 z))\n -- if the left is indeterminate, step the right\n progress (TAAp wt1 wt2) | I i | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n -- if they're both indeterminate, step when the cast steps and indet otherwise\n progress (TAAp wt1 wt2) | I x | I x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | I y | CIFACast (_ , _ , _ , _ , _ , refl , _ , _ ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | I y | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n progress (TAAp wt1 wt2) | I x | I y | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FIndet y))\n -- similar if the left is indetermiante but the right is a boxed val\n progress (TAAp wt1 wt2) | I x | BV x\u2081\n with canonical-indeterminate-forms-arr wt1 x\n progress (TAAp wt1 wt2) | I x | BV y | CIFACast (_ , _ , _ , _ , _ , refl , _ , _ ) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | I x | BV y | CIFAEHole (_ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxedVal y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFANEHole (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxedVal y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFAAp (_ , _ , _ , _ , _ , refl , _) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxedVal y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFACastHole (_ , refl , refl , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxedVal y))\n progress (TAAp wt1 wt2) | I x | BV y | CIFAFailedCast (_ , _ , refl , _ ) = I (IAp (\u03bb _ _ _ _ _ ()) x (FBoxedVal y))\n -- if the left is a boxed value, inspect the right\n progress (TAAp wt1 wt2) | BV v | S (_ , Step x y z) = S (_ , Step (FHAp2 x) y (FHAp2 z))\n progress (TAAp wt1 wt2) | BV v | I i\n with canonical-boxed-forms-arr wt1 v\n ... | CBFLam (_ , _ , refl , _) = S (_ , Step FHOuter ITLam FHOuter)\n ... | CBFCastArr (_ , _ , _ , refl , _ , _) = S (_ , Step FHOuter ITApCast FHOuter)\n progress (TAAp wt1 wt2) | BV v | BV v\u2082\n with canonical-boxed-forms-arr wt1 v\n ... | CBFLam (_ , _ , refl , _) = S (_ , Step FHOuter ITLam FHOuter)\n ... | CBFCastArr (_ , _ , _ , refl , _ , _) = S (_ , Step FHOuter ITApCast FHOuter)\n\n -- empty holes are indeterminate\n progress (TAEHole _ _ ) = I IEHole\n\n -- nonempty holes step if the innards step, indet otherwise\n progress (TANEHole xin wt x\u2081)\n with progress wt\n ... | S (_ , Step x y z) = S (_ , Step (FHNEHole x) y (FHNEHole z))\n ... | I x = I (INEHole (FIndet x))\n ... | BV x = I (INEHole (FBoxedVal x))\n\n -- casts\n progress (TACast wt con)\n with progress wt\n -- step if the innards step\n progress (TACast wt con) | S (_ , Step x y z) = S (_ , Step (FHCast x) y (FHCast z))\n -- if indet, inspect how the types in the cast are realted by consistency:\n -- if they're the same, step by ID\n progress (TACast wt TCRefl) | I x = S (_ , Step FHOuter ITCastID FHOuter)\n -- if first type is hole\n progress (TACast {\u03c41 = \u03c41} wt TCHole1) | I x\n with \u03c41\n progress (TACast wt TCHole1) | I x | b = I (ICastGroundHole GBase x)\n progress (TACast wt TCHole1) | I x | \u2987\u2988 = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole1) | I x | \u03c411 ==> \u03c412\n with ground-decidable (\u03c411 ==> \u03c412)\n progress (TACast wt TCHole1) | I x\u2081 | .\u2987\u2988 ==> .\u2987\u2988 | Inl GHole = I (ICastGroundHole GHole x\u2081)\n progress (TACast wt TCHole1) | I x\u2081 | \u03c411 ==> \u03c412 | Inr x = S (_ , Step FHOuter (ITGround (MGArr (ground-arr-not-hole x))) FHOuter)\n -- if second type is hole\n progress (TACast wt (TCHole2 {b})) | I x\n with canonical-indeterminate-forms-hole wt x\n progress (TACast wt (TCHole2 {b})) | I x | CIFHEHole (_ , _ , _ , refl , f) = I (ICastHoleGround (\u03bb _ _ ()) x GBase)\n progress (TACast wt (TCHole2 {b})) | I x | CIFHNEHole (_ , _ , _ , _ , _ , refl , _ ) = I (ICastHoleGround (\u03bb _ _ ()) x GBase)\n progress (TACast wt (TCHole2 {b})) | I x | CIFHAp (_ , _ , _ , refl , _ ) = I (ICastHoleGround (\u03bb _ _ ()) x GBase)\n progress (TACast wt (TCHole2 {b})) | I x | CIFHCast (_ , \u03c4 , refl , _)\n with htype-dec \u03c4 b\n progress (TACast wt (TCHole2 {b})) | I x\u2081 | CIFHCast (_ , .b , refl , \u03c02 , _) | Inl refl = S (_ , Step FHOuter (ITCastSucceed \u03c02) FHOuter)\n progress (TACast wt (TCHole2 {b})) | I x\u2081 | CIFHCast (_ , _ , refl , \u03c02 , _) | Inr x = S (_ , Step FHOuter (ITCastFail \u03c02 GBase x) FHOuter)\n progress (TACast wt (TCHole2 {\u2987\u2988}))| I x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCHole2 {\u03c411 ==> \u03c412})) | I x\n with ground-decidable (\u03c411 ==> \u03c412)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x\u2081 | Inl GHole\n with canonical-indeterminate-forms-hole wt x\u2081\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | CIFHEHole (_ , _ , _ , refl , _) = I (ICastHoleGround (\u03bb _ _ ()) x GHole)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | CIFHNEHole (_ , _ , _ , _ , _ , refl , _) = I (ICastHoleGround (\u03bb _ _ ()) x GHole)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | CIFHAp (_ , _ , _ , refl , _ ) = I (ICastHoleGround (\u03bb _ _ ()) x GHole)\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | CIFHCast (_ , ._ , refl , GBase , _) = S (_ , Step FHOuter (ITCastFail GBase GHole (\u03bb ())) FHOuter )\n progress (TACast wt (TCHole2 {.\u2987\u2988 ==> .\u2987\u2988})) | I x | Inl GHole | CIFHCast (_ , ._ , refl , GHole , _) = S (_ , Step FHOuter (ITCastSucceed GHole) FHOuter)\n progress (TACast wt (TCHole2 {\u03c411 ==> \u03c412})) | I x\u2081 | Inr x = S (_ , Step FHOuter (ITExpand (MGArr (ground-arr-not-hole x))) FHOuter)\n -- if both are arrows\n progress (TACast wt (TCArr {\u03c41} {\u03c42} {\u03c41'} {\u03c42'} c1 c2)) | I x\n with htype-dec (\u03c41 ==> \u03c42) (\u03c41' ==> \u03c42')\n progress (TACast wt (TCArr c1 c2)) | I x\u2081 | Inl refl = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCArr c1 c2)) | I x\u2081 | Inr x = I (ICastArr x x\u2081)\n -- boxed value cases, inspect how the casts are realted by consistency\n -- step by ID if the casts are the same\n progress (TACast wt TCRefl) | BV x = S (_ , Step FHOuter ITCastID FHOuter)\n -- if left is hole\n progress (TACast wt (TCHole1 {\u03c4 = \u03c4})) | BV x\n with ground-decidable \u03c4\n progress (TACast wt TCHole1) | BV x\u2081 | Inl g = BV (BVHoleCast g x\u2081)\n progress (TACast wt (TCHole1 {b})) | BV x\u2081 | Inr x = abort (x GBase)\n progress (TACast wt (TCHole1 {\u2987\u2988})) | BV x\u2081 | Inr x = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCHole1 {\u03c41 ==> \u03c42})) | BV x\u2081 | Inr x\n with (htype-dec (\u03c41 ==> \u03c42) (\u2987\u2988 ==> \u2987\u2988))\n progress (TACast wt (TCHole1 {.\u2987\u2988 ==> .\u2987\u2988})) | BV x\u2082 | Inr x\u2081 | Inl refl = BV (BVHoleCast GHole x\u2082)\n progress (TACast wt (TCHole1 {\u03c41 ==> \u03c42})) | BV x\u2082 | Inr x\u2081 | Inr x = S (_ , Step FHOuter (ITGround (MGArr x)) FHOuter)\n -- if right is hole\n progress {\u03c4 = \u03c4} (TACast wt TCHole2) | BV x\n with canonical-boxed-forms-hole wt x\n progress {\u03c4 = \u03c4} (TACast wt TCHole2) | BV x | d' , \u03c4' , refl , gnd , wt'\n with htype-dec \u03c4 \u03c4'\n progress (TACast wt TCHole2) | BV x\u2081 | d' , \u03c4 , refl , gnd , wt' | Inl refl = S (_ , Step FHOuter (ITCastSucceed gnd) FHOuter)\n progress {\u03c4 = \u03c4} (TACast wt TCHole2) | BV x\u2081 | _ , _ , refl , _ , _ | Inr _\n with ground-decidable \u03c4\n progress (TACast wt TCHole2) | BV x\u2082 | _ , _ , refl , gnd , _ | Inr x\u2081 | Inl x = S(_ , Step FHOuter (ITCastFail gnd x (flip x\u2081)) FHOuter)\n progress (TACast wt TCHole2) | BV x\u2082 | _ , _ , refl , _ , _ | Inr x\u2081 | Inr x\n with notground x\n progress (TACast wt TCHole2) | BV x\u2083 | _ , _ , refl , _ , _ | Inr _ | Inr _ | Inl refl = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt TCHole2) | BV x\u2083 | _ , _ , refl , _ , _ | Inr _ | Inr x | Inr (_ , _ , refl) = S(_ , Step FHOuter (ITExpand (MGArr (ground-arr-not-hole x))) FHOuter )\n -- if both arrows\n progress (TACast wt (TCArr {\u03c41} {\u03c42} {\u03c41'} {\u03c42'} c1 c2)) | BV x\n with htype-dec (\u03c41 ==> \u03c42) (\u03c41' ==> \u03c42')\n progress (TACast wt (TCArr c1 c2)) | BV x\u2081 | Inl refl = S (_ , Step FHOuter ITCastID FHOuter)\n progress (TACast wt (TCArr c1 c2)) | BV x\u2081 | Inr x = BV (BVArrCast x x\u2081)\n\n -- failed casts\n progress (TAFailedCast wt y z w)\n with progress wt\n progress (TAFailedCast wt y z w) | S (d' , Step x a q) = S (_ , Step (FHFailedCast x) a (FHFailedCast q))\n progress (TAFailedCast wt y z w) | I x = I (IFailedCast (FIndet x) y z w)\n progress (TAFailedCast wt y z w) | BV x = I (IFailedCast (FBoxedVal x) y z w)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"4c8433af18a51f6d63ab5f28270991b262ef907b","subject":"agda : Swierstra Predicate Transformer","message":"agda : Swierstra Predicate Transformer\n","repos":"haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc","old_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/wouter-swierstra-predicate-transformers-6fe8f13\/PredicateTransformers.agda","new_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/wouter-swierstra-predicate-transformers-6fe8f13\/PredicateTransformers.agda","new_contents":"module PredicateTransformers where\n\nopen import Prelude hiding (map; all)\nopen import Level hiding (lift)\n\n------------------------------------------------------------------------------\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\n------------------------------------------------------------------------------\nABSTRACT\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nrefinement type https:\/\/en.wikipedia.org\/wiki\/Refinement_(computing)\n- type with a predicate which must hold for any element of the refined type\n- can express\n - preconditions when used as function args\n - postconditions when used as return types\n\nthe problem statement is centered around \/compositionality\/\n- pure functions enable compositional reasoning\n- effectful programs require reasoning about context\n- paper provides mechanism for reasoning about effectful programs (and for program calculation)\n\nmain idea\n- represent effectful computation as a free monad\n- write an \"interpreter\" that computes a predicate transformer\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to use this refinement relation to\n - show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\nmodule Free where\n -- Free : represents syntax of an effectful computation\n -- C : type of commands\n -- Free C R : returns an 'a' or issues command c : C\n -- For each c : C, there is a set of responses R c\n -- - R : C -> Set : is type family of responses for commands\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\" input to output\n -- input\n -- - pure function 'a\u2192b : a -> b'\n -- - postcondition 'b -> Set' (on output of a\u2192b)\n -- output\n -- - weakest precondition 'a -> Set' on input to a\u2192b that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n module _ where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data isEven : \u2115 \u2192 Set where\n even-z : isEven Nat.zero\n even-s : {n : \u2115} \u2192 isEven n \u2192 isEven (Nat.suc (Nat.suc n))\n\n isEvenWP : \u2115 -> Set\n isEvenWP = wp0 (_+ 2) isEven\n\n _ : isEvenWP \u2261 isEven \u2218 (_+ 2)\n _ = refl\n\n _ : Set\n _ = isEvenWP 5\n\n _ : isEvenWP 5 \u2261 isEven 7\n _ = refl\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - enable via making dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n -- every element a that satisfies P is contained in the set of things that satifies Q\n --\n -- implication between predicates\n --\n -- P a says a \u2208 P\n -- if the extents of P, Q : A \u2192 Set is taken to be\n -- the subset of A satisfying predicates P, Q\n --\n -- the extent of P : A -> Set is the subset of terms of type A satisfying P.\n -- \"extent\" indicates a kind of black-box view of things,\n -- e.g., the \"extent\" of a function is the set of its input-output pairs.\n --\n -- https:\/\/en.wikipedia.org\/wiki\/Extension_%28semantics%29\n -- the extension of a concept, idea, or sign consists of the things to which it applies\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n module _ where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data _\u2264_ : \u2115 \u2192 \u2115 \u2192 Set where\n z\u2264n : \u2200 {n : \u2115} \u2192 0 \u2264 n\n s\u2264s : \u2200 {m n : \u2115} \u2192 m \u2264 n \u2192 Nat.suc m \u2264 Nat.suc n\n\n \u22641\u2286\u22642 : (_\u2264 1) \u2286 (_\u2264 2)\n -- P \u2286 Q\n -- \u2200 a -> P a -> Q a\n -- (a : \u2115) -> (a \u2264 1) -> (a \u2264 2)\n \u22641\u2286\u22642 Zero _ = z\u2264n\n \u22641\u2286\u22642 (Succ Zero) (s\u2264s _) = s\u2264s z\u2264n\n\n {- cannot be proved\n \u22642\u2286\u22641 : (_\u2264 2) \u2286 (_\u2264 1)\n \u22642\u2286\u22641 Zero _ = z\u2264n\n \u22642\u2286\u22641 (Succ a) (s\u2264s p) = {!!}\n -}\n\n -- refinement relation between PTs : pt1 refined by pt2\n -- (same thing as \u2286, but one level up)\n -- for every post condition, pt2 weakens the precondition\n -- \"weakest\" is the right side : pt2 P\n --\n -- pt1 , pt2 : preconditions for satisfying a program\n -- refinement : pt1 maybe be a stronger requirement than pt2\n --\n -- pt2 is a refinement because it weakens what is required by pt1\n -- e.g., pt1 pt2\n -- >= 20 >= 10\n --\n -- weakening the input assumptions gives a stronger result\n --\n -- contract says establish a pre and I will give you a post, i.e.,\n -- \"stronger\" post condition\n -- or \"weaker\" pre condition\n --\n -- refinement of predicate transformers\n --\n -- use case: relate different implementations of the \"same program\"\n -- pt1 \u2291 pt2 : pt1 is refined by pt2\n -- - if pt1 is understood as giving preconditions which satisfy\n -- postcondition P for some given program f1\n -- then his says the precondition can be \/weakened\/ and still guarantee the postcondition\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g\n -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation to\n - relate PT semantics between programs and specifications\n - show a program satisfies its specification\n - show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n - corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n note : for =Free C R b= programs means generating the same AST\n\n instead, define predicate transformers that give \/interpretations\/\n to a particular choice of command type C and response family R\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public using (_+_; _>_; _*_; s\u2264s; z\u2264n)\n renaming (\u2115 to Nat; zero to Zero; suc to Succ)\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n -- one command\n data C : Set where\n Abort : C -- no continuation\n\n -- response code : the program does not execute further\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n -- BIG-STEP SEMANTICS specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n module _ where\n _ : Expr\n _ = Val 3\n _ : Expr\n _ = Div (Val 3) (Val 0)\n\n _ : Val 0 \u21d3 0\n _ = \u21d3Base\n\n _ : Val 3 \u21d3 3\n _ = \u21d3Base\n\n _ : Div (Val 3) (Val 3) \u21d3 1\n _ = \u21d3Step \u21d3Base \u21d3Base\n\n _ : Div (Val 9) (Val 3) \u21d3 3\n _ = \u21d3Step \u21d3Base \u21d3Base\n\n _ : Div (Val 9) (Div (Val 3) (Val 1)) \u21d3 3\n _ = \u21d3Step \u21d3Base (\u21d3Step \u21d3Base \u21d3Base)\n\n _ : Div (Div (Val 9) (Val 1)) (Div (Val 3) (Val 1)) \u21d3 3\n _ = \u21d3Step (\u21d3Step \u21d3Base \u21d3Base) (\u21d3Step \u21d3Base \u21d3Base)\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val n \u27e7 = return n\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\nl ->\n \u27e6 er \u27e7 >>= \\nr ->\n nl \u00f7 nr\n\n module _ where\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n _ : evv \u2261 Pure 3\n _ = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n _ : evd \u2261 Pure 1\n _ = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n _ : evd0 \u2261 Step Abort (\u03bb ())\n _ = refl\n\n -- relate big step semantics to monadic interpreter\n\n -- convert post-condition for a pure function 'f : (x : A) -> b x'\n -- to post-condition on partial functions 'f : (x : A) -> Partial (b x)'\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba -- P holds if operation produces a value\n mustPT _ _ (Step Abort _) = \u22a5 -- if operation fails, there is no result to apply P to\n -- note: could give \u22a4 here, but want to rule out failure\n -- (total correctness)\n\n module _ where\n _ : Expr -> Nat -> Set\n _ = _\u21d3_\n\n _ : Set\n _ = Val 1 \u21d3 1\n\n _ : Expr -> Partial Nat -> Set\n _ = mustPT _\u21d3_\n\n _ : Set\n _ = mustPT _\u21d3_ (Val 1) (Pure 1)\n\n _ : mustPT _\u21d3_ (Val 1) (Pure 1)\n _ = \u21d3Base\n\n _ : Set\n _ = mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n {- a value of this type cannot be constructed\n _ : mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n _ = {!!}\n -}\n _ : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb n -> Pure 3 >>= _\u00f7 n))\n _ = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n _ : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n _ = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n _ : Set\n _ = mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n\n {-\n to relate big step semantics to monadic interpreter\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - _\u21d3_ relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n\n to call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results P' : Partial b -> Set\n - done via 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so mustPT Abort case returns empty type\n -}\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n module _ where\n _ : Expr -> Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n _ = refl\n\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n _ = \u21d3Base\n\n _ : Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n _ = \u21d3Step \u21d3Base \u21d3Base\n\n _ : Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n {- this type cannot be constructed\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n _ = {!!}\n -}\n\n {-\n wpPartial \u27e6_\u27e7 _\u21d3_ : a predicate on expressions\n - a way to express SUCCESSFUL evaluation of an Expr with intended semantics\n\n \u2200 exprs satisfying this predicate\n - monadic interpreter and\n - relational big-step specification\n must agree on result of evaluation\n\n for a given Expr, satisfaction of predicate means\n - Expr evaluates to a value by big-step operational semantics\n - if interpreter returns a value at all\n then that value agrees with big step semantics\n - if interpreter aborts\n then have a proof of \u22a5 (so, it does not abort)\n which will NOT agree with \u21d3 big-step semantics since \u21d3 does not allow div-by-0\n\n What does this say about correctness of interpreter?\n\n does not specify how many Expr that satisfy that characterization also satisfy wpPartial \u27e6_\u27e7 _\u21d3_\n\n to understand wpPartial \u27e6_\u27e7 _\u21d3_ better\n - define SafeDiv : safety criteria that we believe in\n - prove every expression that satisfies SafeDiv satisfies wpPartial \u27e6_\u27e7 _\u21d3_\n - SafeDiv refines wpPartial \u27e6_\u27e7 _\u21d3_\n - if we gave \u22a4 above, then 'correct' would not say that SafeDiv rules out undefined behavior\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val _) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n module _ where\n _ : SafeDiv (Val 3) \u2261 \u22a4\n _ = refl\n\n _ : Set\n _ = SafeDiv (Val 0)\n _ : SafeDiv (Val 0) \u2261 \u22a4\n _ = refl\n\n _ : Set\n _ = SafeDiv (Div (Val 3) (Val 3))\n _ : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 0) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n _ = refl\n\n _ : Set\n _ = SafeDiv (Div (Val 3) (Val 0))\n _ : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val 0 \u21d3 0) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n _ = refl\n\n {-\n expect : any expr : e for which SafeDiv e holds can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n -- (a : Expr) (SafeDiv a) (wpPartial \u27e6_\u27e7 _\u21d3_ a)\n correct (Val _) _ = \u21d3Base\n correct (Div el er) (er\u21d30\u2192\u22a5 , (sdel , sder))\n with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n ... | Pure _ | Pure Zero | _ | er\u21d30 = magic (er\u21d30\u2192\u22a5 er\u21d30)\n ... | Pure _ | Pure (Succ _) | el\u21d3nl | er\u21d3Snr = \u21d3Step el\u21d3nl er\u21d3Snr\n ... | Pure _ | Step Abort _ | _ | ()\n ... | Step Abort _ | _ | () | _\n\n module _ where\n _ : Val 3 \u21d3 3\n _ = correct (Val 3) tt\n\n _ : Div (Val 3) (Val 1) \u21d3 3\n _ = correct (Div (Val 3) (Val 1)) ((\u03bb ()) , (tt , tt))\n\n {- TODO\n _ : Set\n _ = SafeDiv (Div (Val 3) (Val 0))\n sd : SafeDiv (Div (Val 3) (Val 0))\n sd = {!!} , (tt , tt)\n\n _ : \u22a5\n _ = correct (Div (Val 3) (Val 0)) sd\n -}\n\n {-\n Generalization.\n Instead of manually defining SafeDiv,\n define more general predicate\n characterising the domain of a partial function using wpPartial.\n -}\n\n -- domain is well-defined Exprs (i.e., no div-by-0)\n -- returns \u22a4 on Pure; \u22a5 on Step Abort ...\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n module _ where\n _ : dom \u27e6_\u27e7 \u2261 \u03bb z \u2192 mustPT (\u03bb x _ \u2192 \u22a4' zero) z \u27e6 z \u27e7\n _ = refl\n _ : dom \u27e6_\u27e7 (Val 0) \u2261 \u22a4' zero\n _ = refl\n _ : dom \u27e6_\u27e7 (Div (Val 1) (Val 0)) \u2261 \u22a5\n _ = refl\n\n -- can show that the two semantics agree precisely on the domain of the interpreter:\n\n -- everything in domain of interpreter\n -- gets evaluated in agreement with big step semantics\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n -- only things interpreted in agreement with big step semantics\n -- are those that are in domain of interpreter\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on argument expression\n\n sound (Val _) _ = \u21d3Base\n sound (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div _ _) () | Pure _ | Pure Zero | _ | _\n sound (Div _ _) _ | Pure nl | Pure (Succ nr) | \u22a4'zero\u2192el\u21d3nl | \u22a4'zero\u2192er\u21d3Succnr\n = \u21d3Step (\u22a4'zero\u2192el\u21d3nl tt) (\u22a4'zero\u2192er\u21d3Succnr tt)\n sound (Div _ _) () | Pure _ | Step Abort _ | _ | _\n sound (Div _ _) () | Step Abort _ | _ | _ | _\n\n inDom : {n : Nat}\n -> (e : Expr)\n -> \u27e6 e \u27e7 == Pure n\n -> dom \u27e6_\u27e7 e\n inDom (Val _) _ = tt\n inDom (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div _ _) () | Pure _ | Pure Zero\n inDom (Div _ _) _ | Pure _ | Pure (Succ _) = tt\n inDom (Div _ _) () | Pure _ | Step Abort _\n inDom (Div _ _) () | Step Abort _ | _\n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n : (e : Expr) (n : Nat)\n -> \u27e6 e \u27e7 \u2261 Pure n\n -> e \u21d3 n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n e _ eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} Diveler\u21d3_nldivSuccn\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 { _} { _} () | Pure _ | _ | Pure Zero\n wpPartial1 {el} { _} _ | Pure nl | [[[ eqnl ]]] | Pure (Succ _) = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n el nl eqnl\n wpPartial1 { _} { _} () | Pure _ | _ | Step Abort _\n wpPartial1 { _} { _} () | Step Abort _ | _ | _\n\n wpPartial2 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} _ with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 { _} {er} _ | Pure _ | _ | Pure nr | [[[ eqnr ]]] = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n er nr eqnr\n wpPartial2 { _} { _} () | Pure _ | _ | Step Abort _ | _\n wpPartial2 { _} { _} () | Step Abort _ | _ | _ | _\n\n complete (Val _) _ = tt\n complete (Div el er) h\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div _ _) h | Pure nl | [[[ \u27e6el\u27e7\u2261Purenl ]]] | Pure Zero | [[[ \u27e6er\u27e7\u2261Pure0 ]]] | _ | _\n -- Goal\n -- mustPT (\u03bb _ _ \u2192 \u22a4' zero)\n -- (Div el er) (Pure nl >>= (\u03bb nl' \u2192 Pure Zero >>= _\u00f7_ nl'))\n -- \u22a5\n -- Context\n -- h : mustPT _\u21d3_ (Div el er) (\u27e6 el \u27e7 >>= (\u03bb nl' \u2192 \u27e6 er \u27e7 >>= _\u00f7_ nl'))\n rewrite\n \u27e6el\u27e7\u2261Purenl -- h : mustPT _\u21d3_ (Div el er) (\u27e6 er \u27e7 >>= _\u00f7_ nl)\n | \u27e6er\u27e7\u2261Pure0 -- h : \u22a5\n = h -- magic h\n complete (Div _ _) _ | Pure _ | _ | Pure (Succ _) | _ | _ | _ = tt\n complete (Div _ _) _ | Pure _ | _ | Step Abort _ | _ | _ | ()\n complete (Div _ _) _ | Step Abort _ | _ | _ | _ | () | _\n\n module _ where\n {-\n _ : mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n _ = {!!}\n -}\n _ : Set\n _ = mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) <-> (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement\n - to relate Kleisli morphisms\n - to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n this example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre and post condition\n - to its implementation\n\n add top two elements; fails if stack has too few elements\n\n show how to prove definition meets its specification\n -}\n\n -- define specification in terms of a pre\/post condition\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n -- [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n -- for non-dependent examples (e.g., type b does not depend on x : a)\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function (to discard unused arg of type a)\n\n -- describes desired postcondition for addition function\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n -- spec for addition function\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n -- pre post\n {-\n need to relate spec to an implementation\n 'wpPartial' assigns predicate transformer semantics to functions\n 'wpSpec' assigns predicate transformer semantics to specifications\n -}\n -- given a spec, Spec a b\n -- computes weakest precondition that satisfies an arbitrary postcondition P\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\a -> (pre a) -- i.e., spec\u2019s precondition should hold and\n \u2227 (post a \u2286 P a) -- spec's postcondition must imply P\n\n -- using 'wpSpec' can now find a program 'add' that \"refines\" 'addSpec'\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs0 =\n pop xs0 >>= \\{(x1 , xs1) ->\n pop xs1 >>= \\{(x2 , xs2) ->\n return ((x1 + x2) :: xs2)}}\n\n -- verify correct\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd _ (_ :: Nil) (s\u2264s () , _)\n correctnessAdd P a@(x :: y :: xs ) (s\u2264s (s\u2264s z\u2264n) , post_addSpec_a_\u2286P_a)\n -- wpPartial add P (x :: y :: xs)\n = post_addSpec_a_\u2286P_a (x + y :: xs) AddStep\n -- paper version has \"extra\" 'Nil\" case\n--correctnessAdd P Nil ( () , _)\n--correctnessAdd P (_ :: Nil) (s\u2264s () , _)\n--correctnessAdd P (x :: y :: xs ) ( _ , H) = H (x + y :: xs) AddStep\n\n {-\n repeat: this example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition\n - to its implementation\n\n compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\n{-\n------------------------------------------------------------------------------\n4 Mutable State\n\npredicate transformer semantics for mutable state\ngiving rise to Hoare logic\n\nfollowing assumes a fixed type s : Set (the type of the state)\n-}\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n\n -- smart constructor\n get : State s\n get = Step Get return\n\n -- smart constructor\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n -- map free monad to the state monad\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n -- predicate transformer : for every stateful computation\n -- maps a postcondition on b \u00d7 s\n -- to the required precondition on s:\n statePT : forall {l l'} -> {b : Set l}\n -> (b \u00d7 s -> Set l')\n -> State b\n -> ( s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n -- generalise statePT\n -- Sometimes describe postconditions as a relation between inputs and outputs.\n -- For stateful computations, mean enabling the postcondition to also refer to the initial state:\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n -- weakest precondition semantics for Kleisli morphisms a \u2192 State b\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> ( a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> ( a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n -- Given predicate P relating input, initial state, final state and result,\n -- 'wpState' computes the weakest precondition required of input and initial state\n -- to ensure P holds upon completing the computation.\n -- The definition amounts to composing 'wp' and 'statePT' functions.\n\n -- prove soundness of this semantics with respect to the run function\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\n{-\nExample showing how to reason about stateful programs using weakest precondition semantics.\n\nVerification problem proposed by Hutton and Fulger [2008]\n- input : binary tree\n- relabel tree so each leaf has a unique number associated with it\n\nTypical solution uses state monad to keep track of next unused label.\n\nHutton and Fulger challenge : reason about the program, without expanding definition of monadic operations.\n\nExample shows how properties of refinement relation can be used to reason about arbitrary effects.\n-}\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n -- Specification.\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n -- precondition true regardless of input tree and initial state\n -- v\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n -- ^\n -- postcondition is conjunction\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s')\n = (flatten t' == (seq (s) (size t))) -- flattening result t\u2019 produces sequence of numbers from s to s + size t\n \u2227 (s + size t == s') -- output state 's should be precisely size t larger than input state s\n\n -- increment current state; return value (before incr)\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n -- relabelling function\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n module _ where\n _ : Free C R (Tree Nat)\n _ = relabel (Node (Leaf 10) (Leaf 20))\n _ : relabel (Node (Leaf 10) (Leaf 20))\n \u2261 Step Get (\u03bb n1\n \u2192 Step (Put (Succ n1)) (\u03bb _\n \u2192 Step Get (\u03bb n2\n \u2192 Step (Put (Succ n2)) (\u03bb _\n \u2192 Pure (Node (Leaf n1) (Leaf n2))))))\n _ = refl\n\n _ : Pair (Tree Nat) Nat\n _ = run (relabel (Node (Leaf 10) (Leaf 20))) 1\n _ : run (relabel (Node (Leaf 10) (Leaf 20))) 1 \u2261 (Node (Leaf 1) (Leaf 2) , 3)\n _ = refl\n\n -- Show the definition satisfies the specification,\n -- via using 'wpState' to compute weakest precondition semantics of 'relabel'\n -- and formulating desired correctness property:\n correctnessRelabel : forall {a : Set}\n -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set}\n -> (c : State a)\n (f : a -> State b)\n -> \u2200 i P\n -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n -- Proof by induction on input tree.\n -- Proof of Node constructor case is proving\n -- statePT (relabel l >>= (\u03bb l\u2019 \u2192 relabel r >>= (\u03bb r\u2019 \u2192 Pure (Node l\u2019 r\u2019)))) (P (Node l r , i)) i\n -- Not obvious how apply induction hypothesis (IH states that P holds for l and r)\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification, by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence, specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b}\n ( mx : State a)\n ( f : a -> State b) P i\n -> statePT (wpState f \\_ -> P) mx i\n -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b}\n (mx : State a)\n (f : a -> State b)\n spec\n -> \u2200 P i\n -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i)\n -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P))\n -> statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s}\n -> SpecK {zero} (a \u00d7 s) (b \u00d7 s)\n -> a\n -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a)\n (trans (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr\n -> Pair (fl \u2261 seq s sl) (s + sl \u2261 s')\n -> Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'')\n -> Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s\n -> wpSpec relabelSpec P (t , s)\n -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set}\n -> ((x : a) -> Free C R (b x))\n -> ((x : a) -> b x -> Set)\n -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set}\n -> (c : Free C R a) (f : a -> Free C R b)\n -> \u2200 P\n -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P = cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R}\n -> (a -> Free C R b)\n -> (b -> Free C R c)\n -> a\n -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set}\n -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c)\n -> wpCR f1 \u2291 wpCR f2\n -> wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 Data.Nat.\u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n Data.Nat.\u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n Data.Nat.\u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x Data.Nat.\u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n Data.Nat.\u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n Data.Nat.\u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n Data.Nat.\u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n Data.Nat.\u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x Data.Nat.\u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n Data.Nat.\u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n Data.Nat.\u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n Data.Nat.\u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n Data.Nat.\u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","old_contents":"module PredicateTransformers where\n\nopen import Prelude hiding (map; all)\nopen import Level hiding (lift)\n\n------------------------------------------------------------------------------\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\n------------------------------------------------------------------------------\nABSTRACT\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nrefinement type https:\/\/en.wikipedia.org\/wiki\/Refinement_(computing)\n- type with a predicate which must hold for any element of the refined type\n- can express\n - preconditions when used as function args\n - postconditions when used as return types\n\nthe problem statement is centered around \/compositionality\/\n- pure functions enable compositional reasoning\n- effectful programs require reasoning about context\n- paper provides mechanism for reasoning about effectful programs (and for program calculation)\n\nmain idea\n- represent effectful computation as a free monad\n- write an \"interpreter\" that computes a predicate transformer\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to use this refinement relation to\n - show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\nmodule Free where\n -- Free : represents syntax of an effectful computation\n -- C : type of commands\n -- Free C R : returns an 'a' or issues command c : C\n -- For each c : C, there is a set of responses R c\n -- - R : C -> Set : is type family of responses for commands\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\" input to output\n -- input\n -- - pure function 'a\u2192b : a -> b'\n -- - postcondition 'b -> Set' (on output of a\u2192b)\n -- output\n -- - weakest precondition 'a -> Set' on input to a\u2192b that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n module WP0Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data isEven : \u2115 \u2192 Set where\n even-z : isEven Nat.zero\n even-s : {n : \u2115} \u2192 isEven n \u2192 isEven (Nat.suc (Nat.suc n))\n\n isEvenWP : \u2115 -> Set\n isEvenWP = wp0 (_+ 2) isEven\n\n _ : isEvenWP \u2261 isEven \u2218 (_+ 2)\n _ = refl\n\n _ : Set\n _ = isEvenWP 5\n\n _ : isEvenWP 5 \u2261 isEven 7\n _ = refl\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - enable via making dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n -- every element a that satisfies P is contained in the set of things that satifies Q\n --\n -- implication between predicates\n --\n -- P a says a \u2208 P\n -- if the extents of P, Q : A \u2192 Set is taken to be\n -- the subset of A satisfying predicates P, Q\n --\n -- the extent of P : A -> Set is the subset of terms of type A satisfying P.\n -- \"extent\" indicates a kind of black-box view of things,\n -- e.g., the \"extent\" of a function is the set of its input-output pairs.\n --\n -- https:\/\/en.wikipedia.org\/wiki\/Extension_%28semantics%29\n -- the extension of a concept, idea, or sign consists of the things to which it applies\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n module \u2286Test where\n open import Data.Nat using (\u2115; zero; suc; _+_)\n data _\u2264_ : \u2115 \u2192 \u2115 \u2192 Set where\n z\u2264n : \u2200 {n : \u2115} \u2192 0 \u2264 n\n s\u2264s : \u2200 {m n : \u2115} \u2192 m \u2264 n \u2192 Nat.suc m \u2264 Nat.suc n\n\n \u22641\u2286\u22642 : (_\u2264 1) \u2286 (_\u2264 2)\n -- P \u2286 Q\n -- \u2200 a -> P a -> Q a\n -- (a : \u2115) -> (a \u2264 1) -> (a \u2264 2)\n \u22641\u2286\u22642 Zero _ = z\u2264n\n \u22641\u2286\u22642 (Succ Zero) (s\u2264s _) = s\u2264s z\u2264n\n\n {- cannot be proved\n \u22642\u2286\u22641 : (_\u2264 2) \u2286 (_\u2264 1)\n \u22642\u2286\u22641 Zero _ = z\u2264n\n \u22642\u2286\u22641 (Succ a) (s\u2264s p) = {!!}\n -}\n\n -- refinement relation between PTs : pt1 refined by pt2\n -- (same thing as \u2286, but one level up)\n -- for every post condition, pt2 weakens the precondition\n -- \"weakest\" is the right side : pt2 P\n --\n -- pt1 , pt2 : preconditions for satisfying a program\n -- refinement : pt1 maybe be a stronger requirement than pt2\n --\n -- pt2 is a refinement because it weakens what is required by pt1\n -- e.g., pt1 pt2\n -- >= 20 >= 10\n --\n -- weakening the input assumptions gives a stronger result\n --\n -- contract says establish a pre and I will give you a post, i.e.,\n -- \"stronger\" post condition\n -- or \"weaker\" pre condition\n --\n -- refinement of predicate transformers\n --\n -- use case: relate different implementations of the \"same program\"\n -- pt1 \u2291 pt2 : pt1 is refined by pt2\n -- - if pt1 is understood as giving preconditions which satisfy\n -- postcondition P for some given program f1\n -- then his says the precondition can be \/weakened\/ and still guarantee the postcondition\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g\n -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation to\n - relate PT semantics between programs and specifications\n - show a program satisfies its specification\n - show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n - corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n note : for =Free C R b= programs means generating the same AST\n\n instead, define predicate transformers that give \/interpretations\/\n to a particular choice of command type C and response family R\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public using (_+_; _>_; _*_; s\u2264s; z\u2264n)\n renaming (\u2115 to Nat; zero to Zero; suc to Succ)\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n -- one command\n data C : Set where\n Abort : C -- no continuation\n\n -- response code : the program does not execute further\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n -- BIG-STEP SEMANTICS specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n module \u21d3Test where\n _ : Expr\n _ = Val 3\n _ : Expr\n _ = Div (Val 3) (Val 0)\n\n _ : Val 0 \u21d3 0\n _ = \u21d3Base\n\n _ : Val 3 \u21d3 3\n _ = \u21d3Base\n\n _ : Div (Val 3) (Val 3) \u21d3 1\n _ = \u21d3Step \u21d3Base \u21d3Base\n\n _ : Div (Val 9) (Val 3) \u21d3 3\n _ = \u21d3Step \u21d3Base \u21d3Base\n\n _ : Div (Val 9) (Div (Val 3) (Val 1)) \u21d3 3\n _ = \u21d3Step \u21d3Base (\u21d3Step \u21d3Base \u21d3Base)\n\n _ : Div (Div (Val 9) (Val 1)) (Div (Val 3) (Val 1)) \u21d3 3\n _ = \u21d3Step (\u21d3Step \u21d3Base \u21d3Base) (\u21d3Step \u21d3Base \u21d3Base)\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val n \u27e7 = return n\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\nl ->\n \u27e6 er \u27e7 >>= \\nr ->\n nl \u00f7 nr\n\n module InterpTest where\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n _ : evv \u2261 Pure 3\n _ = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n _ : evd \u2261 Pure 1\n _ = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n _ : evd0 \u2261 Step Abort (\u03bb ())\n _ = refl\n\n -- relate big step semantics to monadic interpreter\n\n -- convert post-condition for a pure function 'f : (x : A) -> b x'\n -- to post-condition on partial functions 'f : (x : A) -> Partial (b x)'\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba -- P holds if operation produces a value\n mustPT _ _ (Step Abort _) = \u22a5 -- if operation fails, there is no result to apply P to\n -- note: could give \u22a4 here, but want to rule out failure\n -- (total correctness)\n\n module MustPTTest where\n _ : Expr -> Nat -> Set\n _ = _\u21d3_\n\n _ : Set\n _ = Val 1 \u21d3 1\n\n _ : Expr -> Partial Nat -> Set\n _ = mustPT _\u21d3_\n\n _ : Set\n _ = mustPT _\u21d3_ (Val 1) (Pure 1)\n\n _ : mustPT _\u21d3_ (Val 1) (Pure 1)\n _ = \u21d3Base\n\n _ : Set\n _ = mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n {- a value of this type cannot be constructed\n _ : mustPT _\u21d3_ (Val 1) (Step Abort (\u03bb ()))\n _ = {!!}\n -}\n _ : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb n -> Pure 3 >>= _\u00f7 n))\n _ = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n _ : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n _ = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n _ : Set\n _ = mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n\n {-\n to relate big step semantics to monadic interpreter\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - _\u21d3_ relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n\n to call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results P' : Partial b -> Set\n - done via 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so mustPT Abort case returns empty type\n -}\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n module WpPartialTest where\n _ : Expr -> Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n _ = refl\n\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n _ = \u21d3Base\n\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n _ = \u21d3Step \u21d3Base \u21d3Base\n _ : Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n\n {- this type cannot be constructed\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n _ = {!!}\n -}\n _ : Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 0))\n\n {-\n wpPartial \u27e6_\u27e7 _\u21d3_ : a predicate on expressions\n - a way to express SUCCESSFUL evaluation of an Expr with intended semantics\n\n \u2200 exprs satisfying this predicate\n - monadic interpreter and\n - relational big-step specification\n must agree on result of evaluation\n\n for a given Expr, satisfaction of predicate means\n - Expr evaluates to a value by big-step operational semantics\n - if interpreter returns a value at all\n then that value agrees with big step semantics\n - if interpreter aborts\n then have a proof of \u22a5 (so, it does not abort)\n which will NOT agree with \u21d3 big-step semantics since \u21d3 does not allow div-by-0\n\n What does this say about correctness of interpreter?\n\n does not specify how many Expr that satisfy that characterization also satisfy wpPartial \u27e6_\u27e7 _\u21d3_\n\n to understand wpPartial \u27e6_\u27e7 _\u21d3_ better\n - define SafeDiv : safety criteria that we believe in\n - prove every expression that satisfies SafeDiv satisfies wpPartial \u27e6_\u27e7 _\u21d3_\n - SafeDiv refines wpPartial \u27e6_\u27e7 _\u21d3_\n - if we gave \u22a4 above, then 'correct' would not say that SafeDiv rules out undefined behavior\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val _) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n module SafeDivTest where\n _ : SafeDiv (Val 3) \u2261 \u22a4\n _ = refl\n _ : SafeDiv (Val 0) \u2261 \u22a4\n _ = refl\n _ : Set\n _ = SafeDiv (Val 0)\n\n _ : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 0) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n _ = refl\n _ : Set\n _ = SafeDiv (Div (Val 3) (Val 3))\n\n _ : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val 0 \u21d3 0) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n _ = refl\n _ : Set\n _ = SafeDiv (Div (Val 3) (Val 0))\n\n {-\n expect : any expr : e for which SafeDiv e holds can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n -- (a : Expr) (SafeDiv a) (wpPartial \u27e6_\u27e7 _\u21d3_ a)\n correct (Val _) _ = \u21d3Base\n correct (Div el er) (er\u21d30\u2192\u22a5 , (sdel , sder))\n with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n ... | Pure _ | Pure Zero | _ | er\u21d30 = magic (er\u21d30\u2192\u22a5 er\u21d30)\n ... | Pure _ | Pure (Succ _) | el\u21d3nl | er\u21d3Snr = \u21d3Step el\u21d3nl er\u21d3Snr\n ... | Pure _ | Step Abort _ | _ | ()\n ... | Step Abort _ | _ | () | _\n\n module CorrectTest where\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n _ = \u21d3Base\n\n _ : Set\n _ = wpPartial \u27e6_\u27e7 _\u21d3_ (Val 3)\n\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 1))\n _ = \u21d3Step \u21d3Base \u21d3Base\n {-\n _ : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 3) (Val 0))\n _ = {!!}\n -}\n {-\n Generalization.\n Instead of manually defining SafeDiv,\n define more general predicate\n characterising the domain of a partial function using wpPartial.\n -}\n\n -- domain is well-defined Exprs (i.e., no div-by-0)\n -- returns \u22a4 on Pure; \u22a5 on Step Abort ...\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n module DomTest where\n _ : dom \u27e6_\u27e7 \u2261 \u03bb z \u2192 mustPT (\u03bb x _ \u2192 \u22a4' zero) z \u27e6 z \u27e7\n _ = refl\n _ : dom \u27e6_\u27e7 (Val 0) \u2261 \u22a4' zero\n _ = refl\n _ : dom \u27e6_\u27e7 (Div (Val 1) (Val 0)) \u2261 \u22a5\n _ = refl\n\n -- can show that the two semantics agree precisely on the domain of the interpreter:\n\n -- everything in domain of interpreter\n -- gets evaluated in agreement with big step semantics\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n -- only things interpreted in agreement with big step semantics\n -- are those that are in domain of interpreter\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on argument expression\n\n sound (Val _) _ = \u21d3Base\n sound (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div _ _) () | Pure _ | Pure Zero | _ | _\n sound (Div _ _) _ | Pure nl | Pure (Succ nr) | \u22a4'zero\u2192el\u21d3nl | \u22a4'zero\u2192er\u21d3Succnr\n = \u21d3Step (\u22a4'zero\u2192el\u21d3nl tt) (\u22a4'zero\u2192er\u21d3Succnr tt)\n sound (Div _ _) () | Pure _ | Step Abort _ | _ | _\n sound (Div _ _) () | Step Abort _ | _ | _ | _\n\n inDom : {n : Nat}\n -> (e : Expr)\n -> \u27e6 e \u27e7 == Pure n\n -> dom \u27e6_\u27e7 e\n inDom (Val _) _ = tt\n inDom (Div el er) _ with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div _ _) () | Pure _ | Pure Zero\n inDom (Div _ _) _ | Pure _ | Pure (Succ _) = tt\n inDom (Div _ _) () | Pure _ | Step Abort _\n inDom (Div _ _) () | Step Abort _ | _\n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n : (e : Expr) (n : Nat)\n -> \u27e6 e \u27e7 \u2261 Pure n\n -> e \u21d3 n\n \u27e6e\u27e7\u2261Puren\u2192e\u21d3n e _ eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} Diveler\u21d3_nldivSuccn\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 { _} { _} () | Pure _ | _ | Pure Zero\n wpPartial1 {el} { _} _ | Pure nl | [[[ eqnl ]]] | Pure (Succ _) = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n el nl eqnl\n wpPartial1 { _} { _} () | Pure _ | _ | Step Abort _\n wpPartial1 { _} { _} () | Step Abort _ | _ | _\n\n wpPartial2 : {el er : Expr}\n -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er)\n -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} _ with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 { _} {er} _ | Pure _ | _ | Pure nr | [[[ eqnr ]]] = \u27e6e\u27e7\u2261Puren\u2192e\u21d3n er nr eqnr\n wpPartial2 { _} { _} () | Pure _ | _ | Step Abort _ | _\n wpPartial2 { _} { _} () | Step Abort _ | _ | _ | _\n\n complete (Val _) _ = tt\n complete (Div el er) h\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div _ _) h | Pure nl | [[[ \u27e6el\u27e7\u2261Purenl ]]] | Pure Zero | [[[ \u27e6er\u27e7\u2261Pure0 ]]] | _ | _\n -- Goal\n -- mustPT (\u03bb _ _ \u2192 \u22a4' zero)\n -- (Div el er) (Pure nl >>= (\u03bb nl' \u2192 Pure Zero >>= _\u00f7_ nl'))\n -- \u22a5\n -- Context\n -- h : mustPT _\u21d3_ (Div el er) (\u27e6 el \u27e7 >>= (\u03bb nl' \u2192 \u27e6 er \u27e7 >>= _\u00f7_ nl'))\n rewrite\n \u27e6el\u27e7\u2261Purenl -- h : mustPT _\u21d3_ (Div el er) (\u27e6 er \u27e7 >>= _\u00f7_ nl)\n | \u27e6er\u27e7\u2261Pure0 -- h : \u22a5\n = h -- magic h\n complete (Div _ _) _ | Pure _ | _ | Pure (Succ _) | _ | _ | _ = tt\n complete (Div _ _) _ | Pure _ | _ | Step Abort _ | _ | _ | ()\n complete (Div _ _) _ | Step Abort _ | _ | _ | _ | () | _\n\n module CompleteTest where\n {-\n _ : mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n _ = {!!}\n -}\n _ : Set\n _ = mustPT _\u21d3_ (Div (Val 1) (Val 0)) (\u27e6 Val 0 \u27e7 >>= _\u00f7_ 1)\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) <-> (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement\n - to relate Kleisli morphisms\n - to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n this example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre and post condition\n - to its implementation\n\n add top two elements; fails if stack has too few elements\n\n show how to prove definition meets its specification\n -}\n\n -- define specification in terms of a pre\/post condition\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n -- [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n -- for non-dependent examples (e.g., type b does not depend on x : a)\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function (to discard unused arg of type a)\n\n -- describes desired postcondition for addition function\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n -- spec for addition function\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n -- pre post\n {-\n need to relate spec to an implementation\n 'wpPartial' assigns predicate transformer semantics to functions\n 'wpSpec' assigns predicate transformer semantics to specifications\n -}\n -- given a spec, Spec a b\n -- computes weakest precondition that satisfies an arbitrary postcondition P\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\a -> (pre a) -- i.e., spec\u2019s precondition should hold and\n \u2227 (post a \u2286 P a) -- spec's postcondition must imply P\n\n -- using 'wpSpec' can now find a program 'add' that \"refines\" 'addSpec'\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs0 =\n pop xs0 >>= \\{(x1 , xs1) ->\n pop xs1 >>= \\{(x2 , xs2) ->\n return ((x1 + x2) :: xs2)}}\n\n -- verify correct\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd _ (_ :: Nil) (s\u2264s () , _)\n correctnessAdd P a@(x :: y :: xs ) (s\u2264s (s\u2264s z\u2264n) , post_addSpec_a_\u2286P_a)\n -- wpPartial add P (x :: y :: xs)\n = post_addSpec_a_\u2286P_a (x + y :: xs) AddStep\n -- paper version has \"extra\" 'Nil\" case\n--correctnessAdd P Nil ( () , _)\n--correctnessAdd P (_ :: Nil) (s\u2264s () , _)\n--correctnessAdd P (x :: y :: xs ) ( _ , H) = H (x + y :: xs) AddStep\n\n {-\n repeat: this example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition\n - to its implementation\n\n compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\n{-\n------------------------------------------------------------------------------\n4 Mutable State\n\npredicate transformer semantics for mutable state\ngiving rise to Hoare logic\n\nfollowing assumes a fixed type s : Set (the type of the state)\n-}\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n\n -- smart constructor\n get : State s\n get = Step Get return\n\n -- smart constructor\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n -- map free monad to the state monad\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n -- predicate transformer : for every stateful computation\n -- maps a postcondition on b \u00d7 s\n -- to the required precondition on s:\n statePT : forall {l l'} -> {b : Set l}\n -> (b \u00d7 s -> Set l')\n -> State b\n -> ( s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n -- generalise statePT\n -- Sometimes describe postconditions as a relation between inputs and outputs.\n -- For stateful computations, mean enabling the postcondition to also refer to the initial state:\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n -- weakest precondition semantics for Kleisli morphisms a \u2192 State b\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> ( a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> ( a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n -- Given predicate P relating input, initial state, final state and result,\n -- 'wpState' computes the weakest precondition required of input and initial state\n -- to ensure P holds upon completing the computation.\n -- The definition amounts to composing 'wp' and 'statePT' functions.\n\n -- prove soundness of this semantics with respect to the run function\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\n{-\nExample showing how to reason about stateful programs using weakest precondition semantics.\n\nVerification problem proposed by Hutton and Fulger [2008]\n- input : binary tree\n- relabel tree so each leaf has a unique number associated with it\n\nTypical solution uses state monad to keep track of next unused label.\n\nHutton and Fulger challenge : reason about the program, without expanding definition of monadic operations.\n\nExample shows how properties of refinement relation can be used to reason about arbitrary effects.\n-}\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n -- Specification.\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n -- precondition true regardless of input tree and initial state\n -- v\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n -- ^\n -- postcondition is conjunction\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s')\n = (flatten t' == (seq (s) (size t))) -- flattening result t\u2019 produces sequence of numbers from s to s + size t\n \u2227 (s + size t == s') -- output state 's should be precisely size t larger than input state s\n\n -- increment current state; return value (before incr)\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n -- relabelling function\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n module RelableTest where\n _ : Free C R (Tree Nat)\n _ = relabel (Node (Leaf 10) (Leaf 20))\n _ : relabel (Node (Leaf 10) (Leaf 20))\n \u2261 Step Get (\u03bb n1\n \u2192 Step (Put (Succ n1)) (\u03bb _\n \u2192 Step Get (\u03bb n2\n \u2192 Step (Put (Succ n2)) (\u03bb _\n \u2192 Pure (Node (Leaf n1) (Leaf n2))))))\n _ = refl\n\n _ : Pair (Tree Nat) Nat\n _ = run (relabel (Node (Leaf 10) (Leaf 20))) 1\n _ : run (relabel (Node (Leaf 10) (Leaf 20))) 1 \u2261 (Node (Leaf 1) (Leaf 2) , 3)\n _ = refl\n\n -- Show the definition satisfies the specification,\n -- via using 'wpState' to compute weakest precondition semantics of 'relabel'\n -- and formulating desired correctness property:\n correctnessRelabel : forall {a : Set}\n -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set}\n -> (c : State a)\n (f : a -> State b)\n -> \u2200 i P\n -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n -- Proof by induction on input tree.\n -- Proof of Node constructor case is proving\n -- statePT (relabel l >>= (\u03bb l\u2019 \u2192 relabel r >>= (\u03bb r\u2019 \u2192 Pure (Node l\u2019 r\u2019)))) (P (Node l r , i)) i\n -- Not obvious how apply induction hypothesis (IH states that P holds for l and r)\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification, by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence, specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b}\n ( mx : State a)\n ( f : a -> State b) P i\n -> statePT (wpState f \\_ -> P) mx i\n -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b}\n (mx : State a)\n (f : a -> State b)\n spec\n -> \u2200 P i\n -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i)\n -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P))\n -> statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s}\n -> SpecK {zero} (a \u00d7 s) (b \u00d7 s)\n -> a\n -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a)\n (trans (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr\n -> Pair (fl \u2261 seq s sl) (s + sl \u2261 s')\n -> Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'')\n -> Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s\n -> wpSpec relabelSpec P (t , s)\n -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set}\n -> ((x : a) -> Free C R (b x))\n -> ((x : a) -> b x -> Set)\n -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set}\n -> (c : Free C R a) (f : a -> Free C R b)\n -> \u2200 P\n -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P = cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R}\n -> (a -> Free C R b)\n -> (b -> Free C R c)\n -> a\n -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set}\n -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c)\n -> wpCR f1 \u2291 wpCR f2\n -> wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"147a00bc095894a35d063ba5f815e115c7798064","subject":"Working on polymorphic lists.","message":"Working on polymorphic lists.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/FOT\/FOTC\/Polymorphism\/List.agda","new_file":"notes\/FOT\/FOTC\/Polymorphism\/List.agda","new_contents":"------------------------------------------------------------------------------\n-- Testing polymorphic lists\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --universal-quantified-propositional-functions #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Polymorphism.List where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Data.Bool.Type\nopen import FOTC.Data.Nat.Type\n\n------------------------------------------------------------------------------\n-- Heterogeneous lists\ndata List : D \u2192 Set where\n lnil : List []\n lcons : \u2200 x {xs} \u2192 List xs \u2192 List (x \u2237 xs)\n\n-- Lists of total natural numbers\ndata ListN : D \u2192 Set where\n lnnil : ListN []\n lncons : \u2200 {n ns} \u2192 N n \u2192 ListN ns \u2192 ListN (n \u2237 ns)\n\n-- Lists of total Booleans\ndata ListB : D \u2192 Set where\n lbnil : ListB []\n lbcons : \u2200 {b bs} \u2192 Bool b \u2192 ListB bs \u2192 ListB (b \u2237 bs)\n\n-- Polymorphic lists.\n-- NB. The data type list is in *Set\u2081*.\ndata ListP (A : D \u2192 Set) : D \u2192 Set\u2081 where\n lnil : ListP A []\n lcons : \u2200 {x xs} \u2192 A x \u2192 ListP A xs \u2192 ListP A (x \u2237 xs)\n\nList\u2081 : D \u2192 Set\u2081\nList\u2081 = ListP (\u03bb d \u2192 d \u2261 d)\n\nListN\u2081 : D \u2192 Set\u2081\nListN\u2081 = ListP N\n\nListB\u2081 : D \u2192 Set\u2081\nListB\u2081 = ListP Bool\n","old_contents":"------------------------------------------------------------------------------\n-- Testing polymorphic lists\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --universal-quantified-propositional-functions #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Polymorphism.List where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\n\n------------------------------------------------------------------------------\n\n-- NB. The data type list is in *Set\u2081*.\ndata List (A : D \u2192 Set) : D \u2192 Set\u2081 where\n lnil : List A []\n lcons : \u2200 {x xs} \u2192 A x \u2192 List A xs \u2192 List A (x \u2237 xs)\n{-# ATP axiom lnil lcons #-}\n\npostulate foo : \u2200 x \u2192 x \u2261 x\n{-# ATP prove foo #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ade85e632519eda34331154c3a961ce6642a9497","subject":"adding cases for new IT rules","message":"adding cases for new IT rules\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"preservation.agda","new_file":"preservation.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nmodule preservation where\n preservation : {\u0394 : hctx} {d d' : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n \u0394 \u22a2 d \u21a6 d' \u2192\n \u0394 , \u2205 \u22a2 d' :: \u03c4\n preservation TAConst (Step _ _ _) = TAConst\n preservation (TAAp wt x wt\u2081) (Step x\u2081 (ITCast x\u2082 x\u2083 x\u2084) x\u2085) = TAAp wt x wt\u2081\n preservation (TAAp wt x\u2081 wt\u2081) (Step x\u2082 (ITLam x\u2083) x\u2084) = TAAp wt x\u2081 wt\u2081\n preservation (TAAp x x\u2081 x\u2082) (Step x\u2083 (ITNEHole x\u2084) x\u2085) = TAAp x x\u2081 x\u2082\n preservation (TAAp x x\u2081 x\u2082) (Step x\u2083 ITEHole x\u2085) = TAAp x x\u2081 x\u2082\n preservation (TACast wt x) (Step x\u2081 (ITCast x\u2082 x\u2083 x\u2084) x\u2085) = TACast wt x\n preservation (TACast wt x\u2081) (Step x\u2082 (ITLam x\u2083) x\u2084) = TACast wt x\u2081\n preservation (TACast x x\u2081) (Step x\u2082 (ITNEHole x\u2083) x\u2084) = TACast x x\u2081\n preservation (TACast x x\u2081) (Step x\u2082 ITEHole x\u2084) = TACast x x\u2081\n preservation (TAEHole x x\u2081) (Step x\u2082 x\u2083 x\u2084) = TAEHole x x\u2081\n preservation (TALam wt) (Step x\u2081 (ITCast x\u2082 x\u2083 x\u2084) x\u2085) = TALam wt\n preservation (TALam wt) (Step x\u2082 (ITLam x\u2083) x\u2084) = TALam wt\n preservation (TALam x\u2081) (Step x\u2082 (ITNEHole x\u2083) x\u2084) = TALam x\u2081\n preservation (TALam x\u2081) (Step x\u2082 ITEHole x\u2084) = TALam x\u2081\n preservation (TANEHole x wt x\u2081) (Step x\u2082 (ITCast x\u2083 x\u2084 x\u2085) x\u2086) = TANEHole x wt x\u2081\n preservation (TANEHole x x\u2081 x\u2082) (Step x\u2083 (ITNEHole x\u2084) x\u2085) = TANEHole x x\u2081 x\u2082\n preservation (TANEHole x x\u2081 x\u2082) (Step x\u2083 ITEHole x\u2085) = TANEHole x x\u2081 x\u2082\n preservation (TANEHole x\u2081 wt x\u2082) (Step x\u2083 (ITLam x\u2084) x\u2085) = TANEHole x\u2081 wt x\u2082\n preservation (TAVar x\u2081) step = abort (somenotnone (! x\u2081))\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nmodule preservation where\n preservation : {\u0394 : hctx} {d d' : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n \u0394 \u22a2 d \u21a6 d' \u2192\n \u0394 , \u2205 \u22a2 d' :: \u03c4\n preservation TAConst (Step _ _ _) = TAConst\n preservation (TAVar x\u2081) step = abort (somenotnone (! x\u2081))\n preservation (TALam wt) (Step x\u2082 (ITLam x\u2083) x\u2084) = TALam wt\n preservation (TALam wt) (Step x\u2081 (ITCast x\u2082 x\u2083 x\u2084) x\u2085) = TALam wt\n preservation (TAAp wt x\u2081 wt\u2081) (Step x\u2082 (ITLam x\u2083) x\u2084) = TAAp wt x\u2081 wt\u2081\n preservation (TAAp wt x wt\u2081) (Step x\u2081 (ITCast x\u2082 x\u2083 x\u2084) x\u2085) = TAAp wt x wt\u2081\n preservation (TAEHole x x\u2081) (Step x\u2082 x\u2083 x\u2084) = TAEHole x x\u2081\n preservation (TANEHole x\u2081 wt x\u2082) (Step x\u2083 (ITLam x\u2084) x\u2085) = TANEHole x\u2081 wt x\u2082\n preservation (TANEHole x wt x\u2081) (Step x\u2082 (ITCast x\u2083 x\u2084 x\u2085) x\u2086) = TANEHole x wt x\u2081\n preservation (TACast wt x\u2081) (Step x\u2082 (ITLam x\u2083) x\u2084) = TACast wt x\u2081\n preservation (TACast wt x) (Step x\u2081 (ITCast x\u2082 x\u2083 x\u2084) x\u2085) = TACast wt x\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"70def950c8925dc369db199a79e13cb59ff30970","subject":"adder: use mapAccum","message":"adder: use mapAccum\n","repos":"crypto-agda\/crypto-agda","old_file":"adder.agda","new_file":"adder.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Type\nopen import Data.Nat.NP\nopen import Data.Bool using (if_then_else_)\nimport Data.Vec as V\nopen V using (Vec; []; _\u2237_)\nopen import Function.NP hiding (id)\nopen import FunUniverse.Core hiding (_,_)\nopen import Data.Fin using (Fin; zero; suc; #_; inject+; raise) renaming (to\u2115 to Fin\u25b9\u2115)\n\nmodule adder where\n\nmodule FunAdder\n {t}\n {T : \u2605_ t}\n {funU : FunUniverse T}\n (funOps : FunOps funU)\n where\n\n open FunUniverse funU\n open FunOps funOps renaming (_\u2218_ to _`\u2218_)\n\n\n --iter : \u2200 {n A B S} \u2192 (S `\u00d7 A `\u2192 S `\u00d7 B) \u2192 S `\u00d7 `Vec A n `\u2192 `Vec B n\n\n msb-adder : \u2200 {n} \u2192 `Bits n `\u00d7 `Bits n `\u2192 `Bits n\n msb-adder = , zip > \u204f mapAccum full-adder \u204f snd\n\n -- TODO reverses all over the places... switch to lsb first?\n -- lsb\n adder : \u2200 {n} \u2192 `Bits n `\u00d7 `Bits n `\u2192 `Bits n\n adder = < reverse \u00d7 reverse > \u204f msb-adder \u204f reverse\n\n open import Data.Digit\n\n \u2115\u25b9`Bits : \u2200 \u2113 \u2192 \u2115 \u2192 `\ud835\udfd9 `\u2192 `Bits \u2113\n \u2115\u25b9`Bits \u2113 n\u2080 = constBits (V.reverse (L\u25b9V (L.map F\u25b9\ud835\udfda (proj\u2081 (toDigits 2 n\u2080)))))\n where open import Data.List as L\n open import Data.Product\n open import Data.Two\n L\u25b9V : \u2200 {n} \u2192 List \ud835\udfda \u2192 Vec \ud835\udfda n\n L\u25b9V {zero} xs = []\n L\u25b9V {suc n} [] = V.replicate 0\u2082\n L\u25b9V {suc n} (x \u2237 xs) = x \u2237 L\u25b9V xs\n F\u25b9\ud835\udfda : Fin 2 \u2192 \ud835\udfda\n F\u25b9\ud835\udfda zero = 0\u2082\n F\u25b9\ud835\udfda (suc _) = 1\u2082\n\n{-\nopen import IO\nimport IO.Primitive\n-}\nopen import Data.One\nopen import Data.Two\nopen import Data.Product\n--open import Coinduction\nopen import FunUniverse.Agda\n--open import Data.Nat.Show\nopen FunAdder agdaFunOps\nopen FunOps agdaFunOps\nimport FunUniverse.Cost as Cost\nmodule TimeCost = FunOps Cost.timeOps\n{-\nputBit : \ud835\udfda \u2192 IO \ud835\udfd9\nputBit 1\u2082 = putStr \"1\"\nputBit 0\u2082 = putStr \"0\"\nputBits : \u2200 {n} \u2192 Vec \ud835\udfda n \u2192 IO \ud835\udfd9\nputBits [] = return _\nputBits (x \u2237 bs) = \u266f putBit x >> \u266f putBits bs\n-}\narg1 = \u2115\u25b9`Bits 8 0x0b _\narg2 = \u2115\u25b9`Bits 8 0x1f _\nresult = adder (arg1 , arg2)\nadder-cost : \u2115 \u2192 \u2115\nadder-cost n = FunAdder.adder Cost.timeOps {n}\n{-\nmainIO : IO \ud835\udfd9\nmainIO = \u266f putBits arg1 >>\n \u266f (\u266f putStr \" + \" >>\n \u266f (\u266f putBits arg2 >>\n \u266f (\u266f putStr \" = \" >>\n \u266f (\u266f putBits result >>\n \u266f (\u266f putStr \" cost:\" >>\n \u266f putStr (show (adder-cost 8)))))))\nmain : IO.Primitive.IO \ud835\udfd9\nmain = IO.run mainIO\n-}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Type\nopen import Data.Nat.NP\nopen import Data.Bool using (if_then_else_)\nimport Data.Vec as V\nopen V using (Vec; []; _\u2237_)\nopen import Function.NP hiding (id)\nopen import FunUniverse.Core hiding (_,_)\nopen import Data.Fin using (Fin; zero; suc; #_; inject+; raise) renaming (to\u2115 to Fin\u25b9\u2115)\n\nmodule adder where\n\nmodule FunAdder\n {t}\n {T : \u2605_ t}\n {funU : FunUniverse T}\n (funOps : FunOps funU)\n where\n\n open FunUniverse funU\n open FunOps funOps renaming (_\u2218_ to _`\u2218_)\n\n\n --iter : \u2200 {n A B S} \u2192 (S `\u00d7 A `\u2192 S `\u00d7 B) \u2192 S `\u00d7 `Vec A n `\u2192 `Vec B n\n\n iter : \u2200 {n A B S} \u2192 (S `\u00d7 A `\u2192 S `\u00d7 B) \u2192 S `\u00d7 `Vec A n `\u2192 S `\u00d7 `Vec B n\n iter {zero} F = second <[]>\n iter {suc n} F = second uncons\n \u204f assoc-first F \u204f around (iter F)\n \u204f second <\u2237>\n\n msb-adder : \u2200 {n} \u2192 `Bits n `\u00d7 `Bits n `\u2192 `Bits n\n msb-adder = , zip > \u204f iter full-adder \u204f snd\n\n -- TODO reverses all over the places... switch to lsb first?\n -- lsb\n adder : \u2200 {n} \u2192 `Bits n `\u00d7 `Bits n `\u2192 `Bits n\n adder = < reverse \u00d7 reverse > \u204f msb-adder \u204f reverse\n\n open import Data.Digit\n\n \u2115\u25b9`Bits : \u2200 \u2113 \u2192 \u2115 \u2192 `\ud835\udfd9 `\u2192 `Bits \u2113\n \u2115\u25b9`Bits \u2113 n\u2080 = constBits (V.reverse (L\u25b9V (L.map F\u25b9\ud835\udfda (proj\u2081 (toDigits 2 n\u2080)))))\n where open import Data.List as L\n open import Data.Product\n open import Data.Two\n L\u25b9V : \u2200 {n} \u2192 List \ud835\udfda \u2192 Vec \ud835\udfda n\n L\u25b9V {zero} xs = []\n L\u25b9V {suc n} [] = V.replicate 0\u2082\n L\u25b9V {suc n} (x \u2237 xs) = x \u2237 L\u25b9V xs\n F\u25b9\ud835\udfda : Fin 2 \u2192 \ud835\udfda\n F\u25b9\ud835\udfda zero = 0\u2082\n F\u25b9\ud835\udfda (suc _) = 1\u2082\n\n{-\nopen import IO\nimport IO.Primitive\n-}\nopen import Data.One\nopen import Data.Two\nopen import Data.Product\n--open import Coinduction\nopen import FunUniverse.Agda\n--open import Data.Nat.Show\nopen FunAdder agdaFunOps\nopen FunOps agdaFunOps\nimport FunUniverse.Cost as Cost\nmodule TimeCost = FunOps Cost.timeOps\n{-\nputBit : \ud835\udfda \u2192 IO \ud835\udfd9\nputBit 1\u2082 = putStr \"1\"\nputBit 0\u2082 = putStr \"0\"\nputBits : \u2200 {n} \u2192 Vec \ud835\udfda n \u2192 IO \ud835\udfd9\nputBits [] = return _\nputBits (x \u2237 bs) = \u266f putBit x >> \u266f putBits bs\n-}\narg1 = \u2115\u25b9`Bits 8 0x0b _\narg2 = \u2115\u25b9`Bits 8 0x1f _\nresult = adder (arg1 , arg2)\nadder-cost : \u2115 \u2192 \u2115\nadder-cost n = FunAdder.adder Cost.timeOps {n}\n{-\nmainIO : IO \ud835\udfd9\nmainIO = \u266f putBits arg1 >>\n \u266f (\u266f putStr \" + \" >>\n \u266f (\u266f putBits arg2 >>\n \u266f (\u266f putStr \" = \" >>\n \u266f (\u266f putBits result >>\n \u266f (\u266f putStr \" cost:\" >>\n \u266f putStr (show (adder-cost 8)))))))\nmain : IO.Primitive.IO \ud835\udfd9\nmain = IO.run mainIO\n-}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"0281d874e9b8c1ea549242d7a1ca28f18df3bf90","subject":"Added xy\u2192yy\u2192yn = \u22a5-elim $ 0>x\u2192\u22a5 Nn 0>n\nx>y\u2192yy\u2192ySn =\n trans (<-SS n m) (x>y\u2192ySn))\n\nx\u2265y\u2192x\u226ey : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GE m n \u2192 NLT m n\nx\u2265y\u2192x\u226ey zN zN _ = x\u2270x zN\nx\u2265y\u2192x\u226ey zN (sN Nn) 0\u2265Sn = \u22a5-elim $ 0\u2265S\u2192\u22a5 Nn 0\u2265Sn\nx\u2265y\u2192x\u226ey (sN {m} Nm) zN _ = <-S0 m\nx\u2265y\u2192x\u226ey (sN {m} Nm) (sN {n} Nn) Sm\u2265Sn =\n trans (<-SS m n) (x\u2265y\u2192x\u226ey Nm Nn (trans (sym $ <-SS n (succ m)) Sm\u2265Sn))\n\nx>y\u2192x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 NLE m n\nx>y\u2192x\u2270y zN Nn 0>m = \u22a5-elim $ 0>x\u2192\u22a5 Nn 0>m\nx>y\u2192x\u2270y (sN Nm) zN _ = S\u22700 Nm\nx>y\u2192x\u2270y (sN {m} Nm) (sN {n} Nn) Sm>Sn =\n x\u2270y\u2192Sx\u2270Sy m n (x>y\u2192x\u2270y Nm Nn (trans (sym $ <-SS n m) Sm>Sn))\n\n-- TODO: To move to LTC.Data.Nat.Inequalities.Properties.\npostulate\n x>y\u2192x\u2264y\u2192\u22a5 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 LE m n \u2192 \u22a5\n{-# ATP prove x>y\u2192x\u2264y\u2192\u22a5 x>y\u2192x\u2270y #-}\n\nx>y\u2228x\u2264y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2228 LE m n\nx>y\u2228x\u2264y zN Nn = inj\u2082 $ x\u22650 Nn\nx>y\u2228x\u2264y (sN {m} Nm) zN = inj\u2081 $ <-0S m\nx>y\u2228x\u2264y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m>n \u2192 inj\u2081 (trans (<-SS n m) m>n))\n , (\u03bb m\u2264n \u2192 inj\u2082 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n ] (x>y\u2228x\u2264y Nm Nn)\n\nxy\u2228x\u2264y Nn Nm\n\nx\u2264y\u2228x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2228 NLE m n\nx\u2264y\u2228x\u2270y zN Nn = inj\u2081 (0\u2264x Nn)\nx\u2264y\u2228x\u2270y (sN Nm) zN = inj\u2082 (S\u22700 Nm)\nx\u2264y\u2228x\u2270y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m\u2264n \u2192 inj\u2081 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n , (\u03bb m\u2270n \u2192 inj\u2082 (x\u2270y\u2192Sx\u2270Sy m n m\u2270n))\n ] (x\u2264y\u2228x\u2270y Nm Nn)\n\nx\u2261y\u2192x\u2264y : \u2200 {m n} {Nm : N m} {Nn : N n} \u2192 m \u2261 n \u2192 LE m n\nx\u2261y\u2192x\u2264y {Nm = Nm} refl = x\u2264x Nm\n\nxy\u2192x\u2238y+y\u2261x : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 (m \u2238 n) + n \u2261 m\nx>y\u2192x\u2238y+y\u2261x zN Nn 0>n = \u22a5-elim $ 0>x\u2192\u22a5 Nn 0>n\nx>y\u2192x\u2238y+y\u2261x (sN {m} Nm) zN Sm>0 = prf\n where\n postulate prf : (succ m \u2238 zero) + zero \u2261 succ m\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx>y\u2192x\u2238y+y\u2261x (sN {m} Nm) (sN {n} Nn) Sm>Sn = prf $ x>y\u2192x\u2238y+y\u2261x Nm Nn m>n\n where\n postulate m>n : GT m n\n {-# ATP prove m>n #-}\n\n postulate prf : (m \u2238 n) + n \u2261 m \u2192 -- IH.\n (succ m \u2238 succ n) + succ n \u2261 succ m\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx\u2264y\u2192y\u2238x+x\u2261y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2192 (n \u2238 m) + m \u2261 n\nx\u2264y\u2192y\u2238x+x\u2261y {n = n} zN Nn 0\u2264n = prf\n where\n postulate prf : (n \u2238 zero) + zero \u2261 n\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx\u2264y\u2192y\u2238x+x\u2261y (sN Nm) zN Sm\u22640 = \u22a5-elim $ S\u22640\u2192\u22a5 Nm Sm\u22640\n\nx\u2264y\u2192y\u2238x+x\u2261y (sN {m} Nm) (sN {n} Nn) Sm\u2264Sn = prf $ x\u2264y\u2192y\u2238x+x\u2261y Nm Nn m\u2264n\n where\n postulate m\u2264n : LE m n\n {-# ATP prove m\u2264n #-}\n\n postulate prf : (n \u2238 m) + m \u2261 n \u2192 -- IH.\n (succ n \u2238 succ m) + succ m \u2261 succ n\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx0\u2192x\u2238y0\u2192x\u2238y0 = \u22a5-elim $ x>x\u2192\u22a5 zN 0>0\nx\u2265y\u2192y>0\u2192x\u2238y0\u2192x\u2238y0 = prf\n where\n postulate prf : LT (succ m \u2238 succ n) (succ m)\n {-# ATP prove prf x\u2238yy\u2192yy\u2192yn = \u22a5-elim $ 0>x\u2192\u22a5 Nn 0>n\nx>y\u2192yy\u2192ySn =\n trans (<-SS n m) (x>y\u2192ySn))\n\nx\u2265y\u2192x\u226ey : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GE m n \u2192 NLT m n\nx\u2265y\u2192x\u226ey zN zN _ = x\u2270x zN\nx\u2265y\u2192x\u226ey zN (sN Nn) 0\u2265Sn = \u22a5-elim $ 0\u2265S\u2192\u22a5 Nn 0\u2265Sn\nx\u2265y\u2192x\u226ey (sN {m} Nm) zN _ = <-S0 m\nx\u2265y\u2192x\u226ey (sN {m} Nm) (sN {n} Nn) Sm\u2265Sn =\n trans (<-SS m n) (x\u2265y\u2192x\u226ey Nm Nn (trans (sym $ <-SS n (succ m)) Sm\u2265Sn))\n\nx>y\u2192x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 NLE m n\nx>y\u2192x\u2270y zN Nn 0>m = \u22a5-elim $ 0>x\u2192\u22a5 Nn 0>m\nx>y\u2192x\u2270y (sN Nm) zN _ = S\u22700 Nm\nx>y\u2192x\u2270y (sN {m} Nm) (sN {n} Nn) Sm>Sn =\n x\u2270y\u2192Sx\u2270Sy m n (x>y\u2192x\u2270y Nm Nn (trans (sym $ <-SS n m) Sm>Sn))\n\n-- TODO: To move to LTC.Data.Nat.Inequalities.Properties.\npostulate\n x>y\u2192x\u2264y\u2192\u22a5 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 LE m n \u2192 \u22a5\n{-# ATP prove x>y\u2192x\u2264y\u2192\u22a5 x>y\u2192x\u2270y #-}\n\nx>y\u2228x\u2264y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2228 LE m n\nx>y\u2228x\u2264y zN Nn = inj\u2082 $ x\u22650 Nn\nx>y\u2228x\u2264y (sN {m} Nm) zN = inj\u2081 $ <-0S m\nx>y\u2228x\u2264y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m>n \u2192 inj\u2081 (trans (<-SS n m) m>n))\n , (\u03bb m\u2264n \u2192 inj\u2082 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n ] (x>y\u2228x\u2264y Nm Nn)\n\nxy\u2228x\u2264y Nn Nm\n\nx\u2264y\u2228x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2228 NLE m n\nx\u2264y\u2228x\u2270y zN Nn = inj\u2081 (0\u2264x Nn)\nx\u2264y\u2228x\u2270y (sN Nm) zN = inj\u2082 (S\u22700 Nm)\nx\u2264y\u2228x\u2270y (sN {m} Nm) (sN {n} Nn) =\n [ (\u03bb m\u2264n \u2192 inj\u2081 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n , (\u03bb m\u2270n \u2192 inj\u2082 (x\u2270y\u2192Sx\u2270Sy m n m\u2270n))\n ] (x\u2264y\u2228x\u2270y Nm Nn)\n\nx\u2261y\u2192x\u2264y : \u2200 {m n} {Nm : N m} {Nn : N n} \u2192 m \u2261 n \u2192 LE m n\nx\u2261y\u2192x\u2264y {Nm = Nm} refl = x\u2264x Nm\n\nxy\u2192x\u2238y+y\u2261x : \u2200 {m n} \u2192 N m \u2192 N n \u2192 GT m n \u2192 (m \u2238 n) + n \u2261 m\nx>y\u2192x\u2238y+y\u2261x zN Nn 0>n = \u22a5-elim $ 0>x\u2192\u22a5 Nn 0>n\nx>y\u2192x\u2238y+y\u2261x (sN {m} Nm) zN Sm>0 = prf\n where\n postulate prf : (succ m \u2238 zero) + zero \u2261 succ m\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx>y\u2192x\u2238y+y\u2261x (sN {m} Nm) (sN {n} Nn) Sm>Sn = prf $ x>y\u2192x\u2238y+y\u2261x Nm Nn m>n\n where\n postulate m>n : GT m n\n {-# ATP prove m>n #-}\n\n postulate prf : (m \u2238 n) + n \u2261 m \u2192 -- IH.\n (succ m \u2238 succ n) + succ n \u2261 succ m\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx\u2264y\u2192y\u2238x+x\u2261y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 LE m n \u2192 (n \u2238 m) + m \u2261 n\nx\u2264y\u2192y\u2238x+x\u2261y {n = n} zN Nn 0\u2264n = prf\n where\n postulate prf : (n \u2238 zero) + zero \u2261 n\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx\u2264y\u2192y\u2238x+x\u2261y (sN Nm) zN Sm\u22640 = \u22a5-elim $ S\u22640\u2192\u22a5 Nm Sm\u22640\n\nx\u2264y\u2192y\u2238x+x\u2261y (sN {m} Nm) (sN {n} Nn) Sm\u2264Sn = prf $ x\u2264y\u2192y\u2238x+x\u2261y Nm Nn m\u2264n\n where\n postulate m\u2264n : LE m n\n {-# ATP prove m\u2264n #-}\n\n postulate prf : (n \u2238 m) + m \u2261 n \u2192 -- IH.\n (succ n \u2238 succ m) + succ m \u2261 succ n\n -- Metis 2.3 (release 20101019): SZS status Unknown (using timeout 180 sec).\n -- Vampire 0.6 (revision 903): No-success (using timeout 180 sec).\n {-# ATP prove prf +-comm \u2238-N #-} -- Use the hint sN.\n\nx0\u2192x\u2238y0\u2192x\u2238y0 = \u22a5-elim $ x>x\u2192\u22a5 zN 0>0\nx\u2265y\u2192y>0\u2192x\u2238y0\u2192x\u2238y0 = prf\n where\n postulate prf : LT (succ m \u2238 succ n) (succ m)\n {-# ATP prove prf x\u2238y \u2237 as^\n as'-eq = as' \u2261\u27e8 as'ABP \u27e9\n await b i' is' ds' \u2261\u27e8 awaitCong\u2084 ds'-eq \u27e9\n await b i' is' (error \u2237 ds^) \u2261\u27e8 await-error _ _ _ _ \u27e9\n < i' , b > \u2237 await b i' is' ds^ \u2261\u27e8 refl \u27e9\n < i' , b > \u2237 as^ \u220e\n\n bs^ : D\n bs^ = corrupt os\u2080^ \u00b7 as^\n\n bs'-eq-helper\u2081 : os\u2080' \u2261 T \u2237 tail\u2081 os\u2080' \u2192 bs' \u2261 ok < i' , b > \u2237 bs^\n bs'-eq-helper\u2081 h =\n bs'\n \u2261\u27e8 bs'ABP' \u27e9\n corrupt os\u2080' \u00b7 as'\n \u2261\u27e8 subst\u2082 (\u03bb t t' \u2192 corrupt os\u2080' \u00b7 as' \u2261 corrupt t \u00b7 t')\n h\n as'-eq\n refl\n \u27e9\n corrupt (T \u2237 tail\u2081 os\u2080') \u00b7 (< i' , b > \u2237 as^)\n \u2261\u27e8 corrupt-T _ _ _ \u27e9\n ok < i' , b > \u2237 corrupt (tail\u2081 os\u2080') \u00b7 as^\n \u2261\u27e8 refl \u27e9\n ok < i' , b > \u2237 bs^ \u220e\n\n bs'-eq-helper\u2082 : os\u2080' \u2261 F \u2237 tail\u2081 os\u2080' \u2192 bs' \u2261 error \u2237 bs^\n bs'-eq-helper\u2082 h =\n bs'\n \u2261\u27e8 bs'ABP' \u27e9\n corrupt os\u2080' \u00b7 as'\n \u2261\u27e8 subst\u2082 (\u03bb t t' \u2192 corrupt os\u2080' \u00b7 as' \u2261 corrupt t \u00b7 t')\n h\n as'-eq\n refl\n \u27e9\n corrupt (F \u2237 tail\u2081 os\u2080') \u00b7 (< i' , b > \u2237 as^)\n \u2261\u27e8 corrupt-F _ _ _ \u27e9\n error \u2237 corrupt (tail\u2081 os\u2080') \u00b7 as^\n \u2261\u27e8 refl \u27e9\n error \u2237 bs^ \u220e\n\n bs'-eq : bs' \u2261 ok < i' , b > \u2237 bs^ \u2228 bs' \u2261 error \u2237 bs^\n bs'-eq = case (\u03bb h \u2192 inj\u2081 (bs'-eq-helper\u2081 h))\n (\u03bb h \u2192 inj\u2082 (bs'-eq-helper\u2082 h))\n (head-tail-Fair Fos\u2080')\n\n cs^ : D\n cs^ = ack (not b) \u00b7 bs^\n\n cs'-eq-helper\u2081 : bs' \u2261 ok < i' , b > \u2237 bs^ \u2192 cs' \u2261 b \u2237 cs^\n cs'-eq-helper\u2081 h =\n cs'\n \u2261\u27e8 cs'ABP' \u27e9\n ack (not b) \u00b7 bs'\n \u2261\u27e8 \u00b7-rightCong h \u27e9\n ack (not b) \u00b7 (ok < i' , b > \u2237 bs^)\n \u2261\u27e8 ack-ok\u2262 _ _ _ _ (not-x\u2262x Bb) \u27e9\n not (not b) \u2237 ack (not b) \u00b7 bs^\n \u2261\u27e8 \u2237-leftCong (not-involutive Bb) \u27e9\n b \u2237 ack (not b) \u00b7 bs^\n \u2261\u27e8 refl \u27e9\n b \u2237 cs^ \u220e\n\n cs'-eq-helper\u2082 : bs' \u2261 error \u2237 bs^ \u2192 cs' \u2261 b \u2237 cs^\n cs'-eq-helper\u2082 h =\n cs' \u2261\u27e8 cs'ABP' \u27e9\n ack (not b) \u00b7 bs' \u2261\u27e8 \u00b7-rightCong h \u27e9\n ack (not b) \u00b7 (error \u2237 bs^) \u2261\u27e8 ack-error _ _ \u27e9\n not (not b) \u2237 ack (not b) \u00b7 bs^ \u2261\u27e8 \u2237-leftCong (not-involutive Bb) \u27e9\n b \u2237 ack (not b) \u00b7 bs^ \u2261\u27e8 refl \u27e9\n b \u2237 cs^ \u220e\n\n cs'-eq : cs' \u2261 b \u2237 cs^\n cs'-eq = case cs'-eq-helper\u2081 cs'-eq-helper\u2082 bs'-eq\n\n js'-eq-helper\u2081 : bs' \u2261 ok < i' , b > \u2237 bs^ \u2192 js' \u2261 out (not b) \u00b7 bs^\n js'-eq-helper\u2081 h =\n js'\n \u2261\u27e8 js'ABP' \u27e9\n out (not b) \u00b7 bs'\n \u2261\u27e8 \u00b7-rightCong h \u27e9\n out (not b) \u00b7 (ok < i' , b > \u2237 bs^)\n \u2261\u27e8 out-ok\u2262 (not b) b i' bs^ (not-x\u2262x Bb) \u27e9\n out (not b) \u00b7 bs^ \u220e\n\n js'-eq-helper\u2082 : bs' \u2261 error \u2237 bs^ \u2192 js' \u2261 out (not b) \u00b7 bs^\n js'-eq-helper\u2082 h =\n js' \u2261\u27e8 js'ABP' \u27e9\n out (not b) \u00b7 bs' \u2261\u27e8 \u00b7-rightCong h \u27e9\n out (not b) \u00b7 (error \u2237 bs^) \u2261\u27e8 out-error (not b) bs^ \u27e9\n out (not b) \u00b7 bs^ \u220e\n\n js'-eq : js' \u2261 out (not b) \u00b7 bs^\n js'-eq = case js'-eq-helper\u2081 js'-eq-helper\u2082 bs'-eq\n\n ds^-eq : ds^ \u2261 corrupt os\u2081^ \u00b7 (b \u2237 cs^)\n ds^-eq = \u00b7-rightCong cs'-eq\n\n ABP'IH : ABP' b i' is' os\u2080^ os\u2081^ as^ bs^ cs^ ds^ js'\n ABP'IH = ds^-eq , refl , refl , refl , js'-eq\n\n------------------------------------------------------------------------------\n-- From Dybjer and Sander's paper: From the assumption that os\u2081 \u2208\n-- Fair, and hence by unfolding Fair we conclude that there are ft\u2081 :\n-- F*T and os\u2081'' : Fair, such that os\u2081' = ft\u2081 ++ os\u2081''.\n--\n-- We proceed by induction on ft\u2081 : F*T using helper.\n\nopen Helper\nlemma\u2082 : \u2200 {b i' is' os\u2080' os\u2081' as' bs' cs' ds' js'} \u2192\n Bit b \u2192\n Fair os\u2080' \u2192\n Fair os\u2081' \u2192\n ABP' b i' is' os\u2080' os\u2081' as' bs' cs' ds' js' \u2192\n \u2203[ os\u2080'' ] \u2203[ os\u2081'' ] \u2203[ as'' ] \u2203[ bs'' ] \u2203[ cs'' ] \u2203[ ds'' ]\n Fair os\u2080''\n \u2227 Fair os\u2081''\n \u2227 ABP (not b) is' os\u2080'' os\u2081'' as'' bs'' cs'' ds'' js'\nlemma\u2082 Bb Fos\u2080' Fos\u2081' abp' with Fair-unf Fos\u2081'\n... | ft , os\u2080'' , FTft , h , Fos\u2080'' =\n helper Bb Fos\u2080' abp' ft os\u2080'' FTft Fos\u2080'' h\n","old_contents":"------------------------------------------------------------------------------\n-- ABP lemma 2\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- From Dybjer and Sander's paper: The second lemma states that given\n-- a state of the latter kind (see lemma 1) we will arrive at a new\n-- start state, which is identical to the old start state except that\n-- the bit has alternated and the first item in the input stream has\n-- been removed.\n\nmodule FOTC.Program.ABP.Lemma2I where\n\nopen import Common.FOL.Relation.Binary.EqReasoning\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Base.List.PropertiesI\nopen import FOTC.Base.Loop\nopen import FOTC.Base.PropertiesI\nopen import FOTC.Data.Bool\nopen import FOTC.Data.Bool.PropertiesI\nopen import FOTC.Data.List\nopen import FOTC.Data.List.PropertiesI\nopen import FOTC.Program.ABP.ABP\nopen import FOTC.Program.ABP.Fair\nopen import FOTC.Program.ABP.Fair.PropertiesI\nopen import FOTC.Program.ABP.PropertiesI\nopen import FOTC.Program.ABP.Terms\n\n------------------------------------------------------------------------------\n-- Helper function for the ABP lemma 2\n\nmodule Helper where\n\n helper : \u2200 {b i' is' os\u2080' os\u2081' as' bs' cs' ds' js'} \u2192\n Bit b \u2192\n Fair os\u2080' \u2192\n ABP' b i' is' os\u2080' os\u2081' as' bs' cs' ds' js' \u2192\n \u2200 ft\u2081 os\u2081'' \u2192 F*T ft\u2081 \u2192 Fair os\u2081'' \u2192 os\u2081' \u2261 ft\u2081 ++ os\u2081'' \u2192\n \u2203[ os\u2080'' ] \u2203[ os\u2081'' ] \u2203[ as'' ] \u2203[ bs'' ] \u2203[ cs'' ] \u2203[ ds'' ]\n Fair os\u2080''\n \u2227 Fair os\u2081''\n \u2227 ABP (not b) is' os\u2080'' os\u2081'' as'' bs'' cs'' ds'' js'\n helper {b} {i'} {is'} {os\u2080'} {os\u2081'} {as'} {bs'} {cs'} {ds'} {js'}\n Bb Fos\u2080' (ds'ABP' , as'ABP , bs'ABP' , cs'ABP' , js'ABP')\n .(T \u2237 []) os\u2081'' f*tnil Fos\u2081'' os\u2081'-eq =\n os\u2080' , os\u2081'' , as'' , bs'' , cs'' , ds''\n , Fos\u2080' , Fos\u2081''\n , as''-eq , bs''-eq , cs''-eq , refl , js'-eq\n\n where\n os'\u2081-eq-helper : os\u2081' \u2261 T \u2237 os\u2081''\n os'\u2081-eq-helper =\n os\u2081' \u2261\u27e8 os\u2081'-eq \u27e9\n (true \u2237 []) ++ os\u2081'' \u2261\u27e8 ++-\u2237 true [] os\u2081'' \u27e9\n true \u2237 [] ++ os\u2081'' \u2261\u27e8 \u2237-rightCong (++-leftIdentity os\u2081'') \u27e9\n true \u2237 os\u2081'' \u220e\n\n ds'' : D\n ds'' = corrupt os\u2081'' \u00b7 cs'\n\n ds'-eq : ds' \u2261 ok b \u2237 ds''\n ds'-eq =\n ds' \u2261\u27e8 ds'ABP' \u27e9\n corrupt os\u2081' \u00b7 (b \u2237 cs')\n \u2261\u27e8 \u00b7-leftCong (corruptCong os'\u2081-eq-helper) \u27e9\n corrupt (T \u2237 os\u2081'') \u00b7 (b \u2237 cs')\n \u2261\u27e8 corrupt-T os\u2081'' b cs' \u27e9\n ok b \u2237 corrupt os\u2081'' \u00b7 cs'\n \u2261\u27e8 refl \u27e9\n ok b \u2237 ds'' \u220e\n\n as'' : D\n as'' = as'\n\n as''-eq : as'' \u2261 send (not b) \u00b7 is' \u00b7 ds''\n as''-eq =\n as'' \u2261\u27e8 as'ABP \u27e9\n await b i' is' ds' \u2261\u27e8 awaitCong\u2084 ds'-eq \u27e9\n await b i' is' (ok b \u2237 ds'') \u2261\u27e8 await-ok\u2261 b b i' is' ds'' refl \u27e9\n send (not b) \u00b7 is' \u00b7 ds'' \u220e\n\n bs'' : D\n bs'' = bs'\n\n bs''-eq : bs'' \u2261 corrupt os\u2080' \u00b7 as'\n bs''-eq = bs'ABP'\n\n cs'' : D\n cs'' = cs'\n\n cs''-eq : cs'' \u2261 ack (not b) \u00b7 bs'\n cs''-eq = cs'ABP'\n\n js'-eq : js' \u2261 out (not b) \u00b7 bs''\n js'-eq = js'ABP'\n\n helper {b} {i'} {is'} {os\u2080'} {os\u2081'} {as'} {bs'} {cs'} {ds'} {js'}\n Bb Fos\u2080' (ds'ABP' , as'ABP , bs'ABP' , cs'ABP' , js'ABP')\n .(F \u2237 ft\u2081) os\u2081'' (f*tcons {ft\u2081} FTft\u2081) Fos\u2081'' os\u2081'-eq\n = helper Bb (tail-Fair Fos\u2080') ABP'IH ft\u2081 os\u2081'' FTft\u2081 Fos\u2081'' refl\n\n where\n os\u2080^ : D\n os\u2080^ = tail\u2081 os\u2080'\n\n os\u2081^ : D\n os\u2081^ = ft\u2081 ++ os\u2081''\n\n os\u2081'-eq-helper : os\u2081' \u2261 F \u2237 os\u2081^\n os\u2081'-eq-helper = os\u2081' \u2261\u27e8 os\u2081'-eq \u27e9\n (F \u2237 ft\u2081) ++ os\u2081'' \u2261\u27e8 ++-\u2237 _ _ _ \u27e9\n F \u2237 ft\u2081 ++ os\u2081'' \u2261\u27e8 refl \u27e9\n F \u2237 os\u2081^ \u220e\n\n ds^ : D\n ds^ = corrupt os\u2081^ \u00b7 cs'\n\n ds'-eq : ds' \u2261 error \u2237 ds^\n ds'-eq =\n ds'\n \u2261\u27e8 ds'ABP' \u27e9\n corrupt os\u2081' \u00b7 (b \u2237 cs')\n \u2261\u27e8 \u00b7-leftCong (corruptCong os\u2081'-eq-helper) \u27e9\n corrupt (F \u2237 os\u2081^) \u00b7 (b \u2237 cs')\n \u2261\u27e8 corrupt-F _ _ _ \u27e9\n error \u2237 corrupt os\u2081^ \u00b7 cs'\n \u2261\u27e8 refl \u27e9\n error \u2237 ds^ \u220e\n\n as^ : D\n as^ = await b i' is' ds^\n\n as'-eq : as' \u2261 < i' , b > \u2237 as^\n as'-eq = as' \u2261\u27e8 as'ABP \u27e9\n await b i' is' ds' \u2261\u27e8 awaitCong\u2084 ds'-eq \u27e9\n await b i' is' (error \u2237 ds^) \u2261\u27e8 await-error _ _ _ _ \u27e9\n < i' , b > \u2237 await b i' is' ds^ \u2261\u27e8 refl \u27e9\n < i' , b > \u2237 as^ \u220e\n\n bs^ : D\n bs^ = corrupt os\u2080^ \u00b7 as^\n\n bs'-eq-helper\u2081 : os\u2080' \u2261 T \u2237 tail\u2081 os\u2080' \u2192 bs' \u2261 ok < i' , b > \u2237 bs^\n bs'-eq-helper\u2081 h =\n bs'\n \u2261\u27e8 bs'ABP' \u27e9\n corrupt os\u2080' \u00b7 as'\n \u2261\u27e8 subst\u2082 (\u03bb t t' \u2192 corrupt os\u2080' \u00b7 as' \u2261 corrupt t \u00b7 t')\n h\n as'-eq\n refl\n \u27e9\n corrupt (T \u2237 tail\u2081 os\u2080') \u00b7 (< i' , b > \u2237 as^)\n \u2261\u27e8 corrupt-T _ _ _ \u27e9\n ok < i' , b > \u2237 corrupt (tail\u2081 os\u2080') \u00b7 as^\n \u2261\u27e8 refl \u27e9\n ok < i' , b > \u2237 bs^ \u220e\n\n bs'-eq-helper\u2082 : os\u2080' \u2261 F \u2237 tail\u2081 os\u2080' \u2192 bs' \u2261 error \u2237 bs^\n bs'-eq-helper\u2082 h =\n bs'\n \u2261\u27e8 bs'ABP' \u27e9\n corrupt os\u2080' \u00b7 as'\n \u2261\u27e8 subst\u2082 (\u03bb t t' \u2192 corrupt os\u2080' \u00b7 as' \u2261 corrupt t \u00b7 t')\n h\n as'-eq\n refl\n \u27e9\n corrupt (F \u2237 tail\u2081 os\u2080') \u00b7 (< i' , b > \u2237 as^)\n \u2261\u27e8 corrupt-F _ _ _ \u27e9\n error \u2237 corrupt (tail\u2081 os\u2080') \u00b7 as^\n \u2261\u27e8 refl \u27e9\n error \u2237 bs^ \u220e\n\n bs'-eq : bs' \u2261 ok < i' , b > \u2237 bs^ \u2228 bs' \u2261 error \u2237 bs^\n bs'-eq = case (\u03bb h \u2192 inj\u2081 (bs'-eq-helper\u2081 h))\n (\u03bb h \u2192 inj\u2082 (bs'-eq-helper\u2082 h))\n (head-tail-Fair Fos\u2080')\n\n cs^ : D\n cs^ = ack (not b) \u00b7 bs^\n\n cs'-eq-helper\u2081 : bs' \u2261 ok < i' , b > \u2237 bs^ \u2192 cs' \u2261 b \u2237 cs^\n cs'-eq-helper\u2081 h =\n cs'\n \u2261\u27e8 cs'ABP' \u27e9\n ack (not b) \u00b7 bs'\n \u2261\u27e8 \u00b7-rightCong h \u27e9\n ack (not b) \u00b7 (ok < i' , b > \u2237 bs^)\n \u2261\u27e8 ack-ok\u2262 _ _ _ _ (not-x\u2262x Bb) \u27e9\n not (not b) \u2237 ack (not b) \u00b7 bs^\n \u2261\u27e8 \u2237-leftCong (not-involutive Bb) \u27e9\n b \u2237 ack (not b) \u00b7 bs^\n \u2261\u27e8 refl \u27e9\n b \u2237 cs^ \u220e\n\n cs'-eq-helper\u2082 : bs' \u2261 error \u2237 bs^ \u2192 cs' \u2261 b \u2237 cs^\n cs'-eq-helper\u2082 h =\n cs' \u2261\u27e8 cs'ABP' \u27e9\n ack (not b) \u00b7 bs' \u2261\u27e8 \u00b7-rightCong h \u27e9\n ack (not b) \u00b7 (error \u2237 bs^) \u2261\u27e8 ack-error _ _ \u27e9\n not (not b) \u2237 ack (not b) \u00b7 bs^ \u2261\u27e8 \u2237-leftCong (not-involutive Bb) \u27e9\n b \u2237 ack (not b) \u00b7 bs^ \u2261\u27e8 refl \u27e9\n b \u2237 cs^ \u220e\n\n cs'-eq : cs' \u2261 b \u2237 cs^\n cs'-eq = case cs'-eq-helper\u2081 cs'-eq-helper\u2082 bs'-eq\n\n js'-eq-helper\u2081 : bs' \u2261 ok < i' , b > \u2237 bs^ \u2192 js' \u2261 out (not b) \u00b7 bs^\n js'-eq-helper\u2081 h =\n js'\n \u2261\u27e8 js'ABP' \u27e9\n out (not b) \u00b7 bs'\n \u2261\u27e8 \u00b7-rightCong h \u27e9\n out (not b) \u00b7 (ok < i' , b > \u2237 bs^)\n \u2261\u27e8 out-ok\u2262 (not b) b i' bs^ (not-x\u2262x Bb) \u27e9\n out (not b) \u00b7 bs^ \u220e\n\n js'-eq-helper\u2082 : bs' \u2261 error \u2237 bs^ \u2192 js' \u2261 out (not b) \u00b7 bs^\n js'-eq-helper\u2082 h =\n js' \u2261\u27e8 js'ABP' \u27e9\n out (not b) \u00b7 bs' \u2261\u27e8 \u00b7-rightCong h \u27e9\n out (not b) \u00b7 (error \u2237 bs^) \u2261\u27e8 out-error (not b) bs^ \u27e9\n out (not b) \u00b7 bs^ \u220e\n\n js'-eq : js' \u2261 out (not b) \u00b7 bs^\n js'-eq = case js'-eq-helper\u2081 js'-eq-helper\u2082 bs'-eq\n\n ds^-eq : ds^ \u2261 corrupt os\u2081^ \u00b7 (b \u2237 cs^)\n ds^-eq = \u00b7-rightCong cs'-eq\n\n ABP'IH : ABP' b i' is' os\u2080^ os\u2081^ as^ bs^ cs^ ds^ js'\n ABP'IH = ds^-eq , refl , refl , refl , js'-eq\n\n------------------------------------------------------------------------------\n-- From Dybjer and Sander's paper: From the assumption that os\u2081 \u2208\n-- Fair, and hence by unfolding Fair we conclude that there are ft\u2081 :\n-- F*T and os\u2081'' : Fair, such that os\u2081' = ft\u2081 ++ os\u2081''.\n--\n-- We proceed by induction on ft\u2081 : F*T using helper.\n\nopen Helper\nlemma\u2082 : \u2200 {b i' is' os\u2080' os\u2081' as' bs' cs' ds' js'} \u2192\n Bit b \u2192\n Fair os\u2080' \u2192\n Fair os\u2081' \u2192\n ABP' b i' is' os\u2080' os\u2081' as' bs' cs' ds' js' \u2192\n \u2203[ os\u2080'' ] \u2203[ os\u2081'' ] \u2203[ as'' ] \u2203[ bs'' ] \u2203[ cs'' ] \u2203[ ds'' ]\n Fair os\u2080''\n \u2227 Fair os\u2081''\n \u2227 ABP (not b) is' os\u2080'' os\u2081'' as'' bs'' cs'' ds'' js'\nlemma\u2082 Bb Fos\u2080' Fos\u2081' abp' with Fair-unf Fos\u2081'\n... | ft , os\u2080'' , FTft , h , Fos\u2080'' =\n helper Bb Fos\u2080' abp' ft os\u2080'' FTft Fos\u2080'' h\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"079485362fad6d34f46d04d1da4c3327a2c8b742","subject":"Uncomment","message":"Uncomment\n","repos":"spire\/spire","old_file":"proposal\/examples\/HierMatchExt.agda","new_file":"proposal\/examples\/HierMatchExt.agda","new_contents":"{-\nDemonstrating the technique of using type-changing functions\nto pattern match against types with higher-order arguments\nlike \u03a3 and \u03a0.\n\nThe technique was originally explored to compare higher-order\nsmall arguments that occur in \u03a6 types in\nTacticsMatchingFunctionCalls.agda\n\nThis file shows that the technique scales to large higher-order\nfunctions , in a closed universe with a predicative hierarchy\nof levels that supports elimType.\n\n-}\n\nopen import Data.Empty\nopen import Data.Unit\nopen import Data.Bool\nopen import Data.Nat\nopen import Data.Fin hiding ( _+_ )\nopen import Data.Fin.Props\nopen import Data.Product\nopen import Data.String\nopen import Function\nopen import Relation.Binary.PropositionalEquality hiding ( inspect )\nopen Deprecated-inspect\nmodule HierMatchExt where\n\n----------------------------------------------------------------------\n\nplusrident : (n : \u2115) \u2192 n + 0 \u2261 n\nplusrident zero = refl\nplusrident (suc n) = cong suc (plusrident n)\n\n----------------------------------------------------------------------\n\nrecord Universe : Set\u2081 where\n field\n Codes : Set\n Meaning : Codes \u2192 Set\n\n----------------------------------------------------------------------\n\ndata Even : \u2115 \u2192 Set where\n ezero : Even 0\n esuc : {n : \u2115} \u2192 Even n \u2192 Even (2 + n)\n\ndata Odd : \u2115 \u2192 Set where\n ozero : Odd 1\n osuc : {n : \u2115} \u2192 Odd n \u2192 Odd (2 + n)\n\n----------------------------------------------------------------------\n\ndata TypeForm (U : Universe) : Set\n\u27e6_\/_\u27e7 : (U : Universe) \u2192 TypeForm U \u2192 Set\n\ndata TypeForm U where\n `\u22a5 `\u22a4 `Bool `\u2115 `Type : TypeForm U\n `Fin `Even `Odd : (n : \u2115) \u2192 TypeForm U\n `\u03a0 `\u03a3 : (A : TypeForm U)\n (B : \u27e6 U \/ A \u27e7 \u2192 TypeForm U)\n \u2192 TypeForm U\n `Id : (A : TypeForm U) (x y : \u27e6 U \/ A \u27e7) \u2192 TypeForm U\n `\u27e6_\u27e7 : (A : Universe.Codes U) \u2192 TypeForm U\n\n\u27e6 U \/ `\u22a5 \u27e7 = \u22a5\n\u27e6 U \/ `\u22a4 \u27e7 = \u22a4\n\u27e6 U \/ `Bool \u27e7 = Bool\n\u27e6 U \/ `\u2115 \u27e7 = \u2115\n\u27e6 U \/ `Fin n \u27e7 = Fin n\n\u27e6 U \/ `Even n \u27e7 = Even n\n\u27e6 U \/ `Odd n \u27e7 = Odd n\n\u27e6 U \/ `\u03a0 A B \u27e7 = (a : \u27e6 U \/ A \u27e7) \u2192 \u27e6 U \/ B a \u27e7\n\u27e6 U \/ `\u03a3 A B \u27e7 = \u03a3 \u27e6 U \/ A \u27e7 (\u03bb a \u2192 \u27e6 U \/ B a \u27e7)\n\u27e6 U \/ `Id A x y \u27e7 = _\u2261_ {A = \u27e6 U \/ A \u27e7} x y\n\u27e6 U \/ `Type \u27e7 = Universe.Codes U\n\u27e6 U \/ `\u27e6 A \u27e7 \u27e7 = Universe.Meaning U A\n\n----------------------------------------------------------------------\n\n_`\u2192_ : \u2200{U} (A B : TypeForm U) \u2192 TypeForm U\nA `\u2192 B = `\u03a0 A (\u03bb _ \u2192 B)\n\n_`\u00d7_ : \u2200{U} (A B : TypeForm U) \u2192 TypeForm U\nA `\u00d7 B = `\u03a3 A (\u03bb _ \u2192 B)\n\nLevel : (\u2113 : \u2115) \u2192 Universe\nLevel zero = record { Codes = \u22a5 ; Meaning = \u03bb() }\nLevel (suc \u2113) = record { Codes = TypeForm (Level \u2113)\n ; Meaning = \u27e6_\/_\u27e7 (Level \u2113) }\n\nType : \u2115 \u2192 Set\nType \u2113 = TypeForm (Level \u2113)\n\n\u27e6_\u2223_\u27e7 : (\u2113 : \u2115) \u2192 Type \u2113 \u2192 Set\n\u27e6 \u2113 \u2223 A \u27e7 = \u27e6 Level \u2113 \/ A \u27e7\n\n`Dynamic : (\u2113 : \u2115) \u2192 Type (suc \u2113)\n`Dynamic \u2113 = `\u03a3 `Type `\u27e6_\u27e7\n\n`Tactic : (\u2113 : \u2115) \u2192 Type (suc \u2113)\n`Tactic \u2113 = `Dynamic \u2113 `\u2192 `Dynamic \u2113\n\nTactic : (\u2113 : \u2115) \u2192 Set\nTactic \u2113 = \u27e6 suc \u2113 \u2223 `Tactic \u2113 \u27e7\n\n----------------------------------------------------------------------\n\nrm-plus0-Fin : (\u2113 : \u2115) \u2192 Tactic (suc \u2113)\nrm-plus0-Fin \u2113 (`\u27e6 `\u03a3 `\u2115 B \u27e7 , (n , b))\n with inspect (B n)\n... | `Fin m with-\u2261 p =\n `\u03a0 `\u2115 (\u03bb x \u2192 `Id `Type (B x) (`Fin (x + 0)))\n `\u2192\n `\u03a3 `\u2115 `Fin\n ,\n \u03bb f \u2192 n , subst Fin\n (plusrident n)\n (subst (\u03bb A \u2192 \u27e6 \u2113 \u2223 A \u27e7) (f n) b)\n... | Bn with-\u2261 p = (`\u27e6 `\u03a3 `\u2115 B \u27e7 , (n , subst (\u03bb x \u2192 \u27e6 \u2113 \u2223 B x \u27e7) refl b))\nrm-plus0-Fin \u2113 x = x\n\n----------------------------------------------------------------------\n\neg-rm-plus0-Fin : (\u2113 : \u2115) \u2192\n \u27e6 suc \u2113 \u2223 `\u27e6 `\u03a3 `\u2115 (\u03bb n \u2192 `Fin (n + zero)) \u27e7\n `\u2192 `\u27e6 `\u03a3 `\u2115 (\u03bb n \u2192 `Fin n) \u27e7 \u27e7\neg-rm-plus0-Fin \u2113 (n , i) = proj\u2082\n (rm-plus0-Fin \u2113 (`\u27e6 `\u03a3 `\u2115 (\u03bb n \u2192 `Fin (n + zero)) \u27e7 , (n , i)))\n (\u03bb _ \u2192 refl)\n\n----------------------------------------------------------------------\n","old_contents":"{-\nDemonstrating the technique of using type-changing functions\nto pattern match against types with higher-order arguments\nlike \u03a3 and \u03a0.\n\nThe technique was originally explored to compare higher-order\nsmall arguments that occur in \u03a6 types in\nTacticsMatchingFunctionCalls.agda\n\nThis file shows that the technique scales to large higher-order\nfunctions , in a closed universe with a predicative hierarchy\nof levels that supports elimType.\n\n-}\n\nopen import Data.Empty\nopen import Data.Unit\nopen import Data.Bool\nopen import Data.Nat\nopen import Data.Fin hiding ( _+_ )\nopen import Data.Fin.Props\nopen import Data.Product\nopen import Data.String\nopen import Function\nopen import Relation.Binary.PropositionalEquality hiding ( inspect )\nopen Deprecated-inspect\nmodule HierMatchExt where\n\n----------------------------------------------------------------------\n\nplusrident : (n : \u2115) \u2192 n + 0 \u2261 n\nplusrident zero = refl\nplusrident (suc n) = cong suc (plusrident n)\n\n----------------------------------------------------------------------\n\nrecord Universe : Set\u2081 where\n field\n Codes : Set\n Meaning : Codes \u2192 Set\n\n----------------------------------------------------------------------\n\ndata Even : \u2115 \u2192 Set where\n ezero : Even 0\n esuc : {n : \u2115} \u2192 Even n \u2192 Even (2 + n)\n\ndata Odd : \u2115 \u2192 Set where\n ozero : Odd 1\n osuc : {n : \u2115} \u2192 Odd n \u2192 Odd (2 + n)\n\n----------------------------------------------------------------------\n\ndata TypeForm (U : Universe) : Set\n\u27e6_\/_\u27e7 : (U : Universe) \u2192 TypeForm U \u2192 Set\n\ndata TypeForm U where\n `\u22a5 `\u22a4 `Bool `\u2115 `Type : TypeForm U\n `Fin `Even `Odd : (n : \u2115) \u2192 TypeForm U\n `\u03a0 `\u03a3 : (A : TypeForm U)\n (B : \u27e6 U \/ A \u27e7 \u2192 TypeForm U)\n \u2192 TypeForm U\n `Id : (A : TypeForm U) (x y : \u27e6 U \/ A \u27e7) \u2192 TypeForm U\n `\u27e6_\u27e7 : (A : Universe.Codes U) \u2192 TypeForm U\n\n\u27e6 U \/ `\u22a5 \u27e7 = \u22a5\n\u27e6 U \/ `\u22a4 \u27e7 = \u22a4\n\u27e6 U \/ `Bool \u27e7 = Bool\n\u27e6 U \/ `\u2115 \u27e7 = \u2115\n\u27e6 U \/ `Fin n \u27e7 = Fin n\n\u27e6 U \/ `Even n \u27e7 = Even n\n\u27e6 U \/ `Odd n \u27e7 = Odd n\n\u27e6 U \/ `\u03a0 A B \u27e7 = (a : \u27e6 U \/ A \u27e7) \u2192 \u27e6 U \/ B a \u27e7\n\u27e6 U \/ `\u03a3 A B \u27e7 = \u03a3 \u27e6 U \/ A \u27e7 (\u03bb a \u2192 \u27e6 U \/ B a \u27e7)\n\u27e6 U \/ `Id A x y \u27e7 = _\u2261_ {A = \u27e6 U \/ A \u27e7} x y\n\u27e6 U \/ `Type \u27e7 = Universe.Codes U\n\u27e6 U \/ `\u27e6 A \u27e7 \u27e7 = Universe.Meaning U A\n\n----------------------------------------------------------------------\n\n_`\u2192_ : \u2200{U} (A B : TypeForm U) \u2192 TypeForm U\nA `\u2192 B = `\u03a0 A (\u03bb _ \u2192 B)\n\n_`\u00d7_ : \u2200{U} (A B : TypeForm U) \u2192 TypeForm U\nA `\u00d7 B = `\u03a3 A (\u03bb _ \u2192 B)\n\nLevel : (\u2113 : \u2115) \u2192 Universe\nLevel zero = record { Codes = \u22a5 ; Meaning = \u03bb() }\nLevel (suc \u2113) = record { Codes = TypeForm (Level \u2113)\n ; Meaning = \u27e6_\/_\u27e7 (Level \u2113) }\n\nType : \u2115 \u2192 Set\nType \u2113 = TypeForm (Level \u2113)\n\n\u27e6_\u2223_\u27e7 : (\u2113 : \u2115) \u2192 Type \u2113 \u2192 Set\n\u27e6 \u2113 \u2223 A \u27e7 = \u27e6 Level \u2113 \/ A \u27e7\n\n`Dynamic : (\u2113 : \u2115) \u2192 Type (suc \u2113)\n`Dynamic \u2113 = `\u03a3 `Type `\u27e6_\u27e7\n\n`Tactic : (\u2113 : \u2115) \u2192 Type (suc \u2113)\n`Tactic \u2113 = `Dynamic \u2113 `\u2192 `Dynamic \u2113\n\nTactic : (\u2113 : \u2115) \u2192 Set\nTactic \u2113 = \u27e6 suc \u2113 \u2223 `Tactic \u2113 \u27e7\n\n----------------------------------------------------------------------\n\nrm-plus0-Fin : (\u2113 : \u2115) \u2192 Tactic (suc \u2113)\nrm-plus0-Fin \u2113 (`\u27e6 `\u03a3 `\u2115 B \u27e7 , (n , b))\n with inspect (B n)\n... | `Fin m with-\u2261 p =\n `\u03a0 `\u2115 (\u03bb x \u2192 `Id `Type (B x) (`Fin (x + 0)))\n `\u2192\n `\u03a3 `\u2115 `Fin\n ,\n \u03bb f \u2192 n , subst Fin\n (plusrident n)\n (subst (\u03bb A \u2192 \u27e6 \u2113 \u2223 A \u27e7) (f n) b)\n... | Bn with-\u2261 p = (`\u27e6 `\u03a3 `\u2115 B \u27e7 , (n , subst (\u03bb x \u2192 \u27e6 \u2113 \u2223 B x \u27e7) refl b))\nrm-plus0-Fin \u2113 x = x\n\n----------------------------------------------------------------------\n\n-- eg-rm-plus0-Fin : (\u2113 : \u2115) \u2192\n-- \u27e6 suc \u2113 \u2223 `\u27e6 `\u03a3 `\u2115 (\u03bb n \u2192 `Fin (n + zero)) \u27e7\n-- `\u2192 `\u27e6 `\u03a3 `\u2115 (\u03bb n \u2192 `Fin n) \u27e7 \u27e7\n-- eg-rm-plus0-Fin \u2113 (n , i) = proj\u2082\n-- (rm-plus0-Fin \u2113 (`\u27e6 `\u03a3 `\u2115 (\u03bb n \u2192 `Fin (n + zero)) \u27e7 , (n , i)))\n-- (\u03bb _ \u2192 refl)\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"49783fece9819834dc0920e36e937d1a820c0909","subject":"flat-funs: more polishing work","message":"flat-funs: more polishing work\n","repos":"crypto-agda\/crypto-agda","old_file":"flat-funs.agda","new_file":"flat-funs.agda","new_contents":"module flat-funs where\n\nopen import Data.Unit using (\u22a4)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2^_; _\u2294_)\nopen import Data.Fin using (Fin) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nimport Level as L\nimport Function as F\nimport Data.Product as \u00d7\nopen V using (Vec; []; _\u2237_; _++_)\nopen \u00d7 using (_\u00d7_; _,_; proj\u2081; proj\u2082; uncurry)\n\nopen import Data.Bits using (Bits; _\u2192\u1d47_)\n\nopen import universe\n\nrecord FlatFuns {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n universe : Universe T\n El : T \u2192 Set\n _`\u2192_ : T \u2192 T \u2192 Set\n infix 0 _`\u2192_\n open Universe universe public\n\nrecord FlatFunsOps {t} {T : Set t} (\u266dFuns : FlatFuns T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n infixr 1 _>>>_\n infixr 3 _***_\n infixr 3 _&&&_\n field\n -- Functions\n id : \u2200 {A} \u2192 A `\u2192 A\n _>>>_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (B `\u2192 C) \u2192 (A `\u2192 C)\n const : \u2200 {_A B} \u2192 El B \u2192 _A `\u2192 B\n\n -- Products\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n\n -- Unit\n tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4\n\n -- Vectors\n nil : \u2200 {_A B} \u2192 _A `\u2192 `Vec B 0\n cons : \u2200 {n A} \u2192 (A `\u00d7 `Vec A n) `\u2192 `Vec A (1 + n)\n uncons : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 (A `\u00d7 `Vec A n)\n\n _\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)\n f \u2218 g = g >>> f\n\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n f &&& g = < f , g >\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = < fst >>> f , snd >>> g >\n\n _***_ : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n f *** g = < f \u00d7 g >\n\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n swap = < snd , fst >\n\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n first f = f *** id\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = id *** f\n\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc = < fst >>> fst , first snd >\n\n assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n assoc\u2032 = < second fst , snd >>> snd >\n\n <_`zip`_> : \u2200 {A B C D E F} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 ((D `\u00d7 E) `\u2192 F)\n \u2192 ((A `\u00d7 D) `\u00d7 (B `\u00d7 E)) `\u2192 (C `\u00d7 F)\n < f `zip` g > = < < fst \u00d7 fst > >>> f ,\n < snd \u00d7 snd > >>> g >\n\n head : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n head = uncons >>> fst\n\n tail : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n tail = uncons >>> snd\n\n <_\u2237_> : \u2200 {m n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A m `\u2192 `Vec B n)\n \u2192 `Vec A (1 + m) `\u2192 `Vec B (1 + n)\n < f \u2237 g > = uncons >>> < f \u00d7 g > >>> cons\n\n <_\u2237\u2032_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n < f \u2237\u2032 g > = < f , g > >>> cons\n\n foldl : \u2200 {n A B} \u2192 (B `\u00d7 A `\u2192 B) \u2192 (B `\u00d7 `Vec A n) `\u2192 B\n foldl {zero} f = fst\n foldl {suc n} f = second uncons\n >>> assoc\u2032\n >>> first f\n >>> foldl f\n\n foldl\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldl\u2081 f = uncons >>> foldl f\n\n foldr : \u2200 {n A B} \u2192 (A `\u00d7 B `\u2192 B) \u2192 (`Vec A n `\u00d7 B) `\u2192 B\n foldr {zero} f = snd\n foldr {suc n} f = first uncons\n >>> assoc\n >>> second (foldr f)\n >>> f\n\n foldr\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldr\u2081 f = uncons >>> swap >>> foldr f\n\n map : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A n `\u2192 `Vec B n)\n map {zero} f = nil\n map {suc n} f = < f \u2237 map f >\n\n zipWith : \u2200 {n A B C} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec C n\n zipWith {zero} f = nil\n zipWith {suc n} f = < uncons \u00d7 uncons >\n >>> < f `zip` (zipWith f) >\n >>> cons\n\n zip : \u2200 {n A B} \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec (A `\u00d7 B) n\n zip = zipWith id\n\n singleton : \u2200 {A} \u2192 A `\u2192 `Vec A 1\n singleton = < id , nil > >>> cons\n\n snoc : \u2200 {n A} \u2192 (`Vec A n `\u00d7 A) `\u2192 `Vec A (1 + n)\n snoc {zero} = snd >>> singleton\n snoc {suc n} = first uncons >>> assoc >>> second snoc >>> cons\n\n reverse : \u2200 {n A} \u2192 `Vec A n `\u2192 `Vec A n\n reverse {zero} = nil\n reverse {suc n} = uncons >>> swap >>> first reverse >>> snoc\n\n append : \u2200 {m n A} \u2192 (`Vec A m `\u00d7 `Vec A n) `\u2192 `Vec A (m + n)\n append {zero} = snd\n append {suc m} = first uncons\n >>> assoc\n >>> second append\n >>> cons\n\n splitAt : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 (`Vec A m `\u00d7 `Vec A n)\n splitAt zero = < nil , id >\n splitAt (suc m) = uncons\n >>> second (splitAt m)\n >>> assoc\u2032\n >>> first cons\n\n take : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A m\n take zero = nil\n take (suc m) = < id \u2237 take m >\n\n drop : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A n\n drop zero = id\n drop (suc m) = tail >>> drop m\n\n folda : \u2200 n {A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (2^ n) `\u2192 A\n folda zero f = head\n folda (suc n) f = splitAt (2^ n) >>> < folda n f \u00d7 folda n f > >>> f\n\n init : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n init {zero} = nil\n init {suc n} = < id \u2237 init >\n\n last : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n last {zero} = head\n last {suc n} = tail >>> last\n\n concat : \u2200 {m n A} \u2192 `Vec (`Vec A m) n `\u2192 `Vec A (n * m)\n concat {n = zero} = nil\n concat {n = suc n} = uncons >>> second concat >>> append\n\n group : \u2200 {A} n k \u2192 `Vec A (n * k) `\u2192 `Vec (`Vec A k) n\n group zero k = nil\n group (suc n) k = splitAt k >>> second (group n k) >>> cons\n\n bind : \u2200 {m n A B} \u2192 (A `\u2192 `Vec B m) \u2192 `Vec A n `\u2192 `Vec B (n * m)\n bind f = map f >>> concat\n\n \u229b : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 `Vec A n `\u2192 `Vec B n\n \u229b [] = nil\n \u229b (f \u2237 fs) = uncons >>> < f \u00d7 \u229b fs > >>> cons\n\n replicate : \u2200 {n A} \u2192 A `\u2192 `Vec A n\n replicate {zero} = nil\n replicate {suc n} = < id , replicate > >>> cons\n\n module WithFin\n (`Fin : \u2115 \u2192 T)\n (fz : \u2200 {n _A} \u2192 _A `\u2192 `Fin (suc n))\n (fs : \u2200 {n} \u2192 `Fin n `\u2192 `Fin (suc n))\n (elim-Fin0 : \u2200 {A} \u2192 `Fin 0 `\u2192 A)\n (elim-Fin1+ : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Fin n `\u00d7 A `\u2192 B) \u2192 `Fin (suc n) `\u00d7 A `\u2192 B) where\n\n tabulate : \u2200 {n A _B} \u2192 (`Fin n `\u2192 A) \u2192 _B `\u2192 `Vec A n\n tabulate {zero} f = nil\n tabulate {suc n} f = < fz >>> f , tabulate (fs >>> f) > >>> cons\n\n lookup : \u2200 {n A} \u2192 `Fin n `\u00d7 `Vec A n `\u2192 A\n lookup {zero} = fst >>> elim-Fin0\n lookup {suc n} = elim-Fin1+ head (second tail >>> lookup)\n\n allFin : \u2200 {n _A} \u2192 _A `\u2192 `Vec (`Fin n) n\n allFin = tabulate id\n\n-\u2192- : Set \u2192 Set \u2192 Set\n-\u2192- A B = A \u2192 B\n\n_\u2192\u1da0_ : \u2115 \u2192 \u2115 \u2192 Set\n_\u2192\u1da0_ i o = Fin i \u2192 Fin o\n\nmapArr : \u2200 {s t} {S : Set s} {T : Set t} (F G : T \u2192 S)\n \u2192 (S \u2192 S \u2192 Set) \u2192 (T \u2192 T \u2192 Set)\nmapArr F G _`\u2192_ A B = F A `\u2192 G B\n\nfun\u266dFuns : FlatFuns Set\nfun\u266dFuns = mk Set-U F.id -\u2192-\n\nmodule FunTypes = FlatFuns fun\u266dFuns\n\nbitsFun\u266dFuns : FlatFuns \u2115\nbitsFun\u266dFuns = mk Bits-U Bits _\u2192\u1d47_\n\nmodule BitsFunTypes = FlatFuns bitsFun\u266dFuns\n\nfinFun\u266dFuns : FlatFuns \u2115\nfinFun\u266dFuns = mk Fin-U Fin _\u2192\u1da0_\n\nmodule FinFunTypes = FlatFuns finFun\u266dFuns\n\nfun\u266dOps : FlatFunsOps fun\u266dFuns\nfun\u266dOps = mk F.id (\u03bb f g x \u2192 g (f x)) F.const \u00d7.<_,_> proj\u2081 proj\u2082\n _ (F.const []) (uncurry _\u2237_) V.uncons\n\nmodule FunOps = FlatFunsOps fun\u266dOps\n\nbitsFun\u266dOps : FlatFunsOps bitsFun\u266dFuns\nbitsFun\u266dOps = mk id _>>>_ const <_,_> (\u03bb {A} \u2192 V.take A)\n (\u03bb {A} \u2192 V.drop A) (const []) (const []) id id\n where\n open FlatFuns bitsFun\u266dFuns\n open FlatFunsOps fun\u266dOps using (id; _>>>_; const)\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f , g > x = f x ++ g x\n\nmodule BitsFunOps = FlatFunsOps bitsFun\u266dOps\n\n\u00d7-\u266dFuns : \u2200 {s t} {S : Set s} {T : Set t} \u2192 FlatFuns S \u2192 FlatFuns T \u2192 FlatFuns (S \u00d7 T)\n\u00d7-\u266dFuns funs-S funs-T = mk (\u00d7-U S.universe T.universe)\n (\u03bb { (A\u2080 , A\u2081) \u2192 S.El A\u2080 \u00d7 T.El A\u2081 })\n (\u03bb { (A\u2080 , A\u2081) (B\u2080 , B\u2081) \u2192 (A\u2080 S.`\u2192 B\u2080) \u00d7 (A\u2081 T.`\u2192 B\u2081) })\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7\u22a4-\u266dFuns : \u2200 {s} {S : Set s} \u2192 FlatFuns S \u2192 FlatFuns \u22a4 \u2192 FlatFuns S\n\u00d7\u22a4-\u266dFuns funs-S funs-T = mk S.universe (\u03bb A \u2192 S.El A \u00d7 T.El _)\n (\u03bb A B \u2192 (A S.`\u2192 B) \u00d7 (_ T.`\u2192 _))\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7-\u266dOps : \u2200 {s t} {S : Set s} {T : Set t} {funs-S : FlatFuns S} {funs-T : FlatFuns T}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-T\n \u2192 FlatFunsOps (\u00d7-\u266dFuns funs-S funs-T)\n\u00d7-\u266dOps ops-S ops-T\n = mk (S.id , T.id) (\u00d7.zip S._>>>_ T._>>>_) < S.const \u00d7 T.const >\n (\u00d7.zip S.<_,_> T.<_,_>) (S.fst , T.fst) (S.snd , T.snd)\n (S.tt , T.tt) (S.nil , T.nil) (S.cons , T.cons) (S.uncons , T.uncons)\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-T\n open FlatFunsOps fun\u266dOps\n\n\u00d7\u22a4-\u266dOps : \u2200 {s} {S : Set s} {funs-S : FlatFuns S} {funs-\u22a4 : FlatFuns \u22a4}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-\u22a4\n \u2192 FlatFunsOps (\u00d7\u22a4-\u266dFuns funs-S funs-\u22a4)\n\u00d7\u22a4-\u266dOps ops-S ops-\u22a4\n = mk (S.id , T.id) (\u00d7.zip S._>>>_ T._>>>_) < S.const \u00d7 T.const >\n (\u00d7.zip S.<_,_> T.<_,_>) (S.fst , T.fst) (S.snd , T.snd)\n (S.tt , T.tt) (S.nil , T.nil) (S.cons , T.id) (S.uncons , T.id)\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-\u22a4\n open FlatFunsOps fun\u266dOps\n\nconstFuns : Set \u2192 FlatFuns \u22a4\nconstFuns A = mk \u22a4-U (\u03bb _ \u2192 A) (\u03bb _ _ \u2192 A)\n\nmodule ConstFunTypes A = FlatFuns (constFuns A)\n\ntimeOps : FlatFunsOps (constFuns \u2115)\ntimeOps = mk 0 _+_ (F.const 0) _\u2294_ 0 0 0 0 0 0\n\nmodule TimeOps = FlatFunsOps timeOps\n\nspaceOps : FlatFunsOps (constFuns \u2115)\nspaceOps = mk 0 _+_ const _+_ 0 0 0 0 0 0\n where open FlatFuns (constFuns \u2115)\n const : \u2200 {_A B} \u2192 El B \u2192 _A `\u2192 B\n const {_} {_} x = x\n\nmodule SpaceOps = FlatFunsOps spaceOps\n\ntime\u00d7spaceOps : FlatFunsOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-\u266dOps timeOps spaceOps\n\nmodule Time\u00d7SpaceOps = FlatFunsOps time\u00d7spaceOps\n","old_contents":"module flat-funs where\n\nopen import Data.Unit using (\u22a4)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2^_; _\u2294_)\nimport Data.Vec as V\nopen V using (Vec; []; _\u2237_; _++_)\nimport Level as L\nimport Function as F\nopen import Data.Fin using (Fin) renaming (_+_ to _+\u1da0_)\nimport Data.Product as \u00d7\nopen \u00d7 using (_\u00d7_; _,_; proj\u2081; proj\u2082; uncurry)\n\nopen import Data.Bits using (Bits; _\u2192\u1d47_)\n\nopen import universe\n\nrecord FlatFuns {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n universe : Universe T\n El : T \u2192 Set\n _`\u2192_ : T \u2192 T \u2192 Set\n infix 0 _`\u2192_\n open Universe universe public\n\nrecord FlatFunsOps {t} {T : Set t} (\u266dFuns : FlatFuns T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n infixr 1 _>>>_\n infixr 3 _***_\n infixr 3 _&&&_\n field\n -- Functions\n id : \u2200 {A} \u2192 A `\u2192 A\n _>>>_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (B `\u2192 C) \u2192 (A `\u2192 C)\n const : \u2200 {_A B} \u2192 El B \u2192 _A `\u2192 B\n\n -- Products\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n\n -- Unit\n tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4\n\n -- Vectors\n nil : \u2200 {_A B} \u2192 _A `\u2192 `Vec B 0\n cons : \u2200 {n A} \u2192 (A `\u00d7 `Vec A n) `\u2192 `Vec A (1 + n)\n uncons : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 (A `\u00d7 `Vec A n)\n\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n f &&& g = < f , g >\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = < fst >>> f , snd >>> g >\n\n _***_ : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n f *** g = < f \u00d7 g >\n\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n swap = < snd , fst >\n\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n first f = f *** id\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = id *** f\n\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc = < fst >>> fst , first snd >\n\n assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n assoc\u2032 = < second fst , snd >>> snd >\n\n `\u00d7-zip : \u2200 {A B C D E F} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 ((D `\u00d7 E) `\u2192 F)\n \u2192 ((A `\u00d7 D) `\u00d7 (B `\u00d7 E)) `\u2192 (C `\u00d7 F)\n `\u00d7-zip f g = < < fst >>> fst , snd >>> fst > >>> f ,\n < fst >>> snd , snd >>> snd > >>> g >\n\n head : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n head = uncons >>> fst\n\n tail : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n tail = uncons >>> snd\n\n <_\u2237_> : \u2200 {m n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A m `\u2192 `Vec B n)\n \u2192 `Vec A (1 + m) `\u2192 `Vec B (1 + n)\n < f \u2237 g > = uncons >>> < f \u00d7 g > >>> cons\n\n <_\u2237\u2032_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n < f \u2237\u2032 g > = < f , g > >>> cons\n\n foldl : \u2200 {n A B} \u2192 (B `\u00d7 A `\u2192 B) \u2192 (B `\u00d7 `Vec A n) `\u2192 B\n foldl {zero} f = fst\n foldl {suc n} f = second uncons\n >>> assoc\u2032\n >>> first f\n >>> foldl f\n\n foldl\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldl\u2081 f = uncons >>> foldl f\n\n foldr : \u2200 {n A B} \u2192 (A `\u00d7 B `\u2192 B) \u2192 (`Vec A n `\u00d7 B) `\u2192 B\n foldr {zero} f = snd\n foldr {suc n} f = first uncons\n >>> assoc\n >>> second (foldr f)\n >>> f\n\n foldr\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldr\u2081 f = uncons >>> swap >>> foldr f\n\n map : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A n `\u2192 `Vec B n)\n map {zero} f = nil\n map {suc n} f = < f \u2237 map f >\n\n zipWith : \u2200 {n A B C} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec C n\n zipWith {zero} f = nil\n zipWith {suc n} f = < uncons \u00d7 uncons >\n >>> `\u00d7-zip f (zipWith f)\n >>> cons\n\n zip : \u2200 {n A B} \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec (A `\u00d7 B) n\n zip = zipWith id\n\n singleton : \u2200 {A} \u2192 A `\u2192 `Vec A 1\n singleton = < id , nil > >>> cons\n\n snoc : \u2200 {n A} \u2192 (`Vec A n `\u00d7 A) `\u2192 `Vec A (1 + n)\n snoc {zero} = snd >>> singleton\n snoc {suc n} = first uncons >>> assoc >>> second snoc >>> cons\n\n reverse : \u2200 {n A} \u2192 `Vec A n `\u2192 `Vec A n\n reverse {zero} = nil\n reverse {suc n} = uncons >>> swap >>> first reverse >>> snoc\n\n append : \u2200 {m n A} \u2192 (`Vec A m `\u00d7 `Vec A n) `\u2192 `Vec A (m + n)\n append {zero} = snd\n append {suc m} = first uncons\n >>> assoc\n >>> second append\n >>> cons\n\n splitAt : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 (`Vec A m `\u00d7 `Vec A n)\n splitAt zero = < nil , id >\n splitAt (suc m) = uncons\n >>> second (splitAt m)\n >>> assoc\u2032\n >>> first cons\n\n take : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A m\n take zero = nil\n take (suc m) = < id \u2237 take m >\n\n drop : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A n\n drop zero = id\n drop (suc m) = tail >>> drop m\n\n folda : \u2200 n {A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (2^ n) `\u2192 A\n folda zero f = head\n folda (suc n) f = splitAt (2^ n) >>> < folda n f \u00d7 folda n f > >>> f\n\n init : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n init {zero} = nil\n init {suc n} = < id \u2237 init >\n\n last : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n last {zero} = head\n last {suc n} = tail >>> last\n\n concat : \u2200 {m n A} \u2192 `Vec (`Vec A m) n `\u2192 `Vec A (n * m)\n concat {n = zero} = nil\n concat {n = suc n} = uncons >>> second concat >>> append\n\n group : \u2200 {A} n k \u2192 `Vec A (n * k) `\u2192 `Vec (`Vec A k) n\n group zero k = nil\n group (suc n) k = splitAt k >>> second (group n k) >>> cons\n\n bind : \u2200 {m n A B} \u2192 (A `\u2192 `Vec B m) \u2192 `Vec A n `\u2192 `Vec B (n * m)\n bind f = map f >>> concat\n\n \u229b : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 `Vec A n `\u2192 `Vec B n\n \u229b [] = nil\n \u229b (f \u2237 fs) = uncons >>> < f \u00d7 \u229b fs > >>> cons\n\n replicate : \u2200 {n A} \u2192 A `\u2192 `Vec A n\n replicate {zero} = nil\n replicate {suc n} = < id , replicate > >>> cons\n\n module WithFin\n (`Fin : \u2115 \u2192 T)\n (fz : \u2200 {n _A} \u2192 _A `\u2192 `Fin (suc n))\n (fs : \u2200 {n} \u2192 `Fin n `\u2192 `Fin (suc n))\n (elim-Fin0 : \u2200 {A} \u2192 `Fin 0 `\u2192 A)\n (elim-Fin1+ : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Fin n `\u00d7 A `\u2192 B) \u2192 `Fin (suc n) `\u00d7 A `\u2192 B) where\n\n tabulate : \u2200 {n A _B} \u2192 (`Fin n `\u2192 A) \u2192 _B `\u2192 `Vec A n\n tabulate {zero} f = nil\n tabulate {suc n} f = < fz >>> f , tabulate (fs >>> f) > >>> cons\n\n lookup : \u2200 {n A} \u2192 `Fin n `\u00d7 `Vec A n `\u2192 A\n lookup {zero} = fst >>> elim-Fin0\n lookup {suc n} = elim-Fin1+ head (second tail >>> lookup)\n\n allFin : \u2200 {n _A} \u2192 _A `\u2192 `Vec (`Fin n) n\n allFin = tabulate id\n\n-\u2192- : Set \u2192 Set \u2192 Set\n-\u2192- A B = A \u2192 B\n\n_\u2192\u1da0_ : \u2115 \u2192 \u2115 \u2192 Set\n_\u2192\u1da0_ i o = Fin i \u2192 Fin o\n\nmapArr : \u2200 {s t} {S : Set s} {T : Set t} (F G : T \u2192 S)\n \u2192 (S \u2192 S \u2192 Set) \u2192 (T \u2192 T \u2192 Set)\nmapArr F G _`\u2192_ A B = F A `\u2192 G B\n\nfun\u266dFuns : FlatFuns Set\nfun\u266dFuns = mk Set-U F.id -\u2192-\n\nbitsFun\u266dFuns : FlatFuns \u2115\nbitsFun\u266dFuns = mk Bits-U Bits _\u2192\u1d47_\n\nfinFun\u266dFuns : FlatFuns \u2115\nfinFun\u266dFuns = mk Fin-U Fin _\u2192\u1da0_\n\nfun\u266dOps : FlatFunsOps fun\u266dFuns\nfun\u266dOps = mk F.id (\u03bb f g x \u2192 g (f x)) F.const \u00d7.<_,_> proj\u2081 proj\u2082\n _ (F.const []) (uncurry _\u2237_) uncons\n where\n uncons : \u2200 {n A} \u2192 Vec A (1 + n) \u2192 (A \u00d7 Vec A n)\n uncons (x \u2237 xs) = x , xs\n\nbitsFun\u266dOps : FlatFunsOps bitsFun\u266dFuns\nbitsFun\u266dOps = mk id _>>>_ const <_,_> (\u03bb {A} \u2192 V.take A)\n (\u03bb {A} \u2192 V.drop A) (const []) (const []) id id\n where\n open FlatFuns bitsFun\u266dFuns\n open FlatFunsOps fun\u266dOps using (id; _>>>_; const)\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f , g > x = f x ++ g x\n\n\u00d7-\u266dFuns : \u2200 {s t} {S : Set s} {T : Set t} \u2192 FlatFuns S \u2192 FlatFuns T \u2192 FlatFuns (S \u00d7 T)\n\u00d7-\u266dFuns funs-S funs-T = mk (\u00d7-U S.universe T.universe)\n (\u03bb { (A\u2080 , A\u2081) \u2192 S.El A\u2080 \u00d7 T.El A\u2081 })\n (\u03bb { (A\u2080 , A\u2081) (B\u2080 , B\u2081) \u2192 (A\u2080 S.`\u2192 B\u2080) \u00d7 (A\u2081 T.`\u2192 B\u2081) })\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7\u22a4-\u266dFuns : \u2200 {s} {S : Set s} \u2192 FlatFuns S \u2192 FlatFuns \u22a4 \u2192 FlatFuns S\n\u00d7\u22a4-\u266dFuns funs-S funs-T = mk S.universe (\u03bb A \u2192 S.El A \u00d7 T.El _)\n (\u03bb A B \u2192 (A S.`\u2192 B) \u00d7 (_ T.`\u2192 _))\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7-\u266dOps : \u2200 {s t} {S : Set s} {T : Set t} {funs-S : FlatFuns S} {funs-T : FlatFuns T}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-T\n \u2192 FlatFunsOps (\u00d7-\u266dFuns funs-S funs-T)\n\u00d7-\u266dOps ops-S ops-T\n = mk (S.id , T.id) (\u00d7.zip S._>>>_ T._>>>_) < S.const \u00d7 T.const >\n (\u00d7.zip S.<_,_> T.<_,_>) (S.fst , T.fst) (S.snd , T.snd)\n (S.tt , T.tt) (S.nil , T.nil) (S.cons , T.cons) (S.uncons , T.uncons)\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-T\n open FlatFunsOps fun\u266dOps\n\n\u00d7\u22a4-\u266dOps : \u2200 {s} {S : Set s} {funs-S : FlatFuns S} {funs-\u22a4 : FlatFuns \u22a4}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-\u22a4\n \u2192 FlatFunsOps (\u00d7\u22a4-\u266dFuns funs-S funs-\u22a4)\n\u00d7\u22a4-\u266dOps ops-S ops-\u22a4\n = mk (S.id , T.id) (\u00d7.zip S._>>>_ T._>>>_) < S.const \u00d7 T.const >\n (\u00d7.zip S.<_,_> T.<_,_>) (S.fst , T.fst) (S.snd , T.snd)\n (S.tt , T.tt) (S.nil , T.nil) (S.cons , T.id) (S.uncons , T.id)\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-\u22a4\n open FlatFunsOps fun\u266dOps\n\nconstFuns : Set \u2192 FlatFuns \u22a4\nconstFuns A = mk \u22a4-U (\u03bb _ \u2192 A) (\u03bb _ _ \u2192 A)\n\ntimeOps : FlatFunsOps (constFuns \u2115)\ntimeOps = mk 0 _+_ (F.const 0) _\u2294_ 0 0 0 0 0 0\n\nspaceOps : FlatFunsOps (constFuns \u2115)\nspaceOps = mk 0 _+_ const _+_ 0 0 0 0 0 0\n where open FlatFuns (constFuns \u2115)\n const : \u2200 {_A B} \u2192 El B \u2192 _A `\u2192 B\n const {_} {_} x = x\n\ntime\u00d7spaceOps : FlatFunsOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-\u266dOps timeOps spaceOps\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"1860f92cd71fc4cd326cb8563fd03149af8bb53c","subject":"Two.Equality: export subst and \u21d2\u2261","message":"Two.Equality: export subst and \u21d2\u2261\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Two\/Equality.agda","new_file":"lib\/Data\/Two\/Equality.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Data.Two hiding (_\u225f_; decSetoid)\nopen import Type\nopen import Relation.Binary.NP\nopen import Relation.Nullary\nopen import Function\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_)\n\nmodule Data.Two.Equality where\n\nmodule \u2713-== where\n\n _\u2248_ : (x y : \ud835\udfda) \u2192 \u2605\u2080\n x \u2248 y = \u2713 (x == y)\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {0\u2082} {0\u2082} _ = id\n subst _ {1\u2082} {1\u2082} _ = id\n subst _ {0\u2082} {1\u2082} ()\n subst _ {1\u2082} {0\u2082} ()\n\n \u21d2\u2261 : _\u2248_ \u21d2 _\u2261_\n \u21d2\u2261 = substitutive\u21d2\u2261 subst\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = \ud835\udfda; _\u2248_ = _\u2248_; isDecEquivalence = isDecEquivalence }\n where\n refl : Reflexive _\u2248_\n refl {0\u2082} = _\n refl {1\u2082} = _\n\n sym : Symmetric _\u2248_\n sym {x} {y} eq = subst (\u03bb y \u2192 y \u2248 x) {x} {y} eq (refl {x})\n\n trans : Transitive _\u2248_\n trans {x} {y} {z} x\u2248y y\u2248z = subst (_\u2248_ x) {y} {z} y\u2248z x\u2248y\n\n _\u225f_ : Decidable _\u2248_\n 0\u2082 \u225f 0\u2082 = yes _\n 1\u2082 \u225f 1\u2082 = yes _\n 0\u2082 \u225f 1\u2082 = no (\u03bb())\n 1\u2082 \u225f 0\u2082 = no (\u03bb())\n\n isEquivalence : IsEquivalence _\u2248_\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n\n isDecEquivalence : IsDecEquivalence _\u2248_\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n open DecSetoid decSetoid public hiding (_\u2248_)\n\nmodule ==-\u22611\u2082 where\n\n _\u2248_ : (x y : \ud835\udfda) \u2192 \u2605\u2080\n x \u2248 y = (x == y) \u2261 1\u2082\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {0\u2082} {0\u2082} _ = id\n subst _ {1\u2082} {1\u2082} _ = id\n subst _ {0\u2082} {1\u2082} ()\n subst _ {1\u2082} {0\u2082} ()\n\n \u21d2\u2261 : _\u2248_ \u21d2 _\u2261_\n \u21d2\u2261 = substitutive\u21d2\u2261 subst\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = \ud835\udfda; _\u2248_ = _\u2248_; isDecEquivalence = isDecEquivalence }\n where\n refl : Reflexive _\u2248_\n refl {0\u2082} = \u2261.refl\n refl {1\u2082} = \u2261.refl\n\n sym : Symmetric _\u2248_\n sym {x} {y} eq = subst (\u03bb y \u2192 y \u2248 x) {x} {y} eq (refl {x})\n\n trans : Transitive _\u2248_\n trans {x} x\u2248y y\u2248z = subst (_\u2248_ x) y\u2248z x\u2248y\n\n _\u225f_ : Decidable _\u2248_\n 0\u2082 \u225f 0\u2082 = yes \u2261.refl\n 1\u2082 \u225f 1\u2082 = yes \u2261.refl\n 0\u2082 \u225f 1\u2082 = no (\u03bb())\n 1\u2082 \u225f 0\u2082 = no (\u03bb())\n\n isEquivalence : IsEquivalence _\u2248_\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n\n isDecEquivalence : IsDecEquivalence _\u2248_\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n open DecSetoid decSetoid public hiding (_\u2248_)\n\nneg-xor : \u2200 b\u2080 b\u2081 \u2192 b\u2080 == b\u2081 \u2261 not (b\u2080 xor b\u2081)\nneg-xor 0\u2082 b = \u2261.refl\nneg-xor 1\u2082 b = \u2261.sym (not-involutive b)\n\ncomm : \u2200 b\u2080 b\u2081 \u2192 b\u2080 == b\u2081 \u2261 b\u2081 == b\u2080\ncomm 0\u2082 0\u2082 = \u2261.refl\ncomm 0\u2082 1\u2082 = \u2261.refl\ncomm 1\u2082 0\u2082 = \u2261.refl\ncomm 1\u2082 1\u2082 = \u2261.refl\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Data.Two hiding (_\u225f_; decSetoid)\nopen import Type\nopen import Relation.Binary\nopen import Relation.Nullary\nopen import Function\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_)\n\nmodule Data.Two.Equality where\n\nmodule \u2713-== where\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = \ud835\udfda; _\u2248_ = _\u2248_; isDecEquivalence = isDecEquivalence }\n where\n _\u2248_ : (x y : \ud835\udfda) \u2192 \u2605\u2080\n x \u2248 y = \u2713 (x == y)\n\n refl : Reflexive _\u2248_\n refl {0\u2082} = _\n refl {1\u2082} = _\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {0\u2082} {0\u2082} _ = id\n subst _ {1\u2082} {1\u2082} _ = id\n subst _ {0\u2082} {1\u2082} ()\n subst _ {1\u2082} {0\u2082} ()\n\n sym : Symmetric _\u2248_\n sym {x} {y} eq = subst (\u03bb y \u2192 y \u2248 x) {x} {y} eq (refl {x})\n\n trans : Transitive _\u2248_\n trans {x} {y} {z} x\u2248y y\u2248z = subst (_\u2248_ x) {y} {z} y\u2248z x\u2248y\n\n _\u225f_ : Decidable _\u2248_\n 0\u2082 \u225f 0\u2082 = yes _\n 1\u2082 \u225f 1\u2082 = yes _\n 0\u2082 \u225f 1\u2082 = no (\u03bb())\n 1\u2082 \u225f 0\u2082 = no (\u03bb())\n\n isEquivalence : IsEquivalence _\u2248_\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n\n isDecEquivalence : IsDecEquivalence _\u2248_\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n open DecSetoid decSetoid public hiding (_\u2248_; _\u225f_)\n\nmodule ==-\u22611\u2082 where\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = \ud835\udfda; _\u2248_ = _\u2248_; isDecEquivalence = isDecEquivalence }\n where\n _\u2248_ : (x y : \ud835\udfda) \u2192 \u2605\u2080\n x \u2248 y = (x == y) \u2261 1\u2082\n\n refl : Reflexive _\u2248_\n refl {0\u2082} = \u2261.refl\n refl {1\u2082} = \u2261.refl\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {0\u2082} {0\u2082} _ = id\n subst _ {1\u2082} {1\u2082} _ = id\n subst _ {0\u2082} {1\u2082} ()\n subst _ {1\u2082} {0\u2082} ()\n\n sym : Symmetric _\u2248_\n sym {x} {y} eq = subst (\u03bb y \u2192 y \u2248 x) {x} {y} eq (refl {x})\n\n trans : Transitive _\u2248_\n trans {x} x\u2248y y\u2248z = subst (_\u2248_ x) y\u2248z x\u2248y\n\n _\u225f_ : Decidable _\u2248_\n 0\u2082 \u225f 0\u2082 = yes \u2261.refl\n 1\u2082 \u225f 1\u2082 = yes \u2261.refl\n 0\u2082 \u225f 1\u2082 = no (\u03bb())\n 1\u2082 \u225f 0\u2082 = no (\u03bb())\n\n isEquivalence : IsEquivalence _\u2248_\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n\n isDecEquivalence : IsDecEquivalence _\u2248_\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n open DecSetoid decSetoid public\n\nneg-xor : \u2200 b\u2080 b\u2081 \u2192 b\u2080 == b\u2081 \u2261 not (b\u2080 xor b\u2081)\nneg-xor 0\u2082 b = \u2261.refl\nneg-xor 1\u2082 b = \u2261.sym (not-involutive b)\n\ncomm : \u2200 b\u2080 b\u2081 \u2192 b\u2080 == b\u2081 \u2261 b\u2081 == b\u2080\ncomm 0\u2082 0\u2082 = \u2261.refl\ncomm 0\u2082 1\u2082 = \u2261.refl\ncomm 1\u2082 0\u2082 = \u2261.refl\ncomm 1\u2082 1\u2082 = \u2261.refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"6811ac8de37eac34033c36c115fc359dfa88edc5","subject":"Turn context validity into a datatype","message":"Turn context validity into a datatype\n\nThis avoids inelegant embedded equality proofs. Also revise a bit the\nclient code.\n","repos":"inc-lc\/ilc-agda","old_file":"New\/NewNew.agda","new_file":"New\/NewNew.agda","new_contents":"module New.NewNew where\n\nopen import New.Changes\nopen import New.LangChanges\nopen import New.Lang\nopen import New.Types\nopen import New.Derive\n\n[_]_from_to_ : \u2200 (\u03c4 : Type) \u2192 (dv : Cht \u03c4) \u2192 (v1 v2 : \u27e6 \u03c4 \u27e7Type) \u2192 Set\n[ \u03c3 \u21d2 \u03c4 ] df from f1 to f2 =\n \u2200 (da : Cht \u03c3) (a1 a2 : \u27e6 \u03c3 \u27e7Type) \u2192\n [ \u03c3 ] da from a1 to a2 \u2192 [ \u03c4 ] df a1 da from f1 a1 to f2 a2\n[ int ] dv from v1 to v2 = v2 \u2261 v1 + dv\n[ pair \u03c3 \u03c4 ] (da , db) from (a1 , b1) to (a2 , b2) = [ \u03c3 ] da from a1 to a2 \u00d7 [ \u03c4 ] db from b1 to b2\n\ndata [_]\u0393_from_to_ : \u2200 \u0393 \u2192 eCh \u0393 \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 Set where\n v\u2205 : [ \u2205 ]\u0393 \u2205 from \u2205 to \u2205\n _v\u2022_ : \u2200 {\u03c4 \u0393 dv v1 v2 d\u03c1 \u03c11 \u03c12} \u2192\n (dvv : [ \u03c4 ] dv from v1 to v2) \u2192\n (d\u03c1\u03c1 : [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12) \u2192\n [ \u03c4 \u2022 \u0393 ]\u0393 (dv \u2022 v1 \u2022 d\u03c1) from (v1 \u2022 \u03c11) to (v2 \u2022 \u03c12)\n\n\u27e6\u0393\u227c\u0394\u0393\u27e7 : \u2200 {\u0393 \u03c11 \u03c12 d\u03c1} \u2192 (d\u03c1\u03c1 : [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12) \u2192\n \u03c11 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7\u227c d\u03c1\n\u27e6\u0393\u227c\u0394\u0393\u27e7 v\u2205 = refl\n\u27e6\u0393\u227c\u0394\u0393\u27e7 (dvv v\u2022 d\u03c1\u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u0393\u227c\u0394\u0393\u27e7 d\u03c1\u03c1)\n\nfit-sound : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n \u2200 {d\u03c1 \u03c11 \u03c12} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n \u27e6 t \u27e7Term \u03c11 \u2261 \u27e6 fit t \u27e7Term d\u03c1\nfit-sound t d\u03c1\u03c1 = trans\n (cong \u27e6 t \u27e7Term (\u27e6\u0393\u227c\u0394\u0393\u27e7 d\u03c1\u03c1))\n (sym (weaken-sound t _))\n\nfromtoDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192\n \u2200 {d\u03c1 \u03c11 \u03c12} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ] (\u27e6 x \u27e7\u0394Var \u03c11 d\u03c1) from (\u27e6 x \u27e7Var \u03c11) to (\u27e6 x \u27e7Var \u03c12)\nfromtoDeriveVar this (dvv v\u2022 d\u03c1\u03c1) = dvv\nfromtoDeriveVar (that x) (dvv v\u2022 d\u03c1\u03c1) = fromtoDeriveVar x d\u03c1\u03c1\n\nfromtoDeriveConst : \u2200 {\u03c4} c \u2192\n [ \u03c4 ] \u27e6 deriveConst c \u27e7Term \u2205 from \u27e6 c \u27e7Const to \u27e6 c \u27e7Const\nfromtoDeriveConst plus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb = mn\u00b7pq=mp\u00b7nq {a1} {da} {b1} {db}\nfromtoDeriveConst minus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb | sym (-m\u00b7-n=-mn {b1} {db}) = mn\u00b7pq=mp\u00b7nq {a1} {da} { - b1} { - db}\nfromtoDeriveConst cons da a1 a2 daa db b1 b2 dbb = daa , dbb\nfromtoDeriveConst fst (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = daa\nfromtoDeriveConst snd (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = dbb\n\nfromtoDerive : \u2200 {\u0393} \u03c4 \u2192 (t : Term \u0393 \u03c4) \u2192\n {d\u03c1 : eCh \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ] (\u27e6 t \u27e7\u0394Term \u03c11 d\u03c1) from (\u27e6 t \u27e7Term \u03c11) to (\u27e6 t \u27e7Term \u03c12)\nfromtoDerive \u03c4 (const c) {d\u03c1} {\u03c11} d\u03c1\u03c1 rewrite \u27e6 c \u27e7\u0394Const-rewrite \u03c11 d\u03c1 = fromtoDeriveConst c\nfromtoDerive \u03c4 (var x) d\u03c1\u03c1 = fromtoDeriveVar x d\u03c1\u03c1\nfromtoDerive \u03c4 (app {\u03c3} s t) d\u03c1\u03c1 rewrite sym (fit-sound t d\u03c1\u03c1) =\n let fromToF = fromtoDerive (\u03c3 \u21d2 \u03c4) s d\u03c1\u03c1\n in let fromToB = fromtoDerive \u03c3 t d\u03c1\u03c1 in fromToF _ _ _ fromToB\nfromtoDerive (\u03c3 \u21d2 \u03c4) (abs t) d\u03c1\u03c1 = \u03bb dv v1 v2 dvv \u2192\n fromtoDerive \u03c4 t (dvv v\u2022 d\u03c1\u03c1)\n\n-- Now relate this validity with \u2295. To know that nil and so on are valid, also\n-- relate it to the other definition.\nopen import Postulate.Extensionality\n\nfromto\u2192\u2295 : \u2200 {\u03c4} dv v1 v2 \u2192\n [ \u03c4 ] dv from v1 to v2 \u2192\n v1 \u2295 dv \u2261 v2\n\n\u229d-fromto : \u2200 {\u03c4} (v1 v2 : \u27e6 \u03c4 \u27e7Type) \u2192 [ \u03c4 ] v2 \u229d v1 from v1 to v2\n\u229d-fromto {\u03c3 \u21d2 \u03c4} f1 f2 da a1 a2 daa rewrite sym (fromto\u2192\u2295 _ _ _ daa) = \u229d-fromto (f1 a1) (f2 (a1 \u2295 da))\n\u229d-fromto {int} v1 v2 = sym (update-diff v2 v1)\n\u229d-fromto {pair \u03c3 \u03c4} (a1 , b1) (a2 , b2) = \u229d-fromto a1 a2 , \u229d-fromto b1 b2\n\nnil-fromto : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7Type) \u2192 [ \u03c4 ] nil v from v to v\nnil-fromto v = \u229d-fromto v v\n\nfromto\u2192\u2295 {\u03c3 \u21d2 \u03c4} df f1 f2 dff =\n ext (\u03bb v \u2192 fromto\u2192\u2295 {\u03c4} (df v (nil v)) (f1 v) (f2 v) (dff (nil v) v v (nil-fromto v)))\nfromto\u2192\u2295 {int} dn n1 n2 refl = refl\nfromto\u2192\u2295 {pair \u03c3 \u03c4} (da , db) (a1 , b1) (a2 , b2) (daa , dbb) =\n cong\u2082 _,_ (fromto\u2192\u2295 _ _ _ daa) (fromto\u2192\u2295 _ _ _ dbb)\n\nopen \u2261-Reasoning\n\n-- If df is valid, prove (f1 \u2295 df) (a \u2295 da) \u2261 f1 a \u2295 df a da.\n\n-- This statement uses a \u2295 da instead of a2, which is not the style of this formalization but fits better with the other one.\n-- Instead, WellDefinedFunChangeFromTo (without prime) fits this formalization.\nWellDefinedFunChangeFromTo\u2032 : \u2200 {\u03c3 \u03c4} (f1 : \u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type) \u2192 (df : Cht (\u03c3 \u21d2 \u03c4)) \u2192 Set\nWellDefinedFunChangeFromTo\u2032 f1 df = \u2200 da a \u2192 [ _ ] da from a to (a \u2295 da) \u2192 WellDefinedFunChangePoint f1 df a da\n\nfromto\u2192WellDefined\u2032 : \u2200 {\u03c3 \u03c4 f1 f2 df} \u2192 [ \u03c3 \u21d2 \u03c4 ] df from f1 to f2 \u2192\n WellDefinedFunChangeFromTo\u2032 f1 df\nfromto\u2192WellDefined\u2032 {f1 = f1} {f2} {df} dff da a daa =\n begin\n (f1 \u2295 df) (a \u2295 da)\n \u2261\u27e8\u27e9\n f1 (a \u2295 da) \u2295 df (a \u2295 da) (nil (a \u2295 da))\n \u2261\u27e8 (fromto\u2192\u2295\n (df (a \u2295 da) (nil (a \u2295 da))) _ _\n (dff (nil (a \u2295 da)) _ _ (nil-fromto (a \u2295 da))))\n \u27e9\n f2 (a \u2295 da)\n \u2261\u27e8 sym (fromto\u2192\u2295 _ _ _ (dff da _ _ daa)) \u27e9\n f1 a \u2295 df a da\n \u220e\n\nWellDefinedFunChangeFromTo : \u2200 {\u03c3 \u03c4} (f1 : \u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type) \u2192 (df : Cht (\u03c3 \u21d2 \u03c4)) \u2192 Set\nWellDefinedFunChangeFromTo f1 df = \u2200 da a1 a2 \u2192 [ _ ] da from a1 to a2 \u2192 WellDefinedFunChangePoint f1 df a1 da\n\nfromto\u2192WellDefined : \u2200 {\u03c3 \u03c4 f1 f2 df} \u2192 [ \u03c3 \u21d2 \u03c4 ] df from f1 to f2 \u2192\n WellDefinedFunChangeFromTo f1 df\nfromto\u2192WellDefined {f1 = f1} {f2} {df} dff da a1 a2 daa =\n fromto\u2192WellDefined\u2032 dff da a1 daa\u2032\n where\n daa\u2032 : [ _ ] da from a1 to (a1 \u2295 da)\n daa\u2032 rewrite fromto\u2192\u2295 da a1 a2 daa = daa\n\n-- Recursive isomorphism between the two validities.\n--\n-- Among other things, valid\u2192fromto proves that a validity-preserving function,\n-- with validity defined via (f1 \u2295 df) (a \u2295 da) \u2261 f1 a \u2295 df a da, is also valid\n-- in the \"fromto\" sense.\n--\n-- We can't hope for a better statement, since we need the equation to be\n-- satisfied also by returned or argument functions.\n\nfromto\u2192valid : \u2200 {\u03c4} \u2192\n \u2200 v1 v2 dv \u2192 [ \u03c4 ] dv from v1 to v2 \u2192\n valid v1 dv\nvalid\u2192fromto : \u2200 {\u03c4} v (dv : Cht \u03c4) \u2192 valid v dv \u2192 [ \u03c4 ] dv from v to (v \u2295 dv)\n\nfromto\u2192valid {int} = \u03bb v1 v2 dv x \u2192 tt\nfromto\u2192valid {pair \u03c3 \u03c4} (a1 , b1) (a2 , b2) (da , db) (daa , dbb) = (fromto\u2192valid _ _ _ daa) , (fromto\u2192valid _ _ _ dbb)\nfromto\u2192valid {\u03c3 \u21d2 \u03c4} f1 f2 df dff = \u03bb a da ada \u2192\n fromto\u2192valid _ _ _ (dff da a (a \u2295 da) (valid\u2192fromto a da ada)) ,\n fromto\u2192WellDefined\u2032 dff da a (valid\u2192fromto _ _ ada)\n\nvalid\u2192fromto {int} v dv tt = refl\nvalid\u2192fromto {pair \u03c3 \u03c4} (a , b) (da , db) (ada , bdb) = valid\u2192fromto a da ada , valid\u2192fromto b db bdb\nvalid\u2192fromto {\u03c3 \u21d2 \u03c4} f df fdf da a1 a2 daa = body\n where\n fa1da-valid :\n valid (f a1) (df a1 da) \u00d7\n WellDefinedFunChangePoint f df a1 da\n fa1da-valid = fdf a1 da (fromto\u2192valid _ _ _ daa)\n body : [ \u03c4 ] df a1 da from f a1 to (f \u2295 df) a2\n body rewrite sym (fromto\u2192\u2295 _ _ _ daa) | proj\u2082 fa1da-valid = valid\u2192fromto (f a1) (df a1 da) (proj\u2081 fa1da-valid)\n","old_contents":"module New.NewNew where\n\nopen import New.Changes\nopen import New.LangChanges\nopen import New.Lang\nopen import New.Types\nopen import New.Derive\n\n[_]_from_to_ : \u2200 (\u03c4 : Type) \u2192 (dv : Cht \u03c4) \u2192 (v1 v2 : \u27e6 \u03c4 \u27e7Type) \u2192 Set\n[ \u03c3 \u21d2 \u03c4 ] df from f1 to f2 =\n \u2200 (da : Cht \u03c3) (a1 a2 : \u27e6 \u03c3 \u27e7Type) \u2192\n [ \u03c3 ] da from a1 to a2 \u2192 [ \u03c4 ] df a1 da from f1 a1 to f2 a2\n[ int ] dv from v1 to v2 = v2 \u2261 v1 + dv\n[ pair \u03c3 \u03c4 ] (da , db) from (a1 , b1) to (a2 , b2) = [ \u03c3 ] da from a1 to a2 \u00d7 [ \u03c4 ] db from b1 to b2\n\n-- XXX This would be more elegant as a datatype \u2014 that would avoid the need for\n-- an equality proof.\n[_]\u0393_from_to_ : \u2200 \u0393 \u2192 eCh \u0393 \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 Set\n[ \u2205 ]\u0393 \u2205 from \u2205 to \u2205 = \u22a4\n[ \u03c4 \u2022 \u0393 ]\u0393 (dv \u2022 v1' \u2022 d\u03c1) from (v1 \u2022 \u03c11) to (v2 \u2022 \u03c12) =\n [ \u03c4 ] dv from v1 to v2 \u00d7 v1 \u2261 v1' \u00d7 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12\n\n\u27e6\u0393\u227c\u0394\u0393\u27e7 : \u2200 {\u0393 \u03c11 \u03c12 d\u03c1} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n \u03c11 \u2261 \u27e6 \u0393\u227c\u0394\u0393 \u27e7\u227c d\u03c1\n\u27e6\u0393\u227c\u0394\u0393\u27e7 {\u2205} {\u2205} {\u2205} {\u2205} tt = refl\n\u27e6\u0393\u227c\u0394\u0393\u27e7 {_ \u2022 _} {v1 \u2022 \u03c11} {v2 \u2022 \u03c12} {dv \u2022 .v1 \u2022 d\u03c1} (dvv , refl , d\u03c1\u03c1) = cong\u2082 _\u2022_ refl (\u27e6\u0393\u227c\u0394\u0393\u27e7 d\u03c1\u03c1)\n\nfit-sound : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192\n \u2200 {\u03c11 \u03c12 d\u03c1} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n \u27e6 t \u27e7Term \u03c11 \u2261 \u27e6 fit t \u27e7Term d\u03c1\nfit-sound t d\u03c1\u03c1 = trans\n (cong \u27e6 t \u27e7Term (\u27e6\u0393\u227c\u0394\u0393\u27e7 d\u03c1\u03c1))\n (sym (weaken-sound t _))\n\nfromtoDeriveVar : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192\n (d\u03c1 : eCh \u0393) (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ] (\u27e6 x \u27e7\u0394Var \u03c11 d\u03c1) from (\u27e6 x \u27e7Var \u03c11) to (\u27e6 x \u27e7Var \u03c12)\nfromtoDeriveVar this (dv \u2022 .v1 \u2022 d\u03c1) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dvv , refl , d\u03c1\u03c1) = dvv\nfromtoDeriveVar (that x) (dv \u2022 .v1 \u2022 d\u03c1) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (dvv , refl , d\u03c1\u03c1) = fromtoDeriveVar x d\u03c1 \u03c11 \u03c12 d\u03c1\u03c1\n\nfromtoDeriveConst : \u2200 {\u03c4} c \u2192\n [ \u03c4 ] \u27e6 deriveConst c \u27e7Term \u2205 from \u27e6 c \u27e7Const to \u27e6 c \u27e7Const\nfromtoDeriveConst plus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb = mn\u00b7pq=mp\u00b7nq {a1} {da} {b1} {db}\nfromtoDeriveConst minus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb | sym (-m\u00b7-n=-mn {b1} {db}) = mn\u00b7pq=mp\u00b7nq {a1} {da} { - b1} { - db}\nfromtoDeriveConst cons da a1 a2 daa db b1 b2 dbb = daa , dbb\nfromtoDeriveConst fst (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = daa\nfromtoDeriveConst snd (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = dbb\n\nfromtoDerive : \u2200 {\u0393} \u03c4 \u2192 (t : Term \u0393 \u03c4) \u2192\n {d\u03c1 : eCh \u0393} {\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context} \u2192 [ \u0393 ]\u0393 d\u03c1 from \u03c11 to \u03c12 \u2192\n [ \u03c4 ] (\u27e6 t \u27e7\u0394Term \u03c11 d\u03c1) from (\u27e6 t \u27e7Term \u03c11) to (\u27e6 t \u27e7Term \u03c12)\nfromtoDerive \u03c4 (const c) {d\u03c1} {\u03c11} d\u03c1\u03c1 rewrite \u27e6 c \u27e7\u0394Const-rewrite \u03c11 d\u03c1 = fromtoDeriveConst c\nfromtoDerive \u03c4 (var x) d\u03c1\u03c1 = fromtoDeriveVar x _ _ _ d\u03c1\u03c1\nfromtoDerive \u03c4 (app {\u03c3} s t) d\u03c1\u03c1 rewrite sym (fit-sound t d\u03c1\u03c1) =\n let fromToF = fromtoDerive (\u03c3 \u21d2 \u03c4) s d\u03c1\u03c1\n in let fromToB = fromtoDerive \u03c3 t d\u03c1\u03c1 in fromToF _ _ _ fromToB\nfromtoDerive (\u03c3 \u21d2 \u03c4) (abs t) d\u03c1\u03c1 = \u03bb da a1 a2 daa \u2192\n fromtoDerive \u03c4 t (daa , refl , d\u03c1\u03c1)\n\n-- Now relate this validity with \u2295. To know that nil and so on are valid, also\n-- relate it to the other definition.\nopen import Postulate.Extensionality\n\nfromto\u2192\u2295 : \u2200 {\u03c4} dv v1 v2 \u2192\n [ \u03c4 ] dv from v1 to v2 \u2192\n v1 \u2295 dv \u2261 v2\n\n\u229d-fromto : \u2200 {\u03c4} (v1 v2 : \u27e6 \u03c4 \u27e7Type) \u2192 [ \u03c4 ] v2 \u229d v1 from v1 to v2\n\u229d-fromto {\u03c3 \u21d2 \u03c4} f1 f2 da a1 a2 daa rewrite sym (fromto\u2192\u2295 _ _ _ daa) = \u229d-fromto (f1 a1) (f2 (a1 \u2295 da))\n\u229d-fromto {int} v1 v2 = sym (update-diff v2 v1)\n\u229d-fromto {pair \u03c3 \u03c4} (a1 , b1) (a2 , b2) = \u229d-fromto a1 a2 , \u229d-fromto b1 b2\n\nnil-fromto : \u2200 {\u03c4} (v : \u27e6 \u03c4 \u27e7Type) \u2192 [ \u03c4 ] nil v from v to v\nnil-fromto v = \u229d-fromto v v\n\nfromto\u2192\u2295 {\u03c3 \u21d2 \u03c4} df f1 f2 dff =\n ext (\u03bb v \u2192 fromto\u2192\u2295 {\u03c4} (df v (nil v)) (f1 v) (f2 v) (dff (nil v) v v (nil-fromto v)))\nfromto\u2192\u2295 {int} dn n1 n2 refl = refl\nfromto\u2192\u2295 {pair \u03c3 \u03c4} (da , db) (a1 , b1) (a2 , b2) (daa , dbb) =\n cong\u2082 _,_ (fromto\u2192\u2295 _ _ _ daa) (fromto\u2192\u2295 _ _ _ dbb)\n\nopen \u2261-Reasoning\n\n-- If df is valid, prove (f1 \u2295 df) (a \u2295 da) \u2261 f1 a \u2295 df a da.\n\n-- This statement uses a \u2295 da instead of a2, which is not the style of this formalization but fits better with the other one.\n-- Instead, WellDefinedFunChangeFromTo (without prime) fits this formalization.\nWellDefinedFunChangeFromTo\u2032 : \u2200 {\u03c3 \u03c4} (f1 : \u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type) \u2192 (df : Cht (\u03c3 \u21d2 \u03c4)) \u2192 Set\nWellDefinedFunChangeFromTo\u2032 f1 df = \u2200 da a \u2192 [ _ ] da from a to (a \u2295 da) \u2192 WellDefinedFunChangePoint f1 df a da\n\nfromto\u2192WellDefined\u2032 : \u2200 {\u03c3 \u03c4 f1 f2 df} \u2192 [ \u03c3 \u21d2 \u03c4 ] df from f1 to f2 \u2192\n WellDefinedFunChangeFromTo\u2032 f1 df\nfromto\u2192WellDefined\u2032 {f1 = f1} {f2} {df} dff da a daa =\n begin\n (f1 \u2295 df) (a \u2295 da)\n \u2261\u27e8\u27e9\n f1 (a \u2295 da) \u2295 df (a \u2295 da) (nil (a \u2295 da))\n \u2261\u27e8 (fromto\u2192\u2295\n (df (a \u2295 da) (nil (a \u2295 da))) _ _\n (dff (nil (a \u2295 da)) _ _ (nil-fromto (a \u2295 da))))\n \u27e9\n f2 (a \u2295 da)\n \u2261\u27e8 sym (fromto\u2192\u2295 _ _ _ (dff da _ _ daa)) \u27e9\n f1 a \u2295 df a da\n \u220e\n\nWellDefinedFunChangeFromTo : \u2200 {\u03c3 \u03c4} (f1 : \u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type) \u2192 (df : Cht (\u03c3 \u21d2 \u03c4)) \u2192 Set\nWellDefinedFunChangeFromTo f1 df = \u2200 da a1 a2 \u2192 [ _ ] da from a1 to a2 \u2192 WellDefinedFunChangePoint f1 df a1 da\n\nfromto\u2192WellDefined : \u2200 {\u03c3 \u03c4 f1 f2 df} \u2192 [ \u03c3 \u21d2 \u03c4 ] df from f1 to f2 \u2192\n WellDefinedFunChangeFromTo f1 df\nfromto\u2192WellDefined {f1 = f1} {f2} {df} dff da a1 a2 daa =\n fromto\u2192WellDefined\u2032 dff da a1 daa\u2032\n where\n daa\u2032 : [ _ ] da from a1 to (a1 \u2295 da)\n daa\u2032 rewrite fromto\u2192\u2295 da a1 a2 daa = daa\n\n-- Recursive isomorphism between the two validities.\n--\n-- Among other things, valid\u2192fromto proves that a validity-preserving function,\n-- with validity defined via (f1 \u2295 df) (a \u2295 da) \u2261 f1 a \u2295 df a da, is also valid\n-- in the \"fromto\" sense.\n--\n-- We can't hope for a better statement, since we need the equation to be\n-- satisfied also by returned or argument functions.\n\nfromto\u2192valid : \u2200 {\u03c4} \u2192\n \u2200 v1 v2 dv \u2192 [ \u03c4 ] dv from v1 to v2 \u2192\n valid v1 dv\nvalid\u2192fromto : \u2200 {\u03c4} v (dv : Cht \u03c4) \u2192 valid v dv \u2192 [ \u03c4 ] dv from v to (v \u2295 dv)\n\nfromto\u2192valid {int} = \u03bb v1 v2 dv x \u2192 tt\nfromto\u2192valid {pair \u03c3 \u03c4} (a1 , b1) (a2 , b2) (da , db) (daa , dbb) = (fromto\u2192valid _ _ _ daa) , (fromto\u2192valid _ _ _ dbb)\nfromto\u2192valid {\u03c3 \u21d2 \u03c4} f1 f2 df dff = \u03bb a da ada \u2192\n fromto\u2192valid _ _ _ (dff da a (a \u2295 da) (valid\u2192fromto a da ada)) ,\n fromto\u2192WellDefined\u2032 dff da a (valid\u2192fromto _ _ ada)\n\nvalid\u2192fromto {int} v dv tt = refl\nvalid\u2192fromto {pair \u03c3 \u03c4} (a , b) (da , db) (ada , bdb) = valid\u2192fromto a da ada , valid\u2192fromto b db bdb\nvalid\u2192fromto {\u03c3 \u21d2 \u03c4} f df fdf da a1 a2 daa = body\n where\n fa1da-valid :\n valid (f a1) (df a1 da) \u00d7\n WellDefinedFunChangePoint f df a1 da\n fa1da-valid = fdf a1 da (fromto\u2192valid _ _ _ daa)\n body : [ \u03c4 ] df a1 da from f a1 to (f \u2295 df) a2\n body rewrite sym (fromto\u2192\u2295 _ _ _ daa) | proj\u2082 fa1da-valid = valid\u2192fromto (f a1) (df a1 da) (proj\u2081 fa1da-valid)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ab15e9ee2e4fab1712f707ef0f885e95995a8e73","subject":"Renamed data constructors for the Even predicate.","message":"Renamed data constructors for the Even predicate.\n\nIgnore-this: 95d8f33e5eedf116169e2910b36df9d7\n\ndarcs-hash:20120308132006-3bd4e-3429ea1e04d20b7aeb35b73858d37ff9eb1f23c8.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/FOTC\/Data\/Nat\/Even.agda","new_file":"Draft\/FOTC\/Data\/Nat\/Even.agda","new_contents":"------------------------------------------------------------------------------\n-- Even predicate\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Tested with FOT on 08 March 2012.\n\nmodule Draft.FOTC.Data.Nat.Even where\n\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n\ndata Even : D \u2192 Set where\n zE : Even zero\n nE : \u2200 {d} \u2192 Even d \u2192 Even (succ\u2081 (succ\u2081 d))\n\nEven-ind : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {d} \u2192 A d \u2192 A (succ\u2081 (succ\u2081 d))) \u2192\n \u2200 {d} \u2192 Even d \u2192 A d\nEven-ind A A0 h zE = A0\nEven-ind A A0 h (nE Ed) = h (Even-ind A A0 h Ed)\n","old_contents":"------------------------------------------------------------------------------\n-- Even predicate\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Tested with FOT on 02 March 2012.\n\nmodule Draft.FOTC.Data.Nat.Even where\n\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n\ndata Even : D \u2192 Set where\n zeroeven : Even zero\n nexteven : \u2200 {d} \u2192 Even d \u2192 Even (succ\u2081 (succ\u2081 d))\n\nEven-ind : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {d} \u2192 A d \u2192 A (succ\u2081 (succ\u2081 d))) \u2192\n \u2200 {d} \u2192 Even d \u2192 A d\nEven-ind A A0 h zeroeven = A0\nEven-ind A A0 h (nexteven Ed) = h (Even-ind A A0 h Ed)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"58d573424f0945e2a1df71e3e22bac9f751fc1e3","subject":"proved that holes-disjoint is symmetric","message":"proved that holes-disjoint is symmetric\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"disjointness.agda","new_file":"disjointness.agda","new_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import lemmas-disjointness\n\nopen import structural\n\nmodule disjointness where\n mutual\n expand-new-disjoint-synth : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4'} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-synth HNConst ESConst = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNAsc hn) (ESAsc x) = expand-new-disjoint-ana hn x\n expand-new-disjoint-synth HNVar (ESVar x\u2081) = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNLam1 hn) ()\n expand-new-disjoint-synth (HNLam2 hn) (ESLam x\u2081 exp) = expand-new-disjoint-synth hn exp\n expand-new-disjoint-synth (HNHole x) ESEHole = disjoint-singles x\n expand-new-disjoint-synth (HNNEHole x hn) (ESNEHole x\u2081 exp) = disjoint-parts (expand-new-disjoint-synth hn exp) (disjoint-singles x)\n expand-new-disjoint-synth (HNAp hn hn\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) =\n disjoint-parts (expand-new-disjoint-ana hn x\u2084)\n (expand-new-disjoint-ana hn\u2081 x\u2085)\n\n expand-new-disjoint-ana : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4' \u03c42} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-ana hn (EASubsume x x\u2081 x\u2082 x\u2083) = expand-new-disjoint-synth hn x\u2082\n expand-new-disjoint-ana (HNLam1 hn) (EALam x\u2081 x\u2082 ex) = expand-new-disjoint-ana hn ex\n expand-new-disjoint-ana (HNHole x) EAEHole = disjoint-singles x\n expand-new-disjoint-ana (HNNEHole x hn) (EANEHole x\u2081 x\u2082) = disjoint-parts (expand-new-disjoint-synth hn x\u2082) (disjoint-singles x)\n\n mutual\n expand-disjoint-new-synth : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c4'} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-synth ESConst disj = HNConst\n expand-disjoint-new-synth (ESVar x\u2081) disj = HNVar\n expand-disjoint-new-synth (ESLam x\u2081 ex) disj = HNLam2 (expand-disjoint-new-synth ex disj)\n expand-disjoint-new-synth (ESAp {\u03941 = \u03941} x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) disj\n with expand-disjoint-new-ana x\u2084 (disjoint-union1 disj) | expand-disjoint-new-ana x\u2085 (disjoint-union2 {\u03931 = \u03941} disj)\n ... | ih1 | ih2 = HNAp ih1 ih2\n expand-disjoint-new-synth {\u0393 = \u0393} ESEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-synth (ESNEHole {\u0394 = \u0394} x ex) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth ex (disjoint-union1 disj))\n expand-disjoint-new-synth (ESAsc x) disj = HNAsc (expand-disjoint-new-ana x disj)\n\n expand-disjoint-new-ana : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c42 \u03c4'} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-ana (EALam x\u2081 x\u2082 ex) disj = HNLam1 (expand-disjoint-new-ana ex disj)\n expand-disjoint-new-ana (EASubsume x x\u2081 x\u2082 x\u2083) disj = expand-disjoint-new-synth x\u2082 disj\n expand-disjoint-new-ana EAEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-ana (EANEHole {\u0394 = \u0394} x x\u2081) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth x\u2081 (disjoint-union1 disj))\n\n\n mutual\n -- this looks good but may not *quite* work because of the\n -- weakening calls in the two lambda cases\n expand-ana-disjoint : \u2200{ e1 e2 \u03c41 \u03c42 e1' e2' \u03c41' \u03c42'\u00a0\u0393 \u03941 \u03942 } \u2192\n holes-disjoint e1 e2 \u2192\n \u0393 \u22a2 e1 \u21d0 \u03c41 ~> e1' :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> e2' :: \u03c42' \u22a3 \u03942 \u2192\n \u03941 ## \u03942\n expand-ana-disjoint hd (EASubsume x x\u2081 x\u2082 x\u2083) E2 = expand-synth-disjoint hd x\u2082 E2\n expand-ana-disjoint (HDLam1 hd) (EALam x\u2081 x\u2082 ex1) E2 = expand-ana-disjoint hd ex1 (weaken-ana-expand x\u2081 E2)\n expand-ana-disjoint (HDHole x) EAEHole E2 = ##-comm (expand-new-disjoint-ana x E2)\n expand-ana-disjoint (HDNEHole x hd) (EANEHole x\u2081 x\u2082) E2 = disjoint-parts (expand-synth-disjoint hd x\u2082 E2) (##-comm (expand-new-disjoint-ana x E2))\n\n expand-synth-disjoint : \u2200{ e1 e2 \u03c41 \u03c42 e1' e2' \u03c42'\u00a0\u0393 \u03941 \u03942 } \u2192\n holes-disjoint e1 e2 \u2192\n \u0393 \u22a2 e1 \u21d2 \u03c41 ~> e1' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> e2' :: \u03c42' \u22a3 \u03942 \u2192\n \u03941 ## \u03942\n expand-synth-disjoint HDConst ESConst ana = empty-disj _\n expand-synth-disjoint (HDAsc hd) (ESAsc x) ana = expand-ana-disjoint hd x ana\n expand-synth-disjoint HDVar (ESVar x\u2081) ana = empty-disj _\n expand-synth-disjoint (HDLam1 hd) () ana\n expand-synth-disjoint (HDLam2 hd) (ESLam x\u2081 synth) ana = expand-synth-disjoint hd synth (weaken-ana-expand x\u2081 ana)\n expand-synth-disjoint (HDHole x) ESEHole ana = ##-comm (expand-new-disjoint-ana x ana)\n expand-synth-disjoint (HDNEHole x hd) (ESNEHole x\u2081 synth) ana = disjoint-parts (expand-synth-disjoint hd synth ana) (##-comm (expand-new-disjoint-ana x ana))\n expand-synth-disjoint (HDAp hd hd\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) ana = disjoint-parts (expand-ana-disjoint hd x\u2084 ana) (expand-ana-disjoint hd\u2081 x\u2085 ana)\n\n\n -- these lemmas are all structurally recursive. morally, they\n -- establish the properties about reduction that would be obvious \/\n -- baked into Agda if holes-disjoint was defined as a function\n -- rather than a judgement (datatype), or if we had defined all the\n -- O(n^2) cases rather than relying on a little indirection to only\n -- have O(n) cases. that work has to go somewhwere, and we prefer\n -- that it goes here.\n ds-lem-asc : \u2200{e1 e2 \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (e1 \u00b7: \u03c4)\n ds-lem-asc HDConst = HDConst\n ds-lem-asc (HDAsc hd) = HDAsc (ds-lem-asc hd)\n ds-lem-asc HDVar = HDVar\n ds-lem-asc (HDLam1 hd) = HDLam1 (ds-lem-asc hd)\n ds-lem-asc (HDLam2 hd) = HDLam2 (ds-lem-asc hd)\n ds-lem-asc (HDHole x) = HDHole (HNAsc x)\n ds-lem-asc (HDNEHole x hd) = HDNEHole (HNAsc x) (ds-lem-asc hd)\n ds-lem-asc (HDAp hd hd\u2081) = HDAp (ds-lem-asc hd) (ds-lem-asc hd\u2081)\n\n ds-lem-lam1 : \u2200{e1 e2 x} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x e1)\n ds-lem-lam1 HDConst = HDConst\n ds-lem-lam1 (HDAsc hd) = HDAsc (ds-lem-lam1 hd)\n ds-lem-lam1 HDVar = HDVar\n ds-lem-lam1 (HDLam1 hd) = HDLam1 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDLam2 hd) = HDLam2 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDHole x\u2081) = HDHole (HNLam1 x\u2081)\n ds-lem-lam1 (HDNEHole x\u2081 hd) = HDNEHole (HNLam1 x\u2081) (ds-lem-lam1 hd)\n ds-lem-lam1 (HDAp hd hd\u2081) = HDAp (ds-lem-lam1 hd) (ds-lem-lam1 hd\u2081)\n\n ds-lem-lam2 : \u2200{e1 e2 x \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x [ \u03c4 ] e1)\n ds-lem-lam2 HDConst = HDConst\n ds-lem-lam2 (HDAsc hd) = HDAsc (ds-lem-lam2 hd)\n ds-lem-lam2 HDVar = HDVar\n ds-lem-lam2 (HDLam1 hd) = HDLam1 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDLam2 hd) = HDLam2 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDHole x\u2081) = HDHole (HNLam2 x\u2081)\n ds-lem-lam2 (HDNEHole x\u2081 hd) = HDNEHole (HNLam2 x\u2081) (ds-lem-lam2 hd)\n ds-lem-lam2 (HDAp hd hd\u2081) = HDAp (ds-lem-lam2 hd) (ds-lem-lam2 hd\u2081)\n\n ds-lem-nehole : \u2200{e e1 u} \u2192 holes-disjoint e e1 \u2192 hole-name-new e u \u2192 holes-disjoint e \u2987 e1 \u2988[ u ]\n ds-lem-nehole HDConst \u03bd = HDConst\n ds-lem-nehole (HDAsc hd) (HNAsc \u03bd) = HDAsc (ds-lem-nehole hd \u03bd)\n ds-lem-nehole HDVar \u03bd = HDVar\n ds-lem-nehole (HDLam1 hd) (HNLam1 \u03bd) = HDLam1 (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDLam2 hd) (HNLam2 \u03bd) = HDLam2 (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDHole x) (HNHole x\u2081) = HDHole (HNNEHole (flip x\u2081) x)\n ds-lem-nehole (HDNEHole x hd) (HNNEHole x\u2081 \u03bd) = HDNEHole (HNNEHole (flip x\u2081) x) (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDAp hd hd\u2081) (HNAp \u03bd \u03bd\u2081) = HDAp (ds-lem-nehole hd \u03bd) (ds-lem-nehole hd\u2081 \u03bd\u2081)\n\n ds-lem-ap : \u2200{e1 e2 e3} \u2192 holes-disjoint e3 e1 \u2192 holes-disjoint e3 e2 \u2192 holes-disjoint e3 (e1 \u2218 e2)\n ds-lem-ap HDConst hd2 = HDConst\n ds-lem-ap (HDAsc hd1) (HDAsc hd2) = HDAsc (ds-lem-ap hd1 hd2)\n ds-lem-ap HDVar hd2 = HDVar\n ds-lem-ap (HDLam1 hd1) (HDLam1 hd2) = HDLam1 (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDLam2 hd1) (HDLam2 hd2) = HDLam2 (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDHole x) (HDHole x\u2081) = HDHole (HNAp x x\u2081)\n ds-lem-ap (HDNEHole x hd1) (HDNEHole x\u2081 hd2) = HDNEHole (HNAp x x\u2081) (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDAp hd1 hd2) (HDAp hd3 hd4) = HDAp (ds-lem-ap hd1 hd3) (ds-lem-ap hd2 hd4)\n\n -- holes-disjoint is symmetric\n disjoint-sym : (e1 e2 : hexp) \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint e2 e1\n disjoint-sym .c c HDConst = HDConst\n disjoint-sym .c (e2 \u00b7: x) HDConst = HDAsc (disjoint-sym _ _ HDConst)\n disjoint-sym .c (X x) HDConst = HDVar\n disjoint-sym .c (\u00b7\u03bb x e2) HDConst = HDLam1 (disjoint-sym c e2 HDConst)\n disjoint-sym .c (\u00b7\u03bb x [ x\u2081 ] e2) HDConst = HDLam2 (disjoint-sym c e2 HDConst)\n disjoint-sym .c \u2987\u2988[ x ] HDConst = HDHole HNConst\n disjoint-sym .c \u2987 e2 \u2988[ x ] HDConst = HDNEHole HNConst (disjoint-sym c e2 HDConst)\n disjoint-sym .c (e2 \u2218 e3) HDConst = HDAp (disjoint-sym c e2 HDConst) (disjoint-sym c e3 HDConst)\n\n disjoint-sym _ c (HDAsc hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) | HDAsc ih = HDAsc (ds-lem-asc ih)\n disjoint-sym _ (X x) (HDAsc hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) | HDLam1 ih = HDLam1 (ds-lem-asc ih)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) | HDLam2 ih = HDLam2 (ds-lem-asc ih)\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) | HDHole x\u2081 = HDHole (HNAsc x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) | HDNEHole x\u2081 ih = HDNEHole (HNAsc x\u2081) (ds-lem-asc ih)\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) | HDAp ih ih\u2081 = HDAp (ds-lem-asc ih) (ds-lem-asc ih\u2081)\n\n disjoint-sym _ c HDVar = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) HDVar = HDAsc (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (X x\u2081) HDVar = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) HDVar = HDLam1 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) HDVar = HDLam2 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] HDVar = HDHole HNVar\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] HDVar = HDNEHole HNVar (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (e2 \u2218 e3) HDVar = HDAp (disjoint-sym _ e2 HDVar) (disjoint-sym _ e3 HDVar)\n\n disjoint-sym _ c (HDLam1 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) | HDAsc ih = HDAsc (ds-lem-lam1 ih)\n disjoint-sym _ (X x\u2081) (HDLam1 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) | HDLam1 ih = HDLam1 (ds-lem-lam1 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) | HDLam2 ih = HDLam2 (ds-lem-lam1 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) | HDHole x = HDHole (HNLam1 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) | HDNEHole x ih = HDNEHole (HNLam1 x) (ds-lem-lam1 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam1 ih) (ds-lem-lam1 ih\u2081)\n\n disjoint-sym _ c (HDLam2 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) | HDAsc ih = HDAsc (ds-lem-lam2 ih)\n disjoint-sym _ (X x\u2081) (HDLam2 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) | HDLam1 ih = HDLam1 (ds-lem-lam2 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) | HDLam2 ih = HDLam2 (ds-lem-lam2 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) | HDHole x = HDHole (HNLam2 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) | HDNEHole x ih = HDNEHole (HNLam2 x) (ds-lem-lam2 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam2 ih) (ds-lem-lam2 ih\u2081)\n\n disjoint-sym _ c (HDHole x) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDHole (HNAsc x\u2081)) = HDAsc (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (X x) (HDHole x\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDHole (HNLam1 x\u2081)) = HDLam1 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDHole (HNLam2 x\u2082)) = HDLam2 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2082))\n disjoint-sym _ \u2987\u2988[ x ] (HDHole (HNHole x\u2081)) = HDHole (HNHole (flip x\u2081))\n disjoint-sym _ \u2987 e2 \u2988[ u' ] (HDHole (HNNEHole x x\u2081)) = HDNEHole (HNHole (flip x)) (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (e2 \u2218 e3) (HDHole (HNAp x x\u2081)) = HDAp (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x))\n (disjoint-sym \u2987\u2988[ _ ] e3 (HDHole x\u2081))\n\n disjoint-sym _ c (HDNEHole x hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e \u00b7: x) (HDNEHole (HNAsc x\u2081) hd) | HDAsc ih = HDAsc (ds-lem-nehole ih x\u2081)\n disjoint-sym _ (X x) (HDNEHole x\u2081 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole (HNLam1 x\u2081) hd) | HDLam1 ih = HDLam1 (ds-lem-nehole ih x\u2081)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole x\u2082 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole (HNLam2 x\u2082) hd) | HDLam2 ih = HDLam2 (ds-lem-nehole ih x\u2082)\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole (HNHole x\u2082) hd) | HDHole x\u2081 = HDHole (HNNEHole (flip x\u2082) x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole (HNNEHole x\u2082 x\u2083) hd) | HDNEHole x\u2081 ih = HDNEHole (HNNEHole (flip x\u2082) x\u2081) (ds-lem-nehole ih x\u2083)\n disjoint-sym _ (e2 \u2218 e3) (HDNEHole x hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e1 \u2218 e3) (HDNEHole (HNAp x x\u2081) hd) | HDAp ih ih\u2081 = HDAp (ds-lem-nehole ih x) (ds-lem-nehole ih\u2081 x\u2081)\n\n disjoint-sym _ c (HDAp hd hd\u2081) = HDConst\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) | HDAsc ih | HDAsc ih1 = HDAsc (ds-lem-ap ih ih1)\n disjoint-sym _ (X x) (HDAp hd hd\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) | HDLam1 ih | HDLam1 ih1 = HDLam1 (ds-lem-ap ih ih1)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) | HDLam2 ih | HDLam2 ih1 = HDLam2 (ds-lem-ap ih ih1)\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) | HDHole x\u2081 | HDHole x\u2082 = HDHole (HNAp x\u2081 x\u2082)\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) | HDNEHole x\u2081 ih | HDNEHole x\u2082 ih1 = HDNEHole (HNAp x\u2081 x\u2082) (ds-lem-ap ih ih1)\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) | HDAp ih ih\u2081 | HDAp ih1 ih2 = HDAp (ds-lem-ap ih ih1) (ds-lem-ap ih\u2081 ih2)\n\n\n\n -- note that this is false, so holes-disjoint isn't transitive\n -- disjoint-new : \u2200{e1 e2 u} \u2192 holes-disjoint e1 e2 \u2192 hole-name-new e1 u \u2192 hole-name-new e2 u\n\n -- it's also not reflexive, because \u2987\u2988[ u ] isn't hole-disjoint with\n -- itself since u == u; it's not anti-reflexive, because the\n -- expression c *is* hole-disjoint with itself (abeit vacuously)\n","old_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import lemmas-disjointness\n\nopen import structural\n\nmodule disjointness where\n mutual\n expand-new-disjoint-synth : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4'} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-synth HNConst ESConst = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNAsc hn) (ESAsc x) = expand-new-disjoint-ana hn x\n expand-new-disjoint-synth HNVar (ESVar x\u2081) = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNLam1 hn) ()\n expand-new-disjoint-synth (HNLam2 hn) (ESLam x\u2081 exp) = expand-new-disjoint-synth hn exp\n expand-new-disjoint-synth (HNHole x) ESEHole = disjoint-singles x\n expand-new-disjoint-synth (HNNEHole x hn) (ESNEHole x\u2081 exp) = disjoint-parts (expand-new-disjoint-synth hn exp) (disjoint-singles x)\n expand-new-disjoint-synth (HNAp hn hn\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) =\n disjoint-parts (expand-new-disjoint-ana hn x\u2084)\n (expand-new-disjoint-ana hn\u2081 x\u2085)\n\n expand-new-disjoint-ana : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4' \u03c42} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-ana hn (EASubsume x x\u2081 x\u2082 x\u2083) = expand-new-disjoint-synth hn x\u2082\n expand-new-disjoint-ana (HNLam1 hn) (EALam x\u2081 x\u2082 ex) = expand-new-disjoint-ana hn ex\n expand-new-disjoint-ana (HNHole x) EAEHole = disjoint-singles x\n expand-new-disjoint-ana (HNNEHole x hn) (EANEHole x\u2081 x\u2082) = disjoint-parts (expand-new-disjoint-synth hn x\u2082) (disjoint-singles x)\n\n mutual\n expand-disjoint-new-synth : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c4'} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-synth ESConst disj = HNConst\n expand-disjoint-new-synth (ESVar x\u2081) disj = HNVar\n expand-disjoint-new-synth (ESLam x\u2081 ex) disj = HNLam2 (expand-disjoint-new-synth ex disj)\n expand-disjoint-new-synth (ESAp {\u03941 = \u03941} x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) disj\n with expand-disjoint-new-ana x\u2084 (disjoint-union1 disj) | expand-disjoint-new-ana x\u2085 (disjoint-union2 {\u03931 = \u03941} disj)\n ... | ih1 | ih2 = HNAp ih1 ih2\n expand-disjoint-new-synth {\u0393 = \u0393} ESEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-synth (ESNEHole {\u0394 = \u0394} x ex) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth ex (disjoint-union1 disj))\n expand-disjoint-new-synth (ESAsc x) disj = HNAsc (expand-disjoint-new-ana x disj)\n\n expand-disjoint-new-ana : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c42 \u03c4'} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-ana (EALam x\u2081 x\u2082 ex) disj = HNLam1 (expand-disjoint-new-ana ex disj)\n expand-disjoint-new-ana (EASubsume x x\u2081 x\u2082 x\u2083) disj = expand-disjoint-new-synth x\u2082 disj\n expand-disjoint-new-ana EAEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-ana (EANEHole {\u0394 = \u0394} x x\u2081) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth x\u2081 (disjoint-union1 disj))\n\n\n mutual\n -- this looks good but may not *quite* work because of the\n -- weakening calls in the two lambda cases\n expand-ana-disjoint : \u2200{ e1 e2 \u03c41 \u03c42 e1' e2' \u03c41' \u03c42'\u00a0\u0393 \u03941 \u03942 } \u2192\n holes-disjoint e1 e2 \u2192\n \u0393 \u22a2 e1 \u21d0 \u03c41 ~> e1' :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> e2' :: \u03c42' \u22a3 \u03942 \u2192\n \u03941 ## \u03942\n expand-ana-disjoint hd (EASubsume x x\u2081 x\u2082 x\u2083) E2 = expand-synth-disjoint hd x\u2082 E2\n expand-ana-disjoint (HDLam1 hd) (EALam x\u2081 x\u2082 ex1) E2 = expand-ana-disjoint hd ex1 (weaken-ana-expand x\u2081 E2)\n expand-ana-disjoint (HDHole x) EAEHole E2 = ##-comm (expand-new-disjoint-ana x E2)\n expand-ana-disjoint (HDNEHole x hd) (EANEHole x\u2081 x\u2082) E2 = disjoint-parts (expand-synth-disjoint hd x\u2082 E2) (##-comm (expand-new-disjoint-ana x E2))\n\n expand-synth-disjoint : \u2200{ e1 e2 \u03c41 \u03c42 e1' e2' \u03c42'\u00a0\u0393 \u03941 \u03942 } \u2192\n holes-disjoint e1 e2 \u2192\n \u0393 \u22a2 e1 \u21d2 \u03c41 ~> e1' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> e2' :: \u03c42' \u22a3 \u03942 \u2192\n \u03941 ## \u03942\n expand-synth-disjoint HDConst ESConst ana = empty-disj _\n expand-synth-disjoint (HDAsc hd) (ESAsc x) ana = expand-ana-disjoint hd x ana\n expand-synth-disjoint HDVar (ESVar x\u2081) ana = empty-disj _\n expand-synth-disjoint (HDLam1 hd) () ana\n expand-synth-disjoint (HDLam2 hd) (ESLam x\u2081 synth) ana = expand-synth-disjoint hd synth (weaken-ana-expand x\u2081 ana)\n expand-synth-disjoint (HDHole x) ESEHole ana = ##-comm (expand-new-disjoint-ana x ana)\n expand-synth-disjoint (HDNEHole x hd) (ESNEHole x\u2081 synth) ana = disjoint-parts (expand-synth-disjoint hd synth ana) (##-comm (expand-new-disjoint-ana x ana))\n expand-synth-disjoint (HDAp hd hd\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) ana = disjoint-parts (expand-ana-disjoint hd x\u2084 ana) (expand-ana-disjoint hd\u2081 x\u2085 ana)\n\n\n\n ds-lem-asc : \u2200{e1 e2 \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (e1 \u00b7: \u03c4)\n ds-lem-asc HDConst = HDConst\n ds-lem-asc (HDAsc hd) = HDAsc (ds-lem-asc hd)\n ds-lem-asc HDVar = HDVar\n ds-lem-asc (HDLam1 hd) = HDLam1 (ds-lem-asc hd)\n ds-lem-asc (HDLam2 hd) = HDLam2 (ds-lem-asc hd)\n ds-lem-asc (HDHole x) = HDHole (HNAsc x)\n ds-lem-asc (HDNEHole x hd) = HDNEHole (HNAsc x) (ds-lem-asc hd)\n ds-lem-asc (HDAp hd hd\u2081) = HDAp (ds-lem-asc hd) (ds-lem-asc hd\u2081)\n\n ds-lem-lam1 : \u2200{e1 e2 x} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x e1)\n ds-lem-lam1 HDConst = HDConst\n ds-lem-lam1 (HDAsc hd) = HDAsc (ds-lem-lam1 hd)\n ds-lem-lam1 HDVar = HDVar\n ds-lem-lam1 (HDLam1 hd) = HDLam1 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDLam2 hd) = HDLam2 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDHole x\u2081) = HDHole (HNLam1 x\u2081)\n ds-lem-lam1 (HDNEHole x\u2081 hd) = HDNEHole (HNLam1 x\u2081) (ds-lem-lam1 hd)\n ds-lem-lam1 (HDAp hd hd\u2081) = HDAp (ds-lem-lam1 hd) (ds-lem-lam1 hd\u2081)\n\n ds-lem-lam2 : \u2200{e1 e2 x \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x [ \u03c4 ] e1)\n ds-lem-lam2 HDConst = HDConst\n ds-lem-lam2 (HDAsc hd) = HDAsc (ds-lem-lam2 hd)\n ds-lem-lam2 HDVar = HDVar\n ds-lem-lam2 (HDLam1 hd) = HDLam1 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDLam2 hd) = HDLam2 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDHole x\u2081) = HDHole (HNLam2 x\u2081)\n ds-lem-lam2 (HDNEHole x\u2081 hd) = HDNEHole (HNLam2 x\u2081) (ds-lem-lam2 hd)\n ds-lem-lam2 (HDAp hd hd\u2081) = HDAp (ds-lem-lam2 hd) (ds-lem-lam2 hd\u2081)\n\n disjoint-sym : (e1 e2 : hexp) \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint e2 e1\n disjoint-sym .c c HDConst = HDConst\n disjoint-sym .c (e2 \u00b7: x) HDConst = HDAsc (disjoint-sym _ _ HDConst)\n disjoint-sym .c (X x) HDConst = HDVar\n disjoint-sym .c (\u00b7\u03bb x e2) HDConst = HDLam1 (disjoint-sym c e2 HDConst)\n disjoint-sym .c (\u00b7\u03bb x [ x\u2081 ] e2) HDConst = HDLam2 (disjoint-sym c e2 HDConst)\n disjoint-sym .c \u2987\u2988[ x ] HDConst = HDHole HNConst\n disjoint-sym .c \u2987 e2 \u2988[ x ] HDConst = HDNEHole HNConst (disjoint-sym c e2 HDConst)\n disjoint-sym .c (e2 \u2218 e3) HDConst = HDAp (disjoint-sym c e2 HDConst) (disjoint-sym c e3 HDConst)\n\n disjoint-sym _ c (HDAsc hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) | HDAsc ih = HDAsc (ds-lem-asc ih)\n disjoint-sym _ (X x) (HDAsc hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) | HDLam1 ih = HDLam1 (ds-lem-asc ih)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) | HDLam2 ih = HDLam2 (ds-lem-asc ih)\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) | HDHole x\u2081 = HDHole (HNAsc x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) | HDNEHole x\u2081 ih = HDNEHole (HNAsc x\u2081) (ds-lem-asc ih)\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) | HDAp ih ih\u2081 = HDAp (ds-lem-asc ih) (ds-lem-asc ih\u2081)\n\n disjoint-sym _ c HDVar = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) HDVar = HDAsc (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (X x\u2081) HDVar = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) HDVar = HDLam1 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) HDVar = HDLam2 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] HDVar = HDHole HNVar\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] HDVar = HDNEHole HNVar (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (e2 \u2218 e3) HDVar = HDAp (disjoint-sym _ e2 HDVar) (disjoint-sym _ e3 HDVar)\n\n disjoint-sym _ c (HDLam1 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) | HDAsc ih = HDAsc (ds-lem-lam1 ih)\n disjoint-sym _ (X x\u2081) (HDLam1 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) | HDLam1 ih = HDLam1 (ds-lem-lam1 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) | HDLam2 ih = HDLam2 (ds-lem-lam1 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) | HDHole x = HDHole (HNLam1 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) | HDNEHole x ih = HDNEHole (HNLam1 x) (ds-lem-lam1 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam1 ih) (ds-lem-lam1 ih\u2081)\n\n disjoint-sym _ c (HDLam2 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) | HDAsc ih = HDAsc (ds-lem-lam2 ih)\n disjoint-sym _ (X x\u2081) (HDLam2 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) | HDLam1 ih = HDLam1 (ds-lem-lam2 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) | HDLam2 ih = HDLam2 (ds-lem-lam2 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) | HDHole x = HDHole (HNLam2 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) | HDNEHole x ih = HDNEHole (HNLam2 x) (ds-lem-lam2 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam2 ih) (ds-lem-lam2 ih\u2081)\n\n disjoint-sym _ c (HDHole x) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDHole (HNAsc x\u2081)) = HDAsc (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (X x) (HDHole x\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDHole (HNLam1 x\u2081)) = HDLam1 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDHole (HNLam2 x\u2082)) = HDLam2 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2082))\n disjoint-sym _ \u2987\u2988[ x ] (HDHole (HNHole x\u2081)) = HDHole (HNHole (flip x\u2081))\n disjoint-sym _ \u2987 e2 \u2988[ u' ] (HDHole (HNNEHole x x\u2081)) = HDNEHole (HNHole (flip x)) (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (e2 \u2218 e3) (HDHole (HNAp x x\u2081)) = HDAp (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x))\n (disjoint-sym \u2987\u2988[ _ ] e3 (HDHole x\u2081))\n\n disjoint-sym _ c (HDNEHole x hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDNEHole x\u2081 hd) = HDAsc {!!}\n disjoint-sym _ (X x) (HDNEHole x\u2081 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole x\u2081 hd) = {!!}\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole x\u2082 hd) = {!!}\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole x\u2081 hd) = {!!}\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole x\u2081 hd) = {!!}\n disjoint-sym _ (e2 \u2218 e3) (HDNEHole x hd) = {!!}\n\n disjoint-sym _ c (HDAp hd hd\u2081) = HDConst\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) = {!!}\n disjoint-sym _ (X x) (HDAp hd hd\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) = {!!}\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) = {!!}\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) = {!!}\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) = {!!}\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) = {!!}\n\n\n\n -- note that this is false, so holes-disjoint isn't transitive\n -- disjoint-new : \u2200{e1 e2 u} \u2192 holes-disjoint e1 e2 \u2192 hole-name-new e1 u \u2192 hole-name-new e2 u\n\n -- it's also not reflexive, because \u2987\u2988[ u ] isn't hole-disjoint with\n -- itself since u == u; it's not anti-reflexive, because the\n -- expression c *is* hole-disjoint with itself (abeit vacuously)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c71d9bf25d860bc65cb2c8a3e4ad0f801328fb6e","subject":"Added missing ATP definitions.","message":"Added missing ATP definitions.\n\nIgnore-this: 3b3c373c2dd18ce05ccc1efd4812a2b4\n\ndarcs-hash:20110205131613-3bd4e-50691c642fd8de075a1c33c365bf096588af93a0.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/McCarthy91\/Numbers.agda","new_file":"Draft\/McCarthy91\/Numbers.agda","new_contents":"------------------------------------------------------------------------------\n-- Some naturales numbers\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.Numbers where\n\nopen import LTC.Base\n\n------------------------------------------------------------------------------\n\none = succ zero\ntwo = succ one\nthree = succ two\nfour = succ three\nfive = succ four\nsix = succ five\nseven = succ six\neight = succ seven\nnine = succ eight\nten = succ nine\n\n{-# ATP definition one #-}\n{-# ATP definition two #-}\n{-# ATP definition three #-}\n{-# ATP definition four #-}\n{-# ATP definition five #-}\n{-# ATP definition six #-}\n{-# ATP definition seven #-}\n{-# ATP definition eight #-}\n{-# ATP definition nine #-}\n{-# ATP definition ten #-}\n\neleven = succ ten\ntwelve = succ eleven\nthirteen = succ twelve\nfourteen = succ thirteen\nfifteen = succ fourteen\nsixteen = succ fifteen\nseventeen = succ sixteen\neighteen = succ seventeen\nnineteen = succ eighteen\ntwenty = succ nineteen\n\n{-# ATP definition eleven #-}\n{-# ATP definition twelve #-}\n{-# ATP definition thirteen #-}\n{-# ATP definition fourteen #-}\n{-# ATP definition fifteen #-}\n{-# ATP definition sixteen #-}\n{-# ATP definition seventeen #-}\n{-# ATP definition eighteen #-}\n{-# ATP definition nineteen #-}\n{-# ATP definition twenty #-}\n\ntwenty-one = succ twenty\ntwenty-two = succ twenty-one\ntwenty-three = succ twenty-two\ntwenty-four = succ twenty-three\ntwenty-five = succ twenty-four\ntwenty-six = succ twenty-five\ntwenty-seven = succ twenty-six\ntwenty-eight = succ twenty-seven\ntwenty-nine = succ twenty-eight\nthirty = succ twenty-nine\n\n{-# ATP definition twenty-one #-}\n{-# ATP definition twenty-two #-}\n{-# ATP definition twenty-three #-}\n{-# ATP definition twenty-four #-}\n{-# ATP definition twenty-five #-}\n{-# ATP definition twenty-six #-}\n{-# ATP definition twenty-seven #-}\n{-# ATP definition twenty-eight #-}\n{-# ATP definition twenty-nine #-}\n{-# ATP definition thirty #-}\n\nthirty-one = succ thirty\nthirty-two = succ thirty-one\nthirty-three = succ thirty-two\nthirty-four = succ thirty-three\nthirty-five = succ thirty-four\nthirty-six = succ thirty-five\nthirty-seven = succ thirty-six\nthirty-eight = succ thirty-seven\nthirty-nine = succ thirty-eight\nforty = succ thirty-nine\n\n{-# ATP definition thirty-one #-}\n{-# ATP definition thirty-two #-}\n{-# ATP definition thirty-three #-}\n{-# ATP definition thirty-four #-}\n{-# ATP definition thirty-five #-}\n{-# ATP definition thirty-six #-}\n{-# ATP definition thirty-seven #-}\n{-# ATP definition thirty-eight #-}\n{-# ATP definition thirty-nine #-}\n{-# ATP definition forty #-}\n\nforty-one = succ forty\nforty-two = succ forty-one\nforty-three = succ forty-two\nforty-four = succ forty-three\nforty-five = succ forty-four\nforty-six = succ forty-five\nforty-seven = succ forty-six\nforty-eight = succ forty-seven\nforty-nine = succ forty-eight\nfifty = succ forty-nine\n\n{-# ATP definition forty-one #-}\n{-# ATP definition forty-two #-}\n{-# ATP definition forty-three #-}\n{-# ATP definition forty-four #-}\n{-# ATP definition forty-five #-}\n{-# ATP definition forty-six #-}\n{-# ATP definition forty-seven #-}\n{-# ATP definition forty-eight #-}\n{-# ATP definition forty-nine #-}\n{-# ATP definition fifty #-}\n\nfifty-one = succ fifty\nfifty-two = succ fifty-one\nfifty-three = succ fifty-two\nfifty-four = succ fifty-three\nfifty-five = succ fifty-four\nfifty-six = succ fifty-five\nfifty-seven = succ fifty-six\nfifty-eight = succ fifty-seven\nfifty-nine = succ fifty-eight\nsixty = succ fifty-nine\n\n{-# ATP definition fifty-one #-}\n{-# ATP definition fifty-two #-}\n{-# ATP definition fifty-three #-}\n{-# ATP definition fifty-four #-}\n{-# ATP definition fifty-five #-}\n{-# ATP definition fifty-six #-}\n{-# ATP definition fifty-seven #-}\n{-# ATP definition fifty-eight #-}\n{-# ATP definition fifty-nine #-}\n{-# ATP definition sixty #-}\n\nsixty-one = succ sixty\nsixty-two = succ sixty-one\nsixty-three = succ sixty-two\nsixty-four = succ sixty-three\nsixty-five = succ sixty-four\nsixty-six = succ sixty-five\nsixty-seven = succ sixty-six\nsixty-eight = succ sixty-seven\nsixty-nine = succ sixty-eight\nseventy = succ sixty-nine\n\n{-# ATP definition sixty-one #-}\n{-# ATP definition sixty-two #-}\n{-# ATP definition sixty-three #-}\n{-# ATP definition sixty-four #-}\n{-# ATP definition sixty-five #-}\n{-# ATP definition sixty-six #-}\n{-# ATP definition sixty-seven #-}\n{-# ATP definition sixty-eight #-}\n{-# ATP definition sixty-nine #-}\n{-# ATP definition seventy #-}\n\nseventy-one = succ seventy\nseventy-two = succ seventy-one\nseventy-three = succ seventy-two\nseventy-four = succ seventy-three\nseventy-five = succ seventy-four\nseventy-six = succ seventy-five\nseventy-seven = succ seventy-six\nseventy-eight = succ seventy-seven\nseventy-nine = succ seventy-eight\neighty = succ seventy-nine\n\n{-# ATP definition seventy-one #-}\n{-# ATP definition seventy-two #-}\n{-# ATP definition seventy-three #-}\n{-# ATP definition seventy-four #-}\n{-# ATP definition seventy-five #-}\n{-# ATP definition seventy-six #-}\n{-# ATP definition seventy-seven #-}\n{-# ATP definition seventy-eight #-}\n{-# ATP definition seventy-nine #-}\n{-# ATP definition eighty #-}\n\neighty-one = succ eighty\neighty-two = succ eighty-one\neighty-three = succ eighty-two\neighty-four = succ eighty-three\neighty-five = succ eighty-four\neighty-six = succ eighty-five\neighty-seven = succ eighty-six\neighty-eight = succ eighty-seven\neighty-nine = succ eighty-eight\nninety = succ eighty-nine\n\n{-# ATP definition eighty-one #-}\n{-# ATP definition eighty-two #-}\n{-# ATP definition eighty-three #-}\n{-# ATP definition eighty-four #-}\n{-# ATP definition eighty-five #-}\n{-# ATP definition eighty-six #-}\n{-# ATP definition eighty-seven #-}\n{-# ATP definition eighty-eight #-}\n{-# ATP definition eighty-nine #-}\n{-# ATP definition ninety #-}\n\nninety-one = succ ninety\nninety-two = succ ninety-one\nninety-three = succ ninety-two\nninety-four = succ ninety-three\nninety-five = succ ninety-four\nninety-six = succ ninety-five\nninety-seven = succ ninety-six\nninety-eight = succ ninety-seven\nninety-nine = succ ninety-eight\none-hundred = succ ninety-nine\n\n{-# ATP definition ninety-one #-}\n{-# ATP definition ninety-two #-}\n{-# ATP definition ninety-three #-}\n{-# ATP definition ninety-four #-}\n{-# ATP definition ninety-five #-}\n{-# ATP definition ninety-six #-}\n{-# ATP definition ninety-seven #-}\n{-# ATP definition ninety-eight #-}\n{-# ATP definition ninety-nine #-}\n{-# ATP definition one-hundred #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Some naturales numbers\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.Numbers where\n\nopen import LTC.Base\n\n------------------------------------------------------------------------------\n\none = succ zero\ntwo = succ one\nthree = succ two\nfour = succ three\nfive = succ four\nsix = succ five\nseven = succ six\neight = succ seven\nnine = succ eight\nten = succ nine\n\neleven = succ ten\ntwelve = succ eleven\nthirteen = succ twelve\nfourteen = succ thirteen\nfifteen = succ fourteen\nsixteen = succ fifteen\nseventeen = succ sixteen\neighteen = succ seventeen\nnineteen = succ eighteen\n\ntwenty = succ nineteen\ntwenty-one = succ twenty\ntwenty-two = succ twenty-one\ntwenty-three = succ twenty-two\ntwenty-four = succ twenty-three\ntwenty-five = succ twenty-four\ntwenty-six = succ twenty-five\ntwenty-seven = succ twenty-six\ntwenty-eight = succ twenty-seven\ntwenty-nine = succ twenty-eight\n\nthirty = succ twenty-nine\nthirty-one = succ thirty\nthirty-two = succ thirty-one\nthirty-three = succ thirty-two\nthirty-four = succ thirty-three\nthirty-five = succ thirty-four\nthirty-six = succ thirty-five\nthirty-seven = succ thirty-six\nthirty-eight = succ thirty-seven\nthirty-nine = succ thirty-eight\n\nforty = succ thirty-nine\nforty-one = succ forty\nforty-two = succ forty-one\nforty-three = succ forty-two\nforty-four = succ forty-three\nforty-five = succ forty-four\nforty-six = succ forty-five\nforty-seven = succ forty-six\nforty-eight = succ forty-seven\nforty-nine = succ forty-eight\n\nfifty = succ forty-nine\nfifty-one = succ fifty\nfifty-two = succ fifty-one\nfifty-three = succ fifty-two\nfifty-four = succ fifty-three\nfifty-five = succ fifty-four\nfifty-six = succ fifty-five\nfifty-seven = succ fifty-six\nfifty-eight = succ fifty-seven\nfifty-nine = succ fifty-eight\n\nsixty = succ fifty-nine\nsixty-one = succ sixty\nsixty-two = succ sixty-one\nsixty-three = succ sixty-two\nsixty-four = succ sixty-three\nsixty-five = succ sixty-four\nsixty-six = succ sixty-five\nsixty-seven = succ sixty-six\nsixty-eight = succ sixty-seven\nsixty-nine = succ sixty-eight\n\nseventy = succ sixty-nine\nseventy-one = succ seventy\nseventy-two = succ seventy-one\nseventy-three = succ seventy-two\nseventy-four = succ seventy-three\nseventy-five = succ seventy-four\nseventy-six = succ seventy-five\nseventy-seven = succ seventy-six\nseventy-eight = succ seventy-seven\nseventy-nine = succ seventy-eight\n\neighty = succ seventy-nine\neighty-one = succ eighty\neighty-two = succ eighty-one\neighty-three = succ eighty-two\neighty-four = succ eighty-three\neighty-five = succ eighty-four\neighty-six = succ eighty-five\neighty-seven = succ eighty-six\neighty-eight = succ eighty-seven\neighty-nine = succ eighty-eight\n\nninety = succ eighty-nine\nninety-one = succ ninety\nninety-two = succ ninety-one\nninety-three = succ ninety-two\nninety-four = succ ninety-three\nninety-five = succ ninety-four\nninety-six = succ ninety-five\nninety-seven = succ ninety-six\nninety-eight = succ ninety-seven\nninety-nine = succ ninety-eight\n\none-hundred = succ ninety-nine\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"5d2800ead3ebd1b56a9d52097aa972bb180a479b","subject":"Switch to Agda's support for well-founded induction","message":"Switch to Agda's support for well-founded induction\n\nUgh, this seems as annoying as what you have in Coq :-(\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/ANormalUntyped.agda","new_file":"Thesis\/ANormalUntyped.agda","new_contents":"module Thesis.ANormalUntyped where\n\nopen import Data.Empty\nopen import Data.Product\nopen import Data.Nat\nimport Data.Integer.Base as I\nopen I using (\u2124)\nopen import Data.Integer.Base using (\u2124)\nopen import Relation.Binary.PropositionalEquality\n\n{- Typed deBruijn indexes for untyped languages. -}\n\n-- Using a record gives an eta rule saying that all types are equal.\nrecord Type : Set where\n constructor Uni\n\nrecord DType : Set where\n constructor DUni\n\nopen import Base.Syntax.Context Type public\nimport Base.Syntax.Context DType as DC\n\ndata Term (\u0393 : Context) : Set where\n var : (x : Var \u0393 Uni) \u2192\n Term \u0393\n lett : (f : Var \u0393 Uni) \u2192 (x : Var \u0393 Uni) \u2192 Term (Uni \u2022 \u0393) \u2192 Term \u0393\n\n\u0394\u0394 : Context \u2192 DC.Context\n\u0394\u0394 \u2205 = \u2205\n\u0394\u0394 (\u03c4 \u2022 \u0393) = DUni \u2022 \u0394\u0394 \u0393\n\nderive-dvar : \u2200 {\u0394} \u2192 (x : Var \u0394 Uni) \u2192 DC.Var (\u0394\u0394 \u0394) DUni\nderive-dvar this = DC.this\nderive-dvar (that x) = DC.that (derive-dvar x)\n\ndata DTerm : (\u0394 : Context) \u2192 Set where\n dvar : \u2200 {\u0394} (x : DC.Var (\u0394\u0394 \u0394) DUni) \u2192\n DTerm \u0394\n dlett : \u2200 {\u0394} \u2192\n (f : Var \u0394 Uni) \u2192\n (x : Var \u0394 Uni) \u2192\n (t : Term (Uni \u2022 \u0394)) \u2192\n (df : DC.Var (\u0394\u0394 \u0394) DUni) \u2192\n (dx : DC.Var (\u0394\u0394 \u0394) DUni) \u2192\n (dt : DTerm (Uni \u2022 \u0394)) \u2192\n DTerm \u0394\n\nderive-dterm : \u2200 {\u0394} \u2192 (t : Term \u0394) \u2192 DTerm \u0394\nderive-dterm (var x) = dvar (derive-dvar x)\nderive-dterm (lett f x t) =\n dlett f x t (derive-dvar f) (derive-dvar x) (derive-dterm t)\n\n{-\nderiveC \u0394 (lett f x t) = dlett df x dx\n-}\n\n-- data \u0394CTerm (\u0393 : Context) (\u03c4 : Type) (\u0394 : Context) : Set where\n-- data \u0394CTerm (\u0393 : Context) (\u03c4 : Type) (\u0394 : Context) : Set where\n-- cvar : (x : Var \u0393 \u03c4) \u0394 \u2192\n-- \u0394CTerm \u0393 \u03c4 \u0394\n-- clett : \u2200 {\u03c3 \u03c4\u2081 \u03ba} \u2192 (f : Var \u0393 (\u03c3 \u21d2 \u03c4\u2081)) \u2192 (x : Var \u0393 \u03c3) \u2192\n-- \u0394CTerm (\u03c4\u2081 \u2022 \u0393) \u03c4 (? \u2022 \u0394) \u2192\n-- \u0394CTerm \u0393 \u03c4 \u0394\n\nweaken-term : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u2192\n Term \u0393\u2082\nweaken-term \u0393\u2081\u227c\u0393\u2082 (var x) = var (weaken-var \u0393\u2081\u227c\u0393\u2082 x)\nweaken-term \u0393\u2081\u227c\u0393\u2082 (lett f x t) = lett (weaken-var \u0393\u2081\u227c\u0393\u2082 f) (weaken-var \u0393\u2081\u227c\u0393\u2082 x) (weaken-term (keep _ \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\n-- I don't necessarily recommend having a separate syntactic category for\n-- functions, but we should prove a fundamental lemma for them too, somehow.\n-- I'll probably end up with some ANF allowing lambdas to do the semantics.\ndata Fun (\u0393 : Context) : Set where\n term : Term \u0393 \u2192 Fun \u0393\n abs : \u2200 {\u03c3} \u2192 Fun (\u03c3 \u2022 \u0393) \u2192 Fun \u0393\n\ndata DFun (\u0394 : Context) : Set where\n dterm : DTerm \u0394 \u2192 DFun \u0394\n dabs : DFun (Uni \u2022 \u0394) \u2192 DFun \u0394\n\nderive-dfun : \u2200 {\u0394} \u2192 (t : Fun \u0394) \u2192 DFun \u0394\nderive-dfun (term t) = dterm (derive-dterm t)\nderive-dfun (abs f) = dabs (derive-dfun f)\n\nweaken-fun : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Fun \u0393\u2081 \u2192\n Fun \u0393\u2082\nweaken-fun \u0393\u2081\u227c\u0393\u2082 (term x) = term (weaken-term \u0393\u2081\u227c\u0393\u2082 x)\nweaken-fun \u0393\u2081\u227c\u0393\u2082 (abs f) = abs (weaken-fun (keep _ \u2022 \u0393\u2081\u227c\u0393\u2082) f)\n\ndata Val : Type \u2192 Set\ndata DVal : DType \u2192 Set\n-- data Val (\u03c4 : Type) : Set\n\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\nimport Base.Denotation.Environment DType DVal as D\n\n-- data Val (\u03c4 : Type) where\ndata Val where\n closure : \u2200 {\u0393} \u2192 (t : Fun (Uni \u2022 \u0393)) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val Uni\n intV : \u2200 (n : \u2115) \u2192 Val Uni\n\ndata DVal where\n dclosure : \u2200 {\u0393} \u2192 (dt : DFun (Uni \u2022 \u0393)) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 (d\u03c1 : D.\u27e6 \u0394\u0394 \u0393 \u27e7Context) \u2192 DVal DUni\n dintV : \u2200 (n : \u2124) \u2192 DVal DUni\n\nCh\u0394 : \u2200 (\u0394 : Context) \u2192 Set\nCh\u0394 \u0394 = D.\u27e6 \u0394\u0394 \u0394 \u27e7Context\n\n-- \u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 \u27e6 \u03c4 \u27e7Type\n-- \u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7Var \u03c1\n-- \u27e6 lett f x t \u27e7Term \u03c1 = \u27e6 t \u27e7Term (\u27e6 f \u27e7Var \u03c1 (\u27e6 x \u27e7Var \u03c1) \u2022 \u03c1)\n\n-- XXX separate syntax is a bit dangerous. Also, do I want to be so accurate relative to the original model?\ndata _F\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) : Fun \u0393 \u2192 \u2115 \u2192 Val Uni \u2192 Set where\n abs : \u2200 {t : Fun (Uni \u2022 \u0393)} \u2192\n \u03c1 F\u22a2 abs t \u2193[ 0 ] closure t \u03c1\n\ndata _\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) : Term \u0393 \u2192 \u2115 \u2192 Val Uni \u2192 Set where\n var : \u2200 (x : Var \u0393 Uni) \u2192\n \u03c1 \u22a2 var x \u2193[ 0 ] (\u27e6 x \u27e7Var \u03c1)\n lett : \u2200 n1 n2 {\u0393' \u03c1\u2032 v1 v2 v3} {f x t t'} \u2192\n \u03c1 \u22a2 var f \u2193[ 0 ] closure {\u0393'} t' \u03c1\u2032 \u2192\n \u03c1 \u22a2 var x \u2193[ 0 ] v1 \u2192\n (v1 \u2022 \u03c1\u2032) F\u22a2 t' \u2193[ n1 ] v2 \u2192\n (v2 \u2022 \u03c1) \u22a2 t \u2193[ n2 ] v3 \u2192\n \u03c1 \u22a2 lett f x t \u2193[ suc (suc (n1 + n2)) ] v3\n -- lit : \u2200 n \u2192\n -- \u03c1 \u22a2 const (lit n) \u2193[ 0 ] intV n\n\n-- data _D_\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : D.\u27e6 \u0394\u0394 \u0393 \u27e7Context) : DTerm \u0393 \u2192 \u2115 \u2192 DVal DUni \u2192 Set where\n-- dvar : \u2200 (x : DC.Var (\u0394\u0394 \u0393) DUni) \u2192\n-- \u03c1 D d\u03c1 \u22a2 dvar x \u2193[ 0 ] (D.\u27e6 x \u27e7Var d\u03c1)\n-- dlett : \u2200 n1 n2 n3 n4 {\u0393' \u03c1\u2032 \u03c1'' d\u03c1' v1 v2 v3 dv1 dv2 dv3} {f x t df dx dt t' dt'} \u2192\n-- \u03c1 \u22a2 var f \u2193[ 0 ] closure {\u0393'} t' \u03c1\u2032 \u2192\n-- \u03c1 \u22a2 var x \u2193[ 0 ] v1 \u2192\n-- (v1 \u2022 \u03c1\u2032) F\u22a2 t' \u2193[ n1 ] v2 \u2192\n-- (v2 \u2022 \u03c1) \u22a2 t \u2193[ n2 ] v3 \u2192\n-- -- With a valid input \u03c1' and \u03c1'' coincide? Varies among plausible validity\n-- -- definitions.\n-- \u03c1 D d\u03c1 \u22a2 dvar df \u2193[ 0 ] dclosure {\u0393'} dt' \u03c1'' d\u03c1' \u2192\n-- \u03c1 D d\u03c1 \u22a2 dvar dx \u2193[ 0 ] dv1 \u2192\n-- (v1 \u2022 \u03c1'') D (dv1 \u2022 d\u03c1') F\u22a2 dt' \u2193[ n3 ] dv2 \u2192\n-- (v2 \u2022 \u03c1) D (dv2 \u2022 d\u03c1) \u22a2 dt \u2193[ n4 ] dv3 \u2192\n-- \u03c1 D d\u03c1 \u22a2 dlett f x t df dx dt \u2193[ suc (suc (n1 + n2)) ] dv3\n\n\n-- Do I need to damn count steps here? No.\n\ndata _D_F\u22a2_\u2193_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) : DFun \u0393 \u2192 DVal DUni \u2192 Set where\n dabs : \u2200 {t : DFun (Uni \u2022 \u0393)} \u2192\n \u03c1 D d\u03c1 F\u22a2 dabs t \u2193 dclosure t \u03c1 d\u03c1\n\ndata _D_\u22a2_\u2193_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) : DTerm \u0393 \u2192 DVal DUni \u2192 Set where\n dvar : \u2200 (x : DC.Var (\u0394\u0394 \u0393) DUni) \u2192\n \u03c1 D d\u03c1 \u22a2 dvar x \u2193 (D.\u27e6 x \u27e7Var d\u03c1)\n dlett : \u2200 n1 n2 {\u0393' \u03c1\u2032 \u03c1'' d\u03c1' v1 v2 v3 dv1 dv2 dv3} {f x t df dx dt t' dt'} \u2192\n \u03c1 \u22a2 var f \u2193[ 0 ] closure {\u0393'} t' \u03c1\u2032 \u2192\n \u03c1 \u22a2 var x \u2193[ 0 ] v1 \u2192\n (v1 \u2022 \u03c1\u2032) F\u22a2 t' \u2193[ n1 ] v2 \u2192\n (v2 \u2022 \u03c1) \u22a2 t \u2193[ n2 ] v3 \u2192\n -- With a valid input \u03c1' and \u03c1'' coincide? Varies among plausible validity\n -- definitions.\n \u03c1 D d\u03c1 \u22a2 dvar df \u2193 dclosure {\u0393'} dt' \u03c1'' d\u03c1' \u2192\n \u03c1 D d\u03c1 \u22a2 dvar dx \u2193 dv1 \u2192\n (v1 \u2022 \u03c1'') D (dv1 \u2022 d\u03c1') F\u22a2 dt' \u2193 dv2 \u2192\n (v2 \u2022 \u03c1) D (dv2 \u2022 d\u03c1) \u22a2 dt \u2193 dv3 \u2192\n \u03c1 D d\u03c1 \u22a2 dlett f x t df dx dt \u2193 dv3\n\nopen import Data.Nat.Properties\nopen import Data.Nat.Properties.Simple\nopen import Relation.Binary hiding (_\u21d2_)\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\nsuc\u2238 : \u2200 m n \u2192 n \u2264 m \u2192 suc (m \u2238 n) \u2261 suc m \u2238 n\nsuc\u2238 m zero z\u2264n = refl\nsuc\u2238 (suc m) (suc n) (s\u2264s n\u2264m) = suc\u2238 m n n\u2264m\n\nsuc\u2238suc : \u2200 m n \u2192 n < m \u2192 suc (m \u2238 suc n) \u2261 m \u2238 n\nsuc\u2238suc (suc m) zero (s\u2264s n>=_ : ErrVal \u2192 (Val Uni \u2192 ErrVal) \u2192 ErrVal\n-- Done v >>= f = f v\n-- Error >>= f = Error\n-- TimeOut >>= f = TimeOut\n\n-- Res : Set\n-- Res = \u2115 \u2192 ErrVal\n\n-- -- eval-fun : \u2200 {\u0393} \u2192 Fun \u0393 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res\n-- -- eval-term : \u2200 {\u0393} \u2192 Term \u0393 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res\n\n-- -- apply : Val Uni \u2192 Val Uni \u2192 Res\n-- -- apply (closure f \u03c1) a n = eval-fun f (a \u2022 \u03c1) n\n-- -- apply (intV _) a n = Error\n\n-- -- eval-term t \u03c1 zero = TimeOut\n-- -- eval-term (var x) \u03c1 (suc n) = Done (\u27e6 x \u27e7Var \u03c1)\n-- -- eval-term (lett f x t) \u03c1 (suc n) = apply (\u27e6 f \u27e7Var \u03c1) (\u27e6 x \u27e7Var \u03c1) n >>= (\u03bb v \u2192 eval-term t (v \u2022 \u03c1) n)\n\n-- -- eval-fun (term t) \u03c1 n = eval-term t \u03c1 n\n-- -- eval-fun (abs f) \u03c1 n = Done (closure f \u03c1)\n\n-- -- -- Erasure from typed to untyped values.\n-- -- import Thesis.ANormalBigStep as T\n\n-- -- erase-type : T.Type \u2192 Type\n-- -- erase-type _ = Uni\n\n-- -- erase-val : \u2200 {\u03c4} \u2192 T.Val \u03c4 \u2192 Val (erase-type \u03c4)\n\n-- -- erase-errVal : \u2200 {\u03c4} \u2192 T.ErrVal \u03c4 \u2192 ErrVal\n-- -- erase-errVal (T.Done v) = Done (erase-val v)\n-- -- erase-errVal T.Error = Error\n-- -- erase-errVal T.TimeOut = TimeOut\n\n-- -- erase-res : \u2200 {\u03c4} \u2192 T.Res \u03c4 \u2192 Res\n-- -- erase-res r n = erase-errVal (r n)\n\n-- -- erase-ctx : T.Context \u2192 Context\n-- -- erase-ctx \u2205 = \u2205\n-- -- erase-ctx (\u03c4 \u2022 \u0393) = erase-type \u03c4 \u2022 (erase-ctx \u0393)\n\n-- -- erase-env : \u2200 {\u0393} \u2192 T.Op.\u27e6 \u0393 \u27e7Context \u2192 \u27e6 erase-ctx \u0393 \u27e7Context\n-- -- erase-env \u2205 = \u2205\n-- -- erase-env (v \u2022 \u03c1) = erase-val v \u2022 erase-env \u03c1\n\n-- -- erase-var : \u2200 {\u0393 \u03c4} \u2192 T.Var \u0393 \u03c4 \u2192 Var (erase-ctx \u0393) (erase-type \u03c4)\n-- -- erase-var T.this = this\n-- -- erase-var (T.that x) = that (erase-var x)\n\n-- -- erase-term : \u2200 {\u0393 \u03c4} \u2192 T.Term \u0393 \u03c4 \u2192 Term (erase-ctx \u0393)\n-- -- erase-term (T.var x) = var (erase-var x)\n-- -- erase-term (T.lett f x t) = lett (erase-var f) (erase-var x) (erase-term t)\n\n-- -- erase-fun : \u2200 {\u0393 \u03c4} \u2192 T.Fun \u0393 \u03c4 \u2192 Fun (erase-ctx \u0393)\n-- -- erase-fun (T.term x) = term (erase-term x)\n-- -- erase-fun (T.abs f) = abs (erase-fun f)\n\n-- -- erase-val (T.closure t \u03c1) = closure (erase-fun t) (erase-env \u03c1)\n-- -- erase-val (T.intV n) = intV n\n\n-- -- -- Different erasures commute.\n-- -- erasure-commute-var : \u2200 {\u0393 \u03c4} (x : T.Var \u0393 \u03c4) \u03c1 \u2192\n-- -- erase-val (T.Op.\u27e6 x \u27e7Var \u03c1) \u2261 \u27e6 erase-var x \u27e7Var (erase-env \u03c1)\n-- -- erasure-commute-var T.this (v \u2022 \u03c1) = refl\n-- -- erasure-commute-var (T.that x) (v \u2022 \u03c1) = erasure-commute-var x \u03c1\n\n-- -- erase-bind : \u2200 {\u03c3 \u03c4 \u0393} a (t : T.Term (\u03c3 \u2022 \u0393) \u03c4) \u03c1 n \u2192 erase-errVal (a T.>>= (\u03bb v \u2192 T.eval-term t (v \u2022 \u03c1) n)) \u2261 erase-errVal a >>= (\u03bb v \u2192 eval-term (erase-term t) (v \u2022 erase-env \u03c1) n)\n\n-- -- erasure-commute-fun : \u2200 {\u0393 \u03c4} (t : T.Fun \u0393 \u03c4) \u03c1 n \u2192\n-- -- erase-errVal (T.eval-fun t \u03c1 n) \u2261 eval-fun (erase-fun t) (erase-env \u03c1) n\n\n-- -- erasure-commute-apply : \u2200 {\u03c3 \u03c4} (f : T.Val (\u03c3 T.\u21d2 \u03c4)) a n \u2192 erase-errVal (T.apply f a n) \u2261 apply (erase-val f) (erase-val a) n\n-- -- erasure-commute-apply {\u03c3} (T.closure t \u03c1) a n = erasure-commute-fun t (a \u2022 \u03c1) n\n\n-- -- erasure-commute-term : \u2200 {\u0393 \u03c4} (t : T.Term \u0393 \u03c4) \u03c1 n \u2192\n-- -- erase-errVal (T.eval-term t \u03c1 n) \u2261 eval-term (erase-term t) (erase-env \u03c1) n\n\n-- -- erasure-commute-fun (T.term t) \u03c1 n = erasure-commute-term t \u03c1 n\n-- -- erasure-commute-fun (T.abs t) \u03c1 n = refl\n\n-- -- erasure-commute-term t \u03c1 zero = refl\n-- -- erasure-commute-term (T.var x) \u03c1 (\u2115.suc n) = cong Done (erasure-commute-var x \u03c1)\n-- -- erasure-commute-term (T.lett f x t) \u03c1 (\u2115.suc n) rewrite erase-bind (T.apply (T.Op.\u27e6 f \u27e7Var \u03c1) (T.Op.\u27e6 x \u27e7Var \u03c1) n) t \u03c1 n | erasure-commute-apply (T.Op.\u27e6 f \u27e7Var \u03c1) (T.Op.\u27e6 x \u27e7Var \u03c1) n | erasure-commute-var f \u03c1 | erasure-commute-var x \u03c1 = refl\n\n-- -- erase-bind (T.Done v) t \u03c1 n = erasure-commute-term t (v \u2022 \u03c1) n\n-- -- erase-bind T.Error t \u03c1 n = refl\n-- -- erase-bind T.TimeOut t \u03c1 n = refl\n","old_contents":"module Thesis.ANormalUntyped where\n\nopen import Data.Empty\nopen import Data.Product\nopen import Data.Nat.Base\nimport Data.Integer.Base as I\nopen I using (\u2124)\nopen import Data.Integer.Base using (\u2124)\nopen import Relation.Binary.PropositionalEquality\n\n{- Typed deBruijn indexes for untyped languages. -}\n\n-- Using a record gives an eta rule saying that all types are equal.\nrecord Type : Set where\n constructor Uni\n\nrecord DType : Set where\n constructor DUni\n\nopen import Base.Syntax.Context Type public\nimport Base.Syntax.Context DType as DC\n\ndata Term (\u0393 : Context) : Set where\n var : (x : Var \u0393 Uni) \u2192\n Term \u0393\n lett : (f : Var \u0393 Uni) \u2192 (x : Var \u0393 Uni) \u2192 Term (Uni \u2022 \u0393) \u2192 Term \u0393\n\n\u0394\u0394 : Context \u2192 DC.Context\n\u0394\u0394 \u2205 = \u2205\n\u0394\u0394 (\u03c4 \u2022 \u0393) = DUni \u2022 \u0394\u0394 \u0393\n\nderive-dvar : \u2200 {\u0394} \u2192 (x : Var \u0394 Uni) \u2192 DC.Var (\u0394\u0394 \u0394) DUni\nderive-dvar this = DC.this\nderive-dvar (that x) = DC.that (derive-dvar x)\n\ndata DTerm : (\u0394 : Context) \u2192 Set where\n dvar : \u2200 {\u0394} (x : DC.Var (\u0394\u0394 \u0394) DUni) \u2192\n DTerm \u0394\n dlett : \u2200 {\u0394} \u2192\n (f : Var \u0394 Uni) \u2192\n (x : Var \u0394 Uni) \u2192\n (t : Term (Uni \u2022 \u0394)) \u2192\n (df : DC.Var (\u0394\u0394 \u0394) DUni) \u2192\n (dx : DC.Var (\u0394\u0394 \u0394) DUni) \u2192\n (dt : DTerm (Uni \u2022 \u0394)) \u2192\n DTerm \u0394\n\nderive-dterm : \u2200 {\u0394} \u2192 (t : Term \u0394) \u2192 DTerm \u0394\nderive-dterm (var x) = dvar (derive-dvar x)\nderive-dterm (lett f x t) =\n dlett f x t (derive-dvar f) (derive-dvar x) (derive-dterm t)\n\n{-\nderiveC \u0394 (lett f x t) = dlett df x dx\n-}\n\n-- data \u0394CTerm (\u0393 : Context) (\u03c4 : Type) (\u0394 : Context) : Set where\n-- data \u0394CTerm (\u0393 : Context) (\u03c4 : Type) (\u0394 : Context) : Set where\n-- cvar : (x : Var \u0393 \u03c4) \u0394 \u2192\n-- \u0394CTerm \u0393 \u03c4 \u0394\n-- clett : \u2200 {\u03c3 \u03c4\u2081 \u03ba} \u2192 (f : Var \u0393 (\u03c3 \u21d2 \u03c4\u2081)) \u2192 (x : Var \u0393 \u03c3) \u2192\n-- \u0394CTerm (\u03c4\u2081 \u2022 \u0393) \u03c4 (? \u2022 \u0394) \u2192\n-- \u0394CTerm \u0393 \u03c4 \u0394\n\nweaken-term : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u2192\n Term \u0393\u2082\nweaken-term \u0393\u2081\u227c\u0393\u2082 (var x) = var (weaken-var \u0393\u2081\u227c\u0393\u2082 x)\nweaken-term \u0393\u2081\u227c\u0393\u2082 (lett f x t) = lett (weaken-var \u0393\u2081\u227c\u0393\u2082 f) (weaken-var \u0393\u2081\u227c\u0393\u2082 x) (weaken-term (keep _ \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\n-- I don't necessarily recommend having a separate syntactic category for\n-- functions, but we should prove a fundamental lemma for them too, somehow.\n-- I'll probably end up with some ANF allowing lambdas to do the semantics.\ndata Fun (\u0393 : Context) : Set where\n term : Term \u0393 \u2192 Fun \u0393\n abs : \u2200 {\u03c3} \u2192 Fun (\u03c3 \u2022 \u0393) \u2192 Fun \u0393\n\ndata DFun (\u0394 : Context) : Set where\n dterm : DTerm \u0394 \u2192 DFun \u0394\n dabs : DFun (Uni \u2022 \u0394) \u2192 DFun \u0394\n\nderive-dfun : \u2200 {\u0394} \u2192 (t : Fun \u0394) \u2192 DFun \u0394\nderive-dfun (term t) = dterm (derive-dterm t)\nderive-dfun (abs f) = dabs (derive-dfun f)\n\nweaken-fun : \u2200 {\u0393\u2081 \u0393\u2082} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Fun \u0393\u2081 \u2192\n Fun \u0393\u2082\nweaken-fun \u0393\u2081\u227c\u0393\u2082 (term x) = term (weaken-term \u0393\u2081\u227c\u0393\u2082 x)\nweaken-fun \u0393\u2081\u227c\u0393\u2082 (abs f) = abs (weaken-fun (keep _ \u2022 \u0393\u2081\u227c\u0393\u2082) f)\n\ndata Val : Type \u2192 Set\ndata DVal : DType \u2192 Set\n-- data Val (\u03c4 : Type) : Set\n\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\nimport Base.Denotation.Environment DType DVal as D\n\n-- data Val (\u03c4 : Type) where\ndata Val where\n closure : \u2200 {\u0393} \u2192 (t : Fun (Uni \u2022 \u0393)) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val Uni\n intV : \u2200 (n : \u2115) \u2192 Val Uni\n\ndata DVal where\n dclosure : \u2200 {\u0393} \u2192 (dt : DFun (Uni \u2022 \u0393)) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 (d\u03c1 : D.\u27e6 \u0394\u0394 \u0393 \u27e7Context) \u2192 DVal DUni\n dintV : \u2200 (n : \u2124) \u2192 DVal DUni\n\nCh\u0394 : \u2200 (\u0394 : Context) \u2192 Set\nCh\u0394 \u0394 = D.\u27e6 \u0394\u0394 \u0394 \u27e7Context\n\n-- \u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 \u27e6 \u03c4 \u27e7Type\n-- \u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7Var \u03c1\n-- \u27e6 lett f x t \u27e7Term \u03c1 = \u27e6 t \u27e7Term (\u27e6 f \u27e7Var \u03c1 (\u27e6 x \u27e7Var \u03c1) \u2022 \u03c1)\n\n-- XXX separate syntax is a bit dangerous. Also, do I want to be so accurate relative to the original model?\ndata _F\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) : Fun \u0393 \u2192 \u2115 \u2192 Val Uni \u2192 Set where\n abs : \u2200 {t : Fun (Uni \u2022 \u0393)} \u2192\n \u03c1 F\u22a2 abs t \u2193[ 0 ] closure t \u03c1\n\ndata _\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) : Term \u0393 \u2192 \u2115 \u2192 Val Uni \u2192 Set where\n var : \u2200 (x : Var \u0393 Uni) \u2192\n \u03c1 \u22a2 var x \u2193[ 0 ] (\u27e6 x \u27e7Var \u03c1)\n lett : \u2200 n1 n2 {\u0393' \u03c1\u2032 v1 v2 v3} {f x t t'} \u2192\n \u03c1 \u22a2 var f \u2193[ 0 ] closure {\u0393'} t' \u03c1\u2032 \u2192\n \u03c1 \u22a2 var x \u2193[ 0 ] v1 \u2192\n (v1 \u2022 \u03c1\u2032) F\u22a2 t' \u2193[ n1 ] v2 \u2192\n (v2 \u2022 \u03c1) \u22a2 t \u2193[ n2 ] v3 \u2192\n \u03c1 \u22a2 lett f x t \u2193[ suc (suc (n1 + n2)) ] v3\n -- lit : \u2200 n \u2192\n -- \u03c1 \u22a2 const (lit n) \u2193[ 0 ] intV n\n\n-- data _D_\u22a2_\u2193[_]_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : D.\u27e6 \u0394\u0394 \u0393 \u27e7Context) : DTerm \u0393 \u2192 \u2115 \u2192 DVal DUni \u2192 Set where\n-- dvar : \u2200 (x : DC.Var (\u0394\u0394 \u0393) DUni) \u2192\n-- \u03c1 D d\u03c1 \u22a2 dvar x \u2193[ 0 ] (D.\u27e6 x \u27e7Var d\u03c1)\n-- dlett : \u2200 n1 n2 n3 n4 {\u0393' \u03c1\u2032 \u03c1'' d\u03c1' v1 v2 v3 dv1 dv2 dv3} {f x t df dx dt t' dt'} \u2192\n-- \u03c1 \u22a2 var f \u2193[ 0 ] closure {\u0393'} t' \u03c1\u2032 \u2192\n-- \u03c1 \u22a2 var x \u2193[ 0 ] v1 \u2192\n-- (v1 \u2022 \u03c1\u2032) F\u22a2 t' \u2193[ n1 ] v2 \u2192\n-- (v2 \u2022 \u03c1) \u22a2 t \u2193[ n2 ] v3 \u2192\n-- -- With a valid input \u03c1' and \u03c1'' coincide? Varies among plausible validity\n-- -- definitions.\n-- \u03c1 D d\u03c1 \u22a2 dvar df \u2193[ 0 ] dclosure {\u0393'} dt' \u03c1'' d\u03c1' \u2192\n-- \u03c1 D d\u03c1 \u22a2 dvar dx \u2193[ 0 ] dv1 \u2192\n-- (v1 \u2022 \u03c1'') D (dv1 \u2022 d\u03c1') F\u22a2 dt' \u2193[ n3 ] dv2 \u2192\n-- (v2 \u2022 \u03c1) D (dv2 \u2022 d\u03c1) \u22a2 dt \u2193[ n4 ] dv3 \u2192\n-- \u03c1 D d\u03c1 \u22a2 dlett f x t df dx dt \u2193[ suc (suc (n1 + n2)) ] dv3\n\n\n-- Do I need to damn count steps here? No.\n\ndata _D_F\u22a2_\u2193_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) : DFun \u0393 \u2192 DVal DUni \u2192 Set where\n dabs : \u2200 {t : DFun (Uni \u2022 \u0393)} \u2192\n \u03c1 D d\u03c1 F\u22a2 dabs t \u2193 dclosure t \u03c1 d\u03c1\n\ndata _D_\u22a2_\u2193_ {\u0393} (\u03c1 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) : DTerm \u0393 \u2192 DVal DUni \u2192 Set where\n dvar : \u2200 (x : DC.Var (\u0394\u0394 \u0393) DUni) \u2192\n \u03c1 D d\u03c1 \u22a2 dvar x \u2193 (D.\u27e6 x \u27e7Var d\u03c1)\n dlett : \u2200 n1 n2 {\u0393' \u03c1\u2032 \u03c1'' d\u03c1' v1 v2 v3 dv1 dv2 dv3} {f x t df dx dt t' dt'} \u2192\n \u03c1 \u22a2 var f \u2193[ 0 ] closure {\u0393'} t' \u03c1\u2032 \u2192\n \u03c1 \u22a2 var x \u2193[ 0 ] v1 \u2192\n (v1 \u2022 \u03c1\u2032) F\u22a2 t' \u2193[ n1 ] v2 \u2192\n (v2 \u2022 \u03c1) \u22a2 t \u2193[ n2 ] v3 \u2192\n -- With a valid input \u03c1' and \u03c1'' coincide? Varies among plausible validity\n -- definitions.\n \u03c1 D d\u03c1 \u22a2 dvar df \u2193 dclosure {\u0393'} dt' \u03c1'' d\u03c1' \u2192\n \u03c1 D d\u03c1 \u22a2 dvar dx \u2193 dv1 \u2192\n (v1 \u2022 \u03c1'') D (dv1 \u2022 d\u03c1') F\u22a2 dt' \u2193 dv2 \u2192\n (v2 \u2022 \u03c1) D (dv2 \u2022 d\u03c1) \u22a2 dt \u2193 dv3 \u2192\n \u03c1 D d\u03c1 \u22a2 dlett f x t df dx dt \u2193 dv3\n\n{-# TERMINATING #-} -- Dear lord. Why on Earth.\nmutual\n -- Single context? Yeah, good, that's what we want in the end...\n -- Though we might want more flexibility till when we have replacement\n -- changes.\n rrelT3 : \u2200 {\u0393} (t1 : Fun \u0393) (dt : DFun \u0393) (t2 : Fun \u0393) (\u03c11 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : Ch\u0394 \u0393) (\u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\n rrelT3 t1 dt t2 \u03c11 d\u03c1 \u03c12 k =\n (v1 v2 : Val Uni) \u2192\n \u2200 j n2 (j>=_ : ErrVal \u2192 (Val Uni \u2192 ErrVal) \u2192 ErrVal\n-- Done v >>= f = f v\n-- Error >>= f = Error\n-- TimeOut >>= f = TimeOut\n\n-- Res : Set\n-- Res = \u2115 \u2192 ErrVal\n\n-- -- eval-fun : \u2200 {\u0393} \u2192 Fun \u0393 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res\n-- -- eval-term : \u2200 {\u0393} \u2192 Term \u0393 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res\n\n-- -- apply : Val Uni \u2192 Val Uni \u2192 Res\n-- -- apply (closure f \u03c1) a n = eval-fun f (a \u2022 \u03c1) n\n-- -- apply (intV _) a n = Error\n\n-- -- eval-term t \u03c1 zero = TimeOut\n-- -- eval-term (var x) \u03c1 (suc n) = Done (\u27e6 x \u27e7Var \u03c1)\n-- -- eval-term (lett f x t) \u03c1 (suc n) = apply (\u27e6 f \u27e7Var \u03c1) (\u27e6 x \u27e7Var \u03c1) n >>= (\u03bb v \u2192 eval-term t (v \u2022 \u03c1) n)\n\n-- -- eval-fun (term t) \u03c1 n = eval-term t \u03c1 n\n-- -- eval-fun (abs f) \u03c1 n = Done (closure f \u03c1)\n\n-- -- -- Erasure from typed to untyped values.\n-- -- import Thesis.ANormalBigStep as T\n\n-- -- erase-type : T.Type \u2192 Type\n-- -- erase-type _ = Uni\n\n-- -- erase-val : \u2200 {\u03c4} \u2192 T.Val \u03c4 \u2192 Val (erase-type \u03c4)\n\n-- -- erase-errVal : \u2200 {\u03c4} \u2192 T.ErrVal \u03c4 \u2192 ErrVal\n-- -- erase-errVal (T.Done v) = Done (erase-val v)\n-- -- erase-errVal T.Error = Error\n-- -- erase-errVal T.TimeOut = TimeOut\n\n-- -- erase-res : \u2200 {\u03c4} \u2192 T.Res \u03c4 \u2192 Res\n-- -- erase-res r n = erase-errVal (r n)\n\n-- -- erase-ctx : T.Context \u2192 Context\n-- -- erase-ctx \u2205 = \u2205\n-- -- erase-ctx (\u03c4 \u2022 \u0393) = erase-type \u03c4 \u2022 (erase-ctx \u0393)\n\n-- -- erase-env : \u2200 {\u0393} \u2192 T.Op.\u27e6 \u0393 \u27e7Context \u2192 \u27e6 erase-ctx \u0393 \u27e7Context\n-- -- erase-env \u2205 = \u2205\n-- -- erase-env (v \u2022 \u03c1) = erase-val v \u2022 erase-env \u03c1\n\n-- -- erase-var : \u2200 {\u0393 \u03c4} \u2192 T.Var \u0393 \u03c4 \u2192 Var (erase-ctx \u0393) (erase-type \u03c4)\n-- -- erase-var T.this = this\n-- -- erase-var (T.that x) = that (erase-var x)\n\n-- -- erase-term : \u2200 {\u0393 \u03c4} \u2192 T.Term \u0393 \u03c4 \u2192 Term (erase-ctx \u0393)\n-- -- erase-term (T.var x) = var (erase-var x)\n-- -- erase-term (T.lett f x t) = lett (erase-var f) (erase-var x) (erase-term t)\n\n-- -- erase-fun : \u2200 {\u0393 \u03c4} \u2192 T.Fun \u0393 \u03c4 \u2192 Fun (erase-ctx \u0393)\n-- -- erase-fun (T.term x) = term (erase-term x)\n-- -- erase-fun (T.abs f) = abs (erase-fun f)\n\n-- -- erase-val (T.closure t \u03c1) = closure (erase-fun t) (erase-env \u03c1)\n-- -- erase-val (T.intV n) = intV n\n\n-- -- -- Different erasures commute.\n-- -- erasure-commute-var : \u2200 {\u0393 \u03c4} (x : T.Var \u0393 \u03c4) \u03c1 \u2192\n-- -- erase-val (T.Op.\u27e6 x \u27e7Var \u03c1) \u2261 \u27e6 erase-var x \u27e7Var (erase-env \u03c1)\n-- -- erasure-commute-var T.this (v \u2022 \u03c1) = refl\n-- -- erasure-commute-var (T.that x) (v \u2022 \u03c1) = erasure-commute-var x \u03c1\n\n-- -- erase-bind : \u2200 {\u03c3 \u03c4 \u0393} a (t : T.Term (\u03c3 \u2022 \u0393) \u03c4) \u03c1 n \u2192 erase-errVal (a T.>>= (\u03bb v \u2192 T.eval-term t (v \u2022 \u03c1) n)) \u2261 erase-errVal a >>= (\u03bb v \u2192 eval-term (erase-term t) (v \u2022 erase-env \u03c1) n)\n\n-- -- erasure-commute-fun : \u2200 {\u0393 \u03c4} (t : T.Fun \u0393 \u03c4) \u03c1 n \u2192\n-- -- erase-errVal (T.eval-fun t \u03c1 n) \u2261 eval-fun (erase-fun t) (erase-env \u03c1) n\n\n-- -- erasure-commute-apply : \u2200 {\u03c3 \u03c4} (f : T.Val (\u03c3 T.\u21d2 \u03c4)) a n \u2192 erase-errVal (T.apply f a n) \u2261 apply (erase-val f) (erase-val a) n\n-- -- erasure-commute-apply {\u03c3} (T.closure t \u03c1) a n = erasure-commute-fun t (a \u2022 \u03c1) n\n\n-- -- erasure-commute-term : \u2200 {\u0393 \u03c4} (t : T.Term \u0393 \u03c4) \u03c1 n \u2192\n-- -- erase-errVal (T.eval-term t \u03c1 n) \u2261 eval-term (erase-term t) (erase-env \u03c1) n\n\n-- -- erasure-commute-fun (T.term t) \u03c1 n = erasure-commute-term t \u03c1 n\n-- -- erasure-commute-fun (T.abs t) \u03c1 n = refl\n\n-- -- erasure-commute-term t \u03c1 zero = refl\n-- -- erasure-commute-term (T.var x) \u03c1 (\u2115.suc n) = cong Done (erasure-commute-var x \u03c1)\n-- -- erasure-commute-term (T.lett f x t) \u03c1 (\u2115.suc n) rewrite erase-bind (T.apply (T.Op.\u27e6 f \u27e7Var \u03c1) (T.Op.\u27e6 x \u27e7Var \u03c1) n) t \u03c1 n | erasure-commute-apply (T.Op.\u27e6 f \u27e7Var \u03c1) (T.Op.\u27e6 x \u27e7Var \u03c1) n | erasure-commute-var f \u03c1 | erasure-commute-var x \u03c1 = refl\n\n-- -- erase-bind (T.Done v) t \u03c1 n = erasure-commute-term t (v \u2022 \u03c1) n\n-- -- erase-bind T.Error t \u03c1 n = refl\n-- -- erase-bind T.TimeOut t \u03c1 n = refl\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c01bc7ef92279fc0a542d633c350a718d1a000bd","subject":"Control\/Protocol\/Choreography.agda","message":"Control\/Protocol\/Choreography.agda\n","repos":"crypto-agda\/crypto-agda","old_file":"Control\/Protocol\/Choreography.agda","new_file":"Control\/Protocol\/Choreography.agda","new_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level\nopen import Data.Product\nopen import Data.One\n\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Control.Protocol.Choreography where\nopen import Control.Strategy renaming (Strategy to Client) public\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\n{-\nrecord \u03a3\u00b7 {a b} (A : Set a) (B : ..(_ : A) \u2192 Set b) : Set (a \u2294 b) where\n constructor _,_\n field\n {-..-}proj\u2081\u00b7 : A\n proj\u2082\u00b7 : B proj\u2081\u00b7\n-}\ndata \u03a3\u00b7 {a b} (A : Set a) (B : ..(_ : A) \u2192 Set b) : Set (a \u2294 b) where\n _,_ : ..(proj\u2081\u00b7 : A) (proj\u2082\u00b7 : B proj\u2081\u00b7) \u2192 \u03a3\u00b7 A B\n\ndata Mod : \u2605 where S D : Mod\n\n\u2192M : \u2200 {a b} \u2192 Mod \u2192 \u2605_ a \u2192 \u2605_ b \u2192 \u2605_ (a \u2294 b)\n\u2192M S A B = ..(_ : A) \u2192 B\n\u2192M D A B = A \u2192 B\n\n\u03a0M : \u2200 {a b}(m : Mod) \u2192 (A : \u2605_ a) \u2192 (B : \u2192M m A (\u2605_ b)) \u2192 \u2605_ (a \u2294 b)\n\u03a0M S A B = \u03a0\u00b7 A B\n\u03a0M D A B = \u03a0 A B\n\nappM' : \u2200 {a b}(m : Mod){A : \u2605_ a}{B : \u2605_ b}(P : \u2192M m A B) \u2192 A \u2192 B\nappM' S P x = P x\nappM' D P x = P x\n\nappM : \u2200 {a b}(m : Mod){A : \u2605_ a}{B : \u2192M m A (\u2605_ b)}(P : \u03a0M m A B)(x : A) \u2192 appM' m B x\nappM S P x = P x\nappM D P x = P x\n\ndata \u03a0\u03a3 : \u2605 where\n \u03a0' \u03a3' : \u03a0\u03a3\n\ndata Proto : \u2605\u2081 where\n end : Proto\n mk : (q : \u03a0\u03a3)(d : Mod)(A : \u2605)(B : \u2192M d A Proto) \u2192 Proto\n\ndata Choreo (I : \u2605) : \u2605\u2081 where\n _-[_]\u2192_\u204f_ : (A : I) (M : \u2605) (B : I) (\u2102 : ..(m : M) \u2192 Choreo I) \u2192 Choreo I\n _-[_]\u2192\u2605\u204f_ : (A : I) (M : \u2605) (\u2102 : (m : M) \u2192 Choreo I) \u2192 Choreo I\n \u00f8 : Choreo I\n\ndata [_\/_\u2261_] {I} : Choreo I \u2192 I \u2192 Proto \u2192 \u2605\u2081 where\n \u03a3D : \u2200 {A B M \u2102 \u2102A} \u2192 (\u2200 m \u2192 [ \u2102 m \/ A \u2261 \u2102A m ]) \u2192 [ (A -[ M ]\u2192 B \u204f \u2102) \/ A \u2261 mk \u03a3' D M \u2102A ]\n \u03a0D : \u2200 {A B M \u2102 \u2102B} \u2192 (\u2200 m \u2192 [ \u2102 m \/ B \u2261 \u2102B m ]) \u2192 [ (A -[ M ]\u2192 B \u204f \u2102) \/ B \u2261 mk \u03a0' D M \u2102B ]\n \u03a0S : \u2200 {A B C M \u2102 \u2102C} \u2192 (\u2200 m \u2192 [ \u2102 m \/ C \u2261 \u2102C m ]) \u2192 [ (A -[ M ]\u2192 B \u204f \u2102) \/ C \u2261 mk \u03a0' S M \u2102C ]\n\nTele : Proto \u2192 \u2605\nTele end = \ud835\udfd9\nTele (mk \u03a0' D A B) = \u03a3 A \u03bb x \u2192 Tele (B x)\nTele (mk \u03a3' D A B) = \u03a3 A \u03bb x \u2192 Tele (B x)\nTele (mk \u03a0' S A B) = \u03a3\u00b7 A \u03bb x \u2192 Tele (B x)\nTele (mk \u03a3' S A B) = \u03a3\u00b7 A \u03bb x \u2192 Tele (B x)\n\n{-\n_>>\u2261_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>\u2261 Q = Q _\n\u03a0' A B >>\u2261 Q = \u03a0' A (\u03bb x \u2192 B x >>\u2261 (\u03bb xs \u2192 Q (x , xs)))\n\u03a3' A B >>\u2261 Q = \u03a3' A (\u03bb x \u2192 B x >>\u2261 (\u03bb xs \u2192 Q (x , xs)))\nlater i P >>\u2261 Q = ?\n\n++Tele : \u2200 (P : Proto)(Q : Tele P \u2192 Proto) \u2192 (x : Tele P) \u2192 Tele (Q x) \u2192 Tele (P >>\u2261 Q)\n++Tele end Q x y = y\n++Tele (\u03a0' M C) Q (m , x) y = m , ++Tele (C m) (\u03bb x\u2081 \u2192 Q (m , x\u2081)) x y\n++Tele (\u03a3' M C) Q (m , x) y = m , ++Tele (C m) _ x y\n++Tele (later i P) Q x y = ?\n\nmodule _ (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : \u2605_ b}{f g : A \u2192 B} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n right-unit : \u2200 (P : Proto) \u2192 (P >>\u2261 \u03bb x \u2192 end) \u2261 P\n right-unit end = refl\n right-unit (\u03a0' M C) = let p = funExt (\u03bb x \u2192 right-unit (C x)) in cong (\u03a0' M) p\n right-unit (\u03a3' M C) = cong (\u03a3' M) (funExt (\u03bb x \u2192 right-unit (C x)))\n right-unit (later i P) = ?\n\n assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>\u2261 Q) \u2192 Proto)\n \u2192 P >>\u2261 (\u03bb x \u2192 Q x >>\u2261 (\u03bb y \u2192 R (++Tele P Q x y))) \u2261 ((P >>\u2261 Q) >>\u2261 R)\n assoc end Q R = refl\n assoc (\u03a0' M C\u2081) Q R = cong (\u03a0' M) (funExt (\u03bb x \u2192 assoc (C\u2081 x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb xs \u2192 R (x , xs))))\n assoc (\u03a3' M C\u2081) Q R = cong (\u03a3' M) (funExt (\u03bb x \u2192 assoc (C\u2081 x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb xs \u2192 R (x , xs))))\n assoc (later i P) Q R = ?\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>\u2261 \u03bb _ \u2192 Q\n-}\n\n_\u00d7'_ : Set \u2192 Proto \u2192 Proto\nA \u00d7' B = mk \u03a3' D A \u03bb _ \u2192 B\n\n_\u2192'_ : Set \u2192 Proto \u2192 Proto\nA \u2192' B = mk \u03a0' D A \u03bb _ \u2192 B\n\ndual\u03a0\u03a3 : \u03a0\u03a3 \u2192 \u03a0\u03a3\ndual\u03a0\u03a3 \u03a0' = \u03a3'\ndual\u03a0\u03a3 \u03a3' = \u03a0'\n\ndual : Proto \u2192 Proto\ndual end = end\ndual (mk q S A B) = mk (dual\u03a0\u03a3 q) S A (\u03bb x \u2192 dual (B x))\ndual (mk q D A B) = mk (dual\u03a0\u03a3 q) D A (\u03bb x \u2192 dual (B x))\n\ndata Dual\u03a0\u03a3 : \u03a0\u03a3 \u2192 \u03a0\u03a3 \u2192 \u2605 where\n D\u03a0\u03a3 : Dual\u03a0\u03a3 \u03a0' \u03a3'\n D\u03a3\u03a0 : Dual\u03a0\u03a3 \u03a3' \u03a0'\n\ndata Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n S : \u2200 {A B B' \u03c0 \u03c3} \u2192 Dual\u03a0\u03a3 \u03c0 \u03c3 \u2192 (\u2200 ..x \u2192 Dual (B x) (B' x)) \u2192 Dual (mk \u03c0 S A B) (mk \u03c3 S A B')\n D : \u2200 {A B B' \u03c0 \u03c3} \u2192 Dual\u03a0\u03a3 \u03c0 \u03c3 \u2192 (\u2200 x \u2192 Dual (B x) (B' x)) \u2192 Dual (mk \u03c0 D A B) (mk \u03c3 D A B')\n\ndata [_&_\u2261_] : Proto \u2192 Proto \u2192 Proto \u2192 \u2605\u2081 where\n -- comm : \u2200 {P Q R} \u2192 [ P & Q \u2261 R ] \u2192 [ Q & P \u2261 R ]\n end& : \u2200 {P} \u2192 [ end & P \u2261 P ]\n \u03a0S-S-S : \u2200 {q M P Q R} \u2192 (\u2200 ..m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ mk \u03a0' S M P & mk q S M Q \u2261 mk q S M R ]\n \u03a0S-D-D : \u2200 {q M P Q R} \u2192 (\u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ mk \u03a0' S M P & mk q D M Q \u2261 mk q D M R ]\n \u03a0D\u03a3D\u03a3S : \u2200 {M P Q R} \u2192 (\u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ mk \u03a0' D M P & mk \u03a3' D M Q \u2261 mk \u03a3' S M R ]\n \u03a0D\u03a0D\u03a0D : \u2200 {M P Q R} \u2192 (\u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ mk \u03a0' D M P & mk \u03a0' D M Q \u2261 mk \u03a0' D M R ]\n\n &end : \u2200 {P} \u2192 [ P & end \u2261 P ]\n-- \u03a0S-S-S : \u2200 {q M P Q R} \u2192 (\u2200 ..m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ mk \u03a0' S M P & mk q S M Q \u2261 mk q S M R ]\n-- \u03a0S-D-D : \u2200 {q M P Q R} \u2192 (\u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ mk \u03a0' S M P & mk q D M Q \u2261 mk q D M R ]\n \u00a0\u03a3D\u03a0D\u03a3S : \u2200 {M P Q R} \u2192 (\u2200 m \u2192 [ P m & Q m \u2261 R m ]) \u2192 [ mk \u03a3' D M P & mk \u03a0' D M Q \u2261 mk \u03a3' S M R ]\n\nDual\u03a0\u03a3-sym : \u2200 {P Q} \u2192 Dual\u03a0\u03a3 P Q \u2192 Dual\u03a0\u03a3 Q P\nDual\u03a0\u03a3-sym D\u03a0\u03a3 = D\u03a3\u03a0\nDual\u03a0\u03a3-sym D\u03a3\u03a0 = D\u03a0\u03a3\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (S d f) = S (Dual\u03a0\u03a3-sym d) (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (D d f) = D (Dual\u03a0\u03a3-sym d) (\u03bb x \u2192 Dual-sym (f x))\n\nDual\u03a0\u03a3-spec : \u2200 P \u2192 Dual\u03a0\u03a3 P (dual\u03a0\u03a3 P)\nDual\u03a0\u03a3-spec \u03a0' = D\u03a0\u03a3\nDual\u03a0\u03a3-spec \u03a3' = D\u03a3\u03a0\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (mk \u03c0 S A B) = S (Dual\u03a0\u03a3-spec \u03c0) (\u03bb x \u2192 Dual-spec (B x))\nDual-spec (mk \u03c0 D A B) = D (Dual\u03a0\u03a3-spec \u03c0) (\u03bb x \u2192 Dual-spec (B x))\n\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele P \u2261 Tele (dual P)\n dual-Tele end = refl\n dual-Tele (\u03a0' A B) = cong (\u03a3 A) (funExt (\u03bb x \u2192 dual-Tele (B x)))\n dual-Tele (\u03a3' A B) = cong (\u03a3 A) (funExt (\u03bb x \u2192 dual-Tele (B x)))\n dual-Tele (later i P) = ?\n-}{-\nmodule _ X where\n El : Proto \u2192 \u2605\n El end = X\n El (\u03a0' A B) = \u03a0 A \u03bb x \u2192 El (B x)\n El (\u03a3' A B) = \u03a3 A \u03bb x \u2192 El (B x)\nmodule _ where\n El : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\n El end X = X _\n El (\u03a0' A B) X = \u03a0 A \u03bb x \u2192 El (B x) (\u03bb y \u2192 X (x , y))\n El (\u03a3' A B) X = \u03a3 A \u03bb x \u2192 El (B x) (\u03bb y \u2192 X (x , y))\n El (later i P) = ?\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n bind-spec : (P : Proto)(Q : Tele P \u2192 Proto)(X : Tele (P >>\u2261 Q) \u2192 \u2605) \u2192 El (P >>\u2261 Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P Q x y)))\n bind-spec end Q X = refl\n bind-spec (\u03a0' A B) Q X = cong (\u03a0 A) (funExt (\u03bb x \u2192 bind-spec (B x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb y \u2192 X (x , y))))\n bind-spec (\u03a3' A B) Q X = cong (\u03a3 A) (funExt (\u03bb x \u2192 bind-spec (B x) _ _))\n bind-spec (later i p) Q X = ?\n\n\nmodule _ {A B} where\n com : (P : Proto) \u2192 El P (const A) \u2192 El (dual P) (const B) \u2192 A \u00d7 B\n com end a b = a , b\n com (\u03a0' A B) f (x , y) = com (B x) (f x) y\n com (\u03a3' A B) (x , y) f = com (B x) y (f x)\n com (later i P) x y = ?\n\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n com-cont : (P : Proto){A : Tele P \u2192 \u2605}{B : Tele (dual P) \u2192 \u2605} \u2192 El P A \u2192 El (dual P) B \u2192 \u03a3 (Tele P) A \u00d7 \u03a3 (Tele (dual P)) B\n com-cont end p q = (_ , p) , (_ , q)\n com-cont (\u03a0' A B) p (m , q) with com-cont (B m) (p m) q\n ... | (X , a) , (Y , b) = ((m , X) , a) , (m , Y) , b\n com-cont (\u03a3' A B) (m , p) q with com-cont (B m) p (q m)\n ... | (X , a) , (Y , b) = ((m , X) , a) , (m , Y) , b\n com-cont (later i P) p q = ?\n-}\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Proto \u2192 \u2605\u2081 where\n recvD : \u2200 {M P} \u2192 (\u03a0M D M \u03bb m \u2192 this (P m)) \u2192 ProcessF this (mk \u03a0' D M P)\n recvS : \u2200 {M P} \u2192 (\u03a0M S M \u03bb m \u2192 this (P m)) \u2192 ProcessF this (mk \u03a0' S M P)\n sendD : \u2200 {M P} \u2192 \u03a0M D M (\u03bb m \u2192 this (P m) \u2192 ProcessF this (mk \u03a3' D M P))\n sendS : \u2200 {M P} \u2192 \u03a0M S M (\u03bb m \u2192 this (P m) \u2192 ProcessF this (mk \u03a3' S M P))\n\ndata Process (A : \u2605) : Proto \u2192 \u2605\u2081 where\n do : \u2200 {P} \u2192 ProcessF (Process A) P \u2192 Process A P\n end : A \u2192 Process A end\n\nmutual\n SimL : Proto \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Proto \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n left : \u2200 {q d M P Q} \u2192 SimL (mk q d M P) Q \u2192 Sim (mk q d M P) Q\n right : \u2200 {P q d M Q} \u2192 SimR P (mk q d M Q) \u2192 Sim P (mk q d M Q)\n end : Sim end end\n\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = \u2200 {A} \u2192 Process A (dual P) \u2192 Process A Q\n\n{-\nsim& : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 Sim end PA \u2192 Sim end PB \u2192 Sim end PAB\nsim&R : \u2200 {PA PB PAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 SimR end PA \u2192 SimR end PB \u2192 SimR end PAB\n\nsim& P& (right \u00b7PA) (right \u00b7PB) = {! (sim&R P& \u00b7PA \u00b7PB)!}\nsim& end& PA PB = PB\nsim& &end PA PB = PA\n\nsim&R (\u03a0D\u03a3D\u03a3S P&) (recvD PQA) (sendD m PQB) = sendS m (sim& (P& m) (PQA m) PQB)\nsim&R (\u03a0D\u03a0D\u03a0D P&) (recvD PQA) (recvD PQB) = recvD \u03bb m \u2192 sim& (P& m) (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) (recvS PQA) (recvS PQB) = recvS \u03bb m \u2192 sim& (P& m) (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) (recvS PQA) (sendS m PQB) = sendS m (sim& (P& m) (PQA m) PQB)\nsim&R (\u03a0S-D-D P&) (recvS PQA) (recvD PQB) = recvD \u03bb m \u2192 sim& (P& m) (PQA m) (PQB m)\nsim&R (\u03a0S-D-D P&) (recvS PQA) (sendD m PQB) = sendD m (sim& (P& m) (PQA m) PQB)\nsim&R (\u03a3D\u03a0D\u03a3S P&) (sendD m PQA) (recvD PQB) = sendS m (sim& (P& m) PQA (PQB m))\nsim&R &end _ ()\n-}\n\n{-\nsim&R (\u03a3D\u03a0D\u03a3S x) (sendD x\u2081 x\u2082) (recvD x\u2083) = {!!}\n-}\n\n{-\nsim& : \u2200 {PA PB PAB QA QB QAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 [ QA & QB \u2261 QAB ] \u2192 Sim PA QA \u2192 Sim PB QB \u2192 Sim PAB QAB\nsim&L : \u2200 {PA PB PAB QA QB QAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 [ QA & QB \u2261 QAB ] \u2192 SimL PA QA \u2192 SimL PB QB \u2192 SimL PAB QAB\nsim&R : \u2200 {PA PB PAB QA QB QAB} \u2192 [ PA & PB \u2261 PAB ] \u2192 [ QA & QB \u2261 QAB ] \u2192 SimR PA QA \u2192 SimR PB QB \u2192 SimR PAB QAB\n\nsim& P& Q& (left PQA) (left PQB) = left (sim&L P& Q& PQA PQB)\nsim& P& Q& (left PQA) (right PQB) = {!!}\nsim& P& Q& (left PQA) end = {!!}\nsim& P& Q& (right x) PQB = {!!}\nsim& P& Q& end PQB = {!!}\n\nsim&L (\u03a0D\u03a3D\u03a3S P&) Q& (recvD PQA) (sendD m PQB) = sendS m (sim& (P& m) Q& (PQA m) PQB)\nsim&L (\u03a0D\u03a0D\u03a0D P&) Q& (recvD PQA) (recvD PQB) = recvD \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&L (\u03a0S-S-S P&) Q& (recvS PQA) (recvS PQB) = recvS \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&L (\u03a0S-S-S P&) Q& (recvS PQA) (sendS m PQB) = sendS m (sim& (P& m) Q& (PQA m) PQB)\nsim&L (\u03a0S-D-D P&) Q& (recvS PQA) (recvD PQB) = recvD \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&L (\u03a0S-D-D P&) Q& (recvS PQA) (sendD m PQB) = sendD m (sim& (P& m) Q& (PQA m) PQB)\nsim&L (\u03a3D\u03a0D\u03a3S P&) Q& (sendD m PQA) (recvD PQB) = sendS m (sim& (P& m) Q& PQA (PQB m))\nsim&L &end Q& PQA ()\n\n{-\nsim&R (\u03a0D\u03a3D\u03a3S P&) Q& (recvD PQA) (sendD m PQB) = ?\nsim&R (\u03a0D\u03a0D\u03a0D P&) Q& (recvD PQA) (recvD PQB) = ? -- recvD \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) Q& (recvS PQA) (recvS PQB) = ? -- recvS \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&R (\u03a0S-S-S P&) Q& (recvS PQA) (sendS m PQB) = ? -- sendS m (sim& (P& m) Q& (PQA m) PQB)\nsim&R (\u03a0S-D-D P&) Q& (recvS PQA) (recvD PQB) = ? -- recvD \u03bb m \u2192 sim& (P& m) Q& (PQA m) (PQB m)\nsim&R (\u03a0S-D-D P&) Q& (recvS PQA) (sendD m PQB) = ? -- sendD m (sim& (P& m) Q& (PQA m) PQB)\nsim&R (\u03a3D\u03a0D\u03a3S P&) Q& (sendD m PQA) (recvD PQB) = ? -- sendS m (sim& (P& m) Q& PQA (PQB m))\nsim&R end& Q& PQA PQB = ?\n-}\n-}\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (mk \u03a0' S A B) = right (recvS (\u03bb x \u2192 left (sendS x (sim-id (B x)))))\nsim-id (mk \u03a0' D A B) = right (recvD (\u03bb x \u2192 left (sendD x (sim-id (B x)))))\nsim-id (mk \u03a3' S A B) = left (recvS (\u03bb x \u2192 right (sendS x (sim-id (B x)))))\nsim-id (mk \u03a3' D A B) = left (recvD (\u03bb x \u2192 right (sendD x (sim-id (B x)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (left x) QR = left (sim-compL Q-Q' x QR)\nsim-comp Q-Q' (right x) (left x\u2081) = sim-compRL Q-Q' x x\u2081\nsim-comp Q-Q' (right x) (right x\u2081) = right (sim-compR Q-Q' (right x) x\u2081)\nsim-comp () (right x) end\nsim-comp end end QR = QR\n\nsim-compRL end () QR\nsim-compRL (S D\u03a0\u03a3 x\u2081) (recvS x) (sendS x\u2082 x\u2083) = sim-comp (x\u2081 x\u2082) (x x\u2082) x\u2083\nsim-compRL (D D\u03a0\u03a3 x\u2081) (recvD x) (sendD x\u2082 x\u2083) = sim-comp (x\u2081 x\u2082) (x x\u2082) x\u2083\nsim-compRL (S D\u03a3\u03a0 x) (sendS x\u2081 x\u2082) (recvS x\u2083) = sim-comp (x x\u2081) x\u2082 (x\u2083 x\u2081)\nsim-compRL (D D\u03a3\u03a0 x) (sendD x\u2081 x\u2082) (recvD x\u2083) = sim-comp (x x\u2081) x\u2082 (x\u2083 x\u2081)\n\nsim-compL Q-Q' (recvD PQ) QR = recvD (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR)\nsim-compL Q-Q' (recvS PQ) QR = recvS (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR)\nsim-compL Q-Q' (sendD m PQ) QR = sendD m (sim-comp Q-Q' PQ QR)\nsim-compL Q-Q' (sendS m PQ) QR = sendS m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recvD QR) = recvD (\u03bb m \u2192 sim-comp Q-Q' PQ (QR m))\nsim-compR Q-Q' PQ (recvS QR) = recvS (\u03bb x \u2192 sim-comp Q-Q' PQ (QR x))\nsim-compR Q-Q' PQ (sendD m QR) = sendD m (sim-comp Q-Q' PQ QR)\nsim-compR Q-Q' PQ (sendS m QR) = sendS m (sim-comp Q-Q' PQ QR)\n\n{-\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-comp Q-Q' (left (recvD PQ)) QR = left (recvD (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR))\nsim-comp Q-Q' (left (recvS PQ)) QR = left (recvS (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR))\nsim-comp Q-Q' (left (sendD m PQ)) QR = left (sendD m (sim-comp Q-Q' PQ QR))\nsim-comp Q-Q' (left (sendS m PQ)) QR = left (sendS m (sim-comp Q-Q' PQ QR))\nsim-comp end (right ()) (left x\u2081)\nsim-comp end end QR = QR\nsim-comp end PQ end = PQ\nsim-comp (\u03a0\u03a3'S Q-Q') (right (recvS PQ)) (left (sendS m QR)) = sim-comp (Q-Q' m) (PQ m) QR\nsim-comp (\u03a0\u03a3'D Q-Q') (right (recvD PQ)) (left (sendD m QR)) = sim-comp (Q-Q' m) (PQ m) QR\nsim-comp (\u03a3\u03a0'S Q-Q') (right (sendS m PQ)) (left (recvS QR)) = sim-comp (Q-Q' m) PQ (QR m)\nsim-comp (\u03a3\u03a0'D Q-Q') (right (sendD m PQ)) (left (recvD QR)) = sim-comp (Q-Q' m) PQ (QR m)\nsim-comp Q-Q' PQ (right (recvD QR)) = right (recvD (\u03bb m \u2192 sim-comp Q-Q' PQ (QR m)))\nsim-comp Q-Q' PQ (right (recvS QR)) = right (recvS (\u03bb x \u2192 sim-comp Q-Q' PQ (QR x)))\nsim-comp Q-Q' PQ (right (sendD m QR)) = right (sendD m (sim-comp Q-Q' PQ QR))\nsim-comp Q-Q' PQ (right (sendS m QR)) = right (sendS m (sim-comp Q-Q' PQ QR))\n-}\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (left x) = right (sim-symL x)\n!\u02e2 (right x) = left (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recvD PQ) = recvD (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (recvS PQ) = recvS (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (sendD m PQ) = sendD m (!\u02e2 PQ)\nsim-symL (sendS m PQ) = sendS m (!\u02e2 PQ)\n\nsim-symR (recvD PQ) = recvD (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (recvS PQ) = recvS (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (sendD m PQ) = sendD m (!\u02e2 PQ)\nsim-symR (sendS m PQ) = sendS m (!\u02e2 PQ)\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process \ud835\udfd9 P\nsim-unit (right (recvD P)) = do (recvD (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (right (recvS P)) = do (recvS (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (right (sendD m P)) = do (sendD m (sim-unit P))\nsim-unit (right (sendS m P)) = do (sendS m (sim-unit P))\nsim-unit end = end 0\u2081\n\nmodule _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\nmodule _\n (funExtD : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n (funExtS : \u2200 {a}{b}{A : \u2605_ a}{B : ..(_ : A) \u2192 \u2605_ b}{f g : ..(x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (S D\u03a0\u03a3 x) = cong (S D\u03a0\u03a3) (funExtS (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (D D\u03a0\u03a3 x) = cong (D D\u03a0\u03a3) (funExtD (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (S D\u03a3\u03a0 x) = cong (S D\u03a3\u03a0) (funExtS (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (D D\u03a3\u03a0 x) = cong (D D\u03a3\u03a0) (funExtD (\u03bb y \u2192 Dual-sym-sym (x y)))\n\nmodule _\n (funExtD : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n (funExtS : \u2200 {a}{b}{A : \u2605_ a}{B : ..(_ : A) \u2192 \u2605_ b}{f g : ..(x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n sim-comp-assoc-end : \u2200 {P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (\u00f8P : Sim end P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' \u00f8P PQ) QR\n \u2261 sim-comp P-P' \u00f8P (sim-comp Q-Q' PQ QR)\n sim-comp-assoc-end (S D\u03a0\u03a3 x\u2081) Q-Q' (right (recvS x)) (left (sendS x\u2082 x\u2083)) QR\n = sim-comp-assoc-end (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 QR\n sim-comp-assoc-end (D D\u03a0\u03a3 x\u2081) Q-Q' (right (recvD x)) (left (sendD x\u2082 x\u2083)) QR\n = sim-comp-assoc-end (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 QR\n sim-comp-assoc-end (S D\u03a3\u03a0 x) Q-Q' (right (sendS x\u2081 x\u2082)) (left (recvS x\u2083)) QR\n = sim-comp-assoc-end (x x\u2081) Q-Q' x\u2082 (x\u2083 x\u2081) QR\n sim-comp-assoc-end (D D\u03a3\u03a0 x) Q-Q' (right (sendD x\u2081 x\u2082)) (left (recvD x\u2083)) QR\n = sim-comp-assoc-end (x x\u2081) Q-Q' x\u2082 (x\u2083 x\u2081) QR\n sim-comp-assoc-end P-P' (S D\u03a0\u03a3 x\u2081) (right \u00f8P) (right (recvS x)) (left (sendS x\u2082 x\u2083))\n = sim-comp-assoc-end P-P' (x\u2081 x\u2082) (right \u00f8P) (x x\u2082) x\u2083\n sim-comp-assoc-end P-P' (D D\u03a0\u03a3 x\u2081) (right \u00f8P) (right (recvD x)) (left (sendD x\u2082 x\u2083))\n = sim-comp-assoc-end P-P' (x\u2081 x\u2082) (right \u00f8P) (x x\u2082) x\u2083\n sim-comp-assoc-end P-P' (S D\u03a3\u03a0 x) (right \u00f8P) (right (sendS x\u2081 x\u2082)) (left (recvS x\u2083))\n = sim-comp-assoc-end P-P' (x x\u2081) (right \u00f8P) x\u2082 (x\u2083 x\u2081)\n sim-comp-assoc-end P-P' (D D\u03a3\u03a0 x) (right \u00f8P) (right (sendD x\u2081 x\u2082)) (left (recvD x\u2083))\n = sim-comp-assoc-end P-P' (x x\u2081) (right \u00f8P) x\u2082 (x\u2083 x\u2081)\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (recvD x\u2081))\n = cong (right \u2218 recvD) (funExtD (\u03bb m \u2192 sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (x\u2081 m)))\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (recvS x\u2081))\n = cong (right \u2218 recvS) (funExtS (\u03bb m \u2192 sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (x\u2081 m)))\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (sendD x\u2081 x\u2082))\n = cong (right \u2218 sendD x\u2081) (sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) x\u2082)\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (sendS x\u2081 x\u2082))\n = cong (right \u2218 sendS x\u2081) (sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) x\u2082)\n sim-comp-assoc-end end Q-Q' end PQ QR = refl\n\n \u2666-assoc-end : \u2200 {P Q R}(\u00f8P : Sim end P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (\u00f8P \u2666 PQ) \u2666 QR \u2261 \u00f8P \u2666 (PQ \u2666 QR)\n \u2666-assoc-end = sim-comp-assoc-end (Dual-spec _) (Dual-spec _)\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u223c sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' WP PQ QR {W'} W'-W \u00f8W'\n = sim-comp W'-W \u00f8W' (sim-comp Q-Q' (sim-comp P-P' WP PQ) QR)\n \u2261\u27e8 sym (sim-comp-assoc-end W'-W Q-Q' \u00f8W' (sim-comp P-P' WP PQ) QR) \u27e9\n sim-comp Q-Q' (sim-comp W'-W \u00f8W' (sim-comp P-P' WP PQ)) QR\n \u2261\u27e8 cong (\u03bb X \u2192 sim-comp Q-Q' X QR) (sym (sim-comp-assoc-end W'-W P-P' \u00f8W' WP PQ)) \u27e9\n sim-comp Q-Q' (sim-comp P-P' (sim-comp W'-W \u00f8W' WP) PQ) QR\n \u2261\u27e8 sim-comp-assoc-end P-P' Q-Q' (sim-comp W'-W \u00f8W' WP) PQ QR \u27e9\n sim-comp P-P' (sim-comp W'-W \u00f8W' WP) (sim-comp Q-Q' PQ QR)\n \u2261\u27e8 sim-comp-assoc-end W'-W P-P' \u00f8W' WP (sim-comp Q-Q' PQ QR) \u27e9\n sim-comp W'-W \u00f8W' (sim-comp P-P' WP (sim-comp Q-Q' PQ QR))\n \u220e\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u223c WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n\n\u223c-\u00f8 : \u2200 {P}{s s' : Sim end P} \u2192 s \u223c s' \u2192 s \u2261 s'\n\u223c-\u00f8 s\u223cs' = s\u223cs' end end\n\nmodule _\n (funExtD : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n (funExtS : \u2200 {a}{b}{A : \u2605_ a}{B : ..(_ : A) \u2192 \u2605_ b}{f g : ..(x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (left (recvD x)) = cong (left \u2218 recvD) (funExtD \u03bb m \u2192 sim-!! (x m))\n sim-!! (left (recvS x)) = cong (left \u2218 recvS) (funExtS \u03bb m \u2192 sim-!! (x m))\n sim-!! (left (sendD x x\u2081)) = cong (left \u2218 sendD x) (sim-!! x\u2081)\n sim-!! (left (sendS x x\u2081)) = cong (left \u2218 sendS x) (sim-!! x\u2081)\n sim-!! (right (recvD x)) = cong (right \u2218 recvD) (funExtD \u03bb m \u2192 sim-!! (x m))\n sim-!! (right (recvS x)) = cong (right \u2218 recvS) (funExtS \u03bb m \u2192 sim-!! (x m))\n sim-!! (right (sendD x x\u2081)) = cong (right \u2218 sendD x) (sim-!! x\u2081)\n sim-!! (right (sendS x x\u2081)) = cong (right \u2218 sendS x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end (S D\u03a0\u03a3 x\u2081) (right (recvS x)) (left (sendS x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (D D\u03a0\u03a3 x\u2081) (right (recvD x)) (left (sendD x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (S D\u03a3\u03a0 x) (right (sendS x\u2081 x\u2082)) (left (recvS x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end (D D\u03a3\u03a0 x) (right (sendD x\u2081 x\u2082)) (left (recvD x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (right x) (right (recvD x\u2081))\n = cong (left \u2218 recvD) (funExtD (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (right x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (right x) (right (recvS x\u2081))\n = cong (left \u2218 recvS) (funExtS (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (right x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (right x) (right (sendD x\u2081 x\u2082))\n = cong (left \u2218 sendD x\u2081) (sim-comp-!-end Q-Q' (right x) x\u2082)\n sim-comp-!-end Q-Q' (right x) (right (sendS x\u2081 x\u2082))\n = cong (left \u2218 sendS x\u2081) (sim-comp-!-end Q-Q' (right x) x\u2082)\n sim-comp-!-end end end QR = {!!}\n\n open \u2261-Reasoning\n module _ {P Q}{s s' : Sim P Q} where\n !\u02e2-cong : s \u223c s' \u2192 !\u02e2 s \u223c !\u02e2 s'\n !\u02e2-cong s\u223cs' Q'-Q \u00f8Q'\n = sim-comp Q'-Q \u00f8Q' (!\u02e2 s)\n \u2261\u27e8 {!!} \u27e9\n sim-comp Q'-Q \u00f8Q' (!\u02e2 (sim-comp (Dual-spec Q) s (sim-id _)))\n \u2261\u27e8 {!!} \u27e9\n sim-comp Q'-Q \u00f8Q' (!\u02e2 s')\n \u220e\n\n postulate\n sim-comp-assoc-end' : \u2200 {P Q Q' R R'}(Q-Q' : Dual Q Q')(R-R' : Dual R R')\n (PQ : Sim P Q)(QR : Sim Q' R )(R\u00f8 : Sim R' end)\n \u2192 sim-comp R-R' (sim-comp Q-Q' PQ QR) R\u00f8\n \u2261 sim-comp Q-Q' PQ (sim-comp R-R' QR R\u00f8)\n\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc-end funExtD funExtS R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExtD funExtS Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n -- \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc-end' Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n \u2261\u27e8 \u223c-\u00f8 {!!}\u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n{-\n\nunit-sim : \u2200 {P} \u2192 Process \ud835\udfd9 P \u2192 Sim end P\nunit-sim (do (send m x)) = right (send m (unit-sim x))\nunit-sim (do (recv x)) = right (recv (\u03bb m \u2192 unit-sim (x m)))\nunit-sim (end x) = end\n\n{-\ntoEl : \u2200 {P A} \u2192 Process A P \u2192 El P (const A)\ntoEl (end x) = x\ntoEl (do (recv f)) = \u03bb x \u2192 toEl (f x)\ntoEl (do (send m x)) = m , toEl x\n-}\n\nidP : \u2200 {A} \u2192 Process A (\u03a0' A (const end))\nidP = do (recv end)\n\ndual\u00b2 : \u2200 {P A} \u2192 Process A P \u2192 Process A (dual (dual P))\ndual\u00b2 (end x) = end x\ndual\u00b2 (do (recv x)) = do (recv (\u03bb m \u2192 dual\u00b2 (x m)))\ndual\u00b2 (do (send m x)) = do (send m (dual\u00b2 x))\n\napply-sim : \u2200 {P Q} \u2192 Sim P Q \u2192 P \u27f9\u1d3e Q\napply-sim (left (send m x)) (do (recv x\u2081)) = apply-sim x (x\u2081 m)\napply-sim (left (recv x)) (do (send m x\u2081)) = apply-sim (x m) x\u2081\napply-sim (right (send m x)) P\u2082 = do (send m (apply-sim x P\u2082))\napply-sim (right (recv x)) P\u2082 = do (recv (\u03bb m \u2192 apply-sim (x m) P\u2082))\napply-sim end P = P\n\napply-sim' : \u2200 {P Q} \u2192 Sim P Q \u2192 Q \u27f9\u1d3e P -- \u2200 {A} \u2192 Process A Q \u2192 Process A (dual P)\napply-sim' PQ P = apply-sim (sim-sym PQ) P\n\napply : \u2200 {P Q A} \u2192 P \u27f9 Q \u2192 Process A P \u2192 Process A Q\napply PQ P = apply-sim PQ (dual\u00b2 P)\n\nmodule _ (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n apply-comp : \u2200 {P Q R A}(PQ : Sim P Q)(QR : Sim (dual Q) R)(p : Process A (dual P)) \u2192 apply-sim (sim-comp PQ QR) p \u2261 apply QR (apply-sim PQ p)\n apply-comp (left (send m x)) QR (do (recv x\u2081)) = apply-comp x QR (x\u2081 m)\n apply-comp (left (recv x)) QR (do (send m x\u2081)) = apply-comp (x m) QR x\u2081\n apply-comp (right (send m x)) (left (recv x\u2081)) p = apply-comp x (x\u2081 m) p\n apply-comp (right (send m x)) (right (recv x\u2081)) p = cong (\u03bb X \u2192 do (recv X))\n (funExt (\u03bb m' \u2192 apply-comp (right (send m x)) (x\u2081 m') p))\n apply-comp (right (send m x)) (right (send m\u2081 x\u2081)) p\n rewrite apply-comp (right (send m x)) x\u2081 p = refl\n apply-comp (right (recv x)) (left (send m x\u2081)) p = apply-comp (x m) x\u2081 p\n apply-comp (right (recv x)) (right (send m x\u2081)) p\n rewrite apply-comp (right (recv x)) x\u2081 p = refl\n apply-comp (right (recv x)) (right (recv x\u2081)) p = cong (\u03bb X \u2192 do (recv X))\n (funExt (\u03bb m \u2192 apply-comp (right (recv x)) (x\u2081 m) p))\n apply-comp end QR (do ())\n apply-comp end QR (end x) = refl\n\n{-\n_>>=P_ : \u2200 {A B P}{Q : Tele P \u2192 Proto} \u2192 Process A P \u2192 ((p : Tele P) \u2192 A \u2192 Process B (Q p)) \u2192 Process B (P >>\u2261 Q)\nsend m p >>=P k = send m (p >>=P (\u03bb p\u2081 \u2192 k (m , p\u2081)))\nrecv x >>=P k = recv (\u03bb m \u2192 x m >>=P (\u03bb p \u2192 k (m , p)))\nend x >>=P k = k 0\u2081 x\n\n\n {-\nmodule _ where\n bind-com : (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (dual P) \u2192 Proto)\n (X : Tele (P >>\u2261 Q) \u2192 \u2605)(Y : Tele (dual P >>\u2261 R) \u2192 \u2605)\n \u2192 El (P >>\u2261 Q) X \u2192 El (dual P >>\u2261 R) Y \u2192 ? \u00d7 ?\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"\n-- {-# OPTIONS --without-K #-}\nopen import Coinduction\nopen import Function.NP\nopen import Type\nopen import Level\nopen import Data.Product\nopen import Data.One\n\nopen import Relation.Binary.PropositionalEquality.NP\n\nmodule Control.Protocol.Choreography where\nopen import Control.Strategy renaming (Strategy to Client) public\n\n\u03a0\u00b7 : \u2200 {a b}(A : \u2605_ a) \u2192 (B : ..(_ : A) \u2192 \u2605_ b) \u2192 \u2605_ (a \u2294 b)\n\u03a0\u00b7 A B = ..(x : A) \u2192 B x\n\ndata Mod : \u2605 where S D : Mod\n\n\u2192M : \u2200 {a b} \u2192 Mod \u2192 \u2605_ a \u2192 \u2605_ b \u2192 \u2605_ (a \u2294 b)\n\u2192M S A B = ..(_ : A) \u2192 B\n\u2192M D A B = A \u2192 B\n\n\u03a0M : \u2200 {a b}(m : Mod) \u2192 (A : \u2605_ a) \u2192 (B : \u2192M m A (\u2605_ b)) \u2192 \u2605_ (a \u2294 b)\n\u03a0M S A B = \u03a0\u00b7 A B\n\u03a0M D A B = \u03a0 A B\n\n-- appM : \u2200 {a b}(m : Mod){A : \u2605_ a}{B : \u2192M m A (\u2605_ b)}(P : \u03a0M m A B)(x : A) \u2192 B\n\ndata Proto : \u2605\u2081 where\n end : Proto\n \u03a0' \u03a3' : (f : Mod)(A : \u2605)(B : \u2192M f A Proto) \u2192 Proto\n\n{-\nTele : Proto \u2192 \u2605\nTele end = \ud835\udfd9\nTele (\u03a0' A B) = \u03a3 A \u03bb x \u2192 Tele (B x)\nTele (\u03a3' A B) = \u03a3 A \u03bb x \u2192 Tele (B x)\nTele (later i P) = ?\n\n_>>\u2261_ : (P : Proto) \u2192 (Tele P \u2192 Proto) \u2192 Proto\nend >>\u2261 Q = Q _\n\u03a0' A B >>\u2261 Q = \u03a0' A (\u03bb x \u2192 B x >>\u2261 (\u03bb xs \u2192 Q (x , xs)))\n\u03a3' A B >>\u2261 Q = \u03a3' A (\u03bb x \u2192 B x >>\u2261 (\u03bb xs \u2192 Q (x , xs)))\nlater i P >>\u2261 Q = ?\n\n++Tele : \u2200 (P : Proto)(Q : Tele P \u2192 Proto) \u2192 (x : Tele P) \u2192 Tele (Q x) \u2192 Tele (P >>\u2261 Q)\n++Tele end Q x y = y\n++Tele (\u03a0' M C) Q (m , x) y = m , ++Tele (C m) (\u03bb x\u2081 \u2192 Q (m , x\u2081)) x y\n++Tele (\u03a3' M C) Q (m , x) y = m , ++Tele (C m) _ x y\n++Tele (later i P) Q x y = ?\n\nmodule _ (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : \u2605_ b}{f g : A \u2192 B} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n right-unit : \u2200 (P : Proto) \u2192 (P >>\u2261 \u03bb x \u2192 end) \u2261 P\n right-unit end = refl\n right-unit (\u03a0' M C) = let p = funExt (\u03bb x \u2192 right-unit (C x)) in cong (\u03a0' M) p\n right-unit (\u03a3' M C) = cong (\u03a3' M) (funExt (\u03bb x \u2192 right-unit (C x)))\n right-unit (later i P) = ?\n\n assoc : \u2200 (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (P >>\u2261 Q) \u2192 Proto)\n \u2192 P >>\u2261 (\u03bb x \u2192 Q x >>\u2261 (\u03bb y \u2192 R (++Tele P Q x y))) \u2261 ((P >>\u2261 Q) >>\u2261 R)\n assoc end Q R = refl\n assoc (\u03a0' M C\u2081) Q R = cong (\u03a0' M) (funExt (\u03bb x \u2192 assoc (C\u2081 x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb xs \u2192 R (x , xs))))\n assoc (\u03a3' M C\u2081) Q R = cong (\u03a3' M) (funExt (\u03bb x \u2192 assoc (C\u2081 x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb xs \u2192 R (x , xs))))\n assoc (later i P) Q R = ?\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>\u2261 \u03bb _ \u2192 Q\n-}\n\n_\u00d7'_ : Set \u2192 Proto \u2192 Proto\nA \u00d7' B = \u03a3' D A \u03bb _ \u2192 B\n\n_\u2192'_ : Set \u2192 Proto \u2192 Proto\nA \u2192' B = \u03a0' D A \u03bb _ \u2192 B\n\ndual : Proto \u2192 Proto\ndual end = end\ndual (\u03a0' S A B) = \u03a3' S A (\u03bb x \u2192 dual (B x))\ndual (\u03a0' D A B) = \u03a3' D A (\u03bb x \u2192 dual (B x))\ndual (\u03a3' S A B) = \u03a0' S A (\u03bb x \u2192 dual (B x))\ndual (\u03a3' D A B) = \u03a0' D A (\u03bb x \u2192 dual (B x))\n\ndata Dual : Proto \u2192 Proto \u2192 \u2605\u2081 where\n end : Dual end end\n \u03a0\u03a3'S : \u2200 {A B B'} \u2192 (\u2200 ..x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a0' S A B) (\u03a3' S A B')\n \u03a0\u03a3'D : \u2200 {A B B'} \u2192 (\u2200 x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a0' D A B) (\u03a3' D A B')\n \u03a3\u03a0'S : \u2200 {A B B'} \u2192 (\u2200 ..x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a3' S A B) (\u03a0' S A B')\n \u03a3\u03a0'D : \u2200 {A B B'} \u2192 (\u2200 x \u2192 Dual (B x) (B' x)) \u2192 Dual (\u03a3' D A B) (\u03a0' D A B')\n\nDual-sym : \u2200 {P Q} \u2192 Dual P Q \u2192 Dual Q P\nDual-sym end = end\nDual-sym (\u03a0\u03a3'S f) = \u03a3\u03a0'S (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a0\u03a3'D f) = \u03a3\u03a0'D (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u03a0'S f) = \u03a0\u03a3'S (\u03bb x \u2192 Dual-sym (f x))\nDual-sym (\u03a3\u03a0'D f) = \u03a0\u03a3'D (\u03bb x \u2192 Dual-sym (f x))\n\nDual-spec : \u2200 P \u2192 Dual P (dual P)\nDual-spec end = end\nDual-spec (\u03a0' S A B) = \u03a0\u03a3'S (\u03bb x \u2192 Dual-spec (B x))\nDual-spec (\u03a0' D A B) = \u03a0\u03a3'D (\u03bb x \u2192 Dual-spec (B x))\nDual-spec (\u03a3' S A B) = \u03a3\u03a0'S (\u03bb x \u2192 Dual-spec (B x))\nDual-spec (\u03a3' D A B) = \u03a3\u03a0'D (\u03bb x \u2192 Dual-spec (B x))\n{-\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n dual-Tele : \u2200 P \u2192 Tele P \u2261 Tele (dual P)\n dual-Tele end = refl\n dual-Tele (\u03a0' A B) = cong (\u03a3 A) (funExt (\u03bb x \u2192 dual-Tele (B x)))\n dual-Tele (\u03a3' A B) = cong (\u03a3 A) (funExt (\u03bb x \u2192 dual-Tele (B x)))\n dual-Tele (later i P) = ?\n-}{-\nmodule _ X where\n El : Proto \u2192 \u2605\n El end = X\n El (\u03a0' A B) = \u03a0 A \u03bb x \u2192 El (B x)\n El (\u03a3' A B) = \u03a3 A \u03bb x \u2192 El (B x)\nmodule _ where\n El : (P : Proto) \u2192 (Tele P \u2192 \u2605) \u2192 \u2605\n El end X = X _\n El (\u03a0' A B) X = \u03a0 A \u03bb x \u2192 El (B x) (\u03bb y \u2192 X (x , y))\n El (\u03a3' A B) X = \u03a3 A \u03bb x \u2192 El (B x) (\u03bb y \u2192 X (x , y))\n El (later i P) = ?\n\nmodule ElBind (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n\n bind-spec : (P : Proto)(Q : Tele P \u2192 Proto)(X : Tele (P >>\u2261 Q) \u2192 \u2605) \u2192 El (P >>\u2261 Q) X \u2261 El P (\u03bb x \u2192 El (Q x) (\u03bb y \u2192 X (++Tele P Q x y)))\n bind-spec end Q X = refl\n bind-spec (\u03a0' A B) Q X = cong (\u03a0 A) (funExt (\u03bb x \u2192 bind-spec (B x) (\u03bb xs \u2192 Q (x , xs)) (\u03bb y \u2192 X (x , y))))\n bind-spec (\u03a3' A B) Q X = cong (\u03a3 A) (funExt (\u03bb x \u2192 bind-spec (B x) _ _))\n bind-spec (later i p) Q X = ?\n\n\nmodule _ {A B} where\n com : (P : Proto) \u2192 El P (const A) \u2192 El (dual P) (const B) \u2192 A \u00d7 B\n com end a b = a , b\n com (\u03a0' A B) f (x , y) = com (B x) (f x) y\n com (\u03a3' A B) (x , y) f = com (B x) y (f x)\n com (later i P) x y = ?\n\nmodule _ (funExt : \u2200 {a b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)where\n com-cont : (P : Proto){A : Tele P \u2192 \u2605}{B : Tele (dual P) \u2192 \u2605} \u2192 El P A \u2192 El (dual P) B \u2192 \u03a3 (Tele P) A \u00d7 \u03a3 (Tele (dual P)) B\n com-cont end p q = (_ , p) , (_ , q)\n com-cont (\u03a0' A B) p (m , q) with com-cont (B m) (p m) q\n ... | (X , a) , (Y , b) = ((m , X) , a) , (m , Y) , b\n com-cont (\u03a3' A B) (m , p) q with com-cont (B m) p (q m)\n ... | (X , a) , (Y , b) = ((m , X) , a) , (m , Y) , b\n com-cont (later i P) p q = ?\n-}\n\ndata ProcessF (this : Proto \u2192 \u2605\u2081): Proto \u2192 \u2605\u2081 where\n recvD : \u2200 {M P} \u2192 (\u03a0M D M \u03bb m \u2192 this (P m)) \u2192 ProcessF this (\u03a0' D M P)\n recvS : \u2200 {M P} \u2192 (\u03a0M S M \u03bb m \u2192 this (P m)) \u2192 ProcessF this (\u03a0' S M P)\n sendD : \u2200 {M P} \u2192 \u03a0M D M (\u03bb m \u2192 this (P m) \u2192 ProcessF this (\u03a3' D M P))\n sendS : \u2200 {M P} \u2192 \u03a0M S M (\u03bb m \u2192 this (P m) \u2192 ProcessF this (\u03a3' S M P))\n\ndata Process (A : \u2605) : Proto \u2192 \u2605\u2081 where\n do : \u2200 {P} \u2192 ProcessF (Process A) P \u2192 Process A P\n end : A \u2192 Process A end\n\nmutual\n SimL : Proto \u2192 Proto \u2192 \u2605\u2081\n SimL P Q = ProcessF (flip Sim Q) P\n\n SimR : Proto \u2192 Proto \u2192 \u2605\u2081\n SimR P Q = ProcessF (Sim P) Q\n\n data Sim : Proto \u2192 Proto \u2192 \u2605\u2081 where\n left : \u2200 {P Q} \u2192 SimL P Q \u2192 Sim P Q\n right : \u2200 {P Q} \u2192 SimR P Q \u2192 Sim P Q\n end : Sim end end\n\n_\u27f9_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9 Q = Sim (dual P) Q\n\n_\u27f9\u1d3e_ : Proto \u2192 Proto \u2192 \u2605\u2081\nP \u27f9\u1d3e Q = \u2200 {A} \u2192 Process A (dual P) \u2192 Process A Q\n\nsim-id : \u2200 P \u2192 Sim (dual P) P\nsim-id end = end\nsim-id (\u03a0' S A B) = right (recvS (\u03bb x \u2192 left (sendS x (sim-id (B x)))))\nsim-id (\u03a0' D A B) = right (recvD (\u03bb x \u2192 left (sendD x (sim-id (B x)))))\nsim-id (\u03a3' S A B) = left (recvS (\u03bb x \u2192 right (sendS x (sim-id (B x)))))\nsim-id (\u03a3' D A B) = left (recvD (\u03bb x \u2192 right (sendD x (sim-id (B x)))))\n\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-compRL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimR P Q \u2192 SimL Q' R \u2192 Sim P R\nsim-compL : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 SimL P Q \u2192 Sim Q' R \u2192 SimL P R\nsim-compR : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 SimR Q' R \u2192 SimR P R\n\nsim-comp Q-Q' (left x) QR = left (sim-compL Q-Q' x QR)\nsim-comp Q-Q' (right x) (left x\u2081) = sim-compRL Q-Q' x x\u2081\nsim-comp Q-Q' (right x) (right x\u2081) = right (sim-compR Q-Q' (right x) x\u2081)\nsim-comp end (right x) end = right x\nsim-comp end end QR = QR\n\nsim-compRL end () QR\nsim-compRL (\u03a0\u03a3'S x\u2081) (recvS x) (sendS x\u2082 x\u2083) = sim-comp (x\u2081 x\u2082) (x x\u2082) x\u2083\nsim-compRL (\u03a0\u03a3'D x\u2081) (recvD x) (sendD x\u2082 x\u2083) = sim-comp (x\u2081 x\u2082) (x x\u2082) x\u2083\nsim-compRL (\u03a3\u03a0'S x) (sendS x\u2081 x\u2082) (recvS x\u2083) = sim-comp (x x\u2081) x\u2082 (x\u2083 x\u2081)\nsim-compRL (\u03a3\u03a0'D x) (sendD x\u2081 x\u2082) (recvD x\u2083) = sim-comp (x x\u2081) x\u2082 (x\u2083 x\u2081)\n\nsim-compL Q-Q' (recvD PQ) QR = recvD (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR)\nsim-compL Q-Q' (recvS PQ) QR = recvS (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR)\nsim-compL Q-Q' (sendD m PQ) QR = sendD m (sim-comp Q-Q' PQ QR)\nsim-compL Q-Q' (sendS m PQ) QR = sendS m (sim-comp Q-Q' PQ QR)\n\nsim-compR Q-Q' PQ (recvD QR) = recvD (\u03bb m \u2192 sim-comp Q-Q' PQ (QR m))\nsim-compR Q-Q' PQ (recvS QR) = recvS (\u03bb x \u2192 sim-comp Q-Q' PQ (QR x))\nsim-compR Q-Q' PQ (sendD m QR) = sendD m (sim-comp Q-Q' PQ QR)\nsim-compR Q-Q' PQ (sendS m QR) = sendS m (sim-comp Q-Q' PQ QR)\n\n{-\nsim-comp : \u2200 {P Q Q' R} \u2192 Dual Q Q' \u2192 Sim P Q \u2192 Sim Q' R \u2192 Sim P R\nsim-comp Q-Q' (left (recvD PQ)) QR = left (recvD (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR))\nsim-comp Q-Q' (left (recvS PQ)) QR = left (recvS (\u03bb m \u2192 sim-comp Q-Q' (PQ m) QR))\nsim-comp Q-Q' (left (sendD m PQ)) QR = left (sendD m (sim-comp Q-Q' PQ QR))\nsim-comp Q-Q' (left (sendS m PQ)) QR = left (sendS m (sim-comp Q-Q' PQ QR))\nsim-comp end (right ()) (left x\u2081)\nsim-comp end end QR = QR\nsim-comp end PQ end = PQ\nsim-comp (\u03a0\u03a3'S Q-Q') (right (recvS PQ)) (left (sendS m QR)) = sim-comp (Q-Q' m) (PQ m) QR\nsim-comp (\u03a0\u03a3'D Q-Q') (right (recvD PQ)) (left (sendD m QR)) = sim-comp (Q-Q' m) (PQ m) QR\nsim-comp (\u03a3\u03a0'S Q-Q') (right (sendS m PQ)) (left (recvS QR)) = sim-comp (Q-Q' m) PQ (QR m)\nsim-comp (\u03a3\u03a0'D Q-Q') (right (sendD m PQ)) (left (recvD QR)) = sim-comp (Q-Q' m) PQ (QR m)\nsim-comp Q-Q' PQ (right (recvD QR)) = right (recvD (\u03bb m \u2192 sim-comp Q-Q' PQ (QR m)))\nsim-comp Q-Q' PQ (right (recvS QR)) = right (recvS (\u03bb x \u2192 sim-comp Q-Q' PQ (QR x)))\nsim-comp Q-Q' PQ (right (sendD m QR)) = right (sendD m (sim-comp Q-Q' PQ QR))\nsim-comp Q-Q' PQ (right (sendS m QR)) = right (sendS m (sim-comp Q-Q' PQ QR))\n-}\n\n_\u2666_ : \u2200 {P Q R} \u2192 Sim P Q \u2192 Sim (dual Q) R \u2192 Sim P R\n_\u2666_ = sim-comp (Dual-spec _)\n\n\u27f9-comp : \u2200 {P Q R} \u2192 P \u27f9 Q \u2192 Q \u27f9 R \u2192 P \u27f9 R\n\u27f9-comp = _\u2666_\n\n!\u02e2 : \u2200 {P Q} \u2192 Sim P Q \u2192 Sim Q P\nsim-symL : \u2200 {P Q} \u2192 SimL P Q \u2192 SimR Q P\nsim-symR : \u2200 {P Q} \u2192 SimR P Q \u2192 SimL Q P\n\n!\u02e2 (left x) = right (sim-symL x)\n!\u02e2 (right x) = left (sim-symR x)\n!\u02e2 end = end\n\nsim-symL (recvD PQ) = recvD (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (recvS PQ) = recvS (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symL (sendD m PQ) = sendD m (!\u02e2 PQ)\nsim-symL (sendS m PQ) = sendS m (!\u02e2 PQ)\n\nsim-symR (recvD PQ) = recvD (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (recvS PQ) = recvS (\u03bb m \u2192 !\u02e2 (PQ m))\nsim-symR (sendD m PQ) = sendD m (!\u02e2 PQ)\nsim-symR (sendS m PQ) = sendS m (!\u02e2 PQ)\n\nsim-unit : \u2200 {P} \u2192 Sim end P \u2192 Process \ud835\udfd9 P\nsim-unit (left ())\nsim-unit (right (recvD P)) = do (recvD (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (right (recvS P)) = do (recvS (\u03bb m \u2192 sim-unit (P m)))\nsim-unit (right (sendD m P)) = do (sendD m (sim-unit P))\nsim-unit (right (sendS m P)) = do (sendS m (sim-unit P))\nsim-unit end = end 0\u2081\n\nmodule _ {P Q : Proto} where\n infix 2 _\u223c_\n _\u223c_ : (PQ PQ' : Sim P Q) \u2192 \u2605\u2081\n PQ \u223c PQ' = \u2200 {P'} \u2192 (P'-P : Dual P' P) \u2192 (\u00f8P : Sim end P')\n \u2192 sim-comp P'-P \u00f8P PQ \u2261 sim-comp P'-P \u00f8P PQ'\n\nmodule _\n (funExtD : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n (funExtS : \u2200 {a}{b}{A : \u2605_ a}{B : ..(_ : A) \u2192 \u2605_ b}{f g : ..(x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n Dual-sym-sym : \u2200 {P Q} (P-Q : Dual P Q) \u2192 P-Q \u2261 Dual-sym (Dual-sym P-Q)\n Dual-sym-sym end = refl\n Dual-sym-sym (\u03a0\u03a3'S x) = cong \u03a0\u03a3'S (funExtS (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a0\u03a3'D x) = cong \u03a0\u03a3'D (funExtD (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u03a0'S x) = cong \u03a3\u03a0'S (funExtS (\u03bb y \u2192 Dual-sym-sym (x y)))\n Dual-sym-sym (\u03a3\u03a0'D x) = cong \u03a3\u03a0'D (funExtD (\u03bb y \u2192 Dual-sym-sym (x y)))\n\nmodule _\n (funExtD : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n (funExtS : \u2200 {a}{b}{A : \u2605_ a}{B : ..(_ : A) \u2192 \u2605_ b}{f g : ..(x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n sim-comp-assoc-end : \u2200 {P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (\u00f8P : Sim end P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' \u00f8P PQ) QR\n \u2261 sim-comp P-P' \u00f8P (sim-comp Q-Q' PQ QR)\n sim-comp-assoc-end P-P' Q-Q' (left ()) PQ QR\n sim-comp-assoc-end end Q-Q' (right ()) (left PQ) QR\n sim-comp-assoc-end (\u03a0\u03a3'S x\u2081) Q-Q' (right (recvS x)) (left (sendS x\u2082 x\u2083)) QR\n = sim-comp-assoc-end (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 QR\n sim-comp-assoc-end (\u03a0\u03a3'D x\u2081) Q-Q' (right (recvD x)) (left (sendD x\u2082 x\u2083)) QR\n = sim-comp-assoc-end (x\u2081 x\u2082) Q-Q' (x x\u2082) x\u2083 QR\n sim-comp-assoc-end (\u03a3\u03a0'S x) Q-Q' (right (sendS x\u2081 x\u2082)) (left (recvS x\u2083)) QR\n = sim-comp-assoc-end (x x\u2081) Q-Q' x\u2082 (x\u2083 x\u2081) QR\n sim-comp-assoc-end (\u03a3\u03a0'D x) Q-Q' (right (sendD x\u2081 x\u2082)) (left (recvD x\u2083)) QR\n = sim-comp-assoc-end (x x\u2081) Q-Q' x\u2082 (x\u2083 x\u2081) QR\n sim-comp-assoc-end P-P' end (right \u00f8P) (right ()) (left x\u2081)\n sim-comp-assoc-end P-P' (\u03a0\u03a3'S x\u2081) (right \u00f8P) (right (recvS x)) (left (sendS x\u2082 x\u2083))\n = sim-comp-assoc-end P-P' (x\u2081 x\u2082) (right \u00f8P) (x x\u2082) x\u2083\n sim-comp-assoc-end P-P' (\u03a0\u03a3'D x\u2081) (right \u00f8P) (right (recvD x)) (left (sendD x\u2082 x\u2083))\n = sim-comp-assoc-end P-P' (x\u2081 x\u2082) (right \u00f8P) (x x\u2082) x\u2083\n sim-comp-assoc-end P-P' (\u03a3\u03a0'S x) (right \u00f8P) (right (sendS x\u2081 x\u2082)) (left (recvS x\u2083))\n = sim-comp-assoc-end P-P' (x x\u2081) (right \u00f8P) x\u2082 (x\u2083 x\u2081)\n sim-comp-assoc-end P-P' (\u03a3\u03a0'D x) (right \u00f8P) (right (sendD x\u2081 x\u2082)) (left (recvD x\u2083))\n = sim-comp-assoc-end P-P' (x x\u2081) (right \u00f8P) x\u2082 (x\u2083 x\u2081)\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (recvD x\u2081))\n = cong (right \u2218 recvD) (funExtD (\u03bb m \u2192 sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (x\u2081 m)))\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (recvS x\u2081))\n = cong (right \u2218 recvS) (funExtS (\u03bb m \u2192 sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (x\u2081 m)))\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (sendD x\u2081 x\u2082))\n = cong (right \u2218 sendD x\u2081) (sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) x\u2082)\n sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) (right (sendS x\u2081 x\u2082))\n = cong (right \u2218 sendS x\u2081) (sim-comp-assoc-end P-P' Q-Q' (right \u00f8P) (right x) x\u2082)\n sim-comp-assoc-end P-P' end (right \u00f8P) (right ()) end\n sim-comp-assoc-end end end (right \u00f8P) end QR = refl\n sim-comp-assoc-end end Q-Q' end PQ QR = refl\n\n \u2666-assoc-end : \u2200 {P Q R}(\u00f8P : Sim end P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (\u00f8P \u2666 PQ) \u2666 QR \u2261 \u00f8P \u2666 (PQ \u2666 QR)\n \u2666-assoc-end = sim-comp-assoc-end (Dual-spec _) (Dual-spec _)\n\n open \u2261-Reasoning\n sim-comp-assoc : \u2200 {W P P' Q Q' R}(P-P' : Dual P P')(Q-Q' : Dual Q Q')\n (WP : Sim W P)(PQ : Sim P' Q)(QR : Sim Q' R)\n \u2192 sim-comp Q-Q' (sim-comp P-P' WP PQ) QR\n \u223c sim-comp P-P' WP (sim-comp Q-Q' PQ QR)\n sim-comp-assoc P-P' Q-Q' WP PQ QR {W'} W'-W \u00f8W'\n = sim-comp W'-W \u00f8W' (sim-comp Q-Q' (sim-comp P-P' WP PQ) QR)\n \u2261\u27e8 sym (sim-comp-assoc-end W'-W Q-Q' \u00f8W' (sim-comp P-P' WP PQ) QR) \u27e9\n sim-comp Q-Q' (sim-comp W'-W \u00f8W' (sim-comp P-P' WP PQ)) QR\n \u2261\u27e8 cong (\u03bb X \u2192 sim-comp Q-Q' X QR) (sym (sim-comp-assoc-end W'-W P-P' \u00f8W' WP PQ)) \u27e9\n sim-comp Q-Q' (sim-comp P-P' (sim-comp W'-W \u00f8W' WP) PQ) QR\n \u2261\u27e8 sim-comp-assoc-end P-P' Q-Q' (sim-comp W'-W \u00f8W' WP) PQ QR \u27e9\n sim-comp P-P' (sim-comp W'-W \u00f8W' WP) (sim-comp Q-Q' PQ QR)\n \u2261\u27e8 sim-comp-assoc-end W'-W P-P' \u00f8W' WP (sim-comp Q-Q' PQ QR) \u27e9\n sim-comp W'-W \u00f8W' (sim-comp P-P' WP (sim-comp Q-Q' PQ QR))\n \u220e\n\n \u2666-assoc : \u2200 {W P Q R}(WP : Sim W P)(PQ : Sim (dual P) Q)(QR : Sim (dual Q) R)\n \u2192 (WP \u2666 PQ) \u2666 QR \u223c WP \u2666 (PQ \u2666 QR)\n \u2666-assoc = sim-comp-assoc (Dual-spec _) (Dual-spec _)\n\n\n\u223c-\u00f8 : \u2200 {P}{s s' : Sim end P} \u2192 s \u223c s' \u2192 s \u2261 s'\n\u223c-\u00f8 s\u223cs' = s\u223cs' end end\n\nmodule _\n (funExtD : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n (funExtS : \u2200 {a}{b}{A : \u2605_ a}{B : ..(_ : A) \u2192 \u2605_ b}{f g : ..(x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g)\n where\n\n sim-!! : \u2200 {P Q}(PQ : Sim P Q) \u2192 PQ \u2261 !\u02e2 (!\u02e2 PQ)\n sim-!! (left (recvD x)) = cong (left \u2218 recvD) (funExtD \u03bb m \u2192 sim-!! (x m))\n sim-!! (left (recvS x)) = cong (left \u2218 recvS) (funExtS \u03bb m \u2192 sim-!! (x m))\n sim-!! (left (sendD x x\u2081)) = cong (left \u2218 sendD x) (sim-!! x\u2081)\n sim-!! (left (sendS x x\u2081)) = cong (left \u2218 sendS x) (sim-!! x\u2081)\n sim-!! (right (recvD x)) = cong (right \u2218 recvD) (funExtD \u03bb m \u2192 sim-!! (x m))\n sim-!! (right (recvS x)) = cong (right \u2218 recvS) (funExtS \u03bb m \u2192 sim-!! (x m))\n sim-!! (right (sendD x x\u2081)) = cong (right \u2218 sendD x) (sim-!! x\u2081)\n sim-!! (right (sendS x x\u2081)) = cong (right \u2218 sendS x) (sim-!! x\u2081)\n sim-!! end = refl\n\n sim-comp-!-end : \u2200 {Q Q' R}(Q-Q' : Dual Q Q')(\u00b7Q : Sim end Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 \u00b7Q) \u2261 !\u02e2 (sim-comp Q-Q' \u00b7Q Q'R)\n sim-comp-!-end Q-Q' (left ()) QR\n sim-comp-!-end end (right ()) (left x\u2081)\n sim-comp-!-end (\u03a0\u03a3'S x\u2081) (right (recvS x)) (left (sendS x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a0\u03a3'D x\u2081) (right (recvD x)) (left (sendD x\u2082 x\u2083)) = sim-comp-!-end (x\u2081 x\u2082) (x x\u2082) x\u2083\n sim-comp-!-end (\u03a3\u03a0'S x) (right (sendS x\u2081 x\u2082)) (left (recvS x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end (\u03a3\u03a0'D x) (right (sendD x\u2081 x\u2082)) (left (recvD x\u2083)) = sim-comp-!-end (x x\u2081) x\u2082 (x\u2083 x\u2081)\n sim-comp-!-end Q-Q' (right x) (right (recvD x\u2081))\n = cong (left \u2218 recvD) (funExtD (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (right x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (right x) (right (recvS x\u2081))\n = cong (left \u2218 recvS) (funExtS (\u03bb x\u2082 \u2192 sim-comp-!-end Q-Q' (right x) (x\u2081 x\u2082)))\n sim-comp-!-end Q-Q' (right x) (right (sendD x\u2081 x\u2082))\n = cong (left \u2218 sendD x\u2081) (sim-comp-!-end Q-Q' (right x) x\u2082)\n sim-comp-!-end Q-Q' (right x) (right (sendS x\u2081 x\u2082))\n = cong (left \u2218 sendS x\u2081) (sim-comp-!-end Q-Q' (right x) x\u2082)\n sim-comp-!-end end (right x) end = refl\n sim-comp-!-end end end QR = {!!}\n\n open \u2261-Reasoning\n module _ {P Q}{s s' : Sim P Q} where\n !\u02e2-cong : s \u223c s' \u2192 !\u02e2 s \u223c !\u02e2 s'\n !\u02e2-cong s\u223cs' Q'-Q \u00f8Q'\n = sim-comp Q'-Q \u00f8Q' (!\u02e2 s)\n \u2261\u27e8 {!!} \u27e9\n sim-comp Q'-Q \u00f8Q' (!\u02e2 (sim-comp (Dual-spec Q) s (sim-id _)))\n \u2261\u27e8 {!!} \u27e9\n sim-comp Q'-Q \u00f8Q' (!\u02e2 s')\n \u220e\n\n postulate\n sim-comp-assoc-end' : \u2200 {P Q Q' R R'}(Q-Q' : Dual Q Q')(R-R' : Dual R R')\n (PQ : Sim P Q)(QR : Sim Q' R )(R\u00f8 : Sim R' end)\n \u2192 sim-comp R-R' (sim-comp Q-Q' PQ QR) R\u00f8\n \u2261 sim-comp Q-Q' PQ (sim-comp R-R' QR R\u00f8)\n\n\n sim-comp-! : \u2200 {P Q Q' R}(Q-Q' : Dual Q Q')(PQ : Sim P Q)(Q'R : Sim Q' R)\n \u2192 sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ) \u223c !\u02e2 (sim-comp Q-Q' PQ Q'R)\n sim-comp-! Q-Q' PQ Q'R {R'} R'-R \u00f8R'\n = sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))\n \u2261\u27e8 sim-!! (sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2( !\u02e2 ((sim-comp R'-R \u00f8R' (sim-comp (Dual-sym Q-Q') (!\u02e2 Q'R) (!\u02e2 PQ)))))\n \u2261\u27e8 cong (!\u02e2 \u2218 !\u02e2) (sym (sim-comp-assoc-end funExtD funExtS R'-R (Dual-sym Q-Q') \u00f8R' (!\u02e2 Q'R) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (!\u02e2\n (sim-comp (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ)))\n \u2261\u27e8 cong !\u02e2 (sym (sim-comp-!-end (Dual-sym Q-Q') (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)) (!\u02e2 PQ))) \u27e9\n !\u02e2\n (sim-comp (Dual-sym (Dual-sym Q-Q')) (!\u02e2 (!\u02e2 PQ))\n (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong\u2082 (\u03bb X Y \u2192 !\u02e2 (sim-comp X Y (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R)))))\n (sym (Dual-sym-sym funExtD funExtS Q-Q')) (sym (sim-!! PQ)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 Q'R))))\n \u2261\u27e8 cong (!\u02e2 \u2218 sim-comp Q-Q' PQ) (sym (sim-comp-!-end R'-R \u00f8R' (!\u02e2 Q'R))) \u27e9\n !\u02e2\n (sim-comp Q-Q' PQ\n (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 Q'R)) (!\u02e2 \u00f8R')))\n \u2261\u27e8 cong\n (\u03bb X \u2192 !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))))\n (sym (sim-!! Q'R)) \u27e9\n !\u02e2 (sim-comp Q-Q' PQ (sim-comp (Dual-sym R'-R) Q'R (!\u02e2 \u00f8R')))\n -- \u2261\u27e8 cong !\u02e2 (sym (sim-comp-assoc-end' Q-Q' (Dual-sym R'-R) PQ Q'R (!\u02e2 \u00f8R'))) \u27e9\n \u2261\u27e8 \u223c-\u00f8 {!!}\u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (sim-comp Q-Q' PQ Q'R) (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong (\u03bb X \u2192 !\u02e2 (sim-comp (Dual-sym R'-R) X (!\u02e2 \u00f8R'))) (sim-!! (sim-comp Q-Q' PQ Q'R)) \u27e9\n !\u02e2 (sim-comp (Dual-sym R'-R) (!\u02e2 (!\u02e2 (sim-comp Q-Q' PQ Q'R)))\n (!\u02e2 \u00f8R'))\n \u2261\u27e8 cong !\u02e2 (sim-comp-!-end R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))) \u27e9\n !\u02e2 (!\u02e2 (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))))\n \u2261\u27e8 sym (sim-!! (sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R)))) \u27e9\n sim-comp R'-R \u00f8R' (!\u02e2 (sim-comp Q-Q' PQ Q'R))\n \u220e\n\n \u2666-! : \u2200 {P Q R}(PQ : Sim P Q)(QR : Sim (dual Q) R)\n \u2192 !\u02e2 (PQ \u2666 QR) \u223c (!\u02e2 QR) \u2666 (!\u02e2 {!PQ!})\n \u2666-! = {!!}\n{-\n\nunit-sim : \u2200 {P} \u2192 Process \ud835\udfd9 P \u2192 Sim end P\nunit-sim (do (send m x)) = right (send m (unit-sim x))\nunit-sim (do (recv x)) = right (recv (\u03bb m \u2192 unit-sim (x m)))\nunit-sim (end x) = end\n\n{-\ntoEl : \u2200 {P A} \u2192 Process A P \u2192 El P (const A)\ntoEl (end x) = x\ntoEl (do (recv f)) = \u03bb x \u2192 toEl (f x)\ntoEl (do (send m x)) = m , toEl x\n-}\n\nidP : \u2200 {A} \u2192 Process A (\u03a0' A (const end))\nidP = do (recv end)\n\ndual\u00b2 : \u2200 {P A} \u2192 Process A P \u2192 Process A (dual (dual P))\ndual\u00b2 (end x) = end x\ndual\u00b2 (do (recv x)) = do (recv (\u03bb m \u2192 dual\u00b2 (x m)))\ndual\u00b2 (do (send m x)) = do (send m (dual\u00b2 x))\n\napply-sim : \u2200 {P Q} \u2192 Sim P Q \u2192 P \u27f9\u1d3e Q\napply-sim (left (send m x)) (do (recv x\u2081)) = apply-sim x (x\u2081 m)\napply-sim (left (recv x)) (do (send m x\u2081)) = apply-sim (x m) x\u2081\napply-sim (right (send m x)) P\u2082 = do (send m (apply-sim x P\u2082))\napply-sim (right (recv x)) P\u2082 = do (recv (\u03bb m \u2192 apply-sim (x m) P\u2082))\napply-sim end P = P\n\napply-sim' : \u2200 {P Q} \u2192 Sim P Q \u2192 Q \u27f9\u1d3e P -- \u2200 {A} \u2192 Process A Q \u2192 Process A (dual P)\napply-sim' PQ P = apply-sim (sim-sym PQ) P\n\napply : \u2200 {P Q A} \u2192 P \u27f9 Q \u2192 Process A P \u2192 Process A Q\napply PQ P = apply-sim PQ (dual\u00b2 P)\n\nmodule _ (funExt : \u2200 {a}{b}{A : \u2605_ a}{B : A \u2192 \u2605_ b}{f g : (x : A) \u2192 B x} \u2192 (\u2200 x \u2192 f x \u2261 g x) \u2192 f \u2261 g) where\n apply-comp : \u2200 {P Q R A}(PQ : Sim P Q)(QR : Sim (dual Q) R)(p : Process A (dual P)) \u2192 apply-sim (sim-comp PQ QR) p \u2261 apply QR (apply-sim PQ p)\n apply-comp (left (send m x)) QR (do (recv x\u2081)) = apply-comp x QR (x\u2081 m)\n apply-comp (left (recv x)) QR (do (send m x\u2081)) = apply-comp (x m) QR x\u2081\n apply-comp (right (send m x)) (left (recv x\u2081)) p = apply-comp x (x\u2081 m) p\n apply-comp (right (send m x)) (right (recv x\u2081)) p = cong (\u03bb X \u2192 do (recv X))\n (funExt (\u03bb m' \u2192 apply-comp (right (send m x)) (x\u2081 m') p))\n apply-comp (right (send m x)) (right (send m\u2081 x\u2081)) p\n rewrite apply-comp (right (send m x)) x\u2081 p = refl\n apply-comp (right (recv x)) (left (send m x\u2081)) p = apply-comp (x m) x\u2081 p\n apply-comp (right (recv x)) (right (send m x\u2081)) p\n rewrite apply-comp (right (recv x)) x\u2081 p = refl\n apply-comp (right (recv x)) (right (recv x\u2081)) p = cong (\u03bb X \u2192 do (recv X))\n (funExt (\u03bb m \u2192 apply-comp (right (recv x)) (x\u2081 m) p))\n apply-comp end QR (do ())\n apply-comp end QR (end x) = refl\n\n{-\n_>>=P_ : \u2200 {A B P}{Q : Tele P \u2192 Proto} \u2192 Process A P \u2192 ((p : Tele P) \u2192 A \u2192 Process B (Q p)) \u2192 Process B (P >>\u2261 Q)\nsend m p >>=P k = send m (p >>=P (\u03bb p\u2081 \u2192 k (m , p\u2081)))\nrecv x >>=P k = recv (\u03bb m \u2192 x m >>=P (\u03bb p \u2192 k (m , p)))\nend x >>=P k = k 0\u2081 x\n\n\n {-\nmodule _ where\n bind-com : (P : Proto)(Q : Tele P \u2192 Proto)(R : Tele (dual P) \u2192 Proto)\n (X : Tele (P >>\u2261 Q) \u2192 \u2605)(Y : Tele (dual P >>\u2261 R) \u2192 \u2605)\n \u2192 El (P >>\u2261 Q) X \u2192 El (dual P >>\u2261 R) Y \u2192 ? \u00d7 ?\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"1b5c3931a31ac1d019f7a68f56a036201f92a46e","subject":"Fix mixed-if.","message":"Fix mixed-if.\n\nOld-commit-hash: 18ad0d9a08607bece81abf4bedc81a7763b56b9a\n","repos":"inc-lc\/ilc-agda","old_file":"lambda.agda","new_file":"lambda.agda","new_contents":"module lambda where\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types public\nopen import Syntactic.Contexts Type public\n\nopen import Denotational.Notation\nopen import Denotational.Values public\nopen import Denotational.Environments Type \u27e6_\u27e7Type public\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n true false : \u2200 {\u0393} \u2192 Term \u0393 bool\n if : \u2200 {\u0393 \u03c4} \u2192 (t\u2081 : Term \u0393 bool) (t\u2082 t\u2083 : Term \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\n\u27e6 true \u27e7Term \u03c1 = true\n\u27e6 false \u27e7Term \u03c1 = false\n\u27e6 if t\u2081 t\u2082 t\u2083 \u27e7Term \u03c1 with \u27e6 t\u2081 \u27e7Term \u03c1\n... | true = \u27e6 t\u2082 \u27e7Term \u03c1\n... | false = \u27e6 t\u2083 \u27e7Term \u03c1\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n-- NATURAL SEMANTICS\n\n-- Syntax\n\ndata Env : Context \u2192 Set\ndata Val : Type \u2192 Set\n\ndata Val where\n \u27e8abs_,_\u27e9 : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) (\u03c1 : Env \u0393) \u2192 Val (\u03c4\u2081 \u21d2 \u03c4\u2082)\n true : Val bool\n false : Val bool\n\ndata Env where\n \u2205 : Env \u2205\n _\u2022_ : \u2200 {\u0393 \u03c4} \u2192 Val \u03c4 \u2192 Env \u0393 \u2192 Env (\u03c4 \u2022 \u0393)\n\n-- Lookup\n\ninfixr 8 _\u22a2_\u2193_ _\u22a2_\u21a6_\n\ndata _\u22a2_\u21a6_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Var \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n this : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {v : Val \u03c4} \u2192\n v \u2022 \u03c1 \u22a2 this \u21a6 v\n that : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 x} {\u03c1 : Env \u0393} {v\u2081 : Val \u03c4\u2081} {v\u2082 : Val \u03c4\u2082} \u2192\n \u03c1 \u22a2 x \u21a6 v\u2082 \u2192\n v\u2081 \u2022 \u03c1 \u22a2 that x \u21a6 v\u2082\n\n-- Reduction\n\ndata _\u22a2_\u2193_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Term \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c1} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n \u03c1 \u22a2 abs t \u2193 \u27e8abs t , \u03c1 \u27e9\n app : \u2200 {\u0393 \u0393\u2032 \u03c4\u2081 \u03c4\u2082 \u03c1 \u03c1\u2032 v\u2082 v\u2032} {t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2082 : Term \u0393 \u03c4\u2081} {t\u2032 : Term (\u03c4\u2081 \u2022 \u0393\u2032) \u03c4\u2082} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 \u27e8abs t\u2032 , \u03c1\u2032 \u27e9 \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n v\u2082 \u2022 \u03c1\u2032 \u22a2 t\u2032 \u2193 v\u2032 \u2192\n \u03c1 \u22a2 app t\u2081 t\u2082 \u2193 v\u2032\n var : \u2200 {\u0393 \u03c4 x} {\u03c1 : Env \u0393} {v : Val \u03c4}\u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u03c1 \u22a2 var x \u2193 v\n if-true : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {t\u2081 t\u2082 t\u2083} {v\u2082 : Val \u03c4} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 true \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n \u03c1 \u22a2 if t\u2081 t\u2082 t\u2083 \u2193 v\u2082\n if-false : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {t\u2081 t\u2082 t\u2083} {v\u2083 : Val \u03c4} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 false \u2192\n \u03c1 \u22a2 t\u2083 \u2193 v\u2083 \u2192\n \u03c1 \u22a2 if t\u2081 t\u2082 t\u2083 \u2193 v\u2083\n\n-- SOUNDNESS of natural semantics\n\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 Env \u0393 \u2192 \u27e6 \u0393 \u27e7\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 \u27e8abs t , \u03c1 \u27e9 \u27e7Val = \u03bb v \u2192 \u27e6 t \u27e7 (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\u27e6 true \u27e7Val = true\n\u27e6 false \u27e7Val = false\n\nmeaningOfEnv : \u2200 {\u0393} \u2192 Meaning (Env \u0393)\nmeaningOfEnv = meaning \u27e6_\u27e7Env\n\nmeaningOfVal : \u2200 {\u03c4} \u2192 Meaning (Val \u03c4)\nmeaningOfVal = meaning \u27e6_\u27e7Val\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) = trans (cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082)) (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n\u2193-sound (if-true \u2193\u2081 \u2193\u2082) rewrite \u2193-sound \u2193\u2081 = \u2193-sound \u2193\u2082\n\u2193-sound (if-false \u2193\u2081 \u2193\u2083) rewrite \u2193-sound \u2193\u2081 = \u2193-sound \u2193\u2083\n\n-- WEAKENING\n\n-- Weaken a term to a super context\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken \u227c\u2081 (abs t) = abs (weaken (keep _ \u2022 \u227c\u2081) t)\nweaken \u227c\u2081 (app t\u2081 t\u2082) = app (weaken \u227c\u2081 t\u2081) (weaken \u227c\u2081 t\u2082)\nweaken \u227c\u2081 (var x) = var (lift \u227c\u2081 x)\nweaken \u227c\u2081 true = true\nweaken \u227c\u2081 false = false\nweaken \u227c\u2081 (if e\u2081 e\u2082 e\u2083) = if (weaken \u227c\u2081 e\u2081) (weaken \u227c\u2081 e\u2082) (weaken \u227c\u2081 e\u2083)\n\n-- SYMBOLIC EXECUTION\n--\n-- Naming Convention:\n-- \u0393 \u27ea_\u27ebX is like \u27e6_\u27e7X but for symbolic execution in context \u0393.\n\n_\u27ea_\u27ebType : Context \u2192 Type \u2192 Set\n\u0393\u2081 \u27ea \u03c4\u2081 \u21d2 \u03c4\u2082 \u27ebType = \u2200 {\u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2082 \u27ea \u03c4\u2081 \u27ebType \u2192 \u0393\u2082 \u27ea \u03c4\u2082 \u27ebType\n\u0393\u2081 \u27ea bool \u27ebType = Term \u0393\u2081 bool\n\nmodule _ (\u0393 : Context) where\n import Denotational.Environments\n module SymEnv = Denotational.Environments Type (\u03bb \u03c4 \u2192 \u0393 \u27ea \u03c4 \u27ebType)\n\n open SymEnv public using ()\n renaming (\u27e6_\u27e7Context to _\u27ea_\u27ebContext; \u27e6_\u27e7Var to _\u27ea_\u27ebVar_)\n\nliftVal : \u2200 {\u03c4 \u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u27ea \u03c4 \u27ebType \u2192 \u0393\u2082 \u27ea \u03c4 \u27ebType\nliftVal {\u03c4\u2081 \u21d2 \u03c4\u2082} \u227c\u2081 v\u2081 = \u03bb \u227c\u2082 v\u2082 \u2192 v\u2081 (\u227c-trans \u227c\u2081 \u227c\u2082) v\u2082\nliftVal {bool} \u227c\u2081 v = weaken \u227c\u2081 v\n\nliftEnv : \u2200 {\u0393 \u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u27ea \u0393 \u27ebContext \u2192 \u0393\u2082 \u27ea \u0393 \u27ebContext\nliftEnv {\u2205} \u227c\u2081 \u2205 = SymEnv.\u2205\nliftEnv {\u03c4 \u2022 \u0393} \u227c\u2081 (v \u2022 \u03c1) = liftVal \u227c\u2081 v SymEnv.\u2022 liftEnv \u227c\u2081 \u03c1\n\nmixed-if : \u2200 {\u0393\u2081} \u03c4 \u2192 (t\u2081 : Term \u0393\u2081 bool) (v\u2082 v\u2083 : \u0393\u2081 \u27ea \u03c4 \u27ebType) \u2192 \u0393\u2081 \u27ea \u03c4 \u27ebType\nmixed-if (\u03c4\u2081 \u21d2 \u03c4\u2082) t\u2081 v\u2082 v\u2083 = \u03bb \u227c\u2081 v \u2192 mixed-if \u03c4\u2082 (weaken \u227c\u2081 t\u2081) (v\u2082 \u227c\u2081 v) (v\u2083 \u227c\u2081 v)\nmixed-if bool t\u2081 t\u2082 t\u2083 = if t\u2081 t\u2082 t\u2083\n\n_\u27ea_\u27ebTerm_ : \u2200 \u0393\u2081 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u0393\u2081 \u27ea \u0393 \u27ebContext \u2192 \u0393\u2081 \u27ea \u03c4 \u27ebType\n\u0393\u2081 \u27ea abs t \u27ebTerm \u03c1 = \u03bb {\u0393\u2082} \u227c\u2081 v \u2192 \u0393\u2082 \u27ea t \u27ebTerm (v SymEnv.\u2022 liftEnv \u227c\u2081 \u03c1)\n\u0393\u2081 \u27ea app t\u2081 t\u2082 \u27ebTerm \u03c1 = (\u0393\u2081 \u27ea t\u2081 \u27ebTerm \u03c1) \u227c-refl (\u0393\u2081 \u27ea t\u2082 \u27ebTerm \u03c1)\n\u0393\u2081 \u27ea var x \u27ebTerm \u03c1 = \u0393\u2081 \u27ea x \u27ebVar \u03c1\n\u0393\u2081 \u27ea true \u27ebTerm \u03c1 = true\n\u0393\u2081 \u27ea false \u27ebTerm \u03c1 = false\n\u0393\u2081 \u27ea if t\u2081 t\u2082 t\u2083 \u27ebTerm \u03c1 = mixed-if _ (\u0393\u2081 \u27ea t\u2081 \u27ebTerm \u03c1) (\u0393\u2081 \u27ea t\u2082 \u27ebTerm \u03c1) (\u0393\u2081 \u27ea t\u2082 \u27ebTerm \u03c1)\n\n\u2193 : \u2200 {\u0393} \u03c4 \u2192 \u0393 \u27ea \u03c4 \u27ebType \u2192 Term \u0393 \u03c4\n\u2191 : \u2200 {\u0393} \u03c4 \u2192 Term \u0393 \u03c4 \u2192 \u0393 \u27ea \u03c4 \u27ebType\n\n\u2193 (\u03c4\u2081 \u21d2 \u03c4\u2082) v = abs (\u2193 \u03c4\u2082 (v (drop \u03c4\u2081 \u2022 \u227c-refl) (\u2191 \u03c4\u2081 (var this))))\n\u2193 bool v = v\n\n\u2191 (\u03c4\u2081 \u21d2 \u03c4\u2082) t = \u03bb \u227c\u2081 v \u2192 \u2191 \u03c4\u2082 (app (weaken \u227c\u2081 t) (\u2193 \u03c4\u2081 v))\n\u2191 bool t = t\n\n\u2191-Context : \u2200 {\u0393} \u2192 \u0393 \u27ea \u0393 \u27ebContext\n\u2191-Context {\u2205} = SymEnv.\u2205\n\u2191-Context {\u03c4 \u2022 \u0393} = \u2191 \u03c4 (var this) SymEnv.\u2022 liftEnv (drop \u03c4 \u2022 \u227c-refl) \u2191-Context\n\nnorm : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4\nnorm {\u0393} {\u03c4} t = \u2193 \u03c4 (\u0393 \u27ea t \u27ebTerm \u2191-Context)\n","old_contents":"module lambda where\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types public\nopen import Syntactic.Contexts Type public\n\nopen import Denotational.Notation\nopen import Denotational.Values public\nopen import Denotational.Environments Type \u27e6_\u27e7Type public\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n true false : \u2200 {\u0393} \u2192 Term \u0393 bool\n if : \u2200 {\u0393 \u03c4} \u2192 (t\u2081 : Term \u0393 bool) (t\u2082 t\u2083 : Term \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\n\u27e6 true \u27e7Term \u03c1 = true\n\u27e6 false \u27e7Term \u03c1 = false\n\u27e6 if t\u2081 t\u2082 t\u2083 \u27e7Term \u03c1 with \u27e6 t\u2081 \u27e7Term \u03c1\n... | true = \u27e6 t\u2082 \u27e7Term \u03c1\n... | false = \u27e6 t\u2083 \u27e7Term \u03c1\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n-- NATURAL SEMANTICS\n\n-- Syntax\n\ndata Env : Context \u2192 Set\ndata Val : Type \u2192 Set\n\ndata Val where\n \u27e8abs_,_\u27e9 : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) (\u03c1 : Env \u0393) \u2192 Val (\u03c4\u2081 \u21d2 \u03c4\u2082)\n true : Val bool\n false : Val bool\n\ndata Env where\n \u2205 : Env \u2205\n _\u2022_ : \u2200 {\u0393 \u03c4} \u2192 Val \u03c4 \u2192 Env \u0393 \u2192 Env (\u03c4 \u2022 \u0393)\n\n-- Lookup\n\ninfixr 8 _\u22a2_\u2193_ _\u22a2_\u21a6_\n\ndata _\u22a2_\u21a6_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Var \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n this : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {v : Val \u03c4} \u2192\n v \u2022 \u03c1 \u22a2 this \u21a6 v\n that : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 x} {\u03c1 : Env \u0393} {v\u2081 : Val \u03c4\u2081} {v\u2082 : Val \u03c4\u2082} \u2192\n \u03c1 \u22a2 x \u21a6 v\u2082 \u2192\n v\u2081 \u2022 \u03c1 \u22a2 that x \u21a6 v\u2082\n\n-- Reduction\n\ndata _\u22a2_\u2193_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Term \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c1} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n \u03c1 \u22a2 abs t \u2193 \u27e8abs t , \u03c1 \u27e9\n app : \u2200 {\u0393 \u0393\u2032 \u03c4\u2081 \u03c4\u2082 \u03c1 \u03c1\u2032 v\u2082 v\u2032} {t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2082 : Term \u0393 \u03c4\u2081} {t\u2032 : Term (\u03c4\u2081 \u2022 \u0393\u2032) \u03c4\u2082} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 \u27e8abs t\u2032 , \u03c1\u2032 \u27e9 \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n v\u2082 \u2022 \u03c1\u2032 \u22a2 t\u2032 \u2193 v\u2032 \u2192\n \u03c1 \u22a2 app t\u2081 t\u2082 \u2193 v\u2032\n var : \u2200 {\u0393 \u03c4 x} {\u03c1 : Env \u0393} {v : Val \u03c4}\u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u03c1 \u22a2 var x \u2193 v\n if-true : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {t\u2081 t\u2082 t\u2083} {v\u2082 : Val \u03c4} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 true \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n \u03c1 \u22a2 if t\u2081 t\u2082 t\u2083 \u2193 v\u2082\n if-false : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {t\u2081 t\u2082 t\u2083} {v\u2083 : Val \u03c4} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 false \u2192\n \u03c1 \u22a2 t\u2083 \u2193 v\u2083 \u2192\n \u03c1 \u22a2 if t\u2081 t\u2082 t\u2083 \u2193 v\u2083\n\n-- SOUNDNESS of natural semantics\n\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 Env \u0393 \u2192 \u27e6 \u0393 \u27e7\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 \u27e8abs t , \u03c1 \u27e9 \u27e7Val = \u03bb v \u2192 \u27e6 t \u27e7 (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\u27e6 true \u27e7Val = true\n\u27e6 false \u27e7Val = false\n\nmeaningOfEnv : \u2200 {\u0393} \u2192 Meaning (Env \u0393)\nmeaningOfEnv = meaning \u27e6_\u27e7Env\n\nmeaningOfVal : \u2200 {\u03c4} \u2192 Meaning (Val \u03c4)\nmeaningOfVal = meaning \u27e6_\u27e7Val\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) = trans (cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082)) (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n\u2193-sound (if-true \u2193\u2081 \u2193\u2082) rewrite \u2193-sound \u2193\u2081 = \u2193-sound \u2193\u2082\n\u2193-sound (if-false \u2193\u2081 \u2193\u2083) rewrite \u2193-sound \u2193\u2081 = \u2193-sound \u2193\u2083\n\n-- WEAKENING\n\n-- Weaken a term to a super context\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken \u227c\u2081 (abs t) = abs (weaken (keep _ \u2022 \u227c\u2081) t)\nweaken \u227c\u2081 (app t\u2081 t\u2082) = app (weaken \u227c\u2081 t\u2081) (weaken \u227c\u2081 t\u2082)\nweaken \u227c\u2081 (var x) = var (lift \u227c\u2081 x)\nweaken \u227c\u2081 true = true\nweaken \u227c\u2081 false = false\nweaken \u227c\u2081 (if e\u2081 e\u2082 e\u2083) = if (weaken \u227c\u2081 e\u2081) (weaken \u227c\u2081 e\u2082) (weaken \u227c\u2081 e\u2083)\n\n-- SYMBOLIC EXECUTION\n--\n-- Naming Convention:\n-- \u0393 \u27ea_\u27ebX is like \u27e6_\u27e7X but for symbolic execution in context \u0393.\n\n_\u27ea_\u27ebType : Context \u2192 Type \u2192 Set\n\u0393\u2081 \u27ea \u03c4\u2081 \u21d2 \u03c4\u2082 \u27ebType = \u2200 {\u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2082 \u27ea \u03c4\u2081 \u27ebType \u2192 \u0393\u2082 \u27ea \u03c4\u2082 \u27ebType\n\u0393\u2081 \u27ea bool \u27ebType = Term \u0393\u2081 bool\n\nmodule _ (\u0393 : Context) where\n import Denotational.Environments\n module SymEnv = Denotational.Environments Type (\u03bb \u03c4 \u2192 \u0393 \u27ea \u03c4 \u27ebType)\n\n open SymEnv public using ()\n renaming (\u27e6_\u27e7Context to _\u27ea_\u27ebContext; \u27e6_\u27e7Var to _\u27ea_\u27ebVar_)\n\nliftVal : \u2200 {\u03c4 \u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u27ea \u03c4 \u27ebType \u2192 \u0393\u2082 \u27ea \u03c4 \u27ebType\nliftVal {\u03c4\u2081 \u21d2 \u03c4\u2082} \u227c\u2081 v\u2081 = \u03bb \u227c\u2082 v\u2082 \u2192 v\u2081 (\u227c-trans \u227c\u2081 \u227c\u2082) v\u2082\nliftVal {bool} \u227c\u2081 v = weaken \u227c\u2081 v\n\nliftEnv : \u2200 {\u0393 \u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u27ea \u0393 \u27ebContext \u2192 \u0393\u2082 \u27ea \u0393 \u27ebContext\nliftEnv {\u2205} \u227c\u2081 \u2205 = SymEnv.\u2205\nliftEnv {\u03c4 \u2022 \u0393} \u227c\u2081 (v \u2022 \u03c1) = liftVal \u227c\u2081 v SymEnv.\u2022 liftEnv \u227c\u2081 \u03c1\n\nmixed-if : \u2200 {\u0393\u2081} \u03c4 \u2192 (t\u2081 : Term \u0393\u2081 bool) (v\u2082 v\u2083 : \u0393\u2081 \u27ea \u03c4 \u27ebType) \u2192 \u0393\u2081 \u27ea \u03c4 \u27ebType\nmixed-if (\u03c4\u2081 \u21d2 \u03c4\u2082) t\u2081 v\u2082 v\u2083 = \u03bb \u227c\u2081 v \u2192 mixed-if \u03c4\u2082 (weaken \u227c\u2081 t\u2081) (v\u2083 \u227c\u2081 v) (v\u2083 \u227c\u2081 v)\nmixed-if bool t\u2081 t\u2082 t\u2083 = if t\u2081 t\u2082 t\u2083\n\n_\u27ea_\u27ebTerm_ : \u2200 \u0393\u2081 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u0393\u2081 \u27ea \u0393 \u27ebContext \u2192 \u0393\u2081 \u27ea \u03c4 \u27ebType\n\u0393\u2081 \u27ea abs t \u27ebTerm \u03c1 = \u03bb {\u0393\u2082} \u227c\u2081 v \u2192 \u0393\u2082 \u27ea t \u27ebTerm (v SymEnv.\u2022 liftEnv \u227c\u2081 \u03c1)\n\u0393\u2081 \u27ea app t\u2081 t\u2082 \u27ebTerm \u03c1 = (\u0393\u2081 \u27ea t\u2081 \u27ebTerm \u03c1) \u227c-refl (\u0393\u2081 \u27ea t\u2082 \u27ebTerm \u03c1)\n\u0393\u2081 \u27ea var x \u27ebTerm \u03c1 = \u0393\u2081 \u27ea x \u27ebVar \u03c1\n\u0393\u2081 \u27ea true \u27ebTerm \u03c1 = true\n\u0393\u2081 \u27ea false \u27ebTerm \u03c1 = false\n\u0393\u2081 \u27ea if t\u2081 t\u2082 t\u2083 \u27ebTerm \u03c1 = mixed-if _ (\u0393\u2081 \u27ea t\u2081 \u27ebTerm \u03c1) (\u0393\u2081 \u27ea t\u2082 \u27ebTerm \u03c1) (\u0393\u2081 \u27ea t\u2082 \u27ebTerm \u03c1)\n\n\u2193 : \u2200 {\u0393} \u03c4 \u2192 \u0393 \u27ea \u03c4 \u27ebType \u2192 Term \u0393 \u03c4\n\u2191 : \u2200 {\u0393} \u03c4 \u2192 Term \u0393 \u03c4 \u2192 \u0393 \u27ea \u03c4 \u27ebType\n\n\u2193 (\u03c4\u2081 \u21d2 \u03c4\u2082) v = abs (\u2193 \u03c4\u2082 (v (drop \u03c4\u2081 \u2022 \u227c-refl) (\u2191 \u03c4\u2081 (var this))))\n\u2193 bool v = v\n\n\u2191 (\u03c4\u2081 \u21d2 \u03c4\u2082) t = \u03bb \u227c\u2081 v \u2192 \u2191 \u03c4\u2082 (app (weaken \u227c\u2081 t) (\u2193 \u03c4\u2081 v))\n\u2191 bool t = t\n\n\u2191-Context : \u2200 {\u0393} \u2192 \u0393 \u27ea \u0393 \u27ebContext\n\u2191-Context {\u2205} = SymEnv.\u2205\n\u2191-Context {\u03c4 \u2022 \u0393} = \u2191 \u03c4 (var this) SymEnv.\u2022 liftEnv (drop \u03c4 \u2022 \u227c-refl) \u2191-Context\n\nnorm : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4\nnorm {\u0393} {\u03c4} t = \u2193 \u03c4 (\u0393 \u27ea t \u27ebTerm \u2191-Context)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d50f72236e4009f8db49bd947117cb58b3cbfccb","subject":"Removed Agda with constructor from the proof of streamLength.","message":"Removed Agda with constructor from the proof of streamLength.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Data\/Stream\/PropertiesI.agda","new_file":"src\/fot\/FOTC\/Data\/Stream\/PropertiesI.agda","new_contents":"------------------------------------------------------------------------------\n-- Streams properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Stream.PropertiesI where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Base.List.PropertiesI\nopen import FOTC.Data.Conat\nopen import FOTC.Data.Conat.Equality.Type\nopen import FOTC.Data.Colist\nopen import FOTC.Data.Colist.PropertiesI\nopen import FOTC.Data.List\nopen import FOTC.Data.List.PropertiesI\nopen import FOTC.Data.Stream.Type\n\n-----------------------------------------------------------------------------\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Stream predicate is also a pre-fixed point of the functional\n-- StreamF, i.e.\n--\n-- StreamF Stream \u2264 Stream (see FOTC.Data.Stream.Type).\nStream-in : \u2200 {xs} \u2192\n \u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Stream xs' \u2192\n Stream xs\nStream-in h = Stream-coind A h' h\n where\n A : D \u2192 Set\n A xs = \u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Stream xs'\n\n h' : \u2200 {xs} \u2192 A xs \u2192 \u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs'\n h' (x' , xs' , prf , Sxs') = x' , xs' , prf , Stream-out Sxs'\n\nfoo : \u2200 {xs} \u2192 Stream xs \u2192 \u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Stream xs'\nfoo Sxs = Stream-out Sxs\n\n\u2237-Stream : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\n\u2237-Stream {x} {xs} h = \u2237-Stream-helper (Stream-out h)\n where\n \u2237-Stream-helper : \u2203[ x' ] \u2203[ xs' ] x \u2237 xs \u2261 x' \u2237 xs' \u2227 Stream xs' \u2192\n Stream xs\n \u2237-Stream-helper (x' , xs' , prf , Sxs') =\n subst Stream (sym (\u2227-proj\u2082 (\u2237-injective prf))) Sxs'\n\n-- Version using Agda with constructor.\n\u2237-Stream' : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\n\u2237-Stream' h with Stream-out h\n... | x' , xs' , prf , Sxs' =\n subst Stream (sym (\u2227-proj\u2082 (\u2237-injective prf))) Sxs'\n\nStream\u2192Colist : \u2200 {xs} \u2192 Stream xs \u2192 Colist xs\nStream\u2192Colist {xs} Sxs = Colist-coind A h\u2081 h\u2082\n where\n A : D \u2192 Set\n A ys = Stream ys\n\n h\u2081 : \u2200 {xs} \u2192 A xs \u2192 xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs')\n h\u2081 Axs with Stream-out Axs\n ... | x' , xs' , prf , Sxs' = inj\u2082 (x' , xs' , prf , Sxs')\n\n h\u2082 : A xs\n h\u2082 = Sxs\n\n-- Adapted from (Sander 1992, p. 59).\nstreamLength : \u2200 {xs} \u2192 Stream xs \u2192 length xs \u2248N \u221e\nstreamLength {xs} Sxs = \u2248N-coind R h\u2081 h\u2082\n where\n R : D \u2192 D \u2192 Set\n R m n = \u2203[ xs ] Stream xs \u2227 m \u2261 length xs \u2227 n \u2261 \u221e\n\n h\u2081 : \u2200 {m n} \u2192 R m n \u2192\n m \u2261 zero \u2227 n \u2261 zero\n \u2228 (\u2203[ m' ] \u2203[ n' ] m \u2261 succ\u2081 m' \u2227 n \u2261 succ\u2081 n' \u2227 R m' n')\n h\u2081 {m} {n} (xs , Sxs , m=lxs , n\u2261\u221e) = helper\u2081 (Stream-out Sxs)\n where\n helper\u2081 : (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Stream xs') \u2192\n m \u2261 zero \u2227 n \u2261 zero\n \u2228 (\u2203[ m' ] \u2203[ n' ] m \u2261 succ\u2081 m' \u2227 n \u2261 succ\u2081 n' \u2227 R m' n')\n helper\u2081 (x' , xs' , xs\u2261x'\u2237xs' , Sxs') =\n inj\u2082 (length xs'\n , \u221e\n , helper\u2082\n , trans n\u2261\u221e \u221e-eq\n , (xs' , Sxs' , refl , refl))\n where\n helper\u2082 : m \u2261 succ\u2081 (length xs')\n helper\u2082 = trans m=lxs (trans (lengthCong xs\u2261x'\u2237xs') (length-\u2237 x' xs'))\n\n h\u2082 : R (length xs) \u221e\n h\u2082 = xs , Sxs , refl , refl\n\n-- Adapted from (Sander 1992, p. 59). Version using Agda with\n-- constructor.\nstreamLength' : \u2200 {xs} \u2192 Stream xs \u2192 length xs \u2248N \u221e\nstreamLength' {xs} Sxs = \u2248N-coind R h\u2081 h\u2082\n where\n R : D \u2192 D \u2192 Set\n R m n = \u2203[ xs ] Stream xs \u2227 m \u2261 length xs \u2227 n \u2261 \u221e\n\n h\u2081 : \u2200 {m n} \u2192 R m n \u2192\n m \u2261 zero \u2227 n \u2261 zero\n \u2228 (\u2203[ m' ] \u2203[ n' ] m \u2261 succ\u2081 m' \u2227 n \u2261 succ\u2081 n' \u2227 R m' n')\n h\u2081 {m} (xs , Sxs , m=lxs , n\u2261\u221e) with Stream-out Sxs\n ... | x' , xs' , xs\u2261x'\u2237xs' , Sxs' =\n inj\u2082 (length xs' , \u221e , helper , trans n\u2261\u221e \u221e-eq , (xs' , Sxs' , refl , refl))\n where\n helper : m \u2261 succ\u2081 (length xs')\n helper = trans m=lxs (trans (lengthCong xs\u2261x'\u2237xs') (length-\u2237 x' xs'))\n\n h\u2082 : R (length xs) \u221e\n h\u2082 = xs , Sxs , refl , refl\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Sander, Herbert P. (1992). A Logic of Functional Programs with an\n-- Application to Concurrency. PhD thesis. Department of Computer\n-- Sciences: Chalmers University of Technology and University of\n-- Gothenburg.\n","old_contents":"------------------------------------------------------------------------------\n-- Streams properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Stream.PropertiesI where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Base.List.PropertiesI\nopen import FOTC.Data.Conat\nopen import FOTC.Data.Conat.Equality.Type\nopen import FOTC.Data.Colist\nopen import FOTC.Data.Colist.PropertiesI\nopen import FOTC.Data.List\nopen import FOTC.Data.List.PropertiesI\nopen import FOTC.Data.Stream.Type\n\n-----------------------------------------------------------------------------\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Stream predicate is also a pre-fixed point of the functional\n-- StreamF, i.e.\n--\n-- StreamF Stream \u2264 Stream (see FOTC.Data.Stream.Type).\nStream-in : \u2200 {xs} \u2192\n \u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Stream xs' \u2192\n Stream xs\nStream-in h = Stream-coind A h' h\n where\n A : D \u2192 Set\n A xs = \u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Stream xs'\n\n h' : \u2200 {xs} \u2192 A xs \u2192 \u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs'\n h' (x' , xs' , prf , Sxs') = x' , xs' , prf , Stream-out Sxs'\n\nfoo : \u2200 {xs} \u2192 Stream xs \u2192 \u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Stream xs'\nfoo Sxs = Stream-out Sxs\n\n\u2237-Stream : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\n\u2237-Stream {x} {xs} h = \u2237-Stream-helper (Stream-out h)\n where\n \u2237-Stream-helper : \u2203[ x' ] \u2203[ xs' ] x \u2237 xs \u2261 x' \u2237 xs' \u2227 Stream xs' \u2192\n Stream xs\n \u2237-Stream-helper (x' , xs' , prf , Sxs') =\n subst Stream (sym (\u2227-proj\u2082 (\u2237-injective prf))) Sxs'\n\n-- Version using Agda with constructor.\n\u2237-Stream' : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\n\u2237-Stream' h with Stream-out h\n... | x' , xs' , prf , Sxs' =\n subst Stream (sym (\u2227-proj\u2082 (\u2237-injective prf))) Sxs'\n\nStream\u2192Colist : \u2200 {xs} \u2192 Stream xs \u2192 Colist xs\nStream\u2192Colist {xs} Sxs = Colist-coind A h\u2081 h\u2082\n where\n A : D \u2192 Set\n A ys = Stream ys\n\n h\u2081 : \u2200 {xs} \u2192 A xs \u2192 xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs')\n h\u2081 Axs with Stream-out Axs\n ... | x' , xs' , prf , Sxs' = inj\u2082 (x' , xs' , prf , Sxs')\n\n h\u2082 : A xs\n h\u2082 = Sxs\n\n-- Adapted from (Sander 1992, p. 59).\nstreamLength : \u2200 {xs} \u2192 Stream xs \u2192 length xs \u2248N \u221e\nstreamLength {xs} Sxs = \u2248N-coind R h\u2081 h\u2082\n where\n R : D \u2192 D \u2192 Set\n R m n = \u2203[ xs ] Stream xs \u2227 m \u2261 length xs \u2227 n \u2261 \u221e\n\n h\u2081 : \u2200 {m n} \u2192 R m n \u2192\n m \u2261 zero \u2227 n \u2261 zero\n \u2228 (\u2203[ m' ] \u2203[ n' ] m \u2261 succ\u2081 m' \u2227 n \u2261 succ\u2081 n' \u2227 R m' n')\n h\u2081 {m} (xs , Sxs , m=lxs , n\u2261\u221e) with Stream-out Sxs\n ... | x' , xs' , xs\u2261x'\u2237xs' , Sxs' =\n inj\u2082 (length xs' , \u221e , helper , trans n\u2261\u221e \u221e-eq , (xs' , Sxs' , refl , refl))\n where\n helper : m \u2261 succ\u2081 (length xs')\n helper = trans m=lxs (trans (lengthCong xs\u2261x'\u2237xs') (length-\u2237 x' xs'))\n\n h\u2082 : R (length xs) \u221e\n h\u2082 = xs , Sxs , refl , refl\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Sander, Herbert P. (1992). A Logic of Functional Programs with an\n-- Application to Concurrency. PhD thesis. Department of Computer\n-- Sciences: Chalmers University of Technology and University of\n-- Gothenburg.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"54593ec140f1f6fbdb3f258b1affc25608b8b890","subject":"Bits: Bij is now typed","message":"Bits: Bij is now typed\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative.NP\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nopen import Relation.Nullary\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties.NP\nimport Data.List.NP as L\nopen import Function.Bijection.SyntaxKit\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0b \u2237 1b \u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u2219_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u2219 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u2219 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u2219-comm : \u2200 {m} (x y : A m) \u2192 x \u2219 y \u2261 y \u2219 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u2219-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nmodule OperationSyntax where\n module BitBij = BoolBijection\n open BitBij public using (`id) renaming (BoolBij to BitBij; bool-bijKit to bitBijKit; `not to `not\u1d2e)\n open BijectionSyntax Bit BitBij public\n open BijectionSemantics bitBijKit public\n\n {-\n id \u03b5 id\n 0\u21941 swp-inners interchange\n not swap comm\n if0 first ...\n if1 second ...\n -}\n `not : \u2200 {n} \u2192 Bij (1 + n)\n `not = BitBij.`not `\u2237 const `id\n\n `xor : \u2200 {n} \u2192 Bit \u2192 Bij (1 + n)\n `xor b = BitBij.`xor b `\u2237 const `id\n\n `if : \u2200 {n} \u2192 Bij n \u2192 Bij n \u2192 Bij (1 + n)\n `if f g = BitBij.`id `\u2237 cond f g\n\n `if0 : \u2200 {n} \u2192 Bij n \u2192 Bij (1 + n)\n `if0 f = `if `id f\n\n `if1 : \u2200 {n} \u2192 Bij n \u2192 Bij (1 + n)\n `if1 f = `if f `id\n\n -- law: `if0 f `\u204f `if1 g \u2261 `if1 g `; `if0 f\n\n on-firsts : \u2200 {n} \u2192 Bij (1 + n) \u2192 Bij (2 + n)\n on-firsts f = `0\u21941 `\u204f `if0 f `\u204f `0\u21941\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 right swap\n -- (a \u2219 d) \u2219 (c \u2219 b)\n swp-seconds : \u2200 {n} \u2192 Bij (2 + n)\n swp-seconds = `if1 `not `\u204f `0\u21941 `\u204f `if1 `not\n\n on-extremes : \u2200 {n} \u2192 Bij (1 + n) \u2192 Bij (2 + n)\n -- on-extremes f = swp-seconds `\u204f `if0 f `\u204f swp-seconds\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap \u2261 if1 not\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange \u2261 0\u21941\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 left f \u2261 if0 f\n -- (A \u2219 D) \u2219 (b \u2219 c)\n -- where A \u2219 D = f (a \u2219 d)\n -- \u2261 interchange \u2261 0\u21941\n -- (A \u2219 b) \u2219 (D \u2219 c)\n -- \u2261 right swap \u2261 if1 not\n -- (A \u2219 b) \u2219 (c \u2219 D)\n on-extremes f = `if1 `not `\u204f `0\u21941 `\u204f `if0 f `\u204f `0\u21941 `\u204f `if1 `not\n\n map-inner : \u2200 {n} \u2192 Bij (1 + n) \u2192 Bij (2 + n)\n map-inner f = `if1 `not `\u204f `0\u21941 `\u204f `if1 f `\u204f `0\u21941 `\u204f `if1 `not\n\n map-outer : \u2200 {n} \u2192 Bij n \u2192 Bij n \u2192 Bij (1 + n)\n map-outer f g = `if g f\n\n 0\u21941\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bij (1 + n)\n 0\u21941\u2237 [] = `not\n 0\u21941\u2237 (true {-1-} \u2237 p) = on-extremes (0\u21941\u2237 p)\n 0\u21941\u2237 (false{-0-} \u2237 p) = on-firsts (0\u21941\u2237 p)\n\n 0\u2194_ : \u2200 {n} \u2192 Bits n \u2192 Bij n\n 0\u2194 [] = `id\n 0\u2194 (false{-0-} \u2237 p) = `if0 (0\u2194 p)\n 0\u2194 (true{-1-} \u2237 p) = 0\u21941\u2237 p\n\n \u27e80\u2194_\u27e9-sem : \u2200 {n} (p : Bits n) \u2192 Bits n \u2192 Bits n\n \u27e80\u2194 p \u27e9-sem xs = if 0\u207f == xs then p else if p == xs then 0\u207f else xs\n\n if\u2237 : \u2200 {n} a x (xs ys : Bits n) \u2192 (if a then (x \u2237 xs) else (x \u2237 ys)) \u2261 x \u2237 (if a then xs else ys)\n if\u2237 true x xs ys = refl\n if\u2237 false x xs ys = refl\n\n if-not\u2237 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (false \u2237 xs) else (true \u2237 ys)) \u2261 (not a) \u2237 (if a then xs else ys)\n if-not\u2237 true xs ys = refl\n if-not\u2237 false xs ys = refl\n\n if\u2237\u2032 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (true \u2237 xs) else (false \u2237 ys)) \u2261 a \u2237 (if a then xs else ys)\n if\u2237\u2032 true xs ys = refl\n if\u2237\u2032 false xs ys = refl\n\n \u27e80\u21941\u2237_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u21941\u2237 p \u2219 xs \u2261 \u27e80\u2194 (1\u2237 p) \u27e9-sem xs\n \u27e80\u21941\u2237_\u27e9-spec [] (true \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec [] (false \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 true \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 false \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n\n \u27e80\u2194_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u2194 p \u2219 xs \u2261 \u27e80\u2194 p \u27e9-sem xs\n \u27e80\u2194_\u27e9-spec [] [] = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (true \u2237 xs) = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (false \u2237 xs)\n rewrite \u27e80\u2194 ps \u27e9-spec xs\n | if\u2237 (ps == xs) 0b 0\u207f xs\n | if\u2237 (0\u207f == xs) 0b ps (if ps == xs then 0\u207f else xs)\n = refl\n \u27e80\u2194_\u27e9-spec (true \u2237 p) xs = \u27e80\u21941\u2237 p \u27e9-spec xs\n\n private\n module P where\n open PermutationSyntax public\n open PermutationSemantics public\n open P using (Perm; `id; `0\u21941; _`\u204f_)\n\n `\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij (1 + n)\n `\u27e80\u21941+ zero \u27e9 = `0\u21941\n `\u27e80\u21941+ suc i \u27e9 = `0\u21941 `\u204f `tl `\u27e80\u21941+ i \u27e9 `\u204f `0\u21941\n\n `\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n `\u27e80\u21941+ zero \u27e9-spec xs = refl\n `\u27e80\u21941+ suc i \u27e9-spec (x \u2237 _ \u2237 xs) rewrite `\u27e80\u21941+ i \u27e9-spec (x \u2237 xs) = refl\n\n `\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij n\n `\u27e80\u2194 zero \u27e9 = `id\n `\u27e80\u2194 suc i \u27e9 = `\u27e80\u21941+ i \u27e9\n\n `\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n `\u27e80\u2194 zero \u27e9-spec xs = refl\n `\u27e80\u2194 suc i \u27e9-spec xs = `\u27e80\u21941+ i \u27e9-spec xs\n\n {-\n `\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Bij n\n `\u27e8 zero \u2194 j \u27e9 = `\u27e80\u2194 j \u27e9\n `\u27e8 i \u2194 zero \u27e9 = `\u27e80\u2194 i \u27e9\n `\u27e8 suc i \u2194 suc j \u27e9 = `tl `\u27e8 i \u2194 j \u27e9\n\n `\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) xs \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n `\u27e8_\u2194_\u27e9-spec zero j xs = `\u27e80\u2194 j \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) zero xs = `\u27e80\u21941+ i \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) (suc j) (x \u2237 xs) rewrite `\u27e8 i \u2194 j \u27e9-spec xs = refl\n -}\n\n `xor-head : \u2200 {n} \u2192 Bit \u2192 Bij (1 + n)\n `xor-head b = if b then `not else `id\n\n `xor-head-spec : \u2200 b {n} x (xs : Bits n) \u2192 `xor-head b \u2219 (x \u2237 xs) \u2261 (b xor x) \u2237 xs\n `xor-head-spec true x xs = refl\n `xor-head-spec false x xs = refl\n\n `\u27e8_\u2295\u27e9 : \u2200 {n} \u2192 Bits n \u2192 Bij n\n `\u27e8 [] \u2295\u27e9 = `id\n `\u27e8 b \u2237 xs \u2295\u27e9 = `xor-head b `\u204f `tl `\u27e8 xs \u2295\u27e9\n\n `\u27e8_\u2295\u27e9-spec : \u2200 {n} (xs ys : Bits n) \u2192 `\u27e8 xs \u2295\u27e9 \u2219 ys \u2261 xs \u2295 ys\n `\u27e8 [] \u2295\u27e9-spec [] = refl\n `\u27e8 true \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n `\u27e8 false \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) x \u2192 0\u21941 pad \u2295 0\u21941 x \u2261 0\u21941 (pad \u2295 x)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\nmodule PermutationSyntax-Props where\n open PermutationSyntax\n open PermutationSemantics\n -- open PermutationProperties\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) xs \u2192 0\u21941 pad \u2295 0\u21941 xs \u2261 0\u21941 (pad \u2295 xs)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\n -- TODO make use of \u229b-dist-\u2219\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 fs `id xs = refl\n \u2295-dist-\u2219 fs `0\u21941 xs = \u2295-dist-0\u21941 fs xs\n \u2295-dist-\u2219 (f \u2237 fs) (`tl \u03c0) (x \u2237 xs) rewrite \u2295-dist-\u2219 fs \u03c0 xs = refl\n \u2295-dist-\u2219 fs (\u03c0\u2080 `\u204f \u03c0\u2081) xs rewrite \u2295-dist-\u2219 (\u03c0\u2080 \u2219 fs) \u03c0\u2081 (\u03c0\u2080 \u2219 xs)\n | \u2295-dist-\u2219 fs \u03c0\u2080 xs = refl\n {-\n -- \u229b-dist-\u2219 : \u2200 {n a} {A : Set a} (fs : Vec (A \u2192 A) n) \u03c0 xs \u2192 \u03c0 \u2219 fs \u229b \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (fs \u229b xs)\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 pad \u03c0 xs = \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs\n \u2261\u27e8 refl \u27e9\n vmap _xor_ (\u03c0 \u2219 pad) \u229b \u03c0 \u2219 xs\n \u2261\u27e8 TODO \u27e9\n \u03c0 \u2219 vmap _xor_ pad \u229b \u03c0 \u2219 xs\n \u2261\u27e8 \u229b-dist-\u2219 _ (vmap _xor_ pad) \u03c0 xs \u27e9\n \u03c0 \u2219 (vmap _xor_ pad \u229b xs)\n \u2261\u27e8 refl \u27e9\n \u03c0 \u2219 (pad \u2295 xs)\n \u220e where open \u2261-Reasoning\n -- rans {!\u229b-dist-\u2219 (vmap _xor_ (op \u2219 pad)) op xs!} (\u229b-dist-\u2219 (vmap _xor_ pad) op xs)\n-}\nmodule SimpleSearch {a} {A : Set a} (_\u2219_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u2219_ public\n\n module SearchUnit \u03b5 (\u03b5\u2219\u03b5 : \u03b5 \u2219 \u03b5 \u2261 \u03b5) where\n search-const\u03b5\u2261\u03b5 : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-const\u03b5\u2261\u03b5 zero = refl\n search-const\u03b5\u2261\u03b5 (suc n) rewrite search-const\u03b5\u2261\u03b5 n = \u03b5\u2219\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n module SearchInterchange (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u2219 f\u2081 x) \u2261 search f\u2080 \u2219 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u2219-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e where open \u2261-Reasoning\n\n search-0\u21941 : \u2200 {n} (f : Bits n \u2192 A) \u2192 search {n} (f \u2218 0\u21941) \u2261 search {n} f\n search-0\u21941 {zero} _ = refl\n search-0\u21941 {suc zero} _ = refl\n search-0\u21941 {suc (suc n)} _ = \u2219-interchange _ _ _ _\n\n module Bij (\u2219-comm : Commutative _\u2261_ _\u2219_)\n (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n open SearchInterchange \u2219-interchange using (search-0\u21941)\n open OperationSyntax hiding (_\u2219_)\n search-bij : \u2200 {n} f (g : Bits n \u2192 A) \u2192 search (g \u2218 eval f) \u2261 search g\n search-bij `id _ = refl\n search-bij `0\u21941 f = search-0\u21941 f\n search-bij (f `\u204f g) h\n rewrite search-bij f (h \u2218 eval g)\n | search-bij g h\n = refl\n search-bij {suc n} (`id `\u2237 f) g\n rewrite search-bij (f 0b) (g \u2218 0\u2237_)\n | search-bij (f 1b) (g \u2218 1\u2237_)\n = refl\n search-bij {suc n} (`not\u1d2e `\u2237 f) g\n rewrite search-bij (f 1b) (g \u2218 0\u2237_)\n | search-bij (f 0b) (g \u2218 1\u2237_)\n = \u2219-comm _ _\n\nmodule Sum where\n open SimpleSearch _+_ using (module Comm; module SearchInterchange; module SearchUnit; module Bij)\n open SimpleSearch _+_ public using () renaming (search to sum; search-\u2257 to sum-\u2257; searchBit to sumBit;\n search-\u2257\u2082 to sum-\u2257\u2082;\n search-+ to sum-+)\n open Comm \u2115\u00b0.+-comm public renaming (search-comm to sum-comm)\n open SearchUnit 0 refl public renaming\n (search-const\u03b5\u2261\u03b5 to sum-const0\u22610)\n open SearchInterchange +-interchange public renaming (\n search-dist to sum-dist;\n search-searchBit to sum-sumBit;\n search-search to sum-sum;\n search-swap to sum-swap)\n open Bij \u2115\u00b0.+-comm +-interchange public renaming (search-bij to sum-bij)\n\n sum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\n sum-const zero _ = refl\n sum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nmodule Count where\n open Sum\n open OperationSyntax\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n #\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n -- #-ext\n #-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n #-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n #-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n #-bij : \u2200 {n} f (g : Bits n \u2192 Bit) \u2192 #\u27e8 g \u2218 eval f \u27e9 \u2261 #\u27e8 g \u27e9\n #-bij f g = sum-bij f (Bool.to\u2115 \u2218 g)\n\n #-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n #-\u2295 = #-comm\n\n #-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n #-const n b = sum-const n (Bool.to\u2115 b)\n\n #never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n #never\u22610 = sum-const0\u22610\n\n #always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n #always\u22612^ n = sum-const n 1\n\n #-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 Bit) \u2192 sum (\u03bb x \u2192 Bool.to\u2115 (f\u2080 x) + Bool.to\u2115 (f\u2081 x)) \u2261 #\u27e8 f\u2080 \u27e9 + #\u27e8 f\u2081 \u27e9\n #-dist f\u2080 f\u2081 = sum-dist (Bool.to\u2115 \u2218 f\u2080) (Bool.to\u2115 \u2218 f\u2081)\n\n #-+ : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n #\u27e8 f \u27e9 \u2261 sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9 )\n #-+ {m} {n} f = sum-+ {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-# : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9)\n \u2261 sum {n} (\u03bb ys \u2192 #\u27e8 (\u03bb (xs : Bits m) \u2192 f (xs ++ ys)) \u27e9)\n #-# {m} {n} f = sum-sum {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-swap : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192 #\u27e8 f \u2218 vswap n {m} \u27e9 \u2261 #\u27e8 f \u27e9\n #-swap {m} {n} f = sum-swap {m} {n} (Bool.to\u2115 \u2218 f)\n\n #\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n #\u27e8== [] \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n \u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n \u2257-cong-# f g f\u2257g = sum-\u2257 _ _ (cong Bool.to\u2115 \u2218 f\u2257g)\n\n -- #-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n -- #-+ f f0 f1 rewrite f0 | f1 = refl\n\n #-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-take-drop zero n f g with f []\n ... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n ... | false = #never\u22610 n\n #-take-drop (suc m) n f g\n rewrite \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x))\n | #-take-drop m n (f \u2218 0\u2237_) g\n | \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x))\n | #-take-drop m n (f \u2218 1\u2237_) g\n = sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9)\n\n #-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n #\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n #\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n #\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n ... | true | true | _ = s\u2264s z\u2264n\n ... | true | false | p = \u22a5-elim (p _)\n ... | false | _ | _ = z\u2264n\n #\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n #-\u2227-\u2228\u1d47 : \u2200 x y \u2192 Bool.to\u2115 (x \u2227 y) + Bool.to\u2115 (x \u2228 y) \u2261 Bool.to\u2115 x + Bool.to\u2115 y\n #-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (Bool.to\u2115 y) 1 = refl\n #-\u2227-\u2228\u1d47 false y = refl\n\n #-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n #-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #\u2228' {zero} f g with f []\n ... | true = s\u2264s z\u2264n\n ... | false = \u2115\u2264.refl\n #\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n #\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n #\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n #\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n #-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n #-bound zero f = Bool.to\u2115\u22641 (f [])\n #-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n #-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n #-\u2218vnot _ f = #-\u2295 1\u207f f\n\n #-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n #-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n #-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n #-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n #-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n #-not\u2218 zero f with f []\n ... | true = \u2261.refl\n ... | false = \u2261.refl\n #-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n #-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n #-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\nopen SimpleSearch public\nopen Sum public\nopen Count public\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192\n search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = search-\u2257 op _ _ (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2^ n) (toFin xs)\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2^ n + to\u2115 xs\n\nto\u2115-bound : \u2200 {n} (xs : Bits n) \u2192 to\u2115 xs < 2^ n \nto\u2115-bound [] = s\u2264s z\u2264n\nto\u2115-bound {suc n} (1b \u2237 xs) rewrite +-assoc-comm 1 (2^ n) (to\u2115 xs) = \u2115\u2264.refl {2^ n} +-mono to\u2115-bound xs\nto\u2115-bound {suc n} (0b \u2237 xs) = \u2264-steps (2^ n) (to\u2115-bound xs)\n\nto\u2115\u22642\u207f+ : \u2200 {n} (x : Bits n) {y} \u2192 to\u2115 {n} x \u2264 2^ n + y\nto\u2115\u22642\u207f+ {n} x {y} = \u2115\u2264.trans (\u2264-steps y (\u2264-pred (\u2264-steps 1 (to\u2115-bound x))))\n (\u2115\u2264.reflexive (\u2115\u00b0.+-comm y (2^ n)))\n\n2\u207f+\u2270to\u2115 : \u2200 {n x} (y : Bits n) \u2192 2^ n + x \u2270 to\u2115 {n} y\n2\u207f+\u2270to\u2115 {n} {x} y p = \u00acn+\u2264y_\n infixl 2 _===_by_\n infix 1 _qed\n\n chain>_ : (x : A) \u2192 x \u2243 x\n chain> x = prf (refl {x})\n\n _===_by_ : {x y : A} \u2192 x \u2243 y \u2192 (z : A) \u2192 y \u2261 z \u2192 x \u2243 z\n prf p === z by q = prf (trans {_} {_} {_} p q)\n\n _qed : {x y : A} \u2192 x \u2243 y \u2192 x \u2261 y\n prf p qed = p\n\n------------------------------------------------------------------------------\n-- A version from the standard library (Relation.Binary.PreorderReasoning)\nmodule StdLib where\n\n infix 1 begin_\n infixr 2 _\u2261\u27e8_\u27e9_\n infix 2 _\u220e\n\n begin_ : {x y : A} \u2192 x \u2243 y \u2192 x \u2261 y\n begin prf x\u2261y = x\u2261y\n\n _\u2261\u27e8_\u27e9_ : (x : A){y z : A} \u2192 x \u2261 y \u2192 y \u2243 z \u2192 x \u2243 z\n _ \u2261\u27e8 x\u2261y \u27e9 prf y\u2261z = prf (trans x\u2261y y\u2261z)\n\n _\u220e : (x : A) \u2192 x \u2243 x\n _\u220e _ = prf refl\n","old_contents":"-----------------------------------------------------------------------------\n-- Equality reasoning\n-----------------------------------------------------------------------------\n\nmodule MyStdLib.Relation.Binary.EqReasoning\n {A : Set}\n (_\u2261_ : A \u2192 A \u2192 Set)\n (refl : {x : A} \u2192 x \u2261 x)\n (trans : {x y z : A} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z)\n where\n\ninfix 4 _\u2243_\n\nprivate\n data _\u2243_ (x y : A) : Set where\n prf : x \u2261 y \u2192 x \u2243 y\n\nmodule Original where\n ---------------------------------------------------------------------------\n -- A version from Ulf's thesis\n\n infix 2 chain>_\n infixl 2 _===_by_\n infix 1 _qed\n\n chain>_ : (x : A) \u2192 x \u2243 x\n chain> x = prf (refl {x})\n\n _===_by_ : {x y : A} \u2192 x \u2243 y \u2192 (z : A) \u2192 y \u2261 z \u2192 x \u2243 z\n prf p === z by q = prf (trans {_} {_} {_} p q)\n\n _qed : {x y : A} \u2192 x \u2243 y \u2192 x \u2261 y\n prf p qed = p\n\nmodule StdLib where\n\n -- A version from the standard library (Relation.Binary.PreorderReasoning)\n\n infix 1 begin_\n infixr 2 _\u2261\u27e8_\u27e9_\n infix 2 _\u220e\n\n begin_ : {x y : A} \u2192 x \u2243 y \u2192 x \u2261 y\n begin prf x\u2261y = x\u2261y\n\n _\u2261\u27e8_\u27e9_ : (x : A){y z : A} \u2192 x \u2261 y \u2192 y \u2243 z \u2192 x \u2243 z\n _ \u2261\u27e8 x\u2261y \u27e9 prf y\u2261z = prf (trans x\u2261y y\u2261z)\n\n _\u220e : (x : A) \u2192 x \u2243 x\n _\u220e _ = prf refl\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"e23166c1580dcc4547699a337cfc1203c6dae91b","subject":"SHA1 main","message":"SHA1 main\n","repos":"crypto-agda\/crypto-agda","old_file":"sha1.agda","new_file":"sha1.agda","new_contents":"-- https:\/\/upload.wikimedia.org\/wikipedia\/commons\/e\/e2\/SHA-1.svg\n-- http:\/\/www.faqs.org\/rfcs\/rfc3174.html\nopen import Type\nopen import Data.Nat.NP\nopen import Data.Bool using (if_then_else_)\nimport Data.Vec as V\nopen V using (Vec; []; _\u2237_)\n--open import Data.Product\nopen import Function.NP hiding (id)\nopen import FunUniverse.Core hiding (_,_)\nopen import Data.Fin using (Fin; zero; suc; #_; inject+; raise) renaming (to\u2115 to Fin\u25b9\u2115)\n\nmodule sha1 where\n\nmodule FunSHA1\n {t}\n {T : \u2605_ t}\n {funU : FunUniverse T}\n (funOps : FunOps funU)\n where\n\n open FunUniverse funU renaming (`\u22a4 to `\ud835\udfd9; `Bit to `\ud835\udfda)\n open FunOps funOps renaming (_\u2218_ to _`\u2218_)\n\n Word : T\n Word = `Bits 32\n\n map\u00b2\u02b7 : (`\ud835\udfda `\u00d7 `\ud835\udfda `\u2192 `\ud835\udfda) \u2192 Word `\u00d7 Word `\u2192 Word\n map\u00b2\u02b7 = zipWith\n\n map\u02b7 : (`\ud835\udfda `\u2192 `\ud835\udfda) \u2192 Word `\u2192 Word\n map\u02b7 = map\n\n lift : \u2200 {\u0393 A B} \u2192 (A `\u2192 B) \u2192 \u0393 `\u2192 A \u2192 \u0393 `\u2192 B\n lift f g = g \u204f f\n\n lift\u2082 : \u2200 {\u0393 A B C} \u2192 (A `\u00d7 B `\u2192 C) \u2192 \u0393 `\u2192 A \u2192 \u0393 `\u2192 B \u2192 \u0393 `\u2192 C\n lift\u2082 op\u2082 f\u2080 f\u2081 = < f\u2080 , f\u2081 > \u204f op\u2082\n\n `not : \u2200 {\u0393} (f : \u0393 `\u2192 Word) \u2192 \u0393 `\u2192 Word\n `not = lift (map\u02b7 not)\n\n infixr 3 _`\u2295_\n _`\u2295_ : \u2200 {\u0393} (f\u2080 f\u2081 : \u0393 `\u2192 Word) \u2192 \u0393 `\u2192 Word\n _`\u2295_ = lift\u2082 (map\u00b2\u02b7 )\n\n infixr 3 _`\u2227_\n _`\u2227_ : \u2200 {\u0393} (f\u2080 f\u2081 : \u0393 `\u2192 Word) \u2192 \u0393 `\u2192 Word\n _`\u2227_ = lift\u2082 (map\u00b2\u02b7 )\n\n infixr 2 _`\u2228_\n _`\u2228_ : \u2200 {\u0393} (f\u2080 f\u2081 : \u0393 `\u2192 Word) \u2192 \u0393 `\u2192 Word\n _`\u2228_ = lift\u2082 (map\u00b2\u02b7 )\n\n open import Solver.Linear\n\n module LinSolver = Syntax\u1da0 linRewiring\n\n --iter : \u2200 {n A B S} \u2192 (S `\u00d7 A `\u2192 S `\u00d7 B) \u2192 S `\u00d7 `Vec A n `\u2192 `Vec B n\n\n iter : \u2200 {n A B C D} \u2192 (D `\u00d7 A `\u00d7 B `\u2192 D `\u00d7 C) \u2192 D `\u00d7 `Vec A n `\u00d7 `Vec B n `\u2192 `Vec C n\n iter {zero} F = <[]>\n iter {suc n} F = < id \u00d7 < uncons \u00d7 uncons > >\n \u204f (helper \u204f < F \u00d7 id > \u204f < swap \u00d7 id > \u204f assoc \u204f < id \u00d7 iter F >)\n \u204f <\u2237>\n\n where\n open LinSolver\n helper = \u03bb {A} {B} {D} {VA} {VB} \u2192\n rewire\u1da0 (A \u2237 B \u2237 D \u2237 VA \u2237 VB \u2237 [])\n (\u03bb a b d va vb \u2192 (d , (a , va) , (b , vb)) \u21a6 (d , a , b) , (va , vb))\n\n <\u229e> adder : Word `\u00d7 Word `\u2192 Word\n adder = , id > \u204f iter full-adder\n <\u229e> = adder\n\n infixl 4 _`\u229e_\n _`\u229e_ : \u2200 {A} (f g : A `\u2192 Word) \u2192 A `\u2192 Word\n _`\u229e_ = lift\u2082 <\u229e>\n\n <_,_,_> : \u2200 {\u0393 A B C} \u2192 \u0393 `\u2192 A \u2192 \u0393 `\u2192 B \u2192 \u0393 `\u2192 C \u2192 \u0393 `\u2192 (A `\u00d7 B `\u00d7 C)\n < f\u2080 , f\u2081 , f\u2082 > = < f\u2080 , < f\u2081 , f\u2082 > >\n\n <_,_,_,_,_> : \u2200 {\u0393 A B C D E} \u2192 \u0393 `\u2192 A \u2192 \u0393 `\u2192 B \u2192 \u0393 `\u2192 C\n \u2192 \u0393 `\u2192 D \u2192 \u0393 `\u2192 E\n \u2192 \u0393 `\u2192 (A `\u00d7 B `\u00d7 C `\u00d7 D `\u00d7 E)\n < f\u2080 , f\u2081 , f\u2082 , f\u2083 , f\u2084 > = < f\u2080 , < f\u2081 , < f\u2082 , f\u2083 , f\u2084 > > >\n\n <<<\u2085 : `Endo Word\n <<<\u2085 = rot-left 5\n\n <<<\u2083\u2080 : `Endo Word\n <<<\u2083\u2080 = rot-left 30\n\n Word\u00b2 = Word `\u00d7 Word\n Word\u00b3 = Word `\u00d7 Word\u00b2\n Word\u2074 = Word `\u00d7 Word\u00b3\n Word\u2075 = Word `\u00d7 Word\u2074\n\n open import Data.Digit\n\n bits : \u2200 \u2113 \u2192 \u2115 \u2192 `\ud835\udfd9 `\u2192 `Bits \u2113\n bits \u2113 n\u2080 = constBits (L\u25b9V (L.map F\u25b9\ud835\udfda (proj\u2081 (toDigits 2 n\u2080))))\n where open import Data.List as L\n open import Data.Product\n open import Data.Two\n L\u25b9V : \u2200 {n} \u2192 List \ud835\udfda \u2192 Vec \ud835\udfda n\n L\u25b9V {zero} xs = []\n L\u25b9V {suc n} [] = V.replicate 0'\n L\u25b9V {suc n} (x \u2237 xs) = x \u2237 L\u25b9V xs\n F\u25b9\ud835\udfda : Fin 2 \u2192 \ud835\udfda\n F\u25b9\ud835\udfda zero = 0'\n F\u25b9\ud835\udfda (suc _) = 1'\n\n {-\n [_-_mod_] : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n [ m - n mod p ] = {!!}\n\n [_+_mod_] : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n [ m + n mod p ] = {!!}\n -}\n\n #\u02b7 = bits 32\n\n <\u229e\u2075> : Word\u2075 `\u00d7 Word\u2075 `\u2192 Word\u2075\n <\u229e\u2075> = helper \u204f < <\u229e> \u00d7 < <\u229e> \u00d7 < <\u229e> \u00d7 < <\u229e> \u00d7 <\u229e> > > > >\n where\n open LinSolver\n helper = \u03bb {A} {B} {C} {D} {E} {F} {G} {H} {I} {J} \u2192\n rewire\u1da0 (A \u2237 B \u2237 C \u2237 D \u2237 E \u2237 F \u2237 G \u2237 H \u2237 I \u2237 J \u2237 [])\n (\u03bb a b c d e f g h i j \u2192\n ((a , b , c , d , e) , (f , g , h , i , j) \u21a6\n ((a , f) , (b , g) , (c , h) , (d , i) , (e , j))))\n\n iterate\u207f : \u2200 {A} n \u2192 (Fin n \u2192 `Endo A) \u2192 `Endo A\n iterate\u207f zero f = id\n iterate\u207f (suc n) f = f zero \u204f iterate\u207f n (f \u2218 suc)\n\n _\u00b2\u2070 : \u2200 {A} \u2192 (Fin 20 \u2192 `Endo A) \u2192 `Endo A\n _\u00b2\u2070 = iterate\u207f 20\n\n module _ where\n\n K\u2080 = #\u02b7 0x5A827999\n K\u2082 = #\u02b7 0x6ED9EBA1\n K\u2084 = #\u02b7 0x8F1BBCDC\n K\u2086 = #\u02b7 0xCA62C1D6\n\n H0 = #\u02b7 0x67452301\n H1 = #\u02b7 0xEFCDAB89\n H2 = #\u02b7 0x98BADCFE\n H3 = #\u02b7 0x10325476\n H4 = #\u02b7 0xC3D2E1F0\n\n A B C D E : Word\u2075 `\u2192 Word\n A = fst\n B = snd \u204f fst\n C = snd \u204f snd \u204f fst\n D = snd \u204f snd \u204f snd \u204f fst\n E = snd \u204f snd \u204f snd \u204f snd\n\n F\u2080 = B `\u2227 C `\u2228 `not B `\u2227 D\n F\u2082 = B `\u2295 C `\u2295 D\n F\u2084 = B `\u2227 C `\u2228 B `\u2227 D `\u2228 C `\u2227 D\n F\u2086 = F\u2082\n\n module _ (F : Word\u2075 `\u2192 Word)\n (K : `\ud835\udfd9 `\u2192 Word)\n (W : `\ud835\udfd9 `\u2192 Word) where\n Iteration = < A' , A , (B \u204f <<<\u2083\u2080) , C , D >\n where A' = F `\u229e E `\u229e (A \u204f <<<\u2085) `\u229e (tt \u204f W) `\u229e (tt \u204f K)\n\n module _ (W : Fin 80 \u2192 `\ud835\udfd9 `\u2192 Word) where\n W\u2080 W\u2082 W\u2084 W\u2086 : Fin 20 \u2192 `\ud835\udfd9 `\u2192 Word\n W\u2080 = W \u2218 inject+ 60 \u2218 raise 0\n W\u2082 = W \u2218 inject+ 40 \u2218 raise 20\n W\u2084 = W \u2218 inject+ 20 \u2218 raise 40\n W\u2086 = W \u2218 inject+ 0 \u2218 raise 60\n\n Iteration\u2078\u2070 : `Endo Word\u2075\n Iteration\u2078\u2070 =\n (Iteration F\u2080 K\u2080 \u2218 W\u2080)\u00b2\u2070 \u204f\n (Iteration F\u2082 K\u2082 \u2218 W\u2082)\u00b2\u2070 \u204f\n (Iteration F\u2084 K\u2084 \u2218 W\u2084)\u00b2\u2070 \u204f\n (Iteration F\u2086 K\u2086 \u2218 W\u2086)\u00b2\u2070\n\n pad0s : \u2115 \u2192 \u2115\n pad0s zero = 512 \u2238 65\n pad0s (suc _) = STUCK where postulate STUCK : \u2115\n -- pad0s n = [ 512 - [ n + 65 mod 512 ] mod 512 ]\n\n paddedLength : \u2115 \u2192 \u2115\n paddedLength n = n + (1 + pad0s n + 64)\n\n padding : \u2200 {n} \u2192 `Bits n `\u2192 `Bits (paddedLength n)\n padding {n} = < id ,tt\u204f <1\u2237 < <0\u207f> {pad0s n} ++ bits 64 n > > > \u204f append\n\n ite : Endo (`Endo Word\u2075)\n ite f = dup \u204f first f \u204f <\u229e\u2075>\n\n hash-block : `Endo Word\u2075\n hash-block = ite Iteration\u2078\u2070\n\n ite' : \u2200 n (W : Fin n \u2192 Fin 80 \u2192 `\ud835\udfd9 `\u2192 Word)\n \u2192 ((Fin 80 \u2192 `\ud835\udfd9 `\u2192 Word) \u2192 `Endo Word\u2075) \u2192 `Endo Word\u2075\n ite' zero W f = id\n ite' (suc n) W f = f (W zero) \u204f ite' n (W \u2218 suc) f\n\n SHA1 : \u2200 n (W : Fin n \u2192 Fin 80 \u2192 `\ud835\udfd9 `\u2192 Word) \u2192 `\ud835\udfd9 `\u2192 Word\u2075\n SHA1 n W = < H0 , H1 , H2 , H3 , H4 >\n \u204f ite' n W hash-block\n\nmodule AgdaSHA1 where\n open import FunUniverse.Agda\n open FunSHA1 agdaFunOps\n open import Data.Two\n\n SHA1-on-0s : Word\u2075\n SHA1-on-0s = SHA1 1 (\u03bb _ _ _ \u2192 V.replicate 0') _\n\nopen import IO\nimport IO.Primitive\nopen import Data.One\nopen import Data.Two\nopen import Data.Product\nopen import Coinduction\nputBit : \ud835\udfda \u2192 IO \ud835\udfd9\nputBit 1' = putStr \"1\"\nputBit 0' = putStr \"0\"\nputBits : \u2200 {n} \u2192 Vec \ud835\udfda n \u2192 IO \ud835\udfd9\nputBits [] = return _\nputBits (x \u2237 bs) = \u266f putBit x >> \u266f putBits bs\nput\u00d7 : \u2200 {A B : Set} \u2192 (A \u2192 IO \ud835\udfd9) \u2192 (B \u2192 IO \ud835\udfd9) \u2192 (A \u00d7 B) \u2192 IO \ud835\udfd9\nput\u00d7 pA pB (x , y) = \u266f pA x >> \u266f pB y\n{-\nmain : IO.Primitive.IO \ud835\udfd9\nmain = IO.run (put\u00d7 putBits (put\u00d7 putBits (put\u00d7 putBits (put\u00d7 putBits\n putBits))) AgdaSHA1.SHA1-on-0s)\n-}\nfirstBit : \u2200 {A : Set} \u2192 (V.Vec \ud835\udfda 32 \u00d7 A) \u2192 \ud835\udfda\nfirstBit ((b \u2237 _) , _) = b\nmain : IO.Primitive.IO \ud835\udfd9\nmain = IO.run (putBit (firstBit AgdaSHA1.SHA1-on-0s))\n\n-- -}\n","old_contents":"-- https:\/\/upload.wikimedia.org\/wikipedia\/commons\/e\/e2\/SHA-1.svg\n-- http:\/\/www.faqs.org\/rfcs\/rfc3174.html\nopen import Type\nopen import Data.Nat.NP\nopen import Data.Bool using (if_then_else_)\nimport Data.Vec as V\nopen V using (Vec; []; _\u2237_)\n--open import Data.Product\nopen import Function.NP hiding (id)\nopen import FunUniverse.Core hiding (_,_)\nopen import Data.Fin using (Fin; zero; suc; #_; inject+; raise) renaming (to\u2115 to Fin\u25b9\u2115)\n\nmodule sha1 where\n\nmodule FunSHA1\n {t}\n {T : \u2605_ t}\n {funU : FunUniverse T}\n (funOps : FunOps funU)\n where\n\n open FunUniverse funU renaming (`\u22a4 to `\ud835\udfd9; `Bit to `\ud835\udfda)\n open FunOps funOps renaming (_\u2218_ to _`\u2218_)\n\n Word : T\n Word = `Bits 32\n\n map\u00b2\u02b7 : (`\ud835\udfda `\u00d7 `\ud835\udfda `\u2192 `\ud835\udfda) \u2192 Word `\u00d7 Word `\u2192 Word\n map\u00b2\u02b7 = zipWith\n\n map\u02b7 : (`\ud835\udfda `\u2192 `\ud835\udfda) \u2192 Word `\u2192 Word\n map\u02b7 = map\n\n lift : \u2200 {\u0393 A B} \u2192 (A `\u2192 B) \u2192 \u0393 `\u2192 A \u2192 \u0393 `\u2192 B\n lift f g = g \u204f f\n\n lift\u2082 : \u2200 {\u0393 A B C} \u2192 (A `\u00d7 B `\u2192 C) \u2192 \u0393 `\u2192 A \u2192 \u0393 `\u2192 B \u2192 \u0393 `\u2192 C\n lift\u2082 op\u2082 f\u2080 f\u2081 = < f\u2080 , f\u2081 > \u204f op\u2082\n\n `not : \u2200 {\u0393} (f : \u0393 `\u2192 Word) \u2192 \u0393 `\u2192 Word\n `not = lift (map\u02b7 not)\n\n infixr 3 _`\u2295_\n _`\u2295_ : \u2200 {\u0393} (f\u2080 f\u2081 : \u0393 `\u2192 Word) \u2192 \u0393 `\u2192 Word\n _`\u2295_ = lift\u2082 (map\u00b2\u02b7 )\n\n infixr 3 _`\u2227_\n _`\u2227_ : \u2200 {\u0393} (f\u2080 f\u2081 : \u0393 `\u2192 Word) \u2192 \u0393 `\u2192 Word\n _`\u2227_ = lift\u2082 (map\u00b2\u02b7 )\n\n infixr 2 _`\u2228_\n _`\u2228_ : \u2200 {\u0393} (f\u2080 f\u2081 : \u0393 `\u2192 Word) \u2192 \u0393 `\u2192 Word\n _`\u2228_ = lift\u2082 (map\u00b2\u02b7 )\n\n open import Solver.Linear\n\n module LinSolver = Syntax\u1da0 linRewiring\n\n --iter : \u2200 {n A B S} \u2192 (S `\u00d7 A `\u2192 S `\u00d7 B) \u2192 S `\u00d7 `Vec A n `\u2192 `Vec B n\n\n iter : \u2200 {n A B C D} \u2192 (D `\u00d7 A `\u00d7 B `\u2192 D `\u00d7 C) \u2192 D `\u00d7 `Vec A n `\u00d7 `Vec B n `\u2192 `Vec C n\n iter {zero} F = <[]>\n iter {suc n} F = < id \u00d7 < uncons \u00d7 uncons > >\n \u204f (helper \u204f < F \u00d7 id > \u204f < swap \u00d7 id > \u204f assoc \u204f < id \u00d7 iter F >)\n \u204f <\u2237>\n\n where\n open LinSolver\n helper = \u03bb {A} {B} {D} {VA} {VB} \u2192\n rewire\u1da0 (A \u2237 B \u2237 D \u2237 VA \u2237 VB \u2237 [])\n (\u03bb a b d va vb \u2192 (d , (a , va) , (b , vb)) \u21a6 (d , a , b) , (va , vb))\n\n <\u229e> adder : Word `\u00d7 Word `\u2192 Word\n adder = , id > \u204f iter full-adder\n <\u229e> = adder\n\n infixl 4 _`\u229e_\n _`\u229e_ : \u2200 {A} (f g : A `\u2192 Word) \u2192 A `\u2192 Word\n _`\u229e_ = lift\u2082 <\u229e>\n\n <_,_,_> : \u2200 {\u0393 A B C} \u2192 \u0393 `\u2192 A \u2192 \u0393 `\u2192 B \u2192 \u0393 `\u2192 C \u2192 \u0393 `\u2192 (A `\u00d7 B `\u00d7 C)\n < f\u2080 , f\u2081 , f\u2082 > = < f\u2080 , < f\u2081 , f\u2082 > >\n\n <_,_,_,_,_> : \u2200 {\u0393 A B C D E} \u2192 \u0393 `\u2192 A \u2192 \u0393 `\u2192 B \u2192 \u0393 `\u2192 C\n \u2192 \u0393 `\u2192 D \u2192 \u0393 `\u2192 E\n \u2192 \u0393 `\u2192 (A `\u00d7 B `\u00d7 C `\u00d7 D `\u00d7 E)\n < f\u2080 , f\u2081 , f\u2082 , f\u2083 , f\u2084 > = < f\u2080 , < f\u2081 , < f\u2082 , f\u2083 , f\u2084 > > >\n\n <<<\u2085 : `Endo Word\n <<<\u2085 = rot-left 5\n\n <<<\u2083\u2080 : `Endo Word\n <<<\u2083\u2080 = rot-left 30\n\n Word\u00b2 = Word `\u00d7 Word\n Word\u00b3 = Word `\u00d7 Word\u00b2\n Word\u2074 = Word `\u00d7 Word\u00b3\n Word\u2075 = Word `\u00d7 Word\u2074\n\n open import Data.Digit\n\n bits : \u2200 \u2113 \u2192 \u2115 \u2192 `\ud835\udfd9 `\u2192 `Bits \u2113\n bits \u2113 n\u2080 = constBits (L\u25b9V (L.map F\u25b9\ud835\udfda (proj\u2081 (toDigits 2 n\u2080))))\n where open import Data.List as L\n open import Data.Product\n open import Data.Two\n L\u25b9V : \u2200 {n} \u2192 List \ud835\udfda \u2192 Vec \ud835\udfda n\n L\u25b9V {zero} xs = []\n L\u25b9V {suc n} [] = V.replicate 0'\n L\u25b9V {suc n} (x \u2237 xs) = x \u2237 L\u25b9V xs\n F\u25b9\ud835\udfda : Fin 2 \u2192 \ud835\udfda\n F\u25b9\ud835\udfda zero = 0'\n F\u25b9\ud835\udfda (suc _) = 1'\n\n {-\n [_-_mod_] : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n [ m - n mod p ] = {!!}\n\n [_+_mod_] : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n [ m + n mod p ] = {!!}\n -}\n\n #\u02b7 = bits 32\n\n <\u229e\u2075> : Word\u2075 `\u00d7 Word\u2075 `\u2192 Word\u2075\n <\u229e\u2075> = helper \u204f < <\u229e> \u00d7 < <\u229e> \u00d7 < <\u229e> \u00d7 < <\u229e> \u00d7 <\u229e> > > > >\n where\n open LinSolver\n helper = \u03bb {A} {B} {C} {D} {E} {F} {G} {H} {I} {J} \u2192\n rewire\u1da0 (A \u2237 B \u2237 C \u2237 D \u2237 E \u2237 F \u2237 G \u2237 H \u2237 I \u2237 J \u2237 [])\n (\u03bb a b c d e f g h i j \u2192\n ((a , b , c , d , e) , (f , g , h , i , j) \u21a6\n ((a , f) , (b , g) , (c , h) , (d , i) , (e , j))))\n\n iterate\u207f : \u2200 {A} n \u2192 (Fin n \u2192 `Endo A) \u2192 `Endo A\n iterate\u207f zero f = id\n iterate\u207f (suc n) f = f zero \u204f iterate\u207f n (f \u2218 suc)\n\n _\u00b2\u2070 : \u2200 {A} \u2192 (Fin 20 \u2192 `Endo A) \u2192 `Endo A\n _\u00b2\u2070 = iterate\u207f 20\n\n module _ where\n\n K\u2080 = #\u02b7 0x5A827999\n K\u2082 = #\u02b7 0x6ED9EBA1\n K\u2084 = #\u02b7 0x8F1BBCDC\n K\u2086 = #\u02b7 0xCA62C1D6\n\n H0 = #\u02b7 0x67452301\n H1 = #\u02b7 0xEFCDAB89\n H2 = #\u02b7 0x98BADCFE\n H3 = #\u02b7 0x10325476\n H4 = #\u02b7 0xC3D2E1F0\n\n A B C D E : Word\u2075 `\u2192 Word\n A = fst\n B = snd \u204f fst\n C = snd \u204f snd \u204f fst\n D = snd \u204f snd \u204f snd \u204f fst\n E = snd \u204f snd \u204f snd \u204f snd\n\n F\u2080 = B `\u2227 C `\u2228 `not B `\u2227 D\n F\u2082 = B `\u2295 C `\u2295 D\n F\u2084 = B `\u2227 C `\u2228 B `\u2227 D `\u2228 C `\u2227 D\n F\u2086 = F\u2082\n\n module _ (F : Word\u2075 `\u2192 Word)\n (K : `\ud835\udfd9 `\u2192 Word)\n (W : `\ud835\udfd9 `\u2192 Word) where\n Iteration = < A' , A , (B \u204f <<<\u2083\u2080) , C , D >\n where A' = F `\u229e E `\u229e (A \u204f <<<\u2085) `\u229e (tt \u204f W) `\u229e (tt \u204f K)\n\n module _ (W : Fin 80 \u2192 `\ud835\udfd9 `\u2192 Word) where\n W\u2080 W\u2082 W\u2084 W\u2086 : Fin 20 \u2192 `\ud835\udfd9 `\u2192 Word\n W\u2080 = W \u2218 inject+ 60 \u2218 raise 0\n W\u2082 = W \u2218 inject+ 40 \u2218 raise 20\n W\u2084 = W \u2218 inject+ 20 \u2218 raise 40\n W\u2086 = W \u2218 inject+ 0 \u2218 raise 60\n\n Iteration\u2078\u2070 : `Endo Word\u2075\n Iteration\u2078\u2070 =\n (Iteration F\u2080 K\u2080 \u2218 W\u2080)\u00b2\u2070 \u204f\n (Iteration F\u2082 K\u2082 \u2218 W\u2082)\u00b2\u2070 \u204f\n (Iteration F\u2084 K\u2084 \u2218 W\u2084)\u00b2\u2070 \u204f\n (Iteration F\u2086 K\u2086 \u2218 W\u2086)\u00b2\u2070\n\n pad0s : \u2115 \u2192 \u2115\n pad0s zero = 512 \u2238 65\n pad0s (suc _) = STUCK where postulate STUCK : \u2115\n -- pad0s n = [ 512 - [ n + 65 mod 512 ] mod 512 ]\n\n paddedLength : \u2115 \u2192 \u2115\n paddedLength n = n + (1 + pad0s n + 64)\n\n padding : \u2200 {n} \u2192 `Bits n `\u2192 `Bits (paddedLength n)\n padding {n} = < id ,tt\u204f <1\u2237 < <0\u207f> {pad0s n} ++ bits 64 n > > > \u204f append\n\n ite : Endo (`Endo Word\u2075)\n ite f = dup \u204f first f \u204f <\u229e\u2075>\n\n hash-block : `Endo Word\u2075\n hash-block = ite Iteration\u2078\u2070\n\n ite' : \u2200 n (W : Fin n \u2192 Fin 80 \u2192 `\ud835\udfd9 `\u2192 Word)\n \u2192 ((Fin 80 \u2192 `\ud835\udfd9 `\u2192 Word) \u2192 `Endo Word\u2075) \u2192 `Endo Word\u2075\n ite' zero W f = id\n ite' (suc n) W f = f (W zero) \u204f ite' n (W \u2218 suc) f\n\n SHA1 : \u2200 n (W : Fin n \u2192 Fin 80 \u2192 `\ud835\udfd9 `\u2192 Word) \u2192 `\ud835\udfd9 `\u2192 Word\u2075\n SHA1 n W = < H0 , H1 , H2 , H3 , H4 >\n \u204f ite' n W hash-block\n\nmodule AgdaSHA1 where\n open import FunUniverse.Agda\n open FunSHA1 agdaFunOps\n open import Data.Two\n\n SHA1-on-0s : Word\u2075\n SHA1-on-0s = SHA1 1 (\u03bb _ _ _ \u2192 V.replicate 0') _\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"7c96b8369eaff904dd9270e53985e5b78739b42c","subject":"Desc@ICFP: add \\update to model","message":"Desc@ICFP: add \\update to model","repos":"kwangkim\/pigment,kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram","old_file":"models\/IDescTT.agda","new_file":"models\/IDescTT.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n pair : Type -> Type -> Type\n\n--********************************\n-- Typed expressions\n--********************************\n\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\nVal (pair x y) = (Val x) * (Val y)\n\n\n-- Fix menu:\nexprFixMenu : FixMenu Type\nexprFixMenu = ( consE (consE nilE) , \n \\ty -> (const (Val ty), -- Val t\n (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void)))\n\n-- Indexed menu:\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\nchoiceMenu (pair x y) = nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\nchoiceDessert (pair x y) = Void\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\n\n-- Expression:\nexpr : TagIDesc Type\nexpr = exprFixMenu , exprSensitiveMenu\n\nexprIDesc : TagIDesc Type -> (Type -> IDesc Type)\nexprIDesc D = toIDesc Type D\n\n\n--********************************\n-- Closed terms\n--********************************\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : {ty : Type} -> IMu closeTerm ty -> Val ty\neval {ty} term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nEmpty : Type -> Set\nEmpty _ = Zero\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (expr ** Empty)\n\nupdate : {ty : Type} -> IMu closeTerm ty -> IMu closeTerm' ty\nupdate {ty} tm = cata Type closeTerm (IMu closeTerm') (\\ _ tagTm -> con (ESu (fst tagTm) , (snd tagTm))) ty tm\n\n\n--********************************\n-- Closed term' evaluation\n--********************************\n\neval' : {ty : Type} -> IMu closeTerm' ty -> Val ty\neval' {ty} term = cata Type closeTerm' Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm' ty |] Val -> Val ty\n evalOneStep _ (EZe , ())\n evalOneStep _ (ESu EZe , t) = t\n evalOneStep _ ((ESu (ESu EZe)) , (true , ( x , _))) = x\n evalOneStep _ ((ESu (ESu EZe)) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu (ESu EZe))) , (x , y)) = plus x y\n evalOneStep nat (((ESu (ESu (ESu (ESu ()))))) , t) \n evalOneStep bool ((ESu (ESu (ESu EZe))) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu (ESu ())))) , _) \n evalOneStep (pair x y) (ESu (ESu (ESu ())) , _)\n\n\n--********************************\n-- Open terms\n--********************************\n\n-- A context is a snoc-list of types\n-- put otherwise, a context is a type telescope\ndata Context : Set where\n [] : Context\n _,_ : Context -> Type -> Context\n\n-- The environment realizes the context, having a value for each type\nEnv : Context -> Set\nEnv [] = Unit\nEnv (G , S) = Env G * Val S\n\n-- A typed-variable indexes into the context, obtaining a proof that\n-- what we get is what you want (WWGIWYW)\nVar : Context -> Type -> Set\nVar [] T = Zero\nVar (G , S) T = Var G T + (S == T)\n\n-- The lookup gets into the context to extract the value\nlookup : (G : Context) -> Env G -> (T : Type) -> Var G T -> Val T\nlookup [] _ T ()\nlookup (G , .T) (g , t) T (r refl) = t\nlookup (G , S) (g , t) T (l x) = lookup G g T x \n\n-- Open term: holes are either values or variables in a context\nopenTerm : Context -> Type -> IDesc Type\nopenTerm c = toIDesc Type (expr ** (Var c))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\n-- |discharge| is the local substitution expected by |substI|. It is\n-- just sugar around context lookup\ndischarge : (context : Context) ->\n Env context ->\n (ty : Type) ->\n Var context ty ->\n IMu closeTerm' ty\ndischarge ctxt env ty variable = con (ESu EZe , lookup ctxt env ty variable ) \n\n-- |substExpr| is the specialized |substI| to expressions. We get it\n-- generically from the free monad construction.\nsubstExpr : {ty : Type}\n (context : Context)\n (sigma : (ty : Type) ->\n Var context ty ->\n IMu closeTerm' ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm' ty\nsubstExpr {ty} c sig term = \n substI (Var c) Empty expr sig ty term\n\n-- By first doing substitution to close the term, we can use\n-- evaluation of closed terms, obtaining evaluation of open terms\n-- under a valid context.\nevalOpen : {ty : Type}(context : Context) ->\n Env context ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen ctxt env tm = eval' (substExpr ctxt (discharge ctxt env) tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- Test context:\n-- V 0 :-> true, V 1 :-> 2, V 2 :-> ( false , 1 )\ntestContext : Context\ntestContext = (([] , bool) , nat) , pair bool nat\ntestEnv : Env testContext\ntestEnv = ((Void , true ) , su (su ze)) , (false , su ze) \n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , ( l (r refl) ) )\n\ntestSubst1 : IMu closeTerm' nat\ntestSubst1 = substExpr testContext \n (discharge testContext testEnv)\n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext testEnv test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con (ESu (ESu (ESu EZe)) , (con (ESu EZe , su ze) , con ( EZe , l (r refl) )) )\n\ntestSubst2 : IMu closeTerm' nat\ntestSubst2 = substExpr testContext \n (discharge testContext testEnv)\n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext testEnv test2\n\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu (ESu EZe) , (con (EZe , l (l (r refl))) ,\n (con (EZe , l (r refl)) ,\n con (ESu EZe , ze))))\n\ntestSubst3 : IMu closeTerm' nat\ntestSubst3 = substExpr testContext \n (discharge testContext testEnv)\n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext testEnv test3\n\n-- V 2\ntest4 : IMu (openTerm testContext) (pair bool nat)\ntest4 = con (EZe , r refl )\n\ntestSubst4 : IMu closeTerm' (pair bool nat)\ntestSubst4 = substExpr testContext \n (discharge testContext testEnv)\n test4\n-- = (false , 1)\ntestEval4 : Val (pair bool nat)\ntestEval4 = evalOpen testContext testEnv test4","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n pair : Type -> Type -> Type\n\n--********************************\n-- Typed expressions\n--********************************\n\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\nVal (pair x y) = (Val x) * (Val y)\n\n\n-- Fix menu:\nexprFixMenu : FixMenu Type\nexprFixMenu = ( consE (consE nilE) , \n \\ty -> (const (Val ty), -- Val t\n (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void)))\n\n-- Indexed menu:\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\nchoiceMenu (pair x y) = nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\nchoiceDessert (pair x y) = Void\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\n\n-- Expression:\nexpr : TagIDesc Type\nexpr = exprFixMenu , exprSensitiveMenu\n\nexprIDesc : TagIDesc Type -> (Type -> IDesc Type)\nexprIDesc D = toIDesc Type D\n\n\n--********************************\n-- Closed terms\n--********************************\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : {ty : Type} -> IMu closeTerm ty -> Val ty\neval {ty} term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nEmpty : Type -> Set\nEmpty _ = Zero\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (expr ** Empty)\n\n--********************************\n-- Closed term' evaluation\n--********************************\n\neval' : {ty : Type} -> IMu closeTerm' ty -> Val ty\neval' {ty} term = cata Type closeTerm' Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm' ty |] Val -> Val ty\n evalOneStep _ (EZe , ())\n evalOneStep _ (ESu EZe , t) = t\n evalOneStep _ ((ESu (ESu EZe)) , (true , ( x , _))) = x\n evalOneStep _ ((ESu (ESu EZe)) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu (ESu EZe))) , (x , y)) = plus x y\n evalOneStep nat (((ESu (ESu (ESu (ESu ()))))) , t) \n evalOneStep bool ((ESu (ESu (ESu EZe))) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu (ESu ())))) , _) \n evalOneStep (pair x y) (ESu (ESu (ESu ())) , _)\n\n\n--********************************\n-- Open terms\n--********************************\n\n-- A context is a snoc-list of types\n-- put otherwise, a context is a type telescope\ndata Context : Set where\n [] : Context\n _,_ : Context -> Type -> Context\n\n-- The environment realizes the context, having a value for each type\nEnv : Context -> Set\nEnv [] = Unit\nEnv (G , S) = Env G * Val S\n\n-- A typed-variable indexes into the context, obtaining a proof that\n-- what we get is what you want (WWGIWYW)\nVar : Context -> Type -> Set\nVar [] T = Zero\nVar (G , S) T = Var G T + (S == T)\n\n-- The lookup gets into the context to extract the value\nlookup : (G : Context) -> Env G -> (T : Type) -> Var G T -> Val T\nlookup [] _ T ()\nlookup (G , .T) (g , t) T (r refl) = t\nlookup (G , S) (g , t) T (l x) = lookup G g T x \n\n-- Open term: holes are either values or variables in a context\nopenTerm : Context -> Type -> IDesc Type\nopenTerm c = toIDesc Type (expr ** (Var c))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\n-- |discharge| is the local substitution expected by |substI|. It is\n-- just sugar around context lookup\ndischarge : (context : Context) ->\n Env context ->\n (ty : Type) ->\n Var context ty ->\n IMu closeTerm' ty\ndischarge ctxt env ty variable = con (ESu EZe , lookup ctxt env ty variable ) \n\n-- |substExpr| is the specialized |substI| to expressions. We get it\n-- generically from the free monad construction.\nsubstExpr : {ty : Type}\n (context : Context)\n (sigma : (ty : Type) ->\n Var context ty ->\n IMu closeTerm' ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm' ty\nsubstExpr {ty} c sig term = \n substI (Var c) Empty expr sig ty term\n\n-- By first doing substitution to close the term, we can use\n-- evaluation of closed terms, obtaining evaluation of open terms\n-- under a valid context.\nevalOpen : {ty : Type}(context : Context) ->\n Env context ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen ctxt env tm = eval' (substExpr ctxt (discharge ctxt env) tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- Test context:\n-- V 0 :-> true, V 1 :-> 2, V 2 :-> ( false , 1 )\ntestContext : Context\ntestContext = (([] , bool) , nat) , pair bool nat\ntestEnv : Env testContext\ntestEnv = ((Void , true ) , su (su ze)) , (false , su ze) \n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , ( l (r refl) ) )\n\ntestSubst1 : IMu closeTerm' nat\ntestSubst1 = substExpr testContext \n (discharge testContext testEnv)\n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext testEnv test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con (ESu (ESu (ESu EZe)) , (con (ESu EZe , su ze) , con ( EZe , l (r refl) )) )\n\ntestSubst2 : IMu closeTerm' nat\ntestSubst2 = substExpr testContext \n (discharge testContext testEnv)\n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext testEnv test2\n\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu (ESu EZe) , (con (EZe , l (l (r refl))) ,\n (con (EZe , l (r refl)) ,\n con (ESu EZe , ze))))\n\ntestSubst3 : IMu closeTerm' nat\ntestSubst3 = substExpr testContext \n (discharge testContext testEnv)\n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext testEnv test3\n\n-- V 2\ntest4 : IMu (openTerm testContext) (pair bool nat)\ntest4 = con (EZe , r refl )\n\ntestSubst4 : IMu closeTerm' (pair bool nat)\ntestSubst4 = substExpr testContext \n (discharge testContext testEnv)\n test4\n-- = (false , 1)\ntestEval4 : Val (pair bool nat)\ntestEval4 = evalOpen testContext testEnv test4","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ae6b95a252d0d2c5d03ca4dfd2beb4cd1f8f683e","subject":"Cosmetics","message":"Cosmetics\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/FunBigStepSILR2.agda","new_file":"Thesis\/FunBigStepSILR2.agda","new_contents":"-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- But to betray the eventual goal, I can also relate integer values with a\n-- change in the relation witness. That was a completely local change. But that\n-- might also be because we only have few primitives.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\" (ISAC below), POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\n-- In fact, this development is typed, hence some parts of the model are closer\n-- to Ahmed (ESOP 2006), \"Step-Indexed Syntactic Logical Relations for Recursive\n-- and Quantified Types\". But for many relevant aspects, the two papers are\n-- interchangeable.\n--\n-- The main insight from the ISAC paper missing from the other one is how to\n-- step-index a big-step semantics correctly: just ensure that the steps in the\n-- big-step semantics agree with the ones in the small-step semantics. *Then*\n-- everything just works with big-step semantics. Quite a few other details are\n-- fiddly, but those are the same in small-step semantics.\n--\n-- CHEATS:\n-- \"Fuctional big-step semantics\" requires an external termination proof for the\n-- semantics. There it is also mechanized, here it isn't. Worse, the same\n-- termination problem affects some lemmas about the semantics.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen import Data.Nat.Properties.Simple\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\ninfixr 20 _\u21d2_\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\nweaken \u0393\u2081\u227c\u0393\u2082 (const c) = const c\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (weaken-var \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n\n-- WARNING: ISAC's big-step semantics produces a step count as \"output\". But\n-- that would not help Agda establish termination. That's only a problem for a\n-- functional big-step semantics, not for a relational semantics.\n--\n-- So, instead, I tried to use a sort of writer monad: the interpreter gets fuel\n-- and returns the remaining fuel. That's the same trick as in \"functional\n-- big-step semantics\". That *makes* the function terminating, even though Agda\n-- cannot see this because it does not know that the returned fuel is no bigger.\n\n-- Since we focus for now on STLC, unlike that\n-- paper, we could avoid error values because we keep types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) n = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\neval (app s t) \u03c1 (suc n) = (eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)) n\n\neval-const-dec : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-const-dec (lit v) n0 .n0 refl = \u2264-refl\n\n{-# TERMINATING #-}\neval-dec : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-dec (const c) \u03c1 v n0 n1 eq = eval-const-dec c n0 n1 eq\neval-dec (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (app s t) \u03c1 v zero n1 ()\neval-dec (app s t) \u03c1 v (suc n0) n3 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv sn1 | [ seq ] with eval t \u03c1 sn1 | inspect (eval t \u03c1) sn1\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv@(closure st s\u03c1) sn1 | [ seq ] | (Done tv tn2) | [ teq ] = \u2264-step (\u2264-trans (\u2264-trans (eval-dec st _ _ _ _ eq) (eval-dec t _ _ _ _ teq)) (eval-dec s _ _ _ _ seq))\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | Error | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | TimeOut | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Error | [ seq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | TimeOut | [ seq ]\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1)\neval-const-mono (lit v) n0 .n0 refl = refl\n\n-- ARGH\n{-# TERMINATING #-}\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1)\neval-mono (const c) \u03c1 v n0 n1 eq = eval-const-mono c n0 n1 eq\neval-mono (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\neval-mono (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\neval-mono (app s t) \u03c1 v zero n1 ()\neval-mono (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-mono t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n\neval-adjust-plus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1)\neval-adjust-plus zero t \u03c1 v n0 n1 eq = eq\neval-adjust-plus (suc d) t \u03c1 v n0 n1 eq = eval-mono t \u03c1 v (d + n0) (d + n1) (eval-adjust-plus d t \u03c1 v n0 n1 eq)\n\neval-const-strengthen : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1) \u2192 evalConst c n0 \u2261 Done v n1\neval-const-strengthen (lit v) n0 .n0 refl = refl\n\n-- I started trying to prove eval-strengthen, which I appeal to informally\n-- below, but I gave up. I still guess the lemma is true but proving it looks\n-- too painful to bother.\n\n-- Without this lemma, I can't fully prove that this logical relation is\n-- equivalent to the original one.\n-- But this one works (well, at least up to the fundamental theorem, haven't\n-- attempted other lemmas), so it should be good enough.\n\n-- eval-mono-err : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 n \u2192 eval t \u03c1 n \u2261 Error \u2192 eval t \u03c1 (suc n) \u2261 Error\n-- eval-mono-err (const (lit x)) \u03c1 zero eq = {!!}\n-- eval-mono-err (const (lit x)) \u03c1 (suc n) eq = {!!}\n-- eval-mono-err (var x) \u03c1 n eq = {!!}\n-- eval-mono-err (app t t\u2081) \u03c1 n eq = {!!}\n-- eval-mono-err (abs t) \u03c1 n eq = {!!}\n\n-- -- eval t \u03c1 (suc n0) \u2261 Done v (suc n1) \u2192 eval t \u03c1 n0 \u2261 Done v n\n-- eval-aux : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 n \u2192 (\u03a3[ res0 \u2208 ErrVal \u03c4 ] eval t \u03c1 n \u2261 res0) \u00d7 (\u03a3[ resS \u2208 ErrVal \u03c4 ] eval t \u03c1 n \u2261 resS)\n-- eval-aux t \u03c1 n with\n-- eval t \u03c1 n | inspect (eval t \u03c1) n |\n-- eval t \u03c1 (suc n) | inspect (eval t \u03c1) (suc n)\n-- eval-aux t \u03c1 n | res0 | [ eq0 ] | (Done v1 n1) | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | res0 | [ eq0 ] | Error | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | Done v n1 | [ eq0 ] | TimeOut | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | Error | [ eq0 ] | TimeOut | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | TimeOut | [ eq0 ] | TimeOut | [ eq1 ] = (TimeOut , refl) , (TimeOut , refl)\n\n-- {-# TERMINATING #-}\n-- eval-strengthen : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-strengthen (const c) \u03c1 v n0 n1 eq = eval-const-strengthen c n0 n1 eq\n-- eval-strengthen (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq with eval s \u03c1 0 | inspect (eval s \u03c1) 0\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done sv sn1 | [ seq ] with eval-dec s \u03c1 sv 0 sn1 seq\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done sv .0 | [ seq ] | z\u2264n with eval t \u03c1 0 | inspect (eval t \u03c1) 0\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done sv _ | [ seq ] | z\u2264n | Done tv tn1 | [ teq ] with eval-dec t \u03c1 tv 0 tn1 teq\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done (closure st s\u03c1) _ | [ seq ] | z\u2264n | (Done tv .0) | [ teq ] | z\u2264n with eval-dec st _ v 0 (suc n1) eq\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done (closure st s\u03c1) _ | [ seq ] | z\u2264n | (Done tv _) | [ teq ] | z\u2264n | ()\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | Done sv _ | [ seq ] | z\u2264n | Error | [ teq ]\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | Done sv _ | [ seq ] | z\u2264n | TimeOut | [ teq ]\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | Error | [ seq ]\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | TimeOut | [ seq ]\n-- -- eval-dec s \u03c1\n-- -- {!eval-dec s \u03c1 ? (suc zero) (suc n1) !}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 (suc n0) | inspect (eval s \u03c1) (suc n0)\n-- -- eval-strengthen (app s t) \u03c1 v\u2081 (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 n0 = {!eval-strengthen s \u03c1 v n0 n1 seq !}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-strengthen t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | Error | [ seq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | TimeOut | [ seq ] = {!!}\n\n-- eval-adjust-minus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 {\u03c1 v} n0 n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-adjust-minus zero t n0 n1 eq = eq\n-- eval-adjust-minus (suc d) t n0 n1 eq = eval-adjust-minus d t n0 n1 (eval-strengthen t _ _ (d + n0) (d + n1) eq)\n\nimport Data.Integer as I\nopen I using (\u2124)\nmutual\n -- Warning: compared to Ahmed's papers, this definition for relT also requires\n -- t1 to be well-typed, not just t2.\n --\n -- This difference might affect the status of some proofs in Ahmed's papers,\n -- but that's not a problem here.\n\n -- Also: can't confirm this in any of the papers I'm using, but I'd guess that\n -- all papers using environments allow to relate closures with different\n -- implementations and different hidden environments.\n --\n -- To check if the proof goes through with equal context, I changed the proof.\n -- Now a proof that two closures are equivalent contains a proof that their\n -- typing contexts are equivalent. The changes were limited softawre\n -- engineering, the same proofs go through.\n\n -- This is not the same definition of relT, but it is equivalent.\n relT : \u2200 {\u03c4 \u0393} (t1 : Term \u0393 \u03c4) (t2 : Term \u0393 \u03c4) (\u03c11 : \u27e6 \u0393 \u27e7Context) (\u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\n -- This equation is a lemma in the original definition.\n relT t1 t2 \u03c11 \u03c12 zero = \u22a4\n -- To compare this definition, note that the original k is suc n here.\n relT {\u03c4} t1 t2 \u03c11 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n -- Originally we have 0 \u2264 j < k, so j < suc n, so k - j = suc n - j.\n -- It follows that 0 < k - j \u2264 k, hence suc n - j \u2264 suc n, or n - j \u2264 n.\n -- Here, instead of binding j we bind n-j = n - j, require n - j \u2264 n, and\n -- use suc n-j instead of k - j.\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n -- The next assumption is important. This still says that evaluation consumes j steps.\n -- Since j \u2264 n, it is OK to start evaluation with n steps.\n -- Starting with (suc n) and getting suc n-j is equivalent, per eval-mono\n -- and eval-strengthen. But in practice this version is easier to use.\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 0 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n -- Here, computing t2 is allowed to take an unbounded number of steps. Having to write a number at all is annoying.\n\n relV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n -- Show the proof still goes through if we relate clearly different values by\n -- inserting changes in the relation.\n -- There's no syntax to produce such changes, but you can add changes to the\n -- environment.\n relV nat (intV v1) (intV v2) n = \u03a3[ dv \u2208 \u2124 ] dv I.+ (I.+ v1) \u2261 (I.+ v2)\n relV (\u03c3 \u21d2 \u03c4) (closure {\u03931} t1 \u03c11) (closure {\u03932} t2 \u03c12) n =\n \u03a3 (\u03931 \u2261 \u03932) \u03bb { refl \u2192\n \u2200 (k : \u2115) (k\u2264n : k < n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\n }\n -- Above, in the conclusion, I'm not relating app (closure t1 \u03c11) v1 with app\n -- (closure t2 \u03c12) v2 (or some encoding of that that actually works), but the\n -- result of taking a step from that configuration. That is important, because\n -- both Pitts' \"Step-Indexed Biorthogonality: a Tutorial Example\" and\n -- \"Imperative Self-Adjusting Computation\" do the same thing (and point out it's\n -- important).\n\n \u0394\u03c4 : Type \u2192 Type\n \u0394\u03c4 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 (\u0394\u03c4 \u03c3) \u21d2 \u0394\u03c4 \u03c4\n \u0394\u03c4 nat = nat\n\n -- The original relation allows unrelated environments. However, while that is\n -- fine as a logical relation, it's not OK if we want to prove that validity\n -- agrees with oplus. We want a finer relation.\n -- Also: we still need to demand the actual environments to be related, and\n -- the bodies to match. Haven't done that yet. On the other hand, since we do want\n -- to allow for replacement changes, that would probably complicate the proof\n -- elsewhere.\n relT3 : \u2200 {\u03c4 \u0393 \u0394\u0393} (t1 : Term \u0393 \u03c4) (dt : Term \u0394\u0393 (\u0394\u03c4 \u03c4)) (t2 : Term \u0393 \u03c4) (\u03c11 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : \u27e6 \u0394\u0393 \u27e7Context) (\u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\n relT3 t1 dt t2 \u03c11 d\u03c1 \u03c12 zero = \u22a4\n relT3 {\u03c4} t1 dt t2 \u03c11 d\u03c1 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 0 \u00d7\n \u03a3[ dv \u2208 Val (\u0394\u03c4 \u03c4) ] \u03a3[ dn \u2208 \u2115 ] eval dt d\u03c1 dn \u2261 Done dv 0 \u00d7\n relV3 \u03c4 v1 dv v2 (suc n-j)\n\n -- Weakening in this definition is going to be annoying to use. And having to\n -- construct terms is ugly.\n\n -- Weakening could be avoided if we use a separate language of change terms\n -- with two environments, and with a dclosure binding two variables at once,\n -- and so on.\n relV3 : \u2200 \u03c4 (v1 : Val \u03c4) (dv : Val (\u0394\u03c4 \u03c4)) (v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n relV3 nat (intV v1) (intV dv) (intV v2) n = dv + v1 \u2261 v2\n relV3 (\u03c3 \u21d2 \u03c4) (closure {\u03931} t1 \u03c11) (closure dt d\u03c1) (closure {\u03932} t2 \u03c12) n =\n \u03a3 (\u03931 \u2261 \u03932) \u03bb { refl \u2192\n \u2200 (k : \u2115) (k>= (\u03bb sv \u2192 eval t \u03c12 >>= apply sv))\n (sn3 + (tn3 + n3))\n \u2261 Done v2 0\n comp rewrite s2eq-adj | t2eq-adj = eq2\n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | Error | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | TimeOut | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Error | [ s1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | TimeOut | [ s1eq ]\n","old_contents":"-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- But to betray the eventual goal, I can also relate integer values with a\n-- change in the relation witness. That was a completely local change. But that\n-- might also be because we only have few primitives.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\" (ISAC below), POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\n-- In fact, this development is typed, hence some parts of the model are closer\n-- to Ahmed (ESOP 2006), \"Step-Indexed Syntactic Logical Relations for Recursive\n-- and Quantified Types\". But for many relevant aspects, the two papers are\n-- interchangeable.\n--\n-- The main insight from the ISAC paper missing from the other one is how to\n-- step-index a big-step semantics correctly: just ensure that the steps in the\n-- big-step semantics agree with the ones in the small-step semantics. *Then*\n-- everything just works with big-step semantics. Quite a few other details are\n-- fiddly, but those are the same in small-step semantics.\n--\n-- CHEATS:\n-- \"Fuctional big-step semantics\" requires an external termination proof for the\n-- semantics. There it is also mechanized, here it isn't. Worse, the same\n-- termination problem affects some lemmas about the semantics.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen import Data.Nat.Properties.Simple\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\ninfixr 20 _\u21d2_\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\nweaken \u0393\u2081\u227c\u0393\u2082 (const c) = const c\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (weaken-var \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n\n-- WARNING: ISAC's big-step semantics produces a step count as \"output\". But\n-- that would not help Agda establish termination. That's only a problem for a\n-- functional big-step semantics, not for a relational semantics.\n--\n-- So, instead, I tried to use a sort of writer monad: the interpreter gets fuel\n-- and returns the remaining fuel. That's the same trick as in \"functional\n-- big-step semantics\". That *makes* the function terminating, even though Agda\n-- cannot see this because it does not know that the returned fuel is no bigger.\n\n-- Since we focus for now on STLC, unlike that\n-- paper, we could avoid error values because we keep types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) n = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\neval (app s t) \u03c1 (suc n) = (eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)) n\n\neval-const-dec : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-const-dec (lit v) n0 .n0 refl = \u2264-refl\n\n{-# TERMINATING #-}\neval-dec : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-dec (const c) \u03c1 v n0 n1 eq = eval-const-dec c n0 n1 eq\neval-dec (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (app s t) \u03c1 v zero n1 ()\neval-dec (app s t) \u03c1 v (suc n0) n3 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv sn1 | [ seq ] with eval t \u03c1 sn1 | inspect (eval t \u03c1) sn1\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv@(closure st s\u03c1) sn1 | [ seq ] | (Done tv tn2) | [ teq ] = \u2264-step (\u2264-trans (\u2264-trans (eval-dec st _ _ _ _ eq) (eval-dec t _ _ _ _ teq)) (eval-dec s _ _ _ _ seq))\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | Error | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | TimeOut | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Error | [ seq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | TimeOut | [ seq ]\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1)\neval-const-mono (lit v) n0 .n0 refl = refl\n\n-- ARGH\n{-# TERMINATING #-}\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1)\neval-mono (const c) \u03c1 v n0 n1 eq = eval-const-mono c n0 n1 eq\neval-mono (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\neval-mono (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\neval-mono (app s t) \u03c1 v zero n1 ()\neval-mono (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-mono t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n\neval-adjust-plus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1)\neval-adjust-plus zero t \u03c1 v n0 n1 eq = eq\neval-adjust-plus (suc d) t \u03c1 v n0 n1 eq = eval-mono t \u03c1 v (d + n0) (d + n1) (eval-adjust-plus d t \u03c1 v n0 n1 eq)\n\neval-const-strengthen : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1) \u2192 evalConst c n0 \u2261 Done v n1\neval-const-strengthen (lit v) n0 .n0 refl = refl\n\n-- I started trying to prove eval-strengthen, which I appeal to informally\n-- below, but I gave up. I still guess the lemma is true but proving it looks\n-- too painful to bother.\n\n-- Without this lemma, I can't fully prove that this logical relation is\n-- equivalent to the original one.\n-- But this one works (well, at least up to the fundamental theorem, haven't\n-- attempted other lemmas), so it should be good enough.\n\n-- eval-mono-err : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 n \u2192 eval t \u03c1 n \u2261 Error \u2192 eval t \u03c1 (suc n) \u2261 Error\n-- eval-mono-err (const (lit x)) \u03c1 zero eq = {!!}\n-- eval-mono-err (const (lit x)) \u03c1 (suc n) eq = {!!}\n-- eval-mono-err (var x) \u03c1 n eq = {!!}\n-- eval-mono-err (app t t\u2081) \u03c1 n eq = {!!}\n-- eval-mono-err (abs t) \u03c1 n eq = {!!}\n\n-- -- eval t \u03c1 (suc n0) \u2261 Done v (suc n1) \u2192 eval t \u03c1 n0 \u2261 Done v n\n-- eval-aux : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 n \u2192 (\u03a3[ res0 \u2208 ErrVal \u03c4 ] eval t \u03c1 n \u2261 res0) \u00d7 (\u03a3[ resS \u2208 ErrVal \u03c4 ] eval t \u03c1 n \u2261 resS)\n-- eval-aux t \u03c1 n with\n-- eval t \u03c1 n | inspect (eval t \u03c1) n |\n-- eval t \u03c1 (suc n) | inspect (eval t \u03c1) (suc n)\n-- eval-aux t \u03c1 n | res0 | [ eq0 ] | (Done v1 n1) | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | res0 | [ eq0 ] | Error | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | Done v n1 | [ eq0 ] | TimeOut | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | Error | [ eq0 ] | TimeOut | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | TimeOut | [ eq0 ] | TimeOut | [ eq1 ] = (TimeOut , refl) , (TimeOut , refl)\n\n-- {-# TERMINATING #-}\n-- eval-strengthen : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-strengthen (const c) \u03c1 v n0 n1 eq = eval-const-strengthen c n0 n1 eq\n-- eval-strengthen (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq with eval s \u03c1 0 | inspect (eval s \u03c1) 0\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done sv sn1 | [ seq ] with eval-dec s \u03c1 sv 0 sn1 seq\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done sv .0 | [ seq ] | z\u2264n with eval t \u03c1 0 | inspect (eval t \u03c1) 0\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done sv _ | [ seq ] | z\u2264n | Done tv tn1 | [ teq ] with eval-dec t \u03c1 tv 0 tn1 teq\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done (closure st s\u03c1) _ | [ seq ] | z\u2264n | (Done tv .0) | [ teq ] | z\u2264n with eval-dec st _ v 0 (suc n1) eq\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done (closure st s\u03c1) _ | [ seq ] | z\u2264n | (Done tv _) | [ teq ] | z\u2264n | ()\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | Done sv _ | [ seq ] | z\u2264n | Error | [ teq ]\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | Done sv _ | [ seq ] | z\u2264n | TimeOut | [ teq ]\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | Error | [ seq ]\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | TimeOut | [ seq ]\n-- -- eval-dec s \u03c1\n-- -- {!eval-dec s \u03c1 ? (suc zero) (suc n1) !}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 (suc n0) | inspect (eval s \u03c1) (suc n0)\n-- -- eval-strengthen (app s t) \u03c1 v\u2081 (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 n0 = {!eval-strengthen s \u03c1 v n0 n1 seq !}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-strengthen t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | Error | [ seq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | TimeOut | [ seq ] = {!!}\n\n-- eval-adjust-minus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 {\u03c1 v} n0 n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-adjust-minus zero t n0 n1 eq = eq\n-- eval-adjust-minus (suc d) t n0 n1 eq = eval-adjust-minus d t n0 n1 (eval-strengthen t _ _ (d + n0) (d + n1) eq)\n\nimport Data.Integer as I\nopen I using (\u2124)\nmutual\n -- Warning: compared to Ahmed's papers, this definition for relT also requires\n -- t1 to be well-typed, not just t2.\n --\n -- This difference might affect the status of some proofs in Ahmed's papers,\n -- but that's not a problem here.\n\n -- Also: can't confirm this in any of the papers I'm using, but I'd guess that\n -- all papers using environments allow to relate closures with different\n -- implementations and different hidden environments.\n --\n -- To check if the proof goes through with equal context, I changed the proof.\n -- Now a proof that two closures are equivalent contains a proof that their\n -- typing contexts are equivalent. The changes were limited softawre\n -- engineering, the same proofs go through.\n\n -- This is not the same definition of relT, but it is equivalent.\n relT : \u2200 {\u03c4 \u0393} (t1 : Term \u0393 \u03c4) (t2 : Term \u0393 \u03c4) (\u03c11 : \u27e6 \u0393 \u27e7Context) (\u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\n -- This equation is a lemma in the original definition.\n relT t1 t2 \u03c11 \u03c12 zero = \u22a4\n -- To compare this definition, note that the original k is suc n here.\n relT {\u03c4} t1 t2 \u03c11 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n -- Originally we have 0 \u2264 j < k, so j < suc n, so k - j = suc n - j.\n -- It follows that 0 < k - j \u2264 k, hence suc n - j \u2264 suc n, or n - j \u2264 n.\n -- Here, instead of binding j we bind n-j = n - j, require n - j \u2264 n, and\n -- use suc n-j instead of k - j.\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n -- The next assumption is important. This still says that evaluation consumes j steps.\n -- Since j \u2264 n, it is OK to start evaluation with n steps.\n -- Starting with (suc n) and getting suc n-j is equivalent, per eval-mono\n -- and eval-strengthen. But in practice this version is easier to use.\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 0 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n -- Here, computing t2 is allowed to take an unbounded number of steps. Having to write a number at all is annoying.\n\n relV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n -- Show the proof still goes through if we relate clearly different values by\n -- inserting changes in the relation.\n -- There's no syntax to produce such changes, but you can add changes to the\n -- environment.\n relV nat (intV v1) (intV v2) n = \u03a3[ dv \u2208 \u2124 ] dv I.+ (I.+ v1) \u2261 (I.+ v2)\n relV (\u03c3 \u21d2 \u03c4) (closure {\u03931} t1 \u03c11) (closure {\u03932} t2 \u03c12) n =\n \u03a3 (\u03931 \u2261 \u03932) \u03bb { refl \u2192\n \u2200 (k : \u2115) (k\u2264n : k < n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\n }\n -- Above, in the conclusion, I'm not relating app (closure t1 \u03c11) v1 with app\n -- (closure t2 \u03c12) v2 (or some encoding of that that actually works), but the\n -- result of taking a step from that configuration. That is important, because\n -- both Pitts' \"Step-Indexed Biorthogonality: a Tutorial Example\" and\n -- \"Imperative Self-Adjusting Computation\" do the same thing (and point out it's\n -- important).\n\n \u0394\u03c4 : Type \u2192 Type\n \u0394\u03c4 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 (\u0394\u03c4 \u03c3) \u21d2 \u0394\u03c4 \u03c4\n \u0394\u03c4 nat = nat\n\n -- The original relation allows unrelated environments. However, while that is\n -- fine as a logical relation, it's not OK if we want to prove that validity\n -- agrees with oplus. We want a finer relation.\n -- Also: we still need to demand the actual environments to be related, and\n -- the bodies to match. Haven't done that yet. On the other hand, since we do want\n -- to allow for replacement changes, that would probably complicate the proof\n -- elsewhere.\n relT3 : \u2200 {\u03c4 \u0393 \u0394\u0393} (t1 : Term \u0393 \u03c4) (dt : Term \u0394\u0393 (\u0394\u03c4 \u03c4)) (t2 : Term \u0393 \u03c4) (\u03c11 : \u27e6 \u0393 \u27e7Context) (d\u03c1 : \u27e6 \u0394\u0393 \u27e7Context) (\u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\n relT3 t1 dt t2 \u03c11 d\u03c1 \u03c12 zero = \u22a4\n relT3 {\u03c4} t1 dt t2 \u03c11 d\u03c1 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 0 \u00d7\n \u03a3[ dv \u2208 Val (\u0394\u03c4 \u03c4) ] \u03a3[ dn \u2208 \u2115 ] eval dt d\u03c1 dn \u2261 Done dv 0 \u00d7\n relV3 \u03c4 v1 dv v2 (suc n-j)\n\n -- Weakening in this definition is going to be annoying to use. And having to\n -- construct terms is ugly.\n\n -- Weakening could be avoided if we use a separate language of change terms\n -- with two environments, and with a dclosure binding two variables at once,\n -- and so on.\n relV3 : \u2200 \u03c4 (v1 : Val \u03c4) (dv : Val (\u0394\u03c4 \u03c4)) (v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n relV3 nat (intV v1) (intV dv) (intV v2) n = dv + v1 \u2261 v2\n relV3 (\u03c3 \u21d2 \u03c4) (closure {\u03931} t1 \u03c11) (closure dt d\u03c1) (closure {\u03932} t2 \u03c12) n =\n \u03a3 (\u03931 \u2261 \u03932) \u03bb { refl \u2192\n \u2200 (k : \u2115) (k\u2264n : k < n) v1 dv v2 \u2192\n relV3 \u03c3 v1 dv v2 k \u2192\n relT3 t1 (app (weaken (drop (\u0394\u03c4 \u03c3) \u2022 \u227c-refl) dt) (var this)) t2 (v1 \u2022 \u03c11) (dv \u2022 v1 \u2022 d\u03c1) (v2 \u2022 \u03c12) k\n }\n\n -- Relate \u03bb x \u2192 0 and \u03bb x \u2192 1 at any step count.\n example1 : \u2200 n \u2192 relV (nat \u21d2 nat) (closure (const (lit 0)) \u2205) (closure (const (lit 1)) \u2205) n\n example1 n = refl ,\n \u03bb { zero k\u2264n v1 v2 x \u2192 tt\n ; (suc k) k\u2264n v1 v2 x .(intV 0) .k n-j\u2264n refl \u2192 intV 1 , 0 , refl , (I.+ 1 , refl)\n }\n\n -- Relate \u03bb x \u2192 0 and \u03bb x \u2192 x at any step count.\n example2 : \u2200 n \u2192 relV (nat \u21d2 nat) (closure (const (lit 0)) \u2205) (closure (var this) \u2205) n\n example2 n = refl ,\n \u03bb { zero k\u2264n v1 v2 x \u2192 tt\n ; (suc k) k\u2264n (intV v1) (intV v2) x .(intV 0) .k n-j\u2264n refl \u2192 intV v2 , 0 , refl , (I.+ v2 , cong I.+_ (+-right-identity v2))\n }\n\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono m n m\u2264n nat (intV v1) (intV v2) vv = vv\nrelV-mono m n m\u2264n (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (refl , ff) = refl , \u03bb k k\u2264m \u2192 ff k (\u2264-trans k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 .n n-j\u2264n refl = v2 , zero , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\nlt1 : \u2200 {k n} \u2192 k < n \u2192 k \u2264 n\nlt1 (s\u2264s p) = \u2264-step p\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\nfundamental (const (lit v)) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(intV v) .n n-j\u2264n refl = intV v , zero , refl , I.+ zero , refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(closure t \u03c11) .n n-j\u2264n refl = closure t \u03c12 , zero , refl , refl , \u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k (suc n) (lt1 k\u2264n) _ _ _ \u03c1\u03c1)\nfundamental (app s t) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq with eval s \u03c11 n | inspect (eval s \u03c11) n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done sv1 n1 | [ s1eq ] with eval-dec s _ _ n n1 s1eq | eval t \u03c11 n1 | inspect (eval t \u03c11) n1\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done (closure st1 s\u03c11) n1 | [ s1eq ] | n1\u2264n | Done tv1 n2 | [ t1eq ] with eval-dec t _ _ n1 n2 t1eq\n... | n2\u2264n1 with fundamental s (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) (suc n1) (s\u2264s n1\u2264n) (eval-mono s \u03c11 (closure st1 s\u03c11) n n1 s1eq)\n | fundamental t (suc (suc n1)) \u03c11 \u03c12 (rel\u03c1-mono (suc (suc n1)) (suc (suc n)) (s\u2264s (s\u2264s n1\u2264n)) _ _ _ \u03c1\u03c1) tv1 (suc n2) (s\u2264s n2\u2264n1) (eval-mono t \u03c11 tv1 n1 n2 t1eq)\n... | sv2@(closure st2 s\u03c12) , sn3 , s2eq , refl , svv | tv2 , tn3 , t2eq , tvv with svv (suc n2) (s\u2264s (s\u2264s n2\u2264n1)) tv1 tv2 (relV-mono (suc n2) (suc (suc n2)) (s\u2264s (n\u22641+n n2)) _ tv1 tv2 tvv) v1 n-j (eval-dec st1 _ _ _ _ eq) eq\n... | v2 , n3 , eq2 , vv = v2 , suc (sn3 + (tn3 + n3)) , comp , vv\n where\n s2eq-adj : eval s \u03c12 (sn3 + (tn3 + n3)) \u2261 Done (closure st2 s\u03c12) (tn3 + n3)\n s2eq-adj rewrite +-comm sn3 (tn3 + n3)| cong (Done (closure st2 s\u03c12)) (sym (+-right-identity (tn3 + n3))) = eval-adjust-plus (tn3 + n3) s _ sv2 _ _ s2eq\n t2eq-adj : eval t \u03c12 (tn3 + n3) \u2261 Done tv2 n3\n t2eq-adj rewrite +-comm tn3 n3 | cong (Done tv2) (sym (+-right-identity n3)) = eval-adjust-plus n3 t _ tv2 _ _ t2eq\n\n comp : (eval s \u03c12 >>= (\u03bb sv \u2192 eval t \u03c12 >>= apply sv))\n (sn3 + (tn3 + n3))\n \u2261 Done v2 0\n comp rewrite s2eq-adj | t2eq-adj = eq2\n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | Error | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | TimeOut | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Error | [ s1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | TimeOut | [ s1eq ]\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"bae5c62464397c761ac1d7396e349985e33b44af","subject":"Desc stratified model: put implicits up to DescDConst. Troubles starting.","message":"Desc stratified model: put implicits up to DescDConst. Troubles starting.\n","repos":"mietek\/epigram2,mietek\/epigram2,larrytheliquid\/pigit","old_file":"models\/IDesc.agda","new_file":"models\/IDesc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l : Level}{A B : Set l}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l : Level}{A B C : Set l}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {l : Level}{A B : Set l}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set l) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set l} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set l}(R : I -> IDesc I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set l}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : (l : Level) -> Set l -> DescDConst -> IDesc Unit\ndescDChoice _ I lvar = const (lift I)\ndescDChoice x _ lconst = const (Set x)\ndescDChoice _ _ lprod = prod (var Void) (var Void)\ndescDChoice x _ lpi = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice x _ lsigma = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : (l : Level)(I : Set l) -> IDesc Unit\ndescD x I = sigma DescDConst (descDChoice x I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (\\_ -> descD x I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst {l = suc x}\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst {l = suc x}\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst {l = suc x}\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst {l = suc x}\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst {l = suc x}\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n \n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\n-- desc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\n-- descD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\n-- IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l\n\n-- cases : (l : Level)\n-- (I : Set l)\n-- (xs : desc (suc l) (lift I) (descD l I) (IMu (suc l) Unit (\\ _ -> descD l I)))\n-- (hs : desc l I (box l I (descD l I) (IMu l Unit (\u03bb _ -> descD l I)) xs) (\u03bb _ -> IDesc (suc l) I)) ->\n-- IDesc (suc l) I\n-- cases x I ( lvar , i ) hs = var i\n-- cases x I ( lconst , X ) hs = const X\n-- cases x I ( lprod , (D , D') ) ( d , d' ) = prod d d'\n-- cases x I ( lpi , ( S , T ) ) hs = pi S hs\n-- cases x I ( lsigma , ( S , T ) ) hs = sigma S hs\n\n-- phi : {I : Set} -> IDescl I -> IDesc I\n-- phi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l : Level}{A B : Set l}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l : Level}{A B C : Set l}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {l : Level}{A B : Set l}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (l : Level)(I : Set l) : Set (suc l) where\n var : I -> IDesc l I\n const : Set l -> IDesc l I\n prod : IDesc l I -> IDesc l I -> IDesc l I\n sigma : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n pi : (S : Set l) -> (S -> IDesc l I) -> IDesc l I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\ndesc _ I (var i) P = P i\ndesc _ I (const X) P = X\ndesc x I (prod D D') P = desc x I D P * desc x I D' P\ndesc x I (sigma S T) P = Sigma S (\\s -> desc x I (T s) P)\ndesc x I (pi S T) P = (s : S) -> desc x I (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l where\n con : desc l I (R i) (\\j -> IMu l I R j) -> IMu l I R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : (l : Level)(I : Set l)(D : IDesc l I)(P : I -> Set l) -> desc l I D P -> IDesc l (Sigma I P)\nbox _ I (var i) P x = var (i , x)\nbox _ I (const X) P x = const X\nbox x I (prod D D') P (d , d') = prod (box x I D P d) (box x I D' P d')\nbox x I (sigma S T) P (a , b) = box x I (T a) P b\nbox x I (pi S T) P f = pi S (\\s -> box x I (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim (l : Level)\n (I : Set l)\n (R : I -> IDesc l I)\n (P : Sigma I (IMu l I R) -> Set l)\n (m : (i : I)\n (xs : desc l I (R i) (IMu l I R))\n (hs : desc l (Sigma I (IMu l I R)) (box l I (R i) (IMu l I R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu l I R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc l I) -> \n (xs : desc l I D (IMu l I R)) -> \n desc l (Sigma I (IMu l I R)) (box l I D (IMu l I R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : (l : Level)\n (I : Set l)\n (R : I -> IDesc l I)\n (P : Sigma I (IMu l I R) -> Set l)\n (m : (i : I)\n (xs : desc l I (R i) (IMu l I R))\n (hs : desc l (Sigma I (IMu l I R)) (box l I (R i) (IMu l I R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu l I R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst (l : Level) : Set l where\n lvar : DescDConst l\n lconst : DescDConst l\n lprod : DescDConst l\n lpi : DescDConst l\n lsigma : DescDConst l\n\ndescDChoice : (l : Level) -> Set l -> DescDConst (suc l) -> IDesc (suc l) Unit\ndescDChoice _ I lvar = const (lift I)\ndescDChoice x _ lconst = const (Set x)\ndescDChoice _ _ lprod = prod (var Void) (var Void)\ndescDChoice x _ lpi = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice x _ lsigma = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\ndescD x I = sigma (DescDConst (suc x)) (descDChoice x I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (suc x) Unit (\\_ -> descD x I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst (suc x)\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst (suc x)\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst (suc x)\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst (suc x)\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst (suc x)\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n \n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3a71a35e614110c17cabf82ec6bcb4b5ead5c9b8","subject":"Desc@ICFP: update model, adding just Variable from the thing containing Values.","message":"Desc@ICFP: update model, adding just Variable from the thing containing Values.\n","repos":"mietek\/epigram2,mietek\/epigram2,larrytheliquid\/pigit","old_file":"models\/IDescTT.agda","new_file":"models\/IDescTT.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n pair : Type -> Type -> Type\n\n--********************************\n-- Typed expressions\n--********************************\n\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\nVal (pair x y) = (Val x) * (Val y)\n\n\n-- Fix menu:\nexprFixMenu : FixMenu Type\nexprFixMenu = ( consE (consE nilE) , \n \\ty -> (const (Val ty), -- Val t\n (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void)))\n\n-- Indexed menu:\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\nchoiceMenu (pair x y) = nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\nchoiceDessert (pair x y) = Void\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\n\n-- Expression:\nexpr : TagIDesc Type\nexpr = exprFixMenu , exprSensitiveMenu\n\nexprIDesc : TagIDesc Type -> (Type -> IDesc Type)\nexprIDesc D = toIDesc Type D\n\n\n--********************************\n-- Closed terms\n--********************************\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : {ty : Type} -> IMu closeTerm ty -> Val ty\neval {ty} term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nEmpty : Type -> Set\nEmpty _ = Zero\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (expr ** Empty)\n\n--********************************\n-- Closed term' evaluation\n--********************************\n\neval' : {ty : Type} -> IMu closeTerm' ty -> Val ty\neval' {ty} term = cata Type closeTerm' Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm' ty |] Val -> Val ty\n evalOneStep _ (EZe , ())\n evalOneStep _ (ESu EZe , t) = t\n evalOneStep _ ((ESu (ESu EZe)) , (true , ( x , _))) = x\n evalOneStep _ ((ESu (ESu EZe)) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu (ESu EZe))) , (x , y)) = plus x y\n evalOneStep nat (((ESu (ESu (ESu (ESu ()))))) , t) \n evalOneStep bool ((ESu (ESu (ESu EZe))) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu (ESu ())))) , _) \n evalOneStep (pair x y) (ESu (ESu (ESu ())) , _)\n\n\n--********************************\n-- Open terms\n--********************************\n\ndata Vec (A : Set) : Nat -> Set where\n vnil : Vec A ze\n vcons : {n : Nat} -> A -> Vec A n -> Vec A (su n)\n\ndata Fin : Nat -> Set where\n fze : {n : Nat} -> Fin (su n)\n fsu : {n : Nat} -> Fin n -> Fin (su n)\n\nContext : Nat -> Set\nContext n = Vec (Sigma Type (\\ty -> IMu closeTerm' ty)) n\n\ntypeAt : {n : Nat}(c : Context n) -> Fin n -> Type\ntypeAt {ze} c ()\ntypeAt {.(su n)} (vcons x xs) (fze {n}) = fst x\ntypeAt {.(su n)} (vcons x xs) (fsu {n} y) = typeAt xs y\n\nlookup : {n : Nat}(c : Context n)(i : Fin n) -> IMu closeTerm' (typeAt c i)\nlookup {ze} c ()\nlookup {su _} (vcons x _) fze = snd x\nlookup {su _} (vcons _ xs) (fsu y) = lookup xs y\n\n\nVar : {n : Nat} -> Context n -> Type -> Set\nVar {n} c ty = Sigma (Fin n) (\\i -> typeAt c i == ty)\n\nopenTerm : {n : Nat} -> Context n -> Type -> IDesc Type\nopenTerm c = toIDesc Type (expr ** (Var c))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : {n : Nat}\n {context : Context n}\n (ty : Type) ->\n Var context ty ->\n IMu closeTerm' ty\ndischarge {n} {c} ty variable = subst (cong (IMu closeTerm') (snd variable)) (lookup c (fst variable))\n\nsubstExpr : {n : Nat}\n {ty : Type}\n (context : Context n)\n (sigma : (ty : Type) ->\n Var context ty ->\n IMu closeTerm' ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm' ty\nsubstExpr {n} {ty} c sig term = \n substI (Var c) Empty expr sig ty term\n\nevalOpen : {n : Nat}{ty : Type}\n (context : Context n) ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen context tm = eval' (substExpr context discharge tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- V 0 :-> true, V 1 :-> 2, V 2 :-> ( false , 1 )\ntestContext : Context _\ntestContext = vcons (bool , con ((ESu EZe) , true )) \n (vcons (nat , con ((ESu EZe) , su (su ze)) ) \n (vcons (pair bool nat , con ((ESu EZe) , ( false , su ze )))\n vnil))\n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , ( fsu fze , refl ) )\n\ntestSubst1 : IMu closeTerm' nat\ntestSubst1 = substExpr testContext \n discharge \n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con ((ESu (ESu (ESu EZe))) , (con ((ESu EZe) , (su ze)) , con ( EZe , (fsu fze , refl) )) )\n\ntestSubst2 : IMu closeTerm' nat\ntestSubst2 = substExpr testContext \n discharge \n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext test2\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu (ESu EZe) , (con (EZe , (fze , refl)) ,\n (con (EZe , (fsu fze , refl)) ,\n con (ESu EZe , ze))))\n\ntestSubst3 : IMu closeTerm' nat\ntestSubst3 = substExpr testContext \n discharge \n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext test3\n\n-- V 2\ntest4 : IMu (openTerm testContext) (pair bool nat)\ntest4 = con (EZe , ( fsu (fsu fze) , refl ) )\n\ntestSubst4 : IMu closeTerm' (pair bool nat)\ntestSubst4 = substExpr testContext \n discharge \n test4\n-- = (false , 1)\ntestEval4 : Val (pair bool nat)\ntestEval4 = evalOpen testContext test4\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\nsubst : forall {x y} -> x == y -> x -> y\nsubst refl x = x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n pair : Type -> Type -> Type\n\n--********************************\n-- Typed expressions\n--********************************\n\n-- Fix menu:\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty), -- Val t\n (prod (var bool) (prod (var ty) (var ty)), -- if b then t1 else t2\n Void)))\n\n-- Indexed menu:\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\nchoiceMenu (pair x y) = nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\nchoiceDessert (pair x y) = Void\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\n\n-- Expression:\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\nVal (pair x y) = (Val x) * (Val y)\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), \n Void))\n\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\nchoiceFreeMenu (pair x y) = nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void)\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void )\nchoiceFreeDessert (pair x y) = Void\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (exprFree ** Val)\n\n--********************************\n-- Closed term' evaluation\n--********************************\n\neval' : {ty : Type} -> IMu closeTerm' ty -> Val ty\neval' {ty} term = cata Type closeTerm' Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm' ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n evalOneStep (pair x y) (ESu (ESu ()) , _)\n\n\n--********************************\n-- Open terms\n--********************************\n\ndata Vec (A : Set) : Nat -> Set where\n vnil : Vec A ze\n vcons : {n : Nat} -> A -> Vec A n -> Vec A (su n)\n\ndata Fin : Nat -> Set where\n fze : {n : Nat} -> Fin (su n)\n fsu : {n : Nat} -> Fin n -> Fin (su n)\n\nContext : Nat -> Set\nContext n = Vec (Sigma Type (\\ty -> IMu closeTerm' ty)) n\n\ntypeAt : {n : Nat}(c : Context n) -> Fin n -> Type\ntypeAt {ze} c ()\ntypeAt {.(su n)} (vcons x xs) (fze {n}) = fst x\ntypeAt {.(su n)} (vcons x xs) (fsu {n} y) = typeAt xs y\n\nlookup : {n : Nat}(c : Context n)(i : Fin n) -> IMu closeTerm' (typeAt c i)\nlookup {ze} c ()\nlookup {su _} (vcons x _) fze = snd x\nlookup {su _} (vcons _ xs) (fsu y) = lookup xs y\n\n\nVar : {n : Nat} -> Context n -> Type -> Set\nVar {n} c ty = Sigma (Fin n) (\\i -> typeAt c i == ty)\n\nopenTerm : {n : Nat} -> Context n -> Type -> IDesc Type\nopenTerm c = toIDesc Type (exprFree ** (\\ty -> Val ty + Var c ty))\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : {n : Nat}\n {context : Context n}\n (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm' ty\ndischarge ty (l value) = con (EZe , value)\ndischarge {n} {c} ty (r variable) = subst (cong (IMu closeTerm') (snd variable)) (lookup c (fst variable))\n\nsubstExpr : {n : Nat}\n {ty : Type}\n (context : Context n)\n (sigma : (ty : Type) ->\n (Val ty + Var context ty) ->\n IMu closeTerm' ty) ->\n IMu (openTerm context) ty ->\n IMu closeTerm' ty\nsubstExpr {n} {ty} c sig term = \n substI (\\ty -> Val ty + Var c ty) Val exprFree sig ty term\n\nevalOpen : {n : Nat}{ty : Type}\n (context : Context n) ->\n IMu (openTerm context) ty ->\n Val ty\nevalOpen context tm = eval' (substExpr context discharge tm)\n\n--********************************\n-- Tests\n--********************************\n\n-- V 0 :-> true, V 1 :-> 2, V 2 :-> ( false , 1 )\ntestContext : Context _\ntestContext = vcons (bool , con (EZe , true )) \n (vcons (nat , con (EZe , su (su ze)) ) \n (vcons (pair bool nat , con (EZe , ( false , su ze )))\n vnil))\n\n-- V 1\ntest1 : IMu (openTerm testContext) nat\ntest1 = con (EZe , r ( fsu fze , refl ) )\n\ntestSubst1 : IMu closeTerm' nat\ntestSubst1 = substExpr testContext \n discharge \n test1\n-- = 2\ntestEval1 : Val nat\ntestEval1 = evalOpen testContext test1\n\n-- add 1 (V 1)\ntest2 : IMu (openTerm testContext) nat\ntest2 = con (ESu (ESu EZe) , (con (EZe , l (su ze)) , con ( EZe , r (fsu fze , refl) )) )\n\ntestSubst2 : IMu closeTerm' nat\ntestSubst2 = substExpr testContext \n discharge \n test2\n\n-- = 3\ntestEval2 : Val nat\ntestEval2 = evalOpen testContext test2\n\n-- if (V 0) then (V 1) else 0\ntest3 : IMu (openTerm testContext) nat\ntest3 = con (ESu EZe , (con (EZe , r (fze , refl)) ,\n (con (EZe , r (fsu fze , refl)) ,\n con (EZe , l ze))))\n\ntestSubst3 : IMu closeTerm' nat\ntestSubst3 = substExpr testContext \n discharge \n test3\n\n-- = 2\ntestEval3 : Val nat\ntestEval3 = evalOpen testContext test3\n\n-- V 2\ntest4 : IMu (openTerm testContext) (pair bool nat)\ntest4 = con (EZe , r ( fsu (fsu fze) , refl ) )\n\ntestSubst4 : IMu closeTerm' (pair bool nat)\ntestSubst4 = substExpr testContext \n discharge \n test4\n-- = (false , 1)\ntestEval4 : Val (pair bool nat)\ntestEval4 = evalOpen testContext test4\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"37a612ebc77389e35e80565062d1ac398fe5c5fd","subject":"Added doc.","message":"Added doc.\n\nIgnore-this: 364f24408c8fdd04184a133e22e3c7a9\n\ndarcs-hash:20101102124050-3bd4e-94a179f58a55dba886d13fbc0b4c89f2d268ef4b.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/LTC\/Data\/Stream\/Bisimilarity.agda","new_file":"src\/LTC\/Data\/Stream\/Bisimilarity.agda","new_contents":"------------------------------------------------------------------------------\n-- Bisimilarity relation on LTC streams\n------------------------------------------------------------------------------\n\nmodule LTC.Data.Stream.Bisimilarity where\n\nopen import LTC.Minimal\n\ninfix 4 _\u2248_\n\n------------------------------------------------------------------------------\n-- Because the LTC is a first-order theory, we define a first-order\n-- version of the bisimilarity relation.\n\n-- The bisimilarity relation.\npostulate\n _\u2248_ : D \u2192 D \u2192 Set\n\n-- The bisimilarity relation is a post-fixed point of a bisimilar\n-- relation BISI (see below).\npostulate\n -\u2248-gfp\u2081 : {xs ys : D} \u2192 xs \u2248 ys \u2192\n \u2203D (\u03bb x' \u2192\n \u2203D (\u03bb xs' \u2192\n \u2203D (\u03bb ys' \u2192 xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys')))\n{-# ATP axiom -\u2248-gfp\u2081 #-}\n\n-- The bisimilarity relation is the greatest post-fixed point of a\n-- bisimilar relation BISI (see below).\n\n-- N.B. This is a second-order axiom. In the proofs, we *must* use an\n-- axiom scheme instead. Therefore, we do not add this postulate as an\n-- ATP axiom.\npostulate\n -\u2248-gfp\u2082 : {_R_ : D \u2192 D \u2192 Set} \u2192\n -- R is a post-fixed point of BISI.\n ({xs ys : D} \u2192 xs R ys \u2192\n \u2203D (\u03bb x' \u2192\n \u2203D (\u03bb xs' \u2192\n \u2203D (\u03bb ys' \u2192 xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys')))) \u2192\n -- \u2248 is greater than R.\n {xs ys : D} \u2192 xs R ys \u2192 xs \u2248 ys\n\nmodule Bisimulation where\n -- In LTC we won't use the bisimilar relation BISI. This module is\n -- only for illustrative purposes.\n\n -- Adapted from [1]. In this paper the authors use the name\n -- 'as (R :: R') bs' (p. 310).\n -- N.B. This definition should work on streams.\n\n -- [1] Peter Dybjer and Herbert Sander. A functional programming\n -- approach to the specification and verification of concurrent\n -- systems. Formal Aspects of Computing, 1:303\u2013319, 1989.\n\n -- The bisimilar relation.\n BISI : (D \u2192 D \u2192 Set) \u2192 D \u2192 D \u2192 Set\n BISI _R_ xs ys =\n \u2203D (\u03bb x' \u2192\n \u2203D (\u03bb xs' \u2192\n \u2203D (\u03bb ys' \u2192\n xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys')))\n\n -- The bisimilarity relation is a post-fixed point of BISI.\n -\u2248\u2192BISI\u2248 : {xs ys : D} \u2192 xs \u2248 ys \u2192 BISI _\u2248_ xs ys\n -\u2248\u2192BISI\u2248 = -\u2248-gfp\u2081\n\n -- The bisimilarity relation is the greatest post-fixed point of BISI.\n R\u2192BISI-R\u2192R\u2192\u2248 : {_R_ : D \u2192 D \u2192 Set} \u2192\n -- R is a post-fixed point of BISI.\n ({xs ys : D} \u2192 xs R ys \u2192 BISI _R_ xs ys) \u2192\n -- \u2248 is greater than R.\n {xs ys : D} \u2192 xs R ys \u2192 xs \u2248 ys\n R\u2192BISI-R\u2192R\u2192\u2248 = -\u2248-gfp\u2082\n","old_contents":"------------------------------------------------------------------------------\n-- Bisimilarity relation on LTC streams\n------------------------------------------------------------------------------\n\nmodule LTC.Data.Stream.Bisimilarity where\n\nopen import LTC.Minimal\n\ninfix 4 _\u2248_\n\n------------------------------------------------------------------------------\n-- Because the LTC is a first-order theory, we define a first-order\n-- version of the bisimilarity relation.\n\n-- The bisimilarity relation.\npostulate\n _\u2248_ : D \u2192 D \u2192 Set\n\n-- The bisimilarity relation is a post-fixed point of a bisimilar\n-- relation BISI (see below).\npostulate\n -\u2248-gfp\u2081 : {xs ys : D} \u2192 xs \u2248 ys \u2192\n \u2203D (\u03bb x' \u2192\n \u2203D (\u03bb xs' \u2192\n \u2203D (\u03bb ys' \u2192 xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys')))\n{-# ATP axiom -\u2248-gfp\u2081 #-}\n\n-- The bisimilarity relation is the greatest post-fixed point of a\n-- bisimilar relation BISI (see below).\n\n-- N.B. This is a second-order axiom. In the proofs, we *must* use an\n-- axiom scheme instead. Therefore, we do not add this postulate as an\n-- ATP axiom.\npostulate\n -\u2248-gfp\u2082 : {_R_ : D \u2192 D \u2192 Set} \u2192\n -- R is a post-fixed point of BISI.\n ({xs ys : D} \u2192 xs R ys \u2192\n \u2203D (\u03bb x' \u2192\n \u2203D (\u03bb xs' \u2192\n \u2203D (\u03bb ys' \u2192 xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys')))) \u2192\n -- \u2248 is the greater than R.\n {xs ys : D} \u2192 xs R ys \u2192 xs \u2248 ys\n\nmodule Bisimulation where\n -- In LTC we won't use the bisimilar relation BISI. This module is\n -- only for illustrative purposes.\n\n -- Adapted from [1]. In this paper the authors use the name\n -- 'as (R :: R') bs' (p. 310).\n -- N.B. This definition should work on streams.\n\n -- [1] Peter Dybjer and Herbert Sander. A functional programming\n -- approach to the specification and verification of concurrent\n -- systems. Formal Aspects of Computing, 1:303\u2013319, 1989.\n\n -- The bisimilar relation.\n BISI : (D \u2192 D \u2192 Set) \u2192 D \u2192 D \u2192 Set\n BISI _R_ xs ys =\n \u2203D (\u03bb x' \u2192\n \u2203D (\u03bb xs' \u2192\n \u2203D (\u03bb ys' \u2192\n xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys')))\n\n -- The bisimilarity relation is a post-fixed point of BISI.\n -\u2248\u2192BISI\u2248 : {xs ys : D} \u2192 xs \u2248 ys \u2192 BISI _\u2248_ xs ys\n -\u2248\u2192BISI\u2248 = -\u2248-gfp\u2081\n\n -- The bisimilarity relation is the greatest post-fixed point of BISI.\n R\u2192BISI-R\u2192R\u2192\u2248 : {_R_ : D \u2192 D \u2192 Set} \u2192\n ({xs ys : D} \u2192 xs R ys \u2192 BISI _R_ xs ys) \u2192\n {xs ys : D} \u2192 xs R ys \u2192 xs \u2248 ys\n R\u2192BISI-R\u2192R\u2192\u2248 = -\u2248-gfp\u2082\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"909af4f0d102082a0c7a29516dca9a8436fcb3ad","subject":"Added mirror-Tree.","message":"Added mirror-Tree.\n\nIgnore-this: fa2b49ec2e9ab0b33a40ad384b5817f3\n\ndarcs-hash:20110220170905-3bd4e-4e7f837e6d45c5a8ec44cfcf227c9795a5715811.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/LTC\/Program\/Mirror\/Tree\/Closures.agda","new_file":"src\/LTC\/Program\/Mirror\/Tree\/Closures.agda","new_contents":"------------------------------------------------------------------------------\n-- Properties related with the closures of the tree type\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-termination-check #-}\n\nmodule LTC.Program.Mirror.Tree.Closures where\n\nopen import LTC.Base\n\nopen import LTC.Data.List\n\nopen import LTC.Program.Mirror.Mirror\nopen import LTC.Program.Mirror.ListTree.Closures\n\nopen import LTC.Relation.Binary.EqReasoning\n\n------------------------------------------------------------------------------\n\nmirror-Tree : \u2200 {t} \u2192 Tree t \u2192 Tree (mirror \u00b7 t)\nmirror-Tree (treeT d nilLT) =\n subst Tree (sym (mirror-eq d [])) (treeT d helper\u2082)\n where\n helper\u2081 : rev (map mirror []) [] \u2261 []\n helper\u2081 =\n begin\n rev (map mirror []) []\n \u2261\u27e8 subst (\u03bb x \u2192 rev (map mirror []) [] \u2261 rev x [])\n (map-[] mirror)\n refl\n \u27e9\n rev [] []\n \u2261\u27e8 rev-[] [] \u27e9\n []\n \u220e\n\n helper\u2082 : ListTree (rev (map mirror []) [])\n helper\u2082 = subst ListTree (sym helper\u2081) nilLT\n\nmirror-Tree (treeT d (consLT {t} {ts} Tt LTts)) =\n subst Tree (sym (mirror-eq d (t \u2237 ts))) (treeT d helper)\n\n where\n helper : ListTree (reverse (map mirror (t \u2237 ts)))\n helper = reverse-ListTree (map-ListTree mirror mirror-Tree (consLT Tt LTts))\n","old_contents":"------------------------------------------------------------------------------\n-- Properties related with the closures of the tree type\n------------------------------------------------------------------------------\n\nmodule LTC.Program.Mirror.Tree.Closures where\n\nopen import LTC.Base\n\nopen import LTC.Program.Mirror.Mirror\n\nopen import LTC.Data.List\n\n------------------------------------------------------------------------------\n\n-- TODO: To remove the postulate\npostulate\n mirror-Tree : \u2200 {t} \u2192 Tree t \u2192 Tree (mirror \u00b7 t)\n\n-- mirror-Tree : \u2200 {t} \u2192 Tree t \u2192 Tree (mirror \u00b7 t)\n-- mirror-Tree (treeT d nilLT) =\n-- subst Tree (sym (mirror-eq d [])) (treeT d helper\u2082)\n-- where\n-- helper\u2081 : rev (map mirror []) [] \u2261 []\n-- helper\u2081 =\n-- begin\n-- rev (map mirror []) []\n-- \u2261\u27e8 subst (\u03bb x \u2192 rev (map mirror []) [] \u2261 rev x [])\n-- (map-[] mirror)\n-- refl\n-- \u27e9\n-- rev [] []\n-- \u2261\u27e8 rev-[] [] \u27e9\n-- []\n-- \u220e\n\n-- helper\u2082 : ListTree (rev (map mirror []) [])\n-- helper\u2082 = subst ListTree (sym helper\u2081) nilLT\n\n-- mirror-Tree (treeT d (consLT {t} {ts} Tt LTts)) =\n-- subst Tree (sym (mirror-eq d (t \u2237 ts))) (treeT d helper)\n\n-- where\n-- helper : ListTree (reverse (map mirror (t \u2237 ts)))\n-- helper = rev-ListTree (map-ListTree mirror {!!} (consLT Tt LTts)) nilLT\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"038ac179b72512cbb57611d217eb7188f986ee3e","subject":"Cosmetic change.","message":"Cosmetic change.\n\nIgnore-this: a6d436fde1c00a6bec397a3db93e5eb4\n\ndarcs-hash:20120208145259-3bd4e-53e88003a7bb73c14766634e95a9aa210713ab4a.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/PA\/Axiomatic\/Base.agda","new_file":"src\/PA\/Axiomatic\/Base.agda","new_contents":"------------------------------------------------------------------------------\n-- Axiomatic Peano arithmetic base\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule PA.Axiomatic.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfixl 10 _*_\ninfixl 9 _+_\ninfix 7 _\u2250_\n\n------------------------------------------------------------------------------\n-- PA universe\nopen import Common.Universe public renaming ( D to \u2115 )\n\n-- Logical constants\nopen import Common.LogicalConstants public\n\n-- Non-logical constants\npostulate\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n _+_ _*_ : \u2115 \u2192 \u2115 \u2192 \u2115\n\n-- The PA equality.\n-- N.B. The symbol _\u2261_ should not be used because it is hard-coded by\n-- the program agda2atp as the ATPs equality.\npostulate _\u2250_ : \u2115 \u2192 \u2115 \u2192 Set\n\n-- Proper axioms\n-- (From Elliott Mendelson. Introduction to mathematical\n-- logic. Chapman & Hall, 4th edition, 1997, p. 155)\n\n-- N.B. We make the recursion in the first argument for _+_ and _*_.\n\n-- S\u2081. m = n \u2192 m = o \u2192 n = o\n-- S\u2082. m = n \u2192 succ m = succ n\n-- S\u2083. 0 \u2260 succ n\n-- S\u2084. succ m = succ n \u2192 m = n\n-- S\u2085. 0 + n = n\n-- S\u2086. succ m + n = succ (m + n)\n-- S\u2087. 0 * n = 0\n-- S\u2088. succ m * n = (m * n) + m\n-- S\u2089. P(0) \u2192 (\u2200n.P(n) \u2192 P(succ n)) \u2192 \u2200n.P(n), for any wf P(n) of PA.\n\npostulate\n S\u2081 : \u2200 {m n o} \u2192 m \u2250 n \u2192 m \u2250 o \u2192 n \u2250 o\n S\u2082 : \u2200 {m n} \u2192 m \u2250 n \u2192 succ m \u2250 succ n\n S\u2083 : \u2200 {n} \u2192 \u00ac (zero \u2250 succ n)\n S\u2084 : \u2200 {m n} \u2192 succ m \u2250 succ n \u2192 m \u2250 n\n S\u2085 : \u2200 n \u2192 zero + n \u2250 n\n S\u2086 : \u2200 m n \u2192 succ m + n \u2250 succ (m + n)\n S\u2087 : \u2200 n \u2192 zero * n \u2250 zero\n S\u2088 : \u2200 m n \u2192 succ m * n \u2250 n + m * n\n{-# ATP axiom S\u2081 S\u2082 S\u2083 S\u2084 S\u2085 S\u2086 S\u2087 S\u2088 #-}\n\n-- The axiom S\u2089 is a higher-order one, therefore we do not translate\n-- it as an ATP axiom.\npostulate S\u2089 : (P : \u2115 \u2192 Set) \u2192 P zero \u2192 (\u2200 n \u2192 P n \u2192 P (succ n)) \u2192 \u2200 n \u2192 P n\n","old_contents":"------------------------------------------------------------------------------\n-- Axiomatic Peano arithmetic base\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule PA.Axiomatic.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfixl 10 _*_\ninfixl 9 _+_\ninfix 7 _\u2250_\n\n------------------------------------------------------------------------------\n-- PA universe\nopen import Common.Universe public renaming ( D to \u2115 )\n\n-- Logical constants\nopen import Common.LogicalConstants public\n\n-- Non-logical constants\npostulate\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n _+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n _*_ : \u2115 \u2192 \u2115 \u2192 \u2115\n\n-- The PA equality.\n-- N.B. The symbol _\u2261_ should not be used because it is hard-coded by\n-- the program agda2atp as the ATPs equality.\npostulate _\u2250_ : \u2115 \u2192 \u2115 \u2192 Set\n\n-- Proper axioms\n-- (From Elliott Mendelson. Introduction to mathematical\n-- logic. Chapman & Hall, 4th edition, 1997, p. 155)\n\n-- N.B. We make the recursion in the first argument for _+_ and _*_.\n\n-- S\u2081. m = n \u2192 m = o \u2192 n = o\n-- S\u2082. m = n \u2192 succ m = succ n\n-- S\u2083. 0 \u2260 succ n\n-- S\u2084. succ m = succ n \u2192 m = n\n-- S\u2085. 0 + n = n\n-- S\u2086. succ m + n = succ (m + n)\n-- S\u2087. 0 * n = 0\n-- S\u2088. succ m * n = (m * n) + m\n-- S\u2089. P(0) \u2192 (\u2200n.P(n) \u2192 P(succ n)) \u2192 \u2200n.P(n), for any wf P(n) of PA.\n\npostulate\n S\u2081 : \u2200 {m n o} \u2192 m \u2250 n \u2192 m \u2250 o \u2192 n \u2250 o\n S\u2082 : \u2200 {m n} \u2192 m \u2250 n \u2192 succ m \u2250 succ n\n S\u2083 : \u2200 {n} \u2192 \u00ac (zero \u2250 succ n)\n S\u2084 : \u2200 {m n} \u2192 succ m \u2250 succ n \u2192 m \u2250 n\n S\u2085 : \u2200 n \u2192 zero + n \u2250 n\n S\u2086 : \u2200 m n \u2192 succ m + n \u2250 succ (m + n)\n S\u2087 : \u2200 n \u2192 zero * n \u2250 zero\n S\u2088 : \u2200 m n \u2192 succ m * n \u2250 n + m * n\n{-# ATP axiom S\u2081 S\u2082 S\u2083 S\u2084 S\u2085 S\u2086 S\u2087 S\u2088 #-}\n\n-- The axiom S\u2089 is a higher-order one, therefore we do not translate\n-- it as an ATP axiom.\npostulate S\u2089 : (P : \u2115 \u2192 Set) \u2192 P zero \u2192 (\u2200 n \u2192 P n \u2192 P (succ n)) \u2192 \u2200 n \u2192 P n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"0e5b57e4c178ef7eb9eb2bf41c231d36dbe70f5c","subject":"Moved a bug report to the agda2atp tool.","message":"Moved a bug report to the agda2atp tool.\n\nIgnore-this: e1514c644722b889facf44f91ec27a62\n\ndarcs-hash:20110322153307-3bd4e-0b07e862cc164decb95313adbe8b811bafa0a08d.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/Bugs\/GeneralHints.agda","new_file":"Draft\/Bugs\/GeneralHints.agda","new_contents":"","old_contents":"module Draft.Bugs.GeneralHints where\n\nopen import LTC.Base\nopen import LTC.Data.Nat\n\n-- TODO: Bug. The agda2atp tool does not translate the general hints\n\n-- {-# ATP hint zN #-}\n-- {-# ATP hint sN #-}\n\n-- because they are not defined in the file LTC.Data.Nat.Type.\n\npostulate\n N0 : N zero\n{-# ATP prove N0 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"998682f0d09f06ad7918732abe550b386afe02e2","subject":"Make Syntax.Type.Popl14 use Syntax.Type.Plotkin","message":"Make Syntax.Type.Popl14 use Syntax.Type.Plotkin\n\nOld-commit-hash: 031c97aee2022a85ee855ec3149f0f8cd160d5af\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Type\/Popl14.agda","new_file":"Syntax\/Type\/Popl14.agda","new_contents":"module Syntax.Type.Popl14 where\n\n-- Types of Calculus Popl14\n\ndata Popl14-type : Set where\n base-int : Popl14-type\n base-bag : Popl14-type\n\nopen import Syntax.Type.Plotkin Popl14-type public\n\npattern int = base base-int\npattern bag = base base-bag\n\nPopl14-\u0394base : Popl14-type \u2192 Popl14-type\nPopl14-\u0394base base-int = base-int\nPopl14-\u0394base base-bag = base-bag\n\n\u0394Type : Type \u2192 Type\n\u0394Type = lift-\u0394type\u2080 Popl14-\u0394base\n","old_contents":"module Syntax.Type.Popl14 where\n\n-- Types of Calculus Popl14\n\ninfixr 5 _\u21d2_\n\ndata Type : Set where\n int : Type\n bag : Type\n _\u21d2_ : (\u03c3 : Type) \u2192 (\u03c4 : Type) \u2192 Type\n\n\u0394Type : Type \u2192 Type\n\u0394Type int = int\n\u0394Type bag = bag\n\u0394Type (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394Type \u03c3 \u21d2 \u0394Type \u03c4\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"8f4895d3dd6492e95fe8a6c7c34c7bcf5a727766","subject":"Lamport: +extract-signkey +extract-signkey-correct","message":"Lamport: +extract-signkey +extract-signkey-correct\n","repos":"crypto-agda\/crypto-agda","old_file":"Crypto\/Sig\/Lamport.agda","new_file":"Crypto\/Sig\/Lamport.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Function using (_\u2218_)\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Product.NP renaming (map to \u00d7-map)\nopen import Data.Bit hiding (_==_)\nopen import Data.Bits\nopen import Data.Bits.Properties\nopen import Data.Vec.NP\nopen import Relation.Binary.PropositionalEquality.NP\nimport Crypto.Sig.LamportOneBit\n\nmodule Crypto.Sig.Lamport\n (#digest : \u2115)\n (#seed : \u2115)\n (#secret : \u2115)\n (#message : \u2115)\n (hash-secret : Bits #secret \u2192 Bits #digest)\n (seed-expansion : Bits #seed \u2192 Bits (#message * 2* #secret))\n where\n\nopen \u2261-Reasoning\n\nmodule OTS1 = Crypto.Sig.LamportOneBit #secret #digest hash-secret\n\nH = hash-secret\nH\u00b2 = map2* #secret #digest H\n\n#signkey1 = OTS1.#signkey\n#verifkey1 = OTS1.#verifkey\n#signature1 = OTS1.#signature\n#signkey = #message * #signkey1\n#verifkey = #message * #verifkey1\n#signature = #message * #signature1\n\nDigest = Bits #digest\nSeed = Bits #seed\nSecret = Bits #secret\nSignKey = Bits #signkey\nMessage = Bits #message\nSignature = Bits #signature\nVerifKey = Bits #verifkey\n\nverif-key : SignKey \u2192 VerifKey\nverif-key = map* #message OTS1.verif-key\n\nmodule verifkey (vk : VerifKey) where\n vk1s = group #message #verifkey1 vk\n\n concat-vk1s : concat vk1s \u2261 vk\n concat-vk1s = concat-group #message #verifkey1 vk\n\nmodule signkey (sk : SignKey) where\n sk1s = group #message #signkey1 sk\n sc1s = map OTS1.signkey.skP sk1s\n vk = verif-key sk\n open verifkey vk public\n sc1s-sk1s : map (uncurry _++_) sc1s \u2261 sk1s\n sc1s-sk1s = ! map-\u2218 (uncurry _++_) OTS1.signkey.skP sk1s\n \u2219 map-id= (take-drop-lem #secret) sk1s\n H-sc1s-sk1s : map (uncurry _++_ \u2218 \u00d7-map H H) sc1s \u2261 map H\u00b2 sk1s\n H-sc1s-sk1s = ! map-\u2218= (map2*-++ H) sc1s \u2219 ap (map H\u00b2) sc1s-sk1s\n\nmodule signature (sig : Signature) where\n sig1s = group #message #signature1 sig\n\n concat-sig1s : concat sig1s \u2261 sig\n concat-sig1s = concat-group #message #secret sig\n\nkey-gen : Seed \u2192 VerifKey \u00d7 SignKey\nkey-gen s = vk , sk\n module key-gen where\n sk = seed-expansion s\n vk = verif-key sk\n\nsign : SignKey \u2192 Message \u2192 Signature\nsign sk m = sig\n module sign where\n open signkey sk public\n sig1s = map OTS1.sign sk1s \u229b m\n sig = concat sig1s\n\n group-sig : group #message #signature1 sig \u2261 sig1s\n group-sig = group-concat sig1s\n\nverify : VerifKey \u2192 Message \u2192 Signature \u2192 Bit\nverify vk m sig = and checks\n module verify where\n open verifkey vk public\n open signature sig public\n checks = map OTS1.verify vk1s \u229b m \u229b sig1s\n\nprivate\n module lemma {#m} (sk : Bits (suc #m * #signkey1))\n (b : Bit) (m : Bits #m) where\n skL = take #signkey1 sk\n skH = drop #signkey1 sk\n vkL = OTS1.verif-key skL\n vkH = map* #m OTS1.verif-key skH\n sigL = OTS1.sign skL b\n sigH = concat (map OTS1.sign (group #m #signkey1 skH) \u229b m)\n\nverifkey-is-hash-sig : \u2200 sk m (open signkey sk)\n \u2192 map (take2* _) m \u229b vk1s\n \u2261 map H (map OTS1.sign sk1s \u229b m)\nverifkey-is-hash-sig sk m = lemma sk m\n module verifkey-is-hash-sig where\n lemma : \u2200 {#m} sk m \u2192 map (take2* _) m \u229b group #m #verifkey1 (map* #m OTS1.verif-key sk)\n \u2261 map hash-secret (map OTS1.sign (group #m #signkey1 sk) \u229b m)\n lemma sk [] = refl\n lemma {suc #m} sk (b \u2237 m)\n rewrite (let open lemma sk b m in take-++ #verifkey1 vkL vkH)\n | (let open lemma sk b m in drop-++ #verifkey1 vkL vkH)\n | (let open lemma sk b m in take-++ #signkey1 skL skH)\n | (let open lemma sk b m in drop-++ #signkey1 skL skH)\n | (let open lemma sk b m in lemma skH m)\n | (let open lemma sk b m in OTS1.verifkey-is-hash-sig skL b)\n = refl\n\nverify-correct-sig : \u2200 sk m \u2192 verify (verif-key sk) m (sign sk m) \u2261 1b\nverify-correct-sig sk m = lemma sk m\n module verify-correct-sig where\n lemma : \u2200 {#m} sk m \u2192 and (map OTS1.verify (group #m #verifkey1 (map* #m OTS1.verif-key sk)) \u229b m \u229b group #m #signature1\n (concat (map OTS1.sign (group #m #signkey1 sk) \u229b m))) \u2261 1b\n lemma sk [] = refl\n lemma sk (b \u2237 m)\n rewrite (let open lemma sk b m in take-++ #verifkey1 vkL vkH)\n | (let open lemma sk b m in drop-++ #verifkey1 vkL vkH)\n | (let open lemma sk b m in take-++ #signature1 sigL sigH)\n | (let open lemma sk b m in drop-++ #signature1 sigL sigH)\n | (let open lemma sk b m in OTS1.verify-correct-sig skL b)\n | (let open lemma sk b m in lemma skH m)\n = refl\n\nextract-signkey : (sig\u2080 sig\u2081 : Signature) \u2192 SignKey\nextract-signkey sig\u2080 sig\u2081 = sk\n module extract-signkey where\n module sig\u2080 = signature sig\u2080\n module sig\u2081 = signature sig\u2081\n sk1s = map _++_ sig\u2080.sig1s \u229b sig\u2081.sig1s\n sk = concat sk1s\n\nextract-signkey-correct :\n \u2200 sk \u2192 extract-signkey (sign sk 0\u207f) (sign sk 1\u207f) \u2261 sk\nextract-signkey-correct sk = sk-lemma\n where\n module sk = signkey sk\n module sig\u2080 = sign sk 0\u207f\n module sig\u2081 = sign sk 1\u207f\n module sk' = extract-signkey sig\u2080.sig sig\u2081.sig\n\n sk1s-lemma : sk'.sk1s \u2261 sk.sk1s\n sk1s-lemma = ap\u2082 (\u03bb x y \u2192 map _++_ x \u229b y)\n (sig\u2080.group-sig \u2219 \u229b-replicate _ 0b \u2219 map-\u2218= (\u03bb _ \u2192 refl) _)\n (sig\u2081.group-sig \u2219 \u229b-replicate _ 1b \u2219 map-\u2218= (\u03bb _ \u2192 refl) _)\n \u2219 \u229b-take-drop-lem #secret sk.sk1s\n\n sk-lemma : sk'.sk \u2261 sk\n sk-lemma = ap concat sk1s-lemma\n \u2219 concat-group #message _ sk\n\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Function using (_\u2218_)\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Product.NP renaming (map to \u00d7-map)\nopen import Data.Bit hiding (_==_)\nopen import Data.Bits\nopen import Data.Bits.Properties\nopen import Data.Vec.NP\nopen import Relation.Binary.PropositionalEquality.NP\nimport Crypto.Sig.LamportOneBit\n\nmodule Crypto.Sig.Lamport\n (#digest : \u2115)\n (#seed : \u2115)\n (#secret : \u2115)\n (#message : \u2115)\n (hash-secret : Bits #secret \u2192 Bits #digest)\n (seed-expansion : Bits #seed \u2192 Bits (#message * 2* #secret))\n where\n\nmodule OTS1 = Crypto.Sig.LamportOneBit #secret #digest hash-secret\n\nH = hash-secret\nH\u00b2 = map2* #secret #digest H\n\n#signkey1 = OTS1.#signkey\n#verifkey1 = OTS1.#verifkey\n#signature1 = OTS1.#signature\n#signkey = #message * #signkey1\n#verifkey = #message * #verifkey1\n#signature = #message * #signature1\n\nDigest = Bits #digest\nSeed = Bits #seed\nSecret = Bits #secret\nSignKey = Bits #signkey\nMessage = Bits #message\nSignature = Bits #signature\nVerifKey = Bits #verifkey\n\nverif-key : SignKey \u2192 VerifKey\nverif-key = map* #message OTS1.verif-key\n\nmodule verifkey (vk : VerifKey) where\n vk1s = group #message #verifkey1 vk\n\n concat-vk1s : concat vk1s \u2261 vk\n concat-vk1s = concat-group #message #verifkey1 vk\n\nmodule signkey (sk : SignKey) where\n sk1s = group #message #signkey1 sk\n sc1s = map OTS1.signkey.skP sk1s\n vk = verif-key sk\n open verifkey vk public\n sc1s-sk1s : map (uncurry _++_) sc1s \u2261 sk1s\n sc1s-sk1s = ! map-\u2218 (uncurry _++_) OTS1.signkey.skP sk1s\n \u2219 map-id= (take-drop-lem #secret) sk1s\n H-sc1s-sk1s : map (uncurry _++_ \u2218 \u00d7-map H H) sc1s \u2261 map H\u00b2 sk1s\n H-sc1s-sk1s = ! map-\u2218= (map2*-++ H) sc1s \u2219 ap (map H\u00b2) sc1s-sk1s\n\nmodule signature (sig : Signature) where\n sig1s = group #message #signature1 sig\n\n concat-sig1s : concat sig1s \u2261 sig\n concat-sig1s = concat-group #message #secret sig\n\nkey-gen : Seed \u2192 VerifKey \u00d7 SignKey\nkey-gen s = vk , sk\n module key-gen where\n sk = seed-expansion s\n vk = verif-key sk\n\nsign : SignKey \u2192 Message \u2192 Signature\nsign sk m = sig\n module sign where\n open signkey sk public\n sig1s = map OTS1.sign sk1s \u229b m\n sig = concat sig1s\n\n group-sig : group #message #signature1 sig \u2261 sig1s\n group-sig = group-concat sig1s\n\nverify : VerifKey \u2192 Message \u2192 Signature \u2192 Bit\nverify vk m sig = and checks\n module verify where\n open verifkey vk public\n open signature sig public\n checks = map OTS1.verify vk1s \u229b m \u229b sig1s\n\nprivate\n module lemma {#m} (sk : Bits (suc #m * #signkey1))\n (b : Bit) (m : Bits #m) where\n skL = take #signkey1 sk\n skH = drop #signkey1 sk\n vkL = OTS1.verif-key skL\n vkH = map* #m OTS1.verif-key skH\n sigL = OTS1.sign skL b\n sigH = concat (map OTS1.sign (group #m #signkey1 skH) \u229b m)\n\nverifkey-is-hash-sig : \u2200 sk m (open signkey sk)\n \u2192 map (take2* _) m \u229b vk1s\n \u2261 map H (map OTS1.sign sk1s \u229b m)\nverifkey-is-hash-sig sk m = lemma sk m\n module verifkey-is-hash-sig where\n lemma : \u2200 {#m} sk m \u2192 map (take2* _) m \u229b group #m #verifkey1 (map* #m OTS1.verif-key sk)\n \u2261 map hash-secret (map OTS1.sign (group #m #signkey1 sk) \u229b m)\n lemma sk [] = refl\n lemma {suc #m} sk (b \u2237 m)\n rewrite (let open lemma sk b m in take-++ #verifkey1 vkL vkH)\n | (let open lemma sk b m in drop-++ #verifkey1 vkL vkH)\n | (let open lemma sk b m in take-++ #signkey1 skL skH)\n | (let open lemma sk b m in drop-++ #signkey1 skL skH)\n | (let open lemma sk b m in lemma skH m)\n | (let open lemma sk b m in OTS1.verifkey-is-hash-sig skL b)\n = refl\n\nverify-correct-sig : \u2200 sk m \u2192 verify (verif-key sk) m (sign sk m) \u2261 1b\nverify-correct-sig sk m = lemma sk m\n module verify-correct-sig where\n lemma : \u2200 {#m} sk m \u2192 and (map OTS1.verify (group #m #verifkey1 (map* #m OTS1.verif-key sk)) \u229b m \u229b group #m #signature1\n (concat (map OTS1.sign (group #m #signkey1 sk) \u229b m))) \u2261 1b\n lemma sk [] = refl\n lemma sk (b \u2237 m)\n rewrite (let open lemma sk b m in take-++ #verifkey1 vkL vkH)\n | (let open lemma sk b m in drop-++ #verifkey1 vkL vkH)\n | (let open lemma sk b m in take-++ #signature1 sigL sigH)\n | (let open lemma sk b m in drop-++ #signature1 sigL sigH)\n | (let open lemma sk b m in OTS1.verify-correct-sig skL b)\n | (let open lemma sk b m in lemma skH m)\n = refl\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"a11bc0494d5a53ae852712fa2ba53aa140392e9f","subject":"Updated a note.","message":"Updated a note.\n\nIgnore-this: ff92cf63d71d2a20c973e95b701a3ff3\n\ndarcs-hash:20111103145522-3bd4e-b34a41ced2551fcad2ad1814dcd1d73f59a63884.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/LeastFixedPoints.agda","new_file":"notes\/fixed-points\/LeastFixedPoints.agda","new_contents":"-- Tested with Agda 2.2.11 on 11 October 2011.\n\nmodule LeastFixedPoints where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\ninfixl 9 _+_\n\n------------------------------------------------------------------------------\n-- Basic definitions\n\n-- Pre-fixed point : d is a pre-fixed point of f if f d \u2264 d\n\n-- Post-fixed point : d is a post-fixed point of f if d \u2264 f d\n\n-- Fixed-point : d is a fixed-point of f if f d = d\n\n-- Least pre-fixed point : d is the least pre-fixed point of f if\n-- 1. f d \u2264 d -- d is a pre-fixed point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e -- d is the least prefixed-point of f\n\n-- Least fixed-point : d is the least fixed-point of f if\n-- 1. f d = d -- d is a fixed-point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e -- d is the least prefixed-point of f\n\n-- Thm: If d is the least pre-fixed point of f, then d is a fixed\n-- point of f (TODO: source).\n\n------------------------------------------------------------------------------\n-- Auxiliary definitions\n\nflip : {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\nflip f b a = f a b\n\n------------------------------------------------------------------------------\n-- N is a least pre-fixed point of a functor\n\n-- Instead defining the least pre-fixed via a (higher-order)\n-- operator, we will define it using an instance of that operator.\n\n-- The functor\n-- NatF : (D \u2192 Set) \u2192 D \u2192 Set\n-- NatF X n = n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ m \u2227 X m)\n\n-- The natural numbers are the least pre-fixed point of NatF.\npostulate\n N : D \u2192 Set\n\n -- N is pre-fixed point of NatF.\n -- Peter: It corresponds to the introduction rules.\n N-lfp\u2081 : \u2200 n \u2192 n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ\u2081 m \u2227 N m) \u2192 N n\n\n -- N is the least prefixed-point of NatF.\n -- Peter: It corresponds to the elimination rule of an inductively\n -- defined predicate.\n N-lfp\u2082 : \u2200 (P : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ\u2081 m \u2227 P m) \u2192 P n) \u2192\n N n \u2192 P n\n\n------------------------------------------------------------------------------\n-- The data constructors of N.\nzN : N zero\nzN = N-lfp\u2081 zero (inj\u2081 refl)\n\nsN : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\nsN {n} Nn = N-lfp\u2081 (succ\u2081 n) (inj\u2082 (n , (refl , Nn)))\n\n------------------------------------------------------------------------------\n-- Because N is the least pre-fixed point of NatF (i.e. N-lfp\u2081 and\n-- N-lfp\u2082), we can proof that N is also post-fixed point of NatF.\n\n-- N is a post-fixed point of NatF.\nN-lfp\u2083 : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ\u2081 m \u2227 N m)\nN-lfp\u2083 {n} Nn = N-lfp\u2082 P prf Nn\n where\n P : D \u2192 Set\n P x = x \u2261 zero \u2228 \u2203 \u03bb m \u2192 x \u2261 succ\u2081 m \u2227 N m\n\n prf : n \u2261 zero \u2228 \u2203 (\u03bb m \u2192 n \u2261 succ\u2081 m \u2227 P m) \u2192 P n\n prf h = [ inj\u2081 , (\u03bb h\u2081 \u2192 inj\u2082 (prf\u2081 h\u2081)) ] h\n where\n prf\u2081 : \u2203 (\u03bb m \u2192 n \u2261 succ\u2081 m \u2227 (m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 N m'))) \u2192\n \u2203 \u03bb m \u2192 n \u2261 succ\u2081 m \u2227 N m\n prf\u2081 (m , n=Sm , h\u2082) = m , n=Sm , prf\u2082 h\u2082\n where\n prf\u2082 : m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 N m') \u2192 N m\n prf\u2082 h\u2082 = [ (\u03bb h\u2083 \u2192 subst N (sym h\u2083) zN) , prf\u2083 ] h\u2082\n where\n prf\u2083 : \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 N m') \u2192 N m\n prf\u2083 (m' , m\u2261Sm' , Nm') = subst N (sym m\u2261Sm') (sN Nm')\n\n------------------------------------------------------------------------------\n-- The induction principle for N.\nindN : (P : D \u2192 Set) \u2192\n P zero \u2192\n (\u2200 {n} \u2192 P n \u2192 P (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 P n\nindN P P0 is {n} Nn = N-lfp\u2082 P [ prf\u2081 , prf\u2082 ] Nn\n where\n prf\u2081 : n \u2261 zero \u2192 P n\n prf\u2081 n\u22610 = subst P (sym n\u22610) P0\n\n prf\u2082 : \u2203 (\u03bb m \u2192 n \u2261 succ\u2081 m \u2227 P m) \u2192 P n\n prf\u2082 (m , n\u2261Sm , Pm) = subst P (sym n\u2261Sm) (is Pm)\n\n------------------------------------------------------------------------------\n-- Example: We will use N-lfp\u2082 as the induction principle on N.\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ\u2081 d + e \u2261 succ\u2081 (d + e)\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity n = +-0x n\n\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = N-lfp\u2082 P prf Nm\n\n where\n P : D \u2192 Set\n P i = N (i + n)\n\n prf : m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 P m') \u2192 P m\n prf h = [ prf\u2081 , prf\u2082 ] h\n where\n P0 : P zero\n P0 = subst N (sym (+-leftIdentity n)) Nn\n\n prf\u2081 : m \u2261 zero \u2192 P m\n prf\u2081 h\u2081 = subst N (cong (flip _+_ n) (sym h\u2081)) P0\n\n is : \u2200 {i} \u2192 P i \u2192 P (succ\u2081 i)\n is {i} Pi = subst N (sym (+-Sx i n)) (sN Pi)\n\n prf\u2082 : \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 P m') \u2192 P m\n prf\u2082 (m' , m\u2261Sm' , Pm') = subst N (cong (flip _+_ n) (sym m\u2261Sm')) (is Pm')\n","old_contents":"-- Tested with Agda 2.2.11 on 11 October 2011.\n\nmodule LeastFixedPoints where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\ninfixl 9 _+_\n\n------------------------------------------------------------------------------\n-- Basic definitions\n\n-- Pre-fixed point : d is a pre-fixed point of f if f d \u2264 d\n\n-- Post-fixed point : d is a post-fixed point of f if d \u2264 f d\n\n-- Fixed-point : d is a fixed-point of f if f d = d\n\n-- Least pre-fixed point : d is the least pre-fixed point of f if\n-- 1. f d \u2264 d -- d is a pre-fixed point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e -- d is the least prefixed-point of f\n\n-- Least fixed-point : d is the least fixed-point of f if\n-- 1. f d = d -- d is a fixed-point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e -- d is the least prefixed-point of f\n\n-- Thm: If d is the least pre-fixed point of f, then d is a fixed\n-- point of f (TODO: source).\n\n------------------------------------------------------------------------------\n-- Auxiliary definitions\n\nflip : {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\nflip f b a = f a b\n\n------------------------------------------------------------------------------\n-- N is a least pre-fixed point of a functor\n\n-- Instead defining the least pre-fixed via a (higher-order)\n-- operator, we will define it using an instance of that operator.\n\n-- The functor\n-- NatF : (D \u2192 Set) \u2192 D \u2192 Set\n-- NatF X n = n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ m \u2227 X m)\n\n-- The natural numbers are the least pre-fixed point of NatF.\npostulate\n N : D \u2192 Set\n\n -- N is pre-fixed point of NatF.\n -- Peter: It corresponds to the introduction rules.\n N-lfp\u2081 : \u2200 n \u2192 n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ m \u2227 N m) \u2192 N n\n\n -- N is the least prefixed-point of NatF.\n -- Peter: It corresponds to the elimination rule of an inductively\n -- defined predicate.\n N-lfp\u2082 : \u2200 (P : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ m \u2227 P m) \u2192 P n) \u2192\n N n \u2192 P n\n\n------------------------------------------------------------------------------\n-- The data constructors of N.\nzN : N zero\nzN = N-lfp\u2081 zero (inj\u2081 refl)\n\nsN : \u2200 {n} \u2192 N n \u2192 N (succ n)\nsN {n} Nn = N-lfp\u2081 (succ n) (inj\u2082 (n , (refl , Nn)))\n\n------------------------------------------------------------------------------\n-- Because N is the least pre-fixed point of NatF (i.e. N-lfp\u2081 and\n-- N-lfp\u2082), we can proof that N is also post-fixed point of NatF.\n\n-- N is a post-fixed point of NatF.\nN-lfp\u2083 : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ m \u2227 N m)\nN-lfp\u2083 {n} Nn = N-lfp\u2082 P prf Nn\n where\n P : D \u2192 Set\n P x = x \u2261 zero \u2228 \u2203 \u03bb m \u2192 x \u2261 succ m \u2227 N m\n\n prf : n \u2261 zero \u2228 \u2203 (\u03bb m \u2192 n \u2261 succ m \u2227 P m) \u2192 P n\n prf h = [ inj\u2081 , (\u03bb h\u2081 \u2192 inj\u2082 (prf\u2081 h\u2081)) ] h\n where\n prf\u2081 : \u2203 (\u03bb m \u2192 n \u2261 succ m \u2227 (m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 m \u2261 succ m' \u2227 N m'))) \u2192\n \u2203 \u03bb m \u2192 n \u2261 succ m \u2227 N m\n prf\u2081 (m , n=Sm , h\u2082) = m , n=Sm , prf\u2082 h\u2082\n where\n prf\u2082 : m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 m \u2261 succ m' \u2227 N m') \u2192 N m\n prf\u2082 h\u2082 = [ (\u03bb h\u2083 \u2192 subst N (sym h\u2083) zN) , prf\u2083 ] h\u2082\n where\n prf\u2083 : \u2203 (\u03bb m' \u2192 m \u2261 succ m' \u2227 N m') \u2192 N m\n prf\u2083 (m' , m\u2261Sm' , Nm') = subst N (sym m\u2261Sm') (sN Nm')\n\n------------------------------------------------------------------------------\n-- The induction principle for N.\nindN : (P : D \u2192 Set) \u2192\n P zero \u2192\n (\u2200 {n} \u2192 N n \u2192 P n \u2192 P (succ n)) \u2192\n \u2200 {n} \u2192 N n \u2192 P n\nindN P P0 is {n} Nn = N-lfp\u2082 P [ prf\u2081 , prf\u2082 ] Nn\n where\n prf\u2081 : n \u2261 zero \u2192 P n\n prf\u2081 n\u22610 = subst P (sym n\u22610) P0\n\n prf\u2082 : \u2203 (\u03bb m \u2192 n \u2261 succ m \u2227 P m) \u2192 P n\n prf\u2082 (m , n\u2261Sm , Pm) = subst P (sym n\u2261Sm) (is helper Pm)\n where\n helper : N m\n helper = [ prf\u2083 , prf\u2084 ] (N-lfp\u2083 Nn)\n where\n prf\u2083 : n \u2261 zero \u2192 N m\n prf\u2083 n\u22610 = \u22a5-elim (0\u2260S (trans (sym n\u22610) n\u2261Sm))\n\n prf\u2084 : \u2203 (\u03bb m' \u2192 n \u2261 succ m' \u2227 N m') \u2192 N m\n prf\u2084 (m' , n\u2261Sm' , Nm') =\n subst N (succInjective (trans (sym n\u2261Sm') n\u2261Sm)) Nm'\n\n------------------------------------------------------------------------------\n-- Example: We will use N-lfp\u2082 as the induction principle on N.\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ d + e \u2261 succ (d + e)\n\n+-leftIdentity : \u2200 {n} \u2192 N n \u2192 zero + n \u2261 n\n+-leftIdentity {n} _ = +-0x n\n\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = N-lfp\u2082 P prf Nm\n\n where\n P : D \u2192 Set\n P i = N (i + n)\n\n prf : m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 m \u2261 succ m' \u2227 P m') \u2192 P m\n prf h = [ prf\u2081 , prf\u2082 ] h\n where\n P0 : P zero\n P0 = subst N (sym (+-leftIdentity Nn)) Nn\n\n prf\u2081 : m \u2261 zero \u2192 P m\n prf\u2081 h\u2081 = subst N (cong (flip _+_ n) (sym h\u2081)) P0\n\n is : \u2200 {i} \u2192 P i \u2192 P (succ i)\n is {i} Pi = subst N (sym (+-Sx i n)) (sN Pi)\n\n prf\u2082 : \u2203 (\u03bb m' \u2192 m \u2261 succ m' \u2227 P m') \u2192 P m\n prf\u2082 (m' , m\u2261Sm' , Pm') = subst N (cong (flip _+_ n) (sym m\u2261Sm')) (is Pm')\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"91c27a6528221dcbcdc0cfc04817e2db672ed84a","subject":"Fixed module.","message":"Fixed module.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/FOT\/FOTC\/NoPatternMatchingOnRefl.agda","new_file":"notes\/FOT\/FOTC\/NoPatternMatchingOnRefl.agda","new_contents":"------------------------------------------------------------------------------\n-- Proving properties without using pattern matching on refl\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.NoPatternMatchingOnRefl where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\n\nopen import FOTC.Data.Bool\nopen import FOTC.Data.Conat\nopen import FOTC.Data.Conat.Equality\nopen import FOTC.Data.List\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Stream\n\nopen import FOTC.Program.McCarthy91.McCarthy91\n\nopen import FOTC.Relation.Binary.Bisimilarity\nopen import FOTC.Relation.Binary.Bisimilarity.PropertiesI\n\n------------------------------------------------------------------------------\n-- From FOTC.Base.PropertiesI\n\n-- Congruence properties\n\n\u00b7-leftCong : \u2200 {a b c} \u2192 a \u2261 b \u2192 a \u00b7 c \u2261 b \u00b7 c\n\u00b7-leftCong {a} {c = c} h = subst (\u03bb t \u2192 a \u00b7 c \u2261 t \u00b7 c) h refl\n\n\u00b7-rightCong : \u2200 {a b c} \u2192 b \u2261 c \u2192 a \u00b7 b \u2261 a \u00b7 c\n\u00b7-rightCong {a} {b} h = subst (\u03bb t \u2192 a \u00b7 b \u2261 a \u00b7 t) h refl\n\n\u00b7-cong : \u2200 {a b c d} \u2192 a \u2261 b \u2192 c \u2261 d \u2192 a \u00b7 c \u2261 b \u00b7 d\n\u00b7-cong {a} {c = c} h\u2081 h\u2082 = subst\u2082 (\u03bb t\u2081 t\u2082 \u2192 a \u00b7 c \u2261 t\u2081 \u00b7 t\u2082) h\u2081 h\u2082 refl\n\nsuccCong : \u2200 {m n} \u2192 m \u2261 n \u2192 succ\u2081 m \u2261 succ\u2081 n\nsuccCong {m} h = subst (\u03bb t \u2192 succ\u2081 m \u2261 succ\u2081 t) h refl\n\npredCong : \u2200 {m n} \u2192 m \u2261 n \u2192 pred\u2081 m \u2261 pred\u2081 n\npredCong {m} h = subst (\u03bb t \u2192 pred\u2081 m \u2261 pred\u2081 t) h refl\n\nifCong\u2081 : \u2200 {b b' t t'} \u2192 b \u2261 b' \u2192 if b then t else t' \u2261 if b' then t else t'\nifCong\u2081 {b} {t = t} {t'} h =\n subst (\u03bb x \u2192 if b then t else t' \u2261 if x then t else t') h refl\n\nifCong\u2082 : \u2200 {b t\u2081 t\u2082 t} \u2192 t\u2081 \u2261 t\u2082 \u2192 if b then t\u2081 else t \u2261 if b then t\u2082 else t\nifCong\u2082 {b} {t\u2081} {t = t} h =\n subst (\u03bb x \u2192 if b then t\u2081 else t \u2261 if b then x else t) h refl\n\nifCong\u2083 : \u2200 {b t t\u2081 t\u2082} \u2192 t\u2081 \u2261 t\u2082 \u2192 if b then t else t\u2081 \u2261 if b then t else t\u2082\nifCong\u2083 {b} {t} {t\u2081} h =\n subst (\u03bb x \u2192 if b then t else t\u2081 \u2261 if b then t else x) h refl\n\n------------------------------------------------------------------------------\n-- From FOTC.Base.List.PropertiesI\n\n-- Congruence properties\n\n\u2237-leftCong : \u2200 {x y xs} \u2192 x \u2261 y \u2192 x \u2237 xs \u2261 y \u2237 xs\n\u2237-leftCong {x} {xs = xs} h = subst (\u03bb t \u2192 x \u2237 xs \u2261 t \u2237 xs) h refl\n\n\u2237-rightCong : \u2200 {x xs ys} \u2192 xs \u2261 ys \u2192 x \u2237 xs \u2261 x \u2237 ys\n\u2237-rightCong {x}{xs} h = subst (\u03bb t \u2192 x \u2237 xs \u2261 x \u2237 t) h refl\n\n\u2237-Cong : \u2200 {x y xs ys} \u2192 x \u2261 y \u2192 xs \u2261 ys \u2192 x \u2237 xs \u2261 y \u2237 ys\n\u2237-Cong {x} {xs = xs} h\u2081 h\u2082 = subst\u2082 (\u03bb t\u2081 t\u2082 \u2192 x \u2237 xs \u2261 t\u2081 \u2237 t\u2082) h\u2081 h\u2082 refl\n\nheadCong : \u2200 {xs ys} \u2192 xs \u2261 ys \u2192 head\u2081 xs \u2261 head\u2081 ys\nheadCong {xs} h = subst (\u03bb t \u2192 head\u2081 xs \u2261 head\u2081 t) h refl\n\ntailCong : \u2200 {xs ys} \u2192 xs \u2261 ys \u2192 tail\u2081 xs \u2261 tail\u2081 ys\ntailCong {xs} h = subst (\u03bb t \u2192 tail\u2081 xs \u2261 tail\u2081 t) h refl\n\n------------------------------------------------------------------------------\n-- From FOTC.Data.Bool.PropertiesI\n\n-- Congruence properties\n\n&&-leftCong : \u2200 {a b c} \u2192 a \u2261 b \u2192 a && c \u2261 b && c\n&&-leftCong {a} {c = c} h = subst (\u03bb t \u2192 a && c \u2261 t && c) h refl\n\n&&-rightCong : \u2200 {a b c} \u2192 b \u2261 c \u2192 a && b \u2261 a && c\n&&-rightCong {a} {b} h = subst (\u03bb t \u2192 a && b \u2261 a && t) h refl\n\n&&-cong : \u2200 {a b c d } \u2192 a \u2261 c \u2192 b \u2261 d \u2192 a && b \u2261 c && d\n&&-cong {a} {b} h\u2081 h\u2082 = subst\u2082 (\u03bb t\u2081 t\u2082 \u2192 a && b \u2261 t\u2081 && t\u2082) h\u2081 h\u2082 refl\n\nnotCong : \u2200 {a b} \u2192 a \u2261 b \u2192 not a \u2261 not b\nnotCong {a} h = subst (\u03bb t \u2192 not a \u2261 not t) h refl\n\n------------------------------------------------------------------------------\n-- FOTC.Data.Conat.Equality.PropertiesI\n\n\u2248N-refl : \u2200 {n} \u2192 Conat n \u2192 n \u2248N n\n\u2248N-refl {n} Cn = \u2248N-coind R h\u2081 h\u2082\n where\n R : D \u2192 D \u2192 Set\n R a b = Conat a \u2227 Conat b \u2227 a \u2261 b\n\n h\u2081 : \u2200 {a b} \u2192 R a b \u2192\n a \u2261 zero \u2227 b \u2261 zero\n \u2228 (\u2203[ a' ] \u2203[ b' ] a \u2261 succ\u2081 a' \u2227 b \u2261 succ\u2081 b' \u2227 R a' b')\n h\u2081 (Ca , Cb , h) with Conat-unf Ca\n ... | inj\u2081 prf = inj\u2081 (prf , trans (sym h) prf)\n ... | inj\u2082 (a' , prf , Ca') =\n inj\u2082 (a' , a' , prf , trans (sym h) prf , (Ca' , Ca' , refl))\n\n h\u2082 : R n n\n h\u2082 = Cn , Cn , refl\n\n\u2261\u2192\u2248N : \u2200 {m n} \u2192 Conat m \u2192 Conat n \u2192 m \u2261 n \u2192 m \u2248N n\n\u2261\u2192\u2248N {m} Cm _ h = subst (_\u2248N_ m) h (\u2248N-refl Cm)\n\n------------------------------------------------------------------------------\n-- FOTC.Data.List.PropertiesI\n\n-- Congruence properties\n\n++-leftCong : \u2200 {xs ys zs} \u2192 xs \u2261 ys \u2192 xs ++ zs \u2261 ys ++ zs\n++-leftCong {xs} {zs = zs} h = subst (\u03bb t \u2192 xs ++ zs \u2261 t ++ zs) h refl\n\n++-rightCong : \u2200 {xs ys zs} \u2192 ys \u2261 zs \u2192 xs ++ ys \u2261 xs ++ zs\n++-rightCong {xs} {ys} h = subst (\u03bb t \u2192 xs ++ ys \u2261 xs ++ t) h refl\n\nmapCong\u2082 : \u2200 {f xs ys} \u2192 xs \u2261 ys \u2192 map f xs \u2261 map f ys\nmapCong\u2082 {f} {xs} h = subst (\u03bb t \u2192 map f xs \u2261 map f t) h refl\n\nrevCong\u2081 : \u2200 {xs ys zs} \u2192 xs \u2261 ys \u2192 rev xs zs \u2261 rev ys zs\nrevCong\u2081 {xs} {zs = zs} h = subst (\u03bb t \u2192 rev xs zs \u2261 rev t zs) h refl\n\nrevCong\u2082 : \u2200 {xs ys zs} \u2192 ys \u2261 zs \u2192 rev xs ys \u2261 rev xs zs\nrevCong\u2082 {xs} {ys} h = subst (\u03bb t \u2192 rev xs ys \u2261 rev xs t) h refl\n\nreverseCong : \u2200 {xs ys} \u2192 xs \u2261 ys \u2192 reverse xs \u2261 reverse ys\nreverseCong {xs} h = subst (\u03bb t \u2192 reverse xs \u2261 reverse t) h refl\n\nlengthCong : \u2200 {xs ys} \u2192 xs \u2261 ys \u2192 length xs \u2261 length ys\nlengthCong {xs} h = subst (\u03bb t \u2192 length xs \u2261 length t) h refl\n\n------------------------------------------------------------------------------\n-- From FOTC.Data.Nat.Inequalities.PropertiesI\n\n-- Congruence properties\n\nleLeftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 le m o \u2261 le n o\nleLeftCong {m} {o = o} h = subst (\u03bb t \u2192 le m o \u2261 le t o) h refl\n\nltLeftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 lt m o \u2261 lt n o\nltLeftCong {m} {o = o} h = subst (\u03bb t \u2192 lt m o \u2261 lt t o) h refl\n\nltRightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 lt m n \u2261 lt m o\nltRightCong {m} {n} h = subst (\u03bb t \u2192 lt m n \u2261 lt m t) h refl\n\nltCong : \u2200 {m\u2081 n\u2081 m\u2082 n\u2082} \u2192 m\u2081 \u2261 m\u2082 \u2192 n\u2081 \u2261 n\u2082 \u2192 lt m\u2081 n\u2081 \u2261 lt m\u2082 n\u2082\nltCong {m\u2081} {n\u2081} h\u2081 h\u2082 = subst\u2082 (\u03bb t\u2081 t\u2082 \u2192 lt m\u2081 n\u2081 \u2261 lt t\u2081 t\u2082) h\u2081 h\u2082 refl\n\n------------------------------------------------------------------------------\n-- From FOTC.Data.Nat.PropertiesI\n\n-- Congruence properties\n\n+-leftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 m + o \u2261 n + o\n+-leftCong {m} {o = o} h = subst (\u03bb t \u2192 m + o \u2261 t + o) h refl\n\n+-rightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 m + n \u2261 m + o\n+-rightCong {m} {n} h = subst (\u03bb t \u2192 m + n \u2261 m + t) h refl\n\n\u2238-leftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 m \u2238 o \u2261 n \u2238 o\n\u2238-leftCong {m} {o = o} h = subst (\u03bb t \u2192 m \u2238 o \u2261 t \u2238 o) h refl\n\n\u2238-rightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 m \u2238 n \u2261 m \u2238 o\n\u2238-rightCong {m} {n} h = subst (\u03bb t \u2192 m \u2238 n \u2261 m \u2238 t) h refl\n\n*-leftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 m * o \u2261 n * o\n*-leftCong {m} {o = o} h = subst (\u03bb t \u2192 m * o \u2261 t * o) h refl\n\n*-rightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 m * n \u2261 m * o\n*-rightCong {m} {n} h = subst (\u03bb t \u2192 m * n \u2261 m * t) h refl\n\n------------------------------------------------------------------------------\n-- From FOT.FOTC.Data.Stream.Equality.PropertiesI where\n\nstream-\u2261\u2192\u2248 : \u2200 {xs ys} \u2192 Stream xs \u2192 Stream ys \u2192 xs \u2261 ys \u2192 xs \u2248 ys\nstream-\u2261\u2192\u2248 {xs} Sxs _ h = subst (_\u2248_ xs) h (\u2248-refl Sxs)\n\n------------------------------------------------------------------------------\n-- From FOTC.Program.McCarthy91.AuxiliaryPropertiesATP\n\nf\u2089\u2081-x\u2261y : \u2200 {m n o} \u2192 f\u2089\u2081 m \u2261 n \u2192 o \u2261 m \u2192 f\u2089\u2081 o \u2261 n\nf\u2089\u2081-x\u2261y {n = n} h\u2081 h\u2082 = subst (\u03bb t \u2192 f\u2089\u2081 t \u2261 n) (sym h\u2082) h\u2081\n","old_contents":"------------------------------------------------------------------------------\n-- Proving properties without using pattern matching on refl\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.NoPatternMatchingOnRefl where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\n\nopen import FOTC.Data.Bool\nopen import FOTC.Data.Conat\nopen import FOTC.Data.Conat.Equality\nopen import FOTC.Data.List\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Stream\n\nopen import FOTC.Program.McCarthy91.McCarthy91\n\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n-- From FOTC.Base.PropertiesI\n\n-- Congruence properties\n\n\u00b7-leftCong : \u2200 {a b c} \u2192 a \u2261 b \u2192 a \u00b7 c \u2261 b \u00b7 c\n\u00b7-leftCong {a} {c = c} h = subst (\u03bb t \u2192 a \u00b7 c \u2261 t \u00b7 c) h refl\n\n\u00b7-rightCong : \u2200 {a b c} \u2192 b \u2261 c \u2192 a \u00b7 b \u2261 a \u00b7 c\n\u00b7-rightCong {a} {b} h = subst (\u03bb t \u2192 a \u00b7 b \u2261 a \u00b7 t) h refl\n\n\u00b7-cong : \u2200 {a b c d} \u2192 a \u2261 b \u2192 c \u2261 d \u2192 a \u00b7 c \u2261 b \u00b7 d\n\u00b7-cong {a} {c = c} h\u2081 h\u2082 = subst\u2082 (\u03bb t\u2081 t\u2082 \u2192 a \u00b7 c \u2261 t\u2081 \u00b7 t\u2082) h\u2081 h\u2082 refl\n\nsuccCong : \u2200 {m n} \u2192 m \u2261 n \u2192 succ\u2081 m \u2261 succ\u2081 n\nsuccCong {m} h = subst (\u03bb t \u2192 succ\u2081 m \u2261 succ\u2081 t) h refl\n\npredCong : \u2200 {m n} \u2192 m \u2261 n \u2192 pred\u2081 m \u2261 pred\u2081 n\npredCong {m} h = subst (\u03bb t \u2192 pred\u2081 m \u2261 pred\u2081 t) h refl\n\nifCong\u2081 : \u2200 {b b' t t'} \u2192 b \u2261 b' \u2192 if b then t else t' \u2261 if b' then t else t'\nifCong\u2081 {b} {t = t} {t'} h =\n subst (\u03bb x \u2192 if b then t else t' \u2261 if x then t else t') h refl\n\nifCong\u2082 : \u2200 {b t\u2081 t\u2082 t} \u2192 t\u2081 \u2261 t\u2082 \u2192 if b then t\u2081 else t \u2261 if b then t\u2082 else t\nifCong\u2082 {b} {t\u2081} {t = t} h =\n subst (\u03bb x \u2192 if b then t\u2081 else t \u2261 if b then x else t) h refl\n\nifCong\u2083 : \u2200 {b t t\u2081 t\u2082} \u2192 t\u2081 \u2261 t\u2082 \u2192 if b then t else t\u2081 \u2261 if b then t else t\u2082\nifCong\u2083 {b} {t} {t\u2081} h =\n subst (\u03bb x \u2192 if b then t else t\u2081 \u2261 if b then t else x) h refl\n\n------------------------------------------------------------------------------\n-- From FOTC.Base.List.PropertiesI\n\n-- Congruence properties\n\n\u2237-leftCong : \u2200 {x y xs} \u2192 x \u2261 y \u2192 x \u2237 xs \u2261 y \u2237 xs\n\u2237-leftCong {x} {xs = xs} h = subst (\u03bb t \u2192 x \u2237 xs \u2261 t \u2237 xs) h refl\n\n\u2237-rightCong : \u2200 {x xs ys} \u2192 xs \u2261 ys \u2192 x \u2237 xs \u2261 x \u2237 ys\n\u2237-rightCong {x}{xs} h = subst (\u03bb t \u2192 x \u2237 xs \u2261 x \u2237 t) h refl\n\n\u2237-Cong : \u2200 {x y xs ys} \u2192 x \u2261 y \u2192 xs \u2261 ys \u2192 x \u2237 xs \u2261 y \u2237 ys\n\u2237-Cong {x} {xs = xs} h\u2081 h\u2082 = subst\u2082 (\u03bb t\u2081 t\u2082 \u2192 x \u2237 xs \u2261 t\u2081 \u2237 t\u2082) h\u2081 h\u2082 refl\n\nheadCong : \u2200 {xs ys} \u2192 xs \u2261 ys \u2192 head\u2081 xs \u2261 head\u2081 ys\nheadCong {xs} h = subst (\u03bb t \u2192 head\u2081 xs \u2261 head\u2081 t) h refl\n\ntailCong : \u2200 {xs ys} \u2192 xs \u2261 ys \u2192 tail\u2081 xs \u2261 tail\u2081 ys\ntailCong {xs} h = subst (\u03bb t \u2192 tail\u2081 xs \u2261 tail\u2081 t) h refl\n\n------------------------------------------------------------------------------\n-- From FOTC.Data.Bool.PropertiesI\n\n-- Congruence properties\n\n&&-leftCong : \u2200 {a b c} \u2192 a \u2261 b \u2192 a && c \u2261 b && c\n&&-leftCong {a} {c = c} h = subst (\u03bb t \u2192 a && c \u2261 t && c) h refl\n\n&&-rightCong : \u2200 {a b c} \u2192 b \u2261 c \u2192 a && b \u2261 a && c\n&&-rightCong {a} {b} h = subst (\u03bb t \u2192 a && b \u2261 a && t) h refl\n\n&&-cong : \u2200 {a b c d } \u2192 a \u2261 c \u2192 b \u2261 d \u2192 a && b \u2261 c && d\n&&-cong {a} {b} h\u2081 h\u2082 = subst\u2082 (\u03bb t\u2081 t\u2082 \u2192 a && b \u2261 t\u2081 && t\u2082) h\u2081 h\u2082 refl\n\nnotCong : \u2200 {a b} \u2192 a \u2261 b \u2192 not a \u2261 not b\nnotCong {a} h = subst (\u03bb t \u2192 not a \u2261 not t) h refl\n\n------------------------------------------------------------------------------\n-- FOTC.Data.Conat.Equality.PropertiesI\n\n\u2248N-refl : \u2200 {n} \u2192 Conat n \u2192 n \u2248N n\n\u2248N-refl {n} Cn = \u2248N-coind R h\u2081 h\u2082\n where\n R : D \u2192 D \u2192 Set\n R a b = Conat a \u2227 Conat b \u2227 a \u2261 b\n\n h\u2081 : \u2200 {a b} \u2192 R a b \u2192\n a \u2261 zero \u2227 b \u2261 zero\n \u2228 (\u2203[ a' ] \u2203[ b' ] a \u2261 succ\u2081 a' \u2227 b \u2261 succ\u2081 b' \u2227 R a' b')\n h\u2081 (Ca , Cb , h) with Conat-unf Ca\n ... | inj\u2081 prf = inj\u2081 (prf , trans (sym h) prf)\n ... | inj\u2082 (a' , prf , Ca') =\n inj\u2082 (a' , a' , prf , trans (sym h) prf , (Ca' , Ca' , refl))\n\n h\u2082 : R n n\n h\u2082 = Cn , Cn , refl\n\n\u2261\u2192\u2248N : \u2200 {m n} \u2192 Conat m \u2192 Conat n \u2192 m \u2261 n \u2192 m \u2248N n\n\u2261\u2192\u2248N {m} Cm _ h = subst (_\u2248N_ m) h (\u2248N-refl Cm)\n\n------------------------------------------------------------------------------\n-- FOTC.Data.List.PropertiesI\n\n-- Congruence properties\n\n++-leftCong : \u2200 {xs ys zs} \u2192 xs \u2261 ys \u2192 xs ++ zs \u2261 ys ++ zs\n++-leftCong {xs} {zs = zs} h = subst (\u03bb t \u2192 xs ++ zs \u2261 t ++ zs) h refl\n\n++-rightCong : \u2200 {xs ys zs} \u2192 ys \u2261 zs \u2192 xs ++ ys \u2261 xs ++ zs\n++-rightCong {xs} {ys} h = subst (\u03bb t \u2192 xs ++ ys \u2261 xs ++ t) h refl\n\nmapCong\u2082 : \u2200 {f xs ys} \u2192 xs \u2261 ys \u2192 map f xs \u2261 map f ys\nmapCong\u2082 {f} {xs} h = subst (\u03bb t \u2192 map f xs \u2261 map f t) h refl\n\nrevCong\u2081 : \u2200 {xs ys zs} \u2192 xs \u2261 ys \u2192 rev xs zs \u2261 rev ys zs\nrevCong\u2081 {xs} {zs = zs} h = subst (\u03bb t \u2192 rev xs zs \u2261 rev t zs) h refl\n\nrevCong\u2082 : \u2200 {xs ys zs} \u2192 ys \u2261 zs \u2192 rev xs ys \u2261 rev xs zs\nrevCong\u2082 {xs} {ys} h = subst (\u03bb t \u2192 rev xs ys \u2261 rev xs t) h refl\n\nreverseCong : \u2200 {xs ys} \u2192 xs \u2261 ys \u2192 reverse xs \u2261 reverse ys\nreverseCong {xs} h = subst (\u03bb t \u2192 reverse xs \u2261 reverse t) h refl\n\nlengthCong : \u2200 {xs ys} \u2192 xs \u2261 ys \u2192 length xs \u2261 length ys\nlengthCong {xs} h = subst (\u03bb t \u2192 length xs \u2261 length t) h refl\n\n------------------------------------------------------------------------------\n-- From FOTC.Data.Nat.Inequalities.PropertiesI\n\n-- Congruence properties\n\nleLeftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 le m o \u2261 le n o\nleLeftCong {m} {o = o} h = subst (\u03bb t \u2192 le m o \u2261 le t o) h refl\n\nltLeftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 lt m o \u2261 lt n o\nltLeftCong {m} {o = o} h = subst (\u03bb t \u2192 lt m o \u2261 lt t o) h refl\n\nltRightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 lt m n \u2261 lt m o\nltRightCong {m} {n} h = subst (\u03bb t \u2192 lt m n \u2261 lt m t) h refl\n\nltCong : \u2200 {m\u2081 n\u2081 m\u2082 n\u2082} \u2192 m\u2081 \u2261 m\u2082 \u2192 n\u2081 \u2261 n\u2082 \u2192 lt m\u2081 n\u2081 \u2261 lt m\u2082 n\u2082\nltCong {m\u2081} {n\u2081} h\u2081 h\u2082 = subst\u2082 (\u03bb t\u2081 t\u2082 \u2192 lt m\u2081 n\u2081 \u2261 lt t\u2081 t\u2082) h\u2081 h\u2082 refl\n\n------------------------------------------------------------------------------\n-- From FOTC.Data.Nat.PropertiesI\n\n-- Congruence properties\n\n+-leftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 m + o \u2261 n + o\n+-leftCong {m} {o = o} h = subst (\u03bb t \u2192 m + o \u2261 t + o) h refl\n\n+-rightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 m + n \u2261 m + o\n+-rightCong {m} {n} h = subst (\u03bb t \u2192 m + n \u2261 m + t) h refl\n\n\u2238-leftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 m \u2238 o \u2261 n \u2238 o\n\u2238-leftCong {m} {o = o} h = subst (\u03bb t \u2192 m \u2238 o \u2261 t \u2238 o) h refl\n\n\u2238-rightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 m \u2238 n \u2261 m \u2238 o\n\u2238-rightCong {m} {n} h = subst (\u03bb t \u2192 m \u2238 n \u2261 m \u2238 t) h refl\n\n*-leftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 m * o \u2261 n * o\n*-leftCong {m} {o = o} h = subst (\u03bb t \u2192 m * o \u2261 t * o) h refl\n\n*-rightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 m * n \u2261 m * o\n*-rightCong {m} {n} h = subst (\u03bb t \u2192 m * n \u2261 m * t) h refl\n\n------------------------------------------------------------------------------\n-- From FOT.FOTC.Data.Stream.Equality.PropertiesI where\n\nstream-\u2261\u2192\u2248 : \u2200 {xs ys} \u2192 Stream xs \u2192 Stream ys \u2192 xs \u2261 ys \u2192 xs \u2248 ys\nstream-\u2261\u2192\u2248 {xs} Sxs _ h = subst (_\u2248_ xs) h (\u2248-refl Sxs)\n\n------------------------------------------------------------------------------\n-- From FOTC.Program.McCarthy91.AuxiliaryPropertiesATP\n\nf\u2089\u2081-x\u2261y : \u2200 {m n o} \u2192 f\u2089\u2081 m \u2261 n \u2192 o \u2261 m \u2192 f\u2089\u2081 o \u2261 n\nf\u2089\u2081-x\u2261y {n = n} h\u2081 h\u2082 = subst (\u03bb t \u2192 f\u2089\u2081 t \u2261 n) (sym h\u2082) h\u2081\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"5ab3590c56f09a19fa5fe32ec9a9eacf498b4da6","subject":"One Bit->\ud835\udfda","message":"One Bit->\ud835\udfda\n","repos":"crypto-agda\/explore","old_file":"lib\/Explore\/Sum.agda","new_file":"lib\/Explore\/Sum.agda","new_contents":"{-\n\n The main definitions of this module are:\n\n * explore\u228e\n * explore\u228e-ind\n * adequate-sum\u228e\n\n-}\nopen import Type hiding (\u2605)\nopen import Function.NP\nopen import Data.Nat using (_+_)\nimport Level as L\nimport Function.Inverse.NP as FI\nimport Function.Related as FR\nopen FI using (_\u2194_; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nopen import Function.Related.TypeIsomorphisms.NP\nopen import Data.Product.NP\nopen import Data.Sum\nopen import Data.Bit\nopen import Data.Fin using (Fin)\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_ ; module \u2261-Reasoning; cong)\n\nopen import Explore.Type\nopen import Explore.Explorable\n\nmodule Explore.Sum where\n\nmodule _ {a b u} {A : \u2605 a} {B : \u2605 b} {U : \u2605 u}\n (_\u2219_ : U \u2192 U \u2192 U)\n (e\u2081 : (A \u2192 U) \u2192 U)\n (e\u2082 : (B \u2192 U) \u2192 U)\n (f : (A \u228e B) \u2192 U)\n where\n -- find a better place\/name for it\n \u228e\u1d9c : U\n \u228e\u1d9c = e\u2081 (f \u2218 inj\u2081) \u2219 e\u2082 (f \u2218 inj\u2082)\n\nmodule _ {m A B} where\n explore\u228e : Explore m A \u2192 Explore m B \u2192 Explore m (A \u228e B)\n explore\u228e explore\u1d2c explore\u1d2e _\u2219_ = \u228e\u1d9c _\u2219_ (explore\u1d2c _\u2219_) (explore\u1d2e _\u2219_)\n\n module _ {p} {s\u1d2c : Explore m A} {s\u1d2e : Explore m B} where\n explore\u228e-ind : ExploreInd p s\u1d2c \u2192 ExploreInd p s\u1d2e \u2192 ExploreInd p (explore\u228e s\u1d2c s\u1d2e)\n explore\u228e-ind Ps\u1d2c Ps\u1d2e P P\u2219 Pf\n -- TODO clean this up:\n = P\u2219 (Ps\u1d2c (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2081))) P\u2219 (Pf \u2218 inj\u2081))\n (Ps\u1d2e (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2082))) P\u2219 (Pf \u2218 inj\u2082))\n\ninfixr 4 _\u228e\u1d49_ _\u228e\u2071_ _\u228e\u02e2_\n_\u228e\u1d49_ = explore\u228e\n_\u228e\u2071_ = explore\u228e-ind\n\n_\u228e\u02e2_ : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u228e B)\n_\u228e\u02e2_ = \u228e\u1d9c _+_\n\nmodule _ {A B} {sum\u1d2c : Sum A} {sum\u1d2e : Sum B} where\n\n adequate-sum\u228e : AdequateSum sum\u1d2c \u2192 AdequateSum sum\u1d2e \u2192 AdequateSum (sum\u1d2c \u228e\u02e2 sum\u1d2e)\n adequate-sum\u228e asum\u1d2c asum\u1d2e f = (Fin (sum\u1d2c (f \u2218 inj\u2081) + sum\u1d2e (f \u2218 inj\u2082)))\n \u2194\u27e8 FI.sym (Fin-\u228e-+ _ _) \u27e9\n (Fin (sum\u1d2c (f \u2218 inj\u2081)) \u228e Fin (sum\u1d2e (f \u2218 inj\u2082)))\n \u2194\u27e8 asum\u1d2c (f \u2218 inj\u2081) \u228e-cong asum\u1d2e (f \u2218 inj\u2082) \u27e9\n (\u03a3 A (Fin \u2218 f \u2218 inj\u2081) \u228e \u03a3 B (Fin \u2218 f \u2218 inj\u2082))\n \u2194\u27e8 FI.sym \u03a3\u228e-distrib \u27e9\n \u03a3 (A \u228e B) (Fin \u2218 f)\n \u220e\n where open FR.EquationalReasoning\n\nmodule _ {A B} {s\u1d2c : Explore\u2081 A} {s\u1d2e : Explore\u2081 B} where\n s\u1d2c\u207a\u1d2e = s\u1d2c \u228e\u1d49 s\u1d2e\n _\u228e-focus_ : Focus s\u1d2c \u2192 Focus s\u1d2e \u2192 Focus s\u1d2c\u207a\u1d2e\n (f\u1d2c \u228e-focus f\u1d2e) (inj\u2081 x , y) = inj\u2081 (f\u1d2c (x , y))\n (f\u1d2c \u228e-focus f\u1d2e) (inj\u2082 x , y) = inj\u2082 (f\u1d2e (x , y))\n\n _\u228e-unfocus_ : Unfocus s\u1d2c \u2192 Unfocus s\u1d2e \u2192 Unfocus s\u1d2c\u207a\u1d2e\n _\u228e-unfocus_ f\u1d2c f\u1d2e (inj\u2081 x) = first inj\u2081 (f\u1d2c x)\n _\u228e-unfocus_ f\u1d2c f\u1d2e (inj\u2082 y) = first inj\u2082 (f\u1d2e y)\n\n {-\n _\u228e-focused_ : Focused s\u1d2c \u2192 Focused s\u1d2e \u2192 Focused {L.zero} s\u1d2c\u207a\u1d2e\n _\u228e-focused_ f\u1d2c f\u1d2e {B} = inverses (to f\u1d2c \u228e-focus to f\u1d2e) (from f\u1d2c \u228e-unfocus from f\u1d2e) (\u21d2) (\u21d0)\n where\n \u21d2 : (x : \u03a3 (A \u228e {!!}) {!!}) \u2192 _\n \u21d2 (x , y) = {!!}\n \u21d0 : (x : s\u1d2c _\u228e_ (B \u2218 inj\u2081) \u228e s\u1d2e _\u228e_ (B \u2218 inj\u2082)) \u2192 _\n \u21d0 (inj\u2081 x) = cong inj\u2081 {!!}\n \u21d0 (inj\u2082 x) = cong inj\u2082 {!!}\n -}\n\n _\u228e-lookup_ : Lookup s\u1d2c \u2192 Lookup s\u1d2e \u2192 Lookup (s\u1d2c \u228e\u1d49 s\u1d2e)\n (lookup\u1d2c \u228e-lookup lookup\u1d2e) (x , y) = [ lookup\u1d2c x , lookup\u1d2e y ]\n\n _\u228e-reify_ : Reify s\u1d2c \u2192 Reify s\u1d2e \u2192 Reify (s\u1d2c \u228e\u1d49 s\u1d2e)\n (reify\u1d2c \u228e-reify reify\u1d2e) f = (reify\u1d2c (f \u2218 inj\u2081)) , (reify\u1d2e (f \u2218 inj\u2082))\n\nexploreBit : \u2200 {m} \u2192 Explore m Bit\nexploreBit _\u2219_ f = f 0b \u2219 f 1b\n\nexploreBit-ind : \u2200 {m p} \u2192 ExploreInd p {m} exploreBit\nexploreBit-ind _ _P\u2219_ Pf = Pf 0b P\u2219 Pf 1b\n\nfocusBit : \u2200 {a} \u2192 Focus {a} exploreBit\nfocusBit (0b , x) = inj\u2081 x\nfocusBit (1b , x) = inj\u2082 x\n\nfocusedBit : Focused {L.zero} exploreBit\nfocusedBit {B} = inverses focusBit unfocus (\u21d2) (\u21d0)\n where open Explorable\u2081\u2081 exploreBit-ind\n \u21d2 : (x : \u03a3 Bit B) \u2192 _\n \u21d2 (0b , x) = \u2261.refl\n \u21d2 (1b , x) = \u2261.refl\n \u21d0 : (x : B 0b \u228e B 1b) \u2192 _\n \u21d0 (inj\u2081 x) = \u2261.refl\n \u21d0 (inj\u2082 x) = \u2261.refl\n\nlookupBit : \u2200 {a} \u2192 Lookup {a} exploreBit\nlookupBit = proj\n\n-- DEPRECATED\nmodule \u03bc where\n _\u228e-\u03bc_ : \u2200 {A B} \u2192 Explorable A \u2192 Explorable B \u2192 Explorable (A \u228e B)\n \u03bcA \u228e-\u03bc \u03bcB = mk _ (explore-ind \u03bcA \u228e\u2071 explore-ind \u03bcB)\n (adequate-sum\u228e (adequate-sum \u03bcA) (adequate-sum \u03bcB))\n\n \u03bcBit : Explorable Bit\n \u03bcBit = \u03bc-iso (FI.sym \ud835\udfda\u2194\ud835\udfd9\u228e\ud835\udfd9) (\u03bc\ud835\udfd9 \u228e-\u03bc \u03bc\ud835\udfd9)\n\n -- -}\n -- -}\n -- -}\n","old_contents":"{-\n\n The main definitions of this module are:\n\n * explore\u228e\n * explore\u228e-ind\n * adequate-sum\u228e\n\n-}\nopen import Type hiding (\u2605)\nopen import Function.NP\nopen import Data.Nat using (_+_)\nimport Level as L\nimport Function.Inverse.NP as FI\nimport Function.Related as FR\nopen FI using (_\u2194_; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nopen import Function.Related.TypeIsomorphisms.NP\nopen import Data.Product.NP\nopen import Data.Sum\nopen import Data.Bit\nopen import Data.Fin using (Fin)\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_ ; module \u2261-Reasoning; cong)\n\nopen import Explore.Type\nopen import Explore.Explorable\n\nmodule Explore.Sum where\n\nmodule _ {a b u} {A : \u2605 a} {B : \u2605 b} {U : \u2605 u}\n (_\u2219_ : U \u2192 U \u2192 U)\n (e\u2081 : (A \u2192 U) \u2192 U)\n (e\u2082 : (B \u2192 U) \u2192 U)\n (f : (A \u228e B) \u2192 U)\n where\n -- find a better place\/name for it\n \u228e\u1d9c : U\n \u228e\u1d9c = e\u2081 (f \u2218 inj\u2081) \u2219 e\u2082 (f \u2218 inj\u2082)\n\nmodule _ {m A B} where\n explore\u228e : Explore m A \u2192 Explore m B \u2192 Explore m (A \u228e B)\n explore\u228e explore\u1d2c explore\u1d2e _\u2219_ = \u228e\u1d9c _\u2219_ (explore\u1d2c _\u2219_) (explore\u1d2e _\u2219_)\n\n module _ {p} {s\u1d2c : Explore m A} {s\u1d2e : Explore m B} where\n explore\u228e-ind : ExploreInd p s\u1d2c \u2192 ExploreInd p s\u1d2e \u2192 ExploreInd p (explore\u228e s\u1d2c s\u1d2e)\n explore\u228e-ind Ps\u1d2c Ps\u1d2e P P\u2219 Pf\n -- TODO clean this up:\n = P\u2219 (Ps\u1d2c (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2081))) P\u2219 (Pf \u2218 inj\u2081))\n (Ps\u1d2e (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2082))) P\u2219 (Pf \u2218 inj\u2082))\n\ninfixr 4 _\u228e\u1d49_ _\u228e\u2071_ _\u228e\u02e2_\n_\u228e\u1d49_ = explore\u228e\n_\u228e\u2071_ = explore\u228e-ind\n\n_\u228e\u02e2_ : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u228e B)\n_\u228e\u02e2_ = \u228e\u1d9c _+_\n\nmodule _ {A B} {sum\u1d2c : Sum A} {sum\u1d2e : Sum B} where\n\n adequate-sum\u228e : AdequateSum sum\u1d2c \u2192 AdequateSum sum\u1d2e \u2192 AdequateSum (sum\u1d2c \u228e\u02e2 sum\u1d2e)\n adequate-sum\u228e asum\u1d2c asum\u1d2e f = (Fin (sum\u1d2c (f \u2218 inj\u2081) + sum\u1d2e (f \u2218 inj\u2082)))\n \u2194\u27e8 FI.sym (Fin-\u228e-+ _ _) \u27e9\n (Fin (sum\u1d2c (f \u2218 inj\u2081)) \u228e Fin (sum\u1d2e (f \u2218 inj\u2082)))\n \u2194\u27e8 asum\u1d2c (f \u2218 inj\u2081) \u228e-cong asum\u1d2e (f \u2218 inj\u2082) \u27e9\n (\u03a3 A (Fin \u2218 f \u2218 inj\u2081) \u228e \u03a3 B (Fin \u2218 f \u2218 inj\u2082))\n \u2194\u27e8 FI.sym \u03a3\u228e-distrib \u27e9\n \u03a3 (A \u228e B) (Fin \u2218 f)\n \u220e\n where open FR.EquationalReasoning\n\nmodule _ {A B} {s\u1d2c : Explore\u2081 A} {s\u1d2e : Explore\u2081 B} where\n s\u1d2c\u207a\u1d2e = s\u1d2c \u228e\u1d49 s\u1d2e\n _\u228e-focus_ : Focus s\u1d2c \u2192 Focus s\u1d2e \u2192 Focus s\u1d2c\u207a\u1d2e\n (f\u1d2c \u228e-focus f\u1d2e) (inj\u2081 x , y) = inj\u2081 (f\u1d2c (x , y))\n (f\u1d2c \u228e-focus f\u1d2e) (inj\u2082 x , y) = inj\u2082 (f\u1d2e (x , y))\n\n _\u228e-unfocus_ : Unfocus s\u1d2c \u2192 Unfocus s\u1d2e \u2192 Unfocus s\u1d2c\u207a\u1d2e\n _\u228e-unfocus_ f\u1d2c f\u1d2e (inj\u2081 x) = first inj\u2081 (f\u1d2c x)\n _\u228e-unfocus_ f\u1d2c f\u1d2e (inj\u2082 y) = first inj\u2082 (f\u1d2e y)\n\n {-\n _\u228e-focused_ : Focused s\u1d2c \u2192 Focused s\u1d2e \u2192 Focused {L.zero} s\u1d2c\u207a\u1d2e\n _\u228e-focused_ f\u1d2c f\u1d2e {B} = inverses (to f\u1d2c \u228e-focus to f\u1d2e) (from f\u1d2c \u228e-unfocus from f\u1d2e) (\u21d2) (\u21d0)\n where\n \u21d2 : (x : \u03a3 (A \u228e {!!}) {!!}) \u2192 _\n \u21d2 (x , y) = {!!}\n \u21d0 : (x : s\u1d2c _\u228e_ (B \u2218 inj\u2081) \u228e s\u1d2e _\u228e_ (B \u2218 inj\u2082)) \u2192 _\n \u21d0 (inj\u2081 x) = cong inj\u2081 {!!}\n \u21d0 (inj\u2082 x) = cong inj\u2082 {!!}\n -}\n\n _\u228e-lookup_ : Lookup s\u1d2c \u2192 Lookup s\u1d2e \u2192 Lookup (s\u1d2c \u228e\u1d49 s\u1d2e)\n (lookup\u1d2c \u228e-lookup lookup\u1d2e) (x , y) = [ lookup\u1d2c x , lookup\u1d2e y ]\n\n _\u228e-reify_ : Reify s\u1d2c \u2192 Reify s\u1d2e \u2192 Reify (s\u1d2c \u228e\u1d49 s\u1d2e)\n (reify\u1d2c \u228e-reify reify\u1d2e) f = (reify\u1d2c (f \u2218 inj\u2081)) , (reify\u1d2e (f \u2218 inj\u2082))\n\nexploreBit : \u2200 {m} \u2192 Explore m Bit\nexploreBit _\u2219_ f = f 0b \u2219 f 1b\n\nexploreBit-ind : \u2200 {m p} \u2192 ExploreInd p {m} exploreBit\nexploreBit-ind _ _P\u2219_ Pf = Pf 0b P\u2219 Pf 1b\n\nfocusBit : \u2200 {a} \u2192 Focus {a} exploreBit\nfocusBit (0b , x) = inj\u2081 x\nfocusBit (1b , x) = inj\u2082 x\n\nfocusedBit : Focused {L.zero} exploreBit\nfocusedBit {B} = inverses focusBit unfocus (\u21d2) (\u21d0)\n where open Explorable\u2081\u2081 exploreBit-ind\n \u21d2 : (x : \u03a3 Bit B) \u2192 _\n \u21d2 (0b , x) = \u2261.refl\n \u21d2 (1b , x) = \u2261.refl\n \u21d0 : (x : B 0b \u228e B 1b) \u2192 _\n \u21d0 (inj\u2081 x) = \u2261.refl\n \u21d0 (inj\u2082 x) = \u2261.refl\n\nlookupBit : \u2200 {a} \u2192 Lookup {a} exploreBit\nlookupBit = proj\n\n-- DEPRECATED\nmodule \u03bc where\n _\u228e-\u03bc_ : \u2200 {A B} \u2192 Explorable A \u2192 Explorable B \u2192 Explorable (A \u228e B)\n \u03bcA \u228e-\u03bc \u03bcB = mk _ (explore-ind \u03bcA \u228e\u2071 explore-ind \u03bcB)\n (adequate-sum\u228e (adequate-sum \u03bcA) (adequate-sum \u03bcB))\n\n \u03bcBit : Explorable Bit\n \u03bcBit = \u03bc-iso (FI.sym Bit\u2194\ud835\udfd9\u228e\ud835\udfd9) (\u03bc\ud835\udfd9 \u228e-\u03bc \u03bc\ud835\udfd9)\n\n -- -}\n -- -}\n -- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"6e770f559a5e16728cdbe21d12ba340aca39e6aa","subject":"disjointness lemma","message":"disjointness lemma\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"disjointness.agda","new_file":"disjointness.agda","new_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import lemmas-disjointness\nopen import exchange\nopen import lemmas-freshness\nopen import weakening\n\nmodule disjointness where\n mutual\n expand-new-disjoint-synth : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4'} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-synth HNConst ESConst = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNAsc hn) (ESAsc x) = expand-new-disjoint-ana hn x\n expand-new-disjoint-synth HNVar (ESVar x\u2081) = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNLam1 hn) ()\n expand-new-disjoint-synth (HNLam2 hn) (ESLam x\u2081 exp) = expand-new-disjoint-synth hn exp\n expand-new-disjoint-synth (HNHole x) ESEHole = disjoint-singles x\n expand-new-disjoint-synth (HNNEHole x hn) (ESNEHole x\u2081 exp) = disjoint-parts (expand-new-disjoint-synth hn exp) (disjoint-singles x)\n expand-new-disjoint-synth (HNAp hn hn\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) =\n disjoint-parts (expand-new-disjoint-ana hn x\u2084)\n (expand-new-disjoint-ana hn\u2081 x\u2085)\n\n expand-new-disjoint-ana : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4' \u03c42} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-ana hn (EASubsume x x\u2081 x\u2082 x\u2083) = expand-new-disjoint-synth hn x\u2082\n expand-new-disjoint-ana (HNLam1 hn) (EALam x\u2081 x\u2082 ex) = expand-new-disjoint-ana hn ex\n expand-new-disjoint-ana (HNHole x) EAEHole = disjoint-singles x\n expand-new-disjoint-ana (HNNEHole x hn) (EANEHole x\u2081 x\u2082) = disjoint-parts (expand-new-disjoint-synth hn x\u2082) (disjoint-singles x)\n\n mutual\n expand-disjoint-new-synth : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c4'} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-synth ESConst disj = HNConst\n expand-disjoint-new-synth (ESVar x\u2081) disj = HNVar\n expand-disjoint-new-synth (ESLam x\u2081 ex) disj = HNLam2 (expand-disjoint-new-synth ex disj)\n expand-disjoint-new-synth (ESAp {\u03941 = \u03941} x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) disj\n with expand-disjoint-new-ana x\u2084 (disjoint-union1 disj) | expand-disjoint-new-ana x\u2085 (disjoint-union2 {\u03931 = \u03941} disj)\n ... | ih1 | ih2 = HNAp ih1 ih2\n expand-disjoint-new-synth {\u0393 = \u0393} ESEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-synth (ESNEHole {\u0394 = \u0394} x ex) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth ex (disjoint-union1 disj))\n expand-disjoint-new-synth (ESAsc x) disj = HNAsc (expand-disjoint-new-ana x disj)\n\n expand-disjoint-new-ana : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c42 \u03c4'} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-ana (EALam x\u2081 x\u2082 ex) disj = HNLam1 (expand-disjoint-new-ana ex disj)\n expand-disjoint-new-ana (EASubsume x x\u2081 x\u2082 x\u2083) disj = expand-disjoint-new-synth x\u2082 disj\n expand-disjoint-new-ana EAEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-ana (EANEHole {\u0394 = \u0394} x x\u2081) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth x\u2081 (disjoint-union1 disj))\n\n -- collect up the hole names of a term as the indices of a trivial contex\n data holes : (e : hexp) (H : \u22a4 ctx) \u2192 Set where\n HConst : holes c \u2205\n HAsc : \u2200{e \u03c4 H} \u2192 holes e H \u2192 holes (e \u00b7: \u03c4) H\n HVar : \u2200{x} \u2192 holes (X x) \u2205\n HLam1 : \u2200{x e H} \u2192 holes e H \u2192 holes (\u00b7\u03bb x e) H\n HLam2 : \u2200{x e \u03c4 H} \u2192 holes e H \u2192 holes (\u00b7\u03bb x [ \u03c4 ] e) H\n HEHole : \u2200{u} \u2192 holes (\u2987\u2988[ u ]) (\u25a0 (u , <>))\n HNEHole : \u2200{e u H} \u2192 holes e H \u2192 holes (\u2987 e \u2988[ u ]) (H ,, (u , <>))\n HAp : \u2200{e1 e2 H1 H2} \u2192 holes e1 H1 \u2192 holes e2 H2 \u2192 holes (e1 \u2218 e2) (H1 \u222a H2)\n\n -- the above judgement has mode (\u2200,\u2203). this doesn't prove uniqueness; any\n -- contex that extends the one computed here will be indistinguishable\n -- but we'll treat this one as canonical\n find-holes : (e : hexp) \u2192 \u03a3[ H \u2208 \u22a4 ctx ](holes e H)\n find-holes c = \u2205 , HConst\n find-holes (e \u00b7: x) with find-holes e\n ... | (h , d)= h , (HAsc d)\n find-holes (X x) = \u2205 , HVar\n find-holes (\u00b7\u03bb x e) with find-holes e\n ... | (h , d) = h , HLam1 d\n find-holes (\u00b7\u03bb x [ x\u2081 ] e) with find-holes e\n ... | (h , d) = h , HLam2 d\n find-holes \u2987\u2988[ x ] = (\u25a0 (x , <>)) , HEHole\n find-holes \u2987 e \u2988[ x ] with find-holes e\n ... | (h , d) = h ,, (x , <>) , HNEHole d\n find-holes (e1 \u2218 e2) with find-holes e1 | find-holes e2\n ... | (h1 , d1) | (h2 , d2) = (h1 \u222a h2 ) , (HAp d1 d2)\n\n -- two contexts that may contain different mappings have the same domain\n dom-eq : {A B : Set} \u2192 A ctx \u2192 B ctx \u2192 Set\n dom-eq {A} {B} C1 C2 = ((n : Nat) \u2192 \u03a3[ x \u2208 A ]( C1 n == Some x) \u2192 (\u03a3[ y \u2208 B ](C2 n == Some y)))\u00d7\n ((n : Nat) \u2192 \u03a3[ y \u2208 B ]( C2 n == Some y) \u2192 (\u03a3[ x \u2208 A ](C1 n == Some x)))\n\n -- the empty context has the same domain as itself\n dom-\u2205 : {A B : Set} \u2192 dom-eq (\u03bb _ \u2192 None {A}) (\u03bb _ \u2192 None {B})\n dom-\u2205 {A} {B} = (\u03bb n x \u2192 abort (somenotnone (! (\u03c02 x)))) , (\u03bb n x \u2192 abort (somenotnone (! (\u03c02 x))))\n\n -- todo: this seems like i would have proven it already? otw move to lemmas\n singleton-eq : {A : Set} {a : A} \u2192 \u2200{x n y} \u2192 (\u25a0 (x , a)) n == Some y \u2192 x == n\n singleton-eq {A} {a} {x} {n} {y} eq with natEQ x n\n singleton-eq eq | Inl x\u2081 = x\u2081\n singleton-eq eq | Inr x\u2081 = abort (somenotnone (! eq))\n\n -- todo: this seems like i would have proven it already? otw move to lemmas\n singleton-lookup-refl : {A : Set} {n : Nat} {\u03b2 : A} \u2192 (\u25a0 (n , \u03b2)) n == Some \u03b2\n singleton-lookup-refl {n = n} with natEQ n n\n singleton-lookup-refl | Inl refl = \u03bb {\u03b2} \u2192 refl\n singleton-lookup-refl | Inr x = abort (x refl)\n\n -- the singleton contexts formed with any contents but the same index has\n -- the same domain\n dom-single : {A B : Set} (x : Nat) (a : A) (b : B) \u2192 dom-eq (\u25a0 (x , a)) (\u25a0 (x , b))\n dom-single {A} {B} x \u03b1 \u03b2 = (\u03bb n x\u2081 \u2192 \u03b2 , (ap1 (\u03bb qq \u2192 (\u25a0 (qq , \u03b2)) n) (singleton-eq (\u03c02 x\u2081)) \u00b7 singleton-lookup-refl)) ,\n (\u03bb n x\u2081 \u2192 \u03b1 , (ap1 (\u03bb qq \u2192 (\u25a0 (qq , \u03b1)) n) (singleton-eq (\u03c02 x\u2081)) \u00b7 singleton-lookup-refl))\n\n lem-dom-union-apt1 : {A : Set} {\u03941 \u03942 : A ctx} {x : Nat} {y : A} \u2192 x # \u03941 \u2192 ((\u03941 \u222a \u03942) x == Some y) \u2192 (\u03942 x == Some y)\n lem-dom-union-apt1 {A} {\u03941} {\u03942} {x} {y} apt xin with \u03941 x\n lem-dom-union-apt1 apt xin | Some x\u2081 = abort (somenotnone apt)\n lem-dom-union-apt1 apt xin | None = xin\n\n lem-dom-union-apt2 : {A : Set} {\u03941 \u03942 : A ctx} {x : Nat} {y : A} \u2192 x # \u03942 \u2192 ((\u03941 \u222a \u03942) x == Some y) \u2192 (\u03941 x == Some y)\n lem-dom-union-apt2 {A} {\u03941} {\u03942} {x} {y} apt xin with \u03941 x\n lem-dom-union-apt2 apt xin | Some x\u2081 = xin\n lem-dom-union-apt2 apt xin | None = abort (somenotnone (! xin \u00b7 apt))\n\n -- if two disjoint sets each share a domain with two other sets, those\n -- are also disjoint.\n dom-eq-disj : {A B : Set} {\u03941 \u03942 : A ctx} {H1 H2 : B ctx} \u2192\n H1 ## H2 \u2192\n dom-eq \u03941 H1 \u2192\n dom-eq \u03942 H2 \u2192\n \u03941 ## \u03942\n dom-eq-disj {A} {B} {\u03941} {\u03942} {H1} {H2} (d1 , d2) (de1 , de2) (de3 , de4) = guts1 , guts2\n where\n guts1 : (n : Nat) \u2192 dom \u03941 n \u2192 n # \u03942\n guts1 n dom1 with ctxindirect H2 n\n guts1 n dom1 | Inl x = abort (somenotnone (! (\u03c02 x) \u00b7 d1 n (de1 n dom1)))\n guts1 n dom1 | Inr x with ctxindirect \u03942 n\n guts1 n dom1 | Inr x\u2081 | Inl x = abort (somenotnone (! (\u03c02 (de3 n x)) \u00b7 x\u2081))\n guts1 n dom1 | Inr x\u2081 | Inr x = x\n\n guts2 : (n : Nat) \u2192 dom \u03942 n \u2192 n # \u03941\n guts2 n dom2 with ctxindirect H1 n\n guts2 n dom2 | Inl x = abort (somenotnone (! (\u03c02 x) \u00b7 d2 n (de3 n dom2)))\n guts2 n dom2 | Inr x with ctxindirect \u03941 n\n guts2 n dom2 | Inr x\u2081 | Inl x = abort (somenotnone (! (\u03c02 (de1 n x)) \u00b7 x\u2081))\n guts2 n dom2 | Inr x\u2081 | Inr x = x\n\n dom-union : {A B : Set} {\u03941 \u03942 : A ctx} {H1 H2 : B ctx} \u2192\n H1 ## H2 \u2192\n dom-eq \u03941 H1 \u2192\n dom-eq \u03942 H2 \u2192\n dom-eq (\u03941 \u222a \u03942) (H1 \u222a H2)\n dom-union {A} {B} {\u03941} {\u03942} {H1} {H2} disj (p1 , p2) (p3 , p4) = guts1 , guts2\n where\n guts1 : (n : Nat) \u2192\n \u03a3[ x \u2208 A ] ((\u03941 \u222a \u03942) n == Some x) \u2192\n \u03a3[ y \u2208 B ] ((H1 \u222a H2) n == Some y)\n guts1 n (x , eq) with ctxindirect \u03941 n\n guts1 n (x\u2081 , eq) | Inl x with p1 n x\n ... | q1 , q2 = q1 , x\u2208\u222al H1 H2 n q1 q2\n guts1 n (x\u2081 , eq) | Inr x with p3 n (_ , lem-dom-union-apt1 {\u03941 = \u03941} {\u03942 = \u03942} x eq)\n ... | q1 , q2 = q1 , x\u2208\u222ar H1 H2 n q1 q2 (##-comm disj)\n\n guts2 : (n : Nat) \u2192\n \u03a3[ y \u2208 B ] ((H1 \u222a H2) n == Some y) \u2192\n \u03a3[ x \u2208 A ] ((\u03941 \u222a \u03942) n == Some x)\n guts2 n (x , eq) with ctxindirect H1 n\n guts2 n (x\u2081 , eq) | Inl x with p2 n x\n ... | q1 , q2 = q1 , x\u2208\u222al \u03941 \u03942 n q1 q2\n guts2 n (x\u2081 , eq) | Inr x with p4 n (_ , lem-dom-union-apt2 {\u03941 = H2} {\u03942 = H1} x (tr (\u03bb qq \u2192 qq n == Some x\u2081) (\u222acomm H1 H2 disj) eq))\n ... | q1 , q2 = q1 , x\u2208\u222ar \u03941 \u03942 n q1 q2 (##-comm (dom-eq-disj disj (p1 , p2) (p3 , p4)))\n\n\n\n -- the holes of an expression have the same domain as \u0394; that is, we\n -- don't add any extra junk as we expand\n mutual\n holes-delta-ana : \u2200{\u0393 H e \u03c4 d \u03c4' \u0394} \u2192\n holes e H \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n dom-eq \u0394 H\n holes-delta-ana (HLam1 h) (EALam x\u2081 x\u2082 exp) = holes-delta-ana h exp\n holes-delta-ana h (EASubsume x x\u2081 x\u2082 x\u2083) = holes-delta-synth h x\u2082\n holes-delta-ana (HEHole {u = u}) EAEHole = dom-single u _ _\n holes-delta-ana (HNEHole {u = u} h) (EANEHole x x\u2081) = dom-union {!!} (holes-delta-synth h x\u2081) (dom-single u _ _ )\n\n holes-delta-synth : \u2200{\u0393 H e \u03c4 d \u0394} \u2192\n holes e H \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n dom-eq \u0394 H\n holes-delta-synth HConst ESConst = dom-\u2205\n holes-delta-synth (HAsc h) (ESAsc x) = holes-delta-ana h x\n holes-delta-synth HVar (ESVar x\u2081) = dom-\u2205\n holes-delta-synth (HLam2 h) (ESLam x\u2081 exp) = holes-delta-synth h exp\n holes-delta-synth (HEHole {u = u}) ESEHole = dom-single u _ _\n holes-delta-synth (HNEHole {u = u} h) (ESNEHole x exp) = dom-union {!!} (holes-delta-synth h exp) (dom-single u _ _)\n holes-delta-synth (HAp h h\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) = dom-union {!!} (holes-delta-ana h x\u2084) (holes-delta-ana h\u2081 x\u2085)\n\n -- if a hole name is new then it's apart from the holes\n lem-apart-new : \u2200{e H u} \u2192 holes e H \u2192 hole-name-new e u \u2192 u # H\n lem-apart-new HConst HNConst = refl\n lem-apart-new (HAsc h) (HNAsc hn) = lem-apart-new h hn\n lem-apart-new HVar HNVar = refl\n lem-apart-new (HLam1 h) (HNLam1 hn) = lem-apart-new h hn\n lem-apart-new (HLam2 h) (HNLam2 hn) = lem-apart-new h hn\n lem-apart-new HEHole (HNHole x) = apart-singleton (flip x)\n lem-apart-new (HNEHole {u = u'} {H = H} h) (HNNEHole {u = u} x hn) = apart-parts H (\u25a0 (u' , <>)) u (lem-apart-new h hn) (apart-singleton (flip x))\n lem-apart-new (HAp {H1 = H1} {H2 = H2} h h\u2081) (HNAp hn hn\u2081) = apart-parts H1 H2 _ (lem-apart-new h hn) (lem-apart-new h\u2081 hn\u2081)\n\n -- todo: lemmas file?\n lem-dom-apt : {A : Set} {G : A ctx} {x y : Nat} \u2192 x # G \u2192 dom G y \u2192 x \u2260 y\n lem-dom-apt {x = x} {y = y} apt dom with natEQ x y\n lem-dom-apt apt dom | Inl refl = abort (somenotnone (! (\u03c02 dom) \u00b7 apt))\n lem-dom-apt apt dom | Inr x\u2081 = x\u2081\n\n -- if the holes of two expressions are disjoint, so are their collections\n -- of hole names\n holes-disjoint-disjoint : \u2200{ e1 e2 H1 H2} \u2192\n holes e1 H1 \u2192\n holes e2 H2 \u2192\n holes-disjoint e1 e2 \u2192\n H1 ## H2\n holes-disjoint-disjoint HConst he2 HDConst = empty-disj _\n holes-disjoint-disjoint (HAsc he1) he2 (HDAsc hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint HVar he2 HDVar = empty-disj _\n holes-disjoint-disjoint (HLam1 he1) he2 (HDLam1 hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint (HLam2 he1) he2 (HDLam2 hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint HEHole he2 (HDHole x) = lem-apart-sing-disj (lem-apart-new he2 x)\n holes-disjoint-disjoint (HNEHole he1) he2 (HDNEHole x hd) = disjoint-parts (holes-disjoint-disjoint he1 he2 hd) (lem-apart-sing-disj (lem-apart-new he2 x))\n holes-disjoint-disjoint (HAp he1 he2) he3 (HDAp hd hd\u2081) = disjoint-parts (holes-disjoint-disjoint he1 he3 hd) (holes-disjoint-disjoint he2 he3 hd\u2081)\n\n -- if two contexsts are disjoint and each share a domain with another\n -- context, those other two contexts are also disjoint\n domeq-disj : {A B : Set} {H1 H2 : A ctx} {\u03941 \u03942 : B ctx} \u2192\n H1 ## H2 \u2192\n dom-eq \u03941 H1 \u2192\n dom-eq \u03942 H2 \u2192\n \u03941 ## \u03942\n domeq-disj (\u03c01 , \u03c02) (\u03c03 , \u03c04) (\u03c05 , \u03c06) =\n (\u03bb n x \u2192 {!(\u03c03 n x)!}) ,\n (\u03bb n x \u2192 {!!})\n\n -- if you expand two hole-disjoint expressions analytically, the \u0394s\n -- produces are disjoint\n expand-ana-disjoint : \u2200{ e1 e2 \u03c41 \u03c42 e1' e2' \u03c41' \u03c42' \u0393 \u03941 \u03942 } \u2192\n holes-disjoint e1 e2 \u2192\n \u0393 \u22a2 e1 \u21d0 \u03c41 ~> e1' :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> e2' :: \u03c42' \u22a3 \u03942 \u2192\n \u03941 ## \u03942\n expand-ana-disjoint {e1} {e2} hd ana1 ana2\n with find-holes e1 | find-holes e2\n ... | (_ , he1) | (_ , he2) = domeq-disj (holes-disjoint-disjoint he1 he2 hd)\n (holes-delta-ana he1 ana1)\n (holes-delta-ana he2 ana2)\n\n -- these lemmas are all structurally recursive and quite\n -- mechanical. morally, they establish the properties about reduction\n -- that would be obvious \/ baked into Agda if holes-disjoint was defined\n -- as a function rather than a judgement (datatype), or if we had defined\n -- all the O(n^2) cases rather than relying on a little indirection to\n -- only have O(n) cases. that work has to go somewhwere, and we prefer\n -- that it goes here.\n ds-lem-asc : \u2200{e1 e2 \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (e1 \u00b7: \u03c4)\n ds-lem-asc HDConst = HDConst\n ds-lem-asc (HDAsc hd) = HDAsc (ds-lem-asc hd)\n ds-lem-asc HDVar = HDVar\n ds-lem-asc (HDLam1 hd) = HDLam1 (ds-lem-asc hd)\n ds-lem-asc (HDLam2 hd) = HDLam2 (ds-lem-asc hd)\n ds-lem-asc (HDHole x) = HDHole (HNAsc x)\n ds-lem-asc (HDNEHole x hd) = HDNEHole (HNAsc x) (ds-lem-asc hd)\n ds-lem-asc (HDAp hd hd\u2081) = HDAp (ds-lem-asc hd) (ds-lem-asc hd\u2081)\n\n ds-lem-lam1 : \u2200{e1 e2 x} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x e1)\n ds-lem-lam1 HDConst = HDConst\n ds-lem-lam1 (HDAsc hd) = HDAsc (ds-lem-lam1 hd)\n ds-lem-lam1 HDVar = HDVar\n ds-lem-lam1 (HDLam1 hd) = HDLam1 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDLam2 hd) = HDLam2 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDHole x\u2081) = HDHole (HNLam1 x\u2081)\n ds-lem-lam1 (HDNEHole x\u2081 hd) = HDNEHole (HNLam1 x\u2081) (ds-lem-lam1 hd)\n ds-lem-lam1 (HDAp hd hd\u2081) = HDAp (ds-lem-lam1 hd) (ds-lem-lam1 hd\u2081)\n\n ds-lem-lam2 : \u2200{e1 e2 x \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x [ \u03c4 ] e1)\n ds-lem-lam2 HDConst = HDConst\n ds-lem-lam2 (HDAsc hd) = HDAsc (ds-lem-lam2 hd)\n ds-lem-lam2 HDVar = HDVar\n ds-lem-lam2 (HDLam1 hd) = HDLam1 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDLam2 hd) = HDLam2 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDHole x\u2081) = HDHole (HNLam2 x\u2081)\n ds-lem-lam2 (HDNEHole x\u2081 hd) = HDNEHole (HNLam2 x\u2081) (ds-lem-lam2 hd)\n ds-lem-lam2 (HDAp hd hd\u2081) = HDAp (ds-lem-lam2 hd) (ds-lem-lam2 hd\u2081)\n\n ds-lem-nehole : \u2200{e e1 u} \u2192 holes-disjoint e e1 \u2192 hole-name-new e u \u2192 holes-disjoint e \u2987 e1 \u2988[ u ]\n ds-lem-nehole HDConst \u03bd = HDConst\n ds-lem-nehole (HDAsc hd) (HNAsc \u03bd) = HDAsc (ds-lem-nehole hd \u03bd)\n ds-lem-nehole HDVar \u03bd = HDVar\n ds-lem-nehole (HDLam1 hd) (HNLam1 \u03bd) = HDLam1 (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDLam2 hd) (HNLam2 \u03bd) = HDLam2 (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDHole x) (HNHole x\u2081) = HDHole (HNNEHole (flip x\u2081) x)\n ds-lem-nehole (HDNEHole x hd) (HNNEHole x\u2081 \u03bd) = HDNEHole (HNNEHole (flip x\u2081) x) (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDAp hd hd\u2081) (HNAp \u03bd \u03bd\u2081) = HDAp (ds-lem-nehole hd \u03bd) (ds-lem-nehole hd\u2081 \u03bd\u2081)\n\n ds-lem-ap : \u2200{e1 e2 e3} \u2192 holes-disjoint e3 e1 \u2192 holes-disjoint e3 e2 \u2192 holes-disjoint e3 (e1 \u2218 e2)\n ds-lem-ap HDConst hd2 = HDConst\n ds-lem-ap (HDAsc hd1) (HDAsc hd2) = HDAsc (ds-lem-ap hd1 hd2)\n ds-lem-ap HDVar hd2 = HDVar\n ds-lem-ap (HDLam1 hd1) (HDLam1 hd2) = HDLam1 (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDLam2 hd1) (HDLam2 hd2) = HDLam2 (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDHole x) (HDHole x\u2081) = HDHole (HNAp x x\u2081)\n ds-lem-ap (HDNEHole x hd1) (HDNEHole x\u2081 hd2) = HDNEHole (HNAp x x\u2081) (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDAp hd1 hd2) (HDAp hd3 hd4) = HDAp (ds-lem-ap hd1 hd3) (ds-lem-ap hd2 hd4)\n\n -- holes-disjoint is symmetric\n disjoint-sym : (e1 e2 : hexp) \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint e2 e1\n disjoint-sym .c c HDConst = HDConst\n disjoint-sym .c (e2 \u00b7: x) HDConst = HDAsc (disjoint-sym _ _ HDConst)\n disjoint-sym .c (X x) HDConst = HDVar\n disjoint-sym .c (\u00b7\u03bb x e2) HDConst = HDLam1 (disjoint-sym c e2 HDConst)\n disjoint-sym .c (\u00b7\u03bb x [ x\u2081 ] e2) HDConst = HDLam2 (disjoint-sym c e2 HDConst)\n disjoint-sym .c \u2987\u2988[ x ] HDConst = HDHole HNConst\n disjoint-sym .c \u2987 e2 \u2988[ x ] HDConst = HDNEHole HNConst (disjoint-sym c e2 HDConst)\n disjoint-sym .c (e2 \u2218 e3) HDConst = HDAp (disjoint-sym c e2 HDConst) (disjoint-sym c e3 HDConst)\n\n disjoint-sym _ c (HDAsc hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) | HDAsc ih = HDAsc (ds-lem-asc ih)\n disjoint-sym _ (X x) (HDAsc hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) | HDLam1 ih = HDLam1 (ds-lem-asc ih)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) | HDLam2 ih = HDLam2 (ds-lem-asc ih)\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) | HDHole x\u2081 = HDHole (HNAsc x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) | HDNEHole x\u2081 ih = HDNEHole (HNAsc x\u2081) (ds-lem-asc ih)\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) | HDAp ih ih\u2081 = HDAp (ds-lem-asc ih) (ds-lem-asc ih\u2081)\n\n disjoint-sym _ c HDVar = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) HDVar = HDAsc (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (X x\u2081) HDVar = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) HDVar = HDLam1 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) HDVar = HDLam2 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] HDVar = HDHole HNVar\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] HDVar = HDNEHole HNVar (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (e2 \u2218 e3) HDVar = HDAp (disjoint-sym _ e2 HDVar) (disjoint-sym _ e3 HDVar)\n\n disjoint-sym _ c (HDLam1 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) | HDAsc ih = HDAsc (ds-lem-lam1 ih)\n disjoint-sym _ (X x\u2081) (HDLam1 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) | HDLam1 ih = HDLam1 (ds-lem-lam1 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) | HDLam2 ih = HDLam2 (ds-lem-lam1 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) | HDHole x = HDHole (HNLam1 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) | HDNEHole x ih = HDNEHole (HNLam1 x) (ds-lem-lam1 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam1 ih) (ds-lem-lam1 ih\u2081)\n\n disjoint-sym _ c (HDLam2 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) | HDAsc ih = HDAsc (ds-lem-lam2 ih)\n disjoint-sym _ (X x\u2081) (HDLam2 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) | HDLam1 ih = HDLam1 (ds-lem-lam2 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) | HDLam2 ih = HDLam2 (ds-lem-lam2 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) | HDHole x = HDHole (HNLam2 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) | HDNEHole x ih = HDNEHole (HNLam2 x) (ds-lem-lam2 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam2 ih) (ds-lem-lam2 ih\u2081)\n\n disjoint-sym _ c (HDHole x) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDHole (HNAsc x\u2081)) = HDAsc (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (X x) (HDHole x\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDHole (HNLam1 x\u2081)) = HDLam1 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDHole (HNLam2 x\u2082)) = HDLam2 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2082))\n disjoint-sym _ \u2987\u2988[ x ] (HDHole (HNHole x\u2081)) = HDHole (HNHole (flip x\u2081))\n disjoint-sym _ \u2987 e2 \u2988[ u' ] (HDHole (HNNEHole x x\u2081)) = HDNEHole (HNHole (flip x)) (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (e2 \u2218 e3) (HDHole (HNAp x x\u2081)) = HDAp (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x))\n (disjoint-sym \u2987\u2988[ _ ] e3 (HDHole x\u2081))\n\n disjoint-sym _ c (HDNEHole x hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e \u00b7: x) (HDNEHole (HNAsc x\u2081) hd) | HDAsc ih = HDAsc (ds-lem-nehole ih x\u2081)\n disjoint-sym _ (X x) (HDNEHole x\u2081 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole (HNLam1 x\u2081) hd) | HDLam1 ih = HDLam1 (ds-lem-nehole ih x\u2081)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole x\u2082 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole (HNLam2 x\u2082) hd) | HDLam2 ih = HDLam2 (ds-lem-nehole ih x\u2082)\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole (HNHole x\u2082) hd) | HDHole x\u2081 = HDHole (HNNEHole (flip x\u2082) x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole (HNNEHole x\u2082 x\u2083) hd) | HDNEHole x\u2081 ih = HDNEHole (HNNEHole (flip x\u2082) x\u2081) (ds-lem-nehole ih x\u2083)\n disjoint-sym _ (e2 \u2218 e3) (HDNEHole x hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e1 \u2218 e3) (HDNEHole (HNAp x x\u2081) hd) | HDAp ih ih\u2081 = HDAp (ds-lem-nehole ih x) (ds-lem-nehole ih\u2081 x\u2081)\n\n disjoint-sym _ c (HDAp hd hd\u2081) = HDConst\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) | HDAsc ih | HDAsc ih1 = HDAsc (ds-lem-ap ih ih1)\n disjoint-sym _ (X x) (HDAp hd hd\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) | HDLam1 ih | HDLam1 ih1 = HDLam1 (ds-lem-ap ih ih1)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) | HDLam2 ih | HDLam2 ih1 = HDLam2 (ds-lem-ap ih ih1)\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) | HDHole x\u2081 | HDHole x\u2082 = HDHole (HNAp x\u2081 x\u2082)\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) | HDNEHole x\u2081 ih | HDNEHole x\u2082 ih1 = HDNEHole (HNAp x\u2081 x\u2082) (ds-lem-ap ih ih1)\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) | HDAp ih ih\u2081 | HDAp ih1 ih2 = HDAp (ds-lem-ap ih ih1) (ds-lem-ap ih\u2081 ih2)\n\n\n -- note that this is false, so holes-disjoint isn't transitive\n -- disjoint-new : \u2200{e1 e2 u} \u2192 holes-disjoint e1 e2 \u2192 hole-name-new e1 u \u2192 hole-name-new e2 u\n\n -- it's also not reflexive, because \u2987\u2988[ u ] isn't hole-disjoint with\n -- itself since refl : u == u; it's also not anti-reflexive, because the\n -- expression c *is* hole-disjoint with itself (albeit vacuously)\n","old_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import lemmas-disjointness\nopen import exchange\nopen import lemmas-freshness\nopen import weakening\n\nmodule disjointness where\n mutual\n expand-new-disjoint-synth : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4'} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-synth HNConst ESConst = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNAsc hn) (ESAsc x) = expand-new-disjoint-ana hn x\n expand-new-disjoint-synth HNVar (ESVar x\u2081) = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNLam1 hn) ()\n expand-new-disjoint-synth (HNLam2 hn) (ESLam x\u2081 exp) = expand-new-disjoint-synth hn exp\n expand-new-disjoint-synth (HNHole x) ESEHole = disjoint-singles x\n expand-new-disjoint-synth (HNNEHole x hn) (ESNEHole x\u2081 exp) = disjoint-parts (expand-new-disjoint-synth hn exp) (disjoint-singles x)\n expand-new-disjoint-synth (HNAp hn hn\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) =\n disjoint-parts (expand-new-disjoint-ana hn x\u2084)\n (expand-new-disjoint-ana hn\u2081 x\u2085)\n\n expand-new-disjoint-ana : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4' \u03c42} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-ana hn (EASubsume x x\u2081 x\u2082 x\u2083) = expand-new-disjoint-synth hn x\u2082\n expand-new-disjoint-ana (HNLam1 hn) (EALam x\u2081 x\u2082 ex) = expand-new-disjoint-ana hn ex\n expand-new-disjoint-ana (HNHole x) EAEHole = disjoint-singles x\n expand-new-disjoint-ana (HNNEHole x hn) (EANEHole x\u2081 x\u2082) = disjoint-parts (expand-new-disjoint-synth hn x\u2082) (disjoint-singles x)\n\n mutual\n expand-disjoint-new-synth : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c4'} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-synth ESConst disj = HNConst\n expand-disjoint-new-synth (ESVar x\u2081) disj = HNVar\n expand-disjoint-new-synth (ESLam x\u2081 ex) disj = HNLam2 (expand-disjoint-new-synth ex disj)\n expand-disjoint-new-synth (ESAp {\u03941 = \u03941} x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) disj\n with expand-disjoint-new-ana x\u2084 (disjoint-union1 disj) | expand-disjoint-new-ana x\u2085 (disjoint-union2 {\u03931 = \u03941} disj)\n ... | ih1 | ih2 = HNAp ih1 ih2\n expand-disjoint-new-synth {\u0393 = \u0393} ESEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-synth (ESNEHole {\u0394 = \u0394} x ex) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth ex (disjoint-union1 disj))\n expand-disjoint-new-synth (ESAsc x) disj = HNAsc (expand-disjoint-new-ana x disj)\n\n expand-disjoint-new-ana : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c42 \u03c4'} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-ana (EALam x\u2081 x\u2082 ex) disj = HNLam1 (expand-disjoint-new-ana ex disj)\n expand-disjoint-new-ana (EASubsume x x\u2081 x\u2082 x\u2083) disj = expand-disjoint-new-synth x\u2082 disj\n expand-disjoint-new-ana EAEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-ana (EANEHole {\u0394 = \u0394} x x\u2081) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth x\u2081 (disjoint-union1 disj))\n\n data holes : (e : hexp) (H : \u22a4 ctx) \u2192 Set where\n HConst : holes c \u2205\n HAsc : \u2200{e \u03c4 H} \u2192 holes e H \u2192 holes (e \u00b7: \u03c4) H\n HVar : \u2200{x} \u2192 holes (X x) \u2205\n HLam1 : \u2200{x e H} \u2192 holes e H \u2192 holes (\u00b7\u03bb x e) H\n HLam2 : \u2200{x e \u03c4 H} \u2192 holes e H \u2192 holes (\u00b7\u03bb x [ \u03c4 ] e) H\n HEHole : \u2200{u} \u2192 holes (\u2987\u2988[ u ]) (\u25a0 (u , <>))\n HNEHole : \u2200{e u H} \u2192 holes e H \u2192 holes (\u2987 e \u2988[ u ]) (H ,, (u , <>))\n HAp : \u2200{e1 e2 H1 H2} \u2192 holes e1 H1 \u2192 holes e2 H2 \u2192 holes (e1 \u2218 e2) (H1 \u222a H2)\n\n -- proving that the above judgement has mode (\u2200,\u2203), or that it defines a\n -- function, so we can use it in a functional modality below\n find-holes : (e : hexp) \u2192 \u03a3[ H \u2208 \u22a4 ctx ](holes e H)\n find-holes c = \u2205 , HConst\n find-holes (e \u00b7: x) with find-holes e\n ... | (h , d)= h , (HAsc d)\n find-holes (X x) = \u2205 , HVar\n find-holes (\u00b7\u03bb x e) with find-holes e\n ... | (h , d) = h , HLam1 d\n find-holes (\u00b7\u03bb x [ x\u2081 ] e) with find-holes e\n ... | (h , d) = h , HLam2 d\n find-holes \u2987\u2988[ x ] = (\u25a0 (x , <>)) , HEHole\n find-holes \u2987 e \u2988[ x ] with find-holes e\n ... | (h , d) = h ,, (x , <>) , HNEHole d\n find-holes (e1 \u2218 e2) with find-holes e1 | find-holes e2\n ... | (h1 , d1) | (h2 , d2) = (h1 \u222a h2 ) , (HAp d1 d2)\n\n -- two contexts that may contain different mappings have the same domain\n dom-eq : {A B : Set} \u2192 A ctx \u2192 B ctx \u2192 Set\n dom-eq {A} {B} C1 C2 = ((n : Nat) \u2192 \u03a3[ x \u2208 A ]( C1 n == Some x) \u2192 (\u03a3[ y \u2208 B ](C2 n == Some y)))\u00d7\n ((n : Nat) \u2192 \u03a3[ y \u2208 B ]( C2 n == Some y) \u2192 (\u03a3[ x \u2208 A ](C1 n == Some x)))\n\n -- the empty context has the same domain as itself\n dom-\u2205 : {A B : Set} \u2192 dom-eq (\u03bb _ \u2192 None {A}) (\u03bb _ \u2192 None {B})\n dom-\u2205 {A} {B} = (\u03bb n x \u2192 abort (somenotnone (! (\u03c02 x)))) , (\u03bb n x \u2192 abort (somenotnone (! (\u03c02 x))))\n\n -- todo: this seems like i would have proven it already? otw move to lemmas\n singleton-eq : {A : Set} {a : A} \u2192 \u2200{x n y} \u2192 (\u25a0 (x , a)) n == Some y \u2192 x == n\n singleton-eq {A} {a} {x} {n} {y} eq with natEQ x n\n singleton-eq eq | Inl x\u2081 = x\u2081\n singleton-eq eq | Inr x\u2081 = abort (somenotnone (! eq))\n\n -- todo: this seems like i would have proven it already? otw move to lemmas\n singleton-lookup-refl : {A : Set} {n : Nat} {\u03b2 : A} \u2192 (\u25a0 (n , \u03b2)) n == Some \u03b2\n singleton-lookup-refl {n = n} with natEQ n n\n singleton-lookup-refl | Inl refl = \u03bb {\u03b2} \u2192 refl\n singleton-lookup-refl | Inr x = abort (x refl)\n\n -- the singleton contexts formed with any contents but the same index has\n -- the same domain\n dom-single : {A B : Set} (x : Nat) (a : A) (b : B) \u2192 dom-eq (\u25a0 (x , a)) (\u25a0 (x , b))\n dom-single {A} {B} x \u03b1 \u03b2 = (\u03bb n x\u2081 \u2192 \u03b2 , (ap1 (\u03bb qq \u2192 (\u25a0 (qq , \u03b2)) n) (singleton-eq (\u03c02 x\u2081)) \u00b7 singleton-lookup-refl)) ,\n (\u03bb n x\u2081 \u2192 \u03b1 , (ap1 (\u03bb qq \u2192 (\u25a0 (qq , \u03b1)) n) (singleton-eq (\u03c02 x\u2081)) \u00b7 singleton-lookup-refl))\n\n lem-dom-union-apt1 : {A : Set} {\u03941 \u03942 : A ctx} {x : Nat} {y : A} \u2192 x # \u03941 \u2192 ((\u03941 \u222a \u03942) x == Some y) \u2192 (\u03942 x == Some y)\n lem-dom-union-apt1 {A} {\u03941} {\u03942} {x} {y} apt xin with \u03941 x\n lem-dom-union-apt1 apt xin | Some x\u2081 = abort (somenotnone apt)\n lem-dom-union-apt1 apt xin | None = xin\n\n lem-dom-union-apt2 : {A : Set} {\u03941 \u03942 : A ctx} {x : Nat} {y : A} \u2192 x # \u03942 \u2192 ((\u03941 \u222a \u03942) x == Some y) \u2192 (\u03941 x == Some y)\n lem-dom-union-apt2 {A} {\u03941} {\u03942} {x} {y} apt xin with \u03941 x\n lem-dom-union-apt2 apt xin | Some x\u2081 = xin\n lem-dom-union-apt2 apt xin | None = abort (somenotnone (! xin \u00b7 apt))\n\n dom-union : {A B : Set} {\u03941 \u03942 : A ctx} {H1 H2 : B ctx} \u2192\n H1 ## H2 \u2192\n dom-eq \u03941 H1 \u2192\n dom-eq \u03942 H2 \u2192\n dom-eq (\u03941 \u222a \u03942) (H1 \u222a H2)\n dom-union {A} {B} {\u03941} {\u03942} {H1} {H2} disj (p1 , p2) (p3 , p4) = guts1 , guts2\n where\n guts1 : (n : Nat) \u2192\n \u03a3[ x \u2208 A ] ((\u03941 \u222a \u03942) n == Some x) \u2192\n \u03a3[ y \u2208 B ] ((H1 \u222a H2) n == Some y)\n guts1 n (x , eq) with ctxindirect \u03941 n\n guts1 n (x\u2081 , eq) | Inl x with p1 n x\n ... | q1 , q2 = q1 , x\u2208\u222al H1 H2 n q1 q2\n guts1 n (x\u2081 , eq) | Inr x with p3 n (_ , lem-dom-union-apt1 {\u03941 = \u03941} {\u03942 = \u03942} x eq)\n ... | q1 , q2 = q1 , x\u2208\u222ar H1 H2 n q1 q2 (##-comm disj)\n\n guts2 : (n : Nat) \u2192\n \u03a3[ y \u2208 B ] ((H1 \u222a H2) n == Some y) \u2192\n \u03a3[ x \u2208 A ] ((\u03941 \u222a \u03942) n == Some x)\n guts2 n (x , eq) with ctxindirect H1 n\n guts2 n (x\u2081 , eq) | Inl x with p2 n x\n ... | q1 , q2 = q1 , x\u2208\u222al \u03941 \u03942 n q1 q2\n guts2 n (x\u2081 , eq) | Inr x with p4 n (_ , lem-dom-union-apt2 {\u03941 = H2} {\u03942 = H1} x (tr (\u03bb qq \u2192 qq n == Some x\u2081) (\u222acomm H1 H2 disj) eq))\n ... | q1 , q2 = q1 , x\u2208\u222ar \u03941 \u03942 n q1 q2 (##-comm {!!})\n\n\n\n -- the holes of an expression have the same domain as \u0394; that is, we\n -- don't add any extra junk as we expand\n mutual\n holes-delta-ana : \u2200{\u0393 H e \u03c4 d \u03c4' \u0394} \u2192\n holes e H \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n dom-eq \u0394 H\n holes-delta-ana (HLam1 h) (EALam x\u2081 x\u2082 exp) = holes-delta-ana h exp\n holes-delta-ana h (EASubsume x x\u2081 x\u2082 x\u2083) = holes-delta-synth h x\u2082\n holes-delta-ana (HEHole {u = u}) EAEHole = dom-single u _ _\n holes-delta-ana (HNEHole {u = u} h) (EANEHole x x\u2081) = dom-union {!!} (holes-delta-synth h x\u2081) (dom-single u _ _ )\n\n holes-delta-synth : \u2200{\u0393 H e \u03c4 d \u0394} \u2192\n holes e H \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n dom-eq \u0394 H\n holes-delta-synth HConst ESConst = dom-\u2205\n holes-delta-synth (HAsc h) (ESAsc x) = holes-delta-ana h x\n holes-delta-synth HVar (ESVar x\u2081) = dom-\u2205\n holes-delta-synth (HLam2 h) (ESLam x\u2081 exp) = holes-delta-synth h exp\n holes-delta-synth (HEHole {u = u}) ESEHole = dom-single u _ _\n holes-delta-synth (HNEHole {u = u} h) (ESNEHole x exp) = dom-union {!!} (holes-delta-synth h exp) (dom-single u _ _)\n holes-delta-synth (HAp h h\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) = dom-union {!!} (holes-delta-ana h x\u2084) (holes-delta-ana h\u2081 x\u2085)\n\n -- if a hole name is new then it's apart from the holes\n lem-apart-new : \u2200{e H u} \u2192 holes e H \u2192 hole-name-new e u \u2192 u # H\n lem-apart-new HConst HNConst = refl\n lem-apart-new (HAsc h) (HNAsc hn) = lem-apart-new h hn\n lem-apart-new HVar HNVar = refl\n lem-apart-new (HLam1 h) (HNLam1 hn) = lem-apart-new h hn\n lem-apart-new (HLam2 h) (HNLam2 hn) = lem-apart-new h hn\n lem-apart-new HEHole (HNHole x) = apart-singleton (flip x)\n lem-apart-new (HNEHole {u = u'} {H = H} h) (HNNEHole {u = u} x hn) = apart-parts H (\u25a0 (u' , <>)) u (lem-apart-new h hn) (apart-singleton (flip x))\n lem-apart-new (HAp {H1 = H1} {H2 = H2} h h\u2081) (HNAp hn hn\u2081) = apart-parts H1 H2 _ (lem-apart-new h hn) (lem-apart-new h\u2081 hn\u2081)\n\n -- todo: lemmas file?\n lem-dom-apt : {A : Set} {G : A ctx} {x y : Nat} \u2192 x # G \u2192 dom G y \u2192 x \u2260 y\n lem-dom-apt {x = x} {y = y} apt dom with natEQ x y\n lem-dom-apt apt dom | Inl refl = abort (somenotnone (! (\u03c02 dom) \u00b7 apt))\n lem-dom-apt apt dom | Inr x\u2081 = x\u2081\n\n -- if the holes of two expressions are disjoint, so are their collections\n -- of hole names\n holes-disjoint-disjoint : \u2200{ e1 e2 H1 H2} \u2192\n holes e1 H1 \u2192\n holes e2 H2 \u2192\n holes-disjoint e1 e2 \u2192\n H1 ## H2\n holes-disjoint-disjoint HConst he2 HDConst = empty-disj _\n holes-disjoint-disjoint (HAsc he1) he2 (HDAsc hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint HVar he2 HDVar = empty-disj _\n holes-disjoint-disjoint (HLam1 he1) he2 (HDLam1 hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint (HLam2 he1) he2 (HDLam2 hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint HEHole he2 (HDHole x) = lem-apart-sing-disj (lem-apart-new he2 x)\n holes-disjoint-disjoint (HNEHole he1) he2 (HDNEHole x hd) = disjoint-parts (holes-disjoint-disjoint he1 he2 hd) (lem-apart-sing-disj (lem-apart-new he2 x))\n holes-disjoint-disjoint (HAp he1 he2) he3 (HDAp hd hd\u2081) = disjoint-parts (holes-disjoint-disjoint he1 he3 hd) (holes-disjoint-disjoint he2 he3 hd\u2081)\n\n -- if two contexsts are disjoint and each share a domain with another\n -- context, those other two contexts are also disjoint\n domeq-disj : {A B : Set} {H1 H2 : A ctx} {\u03941 \u03942 : B ctx} \u2192\n H1 ## H2 \u2192\n dom-eq \u03941 H1 \u2192\n dom-eq \u03942 H2 \u2192\n \u03941 ## \u03942\n domeq-disj (\u03c01 , \u03c02) (\u03c03 , \u03c04) (\u03c05 , \u03c06) =\n (\u03bb n x \u2192 {!(\u03c03 n x)!}) ,\n (\u03bb n x \u2192 {!!})\n\n -- if you expand two hole-disjoint expressions analytically, the \u0394s\n -- produces are disjoint\n expand-ana-disjoint : \u2200{ e1 e2 \u03c41 \u03c42 e1' e2' \u03c41' \u03c42' \u0393 \u03941 \u03942 } \u2192\n holes-disjoint e1 e2 \u2192\n \u0393 \u22a2 e1 \u21d0 \u03c41 ~> e1' :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> e2' :: \u03c42' \u22a3 \u03942 \u2192\n \u03941 ## \u03942\n expand-ana-disjoint {e1} {e2} hd ana1 ana2\n with find-holes e1 | find-holes e2\n ... | (_ , he1) | (_ , he2) = domeq-disj (holes-disjoint-disjoint he1 he2 hd)\n (holes-delta-ana he1 ana1)\n (holes-delta-ana he2 ana2)\n\n -- these lemmas are all structurally recursive and quite\n -- mechanical. morally, they establish the properties about reduction\n -- that would be obvious \/ baked into Agda if holes-disjoint was defined\n -- as a function rather than a judgement (datatype), or if we had defined\n -- all the O(n^2) cases rather than relying on a little indirection to\n -- only have O(n) cases. that work has to go somewhwere, and we prefer\n -- that it goes here.\n ds-lem-asc : \u2200{e1 e2 \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (e1 \u00b7: \u03c4)\n ds-lem-asc HDConst = HDConst\n ds-lem-asc (HDAsc hd) = HDAsc (ds-lem-asc hd)\n ds-lem-asc HDVar = HDVar\n ds-lem-asc (HDLam1 hd) = HDLam1 (ds-lem-asc hd)\n ds-lem-asc (HDLam2 hd) = HDLam2 (ds-lem-asc hd)\n ds-lem-asc (HDHole x) = HDHole (HNAsc x)\n ds-lem-asc (HDNEHole x hd) = HDNEHole (HNAsc x) (ds-lem-asc hd)\n ds-lem-asc (HDAp hd hd\u2081) = HDAp (ds-lem-asc hd) (ds-lem-asc hd\u2081)\n\n ds-lem-lam1 : \u2200{e1 e2 x} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x e1)\n ds-lem-lam1 HDConst = HDConst\n ds-lem-lam1 (HDAsc hd) = HDAsc (ds-lem-lam1 hd)\n ds-lem-lam1 HDVar = HDVar\n ds-lem-lam1 (HDLam1 hd) = HDLam1 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDLam2 hd) = HDLam2 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDHole x\u2081) = HDHole (HNLam1 x\u2081)\n ds-lem-lam1 (HDNEHole x\u2081 hd) = HDNEHole (HNLam1 x\u2081) (ds-lem-lam1 hd)\n ds-lem-lam1 (HDAp hd hd\u2081) = HDAp (ds-lem-lam1 hd) (ds-lem-lam1 hd\u2081)\n\n ds-lem-lam2 : \u2200{e1 e2 x \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x [ \u03c4 ] e1)\n ds-lem-lam2 HDConst = HDConst\n ds-lem-lam2 (HDAsc hd) = HDAsc (ds-lem-lam2 hd)\n ds-lem-lam2 HDVar = HDVar\n ds-lem-lam2 (HDLam1 hd) = HDLam1 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDLam2 hd) = HDLam2 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDHole x\u2081) = HDHole (HNLam2 x\u2081)\n ds-lem-lam2 (HDNEHole x\u2081 hd) = HDNEHole (HNLam2 x\u2081) (ds-lem-lam2 hd)\n ds-lem-lam2 (HDAp hd hd\u2081) = HDAp (ds-lem-lam2 hd) (ds-lem-lam2 hd\u2081)\n\n ds-lem-nehole : \u2200{e e1 u} \u2192 holes-disjoint e e1 \u2192 hole-name-new e u \u2192 holes-disjoint e \u2987 e1 \u2988[ u ]\n ds-lem-nehole HDConst \u03bd = HDConst\n ds-lem-nehole (HDAsc hd) (HNAsc \u03bd) = HDAsc (ds-lem-nehole hd \u03bd)\n ds-lem-nehole HDVar \u03bd = HDVar\n ds-lem-nehole (HDLam1 hd) (HNLam1 \u03bd) = HDLam1 (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDLam2 hd) (HNLam2 \u03bd) = HDLam2 (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDHole x) (HNHole x\u2081) = HDHole (HNNEHole (flip x\u2081) x)\n ds-lem-nehole (HDNEHole x hd) (HNNEHole x\u2081 \u03bd) = HDNEHole (HNNEHole (flip x\u2081) x) (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDAp hd hd\u2081) (HNAp \u03bd \u03bd\u2081) = HDAp (ds-lem-nehole hd \u03bd) (ds-lem-nehole hd\u2081 \u03bd\u2081)\n\n ds-lem-ap : \u2200{e1 e2 e3} \u2192 holes-disjoint e3 e1 \u2192 holes-disjoint e3 e2 \u2192 holes-disjoint e3 (e1 \u2218 e2)\n ds-lem-ap HDConst hd2 = HDConst\n ds-lem-ap (HDAsc hd1) (HDAsc hd2) = HDAsc (ds-lem-ap hd1 hd2)\n ds-lem-ap HDVar hd2 = HDVar\n ds-lem-ap (HDLam1 hd1) (HDLam1 hd2) = HDLam1 (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDLam2 hd1) (HDLam2 hd2) = HDLam2 (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDHole x) (HDHole x\u2081) = HDHole (HNAp x x\u2081)\n ds-lem-ap (HDNEHole x hd1) (HDNEHole x\u2081 hd2) = HDNEHole (HNAp x x\u2081) (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDAp hd1 hd2) (HDAp hd3 hd4) = HDAp (ds-lem-ap hd1 hd3) (ds-lem-ap hd2 hd4)\n\n -- holes-disjoint is symmetric\n disjoint-sym : (e1 e2 : hexp) \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint e2 e1\n disjoint-sym .c c HDConst = HDConst\n disjoint-sym .c (e2 \u00b7: x) HDConst = HDAsc (disjoint-sym _ _ HDConst)\n disjoint-sym .c (X x) HDConst = HDVar\n disjoint-sym .c (\u00b7\u03bb x e2) HDConst = HDLam1 (disjoint-sym c e2 HDConst)\n disjoint-sym .c (\u00b7\u03bb x [ x\u2081 ] e2) HDConst = HDLam2 (disjoint-sym c e2 HDConst)\n disjoint-sym .c \u2987\u2988[ x ] HDConst = HDHole HNConst\n disjoint-sym .c \u2987 e2 \u2988[ x ] HDConst = HDNEHole HNConst (disjoint-sym c e2 HDConst)\n disjoint-sym .c (e2 \u2218 e3) HDConst = HDAp (disjoint-sym c e2 HDConst) (disjoint-sym c e3 HDConst)\n\n disjoint-sym _ c (HDAsc hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) | HDAsc ih = HDAsc (ds-lem-asc ih)\n disjoint-sym _ (X x) (HDAsc hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) | HDLam1 ih = HDLam1 (ds-lem-asc ih)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) | HDLam2 ih = HDLam2 (ds-lem-asc ih)\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) | HDHole x\u2081 = HDHole (HNAsc x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) | HDNEHole x\u2081 ih = HDNEHole (HNAsc x\u2081) (ds-lem-asc ih)\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) | HDAp ih ih\u2081 = HDAp (ds-lem-asc ih) (ds-lem-asc ih\u2081)\n\n disjoint-sym _ c HDVar = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) HDVar = HDAsc (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (X x\u2081) HDVar = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) HDVar = HDLam1 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) HDVar = HDLam2 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] HDVar = HDHole HNVar\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] HDVar = HDNEHole HNVar (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (e2 \u2218 e3) HDVar = HDAp (disjoint-sym _ e2 HDVar) (disjoint-sym _ e3 HDVar)\n\n disjoint-sym _ c (HDLam1 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) | HDAsc ih = HDAsc (ds-lem-lam1 ih)\n disjoint-sym _ (X x\u2081) (HDLam1 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) | HDLam1 ih = HDLam1 (ds-lem-lam1 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) | HDLam2 ih = HDLam2 (ds-lem-lam1 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) | HDHole x = HDHole (HNLam1 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) | HDNEHole x ih = HDNEHole (HNLam1 x) (ds-lem-lam1 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam1 ih) (ds-lem-lam1 ih\u2081)\n\n disjoint-sym _ c (HDLam2 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) | HDAsc ih = HDAsc (ds-lem-lam2 ih)\n disjoint-sym _ (X x\u2081) (HDLam2 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) | HDLam1 ih = HDLam1 (ds-lem-lam2 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) | HDLam2 ih = HDLam2 (ds-lem-lam2 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) | HDHole x = HDHole (HNLam2 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) | HDNEHole x ih = HDNEHole (HNLam2 x) (ds-lem-lam2 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam2 ih) (ds-lem-lam2 ih\u2081)\n\n disjoint-sym _ c (HDHole x) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDHole (HNAsc x\u2081)) = HDAsc (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (X x) (HDHole x\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDHole (HNLam1 x\u2081)) = HDLam1 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDHole (HNLam2 x\u2082)) = HDLam2 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2082))\n disjoint-sym _ \u2987\u2988[ x ] (HDHole (HNHole x\u2081)) = HDHole (HNHole (flip x\u2081))\n disjoint-sym _ \u2987 e2 \u2988[ u' ] (HDHole (HNNEHole x x\u2081)) = HDNEHole (HNHole (flip x)) (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (e2 \u2218 e3) (HDHole (HNAp x x\u2081)) = HDAp (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x))\n (disjoint-sym \u2987\u2988[ _ ] e3 (HDHole x\u2081))\n\n disjoint-sym _ c (HDNEHole x hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e \u00b7: x) (HDNEHole (HNAsc x\u2081) hd) | HDAsc ih = HDAsc (ds-lem-nehole ih x\u2081)\n disjoint-sym _ (X x) (HDNEHole x\u2081 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole (HNLam1 x\u2081) hd) | HDLam1 ih = HDLam1 (ds-lem-nehole ih x\u2081)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole x\u2082 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole (HNLam2 x\u2082) hd) | HDLam2 ih = HDLam2 (ds-lem-nehole ih x\u2082)\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole (HNHole x\u2082) hd) | HDHole x\u2081 = HDHole (HNNEHole (flip x\u2082) x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole (HNNEHole x\u2082 x\u2083) hd) | HDNEHole x\u2081 ih = HDNEHole (HNNEHole (flip x\u2082) x\u2081) (ds-lem-nehole ih x\u2083)\n disjoint-sym _ (e2 \u2218 e3) (HDNEHole x hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e1 \u2218 e3) (HDNEHole (HNAp x x\u2081) hd) | HDAp ih ih\u2081 = HDAp (ds-lem-nehole ih x) (ds-lem-nehole ih\u2081 x\u2081)\n\n disjoint-sym _ c (HDAp hd hd\u2081) = HDConst\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) | HDAsc ih | HDAsc ih1 = HDAsc (ds-lem-ap ih ih1)\n disjoint-sym _ (X x) (HDAp hd hd\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) | HDLam1 ih | HDLam1 ih1 = HDLam1 (ds-lem-ap ih ih1)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) | HDLam2 ih | HDLam2 ih1 = HDLam2 (ds-lem-ap ih ih1)\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) | HDHole x\u2081 | HDHole x\u2082 = HDHole (HNAp x\u2081 x\u2082)\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) | HDNEHole x\u2081 ih | HDNEHole x\u2082 ih1 = HDNEHole (HNAp x\u2081 x\u2082) (ds-lem-ap ih ih1)\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) | HDAp ih ih\u2081 | HDAp ih1 ih2 = HDAp (ds-lem-ap ih ih1) (ds-lem-ap ih\u2081 ih2)\n\n\n -- note that this is false, so holes-disjoint isn't transitive\n -- disjoint-new : \u2200{e1 e2 u} \u2192 holes-disjoint e1 e2 \u2192 hole-name-new e1 u \u2192 hole-name-new e2 u\n\n -- it's also not reflexive, because \u2987\u2988[ u ] isn't hole-disjoint with\n -- itself since refl : u == u; it's also not anti-reflexive, because the\n -- expression c *is* hole-disjoint with itself (albeit vacuously)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"36c32cf0ea2bf68af624df007475d90a3f2fba5f","subject":"Remove an extra \u2192\u1d47 def","message":"Remove an extra \u2192\u1d47 def\n","repos":"crypto-agda\/crypto-agda","old_file":"circuit.agda","new_file":"circuit.agda","new_contents":"module circuit where\n\nopen import Function\nopen import Data.Nat.NP hiding (_\u225f_; compare)\nopen import Data.Bits\nopen import Data.Bits.Bits2\nopen import Data.Bool hiding (_\u225f_)\nopen import Data.Product hiding (swap; map)\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc; inject+; raise; #_)\nopen import Data.List using (List; []; _\u2237_)\nopen import Data.Vec.NP as Vec using (Vec; []; _\u2237_; foldr; _[_]\u2254_; lookup; _++_; splitAt; tabulate; allFin; concat; ++-decomp) renaming (map to vmap)\nopen import Data.Vec.Properties\nopen import Relation.Nullary.Decidable hiding (map)\nopen import Relation.Binary.PropositionalEquality\nopen import composable\nopen import vcomp\nopen import forkable\n\nCircuitType : Set\u2081\nCircuitType = (i o : \u2115) \u2192 Set\n\nRunCircuit : CircuitType \u2192 Set\nRunCircuit C = \u2200 {i o} \u2192 C i o \u2192 Bits i \u2192 Bits o\n\nRewireFun : CircuitType\nRewireFun i o = Fin o \u2192 Fin i\n\nRewireTbl : CircuitType\nRewireTbl i o = Vec (Fin i) o\n\nmodule Rewire where\n rewire : \u2200 {a i o} {A : Set a} \u2192 (Fin o \u2192 Fin i) \u2192 Vec A i \u2192 Vec A o\n rewire f v = tabulate (flip lookup v \u2218 f)\n\n rewireTbl : \u2200 {a i o} {A : Set a} \u2192 RewireTbl i o \u2192 Vec A i \u2192 Vec A o\n rewireTbl tbl v = vmap (flip lookup v) tbl\n\n runRewireFun : RunCircuit RewireFun\n runRewireFun = rewire\n\n runRewireTbl : RunCircuit RewireTbl\n runRewireTbl = rewireTbl\n\nrecord RewiringBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n\n field\n isComposable : Composable C\n isVComposable : VComposable _+_ C\n open Composable isComposable\n open VComposable isVComposable\n\n field\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n\n rewireWithTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 C i o\n rewireWithTbl = rewire \u2218 flip lookup\n\n idCDefault : \u2200 {i} \u2192 C i i\n idCDefault = rewire id\n\n field\n idC : \u2200 {i} \u2192 C i i\n\n field\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) is \u2192 is =[ rewire r ]= Rewire.rewire r is\n\n{-\n _>>>-spec_ : \u2200 {i m o} {c\u2080 : C i m} {c\u2081 : C m o} {is ms os} \u2192\n is =[ c\u2080 ]= ms \u2192 ms =[ c\u2081 ]= os \u2192 is =[ c\u2080 >>> c\u2081 ]= os\n\n _***-spec_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} {c\u2080 : C i\u2080 o\u2080} {c\u2081 : C i\u2081 o\u2081} {is\u2080 is\u2081 os\u2080 os\u2081} \u2192\n is\u2080 =[ c\u2080 ]= os\u2080 \u2192 is\u2081 =[ c\u2081 ]= os\u2081 \u2192 (is\u2080 ++ is\u2081) =[ c\u2080 *** c\u2081 ]= (os\u2080 ++ os\u2081)\n\n-}\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idC ]= bs\n{-\n rewireWithTbl-spec : \u2200 {i o} (t : RewireTbl i o) is\n \u2192 is =[ rewireWithTbl t ]= Rewire.rewireTbl t is\n rewireWithTbl-spec t is = {!rewire-spec ? ?!}\n-}\n\n idCDefault-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idCDefault ]= bs\n idCDefault-spec bs\n = subst (\u03bb bs' \u2192 bs =[ idCDefault ]= bs') (tabulate\u2218lookup bs) (rewire-spec id bs)\n\n sink : \u2200 i \u2192 C i 0\n sink _ = rewire (\u03bb())\n-- sink-spec : bs =[ sink i ]= []\n\n dup\u2081 : \u2200 o \u2192 C 1 o\n dup\u2081 _ = rewire (const zero)\n-- dup\u2081-spec : (b \u2237 []) =[ dup o ]= replicate o\n\n dup\u2081\u00b2 : C 1 2\n dup\u2081\u00b2 = dup\u2081 2\n-- dup\u2081\u00b2-spec : (b \u2237 []) =[ dup\u2081\u00b2 ]= (b \u2237 b \u2237 [])\n\n vcat : \u2200 {i o n} \u2192 Vec (C i o) n \u2192 C (n * i) (n * o)\n vcat [] = idC\n vcat (x \u2237 xs) = x *** vcat xs\n\n coerce : \u2200 {i o} \u2192 i \u2261 o \u2192 C i o\n coerce refl = idC\n\n{-\n coerce-spec : \u2200 {i o} {i\u2261o : i \u2261 o} {is} \u2192\n is =[ coerce i\u2261o ]= subst Bits i\u2261o is\n coerce-spec = {!!}\n-}\n\n {-\n Sends inputs 0,1,2,...,n to outputs 0,1,2,...,n,0,1,2,...,n,0,1,2,...,n,...\n -}\n dup\u207f : \u2200 {i} k \u2192 C i (k * i)\n dup\u207f {i} k = rewireWithTbl (concat (replicate {n = k} (allFin i)))\n\n{-\n dup\u207f-spec : \u2200 {i} {is : Bits i} k \u2192 is =[ dup\u207f k ]= concat (replicate {n = k} is)\n dup\u207f-spec {i} {is} k = {!rewireWithTbl-spec (concat (replicate {n = k} (allFin _))) ?!}\n-}\n\n {-\n Sends inputs 0,1,2,...,n to outputs 0,0,0,...,1,1,1,...,2,2,2,...,n,n,n,...\n -}\n dup\u207f\u2032 : \u2200 {i} k \u2192 C i (i * k)\n dup\u207f\u2032 {i} k = rewireWithTbl (concat (vmap replicate (allFin i)))\n{-\n dup\u207f\u2032 {i} k = coerce (proj\u2082 \u2115\u00b0.*-identity i) >>>\n (vcat {n = i} (replicate (dup\u2081 _)))\n-}\n\n dup\u00b2 : \u2200 {i} \u2192 C i (i + i)\n dup\u00b2 {i} = dup\u207f 2 >>> coerce (cong (_+_ i) (sym (\u2115\u00b0.+-comm 0 i)))\n\n{-\n dup\u00b2-spec : \u2200 {n} {is : Bits n} \u2192 is =[ dup\u00b2 ]= (is ++ is)\n dup\u00b2-spec = coerce-spec (rewireWithTbl-spec {!!} {!!})\n-}\n\n _&&&_ : \u2200 {i o\u2080 o\u2081} \u2192 C i o\u2080 \u2192 C i o\u2081 \u2192 C i (o\u2080 + o\u2081)\n c\u2080 &&& c\u2081 = dup\u00b2 >>> c\u2080 *** c\u2081\n\n{-\n _&&&-spec_ : \u2200 {i o\u2080 o\u2081} {c\u2080 : C i o\u2080} {c\u2081 : C i o\u2081} {is os\u2080 os\u2081}\n \u2192 is =[ c\u2080 ]= os\u2080 \u2192 is =[ c\u2081 ]= os\u2081 \u2192 is =[ c\u2080 &&& c\u2081 ]= (os\u2080 ++ os\u2081)\n pf\u2080 &&&-spec pf\u2081 = dup\u00b2-spec >>>-spec (pf\u2080 ***-spec pf\u2081)\n-}\n\n ext-before : \u2200 {k i o} \u2192 C i o \u2192 C (k + i) (k + o)\n ext-before {k} c = idC {k} *** c\n\n ext-after : \u2200 {k i o} \u2192 C i o \u2192 C (i + k) (o + k)\n ext-after c = c *** idC\n\n commC : \u2200 m n \u2192 C (m + n) (n + m)\n commC m n = rewireWithTbl (vmap (raise m) (allFin n) ++ vmap (inject+ n) (allFin m))\n\n dropC : \u2200 {i} k \u2192 C (k + i) i\n dropC k = sink k *** idC\n\n takeC : \u2200 {i} k \u2192 C (k + i) k\n takeC {i} k = commC k _ >>> dropC i\n\n swap : \u2200 {i} (x y : Fin i) \u2192 C i i\n -- swap x y = arr (\u03bb xs \u2192 (xs [ x ]\u2254 (lookup y xs)) [ y ]\u2254 (lookup x xs))\n swap x y = rewire (Fin.swap x y)\n\n rev : \u2200 {i} \u2192 C i i\n rev = rewire Fin.reverse\n\n swap\u2082 : C 2 2\n swap\u2082 = rev\n -- swap\u2082 = swap (# 0) (# 1)\n\n Perm : \u2115 \u2192 Set\n Perm n = List (Fin n \u00d7 Fin n)\n\n perm : \u2200 {i} \u2192 Perm i \u2192 C i i\n perm [] = idC\n perm ((x , y) \u2237 \u03c0) = swap x y >>> perm \u03c0\n\n headC : \u2200 {i} \u2192 C (1 + i) 1\n headC = takeC 1\n\n tailC : \u2200 {i} \u2192 C (1 + i) i\n tailC = dropC 1\n\n open Composable isComposable public\n open VComposable isVComposable public\n\nrecord CircuitBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n field\n isRewiringBuilder : RewiringBuilder C\n arr : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n leafC : \u2200 {o} \u2192 Bits o \u2192 C 0 o\n isForkable : Forkable suc C\n\n open RewiringBuilder isRewiringBuilder\n open Forkable isForkable renaming (fork to forkC)\n\n{-\n field\n leafC-spec : \u2200 {o} (os : Bits o) \u2192 [] =[ leafC os ]= os\n forkC-left-spec : \u2200 {i o} {c\u2080 c\u2081 : C i o} {is os}\n \u2192 is =[ c\u2080 ]= os \u2192 (0\u2237 is) =[ forkC c\u2080 c\u2081 ]= os\n forkC-right-spec : \u2200 {i o} {c\u2080 c\u2081 : C i o} {is os}\n \u2192 is =[ c\u2081 ]= os \u2192 (1\u2237 is) =[ forkC c\u2080 c\u2081 ]= os\n-}\n bit : Bit \u2192 C 0 1\n bit b = leafC (b \u2237 [])\n\n{-\n bit-spec : \u2200 b \u2192 [] =[ bit b ]= (b \u2237 [])\n bit-spec b = leafC-spec (b \u2237 [])\n-}\n\n 0\u02b7 : C 0 1\n 0\u02b7 = bit 0b\n\n 0\u02b7\u207f : \u2200 {o} \u2192 C 0 o\n 0\u02b7\u207f = leafC 0\u207f\n\n{-\n 0\u02b7-spec : [] =[ 0\u02b7 ]= 0\u2237 []\n 0\u02b7-spec = bit-spec 0b\n-}\n\n 1\u02b7 : C 0 1\n 1\u02b7 = bit 1b\n\n 1\u02b7\u207f : \u2200 {o} \u2192 C 0 o\n 1\u02b7\u207f = leafC 1\u207f\n\n{-\n 1\u02b7-spec : [] =[ 1\u02b7 ]= 1\u2237 []\n 1\u02b7-spec = bit-spec 1b\n-}\n\n padL : \u2200 {i} k \u2192 C i (k + i)\n padL k = 0\u02b7\u207f {k} *** idC\n\n padR : \u2200 {i} k \u2192 C i (i + k)\n padR k = padL k >>> commC k _\n\n arr' : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n arr' {zero} f = leafC (f [])\n arr' {suc i} f = forkC (arr' {i} (f \u2218 0\u2237_)) (arr' (f \u2218 1\u2237_))\n\n unOp : (Bit \u2192 Bit) \u2192 C 1 1\n unOp op = arr (\u03bb { (x \u2237 []) \u2192 (op x) \u2237 [] })\n\n notC : C 1 1\n notC = unOp not\n\n binOp : (Bit \u2192 Bit \u2192 Bit) \u2192 C 2 1\n binOp op = arr (\u03bb { (x \u2237 y \u2237 []) \u2192 (op x y) \u2237 [] })\n\n terOp : (Bit \u2192 Bit \u2192 Bit \u2192 Bit) \u2192 C 3 1\n terOp op = arr (\u03bb { (x \u2237 y \u2237 z \u2237 []) \u2192 (op x y z) \u2237 [] })\n\n xorC : C 2 1\n xorC = binOp _xor_\n\n eqC : C 2 1\n eqC = binOp _==\u1d47_\n\n orC : C 2 1\n orC = binOp _\u2228_\n\n andC : C 2 1\n andC = binOp _\u2227_\n\n norC : C 2 1\n norC = binOp (\u03bb x y \u2192 not (x \u2228 y))\n\n nandC : C 2 1\n nandC = binOp (\u03bb x y \u2192 not (x \u2227 y))\n\n if\u27e8head=0\u27e9then_else_ : \u2200 {i o} (c\u2080 c\u2081 : C i o) \u2192 C (1 + i) o\n if\u27e8head=0\u27e9then_else_ = forkC\n\n -- Base addition with carry:\n -- * Any input can be used as the carry\n -- * First output is the carry\n --\n -- This can also be seen as the addition of\n -- three bits which result is between 0 and 3\n -- and thus fits in two bit word in binary\n -- representation.\n --\n -- Alternatively you can see it as counting the\n -- number of 1s in a three bits vector.\n add\u2082 : C 3 2\n add\u2082 = carry &&& result\n where carry : C 3 1\n carry = terOp (\u03bb x y z \u2192 x xor (y \u2227 z))\n result : C 3 1\n result = terOp (\u03bb x y z \u2192 x xor (y xor z))\n\n open RewiringBuilder isRewiringBuilder public\n\n_\u2192\u1da0_ : CircuitType\ni \u2192\u1da0 o = Fin o \u2192 Fin i\n\nfinFunRewiringBuilder : RewiringBuilder _\u2192\u1da0_\nfinFunRewiringBuilder = mk (opComp (ixFunComp Fin)) finFunOpVComp id id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1da0_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = Rewire.rewire f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec = tabulate\u2218lookup\n\ntblRewiringBuilder : RewiringBuilder RewireTbl\ntblRewiringBuilder = mk (mk _>>>_) (mk _***_) tabulate (allFin _) _=[_]=_ rewire-spec idC-spec\n where\n open Rewire\n C = RewireTbl\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tbl ]= output = Rewire.rewireTbl tbl input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ tabulate r ]= Rewire.rewire r bs\n rewire-spec r bs = sym (tabulate-\u2218 (flip lookup bs) r)\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ allFin _ ]= bs\n -- idC-spec = map-lookup-allFin\n idC-spec bs rewrite rewire-spec id bs = tabulate\u2218lookup bs\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n c\u2080 >>> c\u2081 = rewireTbl c\u2081 c\u2080\n -- c\u2080 >>> c\u2081 = tabulate (flip lookup c\u2080 \u2218 flip lookup c\u2081)\n -- c\u2080 >>> c\u2081 = vmap (flip lookup c\u2080) c\u2081\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n _***_ {i\u2080} c\u2080 c\u2081 = vmap (inject+ _) c\u2080 ++ vmap (raise i\u2080) c\u2081\n\nbitsFunRewiringBuilder : RewiringBuilder _\u2192\u1d47_\nbitsFunRewiringBuilder = mk bitsFunComp bitsFunVComp Rewire.rewire id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1d47_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ Rewire.rewire r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec bs = refl\n\nbitsFunCircuitBuilder : CircuitBuilder _\u2192\u1d47_\nbitsFunCircuitBuilder = mk bitsFunRewiringBuilder id (\u03bb { bs [] \u2192 bs }) bitsFunFork\n\nmodule BitsFunExtras where\n open CircuitBuilder bitsFunCircuitBuilder\n C = _\u2192\u1d47_\n >>>-spec : \u2200 {i m o} (c\u2080 : C i m) (c\u2081 : C m o) xs \u2192 (c\u2080 >>> c\u2081) xs \u2261 c\u2081 (c\u2080 xs)\n >>>-spec _ _ _ = refl\n ***-spec : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} (c\u2080 : C i\u2080 o\u2080) (c\u2081 : C i\u2081 o\u2081) xs {ys}\n \u2192 (c\u2080 *** c\u2081) (xs ++ ys) \u2261 c\u2080 xs ++ c\u2081 ys\n ***-spec {i\u2080} c\u2080 c\u2081 xs {ys} with splitAt i\u2080 (xs ++ ys)\n ... | pre , post , eq with ++-decomp {xs = xs} {pre} {ys} {post} eq\n ... | eq1 , eq2 rewrite eq1 | eq2 = refl\n\nopen import bintree hiding (_>>>_; _***_)\nopen import flipbased-tree -- hiding (_***_)\n\nTreeBits : CircuitType\nTreeBits i o = Tree (Bits o) i\n\ntreeBitsRewiringBuilder : RewiringBuilder TreeBits\ntreeBitsRewiringBuilder = mk bintree.composable bintree.vcomposable rewire (rewire id) _=[_]=_ rewire-spec idC-spec\n where\n C = TreeBits\n\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n rewire f = fromFun (Rewire.rewire f)\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tree ]= output = toFun tree input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ rewire r ]= Rewire.rewire r bs\n rewire-spec r = toFun\u2218fromFun (tabulate \u2218 flip (Vec.lookup \u2218 r))\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ rewire id ]= bs\n idC-spec bs rewrite toFun\u2218fromFun (tabulate \u2218 flip Vec.lookup) bs | tabulate\u2218lookup bs = refl\n\ntreeBitsCircuitBuilder : CircuitBuilder TreeBits\ntreeBitsCircuitBuilder = mk treeBitsRewiringBuilder fromFun leaf bintree.forkable\n\nRewiringTree : CircuitType\nRewiringTree i o = Tree (Fin i) o\n\nmodule RewiringWith2^Outputs where\n C_\u27e82^_\u27e9 = RewiringTree\n\n rewire : \u2200 {i o} \u2192 RewireFun i (2 ^ o) \u2192 C i \u27e82^ o \u27e9\n rewire f = fromFun (f \u2218 toFin)\n\n lookupFin : \u2200 {i o} \u2192 C i \u27e82^ o \u27e9 \u2192 Fin (2 ^ o) \u2192 Fin i\n lookupFin c x = bintree.lookup (fromFin x) c\n\n _>>>_ : \u2200 {i m o} \u2192 C i \u27e82^ m \u27e9 \u2192 C (2 ^ m) \u27e82^ o \u27e9 \u2192 C i \u27e82^ o \u27e9\n f >>> g = rewire (lookupFin f \u2218 lookupFin g)\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 \u27e82^ o\u2080 \u27e9 \u2192 C i\u2081 \u27e82^ o\u2081 \u27e9 \u2192 C (i\u2080 + i\u2081) \u27e82^ (o\u2080 + o\u2081) \u27e9\n f *** g = f >>= \u03bb x \u2192 map (Fin._+\u2032_ x) g\n\nmodule Test where\n open import Data.Bits.Bits4\n\n tbl : RewireTbl 4 4\n tbl = # 1 \u2237 # 0 \u2237 # 2 \u2237 # 2 \u2237 []\n\n fun : RewireFun 4 4\n fun zero = # 1\n fun (suc zero) = # 0\n fun (suc (suc zero)) = # 2\n fun (suc (suc (suc x))) = # 2\n\n-- swap x y \u2248 swap y x\n-- reverse \u2218 reverse \u2248 id\n\n abs : \u2200 {C} \u2192 RewiringBuilder C \u2192 C 4 4\n abs builder = swap\u2082 *** dup\u2081\u00b2 *** sink 1\n where open RewiringBuilder builder\n\n tinytree : Tree (Fin 4) 2\n tinytree = fork (fork (leaf (# 1)) (leaf (# 0))) (fork (leaf (# 2)) (leaf (# 2)))\n\n bigtree : Tree (Bits 4) 4\n bigtree = fork (fork (fork (same 0000b) (same 0011b)) (fork (same 1000b) (same 1011b)))\n (fork (fork (same 0100b) (same 0111b)) (fork (same 1100b) (same 1111b)))\n where same : \u2200 {n} {A : Set} \u2192 A \u2192 Tree A (suc n)\n same x = fork (leaf x) (leaf x)\n\n test\u2081 : tbl \u2261 tabulate fun\n test\u2081 = refl\n\n test\u2082 : tbl \u2261 abs tblRewiringBuilder\n test\u2082 = refl\n\n test\u2083 : tabulate fun \u2261 tabulate (abs finFunRewiringBuilder)\n test\u2083 = refl\n\n test\u2084 : bigtree \u2261 abs treeBitsRewiringBuilder\n test\u2084 = refl\n\n -- open RewiringWith2^Outputs\n -- test\u2085 : tabulate (lookupFin tinytree) \u2261 tbl\n -- test\u2085 = refl\n","old_contents":"module circuit where\n\nopen import Function\nopen import Data.Nat.NP hiding (_\u225f_; compare)\nopen import Data.Bits\nopen import Data.Bits.Bits2\nopen import Data.Bool hiding (_\u225f_)\nopen import Data.Product hiding (swap; map)\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc; inject+; raise; #_)\nopen import Data.List using (List; []; _\u2237_)\nopen import Data.Vec.NP as Vec using (Vec; []; _\u2237_; foldr; _[_]\u2254_; lookup; _++_; splitAt; tabulate; allFin; concat; ++-decomp) renaming (map to vmap)\nopen import Data.Vec.Properties\nopen import Relation.Nullary.Decidable hiding (map)\nopen import Relation.Binary.PropositionalEquality\nopen import composable\nopen import vcomp\nopen import forkable\n\nCircuitType : Set\u2081\nCircuitType = (i o : \u2115) \u2192 Set\n\nRunCircuit : CircuitType \u2192 Set\nRunCircuit C = \u2200 {i o} \u2192 C i o \u2192 Bits i \u2192 Bits o\n\nRewireFun : CircuitType\nRewireFun i o = Fin o \u2192 Fin i\n\nRewireTbl : CircuitType\nRewireTbl i o = Vec (Fin i) o\n\nmodule Rewire where\n rewire : \u2200 {a i o} {A : Set a} \u2192 (Fin o \u2192 Fin i) \u2192 Vec A i \u2192 Vec A o\n rewire f v = tabulate (flip lookup v \u2218 f)\n\n rewireTbl : \u2200 {a i o} {A : Set a} \u2192 RewireTbl i o \u2192 Vec A i \u2192 Vec A o\n rewireTbl tbl v = vmap (flip lookup v) tbl\n\n runRewireFun : RunCircuit RewireFun\n runRewireFun = rewire\n\n runRewireTbl : RunCircuit RewireTbl\n runRewireTbl = rewireTbl\n\nrecord RewiringBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n\n field\n isComposable : Composable C\n isVComposable : VComposable _+_ C\n open Composable isComposable\n open VComposable isVComposable\n\n field\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n\n rewireWithTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 C i o\n rewireWithTbl = rewire \u2218 flip lookup\n\n idCDefault : \u2200 {i} \u2192 C i i\n idCDefault = rewire id\n\n field\n idC : \u2200 {i} \u2192 C i i\n\n field\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) is \u2192 is =[ rewire r ]= Rewire.rewire r is\n\n{-\n _>>>-spec_ : \u2200 {i m o} {c\u2080 : C i m} {c\u2081 : C m o} {is ms os} \u2192\n is =[ c\u2080 ]= ms \u2192 ms =[ c\u2081 ]= os \u2192 is =[ c\u2080 >>> c\u2081 ]= os\n\n _***-spec_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} {c\u2080 : C i\u2080 o\u2080} {c\u2081 : C i\u2081 o\u2081} {is\u2080 is\u2081 os\u2080 os\u2081} \u2192\n is\u2080 =[ c\u2080 ]= os\u2080 \u2192 is\u2081 =[ c\u2081 ]= os\u2081 \u2192 (is\u2080 ++ is\u2081) =[ c\u2080 *** c\u2081 ]= (os\u2080 ++ os\u2081)\n\n-}\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idC ]= bs\n{-\n rewireWithTbl-spec : \u2200 {i o} (t : RewireTbl i o) is\n \u2192 is =[ rewireWithTbl t ]= Rewire.rewireTbl t is\n rewireWithTbl-spec t is = {!rewire-spec ? ?!}\n-}\n\n idCDefault-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idCDefault ]= bs\n idCDefault-spec bs\n = subst (\u03bb bs' \u2192 bs =[ idCDefault ]= bs') (tabulate\u2218lookup bs) (rewire-spec id bs)\n\n sink : \u2200 i \u2192 C i 0\n sink _ = rewire (\u03bb())\n-- sink-spec : bs =[ sink i ]= []\n\n dup\u2081 : \u2200 o \u2192 C 1 o\n dup\u2081 _ = rewire (const zero)\n-- dup\u2081-spec : (b \u2237 []) =[ dup o ]= replicate o\n\n dup\u2081\u00b2 : C 1 2\n dup\u2081\u00b2 = dup\u2081 2\n-- dup\u2081\u00b2-spec : (b \u2237 []) =[ dup\u2081\u00b2 ]= (b \u2237 b \u2237 [])\n\n vcat : \u2200 {i o n} \u2192 Vec (C i o) n \u2192 C (n * i) (n * o)\n vcat [] = idC\n vcat (x \u2237 xs) = x *** vcat xs\n\n coerce : \u2200 {i o} \u2192 i \u2261 o \u2192 C i o\n coerce refl = idC\n\n{-\n coerce-spec : \u2200 {i o} {i\u2261o : i \u2261 o} {is} \u2192\n is =[ coerce i\u2261o ]= subst Bits i\u2261o is\n coerce-spec = {!!}\n-}\n\n {-\n Sends inputs 0,1,2,...,n to outputs 0,1,2,...,n,0,1,2,...,n,0,1,2,...,n,...\n -}\n dup\u207f : \u2200 {i} k \u2192 C i (k * i)\n dup\u207f {i} k = rewireWithTbl (concat (replicate {n = k} (allFin i)))\n\n{-\n dup\u207f-spec : \u2200 {i} {is : Bits i} k \u2192 is =[ dup\u207f k ]= concat (replicate {n = k} is)\n dup\u207f-spec {i} {is} k = {!rewireWithTbl-spec (concat (replicate {n = k} (allFin _))) ?!}\n-}\n\n {-\n Sends inputs 0,1,2,...,n to outputs 0,0,0,...,1,1,1,...,2,2,2,...,n,n,n,...\n -}\n dup\u207f\u2032 : \u2200 {i} k \u2192 C i (i * k)\n dup\u207f\u2032 {i} k = rewireWithTbl (concat (vmap replicate (allFin i)))\n{-\n dup\u207f\u2032 {i} k = coerce (proj\u2082 \u2115\u00b0.*-identity i) >>>\n (vcat {n = i} (replicate (dup\u2081 _)))\n-}\n\n dup\u00b2 : \u2200 {i} \u2192 C i (i + i)\n dup\u00b2 {i} = dup\u207f 2 >>> coerce (cong (_+_ i) (sym (\u2115\u00b0.+-comm 0 i)))\n\n{-\n dup\u00b2-spec : \u2200 {n} {is : Bits n} \u2192 is =[ dup\u00b2 ]= (is ++ is)\n dup\u00b2-spec = coerce-spec (rewireWithTbl-spec {!!} {!!})\n-}\n\n _&&&_ : \u2200 {i o\u2080 o\u2081} \u2192 C i o\u2080 \u2192 C i o\u2081 \u2192 C i (o\u2080 + o\u2081)\n c\u2080 &&& c\u2081 = dup\u00b2 >>> c\u2080 *** c\u2081\n\n{-\n _&&&-spec_ : \u2200 {i o\u2080 o\u2081} {c\u2080 : C i o\u2080} {c\u2081 : C i o\u2081} {is os\u2080 os\u2081}\n \u2192 is =[ c\u2080 ]= os\u2080 \u2192 is =[ c\u2081 ]= os\u2081 \u2192 is =[ c\u2080 &&& c\u2081 ]= (os\u2080 ++ os\u2081)\n pf\u2080 &&&-spec pf\u2081 = dup\u00b2-spec >>>-spec (pf\u2080 ***-spec pf\u2081)\n-}\n\n ext-before : \u2200 {k i o} \u2192 C i o \u2192 C (k + i) (k + o)\n ext-before {k} c = idC {k} *** c\n\n ext-after : \u2200 {k i o} \u2192 C i o \u2192 C (i + k) (o + k)\n ext-after c = c *** idC\n\n commC : \u2200 m n \u2192 C (m + n) (n + m)\n commC m n = rewireWithTbl (vmap (raise m) (allFin n) ++ vmap (inject+ n) (allFin m))\n\n dropC : \u2200 {i} k \u2192 C (k + i) i\n dropC k = sink k *** idC\n\n takeC : \u2200 {i} k \u2192 C (k + i) k\n takeC {i} k = commC k _ >>> dropC i\n\n swap : \u2200 {i} (x y : Fin i) \u2192 C i i\n -- swap x y = arr (\u03bb xs \u2192 (xs [ x ]\u2254 (lookup y xs)) [ y ]\u2254 (lookup x xs))\n swap x y = rewire (Fin.swap x y)\n\n rev : \u2200 {i} \u2192 C i i\n rev = rewire Fin.reverse\n\n swap\u2082 : C 2 2\n swap\u2082 = rev\n -- swap\u2082 = swap (# 0) (# 1)\n\n Perm : \u2115 \u2192 Set\n Perm n = List (Fin n \u00d7 Fin n)\n\n perm : \u2200 {i} \u2192 Perm i \u2192 C i i\n perm [] = idC\n perm ((x , y) \u2237 \u03c0) = swap x y >>> perm \u03c0\n\n headC : \u2200 {i} \u2192 C (1 + i) 1\n headC = takeC 1\n\n tailC : \u2200 {i} \u2192 C (1 + i) i\n tailC = dropC 1\n\n open Composable isComposable public\n open VComposable isVComposable public\n\nrecord CircuitBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n field\n isRewiringBuilder : RewiringBuilder C\n arr : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n leafC : \u2200 {o} \u2192 Bits o \u2192 C 0 o\n isForkable : Forkable suc C\n\n open RewiringBuilder isRewiringBuilder\n open Forkable isForkable renaming (fork to forkC)\n\n{-\n field\n leafC-spec : \u2200 {o} (os : Bits o) \u2192 [] =[ leafC os ]= os\n forkC-left-spec : \u2200 {i o} {c\u2080 c\u2081 : C i o} {is os}\n \u2192 is =[ c\u2080 ]= os \u2192 (0\u2237 is) =[ forkC c\u2080 c\u2081 ]= os\n forkC-right-spec : \u2200 {i o} {c\u2080 c\u2081 : C i o} {is os}\n \u2192 is =[ c\u2081 ]= os \u2192 (1\u2237 is) =[ forkC c\u2080 c\u2081 ]= os\n-}\n bit : Bit \u2192 C 0 1\n bit b = leafC (b \u2237 [])\n\n{-\n bit-spec : \u2200 b \u2192 [] =[ bit b ]= (b \u2237 [])\n bit-spec b = leafC-spec (b \u2237 [])\n-}\n\n 0\u02b7 : C 0 1\n 0\u02b7 = bit 0b\n\n 0\u02b7\u207f : \u2200 {o} \u2192 C 0 o\n 0\u02b7\u207f = leafC 0\u207f\n\n{-\n 0\u02b7-spec : [] =[ 0\u02b7 ]= 0\u2237 []\n 0\u02b7-spec = bit-spec 0b\n-}\n\n 1\u02b7 : C 0 1\n 1\u02b7 = bit 1b\n\n 1\u02b7\u207f : \u2200 {o} \u2192 C 0 o\n 1\u02b7\u207f = leafC 1\u207f\n\n{-\n 1\u02b7-spec : [] =[ 1\u02b7 ]= 1\u2237 []\n 1\u02b7-spec = bit-spec 1b\n-}\n\n padL : \u2200 {i} k \u2192 C i (k + i)\n padL k = 0\u02b7\u207f {k} *** idC\n\n padR : \u2200 {i} k \u2192 C i (i + k)\n padR k = padL k >>> commC k _\n\n arr' : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n arr' {zero} f = leafC (f [])\n arr' {suc i} f = forkC (arr' {i} (f \u2218 0\u2237_)) (arr' (f \u2218 1\u2237_))\n\n unOp : (Bit \u2192 Bit) \u2192 C 1 1\n unOp op = arr (\u03bb { (x \u2237 []) \u2192 (op x) \u2237 [] })\n\n notC : C 1 1\n notC = unOp not\n\n binOp : (Bit \u2192 Bit \u2192 Bit) \u2192 C 2 1\n binOp op = arr (\u03bb { (x \u2237 y \u2237 []) \u2192 (op x y) \u2237 [] })\n\n terOp : (Bit \u2192 Bit \u2192 Bit \u2192 Bit) \u2192 C 3 1\n terOp op = arr (\u03bb { (x \u2237 y \u2237 z \u2237 []) \u2192 (op x y z) \u2237 [] })\n\n xorC : C 2 1\n xorC = binOp _xor_\n\n eqC : C 2 1\n eqC = binOp _==\u1d47_\n\n orC : C 2 1\n orC = binOp _\u2228_\n\n andC : C 2 1\n andC = binOp _\u2227_\n\n norC : C 2 1\n norC = binOp (\u03bb x y \u2192 not (x \u2228 y))\n\n nandC : C 2 1\n nandC = binOp (\u03bb x y \u2192 not (x \u2227 y))\n\n if\u27e8head=0\u27e9then_else_ : \u2200 {i o} (c\u2080 c\u2081 : C i o) \u2192 C (1 + i) o\n if\u27e8head=0\u27e9then_else_ = forkC\n\n -- Base addition with carry:\n -- * Any input can be used as the carry\n -- * First output is the carry\n --\n -- This can also be seen as the addition of\n -- three bits which result is between 0 and 3\n -- and thus fits in two bit word in binary\n -- representation.\n --\n -- Alternatively you can see it as counting the\n -- number of 1s in a three bits vector.\n add\u2082 : C 3 2\n add\u2082 = carry &&& result\n where carry : C 3 1\n carry = terOp (\u03bb x y z \u2192 x xor (y \u2227 z))\n result : C 3 1\n result = terOp (\u03bb x y z \u2192 x xor (y xor z))\n\n open RewiringBuilder isRewiringBuilder public\n\n_\u2192\u1d47_ : CircuitType\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n_\u2192\u1da0_ : CircuitType\ni \u2192\u1da0 o = Fin o \u2192 Fin i\n\nfinFunRewiringBuilder : RewiringBuilder _\u2192\u1da0_\nfinFunRewiringBuilder = mk (opComp (ixFunComp Fin)) finFunOpVComp id id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1da0_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = Rewire.rewire f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec = tabulate\u2218lookup\n\ntblRewiringBuilder : RewiringBuilder RewireTbl\ntblRewiringBuilder = mk (mk _>>>_) (mk _***_) tabulate (allFin _) _=[_]=_ rewire-spec idC-spec\n where\n open Rewire\n C = RewireTbl\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tbl ]= output = Rewire.rewireTbl tbl input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ tabulate r ]= Rewire.rewire r bs\n rewire-spec r bs = sym (tabulate-\u2218 (flip lookup bs) r)\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ allFin _ ]= bs\n -- idC-spec = map-lookup-allFin\n idC-spec bs rewrite rewire-spec id bs = tabulate\u2218lookup bs\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n c\u2080 >>> c\u2081 = rewireTbl c\u2081 c\u2080\n -- c\u2080 >>> c\u2081 = tabulate (flip lookup c\u2080 \u2218 flip lookup c\u2081)\n -- c\u2080 >>> c\u2081 = vmap (flip lookup c\u2080) c\u2081\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n _***_ {i\u2080} c\u2080 c\u2081 = vmap (inject+ _) c\u2080 ++ vmap (raise i\u2080) c\u2081\n\nbitsFunRewiringBuilder : RewiringBuilder _\u2192\u1d47_\nbitsFunRewiringBuilder = mk bitsFunComp bitsFunVComp Rewire.rewire id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1d47_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ Rewire.rewire r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec bs = refl\n\nbitsFunCircuitBuilder : CircuitBuilder _\u2192\u1d47_\nbitsFunCircuitBuilder = mk bitsFunRewiringBuilder id (\u03bb { bs [] \u2192 bs }) bitsFunFork\n\nmodule BitsFunExtras where\n open CircuitBuilder bitsFunCircuitBuilder\n C = _\u2192\u1d47_\n >>>-spec : \u2200 {i m o} (c\u2080 : C i m) (c\u2081 : C m o) xs \u2192 (c\u2080 >>> c\u2081) xs \u2261 c\u2081 (c\u2080 xs)\n >>>-spec _ _ _ = refl\n ***-spec : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} (c\u2080 : C i\u2080 o\u2080) (c\u2081 : C i\u2081 o\u2081) xs {ys}\n \u2192 (c\u2080 *** c\u2081) (xs ++ ys) \u2261 c\u2080 xs ++ c\u2081 ys\n ***-spec {i\u2080} c\u2080 c\u2081 xs {ys} with splitAt i\u2080 (xs ++ ys)\n ... | pre , post , eq with ++-decomp {xs = xs} {pre} {ys} {post} eq\n ... | eq1 , eq2 rewrite eq1 | eq2 = refl\n\nopen import bintree hiding (_>>>_; _***_)\nopen import flipbased-tree -- hiding (_***_)\n\nTreeBits : CircuitType\nTreeBits i o = Tree (Bits o) i\n\ntreeBitsRewiringBuilder : RewiringBuilder TreeBits\ntreeBitsRewiringBuilder = mk bintree.composable bintree.vcomposable rewire (rewire id) _=[_]=_ rewire-spec idC-spec\n where\n C = TreeBits\n\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n rewire f = fromFun (Rewire.rewire f)\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tree ]= output = toFun tree input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ rewire r ]= Rewire.rewire r bs\n rewire-spec r = toFun\u2218fromFun (tabulate \u2218 flip (Vec.lookup \u2218 r))\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ rewire id ]= bs\n idC-spec bs rewrite toFun\u2218fromFun (tabulate \u2218 flip Vec.lookup) bs | tabulate\u2218lookup bs = refl\n\ntreeBitsCircuitBuilder : CircuitBuilder TreeBits\ntreeBitsCircuitBuilder = mk treeBitsRewiringBuilder fromFun leaf bintree.forkable\n\nRewiringTree : CircuitType\nRewiringTree i o = Tree (Fin i) o\n\nmodule RewiringWith2^Outputs where\n C_\u27e82^_\u27e9 = RewiringTree\n\n rewire : \u2200 {i o} \u2192 RewireFun i (2 ^ o) \u2192 C i \u27e82^ o \u27e9\n rewire f = fromFun (f \u2218 toFin)\n\n lookupFin : \u2200 {i o} \u2192 C i \u27e82^ o \u27e9 \u2192 Fin (2 ^ o) \u2192 Fin i\n lookupFin c x = bintree.lookup (fromFin x) c\n\n _>>>_ : \u2200 {i m o} \u2192 C i \u27e82^ m \u27e9 \u2192 C (2 ^ m) \u27e82^ o \u27e9 \u2192 C i \u27e82^ o \u27e9\n f >>> g = rewire (lookupFin f \u2218 lookupFin g)\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 \u27e82^ o\u2080 \u27e9 \u2192 C i\u2081 \u27e82^ o\u2081 \u27e9 \u2192 C (i\u2080 + i\u2081) \u27e82^ (o\u2080 + o\u2081) \u27e9\n f *** g = f >>= \u03bb x \u2192 map (Fin._+\u2032_ x) g\n\nmodule Test where\n open import Data.Bits.Bits4\n\n tbl : RewireTbl 4 4\n tbl = # 1 \u2237 # 0 \u2237 # 2 \u2237 # 2 \u2237 []\n\n fun : RewireFun 4 4\n fun zero = # 1\n fun (suc zero) = # 0\n fun (suc (suc zero)) = # 2\n fun (suc (suc (suc x))) = # 2\n\n-- swap x y \u2248 swap y x\n-- reverse \u2218 reverse \u2248 id\n\n abs : \u2200 {C} \u2192 RewiringBuilder C \u2192 C 4 4\n abs builder = swap\u2082 *** dup\u2081\u00b2 *** sink 1\n where open RewiringBuilder builder\n\n tinytree : Tree (Fin 4) 2\n tinytree = fork (fork (leaf (# 1)) (leaf (# 0))) (fork (leaf (# 2)) (leaf (# 2)))\n\n bigtree : Tree (Bits 4) 4\n bigtree = fork (fork (fork (same 0000b) (same 0011b)) (fork (same 1000b) (same 1011b)))\n (fork (fork (same 0100b) (same 0111b)) (fork (same 1100b) (same 1111b)))\n where same : \u2200 {n} {A : Set} \u2192 A \u2192 Tree A (suc n)\n same x = fork (leaf x) (leaf x)\n\n test\u2081 : tbl \u2261 tabulate fun\n test\u2081 = refl\n\n test\u2082 : tbl \u2261 abs tblRewiringBuilder\n test\u2082 = refl\n\n test\u2083 : tabulate fun \u2261 tabulate (abs finFunRewiringBuilder)\n test\u2083 = refl\n\n test\u2084 : bigtree \u2261 abs treeBitsRewiringBuilder\n test\u2084 = refl\n\n -- open RewiringWith2^Outputs\n -- test\u2085 : tabulate (lookupFin tinytree) \u2261 tbl\n -- test\u2085 = refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"c828ae94c2b18b988846aa0e7467ff5c68d19206","subject":"Data.Nat: + \u2115+ and \u2115+\u2032","message":"Data.Nat: + \u2115+ and \u2115+\u2032\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Nat\/NP.agda","new_file":"lib\/Data\/Nat\/NP.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Nat.NP where\n\nopen import Type\nimport Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat public hiding (module GeneralisedArithmetic; module \u2264-Reasoning; fold)\nopen import Data.Nat.Properties as Props\nopen import Data.Nat.Logical\nopen import Data.Bool.NP hiding (_==_; module ==)\nopen import Data.Product using (proj\u2081; proj\u2082; \u2203; _,_)\nopen import Data.Sum renaming (map to \u228e-map)\nopen import Data.Empty using (\u22a5-elim; \u22a5)\nopen import Function.NP\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_; _\u2257_; module \u2261-Reasoning)\n\nmodule \u2115\u00b0 = Algebra.CommutativeSemiring Props.commutativeSemiring\nmodule \u2115cmp = StrictTotalOrder Props.strictTotalOrder\nmodule \u2115\u2264 = DecTotalOrder decTotalOrder\nmodule \u2115+ = Algebra.CommutativeMonoid \u2115\u00b0.+-commutativeMonoid\nmodule \u2115+\u2032 = Algebra.Monoid \u2115\u00b0.+-monoid\nmodule \u2294\u00b0 = Algebra.CommutativeSemiringWithoutOne \u2294-\u2293-0-commutativeSemiringWithoutOne\n\nmodule \u2264-Reasoning where\n open Preorder-Reasoning \u2115\u2264.preorder public renaming (_\u223c\u27e8_\u27e9_ to _\u2264\u27e8_\u27e9_)\n infixr 2 _\u2261\u27e8_\u27e9_\n _\u2261\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x \u2261 y \u2192 y \u2264 z \u2192 x \u2264 z\n _ \u2261\u27e8 \u2261.refl \u27e9 p = p\n infixr 2 _<\u27e8_\u27e9_\n _<\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x < y \u2192 y \u2264 z \u2192 x < z\n x <\u27e8 p \u27e9 q = suc x \u2264\u27e8 p \u27e9 q\n\nsuc-injective : \u2200 {n m : \u2115} \u2192 (suc n \u2236 \u2115) \u2261 suc m \u2192 n \u2261 m\nsuc-injective = \u2261.cong pred\n\n+-\u2264-inj : \u2200 x {y z} \u2192 x + y \u2264 x + z \u2192 y \u2264 z\n+-\u2264-inj zero p = p\n+-\u2264-inj (suc x) (s\u2264s p) = +-\u2264-inj x p\n\nsucx\u2270x : \u2200 x \u2192 suc x \u2270 x\nsucx\u2270x zero = \u03bb()\nsucx\u2270x (suc x) = sucx\u2270x x \u2218 \u2264-pred\n\na\u2293b\u2261a : \u2200 {a b} \u2192 a \u2264 b \u2192 a \u2293 b \u2261 a\na\u2293b\u2261a z\u2264n = \u2261.refl\na\u2293b\u2261a (s\u2264s a\u2264b) rewrite a\u2293b\u2261a a\u2264b = \u2261.refl\n\ntotal-\u2264 : \u2200 a b \u2192 a \u2264 b \u228e b \u2264 a\ntotal-\u2264 zero b = inj\u2081 z\u2264n\ntotal-\u2264 (suc a) zero = inj\u2082 z\u2264n\ntotal-\u2264 (suc a) (suc b) with total-\u2264 a b\n... | inj\u2081 p = inj\u2081 (s\u2264s p)\n... | inj\u2082 p = inj\u2082 (s\u2264s p)\n\na\u2261a\u2293b+a\u2238b : \u2200 a b \u2192 a \u2261 a \u2293 b + (a \u2238 b)\na\u2261a\u2293b+a\u2238b zero zero = \u2261.refl\na\u2261a\u2293b+a\u2238b zero (suc b) = \u2261.refl\na\u2261a\u2293b+a\u2238b (suc a) zero = \u2261.refl\na\u2261a\u2293b+a\u2238b (suc a) (suc b) rewrite \u2261.sym (a\u2261a\u2293b+a\u2238b a b) = \u2261.refl\n\n\u00acn\u2264x dist (x * y) (x * z) == x * dist y z))\n\ninfix 8 _^_\n_^_ : \u2115 \u2192 \u2115 \u2192 \u2115\nb ^ zero = 1\nb ^ suc n = b * b ^ n\n\n2^_ : \u2115 \u2192 \u2115\n2^ n = \u27e82^ n * 1 \u27e9\n\n2^-spec : \u2200 n \u2192 2^ n \u2261 2 ^ n\n2^-spec zero = \u2261.refl\n2^-spec (suc n) rewrite 2^-spec n | 2*-spec (2 ^ n) = \u2261.refl\n\n2^*-spec : \u2200 m n \u2192 2^\u27e8 m \u27e9* n \u2261 2 ^ m * n\n2^*-spec zero n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\n2^*-spec (suc m) n rewrite 2^*-spec m n\n | \u2115\u00b0.*-assoc 2 (2 ^ m) n\n | \u2115\u00b0.+-comm (2 ^ m * n) 0 = \u2261.refl\n\n1\u22642^ : \u2200 n \u2192 2^ n \u2265 1\n1+\u22642^ : \u2200 n \u2192 2^ n \u2265 1 + n\n1+\u22642^ zero = s\u2264s z\u2264n\n1+\u22642^ (suc n) = (1\u22642^ n) +-mono (1+\u22642^ n)\n\n1\u22642^ n = \u2115\u2264.trans (s\u2264s z\u2264n) (1+\u22642^ n)\n\n\u2264-steps\u2032 : \u2200 {x} y \u2192 x \u2264 x + y\n\u2264-steps\u2032 {x} y rewrite \u2115\u00b0.+-comm x y = \u2264-steps y \u2115\u2264.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Hyper_operator\n_\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\na \u2191\u27e8 suc n \u27e9 (suc b) = a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\na \u2191\u27e8 0 \u27e9 b = suc b\na \u2191\u27e8 1 \u27e9 0 = a\na \u2191\u27e8 2 \u27e9 0 = 0\na \u2191\u27e8 suc (suc (suc n)) \u27e9 0 = 1\n\nmodule \u2191-Props where\n \u2191-fold : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 b \u2261 fold (a \u2191\u27e8 suc n \u27e9 0) (_\u2191\u27e8_\u27e9_ a n) b\n \u2191-fold a n zero = \u2261.refl\n \u2191-fold a n (suc b) = \u2261.cong (_\u2191\u27e8_\u27e9_ a n) (\u2191-fold a n b)\n\n \u2191\u2080-+ : \u2200 a b \u2192 a \u2191\u27e8 0 \u27e9 b \u2261 1 + b\n \u2191\u2080-+ a b = \u2261.refl\n\n \u2191\u2081-+ : \u2200 a b \u2192 a \u2191\u27e8 1 \u27e9 b \u2261 b + a\n \u2191\u2081-+ a zero = \u2261.refl\n \u2191\u2081-+ a (suc b) = \u2261.cong suc (\u2191\u2081-+ a b)\n\n \u2191\u2082-* : \u2200 a b \u2192 a \u2191\u27e8 2 \u27e9 b \u2261 b * a\n \u2191\u2082-* a zero = \u2261.refl\n \u2191\u2082-* a (suc b) rewrite \u2191\u2082-* a b\n | \u2191\u2081-+ a (b * a)\n | \u2115\u00b0.+-comm (b * a) a\n = \u2261.refl\n\n -- fold 1 (_*_ a) b \u2261 a ^ b\n \u2191\u2083-^ : \u2200 a b \u2192 a \u2191\u27e8 3 \u27e9 b \u2261 a ^ b\n \u2191\u2083-^ a zero = \u2261.refl\n \u2191\u2083-^ a (suc b) rewrite \u2191\u2083-^ a b\n | \u2191\u2082-* a (a ^ b)\n | \u2115\u00b0.*-comm (a ^ b) a\n = \u2261.refl\n\n _\u2191\u27e8_\u27e90 : \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u27e8 0 \u27e90 = 1\n a \u2191\u27e8 1 \u27e90 = a\n a \u2191\u27e8 2 \u27e90 = 0\n a \u2191\u27e8 suc (suc (suc n)) \u27e90 = 1\n\n _`\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _`\u2191\u27e8_\u27e9_ a 0 = suc\n _`\u2191\u27e8_\u27e9_ a (suc n) = fold (a \u2191\u27e8 suc n \u27e90) (_`\u2191\u27e8_\u27e9_ a n)\n\n \u2191-ind-rule : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 (suc b) \u2261 a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\n \u2191-ind-rule a n b = \u2261.refl\n\n _\u2191\u2032\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u2032\u27e8 0 \u27e9 b = suc b\n a \u2191\u2032\u27e8 1 \u27e9 b = a + b\n a \u2191\u2032\u27e8 2 \u27e9 b = a * b\n a \u2191\u2032\u27e8 3 \u27e9 b = a ^ b\n a \u2191\u2032\u27e8 suc (suc (suc (suc n))) \u27e9 b = a \u2191\u27e8 4 + n \u27e9 b\n\n \u2191-\u2191\u2032 : \u2200 a n b \u2192 a \u2191\u27e8 n \u27e9 b \u2261 a \u2191\u2032\u27e8 n \u27e9 b\n \u2191-\u2191\u2032 a 0 b = \u2261.refl\n \u2191-\u2191\u2032 a 1 b = \u2261.trans (\u2191\u2081-+ a b) (\u2115\u00b0.+-comm b a)\n \u2191-\u2191\u2032 a 2 b = \u2261.trans (\u2191\u2082-* a b) (\u2115\u00b0.*-comm b a)\n \u2191-\u2191\u2032 a 3 b = \u2191\u2083-^ a b\n \u2191-\u2191\u2032 a (suc (suc (suc (suc _)))) b = \u2261.refl\n\n _\u21912+\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _\u21912+\u27e8_\u27e9_ a = fold (_*_ a) (fold 1)\n\nmodule InflModule where\n -- Inflationary functions\n Infl< : (f : \u2115 \u2192 \u2115) \u2192 \u2605\n Infl< f = \u2200 {x} \u2192 x < f x\n\n Infl : (f : \u2115 \u2192 \u2115) \u2192 \u2605\n Infl f = Infl< (suc \u2218 f)\n\n InflT< : (f : Endo (Endo \u2115)) \u2192 \u2605\n InflT< f = \u2200 {g} \u2192 Infl< g \u2192 Infl< (f g)\n\n{-\n\u2200 {x} \u2192 x \u2264 f x\n\n\n 1 + x \u2264 1 + f x\n x < 1 + f x\n x < (suc \u2218 f) x\n Infl< (suc \u2218 f)\n\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n\nInfl< f \u2192 Infl f\nInfl< f \u2192 Infl< (suc \u2218 f)\n-}\n\n id-infl : Infl id\n id-infl = \u2115\u2264.refl\n\n \u2218-infl< : \u2200 {f g} \u2192 Infl< f \u2192 Infl< g \u2192 Infl< (f \u2218 g)\n \u2218-infl< inflf< inflg< = <-trans inflg< inflf<\n\n suc-infl< : Infl< suc\n suc-infl< = \u2115\u2264.refl\n\n suc-infl : Infl suc\n suc-infl = s\u2264s (\u2264-step \u2115\u2264.refl)\n\n nest-infl : \u2200 f (finfl : Infl f) n \u2192 Infl (nest n f)\n nest-infl f _ zero = \u2115\u2264.refl\n nest-infl f finfl (suc n) = \u2115\u2264.trans (nest-infl f finfl n) finfl\n\n nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n nest-infl< f finfl< zero = finfl<\n nest-infl< f finfl< (suc n) = <-trans (nest-infl< f finfl< n) finfl< \n\n-- See Function.NP.nest-Properties for more properties\nmodule fold-Props where\n\n fold-suc : \u2200 m n \u2192 fold m suc n \u2261 n + m\n fold-suc m zero = \u2261.refl\n fold-suc m (suc n) rewrite fold-suc m n = \u2261.refl\n\n fold-+ : \u2200 m n \u2192 fold 0 (_+_ m) n \u2261 n * m\n fold-+ m zero = \u2261.refl\n fold-+ m (suc n) rewrite fold-+ m n = \u2261.refl\n\n fold-* : \u2200 m n \u2192 fold 1 (_*_ m) n \u2261 m ^ n\n fold-* m zero = \u2261.refl\n fold-* m (suc n) rewrite fold-* m n = \u2261.refl\n\n{- TODO\n fold-^ : \u2200 m n \u2192 fold 1 (flip _^_ m) n \u2261 m \u2191\u27e8 4 \u27e9 n\n fold-^ m zero = \u2261.refl\n fold-^ m (suc n) rewrite fold-^ m n = ?\n-}\n\n cong-fold : \u2200 {A : \u2605} {f g : Endo A} (f\u2257g : f \u2257 g) {z} \u2192 fold z f \u2257 fold z g\n cong-fold eq zero = \u2261.refl\n cong-fold eq {z} (suc x) rewrite cong-fold eq {z} x = eq _\n\n private\n open \u2191-Props\n module Alt\u21912 where\n alt : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n alt a 0 = \u03bb b \u2192 a \u2191\u27e8 2 \u27e9 b\n alt a (suc n) = fold 1 (alt a n)\n\n alt-ok : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 alt a n\n alt-ok a zero = \u2261.sym \u2218 \u2191-\u2191\u2032 a 2\n alt-ok a (suc n) = cong-fold (alt-ok a n)\n\n{- TODO\n alt' : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 _\u2191\u27e8_\u27e9_ a (2 + n)\n alt' a zero b = \u2261.sym (\u2191-\u2191\u2032 a 2 _)\n alt' a (suc n) zero = \u2261.refl\n alt' a (suc n) (suc x) rewrite alt' a n (_\u21912+\u27e8_\u27e9_ a (suc n) x)\n = \u2261.cong (_\u2191\u27e8_\u27e9_ a (2 + n)) { fold 1 (fold (_*_ a) (fold 1) n) x} {a \u2191\u27e8 suc (suc (suc n)) \u27e9 x} (alt' a (suc n) x)\n-}\n\n open InflModule\n{-\n fold1-infl : \u2200 f \u2192 Infl f \u2192 Infl (fold 1 f)\n fold1-infl f finfl {zero} = {!z\u2264n!}\n fold1-infl f finfl {suc x} =\n x <\u27e8 {!s\u2264s (fold1-infl f finfl {x})!} \u27e9\n fold 1 f x <\u27e8 {!!} \u27e9\n {- f (fold 1 f x) \u2264\u27e8 finfl \u27e9\n f (fold 1 f x) \u2261\u27e8 \u2261.refl \u27e9 -}\n fold 1 f (1 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 f \u2218 suc)\n fold1+1-infl< f finfl< {zero} = finfl<\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + f (fold 1 f x) \u2264\u27e8 finfl< {f (fold 1 f x)} \u27e9\n f (suc (f (fold 1 f x))) \u2264\u27e8 {!!} \u27e9\n fold 1 f (2 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 (f \u2218 suc))\n fold1+1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 suc) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 suc) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+-infl< : \u2200 k f \u2192 Infl< (f \u2218\u2032 _+_ k) \u2192 Infl< (fold 1 (f \u2218\u2032 _+_ k))\n fold1+-infl< k f finfl< {zero} = s\u2264s z\u2264n\n fold1+-infl< k f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-infl< k f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 _+_ k) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 _+_ k) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n \n{-\n fold1+-infl< : \u2200 f \u2192 Infl+2< f \u2192 Infl+2< (fold 1 f)\n fold1+-infl< f finfl< {zero} _ = \u2115\u2264.refl\n fold1+-infl< f finfl< {suc zero} _ = {!!}\n fold1+-infl< f finfl< {suc (suc zero)} _ = {!!}\n fold1+-infl< f finfl< {suc (suc (suc x))} x>1 =\n 4 + x \u2264\u27e8 s\u2264s (fold1+-infl< f finfl< {suc (suc x)} (m\u2264m+n 2 x)) \u27e9\n 1 + fold 1 f (2 + x) \u2264\u27e8 finfl< {!!} \u27e9\n fold 1 f (3 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 f x \u2264\u27e8 finfl< \u27e9\n fold 1 f (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\nmodule ^-Props where\n open \u2261-Reasoning\n\n ^1 : \u2200 n \u2192 n ^ 1 \u2261 n\n ^1 zero = \u2261.refl\n ^1 (suc n) = \u2261.cong suc (proj\u2082 \u2115\u00b0.*-identity n)\n\n ^-+ : \u2200 b m n \u2192 b ^ (m + n) \u2261 b ^ m * b ^ n\n ^-+ b zero n = \u2261.sym (proj\u2082 \u2115\u00b0.+-identity (b ^ n))\n ^-+ b (suc m) n rewrite ^-+ b m n = \u2261.sym (\u2115\u00b0.*-assoc b (b ^ m) (b ^ n))\n\n ^-* : \u2200 b m n \u2192 b ^ (m * n) \u2261 (b ^ n) ^ m\n ^-* b zero n = \u2261.refl\n ^-* b (suc m) n = b ^ (n + m * n)\n \u2261\u27e8 ^-+ b n (m * n) \u27e9\n b ^ n * b ^ (m * n)\n \u2261\u27e8 \u2261.cong (_*_ (b ^ n)) (^-* b m n) \u27e9\n b ^ n * (b ^ n) ^ m \u220e\n\nack : \u2115 \u2192 \u2115 \u2192 \u2115\nack zero n = suc n\nack (suc m) zero = ack m (suc zero)\nack (suc m) (suc n) = ack m (ack (suc m) n)\n\n\u2264\u21d2\u2203 : \u2200 {m n} \u2192 m \u2264 n \u2192 \u2203 \u03bb k \u2192 m + k \u2261 n\n\u2264\u21d2\u2203 z\u2264n = _ , \u2261.refl\n\u2264\u21d2\u2203 (s\u2264s pf) = _ , \u2261.cong suc (proj\u2082 (\u2264\u21d2\u2203 pf))\n\nmodule ack-Props where\n lem\u2238 : \u2200 {m n} \u2192 m \u2264 n \u2192 m + (n \u2238 m) \u2261 n\n lem\u2238 z\u2264n = \u2261.refl\n lem\u2238 (s\u2264s {m} {n} m\u2264n) = \u2261.cong suc (lem\u2238 m\u2264n)\n\n -- n >= m \u2192 m + (n \u2238 m) \u2261 n\n -- n >= m \u2192 \u2203 k \u2192 n \u2261 m + k\n -- \u2203 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 m + ((m + k) \u2238 m) \u2261 m + k\n -- \u2200 k \u2192 m + k \u2261 m + k\n\n -- 3 \u2264 x \u2192 P x \u2192 \u2203 \u03bb k \u2192 P (3 + k)\n\n open InflModule\n\n fold1+-inflT< : \u2200 {z} \u2192 InflT< (fold (suc z))\n fold1+-inflT< _ {zero} = s\u2264s z\u2264n\n fold1+-inflT< {z} {f} finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-inflT< {z} {f} finfl< {x}) \u27e9\n 1 + fold (suc z) f x \u2264\u27e8 finfl< \u27e9\n f (fold (suc z) f x) \u2261\u27e8 \u2261.refl \u27e9\n fold (suc z) f (1 + x)\n \u220e\n where open \u2264-Reasoning\n\n Mon : (f : \u2115 \u2192 \u2115) \u2192 \u2605\n Mon f = \u2200 {x y} \u2192 x \u2264 y \u2192 f x \u2264 f y\n open InflModule\n\n\n \u2115-ind : \u2200 (P : \u2115 \u2192 \u2605) \u2192 P zero \u2192 (\u2200 {n} \u2192 P n \u2192 P (suc n)) \u2192 \u2200 {n} \u2192 P n\n \u2115-ind P P0 PS {zero} = P0\n \u2115-ind P P0 PS {suc n} = PS (\u2115-ind P P0 PS)\n\n open fold-Props\n fold-infl< : \u2200 {f g} \u2192 Infl< f \u2192 InflT< g \u2192 \u2200 {n} \u2192 Infl< (fold f g n)\n fold-infl< {f} {g} inflf< inflTg< {n} = \u2115-ind (Infl< \u2218 fold f g) inflf< inflTg< {n}\n\n fold-mon : \u2200 {f} (fmon : Mon f) (finfl< : Infl< f) {z} \u2192 Mon (fold z f)\n fold-mon fmon finfl< {_} {y = 0} z\u2264n = \u2115\u2264.refl\n fold-mon {f} fmon finfl< {z} {y = suc n} z\u2264n = z \u2264\u27e8 \u2264-step \u2115\u2264.refl \u27e9\n suc z \u2264\u27e8 nest-infl< f finfl< n {z} \u27e9\n fold z f (suc n) \u2261\u27e8 \u2261.refl \u27e9\n fold z f (suc n) \u220e\n where open \u2264-Reasoning\n fold-mon fmon finfl< (s\u2264s m\u2264n) = fmon (fold-mon fmon finfl< m\u2264n)\n\n MonT : Endo (Endo \u2115) \u2192 \u2605\n MonT g = \u2200 {f} \u2192 Mon f \u2192 Infl< f \u2192 Mon (g f)\n\n fold-mon' : \u2200 {f g} \u2192 Mon f \u2192 Infl< f \u2192 MonT g \u2192 InflT< g \u2192 \u2200 {n} \u2192 Mon (fold f g n)\n fold-mon' {f} {g} mon-f infl-f mont-g inflTg< {n} = go n where\n go : \u2200 n \u2192 Mon (fold f g n)\n go zero = mon-f\n go (suc n) = mont-g (go n) (fold-infl< infl-f inflTg< {n})\n\n\n -- +-fold : \u2200 a b \u2192 a + b \u2261 fold a suc b\n -- *-fold : \u2200 a b \u2192 a * b \u2261 fold 0 (_+_ a) b\n\n 1\u22641+a^b : \u2200 a b \u2192 1 \u2264 (1 + a) ^ b\n 1\u22641+a^b a zero = s\u2264s z\u2264n\n 1\u22641+a^b a (suc b) = 1\u22641+a^b a b +-mono z\u2264n\n\n 1+a^-mon : \u2200 {a} \u2192 Mon (_^_ (1 + a))\n 1+a^-mon {a} (z\u2264n {b}) = 1\u22641+a^b a b\n 1+a^-mon {a} (s\u2264s {m} {n} m\u2264n)\n = (1 + a) ^ (1 + m) \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ m + a * (1 + a) ^ m \u2264\u27e8 (1+a^-mon m\u2264n) +-mono ((a \u220e) *-mono (1+a^-mon m\u2264n)) \u27e9\n (1 + a) ^ n + a * (1 + a) ^ n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ (1 + n) \u220e\n where open \u2264-Reasoning\n\n lem2^3 : \u2200 n \u2192 2 ^ 3 \u2264 2 ^ (3 + n)\n lem2^3 n = 1+a^-mon {1} {3} {3 + n} (s\u2264s (s\u2264s (s\u2264s z\u2264n)))\n\n lem2221 : \u2200 m \u2192 2 \u2261 2 \u2191\u27e8 2 + m \u27e9 1\n lem2221 zero = \u2261.refl\n lem2221 (suc m) = lem2221 m\n\n lem4212 : \u2200 m \u2192 4 \u2261 2 \u2191\u27e8 1 + m \u27e9 2\n lem4212 zero = \u2261.refl\n lem4212 (suc m) = 4 \u2261\u27e8 lem4212 m \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 2 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 (1 + m)) (lem2221 m) \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 (2 \u2191\u27e8 2 + m \u27e9 1) \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 2 + m \u27e9 2 \u220e\n where open \u2261-Reasoning\n\n 2* = _*_ 2\n\n -- fold 2* n 1 \u2261 2 ^ n\n\n -- nest-* : \u2200 m n \u2192 nest (m * n) f \u2257 nest m (nest n f)\n\n nest-2* : \u2200 m n \u2192 nest (m * n) (fold 1) 2* \u2261 nest m (nest n (fold 1)) 2*\n nest-2* m n = nest-Properties.nest-* (fold 1) m n 2*\n\n{-\n lem3\u2264 : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 2\n lem3\u2264 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264 (suc n) = \n 3 \u2264\u27e8 {!!} \u27e9\n fold 2* f1 (1 + n) 2 \u220e\n where open \u2264-Reasoning\n f1 = fold 1\n\n lem3\u2264' : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 3\n lem3\u2264' 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264' (suc n) = \n 3 \u2264\u27e8 lem3\u2264 n \u27e9\n fold 2* (fold 1) n 2 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) n (fold 2* (fold 1) 3 1) \u2261\u27e8 {!!} \u27e9\n{- \u2261\u27e8 nest-Properties.nest-* {!2*!} {!!} {!!} {!!} \u27e9\n fold 2* (fold 1) (3 * n) 1 \u2261\u27e8 {!!} \u27e9\n-}\n fold 1 (fold 2* (fold 1) n) 3 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) (1 + n) 3 \u220e\n where open \u2264-Reasoning\n-}\n---\n\n{-\nfold 2* (fold 1) (3 * n) 1 \u2264\n\nfold 2* (fold 1) n (fold 2* (fold 1) n (fold 2* (fold 1) n 1)) \u2264\n\nfold 1 (fold 2* (fold 1) n) 3 \u2264\nfold 2* (fold 1) (1 + n) 3\n-}\n\n{-\n open \u2191-Props\n\n -*\u27e81+n\u27e9-infl : \u2200 n \u2192 Infl (_*_ (1 + n))\n -*\u27e81+n\u27e9-infl n = {!!}\n\n lem121 : \u2200 a b \u2192 b < (1 + a) \u2191\u27e8 2 \u27e9 (1 + b)\n lem121 a b rewrite \u2191\u2082-* (1 + a) (1 + b)\n = 1 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (1 + a) * (1 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (1 + a) (1 + b) \u27e9\n (1 + b) * (1 + a)\n \u220e\n where open \u2264-Reasoning\n\n -- \u2200 a b \u2192 b < (2 + a) * b\n lem222 : \u2200 a b \u2192 b < (2 + a) \u2191\u27e8 2 \u27e9 (2 + b)\n lem222 a b rewrite \u2191\u2082-* (2 + a) (2 + b)\n = 1 + b\n \u2264\u27e8 suc-infl \u27e9\n 2 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (2 + a) * (2 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (2 + a) (2 + b) \u27e9\n (2 + b) * (2 + a)\n \u220e\n where open \u2264-Reasoning\n-}\n{-\n open fold-Props\n\n module Foo a where\n f = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b\n f-lem : \u2200 n \u2192 f (suc n) \u2257 fold 1 (f n)\n f-lem n x = \u2261.refl\n\n f2 = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n f2-lem : \u2200 n \u2192 f2 (suc n) \u2257 fold 1 (f2 n)\n f2-lem n x = {!!}\n\n -- b < (1 + a) * (1 + b)\n\n -- nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n infl3< : \u2200 a n \u2192 Infl< (\u03bb b \u2192 (1 + a) \u21912+\u27e8 n \u27e9 (1 + b))\n infl3< a zero {b} = lem121 a b\n infl3< a (suc n) {b}\n = 1 + b\n -- fold1-infl< : \u2200 f \u2192 Infl< f \u2192 \u2200 x \u2192 x < fold 1 f x\n \u2264\u27e8 {!fold1f2-infl< {_}!} \u27e9\n fold 1 f1 b\n \u2264\u27e8 {!!} \u27e9\n{-\n \u2264\u27e8 fold1-infl< (f 0) (\u03bb {x} \u2192 {!infl3< a n {x}!}) {{!!}} \u27e9\n {!!}\n \u2264\u27e8 {!!} \u27e9\n-}\n{-\n 1 + b\n (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n \u2264\u27e8 {!nest-infl< (\u03bb b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b) ? b {(2 + a) \u21912+\u27e8 n \u27e9 (2 + b)}!} \u27e9\n fold (f 0 1) (f 0) (1 + b)\n \u2264\u27e8 {!\u2115\u2264.reflexive (\u2261.cong (\u03bb g \u2192 g 1) (nest-Properties.nest-+ (f 0) 1 (1 + b)))!} \u27e9\n-}\n fold 1 (f 0) (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n f 1 (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) \u21912+\u27e8 suc n \u27e9 (1 + b)\n \u220e\n\n where\n f = \u03bb k b \u2192 (1 + a) \u21912+\u27e8 k + n \u27e9 b\n f1 = f 0 \u2218 suc\n\n -- fold1f2-infl< : Infl< (fold 1 f2)\n -- fold1f2-infl< = fold1-infl< f2 {!(\u03bb b \u2192 infl3< a n b ?)!}\n\n fold1f1-infl<' : Infl< (fold 1 f1)\n fold1f1-infl<' = {!fold1+1-infl< (f 0) {!infl3< a n!}!}\n lem : \u2200 x \u2192 f 0 (1 + x) < f 0 (f 0 x)\n lem x = {!!}\n postulate\n finfl : Infl< (f 0)\n open \u2264-Reasoning\n-}\n\n_\u21d1\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\na \u21d1\u27e8 n , k \u27e9 b = fold k f n\n module M\u21d1 where \n f : \u2115 \u2192 \u2115\n f x = a \u2191\u27e8 2 + x \u27e9 b\n\nmodule \u21d1-Props where\n _\u21d1\u2032\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\n a \u21d1\u2032\u27e8 n\u2080 , k \u27e9 b = g n\u2080\n module M\u21d1\u2032 where\n h : \u2115 \u2192 \u2115\n h x = a \u2191\u27e8 2 + x \u27e9 b\n\n g : \u2115 \u2192 \u2115\n g 0 = k\n g (suc n) = h (g n)\n\n g-nest : \u2200 n \u2192 g n \u2261 h $\u27e8 n \u27e9 k\n g-nest zero = \u2261.refl\n g-nest (suc n) rewrite g-nest n = \u2261.refl\n\n \u21d1\u2080 : \u2200 a k b \u2192 a \u21d1\u27e8 0 , k \u27e9 b \u2261 k\n \u21d1\u2080 _ _ _ = \u2261.refl\n\n \u21d1\u2081 : \u2200 a k b \u2192 a \u21d1\u27e8 1 , k \u27e9 b \u2261 a \u2191\u27e8 2 + k \u27e9 b\n \u21d1\u2081 _ _ _ = \u2261.refl\n\n \u21d1-\u21d1\u2032 : \u2200 a n k b \u2192 a \u21d1\u27e8 n , k \u27e9 b \u2261 a \u21d1\u2032\u27e8 n , k \u27e9 b\n \u21d1-\u21d1\u2032 a n k b rewrite M\u21d1\u2032.g-nest a n k b n = \u2261.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Graham%27s_number\nGraham's-number : \u2115\nGraham's-number = 3 \u21d1\u27e8 64 , 4 \u27e9 3\n -- = (f\u2218\u2076\u2074\u2026\u2218f)4\n -- where f x = 3 \u2191\u27e8 2 + x \u27e9 3\n\n{-\nmodule GeneralisedArithmetic {a} {A : Set a} (0# : A) (1+ : A \u2192 A) where\n\n 1# : A\n 1# = 1+ 0#\n\n open Data.Nat.GeneralisedArithmetic 0# 1+ public\n\n exp : (* : A \u2192 A \u2192 A) \u2192 (\u2115 \u2192 A \u2192 A)\n exp _*_ n b = fold 1# (\u03bb s \u2192 b * s) n\n-}\n -- hyperop a n b = fold exp\n\nmodule == where\n _\u2248_ : (m n : \u2115) \u2192 \u2605\n m \u2248 n = T (m == n)\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {zero} {zero} _ = id\n subst P {suc _} {suc _} p = subst (P \u2218 suc) p\n subst _ {zero} {suc _} ()\n subst _ {suc _} {zero} ()\n\n sound : \u2200 m n \u2192 T (m == n) \u2192 m \u2261 n\n sound m n p = subst (_\u2261_ m) p \u2261.refl\n\n refl : Reflexive _\u2248_\n refl {zero} = _\n refl {suc n} = refl {n}\n\n sym : Symmetric _\u2248_\n sym {m} {n} eq rewrite sound m n eq = refl {n}\n\n trans : Transitive _\u2248_\n trans {m} {n} {o} m\u2248n n\u2248o rewrite sound m n m\u2248n | sound n o n\u2248o = refl {o}\n\n setoid : Setoid _ _\n setoid = record { Carrier = \u2115; _\u2248_ = _\u2248_\n ; isEquivalence =\n record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} } }\n\n open Setoid setoid public hiding (refl; sym; trans; _\u2248_)\n\n{-\ndata _`\u2264?`_\u219d_ : (m n : \u2115) \u2192 Dec (m \u2264 n) \u2192 \u2605 where\n z\u2264?n : \u2200 {n} \u2192 zero `\u2264?` n \u219d yes z\u2264n\n s\u2264?z : \u2200 {m} \u2192 suc m `\u2264?` zero \u219d no \u03bb()\n s\u2264?s-yes : \u2200 {m n m\u2264n} \u2192 m `\u2264?` n \u219d yes m\u2264n \u2192 suc m `\u2264?` suc n \u219d yes (s\u2264s m\u2264n)\n s\u2264?s-no : \u2200 {m n m\u2270n} \u2192 m `\u2264?` n \u219d no m\u2270n \u2192 suc m `\u2264?` suc n \u219d no (m\u2270n \u2218 \u2264-pred)\n\n`\u2264?`-complete : \u2200 m n \u2192 m `\u2264?` n \u219d (m \u2264? n)\n`\u2264?`-complete zero n = z\u2264?n\n`\u2264?`-complete (suc n) zero = {!s\u2264?z!}\n`\u2264?`-complete (suc m) (suc n) with m \u2264? n | `\u2264?`-complete m n\n... | yes q | r = s\u2264?s-yes r\n... | no q | r = s\u2264?s-no {!!}\n-}\n\n_<=_ : (x y : \u2115) \u2192 Bool\nzero <= _ = true\nsuc _ <= zero = false\nsuc m <= suc n = m <= n\n\nmodule <= where\n \u211b : \u2115 \u2192 \u2115 \u2192 \u2605\n \u211b x y = T (x <= y)\n\n sound : \u2200 m n \u2192 \u211b m n \u2192 m \u2264 n\n sound zero _ _ = z\u2264n\n sound (suc m) (suc n) p = s\u2264s (sound m n p)\n sound (suc m) zero ()\n\n complete : \u2200 {m n} \u2192 m \u2264 n \u2192 \u211b m n\n complete z\u2264n = _\n complete (s\u2264s m\u2264n) = complete m\u2264n\n\n isTotalOrder : IsTotalOrder _\u2261_ \u211b\n isTotalOrder = record { isPartialOrder = isPartialOrder; total = \u03bb x y \u2192 \u228e-map complete complete (\u2115\u2264.total x y) }\n where\n reflexive : \u2200 {i j} \u2192 i \u2261 j \u2192 \u211b i j\n reflexive {i} \u2261.refl = complete (\u2115\u2264.refl {i})\n trans : Transitive \u211b\n trans {x} {y} {z} p q = complete (\u2115\u2264.trans (sound x y p) (sound y z q))\n isPreorder : IsPreorder _\u2261_ \u211b\n isPreorder = record { isEquivalence = \u2261.isEquivalence\n ; reflexive = reflexive\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n antisym : Antisymmetric _\u2261_ \u211b\n antisym {x} {y} p q = \u2115\u2264.antisym (sound x y p) (sound y x q)\n isPartialOrder : IsPartialOrder _\u2261_ \u211b\n isPartialOrder = record { isPreorder = isPreorder; antisym = antisym }\n\n open IsTotalOrder isTotalOrder public\n\n<=-steps\u2032 : \u2200 {x} y \u2192 T (x <= (x + y))\n<=-steps\u2032 {x} y = <=.complete (\u2264-steps\u2032 {x} y)\n\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 : \u2200 x y \u2192 suc x \u2238 y \u2264 suc (x \u2238 y)\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x zero = \u2115\u2264.refl\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 zero (suc y) rewrite 0\u2238n\u22610 y = z\u2264n\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 (suc x) (suc y) = sucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x y\n\nx\u22642y\u2032\u2192x\u2238y\u2264y : \u2200 x y \u2192 x \u2264 2*\u2032 y \u2192 x \u2238 y \u2264 y\nx\u22642y\u2032\u2192x\u2238y\u2264y x zero p = p\nx\u22642y\u2032\u2192x\u2238y\u2264y zero (suc y) p = z\u2264n\nx\u22642y\u2032\u2192x\u2238y\u2264y (suc zero) (suc y) (s\u2264s p) rewrite 0\u2238n\u22610 y = z\u2264n\nx\u22642y\u2032\u2192x\u2238y\u2264y (suc (suc x)) (suc y) (s\u2264s (s\u2264s p))\n = \u2115\u2264.trans (sucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x y) (s\u2264s (x\u22642y\u2032\u2192x\u2238y\u2264y x y p))\n\nx<2y\u2032\u2192x\u2238y \u00aca \u00acb c = \u22a5-elim (p (\u2264-pred c))\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (Props.\u2264-steps 1 1+n\u2264m)\n\nnot<=\u2192< : \u2200 x y \u2192 T (not (x <= y)) \u2192 T (suc y <= x)\nnot<=\u2192< x y p = <=.complete (\u2270\u2192< x y (T'not'\u00ac p \u2218 <=.complete))\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Nat.NP where\n\nopen import Type\nimport Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat public hiding (module GeneralisedArithmetic; module \u2264-Reasoning; fold)\nopen import Data.Nat.Properties as Props\nopen import Data.Nat.Logical\nopen import Data.Bool.NP hiding (_==_; module ==)\nopen import Data.Product using (proj\u2081; proj\u2082; \u2203; _,_)\nopen import Data.Sum renaming (map to \u228e-map)\nopen import Data.Empty using (\u22a5-elim; \u22a5)\nopen import Function.NP\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_; _\u2257_; module \u2261-Reasoning)\n\nmodule \u2115\u00b0 = Algebra.CommutativeSemiring Props.commutativeSemiring\nmodule \u2115cmp = StrictTotalOrder Props.strictTotalOrder\nmodule \u2115\u2264 = DecTotalOrder decTotalOrder\nmodule \u2294\u00b0 = Algebra.CommutativeSemiringWithoutOne \u2294-\u2293-0-commutativeSemiringWithoutOne\n\nmodule \u2264-Reasoning where\n open Preorder-Reasoning \u2115\u2264.preorder public renaming (_\u223c\u27e8_\u27e9_ to _\u2264\u27e8_\u27e9_)\n infixr 2 _\u2261\u27e8_\u27e9_\n _\u2261\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x \u2261 y \u2192 y \u2264 z \u2192 x \u2264 z\n _ \u2261\u27e8 \u2261.refl \u27e9 p = p\n infixr 2 _<\u27e8_\u27e9_\n _<\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x < y \u2192 y \u2264 z \u2192 x < z\n x <\u27e8 p \u27e9 q = suc x \u2264\u27e8 p \u27e9 q\n\nsuc-injective : \u2200 {n m : \u2115} \u2192 (suc n \u2236 \u2115) \u2261 suc m \u2192 n \u2261 m\nsuc-injective = \u2261.cong pred\n\n+-\u2264-inj : \u2200 x {y z} \u2192 x + y \u2264 x + z \u2192 y \u2264 z\n+-\u2264-inj zero p = p\n+-\u2264-inj (suc x) (s\u2264s p) = +-\u2264-inj x p\n\nsucx\u2270x : \u2200 x \u2192 suc x \u2270 x\nsucx\u2270x zero = \u03bb()\nsucx\u2270x (suc x) = sucx\u2270x x \u2218 \u2264-pred\n\na\u2293b\u2261a : \u2200 {a b} \u2192 a \u2264 b \u2192 a \u2293 b \u2261 a\na\u2293b\u2261a z\u2264n = \u2261.refl\na\u2293b\u2261a (s\u2264s a\u2264b) rewrite a\u2293b\u2261a a\u2264b = \u2261.refl\n\ntotal-\u2264 : \u2200 a b \u2192 a \u2264 b \u228e b \u2264 a\ntotal-\u2264 zero b = inj\u2081 z\u2264n\ntotal-\u2264 (suc a) zero = inj\u2082 z\u2264n\ntotal-\u2264 (suc a) (suc b) with total-\u2264 a b\n... | inj\u2081 p = inj\u2081 (s\u2264s p)\n... | inj\u2082 p = inj\u2082 (s\u2264s p)\n\na\u2261a\u2293b+a\u2238b : \u2200 a b \u2192 a \u2261 a \u2293 b + (a \u2238 b)\na\u2261a\u2293b+a\u2238b zero zero = \u2261.refl\na\u2261a\u2293b+a\u2238b zero (suc b) = \u2261.refl\na\u2261a\u2293b+a\u2238b (suc a) zero = \u2261.refl\na\u2261a\u2293b+a\u2238b (suc a) (suc b) rewrite \u2261.sym (a\u2261a\u2293b+a\u2238b a b) = \u2261.refl\n\n\u00acn\u2264x dist (x * y) (x * z) == x * dist y z))\n\ninfix 8 _^_\n_^_ : \u2115 \u2192 \u2115 \u2192 \u2115\nb ^ zero = 1\nb ^ suc n = b * b ^ n\n\n2^_ : \u2115 \u2192 \u2115\n2^ n = \u27e82^ n * 1 \u27e9\n\n2^-spec : \u2200 n \u2192 2^ n \u2261 2 ^ n\n2^-spec zero = \u2261.refl\n2^-spec (suc n) rewrite 2^-spec n | 2*-spec (2 ^ n) = \u2261.refl\n\n2^*-spec : \u2200 m n \u2192 2^\u27e8 m \u27e9* n \u2261 2 ^ m * n\n2^*-spec zero n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\n2^*-spec (suc m) n rewrite 2^*-spec m n\n | \u2115\u00b0.*-assoc 2 (2 ^ m) n\n | \u2115\u00b0.+-comm (2 ^ m * n) 0 = \u2261.refl\n\n1\u22642^ : \u2200 n \u2192 2^ n \u2265 1\n1+\u22642^ : \u2200 n \u2192 2^ n \u2265 1 + n\n1+\u22642^ zero = s\u2264s z\u2264n\n1+\u22642^ (suc n) = (1\u22642^ n) +-mono (1+\u22642^ n)\n\n1\u22642^ n = \u2115\u2264.trans (s\u2264s z\u2264n) (1+\u22642^ n)\n\n\u2264-steps\u2032 : \u2200 {x} y \u2192 x \u2264 x + y\n\u2264-steps\u2032 {x} y rewrite \u2115\u00b0.+-comm x y = \u2264-steps y \u2115\u2264.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Hyper_operator\n_\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\na \u2191\u27e8 suc n \u27e9 (suc b) = a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\na \u2191\u27e8 0 \u27e9 b = suc b\na \u2191\u27e8 1 \u27e9 0 = a\na \u2191\u27e8 2 \u27e9 0 = 0\na \u2191\u27e8 suc (suc (suc n)) \u27e9 0 = 1\n\nmodule \u2191-Props where\n \u2191-fold : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 b \u2261 fold (a \u2191\u27e8 suc n \u27e9 0) (_\u2191\u27e8_\u27e9_ a n) b\n \u2191-fold a n zero = \u2261.refl\n \u2191-fold a n (suc b) = \u2261.cong (_\u2191\u27e8_\u27e9_ a n) (\u2191-fold a n b)\n\n \u2191\u2080-+ : \u2200 a b \u2192 a \u2191\u27e8 0 \u27e9 b \u2261 1 + b\n \u2191\u2080-+ a b = \u2261.refl\n\n \u2191\u2081-+ : \u2200 a b \u2192 a \u2191\u27e8 1 \u27e9 b \u2261 b + a\n \u2191\u2081-+ a zero = \u2261.refl\n \u2191\u2081-+ a (suc b) = \u2261.cong suc (\u2191\u2081-+ a b)\n\n \u2191\u2082-* : \u2200 a b \u2192 a \u2191\u27e8 2 \u27e9 b \u2261 b * a\n \u2191\u2082-* a zero = \u2261.refl\n \u2191\u2082-* a (suc b) rewrite \u2191\u2082-* a b\n | \u2191\u2081-+ a (b * a)\n | \u2115\u00b0.+-comm (b * a) a\n = \u2261.refl\n\n -- fold 1 (_*_ a) b \u2261 a ^ b\n \u2191\u2083-^ : \u2200 a b \u2192 a \u2191\u27e8 3 \u27e9 b \u2261 a ^ b\n \u2191\u2083-^ a zero = \u2261.refl\n \u2191\u2083-^ a (suc b) rewrite \u2191\u2083-^ a b\n | \u2191\u2082-* a (a ^ b)\n | \u2115\u00b0.*-comm (a ^ b) a\n = \u2261.refl\n\n _\u2191\u27e8_\u27e90 : \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u27e8 0 \u27e90 = 1\n a \u2191\u27e8 1 \u27e90 = a\n a \u2191\u27e8 2 \u27e90 = 0\n a \u2191\u27e8 suc (suc (suc n)) \u27e90 = 1\n\n _`\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _`\u2191\u27e8_\u27e9_ a 0 = suc\n _`\u2191\u27e8_\u27e9_ a (suc n) = fold (a \u2191\u27e8 suc n \u27e90) (_`\u2191\u27e8_\u27e9_ a n)\n\n \u2191-ind-rule : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 (suc b) \u2261 a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\n \u2191-ind-rule a n b = \u2261.refl\n\n _\u2191\u2032\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u2032\u27e8 0 \u27e9 b = suc b\n a \u2191\u2032\u27e8 1 \u27e9 b = a + b\n a \u2191\u2032\u27e8 2 \u27e9 b = a * b\n a \u2191\u2032\u27e8 3 \u27e9 b = a ^ b\n a \u2191\u2032\u27e8 suc (suc (suc (suc n))) \u27e9 b = a \u2191\u27e8 4 + n \u27e9 b\n\n \u2191-\u2191\u2032 : \u2200 a n b \u2192 a \u2191\u27e8 n \u27e9 b \u2261 a \u2191\u2032\u27e8 n \u27e9 b\n \u2191-\u2191\u2032 a 0 b = \u2261.refl\n \u2191-\u2191\u2032 a 1 b = \u2261.trans (\u2191\u2081-+ a b) (\u2115\u00b0.+-comm b a)\n \u2191-\u2191\u2032 a 2 b = \u2261.trans (\u2191\u2082-* a b) (\u2115\u00b0.*-comm b a)\n \u2191-\u2191\u2032 a 3 b = \u2191\u2083-^ a b\n \u2191-\u2191\u2032 a (suc (suc (suc (suc _)))) b = \u2261.refl\n\n _\u21912+\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _\u21912+\u27e8_\u27e9_ a = fold (_*_ a) (fold 1)\n\nmodule InflModule where\n -- Inflationary functions\n Infl< : (f : \u2115 \u2192 \u2115) \u2192 \u2605\n Infl< f = \u2200 {x} \u2192 x < f x\n\n Infl : (f : \u2115 \u2192 \u2115) \u2192 \u2605\n Infl f = Infl< (suc \u2218 f)\n\n InflT< : (f : Endo (Endo \u2115)) \u2192 \u2605\n InflT< f = \u2200 {g} \u2192 Infl< g \u2192 Infl< (f g)\n\n{-\n\u2200 {x} \u2192 x \u2264 f x\n\n\n 1 + x \u2264 1 + f x\n x < 1 + f x\n x < (suc \u2218 f) x\n Infl< (suc \u2218 f)\n\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n\nInfl< f \u2192 Infl f\nInfl< f \u2192 Infl< (suc \u2218 f)\n-}\n\n id-infl : Infl id\n id-infl = \u2115\u2264.refl\n\n \u2218-infl< : \u2200 {f g} \u2192 Infl< f \u2192 Infl< g \u2192 Infl< (f \u2218 g)\n \u2218-infl< inflf< inflg< = <-trans inflg< inflf<\n\n suc-infl< : Infl< suc\n suc-infl< = \u2115\u2264.refl\n\n suc-infl : Infl suc\n suc-infl = s\u2264s (\u2264-step \u2115\u2264.refl)\n\n nest-infl : \u2200 f (finfl : Infl f) n \u2192 Infl (nest n f)\n nest-infl f _ zero = \u2115\u2264.refl\n nest-infl f finfl (suc n) = \u2115\u2264.trans (nest-infl f finfl n) finfl\n\n nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n nest-infl< f finfl< zero = finfl<\n nest-infl< f finfl< (suc n) = <-trans (nest-infl< f finfl< n) finfl< \n\n-- See Function.NP.nest-Properties for more properties\nmodule fold-Props where\n\n fold-suc : \u2200 m n \u2192 fold m suc n \u2261 n + m\n fold-suc m zero = \u2261.refl\n fold-suc m (suc n) rewrite fold-suc m n = \u2261.refl\n\n fold-+ : \u2200 m n \u2192 fold 0 (_+_ m) n \u2261 n * m\n fold-+ m zero = \u2261.refl\n fold-+ m (suc n) rewrite fold-+ m n = \u2261.refl\n\n fold-* : \u2200 m n \u2192 fold 1 (_*_ m) n \u2261 m ^ n\n fold-* m zero = \u2261.refl\n fold-* m (suc n) rewrite fold-* m n = \u2261.refl\n\n{- TODO\n fold-^ : \u2200 m n \u2192 fold 1 (flip _^_ m) n \u2261 m \u2191\u27e8 4 \u27e9 n\n fold-^ m zero = \u2261.refl\n fold-^ m (suc n) rewrite fold-^ m n = ?\n-}\n\n cong-fold : \u2200 {A : \u2605} {f g : Endo A} (f\u2257g : f \u2257 g) {z} \u2192 fold z f \u2257 fold z g\n cong-fold eq zero = \u2261.refl\n cong-fold eq {z} (suc x) rewrite cong-fold eq {z} x = eq _\n\n private\n open \u2191-Props\n module Alt\u21912 where\n alt : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n alt a 0 = \u03bb b \u2192 a \u2191\u27e8 2 \u27e9 b\n alt a (suc n) = fold 1 (alt a n)\n\n alt-ok : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 alt a n\n alt-ok a zero = \u2261.sym \u2218 \u2191-\u2191\u2032 a 2\n alt-ok a (suc n) = cong-fold (alt-ok a n)\n\n{- TODO\n alt' : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 _\u2191\u27e8_\u27e9_ a (2 + n)\n alt' a zero b = \u2261.sym (\u2191-\u2191\u2032 a 2 _)\n alt' a (suc n) zero = \u2261.refl\n alt' a (suc n) (suc x) rewrite alt' a n (_\u21912+\u27e8_\u27e9_ a (suc n) x)\n = \u2261.cong (_\u2191\u27e8_\u27e9_ a (2 + n)) { fold 1 (fold (_*_ a) (fold 1) n) x} {a \u2191\u27e8 suc (suc (suc n)) \u27e9 x} (alt' a (suc n) x)\n-}\n\n open InflModule\n{-\n fold1-infl : \u2200 f \u2192 Infl f \u2192 Infl (fold 1 f)\n fold1-infl f finfl {zero} = {!z\u2264n!}\n fold1-infl f finfl {suc x} =\n x <\u27e8 {!s\u2264s (fold1-infl f finfl {x})!} \u27e9\n fold 1 f x <\u27e8 {!!} \u27e9\n {- f (fold 1 f x) \u2264\u27e8 finfl \u27e9\n f (fold 1 f x) \u2261\u27e8 \u2261.refl \u27e9 -}\n fold 1 f (1 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 f \u2218 suc)\n fold1+1-infl< f finfl< {zero} = finfl<\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + f (fold 1 f x) \u2264\u27e8 finfl< {f (fold 1 f x)} \u27e9\n f (suc (f (fold 1 f x))) \u2264\u27e8 {!!} \u27e9\n fold 1 f (2 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 (f \u2218 suc))\n fold1+1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 suc) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 suc) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+-infl< : \u2200 k f \u2192 Infl< (f \u2218\u2032 _+_ k) \u2192 Infl< (fold 1 (f \u2218\u2032 _+_ k))\n fold1+-infl< k f finfl< {zero} = s\u2264s z\u2264n\n fold1+-infl< k f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-infl< k f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 _+_ k) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 _+_ k) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n \n{-\n fold1+-infl< : \u2200 f \u2192 Infl+2< f \u2192 Infl+2< (fold 1 f)\n fold1+-infl< f finfl< {zero} _ = \u2115\u2264.refl\n fold1+-infl< f finfl< {suc zero} _ = {!!}\n fold1+-infl< f finfl< {suc (suc zero)} _ = {!!}\n fold1+-infl< f finfl< {suc (suc (suc x))} x>1 =\n 4 + x \u2264\u27e8 s\u2264s (fold1+-infl< f finfl< {suc (suc x)} (m\u2264m+n 2 x)) \u27e9\n 1 + fold 1 f (2 + x) \u2264\u27e8 finfl< {!!} \u27e9\n fold 1 f (3 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 f x \u2264\u27e8 finfl< \u27e9\n fold 1 f (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\nmodule ^-Props where\n open \u2261-Reasoning\n\n ^1 : \u2200 n \u2192 n ^ 1 \u2261 n\n ^1 zero = \u2261.refl\n ^1 (suc n) = \u2261.cong suc (proj\u2082 \u2115\u00b0.*-identity n)\n\n ^-+ : \u2200 b m n \u2192 b ^ (m + n) \u2261 b ^ m * b ^ n\n ^-+ b zero n = \u2261.sym (proj\u2082 \u2115\u00b0.+-identity (b ^ n))\n ^-+ b (suc m) n rewrite ^-+ b m n = \u2261.sym (\u2115\u00b0.*-assoc b (b ^ m) (b ^ n))\n\n ^-* : \u2200 b m n \u2192 b ^ (m * n) \u2261 (b ^ n) ^ m\n ^-* b zero n = \u2261.refl\n ^-* b (suc m) n = b ^ (n + m * n)\n \u2261\u27e8 ^-+ b n (m * n) \u27e9\n b ^ n * b ^ (m * n)\n \u2261\u27e8 \u2261.cong (_*_ (b ^ n)) (^-* b m n) \u27e9\n b ^ n * (b ^ n) ^ m \u220e\n\nack : \u2115 \u2192 \u2115 \u2192 \u2115\nack zero n = suc n\nack (suc m) zero = ack m (suc zero)\nack (suc m) (suc n) = ack m (ack (suc m) n)\n\n\u2264\u21d2\u2203 : \u2200 {m n} \u2192 m \u2264 n \u2192 \u2203 \u03bb k \u2192 m + k \u2261 n\n\u2264\u21d2\u2203 z\u2264n = _ , \u2261.refl\n\u2264\u21d2\u2203 (s\u2264s pf) = _ , \u2261.cong suc (proj\u2082 (\u2264\u21d2\u2203 pf))\n\nmodule ack-Props where\n lem\u2238 : \u2200 {m n} \u2192 m \u2264 n \u2192 m + (n \u2238 m) \u2261 n\n lem\u2238 z\u2264n = \u2261.refl\n lem\u2238 (s\u2264s {m} {n} m\u2264n) = \u2261.cong suc (lem\u2238 m\u2264n)\n\n -- n >= m \u2192 m + (n \u2238 m) \u2261 n\n -- n >= m \u2192 \u2203 k \u2192 n \u2261 m + k\n -- \u2203 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 m + ((m + k) \u2238 m) \u2261 m + k\n -- \u2200 k \u2192 m + k \u2261 m + k\n\n -- 3 \u2264 x \u2192 P x \u2192 \u2203 \u03bb k \u2192 P (3 + k)\n\n open InflModule\n\n fold1+-inflT< : \u2200 {z} \u2192 InflT< (fold (suc z))\n fold1+-inflT< _ {zero} = s\u2264s z\u2264n\n fold1+-inflT< {z} {f} finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-inflT< {z} {f} finfl< {x}) \u27e9\n 1 + fold (suc z) f x \u2264\u27e8 finfl< \u27e9\n f (fold (suc z) f x) \u2261\u27e8 \u2261.refl \u27e9\n fold (suc z) f (1 + x)\n \u220e\n where open \u2264-Reasoning\n\n Mon : (f : \u2115 \u2192 \u2115) \u2192 \u2605\n Mon f = \u2200 {x y} \u2192 x \u2264 y \u2192 f x \u2264 f y\n open InflModule\n\n\n \u2115-ind : \u2200 (P : \u2115 \u2192 \u2605) \u2192 P zero \u2192 (\u2200 {n} \u2192 P n \u2192 P (suc n)) \u2192 \u2200 {n} \u2192 P n\n \u2115-ind P P0 PS {zero} = P0\n \u2115-ind P P0 PS {suc n} = PS (\u2115-ind P P0 PS)\n\n open fold-Props\n fold-infl< : \u2200 {f g} \u2192 Infl< f \u2192 InflT< g \u2192 \u2200 {n} \u2192 Infl< (fold f g n)\n fold-infl< {f} {g} inflf< inflTg< {n} = \u2115-ind (Infl< \u2218 fold f g) inflf< inflTg< {n}\n\n fold-mon : \u2200 {f} (fmon : Mon f) (finfl< : Infl< f) {z} \u2192 Mon (fold z f)\n fold-mon fmon finfl< {_} {y = 0} z\u2264n = \u2115\u2264.refl\n fold-mon {f} fmon finfl< {z} {y = suc n} z\u2264n = z \u2264\u27e8 \u2264-step \u2115\u2264.refl \u27e9\n suc z \u2264\u27e8 nest-infl< f finfl< n {z} \u27e9\n fold z f (suc n) \u2261\u27e8 \u2261.refl \u27e9\n fold z f (suc n) \u220e\n where open \u2264-Reasoning\n fold-mon fmon finfl< (s\u2264s m\u2264n) = fmon (fold-mon fmon finfl< m\u2264n)\n\n MonT : Endo (Endo \u2115) \u2192 \u2605\n MonT g = \u2200 {f} \u2192 Mon f \u2192 Infl< f \u2192 Mon (g f)\n\n fold-mon' : \u2200 {f g} \u2192 Mon f \u2192 Infl< f \u2192 MonT g \u2192 InflT< g \u2192 \u2200 {n} \u2192 Mon (fold f g n)\n fold-mon' {f} {g} mon-f infl-f mont-g inflTg< {n} = go n where\n go : \u2200 n \u2192 Mon (fold f g n)\n go zero = mon-f\n go (suc n) = mont-g (go n) (fold-infl< infl-f inflTg< {n})\n\n\n -- +-fold : \u2200 a b \u2192 a + b \u2261 fold a suc b\n -- *-fold : \u2200 a b \u2192 a * b \u2261 fold 0 (_+_ a) b\n\n 1\u22641+a^b : \u2200 a b \u2192 1 \u2264 (1 + a) ^ b\n 1\u22641+a^b a zero = s\u2264s z\u2264n\n 1\u22641+a^b a (suc b) = 1\u22641+a^b a b +-mono z\u2264n\n\n 1+a^-mon : \u2200 {a} \u2192 Mon (_^_ (1 + a))\n 1+a^-mon {a} (z\u2264n {b}) = 1\u22641+a^b a b\n 1+a^-mon {a} (s\u2264s {m} {n} m\u2264n)\n = (1 + a) ^ (1 + m) \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ m + a * (1 + a) ^ m \u2264\u27e8 (1+a^-mon m\u2264n) +-mono ((a \u220e) *-mono (1+a^-mon m\u2264n)) \u27e9\n (1 + a) ^ n + a * (1 + a) ^ n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ (1 + n) \u220e\n where open \u2264-Reasoning\n\n lem2^3 : \u2200 n \u2192 2 ^ 3 \u2264 2 ^ (3 + n)\n lem2^3 n = 1+a^-mon {1} {3} {3 + n} (s\u2264s (s\u2264s (s\u2264s z\u2264n)))\n\n lem2221 : \u2200 m \u2192 2 \u2261 2 \u2191\u27e8 2 + m \u27e9 1\n lem2221 zero = \u2261.refl\n lem2221 (suc m) = lem2221 m\n\n lem4212 : \u2200 m \u2192 4 \u2261 2 \u2191\u27e8 1 + m \u27e9 2\n lem4212 zero = \u2261.refl\n lem4212 (suc m) = 4 \u2261\u27e8 lem4212 m \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 2 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 (1 + m)) (lem2221 m) \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 (2 \u2191\u27e8 2 + m \u27e9 1) \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 2 + m \u27e9 2 \u220e\n where open \u2261-Reasoning\n\n 2* = _*_ 2\n\n -- fold 2* n 1 \u2261 2 ^ n\n\n -- nest-* : \u2200 m n \u2192 nest (m * n) f \u2257 nest m (nest n f)\n\n nest-2* : \u2200 m n \u2192 nest (m * n) (fold 1) 2* \u2261 nest m (nest n (fold 1)) 2*\n nest-2* m n = nest-Properties.nest-* (fold 1) m n 2*\n\n{-\n lem3\u2264 : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 2\n lem3\u2264 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264 (suc n) = \n 3 \u2264\u27e8 {!!} \u27e9\n fold 2* f1 (1 + n) 2 \u220e\n where open \u2264-Reasoning\n f1 = fold 1\n\n lem3\u2264' : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 3\n lem3\u2264' 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264' (suc n) = \n 3 \u2264\u27e8 lem3\u2264 n \u27e9\n fold 2* (fold 1) n 2 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) n (fold 2* (fold 1) 3 1) \u2261\u27e8 {!!} \u27e9\n{- \u2261\u27e8 nest-Properties.nest-* {!2*!} {!!} {!!} {!!} \u27e9\n fold 2* (fold 1) (3 * n) 1 \u2261\u27e8 {!!} \u27e9\n-}\n fold 1 (fold 2* (fold 1) n) 3 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) (1 + n) 3 \u220e\n where open \u2264-Reasoning\n-}\n---\n\n{-\nfold 2* (fold 1) (3 * n) 1 \u2264\n\nfold 2* (fold 1) n (fold 2* (fold 1) n (fold 2* (fold 1) n 1)) \u2264\n\nfold 1 (fold 2* (fold 1) n) 3 \u2264\nfold 2* (fold 1) (1 + n) 3\n-}\n\n{-\n open \u2191-Props\n\n -*\u27e81+n\u27e9-infl : \u2200 n \u2192 Infl (_*_ (1 + n))\n -*\u27e81+n\u27e9-infl n = {!!}\n\n lem121 : \u2200 a b \u2192 b < (1 + a) \u2191\u27e8 2 \u27e9 (1 + b)\n lem121 a b rewrite \u2191\u2082-* (1 + a) (1 + b)\n = 1 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (1 + a) * (1 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (1 + a) (1 + b) \u27e9\n (1 + b) * (1 + a)\n \u220e\n where open \u2264-Reasoning\n\n -- \u2200 a b \u2192 b < (2 + a) * b\n lem222 : \u2200 a b \u2192 b < (2 + a) \u2191\u27e8 2 \u27e9 (2 + b)\n lem222 a b rewrite \u2191\u2082-* (2 + a) (2 + b)\n = 1 + b\n \u2264\u27e8 suc-infl \u27e9\n 2 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (2 + a) * (2 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (2 + a) (2 + b) \u27e9\n (2 + b) * (2 + a)\n \u220e\n where open \u2264-Reasoning\n-}\n{-\n open fold-Props\n\n module Foo a where\n f = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b\n f-lem : \u2200 n \u2192 f (suc n) \u2257 fold 1 (f n)\n f-lem n x = \u2261.refl\n\n f2 = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n f2-lem : \u2200 n \u2192 f2 (suc n) \u2257 fold 1 (f2 n)\n f2-lem n x = {!!}\n\n -- b < (1 + a) * (1 + b)\n\n -- nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n infl3< : \u2200 a n \u2192 Infl< (\u03bb b \u2192 (1 + a) \u21912+\u27e8 n \u27e9 (1 + b))\n infl3< a zero {b} = lem121 a b\n infl3< a (suc n) {b}\n = 1 + b\n -- fold1-infl< : \u2200 f \u2192 Infl< f \u2192 \u2200 x \u2192 x < fold 1 f x\n \u2264\u27e8 {!fold1f2-infl< {_}!} \u27e9\n fold 1 f1 b\n \u2264\u27e8 {!!} \u27e9\n{-\n \u2264\u27e8 fold1-infl< (f 0) (\u03bb {x} \u2192 {!infl3< a n {x}!}) {{!!}} \u27e9\n {!!}\n \u2264\u27e8 {!!} \u27e9\n-}\n{-\n 1 + b\n (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n \u2264\u27e8 {!nest-infl< (\u03bb b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b) ? b {(2 + a) \u21912+\u27e8 n \u27e9 (2 + b)}!} \u27e9\n fold (f 0 1) (f 0) (1 + b)\n \u2264\u27e8 {!\u2115\u2264.reflexive (\u2261.cong (\u03bb g \u2192 g 1) (nest-Properties.nest-+ (f 0) 1 (1 + b)))!} \u27e9\n-}\n fold 1 (f 0) (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n f 1 (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) \u21912+\u27e8 suc n \u27e9 (1 + b)\n \u220e\n\n where\n f = \u03bb k b \u2192 (1 + a) \u21912+\u27e8 k + n \u27e9 b\n f1 = f 0 \u2218 suc\n\n -- fold1f2-infl< : Infl< (fold 1 f2)\n -- fold1f2-infl< = fold1-infl< f2 {!(\u03bb b \u2192 infl3< a n b ?)!}\n\n fold1f1-infl<' : Infl< (fold 1 f1)\n fold1f1-infl<' = {!fold1+1-infl< (f 0) {!infl3< a n!}!}\n lem : \u2200 x \u2192 f 0 (1 + x) < f 0 (f 0 x)\n lem x = {!!}\n postulate\n finfl : Infl< (f 0)\n open \u2264-Reasoning\n-}\n\n_\u21d1\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\na \u21d1\u27e8 n , k \u27e9 b = fold k f n\n module M\u21d1 where \n f : \u2115 \u2192 \u2115\n f x = a \u2191\u27e8 2 + x \u27e9 b\n\nmodule \u21d1-Props where\n _\u21d1\u2032\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\n a \u21d1\u2032\u27e8 n\u2080 , k \u27e9 b = g n\u2080\n module M\u21d1\u2032 where\n h : \u2115 \u2192 \u2115\n h x = a \u2191\u27e8 2 + x \u27e9 b\n\n g : \u2115 \u2192 \u2115\n g 0 = k\n g (suc n) = h (g n)\n\n g-nest : \u2200 n \u2192 g n \u2261 h $\u27e8 n \u27e9 k\n g-nest zero = \u2261.refl\n g-nest (suc n) rewrite g-nest n = \u2261.refl\n\n \u21d1\u2080 : \u2200 a k b \u2192 a \u21d1\u27e8 0 , k \u27e9 b \u2261 k\n \u21d1\u2080 _ _ _ = \u2261.refl\n\n \u21d1\u2081 : \u2200 a k b \u2192 a \u21d1\u27e8 1 , k \u27e9 b \u2261 a \u2191\u27e8 2 + k \u27e9 b\n \u21d1\u2081 _ _ _ = \u2261.refl\n\n \u21d1-\u21d1\u2032 : \u2200 a n k b \u2192 a \u21d1\u27e8 n , k \u27e9 b \u2261 a \u21d1\u2032\u27e8 n , k \u27e9 b\n \u21d1-\u21d1\u2032 a n k b rewrite M\u21d1\u2032.g-nest a n k b n = \u2261.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Graham%27s_number\nGraham's-number : \u2115\nGraham's-number = 3 \u21d1\u27e8 64 , 4 \u27e9 3\n -- = (f\u2218\u2076\u2074\u2026\u2218f)4\n -- where f x = 3 \u2191\u27e8 2 + x \u27e9 3\n\n{-\nmodule GeneralisedArithmetic {a} {A : Set a} (0# : A) (1+ : A \u2192 A) where\n\n 1# : A\n 1# = 1+ 0#\n\n open Data.Nat.GeneralisedArithmetic 0# 1+ public\n\n exp : (* : A \u2192 A \u2192 A) \u2192 (\u2115 \u2192 A \u2192 A)\n exp _*_ n b = fold 1# (\u03bb s \u2192 b * s) n\n-}\n -- hyperop a n b = fold exp\n\nmodule == where\n _\u2248_ : (m n : \u2115) \u2192 \u2605\n m \u2248 n = T (m == n)\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {zero} {zero} _ = id\n subst P {suc _} {suc _} p = subst (P \u2218 suc) p\n subst _ {zero} {suc _} ()\n subst _ {suc _} {zero} ()\n\n sound : \u2200 m n \u2192 T (m == n) \u2192 m \u2261 n\n sound m n p = subst (_\u2261_ m) p \u2261.refl\n\n refl : Reflexive _\u2248_\n refl {zero} = _\n refl {suc n} = refl {n}\n\n sym : Symmetric _\u2248_\n sym {m} {n} eq rewrite sound m n eq = refl {n}\n\n trans : Transitive _\u2248_\n trans {m} {n} {o} m\u2248n n\u2248o rewrite sound m n m\u2248n | sound n o n\u2248o = refl {o}\n\n setoid : Setoid _ _\n setoid = record { Carrier = \u2115; _\u2248_ = _\u2248_\n ; isEquivalence =\n record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} } }\n\n open Setoid setoid public hiding (refl; sym; trans; _\u2248_)\n\n{-\ndata _`\u2264?`_\u219d_ : (m n : \u2115) \u2192 Dec (m \u2264 n) \u2192 \u2605 where\n z\u2264?n : \u2200 {n} \u2192 zero `\u2264?` n \u219d yes z\u2264n\n s\u2264?z : \u2200 {m} \u2192 suc m `\u2264?` zero \u219d no \u03bb()\n s\u2264?s-yes : \u2200 {m n m\u2264n} \u2192 m `\u2264?` n \u219d yes m\u2264n \u2192 suc m `\u2264?` suc n \u219d yes (s\u2264s m\u2264n)\n s\u2264?s-no : \u2200 {m n m\u2270n} \u2192 m `\u2264?` n \u219d no m\u2270n \u2192 suc m `\u2264?` suc n \u219d no (m\u2270n \u2218 \u2264-pred)\n\n`\u2264?`-complete : \u2200 m n \u2192 m `\u2264?` n \u219d (m \u2264? n)\n`\u2264?`-complete zero n = z\u2264?n\n`\u2264?`-complete (suc n) zero = {!s\u2264?z!}\n`\u2264?`-complete (suc m) (suc n) with m \u2264? n | `\u2264?`-complete m n\n... | yes q | r = s\u2264?s-yes r\n... | no q | r = s\u2264?s-no {!!}\n-}\n\n_<=_ : (x y : \u2115) \u2192 Bool\nzero <= _ = true\nsuc _ <= zero = false\nsuc m <= suc n = m <= n\n\nmodule <= where\n \u211b : \u2115 \u2192 \u2115 \u2192 \u2605\n \u211b x y = T (x <= y)\n\n sound : \u2200 m n \u2192 \u211b m n \u2192 m \u2264 n\n sound zero _ _ = z\u2264n\n sound (suc m) (suc n) p = s\u2264s (sound m n p)\n sound (suc m) zero ()\n\n complete : \u2200 {m n} \u2192 m \u2264 n \u2192 \u211b m n\n complete z\u2264n = _\n complete (s\u2264s m\u2264n) = complete m\u2264n\n\n isTotalOrder : IsTotalOrder _\u2261_ \u211b\n isTotalOrder = record { isPartialOrder = isPartialOrder; total = \u03bb x y \u2192 \u228e-map complete complete (\u2115\u2264.total x y) }\n where\n reflexive : \u2200 {i j} \u2192 i \u2261 j \u2192 \u211b i j\n reflexive {i} \u2261.refl = complete (\u2115\u2264.refl {i})\n trans : Transitive \u211b\n trans {x} {y} {z} p q = complete (\u2115\u2264.trans (sound x y p) (sound y z q))\n isPreorder : IsPreorder _\u2261_ \u211b\n isPreorder = record { isEquivalence = \u2261.isEquivalence\n ; reflexive = reflexive\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n antisym : Antisymmetric _\u2261_ \u211b\n antisym {x} {y} p q = \u2115\u2264.antisym (sound x y p) (sound y x q)\n isPartialOrder : IsPartialOrder _\u2261_ \u211b\n isPartialOrder = record { isPreorder = isPreorder; antisym = antisym }\n\n open IsTotalOrder isTotalOrder public\n\n<=-steps\u2032 : \u2200 {x} y \u2192 T (x <= (x + y))\n<=-steps\u2032 {x} y = <=.complete (\u2264-steps\u2032 {x} y)\n\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 : \u2200 x y \u2192 suc x \u2238 y \u2264 suc (x \u2238 y)\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x zero = \u2115\u2264.refl\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 zero (suc y) rewrite 0\u2238n\u22610 y = z\u2264n\nsucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 (suc x) (suc y) = sucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x y\n\nx\u22642y\u2032\u2192x\u2238y\u2264y : \u2200 x y \u2192 x \u2264 2*\u2032 y \u2192 x \u2238 y \u2264 y\nx\u22642y\u2032\u2192x\u2238y\u2264y x zero p = p\nx\u22642y\u2032\u2192x\u2238y\u2264y zero (suc y) p = z\u2264n\nx\u22642y\u2032\u2192x\u2238y\u2264y (suc zero) (suc y) (s\u2264s p) rewrite 0\u2238n\u22610 y = z\u2264n\nx\u22642y\u2032\u2192x\u2238y\u2264y (suc (suc x)) (suc y) (s\u2264s (s\u2264s p))\n = \u2115\u2264.trans (sucx\u2238y\u2264suc\u27e8x\u2238y\u27e9 x y) (s\u2264s (x\u22642y\u2032\u2192x\u2238y\u2264y x y p))\n\nx<2y\u2032\u2192x\u2238y \u00aca \u00acb c = \u22a5-elim (p (\u2264-pred c))\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (Props.\u2264-steps 1 1+n\u2264m)\n\nnot<=\u2192< : \u2200 x y \u2192 T (not (x <= y)) \u2192 T (suc y <= x)\nnot<=\u2192< x y p = <=.complete (\u2270\u2192< x y (T'not'\u00ac p \u2218 <=.complete))\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"0ba3572a7a7523455c601da5c200dd85039dcced","subject":"Mention our claims about the Agda code (for #275).","message":"Mention our claims about the Agda code (for #275).\n\nOld-commit-hash: f20d237f6179fe5e79864e2e68d0203143182c6c\n","repos":"inc-lc\/ilc-agda","old_file":"README.agda","new_file":"README.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Machine-checked formalization of the theoretical results presented\n-- in the paper:\n--\n-- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann.\n-- A Theory of Changes for Higher-Order Languages:\n-- Incrementalizing \u03bb-Calculi by Static Differentiation.\n-- To appear at PLDI, ACM 2014.\n--\n-- We claim that this formalization\n--\n-- (1) proves every lemma and theorem in Sec. 2 and 3 of the paper,\n-- (2) and formally specifies the interface between our incrementalization\n-- framework and potential plugins.\n--\n-- The first claim is the main reason for a machine-checked\n-- proof: We want to be sure that we got the proofs right.\n--\n-- The second claim is about reusability and applicability: Only\n-- a clearly defined interface allows other researchers to\n-- provide plugins for our framework.\n------------------------------------------------------------------------\n\nmodule README where\n\n-- We know two good ways to read this code base. You can either\n-- use Emacs with agda2-mode to interact with the source files,\n-- or you can use a web browser to view the pretty-printed and\n-- hyperlinked source files. For Agda power users, we also\n-- include basic setup information for your own machine.\n\n\n-- IF YOU WANT TO USE A BROWSER\n-- ============================\n--\n-- Start with *HTML* version of this readme. On the AEC\n-- submission website (online or inside the VM), follow the link\n-- \"view the Agda code in their browser\" to the file\n-- agda\/README.html.\n--\n-- The source code is syntax highlighted and hyperlinked. You can\n-- click on module names to open the corresponding files, or you\n-- can click on identifiers to jump to their definition. In\n-- general, a Agda file with name `Foo\/Bar\/Baz.agda` contains a\n-- module `Foo.Bar.Baz` and is shown in an HTML file\n-- `Foo.Bar.Baz.html`.\n--\n-- Note that we also include the HTML files generated for our\n-- transitive dependencies from the Agda standard library. This\n-- allows you to follow hyperlinks to the Agda standard\n-- library. It is the default behavior of `agda --html` which we\n-- used to generate the HTML.\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE EMACS\n-- ========================\n--\n-- Open this file in Emacs with agda2-mode installed. See below\n-- for which Agda version you need to install. On the VM image,\n-- everything is setup for you and you can just open this file in\n-- Emacs.\n--\n-- C-c C-l Load code. Type checks and syntax highlights. Use\n-- this if the code is not syntax highlighted, or\n-- after you changed something that you want to type\n-- check.\n--\n-- M-. Jump to definition of identifier at the point.\n-- M-* Jump back.\n--\n-- Note that README.agda imports Everything.agda which imports\n-- every Agda file in our formalization. So if README.agda type\n-- checks successfully, everything does. If you want to type\n-- check everything from scratch, delete the *.agdai files to\n-- disable separate compilation. You can use \"find . -name\n-- '*.agdai' | xargs rm\" to do that.\n--\n-- More information on the Agda mode is available on the Agda wiki:\n--\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Docs.EmacsModeKeyCombinations\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE YOUR OWN SETUP\n-- =================================\n--\n-- To typecheck this formalization, you need to install the appropriate version\n-- of Agda, the Agda standard library (version 0.7), generate Everything.agda\n-- with the attached Haskell helper, and finally run Agda on this file.\n--\n-- Given a Unix-like environment (including Cygwin), running the .\/agdaCheck.sh\n-- script and following instructions given on output will eventually generate\n-- Everything.agda and proceed to type check everything on command line.\n--\n-- We use Agda HEAD from September 2013; Agda 2.3.2.1 might happen to work, but\n-- has some bugs with serialization of code using some recent syntactic sugar\n-- which we use (https:\/\/code.google.com\/p\/agda\/issues\/detail?id=756), so it\n-- might work or not. When it does not, removing Agda caches (.agdai files)\n-- appears to often help. You can use \"find . -name '*.agdai' | xargs rm\" to do\n-- that.\n--\n-- If you're not an Agda power user, it is probably easier to use\n-- the VM image or look at the pretty-printed and hyperlinked\n-- HTML files, see above.\n\n\n\n-- WHERE TO START READING?\n-- =======================\n--\n-- modules.pdf\n-- The graph of dependencies between Agda modules.\n-- Good if you want to get a broad overview.\n--\n-- README.agda\n-- This file. A coarse-grained introduction to the Agda\n-- formalization. Good if you want to begin at the beginning\n-- and understand the structure of our code.\n--\n-- PLDI14-List-of-Theorems.agda\n-- Pointers to the Agda formalizations of all theorems, lemmas\n-- or definitions in the PLDI paper. Good if you want to read\n-- the paper and the Agda code side by side.\n--\n-- Here is an import of this file, so you can jump to it\n-- directly (use M-. in Emacs or click the module name in the\n-- Browser):\n\nimport PLDI14-List-of-Theorems\n\n-- Everything.agda\n-- Imports every Agda module in our formalization. Good if you\n-- want to make sure you don't miss anything.\n--\n-- Again, here's is an import of this file so you can navigate\n-- there:\n\nimport Everything\n\n-- (This import is also important to ensure that if we typecheck\n-- README.agda, we also typecheck every other file of our\n-- formalization).\n\n\n\n-- THE AGDA CODE\n-- =============\n\nimport Postulate.Extensionality\n\nimport Base.Data.DependentList\n\n-- Variables and contexts\nimport Base.Syntax.Context\n\n-- Sets of variables\nimport Base.Syntax.Vars\n\nimport Base.Denotation.Notation\n\n-- Environments\nimport Base.Denotation.Environment\n\n-- Change contexts\nimport Base.Change.Context\n\n-- # Base, parametric proof.\n--\n-- This is for a parametric calculus where:\n-- types are parametric in base types\n-- terms are parametric in constants\n--\n--\n-- Modules are ordered and grouped according to what they represent.\n\n-- ## Definitions\n\nimport Parametric.Syntax.Type\nimport Parametric.Syntax.Term\n\nimport Parametric.Denotation.Value\nimport Parametric.Denotation.Evaluation\n\nimport Parametric.Change.Type\nimport Parametric.Change.Term\n\nimport Parametric.Change.Derive\n\nimport Parametric.Change.Value\nimport Parametric.Change.Evaluation\n\n-- ## Proofs\n\nimport Parametric.Change.Validity\nimport Parametric.Change.Specification\nimport Parametric.Change.Implementation\nimport Parametric.Change.Correctness\n\n-- # Nehemiah plugin\n--\n-- The structure is the same as the parametric proof (down to the\n-- order and the grouping of modules), except for the postulate module.\n\n-- Postulate an abstract data type for integer Bags.\nimport Postulate.Bag-Nehemiah\n\n-- ## Definitions\nimport Nehemiah.Syntax.Type\nimport Nehemiah.Syntax.Term\n\nimport Nehemiah.Denotation.Value\nimport Nehemiah.Denotation.Evaluation\n\nimport Nehemiah.Change.Term\nimport Nehemiah.Change.Type\n\nimport Nehemiah.Change.Derive\n\nimport Nehemiah.Change.Value\nimport Nehemiah.Change.Evaluation\n\n-- ## Proofs\n\nimport Nehemiah.Change.Validity\nimport Nehemiah.Change.Specification\nimport Nehemiah.Change.Implementation\nimport Nehemiah.Change.Correctness\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Machine-checked formalization of the theoretical results presented\n-- in the paper:\n--\n-- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann.\n-- A Theory of Changes for Higher-Order Languages:\n-- Incrementalizing \u03bb-Calculi by Static Differentiation.\n-- To appear at PLDI, ACM 2014.\n--\n------------------------------------------------------------------------\n\nmodule README where\n\n-- We know two good ways to read this code base. You can either\n-- use Emacs with agda2-mode to interact with the source files,\n-- or you can use a web browser to view the pretty-printed and\n-- hyperlinked source files. For Agda power users, we also\n-- include basic setup information for your own machine.\n\n\n-- IF YOU WANT TO USE A BROWSER\n-- ============================\n--\n-- Start with *HTML* version of this readme. On the AEC\n-- submission website (online or inside the VM), follow the link\n-- \"view the Agda code in their browser\" to the file\n-- agda\/README.html.\n--\n-- The source code is syntax highlighted and hyperlinked. You can\n-- click on module names to open the corresponding files, or you\n-- can click on identifiers to jump to their definition. In\n-- general, a Agda file with name `Foo\/Bar\/Baz.agda` contains a\n-- module `Foo.Bar.Baz` and is shown in an HTML file\n-- `Foo.Bar.Baz.html`.\n--\n-- Note that we also include the HTML files generated for our\n-- transitive dependencies from the Agda standard library. This\n-- allows you to follow hyperlinks to the Agda standard\n-- library. It is the default behavior of `agda --html` which we\n-- used to generate the HTML.\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE EMACS\n-- ========================\n--\n-- Open this file in Emacs with agda2-mode installed. See below\n-- for which Agda version you need to install. On the VM image,\n-- everything is setup for you and you can just open this file in\n-- Emacs.\n--\n-- C-c C-l Load code. Type checks and syntax highlights. Use\n-- this if the code is not syntax highlighted, or\n-- after you changed something that you want to type\n-- check.\n--\n-- M-. Jump to definition of identifier at the point.\n-- M-* Jump back.\n--\n-- Note that README.agda imports Everything.agda which imports\n-- every Agda file in our formalization. So if README.agda type\n-- checks successfully, everything does. If you want to type\n-- check everything from scratch, delete the *.agdai files to\n-- disable separate compilation. You can use \"find . -name\n-- '*.agdai' | xargs rm\" to do that.\n--\n-- More information on the Agda mode is available on the Agda wiki:\n--\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode\n-- http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=Docs.EmacsModeKeyCombinations\n--\n-- To get started, continue below on \"Where to start reading?\".\n\n\n\n-- IF YOU WANT TO USE YOUR OWN SETUP\n-- =================================\n--\n-- To typecheck this formalization, you need to install the appropriate version\n-- of Agda, the Agda standard library (version 0.7), generate Everything.agda\n-- with the attached Haskell helper, and finally run Agda on this file.\n--\n-- Given a Unix-like environment (including Cygwin), running the .\/agdaCheck.sh\n-- script and following instructions given on output will eventually generate\n-- Everything.agda and proceed to type check everything on command line.\n--\n-- We use Agda HEAD from September 2013; Agda 2.3.2.1 might happen to work, but\n-- has some bugs with serialization of code using some recent syntactic sugar\n-- which we use (https:\/\/code.google.com\/p\/agda\/issues\/detail?id=756), so it\n-- might work or not. When it does not, removing Agda caches (.agdai files)\n-- appears to often help. You can use \"find . -name '*.agdai' | xargs rm\" to do\n-- that.\n--\n-- If you're not an Agda power user, it is probably easier to use\n-- the VM image or look at the pretty-printed and hyperlinked\n-- HTML files, see above.\n\n\n\n-- WHERE TO START READING?\n-- =======================\n--\n-- modules.pdf\n-- The graph of dependencies between Agda modules.\n-- Good if you want to get a broad overview.\n--\n-- README.agda\n-- This file. A coarse-grained introduction to the Agda\n-- formalization. Good if you want to begin at the beginning\n-- and understand the structure of our code.\n--\n-- PLDI14-List-of-Theorems.agda\n-- Pointers to the Agda formalizations of all theorems, lemmas\n-- or definitions in the PLDI paper. Good if you want to read\n-- the paper and the Agda code side by side.\n--\n-- Here is an import of this file, so you can jump to it\n-- directly (use M-. in Emacs or click the module name in the\n-- Browser):\n\nimport PLDI14-List-of-Theorems\n\n-- Everything.agda\n-- Imports every Agda module in our formalization. Good if you\n-- want to make sure you don't miss anything.\n--\n-- Again, here's is an import of this file so you can navigate\n-- there:\n\nimport Everything\n\n-- (This import is also important to ensure that if we typecheck\n-- README.agda, we also typecheck every other file of our\n-- formalization).\n\n\n\n-- THE AGDA CODE\n-- =============\n\nimport Postulate.Extensionality\n\nimport Base.Data.DependentList\n\n-- Variables and contexts\nimport Base.Syntax.Context\n\n-- Sets of variables\nimport Base.Syntax.Vars\n\nimport Base.Denotation.Notation\n\n-- Environments\nimport Base.Denotation.Environment\n\n-- Change contexts\nimport Base.Change.Context\n\n-- # Base, parametric proof.\n--\n-- This is for a parametric calculus where:\n-- types are parametric in base types\n-- terms are parametric in constants\n--\n--\n-- Modules are ordered and grouped according to what they represent.\n\n-- ## Definitions\n\nimport Parametric.Syntax.Type\nimport Parametric.Syntax.Term\n\nimport Parametric.Denotation.Value\nimport Parametric.Denotation.Evaluation\n\nimport Parametric.Change.Type\nimport Parametric.Change.Term\n\nimport Parametric.Change.Derive\n\nimport Parametric.Change.Value\nimport Parametric.Change.Evaluation\n\n-- ## Proofs\n\nimport Parametric.Change.Validity\nimport Parametric.Change.Specification\nimport Parametric.Change.Implementation\nimport Parametric.Change.Correctness\n\n-- # Nehemiah plugin\n--\n-- The structure is the same as the parametric proof (down to the\n-- order and the grouping of modules), except for the postulate module.\n\n-- Postulate an abstract data type for integer Bags.\nimport Postulate.Bag-Nehemiah\n\n-- ## Definitions\nimport Nehemiah.Syntax.Type\nimport Nehemiah.Syntax.Term\n\nimport Nehemiah.Denotation.Value\nimport Nehemiah.Denotation.Evaluation\n\nimport Nehemiah.Change.Term\nimport Nehemiah.Change.Type\n\nimport Nehemiah.Change.Derive\n\nimport Nehemiah.Change.Value\nimport Nehemiah.Change.Evaluation\n\n-- ## Proofs\n\nimport Nehemiah.Change.Validity\nimport Nehemiah.Change.Specification\nimport Nehemiah.Change.Implementation\nimport Nehemiah.Change.Correctness\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"109b8cf60463ce4ef6a46602bede3b14a658aec6","subject":"IDesc model: paranoid alpha-renaming.","message":"IDesc model: paranoid alpha-renaming.\n","repos":"mietek\/epigram2,larrytheliquid\/pigit,mietek\/epigram2","old_file":"models\/IDesc.agda","new_file":"models\/IDesc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\nunlift : {l : Level}{A : Set l} -> Lifted A -> A\nunlift (lifter a) = a\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\npair : {i j : Level}{A : Set i}{B : A -> Set j} -> \n (x : A) (y : B x) -> Sigma {i = i}{j = j} A B\npair x y = x , y\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l m : Level}{A : Set l}{B : Set m}\n (f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l m n : Level}{A : Set l}{B : Set m}{C : Set n}\n (f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\ntrans : {l : Level}{A : Set l}{x y z : A} -> x == y -> y == z -> x == z\ntrans refl refl = refl\n\nproof-lift-unlift-eq : {l : Level}{A : Set l}(x : Lifted A) -> lifter (unlift x) == x\nproof-lift-unlift-eq (lifter a) = refl\n\npostulate \n reflFun : {l m : Level}{A : Set l}{B : Set m}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set (suc l)) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set (suc l)} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set (suc l)}(R : I -> IDesc {l = l} I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set (suc l)}(D : IDesc I)(X : I -> Set l) -> desc D X -> IDesc (Sigma I X)\nbox (var i) X x = var (i , x)\nbox (const _) X x = const Unit\nbox (prod D D') X (d , d') = prod (box D X d) (box D' X d')\nbox (sigma S T) X (a , b) = box (T a) X b\nbox (pi S T) X f = pi S (\\s -> box (T s) X (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set (suc l)}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set (suc l)}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set (suc l) -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\nIDescD : {l : Level}(I : Set (suc l)) -> IDesc {l = suc l} Unit\nIDescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : {l : Level}(I : Set (suc l)) -> Unit -> Set (suc l)\nIDescl0 {x} I = IMu {l = suc x} (\\_ -> IDescD {l = x} I)\n\nIDescl : {l : Level}(I : Set (suc l)) -> Set (suc l)\nIDescl I = IDescl0 I Void\n\nvarl : {l : Level}{I : Set (suc l)}(i : I) -> IDescl I\nvarl {x} i = con (lvar {l = suc x} , i) \n\nconstl : {l : Level}{I : Set (suc l)}(X : Set l) -> IDescl I\nconstl {x} X = con (lconst {l = suc x} , X)\n\nprodl : {l : Level}{I : Set (suc l)}(D D' : IDescl I) -> IDescl I\nprodl {x} D D' = con (lprod {l = suc x} , (D , D'))\n\n\npil : {l : Level}{I : Set (suc l)}(S : Set l)(T : S -> IDescl I) -> IDescl I\npil {x} S T = con (lpi {l = suc x} , pair {i = suc x}{j = suc x} S (\\s -> T (unlift s)))\n\nsigmal : {l : Level}{I : Set (suc l)}(S : Set l)(T : S -> IDescl I) -> IDescl I\nsigmal {x} S T = con (lsigma {l = suc x} , pair {i = suc x}{j = suc x} S (\\s -> T (unlift s)))\n \n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {l : Level}\n {I : Set (suc l)}\n (xs : desc (IDescD I) (IMu (\u03bb _ -> IDescD I)))\n (hs : desc (box (IDescD I) (IMu (\u03bb _ -> IDescD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S (\\s -> hs (lifter s) )\ncases ( lsigma , ( S , T ) ) hs = sigma S (\\s -> hs (lifter s))\n\nphi : {l : Level}{I : Set (suc l)} -> IDescl I -> IDesc I\nphi {x} {I} d = induction (\\_ -> IDescD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {l : Level}{I : Set (suc l)} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {l : Level}{I : Set (suc l)} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi {x} (pi S T) = cong (pi S) \n (reflFun (\\s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S)\n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n-- From embedding to embedding\n\nproof-psi-phi : {l : Level}(I : Set (suc l)) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi {x} I D = induction (\\ _ -> IDescD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> IDescD I)) -> Set (suc x)\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (IDescD I) (IDescl0 I))\n (hs : desc (box (IDescD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (\\T -> con (lpi {l = suc x} , ( S , T ) )) \n (trans (reflFun (\\ s -> psi (phi (T (lifter (unlift s)))))\n (\\ s -> psi (phi (T (s))))\n (\\s -> cong (\\ s -> psi (phi (T (s))))\n (proof-lift-unlift-eq s)))\n (reflFun (\\s -> psi (phi (T s))) \n T \n hs)) \n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (\\T -> con (lsigma {l = suc x} , ( S , T ) )) \n (trans (reflFun (\\ s \u2192 psi (phi (T (lifter (unlift s)))))\n (\\ s \u2192 psi (phi (T (s))))\n (\\s -> cong (\\ s -> psi (phi (T (s))))\n (proof-lift-unlift-eq s)))\n (reflFun (\\s -> psi (phi (T s))) \n T \n hs))","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\nunlift : {l : Level}{A : Set l} -> Lifted A -> A\nunlift (lifter a) = a\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\npair : {i j : Level}{A : Set i}{B : A -> Set j} -> \n (x : A) (y : B x) -> Sigma {i = i}{j = j} A B\npair x y = x , y\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l m : Level}{A : Set l}{B : Set m}\n (f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l m n : Level}{A : Set l}{B : Set m}{C : Set n}\n (f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\ntrans : {l : Level}{A : Set l}{x y z : A} -> x == y -> y == z -> x == z\ntrans refl refl = refl\n\nproof-lift-unlift-eq : {l : Level}{A : Set l}(x : Lifted A) -> lifter (unlift x) == x\nproof-lift-unlift-eq (lifter a) = refl\n\npostulate \n reflFun : {l m : Level}{A : Set l}{B : Set m}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set (suc l)) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set (suc l)} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set (suc l)}(R : I -> IDesc {l = l} I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set (suc l)}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const Unit\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set (suc l)}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set (suc l)}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set (suc l) -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\nIDescD : {l : Level}(I : Set (suc l)) -> IDesc {l = suc l} Unit\nIDescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : {l : Level}(I : Set (suc l)) -> Unit -> Set (suc l)\nIDescl0 {x} I = IMu {l = suc x} (\\_ -> IDescD {l = x} I)\n\nIDescl : {l : Level}(I : Set (suc l)) -> Set (suc l)\nIDescl I = IDescl0 I Void\n\nvarl : {l : Level}{I : Set (suc l)}(i : I) -> IDescl I\nvarl {x} i = con (lvar {l = suc x} , i) \n\nconstl : {l : Level}{I : Set (suc l)}(X : Set l) -> IDescl I\nconstl {x} X = con (lconst {l = suc x} , X)\n\nprodl : {l : Level}{I : Set (suc l)}(D D' : IDescl I) -> IDescl I\nprodl {x} D D' = con (lprod {l = suc x} , (D , D'))\n\n\npil : {l : Level}{I : Set (suc l)}(S : Set l)(T : S -> IDescl I) -> IDescl I\npil {x} S T = con (lpi {l = suc x} , pair {i = suc x}{j = suc x} S (\\s -> T (unlift s)))\n\nsigmal : {l : Level}{I : Set (suc l)}(S : Set l)(T : S -> IDescl I) -> IDescl I\nsigmal {x} S T = con (lsigma {l = suc x} , pair {i = suc x}{j = suc x} S (\\s -> T (unlift s)))\n \n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {l : Level}\n {I : Set (suc l)}\n (xs : desc (IDescD I) (IMu (\u03bb _ -> IDescD I)))\n (hs : desc (box (IDescD I) (IMu (\u03bb _ -> IDescD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S (\\s -> hs (lifter s) )\ncases ( lsigma , ( S , T ) ) hs = sigma S (\\s -> hs (lifter s))\n\nphi : {l : Level}{I : Set (suc l)} -> IDescl I -> IDesc I\nphi {x} {I} d = induction (\\_ -> IDescD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {l : Level}{I : Set (suc l)} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {l : Level}{I : Set (suc l)} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi {x} (pi S T) = cong (pi S) \n (reflFun (\\s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S)\n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n-- From embedding to embedding\n\nproof-psi-phi : {l : Level}(I : Set (suc l)) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi {x} I D = induction (\\ _ -> IDescD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> IDescD I)) -> Set (suc x)\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (IDescD I) (IDescl0 I))\n (hs : desc (box (IDescD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (\\T -> con (lpi {l = suc x} , ( S , T ) )) \n (trans (reflFun (\\ s -> psi (phi (T (lifter (unlift s)))))\n (\\ s -> psi (phi (T (s))))\n (\\s -> cong (\\ s -> psi (phi (T (s))))\n (proof-lift-unlift-eq s)))\n (reflFun (\\s -> psi (phi (T s))) \n T \n hs)) \n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (\\T -> con (lsigma {l = suc x} , ( S , T ) )) \n (trans (reflFun (\\ s \u2192 psi (phi (T (lifter (unlift s)))))\n (\\ s \u2192 psi (phi (T (s))))\n (\\s -> cong (\\ s -> psi (phi (T (s))))\n (proof-lift-unlift-eq s)))\n (reflFun (\\s -> psi (phi (T s))) \n T \n hs))","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"56113e6c31daaab6d85a46d0bc59c4b4d25cb6c9","subject":"tiny proof simplification","message":"tiny proof simplification\n\nOld-commit-hash: f98c7776e0c807aae905d00908b7eb27c17a0a99\n","repos":"inc-lc\/ilc-agda","old_file":"Natural\/Soundness.agda","new_file":"Natural\/Soundness.agda","new_contents":"module Natural.Soundness where\n\n-- SOUNDNESS of NATURAL SEMANTICS\n--\n-- This module proves consistency of the natural semantics with\n-- respect to the denotational semantics.\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Closures\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\nopen import Denotational.Closures\n\nopen import Natural.Lookup\nopen import Natural.Evaluation\n\n-- Syntactic lookup is consistent with semantic lookup.\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = \u2261-refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n-- Syntactic evaluation is consistent with semantic evaluation.\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = \u2261-refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) =\n \u2261-trans\n (\u2261-cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082))\n (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n\u2193-sound e-true = \u2261-refl \n\u2193-sound e-false = \u2261-refl \n\u2193-sound (if-true {\u03c1 = \u03c1} {c = c} \u2193\u2081 \u2193\u2082) with \u27e6 c \u27e7 \u27e6 \u03c1 \u27e7 | \u2193-sound \u2193\u2081\n\u2193-sound (if-true \u2193\u2081 \u2193\u2082) | true | refl = \u2193-sound \u2193\u2082\n\u2193-sound (if-true \u2193\u2081 \u2193\u2082) | false | ()\n\u2193-sound (if-false {\u03c1 = \u03c1} {c = c} \u2193\u2081 \u2193\u2082) with \u27e6 c \u27e7 \u27e6 \u03c1 \u27e7 | \u2193-sound \u2193\u2081\n\u2193-sound (if-false \u2193\u2081 \u2193\u2082) | false | refl = \u2193-sound \u2193\u2082\n\u2193-sound (if-false \u2193\u2081 \u2193\u2082) | true | ()\n","old_contents":"module Natural.Soundness where\n\n-- SOUNDNESS of NATURAL SEMANTICS\n--\n-- This module proves consistency of the natural semantics with\n-- respect to the denotational semantics.\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types\nopen import Syntactic.Contexts Type\nopen import Syntactic.Terms.Total\nopen import Syntactic.Closures\n\nopen import Denotational.Notation\nopen import Denotational.Values\nopen import Denotational.Environments Type \u27e6_\u27e7Type\nopen import Denotational.Evaluation.Total\nopen import Denotational.Equivalence\nopen import Denotational.Closures\n\nopen import Natural.Lookup\nopen import Natural.Evaluation\n\n-- Syntactic lookup is consistent with semantic lookup.\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = \u2261-refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n-- Syntactic evaluation is consistent with semantic evaluation.\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = \u2261-refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) =\n \u2261-trans\n (\u2261-cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082))\n (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n\u2193-sound e-true = \u2261-refl \n\u2193-sound e-false = \u2261-refl \n\u2193-sound (if-true {\u03c1 = \u03c1} {c = c} \u2193\u2081 \u2193\u2082) with \u27e6 c \u27e7 \u27e6 \u03c1 \u27e7 | \u2193-sound \u2193\u2081\n\u2193-sound (if-true \u2193\u2081 \u2193\u2082) | true | refl = \u2261-trans refl (\u2193-sound \u2193\u2082)\n\u2193-sound (if-true \u2193\u2081 \u2193\u2082) | false | ()\n\u2193-sound (if-false {\u03c1 = \u03c1} {c = c} \u2193\u2081 \u2193\u2082) with \u27e6 c \u27e7 \u27e6 \u03c1 \u27e7 | \u2193-sound \u2193\u2081\n\u2193-sound (if-false \u2193\u2081 \u2193\u2082) | false | refl = \u2261-trans refl (\u2193-sound \u2193\u2082)\n\u2193-sound (if-false \u2193\u2081 \u2193\u2082) | true | ()\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"cb0be83f848e2a7872c46ba210d3df2d2f1d8263","subject":"Bits: remove\/comment the broken stuff","message":"Bits: remove\/comment the broken stuff\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative.NP\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties.NP\nimport Data.List.NP as L\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0b \u2237 1b \u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u2219_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u2219 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u2219 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u2219-comm : \u2200 {m} (x y : A m) \u2192 x \u2219 y \u2261 y \u2219 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u2219-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nmodule OperationSyntax where\n infixr 1 _`\u204f_\n data Op : Set where\n `id `0\u21941 `not : Op\n `tl : Op \u2192 Op\n `if0 : Op \u2192 Op\n _`\u204f_ : Op \u2192 Op \u2192 Op\n\n {-\n id \u03b5 id\n 0\u21941 swp-inners interchange\n not swap comm\n if0 first ...\n if1 second ...\n -}\n\n infixr 9 _\u2219_\n _\u2219_ : Op \u2192 \u2200 {n} \u2192 Endo (Bits n)\n `id \u2219 xs = xs\n `0\u21941 \u2219 xs = 0\u21941 xs\n `not \u2219 [] = []\n `not \u2219 (x \u2237 xs) = not x \u2237 xs\n `tl f \u2219 [] = []\n `tl f \u2219 (x \u2237 xs) = x \u2237 f \u2219 xs\n `if0 f \u2219 [] = []\n `if0 f \u2219 (false \u2237 xs) = false \u2237 f \u2219 xs\n `if0 f \u2219 (true \u2237 xs) = true \u2237 xs\n (f `\u204f g) \u2219 xs = g \u2219 f \u2219 xs\n\n `if1 : Op \u2192 Op\n `if1 f = `not `\u204f `if0 f `\u204f `not\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 right swap\n -- (a \u2219 d) \u2219 (c \u2219 b)\n swp-seconds : Op\n swp-seconds = `if1 `not `\u204f `0\u21941 `\u204f `if1 `not\n\n on-extremes : Op \u2192 Op\n on-extremes f = swp-seconds `\u204f `if0 f `\u204f swp-seconds\n\n on-firsts : Op \u2192 Op\n on-firsts f = `0\u21941 `\u204f `if0 f `\u204f `0\u21941\n\n 0\u21941\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Op\n 0\u21941\u2237 [] = `not\n 0\u21941\u2237 (true {-1-} \u2237 p) = on-extremes (0\u21941\u2237 p)\n 0\u21941\u2237 (false{-0-} \u2237 p) = on-firsts (0\u21941\u2237 p)\n\n 0\u2194_ : \u2200 {n} \u2192 Bits n \u2192 Op\n 0\u2194 [] = `id\n 0\u2194 (false{-0-} \u2237 p) = `if0 (0\u2194 p)\n 0\u2194 (true{-1-} \u2237 p) = 0\u21941\u2237 p\n\n \u27e80\u2194_\u27e9-sem : \u2200 {n} (p : Bits n) \u2192 Bits n \u2192 Bits n\n \u27e80\u2194 p \u27e9-sem xs = if 0\u207f == xs then p else if p == xs then 0\u207f else xs\n\n if\u2237 : \u2200 {n} a x (xs ys : Bits n) \u2192 (if a then (x \u2237 xs) else (x \u2237 ys)) \u2261 x \u2237 (if a then xs else ys)\n if\u2237 true x xs ys = refl\n if\u2237 false x xs ys = refl\n\n if-not\u2237 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (false \u2237 xs) else (true \u2237 ys)) \u2261 (not a) \u2237 (if a then xs else ys)\n if-not\u2237 true xs ys = refl\n if-not\u2237 false xs ys = refl\n\n if\u2237\u2032 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (true \u2237 xs) else (false \u2237 ys)) \u2261 a \u2237 (if a then xs else ys)\n if\u2237\u2032 true xs ys = refl\n if\u2237\u2032 false xs ys = refl\n\n \u27e80\u21941\u2237_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u21941\u2237 p \u2219 xs \u2261 \u27e80\u2194 (1\u2237 p) \u27e9-sem xs\n \u27e80\u21941\u2237_\u27e9-spec [] (true \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec [] (false \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 true \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 false \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n\n \u27e80\u2194_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u2194 p \u2219 xs \u2261 \u27e80\u2194 p \u27e9-sem xs\n \u27e80\u2194_\u27e9-spec [] [] = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (true \u2237 xs) = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (false \u2237 xs)\n rewrite \u27e80\u2194 ps \u27e9-spec xs\n | if\u2237 (ps == xs) 0b 0\u207f xs\n | if\u2237 (0\u207f == xs) 0b ps (if ps == xs then 0\u207f else xs)\n = refl\n \u27e80\u2194_\u27e9-spec (true \u2237 p) xs = \u27e80\u21941\u2237 p \u27e9-spec xs\n\n open PermutationSyntax using (Perm; `id; `0\u21941; `tl; _`\u204f_)\n module P = PermutationSemantics\n\n `\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Op\n `\u27e80\u21941+ zero \u27e9 = `0\u21941\n `\u27e80\u21941+ suc i \u27e9 = `0\u21941 `\u204f `tl `\u27e80\u21941+ i \u27e9 `\u204f `0\u21941\n\n `\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n `\u27e80\u21941+ zero \u27e9-spec xs = refl\n `\u27e80\u21941+ suc i \u27e9-spec (x \u2237 _ \u2237 xs) rewrite `\u27e80\u21941+ i \u27e9-spec (x \u2237 xs) = refl\n\n `\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Op\n `\u27e80\u2194 zero \u27e9 = `id\n `\u27e80\u2194 suc i \u27e9 = `\u27e80\u21941+ i \u27e9\n\n `\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n `\u27e80\u2194 zero \u27e9-spec xs = refl\n `\u27e80\u2194 suc i \u27e9-spec xs = `\u27e80\u21941+ i \u27e9-spec xs\n\n `\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Op\n `\u27e8 zero \u2194 j \u27e9 = `\u27e80\u2194 j \u27e9\n `\u27e8 i \u2194 zero \u27e9 = `\u27e80\u2194 i \u27e9\n `\u27e8 suc i \u2194 suc j \u27e9 = `tl `\u27e8 i \u2194 j \u27e9\n\n `\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) xs \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n `\u27e8_\u2194_\u27e9-spec zero j xs = `\u27e80\u2194 j \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) zero xs = `\u27e80\u21941+ i \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) (suc j) (x \u2237 xs) rewrite `\u27e8 i \u2194 j \u27e9-spec xs = refl\n\n `xor-head : Bit \u2192 Op\n `xor-head b = if b then `not else `id\n\n `xor-head-spec : \u2200 b {n} x (xs : Bits n) \u2192 `xor-head b \u2219 (x \u2237 xs) \u2261 (b xor x) \u2237 xs\n `xor-head-spec true x xs = refl\n `xor-head-spec false x xs = refl\n\n `\u27e8_\u2295\u27e9 : \u2200 {n} \u2192 Bits n \u2192 Op\n `\u27e8 [] \u2295\u27e9 = `id\n `\u27e8 b \u2237 xs \u2295\u27e9 = `xor-head b `\u204f `tl `\u27e8 xs \u2295\u27e9\n\n `\u27e8_\u2295\u27e9-spec : \u2200 {n} (xs ys : Bits n) \u2192 `\u27e8 xs \u2295\u27e9 \u2219 ys \u2261 xs \u2295 ys\n `\u27e8 [] \u2295\u27e9-spec [] = refl\n `\u27e8 true \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n `\u27e8 false \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) x \u2192 0\u21941 pad \u2295 0\u21941 x \u2261 0\u21941 (pad \u2295 x)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\nmodule PermutationSyntax-Props where\n open PermutationSyntax\n open PermutationSemantics\n open PermutationProperties\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) xs \u2192 0\u21941 pad \u2295 0\u21941 xs \u2261 0\u21941 (pad \u2295 xs)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\n {-\n -- \u229b-dist-\u2219 : \u2200 {n a} {A : Set a} (fs : Vec (A \u2192 A) n) \u03c0 xs \u2192 \u03c0 \u2219 fs \u229b \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (fs \u229b xs)\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 pad \u03c0 xs = \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs\n \u2261\u27e8 refl \u27e9\n vmap _xor_ (\u03c0 \u2219 pad) \u229b \u03c0 \u2219 xs\n \u2261\u27e8 {!!} \u27e9\n \u03c0 \u2219 vmap _xor_ pad \u229b \u03c0 \u2219 xs\n \u2261\u27e8 \u229b-dist-\u2219 _ (vmap _xor_ pad) \u03c0 xs \u27e9\n \u03c0 \u2219 (vmap _xor_ pad \u229b xs)\n \u2261\u27e8 refl \u27e9\n \u03c0 \u2219 (pad \u2295 xs)\n \u220e where open \u2261-Reasoning\n -- rans {!\u229b-dist-\u2219 (vmap _xor_ (op \u2219 pad)) op xs!} (\u229b-dist-\u2219 (vmap _xor_ pad) op xs)\n-}\nmodule SimpleSearch {a} {A : Set a} (_\u2219_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u2219_ public\n\n search-const\u03b5\u2261\u03b5 : \u2200 \u03b5 (\u03b5\u2219\u03b5 : \u03b5 \u2219 \u03b5 \u2261 \u03b5) n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-const\u03b5\u2261\u03b5 \u03b5 \u03b5\u2219\u03b5 = go\n where\n go : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n go zero = refl\n go (suc n) rewrite go n = \u03b5\u2219\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n module SearchInterchange (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u2219 f\u2081 x) \u2261 search f\u2080 \u2219 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u2219-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e\n where open \u2261-Reasoning\n\n search-0\u21941 : \u2200 {n} (f : Bits n \u2192 A) \u2192 search {n} (f \u2218 0\u21941) \u2261 search {n} f\n search-0\u21941 {zero} _ = refl\n search-0\u21941 {suc zero} _ = refl\n search-0\u21941 {suc (suc n)} _ = \u2219-interchange _ _ _ _\n\n module Op (\u2219-comm : Commutative _\u2261_ _\u2219_)\n (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n open SearchInterchange \u2219-interchange using (search-0\u21941)\n open OperationSyntax renaming (_\u2219_ to op)\n search-op : \u2200 {n} (f : Bits n \u2192 A) (g : Op) \u2192 search {n} (f \u2218 op g) \u2261 search {n} f\n search-op f `id = refl\n search-op f `0\u21941 = search-0\u21941 f\n search-op {zero} f `not = refl\n search-op {suc n} f `not = \u2219-comm _ _\n search-op {zero} f (`tl g) = refl\n search-op {suc n} f (`tl g) rewrite search-op (f \u2218 0\u2237_) g | search-op (f \u2218 1\u2237_) g = refl\n search-op {zero} f (`if0 g) = refl\n search-op {suc n} f (`if0 g) rewrite search-op (f \u2218 0\u2237_) g = refl\n search-op f (g `\u204f h) rewrite search-op (f \u2218 op h) g = search-op f h\n\nmodule Sum where\n open SimpleSearch _+_ using (module Comm; module SearchInterchange; search-const\u03b5\u2261\u03b5; module Op)\n open SimpleSearch _+_ public using () renaming (search to sum; search-\u2257 to sum-\u2257; searchBit to sumBit;\n search-\u2257\u2082 to sum-\u2257\u2082;\n search-+ to sum-+)\n open Comm \u2115\u00b0.+-comm public renaming (search-comm to sum-comm)\n open SearchInterchange +-interchange public renaming (\n search-dist to sum-dist;\n search-searchBit to sum-sumBit;\n search-search to sum-sum;\n search-swap to sum-swap)\n open Op \u2115\u00b0.+-comm +-interchange public renaming (search-op to sum-op)\n\n sum-const0\u22610 : \u2200 n \u2192 sum {n = n} (const 0) \u2261 0\n sum-const0\u22610 n = search-const\u03b5\u2261\u03b5 0 refl n\n\n sum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\n sum-const zero _ = refl\n sum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nmodule Count where\n open Sum\n open OperationSyntax renaming (_\u2219_ to op)\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n #\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n -- #-ext\n #-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n #-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n #-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n #-op : \u2200 {n} (f : Bits n \u2192 Bit) (g : Op) \u2192 #\u27e8 f \u2218 op g \u27e9 \u2261 #\u27e8 f \u27e9\n #-op f = sum-op (Bool.to\u2115 \u2218 f)\n\n #-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n #-\u2295 = #-comm\n\n #-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n #-const n b = sum-const n (Bool.to\u2115 b)\n\n #never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n #never\u22610 = sum-const0\u22610\n\n #always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n #always\u22612^ n = sum-const n 1\n\n #-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 Bit) \u2192 sum (\u03bb x \u2192 Bool.to\u2115 (f\u2080 x) + Bool.to\u2115 (f\u2081 x)) \u2261 #\u27e8 f\u2080 \u27e9 + #\u27e8 f\u2081 \u27e9\n #-dist f\u2080 f\u2081 = sum-dist (Bool.to\u2115 \u2218 f\u2080) (Bool.to\u2115 \u2218 f\u2081)\n\n #-+ : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n #\u27e8 f \u27e9 \u2261 sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9 )\n #-+ {m} {n} f = sum-+ {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-# : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9)\n \u2261 sum {n} (\u03bb ys \u2192 #\u27e8 (\u03bb (xs : Bits m) \u2192 f (xs ++ ys)) \u27e9)\n #-# {m} {n} f = sum-sum {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-swap : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192 #\u27e8 f \u2218 vswap n {m} \u27e9 \u2261 #\u27e8 f \u27e9\n #-swap {m} {n} f = sum-swap {m} {n} (Bool.to\u2115 \u2218 f)\n\n #\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n #\u27e8== [] \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n \u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n \u2257-cong-# f g f\u2257g = sum-\u2257 _ _ (cong Bool.to\u2115 \u2218 f\u2257g)\n\n -- #-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n -- #-+ f f0 f1 rewrite f0 | f1 = refl\n\n #-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-take-drop zero n f g with f []\n ... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n ... | false = #never\u22610 n\n #-take-drop (suc m) n f g\n rewrite \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x))\n | #-take-drop m n (f \u2218 0\u2237_) g\n | \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x))\n | #-take-drop m n (f \u2218 1\u2237_) g\n = sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9)\n\n #-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n #\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n #\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n #\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n ... | true | true | _ = s\u2264s z\u2264n\n ... | true | false | p = \u22a5-elim (p _)\n ... | false | _ | _ = z\u2264n\n #\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n #-\u2227-\u2228\u1d47 : \u2200 x y \u2192 Bool.to\u2115 (x \u2227 y) + Bool.to\u2115 (x \u2228 y) \u2261 Bool.to\u2115 x + Bool.to\u2115 y\n #-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (Bool.to\u2115 y) 1 = refl\n #-\u2227-\u2228\u1d47 false y = refl\n\n #-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n #-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #\u2228' {zero} f g with f []\n ... | true = s\u2264s z\u2264n\n ... | false = \u2115\u2264.refl\n #\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n #\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n #\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n #\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n #-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n #-bound zero f = Bool.to\u2115\u22641 (f [])\n #-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n #-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n #-\u2218vnot _ f = #-\u2295 1\u207f f\n\n #-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n #-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n #-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n #-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n #-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n #-not\u2218 zero f with f []\n ... | true = \u2261.refl\n ... | false = \u2261.refl\n #-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n #-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n #-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\nopen SimpleSearch public\nopen Sum public\nopen Count public\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192\n search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = search-\u2257 op _ _ (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nopen Defs public\n","old_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative.NP\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties.NP\nimport Data.List.NP as L\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0b \u2237 1b \u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u2219_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u2219 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u2219 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u2219-comm : \u2200 {m} (x y : A m) \u2192 x \u2219 y \u2261 y \u2219 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u2219-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nmodule OperationSyntax where\n infixr 1 _`\u204f_\n data Op : Set where\n `id `0\u21941 `not : Op\n `tl : Op \u2192 Op\n `if0 : Op \u2192 Op\n _`\u204f_ : Op \u2192 Op \u2192 Op\n\n {-\n id \u03b5 id\n 0\u21941 swp-inners interchange\n not swap comm\n if0 first ...\n if1 second ...\n -}\n\n infixr 9 _\u2219_\n _\u2219_ : Op \u2192 \u2200 {n} \u2192 Endo (Bits n)\n `id \u2219 xs = xs\n `0\u21941 \u2219 xs = 0\u21941 xs\n `not \u2219 [] = []\n `not \u2219 (x \u2237 xs) = not x \u2237 xs\n `tl f \u2219 [] = []\n `tl f \u2219 (x \u2237 xs) = x \u2237 f \u2219 xs\n `if0 f \u2219 [] = []\n `if0 f \u2219 (false \u2237 xs) = false \u2237 f \u2219 xs\n `if0 f \u2219 (true \u2237 xs) = true \u2237 xs\n (f `\u204f g) \u2219 xs = g \u2219 f \u2219 xs\n\n `if1 : Op \u2192 Op\n `if1 f = `not `\u204f `if0 f `\u204f `not\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 right swap\n -- (a \u2219 d) \u2219 (c \u2219 b)\n swp-seconds : Op\n swp-seconds = `if1 `not `\u204f `0\u21941 `\u204f `if1 `not\n\n on-extremes : Op \u2192 Op\n on-extremes f = swp-seconds `\u204f `if0 f `\u204f swp-seconds\n\n on-firsts : Op \u2192 Op\n on-firsts f = `0\u21941 `\u204f `if0 f `\u204f `0\u21941\n\n 0\u21941\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Op\n 0\u21941\u2237 [] = `not\n 0\u21941\u2237 (true {-1-} \u2237 p) = on-extremes (0\u21941\u2237 p)\n 0\u21941\u2237 (false{-0-} \u2237 p) = on-firsts (0\u21941\u2237 p)\n\n 0\u2194_ : \u2200 {n} \u2192 Bits n \u2192 Op\n 0\u2194 [] = `id\n 0\u2194 (false{-0-} \u2237 p) = `if0 (0\u2194 p)\n 0\u2194 (true{-1-} \u2237 p) = 0\u21941\u2237 p\n\n \u27e80\u2194_\u27e9-sem : \u2200 {n} (p : Bits n) \u2192 Bits n \u2192 Bits n\n \u27e80\u2194 p \u27e9-sem xs = if 0\u207f == xs then p else if p == xs then 0\u207f else xs\n\n if\u2237 : \u2200 {n} a x (xs ys : Bits n) \u2192 (if a then (x \u2237 xs) else (x \u2237 ys)) \u2261 x \u2237 (if a then xs else ys)\n if\u2237 true x xs ys = refl\n if\u2237 false x xs ys = refl\n\n if-not\u2237 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (false \u2237 xs) else (true \u2237 ys)) \u2261 (not a) \u2237 (if a then xs else ys)\n if-not\u2237 true xs ys = refl\n if-not\u2237 false xs ys = refl\n\n if\u2237\u2032 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (true \u2237 xs) else (false \u2237 ys)) \u2261 a \u2237 (if a then xs else ys)\n if\u2237\u2032 true xs ys = refl\n if\u2237\u2032 false xs ys = refl\n\n \u27e80\u21941\u2237_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u21941\u2237 p \u2219 xs \u2261 \u27e80\u2194 (1\u2237 p) \u27e9-sem xs\n \u27e80\u21941\u2237_\u27e9-spec [] (true \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec [] (false \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 true \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 false \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n\n \u27e80\u2194_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u2194 p \u2219 xs \u2261 \u27e80\u2194 p \u27e9-sem xs\n \u27e80\u2194_\u27e9-spec [] [] = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (true \u2237 xs) = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (false \u2237 xs)\n rewrite \u27e80\u2194 ps \u27e9-spec xs\n | if\u2237 (ps == xs) 0b 0\u207f xs\n | if\u2237 (0\u207f == xs) 0b ps (if ps == xs then 0\u207f else xs)\n = refl\n \u27e80\u2194_\u27e9-spec (true \u2237 p) xs = \u27e80\u21941\u2237 p \u27e9-spec xs\n\n open PermutationSyntax using (Perm; `id; `0\u21941; `tl; _`\u204f_)\n module P = PermutationSemantics\n\n toPerm : Op \u2192 Perm\n toPerm `id = `id\n toPerm `0\u21941 = `0\u21941\n toPerm `not = `id -- Important\n toPerm (`tl f) = `tl (toPerm f)\n toPerm (f `\u204f g) = toPerm f `\u204f toPerm g\n\n infixr 9 _\u2219\u2032_\n _\u2219\u2032_ : Op \u2192 \u2200 {n} \u2192 Endo (Bits n)\n f \u2219\u2032 xs = toPerm f P.\u2219 xs\n\n `\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Op\n `\u27e80\u21941+ zero \u27e9 = `0\u21941\n `\u27e80\u21941+ suc i \u27e9 = `0\u21941 `\u204f `tl `\u27e80\u21941+ i \u27e9 `\u204f `0\u21941\n\n `\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n `\u27e80\u21941+ zero \u27e9-spec xs = refl\n `\u27e80\u21941+ suc i \u27e9-spec (x \u2237 _ \u2237 xs) rewrite `\u27e80\u21941+ i \u27e9-spec (x \u2237 xs) = refl\n\n `\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Op\n `\u27e80\u2194 zero \u27e9 = `id\n `\u27e80\u2194 suc i \u27e9 = `\u27e80\u21941+ i \u27e9\n\n `\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n `\u27e80\u2194 zero \u27e9-spec xs = refl\n `\u27e80\u2194 suc i \u27e9-spec xs = `\u27e80\u21941+ i \u27e9-spec xs\n\n `\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Op\n `\u27e8 zero \u2194 j \u27e9 = `\u27e80\u2194 j \u27e9\n `\u27e8 i \u2194 zero \u27e9 = `\u27e80\u2194 i \u27e9\n `\u27e8 suc i \u2194 suc j \u27e9 = `tl `\u27e8 i \u2194 j \u27e9\n\n `\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) xs \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n `\u27e8_\u2194_\u27e9-spec zero j xs = `\u27e80\u2194 j \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) zero xs = `\u27e80\u21941+ i \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) (suc j) (x \u2237 xs) rewrite `\u27e8 i \u2194 j \u27e9-spec xs = refl\n\n `xor-head : Bit \u2192 Op\n `xor-head b = if b then `not else `id\n\n `xor-head-spec : \u2200 b {n} x (xs : Bits n) \u2192 `xor-head b \u2219 (x \u2237 xs) \u2261 (b xor x) \u2237 xs\n `xor-head-spec true x xs = refl\n `xor-head-spec false x xs = refl\n\n `\u27e8_\u2295\u27e9 : \u2200 {n} \u2192 Bits n \u2192 Op\n `\u27e8 [] \u2295\u27e9 = `id\n `\u27e8 b \u2237 xs \u2295\u27e9 = `xor-head b `\u204f `tl `\u27e8 xs \u2295\u27e9\n\n `\u27e8_\u2295\u27e9-spec : \u2200 {n} (xs ys : Bits n) \u2192 `\u27e8 xs \u2295\u27e9 \u2219 ys \u2261 xs \u2295 ys\n `\u27e8 [] \u2295\u27e9-spec [] = refl\n `\u27e8 true \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n `\u27e8 false \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) x \u2192 0\u21941 pad \u2295 0\u21941 x \u2261 0\u21941 (pad \u2295 x)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\n almost-\u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) f xs \u2192 f \u2219\u2032 pad \u2295 f \u2219 xs \u2261 f \u2219 (pad \u2295 xs)\n almost-\u2295-dist-\u2219 pad `id xs = refl\n almost-\u2295-dist-\u2219 pad `0\u21941 xs = \u2295-dist-0\u21941 pad xs\n almost-\u2295-dist-\u2219 [] `not [] = refl\n almost-\u2295-dist-\u2219 (true \u2237 pad) `not (x \u2237 xs) = refl\n almost-\u2295-dist-\u2219 (false \u2237 pad) `not (x \u2237 xs) = refl\n almost-\u2295-dist-\u2219 [] (`tl f) [] = refl\n almost-\u2295-dist-\u2219 (p \u2237 pad) (`tl f) (x \u2237 xs) rewrite almost-\u2295-dist-\u2219 pad f xs = refl\n almost-\u2295-dist-\u2219 pad (f `\u204f g) xs rewrite almost-\u2295-dist-\u2219 (f \u2219\u2032 pad) g (f \u2219 xs)\n | almost-\u2295-dist-\u2219 pad f xs = refl\n\nmodule PermutationSyntax-Props where\n open PermutationSyntax\n open PermutationSemantics\n open PermutationProperties\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) xs \u2192 0\u21941 pad \u2295 0\u21941 xs \u2261 0\u21941 (pad \u2295 xs)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\n -- \u229b-dist-\u2219 : \u2200 {n a} {A : Set a} (fs : Vec (A \u2192 A) n) \u03c0 xs \u2192 \u03c0 \u2219 fs \u229b \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (fs \u229b xs)\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 pad \u03c0 xs = \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs\n \u2261\u27e8 refl \u27e9\n vmap _xor_ (\u03c0 \u2219 pad) \u229b \u03c0 \u2219 xs\n \u2261\u27e8 {!!} \u27e9\n \u03c0 \u2219 vmap _xor_ pad \u229b \u03c0 \u2219 xs\n \u2261\u27e8 \u229b-dist-\u2219 _ (vmap _xor_ pad) \u03c0 xs \u27e9\n \u03c0 \u2219 (vmap _xor_ pad \u229b xs)\n \u2261\u27e8 refl \u27e9\n \u03c0 \u2219 (pad \u2295 xs)\n \u220e where open \u2261-Reasoning\n -- rans {!\u229b-dist-\u2219 (vmap _xor_ (op \u2219 pad)) op xs!} (\u229b-dist-\u2219 (vmap _xor_ pad) op xs)\n\nmodule SimpleSearch {a} {A : Set a} (_\u2219_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u2219_ public\n\n search-const\u03b5\u2261\u03b5 : \u2200 \u03b5 (\u03b5\u2219\u03b5 : \u03b5 \u2219 \u03b5 \u2261 \u03b5) n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-const\u03b5\u2261\u03b5 \u03b5 \u03b5\u2219\u03b5 = go\n where\n go : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n go zero = refl\n go (suc n) rewrite go n = \u03b5\u2219\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n module SearchInterchange (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u2219 f\u2081 x) \u2261 search f\u2080 \u2219 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u2219-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e\n where open \u2261-Reasoning\n\n search-0\u21941 : \u2200 {n} (f : Bits n \u2192 A) \u2192 search {n} (f \u2218 0\u21941) \u2261 search {n} f\n search-0\u21941 {zero} _ = refl\n search-0\u21941 {suc zero} _ = refl\n search-0\u21941 {suc (suc n)} _ = \u2219-interchange _ _ _ _\n\n module Op (\u2219-comm : Commutative _\u2261_ _\u2219_)\n (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n open SearchInterchange \u2219-interchange using (search-0\u21941)\n open OperationSyntax renaming (_\u2219_ to op)\n search-op : \u2200 {n} (f : Bits n \u2192 A) (g : Op) \u2192 search {n} (f \u2218 op g) \u2261 search {n} f\n search-op f `id = refl\n search-op f `0\u21941 = search-0\u21941 f\n search-op {zero} f `not = refl\n search-op {suc n} f `not = \u2219-comm _ _\n search-op {zero} f (`tl g) = refl\n search-op {suc n} f (`tl g) rewrite search-op (f \u2218 0\u2237_) g | search-op (f \u2218 1\u2237_) g = refl\n search-op {zero} f (`if0 g) = refl\n search-op {suc n} f (`if0 g) rewrite search-op (f \u2218 0\u2237_) g = refl\n search-op f (g `\u204f h) rewrite search-op (f \u2218 op h) g = search-op f h\n\nmodule Sum where\n open SimpleSearch _+_ using (module Comm; module SearchInterchange; search-const\u03b5\u2261\u03b5; module Op)\n open SimpleSearch _+_ public using () renaming (search to sum; search-\u2257 to sum-\u2257; searchBit to sumBit;\n search-\u2257\u2082 to sum-\u2257\u2082;\n search-+ to sum-+)\n open Comm \u2115\u00b0.+-comm public renaming (search-comm to sum-comm)\n open SearchInterchange +-interchange public renaming (\n search-dist to sum-dist;\n search-searchBit to sum-sumBit;\n search-search to sum-sum;\n search-swap to sum-swap)\n open Op \u2115\u00b0.+-comm +-interchange public renaming (search-op to sum-op)\n\n sum-const0\u22610 : \u2200 n \u2192 sum {n = n} (const 0) \u2261 0\n sum-const0\u22610 n = search-const\u03b5\u2261\u03b5 0 refl n\n\n sum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\n sum-const zero _ = refl\n sum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nmodule Count where\n open Sum\n open OperationSyntax renaming (_\u2219_ to op)\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n #\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n -- #-ext\n #-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n #-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n #-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n #-op : \u2200 {n} (f : Bits n \u2192 Bit) (g : Op) \u2192 #\u27e8 f \u2218 op g \u27e9 \u2261 #\u27e8 f \u27e9\n #-op f = sum-op (Bool.to\u2115 \u2218 f)\n\n #-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n #-\u2295 = #-comm\n\n #-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n #-const n b = sum-const n (Bool.to\u2115 b)\n\n #never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n #never\u22610 = sum-const0\u22610\n\n #always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n #always\u22612^ n = sum-const n 1\n\n #-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 Bit) \u2192 sum (\u03bb x \u2192 Bool.to\u2115 (f\u2080 x) + Bool.to\u2115 (f\u2081 x)) \u2261 #\u27e8 f\u2080 \u27e9 + #\u27e8 f\u2081 \u27e9\n #-dist f\u2080 f\u2081 = sum-dist (Bool.to\u2115 \u2218 f\u2080) (Bool.to\u2115 \u2218 f\u2081)\n\n #-+ : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n #\u27e8 f \u27e9 \u2261 sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9 )\n #-+ {m} {n} f = sum-+ {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-# : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9)\n \u2261 sum {n} (\u03bb ys \u2192 #\u27e8 (\u03bb (xs : Bits m) \u2192 f (xs ++ ys)) \u27e9)\n #-# {m} {n} f = sum-sum {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-swap : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192 #\u27e8 f \u2218 vswap n {m} \u27e9 \u2261 #\u27e8 f \u27e9\n #-swap {m} {n} f = sum-swap {m} {n} (Bool.to\u2115 \u2218 f)\n\n #\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n #\u27e8== [] \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n \u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n \u2257-cong-# f g f\u2257g = sum-\u2257 _ _ (cong Bool.to\u2115 \u2218 f\u2257g)\n\n -- #-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n -- #-+ f f0 f1 rewrite f0 | f1 = refl\n\n #-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-take-drop zero n f g with f []\n ... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n ... | false = #never\u22610 n\n #-take-drop (suc m) n f g\n rewrite \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x))\n | #-take-drop m n (f \u2218 0\u2237_) g\n | \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x))\n | #-take-drop m n (f \u2218 1\u2237_) g\n = sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9)\n\n #-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n #\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n #\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n #\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n ... | true | true | _ = s\u2264s z\u2264n\n ... | true | false | p = \u22a5-elim (p _)\n ... | false | _ | _ = z\u2264n\n #\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n #-\u2227-\u2228\u1d47 : \u2200 x y \u2192 Bool.to\u2115 (x \u2227 y) + Bool.to\u2115 (x \u2228 y) \u2261 Bool.to\u2115 x + Bool.to\u2115 y\n #-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (Bool.to\u2115 y) 1 = refl\n #-\u2227-\u2228\u1d47 false y = refl\n\n #-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n #-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #\u2228' {zero} f g with f []\n ... | true = s\u2264s z\u2264n\n ... | false = \u2115\u2264.refl\n #\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n #\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n #\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n #\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n #-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n #-bound zero f = Bool.to\u2115\u22641 (f [])\n #-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n #-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n #-\u2218vnot _ f = #-\u2295 1\u207f f\n\n #-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n #-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n #-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n #-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n #-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n #-not\u2218 zero f with f []\n ... | true = \u2261.refl\n ... | false = \u2261.refl\n #-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n #-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n #-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\nopen SimpleSearch public\nopen Sum public\nopen Count public\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192\n search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = search-\u2257 op _ _ (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nopen Defs public\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"41f662646924858d5d32ac199e0887cf908df64d","subject":"Added infor for an issue.","message":"Added infor for an issue.\n\nIgnore-this: 3d18d25b01fdd40e186f7f1195c43154\n\ndarcs-hash:20120619225108-3bd4e-09bf31bb63af16215eb30f7625f2746506400e2a.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Issues\/BadErase.agda","new_file":"Issues\/BadErase.agda","new_contents":"------------------------------------------------------------------------------\n-- The translation is badly erasing the universal quantification\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Issues.BadErase where\n\npostulate\n _\u2194_ : Set \u2192 Set \u2192 Set\n D : Set\n A : Set\n\npostulate bad\u2081 : ((x : D) \u2192 A) \u2192 A\n{-# ATP prove bad\u2081 #-}\n\n-- Before the patch\n--\n-- Wed Sep 21 04:50:43 COT 2011 ulfn@chalmers.se\n-- * got rid of the Fun constructor in internal syntax (using Pi _ (NoAbs _ _) instead)\n--\n-- the type of bad\u2081 was:\n--\n-- Type: El (Type (Max [])) (Fun r(El (Type (Max [])) (Pi r(El (Type (Max [])) (Def BadErase.D [])) (Abs \"x\" El (Type (Max [])) (Def BadErase.A [])))) (El (Type (Max [])) (Def BadErase.A [])))\n--\n--\n-- After the above patch the type of bad\u2081 was:\n--\n-- Type: El (Type (Max [])) (Pi r(El (Type (Max [])) (Pi r(El (Type (Max [])) (Def BadErase.D [])) (Abs \"x\" El (Type (Max [])) (Def BadErase.A [])))) (NoAbs \"_\" El (Type (Max [])) (Def BadErase.A [])))\n--\n--\n-- On 19 June 2012, the type of bad\u2081 is:\n--\n-- Type: El {getSort = Type (Max []), unEl = Pi r(El {getSort = Type (Max []), unEl = Pi r(El {getSort = Type (Max []), unEl = Def BadErase.D []}) (NoAbs \"x\" El {getSort = Type (Max []), unEl = Def BadErase.A []})}) (NoAbs \"_\" El {getSort = Type (Max []), unEl = Def BadErase.A []})}\n\npostulate bad\u2082 : A \u2192 ((x : D) \u2192 A)\n{-# ATP prove bad\u2082 #-}\n\npostulate bad\u2083 : ((x : D) \u2192 A) \u2194 A\n{-# ATP prove bad\u2083 #-}\n","old_contents":"------------------------------------------------------------------------------\n-- The translation is badly erasing the universal quantification\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Found on 14 March 2012.\n\nmodule Issues.BadErase where\n\npostulate\n _\u2194_ : Set \u2192 Set \u2192 Set\n D : Set\n A : Set\n\npostulate bad\u2081 : ((x : D) \u2192 A) \u2192 A\n{-# ATP prove bad\u2081 #-}\n\npostulate bad\u2082 : A \u2192 ((x : D) \u2192 A)\n{-# ATP prove bad\u2082 #-}\n\npostulate bad\u2083 : ((x : D) \u2192 A) \u2194 A\n{-# ATP prove bad\u2083 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7ebc94aba2846920a72e18a454c5be2ed6faea1d","subject":"Added arithmetic properties (by Ana Bove).","message":"Added arithmetic properties (by Ana Bove).\n\nIgnore-this: f4f694d231eb13a003e65c404d641cb2\n\ndarcs-hash:20110218163424-3bd4e-631646d7d2092969a662e91b13a18d35345a889a.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/McCarthy91\/ArithmeticATP.agda","new_file":"Draft\/McCarthy91\/ArithmeticATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties used by the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.ArithmeticATP where\n\nopen import LTC.Base\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\n\n------------------------------------------------------------------------------\n\npostulate\n 1+x-N : \u2200 {n} \u2192 N n \u2192 N (one + n)\n 11+x-N : \u2200 {n} \u2192 N n \u2192 N (eleven + n)\n x\u223810-N : \u2200 {n} \u2192 N n \u2192 N (n \u2238 ten)\n x+11-N : \u2200 {n} \u2192 N n \u2192 N (n + eleven)\n{-# ATP prove 1+x-N #-}\n{-# ATP prove 11+x-N #-}\n{-# ATP prove x\u223810-N \u2238-N N10 #-}\n{-# ATP prove x+11-N 11+x-N +-comm #-}\n\npostulate\n 91\u2261[100+11\u223810]\u223810 : (one-hundred + eleven \u2238 ten) \u2238 ten \u2261 ninety-one\n{-# ATP prove 91\u2261[100+11\u223810]\u223810 #-}\n\npostulate\n 100\u226189+11 : eighty-nine + eleven \u2261 one-hundred\n 101\u226190+11\u2261101 : ninety + eleven \u2261 hundred-one\n 101\u2261100+11-10 : (one-hundred + eleven) \u2238 ten \u2261 hundred-one\n 102\u226191+11 : ninety-one + eleven \u2261 hundred-two\n 103\u226192+1 : ninety-two + eleven \u2261 hundred-three\n 104\u226193+11 : ninety-three + eleven \u2261 hundred-four\n 105\u226194+11 : ninety-four + eleven \u2261 hundred-five\n 106\u226195+11 : ninety-five + eleven \u2261 hundred-six\n 107\u226196+11 : ninety-six + eleven \u2261 hundred-seven\n 108\u226197+11 : ninety-seven + eleven \u2261 hundred-eight\n 109\u226199+11 : ninety-eight + eleven \u2261 hundred-nine\n 110\u226199+11 : ninety-nine + eleven \u2261 hundred-ten\n 111\u2261100+11 : one-hundred + eleven \u2261 hundred-eleven\n\n{-# ATP prove 100\u226189+11 #-}\n{-# ATP prove 101\u226190+11\u2261101 #-}\n{-# ATP prove 101\u2261100+11-10 #-}\n{-# ATP prove 102\u226191+11 #-}\n{-# ATP prove 103\u226192+1 #-}\n{-# ATP prove 104\u226193+11 #-}\n{-# ATP prove 105\u226194+11 #-}\n{-# ATP prove 106\u226195+11 #-}\n{-# ATP prove 107\u226196+11 #-}\n{-# ATP prove 108\u226197+11 #-}\n{-# ATP prove 109\u226199+11 #-}\n{-# ATP prove 110\u226199+11 #-}\n{-# ATP prove 111\u2261100+11 #-}\n\npostulate\n 101>100' : GT hundred-one one-hundred\n 101>100 : GT ((one-hundred + eleven) \u2238 ten) one-hundred\n 102>100 : GT hundred-two one-hundred\n 103>100 : GT hundred-three one-hundred\n 104>100 : GT hundred-four one-hundred\n 105>100 : GT hundred-five one-hundred\n 106>100 : GT hundred-six one-hundred\n 107>100 : GT hundred-seven one-hundred\n 108>100 : GT hundred-eight one-hundred\n 109>100 : GT hundred-nine one-hundred\n 110>100 : GT hundred-ten one-hundred\n 111>100' : GT hundred-eleven one-hundred\n 111>100 : GT (one-hundred + eleven) one-hundred\n{-# ATP prove 101>100' #-}\n{-# ATP prove 101>100 101>100' 101\u2261100+11-10 #-}\n{-# ATP prove 102>100 #-}\n{-# ATP prove 103>100 #-}\n{-# ATP prove 104>100 #-}\n{-# ATP prove 105>100 #-}\n{-# ATP prove 106>100 #-}\n{-# ATP prove 107>100 #-}\n{-# ATP prove 108>100 #-}\n{-# ATP prove 109>100 #-}\n{-# ATP prove 110>100 #-}\n{-# ATP prove 111>100' #-}\n{-# ATP prove 111>100 111>100' 111\u2261100+11 #-}\n\npostulate\n 99+11>100 : GT (ninety-nine + eleven) one-hundred\n 98+11>100 : GT (ninety-eight + eleven) one-hundred\n 97+11>100 : GT (ninety-seven + eleven) one-hundred\n 96+11>100 : GT (ninety-six + eleven) one-hundred\n 95+11>100 : GT (ninety-five + eleven) one-hundred\n 94+11>100 : GT (ninety-four + eleven) one-hundred\n 93+11>100 : GT (ninety-three + eleven) one-hundred\n 92+11>100 : GT (ninety-two + eleven) one-hundred\n 91+11>100 : GT (ninety-one + eleven) one-hundred\n 90+11>100 : GT (ninety + eleven) one-hundred\n{-# ATP prove 99+11>100 110>100 110\u226199+11 #-}\n{-# ATP prove 98+11>100 109>100 109\u226199+11 #-}\n{-# ATP prove 97+11>100 108>100 108\u226197+11 #-}\n{-# ATP prove 96+11>100 107>100 107\u226196+11 #-}\n{-# ATP prove 95+11>100 106>100 106\u226195+11 #-}\n{-# ATP prove 94+11>100 105>100 105\u226194+11 #-}\n{-# ATP prove 93+11>100 104>100 104\u226193+11 #-}\n{-# ATP prove 92+11>100 103>100 103\u226192+1 #-}\n{-# ATP prove 91+11>100 102>100 102\u226191+11 #-}\n{-# ATP prove 90+11>100 101>100' 101\u226190+11\u2261101 #-}\n\npostulate\n 100<102' : LT one-hundred hundred-two\n 100<102 : LT one-hundred (ninety-one + eleven)\n{-# ATP prove 100<102' #-}\n{-# ATP prove 100<102 100<102' 102\u226191+11 #-}\n\npostulate 91>100\u2192\u22a5 : GT ninety-one one-hundred \u2192 \u22a5\n{-# ATP prove 91>100\u2192\u22a5 #-}\n\npostulate x+1\u2264x\u223810+11 : \u2200 {n} \u2192 N n \u2192 LE (n + one) ((n \u2238 ten) + eleven)\n{-# ATP prove x+1\u2264x\u223810+11 x\u2264y+x\u2238y N10 1+x-N 11+x-N x\u223810-N +-comm #-}\n\npostulate x\u226489\u2192x+11>100\u2192\u22a5 : \u2200 {n} \u2192 N n \u2192 LE n eighty-nine \u2192\n GT (n + eleven) one-hundred \u2192 \u22a5\n{-# ATP prove x\u226489\u2192x+11>100\u2192\u22a5 x>y\u2192x\u2264y\u2192\u22a5 x\u2264y\u2192x+k\u2264y+k x+11-N N89 N100 100\u226189+11 #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Arithmetic stuff used by the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.ArithmeticATP where\n\nopen import LTC.Base\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\n\n------------------------------------------------------------------------------\n\npostulate\n 1+x-N : \u2200 {n} \u2192 N n \u2192 N (one + n)\n 11+x-N : \u2200 {n} \u2192 N n \u2192 N (eleven + n)\n x\u223810-N : \u2200 {n} \u2192 N n \u2192 N (n \u2238 ten)\n x+11-N : \u2200 {n} \u2192 N n \u2192 N (n + eleven)\n{-# ATP prove 1+x-N #-}\n{-# ATP prove 11+x-N #-}\n{-# ATP prove x\u223810-N \u2238-N N10 #-}\n{-# ATP prove x+11-N 11+x-N +-comm #-}\n\npostulate\n 91\u2261[100+11\u223810]\u223810 : (one-hundred + eleven \u2238 ten) \u2238 ten \u2261 ninety-one\n{-# ATP prove 91\u2261[100+11\u223810]\u223810 #-}\n\npostulate\n 100\u226189+11 : eighty-nine + eleven \u2261 one-hundred\n 101\u226190+11\u2261101 : ninety + eleven \u2261 hundred-one\n 101\u2261100+11-10 : (one-hundred + eleven) \u2238 ten \u2261 hundred-one\n 102\u226191+11 : ninety-one + eleven \u2261 hundred-two\n 103\u226192+1 : ninety-two + eleven \u2261 hundred-three\n 104\u226193+11 : ninety-three + eleven \u2261 hundred-four\n 105\u226194+11 : ninety-four + eleven \u2261 hundred-five\n 106\u226195+11 : ninety-five + eleven \u2261 hundred-six\n 107\u226196+11 : ninety-six + eleven \u2261 hundred-seven\n 108\u226197+11 : ninety-seven + eleven \u2261 hundred-eight\n 109\u226199+11 : ninety-eight + eleven \u2261 hundred-nine\n 110\u226199+11 : ninety-nine + eleven \u2261 hundred-ten\n 111\u2261100+11 : one-hundred + eleven \u2261 hundred-eleven\n\n{-# ATP prove 100\u226189+11 #-}\n{-# ATP prove 101\u226190+11\u2261101 #-}\n{-# ATP prove 101\u2261100+11-10 #-}\n{-# ATP prove 102\u226191+11 #-}\n{-# ATP prove 103\u226192+1 #-}\n{-# ATP prove 104\u226193+11 #-}\n{-# ATP prove 105\u226194+11 #-}\n{-# ATP prove 106\u226195+11 #-}\n{-# ATP prove 107\u226196+11 #-}\n{-# ATP prove 108\u226197+11 #-}\n{-# ATP prove 109\u226199+11 #-}\n{-# ATP prove 110\u226199+11 #-}\n{-# ATP prove 111\u2261100+11 #-}\n\npostulate\n 101>100' : GT hundred-one one-hundred\n 101>100 : GT ((one-hundred + eleven) \u2238 ten) one-hundred\n 111>100' : GT hundred-eleven one-hundred\n 111>100 : GT (one-hundred + eleven) one-hundred\n 100<102' : LT one-hundred hundred-two\n 100<102 : LT one-hundred (ninety-one + eleven)\n{-# ATP prove 101>100' #-}\n{-# ATP prove 101>100 101>100' 101\u2261100+11-10 #-}\n{-# ATP prove 111>100' #-}\n{-# ATP prove 111>100 111>100' 111\u2261100+11 #-}\n{-# ATP prove 100<102' #-}\n{-# ATP prove 100<102 100<102' 102\u226191+11 #-}\n\npostulate 91>100\u2192\u22a5 : GT ninety-one one-hundred \u2192 \u22a5\n{-# ATP prove 91>100\u2192\u22a5 #-}\n\npostulate x+1\u2264x\u223810+11 : \u2200 {n} \u2192 N n \u2192 LE (n + one) ((n \u2238 ten) + eleven)\n{-# ATP prove x+1\u2264x\u223810+11 x\u2264y+x\u2238y N10 1+x-N 11+x-N x\u223810-N +-comm #-}\n\npostulate x\u226489\u2192x+11>100\u2192\u22a5 : \u2200 {n} \u2192 N n \u2192 LE n eighty-nine \u2192\n GT (n + eleven) one-hundred \u2192 \u22a5\n{-# ATP prove x\u226489\u2192x+11>100\u2192\u22a5 x>y\u2192x\u2264y\u2192\u22a5 x\u2264y\u2192x+k\u2264y+k x+11-N N89 N100 100\u226189+11 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"62a643cc7f1e2ea25a239431d779ca974cdeed73","subject":"Data.Vec.NP: count, count\u1da0 and properties","message":"Data.Vec.NP: count, count\u1da0 and properties\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Vec\/NP.agda","new_file":"lib\/Data\/Vec\/NP.agda","new_contents":"module Data.Vec.NP where\n\nopen import Data.Vec public\nopen import Data.Nat using (\u2115; suc; zero; _+_)\nopen import Data.Fin renaming (_+_ to _+\u1da0_)\nopen import Data.Fin.Props\nopen import Data.Bool\nopen import Data.Product hiding (map)\nopen import Function\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\n\ncount\u1da0 : \u2200 {n a} {A : Set a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 Fin (suc n)\ncount\u1da0 pred = foldr (Fin \u2218 suc) (\u03bb x \u2192 if pred x then suc else inject\u2081) zero\n\ncount : \u2200 {n a} {A : Set a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 \u2115\ncount pred = to\u2115 \u2218 count\u1da0 pred\n\ncount-\u2218 : \u2200 {n a b} {A : Set a} {B : Set b} (f : A \u2192 B) (pred : B \u2192 Bool) \u2192\n count {n} (pred \u2218 f) \u2257 count pred \u2218 map f\ncount-\u2218 f pred [] = refl\ncount-\u2218 f pred (x \u2237 xs) with pred (f x)\n... | true rewrite count-\u2218 f pred xs = refl\n... | false rewrite inject\u2081-lemma (count\u1da0 pred (map f xs))\n | inject\u2081-lemma (count\u1da0 (pred \u2218 f) xs)\n | count-\u2218 f pred xs = refl\n\ncount-++ : \u2200 {m n a} {A : Set a} (pred : A \u2192 Bool) (xs : Vec A m) (ys : Vec A n)\n \u2192 count pred (xs ++ ys) \u2261 count pred xs + count pred ys\ncount-++ pred [] ys = refl\ncount-++ pred (x \u2237 xs) ys with pred x\n... | true rewrite count-++ pred xs ys = refl\n... | false rewrite inject\u2081-lemma (count\u1da0 pred (xs ++ ys))\n | inject\u2081-lemma (count\u1da0 pred xs) | count-++ pred xs ys = refl\n\nfilter : \u2200 {n a} {A : Set a} (pred : A \u2192 Bool) (xs : Vec A n) \u2192 Vec A (count pred xs)\nfilter pred [] = []\nfilter pred (x \u2237 xs) with pred x\n... | true = x \u2237 filter pred xs\n... | false rewrite inject\u2081-lemma (count\u1da0 pred xs) = filter pred xs\n\n\u03b7 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7 = tabulate \u2218 flip lookup\n\n\u03b7\u2032 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7\u2032 {zero} = \u03bb _ \u2192 []\n\u03b7\u2032 {suc n} = \u03bb xs \u2192 head xs \u2237 \u03b7 (tail xs)\n\nuncons : \u2200 {n a} {A : Set a} \u2192 Vec A (1 + n) \u2192 (A \u00d7 Vec A n)\nuncons (x \u2237 xs) = x , xs\n\nsplitAt\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A m \u00d7 Vec A n\nsplitAt\u2032 m xs = case splitAt m xs of \u03bb { (ys , zs , _) \u2192 (ys , zs) }\n\n++-decomp : \u2200 {m n a} {A : Set a} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2261 (zs ++ ts) \u2192 (xs \u2261 zs \u00d7 ys \u2261 ts)\n++-decomp {zero} {xs = []} {[]} p = refl , p\n++-decomp {suc m} {xs = x \u2237 xs} {z \u2237 zs} eq with ++-decomp {m} {xs = xs} {zs} (cong tail eq)\n... | (q\u2081 , q\u2082) = (cong\u2082 _\u2237_ (cong head eq) q\u2081) , q\u2082\n\n{-\nopen import Data.Vec.Equality\n\nmodule Here {a} {A : Set a} where\n open Equality (\u2261.setoid A)\n \u2248-splitAt : \u2200 {m n} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2248 (zs ++ ts) \u2192 (xs \u2248 zs \u00d7 ys \u2248 ts)\n \u2248-splitAt {zero} {xs = []} {[]} p = []-cong , p\n \u2248-splitAt {suc m} {xs = x \u2237 xs} {z \u2237 zs} (x\u00b9\u2248x\u00b2 \u2237-cong p) with \u2248-splitAt {m} {xs = xs} {zs} p\n ... | (q\u2081 , q\u2082) = x\u00b9\u2248x\u00b2 \u2237-cong q\u2081 , q\u2082\n-}\n\n++-inj\u2081 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 xs \u2261 ys\n++-inj\u2081 xs ys eq = proj\u2081 (++-decomp eq)\n\n++-inj\u2082 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 zs \u2261 ts\n++-inj\u2082 xs ys eq = proj\u2082 (++-decomp {xs = xs} {ys} eq)\n\ntake-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 take m (xs ++ ys) \u2261 xs\ntake-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ntake-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2081 xs\u2081 xs eq)\n\ndrop-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 drop m (xs ++ ys) \u2261 ys\ndrop-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ndrop-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2082 xs\u2081 xs eq)\n\ntake-drop-lem : \u2200 m {n} {a} {A : Set a} (xs : Vec A (m + n)) \u2192 take m xs ++ drop m xs \u2261 xs\ntake-drop-lem m xs with splitAt m xs\ntake-drop-lem m .(ys ++ zs) | ys , zs , refl = refl\n\ntake-them-all : \u2200 n {a} {A : Set a} (xs : Vec A (n + 0)) \u2192 take n xs ++ [] \u2261 xs\ntake-them-all n xs with splitAt n xs\ntake-them-all n .(ys ++ []) | ys , [] , refl = refl\n","old_contents":"module Data.Vec.NP where\n\nopen import Data.Vec public\nopen import Data.Nat using (\u2115; suc; zero; _+_)\nopen import Data.Fin hiding (_+_)\nopen import Data.Fin.Props\nopen import Data.Bool\nopen import Data.Product\nopen import Function\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\n\ncount : \u2200 {n a} {A : Set a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 Fin (suc n)\ncount pred [] = zero\ncount pred (x \u2237 xs) = (if pred x then suc else inject\u2081) (count pred xs)\n\nfilter : \u2200 {n a} {A : Set a} (pred : A \u2192 Bool) (xs : Vec A n) \u2192 Vec A (to\u2115 (count pred xs))\nfilter pred [] = []\nfilter pred (x \u2237 xs) with pred x\n... | true = x \u2237 filter pred xs\n... | false rewrite inject\u2081-lemma (count pred xs) = filter pred xs\n\n\u03b7 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7 = tabulate \u2218 flip lookup\n\n\u03b7\u2032 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7\u2032 {zero} = \u03bb _ \u2192 []\n\u03b7\u2032 {suc n} = \u03bb xs \u2192 head xs \u2237 \u03b7 (tail xs)\n\nuncons : \u2200 {n a} {A : Set a} \u2192 Vec A (1 + n) \u2192 (A \u00d7 Vec A n)\nuncons (x \u2237 xs) = x , xs\n\nsplitAt\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A m \u00d7 Vec A n\nsplitAt\u2032 m xs = case splitAt m xs of \u03bb { (ys , zs , _) \u2192 (ys , zs) }\n\n++-decomp : \u2200 {m n a} {A : Set a} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2261 (zs ++ ts) \u2192 (xs \u2261 zs \u00d7 ys \u2261 ts)\n++-decomp {zero} {xs = []} {[]} p = refl , p\n++-decomp {suc m} {xs = x \u2237 xs} {z \u2237 zs} eq with ++-decomp {m} {xs = xs} {zs} (cong tail eq)\n... | (q\u2081 , q\u2082) = (cong\u2082 _\u2237_ (cong head eq) q\u2081) , q\u2082\n\n{-\nopen import Data.Vec.Equality\n\nmodule Here {a} {A : Set a} where\n open Equality (\u2261.setoid A)\n \u2248-splitAt : \u2200 {m n} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2248 (zs ++ ts) \u2192 (xs \u2248 zs \u00d7 ys \u2248 ts)\n \u2248-splitAt {zero} {xs = []} {[]} p = []-cong , p\n \u2248-splitAt {suc m} {xs = x \u2237 xs} {z \u2237 zs} (x\u00b9\u2248x\u00b2 \u2237-cong p) with \u2248-splitAt {m} {xs = xs} {zs} p\n ... | (q\u2081 , q\u2082) = x\u00b9\u2248x\u00b2 \u2237-cong q\u2081 , q\u2082\n-}\n\n++-inj\u2081 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 xs \u2261 ys\n++-inj\u2081 xs ys eq = proj\u2081 (++-decomp eq)\n\n++-inj\u2082 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 zs \u2261 ts\n++-inj\u2082 xs ys eq = proj\u2082 (++-decomp {xs = xs} {ys} eq)\n\ntake-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 take m (xs ++ ys) \u2261 xs\ntake-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ntake-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2081 xs\u2081 xs eq)\n\ndrop-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 drop m (xs ++ ys) \u2261 ys\ndrop-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ndrop-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2082 xs\u2081 xs eq)\n\ntake-drop-lem : \u2200 m {n} {a} {A : Set a} (xs : Vec A (m + n)) \u2192 take m xs ++ drop m xs \u2261 xs\ntake-drop-lem m xs with splitAt m xs\ntake-drop-lem m .(ys ++ zs) | ys , zs , refl = refl\n\ntake-them-all : \u2200 n {a} {A : Set a} (xs : Vec A (n + 0)) \u2192 take n xs ++ [] \u2261 xs\ntake-them-all n xs with splitAt n xs\ntake-them-all n .(ys ++ []) | ys , [] , refl = refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"47b55ae0449921322131bd6dbdab41837ccb68c8","subject":"Add support for general vector to our flat-funs abstraction","message":"Add support for general vector to our flat-funs abstraction\n","repos":"crypto-agda\/crypto-agda","old_file":"flat-funs.agda","new_file":"flat-funs.agda","new_contents":"module flat-funs where\n\nopen import Data.Nat\nimport Level as L\nopen import composable\nopen import vcomp\n\nrecord FlatFuns {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n `\u22a4 : T\n `Bit : T\n _`\u00d7_ : T \u2192 T \u2192 T\n _`^_ : T \u2192 \u2115 \u2192 T\n _`\u2192_ : T \u2192 T \u2192 Set\n\n `Vec : T \u2192 \u2115 \u2192 T\n `Vec A n = A `^ n\n\n `Bits : \u2115 \u2192 T\n `Bits n = `Bit `^ n\n\n infixr 2 _`\u00d7_\n infixl 2 _`^_\n infix 0 _`\u2192_\n\nrecord FlatFunsOps {t} {T : Set t} (\u266dFuns : FlatFuns T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n field\n idO : \u2200 {A} \u2192 A `\u2192 A\n isComposable : Composable _`\u2192_\n isVComposable : VComposable _`\u00d7_ _`\u2192_\n\n -- Fanout\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n\n open FlatFuns \u266dFuns public\n open Composable isComposable public\n open VComposable isVComposable public\n\nopen import Data.Unit using (\u22a4)\nopen import Data.Vec\nopen import Data.Bits\nopen import Data.Product\nopen import Function\n\nfun\u266dFuns : FlatFuns Set\nfun\u266dFuns = mk \u22a4 Bit _\u00d7_ Vec (\u03bb A B \u2192 A \u2192 B)\n\nbitsFun\u266dFuns : FlatFuns \u2115\nbitsFun\u266dFuns = mk 0 1 _+_ _*_ (\u03bb i o \u2192 Bits i \u2192 Bits o)\n\nfun\u266dOps : FlatFunsOps fun\u266dFuns\nfun\u266dOps = mk id funComp funVComp _&&&_ proj\u2081 proj\u2082\n where\n _&&&_ : \u2200 {A B C : Set} \u2192 (A \u2192 B) \u2192 (A \u2192 C) \u2192 A \u2192 B \u00d7 C\n (f &&& g) x = (f x , g x)\n\nbitsFun\u266dOps : FlatFunsOps bitsFun\u266dFuns\nbitsFun\u266dOps = mk id bitsFunComp bitsFunVComp _&&&_ (\u03bb {A} \u2192 take A) (\u03bb {A} \u2192 drop A)\n where\n open FlatFuns bitsFun\u266dFuns\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n (f &&& g) x = (f x ++ g x)\n","old_contents":"module flat-funs where\n\nopen import Data.Nat\nimport Level as L\nopen import composable\nopen import vcomp\n\nrecord FlatFuns {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n `Bits : \u2115 \u2192 T\n `Bit : T\n _`\u00d7_ : T \u2192 T \u2192 T\n _`\u2192_ : T \u2192 T \u2192 Set\n infixr 2 _`\u00d7_\n infix 0 _`\u2192_\n\nrecord FlatFunsOps {t} {T : Set t} (\u266dFuns : FlatFuns T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n field\n idO : \u2200 {A} \u2192 A `\u2192 A\n isComposable : Composable _`\u2192_\n isVComposable : VComposable _`\u00d7_ _`\u2192_\n\n -- Fanout\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n\n open FlatFuns \u266dFuns public\n open Composable isComposable public\n open VComposable isVComposable public\n\nopen import Data.Bits\nopen import Data.Product\nopen import Function\n\nfun\u266dFuns : FlatFuns Set\nfun\u266dFuns = mk Bits Bit _\u00d7_ (\u03bb A B \u2192 A \u2192 B)\n\nbitsfun\u266dFuns : FlatFuns \u2115\nbitsfun\u266dFuns = mk id 1 _+_ (\u03bb i o \u2192 Bits i \u2192 Bits o)\n\nfun\u266dOps : FlatFunsOps fun\u266dFuns\nfun\u266dOps = mk id funComp funVComp _&&&_\n where\n _&&&_ : \u2200 {A B C : Set} \u2192 (A \u2192 B) \u2192 (A \u2192 C) \u2192 A \u2192 B \u00d7 C\n (f &&& g) x = (f x , g x)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"da85f556d63b099902d17027a8e055c87c053e25","subject":"Added doc.","message":"Added doc.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/GreatestFixedPoints\/Conat.agda","new_file":"notes\/fixed-points\/GreatestFixedPoints\/Conat.agda","new_contents":"------------------------------------------------------------------------------\n-- Co-inductive natural numbers\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule GreatestFixedPoints.Conat where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- Conat is a greatest fixed-point of a functor\n\n-- The functor.\nNatF : (D \u2192 Set) \u2192 D \u2192 Set\nNatF A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n\n-- The co-natural numbers are the greatest fixed-point of NatF.\npostulate\n Conat : D \u2192 Set\n\n -- Conat is a post-fixed point of NatF, i.e.\n --\n -- Conat \u2264 NatF Conat.\n Conat-unf : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\n\n -- The higher-order version.\n Conat-unf-ho : \u2200 {n} \u2192 Conat n \u2192 NatF Conat n\n\n -- Conat is the greatest post-fixed point of NatF, i.e\n --\n -- \u2200 P. P \u2264 NatF P \u21d2 P \u2264 Conat.\n Conat-coind :\n \u2200 (A : D \u2192 Set) \u2192\n -- A is post-fixed point of ConatF.\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n -- Conat is greater than A.\n \u2200 {n} \u2192 A n \u2192 Conat n\n\n -- The higher-order version.\n Conat-coind-ho :\n \u2200 (A : D \u2192 Set) \u2192 (\u2200 {n} \u2192 A n \u2192 NatF A n) \u2192 \u2200 {n} \u2192 A n \u2192 Conat n\n\n -- 22 December 2013. This is a stronger induction principle. If we\n -- use it, we can use the trivial A = \u03bb x \u2192 x \u2261 x in the\n -- proofs. Unfortunately, we don't have a justification for this\n -- principle.\n Conat-coind-stronger :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n A n \u2192 Conat n\n\n------------------------------------------------------------------------------\n-- Conat-unf and Conat-unf-ho are equivalents\n\nConat-unf' : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\nConat-unf' = Conat-unf-ho\n\nConat-unf-ho' : \u2200 {n} \u2192 Conat n \u2192 NatF Conat n\nConat-unf-ho' = Conat-unf\n\n------------------------------------------------------------------------------\n-- Conat-coind and Conat-coind-ho are equivalents\n\nConat-coind' :\n \u2200 (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind' = Conat-coind-ho\n\nConat-coind-ho' :\n \u2200 (A : D \u2192 Set) \u2192 (\u2200 {n} \u2192 A n \u2192 NatF A n) \u2192 \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind-ho' = Conat-coind\n\n------------------------------------------------------------------------------\n-- From Conat-coind\/Conat-coind-stronger to Conat-coind-stronger\/Conat-coind\n\nConat-coind'' :\n \u2200 (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind'' A h An = Conat-coind-stronger A h An\n\n-- 22 December 2013: We cannot prove Conat-coind-stronger using\n-- Conat-coind.\nConat-coind-stronger'' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n A n \u2192 Conat n\nConat-coind-stronger'' A h An = Conat-coind A {!!} An\n","old_contents":"------------------------------------------------------------------------------\n-- Co-inductive natural numbers\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule GreatestFixedPoints.Conat where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- Conat is a greatest fixed-point of a functor\n\n-- The functor.\nNatF : (D \u2192 Set) \u2192 D \u2192 Set\nNatF A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n\n-- The co-natural numbers are the greatest fixed-point of NatF.\npostulate\n Conat : D \u2192 Set\n\n -- Conat is a post-fixed point of NatF, i.e.\n --\n -- Conat \u2264 NatF Conat.\n Conat-unf : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\n\n -- The higher-order version.\n Conat-unf-ho : \u2200 {n} \u2192 Conat n \u2192 NatF Conat n\n\n -- Conat is the greatest post-fixed point of NatF, i.e\n --\n -- \u2200 P. P \u2264 NatF P \u21d2 P \u2264 Conat.\n Conat-coind : \u2200 (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\n\n -- The higher-order version.\n Conat-coind-ho : \u2200 (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 NatF A n) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\n\n Conat-coind-wrong : \u2200 (A : D \u2192 Set) {n} \u2192\n -- A is post-fixed point of ConatF.\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n -- Conat is greater than A.\n A n \u2192 Conat n\n\n------------------------------------------------------------------------------\n-- Conat-unf and Conat-unf-ho are equivalents\n\nConat-unf' : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 Conat n')\nConat-unf' = Conat-unf-ho\n\nConat-unf-ho' : \u2200 {n} \u2192 Conat n \u2192 NatF Conat n\nConat-unf-ho' = Conat-unf\n\n------------------------------------------------------------------------------\n-- Conat-coind and Conat-coind-ho are equivalents\n\nConat-coind' : \u2200 (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind' = Conat-coind-ho\n\nConat-coind-ho' : \u2200 (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 NatF A n) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind-ho' = Conat-coind\n\n------------------------------------------------------------------------------\n-- From Conat-coind\/Conat-coind-wrong to Conat-coind-wrong\/Conat-coind\n\nConat-coind'' : \u2200 (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n \u2200 {n} \u2192 A n \u2192 Conat n\nConat-coind'' A h An = Conat-coind-wrong A h An\n\n-- 22 December 2013: We cannot prove Conat-coind-wrong'' using\n-- Conat-coind.\nConat-coind-wrong'' : \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')) \u2192\n A n \u2192 Conat n\nConat-coind-wrong'' A h An = Conat-coind A {!!} An\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"160cee2820de60aaa4ac5af26e55e79c762eaa39","subject":"forking-lemma: some progress","message":"forking-lemma: some progress\n","repos":"crypto-agda\/crypto-agda","old_file":"forking-lemma.agda","new_file":"forking-lemma.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Type using (Type)\nopen import Function.NP\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Vec.NP hiding (sum)\nopen import Data.Maybe\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Two.Base hiding (_==_; _\u00b2)\nopen import Data.Fin.NP as Fin hiding (_+_; _-_; _\u2264_)\nopen import Data.Product.NP\nopen import Relation.Nullary\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP hiding (J)\n\nmodule forking-lemma where\n\n{-\n_\u22651 : \u2200 {n} \u2192 Fin n \u2192 \ud835\udfda\nzero \u22651 = 0\u2082\nsuc _ \u22651 = 1\u2082\n-}\n\nreplace : \u2200 {A : Type} {q} (I : Fin q)\n (hs hs' : Vec A q) \u2192 Vec A q\nreplace zero hs hs' = hs'\nreplace (suc I) (h \u2237 hs) (_ \u2237 hs') = h \u2237 replace I hs hs'\n\ntest-replace : replace (suc zero) (40 \u2237 41 \u2237 42 \u2237 []) (60 \u2237 61 \u2237 62 \u2237 []) \u2261 40 \u2237 61 \u2237 62 \u2237 []\ntest-replace = refl\n\n\u2261-prefix : \u2200 {A : Type} {q} (I : Fin (suc q))\n (v\u2080 v\u2081 : Vec A q) \u2192 Type\n\u2261-prefix zero _ _ = \ud835\udfd9\n\u2261-prefix (suc I) (x\u2080 \u2237 v\u2080) (x\u2081 \u2237 v\u2081) = (x\u2080 \u2261 x\u2081) \u00d7 \u2261-prefix I v\u2080 v\u2081\n\n\u2261-prefix (suc ()) [] []\n\npostulate\n -- Pub : Type\n Res : Type -- Side output\n {-RndIG : Type-}\n\n -- IG : RndIG \u2192 Pub\n\n q : \u2115\n\n instance\n q\u22651 : q \u2265 1\n\n #h : \u2115\n #h\u22652 : #h \u2265 2\n\n #\u03c1 : \u2115\n #\u03c1\u22651 : #\u03c1 \u2265 1\n\nRndAdv : Type -- Coin\nRndAdv = Fin #\u03c1\n\nH : Type\nH = Fin #h\n\nAdv = {-(x : Pub)-}(hs : Vec H q)(\u03c1 : RndAdv) \u2192 Fin (suc q) \u00d7 Res\n\npostulate\n A : Adv\n-- A x hs \u03c1\n-- A x hs = \u03c1 \u2190$ ...H ; A x hs \u03c1\n\n-- IO : Type \u2192 Type\n-- print : String \u2192 IO ()\n-- #r : \u2115\n-- #r\u22651 : #r \u2265 1\n-- Rnd = Fin #r\n-- random : IO Rnd\n-- run : (A : Input \u2192 Rnd \u2192 Res) \u2192 Input \u2192 IO Res\n-- run A i = do r \u2190 random\n-- return (A i r)\n\n-- Not used yet\nwell-def : Adv \u2192 Type\nwell-def A =\n \u2200 {-x-} hs \u03c1 \u2192\n case A {-x-} hs \u03c1 of \u03bb { (I , \u03c3) \u2192\n \u2200 hs' \u2192 \u2261-prefix I hs hs' \u2192 A {-x-} hs' \u03c1 \u2261 (I , \u03c3) }\n\nrecord \u03a9 : Type where\n field\n -- rIG : RndIG\n hs hs' : Vec H q\n \u03c1 : RndAdv\n\nEvent = \u03a9 \u2192 \ud835\udfda\n\n#\u03a9 = ((#h ^ q) ^2) * #\u03c1\n-- \u03a9 \u2243 Fin #\u03a9\n\ninstance\n #\u03a9\u22651 : #\u03a9 \u2265 1\n #\u03a9\u22651 = {!!}\n\nmodule M (r : \u03a9) where\n open \u03a9 r\n\n F' : (I-1 : Fin q)(\u03c3 : Res) \u2192 Maybe (Res \u00d7 Res)\n F' I-1 \u03c3 =\n case I=I' \u2227 h=h'\n 0: nothing\n 1: just (\u03c3 , \u03c3')\n module F' where\n res' = A (replace I-1 hs hs') \u03c1\n I' = fst res'\n \u03c3' = snd res'\n h = hs \u203c I-1\n h' = hs' \u203c I-1\n I = suc I-1\n I=I' = I == I'\n h=h' = h == h'\n\n F : Maybe (Res \u00d7 Res)\n F =\n case I of \u03bb\n { zero \u2192 nothing\n ; (suc I-1) \u2192 F' I-1 \u03c3\n }\n module F where\n res = A hs \u03c1\n I = fst res\n \u03c3 = snd res\n -- I\u22651 = not (I == zero)\n\nI\u22651\u2227_ : (f : \u03a9 \u2192 Fin q \u2192 Res \u2192 \ud835\udfda) \u2192 Event\n(I\u22651\u2227 f) r = case I of \u03bb\n { zero \u2192 0\u2082\n ; (suc I-1) \u2192 f r I-1 \u03c3\n }\n where open M.F r\n\nI\u22651 I\u22651\u2227I=I' I\u22651\u2227h=h' I\u22651\u2227I=I'\u2227h\u2262h' : Event\n\nI\u22651 = I\u22651\u2227 \u03bb _ _ _ \u2192 1\u2082\n\nI\u22651\u2227I=I' = I\u22651\u2227 \u03bb r I-1 \u03c3 \u2192 let open M.F' r I-1 \u03c3 in I=I'\nI\u22651\u2227h=h' = I\u22651\u2227 \u03bb r I-1 \u03c3 \u2192 let open M.F' r I-1 \u03c3 in h=h'\nI\u22651\u2227I=I'\u2227h\u2262h' = I\u22651\u2227 \u03bb r I-1 \u03c3 \u2192 let open M.F' r I-1 \u03c3 in I=I' \u2227 not h=h'\n\nI=1+_ I=I'=1+_ : (i : Fin q) \u2192 Event\n\nI=1+ i = I\u22651\u2227 \u03bb r I-1 \u03c3 \u2192 let open M.F' r I-1 \u03c3 in I == suc i\nI=I'=1+ i = I\u22651\u2227 \u03bb r I-1 \u03c3 \u2192 let open M.F' r I-1 \u03c3 in I == suc i \u2227 I=I'\n\n-- Acceptance event for A\nacc : Event\nacc = I\u22651\n\nfrk : Event\nfrk = is-just \u2218 M.F\n\ninstance\n #h\u22651 : #h \u2265 1\n #h\u22651 = \u2115\u2264.trans (s\u2264s z\u2264n) #h\u22652\n\ninfix 0 _\u2265'_\ninfixr 2 _\u2265\u27e8_\u27e9_ _\u2261\u27e8_\u27e9_ _\u2261\u27e8by-definition\u27e9_\ninfix 2 _\u220e\n\npostulate\n -- [0,1] : Type\n \u211d : Type\n -- [0,1]\u25b9\u211d : [0,1] \u2192 \u211d\n -- x - y requires x \u2265 y\n -- _\u00b7_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n _-_ _\u00b7_ : \u211d \u2192 \u211d \u2192 \u211d\n -- _-_ :\n -- _-_ : [0,1] \u2192 [0,1] \u2192 \u211d\n -- _\u2265'_ : [0,1] \u2192 [0,1] \u2192 Type\n _\u2265'_ : \u211d \u2192 \u211d \u2192 Type\n 1\/_ : (d : \u2115){{_ : d \u2265 1}} \u2192 \u211d\n\n _\u2265\u27e8_\u27e9_ : \u2200 x {y} \u2192 x \u2265' y \u2192 \u2200 {z} \u2192 y \u2265' z \u2192 x \u2265' z\n _\u2261\u27e8_\u27e9_ : \u2200 x {y} \u2192 x \u2261 y \u2192 \u2200 {z} \u2192 y \u2265' z \u2192 x \u2265' z\n _\u220e : \u2200 x \u2192 x \u2265' x\n\n \u2115\u25b9\u211d : \u2115 \u2192 \u211d\n\n sum : Event \u2192 \u2115\n sum-ext : {A B : Event} \u2192 (\u2200 r \u2192 A r \u2261 B r) \u2192 sum A \u2261 sum B\n\n sumFin : (q : \u2115) (f : Fin q \u2192 \u211d) \u2192 \u211d\n\nRndVar = \u03a9 \u2192 \u211d\n\n_\u00b2 : \u211d \u2192 \u211d\nx \u00b2 = x \u00b7 x\n\n_\u00b2' : RndVar \u2192 RndVar\n(X \u00b2') r = (X r)\u00b2\n\npostulate\n E[_] : RndVar \u2192 \u211d\n\n lemma2 : \u2200 X \u2192 E[ X \u00b2' ] \u2265' E[ X ] \u00b2\n\n_\u2261\u27e8by-definition\u27e9_ : \u2200 x {z} \u2192 x \u2265' z \u2192 x \u2265' z\nx \u2261\u27e8by-definition\u27e9 y = x \u2261\u27e8 refl \u27e9 y\n\n-- _\/_ : [0,1] \u2192 (d : \u2115){{_ : d \u2265 1}} \u2192 [0,1]\n_\/_ : \u211d \u2192 (d : \u2115){{_ : d \u2265 1}} \u2192 \u211d\nx \/ y = x \u00b7 1\/ y\n\nPr : Event \u2192 \u211d -- [0,1]\nPr A = \u2115\u25b9\u211d (sum A) \/ #\u03a9\n\nPr-ext : \u2200 {A B : Event} \u2192 (\u2200 r \u2192 A r \u2261 B r) \u2192 Pr A \u2261 Pr B\nPr-ext f = ap (\u03bb x \u2192 \u2115\u25b9\u211d x \/ #\u03a9) (sum-ext f)\n\n-- Lemma 1, equation (3)\nlemma1-3 : Pr frk \u2265' Pr acc \u00b7 ((Pr acc \/ q) - (1\/ #h))\nlemma1-3 = Pr frk\n \u2261\u27e8 {!Pr!} \u27e9\n Pr I\u22651\u2227I=I'\u2227h\u2262h'\n \u2265\u27e8 {!!} \u27e9\n Pr I\u22651\u2227I=I' - Pr I\u22651\u2227h=h'\n \u2261\u27e8 ap (\u03bb x \u2192 Pr I\u22651\u2227I=I' - x) {!!} \u27e9\n Pr I\u22651\u2227I=I' - (Pr I\u22651 \/ #h)\n \u2261\u27e8by-definition\u27e9\n Pr I\u22651\u2227I=I' - (Pr acc \/ #h)\n \u2261\u27e8 {!!} \u27e9\n Pr acc \u00b7 ((Pr acc \/ q) - (1\/ #h))\n \u220e\n\nlemma1-4 : Pr I\u22651\u2227I=I' \u2265' (Pr acc)\u00b2 \/ q\nlemma1-4\n = Pr I\u22651\u2227I=I'\n \u2261\u27e8 {!!} \u27e9\n sumFin q (\u03bb i \u2192 Pr (I=I'=1+ i))\n \u2261\u27e8 {!!} \u27e9 -- Conditional probabilities\n sumFin q (\u03bb i \u2192 Pr (I=1+ i) \u00b7 Pr (I=I'=1+ i))\n \u2261\u27e8 {!!} \u27e9\n (Pr acc)\u00b2 \/ q\n \u220e\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Type using (Type)\nopen import Function.NP\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Vec\nopen import Data.Maybe\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Two.Base hiding (_==_)\nopen import Data.Fin.NP as Fin hiding (_+_; _-_; _\u2264_)\nopen import Data.Product.NP\nopen import Relation.Nullary\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\n\nmodule forking-lemma where\n\n_\u22651 : \u2200 {n} \u2192 Fin n \u2192 \ud835\udfda\nzero \u22651 = 0\u2082\nsuc _ \u22651 = 1\u2082\n\ninfix 8 _\u203c_\n_\u203c_ : \u2200 {n a}{A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_\u203c_ = flip lookup\n\nreplace : \u2200 {A : Type} {q} (I : Fin q)\n (hs hs' : Vec A q) \u2192 Vec A q\nreplace zero hs hs' = hs'\nreplace (suc I) (h \u2237 hs) (_ \u2237 hs') = h \u2237 replace I hs hs'\n\ntest-replace : replace (suc zero) (40 \u2237 41 \u2237 42 \u2237 []) (60 \u2237 61 \u2237 62 \u2237 []) \u2261 40 \u2237 61 \u2237 62 \u2237 []\ntest-replace = refl\n\n\u2261-prefix : \u2200 {A : Type} {q} (I : Fin (suc q))\n (v\u2080 v\u2081 : Vec A q) \u2192 Type\n\u2261-prefix zero _ _ = \ud835\udfd9\n\u2261-prefix (suc I) (x\u2080 \u2237 v\u2080) (x\u2081 \u2237 v\u2081) = (x\u2080 \u2261 x\u2081) \u00d7 \u2261-prefix I v\u2080 v\u2081\n\n\u2261-prefix (suc ()) [] []\n\npostulate\n -- Pub : Type\n Res : Type -- Side output\n RndAdv : Type -- Coin\n {-RndIG : Type-}\n\n -- IG : RndIG \u2192 Pub\n\n q : \u2115\n\n instance\n q\u22651 : q \u2265 1\n\n #h : \u2115\n #h\u22652 : #h \u2265 2\n\nH : Type\nH = Fin #h\n\nAdv = {-(x : Pub)-}(hs : Vec H q)(\u03c1 : RndAdv) \u2192 Fin (suc q) \u00d7 Res\n\npostulate\n A : Adv\n-- A x hs \u03c1\n-- A x hs = \u03c1 \u2190$ ...H ; A x hs \u03c1\n\n-- IO : Type \u2192 Type\n-- print : String \u2192 IO ()\n-- #r : \u2115\n-- #r\u22651 : #r \u2265 1\n-- Rnd = Fin #r\n-- random : IO Rnd\n-- run : (A : Input \u2192 Rnd \u2192 Res) \u2192 Input \u2192 IO Res\n-- run A i = do r \u2190 random\n-- return (A i r)\n\n-- Not used yet\nwell-def : Adv \u2192 Type\nwell-def A =\n \u2200 {-x-} hs \u03c1 \u2192\n case A {-x-} hs \u03c1 of \u03bb { (I , \u03c3) \u2192\n \u2200 hs' \u2192 \u2261-prefix I hs hs' \u2192 A {-x-} hs' \u03c1 \u2261 (I , \u03c3) }\n\nrecord \u03a9 : Type where\n field\n -- rIG : RndIG\n hs hs' : Vec H q\n \u03c1 : RndAdv\n\nEvent = \u03a9 \u2192 \ud835\udfda\n\nmodule M (r : \u03a9) where\n open \u03a9 r\n\n F' : (I-1 : Fin q)(\u03c3 : Res) \u2192 Maybe (Res \u00d7 Res)\n F' I-1 \u03c3 =\n case I=I' \u2227 h=h' of \u03bb\n { 1\u2082 \u2192 just (\u03c3 , \u03c3')\n ; 0\u2082 \u2192 nothing\n }\n module F' where\n res' = A (replace I-1 hs hs') \u03c1\n I' = fst res'\n \u03c3' = snd res'\n h = hs \u203c I-1\n h' = hs' \u203c I-1\n I = suc I-1\n I=I' = I == I'\n h=h' = h == h'\n\n F : Maybe (Res \u00d7 Res)\n F =\n case I of \u03bb\n { zero \u2192 nothing\n ; (suc I-1) \u2192 F' I-1 \u03c3\n }\n module F where\n res = A hs \u03c1\n I = fst res\n \u03c3 = snd res\n I\u22651 = not (I == zero)\n\n-- Acceptance event for A\nacc : Event\nacc r = J \u22651\n where open \u03a9 r\n -- x = IG rIG\n J = fst (A {-x-} hs \u03c1)\n\nfrk : Event\nfrk r = case M.F r of \u03bb { (just _) \u2192 1\u2082 ; nothing \u2192 0\u2082 }\n\ninstance\n #h\u22651 : #h \u2265 1\n #h\u22651 = \u2115\u2264.trans (s\u2264s z\u2264n) #h\u22652\n\ninfix 0 _\u2265'_\ninfixr 2 _\u2265\u27e8_\u27e9_ _\u2261\u27e8_\u27e9_\ninfix 2 _\u220e\n\npostulate\n [0,1] : Type\n Pr : Event \u2192 [0,1]\n _-_ _\u00b7_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n _\u2265'_ : [0,1] \u2192 [0,1] \u2192 Type\n 1\/_ : (d : \u2115){{_ : d \u2265 1}} \u2192 [0,1]\n\n _\u2265\u27e8_\u27e9_ : \u2200 x {y} \u2192 x \u2265' y \u2192 \u2200 {z} \u2192 y \u2265' z \u2192 x \u2265' z\n _\u2261\u27e8_\u27e9_ : \u2200 x {y} \u2192 x \u2261 y \u2192 \u2200 {z} \u2192 y \u2265' z \u2192 x \u2265' z\n _\u220e : \u2200 x \u2192 x \u2265' x\n\n_\/_ : [0,1] \u2192 (d : \u2115){{_ : d \u2265 1}} \u2192 [0,1]\nx \/ y = x \u00b7 1\/ y\n\n{-\nlemma3 : Pr frk \u2265' Pr acc \u00b7 ((Pr acc \/ q) - (1\/ #h))\nlemma3 = Pr frk\n \u2261\u27e8 {!!} \u27e9\n Pr {!\u03bb r \u2192 I=I' r \u2227 I\u22651 r!}\n \u2265\u27e8 {!!} \u27e9\n Pr acc \u00b7 ((Pr acc \/ q) - (1\/ #h))\n \u220e\n where\n open M.F\n -- open M.F' I-1 \u03c3\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"51004af03df620436d3816d6e0778bba60c327f4","subject":"Vec: +map-ext +sum-distrib\u02e1","message":"Vec: +map-ext +sum-distrib\u02e1\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Vec\/NP.agda","new_file":"lib\/Data\/Vec\/NP.agda","new_contents":"module Data.Vec.NP where\n\nimport Level as L\nopen import Category.Applicative\nopen import Data.Vec public hiding (_\u229b_; zipWith; zip; map; applicative)\nopen import Data.Nat.NP using (\u2115; suc; zero; _+_; _*_; module \u2115\u00b0)\nopen import Data.Fin renaming (_+_ to _+\u1da0_)\nimport Data.Fin.Props as F\nopen import Data.Bool\nopen import Data.Product hiding (map; zip; swap)\nopen import Function.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Function.Bijection.SyntaxKit\n\nmodule waiting-for-a-fix-in-the-stdlib where\n\n infixl 4 _\u229b_\n\n _\u229b_ : \u2200 {a b n} {A : Set a} {B : Set b} \u2192\n Vec (A \u2192 B) n \u2192 Vec A n \u2192 Vec B n\n _\u229b_ {n = zero} fs xs = []\n _\u229b_ {n = suc n} fs xs = head fs (head xs) \u2237 (tail fs \u229b tail xs)\n\n applicative : \u2200 {a n} \u2192 RawApplicative (\u03bb (A : Set a) \u2192 Vec A n)\n applicative = record\n { pure = replicate\n ; _\u229b_ = _\u229b_\n }\n\n map : \u2200 {a b n} {A : Set a} {B : Set b} \u2192\n (A \u2192 B) \u2192 Vec A n \u2192 Vec B n\n map f xs = replicate f \u229b xs\n\n zipWith : \u2200 {a b c n} {A : Set a} {B : Set b} {C : Set c} \u2192\n (A \u2192 B \u2192 C) \u2192 Vec A n \u2192 Vec B n \u2192 Vec C n\n zipWith _\u2295_ xs ys = replicate _\u2295_ \u229b xs \u229b ys\n\n zip : \u2200 {a b n} {A : Set a} {B : Set b} \u2192\n Vec A n \u2192 Vec B n \u2192 Vec (A \u00d7 B) n\n zip = zipWith _,_\n\n tabulate-\u2218 : \u2200 {n a b} {A : Set a} {B : Set b}\n (f : A \u2192 B) (g : Fin n \u2192 A) \u2192\n tabulate (f \u2218 g) \u2261 map f (tabulate g)\n tabulate-\u2218 {zero} f g = refl\n tabulate-\u2218 {suc n} f g =\n \u2261.cong (_\u2237_ (f (g zero))) (tabulate-\u2218 f (g \u2218 suc))\n\n -- map is functorial.\n\n map-id : \u2200 {a n} {A : Set a} \u2192 map id \u2257 id {A = Vec A n}\n map-id [] = refl\n map-id (x \u2237 xs) = \u2261.cong (_\u2237_ x) (map-id xs)\n\n map-\u2218 : \u2200 {a b c n} {A : Set a} {B : Set b} {C : Set c}\n (f : B \u2192 C) (g : A \u2192 B) \u2192\n _\u2257_ {A = Vec A n} (map (f \u2218 g)) (map f \u2218 map g)\n map-\u2218 f g [] = refl\n map-\u2218 f g (x \u2237 xs) = \u2261.cong (_\u2237_ (f (g x))) (map-\u2218 f g xs)\n\n map-ext : \u2200 {a b} {A : Set a} {B : Set b} {f g : A \u2192 B} {n} \u2192 f \u2257 g \u2192 map f \u2257 map {n = n} g\n map-ext f\u2257g [] = refl\n map-ext f\u2257g (x \u2237 xs) rewrite f\u2257g x | map-ext f\u2257g xs = refl\n\nopen waiting-for-a-fix-in-the-stdlib public\n\n-- Trying to get rid of the foldl in the definition of reverse and\n-- without using equations on natural numbers.\n-- In the end that's not very convincing.\nmodule Alternative-Reverse where\n rev-+ : \u2115 \u2192 \u2115 \u2192 \u2115\n rev-+ zero = id\n rev-+ (suc x) = rev-+ x \u2218 suc\n\n rev-app : \u2200 {a} {A : Set a} {m n} \u2192\n Vec A n \u2192 Vec A m \u2192 Vec A (rev-+ n m)\n rev-app [] = id\n rev-app (x \u2237 xs) = rev-app xs \u2218 _\u2237_ x\n\n rev-aux : \u2200 {a} {A : Set a} {m} n \u2192\n Vec A (rev-+ n zero) \u2192\n (\u2200 {m} \u2192 A \u2192 Vec A (rev-+ n m) \u2192 Vec A (rev-+ n (suc m))) \u2192\n Vec A m \u2192 Vec A (rev-+ n m)\n rev-aux m acc op [] = acc\n rev-aux m acc op (x \u2237 xs) = rev-aux (suc m) (op x acc) op xs\n\n alt-reverse : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n alt-reverse = rev-aux 0 [] _\u2237_\n\nvuncurry : \u2200 {n a b} {A : Set a} {B : Set b} (f : A \u2192 Vec A n \u2192 B) \u2192 Vec A (1 + n) \u2192 B\nvuncurry f (x \u2237 xs) = f x xs\n\ncount\u1da0 : \u2200 {n a} {A : Set a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 Fin (suc n)\ncount\u1da0 pred = foldr (Fin \u2218 suc) (\u03bb x \u2192 if pred x then suc else inject\u2081) zero\n\ncount : \u2200 {n a} {A : Set a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 \u2115\ncount pred = to\u2115 \u2218 count\u1da0 pred\n\ncount-\u2218 : \u2200 {n a b} {A : Set a} {B : Set b} (f : A \u2192 B) (pred : B \u2192 Bool) \u2192\n count {n} (pred \u2218 f) \u2257 count pred \u2218 map f\ncount-\u2218 f pred [] = refl\ncount-\u2218 f pred (x \u2237 xs) with pred (f x)\n... | true rewrite count-\u2218 f pred xs = refl\n... | false rewrite F.inject\u2081-lemma (count\u1da0 pred (map f xs))\n | F.inject\u2081-lemma (count\u1da0 (pred \u2218 f) xs)\n | count-\u2218 f pred xs = refl\n\ncount-++ : \u2200 {m n a} {A : Set a} (pred : A \u2192 Bool) (xs : Vec A m) (ys : Vec A n)\n \u2192 count pred (xs ++ ys) \u2261 count pred xs + count pred ys\ncount-++ pred [] ys = refl\ncount-++ pred (x \u2237 xs) ys with pred x\n... | true rewrite count-++ pred xs ys = refl\n... | false rewrite F.inject\u2081-lemma (count\u1da0 pred (xs ++ ys))\n | F.inject\u2081-lemma (count\u1da0 pred xs) | count-++ pred xs ys = refl\n\next-count\u1da0 : \u2200 {n a} {A : Set a} {f g : A \u2192 Bool} \u2192 f \u2257 g \u2192 (xs : Vec A n) \u2192 count\u1da0 f xs \u2261 count\u1da0 g xs\next-count\u1da0 f\u2257g [] = refl\next-count\u1da0 f\u2257g (x \u2237 xs) rewrite ext-count\u1da0 f\u2257g xs | f\u2257g x = refl\n\nfilter : \u2200 {n a} {A : Set a} (pred : A \u2192 Bool) (xs : Vec A n) \u2192 Vec A (count pred xs)\nfilter pred [] = []\nfilter pred (x \u2237 xs) with pred x\n... | true = x \u2237 filter pred xs\n... | false rewrite F.inject\u2081-lemma (count\u1da0 pred xs) = filter pred xs\n\n\u03b7 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7 = tabulate \u2218 flip lookup\n\n\u03b7\u2032 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7\u2032 {zero} = \u03bb _ \u2192 []\n\u03b7\u2032 {suc n} = \u03bb xs \u2192 head xs \u2237 \u03b7 (tail xs)\n\nshallow-\u03b7 : \u2200 {n a} {A : Set a} (xs : Vec A (1 + n)) \u2192 xs \u2261 head xs \u2237 tail xs\nshallow-\u03b7 (x \u2237 xs) = \u2261.refl\n\nuncons : \u2200 {n a} {A : Set a} \u2192 Vec A (1 + n) \u2192 (A \u00d7 Vec A n)\nuncons (x \u2237 xs) = x , xs\n\nsplitAt\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A m \u00d7 Vec A n\nsplitAt\u2032 m xs = case splitAt m xs of \u03bb { (ys , zs , _) \u2192 (ys , zs) }\n\n++-decomp : \u2200 {m n a} {A : Set a} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2261 (zs ++ ts) \u2192 (xs \u2261 zs \u00d7 ys \u2261 ts)\n++-decomp {zero} {xs = []} {[]} p = refl , p\n++-decomp {suc m} {xs = x \u2237 xs} {z \u2237 zs} eq with ++-decomp {m} {xs = xs} {zs} (cong tail eq)\n... | (q\u2081 , q\u2082) = (cong\u2082 _\u2237_ (cong head eq) q\u2081) , q\u2082\n\n{-\nopen import Data.Vec.Equality\n\nmodule Here {a} {A : Set a} where\n open Equality (\u2261.setoid A)\n \u2248-splitAt : \u2200 {m n} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2248 (zs ++ ts) \u2192 (xs \u2248 zs \u00d7 ys \u2248 ts)\n \u2248-splitAt {zero} {xs = []} {[]} p = []-cong , p\n \u2248-splitAt {suc m} {xs = x \u2237 xs} {z \u2237 zs} (x\u00b9\u2248x\u00b2 \u2237-cong p) with \u2248-splitAt {m} {xs = xs} {zs} p\n ... | (q\u2081 , q\u2082) = x\u00b9\u2248x\u00b2 \u2237-cong q\u2081 , q\u2082\n-}\n\n++-inj\u2081 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 xs \u2261 ys\n++-inj\u2081 xs ys eq = proj\u2081 (++-decomp eq)\n\n++-inj\u2082 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 zs \u2261 ts\n++-inj\u2082 xs ys eq = proj\u2082 (++-decomp {xs = xs} {ys} eq)\n\ntake-\u2237 : \u2200 {m a} {A : Set a} n x (xs : Vec A (n + m)) \u2192 take (suc n) (x \u2237 xs) \u2261 x \u2237 take n xs\ntake-\u2237 n x xs with splitAt n xs\ntake-\u2237 _ _ ._ | _ , _ , refl = refl\n\ndrop-\u2237 : \u2200 {m a} {A : Set a} n x (xs : Vec A (n + m)) \u2192 drop (suc n) (x \u2237 xs) \u2261 drop n xs\ndrop-\u2237 n x xs with splitAt n xs\ndrop-\u2237 _ _ ._ | _ , _ , refl = refl\n\ntake-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 take m (xs ++ ys) \u2261 xs\ntake-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ntake-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2081 xs\u2081 xs eq)\n\ndrop-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 drop m (xs ++ ys) \u2261 ys\ndrop-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ndrop-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2082 xs\u2081 xs eq)\n\ntake-drop-lem : \u2200 m {n} {a} {A : Set a} (xs : Vec A (m + n)) \u2192 take m xs ++ drop m xs \u2261 xs\ntake-drop-lem m xs with splitAt m xs\ntake-drop-lem m .(ys ++ zs) | ys , zs , refl = refl\n\ntake-them-all : \u2200 n {a} {A : Set a} (xs : Vec A (n + 0)) \u2192 take n xs ++ [] \u2261 xs\ntake-them-all n xs with splitAt n xs\ntake-them-all n .(ys ++ []) | ys , [] , refl = refl\n\ndrop\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A n\ndrop\u2032 zero = id\ndrop\u2032 (suc m) = drop\u2032 m \u2218 tail\n\ndrop\u2032-spec : \u2200 {a} {A : Set a} m {n} \u2192 drop\u2032 {A = A} m {n} \u2257 drop m {n}\ndrop\u2032-spec zero xs = refl\ndrop\u2032-spec (suc m) (x \u2237 xs) rewrite drop\u2032-spec m xs | drop-\u2237 m x xs = refl\n\ntake\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A m\ntake\u2032 zero _ = []\ntake\u2032 (suc m) xs = head xs \u2237 take\u2032 m (tail xs)\n\ntake\u2032-spec : \u2200 {a} {A : Set a} m {n} \u2192 take\u2032 {A = A} m {n} \u2257 take m {n}\ntake\u2032-spec zero xs = refl\ntake\u2032-spec (suc m) (x \u2237 xs) rewrite take\u2032-spec m xs | take-\u2237 m x xs = refl\n\nswap : \u2200 m {n} {a} {A : Set a} \u2192 Vec A (m + n) \u2192 Vec A (n + m)\nswap m xs = drop m xs ++ take m xs\n\nswap-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 swap m (xs ++ ys) \u2261 ys ++ xs\nswap-++ m xs ys rewrite drop-++ m xs ys | take-++ m xs ys = refl\n\nrewire : \u2200 {a i o} {A : Set a} \u2192 (Fin o \u2192 Fin i) \u2192 Vec A i \u2192 Vec A o\nrewire f v = tabulate (flip lookup v \u2218 f)\n\nRewireTbl : (i o : \u2115) \u2192 Set\nRewireTbl i o = Vec (Fin i) o\n\nrewireTbl : \u2200 {a i o} {A : Set a} \u2192 RewireTbl i o \u2192 Vec A i \u2192 Vec A o\nrewireTbl tbl v = map (flip lookup v) tbl\n\non\u1d62 : \u2200 {a} {A : Set a} (f : A \u2192 A) {n} (i : Fin n) \u2192 Vec A n \u2192 Vec A n\non\u1d62 f zero (x \u2237 xs) = f x \u2237 xs\non\u1d62 f (suc i) (x \u2237 xs) = x \u2237 on\u1d62 f i xs\n\n-- Exchange elements at positions 0 and 1 of a given vector\n-- (this only apply if the vector is long enough).\n0\u21941 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n0\u21941 (x\u2080 \u2237 x\u2081 \u2237 xs) = x\u2081 \u2237 x\u2080 \u2237 xs\n0\u21941 xs = xs\n\n\u229b-dist-0\u21941 : \u2200 {n a} {A : Set a} (fs : Vec (Endo A) n) xs \u2192 0\u21941 fs \u229b 0\u21941 xs \u2261 0\u21941 (fs \u229b xs)\n\u229b-dist-0\u21941 _ [] = refl\n\u229b-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n\u229b-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\nmap-tail : \u2200 {m n a} {A : Set a} \u2192 (Vec A m \u2192 Vec A n) \u2192 Vec A (suc m) \u2192 Vec A (suc n)\nmap-tail f (x \u2237 xs) = x \u2237 f xs\n\nmap-tail-id : \u2200 {n a} {A : Set a} \u2192 map-tail id \u2257 id {A = Vec A (suc n)}\nmap-tail-id (x \u2237 xs) = \u2261.refl\n\nmap-tail\u2218map-tail : \u2200 {m n o a} {A : Set a}\n (f : Vec A o \u2192 Vec A m)\n (g : Vec A n \u2192 Vec A o)\n \u2192 map-tail f \u2218 map-tail g \u2257 map-tail (f \u2218 g)\nmap-tail\u2218map-tail f g (x \u2237 xs) = refl\n\nmap-tail-\u2257 : \u2200 {m n a} {A : Set a} (f g : Vec A m \u2192 Vec A n) \u2192 f \u2257 g \u2192 map-tail f \u2257 map-tail g\nmap-tail-\u2257 f g f\u2257g (x \u2237 xs) rewrite f\u2257g xs = refl\n\n-- \u27e80\u21941+ i \u27e9: Exchange elements at position 0 and 1+i.\n\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A (1 + n) \u2192 Vec A (1 + n)\n\u27e80\u21941+ zero \u27e9 = 0\u21941\n\u27e80\u21941+ suc i \u27e9 = 0\u21941 \u2218 (map-tail \u27e80\u21941+ i \u27e9) \u2218 0\u21941\n {- 0 1 2 3 ... i 1+i ... n\n 1 0 2 3 ... i 1+i ... n\n 1 1+i 2 3 ... i 0 ... n\n\n 1+i 1 2 3 ... i 0 ... n\n -}\n\n-- \u27e80\u2194 i \u27e9: Exchange elements at position 0 and i.\n\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u27e80\u2194 zero \u27e9 = id\n\u27e80\u2194 suc i \u27e9 = \u27e80\u21941+ i \u27e9\n\n\u27e80\u2194zero\u27e9 : \u2200 {n a} {A : Set a} \u2192 \u27e80\u2194 zero \u27e9 \u2257 id {A = Vec A (suc n)}\n\u27e80\u2194zero\u27e9 _ = \u2261.refl\n\n_\u00b2 : \u2200 {a} {A : Set a} \u2192 Endo (Endo A)\nf \u00b2 = f \u2218 f\n\nmodule \u27e8\u2194\u27e9 {a} (A : Set a) where\n\n \u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Vec A n \u2192 Vec A n\n \u27e8 zero \u2194 j \u27e9 = \u27e80\u2194 j \u27e9\n \u27e8 i \u2194 zero \u27e9 = \u27e80\u2194 i \u27e9\n \u27e8 suc i \u2194 suc j \u27e9 = map-tail \u27e8 i \u2194 j \u27e9\n-- \u27e8 # 0 \u2194 # 1 \u27e9\n\n comm : \u2200 {n} (i j : Fin n) \u2192 \u27e8 i \u2194 j \u27e9 \u2257 \u27e8 j \u2194 i \u27e9\n comm zero zero _ = \u2261.refl\n comm zero (suc _) _ = \u2261.refl\n comm (suc _) zero _ = \u2261.refl\n comm (suc i) (suc j) (x \u2237 xs) rewrite comm i j xs = \u2261.refl\n\n 0\u21941\u00b2-cancel : \u2200 {n} \u2192 0\u21941 \u00b2 \u2257 id {A = Vec A n}\n 0\u21941\u00b2-cancel [] = refl\n 0\u21941\u00b2-cancel (_ \u2237 []) = refl\n 0\u21941\u00b2-cancel (x \u2237 x\u2081 \u2237 xs) = refl\n\n \u27e80\u21941+_\u27e9\u00b2-cancel : \u2200 {n} (i : Fin n) \u2192 \u27e80\u21941+ i \u27e9 \u00b2 \u2257 id {A = Vec A (1 + n)}\n \u27e80\u21941+ zero \u27e9\u00b2-cancel xs = 0\u21941\u00b2-cancel xs\n \u27e80\u21941+ suc i \u27e9\u00b2-cancel xs\n rewrite 0\u21941\u00b2-cancel (map-tail \u27e80\u21941+ i \u27e9 (0\u21941 xs))\n | map-tail\u2218map-tail \u27e80\u21941+ i \u27e9 \u27e80\u21941+ i \u27e9 (0\u21941 xs)\n | map-tail-\u2257 _ _ \u27e80\u21941+ i \u27e9\u00b2-cancel (0\u21941 xs)\n | map-tail-id (0\u21941 xs)\n | 0\u21941\u00b2-cancel xs = refl\n\n \u27e80\u2194_\u27e9\u00b2-cancel : \u2200 {n} (i : Fin n) \u2192 \u27e80\u2194 i \u27e9 \u00b2 \u2257 id {A = Vec A n}\n \u27e80\u2194 zero \u27e9\u00b2-cancel _ = \u2261.refl\n \u27e80\u2194 suc i \u27e9\u00b2-cancel xs = \u27e80\u21941+ i \u27e9\u00b2-cancel xs\n\n \u27e8_\u2194_\u27e9\u00b2-cancel : \u2200 {n} (i j : Fin n) \u2192 \u27e8 i \u2194 j \u27e9 \u00b2 \u2257 id\n \u27e8 zero \u2194 j \u27e9\u00b2-cancel xs = \u27e80\u2194 j \u27e9\u00b2-cancel xs\n \u27e8 suc i \u2194 zero \u27e9\u00b2-cancel xs = \u27e80\u21941+ i \u27e9\u00b2-cancel xs\n \u27e8 suc i \u2194 suc j \u27e9\u00b2-cancel xs\n rewrite map-tail\u2218map-tail \u27e8 i \u2194 j \u27e9 \u27e8 i \u2194 j \u27e9 xs\n | map-tail-\u2257 _ _ \u27e8 i \u2194 j \u27e9\u00b2-cancel xs\n | map-tail-id xs = refl\n\n lem01maptail2 : \u2200 {m n a} {A : Set a} (f : Vec A m \u2192 Vec A n) \u2192\n 0\u21941 \u2218 map-tail (map-tail f) \u2218 0\u21941 \u2257 map-tail (map-tail f)\n lem01maptail2 _ (_ \u2237 _ \u2237 _) = refl\n\n \u2194-refl : \u2200 {n} (i : Fin n) \u2192 \u27e8 i \u2194 i \u27e9 \u2257 id\n \u2194-refl zero _ = refl\n \u2194-refl (suc i) xs rewrite map-tail-\u2257 _ _ (\u2194-refl i) xs = map-tail-id xs\n\n {-\n lem1+ : \u2200 {n} (i j : Fin n) \u2192 \u27e80\u21941+ i \u27e9 \u2218 \u27e80\u21941+ j \u27e9 \u2218 \u27e80\u21941+ i \u27e9 \u2257 map-tail \u27e8 i \u2194 j \u27e9\n lem1+ zero zero xs = {!!}\n lem1+ zero (suc j) xs = {!!}\n lem1+ (suc i) zero xs = {!!}\n lem1+ (suc i) (suc j) xs\n rewrite 0\u21941\u00b2-cancel (map-tail \u27e80\u21941+ i \u27e9 (0\u21941 xs))\n | 0\u21941\u00b2-cancel (map-tail \u27e80\u21941+ j \u27e9 (map-tail \u27e80\u21941+ i \u27e9 (0\u21941 xs)))\n | map-tail\u2218map-tail \u27e80\u21941+ j \u27e9 \u27e80\u21941+ i \u27e9 (0\u21941 xs)\n | map-tail\u2218map-tail \u27e80\u21941+ i \u27e9 (\u27e80\u21941+ j \u27e9 \u2218 \u27e80\u21941+ i \u27e9) (0\u21941 xs)\n | map-tail-\u2257 _ _ (lem1+ i j) (0\u21941 xs)\n | lem01maptail2 \u27e8 i \u2194 j \u27e9 xs\n = refl\n\n lem : \u2200 {n} (i j : Fin n) \u2192 \u27e80\u2194 i \u27e9 \u2218 \u27e80\u2194 j \u27e9 \u2218 \u27e80\u2194 i \u27e9 \u2257 \u27e8 i \u2194 j \u27e9\n lem zero j xs = refl\n lem (suc i) zero xs = {!\u27e80\u21941+ i \u27e9\u00b2-cancel xs!}\n lem (suc i) (suc j) xs = (\u27e80\u21941+ i \u27e9 \u2218 \u27e80\u21941+ j \u27e9 \u2218 \u27e80\u21941+ i \u27e9) xs\n \u2261\u27e8 lem1+ i j xs \u27e9\n \u27e8 suc i \u2194 suc j \u27e9 xs\n \u220e where open \u2261-Reasoning\n test = {!!}\n -}\n{-\n lem : \u2200 {n} (i j k : Fin n) \u2192 \u27e8 i \u2194 j \u27e9 \u2218 \u27e8 i \u2194 k \u27e9 \u2218 \u27e8 i \u2194 j \u27e9 \u2257 \u27e8 j \u2194 k \u27e9\n lem i j k xs = (\u27e8 i \u2194 j \u27e9 \u2218 \u27e8 i \u2194 k \u27e9 \u2218 \u27e8 i \u2194 j \u27e9) xs\n (\u27e8 i \u2194 j \u27e9 \u2218 \u27e8 i \u2194 k \u27e9 \u2218 id \u2218 \u27e8 i \u2194 j \u27e9) xs\n (\u27e8 i \u2194 j \u27e9 \u2218 \u27e8 i \u2194 k \u27e9 \u2218 \u27e8 i \u2194 k \u27e9 \u2218 \u27e8 i \u2194 k \u27e9 \u2218 \u27e8 i \u2194 j \u27e9) xs\n \u2261\u27e8 {!!} \u27e9\n \u27e8 j \u2194 k \u27e9 xs\n \u220e where open \u2261-Reasoning\n-}\n\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u27e8_\u2194_\u27e9 i j = \u27e8\u2194\u27e9.\u27e8_\u2194_\u27e9 _ i j\n\nmodule PermutationSyntax where\n infixr 1 _`\u204f_\n data Perm : \u2115 \u2192 Set where\n `id : \u2200 {n} \u2192 Perm n\n `0\u21941 : \u2200 {n} \u2192 Perm (2 + n)\n _`\u204f_ : \u2200 {n} \u2192 Perm n \u2192 Perm n \u2192 Perm n\n `tl : \u2200 {n} \u2192 Perm n \u2192 Perm (1 + n)\n\n _\u207b\u00b9 : \u2200 {n} \u2192 Endo (Perm n)\n `id \u207b\u00b9 = `id\n (f\u2080 `\u204f f\u2081) \u207b\u00b9 = f\u2081 \u207b\u00b9 `\u204f f\u2080 \u207b\u00b9\n `0\u21941 \u207b\u00b9 = `0\u21941\n (`tl f) \u207b\u00b9 = `tl (f \u207b\u00b9)\n\n `\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Perm (1 + n)\n `\u27e80\u21941+ zero \u27e9 = `0\u21941\n `\u27e80\u21941+ suc i \u27e9 = `0\u21941 `\u204f `tl `\u27e80\u21941+ i \u27e9 `\u204f `0\u21941\n\n `\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Perm n\n `\u27e80\u2194 zero \u27e9 = `id\n `\u27e80\u2194 suc i \u27e9 = `\u27e80\u21941+ i \u27e9\n\n `\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Perm n\n `\u27e8 zero \u2194 j \u27e9 = `\u27e80\u2194 j \u27e9\n `\u27e8 i \u2194 zero \u27e9 = `\u27e80\u2194 i \u27e9\n `\u27e8 suc i \u2194 suc j \u27e9 = `tl `\u27e8 i \u2194 j \u27e9\n\nmodule PermutationSemantics {a} {A : Set a} where\n open PermutationSyntax\n\n eval : \u2200 {n} \u2192 Perm n \u2192 Endo (Vec A n)\n eval `id = id\n eval (f `\u204f g) = eval g \u2218 eval f\n eval `0\u21941 = 0\u21941\n eval (`tl f) = \u03bb xs \u2192 head xs \u2237 eval f (tail xs)\n\n infixr 9 _\u2219_\n _\u2219_ : \u2200 {n} \u2192 Perm n \u2192 Endo (Vec A n)\n _\u2219_ = eval\n\n `\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) (xs : Vec A (suc n)) \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n `\u27e80\u21941+ zero \u27e9-spec xs = refl\n `\u27e80\u21941+ suc i \u27e9-spec (x \u2237 y \u2237 xs) rewrite `\u27e80\u21941+ i \u27e9-spec (x \u2237 xs) = refl\n\n `\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) (xs : Vec A n) \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n `\u27e80\u2194 zero \u27e9-spec xs = refl\n `\u27e80\u2194 suc i \u27e9-spec xs = `\u27e80\u21941+ i \u27e9-spec xs\n\n _\u2257\u2032_ : \u2200 {n} \u2192 Perm n \u2192 Perm n \u2192 Set _\n f \u2257\u2032 g = \u2200 xs \u2192 f \u2219 xs \u2261 g \u2219 xs\n\n open \u27e8\u2194\u27e9 A hiding (\u27e8_\u2194_\u27e9)\n\n _\u207b\u00b9-inverse : \u2200 {n} (f : Perm n) \u2192 (f `\u204f f \u207b\u00b9) \u2257\u2032 `id\n (`id \u207b\u00b9-inverse) xs = refl\n ((f `\u204f g) \u207b\u00b9-inverse) xs\n rewrite (g \u207b\u00b9-inverse) (f \u2219 xs)\n | (f \u207b\u00b9-inverse) xs = refl\n (`0\u21941 \u207b\u00b9-inverse) xs = 0\u21941\u00b2-cancel xs\n ((`tl f) \u207b\u00b9-inverse) (x \u2237 xs)\n rewrite (f \u207b\u00b9-inverse) xs = refl\n\n _\u207b\u00b9-involutive : \u2200 {n} (f : Perm n) \u2192 (f \u207b\u00b9) \u207b\u00b9 \u2257\u2032 f\n (`id \u207b\u00b9-involutive) _ = refl\n ((f `\u204f g) \u207b\u00b9-involutive) x\n rewrite (f \u207b\u00b9-involutive) x\n | (g \u207b\u00b9-involutive) (f \u2219 x) = refl\n (`0\u21941 \u207b\u00b9-involutive) _ = refl\n ((`tl f) \u207b\u00b9-involutive) (x \u2237 xs)\n rewrite (f \u207b\u00b9-involutive) xs\n = refl\n\n _\u207b\u00b9-inverse\u2032 : \u2200 {n} (f : Perm n) \u2192 (f \u207b\u00b9 `\u204f f) \u2257\u2032 `id\n (f \u207b\u00b9-inverse\u2032) xs with ((f \u207b\u00b9) \u207b\u00b9-inverse) xs\n ... | p rewrite (f \u207b\u00b9-involutive) (f \u207b\u00b9 \u2219 xs) = p\n\n `\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) (xs : Vec A n) \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n `\u27e8_\u2194_\u27e9-spec zero j xs = `\u27e80\u2194 j \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) zero xs = `\u27e80\u21941+ i \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) (suc j) (x \u2237 xs)\n rewrite `\u27e8 i \u2194 j \u27e9-spec xs = refl\n\nmodule PermutationProperties {a : L.Level} where\n open PermutationSyntax\n open PermutationSemantics\n\n \u229b-dist-\u2219 : \u2200 {n} {A : Set a} (fs : Vec (Endo A) n) (f : Perm n) xs \u2192 (f \u2219 fs \u229b f \u2219 xs) \u2261 f \u2219 (fs \u229b xs)\n \u229b-dist-\u2219 _ `id _ = refl\n \u229b-dist-\u2219 fs `0\u21941 xs = \u229b-dist-0\u21941 fs xs\n \u229b-dist-\u2219 (_ \u2237 fs) (`tl f) (_ \u2237 xs) rewrite \u229b-dist-\u2219 fs f xs = refl\n \u229b-dist-\u2219 fs (f `\u204f g) xs rewrite \u229b-dist-\u2219 (f \u2219 fs) g (f \u2219 xs)\n | \u229b-dist-\u2219 fs f xs = refl\n\n \u2219-replicate : \u2200 {n} {A : Set a} (x : A) (f : Perm n) \u2192 f \u2219 replicate x \u2261 replicate x\n \u2219-replicate x `id = refl\n \u2219-replicate x `0\u21941 = refl\n \u2219-replicate x (`tl f) rewrite \u2219-replicate x f = refl\n \u2219-replicate x (f `\u204f g) rewrite \u2219-replicate x f | \u2219-replicate x g = refl\n\n private\n lem : \u2200 {n} {A : Set a} (fs : Vec (Endo A) n) f xs\n \u2192 fs \u229b f \u2219 xs \u2261 f \u2219 (f \u207b\u00b9 \u2219 fs \u229b xs)\n lem fs f xs rewrite sym (\u229b-dist-\u2219 (f \u207b\u00b9 \u2219 fs) f xs) | (f \u207b\u00b9-inverse\u2032) fs = refl\n\n \u2219-map : \u2200 {n} {A : Set a} (f : Endo A) g (xs : Vec A n) \u2192 map f (g \u2219 xs) \u2261 g \u2219 map f xs\n \u2219-map f g xs rewrite sym (\u229b-dist-\u2219 (replicate f) g xs) | \u2219-replicate f g = refl\n\nmodule BijectionSyntax {a b} (A : Set a) (Bij\u1d2c : Set b) where\n infixr 1 _`\u204f_\n data Bij : \u2115 \u2192 Set (a L.\u2294 b) where\n `id : \u2200 {n} \u2192 Bij n\n `0\u21941 : \u2200 {n} \u2192 Bij (2 + n)\n _`\u204f_ : \u2200 {n} \u2192 Bij n \u2192 Bij n \u2192 Bij n\n _`\u2237_ : \u2200 {n} \u2192 Bij\u1d2c \u2192 (A \u2192 Bij n) \u2192 Bij (1 + n)\n\nmodule BijectionLib where\n open BijectionSyntax\n mapBij : \u2200 {a b\u1d2c} {A : Set a} {Bij\u1d2c : Set b\u1d2c}\n {b b\u1d2e} {B : Set b} {Bij\u1d2e : Set b\u1d2e}\n (f\u1d2e\u1d2c : B \u2192 A)\n (f : Bij\u1d2c \u2192 Bij\u1d2e)\n {n} \u2192 Bij A Bij\u1d2c n \u2192 Bij B Bij\u1d2e n\n mapBij f\u1d2e\u1d2c f `id = `id\n mapBij f\u1d2e\u1d2c f `0\u21941 = `0\u21941\n mapBij f\u1d2e\u1d2c f (`g `\u204f `h) = mapBij f\u1d2e\u1d2c f `g `\u204f mapBij f\u1d2e\u1d2c f `h\n mapBij f\u1d2e\u1d2c f (`f\u1d2c `\u2237 `g) = f `f\u1d2c `\u2237 \u03bb x \u2192 mapBij f\u1d2e\u1d2c f (`g (f\u1d2e\u1d2c x))\n\nmodule BijectionSemantics {a b} {A : Set a} (bijKit\u1d2c : BijKit b A) where\n open BijKit bijKit\u1d2c renaming (Bij to Bij\u1d2c; eval to eval\u1d2c; _\u207b\u00b9 to _\u207b\u00b9\u1d2c;\n idBij to id\u1d2c; _\u2257Bij_ to _\u2257\u1d2c_;\n _\u207b\u00b9-inverse to _\u207b\u00b9-inverse\u1d2c;\n _\u207b\u00b9-involutive to _\u207b\u00b9-involutive\u1d2c;\n id-spec to id\u1d2c-spec)\n open BijectionSyntax A Bij\u1d2c\n\n _\u207b\u00b9 : \u2200 {n} \u2192 Endo (Bij n)\n `id \u207b\u00b9 = `id\n (f\u2080 `\u204f f\u2081) \u207b\u00b9 = f\u2081 \u207b\u00b9 `\u204f f\u2080 \u207b\u00b9\n `0\u21941 \u207b\u00b9 = `0\u21941\n (f\u1d2c `\u2237 f) \u207b\u00b9 = f\u1d2c\u207b\u00b9 `\u2237 \u03bb x \u2192 (f (eval\u1d2c f\u1d2c\u207b\u00b9 x))\u207b\u00b9 where f\u1d2c\u207b\u00b9 = f\u1d2c \u207b\u00b9\u1d2c\n\n eval : \u2200 {n} \u2192 Bij n \u2192 Endo (Vec A n)\n eval `id = id\n eval (f `\u204f g) = eval g \u2218 eval f\n eval `0\u21941 = 0\u21941\n eval (f\u1d2c `\u2237 f) = \u03bb xs \u2192 eval\u1d2c f\u1d2c (head xs) \u2237 eval (f (head xs)) (tail xs)\n\n infixr 9 _\u2219_\n _\u2219_ : \u2200 {n} \u2192 Bij n \u2192 Endo (Vec A n)\n _\u2219_ = eval\n\n _\u2257\u2032_ : \u2200 {n} \u2192 Bij n \u2192 Bij n \u2192 Set _\n f \u2257\u2032 g = \u2200 xs \u2192 f \u2219 xs \u2261 g \u2219 xs\n\n _\u207b\u00b9-inverse : \u2200 {n} (f : Bij n) \u2192 (f `\u204f f \u207b\u00b9) \u2257\u2032 `id\n (`id \u207b\u00b9-inverse) xs = refl\n ((f `\u204f g) \u207b\u00b9-inverse) xs\n rewrite (g \u207b\u00b9-inverse) (f \u2219 xs)\n | (f \u207b\u00b9-inverse) xs = refl\n (`0\u21941 \u207b\u00b9-inverse) xs = \u27e8\u2194\u27e9.0\u21941\u00b2-cancel _ xs\n ((f\u1d2c `\u2237 f) \u207b\u00b9-inverse) (x \u2237 xs)\n rewrite (f\u1d2c \u207b\u00b9-inverse\u1d2c) x | (f x \u207b\u00b9-inverse) xs = refl\n\n _\u207b\u00b9-involutive : \u2200 {n} (f : Bij n) \u2192 (f \u207b\u00b9) \u207b\u00b9 \u2257\u2032 f\n (`id \u207b\u00b9-involutive) _ = refl\n ((f `\u204f g) \u207b\u00b9-involutive) x\n rewrite (f \u207b\u00b9-involutive) x\n | (g \u207b\u00b9-involutive) (f \u2219 x) = refl\n (`0\u21941 \u207b\u00b9-involutive) _ = refl\n ((f\u1d2c `\u2237 f) \u207b\u00b9-involutive) (x \u2237 xs)\n rewrite (f\u1d2c \u207b\u00b9-involutive\u1d2c) x\n | (f\u1d2c \u207b\u00b9-inverse\u1d2c) x\n | (f x \u207b\u00b9-involutive) xs\n = refl\n\n Vec-bijKit : \u2200 n \u2192 BijKit _ (Vec A n)\n Vec-bijKit n = mk (Bij n) eval _\u207b\u00b9 `id _`\u204f_ (\u03bb _ \u2192 refl) (\u03bb _ _ _ \u2192 refl)\n (\u03bb f x \u2192 _\u207b\u00b9-inverse f x) (\u03bb f x \u2192 _\u207b\u00b9-involutive f x)\n\n module VecBijKit n = BijKit (Vec-bijKit n)\n\n `tl : \u2200 {n} \u2192 Bij n \u2192 Bij (1 + n)\n `tl f = id\u1d2c `\u2237 const f\n\n private\n module P where\n open PermutationSyntax public\n open PermutationSemantics {A = A} public\n open P using (Perm; `id; `0\u21941; _`\u204f_)\n fromPerm : \u2200 {n} \u2192 Perm n \u2192 Bij n\n fromPerm `id = `id\n fromPerm `0\u21941 = `0\u21941\n fromPerm (\u03c0\u2080 `\u204f \u03c0\u2081) = fromPerm \u03c0\u2080 `\u204f fromPerm \u03c0\u2081\n fromPerm (P.`tl \u03c0) = `tl (fromPerm \u03c0)\n\n fromPerm-spec : \u2200 {n} \u03c0 (xs : Vec A n) \u2192 \u03c0 P.\u2219 xs \u2261 fromPerm \u03c0 \u2219 xs\n fromPerm-spec `id xs = refl\n fromPerm-spec `0\u21941 xs = refl\n fromPerm-spec (\u03c0 `\u204f \u03c0\u2081) xs rewrite fromPerm-spec \u03c0 xs | fromPerm-spec \u03c0\u2081 (fromPerm \u03c0 \u2219 xs) = refl\n fromPerm-spec (P.`tl \u03c0) (x \u2237 xs) rewrite id\u1d2c-spec x | fromPerm-spec \u03c0 xs = refl\n\n private\n module Unused where\n `\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij (1 + n)\n `\u27e80\u21941+ i \u27e9 = fromPerm P.`\u27e80\u21941+ i \u27e9\n\n `\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) (xs : Vec A (suc n)) \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n `\u27e80\u21941+ i \u27e9-spec xs rewrite sym (P.`\u27e80\u21941+ i \u27e9-spec xs) | fromPerm-spec P.`\u27e80\u21941+ i \u27e9 xs = refl\n\n `\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij n\n `\u27e80\u2194 i \u27e9 = fromPerm P.`\u27e80\u2194 i \u27e9\n\n `\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) (xs : Vec A n) \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n `\u27e80\u2194 i \u27e9-spec xs rewrite sym (P.`\u27e80\u2194 i \u27e9-spec xs) | fromPerm-spec P.`\u27e80\u2194 i \u27e9 xs = refl\n\n `\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Bij n\n `\u27e8 i \u2194 j \u27e9 = fromPerm P.`\u27e8 i \u2194 j \u27e9\n\n `\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) (xs : Vec A n) \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n `\u27e8 i \u2194 j \u27e9-spec xs rewrite sym (P.`\u27e8 i \u2194 j \u27e9-spec xs) | fromPerm-spec P.`\u27e8 i \u2194 j \u27e9 xs = refl\n\nsum-\u2237\u02b3 : \u2200 {n} x (xs : Vec \u2115 n) \u2192 sum (xs \u2237\u02b3 x) \u2261 sum xs + x\nsum-\u2237\u02b3 x [] = \u2115\u00b0.+-comm x 0\nsum-\u2237\u02b3 x (x\u2081 \u2237 xs) rewrite sum-\u2237\u02b3 x xs | \u2115\u00b0.+-assoc x\u2081 (sum xs) x = refl\n\nrot\u2081 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\nrot\u2081 [] = []\nrot\u2081 (x \u2237 xs) = xs \u2237\u02b3 x\n\nrot : \u2200 {n a} {A : Set a} \u2192 \u2115 \u2192 Vec A n \u2192 Vec A n\nrot zero xs = xs\nrot (suc n) xs = rot n (rot\u2081 xs)\n\nsum-distrib\u02e1 : \u2200 {A : Set} {n} f k (xs : Vec A n) \u2192 sum (map (\u03bb x \u2192 k * f x) xs) \u2261 k * sum (map f xs)\nsum-distrib\u02e1 f k [] = \u2115\u00b0.*-comm 0 k\nsum-distrib\u02e1 f k (x \u2237 xs) rewrite sum-distrib\u02e1 f k xs = sym (proj\u2081 \u2115\u00b0.distrib k _ _)\n\nsum-rot\u2081 : \u2200 {n} (xs : Vec \u2115 n) \u2192 sum xs \u2261 sum (rot\u2081 xs)\nsum-rot\u2081 [] = refl\nsum-rot\u2081 (x \u2237 xs) rewrite sum-\u2237\u02b3 x xs = \u2115\u00b0.+-comm x _\n\nmap-\u2237\u02b3 : \u2200 {n a} {A : Set a} (f : A \u2192 \u2115) x (xs : Vec A n) \u2192 map f (xs \u2237\u02b3 x) \u2261 map f xs \u2237\u02b3 f x\nmap-\u2237\u02b3 f x [] = refl\nmap-\u2237\u02b3 f x (x\u2081 \u2237 xs) rewrite map-\u2237\u02b3 f x xs = refl\n\nsum-map-rot\u2081 : \u2200 {n a} {A : Set a} (f : A \u2192 \u2115) (xs : Vec A n) \u2192 sum (map f (rot\u2081 xs)) \u2261 sum (map f xs)\nsum-map-rot\u2081 f [] = refl\nsum-map-rot\u2081 f (x \u2237 xs) rewrite \u2115\u00b0.+-comm (f x) (sum (map f xs))\n | \u2261.sym (sum-\u2237\u02b3 (f x) (map f xs))\n | \u2261.sym (map-\u2237\u02b3 f x xs)\n = refl\n\nprivate\n module Unused where\n module Foo where\n {-\n WRONG\n \u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n \u27e8 i \u2194 j \u27e9 = \u27e80\u2194 i \u27e9 \u2218 \u27e80\u2194 j \u27e9 \u2218 \u27e80\u2194 i \u27e9\n\n \u27e80\u2194\u27e9-spec : \u2200 {n a} {A : Set a} (i : Fin (suc n)) \u2192 \u27e80\u2194 i \u27e9 \u2257 \u27e8 zero \u2194 i \u27e9 {A = A}\n \u27e80\u2194\u27e9-spec _ _ = \u2261.refl\n -}\n\n \u27e80\u2194_\u27e9\u2032 : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n \u27e80\u2194_\u27e9\u2032 {zero} i xs = xs\n \u27e80\u2194_\u27e9\u2032 {suc n} i xs = lookup i xs \u2237 tail (xs [ i ]\u2254 head xs)\n\n -- \u27e8 i \u2194+1\u27e9: Exchange elements at position i and i + 1.\n \u27e8_\u2194+1\u27e9 : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n \u27e8 zero \u2194+1\u27e9 = 0\u21941\n \u27e8 suc i \u2194+1\u27e9 = map-tail \u27e8 i \u2194+1\u27e9\n\n \u27e8\u2194+1\u27e9-spec : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 \u27e8 inject\u2081 i \u2194+1\u27e9 \u2257 \u27e8 inject\u2081 i \u2194 suc i \u27e9 {A = A}\n \u27e8\u2194+1\u27e9-spec zero xs rewrite map-tail-id (0\u21941 xs) = \u2261.refl\n \u27e8\u2194+1\u27e9-spec (suc i) (x \u2237 xs) rewrite \u27e8\u2194+1\u27e9-spec i xs = \u2261.refl\n\n -- rot-up-to i (x\u2080 \u2237 x\u2081 \u2237 x\u2082 \u2237 \u2026 \u2237 x\u1d62 \u2237 xs)\n -- \u2261 (x\u2081 \u2237 x\u2082 \u2237 x\u2083 \u2237 \u2026 \u2237 x\u2080 \u2237 xs)\n rot-up-to : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n rot-up-to zero = id\n rot-up-to (suc i) = map-tail (rot-up-to i) \u2218 0\u21941\n\n -- Inverse of rot-up-to\n rot\u207b\u00b9-up-to : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n rot\u207b\u00b9-up-to zero = id\n rot\u207b\u00b9-up-to (suc i) = 0\u21941 \u2218 map-tail (rot\u207b\u00b9-up-to i)\n\n rot\u207b\u00b9-up-to\u2218rot-up-to : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 rot\u207b\u00b9-up-to i \u2218 rot-up-to i \u2257 id {a} {Vec A n}\n rot\u207b\u00b9-up-to\u2218rot-up-to zero _ = \u2261.refl\n rot\u207b\u00b9-up-to\u2218rot-up-to (suc i) {A = A} (x\u2080 \u2237 []) rewrite rot\u207b\u00b9-up-to\u2218rot-up-to i {A = A} [] = \u2261.refl\n rot\u207b\u00b9-up-to\u2218rot-up-to (suc i) (x\u2080 \u2237 x\u2081 \u2237 xs) rewrite rot\u207b\u00b9-up-to\u2218rot-up-to i (x\u2080 \u2237 xs) = \u2261.refl\n","old_contents":"module Data.Vec.NP where\n\nimport Level as L\nopen import Category.Applicative\nopen import Data.Vec public hiding (_\u229b_; zipWith; zip; map; applicative)\nopen import Data.Nat.NP using (\u2115; suc; zero; _+_; module \u2115\u00b0)\nopen import Data.Fin renaming (_+_ to _+\u1da0_)\nimport Data.Fin.Props as F\nopen import Data.Bool\nopen import Data.Product hiding (map; zip; swap)\nopen import Function.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Function.Bijection.SyntaxKit\n\nmodule waiting-for-a-fix-in-the-stdlib where\n\n infixl 4 _\u229b_\n\n _\u229b_ : \u2200 {a b n} {A : Set a} {B : Set b} \u2192\n Vec (A \u2192 B) n \u2192 Vec A n \u2192 Vec B n\n _\u229b_ {n = zero} fs xs = []\n _\u229b_ {n = suc n} fs xs = head fs (head xs) \u2237 (tail fs \u229b tail xs)\n\n applicative : \u2200 {a n} \u2192 RawApplicative (\u03bb (A : Set a) \u2192 Vec A n)\n applicative = record\n { pure = replicate\n ; _\u229b_ = _\u229b_\n }\n\n map : \u2200 {a b n} {A : Set a} {B : Set b} \u2192\n (A \u2192 B) \u2192 Vec A n \u2192 Vec B n\n map f xs = replicate f \u229b xs\n\n zipWith : \u2200 {a b c n} {A : Set a} {B : Set b} {C : Set c} \u2192\n (A \u2192 B \u2192 C) \u2192 Vec A n \u2192 Vec B n \u2192 Vec C n\n zipWith _\u2295_ xs ys = replicate _\u2295_ \u229b xs \u229b ys\n\n zip : \u2200 {a b n} {A : Set a} {B : Set b} \u2192\n Vec A n \u2192 Vec B n \u2192 Vec (A \u00d7 B) n\n zip = zipWith _,_\n\n tabulate-\u2218 : \u2200 {n a b} {A : Set a} {B : Set b}\n (f : A \u2192 B) (g : Fin n \u2192 A) \u2192\n tabulate (f \u2218 g) \u2261 map f (tabulate g)\n tabulate-\u2218 {zero} f g = refl\n tabulate-\u2218 {suc n} f g =\n \u2261.cong (_\u2237_ (f (g zero))) (tabulate-\u2218 f (g \u2218 suc))\n\n -- map is functorial.\n\n map-id : \u2200 {a n} {A : Set a} \u2192 map id \u2257 id {A = Vec A n}\n map-id [] = refl\n map-id (x \u2237 xs) = \u2261.cong (_\u2237_ x) (map-id xs)\n\n map-\u2218 : \u2200 {a b c n} {A : Set a} {B : Set b} {C : Set c}\n (f : B \u2192 C) (g : A \u2192 B) \u2192\n _\u2257_ {A = Vec A n} (map (f \u2218 g)) (map f \u2218 map g)\n map-\u2218 f g [] = refl\n map-\u2218 f g (x \u2237 xs) = \u2261.cong (_\u2237_ (f (g x))) (map-\u2218 f g xs)\n\nopen waiting-for-a-fix-in-the-stdlib public\n\n-- Trying to get rid of the foldl in the definition of reverse and\n-- without using equations on natural numbers.\n-- In the end that's not very convincing.\nmodule Alternative-Reverse where\n rev-+ : \u2115 \u2192 \u2115 \u2192 \u2115\n rev-+ zero = id\n rev-+ (suc x) = rev-+ x \u2218 suc\n\n rev-app : \u2200 {a} {A : Set a} {m n} \u2192\n Vec A n \u2192 Vec A m \u2192 Vec A (rev-+ n m)\n rev-app [] = id\n rev-app (x \u2237 xs) = rev-app xs \u2218 _\u2237_ x\n\n rev-aux : \u2200 {a} {A : Set a} {m} n \u2192\n Vec A (rev-+ n zero) \u2192\n (\u2200 {m} \u2192 A \u2192 Vec A (rev-+ n m) \u2192 Vec A (rev-+ n (suc m))) \u2192\n Vec A m \u2192 Vec A (rev-+ n m)\n rev-aux m acc op [] = acc\n rev-aux m acc op (x \u2237 xs) = rev-aux (suc m) (op x acc) op xs\n\n alt-reverse : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n alt-reverse = rev-aux 0 [] _\u2237_\n\nvuncurry : \u2200 {n a b} {A : Set a} {B : Set b} (f : A \u2192 Vec A n \u2192 B) \u2192 Vec A (1 + n) \u2192 B\nvuncurry f (x \u2237 xs) = f x xs\n\ncount\u1da0 : \u2200 {n a} {A : Set a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 Fin (suc n)\ncount\u1da0 pred = foldr (Fin \u2218 suc) (\u03bb x \u2192 if pred x then suc else inject\u2081) zero\n\ncount : \u2200 {n a} {A : Set a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 \u2115\ncount pred = to\u2115 \u2218 count\u1da0 pred\n\ncount-\u2218 : \u2200 {n a b} {A : Set a} {B : Set b} (f : A \u2192 B) (pred : B \u2192 Bool) \u2192\n count {n} (pred \u2218 f) \u2257 count pred \u2218 map f\ncount-\u2218 f pred [] = refl\ncount-\u2218 f pred (x \u2237 xs) with pred (f x)\n... | true rewrite count-\u2218 f pred xs = refl\n... | false rewrite F.inject\u2081-lemma (count\u1da0 pred (map f xs))\n | F.inject\u2081-lemma (count\u1da0 (pred \u2218 f) xs)\n | count-\u2218 f pred xs = refl\n\ncount-++ : \u2200 {m n a} {A : Set a} (pred : A \u2192 Bool) (xs : Vec A m) (ys : Vec A n)\n \u2192 count pred (xs ++ ys) \u2261 count pred xs + count pred ys\ncount-++ pred [] ys = refl\ncount-++ pred (x \u2237 xs) ys with pred x\n... | true rewrite count-++ pred xs ys = refl\n... | false rewrite F.inject\u2081-lemma (count\u1da0 pred (xs ++ ys))\n | F.inject\u2081-lemma (count\u1da0 pred xs) | count-++ pred xs ys = refl\n\next-count\u1da0 : \u2200 {n a} {A : Set a} {f g : A \u2192 Bool} \u2192 f \u2257 g \u2192 (xs : Vec A n) \u2192 count\u1da0 f xs \u2261 count\u1da0 g xs\next-count\u1da0 f\u2257g [] = refl\next-count\u1da0 f\u2257g (x \u2237 xs) rewrite ext-count\u1da0 f\u2257g xs | f\u2257g x = refl\n\nfilter : \u2200 {n a} {A : Set a} (pred : A \u2192 Bool) (xs : Vec A n) \u2192 Vec A (count pred xs)\nfilter pred [] = []\nfilter pred (x \u2237 xs) with pred x\n... | true = x \u2237 filter pred xs\n... | false rewrite F.inject\u2081-lemma (count\u1da0 pred xs) = filter pred xs\n\n\u03b7 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7 = tabulate \u2218 flip lookup\n\n\u03b7\u2032 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7\u2032 {zero} = \u03bb _ \u2192 []\n\u03b7\u2032 {suc n} = \u03bb xs \u2192 head xs \u2237 \u03b7 (tail xs)\n\nshallow-\u03b7 : \u2200 {n a} {A : Set a} (xs : Vec A (1 + n)) \u2192 xs \u2261 head xs \u2237 tail xs\nshallow-\u03b7 (x \u2237 xs) = \u2261.refl\n\nuncons : \u2200 {n a} {A : Set a} \u2192 Vec A (1 + n) \u2192 (A \u00d7 Vec A n)\nuncons (x \u2237 xs) = x , xs\n\nsplitAt\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A m \u00d7 Vec A n\nsplitAt\u2032 m xs = case splitAt m xs of \u03bb { (ys , zs , _) \u2192 (ys , zs) }\n\n++-decomp : \u2200 {m n a} {A : Set a} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2261 (zs ++ ts) \u2192 (xs \u2261 zs \u00d7 ys \u2261 ts)\n++-decomp {zero} {xs = []} {[]} p = refl , p\n++-decomp {suc m} {xs = x \u2237 xs} {z \u2237 zs} eq with ++-decomp {m} {xs = xs} {zs} (cong tail eq)\n... | (q\u2081 , q\u2082) = (cong\u2082 _\u2237_ (cong head eq) q\u2081) , q\u2082\n\n{-\nopen import Data.Vec.Equality\n\nmodule Here {a} {A : Set a} where\n open Equality (\u2261.setoid A)\n \u2248-splitAt : \u2200 {m n} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2248 (zs ++ ts) \u2192 (xs \u2248 zs \u00d7 ys \u2248 ts)\n \u2248-splitAt {zero} {xs = []} {[]} p = []-cong , p\n \u2248-splitAt {suc m} {xs = x \u2237 xs} {z \u2237 zs} (x\u00b9\u2248x\u00b2 \u2237-cong p) with \u2248-splitAt {m} {xs = xs} {zs} p\n ... | (q\u2081 , q\u2082) = x\u00b9\u2248x\u00b2 \u2237-cong q\u2081 , q\u2082\n-}\n\n++-inj\u2081 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 xs \u2261 ys\n++-inj\u2081 xs ys eq = proj\u2081 (++-decomp eq)\n\n++-inj\u2082 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 zs \u2261 ts\n++-inj\u2082 xs ys eq = proj\u2082 (++-decomp {xs = xs} {ys} eq)\n\ntake-\u2237 : \u2200 {m a} {A : Set a} n x (xs : Vec A (n + m)) \u2192 take (suc n) (x \u2237 xs) \u2261 x \u2237 take n xs\ntake-\u2237 n x xs with splitAt n xs\ntake-\u2237 _ _ ._ | _ , _ , refl = refl\n\ndrop-\u2237 : \u2200 {m a} {A : Set a} n x (xs : Vec A (n + m)) \u2192 drop (suc n) (x \u2237 xs) \u2261 drop n xs\ndrop-\u2237 n x xs with splitAt n xs\ndrop-\u2237 _ _ ._ | _ , _ , refl = refl\n\ntake-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 take m (xs ++ ys) \u2261 xs\ntake-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ntake-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2081 xs\u2081 xs eq)\n\ndrop-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 drop m (xs ++ ys) \u2261 ys\ndrop-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ndrop-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2082 xs\u2081 xs eq)\n\ntake-drop-lem : \u2200 m {n} {a} {A : Set a} (xs : Vec A (m + n)) \u2192 take m xs ++ drop m xs \u2261 xs\ntake-drop-lem m xs with splitAt m xs\ntake-drop-lem m .(ys ++ zs) | ys , zs , refl = refl\n\ntake-them-all : \u2200 n {a} {A : Set a} (xs : Vec A (n + 0)) \u2192 take n xs ++ [] \u2261 xs\ntake-them-all n xs with splitAt n xs\ntake-them-all n .(ys ++ []) | ys , [] , refl = refl\n\ndrop\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A n\ndrop\u2032 zero = id\ndrop\u2032 (suc m) = drop\u2032 m \u2218 tail\n\ndrop\u2032-spec : \u2200 {a} {A : Set a} m {n} \u2192 drop\u2032 {A = A} m {n} \u2257 drop m {n}\ndrop\u2032-spec zero xs = refl\ndrop\u2032-spec (suc m) (x \u2237 xs) rewrite drop\u2032-spec m xs | drop-\u2237 m x xs = refl\n\ntake\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A m\ntake\u2032 zero _ = []\ntake\u2032 (suc m) xs = head xs \u2237 take\u2032 m (tail xs)\n\ntake\u2032-spec : \u2200 {a} {A : Set a} m {n} \u2192 take\u2032 {A = A} m {n} \u2257 take m {n}\ntake\u2032-spec zero xs = refl\ntake\u2032-spec (suc m) (x \u2237 xs) rewrite take\u2032-spec m xs | take-\u2237 m x xs = refl\n\nswap : \u2200 m {n} {a} {A : Set a} \u2192 Vec A (m + n) \u2192 Vec A (n + m)\nswap m xs = drop m xs ++ take m xs\n\nswap-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 swap m (xs ++ ys) \u2261 ys ++ xs\nswap-++ m xs ys rewrite drop-++ m xs ys | take-++ m xs ys = refl\n\nrewire : \u2200 {a i o} {A : Set a} \u2192 (Fin o \u2192 Fin i) \u2192 Vec A i \u2192 Vec A o\nrewire f v = tabulate (flip lookup v \u2218 f)\n\nRewireTbl : (i o : \u2115) \u2192 Set\nRewireTbl i o = Vec (Fin i) o\n\nrewireTbl : \u2200 {a i o} {A : Set a} \u2192 RewireTbl i o \u2192 Vec A i \u2192 Vec A o\nrewireTbl tbl v = map (flip lookup v) tbl\n\non\u1d62 : \u2200 {a} {A : Set a} (f : A \u2192 A) {n} (i : Fin n) \u2192 Vec A n \u2192 Vec A n\non\u1d62 f zero (x \u2237 xs) = f x \u2237 xs\non\u1d62 f (suc i) (x \u2237 xs) = x \u2237 on\u1d62 f i xs\n\n-- Exchange elements at positions 0 and 1 of a given vector\n-- (this only apply if the vector is long enough).\n0\u21941 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n0\u21941 (x\u2080 \u2237 x\u2081 \u2237 xs) = x\u2081 \u2237 x\u2080 \u2237 xs\n0\u21941 xs = xs\n\n\u229b-dist-0\u21941 : \u2200 {n a} {A : Set a} (fs : Vec (Endo A) n) xs \u2192 0\u21941 fs \u229b 0\u21941 xs \u2261 0\u21941 (fs \u229b xs)\n\u229b-dist-0\u21941 _ [] = refl\n\u229b-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n\u229b-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\nmap-tail : \u2200 {m n a} {A : Set a} \u2192 (Vec A m \u2192 Vec A n) \u2192 Vec A (suc m) \u2192 Vec A (suc n)\nmap-tail f (x \u2237 xs) = x \u2237 f xs\n\nmap-tail-id : \u2200 {n a} {A : Set a} \u2192 map-tail id \u2257 id {A = Vec A (suc n)}\nmap-tail-id (x \u2237 xs) = \u2261.refl\n\nmap-tail\u2218map-tail : \u2200 {m n o a} {A : Set a}\n (f : Vec A o \u2192 Vec A m)\n (g : Vec A n \u2192 Vec A o)\n \u2192 map-tail f \u2218 map-tail g \u2257 map-tail (f \u2218 g)\nmap-tail\u2218map-tail f g (x \u2237 xs) = refl\n\nmap-tail-\u2257 : \u2200 {m n a} {A : Set a} (f g : Vec A m \u2192 Vec A n) \u2192 f \u2257 g \u2192 map-tail f \u2257 map-tail g\nmap-tail-\u2257 f g f\u2257g (x \u2237 xs) rewrite f\u2257g xs = refl\n\n-- \u27e80\u21941+ i \u27e9: Exchange elements at position 0 and 1+i.\n\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A (1 + n) \u2192 Vec A (1 + n)\n\u27e80\u21941+ zero \u27e9 = 0\u21941\n\u27e80\u21941+ suc i \u27e9 = 0\u21941 \u2218 (map-tail \u27e80\u21941+ i \u27e9) \u2218 0\u21941\n {- 0 1 2 3 ... i 1+i ... n\n 1 0 2 3 ... i 1+i ... n\n 1 1+i 2 3 ... i 0 ... n\n\n 1+i 1 2 3 ... i 0 ... n\n -}\n\n-- \u27e80\u2194 i \u27e9: Exchange elements at position 0 and i.\n\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u27e80\u2194 zero \u27e9 = id\n\u27e80\u2194 suc i \u27e9 = \u27e80\u21941+ i \u27e9\n\n\u27e80\u2194zero\u27e9 : \u2200 {n a} {A : Set a} \u2192 \u27e80\u2194 zero \u27e9 \u2257 id {A = Vec A (suc n)}\n\u27e80\u2194zero\u27e9 _ = \u2261.refl\n\n_\u00b2 : \u2200 {a} {A : Set a} \u2192 Endo (Endo A)\nf \u00b2 = f \u2218 f\n\nmodule \u27e8\u2194\u27e9 {a} (A : Set a) where\n\n \u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Vec A n \u2192 Vec A n\n \u27e8 zero \u2194 j \u27e9 = \u27e80\u2194 j \u27e9\n \u27e8 i \u2194 zero \u27e9 = \u27e80\u2194 i \u27e9\n \u27e8 suc i \u2194 suc j \u27e9 = map-tail \u27e8 i \u2194 j \u27e9\n-- \u27e8 # 0 \u2194 # 1 \u27e9\n\n comm : \u2200 {n} (i j : Fin n) \u2192 \u27e8 i \u2194 j \u27e9 \u2257 \u27e8 j \u2194 i \u27e9\n comm zero zero _ = \u2261.refl\n comm zero (suc _) _ = \u2261.refl\n comm (suc _) zero _ = \u2261.refl\n comm (suc i) (suc j) (x \u2237 xs) rewrite comm i j xs = \u2261.refl\n\n 0\u21941\u00b2-cancel : \u2200 {n} \u2192 0\u21941 \u00b2 \u2257 id {A = Vec A n}\n 0\u21941\u00b2-cancel [] = refl\n 0\u21941\u00b2-cancel (_ \u2237 []) = refl\n 0\u21941\u00b2-cancel (x \u2237 x\u2081 \u2237 xs) = refl\n\n \u27e80\u21941+_\u27e9\u00b2-cancel : \u2200 {n} (i : Fin n) \u2192 \u27e80\u21941+ i \u27e9 \u00b2 \u2257 id {A = Vec A (1 + n)}\n \u27e80\u21941+ zero \u27e9\u00b2-cancel xs = 0\u21941\u00b2-cancel xs\n \u27e80\u21941+ suc i \u27e9\u00b2-cancel xs\n rewrite 0\u21941\u00b2-cancel (map-tail \u27e80\u21941+ i \u27e9 (0\u21941 xs))\n | map-tail\u2218map-tail \u27e80\u21941+ i \u27e9 \u27e80\u21941+ i \u27e9 (0\u21941 xs)\n | map-tail-\u2257 _ _ \u27e80\u21941+ i \u27e9\u00b2-cancel (0\u21941 xs)\n | map-tail-id (0\u21941 xs)\n | 0\u21941\u00b2-cancel xs = refl\n\n \u27e80\u2194_\u27e9\u00b2-cancel : \u2200 {n} (i : Fin n) \u2192 \u27e80\u2194 i \u27e9 \u00b2 \u2257 id {A = Vec A n}\n \u27e80\u2194 zero \u27e9\u00b2-cancel _ = \u2261.refl\n \u27e80\u2194 suc i \u27e9\u00b2-cancel xs = \u27e80\u21941+ i \u27e9\u00b2-cancel xs\n\n \u27e8_\u2194_\u27e9\u00b2-cancel : \u2200 {n} (i j : Fin n) \u2192 \u27e8 i \u2194 j \u27e9 \u00b2 \u2257 id\n \u27e8 zero \u2194 j \u27e9\u00b2-cancel xs = \u27e80\u2194 j \u27e9\u00b2-cancel xs\n \u27e8 suc i \u2194 zero \u27e9\u00b2-cancel xs = \u27e80\u21941+ i \u27e9\u00b2-cancel xs\n \u27e8 suc i \u2194 suc j \u27e9\u00b2-cancel xs\n rewrite map-tail\u2218map-tail \u27e8 i \u2194 j \u27e9 \u27e8 i \u2194 j \u27e9 xs\n | map-tail-\u2257 _ _ \u27e8 i \u2194 j \u27e9\u00b2-cancel xs\n | map-tail-id xs = refl\n\n lem01maptail2 : \u2200 {m n a} {A : Set a} (f : Vec A m \u2192 Vec A n) \u2192\n 0\u21941 \u2218 map-tail (map-tail f) \u2218 0\u21941 \u2257 map-tail (map-tail f)\n lem01maptail2 _ (_ \u2237 _ \u2237 _) = refl\n\n \u2194-refl : \u2200 {n} (i : Fin n) \u2192 \u27e8 i \u2194 i \u27e9 \u2257 id\n \u2194-refl zero _ = refl\n \u2194-refl (suc i) xs rewrite map-tail-\u2257 _ _ (\u2194-refl i) xs = map-tail-id xs\n\n {-\n lem1+ : \u2200 {n} (i j : Fin n) \u2192 \u27e80\u21941+ i \u27e9 \u2218 \u27e80\u21941+ j \u27e9 \u2218 \u27e80\u21941+ i \u27e9 \u2257 map-tail \u27e8 i \u2194 j \u27e9\n lem1+ zero zero xs = {!!}\n lem1+ zero (suc j) xs = {!!}\n lem1+ (suc i) zero xs = {!!}\n lem1+ (suc i) (suc j) xs\n rewrite 0\u21941\u00b2-cancel (map-tail \u27e80\u21941+ i \u27e9 (0\u21941 xs))\n | 0\u21941\u00b2-cancel (map-tail \u27e80\u21941+ j \u27e9 (map-tail \u27e80\u21941+ i \u27e9 (0\u21941 xs)))\n | map-tail\u2218map-tail \u27e80\u21941+ j \u27e9 \u27e80\u21941+ i \u27e9 (0\u21941 xs)\n | map-tail\u2218map-tail \u27e80\u21941+ i \u27e9 (\u27e80\u21941+ j \u27e9 \u2218 \u27e80\u21941+ i \u27e9) (0\u21941 xs)\n | map-tail-\u2257 _ _ (lem1+ i j) (0\u21941 xs)\n | lem01maptail2 \u27e8 i \u2194 j \u27e9 xs\n = refl\n\n lem : \u2200 {n} (i j : Fin n) \u2192 \u27e80\u2194 i \u27e9 \u2218 \u27e80\u2194 j \u27e9 \u2218 \u27e80\u2194 i \u27e9 \u2257 \u27e8 i \u2194 j \u27e9\n lem zero j xs = refl\n lem (suc i) zero xs = {!\u27e80\u21941+ i \u27e9\u00b2-cancel xs!}\n lem (suc i) (suc j) xs = (\u27e80\u21941+ i \u27e9 \u2218 \u27e80\u21941+ j \u27e9 \u2218 \u27e80\u21941+ i \u27e9) xs\n \u2261\u27e8 lem1+ i j xs \u27e9\n \u27e8 suc i \u2194 suc j \u27e9 xs\n \u220e where open \u2261-Reasoning\n test = {!!}\n -}\n{-\n lem : \u2200 {n} (i j k : Fin n) \u2192 \u27e8 i \u2194 j \u27e9 \u2218 \u27e8 i \u2194 k \u27e9 \u2218 \u27e8 i \u2194 j \u27e9 \u2257 \u27e8 j \u2194 k \u27e9\n lem i j k xs = (\u27e8 i \u2194 j \u27e9 \u2218 \u27e8 i \u2194 k \u27e9 \u2218 \u27e8 i \u2194 j \u27e9) xs\n (\u27e8 i \u2194 j \u27e9 \u2218 \u27e8 i \u2194 k \u27e9 \u2218 id \u2218 \u27e8 i \u2194 j \u27e9) xs\n (\u27e8 i \u2194 j \u27e9 \u2218 \u27e8 i \u2194 k \u27e9 \u2218 \u27e8 i \u2194 k \u27e9 \u2218 \u27e8 i \u2194 k \u27e9 \u2218 \u27e8 i \u2194 j \u27e9) xs\n \u2261\u27e8 {!!} \u27e9\n \u27e8 j \u2194 k \u27e9 xs\n \u220e where open \u2261-Reasoning\n-}\n\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u27e8_\u2194_\u27e9 i j = \u27e8\u2194\u27e9.\u27e8_\u2194_\u27e9 _ i j\n\nmodule PermutationSyntax where\n infixr 1 _`\u204f_\n data Perm : \u2115 \u2192 Set where\n `id : \u2200 {n} \u2192 Perm n\n `0\u21941 : \u2200 {n} \u2192 Perm (2 + n)\n _`\u204f_ : \u2200 {n} \u2192 Perm n \u2192 Perm n \u2192 Perm n\n `tl : \u2200 {n} \u2192 Perm n \u2192 Perm (1 + n)\n\n _\u207b\u00b9 : \u2200 {n} \u2192 Endo (Perm n)\n `id \u207b\u00b9 = `id\n (f\u2080 `\u204f f\u2081) \u207b\u00b9 = f\u2081 \u207b\u00b9 `\u204f f\u2080 \u207b\u00b9\n `0\u21941 \u207b\u00b9 = `0\u21941\n (`tl f) \u207b\u00b9 = `tl (f \u207b\u00b9)\n\n `\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Perm (1 + n)\n `\u27e80\u21941+ zero \u27e9 = `0\u21941\n `\u27e80\u21941+ suc i \u27e9 = `0\u21941 `\u204f `tl `\u27e80\u21941+ i \u27e9 `\u204f `0\u21941\n\n `\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Perm n\n `\u27e80\u2194 zero \u27e9 = `id\n `\u27e80\u2194 suc i \u27e9 = `\u27e80\u21941+ i \u27e9\n\n `\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Perm n\n `\u27e8 zero \u2194 j \u27e9 = `\u27e80\u2194 j \u27e9\n `\u27e8 i \u2194 zero \u27e9 = `\u27e80\u2194 i \u27e9\n `\u27e8 suc i \u2194 suc j \u27e9 = `tl `\u27e8 i \u2194 j \u27e9\n\nmodule PermutationSemantics {a} {A : Set a} where\n open PermutationSyntax\n\n eval : \u2200 {n} \u2192 Perm n \u2192 Endo (Vec A n)\n eval `id = id\n eval (f `\u204f g) = eval g \u2218 eval f\n eval `0\u21941 = 0\u21941\n eval (`tl f) = \u03bb xs \u2192 head xs \u2237 eval f (tail xs)\n\n infixr 9 _\u2219_\n _\u2219_ : \u2200 {n} \u2192 Perm n \u2192 Endo (Vec A n)\n _\u2219_ = eval\n\n `\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) (xs : Vec A (suc n)) \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n `\u27e80\u21941+ zero \u27e9-spec xs = refl\n `\u27e80\u21941+ suc i \u27e9-spec (x \u2237 y \u2237 xs) rewrite `\u27e80\u21941+ i \u27e9-spec (x \u2237 xs) = refl\n\n `\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) (xs : Vec A n) \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n `\u27e80\u2194 zero \u27e9-spec xs = refl\n `\u27e80\u2194 suc i \u27e9-spec xs = `\u27e80\u21941+ i \u27e9-spec xs\n\n _\u2257\u2032_ : \u2200 {n} \u2192 Perm n \u2192 Perm n \u2192 Set _\n f \u2257\u2032 g = \u2200 xs \u2192 f \u2219 xs \u2261 g \u2219 xs\n\n open \u27e8\u2194\u27e9 A hiding (\u27e8_\u2194_\u27e9)\n\n _\u207b\u00b9-inverse : \u2200 {n} (f : Perm n) \u2192 (f `\u204f f \u207b\u00b9) \u2257\u2032 `id\n (`id \u207b\u00b9-inverse) xs = refl\n ((f `\u204f g) \u207b\u00b9-inverse) xs\n rewrite (g \u207b\u00b9-inverse) (f \u2219 xs)\n | (f \u207b\u00b9-inverse) xs = refl\n (`0\u21941 \u207b\u00b9-inverse) xs = 0\u21941\u00b2-cancel xs\n ((`tl f) \u207b\u00b9-inverse) (x \u2237 xs)\n rewrite (f \u207b\u00b9-inverse) xs = refl\n\n _\u207b\u00b9-involutive : \u2200 {n} (f : Perm n) \u2192 (f \u207b\u00b9) \u207b\u00b9 \u2257\u2032 f\n (`id \u207b\u00b9-involutive) _ = refl\n ((f `\u204f g) \u207b\u00b9-involutive) x\n rewrite (f \u207b\u00b9-involutive) x\n | (g \u207b\u00b9-involutive) (f \u2219 x) = refl\n (`0\u21941 \u207b\u00b9-involutive) _ = refl\n ((`tl f) \u207b\u00b9-involutive) (x \u2237 xs)\n rewrite (f \u207b\u00b9-involutive) xs\n = refl\n\n _\u207b\u00b9-inverse\u2032 : \u2200 {n} (f : Perm n) \u2192 (f \u207b\u00b9 `\u204f f) \u2257\u2032 `id\n (f \u207b\u00b9-inverse\u2032) xs with ((f \u207b\u00b9) \u207b\u00b9-inverse) xs\n ... | p rewrite (f \u207b\u00b9-involutive) (f \u207b\u00b9 \u2219 xs) = p\n\n `\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) (xs : Vec A n) \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n `\u27e8_\u2194_\u27e9-spec zero j xs = `\u27e80\u2194 j \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) zero xs = `\u27e80\u21941+ i \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) (suc j) (x \u2237 xs)\n rewrite `\u27e8 i \u2194 j \u27e9-spec xs = refl\n\nmodule PermutationProperties {a : L.Level} where\n open PermutationSyntax\n open PermutationSemantics\n\n \u229b-dist-\u2219 : \u2200 {n} {A : Set a} (fs : Vec (Endo A) n) (f : Perm n) xs \u2192 (f \u2219 fs \u229b f \u2219 xs) \u2261 f \u2219 (fs \u229b xs)\n \u229b-dist-\u2219 _ `id _ = refl\n \u229b-dist-\u2219 fs `0\u21941 xs = \u229b-dist-0\u21941 fs xs\n \u229b-dist-\u2219 (_ \u2237 fs) (`tl f) (_ \u2237 xs) rewrite \u229b-dist-\u2219 fs f xs = refl\n \u229b-dist-\u2219 fs (f `\u204f g) xs rewrite \u229b-dist-\u2219 (f \u2219 fs) g (f \u2219 xs)\n | \u229b-dist-\u2219 fs f xs = refl\n\n \u2219-replicate : \u2200 {n} {A : Set a} (x : A) (f : Perm n) \u2192 f \u2219 replicate x \u2261 replicate x\n \u2219-replicate x `id = refl\n \u2219-replicate x `0\u21941 = refl\n \u2219-replicate x (`tl f) rewrite \u2219-replicate x f = refl\n \u2219-replicate x (f `\u204f g) rewrite \u2219-replicate x f | \u2219-replicate x g = refl\n\n private\n lem : \u2200 {n} {A : Set a} (fs : Vec (Endo A) n) f xs\n \u2192 fs \u229b f \u2219 xs \u2261 f \u2219 (f \u207b\u00b9 \u2219 fs \u229b xs)\n lem fs f xs rewrite sym (\u229b-dist-\u2219 (f \u207b\u00b9 \u2219 fs) f xs) | (f \u207b\u00b9-inverse\u2032) fs = refl\n\n \u2219-map : \u2200 {n} {A : Set a} (f : Endo A) g (xs : Vec A n) \u2192 map f (g \u2219 xs) \u2261 g \u2219 map f xs\n \u2219-map f g xs rewrite sym (\u229b-dist-\u2219 (replicate f) g xs) | \u2219-replicate f g = refl\n\nmodule BijectionSyntax {a b} (A : Set a) (Bij\u1d2c : Set b) where\n infixr 1 _`\u204f_\n data Bij : \u2115 \u2192 Set (a L.\u2294 b) where\n `id : \u2200 {n} \u2192 Bij n\n `0\u21941 : \u2200 {n} \u2192 Bij (2 + n)\n _`\u204f_ : \u2200 {n} \u2192 Bij n \u2192 Bij n \u2192 Bij n\n _`\u2237_ : \u2200 {n} \u2192 Bij\u1d2c \u2192 (A \u2192 Bij n) \u2192 Bij (1 + n)\n\nmodule BijectionLib where\n open BijectionSyntax\n mapBij : \u2200 {a b\u1d2c} {A : Set a} {Bij\u1d2c : Set b\u1d2c}\n {b b\u1d2e} {B : Set b} {Bij\u1d2e : Set b\u1d2e}\n (f\u1d2e\u1d2c : B \u2192 A)\n (f : Bij\u1d2c \u2192 Bij\u1d2e)\n {n} \u2192 Bij A Bij\u1d2c n \u2192 Bij B Bij\u1d2e n\n mapBij f\u1d2e\u1d2c f `id = `id\n mapBij f\u1d2e\u1d2c f `0\u21941 = `0\u21941\n mapBij f\u1d2e\u1d2c f (`g `\u204f `h) = mapBij f\u1d2e\u1d2c f `g `\u204f mapBij f\u1d2e\u1d2c f `h\n mapBij f\u1d2e\u1d2c f (`f\u1d2c `\u2237 `g) = f `f\u1d2c `\u2237 \u03bb x \u2192 mapBij f\u1d2e\u1d2c f (`g (f\u1d2e\u1d2c x))\n\nmodule BijectionSemantics {a b} {A : Set a} (bijKit\u1d2c : BijKit b A) where\n open BijKit bijKit\u1d2c renaming (Bij to Bij\u1d2c; eval to eval\u1d2c; _\u207b\u00b9 to _\u207b\u00b9\u1d2c;\n idBij to id\u1d2c; _\u2257Bij_ to _\u2257\u1d2c_;\n _\u207b\u00b9-inverse to _\u207b\u00b9-inverse\u1d2c;\n _\u207b\u00b9-involutive to _\u207b\u00b9-involutive\u1d2c;\n id-spec to id\u1d2c-spec)\n open BijectionSyntax A Bij\u1d2c\n\n _\u207b\u00b9 : \u2200 {n} \u2192 Endo (Bij n)\n `id \u207b\u00b9 = `id\n (f\u2080 `\u204f f\u2081) \u207b\u00b9 = f\u2081 \u207b\u00b9 `\u204f f\u2080 \u207b\u00b9\n `0\u21941 \u207b\u00b9 = `0\u21941\n (f\u1d2c `\u2237 f) \u207b\u00b9 = f\u1d2c\u207b\u00b9 `\u2237 \u03bb x \u2192 (f (eval\u1d2c f\u1d2c\u207b\u00b9 x))\u207b\u00b9 where f\u1d2c\u207b\u00b9 = f\u1d2c \u207b\u00b9\u1d2c\n\n eval : \u2200 {n} \u2192 Bij n \u2192 Endo (Vec A n)\n eval `id = id\n eval (f `\u204f g) = eval g \u2218 eval f\n eval `0\u21941 = 0\u21941\n eval (f\u1d2c `\u2237 f) = \u03bb xs \u2192 eval\u1d2c f\u1d2c (head xs) \u2237 eval (f (head xs)) (tail xs)\n\n infixr 9 _\u2219_\n _\u2219_ : \u2200 {n} \u2192 Bij n \u2192 Endo (Vec A n)\n _\u2219_ = eval\n\n _\u2257\u2032_ : \u2200 {n} \u2192 Bij n \u2192 Bij n \u2192 Set _\n f \u2257\u2032 g = \u2200 xs \u2192 f \u2219 xs \u2261 g \u2219 xs\n\n _\u207b\u00b9-inverse : \u2200 {n} (f : Bij n) \u2192 (f `\u204f f \u207b\u00b9) \u2257\u2032 `id\n (`id \u207b\u00b9-inverse) xs = refl\n ((f `\u204f g) \u207b\u00b9-inverse) xs\n rewrite (g \u207b\u00b9-inverse) (f \u2219 xs)\n | (f \u207b\u00b9-inverse) xs = refl\n (`0\u21941 \u207b\u00b9-inverse) xs = \u27e8\u2194\u27e9.0\u21941\u00b2-cancel _ xs\n ((f\u1d2c `\u2237 f) \u207b\u00b9-inverse) (x \u2237 xs)\n rewrite (f\u1d2c \u207b\u00b9-inverse\u1d2c) x | (f x \u207b\u00b9-inverse) xs = refl\n\n _\u207b\u00b9-involutive : \u2200 {n} (f : Bij n) \u2192 (f \u207b\u00b9) \u207b\u00b9 \u2257\u2032 f\n (`id \u207b\u00b9-involutive) _ = refl\n ((f `\u204f g) \u207b\u00b9-involutive) x\n rewrite (f \u207b\u00b9-involutive) x\n | (g \u207b\u00b9-involutive) (f \u2219 x) = refl\n (`0\u21941 \u207b\u00b9-involutive) _ = refl\n ((f\u1d2c `\u2237 f) \u207b\u00b9-involutive) (x \u2237 xs)\n rewrite (f\u1d2c \u207b\u00b9-involutive\u1d2c) x\n | (f\u1d2c \u207b\u00b9-inverse\u1d2c) x\n | (f x \u207b\u00b9-involutive) xs\n = refl\n\n Vec-bijKit : \u2200 n \u2192 BijKit _ (Vec A n)\n Vec-bijKit n = mk (Bij n) eval _\u207b\u00b9 `id _`\u204f_ (\u03bb _ \u2192 refl) (\u03bb _ _ _ \u2192 refl)\n (\u03bb f x \u2192 _\u207b\u00b9-inverse f x) (\u03bb f x \u2192 _\u207b\u00b9-involutive f x)\n\n module VecBijKit n = BijKit (Vec-bijKit n)\n\n `tl : \u2200 {n} \u2192 Bij n \u2192 Bij (1 + n)\n `tl f = id\u1d2c `\u2237 const f\n\n private\n module P where\n open PermutationSyntax public\n open PermutationSemantics {A = A} public\n open P using (Perm; `id; `0\u21941; _`\u204f_)\n fromPerm : \u2200 {n} \u2192 Perm n \u2192 Bij n\n fromPerm `id = `id\n fromPerm `0\u21941 = `0\u21941\n fromPerm (\u03c0\u2080 `\u204f \u03c0\u2081) = fromPerm \u03c0\u2080 `\u204f fromPerm \u03c0\u2081\n fromPerm (P.`tl \u03c0) = `tl (fromPerm \u03c0)\n\n fromPerm-spec : \u2200 {n} \u03c0 (xs : Vec A n) \u2192 \u03c0 P.\u2219 xs \u2261 fromPerm \u03c0 \u2219 xs\n fromPerm-spec `id xs = refl\n fromPerm-spec `0\u21941 xs = refl\n fromPerm-spec (\u03c0 `\u204f \u03c0\u2081) xs rewrite fromPerm-spec \u03c0 xs | fromPerm-spec \u03c0\u2081 (fromPerm \u03c0 \u2219 xs) = refl\n fromPerm-spec (P.`tl \u03c0) (x \u2237 xs) rewrite id\u1d2c-spec x | fromPerm-spec \u03c0 xs = refl\n\n private\n module Unused where\n `\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij (1 + n)\n `\u27e80\u21941+ i \u27e9 = fromPerm P.`\u27e80\u21941+ i \u27e9\n\n `\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) (xs : Vec A (suc n)) \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n `\u27e80\u21941+ i \u27e9-spec xs rewrite sym (P.`\u27e80\u21941+ i \u27e9-spec xs) | fromPerm-spec P.`\u27e80\u21941+ i \u27e9 xs = refl\n\n `\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij n\n `\u27e80\u2194 i \u27e9 = fromPerm P.`\u27e80\u2194 i \u27e9\n\n `\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) (xs : Vec A n) \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n `\u27e80\u2194 i \u27e9-spec xs rewrite sym (P.`\u27e80\u2194 i \u27e9-spec xs) | fromPerm-spec P.`\u27e80\u2194 i \u27e9 xs = refl\n\n `\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Bij n\n `\u27e8 i \u2194 j \u27e9 = fromPerm P.`\u27e8 i \u2194 j \u27e9\n\n `\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) (xs : Vec A n) \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n `\u27e8 i \u2194 j \u27e9-spec xs rewrite sym (P.`\u27e8 i \u2194 j \u27e9-spec xs) | fromPerm-spec P.`\u27e8 i \u2194 j \u27e9 xs = refl\n\nsum-\u2237\u02b3 : \u2200 {n} x (xs : Vec \u2115 n) \u2192 sum (xs \u2237\u02b3 x) \u2261 sum xs + x\nsum-\u2237\u02b3 x [] = \u2115\u00b0.+-comm x 0\nsum-\u2237\u02b3 x (x\u2081 \u2237 xs) rewrite sum-\u2237\u02b3 x xs | \u2115\u00b0.+-assoc x\u2081 (sum xs) x = refl\n\nrot\u2081 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\nrot\u2081 [] = []\nrot\u2081 (x \u2237 xs) = xs \u2237\u02b3 x\n\nrot : \u2200 {n a} {A : Set a} \u2192 \u2115 \u2192 Vec A n \u2192 Vec A n\nrot zero xs = xs\nrot (suc n) xs = rot n (rot\u2081 xs)\n\nsum-rot\u2081 : \u2200 {n} (xs : Vec \u2115 n) \u2192 sum xs \u2261 sum (rot\u2081 xs)\nsum-rot\u2081 [] = refl\nsum-rot\u2081 (x \u2237 xs) rewrite sum-\u2237\u02b3 x xs = \u2115\u00b0.+-comm x _\n\nmap-\u2237\u02b3 : \u2200 {n a} {A : Set a} (f : A \u2192 \u2115) x (xs : Vec A n) \u2192 map f (xs \u2237\u02b3 x) \u2261 map f xs \u2237\u02b3 f x\nmap-\u2237\u02b3 f x [] = refl\nmap-\u2237\u02b3 f x (x\u2081 \u2237 xs) rewrite map-\u2237\u02b3 f x xs = refl\n\nsum-map-rot\u2081 : \u2200 {n a} {A : Set a} (f : A \u2192 \u2115) (xs : Vec A n) \u2192 sum (map f (rot\u2081 xs)) \u2261 sum (map f xs)\nsum-map-rot\u2081 f [] = refl\nsum-map-rot\u2081 f (x \u2237 xs) rewrite \u2115\u00b0.+-comm (f x) (sum (map f xs))\n | \u2261.sym (sum-\u2237\u02b3 (f x) (map f xs))\n | \u2261.sym (map-\u2237\u02b3 f x xs)\n = refl\n\nprivate\n module Unused where\n module Foo where\n {-\n WRONG\n \u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n \u27e8 i \u2194 j \u27e9 = \u27e80\u2194 i \u27e9 \u2218 \u27e80\u2194 j \u27e9 \u2218 \u27e80\u2194 i \u27e9\n\n \u27e80\u2194\u27e9-spec : \u2200 {n a} {A : Set a} (i : Fin (suc n)) \u2192 \u27e80\u2194 i \u27e9 \u2257 \u27e8 zero \u2194 i \u27e9 {A = A}\n \u27e80\u2194\u27e9-spec _ _ = \u2261.refl\n -}\n\n \u27e80\u2194_\u27e9\u2032 : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n \u27e80\u2194_\u27e9\u2032 {zero} i xs = xs\n \u27e80\u2194_\u27e9\u2032 {suc n} i xs = lookup i xs \u2237 tail (xs [ i ]\u2254 head xs)\n\n -- \u27e8 i \u2194+1\u27e9: Exchange elements at position i and i + 1.\n \u27e8_\u2194+1\u27e9 : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n \u27e8 zero \u2194+1\u27e9 = 0\u21941\n \u27e8 suc i \u2194+1\u27e9 = map-tail \u27e8 i \u2194+1\u27e9\n\n \u27e8\u2194+1\u27e9-spec : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 \u27e8 inject\u2081 i \u2194+1\u27e9 \u2257 \u27e8 inject\u2081 i \u2194 suc i \u27e9 {A = A}\n \u27e8\u2194+1\u27e9-spec zero xs rewrite map-tail-id (0\u21941 xs) = \u2261.refl\n \u27e8\u2194+1\u27e9-spec (suc i) (x \u2237 xs) rewrite \u27e8\u2194+1\u27e9-spec i xs = \u2261.refl\n\n -- rot-up-to i (x\u2080 \u2237 x\u2081 \u2237 x\u2082 \u2237 \u2026 \u2237 x\u1d62 \u2237 xs)\n -- \u2261 (x\u2081 \u2237 x\u2082 \u2237 x\u2083 \u2237 \u2026 \u2237 x\u2080 \u2237 xs)\n rot-up-to : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n rot-up-to zero = id\n rot-up-to (suc i) = map-tail (rot-up-to i) \u2218 0\u21941\n\n -- Inverse of rot-up-to\n rot\u207b\u00b9-up-to : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n rot\u207b\u00b9-up-to zero = id\n rot\u207b\u00b9-up-to (suc i) = 0\u21941 \u2218 map-tail (rot\u207b\u00b9-up-to i)\n\n rot\u207b\u00b9-up-to\u2218rot-up-to : \u2200 {n} (i : Fin n) {a} {A : Set a} \u2192 rot\u207b\u00b9-up-to i \u2218 rot-up-to i \u2257 id {a} {Vec A n}\n rot\u207b\u00b9-up-to\u2218rot-up-to zero _ = \u2261.refl\n rot\u207b\u00b9-up-to\u2218rot-up-to (suc i) {A = A} (x\u2080 \u2237 []) rewrite rot\u207b\u00b9-up-to\u2218rot-up-to i {A = A} [] = \u2261.refl\n rot\u207b\u00b9-up-to\u2218rot-up-to (suc i) (x\u2080 \u2237 x\u2081 \u2237 xs) rewrite rot\u207b\u00b9-up-to\u2218rot-up-to i (x\u2080 \u2237 xs) = \u2261.refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"ade0337152617482eadda17198a9884581d80579","subject":"Bits: minor","message":"Bits: minor\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative.NP\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nopen import Relation.Nullary\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties.NP\nimport Data.List.NP as L\nopen import Function.Bijection.SyntaxKit\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0b \u2237 1b \u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u2219_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u2219 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u2219 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u2219-comm : \u2200 {m} (x y : A m) \u2192 x \u2219 y \u2261 y \u2219 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u2219-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nmodule OperationSyntax where\n module BitBij = BoolBijection\n open BitBij public using (`id) renaming (BoolBij to BitBij; bool-bijKit to bitBijKit; `not to `not\u1d2e)\n open BijectionSyntax Bit BitBij public\n open BijectionSemantics bitBijKit public\n\n {-\n id \u03b5 id\n 0\u21941 swp-inners interchange\n not swap comm\n if0 first ...\n if1 second ...\n -}\n `not : Bij\n `not = BitBij.`not `\u2237 const `id\n\n `xor : Bit \u2192 Bij\n `xor b = BitBij.`xor b `\u2237 const `id\n\n `if : Bij \u2192 Bij \u2192 Bij\n `if f g = BitBij.`id `\u2237 cond f g\n\n `if0 : Bij \u2192 Bij\n `if0 f = `if `id f\n\n `if1 : Bij \u2192 Bij\n `if1 f = `if f `id\n\n -- law: `if0 f `\u204f `if1 g \u2261 `if1 g `; `if0 f\n\n on-firsts : Bij \u2192 Bij\n on-firsts f = `0\u21941 `\u204f `if0 f `\u204f `0\u21941\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 right swap\n -- (a \u2219 d) \u2219 (c \u2219 b)\n swp-seconds : Bij\n swp-seconds = `if1 `not `\u204f `0\u21941 `\u204f `if1 `not\n\n on-extremes : Bij \u2192 Bij\n -- on-extremes f = swp-seconds `\u204f `if0 f `\u204f swp-seconds\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap \u2261 if1 not\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange \u2261 0\u21941\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 left f \u2261 if0 f\n -- (A \u2219 D) \u2219 (b \u2219 c)\n -- where A \u2219 D = f (a \u2219 d)\n -- \u2261 interchange \u2261 0\u21941\n -- (A \u2219 b) \u2219 (D \u2219 c)\n -- \u2261 right swap \u2261 if1 not\n -- (A \u2219 b) \u2219 (c \u2219 D)\n on-extremes f = `if1 `not `\u204f `0\u21941 `\u204f `if0 f `\u204f `0\u21941 `\u204f `if1 `not\n\n map-inner : Bij \u2192 Bij\n map-inner f = `if1 `not `\u204f `0\u21941 `\u204f `if1 f `\u204f `0\u21941 `\u204f `if1 `not\n\n map-outer : Bij \u2192 Bij \u2192 Bij\n map-outer f g = `if g f\n\n 0\u21941\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bij\n 0\u21941\u2237 [] = `not\n 0\u21941\u2237 (true {-1-} \u2237 p) = on-extremes (0\u21941\u2237 p)\n 0\u21941\u2237 (false{-0-} \u2237 p) = on-firsts (0\u21941\u2237 p)\n\n 0\u2194_ : \u2200 {n} \u2192 Bits n \u2192 Bij\n 0\u2194 [] = `id\n 0\u2194 (false{-0-} \u2237 p) = `if0 (0\u2194 p)\n 0\u2194 (true{-1-} \u2237 p) = 0\u21941\u2237 p\n\n \u27e80\u2194_\u27e9-sem : \u2200 {n} (p : Bits n) \u2192 Bits n \u2192 Bits n\n \u27e80\u2194 p \u27e9-sem xs = if 0\u207f == xs then p else if p == xs then 0\u207f else xs\n\n if\u2237 : \u2200 {n} a x (xs ys : Bits n) \u2192 (if a then (x \u2237 xs) else (x \u2237 ys)) \u2261 x \u2237 (if a then xs else ys)\n if\u2237 true x xs ys = refl\n if\u2237 false x xs ys = refl\n\n if-not\u2237 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (false \u2237 xs) else (true \u2237 ys)) \u2261 (not a) \u2237 (if a then xs else ys)\n if-not\u2237 true xs ys = refl\n if-not\u2237 false xs ys = refl\n\n if\u2237\u2032 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (true \u2237 xs) else (false \u2237 ys)) \u2261 a \u2237 (if a then xs else ys)\n if\u2237\u2032 true xs ys = refl\n if\u2237\u2032 false xs ys = refl\n\n \u27e80\u21941\u2237_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u21941\u2237 p \u2219 xs \u2261 \u27e80\u2194 (1\u2237 p) \u27e9-sem xs\n \u27e80\u21941\u2237_\u27e9-spec [] (true \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec [] (false \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 true \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 false \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n\n \u27e80\u2194_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u2194 p \u2219 xs \u2261 \u27e80\u2194 p \u27e9-sem xs\n \u27e80\u2194_\u27e9-spec [] [] = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (true \u2237 xs) = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (false \u2237 xs)\n rewrite \u27e80\u2194 ps \u27e9-spec xs\n | if\u2237 (ps == xs) 0b 0\u207f xs\n | if\u2237 (0\u207f == xs) 0b ps (if ps == xs then 0\u207f else xs)\n = refl\n \u27e80\u2194_\u27e9-spec (true \u2237 p) xs = \u27e80\u21941\u2237 p \u27e9-spec xs\n\n private\n module P where\n open PermutationSyntax public\n open PermutationSemantics public\n open P using (Perm; `id; `0\u21941; _`\u204f_)\n\n `\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij\n `\u27e80\u21941+ zero \u27e9 = `0\u21941\n `\u27e80\u21941+ suc i \u27e9 = `0\u21941 `\u204f `tl `\u27e80\u21941+ i \u27e9 `\u204f `0\u21941\n\n `\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n `\u27e80\u21941+ zero \u27e9-spec xs = refl\n `\u27e80\u21941+ suc i \u27e9-spec (x \u2237 _ \u2237 xs) rewrite `\u27e80\u21941+ i \u27e9-spec (x \u2237 xs) = refl\n\n `\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij\n `\u27e80\u2194 zero \u27e9 = `id\n `\u27e80\u2194 suc i \u27e9 = `\u27e80\u21941+ i \u27e9\n\n `\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n `\u27e80\u2194 zero \u27e9-spec xs = refl\n `\u27e80\u2194 suc i \u27e9-spec xs = `\u27e80\u21941+ i \u27e9-spec xs\n\n {-\n `\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Bij\n `\u27e8 zero \u2194 j \u27e9 = `\u27e80\u2194 j \u27e9\n `\u27e8 i \u2194 zero \u27e9 = `\u27e80\u2194 i \u27e9\n `\u27e8 suc i \u2194 suc j \u27e9 = `tl `\u27e8 i \u2194 j \u27e9\n\n `\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) xs \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n `\u27e8_\u2194_\u27e9-spec zero j xs = `\u27e80\u2194 j \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) zero xs = `\u27e80\u21941+ i \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) (suc j) (x \u2237 xs) rewrite `\u27e8 i \u2194 j \u27e9-spec xs = refl\n -}\n\n `xor-head : Bit \u2192 Bij\n `xor-head b = if b then `not else `id\n\n `xor-head-spec : \u2200 b {n} x (xs : Bits n) \u2192 `xor-head b \u2219 (x \u2237 xs) \u2261 (b xor x) \u2237 xs\n `xor-head-spec true x xs = refl\n `xor-head-spec false x xs = refl\n\n `\u27e8_\u2295\u27e9 : \u2200 {n} \u2192 Bits n \u2192 Bij\n `\u27e8 [] \u2295\u27e9 = `id\n `\u27e8 b \u2237 xs \u2295\u27e9 = `xor-head b `\u204f `tl `\u27e8 xs \u2295\u27e9\n\n `\u27e8_\u2295\u27e9-spec : \u2200 {n} (xs ys : Bits n) \u2192 `\u27e8 xs \u2295\u27e9 \u2219 ys \u2261 xs \u2295 ys\n `\u27e8 [] \u2295\u27e9-spec [] = refl\n `\u27e8 true \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n `\u27e8 false \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) x \u2192 0\u21941 pad \u2295 0\u21941 x \u2261 0\u21941 (pad \u2295 x)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\nmodule PermutationSyntax-Props where\n open PermutationSyntax\n open PermutationSemantics\n -- open PermutationProperties\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) xs \u2192 0\u21941 pad \u2295 0\u21941 xs \u2261 0\u21941 (pad \u2295 xs)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\n -- TODO make use of \u229b-dist-\u2219\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 fs `id xs = refl\n \u2295-dist-\u2219 fs `0\u21941 xs = \u2295-dist-0\u21941 fs xs\n \u2295-dist-\u2219 [] (`tl \u03c0) [] = refl\n \u2295-dist-\u2219 (f \u2237 fs) (`tl \u03c0) (x \u2237 xs) rewrite \u2295-dist-\u2219 fs \u03c0 xs = refl\n \u2295-dist-\u2219 fs (\u03c0\u2080 `\u204f \u03c0\u2081) xs rewrite \u2295-dist-\u2219 (\u03c0\u2080 \u2219 fs) \u03c0\u2081 (\u03c0\u2080 \u2219 xs)\n | \u2295-dist-\u2219 fs \u03c0\u2080 xs = refl\n {-\n -- \u229b-dist-\u2219 : \u2200 {n a} {A : Set a} (fs : Vec (A \u2192 A) n) \u03c0 xs \u2192 \u03c0 \u2219 fs \u229b \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (fs \u229b xs)\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 pad \u03c0 xs = \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs\n \u2261\u27e8 refl \u27e9\n vmap _xor_ (\u03c0 \u2219 pad) \u229b \u03c0 \u2219 xs\n \u2261\u27e8 TODO \u27e9\n \u03c0 \u2219 vmap _xor_ pad \u229b \u03c0 \u2219 xs\n \u2261\u27e8 \u229b-dist-\u2219 _ (vmap _xor_ pad) \u03c0 xs \u27e9\n \u03c0 \u2219 (vmap _xor_ pad \u229b xs)\n \u2261\u27e8 refl \u27e9\n \u03c0 \u2219 (pad \u2295 xs)\n \u220e where open \u2261-Reasoning\n -- rans {!\u229b-dist-\u2219 (vmap _xor_ (op \u2219 pad)) op xs!} (\u229b-dist-\u2219 (vmap _xor_ pad) op xs)\n-}\nmodule SimpleSearch {a} {A : Set a} (_\u2219_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u2219_ public\n\n module SearchUnit \u03b5 (\u03b5\u2219\u03b5 : \u03b5 \u2219 \u03b5 \u2261 \u03b5) where\n search-const\u03b5\u2261\u03b5 : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-const\u03b5\u2261\u03b5 zero = refl\n search-const\u03b5\u2261\u03b5 (suc n) rewrite search-const\u03b5\u2261\u03b5 n = \u03b5\u2219\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n module SearchInterchange (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u2219 f\u2081 x) \u2261 search f\u2080 \u2219 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u2219-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e where open \u2261-Reasoning\n\n search-0\u21941 : \u2200 {n} (f : Bits n \u2192 A) \u2192 search {n} (f \u2218 0\u21941) \u2261 search {n} f\n search-0\u21941 {zero} _ = refl\n search-0\u21941 {suc zero} _ = refl\n search-0\u21941 {suc (suc n)} _ = \u2219-interchange _ _ _ _\n\n module Bij (\u2219-comm : Commutative _\u2261_ _\u2219_)\n (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n open SearchInterchange \u2219-interchange using (search-0\u21941)\n open OperationSyntax hiding (_\u2219_)\n search-bij : \u2200 {n} f (g : Bits n \u2192 A) \u2192 search (g \u2218 eval f) \u2261 search g\n search-bij `id _ = refl\n search-bij `0\u21941 f = search-0\u21941 f\n search-bij (f `\u204f g) h\n rewrite search-bij f (h \u2218 eval g)\n | search-bij g h\n = refl\n search-bij {zero} (_ `\u2237 _) _ = refl\n search-bij {suc n} (`id `\u2237 f) g\n rewrite search-bij (f 0b) (g \u2218 0\u2237_)\n | search-bij (f 1b) (g \u2218 1\u2237_)\n = refl\n search-bij {suc n} (`not\u1d2e `\u2237 f) g\n rewrite search-bij (f 1b) (g \u2218 0\u2237_)\n | search-bij (f 0b) (g \u2218 1\u2237_)\n = \u2219-comm _ _\n\nmodule Sum where\n open SimpleSearch _+_ using (module Comm; module SearchInterchange; module SearchUnit; module Bij)\n open SimpleSearch _+_ public using () renaming (search to sum; search-\u2257 to sum-\u2257; searchBit to sumBit;\n search-\u2257\u2082 to sum-\u2257\u2082;\n search-+ to sum-+)\n open Comm \u2115\u00b0.+-comm public renaming (search-comm to sum-comm)\n open SearchUnit 0 refl public renaming\n (search-const\u03b5\u2261\u03b5 to sum-const0\u22610)\n open SearchInterchange +-interchange public renaming (\n search-dist to sum-dist;\n search-searchBit to sum-sumBit;\n search-search to sum-sum;\n search-swap to sum-swap)\n open Bij \u2115\u00b0.+-comm +-interchange public renaming (search-bij to sum-bij)\n\n sum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\n sum-const zero _ = refl\n sum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nmodule Count where\n open Sum\n open OperationSyntax\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n #\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n -- #-ext\n #-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n #-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n #-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n #-bij : \u2200 {n} f (g : Bits n \u2192 Bit) \u2192 #\u27e8 g \u2218 eval f \u27e9 \u2261 #\u27e8 g \u27e9\n #-bij f g = sum-bij f (Bool.to\u2115 \u2218 g)\n\n #-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n #-\u2295 = #-comm\n\n #-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n #-const n b = sum-const n (Bool.to\u2115 b)\n\n #never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n #never\u22610 = sum-const0\u22610\n\n #always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n #always\u22612^ n = sum-const n 1\n\n #-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 Bit) \u2192 sum (\u03bb x \u2192 Bool.to\u2115 (f\u2080 x) + Bool.to\u2115 (f\u2081 x)) \u2261 #\u27e8 f\u2080 \u27e9 + #\u27e8 f\u2081 \u27e9\n #-dist f\u2080 f\u2081 = sum-dist (Bool.to\u2115 \u2218 f\u2080) (Bool.to\u2115 \u2218 f\u2081)\n\n #-+ : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n #\u27e8 f \u27e9 \u2261 sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9 )\n #-+ {m} {n} f = sum-+ {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-# : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9)\n \u2261 sum {n} (\u03bb ys \u2192 #\u27e8 (\u03bb (xs : Bits m) \u2192 f (xs ++ ys)) \u27e9)\n #-# {m} {n} f = sum-sum {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-swap : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192 #\u27e8 f \u2218 vswap n {m} \u27e9 \u2261 #\u27e8 f \u27e9\n #-swap {m} {n} f = sum-swap {m} {n} (Bool.to\u2115 \u2218 f)\n\n #\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n #\u27e8== [] \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n \u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n \u2257-cong-# f g f\u2257g = sum-\u2257 _ _ (cong Bool.to\u2115 \u2218 f\u2257g)\n\n -- #-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n -- #-+ f f0 f1 rewrite f0 | f1 = refl\n\n #-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-take-drop zero n f g with f []\n ... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n ... | false = #never\u22610 n\n #-take-drop (suc m) n f g\n rewrite \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x))\n | #-take-drop m n (f \u2218 0\u2237_) g\n | \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x))\n | #-take-drop m n (f \u2218 1\u2237_) g\n = sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9)\n\n #-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n #\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n #\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n #\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n ... | true | true | _ = s\u2264s z\u2264n\n ... | true | false | p = \u22a5-elim (p _)\n ... | false | _ | _ = z\u2264n\n #\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n #-\u2227-\u2228\u1d47 : \u2200 x y \u2192 Bool.to\u2115 (x \u2227 y) + Bool.to\u2115 (x \u2228 y) \u2261 Bool.to\u2115 x + Bool.to\u2115 y\n #-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (Bool.to\u2115 y) 1 = refl\n #-\u2227-\u2228\u1d47 false y = refl\n\n #-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n #-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #\u2228' {zero} f g with f []\n ... | true = s\u2264s z\u2264n\n ... | false = \u2115\u2264.refl\n #\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n #\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n #\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n #\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n #-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n #-bound zero f = Bool.to\u2115\u22641 (f [])\n #-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n #-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n #-\u2218vnot _ f = #-\u2295 1\u207f f\n\n #-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n #-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n #-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n #-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n #-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n #-not\u2218 zero f with f []\n ... | true = \u2261.refl\n ... | false = \u2261.refl\n #-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n #-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n #-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\nopen SimpleSearch public\nopen Sum public\nopen Count public\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192\n search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = search-\u2257 op _ _ (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2^ n) (toFin xs)\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2^ n + to\u2115 xs\n\nto\u2115-bound : \u2200 {n} (xs : Bits n) \u2192 to\u2115 xs < 2^ n \nto\u2115-bound [] = s\u2264s z\u2264n\nto\u2115-bound {suc n} (1b \u2237 xs) rewrite +-assoc-comm 1 (2^ n) (to\u2115 xs) = \u2115\u2264.refl {2^ n} +-mono to\u2115-bound xs\nto\u2115-bound {suc n} (0b \u2237 xs) = \u2264-steps (2^ n) (to\u2115-bound xs)\n\nto\u2115\u22642\u207f+ : \u2200 {n} (x : Bits n) {y} \u2192 to\u2115 {n} x \u2264 2^ n + y\nto\u2115\u22642\u207f+ {n} x {y} = \u2115\u2264.trans (\u2264-steps y (\u2264-pred (\u2264-steps 1 (to\u2115-bound x))))\n (\u2115\u2264.reflexive (\u2115\u00b0.+-comm y (2^ n)))\n\n2\u207f+\u2270to\u2115 : \u2200 {n x} (y : Bits n) \u2192 2^ n + x \u2270 to\u2115 {n} y\n2\u207f+\u2270to\u2115 {n} {x} y p = \u00acn+\u2264y \u00aca \u00acb c = \ud835\udfd8-elim (p (\u2264-pred c))\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (\u2264-steps 1 1+n\u2264m)\n\nnot<=\u2192< : \u2200 x y \u2192 \u2713 (not (x <= y)) \u2192 \u2713 (suc y <= x)\nnot<=\u2192< x y p = <=.complete (\u2270\u2192< x y (\u2713-not-\u00ac p \u2218 <=.complete))\n\neven? odd? : \u2115 \u2192 \ud835\udfda\neven? zero = 1\u2082\neven? (suc n) = odd? n \nodd? n = not (even? n)\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"-- NOTE with-K so far\n-- TODO {-# OPTIONS --without-K #-}\nmodule Data.Nat.NP where\n\nopen import Type hiding (\u2605)\nimport Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat public hiding (module GeneralisedArithmetic; module \u2264-Reasoning; fold)\nopen import Data.Nat.Properties\nopen import Data.Nat.Logical\nopen import Data.Two hiding (_==_)\nimport Data.Two.Equality as \ud835\udfda==\nopen import Data.Product using (proj\u2081; proj\u2082; \u2203; _,_)\nopen import Data.Sum renaming (map to \u228e-map)\nopen import Data.Zero using (\ud835\udfd8-elim; \ud835\udfd8)\nopen import Function.NP\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_; _\u2257_; module \u2261-Reasoning; !_; _\u2219_; ap) renaming (refl to idp)\n\n\u2115\u02e2 = \u2261.setoid \u2115\n\nmodule \u2115cmp = StrictTotalOrder strictTotalOrder\nmodule \u2115\u2264 = DecTotalOrder decTotalOrder\nmodule \u2115\u00b0 = Algebra.CommutativeSemiring commutativeSemiring\nmodule \u2115+ = Algebra.CommutativeMonoid \u2115\u00b0.+-commutativeMonoid\nmodule \u2115+\u2032 = Algebra.Monoid \u2115\u00b0.+-monoid\nmodule \u2294\u00b0 = Algebra.CommutativeSemiringWithoutOne \u2294-\u2293-0-commutativeSemiringWithoutOne\nmodule \u2115\u02e2 = Setoid \u2115\u02e2\n\ninfixr 8 _\u2219\u2264_\n_\u2219\u2264_ = \u2115\u2264.trans\n_\u2219cmp_ = \u2115cmp.trans\n_\u2219<_ = <-trans\n\n[P:_zero:_suc:_] : \u2200 {p} (P : \u2115 \u2192 \u2605 p) \u2192 P zero \u2192 (\u2200 {n} \u2192 P n \u2192 P (suc n)) \u2192 \u2200 n \u2192 P n\n[P: _ zero: z suc: _ ] zero = z\n[P: P zero: z suc: s ] (suc n) = s ([P: P zero: z suc: s ] n)\n\n[zero:_suc:_] : \u2200 {a} {A : \u2605 a} \u2192 A \u2192 (\u2115 \u2192 A \u2192 A) \u2192 \u2115 \u2192 A\n[zero: z suc: s ] = [P: _ zero: z suc: (\u03bb {n} \u2192 s n) ]\n\nmodule \u2264-Reasoning where\n open Preorder-Reasoning \u2115\u2264.preorder public renaming (_\u223c\u27e8_\u27e9_ to _\u2264\u27e8_\u27e9_)\n infixr 2 _\u2261\u27e8_\u27e9_\n _\u2261\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x \u2261 y \u2192 y \u2264 z \u2192 x \u2264 z\n _ \u2261\u27e8 idp \u27e9 p = p\n infixr 2 _<\u27e8_\u27e9_\n _<\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x < y \u2192 y \u2264 z \u2192 x < z\n x <\u27e8 p \u27e9 q = suc x \u2264\u27e8 p \u27e9 q\n\nsuc-injective : \u2200 {n m : \u2115} \u2192 \u2115.suc n \u2261 suc m \u2192 n \u2261 m\nsuc-injective = ap pred\n\n+-\u2264-inj : \u2200 x {y z} \u2192 x + y \u2264 x + z \u2192 y \u2264 z\n+-\u2264-inj zero = id\n+-\u2264-inj (suc x) = +-\u2264-inj x \u2218 \u2264-pred\n\ninfixl 6 _+\u00b0_\ninfixl 7 _*\u00b0_ _\u2293\u00b0_\ninfixl 6 _\u2238\u00b0_ _\u2294\u00b0_\n\n_+\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 \u2115) \u2192 A \u2192 \u2115\n(f +\u00b0 g) x = f x + g x\n\n_\u2238\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 \u2115) \u2192 A \u2192 \u2115\n(f \u2238\u00b0 g) x = f x \u2238 g x\n\n_*\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 \u2115) \u2192 A \u2192 \u2115\n(f *\u00b0 g) x = f x * g x\n\n_\u2294\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 \u2115) \u2192 A \u2192 \u2115\n(f \u2294\u00b0 g) x = f x \u2294 g x\n\n_\u2293\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 \u2115) \u2192 A \u2192 \u2115\n(f \u2293\u00b0 g) x = f x \u2293 g x\n\n-- this one is not completly in line with the\n-- others\n_\u2264\u00b0_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 \u2115) \u2192 \u2605 a\nf \u2264\u00b0 g = \u2200 x \u2192 f x \u2264 g x\n\nsucx\u2270x : \u2200 x \u2192 suc x \u2270 x\nsucx\u2270x zero = \u03bb()\nsucx\u2270x (suc x) = sucx\u2270x x \u2218 \u2264-pred\n\ntotal-\u2264 : \u2200 a b \u2192 a \u2264 b \u228e b \u2264 a\ntotal-\u2264 zero b = inj\u2081 z\u2264n\ntotal-\u2264 (suc a) zero = inj\u2082 z\u2264n\ntotal-\u2264 (suc a) (suc b) with total-\u2264 a b\n... | inj\u2081 p = inj\u2081 (s\u2264s p)\n... | inj\u2082 p = inj\u2082 (s\u2264s p)\n\na\u2261a\u2293b+a\u2238b : \u2200 a b \u2192 a \u2261 a \u2293 b + (a \u2238 b)\na\u2261a\u2293b+a\u2238b zero zero = idp\na\u2261a\u2293b+a\u2238b zero (suc b) = idp\na\u2261a\u2293b+a\u2238b (suc a) zero = idp\na\u2261a\u2293b+a\u2238b (suc a) (suc b) rewrite ! a\u2261a\u2293b+a\u2238b a b = idp\n\n\u00acn\u2264x \u00aca \u00acb c = \ud835\udfd8-elim (p (\u2264-pred c))\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (\u2264-steps 1 1+n\u2264m)\n\nnot<=\u2192< : \u2200 x y \u2192 \u2713 (not (x <= y)) \u2192 \u2713 (suc y <= x)\nnot<=\u2192< x y p = <=.complete (\u2270\u2192< x y (\u2713-not-\u00ac p \u2218 <=.complete))\n\neven? odd? : \u2115 \u2192 \ud835\udfda\neven? zero = 1\u2082\neven? (suc n) = odd? n \nodd? n = not (even? n)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"20096c73e1203a5dcd9f0ce97c3f22ecc30c0414","subject":"Cosmetic changes.","message":"Cosmetic changes.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/FOT\/FOTC\/Program\/Nest\/NestBC-SL.agda","new_file":"notes\/FOT\/FOTC\/Program\/Nest\/NestBC-SL.agda","new_contents":"------------------------------------------------------------------------------\n-- Example of a nested recursive function using the Bove-Capretta\n-- method\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- From: Bove, A. and Capretta, V. (2001). Nested General Recursion\n-- and Partiality in Type Theory. In: Theorem Proving in Higher Order\n-- Logics (TPHOLs 2001). Ed. by Boulton, R. J. and Jackson,\n-- P. B. Vol. 2152. LNCS. Springer, pp. 121\u2013135.\n\nmodule FOT.FOTC.Program.Nest.NestBC-SL where\n\nopen import Data.Nat renaming ( suc to succ )\nopen import Data.Nat.Properties\n\nopen import Induction\nopen import Induction.Nat\n\nopen import Relation.Binary\n\nmodule NDTO = DecTotalOrder decTotalOrder\n\n------------------------------------------------------------------------------\n-- The original non-terminating function.\n\n{-# NO_TERMINATION_CHECK #-}\nnestI : \u2115 \u2192 \u2115\nnestI 0 = 0\nnestI (succ n) = nestI (nestI n)\n\n\u2264\u2032-trans : Transitive _\u2264\u2032_\n\u2264\u2032-trans i\u2264\u2032j j\u2264\u2032k = \u2264\u21d2\u2264\u2032 (NDTO.trans (\u2264\u2032\u21d2\u2264 i\u2264\u2032j) (\u2264\u2032\u21d2\u2264 j\u2264\u2032k))\n\nmutual\n -- The domain predicate of the nest function.\n data NestDom : \u2115 \u2192 Set where\n nestDom0 : NestDom 0\n nestDomS : \u2200 {n} \u2192 (h\u2081 : NestDom n) \u2192\n (h\u2082 : NestDom (nestD n h\u2081)) \u2192\n NestDom (succ n)\n\n -- The nest function by structural recursion on the domain predicate.\n nestD : \u2200 n \u2192 NestDom n \u2192 \u2115\n nestD .0 nestDom0 = 0\n nestD .(succ n) (nestDomS {n} h\u2081 h\u2082) = nestD (nestD n h\u2081) h\u2082\n\nnestD-\u2264\u2032 : \u2200 n \u2192 (h : NestDom n) \u2192 nestD n h \u2264\u2032 n\nnestD-\u2264\u2032 .0 nestDom0 = \u2264\u2032-refl\nnestD-\u2264\u2032 .(succ n) (nestDomS {n} h\u2081 h\u2082) =\n \u2264\u2032-trans (\u2264\u2032-trans (nestD-\u2264\u2032 (nestD n h\u2081) h\u2082) (nestD-\u2264\u2032 n h\u2081))\n (\u2264\u2032-step \u2264\u2032-refl)\n\n-- The nest function is total.\nallNestDom : \u2200 n \u2192 NestDom n\nallNestDom = build <-rec-builder P ih\n where\n P : \u2115 \u2192 Set\n P = NestDom\n\n ih : \u2200 y \u2192 <-Rec P y \u2192 P y\n ih zero rec = nestDom0\n ih (succ y) rec = nestDomS nd-y (rec (nestD y nd-y) (s\u2264\u2032s (nestD-\u2264\u2032 y nd-y)))\n where\n helper : \u2200 x \u2192 x <\u2032 y \u2192 P x\n helper x Sx\u2264\u2032y = rec x (\u2264\u2032-step Sx\u2264\u2032y)\n\n nd-y : NestDom y\n nd-y = ih y helper\n\n-- The nest function.\nnest : \u2115 \u2192 \u2115\nnest n = nestD n (allNestDom n)\n","old_contents":"------------------------------------------------------------------------------\n-- Example of a nested recursive function using the Bove-Capretta\n-- method\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- From: Ana Bove and Venanzio Capretta. Nested general recursion and\n-- partiality in type theory. vol 2152 LNCS. 2001.\n\nmodule FOT.FOTC.Program.Nest.NestBC-SL where\n\nopen import Data.Nat renaming ( suc to succ )\nopen import Data.Nat.Properties\n\nopen import Induction\nopen import Induction.Nat\n\nopen import Relation.Binary\n\nmodule NDTO = DecTotalOrder decTotalOrder\n\n------------------------------------------------------------------------------\n\n-- The original non-terminating function.\n\n{-# NO_TERMINATION_CHECK #-}\nnestI : \u2115 \u2192 \u2115\nnestI 0 = 0\nnestI (succ n) = nestI (nestI n)\n\n\u2264\u2032-trans : Transitive _\u2264\u2032_\n\u2264\u2032-trans i\u2264\u2032j j\u2264\u2032k = \u2264\u21d2\u2264\u2032 (NDTO.trans (\u2264\u2032\u21d2\u2264 i\u2264\u2032j) (\u2264\u2032\u21d2\u2264 j\u2264\u2032k))\n\nmutual\n -- The domain predicate of the nest function.\n data NestDom : \u2115 \u2192 Set where\n nestDom0 : NestDom 0\n nestDomS : \u2200 {n} \u2192 (h\u2081 : NestDom n) \u2192\n (h\u2082 : NestDom (nestD n h\u2081)) \u2192\n NestDom (succ n)\n\n -- The nest function by structural recursion on the domain predicate.\n nestD : \u2200 n \u2192 NestDom n \u2192 \u2115\n nestD .0 nestDom0 = 0\n nestD .(succ n) (nestDomS {n} h\u2081 h\u2082) = nestD (nestD n h\u2081) h\u2082\n\nnestD-\u2264\u2032 : \u2200 n \u2192 (h : NestDom n) \u2192 nestD n h \u2264\u2032 n\nnestD-\u2264\u2032 .0 nestDom0 = \u2264\u2032-refl\nnestD-\u2264\u2032 .(succ n) (nestDomS {n} h\u2081 h\u2082) =\n \u2264\u2032-trans (\u2264\u2032-trans (nestD-\u2264\u2032 (nestD n h\u2081) h\u2082) (nestD-\u2264\u2032 n h\u2081))\n (\u2264\u2032-step \u2264\u2032-refl)\n\n-- The nest function is total.\nallNestDom : \u2200 n \u2192 NestDom n\nallNestDom = build <-rec-builder P ih\n where\n P : \u2115 \u2192 Set\n P = NestDom\n\n ih : \u2200 y \u2192 <-Rec P y \u2192 P y\n ih zero rec = nestDom0\n ih (succ y) rec = nestDomS nd-y (rec (nestD y nd-y) (s\u2264\u2032s (nestD-\u2264\u2032 y nd-y)))\n where\n helper : \u2200 x \u2192 x <\u2032 y \u2192 P x\n helper x Sx\u2264\u2032y = rec x (\u2264\u2032-step Sx\u2264\u2032y)\n\n nd-y : NestDom y\n nd-y = ih y helper\n\n-- The nest function.\nnest : \u2115 \u2192 \u2115\nnest n = nestD n (allNestDom n)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ce69f696ace671125437eeb170328cc742ca9336","subject":"Bits: Add _<=_","message":"Bits: Add _<=_\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative.NP\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_) renaming (_<=_ to _\u2115<=_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nopen import Relation.Nullary\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties.NP\nimport Data.List.NP as L\nopen import Function.Bijection.SyntaxKit\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\n_<=_ : \u2200 {n} (xs ys : Bits n) \u2192 Bool\n[] <= [] = 1b\n(1b \u2237 xs) <= (0b \u2237 ys) = 0b\n(0b \u2237 xs) <= (1b \u2237 ys) = 1b\n(_ \u2237 xs) <= (_ \u2237 ys) = xs <= ys\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0b \u2237 1b \u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u2219_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u2219 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u2219 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u2219-comm : \u2200 {m} (x y : A m) \u2192 x \u2219 y \u2261 y \u2219 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u2219-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nmodule OperationSyntax where\n module BitBij = BoolBijection\n open BitBij public using (`id) renaming (BoolBij to BitBij; bool-bijKit to bitBijKit; `not to `not\u1d2e)\n open BijectionSyntax Bit BitBij public\n open BijectionSemantics bitBijKit public\n\n {-\n id \u03b5 id\n 0\u21941 swp-inners interchange\n not swap comm\n if0 first ...\n if1 second ...\n -}\n `not : \u2200 {n} \u2192 Bij (1 + n)\n `not = BitBij.`not `\u2237 const `id\n\n `xor : \u2200 {n} \u2192 Bit \u2192 Bij (1 + n)\n `xor b = BitBij.`xor b `\u2237 const `id\n\n `if : \u2200 {n} \u2192 Bij n \u2192 Bij n \u2192 Bij (1 + n)\n `if f g = BitBij.`id `\u2237 cond f g\n\n `if0 : \u2200 {n} \u2192 Bij n \u2192 Bij (1 + n)\n `if0 f = `if `id f\n\n `if1 : \u2200 {n} \u2192 Bij n \u2192 Bij (1 + n)\n `if1 f = `if f `id\n\n -- law: `if0 f `\u204f `if1 g \u2261 `if1 g `; `if0 f\n\n on-firsts : \u2200 {n} \u2192 Bij (1 + n) \u2192 Bij (2 + n)\n on-firsts f = `0\u21941 `\u204f `if0 f `\u204f `0\u21941\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 right swap\n -- (a \u2219 d) \u2219 (c \u2219 b)\n swp-seconds : \u2200 {n} \u2192 Bij (2 + n)\n swp-seconds = `if1 `not `\u204f `0\u21941 `\u204f `if1 `not\n\n on-extremes : \u2200 {n} \u2192 Bij (1 + n) \u2192 Bij (2 + n)\n -- on-extremes f = swp-seconds `\u204f `if0 f `\u204f swp-seconds\n\n -- (a \u2219 b) \u2219 (c \u2219 d)\n -- \u2261 right swap \u2261 if1 not\n -- (a \u2219 b) \u2219 (d \u2219 c)\n -- \u2261 interchange \u2261 0\u21941\n -- (a \u2219 d) \u2219 (b \u2219 c)\n -- \u2261 left f \u2261 if0 f\n -- (A \u2219 D) \u2219 (b \u2219 c)\n -- where A \u2219 D = f (a \u2219 d)\n -- \u2261 interchange \u2261 0\u21941\n -- (A \u2219 b) \u2219 (D \u2219 c)\n -- \u2261 right swap \u2261 if1 not\n -- (A \u2219 b) \u2219 (c \u2219 D)\n on-extremes f = `if1 `not `\u204f `0\u21941 `\u204f `if0 f `\u204f `0\u21941 `\u204f `if1 `not\n\n map-inner : \u2200 {n} \u2192 Bij (1 + n) \u2192 Bij (2 + n)\n map-inner f = `if1 `not `\u204f `0\u21941 `\u204f `if1 f `\u204f `0\u21941 `\u204f `if1 `not\n\n map-outer : \u2200 {n} \u2192 Bij n \u2192 Bij n \u2192 Bij (1 + n)\n map-outer f g = `if g f\n\n 0\u21941\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bij (1 + n)\n 0\u21941\u2237 [] = `not\n 0\u21941\u2237 (true {-1-} \u2237 p) = on-extremes (0\u21941\u2237 p)\n 0\u21941\u2237 (false{-0-} \u2237 p) = on-firsts (0\u21941\u2237 p)\n\n 0\u2194_ : \u2200 {n} \u2192 Bits n \u2192 Bij n\n 0\u2194 [] = `id\n 0\u2194 (false{-0-} \u2237 p) = `if0 (0\u2194 p)\n 0\u2194 (true{-1-} \u2237 p) = 0\u21941\u2237 p\n\n \u27e80\u2194_\u27e9-sem : \u2200 {n} (p : Bits n) \u2192 Bits n \u2192 Bits n\n \u27e80\u2194 p \u27e9-sem xs = if 0\u207f == xs then p else if p == xs then 0\u207f else xs\n\n if\u2237 : \u2200 {n} a x (xs ys : Bits n) \u2192 (if a then (x \u2237 xs) else (x \u2237 ys)) \u2261 x \u2237 (if a then xs else ys)\n if\u2237 true x xs ys = refl\n if\u2237 false x xs ys = refl\n\n if-not\u2237 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (false \u2237 xs) else (true \u2237 ys)) \u2261 (not a) \u2237 (if a then xs else ys)\n if-not\u2237 true xs ys = refl\n if-not\u2237 false xs ys = refl\n\n if\u2237\u2032 : \u2200 {n} a (xs ys : Bits n) \u2192 (if a then (true \u2237 xs) else (false \u2237 ys)) \u2261 a \u2237 (if a then xs else ys)\n if\u2237\u2032 true xs ys = refl\n if\u2237\u2032 false xs ys = refl\n\n \u27e80\u21941\u2237_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u21941\u2237 p \u2219 xs \u2261 \u27e80\u2194 (1\u2237 p) \u27e9-sem xs\n \u27e80\u21941\u2237_\u27e9-spec [] (true \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec [] (false \u2237 []) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 true \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (true \u2237 false \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (true \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (true \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (1\u2237 xs)\n with ps == xs\n ... | true = refl\n ... | false = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 true \u2237 xs) = refl\n \u27e80\u21941\u2237_\u27e9-spec (false \u2237 ps) (false \u2237 false \u2237 xs)\n rewrite \u27e80\u21941\u2237_\u27e9-spec ps (0\u2237 xs)\n with 0\u207f == xs\n ... | true = refl\n ... | false = refl\n\n \u27e80\u2194_\u27e9-spec : \u2200 {n} (p : Bits n) xs \u2192 0\u2194 p \u2219 xs \u2261 \u27e80\u2194 p \u27e9-sem xs\n \u27e80\u2194_\u27e9-spec [] [] = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (true \u2237 xs) = refl\n \u27e80\u2194_\u27e9-spec (false \u2237 ps) (false \u2237 xs)\n rewrite \u27e80\u2194 ps \u27e9-spec xs\n | if\u2237 (ps == xs) 0b 0\u207f xs\n | if\u2237 (0\u207f == xs) 0b ps (if ps == xs then 0\u207f else xs)\n = refl\n \u27e80\u2194_\u27e9-spec (true \u2237 p) xs = \u27e80\u21941\u2237 p \u27e9-spec xs\n\n private\n module P where\n open PermutationSyntax public\n open PermutationSemantics public\n open P using (Perm; `id; `0\u21941; _`\u204f_)\n\n `\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij (1 + n)\n `\u27e80\u21941+ zero \u27e9 = `0\u21941\n `\u27e80\u21941+ suc i \u27e9 = `0\u21941 `\u204f `tl `\u27e80\u21941+ i \u27e9 `\u204f `0\u21941\n\n `\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n `\u27e80\u21941+ zero \u27e9-spec xs = refl\n `\u27e80\u21941+ suc i \u27e9-spec (x \u2237 _ \u2237 xs) rewrite `\u27e80\u21941+ i \u27e9-spec (x \u2237 xs) = refl\n\n `\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Bij n\n `\u27e80\u2194 zero \u27e9 = `id\n `\u27e80\u2194 suc i \u27e9 = `\u27e80\u21941+ i \u27e9\n\n `\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n `\u27e80\u2194 zero \u27e9-spec xs = refl\n `\u27e80\u2194 suc i \u27e9-spec xs = `\u27e80\u21941+ i \u27e9-spec xs\n\n {-\n `\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Bij n\n `\u27e8 zero \u2194 j \u27e9 = `\u27e80\u2194 j \u27e9\n `\u27e8 i \u2194 zero \u27e9 = `\u27e80\u2194 i \u27e9\n `\u27e8 suc i \u2194 suc j \u27e9 = `tl `\u27e8 i \u2194 j \u27e9\n\n `\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) xs \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n `\u27e8_\u2194_\u27e9-spec zero j xs = `\u27e80\u2194 j \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) zero xs = `\u27e80\u21941+ i \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) (suc j) (x \u2237 xs) rewrite `\u27e8 i \u2194 j \u27e9-spec xs = refl\n -}\n\n `xor-head : \u2200 {n} \u2192 Bit \u2192 Bij (1 + n)\n `xor-head b = if b then `not else `id\n\n `xor-head-spec : \u2200 b {n} x (xs : Bits n) \u2192 `xor-head b \u2219 (x \u2237 xs) \u2261 (b xor x) \u2237 xs\n `xor-head-spec true x xs = refl\n `xor-head-spec false x xs = refl\n\n `\u27e8_\u2295\u27e9 : \u2200 {n} \u2192 Bits n \u2192 Bij n\n `\u27e8 [] \u2295\u27e9 = `id\n `\u27e8 b \u2237 xs \u2295\u27e9 = `xor-head b `\u204f `tl `\u27e8 xs \u2295\u27e9\n\n `\u27e8_\u2295\u27e9-spec : \u2200 {n} (xs ys : Bits n) \u2192 `\u27e8 xs \u2295\u27e9 \u2219 ys \u2261 xs \u2295 ys\n `\u27e8 [] \u2295\u27e9-spec [] = refl\n `\u27e8 true \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n `\u27e8 false \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) x \u2192 0\u21941 pad \u2295 0\u21941 x \u2261 0\u21941 (pad \u2295 x)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\nmodule PermutationSyntax-Props where\n open PermutationSyntax\n open PermutationSemantics\n -- open PermutationProperties\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) xs \u2192 0\u21941 pad \u2295 0\u21941 xs \u2261 0\u21941 (pad \u2295 xs)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\n -- TODO make use of \u229b-dist-\u2219\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 fs `id xs = refl\n \u2295-dist-\u2219 fs `0\u21941 xs = \u2295-dist-0\u21941 fs xs\n \u2295-dist-\u2219 (f \u2237 fs) (`tl \u03c0) (x \u2237 xs) rewrite \u2295-dist-\u2219 fs \u03c0 xs = refl\n \u2295-dist-\u2219 fs (\u03c0\u2080 `\u204f \u03c0\u2081) xs rewrite \u2295-dist-\u2219 (\u03c0\u2080 \u2219 fs) \u03c0\u2081 (\u03c0\u2080 \u2219 xs)\n | \u2295-dist-\u2219 fs \u03c0\u2080 xs = refl\n {-\n -- \u229b-dist-\u2219 : \u2200 {n a} {A : Set a} (fs : Vec (A \u2192 A) n) \u03c0 xs \u2192 \u03c0 \u2219 fs \u229b \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (fs \u229b xs)\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 pad \u03c0 xs = \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs\n \u2261\u27e8 refl \u27e9\n vmap _xor_ (\u03c0 \u2219 pad) \u229b \u03c0 \u2219 xs\n \u2261\u27e8 TODO \u27e9\n \u03c0 \u2219 vmap _xor_ pad \u229b \u03c0 \u2219 xs\n \u2261\u27e8 \u229b-dist-\u2219 _ (vmap _xor_ pad) \u03c0 xs \u27e9\n \u03c0 \u2219 (vmap _xor_ pad \u229b xs)\n \u2261\u27e8 refl \u27e9\n \u03c0 \u2219 (pad \u2295 xs)\n \u220e where open \u2261-Reasoning\n -- rans {!\u229b-dist-\u2219 (vmap _xor_ (op \u2219 pad)) op xs!} (\u229b-dist-\u2219 (vmap _xor_ pad) op xs)\n-}\nmodule SimpleSearch {a} {A : Set a} (_\u2219_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u2219_ public\n\n module SearchUnit \u03b5 (\u03b5\u2219\u03b5 : \u03b5 \u2219 \u03b5 \u2261 \u03b5) where\n search-const\u03b5\u2261\u03b5 : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-const\u03b5\u2261\u03b5 zero = refl\n search-const\u03b5\u2261\u03b5 (suc n) rewrite search-const\u03b5\u2261\u03b5 n = \u03b5\u2219\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n module SearchInterchange (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u2219 f\u2081 x) \u2261 search f\u2080 \u2219 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u2219-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e where open \u2261-Reasoning\n\n search-0\u21941 : \u2200 {n} (f : Bits n \u2192 A) \u2192 search {n} (f \u2218 0\u21941) \u2261 search {n} f\n search-0\u21941 {zero} _ = refl\n search-0\u21941 {suc zero} _ = refl\n search-0\u21941 {suc (suc n)} _ = \u2219-interchange _ _ _ _\n\n module Bij (\u2219-comm : Commutative _\u2261_ _\u2219_)\n (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n open SearchInterchange \u2219-interchange using (search-0\u21941)\n open OperationSyntax hiding (_\u2219_)\n search-bij : \u2200 {n} f (g : Bits n \u2192 A) \u2192 search (g \u2218 eval f) \u2261 search g\n search-bij `id _ = refl\n search-bij `0\u21941 f = search-0\u21941 f\n search-bij (f `\u204f g) h\n rewrite search-bij f (h \u2218 eval g)\n | search-bij g h\n = refl\n search-bij {suc n} (`id `\u2237 f) g\n rewrite search-bij (f 0b) (g \u2218 0\u2237_)\n | search-bij (f 1b) (g \u2218 1\u2237_)\n = refl\n search-bij {suc n} (`not\u1d2e `\u2237 f) g\n rewrite search-bij (f 1b) (g \u2218 0\u2237_)\n | search-bij (f 0b) (g \u2218 1\u2237_)\n = \u2219-comm _ _\n\nmodule Sum where\n open SimpleSearch _+_ using (module Comm; module SearchInterchange; module SearchUnit; module Bij)\n open SimpleSearch _+_ public using () renaming (search to sum; search-\u2257 to sum-\u2257; searchBit to sumBit;\n search-\u2257\u2082 to sum-\u2257\u2082;\n search-+ to sum-+)\n open Comm \u2115\u00b0.+-comm public renaming (search-comm to sum-comm)\n open SearchUnit 0 refl public renaming\n (search-const\u03b5\u2261\u03b5 to sum-const0\u22610)\n open SearchInterchange +-interchange public renaming (\n search-dist to sum-dist;\n search-searchBit to sum-sumBit;\n search-search to sum-sum;\n search-swap to sum-swap)\n open Bij \u2115\u00b0.+-comm +-interchange public renaming (search-bij to sum-bij)\n\n sum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\n sum-const zero _ = refl\n sum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nmodule Count where\n open Sum\n open OperationSyntax\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n #\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n -- #-ext\n #-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n #-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n #-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n #-bij : \u2200 {n} f (g : Bits n \u2192 Bit) \u2192 #\u27e8 g \u2218 eval f \u27e9 \u2261 #\u27e8 g \u27e9\n #-bij f g = sum-bij f (Bool.to\u2115 \u2218 g)\n\n #-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n #-\u2295 = #-comm\n\n #-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n #-const n b = sum-const n (Bool.to\u2115 b)\n\n #never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n #never\u22610 = sum-const0\u22610\n\n #always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n #always\u22612^ n = sum-const n 1\n\n #-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 Bit) \u2192 sum (\u03bb x \u2192 Bool.to\u2115 (f\u2080 x) + Bool.to\u2115 (f\u2081 x)) \u2261 #\u27e8 f\u2080 \u27e9 + #\u27e8 f\u2081 \u27e9\n #-dist f\u2080 f\u2081 = sum-dist (Bool.to\u2115 \u2218 f\u2080) (Bool.to\u2115 \u2218 f\u2081)\n\n #-+ : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n #\u27e8 f \u27e9 \u2261 sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9 )\n #-+ {m} {n} f = sum-+ {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-# : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9)\n \u2261 sum {n} (\u03bb ys \u2192 #\u27e8 (\u03bb (xs : Bits m) \u2192 f (xs ++ ys)) \u27e9)\n #-# {m} {n} f = sum-sum {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-swap : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192 #\u27e8 f \u2218 vswap n {m} \u27e9 \u2261 #\u27e8 f \u27e9\n #-swap {m} {n} f = sum-swap {m} {n} (Bool.to\u2115 \u2218 f)\n\n #\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n #\u27e8== [] \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n \u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n \u2257-cong-# f g f\u2257g = sum-\u2257 _ _ (cong Bool.to\u2115 \u2218 f\u2257g)\n\n -- #-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n -- #-+ f f0 f1 rewrite f0 | f1 = refl\n\n #-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-take-drop zero n f g with f []\n ... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n ... | false = #never\u22610 n\n #-take-drop (suc m) n f g\n rewrite \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x))\n | #-take-drop m n (f \u2218 0\u2237_) g\n | \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x))\n | #-take-drop m n (f \u2218 1\u2237_) g\n = sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9)\n\n #-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n #\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n #\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n #\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n ... | true | true | _ = s\u2264s z\u2264n\n ... | true | false | p = \u22a5-elim (p _)\n ... | false | _ | _ = z\u2264n\n #\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n #-\u2227-\u2228\u1d47 : \u2200 x y \u2192 Bool.to\u2115 (x \u2227 y) + Bool.to\u2115 (x \u2228 y) \u2261 Bool.to\u2115 x + Bool.to\u2115 y\n #-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (Bool.to\u2115 y) 1 = refl\n #-\u2227-\u2228\u1d47 false y = refl\n\n #-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n #-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #\u2228' {zero} f g with f []\n ... | true = s\u2264s z\u2264n\n ... | false = \u2115\u2264.refl\n #\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n #\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n #\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n #\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n #-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n #-bound zero f = Bool.to\u2115\u22641 (f [])\n #-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n #-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n #-\u2218vnot _ f = #-\u2295 1\u207f f\n\n #-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n #-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n #-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n #-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n #-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n #-not\u2218 zero f with f []\n ... | true = \u2261.refl\n ... | false = \u2261.refl\n #-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n #-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n #-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\nopen SimpleSearch public\nopen Sum public\nopen Count public\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192\n search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = search-\u2257 op _ _ (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2^ n) (toFin xs)\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2^ n + to\u2115 xs\n\nto\u2115-bound : \u2200 {n} (xs : Bits n) \u2192 to\u2115 xs < 2^ n \nto\u2115-bound [] = s\u2264s z\u2264n\nto\u2115-bound {suc n} (1b \u2237 xs) rewrite +-assoc-comm 1 (2^ n) (to\u2115 xs) = \u2115\u2264.refl {2^ n} +-mono to\u2115-bound xs\nto\u2115-bound {suc n} (0b \u2237 xs) = \u2264-steps (2^ n) (to\u2115-bound xs)\n\nto\u2115\u22642\u207f+ : \u2200 {n} (x : Bits n) {y} \u2192 to\u2115 {n} x \u2264 2^ n + y\nto\u2115\u22642\u207f+ {n} x {y} = \u2115\u2264.trans (\u2264-steps y (\u2264-pred (\u2264-steps 1 (to\u2115-bound x))))\n (\u2115\u2264.reflexive (\u2115\u00b0.+-comm y (2^ n)))\n\n2\u207f+\u2270to\u2115 : \u2200 {n x} (y : Bits n) \u2192 2^ n + x \u2270 to\u2115 {n} y\n2\u207f+\u2270to\u2115 {n} {x} y p = \u00acn+\u2264y-perm : \u2200 {a} {A : Set a} {n} {left\u2080 right\u2080 left\u2081 right\u2081 : Tree A n} \u2192\n Perm left\u2080 left\u2081 \u2192\n Perm right\u2080 right\u2081 \u2192\n Perm (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n< f \u00d7 g >-perm = first f \u204f second-perm g\n\nswp\u2082-perm : \u2200 {a n} {A : Set a} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Perm (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2081\u2081 t\u2080\u2081) (fork t\u2081\u2080 t\u2080\u2080))\nswp\u2082-perm = first swp \u204f swp-seconds \u204f first swp\n\nswp\u2083-perm : \u2200 {a n} {A : Set a} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Perm (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2080\u2080 t\u2081\u2080) (fork t\u2080\u2081 t\u2081\u2081))\nswp\u2083-perm = second-perm swp \u204f swp-seconds \u204f second-perm swp\n\nswp-firsts-perm : \u2200 {n a} {A : Set a} {tA tB tC tD : Tree A n} \u2192\n Perm (fork (fork tA tB) (fork tC tD))\n (fork (fork tC tB) (fork tA tD))\nswp-firsts-perm = < swp \u00d7 swp >-perm \u204f swp-seconds \u204f < swp \u00d7 swp >-perm\n\nSwp\u21d2Perm : \u2200 {n a} {A : Set a} \u2192 Swp {a} {A} {n} \u21d2 Perm {n = n}\nSwp\u21d2Perm (left pf) = first (Swp\u21d2Perm pf)\nSwp\u21d2Perm (right pf) = second-perm (Swp\u21d2Perm pf)\nSwp\u21d2Perm swp\u2081 = swp\nSwp\u21d2Perm swp\u2082 = swp\u2082-perm\n\nSwp\u2605\u21d2Perm : \u2200 {n a} {A : Set a} \u2192 Swp\u2605 {n} {a} {A} \u21d2 Perm {n = n}\nSwp\u2605\u21d2Perm \u03b5 = \u03b5\nSwp\u2605\u21d2Perm (x \u25c5 xs) = Swp\u21d2Perm x \u204f Swp\u2605\u21d2Perm xs\n\nswp-inners : \u2200 {n} \u2192 SwpOp (2 + n)\nswp-inners = second-swpop swp \u204f swp-seconds \u204f second-swpop swp\n\non-extremes : \u2200 {n} \u2192 SwpOp (1 + n) \u2192 SwpOp (2 + n)\non-extremes f = swp-seconds \u204f first f \u204f swp-seconds\n\non-firsts : \u2200 {n} \u2192 SwpOp (1 + n) \u2192 SwpOp (2 + n)\non-firsts f = swp-inners \u204f first f \u204f swp-inners\n\n0\u2194_ : \u2200 {m n} \u2192 Bits m \u2192 SwpOp (m + n)\n0\u2194 [] = \u03b5\n0\u2194 (false{-0-} \u2237 p) = first (0\u2194 p)\n0\u2194 (true{-1-} \u2237 []) = swp\n0\u2194 (true{-1-} \u2237 true {-1-} \u2237 p) = on-extremes (0\u2194 (1b \u2237 p))\n0\u2194 (true{-1-} \u2237 false{-0-} \u2237 p) = on-firsts (0\u2194 (1b \u2237 p))\n\n0\u2194\u2032_ : \u2200 {n} \u2192 Bits n \u2192 SwpOp n\n0\u2194\u2032_ {n} rewrite cong SwpOp (sym (\u2115\u00b0.+-comm n 0)) = 0\u2194_ {n} {0}\n\n[_\u2194_] : \u2200 {m n} (p q : Bits m) \u2192 SwpOp (m + n)\n[ p \u2194 q ] = 0\u2194 p \u204f 0\u2194 q\n\n[_\u2194\u2032_] : \u2200 {n} (p q : Bits n) \u2192 SwpOp n\n[ p \u2194\u2032 q ] = 0\u2194\u2032 p \u204f 0\u2194\u2032 q\n\n_$swp_ : \u2200 {n a} {A : Set a} \u2192 SwpOp n \u2192 Tree A n \u2192 Tree A n\n\u03b5 $swp t = t\n(f \u204f g) $swp t = g $swp (f $swp t)\n(first f) $swp (fork t\u2080 t\u2081) = fork (f $swp t\u2080) t\u2081\nswp $swp (fork t\u2080 t\u2081) = fork t\u2081 t\u2080\nswp-seconds $swp (fork (fork t\u2080 t\u2081) (fork t\u2082 t\u2083)) = fork (fork t\u2080 t\u2083) (fork t\u2082 t\u2081)\n\nswpRel : \u2200 {n a} {A : Set a} (f : SwpOp n) (t : Tree A n) \u2192 Perm t (f $swp t)\nswpRel \u03b5 _ = \u03b5\nswpRel (f \u204f g) _ = swpRel f _ \u204f swpRel g _\nswpRel (first f) (fork _ _) = first (swpRel f _)\nswpRel swp (fork _ _) = swp\nswpRel swp-seconds\n (fork (fork _ _) (fork _ _)) = swp-seconds\n\n[0\u2194_]-Rel : \u2200 {m n a} {A : Set a} (p : Bits m) (t : Tree A (m + n)) \u2192 Perm t ((0\u2194 p) $swp t)\n[0\u2194 p ]-Rel = swpRel (0\u2194 p)\n\nswpOp' : \u2200 {n a} {A : Set a} {t u : Tree A n} \u2192 Perm0\u2194 t u \u2192 SwpOp n\nswpOp' \u03b5 = \u03b5\nswpOp' (first f) = first (swpOp' f)\nswpOp' swp = swp\nswpOp' (firsts f) = on-firsts (swpOp' f)\nswpOp' (extremes f) = on-extremes (swpOp' f)\n\nswpOp : \u2200 {n a} {A : Set a} {t u : Tree A n} \u2192 Perm t u \u2192 SwpOp n\nswpOp \u03b5 = \u03b5\nswpOp (f \u204f g) = swpOp f \u204f swpOp g\nswpOp (first f) = first (swpOp f)\nswpOp swp = swp\nswpOp swp-seconds = swp-seconds\n\nswpOp-sym : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp n\nswpOp-sym \u03b5 = \u03b5\nswpOp-sym (f \u204f g) = swpOp-sym g \u204f swpOp-sym f\nswpOp-sym (first f) = first (swpOp-sym f)\nswpOp-sym swp = swp\nswpOp-sym swp-seconds = swp-seconds\n\nswpOp-sym-involutive : \u2200 {n} (f : SwpOp n) \u2192 swpOp-sym (swpOp-sym f) \u2261 f\nswpOp-sym-involutive \u03b5 = refl\nswpOp-sym-involutive (f \u204f g) rewrite swpOp-sym-involutive f | swpOp-sym-involutive g = refl\nswpOp-sym-involutive (first f) rewrite swpOp-sym-involutive f = refl\nswpOp-sym-involutive swp = refl\nswpOp-sym-involutive swp-seconds = refl\n\nswpOp-sym-sound : \u2200 {n a} {A : Set a} (f : SwpOp n) (t : Tree A n) \u2192 swpOp-sym f $swp (f $swp t) \u2261 t\nswpOp-sym-sound \u03b5 t = refl\nswpOp-sym-sound (f \u204f g) t rewrite swpOp-sym-sound g (f $swp t) | swpOp-sym-sound f t = refl\nswpOp-sym-sound (first f) (fork t _) rewrite swpOp-sym-sound f t = refl\nswpOp-sym-sound swp (fork _ _) = refl\nswpOp-sym-sound swp-seconds (fork (fork _ _) (fork _ _)) = refl\n\nmodule \u00acswp-comm where\n data X : Set where\n A B C D E F G H : X\n n : \u2115\n n = 3\n t : Tree X n\n t = fork (fork (fork (leaf A) (leaf B))(fork (leaf C) (leaf D))) (fork (fork (leaf E) (leaf F))(fork (leaf G) (leaf H)))\n f : SwpOp n\n f = swp\n g : SwpOp n\n g = first swp\n pf : f $swp (g $swp t) \u2262 g $swp (f $swp t)\n pf ()\n\nswp-leaf : \u2200 {a} {A : Set a} (f : SwpOp 0) (x : A) \u2192 f $swp (leaf x) \u2261 leaf x\nswp-leaf \u03b5 x = refl\nswp-leaf (f \u204f g) x rewrite swp-leaf f x | swp-leaf g x = refl\n\nswpOp-sound : \u2200 {n a} {A : Set a} {t u : Tree A n} (perm : Perm t u) \u2192 (swpOp perm $swp t \u2261 u)\nswpOp-sound \u03b5 = refl\nswpOp-sound (f \u204f f\u2081) rewrite swpOp-sound f | swpOp-sound f\u2081 = refl\nswpOp-sound (first f) rewrite swpOp-sound f = refl\nswpOp-sound swp = refl\nswpOp-sound swp-seconds = refl\n\nopen import Relation.Nullary using (Dec ; yes ; no)\nopen import Relation.Nullary.Negation\n\nmodule new-approach where\n\n open import Data.Empty\n\n import Function.Inverse as FI\n open FI using (_\u2194_; module Inverse; _InverseOf_)\n open import Function.Related\n import Function.Equality\n import Relation.Binary.PropositionalEquality as P\n\n data _\u2208_ {a}{A : Set a}(x : A) : {n : \u2115} \u2192 Tree A n \u2192 Set a where\n here : x \u2208 leaf x\n left : {n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 t\u2081 \u2192 x \u2208 fork t\u2081 t\u2082\n right : {n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 t\u2082 \u2192 x \u2208 fork t\u2081 t\u2082\n\n toBits : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t : Tree A n} \u2192 x \u2208 t \u2192 Bits n\n toBits here = []\n toBits (left key) = 0b \u2237 toBits key\n toBits (right key) = 1b \u2237 toBits key\n\n \u2208-lookup : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t : Tree A n}(path : x \u2208 t) \u2192 lookup (toBits path) t \u2261 x\n \u2208-lookup here = refl\n \u2208-lookup (left path) = \u2208-lookup path\n \u2208-lookup (right path) = \u2208-lookup path\n\n lookup-\u2208 : \u2200 {a}{A : Set a}{n : \u2115}(key : Bits n)(t : Tree A n) \u2192 lookup key t \u2208 t\n lookup-\u2208 [] (leaf x) = here\n lookup-\u2208 (true \u2237 key) (fork tree tree\u2081) = right (lookup-\u2208 key tree\u2081)\n lookup-\u2208 (false \u2237 key) (fork tree tree\u2081) = left (lookup-\u2208 key tree)\n\n _\u2248_ : \u2200 {a}{A : Set a}{n : \u2115} \u2192 Tree A n \u2192 Tree A n \u2192 Set _\n t\u2081 \u2248 t\u2082 = \u2200 x \u2192 (x \u2208 t\u2081) \u2194 (x \u2208 t\u2082) \n\n \u2248-refl : {a : _}{A : Set a}{n : \u2115}{t : Tree A n} \u2192 t \u2248 t\n \u2248-refl _ = FI.id\n\n move : \u2200 {a}{A : Set a}{n : \u2115}{t s : Tree A n}{x : A} \u2192 t \u2248 s \u2192 x \u2208 t \u2192 x \u2208 s\n move t\u2248s x\u2208t = Inverse.to (t\u2248s _) Function.Equality.\u27e8$\u27e9 x\u2208t\n\n swap\u2080 : \u2200 {a}{A : Set a}{n : \u2115}(t\u2081 t\u2082 : Tree A n) \u2192 fork t\u2081 t\u2082 \u2248 fork t\u2082 t\u2081\n swap\u2080 t\u2081 t\u2082 = \u03bb x \u2192 record \n { to = \u2192-to-\u27f6 swap\n ; from = \u2192-to-\u27f6 swap\n ; inverse-of = record { left-inverse-of = swap-inv \n ; right-inverse-of = swap-inv } \n } where\n swap : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 fork t\u2081 t\u2082 \u2192 x \u2208 fork t\u2082 t\u2081\n swap (left path) = right path\n swap (right path) = left path\n\n swap-inv : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t\u2081 t\u2082 : Tree A n}(p : x \u2208 fork t\u2081 t\u2082) \u2192 swap (swap p) \u2261 p\n swap-inv (left p) = refl\n swap-inv (right p) = refl\n\n _\u27e8fork\u27e9_ : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 s\u2081 s\u2082 : Tree A n} \u2192 t\u2081 \u2248 s\u2081 \u2192 t\u2082 \u2248 s\u2082 \u2192 fork t\u2081 t\u2082 \u2248 fork s\u2081 s\u2082\n (t1\u2248s1 \u27e8fork\u27e9 t2\u2248s2) y = record \n { to = to\n ; from = from\n ; inverse-of = record { left-inverse-of = frk-linv\n ; right-inverse-of = frk-rinv } \n } where\n \n frk : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 s\u2081 s\u2082 : Tree A n}{x : A} \u2192 t\u2081 \u2248 s\u2081 \u2192 t\u2082 \u2248 s\u2082 \u2192 x \u2208 fork t\u2081 t\u2082 \u2192 x \u2208 fork s\u2081 s\u2082\n frk t1\u2248s1 t2\u2248s2 (left x\u2208t1) = left (move t1\u2248s1 x\u2208t1)\n frk t1\u2248s1 t2\u2248s2 (right x\u2208t2) = right (move t2\u2248s2 x\u2208t2)\n \n to = \u2192-to-\u27f6 (frk t1\u2248s1 t2\u2248s2)\n from = \u2192-to-\u27f6 (frk (\u03bb x \u2192 FI.sym (t1\u2248s1 x)) (\u03bb x \u2192 FI.sym (t2\u2248s2 x)))\n\n \n open Function.Equality using (_\u27e8$\u27e9_)\n open import Function.LeftInverse\n\n frk-linv : from LeftInverseOf to\n frk-linv (left x) = cong left (_InverseOf_.left-inverse-of (Inverse.inverse-of (t1\u2248s1 y)) x)\n frk-linv (right x) = cong right (_InverseOf_.left-inverse-of (Inverse.inverse-of (t2\u2248s2 y)) x)\n\n frk-rinv : from RightInverseOf to -- \u2200 x \u2192 to \u27e8$\u27e9 (from \u27e8$\u27e9 x) \u2261 x\n frk-rinv (left x) = cong left (_InverseOf_.right-inverse-of (Inverse.inverse-of (t1\u2248s1 y)) x)\n frk-rinv (right x) = cong right (_InverseOf_.right-inverse-of (Inverse.inverse-of (t2\u2248s2 y)) x)\n\n Rot\u27f6\u2248 : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 Rot t\u2081 t\u2082 \u2192 t\u2081 \u2248 t\u2082\n Rot\u27f6\u2248 (leaf x) y = FI.id\n Rot\u27f6\u2248 (fork rot rot\u2081) y = (Rot\u27f6\u2248 rot \u27e8fork\u27e9 Rot\u27f6\u2248 rot\u2081) y\n Rot\u27f6\u2248 (krof {_} {l} {l'} {r} {r'} rot rot\u2081) y = \n y \u2208 fork l r \u2194\u27e8 (Rot\u27f6\u2248 rot \u27e8fork\u27e9 Rot\u27f6\u2248 rot\u2081) y \u27e9\n y \u2208 fork r' l' \u2194\u27e8 swap\u2080 r' l' y \u27e9 \n y \u2208 fork l' r' \u220e\n where open EquationalReasoning\n\n put : {a : _}{A : Set a}{n : \u2115} \u2192 Bits n \u2192 A \u2192 Tree A n \u2192 Tree A n\n put [] val tree = leaf val\n put (x \u2237 key) val (fork tree tree\u2081) = if x then fork tree (put key val tree\u2081) \n else fork (put key val tree) tree\u2081\n\n -- move-me\n _\u2237\u2262_ : {n : \u2115}{xs ys : Bits n}(x : Bit) \u2192 x \u2237 xs \u2262 x \u2237 ys \u2192 xs \u2262 ys\n _\u2237\u2262_ x = contraposition $ cong $ _\u2237_ x\n\n \u2208-put : {a : _}{A : Set a}{n : \u2115}(p : Bits n){x : A}(t : Tree A n) \u2192 x \u2208 put p x t\n \u2208-put [] t = here\n \u2208-put (true \u2237 p) (fork t t\u2081) = right (\u2208-put p t\u2081)\n \u2208-put (false \u2237 p) (fork t t\u2081) = left (\u2208-put p t)\n\n \u2208-put-\u2262 : {a : _}{A : Set a}{n : \u2115}(p : Bits n){x y : A}{t : Tree A n}(path : x \u2208 t)\n \u2192 p \u2262 toBits path \u2192 x \u2208 put p y t\n \u2208-put-\u2262 [] here neg = \u22a5-elim (neg refl)\n \u2208-put-\u2262 (true \u2237 p) (left path) neg = left path\n \u2208-put-\u2262 (false \u2237 p) (left path) neg = left (\u2208-put-\u2262 p path (false \u2237\u2262 neg))\n \u2208-put-\u2262 (true \u2237 p) (right path) neg = right (\u2208-put-\u2262 p path (true \u2237\u2262 neg))\n \u2208-put-\u2262 (false \u2237 p) (right path) neg = right path\n\n swap : {a : _}{A : Set a}{n : \u2115} \u2192 (p\u2081 p\u2082 : Bits n) \u2192 Tree A n \u2192 Tree A n\n swap p\u2081 p\u2082 t = put p\u2081 a\u2082 (put p\u2082 a\u2081 t)\n where\n a\u2081 = lookup p\u2081 t\n a\u2082 = lookup p\u2082 t\n\n swap-perm\u2081 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p : x \u2208 t) \u2192 t \u2248 swap (toBits p) (toBits p) t\n swap-perm\u2081 here = \u2248-refl \n swap-perm\u2081 (left path) = swap-perm\u2081 path \u27e8fork\u27e9 \u2248-refl\n swap-perm\u2081 (right path) = \u2248-refl \u27e8fork\u27e9 swap-perm\u2081 path\n\n swap-comm : {a : _}{A : Set a}{n : \u2115} (p\u2081 p\u2082 : Bits n)(t : Tree A n) \u2192 swap p\u2082 p\u2081 t \u2261 swap p\u2081 p\u2082 t\n swap-comm [] [] (leaf x) = refl\n swap-comm (true \u2237 p\u2081) (true \u2237 p\u2082) (fork t t\u2081) = cong (fork t) (swap-comm p\u2081 p\u2082 t\u2081)\n swap-comm (true \u2237 p\u2081) (false \u2237 p\u2082) (fork t t\u2081) = refl\n swap-comm (false \u2237 p\u2081) (true \u2237 p\u2082) (fork t t\u2081) = refl\n swap-comm (false \u2237 p\u2081) (false \u2237 p\u2082) (fork t t\u2081) = cong (flip fork t\u2081) (swap-comm p\u2081 p\u2082 t)\n\n swap-perm\u2082 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p' : Bits n)(p : x \u2208 t) \n \u2192 x \u2208 swap (toBits p) p' t\n swap-perm\u2082 _ here = here\n swap-perm\u2082 (true \u2237 p) (left path) rewrite \u2208-lookup path = right (\u2208-put p _)\n swap-perm\u2082 (false \u2237 p) (left path) = left (swap-perm\u2082 p path)\n swap-perm\u2082 (true \u2237 p) (right path) = right (swap-perm\u2082 p path)\n swap-perm\u2082 (false \u2237 p) (right path) rewrite \u2208-lookup path = left (\u2208-put p _)\n\n swap-perm\u2083 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p\u2081 p\u2082 : Bits n)(p : x \u2208 t)\n \u2192 p\u2081 \u2262 toBits p \u2192 p\u2082 \u2262 toBits p \u2192 x \u2208 swap p\u2081 p\u2082 t\n swap-perm\u2083 [] [] here neg\u2081 neg\u2082 = here\n swap-perm\u2083 (true \u2237 p\u2081) (true \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left path\n swap-perm\u2083 (true \u2237 p\u2081) (false \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left (\u2208-put-\u2262 _ path (false \u2237\u2262 neg\u2082))\n swap-perm\u2083 (false \u2237 p\u2081) (true \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left (\u2208-put-\u2262 _ path (false \u2237\u2262 neg\u2081))\n swap-perm\u2083 (false \u2237 p\u2081) (false \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left \n (swap-perm\u2083 p\u2081 p\u2082 path (false \u2237\u2262 neg\u2081) (false \u2237\u2262 neg\u2082))\n swap-perm\u2083 (true \u2237 p\u2081) (true \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right\n (swap-perm\u2083 p\u2081 p\u2082 path (true \u2237\u2262 neg\u2081) (true \u2237\u2262 neg\u2082))\n swap-perm\u2083 (true \u2237 p\u2081) (false \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right (\u2208-put-\u2262 _ path (true \u2237\u2262 neg\u2081))\n swap-perm\u2083 (false \u2237 p\u2081) (true \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right (\u2208-put-\u2262 _ path (true \u2237\u2262 neg\u2082))\n swap-perm\u2083 (false \u2237 p\u2081) (false \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right path\n\n \u2208-swp : \u2200 {n a} {A : Set a} (f : SwpOp n) {x : A} {t : Tree A n} \u2192 x \u2208 t \u2192 x \u2208 (f $swp t)\n \u2208-swp \u03b5 pf = pf\n \u2208-swp (f \u204f g) pf = \u2208-swp g (\u2208-swp f pf)\n \u2208-swp (first f) {t = fork _ _} (left pf) = left (\u2208-swp f pf)\n \u2208-swp (first f) {t = fork _ _} (right pf) = right pf\n \u2208-swp swp {t = fork t u} (left pf) = right pf\n \u2208-swp swp {t = fork t u} (right pf) = left pf\n \u2208-swp swp-seconds {t = fork (fork _ _) (fork _ _)} (left (left pf)) = left (left pf)\n \u2208-swp swp-seconds {t = fork (fork _ _) (fork _ _)} (left (right pf)) = right (right pf)\n \u2208-swp swp-seconds {t = fork (fork _ _) (fork _ _)} (right (left pf)) = right (left pf)\n \u2208-swp swp-seconds {t = fork (fork _ _) (fork _ _)} (right (right pf)) = left (right pf)\n\nmodule FoldProp {a} {A : Set a} (_\u00b7_ : Op\u2082 A) (op-comm : Commutative _\u2261_ _\u00b7_) (op-assoc : Associative _\u2261_ _\u00b7_) where\n\n \u27ea_\u27eb : \u2200 {n} \u2192 Tree A n \u2192 A\n \u27ea_\u27eb = fold _\u00b7_\n\n _=[fold]\u21d2\u2032_ : \u2200 {\u2113\u2081 \u2113\u2082} \u2192 (\u2200 {m n} \u2192 REL (Tree A m) (Tree A n) \u2113\u2081) \u2192 Rel A \u2113\u2082 \u2192 Set _\n -- _\u223c\u2080_ =[fold]\u21d2 _\u223c\u2081_ = \u2200 {m n} \u2192 _\u223c\u2080_ {m} {n} =[ fold {n} _\u00b7_ ]\u21d2 _\u223c\u2081_\n _\u223c\u2080_ =[fold]\u21d2\u2032 _\u223c\u2081_ = \u2200 {m n} {t : Tree A m} {u : Tree A n} \u2192 t \u223c\u2080 u \u2192 \u27ea t \u27eb \u223c\u2081 \u27ea u \u27eb\n\n _=[fold]\u21d2_ : \u2200 {\u2113\u2081 \u2113\u2082} \u2192 (\u2200 {n} \u2192 Rel (Tree A n) \u2113\u2081) \u2192 Rel A \u2113\u2082 \u2192 Set _\n _\u223c\u2080_ =[fold]\u21d2 _\u223c\u2081_ = \u2200 {n} \u2192 _\u223c\u2080_ =[ fold {n} _\u00b7_ ]\u21d2 _\u223c\u2081_\n\n fold-rot : Rot =[fold]\u21d2 _\u2261_\n fold-rot (leaf x) = refl\n fold-rot (fork rot rot\u2081) = cong\u2082 _\u00b7_ (fold-rot rot) (fold-rot rot\u2081)\n fold-rot (krof rot rot\u2081) rewrite fold-rot rot | fold-rot rot\u2081 = op-comm _ _\n\n -- t \u223c u \u2192 fork v t \u223c fork u w\n\n lem : \u2200 x y z t \u2192 (x \u00b7 y) \u00b7 (z \u00b7 t) \u2261 (t \u00b7 y) \u00b7 (z \u00b7 x)\n lem x y z t = (x \u00b7 y) \u00b7 (z \u00b7 t)\n \u2261\u27e8 op-assoc x y _ \u27e9\n x \u00b7 (y \u00b7 (z \u00b7 t))\n \u2261\u27e8 op-comm x _ \u27e9\n (y \u00b7 (z \u00b7 t)) \u00b7 x\n \u2261\u27e8 op-assoc y (z \u00b7 t) _ \u27e9\n y \u00b7 ((z \u00b7 t) \u00b7 x)\n \u2261\u27e8 cong (\u03bb u \u2192 y \u00b7 (u \u00b7 x)) (op-comm z t) \u27e9\n y \u00b7 ((t \u00b7 z) \u00b7 x)\n \u2261\u27e8 cong (_\u00b7_ y) (op-assoc t z x) \u27e9\n y \u00b7 (t \u00b7 (z \u00b7 x))\n \u2261\u27e8 sym (op-assoc y t _) \u27e9\n (y \u00b7 t) \u00b7 (z \u00b7 x)\n \u2261\u27e8 cong (\u03bb u \u2192 u \u00b7 (z \u00b7 x)) (op-comm y t) \u27e9\n (t \u00b7 y) \u00b7 (z \u00b7 x)\n \u220e\n where open \u2261-Reasoning\n\n fold-swp : Swp =[fold]\u21d2 _\u2261_\n fold-swp (left pf) rewrite fold-swp pf = refl\n fold-swp (right pf) rewrite fold-swp pf = refl\n fold-swp swp\u2081 = op-comm _ _\n fold-swp (swp\u2082 {_} {t\u2080\u2080} {t\u2080\u2081} {t\u2081\u2080} {t\u2081\u2081}) = lem \u27ea t\u2080\u2080 \u27eb \u27ea t\u2080\u2081 \u27eb \u27ea t\u2081\u2080 \u27eb \u27ea t\u2081\u2081 \u27eb\n\n fold-swp\u2605 : Swp\u2605 =[fold]\u21d2 _\u2261_\n fold-swp\u2605 \u03b5 = refl\n fold-swp\u2605 (x \u25c5 xs) rewrite fold-swp x | fold-swp\u2605 xs = refl\n","old_contents":"module prefect-bintree where\n\nopen import Function\nopen import Data.Nat.NP using (\u2115; zero; suc; 2^_; _+_; module \u2115\u00b0)\nopen import Data.Bool\nopen import Data.Bits\nopen import Data.Vec using (Vec; _++_)\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Algebra.FunctionProperties\n\ndata Tree {a} (A : Set a) : \u2115 \u2192 Set a where\n leaf : (x : A) \u2192 Tree A zero\n fork : \u2200 {n} (left right : Tree A n) \u2192 Tree A (suc n)\n\nfromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Tree A n\nfromFun {zero} f = leaf (f [])\nfromFun {suc n} f = fork (fromFun (f \u2218 0\u2237_)) (fromFun (f \u2218 1\u2237_))\n\ntoFun : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Bits n \u2192 A\ntoFun (leaf x) _ = x\ntoFun (fork left right) (b \u2237 bs) = toFun (if b then right else left) bs\n\nlookup : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Tree A n \u2192 A\nlookup = flip toFun\n\nfold : \u2200 {n a} {A : Set a} (op : A \u2192 A \u2192 A) \u2192 Tree A n \u2192 A\nfold _ (leaf x) = x\nfold _\u00b7_ (fork t\u2080 t\u2081) = fold _\u00b7_ t\u2080 \u00b7 fold _\u00b7_ t\u2081\n\nsearch\u2261fold\u2218fromFun : \u2200 {n a} {A : Set a} op (f : Bits n \u2192 A) \u2192 search op f \u2261 fold op (fromFun f)\nsearch\u2261fold\u2218fromFun {zero} op f = refl\nsearch\u2261fold\u2218fromFun {suc n} op f\n rewrite search\u2261fold\u2218fromFun op (f \u2218 0\u2237_)\n | search\u2261fold\u2218fromFun op (f \u2218 1\u2237_) = refl\n\n-- Returns the flat vector of leaves underlying the perfect binary tree.\ntoVec : \u2200 {n a} {A : Set a} \u2192 Tree A n \u2192 Vec A (2^ n)\ntoVec (leaf x) = x \u2237 []\ntoVec (fork t\u2080 t\u2081) = toVec t\u2080 ++ toVec t\u2081\n\nlookup' : \u2200 {m n a} {A : Set a} \u2192 Bits m \u2192 Tree A (m + n) \u2192 Tree A n\nlookup' [] t = t\nlookup' (b \u2237 bs) (fork t t\u2081) = lookup' bs (if b then t\u2081 else t)\n\n\nupdate' : \u2200 {m n a} {A : Set a} \u2192 Bits m \u2192 Tree A n \u2192 Tree A (m + n) \u2192 Tree A (m + n)\nupdate' [] val tree = val\nupdate' (b \u2237 key) val (fork tree tree\u2081) = if b then fork tree (update' key val tree\u2081) \n else fork (update' key val tree) tree\u2081\n\nmap : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 Tree A n \u2192 Tree B n\nmap f (leaf x) = leaf (f x)\nmap f (fork t\u2080 t\u2081) = fork (map f t\u2080) (map f t\u2081)\n\nopen import Relation.Binary\nopen import Data.Star using (Star; \u03b5; _\u25c5_)\n\ndata Swp {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n left : \u2200 {n} {left\u2080 left\u2081 right : Tree A n} \u2192\n Swp left\u2080 left\u2081 \u2192\n Swp (fork left\u2080 right) (fork left\u2081 right)\n right : \u2200 {n} {left right\u2080 right\u2081 : Tree A n} \u2192\n Swp right\u2080 right\u2081 \u2192\n Swp (fork left right\u2080) (fork left right\u2081)\n swp\u2081 : \u2200 {n} {left right : Tree A n} \u2192\n Swp (fork left right) (fork right left)\n swp\u2082 : \u2200 {n} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Swp (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2081\u2081 t\u2080\u2081) (fork t\u2081\u2080 t\u2080\u2080))\n\nSwp\u2605 : \u2200 {n a} {A : Set a} (left right : Tree A n) \u2192 Set a\nSwp\u2605 = Star Swp\n\nSwp-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Swp {A = A} {n})\nSwp-sym (left s) = left (Swp-sym s)\nSwp-sym (right s) = right (Swp-sym s)\nSwp-sym swp\u2081 = swp\u2081\nSwp-sym swp\u2082 = swp\u2082\n\ndata Rot {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n leaf : \u2200 x \u2192 Rot (leaf x) (leaf x)\n fork : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 left\u2081 \u2192\n Rot right\u2080 right\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n krof : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 right\u2081 \u2192\n Rot right\u2080 left\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n\nRot-refl : \u2200 {n a} {A : Set a} \u2192 Reflexive (Rot {A = A} {n})\nRot-refl {x = leaf x} = leaf x\nRot-refl {x = fork _ _} = fork Rot-refl Rot-refl\n\nRot-sym : \u2200 {n a} {A : Set a} \u2192 Symmetric (Rot {A = A} {n})\nRot-sym (leaf x) = leaf x\nRot-sym (fork p\u2080 p\u2081) = fork (Rot-sym p\u2080) (Rot-sym p\u2081)\nRot-sym (krof p\u2080 p\u2081) = krof (Rot-sym p\u2081) (Rot-sym p\u2080)\n\nRot-trans : \u2200 {n a} {A : Set a} \u2192 Transitive (Rot {A = A} {n})\nRot-trans (leaf x) q = q\nRot-trans (fork p\u2080 p\u2081) (fork q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (fork p\u2080 p\u2081) (krof q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (krof p\u2080 p\u2081) (fork q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\nRot-trans (krof p\u2080 p\u2081) (krof q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\n\ndata SwpOp : \u2115 \u2192 Set where\n \u03b5 : \u2200 {n} \u2192 SwpOp n\n\n _\u204f_ : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp n \u2192 SwpOp n\n\n first : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp (suc n)\n\n swp : \u2200 {n} \u2192 SwpOp (suc n)\n\n swp-seconds : \u2200 {n} \u2192 SwpOp (2 + n)\n\ndata Perm {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n \u03b5 : \u2200 {n} {t : Tree A n} \u2192 Perm t t\n\n _\u204f_ : \u2200 {n} {t u v : Tree A n} \u2192 Perm t u \u2192 Perm u v \u2192 Perm t v\n\n first : \u2200 {n} {tA tB tC : Tree A n} \u2192\n Perm tA tB \u2192\n Perm (fork tA tC) (fork tB tC)\n\n swp : \u2200 {n} {tA tB : Tree A n} \u2192\n Perm (fork tA tB) (fork tB tA)\n\n swp-seconds : \u2200 {n} {tA tB tC tD : Tree A n} \u2192\n Perm (fork (fork tA tB) (fork tC tD))\n (fork (fork tA tD) (fork tC tB))\n\ndata Perm0\u2194 {a} {A : Set a} : \u2200 {n} (left right : Tree A n) \u2192 Set a where\n \u03b5 : \u2200 {n} {t : Tree A n} \u2192 Perm0\u2194 t t\n\n swp : \u2200 {n} {tA tB : Tree A n} \u2192\n Perm0\u2194 (fork tA tB) (fork tB tA)\n\n first : \u2200 {n} {tA tB tC : Tree A n} \u2192\n Perm0\u2194 tA tB \u2192\n Perm0\u2194 (fork tA tC) (fork tB tC)\n\n firsts : \u2200 {n} {tA tB tC tD tE tF : Tree A n} \u2192\n Perm0\u2194 (fork tA tC) (fork tE tF) \u2192\n Perm0\u2194 (fork (fork tA tB) (fork tC tD))\n (fork (fork tE tB) (fork tF tD))\n\n extremes : \u2200 {n} {tA tB tC tD tE tF : Tree A n} \u2192\n Perm0\u2194 (fork tA tD) (fork tE tF) \u2192\n Perm0\u2194 (fork (fork tA tB) (fork tC tD))\n (fork (fork tA tB) (fork tC tF))\n\n-- Star Perm0\u2194 can then model any permutation\n\ninfixr 1 _\u204f_\n\nsecond-perm : \u2200 {a} {A : Set a} {n} {left right\u2080 right\u2081 : Tree A n} \u2192\n Perm right\u2080 right\u2081 \u2192\n Perm (fork left right\u2080) (fork left right\u2081)\nsecond-perm f = swp \u204f first f \u204f swp\n\nsecond-swpop : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp (suc n)\nsecond-swpop f = swp \u204f first f \u204f swp\n\n<_\u00d7_>-perm : \u2200 {a} {A : Set a} {n} {left\u2080 right\u2080 left\u2081 right\u2081 : Tree A n} \u2192\n Perm left\u2080 left\u2081 \u2192\n Perm right\u2080 right\u2081 \u2192\n Perm (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n< f \u00d7 g >-perm = first f \u204f second-perm g\n\nswp\u2082-perm : \u2200 {a n} {A : Set a} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Perm (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2081\u2081 t\u2080\u2081) (fork t\u2081\u2080 t\u2080\u2080))\nswp\u2082-perm = first swp \u204f swp-seconds \u204f first swp\n\nswp\u2083-perm : \u2200 {a n} {A : Set a} {t\u2080\u2080 t\u2080\u2081 t\u2081\u2080 t\u2081\u2081 : Tree A n} \u2192\n Perm (fork (fork t\u2080\u2080 t\u2080\u2081) (fork t\u2081\u2080 t\u2081\u2081)) (fork (fork t\u2080\u2080 t\u2081\u2080) (fork t\u2080\u2081 t\u2081\u2081))\nswp\u2083-perm = second-perm swp \u204f swp-seconds \u204f second-perm swp\n\nswp-firsts-perm : \u2200 {n a} {A : Set a} {tA tB tC tD : Tree A n} \u2192\n Perm (fork (fork tA tB) (fork tC tD))\n (fork (fork tC tB) (fork tA tD))\nswp-firsts-perm = < swp \u00d7 swp >-perm \u204f swp-seconds \u204f < swp \u00d7 swp >-perm\n\nSwp\u21d2Perm : \u2200 {n a} {A : Set a} \u2192 Swp {a} {A} {n} \u21d2 Perm {n = n}\nSwp\u21d2Perm (left pf) = first (Swp\u21d2Perm pf)\nSwp\u21d2Perm (right pf) = second-perm (Swp\u21d2Perm pf)\nSwp\u21d2Perm swp\u2081 = swp\nSwp\u21d2Perm swp\u2082 = swp\u2082-perm\n\nSwp\u2605\u21d2Perm : \u2200 {n a} {A : Set a} \u2192 Swp\u2605 {n} {a} {A} \u21d2 Perm {n = n}\nSwp\u2605\u21d2Perm \u03b5 = \u03b5\nSwp\u2605\u21d2Perm (x \u25c5 xs) = Swp\u21d2Perm x \u204f Swp\u2605\u21d2Perm xs\n\nswp-inners : \u2200 {n} \u2192 SwpOp (2 + n)\nswp-inners = second-swpop swp \u204f swp-seconds \u204f second-swpop swp\n\non-extremes : \u2200 {n} \u2192 SwpOp (1 + n) \u2192 SwpOp (2 + n)\non-extremes f = swp-seconds \u204f first f \u204f swp-seconds\n\non-firsts : \u2200 {n} \u2192 SwpOp (1 + n) \u2192 SwpOp (2 + n)\non-firsts f = swp-inners \u204f first f \u204f swp-inners\n\n0\u2194_ : \u2200 {m n} \u2192 Bits m \u2192 SwpOp (m + n)\n0\u2194 [] = \u03b5\n0\u2194 (false{-0-} \u2237 p) = first (0\u2194 p)\n0\u2194 (true{-1-} \u2237 []) = swp\n0\u2194 (true{-1-} \u2237 true {-1-} \u2237 p) = on-extremes (0\u2194 (1b \u2237 p))\n0\u2194 (true{-1-} \u2237 false{-0-} \u2237 p) = on-firsts (0\u2194 (1b \u2237 p))\n\n0\u2194\u2032_ : \u2200 {n} \u2192 Bits n \u2192 SwpOp n\n0\u2194\u2032_ {n} rewrite cong SwpOp (sym (\u2115\u00b0.+-comm n 0)) = 0\u2194_ {n} {0}\n\n[_\u2194_] : \u2200 {m n} (p q : Bits m) \u2192 SwpOp (m + n)\n[ p \u2194 q ] = 0\u2194 p \u204f 0\u2194 q\n\n[_\u2194\u2032_] : \u2200 {n} (p q : Bits n) \u2192 SwpOp n\n[ p \u2194\u2032 q ] = 0\u2194\u2032 p \u204f 0\u2194\u2032 q\n\n_$swp_ : \u2200 {n a} {A : Set a} \u2192 SwpOp n \u2192 Tree A n \u2192 Tree A n\n\u03b5 $swp t = t\n(f \u204f g) $swp t = g $swp (f $swp t)\n(first f) $swp (fork t\u2080 t\u2081) = fork (f $swp t\u2080) t\u2081\nswp $swp (fork t\u2080 t\u2081) = fork t\u2081 t\u2080\nswp-seconds $swp (fork (fork t\u2080 t\u2081) (fork t\u2082 t\u2083)) = fork (fork t\u2080 t\u2083) (fork t\u2082 t\u2081)\n\nswpRel : \u2200 {n a} {A : Set a} (f : SwpOp n) (t : Tree A n) \u2192 Perm t (f $swp t)\nswpRel \u03b5 _ = \u03b5\nswpRel (f \u204f g) _ = swpRel f _ \u204f swpRel g _\nswpRel (first f) (fork _ _) = first (swpRel f _)\nswpRel swp (fork _ _) = swp\nswpRel swp-seconds\n (fork (fork _ _) (fork _ _)) = swp-seconds\n\n[0\u2194_]-Rel : \u2200 {m n a} {A : Set a} (p : Bits m) (t : Tree A (m + n)) \u2192 Perm t ((0\u2194 p) $swp t)\n[0\u2194 p ]-Rel = swpRel (0\u2194 p)\n\nswpOp' : \u2200 {n a} {A : Set a} {t u : Tree A n} \u2192 Perm0\u2194 t u \u2192 SwpOp n\nswpOp' \u03b5 = \u03b5\nswpOp' (first f) = first (swpOp' f)\nswpOp' swp = swp\nswpOp' (firsts f) = on-firsts (swpOp' f)\nswpOp' (extremes f) = on-extremes (swpOp' f)\n\nswpOp : \u2200 {n a} {A : Set a} {t u : Tree A n} \u2192 Perm t u \u2192 SwpOp n\nswpOp \u03b5 = \u03b5\nswpOp (f \u204f g) = swpOp f \u204f swpOp g\nswpOp (first f) = first (swpOp f)\nswpOp swp = swp\nswpOp swp-seconds = swp-seconds\n\nswpOp-sym : \u2200 {n} \u2192 SwpOp n \u2192 SwpOp n\nswpOp-sym \u03b5 = \u03b5\nswpOp-sym (f \u204f g) = swpOp-sym g \u204f swpOp-sym f\nswpOp-sym (first f) = first (swpOp-sym f)\nswpOp-sym swp = swp\nswpOp-sym swp-seconds = swp-seconds\n\nswpOp-sym-involutive : \u2200 {n} (f : SwpOp n) \u2192 swpOp-sym (swpOp-sym f) \u2261 f\nswpOp-sym-involutive \u03b5 = refl\nswpOp-sym-involutive (f \u204f g) rewrite swpOp-sym-involutive f | swpOp-sym-involutive g = refl\nswpOp-sym-involutive (first f) rewrite swpOp-sym-involutive f = refl\nswpOp-sym-involutive swp = refl\nswpOp-sym-involutive swp-seconds = refl\n\nswpOp-sym-sound : \u2200 {n a} {A : Set a} (f : SwpOp n) (t : Tree A n) \u2192 swpOp-sym f $swp (f $swp t) \u2261 t\nswpOp-sym-sound \u03b5 t = refl\nswpOp-sym-sound (f \u204f g) t rewrite swpOp-sym-sound g (f $swp t) | swpOp-sym-sound f t = refl\nswpOp-sym-sound (first f) (fork t _) rewrite swpOp-sym-sound f t = refl\nswpOp-sym-sound swp (fork _ _) = refl\nswpOp-sym-sound swp-seconds (fork (fork _ _) (fork _ _)) = refl\n\nmodule \u00acswp-comm where\n data X : Set where\n A B C D E F G H : X\n n : \u2115\n n = 3\n t : Tree X n\n t = fork (fork (fork (leaf A) (leaf B))(fork (leaf C) (leaf D))) (fork (fork (leaf E) (leaf F))(fork (leaf G) (leaf H)))\n f : SwpOp n\n f = swp\n g : SwpOp n\n g = first swp\n pf : f $swp (g $swp t) \u2262 g $swp (f $swp t)\n pf ()\n\nswp-leaf : \u2200 {a} {A : Set a} (f : SwpOp 0) (x : A) \u2192 f $swp (leaf x) \u2261 leaf x\nswp-leaf \u03b5 x = refl\nswp-leaf (f \u204f g) x rewrite swp-leaf f x | swp-leaf g x = refl\n\nswpOp-sound : \u2200 {n a} {A : Set a} {t u : Tree A n} (perm : Perm t u) \u2192 (swpOp perm $swp t \u2261 u)\nswpOp-sound \u03b5 = refl\nswpOp-sound (f \u204f f\u2081) rewrite swpOp-sound f | swpOp-sound f\u2081 = refl\nswpOp-sound (first f) rewrite swpOp-sound f = refl\nswpOp-sound swp = refl\nswpOp-sound swp-seconds = refl\n\nopen import Relation.Nullary using (Dec ; yes ; no)\nopen import Relation.Nullary.Negation\n\nmodule new-approach where\n\n open import Data.Empty\n\n import Function.Inverse as FI\n open FI using (_\u2194_; module Inverse; _InverseOf_)\n open import Function.Related\n import Function.Equality\n import Relation.Binary.PropositionalEquality as P\n\n data _\u2208_ {a}{A : Set a}(x : A) : {n : \u2115} \u2192 Tree A n \u2192 Set a where\n here : x \u2208 leaf x\n left : {n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 t\u2081 \u2192 x \u2208 fork t\u2081 t\u2082\n right : {n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 t\u2082 \u2192 x \u2208 fork t\u2081 t\u2082\n\n toBits : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t : Tree A n} \u2192 x \u2208 t \u2192 Bits n\n toBits here = []\n toBits (left key) = 0b \u2237 toBits key\n toBits (right key) = 1b \u2237 toBits key\n\n \u2208-lookup : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t : Tree A n}(path : x \u2208 t) \u2192 lookup (toBits path) t \u2261 x\n \u2208-lookup here = refl\n \u2208-lookup (left path) = \u2208-lookup path\n \u2208-lookup (right path) = \u2208-lookup path\n\n lookup-\u2208 : \u2200 {a}{A : Set a}{n : \u2115}(key : Bits n)(t : Tree A n) \u2192 lookup key t \u2208 t\n lookup-\u2208 [] (leaf x) = here\n lookup-\u2208 (true \u2237 key) (fork tree tree\u2081) = right (lookup-\u2208 key tree\u2081)\n lookup-\u2208 (false \u2237 key) (fork tree tree\u2081) = left (lookup-\u2208 key tree)\n\n _\u2248_ : \u2200 {a}{A : Set a}{n : \u2115} \u2192 Tree A n \u2192 Tree A n \u2192 Set _\n t\u2081 \u2248 t\u2082 = \u2200 x \u2192 (x \u2208 t\u2081) \u2194 (x \u2208 t\u2082) \n\n \u2248-refl : {a : _}{A : Set a}{n : \u2115}{t : Tree A n} \u2192 t \u2248 t\n \u2248-refl _ = FI.id\n\n move : \u2200 {a}{A : Set a}{n : \u2115}{t s : Tree A n}{x : A} \u2192 t \u2248 s \u2192 x \u2208 t \u2192 x \u2208 s\n move t\u2248s x\u2208t = Inverse.to (t\u2248s _) Function.Equality.\u27e8$\u27e9 x\u2208t\n\n swap\u2080 : \u2200 {a}{A : Set a}{n : \u2115}(t\u2081 t\u2082 : Tree A n) \u2192 fork t\u2081 t\u2082 \u2248 fork t\u2082 t\u2081\n swap\u2080 t\u2081 t\u2082 = \u03bb x \u2192 record \n { to = \u2192-to-\u27f6 swap\n ; from = \u2192-to-\u27f6 swap\n ; inverse-of = record { left-inverse-of = swap-inv \n ; right-inverse-of = swap-inv } \n } where\n swap : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 x \u2208 fork t\u2081 t\u2082 \u2192 x \u2208 fork t\u2082 t\u2081\n swap (left path) = right path\n swap (right path) = left path\n\n swap-inv : \u2200 {a}{A : Set a}{x : A}{n : \u2115}{t\u2081 t\u2082 : Tree A n}(p : x \u2208 fork t\u2081 t\u2082) \u2192 swap (swap p) \u2261 p\n swap-inv (left p) = refl\n swap-inv (right p) = refl\n\n _\u27e8fork\u27e9_ : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 s\u2081 s\u2082 : Tree A n} \u2192 t\u2081 \u2248 s\u2081 \u2192 t\u2082 \u2248 s\u2082 \u2192 fork t\u2081 t\u2082 \u2248 fork s\u2081 s\u2082\n (t1\u2248s1 \u27e8fork\u27e9 t2\u2248s2) y = record \n { to = to\n ; from = from\n ; inverse-of = record { left-inverse-of = frk-linv\n ; right-inverse-of = frk-rinv } \n } where\n \n frk : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 s\u2081 s\u2082 : Tree A n}{x : A} \u2192 t\u2081 \u2248 s\u2081 \u2192 t\u2082 \u2248 s\u2082 \u2192 x \u2208 fork t\u2081 t\u2082 \u2192 x \u2208 fork s\u2081 s\u2082\n frk t1\u2248s1 t2\u2248s2 (left x\u2208t1) = left (move t1\u2248s1 x\u2208t1)\n frk t1\u2248s1 t2\u2248s2 (right x\u2208t2) = right (move t2\u2248s2 x\u2208t2)\n \n to = \u2192-to-\u27f6 (frk t1\u2248s1 t2\u2248s2)\n from = \u2192-to-\u27f6 (frk (\u03bb x \u2192 FI.sym (t1\u2248s1 x)) (\u03bb x \u2192 FI.sym (t2\u2248s2 x)))\n\n \n open Function.Equality using (_\u27e8$\u27e9_)\n open import Function.LeftInverse\n\n frk-linv : from LeftInverseOf to\n frk-linv (left x) = cong left (_InverseOf_.left-inverse-of (Inverse.inverse-of (t1\u2248s1 y)) x)\n frk-linv (right x) = cong right (_InverseOf_.left-inverse-of (Inverse.inverse-of (t2\u2248s2 y)) x)\n\n frk-rinv : from RightInverseOf to -- \u2200 x \u2192 to \u27e8$\u27e9 (from \u27e8$\u27e9 x) \u2261 x\n frk-rinv (left x) = cong left (_InverseOf_.right-inverse-of (Inverse.inverse-of (t1\u2248s1 y)) x)\n frk-rinv (right x) = cong right (_InverseOf_.right-inverse-of (Inverse.inverse-of (t2\u2248s2 y)) x)\n\n Rot\u27f6\u2248 : \u2200 {a}{A : Set a}{n : \u2115}{t\u2081 t\u2082 : Tree A n} \u2192 Rot t\u2081 t\u2082 \u2192 t\u2081 \u2248 t\u2082\n Rot\u27f6\u2248 (leaf x) y = FI.id\n Rot\u27f6\u2248 (fork rot rot\u2081) y = (Rot\u27f6\u2248 rot \u27e8fork\u27e9 Rot\u27f6\u2248 rot\u2081) y\n Rot\u27f6\u2248 (krof {_} {l} {l'} {r} {r'} rot rot\u2081) y = \n y \u2208 fork l r \u2194\u27e8 (Rot\u27f6\u2248 rot \u27e8fork\u27e9 Rot\u27f6\u2248 rot\u2081) y \u27e9\n y \u2208 fork r' l' \u2194\u27e8 swap\u2080 r' l' y \u27e9 \n y \u2208 fork l' r' \u220e\n where open EquationalReasoning\n\n put : {a : _}{A : Set a}{n : \u2115} \u2192 Bits n \u2192 A \u2192 Tree A n \u2192 Tree A n\n put [] val tree = leaf val\n put (x \u2237 key) val (fork tree tree\u2081) = if x then fork tree (put key val tree\u2081) \n else fork (put key val tree) tree\u2081\n\n -- move-me\n _\u2237\u2262_ : {n : \u2115}{xs ys : Bits n}(x : Bit) \u2192 x \u2237 xs \u2262 x \u2237 ys \u2192 xs \u2262 ys\n _\u2237\u2262_ x = contraposition $ cong $ _\u2237_ x\n\n \u2208-put : {a : _}{A : Set a}{n : \u2115}(p : Bits n){x : A}(t : Tree A n) \u2192 x \u2208 put p x t\n \u2208-put [] t = here\n \u2208-put (true \u2237 p) (fork t t\u2081) = right (\u2208-put p t\u2081)\n \u2208-put (false \u2237 p) (fork t t\u2081) = left (\u2208-put p t)\n\n \u2208-put-\u2262 : {a : _}{A : Set a}{n : \u2115}(p : Bits n){x y : A}{t : Tree A n}(path : x \u2208 t)\n \u2192 p \u2262 toBits path \u2192 x \u2208 put p y t\n \u2208-put-\u2262 [] here neg = \u22a5-elim (neg refl)\n \u2208-put-\u2262 (true \u2237 p) (left path) neg = left path\n \u2208-put-\u2262 (false \u2237 p) (left path) neg = left (\u2208-put-\u2262 p path (false \u2237\u2262 neg))\n \u2208-put-\u2262 (true \u2237 p) (right path) neg = right (\u2208-put-\u2262 p path (true \u2237\u2262 neg))\n \u2208-put-\u2262 (false \u2237 p) (right path) neg = right path\n\n swap : {a : _}{A : Set a}{n : \u2115} \u2192 (p\u2081 p\u2082 : Bits n) \u2192 Tree A n \u2192 Tree A n\n swap p\u2081 p\u2082 t = put p\u2081 a\u2082 (put p\u2082 a\u2081 t)\n where\n a\u2081 = lookup p\u2081 t\n a\u2082 = lookup p\u2082 t\n\n swap-perm\u2081 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p : x \u2208 t) \u2192 t \u2248 swap (toBits p) (toBits p) t\n swap-perm\u2081 here = \u2248-refl \n swap-perm\u2081 (left path) = swap-perm\u2081 path \u27e8fork\u27e9 \u2248-refl\n swap-perm\u2081 (right path) = \u2248-refl \u27e8fork\u27e9 swap-perm\u2081 path\n\n swap-comm : {a : _}{A : Set a}{n : \u2115} (p\u2081 p\u2082 : Bits n)(t : Tree A n) \u2192 swap p\u2082 p\u2081 t \u2261 swap p\u2081 p\u2082 t\n swap-comm [] [] (leaf x) = refl\n swap-comm (true \u2237 p\u2081) (true \u2237 p\u2082) (fork t t\u2081) = cong (fork t) (swap-comm p\u2081 p\u2082 t\u2081)\n swap-comm (true \u2237 p\u2081) (false \u2237 p\u2082) (fork t t\u2081) = refl\n swap-comm (false \u2237 p\u2081) (true \u2237 p\u2082) (fork t t\u2081) = refl\n swap-comm (false \u2237 p\u2081) (false \u2237 p\u2082) (fork t t\u2081) = cong (flip fork t\u2081) (swap-comm p\u2081 p\u2082 t)\n\n swap-perm\u2082 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p' : Bits n)(p : x \u2208 t) \n \u2192 x \u2208 swap (toBits p) p' t\n swap-perm\u2082 _ here = here\n swap-perm\u2082 (true \u2237 p) (left path) rewrite \u2208-lookup path = right (\u2208-put p _)\n swap-perm\u2082 (false \u2237 p) (left path) = left (swap-perm\u2082 p path)\n swap-perm\u2082 (true \u2237 p) (right path) = right (swap-perm\u2082 p path)\n swap-perm\u2082 (false \u2237 p) (right path) rewrite \u2208-lookup path = left (\u2208-put p _)\n\n swap-perm\u2083 : {a : _}{A : Set a}{n : \u2115}{t : Tree A n}{x : A}(p\u2081 p\u2082 : Bits n)(p : x \u2208 t)\n \u2192 p\u2081 \u2262 toBits p \u2192 p\u2082 \u2262 toBits p \u2192 x \u2208 swap p\u2081 p\u2082 t\n swap-perm\u2083 [] [] here neg\u2081 neg\u2082 = here\n swap-perm\u2083 (true \u2237 p\u2081) (true \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left path\n swap-perm\u2083 (true \u2237 p\u2081) (false \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left (\u2208-put-\u2262 _ path (false \u2237\u2262 neg\u2082))\n swap-perm\u2083 (false \u2237 p\u2081) (true \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left (\u2208-put-\u2262 _ path (false \u2237\u2262 neg\u2081))\n swap-perm\u2083 (false \u2237 p\u2081) (false \u2237 p\u2082) (left path) neg\u2081 neg\u2082 = left \n (swap-perm\u2083 p\u2081 p\u2082 path (false \u2237\u2262 neg\u2081) (false \u2237\u2262 neg\u2082))\n swap-perm\u2083 (true \u2237 p\u2081) (true \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right\n (swap-perm\u2083 p\u2081 p\u2082 path (true \u2237\u2262 neg\u2081) (true \u2237\u2262 neg\u2082))\n swap-perm\u2083 (true \u2237 p\u2081) (false \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right (\u2208-put-\u2262 _ path (true \u2237\u2262 neg\u2081))\n swap-perm\u2083 (false \u2237 p\u2081) (true \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right (\u2208-put-\u2262 _ path (true \u2237\u2262 neg\u2082))\n swap-perm\u2083 (false \u2237 p\u2081) (false \u2237 p\u2082) (right path) neg\u2081 neg\u2082 = right path\n\nmodule FoldProp {a} {A : Set a} (_\u00b7_ : Op\u2082 A) (op-comm : Commutative _\u2261_ _\u00b7_) (op-assoc : Associative _\u2261_ _\u00b7_) where\n\n \u27ea_\u27eb : \u2200 {n} \u2192 Tree A n \u2192 A\n \u27ea_\u27eb = fold _\u00b7_\n\n _=[fold]\u21d2\u2032_ : \u2200 {\u2113\u2081 \u2113\u2082} \u2192 (\u2200 {m n} \u2192 REL (Tree A m) (Tree A n) \u2113\u2081) \u2192 Rel A \u2113\u2082 \u2192 Set _\n -- _\u223c\u2080_ =[fold]\u21d2 _\u223c\u2081_ = \u2200 {m n} \u2192 _\u223c\u2080_ {m} {n} =[ fold {n} _\u00b7_ ]\u21d2 _\u223c\u2081_\n _\u223c\u2080_ =[fold]\u21d2\u2032 _\u223c\u2081_ = \u2200 {m n} {t : Tree A m} {u : Tree A n} \u2192 t \u223c\u2080 u \u2192 \u27ea t \u27eb \u223c\u2081 \u27ea u \u27eb\n\n _=[fold]\u21d2_ : \u2200 {\u2113\u2081 \u2113\u2082} \u2192 (\u2200 {n} \u2192 Rel (Tree A n) \u2113\u2081) \u2192 Rel A \u2113\u2082 \u2192 Set _\n _\u223c\u2080_ =[fold]\u21d2 _\u223c\u2081_ = \u2200 {n} \u2192 _\u223c\u2080_ =[ fold {n} _\u00b7_ ]\u21d2 _\u223c\u2081_\n\n fold-rot : Rot =[fold]\u21d2 _\u2261_\n fold-rot (leaf x) = refl\n fold-rot (fork rot rot\u2081) = cong\u2082 _\u00b7_ (fold-rot rot) (fold-rot rot\u2081)\n fold-rot (krof rot rot\u2081) rewrite fold-rot rot | fold-rot rot\u2081 = op-comm _ _\n\n -- t \u223c u \u2192 fork v t \u223c fork u w\n\n lem : \u2200 x y z t \u2192 (x \u00b7 y) \u00b7 (z \u00b7 t) \u2261 (t \u00b7 y) \u00b7 (z \u00b7 x)\n lem x y z t = (x \u00b7 y) \u00b7 (z \u00b7 t)\n \u2261\u27e8 op-assoc x y _ \u27e9\n x \u00b7 (y \u00b7 (z \u00b7 t))\n \u2261\u27e8 op-comm x _ \u27e9\n (y \u00b7 (z \u00b7 t)) \u00b7 x\n \u2261\u27e8 op-assoc y (z \u00b7 t) _ \u27e9\n y \u00b7 ((z \u00b7 t) \u00b7 x)\n \u2261\u27e8 cong (\u03bb u \u2192 y \u00b7 (u \u00b7 x)) (op-comm z t) \u27e9\n y \u00b7 ((t \u00b7 z) \u00b7 x)\n \u2261\u27e8 cong (_\u00b7_ y) (op-assoc t z x) \u27e9\n y \u00b7 (t \u00b7 (z \u00b7 x))\n \u2261\u27e8 sym (op-assoc y t _) \u27e9\n (y \u00b7 t) \u00b7 (z \u00b7 x)\n \u2261\u27e8 cong (\u03bb u \u2192 u \u00b7 (z \u00b7 x)) (op-comm y t) \u27e9\n (t \u00b7 y) \u00b7 (z \u00b7 x)\n \u220e\n where open \u2261-Reasoning\n\n fold-swp : Swp =[fold]\u21d2 _\u2261_\n fold-swp (left pf) rewrite fold-swp pf = refl\n fold-swp (right pf) rewrite fold-swp pf = refl\n fold-swp swp\u2081 = op-comm _ _\n fold-swp (swp\u2082 {_} {t\u2080\u2080} {t\u2080\u2081} {t\u2081\u2080} {t\u2081\u2081}) = lem \u27ea t\u2080\u2080 \u27eb \u27ea t\u2080\u2081 \u27eb \u27ea t\u2081\u2080 \u27eb \u27ea t\u2081\u2081 \u27eb\n\n fold-swp\u2605 : Swp\u2605 =[fold]\u21d2 _\u2261_\n fold-swp\u2605 \u03b5 = refl\n fold-swp\u2605 (x \u25c5 xs) rewrite fold-swp x | fold-swp\u2605 xs = refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"9999ee97cc4e6910cd88265a1a3e71cac16b407f","subject":"bintree,sorting: cleanup","message":"bintree,sorting: cleanup\n","repos":"crypto-agda\/crypto-agda","old_file":"prefect-bintree-sorting.agda","new_file":"prefect-bintree-sorting.agda","new_contents":"module prefect-bintree-sorting where\nimport Level as L\nopen import Function.NP\nimport Data.Nat.NP as Nat\nopen Nat using (\u2115; zero; suc; 2^_; _+_; _\u2238_; module \u2115\u00b0; module \u2115\u2264; +-\u2264-inj; \u00acn+\u2264y meaning that the naturals are indeed the\n -- same and inr <> that they are not.\n natEQ : (x y : Nat) \u2192 ((x == y) + ((x == y) \u2192 \u22a5))\n natEQ Z Z = Inl refl\n natEQ Z (1+ y) = Inr (\u03bb ())\n natEQ (1+ x) Z = Inr (\u03bb ())\n natEQ (1+ x) (1+ y) with natEQ x y\n natEQ (1+ x) (1+ .x) | Inl refl = Inl refl\n ... | Inr b = Inr (\u03bb x\u2081 \u2192 b (1+inj x y x\u2081))\n\n -- nat equality as a predicate. this saves some very repetative casing.\n natEQp : (x y : Nat) \u2192 Set\n natEQp x y with natEQ x y\n natEQp x .x | Inl refl = \u22a5\n natEQp x y | Inr x\u2081 = \u22a4\n\n -- there are plenty of nats\n different-nat : (x : Nat) \u2192 \u03a3[ y \u2208 Nat ] (y \u2260 x)\n different-nat Z = 1+ Z , (\u03bb ())\n different-nat (1+ x) with different-nat x\n different-nat (1+ x) | \u03c01 , \u03c02 with natEQ \u03c01 x\n different-nat (1+ x) | .x , \u03c02 | Inl refl = abort (\u03c02 refl)\n different-nat (1+ x) | \u03c01 , \u03c02 | Inr x\u2081 = Z , (\u03bb ())\n\n different-nat2 : (x y : Nat) \u2192 \u03a3[ z \u2208 Nat ](z \u2260 y \u00d7 z \u2260 x)\n different-nat2 Z Z = 1+ Z , (\u03bb ()) , (\u03bb ())\n different-nat2 Z (1+ y) = (1+ (1+ y)) , 1+not2+ y , 1+notZ (1+ y)\n different-nat2 (1+ x) Z = 1+ (1+ x) , 1+notZ (1+ x) , 1+not2+ x\n different-nat2 (1+ x) (1+ y) with different-nat2 x y\n ... | z , \u2260x , \u2260y = 1+ z , (\u03bb x\u2081 \u2192 \u2260x (1+inj z y x\u2081)) , (\u03bb x\u2081 \u2192 \u2260y (1+inj z x x\u2081))\n","old_contents":"open import Prelude\n\nmodule Nat where\n data Nat : Set where\n Z : Nat\n 1+ : Nat \u2192 Nat\n\n {-# BUILTIN NATURAL Nat #-}\n\n -- the succ operation is injective\n 1+inj : (x y : Nat) \u2192 (1+ x == 1+ y) \u2192 x == y\n 1+inj Z .0 refl = refl\n 1+inj (1+ x) .(1+ x) refl = refl\n\n -- equality of naturals is decidable. we represent this as computing a\n -- choice of units, with inl <> meaning that the naturals are indeed the\n -- same and inr <> that they are not.\n natEQ : (x y : Nat) \u2192 ((x == y) + ((x == y) \u2192 \u22a5))\n natEQ Z Z = Inl refl\n natEQ Z (1+ y) = Inr (\u03bb ())\n natEQ (1+ x) Z = Inr (\u03bb ())\n natEQ (1+ x) (1+ y) with natEQ x y\n natEQ (1+ x) (1+ .x) | Inl refl = Inl refl\n ... | Inr b = Inr (\u03bb x\u2081 \u2192 b (1+inj x y x\u2081))\n\n -- nat equality as a predicate. this saves some very repetative casing.\n natEQp : (x y : Nat) \u2192 Set\n natEQp x y with natEQ x y\n natEQp x .x | Inl refl = \u22a5\n natEQp x y | Inr x\u2081 = \u22a4\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"864ac27c4d8534c751ee19fc93407a6e3fda2863","subject":"missing a file","message":"missing a file\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"all.agda","new_file":"all.agda","new_contents":"open import List\nopen import Nat\nopen import Prelude\n\nopen import contexts\nopen import core\nopen import lemmas-consistency\nopen import lemmas-matching\nopen import synth-unicity\nopen import htype-decidable\n\nopen import correspondence\n-- open import expandability\n-- open import expansion-unicity\n-- open import preservation\n-- open import progress\n-- open import type-assignment-unicity\n-- open import typed-expansion\n","old_contents":"open import List\nopen import Nat\nopen import Prelude\n\nopen import contexts\nopen import core\nopen import lemmas-consistency\nopen import lemmas-matching\nopen import synth-unicity\n\nopen import correspondence\n-- open import expandability\n-- open import expansion-unicity\n-- open import preservation\n-- open import progress\n-- open import type-assignment-unicity\n-- open import typed-expansion\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9911b24bcce90c2dbcecb1d4addf102a7bf6cb2e","subject":"bij, Alt-Syn: ``tail based on ``\u2218","message":"bij, Alt-Syn: ``tail based on ``\u2218\n","repos":"crypto-agda\/crypto-agda","old_file":"bijection-fin.agda","new_file":"bijection-fin.agda","new_contents":"module bijection-fin where\n\n open import bijection\n open import Function.NP hiding (Cmp)\n open import Relation.Binary.PropositionalEquality\n\n open import Data.Empty\n open import Data.Nat.NP\n open import Data.Fin using (Fin ; zero ; suc ; from\u2115 ; inject\u2081)\n open import Data.Vec hiding ([_])\n\n data `Syn : \u2115 \u2192 Set where \n `id : \u2200 {n} \u2192 `Syn n\n `swap : \u2200 {n} \u2192 `Syn (2 + n)\n `tail : \u2200 {n} \u2192 `Syn n \u2192 `Syn (1 + n)\n _`\u2218_ : \u2200 {n} \u2192 `Syn n \u2192 `Syn n \u2192 `Syn n\n\n `Rep = Fin\n\n `Ix = \u2115\n\n `Tree : Set \u2192 `Ix \u2192 Set\n `Tree X = Vec X\n\n `fromFun : \u2200 {i X} \u2192 (`Rep i \u2192 X) \u2192 `Tree X i\n `fromFun = tabulate\n\n `toFun : \u2200 {i X} \u2192 `Tree X i \u2192 (`Rep i \u2192 X)\n `toFun T zero = head T\n `toFun T (suc i) = `toFun (tail T) i\n\n `toFun\u2218fromFun : \u2200 {i X}(f : `Rep i \u2192 X) \u2192 f \u2257 `toFun (`fromFun f)\n `toFun\u2218fromFun f zero = refl\n `toFun\u2218fromFun f (suc i) = `toFun\u2218fromFun (f \u2218 suc) i\n\n fin-swap : \u2200 {n} \u2192 Endo (Fin (2 + n))\n fin-swap zero = suc zero\n fin-swap (suc zero) = zero\n fin-swap (suc (suc i)) = suc (suc i)\n\n fin-tail : \u2200 {n} \u2192 Endo (Fin n) \u2192 Endo (Fin (1 + n))\n fin-tail f zero = zero\n fin-tail f (suc i) = suc (f i)\n\n `evalArg : \u2200 {i} \u2192 `Syn i \u2192 Endo (`Rep i)\n `evalArg `id = id\n `evalArg `swap = fin-swap\n `evalArg (`tail f) = fin-tail (`evalArg f)\n `evalArg (S `\u2218 S\u2081) = `evalArg S \u2218 `evalArg S\u2081\n\n vec-swap : \u2200 {n}{X : Set} \u2192 Endo (Vec X (2 + n))\n vec-swap xs = head (tail xs) \u2237 head xs \u2237 tail (tail xs)\n\n vec-tail : \u2200 {n}{X : Set} \u2192 Endo (Vec X n) \u2192 Endo (Vec X (1 + n))\n vec-tail f xs = head xs \u2237 f (tail xs)\n\n `evalTree : \u2200 {i X} \u2192 `Syn i \u2192 Endo (`Tree X i)\n `evalTree `id = id\n `evalTree `swap = vec-swap\n `evalTree (`tail f) = vec-tail (`evalTree f)\n `evalTree (S `\u2218 S\u2081) = `evalTree S \u2218 `evalTree S\u2081\n\n `eval-proof : \u2200 {i X} S (T : `Tree X i) \u2192 `toFun T \u2257 `toFun (`evalTree S T) \u2218 `evalArg S\n `eval-proof `id T i = refl\n `eval-proof `swap T zero = refl\n `eval-proof `swap T (suc zero) = refl\n `eval-proof `swap T (suc (suc i)) = refl\n `eval-proof (`tail S) T zero = refl\n `eval-proof (`tail S) T (suc i) = `eval-proof S (tail T) i\n `eval-proof (S `\u2218 S\u2081) T i rewrite\n `eval-proof S\u2081 T i |\n `eval-proof S (`evalTree S\u2081 T) (`evalArg S\u2081 i) = refl\n\n `inv : \u2200 {i} \u2192 Endo (`Syn i)\n `inv `id = `id\n `inv `swap = `swap\n `inv (`tail S) = `tail (`inv S)\n `inv (S `\u2218 S\u2081) = `inv S\u2081 `\u2218 `inv S\n\n `inv-proof : \u2200 {i} \u2192 (S : `Syn i) \u2192 `evalArg S \u2218 `evalArg (`inv S) \u2257 id\n `inv-proof `id x = refl\n `inv-proof `swap zero = refl\n `inv-proof `swap (suc zero) = refl\n `inv-proof `swap (suc (suc x)) = refl\n `inv-proof (`tail S) zero = refl\n `inv-proof (`tail S) (suc x) rewrite `inv-proof S x = refl\n `inv-proof (S `\u2218 S\u2081) x rewrite \n `inv-proof S\u2081 (`evalArg (`inv S) x) |\n `inv-proof S x = refl\n\n `RC : \u2200 {i} \u2192 Cmp (`Rep i)\n `RC zero zero = eq\n `RC zero (suc j) = lt\n `RC (suc i) zero = gt\n `RC (suc i) (suc j) = `RC i j\n\n insert : \u2200 {n X} \u2192 Cmp X \u2192 X \u2192 Vec X n \u2192 Vec X (1 + n)\n insert X-cmp x [] = x \u2237 []\n insert X-cmp x (x\u2081 \u2237 xs) with X-cmp x x\u2081\n insert X-cmp x (x\u2081 \u2237 xs) | lt = x \u2237 x\u2081 \u2237 xs\n insert X-cmp x (x\u2081 \u2237 xs) | eq = x \u2237 x\u2081 \u2237 xs\n insert X-cmp x (x\u2081 \u2237 xs) | gt = x\u2081 \u2237 insert X-cmp x xs\n\n `sort : \u2200 {i X} \u2192 Cmp X \u2192 Endo (`Tree X i)\n `sort X-cmp [] = []\n `sort X-cmp (x \u2237 xs) = insert X-cmp x (`sort X-cmp xs)\n\n insert-syn : \u2200 {n X} \u2192 Cmp X \u2192 X \u2192 Vec X n \u2192 `Syn (1 + n)\n insert-syn X-cmp x [] = `id\n insert-syn X-cmp x (x\u2081 \u2237 xs) with X-cmp x x\u2081\n insert-syn X-cmp x (x\u2081 \u2237 xs) | lt = `id\n insert-syn X-cmp x (x\u2081 \u2237 xs) | eq = `id\n insert-syn X-cmp x (x\u2081 \u2237 xs) | gt = `tail (insert-syn X-cmp x xs) `\u2218 `swap\n\n `sort-syn : \u2200 {i X} \u2192 Cmp X \u2192 `Tree X i \u2192 `Syn i\n `sort-syn X-cmp [] = `id\n `sort-syn X-cmp (x \u2237 xs) = insert-syn X-cmp x (`sort X-cmp xs) `\u2218 `tail (`sort-syn X-cmp xs)\n\n insert-proof : \u2200 {n X}(X-cmp : Cmp X) x (T : Vec X n) \u2192 insert X-cmp x T \u2261 `evalTree (insert-syn X-cmp x T) (x \u2237 T)\n insert-proof X-cmp x [] = refl\n insert-proof X-cmp x (x\u2081 \u2237 T) with X-cmp x x\u2081\n insert-proof X-cmp x (x\u2081 \u2237 T) | lt = refl\n insert-proof X-cmp x (x\u2081 \u2237 T) | eq = refl\n insert-proof X-cmp x (x\u2081 \u2237 T) | gt rewrite insert-proof X-cmp x T = refl\n\n `sort-proof : \u2200 {i X}(X-cmp : Cmp X)(T : `Tree X i) \u2192 `sort X-cmp T \u2261 `evalTree (`sort-syn X-cmp T) T\n `sort-proof X-cmp [] = refl\n `sort-proof X-cmp (x \u2237 T) rewrite \n sym (`sort-proof X-cmp T)= insert-proof X-cmp x (`sort X-cmp T)\n\n module Alt-Syn where\n\n data ``Syn : \u2115 \u2192 Set where\n `id : \u2200 {n} \u2192 ``Syn n\n _`\u2218_ : \u2200 {n} \u2192 ``Syn n \u2192 ``Syn n \u2192 ``Syn n\n `swap : \u2200 {n} m \u2192 ``Syn (m + 2 + n)\n\n swap-fin : \u2200 {n} m \u2192 Endo (Fin (m + 2 + n))\n swap-fin zero zero = suc zero\n swap-fin zero (suc zero) = zero\n swap-fin zero (suc (suc i)) = suc (suc i)\n swap-fin (suc m) zero = zero\n swap-fin (suc m) (suc i) = suc (swap-fin m i)\n\n ``evalArg : \u2200 {n} \u2192 ``Syn n \u2192 Endo (`Rep n)\n ``evalArg `id = id\n ``evalArg (S `\u2218 S\u2081) = ``evalArg S \u2218 ``evalArg S\u2081\n ``evalArg (`swap m) = swap-fin m\n\n _``\u2218_ : \u2200 {n} \u2192 ``Syn n \u2192 ``Syn n \u2192 ``Syn n\n `id ``\u2218 y = y\n (x `\u2218 x\u2081) ``\u2218 `id = x `\u2218 x\u2081\n (x `\u2218 x\u2081) ``\u2218 (y `\u2218 y\u2081) = x `\u2218 (x\u2081 `\u2218 (y `\u2218 y\u2081))\n (x `\u2218 x\u2081) ``\u2218 `swap m = x `\u2218 (x\u2081 ``\u2218 `swap m)\n `swap m ``\u2218 y = `swap m `\u2218 y\n\n ``tail : \u2200 {n} \u2192 ``Syn n \u2192 ``Syn (suc n)\n ``tail `id = `id\n ``tail (S `\u2218 S\u2081) = ``tail S ``\u2218 ``tail S\u2081\n ``tail (`swap m) = `swap (suc m)\n\n translate : \u2200 {n} \u2192 `Syn n \u2192 ``Syn n\n translate `id = `id\n translate `swap = `swap 0\n translate (`tail S) = ``tail (translate S)\n translate (S `\u2218 S\u2081) = translate S ``\u2218 translate S\u2081\n\n ``\u2218-p : \u2200 {n}(A B : ``Syn n) \u2192 ``evalArg (A ``\u2218 B) \u2257 ``evalArg (A `\u2218 B)\n ``\u2218-p `id B x = refl\n ``\u2218-p (A `\u2218 A\u2081) `id x = refl\n ``\u2218-p (A `\u2218 A\u2081) (B `\u2218 B\u2081) x = refl\n ``\u2218-p (A `\u2218 A\u2081) (`swap m) x rewrite ``\u2218-p A\u2081 (`swap m) x = refl\n ``\u2218-p (`swap m) B x = refl\n\n ``tail-p : \u2200 {n} (S : ``Syn n) \u2192 fin-tail (``evalArg S) \u2257 ``evalArg (``tail S)\n ``tail-p `id zero = refl\n ``tail-p `id (suc x) = refl\n ``tail-p (S `\u2218 S\u2081) zero rewrite ``\u2218-p (``tail S) (``tail S\u2081) zero\n | sym (``tail-p S\u2081 zero) = ``tail-p S zero\n ``tail-p (S `\u2218 S\u2081) (suc x) rewrite ``\u2218-p (``tail S) (``tail S\u2081) (suc x)\n | sym (``tail-p S\u2081 (suc x)) = ``tail-p S (suc (``evalArg S\u2081 x))\n ``tail-p (`swap m) zero = refl\n ``tail-p (`swap m) (suc x) = refl\n\n `eval`` : \u2200 {n} (S : `Syn n) \u2192 `evalArg S \u2257 ``evalArg (translate S)\n `eval`` `id x = refl\n `eval`` `swap zero = refl\n `eval`` `swap (suc zero) = refl\n `eval`` `swap (suc (suc x)) = refl\n `eval`` (`tail S) zero = ``tail-p (translate S) zero\n `eval`` (`tail S) (suc x) rewrite `eval`` S x = ``tail-p (translate S) (suc x)\n `eval`` (S `\u2218 S\u2081) x rewrite ``\u2218-p (translate S) (translate S\u2081) x | sym (`eval`` S\u2081 x) | `eval`` S (`evalArg S\u2081 x) = refl\n\n\n data Fin-View : \u2200 {n} \u2192 Fin n \u2192 Set where\n max : \u2200 {n} \u2192 Fin-View (from\u2115 n)\n inject : \u2200 {n} \u2192 (i : Fin n) \u2192 Fin-View (inject\u2081 i)\n\n data _\u2264F_ : \u2200 {n} \u2192 Fin n \u2192 Fin n \u2192 Set where\n z\u2264i : {n : \u2115}{i : Fin (suc n)} \u2192 zero \u2264F i\n s\u2264s : {n : \u2115}{i j : Fin n} \u2192 i \u2264F j \u2192 suc i \u2264F suc j\n\n \u2264F-refl : \u2200 {n} (x : Fin n) \u2192 x \u2264F x\n \u2264F-refl zero = z\u2264i\n \u2264F-refl (suc i) = s\u2264s (\u2264F-refl i)\n\n _\u2261 : \u2200 {i} (x y : Fin i) \u2192 eq \u2261 `RC x y \u2192 x \u2261 y\n eq=>\u2261 zero zero p = refl\n eq=>\u2261 zero (suc y) ()\n eq=>\u2261 (suc x) zero ()\n eq=>\u2261 (suc x) (suc y) p rewrite eq=>\u2261 x y p = refl\n\n insert-Sorted : \u2200 {n l}{V : Vec (Fin n) l}(x : Fin n) \u2192 Sorted {Fin n} `RC V \u2192 Sorted {Fin n} `RC (insert `RC x V)\n insert-Sorted x [] = sing x\n insert-Sorted x (sing x\u2081) with `RC x x\u2081 | dbl-lt {XC = `RC} x x\u2081 {[]} | eq=>\u2261 x x\u2081 | flip-RC x x\u2081\n insert-Sorted x (sing x\u2081) | lt | b | _ | _ = b refl (sing x\u2081)\n insert-Sorted x (sing x\u2081) | eq | _ | p | _ rewrite p refl = dbl-eq x\u2081 (sing x\u2081)\n insert-Sorted x (sing x\u2081) | gt | b | _ | l = dbl-lt x\u2081 x l (sing x)\n insert-Sorted x (dbl-lt y y' {xs} prf xs\u2081) with `RC x y | dbl-lt {XC = `RC} x y {y' \u2237 xs} | eq=>\u2261 x y | flip-RC x y\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | lt | b | p | l\u2081 = b refl (dbl-lt y y' prf xs\u2081)\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | eq | b | p | l\u2081 rewrite p refl = dbl-eq y (dbl-lt y y' prf xs\u2081)\n insert-Sorted x (dbl-lt y y' {xs} prf xs\u2081) | gt | b | p | l\u2081 with `RC x y' | insert-Sorted x xs\u2081\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | gt | b | p | l\u2081 | lt | xs' = dbl-lt y x l\u2081 xs'\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | gt | b | p | l\u2081 | eq | xs' = dbl-lt y x l\u2081 xs'\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | gt | b | p | l\u2081 | gt | xs' = dbl-lt y y' prf xs'\n insert-Sorted x (dbl-eq y {xs} xs\u2081) with `RC x y | inspect (`RC x) y | dbl-lt {XC = `RC} x y {y \u2237 xs} | eq=>\u2261 x y | flip-RC x y | insert-Sorted x xs\u2081\n insert-Sorted x (dbl-eq y xs\u2081) | lt | _ | b | p | l | _ = b refl (dbl-eq y xs\u2081)\n insert-Sorted x (dbl-eq y xs\u2081) | eq | _ | b | p | l | _ rewrite p refl = dbl-eq y (dbl-eq y xs\u2081)\n insert-Sorted x (dbl-eq y xs\u2081) | gt | [ prf ] | b | p | l | ss rewrite prf = dbl-eq y ss \n\n sort-Sorted : \u2200 {n l}(V : Vec (Fin n) l) \u2192 Sorted `RC (`sort `RC V)\n sort-Sorted [] = []\n sort-Sorted (x \u2237 V) = insert-Sorted x (sort-Sorted V)\n\n RC-refl : \u2200 {i}(x : Fin i) \u2192 `RC x x \u2261 eq\n RC-refl zero = refl\n RC-refl (suc x) = RC-refl x\n\n STail : \u2200 {X l}{XC : Cmp X}{xs : Vec X (suc l)} \u2192 Sorted XC xs \u2192 Sorted XC (tail xs)\n STail (sing x) = []\n STail (dbl-lt x y x\u2081 T) = T\n STail (dbl-eq x T) = T\n\n module sproof {X}(XC : Cmp X)(XC-refl : \u2200 x \u2192 XC x x \u2261 eq)\n (eq\u2261 : \u2200 x y \u2192 XC x y \u2261 eq \u2192 x \u2261 y)\n (lt-trans : \u2200 x y z \u2192 XC x y \u2261 lt \u2192 XC y z \u2261 lt \u2192 XC x z \u2261 lt)\n (XC-flip : \u2200 x y \u2192 opposite (XC x y) \u2261 XC y x)\n where\n\n open import Data.Sum\n\n _\u2264X_ : X \u2192 X \u2192 Set\n x \u2264X y = XC x y \u2261 lt \u228e XC x y \u2261 eq\n\n \u2264X-trans : \u2200 {x y z} \u2192 x \u2264X y \u2192 y \u2264X z \u2192 x \u2264X z\n \u2264X-trans (inj\u2081 x\u2081) (inj\u2081 x\u2082) = inj\u2081 (lt-trans _ _ _ x\u2081 x\u2082)\n \u2264X-trans {_}{y}{z}(inj\u2081 x\u2081) (inj\u2082 y\u2081) rewrite eq\u2261 y z y\u2081 = inj\u2081 x\u2081\n \u2264X-trans {x}{y} (inj\u2082 y\u2081) y\u2264z rewrite eq\u2261 x y y\u2081 = y\u2264z\n\n h\u2264t : \u2200 {n}{T : `Tree X (2 + n)} \u2192 Sorted XC T \u2192 head T \u2264X head (tail T)\n h\u2264t (dbl-lt x y x\u2081 ST) = inj\u2081 (sym x\u2081)\n h\u2264t (dbl-eq x ST) rewrite XC-refl x = inj\u2082 refl\n\n head-p : \u2200 {n}{T : `Tree X (suc n)} i \u2192 Sorted XC T \u2192 head T \u2264X `toFun T i\n head-p {T = T} zero ST rewrite XC-refl (head T) = inj\u2082 refl\n head-p {zero} (suc ()) ST\n head-p {suc n} (suc i) ST = \u2264X-trans (h\u2264t ST) (head-p i (STail ST))\n\n toFun-p : \u2200 {n}{T : `Tree X n}{i j : Fin n} \u2192 i \u2264F j \u2192 Sorted XC T \u2192 `toFun T i \u2264X `toFun T j\n toFun-p {j = j} z\u2264i ST = head-p j ST\n toFun-p (s\u2264s i\u2264Fj) ST = toFun-p i\u2264Fj (STail ST)\n\n sort-proof : \u2200 {i}{T : `Tree X i} \u2192 Sorted XC T \u2192 Is-Mono `RC XC (`toFun T)\n sort-proof {T = T} T\u2081 zero zero rewrite XC-refl (head T) = _\n sort-proof T\u2081 zero (suc y) with toFun-p (z\u2264i {i = suc y}) T\u2081\n sort-proof T zero (suc y) | inj\u2081 x rewrite x = _\n sort-proof T zero (suc y) | inj\u2082 y\u2081 rewrite y\u2081 = _\n sort-proof {T = T} T\u2081 (suc x) zero with toFun-p (z\u2264i {i = suc x}) T\u2081 | XC-flip (head T) (`toFun (tail T) x)\n sort-proof T (suc x) zero | inj\u2081 x\u2081 | l rewrite x\u2081 | sym l = _\n sort-proof T (suc x) zero | inj\u2082 y | l rewrite y | sym l = _\n sort-proof T\u2081 (suc x) (suc y) = sort-proof (STail T\u2081) x y\n\n lt-trans-RC : \u2200 {i} (x y z : Fin i) \u2192 `RC x y \u2261 lt \u2192 `RC y z \u2261 lt \u2192 `RC x z \u2261 lt\n lt-trans-RC zero zero zero x\u2261 x\u2081 y\u2081 (sym x\u2082)) lt-trans-RC flip-RC (sort-Sorted T) x y\n\n\n\n module toNat n (f : Endo (Fin (suc n)))(f-inj : Is-Inj f)(f-mono : Is-Mono `RC `RC f) where\n \n import prefect-bintree-sorting\n open prefect-bintree-sorting.MM\n open import Data.Sum\n\n move-to-RC : \u2200 {n}{x y : Fin n} \u2192 x \u2264F y \u2192 `RC x y \u2261 lt \u228e `RC x y \u2261 eq\n move-to-RC {y = zero} z\u2264i = inj\u2082 refl\n move-to-RC {y = suc y} z\u2264i = inj\u2081 refl\n move-to-RC (s\u2264s x\u2264Fy) = move-to-RC x\u2264Fy\n\n move-from-RC : \u2200 {n}(x y : Fin n) \u2192 lt \u2261 `RC x y \u228e eq \u2261 `RC x y \u2192 x \u2264F y\n move-from-RC zero zero prf = z\u2264i\n move-from-RC zero (suc y) prf = z\u2264i\n move-from-RC (suc x) zero (inj\u2081 ())\n move-from-RC (suc x) zero (inj\u2082 ())\n move-from-RC (suc x) (suc y) prf = s\u2264s (move-from-RC x y prf)\n\n proper-mono : \u2200 {x y} \u2192 x \u2264F y \u2192 f x \u2264F f y\n proper-mono {x} {y} x\u2264Fy with `RC x y | `RC (f x) (f y) | move-to-RC x\u2264Fy | f-mono x y | move-from-RC (f x) (f y)\n proper-mono x\u2264Fy | .lt | lt | inj\u2081 refl | r4 | r5 = r5 (inj\u2081 refl)\n proper-mono x\u2264Fy | .lt | eq | inj\u2081 refl | r4 | r5 = r5 (inj\u2082 refl)\n proper-mono x\u2264Fy | .lt | gt | inj\u2081 refl | () | r5\n proper-mono x\u2264Fy | .eq | lt | inj\u2082 refl | () | r5\n proper-mono x\u2264Fy | .eq | eq | inj\u2082 refl | r4 | r5 = r5 (inj\u2082 refl)\n proper-mono x\u2264Fy | .eq | gt | inj\u2082 refl | () | r5\n\n getFrom : \u2200 n \u2192 \u2115 \u2192 Fin (suc n)\n getFrom zero i = zero\n getFrom (suc n\u2081) zero = zero\n getFrom (suc n\u2081) (suc i) = suc (getFrom n\u2081 i)\n\n getInj : {n x y : \u2115} \u2192 x \u2264 n \u2192 y \u2264 n \u2192 getFrom n x \u2261 getFrom n y \u2192 x \u2261 y\n getInj z\u2264n z\u2264n prf = refl\n getInj z\u2264n (s\u2264s y\u2264n) ()\n getInj (s\u2264s x\u2264n) z\u2264n ()\n getInj (s\u2264s x\u2264n) (s\u2264s y\u2264n) prf rewrite (getInj x\u2264n y\u2264n (suc-inj prf)) = refl\n\n getMono : {n x y : \u2115} \u2192 x \u2264 y \u2192 y \u2264 n \u2192 getFrom n x \u2264F getFrom n y\n getMono z\u2264n z\u2264n = \u2264F-refl _\n getMono z\u2264n (s\u2264s y\u2264n) = z\u2264i\n getMono (s\u2264s x\u2264y) (s\u2264s y\u2264n) = s\u2264s (getMono x\u2264y y\u2264n)\n\n forget : \u2200 {n} \u2192 Fin n \u2192 \u2115\n forget zero = zero\n forget (suc i) = suc (forget i)\n\n forgetInj : \u2200 {n}{i j : Fin n} \u2192 forget i \u2261 forget j \u2192 i \u2261 j\n forgetInj {.(suc _)} {zero} {zero} prf = refl\n forgetInj {.(suc _)} {zero} {suc j} ()\n forgetInj {.(suc _)} {suc i} {zero} ()\n forgetInj {.(suc _)} {suc i} {suc j} prf rewrite forgetInj (nsuc-inj prf) = refl\n\n getForget : \u2200 {n}(i : Fin (suc n)) \u2192 getFrom n (forget i) \u2261 i\n getForget {zero} zero = refl\n getForget {zero} (suc ())\n getForget {suc n\u2081} zero = refl\n getForget {suc n\u2081} (suc i) rewrite getForget i = refl\n\n\n forget< : \u2200 {n} \u2192 (i : Fin n) \u2192 forget i < n\n forget< {zero} ()\n forget< {suc n\u2081} zero = s\u2264s z\u2264n\n forget< {suc n\u2081} (suc i) = s\u2264s (forget< i)\n\n forget-mono : \u2200 {n}{i j : Fin n} \u2192 i \u2264F j \u2192 forget i \u2264 forget j\n forget-mono z\u2264i = z\u2264n\n forget-mono (s\u2264s i\u2264F) = s\u2264s (forget-mono i\u2264F)\n\n fn : Endo \u2115\n fn = forget \u2218 f \u2218 getFrom n\n\n return : f \u2257 getFrom n \u2218 fn \u2218 forget\n return x rewrite getForget x | getForget (f x) = refl\n\n fn-monotone : Monotone (suc n) fn\n fn-monotone {x} {y} x\u2264y (s\u2264s y\u2264n) = forget-mono (proper-mono (getMono x\u2264y y\u2264n))\n\n fn-inj : IsInj (suc n) fn\n fn-inj {x}{y} (s\u2264s sx\u2264sn) (s\u2264s sy\u2264sn) prf = getInj sx\u2264sn sy\u2264sn (f-inj (getFrom n x) (getFrom n y) (forgetInj prf))\n\n fn-bounded : Bounded (suc n) fn\n fn-bounded x _ = forget< (f (getFrom n x))\n\n fn\u2257id : \u2200 x \u2192 x < (suc n) \u2192 fn x \u2261 x\n fn\u2257id = M.is-id fn fn-monotone fn-inj fn-bounded \n\n f\u2257id : f \u2257 id\n f\u2257id x rewrite return x | fn\u2257id (forget x) (forget< x) = getForget x\n\n fin-view : \u2200 {n} \u2192 (i : Fin (suc n)) \u2192 Fin-View i\n fin-view {zero} zero = max\n fin-view {zero} (suc ())\n fin-view {suc n} zero = inject _\n fin-view {suc n} (suc i) with fin-view i\n fin-view {suc n} (suc .(from\u2115 n)) | max = max\n fin-view {suc n} (suc .(inject\u2081 i)) | inject i = inject _\n\n absurd : {X : Set} \u2192 .\u22a5 \u2192 X\n absurd ()\n\n drop\u2081 : \u2200 {n} \u2192 (i : Fin (suc n)) \u2192 .(i \u2262 from\u2115 n) \u2192 Fin n\n drop\u2081 i neq with fin-view i\n drop\u2081 {n} .(from\u2115 n) neq | max = absurd (neq refl)\n drop\u2081 .(inject\u2081 i) neq | inject i = i\n\n drop\u2081\u2192inject\u2081 : \u2200 {n}(i : Fin (suc n))(j : Fin n).(p : i \u2262 from\u2115 n) \u2192 drop\u2081 i p \u2261 j \u2192 i \u2261 inject\u2081 j\n drop\u2081\u2192inject\u2081 i j p q with fin-view i\n drop\u2081\u2192inject\u2081 {n} .(from\u2115 n) j p q | max = absurd (p refl)\n drop\u2081\u2192inject\u2081 .(inject\u2081 i) j p q | inject i = cong inject\u2081 q\n\n\n `mono-inj\u2192id : \u2200{i}(f : Endo (`Rep i)) \u2192 Is-Inj f \u2192 Is-Mono `RC `RC f \u2192 f \u2257 id\n `mono-inj\u2192id {zero} = \u03bb f x x\u2081 ()\n `mono-inj\u2192id {suc i} = toNat.f\u2257id i \n\n\n interface : Interface\n interface = record \n { Ix = `Ix\n ; Rep = `Rep\n ; Syn = `Syn\n ; Tree = `Tree\n ; fromFun = `fromFun\n ; toFun = `toFun\n ; toFun\u2218fromFun = `toFun\u2218fromFun\n ; evalArg = `evalArg\n ; evalTree = `evalTree\n ; eval-proof = `eval-proof\n ; inv = `inv\n ; inv-proof = `inv-proof\n ; RC = `RC\n ; sort = `sort\n ; sort-syn = `sort-syn\n ; sort-proof = `sort-proof\n ; sort-mono = `sort-mono\n ; mono-inj\u2192id = `mono-inj\u2192id\n }\n\n open import Data.Bool\n\n count : \u2200 {n} \u2192 (Fin n \u2192 \u2115) \u2192 \u2115\n count {n} f = sum (tabulate f)\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Fin n \u2192 Bool) \u2192 \u2115\n #\u27e8 f \u27e9 = count (\u03bb x \u2192 if f x then 1 else 0)\n\n #-ext : \u2200 {n} \u2192 (f g : Fin n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-ext {zero} f g f\u2257g = refl\n #-ext {suc n} f g f\u2257g rewrite f\u2257g zero | #-ext (f \u2218 suc) (g \u2218 suc) (f\u2257g \u2218 suc) = refl\n\n com-assoc : \u2200 x y z \u2192 x + (y + z) \u2261 y + (x + z)\n com-assoc x y z rewrite \n sym (\u2115\u00b0.+-assoc x y z) |\n \u2115\u00b0.+-comm x y |\n \u2115\u00b0.+-assoc y x z = refl\n \n syn-pres : \u2200 {n}(f : Fin n \u2192 \u2115)(S : `Syn n)\n \u2192 count f \u2261 count (f \u2218 `evalArg S)\n syn-pres f `id = refl\n syn-pres f `swap = com-assoc (f zero) (f (suc zero)) (count (\u03bb i \u2192 f (suc (suc i))))\n syn-pres f (`tail S) rewrite syn-pres (f \u2218 suc) S = refl\n syn-pres f (S `\u2218 S\u2081) rewrite syn-pres f S = syn-pres (f \u2218 `evalArg S) S\u2081\n\n #-perm : \u2200 {n}(f : Fin n \u2192 Bool)(p : Endo (Fin n)) \u2192 Is-Inj p\n \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 p \u27e9\n #-perm f p p-inj = trans (syn-pres (\u03bb x \u2192 if f x then 1 else 0) (sort-bij p))\n (#-ext _ _ f\u2218eval\u2257f\u2218p)\n where \n open abs interface\n f\u2218eval\u2257f\u2218p : f \u2218 `evalArg (sort-bij p) \u2257 f \u2218 p\n f\u2218eval\u2257f\u2218p x rewrite thm p p-inj x = refl\n\n test : `Syn 8\n test = abs.sort-bij interface (\u03bb x \u2192 `evalArg (`tail `swap) x)\n","old_contents":"module bijection-fin where\n\n open import bijection\n open import Function.NP hiding (Cmp)\n open import Relation.Binary.PropositionalEquality\n\n open import Data.Empty\n open import Data.Nat.NP\n open import Data.Fin using (Fin ; zero ; suc ; from\u2115 ; inject\u2081)\n open import Data.Vec hiding ([_])\n\n data `Syn : \u2115 \u2192 Set where \n `id : \u2200 {n} \u2192 `Syn n\n `swap : \u2200 {n} \u2192 `Syn (2 + n)\n `tail : \u2200 {n} \u2192 `Syn n \u2192 `Syn (1 + n)\n _`\u2218_ : \u2200 {n} \u2192 `Syn n \u2192 `Syn n \u2192 `Syn n\n\n `Rep = Fin\n\n `Ix = \u2115\n\n `Tree : Set \u2192 `Ix \u2192 Set\n `Tree X = Vec X\n\n `fromFun : \u2200 {i X} \u2192 (`Rep i \u2192 X) \u2192 `Tree X i\n `fromFun = tabulate\n\n `toFun : \u2200 {i X} \u2192 `Tree X i \u2192 (`Rep i \u2192 X)\n `toFun T zero = head T\n `toFun T (suc i) = `toFun (tail T) i\n\n `toFun\u2218fromFun : \u2200 {i X}(f : `Rep i \u2192 X) \u2192 f \u2257 `toFun (`fromFun f)\n `toFun\u2218fromFun f zero = refl\n `toFun\u2218fromFun f (suc i) = `toFun\u2218fromFun (f \u2218 suc) i\n\n fin-swap : \u2200 {n} \u2192 Endo (Fin (2 + n))\n fin-swap zero = suc zero\n fin-swap (suc zero) = zero\n fin-swap (suc (suc i)) = suc (suc i)\n\n fin-tail : \u2200 {n} \u2192 Endo (Fin n) \u2192 Endo (Fin (1 + n))\n fin-tail f zero = zero\n fin-tail f (suc i) = suc (f i)\n\n `evalArg : \u2200 {i} \u2192 `Syn i \u2192 Endo (`Rep i)\n `evalArg `id = id\n `evalArg `swap = fin-swap\n `evalArg (`tail f) = fin-tail (`evalArg f)\n `evalArg (S `\u2218 S\u2081) = `evalArg S \u2218 `evalArg S\u2081\n\n vec-swap : \u2200 {n}{X : Set} \u2192 Endo (Vec X (2 + n))\n vec-swap xs = head (tail xs) \u2237 head xs \u2237 tail (tail xs)\n\n vec-tail : \u2200 {n}{X : Set} \u2192 Endo (Vec X n) \u2192 Endo (Vec X (1 + n))\n vec-tail f xs = head xs \u2237 f (tail xs)\n\n `evalTree : \u2200 {i X} \u2192 `Syn i \u2192 Endo (`Tree X i)\n `evalTree `id = id\n `evalTree `swap = vec-swap\n `evalTree (`tail f) = vec-tail (`evalTree f)\n `evalTree (S `\u2218 S\u2081) = `evalTree S \u2218 `evalTree S\u2081\n\n `eval-proof : \u2200 {i X} S (T : `Tree X i) \u2192 `toFun T \u2257 `toFun (`evalTree S T) \u2218 `evalArg S\n `eval-proof `id T i = refl\n `eval-proof `swap T zero = refl\n `eval-proof `swap T (suc zero) = refl\n `eval-proof `swap T (suc (suc i)) = refl\n `eval-proof (`tail S) T zero = refl\n `eval-proof (`tail S) T (suc i) = `eval-proof S (tail T) i\n `eval-proof (S `\u2218 S\u2081) T i rewrite\n `eval-proof S\u2081 T i |\n `eval-proof S (`evalTree S\u2081 T) (`evalArg S\u2081 i) = refl\n\n `inv : \u2200 {i} \u2192 Endo (`Syn i)\n `inv `id = `id\n `inv `swap = `swap\n `inv (`tail S) = `tail (`inv S)\n `inv (S `\u2218 S\u2081) = `inv S\u2081 `\u2218 `inv S\n\n `inv-proof : \u2200 {i} \u2192 (S : `Syn i) \u2192 `evalArg S \u2218 `evalArg (`inv S) \u2257 id\n `inv-proof `id x = refl\n `inv-proof `swap zero = refl\n `inv-proof `swap (suc zero) = refl\n `inv-proof `swap (suc (suc x)) = refl\n `inv-proof (`tail S) zero = refl\n `inv-proof (`tail S) (suc x) rewrite `inv-proof S x = refl\n `inv-proof (S `\u2218 S\u2081) x rewrite \n `inv-proof S\u2081 (`evalArg (`inv S) x) |\n `inv-proof S x = refl\n\n `RC : \u2200 {i} \u2192 Cmp (`Rep i)\n `RC zero zero = eq\n `RC zero (suc j) = lt\n `RC (suc i) zero = gt\n `RC (suc i) (suc j) = `RC i j\n\n insert : \u2200 {n X} \u2192 Cmp X \u2192 X \u2192 Vec X n \u2192 Vec X (1 + n)\n insert X-cmp x [] = x \u2237 []\n insert X-cmp x (x\u2081 \u2237 xs) with X-cmp x x\u2081\n insert X-cmp x (x\u2081 \u2237 xs) | lt = x \u2237 x\u2081 \u2237 xs\n insert X-cmp x (x\u2081 \u2237 xs) | eq = x \u2237 x\u2081 \u2237 xs\n insert X-cmp x (x\u2081 \u2237 xs) | gt = x\u2081 \u2237 insert X-cmp x xs\n\n `sort : \u2200 {i X} \u2192 Cmp X \u2192 Endo (`Tree X i)\n `sort X-cmp [] = []\n `sort X-cmp (x \u2237 xs) = insert X-cmp x (`sort X-cmp xs)\n\n insert-syn : \u2200 {n X} \u2192 Cmp X \u2192 X \u2192 Vec X n \u2192 `Syn (1 + n)\n insert-syn X-cmp x [] = `id\n insert-syn X-cmp x (x\u2081 \u2237 xs) with X-cmp x x\u2081\n insert-syn X-cmp x (x\u2081 \u2237 xs) | lt = `id\n insert-syn X-cmp x (x\u2081 \u2237 xs) | eq = `id\n insert-syn X-cmp x (x\u2081 \u2237 xs) | gt = `tail (insert-syn X-cmp x xs) `\u2218 `swap\n\n `sort-syn : \u2200 {i X} \u2192 Cmp X \u2192 `Tree X i \u2192 `Syn i\n `sort-syn X-cmp [] = `id\n `sort-syn X-cmp (x \u2237 xs) = insert-syn X-cmp x (`sort X-cmp xs) `\u2218 `tail (`sort-syn X-cmp xs)\n\n insert-proof : \u2200 {n X}(X-cmp : Cmp X) x (T : Vec X n) \u2192 insert X-cmp x T \u2261 `evalTree (insert-syn X-cmp x T) (x \u2237 T)\n insert-proof X-cmp x [] = refl\n insert-proof X-cmp x (x\u2081 \u2237 T) with X-cmp x x\u2081\n insert-proof X-cmp x (x\u2081 \u2237 T) | lt = refl\n insert-proof X-cmp x (x\u2081 \u2237 T) | eq = refl\n insert-proof X-cmp x (x\u2081 \u2237 T) | gt rewrite insert-proof X-cmp x T = refl\n\n `sort-proof : \u2200 {i X}(X-cmp : Cmp X)(T : `Tree X i) \u2192 `sort X-cmp T \u2261 `evalTree (`sort-syn X-cmp T) T\n `sort-proof X-cmp [] = refl\n `sort-proof X-cmp (x \u2237 T) rewrite \n sym (`sort-proof X-cmp T)= insert-proof X-cmp x (`sort X-cmp T)\n\n module Alt-Syn where\n\n data ``Syn : \u2115 \u2192 Set where\n `id : \u2200 {n} \u2192 ``Syn n\n _`\u2218_ : \u2200 {n} \u2192 ``Syn n \u2192 ``Syn n \u2192 ``Syn n\n `swap : \u2200 {n} m \u2192 ``Syn (m + 2 + n)\n\n swap-fin : \u2200 {n} m \u2192 Endo (Fin (m + 2 + n))\n swap-fin zero zero = suc zero\n swap-fin zero (suc zero) = zero\n swap-fin zero (suc (suc i)) = suc (suc i)\n swap-fin (suc m) zero = zero\n swap-fin (suc m) (suc i) = suc (swap-fin m i)\n\n ``evalArg : \u2200 {n} \u2192 ``Syn n \u2192 Endo (`Rep n)\n ``evalArg `id = id\n ``evalArg (S `\u2218 S\u2081) = ``evalArg S \u2218 ``evalArg S\u2081\n ``evalArg (`swap m) = swap-fin m\n \n ``tail : \u2200 {n} \u2192 ``Syn n \u2192 ``Syn (suc n)\n ``tail `id = `id\n ``tail (S `\u2218 S\u2081) = ``tail S `\u2218 ``tail S\u2081\n ``tail (`swap m) = `swap (suc m)\n\n _``\u2218_ : \u2200 {n} \u2192 ``Syn n \u2192 ``Syn n \u2192 ``Syn n\n `id ``\u2218 y = y\n (x `\u2218 x\u2081) ``\u2218 `id = x `\u2218 x\u2081\n (x `\u2218 x\u2081) ``\u2218 (y `\u2218 y\u2081) = x `\u2218 (x\u2081 `\u2218 (y `\u2218 y\u2081))\n (x `\u2218 x\u2081) ``\u2218 `swap m = x `\u2218 (x\u2081 ``\u2218 `swap m)\n `swap m ``\u2218 y = `swap m `\u2218 y\n\n translate : \u2200 {n} \u2192 `Syn n \u2192 ``Syn n\n translate `id = `id\n translate `swap = `swap 0\n translate (`tail S) = ``tail (translate S)\n translate (S `\u2218 S\u2081) = translate S ``\u2218 translate S\u2081\n\n ``tail-p : \u2200 {n} (S : ``Syn n) \u2192 fin-tail (``evalArg S) \u2257 ``evalArg (``tail S)\n ``tail-p `id zero = refl\n ``tail-p `id (suc x) = refl\n ``tail-p (S `\u2218 S\u2081) zero rewrite sym (``tail-p S\u2081 zero) = ``tail-p S zero\n ``tail-p (S `\u2218 S\u2081) (suc x) rewrite sym (``tail-p S\u2081 (suc x)) = ``tail-p S (suc (``evalArg S\u2081 x))\n ``tail-p (`swap m) zero = refl\n ``tail-p (`swap m) (suc x) = refl\n\n ``\u2218-p : \u2200 {n}(A B : ``Syn n) \u2192 ``evalArg (A ``\u2218 B) \u2257 ``evalArg A \u2218 ``evalArg B\n ``\u2218-p `id B x = refl\n ``\u2218-p (A `\u2218 A\u2081) `id x = refl\n ``\u2218-p (A `\u2218 A\u2081) (B `\u2218 B\u2081) x = refl\n ``\u2218-p (A `\u2218 A\u2081) (`swap m) x rewrite ``\u2218-p A\u2081 (`swap m) x = refl\n ``\u2218-p (`swap m) B x = refl\n\n `eval`` : \u2200 {n} (S : `Syn n) \u2192 `evalArg S \u2257 ``evalArg (translate S)\n `eval`` `id x = refl\n `eval`` `swap zero = refl\n `eval`` `swap (suc zero) = refl\n `eval`` `swap (suc (suc x)) = refl\n `eval`` (`tail S) zero = ``tail-p (translate S) zero\n `eval`` (`tail S) (suc x) rewrite `eval`` S x = ``tail-p (translate S) (suc x)\n `eval`` (S `\u2218 S\u2081) x rewrite ``\u2218-p (translate S) (translate S\u2081) x | sym (`eval`` S\u2081 x) | `eval`` S (`evalArg S\u2081 x) = refl\n\n\n data Fin-View : \u2200 {n} \u2192 Fin n \u2192 Set where\n max : \u2200 {n} \u2192 Fin-View (from\u2115 n)\n inject : \u2200 {n} \u2192 (i : Fin n) \u2192 Fin-View (inject\u2081 i)\n\n data _\u2264F_ : \u2200 {n} \u2192 Fin n \u2192 Fin n \u2192 Set where\n z\u2264i : {n : \u2115}{i : Fin (suc n)} \u2192 zero \u2264F i\n s\u2264s : {n : \u2115}{i j : Fin n} \u2192 i \u2264F j \u2192 suc i \u2264F suc j\n\n \u2264F-refl : \u2200 {n} (x : Fin n) \u2192 x \u2264F x\n \u2264F-refl zero = z\u2264i\n \u2264F-refl (suc i) = s\u2264s (\u2264F-refl i)\n\n _\u2261 : \u2200 {i} (x y : Fin i) \u2192 eq \u2261 `RC x y \u2192 x \u2261 y\n eq=>\u2261 zero zero p = refl\n eq=>\u2261 zero (suc y) ()\n eq=>\u2261 (suc x) zero ()\n eq=>\u2261 (suc x) (suc y) p rewrite eq=>\u2261 x y p = refl\n\n insert-Sorted : \u2200 {n l}{V : Vec (Fin n) l}(x : Fin n) \u2192 Sorted {Fin n} `RC V \u2192 Sorted {Fin n} `RC (insert `RC x V)\n insert-Sorted x [] = sing x\n insert-Sorted x (sing x\u2081) with `RC x x\u2081 | dbl-lt {XC = `RC} x x\u2081 {[]} | eq=>\u2261 x x\u2081 | flip-RC x x\u2081\n insert-Sorted x (sing x\u2081) | lt | b | _ | _ = b refl (sing x\u2081)\n insert-Sorted x (sing x\u2081) | eq | _ | p | _ rewrite p refl = dbl-eq x\u2081 (sing x\u2081)\n insert-Sorted x (sing x\u2081) | gt | b | _ | l = dbl-lt x\u2081 x l (sing x)\n insert-Sorted x (dbl-lt y y' {xs} prf xs\u2081) with `RC x y | dbl-lt {XC = `RC} x y {y' \u2237 xs} | eq=>\u2261 x y | flip-RC x y\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | lt | b | p | l\u2081 = b refl (dbl-lt y y' prf xs\u2081)\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | eq | b | p | l\u2081 rewrite p refl = dbl-eq y (dbl-lt y y' prf xs\u2081)\n insert-Sorted x (dbl-lt y y' {xs} prf xs\u2081) | gt | b | p | l\u2081 with `RC x y' | insert-Sorted x xs\u2081\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | gt | b | p | l\u2081 | lt | xs' = dbl-lt y x l\u2081 xs'\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | gt | b | p | l\u2081 | eq | xs' = dbl-lt y x l\u2081 xs'\n insert-Sorted x (dbl-lt y y' prf xs\u2081) | gt | b | p | l\u2081 | gt | xs' = dbl-lt y y' prf xs'\n insert-Sorted x (dbl-eq y {xs} xs\u2081) with `RC x y | inspect (`RC x) y | dbl-lt {XC = `RC} x y {y \u2237 xs} | eq=>\u2261 x y | flip-RC x y | insert-Sorted x xs\u2081\n insert-Sorted x (dbl-eq y xs\u2081) | lt | _ | b | p | l | _ = b refl (dbl-eq y xs\u2081)\n insert-Sorted x (dbl-eq y xs\u2081) | eq | _ | b | p | l | _ rewrite p refl = dbl-eq y (dbl-eq y xs\u2081)\n insert-Sorted x (dbl-eq y xs\u2081) | gt | [ prf ] | b | p | l | ss rewrite prf = dbl-eq y ss \n\n sort-Sorted : \u2200 {n l}(V : Vec (Fin n) l) \u2192 Sorted `RC (`sort `RC V)\n sort-Sorted [] = []\n sort-Sorted (x \u2237 V) = insert-Sorted x (sort-Sorted V)\n\n RC-refl : \u2200 {i}(x : Fin i) \u2192 `RC x x \u2261 eq\n RC-refl zero = refl\n RC-refl (suc x) = RC-refl x\n\n STail : \u2200 {X l}{XC : Cmp X}{xs : Vec X (suc l)} \u2192 Sorted XC xs \u2192 Sorted XC (tail xs)\n STail (sing x) = []\n STail (dbl-lt x y x\u2081 T) = T\n STail (dbl-eq x T) = T\n\n module sproof {X}(XC : Cmp X)(XC-refl : \u2200 x \u2192 XC x x \u2261 eq)\n (eq\u2261 : \u2200 x y \u2192 XC x y \u2261 eq \u2192 x \u2261 y)\n (lt-trans : \u2200 x y z \u2192 XC x y \u2261 lt \u2192 XC y z \u2261 lt \u2192 XC x z \u2261 lt)\n (XC-flip : \u2200 x y \u2192 opposite (XC x y) \u2261 XC y x)\n where\n\n open import Data.Sum\n\n _\u2264X_ : X \u2192 X \u2192 Set\n x \u2264X y = XC x y \u2261 lt \u228e XC x y \u2261 eq\n\n \u2264X-trans : \u2200 {x y z} \u2192 x \u2264X y \u2192 y \u2264X z \u2192 x \u2264X z\n \u2264X-trans (inj\u2081 x\u2081) (inj\u2081 x\u2082) = inj\u2081 (lt-trans _ _ _ x\u2081 x\u2082)\n \u2264X-trans {_}{y}{z}(inj\u2081 x\u2081) (inj\u2082 y\u2081) rewrite eq\u2261 y z y\u2081 = inj\u2081 x\u2081\n \u2264X-trans {x}{y} (inj\u2082 y\u2081) y\u2264z rewrite eq\u2261 x y y\u2081 = y\u2264z\n\n h\u2264t : \u2200 {n}{T : `Tree X (2 + n)} \u2192 Sorted XC T \u2192 head T \u2264X head (tail T)\n h\u2264t (dbl-lt x y x\u2081 ST) = inj\u2081 (sym x\u2081)\n h\u2264t (dbl-eq x ST) rewrite XC-refl x = inj\u2082 refl\n\n head-p : \u2200 {n}{T : `Tree X (suc n)} i \u2192 Sorted XC T \u2192 head T \u2264X `toFun T i\n head-p {T = T} zero ST rewrite XC-refl (head T) = inj\u2082 refl\n head-p {zero} (suc ()) ST\n head-p {suc n} (suc i) ST = \u2264X-trans (h\u2264t ST) (head-p i (STail ST))\n\n toFun-p : \u2200 {n}{T : `Tree X n}{i j : Fin n} \u2192 i \u2264F j \u2192 Sorted XC T \u2192 `toFun T i \u2264X `toFun T j\n toFun-p {j = j} z\u2264i ST = head-p j ST\n toFun-p (s\u2264s i\u2264Fj) ST = toFun-p i\u2264Fj (STail ST)\n\n sort-proof : \u2200 {i}{T : `Tree X i} \u2192 Sorted XC T \u2192 Is-Mono `RC XC (`toFun T)\n sort-proof {T = T} T\u2081 zero zero rewrite XC-refl (head T) = _\n sort-proof T\u2081 zero (suc y) with toFun-p (z\u2264i {i = suc y}) T\u2081\n sort-proof T zero (suc y) | inj\u2081 x rewrite x = _\n sort-proof T zero (suc y) | inj\u2082 y\u2081 rewrite y\u2081 = _\n sort-proof {T = T} T\u2081 (suc x) zero with toFun-p (z\u2264i {i = suc x}) T\u2081 | XC-flip (head T) (`toFun (tail T) x)\n sort-proof T (suc x) zero | inj\u2081 x\u2081 | l rewrite x\u2081 | sym l = _\n sort-proof T (suc x) zero | inj\u2082 y | l rewrite y | sym l = _\n sort-proof T\u2081 (suc x) (suc y) = sort-proof (STail T\u2081) x y\n\n lt-trans-RC : \u2200 {i} (x y z : Fin i) \u2192 `RC x y \u2261 lt \u2192 `RC y z \u2261 lt \u2192 `RC x z \u2261 lt\n lt-trans-RC zero zero zero x\u2261 x\u2081 y\u2081 (sym x\u2082)) lt-trans-RC flip-RC (sort-Sorted T) x y\n\n\n\n module toNat n (f : Endo (Fin (suc n)))(f-inj : Is-Inj f)(f-mono : Is-Mono `RC `RC f) where\n \n import prefect-bintree-sorting\n open prefect-bintree-sorting.MM\n open import Data.Sum\n\n move-to-RC : \u2200 {n}{x y : Fin n} \u2192 x \u2264F y \u2192 `RC x y \u2261 lt \u228e `RC x y \u2261 eq\n move-to-RC {y = zero} z\u2264i = inj\u2082 refl\n move-to-RC {y = suc y} z\u2264i = inj\u2081 refl\n move-to-RC (s\u2264s x\u2264Fy) = move-to-RC x\u2264Fy\n\n move-from-RC : \u2200 {n}(x y : Fin n) \u2192 lt \u2261 `RC x y \u228e eq \u2261 `RC x y \u2192 x \u2264F y\n move-from-RC zero zero prf = z\u2264i\n move-from-RC zero (suc y) prf = z\u2264i\n move-from-RC (suc x) zero (inj\u2081 ())\n move-from-RC (suc x) zero (inj\u2082 ())\n move-from-RC (suc x) (suc y) prf = s\u2264s (move-from-RC x y prf)\n\n proper-mono : \u2200 {x y} \u2192 x \u2264F y \u2192 f x \u2264F f y\n proper-mono {x} {y} x\u2264Fy with `RC x y | `RC (f x) (f y) | move-to-RC x\u2264Fy | f-mono x y | move-from-RC (f x) (f y)\n proper-mono x\u2264Fy | .lt | lt | inj\u2081 refl | r4 | r5 = r5 (inj\u2081 refl)\n proper-mono x\u2264Fy | .lt | eq | inj\u2081 refl | r4 | r5 = r5 (inj\u2082 refl)\n proper-mono x\u2264Fy | .lt | gt | inj\u2081 refl | () | r5\n proper-mono x\u2264Fy | .eq | lt | inj\u2082 refl | () | r5\n proper-mono x\u2264Fy | .eq | eq | inj\u2082 refl | r4 | r5 = r5 (inj\u2082 refl)\n proper-mono x\u2264Fy | .eq | gt | inj\u2082 refl | () | r5\n\n getFrom : \u2200 n \u2192 \u2115 \u2192 Fin (suc n)\n getFrom zero i = zero\n getFrom (suc n\u2081) zero = zero\n getFrom (suc n\u2081) (suc i) = suc (getFrom n\u2081 i)\n\n getInj : {n x y : \u2115} \u2192 x \u2264 n \u2192 y \u2264 n \u2192 getFrom n x \u2261 getFrom n y \u2192 x \u2261 y\n getInj z\u2264n z\u2264n prf = refl\n getInj z\u2264n (s\u2264s y\u2264n) ()\n getInj (s\u2264s x\u2264n) z\u2264n ()\n getInj (s\u2264s x\u2264n) (s\u2264s y\u2264n) prf rewrite (getInj x\u2264n y\u2264n (suc-inj prf)) = refl\n\n getMono : {n x y : \u2115} \u2192 x \u2264 y \u2192 y \u2264 n \u2192 getFrom n x \u2264F getFrom n y\n getMono z\u2264n z\u2264n = \u2264F-refl _\n getMono z\u2264n (s\u2264s y\u2264n) = z\u2264i\n getMono (s\u2264s x\u2264y) (s\u2264s y\u2264n) = s\u2264s (getMono x\u2264y y\u2264n)\n\n forget : \u2200 {n} \u2192 Fin n \u2192 \u2115\n forget zero = zero\n forget (suc i) = suc (forget i)\n\n forgetInj : \u2200 {n}{i j : Fin n} \u2192 forget i \u2261 forget j \u2192 i \u2261 j\n forgetInj {.(suc _)} {zero} {zero} prf = refl\n forgetInj {.(suc _)} {zero} {suc j} ()\n forgetInj {.(suc _)} {suc i} {zero} ()\n forgetInj {.(suc _)} {suc i} {suc j} prf rewrite forgetInj (nsuc-inj prf) = refl\n\n getForget : \u2200 {n}(i : Fin (suc n)) \u2192 getFrom n (forget i) \u2261 i\n getForget {zero} zero = refl\n getForget {zero} (suc ())\n getForget {suc n\u2081} zero = refl\n getForget {suc n\u2081} (suc i) rewrite getForget i = refl\n\n\n forget< : \u2200 {n} \u2192 (i : Fin n) \u2192 forget i < n\n forget< {zero} ()\n forget< {suc n\u2081} zero = s\u2264s z\u2264n\n forget< {suc n\u2081} (suc i) = s\u2264s (forget< i)\n\n forget-mono : \u2200 {n}{i j : Fin n} \u2192 i \u2264F j \u2192 forget i \u2264 forget j\n forget-mono z\u2264i = z\u2264n\n forget-mono (s\u2264s i\u2264F) = s\u2264s (forget-mono i\u2264F)\n\n fn : Endo \u2115\n fn = forget \u2218 f \u2218 getFrom n\n\n return : f \u2257 getFrom n \u2218 fn \u2218 forget\n return x rewrite getForget x | getForget (f x) = refl\n\n fn-monotone : Monotone (suc n) fn\n fn-monotone {x} {y} x\u2264y (s\u2264s y\u2264n) = forget-mono (proper-mono (getMono x\u2264y y\u2264n))\n\n fn-inj : IsInj (suc n) fn\n fn-inj {x}{y} (s\u2264s sx\u2264sn) (s\u2264s sy\u2264sn) prf = getInj sx\u2264sn sy\u2264sn (f-inj (getFrom n x) (getFrom n y) (forgetInj prf))\n\n fn-bounded : Bounded (suc n) fn\n fn-bounded x _ = forget< (f (getFrom n x))\n\n fn\u2257id : \u2200 x \u2192 x < (suc n) \u2192 fn x \u2261 x\n fn\u2257id = M.is-id fn fn-monotone fn-inj fn-bounded \n\n f\u2257id : f \u2257 id\n f\u2257id x rewrite return x | fn\u2257id (forget x) (forget< x) = getForget x\n\n fin-view : \u2200 {n} \u2192 (i : Fin (suc n)) \u2192 Fin-View i\n fin-view {zero} zero = max\n fin-view {zero} (suc ())\n fin-view {suc n} zero = inject _\n fin-view {suc n} (suc i) with fin-view i\n fin-view {suc n} (suc .(from\u2115 n)) | max = max\n fin-view {suc n} (suc .(inject\u2081 i)) | inject i = inject _\n\n absurd : {X : Set} \u2192 .\u22a5 \u2192 X\n absurd ()\n\n drop\u2081 : \u2200 {n} \u2192 (i : Fin (suc n)) \u2192 .(i \u2262 from\u2115 n) \u2192 Fin n\n drop\u2081 i neq with fin-view i\n drop\u2081 {n} .(from\u2115 n) neq | max = absurd (neq refl)\n drop\u2081 .(inject\u2081 i) neq | inject i = i\n\n drop\u2081\u2192inject\u2081 : \u2200 {n}(i : Fin (suc n))(j : Fin n).(p : i \u2262 from\u2115 n) \u2192 drop\u2081 i p \u2261 j \u2192 i \u2261 inject\u2081 j\n drop\u2081\u2192inject\u2081 i j p q with fin-view i\n drop\u2081\u2192inject\u2081 {n} .(from\u2115 n) j p q | max = absurd (p refl)\n drop\u2081\u2192inject\u2081 .(inject\u2081 i) j p q | inject i = cong inject\u2081 q\n\n\n `mono-inj\u2192id : \u2200{i}(f : Endo (`Rep i)) \u2192 Is-Inj f \u2192 Is-Mono `RC `RC f \u2192 f \u2257 id\n `mono-inj\u2192id {zero} = \u03bb f x x\u2081 ()\n `mono-inj\u2192id {suc i} = toNat.f\u2257id i \n\n\n interface : Interface\n interface = record \n { Ix = `Ix\n ; Rep = `Rep\n ; Syn = `Syn\n ; Tree = `Tree\n ; fromFun = `fromFun\n ; toFun = `toFun\n ; toFun\u2218fromFun = `toFun\u2218fromFun\n ; evalArg = `evalArg\n ; evalTree = `evalTree\n ; eval-proof = `eval-proof\n ; inv = `inv\n ; inv-proof = `inv-proof\n ; RC = `RC\n ; sort = `sort\n ; sort-syn = `sort-syn\n ; sort-proof = `sort-proof\n ; sort-mono = `sort-mono\n ; mono-inj\u2192id = `mono-inj\u2192id\n }\n\n open import Data.Bool\n\n count : \u2200 {n} \u2192 (Fin n \u2192 \u2115) \u2192 \u2115\n count {n} f = sum (tabulate f)\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Fin n \u2192 Bool) \u2192 \u2115\n #\u27e8 f \u27e9 = count (\u03bb x \u2192 if f x then 1 else 0)\n\n #-ext : \u2200 {n} \u2192 (f g : Fin n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-ext {zero} f g f\u2257g = refl\n #-ext {suc n} f g f\u2257g rewrite f\u2257g zero | #-ext (f \u2218 suc) (g \u2218 suc) (f\u2257g \u2218 suc) = refl\n\n com-assoc : \u2200 x y z \u2192 x + (y + z) \u2261 y + (x + z)\n com-assoc x y z rewrite \n sym (\u2115\u00b0.+-assoc x y z) |\n \u2115\u00b0.+-comm x y |\n \u2115\u00b0.+-assoc y x z = refl\n \n syn-pres : \u2200 {n}(f : Fin n \u2192 \u2115)(S : `Syn n)\n \u2192 count f \u2261 count (f \u2218 `evalArg S)\n syn-pres f `id = refl\n syn-pres f `swap = com-assoc (f zero) (f (suc zero)) (count (\u03bb i \u2192 f (suc (suc i))))\n syn-pres f (`tail S) rewrite syn-pres (f \u2218 suc) S = refl\n syn-pres f (S `\u2218 S\u2081) rewrite syn-pres f S = syn-pres (f \u2218 `evalArg S) S\u2081\n\n #-perm : \u2200 {n}(f : Fin n \u2192 Bool)(p : Endo (Fin n)) \u2192 Is-Inj p\n \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 p \u27e9\n #-perm f p p-inj = trans (syn-pres (\u03bb x \u2192 if f x then 1 else 0) (sort-bij p))\n (#-ext _ _ f\u2218eval\u2257f\u2218p)\n where \n open abs interface\n f\u2218eval\u2257f\u2218p : f \u2218 `evalArg (sort-bij p) \u2257 f \u2218 p\n f\u2218eval\u2257f\u2218p x rewrite thm p p-inj x = refl\n\n test : `Syn 8\n test = abs.sort-bij interface (\u03bb x \u2192 `evalArg (`tail `swap) x)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"5c27a957c2f9418fc4c228d5ab37e5d074265b89","subject":"So Bool","message":"So Bool\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bool\/NP.agda","new_file":"lib\/Data\/Bool\/NP.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Bool.NP where\n\nopen import Data.Bool using (Bool; true; false; T; if_then_else_; not)\nimport Algebra\nimport Data.Bool.Properties as B\nopen import Data.Unit using (\u22a4)\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Nat using (\u2115; _\u2264_; z\u2264n; s\u2264s)\nopen import Function\nopen import Relation.Binary.NP\nopen import Relation.Binary.Logical\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\nimport Relation.Binary.PropositionalEquality as \u2261\nimport Function.Equivalence as E\nopen E.Equivalence using (to; from)\nopen import Function.Equality using (_\u27e8$\u27e9_)\nopen \u2261 using (_\u2261_)\n\ncond : \u2200 {a} {A : Set a} \u2192 A \u2192 A \u2192 Bool \u2192 A\ncond x y b = if b then x else y\n\nmodule Xor\u00b0 = Algebra.CommutativeRing B.commutativeRing-xor-\u2227\nmodule Bool\u00b0 = Algebra.CommutativeSemiring B.commutativeSemiring-\u2227-\u2228\n\ncheck : \u2200 b \u2192 {pf : T b} \u2192 \u22a4\ncheck = _\n\nIf_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 (T b \u2192 A) \u2192 (T (not b) \u2192 B) \u2192 if b then A else B\nIf_then_else_ true x _ = x _\nIf_then_else_ false _ x = x _\n\nIf\u2032_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 A \u2192 B \u2192 if b then A else B\nIf\u2032_then_else_ true x _ = x\nIf\u2032_then_else_ false _ x = x\n\nIf-map : \u2200 {A B C D : Set} b (f : T b \u2192 A \u2192 C) (g : T (not b) \u2192 B \u2192 D) \u2192\n if b then A else B \u2192 if b then C else D\nIf-map true f _ = f _\nIf-map false _ f = f _\n\nIf-elim : \u2200 {A B : Set} {P : Bool \u2192 Set}\n b (f : T b \u2192 A \u2192 P true) (g : T (not b) \u2192 B \u2192 P false) \u2192 if b then A else B \u2192 P b\nIf-elim true f _ = f _\nIf-elim false _ f = f _\n\nIf-true : \u2200 {A B : Set} {b} \u2192 T b \u2192 (if b then A else B) \u2261 A\nIf-true {b = true} _ = \u2261.refl\nIf-true {b = false} ()\n\nIf-false : \u2200 {A B : Set} {b} \u2192 T (not b) \u2192 (if b then A else B) \u2261 B\nIf-false {b = true} ()\nIf-false {b = false} _ = \u2261.refl\n\ncong-if : \u2200 {A B : Set} b {t\u2080 t\u2081} (f : A \u2192 B) \u2192 (if b then f t\u2080 else f t\u2081) \u2261 f (if b then t\u2080 else t\u2081)\ncong-if true _ = \u2261.refl\ncong-if false _ = \u2261.refl\n\nif-not : \u2200 {a} {A : Set a} b {t\u2080 t\u2081 : A} \u2192 (if b then t\u2080 else t\u2081) \u2261 (if not b then t\u2081 else t\u2080)\nif-not true = \u2261.refl\nif-not false = \u2261.refl\n\ndata \u27e6Bool\u27e7 : (b\u2081 b\u2082 : Bool) \u2192 Set where\n \u27e6true\u27e7 : \u27e6Bool\u27e7 true true\n \u27e6false\u27e7 : \u27e6Bool\u27e7 false false\n\nprivate\n module \u27e6Bool\u27e7-Internals where\n refl : Reflexive \u27e6Bool\u27e7\n refl {true} = \u27e6true\u27e7\n refl {false} = \u27e6false\u27e7\n\n sym : Symmetric \u27e6Bool\u27e7\n sym \u27e6true\u27e7 = \u27e6true\u27e7\n sym \u27e6false\u27e7 = \u27e6false\u27e7\n\n trans : Transitive \u27e6Bool\u27e7\n trans \u27e6true\u27e7 = id\n trans \u27e6false\u27e7 = id\n\n subst : \u2200 {\u2113} \u2192 Substitutive \u27e6Bool\u27e7 \u2113\n subst _ \u27e6true\u27e7 = id\n subst _ \u27e6false\u27e7 = id\n\n _\u225f_ : Decidable \u27e6Bool\u27e7\n true \u225f true = yes \u27e6true\u27e7\n false \u225f false = yes \u27e6false\u27e7\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence \u27e6Bool\u27e7\n isEquivalence = record { refl = refl; sym = sym; trans = trans }\n\n isDecEquivalence : IsDecEquivalence \u27e6Bool\u27e7\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isDecEquivalence = isDecEquivalence }\n\n equality : Equality \u27e6Bool\u27e7\n equality = record { isEquivalence = isEquivalence; subst = subst }\n\nmodule \u27e6Bool\u27e7-Props where\n open \u27e6Bool\u27e7-Internals public using (subst; decSetoid; equality)\n open DecSetoid decSetoid public\n open Equality equality public hiding (subst; isEquivalence; refl; reflexive; sym; trans)\n\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6Bool\u27e7 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63) if_then_else_ if_then_else_\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 : \u2200 {\u2113\u2081 \u2113\u2082 \u2113\u1d63} \u2192\n (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 B\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7\n \u27e8 b\u1d63 \u2236 \u27e6Bool\u27e7 \u27e9\u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 \u27e6if\u27e8 \u27e6Set\u27e7 _ \u27e9 b\u1d63 then A\u1d63 else B\u1d63 \u27e7)\n If\u2032_then_else_ If\u2032_then_else_\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n_==_ : (x y : Bool) \u2192 Bool\ntrue == true = true\ntrue == false = false\nfalse == true = false\nfalse == false = true\n\nmodule == where\n _\u2248_ : (x y : Bool) \u2192 Set\n x \u2248 y = T (x == y)\n\n refl : Reflexive _\u2248_\n refl {true} = _\n refl {false} = _\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {true} {true} _ = id\n subst _ {false} {false} _ = id\n subst _ {true} {false} ()\n subst _ {false} {true} ()\n\n sym : Symmetric _\u2248_\n sym {x} {y} eq = subst (\u03bb y \u2192 y \u2248 x) {x} {y} eq (refl {x})\n\n trans : Transitive _\u2248_\n trans {x} {y} {z} x\u2248y y\u2248z = subst (_\u2248_ x) {y} {z} y\u2248z x\u2248y\n\n _\u225f_ : Decidable _\u2248_\n true \u225f true = yes _\n false \u225f false = yes _\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence _\u2248_\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n\n isDecEquivalence : IsDecEquivalence _\u2248_\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = _\u2248_ ; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = _\u2248_; isDecEquivalence = isDecEquivalence }\n\nmodule \u27e6Bool\u27e7-Reasoning = Setoid-Reasoning \u27e6Bool\u27e7-Props.setoid\n\nopen Data.Bool public\n\n\u27e6true\u27e7\u2032 : \u2200 {b} \u2192 T b \u2192 \u27e6Bool\u27e7 true b\n\u27e6true\u27e7\u2032 {true} _ = \u27e6true\u27e7\n\u27e6true\u27e7\u2032 {false} ()\n\n\u27e6false\u27e7\u2032 : \u2200 {b} \u2192 T (not b) \u2192 \u27e6Bool\u27e7 false b\n\u27e6false\u27e7\u2032 {true} ()\n\u27e6false\u27e7\u2032 {false} _ = \u27e6false\u27e7\n\n\u2261\u2192T : \u2200 {b} \u2192 b \u2261 true \u2192 T b\n\u2261\u2192T \u2261.refl = _\n\n\u2261\u2192Tnot : \u2200 {b} \u2192 b \u2261 false \u2192 T (not b)\n\u2261\u2192Tnot \u2261.refl = _\n\nT\u2192\u2261 : \u2200 {b} \u2192 T b \u2192 b \u2261 true\nT\u2192\u2261 {true} _ = \u2261.refl\nT\u2192\u2261 {false} ()\n\nTnot\u2192\u2261 : \u2200 {b} \u2192 T (not b) \u2192 b \u2261 false\nTnot\u2192\u2261 {false} _ = \u2261.refl\nTnot\u2192\u2261 {true} ()\n\nT\u2227 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T b\u2082 \u2192 T (b\u2081 \u2227 b\u2082)\nT\u2227 p q = _\u27e8$\u27e9_ (from B.T-\u2227) (p , q)\n\nT\u2227\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2081\nT\u2227\u2081 = proj\u2081 \u2218 _\u27e8$\u27e9_ (to B.T-\u2227)\n\nT\u2227\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2082\nT\u2227\u2082 {b\u2081} = proj\u2082 \u2218 _\u27e8$\u27e9_ (to (B.T-\u2227 {b\u2081}))\n\nT\u2228'\u228e : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2228 b\u2082) \u2192 T b\u2081 \u228e T b\u2082\nT\u2228'\u228e {b\u2081} = _\u27e8$\u27e9_ (to (B.T-\u2228 {b\u2081}))\n\nT\u2228\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2081 = _\u27e8$\u27e9_ (from B.T-\u2228) \u2218 inj\u2081\n\nT\u2228\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2082 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2082 {b\u2081} = _\u27e8$\u27e9_ (from (B.T-\u2228 {b\u2081})) \u2218 inj\u2082\n\nT'not'\u00ac : \u2200 {b} \u2192 T (not b) \u2192 \u00ac (T b)\nT'not'\u00ac {false} _ = \u03bb()\nT'not'\u00ac {true} ()\n\nT'\u00ac'not : \u2200 {b} \u2192 \u00ac (T b) \u2192 T (not b)\nT'\u00ac'not {true} f = f _\nT'\u00ac'not {false} _ = _\n\n\u2227\u21d2\u2228 : \u2200 x y \u2192 T (x \u2227 y) \u2192 T (x \u2228 y)\n\u2227\u21d2\u2228 true y = _\n\u2227\u21d2\u2228 false y = \u03bb ()\n\nTdec : \u2200 b \u2192 Dec (T b)\nTdec true = yes _\nTdec false = no \u03bb()\n\nde-morgan : \u2200 x y \u2192 not (x \u2228 y) \u2261 not x \u2227 not y\nde-morgan true _ = \u2261.refl\nde-morgan false _ = \u2261.refl\n\n-- false is 0 and true is 1\nto\u2115 : Bool \u2192 \u2115\nto\u2115 b = if b then 1 else 0\n\nto\u2115\u22641 : \u2200 b \u2192 to\u2115 b \u2264 1\nto\u2115\u22641 true = s\u2264s z\u2264n\nto\u2115\u22641 false = z\u2264n\n\nxor-not-not : \u2200 x y \u2192 (not x) xor (not y) \u2261 x xor y\nxor-not-not true y = \u2261.refl\nxor-not-not false y = B.not-involutive y\n\nnot-inj : \u2200 {x y} \u2192 not x \u2261 not y \u2192 x \u2261 y\nnot-inj {true} {true} _ = \u2261.refl\nnot-inj {true} {false} ()\nnot-inj {false} {true} ()\nnot-inj {false} {false} _ = \u2261.refl\n\nxor-inj\u2081 : \u2200 x {y z} \u2192 x xor y \u2261 x xor z \u2192 y \u2261 z\nxor-inj\u2081 true = not-inj\nxor-inj\u2081 false = id\n\nxor-inj\u2082 : \u2200 x {y z} \u2192 y xor x \u2261 z xor x \u2192 y \u2261 z\nxor-inj\u2082 x {y} {z} rewrite Xor\u00b0.+-comm y x | Xor\u00b0.+-comm z x = xor-inj\u2081 x\n\ndata So : Bool \u2192 Set where\n oh! : So true\n\nSo\u2192T : \u2200 {b} \u2192 So b \u2192 T b\nSo\u2192T oh! = _\n\nT\u2192So : \u2200 {b} \u2192 T b \u2192 So b\nT\u2192So {true} _ = oh!\nT\u2192So {false} ()\n\nSo\u2192\u2261 : \u2200 {b} \u2192 So b \u2192 b \u2261 true\nSo\u2192\u2261 oh! = \u2261.refl\n\n\u2261\u2192So : \u2200 {b} \u2192 b \u2261 true \u2192 So b\n\u2261\u2192So \u2261.refl = oh!\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Bool.NP where\n\nopen import Data.Bool using (Bool; true; false; T; if_then_else_; not)\nimport Algebra\nimport Data.Bool.Properties as B\nopen import Data.Unit using (\u22a4)\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Nat using (\u2115; _\u2264_; z\u2264n; s\u2264s)\nopen import Function\nopen import Relation.Binary.NP\nopen import Relation.Binary.Logical\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\nimport Relation.Binary.PropositionalEquality as \u2261\nimport Function.Equivalence as E\nopen E.Equivalence using (to; from)\nopen import Function.Equality using (_\u27e8$\u27e9_)\nopen \u2261 using (_\u2261_)\n\ncond : \u2200 {a} {A : Set a} \u2192 A \u2192 A \u2192 Bool \u2192 A\ncond x y b = if b then x else y\n\nmodule Xor\u00b0 = Algebra.CommutativeRing B.commutativeRing-xor-\u2227\nmodule Bool\u00b0 = Algebra.CommutativeSemiring B.commutativeSemiring-\u2227-\u2228\n\ncheck : \u2200 b \u2192 {pf : T b} \u2192 \u22a4\ncheck = _\n\nIf_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 (T b \u2192 A) \u2192 (T (not b) \u2192 B) \u2192 if b then A else B\nIf_then_else_ true x _ = x _\nIf_then_else_ false _ x = x _\n\nIf\u2032_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 A \u2192 B \u2192 if b then A else B\nIf\u2032_then_else_ true x _ = x\nIf\u2032_then_else_ false _ x = x\n\nIf-map : \u2200 {A B C D : Set} b (f : T b \u2192 A \u2192 C) (g : T (not b) \u2192 B \u2192 D) \u2192\n if b then A else B \u2192 if b then C else D\nIf-map true f _ = f _\nIf-map false _ f = f _\n\nIf-elim : \u2200 {A B : Set} {P : Bool \u2192 Set}\n b (f : T b \u2192 A \u2192 P true) (g : T (not b) \u2192 B \u2192 P false) \u2192 if b then A else B \u2192 P b\nIf-elim true f _ = f _\nIf-elim false _ f = f _\n\nIf-true : \u2200 {A B : Set} {b} \u2192 T b \u2192 (if b then A else B) \u2261 A\nIf-true {b = true} _ = \u2261.refl\nIf-true {b = false} ()\n\nIf-false : \u2200 {A B : Set} {b} \u2192 T (not b) \u2192 (if b then A else B) \u2261 B\nIf-false {b = true} ()\nIf-false {b = false} _ = \u2261.refl\n\ncong-if : \u2200 {A B : Set} b {t\u2080 t\u2081} (f : A \u2192 B) \u2192 (if b then f t\u2080 else f t\u2081) \u2261 f (if b then t\u2080 else t\u2081)\ncong-if true _ = \u2261.refl\ncong-if false _ = \u2261.refl\n\nif-not : \u2200 {a} {A : Set a} b {t\u2080 t\u2081 : A} \u2192 (if b then t\u2080 else t\u2081) \u2261 (if not b then t\u2081 else t\u2080)\nif-not true = \u2261.refl\nif-not false = \u2261.refl\n\ndata \u27e6Bool\u27e7 : (b\u2081 b\u2082 : Bool) \u2192 Set where\n \u27e6true\u27e7 : \u27e6Bool\u27e7 true true\n \u27e6false\u27e7 : \u27e6Bool\u27e7 false false\n\nprivate\n module \u27e6Bool\u27e7-Internals where\n refl : Reflexive \u27e6Bool\u27e7\n refl {true} = \u27e6true\u27e7\n refl {false} = \u27e6false\u27e7\n\n sym : Symmetric \u27e6Bool\u27e7\n sym \u27e6true\u27e7 = \u27e6true\u27e7\n sym \u27e6false\u27e7 = \u27e6false\u27e7\n\n trans : Transitive \u27e6Bool\u27e7\n trans \u27e6true\u27e7 = id\n trans \u27e6false\u27e7 = id\n\n subst : \u2200 {\u2113} \u2192 Substitutive \u27e6Bool\u27e7 \u2113\n subst _ \u27e6true\u27e7 = id\n subst _ \u27e6false\u27e7 = id\n\n _\u225f_ : Decidable \u27e6Bool\u27e7\n true \u225f true = yes \u27e6true\u27e7\n false \u225f false = yes \u27e6false\u27e7\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence \u27e6Bool\u27e7\n isEquivalence = record { refl = refl; sym = sym; trans = trans }\n\n isDecEquivalence : IsDecEquivalence \u27e6Bool\u27e7\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isDecEquivalence = isDecEquivalence }\n\n equality : Equality \u27e6Bool\u27e7\n equality = record { isEquivalence = isEquivalence; subst = subst }\n\nmodule \u27e6Bool\u27e7-Props where\n open \u27e6Bool\u27e7-Internals public using (subst; decSetoid; equality)\n open DecSetoid decSetoid public\n open Equality equality public hiding (subst; isEquivalence; refl; reflexive; sym; trans)\n\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6Bool\u27e7 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63) if_then_else_ if_then_else_\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 : \u2200 {\u2113\u2081 \u2113\u2082 \u2113\u1d63} \u2192\n (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 B\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7\n \u27e8 b\u1d63 \u2236 \u27e6Bool\u27e7 \u27e9\u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 \u27e6if\u27e8 \u27e6Set\u27e7 _ \u27e9 b\u1d63 then A\u1d63 else B\u1d63 \u27e7)\n If\u2032_then_else_ If\u2032_then_else_\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n_==_ : (x y : Bool) \u2192 Bool\ntrue == true = true\ntrue == false = false\nfalse == true = false\nfalse == false = true\n\nmodule == where\n _\u2248_ : (x y : Bool) \u2192 Set\n x \u2248 y = T (x == y)\n\n refl : Reflexive _\u2248_\n refl {true} = _\n refl {false} = _\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {true} {true} _ = id\n subst _ {false} {false} _ = id\n subst _ {true} {false} ()\n subst _ {false} {true} ()\n\n sym : Symmetric _\u2248_\n sym {x} {y} eq = subst (\u03bb y \u2192 y \u2248 x) {x} {y} eq (refl {x})\n\n trans : Transitive _\u2248_\n trans {x} {y} {z} x\u2248y y\u2248z = subst (_\u2248_ x) {y} {z} y\u2248z x\u2248y\n\n _\u225f_ : Decidable _\u2248_\n true \u225f true = yes _\n false \u225f false = yes _\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence _\u2248_\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n\n isDecEquivalence : IsDecEquivalence _\u2248_\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = _\u2248_ ; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = _\u2248_; isDecEquivalence = isDecEquivalence }\n\nmodule \u27e6Bool\u27e7-Reasoning = Setoid-Reasoning \u27e6Bool\u27e7-Props.setoid\n\nopen Data.Bool public\n\n\u27e6true\u27e7\u2032 : \u2200 {b} \u2192 T b \u2192 \u27e6Bool\u27e7 true b\n\u27e6true\u27e7\u2032 {true} _ = \u27e6true\u27e7\n\u27e6true\u27e7\u2032 {false} ()\n\n\u27e6false\u27e7\u2032 : \u2200 {b} \u2192 T (not b) \u2192 \u27e6Bool\u27e7 false b\n\u27e6false\u27e7\u2032 {true} ()\n\u27e6false\u27e7\u2032 {false} _ = \u27e6false\u27e7\n\n\u2261\u2192T : \u2200 {b} \u2192 b \u2261 true \u2192 T b\n\u2261\u2192T \u2261.refl = _\n\n\u2261\u2192Tnot : \u2200 {b} \u2192 b \u2261 false \u2192 T (not b)\n\u2261\u2192Tnot \u2261.refl = _\n\nT\u2192\u2261 : \u2200 {b} \u2192 T b \u2192 b \u2261 true\nT\u2192\u2261 {true} _ = \u2261.refl\nT\u2192\u2261 {false} ()\n\nTnot\u2192\u2261 : \u2200 {b} \u2192 T (not b) \u2192 b \u2261 false\nTnot\u2192\u2261 {false} _ = \u2261.refl\nTnot\u2192\u2261 {true} ()\n\nT\u2227 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T b\u2082 \u2192 T (b\u2081 \u2227 b\u2082)\nT\u2227 p q = _\u27e8$\u27e9_ (from B.T-\u2227) (p , q)\n\nT\u2227\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2081\nT\u2227\u2081 = proj\u2081 \u2218 _\u27e8$\u27e9_ (to B.T-\u2227)\n\nT\u2227\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2082\nT\u2227\u2082 {b\u2081} = proj\u2082 \u2218 _\u27e8$\u27e9_ (to (B.T-\u2227 {b\u2081}))\n\nT\u2228'\u228e : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2228 b\u2082) \u2192 T b\u2081 \u228e T b\u2082\nT\u2228'\u228e {b\u2081} = _\u27e8$\u27e9_ (to (B.T-\u2228 {b\u2081}))\n\nT\u2228\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2081 = _\u27e8$\u27e9_ (from B.T-\u2228) \u2218 inj\u2081\n\nT\u2228\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2082 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2082 {b\u2081} = _\u27e8$\u27e9_ (from (B.T-\u2228 {b\u2081})) \u2218 inj\u2082\n\nT'not'\u00ac : \u2200 {b} \u2192 T (not b) \u2192 \u00ac (T b)\nT'not'\u00ac {false} _ = \u03bb()\nT'not'\u00ac {true} ()\n\nT'\u00ac'not : \u2200 {b} \u2192 \u00ac (T b) \u2192 T (not b)\nT'\u00ac'not {true} f = f _\nT'\u00ac'not {false} _ = _\n\n\u2227\u21d2\u2228 : \u2200 x y \u2192 T (x \u2227 y) \u2192 T (x \u2228 y)\n\u2227\u21d2\u2228 true y = _\n\u2227\u21d2\u2228 false y = \u03bb ()\n\nTdec : \u2200 b \u2192 Dec (T b)\nTdec true = yes _\nTdec false = no \u03bb()\n\nde-morgan : \u2200 x y \u2192 not (x \u2228 y) \u2261 not x \u2227 not y\nde-morgan true _ = \u2261.refl\nde-morgan false _ = \u2261.refl\n\n-- false is 0 and true is 1\nto\u2115 : Bool \u2192 \u2115\nto\u2115 b = if b then 1 else 0\n\nto\u2115\u22641 : \u2200 b \u2192 to\u2115 b \u2264 1\nto\u2115\u22641 true = s\u2264s z\u2264n\nto\u2115\u22641 false = z\u2264n\n\nxor-not-not : \u2200 x y \u2192 (not x) xor (not y) \u2261 x xor y\nxor-not-not true y = \u2261.refl\nxor-not-not false y = B.not-involutive y\n\nnot-inj : \u2200 {x y} \u2192 not x \u2261 not y \u2192 x \u2261 y\nnot-inj {true} {true} _ = \u2261.refl\nnot-inj {true} {false} ()\nnot-inj {false} {true} ()\nnot-inj {false} {false} _ = \u2261.refl\n\nxor-inj\u2081 : \u2200 x {y z} \u2192 x xor y \u2261 x xor z \u2192 y \u2261 z\nxor-inj\u2081 true = not-inj\nxor-inj\u2081 false = id\n\nxor-inj\u2082 : \u2200 x {y z} \u2192 y xor x \u2261 z xor x \u2192 y \u2261 z\nxor-inj\u2082 x {y} {z} rewrite Xor\u00b0.+-comm y x | Xor\u00b0.+-comm z x = xor-inj\u2081 x\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"82e8bb92981e2fee80ec5aaf32d899355e992d09","subject":"Made the dist proof more general","message":"Made the dist proof more general\n","repos":"crypto-agda\/crypto-agda","old_file":"sum.agda","new_file":"sum.agda","new_contents":"import Level as L\nopen L using (Lift)\nopen import Type hiding (\u2605)\nopen import Function\nopen import Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat.NP hiding (_^_)\nopen import Data.Nat.Properties\nopen import Data.Unit hiding (_\u2264_)\nopen import Data.Sum\nopen import Data.Maybe.NP\nopen import Data.Product\nopen import Data.Bits\nopen import Data.Bool.NP as Bool\nopen import Function.Equality using (_\u27e8$\u27e9_)\nimport Function.Inverse as FI\nopen FI using (_\u2194_; module Inverse)\nimport Function.Related as FR\nopen import Function.Related.TypeIsomorphisms.NP\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_)\nopen import Search.Type\nopen import Search.Searchable renaming (Searchable to SumProp)\nopen import Search.Searchable.Product\n\nmodule sum where\n\nsum-lin\u21d2sum-zero : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumLin sum \u2192 SumZero sum\nsum-lin\u21d2sum-zero sum-lin = sum-lin (\u03bb _ \u2192 0) 0\n\nsum-mono\u21d2sum-ext : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumMono sum \u2192 SumExt sum\nsum-mono\u21d2sum-ext sum-mono f\u2257g = \u2115\u2264.antisym (sum-mono (\u2115\u2264.reflexive \u2218 f\u2257g)) (sum-mono (\u2115\u2264.reflexive \u2218 \u2261.sym \u2218 f\u2257g))\n\nsum-ext+sum-hom\u21d2sum-mono : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumExt sum \u2192 SumHom sum \u2192 SumMono sum\nsum-ext+sum-hom\u21d2sum-mono {sum = sum} sum-ext sum-hom {f} {g} f\u2264\u00b0g =\n sum f \u2264\u27e8 m\u2264m+n _ _ \u27e9\n sum f + sum (\u03bb x \u2192 g x \u2238 f x) \u2261\u27e8 \u2261.sym (sum-hom _ _) \u27e9\n sum (\u03bb x \u2192 f x + (g x \u2238 f x)) \u2261\u27e8 sum-ext (m+n\u2238m\u2261n \u2218 f\u2264\u00b0g) \u27e9\n sum g \u220e where open \u2264-Reasoning\n\nsearch-swap' : \u2200 {A B} cm (\u03bcA : SumProp A) (\u03bcB : SumProp B) f \u2192\n let open CMon cm\n s\u1d2c = search \u03bcA _\u2219_\n s\u1d2e = search \u03bcB _\u2219_ in\n s\u1d2c (s\u1d2e \u2218 f) \u2248 s\u1d2e (s\u1d2c \u2218 flip f)\nsearch-swap' cm \u03bcA \u03bcB f = search-swap \u03bcA sg f (search-hom \u03bcB cm)\n where open CMon cm\n\nsum-swap : \u2200 {A B} (\u03bcA : SumProp A) (\u03bcB : SumProp B) f \u2192\n sum \u03bcA (sum \u03bcB \u2218 f) \u2261 sum \u03bcB (sum \u03bcA \u2218 flip f)\nsum-swap = search-swap' \u2115\u00b0.+-commutativeMonoid\n\n_\u2248Sum_ : \u2200 {A} \u2192 (sum\u2080 sum\u2081 : Sum A) \u2192 \u2605 _\nsum\u2080 \u2248Sum sum\u2081 = \u2200 f \u2192 sum\u2080 f \u2261 sum\u2081 f\n\n_\u2248Search_ : \u2200 {A} \u2192 (s\u2080 s\u2081 : Search A) \u2192 \u2605\u2081\ns\u2080 \u2248Search s\u2081 = \u2200 {B} (op : Op\u2082 B) f \u2192 s\u2080 op f \u2261 s\u2081 op f\n\n\u03bcLift\u22a4 : SumProp (Lift \u22a4)\n\u03bcLift\u22a4 = _ , ind\n where\n srch : Search (Lift \u22a4)\n srch _ f = f _\n\n ind : SearchInd srch\n ind _ _ Pf = Pf _\n\n\u03bc\u22a4 : SumProp \u22a4\n\u03bc\u22a4 = srch , ind\n where\n srch : Search \u22a4\n srch _ f = f _\n\n ind : SearchInd srch\n ind _ _ Pf = Pf _\n\n\u03bcBit : SumProp Bit\n\u03bcBit = searchBit , ind\n where\n searchBit : Search Bit\n searchBit _\u2219_ f = f 0b \u2219 f 1b\n\n ind : SearchInd searchBit\n ind _ _P\u2219_ Pf = Pf 0b P\u2219 Pf 1b\n\ninfixr 4 _+Search_\n\n_+Search_ : \u2200 {A B} \u2192 Search A \u2192 Search B \u2192 Search (A \u228e B)\n(search\u1d2c +Search search\u1d2e) _\u2219_ f = search\u1d2c _\u2219_ (f \u2218 inj\u2081) \u2219 search\u1d2e _\u2219_ (f \u2218 inj\u2082)\n\n_+SearchInd_ : \u2200 {A B} {s\u1d2c : Search A} {s\u1d2e : Search B} \u2192\n SearchInd s\u1d2c \u2192 SearchInd s\u1d2e \u2192 SearchInd (s\u1d2c +Search s\u1d2e)\n(Ps\u1d2c +SearchInd Ps\u1d2e) P P\u2219 Pf\n = P\u2219 (Ps\u1d2c (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2081))) P\u2219 (Pf \u2218 inj\u2081))\n (Ps\u1d2e (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2082))) P\u2219 (Pf \u2218 inj\u2082))\n\ninfixr 4 _+Sum_\n\n_+Sum_ : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u228e B)\n(sum\u1d2c +Sum sum\u1d2e) f = sum\u1d2c (f \u2218 inj\u2081) + sum\u1d2e (f \u2218 inj\u2082)\n\n_+\u03bc_ : \u2200 {A B} \u2192 SumProp A \u2192 SumProp B \u2192 SumProp (A \u228e B)\n\u03bcA +\u03bc \u03bcB = _ , search-ind \u03bcA +SearchInd search-ind \u03bcB\n\n\n_\u228e'_ : \u2605\u2080 \u2192 \u2605\u2080 \u2192 \u2605\u2080\nA \u228e' B = \u03a3 Bool (cond A B)\n\n_\u03bc\u228e'_ : \u2200 {A B} \u2192 SumProp A \u2192 SumProp B \u2192 SumProp (A \u228e' B)\n\u03bcA \u03bc\u228e' \u03bcB = \u03bc\u03a3 \u03bcBit (\u03bb { {true} \u2192 \u03bcA ; {false} \u2192 \u03bcB })\n\nsum-const : \u2200 {A} (\u03bcA : SumProp A) \u2192 \u2200 k \u2192 sum \u03bcA (const k) \u2261 Card \u03bcA * k\nsum-const \u03bcA k\n rewrite \u2115\u00b0.*-comm (Card \u03bcA) k\n | \u2261.sym (sum-lin \u03bcA (const 1) k)\n | proj\u2082 \u2115\u00b0.*-identity k = \u2261.refl\n\ninfixr 4 _\u00d7Sum-proj\u2081_ _\u00d7Sum-proj\u2081'_ _\u00d7Sum-proj\u2082_ _\u00d7Sum-proj\u2082'_\n\n_\u00d7Sum-proj\u2081_ : \u2200 {A B}\n (\u03bcA : SumProp A)\n (\u03bcB : SumProp B)\n f \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 Card \u03bcB * sum \u03bcA f\n(\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n rewrite sum-ext \u03bcA (sum-const \u03bcB \u2218 f)\n = sum-lin \u03bcA f (Card \u03bcB)\n\n_\u00d7Sum-proj\u2082_ : \u2200 {A B}\n (\u03bcA : SumProp A)\n (\u03bcB : SumProp B)\n f \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 Card \u03bcA * sum \u03bcB f\n(\u03bcA \u00d7Sum-proj\u2082 \u03bcB) f = sum-const \u03bcA (sum \u03bcB f)\n\n_\u00d7Sum-proj\u2081'_ : \u2200 {A B}\n (\u03bcA : SumProp A) (\u03bcB : SumProp B)\n {f} {g} \u2192\n sum \u03bcA f \u2261 sum \u03bcA g \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 sum (\u03bcA \u00d7\u03bc \u03bcB) (g \u2218 proj\u2081)\n(\u03bcA \u00d7Sum-proj\u2081' \u03bcB) {f} {g} sumf\u2261sumg\n rewrite (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n | (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) g\n | sumf\u2261sumg = \u2261.refl\n\n_\u00d7Sum-proj\u2082'_ : \u2200 {A B}\n (\u03bcA : SumProp A) (\u03bcB : SumProp B)\n {f} {g} \u2192\n sum \u03bcB f \u2261 sum \u03bcB g \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 sum (\u03bcA \u00d7\u03bc \u03bcB) (g \u2218 proj\u2082)\n(\u03bcA \u00d7Sum-proj\u2082' \u03bcB) sumf\u2261sumg = sum-ext \u03bcA (const sumf\u2261sumg)\n\n\u03bc-view : \u2200 {A B} \u2192 (A \u2192 B) \u2192 SumProp A \u2192 SumProp B\n\u03bc-view {A}{B} A\u2192B \u03bcA = search\u1d2e , ind\n where\n search\u1d2e : Search B\n search\u1d2e m f = search \u03bcA m (f \u2218 A\u2192B)\n\n ind : SearchInd search\u1d2e\n ind P P\u2219 Pf = search-ind \u03bcA (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 A\u2192B))) P\u2219 (Pf \u2218 A\u2192B)\n\n\u03bc-iso : \u2200 {A B} \u2192 (A \u2194 B) \u2192 SumProp A \u2192 SumProp B\n\u03bc-iso A\u2194B = \u03bc-view (_\u27e8$\u27e9_ (Inverse.to A\u2194B))\n\n\u03bc-view-preserve : \u2200 {A B} (A\u2192B : A \u2192 B)(B\u2192A : B \u2192 A)(A\u2248B : id \u2257 B\u2192A \u2218 A\u2192B) f (\u03bcA : SumProp A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-view A\u2192B \u03bcA) (f \u2218 B\u2192A)\n\u03bc-view-preserve A\u2192B B\u2192A A\u2248B f \u03bcA = sum-ext \u03bcA (\u2261.cong f \u2218 A\u2248B)\n\n\u03bc-iso-preserve : \u2200 {A B} (A\u2194B : A \u2194 B) f (\u03bcA : SumProp A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-iso A\u2194B \u03bcA) (f \u2218 _\u27e8$\u27e9_ (Inverse.from A\u2194B))\n\u03bc-iso-preserve A\u2194B f \u03bcA = \u03bc-view-preserve (_\u27e8$\u27e9_ (Inverse.to A\u2194B)) (_\u27e8$\u27e9_ (Inverse.from A\u2194B))\n (\u2261.sym \u2218 Inverse.left-inverse-of A\u2194B) f \u03bcA\n\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Vec.NP as Vec using (Vec; tabulate; _++_) renaming (map to vmap; sum to vsum; foldr to vfoldr; foldr\u2081 to vfoldr\u2081)\n\nvmsum : \u2200 m {n} \u2192 let open Mon m in\n Vec C n \u2192 C\nvmsum m = vfoldr _ _\u2219_ \u03b5\n where open Monoid m\n\nvsgsum : \u2200 sg {n} \u2192 let open Sgrp sg in\n Vec C (suc n) \u2192 C\nvsgsum sg = vfoldr\u2081 _\u2219_\n where open Sgrp sg\n\n-- let's recall that: tabulate f \u2257 vmap f (allFin n)\n\n-- searchMonFin : \u2200 n \u2192 SearchMon (Fin n)\n-- searchMonFin n m f = vmsum m (tabulate f)\n\nsearchFinSuc : \u2200 n \u2192 Search (Fin (suc n))\nsearchFinSuc n _\u2219_ f = vfoldr\u2081 _\u2219_ (tabulate f)\n\n\u03bcMaybe : \u2200 {A} \u2192 SumProp A \u2192 SumProp (Maybe A)\n\u03bcMaybe \u03bcA = \u03bc-iso (FI.sym Maybe\u2194\u22a4\u228e) (\u03bc\u22a4 +\u03bc \u03bcA)\n\n\u03bcMaybe^ : \u2200 {A} n \u2192 SumProp A \u2192 SumProp (Maybe^ n A)\n\u03bcMaybe^ zero \u03bcA = \u03bcA\n\u03bcMaybe^ (suc n) \u03bcA = \u03bcMaybe (\u03bcMaybe^ n \u03bcA)\n\n\u03bcFinSuc : \u2200 n \u2192 SumProp (Fin (suc n))\n\u03bcFinSuc n = searchFinSuc n , ind n\n where ind : \u2200 n \u2192 SearchInd (searchFinSuc n)\n ind zero P P\u2219 Pf = Pf zero\n ind (suc n) P P\u2219 Pf = P\u2219 (Pf zero) (ind n (\u03bb s \u2192 P (\u03bb op f \u2192 s op (f \u2218 suc))) P\u2219 (Pf \u2218 suc))\n\n\u03bcFinSucIso : \u2200 n \u2192 SumProp (Fin (suc n))\n\u03bcFinSucIso n = \u03bc-iso (Maybe^\u22a4\u2194Fin1+ n) (\u03bcMaybe^ n \u03bc\u22a4)\n\n\u03bc^ : \u2200 {A} (\u03bcA : SumProp A) n \u2192 SumProp (A ^ n)\n\u03bc^ \u03bcA zero = \u03bcLift\u22a4\n\u03bc^ \u03bcA (suc n) = \u03bcA \u00d7\u03bc \u03bc^ \u03bcA n\n\n\u03bcVec : \u2200 {A} (\u03bcA : SumProp A) n \u2192 SumProp (Vec A n)\n\u03bcVec \u03bcA n = \u03bc-iso (^\u2194Vec n) (\u03bc^ \u03bcA n)\n\nsearchVec : \u2200 {A} n \u2192 Search A \u2192 Search (Vec A n)\nsearchVec zero search\u1d2c op f = f []\nsearchVec (suc n) search\u1d2c op f = search\u1d2c op (\u03bb x \u2192 searchVec n search\u1d2c op (f \u2218 _\u2237_ x))\n\nsearchVec-spec : \u2200 {A} (\u03bcA : SumProp A) n \u2192 searchVec n (search \u03bcA) \u2248Search search (\u03bcVec \u03bcA n)\nsearchVec-spec \u03bcA zero op f = \u2261.refl\nsearchVec-spec \u03bcA (suc n) op f = search-ext \u03bcA op (\u03bb x \u2192 searchVec-spec \u03bcA n op (f \u2218 _\u2237_ x))\n\nsearchVec-++ : \u2200 {A} n {m} (\u03bcA : SumProp A) sg\n \u2192 let open Sgrp sg in\n (f : Vec A (n + m) \u2192 C)\n \u2192 search (\u03bcVec \u03bcA (n + m)) _\u2219_ f\n \u2248 search (\u03bcVec \u03bcA n) _\u2219_ (\u03bb xs \u2192\n search (\u03bcVec \u03bcA m) _\u2219_ (\u03bb ys \u2192\n f (xs ++ ys)))\nsearchVec-++ zero \u03bcA sg f = Sgrp.refl sg\nsearchVec-++ (suc n) \u03bcA sg f = search-sg-ext \u03bcA sg (\u03bb x \u2192\n searchVec-++ n \u03bcA sg (f \u2218 _\u2237_ x))\n\nsumVec-swap : \u2200 {A} n {m} (\u03bcA : SumProp A)(f : Vec A (n + m) \u2192 \u2115)\n \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 f (xs ++ ys)))\nsumVec-swap n {m} \u03bcA f = sum-swap (\u03bcVec \u03bcA n) (\u03bcVec \u03bcA m) (\u03bb xs ys \u2192 f (xs ++ ys))\n\nswapS : \u2200 {A B} \u2192 SumProp (A \u00d7 B) \u2192 SumProp (B \u00d7 A)\nswapS = \u03bc-iso swap-iso\n\nswapS-preserve : \u2200 {A B} f (\u03bcA\u00d7B : SumProp (A \u00d7 B)) \u2192 sum \u03bcA\u00d7B f \u2261 sum (swapS \u03bcA\u00d7B) (f \u2218 swap)\nswapS-preserve = \u03bc-iso-preserve swap-iso\n\nmodule _ {A : Set}(\u03bcA : SumProp A) where\n\n private\n sA = search \u03bcA\n\n extend : \u2200 {n} \u2192 A \u2192 (Fin n \u2192 A) \u2192 Fin (suc n) \u2192 A\n extend x g zero = x\n extend x g (suc i) = g i\n\n abs : Fin 0 \u2192 A\n abs ()\n\n -- There is one function Fin 0 \u2192 A (called abs) so this should be fine\n -- if not there is a version below that forces the domain to be non-empty\n sFun : \u2200 n \u2192 Search (Fin n \u2192 A)\n sFun zero op f = f abs\n sFun (suc n) op f = sA op (\u03bb x \u2192 sFun n op (f \u2218 extend x))\n\n Ind : \u2200 n \u2192 SearchInd (sFun n)\n Ind zero P P\u2219 Pf = Pf abs\n Ind (suc n) P P\u2219 Pf = \n search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (\u03bb x \u2192 sFun n op (f \u2218 extend x)))) \n P\u2219 \n (\u03bb x \u2192 Ind n (\u03bb sf \u2192 P (\u03bb op f \u2192 sf op (f \u2218 extend x))) \n P\u2219 (Pf \u2218 extend x))\n\n \u03bcFun : \u2200 {n} \u2192 SumProp (Fin n \u2192 A)\n \u03bcFun = sFun _ , Ind _\n\n\nmodule _ {A}(\u03bcA : SumProp A)\n (cmonoid : CommutativeMonoid L.zero L.zero)\n (_\u25ce_ : let open CMon cmonoid in Carrier \u2192 Carrier \u2192 Carrier)\n (distrib : let open CMon cmonoid in _DistributesOver_ _\u2248_ _\u25ce_ _\u2219_)\n (_\u25ce-cong_ : let open CMon cmonoid in _\u25ce_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_) where\n\n open CMon cmonoid\n s\u1d2c = search \u03bcA _\u2219_\n\n bigDistr : \u2200 I F \u2192 search (\u03bcFinSuc I) _\u25ce_ (s\u1d2c \u2218 F)\n \u2248 search (\u03bcFun \u03bcA) _\u2219_ (\u03bb f \u2192 search (\u03bcFinSuc I) _\u25ce_ (\u03bb i \u2192 F i (f i)))\n bigDistr zero _ = refl\n bigDistr (suc I) F\n = \u03a0' (suc I) (s\u1d2c \u2218 F)\n \u2248\u27e8 refl \u27e9\n s\u1d2c (F zero) \u25ce \u03a0' I (s\u1d2c \u2218 Fj)\n \u2248\u27e8 refl \u25ce-cong bigDistr I Fj \u27e9\n (s\u1d2c \u2218 F) zero \u25ce \u03a3F I (\u03bb f \u2192 \u03a0' I (\u03bb i \u2192 F (suc i) (f i)))\n \u2248\u27e8 sym\n (search-lin\u02e1 (\u03bcF I) monoid _\u25ce_\n (\u03bb f \u2192 \u03a0' I (\u03bb i \u2192 F (suc i) (f i))) (s\u1d2c (F zero)) (proj\u2081 distrib)) \u27e9\n \u03a3F I (\u03bb f \u2192 (s\u1d2c \u2218 F) zero \u25ce \u03a0' I (\u03bb i \u2192 F (suc i) (f i)))\n \u2248\u27e8 search-sg-ext (\u03bcF I) (Monoid.semigroup monoid)\n (\u03bb f \u2192 sym (search-lin\u02b3 \u03bcA monoid _\u25ce_ (F zero) (\u03a0' I (\u03bb i \u2192 F (suc i) (f i))) (proj\u2082 distrib))) \u27e9\n \u03a3F I (\u03bb f \u2192 s\u1d2c (\u03bb j \u2192 F zero j \u25ce (\u03a0' I \u03bb i \u2192 F (suc i) (f i))))\n \u2248\u27e8 search-sg-ext (\u03bcF I) (Monoid.semigroup monoid)\n {(\u03bb f \u2192 s\u1d2c (\u03bb j \u2192 F zero j \u25ce (\u03a0' I \u03bb i \u2192 F (suc i) (f i))))}\n {(\u03bb f \u2192 s\u1d2c (\u03bb j \u2192 \u03a0' (suc I) (\u03bb i \u2192 F i (extend \u03bcA j f i))))}\n (\u03bb f \u2192 refl) \u27e9\n \u03a3F I (\u03bb f \u2192 s\u1d2c (\u03bb j \u2192 \u03a0' (suc I) (\u03bb i \u2192 F i (extend \u03bcA j f i))))\n \u2248\u27e8 search-swap (\u03bcF I) (Monoid.semigroup monoid) (\u03bb f j \u2192 \u03a0' (suc I) (\u03bb i \u2192 F i (extend \u03bcA j f i))) {s\u1d2e = s\u1d2c} (search-hom \u03bcA cmonoid) \u27e9\n s\u1d2c (\u03bb j \u2192 \u03a3F I (\u03bb f \u2192 \u03a0' (suc I) (\u03bb i \u2192 F i (extend \u03bcA j f i))))\n \u2248\u27e8 refl \u27e9\n \u03a3F (suc I) (\u03bb f \u2192 \u03a0' (suc I) (\u03bb i \u2192 F i (f i)))\n \u220e\n where\n \u03bcF = \u03bb i \u2192 \u03bcFun \u03bcA {suc i}\n \u03a0' = \u03bb i \u2192 search (\u03bcFinSuc i) _\u25ce_\n \u03a3F = \u03bb i \u2192 search (\u03bcF i) _\u2219_\n Fj = \u03bb i j \u2192 F (suc i) j\n\n{-\n -- If we want to force non-empty domain\n\n sFun : \u2200 n \u2192 Search (Fin (suc n) \u2192 A)\n sFun zero op f = sA op (f \u2218 const)\n sFun (suc n) op f = sA op (\u03bb x \u2192 sFun n op (f \u2218 extend x))\n\n Ind : \u2200 n \u2192 SearchInd (sFun n)\n Ind zero P P\u2219 Pf = search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (f \u2218 const))) P\u2219 (Pf \u2218 const)\n Ind (suc n) P P\u2219 Pf = search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (\u03bb x \u2192 sFun n op (f \u2218 extend x)))) \n P\u2219 \n (\u03bb x \u2192 Ind n (\u03bb sf \u2192 P (\u03bb op f \u2192 sf op (f \u2218 extend x))) P\u2219 (Pf \u2218 extend x))\n\n-}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"import Level as L\nopen L using (Lift)\nopen import Type hiding (\u2605)\nopen import Function\nopen import Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat.NP hiding (_^_)\nopen import Data.Nat.Properties\nopen import Data.Unit hiding (_\u2264_)\nopen import Data.Sum\nopen import Data.Maybe.NP\nopen import Data.Product\nopen import Data.Bits\nopen import Data.Bool.NP as Bool\nopen import Function.Equality using (_\u27e8$\u27e9_)\nimport Function.Inverse as FI\nopen FI using (_\u2194_; module Inverse)\nimport Function.Related as FR\nopen import Function.Related.TypeIsomorphisms.NP\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_)\nopen import Search.Type\nopen import Search.Searchable renaming (Searchable to SumProp)\nopen import Search.Searchable.Product\n\nmodule sum where\n\nsum-lin\u21d2sum-zero : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumLin sum \u2192 SumZero sum\nsum-lin\u21d2sum-zero sum-lin = sum-lin (\u03bb _ \u2192 0) 0\n\nsum-mono\u21d2sum-ext : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumMono sum \u2192 SumExt sum\nsum-mono\u21d2sum-ext sum-mono f\u2257g = \u2115\u2264.antisym (sum-mono (\u2115\u2264.reflexive \u2218 f\u2257g)) (sum-mono (\u2115\u2264.reflexive \u2218 \u2261.sym \u2218 f\u2257g))\n\nsum-ext+sum-hom\u21d2sum-mono : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumExt sum \u2192 SumHom sum \u2192 SumMono sum\nsum-ext+sum-hom\u21d2sum-mono {sum = sum} sum-ext sum-hom {f} {g} f\u2264\u00b0g =\n sum f \u2264\u27e8 m\u2264m+n _ _ \u27e9\n sum f + sum (\u03bb x \u2192 g x \u2238 f x) \u2261\u27e8 \u2261.sym (sum-hom _ _) \u27e9\n sum (\u03bb x \u2192 f x + (g x \u2238 f x)) \u2261\u27e8 sum-ext (m+n\u2238m\u2261n \u2218 f\u2264\u00b0g) \u27e9\n sum g \u220e where open \u2264-Reasoning\n\nsearch-swap' : \u2200 {A B} cm (\u03bcA : SumProp A) (\u03bcB : SumProp B) f \u2192\n let open CMon cm\n s\u1d2c = search \u03bcA _\u2219_\n s\u1d2e = search \u03bcB _\u2219_ in\n s\u1d2c (s\u1d2e \u2218 f) \u2248 s\u1d2e (s\u1d2c \u2218 flip f)\nsearch-swap' cm \u03bcA \u03bcB f = search-swap \u03bcA sg f (search-hom \u03bcB cm)\n where open CMon cm\n\nsum-swap : \u2200 {A B} (\u03bcA : SumProp A) (\u03bcB : SumProp B) f \u2192\n sum \u03bcA (sum \u03bcB \u2218 f) \u2261 sum \u03bcB (sum \u03bcA \u2218 flip f)\nsum-swap = search-swap' \u2115\u00b0.+-commutativeMonoid\n\n_\u2248Sum_ : \u2200 {A} \u2192 (sum\u2080 sum\u2081 : Sum A) \u2192 \u2605 _\nsum\u2080 \u2248Sum sum\u2081 = \u2200 f \u2192 sum\u2080 f \u2261 sum\u2081 f\n\n_\u2248Search_ : \u2200 {A} \u2192 (s\u2080 s\u2081 : Search A) \u2192 \u2605\u2081\ns\u2080 \u2248Search s\u2081 = \u2200 {B} (op : Op\u2082 B) f \u2192 s\u2080 op f \u2261 s\u2081 op f\n\n\u03bcLift\u22a4 : SumProp (Lift \u22a4)\n\u03bcLift\u22a4 = _ , ind\n where\n srch : Search (Lift \u22a4)\n srch _ f = f _\n\n ind : SearchInd srch\n ind _ _ Pf = Pf _\n\n\u03bc\u22a4 : SumProp \u22a4\n\u03bc\u22a4 = srch , ind\n where\n srch : Search \u22a4\n srch _ f = f _\n\n ind : SearchInd srch\n ind _ _ Pf = Pf _\n\n\u03bcBit : SumProp Bit\n\u03bcBit = searchBit , ind\n where\n searchBit : Search Bit\n searchBit _\u2219_ f = f 0b \u2219 f 1b\n\n ind : SearchInd searchBit\n ind _ _P\u2219_ Pf = Pf 0b P\u2219 Pf 1b\n\ninfixr 4 _+Search_\n\n_+Search_ : \u2200 {A B} \u2192 Search A \u2192 Search B \u2192 Search (A \u228e B)\n(search\u1d2c +Search search\u1d2e) _\u2219_ f = search\u1d2c _\u2219_ (f \u2218 inj\u2081) \u2219 search\u1d2e _\u2219_ (f \u2218 inj\u2082)\n\n_+SearchInd_ : \u2200 {A B} {s\u1d2c : Search A} {s\u1d2e : Search B} \u2192\n SearchInd s\u1d2c \u2192 SearchInd s\u1d2e \u2192 SearchInd (s\u1d2c +Search s\u1d2e)\n(Ps\u1d2c +SearchInd Ps\u1d2e) P P\u2219 Pf\n = P\u2219 (Ps\u1d2c (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2081))) P\u2219 (Pf \u2218 inj\u2081))\n (Ps\u1d2e (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2082))) P\u2219 (Pf \u2218 inj\u2082))\n\ninfixr 4 _+Sum_\n\n_+Sum_ : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u228e B)\n(sum\u1d2c +Sum sum\u1d2e) f = sum\u1d2c (f \u2218 inj\u2081) + sum\u1d2e (f \u2218 inj\u2082)\n\n_+\u03bc_ : \u2200 {A B} \u2192 SumProp A \u2192 SumProp B \u2192 SumProp (A \u228e B)\n\u03bcA +\u03bc \u03bcB = _ , search-ind \u03bcA +SearchInd search-ind \u03bcB\n\n\n_\u228e'_ : \u2605\u2080 \u2192 \u2605\u2080 \u2192 \u2605\u2080\nA \u228e' B = \u03a3 Bool (cond A B)\n\n_\u03bc\u228e'_ : \u2200 {A B} \u2192 SumProp A \u2192 SumProp B \u2192 SumProp (A \u228e' B)\n\u03bcA \u03bc\u228e' \u03bcB = \u03bc\u03a3 \u03bcBit (\u03bb { {true} \u2192 \u03bcA ; {false} \u2192 \u03bcB })\n\nsum-const : \u2200 {A} (\u03bcA : SumProp A) \u2192 \u2200 k \u2192 sum \u03bcA (const k) \u2261 Card \u03bcA * k\nsum-const \u03bcA k\n rewrite \u2115\u00b0.*-comm (Card \u03bcA) k\n | \u2261.sym (sum-lin \u03bcA (const 1) k)\n | proj\u2082 \u2115\u00b0.*-identity k = \u2261.refl\n\ninfixr 4 _\u00d7Sum-proj\u2081_ _\u00d7Sum-proj\u2081'_ _\u00d7Sum-proj\u2082_ _\u00d7Sum-proj\u2082'_\n\n_\u00d7Sum-proj\u2081_ : \u2200 {A B}\n (\u03bcA : SumProp A)\n (\u03bcB : SumProp B)\n f \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 Card \u03bcB * sum \u03bcA f\n(\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n rewrite sum-ext \u03bcA (sum-const \u03bcB \u2218 f)\n = sum-lin \u03bcA f (Card \u03bcB)\n\n_\u00d7Sum-proj\u2082_ : \u2200 {A B}\n (\u03bcA : SumProp A)\n (\u03bcB : SumProp B)\n f \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 Card \u03bcA * sum \u03bcB f\n(\u03bcA \u00d7Sum-proj\u2082 \u03bcB) f = sum-const \u03bcA (sum \u03bcB f)\n\n_\u00d7Sum-proj\u2081'_ : \u2200 {A B}\n (\u03bcA : SumProp A) (\u03bcB : SumProp B)\n {f} {g} \u2192\n sum \u03bcA f \u2261 sum \u03bcA g \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 sum (\u03bcA \u00d7\u03bc \u03bcB) (g \u2218 proj\u2081)\n(\u03bcA \u00d7Sum-proj\u2081' \u03bcB) {f} {g} sumf\u2261sumg\n rewrite (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n | (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) g\n | sumf\u2261sumg = \u2261.refl\n\n_\u00d7Sum-proj\u2082'_ : \u2200 {A B}\n (\u03bcA : SumProp A) (\u03bcB : SumProp B)\n {f} {g} \u2192\n sum \u03bcB f \u2261 sum \u03bcB g \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 sum (\u03bcA \u00d7\u03bc \u03bcB) (g \u2218 proj\u2082)\n(\u03bcA \u00d7Sum-proj\u2082' \u03bcB) sumf\u2261sumg = sum-ext \u03bcA (const sumf\u2261sumg)\n\n\u03bc-view : \u2200 {A B} \u2192 (A \u2192 B) \u2192 SumProp A \u2192 SumProp B\n\u03bc-view {A}{B} A\u2192B \u03bcA = search\u1d2e , ind\n where\n search\u1d2e : Search B\n search\u1d2e m f = search \u03bcA m (f \u2218 A\u2192B)\n\n ind : SearchInd search\u1d2e\n ind P P\u2219 Pf = search-ind \u03bcA (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 A\u2192B))) P\u2219 (Pf \u2218 A\u2192B)\n\n\u03bc-iso : \u2200 {A B} \u2192 (A \u2194 B) \u2192 SumProp A \u2192 SumProp B\n\u03bc-iso A\u2194B = \u03bc-view (_\u27e8$\u27e9_ (Inverse.to A\u2194B))\n\n\u03bc-view-preserve : \u2200 {A B} (A\u2192B : A \u2192 B)(B\u2192A : B \u2192 A)(A\u2248B : id \u2257 B\u2192A \u2218 A\u2192B) f (\u03bcA : SumProp A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-view A\u2192B \u03bcA) (f \u2218 B\u2192A)\n\u03bc-view-preserve A\u2192B B\u2192A A\u2248B f \u03bcA = sum-ext \u03bcA (\u2261.cong f \u2218 A\u2248B)\n\n\u03bc-iso-preserve : \u2200 {A B} (A\u2194B : A \u2194 B) f (\u03bcA : SumProp A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-iso A\u2194B \u03bcA) (f \u2218 _\u27e8$\u27e9_ (Inverse.from A\u2194B))\n\u03bc-iso-preserve A\u2194B f \u03bcA = \u03bc-view-preserve (_\u27e8$\u27e9_ (Inverse.to A\u2194B)) (_\u27e8$\u27e9_ (Inverse.from A\u2194B))\n (\u2261.sym \u2218 Inverse.left-inverse-of A\u2194B) f \u03bcA\n\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Vec.NP as Vec using (Vec; tabulate; _++_) renaming (map to vmap; sum to vsum; foldr to vfoldr; foldr\u2081 to vfoldr\u2081)\n\nvmsum : \u2200 m {n} \u2192 let open Mon m in\n Vec C n \u2192 C\nvmsum m = vfoldr _ _\u2219_ \u03b5\n where open Monoid m\n\nvsgsum : \u2200 sg {n} \u2192 let open Sgrp sg in\n Vec C (suc n) \u2192 C\nvsgsum sg = vfoldr\u2081 _\u2219_\n where open Sgrp sg\n\n-- let's recall that: tabulate f \u2257 vmap f (allFin n)\n\n-- searchMonFin : \u2200 n \u2192 SearchMon (Fin n)\n-- searchMonFin n m f = vmsum m (tabulate f)\n\nsearchFinSuc : \u2200 n \u2192 Search (Fin (suc n))\nsearchFinSuc n _\u2219_ f = vfoldr\u2081 _\u2219_ (tabulate f)\n\n\u03bcMaybe : \u2200 {A} \u2192 SumProp A \u2192 SumProp (Maybe A)\n\u03bcMaybe \u03bcA = \u03bc-iso (FI.sym Maybe\u2194\u22a4\u228e) (\u03bc\u22a4 +\u03bc \u03bcA)\n\n\u03bcMaybe^ : \u2200 {A} n \u2192 SumProp A \u2192 SumProp (Maybe^ n A)\n\u03bcMaybe^ zero \u03bcA = \u03bcA\n\u03bcMaybe^ (suc n) \u03bcA = \u03bcMaybe (\u03bcMaybe^ n \u03bcA)\n\n\u03bcFinSuc : \u2200 n \u2192 SumProp (Fin (suc n))\n\u03bcFinSuc n = searchFinSuc n , ind n\n where ind : \u2200 n \u2192 SearchInd (searchFinSuc n)\n ind zero P P\u2219 Pf = Pf zero\n ind (suc n) P P\u2219 Pf = P\u2219 (Pf zero) (ind n (\u03bb s \u2192 P (\u03bb op f \u2192 s op (f \u2218 suc))) P\u2219 (Pf \u2218 suc))\n\n\u03bcFinSucIso : \u2200 n \u2192 SumProp (Fin (suc n))\n\u03bcFinSucIso n = \u03bc-iso (Maybe^\u22a4\u2194Fin1+ n) (\u03bcMaybe^ n \u03bc\u22a4)\n\n\u03bc^ : \u2200 {A} (\u03bcA : SumProp A) n \u2192 SumProp (A ^ n)\n\u03bc^ \u03bcA zero = \u03bcLift\u22a4\n\u03bc^ \u03bcA (suc n) = \u03bcA \u00d7\u03bc \u03bc^ \u03bcA n\n\n\u03bcVec : \u2200 {A} (\u03bcA : SumProp A) n \u2192 SumProp (Vec A n)\n\u03bcVec \u03bcA n = \u03bc-iso (^\u2194Vec n) (\u03bc^ \u03bcA n)\n\nsearchVec : \u2200 {A} n \u2192 Search A \u2192 Search (Vec A n)\nsearchVec zero search\u1d2c op f = f []\nsearchVec (suc n) search\u1d2c op f = search\u1d2c op (\u03bb x \u2192 searchVec n search\u1d2c op (f \u2218 _\u2237_ x))\n\nsearchVec-spec : \u2200 {A} (\u03bcA : SumProp A) n \u2192 searchVec n (search \u03bcA) \u2248Search search (\u03bcVec \u03bcA n)\nsearchVec-spec \u03bcA zero op f = \u2261.refl\nsearchVec-spec \u03bcA (suc n) op f = search-ext \u03bcA op (\u03bb x \u2192 searchVec-spec \u03bcA n op (f \u2218 _\u2237_ x))\n\nsearchVec-++ : \u2200 {A} n {m} (\u03bcA : SumProp A) sg\n \u2192 let open Sgrp sg in\n (f : Vec A (n + m) \u2192 C)\n \u2192 search (\u03bcVec \u03bcA (n + m)) _\u2219_ f\n \u2248 search (\u03bcVec \u03bcA n) _\u2219_ (\u03bb xs \u2192\n search (\u03bcVec \u03bcA m) _\u2219_ (\u03bb ys \u2192\n f (xs ++ ys)))\nsearchVec-++ zero \u03bcA sg f = Sgrp.refl sg\nsearchVec-++ (suc n) \u03bcA sg f = search-sg-ext \u03bcA sg (\u03bb x \u2192\n searchVec-++ n \u03bcA sg (f \u2218 _\u2237_ x))\n\nsumVec-swap : \u2200 {A} n {m} (\u03bcA : SumProp A)(f : Vec A (n + m) \u2192 \u2115)\n \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 f (xs ++ ys)))\nsumVec-swap n {m} \u03bcA f = sum-swap (\u03bcVec \u03bcA n) (\u03bcVec \u03bcA m) (\u03bb xs ys \u2192 f (xs ++ ys))\n\nswapS : \u2200 {A B} \u2192 SumProp (A \u00d7 B) \u2192 SumProp (B \u00d7 A)\nswapS = \u03bc-iso swap-iso\n\nswapS-preserve : \u2200 {A B} f (\u03bcA\u00d7B : SumProp (A \u00d7 B)) \u2192 sum \u03bcA\u00d7B f \u2261 sum (swapS \u03bcA\u00d7B) (f \u2218 swap)\nswapS-preserve = \u03bc-iso-preserve swap-iso\n\nmodule _ {A : Set}(\u03bcA : SumProp A) where\n\n private\n sA = search \u03bcA\n\n extend : \u2200 {n} \u2192 A \u2192 (Fin n \u2192 A) \u2192 Fin (suc n) \u2192 A\n extend x g zero = x\n extend x g (suc i) = g i\n\n abs : Fin 0 \u2192 A\n abs ()\n\n -- There is one function Fin 0 \u2192 A (called abs) so this should be fine\n -- if not there is a version below that forces the domain to be non-empty\n sFun : \u2200 n \u2192 Search (Fin n \u2192 A)\n sFun zero op f = f abs\n sFun (suc n) op f = sA op (\u03bb x \u2192 sFun n op (f \u2218 extend x))\n\n Ind : \u2200 n \u2192 SearchInd (sFun n)\n Ind zero P P\u2219 Pf = Pf abs\n Ind (suc n) P P\u2219 Pf = \n search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (\u03bb x \u2192 sFun n op (f \u2218 extend x)))) \n P\u2219 \n (\u03bb x \u2192 Ind n (\u03bb sf \u2192 P (\u03bb op f \u2192 sf op (f \u2218 extend x))) \n P\u2219 (Pf \u2218 extend x))\n\n \u03bcFun : \u2200 {n} \u2192 SumProp (Fin n \u2192 A)\n \u03bcFun = sFun _ , Ind _\n\n\nbigDistr : \u2200 I J F \u2192 search (\u03bcFinSuc I) _*_ (search (\u03bcFinSuc J) _+_ \u2218 F)\n \u2261.\u2261 search (\u03bcFun (\u03bcFinSuc J)) _+_ (\u03bb f \u2192 search (\u03bcFinSuc I) _*_ (\u03bb i \u2192 F i (f i)))\nbigDistr zero _ _ = \u2261.refl\nbigDistr (suc I) J F\n = \u03a0' (suc I) (\u03a3' J \u2218 F)\n \u2261\u27e8 \u2261.refl \u27e9\n (\u03a3' J \u2218 F) zero * \u03a0' I (\u03a3' J \u2218 Fj)\n \u2261\u27e8 \u2261.cong (_*_ ((\u03a3' J \u2218 F) zero)) (bigDistr I J Fj) \u27e9\n (\u03a3' J \u2218 F) zero * \u03a3F I J (\u03bb f \u2192 \u03a0' I (\u03bb i \u2192 F (suc i) (f i)))\n \u2261\u27e8 \u2261.sym\n (search-lin\u02e1 (\u03bcF I J) \u2115+.monoid _*_\n (\u03bb f \u2192 \u03a0' I (\u03bb i \u2192 F (suc i) (f i))) (\u03a3' J (F zero)) (proj\u2081 (\u2115\u00b0.distrib)))\u27e9\n \u03a3F I J (\u03bb f \u2192 (\u03a3' J \u2218 F) zero * \u03a0' I (\u03bb i \u2192 F (suc i) (f i)))\n \u2261\u27e8 search-ext (\u03bcF I J) _+_\n {\u03bb f \u2192 (\u03a3' J \u2218 F) zero * \u03a0' I (\u03bb i \u2192 F (suc i) (f i))}\n {\u03bb f \u2192 \u03a3' J (\u03bb j \u2192 F zero j * (\u03a0' I \u03bb i \u2192 F (suc i) (f i)))}\n (\u03bb f \u2192 \u2261.sym (search-lin\u02b3 (\u03bc J) \u2115+.monoid _*_ (F zero) (\u03a0' I (\u03bb i \u2192 F (suc i) (f i))) (proj\u2082 \u2115\u00b0.distrib))) \u27e9\n \u03a3F I J (\u03bb f \u2192 \u03a3' J (\u03bb j \u2192 F zero j * (\u03a0' I \u03bb i \u2192 F (suc i) (f i))))\n \u2261\u27e8 search-ext (\u03bcF I J) _+_\n {(\u03bb f \u2192 \u03a3' J (\u03bb j \u2192 F zero j * (\u03a0' I \u03bb i \u2192 F (suc i) (f i))))}\n {(\u03bb f \u2192 \u03a3' J (\u03bb j \u2192 \u03a0' (suc I) (\u03bb i \u2192 F i (extend (\u03bcFinSuc J) j f i))))}\n (\u03bb f \u2192 \u2261.refl) \u27e9\n \u03a3F I J (\u03bb f \u2192 \u03a3' J (\u03bb j \u2192 \u03a0' (suc I) (\u03bb i \u2192 F i (extend (\u03bcFinSuc J) j f i))))\n \u2261\u27e8 search-swap (\u03bcF I J) \u2115+.semigroup (\u03bb f j \u2192 \u03a0' (suc I) (\u03bb i \u2192 F i (extend (\u03bc J) j f i))) {s\u1d2e = sum (\u03bc J)} (sum-hom (\u03bc J)) \u27e9\n \u03a3' J (\u03bb j \u2192 \u03a3F I J (\u03bb f \u2192 \u03a0' (suc I) (\u03bb i \u2192 F i (extend (\u03bcFinSuc J) j f i))))\n \u2261\u27e8 \u2261.refl \u27e9\n \u03a3F (suc I) J (\u03bb f \u2192 \u03a0' (suc I) (\u03bb i \u2192 F i (f i)))\n \u220e\n where\n open \u2261.\u2261-Reasoning\n \u03bc = \u03bcFinSuc\n \u03bcF = \u03bb i j \u2192 \u03bcFun (\u03bc j) {suc i}\n \u03a0' = \u03bb i \u2192 search (\u03bc i) _*_\n \u03a3' = \u03bb i \u2192 search (\u03bc i) _+_\n \u03a3F = \u03bb i j \u2192 search (\u03bcF i j) _+_\n Fj = \u03bb i j \u2192 F (suc i) j\n\n{-\n -- If we want to force non-empty domain\n\n sFun : \u2200 n \u2192 Search (Fin (suc n) \u2192 A)\n sFun zero op f = sA op (f \u2218 const)\n sFun (suc n) op f = sA op (\u03bb x \u2192 sFun n op (f \u2218 extend x))\n\n Ind : \u2200 n \u2192 SearchInd (sFun n)\n Ind zero P P\u2219 Pf = search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (f \u2218 const))) P\u2219 (Pf \u2218 const)\n Ind (suc n) P P\u2219 Pf = search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (\u03bb x \u2192 sFun n op (f \u2218 extend x)))) \n P\u2219 \n (\u03bb x \u2192 Ind n (\u03bb sf \u2192 P (\u03bb op f \u2192 sf op (f \u2218 extend x))) P\u2219 (Pf \u2218 extend x))\n\n-}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"46c02ef522b5957f407dbf9523e47826963a24e1","subject":"TaggedDeltaTypes: Add corollary of correctness theorem\\ ... as well as rename `t is-valid-wrt \u03c1` to `t is-valid-for \u03c1`","message":"TaggedDeltaTypes: Add corollary of correctness theorem\\\n... as well as rename `t is-valid-wrt \u03c1` to `t is-valid-for \u03c1`\n\nOld-commit-hash: 575e637fabc7edce80fd8549273de2fda059f876\n","repos":"inc-lc\/ilc-agda","old_file":"experimental\/TaggedDeltaTypes.agda","new_file":"experimental\/TaggedDeltaTypes.agda","new_contents":"{-\nThe goal of this file is to make the domain of \u0394 types smaller\nso as to be nearer to full abstraction and hopefully close\nenough for the purpose of having explicit nil changes.\n-}\n\nmodule TaggedDeltaTypes where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Nat\nopen import Data.Unit using (\u22a4 ; tt)\nimport Data.Integer as \u2124\nimport Data.Product as Product\nopen import Data.Product using (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\nimport Level\n\n---------------------------------------------------------\n-- Postulates: Extensionality and bag properties (#55) --\n---------------------------------------------------------\n\npostulate extensionality : Extensionality Level.zero Level.zero\n-- Instead of:\n-- open import Data.NatBag renaming\n--- (map to mapBag ; empty to emptyBag ; update to updateBag)\n-- open import Data.NatBag.Properties\npostulate Bag : Set\npostulate emptyBag : Bag\npostulate mapBag : (\u2115 \u2192 \u2115) \u2192 Bag \u2192 Bag\npostulate _++_ : Bag \u2192 Bag \u2192 Bag\npostulate _\\\\_ : Bag \u2192 Bag \u2192 Bag\ninfixr 5 _++_\ninfixl 9 _\\\\_\npostulate b++\u2205=b : \u2200 {b : Bag} \u2192 b ++ emptyBag \u2261 b\npostulate b++[d\\\\b]=d : \u2200 {b d} \u2192 b ++ (d \\\\ b) \u2261 d\n\n----------------------------\n-- Useful data structures --\n----------------------------\n\nrecord Quadruple\n (A : Set) (B : A \u2192 Set) (C : (a : A) \u2192 B a \u2192 Set)\n (D : (a : A) \u2192 (b : B a) \u2192 (c : C a b) \u2192 Set): Set where\n constructor cons\n field\n car : A\n cadr : B car\n caddr : C car cadr\n cdddr : D car cadr caddr\n\nopen Quadruple public\n\ncouple : Set \u2192 Set \u2192 Set\ncouple A B = Quadruple A (\u03bb _ \u2192 B) (\u03bb _ _ \u2192 \u22a4) (\u03bb _ _ _ \u2192 \u22a4)\n\ntriple : Set \u2192 Set \u2192 Set \u2192 Set\ntriple A B C = Quadruple A (\u03bb _ \u2192 B) (\u03bb _ _ \u2192 C) (\u03bb _ _ _ \u2192 \u22a4)\n\n------------------------\n-- Syntax of programs --\n------------------------\n\ndata Type : Set where\n nats : Type\n bags : Type\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u03c4 \u0393} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u03c3 \u03c4 \u0393} \u2192 (x : Var \u0393 \u03c3) \u2192 Var (\u03c4 \u2022 \u0393) \u03c3\n\ndata Term : Context -> Type -> Set where\n\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n bag : \u2200 {\u0393} \u2192 (b : Bag) \u2192 Term \u0393 bags\n\n var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4 \u0393} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 Term \u0393 (\u03c3 \u21d2 \u03c4)\n app : \u2200 {\u03c3 \u03c4 \u0393} \u2192 (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) (t : Term \u0393 \u03c3) \u2192 Term \u0393 \u03c4\n\n add : \u2200 {\u0393} \u2192 (s : Term \u0393 nats) \u2192 (t : Term \u0393 nats) \u2192 Term \u0393 nats\n map : \u2200 {\u0393} \u2192 (f : Term \u0393 (nats \u21d2 nats)) \u2192 (b : Term \u0393 bags) \u2192\n Term \u0393 bags\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u0393\u227c\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0393\n\u0393\u227c\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0393 {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u0393\u227c\u0393 {\u0393}\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\nweaken subctx (bag b) = bag b\nweaken subctx (add t\u2081 t\u2082) = add (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (map f b) = map (weaken subctx f) (weaken subctx b)\n\n---------------------------\n-- Semantics of programs --\n---------------------------\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 nats \u27e7Type = \u2115\n\u27e6 bags \u27e7Type = Bag\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nidentity-weakening : \u2200 {\u0393} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192 \u27e6 \u0393\u227c\u0393 {\u0393} \u27e7 \u03c1 \u2261 \u03c1\nidentity-weakening {\u2205} {\u2205} = refl\nidentity-weakening {\u03c4 \u2022 \u0393} {v \u2022 \u03c1} =\n cong\u2082 _\u2022_ {x = v} refl (identity-weakening {\u0393} {\u03c1})\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\u27e6 bag b \u27e7Term \u03c1 = b\n\u27e6 add m n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 map f b \u27e7Term \u03c1 = mapBag (\u27e6 f \u27e7Term \u03c1) (\u27e6 b \u27e7Term \u03c1)\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n_\u27e8$\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\n\n-- infix 0 $ in Haskell\ninfixl 0 _\u27e8$\u27e9_\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = weaken-sound t\u2081 \u03c1 \u27e8$\u27e9 weaken-sound t\u2082 \u03c1\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (bag b) \u03c1 = refl\nweaken-sound (add m n) \u03c1 = cong\u2082 _+_ (weaken-sound m \u03c1) (weaken-sound n \u03c1)\nweaken-sound (map f b) \u03c1 =\n cong\u2082 mapBag (weaken-sound f \u03c1) (weaken-sound b \u03c1)\n\n-----------------------\n-- Syntax of changes --\n-----------------------\n\n-- Validity proofs are not literally embedded in terms.\n-- They are introduced and checked at interpretation time.\n-- Invalid programs are well-formed terms,\n-- they just denote the empty function.\n--\n-- Thus do we avoid the horrible mutual recursions between\n-- the syntax and semantics of changes and between the\n-- program transformation and its correctness, which drives\n-- the type checker to thrashing.\n\ndata \u0394Term : Context \u2192 Type \u2192 Set where\n -- changes to numbers are replacement pairs\n \u0394nat : \u2200 {\u0393} \u2192 (old : \u2115) \u2192 (new : \u2115) \u2192 \u0394Term \u0393 nats\n -- changes to bags are bags\n \u0394bag : \u2200 {\u0393} \u2192 (db : Bag) \u2192 \u0394Term \u0393 bags\n -- changes to variables are variables\n \u0394var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 \u0394Term \u0393 \u03c4\n -- changes to abstractions are binders of x and dx\n \u0394abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (dt : \u0394Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192\n \u0394Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n -- changes to applications are applications of a value and a change\n \u0394app : \u2200 {\u03c3 \u03c4 \u0393}\n (ds : \u0394Term \u0393 (\u03c3 \u21d2 \u03c4))\n ( t : Term \u0393 \u03c3)\n (dt : \u0394Term \u0393 \u03c3) \u2192\n \u0394Term \u0393 \u03c4\n -- changes to addition are changes to their components\n \u0394add : \u2200 {\u0393}\n (ds : \u0394Term \u0393 nats)\n (dt : \u0394Term \u0393 nats) \u2192\n \u0394Term \u0393 nats\n -- There are two kinds of changes to maps:\n -- 0. recomputation,\n -- 1. mapping over changes,\n -- the latter used only with some form of isNil available.\n \u0394map\u2080 : \u2200 {\u0393}\n ( s : Term \u0393 (nats \u21d2 nats))\n (ds : \u0394Term \u0393 (nats \u21d2 nats))\n ( t : Term \u0393 bags)\n (dt : \u0394Term \u0393 bags) \u2192\n \u0394Term \u0393 bags\n \u0394map\u2081 : \u2200 {\u0393}\n ( s : Term \u0393 (nats \u21d2 nats))\n (dt : \u0394Term \u0393 bags) \u2192\n \u0394Term \u0393 bags\n\n---------------------------------\n-- Semantic domains of changes --\n---------------------------------\n\n\u0394Val : Type \u2192 Set\n\u0394Env : Context \u2192 Set\nvalid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\n\n-- \u0394Val : Type \u2192 Set\n\u0394Val nats = \u2115 \u00d7 \u2115\n\u0394Val bags = Bag\n\u0394Val (\u03c3 \u21d2 \u03c4) = (v : \u27e6 \u03c3 \u27e7) \u2192 (dv : \u0394Val \u03c3) \u2192 valid v dv \u2192 \u0394Val \u03c4\n\n-- \u0394Env : Context \u2192 Set\n\u0394Env \u2205 = EmptySet\n\u0394Env (\u03c4 \u2022 \u0393) = Quadruple\n \u27e6 \u03c4 \u27e7\n (\u03bb _ \u2192 \u0394Val \u03c4)\n (\u03bb v dv \u2192 valid v dv)\n (\u03bb _ _ _ \u2192 \u0394Env \u0393)\n\n_\u2295_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n_\u229d_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\ninfixl 6 _\u2295_ _\u229d_ -- as with + - in GHC.Num\n\n-- valid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\nvalid {nats} n dn = n \u2261 proj\u2081 dn\nvalid {bags} b db = \u22a4\nvalid {\u03c3 \u21d2 \u03c4} f df =\n \u2200 (v : \u27e6 \u03c3 \u27e7) (dv : \u0394Val \u03c3) (R[v,dv] : valid v dv)\n \u2192 valid (f v) (df v dv R[v,dv])\n \u00d7 (f \u2295 df) (v \u2295 dv) \u2261 f v \u2295 df v dv R[v,dv]\n\nR[v,u\u229dv] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229d v)\n\n-- _\u2295_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n_\u2295_ {nats} n dn = proj\u2082 dn\n_\u2295_ {bags} b db = b ++ db\n_\u2295_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u2295 df v (v \u229d v) R[v,u\u229dv]\n\n-- _\u229d_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\n_\u229d_ {nats} m n = (n , m)\n_\u229d_ {bags} b d = b \\\\ d\n_\u229d_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb v dv R[v,dv] \u2192 f (v \u2295 dv) \u229d g v\n\nv\u2295[u\u229dv]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 v \u2295 (u \u229d v) \u2261 u\nv\u2295[u\u229dv]=u {nats} {u} {v} = refl\nv\u2295[u\u229dv]=u {bags} {u} {v} = b++[d\\\\b]=d {v} {u}\nv\u2295[u\u229dv]=u {\u03c3 \u21d2 \u03c4} {u} {v} = extensionality (\u03bb w \u2192\n begin\n (v \u2295 (u \u229d v)) w\n \u2261\u27e8 refl \u27e9 -- for clarity\n v w \u2295 (u (w \u2295 (w \u229d w)) \u229d v w)\n \u2261\u27e8 cong (\u03bb hole \u2192 v w \u2295 (u hole \u229d v w)) v\u2295[u\u229dv]=u \u27e9\n v w \u2295 (u w \u229d v w)\n \u2261\u27e8 v\u2295[u\u229dv]=u \u27e9\n u w\n \u220e) where open \u2261-Reasoning\n\n-- R[v,u\u229dv] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229d v)\nR[v,u\u229dv] {nats} {u} {v} = refl\nR[v,u\u229dv] {bags} {u} {v} = tt\nR[v,u\u229dv] {\u03c3 \u21d2 \u03c4} {u} {v} = \u03bb w dw R[w,dw] \u2192\n let\n w\u2032 = w \u2295 dw\n in\n R[v,u\u229dv]\n ,\n (begin\n (v \u2295 (u \u229d v)) w\u2032\n \u2261\u27e8 cong (\u03bb hole \u2192 hole w\u2032) (v\u2295[u\u229dv]=u {u = u} {v}) \u27e9\n u w\u2032\n \u2261\u27e8 sym (v\u2295[u\u229dv]=u {u = u w\u2032} {v w}) \u27e9\n v w \u2295 (u \u229d v) w dw R[w,dw]\n \u220e) where open \u2261-Reasoning\n\nignore : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394Env \u0393) \u2192 \u27e6 \u0393 \u27e7\nignore {\u2205} \u03c1 = \u2205\nignore {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) = v \u2022 ignore \u03c1\n\nupdate : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394Env \u0393) \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u03c1 = \u2205\nupdate {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) = (v \u2295 dv) \u2022 update \u03c1\n\n--------------------------\n-- Semantics of changes --\n--------------------------\n\n\u27e6_\u27e7\u0394Var : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 \u0394Env \u0393 \u2192 \u0394Val \u03c4\n\u27e6 this \u27e7\u0394Var (cons v dv R[v,dv] \u03c1) = dv\n\u27e6 that x \u27e7\u0394Var (cons v dv R[v,dv] \u03c1) = \u27e6 x \u27e7\u0394Var \u03c1\n\n_is-valid-for_ : \u2200 {\u03c4 \u0393} \u2192 \u0394Term \u0393 \u03c4 \u2192 \u0394Env \u0393 \u2192 Set\n\n\u27e6_\u27e7\u0394 : \u2200 {\u03c4 \u0393} \u2192\n (t : \u0394Term \u0393 \u03c4) \u2192 (\u03c1 : \u0394Env \u0393) \u2192 t is-valid-for \u03c1 \u2192\n \u0394Val \u03c4\n\n-- _is-valid-for_ : \u2200 {\u03c4 \u0393} \u2192 \u0394Term \u0393 \u03c4 \u2192 \u0394Env \u0393 \u2192 Set\n\u0394nat old new is-valid-for \u03c1 = \u22a4\n\u0394bag db is-valid-for \u03c1 = \u22a4\n\u0394var x is-valid-for \u03c1 = \u22a4\n\n_is-valid-for_ {\u03c3 \u21d2 \u03c4} (\u0394abs dt) \u03c1 =\n (v : \u27e6 \u03c3 \u27e7) (dv : \u0394Val \u03c3) (R[v,dv] : valid v dv) \u2192\n _is-valid-for_ dt (cons v dv R[v,dv] \u03c1)\n\n\u0394app ds t dt is-valid-for \u03c1 = Quadruple\n (ds is-valid-for \u03c1)\n (\u03bb _ \u2192 dt is-valid-for \u03c1)\n (\u03bb _ v-dt \u2192 valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1 v-dt))\n (\u03bb _ _ _ \u2192 \u22a4)\n\n\u0394add ds dt is-valid-for \u03c1 = couple\n (ds is-valid-for \u03c1)\n (dt is-valid-for \u03c1)\n\n\u0394map\u2080 s ds t dt is-valid-for \u03c1 = couple\n (ds is-valid-for \u03c1)\n (dt is-valid-for \u03c1)\n_is-valid-for_ (\u0394map\u2081 s db) \u03c1 = db is-valid-for \u03c1\n\n\u27e6 \u0394nat old new \u27e7\u0394 \u03c1 tt = old , new\n\u27e6 \u0394bag db \u27e7\u0394 \u03c1 tt = db\n\u27e6 \u0394var x \u27e7\u0394 \u03c1 tt = \u27e6 x \u27e7\u0394Var \u03c1\n\n\u27e6 \u0394abs dt \u27e7\u0394 \u03c1 make-valid = \u03bb v dv R[v,dv] \u2192\n \u27e6 dt \u27e7\u0394 (cons v dv R[v,dv] \u03c1) (make-valid v dv R[v,dv])\n\n\u27e6 \u0394app ds t dt \u27e7\u0394 \u03c1 (cons v-ds v-dt R[ds,dt] _) =\n \u27e6 ds \u27e7\u0394 \u03c1 v-ds (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1 v-dt) R[ds,dt]\n\n\u27e6 \u0394add ds dt \u27e7\u0394 \u03c1 (cons v-ds v-dt _ _) =\n let\n (old-s , new-s) = \u27e6 ds \u27e7\u0394 \u03c1 v-ds\n (old-t , new-t) = \u27e6 dt \u27e7\u0394 \u03c1 v-dt\n in\n (old-s + old-t , new-s + new-t)\n\n\u27e6 \u0394map\u2080 s ds t dt \u27e7\u0394 \u03c1 (cons v-ds v-dt _ _) =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n v = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 ds \u27e7\u0394 \u03c1 v-ds\n dv = \u27e6 dt \u27e7\u0394 \u03c1 v-dt\n in\n mapBag (f \u2295 df) (v \u2295 dv) \u229d mapBag f v\n\n\u27e6 \u0394map\u2081 s dt \u27e7\u0394 \u03c1 v-dt = mapBag (\u27e6 s \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1 v-dt)\n\n-- Minor issue about concrete syntax\n--\n-- Because \u27e6_\u27e7\u0394 have dependently typed arguments,\n-- we can't make it an instance of the Meaning\n-- type class and can't use \u27e6_\u27e7 on \u0394Terms.\n--\n-- Error message:\n-- Cannot instantiate the metavariable _872 to solution ((\u03c1 : \u0394Env .\u0393)\n-- \u2192 t is-valid-for \u03c1 \u2192 \u0394Val .\u03c4) since it contains the variable t\n-- which is not in scope of the metavariable or irrelevant in the\n-- metavariable but relevant in the solution\n-- when checking that the expression \u27e6_\u27e7\u0394 has type\n-- \u0394Term .\u0393 .\u03c4 \u2192 _Semantics_872\n--\n-- meaning-\u0394Term : \u2200 {\u03c4 \u0393} \u2192 Meaning (\u0394Term \u0393 \u03c4)\n-- meaning-\u0394Term = meaning \u27e6_\u27e7\u0394\n\n----------------------------\n-- Program transformation --\n----------------------------\n\nderive : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394Term \u0393 \u03c4\n\nderive (nat n) = \u0394nat n n\nderive (bag b) = \u0394bag emptyBag\nderive (var x) = \u0394var x\nderive (abs t) = \u0394abs (derive t)\nderive (app s t) = \u0394app (derive s) t (derive t)\nderive (add s t) = \u0394add (derive s) (derive t)\nderive (map f b) = \u0394map\u2080 f (derive f) b (derive b)\n\n-----------------\n-- Correctness --\n-----------------\n\nunrestricted : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) {\u03c1 : \u0394Env \u0393} \u2192\n derive t is-valid-for \u03c1\n\nvalidity : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t))\n\ncorrectness : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393}\n \u2192 \u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n \u2261 \u27e6 t \u27e7 (update \u03c1)\n\n-- Corollary: (f \u2295 df) (v \u2295 dv) = f v \u2295 df v dv\n\ncorollary : \u2200 {\u03c3 \u03c4 \u0393}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) (t : Term \u0393 \u03c3) {\u03c1 : \u0394Env \u0393} \u2192\n (\u27e6 s \u27e7 (ignore \u03c1) \u2295 \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s))\n (\u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t))\n \u2261 \u27e6 s \u27e7 (ignore \u03c1) (\u27e6 t \u27e7 (ignore \u03c1)) \u2295\n \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s) (\u27e6 t \u27e7 (ignore \u03c1))\n (\u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)) (validity {t = t})\n\ncorollary s t {\u03c1} = proj\u2082\n (validity {t = s} (\u27e6 t \u27e7 (ignore \u03c1))\n (\u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)) (validity {t = t}))\n\nunrestricted (nat n) = tt\nunrestricted (bag b) = tt\nunrestricted (var x) {\u03c1} = tt\nunrestricted (abs t) {\u03c1} = (\u03bb _ _ _ \u2192 unrestricted t)\nunrestricted (app s t) {\u03c1} = cons\n (unrestricted (s))\n (unrestricted (t))\n (validity {t = t}) tt\nunrestricted (add s t) {\u03c1} = cons\n (unrestricted (s))\n (unrestricted (t)) tt tt\nunrestricted (map s t) {\u03c1} = cons\n (unrestricted (s))\n (unrestricted (t)) tt tt\n\nvalidity-var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} \u2192 valid (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 x \u27e7\u0394Var \u03c1)\n\nvalidity-var this {cons v dv R[v,dv] \u03c1} = R[v,dv]\nvalidity-var (that x) {cons v dv R[v,dv] \u03c1} = validity-var x\n\nvalidity {t = nat n} = refl\nvalidity {t = bag b} = tt\nvalidity {t = var x} = validity-var x\nvalidity {t = map f b} = tt\nvalidity {t = add s t} = cong\u2082 _+_ (validity {t = s}) (validity {t = t})\n\nvalidity {t = app s t} {\u03c1} =\n let\n v = \u27e6 t \u27e7 (ignore \u03c1)\n dv = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n in\n proj\u2081 (validity {t = s} {\u03c1} v dv (validity {t = t} {\u03c1}))\n\nvalidity {t = abs t} {\u03c1} = \u03bb v dv R[v,dv] \u2192\n let\n v\u2032 = v \u2295 dv\n dv\u2032 = v\u2032 \u229d v\u2032\n \u03c1\u2081 = cons v dv R[v,dv] \u03c1\n \u03c1\u2082 = cons v\u2032 dv\u2032 R[v,u\u229dv] \u03c1\n in\n validity {t = t} {\u03c1\u2081}\n ,\n (begin\n \u27e6 t \u27e7 (ignore \u03c1\u2082) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1\u2082 (unrestricted t)\n \u2261\u27e8 correctness {t = t} {\u03c1\u2082} \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2082)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 (hole \u2022 update \u03c1)) v\u2295[u\u229dv]=u \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2081)\n \u2261\u27e8 sym (correctness {t = t} {\u03c1\u2081}) \u27e9\n \u27e6 t \u27e7 (ignore \u03c1\u2081) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1\u2081 (unrestricted t)\n \u220e) where open \u2261-Reasoning\n\ncorrectVar : \u2200 {\u03c4 \u0393} {x : Var \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n \u27e6 x \u27e7 (ignore \u03c1) \u2295 \u27e6 x \u27e7\u0394Var \u03c1 \u2261 \u27e6 x \u27e7 (update \u03c1)\n\ncorrectVar {x = this } {cons v dv R[v,dv] \u03c1} = refl\ncorrectVar {x = that y} {cons v dv R[v,dv] \u03c1} = correctVar {x = y} {\u03c1}\n\ncorrectness {t = nat n} = refl\ncorrectness {t = bag b} = b++\u2205=b\ncorrectness {t = var x} = correctVar {x = x}\n\ncorrectness {t = add s t} =\n cong\u2082 _+_ (correctness {t = s}) (correctness {t = t})\n\ncorrectness {t = map s t} {\u03c1} =\n trans (b++[d\\\\b]=d {mapBag f b} {mapBag (f \u2295 df) (b \u2295 db)})\n (cong\u2082 mapBag (correctness {t = s}) (correctness {t = t}))\n where\n f = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n db = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n\ncorrectness {t = app s t} {\u03c1} =\n let\n v = \u27e6 t \u27e7 (ignore \u03c1)\n dv = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n in trans\n (sym (proj\u2082 (validity {t = s} {\u03c1} v dv (validity {t = t} {\u03c1}))))\n (correctness {t = s} \u27e8$\u27e9 correctness {t = t})\n\ncorrectness {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u0393} {abs t} {\u03c1} = extensionality (\u03bb v \u2192\n let\n \u03c1\u2032 : \u0394Env (\u03c4\u2081 \u2022 \u0393)\n \u03c1\u2032 = cons v (v \u229d v) R[v,u\u229dv] \u03c1\n in\n begin\n \u27e6 t \u27e7 (ignore \u03c1\u2032) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1\u2032 (unrestricted t)\n \u2261\u27e8 correctness {t = t} {\u03c1\u2032} \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2032)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 (hole \u2022 update \u03c1)) v\u2295[u\u229dv]=u \u27e9\n \u27e6 t \u27e7 (v \u2022 update \u03c1)\n \u220e\n ) where open \u2261-Reasoning\n","old_contents":"{-\nThe goal of this file is to make the domain of \u0394 types smaller\nso as to be nearer to full abstraction and hopefully close\nenough for the purpose of having explicit nil changes.\n-}\n\nmodule TaggedDeltaTypes where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Nat\nopen import Data.Unit using (\u22a4 ; tt)\nimport Data.Integer as \u2124\nimport Data.Product as Product\nopen import Data.Product using (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\nimport Level\n\n---------------------------------------------------------\n-- Postulates: Extensionality and bag properties (#55) --\n---------------------------------------------------------\n\npostulate extensionality : Extensionality Level.zero Level.zero\n-- Instead of:\n-- open import Data.NatBag renaming\n--- (map to mapBag ; empty to emptyBag ; update to updateBag)\n-- open import Data.NatBag.Properties\npostulate Bag : Set\npostulate emptyBag : Bag\npostulate mapBag : (\u2115 \u2192 \u2115) \u2192 Bag \u2192 Bag\npostulate _++_ : Bag \u2192 Bag \u2192 Bag\npostulate _\\\\_ : Bag \u2192 Bag \u2192 Bag\ninfixr 5 _++_\ninfixl 9 _\\\\_\npostulate b++\u2205=b : \u2200 {b : Bag} \u2192 b ++ emptyBag \u2261 b\npostulate b++[d\\\\b]=d : \u2200 {b d} \u2192 b ++ (d \\\\ b) \u2261 d\n\n----------------------------\n-- Useful data structures --\n----------------------------\n\nrecord Quadruple\n (A : Set) (B : A \u2192 Set) (C : (a : A) \u2192 B a \u2192 Set)\n (D : (a : A) \u2192 (b : B a) \u2192 (c : C a b) \u2192 Set): Set where\n constructor cons\n field\n car : A\n cadr : B car\n caddr : C car cadr\n cdddr : D car cadr caddr\n\nopen Quadruple public\n\ncouple : Set \u2192 Set \u2192 Set\ncouple A B = Quadruple A (\u03bb _ \u2192 B) (\u03bb _ _ \u2192 \u22a4) (\u03bb _ _ _ \u2192 \u22a4)\n\ntriple : Set \u2192 Set \u2192 Set \u2192 Set\ntriple A B C = Quadruple A (\u03bb _ \u2192 B) (\u03bb _ _ \u2192 C) (\u03bb _ _ _ \u2192 \u22a4)\n\n------------------------\n-- Syntax of programs --\n------------------------\n\ndata Type : Set where\n nats : Type\n bags : Type\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u03c4 \u0393} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u03c3 \u03c4 \u0393} \u2192 (x : Var \u0393 \u03c3) \u2192 Var (\u03c4 \u2022 \u0393) \u03c3\n\ndata Term : Context -> Type -> Set where\n\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n bag : \u2200 {\u0393} \u2192 (b : Bag) \u2192 Term \u0393 bags\n\n var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4 \u0393} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 Term \u0393 (\u03c3 \u21d2 \u03c4)\n app : \u2200 {\u03c3 \u03c4 \u0393} \u2192 (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) (t : Term \u0393 \u03c3) \u2192 Term \u0393 \u03c4\n\n add : \u2200 {\u0393} \u2192 (s : Term \u0393 nats) \u2192 (t : Term \u0393 nats) \u2192 Term \u0393 nats\n map : \u2200 {\u0393} \u2192 (f : Term \u0393 (nats \u21d2 nats)) \u2192 (b : Term \u0393 bags) \u2192\n Term \u0393 bags\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u0393\u227c\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0393\n\u0393\u227c\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0393 {\u03c4 \u2022 \u0393} = keep \u03c4 \u2022 \u0393\u227c\u0393 {\u0393}\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\nweaken subctx (bag b) = bag b\nweaken subctx (add t\u2081 t\u2082) = add (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (map f b) = map (weaken subctx f) (weaken subctx b)\n\n---------------------------\n-- Semantics of programs --\n---------------------------\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 nats \u27e7Type = \u2115\n\u27e6 bags \u27e7Type = Bag\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nidentity-weakening : \u2200 {\u0393} {\u03c1 : \u27e6 \u0393 \u27e7} \u2192 \u27e6 \u0393\u227c\u0393 {\u0393} \u27e7 \u03c1 \u2261 \u03c1\nidentity-weakening {\u2205} {\u2205} = refl\nidentity-weakening {\u03c4 \u2022 \u0393} {v \u2022 \u03c1} =\n cong\u2082 _\u2022_ {x = v} refl (identity-weakening {\u0393} {\u03c1})\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\u27e6 bag b \u27e7Term \u03c1 = b\n\u27e6 add m n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 map f b \u27e7Term \u03c1 = mapBag (\u27e6 f \u27e7Term \u03c1) (\u27e6 b \u27e7Term \u03c1)\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n_\u27e8$\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\n\n-- infix 0 $ in Haskell\ninfixl 0 _\u27e8$\u27e9_\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = weaken-sound t\u2081 \u03c1 \u27e8$\u27e9 weaken-sound t\u2082 \u03c1\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\nweaken-sound (bag b) \u03c1 = refl\nweaken-sound (add m n) \u03c1 = cong\u2082 _+_ (weaken-sound m \u03c1) (weaken-sound n \u03c1)\nweaken-sound (map f b) \u03c1 =\n cong\u2082 mapBag (weaken-sound f \u03c1) (weaken-sound b \u03c1)\n\n-----------------------\n-- Syntax of changes --\n-----------------------\n\n-- Validity proofs are not literally embedded in terms.\n-- They are introduced and checked at interpretation time.\n-- Invalid programs are well-formed terms,\n-- they just denote the empty function.\n--\n-- Thus do we avoid the horrible mutual recursions between\n-- the syntax and semantics of changes and between the\n-- program transformation and its correctness, which drives\n-- the type checker to thrashing.\n\ndata \u0394Term : Context \u2192 Type \u2192 Set where\n -- changes to numbers are replacement pairs\n \u0394nat : \u2200 {\u0393} \u2192 (old : \u2115) \u2192 (new : \u2115) \u2192 \u0394Term \u0393 nats\n -- changes to bags are bags\n \u0394bag : \u2200 {\u0393} \u2192 (db : Bag) \u2192 \u0394Term \u0393 bags\n -- changes to variables are variables\n \u0394var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192 \u0394Term \u0393 \u03c4\n -- changes to abstractions are binders of x and dx\n \u0394abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (dt : \u0394Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192\n \u0394Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n -- changes to applications are applications of a value and a change\n \u0394app : \u2200 {\u03c3 \u03c4 \u0393}\n (ds : \u0394Term \u0393 (\u03c3 \u21d2 \u03c4))\n ( t : Term \u0393 \u03c3)\n (dt : \u0394Term \u0393 \u03c3) \u2192\n \u0394Term \u0393 \u03c4\n -- changes to addition are changes to their components\n \u0394add : \u2200 {\u0393}\n (ds : \u0394Term \u0393 nats)\n (dt : \u0394Term \u0393 nats) \u2192\n \u0394Term \u0393 nats\n -- There are two kinds of changes to maps:\n -- 0. recomputation,\n -- 1. mapping over changes,\n -- the latter used only with some form of isNil available.\n \u0394map\u2080 : \u2200 {\u0393}\n ( s : Term \u0393 (nats \u21d2 nats))\n (ds : \u0394Term \u0393 (nats \u21d2 nats))\n ( t : Term \u0393 bags)\n (dt : \u0394Term \u0393 bags) \u2192\n \u0394Term \u0393 bags\n \u0394map\u2081 : \u2200 {\u0393}\n ( s : Term \u0393 (nats \u21d2 nats))\n (dt : \u0394Term \u0393 bags) \u2192\n \u0394Term \u0393 bags\n\n---------------------------------\n-- Semantic domains of changes --\n---------------------------------\n\n\u0394Val : Type \u2192 Set\n\u0394Env : Context \u2192 Set\nvalid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\n\n-- \u0394Val : Type \u2192 Set\n\u0394Val nats = \u2115 \u00d7 \u2115\n\u0394Val bags = Bag\n\u0394Val (\u03c3 \u21d2 \u03c4) = (v : \u27e6 \u03c3 \u27e7) \u2192 (dv : \u0394Val \u03c3) \u2192 valid v dv \u2192 \u0394Val \u03c4\n\n-- \u0394Env : Context \u2192 Set\n\u0394Env \u2205 = EmptySet\n\u0394Env (\u03c4 \u2022 \u0393) = Quadruple\n \u27e6 \u03c4 \u27e7\n (\u03bb _ \u2192 \u0394Val \u03c4)\n (\u03bb v dv \u2192 valid v dv)\n (\u03bb _ _ _ \u2192 \u0394Env \u0393)\n\n_\u2295_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n_\u229d_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\ninfixl 6 _\u2295_ _\u229d_ -- as with + - in GHC.Num\n\n-- valid : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 Set\nvalid {nats} n dn = n \u2261 proj\u2081 dn\nvalid {bags} b db = \u22a4\nvalid {\u03c3 \u21d2 \u03c4} f df =\n \u2200 (v : \u27e6 \u03c3 \u27e7) (dv : \u0394Val \u03c3) (R[v,dv] : valid v dv)\n \u2192 valid (f v) (df v dv R[v,dv])\n \u00d7 (f \u2295 df) (v \u2295 dv) \u2261 f v \u2295 df v dv R[v,dv]\n\nR[v,u\u229dv] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229d v)\n\n-- _\u2295_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n_\u2295_ {nats} n dn = proj\u2082 dn\n_\u2295_ {bags} b db = b ++ db\n_\u2295_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u2295 df v (v \u229d v) R[v,u\u229dv]\n\n-- _\u229d_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u0394Val \u03c4\n_\u229d_ {nats} m n = (n , m)\n_\u229d_ {bags} b d = b \\\\ d\n_\u229d_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb v dv R[v,dv] \u2192 f (v \u2295 dv) \u229d g v\n\nv\u2295[u\u229dv]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 v \u2295 (u \u229d v) \u2261 u\nv\u2295[u\u229dv]=u {nats} {u} {v} = refl\nv\u2295[u\u229dv]=u {bags} {u} {v} = b++[d\\\\b]=d {v} {u}\nv\u2295[u\u229dv]=u {\u03c3 \u21d2 \u03c4} {u} {v} = extensionality (\u03bb w \u2192\n begin\n (v \u2295 (u \u229d v)) w\n \u2261\u27e8 refl \u27e9 -- for clarity\n v w \u2295 (u (w \u2295 (w \u229d w)) \u229d v w)\n \u2261\u27e8 cong (\u03bb hole \u2192 v w \u2295 (u hole \u229d v w)) v\u2295[u\u229dv]=u \u27e9\n v w \u2295 (u w \u229d v w)\n \u2261\u27e8 v\u2295[u\u229dv]=u \u27e9\n u w\n \u220e) where open \u2261-Reasoning\n\n-- R[v,u\u229dv] : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 valid {\u03c4} v (u \u229d v)\nR[v,u\u229dv] {nats} {u} {v} = refl\nR[v,u\u229dv] {bags} {u} {v} = tt\nR[v,u\u229dv] {\u03c3 \u21d2 \u03c4} {u} {v} = \u03bb w dw R[w,dw] \u2192\n let\n w\u2032 = w \u2295 dw\n in\n R[v,u\u229dv]\n ,\n (begin\n (v \u2295 (u \u229d v)) w\u2032\n \u2261\u27e8 cong (\u03bb hole \u2192 hole w\u2032) (v\u2295[u\u229dv]=u {u = u} {v}) \u27e9\n u w\u2032\n \u2261\u27e8 sym (v\u2295[u\u229dv]=u {u = u w\u2032} {v w}) \u27e9\n v w \u2295 (u \u229d v) w dw R[w,dw]\n \u220e) where open \u2261-Reasoning\n\nignore : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394Env \u0393) \u2192 \u27e6 \u0393 \u27e7\nignore {\u2205} \u03c1 = \u2205\nignore {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) = v \u2022 ignore \u03c1\n\nupdate : \u2200 {\u0393 : Context} \u2192 (\u03c1 : \u0394Env \u0393) \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u03c1 = \u2205\nupdate {\u03c4 \u2022 \u0393} (cons v dv R[v,dv] \u03c1) = (v \u2295 dv) \u2022 update \u03c1\n\n--------------------------\n-- Semantics of changes --\n--------------------------\n\n\u27e6_\u27e7\u0394Var : \u2200 {\u03c4 \u0393} \u2192 Var \u0393 \u03c4 \u2192 \u0394Env \u0393 \u2192 \u0394Val \u03c4\n\u27e6 this \u27e7\u0394Var (cons v dv R[v,dv] \u03c1) = dv\n\u27e6 that x \u27e7\u0394Var (cons v dv R[v,dv] \u03c1) = \u27e6 x \u27e7\u0394Var \u03c1\n\n_is-valid-wrt_ : \u2200 {\u03c4 \u0393} \u2192 \u0394Term \u0393 \u03c4 \u2192 \u0394Env \u0393 \u2192 Set\n\n\u27e6_\u27e7\u0394 : \u2200 {\u03c4 \u0393} \u2192\n (t : \u0394Term \u0393 \u03c4) \u2192 (\u03c1 : \u0394Env \u0393) \u2192 t is-valid-wrt \u03c1 \u2192\n \u0394Val \u03c4\n\n-- _is-valid-wrt_ : \u2200 {\u03c4 \u0393} \u2192 \u0394Term \u0393 \u03c4 \u2192 \u0394Env \u0393 \u2192 Set\n\u0394nat old new is-valid-wrt \u03c1 = \u22a4\n\u0394bag db is-valid-wrt \u03c1 = \u22a4\n\u0394var x is-valid-wrt \u03c1 = \u22a4\n\n_is-valid-wrt_ {\u03c3 \u21d2 \u03c4} (\u0394abs dt) \u03c1 =\n (v : \u27e6 \u03c3 \u27e7) (dv : \u0394Val \u03c3) (R[v,dv] : valid v dv) \u2192\n _is-valid-wrt_ dt (cons v dv R[v,dv] \u03c1)\n\n\u0394app ds t dt is-valid-wrt \u03c1 = Quadruple\n (ds is-valid-wrt \u03c1)\n (\u03bb _ \u2192 dt is-valid-wrt \u03c1)\n (\u03bb _ v-dt \u2192 valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1 v-dt))\n (\u03bb _ _ _ \u2192 \u22a4)\n\n\u0394add ds dt is-valid-wrt \u03c1 = couple\n (ds is-valid-wrt \u03c1)\n (dt is-valid-wrt \u03c1)\n\n\u0394map\u2080 s ds t dt is-valid-wrt \u03c1 = couple\n (ds is-valid-wrt \u03c1)\n (dt is-valid-wrt \u03c1)\n_is-valid-wrt_ (\u0394map\u2081 s db) \u03c1 = db is-valid-wrt \u03c1\n\n\u27e6 \u0394nat old new \u27e7\u0394 \u03c1 tt = old , new\n\u27e6 \u0394bag db \u27e7\u0394 \u03c1 tt = db\n\u27e6 \u0394var x \u27e7\u0394 \u03c1 tt = \u27e6 x \u27e7\u0394Var \u03c1\n\n\u27e6 \u0394abs dt \u27e7\u0394 \u03c1 make-valid = \u03bb v dv R[v,dv] \u2192\n \u27e6 dt \u27e7\u0394 (cons v dv R[v,dv] \u03c1) (make-valid v dv R[v,dv])\n\n\u27e6 \u0394app ds t dt \u27e7\u0394 \u03c1 (cons v-ds v-dt R[ds,dt] _) =\n \u27e6 ds \u27e7\u0394 \u03c1 v-ds (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1 v-dt) R[ds,dt]\n\n\u27e6 \u0394add ds dt \u27e7\u0394 \u03c1 (cons v-ds v-dt _ _) =\n let\n (old-s , new-s) = \u27e6 ds \u27e7\u0394 \u03c1 v-ds\n (old-t , new-t) = \u27e6 dt \u27e7\u0394 \u03c1 v-dt\n in\n (old-s + old-t , new-s + new-t)\n\n\u27e6 \u0394map\u2080 s ds t dt \u27e7\u0394 \u03c1 (cons v-ds v-dt _ _) =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n v = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 ds \u27e7\u0394 \u03c1 v-ds\n dv = \u27e6 dt \u27e7\u0394 \u03c1 v-dt\n in\n mapBag (f \u2295 df) (v \u2295 dv) \u229d mapBag f v\n\n\u27e6 \u0394map\u2081 s dt \u27e7\u0394 \u03c1 v-dt = mapBag (\u27e6 s \u27e7 (ignore \u03c1)) (\u27e6 dt \u27e7\u0394 \u03c1 v-dt)\n\n-- Minor issue about concrete syntax\n--\n-- Because \u27e6_\u27e7\u0394 have dependently typed arguments,\n-- we can't make it an instance of the Meaning\n-- type class and can't use \u27e6_\u27e7 on \u0394Terms.\n--\n-- Error message:\n-- Cannot instantiate the metavariable _872 to solution ((\u03c1 : \u0394Env .\u0393)\n-- \u2192 t is-valid-wrt \u03c1 \u2192 \u0394Val .\u03c4) since it contains the variable t\n-- which is not in scope of the metavariable or irrelevant in the\n-- metavariable but relevant in the solution\n-- when checking that the expression \u27e6_\u27e7\u0394 has type\n-- \u0394Term .\u0393 .\u03c4 \u2192 _Semantics_872\n--\n-- meaning-\u0394Term : \u2200 {\u03c4 \u0393} \u2192 Meaning (\u0394Term \u0393 \u03c4)\n-- meaning-\u0394Term = meaning \u27e6_\u27e7\u0394\n\n----------------------------\n-- Program transformation --\n----------------------------\n\nderive : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394Term \u0393 \u03c4\n\nderive (nat n) = \u0394nat n n\nderive (bag b) = \u0394bag emptyBag\nderive (var x) = \u0394var x\nderive (abs t) = \u0394abs (derive t)\nderive (app s t) = \u0394app (derive s) t (derive t)\nderive (add s t) = \u0394add (derive s) (derive t)\nderive (map f b) = \u0394map\u2080 f (derive f) b (derive b)\n\n-----------------\n-- Correctness --\n-----------------\n\nunrestricted : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n derive t is-valid-wrt \u03c1\n\nvalidity : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted {t = t}))\n\ncorrectness : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393}\n \u2192 \u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted {t = t})\n \u2261 \u27e6 t \u27e7 (update \u03c1)\n\nunrestricted {t = nat n} = tt\nunrestricted {t = bag b} = tt\nunrestricted {t = var x} {\u03c1} = tt\nunrestricted {t = abs t} {\u03c1} = (\u03bb _ _ _ \u2192 unrestricted {t = t})\nunrestricted {t = app s t} {\u03c1} = cons\n (unrestricted {t = s})\n (unrestricted {t = t})\n (validity {t = t}) tt\nunrestricted {t = add s t} {\u03c1} = cons\n (unrestricted {t = s})\n (unrestricted {t = t}) tt tt\nunrestricted {t = map s t} {\u03c1} = cons\n (unrestricted {t = s})\n (unrestricted {t = t}) tt tt\n\nvalidity-var : \u2200 {\u03c4 \u0393} \u2192 (x : Var \u0393 \u03c4) \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} \u2192 valid (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 x \u27e7\u0394Var \u03c1)\n\nvalidity-var this {cons v dv R[v,dv] \u03c1} = R[v,dv]\nvalidity-var (that x) {cons v dv R[v,dv] \u03c1} = validity-var x\n\nvalidity {t = nat n} = refl\nvalidity {t = bag b} = tt\nvalidity {t = var x} = validity-var x\nvalidity {t = map f b} = tt\nvalidity {t = add s t} = cong\u2082 _+_ (validity {t = s}) (validity {t = t})\n\nvalidity {t = app s t} {\u03c1} =\n let\n v = \u27e6 t \u27e7 (ignore \u03c1)\n dv = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted {t = t})\n in\n proj\u2081 (validity {t = s} {\u03c1} v dv (validity {t = t} {\u03c1}))\n\nvalidity {t = abs t} {\u03c1} = \u03bb v dv R[v,dv] \u2192\n let\n v\u2032 = v \u2295 dv\n dv\u2032 = v\u2032 \u229d v\u2032\n \u03c1\u2081 = cons v dv R[v,dv] \u03c1\n \u03c1\u2082 = cons v\u2032 dv\u2032 R[v,u\u229dv] \u03c1\n in\n validity {t = t} {\u03c1\u2081}\n ,\n (begin\n \u27e6 t \u27e7 (ignore \u03c1\u2082) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1\u2082 (unrestricted {t = t})\n \u2261\u27e8 correctness {t = t} {\u03c1\u2082} \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2082)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 (hole \u2022 update \u03c1)) v\u2295[u\u229dv]=u \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2081)\n \u2261\u27e8 sym (correctness {t = t} {\u03c1\u2081}) \u27e9\n \u27e6 t \u27e7 (ignore \u03c1\u2081) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1\u2081 (unrestricted {t = t})\n \u220e) where open \u2261-Reasoning\n\ncorrectVar : \u2200 {\u03c4 \u0393} {x : Var \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n \u27e6 x \u27e7 (ignore \u03c1) \u2295 \u27e6 x \u27e7\u0394Var \u03c1 \u2261 \u27e6 x \u27e7 (update \u03c1)\n\ncorrectVar {x = this } {cons v dv R[v,dv] \u03c1} = refl\ncorrectVar {x = that y} {cons v dv R[v,dv] \u03c1} = correctVar {x = y} {\u03c1}\n\ncorrectness {t = nat n} = refl\ncorrectness {t = bag b} = b++\u2205=b\ncorrectness {t = var x} = correctVar {x = x}\n\ncorrectness {t = add s t} =\n cong\u2082 _+_ (correctness {t = s}) (correctness {t = t})\n\ncorrectness {t = map s t} {\u03c1} =\n trans (b++[d\\\\b]=d {mapBag f b} {mapBag (f \u2295 df) (b \u2295 db)})\n (cong\u2082 mapBag (correctness {t = s}) (correctness {t = t}))\n where\n f = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted {t = s})\n db = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted {t = t})\n\ncorrectness {t = app s t} {\u03c1} =\n let\n v = \u27e6 t \u27e7 (ignore \u03c1)\n dv = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted {t = t})\n in trans\n (sym (proj\u2082 (validity {t = s} {\u03c1} v dv (validity {t = t} {\u03c1}))))\n (correctness {t = s} \u27e8$\u27e9 correctness {t = t})\n\ncorrectness {\u03c4\u2081 \u21d2 \u03c4\u2082} {\u0393} {abs t} {\u03c1} = extensionality (\u03bb v \u2192\n let\n \u03c1\u2032 : \u0394Env (\u03c4\u2081 \u2022 \u0393)\n \u03c1\u2032 = cons v (v \u229d v) R[v,u\u229dv] \u03c1\n in\n begin\n \u27e6 t \u27e7 (ignore \u03c1\u2032) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1\u2032 (unrestricted {t = t})\n \u2261\u27e8 correctness {t = t} {\u03c1\u2032} \u27e9\n \u27e6 t \u27e7 (update \u03c1\u2032)\n \u2261\u27e8 cong (\u03bb hole \u2192 \u27e6 t \u27e7 (hole \u2022 update \u03c1)) v\u2295[u\u229dv]=u \u27e9\n \u27e6 t \u27e7 (v \u2022 update \u03c1)\n \u220e\n ) where open \u2261-Reasoning\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"da122aedbefd1f85da61af8842c203dbcd40a83c","subject":"ok now actually close #1","message":"ok now actually close #1\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"core.agda","new_file":"core.agda","new_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n -- used to mark which dhexp holes have been evaluated\n data mark : Set where\n \u2713 : mark\n \u2717 : mark\n\n mutual\n subst : Set -- todo: no idea if this is right; mutual thing is weird\n subst = dhexp ctx\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 subst \u00d7 mark) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 subst \u00d7 mark) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n <_>_ : htyp \u2192 dhexp \u2192 dhexp\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows, sums, and products\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = htyp ctx\n\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n -- todo: this probably belongs in contexts, but need to abstract it.\n id : tctx \u2192 subst\n id ctx x with ctx x\n id ctx x | Some \u03c4 = Some (X x)\n id ctx x | None = None\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 tctx \u00d7 htyp)\n u ::[ \u0393 ] \u03c4 = u , \u0393 , \u03c4\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {n : Nat} \u2192\n (n , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of n?\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192 -- todo: uniqueness of n?\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- todo: do we care about completeness of hexp or e-umlauts?\n -- those types without holes anywhere\n tcomplete : htyp \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (\u03c41 ==> \u03c42) = tcomplete \u03c41 \u00d7 tcomplete \u03c42\n\n -- those expressions without holes anywhere\n ecomplete : hexp \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: \u03c4) = ecomplete e1 \u00d7 tcomplete \u03c4\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ \u03c4 ] e) = tcomplete \u03c4 \u00d7 ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp1 : \u2200{\u0393 e1 e2 d2 d1 \u03941 \u03c41 \u03c42 \u03942} \u2192\n \u03941 ## \u03942 \u2192\n \u0393 \u22a2 e1 => \u2987\u2988 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u2987\u2988) ~> d1 :: \u03c41 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u2987\u2988 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u2987\u2988 ~> (< \u03c42 ==> \u2987\u2988 > d1) \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESAp2 : \u2200{\u0393 e1 \u03c42 \u03c4 d1 d2 \u03941 \u03942 \u03c42' e2} \u2192\n \u03941 ## \u03942 \u2192\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n (\u03c42 == \u03c42' \u2192 \u22a5) \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 (< \u03c42 > d2) \u22a3 (\u03941 \u222a \u03942)\n ESAp3 : \u2200{\u0393 e1 \u03c4 d1 \u03941 e2 \u03c42 d2 \u03942 } \u2192\n \u03941 ## \u03942 \u2192\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , id \u0393 , \u2717 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , id \u0393 , \u2717 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc1 : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u03c4 == \u03c4' \u2192 \u22a5) \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> (< \u03c4 > d) \u22a3 \u0394\n ESAsc2 : \u2200{\u0393 e \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c41 ==> \u03c42 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EALamHole : \u2200{\u0393 x e \u03c4 d \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u2987\u2988)) \u22a2 e \u21d0 \u2987\u2988 ~> d :: \u03c4 \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u2987\u2988 ~> \u00b7\u03bb x [ \u2987\u2988 ] d :: \u2987\u2988 ==> \u03c4 \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 (e == \u2987\u2988[ u ] \u2192 \u22a5)) \u2192\n ((e' : hexp) (u : Nat) \u2192 (e == \u2987 e' \u2988[ u ] \u2192 \u22a5)) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , id \u0393 , \u2717 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , id \u0393 , \u2717 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n mutual\n -- substitition type assignment\n _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 subst \u2192 tctx \u2192 Set\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' =\n (x : Nat) (d : dhexp) \u2192\n (x , d) \u2208 \u03c3 \u2192\n \u03a3[ \u03c4 \u2208 htyp ] (\u0393' x == Some \u03c4 \u00d7 \u0394 , \u0393 \u22a2 d :: \u03c4)\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c42 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u03c41 \u25b8arr (\u03c42 ==> \u03c4) \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4 m} \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n (\u0394 ,, u ::[ \u0393' ] \u03c4) , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 , m \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 m} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n (\u0394 ,, u ::[ \u0393' ] \u03c4) , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 , m \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c4 \u03c4'} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0394 , \u0393 \u22a2 < \u03c4 > d :: \u03c4\n\n postulate -- todo: write this later\n [_]_ : subst \u2192 dhexp \u2192 dhexp\n\n -- value\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n mutual\n -- indeterminate\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 d1 indet \u2192 d2 final \u2192 (d1 \u2218 d2) indet\n ICast : \u2200{d \u03c4} \u2192 d indet \u2192 (< \u03c4 > d) indet\n\n -- final\n data _final : (d : dhexp) \u2192 Set where\n FVal : \u2200{d} \u2192 d val \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n------------- these two judgements are still being figured out; form\n------------- changing, etc. double check everything here once it settles\n------------- before doing anything with it\n -- error\n data _err[_] : (d : dhexp) \u2192 (\u0394 : hctx) \u2192 Set where\n -- ERNEHole\n -- ERCastError\n -- ERLam\n -- ERAp1\n -- ERAp2\n -- ERCast\n\n -- small step semantics\n data _\u21a6_ : (d d' : dhexp) \u2192 Set where\n STHole : \u2200{ d d' u \u03c3 } \u2192\n d \u21a6 d' \u2192\n \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 \u21a6 \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9\n -- STCast\n STAp1 : \u2200{ d1 d2 d1' } \u2192\n d1 \u21a6 d1' \u2192\n (d1 \u2218 d2) \u21a6 (d1' \u2218 d2)\n STAp2 : \u2200{ d1 d2 d2' } \u2192\n d1 final \u2192\n d2 \u21a6 d2' \u2192\n (d1 \u2218 d2) \u21a6 (d1 \u2218 d2')\n -- STAp\u03b2 : \u2200{ d1 d2 \u03c4 x } \u2192\n -- d2 final \u2192\n -- ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u21a6 ([ \u27e6 x , {!!} \u27e7 ] d2)\n","old_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n -- used to mark which dhexp holes have been evaluated\n data mark : Set where\n \u2713 : mark\n \u2717 : mark\n\n mutual\n subst : Set -- todo: no idea if this is right; mutual thing is weird\n subst = dhexp ctx\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 subst) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 subst \u00d7 mark) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n <_>_ : htyp \u2192 dhexp \u2192 dhexp\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows, sums, and products\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = htyp ctx\n\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n -- todo: this probably belongs in contexts, but need to abstract it.\n id : tctx \u2192 subst\n id ctx x with ctx x\n id ctx x | Some \u03c4 = Some (X x)\n id ctx x | None = None\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 tctx \u00d7 htyp)\n u ::[ \u0393 ] \u03c4 = u , \u0393 , \u03c4\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {n : Nat} \u2192\n (n , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of n?\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192 -- todo: uniqueness of n?\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- todo: do we care about completeness of hexp or e-umlauts?\n -- those types without holes anywhere\n tcomplete : htyp \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (\u03c41 ==> \u03c42) = tcomplete \u03c41 \u00d7 tcomplete \u03c42\n\n -- those expressions without holes anywhere\n ecomplete : hexp \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: \u03c4) = ecomplete e1 \u00d7 tcomplete \u03c4\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ \u03c4 ] e) = tcomplete \u03c4 \u00d7 ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp1 : \u2200{\u0393 e1 e2 d2 d1 \u03941 \u03c41 \u03c42 \u03942} \u2192\n \u03941 ## \u03942 \u2192\n \u0393 \u22a2 e1 => \u2987\u2988 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u2987\u2988) ~> d1 :: \u03c41 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u2987\u2988 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u2987\u2988 ~> (< \u03c42 ==> \u2987\u2988 > d1) \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESAp2 : \u2200{\u0393 e1 \u03c42 \u03c4 d1 d2 \u03941 \u03942 \u03c42' e2} \u2192\n \u03941 ## \u03942 \u2192\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n (\u03c42 == \u03c42' \u2192 \u22a5) \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 (< \u03c42 > d2) \u22a3 (\u03941 \u222a \u03942)\n ESAp3 : \u2200{\u0393 e1 \u03c4 d1 \u03941 e2 \u03c42 d2 \u03942 } \u2192\n \u03941 ## \u03942 \u2192\n \u0393 \u22a2 e1 \u21d2 (\u03c42 ==> \u03c4) ~> d1 \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42 \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> d1 \u2218 d2 \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , id \u0393 , \u2717 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc1 : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u03c4 == \u03c4' \u2192 \u22a5) \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> (< \u03c4 > d) \u22a3 \u0394\n ESAsc2 : \u2200{\u0393 e \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c41 ==> \u03c42 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EALamHole : \u2200{\u0393 x e \u03c4 d \u0394 } \u2192\n (x # \u0393) \u2192\n (\u0393 ,, (x , \u2987\u2988)) \u22a2 e \u21d0 \u2987\u2988 ~> d :: \u03c4 \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u2987\u2988 ~> \u00b7\u03bb x [ \u2987\u2988 ] d :: \u2987\u2988 ==> \u03c4 \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 (e == \u2987\u2988[ u ] \u2192 \u22a5)) \u2192\n ((e' : hexp) (u : Nat) \u2192 (e == \u2987 e' \u2988[ u ] \u2192 \u22a5)) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , id \u0393 , \u2717 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n mutual\n -- substitition type assignment\n _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 subst \u2192 tctx \u2192 Set\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' =\n (x : Nat) (d : dhexp) \u2192\n (x , d) \u2208 \u03c3 \u2192\n \u03a3[ \u03c4 \u2208 htyp ] (\u0393' x == Some \u03c4 \u00d7 \u0394 , \u0393 \u22a2 d :: \u03c4)\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c42 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u03c41 \u25b8arr (\u03c42 ==> \u03c4) \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4} \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n (\u0394 ,, u ::[ \u0393' ] \u03c4) , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n (\u0394 ,, u ::[ \u0393' ] \u03c4) , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 , \u2717 \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c4 \u03c4'} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0394 , \u0393 \u22a2 < \u03c4 > d :: \u03c4\n\n postulate -- todo: write this later\n [_]_ : subst \u2192 dhexp \u2192 dhexp\n\n -- value\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n mutual\n -- indeterminate\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 d1 indet \u2192 d2 final \u2192 (d1 \u2218 d2) indet\n ICast : \u2200{d \u03c4} \u2192 d indet \u2192 (< \u03c4 > d) indet\n\n -- final\n data _final : (d : dhexp) \u2192 Set where\n FVal : \u2200{d} \u2192 d val \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n------------- these two judgements are still being figured out; form\n------------- changing, etc. double check everything here once it settles\n------------- before doing anything with it\n -- error\n data _err[_] : (d : dhexp) \u2192 (\u0394 : hctx) \u2192 Set where\n -- ERNEHole\n -- ERCastError\n -- ERLam\n -- ERAp1\n -- ERAp2\n -- ERCast\n\n -- small step semantics\n data _\u21a6_ : (d d' : dhexp) \u2192 Set where\n STHole : \u2200{ d d' u \u03c3 } \u2192\n d \u21a6 d' \u2192\n \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 \u21a6 \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9\n -- STCast\n STAp1 : \u2200{ d1 d2 d1' } \u2192\n d1 \u21a6 d1' \u2192\n (d1 \u2218 d2) \u21a6 (d1' \u2218 d2)\n STAp2 : \u2200{ d1 d2 d2' } \u2192\n d1 final \u2192\n d2 \u21a6 d2' \u2192\n (d1 \u2218 d2) \u21a6 (d1 \u2218 d2')\n -- STAp\u03b2 : \u2200{ d1 d2 \u03c4 x } \u2192\n -- d2 final \u2192\n -- ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u21a6 ([ \u27e6 x , {!!} \u27e7 ] d2)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b6ddc4ccb2f694ad05da5eae2ba3c79390c94479","subject":"adding back lemma i actually use!","message":"adding back lemma i actually use!\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"preservation.agda","new_file":"preservation.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nopen import lemmas-consistency\nopen import type-assignment-unicity\nopen import binders-disjoint-checks\n\nopen import lemmas-subst-ta\n\nmodule preservation where\n -- if d and d' both result from filling the hole in \u03b5 with terms of the\n -- same type, they too have the same type.\n wt-different-fill : \u2200{ \u0394 \u0393 d \u03b5 d1 d2 d' \u03c4 \u03c41 } \u2192\n d == \u03b5 \u27e6 d1 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n d' == \u03b5 \u27e6 d2 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n wt-different-fill FHOuter D1 D2 D3 FHOuter\n with type-assignment-unicity D1 D2\n ... | refl = D3\n wt-different-fill (FHAp1 eps) (TAAp D1 D2) D3 D4 (FHAp1 D5) = TAAp (wt-different-fill eps D1 D3 D4 D5) D2\n wt-different-fill (FHAp2 eps) (TAAp D1 D2) D3 D4 (FHAp2 D5) = TAAp D1 (wt-different-fill eps D2 D3 D4 D5)\n wt-different-fill (FHNEHole eps) (TANEHole x D1 x\u2081) D2 D3 (FHNEHole D4) = TANEHole x (wt-different-fill eps D1 D2 D3 D4) x\u2081\n wt-different-fill (FHCast eps) (TACast D1 x) D2 D3 (FHCast D4) = TACast (wt-different-fill eps D1 D2 D3 D4) x\n wt-different-fill (FHFailedCast x) (TAFailedCast y x\u2081 x\u2082 x\u2083) D3 D4 (FHFailedCast eps) = TAFailedCast (wt-different-fill x y D3 D4 eps) x\u2081 x\u2082 x\u2083\n\n -- if a well typed term results from filling the hole in \u03b5, then the term\n -- that filled the hole is also well typed\n wt-filling : \u2200{ \u03b5 \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u03a3[ \u03c4' \u2208 htyp ] (\u0394 , \u0393 \u22a2 d' :: \u03c4')\n wt-filling TAConst FHOuter = _ , TAConst\n wt-filling (TAVar x\u2081) FHOuter = _ , TAVar x\u2081\n wt-filling (TALam f ta) FHOuter = _ , TALam f ta\n\n wt-filling (TAAp ta ta\u2081) FHOuter = _ , TAAp ta ta\u2081\n wt-filling (TAAp ta ta\u2081) (FHAp1 eps) = wt-filling ta eps\n wt-filling (TAAp ta ta\u2081) (FHAp2 eps) = wt-filling ta\u2081 eps\n\n wt-filling (TAEHole x x\u2081) FHOuter = _ , TAEHole x x\u2081\n wt-filling (TANEHole x ta x\u2081) FHOuter = _ , TANEHole x ta x\u2081\n wt-filling (TANEHole x ta x\u2081) (FHNEHole eps) = wt-filling ta eps\n wt-filling (TACast ta x) FHOuter = _ , TACast ta x\n wt-filling (TACast ta x) (FHCast eps) = wt-filling ta eps\n wt-filling (TAFailedCast x y z w) FHOuter = _ , TAFailedCast x y z w\n wt-filling (TAFailedCast x x\u2081 x\u2082 x\u2083) (FHFailedCast y) = wt-filling x y\n\n -- instruction transitions preserve type\n preserve-trans : \u2200{ \u0394 \u0393 d \u03c4 d' } \u2192\n binders-unique d \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u2192> d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n preserve-trans bd TAConst ()\n preserve-trans bd (TAVar x\u2081) ()\n preserve-trans bd (TALam _ ta) ()\n preserve-trans (BUAp (BULam bd x\u2081) bd\u2081 (BDLam x\u2082 x\u2083)) (TAAp (TALam apt ta) ta\u2081) ITLam = lem-subst apt x\u2082 bd\u2081 ta ta\u2081\n preserve-trans bd (TAAp (TACast ta TCRefl) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 TCRefl)) TCRefl\n preserve-trans bd (TAAp (TACast ta (TCArr x x\u2081)) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 (~sym x))) x\u2081\n preserve-trans bd (TAEHole x x\u2081) ()\n preserve-trans bd (TANEHole x ta x\u2081) ()\n preserve-trans bd (TACast ta x) (ITCastID) = ta\n preserve-trans bd (TACast (TACast ta x) x\u2081) (ITCastSucceed x\u2082) = ta\n preserve-trans bd (TACast ta x) (ITGround (MGArr x\u2081)) = TACast (TACast ta (TCArr TCHole1 TCHole1)) TCHole1\n preserve-trans bd (TACast ta TCHole2) (ITExpand (MGArr x\u2081)) = TACast (TACast ta TCHole2) (TCArr TCHole2 TCHole2)\n preserve-trans bd (TACast (TACast ta x) x\u2081) (ITCastFail w y z) = TAFailedCast ta w y z\n preserve-trans bd (TAFailedCast x y z q) ()\n\n lem-bd-\u03b51 : \u2200{ d \u03b5 d0} \u2192 d == \u03b5 \u27e6 d0 \u27e7 \u2192 binders-unique d \u2192 binders-unique d0\n lem-bd-\u03b51 FHOuter bd = bd\n lem-bd-\u03b51 (FHAp1 eps) (BUAp bd bd\u2081 x) = lem-bd-\u03b51 eps bd\n lem-bd-\u03b51 (FHAp2 eps) (BUAp bd bd\u2081 x) = lem-bd-\u03b51 eps bd\u2081\n lem-bd-\u03b51 (FHNEHole eps) (BUNEHole bd x) = lem-bd-\u03b51 eps bd\n lem-bd-\u03b51 (FHCast eps) (BUCast bd) = lem-bd-\u03b51 eps bd\n lem-bd-\u03b51 (FHFailedCast eps) (BUFailedCast bd) = lem-bd-\u03b51 eps bd\n\n -- this is the main preservation theorem, gluing together the above\n preservation : {\u0394 : hctx} {d d' : ihexp} {\u03c4 : htyp} {\u0393 : tctx} \u2192\n binders-unique d \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u21a6 d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n preservation bd D (Step x x\u2081 x\u2082)\n with wt-filling D x\n ... | (_ , wt) = wt-different-fill x D wt (preserve-trans (lem-bd-\u03b51 x bd) wt x\u2081) x\u2082\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nopen import lemmas-consistency\nopen import type-assignment-unicity\nopen import binders-disjoint-checks\n\nopen import lemmas-subst-ta\n\nmodule preservation where\n -- if d and d' both result from filling the hole in \u03b5 with terms of the\n -- same type, they too have the same type.\n wt-different-fill : \u2200{ \u0394 \u0393 d \u03b5 d1 d2 d' \u03c4 \u03c41 } \u2192\n d == \u03b5 \u27e6 d1 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n d' == \u03b5 \u27e6 d2 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n wt-different-fill FHOuter D1 D2 D3 FHOuter\n with type-assignment-unicity D1 D2\n ... | refl = D3\n wt-different-fill (FHAp1 eps) (TAAp D1 D2) D3 D4 (FHAp1 D5) = TAAp (wt-different-fill eps D1 D3 D4 D5) D2\n wt-different-fill (FHAp2 eps) (TAAp D1 D2) D3 D4 (FHAp2 D5) = TAAp D1 (wt-different-fill eps D2 D3 D4 D5)\n wt-different-fill (FHNEHole eps) (TANEHole x D1 x\u2081) D2 D3 (FHNEHole D4) = TANEHole x (wt-different-fill eps D1 D2 D3 D4) x\u2081\n wt-different-fill (FHCast eps) (TACast D1 x) D2 D3 (FHCast D4) = TACast (wt-different-fill eps D1 D2 D3 D4) x\n wt-different-fill (FHFailedCast x) (TAFailedCast y x\u2081 x\u2082 x\u2083) D3 D4 (FHFailedCast eps) = TAFailedCast (wt-different-fill x y D3 D4 eps) x\u2081 x\u2082 x\u2083\n\n -- if a well typed term results from filling the hole in \u03b5, then the term\n -- that filled the hole is also well typed\n wt-filling : \u2200{ \u03b5 \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u03a3[ \u03c4' \u2208 htyp ] (\u0394 , \u0393 \u22a2 d' :: \u03c4')\n wt-filling TAConst FHOuter = _ , TAConst\n wt-filling (TAVar x\u2081) FHOuter = _ , TAVar x\u2081\n wt-filling (TALam f ta) FHOuter = _ , TALam f ta\n\n wt-filling (TAAp ta ta\u2081) FHOuter = _ , TAAp ta ta\u2081\n wt-filling (TAAp ta ta\u2081) (FHAp1 eps) = wt-filling ta eps\n wt-filling (TAAp ta ta\u2081) (FHAp2 eps) = wt-filling ta\u2081 eps\n\n wt-filling (TAEHole x x\u2081) FHOuter = _ , TAEHole x x\u2081\n wt-filling (TANEHole x ta x\u2081) FHOuter = _ , TANEHole x ta x\u2081\n wt-filling (TANEHole x ta x\u2081) (FHNEHole eps) = wt-filling ta eps\n wt-filling (TACast ta x) FHOuter = _ , TACast ta x\n wt-filling (TACast ta x) (FHCast eps) = wt-filling ta eps\n wt-filling (TAFailedCast x y z w) FHOuter = _ , TAFailedCast x y z w\n wt-filling (TAFailedCast x x\u2081 x\u2082 x\u2083) (FHFailedCast y) = wt-filling x y\n\n -- instruction transitions preserve type\n preserve-trans : \u2200{ \u0394 \u0393 d \u03c4 d' } \u2192\n binders-unique d \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u2192> d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n preserve-trans bd TAConst ()\n preserve-trans bd (TAVar x\u2081) ()\n preserve-trans bd (TALam _ ta) ()\n preserve-trans (BUAp (BULam bd x\u2081) bd\u2081 (BDLam x\u2082 x\u2083)) (TAAp (TALam apt ta) ta\u2081) ITLam = lem-subst apt x\u2082 bd\u2081 ta ta\u2081\n preserve-trans bd (TAAp (TACast ta TCRefl) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 TCRefl)) TCRefl\n preserve-trans bd (TAAp (TACast ta (TCArr x x\u2081)) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 (~sym x))) x\u2081\n preserve-trans bd (TAEHole x x\u2081) ()\n preserve-trans bd (TANEHole x ta x\u2081) ()\n preserve-trans bd (TACast ta x) (ITCastID) = ta\n preserve-trans bd (TACast (TACast ta x) x\u2081) (ITCastSucceed x\u2082) = ta\n preserve-trans bd (TACast ta x) (ITGround (MGArr x\u2081)) = TACast (TACast ta (TCArr TCHole1 TCHole1)) TCHole1\n preserve-trans bd (TACast ta TCHole2) (ITExpand (MGArr x\u2081)) = TACast (TACast ta TCHole2) (TCArr TCHole2 TCHole2)\n preserve-trans bd (TACast (TACast ta x) x\u2081) (ITCastFail w y z) = TAFailedCast ta w y z\n preserve-trans bd (TAFailedCast x y z q) ()\n\n -- this is the main preservation theorem, gluing together the above\n preservation : {\u0394 : hctx} {d d' : ihexp} {\u03c4 : htyp} {\u0393 : tctx} \u2192\n binders-unique d \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u21a6 d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n preservation bd D (Step x x\u2081 x\u2082)\n with wt-filling D x\n ... | (_ , wt) = wt-different-fill x D wt (preserve-trans (lem-bd-\u03b51 x bd) wt x\u2081) x\u2082\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c31ff66e6a63f4fa648d6283d999831bc309a2b8","subject":"adding a comment","message":"adding a comment\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"core.agda","new_file":"core.agda","new_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n mutual\n subst : Set -- todo: no idea if this is right; mutual thing is weird\n subst = dhexp ctx\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 subst) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 subst) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n _\u27e8_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 dhexp\n _\u27e8\u2987\u2988\u21d2\u0338_\u27e9 : dhexp \u2192 htyp \u2192 dhexp\n\n -- notation for chaining together agreeable casts\n _\u27e8_\u21d2_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 htyp \u2192 dhexp\n d \u27e8 t1 \u21d2 t2 \u21d2 t3 \u27e9 = d \u27e8 t1 \u21d2 t2 \u27e9 \u27e8 t2 \u21d2 t3 \u27e9\n\n -- notation for a chained together failing cast into hole\n _\u27e8_\u21d2\u2987\u2988\u21d2\u0338_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 dhexp\n d \u27e8 t1 \u21d2\u2987\u2988\u21d2\u0338 t2 \u27e9 = d \u27e8 t1 \u21d2 \u2987\u2988 \u27e9 \u27e8\u2987\u2988\u21d2\u0338 t2 \u27e9\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = htyp ctx\n\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n -- todo: this probably belongs in contexts, but need to abstract it.\n id : tctx \u2192 subst\n id ctx x with ctx x\n id ctx x | Some \u03c4 = Some (X x)\n id ctx x | None = None\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 (tctx \u00d7 htyp))\n u ::[ \u0393 ] \u03c4 = u , (\u0393 , \u03c4)\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {n : Nat} \u2192\n (n , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of u?\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192 -- todo: uniqueness of u?\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- todo: do we care about completeness of hexp or e-umlauts? should this\n -- be judgemental rather than functional?\n\n -- those types without holes anywhere\n tcomplete : htyp \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (\u03c41 ==> \u03c42) = tcomplete \u03c41 \u00d7 tcomplete \u03c42\n\n -- those expressions without holes anywhere\n ecomplete : hexp \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: \u03c4) = ecomplete e1 \u00d7 tcomplete \u03c4\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ \u03c4 ] e) = tcomplete \u03c4 \u00d7 ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp : \u2200{\u0393 e1 \u03c4 \u03c41 \u03c41' \u03c42 \u03c42' d1 \u03941 e2 d2 \u03942 } \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: need to think about disjointness and context rep\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u03c4) ~> d1 :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> (d1 \u27e8 \u03c41' \u21d2 \u03c42 ==> \u03c4 \u27e9) \u2218 (d2 \u27e8 \u03c42' \u21d2 \u03c42 \u27e9) \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u27e8 \u03c4' \u21d2 \u03c4 \u27e9 \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c4 \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c4 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 e \u2260 \u2987\u2988[ u ]) \u2192\n ((e' : hexp) (u : Nat) \u2192 e \u2260 \u2987 e' \u2988[ u ]) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n -- ground\n data _ground : (\u03c4 : htyp) \u2192 Set where\n GBase : b ground\n GHole : \u2987\u2988 ==> \u2987\u2988 ground\n\n mutual\n -- substitition type assignment\n _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 subst \u2192 tctx \u2192 Set\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' =\n (x : Nat) (d : dhexp) (xd\u2208\u03c3 : (x , d) \u2208 \u03c3) \u2192\n \u03a3[ \u03c4 \u2208 htyp ] ((\u0393' x == Some \u03c4 \u00d7 (\u0394 , \u0393 \u22a2 d :: \u03c4)))\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 ==> \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 } \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c41 \u03c42} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u03c41 ~ \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 :: \u03c42\n TAFailedCast : \u2200{\u0394 \u0393 d \u03c41 \u03c42} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 :: \u03c42\n\n -- substitution;; todo: maybe get a premise that it's final; analagous to \"value substitution\"\n [_\/_]_ : dhexp \u2192 Nat \u2192 dhexp \u2192 dhexp\n [ d \/ y ] c = c\n [ d \/ y ] X x\n with natEQ x y\n [ d \/ y ] X .y | Inl refl = d\n [ d \/ y ] X x | Inr neq = X y\n [ d \/ y ] (\u00b7\u03bb x [ x\u2081 ] d') = \u00b7\u03bb x [ x\u2081 ] ( [ d \/ y ] d') -- TODO: i *think* barendrecht's saves us here, or at least i want it to. may need to reformulat this as a relation --> set\n [ d \/ y ] \u2987\u2988\u27e8 u , \u03c3 \u27e9 = \u2987\u2988\u27e8 u , \u03c3 \u27e9\n [ d \/ y ] \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9 = \u2987 [ d \/ y ] d' \u2988\u27e8 u , \u03c3 \u27e9\n [ d \/ y ] (d1 \u2218 d2) = ([ d \/ y ] d1) \u2218 ([ d \/ y ] d2)\n [ d \/ y ] (d' \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 ) = ([ d \/ y ] d') \u27e8 \u03c41 \u21d2 \u03c42 \u27e9\n [ d \/ y ] (d' \u27e8\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 ) = ([ d \/ y ] d') \u27e8\u2987\u2988\u21d2\u0338 \u03c42 \u27e9\n\n -- value\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n data _boxedval : (d : dhexp) \u2192 Set where\n BVVal : \u2200{d} \u2192 d val \u2192 d boxedval\n BVArrCast : \u2200{ d \u03c41 \u03c42 \u03c43 \u03c44 } \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d boxedval \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 boxedval\n BVHoleCast : \u2200{ \u03c4 d } \u2192 \u03c4 ground \u2192 d boxedval \u2192 d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 boxedval\n\n mutual\n -- indeterminate\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192\n d1 \u2260 (d1' \u27e8(\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44)\u27e9)) \u2192\n d1 indet \u2192\n d2 final \u2192\n (d1 \u2218 d2) indet -- todo: should there be two ap rules?\n ICastArr : \u2200{d \u03c41 \u03c42 \u03c43 \u03c44} \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d indet \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 indet\n ICastGroundHole : \u2200{ \u03c4 d } \u2192\n \u03c4 ground \u2192\n d indet \u2192\n d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 indet\n ICastHoleGround : \u2200 { d \u03c4 } \u2192\n ((d' : dhexp) (\u03c4' : htyp) \u2192 d \u2260 (d' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9)) \u2192\n d indet \u2192\n \u03c4 ground \u2192\n d \u27e8 \u2987\u2988 \u21d2 \u03c4 \u27e9 indet\n IFailedCast : \u2200{ d \u03c41 \u03c42 } \u2192\n d final \u2192\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 indet\n\n -- final\n data _final : (d : dhexp) \u2192 Set where\n FBoxed : \u2200{d} \u2192 d boxedval \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n\n -- -- contextual dynamics\n\n -- evaluation contexts\n data ectx : Set where\n \u2299 : ectx\n _\u2218\u2081_ : ectx \u2192 dhexp \u2192 ectx\n _\u2218\u2082_ : dhexp \u2192 ectx \u2192 ectx\n \u2987_\u2988\u27e8_\u27e9 : ectx \u2192 (Nat \u00d7 subst ) \u2192 ectx\n _\u27e8_\u21d2_\u27e9 : ectx \u2192 htyp \u2192 htyp \u2192 ectx\n _\u27e8\u2987\u2988\u21d2\u0338_\u27e9 : ectx \u2192 htyp \u2192 ectx\n\n -- todo\/ note: this judgement is redundant now; in the absence of the\n -- premises in the red brackets in the notes PDF, all syntactically well\n -- formed ectxs are valid; with finality premises, that's not true. so it\n -- might make sense to remove this judgement entirely, but need to make\n -- sure to describe why we don't have the redbox things and how we'd patch\n -- it up if we wanted to force a particular evaluation order in some\n -- document somewhere (probably a README for this repo, or a sentence in\n -- the paper text or both)\n\n --\u03b5 is an evaluation context\n data _evalctx : (\u03b5 : ectx) \u2192 Set where\n ECDot : \u2299 evalctx\n ECAp1 : \u2200{d \u03b5} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u2218\u2081 d) evalctx\n ECAp2 : \u2200{d \u03b5} \u2192\n -- d final \u2192 -- red box\n \u03b5 evalctx \u2192\n (d \u2218\u2082 \u03b5) evalctx\n ECNEHole : \u2200{\u03b5 u \u03c3} \u2192\n \u03b5 evalctx \u2192\n \u2987 \u03b5 \u2988\u27e8 u , \u03c3 \u27e9 evalctx\n ECCast : \u2200{ \u03b5 \u03c41 \u03c42} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) evalctx\n ECFailedCast : \u2200{ \u03b5 \u03c4 } \u2192\n \u03b5 evalctx \u2192\n \u03b5 \u27e8\u2987\u2988\u21d2\u0338 \u03c4 \u27e9 evalctx\n\n -- d is the result of filling the hole in \u03b5 with d'\n data _==_\u27e6_\u27e7 : (d : dhexp) (\u03b5 : ectx) (d' : dhexp) \u2192 Set where\n FHOuter : \u2200{d} \u2192 d == \u2299 \u27e6 d \u27e7 -- this used to a have a premise of being final for some reason\n FHAp1 : \u2200{d1 d1' d2 \u03b5} \u2192\n d1 == \u03b5 \u27e6 d1' \u27e7 \u2192\n (d1 \u2218 d2) == (\u03b5 \u2218\u2081 d2) \u27e6 d1' \u27e7\n FHAp2 : \u2200{d1 d2 d2' \u03b5} \u2192\n -- d1 final \u2192 -- red box\n d2 == \u03b5 \u27e6 d2' \u27e7 \u2192\n (d1 \u2218 d2) == (d1 \u2218\u2082 \u03b5) \u27e6 d2' \u27e7\n FHNEHole : \u2200{ d d' \u03b5 u \u03c3} \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u2987 d \u2988\u27e8 (u , \u03c3 ) \u27e9 == \u2987 \u03b5 \u2988\u27e8 (u , \u03c3 ) \u27e9 \u27e6 d' \u27e7\n FHCast : \u2200{ d d' \u03b5 \u03c41 \u03c42 } \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 == \u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 \u27e6 d' \u27e7\n FHFailedCast : --\u2200{ d d' \u03b5 \u03c4} \u2192\n {d d' : dhexp } {\u03c4 : htyp } { \u03b5 : ectx } \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n (d \u27e8\u2987\u2988\u21d2\u0338 \u03c4 \u27e9) == (\u03b5 \u27e8\u2987\u2988\u21d2\u0338 \u03c4 \u27e9) \u27e6 d' \u27e7\n\n data _\u25b8gnd_ : htyp \u2192 htyp \u2192 Set where\n MGArr : \u2200{\u03c41 \u03c42} \u2192\n (\u03c41 ==> \u03c42) \u2260 (\u2987\u2988 ==> \u2987\u2988) \u2192\n (\u03c41 ==> \u03c42) \u25b8gnd (\u2987\u2988 ==> \u2987\u2988)\n\n -- instruction transition judgement\n data _\u2192>_ : (d d' : dhexp) \u2192 Set where\n ITLam : \u2200{ x \u03c4 d1 d2 } \u2192\n -- d2 final \u2192 -- red box\n ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u2192> ([ d2 \/ x ] d1) -- todo: this is very unlikely to work long term\n ITCastID : \u2200{d \u03c4 } \u2192\n -- d final \u2192 -- red box\n (d \u27e8 \u03c4 \u21d2 \u03c4 \u27e9) \u2192> d\n ITCastSucceed : \u2200{d \u03c4 } \u2192\n -- d final \u2192 -- red box\n \u03c4 ground \u2192\n (d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u21d2 \u03c4 \u27e9) \u2192> d\n ITCastFail : \u2200{ d \u03c41 \u03c42} \u2192\n -- d final \u2192 -- red box\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n (d \u27e8 \u03c41 \u21d2 \u2987\u2988 \u21d2 \u03c42 \u27e9) \u2192> (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n ITApCast : \u2200{d1 d2 \u03c41 \u03c42 \u03c41' \u03c42' } \u2192\n -- d1 final \u2192 -- red box\n -- d2 final \u2192 -- red box\n ((d1 \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c41' ==> \u03c42')\u27e9) \u2218 d2) \u2192> ((d1 \u2218 (d2 \u27e8 \u03c41' \u21d2 \u03c41 \u27e9)) \u27e8 \u03c42 \u21d2 \u03c42' \u27e9)\n ITGround : \u2200{ d \u03c4 \u03c4'} \u2192\n -- d final \u2192 -- red box\n \u03c4 \u25b8gnd \u03c4' \u2192\n (d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9) \u2192> (d \u27e8 \u03c4 \u21d2 \u03c4' \u21d2 \u2987\u2988 \u27e9)\n ITExpand : \u2200{d \u03c4 \u03c4' } \u2192\n -- d final \u2192 -- red box\n \u03c4 \u25b8gnd \u03c4' \u2192\n (d \u27e8 \u2987\u2988 \u21d2 \u03c4 \u27e9) \u2192> (d \u27e8 \u2987\u2988 \u21d2 \u03c4' \u21d2 \u03c4 \u27e9)\n\n data _\u21a6_ : (d d' : dhexp) \u2192 Set where\n Step : \u2200{ d d0 d' d0' \u03b5} \u2192\n d == \u03b5 \u27e6 d0 \u27e7 \u2192\n d0 \u2192> d0' \u2192\n d' == \u03b5 \u27e6 d0' \u27e7 \u2192\n d \u21a6 d'\n\n data _\u21a6*_ : (d d' : dhexp) \u2192 Set where\n MSRefl : \u2200{d} \u2192 d \u21a6* d\n MSStep : \u2200{d d' d''} \u2192\n d \u21a6 d' \u2192\n d' \u21a6* d'' \u2192\n d \u21a6* d''\n","old_contents":"open import Nat\nopen import Prelude\nopen import contexts\n\nmodule core where\n -- types\n data htyp : Set where\n b : htyp\n \u2987\u2988 : htyp\n _==>_ : htyp \u2192 htyp \u2192 htyp\n\n -- arrow type constructors bind very tightly\n infixr 25 _==>_\n\n -- expressions\n data hexp : Set where\n c : hexp\n _\u00b7:_ : hexp \u2192 htyp \u2192 hexp\n X : Nat \u2192 hexp\n \u00b7\u03bb : Nat \u2192 hexp \u2192 hexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 hexp \u2192 hexp\n \u2987\u2988[_] : Nat \u2192 hexp\n \u2987_\u2988[_] : hexp \u2192 Nat \u2192 hexp\n _\u2218_ : hexp \u2192 hexp \u2192 hexp\n\n mutual\n subst : Set -- todo: no idea if this is right; mutual thing is weird\n subst = dhexp ctx\n\n -- expressions without ascriptions but with casts\n data dhexp : Set where\n c : dhexp\n X : Nat \u2192 dhexp\n \u00b7\u03bb_[_]_ : Nat \u2192 htyp \u2192 dhexp \u2192 dhexp\n \u2987\u2988\u27e8_\u27e9 : (Nat \u00d7 subst) \u2192 dhexp\n \u2987_\u2988\u27e8_\u27e9 : dhexp \u2192 (Nat \u00d7 subst) \u2192 dhexp\n _\u2218_ : dhexp \u2192 dhexp \u2192 dhexp\n _\u27e8_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 dhexp\n _\u27e8\u2987\u2988\u21d2\u0338_\u27e9 : dhexp \u2192 htyp \u2192 dhexp\n\n -- notation for chaining together agreeable casts\n _\u27e8_\u21d2_\u21d2_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 htyp \u2192 dhexp\n d \u27e8 t1 \u21d2 t2 \u21d2 t3 \u27e9 = d \u27e8 t1 \u21d2 t2 \u27e9 \u27e8 t2 \u21d2 t3 \u27e9\n\n -- notation for a chained together failing cast into hole\n _\u27e8_\u21d2\u2987\u2988\u21d2\u0338_\u27e9 : dhexp \u2192 htyp \u2192 htyp \u2192 dhexp\n d \u27e8 t1 \u21d2\u2987\u2988\u21d2\u0338 t2 \u27e9 = d \u27e8 t1 \u21d2 \u2987\u2988 \u27e9 \u27e8\u2987\u2988\u21d2\u0338 t2 \u27e9\n\n -- type consistency\n data _~_ : (t1 t2 : htyp) \u2192 Set where\n TCRefl : {\u03c4 : htyp} \u2192 \u03c4 ~ \u03c4\n TCHole1 : {\u03c4 : htyp} \u2192 \u03c4 ~ \u2987\u2988\n TCHole2 : {\u03c4 : htyp} \u2192 \u2987\u2988 ~ \u03c4\n TCArr : {\u03c41 \u03c42 \u03c41' \u03c42' : htyp} \u2192\n \u03c41 ~ \u03c41' \u2192\n \u03c42 ~ \u03c42' \u2192\n \u03c41 ==> \u03c42 ~ \u03c41' ==> \u03c42'\n\n -- type inconsistency\n data _~\u0338_ : (\u03c41 \u03c42 : htyp) \u2192 Set where\n ICBaseArr1 : {\u03c41 \u03c42 : htyp} \u2192 b ~\u0338 \u03c41 ==> \u03c42\n ICBaseArr2 : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 ~\u0338 b\n ICArr1 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c41 ~\u0338 \u03c43 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n ICArr2 : {\u03c41 \u03c42 \u03c43 \u03c44 : htyp} \u2192\n \u03c42 ~\u0338 \u03c44 \u2192\n \u03c41 ==> \u03c42 ~\u0338 \u03c43 ==> \u03c44\n\n --- matching for arrows\n data _\u25b8arr_ : htyp \u2192 htyp \u2192 Set where\n MAHole : \u2987\u2988 \u25b8arr \u2987\u2988 ==> \u2987\u2988\n MAArr : {\u03c41 \u03c42 : htyp} \u2192 \u03c41 ==> \u03c42 \u25b8arr \u03c41 ==> \u03c42\n\n -- aliases for type and hole contexts\n tctx : Set\n tctx = htyp ctx\n\n hctx : Set\n hctx = (htyp ctx \u00d7 htyp) ctx\n\n -- todo: this probably belongs in contexts, but need to abstract it.\n id : tctx \u2192 subst\n id ctx x with ctx x\n id ctx x | Some \u03c4 = Some (X x)\n id ctx x | None = None\n\n -- this is just fancy notation to match the paper\n _::[_]_ : Nat \u2192 tctx \u2192 htyp \u2192 (Nat \u00d7 (tctx \u00d7 htyp))\n u ::[ \u0393 ] \u03c4 = u , (\u0393 , \u03c4)\n\n -- bidirectional type checking judgements for hexp\n mutual\n -- synthesis\n data _\u22a2_=>_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n SConst : {\u0393 : tctx} \u2192 \u0393 \u22a2 c => b\n SAsc : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) => \u03c4\n SVar : {\u0393 : tctx} {\u03c4 : htyp} {n : Nat} \u2192\n (n , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X n => \u03c4\n SAp : {\u0393 : tctx} {e1 e2 : hexp} {\u03c4 \u03c41 \u03c42 : htyp} \u2192\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e2 <= \u03c42 \u2192\n \u0393 \u22a2 (e1 \u2218 e2) => \u03c4\n SEHole : {\u0393 : tctx} {u : Nat} \u2192 \u0393 \u22a2 \u2987\u2988[ u ] => \u2987\u2988 -- todo: uniqueness of u?\n SNEHole : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {u : Nat} \u2192 -- todo: uniqueness of u?\n \u0393 \u22a2 e => \u03c4 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] => \u2987\u2988\n SLam : {\u0393 : tctx} {e : hexp} {\u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e => \u03c42 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e => \u03c41 ==> \u03c42\n\n -- analysis\n data _\u22a2_<=_ : (\u0393 : htyp ctx) (e : hexp) (\u03c4 : htyp) \u2192 Set where\n ASubsume : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} \u2192\n \u0393 \u22a2 e => \u03c4' \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e <= \u03c4\n ALam : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c41 \u03c42 : htyp} {x : Nat} \u2192\n x # \u0393 \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e <= \u03c42 \u2192\n \u0393 \u22a2 (\u00b7\u03bb x e) <= \u03c4\n\n -- todo: do we care about completeness of hexp or e-umlauts? should this\n -- be judgemental rather than functional?\n\n -- those types without holes anywhere\n tcomplete : htyp \u2192 Set\n tcomplete b = \u22a4\n tcomplete \u2987\u2988 = \u22a5\n tcomplete (\u03c41 ==> \u03c42) = tcomplete \u03c41 \u00d7 tcomplete \u03c42\n\n -- those expressions without holes anywhere\n ecomplete : hexp \u2192 Set\n ecomplete c = \u22a4\n ecomplete (e1 \u00b7: \u03c4) = ecomplete e1 \u00d7 tcomplete \u03c4\n ecomplete (X _) = \u22a4\n ecomplete (\u00b7\u03bb _ e1) = ecomplete e1\n ecomplete \u2987\u2988[ u ] = \u22a5\n ecomplete \u2987 e1 \u2988[ u ] = \u22a5\n ecomplete (e1 \u2218 e2) = ecomplete e1 \u00d7 ecomplete e2\n ecomplete (\u00b7\u03bb x [ \u03c4 ] e) = tcomplete \u03c4 \u00d7 ecomplete e\n\n -- expansion\n mutual\n data _\u22a2_\u21d2_~>_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u0394 : hctx) \u2192 Set where\n ESConst : \u2200{\u0393} \u2192 \u0393 \u22a2 c \u21d2 b ~> c \u22a3 \u2205\n ESVar : \u2200{\u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192\n \u0393 \u22a2 X x \u21d2 \u03c4 ~> X x \u22a3 \u2205\n ESLam : \u2200{\u0393 x \u03c41 \u03c42 e d \u0394 } \u2192\n (x # \u0393) \u2192 -- todo: i added this\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d2 \u03c42 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] e \u21d2 (\u03c41 ==> \u03c42) ~> \u00b7\u03bb x [ \u03c41 ] d \u22a3 \u0394\n ESAp : \u2200{\u0393 e1 \u03c4 \u03c41 \u03c41' \u03c42 \u03c42' d1 \u03941 e2 d2 \u03942 } \u2192\n -- \u03941 ## \u03942 \u2192 -- todo: need to think about disjointness and context rep\n \u0393 \u22a2 e1 => \u03c41 \u2192\n \u03c41 \u25b8arr \u03c42 ==> \u03c4 \u2192\n \u0393 \u22a2 e1 \u21d0 (\u03c42 ==> \u03c4) ~> d1 :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> d2 :: \u03c42' \u22a3 \u03942 \u2192\n \u0393 \u22a2 e1 \u2218 e2 \u21d2 \u03c4 ~> (d1 \u27e8 \u03c41' \u21d2 \u03c42 ==> \u03c4 \u27e9) \u2218 (d2 \u27e8 \u03c42' \u21d2 \u03c42 \u27e9) \u22a3 (\u03941 \u222a \u03942)\n ESEHole : \u2200{ \u0393 u } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 \u22a3 \u25a0 (u ::[ \u0393 ] \u2987\u2988)\n ESNEHole : \u2200{ \u0393 e \u03c4 d u \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d2 \u2987\u2988 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u2987\u2988)\n ESAsc : \u2200 {\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 (e \u00b7: \u03c4) \u21d2 \u03c4 ~> d \u27e8 \u03c4' \u21d2 \u03c4 \u27e9 \u22a3 \u0394\n\n data _\u22a2_\u21d0_~>_::_\u22a3_ : (\u0393 : tctx) (e : hexp) (\u03c4 : htyp) (d : dhexp) (\u03c4' : htyp) (\u0394 : hctx) \u2192 Set where\n EALam : \u2200{\u0393 x \u03c4 \u03c41 \u03c42 e d \u03c42' \u0394 } \u2192\n (x # \u0393) \u2192\n \u03c4 \u25b8arr \u03c41 ==> \u03c42 \u2192\n (\u0393 ,, (x , \u03c41)) \u22a2 e \u21d0 \u03c42 ~> d :: \u03c42' \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u00b7\u03bb x e \u21d0 \u03c4 ~> \u00b7\u03bb x [ \u03c41 ] d :: \u03c41 ==> \u03c42' \u22a3 \u0394\n EASubsume : \u2200{e \u0393 \u03c4' d \u0394 \u03c4} \u2192\n ((u : Nat) \u2192 e \u2260 \u2987\u2988[ u ]) \u2192\n ((e' : hexp) (u : Nat) \u2192 e \u2260 \u2987 e' \u2988[ u ]) \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u03c4 ~ \u03c4' \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394\n EAEHole : \u2200{ \u0393 u \u03c4 } \u2192\n \u0393 \u22a2 \u2987\u2988[ u ] \u21d0 \u03c4 ~> \u2987\u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 \u25a0 (u ::[ \u0393 ] \u03c4)\n EANEHole : \u2200{ \u0393 e u \u03c4 d \u03c4' \u0394 } \u2192\n \u0393 \u22a2 e \u21d2 \u03c4' ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 \u2987 e \u2988[ u ] \u21d0 \u03c4 ~> \u2987 d \u2988\u27e8 u , id \u0393 \u27e9 :: \u03c4 \u22a3 (\u0394 ,, u ::[ \u0393 ] \u03c4)\n\n -- ground\n data _ground : (\u03c4 : htyp) \u2192 Set where\n GBase : b ground\n GHole : \u2987\u2988 ==> \u2987\u2988 ground\n\n mutual\n -- substitition type assignment\n _,_\u22a2_:s:_ : hctx \u2192 tctx \u2192 subst \u2192 tctx \u2192 Set\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' =\n (x : Nat) (d : dhexp) (xd\u2208\u03c3 : (x , d) \u2208 \u03c3) \u2192\n \u03a3[ \u03c4 \u2208 htyp ] ((\u0393' x == Some \u03c4 \u00d7 (\u0394 , \u0393 \u22a2 d :: \u03c4)))\n\n -- type assignment\n data _,_\u22a2_::_ : (\u0394 : hctx) (\u0393 : tctx) (d : dhexp) (\u03c4 : htyp) \u2192 Set where\n TAConst : \u2200{\u0394 \u0393} \u2192 \u0394 , \u0393 \u22a2 c :: b\n TAVar : \u2200{\u0394 \u0393 x \u03c4} \u2192 (x , \u03c4) \u2208 \u0393 \u2192 \u0394 , \u0393 \u22a2 X x :: \u03c4\n TALam : \u2200{ \u0394 \u0393 x \u03c41 d \u03c42} \u2192\n \u0394 , (\u0393 ,, (x , \u03c41)) \u22a2 d :: \u03c42 \u2192\n \u0394 , \u0393 \u22a2 \u00b7\u03bb x [ \u03c41 ] d :: (\u03c41 ==> \u03c42)\n TAAp : \u2200{ \u0394 \u0393 d1 d2 \u03c41 \u03c4} \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 ==> \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d1 \u2218 d2 :: \u03c4\n TAEHole : \u2200{ \u0394 \u0393 \u03c3 u \u0393' \u03c4} \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987\u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TANEHole : \u2200 { \u0394 \u0393 d \u03c4' \u0393' u \u03c3 \u03c4 } \u2192\n (u , (\u0393' , \u03c4)) \u2208 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4' \u2192\n \u0394 , \u0393 \u22a2 \u03c3 :s: \u0393' \u2192\n \u0394 , \u0393 \u22a2 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 :: \u03c4\n TACast : \u2200{ \u0394 \u0393 d \u03c41 \u03c42} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u03c41 ~ \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 :: \u03c42\n TAFailedCast : \u2200{\u0394 \u0393 d \u03c41 \u03c42} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c41 \u2192\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n \u0394 , \u0393 \u22a2 d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 :: \u03c42\n\n -- substitution;; todo: maybe get a premise that it's final; analagous to \"value substitution\"\n [_\/_]_ : dhexp \u2192 Nat \u2192 dhexp \u2192 dhexp\n [ d \/ y ] c = c\n [ d \/ y ] X x\n with natEQ x y\n [ d \/ y ] X .y | Inl refl = d\n [ d \/ y ] X x | Inr neq = X y\n [ d \/ y ] (\u00b7\u03bb x [ x\u2081 ] d') = \u00b7\u03bb x [ x\u2081 ] ( [ d \/ y ] d') -- TODO: i *think* barendrecht's saves us here, or at least i want it to. may need to reformulat this as a relation --> set\n [ d \/ y ] \u2987\u2988\u27e8 u , \u03c3 \u27e9 = \u2987\u2988\u27e8 u , \u03c3 \u27e9\n [ d \/ y ] \u2987 d' \u2988\u27e8 u , \u03c3 \u27e9 = \u2987 [ d \/ y ] d' \u2988\u27e8 u , \u03c3 \u27e9\n [ d \/ y ] (d1 \u2218 d2) = ([ d \/ y ] d1) \u2218 ([ d \/ y ] d2)\n [ d \/ y ] (d' \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 ) = ([ d \/ y ] d') \u27e8 \u03c41 \u21d2 \u03c42 \u27e9\n [ d \/ y ] (d' \u27e8\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 ) = ([ d \/ y ] d') \u27e8\u2987\u2988\u21d2\u0338 \u03c42 \u27e9\n\n -- value\n data _val : (d : dhexp) \u2192 Set where\n VConst : c val\n VLam : \u2200{x \u03c4 d} \u2192 (\u00b7\u03bb x [ \u03c4 ] d) val\n\n data _boxedval : (d : dhexp) \u2192 Set where\n BVVal : \u2200{d} \u2192 d val \u2192 d boxedval\n BVArrCast : \u2200{ d \u03c41 \u03c42 \u03c43 \u03c44 } \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d boxedval \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 boxedval\n BVHoleCast : \u2200{ \u03c4 d } \u2192 \u03c4 ground \u2192 d boxedval \u2192 d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 boxedval\n\n mutual\n -- indeterminate\n data _indet : (d : dhexp) \u2192 Set where\n IEHole : \u2200{u \u03c3} \u2192 \u2987\u2988\u27e8 u , \u03c3 \u27e9 indet\n INEHole : \u2200{d u \u03c3} \u2192 d final \u2192 \u2987 d \u2988\u27e8 u , \u03c3 \u27e9 indet\n IAp : \u2200{d1 d2} \u2192 ((\u03c41 \u03c42 \u03c43 \u03c44 : htyp) (d1' : dhexp) \u2192\n d1 \u2260 (d1' \u27e8(\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44)\u27e9)) \u2192\n d1 indet \u2192\n d2 final \u2192\n (d1 \u2218 d2) indet -- todo: should there be two ap rules?\n ICastArr : \u2200{d \u03c41 \u03c42 \u03c43 \u03c44} \u2192\n \u03c41 ==> \u03c42 \u2260 \u03c43 ==> \u03c44 \u2192\n d indet \u2192\n d \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c43 ==> \u03c44) \u27e9 indet\n ICastGroundHole : \u2200{ \u03c4 d } \u2192\n \u03c4 ground \u2192\n d indet \u2192\n d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9 indet\n ICastHoleGround : \u2200 { d \u03c4 } \u2192\n ((d' : dhexp) (\u03c4' : htyp) \u2192 d \u2260 (d' \u27e8 \u03c4' \u21d2 \u2987\u2988 \u27e9)) \u2192\n d indet \u2192\n \u03c4 ground \u2192\n d \u27e8 \u2987\u2988 \u21d2 \u03c4 \u27e9 indet\n IFailedCast : \u2200{ d \u03c41 \u03c42 } \u2192\n d final \u2192\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9 indet\n\n -- final\n data _final : (d : dhexp) \u2192 Set where\n FBoxed : \u2200{d} \u2192 d boxedval \u2192 d final\n FIndet : \u2200{d} \u2192 d indet \u2192 d final\n\n\n -- -- contextual dynamics\n\n -- evaluation contexts\n data ectx : Set where\n \u2299 : ectx\n _\u2218\u2081_ : ectx \u2192 dhexp \u2192 ectx\n _\u2218\u2082_ : dhexp \u2192 ectx \u2192 ectx\n \u2987_\u2988\u27e8_\u27e9 : ectx \u2192 (Nat \u00d7 subst ) \u2192 ectx\n _\u27e8_\u21d2_\u27e9 : ectx \u2192 htyp \u2192 htyp \u2192 ectx\n _\u27e8\u2987\u2988\u21d2\u0338_\u27e9 : ectx \u2192 htyp \u2192 ectx\n\n --\u03b5 is an evaluation context\n data _evalctx : (\u03b5 : ectx) \u2192 Set where\n ECDot : \u2299 evalctx\n ECAp1 : \u2200{d \u03b5} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u2218\u2081 d) evalctx\n ECAp2 : \u2200{d \u03b5} \u2192\n -- d final \u2192 -- red box\n \u03b5 evalctx \u2192\n (d \u2218\u2082 \u03b5) evalctx\n ECNEHole : \u2200{\u03b5 u \u03c3} \u2192\n \u03b5 evalctx \u2192\n \u2987 \u03b5 \u2988\u27e8 u , \u03c3 \u27e9 evalctx\n ECCast : \u2200{ \u03b5 \u03c41 \u03c42} \u2192\n \u03b5 evalctx \u2192\n (\u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9) evalctx\n ECFailedCast : \u2200{ \u03b5 \u03c4 } \u2192\n \u03b5 evalctx \u2192\n \u03b5 \u27e8\u2987\u2988\u21d2\u0338 \u03c4 \u27e9 evalctx\n\n -- d is the result of filling the hole in \u03b5 with d'\n data _==_\u27e6_\u27e7 : (d : dhexp) (\u03b5 : ectx) (d' : dhexp) \u2192 Set where\n FHOuter : \u2200{d} \u2192 d == \u2299 \u27e6 d \u27e7 -- this used to a have a premise of being final for some reason\n FHAp1 : \u2200{d1 d1' d2 \u03b5} \u2192\n d1 == \u03b5 \u27e6 d1' \u27e7 \u2192\n (d1 \u2218 d2) == (\u03b5 \u2218\u2081 d2) \u27e6 d1' \u27e7\n FHAp2 : \u2200{d1 d2 d2' \u03b5} \u2192\n -- d1 final \u2192 -- red box\n d2 == \u03b5 \u27e6 d2' \u27e7 \u2192\n (d1 \u2218 d2) == (d1 \u2218\u2082 \u03b5) \u27e6 d2' \u27e7\n FHNEHole : \u2200{ d d' \u03b5 u \u03c3} \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u2987 d \u2988\u27e8 (u , \u03c3 ) \u27e9 == \u2987 \u03b5 \u2988\u27e8 (u , \u03c3 ) \u27e9 \u27e6 d' \u27e7\n FHCast : \u2200{ d d' \u03b5 \u03c41 \u03c42 } \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n d \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 == \u03b5 \u27e8 \u03c41 \u21d2 \u03c42 \u27e9 \u27e6 d' \u27e7\n FHFailedCast : --\u2200{ d d' \u03b5 \u03c4} \u2192\n {d d' : dhexp } {\u03c4 : htyp } { \u03b5 : ectx } \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n (d \u27e8\u2987\u2988\u21d2\u0338 \u03c4 \u27e9) == (\u03b5 \u27e8\u2987\u2988\u21d2\u0338 \u03c4 \u27e9) \u27e6 d' \u27e7\n\n data _\u25b8gnd_ : htyp \u2192 htyp \u2192 Set where\n MGArr : \u2200{\u03c41 \u03c42} \u2192\n (\u03c41 ==> \u03c42) \u2260 (\u2987\u2988 ==> \u2987\u2988) \u2192\n (\u03c41 ==> \u03c42) \u25b8gnd (\u2987\u2988 ==> \u2987\u2988)\n\n -- instruction transition judgement\n data _\u2192>_ : (d d' : dhexp) \u2192 Set where\n ITLam : \u2200{ x \u03c4 d1 d2 } \u2192\n -- d2 final \u2192 -- red box\n ((\u00b7\u03bb x [ \u03c4 ] d1) \u2218 d2) \u2192> ([ d2 \/ x ] d1) -- todo: this is very unlikely to work long term\n ITCastID : \u2200{d \u03c4 } \u2192\n -- d final \u2192 -- red box\n (d \u27e8 \u03c4 \u21d2 \u03c4 \u27e9) \u2192> d\n ITCastSucceed : \u2200{d \u03c4 } \u2192\n -- d final \u2192 -- red box\n \u03c4 ground \u2192\n (d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u21d2 \u03c4 \u27e9) \u2192> d\n ITCastFail : \u2200{ d \u03c41 \u03c42} \u2192\n -- d final \u2192 -- red box\n \u03c41 ground \u2192\n \u03c42 ground \u2192\n \u03c41 \u2260 \u03c42 \u2192\n (d \u27e8 \u03c41 \u21d2 \u2987\u2988 \u21d2 \u03c42 \u27e9) \u2192> (d \u27e8 \u03c41 \u21d2\u2987\u2988\u21d2\u0338 \u03c42 \u27e9)\n ITApCast : \u2200{d1 d2 \u03c41 \u03c42 \u03c41' \u03c42' } \u2192\n -- d1 final \u2192 -- red box\n -- d2 final \u2192 -- red box\n ((d1 \u27e8 (\u03c41 ==> \u03c42) \u21d2 (\u03c41' ==> \u03c42')\u27e9) \u2218 d2) \u2192> ((d1 \u2218 (d2 \u27e8 \u03c41' \u21d2 \u03c41 \u27e9)) \u27e8 \u03c42 \u21d2 \u03c42' \u27e9)\n ITGround : \u2200{ d \u03c4 \u03c4'} \u2192\n -- d final \u2192 -- red box\n \u03c4 \u25b8gnd \u03c4' \u2192\n (d \u27e8 \u03c4 \u21d2 \u2987\u2988 \u27e9) \u2192> (d \u27e8 \u03c4 \u21d2 \u03c4' \u21d2 \u2987\u2988 \u27e9)\n ITExpand : \u2200{d \u03c4 \u03c4' } \u2192\n -- d final \u2192 -- red box\n \u03c4 \u25b8gnd \u03c4' \u2192\n (d \u27e8 \u2987\u2988 \u21d2 \u03c4 \u27e9) \u2192> (d \u27e8 \u2987\u2988 \u21d2 \u03c4' \u21d2 \u03c4 \u27e9)\n\n data _\u21a6_ : (d d' : dhexp) \u2192 Set where\n Step : \u2200{ d d0 d' d0' \u03b5} \u2192\n d == \u03b5 \u27e6 d0 \u27e7 \u2192\n d0 \u2192> d0' \u2192\n d' == \u03b5 \u27e6 d0' \u27e7 \u2192\n d \u21a6 d'\n\n data _\u21a6*_ : (d d' : dhexp) \u2192 Set where\n MSRefl : \u2200{d} \u2192 d \u21a6* d\n MSStep : \u2200{d d' d''} \u2192\n d \u21a6 d' \u2192\n d' \u21a6* d'' \u2192\n d \u21a6* d''\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"29a2acc368333b64034b4cab4b405a5db447d918","subject":"IDescTT model: implement the Vec and Fin examples","message":"IDescTT model: implement the Vec and Fin examples\n","repos":"mietek\/epigram2,larrytheliquid\/pigit,mietek\/epigram2","old_file":"models\/IDescTT.agda","new_file":"models\/IDescTT.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--****************\n-- Meta-language\n--****************\n\n-- Note that we could define Nat, Bool, and the related operations in\n-- IDesc. But it is awful to code with them, in Agda.\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n--****************\n-- Vec (constraints)\n--****************\n\ndata VecConst : Set where\n Vnil : VecConst\n Vcons : VecConst\n\nvecDChoice : Set -> Nat -> VecConst -> IDesc Nat\nvecDChoice X n Vnil = const (n == ze)\nvecDChoice X n Vcons = sigma Nat (\\m -> prod (var m) (const (n == su m)))\n\nvecD : Set -> Nat -> IDesc Nat\nvecD X n = sigma VecConst (vecDChoice X n)\n\nvec : Set -> Nat -> Set\nvec X n = IMu (vecD X) n\n\n--****************\n-- Vec (de-tagged, forced)\n--****************\n\ndata VecConst2 : Nat -> Set where\n Vnil : VecConst2 ze\n Vcons : {n : Nat} -> VecConst2 (su n)\n\nvecDChoice2 : Set -> (n : Nat) -> VecConst2 n -> IDesc Nat\nvecDChoice2 X ze Vnil = const Unit\nvecDChoice2 X (su n) Vcons = prod (const X) (var n)\n\nvecD2 : Set -> Nat -> IDesc Nat\nvecD2 X n = sigma (VecConst2 n) (vecDChoice2 X n)\n\nvec2 : Set -> Nat -> Set\nvec2 X n = IMu (vecD2 X) n\n\n--****************\n-- Fin (de-tagged)\n--****************\n\ndata FinConst : Nat -> Set where\n Fz : {n : Nat} -> FinConst (su n)\n Fs : {n : Nat} -> FinConst (su n)\n\nfinDChoice : (n : Nat) -> FinConst n -> IDesc Nat\nfinDChoice ze ()\nfinDChoice (su n) Fz = const Unit\nfinDChoice (su n) Fs = var n\n\nfinD : Nat -> IDesc Nat\nfinD n = sigma (FinConst n) (finDChoice n) \n\nfin : Nat -> Set\nfin n = IMu finD n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n\n\n--********************************\n-- Typed expressions\n--********************************\n\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty),\n (prod (var bool) (prod (var ty) (var ty)), \n Void)))\n\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n\n--********************************\n-- Open terms\n--********************************\n\nVar : EnumU -> Type -> Set\nVar dom _ = EnumT dom\n\nopenTerm : EnumU -> Type -> IDesc Type\nopenTerm dom = exprIDesc (\\ty -> Val ty + Var dom ty) expr\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm ty\ndischarge ty vars context (l value) = con ( EZe , value )\ndischarge ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm ty) context variable \n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), \n Void))\n\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void)\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void )\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (exprFree ** Val)\n\nopenTerm' : EnumU -> Type -> IDesc Type\nopenTerm' dom = toIDesc Type (exprFree ** (\\ty -> Val ty + Var dom ty))\n\n--********************************\n-- Evaluation of open terms'\n--********************************\n\ndischarge' : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm' ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm' ty\ndischarge' ty vars context (l value) = con (EZe , value) \ndischarge' ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm' ty) context variable\n\nchooseDom : (ty : Type)(domNat domBool : EnumU) -> EnumU\nchooseDom nat domNat _ = domNat\nchooseDom bool _ domBool = domBool\n\nchooseGamma : (ty : Type)\n (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool)) ->\n spi dom (\\_ -> IMu closeTerm' ty)\nchooseGamma nat dom gammaNat gammaBool = gammaNat\nchooseGamma bool dom gammaNat gammaBool = gammaBool\n\nsubstExpr : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (sigma : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var dom ty) ->\n IMu closeTerm' ty)\n (ty : Type) ->\n IMu (openTerm' dom) ty ->\n IMu closeTerm' ty\nsubstExpr dom gammaNat gammaBool sig ty term = \n substI (\\ty -> Val ty + Var dom ty)\n Val\n exprFree\n (sig dom gammaNat gammaBool)\n ty\n term\n\n\nsigmaExpr : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var dom ty) ->\n IMu closeTerm' ty\nsigmaExpr dom gammaNat gammaBool ty v = \n discharge' ty\n dom\n (chooseGamma ty dom gammaNat gammaBool)\n v\n\ntest : IMu (openTerm' (consE (consE nilE))) nat ->\n IMu closeTerm' nat\ntest term = substExpr (consE (consE nilE)) \n ( con ( EZe , ze ) , ( con (EZe , su ze ) , Void )) \n ( con ( EZe , true ) , ( con ( EZe , false ) , Void ))\n sigmaExpr nat term\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDescTT where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|] : {I : Set} -> IDesc I -> (I -> Set) -> Set\n[| var i |] P = P i\n[| const X |] P = X\n[| prod D D' |] P = [| D |] P * [| D' |] P\n[| sigma S T |] P = Sigma S (\\s -> [| T s |] P)\n[| pi S T |] P = (s : S) -> [| T s |] P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : [| R i |] (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {I : Set}(D : IDesc I)(P : I -> Set) -> [| D |] P -> IDesc (Sigma I P)\nAll (var i) P x = var (i , x)\nAll (const X) P x = const Unit\nAll (prod D D') P (d , d') = prod (All D P d) (All D' P d')\nAll (sigma S T) P (a , b) = All (T a) P b\nAll (pi S T) P f = pi S (\\s -> All (T s) P (f s))\n\nall : {I : Set}(D : IDesc I)(X : I -> Set)\n (R : Sigma I X -> Set)(P : (x : Sigma I X) -> R x) -> \n (xs : [| D |] X) -> [| All D X xs |] R\nall (var i) X R P x = P (i , x)\nall (const K) X R P k = Void\nall (prod D D') X R P (x , y) = ( all D X R P x , all D' X R P y )\nall (sigma S T) X R P (a , b) = all (T a) X R P b\nall (pi S T) X R P f = \\a -> all (T a) X R P (f a)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI {I} R P m i (con xs) = m i xs (all (R i) (IMu R) P induct xs)\n where induct : (x : Sigma I (IMu R)) -> P x\n induct (i , xs) = indI R P m i xs\n-}\n\n-- But the termination-checker complains, so here we go\n-- inductive-recursive:\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs ))\n where\n\n mutual\n indI : (i : I)(x : IMu R i) -> P (i , x)\n indI i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : [| D |] (IMu R)) -> \n [| All D (IMu R) xs |] P\n hyps (var i) x = indI i x\n hyps (const X) x = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\nindI : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : [| R i |] (IMu R))\n (hs : [| All (R i) (IMu R) xs |] P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\nindI = Elim.indI\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> IDesc Unit\nnatCases Ze = const Unit\nnatCases Suc = var Void\n\nNatD : Unit -> IDesc Unit\nNatD Void = sigma NatConst natCases\n\nNatd : Unit -> Set\nNatd x = IMu NatD x\n\nzed : Natd Void\nzed = con (Ze , Void)\n\nsud : Natd Void -> Natd Void\nsud n = con (Su , n)\n\n\n--****************\n-- Desc\n--****************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\n-- Unlike in Desc.agda, we don't carry the levitation of finite sets\n-- here. We hard-code them and manipulate with standard Agda\n-- machinery. Both presentation are isomorph but, in Agda, the coded\n-- one quickly gets unusable.\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\n-- A special switch, for tagged descriptions. Switching on a\n-- concatenation of finite sets:\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> [| R i |] T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = indI R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : [| D |] (IMu R))\n (ms : [| All D (IMu R) xs |] (\\it -> T (fst it))) -> \n [| D |] T\n replace (var i) T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Meta-language\n--********************************\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n\n\n--********************************\n-- Typed expressions\n--********************************\n\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty),\n (prod (var bool) (prod (var ty) (var ty)), \n Void)))\n\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> [| closeTerm ty |] Val -> Val ty\n evalOneStep _ (EZe , t) = t\n evalOneStep _ ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep _ ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n\n--********************************\n-- Open terms\n--********************************\n\nVar : EnumU -> Type -> Set\nVar dom _ = EnumT dom\n\nopenTerm : EnumU -> Type -> IDesc Type\nopenTerm dom = exprIDesc (\\ty -> Val ty + Var dom ty) expr\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\ndischarge : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm ty\ndischarge ty vars context (l value) = con ( EZe , value )\ndischarge ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm ty) context variable \n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : {I : Set} (R : TagIDesc I)(X : I -> Set) -> TagIDesc I\n((E , ED) , FFD) ** X = ((( consE E , \\ i -> ( const (X i) , ED i ))) , FFD) \n\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : {I : Set}\n (R : TagIDesc I)(X Y : I -> Set) ->\n ((i : I) -> X i -> IMu (toIDesc I (R ** Y)) i) ->\n (i : I) -> \n [| toIDesc I (R ** X) i |] (IMu (toIDesc I (R ** Y))) ->\n IMu (toIDesc I (R ** Y)) i\napply (( E , ED) , (F , FD)) X Y sig i (EZe , x) = sig i x\napply (( E , ED) , (F , FD)) X Y sig i (ESu n , t) = con (ESu n , t)\n\nsubstI : {I : Set} (X Y : I -> Set)(R : TagIDesc I)\n (sigma : (i : I) -> X i -> IMu (toIDesc I (R ** Y)) i)\n (i : I)(D : IMu (toIDesc I (R ** X)) i) ->\n IMu (toIDesc I (R ** Y)) i\nsubstI {I} X Y R sig i term = cata I (toIDesc I (R ** X)) (IMu (toIDesc I (R ** Y))) (apply R X Y sig) i term \n\n\n--********************************************\n-- Hutton's razor is free monad\n--********************************************\n\nexprFreeFixMenu : FixMenu Type\nexprFreeFixMenu = ( consE nilE , \n \\ty -> (prod (var bool) (prod (var ty) (var ty)), \n Void))\n\nchoiceFreeMenu : Type -> EnumU\nchoiceFreeMenu nat = consE nilE\nchoiceFreeMenu bool = consE nilE\n\nchoiceFreeDessert : (ty : Type) -> spi (choiceFreeMenu ty) (\\ _ -> IDesc Type)\nchoiceFreeDessert nat = (prod (var nat) (var nat) , Void)\nchoiceFreeDessert bool = (prod (var nat) (var nat) , Void )\n\nexprFreeSensitiveMenu : SensitiveMenu Type\nexprFreeSensitiveMenu = ( choiceFreeMenu , choiceFreeDessert )\n\nexprFree : TagIDesc Type\nexprFree = exprFreeFixMenu , exprFreeSensitiveMenu\n\nexprFreeC : (Type -> Set) -> TagIDesc Type\nexprFreeC X = exprFree ** X\n\ncloseTerm' : Type -> IDesc Type\ncloseTerm' = toIDesc Type (exprFree ** Val)\n\nopenTerm' : EnumU -> Type -> IDesc Type\nopenTerm' dom = toIDesc Type (exprFree ** (\\ty -> Val ty + Var dom ty))\n\n--********************************\n-- Evaluation of open terms'\n--********************************\n\ndischarge' : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm' ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm' ty\ndischarge' ty vars context (l value) = con (EZe , value) \ndischarge' ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm' ty) context variable\n\nchooseDom : (ty : Type)(domNat domBool : EnumU) -> EnumU\nchooseDom nat domNat _ = domNat\nchooseDom bool _ domBool = domBool\n\nchooseGamma : (ty : Type)\n (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool)) ->\n spi dom (\\_ -> IMu closeTerm' ty)\nchooseGamma nat dom gammaNat gammaBool = gammaNat\nchooseGamma bool dom gammaNat gammaBool = gammaBool\n\nsubstExpr : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (sigma : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var dom ty) ->\n IMu closeTerm' ty)\n (ty : Type) ->\n IMu (openTerm' dom) ty ->\n IMu closeTerm' ty\nsubstExpr dom gammaNat gammaBool sig ty term = \n substI (\\ty -> Val ty + Var dom ty)\n Val\n exprFree\n (sig dom gammaNat gammaBool)\n ty\n term\n\n\nsigmaExpr : (dom : EnumU)\n (gammaNat : spi dom (\\_ -> IMu closeTerm' nat))\n (gammaBool : spi dom (\\_ -> IMu closeTerm' bool))\n (ty : Type) ->\n (Val ty + Var dom ty) ->\n IMu closeTerm' ty\nsigmaExpr dom gammaNat gammaBool ty v = \n discharge' ty\n dom\n (chooseGamma ty dom gammaNat gammaBool)\n v\n\ntest : IMu (openTerm' (consE (consE nilE))) nat ->\n IMu closeTerm' nat\ntest term = substExpr (consE (consE nilE)) \n ( con ( EZe , ze ) , ( con (EZe , su ze ) , Void )) \n ( con ( EZe , true ) , ( con ( EZe , false ) , Void ))\n sigmaExpr nat term\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"2a67bfcd92735cccbdf442cb056b3261f69532be","subject":"Added missing file to a README.agad.","message":"Added missing file to a README.agad.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/DistributiveLaws\/README.agda","new_file":"src\/fot\/DistributiveLaws\/README.agda","new_contents":"------------------------------------------------------------------------------\n-- Distributive laws on a binary operation (Stanovsk\u00fd example)\n------------------------------------------------------------------------------\n\nmodule DistributiveLaws.README where\n\n-- Let _\u00b7_ be a left-associative binary operation which satifies the\n-- left and right distributive axioms:\n--\n-- \u2200 x y z \u2192 x \u2219 (y \u2219 z) \u2261 (x \u2219 y) \u2219 (x \u2219 z)\n-- \u2200 x y z \u2192 (x \u2219 y) \u2219 z \u2261 (x \u2219 z) \u2219 (y \u2219 z).\n\n-- We prove some properties of Stanovsk\u00fd (2008): Task\u00a0B, Lemma\u00a03,\n-- Lemma\u00a04, Lemma\u00a05 (Task\u00a0A) and Lemma\u00a06.\n\n------------------------------------------------------------------------------\n-- The axioms\nopen import DistributiveLaws.Base\n\n-- The interactive and combined proofs\nopen import DistributiveLaws.TaskB-AllStepsATP\nopen import DistributiveLaws.TaskB-HalvedStepsATP\nopen import DistributiveLaws.TaskB-I\nopen import DistributiveLaws.TaskB-TopDownATP\nopen import DistributiveLaws.Lemma3-ATP\nopen import DistributiveLaws.Lemma4-ATP\nopen import DistributiveLaws.Lemma5-ATP\nopen import DistributiveLaws.Lemma6-ATP\n\n-- Unproven theorem by the ATPs\nopen import DistributiveLaws.TaskB.UnprovedATP\n\n------------------------------------------------------------------------------\n-- References:\n--\n-- Stanovsk\u00fd, David (2008). Distributive Groupoids are\n-- Symmetrical-by-Medial: An Elementary Proof. Commentations\n-- Mathematicae Universitatis Carolinae 49.4, pp. 541\u2013546.\n","old_contents":"------------------------------------------------------------------------------\n-- Distributive laws on a binary operation (Stanovsk\u00fd example)\n------------------------------------------------------------------------------\n\nmodule DistributiveLaws.README where\n\n-- Let _\u00b7_ be a left-associative binary operation which satifies the\n-- left and right distributive axioms:\n--\n-- \u2200 x y z \u2192 x \u2219 (y \u2219 z) \u2261 (x \u2219 y) \u2219 (x \u2219 z)\n-- \u2200 x y z \u2192 (x \u2219 y) \u2219 z \u2261 (x \u2219 z) \u2219 (y \u2219 z).\n\n-- We prove some properties of Stanovsk\u00fd (2008): Task\u00a0B, Lemma\u00a03,\n-- Lemma\u00a04, Lemma\u00a05 (Task\u00a0A) and Lemma\u00a06.\n\n------------------------------------------------------------------------------\n-- The axioms\nopen import DistributiveLaws.Base\n\n-- The interactive and combined proofs\nopen import DistributiveLaws.TaskB-HalvedStepsATP\nopen import DistributiveLaws.TaskB-I\nopen import DistributiveLaws.TaskB-TopDownATP\nopen import DistributiveLaws.Lemma3-ATP\nopen import DistributiveLaws.Lemma4-ATP\nopen import DistributiveLaws.Lemma5-ATP\nopen import DistributiveLaws.Lemma6-ATP\n\n-- Unproven theorem by the ATPs\nopen import DistributiveLaws.TaskB.UnprovedATP\n\n------------------------------------------------------------------------------\n-- References:\n--\n-- Stanovsk\u00fd, David (2008). Distributive Groupoids are\n-- Symmetrical-by-Medial: An Elementary Proof. Commentations\n-- Mathematicae Universitatis Carolinae 49.4, pp. 541\u2013546.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3ba9b45f84d23195411b05ae3c14e2a5859aeb61","subject":"Describe the purpose of Model.agda (see #28).","message":"Describe the purpose of Model.agda (see #28).\n\nThere are two comments because I intend to split this file.\n\nOld-commit-hash: f61a7ef89adc97a015021be9ef43504b3b124e96\n","repos":"inc-lc\/ilc-agda","old_file":"Model.agda","new_file":"Model.agda","new_contents":"module Model where\n\n-- SIMPLE TYPES\n--\n-- This module defines the syntax of simple types.\n\n\n-- VALUES\n--\n-- This module defines the model theory of simple types, that is,\n-- it defines for every type, the set of values of that type.\n--\n-- In fact, we only describe a single model here.\n\nimport Relation.Binary as B\n\nopen import Relation.Binary using\n (IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)\nimport Relation.Binary.EqReasoning as EqR\n\nopen import Relation.Nullary using (\u00ac_)\n\nopen import meaning\n\n\n-- SIMPLE TYPES\n\n-- Syntax\n\ndata Type : Set where\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n bool : Type\n\ninfixr 5 _\u21d2_\n\n-- Denotational Semantics\n\ndata Bool : Set where\n true false : Bool\n\nnot : Bool \u2192 Bool\nnot true = false\nnot false = true\n\n_xor_ : Bool \u2192 Bool \u2192 Bool\ntrue xor b = not b\nfalse xor b = b\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 bool \u27e7Type = Bool\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\n-- Value Equivalence\nopen import Level using (zero)\nopen import Relation.Binary.PropositionalEquality\npostulate ext : Extensionality zero zero\n\nnot-not : \u2200 a \u2192 a \u2261 not (not a)\nnot-not true = refl\nnot-not false = refl\n\na-xor-a-false : \u2200 a \u2192 (a xor a) \u2261 false\na-xor-a-false true = refl\na-xor-a-false false = refl\n\na-xor-false-a : \u2200 a \u2192 (false xor a) \u2261 a\na-xor-false-a b = refl\n\nxor-associative : \u2200 a b c \u2192 ((b xor c) xor a) \u2261 (b xor (c xor a))\nxor-associative a true true = not-not a\nxor-associative a true false = refl\nxor-associative a false c = refl\n\na-xor-false : \u2200 a \u2192 a xor false \u2261 a\na-xor-false true = refl\na-xor-false false = refl\n\na-xor-true : \u2200 a \u2192 a xor true \u2261 not a\na-xor-true true = refl\na-xor-true false = refl\n\nxor-commutative : \u2200 a b \u2192 a xor b \u2261 b xor a\nxor-commutative true b rewrite a-xor-true b = refl\nxor-commutative false b rewrite a-xor-false b = refl\n\nxor-cancellative-2 : \u2200 a b \u2192 (b xor (a xor a)) \u2261 b\nxor-cancellative-2 a b rewrite a-xor-a-false a = a-xor-false b\n\nxor-cancellative : \u2200 a b \u2192 ((b xor a) xor a) \u2261 b\nxor-cancellative a b rewrite xor-associative a b a = xor-cancellative-2 a b\n\n\u2261-refl : \u2200 {\u03c4} {v : \u27e6 \u03c4 \u27e7} \u2192\n v \u2261 v\n\u2261-refl = refl\n\n\u2261-sym : \u2200 {\u03c4} {v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2081\n\u2261-sym = sym\n\n\u2261-trans : \u2200 {\u03c4} {v\u2081 v\u2082 v\u2083 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2083 \u2192 v\u2081 \u2261 v\u2083\n\u2261-trans = trans\n\n\u2261-cong : \u2200 {\u03c4\u2082 \u03c4\u2081 v\u2081 v\u2082} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 f v\u2081 \u2261 f v\u2082\n\u2261-cong f = cong f\n\u2261-cong\u2082 : \u2200 {\u03c4\u2083 \u03c4\u2081 \u03c4\u2082 v\u2081 v\u2082 v\u2083 v\u2084} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7 \u2192 \u27e6 \u03c4\u2083 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 f v\u2081 v\u2083 \u2261 f v\u2082 v\u2084\n\u2261-cong\u2082 f = cong\u2082 f\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = \u2261-cong\u2082 (\u03bb x y \u2192 x y)\n\n\u2261-isEquivalence : \u2200 {\u03c4 : Set} \u2192 IsEquivalence (_\u2261_ {A = \u03c4})\n\u2261-isEquivalence = isEquivalence\n\n\u2261-setoid : Type \u2192 Setoid _ _\n\u2261-setoid \u03c4 = record\n { Carrier = \u27e6 \u03c4 \u27e7\n ; _\u2248_ = _\u2261_\n ; isEquivalence = \u2261-isEquivalence\n }\n\n\u2261-consistent : \u00ac (\u2200 (\u03c4 : Type) \u2192 (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192 v\u2081 \u2261 v\u2082)\n\u2261-consistent H with H bool true false\n... | ()\n","old_contents":"module Model where\n\nimport Relation.Binary as B\n\nopen import Relation.Binary using\n (IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)\nimport Relation.Binary.EqReasoning as EqR\n\nopen import Relation.Nullary using (\u00ac_)\n\nopen import meaning\n\n\n-- SIMPLE TYPES\n\n-- Syntax\n\ndata Type : Set where\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n bool : Type\n\ninfixr 5 _\u21d2_\n\n-- Denotational Semantics\n\ndata Bool : Set where\n true false : Bool\n\nnot : Bool \u2192 Bool\nnot true = false\nnot false = true\n\n_xor_ : Bool \u2192 Bool \u2192 Bool\ntrue xor b = not b\nfalse xor b = b\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 bool \u27e7Type = Bool\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\n-- Value Equivalence\nopen import Level using (zero)\nopen import Relation.Binary.PropositionalEquality\npostulate ext : Extensionality zero zero\n\nnot-not : \u2200 a \u2192 a \u2261 not (not a)\nnot-not true = refl\nnot-not false = refl\n\na-xor-a-false : \u2200 a \u2192 (a xor a) \u2261 false\na-xor-a-false true = refl\na-xor-a-false false = refl\n\na-xor-false-a : \u2200 a \u2192 (false xor a) \u2261 a\na-xor-false-a b = refl\n\nxor-associative : \u2200 a b c \u2192 ((b xor c) xor a) \u2261 (b xor (c xor a))\nxor-associative a true true = not-not a\nxor-associative a true false = refl\nxor-associative a false c = refl\n\na-xor-false : \u2200 a \u2192 a xor false \u2261 a\na-xor-false true = refl\na-xor-false false = refl\n\na-xor-true : \u2200 a \u2192 a xor true \u2261 not a\na-xor-true true = refl\na-xor-true false = refl\n\nxor-commutative : \u2200 a b \u2192 a xor b \u2261 b xor a\nxor-commutative true b rewrite a-xor-true b = refl\nxor-commutative false b rewrite a-xor-false b = refl\n\nxor-cancellative-2 : \u2200 a b \u2192 (b xor (a xor a)) \u2261 b\nxor-cancellative-2 a b rewrite a-xor-a-false a = a-xor-false b\n\nxor-cancellative : \u2200 a b \u2192 ((b xor a) xor a) \u2261 b\nxor-cancellative a b rewrite xor-associative a b a = xor-cancellative-2 a b\n\n\u2261-refl : \u2200 {\u03c4} {v : \u27e6 \u03c4 \u27e7} \u2192\n v \u2261 v\n\u2261-refl = refl\n\n\u2261-sym : \u2200 {\u03c4} {v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2081\n\u2261-sym = sym\n\n\u2261-trans : \u2200 {\u03c4} {v\u2081 v\u2082 v\u2083 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2083 \u2192 v\u2081 \u2261 v\u2083\n\u2261-trans = trans\n\n\u2261-cong : \u2200 {\u03c4\u2082 \u03c4\u2081 v\u2081 v\u2082} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 f v\u2081 \u2261 f v\u2082\n\u2261-cong f = cong f\n\u2261-cong\u2082 : \u2200 {\u03c4\u2083 \u03c4\u2081 \u03c4\u2082 v\u2081 v\u2082 v\u2083 v\u2084} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7 \u2192 \u27e6 \u03c4\u2083 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 f v\u2081 v\u2083 \u2261 f v\u2082 v\u2084\n\u2261-cong\u2082 f = cong\u2082 f\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = \u2261-cong\u2082 (\u03bb x y \u2192 x y)\n\n\u2261-isEquivalence : \u2200 {\u03c4 : Set} \u2192 IsEquivalence (_\u2261_ {A = \u03c4})\n\u2261-isEquivalence = isEquivalence\n\n\u2261-setoid : Type \u2192 Setoid _ _\n\u2261-setoid \u03c4 = record\n { Carrier = \u27e6 \u03c4 \u27e7\n ; _\u2248_ = _\u2261_\n ; isEquivalence = \u2261-isEquivalence\n }\n\n\u2261-consistent : \u00ac (\u2200 (\u03c4 : Type) \u2192 (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192 v\u2081 \u2261 v\u2082)\n\u2261-consistent H with H bool true false\n... | ()\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d7de8ffd12566de5f900f5f5c79efb62ebdd7862","subject":"In progress adding StableUnderInjection","message":"In progress adding StableUnderInjection\n","repos":"crypto-agda\/crypto-agda","old_file":"sum-properties.agda","new_file":"sum-properties.agda","new_contents":"module sum-properties where\n\nopen import Type\n\nimport Level as L\n\nopen import Data.Bool.NP\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\n\nopen import Function.NP\n\nopen import sum\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_ ; _\u2257_ ; _\u2257\u2082_)\n\nsum-lem : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA f \u2261 sum \u03bcA (\u03bb x \u2192 f x \u2293 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2238 g x)\nsum-lem \u03bcA f g = \u2261.trans (sum-ext \u03bcA f\u2257f\u2293g+f\u2238g) (sum-hom \u03bcA (\u03bb x \u2192 f x \u2293 g x) (\u03bb x \u2192 f x \u2238 g x))\n where\n f\u2257f\u2293g+f\u2238g : f \u2257 (\u03bb x \u2192 f x \u2293 g x + (f x \u2238 g x))\n f\u2257f\u2293g+f\u2238g x = a\u2261a\u2293b+a\u2238b (f x) (g x)\n\nsum-lem\u2082 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA f + sum \u03bcA g \u2261 sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2293 g x)\nsum-lem\u2082 \u03bcA f g =\n sum \u03bcA f + sum \u03bcA g \u2261\u27e8 \u2261.sym (sum-hom \u03bcA f g) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x + g x) \u2261\u27e8 sum-ext \u03bcA (\u03bb x \u2192 lemma (f x) (g x)) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x \u2294 g x + f x \u2293 g x) \u2261\u27e8 sum-hom \u03bcA (\u03bb x \u2192 f x \u2294 g x) (\u03bb x \u2192 f x \u2293 g x) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2293 g x) \u220e\n where\n open \u2261.\u2261-Reasoning\n lemma : \u2200 a b \u2192 a + b \u2261 a \u2294 b + a \u2293 b\n lemma zero b rewrite \u2115\u00b0.+-comm b 0 = \u2261.refl\n lemma (suc a) zero = \u2261.refl\n lemma (suc a) (suc b) rewrite +-assoc-comm a 1 b\n | +-assoc-comm (a \u2294 b) 1 (a \u2293 b) = \u2261.cong (suc \u2218 suc) (lemma a b)\n\nto\u2115-\u2293 : \u2200 a b \u2192 to\u2115 a \u2293 to\u2115 b \u2261 to\u2115 (a \u2227 b)\nto\u2115-\u2293 true true = \u2261.refl\nto\u2115-\u2293 true false = \u2261.refl\nto\u2115-\u2293 false b = \u2261.refl\n\nto\u2115-\u2294 : \u2200 a b \u2192 to\u2115 a \u2294 to\u2115 b \u2261 to\u2115 (a \u2228 b)\nto\u2115-\u2294 true true = \u2261.refl\nto\u2115-\u2294 true false = \u2261.refl\nto\u2115-\u2294 false b = \u2261.refl\n\nto\u2115-\u2238 : \u2200 a b \u2192 to\u2115 a \u2238 to\u2115 b \u2261 to\u2115 (a \u2227 not b)\nto\u2115-\u2238 true true = \u2261.refl\nto\u2115-\u2238 true false = \u2261.refl\nto\u2115-\u2238 false true = \u2261.refl\nto\u2115-\u2238 false false = \u2261.refl\n\ncount-lem : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool)\n \u2192 count \u03bcA f \u2261 count \u03bcA (\u03bb x \u2192 f x \u2227 g x) + count \u03bcA (\u03bb x \u2192 f x \u2227 not (g x))\ncount-lem \u03bcA f g rewrite sum-lem \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g)\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2293 (f x) (g x)) \n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2238 (f x) (g x)) = \u2261.refl\n\ncount-lem\u2082 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool) \u2192 count \u03bcA f + count \u03bcA g \u2261 count \u03bcA (\u03bb x \u2192 f x \u2228 g x) + count \u03bcA (\u03bb x \u2192 f x \u2227 g x)\ncount-lem\u2082 \u03bcA f g rewrite sum-lem\u2082 \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g)\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2294 (f x) (g x))\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2293 (f x) (g x)) = \u2261.refl\n\n\nsum-\u2294 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) \u2264 sum \u03bcA f + sum \u03bcA g\nsum-\u2294 \u03bcA f g = \u2115\u2264.trans\n (sum-mono \u03bcA (\u03bb x \u2192 \u2294\u2264+ (f x) (g x)))\n (\u2115\u2264.reflexive (sum-hom \u03bcA f g))\n where\n \u2294\u2264+ : \u2200 a b \u2192 a \u2294 b \u2264 a + b\n \u2294\u2264+ zero b = \u2115\u2264.refl\n \u2294\u2264+ (suc a) zero = \u2115\u2264.reflexive (\u2261.cong suc (\u2115\u00b0.+-comm 0 a))\n \u2294\u2264+ (suc a) (suc b) = s\u2264s (\u2115\u2264.trans (\u2294\u2264+ a b) (\u2115\u2264.trans (\u2264-step \u2115\u2264.refl) (\u2115\u2264.reflexive (+-assoc-comm 1 a b))))\n\ncount-\u2228 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool) \u2192 count \u03bcA (\u03bb x \u2192 f x \u2228 g x) \u2264 count \u03bcA f + count \u03bcA g\ncount-\u2228 \u03bcA f g = \u2115\u2264.trans (\u2115\u2264.reflexive (sum-ext \u03bcA (\u03bb x \u2192 \u2261.sym (to\u2115-\u2294 (f x) (g x))))) \n (sum-\u2294 \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g))\n\n\nsum-ext\u2082 : \u2200 {A B}{f g : A \u2192 B \u2192 \u2115}(\u03bcA : SumProp A)(\u03bcB : SumProp B) \u2192 f \u2257\u2082 g \u2192 sum \u03bcA (sum \u03bcB \u2218 f) \u2261 sum \u03bcA (sum \u03bcB \u2218 g)\nsum-ext\u2082 \u03bcA \u03bcB f\u2257g = sum-ext \u03bcA (sum-ext \u03bcB \u2218 f\u2257g)\n\nInjective : \u2200 {a b}{A : Set a}{B : Set b}(f : A \u2192 B) \u2192 Set (a L.\u2294 b)\nInjective f = \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y\n\nStableUnderInjection : \u2200 {A} \u2192 SumProp A \u2192 Set\nStableUnderInjection \u03bc = \u2200 f p \u2192 Injective p \u2192 sum \u03bc f \u2261 sum \u03bc (f \u2218 p)\n\n#-StableUnderInjection : \u2200 {A}{\u03bc : SumProp A} \u2192 StableUnderInjection \u03bc\n \u2192 \u2200 f p \u2192 Injective p \u2192 count \u03bc f \u2261 count \u03bc (f \u2218 p)\n#-StableUnderInjection sui f p p-inj = sui (to\u2115 \u2218 f) p p-inj\n\nopen import Data.Fin using (Fin ; zero ; suc)\n\nrecord Finable {A : Set}(\u03bcA : SumProp A) : Set where\n constructor mk\n field\n FinCard : \u2115\n toFin : A \u2192 Fin (suc FinCard)\n fromFin : Fin (suc FinCard) \u2192 A\n to-inj : Injective toFin\n from-inj : Injective fromFin\n iso : fromFin \u2218 toFin \u2257 id\n sums-ok : \u2200 f \u2192 sum \u03bcA f \u2261 sum (\u03bcFin FinCard) (f \u2218 fromFin)\n\npostulate\n Fin-Stab : \u2200 n \u2192 StableUnderInjection (\u03bcFin n)\n\n\u22a4-Finable : Finable \u03bc\u22a4\n\u22a4-Finable = mk 0 (\u03bb x \u2192 zero) (\u03bb x \u2192 _) (\u03bb x\u2081 \u2192 \u2261.refl) (\u03bb x\u2081 \u2192 help) (\u03bb x \u2192 \u2261.refl) (\u03bb f \u2192 \u2261.refl) \n where help : {x y : Fin 1} \u2192 x \u2261 y\n help {zero} {zero} = \u2261.refl\n help {zero} {suc ()}\n help {suc ()}\n\n+-Finable : \u2200 {A B}(\u03bcA : SumProp A)(\u03bcB : SumProp B) \u2192 Finable \u03bcA \u2192 Finable \u03bcB \u2192 Finable (\u03bcA +\u03bc \u03bcB)\n+-Finable {A}{B} \u03bcA \u03bcB finA finB = mk FinCard toFin fromFin to-inj from-inj iso sums-ok where\n open import Data.Sum\n open import Data.Empty\n\n |A| = suc (Finable.FinCard finA)\n |B| = suc (Finable.FinCard finB)\n\n Fsuc-injective : \u2200 {n} {i j : Fin n} \u2192 Fin.suc i \u2261 suc j \u2192 i \u2261 j\n Fsuc-injective \u2261.refl = \u2261.refl\n \n inj\u2081-inj : \u2200 {A B : Set} {x y : A} \u2192 inj\u2081 {B = B} x \u2261 inj\u2081 y \u2192 x \u2261 y\n inj\u2081-inj \u2261.refl = \u2261.refl\n\n inj\u2082-inj : \u2200 {A B : Set} {x y : B} \u2192 inj\u2082 {A = A} x \u2261 inj\u2082 y \u2192 x \u2261 y\n inj\u2082-inj \u2261.refl = \u2261.refl\n\n fin\u2081 : \u2200 n {m} \u2192 Fin n \u2192 Fin (n + m)\n fin\u2081 zero ()\n fin\u2081 (suc n) zero = zero\n fin\u2081 (suc n) (suc i) = suc (fin\u2081 n i)\n\n fin\u2082 : \u2200 n {m} \u2192 Fin m \u2192 Fin (n + m)\n fin\u2082 zero i = i\n fin\u2082 (suc n) i = suc (fin\u2082 n i)\n\n toFin' : \u2200 {n} {m} \u2192 Fin n \u228e Fin m \u2192 Fin (n + m)\n toFin' {n} (inj\u2081 x) = fin\u2081 n x\n toFin' {n} (inj\u2082 y) = fin\u2082 n y\n\n fin\u2081-inj : \u2200 n {m} \u2192 Injective (fin\u2081 n {m})\n fin\u2081-inj .(suc n) {m} {zero {n}} {zero} x\u2261y = \u2261.refl\n fin\u2081-inj .(suc n) {m} {zero {n}} {suc y} ()\n fin\u2081-inj .(suc n) {m} {suc {n} x} {zero} ()\n fin\u2081-inj .(suc n) {m} {suc {n} x} {suc y} x\u2261y = \u2261.cong suc (fin\u2081-inj n (Fsuc-injective x\u2261y))\n\n fin\u2082-inj : \u2200 n {m} \u2192 Injective (fin\u2082 n {m})\n fin\u2082-inj zero eq = eq\n fin\u2082-inj (suc n) eq = fin\u2082-inj n (Fsuc-injective eq)\n\n fin\u2081\u2260fin\u2082 : \u2200 n {m} x y \u2192 (fin\u2081 n {m} x \u2261 fin\u2082 n y) \u2192 \u22a5\n fin\u2081\u2260fin\u2082 .(suc n) (zero {n}) zero ()\n fin\u2081\u2260fin\u2082 .(suc n) (zero {n}) (suc y) ()\n fin\u2081\u2260fin\u2082 .(suc n) (suc {n} x) zero eq = fin\u2081\u2260fin\u2082 n x zero (Fsuc-injective eq)\n fin\u2081\u2260fin\u2082 .(suc n) (suc {n} x) (suc y) eq = fin\u2081\u2260fin\u2082 n x (suc y) (Fsuc-injective eq)\n\n fromFin' : \u2200 n {m} \u2192 Fin (n + m) \u2192 Fin n \u228e Fin m\n fromFin' zero k = inj\u2082 k\n fromFin' (suc n) zero = inj\u2081 zero\n fromFin' (suc n) (suc k) with fromFin' n k\n ... | inj\u2081 x = inj\u2081 (suc x)\n ... | inj\u2082 y = inj\u2082 y\n\n fromFin'-inj : \u2200 n {m} x y \u2192 fromFin' n {m} x \u2261 fromFin' n y \u2192 x \u2261 y\n fromFin'-inj zero x y eq = inj\u2082-inj eq\n fromFin'-inj (suc n) zero zero eq = \u2261.refl\n fromFin'-inj (suc n) zero (suc y) eq = {!!}\n fromFin'-inj (suc n) (suc x) zero eq = {!!}\n fromFin'-inj (suc n) (suc x) (suc y) eq = {!fromFin'-inj n x y!}\n\n fromFin'-inj\u2081 : \u2200 n {m} x \u2192 fromFin' n {m} (fin\u2081 n x) \u2261 inj\u2081 x\n fromFin'-inj\u2081 zero ()\n fromFin'-inj\u2081 (suc n) zero = \u2261.refl\n fromFin'-inj\u2081 (suc n) {m} (suc x) rewrite fromFin'-inj\u2081 n {m} x = \u2261.refl\n\n fromFin'-inj\u2082 : \u2200 n {m} x \u2192 fromFin' n {m} (fin\u2082 n x) \u2261 inj\u2082 x\n fromFin'-inj\u2082 zero x = \u2261.refl\n fromFin'-inj\u2082 (suc n) x rewrite fromFin'-inj\u2082 n x = \u2261.refl\n\n FinCard : \u2115\n FinCard = Finable.FinCard finA + suc (Finable.FinCard finB)\n\n toFin : A \u228e B \u2192 Fin (suc FinCard)\n toFin (inj\u2081 x) = toFin' (inj\u2081 (Finable.toFin finA x))\n toFin (inj\u2082 y) = toFin' {n = |A|} (inj\u2082 (Finable.toFin finB y))\n\n fromFin : Fin (suc FinCard) \u2192 A \u228e B\n fromFin x+y with fromFin' |A| x+y\n ... | inj\u2081 x = inj\u2081 (Finable.fromFin finA x)\n ... | inj\u2082 y = inj\u2082 (Finable.fromFin finB y)\n\n to-inj : Injective toFin\n to-inj {inj\u2081 x} {inj\u2081 x\u2081} tx\u2261ty = \u2261.cong inj\u2081 (Finable.to-inj finA (fin\u2081-inj |A| tx\u2261ty))\n to-inj {inj\u2081 x} {inj\u2082 y} tx\u2261ty = \u22a5-elim (fin\u2081\u2260fin\u2082 |A| _ _ tx\u2261ty)\n to-inj {inj\u2082 y} {inj\u2081 x} tx\u2261ty = \u22a5-elim (fin\u2081\u2260fin\u2082 |A| _ _ (\u2261.sym tx\u2261ty))\n to-inj {inj\u2082 y} {inj\u2082 y\u2081} tx\u2261ty = \u2261.cong inj\u2082 (Finable.to-inj finB (fin\u2082-inj |A| tx\u2261ty))\n\n from-inj : Injective fromFin\n from-inj {x} {y} fx\u2261fy = {!!}\n\n iso : fromFin \u2218 toFin \u2257 id\n iso (inj\u2081 x) rewrite fromFin'-inj\u2081 |A| {|B|} (Finable.toFin finA x) | Finable.iso finA x = \u2261.refl\n iso (inj\u2082 y) rewrite fromFin'-inj\u2082 |A| {|B|} (Finable.toFin finB y) | Finable.iso finB y = \u2261.refl\n\n sums-ok : \u2200 f \u2192 sum (\u03bcA +\u03bc \u03bcB) f \u2261 sum (\u03bcFin FinCard) (f \u2218 fromFin)\n sums-ok f =\n sum (\u03bcA +\u03bc \u03bcB) f\n \u2261\u27e8 \u2261.cong\u2082 _+_ (Finable.sums-ok finA (f \u2218 inj\u2081))\n (Finable.sums-ok finB (f \u2218 inj\u2082)) \u27e9\n sum (\u03bcFin (Finable.FinCard finA)) (f \u2218 inj\u2081 \u2218 Finable.fromFin finA)\n + sum (\u03bcFin (Finable.FinCard finB)) (f \u2218 inj\u2082 \u2218 Finable.fromFin finB)\n \u2261\u27e8 {!!} \u27e9\n sum (\u03bcFin FinCard) (f \u2218 fromFin)\n \u220e\n where open \u2261.\u2261-Reasoning\n\nStableIfFinable : \u2200 {A} (\u03bcA : SumProp A) \u2192 Finable \u03bcA \u2192 StableUnderInjection \u03bcA\nStableIfFinable \u03bcA fin f p p-inj\n = sum \u03bcA f\n \u2261\u27e8 sums-ok f \u27e9\n sum (\u03bcFin FinCard) (f \u2218 fromFin)\n \u2261\u27e8 Fin-Stab FinCard (f \u2218 fromFin) (toFin \u2218 p \u2218 fromFin) (from-inj \u2218 p-inj \u2218 to-inj) \u27e9\n sum (\u03bcFin FinCard) (f \u2218 fromFin \u2218 toFin \u2218 p \u2218 fromFin)\n \u2261\u27e8 sum-ext (\u03bcFin FinCard) (\u03bb x \u2192 \u2261.cong f (iso (p (fromFin x)))) \u27e9\n sum (\u03bcFin FinCard) (f \u2218 p \u2218 fromFin)\n \u2261\u27e8 \u2261.sym (sums-ok (f \u2218 p)) \u27e9\n sum \u03bcA (f \u2218 p)\n \u220e\n where open \u2261.\u2261-Reasoning\n open Finable fin\n\n\n{-\nmodule _ where\n open import bijection-fin\n open import Data.Vec.NP\n\n \u03bcFinSUI : \u2200 {n} \u2192 StableUnderInjection (\u03bcFin n)\n \u03bcFinSUI {n} f p p-inj rewrite \u2261.sym (tabulate-\u2218 f id)\n | \u2261.sym (tabulate-\u2218 (f \u2218 p) id) = count-perm f p (\u03bb x y \u2192 p-inj)\n-}\n\n\n","old_contents":"module sum-properties where\n\nopen import Type\n\nimport Level as L\n\nopen import Data.Bool.NP\nopen import Data.Nat.NP\nopen import Data.Nat.Properties\n\nopen import Function.NP\n\nopen import sum\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_ ; _\u2257_ ; _\u2257\u2082_)\n\nsum-lem : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA f \u2261 sum \u03bcA (\u03bb x \u2192 f x \u2293 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2238 g x)\nsum-lem \u03bcA f g = \u2261.trans (sum-ext \u03bcA f\u2257f\u2293g+f\u2238g) (sum-hom \u03bcA (\u03bb x \u2192 f x \u2293 g x) (\u03bb x \u2192 f x \u2238 g x))\n where\n f\u2257f\u2293g+f\u2238g : f \u2257 (\u03bb x \u2192 f x \u2293 g x + (f x \u2238 g x))\n f\u2257f\u2293g+f\u2238g x = a\u2261a\u2293b+a\u2238b (f x) (g x)\n\nsum-lem\u2082 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA f + sum \u03bcA g \u2261 sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2293 g x)\nsum-lem\u2082 \u03bcA f g =\n sum \u03bcA f + sum \u03bcA g \u2261\u27e8 \u2261.sym (sum-hom \u03bcA f g) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x + g x) \u2261\u27e8 sum-ext \u03bcA (\u03bb x \u2192 lemma (f x) (g x)) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x \u2294 g x + f x \u2293 g x) \u2261\u27e8 sum-hom \u03bcA (\u03bb x \u2192 f x \u2294 g x) (\u03bb x \u2192 f x \u2293 g x) \u27e9\n sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) + sum \u03bcA (\u03bb x \u2192 f x \u2293 g x) \u220e\n where\n open \u2261.\u2261-Reasoning\n lemma : \u2200 a b \u2192 a + b \u2261 a \u2294 b + a \u2293 b\n lemma zero b rewrite \u2115\u00b0.+-comm b 0 = \u2261.refl\n lemma (suc a) zero = \u2261.refl\n lemma (suc a) (suc b) rewrite +-assoc-comm a 1 b\n | +-assoc-comm (a \u2294 b) 1 (a \u2293 b) = \u2261.cong (suc \u2218 suc) (lemma a b)\n\nto\u2115-\u2293 : \u2200 a b \u2192 to\u2115 a \u2293 to\u2115 b \u2261 to\u2115 (a \u2227 b)\nto\u2115-\u2293 true true = \u2261.refl\nto\u2115-\u2293 true false = \u2261.refl\nto\u2115-\u2293 false b = \u2261.refl\n\nto\u2115-\u2294 : \u2200 a b \u2192 to\u2115 a \u2294 to\u2115 b \u2261 to\u2115 (a \u2228 b)\nto\u2115-\u2294 true true = \u2261.refl\nto\u2115-\u2294 true false = \u2261.refl\nto\u2115-\u2294 false b = \u2261.refl\n\nto\u2115-\u2238 : \u2200 a b \u2192 to\u2115 a \u2238 to\u2115 b \u2261 to\u2115 (a \u2227 not b)\nto\u2115-\u2238 true true = \u2261.refl\nto\u2115-\u2238 true false = \u2261.refl\nto\u2115-\u2238 false true = \u2261.refl\nto\u2115-\u2238 false false = \u2261.refl\n\ncount-lem : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool)\n \u2192 count \u03bcA f \u2261 count \u03bcA (\u03bb x \u2192 f x \u2227 g x) + count \u03bcA (\u03bb x \u2192 f x \u2227 not (g x))\ncount-lem \u03bcA f g rewrite sum-lem \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g)\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2293 (f x) (g x)) \n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2238 (f x) (g x)) = \u2261.refl\n\ncount-lem\u2082 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool) \u2192 count \u03bcA f + count \u03bcA g \u2261 count \u03bcA (\u03bb x \u2192 f x \u2228 g x) + count \u03bcA (\u03bb x \u2192 f x \u2227 g x)\ncount-lem\u2082 \u03bcA f g rewrite sum-lem\u2082 \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g)\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2294 (f x) (g x))\n | sum-ext \u03bcA (\u03bb x \u2192 to\u2115-\u2293 (f x) (g x)) = \u2261.refl\n\n\nsum-\u2294 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 \u2115) \u2192 sum \u03bcA (\u03bb x \u2192 f x \u2294 g x) \u2264 sum \u03bcA f + sum \u03bcA g\nsum-\u2294 \u03bcA f g = \u2115\u2264.trans\n (sum-mono \u03bcA (\u03bb x \u2192 \u2294\u2264+ (f x) (g x)))\n (\u2115\u2264.reflexive (sum-hom \u03bcA f g))\n where\n \u2294\u2264+ : \u2200 a b \u2192 a \u2294 b \u2264 a + b\n \u2294\u2264+ zero b = \u2115\u2264.refl\n \u2294\u2264+ (suc a) zero = \u2115\u2264.reflexive (\u2261.cong suc (\u2115\u00b0.+-comm 0 a))\n \u2294\u2264+ (suc a) (suc b) = s\u2264s (\u2115\u2264.trans (\u2294\u2264+ a b) (\u2115\u2264.trans (\u2264-step \u2115\u2264.refl) (\u2115\u2264.reflexive (+-assoc-comm 1 a b))))\n\ncount-\u2228 : \u2200 {A : \u2605}(\u03bcA : SumProp A)(f g : A \u2192 Bool) \u2192 count \u03bcA (\u03bb x \u2192 f x \u2228 g x) \u2264 count \u03bcA f + count \u03bcA g\ncount-\u2228 \u03bcA f g = \u2115\u2264.trans (\u2115\u2264.reflexive (sum-ext \u03bcA (\u03bb x \u2192 \u2261.sym (to\u2115-\u2294 (f x) (g x))))) \n (sum-\u2294 \u03bcA (to\u2115 \u2218 f) (to\u2115 \u2218 g))\n\n\nsum-ext\u2082 : \u2200 {A B}{f g : A \u2192 B \u2192 \u2115}(\u03bcA : SumProp A)(\u03bcB : SumProp B) \u2192 f \u2257\u2082 g \u2192 sum \u03bcA (sum \u03bcB \u2218 f) \u2261 sum \u03bcA (sum \u03bcB \u2218 g)\nsum-ext\u2082 \u03bcA \u03bcB f\u2257g = sum-ext \u03bcA (sum-ext \u03bcB \u2218 f\u2257g)\n\nInjective : \u2200 {a b}{A : Set a}{B : Set b}(f : A \u2192 B) \u2192 Set (a L.\u2294 b)\nInjective f = \u2200 {x y} \u2192 f x \u2261 f y \u2192 x \u2261 y\n\nStableUnderInjection : \u2200 {A} \u2192 SumProp A \u2192 Set\nStableUnderInjection \u03bc = \u2200 f p \u2192 Injective p \u2192 sum \u03bc f \u2261 sum \u03bc (f \u2218 p)\n\nmodule _ where\n open import bijection-fin\n open import Data.Vec.NP\n\n \u03bcFinSUI : \u2200 {n} \u2192 StableUnderInjection (\u03bcFin n)\n \u03bcFinSUI {n} f p p-inj rewrite \u2261.sym (tabulate-\u2218 f id)\n | \u2261.sym (tabulate-\u2218 (f \u2218 p) id) = count-perm f p (\u03bb x y \u2192 p-inj)\n\n\n#-StableUnderInjection : \u2200 {A}{\u03bc : SumProp A} \u2192 StableUnderInjection \u03bc\n \u2192 \u2200 f p \u2192 Injective p \u2192 count \u03bc f \u2261 count \u03bc (f \u2218 p)\n#-StableUnderInjection sui f p p-inj = sui (to\u2115 \u2218 f) p p-inj\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"74de191538e421fd0a62f67344c5e83fd18f7612","subject":"possible agda bug demonstration","message":"possible agda bug demonstration\n","repos":"xekoukou\/sparrow,xekoukou\/sparrow,xekoukou\/sparrow,xekoukou\/sparrow","old_file":"agda\/IndexLF.agda","new_file":"agda\/IndexLF.agda","new_contents":"\nmodule IndexLF where\n\nopen import Common\nopen import LinLogic\n--open import LinDepT\n--open import LinT \n--open import SetLL\n--open import SetLLProp\n--open import SetLLRem\nopen import LinFun\n\nopen import Data.Product\n\n\ndata IndexLFC {i u} : \u2200{ll rll} \u2192 LFun {i} {u} ll rll \u2192 Set where\n \u2193c : \u2200{ll \u221erll prf \u221elf} \u2192 IndexLFC (call {i} {u} {ll} {\u221erll} {prf} \u221elf)\n _\u2190\u2282 : \u2200{rll pll ell ll ind elf lf}\n \u2192 IndexLFC elf\n \u2192 IndexLFC (_\u2282_ {i} {u} {pll} {ll} {ell} {rll} {ind} elf lf)\n \u2282\u2192_ : \u2200{rll pll ell ll ind elf lf}\n \u2192 IndexLFC lf\n \u2192 IndexLFC (_\u2282_ {i} {u} {pll} {ll} {ell} {rll} {ind} elf lf)\n tr : \u2200{ll orll rll} \u2192 {ltr : LLTr orll ll} \u2192 {lf : LFun {i} {u} orll rll}\n \u2192 IndexLFC lf \u2192 IndexLFC (tr ltr lf) \n com : \u2200{rll ll frll prfi prfo df lf}\n \u2192 IndexLFC lf\n \u2192 IndexLFC (com {i} {u} {rll} {ll} {frll} {{prfi}} {{prfo}} df lf)\n\n\n\ndata SetLFC {i u oll orll} (olf : LFun {i} {u} oll orll) : \u2200{ll rll} \u2192 LFun {i} {u} ll rll \u2192 Set (lsuc u) where\n \u2193c : \u2200{ll \u221erll prf \u221elf} \u2192 IndexLFC olf \u2192 SetLFC olf (call {i} {u} {ll} {\u221erll} {prf} \u221elf)\n _\u2190\u2282 : \u2200{rll pll ell ll ind elf lf}\n \u2192 SetLFC olf elf\n \u2192 SetLFC olf (_\u2282_ {i} {u} {pll} {ll} {ell} {rll} {ind} elf lf)\n \u2282\u2192_ : \u2200{rll pll ell ll ind elf lf}\n \u2192 SetLFC olf lf\n \u2192 SetLFC olf (_\u2282_ {i} {u} {pll} {ll} {ell} {rll} {ind} elf lf)\n _\u2190\u2282\u2192_ : \u2200{rll pll ell ll ind elf lf}\n \u2192 SetLFC olf elf\n \u2192 SetLFC olf lf\n \u2192 SetLFC olf (_\u2282_ {i} {u} {pll} {ll} {ell} {rll} {ind} elf lf)\n tr : \u2200{ll orll rll} \u2192 {ltr : LLTr orll ll} \u2192 {lf : LFun {i} {u} orll rll}\n \u2192 SetLFC olf lf\n \u2192 SetLFC olf (tr ltr lf) \n com : \u2200{rll ll frll prfi prfo df lf}\n \u2192 SetLFC olf lf\n \u2192 SetLFC olf (com {i} {u} {rll} {ll} {frll} {{prfi}} {{prfo}} df lf)\n\ndata MSetLFC {i u oll orll} (olf : LFun {i} {u} oll orll) : \u2200{ll rll} \u2192 LFun {i} {u} ll rll \u2192 Set (lsuc u) where\n \u2205 : \u2200{ll rll} \u2192 {lf : LFun {i} {u} ll rll} \u2192 MSetLFC olf lf\n \u00ac\u2205 : \u2200{ll rll} \u2192 {lf : LFun {i} {u} ll rll} \u2192 SetLFC olf lf \u2192 MSetLFC olf lf\n\n\u2205-addLFC : \u2200{i u oll orll ll rll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 {lf : LFun {i} {u} ll rll} \u2192 IndexLFC olf \u2192 IndexLFC lf \u2192 SetLFC olf lf \n\u2205-addLFC oic \u2193c = \u2193c oic\n\u2205-addLFC oic (ic \u2190\u2282) = (\u2205-addLFC oic ic) \u2190\u2282\n\u2205-addLFC oic (\u2282\u2192 ic) = \u2282\u2192 (\u2205-addLFC oic ic)\n\u2205-addLFC oic (tr ic) = tr (\u2205-addLFC oic ic)\n\u2205-addLFC oic (com ic) = com (\u2205-addLFC oic ic)\n\naddLFC : \u2200{i u oll orll ll rll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 {lf : LFun {i} {u} ll rll} \u2192 SetLFC olf lf \u2192 IndexLFC olf \u2192 IndexLFC lf \u2192 SetLFC olf lf \naddLFC (\u2193c x) oic \u2193c = \u2193c oic -- replace\naddLFC (s \u2190\u2282) oic (ic \u2190\u2282) = (addLFC s oic ic) \u2190\u2282\naddLFC (\u2282\u2192 s) oic (ic \u2190\u2282) = (\u2205-addLFC oic ic) \u2190\u2282\u2192 s\naddLFC (s \u2190\u2282\u2192 s\u2081) oic (ic \u2190\u2282) = (addLFC s oic ic) \u2190\u2282\u2192 s\u2081\naddLFC (s \u2190\u2282) oic (\u2282\u2192 ic) = s \u2190\u2282\u2192 (\u2205-addLFC oic ic)\naddLFC (\u2282\u2192 s) oic (\u2282\u2192 ic) = \u2282\u2192 (addLFC s oic ic)\naddLFC (s \u2190\u2282\u2192 s\u2081) oic (\u2282\u2192 ic) = s \u2190\u2282\u2192 (addLFC s\u2081 oic ic)\naddLFC (tr s) oic (tr ic) = tr (addLFC s oic ic)\naddLFC (com s) oic (com ic) = com (addLFC s oic ic)\n\n\nmaddLFC : \u2200{i u oll orll ll rll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 {lf : LFun {i} {u} ll rll} \u2192 MSetLFC olf lf \u2192 IndexLFC olf \u2192 IndexLFC lf \u2192 MSetLFC olf lf\nmaddLFC \u2205 oic ic = \u00ac\u2205 (\u2205-addLFC oic ic)\nmaddLFC (\u00ac\u2205 x) oic ic = \u00ac\u2205 (addLFC x oic ic)\n\ndata SetLFCRem {i u oll orll} (olf : LFun {i} {u} oll orll) : LinLogic i {u} \u2192 Set (lsuc u) where\n \u2193c : \u2200{\u221ell} \u2192 IndexLFC {i} olf \u2192 SetLFCRem olf (call \u221ell)\n _\u2190\u2227 : \u2200{rs ls} \u2192 SetLFCRem olf ls \u2192 SetLFCRem olf (ls \u2227 rs)\n \u2227\u2192_ : \u2200{rs ls} \u2192 SetLFCRem olf rs \u2192 SetLFCRem olf (ls \u2227 rs)\n _\u2190\u2227\u2192_ : \u2200{rs ls} \u2192 SetLFCRem olf ls \u2192 SetLFCRem olf rs \u2192 SetLFCRem olf (ls \u2227 rs)\n _\u2190\u2228 : \u2200{rs ls} \u2192 SetLFCRem olf ls \u2192 SetLFCRem olf (ls \u2228 rs)\n \u2228\u2192_ : \u2200{rs ls} \u2192 SetLFCRem olf rs \u2192 SetLFCRem olf (ls \u2228 rs)\n _\u2190\u2228\u2192_ : \u2200{rs ls} \u2192 SetLFCRem olf ls \u2192 SetLFCRem olf rs \u2192 SetLFCRem olf (ls \u2228 rs)\n _\u2190\u2202 : \u2200{rs ls} \u2192 SetLFCRem olf ls \u2192 SetLFCRem olf (ls \u2202 rs)\n \u2202\u2192_ : \u2200{rs ls} \u2192 SetLFCRem olf rs \u2192 SetLFCRem olf (ls \u2202 rs)\n _\u2190\u2202\u2192_ : \u2200{rs ls} \u2192 SetLFCRem olf ls \u2192 SetLFCRem olf rs \u2192 SetLFCRem olf (ls \u2202 rs)\n\ndata MSetLFCRem {i u oll orll} (olf : LFun {i} {u} oll orll) : LinLogic i {u} \u2192 Set (lsuc u) where\n \u2205 : \u2200{ll} \u2192 MSetLFCRem olf ll\n \u00ac\u2205 : \u2200{ll} \u2192 SetLFCRem olf ll \u2192 MSetLFCRem olf ll\n\n\u2205-addLFCRem : \u2200{i u ll \u221erll oll orll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 (ind : IndexLL {i} {u} (call \u221erll) ll) \u2192 IndexLFC olf\n \u2192 SetLFCRem olf ll\n\u2205-addLFCRem \u2193 m = \u2193c m\n\u2205-addLFCRem (ind \u2190\u2227) m = (\u2205-addLFCRem ind m) \u2190\u2227\n\u2205-addLFCRem (\u2227\u2192 ind) m = \u2227\u2192 (\u2205-addLFCRem ind m)\n\u2205-addLFCRem (ind \u2190\u2228) m = (\u2205-addLFCRem ind m) \u2190\u2228\n\u2205-addLFCRem (\u2228\u2192 ind) m = \u2228\u2192 (\u2205-addLFCRem ind m)\n\u2205-addLFCRem (ind \u2190\u2202) m = (\u2205-addLFCRem ind m) \u2190\u2202\n\u2205-addLFCRem (\u2202\u2192 ind) m = \u2202\u2192 (\u2205-addLFCRem ind m)\n\naddLFCRem : \u2200{i u ll \u221erll oll orll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 SetLFCRem olf ll \u2192 (ind : IndexLL {i} {u} (call \u221erll) ll) \u2192 IndexLFC olf\n \u2192 SetLFCRem olf ll\naddLFCRem (\u2193c rm) ind m = \u2193c m\naddLFCRem (s \u2190\u2227) (ind \u2190\u2227) m = (addLFCRem s ind m) \u2190\u2227\naddLFCRem (s \u2190\u2227) (\u2227\u2192 ind) m = s \u2190\u2227\u2192 (\u2205-addLFCRem ind m)\naddLFCRem (\u2227\u2192 s) (ind \u2190\u2227) m = (\u2205-addLFCRem ind m) \u2190\u2227\u2192 s\naddLFCRem (\u2227\u2192 s) (\u2227\u2192 ind) m = \u2227\u2192 addLFCRem s ind m\naddLFCRem (s \u2190\u2227\u2192 s\u2081) (ind \u2190\u2227) m = (addLFCRem s ind m) \u2190\u2227\u2192 s\u2081\naddLFCRem (s \u2190\u2227\u2192 s\u2081) (\u2227\u2192 ind) m = s \u2190\u2227\u2192 (addLFCRem s\u2081 ind m)\naddLFCRem (s \u2190\u2228) (ind \u2190\u2228) m = (addLFCRem s ind m) \u2190\u2228\naddLFCRem (s \u2190\u2228) (\u2228\u2192 ind) m = s \u2190\u2228\u2192 (\u2205-addLFCRem ind m)\naddLFCRem (\u2228\u2192 s) (ind \u2190\u2228) m = (\u2205-addLFCRem ind m) \u2190\u2228\u2192 s\naddLFCRem (\u2228\u2192 s) (\u2228\u2192 ind) m = \u2228\u2192 addLFCRem s ind m\naddLFCRem (s \u2190\u2228\u2192 s\u2081) (ind \u2190\u2228) m = (addLFCRem s ind m) \u2190\u2228\u2192 s\u2081\naddLFCRem (s \u2190\u2228\u2192 s\u2081) (\u2228\u2192 ind) m = s \u2190\u2228\u2192 (addLFCRem s\u2081 ind m)\naddLFCRem (s \u2190\u2202) (ind \u2190\u2202) m = (addLFCRem s ind m) \u2190\u2202\naddLFCRem (s \u2190\u2202) (\u2202\u2192 ind) m = s \u2190\u2202\u2192 (\u2205-addLFCRem ind m)\naddLFCRem (\u2202\u2192 s) (ind \u2190\u2202) m = (\u2205-addLFCRem ind m) \u2190\u2202\u2192 s\naddLFCRem (\u2202\u2192 s) (\u2202\u2192 ind) m = \u2202\u2192 addLFCRem s ind m\naddLFCRem (s \u2190\u2202\u2192 s\u2081) (ind \u2190\u2202) m = (addLFCRem s ind m) \u2190\u2202\u2192 s\u2081\naddLFCRem (s \u2190\u2202\u2192 s\u2081) (\u2202\u2192 ind) m = s \u2190\u2202\u2192 (addLFCRem s\u2081 ind m)\n\nmadd : \u2200{i u ll \u221erll oll orll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 MSetLFCRem olf ll \u2192 (ind : IndexLL {i} {u} (call \u221erll) ll) \u2192 IndexLFC olf\n \u2192 MSetLFCRem olf ll\nmadd \u2205 ind m = \u00ac\u2205 (\u2205-addLFCRem ind m)\nmadd (\u00ac\u2205 x) ind m = \u00ac\u2205 (addLFCRem x ind m)\n\n\ntruncSetLFCRem : \u2200{i} \u2192 \u2200{u ll oll orll q} \u2192 {olf : LFun {i} {u} oll orll} \u2192 MSetLFCRem {i} {u} olf ll \u2192 (ind : IndexLL {i} {u} q ll) \u2192 MSetLFCRem {i} olf q\ntruncSetLFCRem \u2205 ind = \u2205\ntruncSetLFCRem (\u00ac\u2205 x) \u2193 = \u00ac\u2205 x\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2227)) (ind \u2190\u2227) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (\u2227\u2192 x)) (ind \u2190\u2227) = \u2205\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2227\u2192 x\u2081)) (ind \u2190\u2227) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2227)) (\u2227\u2192 ind) = \u2205\ntruncSetLFCRem (\u00ac\u2205 (\u2227\u2192 x)) (\u2227\u2192 ind) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2227\u2192 x\u2081)) (\u2227\u2192 ind) = truncSetLFCRem (\u00ac\u2205 x\u2081) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2228)) (ind \u2190\u2228) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (\u2228\u2192 x)) (ind \u2190\u2228) = \u2205\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2228\u2192 x\u2081)) (ind \u2190\u2228) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2228)) (\u2228\u2192 ind) = \u2205\ntruncSetLFCRem (\u00ac\u2205 (\u2228\u2192 x)) (\u2228\u2192 ind) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2228\u2192 x\u2081)) (\u2228\u2192 ind) = truncSetLFCRem (\u00ac\u2205 x\u2081) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2202)) (ind \u2190\u2202) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (\u2202\u2192 x)) (ind \u2190\u2202) = \u2205\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2202\u2192 x\u2081)) (ind \u2190\u2202) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2202)) (\u2202\u2192 ind) = \u2205\ntruncSetLFCRem (\u00ac\u2205 (\u2202\u2192 x)) (\u2202\u2192 ind) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2202\u2192 x\u2081)) (\u2202\u2192 ind) = truncSetLFCRem (\u00ac\u2205 x\u2081) ind\n\ndelLFCRem : \u2200{i u oll orll ll pll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 SetLFCRem {i} olf ll \u2192 (ind : IndexLL {i} {u} pll ll) \u2192 (rll : LinLogic i)\n \u2192 MSetLFCRem {i} olf (replLL ll ind rll)\ndelLFCRem s \u2193 rll = \u2205\ndelLFCRem (s \u2190\u2227) (ind \u2190\u2227) rll with (delLFCRem s ind rll)\ndelLFCRem (s \u2190\u2227) (ind \u2190\u2227) rll | \u2205 = \u2205\ndelLFCRem (s \u2190\u2227) (ind \u2190\u2227) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2227)\ndelLFCRem (\u2227\u2192 s) (ind \u2190\u2227) rll = \u00ac\u2205 (\u2227\u2192 (s))\ndelLFCRem (s \u2190\u2227\u2192 s\u2081) (ind \u2190\u2227) rll with (delLFCRem s ind rll)\ndelLFCRem (s \u2190\u2227\u2192 s\u2081) (ind \u2190\u2227) rll | \u2205 = \u00ac\u2205 (\u2227\u2192 (s\u2081))\ndelLFCRem (s \u2190\u2227\u2192 s\u2081) (ind \u2190\u2227) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2227\u2192 (s\u2081))\ndelLFCRem (s \u2190\u2227) (\u2227\u2192 ind) rll = \u00ac\u2205 ((s) \u2190\u2227)\ndelLFCRem (\u2227\u2192 s) (\u2227\u2192 ind) rll with (delLFCRem s ind rll)\ndelLFCRem (\u2227\u2192 s) (\u2227\u2192 ind) rll | \u2205 = \u2205\ndelLFCRem (\u2227\u2192 s) (\u2227\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 (\u2227\u2192 x)\ndelLFCRem (s \u2190\u2227\u2192 s\u2081) (\u2227\u2192 ind) rll with (delLFCRem s\u2081 ind rll)\ndelLFCRem (s \u2190\u2227\u2192 s\u2081) (\u2227\u2192 ind) rll | \u2205 = \u00ac\u2205 ((s) \u2190\u2227)\ndelLFCRem (s \u2190\u2227\u2192 s\u2081) (\u2227\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 ((s) \u2190\u2227\u2192 x)\ndelLFCRem (s \u2190\u2228) (ind \u2190\u2228) rll with (delLFCRem s ind rll)\ndelLFCRem (s \u2190\u2228) (ind \u2190\u2228) rll | \u2205 = \u2205\ndelLFCRem (s \u2190\u2228) (ind \u2190\u2228) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2228)\ndelLFCRem (\u2228\u2192 s) (ind \u2190\u2228) rll = \u00ac\u2205 (\u2228\u2192 (s))\ndelLFCRem (s \u2190\u2228\u2192 s\u2081) (ind \u2190\u2228) rll with (delLFCRem s ind rll)\ndelLFCRem (s \u2190\u2228\u2192 s\u2081) (ind \u2190\u2228) rll | \u2205 = \u00ac\u2205 (\u2228\u2192 (s\u2081))\ndelLFCRem (s \u2190\u2228\u2192 s\u2081) (ind \u2190\u2228) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2228\u2192 (s\u2081))\ndelLFCRem (s \u2190\u2228) (\u2228\u2192 ind) rll = \u00ac\u2205 ((s) \u2190\u2228)\ndelLFCRem (\u2228\u2192 s) (\u2228\u2192 ind) rll with (delLFCRem s ind rll)\ndelLFCRem (\u2228\u2192 s) (\u2228\u2192 ind) rll | \u2205 = \u2205\ndelLFCRem (\u2228\u2192 s) (\u2228\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 (\u2228\u2192 x)\ndelLFCRem (s \u2190\u2228\u2192 s\u2081) (\u2228\u2192 ind) rll with (delLFCRem s\u2081 ind rll)\ndelLFCRem (s \u2190\u2228\u2192 s\u2081) (\u2228\u2192 ind) rll | \u2205 = \u00ac\u2205 ((s) \u2190\u2228)\ndelLFCRem (s \u2190\u2228\u2192 s\u2081) (\u2228\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 ((s) \u2190\u2228\u2192 x)\ndelLFCRem (s \u2190\u2202) (ind \u2190\u2202) rll with (delLFCRem s ind rll)\ndelLFCRem (s \u2190\u2202) (ind \u2190\u2202) rll | \u2205 = \u2205\ndelLFCRem (s \u2190\u2202) (ind \u2190\u2202) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2202)\ndelLFCRem (\u2202\u2192 s) (ind \u2190\u2202) rll = \u00ac\u2205 (\u2202\u2192 (s))\ndelLFCRem (s \u2190\u2202\u2192 s\u2081) (ind \u2190\u2202) rll with (delLFCRem s ind rll)\ndelLFCRem (s \u2190\u2202\u2192 s\u2081) (ind \u2190\u2202) rll | \u2205 = \u00ac\u2205 (\u2202\u2192 (s\u2081))\ndelLFCRem (s \u2190\u2202\u2192 s\u2081) (ind \u2190\u2202) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2202\u2192 (s\u2081))\ndelLFCRem (s \u2190\u2202) (\u2202\u2192 ind) rll = \u00ac\u2205 ((s) \u2190\u2202)\ndelLFCRem (\u2202\u2192 s) (\u2202\u2192 ind) rll with (delLFCRem s ind rll)\ndelLFCRem (\u2202\u2192 s) (\u2202\u2192 ind) rll | \u2205 = \u2205\ndelLFCRem (\u2202\u2192 s) (\u2202\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 (\u2202\u2192 x)\ndelLFCRem (s \u2190\u2202\u2192 s\u2081) (\u2202\u2192 ind) rll with (delLFCRem s\u2081 ind rll)\ndelLFCRem (s \u2190\u2202\u2192 s\u2081) (\u2202\u2192 ind) rll | \u2205 = \u00ac\u2205 ((s) \u2190\u2202)\ndelLFCRem (s \u2190\u2202\u2192 s\u2081) (\u2202\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 ((s) \u2190\u2202\u2192 x)\n\nmdelLFCRem : \u2200{i u oll orll ll pll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 MSetLFCRem {i} olf ll \u2192 (ind : IndexLL {i} {u} pll ll) \u2192 (rll : LinLogic i)\n \u2192 MSetLFCRem {i} olf (replLL ll ind rll)\nmdelLFCRem \u2205 ind rll = \u2205\nmdelLFCRem (\u00ac\u2205 x) ind rll = delLFCRem x ind rll\n\ntranLFCRem : \u2200{i u oll orll ll rll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 SetLFCRem {i} olf ll \u2192 (tr : LLTr {i} {u} rll ll)\n \u2192 SetLFCRem olf rll\ntranLFCRem s I = s\ntranLFCRem (s \u2190\u2202) (\u2202c ltr) = tranLFCRem (\u2202\u2192 s) ltr\ntranLFCRem (\u2202\u2192 s) (\u2202c ltr) = tranLFCRem (s \u2190\u2202) ltr\ntranLFCRem (s \u2190\u2202\u2192 s\u2081) (\u2202c ltr) = tranLFCRem (s\u2081 \u2190\u2202\u2192 s) ltr\ntranLFCRem (s \u2190\u2228) (\u2228c ltr) = tranLFCRem (\u2228\u2192 s) ltr\ntranLFCRem (\u2228\u2192 s) (\u2228c ltr) = tranLFCRem (s \u2190\u2228) ltr\ntranLFCRem (s \u2190\u2228\u2192 s\u2081) (\u2228c ltr) = tranLFCRem (s\u2081 \u2190\u2228\u2192 s) ltr\ntranLFCRem (s \u2190\u2227) (\u2227c ltr) = tranLFCRem (\u2227\u2192 s) ltr\ntranLFCRem (\u2227\u2192 s) (\u2227c ltr) = tranLFCRem (s \u2190\u2227) ltr\ntranLFCRem (s \u2190\u2227\u2192 s\u2081) (\u2227c ltr) = tranLFCRem (s\u2081 \u2190\u2227\u2192 s) ltr\ntranLFCRem ((s \u2190\u2227) \u2190\u2227) (\u2227\u2227d ltr) = tranLFCRem (s \u2190\u2227) ltr\ntranLFCRem ((\u2227\u2192 s) \u2190\u2227) (\u2227\u2227d ltr) = tranLFCRem (\u2227\u2192 (s \u2190\u2227)) ltr\ntranLFCRem ((s \u2190\u2227\u2192 s\u2081) \u2190\u2227) (\u2227\u2227d ltr) = tranLFCRem (s \u2190\u2227\u2192 (s\u2081 \u2190\u2227)) ltr\ntranLFCRem (\u2227\u2192 s) (\u2227\u2227d ltr) = tranLFCRem (\u2227\u2192 (\u2227\u2192 s)) ltr\ntranLFCRem ((s \u2190\u2227) \u2190\u2227\u2192 s\u2081) (\u2227\u2227d ltr) = tranLFCRem (s \u2190\u2227\u2192 (\u2227\u2192 s\u2081)) ltr\ntranLFCRem ((\u2227\u2192 s) \u2190\u2227\u2192 s\u2081) (\u2227\u2227d ltr) = tranLFCRem (\u2227\u2192 (s \u2190\u2227\u2192 s\u2081)) ltr\ntranLFCRem ((s \u2190\u2227\u2192 s\u2081) \u2190\u2227\u2192 s\u2082) (\u2227\u2227d ltr) = tranLFCRem (s \u2190\u2227\u2192 (s\u2081 \u2190\u2227\u2192 s\u2082)) ltr\ntranLFCRem (s \u2190\u2227) (\u00ac\u2227\u2227d ltr) = tranLFCRem ((s \u2190\u2227) \u2190\u2227) ltr\ntranLFCRem (\u2227\u2192 (s \u2190\u2227)) (\u00ac\u2227\u2227d ltr) = tranLFCRem ((\u2227\u2192 s) \u2190\u2227) ltr\ntranLFCRem (\u2227\u2192 (\u2227\u2192 s)) (\u00ac\u2227\u2227d ltr) = tranLFCRem (\u2227\u2192 s) ltr\ntranLFCRem (\u2227\u2192 (s \u2190\u2227\u2192 s\u2081)) (\u00ac\u2227\u2227d ltr) = tranLFCRem ((\u2227\u2192 s) \u2190\u2227\u2192 s\u2081) ltr\ntranLFCRem (s \u2190\u2227\u2192 (s\u2081 \u2190\u2227)) (\u00ac\u2227\u2227d ltr) = tranLFCRem ((s \u2190\u2227\u2192 s\u2081) \u2190\u2227) ltr\ntranLFCRem (s \u2190\u2227\u2192 (\u2227\u2192 s\u2081)) (\u00ac\u2227\u2227d ltr) = tranLFCRem ((s \u2190\u2227) \u2190\u2227\u2192 s\u2081) ltr\ntranLFCRem (s \u2190\u2227\u2192 (s\u2081 \u2190\u2227\u2192 s\u2082)) (\u00ac\u2227\u2227d ltr) = tranLFCRem ((s \u2190\u2227\u2192 s\u2081) \u2190\u2227\u2192 s\u2082) ltr\ntranLFCRem ((s \u2190\u2228) \u2190\u2228) (\u2228\u2228d ltr) = tranLFCRem (s \u2190\u2228) ltr\ntranLFCRem ((\u2228\u2192 s) \u2190\u2228) (\u2228\u2228d ltr) = tranLFCRem (\u2228\u2192 (s \u2190\u2228)) ltr\ntranLFCRem ((s \u2190\u2228\u2192 s\u2081) \u2190\u2228) (\u2228\u2228d ltr) = tranLFCRem (s \u2190\u2228\u2192 (s\u2081 \u2190\u2228)) ltr\ntranLFCRem (\u2228\u2192 s) (\u2228\u2228d ltr) = tranLFCRem (\u2228\u2192 (\u2228\u2192 s)) ltr\ntranLFCRem ((s \u2190\u2228) \u2190\u2228\u2192 s\u2081) (\u2228\u2228d ltr) = tranLFCRem (s \u2190\u2228\u2192 (\u2228\u2192 s\u2081)) ltr\ntranLFCRem ((\u2228\u2192 s) \u2190\u2228\u2192 s\u2081) (\u2228\u2228d ltr) = tranLFCRem (\u2228\u2192 (s \u2190\u2228\u2192 s\u2081)) ltr\ntranLFCRem ((s \u2190\u2228\u2192 s\u2081) \u2190\u2228\u2192 s\u2082) (\u2228\u2228d ltr) = tranLFCRem (s \u2190\u2228\u2192 (s\u2081 \u2190\u2228\u2192 s\u2082)) ltr\ntranLFCRem (s \u2190\u2228) (\u00ac\u2228\u2228d ltr) = tranLFCRem ((s \u2190\u2228) \u2190\u2228) ltr\ntranLFCRem (\u2228\u2192 (s \u2190\u2228)) (\u00ac\u2228\u2228d ltr) = tranLFCRem ((\u2228\u2192 s) \u2190\u2228) ltr\ntranLFCRem (\u2228\u2192 (\u2228\u2192 s)) (\u00ac\u2228\u2228d ltr) = tranLFCRem (\u2228\u2192 s) ltr\ntranLFCRem (\u2228\u2192 (s \u2190\u2228\u2192 s\u2081)) (\u00ac\u2228\u2228d ltr) = tranLFCRem ((\u2228\u2192 s) \u2190\u2228\u2192 s\u2081) ltr\ntranLFCRem (s \u2190\u2228\u2192 (s\u2081 \u2190\u2228)) (\u00ac\u2228\u2228d ltr) = tranLFCRem ((s \u2190\u2228\u2192 s\u2081) \u2190\u2228) ltr\ntranLFCRem (s \u2190\u2228\u2192 (\u2228\u2192 s\u2081)) (\u00ac\u2228\u2228d ltr) = tranLFCRem ((s \u2190\u2228) \u2190\u2228\u2192 s\u2081) ltr\ntranLFCRem (s \u2190\u2228\u2192 (s\u2081 \u2190\u2228\u2192 s\u2082)) (\u00ac\u2228\u2228d ltr) = tranLFCRem ((s \u2190\u2228\u2192 s\u2081) \u2190\u2228\u2192 s\u2082) ltr\ntranLFCRem ((s \u2190\u2202) \u2190\u2202) (\u2202\u2202d ltr) = tranLFCRem (s \u2190\u2202) ltr\ntranLFCRem ((\u2202\u2192 s) \u2190\u2202) (\u2202\u2202d ltr) = tranLFCRem (\u2202\u2192 (s \u2190\u2202)) ltr\ntranLFCRem ((s \u2190\u2202\u2192 s\u2081) \u2190\u2202) (\u2202\u2202d ltr) = tranLFCRem (s \u2190\u2202\u2192 (s\u2081 \u2190\u2202)) ltr\ntranLFCRem (\u2202\u2192 s) (\u2202\u2202d ltr) = tranLFCRem (\u2202\u2192 (\u2202\u2192 s)) ltr\ntranLFCRem ((s \u2190\u2202) \u2190\u2202\u2192 s\u2081) (\u2202\u2202d ltr) = tranLFCRem (s \u2190\u2202\u2192 (\u2202\u2192 s\u2081)) ltr\ntranLFCRem ((\u2202\u2192 s) \u2190\u2202\u2192 s\u2081) (\u2202\u2202d ltr) = tranLFCRem (\u2202\u2192 (s \u2190\u2202\u2192 s\u2081)) ltr\ntranLFCRem ((s \u2190\u2202\u2192 s\u2081) \u2190\u2202\u2192 s\u2082) (\u2202\u2202d ltr) = tranLFCRem (s \u2190\u2202\u2192 (s\u2081 \u2190\u2202\u2192 s\u2082)) ltr\ntranLFCRem (s \u2190\u2202) (\u00ac\u2202\u2202d ltr) = tranLFCRem ((s \u2190\u2202) \u2190\u2202) ltr\ntranLFCRem (\u2202\u2192 (s \u2190\u2202)) (\u00ac\u2202\u2202d ltr) = tranLFCRem ((\u2202\u2192 s) \u2190\u2202) ltr\ntranLFCRem (\u2202\u2192 (\u2202\u2192 s)) (\u00ac\u2202\u2202d ltr) = tranLFCRem (\u2202\u2192 s) ltr\ntranLFCRem (\u2202\u2192 (s \u2190\u2202\u2192 s\u2081)) (\u00ac\u2202\u2202d ltr) = tranLFCRem ((\u2202\u2192 s) \u2190\u2202\u2192 s\u2081) ltr\ntranLFCRem (s \u2190\u2202\u2192 (s\u2081 \u2190\u2202)) (\u00ac\u2202\u2202d ltr) = tranLFCRem ((s \u2190\u2202\u2192 s\u2081) \u2190\u2202) ltr\ntranLFCRem (s \u2190\u2202\u2192 (\u2202\u2192 s\u2081)) (\u00ac\u2202\u2202d ltr) = tranLFCRem ((s \u2190\u2202) \u2190\u2202\u2192 s\u2081) ltr\ntranLFCRem (s \u2190\u2202\u2192 (s\u2081 \u2190\u2202\u2192 s\u2082)) (\u00ac\u2202\u2202d ltr) = tranLFCRem ((s \u2190\u2202\u2192 s\u2081) \u2190\u2202\u2192 s\u2082) ltr\n\n\nextendLFCRem : \u2200{i u oll orll ll pll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 IndexLL {i} {u} pll ll \u2192 SetLFCRem {i} olf pll \u2192 SetLFCRem olf ll\nextendLFCRem \u2193 sr = sr\nextendLFCRem (ind \u2190\u2227) sr = (extendLFCRem ind sr) \u2190\u2227\nextendLFCRem (\u2227\u2192 ind) sr = \u2227\u2192 (extendLFCRem ind sr)\nextendLFCRem (ind \u2190\u2228) sr = (extendLFCRem ind sr) \u2190\u2228\nextendLFCRem (\u2228\u2192 ind) sr = \u2228\u2192 (extendLFCRem ind sr)\nextendLFCRem (ind \u2190\u2202) sr = (extendLFCRem ind sr) \u2190\u2202\nextendLFCRem (\u2202\u2192 ind) sr = \u2202\u2192 (extendLFCRem ind sr)\n\nreplaceLFCRem : \u2200{i u oll orll ll pll rll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 (ind : IndexLL {i} {u} pll ll) \u2192 SetLFCRem {i} olf rll \u2192 SetLFCRem olf ll \u2192 SetLFCRem olf (replLL ll ind rll)\nreplaceLFCRem \u2193 esr sr = esr\nreplaceLFCRem {rll = rll} (ind \u2190\u2227) esr (sr \u2190\u2227) = replaceLFCRem ind esr sr \u2190\u2227\nreplaceLFCRem {rll = rll} (ind \u2190\u2227) esr (\u2227\u2192 sr) = (extendLFCRem (updateIndex rll ind) esr) \u2190\u2227\u2192 sr\nreplaceLFCRem {rll = rll} (ind \u2190\u2227) esr (sr \u2190\u2227\u2192 sr\u2081) = (replaceLFCRem ind esr sr) \u2190\u2227\u2192 sr\u2081\nreplaceLFCRem {rll = rll} (\u2227\u2192 ind) esr (sr \u2190\u2227) = sr \u2190\u2227\u2192 (extendLFCRem (updateIndex rll ind) esr)\nreplaceLFCRem {rll = rll} (\u2227\u2192 ind) esr (\u2227\u2192 sr) = \u2227\u2192 replaceLFCRem ind esr sr\nreplaceLFCRem {rll = rll} (\u2227\u2192 ind) esr (sr \u2190\u2227\u2192 sr\u2081) = sr \u2190\u2227\u2192 replaceLFCRem ind esr sr\u2081\nreplaceLFCRem {rll = rll} (ind \u2190\u2228) esr (sr \u2190\u2228) = replaceLFCRem ind esr sr \u2190\u2228\nreplaceLFCRem {rll = rll} (ind \u2190\u2228) esr (\u2228\u2192 sr) = (extendLFCRem (updateIndex rll ind) esr) \u2190\u2228\u2192 sr\nreplaceLFCRem {rll = rll} (ind \u2190\u2228) esr (sr \u2190\u2228\u2192 sr\u2081) = (replaceLFCRem ind esr sr) \u2190\u2228\u2192 sr\u2081\nreplaceLFCRem {rll = rll} (\u2228\u2192 ind) esr (sr \u2190\u2228) = sr \u2190\u2228\u2192 (extendLFCRem (updateIndex rll ind) esr)\nreplaceLFCRem {rll = rll} (\u2228\u2192 ind) esr (\u2228\u2192 sr) = \u2228\u2192 replaceLFCRem ind esr sr\nreplaceLFCRem {rll = rll} (\u2228\u2192 ind) esr (sr \u2190\u2228\u2192 sr\u2081) = sr \u2190\u2228\u2192 replaceLFCRem ind esr sr\u2081\nreplaceLFCRem {rll = rll} (ind \u2190\u2202) esr (sr \u2190\u2202) = replaceLFCRem ind esr sr \u2190\u2202\nreplaceLFCRem {rll = rll} (ind \u2190\u2202) esr (\u2202\u2192 sr) = (extendLFCRem (updateIndex rll ind) esr) \u2190\u2202\u2192 sr\nreplaceLFCRem {rll = rll} (ind \u2190\u2202) esr (sr \u2190\u2202\u2192 sr\u2081) = (replaceLFCRem ind esr sr) \u2190\u2202\u2192 sr\u2081\nreplaceLFCRem {rll = rll} (\u2202\u2192 ind) esr (sr \u2190\u2202) = sr \u2190\u2202\u2192 (extendLFCRem (updateIndex rll ind) esr)\nreplaceLFCRem {rll = rll} (\u2202\u2192 ind) esr (\u2202\u2192 sr) = \u2202\u2192 replaceLFCRem ind esr sr\nreplaceLFCRem {rll = rll} (\u2202\u2192 ind) esr (sr \u2190\u2202\u2192 sr\u2081) = sr \u2190\u2202\u2192 replaceLFCRem ind esr sr\u2081\n\n\nmreplaceLFCRem : \u2200{i u oll orll ll pll rll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 (ind : IndexLL {i} {u} pll ll) \u2192 MSetLFCRem {i} olf rll \u2192 MSetLFCRem olf ll \u2192 MSetLFCRem olf (replLL ll ind rll)\nmreplaceLFCRem ind \u2205 \u2205 = \u2205\nmreplaceLFCRem {rll = rll} ind \u2205 (\u00ac\u2205 x) = delLFCRem x ind rll\nmreplaceLFCRem {rll = rll} ind (\u00ac\u2205 x) \u2205 = \u00ac\u2205 (extendLFCRem (updateIndex rll ind) x)\nmreplaceLFCRem ind (\u00ac\u2205 x) (\u00ac\u2205 x\u2081) = \u00ac\u2205 (replaceLFCRem ind x x\u2081)\n\nfindCallGraph : \u2200{i u oll orll ll rll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 (lf : LFun {i} {u} ll rll) \u2192 (IndexLFC lf \u2192 IndexLFC olf) \u2192 MSetLFCRem olf ll \u2192 MSetLFC olf olf \u2192 MSetLFCRem olf rll \u00d7 MSetLFC olf olf\nfindCallGraph I if msr ms = msr , ms\nfindCallGraph (_\u2282_ {ind = ind} lf lf\u2081) if msr ms = let emsr , ems = findCallGraph lf (\u03bb x \u2192 if (x \u2190\u2282)) (truncSetLFCRem msr ind) ms\n in findCallGraph lf\u2081 (\u03bb x \u2192 if (\u2282\u2192 x)) (mreplaceLFCRem ind emsr msr) ems \nfindCallGraph (tr ltr lf) if \u2205 ms = \u2205 , ms\nfindCallGraph (tr ltr lf) if (\u00ac\u2205 x) ms = findCallGraph lf (\u03bb x \u2192 if (tr x)) (\u00ac\u2205 $ tranLFCRem x ltr) ms\nfindCallGraph (com df lf) if \u2205 ms = findCallGraph lf (\u03bb x \u2192 if (com x)) \u2205 ms\nfindCallGraph (com df lf) if (\u00ac\u2205 x) ms = IMPOSSIBLE\nfindCallGraph {ll = ll} {rll = rll} {olf = olf} (call x) if msr ms = {!!} -- where\n\n--findCallGraph {ll = ll} {rll = call .\u221erll} {olf = olf} (call {\u221erll = \u221erll} x) if msr ms = {!!} -- where\n\n-- hf : IndexLFC olf \u2192 MSetLFCRem olf ll \u2192 MSetLFC olf olf \u2192 MSetLFCRem olf rll \u00d7 MSetLFC olf olf\n-- hf oic msr ms = {!!}\n","old_contents":"\nmodule IndexLF where\n\nopen import Common\nopen import LinLogic\n--open import LinDepT\n--open import LinT \n--open import SetLL\n--open import SetLLProp\n--open import SetLLRem\nopen import LinFun\n\nopen import Data.Product\n\n\ndata IndexLFC {i u} : \u2200{ll rll} \u2192 LFun {i} {u} ll rll \u2192 Set where\n \u2193 : \u2200{ll rll} \u2192 {lf : LFun {i} {u} ll rll} \u2192 IndexLFC lf\n _\u2190\u2282_ : \u2200{rll pll ell ll ind elf lf}\n \u2192 IndexLFC elf\n \u2192 IndexLFC (_\u2282_ {i} {u} {pll} {ll} {ell} {rll} {ind} elf lf)\n _\u2282\u2192_ : \u2200{rll pll ell ll ind elf lf}\n \u2192 IndexLFC lf\n \u2192 IndexLFC (_\u2282_ {i} {u} {pll} {ll} {ell} {rll} {ind} elf lf)\n tr : \u2200{ll orll rll} \u2192 {ltr : LLTr orll ll} \u2192 {lf : LFun {i} {u} orll rll}\n \u2192 IndexLFC lf \u2192 IndexLFC (tr ltr lf) \n com : \u2200{rll ll frll prfi prfo df lf}\n \u2192 IndexLFC lf\n \u2192 IndexLFC (com {i} {u} {rll} {ll} {frll} {{prfi}} {{prfo}} df lf)\n\n\n\ndata SetLFC {i u oll orll} (olf : LFun {i} {u} oll orll) : \u2200{ll rll} \u2192 LFun {i} {u} ll rll \u2192 Set (lsuc u) where\n \u2193 : \u2200{ll rll} \u2192 {lf : LFun {i} {u} ll rll} \u2192 IndexLFC olf \u2192 SetLFC olf lf\n _\u2190\u2282_ : \u2200{rll pll ell ll ind elf lf}\n \u2192 SetLFC olf elf\n \u2192 SetLFC olf (_\u2282_ {i} {u} {pll} {ll} {ell} {rll} {ind} elf lf)\n _\u2282\u2192_ : \u2200{rll pll ell ll ind elf lf}\n \u2192 SetLFC olf lf\n \u2192 SetLFC olf (_\u2282_ {i} {u} {pll} {ll} {ell} {rll} {ind} elf lf)\n _\u2190\u2282\u2192_ : \u2200{rll pll ell ll ind elf lf}\n \u2192 SetLFC olf elf\n \u2192 SetLFC olf lf\n \u2192 SetLFC olf (_\u2282_ {i} {u} {pll} {ll} {ell} {rll} {ind} elf lf)\n tr : \u2200{ll orll rll} \u2192 {ltr : LLTr orll ll} \u2192 {lf : LFun {i} {u} orll rll}\n \u2192 SetLFC olf lf\n \u2192 SetLFC olf (tr ltr lf) \n com : \u2200{rll ll frll prfi prfo df lf}\n \u2192 SetLFC olf lf\n \u2192 SetLFC olf (com {i} {u} {rll} {ll} {frll} {{prfi}} {{prfo}} df lf)\n\ndata MSetLFC {i u oll orll} (olf : LFun {i} {u} oll orll) : \u2200{ll rll} \u2192 LFun {i} {u} ll rll \u2192 Set (lsuc u) where\n \u2205 : \u2200{ll rll} \u2192 {lf : LFun {i} {u} ll rll} \u2192 MSetLFC olf lf\n \u00ac\u2205 : \u2200{ll rll} \u2192 {lf : LFun {i} {u} ll rll} \u2192 SetLFC olf lf \u2192 MSetLFC olf lf\n\n\ndata SetLFCRem {i u oll orll} (olf : LFun {i} {u} oll orll) : LinLogic i {u} \u2192 Set (lsuc u) where\n \u2193c : \u2200{\u221ell} \u2192 IndexLFC {i} olf \u2192 SetLFCRem olf (call \u221ell)\n _\u2190\u2227 : \u2200{rs ls} \u2192 SetLFCRem olf ls \u2192 SetLFCRem olf (ls \u2227 rs)\n \u2227\u2192_ : \u2200{rs ls} \u2192 SetLFCRem olf rs \u2192 SetLFCRem olf (ls \u2227 rs)\n _\u2190\u2227\u2192_ : \u2200{rs ls} \u2192 SetLFCRem olf ls \u2192 SetLFCRem olf rs \u2192 SetLFCRem olf (ls \u2227 rs)\n _\u2190\u2228 : \u2200{rs ls} \u2192 SetLFCRem olf ls \u2192 SetLFCRem olf (ls \u2228 rs)\n \u2228\u2192_ : \u2200{rs ls} \u2192 SetLFCRem olf rs \u2192 SetLFCRem olf (ls \u2228 rs)\n _\u2190\u2228\u2192_ : \u2200{rs ls} \u2192 SetLFCRem olf ls \u2192 SetLFCRem olf rs \u2192 SetLFCRem olf (ls \u2228 rs)\n _\u2190\u2202 : \u2200{rs ls} \u2192 SetLFCRem olf ls \u2192 SetLFCRem olf (ls \u2202 rs)\n \u2202\u2192_ : \u2200{rs ls} \u2192 SetLFCRem olf rs \u2192 SetLFCRem olf (ls \u2202 rs)\n _\u2190\u2202\u2192_ : \u2200{rs ls} \u2192 SetLFCRem olf ls \u2192 SetLFCRem olf rs \u2192 SetLFCRem olf (ls \u2202 rs)\n\ndata MSetLFCRem {i u oll orll} (olf : LFun {i} {u} oll orll) : LinLogic i {u} \u2192 Set (lsuc u) where\n \u2205 : \u2200{ll} \u2192 MSetLFCRem olf ll\n \u00ac\u2205 : \u2200{ll} \u2192 SetLFCRem olf ll \u2192 MSetLFCRem olf ll\n\n\ntruncSetLFCRem : \u2200{i} \u2192 \u2200{u ll oll orll q} \u2192 {olf : LFun {i} {u} oll orll} \u2192 MSetLFCRem {i} {u} olf ll \u2192 (ind : IndexLL {i} {u} q ll) \u2192 MSetLFCRem {i} olf q\ntruncSetLFCRem \u2205 ind = \u2205\ntruncSetLFCRem (\u00ac\u2205 x) \u2193 = \u00ac\u2205 x\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2227)) (ind \u2190\u2227) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (\u2227\u2192 x)) (ind \u2190\u2227) = \u2205\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2227\u2192 x\u2081)) (ind \u2190\u2227) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2227)) (\u2227\u2192 ind) = \u2205\ntruncSetLFCRem (\u00ac\u2205 (\u2227\u2192 x)) (\u2227\u2192 ind) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2227\u2192 x\u2081)) (\u2227\u2192 ind) = truncSetLFCRem (\u00ac\u2205 x\u2081) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2228)) (ind \u2190\u2228) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (\u2228\u2192 x)) (ind \u2190\u2228) = \u2205\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2228\u2192 x\u2081)) (ind \u2190\u2228) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2228)) (\u2228\u2192 ind) = \u2205\ntruncSetLFCRem (\u00ac\u2205 (\u2228\u2192 x)) (\u2228\u2192 ind) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2228\u2192 x\u2081)) (\u2228\u2192 ind) = truncSetLFCRem (\u00ac\u2205 x\u2081) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2202)) (ind \u2190\u2202) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (\u2202\u2192 x)) (ind \u2190\u2202) = \u2205\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2202\u2192 x\u2081)) (ind \u2190\u2202) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2202)) (\u2202\u2192 ind) = \u2205\ntruncSetLFCRem (\u00ac\u2205 (\u2202\u2192 x)) (\u2202\u2192 ind) = truncSetLFCRem (\u00ac\u2205 x) ind\ntruncSetLFCRem (\u00ac\u2205 (x \u2190\u2202\u2192 x\u2081)) (\u2202\u2192 ind) = truncSetLFCRem (\u00ac\u2205 x\u2081) ind\n\ndelLFCRem : \u2200{i u oll orll ll pll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 SetLFCRem {i} olf ll \u2192 (ind : IndexLL {i} {u} pll ll) \u2192 (rll : LinLogic i)\n \u2192 MSetLFCRem {i} olf (replLL ll ind rll)\ndelLFCRem s \u2193 rll = \u2205\ndelLFCRem (s \u2190\u2227) (ind \u2190\u2227) rll with (delLFCRem s ind rll)\ndelLFCRem (s \u2190\u2227) (ind \u2190\u2227) rll | \u2205 = \u2205\ndelLFCRem (s \u2190\u2227) (ind \u2190\u2227) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2227)\ndelLFCRem (\u2227\u2192 s) (ind \u2190\u2227) rll = \u00ac\u2205 (\u2227\u2192 (s))\ndelLFCRem (s \u2190\u2227\u2192 s\u2081) (ind \u2190\u2227) rll with (delLFCRem s ind rll)\ndelLFCRem (s \u2190\u2227\u2192 s\u2081) (ind \u2190\u2227) rll | \u2205 = \u00ac\u2205 (\u2227\u2192 (s\u2081))\ndelLFCRem (s \u2190\u2227\u2192 s\u2081) (ind \u2190\u2227) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2227\u2192 (s\u2081))\ndelLFCRem (s \u2190\u2227) (\u2227\u2192 ind) rll = \u00ac\u2205 ((s) \u2190\u2227)\ndelLFCRem (\u2227\u2192 s) (\u2227\u2192 ind) rll with (delLFCRem s ind rll)\ndelLFCRem (\u2227\u2192 s) (\u2227\u2192 ind) rll | \u2205 = \u2205\ndelLFCRem (\u2227\u2192 s) (\u2227\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 (\u2227\u2192 x)\ndelLFCRem (s \u2190\u2227\u2192 s\u2081) (\u2227\u2192 ind) rll with (delLFCRem s\u2081 ind rll)\ndelLFCRem (s \u2190\u2227\u2192 s\u2081) (\u2227\u2192 ind) rll | \u2205 = \u00ac\u2205 ((s) \u2190\u2227)\ndelLFCRem (s \u2190\u2227\u2192 s\u2081) (\u2227\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 ((s) \u2190\u2227\u2192 x)\ndelLFCRem (s \u2190\u2228) (ind \u2190\u2228) rll with (delLFCRem s ind rll)\ndelLFCRem (s \u2190\u2228) (ind \u2190\u2228) rll | \u2205 = \u2205\ndelLFCRem (s \u2190\u2228) (ind \u2190\u2228) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2228)\ndelLFCRem (\u2228\u2192 s) (ind \u2190\u2228) rll = \u00ac\u2205 (\u2228\u2192 (s))\ndelLFCRem (s \u2190\u2228\u2192 s\u2081) (ind \u2190\u2228) rll with (delLFCRem s ind rll)\ndelLFCRem (s \u2190\u2228\u2192 s\u2081) (ind \u2190\u2228) rll | \u2205 = \u00ac\u2205 (\u2228\u2192 (s\u2081))\ndelLFCRem (s \u2190\u2228\u2192 s\u2081) (ind \u2190\u2228) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2228\u2192 (s\u2081))\ndelLFCRem (s \u2190\u2228) (\u2228\u2192 ind) rll = \u00ac\u2205 ((s) \u2190\u2228)\ndelLFCRem (\u2228\u2192 s) (\u2228\u2192 ind) rll with (delLFCRem s ind rll)\ndelLFCRem (\u2228\u2192 s) (\u2228\u2192 ind) rll | \u2205 = \u2205\ndelLFCRem (\u2228\u2192 s) (\u2228\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 (\u2228\u2192 x)\ndelLFCRem (s \u2190\u2228\u2192 s\u2081) (\u2228\u2192 ind) rll with (delLFCRem s\u2081 ind rll)\ndelLFCRem (s \u2190\u2228\u2192 s\u2081) (\u2228\u2192 ind) rll | \u2205 = \u00ac\u2205 ((s) \u2190\u2228)\ndelLFCRem (s \u2190\u2228\u2192 s\u2081) (\u2228\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 ((s) \u2190\u2228\u2192 x)\ndelLFCRem (s \u2190\u2202) (ind \u2190\u2202) rll with (delLFCRem s ind rll)\ndelLFCRem (s \u2190\u2202) (ind \u2190\u2202) rll | \u2205 = \u2205\ndelLFCRem (s \u2190\u2202) (ind \u2190\u2202) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2202)\ndelLFCRem (\u2202\u2192 s) (ind \u2190\u2202) rll = \u00ac\u2205 (\u2202\u2192 (s))\ndelLFCRem (s \u2190\u2202\u2192 s\u2081) (ind \u2190\u2202) rll with (delLFCRem s ind rll)\ndelLFCRem (s \u2190\u2202\u2192 s\u2081) (ind \u2190\u2202) rll | \u2205 = \u00ac\u2205 (\u2202\u2192 (s\u2081))\ndelLFCRem (s \u2190\u2202\u2192 s\u2081) (ind \u2190\u2202) rll | \u00ac\u2205 x = \u00ac\u2205 (x \u2190\u2202\u2192 (s\u2081))\ndelLFCRem (s \u2190\u2202) (\u2202\u2192 ind) rll = \u00ac\u2205 ((s) \u2190\u2202)\ndelLFCRem (\u2202\u2192 s) (\u2202\u2192 ind) rll with (delLFCRem s ind rll)\ndelLFCRem (\u2202\u2192 s) (\u2202\u2192 ind) rll | \u2205 = \u2205\ndelLFCRem (\u2202\u2192 s) (\u2202\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 (\u2202\u2192 x)\ndelLFCRem (s \u2190\u2202\u2192 s\u2081) (\u2202\u2192 ind) rll with (delLFCRem s\u2081 ind rll)\ndelLFCRem (s \u2190\u2202\u2192 s\u2081) (\u2202\u2192 ind) rll | \u2205 = \u00ac\u2205 ((s) \u2190\u2202)\ndelLFCRem (s \u2190\u2202\u2192 s\u2081) (\u2202\u2192 ind) rll | \u00ac\u2205 x = \u00ac\u2205 ((s) \u2190\u2202\u2192 x)\n\nmdelLFCRem : \u2200{i u oll orll ll pll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 MSetLFCRem {i} olf ll \u2192 (ind : IndexLL {i} {u} pll ll) \u2192 (rll : LinLogic i)\n \u2192 MSetLFCRem {i} olf (replLL ll ind rll)\nmdelLFCRem \u2205 ind rll = \u2205\nmdelLFCRem (\u00ac\u2205 x) ind rll = delLFCRem x ind rll\n\n\n\nmreplaceLFCRem : \u2200{i u oll orll ll pll rll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 (ind : IndexLL {i} {u} pll ll) \u2192 MSetLFCRem {i} olf rll \u2192 MSetLFCRem olf ll \u2192 MSetLFCRem olf (replLL ll ind rll)\nmreplaceLFCRem ind \u2205 \u2205 = \u2205\nmreplaceLFCRem ind \u2205 (\u00ac\u2205 x) = {!!}\nmreplaceLFCRem ind (\u00ac\u2205 x) ms = {!!}\n\n\nfindCallGraph : \u2200{i u oll orll ll rll} \u2192 {olf : LFun {i} {u} oll orll} \u2192 (lf : LFun {i} {u} ll rll) \u2192 MSetLFCRem olf ll \u2192 MSetLFC olf olf \u2192 MSetLFCRem olf rll \u00d7 MSetLFC olf olf\nfindCallGraph I msr ms = msr , ms\nfindCallGraph (_\u2282_ {ind = ind} lf lf\u2081) msr ms = let emsr , ems = findCallGraph lf (truncSetLFCRem msr ind) ms\n in {!!} \nfindCallGraph (tr ltr lf) msr ms = {!!}\nfindCallGraph (com df lf) msr ms = {!!}\nfindCallGraph (call x) msr ms = {!!}\n","returncode":0,"stderr":"","license":"mpl-2.0","lang":"Agda"} {"commit":"fce4641e9e8aec601f2ab48e128ad529e5ac3a98","subject":"Clarify the problem even more","message":"Clarify the problem even more\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/SIRelBigStep\/DeriveCorrect.agda","new_file":"Thesis\/SIRelBigStep\/DeriveCorrect.agda","new_contents":"--\n-- In this module we conclude our proof: we have shown that t, derive-dterm t\n-- and produce related values (in the sense of the fundamental property,\n-- rfundamental3), and for related values v1, dv and v2 we have v1 \u2295 dv \u2261\n-- v2 (because \u2295 agrees with validity, rrelV3\u2192\u2295).\n--\n-- We now put these facts together via derive-correct-si and derive-correct.\n-- This is immediate, even though I spend so much code on it:\n-- Indeed, all theorems are immediate corollaries of what we established (as\n-- explained above); only the statements are longer, especially because I bother\n-- expanding them.\n\nmodule Thesis.SIRelBigStep.DeriveCorrect where\n\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\n\nopen import Thesis.SIRelBigStep.IlcSILR\nopen import Thesis.SIRelBigStep.FundamentalProperty\n\n-- Theorem statement. This theorem still mentions step-indexes explicitly.\nderive-correct-si-type =\n \u2200 {\u03c4 \u0393 k} (t : Term \u0393 \u03c4) \u03c11 d\u03c1 \u03c12 \u2192 (\u03c1\u03c1 : rrel\u03c13 \u0393 \u03c11 d\u03c1 \u03c12 k) \u2192\n rrelT3-skeleton (\u03bb v1 dv v2 _ \u2192 v1 \u2295 dv \u2261 v2) t (derive-dterm t) t \u03c11 d\u03c1 \u03c12 k\n\n-- A verified expansion of the theorem statement.\nderive-correct-si-type-means :\n derive-correct-si-type \u2261\n \u2200 {\u03c4 \u0393 k} (t : Term \u0393 \u03c4)\n \u03c11 d\u03c1 \u03c12 (\u03c1\u03c1 : rrel\u03c13 \u0393 \u03c11 d\u03c1 \u03c12 k) \u2192\n (v1 v2 : Val \u03c4) \u2192\n \u2200 j (j e2 and de2 : e2 ->\n-- e3, we must show that if e1 and e3 evaluate something happens, but to use our\n-- hypothesis we need that e2 also terminates, which in our case does not\n-- follow. For Ahmed 2006, instead, e1 terminates and e1 \u2264 e2 implies that e2\n-- terminates.\n--\n-- Indeed, it seems that we can have a change from e1 to looping e2 and a change\n-- from looping e2 to e3, and I don't expect the composition of such changes to\n-- satisfy anything.\n--\n-- Indeed, \"Imperative self-adjusting computation\" does not mention any\n-- transitivity result (even in the technical report).\n","old_contents":"--\n-- In this module we conclude our proof: we have shown that t, derive-dterm t\n-- and produce related values (in the sense of the fundamental property,\n-- rfundamental3), and for related values v1, dv and v2 we have v1 \u2295 dv \u2261\n-- v2 (because \u2295 agrees with validity, rrelV3\u2192\u2295).\n--\n-- We now put these facts together via derive-correct-si and derive-correct.\n-- This is immediate, even though I spend so much code on it:\n-- Indeed, all theorems are immediate corollaries of what we established (as\n-- explained above); only the statements are longer, especially because I bother\n-- expanding them.\n\nmodule Thesis.SIRelBigStep.DeriveCorrect where\n\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\n\nopen import Thesis.SIRelBigStep.IlcSILR\nopen import Thesis.SIRelBigStep.FundamentalProperty\n\n-- Theorem statement. This theorem still mentions step-indexes explicitly.\nderive-correct-si-type =\n \u2200 {\u03c4 \u0393 k} (t : Term \u0393 \u03c4) \u03c11 d\u03c1 \u03c12 \u2192 (\u03c1\u03c1 : rrel\u03c13 \u0393 \u03c11 d\u03c1 \u03c12 k) \u2192\n rrelT3-skeleton (\u03bb v1 dv v2 _ \u2192 v1 \u2295 dv \u2261 v2) t (derive-dterm t) t \u03c11 d\u03c1 \u03c12 k\n\n-- A verified expansion of the theorem statement.\nderive-correct-si-type-means :\n derive-correct-si-type \u2261\n \u2200 {\u03c4 \u0393 k} (t : Term \u0393 \u03c4)\n \u03c11 d\u03c1 \u03c12 (\u03c1\u03c1 : rrel\u03c13 \u0393 \u03c11 d\u03c1 \u03c12 k) \u2192\n (v1 v2 : Val \u03c4) \u2192\n \u2200 j (j Set\nDom\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7 = Dom\u27e6 \u03c4\u2081 \u27e7 \u2192 Dom\u27e6 \u03c4\u2082 \u27e7\n\n-- TYPING CONTEXTS\n\n-- Syntax\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n-- Denotational Semantics\n\ndata Empty : Set where\n \u2205 : Empty\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\nEnv\u27e6_\u27e7 : Context \u2192 Set\nEnv\u27e6 \u2205 \u27e7 = Empty\nEnv\u27e6 \u03c4 \u2022 \u0393 \u27e7 = Bind Dom\u27e6 \u03c4 \u27e7 Env\u27e6 \u0393 \u27e7\n\n-- VARIABLES\n\n-- Syntax\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\n-- Denotational Semantics\n\nlookup\u27e6_\u27e7 : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Env\u27e6 \u0393 \u27e7 \u2192 Dom\u27e6 \u03c4 \u27e7\nlookup\u27e6 this \u27e7 (v \u2022 \u03c1) = v\nlookup\u27e6 that x \u27e7 (v \u2022 \u03c1) = lookup\u27e6 x \u27e7 \u03c1\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n-- Denotational Semantics\n\neval\u27e6_\u27e7 : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Env\u27e6 \u0393 \u27e7 \u2192 Dom\u27e6 \u03c4 \u27e7\neval\u27e6 abs t \u27e7 \u03c1 = \u03bb v \u2192 eval\u27e6 t \u27e7 (v \u2022 \u03c1)\neval\u27e6 app t\u2081 t\u2082 \u27e7 \u03c1 = (eval\u27e6 t\u2081 \u27e7 \u03c1) (eval\u27e6 t\u2082 \u27e7 \u03c1)\neval\u27e6 var x \u27e7 \u03c1 = lookup\u27e6 x \u27e7 \u03c1\n\n-- NATURAL SEMANTICS\n\n-- Syntax\n\ndata Env : Context \u2192 Set\ndata Val : Type \u2192 Set\n\ndata Val where\n \u27e8abs_,_\u27e9 : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) (\u03c1 : Env \u0393) \u2192 Val (\u03c4\u2081 \u21d2 \u03c4\u2082)\n\ndata Env where\n \u2205 : Env \u2205\n _\u2022_ : \u2200 {\u0393 \u03c4} \u2192 Val \u03c4 \u2192 Env \u0393 \u2192 Env (\u03c4 \u2022 \u0393)\n\n-- Lookup\n\ninfixr 8 _\u22a2_\u2193_ _\u22a2_\u21a6_\n\ndata _\u22a2_\u21a6_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Var \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n this : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {v : Val \u03c4} \u2192\n v \u2022 \u03c1 \u22a2 this \u21a6 v\n that : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 x} {\u03c1 : Env \u0393} {v\u2081 : Val \u03c4\u2081} {v\u2082 : Val \u03c4\u2082} \u2192\n \u03c1 \u22a2 x \u21a6 v\u2082 \u2192\n v\u2081 \u2022 \u03c1 \u22a2 that x \u21a6 v\u2082\n\n-- Reduction\n\ndata _\u22a2_\u2193_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Term \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c1} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n \u03c1 \u22a2 abs t \u2193 \u27e8abs t , \u03c1 \u27e9\n app : \u2200 {\u0393 \u0393\u2032 \u03c4\u2081 \u03c4\u2082 \u03c1 \u03c1\u2032 v\u2082 v\u2032} {t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2082 : Term \u0393 \u03c4\u2081} {t\u2032 : Term (\u03c4\u2081 \u2022 \u0393\u2032) \u03c4\u2082} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 \u27e8abs t\u2032 , \u03c1\u2032 \u27e9 \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n v\u2082 \u2022 \u03c1\u2032 \u22a2 t\u2032 \u2193 v\u2032 \u2192\n \u03c1 \u22a2 app t\u2081 t\u2082 \u2193 v\u2032\n var : \u2200 {\u0393 \u03c4 x} {\u03c1 : Env \u0393} {v : Val \u03c4}\u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u03c1 \u22a2 var x \u2193 v\n\n-- WEAKENING\n\n-- Extend a context to a super context\n\ninfixr 10 _\u22ce_\n\n_\u22ce_ : (\u0393\u2081 \u0393\u2081 : Context) \u2192 Context\n\u2205 \u22ce \u0393\u2082 = \u0393\u2082\n(\u03c4 \u2022 \u0393\u2081) \u22ce \u0393\u2082 = \u03c4 \u2022 \u0393\u2081 \u22ce \u0393\u2082\n\n-- Lift a variable to a super context\n\nlift : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nlift {\u2205} {\u2205} x = x\nlift {\u2205} {\u03c4 \u2022 \u0393\u2082} x = that (lift {\u2205} {\u0393\u2082} x)\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} this = this\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} (that x) = that (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- Weaken a term to a super context\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken {\u0393\u2081} {\u0393\u2082} (abs {\u03c4\u2081 = \u03c4} t) = abs (weaken {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} t)\nweaken {\u0393\u2081} {\u0393\u2082} (app t\u2081 t\u2082) = app (weaken {\u0393\u2081} {\u0393\u2082} t\u2081) (weaken {\u0393\u2081} {\u0393\u2082} t\u2082)\nweaken {\u0393\u2081} {\u0393\u2082} (var x) = var (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\napply : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbf. \u03bbx. apply ( df x ) ( f x )\n\ncompose : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4)\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbdg. \u03bbx. compose ( df x ) ( dg x )\n\nnil : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4)\nnil {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs nil\n -- \u03bbx. nil\n\n-- Hey, apply is \u03b1-equivalent to compose, what's going on?\n-- Oh, and `\u0394-Type` is the identity function?\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u03c4 \u2022 \u0394-Type \u03c4 \u2022 \u0394-Context \u0393\n\n-- CHANGE VARIABLES\n\n-- changes 'x' to 'dx'\n\nrename : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nrename this = that this\nrename (that x) = that (that (rename x))\n\n-- changes 'x' to 'nil' (if internally bound) or 'dx' (if externally bound)\n\n\u0394-var : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-var {\u2205} x = var (rename x)\n\u0394-var {\u03c4 \u2022 \u0393} this = nil\n\u0394-var {\u03c4 \u2022 \u0393} (that x) = \u0394-var {\u0393} x\n\n-- CHANGE TERMS\n\n\u0394-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-term {\u0393} (abs {\u03c4\u2081 = \u03c4} t) = abs (\u0394-term {\u03c4 \u2022 \u0393} t)\n\u0394-term {\u0393} (app t\u2081 t\u2082) = {!!}\n\u0394-term {\u0393} (var x) = weaken {\u2205} {\u0393} (\u0394-var {\u0393} x)\n","old_contents":"module incremental where\n\n-- SIMPLE TYPES\n\n-- Syntax\n\ndata Type : Set where\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\n-- Semantics\n\nDom\u27e6_\u27e7 : Type -> Set\nDom\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7 = Dom\u27e6 \u03c4\u2081 \u27e7 \u2192 Dom\u27e6 \u03c4\u2082 \u27e7\n\n-- TYPING CONTEXTS\n\n-- Syntax\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n-- Semantics\n\ndata Empty : Set where\n \u2205 : Empty\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\nEnv\u27e6_\u27e7 : Context \u2192 Set\nEnv\u27e6 \u2205 \u27e7 = Empty\nEnv\u27e6 \u03c4 \u2022 \u0393 \u27e7 = Bind Dom\u27e6 \u03c4 \u27e7 Env\u27e6 \u0393 \u27e7\n\n-- VARIABLES\n\n-- Syntax\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\n-- Semantics\n\nlookup\u27e6_\u27e7 : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Env\u27e6 \u0393 \u27e7 \u2192 Dom\u27e6 \u03c4 \u27e7\nlookup\u27e6 this \u27e7 (v \u2022 \u03c1) = v\nlookup\u27e6 that x \u27e7 (v \u2022 \u03c1) = lookup\u27e6 x \u27e7 \u03c1\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n-- Semantics\n\neval\u27e6_\u27e7 : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Env\u27e6 \u0393 \u27e7 \u2192 Dom\u27e6 \u03c4 \u27e7\neval\u27e6 abs t \u27e7 \u03c1 = \u03bb v \u2192 eval\u27e6 t \u27e7 (v \u2022 \u03c1)\neval\u27e6 app t\u2081 t\u2082 \u27e7 \u03c1 = (eval\u27e6 t\u2081 \u27e7 \u03c1) (eval\u27e6 t\u2082 \u27e7 \u03c1)\neval\u27e6 var x \u27e7 \u03c1 = lookup\u27e6 x \u27e7 \u03c1\n\n\n-- WEAKENING\n\n-- Extend a context to a super context\n\ninfixr 10 _\u22ce_\n\n_\u22ce_ : (\u0393\u2081 \u0393\u2081 : Context) \u2192 Context\n\u2205 \u22ce \u0393\u2082 = \u0393\u2082\n(\u03c4 \u2022 \u0393\u2081) \u22ce \u0393\u2082 = \u03c4 \u2022 \u0393\u2081 \u22ce \u0393\u2082\n\n-- Lift a variable to a super context\n\nlift : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nlift {\u2205} {\u2205} x = x\nlift {\u2205} {\u03c4 \u2022 \u0393\u2082} x = that (lift {\u2205} {\u0393\u2082} x)\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} this = this\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} (that x) = that (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- Weaken a term to a super context\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken {\u0393\u2081} {\u0393\u2082} (abs {\u03c4\u2081 = \u03c4} t) = abs (weaken {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} t)\nweaken {\u0393\u2081} {\u0393\u2082} (app t\u2081 t\u2082) = app (weaken {\u0393\u2081} {\u0393\u2082} t\u2081) (weaken {\u0393\u2081} {\u0393\u2082} t\u2082)\nweaken {\u0393\u2081} {\u0393\u2082} (var x) = var (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\napply : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbf. \u03bbx. apply ( df x ) ( f x )\n\ncompose : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4)\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbdg. \u03bbx. compose ( df x ) ( dg x )\n\nnil : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4)\nnil {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs nil\n -- \u03bbx. nil\n\n-- Hey, apply is \u03b1-equivalent to compose, what's going on?\n-- Oh, and `\u0394-Type` is the identity function?\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u03c4 \u2022 \u0394-Type \u03c4 \u2022 \u0394-Context \u0393\n\n-- CHANGE VARIABLES\n\n-- changes 'x' to 'dx'\n\nrename : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nrename this = that this\nrename (that x) = that (that (rename x))\n\n-- changes 'x' to 'nil' (if internally bound) or 'dx' (if externally bound)\n\n\u0394-var : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-var {\u2205} x = var (rename x)\n\u0394-var {\u03c4 \u2022 \u0393} this = nil\n\u0394-var {\u03c4 \u2022 \u0393} (that x) = \u0394-var {\u0393} x\n\n-- CHANGE TERMS\n\n\u0394-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-term {\u0393} (abs {\u03c4\u2081 = \u03c4} t) = abs (\u0394-term {\u03c4 \u2022 \u0393} t)\n\u0394-term {\u0393} (app t\u2081 t\u2082) = {!!}\n\u0394-term {\u0393} (var x) = weaken {\u2205} {\u0393} (\u0394-var {\u0393} x)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"64e621c3d2a135c31988f71656671c97b59d8eca","subject":"Add separations","message":"Add separations\n","repos":"louisswarren\/hieretikz","old_file":"arrow.agda","new_file":"arrow.agda","new_contents":"data Bool : Set where\n true : Bool\n false : Bool\n\n\n_or_ : Bool \u2192 Bool \u2192 Bool\ntrue or true = true\ntrue or false = true\nfalse or true = true\nfalse or false = false\n\n\n\n----------------------------------------\n\n\n\ndata \u2115 : Set where\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n{-# BUILTIN NATURAL \u2115 #-}\n\n\n_\u2261_ : \u2115 \u2192 \u2115 \u2192 Bool\nzero \u2261 zero = true\nsuc n \u2261 suc m = n \u2261 m\n_ \u2261 _ = false\n\n\n\n----------------------------------------\n\n\n\ndata List (A : Set) : Set where\n \u2218 : List A\n _\u2237_ : A \u2192 List A \u2192 List A\n\n\nany : {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 Bool\nany _ \u2218 = false\nany f (x \u2237 xs) = (f x) or (any f xs)\n\n\napply : {A B : Set} \u2192 (A \u2192 B) \u2192 List A \u2192 List B\napply _ \u2218 = \u2218\napply f (x \u2237 xs) = (f x) \u2237 (apply f xs)\n\n\n_\u2208_ : \u2115 \u2192 List \u2115 \u2192 Bool\nx \u2208 \u2218 = false\nx \u2208 (y \u2237 ys) with x \u2261 y\n... | true = true\n... | false = x \u2208 ys\n\n\n_\u220b_ : List \u2115 \u2192 \u2115 \u2192 Bool\nxs \u220b y = y \u2208 xs\n\n----------------------------------------\n\n\n\ndata Arrow : Set where\n \u21d2_ : \u2115 \u2192 Arrow\n _\u21d2_ : \u2115 \u2192 Arrow \u2192 Arrow\n\n\n\nclosure : List Arrow \u2192 List \u2115 \u2192 List \u2115\nclosure \u2218 found = found\nclosure ((\u21d2 n) \u2237 rest) found = n \u2237 (closure rest (n \u2237 found))\nclosure ((n \u21d2 q) \u2237 rest) found with (n \u2208 found) or (n \u2208 (closure rest found))\n... | true = closure (q \u2237 rest) found\n... | false = closure rest found\n\n\n\n_,_\u22a2_ : List Arrow \u2192 List \u2115 \u2192 \u2115 \u2192 Bool\ncs , ps \u22a2 q = q \u2208 (closure cs ps)\n\n\n\n----------------------------------------\n\n\n\ndata Separation : Set where\n model : List \u2115 \u2192 List \u2115 \u2192 Separation\n\n\n_,_\u22a8_ : Separation \u2192 List Arrow \u2192 \u2115 \u2192 Bool\n((model holds _) , cs \u22a8 n) = (cs , holds \u22a2 n)\n\n_,_\u00ac\u22a8_ : Separation \u2192 List Arrow \u2192 \u2115 \u2192 Bool\n((model _ fails) , cs \u00ac\u22a8 n) = any (_\u220b_ (closure cs (n \u2237 \u2218))) fails\n\n\n\n\n--_,_,_\u00ac\u22a8_ : List Separation \u2192 List Arrow \u2192 List \u2115 \u2192 \u2115 \u2192 Bool\n\n\n\n","old_contents":"data Bool : Set where\n true : Bool\n false : Bool\n\n\n_or_ : Bool \u2192 Bool \u2192 Bool\ntrue or true = true\ntrue or false = true\nfalse or true = true\nfalse or false = false\n\n\n\n----------------------------------------\n\n\n\ndata \u2115 : Set where\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n{-# BUILTIN NATURAL \u2115 #-}\n\n\n_\u2261_ : \u2115 \u2192 \u2115 \u2192 Bool\nzero \u2261 zero = true\nsuc n \u2261 suc m = n \u2261 m\n_ \u2261 _ = false\n\n\n\n----------------------------------------\n\n\n\ndata List (A : Set) : Set where\n \u2218 : List A\n _\u2237_ : A \u2192 List A \u2192 List A\n\n\n_\u2208_ : \u2115 \u2192 List \u2115 \u2192 Bool\nx \u2208 \u2218 = false\nx \u2208 (y \u2237 ys) with x \u2261 y\n... | true = true\n... | false = x \u2208 ys\n\n\n\n----------------------------------------\n\n\n\ndata Arrow : Set where\n \u21d2_ : \u2115 \u2192 Arrow\n _\u21d2_ : \u2115 \u2192 Arrow \u2192 Arrow\n\n\n\nclosure : List Arrow \u2192 List \u2115 \u2192 List \u2115\nclosure \u2218 found = found\nclosure ((\u21d2 n) \u2237 rest) found = n \u2237 (closure rest (n \u2237 found))\nclosure ((n \u21d2 q) \u2237 rest) found with (n \u2208 found) or (n \u2208 (closure rest found))\n... | true = closure (q \u2237 rest) found\n... | false = closure rest found\n\n\n_\u22a2_ : List Arrow \u2192 Arrow \u2192 Bool\ncs \u22a2 (\u21d2 q) = q \u2208 (closure cs \u2218)\ncs \u22a2 (p \u21d2 q) = ((\u21d2 p) \u2237 cs) \u22a2 q\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"dde3bebb67c586b0525f1ff8a908f73f37e56d97","subject":"Stratified Desc model: checkpoint Set0\/Set1","message":"Stratified Desc model: checkpoint Set0\/Set1","repos":"kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram,kwangkim\/pigment","old_file":"models\/Desc.agda","new_file":"models\/Desc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule Desc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\nunlift : {l : Level}{A : Set l} -> Lifted A -> A\nunlift (lifter a) = a\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\npair : {i j : Level}{A : Set i}{B : A -> Set j} -> \n (x : A) (y : B x) -> Sigma {i = i}{j = j} A B\npair x y = x , y\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l m : Level}{A : Set l}{B : Set m}\n (f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l m n : Level}{A : Set l}{B : Set m}{C : Set n}\n (f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\ntrans : {l : Level}{A : Set l}{x y z : A} -> x == y -> y == z -> x == z\ntrans refl refl = refl\n\nproof-lift-unlift-eq : {l : Level}{A : Set l}(x : Lifted A) -> lifter (unlift x) == x\nproof-lift-unlift-eq (lifter a) = refl\n\npostulate \n reflFun : {l m : Level}{A : Set l}{B : A -> Set m}(f : (a : A) -> B a)(g : (a : A) -> B a)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata Desc : Set1 where\n id : Desc\n const : Set -> Desc\n prod : Desc -> Desc -> Desc\n sigma : (S : Set) -> (S -> Desc) -> Desc\n pi : (S : Set) -> (S -> Desc) -> Desc\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|]_ : Desc -> Set -> Set\n[| id |] Z = Z\n[| const X |] Z = X\n[| prod D D' |] Z = [| D |] Z * [| D' |] Z\n[| sigma S T |] Z = Sigma S (\\s -> [| T s |] Z)\n[| pi S T |] Z = (s : S) -> [| T s |] Z\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata Mu (D : Desc) : Set where\n con : [| D |] (Mu D) -> Mu D\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {l : Level}(D : Desc)(X : Set)(P : X -> Set l) -> [| D |] X -> Set l\nAll id X P x = P x\nAll (const Z) X P x = Unit\nAll (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')\nAll (sigma S T) X P (a , b) = All (T a) X P b\nAll (pi S T) X P f = (s : S) -> All (T s) X P (f s)\n\n\nall : (D : Desc)(X : Set)(P : X -> Set)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x\nall id X P R x = R x\nall (const Z) X P R z = Void\nall (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'\nall (sigma S T) X P R (a , b) = all (T a) X P R b\nall (pi S T) X P R f = \\ s -> all (T s) X P R (f s)\n\n--********************************************\n-- Map\n--********************************************\n\nmap : (D : Desc)(X Y : Set)(f : X -> Y)(v : [| D |] X) -> [| D |] Y\nmap id X Y sig x = sig x\nmap (const Z) X Y sig z = z\nmap (prod D D') X Y sig (d , d') = map D X Y sig d , map D' X Y sig d'\nmap (sigma S T) X Y sig (a , b) = (a , map (T a) X Y sig b)\nmap (pi S T) X Y sig f = \\x -> map (T x) X Y sig (f x)\n\n\nproof-map-id : (D : Desc)(X : Set)(v : [| D |] X) -> map D X X (\\x -> x) v == v\nproof-map-id id X v = refl\nproof-map-id (const Z) X v = refl\nproof-map-id (prod D D') X (v , v') = cong2 (\\x y -> (x , y)) (proof-map-id D X v) (proof-map-id D' X v')\nproof-map-id (sigma S T) X (a , b) = cong (\\x -> (a , x)) (proof-map-id (T a) X b) \nproof-map-id (pi S T) X f = reflFun (\\a -> map (T a) X X (\\x -> x) (f a)) f (\\a -> proof-map-id (T a) X (f a))\n\n\nproof-map-compos : (D : Desc)(X Y Z : Set)\n (f : X -> Y)(g : Y -> Z)\n (v : [| D |] X) -> \n map D X Z (\\x -> g (f x)) v == map D Y Z g (map D X Y f v)\nproof-map-compos id X Y Z f g v = refl\nproof-map-compos (const K) X Y Z f g v = refl\nproof-map-compos (prod D D') X Y Z f g (v , v') = cong2 (\\x y -> (x , y)) \n (proof-map-compos D X Y Z f g v)\n (proof-map-compos D' X Y Z f g v')\nproof-map-compos (sigma S T) X Y Z f g (a , b) = cong (\\x -> (a , x)) (proof-map-compos (T a) X Y Z f g b)\nproof-map-compos (pi S T) X Y Z f g fc = reflFun (\\a -> map (T a) X Z (\\x -> g (f x)) (fc a))\n (\\a -> map (T a) Y Z g (map (T a) X Y f (fc a)))\n (\\a -> proof-map-compos (T a) X Y Z f g (fc a))\n\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms (con xs) = ms xs (all D (Mu D) P (\\x -> induction D P ms x) xs)\n-}\n\n-- But the termination checker is unhappy.\n-- So we write the following:\n\nmodule Elim {l : Level}\n (D : Desc)\n (P : Mu D -> Set l)\n (ms : (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x))\n where\n\n mutual\n induction : (x : Mu D) -> P x\n induction (con xs) = ms xs (hyps D xs)\n \n hyps : (D' : Desc)\n (xs : [| D' |] (Mu D)) ->\n All D' (Mu D) P xs\n hyps id x = induction x\n hyps (const Z) z = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (sigma S T) (a , b) = hyps (T a) b\n hyps (pi S T) f = \\s -> hyps (T s) (f s)\n \ninduction : {l : Level}\n (D : Desc) \n (P : Mu D -> Set l) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms x = Elim.induction D P ms x\n\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> Desc\nnatCases Ze = const Unit\nnatCases Suc = id\n\nNatD : Desc\nNatD = sigma NatConst natCases\n\nNat : Set\nNat = Mu NatD\n\nze : Nat\nze = con (Ze , Void)\n\nsu : Nat -> Nat\nsu n = con (Su , n)\n\n--****************\n-- List\n--****************\n\ndata ListConst : Set where\n Nil : ListConst\n Cons : ListConst\n\nlistCases : Set -> ListConst -> Desc\nlistCases X Nil = const Unit\nlistCases X Cons = sigma X (\\_ -> id)\n\nListD : Set -> Desc\nListD X = sigma ListConst (listCases X)\n\nList : Set -> Set\nList X = Mu (ListD X)\n\nnil : {X : Set} -> List X\nnil = con ( Nil , Void )\n\ncons : {X : Set} -> X -> List X -> List X\ncons x t = con ( Cons , ( x , t ))\n\n--****************\n-- Tree\n--****************\n\ndata TreeConst : Set where\n Leaf : TreeConst\n Node : TreeConst\n\ntreeCases : Set -> TreeConst -> Desc\ntreeCases X Leaf = const Unit\ntreeCases X Node = sigma X (\\_ -> prod id id)\n\nTreeD : Set -> Desc\nTreeD X = sigma TreeConst (treeCases X)\n\nTree : Set -> Set\nTree X = Mu (TreeD X)\n\nleaf : {X : Set} -> Tree X\nleaf = con (Leaf , Void)\n\nnode : {X : Set} -> X -> Tree X -> Tree X -> Tree X\nnode x le ri = con (Node , (x , (le , ri)))\n\n\n--********************************************\n-- Finite sets\n--********************************************\n\n-- If we weren't such bug fans of levitating things, we would\n-- implement finite sets with:\n\n{-\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n-}\n\n-- But no, we make it fly in Desc:\n\nEnumU : Set\nEnumU = Nat\n\nnilE : EnumU\nnilE = ze\n\nconsE : EnumU -> EnumU\nconsE e = su e \n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\n-- All : {l : Level}(D : Desc)(X : Set l)(P : X -> Set l) -> [| D |] X -> Set l\n\n{-\ncasesSpi : (xs : [| NatD |] Nat) -> \n All NatD (Nat {l = zero}) (\\e -> (EnumT e -> Set1) -> Set1) xs -> \n (EnumT (con xs) -> Set1) -> Set1\ncasesSpi (Ze , Void) hs P' = Unit\ncasesSpi (Su , n) hs P' = P' EZe * hs (\\e -> P' (ESu e))\n-}\n\n{-\ninduction : {l : Level}\n (D : Desc) \n (P : Mu D -> Set l) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\n-}\n\ncasesSpi : (xs : [| NatD |] Nat) -> \n All NatD Nat (\\e -> (EnumT e -> Set) -> Set) xs -> \n (EnumT (con xs) -> Set) -> Set\ncasesSpi (Ze , Void) hs P' = Unit\ncasesSpi (Su , n) hs P' = P' EZe * hs (\\e -> P' (ESu e))\n\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi e P = induction NatD (\\E -> (EnumT E -> Set) -> Set) casesSpi e P\n\n{-\ncasesSwitch : (xs : [| NatD |] Nat) ->\n All NatD Nat (\\e -> (P' : EnumT e -> Set1)\n (b' : spi e P')\n (x' : EnumT e) -> P' x') xs ->\n (P' : EnumT (con xs) -> Set1)\n (b' : spi (con xs) P')\n (x' : EnumT (con xs)) -> P' x'\ncasesSwitch (Ze , Void) hs P' b' ()\ncasesSwitch (Su , n) hs P' b' EZe = fst b'\ncasesSwitch (Su , n) hs P' b' (ESu e') = hs (\\e -> P' (ESu e)) (snd b') e'\n\n\nswitch : (e : EnumU)\n (P : EnumT e -> Set1)\n (b : spi e P)\n (x : EnumT e) -> P x\nswitch e P b xs = induction NatD\n (\\e -> (P : EnumT e -> Set1)\n (b : spi e P)\n (xs : EnumT e) -> P xs) \n casesSwitch e P b xs \n\n\n--********************************************\n-- Tagged description\n--********************************************\n\n-- data Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j)\n-- spi : {l : Level}(e : EnumU)(P : EnumT e -> Set l) -> Set l\n\nTagDesc : Set2\nTagDesc = Sigma (EnumU {l = suc (suc zero)}) (\\e -> spi e (\\_ -> Desc {l = zero}))\n\ntoDesc : {l : Level} -> TagDesc -> Desc {l = zero}\ntoDesc {x} (B , F) = {!!} -- sigma (EnumT B) (\\e -> ?) --switch B (\\_ -> Desc {l = x}) F e)\n\ntest : (E : EnumU {l = suc (suc zero)}) -> spi E (\\_ -> Desc {l = zero}) -> EnumT E -> Desc {l = zero}\ntest B F e = {!switch B (\\_ -> Desc {l = zero}) F e!}\n\n-}\n--********************************************\n-- Catamorphism\n--********************************************\n\n{-\n\ncata : (D : Desc)\n (T : Set) ->\n ([| D |] T -> T) ->\n (Mu D) -> T\ncata D T phi x = induction D (\\_ -> T) (\\x ms -> phi (replace D T x ms)) x\n where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\\_ -> T) xs) -> [| D' |] T\n replace id T x y = y\n replace (const Z) T z z' = z'\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : TagDesc -> (X : Set) -> TagDesc\n(e , D) ** X = consE e , (const X , D)\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : (D : TagDesc)(X Y : Set) ->\n (X -> Mu (toDesc (D ** Y))) ->\n [| toDesc (D ** X) |] (Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\napply (E , B) X Y sig (EZe , x) = sig x\napply (E , B) X Y sig (ESu n , t) = con (ESu n , t)\n\nsubst : (D : TagDesc)(X Y : Set) ->\n Mu (toDesc (D ** X)) ->\n (X -> Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\nsubst D X Y x sig = cata (toDesc (D ** X)) (Mu (toDesc (D ** Y))) (apply D X Y sig) x\n-}","old_contents":"\n {-# OPTIONS --universe-polymorphism #-}\n\nmodule Desc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\nunlift : {l : Level}{A : Set l} -> Lifted A -> A\nunlift (lifter a) = a\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\npair : {i j : Level}{A : Set i}{B : A -> Set j} -> \n (x : A) (y : B x) -> Sigma {i = i}{j = j} A B\npair x y = x , y\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l m : Level}{A : Set l}{B : Set m}\n (f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l m n : Level}{A : Set l}{B : Set m}{C : Set n}\n (f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\ntrans : {l : Level}{A : Set l}{x y z : A} -> x == y -> y == z -> x == z\ntrans refl refl = refl\n\nproof-lift-unlift-eq : {l : Level}{A : Set l}(x : Lifted A) -> lifter (unlift x) == x\nproof-lift-unlift-eq (lifter a) = refl\n\npostulate \n reflFun : {l m : Level}{A : Set l}{B : A -> Set m}(f : (a : A) -> B a)(g : (a : A) -> B a)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata Desc {l : Level} : Set (suc l) where\n id : Desc\n const : Set l -> Desc\n prod : Desc -> Desc -> Desc\n sigma : (S : Set l) -> (S -> Desc) -> Desc\n pi : (S : Set l) -> (S -> Desc) -> Desc\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|]_ : {l : Level} -> Desc -> Set l -> Set l\n[| id |] Z = Z\n[| const X |] Z = X\n[| prod D D' |] Z = [| D |] Z * [| D' |] Z\n[| sigma S T |] Z = Sigma S (\\s -> [| T s |] Z)\n[| pi S T |] Z = (s : S) -> [| T s |] Z\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata Mu {l : Level}(D : Desc {l = l}) : Set l where\n con : [| D |] (Mu D) -> Mu D\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {l : Level}(D : Desc)(X : Set l)(P : X -> Set l) -> [| D |] X -> Set l\nAll id X P x = P x\nAll (const Z) X P x = Unit\nAll (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')\nAll (sigma S T) X P (a , b) = All (T a) X P b\nAll (pi S T) X P f = (s : S) -> All (T s) X P (f s)\n\nall : {l : Level}(D : Desc)(X : Set l)(P : X -> Set l)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x\nall id X P R x = R x\nall (const Z) X P R z = Void\nall (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'\nall (sigma S T) X P R (a , b) = all (T a) X P R b\nall (pi S T) X P R f = \\ s -> all (T s) X P R (f s)\n\n--********************************************\n-- Map\n--********************************************\n\nmap : {l : Level}(D : Desc)(X Y : Set l)(f : X -> Y)(v : [| D |] X) -> [| D |] Y\nmap id X Y sig x = sig x\nmap (const Z) X Y sig z = z\nmap (prod D D') X Y sig (d , d') = map D X Y sig d , map D' X Y sig d'\nmap (sigma S T) X Y sig (a , b) = (a , map (T a) X Y sig b)\nmap (pi S T) X Y sig f = \\x -> map (T x) X Y sig (f x)\n\n\nproof-map-id : {l : Level}(D : Desc)(X : Set l)(v : [| D |] X) -> map D X X (\\x -> x) v == v\nproof-map-id id X v = refl\nproof-map-id (const Z) X v = refl\nproof-map-id (prod D D') X (v , v') = cong2 (\\x y -> (x , y)) (proof-map-id D X v) (proof-map-id D' X v')\nproof-map-id (sigma S T) X (a , b) = cong (\\x -> (a , x)) (proof-map-id (T a) X b) \nproof-map-id (pi S T) X f = reflFun (\\a -> map (T a) X X (\\x -> x) (f a)) f (\\a -> proof-map-id (T a) X (f a))\n\n\nproof-map-compos : {l : Level}(D : Desc)(X Y Z : Set l)\n (f : X -> Y)(g : Y -> Z)\n (v : [| D |] X) -> \n map D X Z (\\x -> g (f x)) v == map D Y Z g (map D X Y f v)\nproof-map-compos id X Y Z f g v = refl\nproof-map-compos (const K) X Y Z f g v = refl\nproof-map-compos (prod D D') X Y Z f g (v , v') = cong2 (\\x y -> (x , y)) \n (proof-map-compos D X Y Z f g v)\n (proof-map-compos D' X Y Z f g v')\nproof-map-compos (sigma S T) X Y Z f g (a , b) = cong (\\x -> (a , x)) (proof-map-compos (T a) X Y Z f g b)\nproof-map-compos (pi S T) X Y Z f g fc = reflFun (\\a -> map (T a) X Z (\\x -> g (f x)) (fc a))\n (\\a -> map (T a) Y Z g (map (T a) X Y f (fc a)))\n (\\a -> proof-map-compos (T a) X Y Z f g (fc a))\n\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\ninduction : {l : Set}\n (D : Desc) \n (P : Mu D -> Set l) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms (con xs) = ms xs (all D (Mu D) P (\\x -> induction D P ms x) xs)\n-}\n\n-- But the termination checker is unhappy.\n-- So we write the following:\n\nmodule Elim {l : Level}\n (D : Desc)\n (P : Mu D -> Set l)\n (ms : (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x))\n where\n\n mutual\n induction : (x : Mu D) -> P x\n induction (con xs) = ms xs (hyps D xs)\n \n hyps : (D' : Desc)\n (xs : [| D' |] (Mu D)) ->\n All D' (Mu D) P xs\n hyps id x = induction x\n hyps (const Z) z = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (sigma S T) (a , b) = hyps (T a) b\n hyps (pi S T) f = \\s -> hyps (T s) (f s)\n \ninduction : {l : Level}\n (D : Desc) \n (P : Mu D -> Set l) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms x = Elim.induction D P ms x\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst {l : Level} : Set l where\n Ze : NatConst\n Su : NatConst\n\nnatCases : {l : Level} -> NatConst {l = l} -> Desc {l = l}\nnatCases Ze = const Unit\nnatCases Suc = id\n\nNatD : {l : Level} -> Desc {l = l}\nNatD {x} = sigma {l = x} NatConst natCases\n\nNat : {l : Level} -> Set l\nNat = Mu NatD\n\nze : {l : Level} -> Nat {l = l}\nze {x} = con (pair {i = x} {j = x} Ze Void)\n\nsu : {l : Level} -> Nat {l = l} -> Nat {l = l}\nsu {x} n = con (pair {i = x} {j = x} Su n)\n\n--****************\n-- List\n--****************\n\ndata ListConst : Set where\n Nil : ListConst\n Cons : ListConst\n\nlistCases : Set -> ListConst -> Desc\nlistCases X Nil = const Unit\nlistCases X Cons = sigma X (\\_ -> id)\n\nListD : Set -> Desc\nListD X = sigma ListConst (listCases X)\n\nList : Set -> Set\nList X = Mu (ListD X)\n\nnil : {X : Set} -> List X\nnil = con ( Nil , Void )\n\ncons : {X : Set} -> X -> List X -> List X\ncons x t = con ( Cons , ( x , t ))\n\n--****************\n-- Tree\n--****************\n\ndata TreeConst : Set where\n Leaf : TreeConst\n Node : TreeConst\n\ntreeCases : Set -> TreeConst -> Desc\ntreeCases X Leaf = const Unit\ntreeCases X Node = sigma X (\\_ -> prod id id)\n\nTreeD : Set -> Desc\nTreeD X = sigma TreeConst (treeCases X)\n\nTree : Set -> Set\nTree X = Mu (TreeD X)\n\nleaf : {X : Set} -> Tree X\nleaf = con (Leaf , Void)\n\nnode : {X : Set} -> X -> Tree X -> Tree X -> Tree X\nnode x le ri = con (Node , (x , (le , ri)))\n\n\n--********************************************\n-- Finite sets\n--********************************************\n\n-- If we weren't such bug fans of levitating things, we would\n-- implement finite sets with:\n\n{-\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n-}\n\n-- But no, we make it fly in Desc:\n\nEnumU : {l : Level} -> Set l\nEnumU = Nat\n\nnilE : {l : Level} -> EnumU {l = l}\nnilE {x} = ze {l = x}\n\nconsE : {l : Level} -> EnumU -> EnumU {l = l}\nconsE {x} e = su {l = x} e \n\ndata EnumT {l : Level} : (e : EnumU {l = l}) -> Set l where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\n-- All : {l : Level}(D : Desc)(X : Set l)(P : X -> Set l) -> [| D |] X -> Set l\n\n{-\ncasesSpi : (xs : [| NatD |] Nat) -> \n All NatD (Nat {l = zero}) (\\e -> (EnumT e -> Set1) -> Set1) xs -> \n (EnumT (con xs) -> Set1) -> Set1\ncasesSpi (Ze , Void) hs P' = Unit\ncasesSpi (Su , n) hs P' = P' EZe * hs (\\e -> P' (ESu e))\n-}\n\n{-\ninduction : {l : Level}\n (D : Desc) \n (P : Mu D -> Set l) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\n-}\n\nspi : (e : EnumU {l = zero})(P : EnumT {l = zero} e -> Set) -> Set\nspi e P = induction NatD (\\E -> {!(EnumT {l = zero} e -> Set) -> Set!}) {!!} e ?\n\n{-\ncasesSwitch : (xs : [| NatD |] Nat) ->\n All NatD Nat (\\e -> (P' : EnumT e -> Set1)\n (b' : spi e P')\n (x' : EnumT e) -> P' x') xs ->\n (P' : EnumT (con xs) -> Set1)\n (b' : spi (con xs) P')\n (x' : EnumT (con xs)) -> P' x'\ncasesSwitch (Ze , Void) hs P' b' ()\ncasesSwitch (Su , n) hs P' b' EZe = fst b'\ncasesSwitch (Su , n) hs P' b' (ESu e') = hs (\\e -> P' (ESu e)) (snd b') e'\n\n\nswitch : (e : EnumU)\n (P : EnumT e -> Set1)\n (b : spi e P)\n (x : EnumT e) -> P x\nswitch e P b xs = induction NatD\n (\\e -> (P : EnumT e -> Set1)\n (b : spi e P)\n (xs : EnumT e) -> P xs) \n casesSwitch e P b xs \n\n\n--********************************************\n-- Tagged description\n--********************************************\n\n-- data Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j)\n-- spi : {l : Level}(e : EnumU)(P : EnumT e -> Set l) -> Set l\n\nTagDesc : Set2\nTagDesc = Sigma (EnumU {l = suc (suc zero)}) (\\e -> spi e (\\_ -> Desc {l = zero}))\n\ntoDesc : {l : Level} -> TagDesc -> Desc {l = zero}\ntoDesc {x} (B , F) = {!!} -- sigma (EnumT B) (\\e -> ?) --switch B (\\_ -> Desc {l = x}) F e)\n\ntest : (E : EnumU {l = suc (suc zero)}) -> spi E (\\_ -> Desc {l = zero}) -> EnumT E -> Desc {l = zero}\ntest B F e = {!switch B (\\_ -> Desc {l = zero}) F e!}\n\n-}\n--********************************************\n-- Catamorphism\n--********************************************\n\n{-\n\ncata : (D : Desc)\n (T : Set) ->\n ([| D |] T -> T) ->\n (Mu D) -> T\ncata D T phi x = induction D (\\_ -> T) (\\x ms -> phi (replace D T x ms)) x\n where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\\_ -> T) xs) -> [| D' |] T\n replace id T x y = y\n replace (const Z) T z z' = z'\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : TagDesc -> (X : Set) -> TagDesc\n(e , D) ** X = consE e , (const X , D)\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : (D : TagDesc)(X Y : Set) ->\n (X -> Mu (toDesc (D ** Y))) ->\n [| toDesc (D ** X) |] (Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\napply (E , B) X Y sig (EZe , x) = sig x\napply (E , B) X Y sig (ESu n , t) = con (ESu n , t)\n\nsubst : (D : TagDesc)(X Y : Set) ->\n Mu (toDesc (D ** X)) ->\n (X -> Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\nsubst D X Y x sig = cata (toDesc (D ** X)) (Mu (toDesc (D ** Y))) (apply D X Y sig) x\n-}","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"2ec8aea3dc17eefe9711d6abe7b362518630d643","subject":"Only white space.","message":"Only white space.\n\nIgnore-this: 3c31ab0a948cb2060a9774b36afb0522\n\ndarcs-hash:20100505162557-3bd4e-272690a3fb1e771a2d00378d140d2867c3e088dc.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"MyStdLib\/Induction\/WellFounded.agda","new_file":"MyStdLib\/Induction\/WellFounded.agda","new_contents":"------------------------------------------------------------------------------\n-- Well-founded induction\n------------------------------------------------------------------------------\n\nmodule MyStdLib.Induction.WellFounded where\n\n-- From: http:\/\/www.iis.sinica.edu.tw\/~scm\/2008\/well-founded-recursion-and-accessibility\/\n\ndata Acc {A : Set}(R : A \u2192 A \u2192 Set) : A \u2192 Set where\n acc : (x : A) \u2192 ((y : A) \u2192 R y x \u2192 Acc R y) \u2192 Acc R x\n\nWellFounded : {A : Set} \u2192 (A \u2192 A \u2192 Set) \u2192 Set\nWellFounded {A} R = (x : A) \u2192 Acc R x\n\naccFold : {A : Set}(R : A \u2192 A \u2192 Set){P : A \u2192 Set} \u2192\n ((x : A) \u2192 ((y : A) \u2192 R y x \u2192 P y) \u2192 P x) \u2192\n (x : A) \u2192 Acc R x \u2192 P x\naccFold R f x (acc .x h) = f x (\u03bb y y d' \u2192 \u22a5\n lem2 IEHole ()\n lem2 (INEHole x) ()\n lem2 (IAp x\u2081 () x\u2082) (ITLam x\u2083)\n lem2 (IAp x (ICastArr x\u2081 ind) x\u2082) (ITApCast x\u2083 x\u2084) = x _ _ _ _ _ refl\n lem2 (ICastArr x ind) (ITCastID (FBoxed x\u2081)) = vi x\u2081 ind\n lem2 (ICastArr x ind) (ITCastID (FIndet x\u2081)) = x refl\n lem2 (ICastGroundHole x ind) stp = {!!}\n lem2 (ICastHoleGround x ind x\u2081) stp = {!!}\n\n lem3 : \u2200{d d'} \u2192 d boxedval \u2192 d \u2192> d' \u2192 \u22a5\n lem3 (BVVal VConst) ()\n lem3 (BVVal VLam) ()\n lem3 (BVArrCast x bv) (ITCastID x\u2081) = x refl\n lem3 (BVHoleCast () bv) (ITCastID x\u2081)\n lem3 (BVHoleCast () bv) (ITCastSucceed x\u2081 x\u2082)\n lem3 (BVHoleCast GHole bv) (ITGround x\u2081) = {!!} -- cyrus\n\n lem1 : \u2200{d d'} \u2192 d final \u2192 d \u2192> d' \u2192 \u22a5\n lem1 (FBoxed x) = lem3 x\n lem1 (FIndet x) = lem2 x\n\n lem4 : \u2200{d \u03b5 x} \u2192 d final \u2192 d == \u03b5 \u27e6 x \u27e7 \u2192 x final\n lem4 x FHOuter = x\n lem4 (FBoxed (BVVal ())) (FHAp1 eps)\n lem4 (FBoxed (BVVal ())) (FHAp2 x\u2082 eps)\n lem4 (FBoxed (BVVal ())) (FHNEHole eps)\n lem4 (FBoxed (BVVal ())) (FHCast eps)\n lem4 (FBoxed (BVArrCast x\u2081 x\u2082)) (FHCast eps) = lem4 (FBoxed x\u2082) eps\n lem4 (FBoxed (BVHoleCast x\u2081 x\u2082)) (FHCast eps) = lem4 (FBoxed x\u2082) eps\n lem4 (FIndet (IAp x\u2081 x\u2082 x\u2083)) (FHAp1 eps) = lem4 (FIndet x\u2082) eps\n lem4 (FIndet (IAp x\u2081 x\u2082 x\u2083)) (FHAp2 x\u2084 eps) = lem4 x\u2083 eps\n lem4 (FIndet (INEHole x\u2081)) (FHNEHole eps) = lem4 x\u2081 eps\n lem4 (FIndet (ICastArr x\u2081 x\u2082)) (FHCast eps) = lem4 (FIndet x\u2082) eps\n lem4 (FIndet (ICastGroundHole x\u2081 x\u2082)) (FHCast eps) = lem4 (FIndet x\u2082) eps\n lem4 (FIndet (ICastHoleGround x\u2081 x\u2082 x\u2083)) (FHCast eps) = lem4 (FIndet x\u2082) eps\n\n lem5 : \u2200{d d' d'' \u03b5} \u2192 d final \u2192 d == \u03b5 \u27e6 d' \u27e7 \u2192 d' \u2192> d'' \u2192 \u22a5\n lem5 f sub step = lem1 (lem4 f sub) step\n\n -- indeterminates and expressions that step are disjoint\n mutual\n is : \u2200{d} \u2192 d indet \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n is IEHole (d' , Step FHOuter () FHOuter)\n is (INEHole x) (d' , Step FHOuter () FHOuter)\n is (INEHole x) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = lem5 x x\u2081 x\u2082\n is (IAp x\u2081 () x\u2082) (_ , Step FHOuter (ITLam x\u2083) FHOuter)\n is (IAp x (ICastArr x\u2081 ind) x\u2082) (_ , Step FHOuter (ITApCast x\u2083 x\u2084) FHOuter) = x _ _ _ _ _ refl\n is (IAp x ind _) (_ , Step (FHAp1 x\u2082) x\u2083 (FHAp1 x\u2084)) = is ind (_ , Step x\u2082 x\u2083 x\u2084)\n is (IAp x ind f) (_ , Step (FHAp2 x\u2082 x\u2083) x\u2084 (FHAp2 x\u2085 x\u2086)) = fs f (_ , Step x\u2083 x\u2084 x\u2086)\n is (ICastArr x ind) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n is (ICastArr x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = is ind (_ , Step x\u2081 x\u2082 x\u2083)\n is (ICastGroundHole () ind) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n is (ICastGroundHole x ind) (d' , Step FHOuter (ITCastSucceed x\u2081 ()) FHOuter)\n is (ICastGroundHole GHole ind) (_ , Step FHOuter (ITGround (FBoxed x)) FHOuter) = vi x ind\n is (ICastGroundHole GHole ind) (_ , Step FHOuter (ITGround (FIndet x)) FHOuter) = {!!} -- cyrus\n is (ICastGroundHole x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = is ind (_ , Step x\u2081 x\u2082 x\u2083)\n is (ICastHoleGround x ind ()) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n is (ICastHoleGround x ind g) (d' , Step FHOuter (ITCastSucceed x\u2081 x\u2082) FHOuter) = x _ _ refl\n is (ICastHoleGround x ind GHole) (_ , Step FHOuter (ITExpand x\u2081 x\u2082) FHOuter) = x\u2082 refl\n is (ICastHoleGround x ind g) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = is ind (_ , Step x\u2081 x\u2082 x\u2083)\n\n fs : \u2200{d} \u2192 d final \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 \u22a5\n fs (FBoxed x) stp = vs x stp\n fs (FIndet x) stp = is x stp\n\n -- errors and expressions that step are disjoint\n es : \u2200{d} \u2192 d casterr \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n es er stp = {!!}\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import type-assignment-unicity\n\n-- taken together, the theorems in this file argue that for any expression\n-- d, at most one summand of the labeled sum that results from progress may\n-- be true at any time, i.e. that values, indeterminates, errors, and\n-- expressions that step are pairwise disjoint. (note that as a consequence\n-- of currying and comutativity of products, this means that there are six\n-- theorems to prove)\nmodule progress-checks where\n -- boxed values and indeterminates are disjoint\n vi : \u2200{d} \u2192 d boxedval \u2192 d indet \u2192 \u22a5\n vi (BVVal VConst) ()\n vi (BVVal VLam) ()\n vi (BVArrCast x bv) (ICastArr x\u2081 ind) = vi bv ind\n vi (BVHoleCast x bv) (ICastGroundHole x\u2081 ind) = vi bv ind\n vi (BVHoleCast x bv) (ICastHoleGround x\u2081 ind x\u2082) = vi bv ind\n\n -- boxed values and errors are disjoint\n ve : \u2200{d} \u2192 d boxedval \u2192 d casterr \u2192 \u22a5\n ve (BVVal ()) (CECastFinal x\u2081 x\u2082 x\u2083 x\u2084)\n ve (BVHoleCast x bv) (CECastFinal x\u2081 x\u2082 () x\u2084)\n ve (BVArrCast x bv) (CECong FHOuter (CECong eps er)) = {!!}\n ve (BVArrCast x bv) (CECong (FHCast x\u2081) er) = ve bv (CECong x\u2081 er)\n ve (BVHoleCast x bv) (CECong x\u2081 er) = {!!}\n ve (BVVal x) (CECong x\u2081 er) = ve (lem-valfill x x\u2081) er\n where\n lem-valfill : \u2200{\u03b5 d d'} \u2192 d val \u2192 d == \u03b5 \u27e6 d' \u27e7 \u2192 d' boxedval\n lem-valfill VConst FHOuter = BVVal VConst\n lem-valfill VLam FHOuter = BVVal VLam\n\n -- boxed values and expressions that step are disjoint\n vs : \u2200{d} \u2192 d boxedval \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n vs (BVVal VConst) (d' , Step FHOuter () x\u2083)\n vs (BVVal VLam) (d' , Step FHOuter () x\u2083)\n vs (BVArrCast x bv) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n vs (BVArrCast x bv) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = vs bv (_ , Step x\u2081 x\u2082 x\u2083)\n vs (BVHoleCast x bv) (d' , Step FHOuter x\u2082 FHOuter) = {!x\u2082!} -- cyrus\n vs (BVHoleCast x bv) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = vs bv (_ , Step x\u2081 x\u2082 x\u2083)\n\n mutual\n -- indeterminates and errors are disjoint\n ie : \u2200{d} \u2192 d indet \u2192 d casterr \u2192 \u22a5\n ie IEHole (CECong FHOuter err) = ie IEHole err -- this is extremely strange\n ie (INEHole x) (CECong x\u2081 err) = {!!} -- fe x {!!}\n ie (IAp x indet x\u2081) (CECong x\u2082 err) = {!x\u2082!}\n ie (ICastArr x indet) (CECong x\u2081 err) = {!!}\n ie (ICastGroundHole x indet) (CECastFinal x\u2081 x\u2082 () x\u2084)\n ie (ICastGroundHole x indet) (CECong x\u2081 err) = {!!}\n ie (ICastHoleGround x indet x\u2081) (CECastFinal x\u2082 x\u2083 x\u2084 x\u2085) = {!!}\n ie (ICastHoleGround x indet x\u2081) (CECong x\u2082 err) = {!!}\n\n -- ie (INEHole x) (ENEHole e) = fe x e\n -- ie (IAp i x) (EAp1 e) = ie i e\n -- ie (IAp i x) (EAp2 y e) = fe x e\n -- ie (ICast i) (ECastError x x\u2081) = {!!} -- todo: this is evidence that casts are busted\n -- ie (ICast i) (ECastProp x) = ie i x\n\n -- final expressions are not errors (not one of the 6 cases for progress, just a convenience)\n fe : \u2200{d} \u2192 d final \u2192 d casterr \u2192 \u22a5\n fe (FBoxed x) err = ve x err\n fe (FIndet x) err = ie x err\n\n -- todo: these are bad names; probably some places below where i inlined\n -- some of these lemmas before i'd come up with them\n lem2 : \u2200{d d'} \u2192 d indet \u2192 d \u2192> d' \u2192 \u22a5\n lem2 IEHole ()\n lem2 (INEHole x) ()\n lem2 (IAp x\u2081 () x\u2082) (ITLam x\u2083)\n lem2 (IAp x (ICastArr x\u2081 ind) x\u2082) (ITApCast x\u2083 x\u2084) = {!!} -- cyrus\n lem2 (ICastArr x ind) (ITCastID (FBoxed x\u2081)) = vi x\u2081 ind\n lem2 (ICastArr x ind) (ITCastID (FIndet x\u2081)) = x refl\n lem2 (ICastGroundHole x ind) stp = {!!}\n lem2 (ICastHoleGround x ind x\u2081) stp = {!!}\n\n lem3 : \u2200{d d'} \u2192 d boxedval \u2192 d \u2192> d' \u2192 \u22a5\n lem3 (BVVal VConst) ()\n lem3 (BVVal VLam) ()\n lem3 (BVArrCast x bv) (ITCastID x\u2081) = x refl\n lem3 (BVHoleCast () bv) (ITCastID x\u2081)\n lem3 (BVHoleCast () bv) (ITCastSucceed x\u2081 x\u2082)\n lem3 (BVHoleCast GHole bv) (ITGround x\u2081) = {!!} -- cyrus\n\n lem1 : \u2200{d d'} \u2192 d final \u2192 d \u2192> d' \u2192 \u22a5\n lem1 (FBoxed x) = lem3 x\n lem1 (FIndet x) = lem2 x\n\n lem4 : \u2200{d \u03b5 x} \u2192 d final \u2192 d == \u03b5 \u27e6 x \u27e7 \u2192 x final\n lem4 (FBoxed x) FHOuter = FBoxed x\n lem4 (FBoxed (BVVal ())) (FHAp1 eps)\n lem4 (FBoxed (BVVal ())) (FHAp2 x\u2082 eps)\n lem4 (FBoxed (BVVal ())) (FHNEHole eps)\n lem4 (FBoxed (BVVal ())) (FHCast eps)\n lem4 (FBoxed (BVArrCast x\u2081 x\u2082)) (FHCast eps) = lem4 (FBoxed x\u2082) eps\n lem4 (FBoxed (BVHoleCast x\u2081 x\u2082)) (FHCast eps) = lem4 (FBoxed x\u2082) eps\n lem4 (FIndet x) FHOuter = FIndet x\n lem4 (FIndet (IAp x\u2081 x\u2082 x\u2083)) (FHAp1 eps) = lem4 (FIndet x\u2082) eps\n lem4 (FIndet (IAp x\u2081 x\u2082 x\u2083)) (FHAp2 x\u2084 eps) = lem4 x\u2083 eps\n lem4 (FIndet (INEHole x\u2081)) (FHNEHole eps) = lem4 x\u2081 eps\n lem4 (FIndet (ICastArr x\u2081 x\u2082)) (FHCast eps) = lem4 (FIndet x\u2082) eps\n lem4 (FIndet (ICastGroundHole x\u2081 x\u2082)) (FHCast eps) = lem4 (FIndet x\u2082) eps\n lem4 (FIndet (ICastHoleGround x\u2081 x\u2082 x\u2083)) (FHCast eps) = lem4 (FIndet x\u2082) eps\n\n lem5 : \u2200{d d' d'' \u03b5} \u2192 d final \u2192 d == \u03b5 \u27e6 d' \u27e7 \u2192 d' \u2192> d'' \u2192 \u22a5\n lem5 f sub step = lem1 (lem4 f sub) step\n\n -- indeterminates and expressions that step are disjoint\n mutual\n is : \u2200{d} \u2192 d indet \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n is IEHole (d' , Step FHOuter () FHOuter)\n is (INEHole x) (d' , Step FHOuter () FHOuter)\n is (INEHole x) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = lem5 x x\u2081 x\u2082\n is (IAp x\u2081 () x\u2082) (_ , Step FHOuter (ITLam x\u2083) FHOuter)\n is (IAp x (ICastArr x\u2081 ind) x\u2082) (_ , Step FHOuter (ITApCast x\u2083 x\u2084) FHOuter) = {!!} -- cyrus \/ maybe that error in the rule with pi-types\n is (IAp x ind _) (_ , Step (FHAp1 x\u2082) x\u2083 (FHAp1 x\u2084)) = is ind (_ , Step x\u2082 x\u2083 x\u2084)\n is (IAp x ind f) (_ , Step (FHAp2 x\u2082 x\u2083) x\u2084 (FHAp2 x\u2085 x\u2086)) = fs f (_ , Step x\u2083 x\u2084 x\u2086)\n is (ICastArr x ind) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n is (ICastArr x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = is ind (_ , Step x\u2081 x\u2082 x\u2083)\n is (ICastGroundHole () ind) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n is (ICastGroundHole x ind) (d' , Step FHOuter (ITCastSucceed x\u2081 ()) FHOuter)\n is (ICastGroundHole GHole ind) (_ , Step FHOuter (ITGround (FBoxed x)) FHOuter) = vi x ind\n is (ICastGroundHole GHole ind) (_ , Step FHOuter (ITGround (FIndet x)) FHOuter) = {!!}\n is (ICastGroundHole x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = is ind (_ , Step x\u2081 x\u2082 x\u2083)\n is (ICastHoleGround x ind g) (d' , Step FHOuter x\u2082 FHOuter) = {!!}\n is (ICastHoleGround x ind g) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = is ind (_ , Step x\u2081 x\u2082 x\u2083)\n\n fs : \u2200{d} \u2192 d final \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 \u22a5\n fs (FBoxed x) stp = vs x stp\n fs (FIndet x) stp = is x stp\n\n -- errors and expressions that step are disjoint\n es : \u2200{d} \u2192 d casterr \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n es er stp = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"1bea8799be81d6b9e2d85a6427556d95be3d529e","subject":"red counterexamples; green examples and counterexamples","message":"red counterexamples; green examples and counterexamples\n","repos":"toonn\/popartt","old_file":"koopa.agda","new_file":"koopa.agda","new_contents":"{-\n\n Verified KoopaTroopa Movement\n Toon Nolten\n\n-}\n\nmodule koopa where\n open import Data.Nat\n open import Data.Fin renaming (_+_ to _F+_; _<_ to _F<_; suc to fsuc;\n zero to fzero)\n open import Data.Vec renaming (map to vmap; lookup to vlookup;\n replicate to vreplicate)\n open import Data.Unit\n open import Data.Empty\n\n module Matrix where\n data Matrix (A : Set) : \u2115 \u2192 \u2115 \u2192 Set where\n Mat : {w h : \u2115} \u2192 Vec (Vec A w) h \u2192 Matrix A w h\n\n lookup : \u2200 {w h} {A : Set} \u2192 Fin h \u2192 Fin w \u2192 Matrix A w h \u2192 A\n lookup row column (Mat rows) = vlookup column (vlookup row rows)\n open Matrix\n\n data Color : Set where\n Green : Color\n Red : Color\n\n data KoopaTroopa : Color \u2192 Set where\n _KT : (c : Color) \u2192 KoopaTroopa c\n\n data Material : Set where\n gas : Material\n -- liquid : Material\n solid : Material\n\n data Clearance : Set where\n Low : Clearance\n High : Clearance\n God : Clearance\n\n record Position : Set where\n constructor pos\n field\n x : \u2115\n y : \u2115\n mat : Material\n clr : Clearance\n\n data _c>_ : Color \u2192 Clearance \u2192 Set where\n : \u2200 {c} \u2192 c c> Low\n : Green c> High\n\n data _follows_\u27e8_\u27e9 : Position \u2192 Position \u2192 Color \u2192 Set where\n stay : \u2200 {c x y} \u2192 pos x y gas Low follows pos x y gas Low \u27e8 c \u27e9\n next : \u2200 {c cl x y}{{_ : c c> cl}} \u2192\n pos (suc x) y gas cl follows pos x y gas Low \u27e8 c \u27e9\n back : \u2200 {c cl x y}{{_ : c c> cl}} \u2192\n pos x y gas cl follows pos (suc x) y gas Low \u27e8 c \u27e9\n -- jump : \u2200 {c x y} \u2192 pos x (suc y) gas High follows pos x y gas Low \u27e8 c \u27e9\n fall : \u2200 {c cl x y} \u2192 pos x y gas cl follows pos x (suc y) gas High \u27e8 c \u27e9\n\n\n infixr 5 _\u21a0\u27e8_\u27e9_\n data Path {c : Color} (Koopa : KoopaTroopa c) :\n Position \u2192 Position \u2192 Set where\n [] : \u2200 {p} \u2192 Path Koopa p p\n _\u21a0\u27e8_\u27e9_ : {q r : Position} \u2192 (p : Position) \u2192 q follows p \u27e8 c \u27e9\n \u2192 (qs : Path Koopa q r) \u2192 Path Koopa p r\n\n ex_path : Path (Red KT) (pos 0 0 gas Low) (pos 0 0 gas Low)\n ex_path = pos 0 0 gas Low \u21a0\u27e8 next \u27e9\n pos 1 0 gas Low \u21a0\u27e8 back \u27e9\n pos 0 0 gas Low \u21a0\u27e8 stay \u27e9 []\n\n matToPosVec : {n : \u2115} \u2192 Vec Material n \u2192 Vec Material n \u2192 \u2115 \u2192 \u2115 \u2192\n Vec Position n\n matToPosVec [] [] _ _ = []\n matToPosVec (mat \u2237 mats) (under \u2237 unders) x y =\n pos x y mat cl \u2237 matToPosVec mats unders (x + 1) y\n where\n clearance : Material \u2192 Material \u2192 Clearance\n clearance gas gas = High\n clearance gas solid = Low\n clearance solid _ = God\n cl = clearance mat under\n\n matToPosVecs : {w h : \u2115} \u2192 Vec (Vec Material w) h \u2192 Vec (Vec Position w) h\n matToPosVecs [] = []\n matToPosVecs (_\u2237_ {y} mats matss) =\n matToPosVec mats (unders matss gas) 0 y \u2237 matToPosVecs matss\n where\n unders : \u2200 {m n \u2113}{A : Set \u2113} \u2192 Vec (Vec A m) n \u2192 A \u2192 Vec A m\n unders [] fallback = vreplicate fallback\n unders (us \u2237 _) _ = us\n\n matsToMat : {w h : \u2115} \u2192 Vec (Vec Material w) h \u2192 Matrix Position w h\n matsToMat matss = Mat (reverse (matToPosVecs matss))\n\n \u25a1 : Material\n \u25a1 = gas\n \u25a0 : Material\n \u25a0 = solid\n example_level : Matrix Position 10 7\n example_level = matsToMat (\n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a0 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 []) \u2237 \n (\u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 []) \u2237 [])\n\n _<'_ : \u2115 \u2192 \u2115 \u2192 Set\n m <' zero = \u22a5\n zero <' suc n = \u22a4\n suc m <' suc n = m <' n\n\n fromNat : \u2200 {n}(k : \u2115){_ : k <' n} \u2192 Fin n\n fromNat {zero} k {}\n fromNat {suc n} zero = fzero\n fromNat {suc n} (suc k) {p} = fsuc (fromNat k {p})\n \n f : \u2200 {n}(k : \u2115){_ : k <' n} \u2192 Fin n\n f = fromNat\n\n p : (x : Fin 10) \u2192 (y : Fin 7) \u2192 Position\n p x y = lookup y x example_level\n\n red_path_one : Path (Red KT) (p (f 7) (f 6)) (p (f 8) (f 6))\n red_path_one = p (f 7) (f 6) \u21a0\u27e8 back \u27e9\n p (f 6) (f 6) \u21a0\u27e8 next \u27e9\n p (f 7) (f 6) \u21a0\u27e8 next \u27e9\n p (f 8) (f 6) \u21a0\u27e8 stay \u27e9 []\n\n red_path_two : Path (Red KT) (p (f 2) (f 1)) (p (f 3) (f 1))\n red_path_two = p (f 2) (f 1) \u21a0\u27e8 back \u27e9\n p (f 1) (f 1) \u21a0\u27e8 next \u27e9\n p (f 2) (f 1) \u21a0\u27e8 next \u27e9\n p (f 3) (f 1) \u21a0\u27e8 next \u27e9\n p (f 4) (f 1) \u21a0\u27e8 back \u27e9\n p (f 3) (f 1) \u21a0\u27e8 stay \u27e9\n []\n\n -- -- Type error shows up 'late' because 'cons' is right associative\n -- red_nopath_one : Path (Red KT) (p (f 1) (f 1)) (p (f 0) (f 1))\n -- red_nopath_one = p (f 1) (f 1) \u21a0\u27e8 back \u27e9\n -- p (f 0) (f 1) \u21a0\u27e8 stay \u27e9\n -- []\n\n -- -- Red KoopaTroopa can't step into a wall\n -- red_nopath_two : Path (Red KT) (p (f 1) (f 1)) (p (f 0) (f 1))\n -- red_nopath_two = p (f 1) (f 1) \u21a0\u27e8 back \u27e9 []\n\n -- -- Red KoopaTroopa can't step into air\n -- red_nopath_three : Path (Red KT) (p (f 4) (f 1)) (p (f 5) (f 1))\n -- red_nopath_three = p (f 4) (f 1) \u21a0\u27e8 next \u27e9 []\n\n -- Any path that is valid for red KoopaTroopas, is also valid for green\n -- KoopaTroopas because we did not constrain KoopaTroopas to only turn\n -- When there is an obstacle\n green_path_one : Path (Green KT) (p (f 7) (f 6)) (p (f 8) (f 6))\n green_path_one = p (f 7) (f 6) \u21a0\u27e8 back \u27e9\n p (f 6) (f 6) \u21a0\u27e8 next \u27e9\n p (f 7) (f 6) \u21a0\u27e8 next \u27e9\n p (f 8) (f 6) \u21a0\u27e8 stay \u27e9 []\n\n green_path_two : Path (Green KT) (p (f 7) (f 6)) (p (f 5) (f 0))\n green_path_two = p (f 7) (f 6) \u21a0\u27e8 back \u27e9\n p (f 6) (f 6) \u21a0\u27e8 back \u27e9\n p (f 5) (f 6) \u21a0\u27e8 fall \u27e9\n p (f 5) (f 5) \u21a0\u27e8 fall \u27e9\n p (f 5) (f 4) \u21a0\u27e8 back \u27e9\n p (f 4) (f 4) \u21a0\u27e8 back \u27e9\n p (f 3) (f 4) \u21a0\u27e8 back \u27e9\n p (f 2) (f 4) \u21a0\u27e8 fall \u27e9\n p (f 2) (f 3) \u21a0\u27e8 fall \u27e9\n p (f 2) (f 2) \u21a0\u27e8 fall \u27e9\n p (f 2) (f 1) \u21a0\u27e8 back \u27e9\n p (f 1) (f 1) \u21a0\u27e8 next \u27e9\n p (f 2) (f 1) \u21a0\u27e8 next \u27e9\n p (f 3) (f 1) \u21a0\u27e8 next \u27e9\n p (f 4) (f 1) \u21a0\u27e8 next \u27e9\n p (f 5) (f 1) \u21a0\u27e8 fall \u27e9\n []\n\n -- -- Green KoopaTroopa can't step into a wall\n -- green_nopath_one : Path (Green KT) (p (f 1) (f 1)) (p (f 0) (f 1))\n -- green_nopath_one = p (f 1) (f 1) \u21a0\u27e8 back \u27e9 []\n","old_contents":"{-\n\n Verified Koopa Troopa Movement\n Toon Nolten\n\n-}\n\nmodule koopa where\n open import Data.Nat\n open import Data.Fin renaming (_+_ to _F+_; _<_ to _F<_; suc to fsuc;\n zero to fzero)\n open import Data.Vec renaming (map to vmap; lookup to vlookup;\n replicate to vreplicate)\n open import Data.Unit\n open import Data.Empty\n\n module Matrix where\n data Matrix (A : Set) : \u2115 \u2192 \u2115 \u2192 Set where\n Mat : {w h : \u2115} \u2192 Vec (Vec A w) h \u2192 Matrix A w h\n\n lookup : \u2200 {w h} {A : Set} \u2192 Fin h \u2192 Fin w \u2192 Matrix A w h \u2192 A\n lookup row column (Mat rows) = vlookup column (vlookup row rows)\n open Matrix\n\n data Color : Set where\n Green : Color\n Red : Color\n\n data KoopaTroopa : Color \u2192 Set where\n _KT : (c : Color) \u2192 KoopaTroopa c\n\n data Material : Set where\n gas : Material\n -- liquid : Material\n solid : Material\n\n data Clearance : Set where\n Low : Clearance\n High : Clearance\n God : Clearance\n\n record Position : Set where\n constructor pos\n field\n x : \u2115\n y : \u2115\n mat : Material\n clr : Clearance\n\n data _c>_ : Color \u2192 Clearance \u2192 Set where\n : \u2200 {c} \u2192 c c> Low\n : Green c> High\n\n data _follows_\u27e8_\u27e9 : Position \u2192 Position \u2192 Color \u2192 Set where\n stay : \u2200 {c x y} \u2192 pos x y gas Low follows pos x y gas Low \u27e8 c \u27e9\n next : \u2200 {c cl x y}{{_ : c c> cl}} \u2192\n pos (suc x) y gas cl follows pos x y gas Low \u27e8 c \u27e9\n back : \u2200 {c cl x y}{{_ : c c> cl}} \u2192\n pos x y gas cl follows pos (suc x) y gas Low \u27e8 c \u27e9\n -- jump : \u2200 {c x y} \u2192 pos x (suc y) gas High follows pos x y gas Low \u27e8 c \u27e9\n fall : \u2200 {c cl x y} \u2192 pos x y gas cl follows pos x (suc y) gas High \u27e8 c \u27e9\n\n\n infixr 5 _\u21a0\u27e8_\u27e9_\n data Path {c : Color} (Koopa : KoopaTroopa c) :\n Position \u2192 Position \u2192 Set where\n [] : \u2200 {p} \u2192 Path Koopa p p\n _\u21a0\u27e8_\u27e9_ : {q r : Position} \u2192 (p : Position) \u2192 q follows p \u27e8 c \u27e9\n \u2192 (qs : Path Koopa q r) \u2192 Path Koopa p r\n\n ex_path : Path (Red KT) (pos 0 0 gas Low) (pos 0 0 gas Low)\n ex_path = pos 0 0 gas Low \u21a0\u27e8 next \u27e9\n pos 1 0 gas Low \u21a0\u27e8 back \u27e9\n pos 0 0 gas Low \u21a0\u27e8 stay \u27e9 []\n\n matToPosVec : {n : \u2115} \u2192 Vec Material n \u2192 Vec Material n \u2192 \u2115 \u2192 \u2115 \u2192\n Vec Position n\n matToPosVec [] [] _ _ = []\n matToPosVec (mat \u2237 mats) (under \u2237 unders) x y =\n pos x y mat cl \u2237 matToPosVec mats unders (x + 1) y\n where\n clearance : Material \u2192 Material \u2192 Clearance\n clearance gas gas = High\n clearance gas solid = Low\n clearance solid _ = God\n cl = clearance mat under\n\n matToPosVecs : {w h : \u2115} \u2192 Vec (Vec Material w) h \u2192 Vec (Vec Position w) h\n matToPosVecs [] = []\n matToPosVecs (_\u2237_ {y} mats matss) =\n matToPosVec mats (unders matss gas) 0 y \u2237 matToPosVecs matss\n where\n unders : \u2200 {m n \u2113}{A : Set \u2113} \u2192 Vec (Vec A m) n \u2192 A \u2192 Vec A m\n unders [] fallback = vreplicate fallback\n unders (us \u2237 _) _ = us\n\n matsToMat : {w h : \u2115} \u2192 Vec (Vec Material w) h \u2192 Matrix Position w h\n matsToMat matss = Mat (reverse (matToPosVecs matss))\n\n \u25a1 : Material\n \u25a1 = gas\n \u25a0 : Material\n \u25a0 = solid\n example_level : Matrix Position 10 7\n example_level = matsToMat (\n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 []) \u2237 \n (\u25a0 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 []) \u2237 \n (\u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a1 \u2237 \u25a1 \u2237 \u25a0 \u2237 \u25a0 \u2237 \u25a0 \u2237 []) \u2237 [])\n\n _<'_ : \u2115 \u2192 \u2115 \u2192 Set\n m <' zero = \u22a5\n zero <' suc n = \u22a4\n suc m <' suc n = m <' n\n\n fromNat : \u2200 {n}(k : \u2115){_ : k <' n} \u2192 Fin n\n fromNat {zero} k {}\n fromNat {suc n} zero = fzero\n fromNat {suc n} (suc k) {p} = fsuc (fromNat k {p})\n \n f : \u2200 {n}(k : \u2115){_ : k <' n} \u2192 Fin n\n f = fromNat\n\n p : (x : Fin 10) \u2192 (y : Fin 7) \u2192 Position\n p x y = lookup y x example_level\n\n red_path_one : Path (Red KT) (p (f 7) (f 6)) (p (f 8) (f 6))\n red_path_one = p (f 7) (f 6) \u21a0\u27e8 back \u27e9\n p (f 6) (f 6) \u21a0\u27e8 next \u27e9\n p (f 7) (f 6) \u21a0\u27e8 next \u27e9\n p (f 8) (f 6) \u21a0\u27e8 stay \u27e9 []\n\n red_path_two : Path (Red KT) (p (f 2) (f 1)) (p (f 3) (f 1))\n red_path_two = p (f 2) (f 1) \u21a0\u27e8 back \u27e9\n p (f 1) (f 1) \u21a0\u27e8 next \u27e9\n p (f 2) (f 1) \u21a0\u27e8 next \u27e9\n p (f 3) (f 1) \u21a0\u27e8 next \u27e9\n p (f 4) (f 1) \u21a0\u27e8 back \u27e9\n p (f 3) (f 1) \u21a0\u27e8 stay \u27e9\n []\n\n red_nopath_one : Path (Red KT) (p (f 1) (f 1)) (p (f 0) (f 1))\n red_nopath_one = p (f 1) (f 1) \u21a0\u27e8 back \u27e9\n p (f 0) (f 1) \u21a0\u27e8 stay \u27e9\n []\n\n red_nopath_two : Path (Red KT) (p (f 1) (f 1)) (p (f 0) (f 1))\n red_nopath_two = p (f 1) (f 1) \u21a0\u27e8 back \u27e9 []\n","returncode":0,"stderr":"","license":"bsd-2-clause","lang":"Agda"} {"commit":"5d9abe59d5961777849cc3f8b64da8fe85ce608b","subject":"Logical: +\u27e6\u2605\u27e7\u2080, +\u27e6\u2192\u27e7\u21d4Preserves, +\u27e6\u2192\u27e7\u00b2\u21d4Preserves\u2082","message":"Logical: +\u27e6\u2605\u27e7\u2080, +\u27e6\u2192\u27e7\u21d4Preserves, +\u27e6\u2192\u27e7\u00b2\u21d4Preserves\u2082\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Relation\/Binary\/Logical.agda","new_file":"lib\/Relation\/Binary\/Logical.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Relation.Binary.Logical where\n\nopen import Type hiding (\u2605)\nopen import Level\nopen import Algebra.FunctionProperties\nopen import Data.Product\nopen import Data.Unit\nopen import Data.Empty\nopen import Relation.Nullary\nopen import Relation.Unary.NP hiding (Decidable)\nopen import Relation.Binary\n\n\u27e6\u2605\u27e7 : \u2200 {a\u2081 a\u2082} a\u1d63 (A\u2081 : \u2605 a\u2081) (A\u2082 : \u2605 a\u2082) \u2192 \u2605 _\n\u27e6\u2605\u27e7 a\u1d63 A\u2081 A\u2082 = A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63\n\n\u27e6\u2605\u27e7\u2080 : \u2200 {a\u2081 a\u2082} (A\u2081 : \u2605 a\u2081) (A\u2082 : \u2605 a\u2082) \u2192 \u2605 _\n\u27e6\u2605\u27e7\u2080 = \u27e6\u2605\u27e7 zero\n\n\u27e6\u2605\u2080\u27e7 : \u2200 (A\u2081 A\u2082 : \u2605\u2080) \u2192 \u2605\u2081\n\u27e6\u2605\u2080\u27e7 = \u27e6\u2605\u27e7\u2080\n\n\u27e6\u2605\u2081\u27e7 : \u2200 (A\u2081 A\u2082 : \u2605\u2081) \u2192 \u2605\u2082\n\u27e6\u2605\u2081\u27e7 = \u27e6\u2605\u27e7 (suc zero)\n\n-- old name\n\u27e6Set\u27e7 : \u2200 {a\u2081 a\u2082} a\u1d63 (A\u2081 : \u2605 a\u2081) (A\u2082 : \u2605 a\u2082) \u2192 \u2605 _\n\u27e6Set\u27e7 a\u1d63 A\u2081 A\u2082 = A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63\n\n-- old name\n\u27e6Set\u2080\u27e7 : \u2200 (A\u2081 A\u2082 : Set) \u2192 Set\u2081\n\u27e6Set\u2080\u27e7 = \u27e6\u2605\u2080\u27e7\n\n-- old name\n\u27e6Set\u2081\u27e7 : \u2200 (A\u2081 A\u2082 : Set\u2081) \u2192 Set\u2082\n\u27e6Set\u2081\u27e7 = \u27e6\u2605\u2081\u27e7\n\n\u27e6\u03a0\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : A\u2081 \u2192 \u2605 b\u2081} {B\u2082 : A\u2082 \u2192 \u2605 b\u2082} (B\u1d63 : \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u2081 x\u2081 \u2192 B\u2082 x\u2082 \u2192 \u2605 b\u1d63)\n (f\u2081 : (x : A\u2081) \u2192 B\u2081 x) (f\u2082 : (x : A\u2082) \u2192 B\u2082 x) \u2192 \u2605 _\n\u27e6\u03a0\u27e7 A\u1d63 B\u1d63 = \u03bb f\u2081 f\u2082 \u2192 \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u1d63 x\u1d63 (f\u2081 x\u2081) (f\u2082 x\u2082)\n\ninfixr 0 \u27e6\u03a0\u27e7\nsyntax \u27e6\u03a0\u27e7 A\u1d63 (\u03bb x\u1d63 \u2192 f) = \u27e8 x\u1d63 \u2236 A\u1d63 \u27e9\u27e6\u2192\u27e7 f\n\n\u27e6\u03a0\u27e7e : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : A\u2081 \u2192 \u2605 b\u2081} {B\u2082 : A\u2082 \u2192 \u2605 b\u2082} (B\u1d63 : \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u2081 x\u2081 \u2192 B\u2082 x\u2082 \u2192 \u2605 b\u1d63)\n (f\u2081 : (x : A\u2081) \u2192 B\u2081 x) (f\u2082 : (x : A\u2082) \u2192 B\u2082 x) \u2192 \u2605 _\n\u27e6\u03a0\u27e7e A\u1d63 B\u1d63 = \u03bb f\u2081 f\u2082 \u2192 \u2200 x\u2081 x\u2082 (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u1d63 x\u1d63 (f\u2081 x\u2081) (f\u2082 x\u2082)\n\n\u27e6\u2200\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : A\u2081 \u2192 \u2605 b\u2081} {B\u2082 : A\u2082 \u2192 \u2605 b\u2082} (B\u1d63 : \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u2081 x\u2081 \u2192 B\u2082 x\u2082 \u2192 \u2605 b\u1d63)\n (f\u2081 : {x : A\u2081} \u2192 B\u2081 x) (f\u2082 : {x : A\u2082} \u2192 B\u2082 x) \u2192 \u2605 _\n\u27e6\u2200\u27e7 A\u1d63 B\u1d63 = \u03bb f\u2081 f\u2082 \u2192 \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u1d63 x\u1d63 (f\u2081 {x\u2081}) (f\u2082 {x\u2082})\n\ninfixr 0 \u27e6\u2200\u27e7\nsyntax \u27e6\u2200\u27e7 A\u1d63 (\u03bb x\u1d63 \u2192 f) = \u2200\u27e8 x\u1d63 \u2236 A\u1d63 \u27e9\u27e6\u2192\u27e7 f\n\ninfixr 1 _\u27e6\u2192\u27e7_\n_\u27e6\u2192\u27e7_ : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 \u2605 b\u1d63)\n (f\u2081 : A\u2081 \u2192 B\u2081) (f\u2082 : A\u2082 \u2192 B\u2082) \u2192 \u2605 _\nA\u1d63 \u27e6\u2192\u27e7 B\u1d63 = \u27e6\u03a0\u27e7 A\u1d63 (\u03bb _ \u2192 B\u1d63)\n\ninfixr 0 _\u27e6\u2192\u27e7e_\n_\u27e6\u2192\u27e7e_ : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 \u2605 b\u1d63)\n (f\u2081 : A\u2081 \u2192 B\u2081) (f\u2082 : A\u2082 \u2192 B\u2082) \u2192 \u2605 _\n_\u27e6\u2192\u27e7e_ A\u1d63 B\u1d63 = \u27e6\u03a0\u27e7e A\u1d63 (\u03bb _ \u2192 B\u1d63)\n\nrecord \u27e6\u22a4\u27e7 (x\u2081 x\u2082 : \u22a4) : \u2605\u2080 where\n constructor \u27e6tt\u27e7\n\ndata \u27e6\u22a5\u27e7 (x\u2081 x\u2082 : \u22a5) : \u2605\u2080 where\n\ninfix 3 \u27e6\u00ac\u27e7_\n\n\u27e6\u00ac\u27e7_ : \u2200 {a\u2081 a\u2082 a\u209a} \u2192 (\u27e6\u2605\u27e7 {a\u2081} {a\u2082} a\u209a \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 _) \u00ac_ \u00ac_\n\u27e6\u00ac\u27e7 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 \u27e6\u22a5\u27e7\n\n-- Products \u27e6\u03a3\u27e7, \u27e6\u2203\u27e7, \u27e6\u00d7\u27e7 are in Data.Product.NP\n\n\u27e6Pred\u27e7 : \u2200 {p\u2081 p\u2082} p\u1d63 {a\u2081 a\u2082 a\u1d63} \u2192 (\u27e6\u2605\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 _) (Pred p\u2081) (Pred p\u2082)\n\u27e6Pred\u27e7 p\u1d63 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 p\u1d63\n\nprivate\n REL\u2032 : \u2200 \u2113 {a b} \u2192 \u2605 a \u2192 \u2605 b \u2192 \u2605 (a \u2294 b \u2294 suc \u2113)\n REL\u2032 \u2113 A B = A \u2192 B \u2192 \u2605 \u2113\n\n \u27e6REL\u27e7\u2032 : \u2200 {a\u2081 a\u2082 a\u1d63 b\u2081 b\u2082 b\u1d63 r\u2081 r\u2082} r\u1d63 \u2192\n (\u27e6\u2605\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 {b\u2081} {b\u2082} b\u1d63 \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 _) (REL\u2032 r\u2081) (REL\u2032 r\u2082)\n \u27e6REL\u27e7\u2032 r\u1d63 A\u1d63 B\u1d63 = A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 (\u27e6\u2605\u27e7 r\u1d63)\n\n\u27e6REL\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 \u2605 b\u1d63)\n {r\u2081 r\u2082} r\u1d63 (\u223c\u2081 : REL A\u2081 B\u2081 r\u2081) (\u223c\u2082 : REL A\u2082 B\u2082 r\u2082) \u2192 \u2605 _\n\u27e6REL\u27e7 A\u1d63 B\u1d63 r\u1d63 = A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 (\u27e6\u2605\u27e7 r\u1d63)\n\n\u27e6Rel\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {r\u2081 r\u2082} \u2113\u1d63 (\u223c\u2081 : Rel A\u2081 r\u2081) (\u223c\u2082 : Rel A\u2082 r\u2082) \u2192 \u2605 _\n\u27e6Rel\u27e7 A\u1d63 r\u1d63 = \u27e6REL\u27e7 A\u1d63 A\u1d63 r\u1d63\n\ndata \u27e6Dec\u27e7 {\u2113\u2081 \u2113\u2082 \u2113\u1d63} {P\u2081 : \u2605 \u2113\u2081} {P\u2082 : \u2605 \u2113\u2082} (P\u1d63 : P\u2081 \u2192 P\u2082 \u2192 \u2605 \u2113\u1d63) : \u27e6\u2605\u27e7 (\u2113\u2081 \u2294 \u2113\u2082 \u2294 \u2113\u1d63) (Dec P\u2081) (Dec P\u2082) where\n yes : {p\u2081 : P\u2081} {p\u2082 : P\u2082} (p\u1d63 : P\u1d63 p\u2081 p\u2082) \u2192 \u27e6Dec\u27e7 P\u1d63 (yes p\u2081) (yes p\u2082)\n no : {\u00acp\u2081 : \u00ac P\u2081} {\u00acp\u2082 : \u00ac P\u2082} (\u00acp\u1d63 : (\u27e6\u00ac\u27e7 P\u1d63) \u00acp\u2081 \u00acp\u2082) \u2192 \u27e6Dec\u27e7 P\u1d63 (no \u00acp\u2081) (no \u00acp\u2082)\n\nprivate\n \u27e6Dec\u27e7' : \u2200 {p\u2081} {p\u2082} {p\u1d63} \u2192 \u27e6Pred\u27e7 {p\u2081} {p\u2082} _ (\u27e6\u2605\u27e7 p\u1d63) Dec Dec\n \u27e6Dec\u27e7' = \u27e6Dec\u27e7\n\n\u27e6Decidable\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63 b\u2081 b\u2082 b\u1d63 \u2113\u2081 \u2113\u2082 \u2113\u1d63}\n \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6\u2605\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e9\u27e6\u2192\u27e7\n \u2200\u27e8 B\u1d63 \u2236 \u27e6\u2605\u27e7 {b\u2081} {b\u2082} b\u1d63 \u27e9\u27e6\u2192\u27e7\n \u27e6REL\u27e7 A\u1d63 B\u1d63 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 _) Decidable Decidable\n\u27e6Decidable\u27e7 A\u1d63 B\u1d63 _\u223c\u1d63_ = \u27e8 x\u1d63 \u2236 A\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e8 y\u1d63 \u2236 B\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6Dec\u27e7 (x\u1d63 \u223c\u1d63 y\u1d63)\n\n\u27e6Op\u2081\u27e7 : \u2200 {a} \u2192 (\u27e6\u2605\u27e7 {a} {a} a \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 a) Op\u2081 Op\u2081\n\u27e6Op\u2081\u27e7 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 A\u1d63\n\n\u27e6Op\u2082\u27e7 : \u2200 {a} \u2192 (\u27e6\u2605\u27e7 {a} {a} a \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 a) Op\u2082 Op\u2082\n\u27e6Op\u2082\u27e7 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63\n\nopen import Function.Equivalence\nprivate\n \u27e6\u2192\u27e7\u21d4Preserves :\n \u2200 {a b a\u1d63 b\u1d63}\n {A : \u2605 a} {B : \u2605 b}\n {A\u1d63 : \u27e6\u2605\u27e7 a\u1d63 A A}\n {B\u1d63 : \u27e6\u2605\u27e7 b\u1d63 B B}\n {f}\n \u2192 (A\u1d63 \u27e6\u2192\u27e7 B\u1d63) f f \u21d4 f Preserves A\u1d63 \u27f6 B\u1d63\n\n \u27e6\u2192\u27e7\u21d4Preserves = equivalence (\u03bb x \u2192 x) (\u03bb x \u2192 x)\n\n \u27e6\u2192\u27e7\u00b2\u21d4Preserves\u2082 :\n \u2200 {a b c a\u1d63 b\u1d63 c\u1d63}\n {A : \u2605 a} {B : \u2605 b} {C : \u2605 c}\n {A\u1d63 : \u27e6\u2605\u27e7 a\u1d63 A A}\n {B\u1d63 : \u27e6\u2605\u27e7 b\u1d63 B B}\n {C\u1d63 : \u27e6\u2605\u27e7 c\u1d63 C C}\n {f}\n \u2192 (A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 C\u1d63) f f \u21d4 f Preserves\u2082 A\u1d63 \u27f6 B\u1d63 \u27f6 C\u1d63\n\n \u27e6\u2192\u27e7\u00b2\u21d4Preserves\u2082 = equivalence (\u03bb f {x} {y} {_} {_} z \u2192 f {x} {y} z)\n (\u03bb f {x} {y} z {_} {_} \u2192 f z)\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Relation.Binary.Logical where\n\nopen import Type hiding (\u2605)\nopen import Level\nopen import Algebra.FunctionProperties\nopen import Data.Product\nopen import Data.Unit\nopen import Data.Empty\nopen import Relation.Nullary\nopen import Relation.Unary.NP hiding (Decidable)\nopen import Relation.Binary\n\n\u27e6\u2605\u27e7 : \u2200 {a\u2081 a\u2082} a\u1d63 (A\u2081 : \u2605 a\u2081) (A\u2082 : \u2605 a\u2082) \u2192 \u2605 _\n\u27e6\u2605\u27e7 a\u1d63 A\u2081 A\u2082 = A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63\n\n\u27e6\u2605\u2080\u27e7 : \u2200 (A\u2081 A\u2082 : \u2605\u2080) \u2192 \u2605\u2081\n\u27e6\u2605\u2080\u27e7 = \u27e6\u2605\u27e7 zero\n\n\u27e6\u2605\u2081\u27e7 : \u2200 (A\u2081 A\u2082 : \u2605\u2081) \u2192 \u2605\u2082\n\u27e6\u2605\u2081\u27e7 = \u27e6\u2605\u27e7 (suc zero)\n\n-- old name\n\u27e6Set\u27e7 : \u2200 {a\u2081 a\u2082} a\u1d63 (A\u2081 : \u2605 a\u2081) (A\u2082 : \u2605 a\u2082) \u2192 \u2605 _\n\u27e6Set\u27e7 a\u1d63 A\u2081 A\u2082 = A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63\n\n-- old name\n\u27e6Set\u2080\u27e7 : \u2200 (A\u2081 A\u2082 : Set) \u2192 Set\u2081\n\u27e6Set\u2080\u27e7 = \u27e6\u2605\u2080\u27e7\n\n-- old name\n\u27e6Set\u2081\u27e7 : \u2200 (A\u2081 A\u2082 : Set\u2081) \u2192 Set\u2082\n\u27e6Set\u2081\u27e7 = \u27e6\u2605\u2081\u27e7\n\n\u27e6\u03a0\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : A\u2081 \u2192 \u2605 b\u2081} {B\u2082 : A\u2082 \u2192 \u2605 b\u2082} (B\u1d63 : \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u2081 x\u2081 \u2192 B\u2082 x\u2082 \u2192 \u2605 b\u1d63)\n (f\u2081 : (x : A\u2081) \u2192 B\u2081 x) (f\u2082 : (x : A\u2082) \u2192 B\u2082 x) \u2192 \u2605 _\n\u27e6\u03a0\u27e7 A\u1d63 B\u1d63 = \u03bb f\u2081 f\u2082 \u2192 \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u1d63 x\u1d63 (f\u2081 x\u2081) (f\u2082 x\u2082)\n\ninfixr 0 \u27e6\u03a0\u27e7\nsyntax \u27e6\u03a0\u27e7 A\u1d63 (\u03bb x\u1d63 \u2192 f) = \u27e8 x\u1d63 \u2236 A\u1d63 \u27e9\u27e6\u2192\u27e7 f\n\n\u27e6\u03a0\u27e7e : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : A\u2081 \u2192 \u2605 b\u2081} {B\u2082 : A\u2082 \u2192 \u2605 b\u2082} (B\u1d63 : \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u2081 x\u2081 \u2192 B\u2082 x\u2082 \u2192 \u2605 b\u1d63)\n (f\u2081 : (x : A\u2081) \u2192 B\u2081 x) (f\u2082 : (x : A\u2082) \u2192 B\u2082 x) \u2192 \u2605 _\n\u27e6\u03a0\u27e7e A\u1d63 B\u1d63 = \u03bb f\u2081 f\u2082 \u2192 \u2200 x\u2081 x\u2082 (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u1d63 x\u1d63 (f\u2081 x\u2081) (f\u2082 x\u2082)\n\n\u27e6\u2200\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : A\u2081 \u2192 \u2605 b\u2081} {B\u2082 : A\u2082 \u2192 \u2605 b\u2082} (B\u1d63 : \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u2081 x\u2081 \u2192 B\u2082 x\u2082 \u2192 \u2605 b\u1d63)\n (f\u2081 : {x : A\u2081} \u2192 B\u2081 x) (f\u2082 : {x : A\u2082} \u2192 B\u2082 x) \u2192 \u2605 _\n\u27e6\u2200\u27e7 A\u1d63 B\u1d63 = \u03bb f\u2081 f\u2082 \u2192 \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u1d63 x\u1d63 (f\u2081 {x\u2081}) (f\u2082 {x\u2082})\n\ninfixr 0 \u27e6\u2200\u27e7\nsyntax \u27e6\u2200\u27e7 A\u1d63 (\u03bb x\u1d63 \u2192 f) = \u2200\u27e8 x\u1d63 \u2236 A\u1d63 \u27e9\u27e6\u2192\u27e7 f\n\ninfixr 1 _\u27e6\u2192\u27e7_\n_\u27e6\u2192\u27e7_ : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 \u2605 b\u1d63)\n (f\u2081 : A\u2081 \u2192 B\u2081) (f\u2082 : A\u2082 \u2192 B\u2082) \u2192 \u2605 _\nA\u1d63 \u27e6\u2192\u27e7 B\u1d63 = \u27e6\u03a0\u27e7 A\u1d63 (\u03bb _ \u2192 B\u1d63)\n\ninfixr 0 _\u27e6\u2192\u27e7e_\n_\u27e6\u2192\u27e7e_ : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 \u2605 b\u1d63)\n (f\u2081 : A\u2081 \u2192 B\u2081) (f\u2082 : A\u2082 \u2192 B\u2082) \u2192 \u2605 _\n_\u27e6\u2192\u27e7e_ A\u1d63 B\u1d63 = \u27e6\u03a0\u27e7e A\u1d63 (\u03bb _ \u2192 B\u1d63)\n\nrecord \u27e6\u22a4\u27e7 (x\u2081 x\u2082 : \u22a4) : \u2605\u2080 where\n constructor \u27e6tt\u27e7\n\ndata \u27e6\u22a5\u27e7 (x\u2081 x\u2082 : \u22a5) : \u2605\u2080 where\n\ninfix 3 \u27e6\u00ac\u27e7_\n\n\u27e6\u00ac\u27e7_ : \u2200 {a\u2081 a\u2082 a\u209a} \u2192 (\u27e6\u2605\u27e7 {a\u2081} {a\u2082} a\u209a \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 _) \u00ac_ \u00ac_\n\u27e6\u00ac\u27e7 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 \u27e6\u22a5\u27e7\n\n-- Products \u27e6\u03a3\u27e7, \u27e6\u2203\u27e7, \u27e6\u00d7\u27e7 are in Data.Product.NP\n\n\u27e6Pred\u27e7 : \u2200 {p\u2081 p\u2082} p\u1d63 {a\u2081 a\u2082 a\u1d63} \u2192 (\u27e6\u2605\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 _) (Pred p\u2081) (Pred p\u2082)\n\u27e6Pred\u27e7 p\u1d63 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 p\u1d63\n\nprivate\n REL\u2032 : \u2200 \u2113 {a b} \u2192 \u2605 a \u2192 \u2605 b \u2192 \u2605 (a \u2294 b \u2294 suc \u2113)\n REL\u2032 \u2113 A B = A \u2192 B \u2192 \u2605 \u2113\n\n \u27e6REL\u27e7\u2032 : \u2200 {a\u2081 a\u2082 a\u1d63 b\u2081 b\u2082 b\u1d63 r\u2081 r\u2082} r\u1d63 \u2192\n (\u27e6\u2605\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 {b\u2081} {b\u2082} b\u1d63 \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 _) (REL\u2032 r\u2081) (REL\u2032 r\u2082)\n \u27e6REL\u27e7\u2032 r\u1d63 A\u1d63 B\u1d63 = A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 (\u27e6\u2605\u27e7 r\u1d63)\n\n\u27e6REL\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 \u2605 b\u1d63)\n {r\u2081 r\u2082} r\u1d63 (\u223c\u2081 : REL A\u2081 B\u2081 r\u2081) (\u223c\u2082 : REL A\u2082 B\u2082 r\u2082) \u2192 \u2605 _\n\u27e6REL\u27e7 A\u1d63 B\u1d63 r\u1d63 = A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 (\u27e6\u2605\u27e7 r\u1d63)\n\n\u27e6Rel\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {r\u2081 r\u2082} \u2113\u1d63 (\u223c\u2081 : Rel A\u2081 r\u2081) (\u223c\u2082 : Rel A\u2082 r\u2082) \u2192 \u2605 _\n\u27e6Rel\u27e7 A\u1d63 r\u1d63 = \u27e6REL\u27e7 A\u1d63 A\u1d63 r\u1d63\n\ndata \u27e6Dec\u27e7 {\u2113\u2081 \u2113\u2082 \u2113\u1d63} {P\u2081 : \u2605 \u2113\u2081} {P\u2082 : \u2605 \u2113\u2082} (P\u1d63 : P\u2081 \u2192 P\u2082 \u2192 \u2605 \u2113\u1d63) : \u27e6\u2605\u27e7 (\u2113\u2081 \u2294 \u2113\u2082 \u2294 \u2113\u1d63) (Dec P\u2081) (Dec P\u2082) where\n yes : {p\u2081 : P\u2081} {p\u2082 : P\u2082} (p\u1d63 : P\u1d63 p\u2081 p\u2082) \u2192 \u27e6Dec\u27e7 P\u1d63 (yes p\u2081) (yes p\u2082)\n no : {\u00acp\u2081 : \u00ac P\u2081} {\u00acp\u2082 : \u00ac P\u2082} (\u00acp\u1d63 : (\u27e6\u00ac\u27e7 P\u1d63) \u00acp\u2081 \u00acp\u2082) \u2192 \u27e6Dec\u27e7 P\u1d63 (no \u00acp\u2081) (no \u00acp\u2082)\n\nprivate\n \u27e6Dec\u27e7' : \u2200 {p\u2081} {p\u2082} {p\u1d63} \u2192 \u27e6Pred\u27e7 {p\u2081} {p\u2082} _ (\u27e6\u2605\u27e7 p\u1d63) Dec Dec\n \u27e6Dec\u27e7' = \u27e6Dec\u27e7\n\n\u27e6Decidable\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63 b\u2081 b\u2082 b\u1d63 \u2113\u2081 \u2113\u2082 \u2113\u1d63}\n \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6\u2605\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e9\u27e6\u2192\u27e7\n \u2200\u27e8 B\u1d63 \u2236 \u27e6\u2605\u27e7 {b\u2081} {b\u2082} b\u1d63 \u27e9\u27e6\u2192\u27e7\n \u27e6REL\u27e7 A\u1d63 B\u1d63 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 _) Decidable Decidable\n\u27e6Decidable\u27e7 A\u1d63 B\u1d63 _\u223c\u1d63_ = \u27e8 x\u1d63 \u2236 A\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e8 y\u1d63 \u2236 B\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6Dec\u27e7 (x\u1d63 \u223c\u1d63 y\u1d63)\n\n\u27e6Op\u2081\u27e7 : \u2200 {a} \u2192 (\u27e6\u2605\u27e7 {a} {a} a \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 a) Op\u2081 Op\u2081\n\u27e6Op\u2081\u27e7 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 A\u1d63\n\n\u27e6Op\u2082\u27e7 : \u2200 {a} \u2192 (\u27e6\u2605\u27e7 {a} {a} a \u27e6\u2192\u27e7 \u27e6\u2605\u27e7 a) Op\u2082 Op\u2082\n\u27e6Op\u2082\u27e7 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"5a4ce333657f637b5ecb5b95c970bcacc6ba9988","subject":"Nand.Properties: renaming","message":"Nand.Properties: renaming\n","repos":"crypto-agda\/crypto-agda","old_file":"FunUniverse\/Nand\/Properties.agda","new_file":"FunUniverse\/Nand\/Properties.agda","new_contents":"open import Level.NP\nopen import Type\nopen import Data.Two\nopen import Data.Product\nopen import Function.NP\nopen import Relation.Binary\nopen import Relation.Nullary.Decidable\nopen import Relation.Binary.PropositionalEquality\n\nopen import Explore.Type\nimport Explore.Explorable\nopen import Explore.Universe\n\nopen import FunUniverse.Nand\nopen import FunUniverse.Agda\n\nmodule FunUniverse.Nand.Properties where\n\nmodule Test {B : \u2605} (_\u225f_ : Decidable {A = B} _\u2261_)\n (A : Ty)\n {f g : El A \u2192 B} where\n module _ {\u2113} where\n A\u1d49 : Explore \u2113 (El A)\n A\u1d49 = exploreU A\n A\u2071 : ExploreInd \u2113 A\u1d49\n A\u2071 = exploreU-ind A\n A\u02e1 : Lookup {\u2080} A\u1d49\n A\u02e1 = lookupU A\n\n Check! = A\u1d49 _\u00d7_ \u03bb x \u2192 \u2713 \u230a f x \u225f g x \u230b\n\n check! : {p\u2713 : Check!} \u2192 f \u2257 g\n check! {p\u2713} x = toWitness (A\u02e1 p\u2713 x)\n\n {- Unused\n open Explore.Explorable.Explorable\u2080 A\u2071\n test-\u2227 = big-\u2227 \u03bb x \u2192 \u230a f x \u225f g x \u230b\n -}\n\nmodule Test22 where\n nand nand' : \ud835\udfda \u00d7 \ud835\udfda \u2192 \ud835\udfda\n\n nand (x , y) = not (x \u2227 y)\n nand' (x , y) = not x \u2228 not y\n\n module N = FromNand funRewiring nand\n module T = Test Data.Two._\u225f_\n\n module UnOp where\n open T \ud835\udfda\u2032\n\n not-ok : N.not \u2257 not\n not-ok = check!\n\n module BinOp where\n open T (\ud835\udfda\u2032 \u00d7\u2032 \ud835\udfda\u2032)\n\n nand-ok : nand \u2257 nand'\n nand-ok = check!\n\n and-ok : N.and \u2257 uncurry _\u2227_\n and-ok = check!\n\n or-ok : N.or \u2257 uncurry _\u2228_\n or-ok = check!\n\n nor-ok : N.nor \u2257 (not \u2218 uncurry _\u2228_)\n nor-ok = check!\n\n xor-ok : N.xor \u2257 uncurry _xor_\n xor-ok = check!\n\n xnor-ok : N.xnor \u2257 uncurry _==_\n xnor-ok = check!\n\n module TriOp where\n open T (\ud835\udfda\u2032 \u00d7\u2032 (\ud835\udfda\u2032 \u00d7\u2032 \ud835\udfda\u2032))\n\n fork : \ud835\udfda \u00d7 \ud835\udfda \u00d7 \ud835\udfda \u2192 \ud835\udfda\n fork (c , e\u1d62) = proj e\u1d62 c\n\n fork-ok : N.fork \u2257 fork\n fork-ok = check!\n","old_contents":"open import Level.NP\nopen import Type\nopen import Data.Two\nopen import Data.Product\nopen import Function.NP\nopen import Relation.Binary\nopen import Relation.Nullary.Decidable\nopen import Relation.Binary.PropositionalEquality\n\nopen import Explore.Type\nimport Explore.Explorable\nopen import Explore.Universe\n\nopen import FunUniverse.Nand\nopen import FunUniverse.Agda\n\nmodule FunUniverse.Nand.Properties where\n\nmodule Test {B : \u2605} (_\u225f_ : Decidable {A = B} _\u2261_)\n (A : Ty)\n {f g : El A \u2192 B} where\n module _ {\u2113} where\n A\u1d49 : Explore \u2113 (El A)\n A\u1d49 = exploreU A\n A\u2071 : ExploreInd \u2113 A\u1d49\n A\u2071 = exploreU-ind A\n A\u02e1 : Lookup {\u2080} A\u1d49\n A\u02e1 = lookupU A\n\n Check! = A\u1d49 _\u00d7_ \u03bb x \u2192 \u2713 \u230a f x \u225f g x \u230b\n\n check! : {p\u2713 : Check!} \u2192 f \u2257 g\n check! {p\u2713} x = toWitness (A\u02e1 p\u2713 x)\n\n {- Unused\n open Explore.Explorable.Explorable\u2080 A\u2071\n test-\u2227 = big-\u2227 \u03bb x \u2192 \u230a f x \u225f g x \u230b\n -}\n\nmodule Test22 where\n nand nand' : \ud835\udfda \u00d7 \ud835\udfda \u2192 \ud835\udfda\n\n nand (x , y) = not (x \u2227 y)\n nand' (x , y) = not x \u2228 not y\n\n module N = FromNand funRewiring nand\n module T = Test Data.Two._\u225f_\n\n module UnOp where\n open T \ud835\udfda\u2032\n\n pf-not : N.not \u2257 not\n pf-not = check! \n\n module BinOp where\n open T (\ud835\udfda\u2032 \u00d7\u2032 \ud835\udfda\u2032)\n\n pf-nand : nand \u2257 nand'\n pf-nand = check!\n\n pf-and : N.and \u2257 uncurry _\u2227_\n pf-and = check!\n\n pf-or : N.or \u2257 uncurry _\u2228_\n pf-or = check!\n\n pf-nor : N.nor \u2257 (not \u2218 uncurry _\u2228_)\n pf-nor = check!\n\n pf-xor : N.xor \u2257 uncurry _xor_\n pf-xor = check!\n\n pf-xnor : N.xnor \u2257 uncurry _==_\n pf-xnor = check!\n\n module TriOp where\n open T (\ud835\udfda\u2032 \u00d7\u2032 (\ud835\udfda\u2032 \u00d7\u2032 \ud835\udfda\u2032))\n\n fork : \ud835\udfda \u00d7 \ud835\udfda \u00d7 \ud835\udfda \u2192 \ud835\udfda\n fork (c , e\u1d62) = proj e\u1d62 c\n\n pf-fork : N.fork \u2257 fork\n pf-fork = check!\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"a18ee58c296f46e92e3f6f577648c5b871e3b128","subject":"Added a proof using an instance of an induction principle.","message":"Added a proof using an instance of an induction principle.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/FOT\/FOTC\/Data\/Nat\/AddTotality.agda","new_file":"notes\/FOT\/FOTC\/Data\/Nat\/AddTotality.agda","new_contents":"------------------------------------------------------------------------------\n-- Totality of natural numbers addition\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Data.Nat.AddTotality where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n-- Interactive proof using the induction principle for natural numbers.\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = N-ind A A0 is Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n A0 : A zero\n A0 = subst N (sym (+-0x n)) Nn\n\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} ih = subst N (sym (+-Sx i n)) (nsucc ih)\n\n-- Combined proof using an instance of the induction principle.\n+-N-ind : \u2200 {n} \u2192\n N (zero + n) \u2192\n (\u2200 {m} \u2192 N (m + n) \u2192 N (succ\u2081 m + n)) \u2192\n \u2200 {m} \u2192 N m \u2192 N (m + n)\n+-N-ind {n} = N-ind (\u03bb i \u2192 N (i + n))\n\n+-N\u2081 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N\u2081 {n = n} Nm Nn = +-N-ind prf\u2081 prf\u2082 Nm\n where\n prf\u2081 : N (zero + n)\n prf\u2081 = subst N (sym (+-0x n)) Nn\n\n prf\u2082 : \u2200 {m} \u2192 N (m + n) \u2192 N (succ\u2081 m + n)\n prf\u2082 {m} ih = subst N (sym (+-Sx m n)) (nsucc ih)\n\npostulate +-N\u2082 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n{-# ATP prove +-N\u2082 +-N-ind #-}\n\n-- Combined proof using the induction principle.\n\n-- The translation is\n-- \u2200 p. app\u2081(p,zero) \u2192\n-- (\u2200 x. app\u2081(n,x) \u2192 app\u2081(p,x) \u2192 app\u2081(p,appFn(succ,x))) \u2192 -- indN\n-- (\u2200 x. app\u2081(n,x) \u2192 app\u2081(p,x))\n----------------------------------------------------------------\n-- \u2200 x y. app\u2081(n,x) \u2192 app\u2081(n,y) \u2192 app\u2081(n,appFn(appFn(+,x),y)) -- +-N\u2082\n\npostulate +-N\u2083 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n-- The ATPs could not prove this postulate.\n-- {-# ATP prove +-N\u2083 N-ind #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Totality of natural numbers addition\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Data.Nat.AddTotality where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n-- Interactive proof using the induction principle for natural numbers.\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = N-ind A A0 is Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n A0 : A zero\n A0 = subst N (sym (+-0x n)) Nn\n\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} ih = subst N (sym (+-Sx i n)) (nsucc ih)\n\n-- Combined proof using an instance of the induction principle.\n+-N-ind : \u2200 n \u2192\n N (zero + n) \u2192\n (\u2200 {m} \u2192 N (m + n) \u2192 N (succ\u2081 m + n)) \u2192\n \u2200 {m} \u2192 N m \u2192 N (m + n)\n+-N-ind n = N-ind (\u03bb i \u2192 N (i + n))\n\npostulate +-N\u2081 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n{-# ATP prove +-N\u2081 +-N-ind #-}\n\n-- Combined proof using the induction principle.\n\n-- The translation is\n-- \u2200 p. app\u2081(p,zero) \u2192\n-- (\u2200 x. app\u2081(n,x) \u2192 app\u2081(p,x) \u2192 app\u2081(p,appFn(succ,x))) \u2192 -- indN\n-- (\u2200 x. app\u2081(n,x) \u2192 app\u2081(p,x))\n----------------------------------------------------------------\n-- \u2200 x y. app\u2081(n,x) \u2192 app\u2081(n,y) \u2192 app\u2081(n,appFn(appFn(+,x),y)) -- +-N\u2082\n\npostulate +-N\u2082 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n-- The ATPs could not prove this postulate.\n-- {-# ATP prove +-N\u2082 N-ind #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"aa15436d10416e76d1a5f6494f6bf9dfde53c404","subject":"Drop unused redundant case","message":"Drop unused redundant case\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/SIRelBigStep\/DSyntax.agda","new_file":"Thesis\/SIRelBigStep\/DSyntax.agda","new_contents":"module Thesis.SIRelBigStep.DSyntax where\n\nopen import Thesis.SIRelBigStep.Syntax public\n\n-- data DType : Set where\n-- _\u21d2_ : (\u03c3 \u03c4 : DType) \u2192 DType\n-- int : DType\nDType = Type\n\nimport Base.Syntax.Context\nmodule DC = Base.Syntax.Context DType\n\n\u0394\u03c4 : Type \u2192 DType\n\u0394\u03c4 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394\u03c4 \u03c3 \u21d2 \u0394\u03c4 \u03c4\n\u0394\u03c4 (pair \u03c41 \u03c42) = pair (\u0394\u03c4 \u03c41) (\u0394\u03c4 \u03c42)\n\u0394\u03c4 nat = nat\n\n\u0394\u0394 : Context \u2192 DC.Context\n\u0394\u0394 \u2205 = \u2205\n\u0394\u0394 (\u03c4 \u2022 \u0393) = \u0394\u03c4 \u03c4 \u2022 \u0394\u0394 \u0393\n--\u0394\u0394 \u0393 = \u0393\n\n-- A DTerm evaluates in normal context \u0394, change context (\u0394\u0394 \u0394), and produces\n-- a result of type (\u0394t \u03c4).\ndata DTerm (\u0394 : Context) (\u03c4 : DType) : Set\ndata DSVal (\u0394 : Context) : (\u03c4 : DType) \u2192 Set where\n dvar : \u2200 {\u03c4} \u2192\n (x : Var \u0394 \u03c4) \u2192\n DSVal \u0394 \u03c4\n dabs : \u2200 {\u03c3 \u03c4}\n (dt : DTerm (\u03c3 \u2022 \u0394) \u03c4) \u2192\n DSVal \u0394 (\u03c3 \u21d2 \u03c4)\n dcons : \u2200 {\u03c41 \u03c42}\n (dsv1 : DSVal \u0394 \u03c41)\n (dsv2 : DSVal \u0394 \u03c42) \u2192\n DSVal \u0394 (pair \u03c41 \u03c42)\n dconst : \u2200 {\u03c4} \u2192 (dc : Const (\u0394\u03c4 \u03c4)) \u2192 DSVal \u0394 \u03c4\n\ndata DTerm (\u0394 : Context) (\u03c4 : DType) where\n dval :\n DSVal \u0394 \u03c4 \u2192\n DTerm \u0394 \u03c4\n dprimapp : \u2200 {\u03c3}\n (p : Primitive (\u03c3 \u21d2 \u03c4)) \u2192\n (sv : SVal \u0394 \u03c3) \u2192\n (dsv : DSVal \u0394 \u03c3) \u2192\n DTerm \u0394 \u03c4\n dapp : \u2200 {\u03c3}\n (dvs : DSVal \u0394 (\u03c3 \u21d2 \u03c4)) \u2192\n (vt : SVal \u0394 \u03c3) \u2192\n (dvt : DSVal \u0394 \u03c3) \u2192\n DTerm \u0394 \u03c4\n dlett : \u2200 {\u03c3}\n (s : Term \u0394 \u03c3) \u2192\n (ds : DTerm \u0394 \u03c3) \u2192\n (dt : DTerm (\u03c3 \u2022 \u0394) \u03c4) \u2192\n DTerm \u0394 \u03c4\n","old_contents":"module Thesis.SIRelBigStep.DSyntax where\n\nopen import Thesis.SIRelBigStep.Syntax public\n\n-- data DType : Set where\n-- _\u21d2_ : (\u03c3 \u03c4 : DType) \u2192 DType\n-- int : DType\nDType = Type\n\nimport Base.Syntax.Context\nmodule DC = Base.Syntax.Context DType\n\n\u0394\u03c4 : Type \u2192 DType\n\u0394\u03c4 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394\u03c4 \u03c3 \u21d2 \u0394\u03c4 \u03c4\n\u0394\u03c4 (pair \u03c41 \u03c42) = pair (\u0394\u03c4 \u03c41) (\u0394\u03c4 \u03c42)\n\u0394\u03c4 nat = nat\n\n\u0394\u0394 : Context \u2192 DC.Context\n\u0394\u0394 \u2205 = \u2205\n\u0394\u0394 (\u03c4 \u2022 \u0393) = \u0394\u03c4 \u03c4 \u2022 \u0394\u0394 \u0393\n--\u0394\u0394 \u0393 = \u0393\n\n-- A DTerm evaluates in normal context \u0394, change context (\u0394\u0394 \u0394), and produces\n-- a result of type (\u0394t \u03c4).\ndata DTerm (\u0394 : Context) (\u03c4 : DType) : Set\ndata DSVal (\u0394 : Context) : (\u03c4 : DType) \u2192 Set where\n dvar : \u2200 {\u03c4} \u2192\n (x : Var \u0394 \u03c4) \u2192\n DSVal \u0394 \u03c4\n dabs : \u2200 {\u03c3 \u03c4}\n (dt : DTerm (\u03c3 \u2022 \u0394) \u03c4) \u2192\n DSVal \u0394 (\u03c3 \u21d2 \u03c4)\n dcons : \u2200 {\u03c41 \u03c42}\n (dsv1 : DSVal \u0394 \u03c41)\n (dsv2 : DSVal \u0394 \u03c42) \u2192\n DSVal \u0394 (pair \u03c41 \u03c42)\n dconst : \u2200 {\u03c4} \u2192 (dc : Const (\u0394\u03c4 \u03c4)) \u2192 DSVal \u0394 \u03c4\n\ndata DTerm (\u0394 : Context) (\u03c4 : DType) where\n dval :\n DSVal \u0394 \u03c4 \u2192\n DTerm \u0394 \u03c4\n dprimapp : \u2200 {\u03c3}\n (p : Primitive (\u03c3 \u21d2 \u03c4)) \u2192\n (sv : SVal \u0394 \u03c3) \u2192\n (dsv : DSVal \u0394 \u03c3) \u2192\n DTerm \u0394 \u03c4\n dconst :\n (c : Const \u03c4) \u2192\n DTerm \u0394 \u03c4\n dapp : \u2200 {\u03c3}\n (dvs : DSVal \u0394 (\u03c3 \u21d2 \u03c4)) \u2192\n (vt : SVal \u0394 \u03c3) \u2192\n (dvt : DSVal \u0394 \u03c3) \u2192\n DTerm \u0394 \u03c4\n dlett : \u2200 {\u03c3}\n (s : Term \u0394 \u03c3) \u2192\n (ds : DTerm \u0394 \u03c3) \u2192\n (dt : DTerm (\u03c3 \u2022 \u0394) \u03c4) \u2192\n DTerm \u0394 \u03c4\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7ca8c4411d108521cba992c9c85cd4eb2fdff0ed","subject":"Let the code speak for itself","message":"Let the code speak for itself\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/Examples\/PropositionalDesc.agda","new_file":"formalization\/agda\/Spire\/Examples\/PropositionalDesc.agda","new_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nmodule Spire.Examples.PropositionalDesc where\n\n----------------------------------------------------------------------\n\nelimEq : (A : Set) (x : A) (P : (y : A) \u2192 x \u2261 y \u2192 Set)\n \u2192 P x refl\n \u2192 (y : A) (p : x \u2261 y) \u2192 P y p\nelimEq A .x P prefl x refl = prefl\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nCases : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nCases [] P = \u22a4\nCases (l \u2237 E) P = P here \u00d7 Cases E \u03bb t \u2192 P (there t)\n\ncase : (E : Enum) (P : Tag E \u2192 Set) (cs : Cases E P) (t : Tag E) \u2192 P t\ncase (l \u2237 E) P (c , cs) here = c\ncase (l \u2237 E) P (c , cs) (there t) = case E (\u03bb t \u2192 P (there t)) cs t\n\nUncurriedCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedCases E P X = Cases E P \u2192 X\n\nCurriedCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedCases [] P X = X\nCurriedCases (l \u2237 E) P X = P here \u2192 CurriedCases E (\u03bb t \u2192 P (there t)) X\n\ncurryCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n (f : UncurriedCases E P X) \u2192 CurriedCases E P X\ncurryCases [] P X f = f tt\ncurryCases (l \u2237 E) P X f = \u03bb c \u2192 curryCases E (\u03bb t \u2192 P (there t)) X (\u03bb cs \u2192 f (c , cs))\n\nuncurryCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n (f : CurriedCases E P X) \u2192 UncurriedCases E P X\nuncurryCases [] P X x tt = x\nuncurryCases (l \u2237 E) P X f (c , cs) = uncurryCases E (\u03bb t \u2192 P (there t)) X (f c) cs\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n `End : (i : I) \u2192 Desc I\n `Rec : (i : I) (D : Desc I) \u2192 Desc I\n `Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n `RecFun : (A : Set) (B : A \u2192 I) (D : Desc I) \u2192 Desc I\n\nISet : Set \u2192 Set\u2081\nISet I = I \u2192 Set\n\nEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 ISet I\nEl I (`End j) X i = j \u2261 i\nEl I (`Rec j D) X i = X j \u00d7 El I D X i\nEl I (`Arg A B) X i = \u03a3 A (\u03bb a \u2192 El I (B a) X i)\nEl I (`RecFun A B D) X i = ((a : A) \u2192 X (B a)) \u00d7 El I D X i\n\nAll : (I : Set) (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El I D X i) \u2192 Set\nAll I (`End j) X P i q = \u22a4\nAll I (`Rec j D) X P i (x , xs) = P j x \u00d7 All I D X P i xs\nAll I (`Arg A B) X P i (a , b) = All I (B a) X P i b\nAll I (`RecFun A B D) X P i (f , xs) = ((a : A) \u2192 P (B a) (f a)) \u00d7 All I D X P i xs\n\ncaseD : (E : Enum) (I : Set) (cs : Cases E (\u03bb _ \u2192 Desc I)) (t : Tag E) \u2192 Desc I\ncaseD E I cs t = case E (\u03bb _ \u2192 Desc I) cs t\n\n----------------------------------------------------------------------\n\nUncurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl I D X = {i : I} \u2192 El I D X i \u2192 X i\n\nCurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl I (`End i) X = X i\nCurriedEl I (`Rec j D) X = (x : X j) \u2192 CurriedEl I D X\nCurriedEl I (`Arg A B) X = (a : A) \u2192 CurriedEl I (B a) X\nCurriedEl I (`RecFun A B D) X = ((a : A) \u2192 X (B a)) \u2192 CurriedEl I D X\n\ncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : UncurriedEl I D X) \u2192 CurriedEl I D X\ncurryEl I (`End i) X cn = cn refl\ncurryEl I (`Rec i D) X cn = \u03bb x \u2192 curryEl I D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl I (`Arg A B) X cn = \u03bb a \u2192 curryEl I (B a) X (\u03bb xs \u2192 cn (a , xs))\ncurryEl I (`RecFun A B D) X cn = \u03bb f \u2192 curryEl I D X (\u03bb xs \u2192 cn (f , xs))\n\nuncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : CurriedEl I D X) \u2192 UncurriedEl I D X\nuncurryEl I (`End i) X cn refl = cn\nuncurryEl I (`Rec i D) X cn (x , xs) = uncurryEl I D X (cn x) xs\nuncurryEl I (`Arg A B) X cn (a , xs) = uncurryEl I (B a) X (cn a) xs\nuncurryEl I (`RecFun A B D) X cn (f , xs) = uncurryEl I D X (cn f) xs\n\ndata \u03bc (I : Set) (D : Desc I) : I \u2192 Set where\n con : UncurriedEl I D (\u03bc I D)\n\ncon2 : (I : Set) (D : Desc I) \u2192 CurriedEl I D (\u03bc I D)\ncon2 I D = curryEl I D (\u03bc I D) con\n\n----------------------------------------------------------------------\n\nUncurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : {i : I} \u2192 El I D X i \u2192 X i)\n \u2192 Set\nUncurriedAll I D X P cn =\n (i : I) (xs : El I D X i) \u2192 All I D X P i xs \u2192 P i (cn xs)\n\nCurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n \u2192 Set\nCurriedAll I (`End i) X P cn =\n P i (cn refl)\nCurriedAll I (`Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedAll I (`Arg A B) X P cn =\n (a : A) \u2192 CurriedAll I (B a) X P (\u03bb xs \u2192 cn (a , xs))\nCurriedAll I (`RecFun A B D) X P cn =\n (f : (a : A) \u2192 X (B a)) (ihf : (a : A) \u2192 P (B a) (f a)) \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (f , xs))\n\ncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : UncurriedAll I D X P cn)\n \u2192 CurriedAll I D X P cn\ncurryAll I (`End i) X P cn pf =\n pf i refl tt\ncurryAll I (`Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryAll I D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryAll I (`Arg A B) X P cn pf =\n \u03bb a \u2192 curryAll I (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\ncurryAll I (`RecFun A B D) X P cn pf =\n \u03bb f ihf \u2192 curryAll I D X P (\u03bb xs \u2192 cn (f , xs)) (\u03bb i xs ihs \u2192 pf i (f , xs) (ihf , ihs))\n\nuncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : CurriedAll I D X P cn)\n \u2192 UncurriedAll I D X P cn\nuncurryAll I (`End .i) X P cn pf i refl tt =\n pf\nuncurryAll I (`Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryAll I (`Arg A B) X P cn pf i (a , xs) ihs =\n uncurryAll I (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\nuncurryAll I (`RecFun A B D) X P cn pf i (f , xs) (ihf , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (f , ys)) (pf f ihf) i xs ihs\n\n----------------------------------------------------------------------\n\nind :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : UncurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\n\nhyps :\n (I : Set)\n (D\u2081 : Desc I)\n (P : (i : I) \u2192 \u03bc I D\u2081 i \u2192 Set)\n (pcon : UncurriedAll I D\u2081 (\u03bc I D\u2081) P con)\n (D\u2082 : Desc I)\n (i : I)\n (xs : El I D\u2082 (\u03bc I D\u2081) i)\n \u2192 All I D\u2082 (\u03bc I D\u2081) P i xs\n\nind I D P pcon i (con xs) = pcon i xs (hyps I D P pcon D i xs)\n\nhyps I D P pcon (`End j) i q = tt\nhyps I D P pcon (`Rec j A) i (x , xs) = ind I D P pcon j x , hyps I D P pcon A i xs\nhyps I D P pcon (`Arg A B) i (a , b) = hyps I D P pcon (B a) i b\nhyps I D P pcon (`RecFun A B E) i (f , xs) = (\u03bb a \u2192 ind I D P pcon (B a) (f a)) , hyps I D P pcon E i xs\n\n----------------------------------------------------------------------\n\nind2 :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : CurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\nind2 I D P pcon i x = ind I D P (uncurryAll I D (\u03bc I D) P con pcon) i x\n\nelim :\n (I : Set)\n (E : Enum)\n (Cs : Tag E \u2192 Desc I)\n \u2192 let D = `Arg (Tag E) Cs in\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n \u2192 UncurriedCases E\n (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))) \n ((i : I) (x : \u03bc I D i) \u2192 P i x)\nelim I E Cs P cs i x =\n let D = `Arg (Tag E) Cs in\n ind2 I D P (case E (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))) cs) i x\n\nelim2 :\n (I : Set)\n (E : Enum)\n (Cs : Tag E \u2192 Desc I)\n \u2192 let D = `Arg (Tag E) Cs in\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n \u2192 let Q = (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs)))\n X = ((i : I) (x : \u03bc I D i) \u2192 P i x)\n in CurriedCases E Q X\nelim2 I E Cs P =\n let D = `Arg (Tag E) Cs\n Q = (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs)))\n X = ((i : I) (x : \u03bc I D i) \u2192 P i x)\n in curryCases E Q X (elim I E Cs P)\n\n----------------------------------------------------------------------\n\nmodule Sugared where\n\n data \u2115T : Set where `zero `suc : \u2115T\n data VecT : Set where `nil `cons : VecT\n\n \u2115D : Desc \u22a4\n \u2115D = `Arg \u2115T \u03bb\n { `zero \u2192 `End tt\n ; `suc \u2192 `Rec tt (`End tt)\n }\n\n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n\n zero : \u2115 tt\n zero = con (`zero , refl)\n\n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (`suc , n , refl)\n\n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg VecT \u03bb\n { `nil \u2192 `End zero\n ; `cons \u2192 `Arg (\u2115 tt) \u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n))\n }\n\n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n\n nil : (A : Set) \u2192 Vec A zero\n nil A = con (`nil , refl)\n\n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (`cons , n , x , xs , refl)\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 n\n ; tt (`suc , m , q) (ih , tt) n \u2192 suc (ih n)\n }\n )\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 zero\n ; tt (`suc , m , q) (ih , tt) n \u2192 add n (ih n)\n }\n )\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) ih n ys \u2192 ys\n ; .(con (`suc , m , refl)) (`cons , m , x , xs , refl) (ih , tt) n ys \u2192 cons A (add m n) x (ih n ys)\n }\n )\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) tt \u2192 nil A\n ; .(con (`suc , n , refl)) (`cons , n , xs , xss , refl) (ih , tt) \u2192 append A m xs (mult n m) ih\n }\n )\n\n----------------------------------------------------------------------\n\nmodule Desugared where\n\n \u2115T : Enum\n \u2115T = \"zero\" \u2237 \"suc\" \u2237 []\n \n VecT : Enum\n VecT = \"nil\" \u2237 \"cons\" \u2237 []\n \n \u2115C : Tag \u2115T \u2192 Desc \u22a4\n \u2115C = caseD \u2115T \u22a4\n ( `End tt\n , `Rec tt (`End tt)\n , tt\n )\n \n \u2115D : Desc \u22a4\n \u2115D = `Arg (Tag \u2115T) \u2115C\n \n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n \n zero : \u2115 tt\n zero = con (here , refl)\n \n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (there here , n , refl)\n \n zero2 : \u2115 tt\n zero2 = con2 \u22a4 \u2115D here\n\n suc2 : \u2115 tt \u2192 \u2115 tt\n suc2 = con2 \u22a4 \u2115D (there here)\n\n VecC : (A : Set) \u2192 Tag VecT \u2192 Desc (\u2115 tt)\n VecC A = caseD VecT (\u2115 tt)\n ( `End zero\n , `Arg (\u2115 tt) (\u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n)))\n , tt\n )\n \n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg (Tag VecT) (VecC A)\n \n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n \n nil : (A : Set) \u2192 Vec A zero\n nil A = con (here , refl)\n \n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (there here , n , x , xs , refl)\n \n nil2 : (A : Set) \u2192 Vec A zero\n nil2 A = con2 (\u2115 tt) (VecD A) here\n\n cons2 : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons2 A = con2 (\u2115 tt) (VecD A) (there here)\n\n----------------------------------------------------------------------\n\n module Induction where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 n)\n , (\u03bb m,q ih,tt n \u2192 suc (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 zero)\n , (\u03bb m,q ih,tt n \u2192 add n (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb m t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) m)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)) m (t , c))\n (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n )\n ( (\u03bb q ih n ys \u2192 subst (\u03bb m \u2192 Vec A (add m n)) q ys)\n , (\u03bb m',x,xs,q ih,tt n ys \u2192\n let m' = proj\u2081 m',x,xs,q\n x = proj\u2081 (proj\u2082 m',x,xs,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 m',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb m \u2192 Vec A (add m n)) q (cons A (add m' n) x (ih n ys))\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC (Vec A m) t) (Vec (Vec A m)) n)\n (ih : All (\u2115 tt) (VecD (Vec A m)) (Vec (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m)) n (t , c))\n \u2192 Vec A (mult n m)\n )\n ( (\u03bb q ih \u2192 subst (\u03bb n \u2192 Vec A (mult n m)) q (nil A))\n , (\u03bb n',xs,xss,q ih,tt \u2192\n let n' = proj\u2081 n',xs,xss,q\n xs = proj\u2081 (proj\u2082 n',xs,xss,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 n',xs,xss,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb n \u2192 Vec A (mult n m)) q (append A m xs (mult n' m) ih)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n module Eliminator where\n\n elim\u2115 : (P : (\u2115 tt) \u2192 Set)\n (pzero : P zero)\n (psuc : (m : \u2115 tt) \u2192 P m \u2192 P (suc m))\n (n : \u2115 tt)\n \u2192 P n\n elim\u2115 P pzero psuc = ind \u22a4 \u2115D (\u03bb u n \u2192 P n)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 P n) u (t , c))\n \u2192 P (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (here , q)))\n pzero\n u q\n )\n , (\u03bb n,q ih,tt \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (there here , proj\u2081 n,q , q)))\n (psuc (proj\u2081 n,q) (proj\u2081 ih,tt))\n u (proj\u2082 n,q)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n\n elimVec : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set)\n (pnil : P zero (nil A))\n (pcons : (n : \u2115 tt) (a : A) (xs : Vec A n) \u2192 P n xs \u2192 P (suc n) (cons A n a xs))\n (n : \u2115 tt)\n (xs : Vec A n)\n \u2192 P n xs\n elimVec A P pnil pcons = ind (\u2115 tt) (VecD A) (\u03bb n xs \u2192 P n xs)\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) n)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb n xs \u2192 P n xs) n (t , c))\n \u2192 P n (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq (\u2115 tt) zero (\u03bb n q \u2192 P n (con (here , q)))\n pnil\n n q\n )\n , (\u03bb n',x,xs,q ih,tt \u2192\n let n' = proj\u2081 n',x,xs,q\n x = proj\u2081 (proj\u2082 n',x,xs,q)\n xs = proj\u2081 (proj\u2082 (proj\u2082 n',x,xs,q))\n q = proj\u2082 (proj\u2082 (proj\u2082 n',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n elimEq (\u2115 tt) (suc n') (\u03bb n q \u2192 P n (con (there here , n' , x , xs , q)))\n (pcons n' x xs ih )\n n q\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elimVec A (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elimVec (Vec A m) (\u03bb n xss \u2192 Vec A (mult n m))\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n\n module GenericEliminator where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim2 \u22a4 \u2115T \u2115C _\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim2 \u22a4 \u2115T \u2115C _\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elim2 (\u2115 tt) VecT (VecC A) _\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elim2 (\u2115 tt) VecT (VecC (Vec A m)) _\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n","old_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nmodule Spire.Examples.PropositionalDesc where\n\n----------------------------------------------------------------------\n\nelimEq : (A : Set) (x : A) (P : (y : A) \u2192 x \u2261 y \u2192 Set)\n \u2192 P x refl\n \u2192 (y : A) (p : x \u2261 y) \u2192 P y p\nelimEq A .x P prefl x refl = prefl\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nCases : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nCases [] P = \u22a4\nCases (l \u2237 E) P = P here \u00d7 Cases E \u03bb t \u2192 P (there t)\n\ncase : (E : Enum) (P : Tag E \u2192 Set) (cs : Cases E P) (t : Tag E) \u2192 P t\ncase (l \u2237 E) P (c , cs) here = c\ncase (l \u2237 E) P (c , cs) (there t) = case E (\u03bb t \u2192 P (there t)) cs t\n\nUncurriedCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedCases E P X = Cases E P \u2192 X\n\nCurriedCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedCases [] P X = X\nCurriedCases (l \u2237 E) P X = P here \u2192 CurriedCases E (\u03bb t \u2192 P (there t)) X\n\ncurryCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n (f : UncurriedCases E P X) \u2192 CurriedCases E P X\ncurryCases [] P X f = f tt\ncurryCases (l \u2237 E) P X f = \u03bb c \u2192 curryCases E (\u03bb t \u2192 P (there t)) X (\u03bb cs \u2192 f (c , cs))\n\nuncurryCases : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n (f : CurriedCases E P X) \u2192 UncurriedCases E P X\nuncurryCases [] P X x tt = x\nuncurryCases (l \u2237 E) P X f (c , cs) = uncurryCases E (\u03bb t \u2192 P (there t)) X (f c) cs\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n `End : (i : I) \u2192 Desc I\n `Rec : (i : I) (D : Desc I) \u2192 Desc I\n `Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n `RecFun : (A : Set) (B : A \u2192 I) (D : Desc I) \u2192 Desc I\n\nISet : Set \u2192 Set\u2081\nISet I = I \u2192 Set\n\nEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 ISet I\nEl I (`End j) X i = j \u2261 i\nEl I (`Rec j D) X i = X j \u00d7 El I D X i\nEl I (`Arg A B) X i = \u03a3 A (\u03bb a \u2192 El I (B a) X i)\nEl I (`RecFun A B D) X i = ((a : A) \u2192 X (B a)) \u00d7 El I D X i\n\nAll : (I : Set) (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El I D X i) \u2192 Set\nAll I (`End j) X P i q = \u22a4\nAll I (`Rec j D) X P i (x , xs) = P j x \u00d7 All I D X P i xs\nAll I (`Arg A B) X P i (a , b) = All I (B a) X P i b\nAll I (`RecFun A B D) X P i (f , xs) = ((a : A) \u2192 P (B a) (f a)) \u00d7 All I D X P i xs\n\ncaseD : (E : Enum) (I : Set) (cs : Cases E (\u03bb _ \u2192 Desc I)) (t : Tag E) \u2192 Desc I\ncaseD E I cs t = case E (\u03bb _ \u2192 Desc I) cs t\n\n----------------------------------------------------------------------\n\nUncurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl I D X = {i : I} \u2192 El I D X i \u2192 X i\n\nCurriedEl : (I : Set) (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl I (`End i) X = X i\nCurriedEl I (`Rec j D) X = (x : X j) \u2192 CurriedEl I D X\nCurriedEl I (`Arg A B) X = (a : A) \u2192 CurriedEl I (B a) X\nCurriedEl I (`RecFun A B D) X = ((a : A) \u2192 X (B a)) \u2192 CurriedEl I D X\n\ncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : UncurriedEl I D X) \u2192 CurriedEl I D X\ncurryEl I (`End i) X cn = cn refl\ncurryEl I (`Rec i D) X cn = \u03bb x \u2192 curryEl I D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl I (`Arg A B) X cn = \u03bb a \u2192 curryEl I (B a) X (\u03bb xs \u2192 cn (a , xs))\ncurryEl I (`RecFun A B D) X cn = \u03bb f \u2192 curryEl I D X (\u03bb xs \u2192 cn (f , xs))\n\nuncurryEl : (I : Set) (D : Desc I) (X : ISet I)\n (cn : CurriedEl I D X) \u2192 UncurriedEl I D X\nuncurryEl I (`End i) X cn refl = cn\nuncurryEl I (`Rec i D) X cn (x , xs) = uncurryEl I D X (cn x) xs\nuncurryEl I (`Arg A B) X cn (a , xs) = uncurryEl I (B a) X (cn a) xs\nuncurryEl I (`RecFun A B D) X cn (f , xs) = uncurryEl I D X (cn f) xs\n\ndata \u03bc (I : Set) (D : Desc I) (i : I) : Set where\n -- this equals UncurriedEl I D (\u03bc I D)\n con : El I D (\u03bc I D) i \u2192 \u03bc I D i\n\ncon2 : (I : Set) (D : Desc I) \u2192 CurriedEl I D (\u03bc I D)\ncon2 I D = curryEl I D (\u03bc I D) con\n\n----------------------------------------------------------------------\n\nUncurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : {i : I} \u2192 El I D X i \u2192 X i)\n \u2192 Set\nUncurriedAll I D X P cn =\n (i : I) (xs : El I D X i) \u2192 All I D X P i xs \u2192 P i (cn xs)\n\nCurriedAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n \u2192 Set\nCurriedAll I (`End i) X P cn =\n P i (cn refl)\nCurriedAll I (`Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedAll I (`Arg A B) X P cn =\n (a : A) \u2192 CurriedAll I (B a) X P (\u03bb xs \u2192 cn (a , xs))\nCurriedAll I (`RecFun A B D) X P cn =\n (f : (a : A) \u2192 X (B a)) (ihf : (a : A) \u2192 P (B a) (f a)) \u2192 CurriedAll I D X P (\u03bb xs \u2192 cn (f , xs))\n\ncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : UncurriedAll I D X P cn)\n \u2192 CurriedAll I D X P cn\ncurryAll I (`End i) X P cn pf =\n pf i refl tt\ncurryAll I (`Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryAll I D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryAll I (`Arg A B) X P cn pf =\n \u03bb a \u2192 curryAll I (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\ncurryAll I (`RecFun A B D) X P cn pf =\n \u03bb f ihf \u2192 curryAll I D X P (\u03bb xs \u2192 cn (f , xs)) (\u03bb i xs ihs \u2192 pf i (f , xs) (ihf , ihs))\n\nuncurryAll : (I : Set) (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl I D X)\n (pf : CurriedAll I D X P cn)\n \u2192 UncurriedAll I D X P cn\nuncurryAll I (`End .i) X P cn pf i refl tt =\n pf\nuncurryAll I (`Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryAll I (`Arg A B) X P cn pf i (a , xs) ihs =\n uncurryAll I (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\nuncurryAll I (`RecFun A B D) X P cn pf i (f , xs) (ihf , ihs) =\n uncurryAll I D X P (\u03bb ys \u2192 cn (f , ys)) (pf f ihf) i xs ihs\n\n----------------------------------------------------------------------\n\nind :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : UncurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\n\nhyps :\n (I : Set)\n (D\u2081 : Desc I)\n (P : (i : I) \u2192 \u03bc I D\u2081 i \u2192 Set)\n (pcon : UncurriedAll I D\u2081 (\u03bc I D\u2081) P con)\n (D\u2082 : Desc I)\n (i : I)\n (xs : El I D\u2082 (\u03bc I D\u2081) i)\n \u2192 All I D\u2082 (\u03bc I D\u2081) P i xs\n\nind I D P pcon i (con xs) = pcon i xs (hyps I D P pcon D i xs)\n\nhyps I D P pcon (`End j) i q = tt\nhyps I D P pcon (`Rec j A) i (x , xs) = ind I D P pcon j x , hyps I D P pcon A i xs\nhyps I D P pcon (`Arg A B) i (a , b) = hyps I D P pcon (B a) i b\nhyps I D P pcon (`RecFun A B E) i (f , xs) = (\u03bb a \u2192 ind I D P pcon (B a) (f a)) , hyps I D P pcon E i xs\n\n----------------------------------------------------------------------\n\nind2 :\n (I : Set)\n (D : Desc I)\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n (pcon : CurriedAll I D (\u03bc I D) P con)\n (i : I)\n (x : \u03bc I D i)\n \u2192 P i x\nind2 I D P pcon i x = ind I D P (uncurryAll I D (\u03bc I D) P con pcon) i x\n\nelim :\n (I : Set)\n (E : Enum)\n (Cs : Tag E \u2192 Desc I)\n \u2192 let D = `Arg (Tag E) Cs in\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n \u2192 UncurriedCases E\n (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))) \n ((i : I) (x : \u03bc I D i) \u2192 P i x)\nelim I E Cs P cs i x =\n let D = `Arg (Tag E) Cs in\n ind2 I D P (case E (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs))) cs) i x\n\nelim2 :\n (I : Set)\n (E : Enum)\n (Cs : Tag E \u2192 Desc I)\n \u2192 let D = `Arg (Tag E) Cs in\n (P : (i : I) \u2192 \u03bc I D i \u2192 Set)\n \u2192 let Q = (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs)))\n X = ((i : I) (x : \u03bc I D i) \u2192 P i x)\n in CurriedCases E Q X\nelim2 I E Cs P =\n let D = `Arg (Tag E) Cs\n Q = (\u03bb t \u2192 CurriedAll I (Cs t) (\u03bc I D) P (\u03bb xs \u2192 con (t , xs)))\n X = ((i : I) (x : \u03bc I D i) \u2192 P i x)\n in curryCases E Q X (elim I E Cs P)\n\n----------------------------------------------------------------------\n\nmodule Sugared where\n\n data \u2115T : Set where `zero `suc : \u2115T\n data VecT : Set where `nil `cons : VecT\n\n \u2115D : Desc \u22a4\n \u2115D = `Arg \u2115T \u03bb\n { `zero \u2192 `End tt\n ; `suc \u2192 `Rec tt (`End tt)\n }\n\n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n\n zero : \u2115 tt\n zero = con (`zero , refl)\n\n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (`suc , n , refl)\n\n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg VecT \u03bb\n { `nil \u2192 `End zero\n ; `cons \u2192 `Arg (\u2115 tt) \u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n))\n }\n\n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n\n nil : (A : Set) \u2192 Vec A zero\n nil A = con (`nil , refl)\n\n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (`cons , n , x , xs , refl)\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 n\n ; tt (`suc , m , q) (ih , tt) n \u2192 suc (ih n)\n }\n )\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb\n { tt (`zero , q) tt n \u2192 zero\n ; tt (`suc , m , q) (ih , tt) n \u2192 add n (ih n)\n }\n )\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) ih n ys \u2192 ys\n ; .(con (`suc , m , refl)) (`cons , m , x , xs , refl) (ih , tt) n ys \u2192 cons A (add m n) x (ih n ys)\n }\n )\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb\n { .(con (`zero , refl)) (`nil , refl) tt \u2192 nil A\n ; .(con (`suc , n , refl)) (`cons , n , xs , xss , refl) (ih , tt) \u2192 append A m xs (mult n m) ih\n }\n )\n\n----------------------------------------------------------------------\n\nmodule Desugared where\n\n \u2115T : Enum\n \u2115T = \"zero\" \u2237 \"suc\" \u2237 []\n \n VecT : Enum\n VecT = \"nil\" \u2237 \"cons\" \u2237 []\n \n \u2115C : Tag \u2115T \u2192 Desc \u22a4\n \u2115C = caseD \u2115T \u22a4\n ( `End tt\n , `Rec tt (`End tt)\n , tt\n )\n \n \u2115D : Desc \u22a4\n \u2115D = `Arg (Tag \u2115T) \u2115C\n \n \u2115 : \u22a4 \u2192 Set\n \u2115 = \u03bc \u22a4 \u2115D\n \n zero : \u2115 tt\n zero = con (here , refl)\n \n suc : \u2115 tt \u2192 \u2115 tt\n suc n = con (there here , n , refl)\n \n zero2 : \u2115 tt\n zero2 = con2 \u22a4 \u2115D here\n\n suc2 : \u2115 tt \u2192 \u2115 tt\n suc2 = con2 \u22a4 \u2115D (there here)\n\n VecC : (A : Set) \u2192 Tag VecT \u2192 Desc (\u2115 tt)\n VecC A = caseD VecT (\u2115 tt)\n ( `End zero\n , `Arg (\u2115 tt) (\u03bb n \u2192 `Arg A \u03bb _ \u2192 `Rec n (`End (suc n)))\n , tt\n )\n \n VecD : (A : Set) \u2192 Desc (\u2115 tt)\n VecD A = `Arg (Tag VecT) (VecC A)\n \n Vec : (A : Set) (n : \u2115 tt) \u2192 Set\n Vec A n = \u03bc (\u2115 tt) (VecD A) n\n \n nil : (A : Set) \u2192 Vec A zero\n nil A = con (here , refl)\n \n cons : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons A n x xs = con (there here , n , x , xs , refl)\n \n nil2 : (A : Set) \u2192 Vec A zero\n nil2 A = con2 (\u2115 tt) (VecD A) here\n\n cons2 : (A : Set) (n : \u2115 tt) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\n cons2 A = con2 (\u2115 tt) (VecD A) (there here)\n\n----------------------------------------------------------------------\n\n module Induction where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 n)\n , (\u03bb m,q ih,tt n \u2192 suc (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = ind \u22a4 \u2115D (\u03bb _ _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 \u2115 u \u2192 \u2115 u) u (t , c))\n \u2192 \u2115 u \u2192 \u2115 u\n )\n ( (\u03bb q ih n \u2192 zero)\n , (\u03bb m,q ih,tt n \u2192 add n (proj\u2081 ih,tt n))\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n) \n append A = ind (\u2115 tt) (VecD A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb m t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) m)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)) m (t , c))\n (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n )\n ( (\u03bb q ih n ys \u2192 subst (\u03bb m \u2192 Vec A (add m n)) q ys)\n , (\u03bb m',x,xs,q ih,tt n ys \u2192\n let m' = proj\u2081 m',x,xs,q\n x = proj\u2081 (proj\u2082 m',x,xs,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 m',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb m \u2192 Vec A (add m n)) q (cons A (add m' n) x (ih n ys))\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = ind (\u2115 tt) (VecD (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m))\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC (Vec A m) t) (Vec (Vec A m)) n)\n (ih : All (\u2115 tt) (VecD (Vec A m)) (Vec (Vec A m)) (\u03bb n xss \u2192 Vec A (mult n m)) n (t , c))\n \u2192 Vec A (mult n m)\n )\n ( (\u03bb q ih \u2192 subst (\u03bb n \u2192 Vec A (mult n m)) q (nil A))\n , (\u03bb n',xs,xss,q ih,tt \u2192\n let n' = proj\u2081 n',xs,xss,q\n xs = proj\u2081 (proj\u2082 n',xs,xss,q)\n q = proj\u2082 (proj\u2082 (proj\u2082 n',xs,xss,q))\n ih = proj\u2081 ih,tt\n in\n subst (\u03bb n \u2192 Vec A (mult n m)) q (append A m xs (mult n' m) ih)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n module Eliminator where\n\n elim\u2115 : (P : (\u2115 tt) \u2192 Set)\n (pzero : P zero)\n (psuc : (m : \u2115 tt) \u2192 P m \u2192 P (suc m))\n (n : \u2115 tt)\n \u2192 P n\n elim\u2115 P pzero psuc = ind \u22a4 \u2115D (\u03bb u n \u2192 P n)\n (\u03bb u t,c \u2192 case \u2115T\n (\u03bb t \u2192 (c : El \u22a4 (\u2115C t) \u2115 u)\n (ih : All \u22a4 \u2115D \u2115 (\u03bb u n \u2192 P n) u (t , c))\n \u2192 P (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (here , q)))\n pzero\n u q\n )\n , (\u03bb n,q ih,tt \u2192\n elimEq \u22a4 tt (\u03bb u q \u2192 P (con (there here , proj\u2081 n,q , q)))\n (psuc (proj\u2081 n,q) (proj\u2081 ih,tt))\n u (proj\u2082 n,q)\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n tt\n\n elimVec : (A : Set) (P : (n : \u2115 tt) \u2192 Vec A n \u2192 Set)\n (pnil : P zero (nil A))\n (pcons : (n : \u2115 tt) (a : A) (xs : Vec A n) \u2192 P n xs \u2192 P (suc n) (cons A n a xs))\n (n : \u2115 tt)\n (xs : Vec A n)\n \u2192 P n xs\n elimVec A P pnil pcons = ind (\u2115 tt) (VecD A) (\u03bb n xs \u2192 P n xs)\n (\u03bb n t,c \u2192 case VecT\n (\u03bb t \u2192 (c : El (\u2115 tt) (VecC A t) (Vec A) n)\n (ih : All (\u2115 tt) (VecD A) (Vec A) (\u03bb n xs \u2192 P n xs) n (t , c))\n \u2192 P n (con (t , c))\n )\n ( (\u03bb q ih \u2192\n elimEq (\u2115 tt) zero (\u03bb n q \u2192 P n (con (here , q)))\n pnil\n n q\n )\n , (\u03bb n',x,xs,q ih,tt \u2192\n let n' = proj\u2081 n',x,xs,q\n x = proj\u2081 (proj\u2082 n',x,xs,q)\n xs = proj\u2081 (proj\u2082 (proj\u2082 n',x,xs,q))\n q = proj\u2082 (proj\u2082 (proj\u2082 n',x,xs,q))\n ih = proj\u2081 ih,tt\n in\n elimEq (\u2115 tt) (suc n') (\u03bb n q \u2192 P n (con (there here , n' , x , xs , q)))\n (pcons n' x xs ih )\n n q\n )\n , tt\n )\n (proj\u2081 t,c)\n (proj\u2082 t,c)\n )\n\n----------------------------------------------------------------------\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n \n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim\u2115 (\u03bb _ \u2192 \u2115 tt \u2192 \u2115 tt)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n \n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elimVec A (\u03bb m xs \u2192 (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n \n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elimVec (Vec A m) (\u03bb n xss \u2192 Vec A (mult n m))\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n\n module GenericEliminator where\n\n add : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n add = elim2 \u22a4 \u2115T \u2115C _\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n tt\n\n mult : \u2115 tt \u2192 \u2115 tt \u2192 \u2115 tt\n mult = elim2 \u22a4 \u2115T \u2115C _\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n tt\n\n append : (A : Set) (m : \u2115 tt) (xs : Vec A m) (n : \u2115 tt) (ys : Vec A n) \u2192 Vec A (add m n)\n append A = elim2 (\u2115 tt) VecT (VecC A) _\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons A (add m n) x (ih n ys))\n\n concat : (A : Set) (m n : \u2115 tt) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\n concat A m = elim2 (\u2115 tt) VecT (VecC (Vec A m)) _\n (nil A)\n (\u03bb n xs xss ih \u2192 append A m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"1131c2f7ec949c07dd71af9ef1372bf7a89cf90e","subject":"Added doc.","message":"Added doc.\n\nIgnore-this: 195af1bec8b3d065622ddc54d20fb410\n\ndarcs-hash:20110805133551-3bd4e-85248138588af499a8b16b7c2c1211186cfb3687.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/Setoids\/FOTC.agda","new_file":"Draft\/Setoids\/FOTC.agda","new_contents":"------------------------------------------------------------------------------\n-- Using setoids to formalize the FOTC\n------------------------------------------------------------------------------\n\n{-\nFrom Peter emails:\n\nAt that time I was considering an inductive data type D and an\ninductively defined equality on D. But I think what we are doing now\nis better.\n\nFor = : The reason is that with the propositional identity we have\nidentity elimination which lets us replace equals by equals. We cannot\ndo that in general if we have setoid equality.\n\nFor D: the reason why I prefer to postulate D is that we have no use\nfor the inductive structure of D, and this inductive structure would\nmake e g 0 + 0 different from 0. So in that case we need setoid\nequality.\n-}\n\nmodule FOTC where\n\n-- We add 3 to the fixities of the standard library.\ninfixl 9 _\u00b7_ -- The symbol is '\\cdot'.\n\n------------------------------------------------------------------------------\n\ndata D : Set where\n K S : D\n _\u00b7_ : D \u2192 D \u2192 D\n\nmodule PeterEquality where\n\n -- From Peter's slides\n -- http:\/\/www.cse.chalmers.se\/~peterd\/slides\/Amagasaki.pdf\n\n infix 7 _\u2250_\n\n data _\u2250_ : D \u2192 D \u2192 Set where\n refl : \u2200 x \u2192 x \u2250 x\n sym : \u2200 {x y} \u2192 x \u2250 y \u2192 y \u2250 x\n trans : \u2200 {x y z} \u2192 x \u2250 y \u2192 y \u2250 z \u2192 x \u2250 z\n cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2250 x\u2082 \u2192 y\u2081 \u2250 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2250 x\u2082 \u00b7 y\u2082\n Kax : \u2200 x y \u2192 K \u00b7 x \u00b7 y \u2250 x\n Sax : \u2200 x y z \u2192 S \u00b7 x \u00b7 y \u00b7 z \u2250 x \u00b7 z \u00b7 (y \u00b7 z)\n\n -- It seems we cannot define the identity elimination using the setoid\n -- equality\n -- subst : \u2200 {x y} (P : D \u2192 Set) \u2192 x \u2250 y \u2192 P x \u2192 P y\n -- subst P x\u2250y Px = {!!}\n\n------------------------------------------------------------------------------\n\nmodule LeibnizEquality where\n\n -- Barthe et al. [*, p. 262] use the Leibniz equality when\n -- they talk about setoids.\n\n -- [*] Gilles Barthe, Venanzio Capretta, and Olivier Pons. Setoids in\n -- type theory. Journal of Functional Programming, 13(2):261\u2013293, 2003\n\n -- Using the Leibniz equality\n -- (Adapted from Agda\/examples\/lib\/Logic\/Leibniz.agda)\n\n infix 7 _\u2250_\n\n _\u2250_ : D \u2192 D \u2192 Set\u2081\n x \u2250 y = (P : D \u2192 Set) \u2192 P x \u2192 P y\n\n -- We can proof the setoids properties\n\n \u2250-refl : \u2200 x \u2192 x \u2250 x\n \u2250-refl x P Px = Px\n\n \u2250-sym : \u2200 {x y} \u2192 x \u2250 y \u2192 y \u2250 x\n \u2250-sym {x} x\u2250y P Py = x\u2250y (\u03bb z \u2192 P z \u2192 P x) (\u03bb Px \u2192 Px) Py\n\n \u2250-trans : \u2200 x y z \u2192 x \u2250 y \u2192 y \u2250 z \u2192 x \u2250 z\n \u2250-trans x y z x\u2250y y\u2250z P Px = y\u2250z P (x\u2250y P Px)\n\n -- and the identity elimination\n\n \u2250-subst : \u2200 (P : D \u2192 Set) {x y} \u2192 x \u2250 y \u2192 P x \u2192 P y\n \u2250-subst P x\u2250y = x\u2250y P\n\n -- but it seems we cannot prove the congruency\n\n -- \u2250-cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2250 x\u2082 \u2192 y\u2081 \u2250 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2250 x\u2082 \u00b7 y\u2082\n -- \u2250-cong x\u2081\u2250x\u2082 y\u2081\u2250y\u2082 P Px\u2081y\u2081 = {!!}\n","old_contents":"------------------------------------------------------------------------------\n-- Using setoids to formalize the FOTC\n------------------------------------------------------------------------------\n\nmodule FOTC where\n\n-- We add 3 to the fixities of the standard library.\ninfixl 9 _\u00b7_ -- The symbol is '\\cdot'.\n------------------------------------------------------------------------------\n\ndata D : Set where\n K S : D\n _\u00b7_ : D \u2192 D \u2192 D\n\nmodule PeterEquality where\n\n -- From Peter's slides\n -- http:\/\/www.cse.chalmers.se\/~peterd\/slides\/Amagasaki.pdf\n\n infix 7 _\u2250_\n\n data _\u2250_ : D \u2192 D \u2192 Set where\n refl : \u2200 x \u2192 x \u2250 x\n sym : \u2200 {x y} \u2192 x \u2250 y \u2192 y \u2250 x\n trans : \u2200 {x y z} \u2192 x \u2250 y \u2192 y \u2250 z \u2192 x \u2250 z\n cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2250 x\u2082 \u2192 y\u2081 \u2250 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2250 x\u2082 \u00b7 y\u2082\n Kax : \u2200 x y \u2192 K \u00b7 x \u00b7 y \u2250 x\n Sax : \u2200 x y z \u2192 S \u00b7 x \u00b7 y \u00b7 z \u2250 x \u00b7 z \u00b7 (y \u00b7 z)\n\n -- It seems we cannot define the identity elimination using the setoid\n -- equality\n -- subst : \u2200 {x y} (P : D \u2192 Set) \u2192 x \u2250 y \u2192 P x \u2192 P y\n -- subst P x\u2250y Px = {!!}\n\n------------------------------------------------------------------------------\n\nmodule LeibnizEquality where\n\n -- Barthe et al. [*, p. 262] use the Leibniz equality when\n -- they talk about setoids.\n\n -- [*] Gilles Barthe, Venanzio Capretta, and Olivier Pons. Setoids in\n -- type theory. Journal of Functional Programming, 13(2):261\u2013293, 2003\n\n -- Using the Leibniz equality\n -- (Adapted from Agda\/examples\/lib\/Logic\/Leibniz.agda)\n\n infix 7 _\u2250_\n\n _\u2250_ : D \u2192 D \u2192 Set\u2081\n x \u2250 y = (P : D \u2192 Set) \u2192 P x \u2192 P y\n\n -- We can proof the setoids properties\n\n \u2250-refl : \u2200 x \u2192 x \u2250 x\n \u2250-refl x P Px = Px\n\n \u2250-sym : \u2200 {x y} \u2192 x \u2250 y \u2192 y \u2250 x\n \u2250-sym {x} x\u2250y P Py = x\u2250y (\u03bb z \u2192 P z \u2192 P x) (\u03bb Px \u2192 Px) Py\n\n \u2250-trans : \u2200 x y z \u2192 x \u2250 y \u2192 y \u2250 z \u2192 x \u2250 z\n \u2250-trans x y z x\u2250y y\u2250z P Px = y\u2250z P (x\u2250y P Px)\n\n -- and the identity elimination\n\n \u2250-subst : \u2200 (P : D \u2192 Set) {x y} \u2192 x \u2250 y \u2192 P x \u2192 P y\n \u2250-subst P x\u2250y = x\u2250y P\n\n -- but it seems we cannot prove the congruency\n -- \u2250-cong : \u2200 {x\u2081 x\u2082 y\u2081 y\u2082} \u2192 x\u2081 \u2250 x\u2082 \u2192 y\u2081 \u2250 y\u2082 \u2192 x\u2081 \u00b7 y\u2081 \u2250 x\u2082 \u00b7 y\u2082\n -- \u2250-cong x\u2081\u2250x\u2082 y\u2081\u2250y\u2082 P Px\u2081y\u2081 = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"92509a078139c27bd15ff2e40e8df4061281e344","subject":"Fixed a note on least fixed-points.","message":"Fixed a note on least fixed-points.\n\nIgnore-this: 8b79d84c55313d3921e3072d6e704449\n\ndarcs-hash:20120306150936-3bd4e-79d9d5387fe2a301e75e785c41d72d50a0c0bd81.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/LeastFixedPoints.agda","new_file":"notes\/fixed-points\/LeastFixedPoints.agda","new_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Tested with FOT on 06 March 2012.\n\nmodule LeastFixedPoints where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\ninfixl 9 _+_\n\n------------------------------------------------------------------------------\n-- Basic definitions\n\n-- Pre-fixed point : d is a pre-fixed point of f if f d \u2264 d\n\n-- Post-fixed point : d is a post-fixed point of f if d \u2264 f d\n\n-- Fixed-point : d is a fixed-point of f if f d = d\n\n-- Least pre-fixed point : d is the least pre-fixed point of f if\n-- 1. f d \u2264 d -- d is a pre-fixed point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Least fixed-point : d is the least fixed-point of f if\n-- 1. f d = d -- d is a fixed-point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Thm: If d is the least pre-fixed point of f, then d is the least\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n-- Thm: If d is the greatest post-fixed point of f, then d is the greatest\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n------------------------------------------------------------------------------\n-- Auxiliary definitions\n\nflip : {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\nflip f b a = f a b\n\n------------------------------------------------------------------------------\n-- N is a least fixed point of a functor\n\n-- Instead defining the least fixed-point via a (higher-order)\n-- operator, we will define it using an instance of that operator.\n\n-- The functor\nNatF : (D \u2192 Set) \u2192 D \u2192 Set\nNatF P n = n \u2261 zero \u2228 (\u2203[ m ] n \u2261 succ\u2081 m \u2227 P m)\n\n-- The natural numbers are the least fixed-point of NatF.\npostulate\n N : D \u2192 Set\n\n -- N is a pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the introduction rules.\n N-lfp\u2081 : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ m ] n \u2261 succ\u2081 m \u2227 N m) \u2192 N n\n -- N-lfp\u2081 : \u2200 n \u2192 NatF N n \u2192 N n -- Higher-order version\n\n -- N is a the least pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the elimination rule of an inductively\n -- defined predicate.\n N-lfp\u2082 : (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ m ] n \u2261 succ\u2081 m \u2227 A m) \u2192 A n) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n -- N-lfp\u2082 : (A : D \u2192 Set) \u2192 -- Higher-order version\n -- (\u2200 {n} \u2192 NatF A n \u2192 A n) \u2192\n -- \u2200 {n} \u2192 N n \u2192 A n\n\n------------------------------------------------------------------------------\n-- The data constructors of N.\nzN : N zero\nzN = N-lfp\u2081 (inj\u2081 refl)\n\nsN : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\nsN Nn = N-lfp\u2081 (inj\u2082 (_ , (refl , Nn)))\n\n------------------------------------------------------------------------------\n-- Because N is the least pre-fixed point of NatF (i.e. N-lfp\u2081 and\n-- N-lfp\u2082), we can proof that N is also a post-fixed point of NatF.\n\n-- N is a post-fixed point of NatF.\nN-lfp\u2083 : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ\u2081 m \u2227 N m)\nN-lfp\u2083 Nn = N-lfp\u2082 A prf Nn\n where\n A : D \u2192 Set\n A x = x \u2261 zero \u2228 \u2203 \u03bb m \u2192 x \u2261 succ\u2081 m \u2227 N m\n\n prf : \u2200 {n'} \u2192 n' \u2261 zero \u2228 \u2203 (\u03bb m \u2192 n' \u2261 succ\u2081 m \u2227 A m) \u2192 A n'\n prf {n'} h = [ inj\u2081 , (\u03bb h\u2081 \u2192 inj\u2082 (prf\u2081 h\u2081)) ] h\n where\n prf\u2081 : \u2203 (\u03bb m \u2192 n' \u2261 succ\u2081 m \u2227 (m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 N m'))) \u2192\n \u2203 \u03bb m \u2192 n' \u2261 succ\u2081 m \u2227 N m\n prf\u2081 (m , n'=Sm , h\u2082) = m , n'=Sm , prf\u2082 h\u2082\n where\n prf\u2082 : m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 N m') \u2192 N m\n prf\u2082 h\u2082 = [ (\u03bb h\u2083 \u2192 subst N (sym h\u2083) zN) , prf\u2083 ] h\u2082\n where\n prf\u2083 : \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 N m') \u2192 N m\n prf\u2083 (_ , m\u2261Sm' , Nm') = subst N (sym m\u2261Sm') (sN Nm')\n\n------------------------------------------------------------------------------\n-- The induction principle for N *without* the hypothesis N n in the\n-- induction step.\nindN\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\nindN\u2081 A A0 is Nn = N-lfp\u2082 A [ prf\u2081 , prf\u2082 ] Nn\n where\n prf\u2081 : \u2200 {n'} \u2192 n' \u2261 zero \u2192 A n'\n prf\u2081 n'\u22610 = subst A (sym n'\u22610) A0\n\n prf\u2082 : \u2200 {n'} \u2192 \u2203 (\u03bb m \u2192 n' \u2261 succ\u2081 m \u2227 A m) \u2192 A n'\n prf\u2082 (_ , n'\u2261Sm , Am) = subst A (sym n'\u2261Sm) (is Am)\n\n-- The induction principle for N *with* the hypothesis N n in the\n-- induction step.\n--\n-- 2012-03-06. We cannot proof this principle because N-lfp\u2082 does not\n-- have the hypothesis N n.\n--\n-- indN\u2082 : (A : D \u2192 Set) \u2192\n-- A zero \u2192\n-- (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n-- \u2200 {n} \u2192 N n \u2192 A n\n-- indN\u2082 A A0 is Nn = N-lfp\u2082 A [ prf\u2081 , prf\u2082 ] Nn\n-- where\n-- prf\u2081 : \u2200 {n'} \u2192 n' \u2261 zero \u2192 A n'\n-- prf\u2081 n'\u22610 = subst A (sym n'\u22610) A0\n\n-- prf\u2082 : \u2200 {n'} \u2192 \u2203 (\u03bb m \u2192 n' \u2261 succ\u2081 m \u2227 A m) \u2192 A n'\n-- prf\u2082 {n'} (m , n'\u2261Sm , Am) = subst A (sym n'\u2261Sm) (is helper Am)\n-- where\n-- helper : N m\n-- helper = [ prf\u2083 , prf\u2084 ] (N-lfp\u2083 {!!})\n-- where\n-- prf\u2083 : n' \u2261 zero \u2192 N m\n-- prf\u2083 n'\u22610 = \u22a5-elim (0\u2260S (trans (sym n'\u22610) n'\u2261Sm))\n\n-- prf\u2084 : \u2203 (\u03bb m' \u2192 n' \u2261 succ\u2081 m' \u2227 N m') \u2192 N m\n-- prf\u2084 (_ , n'\u2261Sm' , Nm') =\n-- subst N (succInjective (trans (sym n'\u2261Sm') n'\u2261Sm)) Nm'\n\n------------------------------------------------------------------------------\n-- Example: We will use N-lfp\u2082 as the induction principle on N.\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ\u2081 d + e \u2261 succ\u2081 (d + e)\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity n = +-0x n\n\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {n = n} Nm Nn = N-lfp\u2082 A prf Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n prf : \u2200 {m'} \u2192 m' \u2261 zero \u2228 \u2203 (\u03bb m'' \u2192 m' \u2261 succ\u2081 m'' \u2227 A m'') \u2192 A m'\n prf h = [ prf\u2081 , prf\u2082 ] h\n where\n A0 : A zero\n A0 = subst N (sym (+-leftIdentity n)) Nn\n\n prf\u2081 : \u2200 {m} \u2192 m \u2261 zero \u2192 A m\n prf\u2081 h\u2081 = subst N (cong (flip _+_ n) (sym h\u2081)) A0\n\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} ih = subst N (sym (+-Sx i n)) (sN ih)\n\n prf\u2082 : \u2200 {m} \u2192 \u2203 (\u03bb m'' \u2192 m \u2261 succ\u2081 m'' \u2227 A m'') \u2192 A m\n prf\u2082 (_ , m\u2261Sm'' , Am'') =\n subst N (cong (flip _+_ n) (sym m\u2261Sm'')) (is Am'')\n","old_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- Tested with FOT on 02 March 2012.\n\nmodule LeastFixedPoints where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\ninfixl 9 _+_\n\n------------------------------------------------------------------------------\n-- Basic definitions\n\n-- Pre-fixed point : d is a pre-fixed point of f if f d \u2264 d\n\n-- Post-fixed point : d is a post-fixed point of f if d \u2264 f d\n\n-- Fixed-point : d is a fixed-point of f if f d = d\n\n-- Least pre-fixed point : d is the least pre-fixed point of f if\n-- 1. f d \u2264 d -- d is a pre-fixed point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Least fixed-point : d is the least fixed-point of f if\n-- 1. f d = d -- d is a fixed-point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Thm: If d is the least pre-fixed point of f, then d is a fixed\n-- point of f (TODO: source).\n\n------------------------------------------------------------------------------\n-- Auxiliary definitions\n\nflip : {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\nflip f b a = f a b\n\n------------------------------------------------------------------------------\n-- N is a least pre-fixed point of a functor\n\n-- Instead defining the least pre-fixed via a (higher-order)\n-- operator, we will define it using an instance of that operator.\n\n-- The functor\nNatF : (D \u2192 Set) \u2192 D \u2192 Set\nNatF X n = n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ\u2081 m \u2227 X m)\n\n-- The natural numbers are the least pre-fixed point of NatF.\npostulate\n N : D \u2192 Set\n\n -- N is pre-fixed point of NatF.\n -- Peter: It corresponds to the introduction rules.\n N-lfp\u2081 : \u2200 n \u2192 n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ\u2081 m \u2227 N m) \u2192 N n\n -- N-lfp\u2081 : \u2200 n \u2192 NatF N n \u2192 N n -- Higher-order version\n\n -- Peter: It corresponds to the elimination rule of an inductively\n -- defined predicate.\n N-lfp\u2082 : (A : D \u2192 Set) \u2192 \u2200 {n} \u2192\n (n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ\u2081 m \u2227 A m) \u2192 A n) \u2192\n N n \u2192 A n\n -- N-lfp\u2082 : (A : D \u2192 Set) \u2192 \u2200 {n} \u2192 -- Higher-order version\n -- (NatF A n \u2192 A n) \u2192\n -- N n \u2192 A n\n\n------------------------------------------------------------------------------\n-- The data constructors of N.\nzN : N zero\nzN = N-lfp\u2081 zero (inj\u2081 refl)\n\nsN : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\nsN {n} Nn = N-lfp\u2081 (succ\u2081 n) (inj\u2082 (\u2203-intro (refl , Nn)))\n\n------------------------------------------------------------------------------\n-- Because N is the least pre-fixed point of NatF (i.e. N-lfp\u2081 and\n-- N-lfp\u2082), we can proof that N is also a post-fixed point of NatF.\n\n-- N is a post-fixed point of NatF.\nN-lfp\u2083 : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203 \u03bb m \u2192 n \u2261 succ\u2081 m \u2227 N m)\nN-lfp\u2083 {n} Nn = N-lfp\u2082 A prf Nn\n where\n A : D \u2192 Set\n A x = x \u2261 zero \u2228 \u2203 \u03bb m \u2192 x \u2261 succ\u2081 m \u2227 N m\n\n prf : n \u2261 zero \u2228 \u2203 (\u03bb m \u2192 n \u2261 succ\u2081 m \u2227 A m) \u2192 A n\n prf h = [ inj\u2081 , (\u03bb h\u2081 \u2192 inj\u2082 (prf\u2081 h\u2081)) ] h\n where\n prf\u2081 : \u2203 (\u03bb m \u2192 n \u2261 succ\u2081 m \u2227 (m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 N m'))) \u2192\n \u2203 \u03bb m \u2192 n \u2261 succ\u2081 m \u2227 N m\n prf\u2081 (\u2203-intro {m} (n=Sm , h\u2082)) = \u2203-intro (n=Sm , prf\u2082 h\u2082)\n where\n prf\u2082 : m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 N m') \u2192 N m\n prf\u2082 h\u2082 = [ (\u03bb h\u2083 \u2192 subst N (sym h\u2083) zN) , prf\u2083 ] h\u2082\n where\n prf\u2083 : \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 N m') \u2192 N m\n prf\u2083 (\u2203-intro (m\u2261Sm' , Nm')) = subst N (sym m\u2261Sm') (sN Nm')\n\n------------------------------------------------------------------------------\n-- The induction principle for N *without* the hypothesis N n in the\n-- induction step.\nindN\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\nindN\u2081 A A0 is {n} Nn = N-lfp\u2082 A [ prf\u2081 , prf\u2082 ] Nn\n where\n prf\u2081 : n \u2261 zero \u2192 A n\n prf\u2081 n\u22610 = subst A (sym n\u22610) A0\n\n prf\u2082 : \u2203 (\u03bb m \u2192 n \u2261 succ\u2081 m \u2227 A m) \u2192 A n\n prf\u2082 (\u2203-intro (n\u2261Sm , Am)) = subst A (sym n\u2261Sm) (is Am)\n\n-- The induction principle for N *with* the hypothesis N n in the\n-- induction step.\nindN\u2082 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\nindN\u2082 A A0 is {n} Nn = N-lfp\u2082 A [ prf\u2081 , prf\u2082 ] Nn\n where\n prf\u2081 : n \u2261 zero \u2192 A n\n prf\u2081 n\u22610 = subst A (sym n\u22610) A0\n\n prf\u2082 : \u2203 (\u03bb m \u2192 n \u2261 succ\u2081 m \u2227 A m) \u2192 A n\n prf\u2082 (\u2203-intro {m} (n\u2261Sm , Am)) = subst A (sym n\u2261Sm) (is helper Am)\n where\n helper : N m\n helper = [ prf\u2083 , prf\u2084 ] (N-lfp\u2083 Nn)\n where\n prf\u2083 : n \u2261 zero \u2192 N m\n prf\u2083 n\u22610 = \u22a5-elim (0\u2260S (trans (sym n\u22610) n\u2261Sm))\n\n prf\u2084 : \u2203 (\u03bb m' \u2192 n \u2261 succ\u2081 m' \u2227 N m') \u2192 N m\n prf\u2084 (\u2203-intro (n\u2261Sm' , Nm')) =\n subst N (succInjective (trans (sym n\u2261Sm') n\u2261Sm)) Nm'\n\n------------------------------------------------------------------------------\n-- Example: We will use N-lfp\u2082 as the induction principle on N.\npostulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ\u2081 d + e \u2261 succ\u2081 (d + e)\n\n+-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n+-leftIdentity n = +-0x n\n\n+-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n+-N {m} {n} Nm Nn = N-lfp\u2082 A prf Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n prf : m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n prf h = [ prf\u2081 , prf\u2082 ] h\n where\n A0 : A zero\n A0 = subst N (sym (+-leftIdentity n)) Nn\n\n prf\u2081 : m \u2261 zero \u2192 A m\n prf\u2081 h\u2081 = subst N (cong (flip _+_ n) (sym h\u2081)) A0\n\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} ih = subst N (sym (+-Sx i n)) (sN ih)\n\n prf\u2082 : \u2203 (\u03bb m' \u2192 m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n prf\u2082 (\u2203-intro (m\u2261Sm' , Am')) =\n subst N (cong (flip _+_ n) (sym m\u2261Sm')) (is Am')\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b50b779421271cc824ea5ccbbe9d9ee045719ec7","subject":"t-abs case of validity-of-derive; remove outdated bug report task","message":"t-abs case of validity-of-derive; remove outdated bug report task\n\nOld-commit-hash: 423c6e803350c77c286eca5a83687e57ecf313c9\n","repos":"inc-lc\/ilc-agda","old_file":"nats\/A.agda","new_file":"nats\/A.agda","new_contents":"-- TODO: Include proof of\n-- \"not all correctly-typed values are valid changes\"\n\nmodule A where\n\nopen import Data.Product\nopen import Data.Nat using (\u2115)\nopen import Data.Unit using (\u22a4)\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Level using\n (zero ; Level ; suc)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\npostulate extensionality : Extensionality zero zero\n\ndata Type : Set where\n nats : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n\u27e8\u2205\u27e9 : \u2205 \u2261 \u2205\n\u27e8\u2205\u27e9 = refl\n\n_\u27e8\u2022\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} \u2192 \u03c4\u2081 \u2261 \u03c4\u2082 \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u03c4\u2081 \u2022 \u0393\u2081 \u2261 \u03c4\u2082 \u2022 \u0393\u2082\n_\u27e8\u2022\u27e9_ = cong\u2082 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n -- Change to nats = replacement Church pairs\n -- 3 -> 5 ::= \u03bbf. f 3 5\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u227c-reflexivity : Reflexive _\u227c_\n\u227c-reflexivity {\u2205} = \u2205\u227c\u2205\n\u227c-reflexivity {\u03c4 \u2022 x} = keep \u03c4 \u2022 \u227c-reflexivity\n\n\u227c-reflexive : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u0393\u2081 \u227c \u0393\u2082\n\u227c-reflexive refl = \u227c-reflexivity\n\n\u227c-transitive : Transitive _\u227c_\n\u227c-transitive \u2205\u227c\u2205 rel1 = rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n keep \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (keep \u03c4 \u2022 rel0) rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n drop \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (drop \u03c4 \u2022 rel0) rel1\n\n\u227c-isPreorder : IsPreorder _\u2261_ _\u227c_\n\u227c-isPreorder = record\n { isEquivalence = isEquivalence\n ; reflexive = \u227c-reflexive\n ; trans = \u227c-transitive\n }\n\n\u227c-preorder : Preorder _ _ _\n\u227c-preorder = record\n { Carrier = Context\n ; _\u2248_ = _\u2261_\n ; isPreorder = \u227c-isPreorder\n }\n\nmodule \u227c-reasoning where\n open import Relation.Binary.PreorderReasoning \u227c-preorder public\n renaming\n ( _\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_\n ; _\u223c\u27e8_\u27e9_ to _\u227c\u27e8_\u27e9_\n ; _\u2248\u27e8\u27e9_ to _\u2261\u27e8\u27e9_\n )\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\n\nrecord Meaning (Syntax : Set) {\u2113 : Level} : Set (suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 nats \u27e7Type = \u2115\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = cong\u2082 (\u03bb x y \u2192 x y)\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = \u2261-app (weaken-sound t\u2081 \u03c1) (weaken-sound t\u2082 \u03c1)\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\n-- Changes to \u2115 are replacement Church pairs. The only arguments\n-- of conern are `fst` and `snd`, so the Church pairs don't have\n-- to be polymorphic.\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type nats = (nats \u21d2 nats \u21d2 nats) \u21d2 nats\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\n-- It is clear that \u27e6\u229d\u27e7 exists on the semantic level:\n-- there exists an Agda value to describe the change between any\n-- two Agda values denoted by terms. If we have (not dependently-\n-- typed) arrays, no term denotes the change between two arrays\n-- of different lengths. Thus no full abstraction.\n\n\u27e6derive\u27e7 : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ninfixl 6 _\u27e6\u2295\u27e7_\ninfixl 6 _\u27e6\u229d\u27e7_\n\n\u27e6fst\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e6snd\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\n\u27e6derive\u27e7 {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 f (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f v\n\u27e6derive\u27e7 {nats} n = \u03bb f \u2192 f n n\n\n_\u27e6\u229d\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 f\u2081 (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f\u2082 v\n_\u27e6\u229d\u27e7_ {nats} m n = \u03bb f \u2192 f n m\n-- m \u27e6\u229d\u27e7 n ::= replace n by m\n\n_\u27e6\u2295\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n_\u27e6\u2295\u27e7_ {nats} n dn = dn \u27e6snd\u27e7\n\n\u27e6fst\u27e7 m n = m\n\u27e6snd\u27e7 m n = n\n\n-- Strong validity!\ndata \u27e6Valid\u0394\u27e7 : {\u03c4 : Type} \u2192 (v : \u27e6 \u03c4 \u27e7) \u2192 (dv : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n -- Following Pierce's case names `T-Var`, `T-Abs`, `T-App`\n -- with Agda's capitalization convention\n -- generalized to the semantic (value) domain\n v-nat : (n : \u2115) \u2192 (dn : (\u2115 \u2192 \u2115 \u2192 \u2115) \u2192 \u2115) \u2192\n n \u2261 dn \u27e6fst\u27e7 \u2192\n \u27e6Valid\u0394\u27e7 n dn\n\n v-fun : {\u03c4\u2081 \u03c4\u2082 : Type} \u2192\n (f : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) \u2192 (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) \u2192\n -- A strong antecedent: f and df map invalid changes to\n -- valid changes! So long as invalid changes exist,\n -- this is NOT satisfied by\n --\n -- f = \u27e6 \u03bb x. x \u27e7 = identity\n -- df = \u27e6 \u03bb x dx. dx \u27e7 = \u27e6snd\u27e7,\n --\n -- negating any hope of \u27e6Valid\u0394\u27e7 \u27e6 t \u27e7 \u27e6 derive t \u27e7.\n (\u2200 {s ds} \u2192 {-Valid\u0394 s ds \u2192-} \u27e6Valid\u0394\u27e7 (f s) (df s ds)) \u2192\n (\u2200 {s ds} \u2192 (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 (f s \u27e6\u2295\u27e7 df s ds)) \u2192\n \u27e6Valid\u0394\u27e7 f df\n\nabsurdity-of-0=1 : 0 \u2261 1 \u2192 (\u2200 {A : Set} \u2192 A)\nabsurdity-of-0=1 ()\n\nit-is-absurd-that : \u27e6Valid\u0394\u27e7 {nats} 0 (\u03bb f \u2192 f 1 1) \u2192 (\u2200 {A : Set} \u2192 A)\nit-is-absurd-that (v-nat .0 .(\u03bb f \u2192 f 1 1) 0=1) = absurdity-of-0=1 0=1\n\n-- If (\u03bb x dx \u2192 dx) is a \u27e6Valid\u0394\u27e7 to (\u03bb x \u2192 x),\n-- then impossible is nothing.\nno-way : \u27e6Valid\u0394\u27e7 {nats \u21d2 nats} (\u03bb x \u2192 x) (\u03bb x dx \u2192 dx) \u2192\n (\u2200 {A : Set} \u2192 A)\nno-way (v-fun .(\u03bb x \u2192 x) .(\u03bb x dx \u2192 dx) validity correctness) =\n it-is-absurd-that (validity {0} {\u03bb f \u2192 f 1 1})\n\n-- Cool lemmas\n\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u229d\u27e7 f \u2261 \u27e6derive\u27e7 f\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {nats} f = refl\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = refl\n\nf\u2295[g\u229df]=g : \u2200 {\u03c4 : Type} (f g : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) \u2261 g\n\nf\u2295\u0394f=f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (\u27e6derive\u27e7 f) \u2261 f\n\nf\u2295[g\u229df]=g {nats} m n = refl\nf\u2295[g\u229df]=g {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = f x} {y = f x} refl\n (cong\u2082 _\u27e6\u229d\u27e7_ (cong g (f\u2295\u0394f=f x)) refl) \u27e9\n f x \u27e6\u2295\u27e7 (g x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (g x) \u27e9\n g x\n \u220e\n ) where open \u2261-Reasoning\n\nf\u2295\u0394f=f {nats} n = refl\nf\u2295\u0394f=f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 \u27e6derive\u27e7 f) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (f (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong (\u03bb hole \u2192 f x \u27e6\u2295\u27e7 (f hole \u27e6\u229d\u27e7 f x)) (f\u2295\u0394f=f x) \u27e9\n f x \u27e6\u2295\u27e7 (f x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (f x) \u27e9\n f x\n \u220e\n ) where open \u2261-Reasoning\n\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {nats} n dn = n \u2261 dn \u27e6fst\u27e7\nvalid-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (s : \u27e6 \u03c4\u2081 \u27e7) (ds : \u27e6 \u0394-Type \u03c4\u2081 \u27e7) (R[s,ds] : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7 -- (valid-\u0394:1)\n (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 f s \u27e6\u2295\u27e7 df s ds -- (valid-\u0394:2)\n\nR[f,g\u229df] : \u2200 {\u03c4} (f g : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (g \u27e6\u229d\u27e7 f)\nR[f,g\u229df] {nats} m n = refl\nR[f,g\u229df] {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb x dx R[x,dx] \u2192\n R[f,g\u229df] {\u03c4\u2082} (f x) (g (x \u27e6\u2295\u27e7 dx)) -- (valid-\u0394:1)\n , -- tuple constructor\n (begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 refl \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7\n (g ((x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 \u27e6derive\u27e7 (x \u27e6\u2295\u27e7 dx)) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 cong (\u03bb hole \u2192 f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g hole \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx)))\n (f\u2295\u0394f=f (x \u27e6\u2295\u27e7 dx)) \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 f\u2295[g\u229df]=g (f (x \u27e6\u2295\u27e7 dx)) (g (x \u27e6\u2295\u27e7 dx)) \u27e9\n g (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (f x) (g (x \u27e6\u2295\u27e7 dx))) \u27e9\n f x \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) x dx\n \u220e)\n where open \u2261-Reasoning\n\nR[f,\u0394f] : \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (\u27e6derive\u27e7 f)\nR[f,\u0394f] f rewrite sym (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f f) = R[f,g\u229df] f f\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393 -- push \u03c4, then push \u0394\u03c4.\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 (keep \u03c4 \u2022 \u0393\u227c\u0394\u0393)\n\n-- Data type to hold proofs that environments are valid\ndata Valid-\u0394env : {\u0393 : Context} (\u03c1 : \u27e6 \u0393 \u27e7) (\u0394\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is its own valid \u0394-environment.\n \u03c1=\u2205 : Valid-\u0394env {\u2205} \u2205 \u2205\n -- Induction case: the change introduced therein should be valid,\n -- and the smaller \u0394-environment should be valid as well.\n \u03c1=v\u2022\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {\u03c1\u2080 : \u27e6 \u0393\u2080 \u27e7} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Valid-\u0394env \u03c1\u2080 d\u03c1\u2080 \u2192\n Valid-\u0394env {\u03c4 \u2022 \u0393\u2080} (v \u2022 \u03c1\u2080) (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- Data type to hold proofs that a \u0394-env is consistent with self\ndata Consistent-\u0394env : {\u0393 : Context} (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is consistent with itself\n d\u03c1=\u2205 : Consistent-\u0394env {\u2205} \u2205\n -- Induction case: the change introduced at top level\n -- should be valid.\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Consistent-\u0394env d\u03c1\u2080 \u2192\n Consistent-\u0394env {\u03c4 \u2022 \u0393\u2080} (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- If a \u0394-environment is valid for some other environment,\n-- then it is also consistent with itself.\n\nvalid-\u0394env-is-consistent :\n \u2200 {\u0393 : Context} {\u03c1 : \u27e6 \u0393 \u27e7} {d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Valid-\u0394env \u03c1 d\u03c1 \u2192 Consistent-\u0394env d\u03c1\n\nvalid-\u0394env-is-consistent \u03c1=\u2205 = d\u03c1=\u2205\nvalid-\u0394env-is-consistent (\u03c1=v\u2022\u03c1\u2080 valid[v,dv] valid[\u03c1\u2080,d\u03c1\u2080]) =\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] (valid-\u0394env-is-consistent valid[\u03c1\u2080,d\u03c1\u2080])\n\n-- finally, update and ignore\n\nupdate : \u2200 {\u0393} \u2192 (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env d\u03c1} \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 {d\u03c1=\u2205} = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 d\u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] consistent[d\u03c1\u2080]} =\n (v \u27e6\u2295\u27e7 dv) \u2022 update d\u03c1 {consistent[d\u03c1\u2080]}\n\n-- Ignorance is bliss (don't have to pass consistency proofs around :D)\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 -- Using a proof to describe computation\n\n-- Naming scheme follows weakenVar\/weaken\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- derive(n) = \u03bbf. f n n\nderive (nat n) = abs (app (app (var this) (nat n)) (nat n))\n\n-- derive(x) = dx\nderive (var x) = var (deriveVar x)\n\n-- derive(\u03bbx. t) = \u03bbx. \u03bbdx. derive(t)\nderive (abs t) = abs (abs (derive t))\n\n-- derive(f s) = derive(f) s derive(s)\nderive (app f s) = app (app (derive f) (weaken \u0393\u227c\u0394\u0393 s)) (derive s)\n\n-- Extensional equivalence for changes\ndata as-\u0394_is_ext-equiv-to_ :\n \u2200 (\u03c4 : Type) \u2192 (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 (dg : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n ext-\u0394 : \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n (\u2200 (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192\n (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)) \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg\n\ninfix 3 as-\u0394_is_ext-equiv-to_\n\n-- Extractor for extensional-equivalence-as-changes:\n-- Given a value of the data type holding the proof,\n-- returns the proof in applicable form.\n--\n-- Question: Would it not have been better if such\n-- proof-holding types were defined as a function in\n-- the first place, say in the manner of `valid-\u0394`?\n--\nextract-\u0394equiv :\n \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg \u2192\n (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192 (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)\n\nextract-\u0394equiv (ext-\u0394 proof-method) = proof-method\n\n-- Distribution lemmas of validity over \u27e6\u229d\u27e7 and \u27e6\u2295\u27e7\n\napplication-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v) \u2261 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\napplication-over-\u2295-and-\u229d f g df v =\n begin\n f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (cong\u2082 _\u27e6\u2295\u27e7_\n (cong f ((sym (f\u2295\u0394f=f v))))\n (cong\u2082 df\n (sym (f\u2295\u0394f=f v))\n (cong \u27e6derive\u27e7 (sym (f\u2295\u0394f=f v)))))\n refl \u27e9\n f (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v)\n \u27e6\u2295\u27e7 df (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v) (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v))\n \u27e6\u229d\u27e7 f v\n \u2261\u27e8 refl \u27e9\n (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e where open \u2261-Reasoning\n\nvalidity-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n valid-\u0394 g (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) \u2192\n valid-\u0394 (g v) (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\nvalidity-over-\u2295-and-\u229d f g df v R[g,f\u2295df]\n rewrite application-over-\u2295-and-\u229d f g df v =\n proj\u2081 (R[g,f\u2295df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))\n\n-- diff-apply\ndf=f\u2295df\u229df :\n \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n valid-\u0394 f df \u2192 as-\u0394 \u03c4 is df ext-equiv-to f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f\n\n-- Case nat: this REFL is more obvious to Agda than to a human.\ndf=f\u2295df\u229df {nats} n dn valid-f-df =\n ext-\u0394 (\u03bb m valid-m-dn valid-rhs \u2192 refl)\n\ndf=f\u2295df\u229df {\u03c4\u2081 \u21d2 \u03c4\u2082} f df R[f,df] = ext-\u0394 (\n \u03bb g R[g,df] R[g,f\u2295df\u229df] \u2192\n extensionality (\u03bb v \u2192\n begin\n g v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n \u2261\u27e8 extract-\u0394equiv\n (df=f\u2295df\u229df\n (f v) (df v (\u27e6derive\u27e7 v))\n (proj\u2081 (R[f,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))))\n (g v)\n (proj\u2081 (R[g,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v)))\n (validity-over-\u2295-and-\u229d f g df v R[g,f\u2295df\u229df]) \u27e9\n g v \u27e6\u2295\u27e7 (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = g v} {y = g v} refl\n (application-over-\u2295-and-\u229d f g df v) \u27e9\n g v \u27e6\u2295\u27e7 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e\n )\n )\n where open \u2261-Reasoning\n\n\n\ncorrectness-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 deriveVar x \u27e7 \u03c1\n ext-equiv-to\n \u27e6 x \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 x \u27e7 (ignore \u03c1)\n\ncorrectness-of-deriveVar {\u03c4 \u2022 \u0393\u2080} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n this = df=f\u2295df\u229df {\u03c4} v dv R[v,dv]\n\ncorrectness-of-deriveVar {\u03c4\u2080 \u2022 \u0393\u2080} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4\u2080} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n (that x) = correctness-of-deriveVar \u03c1 x\n\n\n\ncorrectness-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 derive t \u27e7 \u03c1\n ext-equiv-to\n \u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 t \u27e7 (ignore \u03c1)\n\n-- Mutually recursive lemma: derivatives are valid\nvalidity-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 deriveVar x \u27e7 \u03c1)\n\nvalidity-of-deriveVar {\u03c4 \u2022 \u0393} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n this = R[v,dv]\n\nvalidity-of-deriveVar {\u03c4\u2080 \u2022 \u0393} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n (that x) = validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 {consistency} (var x) =\n validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 (nat n) = refl\n\nvalidity-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs t) = \u03bb v dv R[v,dv] \u2192\n validity-of-derive (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency} t\n , -- tuple constructor\n (begin\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n ((R[f,g\u229df] (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 f\u2295[g\u229df]=g (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u2261\u27e8 cong \u27e6 t \u27e7 (cong\u2082 _\u2022_ (f\u2295\u0394f=f (v \u27e6\u2295\u27e7 dv)) refl) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency}))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 sym (extract-\u0394equiv\n (correctness-of-derive\n ((dv \u2022 v \u2022 \u03c1))\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (R[f,g\u229df] (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u220e)\n where open \u2261-Reasoning\n\nvalidity-of-derive \u03c1 (app t t\u2081) = {!!}\n\ncorrectness-of-derive \u03c1 (var x) = correctness-of-deriveVar \u03c1 x\n\ncorrectness-of-derive \u03c1 (nat n) = ext-\u0394 (\u03bb _ _ _ \u2192 refl)\n\ncorrectness-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs {.\u03c4\u2081} {.\u03c4\u2082} t) =\n ext-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n extensionality {\u27e6 \u03c4\u2081 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4\u2082 \u27e7} (\u03bb x \u2192\n begin\n f x \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive {\u03c4\u2081 \u2022 \u0393} {\u03c4\u2082}\n (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency}\n t)\n (f x)\n (proj\u2081 (R[f,\u0394t] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x)))\n (proj\u2081 (R[f,t\u2032\u229dt] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x))) \u27e9\n f x\n \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 (update (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency})\n \u27e6\u229d\u27e7 \u27e6 t \u27e7 (x \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u220e\n )) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 (app {\u03c4\u2081} {\u03c4\u2082} {\u0393} t\u2081 t\u2082) =\n ext-\u0394 {\u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n begin\n f \u27e6\u2295\u27e7 \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n \u2261\u27e8 {!!} \u27e9\n f \u27e6\u2295\u27e7\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n \u220e\n ) where open \u2261-Reasoning\n\n","old_contents":"-- TODO: Include proof of\n-- \"not all correctly-typed values are valid changes\"\n\nmodule A where\n\nopen import Data.Product\nopen import Data.Nat using (\u2115)\nopen import Data.Unit using (\u22a4)\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Level using\n (zero ; Level ; suc)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\npostulate extensionality : Extensionality zero zero\n\ndata Type : Set where\n nats : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n\u27e8\u2205\u27e9 : \u2205 \u2261 \u2205\n\u27e8\u2205\u27e9 = refl\n\n_\u27e8\u2022\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393\u2081 \u0393\u2082} \u2192 \u03c4\u2081 \u2261 \u03c4\u2082 \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u03c4\u2081 \u2022 \u0393\u2081 \u2261 \u03c4\u2082 \u2022 \u0393\u2082\n_\u27e8\u2022\u27e9_ = cong\u2082 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n nat : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 Term \u0393 nats\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n -- Change to nats = replacement Church pairs\n -- 3 -> 5 ::= \u03bbf. f 3 5\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\n\u227c-reflexivity : Reflexive _\u227c_\n\u227c-reflexivity {\u2205} = \u2205\u227c\u2205\n\u227c-reflexivity {\u03c4 \u2022 x} = keep \u03c4 \u2022 \u227c-reflexivity\n\n\u227c-reflexive : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u2261 \u0393\u2082 \u2192 \u0393\u2081 \u227c \u0393\u2082\n\u227c-reflexive refl = \u227c-reflexivity\n\n\u227c-transitive : Transitive _\u227c_\n\u227c-transitive \u2205\u227c\u2205 rel1 = rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n keep \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (keep \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (keep \u03c4 \u2022 rel0) rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (keep .\u03c4 \u2022 rel1) =\n drop \u03c4 \u2022 \u227c-transitive rel0 rel1\n\u227c-transitive (drop \u03c4 \u2022 rel0) (drop \u03c4\u2081 \u2022 rel1) =\n drop \u03c4\u2081 \u2022 \u227c-transitive (drop \u03c4 \u2022 rel0) rel1\n\n\u227c-isPreorder : IsPreorder _\u2261_ _\u227c_\n\u227c-isPreorder = record\n { isEquivalence = isEquivalence\n ; reflexive = \u227c-reflexive\n ; trans = \u227c-transitive\n }\n\n\u227c-preorder : Preorder _ _ _\n\u227c-preorder = record\n { Carrier = Context\n ; _\u2248_ = _\u2261_\n ; isPreorder = \u227c-isPreorder\n }\n\nmodule \u227c-reasoning where\n open import Relation.Binary.PreorderReasoning \u227c-preorder public\n renaming\n ( _\u2248\u27e8_\u27e9_ to _\u2261\u27e8_\u27e9_\n ; _\u223c\u27e8_\u27e9_ to _\u227c\u27e8_\u27e9_\n ; _\u2248\u27e8\u27e9_ to _\u2261\u27e8\u27e9_\n )\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (nat x) = nat x\n\nrecord Meaning (Syntax : Set) {\u2113 : Level} : Set (suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 nats \u27e7Type = \u2115\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 nat n \u27e7Term \u03c1 = n\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = cong\u2082 (\u03bb x y \u2192 x y)\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = \u2261-app (weaken-sound t\u2081 \u03c1) (weaken-sound t\u2082 \u03c1)\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (nat n) \u03c1 = refl\n\n-- Changes to \u2115 are replacement Church pairs. The only arguments\n-- of conern are `fst` and `snd`, so the Church pairs don't have\n-- to be polymorphic.\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type nats = (nats \u21d2 nats \u21d2 nats) \u21d2 nats\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\n-- It is clear that \u27e6\u229d\u27e7 exists on the semantic level:\n-- there exists an Agda value to describe the change between any\n-- two Agda values denoted by terms. If we have (not dependently-\n-- typed) arrays, no term denotes the change between two arrays\n-- of different lengths. Thus no full abstraction.\n\n\u27e6derive\u27e7 : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ninfixl 6 _\u27e6\u2295\u27e7_\ninfixl 6 _\u27e6\u229d\u27e7_\n\n\u27e6fst\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e6snd\u27e7 : \u2115 \u2192 \u2115 \u2192 \u2115\n\n\u27e6derive\u27e7 {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 f (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f v\n\u27e6derive\u27e7 {nats} n = \u03bb f \u2192 f n n\n\n_\u27e6\u229d\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 f\u2081 (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f\u2082 v\n_\u27e6\u229d\u27e7_ {nats} m n = \u03bb f \u2192 f n m\n-- m \u27e6\u229d\u27e7 n ::= replace n by m\n\n_\u27e6\u2295\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n_\u27e6\u2295\u27e7_ {nats} n dn = dn \u27e6snd\u27e7\n\n\u27e6fst\u27e7 m n = m\n\u27e6snd\u27e7 m n = n\n\n-- Strong validity!\ndata \u27e6Valid\u0394\u27e7 : {\u03c4 : Type} \u2192 (v : \u27e6 \u03c4 \u27e7) \u2192 (dv : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n -- Following Pierce's case names `T-Var`, `T-Abs`, `T-App`\n -- with Agda's capitalization convention\n -- generalized to the semantic (value) domain\n v-nat : (n : \u2115) \u2192 (dn : (\u2115 \u2192 \u2115 \u2192 \u2115) \u2192 \u2115) \u2192\n n \u2261 dn \u27e6fst\u27e7 \u2192\n \u27e6Valid\u0394\u27e7 n dn\n\n v-fun : {\u03c4\u2081 \u03c4\u2082 : Type} \u2192\n (f : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) \u2192 (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) \u2192\n -- A strong antecedent: f and df map invalid changes to\n -- valid changes! So long as invalid changes exist,\n -- this is NOT satisfied by\n --\n -- f = \u27e6 \u03bb x. x \u27e7 = identity\n -- df = \u27e6 \u03bb x dx. dx \u27e7 = \u27e6snd\u27e7,\n --\n -- negating any hope of \u27e6Valid\u0394\u27e7 \u27e6 t \u27e7 \u27e6 derive t \u27e7.\n (\u2200 {s ds} \u2192 {-Valid\u0394 s ds \u2192-} \u27e6Valid\u0394\u27e7 (f s) (df s ds)) \u2192\n (\u2200 {s ds} \u2192 (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 (f s \u27e6\u2295\u27e7 df s ds)) \u2192\n \u27e6Valid\u0394\u27e7 f df\n\nabsurdity-of-0=1 : 0 \u2261 1 \u2192 (\u2200 {A : Set} \u2192 A)\nabsurdity-of-0=1 ()\n\nit-is-absurd-that : \u27e6Valid\u0394\u27e7 {nats} 0 (\u03bb f \u2192 f 1 1) \u2192 (\u2200 {A : Set} \u2192 A)\nit-is-absurd-that (v-nat .0 .(\u03bb f \u2192 f 1 1) 0=1) = absurdity-of-0=1 0=1\n\n-- If (\u03bb x dx \u2192 dx) is a \u27e6Valid\u0394\u27e7 to (\u03bb x \u2192 x),\n-- then impossible is nothing.\nno-way : \u27e6Valid\u0394\u27e7 {nats \u21d2 nats} (\u03bb x \u2192 x) (\u03bb x dx \u2192 dx) \u2192\n (\u2200 {A : Set} \u2192 A)\nno-way (v-fun .(\u03bb x \u2192 x) .(\u03bb x dx \u2192 dx) validity correctness) =\n it-is-absurd-that (validity {0} {\u03bb f \u2192 f 1 1})\n\n-- Cool lemmas\n\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u229d\u27e7 f \u2261 \u27e6derive\u27e7 f\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {nats} f = refl\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = refl\n\nf\u2295[g\u229df]=g : \u2200 {\u03c4 : Type} (f g : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) \u2261 g\n\nf\u2295\u0394f=f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (\u27e6derive\u27e7 f) \u2261 f\n\nf\u2295[g\u229df]=g {nats} m n = refl\nf\u2295[g\u229df]=g {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = f x} {y = f x} refl\n (cong\u2082 _\u27e6\u229d\u27e7_ (cong g (f\u2295\u0394f=f x)) refl) \u27e9\n f x \u27e6\u2295\u27e7 (g x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (g x) \u27e9\n g x\n \u220e\n ) where open \u2261-Reasoning\n\nf\u2295\u0394f=f {nats} n = refl\nf\u2295\u0394f=f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 \u27e6derive\u27e7 f) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (f (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong (\u03bb hole \u2192 f x \u27e6\u2295\u27e7 (f hole \u27e6\u229d\u27e7 f x)) (f\u2295\u0394f=f x) \u27e9\n f x \u27e6\u2295\u27e7 (f x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (f x) \u27e9\n f x\n \u220e\n ) where open \u2261-Reasoning\n\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {nats} n dn = n \u2261 dn \u27e6fst\u27e7\nvalid-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (s : \u27e6 \u03c4\u2081 \u27e7) (ds : \u27e6 \u0394-Type \u03c4\u2081 \u27e7) (R[s,ds] : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7 -- (valid-\u0394:1)\n (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 f s \u27e6\u2295\u27e7 df s ds -- (valid-\u0394:2)\n\nR[f,g\u229df] : \u2200 {\u03c4} (f g : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (g \u27e6\u229d\u27e7 f)\nR[f,g\u229df] {nats} m n = refl\nR[f,g\u229df] {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb x dx R[x,dx] \u2192\n R[f,g\u229df] {\u03c4\u2082} (f x) (g (x \u27e6\u2295\u27e7 dx)) -- (valid-\u0394:1)\n , -- tuple constructor\n (begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 refl \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7\n (g ((x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 \u27e6derive\u27e7 (x \u27e6\u2295\u27e7 dx)) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 cong (\u03bb hole \u2192 f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g hole \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx)))\n (f\u2295\u0394f=f (x \u27e6\u2295\u27e7 dx)) \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 f\u2295[g\u229df]=g (f (x \u27e6\u2295\u27e7 dx)) (g (x \u27e6\u2295\u27e7 dx)) \u27e9\n g (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (f x) (g (x \u27e6\u2295\u27e7 dx))) \u27e9\n f x \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) x dx\n \u220e)\n where open \u2261-Reasoning\n\nR[f,\u0394f] : \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (\u27e6derive\u27e7 f)\nR[f,\u0394f] f rewrite sym (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f f) = R[f,g\u229df] f f\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393 -- push \u03c4, then push \u0394\u03c4.\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 (keep \u03c4 \u2022 \u0393\u227c\u0394\u0393)\n\n-- Data type to hold proofs that environments are valid\ndata Valid-\u0394env : {\u0393 : Context} (\u03c1 : \u27e6 \u0393 \u27e7) (\u0394\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is its own valid \u0394-environment.\n \u03c1=\u2205 : Valid-\u0394env {\u2205} \u2205 \u2205\n -- Induction case: the change introduced therein should be valid,\n -- and the smaller \u0394-environment should be valid as well.\n \u03c1=v\u2022\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {\u03c1\u2080 : \u27e6 \u0393\u2080 \u27e7} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Valid-\u0394env \u03c1\u2080 d\u03c1\u2080 \u2192\n Valid-\u0394env {\u03c4 \u2022 \u0393\u2080} (v \u2022 \u03c1\u2080) (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- Data type to hold proofs that a \u0394-env is consistent with self\ndata Consistent-\u0394env : {\u0393 : Context} (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is consistent with itself\n d\u03c1=\u2205 : Consistent-\u0394env {\u2205} \u2205\n -- Induction case: the change introduced at top level\n -- should be valid.\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Consistent-\u0394env d\u03c1\u2080 \u2192\n Consistent-\u0394env {\u03c4 \u2022 \u0393\u2080} (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- If a \u0394-environment is valid for some other environment,\n-- then it is also consistent with itself.\n\nvalid-\u0394env-is-consistent :\n \u2200 {\u0393 : Context} {\u03c1 : \u27e6 \u0393 \u27e7} {d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Valid-\u0394env \u03c1 d\u03c1 \u2192 Consistent-\u0394env d\u03c1\n\nvalid-\u0394env-is-consistent \u03c1=\u2205 = d\u03c1=\u2205\nvalid-\u0394env-is-consistent (\u03c1=v\u2022\u03c1\u2080 valid[v,dv] valid[\u03c1\u2080,d\u03c1\u2080]) =\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] (valid-\u0394env-is-consistent valid[\u03c1\u2080,d\u03c1\u2080])\n\n-- finally, update and ignore\n\nupdate : \u2200 {\u0393} \u2192 (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env d\u03c1} \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 {d\u03c1=\u2205} = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 d\u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] consistent[d\u03c1\u2080]} =\n (v \u27e6\u2295\u27e7 dv) \u2022 update d\u03c1 {consistent[d\u03c1\u2080]}\n\n-- Ignorance is bliss (don't have to pass consistency proofs around :D)\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 -- Using a proof to describe computation\n\n-- Naming scheme follows weakenVar\/weaken\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- derive(n) = \u03bbf. f n n\nderive (nat n) = abs (app (app (var this) (nat n)) (nat n))\n\n-- derive(x) = dx\nderive (var x) = var (deriveVar x)\n\n-- derive(\u03bbx. t) = \u03bbx. \u03bbdx. derive(t)\nderive (abs t) = abs (abs (derive t))\n\n-- derive(f s) = derive(f) s derive(s)\nderive (app f s) = app (app (derive f) (weaken \u0393\u227c\u0394\u0393 s)) (derive s)\n\n-- Extensional equivalence for changes\ndata as-\u0394_is_ext-equiv-to_ :\n \u2200 (\u03c4 : Type) \u2192 (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 (dg : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n ext-\u0394 : \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n (\u2200 (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192\n (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)) \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg\n\ninfix 3 as-\u0394_is_ext-equiv-to_\n\n-- Extractor for extensional-equivalence-as-changes:\n-- Given a value of the data type holding the proof,\n-- returns the proof in applicable form.\n--\n-- Question: Would it not have been better if such\n-- proof-holding types were defined as a function in\n-- the first place, say in the manner of `valid-\u0394`?\n--\nextract-\u0394equiv :\n \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n as-\u0394 \u03c4 is df ext-equiv-to dg \u2192\n (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192 (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)\n\nextract-\u0394equiv (ext-\u0394 proof-method) = proof-method\n\n-- Distribution lemmas of validity over \u27e6\u229d\u27e7 and \u27e6\u2295\u27e7\n\napplication-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v) \u2261 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\napplication-over-\u2295-and-\u229d f g df v =\n begin\n f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (cong\u2082 _\u27e6\u2295\u27e7_\n (cong f ((sym (f\u2295\u0394f=f v))))\n (cong\u2082 df\n (sym (f\u2295\u0394f=f v))\n (cong \u27e6derive\u27e7 (sym (f\u2295\u0394f=f v)))))\n refl \u27e9\n f (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v)\n \u27e6\u2295\u27e7 df (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v) (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v))\n \u27e6\u229d\u27e7 f v\n \u2261\u27e8 refl \u27e9\n (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e where open \u2261-Reasoning\n\nvalidity-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n valid-\u0394 g (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) \u2192\n valid-\u0394 (g v) (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\nvalidity-over-\u2295-and-\u229d f g df v R[g,f\u2295df]\n rewrite application-over-\u2295-and-\u229d f g df v =\n proj\u2081 (R[g,f\u2295df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))\n\n-- diff-apply\ndf=f\u2295df\u229df :\n \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n valid-\u0394 f df \u2192 as-\u0394 \u03c4 is df ext-equiv-to f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f\n\n-- Case nat: this REFL is more obvious to Agda than to a human.\ndf=f\u2295df\u229df {nats} n dn valid-f-df =\n ext-\u0394 (\u03bb m valid-m-dn valid-rhs \u2192 refl)\n\ndf=f\u2295df\u229df {\u03c4\u2081 \u21d2 \u03c4\u2082} f df R[f,df] = ext-\u0394 (\n \u03bb g R[g,df] R[g,f\u2295df\u229df] \u2192\n extensionality (\u03bb v \u2192\n begin\n g v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n \u2261\u27e8 extract-\u0394equiv\n (df=f\u2295df\u229df\n (f v) (df v (\u27e6derive\u27e7 v))\n (proj\u2081 (R[f,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))))\n (g v)\n (proj\u2081 (R[g,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v)))\n (validity-over-\u2295-and-\u229d f g df v R[g,f\u2295df\u229df]) \u27e9\n g v \u27e6\u2295\u27e7 (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = g v} {y = g v} refl\n (application-over-\u2295-and-\u229d f g df v) \u27e9\n g v \u27e6\u2295\u27e7 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e\n )\n )\n where open \u2261-Reasoning\n\n\n\ncorrectness-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 deriveVar x \u27e7 \u03c1\n ext-equiv-to\n \u27e6 x \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 x \u27e7 (ignore \u03c1)\n\ncorrectness-of-deriveVar {\u03c4 \u2022 \u0393\u2080} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n this = df=f\u2295df\u229df {\u03c4} v dv R[v,dv]\n --Putting `df=f\u2295df\u229df v dv ?` on RHS triggers Agda bug.\n --TODO: Report it as Agda tells us to.\n\ncorrectness-of-deriveVar {\u03c4\u2080 \u2022 \u0393\u2080} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4\u2080} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n (that x) = correctness-of-deriveVar \u03c1 x\n\n\n\ncorrectness-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n as-\u0394 \u03c4 is\n \u27e6 derive t \u27e7 \u03c1\n ext-equiv-to\n \u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 t \u27e7 (ignore \u03c1)\n\n-- Mutually recursive lemma: derivatives are valid\nvalidity-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 deriveVar x \u27e7 \u03c1)\n\nvalidity-of-deriveVar {\u03c4 \u2022 \u0393} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n this = R[v,dv]\n\nvalidity-of-deriveVar {\u03c4\u2080 \u2022 \u0393} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n (that x) = validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 {consistency} (var x) =\n validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 (nat n) = refl\n\nvalidity-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs t) = \u03bb v dv R[v,dv] \u2192\n validity-of-derive (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency} t\n , -- tuple constructor\n (begin\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n \u2261\u27e8 {!!} \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)\n \u27e6\u2295\u27e7 {!!}\n \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n\n \u2261\u27e8 {!!} \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7\u227c \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u220e)\n where open \u2261-Reasoning\n\nvalidity-of-derive \u03c1 (app t t\u2081) = {!!}\n\ncorrectness-of-derive \u03c1 (var x) = correctness-of-deriveVar \u03c1 x\n\ncorrectness-of-derive \u03c1 (nat n) = ext-\u0394 (\u03bb _ _ _ \u2192 refl)\n\ncorrectness-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs {.\u03c4\u2081} {.\u03c4\u2082} t) =\n ext-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n extensionality {\u27e6 \u03c4\u2081 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4\u2082 \u27e7} (\u03bb x \u2192\n begin\n f x \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive {\u03c4\u2081 \u2022 \u0393} {\u03c4\u2082}\n (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency}\n t)\n (f x)\n (proj\u2081 (R[f,\u0394t] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x)))\n (proj\u2081 (R[f,t\u2032\u229dt] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x))) \u27e9\n f x\n \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 (update (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency})\n \u27e6\u229d\u27e7 \u27e6 t \u27e7 (x \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u220e\n )) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 (app {\u03c4\u2081} {\u03c4\u2082} {\u0393} t\u2081 t\u2082) =\n ext-\u0394 {\u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n begin\n f \u27e6\u2295\u27e7 \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n \u2261\u27e8 {!!} \u27e9\n f \u27e6\u2295\u27e7\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n \u220e\n ) where open \u2261-Reasoning\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9e3f211e88b705e21001d83b692c5504a15a4f1a","subject":"Data.Bits: use 2^ instead of _^_ 2 in allBits and #\u27e8_\u27e9","message":"Data.Bits: use 2^ instead of _^_ 2 in allBits and #\u27e8_\u27e9\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_\u2264_; _==_)\nopen import Data.Nat.DivMod\nopen import Data.Bool.NP hiding (_==_)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise)\nopen import Data.Vec.NP hiding (_\u229b_) renaming (map to vmap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_)\nopen import Data.Vec.NP public using ([]; _\u2237_; head; tail; replicate)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x = cong (_\u2237_ x) (vnot\u2218vnot\u2257id xs)\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n\n where open RawMonad L.monad\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9 = count pred (allBits _)\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nopen Defs public\n","old_contents":"module Data.Bits where\n\n-- cleanup\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_\u2264_; _==_)\nopen import Data.Nat.DivMod\nopen import Data.Bool.NP hiding (_==_)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise)\nopen import Data.Vec.NP hiding (_\u229b_) renaming (map to vmap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_)\nopen import Data.Vec.NP public using ([]; _\u2237_; head; tail; replicate)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x = cong (_\u2237_ x) (vnot\u2218vnot\u2257id xs)\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n\n where open RawMonad L.monad\nallBits : \u2200 n \u2192 Vec (Bits n) (2 ^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) rewrite \u2115\u00b0.+-comm (2 ^ n) 0 = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2 ^ n))\n#\u27e8 pred \u27e9 = count pred (allBits _)\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nopen Defs public\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"5e52be904bd2a54d670b67f500b2a286bace1ddf","subject":"lemma churn","message":"lemma churn\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"typed-expansion.agda","new_file":"typed-expansion.agda","new_contents":"","old_contents":"","returncode":0,"stderr":"unknown","license":"mit","lang":"Agda"} {"commit":"de7e98cf756a7d615f97b80685caf879cc0c9211","subject":"cleaning up a little and documenting why the holes are puzzling #37","message":"cleaning up a little and documenting why the holes are puzzling #37\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"complete-expansion.agda","new_file":"complete-expansion.agda","new_contents":"open import Nat\nopen import Prelude\nopen import core\nopen import contexts\n\nopen import correspondence\n\nmodule complete-expansion where\n -- todo remove when you've proven typed expansion\n postulate\n typed-expansion-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4\n typed-expansion-ana : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u03c4' ~ \u03c4) \u00d7 (\u0394 , \u0393 \u22a2 d :: \u03c4')\n\n gcomp-extend : \u2200{\u0393 \u03c4 x} \u2192 \u0393 gcomplete \u2192 \u03c4 tcomplete \u2192 (\u0393 ,, (x , \u03c4)) gcomplete\n gcomp-extend {x = x} gc tc x\u2081 t x\u2082 with natEQ x\u2081 x\n gcomp-extend {\u0393 = \u0393} gc tc x t x\u2083 | Inl refl with \u0393 x\n gcomp-extend gc tc x x\u2081 refl | Inl refl | Some .x\u2081 = {!!} -- stupid context stuff again\n gcomp-extend gc tc x t x\u2083 | Inl refl | None with natEQ x x\n gcomp-extend gc tc x \u03c4 refl | Inl refl | None | Inl refl = tc\n gcomp-extend gc tc x t x\u2083 | Inl refl | None | Inr x\u2081 = abort (somenotnone (! x\u2083))\n gcomp-extend {\u0393 = \u0393} gc tc x\u2081 t x\u2083 | Inr x\u2082 with \u0393 x\u2081\n gcomp-extend gc tc x\u2081 t x\u2084 | Inr x\u2083 | Some x = {!tc!} -- ditto\n gcomp-extend {x = x} gc tc x\u2081 t x\u2083 | Inr x\u2082 | None with natEQ x x\u2081\n gcomp-extend gc tc x t refl | Inr x\u2083 | None | Inl refl = tc\n gcomp-extend gc tc x\u2081 t x\u2084 | Inr x\u2083 | None | Inr x\u2082 = abort (somenotnone (! x\u2084))\n\n -- this might be derivable from things below and a fact about => and ::\n -- that we seem to have not proven\n complete-ta : \u2200{\u0393 \u0394 d \u03c4} \u2192 (\u0393 gcomplete) \u2192 (\u0394 , \u0393 \u22a2 d :: \u03c4) \u2192 d dcomplete \u2192 \u03c4 tcomplete\n complete-ta gc TAConst comp = TCBase\n complete-ta gc (TAVar x\u2081) DCVar = gc _ _ x\u2081\n complete-ta gc (TALam wt) (DCLam comp x\u2081) = TCArr x\u2081 (complete-ta (gcomp-extend gc x\u2081) wt comp)\n complete-ta gc (TAAp wt wt\u2081) (DCAp comp comp\u2081) with complete-ta gc wt comp\n complete-ta gc (TAAp wt wt\u2081) (DCAp comp comp\u2081) | TCArr qq qq\u2081 = qq\u2081\n complete-ta gc (TAEHole x x\u2081) ()\n complete-ta gc (TANEHole x wt x\u2081) ()\n complete-ta gc (TACast wt x) (DCCast comp x\u2081 x\u2082) = x\u2082\n complete-ta gc (TAFailedCast wt x x\u2081 x\u2082) ()\n\n comp-synth : \u2200{\u0393 e \u03c4} \u2192\n \u0393 gcomplete \u2192\n e ecomplete \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u03c4 tcomplete\n comp-synth gc ec SConst = TCBase\n comp-synth gc (ECAsc x ec) (SAsc x\u2081) = x\n comp-synth gc ec (SVar x) = gc _ _ x\n comp-synth gc (ECAp ec ec\u2081) (SAp wt MAHole x\u2081) with comp-synth gc ec wt\n ... | ()\n comp-synth gc (ECAp ec ec\u2081) (SAp wt MAArr x\u2081) with comp-synth gc ec wt\n comp-synth gc (ECAp ec ec\u2081) (SAp wt MAArr x\u2081) | TCArr qq qq\u2081 = qq\u2081\n comp-synth gc () SEHole\n comp-synth gc () (SNEHole wt)\n comp-synth gc (ECLam2 ec x\u2081) (SLam x\u2082 wt) = TCArr x\u2081 (comp-synth (gcomp-extend gc x\u2081) ec wt)\n\n mutual\n complete-expansion-synth : \u2200{e \u03c4 \u0393 \u0394 d} \u2192\n \u0393 gcomplete \u2192\n e ecomplete \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n d dcomplete\n complete-expansion-synth gc ec ESConst = DCConst\n complete-expansion-synth gc ec (ESVar x\u2081) = DCVar\n complete-expansion-synth gc (ECLam2 ec x\u2081) (ESLam x\u2082 exp) = DCLam (complete-expansion-synth (gcomp-extend gc x\u2081) ec exp) x\u2081\n complete-expansion-synth gc (ECAp ec ec\u2081) (ESAp x MAHole x\u2082 x\u2083) with comp-synth gc ec x\n ... | ()\n complete-expansion-synth gc (ECAp ec ec\u2081) (ESAp x MAArr x\u2082 x\u2083)\n with complete-expansion-ana gc ec x\u2082 | complete-expansion-ana gc ec\u2081 x\u2083 | comp-synth gc ec x\n ... | ih1 | ih2 | TCArr c1 c2 = DCAp (DCCast ih1 (comp-ana gc x\u2082 ih1) (TCArr c1 c2))\n (DCCast ih2 (comp-ana gc x\u2083 ih2) c1)\n complete-expansion-synth gc () ESEHole\n complete-expansion-synth gc () (ESNEHole exp)\n complete-expansion-synth gc (ECAsc x ec) (ESAsc x\u2081)\n with complete-expansion-ana gc ec x\u2081\n ... | ih = DCCast ih (comp-ana gc x\u2081 ih) x\n\n complete-expansion-ana : \u2200{e \u03c4 \u03c4' \u0393 \u0394 d} \u2192\n \u0393 gcomplete \u2192\n e ecomplete \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n d dcomplete\n complete-expansion-ana gc (ECLam1 ec) (EALam x\u2081 MAHole exp) = {!!} -- not an ih because the ctx isn't gcomplete,\n complete-expansion-ana gc (ECLam1 ec) (EALam x\u2081 MAArr exp) -- since this is the unannotated lambda form, \u03c41 comes out of the sky with no premise about completeness\n with complete-expansion-ana (gcomp-extend gc {!!}) ec exp\n ... | ih = DCLam ih {!!}\n complete-expansion-ana gc ec (EASubsume x x\u2081 x\u2082 x\u2083) = complete-expansion-synth gc ec x\u2082\n\n --this is just a convenience since it shows up a few times above\n comp-ana : \u2200{\u0393 e \u03c4 d \u03c4' \u0394} \u2192\n \u0393 gcomplete \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n d dcomplete \u2192\n \u03c4' tcomplete\n comp-ana gc ex dc = complete-ta gc (\u03c02 (typed-expansion-ana ex)) dc\n","old_contents":"open import Nat\nopen import Prelude\nopen import core\nopen import contexts\n\nopen import correspondence\n\nmodule complete-expansion where\n -- todo: do you really need this? didn't in complete preservation; come back to this\n gcomp-extend : \u2200{\u0393 \u03c4 x} \u2192 \u0393 gcomplete \u2192 \u03c4 tcomplete \u2192 (\u0393 ,, (x , \u03c4)) gcomplete\n gcomp-extend {x = x} gc tc x\u2081 t x\u2082 with natEQ x\u2081 x\n gcomp-extend {\u0393 = \u0393} gc tc x t x\u2083 | Inl refl with \u0393 x\n gcomp-extend gc tc x x\u2081 refl | Inl refl | Some .x\u2081 = {!!} -- stupid context stuff again\n gcomp-extend gc tc x t x\u2083 | Inl refl | None with natEQ x x\n gcomp-extend gc tc x \u03c4 refl | Inl refl | None | Inl refl = tc\n gcomp-extend gc tc x t x\u2083 | Inl refl | None | Inr x\u2081 = abort (somenotnone (! x\u2083))\n gcomp-extend {\u0393 = \u0393} gc tc x\u2081 t x\u2083 | Inr x\u2082 with \u0393 x\u2081\n gcomp-extend gc tc x\u2081 t x\u2084 | Inr x\u2083 | Some x = {!tc!} -- ditto\n gcomp-extend {x = x} gc tc x\u2081 t x\u2083 | Inr x\u2082 | None with natEQ x x\u2081\n gcomp-extend gc tc x t refl | Inr x\u2083 | None | Inl refl = tc\n gcomp-extend gc tc x\u2081 t x\u2084 | Inr x\u2083 | None | Inr x\u2082 = abort (somenotnone (! x\u2084))\n\n complete-ta : \u2200{\u0393 \u0394 d \u03c4} \u2192 (\u0393 gcomplete) \u2192 (\u0394 , \u0393 \u22a2 d :: \u03c4) \u2192 d dcomplete \u2192 \u03c4 tcomplete\n complete-ta gc TAConst comp = TCBase\n complete-ta gc (TAVar x\u2081) DCVar = gc _ _ x\u2081\n complete-ta gc (TALam wt) (DCLam comp x\u2081) = TCArr x\u2081 (complete-ta (gcomp-extend gc x\u2081) wt comp)\n complete-ta gc (TAAp wt wt\u2081) (DCAp comp comp\u2081) with complete-ta gc wt comp\n complete-ta gc (TAAp wt wt\u2081) (DCAp comp comp\u2081) | TCArr qq qq\u2081 = qq\u2081\n complete-ta gc (TAEHole x x\u2081) ()\n complete-ta gc (TANEHole x wt x\u2081) ()\n complete-ta gc (TACast wt x) (DCCast comp x\u2081 x\u2082) = x\u2082\n complete-ta gc (TAFailedCast wt x x\u2081 x\u2082) ()\n\n synth-hole-not-comp : \u2200{\u0393 e} \u2192 \u0393 \u22a2 e => \u2987\u2988 \u2192 \u0393 gcomplete \u2192 e ecomplete \u2192 \u22a5 -- this is not a strong enough IH\n synth-hole-not-comp (SAsc x) gc (ECAsc () ec)\n synth-hole-not-comp (SVar {n = n} x) gc ec with gc n _ x\n ... | ()\n synth-hole-not-comp (SAp wt MAHole x\u2081) gc (ECAp ec ec\u2081) = synth-hole-not-comp wt gc ec\n synth-hole-not-comp (SAp wt MAArr x\u2081) gc (ECAp ec ec\u2081) = {!!}\n synth-hole-not-comp SEHole gc ()\n synth-hole-not-comp (SNEHole wt) gc ()\n\n -- todo remove when you've proven typed expansion\n postulate\n typed-expansion-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4\n typed-expansion-ana : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n (\u03c4' ~ \u03c4) \u00d7 (\u0394 , \u0393 \u22a2 d :: \u03c4')\n mutual\n complete-expansion-synth : \u2200{e \u03c4 \u0393 \u0394 d} \u2192\n \u0393 gcomplete \u2192\n e ecomplete \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n d dcomplete\n complete-expansion-synth gc ec ESConst = DCConst\n complete-expansion-synth gc ec (ESVar x\u2081) = DCVar\n complete-expansion-synth gc (ECLam2 ec x\u2081) (ESLam x\u2082 exp) = DCLam (complete-expansion-synth (gcomp-extend gc x\u2081) ec exp) x\u2081\n complete-expansion-synth gc (ECAp ec ec\u2081) (ESAp x MAHole x\u2082 x\u2083) = abort (synth-hole-not-comp x gc ec)\n complete-expansion-synth gc (ECAp ec ec\u2081) (ESAp x MAArr x\u2082 x\u2083) = {!!}\n complete-expansion-synth gc () ESEHole\n complete-expansion-synth gc () (ESNEHole exp)\n complete-expansion-synth gc (ECAsc x ec) (ESAsc x\u2081) with complete-expansion-ana gc ec x\u2081\n ... | ih = DCCast ih (complete-ta gc (\u03c02 (typed-expansion-ana x\u2081)) ih) x\n\n complete-expansion-ana : \u2200{e \u03c4 \u03c4' \u0393 \u0394 d} \u2192\n \u0393 gcomplete \u2192\n e ecomplete \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n d dcomplete\n complete-expansion-ana gc (ECLam1 ec) (EALam x\u2081 MAHole exp) = {!!}\n complete-expansion-ana gc (ECLam1 ec) (EALam x\u2081 MAArr exp)\n with complete-expansion-ana (gcomp-extend gc {!!}) ec exp\n ... | ih = DCLam ih {!!}\n complete-expansion-ana gc ec (EASubsume x x\u2081 x\u2082 x\u2083) = complete-expansion-synth gc ec x\u2082\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"8df38c66c4e14c281a382410d07f8d6895927b9d","subject":"Cosmetic changes.","message":"Cosmetic changes.\n\nIgnore-this: 4effbc2cfa6aca7c6869101cc72a1849\n\ndarcs-hash:20110511171225-3bd4e-1596536d6deb7589d87a6ff01f607d89c680aaeb.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Program\/McCarthy91\/Properties\/MainATP.agda","new_file":"src\/FOTC\/Program\/McCarthy91\/Properties\/MainATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Main properties of the McCarthy 91 function\n------------------------------------------------------------------------------\n\n-- The main properties proved of the McCarthy 91 function (called\n-- mc91) are\n\n-- 1. The function always terminates.\n-- 2. For all n, n < mc91 n + 11.\n-- 3. For all n > 100, then mc91 n = n - 10.\n-- 4. For all n <= 100, then mc91 n = 91.\n\n-- N.B This module does not contain combined proofs, but it imports\n-- modules which contain combined proofs.\n\nmodule FOTC.Program.McCarthy91.Properties.MainATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers.TotalityATP\n\nopen import FOTC.Program.McCarthy91.ArithmeticATP\nopen import FOTC.Program.McCarthy91.EquationsATP\nopen import FOTC.Program.McCarthy91.McCarthy91\nopen import FOTC.Program.McCarthy91.MCR\nopen import FOTC.Program.McCarthy91.MCR.LT2MCR-ATP\nopen import FOTC.Program.McCarthy91.MCR.Induction.Acc.WellFoundedInductionATP\nopen import FOTC.Program.McCarthy91.Properties.AuxiliaryATP\n\n------------------------------------------------------------------------------\n\nmc91-N-ineq : \u2200 {n} \u2192 N n \u2192 N (mc91 n) \u2227 LT n (mc91 n + eleven)\nmc91-N-ineq = wfInd-MCR P mc91-N-ineq-aux\n where\n P : D \u2192 Set\n P d = N (mc91 d) \u2227 LT d (mc91 d + eleven)\n\n mc91-N-ineq-aux : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 MCR k m \u2192 P k) \u2192 P m\n mc91-N-ineq-aux {m} Nm f with x>y\u2228x\u2264y Nm 100-N\n ... | inj\u2081 m>100 = ( Nmc91>100 Nm m>100 , x100 Nm m>100 )\n ... | inj\u2082 m\u2264100 =\n let Nm+11 : N (m + eleven)\n Nm+11 = x+11-N Nm\n\n ih1 : P (m + eleven)\n ih1 = f Nm+11 (LT2MCR (x+11-N Nm) Nm m\u2264100 (xy\u2228x\u2264y Nm 100-N\n ... | inj\u2081 m>100 = inj\u2081 ( m>100 , mc91-eq\u2081 m m>100 )\n ... | inj\u2082 m\u2264100 with x100 , _ ) = \u22a5-elim (x\u226489\u2192x+11>100\u2192\u22a5 Nm\n m\u226489 m+11>100)\n ... | inj\u2082 ( _ , res ) = res\n\n mc91-res-m\u226489 : mc91 m \u2261 ninety-one\n mc91-res-m\u226489 = mc91x-res\u2264100 Nm ninety-one m\u2264100\n mc91-res-m+11 mc91-res-91\n\n------------------------------------------------------------------------------\n-- Main properties\n\n-- The function always terminates.\nmc91-N : \u2200 {n} \u2192 N n \u2192 N (mc91 n)\nmc91-N Nn = \u2227-proj\u2081 (mc91-N-ineq Nn)\n\n-- For all n, n < mc91 n + 11.\nmc91-ineq : \u2200 {n} \u2192 N n \u2192 LT n (mc91 n + eleven)\nmc91-ineq Nn = \u2227-proj\u2082 (mc91-N-ineq Nn)\n\n-- For all n > 100, then mc91 n = n - 10.\nmc91-res>100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192 mc91 n \u2261 n \u2238 ten\nmc91-res>100 Nn n>100 with mc91-res Nn\n... | inj\u2081 ( _ , res ) = res\n... | inj\u2082 ( n\u2264100 , _ ) = \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nn 100-N n>100 n\u2264100)\n\n-- For all n <= 100, then mc91 n = 91.\nmc91-res\u2264100 : \u2200 {n} \u2192 N n \u2192 LE n one-hundred \u2192 mc91 n \u2261 ninety-one\nmc91-res\u2264100 Nn n\u2264100 with mc91-res Nn\n... | inj\u2081 ( n>100 , _ ) = \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nn 100-N n>100 n\u2264100)\n... | inj\u2082 ( _ , res ) = res\n","old_contents":"------------------------------------------------------------------------------\n-- Main properties of the McCarthy 91 function\n------------------------------------------------------------------------------\n\n-- The main properties proved of the McCarthy 91 function (called\n-- mc91) are\n\n-- 1. The function always terminates.\n-- 2. For all n, n < mc91 n + 11.\n-- 3. For all n > 100, then mc91 n = n - 10.\n-- 4. For all n <= 100, then mc91 n = 91.\n\n-- N.B This module does not contain combined proofs, but it imports\n-- modules which contain combined proofs.\n\nmodule FOTC.Program.McCarthy91.Properties.MainATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers.TotalityATP\n\nopen import FOTC.Program.McCarthy91.ArithmeticATP\nopen import FOTC.Program.McCarthy91.EquationsATP\nopen import FOTC.Program.McCarthy91.McCarthy91\nopen import FOTC.Program.McCarthy91.MCR\nopen import FOTC.Program.McCarthy91.MCR.LT2MCR-ATP\nopen import FOTC.Program.McCarthy91.MCR.Induction.Acc.WellFoundedInductionATP\nopen import FOTC.Program.McCarthy91.Properties.AuxiliaryATP\n\n------------------------------------------------------------------------------\n\nmc91-N-ineq : \u2200 {n} \u2192 N n \u2192 N (mc91 n) \u2227 LT n (mc91 n + eleven)\nmc91-N-ineq = wfInd-MCR P mc91-N-ineq-aux\n where\n P : D \u2192 Set\n P d = N (mc91 d) \u2227 LT d (mc91 d + eleven)\n\n mc91-N-ineq-aux : \u2200 {m} \u2192 N m \u2192 (\u2200 {k} \u2192 N k \u2192 MCR k m \u2192 P k) \u2192 P m\n mc91-N-ineq-aux {m} Nm f with x>y\u2228x\u2264y Nm 100-N\n ... | inj\u2081 m>100 = ( Nmc91>100 Nm m>100 , x100 Nm m>100 )\n ... | inj\u2082 m\u2264100 =\n let Nm+11 : N (m + eleven)\n Nm+11 = x+11-N Nm\n\n ih1 : P (m + eleven)\n ih1 = f Nm+11 (LT2MCR (x+11-N Nm) Nm m\u2264100 (xy\u2228x\u2264y Nm 100-N\n ... | inj\u2081 m>100 = inj\u2081 ( m>100 , mc91-eq\u2081 m m>100 )\n ... | inj\u2082 m\u2264100 with x100 , _) = \u22a5-elim (x\u226489\u2192x+11>100\u2192\u22a5 Nm\n m\u226489 m+11>100)\n ... | inj\u2082 ( _ , res ) = res\n\n mc91-res-m\u226489 : mc91 m \u2261 ninety-one\n mc91-res-m\u226489 = mc91x-res\u2264100 Nm ninety-one m\u2264100\n mc91-res-m+11 mc91-res-91\n\n------------------------------------------------------------------------------\n-- Main properties\n\n-- The function always terminates.\nmc91-N : \u2200 {n} \u2192 N n \u2192 N (mc91 n)\nmc91-N Nn = \u2227-proj\u2081 (mc91-N-ineq Nn)\n\n-- For all n, n < mc91 n + 11.\nmc91-ineq : \u2200 {n} \u2192 N n \u2192 LT n (mc91 n + eleven)\nmc91-ineq Nn = \u2227-proj\u2082 (mc91-N-ineq Nn)\n\n-- For all n > 100, then mc91 n = n - 10.\nmc91-res>100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192 mc91 n \u2261 n \u2238 ten\nmc91-res>100 Nn n>100 with mc91-res Nn\n... | inj\u2081 ( _ , res ) = res\n... | inj\u2082 ( n\u2264100 , _ ) = \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nn 100-N n>100 n\u2264100)\n\n-- For all n <= 100, then mc91 n = 91.\nmc91-res\u2264100 : \u2200 {n} \u2192 N n \u2192 LE n one-hundred \u2192 mc91 n \u2261 ninety-one\nmc91-res\u2264100 Nn n\u2264100 with mc91-res Nn\n... | inj\u2081 ( n>100 , _ ) = \u22a5-elim (x>y\u2192x\u2264y\u2192\u22a5 Nn 100-N n>100 n\u2264100)\n... | inj\u2082 ( _ , res ) = res\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"fce2b2a119cd45e59ea06ab4bbb3f453a5d037f2","subject":"Derive during symbolic execution (see #50).","message":"Derive during symbolic execution (see #50).\n\nSurprisingly painless. Is this what we want?\n\nOld-commit-hash: af9942ac1056e270f468dc06560517ac7de2cc39\n","repos":"inc-lc\/ilc-agda","old_file":"lambda.agda","new_file":"lambda.agda","new_contents":"module lambda where\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types public\nopen import Syntactic.Contexts Type public\n\nopen import Denotational.Notation\nopen import Denotational.Values public\nopen import Denotational.Environments Type \u27e6_\u27e7Type public\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n true false : \u2200 {\u0393} \u2192 Term \u0393 bool\n if : \u2200 {\u0393 \u03c4} \u2192 (t\u2081 : Term \u0393 bool) (t\u2082 t\u2083 : Term \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\n\u27e6 true \u27e7Term \u03c1 = true\n\u27e6 false \u27e7Term \u03c1 = false\n\u27e6 if t\u2081 t\u2082 t\u2083 \u27e7Term \u03c1 with \u27e6 t\u2081 \u27e7Term \u03c1\n... | true = \u27e6 t\u2082 \u27e7Term \u03c1\n... | false = \u27e6 t\u2083 \u27e7Term \u03c1\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n-- NATURAL SEMANTICS\n\n-- Syntax\n\ndata Env : Context \u2192 Set\ndata Val : Type \u2192 Set\n\ndata Val where\n \u27e8abs_,_\u27e9 : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) (\u03c1 : Env \u0393) \u2192 Val (\u03c4\u2081 \u21d2 \u03c4\u2082)\n true : Val bool\n false : Val bool\n\ndata Env where\n \u2205 : Env \u2205\n _\u2022_ : \u2200 {\u0393 \u03c4} \u2192 Val \u03c4 \u2192 Env \u0393 \u2192 Env (\u03c4 \u2022 \u0393)\n\n-- Lookup\n\ninfixr 8 _\u22a2_\u2193_ _\u22a2_\u21a6_\n\ndata _\u22a2_\u21a6_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Var \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n this : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {v : Val \u03c4} \u2192\n v \u2022 \u03c1 \u22a2 this \u21a6 v\n that : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 x} {\u03c1 : Env \u0393} {v\u2081 : Val \u03c4\u2081} {v\u2082 : Val \u03c4\u2082} \u2192\n \u03c1 \u22a2 x \u21a6 v\u2082 \u2192\n v\u2081 \u2022 \u03c1 \u22a2 that x \u21a6 v\u2082\n\n-- Reduction\n\ndata _\u22a2_\u2193_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Term \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c1} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n \u03c1 \u22a2 abs t \u2193 \u27e8abs t , \u03c1 \u27e9\n app : \u2200 {\u0393 \u0393\u2032 \u03c4\u2081 \u03c4\u2082 \u03c1 \u03c1\u2032 v\u2082 v\u2032} {t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2082 : Term \u0393 \u03c4\u2081} {t\u2032 : Term (\u03c4\u2081 \u2022 \u0393\u2032) \u03c4\u2082} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 \u27e8abs t\u2032 , \u03c1\u2032 \u27e9 \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n v\u2082 \u2022 \u03c1\u2032 \u22a2 t\u2032 \u2193 v\u2032 \u2192\n \u03c1 \u22a2 app t\u2081 t\u2082 \u2193 v\u2032\n var : \u2200 {\u0393 \u03c4 x} {\u03c1 : Env \u0393} {v : Val \u03c4}\u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u03c1 \u22a2 var x \u2193 v\n if-true : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {t\u2081 t\u2082 t\u2083} {v\u2082 : Val \u03c4} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 true \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n \u03c1 \u22a2 if t\u2081 t\u2082 t\u2083 \u2193 v\u2082\n if-false : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {t\u2081 t\u2082 t\u2083} {v\u2083 : Val \u03c4} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 false \u2192\n \u03c1 \u22a2 t\u2083 \u2193 v\u2083 \u2192\n \u03c1 \u22a2 if t\u2081 t\u2082 t\u2083 \u2193 v\u2083\n\n-- SOUNDNESS of natural semantics\n\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 Env \u0393 \u2192 \u27e6 \u0393 \u27e7\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 \u27e8abs t , \u03c1 \u27e9 \u27e7Val = \u03bb v \u2192 \u27e6 t \u27e7 (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\u27e6 true \u27e7Val = true\n\u27e6 false \u27e7Val = false\n\nmeaningOfEnv : \u2200 {\u0393} \u2192 Meaning (Env \u0393)\nmeaningOfEnv = meaning \u27e6_\u27e7Env\n\nmeaningOfVal : \u2200 {\u03c4} \u2192 Meaning (Val \u03c4)\nmeaningOfVal = meaning \u27e6_\u27e7Val\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) = trans (cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082)) (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n\u2193-sound (if-true \u2193\u2081 \u2193\u2082) rewrite \u2193-sound \u2193\u2081 = \u2193-sound \u2193\u2082\n\u2193-sound (if-false \u2193\u2081 \u2193\u2083) rewrite \u2193-sound \u2193\u2081 = \u2193-sound \u2193\u2083\n\n-- WEAKENING\n\n-- Weaken a term to a super context\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken \u227c\u2081 (abs t) = abs (weaken (keep _ \u2022 \u227c\u2081) t)\nweaken \u227c\u2081 (app t\u2081 t\u2082) = app (weaken \u227c\u2081 t\u2081) (weaken \u227c\u2081 t\u2082)\nweaken \u227c\u2081 (var x) = var (lift \u227c\u2081 x)\nweaken \u227c\u2081 true = true\nweaken \u227c\u2081 false = false\nweaken \u227c\u2081 (if e\u2081 e\u2082 e\u2083) = if (weaken \u227c\u2081 e\u2081) (weaken \u227c\u2081 e\u2082) (weaken \u227c\u2081 e\u2083)\n\n-- SYMBOLIC EXECUTION\n--\n-- Naming Convention:\n-- \u0393 \u27ea_\u27ebX is like \u27e6_\u27e7X but for symbolic execution in context \u0393.\n\n_\u27ea_\u27ebType : Context \u2192 Type \u2192 Set\n\u0393\u2081 \u27ea \u03c4\u2081 \u21d2 \u03c4\u2082 \u27ebType = \u2200 {\u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2082 \u27ea \u03c4\u2081 \u27ebType \u2192 \u0393\u2082 \u27ea \u03c4\u2082 \u27ebType\n\u0393\u2081 \u27ea bool \u27ebType = Term \u0393\u2081 bool\n\nmodule _ (\u0393 : Context) where\n import Denotational.Environments\n module SymEnv = Denotational.Environments Type (\u03bb \u03c4 \u2192 \u0393 \u27ea \u03c4 \u27ebType)\n\n open SymEnv public using ()\n renaming (\u27e6_\u27e7Context to _\u27ea_\u27ebContext; \u27e6_\u27e7Var to _\u27ea_\u27ebVar_)\n\nliftVal : \u2200 {\u03c4 \u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u27ea \u03c4 \u27ebType \u2192 \u0393\u2082 \u27ea \u03c4 \u27ebType\nliftVal {\u03c4\u2081 \u21d2 \u03c4\u2082} \u227c\u2081 v\u2081 = \u03bb \u227c\u2082 v\u2082 \u2192 v\u2081 (\u227c-trans \u227c\u2081 \u227c\u2082) v\u2082\nliftVal {bool} \u227c\u2081 v = weaken \u227c\u2081 v\n\nliftEnv : \u2200 {\u0393 \u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u27ea \u0393 \u27ebContext \u2192 \u0393\u2082 \u27ea \u0393 \u27ebContext\nliftEnv {\u2205} \u227c\u2081 \u2205 = SymEnv.\u2205\nliftEnv {\u03c4 \u2022 \u0393} \u227c\u2081 (v \u2022 \u03c1) = liftVal \u227c\u2081 v SymEnv.\u2022 liftEnv \u227c\u2081 \u03c1\n\nmixed-if : \u2200 {\u0393\u2081} \u03c4 \u2192 (t\u2081 : Term \u0393\u2081 bool) (v\u2082 v\u2083 : \u0393\u2081 \u27ea \u03c4 \u27ebType) \u2192 \u0393\u2081 \u27ea \u03c4 \u27ebType\nmixed-if (\u03c4\u2081 \u21d2 \u03c4\u2082) t\u2081 v\u2082 v\u2083 = \u03bb \u227c\u2081 v \u2192 mixed-if \u03c4\u2082 (weaken \u227c\u2081 t\u2081) (v\u2082 \u227c\u2081 v) (v\u2083 \u227c\u2081 v)\nmixed-if bool t\u2081 t\u2082 t\u2083 = if t\u2081 t\u2082 t\u2083\n\n_\u27ea_\u27ebTerm_ : \u2200 \u0393\u2081 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u0393\u2081 \u27ea \u0393 \u27ebContext \u2192 \u0393\u2081 \u27ea \u03c4 \u27ebType\n\u0393\u2081 \u27ea abs t \u27ebTerm \u03c1 = \u03bb {\u0393\u2082} \u227c\u2081 v \u2192 \u0393\u2082 \u27ea t \u27ebTerm (v SymEnv.\u2022 liftEnv \u227c\u2081 \u03c1)\n\u0393\u2081 \u27ea app t\u2081 t\u2082 \u27ebTerm \u03c1 = (\u0393\u2081 \u27ea t\u2081 \u27ebTerm \u03c1) \u227c-refl (\u0393\u2081 \u27ea t\u2082 \u27ebTerm \u03c1)\n\u0393\u2081 \u27ea var x \u27ebTerm \u03c1 = \u0393\u2081 \u27ea x \u27ebVar \u03c1\n\u0393\u2081 \u27ea true \u27ebTerm \u03c1 = true\n\u0393\u2081 \u27ea false \u27ebTerm \u03c1 = false\n\u0393\u2081 \u27ea if t\u2081 t\u2082 t\u2083 \u27ebTerm \u03c1 = mixed-if _ (\u0393\u2081 \u27ea t\u2081 \u27ebTerm \u03c1) (\u0393\u2081 \u27ea t\u2082 \u27ebTerm \u03c1) (\u0393\u2081 \u27ea t\u2082 \u27ebTerm \u03c1)\n\n\u2193 : \u2200 {\u0393} \u03c4 \u2192 \u0393 \u27ea \u03c4 \u27ebType \u2192 Term \u0393 \u03c4\n\u2191 : \u2200 {\u0393} \u03c4 \u2192 Term \u0393 \u03c4 \u2192 \u0393 \u27ea \u03c4 \u27ebType\n\n\u2193 (\u03c4\u2081 \u21d2 \u03c4\u2082) v = abs (\u2193 \u03c4\u2082 (v (drop \u03c4\u2081 \u2022 \u227c-refl) (\u2191 \u03c4\u2081 (var this))))\n\u2193 bool v = v\n\n\u2191 (\u03c4\u2081 \u21d2 \u03c4\u2082) t = \u03bb \u227c\u2081 v \u2192 \u2191 \u03c4\u2082 (app (weaken \u227c\u2081 t) (\u2193 \u03c4\u2081 v))\n\u2191 bool t = t\n\n\u2191-Context : \u2200 {\u0393} \u2192 \u0393 \u27ea \u0393 \u27ebContext\n\u2191-Context {\u2205} = SymEnv.\u2205\n\u2191-Context {\u03c4 \u2022 \u0393} = \u2191 \u03c4 (var this) SymEnv.\u2022 liftEnv (drop \u03c4 \u2022 \u227c-refl) \u2191-Context\n\nnorm : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4\nnorm {\u0393} {\u03c4} t = \u2193 \u03c4 (\u0393 \u27ea t \u27ebTerm \u2191-Context)\n\n-- SYMBOLIC DERIVATION\n--\n-- compare derive with _\u27ea_\u27eb_Term\n-- and derive-var with \u27e6_\u27e7Var\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\u0394-Type bool = bool\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u0394-Context \u0393\n\nxor\u2083 : \u2200 {\u0393} \u2192 Term \u0393 bool \u2192 Term \u0393 bool \u2192 Term \u0393 bool \u2192 Term \u0393 bool\nxor\u2083 t\u2081 t\u2082 t\u2083\n = if t\u2081 (if t\u2082 t\u2083 (if t\u2083 false true)) (if t\u2082 (if t\u2083 false true) t\u2083)\n\nderive-if : \u2200 {\u03c4 \u0393\u2081} \u2192\n (t dt : Term \u0393\u2081 bool) \u2192\n (v\u2081 : \u0393\u2081 \u27ea \u03c4 \u27ebType) (dv\u2081 : \u0393\u2081 \u27ea \u0394-Type \u03c4 \u27ebType) \u2192\n (v\u2082 : \u0393\u2081 \u27ea \u03c4 \u27ebType) (dv\u2082 : \u0393\u2081 \u27ea \u0394-Type \u03c4 \u27ebType) \u2192\n \u0393\u2081 \u27ea \u0394-Type \u03c4 \u27ebType\nderive-if {\u03c4\u2081 \u21d2 \u03c4\u2082} t dt v\u2081 dv\u2081 v\u2082 dv\u2082 =\n \u03bb \u227c\u2081 v \u227c\u2082 dv \u2192 let \u227c\u2083 = \u227c-trans \u227c\u2081 \u227c\u2082 in\n derive-if (weaken \u227c\u2083 t) (weaken \u227c\u2083 dt)\n (v\u2081 \u227c\u2083 (liftVal \u227c\u2082 v)) (dv\u2081 \u227c\u2081 v \u227c\u2082 dv)\n (v\u2082 \u227c\u2083 (liftVal \u227c\u2082 v)) (dv\u2082 \u227c\u2081 v \u227c\u2082 dv)\nderive-if {bool} t\u2081 dt\u2081 t\u2082 dt\u2082 t\u2083 dt\u2083 =\n if dt\u2081 (if t\u2081 (xor\u2083 t\u2082 t\u2083 dt\u2083) (xor\u2083 t\u2082 t\u2083 dt\u2082)) (if t\u2081 dt\u2083 dt\u2082)\n\nderive-var : \u2200 {\u0393\u2081 \u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u0393\u2081 \u27ea \u0394-Context \u0393 \u27ebContext \u2192 \u0393\u2081 \u27ea \u0394-Type \u03c4 \u27ebType\nderive-var this (dv SymEnv.\u2022 d\u03c1) = dv\nderive-var (that x) (dv SymEnv.\u2022 d\u03c1) = derive-var x d\u03c1\n\nderive : \u2200 {\u0393\u2081 \u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u0393\u2081 \u27ea \u0393 \u27ebContext \u2192 \u0393\u2081 \u27ea \u0394-Context \u0393 \u27ebContext \u2192 \u0393\u2081 \u27ea \u0394-Type \u03c4 \u27ebType\nderive (abs t) \u03c1 d\u03c1 =\n \u03bb \u227c\u2081 v \u227c\u2082 dv \u2192 derive t (liftVal \u227c\u2082 v SymEnv.\u2022 liftEnv (\u227c-trans \u227c\u2081 \u227c\u2082) \u03c1) (dv SymEnv.\u2022 liftEnv (\u227c-trans \u227c\u2081 \u227c\u2082) d\u03c1)\nderive (app t\u2081 t\u2082) \u03c1 d\u03c1 =\n derive t\u2081 \u03c1 d\u03c1 \u227c-refl (_ \u27ea t\u2082 \u27ebTerm \u03c1) \u227c-refl (derive t\u2082 \u03c1 d\u03c1)\nderive (var x) \u03c1 d\u03c1 = derive-var x d\u03c1\nderive true \u03c1 d\u03c1 = false\nderive false \u03c1 d\u03c1 = false\nderive {\u0393\u2081} (if t\u2081 t\u2082 t\u2083) \u03c1 d\u03c1 =\n derive-if (\u0393\u2081 \u27ea t\u2081 \u27ebTerm \u03c1) (derive t\u2081 \u03c1 d\u03c1)\n (\u0393\u2081 \u27ea t\u2082 \u27ebTerm \u03c1) (derive t\u2082 \u03c1 d\u03c1)\n (\u0393\u2081 \u27ea t\u2083 \u27ebTerm \u03c1) (derive t\u2083 \u03c1 d\u03c1)\n","old_contents":"module lambda where\n\nopen import Relation.Binary.PropositionalEquality\n\nopen import Syntactic.Types public\nopen import Syntactic.Contexts Type public\n\nopen import Denotational.Notation\nopen import Denotational.Values public\nopen import Denotational.Environments Type \u27e6_\u27e7Type public\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n true false : \u2200 {\u0393} \u2192 Term \u0393 bool\n if : \u2200 {\u0393 \u03c4} \u2192 (t\u2081 : Term \u0393 bool) (t\u2082 t\u2083 : Term \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\n\u27e6 true \u27e7Term \u03c1 = true\n\u27e6 false \u27e7Term \u03c1 = false\n\u27e6 if t\u2081 t\u2082 t\u2083 \u27e7Term \u03c1 with \u27e6 t\u2081 \u27e7Term \u03c1\n... | true = \u27e6 t\u2082 \u27e7Term \u03c1\n... | false = \u27e6 t\u2083 \u27e7Term \u03c1\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n-- NATURAL SEMANTICS\n\n-- Syntax\n\ndata Env : Context \u2192 Set\ndata Val : Type \u2192 Set\n\ndata Val where\n \u27e8abs_,_\u27e9 : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) (\u03c1 : Env \u0393) \u2192 Val (\u03c4\u2081 \u21d2 \u03c4\u2082)\n true : Val bool\n false : Val bool\n\ndata Env where\n \u2205 : Env \u2205\n _\u2022_ : \u2200 {\u0393 \u03c4} \u2192 Val \u03c4 \u2192 Env \u0393 \u2192 Env (\u03c4 \u2022 \u0393)\n\n-- Lookup\n\ninfixr 8 _\u22a2_\u2193_ _\u22a2_\u21a6_\n\ndata _\u22a2_\u21a6_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Var \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n this : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {v : Val \u03c4} \u2192\n v \u2022 \u03c1 \u22a2 this \u21a6 v\n that : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 x} {\u03c1 : Env \u0393} {v\u2081 : Val \u03c4\u2081} {v\u2082 : Val \u03c4\u2082} \u2192\n \u03c1 \u22a2 x \u21a6 v\u2082 \u2192\n v\u2081 \u2022 \u03c1 \u22a2 that x \u21a6 v\u2082\n\n-- Reduction\n\ndata _\u22a2_\u2193_ : \u2200 {\u0393 \u03c4} \u2192 Env \u0393 \u2192 Term \u0393 \u03c4 \u2192 Val \u03c4 \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082 \u03c1} {t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082} \u2192\n \u03c1 \u22a2 abs t \u2193 \u27e8abs t , \u03c1 \u27e9\n app : \u2200 {\u0393 \u0393\u2032 \u03c4\u2081 \u03c4\u2082 \u03c1 \u03c1\u2032 v\u2082 v\u2032} {t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)} {t\u2082 : Term \u0393 \u03c4\u2081} {t\u2032 : Term (\u03c4\u2081 \u2022 \u0393\u2032) \u03c4\u2082} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 \u27e8abs t\u2032 , \u03c1\u2032 \u27e9 \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n v\u2082 \u2022 \u03c1\u2032 \u22a2 t\u2032 \u2193 v\u2032 \u2192\n \u03c1 \u22a2 app t\u2081 t\u2082 \u2193 v\u2032\n var : \u2200 {\u0393 \u03c4 x} {\u03c1 : Env \u0393} {v : Val \u03c4}\u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u03c1 \u22a2 var x \u2193 v\n if-true : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {t\u2081 t\u2082 t\u2083} {v\u2082 : Val \u03c4} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 true \u2192\n \u03c1 \u22a2 t\u2082 \u2193 v\u2082 \u2192\n \u03c1 \u22a2 if t\u2081 t\u2082 t\u2083 \u2193 v\u2082\n if-false : \u2200 {\u0393 \u03c4} {\u03c1 : Env \u0393} {t\u2081 t\u2082 t\u2083} {v\u2083 : Val \u03c4} \u2192\n \u03c1 \u22a2 t\u2081 \u2193 false \u2192\n \u03c1 \u22a2 t\u2083 \u2193 v\u2083 \u2192\n \u03c1 \u22a2 if t\u2081 t\u2082 t\u2083 \u2193 v\u2083\n\n-- SOUNDNESS of natural semantics\n\n\u27e6_\u27e7Env : \u2200 {\u0393} \u2192 Env \u0393 \u2192 \u27e6 \u0393 \u27e7\n\u27e6_\u27e7Val : \u2200 {\u03c4} \u2192 Val \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n\n\u27e6 \u2205 \u27e7Env = \u2205\n\u27e6 v \u2022 \u03c1 \u27e7Env = \u27e6 v \u27e7Val \u2022 \u27e6 \u03c1 \u27e7Env\n\n\u27e6 \u27e8abs t , \u03c1 \u27e9 \u27e7Val = \u03bb v \u2192 \u27e6 t \u27e7 (v \u2022 \u27e6 \u03c1 \u27e7Env)\n\u27e6 true \u27e7Val = true\n\u27e6 false \u27e7Val = false\n\nmeaningOfEnv : \u2200 {\u0393} \u2192 Meaning (Env \u0393)\nmeaningOfEnv = meaning \u27e6_\u27e7Env\n\nmeaningOfVal : \u2200 {\u03c4} \u2192 Meaning (Val \u03c4)\nmeaningOfVal = meaning \u27e6_\u27e7Val\n\n\u21a6-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {x : Var \u0393 \u03c4} \u2192\n \u03c1 \u22a2 x \u21a6 v \u2192\n \u27e6 x \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u21a6-sound this = refl\n\u21a6-sound (that \u21a6) = \u21a6-sound \u21a6\n\n\u2193-sound : \u2200 {\u0393 \u03c4 \u03c1 v} {t : Term \u0393 \u03c4} \u2192\n \u03c1 \u22a2 t \u2193 v \u2192\n \u27e6 t \u27e7 \u27e6 \u03c1 \u27e7 \u2261 \u27e6 v \u27e7\n\u2193-sound abs = refl\n\u2193-sound (app \u2193\u2081 \u2193\u2082 \u2193\u2032) = trans (cong\u2082 (\u03bb x y \u2192 x y) (\u2193-sound \u2193\u2081) (\u2193-sound \u2193\u2082)) (\u2193-sound \u2193\u2032)\n\u2193-sound (var \u21a6) = \u21a6-sound \u21a6\n\u2193-sound (if-true \u2193\u2081 \u2193\u2082) rewrite \u2193-sound \u2193\u2081 = \u2193-sound \u2193\u2082\n\u2193-sound (if-false \u2193\u2081 \u2193\u2083) rewrite \u2193-sound \u2193\u2081 = \u2193-sound \u2193\u2083\n\n-- WEAKENING\n\n-- Weaken a term to a super context\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken \u227c\u2081 (abs t) = abs (weaken (keep _ \u2022 \u227c\u2081) t)\nweaken \u227c\u2081 (app t\u2081 t\u2082) = app (weaken \u227c\u2081 t\u2081) (weaken \u227c\u2081 t\u2082)\nweaken \u227c\u2081 (var x) = var (lift \u227c\u2081 x)\nweaken \u227c\u2081 true = true\nweaken \u227c\u2081 false = false\nweaken \u227c\u2081 (if e\u2081 e\u2082 e\u2083) = if (weaken \u227c\u2081 e\u2081) (weaken \u227c\u2081 e\u2082) (weaken \u227c\u2081 e\u2083)\n\n-- SYMBOLIC EXECUTION\n--\n-- Naming Convention:\n-- \u0393 \u27ea_\u27ebX is like \u27e6_\u27e7X but for symbolic execution in context \u0393.\n\n_\u27ea_\u27ebType : Context \u2192 Type \u2192 Set\n\u0393\u2081 \u27ea \u03c4\u2081 \u21d2 \u03c4\u2082 \u27ebType = \u2200 {\u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2082 \u27ea \u03c4\u2081 \u27ebType \u2192 \u0393\u2082 \u27ea \u03c4\u2082 \u27ebType\n\u0393\u2081 \u27ea bool \u27ebType = Term \u0393\u2081 bool\n\nmodule _ (\u0393 : Context) where\n import Denotational.Environments\n module SymEnv = Denotational.Environments Type (\u03bb \u03c4 \u2192 \u0393 \u27ea \u03c4 \u27ebType)\n\n open SymEnv public using ()\n renaming (\u27e6_\u27e7Context to _\u27ea_\u27ebContext; \u27e6_\u27e7Var to _\u27ea_\u27ebVar_)\n\nliftVal : \u2200 {\u03c4 \u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u27ea \u03c4 \u27ebType \u2192 \u0393\u2082 \u27ea \u03c4 \u27ebType\nliftVal {\u03c4\u2081 \u21d2 \u03c4\u2082} \u227c\u2081 v\u2081 = \u03bb \u227c\u2082 v\u2082 \u2192 v\u2081 (\u227c-trans \u227c\u2081 \u227c\u2082) v\u2082\nliftVal {bool} \u227c\u2081 v = weaken \u227c\u2081 v\n\nliftEnv : \u2200 {\u0393 \u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u27ea \u0393 \u27ebContext \u2192 \u0393\u2082 \u27ea \u0393 \u27ebContext\nliftEnv {\u2205} \u227c\u2081 \u2205 = SymEnv.\u2205\nliftEnv {\u03c4 \u2022 \u0393} \u227c\u2081 (v \u2022 \u03c1) = liftVal \u227c\u2081 v SymEnv.\u2022 liftEnv \u227c\u2081 \u03c1\n\nmixed-if : \u2200 {\u0393\u2081} \u03c4 \u2192 (t\u2081 : Term \u0393\u2081 bool) (v\u2082 v\u2083 : \u0393\u2081 \u27ea \u03c4 \u27ebType) \u2192 \u0393\u2081 \u27ea \u03c4 \u27ebType\nmixed-if (\u03c4\u2081 \u21d2 \u03c4\u2082) t\u2081 v\u2082 v\u2083 = \u03bb \u227c\u2081 v \u2192 mixed-if \u03c4\u2082 (weaken \u227c\u2081 t\u2081) (v\u2082 \u227c\u2081 v) (v\u2083 \u227c\u2081 v)\nmixed-if bool t\u2081 t\u2082 t\u2083 = if t\u2081 t\u2082 t\u2083\n\n_\u27ea_\u27ebTerm_ : \u2200 \u0393\u2081 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u0393\u2081 \u27ea \u0393 \u27ebContext \u2192 \u0393\u2081 \u27ea \u03c4 \u27ebType\n\u0393\u2081 \u27ea abs t \u27ebTerm \u03c1 = \u03bb {\u0393\u2082} \u227c\u2081 v \u2192 \u0393\u2082 \u27ea t \u27ebTerm (v SymEnv.\u2022 liftEnv \u227c\u2081 \u03c1)\n\u0393\u2081 \u27ea app t\u2081 t\u2082 \u27ebTerm \u03c1 = (\u0393\u2081 \u27ea t\u2081 \u27ebTerm \u03c1) \u227c-refl (\u0393\u2081 \u27ea t\u2082 \u27ebTerm \u03c1)\n\u0393\u2081 \u27ea var x \u27ebTerm \u03c1 = \u0393\u2081 \u27ea x \u27ebVar \u03c1\n\u0393\u2081 \u27ea true \u27ebTerm \u03c1 = true\n\u0393\u2081 \u27ea false \u27ebTerm \u03c1 = false\n\u0393\u2081 \u27ea if t\u2081 t\u2082 t\u2083 \u27ebTerm \u03c1 = mixed-if _ (\u0393\u2081 \u27ea t\u2081 \u27ebTerm \u03c1) (\u0393\u2081 \u27ea t\u2082 \u27ebTerm \u03c1) (\u0393\u2081 \u27ea t\u2082 \u27ebTerm \u03c1)\n\n\u2193 : \u2200 {\u0393} \u03c4 \u2192 \u0393 \u27ea \u03c4 \u27ebType \u2192 Term \u0393 \u03c4\n\u2191 : \u2200 {\u0393} \u03c4 \u2192 Term \u0393 \u03c4 \u2192 \u0393 \u27ea \u03c4 \u27ebType\n\n\u2193 (\u03c4\u2081 \u21d2 \u03c4\u2082) v = abs (\u2193 \u03c4\u2082 (v (drop \u03c4\u2081 \u2022 \u227c-refl) (\u2191 \u03c4\u2081 (var this))))\n\u2193 bool v = v\n\n\u2191 (\u03c4\u2081 \u21d2 \u03c4\u2082) t = \u03bb \u227c\u2081 v \u2192 \u2191 \u03c4\u2082 (app (weaken \u227c\u2081 t) (\u2193 \u03c4\u2081 v))\n\u2191 bool t = t\n\n\u2191-Context : \u2200 {\u0393} \u2192 \u0393 \u27ea \u0393 \u27ebContext\n\u2191-Context {\u2205} = SymEnv.\u2205\n\u2191-Context {\u03c4 \u2022 \u0393} = \u2191 \u03c4 (var this) SymEnv.\u2022 liftEnv (drop \u03c4 \u2022 \u227c-refl) \u2191-Context\n\nnorm : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4\nnorm {\u0393} {\u03c4} t = \u2193 \u03c4 (\u0393 \u27ea t \u27ebTerm \u2191-Context)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"8dc6fd5c821f19a87d96c7731f39280af92e1c17","subject":"Renamed some properties.","message":"Renamed some properties.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Data\/Stream\/Equality\/PropertiesI.agda","new_file":"src\/fot\/FOTC\/Data\/Stream\/Equality\/PropertiesI.agda","new_contents":"------------------------------------------------------------------------------\n-- Properties for the equality on streams\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Stream.Equality.PropertiesI where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Data.Stream\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n\nstream-\u2248-refl : \u2200 {xs} \u2192 Stream xs \u2192 xs \u2248 xs\nstream-\u2248-refl {xs} Sxs = \u2248-coind R h\u2081 h\u2082\n where\n R : D \u2192 D \u2192 Set\n R xs ys = Stream xs \u2227 Stream xs \u2227 xs \u2261 ys\n\n h\u2081 : \u2200 {xs ys} \u2192 R xs ys \u2192 \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n R xs' ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n h\u2081 (Sxs , Sys , refl) with (Stream-unf Sxs)\n ... | x' , xs' , Sxs' , prf = x' , xs' , xs' , (Sxs' , Sxs' , refl) , prf , prf\n\n h\u2082 : R xs xs\n h\u2082 = Sxs , Sxs , refl\n\nstream-\u2261\u2192\u2248 : \u2200 {xs ys} \u2192 Stream xs \u2192 Stream ys \u2192 xs \u2261 ys \u2192 xs \u2248 ys\nstream-\u2261\u2192\u2248 Sxs _ refl = stream-\u2248-refl Sxs\n\n\u2248\u2192Stream : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Stream xs \u2227 Stream ys\n\u2248\u2192Stream {xs} {ys} h = Stream-coind P\u2081 h\u2081 (ys , h) , Stream-coind P\u2082 h\u2082 (xs , h)\n where\n P\u2081 : D \u2192 Set\n P\u2081 ws = \u2203[ zs ] ws \u2248 zs\n\n h\u2081 : \u2200 {ws} \u2192 P\u2081 ws \u2192 \u2203[ w' ] \u2203[ ws' ] P\u2081 ws' \u2227 ws \u2261 w' \u2237 ws'\n h\u2081 {ws} (zs , h\u2081) with \u2248-unf h\u2081\n ... | w' , ws' , zs' , prf\u2081 , prf\u2082 , _ = w' , ws' , (zs' , prf\u2081) , prf\u2082\n\n P\u2082 : D \u2192 Set\n P\u2082 zs = \u2203[ ws ] ws \u2248 zs\n\n h\u2082 : \u2200 {zs} \u2192 P\u2082 zs \u2192 \u2203[ z' ] \u2203[ zs' ] P\u2082 zs' \u2227 zs \u2261 z' \u2237 zs'\n h\u2082 {zs} (ws , h\u2081) with \u2248-unf h\u2081\n ... | w' , ws' , zs' , prf\u2081 , _ , prf\u2082 = w' , zs' , (ws' , prf\u2081) , prf\u2082\n","old_contents":"------------------------------------------------------------------------------\n-- Properties for the equality on streams\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Stream.Equality.PropertiesI where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Data.Stream\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n\n\u2248-Stream-refl : \u2200 {xs} \u2192 Stream xs \u2192 xs \u2248 xs\n\u2248-Stream-refl {xs} Sxs = \u2248-coind R h\u2081 h\u2082\n where\n R : D \u2192 D \u2192 Set\n R xs ys = Stream xs \u2227 Stream xs \u2227 xs \u2261 ys\n\n h\u2081 : \u2200 {xs ys} \u2192 R xs ys \u2192 \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n R xs' ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n h\u2081 (Sxs , Sys , refl) with (Stream-unf Sxs)\n ... | x' , xs' , Sxs' , prf = x' , xs' , xs' , (Sxs' , Sxs' , refl) , prf , prf\n\n h\u2082 : R xs xs\n h\u2082 = Sxs , Sxs , refl\n\n\u2261\u2192\u2248-Stream : \u2200 {xs ys} \u2192 Stream xs \u2192 Stream ys \u2192 xs \u2261 ys \u2192 xs \u2248 ys\n\u2261\u2192\u2248-Stream Sxs _ refl = \u2248-Stream-refl Sxs\n\n\u2248\u2192Stream : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 Stream xs \u2227 Stream ys\n\u2248\u2192Stream {xs} {ys} h = Stream-coind P\u2081 h\u2081 (ys , h) , Stream-coind P\u2082 h\u2082 (xs , h)\n where\n P\u2081 : D \u2192 Set\n P\u2081 ws = \u2203[ zs ] ws \u2248 zs\n\n h\u2081 : \u2200 {ws} \u2192 P\u2081 ws \u2192 \u2203[ w' ] \u2203[ ws' ] P\u2081 ws' \u2227 ws \u2261 w' \u2237 ws'\n h\u2081 {ws} (zs , h\u2081) with \u2248-unf h\u2081\n ... | w' , ws' , zs' , prf\u2081 , prf\u2082 , _ = w' , ws' , (zs' , prf\u2081) , prf\u2082\n\n P\u2082 : D \u2192 Set\n P\u2082 zs = \u2203[ ws ] ws \u2248 zs\n\n h\u2082 : \u2200 {zs} \u2192 P\u2082 zs \u2192 \u2203[ z' ] \u2203[ zs' ] P\u2082 zs' \u2227 zs \u2261 z' \u2237 zs'\n h\u2082 {zs} (ws , h\u2081) with \u2248-unf h\u2081\n ... | w' , ws' , zs' , prf\u2081 , _ , prf\u2082 = w' , zs' , (ws' , prf\u2081) , prf\u2082\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"4dd2701282fa75b452d77d8bb170e29ea6fa6956","subject":"clean up cut elimination","message":"clean up cut elimination\n","repos":"crypto-agda\/protocols","old_file":"js-experiment\/Terms.agda","new_file":"js-experiment\/Terms.agda","new_contents":"open import proto\nopen import Types\nopen import prelude\nopen import uri\n\nmodule Terms where\n\ninfix 2 \u22a2\u02e2_ \u22a2_ \u22a2\u1d9c\u1da0_\n\ndata \u22a2_ : (\u0394 : Env) \u2192 Set\u2081 where\n end : \u2200{\u0394}{e : EndedEnv \u0394}\n ------------------\n \u2192 \u22a2 \u0394\n\n output : \u2200 {\u0394 d M P}{{_ : SER M}}\n (l : d \u21a6 send P \u2208 \u0394)(m : M)\n (p : \u22a2 \u0394 [ l \u2254 m ])\n -------------------\n \u2192 \u22a2 \u0394\n\n input : \u2200 {\u0394 d M}{P : M \u2192 _}{{_ : SER M}}\n (l : d \u21a6 recv P \u2208 \u0394)\n (p : \u2200 m \u2192 \u22a2 \u0394 [ l \u2254 m ])\n ----------------------\n \u2192 \u22a2 \u0394\n\n mix : \u2200 {\u0394 \u0394\u2080 \u0394\u2081} (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ])\n (p : \u22a2 \u0394\u2080) (q : \u22a2 \u0394\u2081)\n --------------------\n \u2192 \u22a2 \u0394\n\n cut : \u2200 {\u0394 \u0394\u2080 \u0394\u2081} (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ]) {P d}\n (p : \u22a2 (\u0394\u2080 , d \u21a6 dual P))\n (q : \u22a2 (\u0394\u2081 , d \u21a6 P))\n ---------------------\n \u2192 \u22a2 \u0394\n\n fwd : \u2200 c d {P} \u2192 \u22a2 (\u03b5 , c \u21a6 P , d \u21a6 dual P)\n\n exch-last :\n \u2200 {\u0394 c d P Q} \u2192\n (p : \u22a2 \u0394 , c \u21a6 P , d \u21a6 Q)\n \u2192 \u22a2 \u0394 , d \u21a6 Q , c \u21a6 P\n\n wk-last : \u2200 {\u0394 d}\n (p : \u22a2 \u0394)\n -----------------------\n \u2192 \u22a2 (\u0394 , d \u21a6 end)\n\n end-last : \u2200 {\u0394 d}\n (p : \u22a2 (\u0394 , d \u21a6 end))\n ----------------------\n \u2192 \u22a2 \u0394\n\ndata \u22a2\u1d9c\u1da0_ (\u0394 : Env) : Set\u2081 where\n end : {e : EndedEnv \u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394\n\n output : \u2200 {d M P}{{_ : SER M}}\n (l : d \u21a6 send P \u2208 \u0394) \u2192 (m : M)\n (p : \u22a2\u1d9c\u1da0 \u0394 [ l \u2254 m ])\n --------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\n\n input : \u2200 {d M}{P : M \u2192 _}{{_ : SER M}}\n (l : d \u21a6 recv P \u2208 \u0394)\n (p : \u2200 m \u2192 \u22a2\u1d9c\u1da0 \u0394 [ l \u2254 m ])\n ----------------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\n\n-- The \u0394 for the server contains the view point of the clients\n-- The point is that the meaning of _,_ in \u0394 is \u2297 while it\n-- is \u214b in \u22a2\u1d9c\u1da0\nrecord \u22a2\u02e2_ (\u0394 : Env) : Set\u2081 where\n coinductive\n field\n server-output :\n \u2200 {d M}{{_ : SER M}}{P : M \u2192 Proto}\n (l : d \u21a6 recv P \u2208 \u0394) \u2192\n \u03a3 M \u03bb m \u2192 \u22a2\u02e2 \u0394 [ l \u2254 m ]\n server-input :\n \u2200 {d M}{{_ : SER M}}{P : M \u2192 Proto}\n (l : d \u21a6 send P \u2208 \u0394)\n (m : M) \u2192 \u22a2\u02e2 \u0394 [ l \u2254 m ]\nopen \u22a2\u02e2_ public\n\n-- This is just to confirm that we have enough cases\ntelecom' : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u22a2\u02e2 \u0394 \u2192 \ud835\udfd9\ntelecom' end q = _\ntelecom' (output l m p) q\n = telecom' p (server-input q l m)\ntelecom' (input l p) q\n = case server-output q l of \u03bb { (m , s) \u2192\n telecom' (p m) s }\n\nembed\u1d9c\u1da0 : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u22a2 \u0394\nembed\u1d9c\u1da0 (end {e = e}) = end {e = e}\nembed\u1d9c\u1da0 (output l m p) = output l m (embed\u1d9c\u1da0 p)\nembed\u1d9c\u1da0 (input l p) = input l \u03bb m \u2192 embed\u1d9c\u1da0 (p m)\n\nmix\u1d9c\u1da0 : \u2200 {\u0394 \u0394\u2080 \u0394\u2081} (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ])\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080)\n (q : \u22a2\u1d9c\u1da0 \u0394\u2081)\n -------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\nmix\u1d9c\u1da0 \u0394\u209b end q = tr \u22a2\u1d9c\u1da0_ (Zip-identity \u0394\u209b) q\nmix\u1d9c\u1da0 \u0394\u209b (output l m p) q\n = output (Zip-com\u2208\u2080 \u0394\u209b l) m (mix\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) p q)\nmix\u1d9c\u1da0 \u0394\u209b (input l p) q\n = input (Zip-com\u2208\u2080 \u0394\u209b l) \u03bb m \u2192 mix\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) (p m) q\n\ncut\u1d9c\u1da0 : \u2200 {\u0394 \u0394\u2080 \u0394\u2081}\n (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ]) d P\n (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 dual P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P))\n ---------------------------\n \u2192 \u22a2\u1d9c\u1da0 (\u0394 , d \u21a6 end)\n\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) (output here m p) (input here q) = cut\u1d9c\u1da0 \u0394\u209b d (P m) p (q m)\ncut\u1d9c\u1da0 \u0394\u209b d (send P) (input here p) (output here m q) = cut\u1d9c\u1da0 \u0394\u209b d (P m) (p m) q\n\ncut\u1d9c\u1da0 \u0394\u209b d P (output (there l) m p) q\n = output (there (Zip-com\u2208\u2080 \u0394\u209b l)) m (cut\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) d _ p q)\ncut\u1d9c\u1da0 \u0394\u209b d P (input (there l) p) q\n = input (there (Zip-com\u2208\u2080 \u0394\u209b l)) \u03bb m \u2192 cut\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) d _ (p m) q\ncut\u1d9c\u1da0 \u0394\u209b d P p (output (there l) m q)\n = output (there (Zip-com\u2208\u2081 \u0394\u209b l)) m (cut\u1d9c\u1da0 (Zip-\u2254\u2081 l \u0394\u209b) d _ p q)\ncut\u1d9c\u1da0 \u0394\u209b d P p (input (there l) q)\n = input (there (Zip-com\u2208\u2081 \u0394\u209b l)) \u03bb m \u2192 cut\u1d9c\u1da0 (Zip-\u2254\u2081 l \u0394\u209b) d _ p (q m)\n\ncut\u1d9c\u1da0 \u0394\u209b d end p q = mix\u1d9c\u1da0 (\u0394\u209b , d \u21a6\u2080 end) p q\ncut\u1d9c\u1da0 _ _ (com _ _) (end {e = _ , ()}) _\ncut\u1d9c\u1da0 _ _ (com _ _) _ (end {e = _ , ()})\n\n\nend-last\u1d9c\u1da0 : \u2200 {\u0394 d}\n (p : \u22a2\u1d9c\u1da0 (\u0394 , d \u21a6 end))\n -----------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\nend-last\u1d9c\u1da0 (end {e = e , _}) = end {e = e}\nend-last\u1d9c\u1da0 (output (there l) m p) = output l m (end-last\u1d9c\u1da0 p)\nend-last\u1d9c\u1da0 (input (there l) p) = input l \u03bb m \u2192 end-last\u1d9c\u1da0 (p m)\n\nwk-last\u1d9c\u1da0 : \u2200 {\u0394 d}\n (p : \u22a2\u1d9c\u1da0 \u0394)\n -----------------------\n \u2192 \u22a2\u1d9c\u1da0 (\u0394 , d \u21a6 end)\nwk-last\u1d9c\u1da0 end = end {e = \u2026 , _}\nwk-last\u1d9c\u1da0 (output l m p) = output (there l) m (wk-last\u1d9c\u1da0 p)\nwk-last\u1d9c\u1da0 (input l p) = input (there l) \u03bb m \u2192 wk-last\u1d9c\u1da0 (p m)\n\nwk-,,\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 \u2192 EndedEnv \u0394\u2081 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\nwk-,,\u1d9c\u1da0 {\u0394\u2081 = \u03b5} p E = p\nwk-,,\u1d9c\u1da0 {\u0394\u2081 = \u0394\u2081 , d \u21a6 P} p (E , e) rewrite Ended-\u2261end e\n = wk-last\u1d9c\u1da0 (wk-,,\u1d9c\u1da0 p E)\n\nmodule _ {d P \u0394\u2080} where\n pre-wk-\u2208 : \u2200 {\u0394\u2081} \u2192 d \u21a6 P \u2208 \u0394\u2081 \u2192 d \u21a6 P \u2208 (\u0394\u2080 ,, \u0394\u2081)\n pre-wk-\u2208 here = here\n pre-wk-\u2208 (there l) = there (pre-wk-\u2208 l)\n\n{-\npre-wk\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 EndedEnv \u0394\u2080 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\npre-wk\u1d9c\u1da0 E\u0394\u2080 end = end {e = {!!}}\npre-wk\u1d9c\u1da0 {\u0394\u2080} {\u0394\u2081} E\u0394\u2080 (output l m p) =\n output (pre-wk-\u2208 l) m (pre-wk\u1d9c\u1da0 {\u0394\u2080} {{!\u0394\u2081!}} E\u0394\u2080 {!!})\npre-wk\u1d9c\u1da0 E\u0394\u2080 (input l p) = {!!}\n-}\n\nfwd-mix\u1d9c\u1da0 : \u2200 {\u0394 c d} P \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u22a2\u1d9c\u1da0 (\u0394 , c \u21a6 P , d \u21a6 dual P)\nfwd-mix\u1d9c\u1da0 end p = wk-last\u1d9c\u1da0 (wk-last\u1d9c\u1da0 p)\nfwd-mix\u1d9c\u1da0 (recv P) p = input (there here) \u03bb m \u2192 output here m (fwd-mix\u1d9c\u1da0 (P m) p)\nfwd-mix\u1d9c\u1da0 (send P) p = input here \u03bb m \u2192 output (there here) m (fwd-mix\u1d9c\u1da0 (P m) p)\n\nfwd\u1d9c\u1da0 : \u2200 c d {P} \u2192 \u22a2\u1d9c\u1da0 (\u03b5 , c \u21a6 P , d \u21a6 dual P)\nfwd\u1d9c\u1da0 _ _ {P} = fwd-mix\u1d9c\u1da0 {\u03b5} P end\n\n\u03b5,, : \u2200 \u0394 \u2192 \u03b5 ,, \u0394 \u2261 \u0394\n\u03b5,, \u03b5 = refl\n\u03b5,, (\u0394 , d \u21a6 P) rewrite \u03b5,, \u0394 = refl\n\npostulate\n exch\u1d9c\u1da0 :\n \u2200 \u0394\u2080 \u0394\u2081 \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394\u2081 ,, \u0394\u2080)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\n {-\nexch\u1d9c\u1da0 \u03b5 \u0394\u2081 p rewrite \u03b5,, \u0394\u2081 = p\nexch\u1d9c\u1da0 \u0394\u2080 \u03b5 p rewrite \u03b5,, \u0394\u2080 = p\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) end = {!!}\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 ._) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) (output here m p) = {!exch\u1d9c\u1da0!}\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) (output (there l) m p) = {!!}\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) (input l p) = {!!}\n-}\n\npre-wk\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 EndedEnv \u0394\u2080 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\npre-wk\u1d9c\u1da0 {\u0394\u2080} {\u0394\u2081} E p = exch\u1d9c\u1da0 \u0394\u2080 \u0394\u2081 (wk-,,\u1d9c\u1da0 p E)\n\nend-of : Env \u2192 Env\nend-of \u03b5 = \u03b5\nend-of (\u0394 , d \u21a6 P) = end-of \u0394 , d \u21a6 end\n\nend-of-Ended : \u2200 \u0394 \u2192 EndedEnv (end-of \u0394)\nend-of-Ended \u03b5 = _\nend-of-Ended (\u0394 , d \u21a6 P) = end-of-Ended \u0394 , _\n\nend-of-\u22ce : \u2200 \u0394 \u2192 [ \u0394 is \u0394 \u22ce end-of \u0394 ]\nend-of-\u22ce \u03b5 = \u03b5\nend-of-\u22ce (\u0394 , d \u21a6 P) = end-of-\u22ce \u0394 , d \u21a6\u2080 P\n\nend-of-,,-\u22ce : \u2200 \u0394\u2080 \u0394\u2081 \u2192 [ \u0394\u2080 ,, \u0394\u2081 is \u0394\u2080 ,, end-of \u0394\u2081 \u22ce end-of \u0394\u2080 ,, \u0394\u2081 ]\nend-of-,,-\u22ce \u0394\u2080 \u03b5 = end-of-\u22ce \u0394\u2080\nend-of-,,-\u22ce \u0394\u2080 (\u0394\u2081 , d \u21a6 P) = end-of-,,-\u22ce \u0394\u2080 \u0394\u2081 , d \u21a6\u2081 P\n\n,,-assoc : \u2200 {\u0394\u2080 \u0394\u2081 \u0394\u2082} \u2192 \u0394\u2080 ,, (\u0394\u2081 ,, \u0394\u2082) \u2261 (\u0394\u2080 ,, \u0394\u2081) ,, \u0394\u2082\n,,-assoc {\u0394\u2082 = \u03b5} = refl\n,,-assoc {\u0394\u2080} {\u0394\u2081} {\u0394\u2082 , d \u21a6 P} rewrite ,,-assoc {\u0394\u2080} {\u0394\u2081} {\u0394\u2082} = refl\n\ncut,,\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} d P\n (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 dual P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P))\n ---------------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\ncut,,\u1d9c\u1da0 {\u0394\u2080}{\u0394\u2081} d P p q =\n end-last\u1d9c\u1da0\n (cut\u1d9c\u1da0 \u0394\u209b d P\n (exch\u1d9c\u1da0 (\u0394\u2080 ,, end-of \u0394\u2081) (\u03b5 , d \u21a6 dual P)\n (tr \u22a2\u1d9c\u1da0_ (! (,,-assoc {\u03b5 , d \u21a6 dual P} {\u0394\u2080} {end-of \u0394\u2081}))\n (wk-,,\u1d9c\u1da0 {_} {end-of \u0394\u2081}\n (exch\u1d9c\u1da0 (\u03b5 , d \u21a6 dual P) _ p) (end-of-Ended _))))\n (pre-wk\u1d9c\u1da0 (end-of-Ended _) q))\n where \u0394\u209b = end-of-,,-\u22ce \u0394\u2080 \u0394\u2081\n\npostulate\n !cut,,\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} d P\n (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 dual P))\n ---------------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\n-- !cut,,\u1d9c\u1da0 {\u0394\u2080}{\u0394\u2081} d P p q = {!!}\n\n-- only the last two are exchanged, some more has to be done\nexch-last\u1d9c\u1da0 :\n \u2200 {\u0394 c d P Q} \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394 , c \u21a6 P , d \u21a6 Q)\n \u2192 \u22a2\u1d9c\u1da0 \u0394 , d \u21a6 Q , c \u21a6 P\nexch-last\u1d9c\u1da0 (end {e = (a , b) , c}) = end {e = (a , c) , b}\nexch-last\u1d9c\u1da0 (output here m p) = output (there here) m (exch-last\u1d9c\u1da0 p)\nexch-last\u1d9c\u1da0 (output (there here) m p) = output here m (exch-last\u1d9c\u1da0 p)\nexch-last\u1d9c\u1da0 (output (there (there l)) m p) = output (there (there l)) m (exch-last\u1d9c\u1da0 p)\nexch-last\u1d9c\u1da0 (input here p) = input (there here) \u03bb m \u2192 exch-last\u1d9c\u1da0 (p m)\nexch-last\u1d9c\u1da0 (input (there here) p) = input here \u03bb m \u2192 exch-last\u1d9c\u1da0 (p m)\nexch-last\u1d9c\u1da0 (input (there (there l)) p) = input (there (there l)) \u03bb m \u2192 exch-last\u1d9c\u1da0 (p m)\n\n{-\ndata Relabel : Env \u2192 Env \u2192 Set where\n \u03b5 : Relabel \u03b5 \u03b5\n _,_\u21a6_ : \u2200 {\u0394\u2080 \u0394\u2081 c d P} \u2192 Relabel \u0394\u2080 \u0394\u2081 \u2192 Relabel (\u0394\u2080 , c \u21a6 P) (\u0394\u2081 , d \u21a6 P)\n\nmodule _ where\n rebalel-\u2208 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 Relabel \u0394\u2080 \u0394\u2081\n (l : d \u21a6 P \u2208 \u0394\u2080)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081\n relabel : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 Relabel \u0394\u2080 \u0394\u2081\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081\n relabel end = {!end!}\n relabel (output l m p) = output {!l!} {!!} {!!}\n relabel (input l p) = {!!}\n\npar\u1d9c\u1da0 : \u2200 {\u0394 c} P Q\n (p : \u22a2\u1d9c\u1da0 (\u0394 , c \u21a6 P , c \u21a6 Q))\n \u2192 \u22a2\u1d9c\u1da0 (\u0394 , c \u21a6 (P \u214b' Q))\n-- TODO only one channel name!!!\n-- TODO empty context\n-- TODO try to match on 'p' first\nbroken-par\u1d9c\u1da0 : \u2200 {c d e} P Q\n (p : \u22a2\u1d9c\u1da0 (\u03b5 , c \u21a6 P , d \u21a6 Q))\n \u2192 \u22a2\u1d9c\u1da0 (\u03b5 , e \u21a6 (P \u214b' Q))\nbroken-par\u1d9c\u1da0 end Q p = {!end-last\u1d9c\u1da0 (exch-last\u1d9c\u1da0 p)!}\nbroken-par\u1d9c\u1da0 (com x P) end p = end-last\u1d9c\u1da0 {!p!}\nbroken-par\u1d9c\u1da0 (com x P) (com y Q) (end {e = _ , ()})\n\nbroken-par\u1d9c\u1da0 (com x P) (com .OUT Q) (output here m p)\n = output here R (output here m (broken-par\u1d9c\u1da0 _ _ p))\nbroken-par\u1d9c\u1da0 (com x P) (com .IN Q) (input here p)\n = output here R (input here \u03bb m \u2192 broken-par\u1d9c\u1da0 _ _ (p m))\n\nbroken-par\u1d9c\u1da0 (com .OUT P) (com y Q) (output (there here) m p)\n = output here L (output here m (broken-par\u1d9c\u1da0 _ _ p))\nbroken-par\u1d9c\u1da0 (com .IN P) (com y Q) (input (there here) p)\n = output here L (input here \u03bb m \u2192 broken-par\u1d9c\u1da0 _ _ (p m))\n\nbroken-par\u1d9c\u1da0 (com x P) (com y Q) (output (there (there ())) m p)\nbroken-par\u1d9c\u1da0 (com x P) (com y Q) (input (there (there ())) p)\n-}\n\nmodule _ {c d cd} where\n bi-fwd : \u2200 P Q \u2192 \u22a2\u1d9c\u1da0 (\u03b5 , cd \u21a6 P \u2297 Q , c \u21a6 dual P , d \u21a6 dual Q)\n\n private\n module _ {M} {{_ : SER M}} {P : M \u2192 Proto} {Q} where\n goL : \u2200 x \u2192 \u22a2\u1d9c\u1da0 \u03b5 , cd \u21a6 com x (\u03bb m \u2192 P m \u2297 Q)\n , c \u21a6 dual (com x P)\n , d \u21a6 dual Q\n\n goL IN = input (there (there here)) \u03bb m \u2192 output (there here) m (bi-fwd _ _)\n goL OUT = input (there here) \u03bb m \u2192 output (there (there here)) m (bi-fwd _ _)\n\n goR : \u2200 x \u2192 \u22a2\u1d9c\u1da0 \u03b5 , cd \u21a6 com x (\u03bb m \u2192 Q \u2297 P m)\n , c \u21a6 dual Q\n , d \u21a6 dual (com x P)\n goR IN = input (there (there here)) \u03bb m \u2192 output here m (bi-fwd _ _)\n goR OUT = input here \u03bb m \u2192 output (there (there here)) m (bi-fwd _ _)\n\n bi-fwd end Q = exch-last\u1d9c\u1da0 (wk-last\u1d9c\u1da0 (fwd\u1d9c\u1da0 _ _))\n bi-fwd (com x P) end = wk-last\u1d9c\u1da0 (fwd\u1d9c\u1da0 _ _)\n bi-fwd (com x P) (com y Q) = input (there (there here)) [L: goL x ,R: goR y ]\n\n module _ {\u0394\u2080 \u0394\u2081 P Q} where\n \u2297\u1d9c\u1da0 : (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , c \u21a6 P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 Q))\n ----------------------------------\n \u2192 \u22a2\u1d9c\u1da0 (\u0394\u2080 ,, \u0394\u2081 , cd \u21a6 (P \u2297 Q))\n \u2297\u1d9c\u1da0 p q = !cut,,\u1d9c\u1da0 _ _ p (!cut,,\u1d9c\u1da0 _ _ q (bi-fwd P Q))\n\n {-\nexch\u1d9c\u1da0 :\n \u2200 {\u0394 c d P Q} \u2192\n (l : c \u21a6 P \u2208 \u0394)\n (p : \u22a2\u1d9c\u1da0 \u0394 , d \u21a6 Q)\n \u2192 \u22a2\u1d9c\u1da0 \u0394 [ l \u2254 d \u21a6 Q ] , c \u21a6 P\nexch\u1d9c\u1da0 here p = exch-last\u1d9c\u1da0 p\nexch\u1d9c\u1da0 (there l) p = {!!}\n-}\n\n{-\nrot\u1d9c\u1da0 : \u2200 \u0394 {c P} \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394 , c \u21a6 P)\n \u2192 \u22a2\u1d9c\u1da0 \u03b5 , c \u21a6 P ,, \u0394\nrot\u1d9c\u1da0 \u03b5 p = p\nrot\u1d9c\u1da0 (\u0394 , d \u21a6 P) p = {!rot\u1d9c\u1da0 \u0394 p!}\n\nexch\u1d9c\u1da0 :\n \u2200 {\u0394\u2080} \u0394\u2081 {c d P Q} \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080 , c \u21a6 P , d \u21a6 Q ,, \u0394\u2081)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 , d \u21a6 Q , c \u21a6 P ,, \u0394\u2081\nexch\u1d9c\u1da0 \u03b5 p = exch-last\u1d9c\u1da0 p\nexch\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P) (end e) = end {!!}\nexch\u1d9c\u1da0 (\u0394\u2081 , d\u2081 \u21a6 ._) (output here m p) = output here m ({!exch\u1d9c\u1da0 \u0394\u2081 p!})\nexch\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P) (output (there l) m p) = {!!}\nexch\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P) (input l p) = {!!}\n-}\n\n_\u2286_ : (\u0394\u2080 \u0394\u2081 : Env) \u2192 Set\u2081\n_\u2287_ : (\u0394\u2080 \u0394\u2081 : Env) \u2192 Set\u2081\n\n\u0394\u2080 \u2286 \u0394\u2081 = \u2200 {d P} \u2192 d \u21a6 P \u2208 \u0394\u2080 \u2192 d \u21a6 P \u2208 \u0394\u2081\n\u0394\u2080 \u2287 \u0394\u2081 = \u0394\u2081 \u2286 \u0394\u2080\n\nget-put : \u2200 {d P \u0394 c Q} \u2192\n (l : d \u21a6 P \u2208 \u0394) \u2192 c \u21a6 Q \u2208 (\u0394 [ l \u2254 c \u21a6 Q ])\nget-put here = here\nget-put (there l) = there (get-put l)\n\n{-\n\u2286_[_\u2254_\u21a6_] : \u2200 {\u0394\u2080 \u0394\u2081} (f : \u0394\u2080 \u2286 \u0394\u2081)\n {d P} (l : d \u21a6 P \u2208 \u0394\u2080) (c : URI) (Q : Proto)\n \u2192 (\u0394\u2080 [ l \u2254 c \u21a6 Q ]) \u2286 (\u0394\u2081 [ f l \u2254 c \u21a6 Q ])\n\u2286 f [ l \u2254 c \u21a6 Q ] {d'} {P'} l' = {!!}\n\n(l : d \u21a6 P \u2208 \u0394)\n\u2192 \u0394 [ l \u2254 ]\n\nrecord _\u2248_ (\u0394\u2080 \u0394\u2081 : Env) : Set\u2081 where\n constructor _,_\n field\n \u2248\u2286 : \u0394\u2080 \u2286 \u0394\u2081\n \u2248\u2287 : \u0394\u2080 \u2287 \u0394\u2081\nopen _\u2248_ public\n\n\u2248_[_\u2254_\u21a6_] : \u2200 {\u0394\u2080 \u0394\u2081} (\u0394\u2091 : \u0394\u2080 \u2248 \u0394\u2081)\n {d P} (l : d \u21a6 P \u2208 \u0394\u2080) (c : URI) (Q : Proto)\n \u2192 (\u0394\u2080 [ l \u2254 c \u21a6 Q ]) \u2248 (\u0394\u2081 [ \u2248\u2286 \u0394\u2091 l \u2254 m ])\n\u2248 \u0394\u2091 [ here \u2254 m ] = {!!}\n\u2248 \u0394\u2091 [ there l \u2254 m ] = {!!}\n\n{-(\u03bb l' \u2192 {!\u2248\u2286 \u0394\u2091!}) , from\n where\n from : \u2200 {\u0394\u2080 \u0394\u2081 d io M} {P : M \u2192 Proto} {ser : SER M}\n {\u0394\u2091 : \u0394\u2080 \u2248 \u0394\u2081} {l : d \u21a6 com io P \u2208 \u0394\u2080} {m : M} {d\u2081} {P\u2081} \u2192\n d\u2081 \u21a6 P\u2081 \u2208 (\u0394\u2081 [ \u2248\u2286 \u0394\u2091 l \u2254 m ]) \u2192 d\u2081 \u21a6 P\u2081 \u2208 (\u0394\u2080 [ l \u2254 m ])\n from = {!!}\n\n\u2248\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081}\n (\u0394\u2091 : \u0394\u2080 \u2248 \u0394\u2081)\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080)\n -----------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081\n\u2248\u1d9c\u1da0 \u0394\u2091 (end {e = e}) = end {e = {!!}}\n\u2248\u1d9c\u1da0 \u0394\u2091 (output l m p) = output (\u2248\u2286 \u0394\u2091 l) m (\u2248\u1d9c\u1da0 (\u2248 \u0394\u2091 [ l \u2254 m ]) p)\n\u2248\u1d9c\u1da0 \u0394\u2091 (input l p) = {!!}\n-}\n-}\n\ncut-elim : \u2200 {\u0394} (p : \u22a2 \u0394)\n ------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\ncut-elim (end {e = e}) = end {e = e}\ncut-elim (output l m p) = output l m (cut-elim p)\ncut-elim (input l p) = input l (\u03bb m \u2192 cut-elim (p m))\ncut-elim (mix \u0394\u209b p q) = mix\u1d9c\u1da0 \u0394\u209b (cut-elim p) (cut-elim q)\ncut-elim (cut \u0394\u209b {P} {d} p q) = end-last\u1d9c\u1da0 (cut\u1d9c\u1da0 \u0394\u209b d P (cut-elim p) (cut-elim q))\ncut-elim (end-last p) = end-last\u1d9c\u1da0 (cut-elim p)\ncut-elim (wk-last p) = wk-last\u1d9c\u1da0 (cut-elim p)\ncut-elim (fwd c d) = fwd\u1d9c\u1da0 c d\ncut-elim (exch-last p) = exch-last\u1d9c\u1da0 (cut-elim p)\n\n{-\n\nstart : \u2200 {\u0394} P\n \u2192 \u22a2 [ clientURI \u21a6 dual P ]\n \u2192 (\u2200 d \u2192 \u22a2 (\u0394 , d \u21a6 P))\n \u2192 \u22a2 \u0394\nstart P p q = cut {!!} (... p) (q {!!})\n-}\n\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 : \u2200 {P d} \u2192 \u22a2\u1d9c\u1da0 [ d \u21a6 P ] \u2192 \u27e6 P \u27e7\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 {end} end = _\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 {com x P} (end {e = _ , ()})\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (output here m der) = m , \u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 der\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (output (there ()) m der)\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (input here x\u2081) m = \u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (x\u2081 m)\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (input (there ()) x\u2081)\n\nSatisfy : \u2200 {p d} P \u2192 (R : \u27e6 log P \u27e7 \u2192 Set p) \u2192 \u22a2 [ d \u21a6 P ] \u2192 Set p\nSatisfy P Rel d = (d' : \u27e6 dual P \u27e7) \u2192 Rel (telecom P (\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (cut-elim d)) d')\n\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 : \u2200 {P d} \u2192 \u27e6 P \u27e7 \u2192 \u22a2\u1d9c\u1da0 [ d \u21a6 P ]\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 {end} p = end\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 {recv P} p = input here (\u03bb m \u2192 \u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 (p m))\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 {send P} (m , p) = output here m (\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 p)\n\n\u27e6\u27e7\u2192\u22a2 : \u2200 {P d} \u2192 \u27e6 P \u27e7 \u2192 \u22a2 [ d \u21a6 P ]\n\u27e6\u27e7\u2192\u22a2 {end} p = end\n\u27e6\u27e7\u2192\u22a2 {recv P} p = input here (\u03bb m \u2192 \u27e6\u27e7\u2192\u22a2 (p m))\n\u27e6\u27e7\u2192\u22a2 {send P} (m , p) = output here m (\u27e6\u27e7\u2192\u22a2 p)\n\n{-\n\u22a2toProc : \u2200 {\u0394} \u2192 \u22a2 \u0394 \u2192 JSProc\n\u22a2toProc end = end\n\u22a2toProc (output {d = d} l m prg) = output d (serialize m) (\u22a2toProc prg)\n\u22a2toProc (input {d = d} l prg) = input d ([succeed: (\u03bb m \u2192 \u22a2toProc (prg m)) ,fail: error ] \u2218 parse)\n\u22a2toProc (start P prg x) = start (\u22a2toProc prg) (\u03bb d \u2192 \u22a2toProc (x d))\n\n\n\u22a2toProc-WT : \u2200 {\u0394} (der : \u22a2 \u0394) \u2192 \u0394 \u22a2 \u22a2toProc der\n\u22a2toProc-WT (end {x}) = end {_} {x}\n\u22a2toProc-WT (output {{x}} l m der) = output l (sym (rinv m)) (\u22a2toProc-WT der)\n\u22a2toProc-WT (input {{x}} l x\u2081) = input l \u03bb s m x \u2192\n subst (\u03bb X \u2192 _ [ l \u2254 m ] \u22a2 [succeed: (\u22a2toProc \u2218 x\u2081) ,fail: error ] X) x (\u22a2toProc-WT (x\u2081 m))\n\u22a2toProc-WT (start P der x) = start P (\u22a2toProc-WT der) (\u03bb d \u2192 \u22a2toProc-WT (x d))\n-}\n\n\u27e6_\u27e7E : Env \u2192 Set\n\u27e6 \u03b5 \u27e7E = \ud835\udfd9\n\u27e6 \u0394 , d \u21a6 P \u27e7E = \u27e6 \u0394 \u27e7E \u00d7 \u27e6 P \u27e7\n\nmapEnv : (Proto \u2192 Proto) \u2192 Env \u2192 Env\nmapEnv f \u03b5 = \u03b5\nmapEnv f (\u0394 , d \u21a6 P) = mapEnv f \u0394 , d \u21a6 f P\n\nmapEnv-all : \u2200 {P Q : URI \u2192 Proto \u2192 Set}{\u0394}{f : Proto \u2192 Proto}\n \u2192 (\u2200 d x \u2192 P d x \u2192 Q d (f x))\n \u2192 AllEnv P \u0394 \u2192 AllEnv Q (mapEnv f \u0394)\nmapEnv-all {\u0394 = \u03b5} f\u2081 \u2200\u0394 = \u2200\u0394\nmapEnv-all {\u0394 = \u0394 , d \u21a6 P\u2081} f\u2081 (\u2200\u0394 , P) = (mapEnv-all f\u2081 \u2200\u0394) , f\u2081 d P\u2081 P\n\nmapEnv-Ended : \u2200 {f : Proto \u2192 Proto}{\u0394} \u2192 Ended (f end)\n \u2192 AllEnv (\u03bb _ \u2192 Ended) \u0394 \u2192 AllEnv (\u03bb _ \u2192 Ended) (mapEnv f \u0394)\nmapEnv-Ended eq = mapEnv-all (\u03bb { d end _ \u2192 eq ; d (send P) () ; d (recv P) () })\n\nmapEnv-\u2208 : \u2200 {d P f \u0394} \u2192 d \u21a6 P \u2208 \u0394 \u2192 d \u21a6 f P \u2208 mapEnv f \u0394\nmapEnv-\u2208 here = here\nmapEnv-\u2208 (there der) = there (mapEnv-\u2208 der)\n\nmodule _ {d c M cf}{m : M}{{_ : M \u2243? SERIAL}}{p} where\n subst-lemma-one-point-four : \u2200 {\u0394}( l : d \u21a6 com c p \u2208 \u0394 ) \u2192\n let f = mapProto cf\n in (mapEnv f (\u0394 [ l \u2254 m ])) \u2261 (_[_\u2254_]{c = cf c} (mapEnv f \u0394) (mapEnv-\u2208 l) m)\n subst-lemma-one-point-four here = refl\n subst-lemma-one-point-four (there {d' = d'}{P'} l) = ap (\u03bb X \u2192 X , d' \u21a6 mapProto cf P') (subst-lemma-one-point-four l)\n\nmodule _ {d P} where\n project : \u2200 {\u0394} \u2192 d \u21a6 P \u2208 \u0394 \u2192 \u27e6 \u0394 \u27e7E \u2192 \u27e6 P \u27e7\n project here env = snd env\n project (there mem) env = project mem (fst env)\n\nempty : \u2200 {\u0394} \u2192 AllEnv (\u03bb _ \u2192 Ended) \u0394 \u2192 \u27e6 \u0394 \u27e7E\nempty {\u03b5} <> = _\nempty {\u0394 , d \u21a6 end} (fst , <>) = empty fst , _\nempty {\u0394 , d \u21a6 com x P} (fst , ())\n\nnoRecvInLog : \u2200 {d M}{{_ : M \u2243? SERIAL}}{P : M \u2192 _}{\u0394} \u2192 d \u21a6 recv P \u2208 mapEnv log \u0394 \u2192 \ud835\udfd8\nnoRecvInLog {\u0394 = \u03b5} ()\nnoRecvInLog {\u0394 = \u0394 , d\u2081 \u21a6 end} (there l) = noRecvInLog l\nnoRecvInLog {\u0394 = \u0394 , d\u2081 \u21a6 com x\u2081 P\u2081} (there l) = noRecvInLog l\n\nmodule _ {d M P}{{_ : M \u2243? SERIAL}} where\n lookup : \u2200 {\u0394}(l : d \u21a6 send P \u2208 \u0394) \u2192 \u27e6 \u0394 \u27e7E \u2192 \u03a3 M \u03bb m \u2192 \u27e6 \u0394 [ l \u2254 m ] \u27e7E\n lookup here (env , (m , p)) = m , (env , p)\n lookup (there l) (env , P') = let (m , env') = lookup l env in m , (env' , P')\n\n set : \u2200 {\u0394}(l : d \u21a6 recv P \u2208 \u0394) \u2192 \u27e6 \u0394 \u27e7E \u2192 (m : M) \u2192 \u27e6 \u0394 [ l \u2254 m ] \u27e7E\n set here (env , f) m = env , f m\n set (there l) (env , P') m = set l env m , P'\n\n set\u03a3 : \u2200 {\u0394}(l : d \u21a6 send P \u2208 \u0394) \u2192 (m : M) \u2192 \u27e6 \u0394 [ l \u2254 m ] \u27e7E \u2192 \u27e6 \u0394 \u27e7E\n set\u03a3 here m env = fst env , (m , snd env)\n set\u03a3 (there l) m env = set\u03a3 l m (fst env) , snd env\n\n {-\nforgetConc : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 mapEnv log \u0394 \u2192 \u27e6 mapEnv log \u0394 \u27e7E\nforgetConc (end e) = empty \u2026\nforgetConc {\u0394} (output l m der) = set\u03a3 l m (forgetConc {{!set\u03a3 l m!}} der) -- (forgetConc der)\nforgetConc (input l x\u2081) with noRecvInLog l\n... | ()\n-}\n\n\u22a2telecom : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u27e6 mapEnv dual \u0394 \u27e7E \u2192 \u22a2 mapEnv log \u0394\n\u22a2telecom end env = end {e = mapEnv-Ended _ \u2026}\n\u22a2telecom (output l m der) env = output (mapEnv-\u2208 l) m (subst (\u22a2_)\n (subst-lemma-one-point-four l) (\u22a2telecom der (subst \u27e6_\u27e7E (sym (subst-lemma-one-point-four l)) (set (mapEnv-\u2208 l) env m))))\n\u22a2telecom (input l x\u2081) env = let (m , env') = lookup (mapEnv-\u2208 l) env\n hyp = \u22a2telecom (x\u2081 m) (subst (\u27e6_\u27e7E) (sym (subst-lemma-one-point-four l)) env')\n in output (mapEnv-\u2208 l) m\n (subst (\u22a2_) (subst-lemma-one-point-four l) hyp)\n\n-- old version\n{-\ncut\u1d9c\u1da0 : \u2200 {\u0394 d P} \u2192 \u27e6 dual P \u27e7 \u2192 \u22a2\u1d9c\u1da0 \u0394 , d \u21a6 P \u2192 \u22a2\u1d9c\u1da0 \u0394\ncut\u1d9c\u1da0 D (end {allEnded = \u0394E , PE }) = end {allEnded = \u0394E}\ncut\u1d9c\u1da0 D (output here m E) = cut\u1d9c\u1da0 (D m) E\ncut\u1d9c\u1da0 D (output (there l) m E) = output l m (cut\u1d9c\u1da0 D E)\ncut\u1d9c\u1da0 (m , D) (input here x\u2081) = cut\u1d9c\u1da0 D (x\u2081 m)\ncut\u1d9c\u1da0 D (input (there l) x\u2081) = input l (\u03bb m \u2192 cut\u1d9c\u1da0 D (x\u2081 m))\n\ncut : \u2200 {\u0394 \u0394' \u0393 \u0393' d P} \u2192 \u22a2 \u0394 , clientURI \u21a6 dual P +++ \u0394' \u2192 \u22a2 \u0393 , d \u21a6 P +++ \u0393' \u2192 \u22a2 (\u0394 +++ \u0394') +++ (\u0393 +++ \u0393')\ncut D E = {!!}\n-}\n\n\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"open import proto\nopen import Types\nopen import prelude\nopen import uri\n\nmodule Terms where\n\ninfix 2 \u22a2\u02e2_ \u22a2_ \u22a2\u1d9c\u1da0_\n\ndata \u22a2_ : (\u0394 : Env) \u2192 Set\u2081 where\n end : \u2200{\u0394}{e : EndedEnv \u0394}\n ------------------\n \u2192 \u22a2 \u0394\n\n output : \u2200 {\u0394 d M P}{{_ : SER M}}\n (l : d \u21a6 send P \u2208 \u0394)(m : M)\n (p : \u22a2 \u0394 [ l \u2254 m ])\n -------------------\n \u2192 \u22a2 \u0394\n\n input : \u2200 {\u0394 d M}{P : M \u2192 _}{{_ : SER M}}\n (l : d \u21a6 recv P \u2208 \u0394)\n (p : \u2200 m \u2192 \u22a2 \u0394 [ l \u2254 m ])\n ----------------------\n \u2192 \u22a2 \u0394\n\n mix : \u2200 {\u0394 \u0394\u2080 \u0394\u2081} (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ])\n (p : \u22a2 \u0394\u2080) (q : \u22a2 \u0394\u2081)\n --------------------\n \u2192 \u22a2 \u0394\n\n cut : \u2200 {\u0394 \u0394\u2080 \u0394\u2081} (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ]) {P d}\n (p : \u22a2 (\u0394\u2080 , d \u21a6 dual P))\n (q : \u22a2 (\u0394\u2081 , d \u21a6 P))\n ---------------------\n \u2192 \u22a2 \u0394\n\n fwd : \u2200 c d {P} \u2192 \u22a2 (\u03b5 , c \u21a6 P , d \u21a6 dual P)\n\n exch-last :\n \u2200 {\u0394 c d P Q} \u2192\n (p : \u22a2 \u0394 , c \u21a6 P , d \u21a6 Q)\n \u2192 \u22a2 \u0394 , d \u21a6 Q , c \u21a6 P\n\n wk-last : \u2200 {\u0394 d}\n (p : \u22a2 \u0394)\n -----------------------\n \u2192 \u22a2 (\u0394 , d \u21a6 end)\n\n end-last : \u2200 {\u0394 d}\n (p : \u22a2 (\u0394 , d \u21a6 end))\n ----------------------\n \u2192 \u22a2 \u0394\n\ndata \u22a2\u1d9c\u1da0_ (\u0394 : Env) : Set\u2081 where\n end : {e : EndedEnv \u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394\n\n output : \u2200 {d M P}{{_ : SER M}}\n (l : d \u21a6 send P \u2208 \u0394) \u2192 (m : M)\n (p : \u22a2\u1d9c\u1da0 \u0394 [ l \u2254 m ])\n --------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\n\n input : \u2200 {d M}{P : M \u2192 _}{{_ : SER M}}\n (l : d \u21a6 recv P \u2208 \u0394)\n (p : \u2200 m \u2192 \u22a2\u1d9c\u1da0 \u0394 [ l \u2254 m ])\n ----------------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\n\n-- The \u0394 for the server contains the view point of the clients\n-- The point is that the meaning of _,_ in \u0394 is \u2297 while it\n-- is \u214b in \u22a2\u1d9c\u1da0\nrecord \u22a2\u02e2_ (\u0394 : Env) : Set\u2081 where\n coinductive\n field\n server-output :\n \u2200 {d M}{{_ : SER M}}{P : M \u2192 Proto}\n (l : d \u21a6 recv P \u2208 \u0394) \u2192\n \u03a3 M \u03bb m \u2192 \u22a2\u02e2 \u0394 [ l \u2254 m ]\n server-input :\n \u2200 {d M}{{_ : SER M}}{P : M \u2192 Proto}\n (l : d \u21a6 send P \u2208 \u0394)\n (m : M) \u2192 \u22a2\u02e2 \u0394 [ l \u2254 m ]\nopen \u22a2\u02e2_ public\n\n-- This is just to confirm that we have enough cases\ntelecom' : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u22a2\u02e2 \u0394 \u2192 \ud835\udfd9\ntelecom' end q = _\ntelecom' (output l m p) q\n = telecom' p (server-input q l m)\ntelecom' (input l p) q\n = case server-output q l of \u03bb { (m , s) \u2192\n telecom' (p m) s }\n\nembed\u1d9c\u1da0 : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u22a2 \u0394\nembed\u1d9c\u1da0 (end {e = e}) = end {e = e}\nembed\u1d9c\u1da0 (output l m p) = output l m (embed\u1d9c\u1da0 p)\nembed\u1d9c\u1da0 (input l p) = input l \u03bb m \u2192 embed\u1d9c\u1da0 (p m)\n\nmix\u1d9c\u1da0 : \u2200 {\u0394 \u0394\u2080 \u0394\u2081} (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ])\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080)\n (q : \u22a2\u1d9c\u1da0 \u0394\u2081)\n -------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\nmix\u1d9c\u1da0 \u0394\u209b end q = tr \u22a2\u1d9c\u1da0_ (Zip-identity \u0394\u209b) q\nmix\u1d9c\u1da0 \u0394\u209b (output l m p) q\n = output (Zip-com\u2208\u2080 \u0394\u209b l) m (mix\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) p q)\nmix\u1d9c\u1da0 \u0394\u209b (input l p) q\n = input (Zip-com\u2208\u2080 \u0394\u209b l) \u03bb m \u2192 mix\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) (p m) q\n\ncut\u1d9c\u1da0 : \u2200 {\u0394 \u0394\u2080 \u0394\u2081}\n (\u0394\u209b : [ \u0394 is \u0394\u2080 \u22ce \u0394\u2081 ]) d P\n (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 dual P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P))\n ---------------------------\n \u2192 \u22a2\u1d9c\u1da0 (\u0394 , d \u21a6 end)\ncut\u1d9c\u1da0 \u0394\u209b d end p q = mix\u1d9c\u1da0 (\u0394\u209b , d \u21a6\u2080 end) p q\n\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) (output here m p) (input here q) = cut\u1d9c\u1da0 \u0394\u209b d (P m) p (q m)\ncut\u1d9c\u1da0 \u0394\u209b d (send P) (input here p) (output here m q) = cut\u1d9c\u1da0 \u0394\u209b d (P m) (p m) q\n\ncut\u1d9c\u1da0 \u0394\u209b d (send P) (output (there l) m p) q\n = output (there (Zip-com\u2208\u2080 \u0394\u209b l)) m (cut\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) d _ p q)\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) (output (there l) m p) q\n = output (there (Zip-com\u2208\u2080 \u0394\u209b l)) m (cut\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) d _ p q)\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) (input (there l) p) q\n = input (there (Zip-com\u2208\u2080 \u0394\u209b l)) \u03bb m \u2192 cut\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) d _ (p m) q\ncut\u1d9c\u1da0 \u0394\u209b d (send P) (input (there l) p) q\n = input (there (Zip-com\u2208\u2080 \u0394\u209b l)) \u03bb m \u2192 cut\u1d9c\u1da0 (Zip-\u2254\u2080 l \u0394\u209b) d _ (p m) q\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) p (output (there l) m q)\n = output (there (Zip-com\u2208\u2081 \u0394\u209b l)) m (cut\u1d9c\u1da0 (Zip-\u2254\u2081 l \u0394\u209b) d _ p q)\ncut\u1d9c\u1da0 \u0394\u209b d (send P) p (output (there l) m q)\n = output (there (Zip-com\u2208\u2081 \u0394\u209b l)) m (cut\u1d9c\u1da0 (Zip-\u2254\u2081 l \u0394\u209b) d _ p q)\ncut\u1d9c\u1da0 \u0394\u209b d (send P) p (input (there l) q)\n = input (there (Zip-com\u2208\u2081 \u0394\u209b l)) \u03bb m \u2192 cut\u1d9c\u1da0 (Zip-\u2254\u2081 l \u0394\u209b) d _ p (q m)\ncut\u1d9c\u1da0 \u0394\u209b d (recv P) p (input (there l) q)\n = input (there (Zip-com\u2208\u2081 \u0394\u209b l)) \u03bb m \u2192 cut\u1d9c\u1da0 (Zip-\u2254\u2081 l \u0394\u209b) d _ p (q m)\n\ncut\u1d9c\u1da0 _ _ (com _ _) (end {e = _ , ()}) _\ncut\u1d9c\u1da0 _ _ (com _ _) _ (end {e = _ , ()})\n\nend-last\u1d9c\u1da0 : \u2200 {\u0394 d}\n (p : \u22a2\u1d9c\u1da0 (\u0394 , d \u21a6 end))\n -----------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\nend-last\u1d9c\u1da0 (end {e = e , _}) = end {e = e}\nend-last\u1d9c\u1da0 (output (there l) m p) = output l m (end-last\u1d9c\u1da0 p)\nend-last\u1d9c\u1da0 (input (there l) p) = input l \u03bb m \u2192 end-last\u1d9c\u1da0 (p m)\n\nwk-last\u1d9c\u1da0 : \u2200 {\u0394 d}\n (p : \u22a2\u1d9c\u1da0 \u0394)\n -----------------------\n \u2192 \u22a2\u1d9c\u1da0 (\u0394 , d \u21a6 end)\nwk-last\u1d9c\u1da0 end = end {e = \u2026 , _}\nwk-last\u1d9c\u1da0 (output l m p) = output (there l) m (wk-last\u1d9c\u1da0 p)\nwk-last\u1d9c\u1da0 (input l p) = input (there l) \u03bb m \u2192 wk-last\u1d9c\u1da0 (p m)\n\nwk-,,\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 \u2192 EndedEnv \u0394\u2081 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\nwk-,,\u1d9c\u1da0 {\u0394\u2081 = \u03b5} p E = p\nwk-,,\u1d9c\u1da0 {\u0394\u2081 = \u0394\u2081 , d \u21a6 P} p (E , e) rewrite Ended-\u2261end e\n = wk-last\u1d9c\u1da0 (wk-,,\u1d9c\u1da0 p E)\n\nmodule _ {d P \u0394\u2080} where\n pre-wk-\u2208 : \u2200 {\u0394\u2081} \u2192 d \u21a6 P \u2208 \u0394\u2081 \u2192 d \u21a6 P \u2208 (\u0394\u2080 ,, \u0394\u2081)\n pre-wk-\u2208 here = here\n pre-wk-\u2208 (there l) = there (pre-wk-\u2208 l)\n\n{-\npre-wk\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 EndedEnv \u0394\u2080 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\npre-wk\u1d9c\u1da0 E\u0394\u2080 end = end {e = {!!}}\npre-wk\u1d9c\u1da0 {\u0394\u2080} {\u0394\u2081} E\u0394\u2080 (output l m p) =\n output (pre-wk-\u2208 l) m (pre-wk\u1d9c\u1da0 {\u0394\u2080} {{!\u0394\u2081!}} E\u0394\u2080 {!!})\npre-wk\u1d9c\u1da0 E\u0394\u2080 (input l p) = {!!}\n-}\n\nfwd-mix\u1d9c\u1da0 : \u2200 {\u0394 c d} P \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u22a2\u1d9c\u1da0 (\u0394 , c \u21a6 P , d \u21a6 dual P)\nfwd-mix\u1d9c\u1da0 end p = wk-last\u1d9c\u1da0 (wk-last\u1d9c\u1da0 p)\nfwd-mix\u1d9c\u1da0 (recv P) p = input (there here) \u03bb m \u2192 output here m (fwd-mix\u1d9c\u1da0 (P m) p)\nfwd-mix\u1d9c\u1da0 (send P) p = input here \u03bb m \u2192 output (there here) m (fwd-mix\u1d9c\u1da0 (P m) p)\n\nfwd\u1d9c\u1da0 : \u2200 c d {P} \u2192 \u22a2\u1d9c\u1da0 (\u03b5 , c \u21a6 P , d \u21a6 dual P)\nfwd\u1d9c\u1da0 _ _ {P} = fwd-mix\u1d9c\u1da0 {\u03b5} P end\n\n\u03b5,, : \u2200 \u0394 \u2192 \u03b5 ,, \u0394 \u2261 \u0394\n\u03b5,, \u03b5 = refl\n\u03b5,, (\u0394 , d \u21a6 P) rewrite \u03b5,, \u0394 = refl\n\npostulate\n exch\u1d9c\u1da0 :\n \u2200 \u0394\u2080 \u0394\u2081 \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394\u2081 ,, \u0394\u2080)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\n {-\nexch\u1d9c\u1da0 \u03b5 \u0394\u2081 p rewrite \u03b5,, \u0394\u2081 = p\nexch\u1d9c\u1da0 \u0394\u2080 \u03b5 p rewrite \u03b5,, \u0394\u2080 = p\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) end = {!!}\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 ._) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) (output here m p) = {!exch\u1d9c\u1da0!}\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) (output (there l) m p) = {!!}\nexch\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P) (\u0394\u2081 , d\u2081 \u21a6 P\u2081) (input l p) = {!!}\n-}\n\npre-wk\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 EndedEnv \u0394\u2080 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081 \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\npre-wk\u1d9c\u1da0 {\u0394\u2080} {\u0394\u2081} E p = exch\u1d9c\u1da0 \u0394\u2080 \u0394\u2081 (wk-,,\u1d9c\u1da0 p E)\n\nend-of : Env \u2192 Env\nend-of \u03b5 = \u03b5\nend-of (\u0394 , d \u21a6 P) = end-of \u0394 , d \u21a6 end\n\nend-of-Ended : \u2200 \u0394 \u2192 EndedEnv (end-of \u0394)\nend-of-Ended \u03b5 = _\nend-of-Ended (\u0394 , d \u21a6 P) = end-of-Ended \u0394 , _\n\nend-of-\u22ce : \u2200 \u0394 \u2192 [ \u0394 is \u0394 \u22ce end-of \u0394 ]\nend-of-\u22ce \u03b5 = \u03b5\nend-of-\u22ce (\u0394 , d \u21a6 P) = end-of-\u22ce \u0394 , d \u21a6\u2080 P\n\nend-of-,,-\u22ce : \u2200 \u0394\u2080 \u0394\u2081 \u2192 [ \u0394\u2080 ,, \u0394\u2081 is \u0394\u2080 ,, end-of \u0394\u2081 \u22ce end-of \u0394\u2080 ,, \u0394\u2081 ]\nend-of-,,-\u22ce \u0394\u2080 \u03b5 = end-of-\u22ce \u0394\u2080\nend-of-,,-\u22ce \u0394\u2080 (\u0394\u2081 , d \u21a6 P) = end-of-,,-\u22ce \u0394\u2080 \u0394\u2081 , d \u21a6\u2081 P\n\n,,-assoc : \u2200 {\u0394\u2080 \u0394\u2081 \u0394\u2082} \u2192 \u0394\u2080 ,, (\u0394\u2081 ,, \u0394\u2082) \u2261 (\u0394\u2080 ,, \u0394\u2081) ,, \u0394\u2082\n,,-assoc {\u0394\u2082 = \u03b5} = refl\n,,-assoc {\u0394\u2080} {\u0394\u2081} {\u0394\u2082 , d \u21a6 P} rewrite ,,-assoc {\u0394\u2080} {\u0394\u2081} {\u0394\u2082} = refl\n\ncut,,\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} d P\n (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 dual P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P))\n ---------------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\ncut,,\u1d9c\u1da0 {\u0394\u2080}{\u0394\u2081} d P p q =\n end-last\u1d9c\u1da0\n (cut\u1d9c\u1da0 \u0394\u209b d P\n (exch\u1d9c\u1da0 (\u0394\u2080 ,, end-of \u0394\u2081) (\u03b5 , d \u21a6 dual P)\n (tr \u22a2\u1d9c\u1da0_ (! (,,-assoc {\u03b5 , d \u21a6 dual P} {\u0394\u2080} {end-of \u0394\u2081}))\n (wk-,,\u1d9c\u1da0\n (exch\u1d9c\u1da0 (\u03b5 , d \u21a6 dual P) _ p) (end-of-Ended _))))\n (pre-wk\u1d9c\u1da0 (end-of-Ended _) q))\n where \u0394\u209b = end-of-,,-\u22ce \u0394\u2080 \u0394\u2081\n\npostulate\n !cut,,\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081} d P\n (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , d \u21a6 P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 dual P))\n ---------------------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 ,, \u0394\u2081\n-- !cut,,\u1d9c\u1da0 {\u0394\u2080}{\u0394\u2081} d P p q = {!!}\n\n-- only the last two are exchanged, some more has to be done\nexch-last\u1d9c\u1da0 :\n \u2200 {\u0394 c d P Q} \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394 , c \u21a6 P , d \u21a6 Q)\n \u2192 \u22a2\u1d9c\u1da0 \u0394 , d \u21a6 Q , c \u21a6 P\nexch-last\u1d9c\u1da0 (end {e = (a , b) , c}) = end {e = (a , c) , b}\nexch-last\u1d9c\u1da0 (output here m p) = output (there here) m (exch-last\u1d9c\u1da0 p)\nexch-last\u1d9c\u1da0 (output (there here) m p) = output here m (exch-last\u1d9c\u1da0 p)\nexch-last\u1d9c\u1da0 (output (there (there l)) m p) = output (there (there l)) m (exch-last\u1d9c\u1da0 p)\nexch-last\u1d9c\u1da0 (input here p) = input (there here) \u03bb m \u2192 exch-last\u1d9c\u1da0 (p m)\nexch-last\u1d9c\u1da0 (input (there here) p) = input here \u03bb m \u2192 exch-last\u1d9c\u1da0 (p m)\nexch-last\u1d9c\u1da0 (input (there (there l)) p) = input (there (there l)) \u03bb m \u2192 exch-last\u1d9c\u1da0 (p m)\n\n{-\ndata Relabel : Env \u2192 Env \u2192 Set where\n \u03b5 : Relabel \u03b5 \u03b5\n _,_\u21a6_ : \u2200 {\u0394\u2080 \u0394\u2081 c d P} \u2192 Relabel \u0394\u2080 \u0394\u2081 \u2192 Relabel (\u0394\u2080 , c \u21a6 P) (\u0394\u2081 , d \u21a6 P)\n\nmodule _ where\n rebalel-\u2208 : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 Relabel \u0394\u2080 \u0394\u2081\n (l : d \u21a6 P \u2208 \u0394\u2080)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081\n relabel : \u2200 {\u0394\u2080 \u0394\u2081} \u2192 Relabel \u0394\u2080 \u0394\u2081\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081\n relabel end = {!end!}\n relabel (output l m p) = output {!l!} {!!} {!!}\n relabel (input l p) = {!!}\n\npar\u1d9c\u1da0 : \u2200 {\u0394 c} P Q\n (p : \u22a2\u1d9c\u1da0 (\u0394 , c \u21a6 P , c \u21a6 Q))\n \u2192 \u22a2\u1d9c\u1da0 (\u0394 , c \u21a6 (P \u214b' Q))\n-- TODO only one channel name!!!\n-- TODO empty context\n-- TODO try to match on 'p' first\nbroken-par\u1d9c\u1da0 : \u2200 {c d e} P Q\n (p : \u22a2\u1d9c\u1da0 (\u03b5 , c \u21a6 P , d \u21a6 Q))\n \u2192 \u22a2\u1d9c\u1da0 (\u03b5 , e \u21a6 (P \u214b' Q))\nbroken-par\u1d9c\u1da0 end Q p = {!end-last\u1d9c\u1da0 (exch-last\u1d9c\u1da0 p)!}\nbroken-par\u1d9c\u1da0 (com x P) end p = end-last\u1d9c\u1da0 {!p!}\nbroken-par\u1d9c\u1da0 (com x P) (com y Q) (end {e = _ , ()})\n\nbroken-par\u1d9c\u1da0 (com x P) (com .OUT Q) (output here m p)\n = output here R (output here m (broken-par\u1d9c\u1da0 _ _ p))\nbroken-par\u1d9c\u1da0 (com x P) (com .IN Q) (input here p)\n = output here R (input here \u03bb m \u2192 broken-par\u1d9c\u1da0 _ _ (p m))\n\nbroken-par\u1d9c\u1da0 (com .OUT P) (com y Q) (output (there here) m p)\n = output here L (output here m (broken-par\u1d9c\u1da0 _ _ p))\nbroken-par\u1d9c\u1da0 (com .IN P) (com y Q) (input (there here) p)\n = output here L (input here \u03bb m \u2192 broken-par\u1d9c\u1da0 _ _ (p m))\n\nbroken-par\u1d9c\u1da0 (com x P) (com y Q) (output (there (there ())) m p)\nbroken-par\u1d9c\u1da0 (com x P) (com y Q) (input (there (there ())) p)\n-}\n\nmodule _ {c d cd} where\n bi-fwd : \u2200 P Q \u2192 \u22a2\u1d9c\u1da0 (\u03b5 , cd \u21a6 P \u2297 Q , c \u21a6 dual P , d \u21a6 dual Q)\n\n private\n module _ {M} {{_ : SER M}} {P : M \u2192 Proto} {Q} where\n goL : \u2200 x \u2192 \u22a2\u1d9c\u1da0 \u03b5 , cd \u21a6 com x (\u03bb m \u2192 P m \u2297 Q)\n , c \u21a6 dual (com x P)\n , d \u21a6 dual Q\n\n goL IN = input (there (there here)) \u03bb m \u2192 output (there here) m (bi-fwd _ _)\n goL OUT = input (there here) \u03bb m \u2192 output (there (there here)) m (bi-fwd _ _)\n\n goR : \u2200 x \u2192 \u22a2\u1d9c\u1da0 \u03b5 , cd \u21a6 com x (\u03bb m \u2192 Q \u2297 P m)\n , c \u21a6 dual Q\n , d \u21a6 dual (com x P)\n goR IN = input (there (there here)) \u03bb m \u2192 output here m (bi-fwd _ _)\n goR OUT = input here \u03bb m \u2192 output (there (there here)) m (bi-fwd _ _)\n\n bi-fwd end Q = exch-last\u1d9c\u1da0 (wk-last\u1d9c\u1da0 (fwd\u1d9c\u1da0 _ _))\n bi-fwd (com x P) end = wk-last\u1d9c\u1da0 (fwd\u1d9c\u1da0 _ _)\n bi-fwd (com x P) (com y Q) = input (there (there here)) [L: goL x ,R: goR y ]\n\n module _ {\u0394\u2080 \u0394\u2081 P Q} where\n \u2297\u1d9c\u1da0 : (p : \u22a2\u1d9c\u1da0 (\u0394\u2080 , c \u21a6 P))\n (q : \u22a2\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 Q))\n ----------------------------------\n \u2192 \u22a2\u1d9c\u1da0 (\u0394\u2080 ,, \u0394\u2081 , cd \u21a6 (P \u2297 Q))\n \u2297\u1d9c\u1da0 p q = !cut,,\u1d9c\u1da0 _ _ p (!cut,,\u1d9c\u1da0 _ _ q (bi-fwd P Q))\n\n {-\nexch\u1d9c\u1da0 :\n \u2200 {\u0394 c d P Q} \u2192\n (l : c \u21a6 P \u2208 \u0394)\n (p : \u22a2\u1d9c\u1da0 \u0394 , d \u21a6 Q)\n \u2192 \u22a2\u1d9c\u1da0 \u0394 [ l \u2254 d \u21a6 Q ] , c \u21a6 P\nexch\u1d9c\u1da0 here p = exch-last\u1d9c\u1da0 p\nexch\u1d9c\u1da0 (there l) p = {!!}\n-}\n\n{-\nrot\u1d9c\u1da0 : \u2200 \u0394 {c P} \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394 , c \u21a6 P)\n \u2192 \u22a2\u1d9c\u1da0 \u03b5 , c \u21a6 P ,, \u0394\nrot\u1d9c\u1da0 \u03b5 p = p\nrot\u1d9c\u1da0 (\u0394 , d \u21a6 P) p = {!rot\u1d9c\u1da0 \u0394 p!}\n\nexch\u1d9c\u1da0 :\n \u2200 {\u0394\u2080} \u0394\u2081 {c d P Q} \u2192\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080 , c \u21a6 P , d \u21a6 Q ,, \u0394\u2081)\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2080 , d \u21a6 Q , c \u21a6 P ,, \u0394\u2081\nexch\u1d9c\u1da0 \u03b5 p = exch-last\u1d9c\u1da0 p\nexch\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P) (end e) = end {!!}\nexch\u1d9c\u1da0 (\u0394\u2081 , d\u2081 \u21a6 ._) (output here m p) = output here m ({!exch\u1d9c\u1da0 \u0394\u2081 p!})\nexch\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P) (output (there l) m p) = {!!}\nexch\u1d9c\u1da0 (\u0394\u2081 , d \u21a6 P) (input l p) = {!!}\n-}\n\n_\u2286_ : (\u0394\u2080 \u0394\u2081 : Env) \u2192 Set\u2081\n_\u2287_ : (\u0394\u2080 \u0394\u2081 : Env) \u2192 Set\u2081\n\n\u0394\u2080 \u2286 \u0394\u2081 = \u2200 {d P} \u2192 d \u21a6 P \u2208 \u0394\u2080 \u2192 d \u21a6 P \u2208 \u0394\u2081\n\u0394\u2080 \u2287 \u0394\u2081 = \u0394\u2081 \u2286 \u0394\u2080\n\nget-put : \u2200 {d P \u0394 c Q} \u2192\n (l : d \u21a6 P \u2208 \u0394) \u2192 c \u21a6 Q \u2208 (\u0394 [ l \u2254 c \u21a6 Q ])\nget-put here = here\nget-put (there l) = there (get-put l)\n\n{-\n\u2286_[_\u2254_\u21a6_] : \u2200 {\u0394\u2080 \u0394\u2081} (f : \u0394\u2080 \u2286 \u0394\u2081)\n {d P} (l : d \u21a6 P \u2208 \u0394\u2080) (c : URI) (Q : Proto)\n \u2192 (\u0394\u2080 [ l \u2254 c \u21a6 Q ]) \u2286 (\u0394\u2081 [ f l \u2254 c \u21a6 Q ])\n\u2286 f [ l \u2254 c \u21a6 Q ] {d'} {P'} l' = {!!}\n\n(l : d \u21a6 P \u2208 \u0394)\n\u2192 \u0394 [ l \u2254 ]\n\nrecord _\u2248_ (\u0394\u2080 \u0394\u2081 : Env) : Set\u2081 where\n constructor _,_\n field\n \u2248\u2286 : \u0394\u2080 \u2286 \u0394\u2081\n \u2248\u2287 : \u0394\u2080 \u2287 \u0394\u2081\nopen _\u2248_ public\n\n\u2248_[_\u2254_\u21a6_] : \u2200 {\u0394\u2080 \u0394\u2081} (\u0394\u2091 : \u0394\u2080 \u2248 \u0394\u2081)\n {d P} (l : d \u21a6 P \u2208 \u0394\u2080) (c : URI) (Q : Proto)\n \u2192 (\u0394\u2080 [ l \u2254 c \u21a6 Q ]) \u2248 (\u0394\u2081 [ \u2248\u2286 \u0394\u2091 l \u2254 m ])\n\u2248 \u0394\u2091 [ here \u2254 m ] = {!!}\n\u2248 \u0394\u2091 [ there l \u2254 m ] = {!!}\n\n{-(\u03bb l' \u2192 {!\u2248\u2286 \u0394\u2091!}) , from\n where\n from : \u2200 {\u0394\u2080 \u0394\u2081 d io M} {P : M \u2192 Proto} {ser : SER M}\n {\u0394\u2091 : \u0394\u2080 \u2248 \u0394\u2081} {l : d \u21a6 com io P \u2208 \u0394\u2080} {m : M} {d\u2081} {P\u2081} \u2192\n d\u2081 \u21a6 P\u2081 \u2208 (\u0394\u2081 [ \u2248\u2286 \u0394\u2091 l \u2254 m ]) \u2192 d\u2081 \u21a6 P\u2081 \u2208 (\u0394\u2080 [ l \u2254 m ])\n from = {!!}\n\n\u2248\u1d9c\u1da0 : \u2200 {\u0394\u2080 \u0394\u2081}\n (\u0394\u2091 : \u0394\u2080 \u2248 \u0394\u2081)\n (p : \u22a2\u1d9c\u1da0 \u0394\u2080)\n -----------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\u2081\n\u2248\u1d9c\u1da0 \u0394\u2091 (end {e = e}) = end {e = {!!}}\n\u2248\u1d9c\u1da0 \u0394\u2091 (output l m p) = output (\u2248\u2286 \u0394\u2091 l) m (\u2248\u1d9c\u1da0 (\u2248 \u0394\u2091 [ l \u2254 m ]) p)\n\u2248\u1d9c\u1da0 \u0394\u2091 (input l p) = {!!}\n-}\n-}\n\ncut-elim : \u2200 {\u0394} (p : \u22a2 \u0394)\n ------------\n \u2192 \u22a2\u1d9c\u1da0 \u0394\ncut-elim (end {e = e}) = end {e = e}\ncut-elim (output l m p) = output l m (cut-elim p)\ncut-elim (input l p) = input l (\u03bb m \u2192 cut-elim (p m))\ncut-elim (mix \u0394\u209b p q) = mix\u1d9c\u1da0 \u0394\u209b (cut-elim p) (cut-elim q)\ncut-elim (cut \u0394\u209b {P} {d} p q) = end-last\u1d9c\u1da0 (cut\u1d9c\u1da0 \u0394\u209b d P (cut-elim p) (cut-elim q))\ncut-elim (end-last p) = end-last\u1d9c\u1da0 (cut-elim p)\ncut-elim (wk-last p) = wk-last\u1d9c\u1da0 (cut-elim p)\ncut-elim (fwd c d) = fwd\u1d9c\u1da0 c d\ncut-elim (exch-last p) = exch-last\u1d9c\u1da0 (cut-elim p)\n\n{-\n\nstart : \u2200 {\u0394} P\n \u2192 \u22a2 [ clientURI \u21a6 dual P ]\n \u2192 (\u2200 d \u2192 \u22a2 (\u0394 , d \u21a6 P))\n \u2192 \u22a2 \u0394\nstart P p q = cut {!!} (... p) (q {!!})\n-}\n\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 : \u2200 {P d} \u2192 \u22a2\u1d9c\u1da0 [ d \u21a6 P ] \u2192 \u27e6 P \u27e7\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 {end} end = _\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 {com x P} (end {e = _ , ()})\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (output here m der) = m , \u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 der\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (output (there ()) m der)\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (input here x\u2081) m = \u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (x\u2081 m)\n\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (input (there ()) x\u2081)\n\nSatisfy : \u2200 {p d} P \u2192 (R : \u27e6 log P \u27e7 \u2192 Set p) \u2192 \u22a2 [ d \u21a6 P ] \u2192 Set p\nSatisfy P Rel d = (d' : \u27e6 dual P \u27e7) \u2192 Rel (telecom P (\u22a2\u1d9c\u1da0\u2192\u27e6\u27e7 (cut-elim d)) d')\n\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 : \u2200 {P d} \u2192 \u27e6 P \u27e7 \u2192 \u22a2\u1d9c\u1da0 [ d \u21a6 P ]\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 {end} p = end\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 {recv P} p = input here (\u03bb m \u2192 \u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 (p m))\n\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 {send P} (m , p) = output here m (\u27e6\u27e7\u2192\u22a2\u1d9c\u1da0 p)\n\n\u27e6\u27e7\u2192\u22a2 : \u2200 {P d} \u2192 \u27e6 P \u27e7 \u2192 \u22a2 [ d \u21a6 P ]\n\u27e6\u27e7\u2192\u22a2 {end} p = end\n\u27e6\u27e7\u2192\u22a2 {recv P} p = input here (\u03bb m \u2192 \u27e6\u27e7\u2192\u22a2 (p m))\n\u27e6\u27e7\u2192\u22a2 {send P} (m , p) = output here m (\u27e6\u27e7\u2192\u22a2 p)\n\n{-\n\u22a2toProc : \u2200 {\u0394} \u2192 \u22a2 \u0394 \u2192 JSProc\n\u22a2toProc end = end\n\u22a2toProc (output {d = d} l m prg) = output d (serialize m) (\u22a2toProc prg)\n\u22a2toProc (input {d = d} l prg) = input d ([succeed: (\u03bb m \u2192 \u22a2toProc (prg m)) ,fail: error ] \u2218 parse)\n\u22a2toProc (start P prg x) = start (\u22a2toProc prg) (\u03bb d \u2192 \u22a2toProc (x d))\n\n\n\u22a2toProc-WT : \u2200 {\u0394} (der : \u22a2 \u0394) \u2192 \u0394 \u22a2 \u22a2toProc der\n\u22a2toProc-WT (end {x}) = end {_} {x}\n\u22a2toProc-WT (output {{x}} l m der) = output l (sym (rinv m)) (\u22a2toProc-WT der)\n\u22a2toProc-WT (input {{x}} l x\u2081) = input l \u03bb s m x \u2192\n subst (\u03bb X \u2192 _ [ l \u2254 m ] \u22a2 [succeed: (\u22a2toProc \u2218 x\u2081) ,fail: error ] X) x (\u22a2toProc-WT (x\u2081 m))\n\u22a2toProc-WT (start P der x) = start P (\u22a2toProc-WT der) (\u03bb d \u2192 \u22a2toProc-WT (x d))\n-}\n\n\u27e6_\u27e7E : Env \u2192 Set\n\u27e6 \u03b5 \u27e7E = \ud835\udfd9\n\u27e6 \u0394 , d \u21a6 P \u27e7E = \u27e6 \u0394 \u27e7E \u00d7 \u27e6 P \u27e7\n\nmapEnv : (Proto \u2192 Proto) \u2192 Env \u2192 Env\nmapEnv f \u03b5 = \u03b5\nmapEnv f (\u0394 , d \u21a6 P) = mapEnv f \u0394 , d \u21a6 f P\n\nmapEnv-all : \u2200 {P Q : URI \u2192 Proto \u2192 Set}{\u0394}{f : Proto \u2192 Proto}\n \u2192 (\u2200 d x \u2192 P d x \u2192 Q d (f x))\n \u2192 AllEnv P \u0394 \u2192 AllEnv Q (mapEnv f \u0394)\nmapEnv-all {\u0394 = \u03b5} f\u2081 \u2200\u0394 = \u2200\u0394\nmapEnv-all {\u0394 = \u0394 , d \u21a6 P\u2081} f\u2081 (\u2200\u0394 , P) = (mapEnv-all f\u2081 \u2200\u0394) , f\u2081 d P\u2081 P\n\nmapEnv-Ended : \u2200 {f : Proto \u2192 Proto}{\u0394} \u2192 Ended (f end)\n \u2192 AllEnv (\u03bb _ \u2192 Ended) \u0394 \u2192 AllEnv (\u03bb _ \u2192 Ended) (mapEnv f \u0394)\nmapEnv-Ended eq = mapEnv-all (\u03bb { d end _ \u2192 eq ; d (send P) () ; d (recv P) () })\n\nmapEnv-\u2208 : \u2200 {d P f \u0394} \u2192 d \u21a6 P \u2208 \u0394 \u2192 d \u21a6 f P \u2208 mapEnv f \u0394\nmapEnv-\u2208 here = here\nmapEnv-\u2208 (there der) = there (mapEnv-\u2208 der)\n\nmodule _ {d c M cf}{m : M}{{_ : M \u2243? SERIAL}}{p} where\n subst-lemma-one-point-four : \u2200 {\u0394}( l : d \u21a6 com c p \u2208 \u0394 ) \u2192\n let f = mapProto cf\n in (mapEnv f (\u0394 [ l \u2254 m ])) \u2261 (_[_\u2254_]{c = cf c} (mapEnv f \u0394) (mapEnv-\u2208 l) m)\n subst-lemma-one-point-four here = refl\n subst-lemma-one-point-four (there {d' = d'}{P'} l) = ap (\u03bb X \u2192 X , d' \u21a6 mapProto cf P') (subst-lemma-one-point-four l)\n\nmodule _ {d P} where\n project : \u2200 {\u0394} \u2192 d \u21a6 P \u2208 \u0394 \u2192 \u27e6 \u0394 \u27e7E \u2192 \u27e6 P \u27e7\n project here env = snd env\n project (there mem) env = project mem (fst env)\n\nempty : \u2200 {\u0394} \u2192 AllEnv (\u03bb _ \u2192 Ended) \u0394 \u2192 \u27e6 \u0394 \u27e7E\nempty {\u03b5} <> = _\nempty {\u0394 , d \u21a6 end} (fst , <>) = empty fst , _\nempty {\u0394 , d \u21a6 com x P} (fst , ())\n\nnoRecvInLog : \u2200 {d M}{{_ : M \u2243? SERIAL}}{P : M \u2192 _}{\u0394} \u2192 d \u21a6 recv P \u2208 mapEnv log \u0394 \u2192 \ud835\udfd8\nnoRecvInLog {\u0394 = \u03b5} ()\nnoRecvInLog {\u0394 = \u0394 , d\u2081 \u21a6 end} (there l) = noRecvInLog l\nnoRecvInLog {\u0394 = \u0394 , d\u2081 \u21a6 com x\u2081 P\u2081} (there l) = noRecvInLog l\n\nmodule _ {d M P}{{_ : M \u2243? SERIAL}} where\n lookup : \u2200 {\u0394}(l : d \u21a6 send P \u2208 \u0394) \u2192 \u27e6 \u0394 \u27e7E \u2192 \u03a3 M \u03bb m \u2192 \u27e6 \u0394 [ l \u2254 m ] \u27e7E\n lookup here (env , (m , p)) = m , (env , p)\n lookup (there l) (env , P') = let (m , env') = lookup l env in m , (env' , P')\n\n set : \u2200 {\u0394}(l : d \u21a6 recv P \u2208 \u0394) \u2192 \u27e6 \u0394 \u27e7E \u2192 (m : M) \u2192 \u27e6 \u0394 [ l \u2254 m ] \u27e7E\n set here (env , f) m = env , f m\n set (there l) (env , P') m = set l env m , P'\n\n set\u03a3 : \u2200 {\u0394}(l : d \u21a6 send P \u2208 \u0394) \u2192 (m : M) \u2192 \u27e6 \u0394 [ l \u2254 m ] \u27e7E \u2192 \u27e6 \u0394 \u27e7E\n set\u03a3 here m env = fst env , (m , snd env)\n set\u03a3 (there l) m env = set\u03a3 l m (fst env) , snd env\n\n {-\nforgetConc : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 mapEnv log \u0394 \u2192 \u27e6 mapEnv log \u0394 \u27e7E\nforgetConc (end e) = empty \u2026\nforgetConc {\u0394} (output l m der) = set\u03a3 l m (forgetConc {{!set\u03a3 l m!}} der) -- (forgetConc der)\nforgetConc (input l x\u2081) with noRecvInLog l\n... | ()\n-}\n\n\u22a2telecom : \u2200 {\u0394} \u2192 \u22a2\u1d9c\u1da0 \u0394 \u2192 \u27e6 mapEnv dual \u0394 \u27e7E \u2192 \u22a2 mapEnv log \u0394\n\u22a2telecom end env = end {e = mapEnv-Ended _ \u2026}\n\u22a2telecom (output l m der) env = output (mapEnv-\u2208 l) m (subst (\u22a2_)\n (subst-lemma-one-point-four l) (\u22a2telecom der (subst \u27e6_\u27e7E (sym (subst-lemma-one-point-four l)) (set (mapEnv-\u2208 l) env m))))\n\u22a2telecom (input l x\u2081) env = let (m , env') = lookup (mapEnv-\u2208 l) env\n hyp = \u22a2telecom (x\u2081 m) (subst (\u27e6_\u27e7E) (sym (subst-lemma-one-point-four l)) env')\n in output (mapEnv-\u2208 l) m\n (subst (\u22a2_) (subst-lemma-one-point-four l) hyp)\n\n-- old version\n{-\ncut\u1d9c\u1da0 : \u2200 {\u0394 d P} \u2192 \u27e6 dual P \u27e7 \u2192 \u22a2\u1d9c\u1da0 \u0394 , d \u21a6 P \u2192 \u22a2\u1d9c\u1da0 \u0394\ncut\u1d9c\u1da0 D (end {allEnded = \u0394E , PE }) = end {allEnded = \u0394E}\ncut\u1d9c\u1da0 D (output here m E) = cut\u1d9c\u1da0 (D m) E\ncut\u1d9c\u1da0 D (output (there l) m E) = output l m (cut\u1d9c\u1da0 D E)\ncut\u1d9c\u1da0 (m , D) (input here x\u2081) = cut\u1d9c\u1da0 D (x\u2081 m)\ncut\u1d9c\u1da0 D (input (there l) x\u2081) = input l (\u03bb m \u2192 cut\u1d9c\u1da0 D (x\u2081 m))\n\ncut : \u2200 {\u0394 \u0394' \u0393 \u0393' d P} \u2192 \u22a2 \u0394 , clientURI \u21a6 dual P +++ \u0394' \u2192 \u22a2 \u0393 , d \u21a6 P +++ \u0393' \u2192 \u22a2 (\u0394 +++ \u0394') +++ (\u0393 +++ \u0393')\ncut D E = {!!}\n-}\n\n\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"e35fc05915324dc4d1f2447d4d45b5c919b06b78","subject":"Removed files related to agda2atp.","message":"Removed files related to agda2atp.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/README\/Test.agda","new_file":"notes\/README\/Test.agda","new_contents":"","old_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Test where\n\ndata _\u2228_ (A B : Set) : Set where\n inj\u2081 : A \u2192 A \u2228 B\n inj\u2082 : B \u2192 A \u2228 B\n\npostulate\n A B : Set\n \u2228-comm : A \u2228 B \u2192 B \u2228 A\n{-# ATP prove \u2228-comm #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"00065eb75ec8eb897e66dfd3bfb4c202d976fd57","subject":"Use booleans from Data.Bool (fix #31).","message":"Use booleans from Data.Bool (fix #31).\n\nOld-commit-hash: c2de96202cc68d0b06b45d3c977bdf935e14eec1\n","repos":"inc-lc\/ilc-agda","old_file":"Denotational\/Values.agda","new_file":"Denotational\/Values.agda","new_contents":"module Denotational.Values where\n\n-- VALUES\n--\n-- This module defines the model theory of simple types, that is,\n-- it defines for every type, the set of values of that type.\n--\n-- In fact, we only describe a single model here.\n\nopen import Data.Bool public\n\nimport Relation.Binary as B\n\nopen import Relation.Binary using\n (IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)\nimport Relation.Binary.EqReasoning as EqR\n\nopen import Relation.Nullary using (\u00ac_)\n\nopen import Denotational.Notation\n\nopen import Syntactic.Types\n\n-- Denotational Semantics\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 bool \u27e7Type = Bool\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\n-- Value Equivalence\nopen import Level using (zero)\nopen import Relation.Binary.PropositionalEquality\npostulate ext : Extensionality zero zero\n\nnot-not : \u2200 a \u2192 a \u2261 not (not a)\nnot-not true = refl\nnot-not false = refl\n\na-xor-a-false : \u2200 a \u2192 (a xor a) \u2261 false\na-xor-a-false true = refl\na-xor-a-false false = refl\n\na-xor-false-a : \u2200 a \u2192 (false xor a) \u2261 a\na-xor-false-a b = refl\n\nxor-associative : \u2200 a b c \u2192 ((b xor c) xor a) \u2261 (b xor (c xor a))\nxor-associative a true true = not-not a\nxor-associative a true false = refl\nxor-associative a false c = refl\n\na-xor-false : \u2200 a \u2192 a xor false \u2261 a\na-xor-false true = refl\na-xor-false false = refl\n\na-xor-true : \u2200 a \u2192 a xor true \u2261 not a\na-xor-true true = refl\na-xor-true false = refl\n\nxor-commutative : \u2200 a b \u2192 a xor b \u2261 b xor a\nxor-commutative true b rewrite a-xor-true b = refl\nxor-commutative false b rewrite a-xor-false b = refl\n\nxor-cancellative-2 : \u2200 a b \u2192 (b xor (a xor a)) \u2261 b\nxor-cancellative-2 a b rewrite a-xor-a-false a = a-xor-false b\n\nxor-cancellative : \u2200 a b \u2192 ((b xor a) xor a) \u2261 b\nxor-cancellative a b rewrite xor-associative a b a = xor-cancellative-2 a b\n\n\u2261-refl : \u2200 {\u03c4} {v : \u27e6 \u03c4 \u27e7} \u2192\n v \u2261 v\n\u2261-refl = refl\n\n\u2261-sym : \u2200 {\u03c4} {v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2081\n\u2261-sym = sym\n\n\u2261-trans : \u2200 {\u03c4} {v\u2081 v\u2082 v\u2083 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2083 \u2192 v\u2081 \u2261 v\u2083\n\u2261-trans = trans\n\n\u2261-cong : \u2200 {\u03c4\u2082 \u03c4\u2081 v\u2081 v\u2082} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 f v\u2081 \u2261 f v\u2082\n\u2261-cong f = cong f\n\u2261-cong\u2082 : \u2200 {\u03c4\u2083 \u03c4\u2081 \u03c4\u2082 v\u2081 v\u2082 v\u2083 v\u2084} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7 \u2192 \u27e6 \u03c4\u2083 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 f v\u2081 v\u2083 \u2261 f v\u2082 v\u2084\n\u2261-cong\u2082 f = cong\u2082 f\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = \u2261-cong\u2082 (\u03bb x y \u2192 x y)\n\n\u2261-isEquivalence : \u2200 {\u03c4 : Set} \u2192 IsEquivalence (_\u2261_ {A = \u03c4})\n\u2261-isEquivalence = isEquivalence\n\n\u2261-setoid : Type \u2192 Setoid _ _\n\u2261-setoid \u03c4 = record\n { Carrier = \u27e6 \u03c4 \u27e7\n ; _\u2248_ = _\u2261_\n ; isEquivalence = \u2261-isEquivalence\n }\n\n\u2261-consistent : \u00ac (\u2200 (\u03c4 : Type) \u2192 (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192 v\u2081 \u2261 v\u2082)\n\u2261-consistent H with H bool true false\n... | ()\n","old_contents":"module Denotational.Values where\n\n-- VALUES\n--\n-- This module defines the model theory of simple types, that is,\n-- it defines for every type, the set of values of that type.\n--\n-- In fact, we only describe a single model here.\n\nimport Relation.Binary as B\n\nopen import Relation.Binary using\n (IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)\nimport Relation.Binary.EqReasoning as EqR\n\nopen import Relation.Nullary using (\u00ac_)\n\nopen import Denotational.Notation\n\nopen import Syntactic.Types\n\n-- Denotational Semantics\n\ndata Bool : Set where\n true false : Bool\n\nnot : Bool \u2192 Bool\nnot true = false\nnot false = true\n\n_xor_ : Bool \u2192 Bool \u2192 Bool\ntrue xor b = not b\nfalse xor b = b\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\u27e6 bool \u27e7Type = Bool\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\n-- Value Equivalence\nopen import Level using (zero)\nopen import Relation.Binary.PropositionalEquality\npostulate ext : Extensionality zero zero\n\nnot-not : \u2200 a \u2192 a \u2261 not (not a)\nnot-not true = refl\nnot-not false = refl\n\na-xor-a-false : \u2200 a \u2192 (a xor a) \u2261 false\na-xor-a-false true = refl\na-xor-a-false false = refl\n\na-xor-false-a : \u2200 a \u2192 (false xor a) \u2261 a\na-xor-false-a b = refl\n\nxor-associative : \u2200 a b c \u2192 ((b xor c) xor a) \u2261 (b xor (c xor a))\nxor-associative a true true = not-not a\nxor-associative a true false = refl\nxor-associative a false c = refl\n\na-xor-false : \u2200 a \u2192 a xor false \u2261 a\na-xor-false true = refl\na-xor-false false = refl\n\na-xor-true : \u2200 a \u2192 a xor true \u2261 not a\na-xor-true true = refl\na-xor-true false = refl\n\nxor-commutative : \u2200 a b \u2192 a xor b \u2261 b xor a\nxor-commutative true b rewrite a-xor-true b = refl\nxor-commutative false b rewrite a-xor-false b = refl\n\nxor-cancellative-2 : \u2200 a b \u2192 (b xor (a xor a)) \u2261 b\nxor-cancellative-2 a b rewrite a-xor-a-false a = a-xor-false b\n\nxor-cancellative : \u2200 a b \u2192 ((b xor a) xor a) \u2261 b\nxor-cancellative a b rewrite xor-associative a b a = xor-cancellative-2 a b\n\n\u2261-refl : \u2200 {\u03c4} {v : \u27e6 \u03c4 \u27e7} \u2192\n v \u2261 v\n\u2261-refl = refl\n\n\u2261-sym : \u2200 {\u03c4} {v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2081\n\u2261-sym = sym\n\n\u2261-trans : \u2200 {\u03c4} {v\u2081 v\u2082 v\u2083 : \u27e6 \u03c4 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2082 \u2261 v\u2083 \u2192 v\u2081 \u2261 v\u2083\n\u2261-trans = trans\n\n\u2261-cong : \u2200 {\u03c4\u2082 \u03c4\u2081 v\u2081 v\u2082} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 f v\u2081 \u2261 f v\u2082\n\u2261-cong f = cong f\n\u2261-cong\u2082 : \u2200 {\u03c4\u2083 \u03c4\u2081 \u03c4\u2082 v\u2081 v\u2082 v\u2083 v\u2084} (f : \u27e6 \u03c4\u2081 \u27e7 \u2192 \u27e6 \u03c4\u2082 \u27e7 \u2192 \u27e6 \u03c4\u2083 \u27e7) \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 f v\u2081 v\u2083 \u2261 f v\u2082 v\u2084\n\u2261-cong\u2082 f = cong\u2082 f\n\n\u2261-app : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n\u2261-app = \u2261-cong\u2082 (\u03bb x y \u2192 x y)\n\n\u2261-isEquivalence : \u2200 {\u03c4 : Set} \u2192 IsEquivalence (_\u2261_ {A = \u03c4})\n\u2261-isEquivalence = isEquivalence\n\n\u2261-setoid : Type \u2192 Setoid _ _\n\u2261-setoid \u03c4 = record\n { Carrier = \u27e6 \u03c4 \u27e7\n ; _\u2248_ = _\u2261_\n ; isEquivalence = \u2261-isEquivalence\n }\n\n\u2261-consistent : \u00ac (\u2200 (\u03c4 : Type) \u2192 (v\u2081 v\u2082 : \u27e6 \u03c4 \u27e7) \u2192 v\u2081 \u2261 v\u2082)\n\u2261-consistent H with H bool true false\n... | ()\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ccabb7380964bd2d763e0b6231ec252a0f2de3b6","subject":"More (failed) progress on the proof","message":"More (failed) progress on the proof\n\nPart of this goes in a wrong direction, since `fundamental-aux2` appears\nto be very false (even though it has just one hole): we need a proof of\n`rel\u03c1 \u0393 \u03c11 \u03c12 (2 + n)` when we only have `rel\u03c1 \u0393 \u03c11 \u03c12 (1 + n)` in\ncontext.\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/FunBigStepSILR.agda","new_file":"Thesis\/FunBigStepSILR.agda","new_contents":"{-# OPTIONS --allow-unsolved-metas #-}\n-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by Dargaye and Leroy (2010), \"A\n-- verified framework for higher-order uncurrying optimizations\" (and a bit by\n-- \"Functional Big-Step Semantics\"), though I deviate somewhere.\nmodule Thesis.FunBigStepSILR where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value, following \"Type Soundness Proofs with\n-- Definitional Interpreters\". Since we focus for now on STLC, unlike that\n-- paper, we can avoid error values by keeping types.\n--\n-- One could drop types and add error values, to reproduce what they do.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = \u2115 \u2192 ErrVal \u03c4\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) n = Done (intV v)\n\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval t \u03c1 zero = TimeOut\neval (const c) \u03c1 (suc n) = evalConst c n\neval (var x) \u03c1 (suc n) = Done (\u27e6 x \u27e7Var \u03c1)\neval (abs t) \u03c1 (suc n) = Done (closure t \u03c1)\neval (app s t) \u03c1 (suc n) with eval s \u03c1 n | eval t \u03c1 n\n... | Done f | Done a = apply f a n\n... | _ | _ = TimeOut\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n \u2192 evalConst c n \u2261 Done v \u2192 evalConst c (suc n) \u2261 Done v\neval-const-mono (lit v) n eq = eq\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 \u2200 v n \u2192 eval t \u03c1 n \u2261 Done v \u2192 eval t \u03c1 (suc n) \u2261 Done v\neval-mono t \u03c1 v zero ()\neval-mono (const c) \u03c1 v (suc n) eq = eval-const-mono c n eq\neval-mono (var x) \u03c1 v (suc n) eq = eq\neval-mono (app s t) \u03c1 v (suc n) eq with eval s \u03c1 n | inspect (eval s \u03c1) n | eval t \u03c1 n | inspect (eval t \u03c1) n\neval-mono (app s t) \u03c1 v (suc n) eq | Done sv | [ seq ] | (Done tv) | [ teq ] with eval s \u03c1 (suc n) | eval-mono s \u03c1 sv n seq | eval t \u03c1 (suc n) | eval-mono t \u03c1 tv n teq\neval-mono (app s t) \u03c1 v (suc n) eq | Done (closure ct c\u03c1) | [ seq ] | (Done tv) | [ teq ] | .(Done (closure ct c\u03c1)) | refl | .(Done tv) | refl = eval-mono ct (tv \u2022 c\u03c1) _ n eq\neval-mono (app s t) \u03c1 v (suc n) () | Done _ | [ seq ] | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n) () | TimeOut | [ seq ] | tv | [ teq ]\n-- = {!eval-mono s \u03c1 (suc n)!}\neval-mono (abs t) \u03c1 v (suc n) eq = eq\n\n-- Can we prove eval sound wrt. our reference denotational semantics? Yes! Very\n-- cool! (Commented out until I paste that semantics here.)\n-- eval-sound : \u2200 {\u0393 \u03c4} \u03c1 v n (t : Term \u0393 \u03c4) \u2192\n-- eval t \u03c1 n \u2261 Done v \u2192\n-- \u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 v \u27e7Val\n\n-- apply-sound : \u2200 {\u0393 \u03c3 \u03c4} \u03c1 v f a n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t \u2192\n-- \u27e6 s \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 f \u27e7Val \u2192\n-- \u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 a \u27e7Val \u2192\n-- apply f a n \u2261 Done v \u2192\n-- \u27e6 s \u27e7Term \u27e6 \u03c1 \u27e7Env (\u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env) \u2261 \u27e6 v \u27e7Val\n-- apply-sound _ v (closure ft \u03c1) a n s t feq aeq eq rewrite feq | aeq = eval-sound (a \u2022 \u03c1) v n ft eq\n\n-- eval-sound \u03c1 v zero t ()\n-- eval-sound \u03c1 v (\u2115.suc n) (const c) eq = {!!}\n-- eval-sound \u03c1 v (\u2115.suc n) (var x) refl = \u21a6-sound \u03c1 x\n-- eval-sound \u03c1 v (\u2115.suc n) (abs t) refl = refl\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) eq with eval s \u03c1 n | inspect (eval s \u03c1) n | eval t \u03c1 n | inspect (eval t \u03c1) n\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) eq | Done f | [ feq ] | Done a | [ aeq ] =\n-- let feq = eval-sound \u03c1 f n s feq; aeq = eval-sound \u03c1 a n t aeq in apply-sound \u03c1 v f a n s t feq aeq eq\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) () | Done f | _ | TimeOut | _\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) () | TimeOut | _ | _ | _\n-- -- eval-sound n (const c) eq = {!!}\n-- -- eval-sound n (var x) eq = {!!}\n-- -- eval-sound n (app s t) eq = {!!} -- with eval s \u03c1 n\n-- -- eval-sound n (abs t) eq = {!!}\n-- -- eval-sound n (const c) eq = {!!}\n-- -- eval-sound n (var x) eq = {!!}\n-- -- eval-sound n (app s t) eq = {!!} -- with eval s \u03c1 n\n-- -- eval-sound n (abs t) eq = {!!}\n\nrelV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\n-- XXX don't we want *RELATED* input environments? XXX seems not???\n\n-- Indexing not according to source. But I can't quite write the correct\n-- indexing without changing the definitions a lot.\nrelT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\nrelT {\u03c4} t1 t2 \u03c11 \u03c12 n =\n (v1 : Val \u03c4) \u2192\n eval t1 \u03c11 n \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] (eval t2 \u03c12 n \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 n)\n\nimport Data.Fin as F\nopen F using (Fin; _\u2115-_)\n\n-- This is closer to what's used in Dargaye and Leroy, but not the same.\n\nrelT2 : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\nrelT2 {\u03c4} t1 t2 \u03c11 \u03c12 n =\n (v1 : Val \u03c4) \u2192\n \u2200 (j : Fin (suc n)) \u2192\n eval t1 \u03c11 (F.to\u2115 j) \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] eval t2 \u03c12 (F.to\u2115 j) \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 (F.to\u2115 (n F.\u2115- j))\n\nrelV \u03c4 v1 v2 zero = \u22a4\n-- Seems the proof for abs would go through even if here we do not step down.\n-- However, that only works as long as we use a typed language; not stepping\n-- down here, in an untyped language, gives a non-well-founded definition.\nrelV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) =\n \u2200 (k : \u2115) (k\u2264n : k \u2264 n) \u2192\n \u2200 v1 v2 \u2192 relV \u03c3 v1 v2 k \u2192 relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\nrelV nat v1 v2 (suc n) = v1 \u2261 v2\n\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl)\n\nopen import Data.Nat.Properties\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono zero n m\u2264n \u03c4 v1 v2 vv = tt\nrelV-mono (suc m) zero () \u03c4 v1 v2 vv\nrelV-mono (suc m) (suc n) m\u2264n nat v1 v2 vv = vv\nrelV-mono (suc m) (suc n) (_\u2264_.s\u2264s m\u2264n) (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) vv k k\u2264m = vv k (DecTotalOrder.trans decTotalOrder k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\n-- relV-mono \u03c4 v1 v2 vv = tt\n-- relV-mono nat v1 v2 (suc n) vv = vv\n-- relV-mono (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) vv k k\u2264n = vv k ?\n\n-- relV-mono : \u2200 \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 (suc n) \u2192 relV \u03c4 v1 v2 n\n-- relV-mono \u03c4 v1 v2 zero vv = tt\n-- relV-mono nat v1 v2 (suc n) vv = vv\n-- relV-mono (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) vv k k\u2264n = vv k (\u2264-step k\u2264n)\n\nrelV-apply-go : \u2200 {\u03c3 \u03c4} sv1 sv2 tv1 tv2\n n\n (svv : relV (\u03c3 \u21d2 \u03c4) sv1 sv2 (suc (suc n)))\n (tvv : relV \u03c3 tv1 tv2 (suc (suc n)))\n v1 \u2192\n apply sv1 tv1 n \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] (apply sv2 tv2 n \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 (suc n))\nrelV-apply-go (closure st1 \u03c11) (closure st2 \u03c12) tv1 tv2 zero svv tvv v1 ()\nrelV-apply-go (closure st1 \u03c11) (closure st2 \u03c12) tv1 tv2 (suc n) svv tvv v1 eqv1\n with svv (suc n) (\u2264-step \u2264-refl) tv1 tv2 (relV-mono _ _ (\u2264-step (\u2264-step \u2264-refl)) _ _ _ tvv) v1 eqv1\n | svv (suc (suc n)) \u2264-refl tv1 tv2 (relV-mono _ _ (\u2264-step \u2264-refl) _ _ _ tvv) v1 (eval-mono st1 (tv1 \u2022 \u03c11) v1 (suc n) eqv1)\nrelV-apply-go (closure st1 \u03c11) (closure st2 \u03c12) tv1 tv2 (suc n) svv tvv v1 eqv1 | v2' , eqv2' , final-v' | v2 , eqv2 , final-v with trans (sym (eval-mono st2 (tv2 \u2022 \u03c12) v2' (suc n) eqv2')) eqv2\nrelV-apply-go (closure st1 \u03c11) (closure st2 \u03c12) tv1 tv2 (suc n) svv tvv v1 eqv1 | .v2 , eqv2' , final-v' | v2 , eqv2 , final-v | refl = v2 , eqv2' , final-v\n\n-- eqv2'\n-- (suc n) ? tv1 tv2 (relV-mono _ _ (n\u22641+n (suc n)) _ _ _ tvv) v1 eqv1\n-- ... | v2 , eqv2 , final-v = v2 , eqv2 , final-v\n\nrelV-apply : \u2200 {\u03c3 \u03c4 sv1 sv2 tv1 tv2}\n n\n (svv : relV (\u03c3 \u21d2 \u03c4) sv1 sv2 (suc (suc n)))\n (tvv : relV \u03c3 tv1 tv2 (suc (suc n)))\n {v1} \u2192\n apply sv1 tv1 n \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] (apply sv2 tv2 n \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 (suc n))\nrelV-apply n svv tvv eqv1 = relV-apply-go _ _ _ _ n svv tvv _ eqv1\n--\n\n-- fundamental lemma of logical relations.\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero _ _ _ _ ()\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 refl = v2 , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\n\nfundamental-aux : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 (suc n)) \u2192 (v1 : Val \u03c4) \u2192\n eval t \u03c11 n \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] (eval t \u03c12 n \u2261 Done v2 \u00d7 eval t \u03c12 (suc n) \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 (suc n))\nfundamental-aux s n \u03c11 \u03c12 \u03c1\u03c1 sv1 seq1 with eval s \u03c12 n | inspect (eval s \u03c12) n | fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 seq1 | fundamental s (suc n) \u03c11 \u03c12 \u03c1\u03c1 sv1 (eval-mono s \u03c11 sv1 n seq1)\n... | Done sv2 | [ seq2 ] | (.sv2 , refl , svv) | (sv2' , sveq , svv') with trans (sym (eval-mono s \u03c12 sv2 n seq2)) sveq\nfundamental-aux s n \u03c11 \u03c12 \u03c1\u03c1 sv1 seq1 | Done sv2 | [ seq2 ] | (.sv2 , refl , svv) | (.sv2 , sveq , svv') | refl = sv2 , refl , sveq , svv'\n\nfundamental-aux2 : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 (suc n)) \u2192 (v1 : Val \u03c4) \u2192\n eval t \u03c11 n \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] (eval t \u03c12 n \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 (suc (suc n)))\nfundamental-aux2 s n \u03c11 \u03c12 \u03c1\u03c1 sv1 seq1 with fundamental-aux s n \u03c11 \u03c12 \u03c1\u03c1 sv1 seq1\n... | sv2 , eq1 , eq2 , svv with fundamental-aux s (suc n) \u03c11 \u03c12 {!!} sv1 (eval-mono s \u03c11 sv1 n seq1)\n... | sv2' , eq1' , eq2' , svv' = sv2' , trans eq1 (trans (sym eq2) eq1') , svv'\n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 v ()\n-- XXX trivial case for constants.\nfundamental (const (lit nv)) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(intV nv) refl = intV nv , refl , refl\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 refl = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1 (\u27e6 x \u27e7Var \u03c11) refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 (closure .t .\u03c11) refl =\n closure t \u03c12 , refl , \u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k _ (\u2264-step k\u2264n) _ _ _ \u03c1\u03c1)\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 with eval s \u03c11 n | inspect (eval s \u03c11) n | eval t \u03c11 n | inspect (eval t \u03c11) n\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | Done tv1 | [ teq1 ] with eval s \u03c12 n | fundamental-aux2 s n \u03c11 \u03c12 \u03c1\u03c1 sv1 seq1 | eval t \u03c12 n | fundamental-aux2 t n \u03c11 \u03c12 \u03c1\u03c1 tv1 teq1\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | (Done tv1) | [ teq1 ] | (Done sv2) | (.sv2 , refl , svv') | (Done tv2) | (.tv2 , refl , tvv') = relV-apply n svv' tvv' t\u03c11\u2193v1\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | Done tv1 | [ teq1 ] with eval s \u03c12 n | inspect (eval s \u03c12) n | fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 seq1 | fundamental s (suc n) \u03c11 \u03c12 \u03c1\u03c1 sv1 (eval-mono s \u03c11 sv1 n seq1)\n-- ... | Done sv2 | [ seq2 ] | (.sv2 , refl , svv) | (sv2' , sveq , svv') with trans (sym (eval-mono s \u03c12 sv2 n seq2)) sveq\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | (Done tv1) | [ teq1 ] | (Done sv2) | [ seq2 ] | (.sv2 , refl , svv) | (.sv2 , sveq , svv') | refl = {!!}\n-- | fundamental t n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) tv1 teq1\n\n-- fundamental {\u03c4 = \u03c4} (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | Done tv1 | [ teq1 ] | Done sv2 | [ seq2 ] | Done tv2 | (.sv2 , refl , svv) | (.tv2 , refl , tvv) = relV-apply n svv tvv t\u03c11\u2193v1\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | Done sv1 | [ seq1 ] | TimeOut | [ teq1 ]\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | TimeOut | [ seq1 ] | tv1 | [ teq1 ]\n\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | Done tv1 | [ teq1 ] with eval s \u03c12 n | inspect (eval s \u03c12) n | eval t \u03c12 n | fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 seq1\n--... | sv2 | [ seq2 ] | tv2 | (sv2' , s\u03c12\u2193sv2 , svv) = ?\n\n\n-- TODO: match sv2 before matching on fundamental s.\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | Done tv1 with eval s \u03c12 n | inspect (eval s \u03c12) n | eval t \u03c12 n | fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 eq1\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | (Done tv2) | [ teq1 ] | (sv2' , s\u03c12\u2193sv2 , svv) = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | TimeOut | [ teq1 ] | (sv2' , s\u03c12\u2193sv2 , svv) with fundamental t n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) tv1 {!teq1!}\n-- ... | (tv2' , t\u03c12\u2193tv2 , tvv) = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | TimeOut | [ eq2 ] | b | [ teq1 ] | (sv2' , () , svv)\n\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | (Done tv2) with fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 eq1\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | (Done tv2) | (sv2' , s\u03c12\u2193sv2 , svv) = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | TimeOut = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | TimeOut | [ eq2 ] | tv2 = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | Done tv1 with fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 eq1\n-- fundamental {\u03c4 = \u03c4} (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | Done tv1 | (sv2 , s\u03c12\u2193sv2 , svv) = v2 , {!!}\n-- where\n-- v2 : Val \u03c4\n-- v2 = {!!}\n-- -- t\u03c12\u2193v2 : apply sv2 tv2 n \u2261 Done v1\n-- -- t\u03c12\u2193v2 = ?\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | Done sv | _ | TimeOut\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | TimeOut | _ | Done tv1\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | TimeOut | _ | TimeOut\n","old_contents":"{-# OPTIONS --allow-unsolved-metas #-}\n-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by Dargaye and Leroy (2010), \"A\n-- verified framework for higher-order uncurrying optimizations\" (and a bit by\n-- \"Functional Big-Step Semantics\"), though I deviate somewhere.\nmodule Thesis.FunBigStepSILR where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value, following \"Type Soundness Proofs with\n-- Definitional Interpreters\". Since we focus for now on STLC, unlike that\n-- paper, we can avoid error values by keeping types.\n--\n-- One could drop types and add error values, to reproduce what they do.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = \u2115 \u2192 ErrVal \u03c4\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) n = Done (intV v)\n\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval t \u03c1 zero = TimeOut\neval (const c) \u03c1 (suc n) = evalConst c n\neval (var x) \u03c1 (suc n) = Done (\u27e6 x \u27e7Var \u03c1)\neval (abs t) \u03c1 (suc n) = Done (closure t \u03c1)\neval (app s t) \u03c1 (suc n) with eval s \u03c1 n | eval t \u03c1 n\n... | Done f | Done a = apply f a n\n... | _ | _ = TimeOut\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n \u2192 evalConst c n \u2261 Done v \u2192 evalConst c (suc n) \u2261 Done v\neval-const-mono (lit v) n eq = eq\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 \u2200 v n \u2192 eval t \u03c1 n \u2261 Done v \u2192 eval t \u03c1 (suc n) \u2261 Done v\neval-mono t \u03c1 v zero ()\neval-mono (const c) \u03c1 v (suc n) eq = eval-const-mono c n eq\neval-mono (var x) \u03c1 v (suc n) eq = eq\neval-mono (app s t) \u03c1 v (suc n) eq with eval s \u03c1 n | inspect (eval s \u03c1) n | eval t \u03c1 n | inspect (eval t \u03c1) n\neval-mono (app s t) \u03c1 v (suc n) eq | Done sv | [ seq ] | (Done tv) | [ teq ] with eval s \u03c1 (suc n) | eval-mono s \u03c1 sv n seq | eval t \u03c1 (suc n) | eval-mono t \u03c1 tv n teq\neval-mono (app s t) \u03c1 v (suc n) eq | Done (closure ct c\u03c1) | [ seq ] | (Done tv) | [ teq ] | .(Done (closure ct c\u03c1)) | refl | .(Done tv) | refl = eval-mono ct (tv \u2022 c\u03c1) _ n eq\neval-mono (app s t) \u03c1 v (suc n) () | Done _ | [ seq ] | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n) () | TimeOut | [ seq ] | tv | [ teq ]\n-- = {!eval-mono s \u03c1 (suc n)!}\neval-mono (abs t) \u03c1 v (suc n) eq = eq\n\n-- Can we prove eval sound wrt. our reference denotational semantics? Yes! Very\n-- cool! (Commented out until I paste that semantics here.)\n-- eval-sound : \u2200 {\u0393 \u03c4} \u03c1 v n (t : Term \u0393 \u03c4) \u2192\n-- eval t \u03c1 n \u2261 Done v \u2192\n-- \u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 v \u27e7Val\n\n-- apply-sound : \u2200 {\u0393 \u03c3 \u03c4} \u03c1 v f a n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t \u2192\n-- \u27e6 s \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 f \u27e7Val \u2192\n-- \u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env \u2261 \u27e6 a \u27e7Val \u2192\n-- apply f a n \u2261 Done v \u2192\n-- \u27e6 s \u27e7Term \u27e6 \u03c1 \u27e7Env (\u27e6 t \u27e7Term \u27e6 \u03c1 \u27e7Env) \u2261 \u27e6 v \u27e7Val\n-- apply-sound _ v (closure ft \u03c1) a n s t feq aeq eq rewrite feq | aeq = eval-sound (a \u2022 \u03c1) v n ft eq\n\n-- eval-sound \u03c1 v zero t ()\n-- eval-sound \u03c1 v (\u2115.suc n) (const c) eq = {!!}\n-- eval-sound \u03c1 v (\u2115.suc n) (var x) refl = \u21a6-sound \u03c1 x\n-- eval-sound \u03c1 v (\u2115.suc n) (abs t) refl = refl\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) eq with eval s \u03c1 n | inspect (eval s \u03c1) n | eval t \u03c1 n | inspect (eval t \u03c1) n\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) eq | Done f | [ feq ] | Done a | [ aeq ] =\n-- let feq = eval-sound \u03c1 f n s feq; aeq = eval-sound \u03c1 a n t aeq in apply-sound \u03c1 v f a n s t feq aeq eq\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) () | Done f | _ | TimeOut | _\n-- eval-sound \u03c1 v (\u2115.suc n) (app s t) () | TimeOut | _ | _ | _\n-- -- eval-sound n (const c) eq = {!!}\n-- -- eval-sound n (var x) eq = {!!}\n-- -- eval-sound n (app s t) eq = {!!} -- with eval s \u03c1 n\n-- -- eval-sound n (abs t) eq = {!!}\n-- -- eval-sound n (const c) eq = {!!}\n-- -- eval-sound n (var x) eq = {!!}\n-- -- eval-sound n (app s t) eq = {!!} -- with eval s \u03c1 n\n-- -- eval-sound n (abs t) eq = {!!}\n\nrelV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\n-- Indexing not according to source. But I can't quite write the correct\n-- indexing without changing the definitions a lot.\nrelT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\nrelT {\u03c4} t1 t2 \u03c11 \u03c12 n =\n (v1 : Val \u03c4) \u2192\n eval t1 \u03c11 n \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] (eval t2 \u03c12 n \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 n)\n\nimport Data.Fin as F\nopen F using (Fin; _\u2115-_)\n\n-- This is closer to what's used in Dargaye and Leroy, but not the same.\n\nrelT2 : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\nrelT2 {\u03c4} t1 t2 \u03c11 \u03c12 n =\n (v1 : Val \u03c4) \u2192\n \u2200 (j : Fin (suc n)) \u2192\n eval t1 \u03c11 (F.to\u2115 j) \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] eval t2 \u03c12 (F.to\u2115 j) \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 (F.to\u2115 (n F.\u2115- j))\n\nrelV \u03c4 v1 v2 zero = \u22a4\n-- Seems the proof for abs would go through even if here we do not step down.\nrelV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) =\n \u2200 (k : \u2115) (k\u2264n : k \u2264 n) \u2192\n \u2200 v1 v2 \u2192 relV \u03c3 v1 v2 k \u2192 relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\nrelV nat v1 v2 (suc n) = v1 \u2261 v2\n\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl)\n\nrelV-apply-go : \u2200 {\u03c3 \u03c4} sv1 sv2 tv1 tv2\n n\n (svv : relV (\u03c3 \u21d2 \u03c4) sv1 sv2 (suc n))\n (tvv : relV \u03c3 tv1 tv2 (suc n))\n v1 \u2192\n apply sv1 tv1 n \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] (apply sv2 tv2 n \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 n)\nrelV-apply-go (closure st1 \u03c11) (closure st2 \u03c12) tv1 tv2 zero svv tvv v1 ()\nrelV-apply-go (closure st1 \u03c11) (closure st2 \u03c12) tv1 tv2 (suc n) svv tvv v1 eqv1 with svv (suc n) \u2264-refl tv1 tv2 {! tvv !} v1 eqv1\n... | v2 , eqv2 , final-v = v2 , eqv2 , final-v\n\nrelV-apply : \u2200 {\u03c3 \u03c4 sv1 sv2 tv1 tv2}\n n\n (svv : relV (\u03c3 \u21d2 \u03c4) sv1 sv2 (suc n))\n (tvv : relV \u03c3 tv1 tv2 (suc n))\n {v1} \u2192\n apply sv1 tv1 n \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] (apply sv2 tv2 n \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 (suc n))\nrelV-apply n svv tvv eqv1 = {!!}\n-- relV-apply-go _ _ _ _ n svv tvv _ eqv1\n\nopen import Data.Nat.Properties\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono zero n m\u2264n \u03c4 v1 v2 vv = tt\nrelV-mono (suc m) zero () \u03c4 v1 v2 vv\nrelV-mono (suc m) (suc n) m\u2264n nat v1 v2 vv = vv\nrelV-mono (suc m) (suc n) (_\u2264_.s\u2264s m\u2264n) (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) vv k k\u2264m = vv k (DecTotalOrder.trans decTotalOrder k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\n-- relV-mono \u03c4 v1 v2 vv = tt\n-- relV-mono nat v1 v2 (suc n) vv = vv\n-- relV-mono (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) vv k k\u2264n = vv k ?\n\n-- relV-mono : \u2200 \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 (suc n) \u2192 relV \u03c4 v1 v2 n\n-- relV-mono \u03c4 v1 v2 zero vv = tt\n-- relV-mono nat v1 v2 (suc n) vv = vv\n-- relV-mono (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) vv k k\u2264n = vv k (\u2264-step k\u2264n)\n\n-- fundamental lemma of logical relations.\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero _ _ _ _ ()\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 refl = v2 , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\n\nfundamental-aux : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 (suc n)) \u2192 (v1 : Val \u03c4) \u2192\n eval t \u03c11 n \u2261 Done v1 \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] (eval t \u03c12 n \u2261 Done v2 \u00d7 eval t \u03c12 (suc n) \u2261 Done v2 \u00d7 relV \u03c4 v1 v2 (suc n))\nfundamental-aux s n \u03c11 \u03c12 \u03c1\u03c1 sv1 seq1 with eval s \u03c12 n | inspect (eval s \u03c12) n | fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 seq1 | fundamental s (suc n) \u03c11 \u03c12 \u03c1\u03c1 sv1 (eval-mono s \u03c11 sv1 n seq1)\n... | Done sv2 | [ seq2 ] | (.sv2 , refl , svv) | (sv2' , sveq , svv') with trans (sym (eval-mono s \u03c12 sv2 n seq2)) sveq\nfundamental-aux s n \u03c11 \u03c12 \u03c1\u03c1 sv1 seq1 | Done sv2 | [ seq2 ] | (.sv2 , refl , svv) | (.sv2 , sveq , svv') | refl = sv2 , refl , sveq , svv'\n--\n-- |\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 v ()\n-- XXX trivial case for constants.\nfundamental (const (lit nv)) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(intV nv) refl = intV nv , refl , refl\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 refl = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1 (\u27e6 x \u27e7Var \u03c11) refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 (closure .t .\u03c11) refl =\n closure t \u03c12 , refl , \u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k _ (\u2264-step k\u2264n) _ _ _ \u03c1\u03c1)\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 with eval s \u03c11 n | inspect (eval s \u03c11) n | eval t \u03c11 n | inspect (eval t \u03c11) n\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | Done tv1 | [ teq1 ] with eval s \u03c12 n | fundamental-aux s n \u03c11 \u03c12 \u03c1\u03c1 sv1 seq1 | eval t \u03c12 n | fundamental-aux t n \u03c11 \u03c12 \u03c1\u03c1 tv1 teq1\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | (Done tv1) | [ teq1 ] | (Done sv2) | (.sv2 , refl , seq2' , svv') | (Done tv2) | (.tv2 , refl , teq2' , tvv') = relV-apply n svv' tvv' t\u03c11\u2193v1\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | Done tv1 | [ teq1 ] with eval s \u03c12 n | inspect (eval s \u03c12) n | fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 seq1 | fundamental s (suc n) \u03c11 \u03c12 \u03c1\u03c1 sv1 (eval-mono s \u03c11 sv1 n seq1)\n-- ... | Done sv2 | [ seq2 ] | (.sv2 , refl , svv) | (sv2' , sveq , svv') with trans (sym (eval-mono s \u03c12 sv2 n seq2)) sveq\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | (Done tv1) | [ teq1 ] | (Done sv2) | [ seq2 ] | (.sv2 , refl , svv) | (.sv2 , sveq , svv') | refl = {!!}\n-- | fundamental t n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) tv1 teq1\n\n-- fundamental {\u03c4 = \u03c4} (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | Done tv1 | [ teq1 ] | Done sv2 | [ seq2 ] | Done tv2 | (.sv2 , refl , svv) | (.tv2 , refl , tvv) = relV-apply n svv tvv t\u03c11\u2193v1\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | Done sv1 | [ seq1 ] | TimeOut | [ teq1 ]\nfundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | TimeOut | [ seq1 ] | tv1 | [ teq1 ]\n\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ seq1 ] | Done tv1 | [ teq1 ] with eval s \u03c12 n | inspect (eval s \u03c12) n | eval t \u03c12 n | fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 seq1\n--... | sv2 | [ seq2 ] | tv2 | (sv2' , s\u03c12\u2193sv2 , svv) = ?\n\n\n-- TODO: match sv2 before matching on fundamental s.\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | Done tv1 with eval s \u03c12 n | inspect (eval s \u03c12) n | eval t \u03c12 n | fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 eq1\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | (Done tv2) | [ teq1 ] | (sv2' , s\u03c12\u2193sv2 , svv) = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | TimeOut | [ teq1 ] | (sv2' , s\u03c12\u2193sv2 , svv) with fundamental t n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) tv1 {!teq1!}\n-- ... | (tv2' , t\u03c12\u2193tv2 , tvv) = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | TimeOut | [ eq2 ] | b | [ teq1 ] | (sv2' , () , svv)\n\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | (Done tv2) with fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 eq1\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | (Done tv2) | (sv2' , s\u03c12\u2193sv2 , svv) = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | (Done sv2) | [ eq2 ] | TimeOut = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | (Done tv1) | TimeOut | [ eq2 ] | tv2 = {!!}\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | Done tv1 with fundamental s n \u03c11 \u03c12 (rel\u03c1-mono n _ (n\u22641+n n) _ _ _ \u03c1\u03c1) sv1 eq1\n-- fundamental {\u03c4 = \u03c4} (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 t\u03c11\u2193v1 | Done sv1 | [ eq1 ] | Done tv1 | (sv2 , s\u03c12\u2193sv2 , svv) = v2 , {!!}\n-- where\n-- v2 : Val \u03c4\n-- v2 = {!!}\n-- -- t\u03c12\u2193v2 : apply sv2 tv2 n \u2261 Done v1\n-- -- t\u03c12\u2193v2 = ?\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | Done sv | _ | TimeOut\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | TimeOut | _ | Done tv1\n-- fundamental (app s t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 v1 () | TimeOut | _ | TimeOut\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"64a0d4678c9a3cfe69e473c9b5f335cd071c1e13","subject":"Move import to more sensible place","message":"Move import to more sensible place\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Validity.agda","new_file":"Parametric\/Change\/Validity.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e)\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Denotation.Value as Value\nopen import Base.Change.Algebra as CA\n using (ChangeAlgebraFamily)\n\nmodule Parametric.Change.Validity\n {Base : Type.Structure}\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Extension Point: Change algebras for base types\nStructure : Set\u2081\nStructure = ChangeAlgebraFamily zero \u27e6_\u27e7Base\n\nmodule Structure (change-algebra-base : Structure) where\n -- change algebras\n\n open CA public renaming\n ( [] to \u2205\n ; _\u2237_ to _\u2022_\n )\n\n -- We provide: change algebra for every type\n change-algebra : \u2200 \u03c4 \u2192 ChangeAlgebra zero \u27e6 \u03c4 \u27e7Type\n change-algebra (base \u03b9) = change-algebra\u208d \u03b9 \u208e\n change-algebra (\u03c4\u2081 \u21d2 \u03c4\u2082) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}}\n\n change-algebra-family : ChangeAlgebraFamily zero \u27e6_\u27e7Type\n change-algebra-family = family change-algebra\n\n -- function changes\n\n open FunctionChanges public using (cons)\n module _ {\u03c4\u2081 \u03c4\u2082 : Type} where\n open FunctionChanges.FunctionChange _ _ {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}} public\n renaming\n ( correct to is-valid\n ; apply to call-change\n )\n\n -- We also provide: change environments (aka. environment changes).\n\n open ListChanges \u27e6_\u27e7Type {{change-algebra-family}} public using () renaming\n ( changeAlgebra to environment-changes\n )\n\n after-env : \u2200 {\u0393 : Context} \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) \u2192 \u27e6 \u0393 \u27e7\n after-env {\u0393} = after\u208d \u0393 \u208e\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e)\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Denotation.Value as Value\n\nmodule Parametric.Change.Validity\n {Base : Type.Structure}\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Base.Change.Algebra as CA\n using (ChangeAlgebraFamily)\nopen import Level\n\n-- Extension Point: Change algebras for base types\nStructure : Set\u2081\nStructure = ChangeAlgebraFamily zero \u27e6_\u27e7Base\n\nmodule Structure (change-algebra-base : Structure) where\n -- change algebras\n\n open CA public renaming\n ( [] to \u2205\n ; _\u2237_ to _\u2022_\n )\n\n -- We provide: change algebra for every type\n change-algebra : \u2200 \u03c4 \u2192 ChangeAlgebra zero \u27e6 \u03c4 \u27e7Type\n change-algebra (base \u03b9) = change-algebra\u208d \u03b9 \u208e\n change-algebra (\u03c4\u2081 \u21d2 \u03c4\u2082) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}}\n\n change-algebra-family : ChangeAlgebraFamily zero \u27e6_\u27e7Type\n change-algebra-family = family change-algebra\n\n -- function changes\n\n open FunctionChanges public using (cons)\n module _ {\u03c4\u2081 \u03c4\u2082 : Type} where\n open FunctionChanges.FunctionChange _ _ {{change-algebra \u03c4\u2081}} {{change-algebra \u03c4\u2082}} public\n renaming\n ( correct to is-valid\n ; apply to call-change\n )\n\n -- We also provide: change environments (aka. environment changes).\n\n open ListChanges \u27e6_\u27e7Type {{change-algebra-family}} public using () renaming\n ( changeAlgebra to environment-changes\n )\n\n after-env : \u2200 {\u0393 : Context} \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} (d\u03c1 : \u0394\u208d \u0393 \u208e \u03c1) \u2192 \u27e6 \u0393 \u27e7\n after-env {\u0393} = after\u208d \u0393 \u208e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"0c7f128c9e77179348618b2a7fbb141228b0b822","subject":"Extensional equivalence on bags Checkpoint: NatMap is unnecessary. Remove it.","message":"Extensional equivalence on bags\nCheckpoint: NatMap is unnecessary. Remove it.\n\nOld-commit-hash: f465114a1f29ce95ce687d717c48ad6d5bbf7693\n","repos":"inc-lc\/ilc-agda","old_file":"experimental\/MapBag.agda","new_file":"experimental\/MapBag.agda","new_contents":"{-\n\nIntroducing the MapBag, which can be considered a bag\nof \u2124 with negative multiplicities, or a map from\n\u2124 to \u2124 with default value 0.\n\nThe initial goal of this file is to make the 5th example\ndescribed in \/examples.md, \"Map.mapValues\", fast:\n\n -- Haskell's Data.Map.map is Scala's Map.mapValues\n map :: (a -> b) -> Map k a -> Map k b\n \n incVal :: Map Integer Integer -> Map Integer Integer\n incVal = map (+1)\n\n old = fromAscList [(1, 1), (2, 2) .. (n, n)]\n res = incVal old = [(1, 2), (2, 3) .. (n, n + 1)]\n\n\nTODO\nX. Stop not getting why hole in half\u2081-WF can't be filled\nX. Prove well-foundedness of half\u2081\nX. Fix singleton\n3. Make sure this file has no hole\n X. Replace \u2115 by \u2124\n 0. Introduce addition\n 0. Add MapBags and map\n 0. Figure out a way to communicate to a derivative that\n certain changes are always nil (in this case, `+1`).\n\n4. Finish ExplicitNils\n5. Consider appending ExplicitNils\n\n\nChecklist: Adding syntactic constructs\n\n- weaken\n- \u27e6_\u27e7Term\n- weaken-sound\n- derive (symbolic derivation)\n- validity-of-derive\n- correctness-of-derive\n\nChecklist: Adding types\n\n- \u27e6_\u27e7Type\n- \u0394-Type\n- \u27e6derive\u27e7\n- _\u27e6\u229d\u27e7_\n- _\u27e6\u2295\u27e7_\n- f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f\n- f\u2295[g\u229df]=g\n- f\u2295\u0394f=f\n- valid-\u0394\n- R[f,g\u229df]\n- df=f\u2295df\u229df\n- R (inside validity-of-derive)\n\n-}\n\nmodule MapBag where\n\nopen import Data.Unit using\n (\u22a4)\nopen import Data.Nat using\n (\u2115 ; suc ; _\u2264\u2032_ ; \u2264\u2032-refl ; \u2264\u2032-step)\nopen import Induction.Nat using\n (<-rec)\nopen import Data.Integer using\n (\u2124 ; +_ ; -[1+_] ; _+_ ; _-_)\nopen import Data.Product using\n (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\nopen import Relation.Binary.PropositionalEquality\n\nimport Level\nimport Data.Product as Product\n\npostulate extensionality : Extensionality Level.zero Level.zero\n\n-- Map nats to ints with default 0\ndata NatMap : Set where\n \u2205 : NatMap\n \u2115tree : (v : \u2124) \u2192 (left : NatMap) \u2192 (right : NatMap) \u2192 NatMap\n\n-- To understand why there must be an empty NatMap,\n-- observe the termination checker's complaint upon\n-- seeing Haskell's empty NatMap:\n--\n-- emptyMap : NatMap\n-- emptyMap = \u2115tree (+ 0) emptyMap emptyMap\n\ndata Oddity : Set where\n odd : Oddity\n even : Oddity\n\noddity : \u2115 \u2192 Oddity\noddity 0 = even\noddity 1 = odd\noddity (suc (suc n)) = oddity n\n\n-- Elimination form of Oddity to please termination checker\n-- ... eventually.\n\nif-odd_then_else_ : \u2200 {A : Set} \u2192 \u2115 \u2192 A \u2192 A \u2192 A\nif-odd k then thenBranch else elseBranch with oddity k\n... | odd = thenBranch\n... | even = elseBranch\n\n-- oddity-index k = (oddity k , (k - 1) \/ 2)\n-- Here to please termination checker so that \u2115lookup\n-- doesn't have to pass well-foundedness-proofs around\noddity-index : \u2115 \u2192 Oddity \u00d7 \u2115\noddity-index 0 = (even , 0)\noddity-index 1 = (odd , 0)\noddity-index 2 = (even , 0)\noddity-index (suc (suc k)) = (oddity-k , suc [k-1]\/2) where\n oddity-index-k = oddity-index k\n oddity-k = proj\u2081 oddity-index-k\n [k-1]\/2 = proj\u2082 oddity-index-k\n\n-- Computes next key after going down 1 level of a nat tree\nhalf\u2081 : \u2115 \u2192 \u2115\nhalf\u2081 0 = 0\nhalf\u2081 1 = 0\nhalf\u2081 2 = 0\nhalf\u2081 (suc (suc n)) = suc (half\u2081 n)\n\n-- Nonzero n holds a proof that n != 0.\ndata Nonzero : \u2115 \u2192 Set where\n suc : (n : \u2115) \u2192 Nonzero (suc n)\n\n\u2264\u2032-suc : \u2200 {m n : \u2115} \u2192 m \u2264\u2032 n \u2192 (suc m) \u2264\u2032 (suc n)\n\u2264\u2032-suc \u2264\u2032-refl = \u2264\u2032-refl\n\u2264\u2032-suc (\u2264\u2032-step le) = \u2264\u2032-step (\u2264\u2032-suc le)\n\nhalf\u2081-WF : \u2200 (n : \u2115) \u2192 Nonzero n \u2192 suc (half\u2081 n) \u2264\u2032 n\nhalf\u2081-WF 0 ()\nhalf\u2081-WF 1 _ = \u2264\u2032-refl\nhalf\u2081-WF 2 _ = \u2264\u2032-step \u2264\u2032-refl\nhalf\u2081-WF (suc (suc (suc n))) _ =\n \u2264\u2032-suc {suc (half\u2081 (suc n))} {suc (suc n)}\n (\u2264\u2032-step (half\u2081-WF (suc n) (suc n)))\n\n-- Here to please the termination checker\nsingleton : \u2115 \u2192 \u2124 \u2192 NatMap\nsingleton k v = loop k where\n loop : \u2115 \u2192 NatMap\n loop = <-rec _ \u03bb\n { 0 _ \u2192 \u2115tree v \u2205 \u2205\n ; (suc n\u2080) rec \u2192\n let\n n = suc n\u2080\n next = rec (half\u2081 n) (half\u2081-WF n (suc n\u2080))\n in\n if-odd n\n then \u2115tree (+ 0) next \u2205\n else \u2115tree (+ 0) \u2205 next\n }\n\n\u2115lookup : \u2115 \u2192 NatMap \u2192 \u2124\n\u2115lookup _ \u2205 = (+ 0)\n\u2115lookup 0 (\u2115tree v _ _) = v\n\u2115lookup k (\u2115tree _ left right) with oddity-index k\n... | (odd , [k-1]\/2) = \u2115lookup [k-1]\/2 left\n... | (even , [k-1]\/2) = \u2115lookup [k-1]\/2 right\n\n-- `update` in the sense of\n-- Data.Sequence.update : Int \u2192 a \u2192 Seq a \u2192 Seq a\n\u2115update : \u2115 \u2192 \u2124 \u2192 NatMap \u2192 NatMap\n\u2115update k v \u2205 = singleton k v\n\u2115update 0 v (\u2115tree v\u2080 left right) = \u2115tree v left right\n\u2115update k v (\u2115tree v\u2080 left right) with oddity-index k\n... | (odd , [k-1]\/2) = \u2115tree v\u2080 (\u2115update [k-1]\/2 v left) right\n... | (even , [k-1]\/2) = \u2115tree v\u2080 left (\u2115update [k-1]\/2 v right)\n\nMapBag = NatMap \u00d7 NatMap\n\nemptyBag : MapBag\nemptyBag = (\u2205 , \u2205)\n\nlookup : \u2124 \u2192 MapBag \u2192 \u2124\nlookup -[1+ k ] (negative , nonnegative) = \u2115lookup k negative\nlookup (+ k) (negative , nonnegative) = \u2115lookup k nonnegative\n\nupdate : \u2124 \u2192 \u2124 \u2192 MapBag \u2192 MapBag\nupdate -[1+ k ] v (neg , pos) = (\u2115update k v neg , pos)\nupdate ( + k ) v (neg , pos) = (neg , \u2115update k v pos)\n\n-- We implement nothing but the necessary NatMap operations to save time:\n-- union, difference, mapValues.\n\n\u2115mapValues : (\u2124 \u2192 \u2124) \u2192 NatMap \u2192 NatMap\n\u2115mapValues _ \u2205 = \u2205\n\u2115mapValues f (\u2115tree v left right) =\n \u2115tree (f v) (\u2115mapValues f left) (\u2115mapValues f right)\n\nmapValues : (\u2124 \u2192 \u2124) \u2192 MapBag \u2192 MapBag\nmapValues f (neg , pos) = (\u2115mapValues f neg , \u2115mapValues f pos)\n\n\u2115union : NatMap \u2192 NatMap \u2192 NatMap\n\u2115union \u2205 b = b\n\u2115union b \u2205 = b\n\u2115union (\u2115tree v\u2081 left\u2081 right\u2081) (\u2115tree v\u2082 left\u2082 right\u2082) =\n \u2115tree (v\u2081 + v\u2082) (\u2115union left\u2081 left\u2082) (\u2115union right\u2081 right\u2082)\n\n-- Prelude.(++) : [a] \u2192 [a] \u2192 [a]\n_++_ : MapBag \u2192 MapBag \u2192 MapBag\nb\u2081 ++ b\u2082 = Product.zip \u2115union \u2115union b\u2081 b\u2082\n\ninfixr 5 _++_\n\n\u2115diff : NatMap \u2192 NatMap \u2192 NatMap\n\u2115diff \u2205 b = \u2205\n\u2115diff b \u2205 = b\n\u2115diff (\u2115tree v\u2081 left\u2081 right\u2081) (\u2115tree v\u2082 left\u2082 right\u2082) =\n \u2115tree (v\u2081 - v\u2082) (\u2115diff left\u2081 left\u2082) (\u2115diff right\u2081 right\u2082)\n\n-- Data.Map.(\\\\) : Map k a \u2192 Map k b \u2192 Map k a (where b = a = \u2124)\n_\\\\_ : MapBag \u2192 MapBag \u2192 MapBag\nb\u2081 \\\\ b\u2082 = Product.zip \u2115diff \u2115diff b\u2081 b\u2082\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n-- Bag's extensional equivalence:\n-- Remove Quinean \"identity without identity\" on bags\n-- (keys with 0 multiplicities and no subkeys are as good\n-- as not to exist).\n\ndata \u2115Empty : NatMap \u2192 Set where\n is-empty : \u2115Empty \u2205\n 0-mult : \u2200 {left right} \u2192 \u2115Empty left \u2192 \u2115Empty right \u2192\n \u2115Empty (\u2115tree (+ 0) left right)\n\nEmpty : MapBag \u2192 Set\nEmpty (neg , pos) = \u2115Empty neg \u00d7 \u2115Empty pos\n\npostulate\n ext-bag : \u2200 {b\u2081 b\u2082} \u2192 Empty (b\u2081 \\\\ b\u2082) \u2192 b\u2081 \u2261 b\u2082\n\ninfixl 9 _\\\\_\n\ndata Type : Set where\n ints : Type\n bags : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n\n int : \u2200 {\u0393} \u2192 (n : \u2124) \u2192 Term \u0393 ints\n bag : \u2200 {\u0393} \u2192 (b : MapBag) \u2192 Term \u0393 bags\n add : \u2200 {\u0393} \u2192 (t\u2081 : Term \u0393 ints) \u2192 (t\u2082 : Term \u0393 ints) \u2192 Term \u0393 ints\n map : \u2200 {\u0393} \u2192 (f : Term \u0393 (ints \u21d2 ints)) \u2192 (b : Term \u0393 bags) \u2192 Term \u0393 bags\n\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n -- Change to ints = replacement Church pairs\n -- 3 -> 5 ::= \u03bbf. f 3 5\n\n -- Change to bags = a summand\n -- b\u2081 -> b\u2082 ::= b\u2082 \\\\ b\u2081\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (int x) = int x\nweaken subctx (bag b) = bag b\nweaken subctx (add t\u2081 t\u2082) = add (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (map f b) = map (weaken subctx f) (weaken subctx b)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 ints \u27e7Type = \u2124\n\u27e6 bags \u27e7Type = MapBag\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 int n \u27e7Term \u03c1 = n\n\u27e6 bag b \u27e7Term \u03c1 = b\n\u27e6 add m n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 map f b \u27e7Term \u03c1 = mapValues (\u27e6 f \u27e7Term \u03c1) (\u27e6 b \u27e7Term \u03c1)\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n_\u27e8$\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\ninfix 0 _\u27e8$\u27e9_ -- infix 0 $ in Haskell\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = weaken-sound t\u2081 \u03c1 \u27e8$\u27e9 weaken-sound t\u2082 \u03c1\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (int n) \u03c1 = refl\n\nweaken-sound (bag b) \u03c1 = refl\nweaken-sound (add m n) \u03c1 = cong\u2082 _+_ (weaken-sound m \u03c1) (weaken-sound n \u03c1)\nweaken-sound (map f b) \u03c1 = cong\u2082 mapValues (weaken-sound f \u03c1)\n (weaken-sound b \u03c1)\n\n-- Changes to \u2124 are replacement Church pairs. The only arguments\n-- of conern are `fst` and `snd`, so the Church pairs don't have\n-- to be polymorphic.\n--\n-- Changes on mapbags are mapbags. They allow negative multiplicities\n-- to begin with.\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type ints = (ints \u21d2 ints \u21d2 ints) \u21d2 ints\n\u0394-Type bags = bags\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\n-- It is clear that \u27e6\u229d\u27e7 exists on the semantic level:\n-- there exists an Agda value to describe the change between any\n-- two Agda values denoted by terms. If we have (not dependently-\n-- typed) arrays, no term denotes the change between two arrays\n-- of different lengths. Thus no full abstraction.\n\n\u27e6derive\u27e7 : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ninfixl 6 _\u27e6\u2295\u27e7_\ninfixl 6 _\u27e6\u229d\u27e7_\n\n\u27e6fst\u27e7 : \u2124 \u2192 \u2124 \u2192 \u2124\n\u27e6snd\u27e7 : \u2124 \u2192 \u2124 \u2192 \u2124\n\n\u27e6derive\u27e7 {ints} n = \u03bb f \u2192 f n n\n\u27e6derive\u27e7 {bags} b = emptyBag\n\u27e6derive\u27e7 {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 f (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f v\n\n_\u27e6\u229d\u27e7_ {ints} m n = \u03bb f \u2192 f n m\n_\u27e6\u229d\u27e7_ {bags} b\u2081 b\u2082 = b\u2081 \\\\ b\u2082\n_\u27e6\u229d\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 f\u2081 (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f\u2082 v\n-- m \u27e6\u229d\u27e7 n ::= replace n by m\n\n_\u27e6\u2295\u27e7_ {ints} n dn = dn \u27e6snd\u27e7\n_\u27e6\u2295\u27e7_ {bags} b db = b ++ db\n_\u27e6\u2295\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n\n\u27e6fst\u27e7 m n = m\n\u27e6snd\u27e7 m n = n\n{-\n-- Cool lemmas\n\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u229d\u27e7 f \u2261 \u27e6derive\u27e7 f\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {ints} f = refl\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {bags} b = {!!}\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = refl\n\nf\u2295[g\u229df]=g : \u2200 {\u03c4 : Type} (f g : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) \u2261 g\n\nf\u2295\u0394f=f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (\u27e6derive\u27e7 f) \u2261 f\n\nf\u2295[g\u229df]=g {ints} m n = refl\nf\u2295[g\u229df]=g {bags} b\u2081 b\u2082 = {!!}\nf\u2295[g\u229df]=g {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = f x} {y = f x} refl\n (cong\u2082 _\u27e6\u229d\u27e7_ (cong g (f\u2295\u0394f=f x)) refl) \u27e9\n f x \u27e6\u2295\u27e7 (g x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (g x) \u27e9\n g x\n \u220e\n ) where open \u2261-Reasoning\n\nf\u2295\u0394f=f {ints} n = refl\nf\u2295\u0394f=f {bags} b = {!!}\nf\u2295\u0394f=f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 \u27e6derive\u27e7 f) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (f (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong (\u03bb hole \u2192 f x \u27e6\u2295\u27e7 (f hole \u27e6\u229d\u27e7 f x)) (f\u2295\u0394f=f x) \u27e9\n f x \u27e6\u2295\u27e7 (f x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (f x) \u27e9\n f x\n \u220e\n ) where open \u2261-Reasoning\n\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {ints} n dn = n \u2261 dn \u27e6fst\u27e7\nvalid-\u0394 {bags} b db = {!!}\nvalid-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (s : \u27e6 \u03c4\u2081 \u27e7) (ds : \u27e6 \u0394-Type \u03c4\u2081 \u27e7) (R[s,ds] : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7 -- (valid-\u0394:1)\n (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 f s \u27e6\u2295\u27e7 df s ds -- (valid-\u0394:2)\n\nR[f,g\u229df] : \u2200 {\u03c4} (f g : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (g \u27e6\u229d\u27e7 f)\nR[f,g\u229df] {ints} m n = refl\nR[f,g\u229df] {bags} b\u2081 b\u2082 = {!!}\nR[f,g\u229df] {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb x dx R[x,dx] \u2192\n R[f,g\u229df] {\u03c4\u2082} (f x) (g (x \u27e6\u2295\u27e7 dx)) -- (valid-\u0394:1)\n , -- tuple constructor\n (begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 refl \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7\n (g ((x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 \u27e6derive\u27e7 (x \u27e6\u2295\u27e7 dx)) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 cong (\u03bb hole \u2192 f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g hole \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx)))\n (f\u2295\u0394f=f (x \u27e6\u2295\u27e7 dx)) \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 f\u2295[g\u229df]=g (f (x \u27e6\u2295\u27e7 dx)) (g (x \u27e6\u2295\u27e7 dx)) \u27e9\n g (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (f x) (g (x \u27e6\u2295\u27e7 dx))) \u27e9\n f x \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) x dx\n \u220e)\n where open \u2261-Reasoning\n\nR[f,\u0394f] : \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (\u27e6derive\u27e7 f)\nR[f,\u0394f] f rewrite sym (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f f) = R[f,g\u229df] f f\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393 -- push \u03c4, then push \u0394\u03c4.\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 (keep \u03c4 \u2022 \u0393\u227c\u0394\u0393)\n\n-- Data type to hold proofs that environments are valid\ndata Valid-\u0394env : {\u0393 : Context} (\u03c1 : \u27e6 \u0393 \u27e7) (\u0394\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is its own valid \u0394-environment.\n \u03c1=\u2205 : Valid-\u0394env {\u2205} \u2205 \u2205\n -- Induction case: the change introduced therein should be valid,\n -- and the smaller \u0394-environment should be valid as well.\n \u03c1=v\u2022\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {\u03c1\u2080 : \u27e6 \u0393\u2080 \u27e7} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Valid-\u0394env \u03c1\u2080 d\u03c1\u2080 \u2192\n Valid-\u0394env {\u03c4 \u2022 \u0393\u2080} (v \u2022 \u03c1\u2080) (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- Data type to hold proofs that a \u0394-env is consistent with self\ndata Consistent-\u0394env : {\u0393 : Context} (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is consistent with itself\n d\u03c1=\u2205 : Consistent-\u0394env {\u2205} \u2205\n -- Induction case: the change introduced at top level\n -- should be valid.\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Consistent-\u0394env d\u03c1\u2080 \u2192\n Consistent-\u0394env {\u03c4 \u2022 \u0393\u2080} (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- If a \u0394-environment is valid for some other environment,\n-- then it is also consistent with itself.\n\nvalid-\u0394env-is-consistent :\n \u2200 {\u0393 : Context} {\u03c1 : \u27e6 \u0393 \u27e7} {d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Valid-\u0394env \u03c1 d\u03c1 \u2192 Consistent-\u0394env d\u03c1\n\nvalid-\u0394env-is-consistent \u03c1=\u2205 = d\u03c1=\u2205\nvalid-\u0394env-is-consistent (\u03c1=v\u2022\u03c1\u2080 valid[v,dv] valid[\u03c1\u2080,d\u03c1\u2080]) =\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] (valid-\u0394env-is-consistent valid[\u03c1\u2080,d\u03c1\u2080])\n\n-- finally, update and ignore\n\nupdate : \u2200 {\u0393} \u2192 (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env d\u03c1} \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 {d\u03c1=\u2205} = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 d\u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] consistent[d\u03c1\u2080]} =\n (v \u27e6\u2295\u27e7 dv) \u2022 update d\u03c1 {consistent[d\u03c1\u2080]}\n\n-- Ignorance is bliss (don't have to pass consistency proofs around :D)\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 -- Using a proof to describe computation\n\n-- Naming scheme follows weakenVar\/weaken\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- derive(n) = \u03bbf. f n n\nderive (int n) = abs (app (app (var this) (int n)) (int n))\n\n-- derive(x) = dx\nderive (var x) = var (deriveVar x)\n\n-- derive(\u03bbx. t) = \u03bbx. \u03bbdx. derive(t)\nderive (abs t) = abs (abs (derive t))\n\n-- derive(f s) = derive(f) s derive(s)\nderive (app f s) = app (app (derive f) (weaken \u0393\u227c\u0394\u0393 s)) (derive s)\n\nderive _ = {!!}\n\n-- Extensional equivalence for changes\ndata Extensionally-equivalent-as-changes :\n \u2200 (\u03c4 : Type) \u2192 (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 (dg : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n ext-\u0394 : \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n (\u2200 (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192\n (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)) \u2192\n Extensionally-equivalent-as-changes \u03c4 df dg\n\nsyntax Extensionally-equivalent-as-changes \u03c4 df dg = df \u2248 dg :\u0394 \u03c4\n\n-- Question: How to declare fixity for infix syntax?\n-- infix 4 _\u2248_:\u0394_ -- same as \u2261\n\n-- Extractor for extensional-equivalence-as-changes:\n-- Given a value of the data type holding the proof,\n-- returns the proof in applicable form.\n--\n-- It would not be necessary if such\n-- proof-holding types were defined as a function in\n-- the first place, say in the manner of `valid-\u0394`.\n--\nextract-\u0394equiv :\n \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n df \u2248 dg :\u0394 \u03c4 \u2192\n (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192 (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)\n\nextract-\u0394equiv (ext-\u0394 proof-method) = proof-method\n\n-- Distribution lemmas of validity over \u27e6\u229d\u27e7 and \u27e6\u2295\u27e7\n\napplication-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v) \u2261 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\napplication-over-\u2295-and-\u229d f g df v =\n begin\n f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (cong\u2082 _\u27e6\u2295\u27e7_\n (cong f ((sym (f\u2295\u0394f=f v))))\n (cong\u2082 df\n (sym (f\u2295\u0394f=f v))\n (cong \u27e6derive\u27e7 (sym (f\u2295\u0394f=f v)))))\n refl \u27e9\n f (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v)\n \u27e6\u2295\u27e7 df (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v) (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v))\n \u27e6\u229d\u27e7 f v\n \u2261\u27e8 refl \u27e9\n (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e where open \u2261-Reasoning\n\nvalidity-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n valid-\u0394 g (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) \u2192\n valid-\u0394 (g v) (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\nvalidity-over-\u2295-and-\u229d f g df v R[g,f\u2295df]\n rewrite application-over-\u2295-and-\u229d f g df v =\n proj\u2081 (R[g,f\u2295df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))\n\n-- diff-apply\ndf=f\u2295df\u229df :\n \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n valid-\u0394 f df \u2192 df \u2248 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) :\u0394 \u03c4\n\n-- Case int: this REFL is more obvious to Agda than to a human.\ndf=f\u2295df\u229df {ints} n dn valid-n-dn =\n ext-\u0394 (\u03bb m valid-m-dn valid-rhs \u2192 refl)\n\ndf=f\u2295df\u229df {bags} b db valid-b-db = {!!}\n\ndf=f\u2295df\u229df {\u03c4\u2081 \u21d2 \u03c4\u2082} f df R[f,df] = ext-\u0394 (\n \u03bb g R[g,df] R[g,f\u2295df\u229df] \u2192\n extensionality (\u03bb v \u2192\n begin\n g v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n \u2261\u27e8 extract-\u0394equiv\n (df=f\u2295df\u229df\n (f v) (df v (\u27e6derive\u27e7 v))\n (proj\u2081 (R[f,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))))\n (g v)\n (proj\u2081 (R[g,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v)))\n (validity-over-\u2295-and-\u229d f g df v R[g,f\u2295df\u229df]) \u27e9\n g v \u27e6\u2295\u27e7 (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = g v} {y = g v} refl\n (application-over-\u2295-and-\u229d f g df v) \u27e9\n g v \u27e6\u2295\u27e7 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e\n )\n )\n where open \u2261-Reasoning\n\n\n\ncorrectness-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n (\u27e6 deriveVar x \u27e7 \u03c1)\n \u2248\n (\u27e6 x \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 x \u27e7 (ignore \u03c1)) :\u0394 \u03c4\n\ncorrectness-of-deriveVar {\u03c4 \u2022 \u0393\u2080} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n this = df=f\u2295df\u229df {\u03c4} v dv R[v,dv]\n\ncorrectness-of-deriveVar {\u03c4\u2080 \u2022 \u0393\u2080} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4\u2080} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n (that x) = correctness-of-deriveVar \u03c1 x\n\n\n\ncorrectness-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n\n (\u27e6 derive t \u27e7 \u03c1)\n \u2248 (\u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 t \u27e7 (ignore \u03c1)) :\u0394 \u03c4\n\n-- Mutually recursive lemma: derivatives are valid\nvalidity-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 deriveVar x \u27e7 \u03c1)\n\nvalidity-of-deriveVar {\u03c4 \u2022 \u0393} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n this = R[v,dv]\n\nvalidity-of-deriveVar {\u03c4\u2080 \u2022 \u0393} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n (that x) = validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 {consistency} (var x) =\n validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 (int n) = refl\n\nvalidity-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs t) = \u03bb v dv R[v,dv] \u2192\n validity-of-derive (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency} t\n , -- tuple constructor\n (begin\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n ((R[f,g\u229df] (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 f\u2295[g\u229df]=g (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u2261\u27e8 cong \u27e6 t \u27e7 (cong\u2082 _\u2022_ (f\u2295\u0394f=f (v \u27e6\u2295\u27e7 dv)) refl) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency}))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 sym (extract-\u0394equiv\n (correctness-of-derive\n ((dv \u2022 v \u2022 \u03c1))\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (R[f,g\u229df] (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u220e)\n where open \u2261-Reasoning\n\nvalidity-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} t\u2081 t\u2082)\n = R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7]\n where\n open \u2261-Reasoning\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 : \u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1) \u2261 v\u2081 v\u2082\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 rewrite (sym v\u2082=old-v\u2082) = refl\n\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 : \u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1 \u2261 dv\u2081 v\u2082 dv\u2082\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = refl\n\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] : valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (dv\u2081 v\u2082 dv\u2082)\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] rewrite \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 = R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n -- What I want to write:\n {-\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] rewrite \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n -}\n\n -- What I have to write:\n\n R : {\u03c4 : Type} \u2192 {v : \u27e6 \u03c4 \u27e7} \u2192 {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 valid-\u0394 v dv\u2081 \u2192 valid-\u0394 v dv\u2082\n\n R {ints} dv\u2081=dv\u2082 refl = cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl\n\n R {bags} dv\u2081=dv\u2082 _ = {!!}\n\n --R {\u03c4\u2081 \u21d2 \u03c4\u2082} dv\u2081=dv\u2082 valid-dv\u2081 rewrite dv\u2081=dv\u2082 = {!valid-dv\u2081!}\n R {\u03c4\u2081 \u21d2 \u03c4\u2082} {v} {dv\u2081} {dv\u2082} dv\u2081=dv\u2082 valid-dv\u2081 =\n \u03bb s ds R[s,ds] \u2192\n R {\u03c4\u2082} {v s} {dv\u2081 s ds} {dv\u2082 s ds}\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl)\n (proj\u2081 (valid-dv\u2081 s ds R[s,ds]))\n ,\n (begin\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2082 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v (s \u27e6\u2295\u27e7 ds)} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) (sym dv\u2081=dv\u2082) refl) refl) \u27e9\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2081 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 sym (proj\u2082 (valid-dv\u2081\n (s \u27e6\u2295\u27e7 ds)\n (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n (R[f,\u0394f] (s \u27e6\u2295\u27e7 ds)))) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds \u27e6\u2295\u27e7 \u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong (v \u27e6\u2295\u27e7 dv\u2081) (f\u2295\u0394f=f (s \u27e6\u2295\u27e7 ds)) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds)\n \u2261\u27e8 proj\u2082 (valid-dv\u2081 s ds R[s,ds]) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2081 s ds\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v s} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2082 s ds\n \u220e) where open \u2261-Reasoning\n\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] = R \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n\nvalidity-of-derive \u03c1 {consistency} _ = {!!}\n\ncorrectness-of-derive \u03c1 (var x) = correctness-of-deriveVar \u03c1 x\n\ncorrectness-of-derive \u03c1 (int n) = ext-\u0394 (\u03bb _ _ _ \u2192 refl)\n\ncorrectness-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs {.\u03c4\u2081} {.\u03c4\u2082} t) =\n ext-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n extensionality {\u27e6 \u03c4\u2081 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4\u2082 \u27e7} (\u03bb x \u2192\n begin\n f x \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive {\u03c4\u2081 \u2022 \u0393} {\u03c4\u2082}\n (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency}\n t)\n (f x)\n (proj\u2081 (R[f,\u0394t] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x)))\n (proj\u2081 (R[f,t\u2032\u229dt] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x))) \u27e9\n f x\n \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 (update (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency})\n \u27e6\u229d\u27e7 \u27e6 t \u27e7 (x \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u220e\n )) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} {\u0393} t\u2081 t\u2082) =\n ext-\u0394 {\u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n begin\n f \u27e6\u2295\u27e7 \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n \u2261\u27e8 extract-\u0394equiv ext-\u0394[t\u2081t\u2082] f R[f,\u0394t] R[f,t\u2032\u229dt] \u27e9\n f \u27e6\u2295\u27e7\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n \u220e)\n where\n open \u2261-Reasoning\n\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n v\u2081\u2295dv\u2081=v\u2081\u2032 : v\u2081 \u27e6\u2295\u27e7 dv\u2081 \u2261 v\u2081\u2032\n v\u2081\u2295dv\u2081=v\u2081\u2032 =\n begin\n v\u2081 \u27e6\u2295\u27e7 dv\u2081\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2081)\n v\u2081 valid-dv\u2081 (R[f,g\u229df] v\u2081 v\u2081\u2032) \u27e9\n v\u2081 \u27e6\u2295\u27e7 (v\u2081\u2032 \u27e6\u229d\u27e7 v\u2081)\n \u2261\u27e8 f\u2295[g\u229df]=g v\u2081 v\u2081\u2032 \u27e9\n v\u2081\u2032\n \u220e\n\n -- TODO: remove code duplication.\n v\u2082\u2295dv\u2082=v\u2082\u2032 : v\u2082 \u27e6\u2295\u27e7 dv\u2082 \u2261 v\u2082\u2032\n v\u2082\u2295dv\u2082=v\u2082\u2032 rewrite v\u2082=old-v\u2082 =\n begin\n old-v\u2082 \u27e6\u2295\u27e7 dv\u2082\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2082)\n old-v\u2082\n (validity-of-derive \u03c1 {consistency} t\u2082)\n (R[f,g\u229df] old-v\u2082 v\u2082\u2032) \u27e9\n old-v\u2082 \u27e6\u2295\u27e7 (v\u2082\u2032 \u27e6\u229d\u27e7 old-v\u2082)\n \u2261\u27e8 f\u2295[g\u229df]=g old-v\u2082 v\u2082\u2032 \u27e9\n v\u2082\u2032\n \u220e\n where old-v\u2082 = \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] : v\u2081\u2032 v\u2082\u2032 \u2261 (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082)\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] = sym (cong\u2082 (\u03bb f x \u2192 f x) v\u2081\u2295dv\u2081=v\u2081\u2032 v\u2082\u2295dv\u2082=v\u2082\u2032)\n\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 :\n (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082) \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 = proj\u2082 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082 \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n cong\u2082 _\u27e6\u229d\u27e7_\n (trans v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082)\n refl\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n (dv\u2081 v\u2082 dv\u2082) \u2248 (v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082) :\u0394 \u03c4\u2082\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n df=f\u2295df\u229df (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082) R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 : (dv\u2081 v\u2082 dv\u2082) \u2248 (v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082) :\u0394 \u03c4\u2082\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 rewrite v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082\n\n ext-\u0394[t\u2081t\u2082] :\n (\u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1))\n \u2248\n (\u27e6 t\u2081 \u27e7 (update \u03c1 {consistency}) (\u27e6 t\u2082 \u27e7 (update \u03c1 {consistency}))\n \u27e6\u229d\u27e7 \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n :\u0394 \u03c4\u2082\n ext-\u0394[t\u2081t\u2082] rewrite sym v\u2082=old-v\u2082 = dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082\n\ncorrectness-of-derive \u03c1 {consistency} _ = {!!}\n\ncorrectness-on-closed-terms : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192\n \u2200 (f : Term \u2205 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192\n \u2200 (s : Term \u2205 \u03c4\u2081) (ds : Term \u2205 (\u0394-Type \u03c4\u2081))\n {R[v,dv] : valid-\u0394 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)} \u2192\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205 \u27e6\u2295\u27e7 \u27e6 ds \u27e7 \u2205)\n \u2261\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)\n\ncorrectness-on-closed-terms {\u03c4\u2081} {\u03c4\u2082} f s ds {R[v,dv]} =\n begin\n h (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (f\u2295[g\u229df]=g h h))\n refl \u27e9\n (h \u27e6\u2295\u27e7 (h \u27e6\u229d\u27e7 h)) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (extract-\u0394equiv\n (correctness-of-derive \u2205 f)\n h\n (validity-of-derive \u2205 {d\u03c1=\u2205} f)\n (R[f,g\u229df] h h)))\n refl \u27e9\n (h \u27e6\u2295\u27e7 \u0394h) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 proj\u2082 (validity-of-derive \u2205 {d\u03c1=\u2205} f v dv R[v,dv]) \u27e9\n h v \u27e6\u2295\u27e7 \u0394h v dv\n \u220e\n where\n open \u2261-Reasoning\n h : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n h = \u27e6 f \u27e7 \u2205\n \u0394h : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n \u0394h = \u27e6 derive f \u27e7 \u2205\n v : \u27e6 \u03c4\u2081 \u27e7\n v = \u27e6 s \u27e7 \u2205\n dv : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv = \u27e6 ds \u27e7 \u2205\n\n-}\n\n","old_contents":"{-\n\nIntroducing the MapBag, which can be considered a bag\nof \u2124 with negative multiplicities, or a map from\n\u2124 to \u2124 with default value 0.\n\nThe initial goal of this file is to make the 5th example\ndescribed in \/examples.md, \"Map.mapValues\", fast:\n\n -- Haskell's Data.Map.map is Scala's Map.mapValues\n map :: (a -> b) -> Map k a -> Map k b\n \n incVal :: Map Integer Integer -> Map Integer Integer\n incVal = map (+1)\n\n old = fromAscList [(1, 1), (2, 2) .. (n, n)]\n res = incVal old = [(1, 2), (2, 3) .. (n, n + 1)]\n\nTODO\nX. Replace \u2115 by \u2124\n2. Introduce addition\n3. Add MapBags and map\n4. Figure out a way to communicate to a derivative that\n certain changes are always nil (in this case, `+1`).\n\n\nChecklist: Adding syntactic constructs\n\n- weaken\n- \u27e6_\u27e7Term\n- weaken-sound\n- derive (symbolic derivation)\n- validity-of-derive\n- correctness-of-derive\n\nChecklist: Adding types\n\n- \u27e6_\u27e7Type\n- \u0394-Type\n- \u27e6derive\u27e7\n- _\u27e6\u229d\u27e7_\n- _\u27e6\u2295\u27e7_\n- f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f\n- f\u2295[g\u229df]=g\n- f\u2295\u0394f=f\n- valid-\u0394\n- R[f,g\u229df]\n- df=f\u2295df\u229df\n- R (inside validity-of-derive)\n\n-}\n\nmodule MapBag where\n\nopen import Data.Unit using\n (\u22a4)\nopen import Data.Nat using\n (\u2115 ; suc ; _\u2264\u2032_ ; \u2264\u2032-refl ; \u2264\u2032-step)\nopen import Induction.Nat using\n (<-rec)\nopen import Data.Integer using\n (\u2124 ; +_ ; -[1+_] ; _+_ ; _-_)\nopen import Data.Product using\n (_\u00d7_ ; _,_ ; proj\u2081 ; proj\u2082)\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\n\nopen import Relation.Binary.PropositionalEquality\n\nimport Level\n\npostulate extensionality : Extensionality Level.zero Level.zero\n\n-- Map nats to ints with default 0\ndata NatMap : Set where\n \u2205 : NatMap\n \u2115tree : (v : \u2124) \u2192 (left : NatMap) \u2192 (right : NatMap) \u2192 NatMap\n\n{- To understand why there must be an empty NatMap,\n observe the termination checker's complaint upon\n seeing Haskell's empty NatMap:\n\nemptyMap : NatMap\nemptyMap = \u2115tree (+ 0) emptyMap emptyMap\n-}\n\ndata Oddity : Set where\n odd : Oddity\n even : Oddity\n\noddity : \u2115 \u2192 Oddity\noddity 0 = even\noddity 1 = odd\noddity (suc (suc n)) = oddity n\n\n-- Elimination form of Oddity to please termination checker\n-- ... eventually.\n\nif-odd_then_else_ : \u2200 {A : Set} \u2192 \u2115 \u2192 A \u2192 A \u2192 A\nif-odd k then thenBranch else elseBranch with oddity k\n... | odd = thenBranch\n... | even = elseBranch\n\n-- oddity-index k = (oddity k , (k - 1) \/ 2)\noddity-index : \u2115 \u2192 Oddity \u00d7 \u2115\noddity-index 0 = (even , 0)\noddity-index 1 = (odd , 0)\noddity-index 2 = (even , 0)\noddity-index (suc (suc k)) = (oddity-k , suc [k-1]\/2) where\n oddity-index-k = oddity-index k\n oddity-k = proj\u2081 oddity-index-k\n [k-1]\/2 = proj\u2082 oddity-index-k\n\n-- Copied from Induction.Nat.Examples (declared private)\n\nhalf\u2081 : \u2115 \u2192 \u2115\nhalf\u2081 0 = 0\nhalf\u2081 1 = 0\nhalf\u2081 2 = 0\nhalf\u2081 (suc (suc n)) = suc (half\u2081 n)\n\ndata Nonzero : \u2115 \u2192 Set where\n suc : (n : \u2115) \u2192 Nonzero (suc n)\n\n\u2264\u2032-suc : \u2200 {m n : \u2115} \u2192 m \u2264\u2032 n \u2192 (suc m) \u2264\u2032 (suc n)\n\u2264\u2032-suc \u2264\u2032-refl = \u2264\u2032-refl\n\u2264\u2032-suc (\u2264\u2032-step le) = \u2264\u2032-step (\u2264\u2032-suc le)\n\nhalf\u2081-\u03b2\u2261 : \u2200 {n : \u2115} \u2192 Nonzero n \u2192 half\u2081 (suc (suc n)) \u2261 suc (half\u2081 n)\nhalf\u2081-\u03b2\u2261 {0} ()\nhalf\u2081-\u03b2\u2261 {suc n} nz = refl\n\nhalf\u2081-WF : \u2200 (n : \u2115) \u2192 Nonzero n \u2192 suc (half\u2081 n) \u2264\u2032 n\nhalf\u2081-WF 0 ()\nhalf\u2081-WF 1 _ = \u2264\u2032-refl\nhalf\u2081-WF 2 _ = \u2264\u2032-step \u2264\u2032-refl\nhalf\u2081-WF (suc (suc (suc n))) _ =\n \u2264\u2032-suc {suc (half\u2081 (suc n))} {suc (suc n)} {!half\u2081-WF (suc n) ?!}\n-- TODO:\n-- 0. Stop not getting why hole#0 can't be filled\n-- 1. Prove well-foundedness of half\u2081\n-- 2. Fix singleton\n-- 3. Make sure this file has no hole\n-- 4. Finish ExplicitNils\n-- 5. Consider appending ExplicitNils\n\n-- Here to please the termination checker\nsingleton : \u2115 \u2192 \u2124 \u2192 NatMap\nsingleton k v = loop k where\n loop : \u2115 \u2192 NatMap\n loop = <-rec _ \u03bb\n { 0 _ \u2192 \u2115tree v \u2205 \u2205\n ; n rec \u2192\n let\n next = half\u2081 n\n in -- TODO: Case distinction\n rec next {!!}\n }\n\n{-\ncRec _ \u03bb\n { 0 _ \u2192 (0 , \u2115tree v \u2205 \u2205)\n ; 1 _ \u2192 (100 , \u2115tree (+ 0) (\u2115tree v \u2205 \u2205) \u2205)\n ; 2 _ \u2192 (200 , \u2115tree (+ 0) \u2205 (\u2115tree v \u2205 \u2205))\n ; (suc (suc n)) (_ , self , _) \u2192\n let\n [[n+2]-1]\/2 = suc (proj\u2081 self)\n half-map = proj\u2082 self\n in if-odd n\n then ([[n+2]-1]\/2 , \u2115tree (+ 0) half-map \u2205)\n else ([[n+2]-1]\/2 , \u2115tree (+ 0) \u2205 half-map)\n }\n-}\n\n\u2115lookup : \u2115 \u2192 NatMap \u2192 \u2124\n\u2115lookup _ \u2205 = (+ 0)\n\u2115lookup 0 (\u2115tree v _ _) = v\n\u2115lookup k (\u2115tree _ left right) with oddity-index k\n... | (odd , [k-1]\/2) = \u2115lookup [k-1]\/2 left\n... | (even , [k-1]\/2) = \u2115lookup [k-1]\/2 right\n\n-- `\u2192` and `in` are keywords\n\u2115set_\u21d2_within_ : \u2115 \u2192 \u2124 \u2192 NatMap \u2192 NatMap\n\u2115set k \u21d2 v within \u2205 = singleton k v\n\u2115set 0 \u21d2 v within (\u2115tree v\u2080 left right) = \u2115tree v left right\n\u2115set k \u21d2 v within (\u2115tree v\u2080 left right) with oddity-index k\n... | (odd , [k-1]\/2) = \u2115tree v\u2080 left (\u2115set [k-1]\/2 \u21d2 v within right)\n... | (even , [k-1]\/2) = \u2115tree v\u2080 (\u2115set [k-1]\/2 \u21d2 v within left) right\n\n-- We implement nothing but the necessary NatMap operations to save time:\n-- union, difference, mapValues.\n\n\u2115mapValues : (\u2124 \u2192 \u2124) \u2192 NatMap \u2192 NatMap\n\u2115mapValues _ \u2205 = \u2205\n\u2115mapValues f (\u2115tree v left right) =\n \u2115tree (f v) (\u2115mapValues f left) (\u2115mapValues f right)\n\nMapBag = NatMap \u00d7 NatMap\n\nlookup : \u2124 \u2192 MapBag \u2192 \u2124\nlookup -[1+ k ] b = \u2115lookup k (proj\u2081 b)\nlookup (+ k) b = \u2115lookup k (proj\u2082 b)\n\nset_\u21d2_within_ : \u2124 \u2192 \u2124 \u2192 MapBag \u2192 MapBag\nset -[1+ k ] \u21d2 v within (neg , pos) = (\u2115set k \u21d2 v within neg , pos)\nset + k \u21d2 v within (neg , pos) = (neg , \u2115set k \u21d2 v within pos)\n\nmapValues : (\u2124 \u2192 \u2124) \u2192 MapBag \u2192 MapBag\nmapValues f (neg , pos) = (\u2115mapValues f neg , \u2115mapValues f pos)\n\n{-\n\ndata Type : Set where\n ints : Type\n bags : Type\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\ndata Term : Context -> Type -> Set where\n\n int : \u2200 {\u0393} \u2192 (n : \u2124) \u2192 Term \u0393 ints\n bag : \u2200 {\u0393} \u2192 (b : MapBag) \u2192 Term \u0393 bags\n add : \u2200 {\u0393} \u2192 (t\u2081 : Term \u0393 ints) \u2192 (t\u2082 : Term \u0393 ints) \u2192 Term \u0393 ints\n map : \u2200 {\u0393} \u2192 (f : Term \u0393 (ints \u21d2 ints)) \u2192 (b : Term \u0393 bags) \u2192 Term \u0393 bags\n\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n abs : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u03c4\u2081 \u03c4\u2082 \u0393} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081)\n \u2192 Term \u0393 \u03c4\u2082\n\n -- Change to ints = replacement Church pairs\n -- 3 -> 5 ::= \u03bbf. f 3 5\n\ninfix 8 _\u227c_\n\ndata _\u227c_ : (\u0393\u2081 \u0393\u2082 : Context) \u2192 Set where\n \u2205\u227c\u2205 : \u2205 \u227c \u2205\n keep_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u03c4 \u2022 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n drop_\u2022_ : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 (\u03c4 : Type) \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u0393\u2081 \u227c \u03c4 \u2022 \u0393\u2082\n\nweakenVar : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 Var \u0393\u2081 \u03c4 \u2192 Var \u0393\u2082 \u03c4\nweakenVar \u2205\u227c\u2205 x = x\nweakenVar (keep \u03c4\u2081 \u2022 subctx) this = this\nweakenVar (keep \u03c4\u2081 \u2022 subctx) (that y) = that (weakenVar subctx y)\nweakenVar (drop \u03c4\u2081 \u2022 subctx) x = that (weakenVar subctx x)\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 (subctx : \u0393\u2081 \u227c \u0393\u2082) \u2192 Term \u0393\u2081 \u03c4 \u2192 Term \u0393\u2082 \u03c4\nweaken subctx (abs {\u03c4} t) = abs (weaken (keep \u03c4 \u2022 subctx) t)\nweaken subctx (app t\u2081 t\u2082) = app (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (var x) = var (weakenVar subctx x)\nweaken subctx (int x) = int x\nweaken subctx (bag b) = bag b\nweaken subctx (add t\u2081 t\u2082) = add (weaken subctx t\u2081) (weaken subctx t\u2082)\nweaken subctx (map f b) = map (weaken subctx f) (weaken subctx b)\n\nrecord Meaning (Syntax : Set) {\u2113 : Level.Level} : Set (Level.suc \u2113) where\n constructor\n meaning\n field\n {Semantics} : Set \u2113\n \u27e8_\u27e9\u27e6_\u27e7 : Syntax \u2192 Semantics\n\nopen Meaning {{...}} public\n renaming (\u27e8_\u27e9\u27e6_\u27e7 to \u27e6_\u27e7)\n\n\u27e6_\u27e7Type : Type -> Set\n\u27e6 ints \u27e7Type = \u2124\n\u27e6 bags \u27e7Type = MapBag\n\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7Type = \u27e6 \u03c4\u2081 \u27e7Type \u2192 \u27e6 \u03c4\u2082 \u27e7Type\n\nmeaningOfType : Meaning Type\nmeaningOfType = meaning \u27e6_\u27e7Type\n\ndata EmptySet : Set where\n \u2205 : EmptySet\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\n\u27e6_\u27e7Context : Context \u2192 Set\n\u27e6 \u2205 \u27e7Context = EmptySet\n\u27e6 \u03c4 \u2022 \u0393 \u27e7Context = Bind \u27e6 \u03c4 \u27e7 \u27e6 \u0393 \u27e7Context\n\nmeaningOfContext : Meaning Context\nmeaningOfContext = meaning \u27e6_\u27e7Context\n\n\u27e6_\u27e7Var : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 this \u27e7Var (v \u2022 \u03c1) = v\n\u27e6 that x \u27e7Var (v \u2022 \u03c1) = \u27e6 x \u27e7Var \u03c1\n\nmeaningOfVar : \u2200 {\u0393 \u03c4} \u2192 Meaning (Var \u0393 \u03c4)\nmeaningOfVar = meaning \u27e6_\u27e7Var\n\n\u27e6_\u27e7\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 \u0393\u2081 \u227c \u0393\u2082 \u2192 \u27e6 \u0393\u2082 \u27e7 \u2192 \u27e6 \u0393\u2081 \u27e7\n\u27e6 \u2205\u227c\u2205 \u27e7\u227c \u2205 = \u2205\n\u27e6 keep \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = v \u2022 \u27e6 subctx \u27e7\u227c \u03c1\n\u27e6 drop \u03c4 \u2022 subctx \u27e7\u227c (v \u2022 \u03c1) = \u27e6 subctx \u27e7\u227c \u03c1\n\nmeaningOf\u227c : \u2200 {\u0393\u2081 \u0393\u2082} \u2192 Meaning (\u0393\u2081 \u227c \u0393\u2082)\nmeaningOf\u227c = meaning \u27e6_\u27e7\u227c\n\nweakenVar-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} (subctx : \u0393\u2081 \u227c \u0393\u2082) (x : Var \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weakenVar subctx x \u27e7 \u03c1 \u2261 \u27e6 x \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweakenVar-sound \u2205\u227c\u2205 () \u03c1\nweakenVar-sound (keep \u03c4 \u2022 subctx) this (v \u2022 \u03c1) = refl\nweakenVar-sound (keep \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx x \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) this (v \u2022 \u03c1) =\n weakenVar-sound subctx this \u03c1\nweakenVar-sound (drop \u03c4 \u2022 subctx) (that x) (v \u2022 \u03c1) =\n weakenVar-sound subctx (that x) \u03c1\n\n\u27e6_\u27e7Term : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n\u27e6 abs t \u27e7Term \u03c1 = \u03bb v \u2192 \u27e6 t \u27e7Term (v \u2022 \u03c1)\n\u27e6 app t\u2081 t\u2082 \u27e7Term \u03c1 = (\u27e6 t\u2081 \u27e7Term \u03c1) (\u27e6 t\u2082 \u27e7Term \u03c1)\n\u27e6 var x \u27e7Term \u03c1 = \u27e6 x \u27e7 \u03c1\n\u27e6 int n \u27e7Term \u03c1 = n\n\u27e6 bag b \u27e7Term \u03c1 = b\n\u27e6 add m n \u27e7Term \u03c1 = \u27e6 m \u27e7Term \u03c1 + \u27e6 n \u27e7Term \u03c1\n\u27e6 map f b \u27e7Term \u03c1 = mapValues (\u27e6 f \u27e7Term \u03c1) (\u27e6 b \u27e7Term \u03c1)\n\nmeaningOfTerm : \u2200 {\u0393 \u03c4} \u2192 Meaning (Term \u0393 \u03c4)\nmeaningOfTerm = meaning \u27e6_\u27e7Term\n\n_\u27e8$\u27e9_ : \u2200 {\u03c4\u2081 \u03c4\u2082} {v\u2081 v\u2082 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7} {v\u2083 v\u2084 : \u27e6 \u03c4\u2081 \u27e7} \u2192\n v\u2081 \u2261 v\u2082 \u2192 v\u2083 \u2261 v\u2084 \u2192 v\u2081 v\u2083 \u2261 v\u2082 v\u2084\n_\u27e8$\u27e9_ = cong\u2082 (\u03bb x y \u2192 x y)\ninfix 0 _\u27e8$\u27e9_ -- infix 0 $ in Haskell\n\nweaken-sound : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} {subctx : \u0393\u2081 \u227c \u0393\u2082} (t : Term \u0393\u2081 \u03c4) \u2192\n \u2200 (\u03c1 : \u27e6 \u0393\u2082 \u27e7) \u2192 \u27e6 weaken subctx t \u27e7 \u03c1 \u2261 \u27e6 t \u27e7 (\u27e6 subctx \u27e7 \u03c1)\nweaken-sound (abs t) \u03c1 = extensionality (\u03bb v \u2192 weaken-sound t (v \u2022 \u03c1))\nweaken-sound (app t\u2081 t\u2082) \u03c1 = weaken-sound t\u2081 \u03c1 \u27e8$\u27e9 weaken-sound t\u2082 \u03c1\nweaken-sound {subctx = subctx} (var x) \u03c1 = weakenVar-sound subctx x \u03c1\nweaken-sound (int n) \u03c1 = refl\n\nweaken-sound (bag b) \u03c1 = refl\nweaken-sound (add m n) \u03c1 = cong\u2082 _+_ (weaken-sound m \u03c1) (weaken-sound n \u03c1)\nweaken-sound (map f b) \u03c1 = cong\u2082 mapValues (weaken-sound f \u03c1)\n (weaken-sound b \u03c1)\n\n-- Changes to \u2124 are replacement Church pairs. The only arguments\n-- of conern are `fst` and `snd`, so the Church pairs don't have\n-- to be polymorphic.\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type ints = (ints \u21d2 ints \u21d2 ints) \u21d2 ints\n\u0394-Type bags = bags\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\n-- It is clear that \u27e6\u229d\u27e7 exists on the semantic level:\n-- there exists an Agda value to describe the change between any\n-- two Agda values denoted by terms. If we have (not dependently-\n-- typed) arrays, no term denotes the change between two arrays\n-- of different lengths. Thus no full abstraction.\n\n\u27e6derive\u27e7 : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n_\u27e6\u2295\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7\n_\u27e6\u229d\u27e7_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7\n\ninfixl 6 _\u27e6\u2295\u27e7_\ninfixl 6 _\u27e6\u229d\u27e7_\n\n\u27e6fst\u27e7 : \u2124 \u2192 \u2124 \u2192 \u2124\n\u27e6snd\u27e7 : \u2124 \u2192 \u2124 \u2192 \u2124\n\n\u27e6derive\u27e7 {ints} n = \u03bb f \u2192 f n n\n\u27e6derive\u27e7 {bags} b = \u2205\n\u27e6derive\u27e7 {\u03c4\u2081 \u21d2 \u03c4\u2082} f = \u03bb v dv \u2192 f (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f v\n\n_\u27e6\u229d\u27e7_ {ints} m n = \u03bb f \u2192 f n m\n_\u27e6\u229d\u27e7_ {bags} b\u2081 b\u2082 = {!!}\n_\u27e6\u229d\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f\u2081 f\u2082 = \u03bb v dv \u2192 f\u2081 (v \u27e6\u2295\u27e7 dv) \u27e6\u229d\u27e7 f\u2082 v\n-- m \u27e6\u229d\u27e7 n ::= replace n by m\n\n_\u27e6\u2295\u27e7_ {ints} n dn = dn \u27e6snd\u27e7\n_\u27e6\u2295\u27e7_ {bags} b db = {!!}\n_\u27e6\u2295\u27e7_ {\u03c4\u2081 \u21d2 \u03c4\u2082} f df = \u03bb v \u2192 f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n\n\u27e6fst\u27e7 m n = m\n\u27e6snd\u27e7 m n = n\n\n-- Cool lemmas\n\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u229d\u27e7 f \u2261 \u27e6derive\u27e7 f\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {ints} f = refl\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {bags} b = {!!}\nf\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = refl\n\nf\u2295[g\u229df]=g : \u2200 {\u03c4 : Type} (f g : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) \u2261 g\n\nf\u2295\u0394f=f : \u2200 {\u03c4 : Type} (f : \u27e6 \u03c4 \u27e7) \u2192 f \u27e6\u2295\u27e7 (\u27e6derive\u27e7 f) \u2261 f\n\nf\u2295[g\u229df]=g {ints} m n = refl\nf\u2295[g\u229df]=g {bags} b\u2081 b\u2082 = {!!}\nf\u2295[g\u229df]=g {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = f x} {y = f x} refl\n (cong\u2082 _\u27e6\u229d\u27e7_ (cong g (f\u2295\u0394f=f x)) refl) \u27e9\n f x \u27e6\u2295\u27e7 (g x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (g x) \u27e9\n g x\n \u220e\n ) where open \u2261-Reasoning\n\nf\u2295\u0394f=f {ints} n = refl\nf\u2295\u0394f=f {bags} b = {!!}\nf\u2295\u0394f=f {\u03c4\u2081 \u21d2 \u03c4\u2082} f = extensionality (\u03bb x \u2192\n begin\n (f \u27e6\u2295\u27e7 \u27e6derive\u27e7 f) x\n \u2261\u27e8 refl \u27e9\n f x \u27e6\u2295\u27e7 (f (x \u27e6\u2295\u27e7 \u27e6derive\u27e7 x) \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 cong (\u03bb hole \u2192 f x \u27e6\u2295\u27e7 (f hole \u27e6\u229d\u27e7 f x)) (f\u2295\u0394f=f x) \u27e9\n f x \u27e6\u2295\u27e7 (f x \u27e6\u229d\u27e7 f x)\n \u2261\u27e8 f\u2295[g\u229df]=g (f x) (f x) \u27e9\n f x\n \u220e\n ) where open \u2261-Reasoning\n\nvalid-\u0394 : {\u03c4 : Type} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u0394-Type \u03c4 \u27e7 \u2192 Set\nvalid-\u0394 {ints} n dn = n \u2261 dn \u27e6fst\u27e7\nvalid-\u0394 {bags} b db = {!!}\nvalid-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082} f df =\n \u2200 (s : \u27e6 \u03c4\u2081 \u27e7) (ds : \u27e6 \u0394-Type \u03c4\u2081 \u27e7) (R[s,ds] : valid-\u0394 s ds) \u2192\n valid-\u0394 (f s) (df s ds) \u00d7 -- (valid-\u0394:1)\n (f \u27e6\u2295\u27e7 df) (s \u27e6\u2295\u27e7 ds) \u2261 f s \u27e6\u2295\u27e7 df s ds -- (valid-\u0394:2)\n\nR[f,g\u229df] : \u2200 {\u03c4} (f g : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (g \u27e6\u229d\u27e7 f)\nR[f,g\u229df] {ints} m n = refl\nR[f,g\u229df] {bags} b\u2081 b\u2082 = {!!}\nR[f,g\u229df] {\u03c4\u2081 \u21d2 \u03c4\u2082} f g = \u03bb x dx R[x,dx] \u2192\n R[f,g\u229df] {\u03c4\u2082} (f x) (g (x \u27e6\u2295\u27e7 dx)) -- (valid-\u0394:1)\n , -- tuple constructor\n (begin\n (f \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f)) (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 refl \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7\n (g ((x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 \u27e6derive\u27e7 (x \u27e6\u2295\u27e7 dx)) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 cong (\u03bb hole \u2192 f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g hole \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx)))\n (f\u2295\u0394f=f (x \u27e6\u2295\u27e7 dx)) \u27e9\n f (x \u27e6\u2295\u27e7 dx) \u27e6\u2295\u27e7 (g (x \u27e6\u2295\u27e7 dx) \u27e6\u229d\u27e7 f (x \u27e6\u2295\u27e7 dx))\n \u2261\u27e8 f\u2295[g\u229df]=g (f (x \u27e6\u2295\u27e7 dx)) (g (x \u27e6\u2295\u27e7 dx)) \u27e9\n g (x \u27e6\u2295\u27e7 dx)\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (f x) (g (x \u27e6\u2295\u27e7 dx))) \u27e9\n f x \u27e6\u2295\u27e7 (g \u27e6\u229d\u27e7 f) x dx\n \u220e)\n where open \u2261-Reasoning\n\nR[f,\u0394f] : \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 {\u03c4} f (\u27e6derive\u27e7 f)\nR[f,\u0394f] f rewrite sym (f\u27e6\u229d\u27e7f=\u27e6deriv\u27e7f f) = R[f,g\u229df] f f\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u0394-Type \u03c4 \u2022 \u03c4 \u2022 \u0394-Context \u0393 -- push \u03c4, then push \u0394\u03c4.\n\n\u0393\u227c\u0394\u0393 : \u2200 {\u0393} \u2192 \u0393 \u227c \u0394-Context \u0393\n\u0393\u227c\u0394\u0393 {\u2205} = \u2205\u227c\u2205\n\u0393\u227c\u0394\u0393 {\u03c4 \u2022 \u0393} = drop \u0394-Type \u03c4 \u2022 (keep \u03c4 \u2022 \u0393\u227c\u0394\u0393)\n\n-- Data type to hold proofs that environments are valid\ndata Valid-\u0394env : {\u0393 : Context} (\u03c1 : \u27e6 \u0393 \u27e7) (\u0394\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is its own valid \u0394-environment.\n \u03c1=\u2205 : Valid-\u0394env {\u2205} \u2205 \u2205\n -- Induction case: the change introduced therein should be valid,\n -- and the smaller \u0394-environment should be valid as well.\n \u03c1=v\u2022\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {\u03c1\u2080 : \u27e6 \u0393\u2080 \u27e7} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Valid-\u0394env \u03c1\u2080 d\u03c1\u2080 \u2192\n Valid-\u0394env {\u03c4 \u2022 \u0393\u2080} (v \u2022 \u03c1\u2080) (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- Data type to hold proofs that a \u0394-env is consistent with self\ndata Consistent-\u0394env : {\u0393 : Context} (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 Set\n where\n -- Base case: the empty environment is consistent with itself\n d\u03c1=\u2205 : Consistent-\u0394env {\u2205} \u2205\n -- Induction case: the change introduced at top level\n -- should be valid.\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 : \u2200 {\u03c4 : Type} {v : \u27e6 \u03c4 \u27e7} {dv : \u27e6 \u0394-Type \u03c4 \u27e7}\n {\u0393\u2080 : Context} {d\u03c1\u2080 : \u27e6 \u0394-Context \u0393\u2080 \u27e7} \u2192\n valid-\u0394 v dv \u2192 Consistent-\u0394env d\u03c1\u2080 \u2192\n Consistent-\u0394env {\u03c4 \u2022 \u0393\u2080} (dv \u2022 v \u2022 d\u03c1\u2080)\n\n-- If a \u0394-environment is valid for some other environment,\n-- then it is also consistent with itself.\n\nvalid-\u0394env-is-consistent :\n \u2200 {\u0393 : Context} {\u03c1 : \u27e6 \u0393 \u27e7} {d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7} \u2192\n Valid-\u0394env \u03c1 d\u03c1 \u2192 Consistent-\u0394env d\u03c1\n\nvalid-\u0394env-is-consistent \u03c1=\u2205 = d\u03c1=\u2205\nvalid-\u0394env-is-consistent (\u03c1=v\u2022\u03c1\u2080 valid[v,dv] valid[\u03c1\u2080,d\u03c1\u2080]) =\n d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] (valid-\u0394env-is-consistent valid[\u03c1\u2080,d\u03c1\u2080])\n\n-- finally, update and ignore\n\nupdate : \u2200 {\u0393} \u2192 (d\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) \u2192 {_ : Consistent-\u0394env d\u03c1} \u2192 \u27e6 \u0393 \u27e7\nupdate {\u2205} \u2205 {d\u03c1=\u2205} = \u2205\nupdate {\u03c4 \u2022 \u0393} (dv \u2022 v \u2022 d\u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 valid[v,dv] consistent[d\u03c1\u2080]} =\n (v \u27e6\u2295\u27e7 dv) \u2022 update d\u03c1 {consistent[d\u03c1\u2080]}\n\n-- Ignorance is bliss (don't have to pass consistency proofs around :D)\n\nignore : \u2200 {\u0393} \u2192 \u27e6 \u0394-Context \u0393 \u27e7 \u2192 \u27e6 \u0393 \u27e7\nignore = \u27e6 \u0393\u227c\u0394\u0393 \u27e7 -- Using a proof to describe computation\n\n-- Naming scheme follows weakenVar\/weaken\n\nderiveVar : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nderiveVar this = this\nderiveVar (that x) = that (that (deriveVar x))\n\nderive : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Term (\u0394-Context \u0393) (\u0394-Type \u03c4)\n\n-- derive(n) = \u03bbf. f n n\nderive (int n) = abs (app (app (var this) (int n)) (int n))\n\n-- derive(x) = dx\nderive (var x) = var (deriveVar x)\n\n-- derive(\u03bbx. t) = \u03bbx. \u03bbdx. derive(t)\nderive (abs t) = abs (abs (derive t))\n\n-- derive(f s) = derive(f) s derive(s)\nderive (app f s) = app (app (derive f) (weaken \u0393\u227c\u0394\u0393 s)) (derive s)\n\nderive _ = {!!}\n\n-- Extensional equivalence for changes\ndata Extensionally-equivalent-as-changes :\n \u2200 (\u03c4 : Type) \u2192 (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 (dg : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192 Set where\n ext-\u0394 : \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n (\u2200 (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192\n (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)) \u2192\n Extensionally-equivalent-as-changes \u03c4 df dg\n\nsyntax Extensionally-equivalent-as-changes \u03c4 df dg = df \u2248 dg :\u0394 \u03c4\n\n-- Question: How to declare fixity for infix syntax?\n-- infix 4 _\u2248_:\u0394_ -- same as \u2261\n\n-- Extractor for extensional-equivalence-as-changes:\n-- Given a value of the data type holding the proof,\n-- returns the proof in applicable form.\n--\n-- It would not be necessary if such\n-- proof-holding types were defined as a function in\n-- the first place, say in the manner of `valid-\u0394`.\n--\nextract-\u0394equiv :\n \u2200 {\u03c4 : Type} {df dg : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n df \u2248 dg :\u0394 \u03c4 \u2192\n (f : \u27e6 \u03c4 \u27e7) \u2192 valid-\u0394 f df \u2192 valid-\u0394 f dg \u2192 (f \u27e6\u2295\u27e7 df) \u2261 (f \u27e6\u2295\u27e7 dg)\n\nextract-\u0394equiv (ext-\u0394 proof-method) = proof-method\n\n-- Distribution lemmas of validity over \u27e6\u229d\u27e7 and \u27e6\u2295\u27e7\n\napplication-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v) \u2261 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\napplication-over-\u2295-and-\u229d f g df v =\n begin\n f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v\n \u2261\u27e8 cong\u2082 _\u27e6\u229d\u27e7_\n (cong\u2082 _\u27e6\u2295\u27e7_\n (cong f ((sym (f\u2295\u0394f=f v))))\n (cong\u2082 df\n (sym (f\u2295\u0394f=f v))\n (cong \u27e6derive\u27e7 (sym (f\u2295\u0394f=f v)))))\n refl \u27e9\n f (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v)\n \u27e6\u2295\u27e7 df (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v) (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 \u27e6derive\u27e7 v))\n \u27e6\u229d\u27e7 f v\n \u2261\u27e8 refl \u27e9\n (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e where open \u2261-Reasoning\n\nvalidity-over-\u2295-and-\u229d :\n \u2200 {\u03c4\u2081 \u03c4\u2082 : Type}\n (f g : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7) (df : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7) (v : \u27e6 \u03c4\u2081 \u27e7) \u2192\n valid-\u0394 g (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) \u2192\n valid-\u0394 (g v) (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\nvalidity-over-\u2295-and-\u229d f g df v R[g,f\u2295df]\n rewrite application-over-\u2295-and-\u229d f g df v =\n proj\u2081 (R[g,f\u2295df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))\n\n-- diff-apply\ndf=f\u2295df\u229df :\n \u2200 {\u03c4} (f : \u27e6 \u03c4 \u27e7) (df : \u27e6 \u0394-Type \u03c4 \u27e7) \u2192\n valid-\u0394 f df \u2192 df \u2248 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) :\u0394 \u03c4\n\n-- Case int: this REFL is more obvious to Agda than to a human.\ndf=f\u2295df\u229df {ints} n dn valid-n-dn =\n ext-\u0394 (\u03bb m valid-m-dn valid-rhs \u2192 refl)\n\ndf=f\u2295df\u229df {bags} b db valid-b-db = {!!}\n\ndf=f\u2295df\u229df {\u03c4\u2081 \u21d2 \u03c4\u2082} f df R[f,df] = ext-\u0394 (\n \u03bb g R[g,df] R[g,f\u2295df\u229df] \u2192\n extensionality (\u03bb v \u2192\n begin\n g v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v)\n \u2261\u27e8 extract-\u0394equiv\n (df=f\u2295df\u229df\n (f v) (df v (\u27e6derive\u27e7 v))\n (proj\u2081 (R[f,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v))))\n (g v)\n (proj\u2081 (R[g,df] v (\u27e6derive\u27e7 v) (R[f,\u0394f] v)))\n (validity-over-\u2295-and-\u229d f g df v R[g,f\u2295df\u229df]) \u27e9\n g v \u27e6\u2295\u27e7 (f v \u27e6\u2295\u27e7 df v (\u27e6derive\u27e7 v) \u27e6\u229d\u27e7 f v)\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = g v} {y = g v} refl\n (application-over-\u2295-and-\u229d f g df v) \u27e9\n g v \u27e6\u2295\u27e7 (f \u27e6\u2295\u27e7 df \u27e6\u229d\u27e7 f) v (\u27e6derive\u27e7 v)\n \u220e\n )\n )\n where open \u2261-Reasoning\n\n\n\ncorrectness-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n (\u27e6 deriveVar x \u27e7 \u03c1)\n \u2248\n (\u27e6 x \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 x \u27e7 (ignore \u03c1)) :\u0394 \u03c4\n\ncorrectness-of-deriveVar {\u03c4 \u2022 \u0393\u2080} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n this = df=f\u2295df\u229df {\u03c4} v dv R[v,dv]\n\ncorrectness-of-deriveVar {\u03c4\u2080 \u2022 \u0393\u2080} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 {.\u03c4\u2080} {.v} {.dv} {.\u0393\u2080} {.\u03c1} R[v,dv] _}\n (that x) = correctness-of-deriveVar \u03c1 x\n\n\n\ncorrectness-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n\n (\u27e6 derive t \u27e7 \u03c1)\n \u2248 (\u27e6 t \u27e7 (update \u03c1 {consistency}) \u27e6\u229d\u27e7 \u27e6 t \u27e7 (ignore \u03c1)) :\u0394 \u03c4\n\n-- Mutually recursive lemma: derivatives are valid\nvalidity-of-derive : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (t : Term \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive t \u27e7 \u03c1)\n\nvalidity-of-deriveVar : \u2200 {\u0393 \u03c4} \u2192\n \u2200 (\u03c1 : \u27e6 \u0394-Context \u0393 \u27e7) {consistency : Consistent-\u0394env \u03c1} \u2192\n \u2200 (x : Var \u0393 \u03c4) \u2192\n valid-\u0394 (\u27e6 x \u27e7 (ignore \u03c1)) (\u27e6 deriveVar x \u27e7 \u03c1)\n\nvalidity-of-deriveVar {\u03c4 \u2022 \u0393} {.\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n this = R[v,dv]\n\nvalidity-of-deriveVar {\u03c4\u2080 \u2022 \u0393} {\u03c4}\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n (that x) = validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 {consistency} (var x) =\n validity-of-deriveVar \u03c1 {consistency} x\n\nvalidity-of-derive \u03c1 (int n) = refl\n\nvalidity-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs t) = \u03bb v dv R[v,dv] \u2192\n validity-of-derive (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency} t\n , -- tuple constructor\n (begin\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n \u27e6 derive t \u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv) \u2022 (v \u27e6\u2295\u27e7 dv) \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] (v \u27e6\u2295\u27e7 dv)) consistency}\n t)\n ((R[f,g\u229df] (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 f\u2295[g\u229df]=g (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv \u27e6\u2295\u27e7 (\u27e6derive\u27e7 (v \u27e6\u2295\u27e7 dv))) \u2022\n update \u03c1 {consistency})\n \u2261\u27e8 cong \u27e6 t \u27e7 (cong\u2082 _\u2022_ (f\u2295\u0394f=f (v \u27e6\u2295\u27e7 dv)) refl) \u27e9\n \u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u2261\u27e8 sym (f\u2295[g\u229df]=g (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency}))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})\n \u27e6\u229d\u27e7\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u2261\u27e8 sym (extract-\u0394equiv\n (correctness-of-derive\n ((dv \u2022 v \u2022 \u03c1))\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (validity-of-derive\n (dv \u2022 v \u2022 \u03c1) {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 R[v,dv] consistency}\n t)\n (R[f,g\u229df] (\u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n (\u27e6 t \u27e7 ((v \u27e6\u2295\u27e7 dv) \u2022 update \u03c1 {consistency})))) \u27e9\n \u27e6 t \u27e7 (v \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (dv \u2022 v \u2022 \u03c1)\n \u220e)\n where open \u2261-Reasoning\n\nvalidity-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} t\u2081 t\u2082)\n = R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7]\n where\n open \u2261-Reasoning\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 : \u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1) \u2261 v\u2081 v\u2082\n \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 rewrite (sym v\u2082=old-v\u2082) = refl\n\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 : \u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1 \u2261 dv\u2081 v\u2082 dv\u2082\n \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = refl\n\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] : valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (dv\u2081 v\u2082 dv\u2082)\n R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082] rewrite \u27e6t\u2081t\u2082\u27e7=v\u2081v\u2082 = R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n -- What I want to write:\n {-\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] rewrite \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 = R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n -}\n\n -- What I have to write:\n\n R : {\u03c4 : Type} \u2192 {v : \u27e6 \u03c4 \u27e7} \u2192 {dv\u2081 dv\u2082 : \u27e6 \u0394-Type \u03c4 \u27e7} \u2192\n dv\u2081 \u2261 dv\u2082 \u2192 valid-\u0394 v dv\u2081 \u2192 valid-\u0394 v dv\u2082\n\n R {ints} dv\u2081=dv\u2082 refl = cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl\n\n R {bags} dv\u2081=dv\u2082 _ = {!!}\n\n --R {\u03c4\u2081 \u21d2 \u03c4\u2082} dv\u2081=dv\u2082 valid-dv\u2081 rewrite dv\u2081=dv\u2082 = {!valid-dv\u2081!}\n R {\u03c4\u2081 \u21d2 \u03c4\u2082} {v} {dv\u2081} {dv\u2082} dv\u2081=dv\u2082 valid-dv\u2081 =\n \u03bb s ds R[s,ds] \u2192\n R {\u03c4\u2082} {v s} {dv\u2081 s ds} {dv\u2082 s ds}\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl)\n (proj\u2081 (valid-dv\u2081 s ds R[s,ds]))\n ,\n (begin\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2082 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v (s \u27e6\u2295\u27e7 ds)} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) (sym dv\u2081=dv\u2082) refl) refl) \u27e9\n v (s \u27e6\u2295\u27e7 ds) \u27e6\u2295\u27e7 dv\u2081 (s \u27e6\u2295\u27e7 ds) (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 sym (proj\u2082 (valid-dv\u2081\n (s \u27e6\u2295\u27e7 ds)\n (\u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n (R[f,\u0394f] (s \u27e6\u2295\u27e7 ds)))) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds \u27e6\u2295\u27e7 \u27e6derive\u27e7 (s \u27e6\u2295\u27e7 ds))\n \u2261\u27e8 cong (v \u27e6\u2295\u27e7 dv\u2081) (f\u2295\u0394f=f (s \u27e6\u2295\u27e7 ds)) \u27e9\n (v \u27e6\u2295\u27e7 dv\u2081) (s \u27e6\u2295\u27e7 ds)\n \u2261\u27e8 proj\u2082 (valid-dv\u2081 s ds R[s,ds]) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2081 s ds\n \u2261\u27e8 cong\u2082 _\u27e6\u2295\u27e7_\n {x = v s} refl\n (cong\u2082 (\u03bb f x \u2192 f x)\n (cong\u2082 (\u03bb f x \u2192 f x) dv\u2081=dv\u2082 refl) refl) \u27e9\n v s \u27e6\u2295\u27e7 dv\u2082 s ds\n \u220e) where open \u2261-Reasoning\n\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] :\n valid-\u0394 (\u27e6 app t\u2081 t\u2082 \u27e7 (ignore \u03c1)) (\u27e6 derive (app t\u2081 t\u2082) \u27e7 \u03c1)\n R[\u27e6t\u2081t\u2082\u27e7,\u27e6\u0394[t\u2081t\u2082]\u27e7] = R \u27e6\u0394[t\u2081t\u2082]\u27e7=dv\u2081v\u2082dv\u2082 R[\u27e6t\u2081t\u2082\u27e7,dv\u2081v\u2082dv\u2082]\n\nvalidity-of-derive \u03c1 {consistency} _ = {!!}\n\ncorrectness-of-derive \u03c1 (var x) = correctness-of-deriveVar \u03c1 x\n\ncorrectness-of-derive \u03c1 (int n) = ext-\u0394 (\u03bb _ _ _ \u2192 refl)\n\ncorrectness-of-derive {\u0393} {\u03c4\u2081 \u21d2 \u03c4\u2082}\n \u03c1 {consistency} (abs {.\u03c4\u2081} {.\u03c4\u2082} t) =\n ext-\u0394 {\u03c4\u2081 \u21d2 \u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n extensionality {\u27e6 \u03c4\u2081 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4\u2082 \u27e7} (\u03bb x \u2192\n begin\n f x \u27e6\u2295\u27e7 \u27e6 derive t \u27e7 (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive {\u03c4\u2081 \u2022 \u0393} {\u03c4\u2082}\n (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency}\n t)\n (f x)\n (proj\u2081 (R[f,\u0394t] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x)))\n (proj\u2081 (R[f,t\u2032\u229dt] x (\u27e6derive\u27e7 x) (R[f,\u0394f] x))) \u27e9\n f x\n \u27e6\u2295\u27e7\n (\u27e6 t \u27e7 (update (\u27e6derive\u27e7 x \u2022 x \u2022 \u03c1)\n {d\u03c1=dv\u2022v\u2022d\u03c1\u2080 (R[f,\u0394f] x) consistency})\n \u27e6\u229d\u27e7 \u27e6 t \u27e7 (x \u2022 \u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1))\n \u220e\n )) where open \u2261-Reasoning\n\ncorrectness-of-derive \u03c1 {consistency} (app {\u03c4\u2081} {\u03c4\u2082} {\u0393} t\u2081 t\u2082) =\n ext-\u0394 {\u03c4\u2082}\n (\u03bb f R[f,\u0394t] R[f,t\u2032\u229dt] \u2192\n begin\n f \u27e6\u2295\u27e7 \u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1)\n \u2261\u27e8 extract-\u0394equiv ext-\u0394[t\u2081t\u2082] f R[f,\u0394t] R[f,t\u2032\u229dt] \u27e9\n f \u27e6\u2295\u27e7\n (\u27e6 t\u2081 \u27e7 (update \u03c1) (\u27e6 t\u2082 \u27e7 (update \u03c1))\n \u27e6\u229d\u27e7\n \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n \u220e)\n where\n open \u2261-Reasoning\n\n v\u2081 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081 = \u27e6 t\u2081 \u27e7 (ignore \u03c1)\n v\u2082 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082 = \u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1\n\n dv\u2081 : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n dv\u2081 = \u27e6 derive t\u2081 \u27e7 \u03c1\n dv\u2082 : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv\u2082 = \u27e6 derive t\u2082 \u27e7 \u03c1\n\n v\u2081\u2032 : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n v\u2081\u2032 = \u27e6 t\u2081 \u27e7 (update \u03c1 {consistency})\n v\u2082\u2032 : \u27e6 \u03c4\u2081 \u27e7\n v\u2082\u2032 = \u27e6 t\u2082 \u27e7 (update \u03c1 {consistency})\n\n v\u2082=old-v\u2082 : v\u2082 \u2261 \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n v\u2082=old-v\u2082 = weaken-sound {subctx = \u0393\u227c\u0394\u0393} t\u2082 \u03c1\n\n valid-dv\u2081 : valid-\u0394 v\u2081 dv\u2081\n valid-dv\u2081 = validity-of-derive \u03c1 {consistency} t\u2081\n \n valid-dv\u2082 : valid-\u0394 v\u2082 dv\u2082\n valid-dv\u2082 rewrite v\u2082=old-v\u2082 =\n validity-of-derive \u03c1 {consistency} t\u2082\n\n v\u2081\u2295dv\u2081=v\u2081\u2032 : v\u2081 \u27e6\u2295\u27e7 dv\u2081 \u2261 v\u2081\u2032\n v\u2081\u2295dv\u2081=v\u2081\u2032 =\n begin\n v\u2081 \u27e6\u2295\u27e7 dv\u2081\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2081)\n v\u2081 valid-dv\u2081 (R[f,g\u229df] v\u2081 v\u2081\u2032) \u27e9\n v\u2081 \u27e6\u2295\u27e7 (v\u2081\u2032 \u27e6\u229d\u27e7 v\u2081)\n \u2261\u27e8 f\u2295[g\u229df]=g v\u2081 v\u2081\u2032 \u27e9\n v\u2081\u2032\n \u220e\n\n -- TODO: remove code duplication.\n v\u2082\u2295dv\u2082=v\u2082\u2032 : v\u2082 \u27e6\u2295\u27e7 dv\u2082 \u2261 v\u2082\u2032\n v\u2082\u2295dv\u2082=v\u2082\u2032 rewrite v\u2082=old-v\u2082 =\n begin\n old-v\u2082 \u27e6\u2295\u27e7 dv\u2082\n \u2261\u27e8 extract-\u0394equiv\n (correctness-of-derive \u03c1 {consistency} t\u2082)\n old-v\u2082\n (validity-of-derive \u03c1 {consistency} t\u2082)\n (R[f,g\u229df] old-v\u2082 v\u2082\u2032) \u27e9\n old-v\u2082 \u27e6\u2295\u27e7 (v\u2082\u2032 \u27e6\u229d\u27e7 old-v\u2082)\n \u2261\u27e8 f\u2295[g\u229df]=g old-v\u2082 v\u2082\u2032 \u27e9\n v\u2082\u2032\n \u220e\n where old-v\u2082 = \u27e6 t\u2082 \u27e7 (ignore \u03c1)\n\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] : v\u2081\u2032 v\u2082\u2032 \u2261 (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082)\n v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] = sym (cong\u2082 (\u03bb f x \u2192 f x) v\u2081\u2295dv\u2081=v\u2081\u2032 v\u2082\u2295dv\u2082=v\u2082\u2032)\n\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 :\n (v\u2081 \u27e6\u2295\u27e7 dv\u2081) (v\u2082 \u27e6\u2295\u27e7 dv\u2082) \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082\n [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082 = proj\u2082 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082 \u2261 v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082\n v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n cong\u2082 _\u27e6\u229d\u27e7_\n (trans v\u2081\u2032v\u2082\u2032=[v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082] [v\u2081\u2295dv\u2081][v\u2082\u2295dv\u2082]=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082)\n refl\n\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] : valid-\u0394 (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082)\n R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082] = proj\u2081 (valid-dv\u2081 v\u2082 dv\u2082 valid-dv\u2082)\n\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 :\n (dv\u2081 v\u2082 dv\u2082) \u2248 (v\u2081 v\u2082 \u27e6\u2295\u27e7 dv\u2081 v\u2082 dv\u2082 \u27e6\u229d\u27e7 v\u2081 v\u2082) :\u0394 \u03c4\u2082\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n df=f\u2295df\u229df (v\u2081 v\u2082) (dv\u2081 v\u2082 dv\u2082) R[v\u2081v\u2082,dv\u2081v\u2082dv\u2082]\n\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 : (dv\u2081 v\u2082 dv\u2082) \u2248 (v\u2081\u2032 v\u2082\u2032 \u27e6\u229d\u27e7 v\u2081 v\u2082) :\u0394 \u03c4\u2082\n dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082 rewrite v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082 =\n dv\u2081v\u2082dv\u2082=v\u2081v\u2082\u2295dv\u2081v\u2082dv\u2082\u229dv\u2081v\u2082\n\n ext-\u0394[t\u2081t\u2082] :\n (\u27e6 derive t\u2081 \u27e7 \u03c1 (\u27e6 weaken \u0393\u227c\u0394\u0393 t\u2082 \u27e7 \u03c1) (\u27e6 derive t\u2082 \u27e7 \u03c1))\n \u2248\n (\u27e6 t\u2081 \u27e7 (update \u03c1 {consistency}) (\u27e6 t\u2082 \u27e7 (update \u03c1 {consistency}))\n \u27e6\u229d\u27e7 \u27e6 t\u2081 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1) (\u27e6 t\u2082 \u27e7 (\u27e6 \u0393\u227c\u0394\u0393 \u27e7 \u03c1)))\n :\u0394 \u03c4\u2082\n ext-\u0394[t\u2081t\u2082] rewrite sym v\u2082=old-v\u2082 = dv\u2081v\u2082dv\u2082=v\u2081\u2032v\u2082\u2032\u229dv\u2081v\u2082\n\ncorrectness-of-derive \u03c1 {consistency} _ = {!!}\n\ncorrectness-on-closed-terms : \u2200 {\u03c4\u2081 \u03c4\u2082} \u2192\n \u2200 (f : Term \u2205 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192\n \u2200 (s : Term \u2205 \u03c4\u2081) (ds : Term \u2205 (\u0394-Type \u03c4\u2081))\n {R[v,dv] : valid-\u0394 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)} \u2192\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205 \u27e6\u2295\u27e7 \u27e6 ds \u27e7 \u2205)\n \u2261\n \u27e6 f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) \u27e6\u2295\u27e7 \u27e6 derive f \u27e7 \u2205 (\u27e6 s \u27e7 \u2205) (\u27e6 ds \u27e7 \u2205)\n\ncorrectness-on-closed-terms {\u03c4\u2081} {\u03c4\u2082} f s ds {R[v,dv]} =\n begin\n h (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (f\u2295[g\u229df]=g h h))\n refl \u27e9\n (h \u27e6\u2295\u27e7 (h \u27e6\u229d\u27e7 h)) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 cong\u2082 (\u03bb f x \u2192 f x)\n (sym (extract-\u0394equiv\n (correctness-of-derive \u2205 f)\n h\n (validity-of-derive \u2205 {d\u03c1=\u2205} f)\n (R[f,g\u229df] h h)))\n refl \u27e9\n (h \u27e6\u2295\u27e7 \u0394h) (v \u27e6\u2295\u27e7 dv)\n \u2261\u27e8 proj\u2082 (validity-of-derive \u2205 {d\u03c1=\u2205} f v dv R[v,dv]) \u27e9\n h v \u27e6\u2295\u27e7 \u0394h v dv\n \u220e\n where\n open \u2261-Reasoning\n h : \u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7\n h = \u27e6 f \u27e7 \u2205\n \u0394h : \u27e6 \u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) \u27e7\n \u0394h = \u27e6 derive f \u27e7 \u2205\n v : \u27e6 \u03c4\u2081 \u27e7\n v = \u27e6 s \u27e7 \u2205\n dv : \u27e6 \u0394-Type \u03c4\u2081 \u27e7\n dv = \u27e6 ds \u27e7 \u2205\n\n-}\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"49d4d79844d886e9a42816b68ac8c38d2ec28bc1","subject":"Syntax.Type.Plotkin: prove that lift-\u0394type is not a monadic bind.","message":"Syntax.Type.Plotkin: prove that lift-\u0394type is not a monadic bind.\n\nOld-commit-hash: 68f7ee53aed3f5811b2bafa2deef7f8b4349c6b3\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Type\/Plotkin.agda","new_file":"Syntax\/Type\/Plotkin.agda","new_contents":"module Syntax.Type.Plotkin where\n\n-- Types for language description \u00e0 la Plotkin (LCF as PL)\n--\n-- Given base types, we build function types.\n\ninfixr 5 _\u21d2_\n\ndata Type (B : Set {- of base types -}) : Set where\n base : (\u03b9 : B) \u2192 Type B\n _\u21d2_ : (\u03c3 : Type B) \u2192 (\u03c4 : Type B) \u2192 Type B\n\n-- Lift (\u0394 : B \u2192 Type B) to (\u0394type : Type B \u2192 Type B)\n-- according to \u0394 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394 \u03c3 \u21d2 \u0394 \u03c4\nlift-\u0394type : \u2200 {B} \u2192 (B \u2192 Type B) \u2192 (Type B \u2192 Type B)\nlift-\u0394type f (base \u03b9) = f \u03b9\nlift-\u0394type f (\u03c3 \u21d2 \u03c4) = let \u0394 = lift-\u0394type f in \u03c3 \u21d2 \u0394 \u03c3 \u21d2 \u0394 \u03c4\n\n-- Note: the above is *not* a monadic bind.\n--\n-- Proof. base` is the most straightforward `return` of the\n-- functor `Type`.\n--\n-- return : B \u2192 Type B\n-- return = base\n--\n-- Let\n--\n-- m : Type B\n-- m = base \u03ba \u21d2 base \u03b9\n--\n-- then\n--\n-- m >>= return = lift-\u0394type return m\n-- = base \u03ba \u21d2 base \u03ba \u21d2 base \u03b9\n--\n-- violating the second monadic law, m >>= return \u2261 m. \u220e\n\nopen import Function\n\n-- Variant of lift-\u0394type for (\u0394 : B \u2192 B).\nlift-\u0394type\u2080 : \u2200 {B} \u2192 (B \u2192 B) \u2192 (Type B \u2192 Type B)\nlift-\u0394type\u2080 f = lift-\u0394type $ base \u2218 f\n-- This has a similar type to the type of `fmap`,\n-- and `base` has a similar type to the type of `return`.\n--\n-- Similarly, for collections map can be defined from flatMap,\n-- like lift-\u0394type\u2080 can be defined in terms of lift-\u0394type.\n","old_contents":"module Syntax.Type.Plotkin where\n\n-- Types for language description \u00e0 la Plotkin (LCF as PL)\n--\n-- Given base types, we build function types.\n\ninfixr 5 _\u21d2_\n\ndata Type (B : Set {- of base types -}) : Set where\n base : (\u03b9 : B) \u2192 Type B\n _\u21d2_ : (\u03c3 : Type B) \u2192 (\u03c4 : Type B) \u2192 Type B\n\n-- Lift (\u0394 : B \u2192 Type B) to (\u0394type : Type B \u2192 Type B)\n-- according to \u0394 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 \u0394 \u03c3 \u21d2 \u0394 \u03c4\nlift-\u0394type\u2081 : \u2200 {B} \u2192 (B \u2192 Type B) \u2192 (Type B \u2192 Type B)\nlift-\u0394type\u2081 f (base \u03b9) = f \u03b9\nlift-\u0394type\u2081 f (\u03c3 \u21d2 \u03c4) = let \u0394 = lift-\u0394type\u2081 f in \u03c3 \u21d2 \u0394 \u03c3 \u21d2 \u0394 \u03c4\n\n-- Note: the above is monadic bind with a different argument order.\n\nopen import Function\n\n-- Variant of lift\u2081 for (\u0394 : B \u2192 B).\nlift-\u0394type\u2080 : \u2200 {B} \u2192 (B \u2192 B) \u2192 (Type B \u2192 Type B)\nlift-\u0394type\u2080 f = lift-\u0394type\u2081 $ base \u2218 f\n-- If lift\u2081 is a monadic bind, this is fmap,\n-- and base is return.\n--\n-- Similarly, for collections map can be defined from flatMap, like lift\u2080 can be\n-- defined in terms of lift\u2081.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"6c73670338d1e3fca42f15aabc24ef33c2774bd5","subject":"messing around #10","message":"messing around #10\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"commutativity.agda","new_file":"commutativity.agda","new_contents":"open import Nat\nopen import Prelude\nopen import core\n\nmodule commutativity where\n commutativity : \u2200{d0 d1 d u} \u2192\n -- probably need a premise that d0 is well typed\n d0 \u21a6* d1 \u2192\n (\u27e6 d \/ u \u27e7 d0) \u21a6* (\u27e6 d \/ u \u27e7 d1)\n commutativity MSRefl = MSRefl\n commutativity (MSStep x stp) with commutativity stp\n ... | ih = MSStep {!!} {!!}\n","old_contents":"open import Nat\nopen import Prelude\nopen import core\n\nmodule commutativity where\n commutativity : \u2200{d0 d1 d u} \u2192\n -- probably need a premise that d0 is well typed\n d0 \u21a6* d1 \u2192\n (\u27e6 d \/ u \u27e7 d0) \u21a6* (\u27e6 d \/ u \u27e7 d1)\n commutativity = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"5d76e4ef03b0213900ffaeb8869c559255671064","subject":"Desc model: add the map operator","message":"Desc model: add the map operator","repos":"kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram,kwangkim\/pigment","old_file":"models\/Desc.agda","new_file":"models\/Desc.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule Desc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata Desc : Set where\n id : Desc\n const : Set -> Desc\n prod : Desc -> Desc -> Desc\n sigma : (S : Set) -> (S -> Desc) -> Desc\n pi : (S : Set) -> (S -> Desc) -> Desc\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|]_ : Desc -> Set -> Set\n[| id |] Z = Z\n[| const X |] Z = X\n[| prod D D' |] Z = [| D |] Z * [| D' |] Z\n[| sigma S T |] Z = Sigma S (\\s -> [| T s |] Z)\n[| pi S T |] Z = (s : S) -> [| T s |] Z\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata Mu (D : Desc) : Set where\n con : [| D |] (Mu D) -> Mu D\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : (D : Desc)(X : Set)(P : X -> Set) -> [| D |] X -> Set\nAll id X P x = P x\nAll (const Z) X P x = Z\nAll (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')\nAll (sigma S T) X P (a , b) = All (T a) X P b\nAll (pi S T) X P f = (s : S) -> All (T s) X P (f s)\n\nall : (D : Desc)(X : Set)(P : X -> Set)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x\nall id X P R x = R x\nall (const Z) X P R z = z\nall (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'\nall (sigma S T) X P R (a , b) = all (T a) X P R b\nall (pi S T) X P R f = \\ s -> all (T s) X P R (f s)\n\n--********************************************\n-- Map\n--********************************************\n\nmap : (D : Desc)(X Y : Set)(f : X -> Y)(v : [| D |] X) -> [| D |] Y\nmap id X Y sig x = sig x\nmap (const Z) X Y sig z = z\nmap (prod D D') X Y sig (d , d') = map D X Y sig d , map D' X Y sig d'\nmap (sigma S T) X Y sig (a , b) = (a , map (T a) X Y sig b)\nmap (pi S T) X Y sig f = \\x -> map (T x) X Y sig (f x)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n{-\ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms (con xs) = ms xs (all D (Mu D) P (\\x -> induction D P ms x) xs)\n-}\n\nmodule Elim (D : Desc)\n (P : Mu D -> Set)\n (ms : (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x))\n where\n\n mutual\n induction : (x : Mu D) -> P x\n induction (con xs) = ms xs (hyps D xs)\n \n hyps : (D' : Desc)\n (xs : [| D' |] (Mu D)) ->\n All D' (Mu D) P xs\n hyps id x = induction x\n hyps (const Z) z = z\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (sigma S T) (a , b) = hyps (T a) b\n hyps (pi S T) f = \\s -> hyps (T s) (f s)\n \ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms x = Elim.induction D P ms x\n\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Suc : NatConst\n\nnatCases : NatConst -> Desc\nnatCases Ze = const Unit\nnatCases Suc = id\n\nNatD : Desc\nNatD = sigma NatConst natCases\n\nNat : Set\nNat = Mu NatD\n\nze : Nat\nze = con (Ze , Void)\n\nsuc : Nat -> Nat\nsuc n = con (Suc , n)\n\n--****************\n-- List\n--****************\n\ndata ListConst : Set where\n Nil : ListConst\n Cons : ListConst\n\nlistCases : Set -> ListConst -> Desc\nlistCases X Nil = const Unit\nlistCases X Cons = sigma X (\\_ -> id)\n\nListD : Set -> Desc\nListD X = sigma ListConst (listCases X)\n\nList : Set -> Set\nList X = Mu (ListD X)\n\nnil : {X : Set} -> List X\nnil = con ( Nil , Void )\n\ncons : {X : Set} -> X -> List X -> List X\ncons x t = con ( Cons , ( x , t ))\n\n--****************\n-- Tree\n--****************\n\ndata TreeConst : Set where\n Leaf : TreeConst\n Node : TreeConst\n\ntreeCases : Set -> TreeConst -> Desc\ntreeCases X Leaf = const Unit\ntreeCases X Node = sigma X (\\_ -> prod id id)\n\nTreeD : Set -> Desc\nTreeD X = sigma TreeConst (treeCases X)\n\nTree : Set -> Set\nTree X = Mu (TreeD X)\n\nleaf : {X : Set} -> Tree X\nleaf = con (Leaf , Void)\n\nnode : {X : Set} -> X -> Tree X -> Tree X -> Tree X\nnode x le ri = con (Node , (x , (le , ri)))\n\n--********************************************\n-- Finite sets\n--********************************************\n\nEnumU : Set\nEnumU = Nat\n\nnilE : EnumU\nnilE = ze\n\nconsE : EnumU -> EnumU\nconsE e = suc e\n\n{-\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n-}\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\ncasesSpi : (xs : [| NatD |] Nat) -> \n All NatD Nat (\\e -> (P' : EnumT e -> Set) -> Set) xs -> \n (P' : EnumT (con xs) -> Set) -> Set\ncasesSpi (Ze , Void) hs P' = Unit\ncasesSpi (Suc , n) hs P' = P' EZe * hs (\\e -> P' (ESu e))\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi e P = induction NatD (\\e -> (P : EnumT e -> Set) -> Set) casesSpi e P\n\n{-\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n-}\n\n\ncasesSwitch : (xs : [| NatD |] Nat) ->\n All NatD Nat (\\e -> (P' : EnumT e -> Set)(b' : spi e P')(x' : EnumT e) -> P' x') xs ->\n (P' : EnumT (con xs) -> Set)(b' : spi (con xs) P')(x' : EnumT (con xs)) -> P' x'\ncasesSwitch (Ze , Void) hs P' b' ()\ncasesSwitch (Suc , n) hs P' b' EZe = fst b'\ncasesSwitch (Suc , n) hs P' b' (ESu e') = hs (\\e -> P' (ESu e)) (snd b') e'\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch e P b x = induction NatD (\\e -> (P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x) casesSwitch e P b x\n\n{-\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n-}\n\n--********************************************\n-- Tagged description\n--********************************************\n\nTagDesc : Set\nTagDesc = Sigma EnumU (\\e -> spi e (\\_ -> Desc))\n\ntoDesc : TagDesc -> Desc\ntoDesc (B , F) = sigma (EnumT B) (\\e -> switch B (\\_ -> Desc) F e)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (D : Desc)\n (T : Set) ->\n ([| D |] T -> T) ->\n (Mu D) -> T\ncata D T phi x = induction D (\\_ -> T) (\\x ms -> phi (replace D T x ms )) x\n where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\\_ -> T) xs) -> [| D' |] T\n replace id T x y = y\n replace (const Z) T z z' = z'\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : TagDesc -> (X : Set) -> TagDesc\n(e , D) ** X = consE e , (const X , D)\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : (D : TagDesc)(X Y : Set) ->\n (X -> Mu (toDesc (D ** Y))) ->\n [| toDesc (D ** X) |] (Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\napply (E , B) X Y sig (EZe , x) = sig x\napply (E , B) X Y sig (ESu n , t) = con (ESu n , t)\n\nsubst : (D : TagDesc)(X Y : Set) ->\n Mu (toDesc (D ** X)) ->\n (X -> Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\nsubst D X Y x sig = cata (toDesc (D ** X)) (Mu (toDesc (D ** Y))) (apply D X Y sig) x\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule Desc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata Desc : Set where\n id : Desc\n const : Set -> Desc\n prod : Desc -> Desc -> Desc\n sigma : (S : Set) -> (S -> Desc) -> Desc\n pi : (S : Set) -> (S -> Desc) -> Desc\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|]_ : Desc -> Set -> Set\n[| id |] Z = Z\n[| const X |] Z = X\n[| prod D D' |] Z = [| D |] Z * [| D' |] Z\n[| sigma S T |] Z = Sigma S (\\s -> [| T s |] Z)\n[| pi S T |] Z = (s : S) -> [| T s |] Z\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata Mu (D : Desc) : Set where\n con : [| D |] (Mu D) -> Mu D\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : (D : Desc)(X : Set)(P : X -> Set) -> [| D |] X -> Set\nAll id X P x = P x\nAll (const Z) X P x = Z\nAll (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')\nAll (sigma S T) X P (a , b) = All (T a) X P b\nAll (pi S T) X P f = (s : S) -> All (T s) X P (f s)\n\nall : (D : Desc)(X : Set)(P : X -> Set)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x\nall id X P R x = R x\nall (const Z) X P R z = z\nall (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'\nall (sigma S T) X P R (a , b) = all (T a) X P R b\nall (pi S T) X P R f = \\ s -> all (T s) X P R (f s)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n{-\ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms (con xs) = ms xs (all D (Mu D) P (\\x -> induction D P ms x) xs)\n-}\n\nmodule Elim (D : Desc)\n (P : Mu D -> Set)\n (ms : (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x))\n where\n\n mutual\n induction : (x : Mu D) -> P x\n induction (con xs) = ms xs (hyps D xs)\n \n hyps : (D' : Desc)\n (xs : [| D' |] (Mu D)) ->\n All D' (Mu D) P xs\n hyps id x = induction x\n hyps (const Z) z = z\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (sigma S T) (a , b) = hyps (T a) b\n hyps (pi S T) f = \\s -> hyps (T s) (f s)\n \ninduction : (D : Desc) \n (P : Mu D -> Set) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms x = Elim.induction D P ms x\n\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Suc : NatConst\n\nnatCases : NatConst -> Desc\nnatCases Ze = const Unit\nnatCases Suc = id\n\nNatD : Desc\nNatD = sigma NatConst natCases\n\nNat : Set\nNat = Mu NatD\n\nze : Nat\nze = con (Ze , Void)\n\nsuc : Nat -> Nat\nsuc n = con (Suc , n)\n\n--****************\n-- List\n--****************\n\ndata ListConst : Set where\n Nil : ListConst\n Cons : ListConst\n\nlistCases : Set -> ListConst -> Desc\nlistCases X Nil = const Unit\nlistCases X Cons = sigma X (\\_ -> id)\n\nListD : Set -> Desc\nListD X = sigma ListConst (listCases X)\n\nList : Set -> Set\nList X = Mu (ListD X)\n\nnil : {X : Set} -> List X\nnil = con ( Nil , Void )\n\ncons : {X : Set} -> X -> List X -> List X\ncons x t = con ( Cons , ( x , t ))\n\n--****************\n-- Tree\n--****************\n\ndata TreeConst : Set where\n Leaf : TreeConst\n Node : TreeConst\n\ntreeCases : Set -> TreeConst -> Desc\ntreeCases X Leaf = const Unit\ntreeCases X Node = sigma X (\\_ -> prod id id)\n\nTreeD : Set -> Desc\nTreeD X = sigma TreeConst (treeCases X)\n\nTree : Set -> Set\nTree X = Mu (TreeD X)\n\nleaf : {X : Set} -> Tree X\nleaf = con (Leaf , Void)\n\nnode : {X : Set} -> X -> Tree X -> Tree X -> Tree X\nnode x le ri = con (Node , (x , (le , ri)))\n\n--********************************************\n-- Finite sets\n--********************************************\n\nEnumU : Set\nEnumU = Nat\n\nnilE : EnumU\nnilE = ze\n\nconsE : EnumU -> EnumU\nconsE e = suc e\n\n{-\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n-}\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\ncasesSpi : (xs : [| NatD |] Nat) -> \n All NatD Nat (\\e -> (P' : EnumT e -> Set) -> Set) xs -> \n (P' : EnumT (con xs) -> Set) -> Set\ncasesSpi (Ze , Void) hs P' = Unit\ncasesSpi (Suc , n) hs P' = P' EZe * hs (\\e -> P' (ESu e))\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi e P = induction NatD (\\e -> (P : EnumT e -> Set) -> Set) casesSpi e P\n\n{-\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n-}\n\n\ncasesSwitch : (xs : [| NatD |] Nat) ->\n All NatD Nat (\\e -> (P' : EnumT e -> Set)(b' : spi e P')(x' : EnumT e) -> P' x') xs ->\n (P' : EnumT (con xs) -> Set)(b' : spi (con xs) P')(x' : EnumT (con xs)) -> P' x'\ncasesSwitch (Ze , Void) hs P' b' ()\ncasesSwitch (Suc , n) hs P' b' EZe = fst b'\ncasesSwitch (Suc , n) hs P' b' (ESu e') = hs (\\e -> P' (ESu e)) (snd b') e'\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch e P b x = induction NatD (\\e -> (P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x) casesSwitch e P b x\n\n{-\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n-}\n\n--********************************************\n-- Tagged description\n--********************************************\n\nTagDesc : Set\nTagDesc = Sigma EnumU (\\e -> spi e (\\_ -> Desc))\n\ntoDesc : TagDesc -> Desc\ntoDesc (B , F) = sigma (EnumT B) (\\e -> switch B (\\_ -> Desc) F e)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (D : Desc)\n (T : Set) ->\n ([| D |] T -> T) ->\n (Mu D) -> T\ncata D T phi x = induction D (\\_ -> T) (\\x ms -> phi (replace D T x ms )) x\n where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\\_ -> T) xs) -> [| D' |] T\n replace id T x y = y\n replace (const Z) T z z' = z'\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : TagDesc -> (X : Set) -> TagDesc\n(e , D) ** X = consE e , (const X , D)\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : (D : TagDesc)(X Y : Set) ->\n (X -> Mu (toDesc (D ** Y))) ->\n [| toDesc (D ** X) |] (Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\napply (E , B) X Y sig (EZe , x) = sig x\napply (E , B) X Y sig (ESu n , t) = con (ESu n , t)\n\nsubst : (D : TagDesc)(X Y : Set) ->\n Mu (toDesc (D ** X)) ->\n (X -> Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\nsubst D X Y x sig = cata (toDesc (D ** X)) (Mu (toDesc (D ** Y))) (apply D X Y sig) x\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"eafb25706ba502727c7d1859e45b4de324d4e1f6","subject":"fix parens error in Type.Identity","message":"fix parens error in Type.Identity\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Type\/Identities.agda","new_file":"lib\/Type\/Identities.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Type\n\nopen import Level.NP\nopen import HoTT\nopen import Function.NP\nopen import Function.Extensionality\n\nopen import Data.Maybe.NP using (Maybe ; just ; nothing ; maybe ; maybe\u2032 ; just-injective)\nopen import Data.Zero using (\ud835\udfd8 ; \ud835\udfd8-elim)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Two using (\ud835\udfda ; 0\u2082 ; 1\u2082 ; [0:_1:_]; twist)\nopen import Data.Fin as Fin using (Fin ; suc ; zero)\nopen import Data.Nat.NP as \u2115 using (\u2115 ; suc ; zero)\nopen import Data.Product.NP renaming (proj\u2081 to fst; proj\u2082 to snd)\nopen import Data.Sum using (_\u228e_) renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_ ; ap; coe; coe!; !_; _\u2219_; J ; inspect ; Reveal_is_ ; [_]; tr) renaming (refl to idp; cong\u2082 to ap\u2082; _\u2257_ to _\u223c_)\n\nmodule Type.Identities where\n\nopen Equivalences\n\n\n-- for use with ap\u2082 etc.\n_\u27f6_ : \u2200 {a b} \u2192 \u2605_ a \u2192 \u2605_ b \u2192 \u2605_ (b \u2294 a)\nA \u27f6 B = A \u2192 B\n\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 : \ud835\udfda \u2243 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 = equiv [0: inl _ 1: inr _ ]\n [inl: const 0\u2082 ,inr: const 1\u2082 ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n [0: idp 1: idp ]\n\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 : {{_ : UA}} \u2192 \ud835\udfda \u2261 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 = ua \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9\n\nmodule _ {{_ : UA}}{A : \u2605}{B C : A \u2192 \u2605} where\n \u03a3\u228e-split : (\u03a3 A (\u03bb x \u2192 B x \u228e C x)) \u2261 (\u03a3 A B \u228e \u03a3 A C)\n \u03a3\u228e-split = ua (equiv (\u03bb { (x , inl y) \u2192 inl (x , y)\n ; (x , inr y) \u2192 inr (x , y) })\n (\u03bb { (inl (x , y)) \u2192 x , inl y\n ; (inr (x , y)) \u2192 x , inr y })\n (\u03bb { (inl (x , y)) \u2192 idp\n ; (inr (x , y)) \u2192 idp })\n (\u03bb { (x , inl y) \u2192 idp\n ; (x , inr y) \u2192 idp }))\n\nmodule _ {{_ : UA}}{A B : \u2605}{C : A \u228e B \u2192 \u2605} where\n dist-\u228e-\u03a3-equiv : \u03a3 (A \u228e B) C \u2243 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u03a3 : \u03a3 (A \u228e B) C \u2261 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3 = ua dist-\u228e-\u03a3-equiv\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0-equiv : \u03a0 (A \u228e B) C \u2243 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u03a0 : \u03a0 (A \u228e B) C \u2261 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0 = ua dist-\u00d7-\u03a0-equiv\n\nmodule _ {{_ : UA}}{A B : \u2605}{C : A \u2192 \u2605}{D : B \u2192 \u2605} where\n dist-\u228e-\u03a3[] : (\u03a3 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a3 A C \u228e \u03a3 B D)\n dist-\u228e-\u03a3[] = dist-\u228e-\u03a3\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0[] : (\u03a0 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a0 A C \u00d7 \u03a0 B D)\n dist-\u00d7-\u03a0[] = dist-\u00d7-\u03a0\n\nmodule _ {{_ : UA}}{A B C : \u2605} where\n dist-\u228e-\u00d7-equiv : ((A \u228e B) \u00d7 C) \u2243 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u00d7 : ((A \u228e B) \u00d7 C) \u2261 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7 = ua dist-\u228e-\u00d7-equiv\n\n module _ {{_ : FunExt}} where\n dist-\u00d7-\u2192-equiv : ((A \u228e B) \u2192 C ) \u2243 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u2192 : ((A \u228e B) \u2192 C) \u2261 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192 = ua dist-\u00d7-\u2192-equiv\n\nmodule _ {A : \u2605}{B : A \u2192 \u2605}{C : (x : A) \u2192 B x \u2192 \u2605} where\n \u03a3-assoc-equiv : (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2243 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc-equiv = equiv (\u03bb x \u2192 (fst x , fst (snd x)) , snd (snd x))\n (\u03bb x \u2192 fst (fst x) , snd (fst x) , snd x)\n (\u03bb y \u2192 idp)\n (\u03bb y \u2192 idp)\n\n \u03a3-assoc : {{_ : UA}} \u2192 (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2261 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc = ua \u03a3-assoc-equiv\n\nmodule _ {A B : \u2605} where\n \u00d7-comm-equiv : (A \u00d7 B) \u2243 (B \u00d7 A)\n \u00d7-comm-equiv = equiv swap swap (\u03bb y \u2192 idp) (\u03bb x \u2192 idp)\n\n \u00d7-comm : {{_ : UA}} \u2192 (A \u00d7 B) \u2261 (B \u00d7 A)\n \u00d7-comm = ua \u00d7-comm-equiv\n\n \u228e-comm-equiv : (A \u228e B) \u2243 (B \u228e A)\n \u228e-comm-equiv = equiv [inl: inr ,inr: inl ]\n [inl: inr ,inr: inl ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n\n \u228e-comm : {{_ : UA}} \u2192 (A \u228e B) \u2261 (B \u228e A)\n \u228e-comm = ua \u228e-comm-equiv\n\nmodule _ {A B : \u2605}{C : A \u2192 B \u2192 \u2605} where\n \u03a0\u03a0-comm-equiv : ((x : A)(y : B) \u2192 C x y) \u2243 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm-equiv = equiv flip flip (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a0\u03a0-comm : {{_ : UA}} \u2192 ((x : A)(y : B) \u2192 C x y) \u2261 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm = ua \u03a0\u03a0-comm-equiv\n\n \u03a3\u03a3-comm-equiv : (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2243 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm-equiv = equiv (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb _ \u2192 idp)\n (\u03bb _ \u2192 idp)\n\n \u03a3\u03a3-comm : {{_ : UA}} \u2192 (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2261 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm = ua \u03a3\u03a3-comm-equiv\n\nmodule _ {A B C : \u2605} where\n \u00d7-assoc : {{_ : UA}} \u2192 (A \u00d7 (B \u00d7 C)) \u2261 ((A \u00d7 B) \u00d7 C)\n \u00d7-assoc = \u03a3-assoc\n\n \u228e-assoc-equiv : (A \u228e (B \u228e C)) \u2243 ((A \u228e B) \u228e C)\n \u228e-assoc-equiv = equiv [inl: inl \u2218 inl ,inr: [inl: inl \u2218 inr ,inr: inr ] ]\n [inl: [inl: inl ,inr: inr \u2218 inl ] ,inr: inr \u2218 inr ]\n [inl: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ]\n\n \u228e-assoc : {{_ : UA}} \u2192 (A \u228e (B \u228e C)) \u2261 ((A \u228e B) \u228e C)\n \u228e-assoc = ua \u228e-assoc-equiv\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \ud835\udfd8 \u2192 \u2605) where\n \u03a0\ud835\udfd8-uniq : \u03a0 \ud835\udfd8 A \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq = ua (equiv _ (\u03bb _ ()) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= (\u03bb())))\n\nmodule _ {{_ : UA}}(A : \ud835\udfd9 \u2192 \u2605) where\n \u03a0\ud835\udfd9-uniq : \u03a0 \ud835\udfd9 A \u2261 A _\n \u03a0\ud835\udfd9-uniq = ua (equiv (\u03bb f \u2192 f _) (\u03bb x _ \u2192 x) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\nmodule _ {{_ : UA}}(A : \u2605) where\n \u03a0\ud835\udfd9-uniq\u2032 : (\ud835\udfd9 \u2192 A) \u2261 A\n \u03a0\ud835\udfd9-uniq\u2032 = \u03a0\ud835\udfd9-uniq (\u03bb _ \u2192 A)\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \u2605) where\n \u03a0\ud835\udfd8-uniq\u2032 : (\ud835\udfd8 \u2192 A) \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq\u2032 = \u03a0\ud835\udfd8-uniq (\u03bb _ \u2192 A)\n\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 = \u03a0\ud835\udfd8-uniq\u2032\n\nmodule _ {{_ : FunExt}}(F G : \ud835\udfd8 \u2192 \u2605) where\n -- also by \u03a0\ud835\udfd8-uniq twice\n \u03a0\ud835\udfd8-uniq' : \u03a0 \ud835\udfd8 F \u2261 \u03a0 \ud835\udfd8 G\n \u03a0\ud835\udfd8-uniq' = \u03a0=\u2032 \ud835\udfd8 (\u03bb())\n\nmodule _ {A : \ud835\udfd8 \u2192 \u2605} where\n \u03a3\ud835\udfd8-fst-equiv : \u03a3 \ud835\udfd8 A \u2243 \ud835\udfd8\n \u03a3\ud835\udfd8-fst-equiv = equiv fst (\u03bb()) (\u03bb()) (\u03bb { (() , _) })\n\n \u03a3\ud835\udfd8-fst : {{_ : UA}} \u2192 \u03a3 \ud835\udfd8 A \u2261 \ud835\udfd8\n \u03a3\ud835\udfd8-fst = ua \u03a3\ud835\udfd8-fst-equiv\n\nmodule _ {A : \ud835\udfd9 \u2192 \u2605} where\n \u03a3\ud835\udfd9-snd-equiv : \u03a3 \ud835\udfd9 A \u2243 A _\n \u03a3\ud835\udfd9-snd-equiv = equiv snd (_,_ _) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a3\ud835\udfd9-snd : {{_ : UA}} \u2192 \u03a3 \ud835\udfd9 A \u2261 A _\n \u03a3\ud835\udfd9-snd = ua \u03a3\ud835\udfd9-snd-equiv\n\nmodule _ {A : \u2605} where\n \u228e\ud835\udfd8-inl-equiv : A \u2243 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl-equiv = equiv inl [inl: id ,inr: (\u03bb()) ] [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb()) ] (\u03bb _ \u2192 idp)\n\n \u228e\ud835\udfd8-inl : {{_ : UA}} \u2192 A \u2261 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl = ua \u228e\ud835\udfd8-inl-equiv\n\n \ud835\udfd9\u00d7-snd : {{_ : UA}} \u2192 (\ud835\udfd9 \u00d7 A) \u2261 A\n \ud835\udfd9\u00d7-snd = \u03a3\ud835\udfd9-snd\n\n \u00d7\ud835\udfd9-fst : {{_ : UA}} \u2192 (A \u00d7 \ud835\udfd9) \u2261 A\n \u00d7\ud835\udfd9-fst = \u00d7-comm \u2219 \ud835\udfd9\u00d7-snd\n\n -- old names\n A\u00d7\ud835\udfd9\u2261A = \u00d7\ud835\udfd9-fst\n \ud835\udfd9\u00d7A\u2261A = \ud835\udfd9\u00d7-snd\n\nmodule _ {A : \u2605}{B : A \u2192 \u2605}{C : \u03a3 A B \u2192 \u2605} where\n -- AC: Dependent axiom of choice\n -- In Type Theory it happens to be neither an axiom nor to be choosing anything.\n \u03a0\u03a3-comm-equiv : (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2243 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm-equiv = equiv (\u03bb H \u2192 fst \u2218 H , snd \u2218 H) (\u03bb H \u2192 < fst H , snd H >) (\u03bb H \u2192 idp) (\u03bb H \u2192 idp)\n\n \u03a0\u03a3-comm : {{_ : UA}}\n \u2192 (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2261 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm = ua \u03a0\u03a3-comm-equiv\n\nmodule _ {A : \ud835\udfda \u2192 \u2605}{{_ : UA}}{{_ : FunExt}} where\n \u03a3\ud835\udfda-\u228e : \u03a3 \ud835\udfda A \u2261 (A 0\u2082 \u228e A 1\u2082)\n \u03a3\ud835\udfda-\u228e = \u03a3-first \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 \u2219 dist-\u228e-\u03a3 \u2219 ap\u2082 _\u228e_ \u03a3\ud835\udfd9-snd \u03a3\ud835\udfd9-snd\n\n \u03a0\ud835\udfda-\u00d7 : \u03a0 \ud835\udfda A \u2261 (A 0\u2082 \u00d7 A 1\u2082)\n \u03a0\ud835\udfda-\u00d7 = \u03a0-first \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 \u2219 dist-\u00d7-\u03a0 \u2219 ap\u2082 _\u00d7_ (\u03a0\ud835\udfd9-uniq _) (\u03a0\ud835\udfd9-uniq _)\n\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 = \u03a0\ud835\udfda-\u00d7\n\nmodule _ {A : \u2605}{{_ : UA}}{{_ : FunExt}} where\n \u03a3\ud835\udfda-\u228e\u2032 : (\ud835\udfda \u00d7 A) \u2261 (A \u228e A)\n \u03a3\ud835\udfda-\u228e\u2032 = \u03a3\ud835\udfda-\u228e\n\n -- \ud835\udfda\u2192A\u2194A\u00d7A : (\ud835\udfda \u2192 A) \u2194 (A \u00d7 A)\n -- \ud835\udfda\u2192A\u2194A\u00d7A\n\nmodule _ where\n\n private\n maybe-elim : {X : Set}(m : Maybe X) \u2192 m \u2262 nothing \u2192 X\n maybe-elim {X} m = maybe {B = \u03bb m' \u2192 m' \u2262 _ \u2192 _} (\u03bb x _ \u2192 x) (\u03bb e \u2192 \ud835\udfd8-elim (e idp)) m\n\n maybe-elim-just : {X : Set}(m : Maybe X)(p : m \u2262 nothing)\n \u2192 just (maybe-elim m p) \u2261 m\n maybe-elim-just (just x) p = idp\n maybe-elim-just nothing p = \ud835\udfd8-elim (p idp)\n\n maybe-elim-cong : \u2200 {X : Set}{m m' : Maybe X}{p p'} \u2192 m \u2261 m'\n \u2192 maybe-elim m p \u2261 maybe-elim m' p'\n maybe-elim-cong {m = just x} idp = idp\n maybe-elim-cong {m = nothing} {p = p} idp = \ud835\udfd8-elim (p idp)\n\n not-just : {X : Set}{x : X} \u2192 _\u2262_ {A = Maybe X} (just x) nothing\n not-just ()\n\n !!-p : \u2200 {x}{X : Set x}{x y : X}(p : x \u2261 y) \u2192 ! (! p) \u2261 p\n !!-p idp = idp\n\n !\u2219 : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 ! p \u2219 p \u2261 idp\n !\u2219 idp = idp\n\n \u2219! : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 p \u2219 ! p \u2261 idp\n \u2219! idp = idp\n\n\n record PreservePoint {A B : Set}(eq : Maybe A \u2261 Maybe B) : Set where\n field\n coe-p : \u2200 x \u2192 coe eq (just x) \u2262 nothing\n coe!-p : \u2200 x \u2192 coe! eq (just x) \u2262 nothing\n open PreservePoint\n\n nothing-to-nothing : {A B : Set}(eq : Maybe A \u2261 Maybe B)\n \u2192 coe eq nothing \u2261 nothing \u2192 PreservePoint eq\n nothing-to-nothing eq n2n = record { coe-p = \u03bb x p \u2192 not-just (coe-inj eq (p \u2219 ! n2n))\n ; coe!-p = \u03bb x p \u2192 not-just (coe-same (! !\u2219 eq) (just x)\n \u2219 ! (coe\u2218coe eq (! eq) (just x)) \u2219 ap (coe eq) p \u2219 n2n) }\n\n !-PP : \u2200 {A B : Set} {e : Maybe A \u2261 Maybe B} \u2192 PreservePoint e \u2192 PreservePoint (! e)\n !-PP {e = e} e-pp = record { coe-p = coe!-p e-pp\n ; coe!-p = \u03bb x p \u2192 coe-p e-pp x ( ap (\u03bb X \u2192 coe X (just x)) (! !!-p e ) \u2219 p) }\n\n to : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e \u2192 A \u2192 B\n to e e-pp x = maybe-elim (coe e (just x)) (coe-p e-pp x)\n\n module _{A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}{ep ep'}(e'\u2219e=id : e' \u2219 e \u2261 idp)(x : B) where\n to-to : to e ep (to e' ep' x) \u2261 x\n to-to = just-injective (ap just (maybe-elim-cong\n {m = coe e (just (maybe-elim (coe e' (just x)) _))}\n {m' = coe e (coe e' (just x))}\n {p' = \u03bb q \u2192 not-just (! p \u2219 q)} (ap (coe e) (maybe-elim-just _ _)))\n \u2219 maybe-elim-just (coe e (coe e' (just x))) (\u03bb q \u2192 not-just (! p \u2219 q))\n \u2219 p)\n where\n p : coe e (coe e' (just x)) \u2261 just x\n p = coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n\n cto : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 Maybe A \u2192 Maybe B\n cto e = maybe (maybe\u2032 just (coe e nothing) \u2218 coe e \u2218 just) nothing\n\n module _ {A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}(e'\u2219e=id : e' \u2219 e \u2261 idp) where\n cto-cto : (x : Maybe B) \u2192 cto e (cto e' x) \u2261 x\n cto-cto nothing = idp\n cto-cto (just x) with coe e' (just x) | coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n cto-cto (just x) | just x\u2081 | p = ap (maybe just (coe e nothing)) p\n cto-cto (just x) | nothing | p with coe e' nothing\n | coe\u2218coe e e' nothing \u2219 coe-same e'\u2219e=id nothing\n cto-cto (just x) | nothing | p | just y | q = ap (maybe just (coe e nothing)) q \u2219 p\n cto-cto (just x) | nothing | p | nothing | q = ! q \u2219 p\n\n module _ {A B : Set} where\n create-PP-\u2243 : Maybe A \u2261 Maybe B \u2192 Maybe A \u2243 Maybe B\n create-PP-\u2243 e = equiv (cto e)\n (cto (! e))\n (cto-cto {e = e} {e' = ! e} (!\u2219 e))\n (cto-cto {e = ! e}{e' = e} (\u2219! e))\n\n create-PP : {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 \u03a3 (Maybe A \u2261 Maybe B) PreservePoint\n create-PP eq = ua (create-PP-\u2243 eq) , nothing-to-nothing _ (coe-\u03b2 (create-PP-\u2243 eq) nothing)\n\n Maybe-injective-PP : {{_ : UA}} \u2192 (e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e\n \u2192 A \u2261 B\n Maybe-injective-PP e e-pp = ua\n (equiv (to e e-pp)\n (to (! e) (!-PP e-pp))\n (to-to {e = e} { ! e} {e-pp}{ !-PP e-pp} (!\u2219 e))\n (to-to {e = ! e} {e} { !-PP e-pp} {e-pp} (\u2219! e)))\n\n Maybe-injective : \u2200 {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 A \u2261 B\n Maybe-injective e = let (e' , e'-pp) = create-PP e in Maybe-injective-PP e' e'-pp\n\n\nmodule _ {a}{A : \u2605_ a} where\n Maybe\u2243\ud835\udfd9\u228e : Maybe A \u2243 (\ud835\udfd9 \u228e A)\n Maybe\u2243\ud835\udfd9\u228e = equiv (maybe inr (inl _)) [inl: const nothing ,inr: just ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (maybe (\u03bb x \u2192 idp) idp)\n\n Maybe\u2261\ud835\udfd9\u228e : \u2200 {{_ : UA}}\u2192 Maybe A \u2261 (\ud835\udfd9 \u228e A)\n Maybe\u2261\ud835\udfd9\u228e = ua Maybe\u2243\ud835\udfd9\u228e\n\nmodule _ {{_ : UA}} where\n Fin0\u2261\ud835\udfd8 : Fin 0 \u2261 \ud835\udfd8\n Fin0\u2261\ud835\udfd8 = ua (equiv (\u03bb ()) (\u03bb ()) (\u03bb ()) (\u03bb ()))\n\n Fin1\u2261\ud835\udfd9 : Fin 1 \u2261 \ud835\udfd9\n Fin1\u2261\ud835\udfd9 = ua (equiv _ (\u03bb _ \u2192 zero) (\u03bb _ \u2192 idp) \u03b2)\n where \u03b2 : (_ : Fin 1) \u2192 _\n \u03b2 zero = idp\n \u03b2 (suc ())\n\nmodule _ where\n isZero? : \u2200 {n}{A : Fin (suc n) \u2192 Set} \u2192 ((i : Fin n) \u2192 A (suc i)) \u2192 A zero\n \u2192 (i : Fin (suc n)) \u2192 A i\n isZero? f x zero = x\n isZero? f x (suc i) = f i\n\n Fin\u2218suc\u2243\ud835\udfd9\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2243 (\ud835\udfd9 \u228e Fin n)\n Fin\u2218suc\u2243\ud835\udfd9\u228eFin = equiv (isZero? inr (inl _)) [inl: (\u03bb _ \u2192 zero) ,inr: suc ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n (isZero? (\u03bb _ \u2192 idp) idp)\n\n Fin\u2218suc\u2261\ud835\udfd9\u228eFin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 (\ud835\udfd9 \u228e Fin n)\n Fin\u2218suc\u2261\ud835\udfd9\u228eFin = ua Fin\u2218suc\u2243\ud835\udfd9\u228eFin\n\nFin-\u228e-+ : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u228e Fin n) \u2261 Fin (m \u2115.+ n)\nFin-\u228e-+ {zero} = ap\u2082 _\u228e_ Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm \u2219 ! \u228e\ud835\udfd8-inl\nFin-\u228e-+ {suc m} = ap\u2082 _\u228e_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ap (_\u228e_ \ud835\udfd9) Fin-\u228e-+ \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\n\nFin-\u00d7-* : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u00d7 Fin n) \u2261 Fin (m \u2115.* n)\nFin-\u00d7-* {zero} = ap\u2082 _\u00d7_ Fin0\u2261\ud835\udfd8 idp \u2219 \u03a3\ud835\udfd8-fst \u2219 ! Fin0\u2261\ud835\udfd8\nFin-\u00d7-* {suc m} = ap\u2082 _\u00d7_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u228e-\u00d7 \u2219 ap\u2082 _\u228e_ \u03a3\ud835\udfd9-snd Fin-\u00d7-* \u2219 Fin-\u228e-+\n\nFin-\u2192-^ : \u2200 {{_ : UA}}{{_ : FunExt}}{m n} \u2192 (Fin m \u2192 Fin n) \u2261 Fin (n \u2115.^ m)\nFin-\u2192-^ {zero} = ap\u2082 _\u27f6_ Fin0\u2261\ud835\udfd8 idp \u2219 \u03a0\ud835\udfd8-uniq\u2032 _ \u2219 \u228e\ud835\udfd8-inl \u2219 ap (_\u228e_ \ud835\udfd9) (! Fin0\u2261\ud835\udfd8)\n \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\nFin-\u2192-^ {suc m} = ap\u2082 _\u27f6_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u00d7-\u2192 \u2219 ap\u2082 _\u00d7_ (\u03a0\ud835\udfd9-uniq _) Fin-\u2192-^ \u2219 Fin-\u00d7-*\n\nFin\u2218suc\u2261Maybe\u2218Fin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 Maybe (Fin n)\nFin\u2218suc\u2261Maybe\u2218Fin = Fin\u2218suc\u2261\ud835\udfd9\u228eFin \u2219 ! Maybe\u2261\ud835\udfd9\u228e\n\nFin-injective : \u2200 {{_ : UA}}{m n} \u2192 Fin m \u2261 Fin n \u2192 m \u2261 n\nFin-injective {zero} {zero} Finm\u2261Finn = idp\nFin-injective {zero} {suc n} Finm\u2261Finn = \ud835\udfd8-elim (coe (! Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {zero} Finm\u2261Finn = \ud835\udfd8-elim (coe (Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {suc n} Finm\u2261Finn\n = ap suc (Fin-injective (Maybe-injective\n (! Fin\u2218suc\u2261Maybe\u2218Fin \u2219 Finm\u2261Finn \u2219 Fin\u2218suc\u2261Maybe\u2218Fin)))\n\nFin\u228e-injective : \u2200 {{_ : UA}}{A B : Set} n \u2192 (Fin n \u228e A) \u2261 (Fin n \u228e B) \u2192 A \u2261 B\nFin\u228e-injective zero f = \u228e\ud835\udfd8-inl \u2219 \u228e-comm \u2219 ap\u2082 _\u228e_ (! Fin0\u2261\ud835\udfd8) idp\n \u2219 f \u2219 (ap\u2082 _\u228e_ Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm) \u2219 ! \u228e\ud835\udfd8-inl\nFin\u228e-injective (suc n) f = Fin\u228e-injective n (Maybe-injective\n (Maybe\u2261\ud835\udfd9\u228e \u2219 \u228e-assoc \u2219 ap\u2082 _\u228e_ (! Fin\u2218suc\u2261\ud835\udfd9\u228eFin) idp \u2219 f\n \u2219 ap\u2082 _\u228e_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ! Maybe\u2261\ud835\udfd9\u228e))\n\nmodule _ {{_ : UA}} where\n Lift\u2261id : \u2200 {a} {A : \u2605_ a} \u2192 Lift {a} {a} A \u2261 A\n Lift\u2261id = ua (equiv lower lift (\u03bb _ \u2192 idp) (\u03bb { (lift x) \u2192 idp }))\n\n \u03a0\ud835\udfd9F\u2261F : \u2200 {\u2113} {F : \ud835\udfd9 \u2192 \u2605_ \u2113} \u2192 \u03a0 \ud835\udfd9 F \u2261 F _\n \u03a0\ud835\udfd9F\u2261F = ua (equiv (\u03bb x \u2192 x _) const (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\n \ud835\udfd9\u2192A\u2261A : \u2200 {\u2113} {A : \u2605_ \u2113} \u2192 (\ud835\udfd9 \u2192 A) \u2261 A\n \ud835\udfd9\u2192A\u2261A = \u03a0\ud835\udfd9F\u2261F\n\n not-\ud835\udfda\u2261\ud835\udfda : \ud835\udfda \u2261 \ud835\udfda\n not-\ud835\udfda\u2261\ud835\udfda = twist\n\n{-\nTODO ?\nMaybe\ud835\udfd8\u2194\ud835\udfd9 : Maybe \ud835\udfd8 \u2194 \ud835\udfd9\nMaybe\ud835\udfd8\u2194\ud835\udfd9 = A\u228e\ud835\udfd8\u2194A \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe^\ud835\udfd8\u2194Fin : \u2200 n \u2192 Maybe^ n \ud835\udfd8 \u2194 Fin n\nMaybe^\ud835\udfd8\u2194Fin zero = sym Fin0\u2194\ud835\udfd8\nMaybe^\ud835\udfd8\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\ud835\udfd8\u2194Fin n)\n\nMaybe^\ud835\udfd9\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \ud835\udfd9 \u2194 Fin (suc n)\nMaybe^\ud835\udfd9\u2194Fin1+ n = Maybe^\ud835\udfd8\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\ud835\udfd8\u2194\ud835\udfd9)\n\nMaybe-\u228e : \u2200 {a} {A B : \u2605 a} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e {a} = sym Maybe\u2194Lift\ud835\udfd9\u228e \u2218 \u228e-CMon.assoc (Lift {_} {a} \ud835\udfd9) _ _ \u2218 (Maybe\u2194Lift\ud835\udfd9\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \ud835\udfd8 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \ud835\udfd8\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n-}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Type\n\nopen import Level.NP\nopen import HoTT\nopen import Function.NP\nopen import Function.Extensionality\n\nopen import Data.Maybe.NP using (Maybe ; just ; nothing ; maybe ; maybe\u2032 ; just-injective)\nopen import Data.Zero using (\ud835\udfd8 ; \ud835\udfd8-elim)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Two using (\ud835\udfda ; 0\u2082 ; 1\u2082 ; [0:_1:_]; twist)\nopen import Data.Fin as Fin using (Fin ; suc ; zero)\nopen import Data.Nat.NP as \u2115 using (\u2115 ; suc ; zero)\nopen import Data.Product.NP renaming (proj\u2081 to fst; proj\u2082 to snd)\nopen import Data.Sum using (_\u228e_) renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_ ; ap; coe; coe!; !_; _\u2219_; J ; inspect ; Reveal_is_ ; [_]; tr) renaming (refl to idp; cong\u2082 to ap\u2082; _\u2257_ to _\u223c_)\n\nmodule Type.Identities where\n\nopen Equivalences\n\n\n-- for use with ap\u2082 etc.\n_\u27f6_ : \u2200 {a b} \u2192 \u2605_ a \u2192 \u2605_ b \u2192 \u2605_ (b \u2294 a)\nA \u27f6 B = A \u2192 B\n\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 : \ud835\udfda \u2243 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 = equiv [0: inl _ 1: inr _ ]\n [inl: const 0\u2082 ,inr: const 1\u2082 ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n [0: idp 1: idp ]\n\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 : {{_ : UA}} \u2192 \ud835\udfda \u2261 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 = ua \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9\n\nmodule _ {{_ : UA}}{A : \u2605}{B C : A \u2192 \u2605} where\n \u03a3\u228e-split : (\u03a3 A (\u03bb x \u2192 B x \u228e C x)) \u2261 (\u03a3 A B \u228e \u03a3 A C)\n \u03a3\u228e-split = ua (equiv (\u03bb { (x , inl y) \u2192 inl (x , y)\n ; (x , inr y) \u2192 inr (x , y) })\n (\u03bb { (inl (x , y)) \u2192 x , inl y\n ; (inr (x , y)) \u2192 x , inr y })\n (\u03bb { (inl (x , y)) \u2192 idp\n ; (inr (x , y)) \u2192 idp })\n (\u03bb { (x , inl y) \u2192 idp\n ; (x , inr y) \u2192 idp }))\n\nmodule _ {{_ : UA}}{A B : \u2605}{C : A \u228e B \u2192 \u2605} where\n dist-\u228e-\u03a3-equiv : \u03a3 (A \u228e B) C \u2243 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u03a3 : \u03a3 (A \u228e B) C \u2261 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3 = ua dist-\u228e-\u03a3-equiv\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0-equiv : \u03a0 (A \u228e B) C \u2243 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u03a0 : \u03a0 (A \u228e B) C \u2261 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0 = ua dist-\u00d7-\u03a0-equiv\n\nmodule _ {{_ : UA}}{A B : \u2605}{C : A \u2192 \u2605}{D : B \u2192 \u2605} where\n dist-\u228e-\u03a3[] : (\u03a3 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a3 A C \u228e \u03a3 B D)\n dist-\u228e-\u03a3[] = dist-\u228e-\u03a3\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0[] : (\u03a0 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a0 A C \u00d7 \u03a0 B D)\n dist-\u00d7-\u03a0[] = dist-\u00d7-\u03a0\n\nmodule _ {{_ : UA}}{A B C : \u2605} where\n dist-\u228e-\u00d7-equiv : ((A \u228e B) \u00d7 C) \u2243 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u00d7 : ((A \u228e B) \u00d7 C) \u2261 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7 = ua dist-\u228e-\u00d7-equiv\n\n module _ {{_ : FunExt}} where\n dist-\u00d7-\u2192-equiv : ((A \u228e B) \u2192 C ) \u2243 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u2192 : ((A \u228e B) \u2192 C) \u2261 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192 = ua dist-\u00d7-\u2192-equiv\n\nmodule _ {A : \u2605}{B : A \u2192 \u2605}{C : (x : A) \u2192 B x \u2192 \u2605} where\n \u03a3-assoc-equiv : (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2243 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc-equiv = equiv (\u03bb x \u2192 (fst x , fst (snd x)) , snd (snd x))\n (\u03bb x \u2192 fst (fst x) , snd (fst x) , snd x)\n (\u03bb y \u2192 idp)\n (\u03bb y \u2192 idp)\n\n \u03a3-assoc : {{_ : UA}} \u2192 (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2261 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc = ua \u03a3-assoc-equiv\n\nmodule _ {A B : \u2605} where\n \u00d7-comm-equiv : (A \u00d7 B) \u2243 (B \u00d7 A)\n \u00d7-comm-equiv = equiv swap swap (\u03bb y \u2192 idp) (\u03bb x \u2192 idp)\n\n \u00d7-comm : {{_ : UA}} \u2192 (A \u00d7 B) \u2261 (B \u00d7 A)\n \u00d7-comm = ua \u00d7-comm-equiv\n\n \u228e-comm-equiv : (A \u228e B) \u2243 (B \u228e A)\n \u228e-comm-equiv = equiv [inl: inr ,inr: inl ]\n [inl: inr ,inr: inl ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n\n \u228e-comm : {{_ : UA}} \u2192 (A \u228e B) \u2261 (B \u228e A)\n \u228e-comm = ua \u228e-comm-equiv\n\nmodule _ {A B : \u2605}{C : A \u2192 B \u2192 \u2605} where\n \u03a0\u03a0-comm-equiv : ((x : A)(y : B) \u2192 C x y) \u2243 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm-equiv = equiv flip flip (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a0\u03a0-comm : {{_ : UA}} \u2192 ((x : A)(y : B) \u2192 C x y) \u2261 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm = ua \u03a0\u03a0-comm-equiv\n\n \u03a3\u03a3-comm-equiv : (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2243 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm-equiv = equiv (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb _ \u2192 idp)\n (\u03bb _ \u2192 idp)\n\n \u03a3\u03a3-comm : {{_ : UA}} \u2192 (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2261 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm = ua \u03a3\u03a3-comm-equiv\n\nmodule _ {A B C : \u2605} where\n \u00d7-assoc : {{_ : UA}} \u2192 (A \u00d7 (B \u00d7 C)) \u2261 ((A \u00d7 B) \u00d7 C)\n \u00d7-assoc = \u03a3-assoc\n\n \u228e-assoc-equiv : (A \u228e (B \u228e C)) \u2243 ((A \u228e B) \u228e C)\n \u228e-assoc-equiv = equiv [inl: inl \u2218 inl ,inr: [inl: inl \u2218 inr ,inr: inr ] ]\n [inl: [inl: inl ,inr: inr \u2218 inl ] ,inr: inr \u2218 inr ]\n [inl: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ]\n\n \u228e-assoc : {{_ : UA}} \u2192 (A \u228e (B \u228e C)) \u2261 ((A \u228e B) \u228e C)\n \u228e-assoc = ua \u228e-assoc-equiv\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \ud835\udfd8 \u2192 \u2605) where\n \u03a0\ud835\udfd8-uniq : \u03a0 \ud835\udfd8 A \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq = ua (equiv _ (\u03bb _ ()) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= (\u03bb())))\n\nmodule _ {{_ : UA}}(A : \ud835\udfd9 \u2192 \u2605) where\n \u03a0\ud835\udfd9-uniq : \u03a0 \ud835\udfd9 A \u2261 A _\n \u03a0\ud835\udfd9-uniq = ua (equiv (\u03bb f \u2192 f _) (\u03bb x _ \u2192 x) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\nmodule _ {{_ : UA}}(A : \u2605) where\n \u03a0\ud835\udfd9-uniq\u2032 : (\ud835\udfd9 \u2192 A) \u2261 A\n \u03a0\ud835\udfd9-uniq\u2032 = \u03a0\ud835\udfd9-uniq (\u03bb _ \u2192 A)\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \u2605) where\n \u03a0\ud835\udfd8-uniq\u2032 : (\ud835\udfd8 \u2192 A) \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq\u2032 = \u03a0\ud835\udfd8-uniq (\u03bb _ \u2192 A)\n\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 = \u03a0\ud835\udfd8-uniq\u2032\n\nmodule _ {{_ : FunExt}}(F G : \ud835\udfd8 \u2192 \u2605) where\n -- also by \u03a0\ud835\udfd8-uniq twice\n \u03a0\ud835\udfd8-uniq' : \u03a0 \ud835\udfd8 F \u2261 \u03a0 \ud835\udfd8 G\n \u03a0\ud835\udfd8-uniq' = \u03a0=\u2032 \ud835\udfd8 (\u03bb())\n\nmodule _ {A : \ud835\udfd8 \u2192 \u2605} where\n \u03a3\ud835\udfd8-fst-equiv : \u03a3 \ud835\udfd8 A \u2243 \ud835\udfd8\n \u03a3\ud835\udfd8-fst-equiv = equiv fst (\u03bb()) (\u03bb()) (\u03bb { (() , _) })\n\n \u03a3\ud835\udfd8-fst : {{_ : UA}} \u2192 \u03a3 \ud835\udfd8 A \u2261 \ud835\udfd8\n \u03a3\ud835\udfd8-fst = ua \u03a3\ud835\udfd8-fst-equiv\n\nmodule _ {A : \ud835\udfd9 \u2192 \u2605} where\n \u03a3\ud835\udfd9-snd-equiv : \u03a3 \ud835\udfd9 A \u2243 A _\n \u03a3\ud835\udfd9-snd-equiv = equiv snd (_,_ _) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a3\ud835\udfd9-snd : {{_ : UA}} \u2192 \u03a3 \ud835\udfd9 A \u2261 A _\n \u03a3\ud835\udfd9-snd = ua \u03a3\ud835\udfd9-snd-equiv\n\nmodule _ {A : \u2605} where\n \u228e\ud835\udfd8-inl-equiv : A \u2243 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl-equiv = equiv inl [inl: id ,inr: (\u03bb()) ] [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb()) ] (\u03bb _ \u2192 idp)\n\n \u228e\ud835\udfd8-inl : {{_ : UA}} \u2192 A \u2261 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl = ua \u228e\ud835\udfd8-inl-equiv\n\n \ud835\udfd9\u00d7-snd : {{_ : UA}} \u2192 (\ud835\udfd9 \u00d7 A) \u2261 A\n \ud835\udfd9\u00d7-snd = \u03a3\ud835\udfd9-snd\n\n \u00d7\ud835\udfd9-fst : {{_ : UA}} \u2192 (A \u00d7 \ud835\udfd9) \u2261 A\n \u00d7\ud835\udfd9-fst = \u00d7-comm \u2219 \ud835\udfd9\u00d7-snd\n\n -- old names\n A\u00d7\ud835\udfd9\u2261A = \u00d7\ud835\udfd9-fst\n \ud835\udfd9\u00d7A\u2261A = \ud835\udfd9\u00d7-snd\n\nmodule _ {A : \u2605}{B : A \u2192 \u2605}{C : \u03a3 A B \u2192 \u2605} where\n -- AC: Dependent axiom of choice\n -- In Type Theory it happens to be neither an axiom nor to be choosing anything.\n \u03a0\u03a3-comm-equiv : (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2243 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm-equiv = equiv (\u03bb H \u2192 fst \u2218 H , snd \u2218 H) (\u03bb H \u2192 < fst H , snd H >) (\u03bb H \u2192 idp) (\u03bb H \u2192 idp)\n\n \u03a0\u03a3-comm : {{_ : UA}}\n \u2192 (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2261 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm = ua \u03a0\u03a3-comm-equiv\n\nmodule _ {A : \ud835\udfda \u2192 \u2605}{{_ : UA}}{{_ : FunExt}} where\n \u03a3\ud835\udfda-\u228e : \u03a3 \ud835\udfda A \u2261 (A 0\u2082 \u228e A 1\u2082)\n \u03a3\ud835\udfda-\u228e = \u03a3-first \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 \u2219 dist-\u228e-\u03a3 \u2219 ap\u2082 _\u228e_ \u03a3\ud835\udfd9-snd \u03a3\ud835\udfd9-snd\n\n \u03a0\ud835\udfda-\u00d7 : \u03a0 \ud835\udfda A \u2261 (A 0\u2082 \u00d7 A 1\u2082)\n \u03a0\ud835\udfda-\u00d7 = \u03a0-first \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 \u2219 dist-\u00d7-\u03a0 \u2219 ap\u2082 _\u00d7_ (\u03a0\ud835\udfd9-uniq _) (\u03a0\ud835\udfd9-uniq _)\n\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 = \u03a0\ud835\udfda-\u00d7\n\nmodule _ {A : \u2605}{{_ : UA}}{{_ : FunExt}} where\n \u03a3\ud835\udfda-\u228e\u2032 : \ud835\udfda \u00d7 A \u2261 (A \u228e A)\n \u03a3\ud835\udfda-\u228e\u2032 = \u03a3\ud835\udfda-\u228e\n\n \ud835\udfda\u2192A\u2194A\u00d7A : (\ud835\udfda \u2192 A) \u2194 (A \u00d7 A)\n \ud835\udfda\u2192A\u2194A\u00d7A\n\nmodule _ where\n\n private\n maybe-elim : {X : Set}(m : Maybe X) \u2192 m \u2262 nothing \u2192 X\n maybe-elim {X} m = maybe {B = \u03bb m' \u2192 m' \u2262 _ \u2192 _} (\u03bb x _ \u2192 x) (\u03bb e \u2192 \ud835\udfd8-elim (e idp)) m\n\n maybe-elim-just : {X : Set}(m : Maybe X)(p : m \u2262 nothing)\n \u2192 just (maybe-elim m p) \u2261 m\n maybe-elim-just (just x) p = idp\n maybe-elim-just nothing p = \ud835\udfd8-elim (p idp)\n\n maybe-elim-cong : \u2200 {X : Set}{m m' : Maybe X}{p p'} \u2192 m \u2261 m'\n \u2192 maybe-elim m p \u2261 maybe-elim m' p'\n maybe-elim-cong {m = just x} idp = idp\n maybe-elim-cong {m = nothing} {p = p} idp = \ud835\udfd8-elim (p idp)\n\n not-just : {X : Set}{x : X} \u2192 _\u2262_ {A = Maybe X} (just x) nothing\n not-just ()\n\n !!-p : \u2200 {x}{X : Set x}{x y : X}(p : x \u2261 y) \u2192 ! (! p) \u2261 p\n !!-p idp = idp\n\n !\u2219 : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 ! p \u2219 p \u2261 idp\n !\u2219 idp = idp\n\n \u2219! : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 p \u2219 ! p \u2261 idp\n \u2219! idp = idp\n\n\n record PreservePoint {A B : Set}(eq : Maybe A \u2261 Maybe B) : Set where\n field\n coe-p : \u2200 x \u2192 coe eq (just x) \u2262 nothing\n coe!-p : \u2200 x \u2192 coe! eq (just x) \u2262 nothing\n open PreservePoint\n\n nothing-to-nothing : {A B : Set}(eq : Maybe A \u2261 Maybe B)\n \u2192 coe eq nothing \u2261 nothing \u2192 PreservePoint eq\n nothing-to-nothing eq n2n = record { coe-p = \u03bb x p \u2192 not-just (coe-inj eq (p \u2219 ! n2n))\n ; coe!-p = \u03bb x p \u2192 not-just (coe-same (! !\u2219 eq) (just x)\n \u2219 ! (coe\u2218coe eq (! eq) (just x)) \u2219 ap (coe eq) p \u2219 n2n) }\n\n !-PP : \u2200 {A B : Set} {e : Maybe A \u2261 Maybe B} \u2192 PreservePoint e \u2192 PreservePoint (! e)\n !-PP {e = e} e-pp = record { coe-p = coe!-p e-pp\n ; coe!-p = \u03bb x p \u2192 coe-p e-pp x ( ap (\u03bb X \u2192 coe X (just x)) (! !!-p e ) \u2219 p) }\n\n to : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e \u2192 A \u2192 B\n to e e-pp x = maybe-elim (coe e (just x)) (coe-p e-pp x)\n\n module _{A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}{ep ep'}(e'\u2219e=id : e' \u2219 e \u2261 idp)(x : B) where\n to-to : to e ep (to e' ep' x) \u2261 x\n to-to = just-injective (ap just (maybe-elim-cong\n {m = coe e (just (maybe-elim (coe e' (just x)) _))}\n {m' = coe e (coe e' (just x))}\n {p' = \u03bb q \u2192 not-just (! p \u2219 q)} (ap (coe e) (maybe-elim-just _ _)))\n \u2219 maybe-elim-just (coe e (coe e' (just x))) (\u03bb q \u2192 not-just (! p \u2219 q))\n \u2219 p)\n where\n p : coe e (coe e' (just x)) \u2261 just x\n p = coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n\n cto : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 Maybe A \u2192 Maybe B\n cto e = maybe (maybe\u2032 just (coe e nothing) \u2218 coe e \u2218 just) nothing\n\n module _ {A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}(e'\u2219e=id : e' \u2219 e \u2261 idp) where\n cto-cto : (x : Maybe B) \u2192 cto e (cto e' x) \u2261 x\n cto-cto nothing = idp\n cto-cto (just x) with coe e' (just x) | coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n cto-cto (just x) | just x\u2081 | p = ap (maybe just (coe e nothing)) p\n cto-cto (just x) | nothing | p with coe e' nothing\n | coe\u2218coe e e' nothing \u2219 coe-same e'\u2219e=id nothing\n cto-cto (just x) | nothing | p | just y | q = ap (maybe just (coe e nothing)) q \u2219 p\n cto-cto (just x) | nothing | p | nothing | q = ! q \u2219 p\n\n module _ {A B : Set} where\n create-PP-\u2243 : Maybe A \u2261 Maybe B \u2192 Maybe A \u2243 Maybe B\n create-PP-\u2243 e = equiv (cto e)\n (cto (! e))\n (cto-cto {e = e} {e' = ! e} (!\u2219 e))\n (cto-cto {e = ! e}{e' = e} (\u2219! e))\n\n create-PP : {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 \u03a3 (Maybe A \u2261 Maybe B) PreservePoint\n create-PP eq = ua (create-PP-\u2243 eq) , nothing-to-nothing _ (coe-\u03b2 (create-PP-\u2243 eq) nothing)\n\n Maybe-injective-PP : {{_ : UA}} \u2192 (e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e\n \u2192 A \u2261 B\n Maybe-injective-PP e e-pp = ua\n (equiv (to e e-pp)\n (to (! e) (!-PP e-pp))\n (to-to {e = e} { ! e} {e-pp}{ !-PP e-pp} (!\u2219 e))\n (to-to {e = ! e} {e} { !-PP e-pp} {e-pp} (\u2219! e)))\n\n Maybe-injective : \u2200 {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 A \u2261 B\n Maybe-injective e = let (e' , e'-pp) = create-PP e in Maybe-injective-PP e' e'-pp\n\n\nmodule _ {a}{A : \u2605_ a} where\n Maybe\u2243\ud835\udfd9\u228e : Maybe A \u2243 (\ud835\udfd9 \u228e A)\n Maybe\u2243\ud835\udfd9\u228e = equiv (maybe inr (inl _)) [inl: const nothing ,inr: just ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (maybe (\u03bb x \u2192 idp) idp)\n\n Maybe\u2261\ud835\udfd9\u228e : \u2200 {{_ : UA}}\u2192 Maybe A \u2261 (\ud835\udfd9 \u228e A)\n Maybe\u2261\ud835\udfd9\u228e = ua Maybe\u2243\ud835\udfd9\u228e\n\nmodule _ {{_ : UA}} where\n Fin0\u2261\ud835\udfd8 : Fin 0 \u2261 \ud835\udfd8\n Fin0\u2261\ud835\udfd8 = ua (equiv (\u03bb ()) (\u03bb ()) (\u03bb ()) (\u03bb ()))\n\n Fin1\u2261\ud835\udfd9 : Fin 1 \u2261 \ud835\udfd9\n Fin1\u2261\ud835\udfd9 = ua (equiv _ (\u03bb _ \u2192 zero) (\u03bb _ \u2192 idp) \u03b2)\n where \u03b2 : (_ : Fin 1) \u2192 _\n \u03b2 zero = idp\n \u03b2 (suc ())\n\nmodule _ where\n isZero? : \u2200 {n}{A : Fin (suc n) \u2192 Set} \u2192 ((i : Fin n) \u2192 A (suc i)) \u2192 A zero\n \u2192 (i : Fin (suc n)) \u2192 A i\n isZero? f x zero = x\n isZero? f x (suc i) = f i\n\n Fin\u2218suc\u2243\ud835\udfd9\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2243 (\ud835\udfd9 \u228e Fin n)\n Fin\u2218suc\u2243\ud835\udfd9\u228eFin = equiv (isZero? inr (inl _)) [inl: (\u03bb _ \u2192 zero) ,inr: suc ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n (isZero? (\u03bb _ \u2192 idp) idp)\n\n Fin\u2218suc\u2261\ud835\udfd9\u228eFin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 (\ud835\udfd9 \u228e Fin n)\n Fin\u2218suc\u2261\ud835\udfd9\u228eFin = ua Fin\u2218suc\u2243\ud835\udfd9\u228eFin\n\nFin-\u228e-+ : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u228e Fin n) \u2261 Fin (m \u2115.+ n)\nFin-\u228e-+ {zero} = ap\u2082 _\u228e_ Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm \u2219 ! \u228e\ud835\udfd8-inl\nFin-\u228e-+ {suc m} = ap\u2082 _\u228e_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ap (_\u228e_ \ud835\udfd9) Fin-\u228e-+ \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\n\nFin-\u00d7-* : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u00d7 Fin n) \u2261 Fin (m \u2115.* n)\nFin-\u00d7-* {zero} = ap\u2082 _\u00d7_ Fin0\u2261\ud835\udfd8 idp \u2219 \u03a3\ud835\udfd8-fst \u2219 ! Fin0\u2261\ud835\udfd8\nFin-\u00d7-* {suc m} = ap\u2082 _\u00d7_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u228e-\u00d7 \u2219 ap\u2082 _\u228e_ \u03a3\ud835\udfd9-snd Fin-\u00d7-* \u2219 Fin-\u228e-+\n\nFin-\u2192-^ : \u2200 {{_ : UA}}{{_ : FunExt}}{m n} \u2192 (Fin m \u2192 Fin n) \u2261 Fin (n \u2115.^ m)\nFin-\u2192-^ {zero} = ap\u2082 _\u27f6_ Fin0\u2261\ud835\udfd8 idp \u2219 \u03a0\ud835\udfd8-uniq\u2032 _ \u2219 \u228e\ud835\udfd8-inl \u2219 ap (_\u228e_ \ud835\udfd9) (! Fin0\u2261\ud835\udfd8)\n \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\nFin-\u2192-^ {suc m} = ap\u2082 _\u27f6_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u00d7-\u2192 \u2219 ap\u2082 _\u00d7_ (\u03a0\ud835\udfd9-uniq _) Fin-\u2192-^ \u2219 Fin-\u00d7-*\n\nFin\u2218suc\u2261Maybe\u2218Fin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 Maybe (Fin n)\nFin\u2218suc\u2261Maybe\u2218Fin = Fin\u2218suc\u2261\ud835\udfd9\u228eFin \u2219 ! Maybe\u2261\ud835\udfd9\u228e\n\nFin-injective : \u2200 {{_ : UA}}{m n} \u2192 Fin m \u2261 Fin n \u2192 m \u2261 n\nFin-injective {zero} {zero} Finm\u2261Finn = idp\nFin-injective {zero} {suc n} Finm\u2261Finn = \ud835\udfd8-elim (coe (! Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {zero} Finm\u2261Finn = \ud835\udfd8-elim (coe (Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {suc n} Finm\u2261Finn\n = ap suc (Fin-injective (Maybe-injective\n (! Fin\u2218suc\u2261Maybe\u2218Fin \u2219 Finm\u2261Finn \u2219 Fin\u2218suc\u2261Maybe\u2218Fin)))\n\nFin\u228e-injective : \u2200 {{_ : UA}}{A B : Set} n \u2192 (Fin n \u228e A) \u2261 (Fin n \u228e B) \u2192 A \u2261 B\nFin\u228e-injective zero f = \u228e\ud835\udfd8-inl \u2219 \u228e-comm \u2219 ap\u2082 _\u228e_ (! Fin0\u2261\ud835\udfd8) idp\n \u2219 f \u2219 (ap\u2082 _\u228e_ Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm) \u2219 ! \u228e\ud835\udfd8-inl\nFin\u228e-injective (suc n) f = Fin\u228e-injective n (Maybe-injective\n (Maybe\u2261\ud835\udfd9\u228e \u2219 \u228e-assoc \u2219 ap\u2082 _\u228e_ (! Fin\u2218suc\u2261\ud835\udfd9\u228eFin) idp \u2219 f\n \u2219 ap\u2082 _\u228e_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ! Maybe\u2261\ud835\udfd9\u228e))\n\nmodule _ {{_ : UA}} where\n Lift\u2261id : \u2200 {a} {A : \u2605_ a} \u2192 Lift {a} {a} A \u2261 A\n Lift\u2261id = ua (equiv lower lift (\u03bb _ \u2192 idp) (\u03bb { (lift x) \u2192 idp }))\n\n \u03a0\ud835\udfd9F\u2261F : \u2200 {\u2113} {F : \ud835\udfd9 \u2192 \u2605_ \u2113} \u2192 \u03a0 \ud835\udfd9 F \u2261 F _\n \u03a0\ud835\udfd9F\u2261F = ua (equiv (\u03bb x \u2192 x _) const (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\n \ud835\udfd9\u2192A\u2261A : \u2200 {\u2113} {A : \u2605_ \u2113} \u2192 (\ud835\udfd9 \u2192 A) \u2261 A\n \ud835\udfd9\u2192A\u2261A = \u03a0\ud835\udfd9F\u2261F\n\n not-\ud835\udfda\u2261\ud835\udfda : \ud835\udfda \u2261 \ud835\udfda\n not-\ud835\udfda\u2261\ud835\udfda = twist\n\n{-\nTODO ?\nMaybe\ud835\udfd8\u2194\ud835\udfd9 : Maybe \ud835\udfd8 \u2194 \ud835\udfd9\nMaybe\ud835\udfd8\u2194\ud835\udfd9 = A\u228e\ud835\udfd8\u2194A \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe^\ud835\udfd8\u2194Fin : \u2200 n \u2192 Maybe^ n \ud835\udfd8 \u2194 Fin n\nMaybe^\ud835\udfd8\u2194Fin zero = sym Fin0\u2194\ud835\udfd8\nMaybe^\ud835\udfd8\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\ud835\udfd8\u2194Fin n)\n\nMaybe^\ud835\udfd9\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \ud835\udfd9 \u2194 Fin (suc n)\nMaybe^\ud835\udfd9\u2194Fin1+ n = Maybe^\ud835\udfd8\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\ud835\udfd8\u2194\ud835\udfd9)\n\nMaybe-\u228e : \u2200 {a} {A B : \u2605 a} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e {a} = sym Maybe\u2194Lift\ud835\udfd9\u228e \u2218 \u228e-CMon.assoc (Lift {_} {a} \ud835\udfd9) _ _ \u2218 (Maybe\u2194Lift\ud835\udfd9\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \ud835\udfd8 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \ud835\udfd8\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n-}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"053d434b30937a1cd275add324655ea0c27625ee","subject":"induction on slightly different thing to make the ih match the premise, result of 19 july meeting","message":"induction on slightly different thing to make the ih match the premise, result of 19 july meeting\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"expandability.agda","new_file":"expandability.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import htype-decidable\n\nmodule expandability where\n -- lemma : \u2200 { \u0393 e \u03c4} \u2192\n -- \u0393 \u22a2 e => \u2987\u2988 \u2192\n -- \u0393 \u22a2 e <= \u03c4 ==> \u2987\u2988\n -- lemma wt = ASubsume wt TCHole1\n\n mutual\n expandability-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ]\n (\u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394)\n expandability-synth SConst = _ , _ , ESConst\n expandability-synth (SAsc {\u03c4 = \u03c4} wt)\n with expandability-ana wt\n ... | _ , _ , \u03c4' , D with htype-dec \u03c4 \u03c4'\n ... | Inl _ = _ , _ , ESAsc2 D\n ... | Inr x = _ , _ , ESAsc1 D x\n expandability-synth (SVar {n = n} x) = _ , _ , ESVar x\n expandability-synth (SAp wt1 MAHole wt2)\n with expandability-ana wt2\n ... | d2 , \u03942 , \u03c42 , D2 with expandability-ana (ASubsume wt1 TCHole1)\n ... | d1 , \u03941 , \u03c41 , D1 = _ , _ , ESAp1 {!!} wt1 D1 D2\n expandability-synth (SAp wt1 (MAArr {\u03c42 = \u03c42}) wt2)\n with expandability-synth wt1 | expandability-ana wt2\n ... | d1 , \u03941 , D1\n | d2 , \u03942 , \u03c42' , D2\n with htype-dec \u03c42 \u03c42'\n expandability-synth (SAp wt1 MAArr wt2) | d1 , \u03941 , D1 | d2 , \u03942 , \u03c42' , D2 | Inr neq = _ , _ , ESAp2 {!!} {!D1!} {!!} neq\n expandability-synth (SAp wt1 MAArr wt2) | d1 , \u03941 , D1 | d2 , \u03942 , \u03c42 , D2 | Inl refl = _ , _ , ESAp3 {!!} {!!} {!!}\n expandability-synth SEHole = _ , _ , ESEHole\n expandability-synth (SNEHole wt)\n with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESNEHole wt'\n expandability-synth (SLam x\u2081 wt)\n with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESLam x\u2081 wt'\n\n expandability-ana : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ] \u03a3[ \u03c4' \u2208 htyp ]\n (\u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394)\n expandability-ana {e = e} (ASubsume D x\u2081)\n with expandability-synth D\n -- these cases just pass through, but we need to pattern match so we can prove things aren't holes\n expandability-ana {e = c} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = e \u00b7: x} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = X x} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = \u00b7\u03bb x e} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = \u00b7\u03bb x [ x\u2081 ] e} (ASubsume D x\u2082) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2082\n expandability-ana {e = e1 \u2218 e2} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n -- the two holes are special-cased\n expandability-ana {e = \u2987\u2988[ x ]} (ASubsume _ _ ) | _ , _ , _ = _ , _ , _ , EAEHole\n expandability-ana {e = \u2987 e \u2988[ x ]} (ASubsume (SNEHole wt) _) | _ , _ , _ = _ , _ , _ , EANEHole (\u03c02( \u03c02 (expandability-synth wt)))\n -- the lambda cases\n expandability-ana (ALam x\u2081 MAHole wt)\n with expandability-ana wt\n ... | _ , _ , _ , D' = _ , _ , _ , EALamHole x\u2081 D'\n expandability-ana (ALam x\u2081 MAArr wt)\n with expandability-ana wt\n ... | _ , _\u202f, _ , D' = _ , _ , _ , EALam x\u2081 D'\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import htype-decidable\n\nmodule expandability where\n mutual\n expandability-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e => \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ]\n (\u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394)\n expandability-synth SConst = _ , _ , ESConst\n expandability-synth (SAsc {\u03c4 = \u03c4} wt)\n with expandability-ana wt\n ... | _ , _ , \u03c4' , D with htype-dec \u03c4 \u03c4'\n ... | Inl _ = _ , _ , ESAsc2 D\n ... | Inr x = _ , _ , ESAsc1 D x\n expandability-synth (SVar {n = n} x) = _ , _ , ESVar x\n expandability-synth (SAp wt1 MAHole wt2)\n with expandability-synth wt1 | expandability-ana wt2\n ... | d1 , \u03941 , D1\n | d2 , \u03942 , \u03c42 , D2 = _ , _ , ESAp1 {!!} wt1 {!!} D2\n expandability-synth (SAp wt1 (MAArr {\u03c42 = \u03c42}) wt2)\n with expandability-synth wt1 | expandability-ana wt2\n ... | d1 , \u03941 , D1\n | d2 , \u03942 , \u03c42' , D2\n with htype-dec \u03c42 \u03c42'\n expandability-synth (SAp wt1 MAArr wt2) | d1 , \u03941 , D1 | d2 , \u03942 , \u03c42' , D2 | Inr neq = _ , _ , ESAp2 {!!} {!!} {!!} neq\n expandability-synth (SAp wt1 MAArr wt2) | d1 , \u03941 , D1 | d2 , \u03942 , \u03c42 , D2 | Inl refl = _ , _ , ESAp3 {!!} {!!} {!!}\n expandability-synth SEHole = _ , _ , ESEHole\n expandability-synth (SNEHole wt)\n with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESNEHole wt'\n expandability-synth (SLam x\u2081 wt)\n with expandability-synth wt\n ... | d' , \u0394' , wt' = _ , _ , ESLam x\u2081 wt'\n\n expandability-ana : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} \u2192\n \u0393 \u22a2 e <= \u03c4 \u2192\n \u03a3[ d \u2208 dhexp ] \u03a3[ \u0394 \u2208 hctx ] \u03a3[ \u03c4' \u2208 htyp ]\n (\u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394)\n expandability-ana {e = e} (ASubsume D x\u2081)\n with expandability-synth D\n -- these cases just pass through, but we need to pattern match so we can prove things aren't holes\n expandability-ana {e = c} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = e \u00b7: x} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = X x} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = \u00b7\u03bb x e} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n expandability-ana {e = \u00b7\u03bb x [ x\u2081 ] e} (ASubsume D x\u2082) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2082\n expandability-ana {e = e1 \u2218 e2} (ASubsume D x\u2081) | _ , _ , D' = _ , _ , _ , EASubsume (\u03bb _ ()) (\u03bb _ _ ()) D' x\u2081\n -- the two holes are special-cased\n expandability-ana {e = \u2987\u2988[ x ]} (ASubsume _ _ ) | _ , _ , _ = _ , _ , _ , EAEHole\n expandability-ana {e = \u2987 e \u2988[ x ]} (ASubsume (SNEHole wt) _) | _ , _ , _ = _ , _ , _ , EANEHole (\u03c02( \u03c02 (expandability-synth wt)))\n -- the lambda cases\n expandability-ana (ALam x\u2081 MAHole wt)\n with expandability-ana wt\n ... | _ , _ , _ , D' = _ , _ , _ , EALamHole x\u2081 D'\n expandability-ana (ALam x\u2081 MAArr wt)\n with expandability-ana wt\n ... | _ , _\u202f, _ , D' = _ , _ , _ , EALam x\u2081 D'\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9a897b02de3ac334ef579145aaec743a52f84ecf","subject":"circuit: Some specs","message":"circuit: Some specs\n","repos":"crypto-agda\/crypto-agda","old_file":"circuit.agda","new_file":"circuit.agda","new_contents":"module circuit where\n\nopen import Function\nopen import Data.Nat.NP hiding (_\u225f_; compare)\nopen import Data.Bits\nopen import Data.Bits.Bits2\nopen import Data.Bool hiding (_\u225f_)\nopen import Data.Product hiding (swap; map)\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc; inject+; raise; #_)\nopen import Data.List using (List; []; _\u2237_)\nopen import Data.Vec.NP as Vec using (Vec; []; _\u2237_; foldr; _[_]\u2254_; lookup; _++_; splitAt; tabulate; allFin; concat; ++-decomp) renaming (map to vmap)\nopen import Data.Vec.Properties\nopen import Relation.Nullary.Decidable hiding (map)\nopen import Relation.Binary.PropositionalEquality\n\nCircuitType : Set\u2081\nCircuitType = (i o : \u2115) \u2192 Set\n\nRunCircuit : CircuitType \u2192 Set\nRunCircuit C = \u2200 {i o} \u2192 C i o \u2192 Bits i \u2192 Bits o\n\nmodule Rewire where\n RewireFun : CircuitType\n RewireFun i o = Fin o \u2192 Fin i\n\n RewireTbl : CircuitType\n RewireTbl i o = Vec (Fin i) o\n\n rewire : \u2200 {a i o} {A : Set a} \u2192 (Fin o \u2192 Fin i) \u2192 Vec A i \u2192 Vec A o\n rewire f v = tabulate (flip lookup v \u2218 f)\n\n rewireTbl : \u2200 {a i o} {A : Set a} \u2192 RewireTbl i o \u2192 Vec A i \u2192 Vec A o\n rewireTbl tbl v = vmap (flip lookup v) tbl\n\n runRewireFun : RunCircuit RewireFun\n runRewireFun = rewire\n\n runRewireTbl : RunCircuit RewireTbl\n runRewireTbl = rewireTbl\n\nopen Rewire using (RewireTbl; RewireFun)\n\nrecord RewiringBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n\n infixr 1 _>>>_\n infixr 3 _***_\n\n field\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n\n rewireWithTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 C i o\n rewireWithTbl = rewire \u2218 flip lookup\n\n idCDefault : \u2200 {i} \u2192 C i i\n idCDefault = rewire id\n\n field\n idC : \u2200 {i} \u2192 C i i\n\n field\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) is \u2192 is =[ rewire r ]= Rewire.rewire r is\n\n{-\n _>>>-spec_ : \u2200 {i m o} {c\u2080 : C i m} {c\u2081 : C m o} {is ms os} \u2192\n is =[ c\u2080 ]= ms \u2192 ms =[ c\u2081 ]= os \u2192 is =[ c\u2080 >>> c\u2081 ]= os\n\n _***-spec_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} {c\u2080 : C i\u2080 o\u2080} {c\u2081 : C i\u2081 o\u2081} {is\u2080 is\u2081 os\u2080 os\u2081} \u2192\n is\u2080 =[ c\u2080 ]= os\u2080 \u2192 is\u2081 =[ c\u2081 ]= os\u2081 \u2192 (is\u2080 ++ is\u2081) =[ c\u2080 *** c\u2081 ]= (os\u2080 ++ os\u2081)\n\n-}\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idC ]= bs\n{-\n rewireWithTbl-spec : \u2200 {i o} (t : RewireTbl i o) is\n \u2192 is =[ rewireWithTbl t ]= Rewire.rewireTbl t is\n rewireWithTbl-spec t is = {!rewire-spec ? ?!}\n-}\n\n idCDefault-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idCDefault ]= bs\n idCDefault-spec bs\n = subst (\u03bb bs' \u2192 bs =[ idCDefault ]= bs') (tabulate\u2218lookup bs) (rewire-spec id bs)\n\n sink : \u2200 i \u2192 C i 0\n sink _ = rewire (\u03bb())\n-- sink-spec : bs =[ sink i ]= []\n\n dup\u2081 : \u2200 o \u2192 C 1 o\n dup\u2081 _ = rewire (const zero)\n-- dup\u2081-spec : (b \u2237 []) =[ dup o ]= replicate o\n\n dup\u2081\u00b2 : C 1 2\n dup\u2081\u00b2 = dup\u2081 2\n-- dup\u2081\u00b2-spec : (b \u2237 []) =[ dup\u2081\u00b2 ]= (b \u2237 b \u2237 [])\n\n vcat : \u2200 {i o n} \u2192 Vec (C i o) n \u2192 C (n * i) (n * o)\n vcat [] = idC\n vcat (x \u2237 xs) = x *** vcat xs\n\n coerce : \u2200 {i o} \u2192 i \u2261 o \u2192 C i o\n coerce refl = idC\n\n{-\n coerce-spec : \u2200 {i o} {i\u2261o : i \u2261 o} {is} \u2192\n is =[ coerce i\u2261o ]= subst Bits i\u2261o is\n coerce-spec = {!!}\n-}\n\n {-\n Sends inputs 0,1,2,...,n to outputs 0,1,2,...,n,0,1,2,...,n,0,1,2,...,n,...\n -}\n dup\u207f : \u2200 {i} k \u2192 C i (k * i)\n dup\u207f {i} k = rewireWithTbl (concat (replicate {n = k} (allFin i)))\n\n{-\n dup\u207f-spec : \u2200 {i} {is : Bits i} k \u2192 is =[ dup\u207f k ]= concat (replicate {n = k} is)\n dup\u207f-spec {i} {is} k = {!rewireWithTbl-spec (concat (replicate {n = k} (allFin _))) ?!}\n-}\n\n {-\n Sends inputs 0,1,2,...,n to outputs 0,0,0,...,1,1,1,...,2,2,2,...,n,n,n,...\n -}\n dup\u207f\u2032 : \u2200 {i} k \u2192 C i (i * k)\n dup\u207f\u2032 {i} k = rewireWithTbl (concat (vmap replicate (allFin i)))\n{-\n dup\u207f\u2032 {i} k = coerce (proj\u2082 \u2115\u00b0.*-identity i) >>>\n (vcat {n = i} (replicate (dup\u2081 _)))\n-}\n\n dup\u00b2 : \u2200 {i} \u2192 C i (i + i)\n dup\u00b2 {i} = dup\u207f 2 >>> coerce (cong (_+_ i) (sym (\u2115\u00b0.+-comm 0 i)))\n\n{-\n dup\u00b2-spec : \u2200 {n} {is : Bits n} \u2192 is =[ dup\u00b2 ]= (is ++ is)\n dup\u00b2-spec = coerce-spec (rewireWithTbl-spec {!!} {!!})\n-}\n\n _&&&_ : \u2200 {i o\u2080 o\u2081} \u2192 C i o\u2080 \u2192 C i o\u2081 \u2192 C i (o\u2080 + o\u2081)\n c\u2080 &&& c\u2081 = dup\u00b2 >>> c\u2080 *** c\u2081\n\n{-\n _&&&-spec_ : \u2200 {i o\u2080 o\u2081} {c\u2080 : C i o\u2080} {c\u2081 : C i o\u2081} {is os\u2080 os\u2081}\n \u2192 is =[ c\u2080 ]= os\u2080 \u2192 is =[ c\u2081 ]= os\u2081 \u2192 is =[ c\u2080 &&& c\u2081 ]= (os\u2080 ++ os\u2081)\n pf\u2080 &&&-spec pf\u2081 = dup\u00b2-spec >>>-spec (pf\u2080 ***-spec pf\u2081)\n-}\n\n ext-before : \u2200 {k i o} \u2192 C i o \u2192 C (k + i) (k + o)\n ext-before {k} c = idC {k} *** c\n\n ext-after : \u2200 {k i o} \u2192 C i o \u2192 C (i + k) (o + k)\n ext-after c = c *** idC\n\n commC : \u2200 m n \u2192 C (m + n) (n + m)\n commC m n = rewireWithTbl (vmap (raise m) (allFin n) ++ vmap (inject+ n) (allFin m))\n\n dropC : \u2200 {i} k \u2192 C (k + i) i\n dropC k = sink k *** idC\n\n takeC : \u2200 {i} k \u2192 C (k + i) k\n takeC {i} k = commC k _ >>> dropC i\n\n swap : \u2200 {i} (x y : Fin i) \u2192 C i i\n -- swap x y = arr (\u03bb xs \u2192 (xs [ x ]\u2254 (lookup y xs)) [ y ]\u2254 (lookup x xs))\n swap x y = rewire (Fin.swap x y)\n\n rev : \u2200 {i} \u2192 C i i\n rev = rewire Fin.reverse\n\n swap\u2082 : C 2 2\n swap\u2082 = rev\n -- swap\u2082 = swap (# 0) (# 1)\n\n Perm : \u2115 \u2192 Set\n Perm n = List (Fin n \u00d7 Fin n)\n\n perm : \u2200 {i} \u2192 Perm i \u2192 C i i\n perm [] = idC\n perm ((x , y) \u2237 \u03c0) = swap x y >>> perm \u03c0\n\n headC : \u2200 {i} \u2192 C (1 + i) 1\n headC = takeC 1\n\n tailC : \u2200 {i} \u2192 C (1 + i) i\n tailC = dropC 1\n\nrecord CircuitBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n field\n isRewiringBuilder : RewiringBuilder C\n arr : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n leafC : \u2200 {o} \u2192 Bits o \u2192 C 0 o\n forkC : \u2200 {i o} (c\u2080 c\u2081 : C i o) \u2192 C (1 + i) o\n\n open RewiringBuilder isRewiringBuilder\n\n{-\n field\n leafC-spec : \u2200 {o} (os : Bits o) \u2192 [] =[ leafC os ]= os\n forkC-left-spec : \u2200 {i o} {c\u2080 c\u2081 : C i o} {is os}\n \u2192 is =[ c\u2080 ]= os \u2192 (0\u2237 is) =[ forkC c\u2080 c\u2081 ]= os\n forkC-right-spec : \u2200 {i o} {c\u2080 c\u2081 : C i o} {is os}\n \u2192 is =[ c\u2081 ]= os \u2192 (1\u2237 is) =[ forkC c\u2080 c\u2081 ]= os\n-}\n bit : Bit \u2192 C 0 1\n bit b = leafC (b \u2237 [])\n\n{-\n bit-spec : \u2200 b \u2192 [] =[ bit b ]= (b \u2237 [])\n bit-spec b = leafC-spec (b \u2237 [])\n-}\n\n 0\u02b7 : C 0 1\n 0\u02b7 = bit 0b\n\n 0\u02b7\u207f : \u2200 {o} \u2192 C 0 o\n 0\u02b7\u207f = leafC 0\u207f\n\n{-\n 0\u02b7-spec : [] =[ 0\u02b7 ]= 0\u2237 []\n 0\u02b7-spec = bit-spec 0b\n-}\n\n 1\u02b7 : C 0 1\n 1\u02b7 = bit 1b\n\n 1\u02b7\u207f : \u2200 {o} \u2192 C 0 o\n 1\u02b7\u207f = leafC 1\u207f\n\n{-\n 1\u02b7-spec : [] =[ 1\u02b7 ]= 1\u2237 []\n 1\u02b7-spec = bit-spec 1b\n-}\n\n padL : \u2200 {i} k \u2192 C i (k + i)\n padL k = 0\u02b7\u207f {k} *** idC\n\n padR : \u2200 {i} k \u2192 C i (i + k)\n padR k = padL k >>> commC k _\n\n arr' : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n arr' {zero} f = leafC (f [])\n arr' {suc i} f = forkC (arr' {i} (f \u2218 0\u2237_)) (arr' (f \u2218 1\u2237_))\n\n unOp : (Bit \u2192 Bit) \u2192 C 1 1\n unOp op = arr (\u03bb { (x \u2237 []) \u2192 (op x) \u2237 [] })\n\n notC : C 1 1\n notC = unOp not\n\n binOp : (Bit \u2192 Bit \u2192 Bit) \u2192 C 2 1\n binOp op = arr (\u03bb { (x \u2237 y \u2237 []) \u2192 (op x y) \u2237 [] })\n\n terOp : (Bit \u2192 Bit \u2192 Bit \u2192 Bit) \u2192 C 3 1\n terOp op = arr (\u03bb { (x \u2237 y \u2237 z \u2237 []) \u2192 (op x y z) \u2237 [] })\n\n xorC : C 2 1\n xorC = binOp _xor_\n\n eqC : C 2 1\n eqC = binOp _==\u1d47_\n\n orC : C 2 1\n orC = binOp _\u2228_\n\n andC : C 2 1\n andC = binOp _\u2227_\n\n norC : C 2 1\n norC = binOp (\u03bb x y \u2192 not (x \u2228 y))\n\n nandC : C 2 1\n nandC = binOp (\u03bb x y \u2192 not (x \u2227 y))\n\n if\u27e8head=0\u27e9then_else_ : \u2200 {i o} (c\u2080 c\u2081 : C i o) \u2192 C (1 + i) o\n if\u27e8head=0\u27e9then_else_ = forkC\n\n -- Base addition with carry:\n -- * Any input can be used as the carry\n -- * First output is the carry\n --\n -- This can also be seen as the addition of\n -- three bits which result is between 0 and 3\n -- and thus fits in two bit word in binary\n -- representation.\n --\n -- Alternatively you can see it as counting the\n -- number of 1s in a three bits vector.\n add\u2082 : C 3 2\n add\u2082 = carry &&& result\n where carry : C 3 1\n carry = terOp (\u03bb x y z \u2192 x xor (y \u2227 z))\n result : C 3 1\n result = terOp (\u03bb x y z \u2192 x xor (y xor z))\n\n open RewiringBuilder isRewiringBuilder public\n\n_\u2192\u1d47_ : CircuitType\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n_\u2192\u1da0_ : CircuitType\ni \u2192\u1da0 o = Fin o \u2192 Fin i\n\nfinFunRewiringBuilder : RewiringBuilder _\u2192\u1da0_\nfinFunRewiringBuilder = mk id _\u2218\u2032_ _***_ id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1da0_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = Rewire.rewire f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec = tabulate\u2218lookup\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n _***_ {o\u2080 = o\u2080} f g x with Fin.cmp o\u2080 _ x\n _***_ f _ ._ | Fin.bound x = inject+ _ (f x)\n _***_ {i\u2080} _ g ._ | Fin.free x = raise i\u2080 (g x)\n\ntblRewiringBuilder : RewiringBuilder RewireTbl\ntblRewiringBuilder = mk tabulate _>>>_ _***_ (allFin _) _=[_]=_ rewire-spec idC-spec\n where\n open Rewire\n C = RewireTbl\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tbl ]= output = Rewire.rewireTbl tbl input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ tabulate r ]= Rewire.rewire r bs\n rewire-spec r bs = sym (tabulate-\u2218 (flip lookup bs) r)\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ allFin _ ]= bs\n -- idC-spec = map-lookup-allFin\n idC-spec bs rewrite rewire-spec id bs = tabulate\u2218lookup bs\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n c\u2080 >>> c\u2081 = rewireTbl c\u2081 c\u2080\n -- c\u2080 >>> c\u2081 = tabulate (flip lookup c\u2080 \u2218 flip lookup c\u2081)\n -- c\u2080 >>> c\u2081 = vmap (flip lookup c\u2080) c\u2081\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n _***_ {i\u2080} c\u2080 c\u2081 = vmap (inject+ _) c\u2080 ++ vmap (raise i\u2080) c\u2081\n\nbitsFunRewiringBuilder : RewiringBuilder _\u2192\u1d47_\nbitsFunRewiringBuilder = mk Rewire.rewire _>>>_ _***_ id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1d47_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ Rewire.rewire r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec bs = refl\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n f >>> g = g \u2218 f\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n (f *** g) xs with splitAt _ xs\n ... | ys , zs , _ = f ys ++ g zs\n\nbitsFunCircuitBuilder : CircuitBuilder _\u2192\u1d47_\nbitsFunCircuitBuilder = mk bitsFunRewiringBuilder id (\u03bb { bs [] \u2192 bs }) (\u03bb { f g (b \u2237 bs) \u2192 (if b then f else g) bs })\n\nmodule BitsFunExtras where\n open CircuitBuilder bitsFunCircuitBuilder\n C = _\u2192\u1d47_\n >>>-spec : \u2200 {i m o} (c\u2080 : C i m) (c\u2081 : C m o) xs \u2192 (c\u2080 >>> c\u2081) xs \u2261 c\u2081 (c\u2080 xs)\n >>>-spec _ _ _ = refl\n ***-spec : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} (c\u2080 : C i\u2080 o\u2080) (c\u2081 : C i\u2081 o\u2081) xs {ys}\n \u2192 (c\u2080 *** c\u2081) (xs ++ ys) \u2261 c\u2080 xs ++ c\u2081 ys\n ***-spec {i\u2080} c\u2080 c\u2081 xs {ys} with splitAt i\u2080 (xs ++ ys)\n ... | pre , post , eq with ++-decomp {xs = xs} {pre} {ys} {post} eq\n ... | eq1 , eq2 rewrite eq1 | eq2 = refl\n\nopen import bintree\nopen import flipbased-tree\n\nTreeBits : CircuitType\nTreeBits i o = Tree (Bits o) i\n\nmodule moretree where\n _>>>_ : \u2200 {m n a} {A : Set a} \u2192 Tree (Bits m) n \u2192 Tree A m \u2192 Tree A n\n f >>> g = map (flip bintree.lookup g) f\n\ntreeBitsRewiringBuilder : RewiringBuilder TreeBits\ntreeBitsRewiringBuilder = mk rewire moretree._>>>_ _***_ (rewire id) _=[_]=_ rewire-spec idC-spec\n where\n C = TreeBits\n\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n rewire f = fromFun (Rewire.rewire f)\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tree ]= output = toFun tree input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ rewire r ]= Rewire.rewire r bs\n rewire-spec r = toFun\u2218fromFun (tabulate \u2218 flip (Vec.lookup \u2218 r))\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ rewire id ]= bs\n idC-spec bs rewrite toFun\u2218fromFun (tabulate \u2218 flip Vec.lookup) bs | tabulate\u2218lookup bs = refl\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n (f *** g) = f >>= \u03bb xs \u2192 map (_++_ xs) g\n\ntreeBitsCircuitBuilder : CircuitBuilder TreeBits\ntreeBitsCircuitBuilder = mk treeBitsRewiringBuilder arr leaf fork\n where\n C = TreeBits\n\n arr : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n arr = fromFun\n\nRewiringTree : CircuitType\nRewiringTree i o = Tree (Fin i) o\n\nmodule RewiringWith2^Outputs where\n C_\u27e82^_\u27e9 = RewiringTree\n\n rewire : \u2200 {i o} \u2192 RewireFun i (2 ^ o) \u2192 C i \u27e82^ o \u27e9\n rewire f = fromFun (f \u2218 toFin)\n\n lookupFin : \u2200 {i o} \u2192 C i \u27e82^ o \u27e9 \u2192 Fin (2 ^ o) \u2192 Fin i\n lookupFin c x = bintree.lookup (fromFin x) c\n\n _>>>_ : \u2200 {i m o} \u2192 C i \u27e82^ m \u27e9 \u2192 C (2 ^ m) \u27e82^ o \u27e9 \u2192 C i \u27e82^ o \u27e9\n f >>> g = rewire (lookupFin f \u2218 lookupFin g)\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 \u27e82^ o\u2080 \u27e9 \u2192 C i\u2081 \u27e82^ o\u2081 \u27e9 \u2192 C (i\u2080 + i\u2081) \u27e82^ (o\u2080 + o\u2081) \u27e9\n f *** g = f >>= \u03bb x \u2192 map (Fin._+\u2032_ x) g\n\nmodule Test where\n open import Data.Bits.Bits4\n\n tbl : RewireTbl 4 4\n tbl = # 1 \u2237 # 0 \u2237 # 2 \u2237 # 2 \u2237 []\n\n fun : RewireFun 4 4\n fun zero = # 1\n fun (suc zero) = # 0\n fun (suc (suc zero)) = # 2\n fun (suc (suc (suc x))) = # 2\n\n-- swap x y \u2248 swap y x\n-- reverse \u2218 reverse \u2248 id\n\n abs : \u2200 {C} \u2192 RewiringBuilder C \u2192 C 4 4\n abs builder = swap\u2082 *** dup\u2081\u00b2 *** sink 1\n where open RewiringBuilder builder\n\n tinytree : Tree (Fin 4) 2\n tinytree = fork (fork (leaf (# 1)) (leaf (# 0))) (fork (leaf (# 2)) (leaf (# 2)))\n\n bigtree : Tree (Bits 4) 4\n bigtree = fork (fork (fork (same 0000b) (same 0011b)) (fork (same 1000b) (same 1011b)))\n (fork (fork (same 0100b) (same 0111b)) (fork (same 1100b) (same 1111b)))\n where same : \u2200 {n} {A : Set} \u2192 A \u2192 Tree A (suc n)\n same x = fork (leaf x) (leaf x)\n\n test\u2081 : tbl \u2261 tabulate fun\n test\u2081 = refl\n\n test\u2082 : tbl \u2261 abs tblRewiringBuilder\n test\u2082 = refl\n\n test\u2083 : tabulate fun \u2261 tabulate (abs finFunRewiringBuilder)\n test\u2083 = refl\n\n test\u2084 : bigtree \u2261 abs treeBitsRewiringBuilder\n test\u2084 = refl\n\n -- open RewiringWith2^Outputs\n -- test\u2085 : tabulate (lookupFin tinytree) \u2261 tbl\n -- test\u2085 = refl\n","old_contents":"module circuit where\n\nopen import Function\nopen import Data.Nat.NP hiding (_\u225f_; compare)\nopen import Data.Bits\nopen import Data.Bits.Bits2\nopen import Data.Bool hiding (_\u225f_)\nopen import Data.Product hiding (swap; map)\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc; inject+; raise; #_)\nopen import Data.List using (List; []; _\u2237_)\nopen import Data.Vec using (Vec; []; _\u2237_; foldr; _[_]\u2254_; lookup; _++_; splitAt; tabulate; allFin; concat) renaming (map to vmap)\nopen import Data.Vec.Properties\nopen import Relation.Nullary.Decidable hiding (map)\nopen import Relation.Binary.PropositionalEquality\n\nCircuitType : Set\u2081\nCircuitType = (i o : \u2115) \u2192 Set\n\nRunCircuit : CircuitType \u2192 Set\nRunCircuit C = \u2200 {i o} \u2192 C i o \u2192 Bits i \u2192 Bits o\n\nmodule Rewire where\n RewireFun : CircuitType\n RewireFun i o = Fin o \u2192 Fin i\n\n RewireTbl : CircuitType\n RewireTbl i o = Vec (Fin i) o\n\n rewire : \u2200 {a i o} {A : Set a} \u2192 (Fin o \u2192 Fin i) \u2192 Vec A i \u2192 Vec A o\n rewire f v = tabulate (flip lookup v \u2218 f)\n\n rewireTbl : \u2200 {a i o} {A : Set a} \u2192 RewireTbl i o \u2192 Vec A i \u2192 Vec A o\n rewireTbl tbl v = vmap (flip lookup v) tbl\n\n runRewireFun : RunCircuit RewireFun\n runRewireFun = rewire\n\n runRewireTbl : RunCircuit RewireTbl\n runRewireTbl = rewireTbl\n\nopen Rewire using (RewireTbl; RewireFun)\n\nrecord RewiringBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n\n infixr 1 _>>>_\n infixr 3 _***_\n\n field\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n\n rewireWithTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 C i o\n rewireWithTbl = rewire \u2218 flip lookup\n\n idCDefault : \u2200 {i} \u2192 C i i\n idCDefault = rewire id\n\n field\n idC : \u2200 {i} \u2192 C i i\n\n field\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) is \u2192 is =[ rewire r ]= Rewire.rewire r is\n\n{-\n _>>>-spec_ : \u2200 {i m o} {c\u2080 : C i m} {c\u2081 : C m o} {is ms os} \u2192\n is =[ c\u2080 ]= ms \u2192 ms =[ c\u2081 ]= os \u2192 is =[ c\u2080 >>> c\u2081 ]= os\n\n _***-spec_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} {c\u2080 : C i\u2080 o\u2080} {c\u2081 : C i\u2081 o\u2081} {is\u2080 is\u2081 os\u2080 os\u2081} \u2192\n is\u2080 =[ c\u2080 ]= os\u2080 \u2192 is\u2081 =[ c\u2081 ]= os\u2081 \u2192 (is\u2080 ++ is\u2081) =[ c\u2080 *** c\u2081 ]= (os\u2080 ++ os\u2081)\n\n-}\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idC ]= bs\n{-\n rewireWithTbl-spec : \u2200 {i o} (t : RewireTbl i o) is\n \u2192 is =[ rewireWithTbl t ]= Rewire.rewireTbl t is\n rewireWithTbl-spec t is = {!rewire-spec ? ?!}\n-}\n\n idCDefault-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ idCDefault ]= bs\n idCDefault-spec bs\n = subst (\u03bb bs' \u2192 bs =[ idCDefault ]= bs') (tabulate\u2218lookup bs) (rewire-spec id bs)\n\n sink : \u2200 i \u2192 C i 0\n sink _ = rewire (\u03bb())\n-- sink-spec : bs =[ sink i ]= []\n\n dup\u2081 : \u2200 o \u2192 C 1 o\n dup\u2081 _ = rewire (const zero)\n-- dup\u2081-spec : (b \u2237 []) =[ dup o ]= replicate o\n\n dup\u2081\u00b2 : C 1 2\n dup\u2081\u00b2 = dup\u2081 2\n-- dup\u2081\u00b2-spec : (b \u2237 []) =[ dup\u2081\u00b2 ]= (b \u2237 b \u2237 [])\n\n vcat : \u2200 {i o n} \u2192 Vec (C i o) n \u2192 C (n * i) (n * o)\n vcat [] = idC\n vcat (x \u2237 xs) = x *** vcat xs\n\n coerce : \u2200 {i o} \u2192 i \u2261 o \u2192 C i o\n coerce refl = idC\n\n{-\n coerce-spec : \u2200 {i o} {i\u2261o : i \u2261 o} {is} \u2192\n is =[ coerce i\u2261o ]= subst Bits i\u2261o is\n coerce-spec = {!!}\n-}\n\n {-\n Sends inputs 0,1,2,...,n to outputs 0,1,2,...,n,0,1,2,...,n,0,1,2,...,n,...\n -}\n dup\u207f : \u2200 {i} k \u2192 C i (k * i)\n dup\u207f {i} k = rewireWithTbl (concat (replicate {n = k} (allFin i)))\n\n{-\n dup\u207f-spec : \u2200 {i} {is : Bits i} k \u2192 is =[ dup\u207f k ]= concat (replicate {n = k} is)\n dup\u207f-spec {i} {is} k = {!rewireWithTbl-spec (concat (replicate {n = k} (allFin _))) ?!}\n-}\n\n {-\n Sends inputs 0,1,2,...,n to outputs 0,0,0,...,1,1,1,...,2,2,2,...,n,n,n,...\n -}\n dup\u207f\u2032 : \u2200 {i} k \u2192 C i (i * k)\n dup\u207f\u2032 {i} k = rewireWithTbl (concat (vmap replicate (allFin i)))\n{-\n dup\u207f\u2032 {i} k = coerce (proj\u2082 \u2115\u00b0.*-identity i) >>>\n (vcat {n = i} (replicate (dup\u2081 _)))\n-}\n\n dup\u00b2 : \u2200 {i} \u2192 C i (i + i)\n dup\u00b2 {i} = dup\u207f 2 >>> coerce (cong (_+_ i) (sym (\u2115\u00b0.+-comm 0 i)))\n\n{-\n dup\u00b2-spec : \u2200 {n} {is : Bits n} \u2192 is =[ dup\u00b2 ]= (is ++ is)\n dup\u00b2-spec = coerce-spec (rewireWithTbl-spec {!!} {!!})\n-}\n\n _&&&_ : \u2200 {i o\u2080 o\u2081} \u2192 C i o\u2080 \u2192 C i o\u2081 \u2192 C i (o\u2080 + o\u2081)\n c\u2080 &&& c\u2081 = dup\u00b2 >>> c\u2080 *** c\u2081\n\n{-\n _&&&-spec_ : \u2200 {i o\u2080 o\u2081} {c\u2080 : C i o\u2080} {c\u2081 : C i o\u2081} {is os\u2080 os\u2081}\n \u2192 is =[ c\u2080 ]= os\u2080 \u2192 is =[ c\u2081 ]= os\u2081 \u2192 is =[ c\u2080 &&& c\u2081 ]= (os\u2080 ++ os\u2081)\n pf\u2080 &&&-spec pf\u2081 = dup\u00b2-spec >>>-spec (pf\u2080 ***-spec pf\u2081)\n-}\n\n ext-before : \u2200 {k i o} \u2192 C i o \u2192 C (k + i) (k + o)\n ext-before {k} c = idC {k} *** c\n\n ext-after : \u2200 {k i o} \u2192 C i o \u2192 C (i + k) (o + k)\n ext-after c = c *** idC\n\n commC : \u2200 m n \u2192 C (m + n) (n + m)\n commC m n = rewireWithTbl (vmap (raise m) (allFin n) ++ vmap (inject+ n) (allFin m))\n\n dropC : \u2200 {i} k \u2192 C (k + i) i\n dropC k = sink k *** idC\n\n takeC : \u2200 {i} k \u2192 C (k + i) k\n takeC {i} k = commC k _ >>> dropC i\n\n swap : \u2200 {i} (x y : Fin i) \u2192 C i i\n -- swap x y = arr (\u03bb xs \u2192 (xs [ x ]\u2254 (lookup y xs)) [ y ]\u2254 (lookup x xs))\n swap x y = rewire (Fin.swap x y)\n\n rev : \u2200 {i} \u2192 C i i\n rev = rewire Fin.reverse\n\n swap\u2082 : C 2 2\n swap\u2082 = rev\n -- swap\u2082 = swap (# 0) (# 1)\n\n Perm : \u2115 \u2192 Set\n Perm n = List (Fin n \u00d7 Fin n)\n\n perm : \u2200 {i} \u2192 Perm i \u2192 C i i\n perm [] = idC\n perm ((x , y) \u2237 \u03c0) = swap x y >>> perm \u03c0\n\n headC : \u2200 {i} \u2192 C (1 + i) 1\n headC = takeC 1\n\n tailC : \u2200 {i} \u2192 C (1 + i) i\n tailC = dropC 1\n\nrecord CircuitBuilder (C : CircuitType) : Set\u2081 where\n constructor mk\n field\n isRewiringBuilder : RewiringBuilder C\n arr : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n leafC : \u2200 {o} \u2192 Bits o \u2192 C 0 o\n forkC : \u2200 {i o} (c\u2080 c\u2081 : C i o) \u2192 C (1 + i) o\n\n open RewiringBuilder isRewiringBuilder\n\n{-\n field\n leafC-spec : \u2200 {o} (os : Bits o) \u2192 [] =[ leafC os ]= os\n forkC-left-spec : \u2200 {i o} {c\u2080 c\u2081 : C i o} {is os}\n \u2192 is =[ c\u2080 ]= os \u2192 (0\u2237 is) =[ forkC c\u2080 c\u2081 ]= os\n forkC-right-spec : \u2200 {i o} {c\u2080 c\u2081 : C i o} {is os}\n \u2192 is =[ c\u2081 ]= os \u2192 (1\u2237 is) =[ forkC c\u2080 c\u2081 ]= os\n-}\n bit : Bit \u2192 C 0 1\n bit b = leafC (b \u2237 [])\n\n{-\n bit-spec : \u2200 b \u2192 [] =[ bit b ]= (b \u2237 [])\n bit-spec b = leafC-spec (b \u2237 [])\n-}\n\n 0\u02b7 : C 0 1\n 0\u02b7 = bit 0b\n\n 0\u02b7\u207f : \u2200 {o} \u2192 C 0 o\n 0\u02b7\u207f = leafC 0\u207f\n\n{-\n 0\u02b7-spec : [] =[ 0\u02b7 ]= 0\u2237 []\n 0\u02b7-spec = bit-spec 0b\n-}\n\n 1\u02b7 : C 0 1\n 1\u02b7 = bit 1b\n\n 1\u02b7\u207f : \u2200 {o} \u2192 C 0 o\n 1\u02b7\u207f = leafC 1\u207f\n\n{-\n 1\u02b7-spec : [] =[ 1\u02b7 ]= 1\u2237 []\n 1\u02b7-spec = bit-spec 1b\n-}\n\n padL : \u2200 {i} k \u2192 C i (k + i)\n padL k = 0\u02b7\u207f {k} *** idC\n\n padR : \u2200 {i} k \u2192 C i (i + k)\n padR k = padL k >>> commC k _\n\n arr' : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n arr' {zero} f = leafC (f [])\n arr' {suc i} f = forkC (arr' {i} (f \u2218 0\u2237_)) (arr' (f \u2218 1\u2237_))\n\n unOp : (Bit \u2192 Bit) \u2192 C 1 1\n unOp op = arr (\u03bb { (x \u2237 []) \u2192 (op x) \u2237 [] })\n\n notC : C 1 1\n notC = unOp not\n\n binOp : (Bit \u2192 Bit \u2192 Bit) \u2192 C 2 1\n binOp op = arr (\u03bb { (x \u2237 y \u2237 []) \u2192 (op x y) \u2237 [] })\n\n terOp : (Bit \u2192 Bit \u2192 Bit \u2192 Bit) \u2192 C 3 1\n terOp op = arr (\u03bb { (x \u2237 y \u2237 z \u2237 []) \u2192 (op x y z) \u2237 [] })\n\n xorC : C 2 1\n xorC = binOp _xor_\n\n eqC : C 2 1\n eqC = binOp _==\u1d47_\n\n orC : C 2 1\n orC = binOp _\u2228_\n\n andC : C 2 1\n andC = binOp _\u2227_\n\n norC : C 2 1\n norC = binOp (\u03bb x y \u2192 not (x \u2228 y))\n\n nandC : C 2 1\n nandC = binOp (\u03bb x y \u2192 not (x \u2227 y))\n\n if\u27e8head=0\u27e9then_else_ : \u2200 {i o} (c\u2080 c\u2081 : C i o) \u2192 C (1 + i) o\n if\u27e8head=0\u27e9then_else_ = forkC\n\n -- Base addition with carry:\n -- * Any input can be used as the carry\n -- * First output is the carry\n --\n -- This can also be seen as the addition of\n -- three bits which result is between 0 and 3\n -- and thus fits in two bit word in binary\n -- representation.\n --\n -- Alternatively you can see it as counting the\n -- number of 1s in a three bits vector.\n add\u2082 : C 3 2\n add\u2082 = carry &&& result\n where carry : C 3 1\n carry = terOp (\u03bb x y z \u2192 x xor (y \u2227 z))\n result : C 3 1\n result = terOp (\u03bb x y z \u2192 x xor (y xor z))\n\n open RewiringBuilder isRewiringBuilder public\n\n_\u2192\u1d47_ : CircuitType\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n_\u2192\u1da0_ : CircuitType\ni \u2192\u1da0 o = Fin o \u2192 Fin i\n\nfinFunRewiringBuilder : RewiringBuilder _\u2192\u1da0_\nfinFunRewiringBuilder = mk id _\u2218\u2032_ _***_ id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1da0_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = Rewire.rewire f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec = tabulate\u2218lookup\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n _***_ {o\u2080 = o\u2080} f g x with Fin.cmp o\u2080 _ x\n _***_ f _ ._ | Fin.bound x = inject+ _ (f x)\n _***_ {i\u2080} _ g ._ | Fin.free x = raise i\u2080 (g x)\n\ntblRewiringBuilder : RewiringBuilder RewireTbl\ntblRewiringBuilder = mk tabulate _>>>_ _***_ (allFin _) _=[_]=_ rewire-spec idC-spec\n where\n open Rewire\n C = RewireTbl\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tbl ]= output = Rewire.rewireTbl tbl input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ tabulate r ]= Rewire.rewire r bs\n rewire-spec r bs = sym (tabulate-\u2218 (flip lookup bs) r)\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ allFin _ ]= bs\n -- idC-spec = map-lookup-allFin\n idC-spec bs rewrite rewire-spec id bs = tabulate\u2218lookup bs\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n c\u2080 >>> c\u2081 = rewireTbl c\u2081 c\u2080\n -- c\u2080 >>> c\u2081 = tabulate (flip lookup c\u2080 \u2218 flip lookup c\u2081)\n -- c\u2080 >>> c\u2081 = vmap (flip lookup c\u2080) c\u2081\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n _***_ {i\u2080} c\u2080 c\u2081 = vmap (inject+ _) c\u2080 ++ vmap (raise i\u2080) c\u2081\n\nbitsFunRewiringBuilder : RewiringBuilder _\u2192\u1d47_\nbitsFunRewiringBuilder = mk Rewire.rewire _>>>_ _***_ id _=[_]=_ rewire-spec idC-spec\n where\n C = _\u2192\u1d47_\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ f ]= output = f input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ Rewire.rewire r ]= Rewire.rewire r bs\n rewire-spec r bs = refl\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ id ]= bs\n idC-spec bs = refl\n\n _>>>_ : \u2200 {i m o} \u2192 C i m \u2192 C m o \u2192 C i o\n f >>> g = g \u2218 f\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n (f *** g) xs with splitAt _ xs\n ... | ys , zs , _ = f ys ++ g zs\n\nbitsFunCircuitBuilder : CircuitBuilder _\u2192\u1d47_\nbitsFunCircuitBuilder = mk bitsFunRewiringBuilder id (\u03bb { bs [] \u2192 bs }) (\u03bb { f g (b \u2237 bs) \u2192 (if b then f else g) bs })\n\nopen import bintree\nopen import flipbased-tree\n\nTreeBits : CircuitType\nTreeBits i o = Tree (Bits o) i\n\nmodule moretree where\n _>>>_ : \u2200 {m n a} {A : Set a} \u2192 Tree (Bits m) n \u2192 Tree A m \u2192 Tree A n\n f >>> g = map (flip bintree.lookup g) f\n\ntreeBitsRewiringBuilder : RewiringBuilder TreeBits\ntreeBitsRewiringBuilder = mk rewire moretree._>>>_ _***_ (rewire id) _=[_]=_ rewire-spec idC-spec\n where\n C = TreeBits\n\n rewire : \u2200 {i o} \u2192 RewireFun i o \u2192 C i o\n rewire f = fromFun (Rewire.rewire f)\n\n _=[_]=_ : \u2200 {i o} \u2192 Bits i \u2192 C i o \u2192 Bits o \u2192 Set\n input =[ tree ]= output = toFun tree input \u2261 output\n\n rewire-spec : \u2200 {i o} (r : RewireFun i o) bs \u2192 bs =[ rewire r ]= Rewire.rewire r bs\n rewire-spec r = toFun\u2218fromFun (tabulate \u2218 flip (Data.Vec.lookup \u2218 r))\n\n idC-spec : \u2200 {i} (bs : Bits i) \u2192 bs =[ rewire id ]= bs\n idC-spec bs rewrite toFun\u2218fromFun (tabulate \u2218 flip Data.Vec.lookup) bs | tabulate\u2218lookup bs = refl\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 o\u2080 \u2192 C i\u2081 o\u2081 \u2192 C (i\u2080 + i\u2081) (o\u2080 + o\u2081)\n (f *** g) = f >>= \u03bb xs \u2192 map (_++_ xs) g\n\ntreeBitsCircuitBuilder : CircuitBuilder TreeBits\ntreeBitsCircuitBuilder = mk treeBitsRewiringBuilder arr leaf fork\n where\n C = TreeBits\n\n arr : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 C i o\n arr = fromFun\n\nRewiringTree : CircuitType\nRewiringTree i o = Tree (Fin i) o\n\nmodule RewiringWith2^Outputs where\n C_\u27e82^_\u27e9 = RewiringTree\n\n rewire : \u2200 {i o} \u2192 RewireFun i (2 ^ o) \u2192 C i \u27e82^ o \u27e9\n rewire f = fromFun (f \u2218 toFin)\n\n lookupFin : \u2200 {i o} \u2192 C i \u27e82^ o \u27e9 \u2192 Fin (2 ^ o) \u2192 Fin i\n lookupFin c x = bintree.lookup (fromFin x) c\n\n _>>>_ : \u2200 {i m o} \u2192 C i \u27e82^ m \u27e9 \u2192 C (2 ^ m) \u27e82^ o \u27e9 \u2192 C i \u27e82^ o \u27e9\n f >>> g = rewire (lookupFin f \u2218 lookupFin g)\n\n _***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 C i\u2080 \u27e82^ o\u2080 \u27e9 \u2192 C i\u2081 \u27e82^ o\u2081 \u27e9 \u2192 C (i\u2080 + i\u2081) \u27e82^ (o\u2080 + o\u2081) \u27e9\n f *** g = f >>= \u03bb x \u2192 map (Fin._+\u2032_ x) g\n\nmodule Test where\n open import Data.Bits.Bits4\n\n tbl : RewireTbl 4 4\n tbl = # 1 \u2237 # 0 \u2237 # 2 \u2237 # 2 \u2237 []\n\n fun : RewireFun 4 4\n fun zero = # 1\n fun (suc zero) = # 0\n fun (suc (suc zero)) = # 2\n fun (suc (suc (suc x))) = # 2\n\n-- swap x y \u2248 swap y x\n-- reverse \u2218 reverse \u2248 id\n\n abs : \u2200 {C} \u2192 RewiringBuilder C \u2192 C 4 4\n abs builder = swap\u2082 *** dup\u2081\u00b2 *** sink 1\n where open RewiringBuilder builder\n\n tinytree : Tree (Fin 4) 2\n tinytree = fork (fork (leaf (# 1)) (leaf (# 0))) (fork (leaf (# 2)) (leaf (# 2)))\n\n bigtree : Tree (Bits 4) 4\n bigtree = fork (fork (fork (same 0000b) (same 0011b)) (fork (same 1000b) (same 1011b)))\n (fork (fork (same 0100b) (same 0111b)) (fork (same 1100b) (same 1111b)))\n where same : \u2200 {n} {A : Set} \u2192 A \u2192 Tree A (suc n)\n same x = fork (leaf x) (leaf x)\n\n test\u2081 : tbl \u2261 tabulate fun\n test\u2081 = refl\n\n test\u2082 : tbl \u2261 abs tblRewiringBuilder\n test\u2082 = refl\n\n test\u2083 : tabulate fun \u2261 tabulate (abs finFunRewiringBuilder)\n test\u2083 = refl\n\n test\u2084 : bigtree \u2261 abs treeBitsRewiringBuilder\n test\u2084 = refl\n\n -- open RewiringWith2^Outputs\n -- test\u2085 : tabulate (lookupFin tinytree) \u2261 tbl\n -- test\u2085 = refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"fe1b0ebc0939db24b3806bfd4606e08a57d54b68","subject":"Data.Type->Type","message":"Data.Type->Type\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Text\/Printer.agda","new_file":"lib\/Text\/Printer.agda","new_contents":"module Text.Printer where\n\nopen import Type\nopen import Data.String\nopen import Data.Nat\nopen import Data.Nat.Show\nopen import Data.Bool\nopen import Function\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n\nShowS : \u2605\nShowS = String \u2192 String\n\nPr : \u2605 \u2192 \u2605\nPr A = A \u2192 ShowS\n\n`_ : String \u2192 ShowS\n(` s) tail = Data.String._++_ s tail\n\nparenBase : ShowS \u2192 ShowS\nparenBase doc = ` \"(\" \u2218 doc \u2218 ` \")\"\n\nrecord PrEnv : \u2605 where\n constructor mk\n field\n level : \u2115\n\n withLevel : \u2115 \u2192 PrEnv\n withLevel x = record { level = x }\n\nopen PrEnv\n\nparen : PrEnv \u2192 PrEnv \u2192 ShowS \u2192 ShowS\nparen \u0393 \u0394 = if \u230a level \u0393 \u2264? level \u0394 \u230b then id else parenBase\n","old_contents":"module Text.Printer where\n\nopen import Data.Type\nopen import Data.String\nopen import Data.Nat\nopen import Data.Nat.Show\nopen import Data.Bool\nopen import Function\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\n\nShowS : \u2605\nShowS = String \u2192 String\n\nPr : \u2605 \u2192 \u2605\nPr A = A \u2192 ShowS\n\n`_ : String \u2192 ShowS\n(` s) tail = Data.String._++_ s tail\n\nparenBase : ShowS \u2192 ShowS\nparenBase doc = ` \"(\" \u2218 doc \u2218 ` \")\"\n\nrecord PrEnv : \u2605 where\n constructor mk\n field\n level : \u2115\n\n withLevel : \u2115 \u2192 PrEnv\n withLevel x = record { level = x }\n\nopen PrEnv\n\nparen : PrEnv \u2192 PrEnv \u2192 ShowS \u2192 ShowS\nparen \u0393 \u0394 = if \u230a level \u0393 \u2264? level \u0394 \u230b then id else parenBase\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"9f6aedf21fc179011cac644c9caaa069ed07e28c","subject":"Extract ECC\/ecc as Algebra.DoubleAndAdd in agda-nplib","message":"Extract ECC\/ecc as Algebra.DoubleAndAdd in agda-nplib\n","repos":"crypto-agda\/crypto-agda","old_file":"ECC\/ecc.agda","new_file":"ECC\/ecc.agda","new_contents":"","old_contents":"open import Relation.Binary.PropositionalEquality.NP\nopen import Data.Two.Base\nopen import Data.List\nopen import Function\nopen import Algebra.FunctionProperties.Eq\nopen Implicits\nopen import Algebra.Monoid.Commutative\nopen import Algebra.Ring\n\nmodule ecc\n {\ud835\udd3d : Set} (\ud835\udd3d-ring : Ring \ud835\udd3d)\n {\u2119 : Set} (\u2119-monoid : Commutative-Monoid \u2119)\n where\n\nopen module \ud835\udd3d = Ring \ud835\udd3d-ring\n\nopen module \u2295 = Commutative-Monoid \u2119-monoid\n renaming\n ( _\u2219_ to _\u2295_\n ; \u2219= to \u2295=\n ; \u03b5\u2219-identity to \u03b5\u2295-identity\n ; \u2219\u03b5-identity to \u2295\u03b5-identity\n ; _\u00b2 to 2\u00b7_\n )\n\n2\u00b7-\u2295-distr : \u2200 {P Q} \u2192 2\u00b7 (P \u2295 Q) \u2261 2\u00b7 P \u2295 2\u00b7 Q\n2\u00b7-\u2295-distr = \u2295.interchange\n\n2\u00b7-\u2295 : \u2200 {P Q R} \u2192 2\u00b7 P \u2295 (Q \u2295 R) \u2261 (P \u2295 Q) \u2295 (P \u2295 R)\n2\u00b7-\u2295 = \u2295.interchange\n\n-- NOT used yet\nmultiply-bin : List \ud835\udfda \u2192 \u2119 \u2192 \u2119\nmultiply-bin scalar P = go scalar\n where\n go : List \ud835\udfda \u2192 \u2119\n go [] = P\n go (b \u2237 bs) = [0: x\u2080 1: x\u2081 ] b\n where x\u2080 = 2\u00b7 go bs\n x\u2081 = P \u2295 x\u2080\n\n{-\nmultiply : \ud835\udd3d \u2192 \u2119 \u2192 \u2119\nmultiply scalar P =\n -- if scalar == 0 or scalar >= N: raise Exception(\"Invalid Scalar\/Private Key\")\n multiply-bin (bin scalar) P\n\n_\u00b7_ = multiply\ninfixr 8 _\u00b7_\n-}\n\ninfixl 7 1+2*_\n1+2*_ = \u03bb x \u2192 1+ 2* x\n\ndata Parity-View : \ud835\udd3d \u2192 Set where\n zero\u27e8_\u27e9 : \u2200 {n} \u2192 n \u2261 0# \u2192 Parity-View n\n even_by\u27e8_\u27e9 : \u2200 {m n} \u2192 Parity-View m \u2192 n \u2261 2* m \u2192 Parity-View n\n odd_by\u27e8_\u27e9 : \u2200 {m n} \u2192 Parity-View m \u2192 n \u2261 1+ 2* m \u2192 Parity-View n\n\ncast_by\u27e8_\u27e9 : \u2200 {x y} \u2192 Parity-View x \u2192 y \u2261 x \u2192 Parity-View y\ncast zero\u27e8 x\u2091 \u27e9 by\u27e8 y\u2091 \u27e9 = zero\u27e8 y\u2091 \u2219 x\u2091 \u27e9\ncast even x\u209a by\u27e8 x\u2091 \u27e9 by\u27e8 y\u2091 \u27e9 = even x\u209a by\u27e8 y\u2091 \u2219 x\u2091 \u27e9\ncast odd x\u209a by\u27e8 x\u2091 \u27e9 by\u27e8 y\u2091 \u27e9 = odd x\u209a by\u27e8 y\u2091 \u2219 x\u2091 \u27e9\n\ninfixr 8 _\u00b7\u209a_\n_\u00b7\u209a_ : \u2200 {n} (p : Parity-View n) \u2192 \u2119 \u2192 \u2119\nzero\u27e8 e \u27e9 \u00b7\u209a P = \u03b5\neven p by\u27e8 e \u27e9 \u00b7\u209a P = 2\u00b7 (p \u00b7\u209a P)\nodd p by\u27e8 e \u27e9 \u00b7\u209a P = P \u2295 (2\u00b7 (p \u00b7\u209a P))\n\n_+2*_ : \ud835\udfda \u2192 \ud835\udd3d \u2192 \ud835\udd3d\n0\u2082 +2* m = 2* m\n1\u2082 +2* m = 1+2* m\n\n{-\npostulate\n bin-2* : \u2200 {n} \u2192 bin (2* n) \u2261 0\u2082 \u2237 bin n\n bin-1+2* : \u2200 {n} \u2192 bin (1+2* n) \u2261 1\u2082 \u2237 bin n\n\nbin-+2* : (b : \ud835\udfda)(n : \ud835\udd3d) \u2192 bin (b +2* n) \u2261 b \u2237 bin n\nbin-+2* 1\u2082 n = bin-1+2*\nbin-+2* 0\u2082 n = bin-2*\n-}\n\n-- (msb) most significant bit first\nbin\u209a : \u2200 {n} \u2192 Parity-View n \u2192 List \ud835\udfda\nbin\u209a zero\u27e8 e \u27e9 = []\nbin\u209a even p by\u27e8 e \u27e9 = 0\u2082 \u2237 bin\u209a p\nbin\u209a odd p by\u27e8 e \u27e9 = 1\u2082 \u2237 bin\u209a p\n\nhalf : \u2200 {n} \u2192 Parity-View n \u2192 \ud835\udd3d\nhalf zero\u27e8 _ \u27e9 = 0#\nhalf (even_by\u27e8_\u27e9 {m} _ _) = m\nhalf (odd_by\u27e8_\u27e9 {m} _ _) = m\n\n{-\nbin-parity : \u2200 {n} (p : ParityView n) \u2192 bin n \u2261 parity p \u2237 bin (half p)\nbin-parity (even n) = bin-2*\nbin-parity (odd n) = bin-1+2*\n-}\n\ninfixl 6 1+\u209a_ _+\u209a_\n1+\u209a_ : \u2200 {x} \u2192 Parity-View x \u2192 Parity-View (1+ x)\n1+\u209a zero\u27e8 e \u27e9 = odd zero\u27e8 refl \u27e9 by\u27e8 ap 1+_ (e \u2219 ! 0+-identity) \u27e9\n1+\u209a even p by\u27e8 e \u27e9 = odd p by\u27e8 ap 1+_ e \u27e9\n1+\u209a odd p by\u27e8 e \u27e9 = even 1+\u209a p by\u27e8 ap 1+_ e \u2219 ! +-assoc \u2219 +-interchange \u27e9\n\n_+\u209a_ : \u2200 {x y} \u2192 Parity-View x \u2192 Parity-View y \u2192 Parity-View (x + y)\nzero\u27e8 x\u2091 \u27e9 +\u209a y\u209a = cast y\u209a by\u27e8 ap (\u03bb z \u2192 z + _) x\u2091 \u2219 0+-identity \u27e9\nx\u209a +\u209a zero\u27e8 y\u2091 \u27e9 = cast x\u209a by\u27e8 ap (_+_ _) y\u2091 \u2219 +-comm \u2219 0+-identity \u27e9\neven x\u209a by\u27e8 x\u2091 \u27e9 +\u209a even y\u209a by\u27e8 y\u2091 \u27e9 = even x\u209a +\u209a y\u209a by\u27e8 += x\u2091 y\u2091 \u2219 +-interchange \u27e9\neven x\u209a by\u27e8 x\u2091 \u27e9 +\u209a odd y\u209a by\u27e8 y\u2091 \u27e9 = odd x\u209a +\u209a y\u209a by\u27e8 += x\u2091 y\u2091 \u2219 +-comm \u2219 +-assoc \u2219 ap 1+_ (+-comm \u2219 +-interchange) \u27e9\nodd x\u209a by\u27e8 x\u2091 \u27e9 +\u209a even y\u209a by\u27e8 y\u2091 \u27e9 = odd x\u209a +\u209a y\u209a by\u27e8 += x\u2091 y\u2091 \u2219 +-assoc \u2219 ap 1+_ +-interchange \u27e9\nodd x\u209a by\u27e8 x\u2091 \u27e9 +\u209a odd y\u209a by\u27e8 y\u2091 \u27e9 = even 1+\u209a (x\u209a +\u209a y\u209a) by\u27e8 += x\u2091 y\u2091 \u2219 +-on-sides refl +-interchange \u27e9\n\ninfixl 7 2*\u209a_\n2*\u209a_ : \u2200 {x} \u2192 Parity-View x \u2192 Parity-View (2* x)\n2*\u209a x\u209a = x\u209a +\u209a x\u209a\n\nopen \u2261-Reasoning\n\nmodule _ {P Q} where\n \u00b7\u209a-\u2295-distr : \u2200 {x} (x\u209a : Parity-View x) \u2192 x\u209a \u00b7\u209a (P \u2295 Q) \u2261 x\u209a \u00b7\u209a P \u2295 x\u209a \u00b7\u209a Q\n \u00b7\u209a-\u2295-distr zero\u27e8 x\u2091 \u27e9 = ! \u03b5\u2295-identity\n \u00b7\u209a-\u2295-distr even x\u209a by\u27e8 x\u2091 \u27e9 = ap 2\u00b7_ (\u00b7\u209a-\u2295-distr x\u209a) \u2219 2\u00b7-\u2295-distr\n \u00b7\u209a-\u2295-distr odd x\u209a by\u27e8 x\u2091 \u27e9 = ap (\u03bb z \u2192 P \u2295 Q \u2295 2\u00b7 z) (\u00b7\u209a-\u2295-distr x\u209a)\n \u2219 ap (\u03bb z \u2192 P \u2295 Q \u2295 z) (! 2\u00b7-\u2295)\n \u2219 \u2295.interchange\n\nmodule _ {P} where\n cast-\u00b7\u209a-distr : \u2200 {x y} (x\u209a : Parity-View x) (y\u2091 : y \u2261 x) \u2192 cast x\u209a by\u27e8 y\u2091 \u27e9 \u00b7\u209a P \u2261 x\u209a \u00b7\u209a P\n cast-\u00b7\u209a-distr zero\u27e8 x\u2081 \u27e9 y\u2091 = refl\n cast-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2081 \u27e9 y\u2091 = refl\n cast-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2081 \u27e9 y\u2091 = refl\n\n 1+\u209a-\u00b7\u209a-distr : \u2200 {x} (x\u209a : Parity-View x) \u2192 (1+\u209a x\u209a) \u00b7\u209a P \u2261 P \u2295 x\u209a \u00b7\u209a P\n 1+\u209a-\u00b7\u209a-distr zero\u27e8 x\u2091 \u27e9 = ap (_\u2295_ P) \u03b5\u2295-identity\n 1+\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 = refl\n 1+\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 = ap 2\u00b7_ (1+\u209a-\u00b7\u209a-distr x\u209a) \u2219 \u2295.interchange \u2219 \u2295.assoc\n\n +\u209a-\u00b7\u209a-distr : \u2200 {x y} (x\u209a : Parity-View x) (y\u209a : Parity-View y)\n \u2192 (x\u209a +\u209a y\u209a) \u00b7\u209a P \u2261 x\u209a \u00b7\u209a P \u2295 y\u209a \u00b7\u209a P\n +\u209a-\u00b7\u209a-distr zero\u27e8 x\u2091 \u27e9 y\u209a = cast-\u00b7\u209a-distr y\u209a _ \u2219 ! \u03b5\u2295-identity\n\n +\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 zero\u27e8 y\u2091 \u27e9 = ! \u2295\u03b5-identity\n +\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 zero\u27e8 y\u2091 \u27e9 = ! \u2295\u03b5-identity\n\n +\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 even y\u209a by\u27e8 y\u2091 \u27e9 = ap 2\u00b7_ (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u2219 2\u00b7-\u2295-distr\n +\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 odd y\u209a by\u27e8 y\u2091 \u27e9 = ap (_\u2295_ P) (ap 2\u00b7_ (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u2219 2\u00b7-\u2295-distr) \u2219 \u2295.assoc-comm\n +\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 even y\u209a by\u27e8 y\u2091 \u27e9 = ap (_\u2295_ P) (ap 2\u00b7_ (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u2219 2\u00b7-\u2295-distr) \u2219 ! \u2295.assoc\n +\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 odd y\u209a by\u27e8 y\u2091 \u27e9\n = (odd x\u209a by\u27e8 x\u2091 \u27e9 +\u209a odd y\u209a by\u27e8 y\u2091 \u27e9) \u00b7\u209a P\n \u2261\u27e8by-definition\u27e9\n 2\u00b7((1+\u209a (x\u209a +\u209a y\u209a)) \u00b7\u209a P)\n \u2261\u27e8 ap 2\u00b7_ helper \u27e9\n 2\u00b7(P \u2295 (x\u209a \u00b7\u209a P \u2295 y\u209a \u00b7\u209a P))\n \u2261\u27e8 2\u00b7-\u2295-distr \u27e9\n 2\u00b7 P \u2295 (2\u00b7(x\u209a \u00b7\u209a P \u2295 y\u209a \u00b7\u209a P))\n \u2261\u27e8 ap (_\u2295_ (2\u00b7 P)) 2\u00b7-\u2295-distr \u27e9\n 2\u00b7 P \u2295 (2\u00b7(x\u209a \u00b7\u209a P) \u2295 2\u00b7(y\u209a \u00b7\u209a P))\n \u2261\u27e8 2\u00b7-\u2295 \u27e9\n P \u2295 2\u00b7(x\u209a \u00b7\u209a P) \u2295 (P \u2295 2\u00b7(y\u209a \u00b7\u209a P))\n \u2261\u27e8by-definition\u27e9\n odd x\u209a by\u27e8 x\u2091 \u27e9 \u00b7\u209a P \u2295 odd y\u209a by\u27e8 y\u2091 \u27e9 \u00b7\u209a P\n \u220e\n where helper = (1+\u209a (x\u209a +\u209a y\u209a)) \u00b7\u209a P\n \u2261\u27e8 1+\u209a-\u00b7\u209a-distr (x\u209a +\u209a y\u209a) \u27e9\n P \u2295 ((x\u209a +\u209a y\u209a) \u00b7\u209a P)\n \u2261\u27e8 ap (_\u2295_ P) (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u27e9\n P \u2295 (x\u209a \u00b7\u209a P \u2295 y\u209a \u00b7\u209a P)\n \u220e\n\n*-1+-distr : \u2200 {x y} \u2192 x * (1+ y) \u2261 x + x * y\n*-1+-distr = *-+-distr\u02e1 \u2219 += *1-identity refl\n\n1+-*-distr : \u2200 {x y} \u2192 (1+ x) * y \u2261 y + x * y\n1+-*-distr = *-+-distr\u02b3 \u2219 += 1*-identity refl\n\ninfixl 7 _*\u209a_\n_*\u209a_ : \u2200 {x y} \u2192 Parity-View x \u2192 Parity-View y \u2192 Parity-View (x * y)\nzero\u27e8 x\u2091 \u27e9 *\u209a y\u209a = zero\u27e8 *= x\u2091 refl \u2219 0*-zero \u27e9\nx\u209a *\u209a zero\u27e8 y\u2091 \u27e9 = zero\u27e8 *= refl y\u2091 \u2219 *0-zero \u27e9\neven x\u209a by\u27e8 x\u2091 \u27e9 *\u209a y\u209a = even (x\u209a *\u209a y\u209a) by\u27e8 *= x\u2091 refl \u2219 *-+-distr\u02b3 \u27e9\nx\u209a *\u209a even y\u209a by\u27e8 y\u2091 \u27e9 = even (x\u209a *\u209a y\u209a) by\u27e8 *= refl y\u2091 \u2219 *-+-distr\u02e1 \u27e9\nodd x\u209a by\u27e8 x\u2091 \u27e9 *\u209a odd y\u209a by\u27e8 y\u2091 \u27e9 = odd (x\u209a +\u209a y\u209a +\u209a 2*\u209a (x\u209a *\u209a y\u209a)) by\u27e8 *= x\u2091 y\u2091 \u2219 helper \u27e9\n where\n x = _\n y = _\n helper = (1+2* x)*(1+2* y)\n \u2261\u27e8 1+-*-distr \u27e9\n 1+2* y + 2* x * 1+2* y\n \u2261\u27e8 ap (\u03bb z \u2192 1+2* y + z)\n (2* x * 1+2* y\n \u2261\u27e8 *-1+-distr \u27e9\n (2* x + 2* x * 2* y)\n \u2261\u27e8 += refl *-+-distr\u02e1 \u2219 +-interchange \u27e9\n (2* (x + 2* x * y))\n \u220e) \u27e9\n 1+2* y + 2* (x + 2* x * y)\n \u2261\u27e8 +-assoc \u2219 ap 1+_ +-interchange \u27e9\n 1+2*(y + (x + 2* x * y))\n \u2261\u27e8 ap 1+2*_ (! +-assoc \u2219 += +-comm refl) \u27e9\n 1+2*(x + y + 2* x * y)\n \u2261\u27e8 ap (\u03bb z \u2192 1+2*(x + y + z)) *-+-distr\u02b3 \u27e9\n 1+2*(x + y + 2* (x * y))\n \u220e\n\nmodule _ {P} where\n 2\u00b7-\u00b7\u209a-distr : \u2200 {x} (x\u209a : Parity-View x) \u2192 2\u00b7(x\u209a \u00b7\u209a P) \u2261 x\u209a \u00b7\u209a 2\u00b7 P\n 2\u00b7-\u00b7\u209a-distr x\u209a = ! \u00b7\u209a-\u2295-distr x\u209a\n\n 2*\u209a-\u00b7\u209a-distr : \u2200 {x} (x\u209a : Parity-View x) \u2192 (2*\u209a x\u209a) \u00b7\u209a P \u2261 2\u00b7(x\u209a \u00b7\u209a P)\n 2*\u209a-\u00b7\u209a-distr x\u209a = +\u209a-\u00b7\u209a-distr x\u209a x\u209a\n\n\u00b7\u209a-\u03b5 : \u2200 {x} (x\u209a : Parity-View x) \u2192 x\u209a \u00b7\u209a \u03b5 \u2261 \u03b5\n\u00b7\u209a-\u03b5 zero\u27e8 x\u2091 \u27e9 = refl\n\u00b7\u209a-\u03b5 even x\u209a by\u27e8 x\u2091 \u27e9 = ap 2\u00b7_ (\u00b7\u209a-\u03b5 x\u209a) \u2219 \u03b5\u2295-identity\n\u00b7\u209a-\u03b5 odd x\u209a by\u27e8 x\u2091 \u27e9 = \u03b5\u2295-identity \u2219 ap 2\u00b7_ (\u00b7\u209a-\u03b5 x\u209a) \u2219 \u03b5\u2295-identity\n\n*\u209a-\u00b7\u209a-distr : \u2200 {x y} (x\u209a : Parity-View x) (y\u209a : Parity-View y) {P} \u2192 (x\u209a *\u209a y\u209a) \u00b7\u209a P \u2261 x\u209a \u00b7\u209a y\u209a \u00b7\u209a P\n*\u209a-\u00b7\u209a-distr zero\u27e8 x\u2091 \u27e9 y\u209a = refl\n*\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 zero\u27e8 y\u2091 \u27e9 = ! \u00b7\u209a-\u03b5 even x\u209a by\u27e8 x\u2091 \u27e9\n*\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 zero\u27e8 y\u2091 \u27e9 = ! \u00b7\u209a-\u03b5 odd x\u209a by\u27e8 x\u2091 \u27e9\n\n*\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 even y\u209a by\u27e8 y\u2091 \u27e9 = ap 2\u00b7_ (*\u209a-\u00b7\u209a-distr x\u209a even y\u209a by\u27e8 y\u2091 \u27e9)\n*\u209a-\u00b7\u209a-distr even x\u209a by\u27e8 x\u2091 \u27e9 odd y\u209a by\u27e8 y\u2091 \u27e9 = ap 2\u00b7_ (*\u209a-\u00b7\u209a-distr x\u209a odd y\u209a by\u27e8 y\u2091 \u27e9)\n\n*\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 even y\u209a by\u27e8 y\u2091 \u27e9 {P} =\n ap 2\u00b7_ (*\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 y\u209a) \u2219 2\u00b7-\u2295-distr \u2219 ap (\u03bb z \u2192 2\u00b7 (y\u209a \u00b7\u209a P) \u2295 2\u00b7 z) (2\u00b7-\u00b7\u209a-distr x\u209a)\n*\u209a-\u00b7\u209a-distr odd x\u209a by\u27e8 x\u2091 \u27e9 odd y\u209a by\u27e8 y\u2091 \u27e9 {P} =\n ap (_\u2295_ P)\n (ap 2\u00b7_\n (+\u209a-\u00b7\u209a-distr (x\u209a +\u209a y\u209a) (2*\u209a (x\u209a *\u209a y\u209a))\n \u2219 \u2295= (+\u209a-\u00b7\u209a-distr x\u209a y\u209a) (2*\u209a-\u00b7\u209a-distr (x\u209a *\u209a y\u209a))\n \u2219 \u2295= \u2295.comm (ap 2\u00b7_ (*\u209a-\u00b7\u209a-distr x\u209a y\u209a) \u2219 2\u00b7-\u00b7\u209a-distr x\u209a) \u2219 \u2295.assoc\n \u2219 \u2295= refl (! \u00b7\u209a-\u2295-distr x\u209a) ) \u2219 2\u00b7-\u2295-distr)\n \u2219 \u2295.!assoc\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"5ee6d5ce31819d87d8744774d89d87955150f01e","subject":"Cosmetic changes.","message":"Cosmetic changes.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Data\/Nat\/Inequalities\/PropertiesATP.agda","new_file":"src\/fot\/FOTC\/Data\/Nat\/Inequalities\/PropertiesATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Properties of the inequalities\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Nat.Inequalities.PropertiesATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.EliminationProperties\nopen import FOTC.Data.Nat.PropertiesATP\n\n------------------------------------------------------------------------------\n-- Congruence properties\n\nltLeftCong : \u2200 {m n o} \u2192 m \u2261 n \u2192 lt m o \u2261 lt n o\nltLeftCong refl = refl\n\nltRightCong : \u2200 {m n o} \u2192 n \u2261 o \u2192 lt m n \u2261 lt m o\nltRightCong refl = refl\n\n------------------------------------------------------------------------------\n-- N.B. The elimination properties are in the module\n-- FOTC.Data.Nat.Inequalities.EliminationProperties.\n\nx\u22650 : \u2200 {n} \u2192 N n \u2192 n \u2265 zero\nx\u22650 nzero = lt-0S zero\nx\u22650 (nsucc {n} Nn) = lt-0S (succ\u2081 n)\n\n0\u2264x : \u2200 {n} \u2192 N n \u2192 zero \u2264 n\n0\u2264x Nn = x\u22650 Nn\n\n0\u226fx : \u2200 {n} \u2192 N n \u2192 zero \u226f n\n0\u226fx nzero = lt-00\n0\u226fx (nsucc {n} Nn) = lt-S0 n\n\nx\u226ex : \u2200 {n} \u2192 N n \u2192 n \u226e n\nx\u226ex nzero = lt-00\nx\u226ex (nsucc {n} Nn) = trans (lt-SS n n) (x\u226ex Nn)\n\nSx\u22700 : \u2200 {n} \u2192 N n \u2192 succ\u2081 n \u2270 zero\nSx\u22700 nzero = x\u226ex (nsucc nzero)\nSx\u22700 (nsucc {n} Nn) = trans (lt-SS (succ\u2081 n) zero) (lt-S0 n)\n\nxy\u2192y n \u2192 n < m\nx>y\u2192yn = \u22a5-elim (0>x\u2192\u22a5 Nn 0>n)\nx>y\u2192yy\u2192ySn =\n trans (lt-SS n m) (x>y\u2192ySn))\n\nx\u2265y\u2192x\u226ey : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m \u2265 n \u2192 m \u226e n\nx\u2265y\u2192x\u226ey nzero nzero _ = x\u226ex nzero\nx\u2265y\u2192x\u226ey nzero (nsucc Nn) 0\u2265Sn = \u22a5-elim (0\u2265S\u2192\u22a5 Nn 0\u2265Sn)\nx\u2265y\u2192x\u226ey (nsucc {m} Nm) nzero _ = lt-S0 m\nx\u2265y\u2192x\u226ey (nsucc {m} Nm) (nsucc {n} Nn) Sm\u2265Sn =\n prf (x\u2265y\u2192x\u226ey Nm Nn (trans (sym (lt-SS n (succ\u2081 m))) Sm\u2265Sn))\n where postulate prf : m \u226e n \u2192 succ\u2081 m \u226e succ\u2081 n\n {-# ATP prove prf #-}\n\nx\u226ey\u2192x\u2265y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m \u226e n \u2192 m \u2265 n\nx\u226ey\u2192x\u2265y nzero nzero 0\u226e0 = x\u2265x nzero\nx\u226ey\u2192x\u2265y nzero (nsucc {n} Nn) 0\u226eSn =\n \u22a5-elim (true\u2262false (trans (sym (lt-0S n)) 0\u226eSn))\nx\u226ey\u2192x\u2265y (nsucc Nm) nzero Sm\u226en = x\u22650 (nsucc Nm)\nx\u226ey\u2192x\u2265y (nsucc {m} Nm) (nsucc {n} Nn) Sm\u226eSn =\n prf (x\u226ey\u2192x\u2265y Nm Nn (trans (sym (lt-SS m n)) Sm\u226eSn))\n where postulate prf : m \u2265 n \u2192 succ\u2081 m \u2265 succ\u2081 n\n {-# ATP prove prf #-}\n\nx>y\u2192x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2192 m \u2270 n\nx>y\u2192x\u2270y nzero Nn 0>m = \u22a5-elim (0>x\u2192\u22a5 Nn 0>m)\nx>y\u2192x\u2270y (nsucc Nm) nzero _ = Sx\u22700 Nm\nx>y\u2192x\u2270y (nsucc {m} Nm) (nsucc {n} Nn) Sm>Sn =\n x\u2270y\u2192Sx\u2270Sy m n (x>y\u2192x\u2270y Nm Nn (trans (sym (lt-SS n m)) Sm>Sn))\n\npostulate x>y\u2192x\u2264y\u2192\u22a5 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2192 m \u2264 n \u2192 \u22a5\n{-# ATP prove x>y\u2192x\u2264y\u2192\u22a5 x>y\u2192x\u2270y #-}\n\nx>y\u2228x\u2264y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2228 m \u2264 n\nx>y\u2228x\u2264y nzero Nn = inj\u2082 (x\u22650 Nn)\nx>y\u2228x\u2264y (nsucc {m} Nm) nzero = inj\u2081 (lt-0S m)\nx>y\u2228x\u2264y (nsucc {m} Nm) (nsucc {n} Nn) =\n case (\u03bb m>n \u2192 inj\u2081 (trans (lt-SS n m) m>n))\n (\u03bb m\u2264n \u2192 inj\u2082 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n (x>y\u2228x\u2264y Nm Nn)\n\nxy\u2228x\u2264y Nn Nm\n\nxy\u2228x\u226fy : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2228 m \u226f n\nx>y\u2228x\u226fy nzero Nn = inj\u2082 (0\u226fx Nn)\nx>y\u2228x\u226fy (nsucc {m} Nm) nzero = inj\u2081 (lt-0S m)\nx>y\u2228x\u226fy (nsucc {m} Nm) (nsucc {n} Nn) =\n case (\u03bb h \u2192 inj\u2081 (trans (lt-SS n m) h))\n (\u03bb h \u2192 inj\u2082 (trans (lt-SS n m) h))\n (x>y\u2228x\u226fy Nm Nn)\n\n<-trans : \u2200 {m n o} \u2192 N m \u2192 N n \u2192 N o \u2192 m < n \u2192 n < o \u2192 m < o\n<-trans nzero nzero _ 0<0 _ = \u22a5-elim (0<0\u2192\u22a5 0<0)\n<-trans nzero (nsucc Nn) nzero _ Sn<0 = \u22a5-elim (S<0\u2192\u22a5 Sn<0)\n<-trans nzero (nsucc Nn) (nsucc {o} No) _ _ = lt-0S o\n<-trans (nsucc Nm) Nn nzero _ n<0 = \u22a5-elim (x<0\u2192\u22a5 Nn n<0)\n<-trans (nsucc Nm) nzero (nsucc No) Sm<0 _ = \u22a5-elim (S<0\u2192\u22a5 Sm<0)\n<-trans (nsucc {m} Nm) (nsucc {n} Nn) (nsucc {o} No) Smy\u2192x\u2238y+y\u2261x : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2192 (m \u2238 n) + n \u2261 m\nx>y\u2192x\u2238y+y\u2261x nzero Nn 0>n = \u22a5-elim (0>x\u2192\u22a5 Nn 0>n)\nx>y\u2192x\u2238y+y\u2261x (nsucc {m} Nm) nzero Sm>0 = prf\n where postulate prf : (succ\u2081 m \u2238 zero) + zero \u2261 succ\u2081 m\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx>y\u2192x\u2238y+y\u2261x (nsucc {m} Nm) (nsucc {n} Nn) Sm>Sn = prf (x>y\u2192x\u2238y+y\u2261x Nm Nn m>n)\n where\n postulate m>n : m > n\n {-# ATP prove m>n #-}\n\n postulate prf : (m \u2238 n) + n \u2261 m \u2192 (succ\u2081 m \u2238 succ\u2081 n) + succ\u2081 n \u2261 succ\u2081 m\n {-# ATP prove prf +-comm \u2238-N S\u2238S #-}\n\nx\u2264y\u2192y\u2238x+x\u2261y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m \u2264 n \u2192 (n \u2238 m) + m \u2261 n\nx\u2264y\u2192y\u2238x+x\u2261y {n = n} nzero Nn 0\u2264n = prf\n where postulate prf : (n \u2238 zero) + zero \u2261 n\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx\u2264y\u2192y\u2238x+x\u2261y (nsucc Nm) nzero Sm\u22640 = \u22a5-elim (S\u22640\u2192\u22a5 Nm Sm\u22640)\n\nx\u2264y\u2192y\u2238x+x\u2261y (nsucc {m} Nm) (nsucc {n} Nn) Sm\u2264Sn = prf (x\u2264y\u2192y\u2238x+x\u2261y Nm Nn m\u2264n)\n where\n postulate m\u2264n : m \u2264 n\n {-# ATP prove m\u2264n #-}\n\n postulate prf : (n \u2238 m) + m \u2261 n \u2192 (succ\u2081 n \u2238 succ\u2081 m) + succ\u2081 m \u2261 succ\u2081 n\n {-# ATP prove prf +-comm \u2238-N S\u2238S #-}\n\nx0\u2192x\u2238y zero \u2192 m \u2238 n < m\nx\u2265y\u2192y>0\u2192x\u2238y0 = \u22a5-elim (x>x\u2192\u22a5 nzero 0>0)\nx\u2265y\u2192y>0\u2192x\u2238y0\u2192x\u2238y0 = prf\n where postulate prf : succ\u2081 m \u2238 succ\u2081 n < succ\u2081 m\n {-# ATP prove prf x\u2238yy\u2192y n \u2192 n < m\nx>y\u2192yn = \u22a5-elim (0>x\u2192\u22a5 Nn 0>n)\nx>y\u2192yy\u2192ySn =\n trans (lt-SS n m) (x>y\u2192ySn))\n\nx\u2265y\u2192x\u226ey : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m \u2265 n \u2192 m \u226e n\nx\u2265y\u2192x\u226ey nzero nzero _ = x\u226ex nzero\nx\u2265y\u2192x\u226ey nzero (nsucc Nn) 0\u2265Sn = \u22a5-elim (0\u2265S\u2192\u22a5 Nn 0\u2265Sn)\nx\u2265y\u2192x\u226ey (nsucc {m} Nm) nzero _ = lt-S0 m\nx\u2265y\u2192x\u226ey (nsucc {m} Nm) (nsucc {n} Nn) Sm\u2265Sn =\n prf (x\u2265y\u2192x\u226ey Nm Nn (trans (sym (lt-SS n (succ\u2081 m))) Sm\u2265Sn))\n where postulate prf : m \u226e n \u2192 succ\u2081 m \u226e succ\u2081 n\n {-# ATP prove prf #-}\n\nx\u226ey\u2192x\u2265y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m \u226e n \u2192 m \u2265 n\nx\u226ey\u2192x\u2265y nzero nzero 0\u226e0 = x\u2265x nzero\nx\u226ey\u2192x\u2265y nzero (nsucc {n} Nn) 0\u226eSn =\n \u22a5-elim (true\u2262false (trans (sym (lt-0S n)) 0\u226eSn))\nx\u226ey\u2192x\u2265y (nsucc Nm) nzero Sm\u226en = x\u22650 (nsucc Nm)\nx\u226ey\u2192x\u2265y (nsucc {m} Nm) (nsucc {n} Nn) Sm\u226eSn =\n prf (x\u226ey\u2192x\u2265y Nm Nn (trans (sym (lt-SS m n)) Sm\u226eSn))\n where postulate prf : m \u2265 n \u2192 succ\u2081 m \u2265 succ\u2081 n\n {-# ATP prove prf #-}\n\nx>y\u2192x\u2270y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2192 m \u2270 n\nx>y\u2192x\u2270y nzero Nn 0>m = \u22a5-elim (0>x\u2192\u22a5 Nn 0>m)\nx>y\u2192x\u2270y (nsucc Nm) nzero _ = Sx\u22700 Nm\nx>y\u2192x\u2270y (nsucc {m} Nm) (nsucc {n} Nn) Sm>Sn =\n x\u2270y\u2192Sx\u2270Sy m n (x>y\u2192x\u2270y Nm Nn (trans (sym (lt-SS n m)) Sm>Sn))\n\npostulate\n x>y\u2192x\u2264y\u2192\u22a5 : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2192 m \u2264 n \u2192 \u22a5\n{-# ATP prove x>y\u2192x\u2264y\u2192\u22a5 x>y\u2192x\u2270y #-}\n\nx>y\u2228x\u2264y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2228 m \u2264 n\nx>y\u2228x\u2264y nzero Nn = inj\u2082 (x\u22650 Nn)\nx>y\u2228x\u2264y (nsucc {m} Nm) nzero = inj\u2081 (lt-0S m)\nx>y\u2228x\u2264y (nsucc {m} Nm) (nsucc {n} Nn) =\n case (\u03bb m>n \u2192 inj\u2081 (trans (lt-SS n m) m>n))\n (\u03bb m\u2264n \u2192 inj\u2082 (x\u2264y\u2192Sx\u2264Sy m\u2264n))\n (x>y\u2228x\u2264y Nm Nn)\n\nxy\u2228x\u2264y Nn Nm\n\nxy\u2228x\u226fy : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2228 m \u226f n\nx>y\u2228x\u226fy nzero Nn = inj\u2082 (0\u226fx Nn)\nx>y\u2228x\u226fy (nsucc {m} Nm) nzero = inj\u2081 (lt-0S m)\nx>y\u2228x\u226fy (nsucc {m} Nm) (nsucc {n} Nn) =\n case (\u03bb h \u2192 inj\u2081 (trans (lt-SS n m) h))\n (\u03bb h \u2192 inj\u2082 (trans (lt-SS n m) h))\n (x>y\u2228x\u226fy Nm Nn)\n\n<-trans : \u2200 {m n o} \u2192 N m \u2192 N n \u2192 N o \u2192 m < n \u2192 n < o \u2192 m < o\n<-trans nzero nzero _ 0<0 _ = \u22a5-elim (0<0\u2192\u22a5 0<0)\n<-trans nzero (nsucc Nn) nzero _ Sn<0 = \u22a5-elim (S<0\u2192\u22a5 Sn<0)\n<-trans nzero (nsucc Nn) (nsucc {o} No) _ _ = lt-0S o\n<-trans (nsucc Nm) Nn nzero _ n<0 = \u22a5-elim (x<0\u2192\u22a5 Nn n<0)\n<-trans (nsucc Nm) nzero (nsucc No) Sm<0 _ = \u22a5-elim (S<0\u2192\u22a5 Sm<0)\n<-trans (nsucc {m} Nm) (nsucc {n} Nn) (nsucc {o} No) Smy\u2192x\u2238y+y\u2261x : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m > n \u2192 (m \u2238 n) + n \u2261 m\nx>y\u2192x\u2238y+y\u2261x nzero Nn 0>n = \u22a5-elim (0>x\u2192\u22a5 Nn 0>n)\nx>y\u2192x\u2238y+y\u2261x (nsucc {m} Nm) nzero Sm>0 = prf\n where postulate prf : (succ\u2081 m \u2238 zero) + zero \u2261 succ\u2081 m\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx>y\u2192x\u2238y+y\u2261x (nsucc {m} Nm) (nsucc {n} Nn) Sm>Sn = prf (x>y\u2192x\u2238y+y\u2261x Nm Nn m>n)\n where\n postulate m>n : m > n\n {-# ATP prove m>n #-}\n\n postulate prf : (m \u2238 n) + n \u2261 m \u2192 (succ\u2081 m \u2238 succ\u2081 n) + succ\u2081 n \u2261 succ\u2081 m\n {-# ATP prove prf +-comm \u2238-N S\u2238S #-}\n\nx\u2264y\u2192y\u2238x+x\u2261y : \u2200 {m n} \u2192 N m \u2192 N n \u2192 m \u2264 n \u2192 (n \u2238 m) + m \u2261 n\nx\u2264y\u2192y\u2238x+x\u2261y {n = n} nzero Nn 0\u2264n = prf\n where postulate prf : (n \u2238 zero) + zero \u2261 n\n {-# ATP prove prf +-rightIdentity \u2238-N #-}\n\nx\u2264y\u2192y\u2238x+x\u2261y (nsucc Nm) nzero Sm\u22640 = \u22a5-elim (S\u22640\u2192\u22a5 Nm Sm\u22640)\n\nx\u2264y\u2192y\u2238x+x\u2261y (nsucc {m} Nm) (nsucc {n} Nn) Sm\u2264Sn = prf (x\u2264y\u2192y\u2238x+x\u2261y Nm Nn m\u2264n)\n where\n postulate m\u2264n : m \u2264 n\n {-# ATP prove m\u2264n #-}\n\n postulate prf : (n \u2238 m) + m \u2261 n \u2192 (succ\u2081 n \u2238 succ\u2081 m) + succ\u2081 m \u2261 succ\u2081 n\n {-# ATP prove prf +-comm \u2238-N S\u2238S #-}\n\nx0\u2192x\u2238y zero \u2192 m \u2238 n < m\nx\u2265y\u2192y>0\u2192x\u2238y0 = \u22a5-elim (x>x\u2192\u22a5 nzero 0>0)\nx\u2265y\u2192y>0\u2192x\u2238y0\u2192x\u2238y0 = prf\n where postulate prf : succ\u2081 m \u2238 succ\u2081 n < succ\u2081 m\n {-# ATP prove prf x\u2238y = 0#; <1b> = 0#; cond = 1#; fork = 1+_+_D; tt = 0#;\n <_,_> = _\u2218\u2032_; fst = 0#; snd = 0#;\n dup = 0#; first = F.id; swap = 0#; assoc = 0#;\n = 0#; snd = 0#;\n <_\u00d7_> = _\u2218\u2032_; second = F.id;\n nil = 0#; cons = 0#; uncons = 0# }\n where open D\n\nmodule SeqTimeOpsD where\n Time = Diff\u2115\n open D\n open FlatFunsOps seqTimeOpsD public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2257 0#\n snoc\u22610 zero x = \u2261.refl\n snoc\u22610 (suc n) x rewrite snoc\u22610 n x = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2257 0#\n reverse\u22610 zero x = \u2261.refl\n reverse\u22610 (suc n) x rewrite reverse\u22610 n x | snoc\u22610 n x = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2257 0#\n replicate\u22610 zero = \u03bb _ \u2192 \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2257 0#\n constBit\u22610 true {-1b-} _ = \u2261.refl\n constBit\u22610 false {-0b-} _ = \u2261.refl\n\n sum\u2032 : \u2200 {n} \u2192 Vec Diff\u2115 n \u2192 Diff\u2115\n sum\u2032 = V.foldr (const Diff\u2115) _\u2218\u2032_ id\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec {n} f xs \u2257 sum\u2032 (V.map f xs)\n constVec\u2261sum f [] x = \u2261.refl\n constVec\u2261sum {suc n} f (b \u2237 bs) x rewrite \u2115\u00b0.+-comm (f b x \u2294 constVec f bs x) 0\n | constVec\u2261sum f bs x = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2257 0#\n constBits\u22610 [] x = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) x rewrite constBits\u22610 bs x\n | constBit\u22610 b x = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2257 m *# n\n foldl\u2261* zero n x = \u2261.refl\n foldl\u2261* (suc m) n x rewrite foldl\u2261* m n (n x) = \u2261.refl\n\n foldr\u2261* : \u2200 m n \u2192 foldr {m} n \u2257 m *#\u2032 n\n foldr\u2261* zero n x = \u2261.refl\n foldr\u2261* (suc m) n x rewrite foldr\u2261* m n x = \u2261.refl\n\n #nodes : \u2200 i \u2192 Diff\u2115\n #nodes zero = 0#\n #nodes (suc i) = 1# +# #nodes i +# #nodes i\n\n fromBitsFun\u2261 : \u2200 {i o} (f : Bits i \u2192 Bits o) \u2192 fromBitsFun f \u2257 #nodes i\n fromBitsFun\u2261 {zero} f x = constBits\u22610 (f []) x\n fromBitsFun\u2261 {suc i} f x rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_) x\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_) (#nodes i x) = \u2261.refl\n-}\n\nTime = \u2115\nTimeCost = constFuns Time\nSpace = \u2115\nSpaceCost = constFuns Space\n\nseqTimeLin : LinRewiring TimeCost\nseqTimeLin =\n record {\n id = 0;\n _\u2218_ = _+_;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _+_;\n second = F.id;\n tt\u2192nil = 0;\n nil\u2192tt = 0;\n cons = 0;\n uncons = 0 }\n\nseqTimeRewiring : Rewiring TimeCost\nseqTimeRewiring =\n record {\n linRewiring = seqTimeLin;\n tt = 0;\n dup = 0;\n nil = 0;\n <_,_> = _+_;\n fst = 0;\n snd = 0 }\n\nseqTimeOps : FlatFunsOps TimeCost\nseqTimeOps = record { rewiring = seqTimeRewiring;\n <0b> = 0; <1b> = 0; cond = 1; fork = 1+_+_ }\n\ntimeLin : LinRewiring TimeCost\ntimeLin =\n record {\n id = 0;\n _\u2218_ = _+_;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _\u2294_;\n second = F.id;\n tt\u2192nil = 0;\n nil\u2192tt = 0;\n cons = 0;\n uncons = 0 }\n\ntimeRewiring : Rewiring TimeCost\ntimeRewiring =\n record {\n linRewiring = timeLin;\n tt = 0;\n dup = 0;\n nil = 0;\n <_,_> = _\u2294_;\n fst = 0;\n snd = 0 }\n\ntimeOps : FlatFunsOps TimeCost\ntimeOps = record { rewiring = timeRewiring;\n <0b> = 0; <1b> = 0; cond = 1; fork = 1+_\u2294_ }\n\n{-\ntimeOps\u2261seqTimeOps : timeOps \u2261 record seqTimeOps {\n rewiring = record seqTimeRewiring {\n linRewiring = record seqTimeLin { <_\u00d7_> = _\u2294_ };\n <_,_> = _\u2294_};\n fork = 1+_\u2294_ }\n {-;\n cons = 0; uncons = 0 } -- Without cons = 0... this definition makes\n -- the FlatFunsOps record def yellow\n -}\ntimeOps\u2261seqTimeOps = \u2261.refl\n-}\n\n{-\nopen import maxsemiring\nopen \u2294-+-SemiringSolver\ntimeOps' : \u2200 n \u2192 FlatFunsOps (constFuns (Polynomial n))\ntimeOps' n = record {\n id = con 0; _\u2218_ = _:*_;\n <0b> = con 0; <1b> = con 0; cond = con 1; fork = 1+_+_; tt = con 0;\n <_,_> = _:*_; fst = con 0; snd = con 0;\n dup = con 0; <_\u00d7_> = _:+_; swap = con 0; assoc = con 0;\n = con 0; snd = con 0;\n nil = con 0; cons = con 0; uncons = con 0 }\n where 1+_+_ : Polynomial n \u2192 Polynomial n \u2192 Polynomial n\n 1+ x + y = con 1 :* (x :* y)\n\nModule TimeOps' where\n Time = \u2115\n open FlatFunsOps (timeOps' 1) public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} := con 0\n snoc\u22610 zero = ?\n snoc\u22610 (suc n) = ?\n-}\n\nmodule TimeOps where\n open FlatFunsOps timeOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2294\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2261 0\n replicate\u22610 zero = \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2261 0\n constBit\u22610 true = \u2261.refl\n constBit\u22610 false = \u2261.refl\n\n maximum : \u2200 {n} \u2192 Vec \u2115 n \u2192 \u2115\n maximum = V.foldr (const \u2115) (\u03bb {_} x y \u2192 x \u2294 y) 0\n\n constVec\u22a4\u2261maximum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec\u22a4 {n} f xs \u2261 maximum (V.map f xs)\n constVec\u22a4\u2261maximum f [] = \u2261.refl\n constVec\u22a4\u2261maximum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b \u2294 constVec\u22a4 f bs) 0\n | constVec\u22a4\u2261maximum f bs = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2261 0\n constBits\u22610 [] = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) rewrite constBit\u22610 b\n | constBits\u22610 bs = \u2261.refl\n\n fromBitsFun\u2261i : \u2200 {i o} (f : Bits i \u2192 Bits o) \u2192 fromBitsFun f \u2261 i\n fromBitsFun\u2261i {zero} f rewrite constBits\u22610 (f []) = \u2261.refl\n fromBitsFun\u2261i {suc i} f rewrite fromBitsFun\u2261i {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261i {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (i \u2294 i) 0\n = \u2261.cong suc (i\u2294i\u2261i i)\n\nspaceLin : LinRewiring SpaceCost\nspaceLin =\n record {\n id = 0;\n _\u2218_ = _+_;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _+_;\n second = F.id;\n tt\u2192nil = 0;\n nil\u2192tt = 0;\n cons = 0;\n uncons = 0 }\n\nspaceLin\u2261seqTimeLin : spaceLin \u2261 seqTimeLin\nspaceLin\u2261seqTimeLin = \u2261.refl\n\nspaceRewiring : Rewiring TimeCost\nspaceRewiring =\n record {\n linRewiring = spaceLin;\n tt = 0;\n dup = 1;\n nil = 0;\n <_,_> = 1+_+_;\n fst = 0;\n snd = 0 }\n\nspaceOps : FlatFunsOps SpaceCost\nspaceOps = record { rewiring = spaceRewiring;\n <0b> = 1; <1b> = 1; cond = 1; fork = 1+_+_ }\n\n {-\n-- So far the space cost model is like the sequential time cost model but makes <0b>,<1b>,dup\n-- cost one unit of space.\nspaceOps\u2261seqTimeOps : spaceOps \u2261 record seqTimeOps { <0b> = 1; <1b> = 1; dup = 1; <_,_> = 1+_+_\n ; cons = 0; uncons = 0 } -- same bug here\nspaceOps\u2261seqTimeOps = \u2261.refl\n-}\n\nmodule SpaceOps where\n open FlatFunsOps spaceOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2115\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u2261n : \u2200 n \u2192 replicate {n} \u2261 n\n replicate\u2261n zero = \u2261.refl\n replicate\u2261n (suc n) rewrite replicate\u2261n n = \u2261.refl\n\n constBit\u22611 : \u2200 b \u2192 constBit b \u2261 1\n constBit\u22611 true = \u2261.refl\n constBit\u22611 false = \u2261.refl\n\n constVec\u22a4\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Space) xs \u2192 constVec\u22a4 {n} f xs \u2261 V.sum (V.map f xs)\n constVec\u22a4\u2261sum f [] = \u2261.refl\n constVec\u22a4\u2261sum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b + constVec\u22a4 f bs) 0\n | constVec\u22a4\u2261sum f bs = \u2261.refl\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Space) xs \u2192 constVec {n} f xs \u2261 V.sum (V.map f xs)\n constVec\u2261sum f xs rewrite constVec\u22a4\u2261sum f xs | \u2115\u00b0.+-comm (V.sum (V.map f xs)) 0 = \u2261.refl\n\n constBits\u2261n : \u2200 {n} xs \u2192 constBits {n} xs \u2261 n\n constBits\u2261n [] = \u2261.refl\n constBits\u2261n {suc n} (b \u2237 bs) rewrite constBit\u22611 b\n | constBits\u2261n bs\n | \u2115\u00b0.+-comm n 0 = \u2261.refl\n\n fromBitsFun-cost : \u2200 (i o : \u2115) \u2192 \u2115\n fromBitsFun-cost zero o = o\n fromBitsFun-cost (suc i) o = 1 + 2*(fromBitsFun-cost i o)\n\n fromBitsFun\u2261 : \u2200 {i o} (f : Bits i \u2192 Bits o) \u2192 fromBitsFun f \u2261 fromBitsFun-cost i o\n fromBitsFun\u2261 {zero} {o} f rewrite constBits\u2261n (f []) | \u2115\u00b0.+-comm o 0 = \u2261.refl\n fromBitsFun\u2261 {suc i} {o} f rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (2* (fromBitsFun-cost i o)) 0\n = \u2261.refl\n\n{-\ntime\u00d7spaceOps : FlatFunsOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-\u266dOps timeOps spaceOps\n\nmodule Time\u00d7SpaceOps = FlatFunsOps time\u00d7spaceOps\n-}","old_contents":"module flat-funs-cost where\n\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2*_; 2^_; _^_; _\u2294_; module \u2115\u00b0; module \u2294\u00b0; 2*\u2032_)\nopen import Data.Bool using (true; false)\nimport Data.DifferenceNat\nimport Data.Vec.NP as V\nimport Function as F\nimport Data.Product as \u00d7\nimport Relation.Binary.PropositionalEquality as \u2261\nopen F using (const; _\u2218\u2032_)\nopen V using (Vec; []; _\u2237_; _++_; [_])\nopen \u00d7 using (_\u00d7_)\nopen \u2261 using (_\u2261_; _\u2257_)\n\nopen import Data.Bits using (Bits; 0\u2237_; 1\u2237_)\n\nopen import flat-funs\n\nmodule D where\n open Data.DifferenceNat public renaming (suc to suc#; _+_ to _+#_)\n _*#_ : \u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n zero *# d = 0#\n suc n *# d = (n *# d) +# d\n _*#\u2032_ : \u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n zero *#\u2032 d = 0#\n suc n *#\u2032 d = d +# (n *#\u2032 d)\n 2*#_ : Diff\u2115 \u2192 Diff\u2115\n 2*# n = n +# n\n 2^#_ : \u2115 \u2192 Diff\u2115\n 2^# zero = 1#\n 2^# suc n = 2*# (2^# n)\n 1+_+_D : Diff\u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n 1+ x + y D = 1# \u2218\u2032 (x \u2218\u2032 y)\nopen D using (Diff\u2115)\n\nprivate\n 1+_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n 1+ x + y = 1 + (x + y)\n 1+_\u2294_ : \u2115 \u2192 \u2115 \u2192 \u2115\n 1+ x \u2294 y = 1 + (x \u2294 y)\n i\u2294i\u2261i : \u2200 i \u2192 i \u2294 i \u2261 i\n i\u2294i\u2261i zero = \u2261.refl\n i\u2294i\u2261i (suc i) = \u2261.cong suc (i\u2294i\u2261i i)\n\nseqTimeOpsD : FlatFunsOps (constFuns Diff\u2115)\nseqTimeOpsD = record {\n id = 0#; _\u2218_ = _\u2218\u2032_;\n <0b> = 0#; <1b> = 0#; cond = 1#; fork = 1+_+_D; tt = 0#;\n <_,_> = _\u2218\u2032_; fst = 0#; snd = 0#;\n dup = 0#; first = F.id; swap = 0#; assoc = 0#;\n = 0#; snd = 0#;\n <_\u00d7_> = _\u2218\u2032_; second = F.id;\n nil = 0#; cons = 0#; uncons = 0# }\n where open D\n\nmodule SeqTimeOpsD where\n Time = Diff\u2115\n open D\n open FlatFunsOps seqTimeOpsD public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2257 0#\n snoc\u22610 zero x = \u2261.refl\n snoc\u22610 (suc n) x rewrite snoc\u22610 n x = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2257 0#\n reverse\u22610 zero x = \u2261.refl\n reverse\u22610 (suc n) x rewrite reverse\u22610 n x | snoc\u22610 n x = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2257 0#\n replicate\u22610 zero = \u03bb _ \u2192 \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2257 0#\n constBit\u22610 true {-1b-} _ = \u2261.refl\n constBit\u22610 false {-0b-} _ = \u2261.refl\n\n sum\u2032 : \u2200 {n} \u2192 Vec Diff\u2115 n \u2192 Diff\u2115\n sum\u2032 = V.foldr (const Diff\u2115) _\u2218\u2032_ id\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec {n} f xs \u2257 sum\u2032 (V.map f xs)\n constVec\u2261sum f [] x = \u2261.refl\n constVec\u2261sum {suc n} f (b \u2237 bs) x rewrite \u2115\u00b0.+-comm (f b x \u2294 constVec f bs x) 0\n | constVec\u2261sum f bs x = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2257 0#\n constBits\u22610 [] x = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) x rewrite constBits\u22610 bs x\n | constBit\u22610 b x = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2257 m *# n\n foldl\u2261* zero n x = \u2261.refl\n foldl\u2261* (suc m) n x rewrite foldl\u2261* m n (n x) = \u2261.refl\n\n foldr\u2261* : \u2200 m n \u2192 foldr {m} n \u2257 m *#\u2032 n\n foldr\u2261* zero n x = \u2261.refl\n foldr\u2261* (suc m) n x rewrite foldr\u2261* m n x = \u2261.refl\n\n #nodes : \u2200 i \u2192 Diff\u2115\n #nodes zero = 0#\n #nodes (suc i) = 1# +# #nodes i +# #nodes i\n\n fromBitsFun\u2261 : \u2200 {i o} (f : Bits i \u2192 Bits o) \u2192 fromBitsFun f \u2257 #nodes i\n fromBitsFun\u2261 {zero} f x = constBits\u22610 (f []) x\n fromBitsFun\u2261 {suc i} f x rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_) x\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_) (#nodes i x) = \u2261.refl\n\nseqTimeOps : FlatFunsOps (constFuns \u2115)\nseqTimeOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 0; <1b> = 0; cond = 1; fork = 1+_+_; tt = 0;\n <_,_> = _+_; fst = 0; snd = 0;\n dup = 0; first = F.id; swap = 0; assoc = 0;\n = 0; snd = 0;\n <_\u00d7_> = _+_; second = F.id;\n nil = 0; cons = 0; uncons = 0 }\n\ntimeOps : FlatFunsOps (constFuns \u2115)\ntimeOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 0; <1b> = 0; cond = 1; fork = 1+_\u2294_; tt = 0;\n <_,_> = _\u2294_; fst = 0; snd = 0;\n dup = 0; first = F.id; swap = 0; assoc = 0;\n = 0; snd = 0;\n <_\u00d7_> = _\u2294_; second = F.id;\n nil = 0; cons = 0; uncons = 0 }\n\n\ntimeOps\u2261seqTimeOps : timeOps \u2261 record seqTimeOps {\n <_,_> = _\u2294_; <_\u00d7_> = _\u2294_; fork = 1+_\u2294_\n ; cons = 0; uncons = 0 } -- Without cons = 0... this definition makes\n -- the FlatFunsOps record def yellow\ntimeOps\u2261seqTimeOps = \u2261.refl\n\n{-\nopen import maxsemiring\nopen \u2294-+-SemiringSolver\ntimeOps' : \u2200 n \u2192 FlatFunsOps (constFuns (Polynomial n))\ntimeOps' n = record {\n id = con 0; _\u2218_ = _:*_;\n <0b> = con 0; <1b> = con 0; cond = con 1; fork = 1+_+_; tt = con 0;\n <_,_> = _:*_; fst = con 0; snd = con 0;\n dup = con 0; <_\u00d7_> = _:+_; swap = con 0; assoc = con 0;\n = con 0; snd = con 0;\n nil = con 0; cons = con 0; uncons = con 0 }\n where 1+_+_ : Polynomial n \u2192 Polynomial n \u2192 Polynomial n\n 1+ x + y = con 1 :* (x :* y)\n\nModule TimeOps' where\n Time = \u2115\n open FlatFunsOps (timeOps' 1) public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} := con 0\n snoc\u22610 zero = ?\n snoc\u22610 (suc n) = ?\n-}\n\nmodule TimeOps where\n Time = \u2115\n open FlatFunsOps timeOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2294\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2261 0\n replicate\u22610 zero = \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2261 0\n constBit\u22610 true = \u2261.refl\n constBit\u22610 false = \u2261.refl\n\n maximum : \u2200 {n} \u2192 Vec \u2115 n \u2192 \u2115\n maximum = V.foldr (const \u2115) (\u03bb {_} x y \u2192 x \u2294 y) 0\n\n constVec\u2261maximum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec {n} f xs \u2261 maximum (V.map f xs)\n constVec\u2261maximum f [] = \u2261.refl\n constVec\u2261maximum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b \u2294 constVec f bs) 0\n | constVec\u2261maximum f bs = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2261 0\n constBits\u22610 [] = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) rewrite constBit\u22610 b\n | constBits\u22610 bs = \u2261.refl\n\n fromBitsFun\u2261i : \u2200 {i o} (f : Bits i \u2192 Bits o) \u2192 fromBitsFun f \u2261 i\n fromBitsFun\u2261i {zero} f rewrite constBits\u22610 (f []) = \u2261.refl\n fromBitsFun\u2261i {suc i} f rewrite fromBitsFun\u2261i {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261i {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (i \u2294 i) 0\n = \u2261.cong suc (i\u2294i\u2261i i)\n\nspaceOps : FlatFunsOps (constFuns \u2115)\nspaceOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 1; <1b> = 1; cond = 1; fork = 1+_+_; tt = 0;\n <_,_> = 1+_+_; fst = 0; snd = 0;\n dup = 1; first = F.id; swap = 0; assoc = 0;\n = 0; snd = 0;\n <_\u00d7_> = _+_; second = F.id;\n nil = 0; cons = 0; uncons = 0 }\n\n-- So far the space cost model is like the sequential time cost model but makes <0b>,<1b>,dup\n-- cost one unit of space.\nspaceOps\u2261seqTimeOps : spaceOps \u2261 record seqTimeOps { <0b> = 1; <1b> = 1; dup = 1; <_,_> = 1+_+_\n ; cons = 0; uncons = 0 } -- same bug here\nspaceOps\u2261seqTimeOps = \u2261.refl\n\nmodule SpaceOps where\n Space = \u2115\n open FlatFunsOps spaceOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2115\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u2261n : \u2200 n \u2192 replicate {n} \u2261 n\n replicate\u2261n zero = \u2261.refl\n replicate\u2261n (suc n) rewrite replicate\u2261n n = \u2261.refl\n\n constBit\u22611 : \u2200 b \u2192 constBit b \u2261 1\n constBit\u22611 true = \u2261.refl\n constBit\u22611 false = \u2261.refl\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Space) xs \u2192 constVec {n} f xs \u2261 V.sum (V.map f xs)\n constVec\u2261sum f [] = \u2261.refl\n constVec\u2261sum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b + constVec f bs) 0\n | constVec\u2261sum f bs = \u2261.refl\n\n constBits\u2261n : \u2200 {n} xs \u2192 constBits {n} xs \u2261 n\n constBits\u2261n [] = \u2261.refl\n constBits\u2261n {suc n} (b \u2237 bs) rewrite constBit\u22611 b\n | constBits\u2261n bs\n | \u2115\u00b0.+-comm n 0 = \u2261.refl\n\n fromBitsFun-cost : \u2200 (i o : \u2115) \u2192 \u2115\n fromBitsFun-cost zero o = o\n fromBitsFun-cost (suc i) o = 1 + 2*(fromBitsFun-cost i o)\n\n fromBitsFun\u2261 : \u2200 {i o} (f : Bits i \u2192 Bits o) \u2192 fromBitsFun f \u2261 fromBitsFun-cost i o\n fromBitsFun\u2261 {zero} {o} f rewrite constBits\u2261n (f []) | \u2115\u00b0.+-comm o 0 = \u2261.refl\n fromBitsFun\u2261 {suc i} {o} f rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (2* (fromBitsFun-cost i o)) 0\n = \u2261.refl\n\ntime\u00d7spaceOps : FlatFunsOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-\u266dOps timeOps spaceOps\n\nmodule Time\u00d7SpaceOps = FlatFunsOps time\u00d7spaceOps","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"417a1c46e905d1203ecabf95cc49646b7d090d33","subject":"Product: \u0394","message":"Product: \u0394\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Product\/NP.agda","new_file":"lib\/Data\/Product\/NP.agda","new_contents":"-- move this to product\n{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Product.NP where\n\nopen import Type hiding (\u2605)\nopen import Level\nopen import Data.Product public hiding (\u2203)\nopen import Relation.Binary.PropositionalEquality as \u2261\nopen import Relation.Unary.NP hiding (Decidable)\nopen import Relation.Binary\nopen import Relation.Nullary\nopen import Function\nopen import Function.Injection using (Injection; module Injection)\nopen import Relation.Unary.Logical\nopen import Relation.Binary.Logical\n\n\u2203 : \u2200 {a b} {A : \u2605 a} \u2192 (A \u2192 \u2605 b) \u2192 \u2605 (a \u2294 b)\n\u2203 = \u03a3 _\n\nfirst : \u2200 {a b c} {A : \u2605 a} {B : \u2605 b} {C : B \u2192 \u2605 c} \u2192\n (f : A \u2192 B) \u2192 \u03a3 A (C \u2218 f) \u2192 \u03a3 B C\nfirst f = map f id -- f (x , y) = (f x , y)\n\n-- generalized first\u2032 but differently than first\nfirst' : \u2200 {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} \u2192\n (f : (x : A) \u2192 B x) (p : \u03a3 A C) \u2192 B (proj\u2081 p) \u00d7 C (proj\u2081 p)\nfirst' f (x , y) = (f x , y)\n\nfirst\u2032 : \u2200 {a b c} {A : \u2605 a} {B : \u2605 b} {C : \u2605 c} \u2192\n (f : A \u2192 B) \u2192 A \u00d7 C \u2192 B \u00d7 C\nfirst\u2032 = first\n\nsecond : \u2200 {a p q} {A : \u2605 a} {P : A \u2192 \u2605 p} {Q : A \u2192 \u2605 q} \u2192\n (\u2200 {x} \u2192 P x \u2192 Q x) \u2192 \u03a3 A P \u2192 \u03a3 A Q\nsecond = map id\n\nsecond\u2032 : \u2200 {a b c} {A : \u2605 a} {B : \u2605 b} {C : \u2605 c} \u2192\n (B \u2192 C) \u2192 A \u00d7 B \u2192 A \u00d7 C\nsecond\u2032 f = second f\n\nsyntax \u2203 (\u03bb x \u2192 e) = \u2203[ x ] e\n\nrecord [\u03a3] {a b a\u209a b\u209a}\n {A : \u2605 a}\n {B : A \u2192 \u2605 b}\n (A\u209a : A \u2192 \u2605 a\u209a)\n (B\u209a : {x : A} (x\u209a : A\u209a x)\n \u2192 B x \u2192 \u2605 b\u209a)\n (p : \u03a3 A B) : \u2605 (a\u209a \u2294 b\u209a) where\n constructor _[,]_\n field\n [proj\u2081] : A\u209a (proj\u2081 p)\n [proj\u2082] : B\u209a [proj\u2081] (proj\u2082 p)\nopen [\u03a3] public\ninfixr 4 _[,]_\n\nsyntax [\u03a3] A\u209a (\u03bb x\u209a \u2192 e) = [ x\u209a \u2236 A\u209a ][\u00d7][ e ]\n\n[\u2203] : \u2200 {a a\u209a b b\u209a} \u2192\n (\u2200i\u27e8 A\u209a \u2236 [\u2605] {a} a\u209a \u27e9[\u2192] ((A\u209a [\u2192] [\u2605] {b} b\u209a) [\u2192] [\u2605] _)) \u2203\n[\u2203] {x\u209a = A\u209a} = [\u03a3] A\u209a\n\nsyntax [\u2203] (\u03bb A\u209a \u2192 f) = [\u2203][ A\u209a ] f\n\n_[\u00d7]_ : \u2200 {a b a\u209a b\u209a} \u2192 ([\u2605] {a} a\u209a [\u2192] [\u2605] {b} b\u209a [\u2192] [\u2605] _) _\u00d7_\n_[\u00d7]_ A\u209a B\u209a = [\u03a3] A\u209a (\u03bb _ \u2192 B\u209a)\n\nprivate\n\n Dec\u27e6\u2605\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n (A\u1d63 : \u27e6\u2605\u27e7 a\u1d63 A\u2081 A\u2082)\n \u2192 \u2605 _\n Dec\u27e6\u2605\u27e7 A\u1d63 = \u2200 x\u2081 x\u2082 \u2192 Dec (A\u1d63 x\u2081 x\u2082)\n\n Dec\u27e6Pred\u27e7 : \u2200 {a\u2081 a\u2082 p\u2081 p\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n (A\u1d63 : \u27e6\u2605\u27e7 a\u1d63 A\u2081 A\u2082)\n {P\u2081 : Pred p\u2081 A\u2081} {P\u2082 : Pred p\u2082 A\u2082} {p\u1d63}\n \u2192 (P\u1d63 : \u27e6Pred\u27e7 p\u1d63 A\u1d63 P\u2081 P\u2082) \u2192 \u2605 _\n Dec\u27e6Pred\u27e7 {A\u2081 = A\u2081} {A\u2082} A\u1d63 {P\u2081} {P\u2082} P\u1d63 = \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) y\u2081 y\u2082 \u2192 Dec (P\u1d63 x\u1d63 y\u2081 y\u2082) -- Dec\u27e6\u2605\u27e7 A\u1d63 \u21d2 Dec\u27e6\u2605\u27e7 P\u1d63\n\nrecord \u27e6\u03a3\u27e7 {a\u2081 a\u2082 b\u2082 b\u2081 a\u1d63 b\u1d63}\n {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : A\u2081 \u2192 \u2605 b\u2081}\n {B\u2082 : A\u2082 \u2192 \u2605 b\u2082}\n (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n (B\u1d63 : {x\u2081 : A\u2081} {x\u2082 : A\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082)\n \u2192 B\u2081 x\u2081 \u2192 B\u2082 x\u2082 \u2192 \u2605 b\u1d63)\n (p\u2081 : \u03a3 A\u2081 B\u2081) (p\u2082 : \u03a3 A\u2082 B\u2082) : \u2605 (a\u1d63 \u2294 b\u1d63) where\n constructor _,_\n field\n \u27e6proj\u2081\u27e7 : A\u1d63 (proj\u2081 p\u2081) (proj\u2081 p\u2082)\n \u27e6proj\u2082\u27e7 : B\u1d63 \u27e6proj\u2081\u27e7 (proj\u2082 p\u2081) (proj\u2082 p\u2082)\nopen \u27e6\u03a3\u27e7 public\ninfixr 4 _,_\n\nsyntax \u27e6\u03a3\u27e7 A\u1d63 (\u03bb x\u1d63 \u2192 e) = [ x\u1d63 \u2236 A\u1d63 ]\u27e6\u00d7\u27e7[ e ]\n\n\u27e6\u2203\u27e7 : \u2200 {a\u2081 a\u2082 b\u2082 b\u2081 a\u1d63 b\u1d63}\n {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63}\n {B\u2081 : A\u2081 \u2192 \u2605 b\u2081}\n {B\u2082 : A\u2082 \u2192 \u2605 b\u2082}\n (B\u1d63 : \u27e6Pred\u27e7 b\u1d63 A\u1d63 B\u2081 B\u2082)\n (p\u2081 : \u03a3 A\u2081 B\u2081) (p\u2082 : \u03a3 A\u2082 B\u2082) \u2192 \u2605 _\n\u27e6\u2203\u27e7 = \u27e6\u03a3\u27e7 _\n\nsyntax \u27e6\u2203\u27e7 (\u03bb x\u1d63 \u2192 e) = \u27e6\u2203\u27e7[ x\u1d63 ] e\n\n_\u27e6\u00d7\u27e7_ : \u2200 {a\u2081 a\u2082 b\u2082 b\u2081 a\u1d63 b\u1d63}\n {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082}\n (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 \u2605 b\u1d63)\n (p\u2081 : A\u2081 \u00d7 B\u2081) (p\u2082 : A\u2082 \u00d7 B\u2082) \u2192 \u2605 (a\u1d63 \u2294 b\u1d63)\n_\u27e6\u00d7\u27e7_ A\u1d63 B\u1d63 = \u27e6\u03a3\u27e7 A\u1d63 (\u03bb _ \u2192 B\u1d63)\n\n{-\n_\u27e6\u00d7\u27e7_ : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 \u2605 b\u1d63)\n (p\u2081 : A\u2081 \u00d7 B\u2081) (p\u2082 : A\u2082 \u00d7 B\u2082) \u2192 \u2605 _\n_\u27e6\u00d7\u27e7_ A\u1d63 B\u1d63 = \u03bb p\u2081 p\u2082 \u2192 A\u1d63 (proj\u2081 p\u2081) (proj\u2081 p\u2082) \u00d7\n B\u1d63 (proj\u2082 p\u2081) (proj\u2082 p\u2082)\n-}\n\n{- One can give these two types to \u27e6_,_\u27e7:\n\n\u27e6_,_\u27e7' : \u2200 {a\u2081 a\u2082 b\u2081 b\u2082 a\u1d63 b\u1d63}\n {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : A\u2081 \u2192 \u2605 b\u2081} {B\u2082 : A\u2082 \u2192 \u2605 b\u2082}\n {A\u1d63 : \u27e6\u2605\u27e7 a\u1d63 A\u2081 A\u2082}\n {B\u1d63 : \u27e6Pred\u27e7 A\u1d63 b\u1d63 B\u2081 B\u2082}\n {x\u2081 x\u2082 y\u2081 y\u2082}\n (x\u1d63 : A\u1d63 x\u2081 x\u2082)\n (y\u1d63 : B\u1d63 x\u1d63 y\u2081 y\u2082)\n \u2192 \u27e6\u03a3\u27e7 A\u1d63 B\u1d63 (x\u2081 , y\u2081) (x\u2082 , y\u2082)\n\u27e6_,_\u27e7' = \u27e6_,_\u27e7\n\n\u27e6_,_\u27e7'' : \u2200 {a\u2081 a\u2082 b\u2081 b\u2082 a\u1d63 b\u1d63}\n {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : A\u2081 \u2192 \u2605 b\u2081} {B\u2082 : A\u2082 \u2192 \u2605 b\u2082}\n {A\u1d63 : \u27e6\u2605\u27e7 a\u1d63 A\u2081 A\u2082}\n {B\u1d63 : \u27e6Pred\u27e7 A\u1d63 b\u1d63 B\u2081 B\u2082}\n {p\u2081 p\u2082}\n (\u27e6proj\u2081\u27e7 : A\u1d63 (proj\u2081 p\u2081) (proj\u2081 p\u2082))\n (\u27e6proj\u2082\u27e7 : B\u1d63 \u27e6proj\u2081\u27e7 (proj\u2082 p\u2081) (proj\u2082 p\u2082))\n \u2192 \u27e6\u03a3\u27e7 A\u1d63 B\u1d63 p\u2081 p\u2082\n\u27e6_,_\u27e7'' = \u27e6_,_\u27e7\n-}\n\ndec\u27e6\u03a3\u27e7 : \u2200 {a\u2081 a\u2082 b\u2081 b\u2082 a\u1d63 b\u1d63 A\u2081 A\u2082 B\u2081 B\u2082}\n {A\u1d63 : \u27e6\u2605\u27e7 {a\u2081} {a\u2082} a\u1d63 A\u2081 A\u2082}\n {B\u1d63 : \u27e6Pred\u27e7 {b\u2081} {b\u2082} b\u1d63 A\u1d63 B\u2081 B\u2082}\n (decA\u1d63 : Dec\u27e6\u2605\u27e7 A\u1d63)\n -- (substA\u1d63 : \u2200 {x y \u2113} {p q} (P : A\u1d63 x y \u2192 \u2605 \u2113) \u2192 P p \u2192 P q)\n (uniqA\u1d63 : \u2200 {x y} (p q : A\u1d63 x y) \u2192 p \u2261 q)\n (decB\u1d63 : Dec\u27e6Pred\u27e7 A\u1d63 {_} {_} {b\u1d63 {-BUG: with _ here Agda loops-}} B\u1d63)\n \u2192 Dec\u27e6\u2605\u27e7 (\u27e6\u03a3\u27e7 A\u1d63 B\u1d63)\ndec\u27e6\u03a3\u27e7 {B\u1d63 = B\u1d63} decA\u1d63 uniqA\u1d63 decB\u1d63 (x\u2081 , y\u2081) (x\u2082 , y\u2082) with decA\u1d63 x\u2081 x\u2082\n... | no \u00acx\u1d63 = no (\u00acx\u1d63 \u2218 \u27e6proj\u2081\u27e7)\n... | yes x\u1d63 with decB\u1d63 x\u1d63 y\u2081 y\u2082\n... | yes y\u1d63 = yes (x\u1d63 , y\u1d63)\n... | no \u00acy\u1d63 = no (\u00acy\u1d63 \u2218 f \u2218 \u27e6proj\u2082\u27e7)\n where f : \u2200 {x\u1d63'} \u2192 B\u1d63 x\u1d63' y\u2081 y\u2082 \u2192 B\u1d63 x\u1d63 y\u2081 y\u2082\n f {x\u1d63'} y\u1d63 rewrite uniqA\u1d63 x\u1d63' x\u1d63 = y\u1d63\n\nmodule Dec\u2082 where\n map\u2082\u2032 : \u2200 {p\u2081 p\u2082 q} {P\u2081 : \u2605 p\u2081} {P\u2082 : \u2605 p\u2082} {Q : \u2605 q}\n \u2192 (P\u2081 \u2192 P\u2082 \u2192 Q) \u2192 (Q \u2192 P\u2081) \u2192 (Q \u2192 P\u2082) \u2192 Dec P\u2081 \u2192 Dec P\u2082 \u2192 Dec Q\n map\u2082\u2032 _ \u03c0\u2081 _ (no \u00acp\u2081) _ = no (\u00acp\u2081 \u2218 \u03c0\u2081)\n map\u2082\u2032 _ _ \u03c0\u2082 _ (no \u00acp\u2082) = no (\u00acp\u2082 \u2218 \u03c0\u2082)\n map\u2082\u2032 mk _ _ (yes p\u2081) (yes p\u2082) = yes (mk p\u2081 p\u2082)\n\ndec\u27e6\u00d7\u27e7 : \u2200 {a\u2081 a\u2082 b\u2081 b\u2082 a\u1d63 b\u1d63}\n {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082}\n {A\u1d63 : \u27e6\u2605\u27e7 a\u1d63 A\u2081 A\u2082}\n {B\u1d63 : \u27e6\u2605\u27e7 b\u1d63 B\u2081 B\u2082}\n (decA\u1d63 : Dec\u27e6\u2605\u27e7 A\u1d63)\n (decB\u1d63 : Dec\u27e6\u2605\u27e7 B\u1d63)\n \u2192 Dec\u27e6\u2605\u27e7 (A\u1d63 \u27e6\u00d7\u27e7 B\u1d63)\ndec\u27e6\u00d7\u27e7 decA\u1d63 decB\u1d63 (x\u2081 , y\u2081) (x\u2082 , y\u2082) with decA\u1d63 x\u2081 x\u2082\n... | no \u00acx\u1d63 = no (\u00acx\u1d63 \u2218 \u27e6proj\u2081\u27e7)\n... | yes x\u1d63 with decB\u1d63 y\u2081 y\u2082\n... | yes y\u1d63 = yes (x\u1d63 , y\u1d63)\n... | no \u00acy\u1d63 = no (\u00acy\u1d63 \u2218 \u27e6proj\u2082\u27e7)\n\nmk\u03a3\u2261 : \u2200 {a b} {A : \u2605 a} {x y : A} (B : A \u2192 \u2605 b) {p : B x} {q : B y} (xy : x \u2261 y) \u2192 subst B xy p \u2261 q \u2192 (x \u03a3., p) \u2261 (y , q)\nmk\u03a3\u2261 _ xy h rewrite xy | h = \u2261.refl\n\n\u03a3,-injective\u2082 : \u2200 {a b} {A : \u2605 a} {B : A \u2192 \u2605 b} {x : A} {y z : B x} \u2192 (_,_ {B = B} x y) \u2261 (x \u03a3., z) \u2192 y \u2261 z\n\u03a3,-injective\u2082 refl = refl\n\n\u03a3,-injective\u2081 : \u2200 {a b} {A : \u2605 a} {B : A \u2192 \u2605 b} {x\u2081 x\u2082 : A} {y\u2081 : B x\u2081} {y\u2082 : B x\u2082} \u2192 (x\u2081 \u03a3., y\u2081) \u2261 (x\u2082 , y\u2082) \u2192 x\u2081 \u2261 x\u2082\n\u03a3,-injective\u2081 refl = refl\n\nproj\u2081-injective : \u2200 {a b} {A : \u2605 a} {B : A \u2192 \u2605 b} {x y : \u03a3 A B}\n (B-uniq : \u2200 {z} (p\u2081 p\u2082 : B z) \u2192 p\u2081 \u2261 p\u2082)\n \u2192 proj\u2081 x \u2261 proj\u2081 y \u2192 x \u2261 y\nproj\u2081-injective {x = (a , p\u2081)} {y = (_ , p\u2082)} B-uniq eq rewrite sym eq\n = cong (\u03bb p \u2192 (a , p)) (B-uniq p\u2081 p\u2082)\n\nproj\u2082-irrelevance : \u2200 {a b} {A : \u2605 a} {B C : A \u2192 \u2605 b} {x\u2081 x\u2082 : A}\n {y\u2081 : B x\u2081} {y\u2082 : B x\u2082} {z\u2081 : C x\u2081} {z\u2082 : C x\u2082}\n \u2192 (C-uniq : \u2200 {z} (p\u2081 p\u2082 : C z) \u2192 p\u2081 \u2261 p\u2082)\n \u2192 (x\u2081 \u03a3., y\u2081) \u2261 (x\u2082 , y\u2082)\n \u2192 (x\u2081 \u03a3., z\u2081) \u2261 (x\u2082 , z\u2082)\nproj\u2082-irrelevance C-uniq = proj\u2081-injective C-uniq \u2218 \u03a3,-injective\u2081\n\n\u225f\u03a3 : \u2200 {A : \u2605\u2080} {P : A \u2192 \u2605\u2080}\n (decA : Decidable {A = A} _\u2261_)\n (decP : \u2200 x \u2192 Decidable {A = P x} _\u2261_)\n \u2192 Decidable {A = \u03a3 A P} _\u2261_\n\u225f\u03a3 decA decP (w\u2081 , p\u2081) (w\u2082 , p\u2082) with decA w\u2081 w\u2082\n\u225f\u03a3 decA decP (w , p\u2081) (.w , p\u2082) | yes refl with decP w p\u2081 p\u2082\n\u225f\u03a3 decA decP (w , p) (.w , .p) | yes refl | yes refl = yes refl\n\u225f\u03a3 decA decP (w , p\u2081) (.w , p\u2082) | yes refl | no p\u2262\n = no (p\u2262 \u2218 \u03a3,-injective\u2082)\n\u225f\u03a3 decA decP (w\u2081 , p\u2081) (w\u2082 , p\u2082) | no w\u2262 = no (w\u2262 \u2218 cong proj\u2081)\n\n\u225f\u03a3' : \u2200 {A : \u2605\u2080} {P : A \u2192 \u2605\u2080}\n (decA : Decidable {A = A} _\u2261_)\n (uniqP : \u2200 {x} (p q : P x) \u2192 p \u2261 q)\n \u2192 Decidable {A = \u03a3 A P} _\u2261_\n\u225f\u03a3' decA uniqP (w\u2081 , p\u2081) (w\u2082 , p\u2082) with decA w\u2081 w\u2082\n\u225f\u03a3' decA uniqP (w , p\u2081) (.w , p\u2082) | yes refl\n = yes (cong (\u03bb p \u2192 (w , p)) (uniqP p\u2081 p\u2082))\n\u225f\u03a3' decA uniqP (w\u2081 , p\u2081) (w\u2082 , p\u2082) | no w\u2262 = no (w\u2262 \u2218 cong proj\u2081)\n\nproj\u2081-Injection : \u2200 {a b} {A : \u2605 a} {B : A \u2192 \u2605 b}\n \u2192 (\u2200 {x} (p\u2081 p\u2082 : B x) \u2192 p\u2081 \u2261 p\u2082)\n \u2192 Injection (setoid (\u03a3 A B))\n (setoid A)\nproj\u2081-Injection {B = B} B-uniq\n = record { to = \u2192-to-\u27f6 (proj\u2081 {B = B})\n ; injective = proj\u2081-injective B-uniq\n }\n\n\u0394 : \u2200 {a} {A : \u2605 a} \u2192 A \u2192 A \u00d7 A\n\u0394 x = x , x\n","old_contents":"-- move this to product\n{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Product.NP where\n\nopen import Type hiding (\u2605)\nopen import Level\nopen import Data.Product public hiding (\u2203)\nopen import Relation.Binary.PropositionalEquality as \u2261\nopen import Relation.Unary.NP hiding (Decidable)\nopen import Relation.Binary\nopen import Relation.Nullary\nopen import Function\nopen import Function.Injection using (Injection; module Injection)\nopen import Relation.Unary.Logical\nopen import Relation.Binary.Logical\n\n\u2203 : \u2200 {a b} {A : \u2605 a} \u2192 (A \u2192 \u2605 b) \u2192 \u2605 (a \u2294 b)\n\u2203 = \u03a3 _\n\nfirst : \u2200 {a b c} {A : \u2605 a} {B : \u2605 b} {C : B \u2192 \u2605 c} \u2192\n (f : A \u2192 B) \u2192 \u03a3 A (C \u2218 f) \u2192 \u03a3 B C\nfirst f = map f id -- f (x , y) = (f x , y)\n\n-- generalized first\u2032 but differently than first\nfirst' : \u2200 {a b c} {A : \u2605 a} {B : A \u2192 \u2605 b} {C : A \u2192 \u2605 c} \u2192\n (f : (x : A) \u2192 B x) (p : \u03a3 A C) \u2192 B (proj\u2081 p) \u00d7 C (proj\u2081 p)\nfirst' f (x , y) = (f x , y)\n\nfirst\u2032 : \u2200 {a b c} {A : \u2605 a} {B : \u2605 b} {C : \u2605 c} \u2192\n (f : A \u2192 B) \u2192 A \u00d7 C \u2192 B \u00d7 C\nfirst\u2032 = first\n\nsecond : \u2200 {a p q} {A : \u2605 a} {P : A \u2192 \u2605 p} {Q : A \u2192 \u2605 q} \u2192\n (\u2200 {x} \u2192 P x \u2192 Q x) \u2192 \u03a3 A P \u2192 \u03a3 A Q\nsecond = map id\n\nsecond\u2032 : \u2200 {a b c} {A : \u2605 a} {B : \u2605 b} {C : \u2605 c} \u2192\n (B \u2192 C) \u2192 A \u00d7 B \u2192 A \u00d7 C\nsecond\u2032 f = second f\n\nsyntax \u2203 (\u03bb x \u2192 e) = \u2203[ x ] e\n\nrecord [\u03a3] {a b a\u209a b\u209a}\n {A : \u2605 a}\n {B : A \u2192 \u2605 b}\n (A\u209a : A \u2192 \u2605 a\u209a)\n (B\u209a : {x : A} (x\u209a : A\u209a x)\n \u2192 B x \u2192 \u2605 b\u209a)\n (p : \u03a3 A B) : \u2605 (a\u209a \u2294 b\u209a) where\n constructor _[,]_\n field\n [proj\u2081] : A\u209a (proj\u2081 p)\n [proj\u2082] : B\u209a [proj\u2081] (proj\u2082 p)\nopen [\u03a3] public\ninfixr 4 _[,]_\n\nsyntax [\u03a3] A\u209a (\u03bb x\u209a \u2192 e) = [ x\u209a \u2236 A\u209a ][\u00d7][ e ]\n\n[\u2203] : \u2200 {a a\u209a b b\u209a} \u2192\n (\u2200i\u27e8 A\u209a \u2236 [\u2605] {a} a\u209a \u27e9[\u2192] ((A\u209a [\u2192] [\u2605] {b} b\u209a) [\u2192] [\u2605] _)) \u2203\n[\u2203] {x\u209a = A\u209a} = [\u03a3] A\u209a\n\nsyntax [\u2203] (\u03bb A\u209a \u2192 f) = [\u2203][ A\u209a ] f\n\n_[\u00d7]_ : \u2200 {a b a\u209a b\u209a} \u2192 ([\u2605] {a} a\u209a [\u2192] [\u2605] {b} b\u209a [\u2192] [\u2605] _) _\u00d7_\n_[\u00d7]_ A\u209a B\u209a = [\u03a3] A\u209a (\u03bb _ \u2192 B\u209a)\n\nprivate\n\n Dec\u27e6\u2605\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n (A\u1d63 : \u27e6\u2605\u27e7 a\u1d63 A\u2081 A\u2082)\n \u2192 \u2605 _\n Dec\u27e6\u2605\u27e7 A\u1d63 = \u2200 x\u2081 x\u2082 \u2192 Dec (A\u1d63 x\u2081 x\u2082)\n\n Dec\u27e6Pred\u27e7 : \u2200 {a\u2081 a\u2082 p\u2081 p\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n (A\u1d63 : \u27e6\u2605\u27e7 a\u1d63 A\u2081 A\u2082)\n {P\u2081 : Pred p\u2081 A\u2081} {P\u2082 : Pred p\u2082 A\u2082} {p\u1d63}\n \u2192 (P\u1d63 : \u27e6Pred\u27e7 p\u1d63 A\u1d63 P\u2081 P\u2082) \u2192 \u2605 _\n Dec\u27e6Pred\u27e7 {A\u2081 = A\u2081} {A\u2082} A\u1d63 {P\u2081} {P\u2082} P\u1d63 = \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) y\u2081 y\u2082 \u2192 Dec (P\u1d63 x\u1d63 y\u2081 y\u2082) -- Dec\u27e6\u2605\u27e7 A\u1d63 \u21d2 Dec\u27e6\u2605\u27e7 P\u1d63\n\nrecord \u27e6\u03a3\u27e7 {a\u2081 a\u2082 b\u2082 b\u2081 a\u1d63 b\u1d63}\n {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : A\u2081 \u2192 \u2605 b\u2081}\n {B\u2082 : A\u2082 \u2192 \u2605 b\u2082}\n (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n (B\u1d63 : {x\u2081 : A\u2081} {x\u2082 : A\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082)\n \u2192 B\u2081 x\u2081 \u2192 B\u2082 x\u2082 \u2192 \u2605 b\u1d63)\n (p\u2081 : \u03a3 A\u2081 B\u2081) (p\u2082 : \u03a3 A\u2082 B\u2082) : \u2605 (a\u1d63 \u2294 b\u1d63) where\n constructor _,_\n field\n \u27e6proj\u2081\u27e7 : A\u1d63 (proj\u2081 p\u2081) (proj\u2081 p\u2082)\n \u27e6proj\u2082\u27e7 : B\u1d63 \u27e6proj\u2081\u27e7 (proj\u2082 p\u2081) (proj\u2082 p\u2082)\nopen \u27e6\u03a3\u27e7 public\ninfixr 4 _,_\n\nsyntax \u27e6\u03a3\u27e7 A\u1d63 (\u03bb x\u1d63 \u2192 e) = [ x\u1d63 \u2236 A\u1d63 ]\u27e6\u00d7\u27e7[ e ]\n\n\u27e6\u2203\u27e7 : \u2200 {a\u2081 a\u2082 b\u2082 b\u2081 a\u1d63 b\u1d63}\n {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63}\n {B\u2081 : A\u2081 \u2192 \u2605 b\u2081}\n {B\u2082 : A\u2082 \u2192 \u2605 b\u2082}\n (B\u1d63 : \u27e6Pred\u27e7 b\u1d63 A\u1d63 B\u2081 B\u2082)\n (p\u2081 : \u03a3 A\u2081 B\u2081) (p\u2082 : \u03a3 A\u2082 B\u2082) \u2192 \u2605 _\n\u27e6\u2203\u27e7 = \u27e6\u03a3\u27e7 _\n\nsyntax \u27e6\u2203\u27e7 (\u03bb x\u1d63 \u2192 e) = \u27e6\u2203\u27e7[ x\u1d63 ] e\n\n_\u27e6\u00d7\u27e7_ : \u2200 {a\u2081 a\u2082 b\u2082 b\u2081 a\u1d63 b\u1d63}\n {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082}\n (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 \u2605 b\u1d63)\n (p\u2081 : A\u2081 \u00d7 B\u2081) (p\u2082 : A\u2082 \u00d7 B\u2082) \u2192 \u2605 (a\u1d63 \u2294 b\u1d63)\n_\u27e6\u00d7\u27e7_ A\u1d63 B\u1d63 = \u27e6\u03a3\u27e7 A\u1d63 (\u03bb _ \u2192 B\u1d63)\n\n{-\n_\u27e6\u00d7\u27e7_ : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 \u2605 a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 \u2605 b\u1d63)\n (p\u2081 : A\u2081 \u00d7 B\u2081) (p\u2082 : A\u2082 \u00d7 B\u2082) \u2192 \u2605 _\n_\u27e6\u00d7\u27e7_ A\u1d63 B\u1d63 = \u03bb p\u2081 p\u2082 \u2192 A\u1d63 (proj\u2081 p\u2081) (proj\u2081 p\u2082) \u00d7\n B\u1d63 (proj\u2082 p\u2081) (proj\u2082 p\u2082)\n-}\n\n{- One can give these two types to \u27e6_,_\u27e7:\n\n\u27e6_,_\u27e7' : \u2200 {a\u2081 a\u2082 b\u2081 b\u2082 a\u1d63 b\u1d63}\n {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : A\u2081 \u2192 \u2605 b\u2081} {B\u2082 : A\u2082 \u2192 \u2605 b\u2082}\n {A\u1d63 : \u27e6\u2605\u27e7 a\u1d63 A\u2081 A\u2082}\n {B\u1d63 : \u27e6Pred\u27e7 A\u1d63 b\u1d63 B\u2081 B\u2082}\n {x\u2081 x\u2082 y\u2081 y\u2082}\n (x\u1d63 : A\u1d63 x\u2081 x\u2082)\n (y\u1d63 : B\u1d63 x\u1d63 y\u2081 y\u2082)\n \u2192 \u27e6\u03a3\u27e7 A\u1d63 B\u1d63 (x\u2081 , y\u2081) (x\u2082 , y\u2082)\n\u27e6_,_\u27e7' = \u27e6_,_\u27e7\n\n\u27e6_,_\u27e7'' : \u2200 {a\u2081 a\u2082 b\u2081 b\u2082 a\u1d63 b\u1d63}\n {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : A\u2081 \u2192 \u2605 b\u2081} {B\u2082 : A\u2082 \u2192 \u2605 b\u2082}\n {A\u1d63 : \u27e6\u2605\u27e7 a\u1d63 A\u2081 A\u2082}\n {B\u1d63 : \u27e6Pred\u27e7 A\u1d63 b\u1d63 B\u2081 B\u2082}\n {p\u2081 p\u2082}\n (\u27e6proj\u2081\u27e7 : A\u1d63 (proj\u2081 p\u2081) (proj\u2081 p\u2082))\n (\u27e6proj\u2082\u27e7 : B\u1d63 \u27e6proj\u2081\u27e7 (proj\u2082 p\u2081) (proj\u2082 p\u2082))\n \u2192 \u27e6\u03a3\u27e7 A\u1d63 B\u1d63 p\u2081 p\u2082\n\u27e6_,_\u27e7'' = \u27e6_,_\u27e7\n-}\n\ndec\u27e6\u03a3\u27e7 : \u2200 {a\u2081 a\u2082 b\u2081 b\u2082 a\u1d63 b\u1d63 A\u2081 A\u2082 B\u2081 B\u2082}\n {A\u1d63 : \u27e6\u2605\u27e7 {a\u2081} {a\u2082} a\u1d63 A\u2081 A\u2082}\n {B\u1d63 : \u27e6Pred\u27e7 {b\u2081} {b\u2082} b\u1d63 A\u1d63 B\u2081 B\u2082}\n (decA\u1d63 : Dec\u27e6\u2605\u27e7 A\u1d63)\n -- (substA\u1d63 : \u2200 {x y \u2113} {p q} (P : A\u1d63 x y \u2192 \u2605 \u2113) \u2192 P p \u2192 P q)\n (uniqA\u1d63 : \u2200 {x y} (p q : A\u1d63 x y) \u2192 p \u2261 q)\n (decB\u1d63 : Dec\u27e6Pred\u27e7 A\u1d63 {_} {_} {b\u1d63 {-BUG: with _ here Agda loops-}} B\u1d63)\n \u2192 Dec\u27e6\u2605\u27e7 (\u27e6\u03a3\u27e7 A\u1d63 B\u1d63)\ndec\u27e6\u03a3\u27e7 {B\u1d63 = B\u1d63} decA\u1d63 uniqA\u1d63 decB\u1d63 (x\u2081 , y\u2081) (x\u2082 , y\u2082) with decA\u1d63 x\u2081 x\u2082\n... | no \u00acx\u1d63 = no (\u00acx\u1d63 \u2218 \u27e6proj\u2081\u27e7)\n... | yes x\u1d63 with decB\u1d63 x\u1d63 y\u2081 y\u2082\n... | yes y\u1d63 = yes (x\u1d63 , y\u1d63)\n... | no \u00acy\u1d63 = no (\u00acy\u1d63 \u2218 f \u2218 \u27e6proj\u2082\u27e7)\n where f : \u2200 {x\u1d63'} \u2192 B\u1d63 x\u1d63' y\u2081 y\u2082 \u2192 B\u1d63 x\u1d63 y\u2081 y\u2082\n f {x\u1d63'} y\u1d63 rewrite uniqA\u1d63 x\u1d63' x\u1d63 = y\u1d63\n\nmodule Dec\u2082 where\n map\u2082\u2032 : \u2200 {p\u2081 p\u2082 q} {P\u2081 : \u2605 p\u2081} {P\u2082 : \u2605 p\u2082} {Q : \u2605 q}\n \u2192 (P\u2081 \u2192 P\u2082 \u2192 Q) \u2192 (Q \u2192 P\u2081) \u2192 (Q \u2192 P\u2082) \u2192 Dec P\u2081 \u2192 Dec P\u2082 \u2192 Dec Q\n map\u2082\u2032 _ \u03c0\u2081 _ (no \u00acp\u2081) _ = no (\u00acp\u2081 \u2218 \u03c0\u2081)\n map\u2082\u2032 _ _ \u03c0\u2082 _ (no \u00acp\u2082) = no (\u00acp\u2082 \u2218 \u03c0\u2082)\n map\u2082\u2032 mk _ _ (yes p\u2081) (yes p\u2082) = yes (mk p\u2081 p\u2082)\n\ndec\u27e6\u00d7\u27e7 : \u2200 {a\u2081 a\u2082 b\u2081 b\u2082 a\u1d63 b\u1d63}\n {A\u2081 : \u2605 a\u2081} {A\u2082 : \u2605 a\u2082}\n {B\u2081 : \u2605 b\u2081} {B\u2082 : \u2605 b\u2082}\n {A\u1d63 : \u27e6\u2605\u27e7 a\u1d63 A\u2081 A\u2082}\n {B\u1d63 : \u27e6\u2605\u27e7 b\u1d63 B\u2081 B\u2082}\n (decA\u1d63 : Dec\u27e6\u2605\u27e7 A\u1d63)\n (decB\u1d63 : Dec\u27e6\u2605\u27e7 B\u1d63)\n \u2192 Dec\u27e6\u2605\u27e7 (A\u1d63 \u27e6\u00d7\u27e7 B\u1d63)\ndec\u27e6\u00d7\u27e7 decA\u1d63 decB\u1d63 (x\u2081 , y\u2081) (x\u2082 , y\u2082) with decA\u1d63 x\u2081 x\u2082\n... | no \u00acx\u1d63 = no (\u00acx\u1d63 \u2218 \u27e6proj\u2081\u27e7)\n... | yes x\u1d63 with decB\u1d63 y\u2081 y\u2082\n... | yes y\u1d63 = yes (x\u1d63 , y\u1d63)\n... | no \u00acy\u1d63 = no (\u00acy\u1d63 \u2218 \u27e6proj\u2082\u27e7)\n\nmk\u03a3\u2261 : \u2200 {a b} {A : \u2605 a} {x y : A} (B : A \u2192 \u2605 b) {p : B x} {q : B y} (xy : x \u2261 y) \u2192 subst B xy p \u2261 q \u2192 (x \u03a3., p) \u2261 (y , q)\nmk\u03a3\u2261 _ xy h rewrite xy | h = \u2261.refl\n\n\u03a3,-injective\u2082 : \u2200 {a b} {A : \u2605 a} {B : A \u2192 \u2605 b} {x : A} {y z : B x} \u2192 (_,_ {B = B} x y) \u2261 (x \u03a3., z) \u2192 y \u2261 z\n\u03a3,-injective\u2082 refl = refl\n\n\u03a3,-injective\u2081 : \u2200 {a b} {A : \u2605 a} {B : A \u2192 \u2605 b} {x\u2081 x\u2082 : A} {y\u2081 : B x\u2081} {y\u2082 : B x\u2082} \u2192 (x\u2081 \u03a3., y\u2081) \u2261 (x\u2082 , y\u2082) \u2192 x\u2081 \u2261 x\u2082\n\u03a3,-injective\u2081 refl = refl\n\nproj\u2081-injective : \u2200 {a b} {A : \u2605 a} {B : A \u2192 \u2605 b} {x y : \u03a3 A B}\n (B-uniq : \u2200 {z} (p\u2081 p\u2082 : B z) \u2192 p\u2081 \u2261 p\u2082)\n \u2192 proj\u2081 x \u2261 proj\u2081 y \u2192 x \u2261 y\nproj\u2081-injective {x = (a , p\u2081)} {y = (_ , p\u2082)} B-uniq eq rewrite sym eq\n = cong (\u03bb p \u2192 (a , p)) (B-uniq p\u2081 p\u2082)\n\nproj\u2082-irrelevance : \u2200 {a b} {A : \u2605 a} {B C : A \u2192 \u2605 b} {x\u2081 x\u2082 : A}\n {y\u2081 : B x\u2081} {y\u2082 : B x\u2082} {z\u2081 : C x\u2081} {z\u2082 : C x\u2082}\n \u2192 (C-uniq : \u2200 {z} (p\u2081 p\u2082 : C z) \u2192 p\u2081 \u2261 p\u2082)\n \u2192 (x\u2081 \u03a3., y\u2081) \u2261 (x\u2082 , y\u2082)\n \u2192 (x\u2081 \u03a3., z\u2081) \u2261 (x\u2082 , z\u2082)\nproj\u2082-irrelevance C-uniq = proj\u2081-injective C-uniq \u2218 \u03a3,-injective\u2081\n\n\u225f\u03a3 : \u2200 {A : \u2605\u2080} {P : A \u2192 \u2605\u2080}\n (decA : Decidable {A = A} _\u2261_)\n (decP : \u2200 x \u2192 Decidable {A = P x} _\u2261_)\n \u2192 Decidable {A = \u03a3 A P} _\u2261_\n\u225f\u03a3 decA decP (w\u2081 , p\u2081) (w\u2082 , p\u2082) with decA w\u2081 w\u2082\n\u225f\u03a3 decA decP (w , p\u2081) (.w , p\u2082) | yes refl with decP w p\u2081 p\u2082\n\u225f\u03a3 decA decP (w , p) (.w , .p) | yes refl | yes refl = yes refl\n\u225f\u03a3 decA decP (w , p\u2081) (.w , p\u2082) | yes refl | no p\u2262\n = no (p\u2262 \u2218 \u03a3,-injective\u2082)\n\u225f\u03a3 decA decP (w\u2081 , p\u2081) (w\u2082 , p\u2082) | no w\u2262 = no (w\u2262 \u2218 cong proj\u2081)\n\n\u225f\u03a3' : \u2200 {A : \u2605\u2080} {P : A \u2192 \u2605\u2080}\n (decA : Decidable {A = A} _\u2261_)\n (uniqP : \u2200 {x} (p q : P x) \u2192 p \u2261 q)\n \u2192 Decidable {A = \u03a3 A P} _\u2261_\n\u225f\u03a3' decA uniqP (w\u2081 , p\u2081) (w\u2082 , p\u2082) with decA w\u2081 w\u2082\n\u225f\u03a3' decA uniqP (w , p\u2081) (.w , p\u2082) | yes refl\n = yes (cong (\u03bb p \u2192 (w , p)) (uniqP p\u2081 p\u2082))\n\u225f\u03a3' decA uniqP (w\u2081 , p\u2081) (w\u2082 , p\u2082) | no w\u2262 = no (w\u2262 \u2218 cong proj\u2081)\n\nproj\u2081-Injection : \u2200 {a b} {A : \u2605 a} {B : A \u2192 \u2605 b}\n \u2192 (\u2200 {x} (p\u2081 p\u2082 : B x) \u2192 p\u2081 \u2261 p\u2082)\n \u2192 Injection (setoid (\u03a3 A B))\n (setoid A)\nproj\u2081-Injection {B = B} B-uniq\n = record { to = \u2192-to-\u27f6 (proj\u2081 {B = B})\n ; injective = proj\u2081-injective B-uniq\n }\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"56460e69fe9711b8fb337c5527a4b79861d48324","subject":"Redid the model -- it is now given as System F-omega plus a kind for time.","message":"Redid the model -- it is now given as System F-omega plus a kind for time.\n","repos":"agda\/agda-frp-js,agda\/agda-frp-js","old_file":"src\/agda\/FRP\/JS\/Model.agda","new_file":"src\/agda\/FRP\/JS\/Model.agda","new_contents":"open import FRP.JS.Level using ( Level ; _\u2294_ ) renaming ( zero to o ; suc to \u2191 )\nopen import FRP.JS.Time using ( Time ; _\u225f_ ; _\u2264_ ; _<_ )\nopen import FRP.JS.Bool using ( true ; false )\nopen import FRP.JS.True using ( True ; tt )\n\nmodule FRP.JS.Model where\n\n-- This model is essentially System F-omega with a kind time\n-- together with a type for the partial order on time,\n-- and expressions for reflexivity and transitivity.\n-- We prove parametricity, and then show that parametricity implies causality.\n\n-- Note that this is a \"deep\" notion of causality, not the \"shallow\"\n-- causality usually used in FRP. The pragmatic upshot of this is that\n-- there is only one time model: nested signals are in the same time\n-- model, not a simulated time model. This fits with the JS implementation,\n-- which uses wall clock time for all signals.\n\n-- Propositional equality\n\ndata _\u2261_ {\u03b1} {A : Set \u03b1} (a : A) : A \u2192 Set \u03b1 where\n refl : a \u2261 a\n\ntrans : \u2200 {\u03b1} {A : Set \u03b1} {a b c : A} \u2192 (a \u2261 b) \u2192 (b \u2261 c) \u2192 (a \u2261 c)\ntrans refl refl = refl\n\ncong : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : Set \u03b2} (f : A \u2192 B) {a\u2081 a\u2082 : A} \u2192\n (a\u2081 \u2261 a\u2082) \u2192 (f a\u2081 \u2261 f a\u2082)\ncong f refl = refl\n\napply : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : Set \u03b2} {F G : A \u2192 B} \u2192 (F \u2261 G) \u2192 \n \u2200 {a b} \u2192 (a \u2261 b) \u2192 (F a \u2261 G b)\napply refl refl = refl\n\ncast : \u2200 {\u03b1} {A B : Set \u03b1} \u2192 (A \u2261 B) \u2192 A \u2192 B\ncast refl a = a\n\ncast\u00b2 : \u2200 {\u03b1} {A B : Set \u03b1} {\u211c \u2111 : A \u2192 B \u2192 Set \u03b1} \u2192 (\u211c \u2261 \u2111) \u2192 \u2200 {a b} \u2192 \u211c a b \u2192 \u2111 a b\ncast\u00b2 refl a\u211cb = a\u211cb\n\nirrel : \u2200 b \u2192 (b\u2081 b\u2082 : True b) \u2192 (b\u2081 \u2261 b\u2082)\nirrel true tt tt = refl\nirrel false () ()\n\n-- Postulates (including dependent extensionality)\n\npostulate\n \u2264-refl : \u2200 t \u2192 True (t \u2264 t)\n \u2264-trans : \u2200 t u v \u2192 True (t \u2264 u) \u2192 True (u \u2264 v) \u2192 True (t \u2264 v)\n dext : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : A \u2192 Set \u03b2} {F G : \u2200 a \u2192 B a} \u2192 (\u2200 a \u2192 F a \u2261 G a) \u2192 (F \u2261 G)\n\next : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : Set \u03b2} {F G : A \u2192 B} \u2192\n (\u2200 a \u2192 F a \u2261 G a) \u2192 (F \u2261 G)\next = dext\n\niext : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : A \u2192 Set \u03b2} {F G : \u2200 {a} \u2192 B a} \u2192 \n (\u2200 a \u2192 F {a} \u2261 G {a}) \u2192 ((\u03bb {a} \u2192 F {a}) \u2261 (\u03bb {a} \u2192 G {a}))\niext F\u2248G = cong (\u03bb X {a} \u2192 X a) (dext F\u2248G)\n\n-- Finite products\n\nrecord \u22a4 {\u03b1} : Set \u03b1 where\n constructor tt\n\nopen \u22a4 public\n\nrecord \u03a3 {\u03b1 \u03b2} (A : Set \u03b1) (B : A \u2192 Set \u03b2) : Set (\u03b1 \u2294 \u03b2) where\n constructor _,_\n field\n proj\u2081 : A\n proj\u2082 : B proj\u2081\n\nopen \u03a3 public\n\n_\u00d7_ : \u2200 {\u03b1 \u03b2} \u2192 Set \u03b1 \u2192 Set \u03b2 \u2192 Set (\u03b1 \u2294 \u03b2)\nA \u00d7 B = \u03a3 A (\u03bb a \u2192 B)\n\n_\u00d7\u00b2_ : \u2200 {\u03b1 \u03b2} {A C : Set \u03b1} {B D : Set \u03b2} \u2192 \n (A \u2192 C \u2192 Set \u03b1) \u2192 (B \u2192 D \u2192 Set \u03b2) \u2192 ((A \u00d7 B) \u2192 (C \u00d7 D) \u2192 Set (\u03b1 \u2294 \u03b2))\n(\u211c \u00d7\u00b2 \u2111) (a , b) (c , d) = (\u211c a c \u00d7 \u2111 b d)\n\n_\u2192\u00b2_ : \u2200 {\u03b1 \u03b2} {A C : Set \u03b1} {B D : Set \u03b2} \u2192 \n (A \u2192 C \u2192 Set \u03b1) \u2192 (B \u2192 D \u2192 Set \u03b2) \u2192 ((A \u2192 B) \u2192 (C \u2192 D) \u2192 Set (\u03b1 \u2294 \u03b2))\n(\u211c \u2192\u00b2 \u2111) f g = \u2200 {a b} \u2192 \u211c a b \u2192 \u2111 (f a) (g b)\n\n-- Reactive sets\n\nRSet : \u2200 \u03b1 \u2192 Set (\u2191 \u03b1)\nRSet \u03b1 = Time \u2192 Set \u03b1\n\n-- Equalitional reasoning\n\ninfix 4 _IsRelatedTo_\ninfix 2 _\u220e\ninfixr 2 _\u2261\u27e8_\u27e9_\ninfix 1 begin_\n\ndata _IsRelatedTo_ {\u03b1} {A : Set \u03b1} (a b : A) : Set \u03b1 where\n relTo : (a\u2261b : a \u2261 b) \u2192 a IsRelatedTo b\n\nbegin_ : \u2200 {\u03b1} {A : Set \u03b1} {a b : A} \u2192 a IsRelatedTo b \u2192 a \u2261 b\nbegin relTo a\u2261b = a\u2261b\n\n_\u2261\u27e8_\u27e9_ : \u2200 {\u03b1} {A : Set \u03b1} a {b c : A} \u2192 a \u2261 b \u2192 b IsRelatedTo c \u2192 a IsRelatedTo c\n_ \u2261\u27e8 a\u2261b \u27e9 relTo b\u2261c = relTo (trans a\u2261b b\u2261c)\n\n_\u220e : \u2200 {\u03b1} {A : Set \u03b1} (a : A) \u2192 a IsRelatedTo a\n_\u220e _ = relTo refl\n\n-- Kinds\n\ndata Kind : Set where\n time : Kind\n set : Level \u2192 Kind\n _\u21d2_ : Kind \u2192 Kind \u2192 Kind\n\nlevel : Kind \u2192 Level\nlevel time = o\nlevel (set \u03b1) = \u2191 \u03b1\nlevel (K \u21d2 L) = level K \u2294 level L\n\nK\u27e6_\u27e7 : \u2200 K \u2192 Set (level K)\nK\u27e6 time \u27e7 = Time\nK\u27e6 set \u03b1 \u27e7 = Set \u03b1\nK\u27e6 K \u21d2 L \u27e7 = K\u27e6 K \u27e7 \u2192 K\u27e6 L \u27e7\n\n_\u220b_\u2194_ : \u2200 K \u2192 K\u27e6 K \u27e7 \u2192 K\u27e6 K \u27e7 \u2192 Set (level K)\ntime \u220b t \u2194 u = (t \u2261 u)\nset \u03b1 \u220b A \u2194 B = A \u2192 B \u2192 Set \u03b1\n(K \u21d2 L) \u220b F \u2194 G = \u2200 {A B} \u2192 (K \u220b A \u2194 B) \u2192 (L \u220b F A \u2194 G B)\n\n-- \u2261 can be used as a structural equivalence on relations.\n\nstruct : \u2200 K {A B C D} \u2192 (A \u2261 B) \u2192 (K \u220b B \u2194 D) \u2192 (C \u2261 D) \u2192 (K \u220b A \u2194 C)\nstruct K refl \u211c refl = \u211c\n\nstruct-ext : \u2200 K L {A B} {F G H I : K\u27e6 K \u21d2 L \u27e7} \n (F\u2248G : \u2200 A \u2192 F A \u2261 G A) (\u211c : (K \u21d2 L) \u220b G \u2194 I) (H\u2248I : \u2200 B \u2192 H B \u2261 I B) (\u2111 : K \u220b A \u2194 B) \u2192\n struct L (F\u2248G A) (\u211c \u2111) (H\u2248I B) \u2261 struct (K \u21d2 L) (ext F\u2248G) \u211c (ext H\u2248I) \u2111\nstruct-ext K L {A} {B} F\u2248G \u211c H\u2248I \u2111 \n with ext F\u2248G | ext H\u2248I | F\u2248G A | H\u2248I B\n... | refl | refl | refl | refl = refl\n\nstruct-apply : \u2200 K L {F G H I A B C D} \u2192 \n (F\u2261G : F \u2261 G) (\u211c : (K \u21d2 L) \u220b G \u2194 I) (H\u2261I : H \u2261 I) \u2192 \n (A\u2261B : A \u2261 B) (\u2111 : K \u220b B \u2194 D) (C\u2261D : C \u2261 D) \u2192 \n struct (K \u21d2 L) F\u2261G \u211c H\u2261I (struct K A\u2261B \u2111 C\u2261D)\n \u2261 struct L (apply F\u2261G A\u2261B) (\u211c \u2111) (apply H\u2261I C\u2261D)\nstruct-apply K L refl \u211c refl refl \u2111 refl = refl\n\nstruct-cast : \u2200 {\u03b1 A B C D} (\u211c : set \u03b1 \u220b B \u2194 D) (A\u2261B : A \u2261 B) (C\u2261D : C \u2261 D) {a c} \u2192\n struct (set \u03b1) A\u2261B \u211c C\u2261D a c \u2192 \u211c (cast A\u2261B a) (cast C\u2261D c)\nstruct-cast \u211c refl refl a\u211cc = a\u211cc\n\n-- Type contexts\n\ninfixr 4 _\u2237_\n\ndata Kinds : Set where\n [] : Kinds\n _\u2237_ : Kind \u2192 Kinds \u2192 Kinds\n\nlevels : Kinds \u2192 Level\nlevels [] = o\nlevels (K \u2237 \u03a3) = level K \u2294 levels \u03a3\n\n\u03a3\u27e6_\u27e7 : \u2200 \u03a3 \u2192 Set (levels \u03a3)\n\u03a3\u27e6 [] \u27e7 = \u22a4\n\u03a3\u27e6 K \u2237 \u03a3 \u27e7 = K\u27e6 K \u27e7 \u00d7 \u03a3\u27e6 \u03a3 \u27e7\n\n_\u220b_\u2194*_ : \u2200 \u03a3 \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 Set (levels \u03a3)\n[] \u220b tt \u2194* tt = \u22a4\n(K \u2237 \u03a3) \u220b (A , As) \u2194* (B , Bs) = (K \u220b A \u2194 B) \u00d7 (\u03a3 \u220b As \u2194* Bs)\n\n-- Inclusion order on type contexts.\n-- Credited by Randy Pollack to Geuvers and Nederhof, JAR 1991.\n-- http:\/\/thread.gmane.org\/gmane.comp.lang.agda\/3259\/focus=3267\n\ndata _\u2291_ : Kinds \u2192 Kinds \u2192 Set where\n id : \u2200 {\u03a3} \u2192 \u03a3 \u2291 \u03a3\n keep : \u2200 K {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 ((K \u2237 \u03a3) \u2291 (K \u2237 \u03a5))\n skip : \u2200 K {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 (\u03a3 \u2291 (K \u2237 \u03a5))\n\n\u2291\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 \u03a3\u27e6 \u03a5 \u27e7 \u2192 \u03a3\u27e6 \u03a3 \u27e7\n\u2291\u27e6 id \u27e7 As = As\n\u2291\u27e6 keep K \u03a3\u2291\u03a5 \u27e7 (A , As) = (A , \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As)\n\u2291\u27e6 skip K \u03a3\u2291\u03a5 \u27e7 (A , As) = \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As\n\n\u2291\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5} \u2192 (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) \u2192 \u2200 {As Bs} \u2192 (\u03a5 \u220b As \u2194* Bs) \u2192 (\u03a3 \u220b \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As \u2194* \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 Bs)\n\u2291\u27e6 id \u27e7\u00b2 \u211cs = \u211cs\n\u2291\u27e6 keep K \u03a3\u2291\u03a5 \u27e7\u00b2 (\u211c , \u211cs) = (\u211c , \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)\n\u2291\u27e6 skip K \u03a3\u2291\u03a5 \u27e7\u00b2 (\u211c , \u211cs) = \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs\n\n-- Type variables\n\ndata TVar (K : Kind) : Kinds \u2192 Set where\n zero : \u2200 {\u03a3} \u2192 TVar K (K \u2237 \u03a3)\n suc : \u2200 {L \u03a3} \u2192 TVar K \u03a3 \u2192 TVar K (L \u2237 \u03a3)\n\n\u03c4\u27e6_\u27e7 : \u2200 {\u03a3 K} (\u03c4 : TVar K \u03a3) \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 K\u27e6 K \u27e7\n\u03c4\u27e6 zero \u27e7 (A , As) = A\n\u03c4\u27e6 suc \u03c4 \u27e7 (A , As) = \u03c4\u27e6 \u03c4 \u27e7 As\n\n\u03c4\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 K} (\u03c4 : TVar K \u03a3) {As Bs} \u2192 (\u03a3 \u220b As \u2194* Bs) \u2192 (K \u220b \u03c4\u27e6 \u03c4 \u27e7 As \u2194 \u03c4\u27e6 \u03c4 \u27e7 Bs)\n\u03c4\u27e6 zero \u27e7\u00b2 (\u211c , \u211cs) = \u211c\n\u03c4\u27e6 suc \u03c4 \u27e7\u00b2 (\u211c , \u211cs) = \u03c4\u27e6 \u03c4 \u27e7\u00b2 \u211cs\n\n-- Type constants\n\ndata TConst : Kind \u2192 Set where\n prod fun : \u2200 {\u03b1 \u03b2} \u2192 TConst (set \u03b1 \u21d2 (set \u03b2 \u21d2 set (\u03b1 \u2294 \u03b2)))\n leq lt : TConst (time \u21d2 (time \u21d2 set o))\n univ : \u2200 K {\u03b1} \u2192 TConst ((K \u21d2 set \u03b1) \u21d2 set (level K \u2294 \u03b1))\n\nC\u27e6_\u27e7 : \u2200 {K} \u2192 (TConst K) \u2192 K\u27e6 K \u27e7\nC\u27e6 prod \u27e7 = \u03bb A B \u2192 (A \u00d7 B)\nC\u27e6 fun \u27e7 = \u03bb A B \u2192 (A \u2192 B)\nC\u27e6 leq \u27e7 = \u03bb t u \u2192 True (t \u2264 u)\nC\u27e6 lt \u27e7 = \u03bb t u \u2192 True (t < u)\nC\u27e6 univ K \u27e7 = \u03bb F \u2192 \u2200 A \u2192 F A\n\nC\u27e6_\u27e7\u00b2 : \u2200 {K} (C : TConst K) \u2192 (K \u220b C\u27e6 C \u27e7 \u2194 C\u27e6 C \u27e7)\nC\u27e6 prod \u27e7\u00b2 = \u03bb \u211c \u2111 \u2192 (\u211c \u00d7\u00b2 \u2111)\nC\u27e6 fun \u27e7\u00b2 = \u03bb \u211c \u2111 \u2192 (\u211c \u2192\u00b2 \u2111)\nC\u27e6 leq \u27e7\u00b2 = \u03bb _ _ _ _ \u2192 \u22a4\nC\u27e6 lt \u27e7\u00b2 = \u03bb _ _ _ _ \u2192 \u22a4\nC\u27e6 univ K \u27e7\u00b2 = \u03bb \u211c f g \u2192 \u2200 {a b} \u2111 \u2192 \u211c \u2111 (f a) (g b)\n\n-- Types\n\ndata Typ (\u03a3 : Kinds) : Kind \u2192 Set where\n const : \u2200 {K} \u2192 TConst K \u2192 Typ \u03a3 K\n abs : \u2200 K {L} \u2192 Typ (K \u2237 \u03a3) L \u2192 Typ \u03a3 (K \u21d2 L)\n app : \u2200 {K L} \u2192 Typ \u03a3 (K \u21d2 L) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L\n var : \u2200 {K} \u2192 TVar K \u03a3 \u2192 Typ \u03a3 K\n\ntlevel : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (set \u03b1) \u2192 Level\ntlevel {\u03a3} {\u03b1} T = \u03b1\n\nT\u27e6_\u27e7 : \u2200 {\u03a3 K} (T : Typ \u03a3 K) \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 K\u27e6 K \u27e7\nT\u27e6 const C \u27e7 As = C\u27e6 C \u27e7\nT\u27e6 abs K T \u27e7 As = \u03bb A \u2192 T\u27e6 T \u27e7 (A , As)\nT\u27e6 app T U \u27e7 As = T\u27e6 T \u27e7 As (T\u27e6 U \u27e7 As)\nT\u27e6 var \u03c4 \u27e7 As = \u03c4\u27e6 \u03c4 \u27e7 As\n\nT\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 K} (T : Typ \u03a3 K) {As Bs} \u2192 (\u03a3 \u220b As \u2194* Bs) \u2192 (K \u220b T\u27e6 T \u27e7 As \u2194 T\u27e6 T \u27e7 Bs)\nT\u27e6 const C \u27e7\u00b2 \u211cs = C\u27e6 C \u27e7\u00b2\nT\u27e6 abs K T \u27e7\u00b2 \u211cs = \u03bb \u211c \u2192 T\u27e6 T \u27e7\u00b2 (\u211c , \u211cs)\nT\u27e6 app T U \u27e7\u00b2 \u211cs = T\u27e6 T \u27e7\u00b2 \u211cs (T\u27e6 U \u27e7\u00b2 \u211cs)\nT\u27e6 var \u03c4 \u27e7\u00b2 \u211cs = \u03c4\u27e6 \u03c4 \u27e7\u00b2 \u211cs\n\n-- Type shorthands\n\napp\u2082 : \u2200 {\u03a3 K L M} \u2192 Typ \u03a3 (K \u21d2 (L \u21d2 M)) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L \u2192 Typ \u03a3 M\napp\u2082 T U V = app (app T U) V\n\ncapp : \u2200 {\u03a3 K L} \u2192 TConst (K \u21d2 L) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L\ncapp C = app (const C)\n\ncapp\u2082 : \u2200 {\u03a3 K L M} \u2192 TConst (K \u21d2 (L \u21d2 M)) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L \u2192 Typ \u03a3 M\ncapp\u2082 C = app\u2082 (const C)\n\n_\u2297_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (set \u03b1) \u2192 Typ \u03a3 (set \u03b2) \u2192 Typ \u03a3 (set (\u03b1 \u2294 \u03b2))\n_\u2297_ = capp\u2082 prod\n\n_\u22b8_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (set \u03b1) \u2192 Typ \u03a3 (set \u03b2) \u2192 Typ \u03a3 (set (\u03b1 \u2294 \u03b2))\n_\u22b8_ = capp\u2082 fun\n\n_\u227c_ : \u2200 {\u03a3} \u2192 Typ \u03a3 time \u2192 Typ \u03a3 time \u2192 Typ \u03a3 (set o)\n_\u227c_ = capp\u2082 leq\n\n_\u227a_ : \u2200 {\u03a3} \u2192 Typ \u03a3 time \u2192 Typ \u03a3 time \u2192 Typ \u03a3 (set o)\n_\u227a_ = capp\u2082 lt\n\n\u03a0 : \u2200 {\u03a3 \u03b1} K \u2192 Typ (K \u2237 \u03a3) (set \u03b1) \u2192 Typ \u03a3 (set (level K \u2294 \u03b1))\n\u03a0 K T = capp (univ K) (abs K T)\n\ntvar\u2080 : \u2200 {\u03a3 K} \u2192 Typ (K \u2237 \u03a3) K\ntvar\u2080 = var zero\n\ntvar\u2081 : \u2200 {\u03a3 K L} \u2192 Typ (L \u2237 K \u2237 \u03a3) K\ntvar\u2081 = var (suc zero)\n\ntvar\u2082 : \u2200 {\u03a3 K L M} \u2192 Typ (M \u2237 L \u2237 K \u2237 \u03a3) K\ntvar\u2082 = var (suc (suc zero))\n\ntvar\u2083 : \u2200 {\u03a3 K L M N} \u2192 Typ (N \u2237 M \u2237 L \u2237 K \u2237 \u03a3) K\ntvar\u2083 = var (suc (suc (suc zero)))\n\nrset : Level \u2192 Kind\nrset \u03b1 = time \u21d2 set \u03b1\n\nrset\u2080 : Kind\nrset\u2080 = rset o\n\nprod\u02b3 : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 (rset \u03b2 \u21d2 rset (\u03b1 \u2294 \u03b2)))\nprod\u02b3 {\u03a3} {\u03b1} {\u03b2} = abs (rset \u03b1) (abs (rset \u03b2) (abs time (app tvar\u2082 tvar\u2080 \u2297 app tvar\u2081 tvar\u2080)))\n\n_\u2297\u02b3_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1) \u2192 Typ \u03a3 (rset \u03b2) \u2192 Typ \u03a3 (rset (\u03b1 \u2294 \u03b2))\n_\u2297\u02b3_ = app\u2082 prod\u02b3\n\nfun\u02b3 : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 (rset \u03b2 \u21d2 rset (\u03b1 \u2294 \u03b2)))\nfun\u02b3 {\u03a3} {\u03b1} {\u03b2} = abs (rset \u03b1) (abs (rset \u03b2) (abs time (app tvar\u2082 tvar\u2080 \u22b8 app tvar\u2081 tvar\u2080)))\n\n_\u22b8\u02b3_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1) \u2192 Typ \u03a3 (rset \u03b2) \u2192 Typ \u03a3 (rset (\u03b1 \u2294 \u03b2))\n_\u22b8\u02b3_ = app\u2082 fun\u02b3\n\nalways : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (set \u03b1 \u21d2 rset \u03b1)\nalways {\u03a3} {\u03b1} = abs (set \u03b1) (abs time tvar\u2081)\n\ntaut : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 set \u03b1)\ntaut {\u03a3} {\u03b1} = abs (rset \u03b1) (\u03a0 time (app tvar\u2081 tvar\u2080))\n\ninterval : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 (time \u21d2 (time \u21d2 set \u03b1)))\ninterval {\u03a3} {\u03b1} = abs (rset \u03b1) (abs time (abs time (\u03a0 time \n ((tvar\u2082 \u227c tvar\u2080) \u22b8 ((tvar\u2080 \u227c tvar\u2081) \u22b8 app tvar\u2083 tvar\u2080)))))\n\nconstreq : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 (rset \u03b2 \u21d2 rset (\u03b1 \u2294 \u03b2)))\nconstreq {\u03a3} {\u03b1} {\u03b2} = abs (rset \u03b1) (abs (rset \u03b2) (abs time (\u03a0 time \n ((tvar\u2081 \u227c tvar\u2080) \u22b8 (app (app (app interval tvar\u2083) tvar\u2081) tvar\u2080 \u22b8 app tvar\u2082 tvar\u2080)))))\n\n_\u22b5_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1) \u2192 Typ \u03a3 (rset \u03b2) \u2192 Typ \u03a3 (rset (\u03b1 \u2294 \u03b2))\n_\u22b5_ = app\u2082 constreq\n\n-- Contexts\n\ndata Typs (\u03a3 : Kinds) : Set where\n [] : Typs \u03a3\n _\u2237_ : \u2200 {\u03b1} \u2192 (Typ \u03a3 (set \u03b1)) \u2192 Typs \u03a3 \u2192 Typs \u03a3\n\ntlevels : \u2200 {\u03a3} \u2192 Typs \u03a3 \u2192 Level\ntlevels [] = o\ntlevels (T \u2237 \u0393) = tlevel T \u2294 tlevels \u0393\n\n\u0393\u27e6_\u27e7 : \u2200 {\u03a3} (\u0393 : Typs \u03a3) \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 Set (tlevels \u0393)\n\u0393\u27e6 [] \u27e7 As = \u22a4\n\u0393\u27e6 T \u2237 \u0393 \u27e7 As = T\u27e6 T \u27e7 As \u00d7 \u0393\u27e6 \u0393 \u27e7 As\n\n\u0393\u27e6_\u27e7\u00b2 : \u2200 {\u03a3} (\u0393 : Typs \u03a3) {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 (\u0393\u27e6 \u0393 \u27e7 As \u2192 \u0393\u27e6 \u0393 \u27e7 Bs \u2192 Set (tlevels \u0393))\n\u0393\u27e6 [] \u27e7\u00b2 \u211cs tt tt = \u22a4\n\u0393\u27e6 T \u2237 \u0393 \u27e7\u00b2 \u211cs (a , as) (b , bs) = T\u27e6 T \u27e7\u00b2 \u211cs a b \u00d7 \u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs as bs\n\n-- Weakening of type variables\n\n\u03c4weaken : \u2200 {\u03a3 \u03a5 K} \u2192 (\u03a3 \u2291 \u03a5) \u2192 TVar K \u03a3 \u2192 TVar K \u03a5\n\u03c4weaken id x = x\n\u03c4weaken (keep K \u03a3\u2291\u03a5) zero = zero\n\u03c4weaken (keep K \u03a3\u2291\u03a5) (suc x) = suc (\u03c4weaken \u03a3\u2291\u03a5 x)\n\u03c4weaken (skip K \u03a3\u2291\u03a5) x = suc (\u03c4weaken \u03a3\u2291\u03a5 x)\n\n\u03c4weaken\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5 K} (\u03c4 : TVar K \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) (As : \u03a3\u27e6 \u03a5 \u27e7) \u2192 \n \u03c4\u27e6 \u03c4 \u27e7 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As) \u2261 \u03c4\u27e6 \u03c4weaken \u03a3\u2291\u03a5 \u03c4 \u27e7 As\n\u03c4weaken\u27e6 \u03c4 \u27e7 id As = refl\n\u03c4weaken\u27e6 zero \u27e7 (keep K \u03a3\u2291\u03a5) (A , As) = refl\n\u03c4weaken\u27e6 suc \u03c4 \u27e7 (keep K \u03a3\u2291\u03a5) (A , As) = \u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As\n\u03c4weaken\u27e6 \u03c4 \u27e7 (skip K \u03a3\u2291\u03a5) (A , As) = \u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As\n\n\u03c4weaken\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5 K} (\u03c4 : TVar K \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) {As Bs} (\u211cs : \u03a5 \u220b As \u2194* Bs) \u2192 \n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) \u2261 \n struct K (\u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As) (\u03c4\u27e6 \u03c4weaken \u03a3\u2291\u03a5 \u03c4 \u27e7\u00b2 \u211cs) (\u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 Bs)\n\u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 id \u211cs = refl\n\u03c4weaken\u27e6 zero \u27e7\u00b2 (keep K \u03a3\u2291\u03a5) (\u211c , \u211cs) = refl\n\u03c4weaken\u27e6 suc \u03c4 \u27e7\u00b2 (keep K \u03a3\u2291\u03a5) (\u211c , \u211cs) = \u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs\n\u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 (skip K \u03a3\u2291\u03a5) (\u211c , \u211cs) = \u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs\n\n-- Weakening of types\n\nweaken : \u2200 {\u03a3 \u03a5 K} \u2192 (\u03a3 \u2291 \u03a5) \u2192 Typ \u03a3 K \u2192 Typ \u03a5 K\nweaken \u03a3\u2291\u03a5 (const C) = const C\nweaken \u03a3\u2291\u03a5 (abs K T) = abs K (weaken (keep K \u03a3\u2291\u03a5) T)\nweaken \u03a3\u2291\u03a5 (app T U) = app (weaken \u03a3\u2291\u03a5 T) (weaken \u03a3\u2291\u03a5 U)\nweaken \u03a3\u2291\u03a5 (var \u03c4) = var (\u03c4weaken \u03a3\u2291\u03a5 \u03c4)\n\nweaken\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5 K} (T : Typ \u03a3 K) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) (As : \u03a3\u27e6 \u03a5 \u27e7) \u2192 \n T\u27e6 T \u27e7 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As) \u2261 T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7 As\nweaken\u27e6 const C \u27e7 \u03a3\u2291\u03a5 As = refl\nweaken\u27e6 abs K T \u27e7 \u03a3\u2291\u03a5 As = ext (\u03bb A \u2192 weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (A , As))\nweaken\u27e6 app T U \u27e7 \u03a3\u2291\u03a5 As = apply (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) \nweaken\u27e6 var \u03c4 \u27e7 \u03a3\u2291\u03a5 As = \u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As\n \nweaken\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5 K} (T : Typ \u03a3 K) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) {As Bs} (\u211cs : \u03a5 \u220b As \u2194* Bs) \u2192 \n T\u27e6 T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) \u2261 struct K (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 Bs)\nweaken\u27e6 const C \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs = refl\nweaken\u27e6 abs K {L} T \u27e7\u00b2 \u03a3\u2291\u03a5 {As} {Bs} \u211cs =\n iext (\u03bb A \u2192 iext (\u03bb B \u2192 ext (\u03bb \u211c \u2192 begin\n T\u27e6 abs K T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) \u211c\n \u2261\u27e8 weaken\u27e6 T \u27e7\u00b2 (keep K \u03a3\u2291\u03a5) (\u211c , \u211cs) \u27e9\n struct L \n (weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (A , As)) \n (T\u27e6 weaken (keep K \u03a3\u2291\u03a5) T \u27e7\u00b2 (\u211c , \u211cs)) \n (weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (B , Bs))\n \u2261\u27e8 struct-ext K L \n (\u03bb A \u2192 weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (A , As)) \n (\u03bb \u211c \u2192 T\u27e6 weaken (keep K \u03a3\u2291\u03a5) T \u27e7\u00b2 (\u211c , \u211cs)) \n (\u03bb B \u2192 weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (B , Bs)) \u211c \u27e9\n struct (K \u21d2 L) \n (weaken\u27e6 abs K T \u27e7 \u03a3\u2291\u03a5 As)\n (T\u27e6 weaken \u03a3\u2291\u03a5 (abs K T) \u27e7\u00b2 \u211cs) \n (weaken\u27e6 abs K T \u27e7 \u03a3\u2291\u03a5 Bs) \u211c\n \u220e)))\nweaken\u27e6 app {K} {L} T U \u27e7\u00b2 \u03a3\u2291\u03a5 {As} {Bs} \u211cs = \n begin\n T\u27e6 app T U \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)\n \u2261\u27e8 cong (T\u27e6 T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)) (weaken\u27e6 U \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs) \u27e9\n T\u27e6 T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)\n (struct K (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs))\n \u2261\u27e8 cong (\u03bb X \u2192 X (struct K (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs)))\n (weaken\u27e6 T \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs) \u27e9\n (struct (K \u21d2 L) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 Bs))\n (struct K (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs))\n \u2261\u27e8 struct-apply K L \n (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 Bs) \n (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs) \u27e9\n struct L\n (weaken\u27e6 app T U \u27e7 \u03a3\u2291\u03a5 As)\n (T\u27e6 weaken \u03a3\u2291\u03a5 (app T U) \u27e7\u00b2 \u211cs) \n (weaken\u27e6 app T U \u27e7 \u03a3\u2291\u03a5 Bs)\n \u220e\nweaken\u27e6 var \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs = \u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs\n\n-- Weakening on type contexts\n\nweakens : \u2200 {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 Typs \u03a3 \u2192 Typs \u03a5\nweakens \u03a3\u2291\u03a5 [] = []\nweakens \u03a3\u2291\u03a5 (T \u2237 \u0393) = weaken \u03a3\u2291\u03a5 T \u2237 weakens \u03a3\u2291\u03a5 \u0393\n\nweakens\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5} (\u0393 : Typs \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) (As : \u03a3\u27e6 \u03a5 \u27e7) \u2192 \n \u0393\u27e6 \u0393 \u27e7 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As) \u2192 \u0393\u27e6 weakens \u03a3\u2291\u03a5 \u0393 \u27e7 As\nweakens\u27e6 [] \u27e7 \u03a3\u2291\u03a5 As tt = tt\nweakens\u27e6 T \u2237 \u0393 \u27e7 \u03a3\u2291\u03a5 As (B , Bs) = (cast (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) B , weakens\u27e6 \u0393 \u27e7 \u03a3\u2291\u03a5 As Bs)\n\nweakens\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5} (\u0393 : Typs \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) {As Bs} (\u211cs : \u03a5 \u220b As \u2194* Bs) {as bs} \u2192 \n \u0393\u27e6 \u0393 \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) as bs \u2192 \n \u0393\u27e6 weakens \u03a3\u2291\u03a5 \u0393 \u27e7\u00b2 \u211cs (weakens\u27e6 \u0393 \u27e7 \u03a3\u2291\u03a5 As as) (weakens\u27e6 \u0393 \u27e7 \u03a3\u2291\u03a5 Bs bs)\nweakens\u27e6 [] \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs tt\n = tt\nweakens\u27e6 T \u2237 \u0393 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs (a\u211cb , as\u211cbs) \n = ( struct-cast (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) \n (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 _) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 _) (cast\u00b2 (weaken\u27e6 T \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs) a\u211cb)\n , weakens\u27e6 \u0393 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs as\u211cbs)\n\n-- Variables\n\ndata Var {\u03a3 : Kinds} {\u03b1} (T : Typ \u03a3 (set \u03b1)) : Typs \u03a3 \u2192 Set where\n zero : \u2200 {\u0393} \u2192 Var T (T \u2237 \u0393)\n suc : \u2200 {\u03b2 \u0393} {U : Typ \u03a3 (set \u03b2)} \u2192 Var T \u0393 \u2192 Var T (U \u2237 \u0393)\n\nx\u27e6_\u27e7 : \u2200 {\u03a3} {\u0393 : Typs \u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} \u2192 \n Var T \u0393 \u2192 (As : \u03a3\u27e6 \u03a3 \u27e7) \u2192 (as : \u0393\u27e6 \u0393 \u27e7 As) \u2192 (T\u27e6 T \u27e7 As)\nx\u27e6 zero \u27e7 As (a , as) = a\nx\u27e6 suc x \u27e7 As (a , as) = x\u27e6 x \u27e7 As as\n\nx\u27e6_\u27e7\u00b2 : \u2200 {\u03a3} {\u0393 : Typs \u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} (x : Var T \u0393) \u2192 \n \u2200 {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) {as bs} \u2192 \n (\u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs as bs) \u2192 (T\u27e6 T \u27e7\u00b2 \u211cs (x\u27e6 x \u27e7 As as) (x\u27e6 x \u27e7 Bs bs))\nx\u27e6 zero \u27e7\u00b2 \u211cs (a\u211cb , as\u211cbs) = a\u211cb\nx\u27e6 suc x \u27e7\u00b2 \u211cs (a\u211cb , as\u211cbs) = x\u27e6 x \u27e7\u00b2 \u211cs as\u211cbs\n\n-- Constants\n\ndata Const {\u03a3 : Kinds} : \u2200 {\u03b1} \u2192 Typ \u03a3 (set \u03b1) \u2192 Set where\n pair : \u2200 {\u03b1 \u03b2} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 (set \u03b2) (tvar\u2081 \u22b8 (tvar\u2080 \u22b8 (tvar\u2081 \u2297 tvar\u2080)))))\n fst : \u2200 {\u03b1 \u03b2} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 (set \u03b2) ((tvar\u2081 \u2297 tvar\u2080) \u22b8 tvar\u2081)))\n snd : \u2200 {\u03b1 \u03b2} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 (set \u03b2) ((tvar\u2081 \u2297 tvar\u2080) \u22b8 tvar\u2080)))\n leq-refl : Const (\u03a0 time (tvar\u2080 \u227c tvar\u2080))\n leq-trans : Const (\u03a0 time (\u03a0 time (\u03a0 time ((tvar\u2082 \u227c tvar\u2081) \u22b8 ((tvar\u2081 \u227c tvar\u2080) \u22b8 (tvar\u2082 \u227c tvar\u2080))))))\n\nc\u27e6_\u27e7 : \u2200 {\u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} \u2192 \n Const T \u2192 (As : \u03a3\u27e6 \u03a3 \u27e7) \u2192 (T\u27e6 T \u27e7 As)\nc\u27e6 pair \u27e7 As = \u03bb A B a b \u2192 (a , b)\nc\u27e6 fst \u27e7 As = \u03bb A B \u2192 proj\u2081\nc\u27e6 snd \u27e7 As = \u03bb A B \u2192 proj\u2082\nc\u27e6 leq-refl \u27e7 As = \u2264-refl\nc\u27e6 leq-trans \u27e7 As = \u2264-trans\n\nc\u27e6_\u27e7\u00b2 : \u2200 {\u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} (c : Const T) \u2192 \n \u2200 {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 \n (T\u27e6 T \u27e7\u00b2 \u211cs (c\u27e6 c \u27e7 As) (c\u27e6 c \u27e7 Bs))\nc\u27e6 pair \u27e7\u00b2 \u211cs = \u03bb \u211c \u2111 a\u211cb c\u2111d \u2192 (a\u211cb , c\u2111d)\nc\u27e6 fst \u27e7\u00b2 \u211cs = \u03bb \u211c \u2111 \u2192 proj\u2081\nc\u27e6 snd \u27e7\u00b2 \u211cs = \u03bb \u211c \u2111 \u2192 proj\u2082\nc\u27e6 leq-refl \u27e7\u00b2 \u211cs = _\nc\u27e6 leq-trans \u27e7\u00b2 \u211cs = _\n\n-- Expressions\n\ndata Exp {\u03a3 : Kinds} (\u0393 : Typs \u03a3) : \u2200 {\u03b1} \u2192 Typ \u03a3 (set \u03b1) \u2192 Set where\n const : \u2200 {\u03b1} {T : Typ \u03a3 (set \u03b1)} \u2192 Const T \u2192 Exp \u0393 T\n abs : \u2200 {\u03b1 \u03b2} (T : Typ \u03a3 (set \u03b1)) {U : Typ \u03a3 (set \u03b2)} (M : Exp (T \u2237 \u0393) U) \u2192 Exp \u0393 (T \u22b8 U)\n app : \u2200 {\u03b1 \u03b2} {T : Typ \u03a3 (set \u03b1)} {U : Typ \u03a3 (set \u03b2)} (M : Exp \u0393 (T \u22b8 U)) (N : Exp \u0393 T) \u2192 Exp \u0393 U\n var : \u2200 {\u03b1} {T : Typ \u03a3 (set \u03b1)} \u2192 Var T \u0393 \u2192 Exp \u0393 T\n tabs : \u2200 K {\u03b1} {T : Typ (K \u2237 \u03a3) (set \u03b1)} (M : Exp (weakens (skip K id) \u0393) T) \u2192 Exp \u0393 (\u03a0 K T)\n\nctxt : \u2200 {\u03a3 \u0393 \u03b1 T} \u2192 Exp {\u03a3} \u0393 {\u03b1} T \u2192 Typs \u03a3\nctxt {\u03a3} {\u0393} M = \u0393\n\nM\u27e6_\u27e7 : \u2200 {\u03a3} {\u0393 : Typs \u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} \u2192 \n Exp \u0393 T \u2192 (As : \u03a3\u27e6 \u03a3 \u27e7) \u2192 (as : \u0393\u27e6 \u0393 \u27e7 As) \u2192 (T\u27e6 T \u27e7 As)\nM\u27e6 const c \u27e7 As as = c\u27e6 c \u27e7 As\nM\u27e6 abs T M \u27e7 As as = \u03bb a \u2192 M\u27e6 M \u27e7 As (a , as)\nM\u27e6 app M N \u27e7 As as = M\u27e6 M \u27e7 As as (M\u27e6 N \u27e7 As as)\nM\u27e6 var x \u27e7 As as = x\u27e6 x \u27e7 As as\nM\u27e6 tabs K M \u27e7 As as = \u03bb A \u2192 \n M\u27e6 M \u27e7 (A , As) (weakens\u27e6 ctxt (tabs K M) \u27e7 (skip K id) (A , As) as)\n\nM\u27e6_\u27e7\u00b2 : \u2200 {\u03a3} {\u0393 : Typs \u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} (M : Exp \u0393 T) \u2192 \n \u2200 {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) {as bs} \u2192 \n (\u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs as bs) \u2192 (T\u27e6 T \u27e7\u00b2 \u211cs (M\u27e6 M \u27e7 As as) (M\u27e6 M \u27e7 Bs bs))\nM\u27e6 const c \u27e7\u00b2 \u211cs as\u211cbs = c\u27e6 c \u27e7\u00b2 \u211cs\nM\u27e6 abs T M \u27e7\u00b2 \u211cs as\u211cbs = \u03bb a\u211cb \u2192 M\u27e6 M \u27e7\u00b2 \u211cs (a\u211cb , as\u211cbs)\nM\u27e6 app M N \u27e7\u00b2 \u211cs as\u211cbs = M\u27e6 M \u27e7\u00b2 \u211cs as\u211cbs (M\u27e6 N \u27e7\u00b2 \u211cs as\u211cbs)\nM\u27e6 var x \u27e7\u00b2 \u211cs as\u211cbs = x\u27e6 x \u27e7\u00b2 \u211cs as\u211cbs\nM\u27e6 tabs K M \u27e7\u00b2 \u211cs as\u211cbs = \u03bb \u211c \u2192 \n M\u27e6 M \u27e7\u00b2 (\u211c , \u211cs) (weakens\u27e6 ctxt (tabs K M) \u27e7\u00b2 (skip K id) (\u211c , \u211cs) as\u211cbs)\n\n-- Types with a chosen free world variable\n\n_\u2237\u02b3_ : Kinds \u2192 Kind \u2192 Kinds\n[] \u2237\u02b3 K = K \u2237 []\n(T \u2237 \u03a3) \u2237\u02b3 K = T \u2237 (\u03a3 \u2237\u02b3 K)\n\nTVar+ : Kind \u2192 Kinds \u2192 Set\nTVar+ K \u03a3 = TVar K (\u03a3 \u2237\u02b3 rset\u2080)\n\nTyp+ : Kinds \u2192 Kind \u2192 Set\nTyp+ \u03a3 = Typ (\u03a3 \u2237\u02b3 rset\u2080)\n\nwvar : \u2200 \u03a3 \u2192 TVar+ rset\u2080 \u03a3\nwvar [] = zero\nwvar (K \u2237 \u03a3) = suc (wvar \u03a3)\n\nworld : \u2200 {\u03a3} \u2192 Typ+ \u03a3 rset\u2080\nworld {\u03a3} = var (wvar \u03a3)\n\nWorld : Time \u2192 Set\nWorld t = \u22a4\n\n-- Surface types\n\ndata STyp : Kind \u2192 Set where\n \u27e8_\u27e9 : \u2200 {\u03b1} \u2192 STyp (set \u03b1) \u2192 STyp (rset \u03b1)\n [_] : \u2200 {\u03b1} \u2192 STyp (rset \u03b1) \u2192 STyp (set \u03b1)\n _\u22a0_ _\u21a6_ : \u2200 {\u03b1 \u03b2} \u2192 STyp (set \u03b1) \u2192 STyp (set \u03b2) \u2192 STyp (set (\u03b1 \u2294 \u03b2))\n _\u2227_ _\u21d2_ : \u2200 {\u03b1 \u03b2} \u2192 STyp (rset \u03b1) \u2192 STyp (rset \u03b2) \u2192 STyp (rset (\u03b1 \u2294 \u03b2))\n \u25a1 : \u2200 {\u03b1} \u2192 STyp (rset \u03b1) \u2192 STyp (rset \u03b1)\n\n\u27ea_\u27eb : \u2200 {K} \u2192 STyp K \u2192 Typ+ [] K\n\u27ea \u27e8 T \u27e9 \u27eb = app always \u27ea T \u27eb\n\u27ea [ T ] \u27eb = app taut \u27ea T \u27eb\n\u27ea T \u22a0 U \u27eb = \u27ea T \u27eb \u2297 \u27ea U \u27eb\n\u27ea T \u21a6 U \u27eb = \u27ea T \u27eb \u22b8 \u27ea U \u27eb\n\u27ea T \u2227 U \u27eb = \u27ea T \u27eb \u2297\u02b3 \u27ea U \u27eb\n\u27ea T \u21d2 U \u27eb = \u27ea T \u27eb \u22b8\u02b3 \u27ea U \u27eb\n\u27ea \u25a1 T \u27eb = tvar\u2080 \u22b5 \u27ea T \u27eb\n\nT\u27ea_\u27eb : \u2200 {K} \u2192 STyp K \u2192 K\u27e6 K \u27e7\nT\u27ea T \u27eb = T\u27e6 \u27ea T \u27eb \u27e7 (World , tt)\n\n-- Signals of T are iso to \u25a1 T\n\nSignal : \u2200 {\u03b1} \u2192 RSet \u03b1 \u2192 RSet \u03b1\nSignal A s = \u2200 t \u2192 True (s \u2264 t) \u2192 A t\n\nsignal : \u2200 {\u03b1} (T : STyp (rset \u03b1)) s \u2192 \n T\u27ea \u25a1 T \u27eb s \u2192 Signal T\u27ea T \u27eb s\nsignal T s \u03c3 t s\u2264t = \u03c3 t s\u2264t _\n\nsignal\u207b\u00b9 : \u2200 {\u03b1} (T : STyp (rset \u03b1)) s \u2192 \n Signal T\u27ea T \u27eb s \u2192 T\u27ea \u25a1 T \u27eb s\nsignal\u207b\u00b9 T s \u03c3 t s\u2264t _ = \u03c3 t s\u2264t\n\nsignal-iso : \u2200 {\u03b1} (T : STyp (rset \u03b1)) s \u03c3 \u2192 \n (signal T s (signal\u207b\u00b9 T s \u03c3) \u2261 \u03c3)\nsignal-iso T s \u03c3 = refl\n\nsignal-iso\u207b\u00b9 : \u2200 {\u03b1} (T : STyp (rset \u03b1)) s \u03c3 \u2192\n (signal\u207b\u00b9 T s (signal T s \u03c3) \u2261 \u03c3)\nsignal-iso\u207b\u00b9 T s \u03c3 = refl\n\n-- Signal functions from T to U are iso to \u25a1 T \u21d2 \u25a1 U\n\nSF : \u2200 {\u03b1 \u03b2} \u2192 RSet \u03b1 \u2192 RSet \u03b2 \u2192 RSet (\u03b1 \u2294 \u03b2)\nSF A B s = Signal A s \u2192 Signal B s\n\nsf : \u2200 {\u03b1 \u03b2} (T : STyp (rset \u03b1)) (U : STyp (rset \u03b2)) s \u2192\n T\u27ea \u25a1 T \u21d2 \u25a1 U \u27eb s \u2192 SF T\u27ea T \u27eb T\u27ea U \u27eb s\nsf T U s f \u03c3 = signal U s (f (signal\u207b\u00b9 T s \u03c3))\n\nsf\u207b\u00b9 : \u2200 {\u03b1 \u03b2} (T : STyp (rset \u03b1)) (U : STyp (rset \u03b2)) s \u2192\n SF T\u27ea T \u27eb T\u27ea U \u27eb s \u2192 T\u27ea \u25a1 T \u21d2 \u25a1 U \u27eb s\nsf\u207b\u00b9 T U s f \u03c3 = signal\u207b\u00b9 U s (f (signal T s \u03c3))\n\nsf-iso : \u2200 {\u03b1 \u03b2} (T : STyp (rset \u03b1)) (U : STyp (rset \u03b2)) s f \u2192 \n (sf T U s (sf\u207b\u00b9 T U s f) \u2261 f)\nsf-iso T U s f = refl\n\nsf-iso\u207b\u00b9 : \u2200 {\u03b1 \u03b2} (T : STyp (rset \u03b1)) (U : STyp (rset \u03b2)) s f \u2192 \n (sf\u207b\u00b9 T U s (sf T U s f) \u2261 f)\nsf-iso\u207b\u00b9 T U s f = refl\n\n-- Causality\n\nmutual\n\n _at_\u22a8_\u2248[_]_ : \u2200 {\u03b1} (T : STyp (rset \u03b1)) s \u2192 T\u27ea T \u27eb s \u2192 Time \u2192 T\u27ea T \u27eb s \u2192 Set \u03b1\n \u27e8 T \u27e9 at s \u22a8 a \u2248[ u ] b = T \u22a8 a \u2248[ u ] b\n (T \u2227 U) at s \u22a8 (a , b) \u2248[ u ] (c , d) = (T at s \u22a8 a \u2248[ u ] c) \u00d7 (U at s \u22a8 b \u2248[ u ] d)\n (T \u21d2 U) at s \u22a8 f \u2248[ u ] g = \u2200 a b \u2192 (T at s \u22a8 a \u2248[ u ] b) \u2192 (U at s \u22a8 f a \u2248[ u ] g b)\n \u25a1 T at s \u22a8 \u03c3 \u2248[ u ] \u03c4 = \u2200 t s\u2264t \u2192 True (t \u2264 u) \u2192 (T at t \u22a8 \u03c3 t s\u2264t _ \u2248[ u ] \u03c4 t s\u2264t _)\n\n _\u22a8_\u2248[_]_ : \u2200 {\u03b1} \u2192 (T : STyp (set \u03b1)) \u2192 T\u27ea T \u27eb \u2192 Time \u2192 T\u27ea T \u27eb \u2192 Set \u03b1\n [ T ] \u22a8 \u03c3 \u2248[ u ] \u03c4 = \u2200 s \u2192 (T at s \u22a8 \u03c3 s \u2248[ u ] \u03c4 s)\n (T \u22a0 U) \u22a8 (a , b) \u2248[ u ] (c , d) = (T \u22a8 a \u2248[ u ] c) \u00d7 (U \u22a8 b \u2248[ u ] d)\n (T \u21a6 U) \u22a8 f \u2248[ u ] g = \u2200 a b \u2192 (T \u22a8 a \u2248[ u ] b) \u2192 (U \u22a8 f a \u2248[ u ] g b)\n\nCausal : \u2200 {\u03b1 \u03b2} (T : STyp (rset \u03b1)) (U : STyp (rset \u03b2)) s \u2192 T\u27ea T \u21d2 U \u27eb s \u2192 Set (\u03b1 \u2294 \u03b2)\nCausal T U s f = \u2200 t \u03c3 \u03c4 \u2192 \n (T at s \u22a8 \u03c3 \u2248[ t ] \u03c4) \u2192 (U at s \u22a8 f \u03c3 \u2248[ t ] f \u03c4)\n\n-- Parametricity implies causality\n\n\u211c[_] : Time \u2192 (rset o \u220b World \u2194 World)\n\u211c[ u ] {t} s\u2261t tt tt = True (t \u2264 u)\n\nmutual\n\n \u211c-impl-\u2248_at : \u2200 {\u03b1} (T : STyp (rset \u03b1)) (s u : Time) (a b : T\u27ea T \u27eb s) \u2192\n (T\u27e6 \u27ea T \u27eb \u27e7\u00b2 (\u211c[ u ] , tt) refl a b) \u2192 (T at s \u22a8 a \u2248[ u ] b)\n \u211c-impl-\u2248 \u27e8 T \u27e9 at s u a b a\u211cb\n = \u211c-impl-\u2248 T u a b a\u211cb\n \u211c-impl-\u2248 (T \u2227 U) at s u (a , b) (c , d) (a\u211cc , b\u211cd) \n = (\u211c-impl-\u2248 T at s u a c a\u211cc , \u211c-impl-\u2248 U at s u b d b\u211cd)\n \u211c-impl-\u2248 (T \u21d2 U) at s u f g f\u211cg\n = \u03bb a b a\u2248b \u2192 \u211c-impl-\u2248 U at s u (f a) (g b) (f\u211cg (\u2248-impl-\u211c T at s u a b a\u2248b))\n \u211c-impl-\u2248 (\u25a1 T) at s u \u03c3 \u03c4 \u03c3\u211c\u03c4\n = \u03bb t s\u2264t t\u2264u \u2192 \u211c-impl-\u2248 T at t u (\u03c3 t s\u2264t _) (\u03c4 t s\u2264t _) \n (\u03c3\u211c\u03c4 refl tt (\u03bb {r} _ _ {r\u2264t} _ \u2192 \u2264-trans r t u r\u2264t t\u2264u))\n\n \u2248-impl-\u211c_at : \u2200 {\u03b1} (T : STyp (rset \u03b1)) (s u : Time) (a b : T\u27ea T \u27eb s) \u2192\n (T at s \u22a8 a \u2248[ u ] b) \u2192 (T\u27e6 \u27ea T \u27eb \u27e7\u00b2 (\u211c[ u ] , tt) refl a b)\n \u2248-impl-\u211c \u27e8 T \u27e9 at s u a b a\u2248b\n = \u2248-impl-\u211c T u a b a\u2248b\n \u2248-impl-\u211c (T \u2227 U) at s u (a , b) (c , d) (a\u2248c , b\u2248d)\n = (\u2248-impl-\u211c T at s u a c a\u2248c , \u2248-impl-\u211c U at s u b d b\u2248d)\n \u2248-impl-\u211c (T \u21d2 U) at s u f g f\u2248g\n = \u03bb {a} {b} a\u211cb \u2192 \u2248-impl-\u211c U at s u (f a) (g b) (f\u2248g a b (\u211c-impl-\u2248 T at s u a b a\u211cb))\n \u2248-impl-\u211c (\u25a1 T) at s u \u03c3 \u03c4 \u03c3\u2248\u03c4 = lemma where\n lemma : T\u27e6 \u27ea \u25a1 T \u27eb \u27e7\u00b2 (\u211c[ u ] , tt) {s} refl \u03c3 \u03c4\n lemma {t} refl {s\u2264t} {s\u2264t\u2032} tt t\u2264u with irrel (s \u2264 t) s\u2264t s\u2264t\u2032\n lemma {t} refl {s\u2264t} tt t\u2264u | refl = \n \u2248-impl-\u211c T at t u (\u03c3 t s\u2264t _) (\u03c4 t s\u2264t _) \n (\u03c3\u2248\u03c4 t s\u2264t (t\u2264u refl {s\u2264t} {s\u2264t} tt {\u2264-refl t} {\u2264-refl t} tt))\n\n \u211c-impl-\u2248 : \u2200 {\u03b1} (T : STyp (set \u03b1)) (u : Time) (a b : T\u27ea T \u27eb) \u2192\n (T\u27e6 \u27ea T \u27eb \u27e7\u00b2 (\u211c[ u ] , tt) a b) \u2192 (T \u22a8 a \u2248[ u ] b)\n \u211c-impl-\u2248 (T \u22a0 U) u (a , b) (c , d) (a\u211cc , b\u211cd)\n = (\u211c-impl-\u2248 T u a c a\u211cc , \u211c-impl-\u2248 U u b d b\u211cd)\n \u211c-impl-\u2248 (T \u21a6 U) u f g f\u211cg\n = \u03bb a b a\u2248b \u2192 \u211c-impl-\u2248 U u (f a) (g b) (f\u211cg (\u2248-impl-\u211c T u a b a\u2248b))\n \u211c-impl-\u2248 [ T ] u \u03c3 \u03c4 \u03c3\u211c\u03c4\n = \u03bb s \u2192 \u211c-impl-\u2248 T at s u (\u03c3 s) (\u03c4 s) (\u03c3\u211c\u03c4 refl)\n\n \u2248-impl-\u211c : \u2200 {\u03b1} (T : STyp (set \u03b1)) (u : Time) (a b : T\u27ea T \u27eb) \u2192\n (T \u22a8 a \u2248[ u ] b) \u2192 (T\u27e6 \u27ea T \u27eb \u27e7\u00b2 (\u211c[ u ] , tt) a b)\n \u2248-impl-\u211c (T \u22a0 U) u (a , b) (c , d) (a\u2248c , b\u2248d)\n = (\u2248-impl-\u211c T u a c a\u2248c , \u2248-impl-\u211c U u b d b\u2248d)\n \u2248-impl-\u211c (T \u21a6 U) u f g f\u2248g\n = \u03bb {a} {b} a\u211cb \u2192 \u2248-impl-\u211c U u (f a) (g b) (f\u2248g a b (\u211c-impl-\u2248 T u a b a\u211cb))\n \u2248-impl-\u211c [ T ] u \u03c3 \u03c4 \u03c3\u2248\u03c4 = lemma where\n lemma : T\u27e6 \u27ea [ T ] \u27eb \u27e7\u00b2 (\u211c[ u ] , tt) \u03c3 \u03c4\n lemma {s} refl = \u2248-impl-\u211c T at s u (\u03c3 s) (\u03c4 s) (\u03c3\u2248\u03c4 s)\n\n-- Every F-omega function is causal\n\ncausality : \u2200 {\u03b1 \u03b2} (T : STyp (rset \u03b1)) (U : STyp (rset \u03b2)) (M : Exp [] \u27ea [ T \u21d2 U ] \u27eb) s \u2192 \n Causal T U s (M\u27e6 M \u27e7 (World , tt) tt s)\ncausality T U M s t \n = \u211c-impl-\u2248 T \u21d2 U at s t \n (M\u27e6 M \u27e7 (World , tt) tt s) \n (M\u27e6 M \u27e7 (World , tt) tt s) \n (M\u27e6 M \u27e7\u00b2 (\u211c[ t ] , _) tt refl)\n\n","old_contents":"open import FRP.JS.Level using ( Level ; _\u2294_ ) renaming ( zero to o ; suc to \u2191 )\nopen import FRP.JS.Time using ( Time ; _\u225f_ ; _\u2264_ ; _<_ )\nopen import FRP.JS.True using ( True )\nopen import FRP.JS.Nat using ( \u2115 ; zero ; suc )\n\nmodule FRP.JS.Model where\n\n-- Preliminaries\n\ninfixr 4 _+_\n\nrecord \u22a4 {\u03b1} : Set \u03b1 where\n constructor tt\n\nopen \u22a4 public\n\nrecord \u03a3 {\u03b1 \u03b2} (A : Set \u03b1) (B : A \u2192 Set \u03b2) : Set (\u03b1 \u2294 \u03b2) where\n constructor _,_\n field\n proj\u2081 : A\n proj\u2082 : B proj\u2081\n\nopen \u03a3 public\n\n_\u00d7_ : \u2200 {\u03b1 \u03b2} \u2192 Set \u03b1 \u2192 Set \u03b2 \u2192 Set (\u03b1 \u2294 \u03b2)\nA \u00d7 B = \u03a3 A (\u03bb a \u2192 B)\n\ndata _\u2261_ {\u03b1} {A : Set \u03b1} (a : A) : A \u2192 Set \u03b1 where\n refl : a \u2261 a\n\npostulate\n \u2264-refl : \u2200 t \u2192 True (t \u2264 t)\n \u2264-trans : \u2200 t u v \u2192 True (t \u2264 u) \u2192 True (u \u2264 v) \u2192 True (t \u2264 v)\n\n-- Relations on Set\n\n_\u220b_\u2194_ : \u2200 \u03b1 \u2192 Set \u03b1 \u2192 Set \u03b1 \u2192 Set (\u2191 \u03b1)\n\u03b1 \u220b A \u2194 B = A \u2192 B \u2192 Set \u03b1\n\n-- RSets and relations on RSet\n\nRSet : \u2200 \u03b1 \u2192 Set (\u2191 \u03b1)\nRSet \u03b1 = Time \u2192 Set \u03b1\n\nRSet\u2080 = RSet o\nRSet\u2081 = RSet (\u2191 o)\n\n_\u220b_\u21d4_ : \u2200 \u03b1 \u2192 RSet \u03b1 \u2192 RSet \u03b1 \u2192 Set (\u2191 \u03b1)\n\u03b1 \u220b A \u21d4 B = \u2200 t \u2192 (\u03b1 \u220b A t \u2194 B t)\n\n--- Level sequences\n\ndata Levels : Set where\n \u03b5 : Levels\n _,_ : \u2200 (\u03b1s : Levels) (\u03b1 : Level) \u2192 Levels\n\nmax : Levels \u2192 Level\nmax \u03b5 = o\nmax (\u03b1s , \u03b1) = max \u03b1s \u2294 \u03b1\n\n-- Sequences of Sets\n\nSets : \u2200 \u03b1s \u2192 Set (\u2191 (max \u03b1s))\nSets \u03b5 = \u22a4\nSets (\u03b1s , \u03b1) = Sets \u03b1s \u00d7 Set \u03b1\n\n\u27e8_\u220b_\u27e9 : \u2200 \u03b1s \u2192 Sets \u03b1s \u2192 Set (max \u03b1s)\n\u27e8 \u03b5 \u220b tt \u27e9 = \u22a4\n\u27e8 (\u03b1s , \u03b1) \u220b (As , A) \u27e9 = \u27e8 \u03b1s \u220b As \u27e9 \u00d7 A\n\n_\u220b_\u2194*_ : \u2200 \u03b1s \u2192 Sets \u03b1s \u2192 Sets \u03b1s \u2192 Set (\u2191 (max \u03b1s))\n\u03b5 \u220b tt \u2194* tt = \u22a4\n(\u03b1s , \u03b1) \u220b (As , A) \u2194* (Bs , B) = (\u03b1s \u220b As \u2194* Bs) \u00d7 (\u03b1 \u220b A \u2194 B)\n\n\u27e8_\u220b_\u27e9\u00b2 : \u2200 \u03b1s {As Bs} \u2192 (\u03b1s \u220b As \u2194* Bs) \u2192 (max \u03b1s \u220b \u27e8 \u03b1s \u220b As \u27e9 \u2194 \u27e8 \u03b1s \u220b Bs \u27e9)\n\u27e8 \u03b5 \u220b tt \u27e9\u00b2 tt tt = \u22a4\n\u27e8 (\u03b1s , \u03b1) \u220b (\u211cs , \u211c) \u27e9\u00b2 (as , a) (bs , b) = (\u27e8 \u03b1s \u220b \u211cs \u27e9\u00b2 as bs) \u00d7 (\u211c a b)\n\n-- Sequences of RSets\n\nRSets : \u2200 \u03b1s \u2192 Set (\u2191 (max \u03b1s))\nRSets \u03b5 = \u22a4\nRSets (\u03b1s , \u03b1) = RSets \u03b1s \u00d7 RSet \u03b1\n\n_\u220b_\u21d4*_ : \u2200 \u03b1s \u2192 RSets \u03b1s \u2192 RSets \u03b1s \u2192 Set (\u2191 (max \u03b1s))\n\u03b5 \u220b tt \u21d4* tt = \u22a4\n(\u03b1s , \u03b1) \u220b (As , A) \u21d4* (Bs , B) = (\u03b1s \u220b As \u21d4* Bs) \u00d7 (\u03b1 \u220b A \u21d4 B)\n\n-- Concatenation of sequences\n\n_+_ : Levels \u2192 Levels \u2192 Levels\n\u03b1s + \u03b5 = \u03b1s\n\u03b1s + (\u03b2s , \u03b2) = (\u03b1s + \u03b2s) , \u03b2\n\n_\u220b_++_\u220b_ : \u2200 \u03b1s \u2192 RSets \u03b1s \u2192 \u2200 \u03b2s \u2192 RSets \u03b2s \u2192 RSets (\u03b1s + \u03b2s)\n\u03b1s \u220b As ++ \u03b5 \u220b tt = As\n\u03b1s \u220b As ++ (\u03b2s , \u03b2) \u220b (Bs , B) = ((\u03b1s \u220b As ++ \u03b2s \u220b Bs) , B)\n\n_\u220b_++\u00b2_\u220b_ : \u2200 \u03b1s {As Bs} \u2192 (\u03b1s \u220b As \u21d4* Bs) \u2192 \u2200 \u03b2s {Cs Ds} \u2192 (\u03b2s \u220b Cs \u21d4* Ds) \u2192 \n ((\u03b1s + \u03b2s) \u220b (\u03b1s \u220b As ++ \u03b2s \u220b Cs) \u21d4* (\u03b1s \u220b Bs ++ \u03b2s \u220b Ds))\n\u03b1s \u220b \u211cs ++\u00b2 \u03b5 \u220b tt = \u211cs\n\u03b1s \u220b \u211cs ++\u00b2 (\u03b2s , \u03b2) \u220b (\u2111s , \u2111) = ((\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) , \u2111)\n\n-- Intervals\n\n_[_,_] : \u2200 {\u03b1} \u2192 RSet \u03b1 \u2192 Time \u2192 Time \u2192 Set \u03b1\nA [ s , u ] = \u2200 t \u2192 True (s \u2264 t) \u2192 True (t \u2264 u) \u2192 A t\n\n_[_,_]\u00b2 : \u2200 {\u03b1 A B} \u2192 (\u03b1 \u220b A \u21d4 B) \u2192 \u2200 s u \u2192 (\u03b1 \u220b A [ s , u ] \u2194 B [ s , u ])\n(\u211c [ s , u ]\u00b2) \u03c3 \u03c4 = \u2200 t s\u2264t t\u2264u \u2192 \u211c t (\u03c3 t s\u2264t t\u2264u) (\u03c4 t s\u2264t t\u2264u)\n\n-- Type variables\n\ndata TVar : Levels \u2192 Set\u2081 where\n zero : \u2200 {\u03b1s \u03b1} \u2192 TVar (\u03b1s , \u03b1)\n suc : \u2200 {\u03b1s \u03b1} \u2192 (\u03c4 : TVar \u03b1s) \u2192 TVar (\u03b1s , \u03b1)\n\n\u03c4level : \u2200 {\u03b1s} \u2192 TVar \u03b1s \u2192 Level\n\u03c4level (zero {\u03b1 = \u03b1}) = \u03b1\n\u03c4level (suc \u03c4) = \u03c4level \u03c4\n\n\u03c4\u27e6_\u27e7 : \u2200 {\u03b1s} (\u03c4 : TVar \u03b1s) \u2192 RSets \u03b1s \u2192 RSet (\u03c4level \u03c4)\n\u03c4\u27e6 zero \u27e7 (As , A) = A\n\u03c4\u27e6 suc \u03c4 \u27e7 (As , A) = \u03c4\u27e6 \u03c4 \u27e7 As\n\n\u03c4\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} (\u03c4 : TVar \u03b1s) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 (\u03c4level \u03c4 \u220b \u03c4\u27e6 \u03c4 \u27e7 As \u21d4 \u03c4\u27e6 \u03c4 \u27e7 Bs)\n\u03c4\u27e6 zero \u27e7\u00b2 (\u211cs , \u211c) t a b = \u211c t a b\n\u03c4\u27e6 suc \u03c4 \u27e7\u00b2 (\u211cs , \u211c) t a b = \u03c4\u27e6 \u03c4 \u27e7\u00b2 \u211cs t a b\n\n-- Types\n\ndata Typ (\u03b1s : Levels) : Set\u2081 where\n \u27e8_\u27e9 : (A : Set) \u2192 Typ \u03b1s\n _\u2227_ _\u21d2_ _\u22b5_ : (T : Typ \u03b1s) \u2192 (U : Typ \u03b1s) \u2192 Typ \u03b1s\n tvar : (\u03c4 : TVar \u03b1s) \u2192 Typ \u03b1s\n univ : \u2200 \u03b1 \u2192 (T : Typ (\u03b1s , \u03b1)) \u2192 Typ \u03b1s\n\ntlevel : \u2200 {\u03b1s} \u2192 Typ \u03b1s \u2192 Level\ntlevel \u27e8 A \u27e9 = o\ntlevel (T \u2227 U) = tlevel T \u2294 tlevel U\ntlevel (T \u21d2 U) = tlevel T \u2294 tlevel U\ntlevel (T \u22b5 U) = tlevel T \u2294 tlevel U\ntlevel (tvar \u03c4) = \u03c4level \u03c4\ntlevel (univ \u03b1 T) = \u2191 \u03b1 \u2294 tlevel T\n\nT\u27e6_\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u2192 RSets \u03b1s \u2192 RSet (tlevel T)\nT\u27e6 \u27e8 A \u27e9 \u27e7 As t = A\nT\u27e6 T \u2227 U \u27e7 As t = T\u27e6 T \u27e7 As t \u00d7 T\u27e6 U \u27e7 As t\nT\u27e6 T \u21d2 U \u27e7 As t = T\u27e6 T \u27e7 As t \u2192 T\u27e6 U \u27e7 As t\nT\u27e6 T \u22b5 U \u27e7 As t = \u2200 u \u2192 True (t \u2264 u) \u2192 T\u27e6 T \u27e7 As [ t , u ] \u2192 T\u27e6 U \u27e7 As u\nT\u27e6 tvar \u03c4 \u27e7 As t = \u03c4\u27e6 \u03c4 \u27e7 As t\nT\u27e6 univ \u03b1 T \u27e7 As t = \u2200 (A : RSet \u03b1) \u2192 T\u27e6 T \u27e7 (As , A) t\n\nT\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} (T : Typ \u03b1s) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 (tlevel T \u220b T\u27e6 T \u27e7 As \u21d4 T\u27e6 T \u27e7 Bs)\nT\u27e6 \u27e8 A \u27e9 \u27e7\u00b2 \u211cs t a b = a \u2261 b\nT\u27e6 T \u2227 U \u27e7\u00b2 \u211cs t (a , b) (c , d) = T\u27e6 T \u27e7\u00b2 \u211cs t a c \u00d7 T\u27e6 U \u27e7\u00b2 \u211cs t b d\nT\u27e6 T \u21d2 U \u27e7\u00b2 \u211cs t f g = \u2200 {a b} \u2192 T\u27e6 T \u27e7\u00b2 \u211cs t a b \u2192 T\u27e6 U \u27e7\u00b2 \u211cs t (f a) (g b)\nT\u27e6 T \u22b5 U \u27e7\u00b2 \u211cs t f g = \u2200 u t\u2264u {\u03c3 \u03c4} \u2192 (T\u27e6 T \u27e7\u00b2 \u211cs [ t , u ]\u00b2) \u03c3 \u03c4 \u2192 T\u27e6 U \u27e7\u00b2 \u211cs u (f u t\u2264u \u03c3) (g u t\u2264u \u03c4)\nT\u27e6 tvar \u03c4 \u27e7\u00b2 \u211cs t v w = \u03c4\u27e6 \u03c4 \u27e7\u00b2 \u211cs t v w\nT\u27e6 univ \u03b1 T \u27e7\u00b2 \u211cs t f g = \u2200 {A B} (\u211c : \u03b1 \u220b A \u21d4 B) \u2192 T\u27e6 T \u27e7\u00b2 (\u211cs , \u211c) t (f A) (g B)\n\n-- Contexts\n\ndata Ctxt (\u03b1s : Levels) : Set\u2081 where\n \u03b5 : Ctxt \u03b1s\n _,_at_ : (\u0393 : Ctxt \u03b1s) (T : Typ \u03b1s) (t : Time) \u2192 Ctxt \u03b1s\n\nclevels : \u2200 {\u03b1s} \u2192 Ctxt \u03b1s \u2192 Levels\nclevels \u03b5 = \u03b5\nclevels (\u0393 , T at t) = (clevels \u0393 , tlevel T)\n\n\u0393\u27e6_\u27e7 : \u2200 {\u03b1s} (\u0393 : Ctxt \u03b1s) \u2192 RSets \u03b1s \u2192 Sets (clevels \u0393)\n\u0393\u27e6 \u03b5 \u27e7 As = tt\n\u0393\u27e6 \u0393 , T at t \u27e7 As = (\u0393\u27e6 \u0393 \u27e7 As , T\u27e6 T \u27e7 As t)\n\n\u0393\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} (\u0393 : Ctxt \u03b1s) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 (clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7 As \u2194* \u0393\u27e6 \u0393 \u27e7 Bs)\n\u0393\u27e6 \u03b5 \u27e7\u00b2 \u211cs = tt\n\u0393\u27e6 \u0393 , T at t \u27e7\u00b2 \u211cs = (\u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs , T\u27e6 T \u27e7\u00b2 \u211cs t)\n\n-- Weakening of type variables\n\n\u03c4weaken : \u2200 {\u03b1s} \u03b1 \u03b2s \u2192 TVar (\u03b1s + \u03b2s) \u2192 TVar ((\u03b1s , \u03b1) + \u03b2s)\n\u03c4weaken \u03b1 \u03b5 \u03c4 = suc \u03c4\n\u03c4weaken \u03b1 (\u03b2s , \u03b2) zero = zero\n\u03c4weaken \u03b1 (\u03b2s , \u03b2) (suc \u03c4) = suc (\u03c4weaken \u03b1 \u03b2s \u03c4)\n\n\u27e6\u03c4weaken\u27e7 : \u2200 {\u03b1s} \u03b1 \u03b2s (\u03c4 : TVar (\u03b1s + \u03b2s)) As A Bs t \u2192 \n \u03c4\u27e6 \u03c4 \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t \u2192 \u03c4\u27e6 \u03c4weaken \u03b1 \u03b2s \u03c4 \u27e7 ((\u03b1s , \u03b1) \u220b (As , A) ++ \u03b2s \u220b Bs) t\n\u27e6\u03c4weaken\u27e7 \u03b1 \u03b5 \u03c4 As A Bs t a = a\n\u27e6\u03c4weaken\u27e7 \u03b1 (\u03b2s , \u03b2) zero As A Bs t a = a\n\u27e6\u03c4weaken\u27e7 \u03b1 (\u03b2s , \u03b2) (suc \u03c4) As A (Bs , B) t a = \u27e6\u03c4weaken\u27e7 \u03b1 \u03b2s \u03c4 As A Bs t a\n\n\u27e6\u03c4weaken\u207b\u00b9\u27e7 : \u2200 {\u03b1s} \u03b1 \u03b2s (\u03c4 : TVar (\u03b1s + \u03b2s)) As A Bs t \u2192 \n \u03c4\u27e6 \u03c4weaken \u03b1 \u03b2s \u03c4 \u27e7 ((\u03b1s , \u03b1) \u220b (As , A) ++ \u03b2s \u220b Bs) t \u2192 \u03c4\u27e6 \u03c4 \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t\n\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b5 \u03c4 As A Bs t a = a\n\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 (\u03b2s , \u03b2) zero As A Bs t a = a\n\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 (\u03b2s , \u03b2) (suc \u03c4) As A (Bs , B) t a = \u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u03c4 As A Bs t a\n\n\u27e6\u03c4weaken\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 \u03b2s (\u03c4 : TVar (\u03b1s + \u03b2s)) {As Bs A B Cs Ds} \u211cs \u211c \u2111s t {a b} \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192 \n \u03c4\u27e6 \u03c4weaken \u03b1 \u03b2s \u03c4 \u27e7\u00b2 ((\u03b1s , \u03b1) \u220b (\u211cs , \u211c) ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6\u03c4weaken\u27e7 \u03b1 \u03b2s \u03c4 As A Cs t a) (\u27e6\u03c4weaken\u27e7 \u03b1 \u03b2s \u03c4 Bs B Ds t b)\n\u27e6\u03c4weaken\u27e7\u00b2 \u03b1 \u03b5 \u03c4 \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4weaken\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) zero \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4weaken\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) (suc \u03c4) \u211cs \u211c (\u2111s , \u2111) t a\u211cb = \u27e6\u03c4weaken\u27e7\u00b2 \u03b1 \u03b2s \u03c4 \u211cs \u211c \u2111s t a\u211cb\n\n\u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 \u03b2s (\u03c4 : TVar (\u03b1s + \u03b2s)) {As Bs A B Cs Ds} \u211cs \u211c \u2111s t {a b} \u2192\n \u03c4\u27e6 \u03c4weaken \u03b1 \u03b2s \u03c4 \u27e7\u00b2 ((\u03b1s , \u03b1) \u220b (\u211cs , \u211c) ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u03c4 As A Cs t a) (\u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u03c4 Bs B Ds t b)\n\u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b5 \u03c4 \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) zero \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) (suc \u03c4) \u211cs \u211c (\u2111s , \u2111) t a\u211cb = \u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s \u03c4 \u211cs \u211c \u2111s t a\u211cb\n\n-- Weakening of types\n\ntweaken : \u2200 {\u03b1s} \u03b1 \u03b2s \u2192 Typ (\u03b1s + \u03b2s) \u2192 Typ ((\u03b1s , \u03b1) + \u03b2s)\ntweaken \u03b1 \u03b2s \u27e8 A \u27e9 = \u27e8 A \u27e9\ntweaken \u03b1 \u03b2s (T \u2227 U) = tweaken \u03b1 \u03b2s T \u2227 tweaken \u03b1 \u03b2s U\ntweaken \u03b1 \u03b2s (T \u21d2 U) = tweaken \u03b1 \u03b2s T \u21d2 tweaken \u03b1 \u03b2s U\ntweaken \u03b1 \u03b2s (T \u22b5 U) = tweaken \u03b1 \u03b2s T \u22b5 tweaken \u03b1 \u03b2s U\ntweaken \u03b1 \u03b2s (tvar \u03c4) = tvar (\u03c4weaken \u03b1 \u03b2s \u03c4)\ntweaken \u03b1 \u03b2s (univ \u03b2 T) = univ \u03b2 (tweaken \u03b1 (\u03b2s , \u03b2) T)\n\nmutual\n\n \u27e6tweaken\u27e7 : \u2200 {\u03b1s} \u03b1 \u03b2s (T : Typ (\u03b1s + \u03b2s)) As A Bs t \u2192 \n T\u27e6 T \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t \u2192 \n T\u27e6 tweaken \u03b1 \u03b2s T \u27e7 ((\u03b1s , \u03b1) \u220b (As , A) ++ \u03b2s \u220b Bs) t\n \u27e6tweaken\u27e7 \u03b1 \u03b2s \u27e8 B \u27e9 As A Bs t a = a\n \u27e6tweaken\u27e7 \u03b1 \u03b2s (T \u2227 U) As A Bs t (a , b) = (\u27e6tweaken\u27e7 \u03b1 \u03b2s T As A Bs t a , \u27e6tweaken\u27e7 \u03b1 \u03b2s U As A Bs t b)\n \u27e6tweaken\u27e7 \u03b1 \u03b2s (T \u21d2 U) As A Bs t f = \u03bb a \u2192 \u27e6tweaken\u27e7 \u03b1 \u03b2s U As A Bs t (f (\u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s T As A Bs t a))\n \u27e6tweaken\u27e7 \u03b1 \u03b2s (T \u22b5 U) As A Bs t f = \u03bb v t\u2264v \u03c3 \u2192 \u27e6tweaken\u27e7 \u03b1 \u03b2s U As A Bs v (f v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s T As A Bs u (\u03c3 u t\u2264u u\u2264v)))\n \u27e6tweaken\u27e7 \u03b1 \u03b2s (tvar \u03c4) As A Bs t a = \u27e6\u03c4weaken\u27e7 \u03b1 \u03b2s \u03c4 As A Bs t a\n \u27e6tweaken\u27e7 \u03b1 \u03b2s (univ \u03b2 T) As A Bs t f = \u03bb B \u2192 \u27e6tweaken\u27e7 \u03b1 (\u03b2s , \u03b2) T As A (Bs , B) t (f B)\n\n \u27e6tweaken\u207b\u00b9\u27e7 : \u2200 {\u03b1s} \u03b1 \u03b2s (T : Typ (\u03b1s + \u03b2s)) As A Bs t \u2192 \n T\u27e6 tweaken \u03b1 \u03b2s T \u27e7 ((\u03b1s , \u03b1) \u220b (As , A) ++ \u03b2s \u220b Bs) t \u2192\n T\u27e6 T \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u27e8 B \u27e9 As A Bs t a = a\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s (T \u2227 U) As A Bs t (a , b) = (\u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s T As A Bs t a , \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s U As A Bs t b)\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s (T \u21d2 U) As A Bs t f = \u03bb a \u2192 \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s U As A Bs t (f (\u27e6tweaken\u27e7 \u03b1 \u03b2s T As A Bs t a))\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s (T \u22b5 U) As A Bs t f = \u03bb v t\u2264v \u03c3 \u2192 \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s U As A Bs v (f v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tweaken\u27e7 \u03b1 \u03b2s T As A Bs u (\u03c3 u t\u2264u u\u2264v)))\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s (tvar \u03c4) As A Bs t a = \u27e6\u03c4weaken\u207b\u00b9\u27e7 \u03b1 \u03b2s \u03c4 As A Bs t a\n \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s (univ \u03b2 T) As A Bs t f = \u03bb B \u2192 \u27e6tweaken\u207b\u00b9\u27e7 \u03b1 (\u03b2s , \u03b2) T As A (Bs , B) t (f B)\n\nmutual\n\n \u27e6tweaken\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 \u03b2s (T : Typ (\u03b1s + \u03b2s)) {As Bs A B Cs Ds} \u211cs \u211c \u2111s t {a b} \u2192\n T\u27e6 T \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192 \n T\u27e6 tweaken \u03b1 \u03b2s T \u27e7\u00b2 ((\u03b1s , \u03b1) \u220b (\u211cs , \u211c) ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6tweaken\u27e7 \u03b1 \u03b2s T As A Cs t a) (\u27e6tweaken\u27e7 \u03b1 \u03b2s T Bs B Ds t b)\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s \u27e8 B \u27e9 \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s (T \u2227 U) \u211cs \u211c \u2111s t (a\u211cb , c\u211cd) = (\u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s t a\u211cb , \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s t c\u211cd)\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s (T \u21d2 U) \u211cs \u211c \u2111s t f\u211cg = \u03bb a\u211cb \u2192 \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s t (f\u211cg (\u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s t a\u211cb))\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s (T \u22b5 U) \u211cs \u211c \u2111s t f\u211cg = \u03bb v t\u2264v \u03c3\u211c\u03c4 \u2192 \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s v (f\u211cg v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s u (\u03c3\u211c\u03c4 u t\u2264u u\u2264v)))\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s (tvar \u03c4) \u211cs \u211c \u2111s t a\u211cb = \u27e6\u03c4weaken\u27e7\u00b2 \u03b1 \u03b2s \u03c4 \u211cs \u211c \u2111s t a\u211cb\n \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s (univ \u03b2 T) \u211cs \u211c \u2111s t f\u211cg = \u03bb \u2111 \u2192 \u27e6tweaken\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) T \u211cs \u211c (\u2111s , \u2111) t (f\u211cg \u2111)\n\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 \u03b2s (T : Typ (\u03b1s + \u03b2s)) {As Bs A B Cs Ds} \u211cs \u211c \u2111s t {a b} \u2192\n T\u27e6 tweaken \u03b1 \u03b2s T \u27e7\u00b2 ((\u03b1s , \u03b1) \u220b (\u211cs , \u211c) ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n T\u27e6 T \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s T As A Cs t a) (\u27e6tweaken\u207b\u00b9\u27e7 \u03b1 \u03b2s T Bs B Ds t b)\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s \u27e8 B \u27e9 \u211cs \u211c \u2111s t a\u211cb = a\u211cb\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s (T \u2227 U) \u211cs \u211c \u2111s t (a\u211cb , c\u211cd) = (\u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s t a\u211cb , \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s t c\u211cd)\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s (T \u21d2 U) \u211cs \u211c \u2111s t f\u211cg = \u03bb a\u211cb \u2192 \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s t (f\u211cg (\u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s t a\u211cb))\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s (T \u22b5 U) \u211cs \u211c \u2111s t f\u211cg = \u03bb v t\u2264v \u03c3\u211c\u03c4 \u2192 \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s U \u211cs \u211c \u2111s v (f\u211cg v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b2s T \u211cs \u211c \u2111s u (\u03c3\u211c\u03c4 u t\u2264u u\u2264v)))\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s (tvar \u03c4) \u211cs \u211c \u2111s t a\u211cb = \u27e6\u03c4weaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s \u03c4 \u211cs \u211c \u2111s t a\u211cb\n \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 \u03b2s (univ \u03b2 T) \u211cs \u211c \u2111s t f\u211cg = \u03bb \u2111 \u2192 \u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b1 (\u03b2s , \u03b2) T \u211cs \u211c (\u2111s , \u2111) t (f\u211cg \u2111)\n\n-- Weakening of contexts\n\ncweaken : \u2200 {\u03b1s} \u03b1 \u2192 Ctxt \u03b1s \u2192 Ctxt (\u03b1s , \u03b1)\ncweaken \u03b1 \u03b5 = \u03b5\ncweaken \u03b1 (\u0393 , T at t) = (cweaken \u03b1 \u0393 , tweaken \u03b1 \u03b5 T at t)\n \n\u27e6cweaken\u27e7 : \u2200 {\u03b1s} \u03b1 (\u0393 : Ctxt \u03b1s) As A \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7 As \u27e9 \u2192 \u27e8 clevels (cweaken \u03b1 \u0393) \u220b \u0393\u27e6 cweaken \u03b1 \u0393 \u27e7 (As , A) \u27e9\n\u27e6cweaken\u27e7 \u03b1 \u03b5 As A tt = tt\n\u27e6cweaken\u27e7 \u03b1 (\u0393 , T at t) As A (as , a) = (\u27e6cweaken\u27e7 \u03b1 \u0393 As A as , \u27e6tweaken\u27e7 \u03b1 \u03b5 T As A tt t a)\n\n\u27e6cweaken\u27e7\u00b2 : \u2200 {\u03b1s} \u03b1 (\u0393 : Ctxt \u03b1s) {As Bs A B} \u211cs \u211c {as bs} \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs \u27e9\u00b2 as bs \u2192\n \u27e8 clevels (cweaken \u03b1 \u0393) \u220b \u0393\u27e6 cweaken \u03b1 \u0393 \u27e7\u00b2 (\u211cs , \u211c) \u27e9\u00b2 (\u27e6cweaken\u27e7 \u03b1 \u0393 As A as) (\u27e6cweaken\u27e7 \u03b1 \u0393 Bs B bs)\n\u27e6cweaken\u27e7\u00b2 \u03b1 \u03b5 \u211cs \u211c tt = tt\n\u27e6cweaken\u27e7\u00b2 \u03b1 (\u0393 , T at t) \u211cs \u211c (as\u211cbs , a\u211cb) = (\u27e6cweaken\u27e7\u00b2 \u03b1 \u0393 \u211cs \u211c as\u211cbs , \u27e6tweaken\u27e7\u00b2 \u03b1 \u03b5 T \u211cs \u211c tt t a\u211cb)\n\n-- Substitution into type variables\n\n\u03c4subst : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s \u2192 TVar ((\u03b1s , tlevel T) + \u03b2s) \u2192 Typ (\u03b1s + \u03b2s)\n\u03c4subst T \u03b5 zero = T\n\u03c4subst T \u03b5 (suc \u03c4) = tvar \u03c4\n\u03c4subst T (\u03b2s , \u03b2) zero = tvar zero\n\u03c4subst T (\u03b2s , \u03b2) (suc \u03c4) = tweaken \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4)\n\n\u27e6\u03c4subst\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (\u03c4 : TVar ((\u03b1s , tlevel T) + \u03b2s)) As Bs t \u2192\n \u03c4\u27e6 \u03c4 \u27e7 ((\u03b1s , tlevel T) \u220b (As , T\u27e6 T \u27e7 As) ++ \u03b2s \u220b Bs) t \u2192 \n T\u27e6 \u03c4subst T \u03b2s \u03c4 \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t\n\u27e6\u03c4subst\u27e7 T \u03b5 zero As Bs t a = a\n\u27e6\u03c4subst\u27e7 T \u03b5 (suc \u03c4) As Bs t a = a\n\u27e6\u03c4subst\u27e7 T (\u03b2s , \u03b2) zero As Bs t a = a \n\u27e6\u03c4subst\u27e7 {\u03b1s} T (\u03b2s , \u03b2) (suc \u03c4) As (Bs , B) t a = \n \u27e6tweaken\u27e7 \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4) (\u03b1s \u220b As ++ \u03b2s \u220b Bs) B tt t \n (\u27e6\u03c4subst\u27e7 T \u03b2s \u03c4 As Bs t a)\n\n\u27e6\u03c4subst\u207b\u00b9\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (\u03c4 : TVar ((\u03b1s , tlevel T) + \u03b2s)) As Bs t \u2192\n T\u27e6 \u03c4subst T \u03b2s \u03c4 \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t \u2192\n \u03c4\u27e6 \u03c4 \u27e7 ((\u03b1s , tlevel T) \u220b (As , T\u27e6 T \u27e7 As) ++ \u03b2s \u220b Bs) t\n\u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b5 zero As Bs t a = a\n\u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b5 (suc \u03c4) As Bs t a = a\n\u27e6\u03c4subst\u207b\u00b9\u27e7 T (\u03b2s , \u03b2) zero As Bs t a = a \n\u27e6\u03c4subst\u207b\u00b9\u27e7 {\u03b1s} T (\u03b2s , \u03b2) (suc \u03c4) As (Bs , B) t a = \n \u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b2s \u03c4 As Bs t \n (\u27e6tweaken\u207b\u00b9\u27e7 \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4) (\u03b1s \u220b As ++ \u03b2s \u220b Bs) B tt t a)\n\n\u27e6\u03c4subst\u27e7\u00b2 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (\u03c4 : TVar ((\u03b1s , tlevel T) + \u03b2s)) {As Bs Cs Ds} \u211cs \u2111s t {a b} \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 ((\u03b1s , tlevel T) \u220b (\u211cs , T\u27e6 T \u27e7\u00b2 \u211cs) ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n T\u27e6 \u03c4subst T \u03b2s \u03c4 \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6\u03c4subst\u27e7 T \u03b2s \u03c4 As Bs t a) (\u27e6\u03c4subst\u27e7 T \u03b2s \u03c4 Cs Ds t b)\n\u27e6\u03c4subst\u27e7\u00b2 T \u03b5 zero \u211cs \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4subst\u27e7\u00b2 T \u03b5 (suc \u03c4) \u211cs \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4subst\u27e7\u00b2 T (\u03b2s , \u03b2) zero \u211cs \u2111s t a\u211cb = a\u211cb \n\u27e6\u03c4subst\u27e7\u00b2 {\u03b1s} T (\u03b2s , \u03b2) (suc \u03c4) \u211cs (\u2111s , \u2111) t a\u211cb = \n \u27e6tweaken\u27e7\u00b2 \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4) (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) \u2111 tt t \n (\u27e6\u03c4subst\u27e7\u00b2 T \u03b2s \u03c4 \u211cs \u2111s t a\u211cb)\n\n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (\u03c4 : TVar ((\u03b1s , tlevel T) + \u03b2s)) {As Bs Cs Ds} \u211cs \u2111s t {a b} \u2192\n T\u27e6 \u03c4subst T \u03b2s \u03c4 \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 ((\u03b1s , tlevel T) \u220b (\u211cs , T\u27e6 T \u27e7\u00b2 \u211cs) ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b2s \u03c4 As Bs t a) (\u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b2s \u03c4 Cs Ds t b)\n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T \u03b5 zero \u211cs \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T \u03b5 (suc \u03c4) \u211cs \u2111s t a\u211cb = a\u211cb\n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T (\u03b2s , \u03b2) zero \u211cs \u2111s t a\u211cb = a\u211cb \n\u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 {\u03b1s} T (\u03b2s , \u03b2) (suc \u03c4) \u211cs (\u2111s , \u2111) t a\u211cb = \n \u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T \u03b2s \u03c4 \u211cs \u2111s t \n (\u27e6tweaken\u207b\u00b9\u27e7\u00b2 \u03b2 \u03b5 (\u03c4subst T \u03b2s \u03c4) (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) \u2111 tt t a\u211cb)\n\n-- Substitution into types\n\ntsubst : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s \u2192 Typ ((\u03b1s , tlevel T) + \u03b2s) \u2192 Typ (\u03b1s + \u03b2s)\ntsubst T \u03b2s \u27e8 A \u27e9 = \u27e8 A \u27e9\ntsubst T \u03b2s (U \u2227 V) = tsubst T \u03b2s U \u2227 tsubst T \u03b2s V\ntsubst T \u03b2s (U \u21d2 V) = tsubst T \u03b2s U \u21d2 tsubst T \u03b2s V\ntsubst T \u03b2s (U \u22b5 V) = tsubst T \u03b2s U \u22b5 tsubst T \u03b2s V\ntsubst T \u03b2s (tvar \u03c4) = \u03c4subst T \u03b2s \u03c4\ntsubst T \u03b2s (univ \u03b2 U) = univ \u03b2 (tsubst T (\u03b2s , \u03b2) U)\n\nmutual\n\n \u27e6tsubst\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (U : Typ ((\u03b1s , tlevel T) + \u03b2s)) As Bs t \u2192\n T\u27e6 U \u27e7 ((\u03b1s , tlevel T) \u220b (As , T\u27e6 T \u27e7 As) ++ \u03b2s \u220b Bs) t \u2192 \n T\u27e6 tsubst T \u03b2s U \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t\n \u27e6tsubst\u27e7 T \u03b2s \u27e8 A \u27e9 As Bs t a = a\n \u27e6tsubst\u27e7 T \u03b2s (U \u2227 V) As Bs t (a , b) = (\u27e6tsubst\u27e7 T \u03b2s U As Bs t a , \u27e6tsubst\u27e7 T \u03b2s V As Bs t b)\n \u27e6tsubst\u27e7 T \u03b2s (U \u21d2 V) As Bs t f = \u03bb a \u2192 \u27e6tsubst\u27e7 T \u03b2s V As Bs t (f (\u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s U As Bs t a))\n \u27e6tsubst\u27e7 T \u03b2s (U \u22b5 V) As Bs t f = \u03bb v t\u2264v \u03c3 \u2192 \u27e6tsubst\u27e7 T \u03b2s V As Bs v (f v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s U As Bs u (\u03c3 u t\u2264u u\u2264v)))\n \u27e6tsubst\u27e7 T \u03b2s (tvar \u03c4) As Bs t a = \u27e6\u03c4subst\u27e7 T \u03b2s \u03c4 As Bs t a\n \u27e6tsubst\u27e7 T \u03b2s (univ \u03b2 U) As Bs t f = \u03bb B \u2192 \u27e6tsubst\u27e7 T (\u03b2s , \u03b2) U As (Bs , B) t (f B)\n\n \u27e6tsubst\u207b\u00b9\u27e7 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (U : Typ ((\u03b1s , tlevel T) + \u03b2s)) As Bs t \u2192\n T\u27e6 tsubst T \u03b2s U \u27e7 (\u03b1s \u220b As ++ \u03b2s \u220b Bs) t \u2192\n T\u27e6 U \u27e7 ((\u03b1s , tlevel T) \u220b (As , T\u27e6 T \u27e7 As) ++ \u03b2s \u220b Bs) t\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s \u27e8 A \u27e9 As Bs t a = a\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s (U \u2227 V) As Bs t (a , b) = (\u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s U As Bs t a , \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s V As Bs t b)\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s (U \u21d2 V) As Bs t f = \u03bb a \u2192 \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s V As Bs t (f (\u27e6tsubst\u27e7 T \u03b2s U As Bs t a))\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s (U \u22b5 V) As Bs t f = \u03bb v t\u2264v \u03c3 \u2192 \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s V As Bs v (f v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tsubst\u27e7 T \u03b2s U As Bs u (\u03c3 u t\u2264u u\u2264v)))\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s (tvar \u03c4) As Bs t a = \u27e6\u03c4subst\u207b\u00b9\u27e7 T \u03b2s \u03c4 As Bs t a\n \u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s (univ \u03b2 U) As Bs t f = \u03bb B \u2192 \u27e6tsubst\u207b\u00b9\u27e7 T (\u03b2s , \u03b2) U As (Bs , B) t (f B)\n\nmutual\n\n \u27e6tsubst\u27e7\u00b2 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (U : Typ ((\u03b1s , tlevel T) + \u03b2s)) {As Bs Cs Ds} \u211cs \u2111s t {a b} \u2192\n T\u27e6 U \u27e7\u00b2 ((\u03b1s , tlevel T) \u220b (\u211cs , T\u27e6 T \u27e7\u00b2 \u211cs) ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n T\u27e6 tsubst T \u03b2s U \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6tsubst\u27e7 T \u03b2s U As Bs t a) (\u27e6tsubst\u27e7 T \u03b2s U Cs Ds t b)\n \u27e6tsubst\u27e7\u00b2 T \u03b2s \u27e8 A \u27e9 \u211cs \u2111s t a\u211cb = a\u211cb\n \u27e6tsubst\u27e7\u00b2 T \u03b2s (U \u2227 V) \u211cs \u2111s t (a\u211cb , c\u211cd) = (\u27e6tsubst\u27e7\u00b2 T \u03b2s U \u211cs \u2111s t a\u211cb , \u27e6tsubst\u27e7\u00b2 T \u03b2s V \u211cs \u2111s t c\u211cd)\n \u27e6tsubst\u27e7\u00b2 T \u03b2s (U \u21d2 V) \u211cs \u2111s t f\u211cg = \u03bb a\u211cb \u2192 \u27e6tsubst\u27e7\u00b2 T \u03b2s V \u211cs \u2111s t (f\u211cg (\u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s U \u211cs \u2111s t a\u211cb))\n \u27e6tsubst\u27e7\u00b2 T \u03b2s (U \u22b5 V) \u211cs \u2111s t f\u211cg = \u03bb v t\u2264v \u03c3\u211c\u03c4 \u2192 \u27e6tsubst\u27e7\u00b2 T \u03b2s V \u211cs \u2111s v (f\u211cg v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s U \u211cs \u2111s u (\u03c3\u211c\u03c4 u t\u2264u u\u2264v)))\n \u27e6tsubst\u27e7\u00b2 T \u03b2s (tvar \u03c4) \u211cs \u2111s t a\u211cb = \u27e6\u03c4subst\u27e7\u00b2 T \u03b2s \u03c4 \u211cs \u2111s t a\u211cb\n \u27e6tsubst\u27e7\u00b2 T \u03b2s (univ \u03b2 U) \u211cs \u2111s t f\u211cg = \u03bb \u2111 \u2192 \u27e6tsubst\u27e7\u00b2 T (\u03b2s , \u03b2) U \u211cs (\u2111s , \u2111) t (f\u211cg \u2111)\n\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 : \u2200 {\u03b1s} (T : Typ \u03b1s) \u03b2s (U : Typ ((\u03b1s , tlevel T) + \u03b2s)) {As Bs Cs Ds} \u211cs \u2111s t {a b} \u2192\n T\u27e6 tsubst T \u03b2s U \u27e7\u00b2 (\u03b1s \u220b \u211cs ++\u00b2 \u03b2s \u220b \u2111s) t a b \u2192\n T\u27e6 U \u27e7\u00b2 ((\u03b1s , tlevel T) \u220b (\u211cs , T\u27e6 T \u27e7\u00b2 \u211cs) ++\u00b2 \u03b2s \u220b \u2111s) t (\u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s U As Bs t a) (\u27e6tsubst\u207b\u00b9\u27e7 T \u03b2s U Cs Ds t b)\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s \u27e8 A \u27e9 \u211cs \u2111s t a\u211cb = a\u211cb\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s (U \u2227 V) \u211cs \u2111s t (a\u211cb , c\u211cd) = (\u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s U \u211cs \u2111s t a\u211cb , \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s V \u211cs \u2111s t c\u211cd)\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s (U \u21d2 V) \u211cs \u2111s t f\u211cg = \u03bb a\u211cb \u2192 \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s V \u211cs \u2111s t (f\u211cg (\u27e6tsubst\u27e7\u00b2 T \u03b2s U \u211cs \u2111s t a\u211cb))\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s (U \u22b5 V) \u211cs \u2111s t f\u211cg = \u03bb v t\u2264v \u03c3\u211c\u03c4 \u2192 \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s V \u211cs \u2111s v (f\u211cg v t\u2264v (\u03bb u t\u2264u u\u2264v \u2192 \u27e6tsubst\u27e7\u00b2 T \u03b2s U \u211cs \u2111s u (\u03c3\u211c\u03c4 u t\u2264u u\u2264v)))\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s (tvar \u03c4) \u211cs \u2111s t a\u211cb = \u27e6\u03c4subst\u207b\u00b9\u27e7\u00b2 T \u03b2s \u03c4 \u211cs \u2111s t a\u211cb\n \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T \u03b2s (univ \u03b2 U) \u211cs \u2111s t f\u211cg = \u03bb \u2111 \u2192 \u27e6tsubst\u207b\u00b9\u27e7\u00b2 T (\u03b2s , \u03b2) U \u211cs (\u2111s , \u2111) t (f\u211cg \u2111)\n\n-- Variables\n\ndata Var {\u03b1s} (T : Typ \u03b1s) (t : Time) : (\u0393 : Ctxt \u03b1s) \u2192 Set\u2081 where\n zero : \u2200 {\u0393 : Ctxt \u03b1s} \u2192 Var T t (\u0393 , T at t)\n suc : \u2200 {\u0393 : Ctxt \u03b1s} {U : Typ \u03b1s} {u} \u2192 Var T t \u0393 \u2192 Var T t (\u0393 , U at u)\n\nv\u27e6_\u27e7 : \u2200 {\u03b1s} {\u0393 : Ctxt \u03b1s} {T : Typ \u03b1s} {t} \u2192 Var T t \u0393 \u2192 \u2200 As \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7 As \u27e9 \u2192 T\u27e6 T \u27e7 As t\nv\u27e6 zero \u27e7 As (as , a) = a\nv\u27e6 suc v \u27e7 As (as , a) = v\u27e6 v \u27e7 As as\n\nv\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} {\u0393 : Ctxt \u03b1s} {T : Typ \u03b1s} {t} (\u03c4 : Var T t \u0393) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 \n \u2200 {as bs} \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs \u27e9\u00b2 as bs \u2192 T\u27e6 T \u27e7\u00b2 \u211cs t (v\u27e6 \u03c4 \u27e7 As as) (v\u27e6 \u03c4 \u27e7 Bs bs)\nv\u27e6 zero \u27e7\u00b2 \u211cs (as\u211cbs , a\u211cb) = a\u211cb\nv\u27e6 suc v \u27e7\u00b2 \u211cs (as\u211cbs , a\u211cb) = v\u27e6 v \u27e7\u00b2 \u211cs as\u211cbs\n\n-- Expressions\n\ndata Exp : \u2200 {\u03b1s} \u2192 Ctxt \u03b1s \u2192 Typ \u03b1s \u2192 RSet\u2081 where\n const : \u2200 {\u03b1s \u0393 A t} \u2192 (a : A) \u2192 Exp {\u03b1s} \u0393 \u27e8 A \u27e9 t\n pair : \u2200 {\u03b1s \u0393 T U t} \u2192 (e : Exp \u0393 T t) \u2192 (f : Exp \u0393 U t) \u2192 Exp {\u03b1s} \u0393 (T \u2227 U) t\n fst : \u2200 {\u03b1s \u0393 T U t} \u2192 (e : Exp \u0393 (T \u2227 U) t) \u2192 Exp {\u03b1s} \u0393 T t\n snd : \u2200 {\u03b1s \u0393 T U t} \u2192 (e : Exp \u0393 (T \u2227 U) t) \u2192 Exp {\u03b1s} \u0393 U t\n abs : \u2200 {\u03b1s \u0393 T U t} \u2192 (e : Exp (\u0393 , T at t) U t) \u2192 Exp {\u03b1s} \u0393 (T \u21d2 U) t\n app : \u2200 {\u03b1s \u0393 T U t} \u2192 (f : Exp \u0393 (T \u21d2 U) t) \u2192 (e : Exp \u0393 T t) \u2192 Exp {\u03b1s} \u0393 U t\n var : \u2200 {\u03b1s \u0393 T t} \u2192 (v : Var T t \u0393) \u2192 Exp {\u03b1s} \u0393 T t\n tabs : \u2200 {\u03b1s \u0393} \u03b1 {T t} \u2192 (e : Exp (cweaken \u03b1 \u0393) T t) \u2192 Exp {\u03b1s} \u0393 (univ \u03b1 T) t\n tapp : \u2200 {\u03b1s \u0393 t} (T : Typ \u03b1s) {U} \u2192 (e : Exp \u0393 (univ (tlevel T) U) t) \u2192 Exp {\u03b1s} \u0393 (tsubst T \u03b5 U) t\n\ne\u27e6_\u27e7 : \u2200 {\u03b1s} {\u0393 : Ctxt \u03b1s} {T : Typ \u03b1s} {t} \u2192 Exp \u0393 T t \u2192 \u2200 As \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7 As \u27e9 \u2192 T\u27e6 T \u27e7 As t\ne\u27e6 const a \u27e7 As as = a\ne\u27e6 pair e f \u27e7 As as = (e\u27e6 e \u27e7 As as , e\u27e6 f \u27e7 As as)\ne\u27e6 fst e \u27e7 As as = proj\u2081 (e\u27e6 e \u27e7 As as)\ne\u27e6 snd e \u27e7 As as = proj\u2082 (e\u27e6 e \u27e7 As as)\ne\u27e6 abs e \u27e7 As as = \u03bb a \u2192 e\u27e6 e \u27e7 As (as , a)\ne\u27e6 app f e \u27e7 As as = e\u27e6 f \u27e7 As as (e\u27e6 e \u27e7 As as)\ne\u27e6 var v \u27e7 As as = v\u27e6 v \u27e7 As as\ne\u27e6 tabs {\u0393 = \u0393} \u03b1 e \u27e7 As as = \u03bb A \u2192 e\u27e6 e \u27e7 (As , A) (\u27e6cweaken\u27e7 \u03b1 \u0393 As A as)\ne\u27e6 tapp {t = t} T {U = U} e \u27e7 As as = \u27e6tsubst\u27e7 T \u03b5 U As tt t (e\u27e6 e \u27e7 As as (T\u27e6 T \u27e7 As))\n\ne\u27e6_\u27e7\u00b2 : \u2200 {\u03b1s As Bs} {\u0393 : Ctxt \u03b1s} {T : Typ \u03b1s} {t} (e : Exp \u0393 T t) (\u211cs : \u03b1s \u220b As \u21d4* Bs) \u2192 \n \u2200 {as bs} \u2192 \u27e8 clevels \u0393 \u220b \u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs \u27e9\u00b2 as bs \u2192 T\u27e6 T \u27e7\u00b2 \u211cs t (e\u27e6 e \u27e7 As as) (e\u27e6 e \u27e7 Bs bs)\ne\u27e6 const a \u27e7\u00b2 \u211cs as\u211cbs = refl\ne\u27e6 pair e f \u27e7\u00b2 \u211cs as\u211cbs = (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs , e\u27e6 f \u27e7\u00b2 \u211cs as\u211cbs)\ne\u27e6 fst e \u27e7\u00b2 \u211cs as\u211cbs = proj\u2081 (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs)\ne\u27e6 snd e \u27e7\u00b2 \u211cs as\u211cbs = proj\u2082 (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs)\ne\u27e6 abs e \u27e7\u00b2 \u211cs as\u211cbs = \u03bb a\u211cb \u2192 e\u27e6 e \u27e7\u00b2 \u211cs (as\u211cbs , a\u211cb)\ne\u27e6 app f e \u27e7\u00b2 \u211cs as\u211cbs = e\u27e6 f \u27e7\u00b2 \u211cs as\u211cbs (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs)\ne\u27e6 var v \u27e7\u00b2 \u211cs as\u211cbs = v\u27e6 v \u27e7\u00b2 \u211cs as\u211cbs\ne\u27e6 tabs {\u0393 = \u0393} \u03b1 e \u27e7\u00b2 \u211cs as\u211cbs = \u03bb \u211c \u2192 e\u27e6 e \u27e7\u00b2 (\u211cs , \u211c) (\u27e6cweaken\u27e7\u00b2 \u03b1 \u0393 \u211cs \u211c as\u211cbs)\ne\u27e6 tapp {t = t} T {U = U} e \u27e7\u00b2 \u211cs as\u211cbs = \u27e6tsubst\u27e7\u00b2 T \u03b5 U \u211cs tt t (e\u27e6 e \u27e7\u00b2 \u211cs as\u211cbs (T\u27e6 T \u27e7\u00b2 \u211cs))\n\n-- Surface level types\n\ndata STyp : Set\u2081 where\n \u27e8_\u27e9 : (A : Set) \u2192 STyp\n _\u2227_ _\u21d2_ : (T : STyp) \u2192 (U : STyp) \u2192 STyp\n \u25a1 : (T : STyp) \u2192 STyp\n\n-- Translation of surface level types into types\n\n\u27ea_\u27eb : STyp \u2192 Typ (\u03b5 , o)\n\u27ea \u27e8 A \u27e9 \u27eb = \u27e8 A \u27e9\n\u27ea T \u2227 U \u27eb = \u27ea T \u27eb \u2227 \u27ea U \u27eb\n\u27ea T \u21d2 U \u27eb = \u27ea T \u27eb \u21d2 \u27ea U \u27eb\n\u27ea \u25a1 T \u27eb = tvar zero \u22b5 \u27ea T \u27eb\n\nT\u27ea_\u27eb : STyp \u2192 RSet\u2080\nT\u27ea \u27e8 A \u27e9 \u27eb t = A\nT\u27ea T \u2227 U \u27eb t = T\u27ea T \u27eb t \u00d7 T\u27ea U \u27eb t\nT\u27ea T \u21d2 U \u27eb t = T\u27ea T \u27eb t \u2192 T\u27ea U \u27eb t\nT\u27ea \u25a1 T \u27eb t = \u2200 u \u2192 True (t \u2264 u) \u2192 T\u27ea T \u27eb u\n\nWorld : RSet\u2080\nWorld t = \u22a4\n\nmutual\n\n trans : \u2200 T {t} \u2192 T\u27ea T \u27eb t \u2192 T\u27e6 \u27ea T \u27eb \u27e7 (tt , World) t\n trans \u27e8 A \u27e9 a = a\n trans (T \u2227 U) (a , b) = (trans T a , trans U b)\n trans (T \u21d2 U) f = \u03bb a \u2192 trans U (f (trans\u207b\u00b9 T a))\n trans (\u25a1 T) \u03c3 = \u03bb u t\u2264u \u03c4 \u2192 trans T (\u03c3 u t\u2264u)\n\n trans\u207b\u00b9 : \u2200 T {t} \u2192 T\u27e6 \u27ea T \u27eb \u27e7 (tt , World) t \u2192 T\u27ea T \u27eb t\n trans\u207b\u00b9 \u27e8 A \u27e9 a = a\n trans\u207b\u00b9 (T \u2227 U) (a , b) = (trans\u207b\u00b9 T a , trans\u207b\u00b9 U b)\n trans\u207b\u00b9 (T \u21d2 U) f = \u03bb a \u2192 trans\u207b\u00b9 U (f (trans T a))\n trans\u207b\u00b9 (\u25a1 T) \u03c3 = \u03bb u t\u2264u \u2192 trans\u207b\u00b9 T (\u03c3 u t\u2264u _)\n\n-- Causality\n\n_at_\u220b_\u2248[_\u2235_]_ : \u2200 T t \u2192 T\u27ea T \u27eb t \u2192 \u2200 u \u2192 True (t \u2264 u) \u2192 T\u27ea T \u27eb t \u2192 Set\n\u27e8 A \u27e9 at t \u220b a \u2248[ u \u2235 t\u2264u ] b = a \u2261 b\n(T \u2227 U) at t \u220b (a , b) \u2248[ u \u2235 t\u2264u ] (c , d) = (T at t \u220b a \u2248[ u \u2235 t\u2264u ] c) \u00d7 (U at t \u220b b \u2248[ u \u2235 t\u2264u ] d)\n(T \u21d2 U) at t \u220b f \u2248[ u \u2235 t\u2264u ] g = \u2200 {a b} \u2192 (T at t \u220b a \u2248[ u \u2235 t\u2264u ] b) \u2192 (U at t \u220b f a \u2248[ u \u2235 t\u2264u ] g b)\n(\u25a1 T) at s \u220b \u03c3 \u2248[ u \u2235 s\u2264u ] \u03c4 = \u2200 t s\u2264t t\u2264u \u2192 (T at t \u220b \u03c3 t s\u2264t \u2248[ u \u2235 t\u2264u ] \u03c4 t s\u2264t)\n\nCausal : \u2200 T U t \u2192 T\u27ea T \u21d2 U \u27eb t \u2192 Set\nCausal T U t f = \u2200 u t\u2264u {a b} \u2192 (T at t \u220b a \u2248[ u \u2235 t\u2264u ] b) \u2192 (U at t \u220b f a \u2248[ u \u2235 t\u2264u ] f b)\n\n-- Parametricity implies causality\n\n\u211c[_] : Time \u2192 (o \u220b World \u21d4 World)\n\u211c[ u ] t tt tt = True (t \u2264 u)\n\nmutual\n\n \u211c-impl-\u2248 : \u2200 T t u t\u2264u {a b} \u2192\n T\u27e6 \u27ea T \u27eb \u27e7\u00b2 (tt , \u211c[ u ]) t a b \u2192\n (T at t \u220b trans\u207b\u00b9 T a \u2248[ u \u2235 t\u2264u ] trans\u207b\u00b9 T b)\n \u211c-impl-\u2248 \u27e8 A \u27e9 t u t\u2264u a\u211cb = a\u211cb\n \u211c-impl-\u2248 (T \u2227 U) t u t\u2264u (a\u211cc , b\u211cd) = (\u211c-impl-\u2248 T t u t\u2264u a\u211cc , \u211c-impl-\u2248 U t u t\u2264u b\u211cd)\n \u211c-impl-\u2248 (T \u21d2 U) t u t\u2264u f\u211cg = \u03bb a\u2248b \u2192 \u211c-impl-\u2248 U t u t\u2264u (f\u211cg (\u2248-impl-\u211c T t u t\u2264u a\u2248b))\n \u211c-impl-\u2248 (\u25a1 T) s v s\u2264v \u03c3\u211c\u03c4 = \u03bb u s\u2264u u\u2264v \u2192 \u211c-impl-\u2248 T u v u\u2264v (\u03c3\u211c\u03c4 u s\u2264u (\u03bb t s\u2264t t\u2264u \u2192 \u2264-trans t u v t\u2264u u\u2264v))\n\n \u2248-impl-\u211c : \u2200 T t u t\u2264u {a b} \u2192\n (T at t \u220b a \u2248[ u \u2235 t\u2264u ] b) \u2192\n T\u27e6 \u27ea T \u27eb \u27e7\u00b2 (tt , \u211c[ u ]) t (trans T a) (trans T b)\n \u2248-impl-\u211c \u27e8 A \u27e9 t u t\u2264u a\u2248b = a\u2248b\n \u2248-impl-\u211c (T \u2227 U) t u t\u2264u (a\u2248c , b\u2248d) = (\u2248-impl-\u211c T t u t\u2264u a\u2248c , \u2248-impl-\u211c U t u t\u2264u b\u2248d)\n \u2248-impl-\u211c (T \u21d2 U) t u t\u2264u f\u2248g = \u03bb a\u211cb \u2192 \u2248-impl-\u211c U t u t\u2264u (f\u2248g (\u211c-impl-\u2248 T t u t\u2264u a\u211cb))\n \u2248-impl-\u211c (\u25a1 T) s v s\u2264v \u03c3\u2248\u03c4 = \u03bb u s\u2264u \u03c1 \u2192 \u2248-impl-\u211c T u v (\u03c1 u s\u2264u (\u2264-refl u)) (\u03c3\u2248\u03c4 u s\u2264u (\u03c1 u s\u2264u (\u2264-refl u)))\n\n-- Every expression is causal\n\ne\u27ea_at_\u220b_\u27eb : \u2200 T t \u2192 Exp \u03b5 \u27ea T \u27eb t \u2192 T\u27ea T \u27eb t\ne\u27ea T at t \u220b e \u27eb = trans\u207b\u00b9 T (e\u27e6 e \u27e7 (tt , World) tt)\n\ncausality : \u2200 T U t f \u2192 Causal T U t e\u27ea (T \u21d2 U) at t \u220b f \u27eb \ncausality T U t f u t\u2264u = \u211c-impl-\u2248 (T \u21d2 U) t u t\u2264u (e\u27e6 f \u27e7\u00b2 (tt , \u211c[ u ]) tt)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"872efb80a4cc6d30136b2075976989235e6b3de3","subject":"Possible agda bug","message":"Possible agda bug\n","repos":"xekoukou\/sparrow,xekoukou\/sparrow,xekoukou\/sparrow,xekoukou\/sparrow","old_file":"agda\/LinFunContructw.agda","new_file":"agda\/LinFunContructw.agda","new_contents":"module LinFunContructw where\n\nopen import Common\nopen import LinLogic\nopen import IndexLLProp\nopen import LinFun\nopen import SetLL\nopen import SetLLProp\n\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Product\n\nopen import LinFunContruct\n\next\u21d2\u00acho : \u2200{i u pll rll ll} \u2192 \u2200 s \u2192 (ind : IndexLL {i} {u} pll ll) \u2192 (lind : IndexLL rll ll)\n \u2192 \u00ac Ordered\u1d62 lind ind \u2192 \u00ac hitsAtLeastOnce (extend ind s) lind\next\u21d2\u00acho s \u2193 lind \u00acord x = \u00acord (b\u2264\u1d62a \u2264\u1d62\u2193)\next\u21d2\u00acho s (ind \u2190\u2227) \u2193 \u00acord x = \u00acord (a\u2264\u1d62b \u2264\u1d62\u2193)\next\u21d2\u00acho {pll = pll} {_} {ll = li \u2227 _} s (ind \u2190\u2227) (lind \u2190\u2227) \u00acord\n with replLL li ind pll | replLL-id li ind pll refl | extendg ind s | ext\u21d2\u00acho s ind lind hf where\n hf : \u00ac Ordered\u1d62 lind ind\n hf (a\u2264\u1d62b x\u2081) = \u00acord (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 x\u2081))\n hf (b\u2264\u1d62a x\u2081) = \u00acord (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 x\u2081))\next\u21d2\u00acho {pll = pll} {_} {li \u2227 _} s (ind \u2190\u2227) (lind \u2190\u2227) \u00acord | .li | refl | t | e = {!!} where\n hf : \u00ac hitsAtLeastOnce (t \u2190\u2227) (lind \u2190\u2227)\n hf (hitsAtLeastOnce\u2190\u2227\u2190\u2227 x) = {!!}\next\u21d2\u00acho {pll = pll} {_} {ll = li \u2227 _} s (ind \u2190\u2227) (\u2227\u2192 lind) \u00acord x with replLL li ind pll | replLL-id li ind pll refl | extendg ind s\next\u21d2\u00acho {_} {_} {pll} {_} {li \u2227 _} s (ind \u2190\u2227) (\u2227\u2192 lind) \u00acord () | .li | refl | t\next\u21d2\u00acho s (\u2227\u2192 ind) \u2193 \u00acord x = \u00acord (a\u2264\u1d62b \u2264\u1d62\u2193)\next\u21d2\u00acho s (\u2227\u2192 ind) (lind \u2190\u2227) \u00acord x = {!!}\next\u21d2\u00acho s (\u2227\u2192 ind) (\u2227\u2192 lind) \u00acord x = {!!}\next\u21d2\u00acho s (ind \u2190\u2228) lind \u00acord x = {!!}\next\u21d2\u00acho s (\u2228\u2192 ind) lind \u00acord x = {!!}\next\u21d2\u00acho s (ind \u2190\u2202) lind \u00acord x = {!!}\next\u21d2\u00acho s (\u2202\u2192 ind) lind \u00acord x = {!!}\n\ngoo : \u2200{i u rll pll ll tll} \u2192 (lind : IndexLL {i} {u} rll ll) \u2192 (s : SetLL ll) \u2192 \u2200{rs : SetLL tll}\n \u2192 \u00ac (hitsAtLeastOnce s lind) \u2192 (ind : IndexLL pll ll)\n \u2192 (nord : \u00ac Ordered\u1d62 lind ind)\n \u2192 \u00ac (hitsAtLeastOnce (replacePartOf s to rs at ind) (\u00acord-morph lind ind tll (flipNotOrd\u1d62 nord)))\ngoo \u2193 s \u00acho ind \u00acord = \u03bb _ \u2192 \u00acord (a\u2264\u1d62b \u2264\u1d62\u2193)\ngoo (lind \u2190\u2227) \u2193 \u00acho ind \u00acord = \u03bb _ \u2192 \u00acho hitsAtLeastOnce\u2193\ngoo (lind \u2190\u2227) (s \u2190\u2227) \u00acho \u2193 \u00acord = \u03bb _ \u2192 \u00acord (b\u2264\u1d62a \u2264\u1d62\u2193)\ngoo (lind \u2190\u2227) (s \u2190\u2227) {rs} \u00acho (ind \u2190\u2227) \u00acord x\n with goo lind s {rs} (\u03bb z \u2192 \u00acho (hitsAtLeastOnce\u2190\u2227\u2190\u2227 z)) ind hf where\n hf : \u00ac Ordered\u1d62 lind ind\n hf (a\u2264\u1d62b x) = \u00acord (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 x))\n hf (b\u2264\u1d62a x) = \u00acord (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 x))\ngoo (lind \u2190\u2227) (s \u2190\u2227) {rs} \u00acho (ind \u2190\u2227) \u00acord (hitsAtLeastOnce\u2190\u2227\u2190\u2227 x) | r = \u22a5-elim (r x)\ngoo (lind \u2190\u2227) (s \u2190\u2227) \u00acho (\u2227\u2192 ind) \u00acord (hitsAtLeastOnce\u2190\u2227\u2192\u2190\u2227 x) = \u00acho (hitsAtLeastOnce\u2190\u2227\u2190\u2227 x)\ngoo (lind \u2190\u2227) (\u2227\u2192 s) \u00acho \u2193 \u00acord = \u03bb _ \u2192 \u00acord (b\u2264\u1d62a \u2264\u1d62\u2193)\ngoo (lind \u2190\u2227) (\u2227\u2192 s) \u00acho (ind \u2190\u2227) \u00acord = {!!}\ngoo (lind \u2190\u2227) (\u2227\u2192 s) \u00acho (\u2227\u2192 ind) \u00acord = \u03bb ()\ngoo (lind \u2190\u2227) (s \u2190\u2227\u2192 s\u2081) \u00acho ind \u00acord = {!!}\ngoo (\u2227\u2192 lind) s \u00acho ind \u00acord = {!!}\ngoo (lind \u2190\u2228) s \u00acho ind \u00acord = {!!}\ngoo (\u2228\u2192 lind) s \u00acho ind \u00acord = {!!}\ngoo (lind \u2190\u2202) s \u00acho ind \u00acord = {!!}\ngoo (\u2202\u2192 lind) s \u00acho ind \u00acord = {!!}\n\n\n\n\ngest : \u2200{i u rll ll n dt df tind ts} \u2192 (lf : LFun ll rll)\n \u2192 (ind : IndexLL (\u03c4 {i} {u} {n} {dt} df) ll) \u2192 (s : SetLL ll) \u2192 \u00ac (hitsAtLeastOnce s ind)\n \u2192 \u00ac\u2205 tind \u2261 tranLFMIndexLL lf (\u00ac\u2205 ind) \u2192 \u00ac\u2205 ts \u2261 tranLFMSetLL lf (\u00ac\u2205 s)\n \u2192 \u00ac (hitsAtLeastOnce ts tind) \ngest I ind s \u00acho refl refl = \u00acho\ngest (_\u2282_ {ind = lind} lf lf\u2081) ind s \u00acho eqi eqs with isOrd\u1d62 lind ind\n... | no \u00acp = {!!} where\n is = gest lf {!!} {!!} -- (\u00ac\u2205 (truncSetLL s ind))\n... | yes p = {!!}\ngest {tind = tind} {ts} (tr ltr lf) ind s \u00acho eqi eqs = gest lf (IndexLLProp.tran ind ltr ut) (SetLL.tran s ltr) \u00actho eqi eqs where\n ut = indLow\u21d2UpTran ind ltr \n \u00actho = \u00actrho ltr s ind \u00acho ut\ngest (com df\u2081 lf) ind s \u00acho () eqs\ngest (call x) ind s \u00acho () eqs\n\n\n\n\nmodule _ where\n\n \n\n private\n data M\u00acho {i u ll n dt df} (s : SetLL ll) : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll \u2192 Set where\n \u2205 : M\u00acho s \u2205\n \u00ac\u2205 : {ind : IndexLL (\u03c4 {i} {u} {n} {dt} df) ll} \u2192 (\u00acho : \u00ac (hitsAtLeastOnce s ind))\n \u2192 M\u00acho s (\u00ac\u2205 ind)\n\n hf : \u2200{i u n dt df} \u2192 \u2200 ll \u2192 \u2200{cs} \u2192 (ind : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll)\n \u2192 (s : SetLL ll) \u2192 (ceq : complL\u209b s \u2261 \u00ac\u2205 cs) \u2192 (m\u00acho : M\u00acho s ind) \u2192 LinLogic i {u}\n \u2192 LinLogic i {u}\n hf ll {cs} \u2205 s ceq mnho cll = shrinkcms ll s cs ceq\n hf ll {cs} (\u00ac\u2205 x) s ceq (\u00ac\u2205 \u00acho) cll = replLL (shrinkcms ll s cs ceq) (\u00acho-shr-morph s ceq x \u00acho) cll\n-- (shrinkcms ll s cs ceqi)\n data MLFun {i u ll rll n dt df} (ind : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll)\n (s : SetLL ll) (m\u00acho : M\u00acho s ind) (lf : LFun {i} {u} ll rll) : Set (lsuc u) where\n \u2205 : MLFun ind s m\u00acho lf\n \u00ac\u2205\u00ac\u2205 : \u2200 {cs ts cts} \u2192 (ceqi : complL\u209b s \u2261 \u00ac\u2205 cs)\n \u2192 let tind = tranLFMIndexLL lf ind in\n \u00ac\u2205 ts \u2261 tranLFMSetLL lf (\u00ac\u2205 s) \u2192 (ceqo : complL\u209b ts \u2261 \u00ac\u2205 cts)\n \u2192 ((cll : LinLogic i {u}) \u2192 LFun (hf ll ind s ceqi m\u00acho cll) (hf rll tind ts ceqo {!!} cll))\n \u2192 MLFun ind s m\u00acho lf\n -- We will never reach to a point where complL\u209b ts \u2261 \u2205 because\n -- the input would have the same fate. ( s becomes smaller as we go forward, thus complL\u209b increases. Here we take the case where s is not \u2205.\n \n \n -- s here does contain the ind.\n test : \u2200{i u rll ll n dt df} \u2192 (ind : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll) \u2192 (s : SetLL ll)\n \u2192 \u2200 m\u00acho \u2192 (lf : LFun ll rll) \u2192 MLFun ind s m\u00acho lf\n test ind s lf = {!!}\n \n \n \n \n \n \n \n","old_contents":"module LinFunContructw where\n\nopen import Common\nopen import LinLogic\nopen import IndexLLProp\nopen import LinFun\nopen import SetLL\nopen import SetLLProp\n\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Product\n\nopen import LinFunContruct\n\next\u21d2\u00acho : \u2200{i u pll rll ll} \u2192 \u2200 s \u2192 (ind : IndexLL {i} {u} pll ll) \u2192 (lind : IndexLL rll ll)\n \u2192 \u00ac Ordered\u1d62 lind ind \u2192 \u00ac hitsAtLeastOnce (extend ind s) lind\next\u21d2\u00acho s \u2193 lind \u00acord x = \u00acord (b\u2264\u1d62a \u2264\u1d62\u2193)\next\u21d2\u00acho s (ind \u2190\u2227) \u2193 \u00acord x = \u00acord (a\u2264\u1d62b \u2264\u1d62\u2193)\next\u21d2\u00acho {pll = pll} {_} {ll = li \u2227 _} s (ind \u2190\u2227) (lind \u2190\u2227) \u00acord x\n with replLL li ind pll | replLL-id li ind pll refl | extendg ind s | ext\u21d2\u00acho s ind lind hf where\n hf : \u00ac Ordered\u1d62 lind ind\n hf (a\u2264\u1d62b x\u2081) = \u00acord (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 x\u2081))\n hf (b\u2264\u1d62a x\u2081) = \u00acord (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 x\u2081))\next\u21d2\u00acho {pll = pll} {_} {li \u2227 _} s (ind \u2190\u2227) (lind \u2190\u2227) \u00acord x | .li | refl | t | e = {!!}\next\u21d2\u00acho {pll = pll} {_} {ll = li \u2227 _} s (ind \u2190\u2227) (\u2227\u2192 lind) \u00acord x with replLL li ind pll | replLL-id li ind pll refl | extendg ind s\next\u21d2\u00acho {_} {_} {pll} {_} {li \u2227 _} s (ind \u2190\u2227) (\u2227\u2192 lind) \u00acord () | .li | refl | t\next\u21d2\u00acho s (\u2227\u2192 ind) \u2193 \u00acord x = \u00acord (a\u2264\u1d62b \u2264\u1d62\u2193)\next\u21d2\u00acho s (\u2227\u2192 ind) (lind \u2190\u2227) \u00acord x = {!!}\next\u21d2\u00acho s (\u2227\u2192 ind) (\u2227\u2192 lind) \u00acord x = {!!}\next\u21d2\u00acho s (ind \u2190\u2228) lind \u00acord x = {!!}\next\u21d2\u00acho s (\u2228\u2192 ind) lind \u00acord x = {!!}\next\u21d2\u00acho s (ind \u2190\u2202) lind \u00acord x = {!!}\next\u21d2\u00acho s (\u2202\u2192 ind) lind \u00acord x = {!!}\n\ngoo : \u2200{i u rll pll ll tll} \u2192 (lind : IndexLL {i} {u} rll ll) \u2192 (s : SetLL ll) \u2192 \u2200{rs : SetLL tll}\n \u2192 \u00ac (hitsAtLeastOnce s lind) \u2192 (ind : IndexLL pll ll)\n \u2192 (nord : \u00ac Ordered\u1d62 lind ind)\n \u2192 \u00ac (hitsAtLeastOnce (replacePartOf s to rs at ind) (\u00acord-morph lind ind tll (flipNotOrd\u1d62 nord)))\ngoo \u2193 s \u00acho ind \u00acord = \u03bb _ \u2192 \u00acord (a\u2264\u1d62b \u2264\u1d62\u2193)\ngoo (lind \u2190\u2227) \u2193 \u00acho ind \u00acord = \u03bb _ \u2192 \u00acho hitsAtLeastOnce\u2193\ngoo (lind \u2190\u2227) (s \u2190\u2227) \u00acho \u2193 \u00acord = \u03bb _ \u2192 \u00acord (b\u2264\u1d62a \u2264\u1d62\u2193)\ngoo (lind \u2190\u2227) (s \u2190\u2227) {rs} \u00acho (ind \u2190\u2227) \u00acord x\n with goo lind s {rs} (\u03bb z \u2192 \u00acho (hitsAtLeastOnce\u2190\u2227\u2190\u2227 z)) ind hf where\n hf : \u00ac Ordered\u1d62 lind ind\n hf (a\u2264\u1d62b x) = \u00acord (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 x))\n hf (b\u2264\u1d62a x) = \u00acord (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 x))\ngoo (lind \u2190\u2227) (s \u2190\u2227) {rs} \u00acho (ind \u2190\u2227) \u00acord (hitsAtLeastOnce\u2190\u2227\u2190\u2227 x) | r = \u22a5-elim (r x)\ngoo (lind \u2190\u2227) (s \u2190\u2227) \u00acho (\u2227\u2192 ind) \u00acord (hitsAtLeastOnce\u2190\u2227\u2192\u2190\u2227 x) = \u00acho (hitsAtLeastOnce\u2190\u2227\u2190\u2227 x)\ngoo (lind \u2190\u2227) (\u2227\u2192 s) \u00acho \u2193 \u00acord = \u03bb _ \u2192 \u00acord (b\u2264\u1d62a \u2264\u1d62\u2193)\ngoo (lind \u2190\u2227) (\u2227\u2192 s) \u00acho (ind \u2190\u2227) \u00acord = {!!}\ngoo (lind \u2190\u2227) (\u2227\u2192 s) \u00acho (\u2227\u2192 ind) \u00acord = \u03bb ()\ngoo (lind \u2190\u2227) (s \u2190\u2227\u2192 s\u2081) \u00acho ind \u00acord = {!!}\ngoo (\u2227\u2192 lind) s \u00acho ind \u00acord = {!!}\ngoo (lind \u2190\u2228) s \u00acho ind \u00acord = {!!}\ngoo (\u2228\u2192 lind) s \u00acho ind \u00acord = {!!}\ngoo (lind \u2190\u2202) s \u00acho ind \u00acord = {!!}\ngoo (\u2202\u2192 lind) s \u00acho ind \u00acord = {!!}\n\n\n\n\ngest : \u2200{i u rll ll n dt df tind ts} \u2192 (lf : LFun ll rll)\n \u2192 (ind : IndexLL (\u03c4 {i} {u} {n} {dt} df) ll) \u2192 (s : SetLL ll) \u2192 \u00ac (hitsAtLeastOnce s ind)\n \u2192 \u00ac\u2205 tind \u2261 tranLFMIndexLL lf (\u00ac\u2205 ind) \u2192 \u00ac\u2205 ts \u2261 tranLFMSetLL lf (\u00ac\u2205 s)\n \u2192 \u00ac (hitsAtLeastOnce ts tind) \ngest I ind s \u00acho refl refl = \u00acho\ngest (_\u2282_ {ind = lind} lf lf\u2081) ind s \u00acho eqi eqs with isOrd\u1d62 lind ind\n... | no \u00acp = {!!} where\n is = gest lf {!!} {!!} -- (\u00ac\u2205 (truncSetLL s ind))\n... | yes p = {!!}\ngest {tind = tind} {ts} (tr ltr lf) ind s \u00acho eqi eqs = gest lf (IndexLLProp.tran ind ltr ut) (SetLL.tran s ltr) \u00actho eqi eqs where\n ut = indLow\u21d2UpTran ind ltr \n \u00actho = \u00actrho ltr s ind \u00acho ut\ngest (com df\u2081 lf) ind s \u00acho () eqs\ngest (call x) ind s \u00acho () eqs\n\n\n\n\nmodule _ where\n\n \n\n private\n data M\u00acho {i u ll n dt df} (s : SetLL ll) : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll \u2192 Set where\n \u2205 : M\u00acho s \u2205\n \u00ac\u2205 : {ind : IndexLL (\u03c4 {i} {u} {n} {dt} df) ll} \u2192 (\u00acho : \u00ac (hitsAtLeastOnce s ind))\n \u2192 M\u00acho s (\u00ac\u2205 ind)\n\n hf : \u2200{i u n dt df} \u2192 \u2200 ll \u2192 \u2200{cs} \u2192 (ind : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll)\n \u2192 (s : SetLL ll) \u2192 (ceq : complL\u209b s \u2261 \u00ac\u2205 cs) \u2192 (m\u00acho : M\u00acho s ind) \u2192 LinLogic i {u}\n \u2192 LinLogic i {u}\n hf ll {cs} \u2205 s ceq mnho cll = shrinkcms ll s cs ceq\n hf ll {cs} (\u00ac\u2205 x) s ceq (\u00ac\u2205 \u00acho) cll = replLL (shrinkcms ll s cs ceq) (\u00acho-shr-morph s ceq x \u00acho) cll\n-- (shrinkcms ll s cs ceqi)\n data MLFun {i u ll rll n dt df} (ind : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll)\n (s : SetLL ll) (m\u00acho : M\u00acho s ind) (lf : LFun {i} {u} ll rll) : Set (lsuc u) where\n \u2205 : MLFun ind s m\u00acho lf\n \u00ac\u2205\u00ac\u2205 : \u2200 {cs ts cts} \u2192 (ceqi : complL\u209b s \u2261 \u00ac\u2205 cs)\n \u2192 let tind = tranLFMIndexLL lf ind in\n \u00ac\u2205 ts \u2261 tranLFMSetLL lf (\u00ac\u2205 s) \u2192 (ceqo : complL\u209b ts \u2261 \u00ac\u2205 cts)\n \u2192 ((cll : LinLogic i {u}) \u2192 LFun (hf ll ind s ceqi m\u00acho cll) (hf rll tind ts ceqo {!!} cll))\n \u2192 MLFun ind s m\u00acho lf\n -- We will never reach to a point where complL\u209b ts \u2261 \u2205 because\n -- the input would have the same fate. ( s becomes smaller as we go forward, thus complL\u209b increases. Here we take the case where s is not \u2205.\n \n \n -- s here does contain the ind.\n test : \u2200{i u rll ll n dt df} \u2192 (ind : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll) \u2192 (s : SetLL ll)\n \u2192 \u2200 m\u00acho \u2192 (lf : LFun ll rll) \u2192 MLFun ind s m\u00acho lf\n test ind s lf = {!!}\n \n \n \n \n \n \n \n","returncode":0,"stderr":"","license":"mpl-2.0","lang":"Agda"} {"commit":"a6bbe5d2b2206e1090e8303580a73956ec707683","subject":"Add bot to primitives.","message":"Add bot to primitives.\n\nConsistency at last.\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/Examples\/DarkwingDuck\/Primitive.agda","new_file":"formalization\/agda\/Spire\/Examples\/DarkwingDuck\/Primitive.agda","new_contents":"module Spire.Examples.DarkwingDuck.Primitive where\n\n----------------------------------------------------------------------\n\ninfixr 4 _,_\ninfixr 5 _\u2237_\n\n----------------------------------------------------------------------\n\npostulate String : Set\n{-# BUILTIN STRING String #-}\n\n----------------------------------------------------------------------\n\ndata \u22a5 : Set where\n\nelimBot : (P : \u22a5 \u2192 Set)\n (v : \u22a5) \u2192 P v\nelimBot P ()\n\n----------------------------------------------------------------------\n\ndata \u22a4 : Set where\n tt : \u22a4\n\nelimUnit : (P : \u22a4 \u2192 Set)\n (ptt : P tt)\n (u : \u22a4) \u2192 P u\nelimUnit P ptt tt = ptt\n\n----------------------------------------------------------------------\n\ndata \u03a3 (A : Set) (B : A \u2192 Set) : Set where\n _,_ : (a : A) (b : B a) \u2192 \u03a3 A B\n\nelimPair : {A : Set} {B : A \u2192 Set}\n (P : \u03a3 A B \u2192 Set)\n (ppair : (a : A) (b : B a) \u2192 P (a , b))\n (ab : \u03a3 A B) \u2192 P ab\nelimPair P ppair (a , b) = ppair a b\n\n----------------------------------------------------------------------\n\ndata _\u2261_ {A : Set} (x : A) : A \u2192 Set where\n refl : x \u2261 x\n\nelimEq : {A : Set} {x : A} (P : (y : A) \u2192 x \u2261 y \u2192 Set)\n (prefl : P x refl)\n (y : A) (q : x \u2261 y) \u2192 P y q\nelimEq P prefl x refl = prefl\n\n----------------------------------------------------------------------\n\ndata List (A : Set) : Set where\n [] : List A\n _\u2237_ : (x : A) (xs : List A) \u2192 List A\n\nelimList : {A : Set} (P : List A \u2192 Set)\n (pnil : P [])\n (pcons : (x : A) (xs : List A) \u2192 P xs \u2192 P (x \u2237 xs))\n (xs : List A) \u2192 P xs\nelimList P pnil pcons [] = pnil\nelimList P pnil pcons (x \u2237 xs) = pcons x xs (elimList P pnil pcons xs)\n\n----------------------------------------------------------------------\n\ndata Point (A : Set) : List A \u2192 Set where\n here : \u2200{x xs} \u2192 Point A (x \u2237 xs)\n there : \u2200{x xs} \u2192 Point A xs \u2192 Point A (x \u2237 xs)\n\nelimPoint : {A : Set} (P : (xs : List A) \u2192 Point A xs \u2192 Set)\n (phere : (x : A) (xs : List A) \u2192 P (x \u2237 xs) here)\n (pthere : (x : A) (xs : List A) (t : Point A xs) \u2192 P xs t \u2192 P (x \u2237 xs) (there t))\n (xs : List A) (t : Point A xs) \u2192 P xs t\nelimPoint P phere pthere (x \u2237 xs) here = phere x xs\nelimPoint P phere pthere (x \u2237 xs) (there t) = pthere x xs t (elimPoint P phere pthere xs t)\n\n----------------------------------------------------------------------\n\ndata Tel : Set\u2081 where\n End : Tel\n Arg : (A : Set) (B : A \u2192 Tel) \u2192 Tel\n\nelimTel : (P : Tel \u2192 Set)\n (pend : P End)\n (parg : (A : Set) (B : A \u2192 Tel) (pb : (a : A) \u2192 P (B a)) \u2192 P (Arg A B))\n (T : Tel) \u2192 P T\nelimTel P pend parg End = pend\nelimTel P pend parg (Arg A B) = parg A B (\u03bb a \u2192 elimTel P pend parg (B a))\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n End : (i : I) \u2192 Desc I\n Rec : (i : I) (D : Desc I) \u2192 Desc I\n Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n\nelimDesc : {I : Set} (P : Desc I \u2192 Set)\n (pend : (i : I) \u2192 P (End i))\n (prec : (i : I) (D : Desc I) (pd : P D) \u2192 P (Rec i D))\n (parg : (A : Set) (B : A \u2192 Desc I) (pb : (a : A) \u2192 P (B a)) \u2192 P (Arg A B))\n (D : Desc I) \u2192 P D\nelimDesc P pend prec parg (End i) = pend i\nelimDesc P pend prec parg (Rec i D) = prec i D (elimDesc P pend prec parg D)\nelimDesc P pend prec parg (Arg A B) = parg A B (\u03bb a \u2192 elimDesc P pend prec parg (B a))\n\nEl\u1d30 : {I : Set} (D : Desc I) \u2192 (I \u2192 Set) \u2192 I \u2192 Set\nEl\u1d30 (End j) X i = j \u2261 i\nEl\u1d30 (Rec j D) X i = \u03a3 (X j) (\u03bb _ \u2192 El\u1d30 D X i)\nEl\u1d30 (Arg A B) X i = \u03a3 A (\u03bb a \u2192 El\u1d30 (B a) X i)\n\nHyps : {I : Set} (D : Desc I) (X : I \u2192 Set) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El\u1d30 D X i) \u2192 Set\nHyps (End j) X P i q = \u22a4\nHyps (Rec j D) X P i (x , xs) = \u03a3 (P j x) (\u03bb _ \u2192 Hyps D X P i xs)\nHyps (Arg A B) X P i (a , b) = Hyps (B a) X P i b\n\n----------------------------------------------------------------------\n\ndata \u03bc {I : Set} (D : Desc I) (i : I) : Set where\n init : El\u1d30 D (\u03bc D) i \u2192 \u03bc D i\n\nind : {I : Set} (D : Desc I)\n (M : (i : I) \u2192 \u03bc D i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 D (\u03bc D) i) (ihs : Hyps D (\u03bc D) M i xs) \u2192 M i (init xs))\n (i : I)\n (x : \u03bc D i)\n \u2192 M i x\n\nprove : {I : Set} (D E : Desc I)\n (M : (i : I) \u2192 \u03bc E i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 E (\u03bc E) i) (ihs : Hyps E (\u03bc E) M i xs) \u2192 M i (init xs))\n (i : I) (xs : El\u1d30 D (\u03bc E) i) \u2192 Hyps D (\u03bc E) M i xs\n\nind D M \u03b1 i (init xs) = \u03b1 i xs (prove D D M \u03b1 i xs)\n\nprove (End j) E M \u03b1 i q = tt\nprove (Rec j D) E M \u03b1 i (x , xs) = ind E M \u03b1 j x , prove D E M \u03b1 i xs\nprove (Arg A B) E M \u03b1 i (a , xs) = prove (B a) E M \u03b1 i xs\n\n----------------------------------------------------------------------\n","old_contents":"module Spire.Examples.DarkwingDuck.Primitive where\n\n----------------------------------------------------------------------\n\ninfixr 4 _,_\ninfixr 5 _\u2237_\n\n----------------------------------------------------------------------\n\npostulate String : Set\n{-# BUILTIN STRING String #-}\n\n----------------------------------------------------------------------\n\ndata \u22a4 : Set where\n tt : \u22a4\n\nelimUnit : (P : \u22a4 \u2192 Set)\n (ptt : P tt)\n (u : \u22a4) \u2192 P u\nelimUnit P ptt tt = ptt\n\n----------------------------------------------------------------------\n\ndata \u03a3 (A : Set) (B : A \u2192 Set) : Set where\n _,_ : (a : A) (b : B a) \u2192 \u03a3 A B\n\nelimPair : {A : Set} {B : A \u2192 Set}\n (P : \u03a3 A B \u2192 Set)\n (ppair : (a : A) (b : B a) \u2192 P (a , b))\n (ab : \u03a3 A B) \u2192 P ab\nelimPair P ppair (a , b) = ppair a b\n\n----------------------------------------------------------------------\n\ndata _\u2261_ {A : Set} (x : A) : A \u2192 Set where\n refl : x \u2261 x\n\nelimEq : {A : Set} {x : A} (P : (y : A) \u2192 x \u2261 y \u2192 Set)\n (prefl : P x refl)\n (y : A) (q : x \u2261 y) \u2192 P y q\nelimEq P prefl x refl = prefl\n\n----------------------------------------------------------------------\n\ndata List (A : Set) : Set where\n [] : List A\n _\u2237_ : (x : A) (xs : List A) \u2192 List A\n\nelimList : {A : Set} (P : List A \u2192 Set)\n (pnil : P [])\n (pcons : (x : A) (xs : List A) \u2192 P xs \u2192 P (x \u2237 xs))\n (xs : List A) \u2192 P xs\nelimList P pnil pcons [] = pnil\nelimList P pnil pcons (x \u2237 xs) = pcons x xs (elimList P pnil pcons xs)\n\n----------------------------------------------------------------------\n\ndata Point (A : Set) : List A \u2192 Set where\n here : \u2200{x xs} \u2192 Point A (x \u2237 xs)\n there : \u2200{x xs} \u2192 Point A xs \u2192 Point A (x \u2237 xs)\n\nelimPoint : {A : Set} (P : (xs : List A) \u2192 Point A xs \u2192 Set)\n (phere : (x : A) (xs : List A) \u2192 P (x \u2237 xs) here)\n (pthere : (x : A) (xs : List A) (t : Point A xs) \u2192 P xs t \u2192 P (x \u2237 xs) (there t))\n (xs : List A) (t : Point A xs) \u2192 P xs t\nelimPoint P phere pthere (x \u2237 xs) here = phere x xs\nelimPoint P phere pthere (x \u2237 xs) (there t) = pthere x xs t (elimPoint P phere pthere xs t)\n\n----------------------------------------------------------------------\n\ndata Tel : Set\u2081 where\n End : Tel\n Arg : (A : Set) (B : A \u2192 Tel) \u2192 Tel\n\nelimTel : (P : Tel \u2192 Set)\n (pend : P End)\n (parg : (A : Set) (B : A \u2192 Tel) (pb : (a : A) \u2192 P (B a)) \u2192 P (Arg A B))\n (T : Tel) \u2192 P T\nelimTel P pend parg End = pend\nelimTel P pend parg (Arg A B) = parg A B (\u03bb a \u2192 elimTel P pend parg (B a))\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set\u2081 where\n End : (i : I) \u2192 Desc I\n Rec : (i : I) (D : Desc I) \u2192 Desc I\n Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n\nelimDesc : {I : Set} (P : Desc I \u2192 Set)\n (pend : (i : I) \u2192 P (End i))\n (prec : (i : I) (D : Desc I) (pd : P D) \u2192 P (Rec i D))\n (parg : (A : Set) (B : A \u2192 Desc I) (pb : (a : A) \u2192 P (B a)) \u2192 P (Arg A B))\n (D : Desc I) \u2192 P D\nelimDesc P pend prec parg (End i) = pend i\nelimDesc P pend prec parg (Rec i D) = prec i D (elimDesc P pend prec parg D)\nelimDesc P pend prec parg (Arg A B) = parg A B (\u03bb a \u2192 elimDesc P pend prec parg (B a))\n\nEl\u1d30 : {I : Set} (D : Desc I) \u2192 (I \u2192 Set) \u2192 I \u2192 Set\nEl\u1d30 (End j) X i = j \u2261 i\nEl\u1d30 (Rec j D) X i = \u03a3 (X j) (\u03bb _ \u2192 El\u1d30 D X i)\nEl\u1d30 (Arg A B) X i = \u03a3 A (\u03bb a \u2192 El\u1d30 (B a) X i)\n\nHyps : {I : Set} (D : Desc I) (X : I \u2192 Set) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El\u1d30 D X i) \u2192 Set\nHyps (End j) X P i q = \u22a4\nHyps (Rec j D) X P i (x , xs) = \u03a3 (P j x) (\u03bb _ \u2192 Hyps D X P i xs)\nHyps (Arg A B) X P i (a , b) = Hyps (B a) X P i b\n\n----------------------------------------------------------------------\n\ndata \u03bc {I : Set} (D : Desc I) (i : I) : Set where\n init : El\u1d30 D (\u03bc D) i \u2192 \u03bc D i\n\nind : {I : Set} (D : Desc I)\n (M : (i : I) \u2192 \u03bc D i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 D (\u03bc D) i) (ihs : Hyps D (\u03bc D) M i xs) \u2192 M i (init xs))\n (i : I)\n (x : \u03bc D i)\n \u2192 M i x\n\nprove : {I : Set} (D E : Desc I)\n (M : (i : I) \u2192 \u03bc E i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 E (\u03bc E) i) (ihs : Hyps E (\u03bc E) M i xs) \u2192 M i (init xs))\n (i : I) (xs : El\u1d30 D (\u03bc E) i) \u2192 Hyps D (\u03bc E) M i xs\n\nind D M \u03b1 i (init xs) = \u03b1 i xs (prove D D M \u03b1 i xs)\n\nprove (End j) E M \u03b1 i q = tt\nprove (Rec j D) E M \u03b1 i (x , xs) = ind E M \u03b1 j x , prove D E M \u03b1 i xs\nprove (Arg A B) E M \u03b1 i (a , xs) = prove (B a) E M \u03b1 i xs\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"d43fb2f89ef473dccaa4c9fcc455c8cbc9963bd5","subject":"Identify new Agda substitution bug","message":"Identify new Agda substitution bug\n","repos":"inc-lc\/ilc-agda","old_file":"Nehemiah\/Syntax\/Term.agda","new_file":"Nehemiah\/Syntax\/Term.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- The syntax of terms with the Nehemiah plugin.\n------------------------------------------------------------------------\n\nmodule Nehemiah.Syntax.Term where\n\nopen import Nehemiah.Syntax.Type\n\nopen import Data.Integer\n\nimport Parametric.Syntax.Term Base as Term\n\ndata Const : Term.Structure where\n intlit-const : (n : \u2124) \u2192 Const \u2205 int\n add-const : Const (int \u2022 int \u2022 \u2205) int\n minus-const : Const (int \u2022 \u2205) (int)\n\n empty-const : Const \u2205 (bag)\n insert-const : Const (int \u2022 bag \u2022 \u2205) (bag)\n union-const : Const (bag \u2022 bag \u2022 \u2205) (bag)\n negate-const : Const (bag \u2022 \u2205) (bag)\n\n flatmap-const : Const ((int \u21d2 bag) \u2022 bag \u2022 \u2205) (bag)\n sum-const : Const (bag \u2022 \u2205) (int)\n\n\n--open Term.Structure --works\nopen Term.Structure Const --fails, even with the rest disabled.\n{-\nAn internal error has occurred. Please report this as a bug.\nLocation of the error: src\/full\/Agda\/TypeChecking\/Substitute.hs:183\n-}\n\n{-\n-- Shorthands of constants\n\npattern intlit n = const (intlit-const n) \u2205\npattern add s t = const add-const (s \u2022 t \u2022 \u2205)\npattern minus t = const minus-const (t \u2022 \u2205)\npattern empty = const empty-const \u2205\npattern insert s t = const insert-const (s \u2022 t \u2022 \u2205)\npattern union s t = const union-const (s \u2022 t \u2022 \u2205)\npattern negate t = const negate-const (t \u2022 \u2205)\npattern flatmap s t = const flatmap-const (s \u2022 t \u2022 \u2205)\npattern sum t = const sum-const (t \u2022 \u2205)\n-}\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- The syntax of terms with the Nehemiah plugin.\n------------------------------------------------------------------------\n\nmodule Nehemiah.Syntax.Term where\n\nopen import Nehemiah.Syntax.Type\n\nopen import Data.Integer\n\nimport Parametric.Syntax.Term Base as Term\n\ndata Const : Term.Structure where\n intlit-const : (n : \u2124) \u2192 Const \u2205 int\n add-const : Const (int \u2022 int \u2022 \u2205) int\n minus-const : Const (int \u2022 \u2205) (int)\n\n empty-const : Const \u2205 (bag)\n insert-const : Const (int \u2022 bag \u2022 \u2205) (bag)\n union-const : Const (bag \u2022 bag \u2022 \u2205) (bag)\n negate-const : Const (bag \u2022 \u2205) (bag)\n\n flatmap-const : Const ((int \u21d2 bag) \u2022 bag \u2022 \u2205) (bag)\n sum-const : Const (bag \u2022 \u2205) (int)\n\nopen Term.Structure Const public\n\n-- Shorthands of constants\n\npattern intlit n = const (intlit-const n) \u2205\npattern add s t = const add-const (s \u2022 t \u2022 \u2205)\npattern minus t = const minus-const (t \u2022 \u2205)\npattern empty = const empty-const \u2205\npattern insert s t = const insert-const (s \u2022 t \u2022 \u2205)\npattern union s t = const union-const (s \u2022 t \u2022 \u2205)\npattern negate t = const negate-const (t \u2022 \u2205)\npattern flatmap s t = const flatmap-const (s \u2022 t \u2022 \u2205)\npattern sum t = const sum-const (t \u2022 \u2205)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f78289d4953f689d6d6a0b6d65d36254dc5118e2","subject":"Added map-++.","message":"Added map-++.\n\nIgnore-this: 58116847ac82637c3f4b08d6251c2b1b\n\ndarcs-hash:20110206231146-3bd4e-97dc9840fff42257313c5a2a804d80c41a5d0dea.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/LTC\/Data\/List\/PropertiesI.agda","new_file":"src\/LTC\/Data\/List\/PropertiesI.agda","new_contents":"------------------------------------------------------------------------------\n-- Properties related with lists\n------------------------------------------------------------------------------\n\nmodule LTC.Data.List.PropertiesI where\n\nopen import LTC.Base\n\nopen import Common.Function\n\nopen import LTC.Data.List\nopen import LTC.Data.List.Type\n\nopen import LTC.Relation.Binary.EqReasoning\n\n------------------------------------------------------------------------------\n\n++-List : \u2200 {xs ys} \u2192 List xs \u2192 List ys \u2192 List (xs ++ ys)\n++-List {ys = ys} nilL Lys = subst List (sym (++-[] ys)) Lys\n++-List {ys = ys} (consL x {xs} Lxs) Lys =\n subst List (sym (++-\u2237 x xs ys)) (consL x (++-List Lxs Lys))\n\nmap-List : \u2200 {xs} f \u2192 List xs \u2192 List (map f xs)\nmap-List f nilL = subst List (sym (map-[] f)) nilL\nmap-List f (consL x {xs} Lxs) =\n subst List (sym (map-\u2237 f x xs)) (consL (f \u00b7 x) (map-List f Lxs))\n\nrev-List : \u2200 {xs ys} \u2192 List xs \u2192 List ys \u2192 List (rev xs ys)\nrev-List {ys = ys} nilL Lys = subst List (sym (rev-[] ys)) Lys\nrev-List {ys = ys} (consL x {xs} Lxs) Lys =\n subst List (sym (rev-\u2237 x xs ys)) (rev-List Lxs (consL x Lys))\n\n++-leftIdentity : \u2200 {xs} \u2192 List xs \u2192 [] ++ xs \u2261 xs\n++-leftIdentity {xs} _ = ++-[] xs\n\n++-rightIdentity : \u2200 {xs} \u2192 List xs \u2192 xs ++ [] \u2261 xs\n++-rightIdentity nilL = ++-[] []\n++-rightIdentity (consL x {xs} Lxs) =\n begin\n (x \u2237 xs) ++ []\n \u2261\u27e8 ++-\u2237 x xs [] \u27e9\n x \u2237 (xs ++ [])\n \u2261\u27e8 subst (\u03bb t \u2192 x \u2237 (xs ++ []) \u2261 x \u2237 t)\n (++-rightIdentity Lxs)\n refl\n \u27e9\n x \u2237 xs\n \u220e\n\n++-assoc : \u2200 {xs ys zs} \u2192 List xs \u2192 List ys \u2192 List zs \u2192\n (xs ++ ys) ++ zs \u2261 xs ++ (ys ++ zs)\n++-assoc {ys = ys} {zs} nilL Lys Lzs =\n begin\n ([] ++ ys) ++ zs\n \u2261\u27e8 subst (\u03bb t \u2192 ([] ++ ys) ++ zs \u2261 t ++ zs)\n (++-[] ys)\n refl\n \u27e9\n ys ++ zs\n \u2261\u27e8 sym (++-leftIdentity (++-List Lys Lzs)) \u27e9\n [] ++ ys ++ zs\n \u220e\n\n++-assoc {ys = ys} {zs} (consL x {xs} Lxs) Lys Lzs =\n begin\n ((x \u2237 xs) ++ ys) ++ zs\n \u2261\u27e8 subst (\u03bb t \u2192 ((x \u2237 xs) ++ ys) ++ zs \u2261 t ++ zs)\n (++-\u2237 x xs ys)\n refl\n \u27e9\n (x \u2237 (xs ++ ys)) ++ zs\n \u2261\u27e8 ++-\u2237 x (xs ++ ys) zs \u27e9\n x \u2237 ((xs ++ ys) ++ zs)\n \u2261\u27e8 subst (\u03bb t \u2192 x \u2237 ((xs ++ ys) ++ zs) \u2261 x \u2237 t)\n (++-assoc Lxs Lys Lzs) -- IH.\n refl\n \u27e9\n x \u2237 (xs ++ ys ++ zs)\n \u2261\u27e8 sym (++-\u2237 x xs (ys ++ zs)) \u27e9\n (x \u2237 xs) ++ ys ++ zs\n \u220e\n\nrev-++ : \u2200 {xs ys} \u2192 List xs \u2192 List ys \u2192 rev xs ys \u2261 rev xs [] ++ ys\nrev-++ {ys = ys} nilL Lys =\n begin\n rev [] ys \u2261\u27e8 rev-[] ys \u27e9\n ys \u2261\u27e8 sym $ ++-leftIdentity Lys \u27e9\n [] ++ ys \u2261\u27e8 subst (\u03bb t \u2192 [] ++ ys \u2261 t ++ ys)\n (sym $ rev-[] [])\n refl\n \u27e9\n rev [] [] ++ ys\n \u220e\nrev-++ {ys = ys} (consL x {xs} Lxs) Lys =\n begin\n rev (x \u2237 xs) ys \u2261\u27e8 rev-\u2237 x xs ys \u27e9\n rev xs (x \u2237 ys) \u2261\u27e8 rev-++ Lxs (consL x Lys) \u27e9 -- IH.\n rev xs [] ++ x \u2237 ys\n \u2261\u27e8 subst (\u03bb t \u2192 rev xs [] ++ x \u2237 ys \u2261 rev xs [] ++ t)\n (sym\n ( begin\n (x \u2237 []) ++ ys \u2261\u27e8 ++-\u2237 x [] ys \u27e9\n x \u2237 ([] ++ ys) \u2261\u27e8 subst (\u03bb t \u2192 x \u2237 ([] ++ ys) \u2261 x \u2237 t)\n (++-leftIdentity Lys)\n refl\n \u27e9\n x \u2237 ys\n \u220e\n )\n )\n refl\n \u27e9\n rev xs [] ++ (x \u2237 []) ++ ys\n \u2261\u27e8 sym $ ++-assoc (rev-List Lxs nilL) (consL x nilL) Lys \u27e9\n (rev xs [] ++ (x \u2237 [])) ++ ys\n \u2261\u27e8 subst (\u03bb t \u2192 (rev xs [] ++ (x \u2237 [])) ++ ys \u2261 t ++ ys)\n (sym $ rev-++ Lxs (consL x nilL)) -- IH.\n refl\n \u27e9\n rev xs (x \u2237 []) ++ ys\n \u2261\u27e8 subst (\u03bb t \u2192 rev xs (x \u2237 []) ++ ys \u2261 t ++ ys)\n (sym $ rev-\u2237 x xs [])\n refl\n \u27e9\n rev (x \u2237 xs) [] ++ ys\n \u220e\n\nreverse-++ : \u2200 {xs ys} \u2192 List xs \u2192 List ys \u2192\n reverse (xs ++ ys) \u2261 reverse ys ++ reverse xs\nreverse-++ {ys = ys} nilL Lys =\n begin\n reverse ([] ++ ys)\n \u2261\u27e8 subst (\u03bb t \u2192 reverse ([] ++ ys) \u2261 reverse t)\n (++-[] ys)\n refl\n \u27e9\n reverse ys\n \u2261\u27e8 sym (++-rightIdentity (rev-List Lys nilL)) \u27e9\n reverse ys ++ []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse ys ++ [] \u2261 reverse ys ++ t)\n (sym (rev-[] []))\n refl\n \u27e9\n reverse ys ++ reverse []\n \u220e\n\nreverse-++ (consL x {xs} Lxs) nilL =\n begin\n reverse ((x \u2237 xs) ++ [])\n \u2261\u27e8 subst (\u03bb t \u2192 reverse ((x \u2237 xs) ++ []) \u2261 reverse t)\n (++-rightIdentity (consL x Lxs))\n refl\n \u27e9\n reverse (x \u2237 xs)\n \u2261\u27e8 sym (++-[] (reverse (x \u2237 xs))) \u27e9\n [] ++ reverse (x \u2237 xs)\n \u2261\u27e8 subst (\u03bb t \u2192 [] ++ reverse (x \u2237 xs) \u2261 t ++ reverse (x \u2237 xs))\n (sym (rev-[] []))\n refl\n \u27e9\n reverse [] ++ reverse (x \u2237 xs)\n \u220e\n\nreverse-++ (consL x {xs} Lxs) (consL y {ys} Lys) =\n begin\n reverse ((x \u2237 xs) ++ y \u2237 ys) \u2261\u27e8 refl \u27e9\n rev ((x \u2237 xs) ++ y \u2237 ys) []\n \u2261\u27e8 subst (\u03bb t \u2192 rev ((x \u2237 xs) ++ y \u2237 ys) [] \u2261 rev t [])\n (++-\u2237 x xs (y \u2237 ys))\n refl\n \u27e9\n rev (x \u2237 (xs ++ y \u2237 ys)) [] \u2261\u27e8 rev-\u2237 x (xs ++ y \u2237 ys) [] \u27e9\n rev (xs ++ y \u2237 ys) (x \u2237 [])\n \u2261\u27e8 rev-++ (++-List Lxs (consL y Lys)) (consL x nilL) \u27e9\n rev (xs ++ y \u2237 ys) [] ++ (x \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 rev (xs ++ y \u2237 ys) [] ++ (x \u2237 []) \u2261 t ++ (x \u2237 []))\n refl\n refl\n \u27e9\n reverse (xs ++ y \u2237 ys) ++ (x \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (xs ++ y \u2237 ys) ++ (x \u2237 []) \u2261 t ++ (x \u2237 []))\n (reverse-++ Lxs (consL y Lys)) -- IH.\n refl\n \u27e9\n (reverse (y \u2237 ys) ++ reverse xs) ++ x \u2237 []\n \u2261\u27e8 ++-assoc (rev-List (consL y Lys) nilL)\n (rev-List Lxs nilL)\n (consL x nilL)\n \u27e9\n reverse (y \u2237 ys) ++ reverse xs ++ x \u2237 []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ reverse xs ++ x \u2237 [] \u2261\n reverse (y \u2237 ys) ++ t ++ x \u2237 [])\n refl\n refl\n \u27e9\n reverse (y \u2237 ys) ++ rev xs [] ++ x \u2237 []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ rev xs [] ++ x \u2237 [] \u2261\n reverse (y \u2237 ys) ++ t)\n (sym $ rev-++ Lxs (consL x nilL))\n refl\n \u27e9\n reverse (y \u2237 ys) ++ rev xs (x \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ rev xs (x \u2237 []) \u2261\n reverse (y \u2237 ys) ++ t)\n (sym $ rev-\u2237 x xs [])\n refl\n \u27e9\n reverse (y \u2237 ys) ++ rev (x \u2237 xs) []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ rev (x \u2237 xs) [] \u2261\n reverse (y \u2237 ys) ++ t)\n refl\n refl\n \u27e9\n reverse (y \u2237 ys) ++ reverse (x \u2237 xs)\n \u220e\n\nmap-++ : \u2200 f {xs ys} \u2192 List xs \u2192 List ys \u2192\n map f (xs ++ ys) \u2261 map f xs ++ map f ys\nmap-++ f {ys = ys} nilL Lys =\n begin\n map f ([] ++ ys)\n \u2261\u27e8 subst (\u03bb t \u2192 map f ([] ++ ys) \u2261 map f t)\n (++-[] ys)\n refl\n \u27e9\n map f ys\n \u2261\u27e8 sym (++-leftIdentity (map-List f Lys)) \u27e9\n [] ++ map f ys\n \u2261\u27e8 subst (\u03bb t \u2192 [] ++ map f ys \u2261 t ++ map f ys)\n (sym (map-[] f))\n refl\n \u27e9\n map f [] ++ map f ys\n \u220e\n\nmap-++ f {ys = ys} (consL x {xs} Lxs) Lys =\n begin\n map f ((x \u2237 xs) ++ ys)\n \u2261\u27e8 subst (\u03bb t \u2192 map f ((x \u2237 xs) ++ ys) \u2261 map f t)\n (++-\u2237 x xs ys)\n refl\n \u27e9\n map f (x \u2237 xs ++ ys)\n \u2261\u27e8 map-\u2237 f x (xs ++ ys) \u27e9\n f \u00b7 x \u2237 map f (xs ++ ys)\n \u2261\u27e8 subst (\u03bb t \u2192 f \u00b7 x \u2237 map f (xs ++ ys) \u2261 f \u00b7 x \u2237 t)\n (map-++ f Lxs Lys) -- IH.\n refl\n \u27e9\n f \u00b7 x \u2237 (map f xs ++ map f ys)\n \u2261\u27e8 sym (++-\u2237 (f \u00b7 x) (map f xs) (map f ys)) \u27e9\n (f \u00b7 x \u2237 map f xs) ++ map f ys\n \u2261\u27e8 subst (\u03bb t \u2192 (f \u00b7 x \u2237 map f xs) ++ map f ys \u2261 t ++ map f ys)\n (sym (map-\u2237 f x xs))\n refl\n \u27e9\n map f (x \u2237 xs) ++ map f ys\n \u220e\n","old_contents":"------------------------------------------------------------------------------\n-- Properties related with lists\n------------------------------------------------------------------------------\n\nmodule LTC.Data.List.PropertiesI where\n\nopen import LTC.Base\n\nopen import Common.Function\n\nopen import LTC.Data.List\nopen import LTC.Data.List.Type\n\nopen import LTC.Relation.Binary.EqReasoning\n\n------------------------------------------------------------------------------\n\n++-List : \u2200 {xs ys} \u2192 List xs \u2192 List ys \u2192 List (xs ++ ys)\n++-List {ys = ys} nilL Lys = subst List (sym (++-[] ys)) Lys\n++-List {ys = ys} (consL x {xs} Lxs) Lys =\n subst List (sym (++-\u2237 x xs ys)) (consL x (++-List Lxs Lys))\n\nrev-List : \u2200 {xs ys} \u2192 List xs \u2192 List ys \u2192 List (rev xs ys)\nrev-List {ys = ys} nilL Lys = subst List (sym (rev-[] ys)) Lys\nrev-List {ys = ys} (consL x {xs} Lxs) Lys =\n subst List (sym (rev-\u2237 x xs ys)) (rev-List Lxs (consL x Lys))\n\n++-leftIdentity : \u2200 {xs} \u2192 List xs \u2192 [] ++ xs \u2261 xs\n++-leftIdentity {xs} _ = ++-[] xs\n\n++-rightIdentity : \u2200 {xs} \u2192 List xs \u2192 xs ++ [] \u2261 xs\n++-rightIdentity nilL = ++-[] []\n++-rightIdentity (consL x {xs} Lxs) =\n begin\n (x \u2237 xs) ++ []\n \u2261\u27e8 ++-\u2237 x xs [] \u27e9\n x \u2237 (xs ++ [])\n \u2261\u27e8 subst (\u03bb t \u2192 x \u2237 (xs ++ []) \u2261 x \u2237 t)\n (++-rightIdentity Lxs)\n refl\n \u27e9\n x \u2237 xs\n \u220e\n\n++-assoc : \u2200 {xs ys zs} \u2192 List xs \u2192 List ys \u2192 List zs \u2192\n (xs ++ ys) ++ zs \u2261 xs ++ (ys ++ zs)\n++-assoc {ys = ys} {zs} nilL Lys Lzs =\n begin\n ([] ++ ys) ++ zs\n \u2261\u27e8 subst (\u03bb t \u2192 ([] ++ ys) ++ zs \u2261 t ++ zs)\n (++-[] ys)\n refl\n \u27e9\n ys ++ zs\n \u2261\u27e8 sym (++-leftIdentity (++-List Lys Lzs)) \u27e9\n [] ++ ys ++ zs\n \u220e\n\n++-assoc {ys = ys} {zs} (consL x {xs} Lxs) Lys Lzs =\n begin\n ((x \u2237 xs) ++ ys) ++ zs\n \u2261\u27e8 subst (\u03bb t \u2192 ((x \u2237 xs) ++ ys) ++ zs \u2261 t ++ zs)\n (++-\u2237 x xs ys)\n refl\n \u27e9\n (x \u2237 (xs ++ ys)) ++ zs\n \u2261\u27e8 ++-\u2237 x (xs ++ ys) zs \u27e9\n x \u2237 ((xs ++ ys) ++ zs)\n \u2261\u27e8 subst (\u03bb t \u2192 x \u2237 ((xs ++ ys) ++ zs) \u2261 x \u2237 t)\n (++-assoc Lxs Lys Lzs) -- IH.\n refl\n \u27e9\n x \u2237 (xs ++ ys ++ zs)\n \u2261\u27e8 sym (++-\u2237 x xs (ys ++ zs)) \u27e9\n (x \u2237 xs) ++ ys ++ zs\n \u220e\n\nrev-++ : \u2200 {xs ys} \u2192 List xs \u2192 List ys \u2192 rev xs ys \u2261 rev xs [] ++ ys\nrev-++ {ys = ys} nilL Lys =\n begin\n rev [] ys \u2261\u27e8 rev-[] ys \u27e9\n ys \u2261\u27e8 sym $ ++-leftIdentity Lys \u27e9\n [] ++ ys \u2261\u27e8 subst (\u03bb t \u2192 [] ++ ys \u2261 t ++ ys)\n (sym $ rev-[] [])\n refl\n \u27e9\n rev [] [] ++ ys\n \u220e\nrev-++ {ys = ys} (consL x {xs} Lxs) Lys =\n begin\n rev (x \u2237 xs) ys \u2261\u27e8 rev-\u2237 x xs ys \u27e9\n rev xs (x \u2237 ys) \u2261\u27e8 rev-++ Lxs (consL x Lys) \u27e9 -- IH.\n rev xs [] ++ x \u2237 ys\n \u2261\u27e8 subst (\u03bb t \u2192 rev xs [] ++ x \u2237 ys \u2261 rev xs [] ++ t)\n (sym\n ( begin\n (x \u2237 []) ++ ys \u2261\u27e8 ++-\u2237 x [] ys \u27e9\n x \u2237 ([] ++ ys) \u2261\u27e8 subst (\u03bb t \u2192 x \u2237 ([] ++ ys) \u2261 x \u2237 t)\n (++-leftIdentity Lys)\n refl\n \u27e9\n x \u2237 ys\n \u220e\n )\n )\n refl\n \u27e9\n rev xs [] ++ (x \u2237 []) ++ ys\n \u2261\u27e8 sym $ ++-assoc (rev-List Lxs nilL) (consL x nilL) Lys \u27e9\n (rev xs [] ++ (x \u2237 [])) ++ ys\n \u2261\u27e8 subst (\u03bb t \u2192 (rev xs [] ++ (x \u2237 [])) ++ ys \u2261 t ++ ys)\n (sym $ rev-++ Lxs (consL x nilL)) -- IH.\n refl\n \u27e9\n rev xs (x \u2237 []) ++ ys\n \u2261\u27e8 subst (\u03bb t \u2192 rev xs (x \u2237 []) ++ ys \u2261 t ++ ys)\n (sym $ rev-\u2237 x xs [])\n refl\n \u27e9\n rev (x \u2237 xs) [] ++ ys\n \u220e\n\nreverse-++ : \u2200 {xs ys} \u2192 List xs \u2192 List ys \u2192\n reverse (xs ++ ys) \u2261 reverse ys ++ reverse xs\nreverse-++ {ys = ys} nilL Lys =\n begin\n reverse ([] ++ ys)\n \u2261\u27e8 subst (\u03bb t \u2192 reverse ([] ++ ys) \u2261 reverse t)\n (++-[] ys)\n refl\n \u27e9\n reverse ys\n \u2261\u27e8 sym (++-rightIdentity (rev-List Lys nilL)) \u27e9\n reverse ys ++ []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse ys ++ [] \u2261 reverse ys ++ t)\n (sym (rev-[] []))\n refl\n \u27e9\n reverse ys ++ reverse []\n \u220e\n\nreverse-++ (consL x {xs} Lxs) nilL =\n begin\n reverse ((x \u2237 xs) ++ [])\n \u2261\u27e8 subst (\u03bb t \u2192 reverse ((x \u2237 xs) ++ []) \u2261 reverse t)\n (++-rightIdentity (consL x Lxs))\n refl\n \u27e9\n reverse (x \u2237 xs)\n \u2261\u27e8 sym (++-[] (reverse (x \u2237 xs))) \u27e9\n [] ++ reverse (x \u2237 xs)\n \u2261\u27e8 subst (\u03bb t \u2192 [] ++ reverse (x \u2237 xs) \u2261 t ++ reverse (x \u2237 xs))\n (sym (rev-[] []))\n refl\n \u27e9\n reverse [] ++ reverse (x \u2237 xs)\n \u220e\n\nreverse-++ (consL x {xs} Lxs) (consL y {ys} Lys) =\n begin\n reverse ((x \u2237 xs) ++ y \u2237 ys) \u2261\u27e8 refl \u27e9\n rev ((x \u2237 xs) ++ y \u2237 ys) []\n \u2261\u27e8 subst (\u03bb t \u2192 rev ((x \u2237 xs) ++ y \u2237 ys) [] \u2261 rev t [])\n (++-\u2237 x xs (y \u2237 ys))\n refl\n \u27e9\n rev (x \u2237 (xs ++ y \u2237 ys)) [] \u2261\u27e8 rev-\u2237 x (xs ++ y \u2237 ys) [] \u27e9\n rev (xs ++ y \u2237 ys) (x \u2237 [])\n \u2261\u27e8 rev-++ (++-List Lxs (consL y Lys)) (consL x nilL) \u27e9\n rev (xs ++ y \u2237 ys) [] ++ (x \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 rev (xs ++ y \u2237 ys) [] ++ (x \u2237 []) \u2261 t ++ (x \u2237 []))\n refl\n refl\n \u27e9\n reverse (xs ++ y \u2237 ys) ++ (x \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (xs ++ y \u2237 ys) ++ (x \u2237 []) \u2261 t ++ (x \u2237 []))\n (reverse-++ Lxs (consL y Lys)) -- IH.\n refl\n \u27e9\n (reverse (y \u2237 ys) ++ reverse xs) ++ x \u2237 []\n \u2261\u27e8 ++-assoc (rev-List (consL y Lys) nilL)\n (rev-List Lxs nilL)\n (consL x nilL)\n \u27e9\n reverse (y \u2237 ys) ++ reverse xs ++ x \u2237 []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ reverse xs ++ x \u2237 [] \u2261\n reverse (y \u2237 ys) ++ t ++ x \u2237 [])\n refl\n refl\n \u27e9\n reverse (y \u2237 ys) ++ rev xs [] ++ x \u2237 []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ rev xs [] ++ x \u2237 [] \u2261\n reverse (y \u2237 ys) ++ t)\n (sym $ rev-++ Lxs (consL x nilL))\n refl\n \u27e9\n reverse (y \u2237 ys) ++ rev xs (x \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ rev xs (x \u2237 []) \u2261\n reverse (y \u2237 ys) ++ t)\n (sym $ rev-\u2237 x xs [])\n refl\n \u27e9\n reverse (y \u2237 ys) ++ rev (x \u2237 xs) []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ rev (x \u2237 xs) [] \u2261\n reverse (y \u2237 ys) ++ t)\n refl\n refl\n \u27e9\n reverse (y \u2237 ys) ++ reverse (x \u2237 xs)\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b69fdacecd04c4b7e4945680a7ef182b34551c79","subject":"Prob: start some distE work","message":"Prob: start some distE work\n","repos":"crypto-agda\/crypto-agda","old_file":"Prob.agda","new_file":"Prob.agda","new_contents":"module Prob where\n\nopen import Data.Bool\nopen import Data.Nat\nopen import Data.Vec\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\nopen import Data.Sum\n\nopen import Function\n\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Relation.Nullary\n\nrecord ]0,1[-ops (]0,1[ : Set) (_ : (x y : ]0,1[) \u2192 x ) \u2261 (x \u00b7E y) \/E z < \u00b7E-anti\u2082 x pf >\n\n -- proofs \u2261 maybe should be some Equivalence \u2248\n field\n +E-sym : (x y : ]0,1[) \u2192 x +E y \u2261 y +E x\n +E-assoc : (x y z : ]0,1[) \u2192 (x +E y) +E z \u2261 x +E (y +E z)\n \u00b7\/E-identity : (x : ]0,1[){y : ]0,1[} \u2192 x \u2261 (x \u00b7E y) \/E y < \u00b7E-anti\u2081 x >\n\nmodule ]0,1[-\u211a where\n data ]0,1[ : Set where\n 1+_\/2+x+_ : \u2115 \u2192 \u2115 \u2192 ]0,1[\n -- to\u211a : ]0,1] \u2192 \u211a\n -- to\u211a (1+ x \/1+x+ y) = (1 + x) \/\u211a (1 + x + y)\n\n _ : (x y : ]0,1[) \u2192 x = 1+ x'' \/2+x+ y''\n where x'' = 1 + x' + y' + 2 * x + x * x' + x * y'\n y'' = y + x' + x' * y \u2238 1 \u2238 y' \u2238 x \u2238 x * y'\n\n -- |(a\/b)-(c\/d)| = |(ad-bc)\/bd| = |ad-bc|\/bd\n postulate\n distE : ]0,1[ \u2192 ]0,1[ \u2192 ]0,1[\n -- distE (1+ x \/2+x+ y) (1+ x' \/2+x+ y') = {!!}\n\n postulate\n _+E_ : ]0,1[ \u2192 ]0,1[ \u2192 ]0,1[\n\n postulate\n ) \u2261 (x \u00b7E y) \/E z < \u00b7E-anti\u2082 x pf >\n\n postulate\n +E-sym : (x x\u2081 : ]0,1[) \u2192 x +E x\u2081 \u2261 x\u2081 +E x\n +E-assoc : (x x\u2081 x\u2082 : ]0,1[) \u2192 (x +E x\u2081) +E x\u2082 \u2261 x +E (x\u2081 +E x\u2082)\n \u00b7\/E-identity : (x : ]0,1[) {y : ]0,1[} \u2192 x \u2261 (x \u00b7E y) \/E y < \u00b7E-anti\u2081 x >\n\n ops : ]0,1[-ops ]0,1[ _ 1-E_ distE (\u03bb {x} \u2192 : (x : [0,1]) \u2192 Inc x \u2192 ]0,1[\n 0I < () >\n 1I < () >\n (x I) < pos > = x\n\n 1-I_ : [0,1] \u2192 [0,1]\n 1-I 0I = 1I\n 1-I 1I = 0I\n 1-I (x I) = (1-E x) I\n\n _+I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I +I x = x\n x I +I 0I = x I\n 1I +I _ = 1I -- troublesome\n x I +I 1I = 1I -- troublesome\n x I +I x\u2081 I = (x +E x\u2081) I -- faithful\n\n _\u00b7I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I \u00b7I y = 0I\n 1I \u00b7I y = y\n (x I) \u00b7I 0I = 0I\n (x I) \u00b7I 1I = x I\n (x I) \u00b7I (x\u2081 I) = (x \u00b7E x\u2081) I\n\n -- 1\/I1+_ : \u2115 \u2192 [0,1]\n\n\n _\/I_<_,_> : (x y : [0,1]) \u2192 x \u2264I y \u2192 Pos y \u2192 [0,1]\n x \/I 0I < _ , () >\n x \/I 1I < _ , _ > = x\n 0I \/I _ < _ , _ > = 0I\n 1I \/I _ I < () , _ >\n (x I) \/I y I < E = (x \/E y < pf >) I\n (x I) \/I .x I < E\u2261E , _ > = 1I\n\n\n +I-identity : (x : [0,1]) \u2192 x \u2261 0I +I x\n +I-identity x = refl\n\n *-anti : (x : [0,1]){y z : [0,1]} \u2192 y \u2264I z \u2192 x \u00b7I y \u2264I z\n *-anti 0I le = z\u2264n\n *-anti 1I le = le\n *-anti (x I) z\u2264n = z\u2264n\n *-anti (x I) n\u22641 = n\u22641\n *-anti (x I) (E)) \u2261 (x \u00b7I y) \/I z < *-anti x pr , pos >\n *\/-assoc x y 0I pr ()\n *\/-assoc x y 1I pr pos = refl\n *\/-assoc 0I y (z I) pr pos = refl\n *\/-assoc 1I y (z I) pr pos = refl\n *\/-assoc (x I) 0I (z I) pr pos = refl\n *\/-assoc (x I) 1I (z I) () pos\n *\/-assoc (x I) (y I) (z I) (E : (A B : Event)(pf : Pos Pr[ B ]) \u2192 [0,1]\n Pr[ A \u2223 B ]< pr > = Pr[ A \u2229 B ] \/I Pr[ B ] < Pr-mono (\u2229-lem A) , pr >\n\n _ind_ : (A B : Event) \u2192 Set\n A ind B = Pr[ A ] \u00b7I Pr[ B ] \u2261 Pr[ A \u2229 B ]\n\n union-bound : (A\u2081 A\u2082 : Event) \u2192 Pr[ (A\u2081 \u222a A\u2082) ] \u2264I Pr[ A\u2081 ] +I Pr[ A\u2082 ]\n union-bound A\u2081 A\u2082 = go allU where\n sA\u2081 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2081 = \u03bb xs \u2192 sumP (pr_\u220b_ A\u2081) xs\n sA\u2082 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2082 = sumP (pr_\u220b_ A\u2082)\n go : {n : \u2115}(xs : Vec U n) \u2192 sumP (pr_\u220b_ (A\u2081 \u222a A\u2082)) xs \u2264I sumP (pr_\u220b_ A\u2081) xs +I sumP (pr_\u220b_ A\u2082) xs\n go [] = \u2264I-refl\n go (x \u2237 xs) with A\u2081 x | A\u2082 x\n ... | true | true rewrite +I-assoc (pmf x) (sA\u2081 xs) (pmf x +I sA\u2082 xs)\n | +I-sym (pmf x) (sA\u2082 xs)\n | sym (+I-assoc (sA\u2081 xs) (sA\u2082 xs) (pmf x))\n = \u2264I-pres (pmf x) (\u2264I-mono (pmf x) (go xs))\n ... | true | false rewrite +I-assoc (pmf x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (pmf x) (go xs)\n ... | false | true rewrite sym (+I-assoc (sA\u2081 xs) (pmf x) (sA\u2082 xs))\n | +I-sym (sA\u2081 xs) (pmf x)\n | +I-assoc (pmf x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (pmf x) (go xs)\n ... | false | false = go xs\n\n module RandomVar (V : Set) (_==V_ : V \u2192 V \u2192 Bool) where\n RV : Set\n RV = U \u2192 V\n\n _^-1_ : RV \u2192 V \u2192 Event\n RV ^-1 v = \u03bb x \u2192 RV x ==V v\n\n _\u2261r_ : RV \u2192 V \u2192 Event\n RV \u2261r v = RV ^-1 v\n\n -- sugar, socker et sucre\n Pr[_\u2261_] : RV \u2192 V \u2192 [0,1]\n Pr[ X \u2261 v ] = Pr[ X \u2261r v ]\n\n -- \u2200 (a b) . Pr[X = a and Y = b] = Pr[X = a] \u00b7 Pr[Y = b]\n _indRV_ : RV \u2192 RV \u2192 Set\n X indRV Y = (a b : V) \u2192 (X ^-1 a) ind (Y ^-1 b)\n","old_contents":"module Prob where\n\nopen import Data.Bool\nopen import Data.Nat\nopen import Data.Vec\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5)\nopen import Data.Sum\n\nopen import Function\n\nopen import Relation.Binary.PropositionalEquality.NP\nopen import Relation.Nullary\n\nrecord ]0,1[-ops (]0,1[ : Set) (_ : (x y : ]0,1[) \u2192 x ) \u2261 (x \u00b7E y) \/E z < \u00b7E-anti\u2082 x pf >\n\n -- proofs \u2261 maybe should be some Equivalence \u2248\n field\n +E-sym : (x y : ]0,1[) \u2192 x +E y \u2261 y +E x\n +E-assoc : (x y z : ]0,1[) \u2192 (x +E y) +E z \u2261 x +E (y +E z)\n \u00b7\/E-identity : (x : ]0,1[){y : ]0,1[} \u2192 x \u2261 (x \u00b7E y) \/E y < \u00b7E-anti\u2081 x >\n\nmodule ]0,1[-\u211a where\n data ]0,1[ : Set where\n 1+_\/2+x+_ : \u2115 \u2192 \u2115 \u2192 ]0,1[\n -- to\u211a : ]0,1] \u2192 \u211a\n -- to\u211a (1+ x \/1+x+ y) = (1 + x) \/\u211a (1 + x + y)\n\n postulate\n _ : (x y : ]0,1[) \u2192 x = 1+ x'' \/2+x+ y''\n where x'' = 1 + x' + y' + 2 * x + x * x' + x * y'\n y'' = y + x' + x' * y \u2238 1 \u2238 y' \u2238 x \u2238 x * y'\n\n postulate\n _+E_ : ]0,1[ \u2192 ]0,1[ \u2192 ]0,1[\n\n postulate\n ) \u2261 (x \u00b7E y) \/E z < \u00b7E-anti\u2082 x pf >\n\n postulate\n +E-sym : (x x\u2081 : ]0,1[) \u2192 x +E x\u2081 \u2261 x\u2081 +E x\n +E-assoc : (x x\u2081 x\u2082 : ]0,1[) \u2192 (x +E x\u2081) +E x\u2082 \u2261 x +E (x\u2081 +E x\u2082)\n \u00b7\/E-identity : (x : ]0,1[) {y : ]0,1[} \u2192 x \u2261 (x \u00b7E y) \/E y < \u00b7E-anti\u2081 x >\n\n ops : ]0,1[-ops ]0,1[ _ 1-E_ : (x : [0,1]) \u2192 Inc x \u2192 ]0,1[\n 0I < () >\n 1I < () >\n (x I) < pos > = x\n\n 1-I_ : [0,1] \u2192 [0,1]\n 1-I 0I = 1I\n 1-I 1I = 0I\n 1-I (x I) = (1-E x) I\n\n _+I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I +I x = x\n x I +I 0I = x I\n 1I +I _ = 1I -- troublesome\n x I +I 1I = 1I -- troublesome\n x I +I x\u2081 I = (x +E x\u2081) I -- faithful\n\n _\u00b7I_ : [0,1] \u2192 [0,1] \u2192 [0,1]\n 0I \u00b7I y = 0I\n 1I \u00b7I y = y\n (x I) \u00b7I 0I = 0I\n (x I) \u00b7I 1I = x I\n (x I) \u00b7I (x\u2081 I) = (x \u00b7E x\u2081) I\n\n -- 1\/I1+_ : \u2115 \u2192 [0,1]\n\n\n _\/I_<_,_> : (x y : [0,1]) \u2192 x \u2264I y \u2192 Pos y \u2192 [0,1]\n x \/I 0I < _ , () >\n x \/I 1I < _ , _ > = x\n 0I \/I _ < _ , _ > = 0I\n 1I \/I _ I < () , _ >\n (x I) \/I y I < E = (x \/E y < pf >) I\n (x I) \/I .x I < E\u2261E , _ > = 1I\n\n\n +I-identity : (x : [0,1]) \u2192 x \u2261 0I +I x\n +I-identity x = refl\n\n *-anti : (x : [0,1]){y z : [0,1]} \u2192 y \u2264I z \u2192 x \u00b7I y \u2264I z\n *-anti 0I le = z\u2264n\n *-anti 1I le = le\n *-anti (x I) z\u2264n = z\u2264n\n *-anti (x I) n\u22641 = n\u22641\n *-anti (x I) (E)) \u2261 (x \u00b7I y) \/I z < *-anti x pr , pos >\n *\/-assoc x y 0I pr ()\n *\/-assoc x y 1I pr pos = refl\n *\/-assoc 0I y (z I) pr pos = refl\n *\/-assoc 1I y (z I) pr pos = refl\n *\/-assoc (x I) 0I (z I) pr pos = refl\n *\/-assoc (x I) 1I (z I) () pos\n *\/-assoc (x I) (y I) (z I) (E : (A B : Event)(pf : Pos Pr[ B ]) \u2192 [0,1]\n Pr[ A \u2223 B ]< pr > = Pr[ A \u2229 B ] \/I Pr[ B ] < Pr-mono (\u2229-lem A) , pr >\n\n _ind_ : (A B : Event) \u2192 Set\n A ind B = Pr[ A ] \u00b7I Pr[ B ] \u2261 Pr[ A \u2229 B ]\n\n union-bound : (A\u2081 A\u2082 : Event) \u2192 Pr[ (A\u2081 \u222a A\u2082) ] \u2264I Pr[ A\u2081 ] +I Pr[ A\u2082 ]\n union-bound A\u2081 A\u2082 = go allU where\n sA\u2081 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2081 = \u03bb xs \u2192 sumP (pr_\u220b_ A\u2081) xs\n sA\u2082 : {n : \u2115} \u2192 Vec U n \u2192 [0,1]\n sA\u2082 = sumP (pr_\u220b_ A\u2082)\n go : {n : \u2115}(xs : Vec U n) \u2192 sumP (pr_\u220b_ (A\u2081 \u222a A\u2082)) xs \u2264I sumP (pr_\u220b_ A\u2081) xs +I sumP (pr_\u220b_ A\u2082) xs\n go [] = \u2264I-refl\n go (x \u2237 xs) with A\u2081 x | A\u2082 x\n ... | true | true rewrite +I-assoc (pmf x) (sA\u2081 xs) (pmf x +I sA\u2082 xs)\n | +I-sym (pmf x) (sA\u2082 xs)\n | sym (+I-assoc (sA\u2081 xs) (sA\u2082 xs) (pmf x))\n = \u2264I-pres (pmf x) (\u2264I-mono (pmf x) (go xs))\n ... | true | false rewrite +I-assoc (pmf x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (pmf x) (go xs)\n ... | false | true rewrite sym (+I-assoc (sA\u2081 xs) (pmf x) (sA\u2082 xs))\n | +I-sym (sA\u2081 xs) (pmf x)\n | +I-assoc (pmf x) (sA\u2081 xs) (sA\u2082 xs)\n = \u2264I-pres (pmf x) (go xs)\n ... | false | false = go xs\n\n module RandomVar (V : Set) (_==V_ : V \u2192 V \u2192 Bool) where\n RV : Set\n RV = U \u2192 V\n\n _^-1_ : RV \u2192 V \u2192 Event\n RV ^-1 v = \u03bb x \u2192 RV x ==V v\n\n _\u2261r_ : RV \u2192 V \u2192 Event\n RV \u2261r v = RV ^-1 v\n\n -- sugar, socker et sucre\n Pr[_\u2261_] : RV \u2192 V \u2192 [0,1]\n Pr[ X \u2261 v ] = Pr[ X \u2261r v ]\n\n -- \u2200 (a b) . Pr[X = a and Y = b] = Pr[X = a] \u00b7 Pr[Y = b]\n _indRV_ : RV \u2192 RV \u2192 Set\n X indRV Y = (a b : V) \u2192 (X ^-1 a) ind (Y ^-1 b)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"cce35b611f1cd9557c69b78c3ff9de51d121ee43","subject":"Fixed note.","message":"Fixed note.\n\nIgnore-this: f2e859dc6606334257adeb3012c3436b\n\ndarcs-hash:20120509041415-3bd4e-ac8e7583ced12243b8d1d1b1a5197e9de9c45c18.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/thesis\/logical-framework\/Existential.agda","new_file":"notes\/thesis\/logical-framework\/Existential.agda","new_contents":"-- Tested with FOT on 08 May 2012.\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Existential where\n\nmodule LF where\n postulate\n D : Set\n\n -- Disjunction.\n _\u2228_ : Set \u2192 Set \u2192 Set\n inj\u2081 : {A B : Set} \u2192 A \u2192 A \u2228 B\n inj\u2082 : {A B : Set} \u2192 B \u2192 A \u2228 B\n [_,_] : {A B C : Set} \u2192 (A \u2192 C) \u2192 (B \u2192 C) \u2192 A \u2228 B \u2192 C\n\n -- The existential quantifier type on D.\n \u2203 : (A : D \u2192 Set) \u2192 Set\n _,_ : {A : D \u2192 Set}(t : D) \u2192 A t \u2192 \u2203 A\n \u2203-proj\u2081 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n \u2203-proj\u2082 : {A : D \u2192 Set}(h : \u2203 A) \u2192 A (\u2203-proj\u2081 h)\n \u2203-elim : {A : D \u2192 Set}{B : Set} \u2192 \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B) \u2192 B\n\n syntax \u2203 (\u03bb x \u2192 e) = \u2203[ x ] e\n\n module FOL-Examples where\n -- Using the projections.\n \u2203\u2200\u2081 : {A : D \u2192 D \u2192 Set} \u2192 \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2081 h y = \u2203-proj\u2081 h , (\u2203-proj\u2082 h) y\n\n \u2203\u2228\u2081 : {A B : D \u2192 Set} \u2192 \u2203[ x ](A x \u2228 B x) \u2192 (\u2203[ x ] A x) \u2228 (\u2203[ x ] B x)\n \u2203\u2228\u2081 h = [ (\u03bb Ax \u2192 inj\u2081 (\u2203-proj\u2081 h , Ax))\n , (\u03bb Bx \u2192 inj\u2082 (\u2203-proj\u2081 h , Bx))\n ] (\u2203-proj\u2082 h)\n\n -- Using the elimination\n \u2203\u2200\u2082 : {A : D \u2192 D \u2192 Set} \u2192 \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2082 h y = \u2203-elim h (\u03bb {x} ah \u2192 x , ah y)\n\n \u2203\u2228\u2082 : {A B : D \u2192 Set} \u2192 \u2203[ x ](A x \u2228 B x) \u2192 (\u2203[ x ] A x) \u2228 (\u2203[ x ] B x)\n \u2203\u2228\u2082 h = \u2203-elim h (\u03bb {x} ah \u2192 [ (\u03bb Ax \u2192 inj\u2081 (x , Ax))\n , (\u03bb Bx \u2192 inj\u2082 (x , Bx))\n ] ah)\n\n module NonFOL-Examples where\n\n -- Using the projections.\n non-FOL\u2081 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2081 h = \u2203-proj\u2081 h\n\n -- Using the elimination.\n non-FOL\u2082 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2082 h = \u2203-elim h (\u03bb {x} _ \u2192 x)\n\nmodule Inductive where\n\n open import Common.FOL.FOL\n\n -- The existential proyections.\n \u2203-proj\u2081 : \u2200 {A} \u2192 \u2203 A \u2192 D\n \u2203-proj\u2081 (x , _) = x\n\n \u2203-proj\u2082 : \u2200 {A} \u2192 (h : \u2203 A) \u2192 A (\u2203-proj\u2081 h)\n \u2203-proj\u2082 (_ , Ax) = Ax\n\n -- The existential elimination.\n \u2203-elim : {A : D \u2192 Set}{B : Set} \u2192 \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B) \u2192 B\n \u2203-elim (_ , Ax) h = h Ax\n\n module FOL-Examples where\n -- Using the projections.\n \u2203\u2200\u2081 : {A : D \u2192 D \u2192 Set} \u2192 \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2081 h y = \u2203-proj\u2081 h , (\u2203-proj\u2082 h) y\n\n \u2203\u2228\u2081 : {A B : D \u2192 Set} \u2192 \u2203[ x ](A x \u2228 B x) \u2192 (\u2203[ x ] A x) \u2228 (\u2203[ x ] B x)\n \u2203\u2228\u2081 h = [ (\u03bb Ax \u2192 inj\u2081 (\u2203-proj\u2081 h , Ax))\n , (\u03bb Bx \u2192 inj\u2082 (\u2203-proj\u2081 h , Bx))\n ] (\u2203-proj\u2082 h)\n\n -- Using the elimination.\n \u2203\u2200\u2082 : {A : D \u2192 D \u2192 Set} \u2192 \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2082 h y = \u2203-elim h (\u03bb {x} ah \u2192 x , ah y)\n\n \u2203\u2228\u2082 : {A B : D \u2192 Set} \u2192 \u2203[ x ](A x \u2228 B x) \u2192 (\u2203[ x ] A x) \u2228 (\u2203[ x ] B x)\n \u2203\u2228\u2082 h = \u2203-elim h (\u03bb {x} ah \u2192 [ (\u03bb Ax \u2192 inj\u2081 (x , Ax))\n , (\u03bb Bx \u2192 inj\u2082 (x , Bx))\n ] ah)\n\n -- Using pattern matching.\n \u2203\u2200\u2083 : {A : D \u2192 D \u2192 Set} \u2192 \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2083 (x , Ax) y = x , Ax y\n\n \u2203\u2228\u2083 : {A B : D \u2192 Set} \u2192 \u2203[ x ](A x \u2228 B x) \u2192 (\u2203[ x ] A x) \u2228 (\u2203[ x ] B x)\n \u2203\u2228\u2083 (x , inj\u2081 Ax) = inj\u2081 (x , Ax)\n \u2203\u2228\u2083 (x , inj\u2082 Bx) = inj\u2082 (x , Bx)\n\n module NonFOL-Examples where\n\n -- Using the projections.\n non-FOL\u2081 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2081 h = \u2203-proj\u2081 h\n\n -- Using the elimination.\n non-FOL\u2082 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2082 h = \u2203-elim h (\u03bb {x} _ \u2192 x)\n\n -- Using the pattern matching.\n non-FOL\u2083 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2083 (x , _) = x\n","old_contents":"-- Tested with FOT on 08 May 2012.\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Existential where\n\nmodule LF where\n postulate\n D : Set\n\n -- Disjunction.\n _\u2228_ : Set \u2192 Set \u2192 Set\n inj\u2081 : {A B : Set} \u2192 A \u2192 A \u2228 B\n inj\u2082 : {A B : Set} \u2192 B \u2192 A \u2228 B\n [_,_] : {A B C : Set} \u2192 (A \u2192 C) \u2192 (B \u2192 C) \u2192 A \u2228 B \u2192 C\n\n -- The existential quantifier type on D.\n \u2203 : (A : D \u2192 Set) \u2192 Set\n _,_ : {A : D \u2192 Set}(t : D) \u2192 A t \u2192 \u2203 A\n \u2203-proj\u2081 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n \u2203-proj\u2082 : {A : D \u2192 Set}(h : \u2203 A) \u2192 A (\u2203-proj\u2081 h)\n \u2203-elim : {A : D \u2192 Set}{B : Set} \u2192 \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B) \u2192 B\n\n syntax \u2203 (\u03bb x \u2192 e) = \u2203[ x ] e\n\n module FOL-Examples where\n -- Using the projections.\n \u2203\u2200\u2081 : {A : D \u2192 D \u2192 Set} \u2192 \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2081 h y = \u2203-proj\u2081 h , (\u2203-proj\u2082 h) y\n\n \u2203\u2228\u2081 : {A B : D \u2192 Set} \u2192 \u2203[ x ](A x \u2228 B x) \u2192 (\u2203[ x ] A x) \u2228 (\u2203[ x ] B x)\n \u2203\u2228\u2081 h = [ (\u03bb Ax \u2192 inj\u2081 (\u2203-proj\u2081 h , Ax))\n , (\u03bb Bx \u2192 inj\u2082 (\u2203-proj\u2081 h , Bx))\n ] (\u2203-proj\u2082 h)\n\n -- Using the elimination\n \u2203\u2200\u2082 : {A : D \u2192 D \u2192 Set} \u2192 \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2082 h y = \u2203-elim h (\u03bb {x} ah \u2192 x , ah y)\n\n \u2203\u2228\u2082 : {A B : D \u2192 Set} \u2192 \u2203[ x ](A x \u2228 B x) \u2192 (\u2203[ x ] A x) \u2228 (\u2203[ x ] B x)\n \u2203\u2228\u2082 h = \u2203-elim h (\u03bb {x} ah \u2192 [ (\u03bb Ax \u2192 inj\u2081 (x , Ax))\n , (\u03bb Bx \u2192 inj\u2082 (x , Bx))\n ] ah)\n\n module NonFOL-Examples where\n\n -- Using the projections.\n non-FOL\u2081 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2081 h = \u2203-proj\u2081 h\n\n -- Using the elimination.\n non-FOL\u2082 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2082 h = \u2203-elim h (\u03bb {x} _ \u2192 x)\n\nmodule Inductive where\n\n open import Common.FOL.FOL\n\n -- The existential proyections.\n \u2203-proj\u2081 : \u2200 {A} \u2192 \u2203 A \u2192 D\n \u2203-proj\u2081 (x , _) = x\n\n \u2203-proj\u2082 : \u2200 {A} \u2192 (h : \u2203 A) \u2192 A (\u2203-proj\u2081 h)\n \u2203-proj\u2082 (_ , Ax) = Ax\n\n -- The existential elimination.\n \u2203-elim : {A : D \u2192 Set}{B : Set} \u2192 \u2203 A \u2192 (\u2200 {x} \u2192 A x \u2192 B) \u2192 B\n \u2203-elim (_ , Ax) h = h Ax\n\n module FOL-Examples where\n -- Using the projections.\n \u2203\u2200\u2081 : {A : D \u2192 D \u2192 Set} \u2192 \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2081 h y = \u2203-proj\u2081 h , (\u2203-proj\u2082 h) y\n\n \u2203\u2228\u2081 : {A B : D \u2192 Set} \u2192 \u2203[ x ](A x \u2228 B x) \u2192 (\u2203[ x ] A x) \u2228 (\u2203[ x ] B x)\n \u2203\u2228\u2081 h = [ (\u03bb Ax \u2192 inj\u2081 (\u2203-proj\u2081 h , Ax))\n , (\u03bb Bx \u2192 inj\u2082 (\u2203-proj\u2081 h , Bx))\n ] (\u2203-proj\u2082 h)\n\n -- Using the elimination.\n \u2203\u2200\u2082 : {A : D \u2192 D \u2192 Set} \u2192 \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2082 h y = \u2203-elim h (\u03bb {x} ah \u2192 x , ah y)\n\n \u2203\u2228\u2082 : {A B : D \u2192 Set} \u2192 \u2203[ x ](A x \u2228 B x) \u2192 (\u2203[ x ] A x) \u2228 (\u2203[ x ] B x)\n \u2203\u2228\u2082 h = \u2203-elim h (\u03bb {x} ah\u2081 \u2192 [ (\u03bb Ax \u2192 inj\u2081 (x , Ax))\n , (\u03bb Bx \u2192 inj\u2082 (x , Bx))\n ] ah)\n\n -- Using pattern matching.\n \u2203\u2200\u2083 : {A : D \u2192 D \u2192 Set} \u2192 \u2203[ x ](\u2200 y \u2192 A x y) \u2192 \u2200 y \u2192 \u2203[ x ] A x y\n \u2203\u2200\u2083 (x , Ax) y = x , Ax y\n\n \u2203\u2228\u2083 : {A B : D \u2192 Set} \u2192 \u2203[ x ](A x \u2228 B x) \u2192 (\u2203[ x ] A x) \u2228 (\u2203[ x ] B x)\n \u2203\u2228\u2083 (x , inj\u2081 Ax) = inj\u2081 (x , Ax)\n \u2203\u2228\u2083 (x , inj\u2082 Bx) = inj\u2082 (x , Bx)\n\n module NonFOL-Examples where\n\n -- Using the projections.\n non-FOL\u2081 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2081 h = \u2203-proj\u2081 h\n\n -- Using the elimination.\n non-FOL\u2082 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2082 h = \u2203-elim h (\u03bb {x} _ \u2192 x)\n\n -- Using the pattern matching.\n non-FOL\u2083 : {A : D \u2192 Set} \u2192 \u2203 A \u2192 D\n non-FOL\u2083 (x , _) = x\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"53e7d78959ce72a27c1c3a13af3ce667fbe4e935","subject":"Fixed typo.","message":"Fixed typo.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/LeastFixedPoints\/N.agda","new_file":"notes\/fixed-points\/LeastFixedPoints\/N.agda","new_contents":"------------------------------------------------------------------------------\n-- Equivalence: N as the least fixed-point and N using Agda's data constructor\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule LeastFixedPoints.N where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- Basic definitions\n\n-- Pre-fixed point : d is a pre-fixed point of f if f d \u2264 d\n\n-- Post-fixed point : d is a post-fixed point of f if d \u2264 f d\n\n-- Fixed-point : d is a fixed-point of f if f d = d\n\n-- Least pre-fixed point : d is the least pre-fixed point of f if\n-- 1. f d \u2264 d -- d is a pre-fixed point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Least fixed-point : d is the least fixed-point of f if\n-- 1. f d = d -- d is a fixed-point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Thm: If d is the least pre-fixed point of f, then d is the least\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n-- Thm: If d is the greatest post-fixed point of f, then d is the greatest\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n------------------------------------------------------------------------------\n-- Auxiliary definitions and properties\n\nflip : {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\nflip f b a = f a b\n\ncong : (f : D \u2192 D) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2261 f y\ncong f refl = refl\n\n------------------------------------------------------------------------------\nmodule LFP where\n\n -- N is a least fixed-point of a functor\n\n -- The functor.\n NatF : (D \u2192 Set) \u2192 D \u2192 Set\n NatF A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n\n -- The natural numbers are the least fixed-point of NatF.\n postulate\n N : D \u2192 Set\n\n -- N is a pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the introduction rules.\n N-in : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n\n -- The higher-order version.\n N-in-ho : \u2200 {n} \u2192 NatF N n \u2192 N n\n\n -- N is the least pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the elimination rule of an inductively\n -- defined predicate.\n N-least-pre-fixed :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\n\n -- Higher-order version (incomplete?).\n N-least-pre-fixed-ho :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (NatF A n \u2192 A n) \u2192\n N n \u2192 A n\n\n ----------------------------------------------------------------------------\n -- From\/to N-in\/N-in-ho.\n\n N-in\u2081 : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n N-in\u2081 = N-in-ho\n\n N-in-ho\u2081 : \u2200 {n} \u2192 NatF N n \u2192 N n\n N-in-ho\u2081 = N-in\u2081\n\n ----------------------------------------------------------------------------\n -- From\/to N-least-pre-fixed\/N-least-pre-fixed-ho\n N-least-pre-fixed' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\n N-least-pre-fixed' = N-least-pre-fixed-ho\n\n N-least-pre-fixed-ho' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (NatF A n \u2192 A n) \u2192\n N n \u2192 A n\n N-least-pre-fixed-ho' = N-least-pre-fixed\n\n ----------------------------------------------------------------------------\n -- The data constructors of N.\n nzero : N zero\n nzero = N-in (inj\u2081 refl)\n\n nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n nsucc Nn = N-in (inj\u2082 (_ , refl , Nn))\n\n ----------------------------------------------------------------------------\n -- Because N is the least pre-fixed point of NatF (i.e. N-in and\n -- N-ind), we can proof that N is also a post-fixed point of NatF.\n\n -- N is a post-fixed point of NatF.\n N-post-fixed : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n')\n N-post-fixed = N-least-pre-fixed A h\n where\n A : D \u2192 Set\n A m = m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 N m')\n\n h : \u2200 {m} \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 prf) = inj\u2081 prf\n h (inj\u2082 (m' , prf , Am')) = inj\u2082 (m' , prf , helper Am')\n where\n helper : A m' \u2192 N m'\n helper (inj\u2081 prf') = subst N (sym prf') nzero\n helper (inj\u2082 (m'' , prf' , Am'')) = subst N (sym prf') (nsucc Am'')\n\n ----------------------------------------------------------------------------\n -- The induction principle for N *with* the hypothesis N n in the\n -- induction step.\n\n N-ind\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2081 A A0 is {n} Nn = N-least-pre-fixed A h Nn\n where\n h : n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n\n h (inj\u2081 prf) = subst A (sym prf) A0\n h (inj\u2082 (n' , prf , An')) = subst A (sym prf) (is helper An')\n where\n helper : N n'\n helper with N-post-fixed Nn\n ... | inj\u2081 n\u22610 = \u22a5-elim (0\u2262S (trans (sym n\u22610) prf))\n ... | inj\u2082 (m' , prf' , Nm') =\n subst N (succInjective (trans (sym prf') prf)) Nm'\n\n ----------------------------------------------------------------------------\n -- The induction principle for N *without* the hypothesis N n in the\n -- induction step.\n\n N-ind\u2082 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2082 A A0 is {n} = N-least-pre-fixed A h\n where\n h : n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n\n h (inj\u2081 prf) = subst A (sym prf) A0\n h (inj\u2082 (n' , prf , An')) = subst A (sym prf) (is An')\n\n ----------------------------------------------------------------------------\n -- Example: We will use N-least-pre-fixed as the induction principle on N.\n\n postulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ\u2081 d + e \u2261 succ\u2081 (d + e)\n\n +-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n +-leftIdentity n = +-0x n\n\n +-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n +-N {m} {n} Nm Nn = N-least-pre-fixed A h Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n h : m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 prf) = subst N (cong (flip _+_ n) (sym prf)) A0\n where\n A0 : A zero\n A0 = subst N (sym (+-leftIdentity n)) Nn\n h (inj\u2082 (m' , m\u2261Sm' , Am')) =\n subst N (cong (flip _+_ n) (sym m\u2261Sm')) (is Am')\n where\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} Ai = subst N (sym (+-Sx i n)) (nsucc Ai)\n\n ----------------------------------------------------------------------------\n -- Example: A proof using N-post-fixed.\n\n pred-N : \u2200 {n} \u2192 N n \u2192 N (pred\u2081 n)\n pred-N {n} Nn = case h\u2081 h\u2082 (N-post-fixed Nn)\n where\n h\u2081 : n \u2261 zero \u2192 N (pred\u2081 n)\n h\u2081 n\u22610 = subst N (sym (trans (predCong n\u22610) pred-0)) nzero\n\n h\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n' \u2192 N (pred\u2081 n)\n h\u2082 (n' , prf , Nn') = subst N (sym (trans (predCong prf) (pred-S n'))) Nn'\n\n ----------------------------------------------------------------------------\n -- From\/to N as a least fixed-point to\/from N as data type.\n\n data N' : D \u2192 Set where\n nzero' : N' zero\n nsucc' : \u2200 {n} \u2192 N' n \u2192 N' (succ\u2081 n)\n\n N'\u2192N : \u2200 {n} \u2192 N' n \u2192 N n\n N'\u2192N nzero' = nzero\n N'\u2192N (nsucc' Nn) = nsucc (N'\u2192N Nn)\n\n -- Using N-ind\u2081.\n N\u2192N' : \u2200 {n} \u2192 N n \u2192 N' n\n N\u2192N' = N-ind\u2081 N' nzero' (\u03bb _ \u2192 nsucc')\n\n -- Using N-ind\u2082.\n N\u2192N'\u2081 : \u2200 {n} \u2192 N n \u2192 N' n\n N\u2192N'\u2081 = N-ind\u2082 N' nzero' nsucc'\n\n------------------------------------------------------------------------------\nmodule Data where\n\n data N : D \u2192 Set where\n nzero : N zero\n nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n\n -- The induction principle for N *with* the hypothesis N n in the\n -- induction step.\n N-ind\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2081 A A0 is nzero = A0\n N-ind\u2081 A A0 is (nsucc Nn) = is Nn (N-ind\u2081 A A0 is Nn)\n\n -- The induction principle for N *without* the hypothesis N n in the\n -- induction step.\n N-ind\u2082 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2082 A A0 h nzero = A0\n N-ind\u2082 A A0 h (nsucc Nn) = h (N-ind\u2082 A A0 h Nn)\n\n ----------------------------------------------------------------------------\n -- N-in.\n\n N-in : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n N-in {n} h = case prf\u2081 prf\u2082 h\n where\n prf\u2081 : n \u2261 zero \u2192 N n\n prf\u2081 n\u22610 = subst N (sym n\u22610) nzero\n\n prf\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n' \u2192 N n\n prf\u2082 (n' , prf , Nn') = subst N (sym prf) (nsucc Nn')\n\n ----------------------------------------------------------------------------\n -- From N-ind\u2082 to N-least-pre-fixed.\n\n N\u21920\u2228S : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n')\n N\u21920\u2228S = N-ind\u2082 A A0 is\n where\n A : D \u2192 Set\n A i = i \u2261 zero \u2228 (\u2203[ i' ] i \u2261 succ\u2081 i' \u2227 N i')\n\n A0 : A zero\n A0 = inj\u2081 refl\n\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} Ai = case prf\u2081 prf\u2082 Ai\n where\n prf\u2081 : i \u2261 zero \u2192 succ\u2081 i \u2261 zero \u2228 (\u2203[ i' ] succ\u2081 i \u2261 succ\u2081 i' \u2227 N i')\n prf\u2081 h' = inj\u2082 (i , refl , (subst N (sym h') nzero))\n\n prf\u2082 : \u2203[ i' ] i \u2261 succ\u2081 i' \u2227 N i' \u2192\n succ\u2081 i \u2261 zero \u2228 (\u2203[ i' ] succ\u2081 i \u2261 succ\u2081 i' \u2227 N i')\n prf\u2082 (i' , prf , Ni') = inj\u2082 (i , refl , subst N (sym prf) (nsucc Ni'))\n\n N-least-pre-fixed\u2082 :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\n N-least-pre-fixed\u2082 A {n} h Nn = case prf\u2081 prf\u2082 (N\u21920\u2228S Nn)\n where\n prf\u2081 : n \u2261 zero \u2192 A n\n prf\u2081 n\u22610 = h (inj\u2081 n\u22610)\n\n prf\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n' \u2192 A n\n prf\u2082 (n' , prf , Nn') = h (inj\u2082 (n' , prf , {!!}))\n","old_contents":"------------------------------------------------------------------------------\n-- Equivalence: N as the least fixed-point and N using Agda's data constructor\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule LeastFixedPoints.N where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- Basic definitions\n\n-- Pre-fixed point : d is a pre-fixed point of f if f d \u2264 d\n\n-- Post-fixed point : d is a post-fixed point of f if d \u2264 f d\n\n-- Fixed-point : d is a fixed-point of f if f d = d\n\n-- Least pre-fixed point : d is the least pre-fixed point of f if\n-- 1. f d \u2264 d -- d is a pre-fixed point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Least fixed-point : d is the least fixed-point of f if\n-- 1. f d = d -- d is a fixed-point of f\n-- 2. \u2200 e. f e \u2264 e \u21d2 d \u2264 e\n\n-- Thm: If d is the least pre-fixed point of f, then d is the least\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n-- Thm: If d is the greatest post-fixed point of f, then d is the greatest\n-- fixed-point of f (\u00c9sik, 2009, p. 31).\n\n------------------------------------------------------------------------------\n-- Auxiliary definitions and properties\n\nflip : {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\nflip f b a = f a b\n\ncong : (f : D \u2192 D) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2261 f y\ncong f refl = refl\n\n------------------------------------------------------------------------------\nmodule LFP where\n\n -- N is a least fixed-point of a functor\n\n -- The functor.\n NatF : (D \u2192 Set) \u2192 D \u2192 Set\n NatF A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n\n -- The natural numbers are the least fixed-point of NatF.\n postulate\n N : D \u2192 Set\n\n -- N is a pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the introduction rules.\n N-in : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n\n -- The higher-order version.\n N-in-ho : \u2200 {n} \u2192 NatF N n \u2192 N n\n\n -- N is the least pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the elimination rule of an inductively\n -- defined predicate.\n N-least-pre-fixed :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\n\n -- Higher-order version (incomplete?).\n N-least-pre-fixed-ho :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (NatF A n \u2192 A n) \u2192\n N n \u2192 A n\n\n ----------------------------------------------------------------------------\n -- From\/to N-in\/N-in-ho.\n\n N-in\u2081 : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n N-in\u2081 = N-in-ho\n\n N-in-ho\u2081 : \u2200 {n} \u2192 NatF N n \u2192 N n\n N-in-ho\u2081 = N-in\u2081\n\n ----------------------------------------------------------------------------\n -- From\/to N-least-pre-fixed\/N-least-pre-fixed-ho\n N-least-pre-fixed' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\n N-least-pre-fixed' = N-least-pre-fixed-ho\n\n N-least-pre-fixed-ho' :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (NatF A n \u2192 A n) \u2192\n N n \u2192 A n\n N-least-pre-fixed-ho' = N-least-pre-fixed\n\n ----------------------------------------------------------------------------\n -- The data constructors of N.\n nzero : N zero\n nzero = N-in (inj\u2081 refl)\n\n nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n nsucc Nn = N-in (inj\u2082 (_ , refl , Nn))\n\n ----------------------------------------------------------------------------\n -- Because N is the least pre-fixed point of NatF (i.e. N-in and\n -- N-ind), we can proof that N is also a post-fixed point of NatF.\n\n -- N is a post-fixed point of NatF.\n N-post-fixed : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n')\n N-post-fixed = N-least-pre-fixed A h\n where\n A : D \u2192 Set\n A m = m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 N m')\n\n h : \u2200 {m} \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 prf) = inj\u2081 prf\n h (inj\u2082 (m' , prf , Am')) = inj\u2082 (m' , prf , helper Am')\n where\n helper : A m' \u2192 N m'\n helper (inj\u2081 prf') = subst N (sym prf') nzero\n helper (inj\u2082 (m'' , prf' , Am'')) = subst N (sym prf') (nsucc Am'')\n\n ----------------------------------------------------------------------------\n -- The induction principle for N *with* the hypothesis N n in the\n -- induction step.\n\n N-ind\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2081 A A0 is {n} Nn = N-least-pre-fixed A h Nn\n where\n h : n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n\n h (inj\u2081 prf) = subst A (sym prf) A0\n h (inj\u2082 (n' , prf , An')) = subst A (sym prf) (is helper An')\n where\n helper : N n'\n helper with N-post-fixed Nn\n ... | inj\u2081 n\u22610 = \u22a5-elim (0\u2262S (trans (sym n\u22610) prf))\n ... | inj\u2082 (m' , prf' , Nm') =\n subst N (succInjective (trans (sym prf') prf)) Nm'\n\n ----------------------------------------------------------------------------\n -- The induction principle for N *without* the hypothesis N n in the\n -- induction step.\n\n N-ind\u2082 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2082 A A0 is {n} = N-least-pre-fixed A h\n where\n h : n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n\n h (inj\u2081 prf) = subst A (sym prf) A0\n h (inj\u2082 (n' , prf , An')) = subst A (sym prf) (is An')\n\n ----------------------------------------------------------------------------\n -- Example: We will use N-least-pre-fixed as the induction principle on N.\n\n postulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 succ\u2081 d + e \u2261 succ\u2081 (d + e)\n\n +-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n +-leftIdentity n = +-0x n\n\n +-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n +-N {m} {n} Nm Nn = N-least-pre-fixed A h Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n h : m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 prf) = subst N (cong (flip _+_ n) (sym prf)) A0\n where\n A0 : A zero\n A0 = subst N (sym (+-leftIdentity n)) Nn\n h (inj\u2082 (m' , m\u2261Sm' , Am')) =\n subst N (cong (flip _+_ n) (sym m\u2261Sm')) (is Am')\n where\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} Ai = subst N (sym (+-Sx i n)) (nsucc Ai)\n\n ----------------------------------------------------------------------------\n -- Example: A proof using N-post-fixed.\n\n pred-N : \u2200 {n} \u2192 N n \u2192 N (pred\u2081 n)\n pred-N {n} Nn = case h\u2081 h\u2082 (N-post-fixed Nn)\n where\n h\u2081 : n \u2261 zero \u2192 N (pred\u2081 n)\n h\u2081 n\u22610 = subst N (sym (trans (predCong n\u22610) pred-0)) nzero\n\n h\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n' \u2192 N (pred\u2081 n)\n h\u2082 (n' , prf , Nn') = subst N (sym (trans (predCong prf) (pred-S n'))) Nn'\n\n ----------------------------------------------------------------------------\n -- From\/to N as a least fixed-point to\/from N as data type.\n\n data N' : D \u2192 Set where\n nzero' : N' zero\n nsucc' : \u2200 {n} \u2192 N' n \u2192 N' (succ\u2081 n)\n\n N'\u2192N : \u2200 {n} \u2192 N' n \u2192 N n\n N'\u2192N nzero' = nzero\n N'\u2192N (nsucc' Nn) = nsucc (N'\u2192N Nn)\n\n -- Using N-ind\u2081.\n N\u2192N' : \u2200 {n} \u2192 N n \u2192 N' n\n N\u2192N' = N-ind\u2081 N' nzero' (\u03bb _ \u2192 nsucc')\n\n -- Using N-ind\u2082.\n N\u2192N'\u2081 : \u2200 {n} \u2192 N n \u2192 N' n\n N\u2192N'\u2081 = N-ind\u2082 N' nzero' nsucc'\n\n------------------------------------------------------------------------------\nmodule Data where\n\n data N : D \u2192 Set where\n nzero : N zero\n nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n\n -- The induction principle for N *with* the hypothesis N n in the\n -- induction step.\n N-ind\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2081 A A0 is nzero = A0\n N-ind\u2081 A A0 is (nsucc Nn) = is Nn (N-ind\u2081 A A0 is Nn)\n\n -- The induction principle for N *without* the hypothesis N n in the\n -- induction step.\n N-ind\u2082 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2082 A A0 h nzero = A0\n N-ind\u2082 A A0 h (nsucc Nn) = h (N-ind\u2082 A A0 h Nn)\n\n ----------------------------------------------------------------------------\n -- N-in.\n\n N-in : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n N-in {n} h = case prf\u2081 prf\u2082 h\n where\n prf\u2081 : n \u2261 zero \u2192 N n\n prf\u2081 n\u22610 = subst N (sym n\u22610) nzero\n\n prf\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n' \u2192 N n\n prf\u2082 (n' , prf , Nn') = subst N (sym prf) (nsucc Nn')\n\n ----------------------------------------------------------------------------\n -- From N-ind\u2082 to N-least-pre-fixed.\n\n N\u21920\u2228S : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n')\n N\u21920\u2228S = N-ind\u2082 A A0 is\n where\n A : D \u2192 Set\n A i = i \u2261 zero \u2228 (\u2203[ i' ] i \u2261 succ\u2081 i' \u2227 N i')\n\n A0 : A zero\n A0 = inj\u2081 refl\n\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} Ai = case prf\u2081 prf\u2082 Ai\n where\n prf\u2081 : i \u2261 zero \u2192 succ\u2081 i \u2261 zero \u2228 (\u2203[ i' ] succ\u2081 i \u2261 succ\u2081 i' \u2227 N i')\n prf\u2081 h' = inj\u2082 (i , refl , (subst N (sym h') nzero))\n\n prf\u2082 : \u2203[ i' ] i \u2261 succ\u2081 i' \u2227 N i' \u2192\n succ\u2081 i \u2261 zero \u2228 (\u2203[ i' ] succ\u2081 i \u2261 succ\u2081 i' \u2227 N i')\n prf\u2082 (i' , prf , Ni') = inj\u2082 (i , refl , subst N (sym prf) (nsucc Ni'))\n\n N-least-pre-fixed\u2082 :\n \u2200 (A : D \u2192 Set) {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n N n \u2192 A n\n N-least-pre-fixed\u2082 A {n} h Nn = case prf\u2081 prf\u2082 (N\u21920\u2228S Nn)\n where\n prf\u2081 : n \u2261 zero \u2192 A n\n prf\u2081 n\u22610 = h (inj\u2081 n\u22610)\n\n prf\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n' \u2192 A n\n prf\u2082 (n' , prf , Nn') = h (inj\u2082 (n' , prf , {!!}))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"bdda2e0a702f91fb4c31af12c06276f4be5a20a5","subject":"Showed that the associator is an iso.","message":"Showed that the associator is an iso.\n","repos":"heades\/AUGL","old_file":"dialectica-cats\/DC2Sets.agda","new_file":"dialectica-cats\/DC2Sets.agda","new_contents":"-----------------------------------------------------------------------\n-- This file defines DC\u2082(Sets) and its SMC structure. --\n-----------------------------------------------------------------------\nmodule DC2Sets where\n\nopen import prelude\n\n-- The objects:\nObj : Set\u2081\nObj = \u03a3[ U \u2208 Set ] (\u03a3[ X \u2208 Set ] (U \u2192 X \u2192 Set))\n\n-- The morphisms:\nHom : Obj \u2192 Obj \u2192 Set\nHom (U , X , \u03b1) (V , Y , \u03b2) =\n \u03a3[ f \u2208 (U \u2192 V) ]\n (\u03a3[ F \u2208 (U \u2192 Y \u2192 X) ] (\u2200{u : U}{y : Y} \u2192 \u03b1 u (F u y) \u2192 \u03b2 (f u) y))\n\n-- Composition:\ncomp : {A B C : Obj} \u2192 Hom A B \u2192 Hom B C \u2192 Hom A C\ncomp {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)} (f , F , p\u2081) (g , G , p\u2082) =\n (g \u2218 f , (\u03bb u z \u2192 F u (G (f u) z)), (\u03bb {u} {y} p\u2083 \u2192 p\u2082 (p\u2081 p\u2083)))\n\ninfixl 5 _\u25cb_\n\n_\u25cb_ = comp\n\n-- The contravariant hom-functor:\nHom\u2090 : {A' A B B' : Obj} \u2192 Hom A' A \u2192 Hom B B' \u2192 Hom A B \u2192 Hom A' B'\nHom\u2090 f h g = comp f (comp g h)\n\n-- The identity function:\nid : {A : Obj} \u2192 Hom A A \nid {(U , V , \u03b1)} = (id-set , curry snd , id-set)\n\n-- In this formalization we will only worry about proving that the\n-- data of morphisms are equivalent, and not worry about the morphism\n-- conditions. This will make proofs shorter and faster.\n--\n-- If we have parallel morphisms (f,F) and (g,G) in which we know that\n-- f = g and F = G, then the condition for (f,F) will imply the\n-- condition of (g,G) and vice versa. Thus, we can safely ignore it.\ninfix 4 _\u2261h_\n\n_\u2261h_ : {A B : Obj} \u2192 (f g : Hom A B) \u2192 Set\n_\u2261h_ {(U , X , \u03b1)}{(V , Y , \u03b2)} (f , F , p\u2081) (g , G , p\u2082) = f \u2261 g \u00d7 F \u2261 G\n\n\u2261h-refl : {A B : Obj}{f : Hom A B} \u2192 f \u2261h f\n\u2261h-refl {U , X , \u03b1}{V , Y , \u03b2}{f , F , _} = refl , refl\n\n\u2261h-trans : \u2200{A B}{f g h : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h h \u2192 f \u2261h h\n\u2261h-trans {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _}{h , H , _} (p\u2081 , p\u2082) (p\u2083 , p\u2084) rewrite p\u2081 | p\u2082 | p\u2083 | p\u2084 = refl , refl\n\n\u2261h-sym : \u2200{A B}{f g : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h f\n\u2261h-sym {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\n\u2261h-subst-\u25cb : \u2200{A B C}{f\u2081 f\u2082 : Hom A B}{g\u2081 g\u2082 : Hom B C}{j : Hom A C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 g\u2081 \u2261h g\u2082\n \u2192 f\u2082 \u25cb g\u2082 \u2261h j\n \u2192 f\u2081 \u25cb g\u2081 \u2261h j\n\u2261h-subst-\u25cb {U , X , \u03b1}\n {V , Y , \u03b2}\n {W , Z , \u03b3}\n {f\u2081 , F\u2081 , _}\n {f\u2082 , F\u2082 , _}\n {g\u2081 , G\u2081 , _}\n {g\u2082 , G\u2082 , _}\n {j , J , _}\n (p\u2085 , p\u2086) (p\u2087 , p\u2088) (p\u2089 , p\u2081\u2080) rewrite p\u2085 | p\u2086 | p\u2087 | p\u2088 | p\u2089 | p\u2081\u2080 = refl , refl\n\n\u25cb-assoc : \u2200{A B C D}{f : Hom A B}{g : Hom B C}{h : Hom C D}\n \u2192 f \u25cb (g \u25cb h) \u2261h (f \u25cb g) \u25cb h\n\u25cb-assoc {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{S , T , \u03b9}\n {f , F , _}{g , G , _}{h , H , _} = refl , refl\n\n\u25cb-idl : \u2200{A B}{f : Hom A B} \u2192 id \u25cb f \u2261h f\n\u25cb-idl {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\u25cb-idr : \u2200{A B}{f : Hom A B} \u2192 f \u25cb id \u2261h f\n\u25cb-idr {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\n-----------------------------------------------------------------------\n-- DC\u2082(Sets) is a SMC --\n-----------------------------------------------------------------------\n\n-- The tensor functor: \u2297\n_\u2297\u1d63_ : \u2200{U X V Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 ((U \u00d7 V) \u2192 (X \u00d7 Y) \u2192 Set)\n_\u2297\u1d63_ \u03b1 \u03b2 (u , v) (x , y) = (\u03b1 u x) \u00d7 (\u03b2 v y)\n\n_\u2297\u2092_ : (A B : Obj) \u2192 Obj\n(U , X , \u03b1) \u2297\u2092 (V , Y , \u03b2) = ((U \u00d7 V) , (X \u00d7 Y) , \u03b1 \u2297\u1d63 \u03b2)\n\nF\u2297 : \u2200{Z T V X U Y : Set}{F : U \u2192 Z \u2192 X}{G : V \u2192 T \u2192 Y} \u2192 (U \u00d7 V) \u2192 (Z \u00d7 T) \u2192 (X \u00d7 Y)\nF\u2297 {F = F}{G} (u , v) (z , t) = F u z , G v t\n \n_\u2297\u2090_ : {A B C D : Obj} \u2192 Hom A C \u2192 Hom B D \u2192 Hom (A \u2297\u2092 B) (C \u2297\u2092 D)\n_\u2297\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) = \u27e8 f , g \u27e9 , F\u2297 {F = F}{G} , p\u2297\n where\n p\u2297 : {u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Z (\u03bb x \u2192 T)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (F\u2297 {F = F}{G} u y) \u2192 (\u03b3 \u2297\u1d63 \u03b4) (\u27e8 f , g \u27e9 u) y\n p\u2297 {u , v}{z , t} (p\u2083 , p\u2084) = p\u2081 p\u2083 , p\u2082 p\u2084\n\n\n-- The unit for tensor:\n\u03b9 : \u22a4 \u2192 \u22a4 \u2192 Set\n\u03b9 triv triv = \u22a4\n\nI : Obj\nI = (\u22a4 , \u22a4 , \u03b9)\n\nJ : Obj\nJ = (\u22a4 , \u22a4 , (\u03bb x y \u2192 \u22a5))\n\n\n-- The left-unitor:\n\u03bb\u2297-p : \u2200{U X \u03b1}{u : \u03a3 \u22a4 (\u03bb x \u2192 U)} {y : X} \u2192 (\u03b9 \u2297\u1d63 \u03b1) u (triv , y) \u2192 \u03b1 (snd u) y\n\u03bb\u2297-p {U}{X}{\u03b1}{(triv , u)}{x} = snd\n \n\u03bb\u2297 : \u2200{A : Obj} \u2192 Hom (I \u2297\u2092 A) A\n\u03bb\u2297 {(U , X , \u03b1)} = snd , (\u03bb _ x \u2192 triv , x) , \u03bb\u2297-p\n\n\u03bb\u2297-inv : \u2200{A : Obj} \u2192 Hom A (I \u2297\u2092 A)\n\u03bb\u2297-inv {(U , X , \u03b1)} = (\u03bb u \u2192 triv , u) , (\u03bb _ r \u2192 snd r) , \u03bb\u2297-inv-p\n where\n \u03bb\u2297-inv-p : \u2200{U X \u03b1}{u : U} {y : \u03a3 \u22a4 (\u03bb x \u2192 X)} \u2192 \u03b1 u (snd y) \u2192 (\u03b9 \u2297\u1d63 \u03b1) (triv , u) y\n \u03bb\u2297-inv-p {U}{X}{\u03b1}{u}{triv , x} p = triv , p\n\n-- The right-unitor:\n\u03c1\u2297 : \u2200{A : Obj} \u2192 Hom (A \u2297\u2092 I) A\n\u03c1\u2297 {(U , X , \u03b1)} = fst , (\u03bb r x \u2192 x , triv) , \u03c1\u2297-p\n where\n \u03c1\u2297-p : \u2200{U X \u03b1}{u : \u03a3 U (\u03bb x \u2192 \u22a4)} {y : X} \u2192 (\u03b1 \u2297\u1d63 \u03b9) u (y , triv) \u2192 \u03b1 (fst u) y\n \u03c1\u2297-p {U}{X}{\u03b1}{(u , _)}{x} (p , _) = p\n\n\n\u03c1\u2297-inv : \u2200{A : Obj} \u2192 Hom A (A \u2297\u2092 I)\n\u03c1\u2297-inv {(U , X , \u03b1)} = (\u03bb u \u2192 u , triv) , (\u03bb u r \u2192 fst r) , \u03c1\u2297-p-inv\n where\n \u03c1\u2297-p-inv : \u2200{U X \u03b1}{u : U} {y : \u03a3 X (\u03bb x \u2192 \u22a4)} \u2192 \u03b1 u (fst y) \u2192 (\u03b1 \u2297\u1d63 \u03b9) (u , triv) y\n \u03c1\u2297-p-inv {U}{X}{\u03b1}{u}{x , triv} p = p , triv\n\n-- Symmetry:\n\u03b2\u2297 : \u2200{A B : Obj} \u2192 Hom (A \u2297\u2092 B) (B \u2297\u2092 A)\n\u03b2\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)} = twist-\u00d7 , (\u03bb r\u2081 r\u2082 \u2192 twist-\u00d7 r\u2082) , \u03b2\u2297-p\n where\n \u03b2\u2297-p : \u2200{U V Y X \u03b1 \u03b2}{u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Y (\u03bb x \u2192 X)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (twist-\u00d7 y) \u2192 (\u03b2 \u2297\u1d63 \u03b1) (twist-\u00d7 u) y\n \u03b2\u2297-p {U}{V}{Y}{X}{\u03b1}{\u03b2}{u , v}{y , x} = twist-\u00d7\n\n\n-- The associator:\nF\u03b1-inv : \u2200{\u2113}{U V W X Y Z : Set \u2113} \u2192 (U \u00d7 (V \u00d7 W)) \u2192 ((X \u00d7 Y) \u00d7 Z) \u2192 (X \u00d7 (Y \u00d7 Z))\nF\u03b1-inv (u , (v , w)) ((x , y) , z) = x , y , z\n \n\u03b1\u2297-inv : \u2200{A B C : Obj} \u2192 Hom (A \u2297\u2092 (B \u2297\u2092 C)) ((A \u2297\u2092 B) \u2297\u2092 C)\n\u03b1\u2297-inv {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = rl-assoc-\u00d7 , F\u03b1-inv , \u03b1-inv-cond\n where\n \u03b1-inv-cond : {u : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))}{y : \u03a3 (\u03a3 X (\u03bb x \u2192 Y)) (\u03bb x \u2192 Z)}\n \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) u (F\u03b1-inv u y)\n \u2192 ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) (rl-assoc-\u00d7 u) y\n \u03b1-inv-cond {u , (v , w)}{(x , y) , z} (p\u2081 , (p\u2082 , p\u2083)) = (p\u2081 , p\u2082) , p\u2083\n\n\nF\u03b1 : \u2200{V W X Y U Z : Set} \u2192 ((U \u00d7 V) \u00d7 W) \u2192 (X \u00d7 (Y \u00d7 Z)) \u2192 ((X \u00d7 Y) \u00d7 Z)\nF\u03b1 {V}{W}{X}{Y}{U}{Z} ((u , v) , w) (x , (y , z)) = (x , y) , z\n\n\u03b1\u2297 : \u2200{A B C : Obj} \u2192 Hom ((A \u2297\u2092 B) \u2297\u2092 C) (A \u2297\u2092 (B \u2297\u2092 C)) \n\u03b1\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = (lr-assoc-\u00d7 , F\u03b1 , \u03b1-cond)\n where\n \u03b1-cond : {u : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)}\n {y : \u03a3 X (\u03bb x \u2192 \u03a3 Y (\u03bb x\u2081 \u2192 Z))} \u2192\n ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) u (F\u03b1 u y) \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) (lr-assoc-\u00d7 u) y\n \u03b1-cond {(u , v) , w}{x , (y , z)} ((p\u2081 , p\u2082) , p\u2083) = p\u2081 , p\u2082 , p\u2083\n\n\u03b1\u2297-id\u2081 : \u2200{A B C} \u2192 (\u03b1\u2297 {A}{B}{C}) \u25cb \u03b1\u2297-inv \u2261h id\n\u03b1\u2297-id\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)} \u2192 rl-assoc-\u00d7 (lr-assoc-\u00d7 a) \u2261 a\n aux {(u , v) , w} = refl\n\n aux' : {a : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)} \u2192 (\u03bb z \u2192 F\u03b1 {V}{W}{X}{Y}{U}{Z} a (F\u03b1-inv (lr-assoc-\u00d7 a) z)) \u2261 (\u03bb y \u2192 y)\n aux' {(u , v), w} = ext-set aux''\n where\n aux'' : {a : \u03a3 (\u03a3 X (\u03bb x \u2192 Y)) (\u03bb x \u2192 Z)} \u2192 F\u03b1 ((u , v) , w) (F\u03b1-inv (u , v , w) a) \u2261 a\n aux'' {(x , y) , z} = refl\n\n\u03b1\u2297-id\u2082 : \u2200{A B C} \u2192 (\u03b1\u2297-inv {A}{B}{C}) \u25cb \u03b1\u2297 \u2261h id\n\u03b1\u2297-id\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))} \u2192 lr-assoc-\u00d7 (rl-assoc-\u00d7 a) \u2261 a\n aux {u , (v , w)} = refl\n aux' : {a : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))} \u2192 (\u03bb z \u2192 F\u03b1-inv {_}{U}{V}{W}{X}{Y}{Z} a (F\u03b1 (rl-assoc-\u00d7 a) z)) \u2261 (\u03bb y \u2192 y)\n aux' {u , (v , w)} = ext-set aux''\n where\n aux'' : {a : \u03a3 X (\u03bb x \u2192 \u03a3 Y (\u03bb x\u2081 \u2192 Z))} \u2192 F\u03b1-inv (u , v , w) (F\u03b1 ((u , v) , w) a) \u2261 a\n aux'' {x , (y , z)} = refl\n{- \n-- Internal hom:\n\u22b8-cond : \u2200{U V X Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 (U \u2192 V) \u00d7 (Y \u2192 X) \u2192 U \u00d7 Y \u2192 Set\n\u22b8-cond \u03b1 \u03b2 (f , g) (u , y) = \u03b1 u (g y) \u2192 \u03b2 (f u) y\n\n_\u22b8\u2092_ : Obj \u2192 Obj \u2192 Obj\n(U , X , \u03b1) \u22b8\u2092 (V , Y , \u03b2) = ((U \u2192 V) \u00d7 (Y \u2192 X)) , (U \u00d7 Y) , \u22b8-cond \u03b1 \u03b2\n\n_\u22b8\u2090_ : {A B C D : Obj} \u2192 Hom C A \u2192 Hom B D \u2192 Hom (A \u22b8\u2092 B) (C \u22b8\u2092 D)\n_\u22b8\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) =\n h , H , p\u2083\n where\n h : \u03a3 (U \u2192 V) (\u03bb x \u2192 Y \u2192 X) \u2192 \u03a3 (W \u2192 S) (\u03bb x \u2192 T \u2192 Z)\n h (h\u2081 , h\u2082) = (\u03bb w \u2192 g (h\u2081 (f w))) , (\u03bb t \u2192 F (h\u2082 (G t)))\n H : \u03a3 W (\u03bb x \u2192 T) \u2192 \u03a3 U (\u03bb x \u2192 Y)\n H (w , t) = f w , G t\n p\u2083 : {u : \u03a3 (U \u2192 V) (\u03bb x \u2192 Y \u2192 X)} {y : \u03a3 W (\u03bb x \u2192 T)} \u2192 \u22b8-cond \u03b1 \u03b2 u (H y) \u2192 \u22b8-cond \u03b3 \u03b4 (h u) y\n p\u2083 {h\u2081 , h\u2082}{w , t} c c' = p\u2082 (c (p\u2081 c'))\n\ncur : {A B C : Obj}\n \u2192 Hom (A \u2297\u2092 B) C\n \u2192 Hom A (B \u22b8\u2092 C)\ncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb u \u2192 (\u03bb v \u2192 f (u , v)) , (\u03bb z \u2192 snd (F z) u)) , (\u03bb p \u2192 fst (F (snd p)) (fst p)) , cur-cond\n where\n cur-cond : \u2200{u : U}{y : \u03a3 V (\u03bb x \u2192 Z)}\n \u2192 \u03b1 u (fst (F (snd y)) (fst y))\n \u2192 \u22b8-cond \u03b2 \u03b3 ((\u03bb v \u2192 f (u , v)) , (\u03bb z \u2192 snd (F z) u)) y\n cur-cond {u}{v , z} p\u2082 p\u2083 with p\u2081 {u , v}{z} \n ... | p\u2081' with F z\n ... | (j\u2081 , j\u2082) = p\u2081' (p\u2082 , p\u2083)\n\ncur-\u2261h : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-\u2261h {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}\n {f\u2081 , F\u2081 , p\u2081}{f\u2082 , F\u2082 , p\u2082} (p\u2083 , p\u2084)\n rewrite p\u2083 | p\u2084 = refl , refl\n\ncur-cong : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C} \u2192 f\u2081 \u2261h f\u2082 \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-cong {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)}{f\u2081 , F\u2081 , _}{f\u2082 , F\u2082 , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\nuncur : {A B C : Obj}\n \u2192 Hom A (B \u22b8\u2092 C)\n \u2192 Hom (A \u2297\u2092 B) C\nuncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb p \u2192 fst (f (fst p)) (snd p)) , (\u03bb z \u2192 (\u03bb v \u2192 F (v , z)) , (\u03bb u \u2192 snd (f u) z)) , uncur-cond\n where\n uncur-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : Z}\n \u2192 (\u03b1 \u2297\u1d63 \u03b2) u ((\u03bb v \u2192 F (v , y)) , (\u03bb u\u2081 \u2192 snd (f u\u2081) y))\n \u2192 \u03b3 (fst (f (fst u)) (snd u)) y\n uncur-cond {u , v}{z} (p\u2082 , p\u2083) with p\u2081 {u}{v , z} p\u2082\n ... | p\u2081' with f u\n ... | (j\u2081 , j\u2082) = p\u2081' p\u2083\n\ncur-uncur-bij\u2081 : \u2200{A B C}{f : Hom (A \u2297\u2092 B) C}\n \u2192 uncur (cur f) \u2261h f\ncur-uncur-bij\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{f , F , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : \u03a3 U (\u03bb x \u2192 V)} \u2192 f (fst a , snd a) \u2261 f a\n aux\u2081 {u , v} = refl\n \n aux\u2082 : {a : Z} \u2192 ((\u03bb v \u2192 fst (F a) v) , (\u03bb u \u2192 snd (F a) u)) \u2261 F a\n aux\u2082 {z} with F z\n ... | j\u2081 , j\u2082 = refl\n\ncur-uncur-bij\u2082 : \u2200{A B C}{g : Hom A (B \u22b8\u2092 C)}\n \u2192 cur (uncur g) \u2261h g\ncur-uncur-bij\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{g , G , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : U} \u2192 ((\u03bb v \u2192 fst (g a) v) , (\u03bb z \u2192 snd (g a) z)) \u2261 g a\n aux\u2081 {u} with g u\n ... | (j\u2081 , j\u2082) = refl\n\n aux\u2082 : {a : \u03a3 V (\u03bb x \u2192 Z)} \u2192 G (fst a , snd a) \u2261 G a\n aux\u2082 {v , z} = refl\n \n-- The of-course exponential:\n!\u2092-cond : \u2200{U X : Set}\n \u2192 (U \u2192 X \u2192 Set)\n \u2192 U\n \u2192 (U \u2192 X *)\n \u2192 Set\n!\u2092-cond \u03b1 u f = all-pred (\u03b1 u) (f u)\n \n!\u2092 : Obj \u2192 Obj\n!\u2092 (U , X , \u03b1) = U , (U \u2192 X *) , !\u2092-cond \u03b1\n\n!-cta : {V Y U X : Set}\n \u2192 (Y \u2192 X)\n \u2192 (U \u2192 V)\n \u2192 (V \u2192 Y *)\n \u2192 (U \u2192 X *)\n!-cta F f g = \u03bb u \u2192 list-funct F (g (f u))\n\n!\u2090-cond : \u2200{U V Y X : Set}{F : Y \u2192 X}{f : U \u2192 V}\n \u2192 (\u03b1 : U \u2192 X \u2192 Set)\n \u2192 (\u03b2 : V \u2192 Y \u2192 Set)\n \u2192 (p : {u : U} {y : Y} \u2192 \u03b1 u (F y) \u2192 \u03b2 (f u) y)\n {u : U}{l : Y *}\n \u2192 all-pred (\u03b1 u) (list-funct F l)\n \u2192 all-pred (\u03b2 (f u)) l\n!\u2090-cond _ _ _ {l = []} _ = triv\n!\u2090-cond \u03b1 \u03b2 p {u}{x :: xs} (p' , p'') = p p' , !\u2090-cond \u03b1 \u03b2 p p'' \n \n!\u2090 : {A B : Obj} \u2192 Hom A B \u2192 Hom (!\u2092 A) (!\u2092 B)\n!\u2090 {U , X , \u03b1}{V , Y , \u03b2} (f , F , p) = f , !-cta F f , !\u2090-cond \u03b1 \u03b2 p\n\n-- Of-course is a comonad:\n\u03b5 : \u2200{A} \u2192 Hom (!\u2092 A) A\n\u03b5 {U , X , \u03b1} = id-set , (\u03bb x y \u2192 [ x ]) , fst\n\n\u03b4-cta : {U X : Set} \u2192 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X)) \u2192 U \u2192 \ud835\udd43 X\n\u03b4-cta g u = foldr (\u03bb f rest \u2192 (f u) ++ rest) [] (g u)\n \n\u03b4 : \u2200{A} \u2192 Hom (!\u2092 A) (!\u2092 (!\u2092 A))\n\u03b4 {U , X , \u03b1} = id-set , \u03b4-cta , \u03b4-cond\n where\n \u03b4-cond : {u : U} {l : \ud835\udd43 (U \u2192 \ud835\udd43 X)}\n \u2192 all-pred (\u03b1 u) (foldr (\u03bb f \u2192 _++_ (f u)) [] l)\n \u2192 all-pred (\u03bb f\n \u2192 all-pred (\u03b1 u) (f u)) l\n \u03b4-cond {l = []} _ = triv\n \u03b4-cond {u}{l = x :: l'} p with\n all-pred-append {X}{\u03b1 u}\n {x u}\n {foldr (\u03bb f \u2192 _++_ (f u)) [] l'}\n \u2227-unit \u2227-assoc\n ... | p' rewrite p' = fst p , \u03b4-cond {u} {l'} (snd p)\n\n-- These diagrams can be found on page 22 of the report.\ncomonand-diag\u2081 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (!\u2090 (\u03b4 {A})) \u2261h (\u03b4 {A}) \u25cb (\u03b4 { !\u2092 A})\ncomonand-diag\u2081 {U , X , \u03b1} =\n refl , ext-set (\u03bb {a} \u2192 ext-set (\u03bb {a\u2081} \u2192 aux {a\u2081}{a a\u2081}))\n where\n aux : \u2200{a\u2081 : U}{l : \ud835\udd43 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X))} \u2192\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) []\n (map (\u03bb g u \u2192 foldr (\u03bb f \u2192 _++_ (f u)) [] (g u)) l)\n \u2261\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] (foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] l)\n aux {a}{[]} = refl \n aux {a}{x :: l} rewrite\n sym (foldr-append {l\u2081 = x a}{foldr (\u03bb f \u2192 _++_ (f a)) [] l}{a})\n = cong2 {a = foldr (\u03bb f \u2192 _++_ (f a)) [] (x a)}\n _++_\n refl\n (aux {a}{l})\n\ncomonand-diag\u2082 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (\u03b5 { !\u2092 A}) \u2261h (\u03b4 {A}) \u25cb (!\u2090 (\u03b5 {A}))\ncomonand-diag\u2082 {U , X , \u03b1} =\n refl , ext-set (\u03bb {f} \u2192 ext-set (\u03bb {a} \u2192 aux {a}{f a}))\n where\n aux : \u2200{a : U}{l : X *}\n \u2192 l ++ [] \u2261 foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x y \u2192 x :: []) l)\n aux {a}{[]} = refl\n aux {a}{x :: l} with aux {a}{l}\n ... | IH rewrite ++[] l =\n cong2 {a = x} {x} {l}\n {foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x\u2081 y \u2192 x\u2081 :: []) l)} _::_ refl\n IH\n \nmodule Cartesian where\n \u03c0\u2081 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (U , X , \u03b1))\n \u03c0\u2081 {U}{X}{V}{Y}{\u03b1}{\u03b2} = fst , (\u03bb f \u2192 (\u03bb v u \u2192 f u) , (\u03bb u v \u2192 [])) , \u03c0\u2081-cond\n where\n \u03c0\u2081-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : U \u2192 \ud835\udd43 X} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 y u\u2081) , (\u03bb u\u2081 v \u2192 [])) \u2192\n all-pred (\u03b1 (fst u)) (y (fst u))\n \u03c0\u2081-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2081\n\n \u03c0\u2082 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (V , Y , \u03b2))\n \u03c0\u2082 {U}{X}{V}{Y}{\u03b1}{\u03b2} = snd , (\u03bb f \u2192 (\u03bb v u \u2192 []) , (\u03bb u v \u2192 f v)) , \u03c0\u2082-cond\n where\n \u03c0\u2082-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : V \u2192 \ud835\udd43 Y} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 []) , (\u03bb u\u2081 v \u2192 y v)) \u2192\n all-pred (\u03b2 (snd u)) (y (snd u))\n \u03c0\u2082-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2082\n\n cart-ar-crt : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y) \u2192 W \u2192 \ud835\udd43 Z\n cart-ar-crt (f , F , p\u2081) (g , G , p\u2082) (j\u2081 , j\u2082) w = F (j\u2081 (g w)) w ++ G (j\u2082 (f w)) w\n\n cart-ar : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))\n cart-ar {U}{X}{V}{Y}{W}{Z}{\u03b1}{\u03b2}{\u03b3} (f , F , p\u2081) (g , G , p\u2082)\n = (\u03bb w \u2192 f w , g w) , cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) , cart-ar-cond\n where\n cart-ar-cond : \u2200{u : W} {y : \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y)} \u2192\n all-pred (\u03b3 u) (cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) y u) \u2192\n ((\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b1 u\u2081) (f\u2081 u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b2 u\u2081) (f\u2081 u\u2081)))\n (f u , g u) y\n cart-ar-cond {w}{j\u2081 , j\u2082} p\n rewrite\n all-pred-append {f = \u03b3 w}{F (j\u2081 (g w)) w}{G (j\u2082 (f w)) w} \u2227-unit \u2227-assoc with p\n ... | (a , b) = p\u2081 a , p\u2082 b\n\n cart-diag\u2081 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (U , X , \u03b1)}\n (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (U , X , \u03b1)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2081)\n cart-diag\u2081 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 sym (++[] (map F (j (f w))))))\n\n cart-diag\u2082 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (V , Y , \u03b2)}\n (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (V , Y , \u03b2)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2082)\n cart-diag\u2082 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 refl))\n-}\n","old_contents":"-----------------------------------------------------------------------\n-- This file defines DC\u2082(Sets) and its SMC structure. --\n-----------------------------------------------------------------------\nmodule DC2Sets where\n\nopen import prelude\n\n-- The objects:\nObj : Set\u2081\nObj = \u03a3[ U \u2208 Set ] (\u03a3[ X \u2208 Set ] (U \u2192 X \u2192 Set))\n\n-- The morphisms:\nHom : Obj \u2192 Obj \u2192 Set\nHom (U , X , \u03b1) (V , Y , \u03b2) =\n \u03a3[ f \u2208 (U \u2192 V) ]\n (\u03a3[ F \u2208 (U \u2192 Y \u2192 X) ] (\u2200{u : U}{y : Y} \u2192 \u03b1 u (F u y) \u2192 \u03b2 (f u) y))\n\n-- Composition:\ncomp : {A B C : Obj} \u2192 Hom A B \u2192 Hom B C \u2192 Hom A C\ncomp {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)} (f , F , p\u2081) (g , G , p\u2082) =\n (g \u2218 f , (\u03bb u z \u2192 F u (G (f u) z)), (\u03bb {u} {y} p\u2083 \u2192 p\u2082 (p\u2081 p\u2083)))\n\ninfixl 5 _\u25cb_\n\n_\u25cb_ = comp\n\n-- The contravariant hom-functor:\nHom\u2090 : {A' A B B' : Obj} \u2192 Hom A' A \u2192 Hom B B' \u2192 Hom A B \u2192 Hom A' B'\nHom\u2090 f h g = comp f (comp g h)\n\n-- The identity function:\nid : {A : Obj} \u2192 Hom A A \nid {(U , V , \u03b1)} = (id-set , curry snd , id-set)\n\n-- In this formalization we will only worry about proving that the\n-- data of morphisms are equivalent, and not worry about the morphism\n-- conditions. This will make proofs shorter and faster.\n--\n-- If we have parallel morphisms (f,F) and (g,G) in which we know that\n-- f = g and F = G, then the condition for (f,F) will imply the\n-- condition of (g,G) and vice versa. Thus, we can safely ignore it.\ninfix 4 _\u2261h_\n\n_\u2261h_ : {A B : Obj} \u2192 (f g : Hom A B) \u2192 Set\n_\u2261h_ {(U , X , \u03b1)}{(V , Y , \u03b2)} (f , F , p\u2081) (g , G , p\u2082) = f \u2261 g \u00d7 F \u2261 G\n\n\u2261h-refl : {A B : Obj}{f : Hom A B} \u2192 f \u2261h f\n\u2261h-refl {U , X , \u03b1}{V , Y , \u03b2}{f , F , _} = refl , refl\n\n\u2261h-trans : \u2200{A B}{f g h : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h h \u2192 f \u2261h h\n\u2261h-trans {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _}{h , H , _} (p\u2081 , p\u2082) (p\u2083 , p\u2084) rewrite p\u2081 | p\u2082 | p\u2083 | p\u2084 = refl , refl\n\n\u2261h-sym : \u2200{A B}{f g : Hom A B} \u2192 f \u2261h g \u2192 g \u2261h f\n\u2261h-sym {U , X , \u03b1}{V , Y , \u03b2}{f , F , _}{g , G , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\n\u2261h-subst-\u25cb : \u2200{A B C}{f\u2081 f\u2082 : Hom A B}{g\u2081 g\u2082 : Hom B C}{j : Hom A C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 g\u2081 \u2261h g\u2082\n \u2192 f\u2082 \u25cb g\u2082 \u2261h j\n \u2192 f\u2081 \u25cb g\u2081 \u2261h j\n\u2261h-subst-\u25cb {U , X , \u03b1}\n {V , Y , \u03b2}\n {W , Z , \u03b3}\n {f\u2081 , F\u2081 , _}\n {f\u2082 , F\u2082 , _}\n {g\u2081 , G\u2081 , _}\n {g\u2082 , G\u2082 , _}\n {j , J , _}\n (p\u2085 , p\u2086) (p\u2087 , p\u2088) (p\u2089 , p\u2081\u2080) rewrite p\u2085 | p\u2086 | p\u2087 | p\u2088 | p\u2089 | p\u2081\u2080 = refl , refl\n\n\u25cb-assoc : \u2200{A B C D}{f : Hom A B}{g : Hom B C}{h : Hom C D}\n \u2192 f \u25cb (g \u25cb h) \u2261h (f \u25cb g) \u25cb h\n\u25cb-assoc {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{S , T , \u03b9}\n {f , F , _}{g , G , _}{h , H , _} = refl , refl\n\n\u25cb-idl : \u2200{A B}{f : Hom A B} \u2192 id \u25cb f \u2261h f\n\u25cb-idl {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\u25cb-idr : \u2200{A B}{f : Hom A B} \u2192 f \u25cb id \u2261h f\n\u25cb-idr {U , X , _}{V , Y , _}{f , F , _} = refl , refl\n\n\n-----------------------------------------------------------------------\n-- DC\u2082(Sets) is a SMC --\n-----------------------------------------------------------------------\n\n-- The tensor functor: \u2297\n_\u2297\u1d63_ : \u2200{U X V Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 ((U \u00d7 V) \u2192 (X \u00d7 Y) \u2192 Set)\n_\u2297\u1d63_ \u03b1 \u03b2 (u , v) (x , y) = (\u03b1 u x) \u00d7 (\u03b2 v y)\n\n_\u2297\u2092_ : (A B : Obj) \u2192 Obj\n(U , X , \u03b1) \u2297\u2092 (V , Y , \u03b2) = ((U \u00d7 V) , (X \u00d7 Y) , \u03b1 \u2297\u1d63 \u03b2)\n\nF\u2297 : \u2200{Z T V X U Y : Set}{F : U \u2192 Z \u2192 X}{G : V \u2192 T \u2192 Y} \u2192 (U \u00d7 V) \u2192 (Z \u00d7 T) \u2192 (X \u00d7 Y)\nF\u2297 {F = F}{G} (u , v) (z , t) = F u z , G v t\n \n_\u2297\u2090_ : {A B C D : Obj} \u2192 Hom A C \u2192 Hom B D \u2192 Hom (A \u2297\u2092 B) (C \u2297\u2092 D)\n_\u2297\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) = \u27e8 f , g \u27e9 , F\u2297 {F = F}{G} , p\u2297\n where\n p\u2297 : {u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Z (\u03bb x \u2192 T)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (F\u2297 {F = F}{G} u y) \u2192 (\u03b3 \u2297\u1d63 \u03b4) (\u27e8 f , g \u27e9 u) y\n p\u2297 {u , v}{z , t} (p\u2083 , p\u2084) = p\u2081 p\u2083 , p\u2082 p\u2084\n\n\n-- The unit for tensor:\n\u03b9 : \u22a4 \u2192 \u22a4 \u2192 Set\n\u03b9 triv triv = \u22a4\n\nI : Obj\nI = (\u22a4 , \u22a4 , \u03b9)\n\nJ : Obj\nJ = (\u22a4 , \u22a4 , (\u03bb x y \u2192 \u22a5))\n\n\n-- The left-unitor:\n\u03bb\u2297-p : \u2200{U X \u03b1}{u : \u03a3 \u22a4 (\u03bb x \u2192 U)} {y : X} \u2192 (\u03b9 \u2297\u1d63 \u03b1) u (triv , y) \u2192 \u03b1 (snd u) y\n\u03bb\u2297-p {U}{X}{\u03b1}{(triv , u)}{x} = snd\n \n\u03bb\u2297 : \u2200{A : Obj} \u2192 Hom (I \u2297\u2092 A) A\n\u03bb\u2297 {(U , X , \u03b1)} = snd , (\u03bb _ x \u2192 triv , x) , \u03bb\u2297-p\n\n\u03bb\u2297-inv : \u2200{A : Obj} \u2192 Hom A (I \u2297\u2092 A)\n\u03bb\u2297-inv {(U , X , \u03b1)} = (\u03bb u \u2192 triv , u) , (\u03bb _ r \u2192 snd r) , \u03bb\u2297-inv-p\n where\n \u03bb\u2297-inv-p : \u2200{U X \u03b1}{u : U} {y : \u03a3 \u22a4 (\u03bb x \u2192 X)} \u2192 \u03b1 u (snd y) \u2192 (\u03b9 \u2297\u1d63 \u03b1) (triv , u) y\n \u03bb\u2297-inv-p {U}{X}{\u03b1}{u}{triv , x} p = triv , p\n\n-- The right-unitor:\n\u03c1\u2297 : \u2200{A : Obj} \u2192 Hom (A \u2297\u2092 I) A\n\u03c1\u2297 {(U , X , \u03b1)} = fst , (\u03bb r x \u2192 x , triv) , \u03c1\u2297-p\n where\n \u03c1\u2297-p : \u2200{U X \u03b1}{u : \u03a3 U (\u03bb x \u2192 \u22a4)} {y : X} \u2192 (\u03b1 \u2297\u1d63 \u03b9) u (y , triv) \u2192 \u03b1 (fst u) y\n \u03c1\u2297-p {U}{X}{\u03b1}{(u , _)}{x} (p , _) = p\n\n\n\u03c1\u2297-inv : \u2200{A : Obj} \u2192 Hom A (A \u2297\u2092 I)\n\u03c1\u2297-inv {(U , X , \u03b1)} = (\u03bb u \u2192 u , triv) , (\u03bb u r \u2192 fst r) , \u03c1\u2297-p-inv\n where\n \u03c1\u2297-p-inv : \u2200{U X \u03b1}{u : U} {y : \u03a3 X (\u03bb x \u2192 \u22a4)} \u2192 \u03b1 u (fst y) \u2192 (\u03b1 \u2297\u1d63 \u03b9) (u , triv) y\n \u03c1\u2297-p-inv {U}{X}{\u03b1}{u}{x , triv} p = p , triv\n\n-- Symmetry:\n\u03b2\u2297 : \u2200{A B : Obj} \u2192 Hom (A \u2297\u2092 B) (B \u2297\u2092 A)\n\u03b2\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)} = twist-\u00d7 , (\u03bb r\u2081 r\u2082 \u2192 twist-\u00d7 r\u2082) , \u03b2\u2297-p\n where\n \u03b2\u2297-p : \u2200{U V Y X \u03b1 \u03b2}{u : \u03a3 U (\u03bb x \u2192 V)} {y : \u03a3 Y (\u03bb x \u2192 X)} \u2192 (\u03b1 \u2297\u1d63 \u03b2) u (twist-\u00d7 y) \u2192 (\u03b2 \u2297\u1d63 \u03b1) (twist-\u00d7 u) y\n \u03b2\u2297-p {U}{V}{Y}{X}{\u03b1}{\u03b2}{u , v}{y , x} = twist-\u00d7\n\n\n-- The associator:\n\u03b1\u2297-inv : \u2200{A B C : Obj} \u2192 Hom (A \u2297\u2092 (B \u2297\u2092 C)) ((A \u2297\u2092 B) \u2297\u2092 C)\n\u03b1\u2297-inv {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = rl-assoc-\u00d7 , F\u03b1-inv , \u03b1-inv-cond\n where\n F\u03b1-inv : (U \u00d7 (V \u00d7 W)) \u2192 ((X \u00d7 Y) \u00d7 Z) \u2192 (X \u00d7 (Y \u00d7 Z))\n F\u03b1-inv (u , (v , w)) ((x , y) , z) = x , y , z\n \u03b1-inv-cond : {u : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))}{y : \u03a3 (\u03a3 X (\u03bb x \u2192 Y)) (\u03bb x \u2192 Z)}\n \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) u (F\u03b1-inv u y)\n \u2192 ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) (rl-assoc-\u00d7 u) y\n \u03b1-inv-cond {u , (v , w)}{(x , y) , z} (p\u2081 , (p\u2082 , p\u2083)) = (p\u2081 , p\u2082) , p\u2083\n\n\nF\u03b1 : \u2200{V W X Y U Z : Set} \u2192 ((U \u00d7 V) \u00d7 W) \u2192 (X \u00d7 (Y \u00d7 Z)) \u2192 ((X \u00d7 Y) \u00d7 Z)\nF\u03b1 {V}{W}{X}{Y}{U}{Z} ((u , v) , w) (x , (y , z)) = (x , y) , z\n\n\u03b1\u2297 : \u2200{A B C : Obj} \u2192 Hom ((A \u2297\u2092 B) \u2297\u2092 C) (A \u2297\u2092 (B \u2297\u2092 C)) \n\u03b1\u2297 {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)} = (lr-assoc-\u00d7 , F\u03b1 , \u03b1-cond)\n where\n \u03b1-cond : {u : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)}\n {y : \u03a3 X (\u03bb x \u2192 \u03a3 Y (\u03bb x\u2081 \u2192 Z))} \u2192\n ((\u03b1 \u2297\u1d63 \u03b2) \u2297\u1d63 \u03b3) u (F\u03b1 u y) \u2192 (\u03b1 \u2297\u1d63 (\u03b2 \u2297\u1d63 \u03b3)) (lr-assoc-\u00d7 u) y\n \u03b1-cond {(u , v) , w}{x , (y , z)} ((p\u2081 , p\u2082) , p\u2083) = p\u2081 , p\u2082 , p\u2083\n{-\n\u03b1\u2297-id\u2081 : \u2200{A B C} \u2192 (\u03b1\u2297 {A}{B}{C}) \u25cb \u03b1\u2297-inv \u2261h id\n\u03b1\u2297-id\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 (\u03a3 U (\u03bb x \u2192 V)) (\u03bb x \u2192 W)} \u2192 rl-assoc-\u00d7 (lr-assoc-\u00d7 a) \u2261 a\n aux {(u , v) , w} = refl\n\n aux' : {a : \u03a3 (W \u2192 \u03a3 (V \u2192 X) (\u03bb x \u2192 U \u2192 Y)) (\u03bb x \u2192 \u03a3 U (\u03bb x\u2081 \u2192 V) \u2192 Z)}\n \u2192 ((\u03bb x \u2192 (\u03bb x\u2081 \u2192 fst (fst a x) x\u2081) , (\u03bb x\u2081 \u2192 snd (fst a x) x\u2081)) , (\u03bb x \u2192 snd a (fst x , snd x))) \u2261 a\n aux' {j\u2081 , j\u2082} = eq-\u00d7 (ext-set aux'') (ext-set aux''')\n where\n aux'' : {a : W} \u2192 (fst (j\u2081 a) , snd (j\u2081 a)) \u2261 j\u2081 a\n aux'' {w} with j\u2081 w\n ... | h\u2081 , h\u2082 = refl\n\n aux''' : {a : \u03a3 U (\u03bb x\u2081 \u2192 V)} \u2192 j\u2082 (fst a , snd a) \u2261 j\u2082 a\n aux''' {u , v} = refl\n\n\u03b1\u2297-id\u2082 : \u2200{A B C} \u2192 (\u03b1\u2297-inv {A}{B}{C}) \u25cb \u03b1\u2297 \u2261h id\n\u03b1\u2297-id\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} = ext-set aux , ext-set aux'\n where\n aux : {a : \u03a3 U (\u03bb x \u2192 \u03a3 V (\u03bb x\u2081 \u2192 W))} \u2192 lr-assoc-\u00d7 (rl-assoc-\u00d7 a) \u2261 a\n aux {u , (v , w)} = refl\n aux' : {a\n : \u03a3 (\u03a3 V (\u03bb x \u2192 W) \u2192 X) (\u03bb x \u2192 U \u2192 \u03a3 (W \u2192 Y) (\u03bb x\u2081 \u2192 V \u2192 Z))} \u2192\n ((\u03bb p' \u2192 fst (fst (F\u03b1 {V} {W} {X} {Y} {U} {V} {Z} a) (snd p')) (fst p')) ,\n (\u03bb u \u2192 (\u03bb w \u2192 snd (fst (F\u03b1 {V} {W} {X} {Y} {U} {V} {Z} a) w) u) , (\u03bb v \u2192 snd (F\u03b1 {V} {W} {X} {Y} {U} {V} {Z} a) (u , v))))\n \u2261 a\n aux' {j\u2081 , j\u2082} = eq-\u00d7 (ext-set aux'') (ext-set aux''')\n where\n aux'' : {a : \u03a3 V (\u03bb x \u2192 W)} \u2192 j\u2081 (fst a , snd a) \u2261 j\u2081 a\n aux'' {v , w} = refl\n aux''' : {a : U} \u2192 ((\u03bb w \u2192 fst (j\u2082 a) w) , (\u03bb v \u2192 snd (j\u2082 a) v)) \u2261 j\u2082 a\n aux''' {u} with j\u2082 u\n ... | h\u2081 , h\u2082 = refl\n \n-- Internal hom:\n\u22b8-cond : \u2200{U V X Y : Set} \u2192 (U \u2192 X \u2192 Set) \u2192 (V \u2192 Y \u2192 Set) \u2192 (U \u2192 V) \u00d7 (Y \u2192 X) \u2192 U \u00d7 Y \u2192 Set\n\u22b8-cond \u03b1 \u03b2 (f , g) (u , y) = \u03b1 u (g y) \u2192 \u03b2 (f u) y\n\n_\u22b8\u2092_ : Obj \u2192 Obj \u2192 Obj\n(U , X , \u03b1) \u22b8\u2092 (V , Y , \u03b2) = ((U \u2192 V) \u00d7 (Y \u2192 X)) , (U \u00d7 Y) , \u22b8-cond \u03b1 \u03b2\n\n_\u22b8\u2090_ : {A B C D : Obj} \u2192 Hom C A \u2192 Hom B D \u2192 Hom (A \u22b8\u2092 B) (C \u22b8\u2092 D)\n_\u22b8\u2090_ {(U , X , \u03b1)}{(V , Y , \u03b2)}{(W , Z , \u03b3)}{(S , T , \u03b4)} (f , F , p\u2081) (g , G , p\u2082) =\n h , H , p\u2083\n where\n h : \u03a3 (U \u2192 V) (\u03bb x \u2192 Y \u2192 X) \u2192 \u03a3 (W \u2192 S) (\u03bb x \u2192 T \u2192 Z)\n h (h\u2081 , h\u2082) = (\u03bb w \u2192 g (h\u2081 (f w))) , (\u03bb t \u2192 F (h\u2082 (G t)))\n H : \u03a3 W (\u03bb x \u2192 T) \u2192 \u03a3 U (\u03bb x \u2192 Y)\n H (w , t) = f w , G t\n p\u2083 : {u : \u03a3 (U \u2192 V) (\u03bb x \u2192 Y \u2192 X)} {y : \u03a3 W (\u03bb x \u2192 T)} \u2192 \u22b8-cond \u03b1 \u03b2 u (H y) \u2192 \u22b8-cond \u03b3 \u03b4 (h u) y\n p\u2083 {h\u2081 , h\u2082}{w , t} c c' = p\u2082 (c (p\u2081 c'))\n\ncur : {A B C : Obj}\n \u2192 Hom (A \u2297\u2092 B) C\n \u2192 Hom A (B \u22b8\u2092 C)\ncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb u \u2192 (\u03bb v \u2192 f (u , v)) , (\u03bb z \u2192 snd (F z) u)) , (\u03bb p \u2192 fst (F (snd p)) (fst p)) , cur-cond\n where\n cur-cond : \u2200{u : U}{y : \u03a3 V (\u03bb x \u2192 Z)}\n \u2192 \u03b1 u (fst (F (snd y)) (fst y))\n \u2192 \u22b8-cond \u03b2 \u03b3 ((\u03bb v \u2192 f (u , v)) , (\u03bb z \u2192 snd (F z) u)) y\n cur-cond {u}{v , z} p\u2082 p\u2083 with p\u2081 {u , v}{z} \n ... | p\u2081' with F z\n ... | (j\u2081 , j\u2082) = p\u2081' (p\u2082 , p\u2083)\n\ncur-\u2261h : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C}\n \u2192 f\u2081 \u2261h f\u2082\n \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-\u2261h {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}\n {f\u2081 , F\u2081 , p\u2081}{f\u2082 , F\u2082 , p\u2082} (p\u2083 , p\u2084)\n rewrite p\u2083 | p\u2084 = refl , refl\n\ncur-cong : \u2200{A B C}{f\u2081 f\u2082 : Hom (A \u2297\u2092 B) C} \u2192 f\u2081 \u2261h f\u2082 \u2192 cur f\u2081 \u2261h cur f\u2082\ncur-cong {(U , X , \u03b1)} {(V , Y , \u03b2)} {(W , Z , \u03b3)}{f\u2081 , F\u2081 , _}{f\u2082 , F\u2082 , _} (p\u2081 , p\u2082) rewrite p\u2081 | p\u2082 = refl , refl\n\nuncur : {A B C : Obj}\n \u2192 Hom A (B \u22b8\u2092 C)\n \u2192 Hom (A \u2297\u2092 B) C\nuncur {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3} (f , F , p\u2081)\n = (\u03bb p \u2192 fst (f (fst p)) (snd p)) , (\u03bb z \u2192 (\u03bb v \u2192 F (v , z)) , (\u03bb u \u2192 snd (f u) z)) , uncur-cond\n where\n uncur-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : Z}\n \u2192 (\u03b1 \u2297\u1d63 \u03b2) u ((\u03bb v \u2192 F (v , y)) , (\u03bb u\u2081 \u2192 snd (f u\u2081) y))\n \u2192 \u03b3 (fst (f (fst u)) (snd u)) y\n uncur-cond {u , v}{z} (p\u2082 , p\u2083) with p\u2081 {u}{v , z} p\u2082\n ... | p\u2081' with f u\n ... | (j\u2081 , j\u2082) = p\u2081' p\u2083\n\ncur-uncur-bij\u2081 : \u2200{A B C}{f : Hom (A \u2297\u2092 B) C}\n \u2192 uncur (cur f) \u2261h f\ncur-uncur-bij\u2081 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{f , F , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : \u03a3 U (\u03bb x \u2192 V)} \u2192 f (fst a , snd a) \u2261 f a\n aux\u2081 {u , v} = refl\n \n aux\u2082 : {a : Z} \u2192 ((\u03bb v \u2192 fst (F a) v) , (\u03bb u \u2192 snd (F a) u)) \u2261 F a\n aux\u2082 {z} with F z\n ... | j\u2081 , j\u2082 = refl\n\ncur-uncur-bij\u2082 : \u2200{A B C}{g : Hom A (B \u22b8\u2092 C)}\n \u2192 cur (uncur g) \u2261h g\ncur-uncur-bij\u2082 {U , X , \u03b1}{V , Y , \u03b2}{W , Z , \u03b3}{g , G , p\u2081} = ext-set aux\u2081 , ext-set aux\u2082\n where\n aux\u2081 : {a : U} \u2192 ((\u03bb v \u2192 fst (g a) v) , (\u03bb z \u2192 snd (g a) z)) \u2261 g a\n aux\u2081 {u} with g u\n ... | (j\u2081 , j\u2082) = refl\n\n aux\u2082 : {a : \u03a3 V (\u03bb x \u2192 Z)} \u2192 G (fst a , snd a) \u2261 G a\n aux\u2082 {v , z} = refl\n \n-- The of-course exponential:\n!\u2092-cond : \u2200{U X : Set}\n \u2192 (U \u2192 X \u2192 Set)\n \u2192 U\n \u2192 (U \u2192 X *)\n \u2192 Set\n!\u2092-cond \u03b1 u f = all-pred (\u03b1 u) (f u)\n \n!\u2092 : Obj \u2192 Obj\n!\u2092 (U , X , \u03b1) = U , (U \u2192 X *) , !\u2092-cond \u03b1\n\n!-cta : {V Y U X : Set}\n \u2192 (Y \u2192 X)\n \u2192 (U \u2192 V)\n \u2192 (V \u2192 Y *)\n \u2192 (U \u2192 X *)\n!-cta F f g = \u03bb u \u2192 list-funct F (g (f u))\n\n!\u2090-cond : \u2200{U V Y X : Set}{F : Y \u2192 X}{f : U \u2192 V}\n \u2192 (\u03b1 : U \u2192 X \u2192 Set)\n \u2192 (\u03b2 : V \u2192 Y \u2192 Set)\n \u2192 (p : {u : U} {y : Y} \u2192 \u03b1 u (F y) \u2192 \u03b2 (f u) y)\n {u : U}{l : Y *}\n \u2192 all-pred (\u03b1 u) (list-funct F l)\n \u2192 all-pred (\u03b2 (f u)) l\n!\u2090-cond _ _ _ {l = []} _ = triv\n!\u2090-cond \u03b1 \u03b2 p {u}{x :: xs} (p' , p'') = p p' , !\u2090-cond \u03b1 \u03b2 p p'' \n \n!\u2090 : {A B : Obj} \u2192 Hom A B \u2192 Hom (!\u2092 A) (!\u2092 B)\n!\u2090 {U , X , \u03b1}{V , Y , \u03b2} (f , F , p) = f , !-cta F f , !\u2090-cond \u03b1 \u03b2 p\n\n-- Of-course is a comonad:\n\u03b5 : \u2200{A} \u2192 Hom (!\u2092 A) A\n\u03b5 {U , X , \u03b1} = id-set , (\u03bb x y \u2192 [ x ]) , fst\n\n\u03b4-cta : {U X : Set} \u2192 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X)) \u2192 U \u2192 \ud835\udd43 X\n\u03b4-cta g u = foldr (\u03bb f rest \u2192 (f u) ++ rest) [] (g u)\n \n\u03b4 : \u2200{A} \u2192 Hom (!\u2092 A) (!\u2092 (!\u2092 A))\n\u03b4 {U , X , \u03b1} = id-set , \u03b4-cta , \u03b4-cond\n where\n \u03b4-cond : {u : U} {l : \ud835\udd43 (U \u2192 \ud835\udd43 X)}\n \u2192 all-pred (\u03b1 u) (foldr (\u03bb f \u2192 _++_ (f u)) [] l)\n \u2192 all-pred (\u03bb f\n \u2192 all-pred (\u03b1 u) (f u)) l\n \u03b4-cond {l = []} _ = triv\n \u03b4-cond {u}{l = x :: l'} p with\n all-pred-append {X}{\u03b1 u}\n {x u}\n {foldr (\u03bb f \u2192 _++_ (f u)) [] l'}\n \u2227-unit \u2227-assoc\n ... | p' rewrite p' = fst p , \u03b4-cond {u} {l'} (snd p)\n\n-- These diagrams can be found on page 22 of the report.\ncomonand-diag\u2081 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (!\u2090 (\u03b4 {A})) \u2261h (\u03b4 {A}) \u25cb (\u03b4 { !\u2092 A})\ncomonand-diag\u2081 {U , X , \u03b1} =\n refl , ext-set (\u03bb {a} \u2192 ext-set (\u03bb {a\u2081} \u2192 aux {a\u2081}{a a\u2081}))\n where\n aux : \u2200{a\u2081 : U}{l : \ud835\udd43 (U \u2192 \ud835\udd43 (U \u2192 \ud835\udd43 X))} \u2192\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) []\n (map (\u03bb g u \u2192 foldr (\u03bb f \u2192 _++_ (f u)) [] (g u)) l)\n \u2261\n foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] (foldr (\u03bb f \u2192 _++_ (f a\u2081)) [] l)\n aux {a}{[]} = refl \n aux {a}{x :: l} rewrite\n sym (foldr-append {l\u2081 = x a}{foldr (\u03bb f \u2192 _++_ (f a)) [] l}{a})\n = cong2 {a = foldr (\u03bb f \u2192 _++_ (f a)) [] (x a)}\n _++_\n refl\n (aux {a}{l})\n\ncomonand-diag\u2082 : \u2200{A}\n \u2192 (\u03b4 {A}) \u25cb (\u03b5 { !\u2092 A}) \u2261h (\u03b4 {A}) \u25cb (!\u2090 (\u03b5 {A}))\ncomonand-diag\u2082 {U , X , \u03b1} =\n refl , ext-set (\u03bb {f} \u2192 ext-set (\u03bb {a} \u2192 aux {a}{f a}))\n where\n aux : \u2200{a : U}{l : X *}\n \u2192 l ++ [] \u2261 foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x y \u2192 x :: []) l)\n aux {a}{[]} = refl\n aux {a}{x :: l} with aux {a}{l}\n ... | IH rewrite ++[] l =\n cong2 {a = x} {x} {l}\n {foldr (\u03bb f\u2081 \u2192 _++_ (f\u2081 a)) [] (map (\u03bb x\u2081 y \u2192 x\u2081 :: []) l)} _::_ refl\n IH\n \nmodule Cartesian where\n \u03c0\u2081 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (U , X , \u03b1))\n \u03c0\u2081 {U}{X}{V}{Y}{\u03b1}{\u03b2} = fst , (\u03bb f \u2192 (\u03bb v u \u2192 f u) , (\u03bb u v \u2192 [])) , \u03c0\u2081-cond\n where\n \u03c0\u2081-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : U \u2192 \ud835\udd43 X} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 y u\u2081) , (\u03bb u\u2081 v \u2192 [])) \u2192\n all-pred (\u03b1 (fst u)) (y (fst u))\n \u03c0\u2081-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2081\n\n \u03c0\u2082 : {U X V Y : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 Hom ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2))) (!\u2092 (V , Y , \u03b2))\n \u03c0\u2082 {U}{X}{V}{Y}{\u03b1}{\u03b2} = snd , (\u03bb f \u2192 (\u03bb v u \u2192 []) , (\u03bb u v \u2192 f v)) , \u03c0\u2082-cond\n where\n \u03c0\u2082-cond : \u2200{u : \u03a3 U (\u03bb x \u2192 V)} {y : V \u2192 \ud835\udd43 Y} \u2192\n ((\u03bb u\u2081 f \u2192 all-pred (\u03b1 u\u2081) (f u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f \u2192 all-pred (\u03b2 u\u2081) (f u\u2081)))\n u ((\u03bb v u\u2081 \u2192 []) , (\u03bb u\u2081 v \u2192 y v)) \u2192\n all-pred (\u03b2 (snd u)) (y (snd u))\n \u03c0\u2082-cond {u , v}{f} (p\u2081 , p\u2082) = p\u2082\n\n cart-ar-crt : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y) \u2192 W \u2192 \ud835\udd43 Z\n cart-ar-crt (f , F , p\u2081) (g , G , p\u2082) (j\u2081 , j\u2082) w = F (j\u2081 (g w)) w ++ G (j\u2082 (f w)) w\n\n cart-ar : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (U , X , \u03b1))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) (!\u2092 (V , Y , \u03b2))\n \u2192 Hom (!\u2092 (W , Z , \u03b3)) ((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))\n cart-ar {U}{X}{V}{Y}{W}{Z}{\u03b1}{\u03b2}{\u03b3} (f , F , p\u2081) (g , G , p\u2082)\n = (\u03bb w \u2192 f w , g w) , cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) , cart-ar-cond\n where\n cart-ar-cond : \u2200{u : W} {y : \u03a3 (V \u2192 U \u2192 \ud835\udd43 X) (\u03bb x \u2192 U \u2192 V \u2192 \ud835\udd43 Y)} \u2192\n all-pred (\u03b3 u) (cart-ar-crt {\u03b1 = \u03b1}{\u03b2} (f , F , p\u2081) (g , G , p\u2082) y u) \u2192\n ((\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b1 u\u2081) (f\u2081 u\u2081)) \u2297\u1d63\n (\u03bb u\u2081 f\u2081 \u2192 all-pred (\u03b2 u\u2081) (f\u2081 u\u2081)))\n (f u , g u) y\n cart-ar-cond {w}{j\u2081 , j\u2082} p\n rewrite\n all-pred-append {f = \u03b3 w}{F (j\u2081 (g w)) w}{G (j\u2082 (f w)) w} \u2227-unit \u2227-assoc with p\n ... | (a , b) = p\u2081 a , p\u2082 b\n\n cart-diag\u2081 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (U , X , \u03b1)}\n (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (U , X , \u03b1)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2081)\n cart-diag\u2081 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 sym (++[] (map F (j (f w))))))\n\n cart-diag\u2082 : {U X V Y W Z : Set}\n \u2192 {\u03b1 : U \u2192 X \u2192 Set}\n \u2192 {\u03b2 : V \u2192 Y \u2192 Set}\n \u2192 {\u03b3 : W \u2192 Z \u2192 Set}\n \u2192 {f : Hom (W , Z , \u03b3) (U , X , \u03b1)}\n \u2192 {g : Hom (W , Z , \u03b3) (V , Y , \u03b2)}\n \u2192 _\u2261h_ { !\u2092 (W , Z , \u03b3)}{ !\u2092 (V , Y , \u03b2)}\n (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g)\n (comp { !\u2092 (W , Z , \u03b3)}\n {((!\u2092 (U , X , \u03b1)) \u2297\u2092 (!\u2092 (V , Y , \u03b2)))}\n { !\u2092 (V , Y , \u03b2)}\n (cart-ar {\u03b1 = \u03b1}{\u03b2}{\u03b3} (!\u2090 {W , Z , \u03b3}{U , X , \u03b1} f) (!\u2090 {W , Z , \u03b3}{V , Y , \u03b2} g))\n \u03c0\u2082)\n cart-diag\u2082 {f = f , F , p\u2081}{g , G , p\u2082}\n = refl , ext-set (\u03bb {j} \u2192 ext-set (\u03bb {w} \u2192 refl))\n-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"6dbde1e52f3b14199a1e9ee7097d079561b3923e","subject":"Correct local variable name","message":"Correct local variable name\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/SIRelBigStep\/FundamentalProperty.agda","new_file":"Thesis\/SIRelBigStep\/FundamentalProperty.agda","new_contents":"module Thesis.SIRelBigStep.FundamentalProperty where\n\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\n\nopen import Thesis.SIRelBigStep.IlcSILR\n\nrfundamentalV3v : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 \u2200 \u03c11 d\u03c1 \u03c12 (\u03c1\u03c1 : rrel\u03c13 \u0393 \u03c11 d\u03c1 \u03c12 n) \u2192 rrelV3 \u03c4 (\u27e6 x \u27e7Var \u03c11) (D.\u27e6 x \u27e7Var d\u03c1) (\u27e6 x \u27e7Var \u03c12) n\nrfundamentalV3v x n \u03c11 d\u03c1 \u03c12 \u03c1\u03c1 = \u27e6 x \u27e7RelVar3 \u03c1\u03c1\n\nrfundamental3constV : \u2200 {\u03c4} k (c : Const \u03c4) \u2192\n rrelV3 \u03c4 (eval-const c) (deval (derive-const c) \u2205 \u2205) (eval-const c) k\nrfundamental3constV k (lit n) = refl\n\nrfundamental3 : \u2200 {\u03c4 \u0393} k (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c11 d\u03c1 \u03c12 \u2192 (\u03c1\u03c1 : rrel\u03c13 \u0393 \u03c11 d\u03c1 \u03c12 k) \u2192\n rrelT3 t (derive-dterm t) t \u03c11 d\u03c1 \u03c12 k\n\nrfundamental3svv : \u2200 {\u03c4 \u0393} k (sv : SVal \u0393 \u03c4) \u2192\n \u2200 \u03c11 d\u03c1 \u03c12 \u2192 (\u03c1\u03c1 : rrel\u03c13 \u0393 \u03c11 d\u03c1 \u03c12 k) \u2192 rrelV3 \u03c4 (eval sv \u03c11) (deval (derive-dsval sv) \u03c11 d\u03c1) (eval sv \u03c12) k\nrfundamental3svv k (var x) \u03c11 d\u03c1 \u03c12 \u03c1\u03c1 = rfundamentalV3v x k \u03c11 d\u03c1 \u03c12 \u03c1\u03c1\nrfundamental3svv k (cons sv1 sv2) \u03c11 d\u03c1 \u03c12 \u03c1\u03c1 = rfundamental3svv k sv1 \u03c11 d\u03c1 \u03c12 \u03c1\u03c1 , rfundamental3svv k sv2 \u03c11 d\u03c1 \u03c12 \u03c1\u03c1\nrfundamental3svv k (const c) \u03c11 d\u03c1 \u03c12 \u03c1\u03c1 rewrite deval-derive-const-inv c \u03c11 d\u03c1 = rfundamental3constV k c\nrfundamental3svv k (abs t) \u03c11 d\u03c1 \u03c12 \u03c1\u03c1 = (refl , refl) , refl , rrel\u03c13\u2192\u2295 \u03c11 d\u03c1 \u03c12 \u03c1\u03c1 , refl , refl ,\n \u03bb j j d \u22a3 \u0394 \u2192\n \u0393 \u22a2 e => \u03c4\n correspondence-synth ESConst = SConst\n correspondence-synth (ESVar x\u2081) = SVar x\u2081\n correspondence-synth (ESLam apt ex) with correspondence-synth ex\n ... | ih = SLam apt ih\n correspondence-synth (ESAp1 x x\u2081 x\u2082 x\u2083) = SAp x\u2081 MAHole (correspondence-ana x\u2083)\n correspondence-synth (ESAp2 x ex x\u2081 x\u2082) = SAp (correspondence-synth ex) MAArr (correspondence-ana x\u2081)\n correspondence-synth (ESAp3 x ex x\u2081) = SAp (correspondence-synth ex) MAArr (correspondence-ana x\u2081)\n correspondence-synth ESEHole = SEHole\n correspondence-synth (ESNEHole ex) = SNEHole (correspondence-synth ex)\n correspondence-synth (ESAsc1 x _) = SAsc (correspondence-ana x)\n correspondence-synth (ESAsc2 x) = SAsc (correspondence-ana x)\n\n correspondence-ana : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 e <= \u03c4\n correspondence-ana (EALam apt ex) with correspondence-ana ex\n ... | ih = ALam apt MAArr ih\n correspondence-ana (EASubsume x x\u2081 x\u2082 x\u2083) = ASubsume (correspondence-synth x\u2082) x\u2083\n correspondence-ana EAEHole = ASubsume SEHole TCHole1\n correspondence-ana (EANEHole x) = ASubsume (SNEHole (correspondence-synth x)) TCHole1\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\n\nmodule correspondence where\n mutual\n correspondence-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 e => \u03c4\n correspondence-synth ESConst = SConst\n correspondence-synth (ESVar x\u2081) = SVar x\u2081\n correspondence-synth (ESLam apt ex) with correspondence-synth ex\n ... | ih = SLam apt ih\n correspondence-synth (ESAp1 x x\u2081 x\u2082 x\u2083) = SAp x\u2081 MAHole (correspondence-ana x\u2083)\n correspondence-synth (ESAp2 x ex x\u2081 x\u2082) = SAp (correspondence-synth ex) MAArr (correspondence-ana x\u2081)\n correspondence-synth (ESAp3 x ex x\u2081) = SAp (correspondence-synth ex) MAArr (correspondence-ana x\u2081)\n correspondence-synth ESEHole = SEHole\n correspondence-synth (ESNEHole ex) = SNEHole (correspondence-synth ex)\n correspondence-synth (ESAsc1 x _) = SAsc (correspondence-ana x)\n correspondence-synth (ESAsc2 x) = SAsc (correspondence-ana x)\n\n correspondence-ana : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 e <= \u03c4\n correspondence-ana (EALam apt ex) with correspondence-ana ex\n ... | ih = ALam apt MAArr ih\n correspondence-ana (EASubsume x x\u2081 x\u2082 x\u2083) = ASubsume (correspondence-synth x\u2082) x\u2083\n correspondence-ana EAEHole = ASubsume SEHole TCHole1\n correspondence-ana (EANEHole x x\u2081) = ASubsume (SNEHole (correspondence-synth x)) TCHole1\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7c97a711c13106d425228036a4950ce43957d9ca","subject":"Only match the higher-order argument if its image looks like what we expect.","message":"Only match the higher-order argument if its image looks like what we expect.\n","repos":"spire\/spire","old_file":"proposal\/examples\/HierMatchExt.agda","new_file":"proposal\/examples\/HierMatchExt.agda","new_contents":"{-\nDemonstrating the technique of using type-changing functions\nto pattern match against types with higher-order arguments\nlike \u03a3 and \u03a0.\n\nThe technique was originally explored to compare higher-order\nsmall arguments that occur in \u03a6 types in\nTacticsMatchingFunctionCalls.agda\n\nThis file shows that the technique scales to large higher-order\nfunctions , in a closed universe with a predicative hierarchy\nof levels that supports elimType.\n\n-}\n\nopen import Data.Empty\nopen import Data.Unit\nopen import Data.Bool\nopen import Data.Nat\nopen import Data.Fin hiding ( _+_ )\nopen import Data.Fin.Props\nopen import Data.Product\nopen import Data.String\nopen import Function\nopen import Relation.Binary.PropositionalEquality hiding ( inspect )\nopen Deprecated-inspect\nmodule HierMatchExt where\n\n----------------------------------------------------------------------\n\nplusrident : (n : \u2115) \u2192 n + 0 \u2261 n\nplusrident zero = refl\nplusrident (suc n) = cong suc (plusrident n)\n\n----------------------------------------------------------------------\n\nrecord Universe : Set\u2081 where\n field\n Codes : Set\n Meaning : Codes \u2192 Set\n\n----------------------------------------------------------------------\n\ndata Even : \u2115 \u2192 Set where\n ezero : Even 0\n esuc : {n : \u2115} \u2192 Even n \u2192 Even (2 + n)\n\ndata Odd : \u2115 \u2192 Set where\n ozero : Odd 1\n osuc : {n : \u2115} \u2192 Odd n \u2192 Odd (2 + n)\n\n----------------------------------------------------------------------\n\ndata TypeForm (U : Universe) : Set\n\u27e6_\/_\u27e7 : (U : Universe) \u2192 TypeForm U \u2192 Set\n\ndata TypeForm U where\n `\u22a5 `\u22a4 `Bool `\u2115 `Type : TypeForm U\n `Fin `Even `Odd : (n : \u2115) \u2192 TypeForm U\n `\u03a0 `\u03a3 : (A : TypeForm U)\n (B : \u27e6 U \/ A \u27e7 \u2192 TypeForm U)\n \u2192 TypeForm U\n `Id : (A : TypeForm U) (x y : \u27e6 U \/ A \u27e7) \u2192 TypeForm U\n `\u27e6_\u27e7 : (A : Universe.Codes U) \u2192 TypeForm U\n\n\u27e6 U \/ `\u22a5 \u27e7 = \u22a5\n\u27e6 U \/ `\u22a4 \u27e7 = \u22a4\n\u27e6 U \/ `Bool \u27e7 = Bool\n\u27e6 U \/ `\u2115 \u27e7 = \u2115\n\u27e6 U \/ `Fin n \u27e7 = Fin n\n\u27e6 U \/ `Even n \u27e7 = Even n\n\u27e6 U \/ `Odd n \u27e7 = Odd n\n\u27e6 U \/ `\u03a0 A B \u27e7 = (a : \u27e6 U \/ A \u27e7) \u2192 \u27e6 U \/ B a \u27e7\n\u27e6 U \/ `\u03a3 A B \u27e7 = \u03a3 \u27e6 U \/ A \u27e7 (\u03bb a \u2192 \u27e6 U \/ B a \u27e7)\n\u27e6 U \/ `Id A x y \u27e7 = _\u2261_ {A = \u27e6 U \/ A \u27e7} x y\n\u27e6 U \/ `Type \u27e7 = Universe.Codes U\n\u27e6 U \/ `\u27e6 A \u27e7 \u27e7 = Universe.Meaning U A\n\n----------------------------------------------------------------------\n\n_`\u2192_ : \u2200{U} (A B : TypeForm U) \u2192 TypeForm U\nA `\u2192 B = `\u03a0 A (\u03bb _ \u2192 B)\n\n_`\u00d7_ : \u2200{U} (A B : TypeForm U) \u2192 TypeForm U\nA `\u00d7 B = `\u03a3 A (\u03bb _ \u2192 B)\n\nLevel : (\u2113 : \u2115) \u2192 Universe\nLevel zero = record { Codes = \u22a5 ; Meaning = \u03bb() }\nLevel (suc \u2113) = record { Codes = TypeForm (Level \u2113)\n ; Meaning = \u27e6_\/_\u27e7 (Level \u2113) }\n\nType : \u2115 \u2192 Set\nType \u2113 = TypeForm (Level \u2113)\n\n\u27e6_\u2223_\u27e7 : (\u2113 : \u2115) \u2192 Type \u2113 \u2192 Set\n\u27e6 \u2113 \u2223 A \u27e7 = \u27e6 Level \u2113 \/ A \u27e7\n\n`Dynamic : (\u2113 : \u2115) \u2192 Type (suc \u2113)\n`Dynamic \u2113 = `\u03a3 `Type `\u27e6_\u27e7\n\n`Tactic : (\u2113 : \u2115) \u2192 Type (suc \u2113)\n`Tactic \u2113 = `Dynamic \u2113 `\u2192 `Dynamic \u2113\n\nTactic : (\u2113 : \u2115) \u2192 Set\nTactic \u2113 = \u27e6 suc \u2113 \u2223 `Tactic \u2113 \u27e7\n\n----------------------------------------------------------------------\n\nrm-plus0-Fin : (\u2113 : \u2115) \u2192 Tactic (suc \u2113)\nrm-plus0-Fin \u2113 (`\u27e6 `\u03a3 `\u2115 B \u27e7 , (n , b))\n with inspect (B n)\n... | `Fin m with-\u2261 p =\n `\u03a0 `\u2115 (\u03bb x \u2192 `Id `Type (B x) (`Fin (x + 0)))\n `\u2192\n `\u03a3 `\u2115 `Fin\n ,\n \u03bb f \u2192 n , subst Fin\n (plusrident n)\n (subst (\u03bb A \u2192 \u27e6 \u2113 \u2223 A \u27e7) (f n) b)\n... | Bn with-\u2261 p = (`\u27e6 `\u03a3 `\u2115 B \u27e7 , (n , subst (\u03bb x \u2192 \u27e6 \u2113 \u2223 B x \u27e7) refl b))\nrm-plus0-Fin \u2113 x = x\n\n----------------------------------------------------------------------\n\n-- eg-rm-plus0-Fin : (\u2113 : \u2115) \u2192\n-- \u27e6 suc \u2113 \u2223 `\u27e6 `\u03a3 `\u2115 (\u03bb n \u2192 `Fin (n + zero)) \u27e7\n-- `\u2192 `\u27e6 `\u03a3 `\u2115 (\u03bb n \u2192 `Fin n) \u27e7 \u27e7\n-- eg-rm-plus0-Fin \u2113 (n , i) = proj\u2082\n-- (rm-plus0-Fin \u2113 (`\u27e6 `\u03a3 `\u2115 (\u03bb n \u2192 `Fin (n + zero)) \u27e7 , (n , i)))\n-- (\u03bb _ \u2192 refl)\n\n----------------------------------------------------------------------\n","old_contents":"{-\nDemonstrating the technique of using type-changing functions\nto pattern match against types with higher-order arguments\nlike \u03a3 and \u03a0.\n\nThe technique was originally explored to compare higher-order\nsmall arguments that occur in \u03a6 types in\nTacticsMatchingFunctionCalls.agda\n\nThis file shows that the technique scales to large higher-order\nfunctions , in a closed universe with a predicative hierarchy\nof levels that supports elimType.\n\n-}\n\nopen import Data.Empty\nopen import Data.Unit\nopen import Data.Bool\nopen import Data.Nat\nopen import Data.Fin hiding ( _+_ )\nopen import Data.Fin.Props\nopen import Data.Product\nopen import Data.String\nopen import Function\nopen import Relation.Binary.PropositionalEquality\nmodule HierMatchExt where\n\n----------------------------------------------------------------------\n\nplusrident : (n : \u2115) \u2192 n + 0 \u2261 n\nplusrident zero = refl\nplusrident (suc n) = cong suc (plusrident n)\n\n----------------------------------------------------------------------\n\nrecord Universe : Set\u2081 where\n field\n Codes : Set\n Meaning : Codes \u2192 Set\n\n----------------------------------------------------------------------\n\ndata Even : \u2115 \u2192 Set where\n ezero : Even 0\n esuc : {n : \u2115} \u2192 Even n \u2192 Even (2 + n)\n\ndata Odd : \u2115 \u2192 Set where\n ozero : Odd 1\n osuc : {n : \u2115} \u2192 Odd n \u2192 Odd (2 + n)\n\n----------------------------------------------------------------------\n\ndata TypeForm (U : Universe) : Set\n\u27e6_\/_\u27e7 : (U : Universe) \u2192 TypeForm U \u2192 Set\n\ndata TypeForm U where\n `\u22a5 `\u22a4 `Bool `\u2115 `Type : TypeForm U\n `Fin `Even `Odd : (n : \u2115) \u2192 TypeForm U\n `\u03a0 `\u03a3 : (A : TypeForm U)\n (B : \u27e6 U \/ A \u27e7 \u2192 TypeForm U)\n \u2192 TypeForm U\n `Id : (A : TypeForm U) (x y : \u27e6 U \/ A \u27e7) \u2192 TypeForm U\n `\u27e6_\u27e7 : (A : Universe.Codes U) \u2192 TypeForm U\n\n\u27e6 U \/ `\u22a5 \u27e7 = \u22a5\n\u27e6 U \/ `\u22a4 \u27e7 = \u22a4\n\u27e6 U \/ `Bool \u27e7 = Bool\n\u27e6 U \/ `\u2115 \u27e7 = \u2115\n\u27e6 U \/ `Fin n \u27e7 = Fin n\n\u27e6 U \/ `Even n \u27e7 = Even n\n\u27e6 U \/ `Odd n \u27e7 = Odd n\n\u27e6 U \/ `\u03a0 A B \u27e7 = (a : \u27e6 U \/ A \u27e7) \u2192 \u27e6 U \/ B a \u27e7\n\u27e6 U \/ `\u03a3 A B \u27e7 = \u03a3 \u27e6 U \/ A \u27e7 (\u03bb a \u2192 \u27e6 U \/ B a \u27e7)\n\u27e6 U \/ `Id A x y \u27e7 = _\u2261_ {A = \u27e6 U \/ A \u27e7} x y\n\u27e6 U \/ `Type \u27e7 = Universe.Codes U\n\u27e6 U \/ `\u27e6 A \u27e7 \u27e7 = Universe.Meaning U A\n\n----------------------------------------------------------------------\n\n_`\u2192_ : \u2200{U} (A B : TypeForm U) \u2192 TypeForm U\nA `\u2192 B = `\u03a0 A (\u03bb _ \u2192 B)\n\n_`\u00d7_ : \u2200{U} (A B : TypeForm U) \u2192 TypeForm U\nA `\u00d7 B = `\u03a3 A (\u03bb _ \u2192 B)\n\nLevel : (\u2113 : \u2115) \u2192 Universe\nLevel zero = record { Codes = \u22a5 ; Meaning = \u03bb() }\nLevel (suc \u2113) = record { Codes = TypeForm (Level \u2113)\n ; Meaning = \u27e6_\/_\u27e7 (Level \u2113) }\n\nType : \u2115 \u2192 Set\nType \u2113 = TypeForm (Level \u2113)\n\n\u27e6_\u2223_\u27e7 : (\u2113 : \u2115) \u2192 Type \u2113 \u2192 Set\n\u27e6 \u2113 \u2223 A \u27e7 = \u27e6 Level \u2113 \/ A \u27e7\n\n`Dynamic : (\u2113 : \u2115) \u2192 Type (suc \u2113)\n`Dynamic \u2113 = `\u03a3 `Type `\u27e6_\u27e7\n\n`Tactic : (\u2113 : \u2115) \u2192 Type (suc \u2113)\n`Tactic \u2113 = `Dynamic \u2113 `\u2192 `Dynamic \u2113\n\nTactic : (\u2113 : \u2115) \u2192 Set\nTactic \u2113 = \u27e6 suc \u2113 \u2223 `Tactic \u2113 \u27e7\n\n----------------------------------------------------------------------\n\nrm-plus0-Fin : (\u2113 : \u2115) \u2192 Tactic (suc \u2113)\nrm-plus0-Fin \u2113 (`\u27e6 `\u03a3 `\u2115 B \u27e7 , (n , b)) =\n `\u03a0 `\u2115 (\u03bb x \u2192 `Id `Type (B x) (`Fin (x + 0)))\n `\u2192\n `\u03a3 `\u2115 `Fin\n ,\n \u03bb f \u2192 n , subst Fin\n (plusrident n)\n (subst (\u03bb A \u2192 \u27e6 \u2113 \u2223 A \u27e7) (f n) b)\nrm-plus0-Fin \u2113 x = x\n\n----------------------------------------------------------------------\n\neg-rm-plus0-Fin : (\u2113 : \u2115) \u2192\n \u27e6 suc \u2113 \u2223 `\u27e6 `\u03a3 `\u2115 (\u03bb n \u2192 `Fin (n + zero)) \u27e7\n `\u2192 `\u27e6 `\u03a3 `\u2115 (\u03bb n \u2192 `Fin n) \u27e7 \u27e7\neg-rm-plus0-Fin \u2113 (n , i) = proj\u2082\n (rm-plus0-Fin \u2113 (`\u27e6 `\u03a3 `\u2115 (\u03bb n \u2192 `Fin (n + zero)) \u27e7 , (n , i)))\n (\u03bb _ \u2192 refl)\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"1b96b34f502b64afe8b0080793725a878bdd13d9","subject":"Added N-unf-ho.","message":"Added N-unf-ho.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/LeastFixedPoints\/N.agda","new_file":"notes\/fixed-points\/LeastFixedPoints\/N.agda","new_contents":"------------------------------------------------------------------------------\n-- Equivalence: N as the least fixed-point and N using Agda's data constructor\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule LeastFixedPoints.N where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- Auxiliary definitions and properties\n\nflip : {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\nflip f b a = f a b\n\ncong : (f : D \u2192 D) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2261 f y\ncong f refl = refl\n\n------------------------------------------------------------------------------\nmodule LFP where\n\n -- N is a least fixed-point of a functor\n\n -- The functor.\n NatF : (D \u2192 Set) \u2192 D \u2192 Set\n NatF A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n\n -- The natural numbers are the least fixed-point of NatF.\n postulate\n N : D \u2192 Set\n\n -- N is a pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the introduction rules.\n N-in : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n\n -- The higher-order version.\n N-in-ho : \u2200 {n} \u2192 NatF N n \u2192 N n\n\n -- N is the least pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the elimination rule of an inductively\n -- defined predicate.\n N-ind :\n (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n\n -- Higher-order version.\n N-ind-ho :\n (A : D \u2192 Set) \u2192 (\u2200 {n} \u2192 NatF A n \u2192 A n) \u2192 \u2200 {n} \u2192 N n \u2192 A n\n\n ----------------------------------------------------------------------------\n -- From\/to N-in\/N-in-ho.\n\n N-in\u2081 : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n N-in\u2081 = N-in-ho\n\n N-in-ho\u2081 : \u2200 {n} \u2192 NatF N n \u2192 N n\n N-in-ho\u2081 = N-in\u2081\n\n ----------------------------------------------------------------------------\n -- From\/to N-ind\/N-ind-ho\n\n N-ind' :\n (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind' = N-ind-ho\n\n N-ind-ho' :\n (A : D \u2192 Set) \u2192 (\u2200 {n} \u2192 NatF A n \u2192 A n) \u2192 \u2200 {n} \u2192 N n \u2192 A n\n N-ind-ho' = N-ind\n\n ----------------------------------------------------------------------------\n -- The data constructors of N.\n nzero : N zero\n nzero = N-in (inj\u2081 refl)\n\n nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n nsucc Nn = N-in (inj\u2082 (_ , refl , Nn))\n\n ----------------------------------------------------------------------------\n -- Because N is the least pre-fixed point of NatF (i.e. N-in and\n -- N-ind), we can proof that N is also a post-fixed point of NatF.\n\n -- N is a post-fixed point of NatF.\n N-unf : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n')\n N-unf = N-ind A h\n where\n A : D \u2192 Set\n A m = m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 N m')\n\n h : \u2200 {m} \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 m\u22610) = inj\u2081 m\u22610\n h (inj\u2082 (m' , prf , Am')) = inj\u2082 (m' , prf , helper Am')\n where\n helper : A m' \u2192 N m'\n helper (inj\u2081 prf') = subst N (sym prf') nzero\n helper (inj\u2082 (m'' , prf' , Am'')) = subst N (sym prf') (nsucc Am'')\n\n -- Higher-order version.\n N-unf-ho : \u2200 {n} \u2192 N n \u2192 NatF N n\n N-unf-ho = N-unf\n\n ----------------------------------------------------------------------------\n -- The induction principle for N *with* the hypothesis N n in the\n -- induction step using N-ind.\n\n N-ind\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2081 A A0 h Nn = \u2227-proj\u2082 (N-ind B h' Nn)\n where\n B : D \u2192 Set\n B n = N n \u2227 A n\n\n h' : \u2200 {m} \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 B m') \u2192 B m\n h' (inj\u2081 m\u22610) = subst B (sym m\u22610) (nzero , A0)\n h' (inj\u2082 (m' , prf , Nm' , Am')) =\n (subst N (sym prf) (nsucc Nm')) , subst A (sym prf) (h Nm' Am')\n\n ----------------------------------------------------------------------------\n -- The induction principle for N *without* the hypothesis N n in the\n -- induction step using N-ind\n\n N-ind\u2082 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2082 A A0 h = N-ind A h'\n where\n h' : \u2200 {m} \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h' (inj\u2081 m\u22610) = subst A (sym m\u22610) A0\n h' (inj\u2082 (m' , prf , Am')) = subst A (sym prf) (h Am')\n\n ----------------------------------------------------------------------------\n -- Example: We will use N-ind as the induction\n -- principle on N.\n\n postulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 (succ\u2081 d) + e \u2261 succ\u2081 (d + e)\n\n +-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n +-leftIdentity n = +-0x n\n\n +-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n +-N {n = n} Nm Nn = N-ind A h Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n h : \u2200 {m} \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 m\u22610) = subst N (cong (flip _+_ n) (sym m\u22610)) A0\n where\n A0 : A zero\n A0 = subst N (sym (+-leftIdentity n)) Nn\n h (inj\u2082 (m' , prf , Am')) =\n subst N (cong (flip _+_ n) (sym prf)) (is Am')\n where\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} Ai = subst N (sym (+-Sx i n)) (nsucc Ai)\n\n ----------------------------------------------------------------------------\n -- Example: A proof using N-unf.\n\n pred-N : \u2200 {n} \u2192 N n \u2192 N (pred\u2081 n)\n pred-N {n} Nn = case h\u2081 h\u2082 (N-unf Nn)\n where\n h\u2081 : n \u2261 zero \u2192 N (pred\u2081 n)\n h\u2081 n\u22610 = subst N (sym (trans (predCong n\u22610) pred-0)) nzero\n\n h\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n' \u2192 N (pred\u2081 n)\n h\u2082 (n' , prf , Nn') = subst N (sym (trans (predCong prf) (pred-S n'))) Nn'\n\n ----------------------------------------------------------------------------\n -- From\/to N as a least fixed-point to\/from N as data type.\n\n data N' : D \u2192 Set where\n nzero' : N' zero\n nsucc' : \u2200 {n} \u2192 N' n \u2192 N' (succ\u2081 n)\n\n N'\u2192N : \u2200 {n} \u2192 N' n \u2192 N n\n N'\u2192N nzero' = nzero\n N'\u2192N (nsucc' Nn) = nsucc (N'\u2192N Nn)\n\n -- Using N-ind\u2081.\n N\u2192N' : \u2200 {n} \u2192 N n \u2192 N' n\n N\u2192N' = N-ind\u2081 N' nzero' (\u03bb _ \u2192 nsucc')\n\n -- Using N-ind\u2082.\n N\u2192N'\u2081 : \u2200 {n} \u2192 N n \u2192 N' n\n N\u2192N'\u2081 = N-ind\u2082 N' nzero' nsucc'\n\n------------------------------------------------------------------------------\nmodule Data where\n\n data N : D \u2192 Set where\n nzero : N zero\n nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n\n -- The induction principle for N *with* the hypothesis N n in the\n -- induction step.\n N-ind\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2081 A A0 h nzero = A0\n N-ind\u2081 A A0 h (nsucc Nn) = h Nn (N-ind\u2081 A A0 h Nn)\n\n -- The induction principle for N *without* the hypothesis N n in the\n -- induction step.\n N-ind\u2082 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2082 A A0 h nzero = A0\n N-ind\u2082 A A0 h (nsucc Nn) = h (N-ind\u2082 A A0 h Nn)\n\n ----------------------------------------------------------------------------\n -- N-in.\n\n N-in : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n N-in {n} h = case prf\u2081 prf\u2082 h\n where\n prf\u2081 : n \u2261 zero \u2192 N n\n prf\u2081 n\u22610 = subst N (sym n\u22610) nzero\n\n prf\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n' \u2192 N n\n prf\u2082 (n' , prf , Nn') = subst N (sym prf) (nsucc Nn')\n\n ----------------------------------------------------------------------------\n -- From N-ind\u2082 to N-ind.\n\n N-ind' :\n (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind' A h = N-ind\u2082 A h\u2081 h\u2082\n where\n h\u2081 : A zero\n h\u2081 = h (inj\u2081 refl)\n\n h\u2082 : \u2200 {m} \u2192 A m \u2192 A (succ\u2081 m)\n h\u2082 {m} Am = h (inj\u2082 (m , refl , Am))\n","old_contents":"------------------------------------------------------------------------------\n-- Equivalence: N as the least fixed-point and N using Agda's data constructor\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule LeastFixedPoints.N where\n\nopen import FOTC.Base\nopen import FOTC.Base.PropertiesI\n\n------------------------------------------------------------------------------\n-- Auxiliary definitions and properties\n\nflip : {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2192 B \u2192 A \u2192 C\nflip f b a = f a b\n\ncong : (f : D \u2192 D) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2261 f y\ncong f refl = refl\n\n------------------------------------------------------------------------------\nmodule LFP where\n\n -- N is a least fixed-point of a functor\n\n -- The functor.\n NatF : (D \u2192 Set) \u2192 D \u2192 Set\n NatF A n = n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n')\n\n -- The natural numbers are the least fixed-point of NatF.\n postulate\n N : D \u2192 Set\n\n -- N is a pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the introduction rules.\n N-in : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n\n -- The higher-order version.\n N-in-ho : \u2200 {n} \u2192 NatF N n \u2192 N n\n\n -- N is the least pre-fixed point of NatF.\n --\n -- Peter: It corresponds to the elimination rule of an inductively\n -- defined predicate.\n N-ind :\n (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n\n -- Higher-order version.\n N-ind-ho :\n (A : D \u2192 Set) \u2192 (\u2200 {n} \u2192 NatF A n \u2192 A n) \u2192 \u2200 {n} \u2192 N n \u2192 A n\n\n ----------------------------------------------------------------------------\n -- From\/to N-in\/N-in-ho.\n\n N-in\u2081 : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n N-in\u2081 = N-in-ho\n\n N-in-ho\u2081 : \u2200 {n} \u2192 NatF N n \u2192 N n\n N-in-ho\u2081 = N-in\u2081\n\n ----------------------------------------------------------------------------\n -- From\/to N-ind\/N-ind-ho\n\n N-ind' :\n (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind' = N-ind-ho\n\n N-ind-ho' :\n (A : D \u2192 Set) \u2192 (\u2200 {n} \u2192 NatF A n \u2192 A n) \u2192 \u2200 {n} \u2192 N n \u2192 A n\n N-ind-ho' = N-ind\n\n ----------------------------------------------------------------------------\n -- The data constructors of N.\n nzero : N zero\n nzero = N-in (inj\u2081 refl)\n\n nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n nsucc Nn = N-in (inj\u2082 (_ , refl , Nn))\n\n ----------------------------------------------------------------------------\n -- Because N is the least pre-fixed point of NatF (i.e. N-in and\n -- N-ind), we can proof that N is also a post-fixed point of NatF.\n\n -- N is a post-fixed point of NatF.\n N-unf : \u2200 {n} \u2192 N n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n')\n N-unf = N-ind A h\n where\n A : D \u2192 Set\n A m = m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 N m')\n\n h : \u2200 {m} \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 m\u22610) = inj\u2081 m\u22610\n h (inj\u2082 (m' , prf , Am')) = inj\u2082 (m' , prf , helper Am')\n where\n helper : A m' \u2192 N m'\n helper (inj\u2081 prf') = subst N (sym prf') nzero\n helper (inj\u2082 (m'' , prf' , Am'')) = subst N (sym prf') (nsucc Am'')\n\n ----------------------------------------------------------------------------\n -- The induction principle for N *with* the hypothesis N n in the\n -- induction step using N-ind.\n\n N-ind\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2081 A A0 h Nn = \u2227-proj\u2082 (N-ind B h' Nn)\n where\n B : D \u2192 Set\n B n = N n \u2227 A n\n\n h' : \u2200 {m} \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 B m') \u2192 B m\n h' (inj\u2081 m\u22610) = subst B (sym m\u22610) (nzero , A0)\n h' (inj\u2082 (m' , prf , Nm' , Am')) =\n (subst N (sym prf) (nsucc Nm')) , subst A (sym prf) (h Nm' Am')\n\n ----------------------------------------------------------------------------\n -- The induction principle for N *without* the hypothesis N n in the\n -- induction step using N-ind\n\n N-ind\u2082 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2082 A A0 h = N-ind A h'\n where\n h' : \u2200 {m} \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h' (inj\u2081 m\u22610) = subst A (sym m\u22610) A0\n h' (inj\u2082 (m' , prf , Am')) = subst A (sym prf) (h Am')\n\n ----------------------------------------------------------------------------\n -- Example: We will use N-ind as the induction\n -- principle on N.\n\n postulate\n _+_ : D \u2192 D \u2192 D\n +-0x : \u2200 d \u2192 zero + d \u2261 d\n +-Sx : \u2200 d e \u2192 (succ\u2081 d) + e \u2261 succ\u2081 (d + e)\n\n +-leftIdentity : \u2200 n \u2192 zero + n \u2261 n\n +-leftIdentity n = +-0x n\n\n +-N : \u2200 {m n} \u2192 N m \u2192 N n \u2192 N (m + n)\n +-N {n = n} Nm Nn = N-ind A h Nm\n where\n A : D \u2192 Set\n A i = N (i + n)\n\n h : \u2200 {m} \u2192 m \u2261 zero \u2228 (\u2203[ m' ] m \u2261 succ\u2081 m' \u2227 A m') \u2192 A m\n h (inj\u2081 m\u22610) = subst N (cong (flip _+_ n) (sym m\u22610)) A0\n where\n A0 : A zero\n A0 = subst N (sym (+-leftIdentity n)) Nn\n h (inj\u2082 (m' , prf , Am')) =\n subst N (cong (flip _+_ n) (sym prf)) (is Am')\n where\n is : \u2200 {i} \u2192 A i \u2192 A (succ\u2081 i)\n is {i} Ai = subst N (sym (+-Sx i n)) (nsucc Ai)\n\n ----------------------------------------------------------------------------\n -- Example: A proof using N-unf.\n\n pred-N : \u2200 {n} \u2192 N n \u2192 N (pred\u2081 n)\n pred-N {n} Nn = case h\u2081 h\u2082 (N-unf Nn)\n where\n h\u2081 : n \u2261 zero \u2192 N (pred\u2081 n)\n h\u2081 n\u22610 = subst N (sym (trans (predCong n\u22610) pred-0)) nzero\n\n h\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n' \u2192 N (pred\u2081 n)\n h\u2082 (n' , prf , Nn') = subst N (sym (trans (predCong prf) (pred-S n'))) Nn'\n\n ----------------------------------------------------------------------------\n -- From\/to N as a least fixed-point to\/from N as data type.\n\n data N' : D \u2192 Set where\n nzero' : N' zero\n nsucc' : \u2200 {n} \u2192 N' n \u2192 N' (succ\u2081 n)\n\n N'\u2192N : \u2200 {n} \u2192 N' n \u2192 N n\n N'\u2192N nzero' = nzero\n N'\u2192N (nsucc' Nn) = nsucc (N'\u2192N Nn)\n\n -- Using N-ind\u2081.\n N\u2192N' : \u2200 {n} \u2192 N n \u2192 N' n\n N\u2192N' = N-ind\u2081 N' nzero' (\u03bb _ \u2192 nsucc')\n\n -- Using N-ind\u2082.\n N\u2192N'\u2081 : \u2200 {n} \u2192 N n \u2192 N' n\n N\u2192N'\u2081 = N-ind\u2082 N' nzero' nsucc'\n\n------------------------------------------------------------------------------\nmodule Data where\n\n data N : D \u2192 Set where\n nzero : N zero\n nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n\n -- The induction principle for N *with* the hypothesis N n in the\n -- induction step.\n N-ind\u2081 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 N n \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2081 A A0 h nzero = A0\n N-ind\u2081 A A0 h (nsucc Nn) = h Nn (N-ind\u2081 A A0 h Nn)\n\n -- The induction principle for N *without* the hypothesis N n in the\n -- induction step.\n N-ind\u2082 : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 A n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind\u2082 A A0 h nzero = A0\n N-ind\u2082 A A0 h (nsucc Nn) = h (N-ind\u2082 A A0 h Nn)\n\n ----------------------------------------------------------------------------\n -- N-in.\n\n N-in : \u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n') \u2192 N n\n N-in {n} h = case prf\u2081 prf\u2082 h\n where\n prf\u2081 : n \u2261 zero \u2192 N n\n prf\u2081 n\u22610 = subst N (sym n\u22610) nzero\n\n prf\u2082 : \u2203[ n' ] n \u2261 succ\u2081 n' \u2227 N n' \u2192 N n\n prf\u2082 (n' , prf , Nn') = subst N (sym prf) (nsucc Nn')\n\n ----------------------------------------------------------------------------\n -- From N-ind\u2082 to N-ind.\n\n N-ind' :\n (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 n \u2261 zero \u2228 (\u2203[ n' ] n \u2261 succ\u2081 n' \u2227 A n') \u2192 A n) \u2192\n \u2200 {n} \u2192 N n \u2192 A n\n N-ind' A h = N-ind\u2082 A h\u2081 h\u2082\n where\n h\u2081 : A zero\n h\u2081 = h (inj\u2081 refl)\n\n h\u2082 : \u2200 {m} \u2192 A m \u2192 A (succ\u2081 m)\n h\u2082 {m} Am = h (inj\u2082 (m , refl , Am))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"93e6584220412202dbb64437dd86bca11691e722","subject":"Added x+1\u2264x\u223810+11 and x\u226489\u2192x+11>100\u2192\u22a5 (by Ana Bove).","message":"Added x+1\u2264x\u223810+11 and x\u226489\u2192x+11>100\u2192\u22a5 (by Ana Bove).\n\nIgnore-this: b1dbc3a9d82d80f5abcd37691da80221\n\ndarcs-hash:20110211225502-3bd4e-2c25dac7312cce72f06454d59caa9d09ecc9426b.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/McCarthy91\/ArithmeticATP.agda","new_file":"Draft\/McCarthy91\/ArithmeticATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Arithmetic stuff used by the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.ArithmeticATP where\n\nopen import LTC.Base\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\n\n------------------------------------------------------------------------------\n\npostulate\n 1+x-N : \u2200 {n} \u2192 N n \u2192 N (one + n)\n 11+x-N : \u2200 {n} \u2192 N n \u2192 N (eleven + n)\n x\u223810-N : \u2200 {n} \u2192 N n \u2192 N (n \u2238 ten)\n x+11-N : \u2200 {n} \u2192 N n \u2192 N (n + eleven)\n{-# ATP prove 1+x-N #-}\n{-# ATP prove 11+x-N #-}\n{-# ATP prove x\u223810-N \u2238-N N10 #-}\n{-# ATP prove x+11-N 11+x-N +-comm #-}\n\npostulate\n n111' : eleven + one-hundred \u2261 hundred-eleven\n n111 : one-hundred + eleven \u2261 hundred-eleven\n n101' : hundred-eleven \u2238 ten \u2261 hundred-one\n n101 : (one-hundred + eleven) \u2238 ten \u2261 hundred-one\n n91' : hundred-one \u2238 ten \u2261 ninety-one\n n91 : ((one-hundred + eleven) \u2238 ten) \u2238 ten \u2261 ninety-one\n n102' : eleven + ninety-one \u2261 hundred-two\n n102 : ninety-one + eleven \u2261 hundred-two\n n100' : eleven + eighty-nine \u2261 one-hundred\n n100 : eighty-nine + eleven \u2261 one-hundred\n{-# ATP prove n111' #-}\n{-# ATP prove n111 +-comm n111' #-}\n{-# ATP prove n101' #-}\n{-# ATP prove n101 n111 n101' #-}\n{-# ATP prove n91' #-}\n{-# ATP prove n91 n101 n91' #-}\n{-# ATP prove n102' #-}\n{-# ATP prove n102 n102' +-comm #-}\n{-# ATP prove n100' #-}\n{-# ATP prove n100 n100' +-comm #-}\n\npostulate\n 101>100' : GT hundred-one one-hundred\n 101>100 : GT ((one-hundred + eleven) \u2238 ten) one-hundred\n 111>100' : GT hundred-eleven one-hundred\n 111>100 : GT (one-hundred + eleven) one-hundred\n 100<102' : LT one-hundred hundred-two\n 100<102 : LT one-hundred (ninety-one + eleven)\n{-# ATP prove 101>100' #-}\n{-# ATP prove 101>100 101>100' n101 #-}\n{-# ATP prove 111>100' #-}\n{-# ATP prove 111>100 111>100' n111 #-}\n{-# ATP prove 100<102' #-}\n{-# ATP prove 100<102 100<102' n102 #-}\n\npostulate 91>100\u2192\u22a5 : GT ninety-one one-hundred \u2192 \u22a5\n{-# ATP prove 91>100\u2192\u22a5 #-}\n\npostulate x+1\u2264x\u223810+11 : \u2200 {n} \u2192 N n \u2192 LE (n + one) ((n \u2238 ten) + eleven)\n{-# ATP prove x+1\u2264x\u223810+11 x\u2264y+x-y N10 1+x-N 11+x-N x\u223810-N +-comm #-}\n\npostulate x\u226489\u2192x+11>100\u2192\u22a5 : \u2200 {n} \u2192 N n \u2192 LE n eighty-nine \u2192\n GT (n + eleven) one-hundred \u2192 \u22a5\n{-# ATP prove x\u226489\u2192x+11>100\u2192\u22a5 x>y\u2192x\u2264y\u2192\u22a5 x\u2264y\u2192x+k\u2264y+k x+11-N N89 N100 n100 #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Arithmetic stuff used by the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule Draft.McCarthy91.ArithmeticATP where\n\nopen import LTC.Base\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.Unary.Numbers\nopen import LTC.Data.Nat.Unary.IsN-ATP\n\n------------------------------------------------------------------------------\n\npostulate\n 1+x-N : \u2200 {n} \u2192 N n \u2192 N (one + n)\n 11+x-N : \u2200 {n} \u2192 N n \u2192 N (eleven + n)\n x\u223810-N : \u2200 {n} \u2192 N n \u2192 N (n \u2238 ten)\n x+11-N : \u2200 {n} \u2192 N n \u2192 N (n + eleven)\n{-# ATP prove 1+x-N #-}\n{-# ATP prove 11+x-N #-}\n{-# ATP prove x\u223810-N \u2238-N N10 #-}\n{-# ATP prove x+11-N 11+x-N +-comm #-}\n\npostulate\n n111' : eleven + one-hundred \u2261 hundred-eleven\n n111 : one-hundred + eleven \u2261 hundred-eleven\n n101' : hundred-eleven \u2238 ten \u2261 hundred-one\n n101 : (one-hundred + eleven) \u2238 ten \u2261 hundred-one\n n91' : hundred-one \u2238 ten \u2261 ninety-one\n n91 : ((one-hundred + eleven) \u2238 ten) \u2238 ten \u2261 ninety-one\n n102' : eleven + ninety-one \u2261 hundred-two\n n102 : ninety-one + eleven \u2261 hundred-two\n n100' : eleven + eighty-nine \u2261 one-hundred\n n100 : eighty-nine + eleven \u2261 one-hundred\n{-# ATP prove n111' #-}\n{-# ATP prove n111 +-comm n111' #-}\n{-# ATP prove n101' #-}\n{-# ATP prove n101 n111 n101' #-}\n{-# ATP prove n91' #-}\n{-# ATP prove n91 n101 n91' #-}\n{-# ATP prove n102' #-}\n{-# ATP prove n102 n102' +-comm #-}\n{-# ATP prove n100' #-}\n{-# ATP prove n100 n100' +-comm #-}\n\npostulate\n 101>100' : GT hundred-one one-hundred\n 101>100 : GT ((one-hundred + eleven) \u2238 ten) one-hundred\n 111>100' : GT hundred-eleven one-hundred\n 111>100 : GT (one-hundred + eleven) one-hundred\n 100<102' : LT one-hundred hundred-two\n 100<102 : LT one-hundred (ninety-one + eleven)\n{-# ATP prove 101>100' #-}\n{-# ATP prove 101>100 101>100' n101 #-}\n{-# ATP prove 111>100' #-}\n{-# ATP prove 111>100 111>100' n111 #-}\n{-# ATP prove 100<102' #-}\n{-# ATP prove 100<102 100<102' n102 #-}\n\npostulate 91>100\u2192\u22a5 : GT ninety-one one-hundred \u2192 \u22a5\n{-# ATP prove 91>100\u2192\u22a5 #-}","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"1c937f93f2d8a6adf90e821c16730959c7d7b238","subject":"Removed nil.","message":"Removed nil.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Base.agda","new_file":"src\/fot\/FOTC\/Base.agda","new_contents":"------------------------------------------------------------------------------\n-- The first-order theory of combinators (FOTC) base\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n{-\nFOTC The logical framework (Agda)\n\n* Logical constants * Curry-Howard isomorphism\n* Equality * Identity type\n* Term language and conversion rules * Postulates\n* Inductively defined predicates * Inductive families\n* Co-inductively defined predicates * Greatest fixed-points\n-}\n\n-- References:\n--\n-- \u2022 Peter Aczel. The strength of Martin-L\u00f6f's intuitionistic type\n-- theory with one universe. In Miettinen and V\u00e4\u00e4nanen,\n-- editors. Proc. of the Symposium on Mathematical Logic (Oulu,\n-- 1974), Report No. 2, Department of Philosopy, University of\n-- Helsinki, Helsinki, 1977, pages 1\u201332.\n\nmodule FOTC.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfixl 9 _\u00b7_ -- The symbol is '\\cdot'.\ninfixr 8 _\u2237_\ninfix 8 if_then_else_\n\n------------------------------------------------------------------------------\n-- First-order logic with equality.\nopen import Common.FOL.FOL-Eq public\n\n-- Common definitions.\nopen import Common.DefinitionsATP public\n\n------------------------------------------------------------------------------\n-- The term language of FOTC\n\n-- t ::= x | t \u00b7 t |\n-- | true | false | if\n-- | 0 | succ | pred | iszero\n-- | nil | cons | null | head | tail\n-- | loop\n\npostulate\n _\u00b7_ : D \u2192 D \u2192 D -- FOTC application.\n true false if : D -- FOTC partial Booleans.\n zero succ pred iszero : D -- FOTC partial natural numbers.\n [] cons head tail null : D -- FOTC lists.\n loop : D -- FOTC looping programs.\n\n------------------------------------------------------------------------------\n-- Definitions\n\n-- We define some function symbols for convenience in writing and\n-- looking for an optimization for the ATPs.\n\n-- 2012-03-20. The definitions are inside an abstract block because\n-- the conversion rules (see below) are based on them, so want to\n-- avoid their expansion.\n\nabstract\n if_then_else_ : D \u2192 D \u2192 D \u2192 D\n if b then d\u2081 else d\u2082 = if \u00b7 b \u00b7 d\u2081 \u00b7 d\u2082\n -- {-# ATP definition if_then_else_ #-}\n\n succ\u2081 : D \u2192 D\n succ\u2081 n = succ \u00b7 n\n -- {-# ATP definition succ\u2081 #-}\n\n pred\u2081 : D \u2192 D\n pred\u2081 n = pred \u00b7 n\n -- {-# ATP definition pred\u2081 #-}\n\n iszero\u2081 : D \u2192 D\n iszero\u2081 n = iszero \u00b7 n\n -- {-# ATP definition iszero\u2081 #-}\n\n _\u2237_ : D \u2192 D \u2192 D\n x \u2237 xs = cons \u00b7 x \u00b7 xs\n -- {-# ATP definition _\u2237_ #-}\n\n head\u2081 : D \u2192 D\n head\u2081 xs = head \u00b7 xs\n -- {-# ATP definition head\u2081 #-}\n\n tail\u2081 : D \u2192 D\n tail\u2081 xs = tail \u00b7 xs\n -- {-# ATP definition tail\u2081 #-}\n\n null\u2081 : D \u2192 D\n null\u2081 xs = null \u00b7 xs\n -- {-# ATP definition null\u2081 #-}\n\n------------------------------------------------------------------------------\n-- Conversion rules\n\n-- The conversion relation _conv_ satifies (Aczel 1977, p. 8)\n--\n-- x conv y <=> FOTC \u22a2 x \u2261 y,\n--\n-- therefore, we introduce the conversion rules as non-logical axioms.\n\n-- N.B. Looking for an optimization for the ATPs, we write the\n-- conversion rules on the defined function symbols instead of on the\n-- term constants.\n\n-- Conversion rules for Booleans.\npostulate\n -- if-true : \u2200 d\u2081 {d\u2082} \u2192 if \u00b7 true \u00b7 d\u2081 \u00b7 d\u2082 \u2261 d\u2081\n -- if-false : \u2200 {d\u2081} d\u2082 \u2192 if \u00b7 false \u00b7 d\u2081 \u00b7 d\u2082 \u2261 d\u2082\n if-true : \u2200 d\u2081 {d\u2082} \u2192 if true then d\u2081 else d\u2082 \u2261 d\u2081\n if-false : \u2200 {d\u2081} d\u2082 \u2192 if false then d\u2081 else d\u2082 \u2261 d\u2082\n{-# ATP axiom if-true if-false #-}\n\n-- Conversion rules for pred.\npostulate\n -- N.B. We don't need this equation.\n -- pred-0 : pred \u00b7 zero \u2261 zero\n -- pred-S : \u2200 n \u2192 pred \u00b7 (succ \u00b7 n) \u2261 n\n pred-S : \u2200 n \u2192 pred\u2081 (succ\u2081 n) \u2261 n\n{-# ATP axiom pred-S #-}\n\n-- Conversion rules for iszero.\npostulate\n -- iszero-0 : iszero \u00b7 zero \u2261 true\n -- iszero-S : \u2200 n \u2192 iszero \u00b7 (succ \u00b7 n) \u2261 false\n iszero-0 : iszero\u2081 zero \u2261 true\n iszero-S : \u2200 n \u2192 iszero\u2081 (succ\u2081 n) \u2261 false\n{-# ATP axiom iszero-0 iszero-S #-}\n\n-- Conversion rules for null.\npostulate\n -- null-[] : null \u00b7 nil \u2261 true\n -- null-\u2237 : \u2200 x xs \u2192 null \u00b7 (cons \u00b7 x \u00b7 xs) \u2261 false\n null-[] : null\u2081 [] \u2261 true\n null-\u2237 : \u2200 x xs \u2192 null\u2081 (x \u2237 xs) \u2261 false\n\n-- Conversion rule for head.\npostulate\n-- head-\u2237 : \u2200 x xs \u2192 head \u00b7 (cons \u00b7 x \u00b7 xs) \u2261 x\n head-\u2237 : \u2200 x xs \u2192 head\u2081 (x \u2237 xs) \u2261 x\n{-# ATP axiom head-\u2237 #-}\n\n-- Conversion rule for tail.\npostulate\n-- tail-\u2237 : \u2200 x xs \u2192 tail \u00b7 (cons \u00b7 x \u00b7 xs) \u2261 xs\n tail-\u2237 : \u2200 x xs \u2192 tail\u2081 (x \u2237 xs) \u2261 xs\n{-# ATP axiom tail-\u2237 #-}\n\n-- Conversion rule for loop.\n--\n-- The equation loop-eq adds anything to the logic (because\n-- reflexivity is already an axiom of equality), therefore we won't\n-- add this equation as a first-order logic axiom.\npostulate loop-eq : loop \u2261 loop\n\n------------------------------------------------------------------------------\n-- Discrimination rules\n\npostulate\n true\u2262false : true \u2262 false\n-- 0\u2262S : \u2200 {n} \u2192 zero \u2262 succ \u00b7 n\n 0\u2262S : \u2200 {n} \u2192 zero \u2262 succ\u2081 n\n{-# ATP axiom true\u2262false 0\u2262S #-}\n","old_contents":"------------------------------------------------------------------------------\n-- The first-order theory of combinators (FOTC) base\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n{-\nFOTC The logical framework (Agda)\n\n* Logical constants * Curry-Howard isomorphism\n* Equality * Identity type\n* Term language and conversion rules * Postulates\n* Inductively defined predicates * Inductive families\n* Co-inductively defined predicates * Greatest fixed-points\n-}\n\n-- References:\n--\n-- \u2022 Peter Aczel. The strength of Martin-L\u00f6f's intuitionistic type\n-- theory with one universe. In Miettinen and V\u00e4\u00e4nanen,\n-- editors. Proc. of the Symposium on Mathematical Logic (Oulu,\n-- 1974), Report No. 2, Department of Philosopy, University of\n-- Helsinki, Helsinki, 1977, pages 1\u201332.\n\nmodule FOTC.Base where\n\n-- We add 3 to the fixities of the standard library.\ninfixl 9 _\u00b7_ -- The symbol is '\\cdot'.\ninfixr 8 _\u2237_\ninfix 8 if_then_else_\n\n------------------------------------------------------------------------------\n-- First-order logic with equality.\nopen import Common.FOL.FOL-Eq public\n\n-- Common definitions.\nopen import Common.DefinitionsATP public\n\n------------------------------------------------------------------------------\n-- The term language of FOTC\n\n-- t ::= x | t \u00b7 t |\n-- | true | false | if\n-- | 0 | succ | pred | iszero\n-- | nil | cons | null | head | tail\n-- | loop\n\npostulate\n _\u00b7_ : D \u2192 D \u2192 D -- FOTC application.\n true false if : D -- FOTC partial Booleans.\n zero succ pred iszero : D -- FOTC partial natural numbers.\n nil cons head tail null : D -- FOTC lists.\n loop : D -- FOTC looping programs.\n\n------------------------------------------------------------------------------\n-- Definitions\n\n-- We define some function symbols for convenience in writing and\n-- looking for an optimization for the ATPs.\n\n-- 2012-03-20. The definitions are inside an abstract block because\n-- the conversion rules (see below) are based on them, so want to\n-- avoid their expansion.\n\nabstract\n if_then_else_ : D \u2192 D \u2192 D \u2192 D\n if b then d\u2081 else d\u2082 = if \u00b7 b \u00b7 d\u2081 \u00b7 d\u2082\n -- {-# ATP definition if_then_else_ #-}\n\n succ\u2081 : D \u2192 D\n succ\u2081 n = succ \u00b7 n\n -- {-# ATP definition succ\u2081 #-}\n\n pred\u2081 : D \u2192 D\n pred\u2081 n = pred \u00b7 n\n -- {-# ATP definition pred\u2081 #-}\n\n iszero\u2081 : D \u2192 D\n iszero\u2081 n = iszero \u00b7 n\n -- {-# ATP definition iszero\u2081 #-}\n\n [] : D\n [] = nil\n -- {-# ATP definition [] #-}\n\n _\u2237_ : D \u2192 D \u2192 D\n x \u2237 xs = cons \u00b7 x \u00b7 xs\n -- {-# ATP definition _\u2237_ #-}\n\n head\u2081 : D \u2192 D\n head\u2081 xs = head \u00b7 xs\n -- {-# ATP definition head\u2081 #-}\n\n tail\u2081 : D \u2192 D\n tail\u2081 xs = tail \u00b7 xs\n -- {-# ATP definition tail\u2081 #-}\n\n null\u2081 : D \u2192 D\n null\u2081 xs = null \u00b7 xs\n -- {-# ATP definition null\u2081 #-}\n\n------------------------------------------------------------------------------\n-- Conversion rules\n\n-- The conversion relation _conv_ satifies (Aczel 1977, p. 8)\n--\n-- x conv y <=> FOTC \u22a2 x \u2261 y,\n--\n-- therefore, we introduce the conversion rules as non-logical axioms.\n\n-- N.B. Looking for an optimization for the ATPs, we write the\n-- conversion rules on the defined function symbols instead of on the\n-- term constants.\n\n-- Conversion rules for Booleans.\npostulate\n -- if-true : \u2200 d\u2081 {d\u2082} \u2192 if \u00b7 true \u00b7 d\u2081 \u00b7 d\u2082 \u2261 d\u2081\n -- if-false : \u2200 {d\u2081} d\u2082 \u2192 if \u00b7 false \u00b7 d\u2081 \u00b7 d\u2082 \u2261 d\u2082\n if-true : \u2200 d\u2081 {d\u2082} \u2192 if true then d\u2081 else d\u2082 \u2261 d\u2081\n if-false : \u2200 {d\u2081} d\u2082 \u2192 if false then d\u2081 else d\u2082 \u2261 d\u2082\n{-# ATP axiom if-true if-false #-}\n\n-- Conversion rules for pred.\npostulate\n -- N.B. We don't need this equation.\n -- pred-0 : pred \u00b7 zero \u2261 zero\n -- pred-S : \u2200 n \u2192 pred \u00b7 (succ \u00b7 n) \u2261 n\n pred-S : \u2200 n \u2192 pred\u2081 (succ\u2081 n) \u2261 n\n{-# ATP axiom pred-S #-}\n\n-- Conversion rules for iszero.\npostulate\n -- iszero-0 : iszero \u00b7 zero \u2261 true\n -- iszero-S : \u2200 n \u2192 iszero \u00b7 (succ \u00b7 n) \u2261 false\n iszero-0 : iszero\u2081 zero \u2261 true\n iszero-S : \u2200 n \u2192 iszero\u2081 (succ\u2081 n) \u2261 false\n{-# ATP axiom iszero-0 iszero-S #-}\n\n-- Conversion rules for null.\npostulate\n -- null-[] : null \u00b7 nil \u2261 true\n -- null-\u2237 : \u2200 x xs \u2192 null \u00b7 (cons \u00b7 x \u00b7 xs) \u2261 false\n null-[] : null\u2081 [] \u2261 true\n null-\u2237 : \u2200 x xs \u2192 null\u2081 (x \u2237 xs) \u2261 false\n\n-- Conversion rule for head.\npostulate\n-- head-\u2237 : \u2200 x xs \u2192 head \u00b7 (cons \u00b7 x \u00b7 xs) \u2261 x\n head-\u2237 : \u2200 x xs \u2192 head\u2081 (x \u2237 xs) \u2261 x\n{-# ATP axiom head-\u2237 #-}\n\n-- Conversion rule for tail.\npostulate\n-- tail-\u2237 : \u2200 x xs \u2192 tail \u00b7 (cons \u00b7 x \u00b7 xs) \u2261 xs\n tail-\u2237 : \u2200 x xs \u2192 tail\u2081 (x \u2237 xs) \u2261 xs\n{-# ATP axiom tail-\u2237 #-}\n\n-- Conversion rule for loop.\n--\n-- The equation loop-eq adds anything to the logic (because\n-- reflexivity is already an axiom of equality), therefore we won't\n-- add this equation as a first-order logic axiom.\npostulate loop-eq : loop \u2261 loop\n\n------------------------------------------------------------------------------\n-- Discrimination rules\n\npostulate\n true\u2262false : true \u2262 false\n-- 0\u2262S : \u2200 {n} \u2192 zero \u2262 succ \u00b7 n\n 0\u2262S : \u2200 {n} \u2192 zero \u2262 succ\u2081 n\n{-# ATP axiom true\u2262false 0\u2262S #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b1479e21c569e836f0a8509f99b5b4c287c68821","subject":"Added missing local hint.","message":"Added missing local hint.\n\nIgnore-this: 82466ef5652a782ebf50430560b5d833\n\ndarcs-hash:20110219052705-3bd4e-13b3ac8b81002750798d46f8af10fd7bf4555c01.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/LTC\/Program\/McCarthy91\/ArithmeticATP.agda","new_file":"src\/LTC\/Program\/McCarthy91\/ArithmeticATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties used by the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule LTC.Program.McCarthy91.ArithmeticATP where\n\nopen import LTC.Base\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.UnaryNumbers\nopen import LTC.Data.Nat.UnaryNumbers.IsN-ATP\n\n------------------------------------------------------------------------------\n\npostulate\n 91\u2261[100+11\u223810]\u223810 : (one-hundred + eleven \u2238 ten) \u2238 ten \u2261 ninety-one\n{-# ATP prove 91\u2261[100+11\u223810]\u223810 #-}\n\npostulate\n 100\u226189+11 : eighty-nine + eleven \u2261 one-hundred\n 101\u226190+11\u2261101 : ninety + eleven \u2261 hundred-one\n 101\u2261100+11-10 : (one-hundred + eleven) \u2238 ten \u2261 hundred-one\n 102\u226191+11 : ninety-one + eleven \u2261 hundred-two\n 103\u226192+1 : ninety-two + eleven \u2261 hundred-three\n 104\u226193+11 : ninety-three + eleven \u2261 hundred-four\n 105\u226194+11 : ninety-four + eleven \u2261 hundred-five\n 106\u226195+11 : ninety-five + eleven \u2261 hundred-six\n 107\u226196+11 : ninety-six + eleven \u2261 hundred-seven\n 108\u226197+11 : ninety-seven + eleven \u2261 hundred-eight\n 109\u226199+11 : ninety-eight + eleven \u2261 hundred-nine\n 110\u226199+11 : ninety-nine + eleven \u2261 hundred-ten\n 111\u2261100+11 : one-hundred + eleven \u2261 hundred-eleven\n\n{-# ATP prove 100\u226189+11 #-}\n{-# ATP prove 101\u226190+11\u2261101 #-}\n{-# ATP prove 101\u2261100+11-10 #-}\n{-# ATP prove 102\u226191+11 #-}\n{-# ATP prove 103\u226192+1 #-}\n{-# ATP prove 104\u226193+11 #-}\n{-# ATP prove 105\u226194+11 #-}\n{-# ATP prove 106\u226195+11 #-}\n{-# ATP prove 107\u226196+11 #-}\n{-# ATP prove 108\u226197+11 #-}\n{-# ATP prove 109\u226199+11 #-}\n{-# ATP prove 110\u226199+11 #-}\n{-# ATP prove 111\u2261100+11 #-}\n\npostulate\n 101>100' : GT hundred-one one-hundred\n 101>100 : GT ((one-hundred + eleven) \u2238 ten) one-hundred\n 102>100 : GT hundred-two one-hundred\n 103>100 : GT hundred-three one-hundred\n 104>100 : GT hundred-four one-hundred\n 105>100 : GT hundred-five one-hundred\n 106>100 : GT hundred-six one-hundred\n 107>100 : GT hundred-seven one-hundred\n 108>100 : GT hundred-eight one-hundred\n 109>100 : GT hundred-nine one-hundred\n 110>100 : GT hundred-ten one-hundred\n 111>100' : GT hundred-eleven one-hundred\n 111>100 : GT (one-hundred + eleven) one-hundred\n{-# ATP prove 101>100' #-}\n{-# ATP prove 101>100 101>100' 101\u2261100+11-10 #-}\n{-# ATP prove 102>100 #-}\n{-# ATP prove 103>100 #-}\n{-# ATP prove 104>100 #-}\n{-# ATP prove 105>100 #-}\n{-# ATP prove 106>100 #-}\n{-# ATP prove 107>100 #-}\n{-# ATP prove 108>100 #-}\n{-# ATP prove 109>100 #-}\n{-# ATP prove 110>100 #-}\n{-# ATP prove 111>100' #-}\n{-# ATP prove 111>100 111>100' 111\u2261100+11 #-}\n\npostulate\n 99+11>100 : GT (ninety-nine + eleven) one-hundred\n 98+11>100 : GT (ninety-eight + eleven) one-hundred\n 97+11>100 : GT (ninety-seven + eleven) one-hundred\n 96+11>100 : GT (ninety-six + eleven) one-hundred\n 95+11>100 : GT (ninety-five + eleven) one-hundred\n 94+11>100 : GT (ninety-four + eleven) one-hundred\n 93+11>100 : GT (ninety-three + eleven) one-hundred\n 92+11>100 : GT (ninety-two + eleven) one-hundred\n 91+11>100 : GT (ninety-one + eleven) one-hundred\n 90+11>100 : GT (ninety + eleven) one-hundred\n{-# ATP prove 99+11>100 110>100 110\u226199+11 #-}\n{-# ATP prove 98+11>100 109>100 109\u226199+11 #-}\n{-# ATP prove 97+11>100 108>100 108\u226197+11 #-}\n{-# ATP prove 96+11>100 107>100 107\u226196+11 #-}\n{-# ATP prove 95+11>100 106>100 106\u226195+11 #-}\n{-# ATP prove 94+11>100 105>100 105\u226194+11 #-}\n{-# ATP prove 93+11>100 104>100 104\u226193+11 #-}\n{-# ATP prove 92+11>100 103>100 103\u226192+1 #-}\n{-# ATP prove 91+11>100 102>100 102\u226191+11 #-}\n{-# ATP prove 90+11>100 101>100' 101\u226190+11\u2261101 #-}\n\npostulate\n 100<102' : LT one-hundred hundred-two\n 100<102 : LT one-hundred (ninety-one + eleven)\n{-# ATP prove 100<102' #-}\n{-# ATP prove 100<102 100<102' 102\u226191+11 #-}\n\nx+11-N : \u2200 {n} \u2192 N n \u2192 N (n + eleven)\nx+11-N Nn = +-N Nn N11\n\nx+11\u223810\u2261Sx : \u2200 {n} \u2192 N n \u2192 (n + eleven) \u2238 ten \u2261 succ n\nx+11\u223810\u2261Sx Nn = [x+Sy]\u2238y\u2261Sx Nn N10\n\npostulate 91>100\u2192\u22a5 : GT ninety-one one-hundred \u2192 \u22a5\n{-# ATP prove 91>100\u2192\u22a5 #-}\n\npostulate x+1\u2264x\u223810+11 : \u2200 {n} \u2192 N n \u2192 LE (n + one) ((n \u2238 ten) + eleven)\n{-# ATP prove x+1\u2264x\u223810+11 x\u2264y+x\u2238y N10 N11 +-N \u2238-N +-comm #-}\n\npostulate x\u226489\u2192x+11>100\u2192\u22a5 : \u2200 {n} \u2192 N n \u2192 LE n eighty-nine \u2192\n GT (n + eleven) one-hundred \u2192 \u22a5\n{-# ATP prove x\u226489\u2192x+11>100\u2192\u22a5 x>y\u2192x\u2264y\u2192\u22a5 x\u2264y\u2192x+k\u2264y+k x+11-N N89 N100 100\u226189+11 #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Arithmetic properties used by the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule LTC.Program.McCarthy91.ArithmeticATP where\n\nopen import LTC.Base\n\nopen import LTC.Data.Nat\nopen import LTC.Data.Nat.Inequalities\nopen import LTC.Data.Nat.Inequalities.PropertiesATP\nopen import LTC.Data.Nat.PropertiesATP\nopen import LTC.Data.Nat.UnaryNumbers\nopen import LTC.Data.Nat.UnaryNumbers.IsN-ATP\n\n------------------------------------------------------------------------------\n\npostulate\n 91\u2261[100+11\u223810]\u223810 : (one-hundred + eleven \u2238 ten) \u2238 ten \u2261 ninety-one\n{-# ATP prove 91\u2261[100+11\u223810]\u223810 #-}\n\npostulate\n 100\u226189+11 : eighty-nine + eleven \u2261 one-hundred\n 101\u226190+11\u2261101 : ninety + eleven \u2261 hundred-one\n 101\u2261100+11-10 : (one-hundred + eleven) \u2238 ten \u2261 hundred-one\n 102\u226191+11 : ninety-one + eleven \u2261 hundred-two\n 103\u226192+1 : ninety-two + eleven \u2261 hundred-three\n 104\u226193+11 : ninety-three + eleven \u2261 hundred-four\n 105\u226194+11 : ninety-four + eleven \u2261 hundred-five\n 106\u226195+11 : ninety-five + eleven \u2261 hundred-six\n 107\u226196+11 : ninety-six + eleven \u2261 hundred-seven\n 108\u226197+11 : ninety-seven + eleven \u2261 hundred-eight\n 109\u226199+11 : ninety-eight + eleven \u2261 hundred-nine\n 110\u226199+11 : ninety-nine + eleven \u2261 hundred-ten\n 111\u2261100+11 : one-hundred + eleven \u2261 hundred-eleven\n\n{-# ATP prove 100\u226189+11 #-}\n{-# ATP prove 101\u226190+11\u2261101 #-}\n{-# ATP prove 101\u2261100+11-10 #-}\n{-# ATP prove 102\u226191+11 #-}\n{-# ATP prove 103\u226192+1 #-}\n{-# ATP prove 104\u226193+11 #-}\n{-# ATP prove 105\u226194+11 #-}\n{-# ATP prove 106\u226195+11 #-}\n{-# ATP prove 107\u226196+11 #-}\n{-# ATP prove 108\u226197+11 #-}\n{-# ATP prove 109\u226199+11 #-}\n{-# ATP prove 110\u226199+11 #-}\n{-# ATP prove 111\u2261100+11 #-}\n\npostulate\n 101>100' : GT hundred-one one-hundred\n 101>100 : GT ((one-hundred + eleven) \u2238 ten) one-hundred\n 102>100 : GT hundred-two one-hundred\n 103>100 : GT hundred-three one-hundred\n 104>100 : GT hundred-four one-hundred\n 105>100 : GT hundred-five one-hundred\n 106>100 : GT hundred-six one-hundred\n 107>100 : GT hundred-seven one-hundred\n 108>100 : GT hundred-eight one-hundred\n 109>100 : GT hundred-nine one-hundred\n 110>100 : GT hundred-ten one-hundred\n 111>100' : GT hundred-eleven one-hundred\n 111>100 : GT (one-hundred + eleven) one-hundred\n{-# ATP prove 101>100' #-}\n{-# ATP prove 101>100 101>100' 101\u2261100+11-10 #-}\n{-# ATP prove 102>100 #-}\n{-# ATP prove 103>100 #-}\n{-# ATP prove 104>100 #-}\n{-# ATP prove 105>100 #-}\n{-# ATP prove 106>100 #-}\n{-# ATP prove 107>100 #-}\n{-# ATP prove 108>100 #-}\n{-# ATP prove 109>100 #-}\n{-# ATP prove 110>100 #-}\n{-# ATP prove 111>100' #-}\n{-# ATP prove 111>100 111>100' 111\u2261100+11 #-}\n\npostulate\n 99+11>100 : GT (ninety-nine + eleven) one-hundred\n 98+11>100 : GT (ninety-eight + eleven) one-hundred\n 97+11>100 : GT (ninety-seven + eleven) one-hundred\n 96+11>100 : GT (ninety-six + eleven) one-hundred\n 95+11>100 : GT (ninety-five + eleven) one-hundred\n 94+11>100 : GT (ninety-four + eleven) one-hundred\n 93+11>100 : GT (ninety-three + eleven) one-hundred\n 92+11>100 : GT (ninety-two + eleven) one-hundred\n 91+11>100 : GT (ninety-one + eleven) one-hundred\n 90+11>100 : GT (ninety + eleven) one-hundred\n{-# ATP prove 99+11>100 110>100 110\u226199+11 #-}\n{-# ATP prove 98+11>100 109>100 109\u226199+11 #-}\n{-# ATP prove 97+11>100 108>100 108\u226197+11 #-}\n{-# ATP prove 96+11>100 107>100 107\u226196+11 #-}\n{-# ATP prove 95+11>100 106>100 106\u226195+11 #-}\n{-# ATP prove 94+11>100 105>100 105\u226194+11 #-}\n{-# ATP prove 93+11>100 104>100 104\u226193+11 #-}\n{-# ATP prove 92+11>100 103>100 103\u226192+1 #-}\n{-# ATP prove 91+11>100 102>100 102\u226191+11 #-}\n{-# ATP prove 90+11>100 101>100' 101\u226190+11\u2261101 #-}\n\npostulate\n 100<102' : LT one-hundred hundred-two\n 100<102 : LT one-hundred (ninety-one + eleven)\n{-# ATP prove 100<102' #-}\n{-# ATP prove 100<102 100<102' 102\u226191+11 #-}\n\nx+11-N : \u2200 {n} \u2192 N n \u2192 N (n + eleven)\nx+11-N Nn = +-N Nn N11\n\nx+11\u223810\u2261Sx : \u2200 {n} \u2192 N n \u2192 (n + eleven) \u2238 ten \u2261 succ n\nx+11\u223810\u2261Sx Nn = [x+Sy]\u2238y\u2261Sx Nn N10\n\npostulate 91>100\u2192\u22a5 : GT ninety-one one-hundred \u2192 \u22a5\n{-# ATP prove 91>100\u2192\u22a5 #-}\n\npostulate x+1\u2264x\u223810+11 : \u2200 {n} \u2192 N n \u2192 LE (n + one) ((n \u2238 ten) + eleven)\n{-# ATP prove x+1\u2264x\u223810+11 x\u2264y+x\u2238y N10 N11 +-N +-comm #-}\n\npostulate x\u226489\u2192x+11>100\u2192\u22a5 : \u2200 {n} \u2192 N n \u2192 LE n eighty-nine \u2192\n GT (n + eleven) one-hundred \u2192 \u22a5\n{-# ATP prove x\u226489\u2192x+11>100\u2192\u22a5 x>y\u2192x\u2264y\u2192\u22a5 x\u2264y\u2192x+k\u2264y+k x+11-N N89 N100 100\u226189+11 #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"accc893b55bf3a37d7657a72ec9ac1e69f23636d","subject":"Internal error in agda","message":"Internal error in agda\n","repos":"xekoukou\/sparrow,xekoukou\/sparrow,xekoukou\/sparrow,xekoukou\/sparrow","old_file":"agda\/LinFunContructw.agda","new_file":"agda\/LinFunContructw.agda","new_contents":"-- {-# OPTIONS --show-implicit #-}\n\nmodule LinFunContructw where\n\nopen import Common\nopen import LinLogic\nimport IndexLLProp \nopen import LinFun\nopen import SetLL\nopen import SetLLProp\n\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Product\n\nopen import LinFunContruct\n\n \nmodule _ where\n\n open IndexLLProp\n\n boo : \u2200{ i u ll pll ell cs} \u2192 \u2200 s \u2192 (eq : complL\u209b s \u2261 \u00ac\u2205 cs) \u2192 (ind : IndexLL {i} {u} pll ll) \u2192 (lind : IndexLL ell ll) \u2192 \u2200 \u00achob \u00achoh\n \u2192 let bind = \u00acho-shr-morph s eq ind \u00achob in\n let hind = \u00acho-shr-morph s eq lind \u00achoh in\n Ordered\u1d62 bind hind \u2192 Ordered\u1d62 ind lind\n boo \u2193 () ind lind \u00achob \u00achoh ord\n\n boo (s \u2190\u2227) eq \u2193 lind \u00achob \u00achoh ord = \u22a5-elim (\u00achob hitsAtLeastOnce\u2190\u2227\u2193)\n boo (s \u2190\u2227) eq (ind \u2190\u2227) \u2193 \u00achob \u00achoh ord = \u22a5-elim (\u00achoh hitsAtLeastOnce\u2190\u2227\u2193)\n boo (s \u2190\u2227) eq (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s\n boo (s \u2190\u2227) eq (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord | \u2205 | [ ieq ] = \u22a5-elim (\u00acnho (compl\u2261\u2205\u21d2ho s ieq ind)) where\n \u00acnho : \u00ac (hitsAtLeastOnce s ind)\n \u00acnho x = \u00achob (hitsAtLeastOnce\u2190\u2227\u2190\u2227 x)\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] with r where\n r = boo s ieq ind lind (\u03bb z \u2192 \u00achob (hitsAtLeastOnce\u2190\u2227\u2190\u2227 z)) (\u03bb z \u2192 \u00achoh (hitsAtLeastOnce\u2190\u2227\u2190\u2227 z)) (a\u2264\u1d62b y)\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] | a\u2264\u1d62b x\u2081 = a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 x\u2081)\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] | b\u2264\u1d62a x\u2081 = b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 x\u2081)\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] with r where\n r = boo s ieq ind lind (\u03bb z \u2192 \u00achob (hitsAtLeastOnce\u2190\u2227\u2190\u2227 z)) (\u03bb z \u2192 \u00achoh (hitsAtLeastOnce\u2190\u2227\u2190\u2227 z)) (b\u2264\u1d62a y)\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] | a\u2264\u1d62b x\u2081 = a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 x\u2081)\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] | b\u2264\u1d62a x\u2081 = b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 x\u2081)\n boo (s \u2190\u2227) eq (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s\n boo (s \u2190\u2227) eq (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh ord | \u2205 | [ ieq ] = \u22a5-elim (\u00acnho (compl\u2261\u2205\u21d2ho s ieq ind)) where\n \u00acnho : \u00ac (hitsAtLeastOnce s ind)\n \u00acnho x = \u00achob (hitsAtLeastOnce\u2190\u2227\u2190\u2227 x)\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh (a\u2264\u1d62b ()) | \u00ac\u2205 x | [ ieq ]\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh (b\u2264\u1d62a ()) | \u00ac\u2205 x | [ ieq ]\n boo (s \u2190\u2227) eq (\u2227\u2192 ind) \u2193 \u00achob \u00achoh ord = \u22a5-elim (\u00achoh hitsAtLeastOnce\u2190\u2227\u2193) \n boo (s \u2190\u2227) eq (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s\n boo (s \u2190\u2227) eq (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh ord | \u2205 | [ ieq ] = \u22a5-elim (\u00acnho (compl\u2261\u2205\u21d2ho s ieq lind)) where\n \u00acnho : \u00ac (hitsAtLeastOnce s lind)\n \u00acnho x = \u00achoh (hitsAtLeastOnce\u2190\u2227\u2190\u2227 x)\n boo (s \u2190\u2227) refl (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b ()) | \u00ac\u2205 x | [ ieq ]\n boo (s \u2190\u2227) refl (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a ()) | \u00ac\u2205 x | [ ieq ]\n boo {ll = lll \u2227 rll} (s \u2190\u2227) eq (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh ord with complL\u209b s \n boo {ll = lll \u2227 rll} (s \u2190\u2227) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh ord | \u2205 with shrink rll (fillAllLower rll) | shr-fAL-id rll\n boo {u = _} {lll \u2227 rll} (s \u2190\u2227) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (a\u2264\u1d62b x) | \u2205 | .rll | refl = a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 x)\n boo {u = _} {lll \u2227 rll} (s \u2190\u2227) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (b\u2264\u1d62a x) | \u2205 | .rll | refl = b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 x)\n boo {ll = lll \u2227 rll} (s \u2190\u2227) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh ord | \u00ac\u2205 x with shrink rll (fillAllLower rll) | shr-fAL-id rll\n boo {u = _} {lll \u2227 rll} (s \u2190\u2227) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | .rll | refl = a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 y)\n boo {u = _} {lll \u2227 rll} (s \u2190\u2227) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | .rll | refl = b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 y)\n boo (\u2227\u2192 s) eq \u2193 lind \u00achob \u00achoh ord = \u22a5-elim (\u00achob hitsAtLeastOnce\u2227\u2192\u2193)\n boo (\u2227\u2192 s) eq (\u2227\u2192 ind) \u2193 \u00achob \u00achoh ord = \u22a5-elim (\u00achoh hitsAtLeastOnce\u2227\u2192\u2193)\n boo (\u2227\u2192 s) eq (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s\n boo (\u2227\u2192 s) eq (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh ord | \u2205 | [ ieq ] = \u22a5-elim (\u00acnho (compl\u2261\u2205\u21d2ho s ieq ind)) where\n \u00acnho : \u00ac (hitsAtLeastOnce s ind)\n \u00acnho x = \u00achob (hitsAtLeastOnce\u2227\u2192\u2227\u2192 x)\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | [ ieq ] with r where\n r = boo s ieq ind lind (\u03bb z \u2192 \u00achob (hitsAtLeastOnce\u2227\u2192\u2227\u2192 z)) (\u03bb z \u2192 \u00achoh (hitsAtLeastOnce\u2227\u2192\u2227\u2192 z)) (a\u2264\u1d62b y)\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | [ ieq ] | a\u2264\u1d62b x\u2081 = a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 x\u2081)\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | [ ieq ] | b\u2264\u1d62a x\u2081 = b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 x\u2081)\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | [ ieq ] with r where\n r = boo s ieq ind lind (\u03bb z \u2192 \u00achob (hitsAtLeastOnce\u2227\u2192\u2227\u2192 z)) (\u03bb z \u2192 \u00achoh (hitsAtLeastOnce\u2227\u2192\u2227\u2192 z)) (b\u2264\u1d62a y)\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | [ ieq ] | a\u2264\u1d62b x\u2081 = a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 x\u2081)\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | [ ieq ] | b\u2264\u1d62a x\u2081 = b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 x\u2081)\n boo (\u2227\u2192 s) eq (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s\n boo (\u2227\u2192 s) eq (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh ord | \u2205 | [ ieq ] = \u22a5-elim (\u00acnho (compl\u2261\u2205\u21d2ho s ieq ind)) where\n \u00acnho : \u00ac (hitsAtLeastOnce s ind)\n \u00acnho x = \u00achob (hitsAtLeastOnce\u2227\u2192\u2227\u2192 x)\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b ()) | \u00ac\u2205 x | [ ieq ]\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a ()) | \u00ac\u2205 x | [ ieq ]\n boo (\u2227\u2192 s) eq (ind \u2190\u2227) \u2193 \u00achob \u00achoh ord = \u22a5-elim (\u00achoh hitsAtLeastOnce\u2227\u2192\u2193) \n boo (\u2227\u2192 s) eq (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s\n boo (\u2227\u2192 s) eq (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh ord | \u2205 | [ ieq ] = \u22a5-elim (\u00acnho (compl\u2261\u2205\u21d2ho s ieq lind)) where\n \u00acnho : \u00ac (hitsAtLeastOnce s lind)\n \u00acnho x = \u00achoh (hitsAtLeastOnce\u2227\u2192\u2227\u2192 x)\n boo (\u2227\u2192 s) refl (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh (a\u2264\u1d62b ()) | \u00ac\u2205 x | [ ieq ]\n boo (\u2227\u2192 s) refl (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh (b\u2264\u1d62a ()) | \u00ac\u2205 x | [ ieq ]\n boo {ll = lll \u2227 rll} (\u2227\u2192 s) eq (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord with complL\u209b s \n boo {ll = lll \u2227 rll} (\u2227\u2192 s) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord | \u2205 with shrink lll (fillAllLower lll) | shr-fAL-id lll\n boo {u = _} {lll \u2227 rll} (\u2227\u2192 s) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b x) | \u2205 | .lll | refl = a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 x)\n boo {u = _} {lll \u2227 rll} (\u2227\u2192 s) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a x) | \u2205 | .lll | refl = b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 x)\n boo {ll = lll \u2227 rll} (\u2227\u2192 s) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord | \u00ac\u2205 x with shrink lll (fillAllLower lll) | shr-fAL-id lll\n boo {u = _} {lll \u2227 rll} (\u2227\u2192 s) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | .lll | refl = a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 y)\n boo {u = _} {lll \u2227 rll} (\u2227\u2192 s) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | .lll | refl = b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 y)\n\n\n\n\n boo (s \u2190\u2227\u2192 s\u2081) eq \u2193 lind \u00achob \u00achoh ord = \u22a5-elim (\u00achob hitsAtLeastOnce\u2190\u2227\u2192\u2193)\n boo (s \u2190\u2227\u2192 s\u2081) eq (ind \u2190\u2227) \u2193 \u00achob \u00achoh ord = \u22a5-elim (\u00achoh hitsAtLeastOnce\u2190\u2227\u2192\u2193)\n boo (s \u2190\u2227\u2192 s\u2081) eq (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s | complL\u209b s\u2081\n boo (s \u2190\u2227\u2192 s\u2081) eq (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord | \u2205 | [ ieq ] | e = \u22a5-elim (\u00acnho (compl\u2261\u2205\u21d2ho s ieq ind)) where\n \u00acnho : \u00ac (hitsAtLeastOnce s ind)\n \u00acnho x = \u00achob (hitsAtLeastOnce\u2190\u2227\u2192\u2190\u2227 x)\n boo (s \u2190\u2227\u2192 s\u2081) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord | \u00ac\u2205 x | [ ieq ] | \u2205 with boo s ieq ind lind (\u03bb x\u2081 \u2192 \u00achob (hitsAtLeastOnce\u2190\u2227\u2192\u2190\u2227 x\u2081)) (\u03bb x\u2081 \u2192 \u00achoh (hitsAtLeastOnce\u2190\u2227\u2192\u2190\u2227 x\u2081)) ord\n boo (s \u2190\u2227\u2192 s\u2081) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord | \u00ac\u2205 x | [ ieq ] | \u2205 | a\u2264\u1d62b y = a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 y)\n boo (s \u2190\u2227\u2192 s\u2081) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord | \u00ac\u2205 x | [ ieq ] | \u2205 | b\u2264\u1d62a y = b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 y)\n boo (s \u2190\u2227\u2192 s\u2081) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] | \u00ac\u2205 x\u2081 with boo s ieq ind lind (\u03bb x\u2081 \u2192 \u00achob (hitsAtLeastOnce\u2190\u2227\u2192\u2190\u2227 x\u2081)) (\u03bb x\u2081 \u2192 \u00achoh (hitsAtLeastOnce\u2190\u2227\u2192\u2190\u2227 x\u2081)) (a\u2264\u1d62b y)\n boo (s \u2190\u2227\u2192 s\u2081) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] | \u00ac\u2205 x\u2081 | a\u2264\u1d62b z = a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 z)\n boo (s \u2190\u2227\u2192 s\u2081) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] | \u00ac\u2205 x\u2081 | b\u2264\u1d62a z = b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 z)\n boo (s \u2190\u2227\u2192 s\u2081) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] | \u00ac\u2205 x\u2081 with boo s ieq ind lind (\u03bb x\u2081 \u2192 \u00achob (hitsAtLeastOnce\u2190\u2227\u2192\u2190\u2227 x\u2081)) (\u03bb x\u2081 \u2192 \u00achoh (hitsAtLeastOnce\u2190\u2227\u2192\u2190\u2227 x\u2081)) (b\u2264\u1d62a y)\n boo (s \u2190\u2227\u2192 s\u2081) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] | \u00ac\u2205 x\u2081 | a\u2264\u1d62b z = a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 z)\n boo (s \u2190\u2227\u2192 s\u2081) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] | \u00ac\u2205 x\u2081 | b\u2264\u1d62a z = b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 z)\n boo (s \u2190\u2227\u2192 s\u2081) eq (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s | complL\u209b s\u2081\n ... | g | t | e = ?\n boo (s \u2190\u2227\u2192 s\u2081) eq (\u2227\u2192 ind) lind \u00achob \u00achoh ord = {!!}\n\n boo (s \u2190\u2228) eq ind lind \u00achob \u00achoh ord = {!!}\n boo (\u2228\u2192 s) eq ind lind \u00achob \u00achoh ord = {!!}\n boo (s \u2190\u2228\u2192 s\u2081) eq ind lind \u00achob \u00achoh ord = {!!}\n boo (s \u2190\u2202) eq ind lind \u00achob \u00achoh ord = {!!}\n boo (\u2202\u2192 s) eq ind lind \u00achob \u00achoh ord = {!!}\n boo (s \u2190\u2202\u2192 s\u2081) eq ind lind \u00achob \u00achoh ord = {!!}\n\n\n private\n data M\u00acho {i u ll n dt df} (s : SetLL ll) : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll \u2192 Set where\n \u2205 : M\u00acho s \u2205\n \u00ac\u2205 : {ind : IndexLL (\u03c4 {i} {u} {n} {dt} df) ll} \u2192 (\u00acho : \u00ac (hitsAtLeastOnce s ind))\n \u2192 M\u00acho s (\u00ac\u2205 ind)\n\n hf : \u2200{i u n dt df} \u2192 \u2200 ll \u2192 \u2200{cs} \u2192 (ind : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll)\n \u2192 (s : SetLL ll) \u2192 (ceq : complL\u209b s \u2261 \u00ac\u2205 cs) \u2192 (m\u00acho : M\u00acho s ind) \u2192 LinLogic i {u}\n \u2192 LinLogic i {u}\n hf ll {cs} \u2205 s ceq mnho cll = shrinkcms ll s cs ceq\n hf ll {cs} (\u00ac\u2205 x) s ceq (\u00ac\u2205 \u00acho) cll = replLL (shrinkcms ll s cs ceq) (\u00acho-shr-morph s ceq x \u00acho) cll\n\n hf2 : \u2200{i u ll rll n dt df ts} \u2192 (ind : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll)\n \u2192 (s : SetLL ll) \u2192 (m\u00acho : M\u00acho s ind) \u2192 (lf : LFun {i} {u} ll rll)\n \u2192 \u00ac\u2205 ts \u2261 tranLFMSetLL lf (\u00ac\u2205 s)\n \u2192 M\u00acho ts (tranLFMIndexLL lf ind)\n hf2 \u2205 mnho s lf eqs = \u2205\n hf2 (\u00ac\u2205 x) s mnho lf eqs with tranLFMIndexLL lf (\u00ac\u2205 x) | inspect (\u03bb z \u2192 tranLFMIndexLL lf (\u00ac\u2205 z)) x\n hf2 (\u00ac\u2205 x) s mnho lf eqs | \u2205 | [ eq ] = \u2205\n hf2 (\u00ac\u2205 x) s (\u00ac\u2205 \u00acho) lf eqs | \u00ac\u2205 _ | [ eq ] = \u00ac\u2205 (tranLF-preserves-\u00acho lf x s \u00acho (sym eq) eqs)\n\n\n data MLFun {i u ll rll n dt df} (mind : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll)\n (s : SetLL ll) (m\u00acho : M\u00acho s mind) (lf : LFun {i} {u} ll rll) : Set (lsuc u) where\n \u2205 : \u2200{ts} \u2192 (\u2205 \u2261 mind) \u2192 (complL\u209b s \u2261 \u2205)\n \u2192 (eqs : \u00ac\u2205 ts \u2261 tranLFMSetLL lf (\u00ac\u2205 s)) \u2192 (ceqo : complL\u209b ts \u2261 \u2205) \u2192 MLFun mind s m\u00acho lf\n \u00ac\u2205\u2205 : \u2200 {ind cs} \u2192 (\u00ac\u2205 ind \u2261 mind) \u2192 (ceqi : complL\u209b s \u2261 \u00ac\u2205 cs)\n \u2192 (eqs : \u2205 \u2261 tranLFMSetLL lf (\u00ac\u2205 s))\n \u2192 (cll : LinLogic i {u}) \u2192 LFun (hf ll mind s ceqi m\u00acho cll) rll\n \u2192 MLFun mind s m\u00acho lf \n \u00ac\u2205\u00ac\u2205 : \u2200 {cs ts cts} \u2192 (ceqi : complL\u209b s \u2261 \u00ac\u2205 cs)\n \u2192 let tind = tranLFMIndexLL lf mind in\n (eqs : \u00ac\u2205 ts \u2261 tranLFMSetLL lf (\u00ac\u2205 s)) \u2192 (ceqo : complL\u209b ts \u2261 \u00ac\u2205 cts)\n \u2192 ((cll : LinLogic i {u}) \u2192 LFun (hf ll mind s ceqi m\u00acho cll) (hf rll tind ts ceqo (hf2 mind s m\u00acho lf eqs) cll))\n \u2192 MLFun mind s m\u00acho lf \n --??? We will never reach to a point where complL\u209b ts \u2261 \u2205 because\n -- the input would have the same fate. ( s becomes smaller as we go forward, thus complL\u209b increases. Here we take the case where s is not \u2205.\n -- Correction : In fact , the ordinal remains the same since all the points of the set need to to end at the same com by design. (but we might not be able to prove this now and thus need to go with the weaker argument.)\n \n\n\n\n-- s is special, all of the input points eventually will be consumed by a single com + the point from the index. Thus if complL\u209b s \u2261 \u2205, this means that lf does not contain coms or calls.\n-- Maybe one day, we will provide a datatype that contains that information, for the time being, we use IMPOSSIBLE where necessary.\n\n -- s here does contain the ind.\n -- TODO IMPORTANT After test , we need to remove \u2282 that contain I in the first place.\n test : \u2200{i u rll ll n dt df} \u2192 (ind : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll) \u2192 (s : SetLL ll)\n \u2192 \u2200 m\u00acho \u2192 (lf : LFun ll rll) \u2192 MLFun ind s m\u00acho lf\n test ind s mnho lf with complL\u209b s | inspect complL\u209b s\n test \u2205 s \u2205 lf | \u2205 | [ e ] with tranLFMSetLL lf (\u00ac\u2205 s) | inspect (\u03bb z \u2192 tranLFMSetLL lf (\u00ac\u2205 z)) s\n test \u2205 s \u2205 lf | \u2205 | [ e ] | \u2205 | [ r ] = IMPOSSIBLE\n test \u2205 s \u2205 lf | \u2205 | [ e ] | \u00ac\u2205 x | [ r ] with complL\u209b x | inspect complL\u209b x\n test \u2205 s \u2205 lf | \u2205 | [ e ] | \u00ac\u2205 x | [ r ] | \u2205 | [ t ] = \u2205 refl e (sym r) t\n test \u2205 s \u2205 lf | \u2205 | [ e ] | \u00ac\u2205 x | [ r ] | \u00ac\u2205 x\u2081 | t = IMPOSSIBLE\n test (\u00ac\u2205 x) s (\u00ac\u2205 \u00acho) lf | \u2205 | [ e ] = \u22a5-elim (\u00acho (compl\u2261\u2205\u21d2ho s e x)) \n\n test \u2205 s mnho I | \u00ac\u2205 x | [ eq ] = \u00ac\u2205\u00ac\u2205 eq refl eq (\u03bb cll \u2192 I)\n test (\u00ac\u2205 x\u2081) s (\u00ac\u2205 \u00acho) I | \u00ac\u2205 x | [ eq ] = \u00ac\u2205\u00ac\u2205 eq refl eq (\u03bb cll \u2192 I)\n\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] with truncSetLL s lind | inspect (truncSetLL s) lind\n ... | \u2205 | [ teq ] with (mreplacePartOf (\u00ac\u2205 s) to (\u2205 {ll = ell}) at lind) | inspect (\u03bb z \u2192 mreplacePartOf (\u00ac\u2205 s) to (\u2205 {ll = ell}) at z) lind\n ... | \u2205 | [ meq ] = \u22a5-elim ((trunc\u2261\u2205\u21d2\u00acmrpls\u2261\u2205 s lind teq) meq)\n ... | \u00ac\u2205 mx | [ meq ] with test {n = n} {dt} {df} \u2205 mx \u2205 lf\u2081\n ... | \u2205 indeq ceq eqs ceqo = \u22a5-elim ((del\u21d2\u00acho s lind (sym meq)) (compl\u2261\u2205\u21d2ho mx ceq (a\u2264\u1d62b-morph lind lind ell (\u2264\u1d62-reflexive lind))))\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u2205 | [ teq ] | \u00ac\u2205 mx | [ meq ] | \u00ac\u2205\u2205 () ceqi eqs cll t\n ... | \u00ac\u2205\u00ac\u2205 {ts = ts} {cts} ceqi eqs ceqo t = \u00ac\u2205\u00ac\u2205 eq tseq ceqo ((\u03bb z \u2192 _\u2282_ {ind = \u00acho-shr-morph s eq lind (trunc\u2261\u2205\u21d2\u00acho s lind teq)} lf (g (t z)))) where\n tseq = sym (trans (cong (\u03bb z \u2192 tranLFMSetLL lf\u2081 z) (trans (cong (\u03bb z \u2192 mreplacePartOf \u00ac\u2205 s to tranLFMSetLL lf z at lind) teq) meq)) (sym eqs))\n g = subst (\u03bb a \u2192 LFun a (shrink rll cts)) (shrink-repl-comm s lind eq teq meq ceqi)\n test {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] with test {n = n} {dt} {df} \u2205 trs \u2205 lf\n ... | \u2205 indeq ceq eqs ceqo with (mreplacePartOf (\u00ac\u2205 s) to (tranLFMSetLL lf (\u00ac\u2205 trs)) at lind) | inspect (\u03bb z \u2192 mreplacePartOf (\u00ac\u2205 s) to (tranLFMSetLL lf (\u00ac\u2205 trs)) at z) lind\n ... | \u00ac\u2205 mx | r with test {n = n} {dt} {df} \u2205 mx \u2205 lf\u2081\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq eqs ceqo | \u00ac\u2205 mx | [ meq ] | \u2205 indeq1 ceq1 eqs1 ceqo1 with tranLFMSetLL lf (\u00ac\u2205 trs)\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq eqs ceqo | \u00ac\u2205 mx | [ meq ] | \u2205 indeq1 ceq1 eqs1 ceqo1 | \u2205 = IMPOSSIBLE -- IMPOSSIBLE and not provable with the current information we have.\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq eqs ceqo | \u00ac\u2205 mx | [ meq ] | \u2205 indeq1 ceq1 eqs1 ceqo1 | \u00ac\u2205 x with compl\u2261\u00ac\u2205\u21d2replace-compl\u2261\u00ac\u2205 s lind eq teq ceq x\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq eqs ceqo | \u00ac\u2205 .(replacePartOf s to x at ind) | [ refl ] | \u2205 indeq1 ceq1 eqs1 ceqo1 | \u00ac\u2205 x | proj\u2083 , proj\u2084 with complL\u209b (replacePartOf s to x at ind)\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq eqs ceqo | \u00ac\u2205 .(replacePartOf s to x at ind) | [ refl ] | \u2205 indeq1 refl eqs1 ceqo1 | \u00ac\u2205 x | proj\u2083 , () | .\u2205\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq eqs ceqo | \u00ac\u2205 mx | [ meq ] | \u00ac\u2205\u2205 () ceqi1 eqs1 cll1 t1\n test {rll = rll} {ll = ll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 {ts = ts} indeq ceq eqs ceqo | \u00ac\u2205 mx | [ meq ] | \u00ac\u2205\u00ac\u2205 {cs} ceqi1 eqs1 ceqo1 t1\n = \u00ac\u2205\u00ac\u2205 eq tseq ceqo1 ((\u03bb cll \u2192 subst (\u03bb z \u2192 LFun z (shrink rll _)) (shrink-repl\u2261\u2205 s lind eq teq ceq _ ceqo t) (t1 cll))) where\n tseq = sym (trans (cong (\u03bb z \u2192 tranLFMSetLL lf\u2081 z) (trans (cong (\u03bb z \u2192 mreplacePartOf \u00ac\u2205 s to tranLFMSetLL lf z at lind) teq) meq)) (sym eqs1))\n t : complL\u209b (replacePartOf s to ts at lind) \u2261 \u00ac\u2205 cs\n t with trans (cong (\u03bb z \u2192 mreplacePartOf \u00ac\u2205 s to z at lind) eqs) meq\n ... | g with replacePartOf s to ts at lind\n t | refl | e = ceqi1\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq eqs ceqo | \u2205 | [ meq ] with tranLFMSetLL lf (\u00ac\u2205 trs)\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq refl ceqo | \u2205 | [ () ] | .(\u00ac\u2205 _)\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind\u2081} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u2205 () ceqi eqs cll t\n test {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t\n with (mreplacePartOf (\u00ac\u2205 s) to (tranLFMSetLL lf (\u00ac\u2205 trs)) at lind) | inspect (\u03bb z \u2192 mreplacePartOf (\u00ac\u2205 s) to (tranLFMSetLL lf (\u00ac\u2205 trs)) at z) lind\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t | \u2205 | [ meq ] with tranLFMSetLL lf (\u00ac\u2205 trs)\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi () ceqo t | \u2205 | [ meq ] | \u2205\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t | \u2205 | [ () ] | \u00ac\u2205 _\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t | \u00ac\u2205 mx | [ meq ] with test {n = n} {dt} {df} \u2205 mx \u2205 lf\u2081\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t | \u00ac\u2205 mx | [ meq ] | \u2205 indeq ceq1 eqs1 ceqo1 with tranLFMSetLL lf (\u00ac\u2205 trs)\n test {rll = rll} {ll = ll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 {ts = ts} ceqi refl ceqo t | \u00ac\u2205 .(replacePartOf s to _ at lind) | [ refl ] | \u2205 indeq1 ceq1 eqs1 ceqo1 | .(\u00ac\u2205 _) with complL\u209b ts | ct where\n m = replacePartOf s to ts at lind\n mind = subst (\u03bb z \u2192 IndexLL z (replLL ll lind ell)) (replLL-\u2193 lind) ((a\u2264\u1d62b-morph lind lind ell (\u2264\u1d62-reflexive lind)))\n ct = compl\u2261\u2205\u21d2compltr\u2261\u2205 m ceq1 mind (sym (tr-repl\u21d2id s lind ts))\n test {rll = rll} {ll} {n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 {ts = ts} ceqi refl () t | \u00ac\u2205 .(replacePartOf s to ts at ind) | [ refl ] | \u2205 indeq ceq1 eqs1 ceqo1 | .(\u00ac\u2205 ts) | .\u2205 | refl\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t | \u00ac\u2205 mx | [ meq ] | \u00ac\u2205\u2205 () ceqi\u2081 eqs\u2081 cll1 x\u2081\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t | \u00ac\u2205 mx | [ meq ] | \u00ac\u2205\u00ac\u2205 ceqi1 eqs1 ceqo1 t1\n = \u00ac\u2205\u00ac\u2205 eq tseq ceqo1 (\u03bb cll \u2192 _\u2282_ {ind = hind} (t cll) (subst (\u03bb z \u2192 LFun z _) (sym (ho-shrink-repl-comm s lind eq teq ceqi ceqo w ceqi1)) (t1 cll))) where\n w = trans (cong (\u03bb z \u2192 mreplacePartOf \u00ac\u2205 s to z at lind) eqs) meq\n tseq = sym (trans (cong (\u03bb z \u2192 tranLFMSetLL lf\u2081 z) (trans (cong (\u03bb z \u2192 mreplacePartOf \u00ac\u2205 s to tranLFMSetLL lf z at lind) teq) meq)) (sym eqs1))\n hind = ho-shr-morph s eq lind (sym teq) ceqi\n test {rll = rll} {n = n} {dt} {df} (\u00ac\u2205 ind) s (\u00ac\u2205 \u00acho) (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] with truncSetLL s lind | inspect (truncSetLL s) lind\n ... | \u2205 | [ teq ] with (mreplacePartOf (\u00ac\u2205 s) to (\u2205 {ll = ell}) at lind) | inspect (\u03bb z \u2192 mreplacePartOf (\u00ac\u2205 s) to (\u2205 {ll = ell}) at z) lind\n ... | \u2205 | [ meq ] = \u22a5-elim ((trunc\u2261\u2205\u21d2\u00acmrpls\u2261\u2205 s lind teq) meq)\n ... | \u00ac\u2205 mx | [ meq ] with isLTi lind ind\n ... | no \u00acp with test {n = n} {dt} {df} nind mx (\u00ac\u2205 n\u00acho) lf\u2081 where\n nord = ind\u03c4&\u00acge\u21d2\u00acOrd ind lind \u00acp\n nind = \u00ac\u2205 (\u00acord-morph ind lind ell (flipNotOrd\u1d62 nord))\n n\u00acho = \u00acord&\u00acho-del\u21d2\u00acho ind s \u00acho lind nord (sym meq)\n ... | \u2205 () ceq eqs ceqo\n test {rll = rll} {n = n} {dt} {df} (\u00ac\u2205 ind) s (\u00ac\u2205 \u00acho) (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u2205 | [ teq ] | \u00ac\u2205 mx | [ meq ] | no \u00acp | \u00ac\u2205\u2205 {ind = tind} indeq ceqi eqs cll t\n = \u00ac\u2205\u2205 refl eq tseq cll (_\u2282_ {ind = sind} lf {!best!}) where \n nord = ind\u03c4&\u00acge\u21d2\u00acOrd ind lind \u00acp\n tseq = sym (trans (cong (\u03bb z \u2192 tranLFMSetLL lf\u2081 z) (trans (cong (\u03bb z \u2192 mreplacePartOf \u00ac\u2205 s to tranLFMSetLL lf z at lind) teq) meq)) (sym eqs))\n hind = \u00acho-shr-morph s eq lind ((trunc\u2261\u2205\u21d2\u00acho s lind teq))\n bind = \u00acho-shr-morph s eq ind \u00acho\n sind = \u00acord-morph hind bind cll {!!}\n best = replLL-\u00acordab\u2261ba bind cll hind ell {!!}\n\n find = \u00acord-morph ind lind ell (flipNotOrd\u1d62 nord)\n nind = \u00acord-morph lind ind cll nord\n test {rll = rll} {n = n} {dt} {df} (\u00ac\u2205 ind) s (\u00ac\u2205 \u00acho) (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u2205 | [ teq ] | \u00ac\u2205 mx | [ meq ] | no \u00acp | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t = {!!}\n test {rll = rll} {n = n} {dt} {df} (\u00ac\u2205 ind) s (\u00ac\u2205 \u00acho) (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u2205 | [ teq ] | \u00ac\u2205 mx | [ meq ] | yes p = {!!}\n test (\u00ac\u2205 ind\u2081) s (\u00ac\u2205 \u00acho) (_\u2282_ {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] = {!!}\n test ind s mnho (tr ltr lf) | \u00ac\u2205 x | eq = {!!}\n test ind s mnho (com df\u2081 lf) | \u00ac\u2205 x | eq = {!!}\n test ind s mnho (call x\u2081) | \u00ac\u2205 x | eq = {!!}\n\n\n\n\n\n-- test {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] with tranLFMSetLL lf (\u00ac\u2205 trs) | inspect (\u03bb z \u2192 tranLFMSetLL lf (\u00ac\u2205 z)) trs | test {n = n} {dt} {df} \u2205 trs \u2205 lf\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 | [ tseq ] | I x eqs ceqo with tranLFMSetLL lf (\u00ac\u2205 trs)\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 | [ refl ] | I x () ceqo | .\u2205\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 | tseq | \u00acI\u2205 ceqi eqs x = {!!}\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 | [ tseq ] | \u00acI\u00ac\u2205 ceqi eqs ceqo x with tranLFMSetLL lf (\u00ac\u2205 trs)\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 | [ refl ] | \u00acI\u00ac\u2205 ceqi () ceqo x | .(\u00ac\u2205 _)\n--\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205 ts | tseq | I x eqs ceqo = {!!}\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205 ts | tseq | \u00acI\u2205 ceqi eqs x = {!!}\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205 ts | tseq | \u00acI\u00ac\u2205 ceqi eqs ceqo x = {!!}\n--\n--\n---- with test {n = n} {dt} {df} \u2205 trs \u2205 lf\n---- ... | I ceq with mreplacePartOf (\u00ac\u2205 s) to tlf at lind | inspect (\u03bb z \u2192 mreplacePartOf (\u00ac\u2205 s) to tlf at z) lind where -- tranLFMSetLL lf\u2081 nmx | inspect (tranLFMSetLL lf\u2081) nmx | test \u2205 {!nmx!} \u2205 lf\u2081 where\n---- tlf = tranLFMSetLL lf (\u00ac\u2205 trs)\n---- -- is = test \u2205 {!!} \u2205 lf\u2081\n---- ... | \u2205 | [ meq ] = IMPOSSIBLE -- Since we have I, lf cannot contain com or call, thus tlf is not \u2205.\n---- ... | \u00ac\u2205 mx | [ meq ] with test {n = n} {dt} {df} \u2205 mx \u2205 lf\u2081\n---- ... | I ceq1 = IMPOSSIBLE -- TODO This is impossible because eq assures us that complL\u209b s \u2261 \u00ac\u2205 x but complL\u209b of both ceq and ceq1 are \u2205.\n---- ... | \u00acI\u2205 ceqi1 eqs1 t1 = \u00acI\u2205 eq tseq {!!} where\n---- meeq = subst (\u03bb z \u2192 (mreplacePartOf \u00ac\u2205 s to tranLFMSetLL lf z at lind) \u2261 \u00ac\u2205 mx) (sym teq) meq\n---- tseq = subst (\u03bb z \u2192 \u2205 \u2261 tranLFMSetLL lf\u2081 z) (sym meeq) eqs1\n---- ... | \u00acI\u00ac\u2205 ceqi1 eqs1 ceqo1 t1 = {!!} \n---- test {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00acI\u2205 ceqi eqs q = {!!}\n---- test {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00acI\u00ac\u2205 ceqi eqs ceqo q = {!!} where\n-- test \u2205 s \u2205 (tr ltr lf) | \u00ac\u2205 x | [ eq ] = {!!}\n-- test \u2205 s \u2205 (com df\u2081 lf) | \u00ac\u2205 x | [ eq ] = IMPOSSIBLE -- Since ind is \u2205, this is impossible.\n-- test \u2205 s \u2205 (call x\u2081) | \u00ac\u2205 x | [ eq ] = IMPOSSIBLE\n-- test (\u00ac\u2205 x) s (\u00ac\u2205 \u00acho) lf = {!!}\n-- \n-- \n \n \n \n \n \n","old_contents":"-- {-# OPTIONS --show-implicit #-}\n\nmodule LinFunContructw where\n\nopen import Common\nopen import LinLogic\nimport IndexLLProp \nopen import LinFun\nopen import SetLL\nopen import SetLLProp\n\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Product\n\nopen import LinFunContruct\n\n \nmodule _ where\n\n open IndexLLProp\n\n boo : \u2200{ i u ll pll ell cs} \u2192 \u2200 s \u2192 (eq : complL\u209b s \u2261 \u00ac\u2205 cs) \u2192 (ind : IndexLL {i} {u} pll ll) \u2192 (lind : IndexLL ell ll) \u2192 \u2200 \u00achob \u00achoh\n \u2192 let bind = \u00acho-shr-morph s eq ind \u00achob in\n let hind = \u00acho-shr-morph s eq lind \u00achoh in\n Ordered\u1d62 bind hind \u2192 Ordered\u1d62 ind lind\n boo \u2193 () ind lind \u00achob \u00achoh ord\n\n boo (s \u2190\u2227) eq \u2193 lind \u00achob \u00achoh ord = \u22a5-elim (\u00achob hitsAtLeastOnce\u2190\u2227\u2193)\n boo (s \u2190\u2227) eq (ind \u2190\u2227) \u2193 \u00achob \u00achoh ord = \u22a5-elim (\u00achoh hitsAtLeastOnce\u2190\u2227\u2193)\n boo (s \u2190\u2227) eq (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s\n boo (s \u2190\u2227) eq (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord | \u2205 | [ ieq ] = \u22a5-elim (\u00acnho (compl\u2261\u2205\u21d2ho s ieq ind)) where\n \u00acnho : \u00ac (hitsAtLeastOnce s ind)\n \u00acnho x = \u00achob (hitsAtLeastOnce\u2190\u2227\u2190\u2227 x)\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] with r where\n r = boo s ieq ind lind (\u03bb z \u2192 \u00achob (hitsAtLeastOnce\u2190\u2227\u2190\u2227 z)) (\u03bb z \u2192 \u00achoh (hitsAtLeastOnce\u2190\u2227\u2190\u2227 z)) (a\u2264\u1d62b y)\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] | a\u2264\u1d62b x\u2081 = a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 x\u2081)\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] | b\u2264\u1d62a x\u2081 = b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 x\u2081)\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] with r where\n r = boo s ieq ind lind (\u03bb z \u2192 \u00achob (hitsAtLeastOnce\u2190\u2227\u2190\u2227 z)) (\u03bb z \u2192 \u00achoh (hitsAtLeastOnce\u2190\u2227\u2190\u2227 z)) (b\u2264\u1d62a y)\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] | a\u2264\u1d62b x\u2081 = a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 x\u2081)\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | [ ieq ] | b\u2264\u1d62a x\u2081 = b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 x\u2081)\n boo (s \u2190\u2227) eq (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s\n boo (s \u2190\u2227) eq (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh ord | \u2205 | [ ieq ] = \u22a5-elim (\u00acnho (compl\u2261\u2205\u21d2ho s ieq ind)) where\n \u00acnho : \u00ac (hitsAtLeastOnce s ind)\n \u00acnho x = \u00achob (hitsAtLeastOnce\u2190\u2227\u2190\u2227 x)\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh (a\u2264\u1d62b ()) | \u00ac\u2205 x | [ ieq ]\n boo (s \u2190\u2227) refl (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh (b\u2264\u1d62a ()) | \u00ac\u2205 x | [ ieq ]\n boo (s \u2190\u2227) eq (\u2227\u2192 ind) \u2193 \u00achob \u00achoh ord = \u22a5-elim (\u00achoh hitsAtLeastOnce\u2190\u2227\u2193) \n boo (s \u2190\u2227) eq (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s\n boo (s \u2190\u2227) eq (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh ord | \u2205 | [ ieq ] = \u22a5-elim (\u00acnho (compl\u2261\u2205\u21d2ho s ieq lind)) where\n \u00acnho : \u00ac (hitsAtLeastOnce s lind)\n \u00acnho x = \u00achoh (hitsAtLeastOnce\u2190\u2227\u2190\u2227 x)\n boo (s \u2190\u2227) refl (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b ()) | \u00ac\u2205 x | [ ieq ]\n boo (s \u2190\u2227) refl (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a ()) | \u00ac\u2205 x | [ ieq ]\n boo {ll = lll \u2227 rll} (s \u2190\u2227) eq (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh ord with complL\u209b s \n boo {ll = lll \u2227 rll} (s \u2190\u2227) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh ord | \u2205 with shrink rll (fillAllLower rll) | shr-fAL-id rll\n boo {u = _} {lll \u2227 rll} (s \u2190\u2227) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (a\u2264\u1d62b x) | \u2205 | .rll | refl = a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 x)\n boo {u = _} {lll \u2227 rll} (s \u2190\u2227) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (b\u2264\u1d62a x) | \u2205 | .rll | refl = b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 x)\n boo {ll = lll \u2227 rll} (s \u2190\u2227) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh ord | \u00ac\u2205 x with shrink rll (fillAllLower rll) | shr-fAL-id rll\n boo {u = _} {lll \u2227 rll} (s \u2190\u2227) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | .rll | refl = a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 y)\n boo {u = _} {lll \u2227 rll} (s \u2190\u2227) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | .rll | refl = b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 y)\n boo (\u2227\u2192 s) eq \u2193 lind \u00achob \u00achoh ord = \u22a5-elim (\u00achob hitsAtLeastOnce\u2227\u2192\u2193)\n boo (\u2227\u2192 s) eq (\u2227\u2192 ind) \u2193 \u00achob \u00achoh ord = \u22a5-elim (\u00achoh hitsAtLeastOnce\u2227\u2192\u2193)\n boo (\u2227\u2192 s) eq (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s\n boo (\u2227\u2192 s) eq (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh ord | \u2205 | [ ieq ] = \u22a5-elim (\u00acnho (compl\u2261\u2205\u21d2ho s ieq ind)) where\n \u00acnho : \u00ac (hitsAtLeastOnce s ind)\n \u00acnho x = \u00achob (hitsAtLeastOnce\u2227\u2192\u2227\u2192 x)\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | [ ieq ] with r where\n r = boo s ieq ind lind (\u03bb z \u2192 \u00achob (hitsAtLeastOnce\u2227\u2192\u2227\u2192 z)) (\u03bb z \u2192 \u00achoh (hitsAtLeastOnce\u2227\u2192\u2227\u2192 z)) (a\u2264\u1d62b y)\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | [ ieq ] | a\u2264\u1d62b x\u2081 = a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 x\u2081)\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | [ ieq ] | b\u2264\u1d62a x\u2081 = b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 x\u2081)\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | [ ieq ] with r where\n r = boo s ieq ind lind (\u03bb z \u2192 \u00achob (hitsAtLeastOnce\u2227\u2192\u2227\u2192 z)) (\u03bb z \u2192 \u00achoh (hitsAtLeastOnce\u2227\u2192\u2227\u2192 z)) (b\u2264\u1d62a y)\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | [ ieq ] | a\u2264\u1d62b x\u2081 = a\u2264\u1d62b (\u2264\u1d62\u2227\u2192 x\u2081)\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (\u2227\u2192 lind) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 y)) | \u00ac\u2205 x | [ ieq ] | b\u2264\u1d62a x\u2081 = b\u2264\u1d62a (\u2264\u1d62\u2227\u2192 x\u2081)\n boo (\u2227\u2192 s) eq (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s\n boo (\u2227\u2192 s) eq (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh ord | \u2205 | [ ieq ] = \u22a5-elim (\u00acnho (compl\u2261\u2205\u21d2ho s ieq ind)) where\n \u00acnho : \u00ac (hitsAtLeastOnce s ind)\n \u00acnho x = \u00achob (hitsAtLeastOnce\u2227\u2192\u2227\u2192 x)\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b ()) | \u00ac\u2205 x | [ ieq ]\n boo (\u2227\u2192 s) refl (\u2227\u2192 ind) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a ()) | \u00ac\u2205 x | [ ieq ]\n boo (\u2227\u2192 s) eq (ind \u2190\u2227) \u2193 \u00achob \u00achoh ord = \u22a5-elim (\u00achoh hitsAtLeastOnce\u2227\u2192\u2193) \n boo (\u2227\u2192 s) eq (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s\n boo (\u2227\u2192 s) eq (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh ord | \u2205 | [ ieq ] = \u22a5-elim (\u00acnho (compl\u2261\u2205\u21d2ho s ieq lind)) where\n \u00acnho : \u00ac (hitsAtLeastOnce s lind)\n \u00acnho x = \u00achoh (hitsAtLeastOnce\u2227\u2192\u2227\u2192 x)\n boo (\u2227\u2192 s) refl (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh (a\u2264\u1d62b ()) | \u00ac\u2205 x | [ ieq ]\n boo (\u2227\u2192 s) refl (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh (b\u2264\u1d62a ()) | \u00ac\u2205 x | [ ieq ]\n boo {ll = lll \u2227 rll} (\u2227\u2192 s) eq (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord with complL\u209b s \n boo {ll = lll \u2227 rll} (\u2227\u2192 s) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord | \u2205 with shrink lll (fillAllLower lll) | shr-fAL-id lll\n boo {u = _} {lll \u2227 rll} (\u2227\u2192 s) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b x) | \u2205 | .lll | refl = a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 x)\n boo {u = _} {lll \u2227 rll} (\u2227\u2192 s) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a x) | \u2205 | .lll | refl = b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 x)\n boo {ll = lll \u2227 rll} (\u2227\u2192 s) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord | \u00ac\u2205 x with shrink lll (fillAllLower lll) | shr-fAL-id lll\n boo {u = _} {lll \u2227 rll} (\u2227\u2192 s) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | .lll | refl = a\u2264\u1d62b (\u2264\u1d62\u2190\u2227 y)\n boo {u = _} {lll \u2227 rll} (\u2227\u2192 s) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh (b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 y)) | \u00ac\u2205 x | .lll | refl = b\u2264\u1d62a (\u2264\u1d62\u2190\u2227 y)\n\n\n\n\n boo (s \u2190\u2227\u2192 s\u2081) eq \u2193 lind \u00achob \u00achoh ord = \u22a5-elim (\u00achob hitsAtLeastOnce\u2190\u2227\u2192\u2193)\n boo (s \u2190\u2227\u2192 s\u2081) eq (ind \u2190\u2227) \u2193 \u00achob \u00achoh ord = \u22a5-elim (\u00achoh hitsAtLeastOnce\u2190\u2227\u2192\u2193)\n boo (s \u2190\u2227\u2192 s\u2081) eq (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord with complL\u209b s | inspect complL\u209b s | complL\u209b s\u2081\n boo (s \u2190\u2227\u2192 s\u2081) eq (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord | \u2205 | [ ieq ] | e = \u22a5-elim (\u00acnho (compl\u2261\u2205\u21d2ho s ieq ind)) where\n \u00acnho : \u00ac (hitsAtLeastOnce s ind)\n \u00acnho x = \u00achob (hitsAtLeastOnce\u2190\u2227\u2192\u2190\u2227 x)\n boo (s \u2190\u2227\u2192 s\u2081) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord | \u00ac\u2205 x | [ ieq ] | \u2205 with boo s ieq ind lind {!!} {!!} ord\n ... | g = {!!}\n boo (s \u2190\u2227\u2192 s\u2081) refl (ind \u2190\u2227) (lind \u2190\u2227) \u00achob \u00achoh ord | \u00ac\u2205 x | [ ieq ] | \u00ac\u2205 x\u2081 = {!!}\n boo (s \u2190\u2227\u2192 s\u2081) eq (ind \u2190\u2227) (\u2227\u2192 lind) \u00achob \u00achoh ord = {!!}\n boo (s \u2190\u2227\u2192 s\u2081) eq (\u2227\u2192 ind) lind \u00achob \u00achoh ord = {!!}\n\n boo (s \u2190\u2228) eq ind lind \u00achob \u00achoh ord = {!!}\n boo (\u2228\u2192 s) eq ind lind \u00achob \u00achoh ord = {!!}\n boo (s \u2190\u2228\u2192 s\u2081) eq ind lind \u00achob \u00achoh ord = {!!}\n boo (s \u2190\u2202) eq ind lind \u00achob \u00achoh ord = {!!}\n boo (\u2202\u2192 s) eq ind lind \u00achob \u00achoh ord = {!!}\n boo (s \u2190\u2202\u2192 s\u2081) eq ind lind \u00achob \u00achoh ord = {!!}\n\n\n private\n data M\u00acho {i u ll n dt df} (s : SetLL ll) : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll \u2192 Set where\n \u2205 : M\u00acho s \u2205\n \u00ac\u2205 : {ind : IndexLL (\u03c4 {i} {u} {n} {dt} df) ll} \u2192 (\u00acho : \u00ac (hitsAtLeastOnce s ind))\n \u2192 M\u00acho s (\u00ac\u2205 ind)\n\n hf : \u2200{i u n dt df} \u2192 \u2200 ll \u2192 \u2200{cs} \u2192 (ind : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll)\n \u2192 (s : SetLL ll) \u2192 (ceq : complL\u209b s \u2261 \u00ac\u2205 cs) \u2192 (m\u00acho : M\u00acho s ind) \u2192 LinLogic i {u}\n \u2192 LinLogic i {u}\n hf ll {cs} \u2205 s ceq mnho cll = shrinkcms ll s cs ceq\n hf ll {cs} (\u00ac\u2205 x) s ceq (\u00ac\u2205 \u00acho) cll = replLL (shrinkcms ll s cs ceq) (\u00acho-shr-morph s ceq x \u00acho) cll\n\n hf2 : \u2200{i u ll rll n dt df ts} \u2192 (ind : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll)\n \u2192 (s : SetLL ll) \u2192 (m\u00acho : M\u00acho s ind) \u2192 (lf : LFun {i} {u} ll rll)\n \u2192 \u00ac\u2205 ts \u2261 tranLFMSetLL lf (\u00ac\u2205 s)\n \u2192 M\u00acho ts (tranLFMIndexLL lf ind)\n hf2 \u2205 mnho s lf eqs = \u2205\n hf2 (\u00ac\u2205 x) s mnho lf eqs with tranLFMIndexLL lf (\u00ac\u2205 x) | inspect (\u03bb z \u2192 tranLFMIndexLL lf (\u00ac\u2205 z)) x\n hf2 (\u00ac\u2205 x) s mnho lf eqs | \u2205 | [ eq ] = \u2205\n hf2 (\u00ac\u2205 x) s (\u00ac\u2205 \u00acho) lf eqs | \u00ac\u2205 _ | [ eq ] = \u00ac\u2205 (tranLF-preserves-\u00acho lf x s \u00acho (sym eq) eqs)\n\n\n data MLFun {i u ll rll n dt df} (mind : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll)\n (s : SetLL ll) (m\u00acho : M\u00acho s mind) (lf : LFun {i} {u} ll rll) : Set (lsuc u) where\n \u2205 : \u2200{ts} \u2192 (\u2205 \u2261 mind) \u2192 (complL\u209b s \u2261 \u2205)\n \u2192 (eqs : \u00ac\u2205 ts \u2261 tranLFMSetLL lf (\u00ac\u2205 s)) \u2192 (ceqo : complL\u209b ts \u2261 \u2205) \u2192 MLFun mind s m\u00acho lf\n \u00ac\u2205\u2205 : \u2200 {ind cs} \u2192 (\u00ac\u2205 ind \u2261 mind) \u2192 (ceqi : complL\u209b s \u2261 \u00ac\u2205 cs)\n \u2192 (eqs : \u2205 \u2261 tranLFMSetLL lf (\u00ac\u2205 s))\n \u2192 (cll : LinLogic i {u}) \u2192 LFun (hf ll mind s ceqi m\u00acho cll) rll\n \u2192 MLFun mind s m\u00acho lf \n \u00ac\u2205\u00ac\u2205 : \u2200 {cs ts cts} \u2192 (ceqi : complL\u209b s \u2261 \u00ac\u2205 cs)\n \u2192 let tind = tranLFMIndexLL lf mind in\n (eqs : \u00ac\u2205 ts \u2261 tranLFMSetLL lf (\u00ac\u2205 s)) \u2192 (ceqo : complL\u209b ts \u2261 \u00ac\u2205 cts)\n \u2192 ((cll : LinLogic i {u}) \u2192 LFun (hf ll mind s ceqi m\u00acho cll) (hf rll tind ts ceqo (hf2 mind s m\u00acho lf eqs) cll))\n \u2192 MLFun mind s m\u00acho lf \n --??? We will never reach to a point where complL\u209b ts \u2261 \u2205 because\n -- the input would have the same fate. ( s becomes smaller as we go forward, thus complL\u209b increases. Here we take the case where s is not \u2205.\n -- Correction : In fact , the ordinal remains the same since all the points of the set need to to end at the same com by design. (but we might not be able to prove this now and thus need to go with the weaker argument.)\n \n\n\n\n-- s is special, all of the input points eventually will be consumed by a single com + the point from the index. Thus if complL\u209b s \u2261 \u2205, this means that lf does not contain coms or calls.\n-- Maybe one day, we will provide a datatype that contains that information, for the time being, we use IMPOSSIBLE where necessary.\n\n -- s here does contain the ind.\n -- TODO IMPORTANT After test , we need to remove \u2282 that contain I in the first place.\n test : \u2200{i u rll ll n dt df} \u2192 (ind : MIndexLL (\u03c4 {i} {u} {n} {dt} df) ll) \u2192 (s : SetLL ll)\n \u2192 \u2200 m\u00acho \u2192 (lf : LFun ll rll) \u2192 MLFun ind s m\u00acho lf\n test ind s mnho lf with complL\u209b s | inspect complL\u209b s\n test \u2205 s \u2205 lf | \u2205 | [ e ] with tranLFMSetLL lf (\u00ac\u2205 s) | inspect (\u03bb z \u2192 tranLFMSetLL lf (\u00ac\u2205 z)) s\n test \u2205 s \u2205 lf | \u2205 | [ e ] | \u2205 | [ r ] = IMPOSSIBLE\n test \u2205 s \u2205 lf | \u2205 | [ e ] | \u00ac\u2205 x | [ r ] with complL\u209b x | inspect complL\u209b x\n test \u2205 s \u2205 lf | \u2205 | [ e ] | \u00ac\u2205 x | [ r ] | \u2205 | [ t ] = \u2205 refl e (sym r) t\n test \u2205 s \u2205 lf | \u2205 | [ e ] | \u00ac\u2205 x | [ r ] | \u00ac\u2205 x\u2081 | t = IMPOSSIBLE\n test (\u00ac\u2205 x) s (\u00ac\u2205 \u00acho) lf | \u2205 | [ e ] = \u22a5-elim (\u00acho (compl\u2261\u2205\u21d2ho s e x)) \n\n test \u2205 s mnho I | \u00ac\u2205 x | [ eq ] = \u00ac\u2205\u00ac\u2205 eq refl eq (\u03bb cll \u2192 I)\n test (\u00ac\u2205 x\u2081) s (\u00ac\u2205 \u00acho) I | \u00ac\u2205 x | [ eq ] = \u00ac\u2205\u00ac\u2205 eq refl eq (\u03bb cll \u2192 I)\n\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] with truncSetLL s lind | inspect (truncSetLL s) lind\n ... | \u2205 | [ teq ] with (mreplacePartOf (\u00ac\u2205 s) to (\u2205 {ll = ell}) at lind) | inspect (\u03bb z \u2192 mreplacePartOf (\u00ac\u2205 s) to (\u2205 {ll = ell}) at z) lind\n ... | \u2205 | [ meq ] = \u22a5-elim ((trunc\u2261\u2205\u21d2\u00acmrpls\u2261\u2205 s lind teq) meq)\n ... | \u00ac\u2205 mx | [ meq ] with test {n = n} {dt} {df} \u2205 mx \u2205 lf\u2081\n ... | \u2205 indeq ceq eqs ceqo = \u22a5-elim ((del\u21d2\u00acho s lind (sym meq)) (compl\u2261\u2205\u21d2ho mx ceq (a\u2264\u1d62b-morph lind lind ell (\u2264\u1d62-reflexive lind))))\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u2205 | [ teq ] | \u00ac\u2205 mx | [ meq ] | \u00ac\u2205\u2205 () ceqi eqs cll t\n ... | \u00ac\u2205\u00ac\u2205 {ts = ts} {cts} ceqi eqs ceqo t = \u00ac\u2205\u00ac\u2205 eq tseq ceqo ((\u03bb z \u2192 _\u2282_ {ind = \u00acho-shr-morph s eq lind (trunc\u2261\u2205\u21d2\u00acho s lind teq)} lf (g (t z)))) where\n tseq = sym (trans (cong (\u03bb z \u2192 tranLFMSetLL lf\u2081 z) (trans (cong (\u03bb z \u2192 mreplacePartOf \u00ac\u2205 s to tranLFMSetLL lf z at lind) teq) meq)) (sym eqs))\n g = subst (\u03bb a \u2192 LFun a (shrink rll cts)) (shrink-repl-comm s lind eq teq meq ceqi)\n test {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] with test {n = n} {dt} {df} \u2205 trs \u2205 lf\n ... | \u2205 indeq ceq eqs ceqo with (mreplacePartOf (\u00ac\u2205 s) to (tranLFMSetLL lf (\u00ac\u2205 trs)) at lind) | inspect (\u03bb z \u2192 mreplacePartOf (\u00ac\u2205 s) to (tranLFMSetLL lf (\u00ac\u2205 trs)) at z) lind\n ... | \u00ac\u2205 mx | r with test {n = n} {dt} {df} \u2205 mx \u2205 lf\u2081\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq eqs ceqo | \u00ac\u2205 mx | [ meq ] | \u2205 indeq1 ceq1 eqs1 ceqo1 with tranLFMSetLL lf (\u00ac\u2205 trs)\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq eqs ceqo | \u00ac\u2205 mx | [ meq ] | \u2205 indeq1 ceq1 eqs1 ceqo1 | \u2205 = IMPOSSIBLE -- IMPOSSIBLE and not provable with the current information we have.\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq eqs ceqo | \u00ac\u2205 mx | [ meq ] | \u2205 indeq1 ceq1 eqs1 ceqo1 | \u00ac\u2205 x with compl\u2261\u00ac\u2205\u21d2replace-compl\u2261\u00ac\u2205 s lind eq teq ceq x\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq eqs ceqo | \u00ac\u2205 .(replacePartOf s to x at ind) | [ refl ] | \u2205 indeq1 ceq1 eqs1 ceqo1 | \u00ac\u2205 x | proj\u2083 , proj\u2084 with complL\u209b (replacePartOf s to x at ind)\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq eqs ceqo | \u00ac\u2205 .(replacePartOf s to x at ind) | [ refl ] | \u2205 indeq1 refl eqs1 ceqo1 | \u00ac\u2205 x | proj\u2083 , () | .\u2205\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq eqs ceqo | \u00ac\u2205 mx | [ meq ] | \u00ac\u2205\u2205 () ceqi1 eqs1 cll1 t1\n test {rll = rll} {ll = ll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 {ts = ts} indeq ceq eqs ceqo | \u00ac\u2205 mx | [ meq ] | \u00ac\u2205\u00ac\u2205 {cs} ceqi1 eqs1 ceqo1 t1\n = \u00ac\u2205\u00ac\u2205 eq tseq ceqo1 ((\u03bb cll \u2192 subst (\u03bb z \u2192 LFun z (shrink rll _)) (shrink-repl\u2261\u2205 s lind eq teq ceq _ ceqo t) (t1 cll))) where\n tseq = sym (trans (cong (\u03bb z \u2192 tranLFMSetLL lf\u2081 z) (trans (cong (\u03bb z \u2192 mreplacePartOf \u00ac\u2205 s to tranLFMSetLL lf z at lind) teq) meq)) (sym eqs1))\n t : complL\u209b (replacePartOf s to ts at lind) \u2261 \u00ac\u2205 cs\n t with trans (cong (\u03bb z \u2192 mreplacePartOf \u00ac\u2205 s to z at lind) eqs) meq\n ... | g with replacePartOf s to ts at lind\n t | refl | e = ceqi1\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq eqs ceqo | \u2205 | [ meq ] with tranLFMSetLL lf (\u00ac\u2205 trs)\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 indeq ceq refl ceqo | \u2205 | [ () ] | .(\u00ac\u2205 _)\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind\u2081} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u2205 () ceqi eqs cll t\n test {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t\n with (mreplacePartOf (\u00ac\u2205 s) to (tranLFMSetLL lf (\u00ac\u2205 trs)) at lind) | inspect (\u03bb z \u2192 mreplacePartOf (\u00ac\u2205 s) to (tranLFMSetLL lf (\u00ac\u2205 trs)) at z) lind\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t | \u2205 | [ meq ] with tranLFMSetLL lf (\u00ac\u2205 trs)\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi () ceqo t | \u2205 | [ meq ] | \u2205\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t | \u2205 | [ () ] | \u00ac\u2205 _\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t | \u00ac\u2205 mx | [ meq ] with test {n = n} {dt} {df} \u2205 mx \u2205 lf\u2081\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t | \u00ac\u2205 mx | [ meq ] | \u2205 indeq ceq1 eqs1 ceqo1 with tranLFMSetLL lf (\u00ac\u2205 trs)\n test {rll = rll} {ll = ll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 {ts = ts} ceqi refl ceqo t | \u00ac\u2205 .(replacePartOf s to _ at lind) | [ refl ] | \u2205 indeq1 ceq1 eqs1 ceqo1 | .(\u00ac\u2205 _) with complL\u209b ts | ct where\n m = replacePartOf s to ts at lind\n mind = subst (\u03bb z \u2192 IndexLL z (replLL ll lind ell)) (replLL-\u2193 lind) ((a\u2264\u1d62b-morph lind lind ell (\u2264\u1d62-reflexive lind)))\n ct = compl\u2261\u2205\u21d2compltr\u2261\u2205 m ceq1 mind (sym (tr-repl\u21d2id s lind ts))\n test {rll = rll} {ll} {n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 {ts = ts} ceqi refl () t | \u00ac\u2205 .(replacePartOf s to ts at ind) | [ refl ] | \u2205 indeq ceq1 eqs1 ceqo1 | .(\u00ac\u2205 ts) | .\u2205 | refl\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t | \u00ac\u2205 mx | [ meq ] | \u00ac\u2205\u2205 () ceqi\u2081 eqs\u2081 cll1 x\u2081\n test {rll = rll} {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t | \u00ac\u2205 mx | [ meq ] | \u00ac\u2205\u00ac\u2205 ceqi1 eqs1 ceqo1 t1\n = \u00ac\u2205\u00ac\u2205 eq tseq ceqo1 (\u03bb cll \u2192 _\u2282_ {ind = hind} (t cll) (subst (\u03bb z \u2192 LFun z _) (sym (ho-shrink-repl-comm s lind eq teq ceqi ceqo w ceqi1)) (t1 cll))) where\n w = trans (cong (\u03bb z \u2192 mreplacePartOf \u00ac\u2205 s to z at lind) eqs) meq\n tseq = sym (trans (cong (\u03bb z \u2192 tranLFMSetLL lf\u2081 z) (trans (cong (\u03bb z \u2192 mreplacePartOf \u00ac\u2205 s to tranLFMSetLL lf z at lind) teq) meq)) (sym eqs1))\n hind = ho-shr-morph s eq lind (sym teq) ceqi\n test {rll = rll} {n = n} {dt} {df} (\u00ac\u2205 ind) s (\u00ac\u2205 \u00acho) (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] with truncSetLL s lind | inspect (truncSetLL s) lind\n ... | \u2205 | [ teq ] with (mreplacePartOf (\u00ac\u2205 s) to (\u2205 {ll = ell}) at lind) | inspect (\u03bb z \u2192 mreplacePartOf (\u00ac\u2205 s) to (\u2205 {ll = ell}) at z) lind\n ... | \u2205 | [ meq ] = \u22a5-elim ((trunc\u2261\u2205\u21d2\u00acmrpls\u2261\u2205 s lind teq) meq)\n ... | \u00ac\u2205 mx | [ meq ] with isLTi lind ind\n ... | no \u00acp with test {n = n} {dt} {df} nind mx (\u00ac\u2205 n\u00acho) lf\u2081 where\n nord = ind\u03c4&\u00acge\u21d2\u00acOrd ind lind \u00acp\n nind = \u00ac\u2205 (\u00acord-morph ind lind ell (flipNotOrd\u1d62 nord))\n n\u00acho = \u00acord&\u00acho-del\u21d2\u00acho ind s \u00acho lind nord (sym meq)\n ... | \u2205 () ceq eqs ceqo\n test {rll = rll} {n = n} {dt} {df} (\u00ac\u2205 ind) s (\u00ac\u2205 \u00acho) (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u2205 | [ teq ] | \u00ac\u2205 mx | [ meq ] | no \u00acp | \u00ac\u2205\u2205 {ind = tind} indeq ceqi eqs cll t\n = \u00ac\u2205\u2205 refl eq tseq cll (_\u2282_ {ind = sind} lf {!best!}) where \n nord = ind\u03c4&\u00acge\u21d2\u00acOrd ind lind \u00acp\n tseq = sym (trans (cong (\u03bb z \u2192 tranLFMSetLL lf\u2081 z) (trans (cong (\u03bb z \u2192 mreplacePartOf \u00ac\u2205 s to tranLFMSetLL lf z at lind) teq) meq)) (sym eqs))\n hind = \u00acho-shr-morph s eq lind ((trunc\u2261\u2205\u21d2\u00acho s lind teq))\n bind = \u00acho-shr-morph s eq ind \u00acho\n sind = \u00acord-morph hind bind cll {!!}\n best = replLL-\u00acordab\u2261ba bind cll hind ell {!!}\n\n find = \u00acord-morph ind lind ell (flipNotOrd\u1d62 nord)\n nind = \u00acord-morph lind ind cll nord\n test {rll = rll} {n = n} {dt} {df} (\u00ac\u2205 ind) s (\u00ac\u2205 \u00acho) (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u2205 | [ teq ] | \u00ac\u2205 mx | [ meq ] | no \u00acp | \u00ac\u2205\u00ac\u2205 ceqi eqs ceqo t = {!!}\n test {rll = rll} {n = n} {dt} {df} (\u00ac\u2205 ind) s (\u00ac\u2205 \u00acho) (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u2205 | [ teq ] | \u00ac\u2205 mx | [ meq ] | yes p = {!!}\n test (\u00ac\u2205 ind\u2081) s (\u00ac\u2205 \u00acho) (_\u2282_ {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] = {!!}\n test ind s mnho (tr ltr lf) | \u00ac\u2205 x | eq = {!!}\n test ind s mnho (com df\u2081 lf) | \u00ac\u2205 x | eq = {!!}\n test ind s mnho (call x\u2081) | \u00ac\u2205 x | eq = {!!}\n\n\n\n\n\n-- test {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] with tranLFMSetLL lf (\u00ac\u2205 trs) | inspect (\u03bb z \u2192 tranLFMSetLL lf (\u00ac\u2205 z)) trs | test {n = n} {dt} {df} \u2205 trs \u2205 lf\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 | [ tseq ] | I x eqs ceqo with tranLFMSetLL lf (\u00ac\u2205 trs)\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 | [ refl ] | I x () ceqo | .\u2205\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 | tseq | \u00acI\u2205 ceqi eqs x = {!!}\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 | [ tseq ] | \u00acI\u00ac\u2205 ceqi eqs ceqo x with tranLFMSetLL lf (\u00ac\u2205 trs)\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u2205 | [ refl ] | \u00acI\u00ac\u2205 ceqi () ceqo x | .(\u00ac\u2205 _)\n--\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205 ts | tseq | I x eqs ceqo = {!!}\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205 ts | tseq | \u00acI\u2205 ceqi eqs x = {!!}\n-- test \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = ind} lf lf\u2081) | \u00ac\u2205 x\u2081 | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00ac\u2205 ts | tseq | \u00acI\u00ac\u2205 ceqi eqs ceqo x = {!!}\n--\n--\n---- with test {n = n} {dt} {df} \u2205 trs \u2205 lf\n---- ... | I ceq with mreplacePartOf (\u00ac\u2205 s) to tlf at lind | inspect (\u03bb z \u2192 mreplacePartOf (\u00ac\u2205 s) to tlf at z) lind where -- tranLFMSetLL lf\u2081 nmx | inspect (tranLFMSetLL lf\u2081) nmx | test \u2205 {!nmx!} \u2205 lf\u2081 where\n---- tlf = tranLFMSetLL lf (\u00ac\u2205 trs)\n---- -- is = test \u2205 {!!} \u2205 lf\u2081\n---- ... | \u2205 | [ meq ] = IMPOSSIBLE -- Since we have I, lf cannot contain com or call, thus tlf is not \u2205.\n---- ... | \u00ac\u2205 mx | [ meq ] with test {n = n} {dt} {df} \u2205 mx \u2205 lf\u2081\n---- ... | I ceq1 = IMPOSSIBLE -- TODO This is impossible because eq assures us that complL\u209b s \u2261 \u00ac\u2205 x but complL\u209b of both ceq and ceq1 are \u2205.\n---- ... | \u00acI\u2205 ceqi1 eqs1 t1 = \u00acI\u2205 eq tseq {!!} where\n---- meeq = subst (\u03bb z \u2192 (mreplacePartOf \u00ac\u2205 s to tranLFMSetLL lf z at lind) \u2261 \u00ac\u2205 mx) (sym teq) meq\n---- tseq = subst (\u03bb z \u2192 \u2205 \u2261 tranLFMSetLL lf\u2081 z) (sym meeq) eqs1\n---- ... | \u00acI\u00ac\u2205 ceqi1 eqs1 ceqo1 t1 = {!!} \n---- test {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00acI\u2205 ceqi eqs q = {!!}\n---- test {n = n} {dt} {df} \u2205 s \u2205 (_\u2282_ {ell = ell} {ind = lind} lf lf\u2081) | \u00ac\u2205 x | [ eq ] | \u00ac\u2205 trs | [ teq ] | \u00acI\u00ac\u2205 ceqi eqs ceqo q = {!!} where\n-- test \u2205 s \u2205 (tr ltr lf) | \u00ac\u2205 x | [ eq ] = {!!}\n-- test \u2205 s \u2205 (com df\u2081 lf) | \u00ac\u2205 x | [ eq ] = IMPOSSIBLE -- Since ind is \u2205, this is impossible.\n-- test \u2205 s \u2205 (call x\u2081) | \u00ac\u2205 x | [ eq ] = IMPOSSIBLE\n-- test (\u00ac\u2205 x) s (\u00ac\u2205 \u00acho) lf = {!!}\n-- \n-- \n \n \n \n \n \n","returncode":0,"stderr":"","license":"mpl-2.0","lang":"Agda"} {"commit":"fb73c4c68d0d9e00dac3fd35d9b93d3d01182522","subject":"Use HOAS-enabled helpers for nested abstractions.","message":"Use HOAS-enabled helpers for nested abstractions.\n\nOld-commit-hash: cffcfea69d7b1877dc5d63b49ee0ed9f24dce0d2\n","repos":"inc-lc\/ilc-agda","old_file":"Popl14\/Change\/Term.agda","new_file":"Popl14\/Change\/Term.agda","new_contents":"module Popl14.Change.Term where\n\n-- Terms Calculus Popl14\n--\n-- Contents\n-- - Term constructors\n-- - Weakening on terms\n-- - `fit`: weaken a term to its \u0394Context\n-- - diff-term, apply-term and their syntactic sugars\n\nopen import Syntax.Context.Popl14 public\nopen import Data.Integer\n\nopen import Popl14.Syntax.Term public\n\ndiff-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\napply-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n-- Sugars for diff-term and apply-term\ninfixl 6 _\u2295_ _\u229d_\n_\u2295_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4\n_\u229d_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394Type \u03c4)\nt \u2295 \u0394t = app (app apply-term \u0394t) t\ns \u229d t = app (app diff-term s) t\n\napply-term {int} =\n abs\u2082 (\u03bb \u0394x x \u2192 add x \u0394x)\napply-term {bag} =\n abs\u2082 (\u03bb \u0394x x \u2192 union x \u0394x)\napply-term {\u03c3 \u21d2 \u03c4} =\n let\n \u0394f = var (that (that this))\n f = var (that this)\n x = var this\n in\n -- \u0394f f x\n abs (abs (abs\n (app f x \u2295 app (app \u0394f x) (x \u229d x))))\n\ndiff-term {int} =\n abs\u2082 (\u03bb x y \u2192 add x (minus y))\ndiff-term {bag} =\n abs\u2082 (\u03bb x y \u2192 union x (negate y))\ndiff-term {\u03c3 \u21d2 \u03c4} =\n let\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n in\n -- g f x \u0394x\n abs (abs (abs (abs\n (app g (x \u2295 \u0394x) \u229d app f x))))\n","old_contents":"module Popl14.Change.Term where\n\n-- Terms Calculus Popl14\n--\n-- Contents\n-- - Term constructors\n-- - Weakening on terms\n-- - `fit`: weaken a term to its \u0394Context\n-- - diff-term, apply-term and their syntactic sugars\n\nopen import Syntax.Context.Popl14 public\nopen import Data.Integer\n\nopen import Popl14.Syntax.Term public\n\ndiff-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\napply-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n-- Sugars for diff-term and apply-term\ninfixl 6 _\u2295_ _\u229d_\n_\u2295_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4\n_\u229d_ : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192 Term \u0393 (\u0394Type \u03c4)\nt \u2295 \u0394t = app (app apply-term \u0394t) t\ns \u229d t = app (app diff-term s) t\n\napply-term {int} =\n let \u0394x = var (that this)\n x = var this\n in abs (abs (add x \u0394x))\napply-term {bag} =\n let \u0394x = var (that this)\n x = var this\n in abs (abs (union x \u0394x))\napply-term {\u03c3 \u21d2 \u03c4} =\n let\n \u0394f = var (that (that this))\n f = var (that this)\n x = var this\n in\n -- \u0394f f x\n abs (abs (abs\n (app f x \u2295 app (app \u0394f x) (x \u229d x))))\n\ndiff-term {int} =\n let x = var (that this)\n y = var this\n in abs (abs (add x (minus y)))\ndiff-term {bag} =\n let x = var (that this)\n y = var this\n in abs (abs (union x (negate y)))\ndiff-term {\u03c3 \u21d2 \u03c4} =\n let\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n in\n -- g f x \u0394x\n abs (abs (abs (abs\n (app g (x \u2295 \u0394x) \u229d app f x))))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f1f7df2dcdfa464f6aa33d7715b3bde8b94e46f9","subject":"Also curry index arguments of eliminators.","message":"Also curry index arguments of eliminators.\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/Examples\/DarkwingDuck.agda","new_file":"formalization\/agda\/Spire\/Examples\/DarkwingDuck.agda","new_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nopen import Function\nmodule Spire.Examples.DarkwingDuck where\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nBranches : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nBranches [] P = \u22a4\nBranches (l \u2237 E) P = P here \u00d7 Branches E (\u03bb t \u2192 P (there t))\n\ncase : {E : Enum} (P : Tag E \u2192 Set) (cs : Branches E P) (t : Tag E) \u2192 P t\ncase P (c , cs) here = c\ncase P (c , cs) (there t) = case (\u03bb t \u2192 P (there t)) cs t\n\n----------------------------------------------------------------------\n\ndata Tel : Set where\n End : Tel\n Arg : (A : Set) (B : A \u2192 Tel) \u2192 Tel\n\nEl\u1d40 : Tel \u2192 Set\nEl\u1d40 End = \u22a4\nEl\u1d40 (Arg A B) = \u03a3 A (\u03bb a \u2192 El\u1d40 (B a))\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set where\n End : (i : I) \u2192 Desc I\n Rec : (i : I) (D : Desc I) \u2192 Desc I\n Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n\n----------------------------------------------------------------------\n\nISet : Set \u2192 Set\nISet I = I \u2192 Set\n\nEl\u1d30 : {I : Set} (D : Desc I) \u2192 ISet I \u2192 ISet I\nEl\u1d30 (End j) X i = j \u2261 i\nEl\u1d30 (Rec j D) X i = X j \u00d7 El\u1d30 D X i\nEl\u1d30 (Arg A B) X i = \u03a3 A (\u03bb a \u2192 El\u1d30 (B a) X i)\n\nHyps : {I : Set} (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El\u1d30 D X i) \u2192 Set\nHyps (End j) X P i q = \u22a4\nHyps (Rec j D) X P i (x , xs) = P j x \u00d7 Hyps D X P i xs\nHyps (Arg A B) X P i (a , b) = Hyps (B a) X P i b\n\n----------------------------------------------------------------------\n\ndata \u03bc {I : Set} (D : Desc I) (i : I) : Set where\n init : El\u1d30 D (\u03bc D) i \u2192 \u03bc D i\n\n----------------------------------------------------------------------\n\nind : {I : Set} (D : Desc I)\n (M : (i : I) \u2192 \u03bc D i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 D (\u03bc D) i) (ihs : Hyps D (\u03bc D) M i xs) \u2192 M i (init xs))\n (i : I)\n (x : \u03bc D i)\n \u2192 M i x\n\nprove : {I : Set} (D E : Desc I)\n (M : (i : I) \u2192 \u03bc E i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 E (\u03bc E) i) (ihs : Hyps E (\u03bc E) M i xs) \u2192 M i (init xs))\n (i : I) (xs : El\u1d30 D (\u03bc E) i) \u2192 Hyps D (\u03bc E) M i xs\n\nind D M \u03b1 i (init xs) = \u03b1 i xs (prove D D M \u03b1 i xs)\n\nprove (End j) E M \u03b1 i q = tt\nprove (Rec j D) E M \u03b1 i (x , xs) = ind E M \u03b1 j x , prove D E M \u03b1 i xs\nprove (Arg A B) E M \u03b1 i (a , xs) = prove (B a) E M \u03b1 i xs\n\n----------------------------------------------------------------------\n\nrecord Data : Set where\n field\n P : Tel\n I : El\u1d40 P \u2192 Tel\n E : Enum\n B : (A : El\u1d40 P) \u2192 Branches E (\u03bb _ \u2192 Desc (El\u1d40 (I A)))\n\n C : (A : El\u1d40 P) \u2192 Tag E \u2192 Desc (El\u1d40 (I A))\n C A = case (\u03bb _ \u2192 Desc (El\u1d40 (I A))) (B A)\n\n D : (A : El\u1d40 P) \u2192 Desc (El\u1d40 (I A))\n D A = Arg (Tag E) (C A)\n\n----------------------------------------------------------------------\n\nUncurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedBranches E P X = Branches E P \u2192 X\n\nCurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedBranches [] P X = X\nCurriedBranches (l \u2237 E) P X = P here \u2192 CurriedBranches E (\u03bb t \u2192 P (there t)) X\n\ncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 UncurriedBranches E P X \u2192 CurriedBranches E P X\ncurryBranches {[]} f = f tt\ncurryBranches {l \u2237 E} f = \u03bb c \u2192 curryBranches (\u03bb cs \u2192 f (c , cs))\n\nuncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 CurriedBranches E P X \u2192 UncurriedBranches E P X\nuncurryBranches {[]} x tt = x\nuncurryBranches {l \u2237 E} f (c , cs) = uncurryBranches (f c) cs\n\n----------------------------------------------------------------------\n\nUncurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nUncurriedEl\u1d40 T X = (xs : El\u1d40 T) \u2192 X xs\n\nCurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nCurriedEl\u1d40 End X = X tt\nCurriedEl\u1d40 (Arg A B) X = (a : A) \u2192 CurriedEl\u1d40 (B a) (\u03bb b \u2192 X (a , b))\n\ncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 UncurriedEl\u1d40 T X \u2192 CurriedEl\u1d40 T X\ncurryEl\u1d40 End X f = f tt\ncurryEl\u1d40 (Arg A B) X f = \u03bb a \u2192 curryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (\u03bb b \u2192 f (a , b))\n\nuncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 CurriedEl\u1d40 T X \u2192 UncurriedEl\u1d40 T X\nuncurryEl\u1d40 End X x tt = x\nuncurryEl\u1d40 (Arg A B) X f (a , b) = uncurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (f a) b\n\nICurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nICurriedEl\u1d40 End X = X tt\nICurriedEl\u1d40 (Arg A B) X = {a : A} \u2192 ICurriedEl\u1d40 (B a) (\u03bb b \u2192 X (a , b))\n\nicurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 UncurriedEl\u1d40 T X \u2192 ICurriedEl\u1d40 T X\nicurryEl\u1d40 End X f = f tt\nicurryEl\u1d40 (Arg A B) X f = \u03bb {a} \u2192 icurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (\u03bb b \u2192 f (a , b))\n\niuncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 ICurriedEl\u1d40 T X \u2192 UncurriedEl\u1d40 T X\niuncurryEl\u1d40 End X x tt = x\niuncurryEl\u1d40 (Arg A B) X f (a , b) = iuncurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) f b\n\n----------------------------------------------------------------------\n\nUncurriedEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl\u1d30 D X = \u2200{i} \u2192 El\u1d30 D X i \u2192 X i\n\nCurriedEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl\u1d30 (End i) X = X i\nCurriedEl\u1d30 (Rec i D) X = (x : X i) \u2192 CurriedEl\u1d30 D X\nCurriedEl\u1d30 (Arg A B) X = (a : A) \u2192 CurriedEl\u1d30 (B a) X\n\ncurryEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I)\n \u2192 UncurriedEl\u1d30 D X \u2192 CurriedEl\u1d30 D X\ncurryEl\u1d30 (End i) X cn = cn refl\ncurryEl\u1d30 (Rec i D) X cn = \u03bb x \u2192 curryEl\u1d30 D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl\u1d30 (Arg A B) X cn = \u03bb a \u2192 curryEl\u1d30 (B a) X (\u03bb xs \u2192 cn (a , xs))\n\n----------------------------------------------------------------------\n\nUncurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 Set\nUncurriedHyps D X P cn =\n \u2200 i (xs : El\u1d30 D X i) (ihs : Hyps D X P i xs) \u2192 P i (cn xs)\n\nCurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 Set\nCurriedHyps (End i) X P cn =\n P i (cn refl)\nCurriedHyps (Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedHyps D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedHyps (Arg A B) X P cn =\n (a : A) \u2192 CurriedHyps (B a) X P (\u03bb xs \u2192 cn (a , xs))\n\ncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 UncurriedHyps D X P cn\n \u2192 CurriedHyps D X P cn\ncurryHyps (End i) X P cn pf =\n pf i refl tt\ncurryHyps (Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryHyps D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryHyps (Arg A B) X P cn pf =\n \u03bb a \u2192 curryHyps (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\n\nuncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 CurriedHyps D X P cn\n \u2192 UncurriedHyps D X P cn\nuncurryHyps (End .i) X P cn pf i refl tt =\n pf\nuncurryHyps (Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryHyps D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryHyps (Arg A B) X P cn pf i (a , xs) ihs =\n uncurryHyps (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\n\n----------------------------------------------------------------------\n\nFormUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 UncurriedEl\u1d40 (Data.I R p) \u03bb i\n \u2192 Set\nFormUncurried R p i = \u03bc (Data.D R p) i\n\nForm : (R : Data)\n \u2192 CurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 CurriedEl\u1d40 (Data.I R p) \u03bb i\n \u2192 Set\nForm R =\n curryEl\u1d40 (Data.P R) (\u03bb p \u2192 CurriedEl\u1d40 (Data.I R p) \u03bb i \u2192 Set) \u03bb p \u2192\n curryEl\u1d40 (Data.I R p) (\u03bb i \u2192 Set) \u03bb i \u2192\n FormUncurried R p i\n\n----------------------------------------------------------------------\n\ninjUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p\n in CurriedEl\u1d30 D (\u03bc D)\ninjUncurried R p t = curryEl\u1d30 (Data.C R p t)\n (\u03bc (Data.D R p))\n (\u03bb xs \u2192 init (t , xs))\n\ninj : (R : Data)\n \u2192 ICurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p\n in CurriedEl\u1d30 D (\u03bc D)\ninj R = icurryEl\u1d40 (Data.P R)\n (\u03bb p \u2192 let D = Data.D R p in CurriedEl\u1d30 D (\u03bc D))\n (injUncurried R)\n\n----------------------------------------------------------------------\n\nindCurried : {I : Set} (D : Desc I)\n (M : (i : I) \u2192 \u03bc D i \u2192 Set)\n (f : CurriedHyps D (\u03bc D) M init)\n (i : I)\n (x : \u03bc D i)\n \u2192 M i x\nindCurried D M f i x =\n ind D M (uncurryHyps D (\u03bc D) M init f) i x\n\nSumCurriedHyps : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 Tag (Data.E R) \u2192 Set\nSumCurriedHyps R p M t =\n let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc (Data.D R p) i \u2192 Set) M in\n CurriedHyps (Data.C R p t) (\u03bc (Data.D R p)) unM (\u03bb xs \u2192 init (t , xs))\n\nelimUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in UncurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 (x : \u03bc D i) \u2192 unM i x))\nelimUncurried R p M cs =\n let D = Data.D R p\n unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in\n curryEl\u1d40 (Data.I R p) (\u03bb i \u2192 (x : \u03bc D i) \u2192 unM i x) \u03bb i x \u2192\n indCurried (Data.D R p) unM\n (case (SumCurriedHyps R p M) cs)\n i x\n\nelim : (R : Data)\n \u2192 ICurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in CurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 (x : \u03bc D i) \u2192 unM i x))\nelim R = icurryEl\u1d40 (Data.P R)\n (\u03bb p \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in CurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 (x : \u03bc D i) \u2192 unM i x)))\n (\u03bb p M \u2192 curryBranches\n (elimUncurried R p M))\n\n----------------------------------------------------------------------\n\n\u2115E : Enum\n\u2115E = \"zero\" \u2237 \"suc\" \u2237 []\n\nVecE : Enum\nVecE = \"nil\" \u2237 \"cons\" \u2237 []\n\n\u2115T : Set\n\u2115T = Tag \u2115E\n\nVecT : Set\nVecT = Tag VecE\n\nzeroT : \u2115T\nzeroT = here\n\nsucT : \u2115T\nsucT = there here\n\nnilT : VecT\nnilT = here\n\nconsT : VecT\nconsT = there here\n\n----------------------------------------------------------------------\n\n\u2115R : Data\n\u2115R = record\n { P = End\n ; I = \u03bb _ \u2192 End\n ; E = \u2115E\n ; B = \u03bb _ \u2192\n End tt\n , Rec tt (End tt)\n , tt\n }\n\n\u2115 : Set\n\u2115 = Form \u2115R\n\nzero : \u2115\nzero = inj \u2115R zeroT\n\nsuc : \u2115 \u2192 \u2115\nsuc = inj \u2115R sucT\n\nVecR : Data\nVecR = record\n { P = Arg Set (\u03bb _ \u2192 End)\n ; I = \u03bb _ \u2192 Arg \u2115 (\u03bb _ \u2192 End)\n ; E = VecE\n ; B = \u03bb A \u2192\n End (zero , tt)\n , Arg \u2115 (\u03bb n \u2192 Arg (proj\u2081 A) \u03bb _ \u2192 Rec (n , tt) (End (suc n , tt)))\n , tt\n }\n\nVec : (A : Set) \u2192 \u2115 \u2192 Set\nVec = Form VecR\n\nnil : {A : Set} \u2192 Vec A zero\nnil = inj VecR nilT\n\ncons : {A : Set} (n : \u2115) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\ncons = inj VecR consT\n\n----------------------------------------------------------------------\n\nadd : \u2115 \u2192 \u2115 \u2192 \u2115\nadd = elim \u2115R (\u03bb n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n\nmult : \u2115 \u2192 \u2115 \u2192 \u2115\nmult = elim \u2115R (\u03bb n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n\nappend : {A : Set} (m : \u2115) (xs : Vec A m) (n : \u2115) (ys : Vec A n) \u2192 Vec A (add m n)\nappend {A} = elim VecR (\u03bb m xs \u2192 (n : \u2115) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons (add m n) x (ih n ys))\n\nconcat : {A : Set} (m n : \u2115) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\nconcat {A} m = elim VecR (\u03bb n xss \u2192 Vec A (mult n m))\n nil\n (\u03bb n xs xss ih \u2192 append m xs (mult n m) ih)\n\n----------------------------------------------------------------------\n","old_contents":"{-# OPTIONS --type-in-type #-}\nopen import Data.Unit\nopen import Data.Product hiding ( curry ; uncurry )\nopen import Data.List hiding ( concat )\nopen import Data.String\nopen import Relation.Binary.PropositionalEquality\nopen import Function\nmodule Spire.Examples.DarkwingDuck where\n\n----------------------------------------------------------------------\n\nLabel : Set\nLabel = String\n\nEnum : Set\nEnum = List Label\n\ndata Tag : Enum \u2192 Set where\n here : \u2200{l E} \u2192 Tag (l \u2237 E)\n there : \u2200{l E} \u2192 Tag E \u2192 Tag (l \u2237 E)\n\nBranches : (E : Enum) (P : Tag E \u2192 Set) \u2192 Set\nBranches [] P = \u22a4\nBranches (l \u2237 E) P = P here \u00d7 Branches E (\u03bb t \u2192 P (there t))\n\ncase : {E : Enum} (P : Tag E \u2192 Set) (cs : Branches E P) (t : Tag E) \u2192 P t\ncase P (c , cs) here = c\ncase P (c , cs) (there t) = case (\u03bb t \u2192 P (there t)) cs t\n\n----------------------------------------------------------------------\n\ndata Tel : Set where\n End : Tel\n Arg : (A : Set) (B : A \u2192 Tel) \u2192 Tel\n\nEl\u1d40 : Tel \u2192 Set\nEl\u1d40 End = \u22a4\nEl\u1d40 (Arg A B) = \u03a3 A (\u03bb a \u2192 El\u1d40 (B a))\n\n----------------------------------------------------------------------\n\ndata Desc (I : Set) : Set where\n End : (i : I) \u2192 Desc I\n Rec : (i : I) (D : Desc I) \u2192 Desc I\n Arg : (A : Set) (B : A \u2192 Desc I) \u2192 Desc I\n\n----------------------------------------------------------------------\n\nISet : Set \u2192 Set\nISet I = I \u2192 Set\n\nEl\u1d30 : {I : Set} (D : Desc I) \u2192 ISet I \u2192 ISet I\nEl\u1d30 (End j) X i = j \u2261 i\nEl\u1d30 (Rec j D) X i = X j \u00d7 El\u1d30 D X i\nEl\u1d30 (Arg A B) X i = \u03a3 A (\u03bb a \u2192 El\u1d30 (B a) X i)\n\nHyps : {I : Set} (D : Desc I) (X : ISet I) (P : (i : I) \u2192 X i \u2192 Set) (i : I) (xs : El\u1d30 D X i) \u2192 Set\nHyps (End j) X P i q = \u22a4\nHyps (Rec j D) X P i (x , xs) = P j x \u00d7 Hyps D X P i xs\nHyps (Arg A B) X P i (a , b) = Hyps (B a) X P i b\n\n----------------------------------------------------------------------\n\ndata \u03bc {I : Set} (D : Desc I) (i : I) : Set where\n init : El\u1d30 D (\u03bc D) i \u2192 \u03bc D i\n\n----------------------------------------------------------------------\n\nind : {I : Set} (D : Desc I)\n (M : (i : I) \u2192 \u03bc D i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 D (\u03bc D) i) (ihs : Hyps D (\u03bc D) M i xs) \u2192 M i (init xs))\n (i : I)\n (x : \u03bc D i)\n \u2192 M i x\n\nprove : {I : Set} (D E : Desc I)\n (M : (i : I) \u2192 \u03bc E i \u2192 Set)\n (\u03b1 : \u2200 i (xs : El\u1d30 E (\u03bc E) i) (ihs : Hyps E (\u03bc E) M i xs) \u2192 M i (init xs))\n (i : I) (xs : El\u1d30 D (\u03bc E) i) \u2192 Hyps D (\u03bc E) M i xs\n\nind D M \u03b1 i (init xs) = \u03b1 i xs (prove D D M \u03b1 i xs)\n\nprove (End j) E M \u03b1 i q = tt\nprove (Rec j D) E M \u03b1 i (x , xs) = ind E M \u03b1 j x , prove D E M \u03b1 i xs\nprove (Arg A B) E M \u03b1 i (a , xs) = prove (B a) E M \u03b1 i xs\n\n----------------------------------------------------------------------\n\nrecord Data : Set where\n field\n P : Tel\n I : El\u1d40 P \u2192 Tel\n E : Enum\n B : (A : El\u1d40 P) \u2192 Branches E (\u03bb _ \u2192 Desc (El\u1d40 (I A)))\n\n C : (A : El\u1d40 P) \u2192 Tag E \u2192 Desc (El\u1d40 (I A))\n C A = case (\u03bb _ \u2192 Desc (El\u1d40 (I A))) (B A)\n\n D : (A : El\u1d40 P) \u2192 Desc (El\u1d40 (I A))\n D A = Arg (Tag E) (C A)\n\n----------------------------------------------------------------------\n\nUncurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nUncurriedBranches E P X = Branches E P \u2192 X\n\nCurriedBranches : (E : Enum) (P : Tag E \u2192 Set) (X : Set)\n \u2192 Set\nCurriedBranches [] P X = X\nCurriedBranches (l \u2237 E) P X = P here \u2192 CurriedBranches E (\u03bb t \u2192 P (there t)) X\n\ncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 UncurriedBranches E P X \u2192 CurriedBranches E P X\ncurryBranches {[]} f = f tt\ncurryBranches {l \u2237 E} f = \u03bb c \u2192 curryBranches (\u03bb cs \u2192 f (c , cs))\n\nuncurryBranches : {E : Enum} {P : Tag E \u2192 Set} {X : Set}\n \u2192 CurriedBranches E P X \u2192 UncurriedBranches E P X\nuncurryBranches {[]} x tt = x\nuncurryBranches {l \u2237 E} f (c , cs) = uncurryBranches (f c) cs\n\n----------------------------------------------------------------------\n\nUncurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nUncurriedEl\u1d40 T X = (xs : El\u1d40 T) \u2192 X xs\n\nCurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nCurriedEl\u1d40 End X = X tt\nCurriedEl\u1d40 (Arg A B) X = (a : A) \u2192 CurriedEl\u1d40 (B a) (\u03bb b \u2192 X (a , b))\n\ncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 UncurriedEl\u1d40 T X \u2192 CurriedEl\u1d40 T X\ncurryEl\u1d40 End X f = f tt\ncurryEl\u1d40 (Arg A B) X f = \u03bb a \u2192 curryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (\u03bb b \u2192 f (a , b))\n\nuncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 CurriedEl\u1d40 T X \u2192 UncurriedEl\u1d40 T X\nuncurryEl\u1d40 End X x tt = x\nuncurryEl\u1d40 (Arg A B) X f (a , b) = uncurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (f a) b\n\nICurriedEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set) \u2192 Set\nICurriedEl\u1d40 End X = X tt\nICurriedEl\u1d40 (Arg A B) X = {a : A} \u2192 ICurriedEl\u1d40 (B a) (\u03bb b \u2192 X (a , b))\n\nicurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 UncurriedEl\u1d40 T X \u2192 ICurriedEl\u1d40 T X\nicurryEl\u1d40 End X f = f tt\nicurryEl\u1d40 (Arg A B) X f = \u03bb {a} \u2192 icurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) (\u03bb b \u2192 f (a , b))\n\niuncurryEl\u1d40 : (T : Tel) (X : El\u1d40 T \u2192 Set)\n \u2192 ICurriedEl\u1d40 T X \u2192 UncurriedEl\u1d40 T X\niuncurryEl\u1d40 End X x tt = x\niuncurryEl\u1d40 (Arg A B) X f (a , b) = iuncurryEl\u1d40 (B a) (\u03bb b \u2192 X (a , b)) f b\n\n----------------------------------------------------------------------\n\nUncurriedEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nUncurriedEl\u1d30 D X = \u2200{i} \u2192 El\u1d30 D X i \u2192 X i\n\nCurriedEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I) \u2192 Set\nCurriedEl\u1d30 (End i) X = X i\nCurriedEl\u1d30 (Rec i D) X = (x : X i) \u2192 CurriedEl\u1d30 D X\nCurriedEl\u1d30 (Arg A B) X = (a : A) \u2192 CurriedEl\u1d30 (B a) X\n\ncurryEl\u1d30 : {I : Set} (D : Desc I) (X : ISet I)\n \u2192 UncurriedEl\u1d30 D X \u2192 CurriedEl\u1d30 D X\ncurryEl\u1d30 (End i) X cn = cn refl\ncurryEl\u1d30 (Rec i D) X cn = \u03bb x \u2192 curryEl\u1d30 D X (\u03bb xs \u2192 cn (x , xs))\ncurryEl\u1d30 (Arg A B) X cn = \u03bb a \u2192 curryEl\u1d30 (B a) X (\u03bb xs \u2192 cn (a , xs))\n\n----------------------------------------------------------------------\n\nUncurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 Set\nUncurriedHyps D X P cn =\n \u2200 i (xs : El\u1d30 D X i) (ihs : Hyps D X P i xs) \u2192 P i (cn xs)\n\nCurriedHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 Set\nCurriedHyps (End i) X P cn =\n P i (cn refl)\nCurriedHyps (Rec i D) X P cn =\n (x : X i) \u2192 P i x \u2192 CurriedHyps D X P (\u03bb xs \u2192 cn (x , xs))\nCurriedHyps (Arg A B) X P cn =\n (a : A) \u2192 CurriedHyps (B a) X P (\u03bb xs \u2192 cn (a , xs))\n\ncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 UncurriedHyps D X P cn\n \u2192 CurriedHyps D X P cn\ncurryHyps (End i) X P cn pf =\n pf i refl tt\ncurryHyps (Rec i D) X P cn pf =\n \u03bb x ih \u2192 curryHyps D X P (\u03bb xs \u2192 cn (x , xs)) (\u03bb i xs ihs \u2192 pf i (x , xs) (ih , ihs))\ncurryHyps (Arg A B) X P cn pf =\n \u03bb a \u2192 curryHyps (B a) X P (\u03bb xs \u2192 cn (a , xs)) (\u03bb i xs ihs \u2192 pf i (a , xs) ihs)\n\nuncurryHyps : {I : Set} (D : Desc I) (X : ISet I)\n (P : (i : I) \u2192 X i \u2192 Set)\n (cn : UncurriedEl\u1d30 D X)\n \u2192 CurriedHyps D X P cn\n \u2192 UncurriedHyps D X P cn\nuncurryHyps (End .i) X P cn pf i refl tt =\n pf\nuncurryHyps (Rec j D) X P cn pf i (x , xs) (ih , ihs) =\n uncurryHyps D X P (\u03bb ys \u2192 cn (x , ys)) (pf x ih) i xs ihs\nuncurryHyps (Arg A B) X P cn pf i (a , xs) ihs =\n uncurryHyps (B a) X P (\u03bb ys \u2192 cn (a , ys)) (pf a) i xs ihs\n\n----------------------------------------------------------------------\n\nFormUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 UncurriedEl\u1d40 (Data.I R p) \u03bb i\n \u2192 Set\nFormUncurried R p i = \u03bc (Data.D R p) i\n\nForm : (R : Data)\n \u2192 CurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 CurriedEl\u1d40 (Data.I R p) \u03bb i\n \u2192 Set\nForm R =\n curryEl\u1d40 (Data.P R) (\u03bb p \u2192 CurriedEl\u1d40 (Data.I R p) \u03bb i \u2192 Set) \u03bb p \u2192\n curryEl\u1d40 (Data.I R p) (\u03bb i \u2192 Set) \u03bb i \u2192\n FormUncurried R p i\n\n----------------------------------------------------------------------\n\ninjUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p\n in CurriedEl\u1d30 D (\u03bc D)\ninjUncurried R p t = curryEl\u1d30 (Data.C R p t)\n (\u03bc (Data.D R p))\n (\u03bb xs \u2192 init (t , xs))\n\ninj : (R : Data)\n \u2192 ICurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p\n in CurriedEl\u1d30 D (\u03bc D)\ninj R = icurryEl\u1d40 (Data.P R)\n (\u03bb p \u2192 let D = Data.D R p in CurriedEl\u1d30 D (\u03bc D))\n (injUncurried R)\n\n----------------------------------------------------------------------\n\nindCurried : {I : Set} (D : Desc I)\n (M : (i : I) \u2192 \u03bc D i \u2192 Set)\n (f : CurriedHyps D (\u03bc D) M init)\n (i : I)\n (x : \u03bc D i)\n \u2192 M i x\nindCurried D M f i x =\n ind D M (uncurryHyps D (\u03bc D) M init f) i x\n\nSumCurriedHyps : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : \u2200 i \u2192 \u03bc D i \u2192 Set)\n \u2192 Tag (Data.E R) \u2192 Set\nSumCurriedHyps R p M t =\n CurriedHyps (Data.C R p t) (\u03bc (Data.D R p)) M (\u03bb xs \u2192 init (t , xs))\n\nelimUncurried : (R : Data)\n \u2192 UncurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : UncurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 UncurriedBranches (Data.E R)\n (SumCurriedHyps R p M)\n (\u2200 i (x : \u03bc D i) \u2192 M i x)\nelimUncurried R p M cs i x =\n indCurried (Data.D R p) M\n (case (SumCurriedHyps R p M) cs)\n i x\n\nelim : (R : Data)\n \u2192 ICurriedEl\u1d40 (Data.P R) \u03bb p\n \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in CurriedBranches (Data.E R)\n (SumCurriedHyps R p unM)\n (\u2200 i (x : \u03bc D i) \u2192 unM i x)\nelim R = icurryEl\u1d40 (Data.P R)\n (\u03bb p \u2192 let D = Data.D R p in\n (M : CurriedEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set))\n \u2192 let unM = uncurryEl\u1d40 (Data.I R p) (\u03bb i \u2192 \u03bc D i \u2192 Set) M\n in CurriedBranches (Data.E R)\n (SumCurriedHyps R p unM)\n (\u2200 i (x : \u03bc D i) \u2192 unM i x))\n (\u03bb p M \u2192 curryBranches\n (elimUncurried R p\n (uncurryEl\u1d40 (Data.I R p)\n ((\u03bb i \u2192 \u03bc (Data.D R p) i \u2192 Set)) M)))\n\n----------------------------------------------------------------------\n\n\u2115E : Enum\n\u2115E = \"zero\" \u2237 \"suc\" \u2237 []\n\nVecE : Enum\nVecE = \"nil\" \u2237 \"cons\" \u2237 []\n\n\u2115T : Set\n\u2115T = Tag \u2115E\n\nVecT : Set\nVecT = Tag VecE\n\nzeroT : \u2115T\nzeroT = here\n\nsucT : \u2115T\nsucT = there here\n\nnilT : VecT\nnilT = here\n\nconsT : VecT\nconsT = there here\n\n----------------------------------------------------------------------\n\n\u2115R : Data\n\u2115R = record\n { P = End\n ; I = \u03bb _ \u2192 End\n ; E = \u2115E\n ; B = \u03bb _ \u2192\n End tt\n , Rec tt (End tt)\n , tt\n }\n\n\u2115 : Set\n\u2115 = Form \u2115R\n\nzero : \u2115\nzero = inj \u2115R zeroT\n\nsuc : \u2115 \u2192 \u2115\nsuc = inj \u2115R sucT\n\nVecR : Data\nVecR = record\n { P = Arg Set (\u03bb _ \u2192 End)\n ; I = \u03bb _ \u2192 Arg \u2115 (\u03bb _ \u2192 End)\n ; E = VecE\n ; B = \u03bb A \u2192\n End (zero , tt)\n , Arg \u2115 (\u03bb n \u2192 Arg (proj\u2081 A) \u03bb _ \u2192 Rec (n , tt) (End (suc n , tt)))\n , tt\n }\n\nVec : (A : Set) \u2192 \u2115 \u2192 Set\nVec = Form VecR\n\nnil : {A : Set} \u2192 Vec A zero\nnil = inj VecR nilT\n\ncons : {A : Set} (n : \u2115) (x : A) (xs : Vec A n) \u2192 Vec A (suc n)\ncons = inj VecR consT\n\n----------------------------------------------------------------------\n\nadd : \u2115 \u2192 \u2115 \u2192 \u2115\nadd = elim \u2115R (\u03bb n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 n)\n (\u03bb m ih n \u2192 suc (ih n))\n tt\n\nmult : \u2115 \u2192 \u2115 \u2192 \u2115\nmult = elim \u2115R (\u03bb n \u2192 \u2115 \u2192 \u2115)\n (\u03bb n \u2192 zero)\n (\u03bb m ih n \u2192 add n (ih n))\n tt\n\nappend : {A : Set} (m : \u2115) (xs : Vec A m) (n : \u2115) (ys : Vec A n) \u2192 Vec A (add m n)\nappend {A} m xs = elim VecR (\u03bb m xs \u2192 (n : \u2115) (ys : Vec A n) \u2192 Vec A (add m n))\n (\u03bb n ys \u2192 ys)\n (\u03bb m x xs ih n ys \u2192 cons (add m n) x (ih n ys))\n (m , tt) xs\n\nconcat : {A : Set} (m n : \u2115) (xss : Vec (Vec A m) n) \u2192 Vec A (mult n m)\nconcat {A} m n xss = elim VecR (\u03bb n xss \u2192 Vec A (mult n m))\n nil\n (\u03bb n xs xss ih \u2192 append m xs (mult n m) ih)\n (n , tt) xss\n\n----------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"9d1e02a049e0eac1e796dc1d74d47064871effb3","subject":"Remove power from the guessing game","message":"Remove power from the guessing game\n","repos":"crypto-agda\/crypto-agda","old_file":"otp-sem-sec.agda","new_file":"otp-sem-sec.agda","new_contents":"module otp-sem-sec where\n\nimport Level as L\nopen import Function\nopen import Data.Nat.NP\nopen import Data.Bits\nopen import Data.Bool\nopen import Data.Bool.Properties\nopen import Data.Vec hiding (_>>=_)\nopen import Data.Product.NP hiding (_\u27e6\u00d7\u27e7_)\nopen import Relation.Nullary\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\nopen import flipbased-implem\nopen \u2261-Reasoning\nopen import Data.Unit using (\u22a4)\nopen import composable\nopen import vcomp\nopen import forkable\nopen import flat-funs\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nmodule BitsExtra where\n splitAt\u2032 : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k \u00d7 Bits n\n splitAt\u2032 k xs = case splitAt k xs of \u03bb { (ys , zs , _) \u2192 (ys , zs) }\n\n vnot\u2218vnot : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\n vnot\u2218vnot [] = refl\n vnot\u2218vnot (x \u2237 xs) rewrite not-involutive x = cong (_\u2237_ x) (vnot\u2218vnot xs)\n\nopen BitsExtra\n\nCoins = \u2115\n\nrecord PrgDist : Set\u2081 where\n constructor mk\n field\n _]-[_ : \u2200 {c} (f g : \u21ba c Bit) \u2192 Set\n ]-[-cong : \u2200 {c} {f f' g g' : \u21ba c Bit} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f ]-[ f' \u2192 g ]-[ g'\n\n breaks : \u2200 {c} (EXP : Bit \u2192 \u21ba c Bit) \u2192 Set\n breaks \u2141 = \u2141 0b ]-[ \u2141 1b\n\n _\u2257\u2141_ : \u2200 {c} (\u2141\u2080 \u2141\u2081 : Bit \u2192 \u21ba c Bit) \u2192 Set\n \u2141\u2080 \u2257\u2141 \u2141\u2081 = \u2200 b \u2192 \u2141\u2080 b \u2257\u21ba \u2141\u2081 b\n\n \u2257\u2141-trans : \u2200 {c} \u2192 Transitive (_\u2257\u2141_ {c})\n \u2257\u2141-trans p q b R = trans (p b R) (q b R)\n\n -- An wining adversary for game \u2141\u2080 reduces to a wining adversary for game \u2141\u2081\n _\u21d3_ : \u2200 {c\u2080 c\u2081} (\u2141\u2080 : Bit \u2192 \u21ba c\u2080 Bit) (\u2141\u2081 : Bit \u2192 \u21ba c\u2081 Bit) \u2192 Set\n \u2141\u2080 \u21d3 \u2141\u2081 = breaks \u2141\u2080 \u2192 breaks \u2141\u2081\n\n extensional-reduction : \u2200 {c} {\u2141\u2080 \u2141\u2081 : Bit \u2192 \u21ba c Bit}\n \u2192 \u2141\u2080 \u2257\u2141 \u2141\u2081 \u2192 \u2141\u2080 \u21d3 \u2141\u2081\n extensional-reduction same-games = ]-[-cong (same-games 0b) (same-games 1b)\n\nmodule Guess (prgDist : PrgDist) where\n open PrgDist prgDist\n\n GuessAdv : Coins \u2192 Set\n GuessAdv c = \u21ba c Bit\n\n runGuess\u2141 : \u2200 {ca} (A : GuessAdv ca) (b : Bit) \u2192 \u21ba ca Bit\n runGuess\u2141 A _ = A\n\n -- An oracle: an adversary who can break the guessing game.\n Oracle : Coins \u2192 Set\n Oracle ca = \u2203 (\u03bb (A : GuessAdv ca) \u2192 breaks (runGuess\u2141 A))\n\n -- Any adversary cannot do better than a random guess.\n GuessSec : Coins \u2192 Set\n GuessSec ca = \u2200 (A : GuessAdv ca) \u2192 \u00ac(breaks (runGuess\u2141 A))\n\nmodule AbsSemSec (|M| |C| : \u2115) {t} {T : Set t}\n (\u266dFuns : FlatFuns T) where\n\n open FlatFuns \u266dFuns\n\n M = `Bits |M|\n C = `Bits |C|\n\n record AbsSemSecAdv (|R| : Coins) : Set where\n constructor mk\n\n field\n {|S|} : \u2115\n\n S = `Bits |S|\n R = `Bits |R|\n\n field\n step\u2080 : R `\u2192 (M `\u00d7 M) `\u00d7 S\n step\u2081 : C `\u00d7 S `\u2192 `Bit\n -- step\u2080 : \u27e8 p\u2080 \u27e9 R \u219d (M `\u00d7 M) `\u00d7 S\n -- step\u2081 : \u27e8 p\u2081 \u27e9 C `\u00d7 S \u219d `Bit\n\n SemSecReduction : \u2200 (f : Coins \u2192 Coins) \u2192 Set\n SemSecReduction f = \u2200 {c} \u2192 AbsSemSecAdv c \u2192 AbsSemSecAdv (f c)\n\n-- Here we use Agda functions for FlatFuns.\nmodule FunSemSec (prgDist : PrgDist) (|M| |C| : \u2115) where\n open PrgDist prgDist\n open AbsSemSec |M| |C| fun\u266dFuns\n open FlatFunsOps fun\u266dOps\n\n M\u00b2 = Bit \u2192 M\n\n Enc : \u2200 cc \u2192 Set\n Enc cc = M \u2192 \u21ba cc C\n\n Tr : (cc\u2080 cc\u2081 : Coins) \u2192 Set\n Tr cc\u2080 cc\u2081 = Enc cc\u2080 \u2192 Enc cc\u2081\n\n FunSemSecAdv : Coins \u2192 Set\n FunSemSecAdv = AbsSemSecAdv\n\n module FunSemSecAdv {|R|} (A : FunSemSecAdv |R|) where\n open AbsSemSecAdv A public\n\n step\u2080F : R \u2192 (M\u00b2 \u00d7 S)\n step\u2080F = step\u2080 >>> proj *** idO\n\n step\u2080\u21ba : \u21ba |R| (M\u00b2 \u00d7 S)\n step\u2080\u21ba = mk step\u2080F\n\n step\u2081F : S \u2192 C \u2192 Bit\n step\u2081F s c = step\u2081 (c , s)\n\n -- Returing 0 means Chal wins, Adv looses\n -- 1 means Adv wins, Chal looses\n runSemSec : \u2200 {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b \u2192 \u21ba (ca + cc) Bit\n runSemSec E A b\n = A-step\u2080 >>= \u03bb { (m , s) \u2192 map\u21ba (A-step\u2081 s) (E (m b)) }\n where open FunSemSecAdv A renaming (step\u2080\u21ba to A-step\u2080; step\u2081F to A-step\u2081)\n\n _\u21c4_ : \u2200 {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b \u2192 \u21ba (ca + cc) Bit\n _\u21c4_ = runSemSec\n\n runAdv : \u2200 {|R|} \u2192 FunSemSecAdv |R| \u2192 C \u2192 Bits |R| \u2192 (M \u00d7 M) \u00d7 Bit\n runAdv (mk A-step\u2080 A-step\u2081) C = A-step\u2080 >>> id *** (const C &&& id >>> A-step\u2081)\n\n _\u2257A_ : \u2200 {p} (A\u2081 A\u2082 : FunSemSecAdv p) \u2192 Set\n A\u2080 \u2257A A\u2081 = \u2200 C R \u2192 runAdv A\u2080 C R \u2261 runAdv A\u2081 C R\n\n change-adv : \u2200 {cc ca} {E : Enc cc} {A\u2081 A\u2082 : FunSemSecAdv ca} \u2192 A\u2081 \u2257A A\u2082 \u2192 (E \u21c4 A\u2081) \u2257\u2141 (E \u21c4 A\u2082)\n change-adv {ca = ca} {A\u2081 = _} {_} pf b R with splitAt ca R\n change-adv {E = E} {A\u2081} {A\u2082} pf b ._ | pre \u03a3., post , refl = trans (cong proj\u2082 (helper\u2080 A\u2081)) helper\u2082\n where open FunSemSecAdv\n helper\u2080 = \u03bb A \u2192 pf (run\u21ba (E (proj (proj\u2081 (step\u2080 A pre)) b)) post) pre\n helper\u2082 = cong (\u03bb m \u2192 step\u2081 A\u2082 (run\u21ba (E (proj (proj\u2081 m) b)) post , proj\u2082 (step\u2080 A\u2082 pre)))\n (helper\u2080 A\u2082)\n\n SafeSemSecReduction : \u2200 (f : Coins \u2192 Coins) {cc\u2080 cc\u2081} (E\u2080 : Enc cc\u2080) (E\u2081 : Enc cc\u2081) \u2192 Set\n SafeSemSecReduction f E\u2080 E\u2081 =\n \u2203 \u03bb (red : SemSecReduction f) \u2192\n \u2200 {c} A \u2192 (E\u2080 \u21c4 A) \u21d3 (E\u2081 \u21c4 red {c} A)\n\n SemSecTr : \u2200 {cc\u2080 cc\u2081} (f : Coins \u2192 Coins) (tr : Tr cc\u2080 cc\u2081) \u2192 Set\n SemSecTr {cc\u2080} f tr = \u2200 {E : Enc cc\u2080} \u2192 SafeSemSecReduction f (tr E) E\n\nmodule PostCompSec (prgDist : PrgDist) (|M| |C| : \u2115) where\n module PostCompRed {t} {T : Set t}\n {\u266dFuns : FlatFuns T}\n (\u266dops : FlatFunsOps \u266dFuns) where\n open FlatFunsOps \u266dops\n open AbsSemSec |M| |C| \u266dFuns\n\n post-comp-red : (post-E : C `\u2192 C) \u2192 SemSecReduction _\n post-comp-red post-E (mk A\u2080 A\u2081) = mk A\u2080 (post-E *** idO >>> A\u2081)\n\n open PrgDist prgDist\n open PostCompRed fun\u266dOps\n open FlatFunsOps fun\u266dOps\n open FunSemSec prgDist |M| |C|\n open AbsSemSec |M| |C| fun\u266dFuns\n\n post-comp : \u2200 {cc} (post-E : C \u2192 C) \u2192 Tr cc cc\n post-comp post-E E = E >>> map\u21ba post-E\n\n post-comp-pres-sem-sec : \u2200 {cc} (post-E : C \u2192 C)\n \u2192 SemSecTr id (post-comp {cc} post-E)\n post-comp-pres-sem-sec post-E = post-comp-red post-E , (\u03bb _ \u2192 id)\n\n post-comp-pres-sem-sec' : \u2200 (post-E post-E\u207b\u00b9 : C \u2192 C)\n (post-E-inv : post-E\u207b\u00b9 \u2218 post-E \u2257 id)\n {cc} {E : Enc cc}\n \u2192 SafeSemSecReduction id E (post-comp post-E E)\n post-comp-pres-sem-sec' post-E post-E\u207b\u00b9 post-E-inv {cc} {E} = red , helper where\n E' = post-comp post-E E\n red : SemSecReduction id\n red = post-comp-red post-E\u207b\u00b9\n helper : \u2200 {p} A \u2192 (E \u21c4 A) \u21d3 (E' \u21c4 red {p} A)\n helper {c} A A-breaks-E = A'-breaks-E'\n where open FunSemSecAdv A renaming (step\u2080F to A\u2080F)\n A' = red {c} A\n same-games : (E \u21c4 A) \u2257\u2141 (E' \u21c4 A')\n same-games b R\n rewrite post-E-inv (run\u21ba (E (proj\u2081 (A\u2080F (take c R)) b))\n (drop c R)) = refl\n A'-breaks-E' : breaks (E' \u21c4 A')\n A'-breaks-E' = extensional-reduction same-games A-breaks-E\n\n post-neg : \u2200 {cc} \u2192 Tr cc cc\n post-neg = post-comp vnot\n\n post-neg-pres-sem-sec : \u2200 {cc} \u2192 SemSecTr id (post-neg {cc})\n post-neg-pres-sem-sec {cc} {E} = post-comp-pres-sem-sec vnot {E}\n\n post-neg-pres-sem-sec' : \u2200 {cc} {E : Enc cc}\n \u2192 SafeSemSecReduction id E (post-neg E)\n post-neg-pres-sem-sec' {cc} {E} = post-comp-pres-sem-sec' vnot vnot vnot\u2218vnot {cc} {E}\n\nopen import diff\nimport Data.Fin as Fin\n_]-_-[_ : \u2200 {c} (f : \u21ba c Bit) k (g : \u21ba c Bit) \u2192 Set\n_]-_-[_ {c} f k g = diff (Fin.to\u2115 #\u27e8 run\u21ba f \u27e9) (Fin.to\u2115 #\u27e8 run\u21ba g \u27e9) \u2265 2^(c \u2238 k)\n -- diff (#1 f) (#1 g) \u2265 2^(-k) * 2^ c\n -- diff (#1 f) (#1 g) \u2265 \u03b5 * 2^ c\n -- dist (#1 f) (#1 g) \u2265 \u03b5 * 2^ c\n -- where \u03b5 = 2^ -k\n -- {!dist (#1 f \/ 2^ c) (#1 g \/ 2^ c) > \u03b5 !}\n\nopen import Data.Vec.NP using (count)\n\next-count : \u2200 {n a} {A : Set a} {f g : A \u2192 Bool} \u2192 f \u2257 g \u2192 (xs : Vec A n) \u2192 count f xs \u2261 count g xs\next-count f\u2257g [] = refl\next-count f\u2257g (x \u2237 xs) rewrite ext-count f\u2257g xs | f\u2257g x = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\next-# f\u2257g = ext-count f\u2257g (allBits _)\n\n]-[-cong : \u2200 {k c} {f f' g g' : \u21ba c Bit} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f ]- k -[ f' \u2192 g ]- k -[ g'\n]-[-cong f\u2257g f'\u2257g' f]-[f' rewrite ext-# f\u2257g | ext-# f'\u2257g' = f]-[f'\n\nmodule Concrete k where\n _]-[_ : \u2200 {c} (f g : \u21ba c Bit) \u2192 Set\n _]-[_ f g = f ]- k -[ g\n cong' : \u2200 {c} {f f' g g' : \u21ba c Bit} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f ]-[ f' \u2192 g ]-[ g'\n cong' = ]-[-cong {k}\n prgDist : PrgDist\n prgDist = mk _]-[_ cong'\n module Guess' = Guess prgDist\n module FunSemSec' = FunSemSec prgDist\n module PostCompSec' = PostCompSec prgDist\n","old_contents":"module otp-sem-sec where\n\nimport Level as L\nopen import Function\nopen import Data.Nat.NP\nopen import Data.Bits\nopen import Data.Bool\nopen import Data.Bool.Properties\nopen import Data.Vec hiding (_>>=_)\nopen import Data.Product.NP hiding (_\u27e6\u00d7\u27e7_)\nopen import Relation.Nullary\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality.NP\nopen import flipbased-implem\nopen \u2261-Reasoning\nopen import Data.Unit using (\u22a4)\nopen import composable\nopen import vcomp\nopen import forkable\nopen import flat-funs\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nmodule BitsExtra where\n splitAt\u2032 : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k \u00d7 Bits n\n splitAt\u2032 k xs = case splitAt k xs of \u03bb { (ys , zs , _) \u2192 (ys , zs) }\n\n vnot\u2218vnot : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\n vnot\u2218vnot [] = refl\n vnot\u2218vnot (x \u2237 xs) rewrite not-involutive x = cong (_\u2237_ x) (vnot\u2218vnot xs)\n\nopen BitsExtra\n\nCoins = \u2115\n\nrecord PrgDist : Set\u2081 where\n constructor mk\n field\n _]-[_ : \u2200 {c} (f g : \u21ba c Bit) \u2192 Set\n ]-[-cong : \u2200 {c} {f f' g g' : \u21ba c Bit} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f ]-[ f' \u2192 g ]-[ g'\n\n breaks : \u2200 {c} (EXP : Bit \u2192 \u21ba c Bit) \u2192 Set\n breaks \u2141 = \u2141 0b ]-[ \u2141 1b\n\n _\u2257\u2141_ : \u2200 {c} (\u2141\u2080 \u2141\u2081 : Bit \u2192 \u21ba c Bit) \u2192 Set\n \u2141\u2080 \u2257\u2141 \u2141\u2081 = \u2200 b \u2192 \u2141\u2080 b \u2257\u21ba \u2141\u2081 b\n\n \u2257\u2141-trans : \u2200 {c} \u2192 Transitive (_\u2257\u2141_ {c})\n \u2257\u2141-trans p q b R = trans (p b R) (q b R)\n\n -- An wining adversary for game \u2141\u2080 reduces to a wining adversary for game \u2141\u2081\n _\u21d3_ : \u2200 {c\u2080 c\u2081} (\u2141\u2080 : Bit \u2192 \u21ba c\u2080 Bit) (\u2141\u2081 : Bit \u2192 \u21ba c\u2081 Bit) \u2192 Set\n \u2141\u2080 \u21d3 \u2141\u2081 = breaks \u2141\u2080 \u2192 breaks \u2141\u2081\n\n extensional-reduction : \u2200 {c} {\u2141\u2080 \u2141\u2081 : Bit \u2192 \u21ba c Bit}\n \u2192 \u2141\u2080 \u2257\u2141 \u2141\u2081 \u2192 \u2141\u2080 \u21d3 \u2141\u2081\n extensional-reduction same-games = ]-[-cong (same-games 0b) (same-games 1b)\n\nmodule Guess (Power : Set) (coins : Power \u2192 Coins) (prgDist : PrgDist) where\n open PrgDist prgDist\n\n GuessAdv : Coins \u2192 Set\n GuessAdv c = \u21ba c Bit\n\n runGuess\u2141 : \u2200 {ca} (A : GuessAdv ca) (b : Bit) \u2192 \u21ba ca Bit\n runGuess\u2141 A _ = A\n\n -- An oracle: an adversary who can break the guessing game.\n Oracle : Power \u2192 Set\n Oracle power = \u2203 (\u03bb (A : GuessAdv (coins power)) \u2192 breaks (runGuess\u2141 A))\n\n -- Any adversary cannot do better than a random guess.\n GuessSec : Power \u2192 Set\n GuessSec power = \u2200 (A : GuessAdv (coins power)) \u2192 \u00ac(breaks (runGuess\u2141 A))\n\nmodule AbsSemSec (|M| |C| : \u2115) {t} {T : Set t}\n (\u266dFuns : FlatFuns T) where\n\n open FlatFuns \u266dFuns\n\n M = `Bits |M|\n C = `Bits |C|\n\n record AbsSemSecAdv (|R| : Coins) : Set where\n constructor mk\n\n field\n {|S|} : \u2115\n\n S = `Bits |S|\n R = `Bits |R|\n\n field\n step\u2080 : R `\u2192 (M `\u00d7 M) `\u00d7 S\n step\u2081 : C `\u00d7 S `\u2192 `Bit\n -- step\u2080 : \u27e8 p\u2080 \u27e9 R \u219d (M `\u00d7 M) `\u00d7 S\n -- step\u2081 : \u27e8 p\u2081 \u27e9 C `\u00d7 S \u219d `Bit\n\n SemSecReduction : \u2200 (f : Coins \u2192 Coins) \u2192 Set\n SemSecReduction f = \u2200 {c} \u2192 AbsSemSecAdv c \u2192 AbsSemSecAdv (f c)\n\n-- Here we use Agda functions for FlatFuns.\nmodule FunSemSec (prgDist : PrgDist) (|M| |C| : \u2115) where\n open PrgDist prgDist\n open AbsSemSec |M| |C| fun\u266dFuns\n open FlatFunsOps fun\u266dOps\n\n M\u00b2 = Bit \u2192 M\n\n Enc : \u2200 cc \u2192 Set\n Enc cc = M \u2192 \u21ba cc C\n\n Tr : (cc\u2080 cc\u2081 : Coins) \u2192 Set\n Tr cc\u2080 cc\u2081 = Enc cc\u2080 \u2192 Enc cc\u2081\n\n FunSemSecAdv : Coins \u2192 Set\n FunSemSecAdv = AbsSemSecAdv\n\n module FunSemSecAdv {|R|} (A : FunSemSecAdv |R|) where\n open AbsSemSecAdv A public\n\n step\u2080F : R \u2192 (M\u00b2 \u00d7 S)\n step\u2080F = step\u2080 >>> proj *** idO\n\n step\u2080\u21ba : \u21ba |R| (M\u00b2 \u00d7 S)\n step\u2080\u21ba = mk step\u2080F\n\n step\u2081F : S \u2192 C \u2192 Bit\n step\u2081F s c = step\u2081 (c , s)\n\n -- Returing 0 means Chal wins, Adv looses\n -- 1 means Adv wins, Chal looses\n runSemSec : \u2200 {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b \u2192 \u21ba (ca + cc) Bit\n runSemSec E A b\n = A-step\u2080 >>= \u03bb { (m , s) \u2192 map\u21ba (A-step\u2081 s) (E (m b)) }\n where open FunSemSecAdv A renaming (step\u2080\u21ba to A-step\u2080; step\u2081F to A-step\u2081)\n\n _\u21c4_ : \u2200 {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b \u2192 \u21ba (ca + cc) Bit\n _\u21c4_ = runSemSec\n\n runAdv : \u2200 {|R|} \u2192 FunSemSecAdv |R| \u2192 C \u2192 Bits |R| \u2192 (M \u00d7 M) \u00d7 Bit\n runAdv (mk A-step\u2080 A-step\u2081) C = A-step\u2080 >>> id *** (const C &&& id >>> A-step\u2081)\n\n _\u2257A_ : \u2200 {p} (A\u2081 A\u2082 : FunSemSecAdv p) \u2192 Set\n A\u2080 \u2257A A\u2081 = \u2200 C R \u2192 runAdv A\u2080 C R \u2261 runAdv A\u2081 C R\n\n change-adv : \u2200 {cc ca} {E : Enc cc} {A\u2081 A\u2082 : FunSemSecAdv ca} \u2192 A\u2081 \u2257A A\u2082 \u2192 (E \u21c4 A\u2081) \u2257\u2141 (E \u21c4 A\u2082)\n change-adv {ca = ca} {A\u2081 = _} {_} pf b R with splitAt ca R\n change-adv {E = E} {A\u2081} {A\u2082} pf b ._ | pre \u03a3., post , refl = trans (cong proj\u2082 (helper\u2080 A\u2081)) helper\u2082\n where open FunSemSecAdv\n helper\u2080 = \u03bb A \u2192 pf (run\u21ba (E (proj (proj\u2081 (step\u2080 A pre)) b)) post) pre\n helper\u2082 = cong (\u03bb m \u2192 step\u2081 A\u2082 (run\u21ba (E (proj (proj\u2081 m) b)) post , proj\u2082 (step\u2080 A\u2082 pre)))\n (helper\u2080 A\u2082)\n\n SafeSemSecReduction : \u2200 (f : Coins \u2192 Coins) {cc\u2080 cc\u2081} (E\u2080 : Enc cc\u2080) (E\u2081 : Enc cc\u2081) \u2192 Set\n SafeSemSecReduction f E\u2080 E\u2081 =\n \u2203 \u03bb (red : SemSecReduction f) \u2192\n \u2200 {c} A \u2192 (E\u2080 \u21c4 A) \u21d3 (E\u2081 \u21c4 red {c} A)\n\n SemSecTr : \u2200 {cc\u2080 cc\u2081} (f : Coins \u2192 Coins) (tr : Tr cc\u2080 cc\u2081) \u2192 Set\n SemSecTr {cc\u2080} f tr = \u2200 {E : Enc cc\u2080} \u2192 SafeSemSecReduction f (tr E) E\n\nmodule PostCompSec (prgDist : PrgDist) (|M| |C| : \u2115) where\n module PostCompRed {t} {T : Set t}\n {\u266dFuns : FlatFuns T}\n (\u266dops : FlatFunsOps \u266dFuns) where\n open FlatFunsOps \u266dops\n open AbsSemSec |M| |C| \u266dFuns\n\n post-comp-red : (post-E : C `\u2192 C) \u2192 SemSecReduction _\n post-comp-red post-E (mk A\u2080 A\u2081) = mk A\u2080 (post-E *** idO >>> A\u2081)\n\n open PrgDist prgDist\n open PostCompRed fun\u266dOps\n open FlatFunsOps fun\u266dOps\n open FunSemSec prgDist |M| |C|\n open AbsSemSec |M| |C| fun\u266dFuns\n\n post-comp : \u2200 {cc} (post-E : C \u2192 C) \u2192 Tr cc cc\n post-comp post-E E = E >>> map\u21ba post-E\n\n post-comp-pres-sem-sec : \u2200 {cc} (post-E : C \u2192 C)\n \u2192 SemSecTr id (post-comp {cc} post-E)\n post-comp-pres-sem-sec post-E = post-comp-red post-E , (\u03bb _ \u2192 id)\n\n post-comp-pres-sem-sec' : \u2200 (post-E post-E\u207b\u00b9 : C \u2192 C)\n (post-E-inv : post-E\u207b\u00b9 \u2218 post-E \u2257 id)\n {cc} {E : Enc cc}\n \u2192 SafeSemSecReduction id E (post-comp post-E E)\n post-comp-pres-sem-sec' post-E post-E\u207b\u00b9 post-E-inv {cc} {E} = red , helper where\n E' = post-comp post-E E\n red : SemSecReduction id\n red = post-comp-red post-E\u207b\u00b9\n helper : \u2200 {p} A \u2192 (E \u21c4 A) \u21d3 (E' \u21c4 red {p} A)\n helper {c} A A-breaks-E = A'-breaks-E'\n where open FunSemSecAdv A renaming (step\u2080F to A\u2080F)\n A' = red {c} A\n same-games : (E \u21c4 A) \u2257\u2141 (E' \u21c4 A')\n same-games b R\n rewrite post-E-inv (run\u21ba (E (proj\u2081 (A\u2080F (take c R)) b))\n (drop c R)) = refl\n A'-breaks-E' : breaks (E' \u21c4 A')\n A'-breaks-E' = extensional-reduction same-games A-breaks-E\n\n post-neg : \u2200 {cc} \u2192 Tr cc cc\n post-neg = post-comp vnot\n\n post-neg-pres-sem-sec : \u2200 {cc} \u2192 SemSecTr id (post-neg {cc})\n post-neg-pres-sem-sec {cc} {E} = post-comp-pres-sem-sec vnot {E}\n\n post-neg-pres-sem-sec' : \u2200 {cc} {E : Enc cc}\n \u2192 SafeSemSecReduction id E (post-neg E)\n post-neg-pres-sem-sec' {cc} {E} = post-comp-pres-sem-sec' vnot vnot vnot\u2218vnot {cc} {E}\n\nopen import diff\nimport Data.Fin as Fin\n_]-_-[_ : \u2200 {c} (f : \u21ba c Bit) k (g : \u21ba c Bit) \u2192 Set\n_]-_-[_ {c} f k g = diff (Fin.to\u2115 #\u27e8 run\u21ba f \u27e9) (Fin.to\u2115 #\u27e8 run\u21ba g \u27e9) \u2265 2^(c \u2238 k)\n -- diff (#1 f) (#1 g) \u2265 2^(-k) * 2^ c\n -- diff (#1 f) (#1 g) \u2265 \u03b5 * 2^ c\n -- dist (#1 f) (#1 g) \u2265 \u03b5 * 2^ c\n -- where \u03b5 = 2^ -k\n -- {!dist (#1 f \/ 2^ c) (#1 g \/ 2^ c) > \u03b5 !}\n\nopen import Data.Vec.NP using (count)\n\next-count : \u2200 {n a} {A : Set a} {f g : A \u2192 Bool} \u2192 f \u2257 g \u2192 (xs : Vec A n) \u2192 count f xs \u2261 count g xs\next-count f\u2257g [] = refl\next-count f\u2257g (x \u2237 xs) rewrite ext-count f\u2257g xs | f\u2257g x = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\next-# f\u2257g = ext-count f\u2257g (allBits _)\n\n]-[-cong : \u2200 {k c} {f f' g g' : \u21ba c Bit} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f ]- k -[ f' \u2192 g ]- k -[ g'\n]-[-cong f\u2257g f'\u2257g' f]-[f' rewrite ext-# f\u2257g | ext-# f'\u2257g' = f]-[f'\n\nmodule Concrete k where\n _]-[_ : \u2200 {c} (f g : \u21ba c Bit) \u2192 Set\n _]-[_ f g = f ]- k -[ g\n cong' : \u2200 {c} {f f' g g' : \u21ba c Bit} \u2192 f \u2257\u21ba g \u2192 f' \u2257\u21ba g' \u2192 f ]-[ f' \u2192 g ]-[ g'\n cong' = ]-[-cong {k}\n prgDist : PrgDist\n prgDist = mk _]-[_ cong'\n module Guess' = Guess Coins id prgDist\n module FunSemSec' = FunSemSec prgDist\n module PostCompSec' = PostCompSec prgDist\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"64c75b3b67cf69a5efa9a5d4f14ab7deb2f1e5ae","subject":"Fin: +Fin\u25b9\ud835\udfd8, +\ud835\udfd8\u25b9Fin, +Fin\u25b9\ud835\udfd9, +\ud835\udfd9\u25b9Fin, +Fin\u25b9\ud835\udfda, +\ud835\udfda\u25b9Fin","message":"Fin: +Fin\u25b9\ud835\udfd8, +\ud835\udfd8\u25b9Fin, +Fin\u25b9\ud835\udfd9, +\ud835\udfd9\u25b9Fin, +Fin\u25b9\ud835\udfda, +\ud835\udfda\u25b9Fin\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Fin\/NP.agda","new_file":"lib\/Data\/Fin\/NP.agda","new_contents":"-- NOTE with-K\nmodule Data.Fin.NP where\n\nopen import Type hiding (\u2605)\nopen import Function\nopen import Data.Zero\nopen import Data.One\nopen import Data.Fin public renaming (to\u2115 to Fin\u25b9\u2115)\nopen import Data.Nat.NP using (\u2115; zero; suc; _<=_; module \u2115\u00b0) renaming (_+_ to _+\u2115_)\nopen import Data.Two hiding (_==_)\nimport Data.Vec.NP as Vec\nopen Vec using (Vec; []; _\u2237_; _\u2237\u02b3_; allFin; lookup; rot\u2081) renaming (map to vmap)\nimport Data.Vec.Properties as Vec\nopen import Data.Maybe.NP\nopen import Data.Sum as Sum\nopen import Relation.Binary.PropositionalEquality as \u2261\n\nsuc-injective : \u2200 {m}{i j : Fin m} \u2192 Fin.suc i \u2261 suc j \u2192 i \u2261 j\nsuc-injective refl = refl\n\n-- The isomorphisms about Fin, \ud835\udfd8, \ud835\udfd9, \ud835\udfda are in Function.Related.TypeIsomorphisms.NP\n\nFin\u25b9\ud835\udfd8 : Fin 0 \u2192 \ud835\udfd8\nFin\u25b9\ud835\udfd8 ()\n\n\ud835\udfd8\u25b9Fin : \ud835\udfd8 \u2192 Fin 0\n\ud835\udfd8\u25b9Fin ()\n\nFin\u25b9\ud835\udfd9 : Fin 1 \u2192 \ud835\udfd9\nFin\u25b9\ud835\udfd9 _ = _\n\n\ud835\udfd9\u25b9Fin : \ud835\udfd9 \u2192 Fin 1\n\ud835\udfd9\u25b9Fin _ = zero\n\nFin\u25b9\ud835\udfda : Fin 2 \u2192 \ud835\udfda\nFin\u25b9\ud835\udfda zero = 0\u2082\nFin\u25b9\ud835\udfda (suc _) = 1\u2082\n\n\ud835\udfda\u25b9Fin : \ud835\udfda \u2192 Fin 2\n\ud835\udfda\u25b9Fin = [0: # 0 1: # 1 ]\n\n_+\u2032_ : \u2200 {m n} (x : Fin m) (y : Fin n) \u2192 Fin (m +\u2115 n)\n_+\u2032_ {suc m} {n} zero y rewrite \u2115\u00b0.+-comm (suc m) n = inject+ _ y\nsuc x +\u2032 y = suc (x +\u2032 y)\n\n_==_ : \u2200 {n} (x y : Fin n) \u2192 \ud835\udfda\nx == y = helper (compare x y) where\n helper : \u2200 {n} {i j : Fin n} \u2192 Ordering i j \u2192 \ud835\udfda\n helper (equal _) = 1\u2082\n helper _ = 0\u2082\n\nswap : \u2200 {i} (x y : Fin i) \u2192 Fin i \u2192 Fin i\nswap x y z = case x == z 0: (case y == z 0: z 1: x) 1: y\n\ndata FinSum m n : Fin (m +\u2115 n) \u2192 \u2605\u2080 where\n bound : (x : Fin m) \u2192 FinSum m n (inject+ n x)\n free : (x : Fin n) \u2192 FinSum m n (raise m x)\n\nopen import Relation.Binary.PropositionalEquality\n\ncmp : \u2200 m n (x : Fin (m +\u2115 n)) \u2192 FinSum m n x\ncmp zero n x = free x\ncmp (suc m) n zero = bound zero\ncmp (suc m) n (suc x) with cmp m n x\ncmp (suc m) n (suc .(inject+ n x)) | bound x = bound (suc x)\ncmp (suc m) n (suc .(raise m x)) | free x = free x\n\nmax : \u2200 n \u2192 Fin (suc n)\nmax = from\u2115\n\n-- reverse x = n \u2238 (1 + x)\nreverse : \u2200 {n} \u2192 Fin n \u2192 Fin n\nreverse {suc n} zero = from\u2115 n\nreverse {suc n} (suc x) = inject\u2081 (reverse {n} x)\n\nopen import Data.Nat\nopen import Data.Nat.Properties\nopen import Data.Fin.Props renaming (reverse to reverse-old)\n\nreverse-from\u2115 : \u2200 n \u2192 reverse (from\u2115 n) \u2261 zero\nreverse-from\u2115 zero = refl\nreverse-from\u2115 (suc n) rewrite reverse-from\u2115 n = refl\n\nreverse-inject\u2081 : \u2200 {n} (x : Fin n) \u2192 reverse (inject\u2081 x) \u2261 suc (reverse x)\nreverse-inject\u2081 zero = refl\nreverse-inject\u2081 (suc x) rewrite reverse-inject\u2081 x = refl\n\n{-\nreverse-involutive : \u2200 {n} (x : Fin n) \u2192 reverse (reverse x) \u2261 x\nreverse-involutive zero = reverse-from\u2115 _\nreverse-involutive (suc x) rewrite reverse-inject\u2081 (reverse x) | reverse-involutive x = refl\n-}\n\nreverse-lem : \u2200 {n} (x : Fin n) \u2192 Fin\u25b9\u2115 (reverse x) \u2261 n \u2238 suc (Fin\u25b9\u2115 x)\nreverse-lem zero = to-from _\nreverse-lem (suc x) rewrite inject\u2081-lemma (reverse x) = reverse-lem x\n\nFin\u25b9\u2115-\u2115-lem : \u2200 {n} (x : Fin (suc n)) \u2192 Fin\u25b9\u2115 (n \u2115- x) \u2261 n \u2238 Fin\u25b9\u2115 x\nFin\u25b9\u2115-\u2115-lem zero = to-from _\nFin\u25b9\u2115-\u2115-lem {zero} (suc ())\nFin\u25b9\u2115-\u2115-lem {suc n} (suc x) = Fin\u25b9\u2115-\u2115-lem x\n\nreverse-old-lem : \u2200 {n} (x : Fin n) \u2192 Fin\u25b9\u2115 (reverse-old x) \u2261 n \u2238 suc (Fin\u25b9\u2115 x)\nreverse-old-lem {zero} ()\nreverse-old-lem {suc n} x rewrite inject\u2264-lemma (n \u2115- x) (n\u2238m\u2264n (Fin\u25b9\u2115 x) (suc n)) = Fin\u25b9\u2115-\u2115-lem x\n\ndata FinView {n} : Fin (suc n) \u2192 \u2605\u2080 where\n `from\u2115 : FinView (from\u2115 n)\n `inject\u2081 : \u2200 x \u2192 FinView (inject\u2081 x)\n\nsucFinView : \u2200 {n} {i : Fin (suc n)} \u2192 FinView i \u2192 FinView (suc i)\nsucFinView `from\u2115 = `from\u2115\nsucFinView (`inject\u2081 x) = `inject\u2081 (suc x)\n\nfinView : \u2200 {n} \u2192 (i : Fin (suc n)) \u2192 FinView i\nfinView {zero} zero = `from\u2115\nfinView {suc n} zero = `inject\u2081 zero\nfinView {suc n} (suc i) = sucFinView (finView i)\nfinView {zero} (suc ())\n\nmodule Modulo where\n modq : \u2200 {q} \u2192 Fin (suc q) \u2192 Maybe (Fin q)\n modq {zero} _ = nothing\n modq {suc q} zero = just zero\n modq {suc q} (suc x) = map? suc (modq x)\n\n modq-inj : \u2200 {q} (i j : Fin (suc q)) \u2192 modq i \u2261 modq j \u2192 i \u2261 j\n modq-inj {zero} zero zero eq = refl\n modq-inj {zero} zero (suc ()) eq\n modq-inj {zero} (suc ()) zero eq\n modq-inj {zero} (suc ()) (suc ()) eq\n modq-inj {suc q} zero zero eq = refl\n modq-inj {suc q} zero (suc j) eq with modq j\n modq-inj {suc q} zero (suc j) () | nothing\n modq-inj {suc q} zero (suc j) () | just j'\n modq-inj {suc q} (suc i) zero eq with modq i\n modq-inj {suc q} (suc i) zero () | just x\n modq-inj {suc q} (suc i) zero () | nothing\n modq-inj {suc q} (suc i) (suc j) eq with modq i | modq j | modq-inj i j\n modq-inj {suc q} (suc i) (suc j) eq | just x | just x\u2081 | p = cong suc (p (cong just (suc-injective (just-injective eq))))\n modq-inj {suc q} (suc i) (suc j) () | just x | nothing | p\n modq-inj {suc q} (suc i) (suc j) () | nothing | just x | p\n modq-inj {suc q} (suc i) (suc j) eq | nothing | nothing | p = cong suc (p refl)\n\n modq\u2032 : \u2200 {q} \u2192 Fin (suc q) \u2192 Fin (suc q)\n modq\u2032 {zero} _ = zero\n modq\u2032 {suc q} zero = suc zero\n modq\u2032 {suc q} (suc x) = lift 1 suc (modq\u2032 x)\n\n modqq : \u2200 {q} \u2192 Fin q \u2192 Fin q\n modqq {zero} x = x\n modqq {suc q} x = modq\u2032 x\n\n -- Maybe (Fin n) \u2245 Fin (suc n)\n\n modq\u2032\u2032 : \u2200 {q} \u2192 Fin (suc q) \u2192 Maybe (Fin q)\n modq\u2032\u2032 x with modq\u2032 x\n ... | zero = nothing\n ... | suc y = just y\n\n zero\u2203 : \u2200 {q} \u2192 Fin q \u2192 Fin q\n zero\u2203 {zero} ()\n zero\u2203 {suc q} _ = zero\n\n sucmod : \u2200 {q} \u2192 Fin q \u2192 Fin q\n sucmod x with modq (suc x)\n ... | nothing = zero\u2203 x\n ... | just y = y\n\n modq-suc : \u2200 {q} (i j : Fin q) \u2192 modq (suc i) \u2262 just (zero\u2203 j)\n modq-suc {zero} () () eq\n modq-suc {suc q} i j eq with modq i\n modq-suc {suc q} i j () | just x\n modq-suc {suc q} i j () | nothing\n\n sucmod-inj : \u2200 {q}{i j : Fin q} \u2192 sucmod i \u2261 sucmod j \u2192 i \u2261 j\n sucmod-inj {i = i} {j} eq with modq (suc i) | modq (suc j) | modq-inj (suc i) (suc j) | modq-suc i j | modq-suc j i\n sucmod-inj eq | just _ | just _ | p | _ | _ = suc-injective (p (cong just eq))\n sucmod-inj eq | nothing | nothing | p | _ | _ = suc-injective (p refl)\n sucmod-inj eq | just _ | nothing | _ | p | _ = \ud835\udfd8-elim (p (cong Maybe.just eq))\n sucmod-inj eq | nothing | just _ | _ | _ | p = \ud835\udfd8-elim (p (cong Maybe.just (sym eq)))\n\n modq-from\u2115 : \u2200 q \u2192 modq (from\u2115 q) \u2261 nothing\n modq-from\u2115 zero = refl\n modq-from\u2115 (suc q) rewrite modq-from\u2115 q = refl\n\n modq-inject\u2081 : \u2200 {q} (i : Fin q) \u2192 modq (inject\u2081 i) \u2261 just i\n modq-inject\u2081 zero = refl\n modq-inject\u2081 (suc i) rewrite modq-inject\u2081 i = refl\n\n sucmod-from\u2115 : \u2200 q \u2192 sucmod (from\u2115 q) \u2261 zero\n sucmod-from\u2115 q rewrite modq-from\u2115 q = refl\n\n sucmod-inject\u2081 : \u2200 {n} (i : Fin n) \u2192 sucmod (inject\u2081 i) \u2261 suc i\n sucmod-inject\u2081 i rewrite modq-inject\u2081 i = refl\n\n lem-inject\u2081 : \u2200 {n a} {A : \u2605 a} (i : Fin n) (xs : Vec A n) x \u2192 lookup (inject\u2081 i) (xs \u2237\u02b3 x) \u2261 lookup i xs\n lem-inject\u2081 zero (x\u2080 \u2237 xs) x\u2081 = refl\n lem-inject\u2081 (suc i) (x\u2080 \u2237 xs) x\u2081 = lem-inject\u2081 i xs x\u2081\n\n lem-from\u2115 : \u2200 {n a} {A : \u2605 a} (xs : Vec A n) x \u2192 lookup (from\u2115 n) (xs \u2237\u02b3 x) \u2261 x\n lem-from\u2115 {zero} [] x = refl\n lem-from\u2115 {suc n} (_ \u2237 xs) x = lem-from\u2115 xs x\n\n lookup-sucmod : \u2200 {n a} {A : \u2605 a} (i : Fin (suc n)) (x : A) xs\n \u2192 lookup i (xs \u2237\u02b3 x) \u2261 lookup (sucmod i) (x \u2237 xs)\n lookup-sucmod i x xs with finView i\n lookup-sucmod {n} .(from\u2115 n) x xs | `from\u2115 rewrite sucmod-from\u2115 n = lem-from\u2115 xs x\n lookup-sucmod .(inject\u2081 x) x\u2081 xs | `inject\u2081 x rewrite sucmod-inject\u2081 x = lem-inject\u2081 x xs x\u2081\n\n lookup-map : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b} (f : A \u2192 B) i (xs : Vec A n)\n \u2192 lookup i (vmap f xs) \u2261 f (lookup i xs)\n lookup-map f zero (x \u2237 xs) = refl\n lookup-map f (suc i) (x \u2237 xs) = lookup-map f i xs\n\n vec\u2257\u21d2\u2261 : \u2200 {n a} {A : \u2605 a} (xs ys : Vec A n) \u2192 flip lookup xs \u2257 flip lookup ys \u2192 xs \u2261 ys\n vec\u2257\u21d2\u2261 [] [] _ = refl\n vec\u2257\u21d2\u2261 (x \u2237 xs) (y \u2237 ys) p rewrite vec\u2257\u21d2\u2261 xs ys (p \u2218 suc) | p zero = refl\n\n -- most likely this is subsumed by the StableUnderInjection parts\n private\n module Unused where\n lookup-sucmod-rot\u2081 : \u2200 {n a} {A : \u2605 a} (i : Fin n) (xs : Vec A n)\n \u2192 lookup i (rot\u2081 xs) \u2261 lookup (sucmod i) xs\n lookup-sucmod-rot\u2081 {zero} () xs\n lookup-sucmod-rot\u2081 {suc n} i (x \u2237 xs) = lookup-sucmod i x xs\n\n lookup-rot\u2081-allFin : \u2200 {n} i \u2192 lookup i (rot\u2081 (allFin n)) \u2261 lookup i (vmap sucmod (allFin n))\n lookup-rot\u2081-allFin {n} i rewrite lookup-sucmod-rot\u2081 i (allFin _)\n | Vec.lookup-allFin (sucmod i)\n | lookup-map sucmod i (allFin n)\n | Vec.lookup\u2218tabulate id i\n = refl\n\n rot\u2081-map-sucmod : \u2200 n \u2192 rot\u2081 (allFin n) \u2261 vmap sucmod (allFin n)\n rot\u2081-map-sucmod _ = vec\u2257\u21d2\u2261 _ _ lookup-rot\u2081-allFin\n\n {-\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","old_contents":"-- NOTE with-K\nmodule Data.Fin.NP where\n\nopen import Type hiding (\u2605)\nopen import Function\nopen import Data.Zero\nopen import Data.Fin public renaming (to\u2115 to Fin\u25b9\u2115)\nopen import Data.Nat.NP using (\u2115; zero; suc; _<=_; module \u2115\u00b0) renaming (_+_ to _+\u2115_)\nopen import Data.Bool\nimport Data.Vec.NP as Vec\nopen Vec using (Vec; []; _\u2237_; _\u2237\u02b3_; allFin; lookup; rot\u2081) renaming (map to vmap)\nimport Data.Vec.Properties as Vec\nopen import Data.Maybe.NP\nopen import Data.Sum as Sum\nopen import Relation.Binary.PropositionalEquality as \u2261\n\nsuc-injective : \u2200 {m}{i j : Fin m} \u2192 Fin.suc i \u2261 suc j \u2192 i \u2261 j\nsuc-injective refl = refl\n\n_+\u2032_ : \u2200 {m n} (x : Fin m) (y : Fin n) \u2192 Fin (m +\u2115 n)\n_+\u2032_ {suc m} {n} zero y rewrite \u2115\u00b0.+-comm (suc m) n = inject+ _ y\nsuc x +\u2032 y = suc (x +\u2032 y)\n\n_==_ : \u2200 {n} (x y : Fin n) \u2192 Bool\nx == y = helper (compare x y) where\n helper : \u2200 {n} {i j : Fin n} \u2192 Ordering i j \u2192 Bool\n helper (equal _) = true\n helper _ = false\n\nswap : \u2200 {i} (x y : Fin i) \u2192 Fin i \u2192 Fin i\nswap x y z = if x == z then y else if y == z then x else z\n\ndata FinSum m n : Fin (m +\u2115 n) \u2192 \u2605\u2080 where\n bound : (x : Fin m) \u2192 FinSum m n (inject+ n x)\n free : (x : Fin n) \u2192 FinSum m n (raise m x)\n\nopen import Relation.Binary.PropositionalEquality\n\ncmp : \u2200 m n (x : Fin (m +\u2115 n)) \u2192 FinSum m n x\ncmp zero n x = free x\ncmp (suc m) n zero = bound zero\ncmp (suc m) n (suc x) with cmp m n x\ncmp (suc m) n (suc .(inject+ n x)) | bound x = bound (suc x)\ncmp (suc m) n (suc .(raise m x)) | free x = free x\n\nmax : \u2200 n \u2192 Fin (suc n)\nmax = from\u2115\n\n-- reverse x = n \u2238 (1 + x)\nreverse : \u2200 {n} \u2192 Fin n \u2192 Fin n\nreverse {suc n} zero = from\u2115 n\nreverse {suc n} (suc x) = inject\u2081 (reverse {n} x)\n\nopen import Data.Nat\nopen import Data.Nat.Properties\nopen import Data.Fin.Props renaming (reverse to reverse-old)\n\nreverse-from\u2115 : \u2200 n \u2192 reverse (from\u2115 n) \u2261 zero\nreverse-from\u2115 zero = refl\nreverse-from\u2115 (suc n) rewrite reverse-from\u2115 n = refl\n\nreverse-inject\u2081 : \u2200 {n} (x : Fin n) \u2192 reverse (inject\u2081 x) \u2261 suc (reverse x)\nreverse-inject\u2081 zero = refl\nreverse-inject\u2081 (suc x) rewrite reverse-inject\u2081 x = refl\n\n{-\nreverse-involutive : \u2200 {n} (x : Fin n) \u2192 reverse (reverse x) \u2261 x\nreverse-involutive zero = reverse-from\u2115 _\nreverse-involutive (suc x) rewrite reverse-inject\u2081 (reverse x) | reverse-involutive x = refl\n-}\n\nreverse-lem : \u2200 {n} (x : Fin n) \u2192 Fin\u25b9\u2115 (reverse x) \u2261 n \u2238 suc (Fin\u25b9\u2115 x)\nreverse-lem zero = to-from _\nreverse-lem (suc x) rewrite inject\u2081-lemma (reverse x) = reverse-lem x\n\nFin\u25b9\u2115-\u2115-lem : \u2200 {n} (x : Fin (suc n)) \u2192 Fin\u25b9\u2115 (n \u2115- x) \u2261 n \u2238 Fin\u25b9\u2115 x\nFin\u25b9\u2115-\u2115-lem zero = to-from _\nFin\u25b9\u2115-\u2115-lem {zero} (suc ())\nFin\u25b9\u2115-\u2115-lem {suc n} (suc x) = Fin\u25b9\u2115-\u2115-lem x\n\nreverse-old-lem : \u2200 {n} (x : Fin n) \u2192 Fin\u25b9\u2115 (reverse-old x) \u2261 n \u2238 suc (Fin\u25b9\u2115 x)\nreverse-old-lem {zero} ()\nreverse-old-lem {suc n} x rewrite inject\u2264-lemma (n \u2115- x) (n\u2238m\u2264n (Fin\u25b9\u2115 x) (suc n)) = Fin\u25b9\u2115-\u2115-lem x\n\ndata FinView {n} : Fin (suc n) \u2192 \u2605\u2080 where\n `from\u2115 : FinView (from\u2115 n)\n `inject\u2081 : \u2200 x \u2192 FinView (inject\u2081 x)\n\nsucFinView : \u2200 {n} {i : Fin (suc n)} \u2192 FinView i \u2192 FinView (suc i)\nsucFinView `from\u2115 = `from\u2115\nsucFinView (`inject\u2081 x) = `inject\u2081 (suc x)\n\nfinView : \u2200 {n} \u2192 (i : Fin (suc n)) \u2192 FinView i\nfinView {zero} zero = `from\u2115\nfinView {suc n} zero = `inject\u2081 zero\nfinView {suc n} (suc i) = sucFinView (finView i)\nfinView {zero} (suc ())\n\nmodule Modulo where\n modq : \u2200 {q} \u2192 Fin (suc q) \u2192 Maybe (Fin q)\n modq {zero} _ = nothing\n modq {suc q} zero = just zero\n modq {suc q} (suc x) = map? suc (modq x)\n\n modq-inj : \u2200 {q} (i j : Fin (suc q)) \u2192 modq i \u2261 modq j \u2192 i \u2261 j\n modq-inj {zero} zero zero eq = refl\n modq-inj {zero} zero (suc ()) eq\n modq-inj {zero} (suc ()) zero eq\n modq-inj {zero} (suc ()) (suc ()) eq\n modq-inj {suc q} zero zero eq = refl\n modq-inj {suc q} zero (suc j) eq with modq j\n modq-inj {suc q} zero (suc j) () | nothing\n modq-inj {suc q} zero (suc j) () | just j'\n modq-inj {suc q} (suc i) zero eq with modq i\n modq-inj {suc q} (suc i) zero () | just x\n modq-inj {suc q} (suc i) zero () | nothing\n modq-inj {suc q} (suc i) (suc j) eq with modq i | modq j | modq-inj i j\n modq-inj {suc q} (suc i) (suc j) eq | just x | just x\u2081 | p = cong suc (p (cong just (suc-injective (just-injective eq))))\n modq-inj {suc q} (suc i) (suc j) () | just x | nothing | p\n modq-inj {suc q} (suc i) (suc j) () | nothing | just x | p\n modq-inj {suc q} (suc i) (suc j) eq | nothing | nothing | p = cong suc (p refl)\n\n modq\u2032 : \u2200 {q} \u2192 Fin (suc q) \u2192 Fin (suc q)\n modq\u2032 {zero} _ = zero\n modq\u2032 {suc q} zero = suc zero\n modq\u2032 {suc q} (suc x) = lift 1 suc (modq\u2032 x)\n\n modqq : \u2200 {q} \u2192 Fin q \u2192 Fin q\n modqq {zero} x = x\n modqq {suc q} x = modq\u2032 x\n\n -- Maybe (Fin n) \u2245 Fin (suc n)\n\n modq\u2032\u2032 : \u2200 {q} \u2192 Fin (suc q) \u2192 Maybe (Fin q)\n modq\u2032\u2032 x with modq\u2032 x\n ... | zero = nothing\n ... | suc y = just y\n\n zero\u2203 : \u2200 {q} \u2192 Fin q \u2192 Fin q\n zero\u2203 {zero} ()\n zero\u2203 {suc q} _ = zero\n\n sucmod : \u2200 {q} \u2192 Fin q \u2192 Fin q\n sucmod x with modq (suc x)\n ... | nothing = zero\u2203 x\n ... | just y = y\n\n modq-suc : \u2200 {q} (i j : Fin q) \u2192 modq (suc i) \u2262 just (zero\u2203 j)\n modq-suc {zero} () () eq\n modq-suc {suc q} i j eq with modq i\n modq-suc {suc q} i j () | just x\n modq-suc {suc q} i j () | nothing\n\n sucmod-inj : \u2200 {q}{i j : Fin q} \u2192 sucmod i \u2261 sucmod j \u2192 i \u2261 j\n sucmod-inj {i = i} {j} eq with modq (suc i) | modq (suc j) | modq-inj (suc i) (suc j) | modq-suc i j | modq-suc j i\n sucmod-inj eq | just _ | just _ | p | _ | _ = suc-injective (p (cong just eq))\n sucmod-inj eq | nothing | nothing | p | _ | _ = suc-injective (p refl)\n sucmod-inj eq | just _ | nothing | _ | p | _ = \ud835\udfd8-elim (p (cong Maybe.just eq))\n sucmod-inj eq | nothing | just _ | _ | _ | p = \ud835\udfd8-elim (p (cong Maybe.just (sym eq)))\n\n modq-from\u2115 : \u2200 q \u2192 modq (from\u2115 q) \u2261 nothing\n modq-from\u2115 zero = refl\n modq-from\u2115 (suc q) rewrite modq-from\u2115 q = refl\n\n modq-inject\u2081 : \u2200 {q} (i : Fin q) \u2192 modq (inject\u2081 i) \u2261 just i\n modq-inject\u2081 zero = refl\n modq-inject\u2081 (suc i) rewrite modq-inject\u2081 i = refl\n\n sucmod-from\u2115 : \u2200 q \u2192 sucmod (from\u2115 q) \u2261 zero\n sucmod-from\u2115 q rewrite modq-from\u2115 q = refl\n\n sucmod-inject\u2081 : \u2200 {n} (i : Fin n) \u2192 sucmod (inject\u2081 i) \u2261 suc i\n sucmod-inject\u2081 i rewrite modq-inject\u2081 i = refl\n\n lem-inject\u2081 : \u2200 {n a} {A : \u2605 a} (i : Fin n) (xs : Vec A n) x \u2192 lookup (inject\u2081 i) (xs \u2237\u02b3 x) \u2261 lookup i xs\n lem-inject\u2081 zero (x\u2080 \u2237 xs) x\u2081 = refl\n lem-inject\u2081 (suc i) (x\u2080 \u2237 xs) x\u2081 = lem-inject\u2081 i xs x\u2081\n\n lem-from\u2115 : \u2200 {n a} {A : \u2605 a} (xs : Vec A n) x \u2192 lookup (from\u2115 n) (xs \u2237\u02b3 x) \u2261 x\n lem-from\u2115 {zero} [] x = refl\n lem-from\u2115 {suc n} (_ \u2237 xs) x = lem-from\u2115 xs x\n\n lookup-sucmod : \u2200 {n a} {A : \u2605 a} (i : Fin (suc n)) (x : A) xs\n \u2192 lookup i (xs \u2237\u02b3 x) \u2261 lookup (sucmod i) (x \u2237 xs)\n lookup-sucmod i x xs with finView i\n lookup-sucmod {n} .(from\u2115 n) x xs | `from\u2115 rewrite sucmod-from\u2115 n = lem-from\u2115 xs x\n lookup-sucmod .(inject\u2081 x) x\u2081 xs | `inject\u2081 x rewrite sucmod-inject\u2081 x = lem-inject\u2081 x xs x\u2081\n\n lookup-map : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b} (f : A \u2192 B) i (xs : Vec A n)\n \u2192 lookup i (vmap f xs) \u2261 f (lookup i xs)\n lookup-map f zero (x \u2237 xs) = refl\n lookup-map f (suc i) (x \u2237 xs) = lookup-map f i xs\n\n vec\u2257\u21d2\u2261 : \u2200 {n a} {A : \u2605 a} (xs ys : Vec A n) \u2192 flip lookup xs \u2257 flip lookup ys \u2192 xs \u2261 ys\n vec\u2257\u21d2\u2261 [] [] _ = refl\n vec\u2257\u21d2\u2261 (x \u2237 xs) (y \u2237 ys) p rewrite vec\u2257\u21d2\u2261 xs ys (p \u2218 suc) | p zero = refl\n\n -- most likely this is subsumed by the StableUnderInjection parts\n private\n module Unused where\n lookup-sucmod-rot\u2081 : \u2200 {n a} {A : \u2605 a} (i : Fin n) (xs : Vec A n)\n \u2192 lookup i (rot\u2081 xs) \u2261 lookup (sucmod i) xs\n lookup-sucmod-rot\u2081 {zero} () xs\n lookup-sucmod-rot\u2081 {suc n} i (x \u2237 xs) = lookup-sucmod i x xs\n\n lookup-rot\u2081-allFin : \u2200 {n} i \u2192 lookup i (rot\u2081 (allFin n)) \u2261 lookup i (vmap sucmod (allFin n))\n lookup-rot\u2081-allFin {n} i rewrite lookup-sucmod-rot\u2081 i (allFin _)\n | Vec.lookup-allFin (sucmod i)\n | lookup-map sucmod i (allFin n)\n | Vec.lookup\u2218tabulate id i\n = refl\n\n rot\u2081-map-sucmod : \u2200 n \u2192 rot\u2081 (allFin n) \u2261 vmap sucmod (allFin n)\n rot\u2081-map-sucmod _ = vec\u2257\u21d2\u2261 _ _ lookup-rot\u2081-allFin\n\n {-\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"91b84d72680d7c2c8cecec7fa7f3c4db8507bdfc","subject":"Updated paper's title.","message":"Updated paper's title.\n\nIgnore-this: 2199f50d6284b89c6e8457cec04013a8\n\ndarcs-hash:20110430153854-3bd4e-41252e26df59f2df8a4dd02ba87fc2522d3b58c0.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"README.agda","new_file":"README.agda","new_contents":"------------------------------------------------------------------------------\n-- FOT (First Order Theories)\n------------------------------------------------------------------------------\n\n-- Code accompanying the paper \"Combining Interactive and Automatic\n-- Theorem Proving for Reasoning about Functional Programs\" by Ana\n-- Bove, Peter Dybjer, and Andr\u00e9s Sicard-Ram\u00edrez.\n\n------------------------------------------------------------------------------\n\nmodule README where\n\n------------------------------------------------------------------------------\n-- Description\n------------------------------------------------------------------------------\n\n-- Examples of the formalization of first order theories showing the\n-- combination of interactive proofs with automatics proofs carried\n-- out by first order automatic theorem provers (ATPs).\n\n------------------------------------------------------------------------------\n-- Prerequisites\n------------------------------------------------------------------------------\n\n-- You can download the agda2atp tool (described in above paper) using\n-- darcs with the following command:\n\n-- $ darcs get http:\/\/patch-tag.com\/r\/asr\/agda2atp\n\n-- The agda2atp tool and the FOT code require a modified version of\n-- Agda. See agda2atp\/README.txt for the related instructions.\n\n------------------------------------------------------------------------------\n-- Use\n------------------------------------------------------------------------------\n\n-- Let's suppose you want to use the Metis ATP to prove the group\n-- theory properties stated in the module\n-- GroupTheory.PropertiesATP. First, you should type-check the module using\n-- Agda\n\n-- $ agda -isrc src\/GroupTheory\/PropertiesATP.agda\n\n-- Second, you call the agda2tool using the Metis ATP\n\n-- $ agda2atp -isrc --atp=metis src\/GroupTheory\/PropertiesATP.agda\n\n------------------------------------------------------------------------------\n-- First order theories\n------------------------------------------------------------------------------\n\n-- Conventions\n\n-- The following modules show the formalization of some first order\n-- theories. If the module's name ends in 'I' the module contains\n-- interactive proofs, if it ends in 'ATP' the module contains\n-- combined proofs, otherwise the module contains definions and\/or\n-- interactive proofs that are used by the interactive and combined\n-- proofs.\n\n------------------------------------------------------------------------------\n-- 1. Predicate logic\n\n-- 1.1 Definition of the connectives and quantifiers\nopen import Common.LogicalConstants\nopen import PredicateLogic.Constants\n\n-- 1.2 The law of the excluded middle\nopen import PredicateLogic.ClassicalATP\n\n-- 1.3 Non-empty domains\nopen import PredicateLogic.NonEmptyDomain.TheoremsATP\nopen import PredicateLogic.NonEmptyDomain.TheoremsI\n\n-- 1.4 Logical schemas\nopen import PredicateLogic.SchemasATP\n\n-- 1.5 Propositional logic theorems\nopen import PredicateLogic.Propositional.TheoremsATP\nopen import PredicateLogic.Propositional.TheoremsI\n\n-- 1.6 Predicate logic theorems\nopen import PredicateLogic.TheoremsATP\nopen import PredicateLogic.TheoremsI\n\n------------------------------------------------------------------------------\n-- 2. Equality\n\n-- 2.1 Definition of equality and some properties about it\nopen import Common.Relation.Binary.PropositionalEquality\n\n-- 2.2 The equality reasoning\nopen import Common.Relation.Binary.PreorderReasoning\n\n------------------------------------------------------------------------------\n-- 3. Group theory\n\n-- 3.1 The axioms\nopen import GroupTheory.Base\n\n-- 3.2 Basic properties\nopen import GroupTheory.PropertiesATP\nopen import GroupTheory.PropertiesI\n\n-- 3.3 Commutator properties\nopen import GroupTheory.Commutator.PropertiesATP\nopen import GroupTheory.Commutator.PropertiesI\n\n-- 3.4 Abelian groups\nopen import GroupTheory.AbelianGroup.PropertiesATP\n\n------------------------------------------------------------------------------\n-- 4. Stanovsk\u00fd example (distributive laws on a binary operation)\n\n-- We prove the proposition 2 (task B) of [1]. Let _\u00b7_ be a\n-- left-associative binary operation, the task B consist in given the\n-- left and right distributive axioms\n\n-- \u2200 x y z \u2192 x \u2219 (y \u2219 z) \u2261 (x \u2219 y) \u2219 (x \u2219 z)\n-- \u2200 x y z \u2192 (x \u2219 y) \u2219 z \u2261 (x \u2219 z) \u2219 (y \u2219 z)\n\n-- to prove the theorem\n\n-- \u2200 u x y z \u2192 (x \u2219 y \u2219 (z \u2219 u)) \u2219\n-- (( x \u2219 y \u2219 ( z \u2219 u)) \u2219 (x \u2219 z \u2219 (y \u2219 u))) \u2261\n-- x \u2219 z \u2219 (y \u2219 u)\n\n-- [1] David Stanovsk\u00fd. Distributive groupoids are symmetrical-by-medial:\n-- An elementary proof. Commentations Mathematicae Universitatis\n-- Carolinae, 49(4):541\u2013546, 2008.\n\n-- 4.1 The ATPs could not prove the theorem\nopen import DistributiveLaws.TaskB-ATP\n\n-- 4.2 The interactive and combined proofs\nopen import DistributiveLaws.TaskB-HalvedStepsATP\nopen import DistributiveLaws.TaskB-I\nopen import DistributiveLaws.TaskB-TopDownATP\n\n------------------------------------------------------------------------------\n-- 5. Peano arithmetic (PA)\n\n-- We write two formalizations of PA. In the axiomatic formalization\n-- we use Agda postulates for Peano's axioms. In the inductive\n-- formalization, we use Agda data types and primitive recursive\n-- functions.\n\n-- 5.1 Axiomatic PA\n-- 5.1.1 The axioms\nopen import PA.Axiomatic.Base\n\n-- 5.1.2 Some properties\nopen import PA.Axiomatic.PropertiesATP\nopen import PA.Axiomatic.PropertiesI\n\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityATP\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityI\n\n-- 5.2. Inductive PA\n-- 5.2.1 Some properties\nopen import PA.Inductive.Properties\nopen import PA.Inductive.PropertiesATP\nopen import PA.Inductive.PropertiesI\n\nopen import PA.Inductive.PropertiesByInduction\nopen import PA.Inductive.PropertiesByInductionATP\nopen import PA.Inductive.PropertiesByInductionI\n\n------------------------------------------------------------------------------\n-- 6. FOTC\n\n-- Formalization of (a version of) Azcel's First Order Theory of Combinators.\n\n-- 6.1 The axioms\nopen import FOTC.Base\n\n-- 6.2 Booleans\n\n-- 6.2.2 The axioms\nopen import FOTC.Data.Bool\n\n-- 6.2.3 Properties\nopen import FOTC.Data.Bool.PropertiesATP\nopen import FOTC.Data.Bool.PropertiesI\n\n-- 6.3. Natural numbers\n\n-- 6.3.1 The axioms\nopen import FOTC.Data.Nat\n\n-- 6.3.2 Properties\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Data.Nat.PropertiesI\n\nopen import FOTC.Data.Nat.PropertiesByInductionATP\nopen import FOTC.Data.Nat.PropertiesByInductionI\n\n-- 6.3.3 Divisibility relation\nopen import FOTC.Data.Nat.Divisibility.PropertiesATP\nopen import FOTC.Data.Nat.Divisibility.PropertiesI\n\n-- 6.3.4 Induction\nopen import FOTC.Data.Nat.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.Nat.Induction.NonAcc.LexicographicI\n\n-- 6.3.5 Inequalites\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.Inequalities.PropertiesI\n\n-- 6.3.6 Unary numbers\nopen import FOTC.Data.Nat.UnaryNumbers.IsN-ATP\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\n\n-- 6.4 Lists\n\n-- 6.4.1 The axioms\nopen import FOTC.Data.List\n\n-- 6.4.2 Properties\nopen import FOTC.Data.List.PropertiesATP\nopen import FOTC.Data.List.PropertiesI\n\n-- 6.4.3 Well-founded induction\nopen import FOTC.Data.List.LT-Cons.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.List.LT-Cons.PropertiesI\nopen import FOTC.Data.List.LT-Length.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.List.LT-Length.PropertiesI\n\n-- 6.4.4 Lists of natural numbers\nopen import FOTC.Data.Nat.List.PropertiesATP\nopen import FOTC.Data.Nat.List.PropertiesI\n\n-- 6.5 Programs\n\n-- 6.5.1 The Collatz function: A function without a termination proof\nopen import FOTC.Program.Collatz.PropertiesATP\nopen import FOTC.Program.Collatz.PropertiesI\n\n-- 6.5.2 The GCD algorithm: A non-structurally recursive algorithm\nopen import FOTC.Program.GCD.ProofSpecificationATP\nopen import FOTC.Program.GCD.ProofSpecificationI\n\n-- 6.5.3 The McCarthy 91 function: A function with nested recursion\nopen import FOTC.Program.McCarthy91.Properties.MainATP\n\n-- 6.5.4 The mirror function: A function with higher-order recursion\nopen import FOTC.Program.Mirror.PropertiesATP\nopen import FOTC.Program.Mirror.PropertiesI\n\n-- 6.5.5 Burstall's sort list algorithm: A structurally recursive algorithm\nopen import FOTC.Program.SortList.ProofSpecificationATP\nopen import FOTC.Program.SortList.ProofSpecificationI\n\n------------------------------------------------------------------------------\n-- ATPs failures\n------------------------------------------------------------------------------\n\n-- The ATPs (E, Equinox, Metis and Vampire) could not prove some\n-- theorems.\n\n-- This module was imported in the Stanovsk\u00fd example\n-- open import DistributiveLaws.TaskB-ATP\nopen import LTC-PCF.Program.GCD.EquationsATP\nopen import PA.Axiomatic.PropertiesATP\n\n------------------------------------------------------------------------------\n-- Agsy examples\n------------------------------------------------------------------------------\n\n-- We cannot import the Agsy examples because some modules contain\n-- unsolved metas, therefore see src\/Agsy\/README.txt\n\n------------------------------------------------------------------------------\n-- Other theories\n------------------------------------------------------------------------------\n\n-- 1. LTC-PCF\n-- Formalization of a version of Azcel's Logical Theory of Constructions.\n-- N.B. This was the theory shown in our PLPV'09 paper.\n\n-- 1.1. The axioms\nopen import LTC-PCF.Base\n\n-- 1.2 Natural numberes\n\n-- 1.2.1 The axioms\nopen import LTC-PCF.Data.Nat\n\n-- 1.2.2 Properties\nopen import LTC-PCF.Data.Nat.PropertiesATP\nopen import LTC-PCF.Data.Nat.PropertiesI\n\n-- 1.2.3 Divisibility relation\nopen import LTC-PCF.Data.Nat.Divisibility.Properties\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesATP\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesI\n\n-- 1.2.4 Induction\nopen import LTC-PCF.Data.Nat.Induction.NonAcc.LexicographicI\nopen import LTC-PCF.Data.Nat.Induction.NonAcc.WellFoundedInductionI\n\n-- 1.2.5 Inequalites\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesATP\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesI\n\n-- 1.2.6 The recursive operator\nopen import LTC-PCF.Data.Nat.Rec.EquationsATP\nopen import LTC-PCF.Data.Nat.Rec.EquationsI\n\n-- 1.3 Programs\n\n-- 1.3.1 The division algorithm: A non-structurally recursive algorithm\nopen import LTC-PCF.Program.Division.ProofSpecificationATP\nopen import LTC-PCF.Program.Division.ProofSpecificationI\n\n-- 1.3.2 The GCD algorithm: A non-structurally recursive algorithm\nopen import LTC-PCF.Program.GCD.ProofSpecificationATP\nopen import LTC-PCF.Program.GCD.ProofSpecificationI\n","old_contents":"------------------------------------------------------------------------------\n-- FOT (First Order Theories)\n------------------------------------------------------------------------------\n\n-- Code accompanying the paper \"Combining Automatic and Interactive\n-- Proof in First Order Theories of Combinators\" by Ana Bove,\n-- Peter Dybjer, and Andr\u00e9s Sicard-Ram\u00edrez.\n\n------------------------------------------------------------------------------\n\nmodule README where\n\n------------------------------------------------------------------------------\n-- Description\n------------------------------------------------------------------------------\n\n-- Examples of the formalization of first order theories showing the\n-- combination of interactive proofs with automatics proofs carried\n-- out by first order automatic theorem provers (ATPs).\n\n------------------------------------------------------------------------------\n-- Prerequisites\n------------------------------------------------------------------------------\n\n-- You can download the agda2atp tool (described in above paper) using\n-- darcs with the following command:\n\n-- $ darcs get http:\/\/patch-tag.com\/r\/asr\/agda2atp\n\n-- The agda2atp tool and the FOT code require a modified version of\n-- Agda. See agda2atp\/README.txt for the related instructions.\n\n------------------------------------------------------------------------------\n-- Use\n------------------------------------------------------------------------------\n\n-- Let's suppose you want to use the Metis ATP to prove the group\n-- theory properties stated in the module\n-- GroupTheory.PropertiesATP. First, you should type-check the module using\n-- Agda\n\n-- $ agda -isrc src\/GroupTheory\/PropertiesATP.agda\n\n-- Second, you call the agda2tool using the Metis ATP\n\n-- $ agda2atp -isrc --atp=metis src\/GroupTheory\/PropertiesATP.agda\n\n------------------------------------------------------------------------------\n-- First order theories\n------------------------------------------------------------------------------\n\n-- Conventions\n\n-- The following modules show the formalization of some first order\n-- theories. If the module's name ends in 'I' the module contains\n-- interactive proofs, if it ends in 'ATP' the module contains\n-- combined proofs, otherwise the module contains definions and\/or\n-- interactive proofs that are used by the interactive and combined\n-- proofs.\n\n------------------------------------------------------------------------------\n-- 1. Predicate logic\n\n-- 1.1 Definition of the connectives and quantifiers\nopen import Common.LogicalConstants\nopen import PredicateLogic.Constants\n\n-- 1.2 The law of the excluded middle\nopen import PredicateLogic.ClassicalATP\n\n-- 1.3 Non-empty domains\nopen import PredicateLogic.NonEmptyDomain.TheoremsATP\nopen import PredicateLogic.NonEmptyDomain.TheoremsI\n\n-- 1.4 Logical schemas\nopen import PredicateLogic.SchemasATP\n\n-- 1.5 Propositional logic theorems\nopen import PredicateLogic.Propositional.TheoremsATP\nopen import PredicateLogic.Propositional.TheoremsI\n\n-- 1.6 Predicate logic theorems\nopen import PredicateLogic.TheoremsATP\nopen import PredicateLogic.TheoremsI\n\n------------------------------------------------------------------------------\n-- 2. Equality\n\n-- 2.1 Definition of equality and some properties about it\nopen import Common.Relation.Binary.PropositionalEquality\n\n-- 2.2 The equality reasoning\nopen import Common.Relation.Binary.PreorderReasoning\n\n------------------------------------------------------------------------------\n-- 3. Group theory\n\n-- 3.1 The axioms\nopen import GroupTheory.Base\n\n-- 3.2 Basic properties\nopen import GroupTheory.PropertiesATP\nopen import GroupTheory.PropertiesI\n\n-- 3.3 Commutator properties\nopen import GroupTheory.Commutator.PropertiesATP\nopen import GroupTheory.Commutator.PropertiesI\n\n-- 3.4 Abelian groups\nopen import GroupTheory.AbelianGroup.PropertiesATP\n\n------------------------------------------------------------------------------\n-- 4. Stanovsk\u00fd example (distributive laws on a binary operation)\n\n-- We prove the proposition 2 (task B) of [1]. Let _\u00b7_ be a\n-- left-associative binary operation, the task B consist in given the\n-- left and right distributive axioms\n\n-- \u2200 x y z \u2192 x \u2219 (y \u2219 z) \u2261 (x \u2219 y) \u2219 (x \u2219 z)\n-- \u2200 x y z \u2192 (x \u2219 y) \u2219 z \u2261 (x \u2219 z) \u2219 (y \u2219 z)\n\n-- to prove the theorem\n\n-- \u2200 u x y z \u2192 (x \u2219 y \u2219 (z \u2219 u)) \u2219\n-- (( x \u2219 y \u2219 ( z \u2219 u)) \u2219 (x \u2219 z \u2219 (y \u2219 u))) \u2261\n-- x \u2219 z \u2219 (y \u2219 u)\n\n-- [1] David Stanovsk\u00fd. Distributive groupoids are symmetrical-by-medial:\n-- An elementary proof. Commentations Mathematicae Universitatis\n-- Carolinae, 49(4):541\u2013546, 2008.\n\n-- 4.1 The ATPs could not prove the theorem\nopen import DistributiveLaws.TaskB-ATP\n\n-- 4.2 The interactive and combined proofs\nopen import DistributiveLaws.TaskB-HalvedStepsATP\nopen import DistributiveLaws.TaskB-I\nopen import DistributiveLaws.TaskB-TopDownATP\n\n------------------------------------------------------------------------------\n-- 5. Peano arithmetic (PA)\n\n-- We write two formalizations of PA. In the axiomatic formalization\n-- we use Agda postulates for Peano's axioms. In the inductive\n-- formalization, we use Agda data types and primitive recursive\n-- functions.\n\n-- 5.1 Axiomatic PA\n-- 5.1.1 The axioms\nopen import PA.Axiomatic.Base\n\n-- 5.1.2 Some properties\nopen import PA.Axiomatic.PropertiesATP\nopen import PA.Axiomatic.PropertiesI\n\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityATP\nopen import PA.Axiomatic.Relation.Binary.PropositionalEqualityI\n\n-- 5.2. Inductive PA\n-- 5.2.1 Some properties\nopen import PA.Inductive.Properties\nopen import PA.Inductive.PropertiesATP\nopen import PA.Inductive.PropertiesI\n\nopen import PA.Inductive.PropertiesByInduction\nopen import PA.Inductive.PropertiesByInductionATP\nopen import PA.Inductive.PropertiesByInductionI\n\n------------------------------------------------------------------------------\n-- 6. FOTC\n\n-- Formalization of (a version of) Azcel's First Order Theory of Combinators.\n\n-- 6.1 The axioms\nopen import FOTC.Base\n\n-- 6.2 Booleans\n\n-- 6.2.2 The axioms\nopen import FOTC.Data.Bool\n\n-- 6.2.3 Properties\nopen import FOTC.Data.Bool.PropertiesATP\nopen import FOTC.Data.Bool.PropertiesI\n\n-- 6.3. Natural numbers\n\n-- 6.3.1 The axioms\nopen import FOTC.Data.Nat\n\n-- 6.3.2 Properties\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Data.Nat.PropertiesI\n\nopen import FOTC.Data.Nat.PropertiesByInductionATP\nopen import FOTC.Data.Nat.PropertiesByInductionI\n\n-- 6.3.3 Divisibility relation\nopen import FOTC.Data.Nat.Divisibility.PropertiesATP\nopen import FOTC.Data.Nat.Divisibility.PropertiesI\n\n-- 6.3.4 Induction\nopen import FOTC.Data.Nat.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.Nat.Induction.NonAcc.LexicographicI\n\n-- 6.3.5 Inequalites\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.Inequalities.PropertiesI\n\n-- 6.3.6 Unary numbers\nopen import FOTC.Data.Nat.UnaryNumbers.IsN-ATP\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\n\n-- 6.4 Lists\n\n-- 6.4.1 The axioms\nopen import FOTC.Data.List\n\n-- 6.4.2 Properties\nopen import FOTC.Data.List.PropertiesATP\nopen import FOTC.Data.List.PropertiesI\n\n-- 6.4.3 Well-founded induction\nopen import FOTC.Data.List.LT-Cons.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.List.LT-Cons.PropertiesI\nopen import FOTC.Data.List.LT-Length.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.List.LT-Length.PropertiesI\n\n-- 6.4.4 Lists of natural numbers\nopen import FOTC.Data.Nat.List.PropertiesATP\nopen import FOTC.Data.Nat.List.PropertiesI\n\n-- 6.5 Programs\n\n-- 6.5.1 The Collatz function: A function without a termination proof\nopen import FOTC.Program.Collatz.PropertiesATP\nopen import FOTC.Program.Collatz.PropertiesI\n\n-- 6.5.2 The GCD algorithm: A non-structurally recursive algorithm\nopen import FOTC.Program.GCD.ProofSpecificationATP\nopen import FOTC.Program.GCD.ProofSpecificationI\n\n-- 6.5.3 The McCarthy 91 function: A function with nested recursion\nopen import FOTC.Program.McCarthy91.Properties.MainATP\n\n-- 6.5.4 The mirror function: A function with higher-order recursion\nopen import FOTC.Program.Mirror.PropertiesATP\nopen import FOTC.Program.Mirror.PropertiesI\n\n-- 6.5.5 Burstall's sort list algorithm: A structurally recursive algorithm\nopen import FOTC.Program.SortList.ProofSpecificationATP\nopen import FOTC.Program.SortList.ProofSpecificationI\n\n------------------------------------------------------------------------------\n-- ATPs failures\n------------------------------------------------------------------------------\n\n-- The ATPs (E, Equinox, Metis and Vampire) could not prove some\n-- theorems.\n\n-- This module was imported in the Stanovsk\u00fd example\n-- open import DistributiveLaws.TaskB-ATP\nopen import LTC-PCF.Program.GCD.EquationsATP\nopen import PA.Axiomatic.PropertiesATP\n\n------------------------------------------------------------------------------\n-- Agsy examples\n------------------------------------------------------------------------------\n\n-- We cannot import the Agsy examples because some modules contain\n-- unsolved metas, therefore see src\/Agsy\/README.txt\n\n------------------------------------------------------------------------------\n-- Other theories\n------------------------------------------------------------------------------\n\n-- 1. LTC-PCF\n-- Formalization of a version of Azcel's Logical Theory of Constructions.\n-- N.B. This was the theory shown in our PLPV'09 paper.\n\n-- 1.1. The axioms\nopen import LTC-PCF.Base\n\n-- 1.2 Natural numberes\n\n-- 1.2.1 The axioms\nopen import LTC-PCF.Data.Nat\n\n-- 1.2.2 Properties\nopen import LTC-PCF.Data.Nat.PropertiesATP\nopen import LTC-PCF.Data.Nat.PropertiesI\n\n-- 1.2.3 Divisibility relation\nopen import LTC-PCF.Data.Nat.Divisibility.Properties\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesATP\nopen import LTC-PCF.Data.Nat.Divisibility.PropertiesI\n\n-- 1.2.4 Induction\nopen import LTC-PCF.Data.Nat.Induction.NonAcc.LexicographicI\nopen import LTC-PCF.Data.Nat.Induction.NonAcc.WellFoundedInductionI\n\n-- 1.2.5 Inequalites\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesATP\nopen import LTC-PCF.Data.Nat.Inequalities.PropertiesI\n\n-- 1.2.6 The recursive operator\nopen import LTC-PCF.Data.Nat.Rec.EquationsATP\nopen import LTC-PCF.Data.Nat.Rec.EquationsI\n\n-- 1.3 Programs\n\n-- 1.3.1 The division algorithm: A non-structurally recursive algorithm\nopen import LTC-PCF.Program.Division.ProofSpecificationATP\nopen import LTC-PCF.Program.Division.ProofSpecificationI\n\n-- 1.3.2 The GCD algorithm: A non-structurally recursive algorithm\nopen import LTC-PCF.Program.GCD.ProofSpecificationATP\nopen import LTC-PCF.Program.GCD.ProofSpecificationI\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9ce820b684bc9a0775d9532aa64cb264a9cc8f0b","subject":"Wire together CBV\u21a6CBPV conversion and caching","message":"Wire together CBV\u21a6CBPV conversion and caching\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Denotation\/CachingEvaluation.agda","new_file":"Parametric\/Denotation\/CachingEvaluation.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Caching evaluation\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\n\nimport Parametric.Syntax.MType as MType\nimport Parametric.Syntax.MTerm as MTerm\n\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Denotation.MValue as MValue\nimport Parametric.Denotation.CachingMValue as CachingMValue\nimport Parametric.Denotation.MEvaluation as MEvaluation\n\nmodule Parametric.Denotation.CachingEvaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (ValConst : MTerm.ValConstStructure Const)\n (CompConst : MTerm.CompConstStructure Const)\n -- I should really switch to records - can it get sillier than this?\n (\u27e6_\u27e7ValBase : MEvaluation.ValStructure Const \u27e6_\u27e7Base ValConst CompConst)\n (\u27e6_\u27e7CompBase : MEvaluation.CompStructure Const \u27e6_\u27e7Base ValConst CompConst)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\n\nopen MType.Structure Base\nopen MTerm.Structure Const ValConst CompConst\n\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen MValue.Structure Base \u27e6_\u27e7Base\nopen CachingMValue.Structure Base \u27e6_\u27e7Base\nopen MEvaluation.Structure Const \u27e6_\u27e7Base ValConst CompConst \u27e6_\u27e7ValBase \u27e6_\u27e7CompBase\n\nopen import Base.Denotation.Notation\n\n-- Extension Point: Evaluation of fully applied constants.\nValStructure : Set\nValStructure = \u2200 {\u03a3 \u03c4} \u2192 ValConst \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n\nCompStructure : Set\nCompStructure = \u2200 {\u03a3 \u03c4} \u2192 CompConst \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n\nmodule Structure\n (\u27e6_\u27e7ValBaseTermCache : ValStructure)\n (\u27e6_\u27e7CompBaseTermCache : CompStructure)\n where\n\n {-\n -- Prototype here the type-correctness of a simple non-standard semantics.\n -- This describes a simplified version of the transformation by Liu and\n -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language\n -- terms, we produce host language terms to take advantage of the richer type\n -- system of the host language (in particular, here we need the unit type,\n -- product types and *existentials*).\n --\n -- As usual, we'll later switch to a real term transformation.\n -}\n open import Data.Product hiding (map)\n open import Data.Unit\n\n -- Defining a caching semantics for Term proves to be hard, requiring to\n -- insert and remove caches where we apply constants.\n\n -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.\n\n -- The solution is to distinguish among different kinds of constants. Some are\n -- value constructors (and thus do not return caches), while others are\n -- computation constructors (and thus should return caches). For products, I\n -- believe we will only use the products which are values, not computations\n -- (XXX check CBPV paper for the name).\n \u27e6_\u27e7CompTermCache : \u2200 {\u03c4 \u0393} \u2192 Comp \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n \u27e6_\u27e7ValTermCache : \u2200 {\u03c4 \u0393} \u2192 Val \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n \u27e6_\u27e7ValsTermCache : \u2200 {\u0393 \u03a3} \u2192 Vals \u0393 \u03a3 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache\n\n open import Base.Denotation.Environment ValType \u27e6_\u27e7ValTypeHidCache public\n using ()\n renaming (\u27e6_\u27e7Var to \u27e6_\u27e7ValVarHidCache)\n\n -- This says that the environment does not contain caches... sounds wrong!\n -- Either we add extra variables for the caches, or we store computations in\n -- the environment (but that does not make sense), or we store caches in\n -- values, by acting not on F but on something else (U?).\n\n -- Copy of \u27e6_\u27e7Vals\n \u27e6 \u2205 \u27e7ValsTermCache \u03c1 = \u2205\n \u27e6 vt \u2022 valtms \u27e7ValsTermCache \u03c1 = \u27e6 vt \u27e7ValTermCache \u03c1 \u2022 \u27e6 valtms \u27e7ValsTermCache \u03c1\n\n -- I suspect the plan was to use extra variables; that's annoying to model in\n -- Agda but easier in implementations.\n\n \u27e6 vVar x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7ValVarHidCache \u03c1\n \u27e6 vThunk x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7CompTermCache \u03c1\n -- No caching, because the arguments are values, so evaluating them does not\n -- produce intermediate results.\n \u27e6 vConst c args \u27e7ValTermCache \u03c1 = \u27e6 c \u27e7ValBaseTermCache (\u27e6 args \u27e7ValsTermCache \u03c1)\n\n -- The only caching is done by the interpretation of the constant (because the\n -- arguments are values so need no caching).\n \u27e6_\u27e7CompTermCache (cConst c args) \u03c1 = \u27e6 c \u27e7CompBaseTermCache (\u27e6 args \u27e7ValsTermCache \u03c1)\n\n -- Also, where are introduction forms for pairs and sums among values? With\n -- them, we should see that we can interpret them without adding a cache.\n\n -- Thunks keep seeming noops.\n \u27e6_\u27e7CompTermCache (cForce x) \u03c1 = \u27e6 x \u27e7ValTermCache \u03c1\n\n -- Here, in an actual implementation, we would return the actual cache with\n -- all variables.\n --\n -- The effect of F is a writer monad of cached values, where the monoid is\n -- (isomorphic to) the free monoid over (\u2203 \u03c4 . \u03c4), but we push the\n -- existentials up when pairing things!\n\n -- That's what we're interpreting computations in. XXX maybe consider using\n -- monads directly. But that doesn't deal with arity.\n \u27e6_\u27e7CompTermCache (cReturn v) \u03c1 = vUnit , (\u27e6 v \u27e7ValTermCache \u03c1 , tt)\n\n -- For this to be enough, a lambda needs to return a produce, not to forward\n -- the underlying one (unless there are no intermediate results). The correct\n -- requirements can maybe be enforced through a linear typing discipline.\n\n {-\n -- Here we'd have a problem with the original into from CBPV, because it does\n -- not require converting expressions to the \"CBPV A-normal form\".\n\n \u27e6_\u27e7CompTermCache (v\u2081 into v\u2082) \u03c1 =\n -- Sequence commands and combine their caches.\n {-\n let (_ , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (_ , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in , (r\u2082 , (c\u2081 ,\u2032 c\u2082))\n -}\n\n -- The code above does not work, because we only guarantee that v\u2082 is\n -- a computation, not that it's an F-computation - v\u2082 could also be function\n -- type or a computation product.\n\n -- We need a restricted CBPV, where these two possibilities are forbidden. If\n -- you want to save something between different lambdas, you need to add an F\n -- U to reflect that. (Double-check the papers which show how to encode arity\n -- using CBPV, it seems that they should find the same problem --- XXX they\n -- don't).\n\n -- Instead, just drop the first cache (XXX WRONG).\n \u27e6 v\u2082 \u27e7CompTermCache (proj\u2081 (proj\u2082 (\u27e6 v\u2081 \u27e7CompTermCache \u03c1)) \u2022 \u03c1)\n -}\n\n -- But if we alter _into_ as described above, composing the caches works!\n -- However, we should not forget we also need to save the new intermediate\n -- result, that is the one produced by the first part of the let.\n \u27e6_\u27e7CompTermCache (_into_ {\u03c3} {\u03c4} v\u2081 v\u2082) \u03c1 =\n let (\u03c4\u2081 , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (\u03c4\u2082 , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in (\u03c3 v\u00d7 \u03c4\u2081 v\u00d7 \u03c4\u2082) , (r\u2082 ,\u2032 ( r\u2081 ,\u2032 c\u2081 ,\u2032 c\u2082))\n\n -- Note the compositionality and luck: we don't need to do anything at the\n -- cReturn time, we just need the nested into to do their job, because as I\n -- said intermediate results are a writer monad.\n --\n -- Q: But then, do we still need to do all the other stuff? IOW, do we still\n -- need to forbid (\u03bb x . y <- f args; g args') and replace it with (\u03bb x . y <-\n -- f args; z <- g args'; z)?\n --\n -- A: One thing we still need is using the monadic version of into for the\n -- same reasons - which makes sense, since it has the type of monadic bind.\n --\n -- Maybe not: if we use a monad, which respects left and right identity, the\n -- two above forms are equivalent. But what about associativity? We don't have\n -- associativity with nested tuples in the middle. That's why the monad uses\n -- lists! We can also use nested tuple, as long as in the into case we don't\n -- do (a, b) but append a b (ahem, where?), which decomposes the first list\n -- and prepends it to the second. To this end, we need to know the type of the\n -- first element, or to ensure it's always a pair. Maybe we just want to reuse\n -- an HList.\n\n -- In abstractions, we should start collecting all variables...\n\n -- Here, unlike in \u27e6_\u27e7TermCache, we don't need to invent an empty cache,\n -- that's moved into the handling of cReturn. This makes *the* difference for\n -- nested lambdas, where we don't need to create caches multiple times!\n\n \u27e6_\u27e7CompTermCache (cAbs v) \u03c1 = \u03bb x \u2192 \u27e6 v \u27e7CompTermCache (x \u2022 \u03c1)\n\n -- Here we see that we are in a sort of A-normal form, because the argument is\n -- a value (not quite ANF though, since values can be thunks - that is,\n -- computations which haven't been run yet, I guess. Do we have an use for\n -- that? That allows passing lambdas as arguments directly - which is fine,\n -- because producing a closure indeed does not have intermediate results!).\n \u27e6_\u27e7CompTermCache (cApp t v) \u03c1 = \u27e6 t \u27e7CompTermCache \u03c1 (\u27e6 v \u27e7ValTermCache \u03c1)\n\n \u27e6_\u27e7TermCacheCBV : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 fromCBVCtx \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 cbvToCompType \u03c4 \u27e7CompTypeHidCache\n \u27e6 t \u27e7TermCacheCBV = \u27e6 fromCBV t \u27e7CompTermCache\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Caching evaluation\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\n\nimport Parametric.Syntax.MType as MType\nimport Parametric.Syntax.MTerm as MTerm\n\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Denotation.MValue as MValue\nimport Parametric.Denotation.CachingMValue as CachingMValue\nimport Parametric.Denotation.MEvaluation as MEvaluation\n\nmodule Parametric.Denotation.CachingEvaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (ValConst : MTerm.ValConstStructure Const)\n (CompConst : MTerm.CompConstStructure Const)\n -- I should really switch to records - can it get sillier than this?\n (\u27e6_\u27e7ValBase : MEvaluation.ValStructure Const \u27e6_\u27e7Base ValConst CompConst)\n (\u27e6_\u27e7CompBase : MEvaluation.CompStructure Const \u27e6_\u27e7Base ValConst CompConst)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\n\nopen MType.Structure Base\nopen MTerm.Structure Const ValConst CompConst\n\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen MValue.Structure Base \u27e6_\u27e7Base\nopen CachingMValue.Structure Base \u27e6_\u27e7Base\nopen MEvaluation.Structure Const \u27e6_\u27e7Base ValConst CompConst \u27e6_\u27e7ValBase \u27e6_\u27e7CompBase\n\nopen import Base.Denotation.Notation\n\n-- Extension Point: Evaluation of fully applied constants.\nValStructure : Set\nValStructure = \u2200 {\u03a3 \u03c4} \u2192 ValConst \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n\nCompStructure : Set\nCompStructure = \u2200 {\u03a3 \u03c4} \u2192 CompConst \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n\nmodule Structure\n (\u27e6_\u27e7ValBaseTermCache : ValStructure)\n (\u27e6_\u27e7CompBaseTermCache : CompStructure)\n where\n\n {-\n -- Prototype here the type-correctness of a simple non-standard semantics.\n -- This describes a simplified version of the transformation by Liu and\n -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language\n -- terms, we produce host language terms to take advantage of the richer type\n -- system of the host language (in particular, here we need the unit type,\n -- product types and *existentials*).\n --\n -- As usual, we'll later switch to a real term transformation.\n -}\n open import Data.Product hiding (map)\n open import Data.Unit\n\n -- Defining a caching semantics for Term proves to be hard, requiring to\n -- insert and remove caches where we apply constants.\n\n -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.\n\n -- The solution is to distinguish among different kinds of constants. Some are\n -- value constructors (and thus do not return caches), while others are\n -- computation constructors (and thus should return caches). For products, I\n -- believe we will only use the products which are values, not computations\n -- (XXX check CBPV paper for the name).\n \u27e6_\u27e7CompTermCache : \u2200 {\u03c4 \u0393} \u2192 Comp \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n \u27e6_\u27e7ValTermCache : \u2200 {\u03c4 \u0393} \u2192 Val \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n \u27e6_\u27e7ValsTermCache : \u2200 {\u0393 \u03a3} \u2192 Vals \u0393 \u03a3 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache\n\n open import Base.Denotation.Environment ValType \u27e6_\u27e7ValTypeHidCache public\n using ()\n renaming (\u27e6_\u27e7Var to \u27e6_\u27e7ValVarHidCache)\n\n -- This says that the environment does not contain caches... sounds wrong!\n -- Either we add extra variables for the caches, or we store computations in\n -- the environment (but that does not make sense), or we store caches in\n -- values, by acting not on F but on something else (U?).\n\n -- Copy of \u27e6_\u27e7Vals\n \u27e6 \u2205 \u27e7ValsTermCache \u03c1 = \u2205\n \u27e6 vt \u2022 valtms \u27e7ValsTermCache \u03c1 = \u27e6 vt \u27e7ValTermCache \u03c1 \u2022 \u27e6 valtms \u27e7ValsTermCache \u03c1\n\n -- I suspect the plan was to use extra variables; that's annoying to model in\n -- Agda but easier in implementations.\n\n \u27e6 vVar x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7ValVarHidCache \u03c1\n \u27e6 vThunk x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7CompTermCache \u03c1\n -- No caching, because the arguments are values, so evaluating them does not\n -- produce intermediate results.\n \u27e6 vConst c args \u27e7ValTermCache \u03c1 = \u27e6 c \u27e7ValBaseTermCache (\u27e6 args \u27e7ValsTermCache \u03c1)\n\n -- The only caching is done by the interpretation of the constant (because the\n -- arguments are values so need no caching).\n \u27e6_\u27e7CompTermCache (cConst c args) \u03c1 = \u27e6 c \u27e7CompBaseTermCache (\u27e6 args \u27e7ValsTermCache \u03c1)\n\n -- Also, where are introduction forms for pairs and sums among values? With\n -- them, we should see that we can interpret them without adding a cache.\n\n -- Thunks keep seeming noops.\n \u27e6_\u27e7CompTermCache (cForce x) \u03c1 = \u27e6 x \u27e7ValTermCache \u03c1\n\n -- Here, in an actual implementation, we would return the actual cache with\n -- all variables.\n --\n -- The effect of F is a writer monad of cached values, where the monoid is\n -- (isomorphic to) the free monoid over (\u2203 \u03c4 . \u03c4), but we push the\n -- existentials up when pairing things!\n\n -- That's what we're interpreting computations in. XXX maybe consider using\n -- monads directly. But that doesn't deal with arity.\n \u27e6_\u27e7CompTermCache (cReturn v) \u03c1 = vUnit , (\u27e6 v \u27e7ValTermCache \u03c1 , tt)\n\n -- For this to be enough, a lambda needs to return a produce, not to forward\n -- the underlying one (unless there are no intermediate results). The correct\n -- requirements can maybe be enforced through a linear typing discipline.\n\n {-\n -- Here we'd have a problem with the original into from CBPV, because it does\n -- not require converting expressions to the \"CBPV A-normal form\".\n\n \u27e6_\u27e7CompTermCache (v\u2081 into v\u2082) \u03c1 =\n -- Sequence commands and combine their caches.\n {-\n let (_ , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (_ , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in , (r\u2082 , (c\u2081 ,\u2032 c\u2082))\n -}\n\n -- The code above does not work, because we only guarantee that v\u2082 is\n -- a computation, not that it's an F-computation - v\u2082 could also be function\n -- type or a computation product.\n\n -- We need a restricted CBPV, where these two possibilities are forbidden. If\n -- you want to save something between different lambdas, you need to add an F\n -- U to reflect that. (Double-check the papers which show how to encode arity\n -- using CBPV, it seems that they should find the same problem --- XXX they\n -- don't).\n\n -- Instead, just drop the first cache (XXX WRONG).\n \u27e6 v\u2082 \u27e7CompTermCache (proj\u2081 (proj\u2082 (\u27e6 v\u2081 \u27e7CompTermCache \u03c1)) \u2022 \u03c1)\n -}\n\n -- But if we alter _into_ as described above, composing the caches works!\n -- However, we should not forget we also need to save the new intermediate\n -- result, that is the one produced by the first part of the let.\n \u27e6_\u27e7CompTermCache (_into_ {\u03c3} {\u03c4} v\u2081 v\u2082) \u03c1 =\n let (\u03c4\u2081 , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (\u03c4\u2082 , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in (\u03c3 v\u00d7 \u03c4\u2081 v\u00d7 \u03c4\u2082) , (r\u2082 ,\u2032 ( r\u2081 ,\u2032 c\u2081 ,\u2032 c\u2082))\n\n -- Note the compositionality and luck: we don't need to do anything at the\n -- cReturn time, we just need the nested into to do their job, because as I\n -- said intermediate results are a writer monad.\n --\n -- Q: But then, do we still need to do all the other stuff? IOW, do we still\n -- need to forbid (\u03bb x . y <- f args; g args') and replace it with (\u03bb x . y <-\n -- f args; z <- g args'; z)?\n --\n -- A: One thing we still need is using the monadic version of into for the\n -- same reasons - which makes sense, since it has the type of monadic bind.\n --\n -- Maybe not: if we use a monad, which respects left and right identity, the\n -- two above forms are equivalent. But what about associativity? We don't have\n -- associativity with nested tuples in the middle. That's why the monad uses\n -- lists! We can also use nested tuple, as long as in the into case we don't\n -- do (a, b) but append a b (ahem, where?), which decomposes the first list\n -- and prepends it to the second. To this end, we need to know the type of the\n -- first element, or to ensure it's always a pair. Maybe we just want to reuse\n -- an HList.\n\n -- In abstractions, we should start collecting all variables...\n\n -- Here, unlike in \u27e6_\u27e7TermCache, we don't need to invent an empty cache,\n -- that's moved into the handling of cReturn. This makes *the* difference for\n -- nested lambdas, where we don't need to create caches multiple times!\n\n \u27e6_\u27e7CompTermCache (cAbs v) \u03c1 = \u03bb x \u2192 \u27e6 v \u27e7CompTermCache (x \u2022 \u03c1)\n\n -- Here we see that we are in a sort of A-normal form, because the argument is\n -- a value (not quite ANF though, since values can be thunks - that is,\n -- computations which haven't been run yet, I guess. Do we have an use for\n -- that? That allows passing lambdas as arguments directly - which is fine,\n -- because producing a closure indeed does not have intermediate results!).\n \u27e6_\u27e7CompTermCache (cApp t v) \u03c1 = \u27e6 t \u27e7CompTermCache \u03c1 (\u27e6 v \u27e7ValTermCache \u03c1)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"242a602d070c3965f3dedd346bff306ec822c452","subject":"Group.Homomorphism: add id, \u2218, \u0394, zip, pair","message":"Group.Homomorphism: add id, \u2218, \u0394, zip, pair\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Algebra\/Group\/Homomorphism.agda","new_file":"lib\/Algebra\/Group\/Homomorphism.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule Algebra.Group.Homomorphism where\n\nopen import Type using (Type_)\nopen import Function.NP using (Op\u2082; _\u2218_; id)\nimport Algebra.FunctionProperties.Eq\nopen Algebra.FunctionProperties.Eq.Implicits\nopen import Algebra.Monoid\nopen import Algebra.Monoid.Homomorphism\nopen import Algebra.Raw\nopen import Algebra.Group\nopen import Algebra.Group.Constructions\nopen import Level.NP\nopen import Data.Product.NP\nopen import Data.Nat.NP using (1+_)\nopen import Data.Integer.NP using (\u2124; -[1+_]; +_; -_; module \u2124\u00b0)\nopen import Relation.Binary.PropositionalEquality.NP\nopen \u2261-Reasoning\n\nrecord GroupHomomorphism {a}{A : Type a}{b}{B : Type b}\n (grpA0+ : Group A)(grpB1* : Group B)\n (f : A \u2192 B) : Type (a \u2294 b) where\n constructor mk\n\n open Additive-Group grpA0+\n open Multiplicative-Group grpB1*\n\n field\n hom : Homomorphic\u2082 f _+_ _*_\n\n pres-unit : f 0# \u2261 1#\n pres-unit = unique-1-left part\n where part = f 0# * f 0# \u2261\u27e8 ! hom \u27e9\n f (0# + 0#) \u2261\u27e8 ap f (fst +-identity) \u27e9\n f 0# \u220e\n\n mon-hom : MonoidHomomorphism +-mon *-mon f\n mon-hom = pres-unit , hom\n\n open MonoidHomomorphism mon-hom public\n\n pres-inv : \u2200 {x} \u2192 f (0\u2212 x) \u2261 (f x)\u207b\u00b9\n pres-inv {x} = unique-\u207b\u00b9 part\n where part = f (0\u2212 x) * f x \u2261\u27e8 ! hom \u27e9\n f (0\u2212 x + x) \u2261\u27e8 ap f (fst 0\u2212-inverse) \u27e9\n f 0# \u2261\u27e8 pres-unit \u27e9\n 1# \u220e\n\n 0\u2212-\u207b\u00b9 = pres-inv\n\n \u2212-\/ : \u2200 {x y} \u2192 f (x \u2212 y) \u2261 f x \/ f y\n \u2212-\/ {x} {y} = f (x \u2212 y) \u2261\u27e8 hom \u27e9\n f x * f (0\u2212 y) \u2261\u27e8 ap (_*_ (f x)) pres-inv \u27e9\n f x \/ f y \u220e\n\n hom-iterated\u207b : \u2200 {x} n \u2192 f (x \u2297\u207b n) \u2261 f x ^\u207b n\n hom-iterated\u207b {x} n =\n f (x \u2297\u207b n) \u2261\u27e8by-definition\u27e9\n f (0\u2212(x \u2297\u207a n)) \u2261\u27e8 pres-inv \u27e9\n f(x \u2297\u207a n)\u207b\u00b9 \u2261\u27e8 ap _\u207b\u00b9 (hom-iterated\u207a n) \u27e9\n (f x ^\u207a n)\u207b\u00b9 \u2261\u27e8by-definition\u27e9\n f x ^\u207b n \u220e\n\n hom-iterated : \u2200 {x} i \u2192 f (x \u2297 i) \u2261 f x ^ i\n hom-iterated -[1+ n ] = hom-iterated\u207b (1+ n)\n hom-iterated (+ n) = hom-iterated\u207a n\n\n\u2124+-grp-ops : Group-Ops \u2124\n\u2124+-grp-ops = \u2124+-mon-ops , -_\n\n\u2124+-grp-struct : Group-Struct \u2124+-grp-ops\n\u2124+-grp-struct = \u2124+-mon-struct\n , (\u03bb{x} \u2192 fst \u2124\u00b0.-\u203finverse x)\n , (\u03bb{x} \u2192 snd \u2124\u00b0.-\u203finverse x)\n\n\u2124+-grp : Group \u2124\n\u2124+-grp = _ , \u2124+-grp-struct\n\nmodule \u2124+ = Additive-Group \u2124+-grp\n\nmodule _ {\u2113}{G : Type \u2113}(\ud835\udd3e : Group G) where\n open Group\u1d52\u1d56\n open Group \ud835\udd3e\n\n module \u207b\u00b9-Hom where\n -- The proper type for \u207b\u00b9-hom\u2032\n \u207b\u00b9-hom' : GroupHomomorphism \ud835\udd3e (\ud835\udd3e \u1d52\u1d56) _\u207b\u00b9\n \u207b\u00b9-hom' = mk \u207b\u00b9-hom\u2032\n open GroupHomomorphism \u207b\u00b9-hom' public\n\n module \u2124+-^-Hom {b} where\n ^-+-hom : GroupHomomorphism \u2124+-grp \ud835\udd3e (_^_ b)\n ^-+-hom = mk (\u03bb {i} {j} \u2192 ^-+ i j)\n\n open GroupHomomorphism ^-+-hom public\n\nmodule Stability-Minimal\n {a}{A : Type a}\n {b}{B : Type b}\n (\u03c6 : A \u2192 B)\n (_+_ : Op\u2082 A)\n (_*_ : Op\u2082 B)\n (\u03c6-+-* : \u2200 {x y} \u2192 \u03c6 (x + y) \u2261 \u03c6 x * \u03c6 y)\n {c}{C : Type c}\n (F : (A \u2192 B) \u2192 C)\n (F= : \u2200 {f g : A \u2192 B} \u2192 f \u2257 g \u2192 F f \u2261 F g)\n (F\u03c6* : \u2200 {k} \u2192 F \u03c6 \u2261 F (_*_ k \u2218 \u03c6))\n where\n\n +-stable : \u2200 {k} \u2192 F \u03c6 \u2261 F (\u03c6 \u2218 _+_ k)\n +-stable {k} =\n F \u03c6 \u2261\u27e8 F\u03c6* \u27e9\n F (_*_ (\u03c6 k) \u2218 \u03c6) \u2261\u27e8 F= (\u03bb x \u2192 ! \u03c6-+-*) \u27e9\n F (\u03c6 \u2218 _+_ k) \u220e\n\nmodule Stability\n {a}{A : Type a}\n {b}{B : Type b}\n (G+ : Group A)\n (G* : Group B)\n (\u03c6 : A \u2192 B)\n (\u03c6-hom : GroupHomomorphism G+ G* \u03c6)\n where\n open Additive-Group G+\n open Multiplicative-Group G*\n open GroupHomomorphism \u03c6-hom\n\n open Stability-Minimal \u03c6 _+_ _*_ hom public\n\nopen GroupHomomorphism\n\nmodule Identity\n {a}{A : Type a}\n (\ud835\udd38 : Group A)\n where\n\n id-hom : GroupHomomorphism \ud835\udd38 \ud835\udd38 id\n id-hom = mk refl\n\nmodule Compose\n {a}{A : Type a}\n {b}{B : Type b}\n {c}{C : Type c}\n (\ud835\udd38 : Group A)\n (\ud835\udd39 : Group B)\n (\u2102 : Group C)\n (\u03c8 : A \u2192 B)\n (\u03c8-hom : GroupHomomorphism \ud835\udd38 \ud835\udd39 \u03c8)\n (\u03c6 : B \u2192 C)\n (\u03c6-hom : GroupHomomorphism \ud835\udd39 \u2102 \u03c6)\n where\n\n \u2218-hom : GroupHomomorphism \ud835\udd38 \u2102 (\u03c6 \u2218 \u03c8)\n \u2218-hom = mk (ap \u03c6 (hom \u03c8-hom) \u2219 hom \u03c6-hom)\n\nmodule Delta\n {a}{A : Type a}\n (\ud835\udd38 : Group A)\n where\n open Algebra.Group.Constructions.Product\n\n \u0394-hom : GroupHomomorphism \ud835\udd38 (\u00d7-grp \ud835\udd38 \ud835\udd38) (\u03bb x \u2192 x , x)\n \u0394-hom = mk refl\n\nmodule Zip\n {a\u2080}{A\u2080 : Type a\u2080}\n {a\u2081}{A\u2081 : Type a\u2081}\n {b\u2080}{B\u2080 : Type b\u2080}\n {b\u2081}{B\u2081 : Type b\u2081}\n (\ud835\udd38\u2080 : Group A\u2080)\n (\ud835\udd38\u2081 : Group A\u2081)\n (\ud835\udd39\u2080 : Group B\u2080)\n (\ud835\udd39\u2081 : Group B\u2081)\n (\u03c6\u2080 : A\u2080 \u2192 B\u2080)\n (\u03c6\u2080-hom : GroupHomomorphism \ud835\udd38\u2080 \ud835\udd39\u2080 \u03c6\u2080)\n (\u03c6\u2081 : A\u2081 \u2192 B\u2081)\n (\u03c6\u2081-hom : GroupHomomorphism \ud835\udd38\u2081 \ud835\udd39\u2081 \u03c6\u2081)\n where\n open Algebra.Group.Constructions.Product\n\n zip-hom : GroupHomomorphism (\u00d7-grp \ud835\udd38\u2080 \ud835\udd38\u2081) (\u00d7-grp \ud835\udd39\u2080 \ud835\udd39\u2081) (map \u03c6\u2080 \u03c6\u2081)\n zip-hom = mk (ap\u2082 _,_ (hom \u03c6\u2080-hom) (hom \u03c6\u2081-hom))\n\nmodule Pair\n {a}{A : Type a}\n {b\u2080}{B\u2080 : Type b\u2080}\n {b\u2081}{B\u2081 : Type b\u2081}\n (\ud835\udd38 : Group A)\n (\ud835\udd39\u2080 : Group B\u2080)\n (\ud835\udd39\u2081 : Group B\u2081)\n (\u03c6\u2080 : A \u2192 B\u2080)\n (\u03c6\u2080-hom : GroupHomomorphism \ud835\udd38 \ud835\udd39\u2080 \u03c6\u2080)\n (\u03c6\u2081 : A \u2192 B\u2081)\n (\u03c6\u2081-hom : GroupHomomorphism \ud835\udd38 \ud835\udd39\u2081 \u03c6\u2081)\n where\n\n -- pair = zip \u2218 \u0394\n pair-hom : GroupHomomorphism \ud835\udd38 (Product.\u00d7-grp \ud835\udd39\u2080 \ud835\udd39\u2081) < \u03c6\u2080 , \u03c6\u2081 >\n pair-hom = Compose.\u2218-hom _ _ _\n _ (Delta.\u0394-hom \ud835\udd38)\n _ (Zip.zip-hom _ _ _ _ _ \u03c6\u2080-hom _ \u03c6\u2081-hom)\n -- OR:\n pair-hom = mk (ap\u2082 _,_ (hom \u03c6\u2080-hom) (hom \u03c6\u2081-hom))\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nmodule Algebra.Group.Homomorphism where\n\nopen import Type using (Type_)\nopen import Function.NP using (Op\u2082; _\u2218_)\nimport Algebra.FunctionProperties.Eq\nopen Algebra.FunctionProperties.Eq.Implicits\nopen import Algebra.Monoid\nopen import Algebra.Monoid.Homomorphism\nopen import Algebra.Raw\nopen import Algebra.Group\nopen import Algebra.Group.Constructions\nopen import Level.NP\nopen import Data.Product.NP\nopen import Data.Nat.NP using (1+_)\nopen import Data.Integer.NP using (\u2124; -[1+_]; +_; -_; module \u2124\u00b0)\nopen import Relation.Binary.PropositionalEquality.NP\nopen \u2261-Reasoning\n\nrecord GroupHomomorphism {a}{A : Type a}{b}{B : Type b}\n (grpA0+ : Group A)(grpB1* : Group B)\n (f : A \u2192 B) : Type (a \u2294 b) where\n constructor mk\n\n open Additive-Group grpA0+\n open Multiplicative-Group grpB1*\n\n field\n hom : Homomorphic\u2082 f _+_ _*_\n\n pres-unit : f 0# \u2261 1#\n pres-unit = unique-1-left part\n where part = f 0# * f 0# \u2261\u27e8 ! hom \u27e9\n f (0# + 0#) \u2261\u27e8 ap f (fst +-identity) \u27e9\n f 0# \u220e\n\n mon-hom : MonoidHomomorphism +-mon *-mon f\n mon-hom = pres-unit , hom\n\n open MonoidHomomorphism mon-hom public\n\n pres-inv : \u2200 {x} \u2192 f (0\u2212 x) \u2261 (f x)\u207b\u00b9\n pres-inv {x} = unique-\u207b\u00b9 part\n where part = f (0\u2212 x) * f x \u2261\u27e8 ! hom \u27e9\n f (0\u2212 x + x) \u2261\u27e8 ap f (fst 0\u2212-inverse) \u27e9\n f 0# \u2261\u27e8 pres-unit \u27e9\n 1# \u220e\n\n 0\u2212-\u207b\u00b9 = pres-inv\n\n \u2212-\/ : \u2200 {x y} \u2192 f (x \u2212 y) \u2261 f x \/ f y\n \u2212-\/ {x} {y} = f (x \u2212 y) \u2261\u27e8 hom \u27e9\n f x * f (0\u2212 y) \u2261\u27e8 ap (_*_ (f x)) pres-inv \u27e9\n f x \/ f y \u220e\n\n hom-iterated\u207b : \u2200 {x} n \u2192 f (x \u2297\u207b n) \u2261 f x ^\u207b n\n hom-iterated\u207b {x} n =\n f (x \u2297\u207b n) \u2261\u27e8by-definition\u27e9\n f (0\u2212(x \u2297\u207a n)) \u2261\u27e8 pres-inv \u27e9\n f(x \u2297\u207a n)\u207b\u00b9 \u2261\u27e8 ap _\u207b\u00b9 (hom-iterated\u207a n) \u27e9\n (f x ^\u207a n)\u207b\u00b9 \u2261\u27e8by-definition\u27e9\n f x ^\u207b n \u220e\n\n hom-iterated : \u2200 {x} i \u2192 f (x \u2297 i) \u2261 f x ^ i\n hom-iterated -[1+ n ] = hom-iterated\u207b (1+ n)\n hom-iterated (+ n) = hom-iterated\u207a n\n\n\u2124+-grp-ops : Group-Ops \u2124\n\u2124+-grp-ops = \u2124+-mon-ops , -_\n\n\u2124+-grp-struct : Group-Struct \u2124+-grp-ops\n\u2124+-grp-struct = \u2124+-mon-struct\n , (\u03bb{x} \u2192 fst \u2124\u00b0.-\u203finverse x)\n , (\u03bb{x} \u2192 snd \u2124\u00b0.-\u203finverse x)\n\n\u2124+-grp : Group \u2124\n\u2124+-grp = _ , \u2124+-grp-struct\n\nmodule \u2124+ = Additive-Group \u2124+-grp\n\nmodule _ {\u2113}{G : Type \u2113}(\ud835\udd3e : Group G) where\n open Group\u1d52\u1d56\n open Group \ud835\udd3e\n\n module \u207b\u00b9-Hom where\n -- The proper type for \u207b\u00b9-hom\u2032\n \u207b\u00b9-hom' : GroupHomomorphism \ud835\udd3e (\ud835\udd3e \u1d52\u1d56) _\u207b\u00b9\n \u207b\u00b9-hom' = mk \u207b\u00b9-hom\u2032\n open GroupHomomorphism \u207b\u00b9-hom' public\n\n module \u2124+-^-Hom {b} where\n ^-+-hom : GroupHomomorphism \u2124+-grp \ud835\udd3e (_^_ b)\n ^-+-hom = mk (\u03bb {i} {j} \u2192 ^-+ i j)\n\n open GroupHomomorphism ^-+-hom public\n\nmodule Stability-Minimal\n {a}{A : Type a}\n {b}{B : Type b}\n (\u03c6 : A \u2192 B)\n (_+_ : Op\u2082 A)\n (_*_ : Op\u2082 B)\n (\u03c6-+-* : \u2200 {x y} \u2192 \u03c6 (x + y) \u2261 \u03c6 x * \u03c6 y)\n {c}{C : Type c}\n (F : (A \u2192 B) \u2192 C)\n (F= : \u2200 {f g : A \u2192 B} \u2192 f \u2257 g \u2192 F f \u2261 F g)\n (F\u03c6* : \u2200 {k} \u2192 F \u03c6 \u2261 F (_*_ k \u2218 \u03c6))\n where\n\n +-stable : \u2200 {k} \u2192 F \u03c6 \u2261 F (\u03c6 \u2218 _+_ k)\n +-stable {k} =\n F \u03c6 \u2261\u27e8 F\u03c6* \u27e9\n F (_*_ (\u03c6 k) \u2218 \u03c6) \u2261\u27e8 F= (\u03bb x \u2192 ! \u03c6-+-*) \u27e9\n F (\u03c6 \u2218 _+_ k) \u220e\n\nmodule Stability\n {a}{A : Type a}\n {b}{B : Type b}\n (G+ : Group A)\n (G* : Group B)\n (\u03c6 : A \u2192 B)\n (\u03c6-hom : GroupHomomorphism G+ G* \u03c6)\n where\n open Additive-Group G+\n open Multiplicative-Group G*\n open GroupHomomorphism \u03c6-hom\n\n open Stability-Minimal \u03c6 _+_ _*_ hom public\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"07ec73114140de9e379a1a389af4c43e57ad0b9d","subject":"Updated mirror draft.","message":"Updated mirror draft.\n\nIgnore-this: a5731e95fafcd4ae0b5c3a0055e1aafb\n\ndarcs-hash:20111129114129-3bd4e-320936ab5c1803f73f10c676e42599f5b831afd8.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/FOTC\/Program\/Mirror\/PropertiesListSL.agda","new_file":"Draft\/FOTC\/Program\/Mirror\/PropertiesListSL.agda","new_contents":"-- Tested with the development version of the standard library on\n-- 29 November 2011.\n\nmodule PropertiesListSL where\n\nopen import Algebra\nopen import Data.List as List hiding ( reverse )\nopen import Data.List.Properties hiding ( reverse-++-commute )\nopen import Data.Product hiding (map)\n\nopen import Relation.Binary.PropositionalEquality\nopen \u2261-Reasoning\n\nmodule LM {A : Set} = Monoid (List.monoid A)\n\n------------------------------------------------------------------------------\n\nreverse : {A : Set} \u2192 List A \u2192 List A\nreverse [] = []\nreverse (x \u2237 xs) = reverse xs ++ x \u2237 []\n\n++-rightIdentity : {A : Set}(xs : List A) \u2192 xs ++ [] \u2261 xs\n++-rightIdentity = proj\u2082 LM.identity\n\nreverse-++-commute : {A : Set}(xs ys : List A) \u2192\n reverse (xs ++ ys) \u2261 reverse ys ++ reverse xs\nreverse-++-commute [] ys = sym (++-rightIdentity (reverse ys))\nreverse-++-commute (x \u2237 xs) [] = cong (\u03bb x' \u2192 reverse x' ++ x \u2237 [])\n (++-rightIdentity xs)\nreverse-++-commute (x \u2237 xs) (y \u2237 ys) =\n begin\n reverse (xs ++ y \u2237 ys) ++ x \u2237 []\n \u2261\u27e8 cong (\u03bb x' \u2192 x' ++ x \u2237 []) (reverse-++-commute xs (y \u2237 ys)) \u27e9\n (reverse (y \u2237 ys) ++ reverse xs) ++ x \u2237 []\n \u2261\u27e8 LM.assoc (reverse (y \u2237 ys)) (reverse xs) (x \u2237 []) \u27e9\n reverse (y \u2237 ys) ++ reverse (x \u2237 xs)\n \u220e\n\ndata Tree (A : Set) : Set where\n treeT : A \u2192 List (Tree A) \u2192 Tree A\n\nmirror : {A : Set} \u2192 Tree A \u2192 Tree A\nmirror (treeT a ts) = treeT a (reverse (map mirror ts))\n\nmutual\n mirror\u00b2 : {A : Set} \u2192 (t : Tree A) \u2192 mirror (mirror t) \u2261 t\n mirror\u00b2 (treeT a []) = refl\n mirror\u00b2 (treeT a (t \u2237 ts)) =\n begin\n treeT a (reverse (map mirror (reverse (map mirror ts) ++ mirror t \u2237 [])))\n \u2261\u27e8 subst (\u03bb x \u2192 treeT a (reverse (map mirror (reverse (map mirror ts) ++\n mirror t \u2237 []))) \u2261\n treeT a x)\n (helper (t \u2237 ts))\n refl\n \u27e9\n treeT a (t \u2237 ts)\n \u220e\n\n helper : {A : Set} \u2192 (ts : List (Tree A)) \u2192\n reverse (map mirror (reverse (map mirror ts))) \u2261 ts\n helper [] = refl\n helper (t \u2237 ts) =\n begin\n reverse (map mirror (reverse (map mirror ts) ++ mirror t \u2237 []))\n \u2261\u27e8 subst (\u03bb x \u2192 (reverse (map mirror (reverse (map mirror ts) ++\n mirror t \u2237 []))) \u2261\n reverse x)\n (map-++-commute mirror (reverse (map mirror ts)) (mirror t \u2237 []))\n refl\n \u27e9\n reverse (map mirror (reverse (map mirror ts)) ++\n (map mirror (mirror t \u2237 [])))\n \u2261\u27e8 subst (\u03bb x \u2192 (reverse (map mirror (reverse (map mirror ts)) ++\n (map mirror (mirror t \u2237 [])))) \u2261\n x)\n (reverse-++-commute (map mirror (reverse (map mirror ts)))\n (map mirror (mirror t \u2237 [])))\n refl\n \u27e9\n reverse (map mirror (mirror t \u2237 [])) ++\n reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 refl \u27e9\n mirror (mirror t) \u2237 reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 subst (\u03bb x \u2192 (mirror (mirror t) \u2237\n reverse (map mirror (reverse (map mirror ts)))) \u2261\n (x \u2237 reverse (map mirror (reverse (map mirror ts)))))\n (mirror\u00b2 t) -- IH.\n refl\n \u27e9\n t \u2237 reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 subst (\u03bb x \u2192 t \u2237 reverse (map mirror (reverse (map mirror ts))) \u2261\n t \u2237 x)\n (helper ts)\n refl\n \u27e9\n t \u2237 ts\n \u220e\n","old_contents":"-- Tested with the development version of the standard library on\n-- 02 May 2011.\n\nmodule PropertiesListSL where\n\nopen import Algebra\nopen import Data.List as List hiding ( reverse )\nopen import Data.List.Properties hiding ( reverse-++-commute )\n\nopen import Relation.Binary.PropositionalEquality\nopen \u2261-Reasoning\n\nmodule LM {A : Set} = Monoid (List.monoid A)\n\n------------------------------------------------------------------------------\n\nreverse : {A : Set} \u2192 List A \u2192 List A\nreverse [] = []\nreverse (x \u2237 xs) = reverse xs ++ x \u2237 []\n\n++-rightIdentity : {A : Set}(xs : List A) \u2192 xs ++ [] \u2261 xs\n++-rightIdentity [] = refl\n++-rightIdentity (x \u2237 xs) = cong (_\u2237_ x) (++-rightIdentity xs)\n\nreverse-++-commute : {A : Set}(xs ys : List A) \u2192\n reverse (xs ++ ys) \u2261 reverse ys ++ reverse xs\nreverse-++-commute [] ys = sym (++-rightIdentity (reverse ys))\nreverse-++-commute (x \u2237 xs) [] = cong (\u03bb x' \u2192 reverse x' ++ x \u2237 [])\n (++-rightIdentity xs)\nreverse-++-commute (x \u2237 xs) (y \u2237 ys) =\n begin\n reverse (xs ++ y \u2237 ys) ++ x \u2237 []\n \u2261\u27e8 cong (\u03bb x' \u2192 x' ++ x \u2237 []) (reverse-++-commute xs (y \u2237 ys)) \u27e9\n (reverse (y \u2237 ys) ++ reverse xs) ++ x \u2237 []\n \u2261\u27e8 LM.assoc (reverse (y \u2237 ys)) (reverse xs) (x \u2237 []) \u27e9\n reverse (y \u2237 ys) ++ reverse (x \u2237 xs)\n \u220e\n\ndata Tree (A : Set) : Set where\n treeT : A \u2192 List (Tree A) \u2192 Tree A\n\nmirror : {A : Set} \u2192 Tree A \u2192 Tree A\nmirror (treeT a ts) = treeT a (reverse (map mirror ts))\n\nmutual\n mirror\u00b2 : {A : Set} \u2192 (t : Tree A) \u2192 mirror (mirror t) \u2261 t\n mirror\u00b2 (treeT a []) = refl\n mirror\u00b2 (treeT a (t \u2237 ts)) =\n begin\n treeT a (reverse (map mirror (reverse (map mirror ts) ++ mirror t \u2237 [])))\n \u2261\u27e8 subst (\u03bb x \u2192 treeT a (reverse (map mirror (reverse (map mirror ts) ++\n mirror t \u2237 []))) \u2261\n treeT a x)\n (helper (t \u2237 ts))\n refl\n \u27e9\n treeT a (t \u2237 ts)\n \u220e\n\n helper : {A : Set} \u2192 (ts : List (Tree A)) \u2192\n reverse (map mirror (reverse (map mirror ts))) \u2261 ts\n helper [] = refl\n helper (t \u2237 ts) =\n begin\n reverse (map mirror (reverse (map mirror ts) ++ mirror t \u2237 []))\n \u2261\u27e8 subst (\u03bb x \u2192 (reverse (map mirror (reverse (map mirror ts) ++\n mirror t \u2237 []))) \u2261\n reverse x)\n (map-++-commute mirror (reverse (map mirror ts)) (mirror t \u2237 []))\n refl\n \u27e9\n reverse (map mirror (reverse (map mirror ts)) ++\n (map mirror (mirror t \u2237 [])))\n \u2261\u27e8 subst (\u03bb x \u2192 (reverse (map mirror (reverse (map mirror ts)) ++\n (map mirror (mirror t \u2237 [])))) \u2261\n x)\n (reverse-++-commute (map mirror (reverse (map mirror ts)))\n (map mirror (mirror t \u2237 [])))\n refl\n \u27e9\n reverse (map mirror (mirror t \u2237 [])) ++\n reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 refl \u27e9\n mirror (mirror t) \u2237 reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 subst (\u03bb x \u2192 (mirror (mirror t) \u2237\n reverse (map mirror (reverse (map mirror ts)))) \u2261\n (x \u2237 reverse (map mirror (reverse (map mirror ts)))))\n (mirror\u00b2 t) -- IH.\n refl\n \u27e9\n t \u2237 reverse (map mirror (reverse (map mirror ts)))\n \u2261\u27e8 subst (\u03bb x \u2192 t \u2237 reverse (map mirror (reverse (map mirror ts))) \u2261\n t \u2237 x)\n (helper ts)\n refl\n \u27e9\n t \u2237 ts\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"39d9e13ae25672af17181a0c3d1fee2118ac33c9","subject":"Introduce and use UncurriedTermConstructor.","message":"Introduce and use UncurriedTermConstructor.\n\nOld-commit-hash: d36f33b097ddb2444e6409de52048ef9bf94b001\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Term\/Plotkin.agda","new_file":"Syntax\/Term\/Plotkin.agda","new_contents":"import Syntax.Type.Plotkin as Type\nimport Syntax.Context as Context\n\nmodule Syntax.Term.Plotkin\n {B : Set {- of base types -}}\n {C : Context.Context {Type.Type B} \u2192 Type.Type B \u2192 Set {- of constants -}}\n where\n\n-- Terms of languages described in Plotkin style\n\nopen import Function using (_\u2218_)\nopen import Data.Product\n\nopen Type B\nopen Context {Type}\n\nopen import Denotation.Environment Type\nopen import Syntax.Context.Plotkin B\n\n-- Declarations of Term and Terms to enable mutual recursion\ndata Term\n (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set\n\ndata Terms\n (\u0393 : Context) :\n (\u03a3 : Context) \u2192 Set\n\n-- (Term \u0393 \u03c4) represents a term of type \u03c4\n-- with free variables bound in \u0393.\ndata Term \u0393 where\n const : \u2200 {\u03a3 \u03c4} \u2192\n (c : C \u03a3 \u03c4) \u2192\n Terms \u0393 \u03a3 \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\n-- (Terms \u0393 \u03a3) represents a list of terms with types from \u03a3\n-- with free variables bound in \u0393.\ndata Terms \u0393 where\n \u2205 : Terms \u0393 \u2205\n _\u2022_ : \u2200 {\u03c4 \u03a3} \u2192\n Term \u0393 \u03c4 \u2192\n Terms \u0393 \u03a3 \u2192\n Terms \u0393 (\u03c4 \u2022 \u03a3)\n\ninfixr 9 _\u2022_\n\n-- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n-- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\nlift-diff-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4) \u00d7 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-diff-apply diff apply {base \u03b9} = diff , apply\nlift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\nlift-diff : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4)\n\nlift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\nlift-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n-- Weakening\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\n\nweakenAll : \u2200 {\u0393\u2081 \u0393\u2082 \u03a3} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Terms \u0393\u2081 \u03a3 \u2192\n Terms \u0393\u2082 \u03a3\n\nweaken \u0393\u2081\u227c\u0393\u2082 (const c ts) = const c (weakenAll \u0393\u2081\u227c\u0393\u2082 ts)\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (lift \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\nweakenAll \u0393\u2081\u227c\u0393\u2082 \u2205 = \u2205\nweakenAll \u0393\u2081\u227c\u0393\u2082 (t \u2022 ts) = weaken \u0393\u2081\u227c\u0393\u2082 t \u2022 weakenAll \u0393\u2081\u227c\u0393\u2082 ts\n\n-- Specialized weakening\nweaken\u2081 : \u2200 {\u0393 \u03c3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4\nweaken\u2081 t = weaken (drop _ \u2022 \u227c-refl) t\n\nweaken\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u0393) \u03c4\nweaken\u2082 t = weaken (drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\nweaken\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u03b3 \u2022 \u0393) \u03c4\nweaken\u2083 t = weaken (drop _ \u2022 drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n-- Shorthands for nested applications\napp\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3\napp\u2082 f x = app (app f x)\n\napp\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4\napp\u2083 f x = app\u2082 (app f x)\n\napp\u2084 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5\napp\u2084 f x = app\u2083 (app f x)\n\napp\u2085 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6\napp\u2085 f x = app\u2084 (app f x)\n\napp\u2086 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6 \u21d2 \u03b7) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6 \u2192 Term \u0393 \u03b7\napp\u2086 f x = app\u2085 (app f x)\n\nUncurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nUncurriedTermConstructor \u0393 \u03a3 \u03c4 = Terms \u0393 \u03a3 \u2192 Term \u0393 \u03c4\n\nCurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nCurriedTermConstructor \u0393 \u2205 \u03c4\u2032 = Term \u0393 \u03c4\u2032\nCurriedTermConstructor \u0393 (\u03c4 \u2022 \u03a3) \u03c4\u2032 = Term \u0393 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\u2032\n\n-- helper for lift-\u03b7-const, don't try to understand at home\nlift-\u03b7-const-rec : \u2200 {\u03a3 \u0393 \u03c4} \u2192 UncurriedTermConstructor \u0393 \u03a3 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const-rec {\u2205} k = k \u2205\nlift-\u03b7-const-rec {\u03c4 \u2022 \u03a3} k = \u03bb t \u2192 lift-\u03b7-const-rec (\u03bb ts \u2192 k (t \u2022 ts))\n\nlift-\u03b7-const : \u2200 {\u03a3 \u03c4} \u2192 C \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const constant = lift-\u03b7-const-rec (const constant)\n","old_contents":"import Syntax.Type.Plotkin as Type\nimport Syntax.Context as Context\n\nmodule Syntax.Term.Plotkin\n {B : Set {- of base types -}}\n {C : Context.Context {Type.Type B} \u2192 Type.Type B \u2192 Set {- of constants -}}\n where\n\n-- Terms of languages described in Plotkin style\n\nopen import Function using (_\u2218_)\nopen import Data.Product\n\nopen Type B\nopen Context {Type}\n\nopen import Denotation.Environment Type\nopen import Syntax.Context.Plotkin B\n\n-- Declarations of Term and Terms to enable mutual recursion\ndata Term\n (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set\n\ndata Terms\n (\u0393 : Context) :\n (\u03a3 : Context) \u2192 Set\n\n-- (Term \u0393 \u03c4) represents a term of type \u03c4\n-- with free variables bound in \u0393.\ndata Term \u0393 where\n const : \u2200 {\u03a3 \u03c4} \u2192\n (c : C \u03a3 \u03c4) \u2192\n Terms \u0393 \u03a3 \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\n-- (Terms \u0393 \u03a3) represents a list of terms with types from \u03a3\n-- with free variables bound in \u0393.\ndata Terms \u0393 where\n \u2205 : Terms \u0393 \u2205\n _\u2022_ : \u2200 {\u03c4 \u03a3} \u2192\n Term \u0393 \u03c4 \u2192\n Terms \u0393 \u03a3 \u2192\n Terms \u0393 (\u03c4 \u2022 \u03a3)\n\ninfixr 9 _\u2022_\n\n-- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n-- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\nlift-diff-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192\n term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4) \u00d7 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-diff-apply diff apply {base \u03b9} = diff , apply\nlift-diff-apply diff apply {\u03c3 \u21d2 \u03c4} =\n let\n -- for diff\n g = var (that (that (that this)))\n f = var (that (that this))\n x = var (that this)\n \u0394x = var this\n -- for apply\n \u0394h = var (that (that this))\n h = var (that this)\n y = var this\n -- syntactic sugars\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c3} {\u0393})\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb s t \u2192 app (app diff\u03c3 s) t\n _\u229d\u03c4_ = \u03bb s t \u2192 app (app diff\u03c4 s) t\n _\u2295\u03c3_ = \u03bb t \u0394t \u2192 app (app apply\u03c3 \u0394t) t\n _\u2295\u03c4_ = \u03bb t \u0394t \u2192 app (app apply\u03c4 \u0394t) t\n in\n abs (abs (abs (abs (app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))))\n ,\n abs (abs (abs (app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y))))\n\nlift-diff : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394type \u03c4)\n\nlift-diff diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\nlift-apply : \u2200 {\u0394base : B \u2192 Type} \u2192\n let\n \u0394type = lift-\u0394type \u0394base\n term = Term\n in\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394type (base \u03b9))) \u2192\n (\u2200 {\u03b9 \u0393} \u2192 term \u0393 (\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)) \u2192\n \u2200 {\u03c4 \u0393} \u2192 term \u0393 (\u0394type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\nlift-apply diff apply = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply diff apply {\u03c4} {\u0393})\n\n-- Weakening\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Term \u0393\u2081 \u03c4 \u2192\n Term \u0393\u2082 \u03c4\n\nweakenAll : \u2200 {\u0393\u2081 \u0393\u2082 \u03a3} \u2192\n (\u0393\u2081\u227c\u0393\u2082 : \u0393\u2081 \u227c \u0393\u2082) \u2192\n Terms \u0393\u2081 \u03a3 \u2192\n Terms \u0393\u2082 \u03a3\n\nweaken \u0393\u2081\u227c\u0393\u2082 (const c ts) = const c (weakenAll \u0393\u2081\u227c\u0393\u2082 ts)\nweaken \u0393\u2081\u227c\u0393\u2082 (var x) = var (lift \u0393\u2081\u227c\u0393\u2082 x)\nweaken \u0393\u2081\u227c\u0393\u2082 (app s t) = app (weaken \u0393\u2081\u227c\u0393\u2082 s) (weaken \u0393\u2081\u227c\u0393\u2082 t)\nweaken \u0393\u2081\u227c\u0393\u2082 (abs {\u03c3} t) = abs (weaken (keep \u03c3 \u2022 \u0393\u2081\u227c\u0393\u2082) t)\n\nweakenAll \u0393\u2081\u227c\u0393\u2082 \u2205 = \u2205\nweakenAll \u0393\u2081\u227c\u0393\u2082 (t \u2022 ts) = weaken \u0393\u2081\u227c\u0393\u2082 t \u2022 weakenAll \u0393\u2081\u227c\u0393\u2082 ts\n\n-- Specialized weakening\nweaken\u2081 : \u2200 {\u0393 \u03c3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4\nweaken\u2081 t = weaken (drop _ \u2022 \u227c-refl) t\n\nweaken\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u0393) \u03c4\nweaken\u2082 t = weaken (drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\nweaken\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03c4} \u2192\n Term \u0393 \u03c4 \u2192 Term (\u03b1 \u2022 \u03b2 \u2022 \u03b3 \u2022 \u0393) \u03c4\nweaken\u2083 t = weaken (drop _ \u2022 drop _ \u2022 drop _ \u2022 \u227c-refl) t\n\n-- Shorthands for nested applications\napp\u2082 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3\napp\u2082 f x = app (app f x)\n\napp\u2083 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4\napp\u2083 f x = app\u2082 (app f x)\n\napp\u2084 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5\napp\u2084 f x = app\u2083 (app f x)\n\napp\u2085 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6\napp\u2085 f x = app\u2084 (app f x)\n\napp\u2086 : \u2200 {\u0393 \u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7} \u2192\n Term \u0393 (\u03b1 \u21d2 \u03b2 \u21d2 \u03b3 \u21d2 \u03b4 \u21d2 \u03b5 \u21d2 \u03b6 \u21d2 \u03b7) \u2192\n Term \u0393 \u03b1 \u2192 Term \u0393 \u03b2 \u2192 Term \u0393 \u03b3 \u2192 Term \u0393 \u03b4 \u2192\n Term \u0393 \u03b5 \u2192 Term \u0393 \u03b6 \u2192 Term \u0393 \u03b7\napp\u2086 f x = app\u2085 (app f x)\n\nCurriedTermConstructor : (\u0393 \u03a3 : Context) (\u03c4 : Type) \u2192 Set\nCurriedTermConstructor \u0393 \u2205 \u03c4\u2032 = Term \u0393 \u03c4\u2032\nCurriedTermConstructor \u0393 (\u03c4 \u2022 \u03a3) \u03c4\u2032 = Term \u0393 \u03c4 \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\u2032\n\n-- helper for lift-\u03b7-const, don't try to understand at home\nlift-\u03b7-const-rec : \u2200 {\u03a3 \u0393 \u03c4} \u2192 (Terms \u0393 \u03a3 \u2192 Term \u0393 \u03c4) \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const-rec {\u2205} k = k \u2205\nlift-\u03b7-const-rec {\u03c4 \u2022 \u03a3} k = \u03bb t \u2192 lift-\u03b7-const-rec (\u03bb ts \u2192 k (t \u2022 ts))\n\nlift-\u03b7-const : \u2200 {\u03a3 \u03c4} \u2192 C \u03a3 \u03c4 \u2192 \u2200 {\u0393} \u2192 CurriedTermConstructor \u0393 \u03a3 \u03c4\nlift-\u03b7-const constant = lift-\u03b7-const-rec (const constant)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f520724186899be6fbfeda9feea90380e16bd696","subject":"Removed an unproven conjecture.","message":"Removed an unproven conjecture.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/PA\/Axiomatic\/Mendelson\/Properties\/UnprovedATP.agda","new_file":"src\/fot\/PA\/Axiomatic\/Mendelson\/Properties\/UnprovedATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Unproven PA properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule PA.Axiomatic.Mendelson.Properties.UnprovedATP where\n\nopen import PA.Axiomatic.Mendelson.Base\nopen import PA.Axiomatic.Mendelson.PropertiesATP\n\n------------------------------------------------------------------------------\n\n+-asocc : \u2200 m n o \u2192 m + n + o \u2248 m + (n + o)\n+-asocc m n o = S\u2089 A A0 is m\n where\n A : \u2115 \u2192 Set\n A i = i + n + o \u2248 i + (n + o)\n {-# ATP definition A #-}\n\n postulate A0 : A zero\n {-# ATP prove A0 +-leftCong #-}\n\n -- 25 November 2013: Vampire 0.6 proves the theorem using a time out\n -- of (300 sec).\n postulate is : \u2200 i \u2192 A i \u2192 A (succ i)\n -- {-# ATP prove is +-leftCong #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Unproven PA properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule PA.Axiomatic.Mendelson.Properties.UnprovedATP where\n\nopen import PA.Axiomatic.Mendelson.Base\nopen import PA.Axiomatic.Mendelson.PropertiesATP\n\n------------------------------------------------------------------------------\n\n+-asocc : \u2200 m n o \u2192 m + n + o \u2248 m + (n + o)\n+-asocc m n o = S\u2089 A A0 is m\n where\n A : \u2115 \u2192 Set\n A i = i + n + o \u2248 i + (n + o)\n {-# ATP definition A #-}\n\n postulate A0 : A zero\n {-# ATP prove A0 +-leftCong #-}\n\n -- 25 November 2013: Vampire 0.6 proves the theorem using a time out\n -- of (300 sec).\n postulate is : \u2200 i \u2192 A i \u2192 A (succ i)\n {-# ATP prove is +-leftCong #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"81c758c5545a825b9152e8e2ab898c688bf0d4a9","subject":"Solver.Linear.Parser: comment out the example (long to check)","message":"Solver.Linear.Parser: comment out the example (long to check)\n","repos":"crypto-agda\/crypto-agda","old_file":"Solver\/Linear\/Parser.agda","new_file":"Solver\/Linear\/Parser.agda","new_contents":"open import Data.Vec as Vec using ([]; _\u2237_)\nopen import Text.Parser\nopen import Data.One\nopen import Data.Char\nopen import Data.Nat\nopen import Data.List\nopen import Data.Product hiding (map)\nopen import Data.String as String renaming (toList to S\u25b9L; fromList to L\u25b9S)\nopen import Solver.Linear.Syntax\nopen import Data.Maybe.NP hiding (Eq)\nopen import Relation.Binary.PropositionalEquality\n\nmodule Solver.Linear.Parser where\n\nspaces : Parser \ud835\udfd9\nspaces = manyOneOf\u02e2 \" \\t\\n\\r\" *> pure _\n\npIdent : Parser String\npIdent = someNoneOf\u02e2 \"(,)\u21a6_ \\t\\n\\r\" <* spaces\n\ntok\u1d9c : Char \u2192 Parser \ud835\udfd9\ntok\u1d9c c = char c <* spaces\n\ntok\u02e2 : String \u2192 Parser \ud835\udfd9\ntok\u02e2 s = string s <* spaces\n\n--pPair : \u2200 {A B} \u2192 Parser A \u2192 Parser B \u2192 Parser (A \u00d7 B)\n--pPair pA pB = tok\u1d9c '(' *> \u27ea _,_ \u00b7 pA <* tok\u1d9c ',' \u00b7 pB \u27eb <* tok\u1d9c ')'\n\nmodule _ {A} (pA : Parser A) where\n\n pSimpleSyn pSyn : \u2115 \u2192 Parser (Syn A)\n\n pSimpleSyn n = pure tt <* oneOf\u02e2 \"_\ud835\udfd9\"\n \u27e8|\u27e9 tok\u1d9c '(' *> pSyn n <* tok\u1d9c ')'\n \u27e8|\u27e9 \u27ea var \u00b7 pA \u27eb\n\n pSyn zero = empty\n pSyn (suc n) = \u27ea tuple _ \u00b7 pSimpleSyn n \u00b7 many n (tok\u1d9c ',' *> pSyn n) \u27eb\n\n pEq : \u2115 \u2192 Parser (Eq A)\n pEq n = \u27ea _\u21a6_ \u00b7 pSyn n <* tok\u1d9c '\u21a6' \u00b7 pSyn n \u27eb\n\npSyn\u02e2 : \u2115 \u2192 Parser (Syn String)\npSyn\u02e2 = pSyn pIdent\n\npEq\u02e2 : \u2115 \u2192 Parser (Eq String)\npEq\u02e2 = pEq pIdent\n\nparseSyn\u02e2? : String \u2192? Syn String\nparseSyn\u02e2? s = runParser (pSyn\u02e2 n <* eof) \u2113\n where \u2113 = S\u25b9L s\n n = length \u2113\n\nparseSyn\u02e2 : T[ parseSyn\u02e2? ]\nparseSyn\u02e2 = F[ parseSyn\u02e2? ]\n\nparseEq\u02e2? : String \u2192? Eq String\nparseEq\u02e2? s = runParser (spaces *> pEq\u02e2 (2 * n) <* eof) \u2113\n where \u2113 = S\u25b9L s\n n = length \u2113\n\nparseEq\u02e2 : T[ parseEq\u02e2? ]\nparseEq\u02e2 = F[ parseEq\u02e2? ]\n\nex-A\u21d2B : Eq String\nex-A\u21d2B = parseEq\u02e2 \"A\u21a6B\"\n\nex-\ud835\udfd9\u21d2\ud835\udfd9 : Eq String\nex-\ud835\udfd9\u21d2\ud835\udfd9 = parseEq\u02e2 \"\ud835\udfd9\u21a6\ud835\udfd9\"\n\nex\u2081 : Eq String\nex\u2081 = parseEq\u02e2 \" ( A , B,(\ud835\udfd9,C),(D))\u21a6 B,C , A\"\n\n{-\ntest-ex\u2081 : ex\u2081 \u2261 ((var\"A\" , (var\"B\", (tt , var\"C\") , var\"D\"))\n \u21a6 (var\"B\" , var\"C\" , var\"A\"))\ntest-ex\u2081 = refl\n-}\n\n-- -}\n","old_contents":"open import Data.Vec as Vec using ([]; _\u2237_)\nopen import Text.Parser\nopen import Data.One\nopen import Data.Char\nopen import Data.Nat\nopen import Data.List\nopen import Data.Product hiding (map)\nopen import Data.String as String renaming (toList to S\u25b9L; fromList to L\u25b9S)\nopen import Solver.Linear.Syntax\nopen import Data.Maybe.NP hiding (Eq)\nopen import Relation.Binary.PropositionalEquality\n\nmodule Solver.Linear.Parser where\n\nspaces : Parser \ud835\udfd9\nspaces = manyOneOf\u02e2 \" \\t\\n\\r\" *> pure _\n\npIdent : Parser String\npIdent = someNoneOf\u02e2 \"(,)\u21a6_ \\t\\n\\r\" <* spaces\n\ntok\u1d9c : Char \u2192 Parser \ud835\udfd9\ntok\u1d9c c = char c <* spaces\n\ntok\u02e2 : String \u2192 Parser \ud835\udfd9\ntok\u02e2 s = string s <* spaces\n\n--pPair : \u2200 {A B} \u2192 Parser A \u2192 Parser B \u2192 Parser (A \u00d7 B)\n--pPair pA pB = tok\u1d9c '(' *> \u27ea _,_ \u00b7 pA <* tok\u1d9c ',' \u00b7 pB \u27eb <* tok\u1d9c ')'\n\nmodule _ {A} (pA : Parser A) where\n\n pSimpleSyn pSyn : \u2115 \u2192 Parser (Syn A)\n\n pSimpleSyn n = pure tt <* oneOf\u02e2 \"_\ud835\udfd9\"\n \u27e8|\u27e9 tok\u1d9c '(' *> pSyn n <* tok\u1d9c ')'\n \u27e8|\u27e9 \u27ea var \u00b7 pA \u27eb\n\n pSyn zero = empty\n pSyn (suc n) = \u27ea tuple _ \u00b7 pSimpleSyn n \u00b7 many n (tok\u1d9c ',' *> pSyn n) \u27eb\n\n pEq : \u2115 \u2192 Parser (Eq A)\n pEq n = \u27ea _\u21a6_ \u00b7 pSyn n <* tok\u1d9c '\u21a6' \u00b7 pSyn n \u27eb\n\npSyn\u02e2 : \u2115 \u2192 Parser (Syn String)\npSyn\u02e2 = pSyn pIdent\n\npEq\u02e2 : \u2115 \u2192 Parser (Eq String)\npEq\u02e2 = pEq pIdent\n\nparseSyn\u02e2? : String \u2192? Syn String\nparseSyn\u02e2? s = runParser (pSyn\u02e2 n <* eof) \u2113\n where \u2113 = S\u25b9L s\n n = length \u2113\n\nparseSyn\u02e2 : T[ parseSyn\u02e2? ]\nparseSyn\u02e2 = F[ parseSyn\u02e2? ]\n\nparseEq\u02e2? : String \u2192? Eq String\nparseEq\u02e2? s = runParser (spaces *> pEq\u02e2 (2 * n) <* eof) \u2113\n where \u2113 = S\u25b9L s\n n = length \u2113\n\nparseEq\u02e2 : T[ parseEq\u02e2? ]\nparseEq\u02e2 = F[ parseEq\u02e2? ]\n\nex-A\u21d2B : Eq String\nex-A\u21d2B = parseEq\u02e2 \"A\u21a6B\"\n\nex-\ud835\udfd9\u21d2\ud835\udfd9 : Eq String\nex-\ud835\udfd9\u21d2\ud835\udfd9 = parseEq\u02e2 \"\ud835\udfd9\u21a6\ud835\udfd9\"\n\nex\u2081 : Eq String\nex\u2081 = parseEq\u02e2 \" ( A , B,(\ud835\udfd9,C),(D))\u21a6 B,C , A\"\n\ntest-ex\u2081 : ex\u2081 \u2261 ((var\"A\" , (var\"B\", (tt , var\"C\") , var\"D\"))\n \u21a6 (var\"B\" , var\"C\" , var\"A\"))\ntest-ex\u2081 = refl\n\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"e2eea833ff64ff0f23fff6c4f3d997a65c4b5ef8","subject":"'with' pattern matching","message":"'with' pattern matching\n","repos":"shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps","old_file":"agda-tutorial\/basics.agda","new_file":"agda-tutorial\/basics.agda","new_contents":"data Bool : Set where\n true : Bool\n false : Bool\n\n\nnot : Bool \u2192 Bool\nnot true = false\nnot false = true\n\n\ndata \u2115 : Set where\n O : \u2115\n S : \u2115 \u2192 \u2115\n\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nO + a = a\nS a + b = S (a + b)\n\n_*_ : \u2115 \u2192 \u2115 \u2192 \u2115\nO * a = O\nS a * b = a + (a * b)\n\n_or_ : Bool \u2192 Bool \u2192 Bool\ntrue or _ = true\nfalse or b = b\n\n\nif_then_else_ : {A : Set} \u2192 Bool \u2192 A \u2192 A \u2192 A\nif true then x else y = x\nif false then x else y = y\n\n\ninfixl 60 _*_\ninfixl 40 _+_\ninfixr 20 _or_\ninfix 5 if_then_else_\n\n\ninfixr 40 _::_\ndata List (A : Set) : Set where\n [] : List A\n _::_ : A -> List A -> List A\n\n\n_\u2218_ : {A : Set} -> {B : A -> Set} -> {C : (x : A) -> B x -> Set} ->\n (f : {x : A} -> (y : B x) -> C x y) -> (g : (x : A) -> B x) ->\n (x : A) -> C x (g x)\n_\u2218_ f g a = f (g a)\n\n\nplus-two = S \u2218 S\n\nmap : {A B : Set} -> (A -> B) -> List A -> List B\nmap f [] = []\nmap f (x :: xs) = f x :: map f xs\n\n_++_ : {A : Set} -> List A -> List A -> List A\n[] ++ ys = ys\nx :: xs ++ ys = x :: (xs ++ ys)\n\n\ndata Vec (A : Set) : \u2115 -> Set where\n nil : Vec A O\n cons : (n : \u2115) -> A -> Vec A n -> Vec A (S n)\n\nhead : {A : Set} {n : \u2115} -> Vec A (S n) -> A\nhead (cons n v vs) = v\n\n\nvmap : {A B : Set} (n : \u2115) -> (A -> B) -> Vec A n -> Vec B n\nvmap .O f nil = nil\nvmap .(S n) f (cons n x xs) = cons n (f x) (vmap n f xs)\n\n\nvmap\u2032 : {A B : Set} (n : \u2115) -> (A -> B) -> Vec A n -> Vec B n\nvmap\u2032 O f nil = nil\nvmap\u2032 (S n) f (cons .n x xs) = cons n (f x) (vmap n f xs)\n\n\ndata Fin : \u2115 -> Set where\n fzero : {n : \u2115} -> Fin (S n)\n fsuc : {n : \u2115} -> Fin n -> Fin (S n)\n\n_!_ : {n : \u2115}{A : Set} -> Vec A n -> Fin n -> A\nnil ! ()\ncons n x a ! fzero = x\ncons n x a ! fsuc b = a ! b\n\n\n\ntabulate : {n : \u2115}{A : Set} -> (Fin n -> A) -> Vec A n\ntabulate {O} f = nil\ntabulate {S n} f = cons n (f fzero) (tabulate (f \u2218 fsuc))\n\n\ndata False : Set where\nrecord True : Set where\n\n\ntrivial : True\ntrivial = _\n\n\nisTrue : Bool -> Set\nisTrue true = True\nisTrue false = False\n\n\n_<_ : \u2115 -> \u2115 -> Bool\n_ < O = false\nO < S n = true\nS m < S n = m < n\n\nlength : {A : Set} -> List A -> \u2115\nlength [] = O\nlength (x :: xs) = S (length xs)\n\nlookup : {A : Set}(xs : List A)(n : \u2115) -> isTrue (n < length xs) -> A\nlookup (x :: xs) O b = x\nlookup (x :: xs) (S n) b = lookup xs n b\nlookup [] a ()\n\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\n\ndata _\u2264_ : \u2115 -> \u2115 -> Set where\n \u2264O : {m n : \u2115} -> m == n -> m \u2264 n\n \u2264I : {m n : \u2115} -> m \u2264 n -> m \u2264 S n\n\n\nleq-trans : {l m n : \u2115} -> l \u2264 m -> m \u2264 n -> l \u2264 n\nleq-trans a (\u2264O refl) = a\nleq-trans a (\u2264I b) = \u2264I (leq-trans a b)\n\n\nmin : \u2115 -> \u2115 -> \u2115\nmin a b with a < b\nmin a b | true = a\nmin a b | false = b\n\n\nfilter : {A : Set} -> (A -> Bool) -> List A -> List A\nfilter f [] = []\nfilter f (x :: xs) with f x\n... | true = x :: filter f xs\n... | false = filter f xs\n\n\ndata _\u2260_ : \u2115 -> \u2115 -> Set where\n z\u2260s : {n : \u2115} -> O \u2260 S n\n s\u2260z : {n : \u2115} -> S n \u2260 O\n s\u2260s : {m n : \u2115} -> n \u2260 m \u2192 S n \u2260 S m\n\n\ndata Equal? (n m : \u2115) : Set where\n eq : n == m -> Equal? n m\n neq : n \u2260 m -> Equal? n m\n","old_contents":"data Bool : Set where\n true : Bool\n false : Bool\n\n\nnot : Bool \u2192 Bool\nnot true = false\nnot false = true\n\n\ndata \u2115 : Set where\n O : \u2115\n S : \u2115 \u2192 \u2115\n\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nO + a = a\nS a + b = S (a + b)\n\n_*_ : \u2115 \u2192 \u2115 \u2192 \u2115\nO * a = O\nS a * b = a + (a * b)\n\n_or_ : Bool \u2192 Bool \u2192 Bool\ntrue or _ = true\nfalse or b = b\n\n\nif_then_else_ : {A : Set} \u2192 Bool \u2192 A \u2192 A \u2192 A\nif true then x else y = x\nif false then x else y = y\n\n\ninfixl 60 _*_\ninfixl 40 _+_\ninfixr 20 _or_\ninfix 5 if_then_else_\n\n\ninfixr 40 _::_\ndata List (A : Set) : Set where\n [] : List A\n _::_ : A -> List A -> List A\n\n\n_\u2218_ : {A : Set} -> {B : A -> Set} -> {C : (x : A) -> B x -> Set} ->\n (f : {x : A} -> (y : B x) -> C x y) -> (g : (x : A) -> B x) ->\n (x : A) -> C x (g x)\n_\u2218_ f g a = f (g a)\n\n\nplus-two = S \u2218 S\n\nmap : {A B : Set} -> (A -> B) -> List A -> List B\nmap f [] = []\nmap f (x :: xs) = f x :: map f xs\n\n_++_ : {A : Set} -> List A -> List A -> List A\n[] ++ ys = ys\nx :: xs ++ ys = x :: (xs ++ ys)\n\n\ndata Vec (A : Set) : \u2115 -> Set where\n nil : Vec A O\n cons : (n : \u2115) -> A -> Vec A n -> Vec A (S n)\n\nhead : {A : Set} {n : \u2115} -> Vec A (S n) -> A\nhead (cons n v vs) = v\n\n\nvmap : {A B : Set} (n : \u2115) -> (A -> B) -> Vec A n -> Vec B n\nvmap .O f nil = nil\nvmap .(S n) f (cons n x xs) = cons n (f x) (vmap n f xs)\n\n\nvmap\u2032 : {A B : Set} (n : \u2115) -> (A -> B) -> Vec A n -> Vec B n\nvmap\u2032 O f nil = nil\nvmap\u2032 (S n) f (cons .n x xs) = cons n (f x) (vmap n f xs)\n\n\ndata Fin : \u2115 -> Set where\n fzero : {n : \u2115} -> Fin (S n)\n fsuc : {n : \u2115} -> Fin n -> Fin (S n)\n\n_!_ : {n : \u2115}{A : Set} -> Vec A n -> Fin n -> A\nnil ! ()\ncons n x a ! fzero = x\ncons n x a ! fsuc b = a ! b\n\n\n\ntabulate : {n : \u2115}{A : Set} -> (Fin n -> A) -> Vec A n\ntabulate {O} f = nil\ntabulate {S n} f = cons n (f fzero) (tabulate (f \u2218 fsuc))\n\n\ndata False : Set where\nrecord True : Set where\n\n\ntrivial : True\ntrivial = _\n\n\nisTrue : Bool -> Set\nisTrue true = True\nisTrue false = False\n\n\n_<_ : \u2115 -> \u2115 -> Bool\n_ < O = false\nO < S n = true\nS m < S n = m < n\n\nlength : {A : Set} -> List A -> \u2115\nlength [] = O\nlength (x :: xs) = S (length xs)\n\nlookup : {A : Set}(xs : List A)(n : \u2115) -> isTrue (n < length xs) -> A\nlookup (x :: xs) O b = x\nlookup (x :: xs) (S n) b = lookup xs n b\nlookup [] a ()\n\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\n\ndata _\u2264_ : \u2115 -> \u2115 -> Set where\n \u2264O : {m n : \u2115} -> m == n -> m \u2264 n\n \u2264I : {m n : \u2115} -> m \u2264 n -> m \u2264 S n\n\n\nleq-trans : {l m n : \u2115} -> l \u2264 m -> m \u2264 n -> l \u2264 n\nleq-trans a (\u2264O refl) = a\nleq-trans a (\u2264I b) = \u2264I (leq-trans a b)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"90198c4b819b244f1e0cf351e13107ce74303049","subject":"\u00d7 and + are commutative monoids of setoids","message":"\u00d7 and + are commutative monoids of setoids\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Function\/Related\/TypeIsomorphisms\/NP.agda","new_file":"lib\/Function\/Related\/TypeIsomorphisms\/NP.agda","new_contents":"module Function.Related.TypeIsomorphisms.NP where\n\nimport Level as L\nopen import Algebra\nimport Algebra.FunctionProperties as FP\nopen L using (Lift; lower; lift)\nopen import Type\n\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Vec using (Vec; []; _\u2237_)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_) renaming (_^_ to _**_)\nopen import Data.Maybe.NP\nopen import Data.Product renaming (map to map\u00d7)\n--open import Data.Product.N-ary\nopen import Data.Sum renaming (map to map\u228e)\nopen import Data.Unit\nopen import Data.Empty\n\nimport Function as F\nimport Function.Equality as FE\nopen FE using (_\u27e8$\u27e9_)\nopen import Function.Related as FR\nopen import Function.Related.TypeIsomorphisms public\nimport Function.Inverse as Inv\nopen Inv using (_\u2194_; _\u2218_; sym; id; module Inverse)\nopen import Relation.Binary\nopen import Relation.Binary.Product.Pointwise\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (\u2192-to-\u27f6)\n\n\nInv-isEquivalence : IsEquivalence (Inv.Inverse {f\u2081 = L.zero} {f\u2082 = L.zero})\nInv-isEquivalence = record\n { refl = Inv.id\n ; sym = Inv.sym\n ; trans = F.flip Inv._\u2218_ }\n\nSEToid : Set _\nSEToid = Setoid L.zero L.zero\n\nSEToid\u2081 : Setoid _ _\nSEToid\u2081 = record\n { Carrier = Setoid L.zero L.zero\n ; _\u2248_ = Inv.Inverse\n ; isEquivalence = Inv-isEquivalence }\n\n\u228e-ICommutativeMonoid : CommutativeMonoid _ _\n\u228e-ICommutativeMonoid = record {\n Carrier = SEToid ;\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u228e-setoid_;\n \u03b5 = \u2261.setoid \u22a5;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv-isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u228e-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \u22a5) _\u228e-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = [ (\u03bb ()) , F.id ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = inj\u2082\n ; cong = \u2082\u223c\u2082\n }\n ; inverse-of = record\n { left-inverse-of = [ (\u03bb ()) , (\u03bb x \u2192 \u2082\u223c\u2082 refl) ]\n ; right-inverse-of = \u03bb x \u2192 refl\n }\n } where open Setoid A\n cong-to : Setoid._\u2248_ (\u2261.setoid \u22a5 \u228e-setoid A) =[ _ ]\u21d2 _\u2248_\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y) rewrite x\u223c\u2081y = refl\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = x\u223c\u2082y\n\n assoc : Associative _\u228e-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = [ [ inj\u2081 , inj\u2082 F.\u2218 inj\u2081 ] , inj\u2082 F.\u2218 inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = [ inj\u2081 F.\u2218 inj\u2081 , [ inj\u2081 F.\u2218 inj\u2082 , inj\u2082 ] ]\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = [ [ (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2081\u223c\u2081 (refl A))) , (\u03bb _ \u2192 \u2081\u223c\u2081 (\u2082\u223c\u2082 (refl B)) ) ] , (\u03bb _ \u2192 \u2082\u223c\u2082 (refl C)) ]\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (refl A)) , [ (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2081\u223c\u2081 (refl B))) , (\u03bb _ \u2192 \u2082\u223c\u2082 (\u2082\u223c\u2082 (refl C))) ] ]\n }\n } where\n open Setoid\n cong-to : _\u2248_ ((A \u228e-setoid B) \u228e-setoid C) =[ _ ]\u21d2 _\u2248_ (A \u228e-setoid (B \u228e-setoid C))\n cong-to (\u2081\u223c\u2082 ())\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2082 ()))\n cong-to (\u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 x\u223c\u2081y\n cong-to (\u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2082y)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y) = \u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)\n\n cong-from : _\u2248_ (A \u228e-setoid (B \u228e-setoid C)) =[ _ ]\u21d2 _\u2248_ ((A \u228e-setoid B) \u228e-setoid C)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 x\u223c\u2081y) = \u2081\u223c\u2081 (\u2081\u223c\u2081 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2082 ()))\n cong-from (\u2082\u223c\u2082 (\u2081\u223c\u2081 x\u223c\u2081y)) = \u2081\u223c\u2081 (\u2082\u223c\u2082 x\u223c\u2081y)\n cong-from (\u2082\u223c\u2082 (\u2082\u223c\u2082 x\u223c\u2082y)) = \u2082\u223c\u2082 x\u223c\u2082y\n\n comm : Commutative _\u228e-setoid_\n comm A B = record\n { to = swap'\n ; from = swap'\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B : SEToid} \u2192 A \u228e-setoid B FE.\u27f6 B \u228e-setoid A\n swap' {A} {B} = record\n { _\u27e8$\u27e9_ = [ inj\u2082 , inj\u2081 ]\n ; cong = cong\n } where\n cong : Setoid._\u2248_ (A \u228e-setoid B) =[ _ ]\u21d2 Setoid._\u2248_ (B \u228e-setoid A)\n cong (\u2081\u223c\u2082 ())\n cong (\u2081\u223c\u2081 x\u223c\u2081y) = \u2082\u223c\u2082 x\u223c\u2081y\n cong (\u2082\u223c\u2082 x\u223c\u2082y) = \u2081\u223c\u2081 x\u223c\u2082y\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u228e-setoid B) (swap' FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = [ (\u03bb _ \u2192 \u2081\u223c\u2081 (Setoid.refl A)) , (\u03bb _ \u2192 \u2082\u223c\u2082 (Setoid.refl B)) ]\n\n\u00d7-ICommutativeMonoid : CommutativeMonoid _ _\n\u00d7-ICommutativeMonoid = record {\n Carrier = SEToid ;\n _\u2248_ = Inv.Inverse;\n _\u2219_ = _\u00d7-setoid_;\n \u03b5 = \u2261.setoid \u22a4;\n isCommutativeMonoid = record\n { isSemigroup = record\n { isEquivalence = Inv-isEquivalence\n ; assoc = assoc\n ; \u2219-cong = _\u00d7-inverse_\n }\n ; identity\u02e1 = left-identity\n ; comm = comm\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n left-identity : LeftIdentity (\u2261.setoid \u22a4) _\u00d7-setoid_\n left-identity A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2082\n ; cong = proj\u2082\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u03bb x \u2192 _ , x\n ; cong = \u03bb x \u2192 \u2261.refl , x\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u2261.refl , (Setoid.refl A)\n ; right-inverse-of = \u03bb x \u2192 Setoid.refl A\n }\n }\n\n assoc : Associative _\u00d7-setoid_\n assoc A B C = record\n { to = record\n { _\u27e8$\u27e9_ = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n ; cong = < proj\u2081 F.\u2218 proj\u2081 , < proj\u2082 F.\u2218 proj\u2081 , proj\u2082 > >\n }\n ; from = record\n { _\u27e8$\u27e9_ = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n ; cong = < < proj\u2081 , proj\u2081 F.\u2218 proj\u2082 > , proj\u2082 F.\u2218 proj\u2082 >\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb _ \u2192 Setoid.refl ((A \u00d7-setoid B) \u00d7-setoid C)\n ; right-inverse-of = \u03bb _ \u2192 Setoid.refl (A \u00d7-setoid (B \u00d7-setoid C))\n }\n }\n\n comm : Commutative _\u00d7-setoid_\n comm A B = record\n { to = swap' {A} {B}\n ; from = swap' {B} {A}\n ; inverse-of = record\n { left-inverse-of = inv A B\n ; right-inverse-of = inv B A\n }\n } where\n swap' : \u2200 {A B : SEToid} \u2192 A \u00d7-setoid B FE.\u27f6 B \u00d7-setoid A\n swap' {A}{B} = record { _\u27e8$\u27e9_ = swap; cong = swap }\n\n inv : \u2200 A B \u2192 \u2200 x \u2192 Setoid._\u2248_ (A \u00d7-setoid B) (swap' {B} {A} FE.\u2218 swap' {A} {B}\u27e8$\u27e9 x) x\n inv A B = \u03bb x \u2192 Setoid.refl (A \u00d7-setoid B)\n\n\u00d7\u228e-ICommutativeSemiring : CommutativeSemiring _ _\n\u00d7\u228e-ICommutativeSemiring = record\n { Carrier = SEToid\n ; _\u2248_ = Inv.Inverse\n ; _+_ = _\u228e-setoid_\n ; _*_ = _\u00d7-setoid_\n ; 0# = \u2261.setoid \u22a5\n ; 1# = \u2261.setoid \u22a4\n ; isCommutativeSemiring = record\n { +-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u228e-ICommutativeMonoid\n ; *-isCommutativeMonoid = CommutativeMonoid.isCommutativeMonoid \u00d7-ICommutativeMonoid\n ; distrib\u02b3 = distrib\u02b3\n ; zero\u02e1 = zero\u02e1\n }\n }\n where\n open FP (Inv.Inverse {f\u2081 = L.zero}{f\u2082 = L.zero})\n\n distrib\u02b3 : _\u00d7-setoid_ DistributesOver\u02b3 _\u228e-setoid_\n distrib\u02b3 A B C = record\n { to = record\n { _\u27e8$\u27e9_ = uncurry [ curry inj\u2081 , curry inj\u2082 ]\n ; cong = cong-to\n }\n ; from = record\n { _\u27e8$\u27e9_ = from\n ; cong = cong-from\n }\n ; inverse-of = record\n { left-inverse-of = uncurry [ (\u03bb x y \u2192 (\u2081\u223c\u2081 (refl B)) , (refl A)) , (\u03bb x y \u2192 (\u2082\u223c\u2082 (refl C)) , (refl A)) ]\n ; right-inverse-of = [ uncurry (\u03bb x y \u2192 \u2081\u223c\u2081 ((refl B {x}) , (refl A {y})))\n , uncurry (\u03bb x y \u2192 \u2082\u223c\u2082 ((refl C {x}) , (refl A {y}))) ]\n }\n } where\n open Setoid\n A' = Carrier A\n B' = Carrier B\n C' = Carrier C\n\n cong-to : _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A) =[ _ ]\u21d2 _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A))\n cong-to (\u2081\u223c\u2082 () , _)\n cong-to (\u2081\u223c\u2081 x\u223c\u2081y , A-rel) = \u2081\u223c\u2081 (x\u223c\u2081y , A-rel)\n cong-to (\u2082\u223c\u2082 x\u223c\u2082y , A-rel) = \u2082\u223c\u2082 (x\u223c\u2082y , A-rel)\n\n from : B' \u00d7 A' \u228e C' \u00d7 A' \u2192 (B' \u228e C') \u00d7 A'\n from = [ map\u00d7 inj\u2081 F.id , map\u00d7 inj\u2082 F.id ]\n\n cong-from : _\u2248_ ((B \u00d7-setoid A) \u228e-setoid (C \u00d7-setoid A)) =[ _ ]\u21d2 _\u2248_ ((B \u228e-setoid C) \u00d7-setoid A)\n cong-from (\u2081\u223c\u2082 ())\n cong-from (\u2081\u223c\u2081 (B-rel , A-rel)) = (\u2081\u223c\u2081 B-rel , A-rel)\n cong-from (\u2082\u223c\u2082 (C-rel , A-rel)) = \u2082\u223c\u2082 C-rel , A-rel\n\n zero\u02e1 : LeftZero (\u2261.setoid \u22a5) _\u00d7-setoid_\n zero\u02e1 A = record\n { to = record\n { _\u27e8$\u27e9_ = proj\u2081\n ; cong = proj\u2081\n }\n ; from = record\n { _\u27e8$\u27e9_ = \u22a5-elim\n ; cong = \u03bb { {()} x }\n }\n ; inverse-of = record\n { left-inverse-of = \u03bb x \u2192 \u22a5-elim (proj\u2081 x)\n ; right-inverse-of = \u03bb x \u2192 \u22a5-elim x\n }\n }\n\nmodule \u00d7-CMon = CommutativeMonoid (\u00d7-CommutativeMonoid FR.bijection L.zero)\nmodule \u228e-CMon = CommutativeMonoid (\u228e-CommutativeMonoid FR.bijection L.zero)\nmodule \u00d7\u228e\u00b0 = CommutativeSemiring (\u00d7\u228e-CommutativeSemiring FR.bijection L.zero)\n\nmodule \u00d7-ICMon = CommutativeMonoid \u00d7-ICommutativeMonoid\nmodule \u228e-ICMon = CommutativeMonoid \u228e-ICommutativeMonoid\nmodule \u00d7\u228e\u00b0I = CommutativeSemiring \u00d7\u228e-ICommutativeSemiring\n\nswap-iso : \u2200 {A B} \u2192 (A \u00d7 B) \u2194 (B \u00d7 A)\nswap-iso = \u00d7-CMon.comm _ _\n\nMaybe\u2194\u22a4\u228e : \u2200 {a} {A : Set a} \u2192 Maybe A \u2194 (\u22a4 \u228e A)\nMaybe\u2194\u22a4\u228e\n = record { to = \u2192-to-\u27f6 (maybe inj\u2082 (inj\u2081 _))\n ; from = \u2192-to-\u27f6 [ F.const nothing , just ]\n ; inverse-of\n = record { left-inverse-of = maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ] } }\n\nMaybe-cong : \u2200 {a b} {A : Set a} {B : Set b} \u2192 A \u2194 B \u2192 Maybe A \u2194 Maybe B \nMaybe-cong A\u2194B = sym Maybe\u2194\u22a4\u228e \u2218 id \u228e-cong A\u2194B \u2218 Maybe\u2194\u22a4\u228e\n\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe : \u2200 {A} n \u2192 Maybe (Maybe^ n A) \u2194 Maybe^ n (Maybe A)\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe zero = id\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe (suc n) = Maybe-cong (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n)\n\nMaybe^-cong : \u2200 {A B} n \u2192 A \u2194 B \u2192 Maybe^ n A \u2194 Maybe^ n B\nMaybe^-cong zero = F.id\nMaybe^-cong (suc n) = Maybe-cong F.\u2218 Maybe^-cong n\n\nVec0\u2194\u22a4 : \u2200 {a} {A : Set a} \u2192 Vec A 0 \u2194 \u22a4\nVec0\u2194\u22a4 = record { to = \u2192-to-\u27f6 _\n ; from = \u2192-to-\u27f6 (F.const [])\n ; inverse-of = record { left-inverse-of = \u03bb { [] \u2192 \u2261.refl }\n ; right-inverse-of = \u03bb _ \u2192 \u2261.refl } }\n\nVec\u2218suc\u2194A\u00d7Vec : \u2200 {a} {A : Set a} {n} \u2192 Vec A (suc n) \u2194 (A \u00d7 Vec A n)\nVec\u2218suc\u2194A\u00d7Vec\n = record { to = \u2192-to-\u27f6 (\u03bb { (x \u2237 xs) \u2192 x , xs })\n ; from = \u2192-to-\u27f6 (uncurry _\u2237_)\n ; inverse-of = record { left-inverse-of = \u03bb { (x \u2237 xs) \u2192 \u2261.refl }\n ; right-inverse-of = \u03bb _ \u2192 \u2261.refl } }\n\ninfix 8 _^_\n\n_^_ : \u2605 \u2192 \u2115 \u2192 \u2605\nA ^ 0 = \u22a4\nA ^ suc n = A \u00d7 A ^ n\n\n^\u2194Vec : \u2200 {A} n \u2192 (A ^ n) \u2194 Vec A n\n^\u2194Vec zero = sym Vec0\u2194\u22a4\n^\u2194Vec (suc n) = sym Vec\u2218suc\u2194A\u00d7Vec \u2218 (id \u00d7-cong (^\u2194Vec n))\n\nFin0\u2194\u22a5 : Fin 0 \u2194 \u22a5\nFin0\u2194\u22a5 = record { to = \u2192-to-\u27f6 \u03bb()\n ; from = \u2192-to-\u27f6 \u03bb()\n ; inverse-of = record { left-inverse-of = \u03bb()\n ; right-inverse-of = \u03bb() } }\n\nFin\u2218suc\u2194Maybe\u2218Fin : \u2200 {n} \u2192 Fin (suc n) \u2194 Maybe (Fin n)\nFin\u2218suc\u2194Maybe\u2218Fin {n}\n = record { to = \u2192-to-\u27f6 to\n ; from = \u2192-to-\u27f6 (maybe suc zero)\n ; inverse-of\n = record { left-inverse-of = \u03bb { zero \u2192 \u2261.refl ; (suc n) \u2192 \u2261.refl }\n ; right-inverse-of = maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl } }\n where to : Fin (suc n) \u2192 Maybe (Fin n)\n to zero = nothing\n to (suc n) = just n\n\nLift\u2194id : \u2200 {a} {A : Set a} \u2192 Lift {a} {a} A \u2194 A\nLift\u2194id = record { to = \u2192-to-\u27f6 lower\n ; from = \u2192-to-\u27f6 lift\n ; inverse-of = record { left-inverse-of = \u03bb { (lift x) \u2192 \u2261.refl }\n ; right-inverse-of = \u03bb _ \u2192 \u2261.refl } }\n\n\u22a4\u00d7A\u2194A : \u2200 {A : \u2605} \u2192 (\u22a4 \u00d7 A) \u2194 A\n\u22a4\u00d7A\u2194A = proj\u2081 \u00d7-CMon.identity _ \u2218 sym Lift\u2194id \u00d7-cong id\n\nA\u00d7\u22a4\u2194A : \u2200 {A : \u2605} \u2192 (A \u00d7 \u22a4) \u2194 A\nA\u00d7\u22a4\u2194A = proj\u2082 \u00d7-CMon.identity _ \u2218 id \u00d7-cong sym Lift\u2194id\n\n\u22a5\u228eA\u2194A : \u2200 {A : \u2605} \u2192 (\u22a5 \u228e A) \u2194 A\n\u22a5\u228eA\u2194A = proj\u2081 \u228e-CMon.identity _ \u2218 sym Lift\u2194id \u228e-cong id\n\nA\u228e\u22a5\u2194A : \u2200 {A : \u2605} \u2192 (A \u228e \u22a5) \u2194 A\nA\u228e\u22a5\u2194A = proj\u2082 \u228e-CMon.identity _ \u2218 id \u228e-cong sym Lift\u2194id\n\n\u22a5\u00d7A\u2194\u22a5 : \u2200 {A : \u2605} \u2192 (\u22a5 \u00d7 A) \u2194 \u22a5\n\u22a5\u00d7A\u2194\u22a5 = Lift\u2194id \u2218 proj\u2081 \u00d7\u228e\u00b0.zero _ \u2218 sym (Lift\u2194id \u00d7-cong id)\n\nMaybe\u22a5\u2194\u22a4 : Maybe \u22a5 \u2194 \u22a4\nMaybe\u22a5\u2194\u22a4 = A\u228e\u22a5\u2194A \u2218 Maybe\u2194\u22a4\u228e\n\nMaybe^\u22a5\u2194Fin : \u2200 n \u2192 Maybe^ n \u22a5 \u2194 Fin n\nMaybe^\u22a5\u2194Fin zero = sym Fin0\u2194\u22a5\nMaybe^\u22a5\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\u22a5\u2194Fin n)\n\nMaybe^\u22a4\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \u22a4 \u2194 Fin (suc n)\nMaybe^\u22a4\u2194Fin1+ n = Maybe^\u22a5\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\u22a5\u2194\u22a4)\n\nMaybe-\u228e : \u2200 {A B : \u2605} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e = sym Maybe\u2194\u22a4\u228e \u2218 \u228e-CMon.assoc \u22a4 _ _ \u2218 (Maybe\u2194\u22a4\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \u22a5 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \u22a5\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n\nFin-\u228e-+ : \u2200 m n \u2192 (Fin m \u228e Fin n) \u2194 Fin (m + n)\nFin-\u228e-+ m n = Maybe^\u22a5\u2194Fin (m + n)\n \u2218 Maybe^-\u228e-+ m n\n \u2218 sym (Maybe^\u22a5\u2194Fin m \u228e-cong Maybe^\u22a5\u2194Fin n)\n\nFin\u2218suc\u2194\u22a4\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2194 (\u22a4 \u228e Fin n)\nFin\u2218suc\u2194\u22a4\u228eFin = Maybe\u2194\u22a4\u228e \u2218 Fin\u2218suc\u2194Maybe\u2218Fin\n","old_contents":"module Function.Related.TypeIsomorphisms.NP where\n\nimport Level as L\nopen import Algebra\nopen L using (Lift; lower; lift)\nopen import Type\nimport Function as F\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Vec using (Vec; []; _\u2237_)\nopen import Data.Nat using (\u2115; zero; suc; _+_)\nopen import Data.Maybe.NP\nopen import Data.Product\n--open import Data.Product.N-ary\nopen import Data.Sum renaming (map to map\u228e)\nopen import Data.Unit\nopen import Data.Empty\nopen import Function.Equality using (_\u27e8$\u27e9_)\nopen import Function.Related as FR\nopen import Function.Related.TypeIsomorphisms public\nopen import Function.Inverse using (_\u2194_; _\u2218_; sym; id; module Inverse)\nopen import Relation.Binary.Product.Pointwise\nopen import Relation.Binary.Sum\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (\u2192-to-\u27f6)\n\nmodule \u00d7-CMon = CommutativeMonoid (\u00d7-CommutativeMonoid FR.bijection L.zero)\nmodule \u228e-CMon = CommutativeMonoid (\u228e-CommutativeMonoid FR.bijection L.zero)\nmodule \u00d7\u228e\u00b0 = CommutativeSemiring (\u00d7\u228e-CommutativeSemiring FR.bijection L.zero)\n\nswap-iso : \u2200 {A B} \u2192 (A \u00d7 B) \u2194 (B \u00d7 A)\nswap-iso = \u00d7-CMon.comm _ _\n\nMaybe\u2194\u22a4\u228e : \u2200 {a} {A : Set a} \u2192 Maybe A \u2194 (\u22a4 \u228e A)\nMaybe\u2194\u22a4\u228e\n = record { to = \u2192-to-\u27f6 (maybe inj\u2082 (inj\u2081 _))\n ; from = \u2192-to-\u27f6 [ F.const nothing , just ]\n ; inverse-of\n = record { left-inverse-of = maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl\n ; right-inverse-of = [ (\u03bb _ \u2192 \u2261.refl) , (\u03bb _ \u2192 \u2261.refl) ] } }\n\nMaybe-cong : \u2200 {a b} {A : Set a} {B : Set b} \u2192 A \u2194 B \u2192 Maybe A \u2194 Maybe B \nMaybe-cong A\u2194B = sym Maybe\u2194\u22a4\u228e \u2218 id \u228e-cong A\u2194B \u2218 Maybe\u2194\u22a4\u228e\n\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe : \u2200 {A} n \u2192 Maybe (Maybe^ n A) \u2194 Maybe^ n (Maybe A)\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe zero = id\nMaybe\u2218Maybe^\u2194Maybe^\u2218Maybe (suc n) = Maybe-cong (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n)\n\nMaybe^-cong : \u2200 {A B} n \u2192 A \u2194 B \u2192 Maybe^ n A \u2194 Maybe^ n B\nMaybe^-cong zero = F.id\nMaybe^-cong (suc n) = Maybe-cong F.\u2218 Maybe^-cong n\n\nVec0\u2194\u22a4 : \u2200 {a} {A : Set a} \u2192 Vec A 0 \u2194 \u22a4\nVec0\u2194\u22a4 = record { to = \u2192-to-\u27f6 _\n ; from = \u2192-to-\u27f6 (F.const [])\n ; inverse-of = record { left-inverse-of = \u03bb { [] \u2192 \u2261.refl }\n ; right-inverse-of = \u03bb _ \u2192 \u2261.refl } }\n\nVec\u2218suc\u2194A\u00d7Vec : \u2200 {a} {A : Set a} {n} \u2192 Vec A (suc n) \u2194 (A \u00d7 Vec A n)\nVec\u2218suc\u2194A\u00d7Vec\n = record { to = \u2192-to-\u27f6 (\u03bb { (x \u2237 xs) \u2192 x , xs })\n ; from = \u2192-to-\u27f6 (uncurry _\u2237_)\n ; inverse-of = record { left-inverse-of = \u03bb { (x \u2237 xs) \u2192 \u2261.refl }\n ; right-inverse-of = \u03bb _ \u2192 \u2261.refl } }\n\ninfix 8 _^_\n\n_^_ : \u2605 \u2192 \u2115 \u2192 \u2605\nA ^ 0 = \u22a4\nA ^ suc n = A \u00d7 A ^ n\n\n^\u2194Vec : \u2200 {A} n \u2192 (A ^ n) \u2194 Vec A n\n^\u2194Vec zero = sym Vec0\u2194\u22a4\n^\u2194Vec (suc n) = sym Vec\u2218suc\u2194A\u00d7Vec \u2218 (id \u00d7-cong (^\u2194Vec n))\n\nFin0\u2194\u22a5 : Fin 0 \u2194 \u22a5\nFin0\u2194\u22a5 = record { to = \u2192-to-\u27f6 \u03bb()\n ; from = \u2192-to-\u27f6 \u03bb()\n ; inverse-of = record { left-inverse-of = \u03bb()\n ; right-inverse-of = \u03bb() } }\n\nFin\u2218suc\u2194Maybe\u2218Fin : \u2200 {n} \u2192 Fin (suc n) \u2194 Maybe (Fin n)\nFin\u2218suc\u2194Maybe\u2218Fin {n}\n = record { to = \u2192-to-\u27f6 to\n ; from = \u2192-to-\u27f6 (maybe suc zero)\n ; inverse-of\n = record { left-inverse-of = \u03bb { zero \u2192 \u2261.refl ; (suc n) \u2192 \u2261.refl }\n ; right-inverse-of = maybe (\u03bb _ \u2192 \u2261.refl) \u2261.refl } }\n where to : Fin (suc n) \u2192 Maybe (Fin n)\n to zero = nothing\n to (suc n) = just n\n\nLift\u2194id : \u2200 {a} {A : Set a} \u2192 Lift {a} {a} A \u2194 A\nLift\u2194id = record { to = \u2192-to-\u27f6 lower\n ; from = \u2192-to-\u27f6 lift\n ; inverse-of = record { left-inverse-of = \u03bb { (lift x) \u2192 \u2261.refl }\n ; right-inverse-of = \u03bb _ \u2192 \u2261.refl } }\n\n\u22a4\u00d7A\u2194A : \u2200 {A : \u2605} \u2192 (\u22a4 \u00d7 A) \u2194 A\n\u22a4\u00d7A\u2194A = proj\u2081 \u00d7-CMon.identity _ \u2218 sym Lift\u2194id \u00d7-cong id\n\nA\u00d7\u22a4\u2194A : \u2200 {A : \u2605} \u2192 (A \u00d7 \u22a4) \u2194 A\nA\u00d7\u22a4\u2194A = proj\u2082 \u00d7-CMon.identity _ \u2218 id \u00d7-cong sym Lift\u2194id\n\n\u22a5\u228eA\u2194A : \u2200 {A : \u2605} \u2192 (\u22a5 \u228e A) \u2194 A\n\u22a5\u228eA\u2194A = proj\u2081 \u228e-CMon.identity _ \u2218 sym Lift\u2194id \u228e-cong id\n\nA\u228e\u22a5\u2194A : \u2200 {A : \u2605} \u2192 (A \u228e \u22a5) \u2194 A\nA\u228e\u22a5\u2194A = proj\u2082 \u228e-CMon.identity _ \u2218 id \u228e-cong sym Lift\u2194id\n\nMaybe\u22a5\u2194\u22a4 : Maybe \u22a5 \u2194 \u22a4\nMaybe\u22a5\u2194\u22a4 = A\u228e\u22a5\u2194A \u2218 Maybe\u2194\u22a4\u228e\n\nMaybe^\u22a5\u2194Fin : \u2200 n \u2192 Maybe^ n \u22a5 \u2194 Fin n\nMaybe^\u22a5\u2194Fin zero = sym Fin0\u2194\u22a5\nMaybe^\u22a5\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\u22a5\u2194Fin n)\n\nMaybe^\u22a4\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \u22a4 \u2194 Fin (suc n)\nMaybe^\u22a4\u2194Fin1+ n = Maybe^\u22a5\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\u22a5\u2194\u22a4)\n\nMaybe-\u228e : \u2200 {A B : \u2605} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e = sym Maybe\u2194\u22a4\u228e \u2218 \u228e-CMon.assoc \u22a4 _ _ \u2218 (Maybe\u2194\u22a4\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \u22a5 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \u22a5\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n\nFin-\u228e-+ : \u2200 m n \u2192 (Fin m \u228e Fin n) \u2194 Fin (m + n)\nFin-\u228e-+ m n = Maybe^\u22a5\u2194Fin (m + n)\n \u2218 Maybe^-\u228e-+ m n\n \u2218 sym (Maybe^\u22a5\u2194Fin m \u228e-cong Maybe^\u22a5\u2194Fin n)\n\nFin\u2218suc\u2194\u22a4\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2194 (\u22a4 \u228e Fin n)\nFin\u2218suc\u2194\u22a4\u228eFin = Maybe\u2194\u22a4\u228e \u2218 Fin\u2218suc\u2194Maybe\u2218Fin\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"32b4e15a9837ed6e6276af1fd200e4a3dffc2223","subject":"flipbased: fix choose","message":"flipbased: fix choose\n","repos":"crypto-agda\/crypto-agda","old_file":"flipbased.agda","new_file":"flipbased.agda","new_contents":"module flipbased where\n\nopen import Algebra\nimport Level as L\nopen L using () renaming (_\u2294_ to _L\u2294_)\nopen import Function hiding (_\u27e8_\u27e9_)\nopen import Data.Nat.NP\nopen import Data.Bool\nopen import Data.Nat.Properties\nopen import Data.Product using (proj\u2081; proj\u2082; _,_; swap; _\u00d7_)\nopen import Data.Bits hiding (replicateM)\nopen import Data.Bool\nopen import Data.Vec using (Vec; []; _\u2237_; take; drop; head; tail)\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_)\n\nrecord M {a} n (A : Set a) : Set a where\n constructor mk\n field\n run : Bits n \u2192 A\n\ntoss\u2032 : M 1 Bit\ntoss\u2032 = mk head\n\nreturn\u2032 : \u2200 {a} {A : Set a} \u2192 A \u2192 M 0 A\nreturn\u2032 = mk \u2218 const\n\npure\u2032 : \u2200 {a} {A : Set a} \u2192 A \u2192 M 0 A\npure\u2032 = return\u2032\n\ncomap : \u2200 {m n a} {A : Set a} \u2192 (Bits n \u2192 Bits m) \u2192 M m A \u2192 M n A\ncomap f (mk g) = mk (g \u2218 f)\n\nweaken : \u2200 {m n a} {A : Set a} \u2192 M n A \u2192 M (m + n) A\nweaken {m} = comap (drop m)\n\nweaken\u2032 : \u2200 {m n a} {A : Set a} \u2192 M n A \u2192 M (n + m) A\nweaken\u2032 = comap (take _)\n\nprivate\n take\u2264 : \u2200 {a} {A : Set a} {m n} \u2192 n \u2264 m \u2192 Vec A m \u2192 Vec A n\n take\u2264 z\u2264n _ = []\n take\u2264 (s\u2264s p) (x \u2237 xs) = x \u2237 take\u2264 p xs\n\nweaken\u2264 : \u2200 {m n a} {A : Set a} \u2192 m \u2264 n \u2192 M m A \u2192 M n A\nweaken\u2264 p = comap (take\u2264 p)\n\ncoerce : \u2200 {m n a} {A : Set a} \u2192 m \u2261 n \u2192 M m A \u2192 M n A\ncoerce \u2261.refl = id\n\ntoss : \u2200 {n} \u2192 M (1 + n) Bit\ntoss = weaken\u2032 toss\u2032\n\nreturn : \u2200 {n a} {A : Set a} \u2192 A \u2192 M n A\nreturn = weaken\u2032 \u2218 return\u2032\n\npure : \u2200 {n a} {A : Set a} \u2192 A \u2192 M n A\npure = return\n\n_>>=_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n M n\u2081 A \u2192 (A \u2192 M n\u2082 B) \u2192 M (n\u2081 + n\u2082) B\n_>>=_ {n\u2081} x f = mk (\u03bb bs \u2192 M.run (f (M.run x (take _ bs))) (drop n\u2081 bs))\n\n_>>=\u2032_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n M n\u2081 A \u2192 (A \u2192 M n\u2082 B) \u2192 M (n\u2082 + n\u2081) B\n_>>=\u2032_ {n\u2081} {n\u2082} rewrite \u2115\u00b0.+-comm n\u2082 n\u2081 = _>>=_\n\n_>>_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n M n\u2081 A \u2192 M n\u2082 B \u2192 M (n\u2081 + n\u2082) B\n_>>_ {n\u2081} x y = x >>= const y\n\nmap : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 M n A \u2192 M n B\nmap f x = mk (f \u2218 M.run x)\n-- map f x \u2257 x >>=\u2032 (return {0} \u2218 f)\n\njoin : \u2200 {n\u2081 n\u2082 a} {A : Set a} \u2192\n M n\u2081 (M n\u2082 A) \u2192 M (n\u2081 + n\u2082) A\njoin x = x >>= id\n\ninfixl 4 _\u229b_\n_\u229b_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n M n\u2081 (A \u2192 B) \u2192 M n\u2082 A \u2192 M (n\u2081 + n\u2082) B\n_\u229b_ {n\u2081} mf mx = mf >>= \u03bb f \u2192 map (_$_ f) mx \n\n_\u27e8_\u27e9_ : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} \u2192\n M m A \u2192 (A \u2192 B \u2192 C) \u2192 M n B \u2192 M (m + n) C\nx \u27e8 f \u27e9 y = map f x \u229b y\n\n\u27ea_\u27eb : \u2200 {n} {a} {A : Set a} \u2192 A \u2192 M n A\n\u27ea_\u27eb = pure\n\n\u27ea_\u27eb\u2032 : \u2200 {a} {A : Set a} \u2192 A \u2192 M 0 A\n\u27ea_\u27eb\u2032 = pure\u2032\n\n\u27ea_\u00b7_\u27eb : \u2200 {a b} {A : Set a} {B : Set b} {n} \u2192 (A \u2192 B) \u2192 M n A \u2192 M n B\n\u27ea f \u00b7 x \u27eb = map f x\n\n\u27ea_\u00b7_\u00b7_\u27eb : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} \u2192\n (A \u2192 B \u2192 C) \u2192 M m A \u2192 M n B \u2192 M (m + n) C\n\u27ea f \u00b7 x \u00b7 y \u27eb = map f x \u229b y\n\n\u27ea_\u00b7_\u00b7_\u00b7_\u27eb : \u2200 {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} {m n o} \u2192\n (A \u2192 B \u2192 C \u2192 D) \u2192 M m A \u2192 M n B \u2192 M o C \u2192 M (m + n + o) D\n\u27ea f \u00b7 x \u00b7 y \u00b7 z \u27eb = map f x \u229b y \u229b z\n\nchoose : \u2200 {n a} {A : Set a} \u2192 M n A \u2192 M n A \u2192 M (suc n) A\nchoose x y = toss\u2032 >>= \u03bb b \u2192 if b then x else y\n\n_\u27e8,\u27e9_ : \u2200 {a b} {A : Set a} {B : Set b} {m n} \u2192 M m A \u2192 M n B \u2192 M (m + n) (A \u00d7 B)\nx \u27e8,\u27e9 y = \u27ea _,_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8xor\u27e9_ : \u2200 {n\u2081 n\u2082} \u2192 M n\u2081 Bit \u2192 M n\u2082 Bit \u2192 M (n\u2081 + n\u2082) Bit\nx \u27e8xor\u27e9 y = \u27ea _xor_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8\u2295\u27e9_ : \u2200 {n\u2081 n\u2082 m} \u2192 M n\u2081 (Bits m) \u2192 M n\u2082 (Bits m) \u2192 M (n\u2081 + n\u2082) (Bits m)\nx \u27e8\u2295\u27e9 y = \u27ea _\u2295_ \u00b7 x \u00b7 y \u27eb\n\nreplicateM : \u2200 {n m} {a} {A : Set a} \u2192 M m A \u2192 M (n * m) (Vec A n)\nreplicateM {zero} _ = \u27ea [] \u27eb\nreplicateM {suc _} x = \u27ea _\u2237_ \u00b7 x \u00b7 replicateM x \u27eb\n\nrandom : \u2200 {n} \u2192 M n (Bits n)\n-- random = coerce ? (replicateM toss) -- specialized version for now to avoid coerce\nrandom {zero} = \u27ea [] \u27eb\nrandom {suc _} = \u27ea _\u2237_ \u00b7 toss\u2032 \u00b7 random \u27eb\n\nrandomTbl : \u2200 m n \u2192 M (2 ^ m * n) (Vec (Bits n) (2 ^ m))\nrandomTbl m n = replicateM random\n\nrandomFun : \u2200 m n \u2192 M (2 ^ m * n) (Bits m \u2192 Bits n)\nrandomFun m n = \u27ea funFromTbl \u00b7 randomTbl m n \u27eb\n\nrandomFunExt : \u2200 {n k a} {A : Set a} \u2192 M k (Bits n \u2192 A) \u2192 M (k + k) (Bits (suc n) \u2192 A)\nrandomFunExt f = \u27ea comb \u00b7 f \u00b7 f \u27eb where comb = \u03bb g\u2081 g\u2082 xs \u2192 (if head xs then g\u2081 else g\u2082) (tail xs)\n\n2*_ : \u2115 \u2192 \u2115\n2* x = x + x\n\n2^_ : \u2115 \u2192 \u2115\n2^ 0 = 1\n2^ (suc n) = 2* (2^ n)\n\ncostRndFun : \u2115 \u2192 \u2115 \u2192 \u2115\ncostRndFun zero n = n\ncostRndFun (suc m) n = 2* (costRndFun m n)\n\nlem : \u2200 m n \u2192 costRndFun m n \u2261 2 ^ m * n\nlem zero n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\nlem (suc m) n rewrite lem m n | \u2115\u00b0.*-assoc 2 (2 ^ m) n | \u2115\u00b0.+-comm (2 ^ m * n) 0 = \u2261.refl\n\nrandomFun\u2032 : \u2200 {m n} \u2192 M (costRndFun m n) (Bits m \u2192 Bits n)\nrandomFun\u2032 {zero} = \u27ea const \u00b7 random \u27eb\nrandomFun\u2032 {suc m} = randomFunExt (randomFun\u2032 {m})\n\nrecord ProgEquiv a \u2113 : Set (L.suc \u2113 L\u2294 L.suc a) where\n infix 2 _\u2248_ _\u224b_\n field\n _\u2248_ : \u2200 {n} {A : Set a} \u2192 Rel (M n A) \u2113\n\n refl : \u2200 {n A} \u2192 Reflexive {A = M n A} _\u2248_\n sym : \u2200 {n A} \u2192 Symmetric {A = M n A} _\u2248_\n\n -- not strictly transitive\n\n reflexive : \u2200 {n A} \u2192 _\u2261_ \u21d2 _\u2248_ {n} {A}\n reflexive \u2261.refl = refl\n\n _\u224b_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 M n\u2081 A \u2192 M n\u2082 A \u2192 Set \u2113\n _\u224b_ {n\u2081} {n\u2082} p\u2081 p\u2082 = _\u2248_ {n = n\u2081 \u2294 n\u2082} (weaken\u2264 (m\u2264m\u2294n _ _) p\u2081) (weaken\u2264 (m\u2264n\u2294m _ n\u2081) p\u2082)\n where m\u2264n\u2294m : \u2200 m n \u2192 m \u2264 n \u2294 m\n m\u2264n\u2294m m n rewrite \u2294\u00b0.+-comm n m = m\u2264m\u2294n m n\n\n -- Another name for _\u224b_\n _looks_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 M n\u2081 A \u2192 M n\u2082 A \u2192 Set \u2113\n _looks_ = _\u224b_\n\nmodule WithEquiv (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n\n SecPRG : \u2200 {k n} (prg : (key : Bits k) \u2192 Bits n) \u2192 Set\n SecPRG prg = this looks random where this = \u27ea prg \u00b7 random \u27eb\n\n record PRG k n : Set where\n constructor _,_\n field\n prg : Bits k \u2192 Bits n\n sec : SecPRG prg\n\n OneTimeSecPRF : \u2200 {k m n} (prf : (key : Bits k) (msg : Bits m) \u2192 Bits n) \u2192 Set\n OneTimeSecPRF prf = \u2200 {xs} \u2192 let this = \u27ea prf \u00b7 random \u00b7 \u27ea xs \u27eb\u2032 \u27eb in\n this looks random\n\n record PRF k m n : Set where\n constructor _,_\n field\n prf : Bits k \u2192 Bits m \u2192 Bits n\n sec : OneTimeSecPRF prf\n\nOTP : \u2200 {n} \u2192 Bits n \u2192 Bits n \u2192 Bits n\nOTP key msg = key \u2295 msg\n\ninit : \u2200 {k a} {A : Set a} \u2192 (Bits k \u2192 A) \u2192 M k A\ninit f = \u27ea f \u00b7 random \u27eb\n\nmodule Examples (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n open WithEquiv progEq\n\n left-unit-law = \u2200 {A B : Set} {n} {x : A} {f : A \u2192 M n B} \u2192 return\u2032 x >>= f \u2248 f x\n\n right-unit-law = \u2200 {A : Set} {n} {x : M n A} \u2192 x >>=\u2032 return\u2032 \u2248 x\n\n assoc-law = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : M n\u2081 A} {f : A \u2192 M n\u2082 B} {g : B \u2192 M n\u2083 C}\n \u2192 (x >>= f) >>= g \u224b x >>= (\u03bb x \u2192 f x >>= g)\n\n assoc-law\u2032 = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : M n\u2081 A} {f : A \u2192 M n\u2082 B} {g : B \u2192 M n\u2083 C}\n \u2192 (x >>= f) >>= g \u2248 coerce (\u2261.sym (\u2115\u00b0.+-assoc n\u2081 n\u2082 n\u2083)) (x >>= (\u03bb x \u2192 f x >>= g))\n\n ex\u2081 = \u2200 {x} \u2192 toss\u2032 \u27e8xor\u27e9 \u27ea x \u27eb\u2032 \u2248 \u27ea x \u27eb\n\n ex\u2082 = p \u2248 map swap p where p = toss\u2032 \u27e8,\u27e9 toss\u2032\n\n ex\u2083 = \u2200 {n} \u2192 OneTimeSecPRF {n} OTP\n\n ex\u2084 = \u2200 {k n} (prg : PRG k n) \u2192 OneTimeSecPRF (\u03bb key xs \u2192 xs \u2295 PRG.prg prg key)\n\n ex\u2085 = \u2200 {k n} \u2192 PRG k n \u2192 PRF k n n\n","old_contents":"module flipbased where\n\nopen import Algebra\nimport Level as L\nopen L using () renaming (_\u2294_ to _L\u2294_)\nopen import Function hiding (_\u27e8_\u27e9_)\nopen import Data.Nat.NP\nopen import Data.Bool\nopen import Data.Nat.Properties\nopen import Data.Product using (proj\u2081; proj\u2082; _,_; swap; _\u00d7_)\nopen import Data.Bits hiding (replicateM)\nopen import Data.Bool\nopen import Data.Vec using (Vec; []; _\u2237_; take; drop; head; tail)\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_)\n\nrecord M {a} n (A : Set a) : Set a where\n constructor mk\n field\n run : Bits n \u2192 A\n\ntoss\u2032 : M 1 Bit\ntoss\u2032 = mk head\n\nreturn\u2032 : \u2200 {a} {A : Set a} \u2192 A \u2192 M 0 A\nreturn\u2032 = mk \u2218 const\n\npure\u2032 : \u2200 {a} {A : Set a} \u2192 A \u2192 M 0 A\npure\u2032 = return\u2032\n\ncomap : \u2200 {m n a} {A : Set a} \u2192 (Bits n \u2192 Bits m) \u2192 M m A \u2192 M n A\ncomap f (mk g) = mk (g \u2218 f)\n\nweaken : \u2200 {m n a} {A : Set a} \u2192 M n A \u2192 M (m + n) A\nweaken {m} = comap (drop m)\n\nweaken\u2032 : \u2200 {m n a} {A : Set a} \u2192 M n A \u2192 M (n + m) A\nweaken\u2032 = comap (take _)\n\nprivate\n take\u2264 : \u2200 {a} {A : Set a} {m n} \u2192 n \u2264 m \u2192 Vec A m \u2192 Vec A n\n take\u2264 z\u2264n _ = []\n take\u2264 (s\u2264s p) (x \u2237 xs) = x \u2237 take\u2264 p xs\n\nweaken\u2264 : \u2200 {m n a} {A : Set a} \u2192 m \u2264 n \u2192 M m A \u2192 M n A\nweaken\u2264 p = comap (take\u2264 p)\n\ncoerce : \u2200 {m n a} {A : Set a} \u2192 m \u2261 n \u2192 M m A \u2192 M n A\ncoerce \u2261.refl = id\n\ntoss : \u2200 {n} \u2192 M (1 + n) Bit\ntoss = weaken\u2032 toss\u2032\n\nreturn : \u2200 {n a} {A : Set a} \u2192 A \u2192 M n A\nreturn = weaken\u2032 \u2218 return\u2032\n\npure : \u2200 {n a} {A : Set a} \u2192 A \u2192 M n A\npure = return\n\n_>>=_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n M n\u2081 A \u2192 (A \u2192 M n\u2082 B) \u2192 M (n\u2081 + n\u2082) B\n_>>=_ {n\u2081} x f = mk (\u03bb bs \u2192 M.run (f (M.run x (take _ bs))) (drop n\u2081 bs))\n\n_>>=\u2032_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n M n\u2081 A \u2192 (A \u2192 M n\u2082 B) \u2192 M (n\u2082 + n\u2081) B\n_>>=\u2032_ {n\u2081} {n\u2082} rewrite \u2115\u00b0.+-comm n\u2082 n\u2081 = _>>=_\n\n_>>_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n M n\u2081 A \u2192 M n\u2082 B \u2192 M (n\u2081 + n\u2082) B\n_>>_ {n\u2081} x y = x >>= const y\n\nmap : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 M n A \u2192 M n B\nmap f x = mk (f \u2218 M.run x)\n-- map f x \u2257 x >>=\u2032 (return {0} \u2218 f)\n\njoin : \u2200 {n\u2081 n\u2082 a} {A : Set a} \u2192\n M n\u2081 (M n\u2082 A) \u2192 M (n\u2081 + n\u2082) A\njoin x = x >>= id\n\ninfixl 4 _\u229b_\n_\u229b_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n M n\u2081 (A \u2192 B) \u2192 M n\u2082 A \u2192 M (n\u2081 + n\u2082) B\n_\u229b_ {n\u2081} mf mx = mf >>= \u03bb f \u2192 map (_$_ f) mx \n\n_\u27e8_\u27e9_ : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} \u2192\n M m A \u2192 (A \u2192 B \u2192 C) \u2192 M n B \u2192 M (m + n) C\nx \u27e8 f \u27e9 y = map f x \u229b y\n\n\u27ea_\u27eb : \u2200 {n} {a} {A : Set a} \u2192 A \u2192 M n A\n\u27ea_\u27eb = pure\n\n\u27ea_\u27eb\u2032 : \u2200 {a} {A : Set a} \u2192 A \u2192 M 0 A\n\u27ea_\u27eb\u2032 = pure\u2032\n\n\u27ea_\u00b7_\u27eb : \u2200 {a b} {A : Set a} {B : Set b} {n} \u2192 (A \u2192 B) \u2192 M n A \u2192 M n B\n\u27ea f \u00b7 x \u27eb = map f x\n\n\u27ea_\u00b7_\u00b7_\u27eb : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} \u2192\n (A \u2192 B \u2192 C) \u2192 M m A \u2192 M n B \u2192 M (m + n) C\n\u27ea f \u00b7 x \u00b7 y \u27eb = map f x \u229b y\n\n\u27ea_\u00b7_\u00b7_\u00b7_\u27eb : \u2200 {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} {m n o} \u2192\n (A \u2192 B \u2192 C \u2192 D) \u2192 M m A \u2192 M n B \u2192 M o C \u2192 M (m + n + o) D\n\u27ea f \u00b7 x \u00b7 y \u00b7 z \u27eb = map f x \u229b y \u229b z\n\nchoose : \u2200 {n a} {A : Set a} \u2192 M n A \u2192 M n A \u2192 M (suc n) A\nchoose x y = \u27ea if_then_else_ \u00b7 toss \u00b7 x \u00b7 y \u27eb\n\n_\u27e8,\u27e9_ : \u2200 {a b} {A : Set a} {B : Set b} {m n} \u2192 M m A \u2192 M n B \u2192 M (m + n) (A \u00d7 B)\nx \u27e8,\u27e9 y = \u27ea _,_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8xor\u27e9_ : \u2200 {n\u2081 n\u2082} \u2192 M n\u2081 Bit \u2192 M n\u2082 Bit \u2192 M (n\u2081 + n\u2082) Bit\nx \u27e8xor\u27e9 y = \u27ea _xor_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8\u2295\u27e9_ : \u2200 {n\u2081 n\u2082 m} \u2192 M n\u2081 (Bits m) \u2192 M n\u2082 (Bits m) \u2192 M (n\u2081 + n\u2082) (Bits m)\nx \u27e8\u2295\u27e9 y = \u27ea _\u2295_ \u00b7 x \u00b7 y \u27eb\n\nreplicateM : \u2200 {n m} {a} {A : Set a} \u2192 M m A \u2192 M (n * m) (Vec A n)\nreplicateM {zero} _ = \u27ea [] \u27eb\nreplicateM {suc _} x = \u27ea _\u2237_ \u00b7 x \u00b7 replicateM x \u27eb\n\nrandom : \u2200 {n} \u2192 M n (Bits n)\n-- random = coerce ? (replicateM toss) -- specialized version for now to avoid coerce\nrandom {zero} = \u27ea [] \u27eb\nrandom {suc _} = \u27ea _\u2237_ \u00b7 toss\u2032 \u00b7 random \u27eb\n\nrandomTbl : \u2200 m n \u2192 M (2 ^ m * n) (Vec (Bits n) (2 ^ m))\nrandomTbl m n = replicateM random\n\nrandomFun : \u2200 m n \u2192 M (2 ^ m * n) (Bits m \u2192 Bits n)\nrandomFun m n = \u27ea funFromTbl \u00b7 randomTbl m n \u27eb\n\nrandomFunExt : \u2200 {n k a} {A : Set a} \u2192 M k (Bits n \u2192 A) \u2192 M (k + k) (Bits (suc n) \u2192 A)\nrandomFunExt f = \u27ea comb \u00b7 f \u00b7 f \u27eb where comb = \u03bb g\u2081 g\u2082 xs \u2192 (if head xs then g\u2081 else g\u2082) (tail xs)\n\n2*_ : \u2115 \u2192 \u2115\n2* x = x + x\n\n2^_ : \u2115 \u2192 \u2115\n2^ 0 = 1\n2^ (suc n) = 2* (2^ n)\n\ncostRndFun : \u2115 \u2192 \u2115 \u2192 \u2115\ncostRndFun zero n = n\ncostRndFun (suc m) n = 2* (costRndFun m n)\n\nlem : \u2200 m n \u2192 costRndFun m n \u2261 2 ^ m * n\nlem zero n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\nlem (suc m) n rewrite lem m n | \u2115\u00b0.*-assoc 2 (2 ^ m) n | \u2115\u00b0.+-comm (2 ^ m * n) 0 = \u2261.refl\n\nrandomFun\u2032 : \u2200 {m n} \u2192 M (costRndFun m n) (Bits m \u2192 Bits n)\nrandomFun\u2032 {zero} = \u27ea const \u00b7 random \u27eb\nrandomFun\u2032 {suc m} = randomFunExt (randomFun\u2032 {m})\n\nrecord ProgEquiv a \u2113 : Set (L.suc \u2113 L\u2294 L.suc a) where\n infix 2 _\u2248_ _\u224b_\n field\n _\u2248_ : \u2200 {n} {A : Set a} \u2192 Rel (M n A) \u2113\n\n refl : \u2200 {n A} \u2192 Reflexive {A = M n A} _\u2248_\n sym : \u2200 {n A} \u2192 Symmetric {A = M n A} _\u2248_\n\n -- not strictly transitive\n\n reflexive : \u2200 {n A} \u2192 _\u2261_ \u21d2 _\u2248_ {n} {A}\n reflexive \u2261.refl = refl\n\n _\u224b_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 M n\u2081 A \u2192 M n\u2082 A \u2192 Set \u2113\n _\u224b_ {n\u2081} {n\u2082} p\u2081 p\u2082 = _\u2248_ {n = n\u2081 \u2294 n\u2082} (weaken\u2264 (m\u2264m\u2294n _ _) p\u2081) (weaken\u2264 (m\u2264n\u2294m _ n\u2081) p\u2082)\n where m\u2264n\u2294m : \u2200 m n \u2192 m \u2264 n \u2294 m\n m\u2264n\u2294m m n rewrite \u2294\u00b0.+-comm n m = m\u2264m\u2294n m n\n\n -- Another name for _\u224b_\n _looks_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 M n\u2081 A \u2192 M n\u2082 A \u2192 Set \u2113\n _looks_ = _\u224b_\n\nmodule WithEquiv (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n\n SecPRG : \u2200 {k n} (prg : (key : Bits k) \u2192 Bits n) \u2192 Set\n SecPRG prg = this looks random where this = \u27ea prg \u00b7 random \u27eb\n\n record PRG k n : Set where\n constructor _,_\n field\n prg : Bits k \u2192 Bits n\n sec : SecPRG prg\n\n OneTimeSecPRF : \u2200 {k m n} (prf : (key : Bits k) (msg : Bits m) \u2192 Bits n) \u2192 Set\n OneTimeSecPRF prf = \u2200 {xs} \u2192 let this = \u27ea prf \u00b7 random \u00b7 \u27ea xs \u27eb\u2032 \u27eb in\n this looks random\n\n record PRF k m n : Set where\n constructor _,_\n field\n prf : Bits k \u2192 Bits m \u2192 Bits n\n sec : OneTimeSecPRF prf\n\nOTP : \u2200 {n} \u2192 Bits n \u2192 Bits n \u2192 Bits n\nOTP key msg = key \u2295 msg\n\ninit : \u2200 {k a} {A : Set a} \u2192 (Bits k \u2192 A) \u2192 M k A\ninit f = \u27ea f \u00b7 random \u27eb\n\nmodule Examples (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n open WithEquiv progEq\n\n left-unit-law = \u2200 {A B : Set} {n} {x : A} {f : A \u2192 M n B} \u2192 return\u2032 x >>= f \u2248 f x\n\n right-unit-law = \u2200 {A : Set} {n} {x : M n A} \u2192 x >>=\u2032 return\u2032 \u2248 x\n\n assoc-law = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : M n\u2081 A} {f : A \u2192 M n\u2082 B} {g : B \u2192 M n\u2083 C}\n \u2192 (x >>= f) >>= g \u224b x >>= (\u03bb x \u2192 f x >>= g)\n\n assoc-law\u2032 = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : M n\u2081 A} {f : A \u2192 M n\u2082 B} {g : B \u2192 M n\u2083 C}\n \u2192 (x >>= f) >>= g \u2248 coerce (\u2261.sym (\u2115\u00b0.+-assoc n\u2081 n\u2082 n\u2083)) (x >>= (\u03bb x \u2192 f x >>= g))\n\n ex\u2081 = \u2200 {x} \u2192 toss\u2032 \u27e8xor\u27e9 \u27ea x \u27eb\u2032 \u2248 \u27ea x \u27eb\n\n ex\u2082 = p \u2248 map swap p where p = toss\u2032 \u27e8,\u27e9 toss\u2032\n\n ex\u2083 = \u2200 {n} \u2192 OneTimeSecPRF {n} OTP\n\n ex\u2084 = \u2200 {k n} (prg : PRG k n) \u2192 OneTimeSecPRF (\u03bb key xs \u2192 xs \u2295 PRG.prg prg key)\n\n ex\u2085 = \u2200 {k n} \u2192 PRG k n \u2192 PRF k n n\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"58623914e9c7463bbe57441fd756041084ceb0ff","subject":"liftVec: better integration","message":"liftVec: better integration\n","repos":"crypto-agda\/agda-nplib","old_file":"experiment\/liftVec.agda","new_file":"experiment\/liftVec.agda","new_contents":"open import Function\nopen import Data.Nat using (\u2115 ; suc ; zero)\nopen import Data.Fin using (Fin)\nopen import Data.Vec\nopen import Data.Vec.N-ary.NP\nopen import Relation.Binary.PropositionalEquality\n\nmodule liftVec where\nmodule single (A : Set)(_+_ : A \u2192 A \u2192 A)where\n\n data Tm : Set where\n var : Tm\n cst : A \u2192 Tm\n fun : Tm \u2192 Tm \u2192 Tm\n\n eval : Tm \u2192 A \u2192 A\n eval var x = x\n eval (cst c) x = c\n eval (fun tm tm') x = eval tm x + eval tm' x\n\n veval : {m : \u2115} \u2192 Tm \u2192 Vec A m \u2192 Vec A m\n veval var xs = xs\n veval (cst x) xs = replicate x\n veval (fun tm tm') xs = zipWith _+_ (veval tm xs) (veval tm' xs)\n\n record Fm : Set where\n constructor _=='_\n field\n lhs : Tm\n rhs : Tm\n\n s[_] : Fm \u2192 Set\n s[ lhs ==' rhs ] = (x : A) \u2192 eval lhs x \u2261 eval rhs x\n\n v[_] : Fm \u2192 Set\n v[ lhs ==' rhs ] = {m : \u2115}(xs : Vec A m) \u2192 veval lhs xs \u2261 veval rhs xs\n\n lemma1 : (t : Tm) \u2192 veval t [] \u2261 []\n lemma1 var = refl\n lemma1 (cst x) = refl\n lemma1 (fun t t') rewrite lemma1 t | lemma1 t' = refl\n\n lemma2 : {m : \u2115}(x : A)(xs : Vec A m)(t : Tm) \u2192 veval t (x \u2237 xs) \u2261 eval t x \u2237 veval t xs\n lemma2 _ _ var = refl\n lemma2 _ _ (cst c) = refl\n lemma2 x xs (fun t t') rewrite lemma2 x xs t | lemma2 x xs t' = refl\n\n prf : (t : Fm) \u2192 s[ t ] \u2192 v[ t ]\n prf (lhs ==' rhs) pr [] rewrite lemma1 lhs | lemma1 rhs = refl\n prf (l ==' r) pr (x \u2237 xs) rewrite lemma2 x xs l | lemma2 x xs r = cong\u2082 (_\u2237_) (pr x) (prf (l ==' r) pr xs)\n\nmodule MultiDataType (A : Set) (nrVar : \u2115) where\n data Tm : Set where\n var : Fin nrVar \u2192 Tm\n cst : A \u2192 Tm\n fun : Tm \u2192 Tm \u2192 Tm\n\nmodule multi (A : Set)(_+_ : A \u2192 A \u2192 A)(nrVar : \u2115) where\n open MultiDataType A nrVar public\n\n sEnv : Set\n sEnv = Vec A nrVar\n\n vEnv : \u2115 \u2192 Set\n vEnv m = Vec (Vec A m) nrVar\n\n Var : Set\n Var = Fin nrVar\n\n _!_ : {X : Set} \u2192 Vec X nrVar \u2192 Var \u2192 X\n E ! v = lookup v E\n\n eval : Tm \u2192 sEnv \u2192 A\n eval (var x) G = G ! x\n eval (cst c) G = c\n eval (fun t t') G = eval t G + eval t' G\n\n veval : {m : \u2115} \u2192 Tm \u2192 vEnv m \u2192 Vec A m\n veval (var x) G = G ! x\n veval (cst c) G = replicate c\n veval (fun t t') G = zipWith _+_ (veval t G) (veval t' G)\n\n lemma1 : {xs : Vec (Vec A 0) nrVar} (t : Tm) \u2192 veval t xs \u2261 []\n lemma1 {xs} (var x) with xs ! x\n ... | [] = refl\n lemma1 (cst x) = refl\n lemma1 {xs} (fun t t') rewrite lemma1 {xs} t | lemma1 {xs} t' = refl\n\n lemVar : {m n : \u2115}(G : Vec (Vec A (suc m)) n)(i : Fin n) \u2192 lookup i G \u2261 lookup i (map head G) \u2237 lookup i (map tail G)\n lemVar [] ()\n lemVar ((x \u2237 G) \u2237 G\u2081) Data.Fin.zero = refl\n lemVar (G \u2237 G') (Data.Fin.suc i) = lemVar G' i\n\n lemma2 : {n : \u2115}(xs : vEnv (suc n))(t : Tm) \u2192 veval t xs \u2261 eval t (map head xs) \u2237 veval t (map tail xs)\n lemma2 G (var x) = lemVar G x\n lemma2 _ (cst x) = refl\n lemma2 G (fun t t') rewrite lemma2 G t | lemma2 G t' = refl\n\n s[_==_] : Tm \u2192 Tm \u2192 Set\n s[ l == r ] = (G : Vec A nrVar) \u2192 eval l G \u2261 eval r G\n\n v[_==_] : Tm \u2192 Tm \u2192 Set\n v[ l == r ] = {m : \u2115}(G : Vec (Vec A m) nrVar) \u2192 veval l G \u2261 veval r G\n\n prf : (l r : Tm) \u2192 s[ l == r ] \u2192 v[ l == r ]\n prf l r pr {zero} G rewrite lemma1 {G} l | lemma1 {G} r = refl\n prf l r pr {suc m} G rewrite lemma2 G l | lemma2 G r\n = cong\u2082 _\u2237_ (pr (replicate head \u229b G)) (prf l r pr (map tail G))\n\nmodule Full (A : Set)(_+_ : A \u2192 A \u2192 A) (m : \u2115) n where\n open multi A _+_ n public\n\n solve : \u2200 (l r : Tm) \u2192 \u2200\u207f n (curry\u207f\u2032 (\u03bb G \u2192 eval l G \u2261 eval r G))\n \u2192 \u2200\u207f n (curry\u207f\u2032 (\u03bb G \u2192 veval {m} l G \u2261 veval r G))\n solve l r x = curry\u207f {n} {A = Vec A m} {B = curry\u207f\u2032 f}\n (\u03bb xs \u2192 subst id (sym (curry-$\u207f\u2032 f xs))\n (prf l r (\u03bb G \u2192 subst id (curry-$\u207f\u2032 g G) (x $\u207f G)) xs))\n where f = \u03bb G \u2192 veval {m} l G \u2261 veval r G\n g = \u03bb G \u2192 eval l G \u2261 eval r G\n\nopen import Data.Bool\n\nmodule example where\n\n open import Data.Fin\n open import Data.Bool.NP\n\n module VecBoolXorProps m n = Full Bool _xor_ m n\n open MultiDataType\n\n coolTheorem : {m : \u2115} \u2192 (xs ys : Vec Bool m) \u2192 zipWith _xor_ xs ys \u2261 zipWith _xor_ ys xs\n coolTheorem {m} = VecBoolXorProps.solve m 2 l r Xor\u00b0.+-comm where\n l = fun (var zero) (var (suc zero))\n r = fun (var (suc zero)) (var zero)\n\ntest = example.coolTheorem (true \u2237 false \u2237 []) (false \u2237 false \u2237 [])\n","old_contents":"open import Data.Nat using (\u2115 ; suc ; zero)\nopen import Data.Fin using (Fin)\nopen import Data.Vec\nopen import Relation.Binary.PropositionalEquality\n\nmodule liftVec where \nmodule single (A : Set)(_+_ : A -> A -> A)where\n\n data Tm : Set where\n var : Tm\n cst : A -> Tm\n fun : Tm -> Tm -> Tm\n\n eval : Tm -> A -> A\n eval var x = x\n eval (cst c) x = c\n eval (fun tm tm') x = eval tm x + eval tm' x \n \n veval : {m : \u2115} -> Tm -> Vec A m -> Vec A m \n veval var xs = xs\n veval (cst x) xs = replicate x\n veval (fun tm tm') xs = zipWith _+_ (veval tm xs) (veval tm' xs)\n\n record Fm : Set where\n constructor _=='_\n field\n lhs : Tm\n rhs : Tm\n\n s[_] : Fm -> Set\n s[ lhs ==' rhs ] = (x : A) \u2192 eval lhs x \u2261 eval rhs x\n \n v[_] : Fm -> Set\n v[ lhs ==' rhs ] = {m : \u2115}(xs : Vec A m) \u2192 veval lhs xs \u2261 veval rhs xs\n\n lemma1 : (t : Tm) -> veval t [] \u2261 []\n lemma1 var = refl\n lemma1 (cst x) = refl\n lemma1 (fun t t') rewrite lemma1 t | lemma1 t' = refl\n\n lemma2 : {m : \u2115}(x : A)(xs : Vec A m)(t : Tm) -> veval t (x \u2237 xs) \u2261 eval t x \u2237 veval t xs\n lemma2 _ _ var = refl\n lemma2 _ _ (cst c) = refl\n lemma2 x xs (fun t t') rewrite lemma2 x xs t | lemma2 x xs t' = refl\n\n prf : (t : Fm) \u2192 s[ t ] \u2192 v[ t ]\n prf (lhs ==' rhs) pr [] rewrite lemma1 lhs | lemma1 rhs = refl\n prf (l ==' r) pr (x \u2237 xs) rewrite lemma2 x xs l | lemma2 x xs r = cong\u2082 (_\u2237_) (pr x) (prf (l ==' r) pr xs)\n\nmodule multi {nrVar : \u2115}(A : Set)(_+_ : A -> A -> A) where\n\n sEnv : Set\n sEnv = Vec A nrVar\n\n vEnv : \u2115 -> Set\n vEnv m = Vec (Vec A m) nrVar\n\n Var : Set\n Var = Fin nrVar\n\n _!_ : {X : Set} -> Vec X nrVar -> Var -> X\n E ! v = lookup v E\n\n data Tm : Set where\n var : Fin nrVar -> Tm\n cst : A -> Tm\n fun : Tm -> Tm -> Tm\n\n eval : Tm -> sEnv -> A\n eval (var x) G = G ! x\n eval (cst c) G = c\n eval (fun t t') G = eval t G + eval t' G \n\n veval : {m : \u2115} -> Tm -> vEnv m -> Vec A m\n veval (var x) G = G ! x\n veval (cst c) G = replicate c\n veval (fun t t') G = zipWith _+_ (veval t G) (veval t' G)\n\n lemma1 : {xs : Vec (Vec A 0) nrVar} (t : Tm) -> veval t xs \u2261 []\n lemma1 {xs} (var x) with xs ! x\n ... | [] = refl\n lemma1 (cst x) = refl\n lemma1 {xs} (fun t t') rewrite lemma1 {xs} t | lemma1 {xs} t' = refl\n\n lemVar : {m n : \u2115}(G : Vec (Vec A (suc m)) n)(i : Fin n) -> lookup i G \u2261 lookup i (map head G) \u2237 lookup i (map tail G)\n lemVar [] ()\n lemVar ((x \u2237 G) \u2237 G\u2081) Data.Fin.zero = refl\n lemVar (G \u2237 G') (Data.Fin.suc i) = lemVar G' i\n\n lemma2 : {n : \u2115}(xs : vEnv (suc n))(t : Tm) -> veval t xs \u2261 eval t (map head xs) \u2237 veval t (map tail xs)\n lemma2 G (var x) = lemVar G x \n lemma2 _ (cst x) = refl\n lemma2 G (fun t t') rewrite lemma2 G t | lemma2 G t' = refl\n\n s[_==_] : Tm -> Tm -> Set\n s[ l == r ] = (G : Vec A nrVar) -> eval l G \u2261 eval r G\n\n v[_==_] : Tm -> Tm -> Set\n v[ l == r ] = {m : \u2115}(G : Vec (Vec A m) nrVar) -> veval l G \u2261 veval r G\n\n prf : (l r : Tm) -> s[ l == r ] -> v[ l == r ]\n prf l r pr {zero} G rewrite lemma1 {G} l | lemma1 {G} r = refl\n prf l r pr {suc m} G rewrite lemma2 G l | lemma2 G r = cong\u2082 _\u2237_ (pr (replicate head \u229b G)) (prf l r pr (map tail G))\n\nmodule Forall (A : Set) where\n\n module N = Data.Nat\n\n NPred : \u2115 -> Set\u2081\n NPred 0 = Set\n NPred (N.suc n) = A -> NPred n \n\n NFun : (n : \u2115) -> NPred n -> Set\n NFun zero P = P\n NFun (suc n) P = (x : A) \u2192 NFun n (P x)\n\n \n NApply : {m n : \u2115} -> Vec A n -> Vec (Fin n) m -> NPred m -> Set \n NApply xs [] P = P\n NApply xs (x \u2237 ys) P = NApply xs ys (P (lookup x xs))\n\n\n NVec : {n : \u2115} -> NPred n -> Set\n NVec {n} P = (xs : Vec A n) -> NApply xs (tabulate (\\ x -> x)) P\n\n Build : {n : \u2115}{P : NPred n} -> NFun n P -> NVec P\n Build {n} f xs = go (tabulate (\u03bb x \u2192 x)) f where \n go : {m : \u2115}{P : NPred m}(ys : Vec (Fin n) m)(f : NFun m P) -> NApply xs ys P\n go [] f = f\n go (x \u2237 ys) f = go ys (f (lookup x xs)) \n\nmodule example where\n\n open import Data.Bool\n import Data.Bool.Properties\n open Data.Fin\n\n open import Algebra\n\n module Xor = CommutativeRing Data.Bool.Properties.commutativeRing-xor-\u2227 \n\n coolTheorem : {m : \u2115} -> (xs ys : Vec Bool m) -> zipWith _xor_ xs ys \u2261 zipWith _xor_ ys xs\n coolTheorem xs ys = prf l r (lemma) (xs \u2237 (ys \u2237 [])) where\n open multi {2} Bool _xor_ \n\n open Forall Bool\n \n l = (fun (var zero) (var (suc zero)))\n r = (fun (var (suc zero)) (var zero))\n\n Thm : NPred 2\n Thm = \u03bb x y \u2192 x xor y \u2261 y xor x\n\n Prf : NFun 2 Thm\n Prf true true = refl\n Prf true false = refl\n Prf false true = refl\n Prf false false = refl\n\n lem : NVec Thm\n lem = Build Prf \n \n lemma : (G : Vec Bool 2) -> lookup zero G xor lookup (suc zero) G\n \u2261 lookup (suc zero) G xor lookup zero G\n lemma (true \u2237 true \u2237 G) = refl\n lemma (true \u2237 false \u2237 G) = refl\n lemma (false \u2237 true \u2237 G) = refl\n lemma (false \u2237 false \u2237 G) = refl\n\nopen import Data.Bool\nopen Forall Bool\n\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"c087206d005020617069a7715e8b6d095680aca3","subject":"Only renaming.","message":"Only renaming.\n\nIgnore-this: 4ba18c3a1346ab5098ff19ca162a5b73\n\ndarcs-hash:20101221115850-3bd4e-6abcef31213c6163c23d468dadb1f281395dc591.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/Agsy\/AgsyTest.agda","new_file":"Draft\/Agsy\/AgsyTest.agda","new_contents":"------------------------------------------------------------------------------\n-- Testing Agsy using the Agda standard library\n------------------------------------------------------------------------------\n\nmodule AgsyTest where\n\nopen import Data.Nat\n\nopen import Relation.Binary.PropositionalEquality\nopen \u2261-Reasoning\n\n------------------------------------------------------------------------------\n\n+-assoc : \u2200 m n o \u2192 m + n + o \u2261 m + (n + o) -- via Agsy {-c}\n+-assoc zero n o = refl\n+-assoc (suc m) n o = cong suc (+-assoc m n o)\n\nx+Sy\u2261S[x+y] : \u2200 m n \u2192 m + suc n \u2261 suc (m + n) -- via Agsy {-c}\nx+Sy\u2261S[x+y] zero n = refl\nx+Sy\u2261S[x+y] (suc m) n = cong suc (x+Sy\u2261S[x+y] m n)\n\n0+x\u2261x+0 : \u2200 x \u2192 0 + x \u2261 x + 0 -- via Agsy {-c}\n0+x\u2261x+0 zero = refl\n0+x\u2261x+0 (suc n) = cong suc (0+x\u2261x+0 n)\n\n+-comm : \u2200 m n \u2192 m + n \u2261 n + m -- via Agsy {-c -m}\n+-comm zero n = 0+x\u2261x+0 n\n+-comm (suc m) n =\n begin\n suc (m + n) \u2261\u27e8 cong suc (+-comm m n) \u27e9\n suc (n + m) \u2261\u27e8 sym (x+Sy\u2261S[x+y] n m) \u27e9\n n + suc m\n \u220e\n","old_contents":"------------------------------------------------------------------------------\n-- Testing Agsy using the Agda standard library\n------------------------------------------------------------------------------\n\nmodule AgsyTest where\n\nopen import Data.Nat\n\nopen import Relation.Binary.PropositionalEquality\nopen \u2261-Reasoning\n\n------------------------------------------------------------------------------\n\n+-assoc : \u2200 m n o \u2192 m + n + o \u2261 m + (n + o) -- via Agsy {-c}\n+-assoc zero n o = refl\n+-assoc (suc m) n o = cong suc (+-assoc m n o)\n\nx+1+y\u22611+x+y : \u2200 m n \u2192 m + suc n \u2261 suc (m + n) -- via Agsy {-c}\nx+1+y\u22611+x+y zero n = refl\nx+1+y\u22611+x+y (suc m) n = cong suc (x+1+y\u22611+x+y m n)\n\n0+x\u2261x+0 : \u2200 x \u2192 0 + x \u2261 x + 0 -- via Agsy {-c}\n0+x\u2261x+0 zero = refl\n0+x\u2261x+0 (suc n) = cong suc (0+x\u2261x+0 n)\n\n+-comm : \u2200 m n \u2192 m + n \u2261 n + m -- via Agsy {-c -m}\n+-comm zero n = 0+x\u2261x+0 n\n+-comm (suc m) n =\n begin\n suc (m + n) \u2261\u27e8 cong suc (+-comm m n) \u27e9\n suc (n + m) \u2261\u27e8 sym (x+1+y\u22611+x+y n m) \u27e9\n n + suc m\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"15803dc967c439750b8fa3a9354f2a8732d4d8ca","subject":"flat-funs: more derived ops, , Maybe..., search, find?, findB","message":"flat-funs: more derived ops, , Maybe..., search, find?, findB\n","repos":"crypto-agda\/crypto-agda","old_file":"flat-funs.agda","new_file":"flat-funs.agda","new_contents":"module flat-funs where\n\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2^_)\nopen import Data.Bool using (if_then_else_)\nopen import Function using (_\u2218\u2032_)\nimport Data.Vec.NP as V\nimport Level as L\nopen V using (Vec; []; _\u2237_)\n\nopen import Data.Bits using (Bit; Bits)\n\nimport bintree as Tree\nopen Tree using (Tree)\nopen import data-universe\n\nrecord FlatFuns {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n universe : Universe T\n _`\u2192_ : T \u2192 T \u2192 Set\n infix 0 _`\u2192_\n open Universe universe public\n\nmodule Defaults {t} {T : Set t} (\u266dFuns : FlatFuns T) where\n open FlatFuns \u266dFuns\n\n module CompositionNotations\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)) where\n\n infixr 1 _\u204f_\n infixr 1 _>>>_\n\n _\u204f_ : \u2200 {a b c} \u2192 (a `\u2192 b) \u2192 (b `\u2192 c) \u2192 (a `\u2192 c)\n f \u204f g = g \u2218 f\n\n _>>>_ : \u2200 {a b c} \u2192 (a `\u2192 b) \u2192 (b `\u2192 c) \u2192 (a `\u2192 c)\n f >>> g = f \u204f g\n\n module DefaultsFirstSecond\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (<_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)) where\n\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n first f = < f \u00d7 id >\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = < id \u00d7 f >\n\n module DefaultsGroup2\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4)\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = < f \u2218 fst , g \u2218 snd >\n\n open DefaultsFirstSecond id <_\u00d7_> public\n\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n dup = < id , id >\n\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n swap = < snd , fst >\n\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc = < fst \u2218 fst , first snd >\n\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n = < tt , id >\n\n snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A\n snd = snd\n\n module DefaultsGroup1\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4)\n (snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A)\n (dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A)\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)) where\n\n open CompositionNotations _\u2218_\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = swap \u204f first f \u204f swap\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = first f \u204f second g\n\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f , g > = dup \u204f < f \u00d7 g >\n\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n snd = first tt \u204f snd\n\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n fst = swap \u204f snd\n\n module <\u00d7>Default\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n where\n open CompositionNotations _\u2218_\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = first f \u204f swap \u204f first g \u204f swap\n\n module DefaultAssoc\u2032\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C)))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n where\n open CompositionNotations _\u2218_\n\n -- This definition would cost 1 unit of space instead of 0.\n -- assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n -- assoc\u2032 = < second fst , snd \u204f snd >\n\n assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n assoc\u2032 = swap \u204f first swap \u204f assoc \u204f swap \u204f first swap\n\n module DefaultCond\n (fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n cond = fork fst snd\n\n -- This definition cost 2 units of space instead of 1.\n module DefaultFork\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C))\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A) where\n\n open CompositionNotations _\u2218_\n\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork f g = second < f , g > \u204f cond\n\nrecord FlatFunsOps {t} {T : Set t} (\u266dFuns : FlatFuns T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n\n infixr 9 _\u2218_\n infixr 3 _***_\n infixr 3 _&&&_\n field\n -- Functions\n id : \u2200 {A} \u2192 A `\u2192 A\n _\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)\n\n -- Bit\n <0b> <1b> : \u2200 {_A} \u2192 _A `\u2192 `Bit\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n\n -- Unit\n tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4\n\n -- Products (group 1 primitive functions or derived from group 2)\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n\n -- Products (group 2 primitive functions or derived from group 1)\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A\n\n -- Products (derived from group 1 or 2)\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n\n -- Vectors\n nil : \u2200 {_A B} \u2192 _A `\u2192 `Vec B 0\n cons : \u2200 {n A} \u2192 (A `\u00d7 `Vec A n) `\u2192 `Vec A (1 + n)\n uncons : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 (A `\u00d7 `Vec A n)\n\n open Defaults \u266dFuns\n open CompositionNotations _\u2218_ public\n\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n f &&& g = < f , g >\n\n _***_ : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n f *** g = < f \u00d7 g >\n\n constBit : \u2200 {_A} \u2192 Bit \u2192 _A `\u2192 `Bit\n constBit b = if b then <1b> else <0b>\n\n : \u2200 {A B C} \u2192 (`\u22a4 `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n = \u204f < f \u00d7 g >\n\n <_,tt\u204f_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (`\u22a4 `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f ,tt\u204f g > = \u204f swap\n\n open DefaultAssoc\u2032 _\u2218_ assoc swap first public\n\n : \u2200 {A B} (b : A `\u2192 `Bit) (f g : A `\u2192 B) \u2192 A `\u2192 B\n = < b , id > \u204f fork if-0 if-1\n\n assoc-first : \u2200 {A B C D E} \u2192 (A `\u00d7 B `\u2192 D `\u00d7 E) \u2192 A `\u00d7 B `\u00d7 C `\u2192 D `\u00d7 E `\u00d7 C\n assoc-first f = assoc\u2032 \u204f first f \u204f assoc\n\n assoc-second : \u2200 {A B C D E} \u2192 (B `\u00d7 C `\u2192 E `\u00d7 D) \u2192 (A `\u00d7 B) `\u00d7 C `\u2192 (A `\u00d7 E) `\u00d7 D\n assoc-second f = assoc \u204f second f \u204f assoc\u2032\n\n <_`zip`_> : \u2200 {A B C D E F} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 ((D `\u00d7 E) `\u2192 F)\n \u2192 ((A `\u00d7 D) `\u00d7 (B `\u00d7 E)) `\u2192 (C `\u00d7 F)\n < f `zip` g > = assoc-first (assoc-second swap) \u204f < f \u00d7 g >\n\n{- This one use one unit of space\n < f `zip` g > = < < fst \u00d7 fst > \u204f f ,\n < snd \u00d7 snd > \u204f g >\n-}\n\n head : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n head = uncons \u204f fst\n\n tail : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n tail = uncons \u204f snd\n\n <_\u2237_> : \u2200 {m n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A m `\u2192 `Vec B n)\n \u2192 `Vec A (1 + m) `\u2192 `Vec B (1 + n)\n < f \u2237 g > = uncons \u204f < f \u00d7 g > \u204f cons\n\n <_\u2237\u2032_> : \u2200 {n A B C} \u2192 (A `\u2192 C) \u2192 (B `\u2192 `Vec C n)\n \u2192 A `\u00d7 B `\u2192 `Vec C (1 + n)\n < f \u2237\u2032 g > = < f \u00d7 g > \u204f cons\n\n -- Notice that this one costs 1 unit of space.\n dup\u204f<_\u2237\u2032_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n dup\u204f< f \u2237\u2032 g > = dup \u204f < f \u2237\u2032 g >\n\n : \u2200 {n A B} \u2192 (`\u22a4 `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n = \u204f cons\n\n <_\u2237\u2032tt\u204f_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`\u22a4 `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n < f \u2237\u2032tt\u204f g > = < f ,tt\u204f g > \u204f cons\n\n <0,_> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Bit `\u00d7 B\n <0, f > = , f >\n\n <1,_> : \u2200 {A B} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Bit `\u00d7 B\n <1, f > = , f >\n\n <0,> : \u2200 {A} \u2192 A `\u2192 `Bit `\u00d7 A\n <0,> = <0, id >\n\n <1,> : \u2200 {A} \u2192 A `\u2192 `Bit `\u00d7 A\n <1,> = <1, id >\n\n <0\u2237_> : \u2200 {n A} \u2192 (A `\u2192 `Bits n) \u2192 A `\u2192 `Bits (1 + n)\n <0\u2237 f > = \u2237\u2032 f >\n\n <1\u2237_> : \u2200 {n A} \u2192 (A `\u2192 `Bits n) \u2192 A `\u2192 `Bits (1 + n)\n <1\u2237 f > = \u2237\u2032 f >\n\n <0\u2237> : \u2200 {n} \u2192 `Bits n `\u2192 `Bits (1 + n)\n <0\u2237> = <0\u2237 id >\n\n <1\u2237> : \u2200 {n} \u2192 `Bits n `\u2192 `Bits (1 + n)\n <1\u2237> = <1\u2237 id >\n\n constVec : \u2200 {n b _A} {B : Set b} {C} \u2192 (B \u2192 `\u22a4 `\u2192 C) \u2192 Vec B n \u2192 _A `\u2192 `Vec C n\n constVec f [] = nil\n constVec f (x \u2237 xs) = \n\n constBits : \u2200 {n _A} \u2192 Bits n \u2192 _A `\u2192 `Bits n\n constBits = constVec constBit\n\n foldl : \u2200 {n A B} \u2192 (B `\u00d7 A `\u2192 B) \u2192 (B `\u00d7 `Vec A n) `\u2192 B\n foldl {zero} f = fst\n foldl {suc n} f = second uncons\n \u204f assoc\u2032\n \u204f first f\n \u204f foldl f\n\n foldl\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldl\u2081 f = uncons \u204f foldl f\n\n foldr : \u2200 {n A B} \u2192 (A `\u00d7 B `\u2192 B) \u2192 (`Vec A n `\u00d7 B) `\u2192 B\n foldr {zero} f = snd\n foldr {suc n} f = first uncons\n \u204f assoc\n \u204f second (foldr f)\n \u204f f\n\n foldr\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldr\u2081 f = uncons \u204f swap \u204f foldr f\n\n <_\u229b> : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 `Vec A n `\u2192 `Vec B n\n <_\u229b> [] = nil\n <_\u229b> (f \u2237 fs) = < f \u2237 < fs \u229b> >\n\n map : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A n `\u2192 `Vec B n)\n map f = < V.replicate f \u229b>\n\n zipWith : \u2200 {n A B C} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec C n\n zipWith {zero} f = nil\n zipWith {suc n} f = < uncons \u00d7 uncons >\n \u204f < f `zip` (zipWith f) >\n \u204f cons\n\n zip : \u2200 {n A B} \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec (A `\u00d7 B) n\n zip = zipWith id\n\n : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Vec C 0 `\u00d7 B\n = \n\n <_,nil> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 A `\u2192 B `\u00d7 `Vec C 0\n < f ,nil> = < f ,tt\u204f nil >\n\n singleton : \u2200 {A} \u2192 A `\u2192 `Vec A 1\n singleton = < id ,nil> \u204f cons\n\n snoc : \u2200 {n A} \u2192 (`Vec A n `\u00d7 A) `\u2192 `Vec A (1 + n)\n snoc {zero} = snd \u204f singleton\n snoc {suc n} = first uncons \u204f assoc \u204f second snoc \u204f cons\n\n reverse : \u2200 {n A} \u2192 `Vec A n `\u2192 `Vec A n\n reverse {zero} = nil\n reverse {suc n} = uncons \u204f swap \u204f first reverse \u204f snoc\n\n append : \u2200 {m n A} \u2192 (`Vec A m `\u00d7 `Vec A n) `\u2192 `Vec A (m + n)\n append {zero} = snd\n append {suc m} = first uncons\n \u204f assoc\n \u204f second append\n \u204f cons\n\n splitAt : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 (`Vec A m `\u00d7 `Vec A n)\n splitAt zero = \n splitAt (suc m) = uncons\n \u204f second (splitAt m)\n \u204f assoc\u2032\n \u204f first cons\n\n take : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A m\n take zero = nil\n take (suc m) = < id \u2237 take m >\n\n drop : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A n\n drop zero = id\n drop (suc m) = tail \u204f drop m\n\n folda : \u2200 n {A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (2^ n) `\u2192 A\n folda zero f = head\n folda (suc n) f = splitAt (2^ n) \u204f < folda n f \u00d7 folda n f > \u204f f\n\n init : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n init {zero} = nil\n init {suc n} = < id \u2237 init >\n\n last : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n last {zero} = head\n last {suc n} = tail \u204f last\n\n concat : \u2200 {m n A} \u2192 `Vec (`Vec A m) n `\u2192 `Vec A (n * m)\n concat {n = zero} = nil\n concat {n = suc n} = uncons \u204f second concat \u204f append\n\n group : \u2200 {A} n k \u2192 `Vec A (n * k) `\u2192 `Vec (`Vec A k) n\n group zero k = nil\n group (suc n) k = splitAt k \u204f second (group n k) \u204f cons\n\n bind : \u2200 {m n A B} \u2192 (A `\u2192 `Vec B m) \u2192 `Vec A n `\u2192 `Vec B (n * m)\n bind f = map f \u204f concat\n\n replicate : \u2200 {n A} \u2192 A `\u2192 `Vec A n\n replicate {zero} = nil\n replicate {suc n} = < id , replicate > \u204f cons\n\n `Maybe : T \u2192 T\n `Maybe A = `Bit `\u00d7 A\n\n : \u2200 {A} \u2192 A `\u2192 `Maybe A\n = <0,>\n\n : \u2200 {A} \u2192 A `\u2192 `Maybe A\n = <1,>\n\n : \u2200 {A B C} \u2192 (f : A `\u00d7 B `\u2192 C) (g : B `\u2192 C) \u2192 `Maybe A `\u00d7 B `\u2192 C\n = \n\n _\u2223?_ : \u2200 {A} \u2192 `Maybe A `\u00d7 `Maybe A `\u2192 `Maybe A\n _\u2223?_ = \u2236 id >\n\n _`\u2192?_ : T \u2192 T \u2192 Set\n A `\u2192? B = A `\u2192 `Maybe B\n\n search : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 (`Bits n `\u2192 A) \u2192 `\u22a4 `\u2192 A\n search {zero} _ f = nil \u204f f\n search {suc n} op f = ) , search op (f \u2218 <1\u2237>) > \u204f op\n\n find? : \u2200 {n A} \u2192 (`Bits n `\u2192? A) \u2192 `\u22a4 `\u2192? A\n find? = search _\u2223?_\n\n findB : \u2200 {n} \u2192 (`Bits n `\u2192 `Bit) \u2192 `\u22a4 `\u2192? `Bits n\n findB pred = find? else >\n\n fromTree : \u2200 {n A} \u2192 Tree (`\u22a4 `\u2192 A) n \u2192 `Bits n `\u2192 A\n fromTree (Tree.leaf x) = tt \u204f x\n fromTree (Tree.fork t\u2080 t\u2081) = uncons \u204f fork (fromTree t\u2080) (fromTree t\u2081)\n\n fromFun : \u2200 {n A} \u2192 (Bits n \u2192 `\u22a4 `\u2192 A) \u2192 `Bits n `\u2192 A\n fromFun = fromTree \u2218\u2032 Tree.fromFun\n\n fromBitsFun : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 `Bits i `\u2192 `Bits o\n fromBitsFun f = fromFun (constBits \u2218\u2032 f)\n\n module WithFin\n (`Fin : \u2115 \u2192 T)\n (fz : \u2200 {n _A} \u2192 _A `\u2192 `Fin (suc n))\n (fs : \u2200 {n} \u2192 `Fin n `\u2192 `Fin (suc n))\n (elim-Fin0 : \u2200 {A} \u2192 `Fin 0 `\u2192 A)\n (elim-Fin1+ : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Fin n `\u00d7 A `\u2192 B) \u2192 `Fin (suc n) `\u00d7 A `\u2192 B) where\n\n tabulate : \u2200 {n A _B} \u2192 (`Fin n `\u2192 A) \u2192 _B `\u2192 `Vec A n\n tabulate {zero} f = nil\n tabulate {suc n} f = \u204f cons\n\n lookup : \u2200 {n A} \u2192 `Fin n `\u00d7 `Vec A n `\u2192 A\n lookup {zero} = fst \u204f elim-Fin0\n lookup {suc n} = elim-Fin1+ head (second tail \u204f lookup)\n\n allFin : \u2200 {n _A} \u2192 _A `\u2192 `Vec (`Fin n) n\n allFin = tabulate id\n","old_contents":"module flat-funs where\n\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2^_)\nopen import Data.Bool using (if_then_else_)\nopen import Function using (_\u2218\u2032_)\nimport Data.Vec.NP as V\nimport Level as L\nopen V using (Vec; []; _\u2237_)\n\nopen import Data.Bits using (Bit; Bits)\n\nimport bintree as Tree\nopen Tree using (Tree)\nopen import data-universe\n\nrecord FlatFuns {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n universe : Universe T\n _`\u2192_ : T \u2192 T \u2192 Set\n infix 0 _`\u2192_\n open Universe universe public\n\nmodule Defaults {t} {T : Set t} (\u266dFuns : FlatFuns T) where\n open FlatFuns \u266dFuns\n\n module CompositionNotations\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)) where\n\n infixr 1 _\u204f_\n infixr 1 _>>>_\n\n _\u204f_ : \u2200 {a b c} \u2192 (a `\u2192 b) \u2192 (b `\u2192 c) \u2192 (a `\u2192 c)\n f \u204f g = g \u2218 f\n\n _>>>_ : \u2200 {a b c} \u2192 (a `\u2192 b) \u2192 (b `\u2192 c) \u2192 (a `\u2192 c)\n f >>> g = f \u204f g\n\n module DefaultsFirstSecond\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (<_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)) where\n\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n first f = < f \u00d7 id >\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = < id \u00d7 f >\n\n module DefaultsGroup2\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4)\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = < f \u2218 fst , g \u2218 snd >\n\n open DefaultsFirstSecond id <_\u00d7_> public\n\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n dup = < id , id >\n\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n swap = < snd , fst >\n\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc = < fst \u2218 fst , first snd >\n\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n = < tt , id >\n\n snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A\n snd = snd\n\n module DefaultsGroup1\n (id : \u2200 {A} \u2192 A `\u2192 A)\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4)\n (snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A)\n (dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A)\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)) where\n\n open CompositionNotations _\u2218_\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = swap \u204f first f \u204f swap\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = first f \u204f second g\n\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f , g > = dup \u204f < f \u00d7 g >\n\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n snd = first tt \u204f snd\n\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n fst = swap \u204f snd\n\n module <\u00d7>Default\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n where\n open CompositionNotations _\u2218_\n\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g > = first f \u204f swap \u204f first g \u204f swap\n\n module DefaultAssoc\u2032\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C)))\n (swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A))\n (first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B))\n where\n open CompositionNotations _\u2218_\n\n -- This definition would cost 1 unit of space instead of 0.\n -- assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n -- assoc\u2032 = < second fst , snd \u204f snd >\n\n assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n assoc\u2032 = swap \u204f first swap \u204f assoc \u204f swap \u204f first swap\n\n module DefaultCond\n (fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B)\n (fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A)\n (snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B) where\n\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n cond = fork fst snd\n\n -- This definition cost 2 units of space instead of 1.\n module DefaultFork\n (_\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C))\n (second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C))\n (<_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C)\n (cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A) where\n\n open CompositionNotations _\u2218_\n\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork f g = second < f , g > \u204f cond\n\nrecord FlatFunsOps {t} {T : Set t} (\u266dFuns : FlatFuns T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n\n infixr 9 _\u2218_\n infixr 3 _***_\n infixr 3 _&&&_\n field\n -- Functions\n id : \u2200 {A} \u2192 A `\u2192 A\n _\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)\n\n -- Bit\n <0b> <1b> : \u2200 {_A} \u2192 _A `\u2192 `Bit\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n\n -- Unit\n tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4\n\n -- Products (group 1 primitive functions or derived from group 2)\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n\n -- Products (group 2 primitive functions or derived from group 1)\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n snd : \u2200 {A} \u2192 `\u22a4 `\u00d7 A `\u2192 A\n\n -- Products (derived from group 1 or 2)\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n\n -- Vectors\n nil : \u2200 {_A B} \u2192 _A `\u2192 `Vec B 0\n cons : \u2200 {n A} \u2192 (A `\u00d7 `Vec A n) `\u2192 `Vec A (1 + n)\n uncons : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 (A `\u00d7 `Vec A n)\n\n open Defaults \u266dFuns\n open CompositionNotations _\u2218_ public\n\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n f &&& g = < f , g >\n\n _***_ : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n f *** g = < f \u00d7 g >\n\n constBit : \u2200 {_A} \u2192 Bit \u2192 _A `\u2192 `Bit\n constBit b = if b then <1b> else <0b>\n\n : \u2200 {A B C} \u2192 (`\u22a4 `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n = \u204f < f \u00d7 g >\n\n <_,tt\u204f_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (`\u22a4 `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f ,tt\u204f g > = \u204f swap\n\n open DefaultAssoc\u2032 _\u2218_ assoc swap first public\n\n assoc-first : \u2200 {A B C D E} \u2192 (A `\u00d7 B `\u2192 D `\u00d7 E) \u2192 A `\u00d7 B `\u00d7 C `\u2192 D `\u00d7 E `\u00d7 C\n assoc-first f = assoc\u2032 \u204f first f \u204f assoc\n\n assoc-second : \u2200 {A B C D E} \u2192 (B `\u00d7 C `\u2192 E `\u00d7 D) \u2192 (A `\u00d7 B) `\u00d7 C `\u2192 (A `\u00d7 E) `\u00d7 D\n assoc-second f = assoc \u204f second f \u204f assoc\u2032\n\n <_`zip`_> : \u2200 {A B C D E F} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 ((D `\u00d7 E) `\u2192 F)\n \u2192 ((A `\u00d7 D) `\u00d7 (B `\u00d7 E)) `\u2192 (C `\u00d7 F)\n < f `zip` g > = assoc-first (assoc-second swap) \u204f < f \u00d7 g >\n\n{- This one use one unit of space\n < f `zip` g > = < < fst \u00d7 fst > \u204f f ,\n < snd \u00d7 snd > \u204f g >\n-}\n\n head : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n head = uncons \u204f fst\n\n tail : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n tail = uncons \u204f snd\n\n <_\u2237_> : \u2200 {m n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A m `\u2192 `Vec B n)\n \u2192 `Vec A (1 + m) `\u2192 `Vec B (1 + n)\n < f \u2237 g > = uncons \u204f < f \u00d7 g > \u204f cons\n\n <_\u2237\u2032_> : \u2200 {n A B C} \u2192 (A `\u2192 C) \u2192 (B `\u2192 `Vec C n)\n \u2192 A `\u00d7 B `\u2192 `Vec C (1 + n)\n < f \u2237\u2032 g > = < f \u00d7 g > \u204f cons\n\n -- Notice that this one costs 1 unit of space.\n dup\u204f<_\u2237\u2032_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n dup\u204f< f \u2237\u2032 g > = dup \u204f < f \u2237\u2032 g >\n\n : \u2200 {n A B} \u2192 (`\u22a4 `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n = \u204f cons\n\n <_\u2237\u2032tt\u204f_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`\u22a4 `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n < f \u2237\u2032tt\u204f g > = < f ,tt\u204f g > \u204f cons\n\n constVec : \u2200 {n b _A} {B : Set b} {C} \u2192 (B \u2192 `\u22a4 `\u2192 C) \u2192 Vec B n \u2192 _A `\u2192 `Vec C n\n constVec f [] = nil\n constVec f (x \u2237 xs) = \n\n constBits : \u2200 {n _A} \u2192 Bits n \u2192 _A `\u2192 `Bits n\n constBits = constVec constBit\n\n foldl : \u2200 {n A B} \u2192 (B `\u00d7 A `\u2192 B) \u2192 (B `\u00d7 `Vec A n) `\u2192 B\n foldl {zero} f = fst\n foldl {suc n} f = second uncons\n \u204f assoc\u2032\n \u204f first f\n \u204f foldl f\n\n foldl\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldl\u2081 f = uncons \u204f foldl f\n\n foldr : \u2200 {n A B} \u2192 (A `\u00d7 B `\u2192 B) \u2192 (`Vec A n `\u00d7 B) `\u2192 B\n foldr {zero} f = snd\n foldr {suc n} f = first uncons\n \u204f assoc\n \u204f second (foldr f)\n \u204f f\n\n foldr\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldr\u2081 f = uncons \u204f swap \u204f foldr f\n\n <_\u229b> : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 `Vec A n `\u2192 `Vec B n\n <_\u229b> [] = nil\n <_\u229b> (f \u2237 fs) = < f \u2237 < fs \u229b> >\n\n map : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A n `\u2192 `Vec B n)\n map f = < V.replicate f \u229b>\n\n zipWith : \u2200 {n A B C} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec C n\n zipWith {zero} f = nil\n zipWith {suc n} f = < uncons \u00d7 uncons >\n \u204f < f `zip` (zipWith f) >\n \u204f cons\n\n zip : \u2200 {n A B} \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec (A `\u00d7 B) n\n zip = zipWith id\n\n : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 A `\u2192 `Vec C 0 `\u00d7 B\n = \n\n <_,nil> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 A `\u2192 B `\u00d7 `Vec C 0\n < f ,nil> = < f ,tt\u204f nil >\n\n singleton : \u2200 {A} \u2192 A `\u2192 `Vec A 1\n singleton = < id ,nil> \u204f cons\n\n snoc : \u2200 {n A} \u2192 (`Vec A n `\u00d7 A) `\u2192 `Vec A (1 + n)\n snoc {zero} = snd \u204f singleton\n snoc {suc n} = first uncons \u204f assoc \u204f second snoc \u204f cons\n\n reverse : \u2200 {n A} \u2192 `Vec A n `\u2192 `Vec A n\n reverse {zero} = nil\n reverse {suc n} = uncons \u204f swap \u204f first reverse \u204f snoc\n\n append : \u2200 {m n A} \u2192 (`Vec A m `\u00d7 `Vec A n) `\u2192 `Vec A (m + n)\n append {zero} = snd\n append {suc m} = first uncons\n \u204f assoc\n \u204f second append\n \u204f cons\n\n splitAt : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 (`Vec A m `\u00d7 `Vec A n)\n splitAt zero = \n splitAt (suc m) = uncons\n \u204f second (splitAt m)\n \u204f assoc\u2032\n \u204f first cons\n\n take : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A m\n take zero = nil\n take (suc m) = < id \u2237 take m >\n\n drop : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A n\n drop zero = id\n drop (suc m) = tail \u204f drop m\n\n folda : \u2200 n {A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (2^ n) `\u2192 A\n folda zero f = head\n folda (suc n) f = splitAt (2^ n) \u204f < folda n f \u00d7 folda n f > \u204f f\n\n init : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n init {zero} = nil\n init {suc n} = < id \u2237 init >\n\n last : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n last {zero} = head\n last {suc n} = tail \u204f last\n\n concat : \u2200 {m n A} \u2192 `Vec (`Vec A m) n `\u2192 `Vec A (n * m)\n concat {n = zero} = nil\n concat {n = suc n} = uncons \u204f second concat \u204f append\n\n group : \u2200 {A} n k \u2192 `Vec A (n * k) `\u2192 `Vec (`Vec A k) n\n group zero k = nil\n group (suc n) k = splitAt k \u204f second (group n k) \u204f cons\n\n bind : \u2200 {m n A B} \u2192 (A `\u2192 `Vec B m) \u2192 `Vec A n `\u2192 `Vec B (n * m)\n bind f = map f \u204f concat\n\n replicate : \u2200 {n A} \u2192 A `\u2192 `Vec A n\n replicate {zero} = nil\n replicate {suc n} = < id , replicate > \u204f cons\n\n fromTree : \u2200 {n A} \u2192 Tree (`\u22a4 `\u2192 A) n \u2192 `Bits n `\u2192 A\n fromTree (Tree.leaf x) = tt \u204f x\n fromTree (Tree.fork t\u2080 t\u2081) = uncons \u204f fork (fromTree t\u2080) (fromTree t\u2081)\n\n fromFun : \u2200 {n A} \u2192 (Bits n \u2192 `\u22a4 `\u2192 A) \u2192 `Bits n `\u2192 A\n fromFun = fromTree \u2218\u2032 Tree.fromFun\n\n fromBitsFun : \u2200 {i o} \u2192 (Bits i \u2192 Bits o) \u2192 `Bits i `\u2192 `Bits o\n fromBitsFun f = fromFun (constBits \u2218\u2032 f)\n\n module WithFin\n (`Fin : \u2115 \u2192 T)\n (fz : \u2200 {n _A} \u2192 _A `\u2192 `Fin (suc n))\n (fs : \u2200 {n} \u2192 `Fin n `\u2192 `Fin (suc n))\n (elim-Fin0 : \u2200 {A} \u2192 `Fin 0 `\u2192 A)\n (elim-Fin1+ : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Fin n `\u00d7 A `\u2192 B) \u2192 `Fin (suc n) `\u00d7 A `\u2192 B) where\n\n tabulate : \u2200 {n A _B} \u2192 (`Fin n `\u2192 A) \u2192 _B `\u2192 `Vec A n\n tabulate {zero} f = nil\n tabulate {suc n} f = \u204f cons\n\n lookup : \u2200 {n A} \u2192 `Fin n `\u00d7 `Vec A n `\u2192 A\n lookup {zero} = fst \u204f elim-Fin0\n lookup {suc n} = elim-Fin1+ head (second tail \u204f lookup)\n\n allFin : \u2200 {n _A} \u2192 _A `\u2192 `Vec (`Fin n) n\n allFin = tabulate id\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"306f2622f460038c9688f491c6623f15ec94b233","subject":"Generalize search, add sum, # using sum","message":"Generalize search, add sum, # using sum\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nopen import Data.Bool.NP hiding (_==_)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (_\u229b_; rewire; rewireTbl; sum) renaming (map to vmap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x = cong (_\u2237_ x) (vnot\u2218vnot\u2257id xs)\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n where open RawMonad L.monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u00b7_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u00b7 search (f \u2218 1\u2237_)\n\nsearch\u2032 : \u2200 {n a} {A : \u2115 \u2192 Set a} \u2192 (\u2200 {m} \u2192 A m \u2192 A m \u2192 A (2* m)) \u2192 (Bits n \u2192 A 1) \u2192 A (2^ n)\nsearch\u2032 {n} {a} {A} op f = Search.search 1 2*_ {a} {A} (\u03bb {m} \u2192 op {m}) f\n\nsearch : \u2200 {n a} {A : Set a} \u2192 (A \u2192 A \u2192 A) \u2192 (Bits n \u2192 A) \u2192 A\nsearch {n} {a} {A} _\u00b7_ f = search\u2032 {n} {a} {const A} _\u00b7_ f\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nsum : \u2200 {n} \u2192 (Bits n \u2192 \u2115) \u2192 \u2115\nsum = search _+_\n\nsum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\nsum-const zero _ = refl\nsum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n#\u27e8 pred \u27e9 = sum (\u03bb x \u2192 if pred x then 1 else 0)\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\nsearch-\u00b7-\u03b5\u2261\u03b5 : \u2200 {a} {A : Set a} \u03b5 (_\u00b7_ : A \u2192 A \u2192 A)\n (\u03b5\u00b7\u03b5 : \u03b5 \u00b7 \u03b5 \u2261 \u03b5) n \u2192 search {n} _\u00b7_ (const \u03b5) \u2261 \u03b5\nsearch-\u00b7-\u03b5\u2261\u03b5 \u03b5 _\u00b7_ \u03b5\u00b7\u03b5 = go\n where\n go : \u2200 n \u2192 search {n} _\u00b7_ (const \u03b5) \u2261 \u03b5\n go zero = refl\n go (suc n) rewrite go n = \u03b5\u00b7\u03b5\n\n#never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n#never\u22610 = search-\u00b7-\u03b5\u2261\u03b5 _ _ refl\n\n#always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n#always\u22612^_ zero = refl\n#always\u22612^_ (suc n) = cong\u2082 _+_ pf pf where pf = #always\u22612^ n\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\ncount\u1d47 : Bit \u2192 \u2115\ncount\u1d47 b = if b then 1 else 0\n\n#\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n#\u27e8== [] \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n\u2257-cong-search : \u2200 {n a} {A : Set a} op {f g : Bits n \u2192 A} \u2192 f \u2257 g \u2192 search op f \u2261 search op g\n\u2257-cong-search {zero} op f\u2257g = f\u2257g []\n\u2257-cong-search {suc n} op f\u2257g = cong\u2082 op (\u2257-cong-search op (f\u2257g \u2218 0\u2237_))\n (\u2257-cong-search op (f\u2257g \u2218 1\u2237_))\n\n\u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n\u2257-cong-# f g f\u2257g = \u2257-cong-search _+_ (cong count\u1d47 \u2218 f\u2257g)\n\n#-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n#-+ f f0 f1 rewrite f0 | f1 = refl\n\ntake-\u2237 : \u2200 {m a} {A : Set a} n x (xs : Vec A (n + m)) \u2192 take (suc n) (x \u2237 xs) \u2261 x \u2237 take n xs\ntake-\u2237 n x xs with splitAt n xs\ntake-\u2237 _ _ ._ | _ , _ , refl = refl\n\ndrop-\u2237 : \u2200 {m a} {A : Set a} n x (xs : Vec A (n + m)) \u2192 drop (suc n) (x \u2237 xs) \u2261 drop n xs\ndrop-\u2237 n x xs with splitAt n xs\ndrop-\u2237 _ _ ._ | _ , _ , refl = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\n#-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-take-drop zero n f g with f []\n... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n... | false = #never\u22610 n\n#-take-drop (suc m) n f g = trans (#-+ {a = #\u27e8 f \u2218 0\u2237_ \u27e9 * #\u27e8 g \u27e9} ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)))\n (trans (\u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x)))\n (#-take-drop m n (f \u2218 0\u2237_) g))\n (trans (\u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x)))\n (#-take-drop m n (f \u2218 1\u2237_) g)))\n (sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9))\n\n#-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n#\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n#\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n#\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n... | true | true | _ = s\u2264s z\u2264n\n... | true | false | p = \u22a5-elim (p _)\n... | false | _ | _ = z\u2264n\n#\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n#-\u2227-\u2228\u1d47 : \u2200 x y \u2192 count\u1d47 (x \u2227 y) + count\u1d47 (x \u2228 y) \u2261 count\u1d47 x + count\u1d47 y\n#-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (count\u1d47 y) 1 = refl\n#-\u2227-\u2228\u1d47 false y = refl\n\n#-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n#-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#\u2228' {zero} f g with f []\n... | true = s\u2264s z\u2264n\n... | false = \u2115\u2264.refl\n#\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n#\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n\u2227\u21d2\u2228 : \u2200 x y \u2192 T (x \u2227 y) \u2192 T (x \u2228 y)\n\u2227\u21d2\u2228 true y = _\n\u2227\u21d2\u2228 false y = \u03bb ()\n\n#\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n#\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192 search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = \u2257-cong-search op {-(f |\u2228| g) (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))-} (|de-morgan| f g)\n\nsearch-comm :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-comm {zero} _+_ _*_ f p hom = refl\nsearch-comm {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-comm {n} _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-comm _+_ _*_ f (p \u2218 1\u2237_) hom))\n\n[0\u2194_] : \u2200 {n} \u2192 Fin n \u2192 Bits n \u2192 Bits n\n[0\u2194_] {zero} i xs = xs\n[0\u2194_] {suc n} i xs = lookup i xs \u2237 tail (xs [ i ]\u2254 head xs)\n\n[0\u21941] : Bits 2 \u2192 Bits 2\n[0\u21941] = [0\u2194 suc zero ]\n\n[0\u21941]-spec : [0\u21941] \u2257 (\u03bb { (x \u2237 y \u2237 []) \u2192 y \u2237 x \u2237 [] })\n[0\u21941]-spec (x \u2237 y \u2237 []) = refl\n\nopen Defs public\n","old_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nopen import Data.Bool.NP hiding (_==_)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (_\u229b_; rewire; rewireTbl) renaming (map to vmap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x = cong (_\u2237_ x) (vnot\u2218vnot\u2257id xs)\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n\n where open RawMonad L.monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nsearch : \u2200 {n a} {A : Set a} \u2192 (A \u2192 A \u2192 A) \u2192 (Bits n \u2192 A) \u2192 A\nsearch {zero} _ f = f []\nsearch {suc n} _\u00b7_ f = search _\u00b7_ (f \u2218 0\u2237_) \u00b7 search _\u00b7_ (f \u2218 1\u2237_)\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\n#\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n#\u27e8 pred \u27e9 = search _+_ (\u03bb x \u2192 if pred x then 1 else 0)\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\nsearch-\u00b7-\u03b5\u2261\u03b5 : \u2200 {a} {A : Set a} \u03b5 (_\u00b7_ : A \u2192 A \u2192 A)\n (\u03b5\u00b7\u03b5 : \u03b5 \u00b7 \u03b5 \u2261 \u03b5) n \u2192 search {n} _\u00b7_ (const \u03b5) \u2261 \u03b5\nsearch-\u00b7-\u03b5\u2261\u03b5 \u03b5 _\u00b7_ \u03b5\u00b7\u03b5 = go\n where\n go : \u2200 n \u2192 search {n} _\u00b7_ (const \u03b5) \u2261 \u03b5\n go zero = refl\n go (suc n) rewrite go n = \u03b5\u00b7\u03b5\n\n#never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n#never\u22610 = search-\u00b7-\u03b5\u2261\u03b5 _ _ refl\n\n#always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n#always\u22612^_ zero = refl\n#always\u22612^_ (suc n) = cong\u2082 _+_ pf pf where pf = #always\u22612^ n\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\ncount\u1d47 : Bit \u2192 \u2115\ncount\u1d47 b = if b then 1 else 0\n\n#\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n#\u27e8== [] \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n#\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n\u2257-cong-search : \u2200 {n a} {A : Set a} op {f g : Bits n \u2192 A} \u2192 f \u2257 g \u2192 search op f \u2261 search op g\n\u2257-cong-search {zero} op f\u2257g = f\u2257g []\n\u2257-cong-search {suc n} op f\u2257g = cong\u2082 op (\u2257-cong-search op (f\u2257g \u2218 0\u2237_))\n (\u2257-cong-search op (f\u2257g \u2218 1\u2237_))\n\n\u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n\u2257-cong-# f g f\u2257g = \u2257-cong-search _+_ (cong count\u1d47 \u2218 f\u2257g)\n\n#-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n#-+ f f0 f1 rewrite f0 | f1 = refl\n\ntake-\u2237 : \u2200 {m a} {A : Set a} n x (xs : Vec A (n + m)) \u2192 take (suc n) (x \u2237 xs) \u2261 x \u2237 take n xs\ntake-\u2237 n x xs with splitAt n xs\ntake-\u2237 _ _ ._ | _ , _ , refl = refl\n\ndrop-\u2237 : \u2200 {m a} {A : Set a} n x (xs : Vec A (n + m)) \u2192 drop (suc n) (x \u2237 xs) \u2261 drop n xs\ndrop-\u2237 n x xs with splitAt n xs\ndrop-\u2237 _ _ ._ | _ , _ , refl = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\n#-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-take-drop zero n f g with f []\n... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n... | false = #never\u22610 n\n#-take-drop (suc m) n f g = trans (#-+ {a = #\u27e8 f \u2218 0\u2237_ \u27e9 * #\u27e8 g \u27e9} ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)))\n (trans (\u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x)))\n (#-take-drop m n (f \u2218 0\u2237_) g))\n (trans (\u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x)))\n (#-take-drop m n (f \u2218 1\u2237_) g)))\n (sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9))\n\n#-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n#-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n#-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n#\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n#\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n#\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n#\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n... | true | true | _ = s\u2264s z\u2264n\n... | true | false | p = \u22a5-elim (p _)\n... | false | _ | _ = z\u2264n\n#\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n#-\u2227-\u2228\u1d47 : \u2200 x y \u2192 count\u1d47 (x \u2227 y) + count\u1d47 (x \u2228 y) \u2261 count\u1d47 x + count\u1d47 y\n#-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (count\u1d47 y) 1 = refl\n#-\u2227-\u2228\u1d47 false y = refl\n\n#-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n#-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n#\u2228' {zero} f g with f []\n... | true = s\u2264s z\u2264n\n... | false = \u2115\u2264.refl\n#\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n#\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n#\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n\u2227\u21d2\u2228 : \u2200 x y \u2192 T (x \u2227 y) \u2192 T (x \u2228 y)\n\u2227\u21d2\u2228 true y = _\n\u2227\u21d2\u2228 false y = \u03bb ()\n\n#\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n#\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192 search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = \u2257-cong-search op {-(f |\u2228| g) (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))-} (|de-morgan| f g)\n\nsearch-comm :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-comm {zero} _+_ _*_ f p hom = refl\nsearch-comm {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-comm {n} _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-comm _+_ _*_ f (p \u2218 1\u2237_) hom))\n\n[0\u2194_] : \u2200 {n} \u2192 Fin n \u2192 Bits n \u2192 Bits n\n[0\u2194_] {zero} i xs = xs\n[0\u2194_] {suc n} i xs = lookup i xs \u2237 tail (xs [ i ]\u2254 head xs)\n\n[0\u21941] : Bits 2 \u2192 Bits 2\n[0\u21941] = [0\u2194 suc zero ]\n\n[0\u21941]-spec : [0\u21941] \u2257 (\u03bb { (x \u2237 y \u2237 []) \u2192 y \u2237 x \u2237 [] })\n[0\u21941]-spec (x \u2237 y \u2237 []) = refl\n\nopen Defs public\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"5d213b30c7818d7ea1a1fd530b7ca86d27825545","subject":"+Data.Vec.NP.uncons","message":"+Data.Vec.NP.uncons\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Vec\/NP.agda","new_file":"lib\/Data\/Vec\/NP.agda","new_contents":"module Data.Vec.NP where\n\nopen import Data.Vec public\nopen import Data.Nat using (\u2115; suc; zero; _+_)\nopen import Data.Fin hiding (_+_)\nopen import Data.Fin.Props\nopen import Data.Bool\nopen import Data.Product\nopen import Function\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\n\ncount : \u2200 {n a} {A : Set a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 Fin (suc n)\ncount pred [] = zero\ncount pred (x \u2237 xs) = (if pred x then suc else inject\u2081) (count pred xs)\n\nfilter : \u2200 {n a} {A : Set a} (pred : A \u2192 Bool) (xs : Vec A n) \u2192 Vec A (to\u2115 (count pred xs))\nfilter pred [] = []\nfilter pred (x \u2237 xs) with pred x\n... | true = x \u2237 filter pred xs\n... | false rewrite inject\u2081-lemma (count pred xs) = filter pred xs\n\n\u03b7 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7 = tabulate \u2218 flip lookup\n\n\u03b7\u2032 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7\u2032 {zero} = \u03bb _ \u2192 []\n\u03b7\u2032 {suc n} = \u03bb xs \u2192 head xs \u2237 \u03b7 (tail xs)\n\nuncons : \u2200 {n a} {A : Set a} \u2192 Vec A (1 + n) \u2192 (A \u00d7 Vec A n)\nuncons (x \u2237 xs) = x , xs\n\nsplitAt\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A m \u00d7 Vec A n\nsplitAt\u2032 m xs = case splitAt m xs of \u03bb { (ys , zs , _) \u2192 (ys , zs) }\n\n++-decomp : \u2200 {m n a} {A : Set a} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2261 (zs ++ ts) \u2192 (xs \u2261 zs \u00d7 ys \u2261 ts)\n++-decomp {zero} {xs = []} {[]} p = refl , p\n++-decomp {suc m} {xs = x \u2237 xs} {z \u2237 zs} eq with ++-decomp {m} {xs = xs} {zs} (cong tail eq)\n... | (q\u2081 , q\u2082) = (cong\u2082 _\u2237_ (cong head eq) q\u2081) , q\u2082\n\n{-\nopen import Data.Vec.Equality\n\nmodule Here {a} {A : Set a} where\n open Equality (\u2261.setoid A)\n \u2248-splitAt : \u2200 {m n} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2248 (zs ++ ts) \u2192 (xs \u2248 zs \u00d7 ys \u2248 ts)\n \u2248-splitAt {zero} {xs = []} {[]} p = []-cong , p\n \u2248-splitAt {suc m} {xs = x \u2237 xs} {z \u2237 zs} (x\u00b9\u2248x\u00b2 \u2237-cong p) with \u2248-splitAt {m} {xs = xs} {zs} p\n ... | (q\u2081 , q\u2082) = x\u00b9\u2248x\u00b2 \u2237-cong q\u2081 , q\u2082\n-}\n\n++-inj\u2081 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 xs \u2261 ys\n++-inj\u2081 xs ys eq = proj\u2081 (++-decomp eq)\n\n++-inj\u2082 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 zs \u2261 ts\n++-inj\u2082 xs ys eq = proj\u2082 (++-decomp {xs = xs} {ys} eq)\n\ntake-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 take m (xs ++ ys) \u2261 xs\ntake-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ntake-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2081 xs\u2081 xs eq)\n\ndrop-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 drop m (xs ++ ys) \u2261 ys\ndrop-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ndrop-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2082 xs\u2081 xs eq)\n\ntake-drop-lem : \u2200 m {n} {a} {A : Set a} (xs : Vec A (m + n)) \u2192 take m xs ++ drop m xs \u2261 xs\ntake-drop-lem m xs with splitAt m xs\ntake-drop-lem m .(ys ++ zs) | ys , zs , refl = refl\n\ntake-them-all : \u2200 n {a} {A : Set a} (xs : Vec A (n + 0)) \u2192 take n xs ++ [] \u2261 xs\ntake-them-all n xs with splitAt n xs\ntake-them-all n .(ys ++ []) | ys , [] , refl = refl\n","old_contents":"module Data.Vec.NP where\n\nopen import Data.Vec public\nopen import Data.Nat using (\u2115; suc; zero; _+_)\nopen import Data.Fin hiding (_+_)\nopen import Data.Fin.Props\nopen import Data.Bool\nopen import Data.Product\nopen import Function\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\n\ncount : \u2200 {n a} {A : Set a} \u2192 (A \u2192 Bool) \u2192 Vec A n \u2192 Fin (suc n)\ncount pred [] = zero\ncount pred (x \u2237 xs) = (if pred x then suc else inject\u2081) (count pred xs)\n\nfilter : \u2200 {n a} {A : Set a} (pred : A \u2192 Bool) (xs : Vec A n) \u2192 Vec A (to\u2115 (count pred xs))\nfilter pred [] = []\nfilter pred (x \u2237 xs) with pred x\n... | true = x \u2237 filter pred xs\n... | false rewrite inject\u2081-lemma (count pred xs) = filter pred xs\n\n\u03b7 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7 = tabulate \u2218 flip lookup\n\n\u03b7\u2032 : \u2200 {n a} {A : Set a} \u2192 Vec A n \u2192 Vec A n\n\u03b7\u2032 {zero} = \u03bb _ \u2192 []\n\u03b7\u2032 {suc n} = \u03bb xs \u2192 head xs \u2237 \u03b7 (tail xs)\n\nsplitAt\u2032 : \u2200 {a} {A : Set a} m {n} \u2192 Vec A (m + n) \u2192 Vec A m \u00d7 Vec A n\nsplitAt\u2032 m xs = case splitAt m xs of \u03bb { (ys , zs , _) \u2192 (ys , zs) }\n\n++-decomp : \u2200 {m n a} {A : Set a} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2261 (zs ++ ts) \u2192 (xs \u2261 zs \u00d7 ys \u2261 ts)\n++-decomp {zero} {xs = []} {[]} p = refl , p\n++-decomp {suc m} {xs = x \u2237 xs} {z \u2237 zs} eq with ++-decomp {m} {xs = xs} {zs} (cong tail eq)\n... | (q\u2081 , q\u2082) = (cong\u2082 _\u2237_ (cong head eq) q\u2081) , q\u2082\n\n{-\nopen import Data.Vec.Equality\n\nmodule Here {a} {A : Set a} where\n open Equality (\u2261.setoid A)\n \u2248-splitAt : \u2200 {m n} {xs zs : Vec A m} {ys ts : Vec A n}\n \u2192 (xs ++ ys) \u2248 (zs ++ ts) \u2192 (xs \u2248 zs \u00d7 ys \u2248 ts)\n \u2248-splitAt {zero} {xs = []} {[]} p = []-cong , p\n \u2248-splitAt {suc m} {xs = x \u2237 xs} {z \u2237 zs} (x\u00b9\u2248x\u00b2 \u2237-cong p) with \u2248-splitAt {m} {xs = xs} {zs} p\n ... | (q\u2081 , q\u2082) = x\u00b9\u2248x\u00b2 \u2237-cong q\u2081 , q\u2082\n-}\n\n++-inj\u2081 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 xs \u2261 ys\n++-inj\u2081 xs ys eq = proj\u2081 (++-decomp eq)\n\n++-inj\u2082 : \u2200 {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} \u2192 xs ++ zs \u2261 ys ++ ts \u2192 zs \u2261 ts\n++-inj\u2082 xs ys eq = proj\u2082 (++-decomp {xs = xs} {ys} eq)\n\ntake-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 take m (xs ++ ys) \u2261 xs\ntake-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ntake-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2081 xs\u2081 xs eq)\n\ndrop-++ : \u2200 m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) \u2192 drop m (xs ++ ys) \u2261 ys\ndrop-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys\n... | zs | eq with splitAt m zs\ndrop-++ m xs\u2081 ys\u2081 | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj\u2082 xs\u2081 xs eq)\n\ntake-drop-lem : \u2200 m {n} {a} {A : Set a} (xs : Vec A (m + n)) \u2192 take m xs ++ drop m xs \u2261 xs\ntake-drop-lem m xs with splitAt m xs\ntake-drop-lem m .(ys ++ zs) | ys , zs , refl = refl\n\ntake-them-all : \u2200 n {a} {A : Set a} (xs : Vec A (n + 0)) \u2192 take n xs ++ [] \u2261 xs\ntake-them-all n xs with splitAt n xs\ntake-them-all n .(ys ++ []) | ys , [] , refl = refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"a7ca6f8ee722767ec1edd8583735a23faff8e0d1","subject":"also should be *that* particular delta","message":"also should be *that* particular delta\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress.agda","new_file":"progress.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nmodule progress where\n\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d val + d indet + d err[ \u0394 ] + \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n V : \u2200{d \u0394} \u2192 d val \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n E : \u2200{d \u0394} \u2192 d err[ \u0394 ] \u2192 ok d \u0394\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d') \u2192 ok d \u0394\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192 ok d \u0394\n progress TAConst = V VConst\n progress (TAVar x) = abort (somenotnone (! x))\n progress (TALam D) = V VLam\n progress (TAAp D1 x D2)\n with progress D1 | progress D2\n progress (TAAp TAConst () D2) | V VConst | V x\u2081\n progress {\u0394 = \u0394} (TAAp D1 x\u2082 D2) | V VLam | V x\u2081 = S {\u0394 = \u0394} (_ , Step FRefl (ITLam (FVal x\u2081)) FRefl)\n progress (TAAp D1 x\u2082 D2) | V x | I x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | V x | E x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | V x | S x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | I x | V x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | I x | I x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | I x | E x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | I x | S x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | E x | V x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | E x | I x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | E x | E x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | E x | S x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | S x | V x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | S x | I x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | S x | E x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | S x | S x\u2081 = {!!}\n progress (TAEHole x x\u2081) = I IEHole\n progress (TANEHole x D x\u2081)\n with progress D\n progress (TANEHole x\u2081 D x\u2082) | V x = {!!}\n progress (TANEHole x\u2081 D x\u2082) | I x = {!!}\n progress (TANEHole x\u2081 D x\u2082) | E x = {!!}\n progress (TANEHole x\u2081 D x\u2082) | S x = {!!}\n progress (TACast D x)\n with progress D\n progress (TACast TAConst TCRefl) | V VConst = E {!!}\n progress (TACast TAConst TCHole2) | V VConst = E {!!}\n progress (TACast D x\u2081) | V VLam = {!!}\n progress (TACast D x\u2081) | I x = {!!}\n progress (TACast D x\u2081) | E x = {!!}\n progress (TACast D x\u2083) | S (d , Step x x\u2081 x\u2082) = {!!}\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nmodule progress where\n\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d val + d indet + d err[ \u0394 ] + \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')\n data ok : (d : dhexp) \u2192 Set where\n V : \u2200{d} \u2192 d val \u2192 ok d\n I : \u2200{d} \u2192 d indet \u2192 ok d\n E : \u2200{d \u0394} \u2192 d err[ \u0394 ] \u2192 ok d\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d') \u2192 ok d\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192 ok d\n progress TAConst = V VConst\n progress (TAVar x) = abort (somenotnone (! x))\n progress (TALam D) = V VLam\n progress (TAAp D1 x D2)\n with progress D1 | progress D2\n progress (TAAp TAConst () D2) | V VConst | V x\u2081\n progress {\u0394 = \u0394} (TAAp D1 x\u2082 D2) | V VLam | V x\u2081 = S {\u0394 = \u0394} (_ , Step FRefl (ITLam (FVal x\u2081)) FRefl)\n progress (TAAp D1 x\u2082 D2) | V x | I x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | V x | E x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | V x | S x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | I x | V x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | I x | I x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | I x | E x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | I x | S x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | E x | V x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | E x | I x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | E x | E x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | E x | S x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | S x | V x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | S x | I x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | S x | E x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | S x | S x\u2081 = {!!}\n progress (TAEHole x x\u2081) = I IEHole\n progress (TANEHole x D x\u2081)\n with progress D\n progress (TANEHole x\u2081 D x\u2082) | V x = {!!}\n progress (TANEHole x\u2081 D x\u2082) | I x = {!!}\n progress (TANEHole x\u2081 D x\u2082) | E x = {!!}\n progress (TANEHole x\u2081 D x\u2082) | S x = {!!}\n progress (TACast D x)\n with progress D\n progress (TACast D x\u2081) | V x = {!!}\n progress (TACast D x\u2081) | I x = {!!}\n progress (TACast D x\u2081) | E x = {!!}\n progress (TACast D x\u2081) | S x = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"4271926cfe37f975efd867eb6cbd1673f449ac89","subject":"close #4","message":"close #4\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress-checks.agda","new_file":"progress-checks.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import canonical-forms\nopen import type-assignment-unicity\n\n\n-- taken together, the theorems in this file argue that for any expression\n-- d, at most one summand of the labeled sum that results from progress may\n-- be true at any time, i.e. that values, indeterminates, errors, and\n-- expressions that step are pairwise disjoint. (note that as a consequence\n-- of currying and comutativity of products, this means that there are six\n-- theorems to prove)\nmodule progress-checks where\n -- values and indeterminates are disjoint\n vi : \u2200{d} \u2192 d val \u2192 d indet \u2192 \u22a5\n vi VConst ()\n vi VLam ()\n\n -- values and errors are disjoint\n ve : \u2200{d \u0394} \u2192 d val \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n ve VConst ()\n ve VLam ()\n\n -- values and expressions that step are disjoint\n vs : \u2200{d \u0394} \u2192 d val \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n vs VConst (d , Step (FHFinal x) () (FHFinal x\u2082))\n vs VConst (_ , Step (FHFinal x) () FHEHole)\n vs VConst (_ , Step (FHFinal x) () FHNEHoleEvaled)\n vs VConst (_ , Step (FHFinal x) () (FHNEHoleFinal x\u2082))\n vs VConst (_ , Step (FHFinal x) () (FHCastFinal x\u2082))\n vs VLam (d , Step (FHFinal x\u2081) () (FHFinal x\u2083))\n vs VLam (_ , Step (FHFinal x\u2081) () FHEHole)\n vs VLam (_ , Step (FHFinal x\u2081) () FHNEHoleEvaled)\n vs VLam (_ , Step (FHFinal x\u2081) () (FHNEHoleFinal x\u2083))\n vs VLam (_ , Step (FHFinal x\u2081) () (FHCastFinal x\u2083))\n\n -- indeterminates and errors are disjoint\n ie : \u2200{d \u0394} \u2192 d indet \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n ie IEHole ()\n ie (INEHole (FVal x)) (ENEHole e) = ve x e\n ie (INEHole (FIndet x)) (ENEHole e) = ie x e\n ie (IAp i x) (EAp1 e) = ie i e\n ie (IAp i (FVal x)) (EAp2 e) = ve x e\n ie (IAp i (FIndet x)) (EAp2 e) = ie x e\n\n\n -- todo: these are bad names\n lem2 : \u2200{d \u0394 d'} \u2192 d indet \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem2 IEHole ()\n lem2 (INEHole x) ()\n lem2 (IAp () x\u2081) (ITLam x\u2082)\n\n lem3 : \u2200{d \u0394 d'} \u2192 d val \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem3 VConst ()\n lem3 VLam ()\n\n lem1 : \u2200{d \u0394 d'} \u2192 d final \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem1 (FVal x) st = lem3 x st\n lem1 (FIndet x) st = lem2 x st\n\n -- indeterminates and expressions that step are disjoint\n is : \u2200{d \u0394} \u2192 d indet \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n is IEHole (_ , Step (FHFinal x) q _) = lem1 x q\n is IEHole (_ , Step FHEHole () (FHFinal x))\n is IEHole (_ , Step FHEHole () FHEHole)\n is IEHole (_ , Step FHEHole () FHNEHoleEvaled)\n is IEHole (_ , Step FHEHole () (FHNEHoleFinal x))\n is IEHole (_ , Step FHEHole () (FHCastFinal x))\n is (INEHole x) (_ , Step (FHFinal x\u2081) q _) = lem1 x\u2081 q\n is (INEHole x) (_ , Step FHNEHoleEvaled () (FHFinal x\u2081))\n is (INEHole x) (_ , Step FHNEHoleEvaled () FHEHole)\n is (INEHole x) (_ , Step FHNEHoleEvaled () FHNEHoleEvaled)\n is (INEHole x) (_ , Step FHNEHoleEvaled () (FHNEHoleFinal x\u2081))\n is (INEHole x) (_ , Step FHNEHoleEvaled () (FHCastFinal x\u2081))\n is (IAp i x) (_ , Step (FHFinal x\u2081) q _) = lem1 x\u2081 q\n is (IAp i (FVal x)) (_ , Step (FHAp1 x\u2081 p) q (FHAp1 x\u2082 r)) = vs x (_ , Step p q r)\n is (IAp i (FIndet x)) (_ , Step (FHAp1 x\u2081 p) q (FHAp1 x\u2082 r)) = is x (_ , Step p q r)\n is (IAp i x) (_ , Step (FHAp2 p) q (FHAp2 r)) = is i (_ , (Step p q r))\n\n\n\n -- errors and expressions that step are disjoint\n es : \u2200{d \u0394} \u2192 \u0394 \u22a2 d err \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n es (ECastError x x\u2081) (d' , Step (FHFinal (FVal ())) x\u2083 x\u2084)\n es (ECastError x x\u2081) (d' , Step (FHFinal (FIndet ())) x\u2083 x\u2084)\n-- es (ECastError x x\u2081) (_ , Step (FHCast x\u2082) x\u2083 (FHCast x\u2084)) = {!x\u2082!}\n es (ECastError x x\u2081) (_ , Step (FHCast x\u2082) x\u2083 (FHCast x\u2084)) = {!x\u2082!}\n\n es (ECastError x x\u2081) (d' , Step (FHCastFinal (FVal x\u2082)) (ITCast x\u2083 x\u2084 x\u2085) x\u2086)\n with type-assignment-unicity x x\u2084\n ... | refl = ~apart x\u2081 x\u2085\n es (ECastError x x\u2081) (d' , Step (FHCastFinal (FIndet x\u2082)) (ITCast x\u2083 x\u2084 x\u2085) x\u2086)\n with type-assignment-unicity x x\u2084\n ... | refl = ~apart x\u2081 x\u2085\n es (EAp1 e) (d , Step (FHFinal (FVal x)) x\u2081 q) = vs x (d , Step (FHFinal (FVal x)) x\u2081 q)\n es (EAp1 e) (d , Step (FHFinal (FIndet x)) x\u2081 q) = is x (d , Step (FHFinal (FIndet x)) x\u2081 q)\n es (EAp1 e) (_ , Step (FHAp1 x x\u2081) x\u2082 (FHAp1 x\u2083 x\u2084)) = {!!}\n es (EAp1 e) (_ , Step (FHAp2 x) x\u2081 (FHAp2 x\u2082)) = {!!}\n es (EAp2 e) s = {!!}\n\n --es (ENEHole e) x = {!e!}\n es (ENEHole (ECastError x x\u2081)) (d' , Step (FHFinal x\u2082) x\u2083 x\u2084) = {!!}\n es (ENEHole (ECastError x x\u2081)) (d' , Step FHNEHoleEvaled x\u2082 x\u2083) = {!!}\n es (ENEHole (ECastError x x\u2081)) (d' , Step (FHNEHoleInside x\u2082) x\u2083 x\u2084) = {!!}\n es (ENEHole (ECastError x x\u2081)) (d' , Step (FHNEHoleFinal x\u2082) x\u2083 x\u2084) = {!!}\n es (ENEHole (EAp1 e)) (d' , Step (FHFinal x) x\u2081 x\u2082) = {!!}\n es (ENEHole (EAp1 e)) (d' , Step FHNEHoleEvaled x\u2081 x\u2082) = {!!}\n es (ENEHole (EAp1 e)) (d' , Step (FHNEHoleInside x) x\u2081 x\u2082) = {!!}\n es (ENEHole (EAp1 e)) (d' , Step (FHNEHoleFinal x) x\u2081 x\u2082) = {!!}\n es (ENEHole (EAp2 e)) (d' , Step (FHFinal x) x\u2081 x\u2082) = {!!}\n es (ENEHole (EAp2 e)) (d' , Step FHNEHoleEvaled x\u2081 x\u2082) = {!!}\n es (ENEHole (EAp2 e)) (d' , Step (FHNEHoleInside x) x\u2081 x\u2082) = {!!}\n es (ENEHole (EAp2 e)) (d' , Step (FHNEHoleFinal x) x\u2081 x\u2082) = {!!}\n es (ENEHole (ENEHole e)) (d' , Step (FHFinal x) x\u2081 x\u2082) = {!!}\n es (ENEHole (ENEHole e)) (d' , Step FHNEHoleEvaled x\u2081 x\u2082) = {!!}\n es (ENEHole (ENEHole e)) (d' , Step (FHNEHoleInside x) x\u2081 x\u2082) = {!!}\n es (ENEHole (ENEHole e)) (d' , Step (FHNEHoleFinal x) x\u2081 x\u2082) = {!!}\n es (ENEHole (ECastProp e)) (d' , Step (FHFinal x) x\u2081 x\u2082) = {!!}\n es (ENEHole (ECastProp e)) (d' , Step FHNEHoleEvaled x\u2081 x\u2082) = {!!}\n es (ENEHole (ECastProp e)) (d' , Step (FHNEHoleInside x) x\u2081 x\u2082) = {!!}\n es (ENEHole (ECastProp e)) (d' , Step (FHNEHoleFinal x) x\u2081 x\u2082) = {!!}\n -- es (ENEHole e) (d' , Step (FHFinal (FVal ())) x\u2081 x\u2082)\n -- es (ENEHole e) (d' , Step (FHFinal (FIndet (INEHole x))) () x\u2082)\n -- es (ENEHole e) (d' , Step FHNEHoleEvaled () x\u2082)\n -- es (ENEHole e) (_ , Step (FHNEHoleInside x) x\u2081 (FHNEHoleInside x\u2082)) = {!!}\n -- es (ENEHole e) (_ , Step (FHNEHoleFinal x) (ITNEHole x\u2081) (FHFinal (FVal ())))\n -- es (ENEHole (ECastError x x\u2081)) (_ , Step (FHNEHoleFinal x\u2082) (ITNEHole x\u2083) (FHFinal (FIndet (INEHole x\u2084)))) = {!x\u2081!}\n -- es (ENEHole (EAp1 e)) (_ , Step (FHNEHoleFinal x) (ITNEHole x\u2081) (FHFinal (FIndet (INEHole x\u2082)))) = {!!}\n -- es (ENEHole (EAp2 e)) (_ , Step (FHNEHoleFinal x) (ITNEHole x\u2081) (FHFinal (FIndet (INEHole x\u2082)))) = {!!}\n -- es (ENEHole (ENEHole e)) (_ , Step (FHNEHoleFinal x) (ITNEHole x\u2081) (FHFinal (FIndet (INEHole x\u2082)))) = {!!}\n -- es (ENEHole (ECastProp e)) (_ , Step (FHNEHoleFinal x) (ITNEHole x\u2081) (FHFinal (FIndet (INEHole x\u2082)))) = {!!}\n -- es (ENEHole e) (_ , Step (FHNEHoleFinal x) (ITNEHole x\u2081) FHNEHoleEvaled) = {!!}\n es (ECastProp e) (d' , Step (FHFinal (FVal x)) (ITCast x\u2081 x\u2082 x\u2083) x\u2084) = {!!}\n es (ECastProp e) (d' , Step (FHFinal (FIndet x)) x\u2081 x\u2082) = {!!}\n es (ECastProp e) (d' , Step (FHCast x) x\u2081 x\u2082) = {!!}\n es (ECastProp e) (d' , Step (FHCastFinal x) x\u2081 x\u2082) = {!!}\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import canonical-forms\nopen import type-assignment-unicity\n\n\n-- taken together, the theorems in this file argue that for any expression\n-- d, at most one summand of the labeled sum that results from progress may\n-- be true at any time, i.e. that values, indeterminates, errors, and\n-- expressions that step are pairwise disjoint. (note that as a consequence\n-- of currying and comutativity of products, this means that there are six\n-- theorems to prove)\nmodule progress-checks where\n -- values and indeterminates are disjoint\n vi : \u2200{d} \u2192 d val \u2192 d indet \u2192 \u22a5\n vi VConst ()\n vi VLam ()\n\n -- values and errors are disjoint\n ve : \u2200{d \u0394} \u2192 d val \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n ve VConst ()\n ve VLam ()\n\n -- values and expressions that step are disjoint\n vs : \u2200{d \u0394} \u2192 d val \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n vs VConst (d , Step (FHFinal x) () (FHFinal x\u2082))\n vs VConst (_ , Step (FHFinal x) () FHEHole)\n vs VConst (_ , Step (FHFinal x) () FHNEHoleEvaled)\n vs VConst (_ , Step (FHFinal x) () (FHNEHoleFinal x\u2082))\n vs VConst (_ , Step (FHFinal x) () (FHCastFinal x\u2082))\n vs VLam (d , Step (FHFinal x\u2081) () (FHFinal x\u2083))\n vs VLam (_ , Step (FHFinal x\u2081) () FHEHole)\n vs VLam (_ , Step (FHFinal x\u2081) () FHNEHoleEvaled)\n vs VLam (_ , Step (FHFinal x\u2081) () (FHNEHoleFinal x\u2083))\n vs VLam (_ , Step (FHFinal x\u2081) () (FHCastFinal x\u2083))\n\n -- indeterminates and errors are disjoint\n ie : \u2200{d \u0394} \u2192 d indet \u2192 \u0394 \u22a2 d err \u2192 \u22a5\n ie IEHole ()\n ie (INEHole (FVal x)) (ENEHole e) = ve x e\n ie (INEHole (FIndet x)) (ENEHole e) = ie x e\n ie (IAp i x) (EAp1 e) = ie i e\n ie (IAp i (FVal x)) (EAp2 e) = ve x e\n ie (IAp i (FIndet x)) (EAp2 e) = ie x e\n\n\n -- todo: these are bad names\n lem2 : \u2200{d \u0394 d'} \u2192 d indet \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem2 IEHole ()\n lem2 (INEHole x) ()\n lem2 (IAp () x\u2081) (ITLam x\u2082)\n\n lem3 : \u2200{d \u0394 d'} \u2192 d val \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem3 VConst ()\n lem3 VLam ()\n\n lem1 : \u2200{d \u0394 d'} \u2192 d final \u2192 \u0394 \u22a2 d \u2192> d' \u2192 \u22a5\n lem1 (FVal x) st = lem3 x st\n lem1 (FIndet x) st = lem2 x st\n\n -- indeterminates and expressions that step are disjoint\n is : \u2200{d \u0394} \u2192 d indet \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n is IEHole (_ , Step (FHFinal x) q _) = lem1 x q\n is IEHole (_ , Step FHEHole () (FHFinal x))\n is IEHole (_ , Step FHEHole () FHEHole)\n is IEHole (_ , Step FHEHole () FHNEHoleEvaled)\n is IEHole (_ , Step FHEHole () (FHNEHoleFinal x))\n is IEHole (_ , Step FHEHole () (FHCastFinal x))\n is (INEHole x) (_ , Step (FHFinal x\u2081) q _) = lem1 x\u2081 q\n is (INEHole x) (_ , Step FHNEHoleEvaled () (FHFinal x\u2081))\n is (INEHole x) (_ , Step FHNEHoleEvaled () FHEHole)\n is (INEHole x) (_ , Step FHNEHoleEvaled () FHNEHoleEvaled)\n is (INEHole x) (_ , Step FHNEHoleEvaled () (FHNEHoleFinal x\u2081))\n is (INEHole x) (_ , Step FHNEHoleEvaled () (FHCastFinal x\u2081))\n is (IAp i x) (_ , Step (FHFinal x\u2081) q _) = lem1 x\u2081 q\n is (IAp i x) (_ , Step (FHAp1 x\u2081 p) q (FHAp1 x\u2082 r)) = {!!}\n is (IAp i x) (_ , Step (FHAp2 p) q (FHAp2 r)) = {!!}\n\n\n\n -- errors and expressions that step are disjoint\n es : \u2200{d \u0394} \u2192 \u0394 \u22a2 d err \u2192 (\u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')) \u2192 \u22a5\n es (ECastError x x\u2081) (d' , Step (FHFinal (FVal ())) x\u2083 x\u2084)\n es (ECastError x x\u2081) (d' , Step (FHFinal (FIndet ())) x\u2083 x\u2084)\n-- es (ECastError x x\u2081) (_ , Step (FHCast x\u2082) x\u2083 (FHCast x\u2084)) = {!x\u2082!}\n es (ECastError x x\u2081) (_ , Step (FHCast x\u2082) x\u2083 (FHCast x\u2084)) = {!x\u2082!}\n\n es (ECastError x x\u2081) (d' , Step (FHCastFinal (FVal x\u2082)) (ITCast x\u2083 x\u2084 x\u2085) x\u2086)\n with type-assignment-unicity x x\u2084\n ... | refl = ~apart x\u2081 x\u2085\n es (ECastError x x\u2081) (d' , Step (FHCastFinal (FIndet x\u2082)) (ITCast x\u2083 x\u2084 x\u2085) x\u2086)\n with type-assignment-unicity x x\u2084\n ... | refl = ~apart x\u2081 x\u2085\n es (EAp1 e) (d , Step (FHFinal (FVal x)) x\u2081 q) = vs x (d , Step (FHFinal (FVal x)) x\u2081 q)\n es (EAp1 e) (d , Step (FHFinal (FIndet x)) x\u2081 q) = is x (d , Step (FHFinal (FIndet x)) x\u2081 q)\n es (EAp1 e) (_ , Step (FHAp1 x x\u2081) x\u2082 (FHAp1 x\u2083 x\u2084)) = {!!}\n es (EAp1 e) (_ , Step (FHAp2 x) x\u2081 (FHAp2 x\u2082)) = {!!}\n es (EAp2 e) s = {!!}\n\n --es (ENEHole e) x = {!e!}\n es (ENEHole (ECastError x x\u2081)) (d' , Step (FHFinal x\u2082) x\u2083 x\u2084) = {!!}\n es (ENEHole (ECastError x x\u2081)) (d' , Step FHNEHoleEvaled x\u2082 x\u2083) = {!!}\n es (ENEHole (ECastError x x\u2081)) (d' , Step (FHNEHoleInside x\u2082) x\u2083 x\u2084) = {!!}\n es (ENEHole (ECastError x x\u2081)) (d' , Step (FHNEHoleFinal x\u2082) x\u2083 x\u2084) = {!!}\n es (ENEHole (EAp1 e)) (d' , Step (FHFinal x) x\u2081 x\u2082) = {!!}\n es (ENEHole (EAp1 e)) (d' , Step FHNEHoleEvaled x\u2081 x\u2082) = {!!}\n es (ENEHole (EAp1 e)) (d' , Step (FHNEHoleInside x) x\u2081 x\u2082) = {!!}\n es (ENEHole (EAp1 e)) (d' , Step (FHNEHoleFinal x) x\u2081 x\u2082) = {!!}\n es (ENEHole (EAp2 e)) (d' , Step (FHFinal x) x\u2081 x\u2082) = {!!}\n es (ENEHole (EAp2 e)) (d' , Step FHNEHoleEvaled x\u2081 x\u2082) = {!!}\n es (ENEHole (EAp2 e)) (d' , Step (FHNEHoleInside x) x\u2081 x\u2082) = {!!}\n es (ENEHole (EAp2 e)) (d' , Step (FHNEHoleFinal x) x\u2081 x\u2082) = {!!}\n es (ENEHole (ENEHole e)) (d' , Step (FHFinal x) x\u2081 x\u2082) = {!!}\n es (ENEHole (ENEHole e)) (d' , Step FHNEHoleEvaled x\u2081 x\u2082) = {!!}\n es (ENEHole (ENEHole e)) (d' , Step (FHNEHoleInside x) x\u2081 x\u2082) = {!!}\n es (ENEHole (ENEHole e)) (d' , Step (FHNEHoleFinal x) x\u2081 x\u2082) = {!!}\n es (ENEHole (ECastProp e)) (d' , Step (FHFinal x) x\u2081 x\u2082) = {!!}\n es (ENEHole (ECastProp e)) (d' , Step FHNEHoleEvaled x\u2081 x\u2082) = {!!}\n es (ENEHole (ECastProp e)) (d' , Step (FHNEHoleInside x) x\u2081 x\u2082) = {!!}\n es (ENEHole (ECastProp e)) (d' , Step (FHNEHoleFinal x) x\u2081 x\u2082) = {!!}\n -- es (ENEHole e) (d' , Step (FHFinal (FVal ())) x\u2081 x\u2082)\n -- es (ENEHole e) (d' , Step (FHFinal (FIndet (INEHole x))) () x\u2082)\n -- es (ENEHole e) (d' , Step FHNEHoleEvaled () x\u2082)\n -- es (ENEHole e) (_ , Step (FHNEHoleInside x) x\u2081 (FHNEHoleInside x\u2082)) = {!!}\n -- es (ENEHole e) (_ , Step (FHNEHoleFinal x) (ITNEHole x\u2081) (FHFinal (FVal ())))\n -- es (ENEHole (ECastError x x\u2081)) (_ , Step (FHNEHoleFinal x\u2082) (ITNEHole x\u2083) (FHFinal (FIndet (INEHole x\u2084)))) = {!x\u2081!}\n -- es (ENEHole (EAp1 e)) (_ , Step (FHNEHoleFinal x) (ITNEHole x\u2081) (FHFinal (FIndet (INEHole x\u2082)))) = {!!}\n -- es (ENEHole (EAp2 e)) (_ , Step (FHNEHoleFinal x) (ITNEHole x\u2081) (FHFinal (FIndet (INEHole x\u2082)))) = {!!}\n -- es (ENEHole (ENEHole e)) (_ , Step (FHNEHoleFinal x) (ITNEHole x\u2081) (FHFinal (FIndet (INEHole x\u2082)))) = {!!}\n -- es (ENEHole (ECastProp e)) (_ , Step (FHNEHoleFinal x) (ITNEHole x\u2081) (FHFinal (FIndet (INEHole x\u2082)))) = {!!}\n -- es (ENEHole e) (_ , Step (FHNEHoleFinal x) (ITNEHole x\u2081) FHNEHoleEvaled) = {!!}\n es (ECastProp e) (d' , Step (FHFinal (FVal x)) (ITCast x\u2081 x\u2082 x\u2083) x\u2084) = {!!}\n es (ECastProp e) (d' , Step (FHFinal (FIndet x)) x\u2081 x\u2082) = {!!}\n es (ECastProp e) (d' , Step (FHCast x) x\u2081 x\u2082) = {!!}\n es (ECastProp e) (d' , Step (FHCastFinal x) x\u2081 x\u2082) = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"adefe17101926c368aec9494e33f955f80ab1c53","subject":"Added rev-++-commute (ATP version).","message":"Added rev-++-commute (ATP version).\n\nIgnore-this: b06a4aa75d259c3709c1d9455b834b42\n\ndarcs-hash:20110219140841-3bd4e-622543c31fc443c146ca4ac556726eaabcad0d30.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/Mirror\/ListTree\/PropertiesATP.agda","new_file":"Draft\/Mirror\/ListTree\/PropertiesATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Properties related with lists of trees\n------------------------------------------------------------------------------\n\nmodule Draft.Mirror.ListTree.PropertiesATP where\n\nopen import LTC.Base\n\nopen import Common.Function\n\nopen import Draft.Mirror.Mirror\n\nopen import LTC.Data.List\n\nopen import LTC.Relation.Binary.EqReasoning\n\n------------------------------------------------------------------------------\n\n++-ListTree : \u2200 {xs ys} \u2192 ListTree xs \u2192 ListTree ys \u2192 ListTree (xs ++ ys)\n++-ListTree {ys = ys} nilLT LTys = subst ListTree (sym (++-[] ys)) LTys\n++-ListTree {ys = ys} (consLT {x} {xs} Tx LTxs) LTys =\n subst ListTree (sym (++-\u2237 x xs ys)) (consLT Tx (++-ListTree LTxs LTys))\n\nmap-ListTree : \u2200 {xs} f \u2192 (\u2200 {x} \u2192 Tree x \u2192 Tree (f \u00b7 x)) \u2192\n ListTree xs \u2192 ListTree (map f xs)\nmap-ListTree f fTree nilLT = subst ListTree (sym (map-[] f)) nilLT\nmap-ListTree f fTree (consLT {x} {xs} Tx LTxs) =\n subst ListTree\n (sym (map-\u2237 f x xs))\n (consLT (fTree Tx) (map-ListTree f fTree LTxs))\n\nrev-ListTree : \u2200 {xs ys} \u2192 ListTree xs \u2192 ListTree ys \u2192 ListTree (rev xs ys)\nrev-ListTree {ys = ys} nilLT LTys = subst ListTree (sym (rev-[] ys)) LTys\nrev-ListTree {ys = ys} (consLT {x} {xs} Tx LTxs) LTys =\n subst ListTree (sym (rev-\u2237 x xs ys)) (rev-ListTree LTxs (consLT Tx LTys))\n\n++-leftIdentity : \u2200 {xs} \u2192 ListTree xs \u2192 [] ++ xs \u2261 xs\n++-leftIdentity {xs} _ = ++-[] xs\n\n++-rightIdentity : \u2200 {xs} \u2192 ListTree xs \u2192 xs ++ [] \u2261 xs\n++-rightIdentity nilLT = ++-[] []\n++-rightIdentity (consLT {x} {xs} Tx LTxs) = prf (++-rightIdentity LTxs)\n where\n postulate prf : xs ++ [] \u2261 xs \u2192\n (x \u2237 xs) ++ [] \u2261 x \u2237 xs\n {-# ATP prove prf #-}\n\n++-assoc : \u2200 {xs ys zs} \u2192 ListTree xs \u2192 ListTree ys \u2192 ListTree zs \u2192\n (xs ++ ys) ++ zs \u2261 xs ++ (ys ++ zs)\n++-assoc {ys = ys} {zs} nilLT LTys LTzs = prf\n where\n postulate prf : ([] ++ ys) ++ zs \u2261 [] ++ ys ++ zs\n {-# ATP prove prf #-}\n\n++-assoc {ys = ys} {zs} (consLT {x} {xs} Tx LTxs) LTys LTzs =\n prf (++-assoc LTxs LTys LTzs)\n where\n postulate prf : (xs ++ ys) ++ zs \u2261 xs ++ ys ++ zs \u2192 -- IH.\n ((x \u2237 xs) ++ ys) ++ zs \u2261 (x \u2237 xs) ++ ys ++ zs\n {-# ATP prove prf #-}\n\nrev-++-commute : \u2200 {xs ys} \u2192 ListTree xs \u2192 ListTree ys \u2192\n rev xs ys \u2261 rev xs [] ++ ys\nrev-++-commute {ys = ys} nilLT LTys = prf\n where\n postulate prf : rev [] ys \u2261 rev [] [] ++ ys\n {-# ATP prove prf #-}\n\nrev-++-commute {ys = ys} (consLT {x} {xs} Tx LTxs) LTys =\n prf (rev-++-commute LTxs (consLT Tx LTys))\n (rev-++-commute LTxs (consLT Tx nilLT))\n where\n postulate prf : rev xs (x \u2237 ys) \u2261 rev xs [] ++ x \u2237 ys \u2192 -- IH.\n rev xs (x \u2237 []) \u2261 rev xs [] ++ x \u2237 [] \u2192 -- IH.\n rev (x \u2237 xs) ys \u2261 rev (x \u2237 xs) [] ++ ys\n {-# ATP prove prf ++-assoc rev-ListTree ++-ListTree #-}\n\npostulate\n reverse-\u2237 : \u2200 x {ys} \u2192 ListTree ys \u2192\n reverse (x \u2237 ys) \u2261 reverse ys ++ (x \u2237 [])\n-- {-# ATP prove reverse-\u2237 #-}\n\nreverse-++-commute : \u2200 {xs ys} \u2192 ListTree xs \u2192 ListTree ys \u2192\n reverse (xs ++ ys) \u2261 reverse ys ++ reverse xs\nreverse-++-commute {ys = ys} nilLT LTys =\n begin\n reverse ([] ++ ys)\n \u2261\u27e8 subst (\u03bb t \u2192 reverse ([] ++ ys) \u2261 reverse t)\n (++-[] ys)\n refl\n \u27e9\n reverse ys\n \u2261\u27e8 sym (++-rightIdentity (rev-ListTree LTys nilLT)) \u27e9\n reverse ys ++ []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse ys ++ [] \u2261 reverse ys ++ t)\n (sym (rev-[] []))\n refl\n \u27e9\n reverse ys ++ reverse []\n \u220e\n\nreverse-++-commute (consLT {x} {xs} Tx LTxs) nilLT =\n begin\n reverse ((x \u2237 xs) ++ [])\n \u2261\u27e8 subst (\u03bb t \u2192 reverse ((x \u2237 xs) ++ []) \u2261 reverse t)\n (++-rightIdentity (consLT Tx LTxs))\n refl\n \u27e9\n reverse (x \u2237 xs)\n \u2261\u27e8 sym (++-[] (reverse (x \u2237 xs))) \u27e9\n [] ++ reverse (x \u2237 xs)\n \u2261\u27e8 subst (\u03bb t \u2192 [] ++ reverse (x \u2237 xs) \u2261 t ++ reverse (x \u2237 xs))\n (sym (rev-[] []))\n refl\n \u27e9\n reverse [] ++ reverse (x \u2237 xs)\n \u220e\n\nreverse-++-commute (consLT {x} {xs} Tx LTxs) (consLT {y} {ys} Ty LTys) =\n begin\n reverse ((x \u2237 xs) ++ y \u2237 ys) \u2261\u27e8 refl \u27e9\n rev ((x \u2237 xs) ++ y \u2237 ys) []\n \u2261\u27e8 subst (\u03bb t \u2192 rev ((x \u2237 xs) ++ y \u2237 ys) [] \u2261 rev t [])\n (++-\u2237 x xs (y \u2237 ys))\n refl\n \u27e9\n rev (x \u2237 (xs ++ y \u2237 ys)) [] \u2261\u27e8 rev-\u2237 x (xs ++ y \u2237 ys) [] \u27e9\n rev (xs ++ y \u2237 ys) (x \u2237 [])\n \u2261\u27e8 rev-++-commute (++-ListTree LTxs (consLT Ty LTys)) (consLT Tx nilLT) \u27e9\n rev (xs ++ y \u2237 ys) [] ++ (x \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 rev (xs ++ y \u2237 ys) [] ++ (x \u2237 []) \u2261 t ++ (x \u2237 []))\n refl\n refl\n \u27e9\n reverse (xs ++ y \u2237 ys) ++ (x \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (xs ++ y \u2237 ys) ++ (x \u2237 []) \u2261 t ++ (x \u2237 []))\n (reverse-++-commute LTxs (consLT Ty LTys)) -- IH.\n refl\n \u27e9\n (reverse (y \u2237 ys) ++ reverse xs) ++ x \u2237 []\n \u2261\u27e8 ++-assoc (rev-ListTree (consLT Ty LTys) nilLT)\n (rev-ListTree LTxs nilLT)\n (consLT Tx nilLT)\n \u27e9\n reverse (y \u2237 ys) ++ reverse xs ++ x \u2237 []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ reverse xs ++ x \u2237 [] \u2261\n reverse (y \u2237 ys) ++ t ++ x \u2237 [])\n refl\n refl\n \u27e9\n reverse (y \u2237 ys) ++ rev xs [] ++ x \u2237 []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ rev xs [] ++ x \u2237 [] \u2261\n reverse (y \u2237 ys) ++ t)\n (sym $ rev-++-commute LTxs (consLT Tx nilLT))\n refl\n \u27e9\n reverse (y \u2237 ys) ++ rev xs (x \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ rev xs (x \u2237 []) \u2261\n reverse (y \u2237 ys) ++ t)\n (sym $ rev-\u2237 x xs [])\n refl\n \u27e9\n reverse (y \u2237 ys) ++ rev (x \u2237 xs) []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ rev (x \u2237 xs) [] \u2261\n reverse (y \u2237 ys) ++ t)\n refl\n refl\n \u27e9\n reverse (y \u2237 ys) ++ reverse (x \u2237 xs)\n \u220e\n\nmap-++-commute : \u2200 f {xs ys} \u2192 (\u2200 {x} \u2192 Tree x \u2192 Tree (f \u00b7 x)) \u2192\n ListTree xs \u2192 ListTree ys \u2192\n map f (xs ++ ys) \u2261 map f xs ++ map f ys\nmap-++-commute f {ys = ys} fTree nilLT LTys =\n begin\n map f ([] ++ ys)\n \u2261\u27e8 subst (\u03bb t \u2192 map f ([] ++ ys) \u2261 map f t)\n (++-[] ys)\n refl\n \u27e9\n map f ys\n \u2261\u27e8 sym (++-leftIdentity (map-ListTree f fTree LTys)) \u27e9\n [] ++ map f ys\n \u2261\u27e8 subst (\u03bb t \u2192 [] ++ map f ys \u2261 t ++ map f ys)\n (sym (map-[] f))\n refl\n \u27e9\n map f [] ++ map f ys\n \u220e\n\nmap-++-commute f {ys = ys} fTree (consLT {x} {xs} Tx LTxs) LTys =\n begin\n map f ((x \u2237 xs) ++ ys)\n \u2261\u27e8 subst (\u03bb t \u2192 map f ((x \u2237 xs) ++ ys) \u2261 map f t)\n (++-\u2237 x xs ys)\n refl\n \u27e9\n map f (x \u2237 xs ++ ys)\n \u2261\u27e8 map-\u2237 f x (xs ++ ys) \u27e9\n f \u00b7 x \u2237 map f (xs ++ ys)\n \u2261\u27e8 subst (\u03bb t \u2192 f \u00b7 x \u2237 map f (xs ++ ys) \u2261 f \u00b7 x \u2237 t)\n (map-++-commute f fTree LTxs LTys) -- IH.\n refl\n \u27e9\n f \u00b7 x \u2237 (map f xs ++ map f ys)\n \u2261\u27e8 sym (++-\u2237 (f \u00b7 x) (map f xs) (map f ys)) \u27e9\n (f \u00b7 x \u2237 map f xs) ++ map f ys\n \u2261\u27e8 subst (\u03bb t \u2192 (f \u00b7 x \u2237 map f xs) ++ map f ys \u2261 t ++ map f ys)\n (sym (map-\u2237 f x xs))\n refl\n \u27e9\n map f (x \u2237 xs) ++ map f ys\n \u220e\n","old_contents":"------------------------------------------------------------------------------\n-- Properties related with lists of trees\n------------------------------------------------------------------------------\n\nmodule Draft.Mirror.ListTree.PropertiesATP where\n\nopen import LTC.Base\n\nopen import Common.Function\n\nopen import Draft.Mirror.Mirror\n\nopen import LTC.Data.List\n\nopen import LTC.Relation.Binary.EqReasoning\n\n------------------------------------------------------------------------------\n\n++-ListTree : \u2200 {xs ys} \u2192 ListTree xs \u2192 ListTree ys \u2192 ListTree (xs ++ ys)\n++-ListTree {ys = ys} nilLT LTys = subst ListTree (sym (++-[] ys)) LTys\n++-ListTree {ys = ys} (consLT {x} {xs} Tx LTxs) LTys =\n subst ListTree (sym (++-\u2237 x xs ys)) (consLT Tx (++-ListTree LTxs LTys))\n\nmap-ListTree : \u2200 {xs} f \u2192 (\u2200 {x} \u2192 Tree x \u2192 Tree (f \u00b7 x)) \u2192\n ListTree xs \u2192 ListTree (map f xs)\nmap-ListTree f fTree nilLT = subst ListTree (sym (map-[] f)) nilLT\nmap-ListTree f fTree (consLT {x} {xs} Tx LTxs) =\n subst ListTree\n (sym (map-\u2237 f x xs))\n (consLT (fTree Tx) (map-ListTree f fTree LTxs))\n\nrev-ListTree : \u2200 {xs ys} \u2192 ListTree xs \u2192 ListTree ys \u2192 ListTree (rev xs ys)\nrev-ListTree {ys = ys} nilLT LTys = subst ListTree (sym (rev-[] ys)) LTys\nrev-ListTree {ys = ys} (consLT {x} {xs} Tx LTxs) LTys =\n subst ListTree (sym (rev-\u2237 x xs ys)) (rev-ListTree LTxs (consLT Tx LTys))\n\n++-leftIdentity : \u2200 {xs} \u2192 ListTree xs \u2192 [] ++ xs \u2261 xs\n++-leftIdentity {xs} _ = ++-[] xs\n\n++-rightIdentity : \u2200 {xs} \u2192 ListTree xs \u2192 xs ++ [] \u2261 xs\n++-rightIdentity nilLT = ++-[] []\n++-rightIdentity (consLT {x} {xs} Tx LTxs) = prf (++-rightIdentity LTxs)\n where\n postulate prf : xs ++ [] \u2261 xs \u2192\n (x \u2237 xs) ++ [] \u2261 x \u2237 xs\n {-# ATP prove prf #-}\n\n++-assoc : \u2200 {xs ys zs} \u2192 ListTree xs \u2192 ListTree ys \u2192 ListTree zs \u2192\n (xs ++ ys) ++ zs \u2261 xs ++ (ys ++ zs)\n++-assoc {ys = ys} {zs} nilLT LTys LTzs = prf\n where\n postulate prf : ([] ++ ys) ++ zs \u2261 [] ++ ys ++ zs\n {-# ATP prove prf #-}\n\n++-assoc {ys = ys} {zs} (consLT {x} {xs} Tx LTxs) LTys LTzs =\n prf (++-assoc LTxs LTys LTzs)\n where\n postulate prf : (xs ++ ys) ++ zs \u2261 xs ++ ys ++ zs \u2192 -- IH.\n ((x \u2237 xs) ++ ys) ++ zs \u2261 (x \u2237 xs) ++ ys ++ zs\n {-# ATP prove prf #-}\n\nrev-++-commute : \u2200 {xs ys} \u2192 ListTree xs \u2192 ListTree ys \u2192\n rev xs ys \u2261 rev xs [] ++ ys\nrev-++-commute {ys = ys} nilLT LTys =\n begin\n rev [] ys \u2261\u27e8 rev-[] ys \u27e9\n ys \u2261\u27e8 sym $ ++-leftIdentity LTys \u27e9\n [] ++ ys \u2261\u27e8 subst (\u03bb t \u2192 [] ++ ys \u2261 t ++ ys)\n (sym $ rev-[] [])\n refl\n \u27e9\n rev [] [] ++ ys\n \u220e\n\nrev-++-commute {ys = ys} (consLT {x} {xs} Tx LTxs) LTys =\n begin\n rev (x \u2237 xs) ys \u2261\u27e8 rev-\u2237 x xs ys \u27e9\n rev xs (x \u2237 ys) \u2261\u27e8 rev-++-commute LTxs (consLT Tx LTys) \u27e9 -- IH.\n rev xs [] ++ x \u2237 ys\n \u2261\u27e8 subst (\u03bb t \u2192 rev xs [] ++ x \u2237 ys \u2261 rev xs [] ++ t)\n (sym\n ( begin\n (x \u2237 []) ++ ys \u2261\u27e8 ++-\u2237 x [] ys \u27e9\n x \u2237 ([] ++ ys) \u2261\u27e8 subst (\u03bb t \u2192 x \u2237 ([] ++ ys) \u2261 x \u2237 t)\n (++-leftIdentity LTys)\n refl\n \u27e9\n x \u2237 ys\n \u220e\n )\n )\n refl\n \u27e9\n rev xs [] ++ (x \u2237 []) ++ ys\n \u2261\u27e8 sym $ ++-assoc (rev-ListTree LTxs nilLT) (consLT Tx nilLT) LTys \u27e9\n (rev xs [] ++ (x \u2237 [])) ++ ys\n \u2261\u27e8 subst (\u03bb t \u2192 (rev xs [] ++ (x \u2237 [])) ++ ys \u2261 t ++ ys)\n (sym $ rev-++-commute LTxs (consLT Tx nilLT)) -- IH.\n refl\n \u27e9\n rev xs (x \u2237 []) ++ ys\n \u2261\u27e8 subst (\u03bb t \u2192 rev xs (x \u2237 []) ++ ys \u2261 t ++ ys)\n (sym $ rev-\u2237 x xs [])\n refl\n \u27e9\n rev (x \u2237 xs) [] ++ ys\n \u220e\n\npostulate\n reverse-\u2237 : \u2200 x {ys} \u2192 ListTree ys \u2192\n reverse (x \u2237 ys) \u2261 reverse ys ++ (x \u2237 [])\n-- {-# ATP prove reverse-\u2237 #-}\n\nreverse-++-commute : \u2200 {xs ys} \u2192 ListTree xs \u2192 ListTree ys \u2192\n reverse (xs ++ ys) \u2261 reverse ys ++ reverse xs\nreverse-++-commute {ys = ys} nilLT LTys =\n begin\n reverse ([] ++ ys)\n \u2261\u27e8 subst (\u03bb t \u2192 reverse ([] ++ ys) \u2261 reverse t)\n (++-[] ys)\n refl\n \u27e9\n reverse ys\n \u2261\u27e8 sym (++-rightIdentity (rev-ListTree LTys nilLT)) \u27e9\n reverse ys ++ []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse ys ++ [] \u2261 reverse ys ++ t)\n (sym (rev-[] []))\n refl\n \u27e9\n reverse ys ++ reverse []\n \u220e\n\nreverse-++-commute (consLT {x} {xs} Tx LTxs) nilLT =\n begin\n reverse ((x \u2237 xs) ++ [])\n \u2261\u27e8 subst (\u03bb t \u2192 reverse ((x \u2237 xs) ++ []) \u2261 reverse t)\n (++-rightIdentity (consLT Tx LTxs))\n refl\n \u27e9\n reverse (x \u2237 xs)\n \u2261\u27e8 sym (++-[] (reverse (x \u2237 xs))) \u27e9\n [] ++ reverse (x \u2237 xs)\n \u2261\u27e8 subst (\u03bb t \u2192 [] ++ reverse (x \u2237 xs) \u2261 t ++ reverse (x \u2237 xs))\n (sym (rev-[] []))\n refl\n \u27e9\n reverse [] ++ reverse (x \u2237 xs)\n \u220e\n\nreverse-++-commute (consLT {x} {xs} Tx LTxs) (consLT {y} {ys} Ty LTys) =\n begin\n reverse ((x \u2237 xs) ++ y \u2237 ys) \u2261\u27e8 refl \u27e9\n rev ((x \u2237 xs) ++ y \u2237 ys) []\n \u2261\u27e8 subst (\u03bb t \u2192 rev ((x \u2237 xs) ++ y \u2237 ys) [] \u2261 rev t [])\n (++-\u2237 x xs (y \u2237 ys))\n refl\n \u27e9\n rev (x \u2237 (xs ++ y \u2237 ys)) [] \u2261\u27e8 rev-\u2237 x (xs ++ y \u2237 ys) [] \u27e9\n rev (xs ++ y \u2237 ys) (x \u2237 [])\n \u2261\u27e8 rev-++-commute (++-ListTree LTxs (consLT Ty LTys)) (consLT Tx nilLT) \u27e9\n rev (xs ++ y \u2237 ys) [] ++ (x \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 rev (xs ++ y \u2237 ys) [] ++ (x \u2237 []) \u2261 t ++ (x \u2237 []))\n refl\n refl\n \u27e9\n reverse (xs ++ y \u2237 ys) ++ (x \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (xs ++ y \u2237 ys) ++ (x \u2237 []) \u2261 t ++ (x \u2237 []))\n (reverse-++-commute LTxs (consLT Ty LTys)) -- IH.\n refl\n \u27e9\n (reverse (y \u2237 ys) ++ reverse xs) ++ x \u2237 []\n \u2261\u27e8 ++-assoc (rev-ListTree (consLT Ty LTys) nilLT)\n (rev-ListTree LTxs nilLT)\n (consLT Tx nilLT)\n \u27e9\n reverse (y \u2237 ys) ++ reverse xs ++ x \u2237 []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ reverse xs ++ x \u2237 [] \u2261\n reverse (y \u2237 ys) ++ t ++ x \u2237 [])\n refl\n refl\n \u27e9\n reverse (y \u2237 ys) ++ rev xs [] ++ x \u2237 []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ rev xs [] ++ x \u2237 [] \u2261\n reverse (y \u2237 ys) ++ t)\n (sym $ rev-++-commute LTxs (consLT Tx nilLT))\n refl\n \u27e9\n reverse (y \u2237 ys) ++ rev xs (x \u2237 [])\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ rev xs (x \u2237 []) \u2261\n reverse (y \u2237 ys) ++ t)\n (sym $ rev-\u2237 x xs [])\n refl\n \u27e9\n reverse (y \u2237 ys) ++ rev (x \u2237 xs) []\n \u2261\u27e8 subst (\u03bb t \u2192 reverse (y \u2237 ys) ++ rev (x \u2237 xs) [] \u2261\n reverse (y \u2237 ys) ++ t)\n refl\n refl\n \u27e9\n reverse (y \u2237 ys) ++ reverse (x \u2237 xs)\n \u220e\n\nmap-++-commute : \u2200 f {xs ys} \u2192 (\u2200 {x} \u2192 Tree x \u2192 Tree (f \u00b7 x)) \u2192\n ListTree xs \u2192 ListTree ys \u2192\n map f (xs ++ ys) \u2261 map f xs ++ map f ys\nmap-++-commute f {ys = ys} fTree nilLT LTys =\n begin\n map f ([] ++ ys)\n \u2261\u27e8 subst (\u03bb t \u2192 map f ([] ++ ys) \u2261 map f t)\n (++-[] ys)\n refl\n \u27e9\n map f ys\n \u2261\u27e8 sym (++-leftIdentity (map-ListTree f fTree LTys)) \u27e9\n [] ++ map f ys\n \u2261\u27e8 subst (\u03bb t \u2192 [] ++ map f ys \u2261 t ++ map f ys)\n (sym (map-[] f))\n refl\n \u27e9\n map f [] ++ map f ys\n \u220e\n\nmap-++-commute f {ys = ys} fTree (consLT {x} {xs} Tx LTxs) LTys =\n begin\n map f ((x \u2237 xs) ++ ys)\n \u2261\u27e8 subst (\u03bb t \u2192 map f ((x \u2237 xs) ++ ys) \u2261 map f t)\n (++-\u2237 x xs ys)\n refl\n \u27e9\n map f (x \u2237 xs ++ ys)\n \u2261\u27e8 map-\u2237 f x (xs ++ ys) \u27e9\n f \u00b7 x \u2237 map f (xs ++ ys)\n \u2261\u27e8 subst (\u03bb t \u2192 f \u00b7 x \u2237 map f (xs ++ ys) \u2261 f \u00b7 x \u2237 t)\n (map-++-commute f fTree LTxs LTys) -- IH.\n refl\n \u27e9\n f \u00b7 x \u2237 (map f xs ++ map f ys)\n \u2261\u27e8 sym (++-\u2237 (f \u00b7 x) (map f xs) (map f ys)) \u27e9\n (f \u00b7 x \u2237 map f xs) ++ map f ys\n \u2261\u27e8 subst (\u03bb t \u2192 (f \u00b7 x \u2237 map f xs) ++ map f ys \u2261 t ++ map f ys)\n (sym (map-\u2237 f x xs))\n refl\n \u27e9\n map f (x \u2237 xs) ++ map f ys\n \u220e\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f5174554f8808bb36ec976d1d6d35f45e84062f0","subject":"universe.agda","message":"universe.agda\n","repos":"crypto-agda\/crypto-agda","old_file":"universe.agda","new_file":"universe.agda","new_contents":"module universe where\n\nimport Level as L\nopen import Data.Nat.NP using (\u2115; _+_; _*_; _^_)\nopen import Data.Bits using (Bit)\nopen import Data.Unit using (\u22a4)\nopen import Data.Product using (_\u00d7_; _,_) renaming (zip to \u00d7-zip)\nopen import Data.Vec using (Vec)\n\n-- An interface for small, finite & discrete universes of types.\nrecord Universe {t} (T : Set t) : Set t where\n constructor mk\n field\n `\u22a4 : T\n `Bit : T\n _`\u00d7_ : T \u2192 T \u2192 T\n _`^_ : T \u2192 \u2115 \u2192 T\n\n `Vec : T \u2192 \u2115 \u2192 T\n `Vec A n = A `^ n\n\n `Bits : \u2115 \u2192 T\n `Bits n = `Bit `^ n\n\n infixr 2 _`\u00d7_\n infixl 2 _`^_\n\n-- In Set-U, types are simply represented by Agda types (Set).\nSet-U : Universe Set\nSet-U = mk \u22a4 Bit _\u00d7_ Vec\n\n-- In Bits-U, a type is represented by a natural number\n-- representing the width of the type in a binary representation.\n-- A natural embedding in Set is the Bits type (aka Vec Bool).\nBits-U : Universe \u2115\nBits-U = mk 0 1 _+_ _*_\n\n-- In Fin-U, a type is represented by a natural number\n-- representing the cardinality of the type.\n-- A natural embedding in Set is the Fin type.\nFin-U : Universe \u2115\nFin-U = mk 1 2 _*_ _^_\n\n-- In \u22a4-U, there is only one possible type.\n\u22a4-U : Universe \u22a4\n\u22a4-U = mk _ _ _ _\n\n-- Take the product of two universes. All types have two components, one from\n-- each of the forming universes.\n\u00d7-U : \u2200 {s t} {S : Set s} {T : Set t} \u2192 Universe S \u2192 Universe T \u2192 Universe (S \u00d7 T)\n\u00d7-U S-U T-U = mk (S.`\u22a4 , T.`\u22a4) (S.`Bit , T.`Bit) (\u00d7-zip S._`\u00d7_ T._`\u00d7_)\n (\u03bb { (A\u2080 , A\u2081) n \u2192 S.`Vec A\u2080 n , T.`Vec A\u2081 n })\n where module S = Universe S-U\n module T = Universe T-U\n\n-- Sym-U is a \u201csymantic\u201d (a mix of syntax and semantics) representation\n-- for types. Symantic types are those defined only in term of the\n-- Universe interface. [See the \u201cFinally Tagless\u201d approach by Oleg Kiselyov.]\nSym-U : \u2200 t \u2192 Set (L.suc t)\nSym-U t = \u2200 {T : Set t} \u2192 Universe T \u2192 T\n\n-- Abstract syntax tree from types.\ndata Ty : Set where\n \u22a4\u2032 Bit\u2032 : Ty\n _\u00d7\u2032_ : Ty \u2192 Ty \u2192 Ty\n _^\u2032_ : Ty \u2192 \u2115 \u2192 Ty\n\n-- Ty-U is the universe of the syntactic represented types.\nTy-U : Universe Ty\nTy-U = mk \u22a4\u2032 Bit\u2032 _\u00d7\u2032_ _^\u2032_\n\n-- Turn a syntactic type into a symantic one.\n-- Alternatively:\n-- * a syntactic type is turned into a type of any $given universe.\n-- * the catamorphism for syntactic types.\nfold-U : \u2200 {t} \u2192 Ty \u2192 Sym-U\nfold-U {_} {T} u\u2080 uni = go u\u2080\n where open Universe uni\n go : Ty \u2192 T\n go \u22a4\u2032 = `\u22a4\n go Bit\u2032 = `Bit\n go (t\u2080 \u00d7\u2032 t\u2081) = go t\u2080 `\u00d7 go t\u2081\n go (t ^\u2032 n) = go t `^ n\n\n{-\n\u03a3-U : \u2200 {t} {T : Set t} \u2192 Universe T \u2192 (P : T \u2192 Set) \u2192 Universe (\u03a3 T P)\n\u03a3-U T-U = ?\n where open Universe T-U\n-}\n\n-- The type of universe unary operators or universe transformers.\nUniOp : \u2200 {s t} (S : Set s) (T : Set t) \u2192 Set _\nUniOp S T = Universe S \u2192 Universe T\n\n-- The type of universe binary operators.\nUniOp\u2082 : \u2200 {r s t} (R : Set r) (S : Set s) (T : Set t) \u2192 Set _\nUniOp\u2082 R S T = Universe R \u2192 Universe S \u2192 Universe T\n","old_contents":"module universe where\n\nimport Level as L\nopen import Data.Nat.NP using (\u2115; _+_; _*_; _^_)\nopen import Data.Bits using (Bit)\nopen import Data.Unit using (\u22a4)\nopen import Data.Product using (_\u00d7_; _,_) renaming (zip to \u00d7-zip)\nopen import Data.Vec using (Vec)\n\nrecord Universe {t} (T : Set t) : Set t where\n constructor mk\n field\n `\u22a4 : T\n `Bit : T\n _`\u00d7_ : T \u2192 T \u2192 T\n _`^_ : T \u2192 \u2115 \u2192 T\n\n `Vec : T \u2192 \u2115 \u2192 T\n `Vec A n = A `^ n\n\n `Bits : \u2115 \u2192 T\n `Bits n = `Bit `^ n\n\n infixr 2 _`\u00d7_\n infixl 2 _`^_\n\nSet-U : Universe Set\nSet-U = mk \u22a4 Bit _\u00d7_ Vec\n\nwidth-U : Universe \u2115\nwidth-U = mk 0 1 _+_ _*_\n\ncard-U : Universe \u2115\ncard-U = mk 1 2 _*_ _^_\n\n\u22a4-U : Universe \u22a4\n\u22a4-U = mk _ _ _ _\n\n\u00d7-U : \u2200 {s t} {S : Set s} {T : Set t} \u2192 Universe S \u2192 Universe T \u2192 Universe (S \u00d7 T)\n\u00d7-U S-U T-U = mk (S.`\u22a4 , T.`\u22a4) (S.`Bit , T.`Bit) (\u00d7-zip S._`\u00d7_ T._`\u00d7_)\n (\u03bb { (A\u2080 , A\u2081) n \u2192 S.`Vec A\u2080 n , T.`Vec A\u2081 n })\n where module S = Universe S-U\n module T = Universe T-U\n\nSym-U : \u2200 t \u2192 Set (L.suc t)\nSym-U t = \u2200 {T : Set t} \u2192 Universe T\n\ndata U : Set where\n \u22a4\u2032 Bit\u2032 : U\n _\u00d7\u2032_ : U \u2192 U \u2192 U\n _^\u2032_ : U \u2192 \u2115 \u2192 U\n\nsyn-U : Universe U\nsyn-U = mk \u22a4\u2032 Bit\u2032 _\u00d7\u2032_ _^\u2032_\n\nfold-U : \u2200 {t} {T : Set t} \u2192 Universe T \u2192 U \u2192 T\nfold-U {_} {T} uni = go\n where open Universe uni\n go : U \u2192 T\n go \u22a4\u2032 = `\u22a4\n go Bit\u2032 = `Bit\n go (t\u2080 \u00d7\u2032 t\u2081) = go t\u2080 `\u00d7 go t\u2081\n go (t ^\u2032 n) = go t `^ n\n\nUniOp : \u2200 {s t} (S : Set s) (T : Set t) \u2192 Set _\nUniOp S T = Universe S \u2192 Universe T\n\nUniOp\u2082 : \u2200 {r s t} (R : Set r) (S : Set s) (T : Set t) \u2192 Set _\nUniOp\u2082 R S T = Universe R \u2192 Universe S \u2192 Universe T\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"bd04af9edb07ad3112759a1a95ba54c2c0f3601b","subject":"wip cut","message":"wip cut\n","repos":"crypto-agda\/protocols","old_file":"partensor\/Shallow\/Cut.agda","new_file":"partensor\/Shallow\/Cut.agda","new_contents":"{-# OPTIONS --copatterns #-}\nopen import Function\nopen import Data.Product hiding (zip)\n renaming (_,_ to \u27e8_,_\u27e9; proj\u2081 to fst; proj\u2082 to snd;\n map to \u00d7map)\nopen import Data.Zero\nopen import Data.One\nopen import Data.Two\nopen import Data.Sum\nopen import Data.Nat\n{-\nopen import Data.Vec\nopen import Data.Fin\n-}\n-- open import Data.List\n\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality.NP hiding ([_]; J)\nopen import partensor.Shallow.Dom\nimport partensor.Shallow.Session as Session\nimport partensor.Shallow.Map as Map\nimport partensor.Shallow.Env as Env\nimport partensor.Shallow.Proto as Proto\nopen Session hiding (Ended)\nopen Env hiding (_\/\u2080_; _\/\u2081_; _\/_; Ended)\nopen Proto hiding ()\nopen import partensor.Shallow.Equiv\nopen import partensor.Shallow.Term\n\nmodule partensor.Shallow.Cut where\ninfixl 4 _\u2666Proto'_\n-- things we have but I want better unification\n{-\n _\u2248'_ : \u2200 {\u03b4I \u03b4J} \u2192 Proto \u03b4I \u2192 Proto \u03b4J \u2192 Set\u2081\n \u2248'-refl : \u2200 {\u03b4I}{I : Proto \u03b4I} \u2192 I \u2248' I\n \u2248'-sym : \u2200 {\u03b4I \u03b4J}{I : Proto \u03b4I}{J : Proto \u03b4J} \u2192 I \u2248' J \u2192 J \u2248' I\n \u2248'-trans : \u2200 {\u03b4a \u03b4b \u03b4c}{A : Proto \u03b4a}{B : Proto \u03b4b}{C : Proto \u03b4c} \u2192 A \u2248' B \u2192 B \u2248' C \u2192 A \u2248' C\n-}\npostulate\n _\u2666Proto'_ : \u2200 {\u03b4a \u03b4b}(A : Proto \u03b4a)(B : Proto \u03b4b) \u2192 Proto (\u03b4a \u2666Doms \u03b4b)\n\n{-\ndata DifferentVarsDoms : \u2200 {\u03b4I c d} \u2192 Doms'.[ c ]\u2208 \u03b4I \u2192 Doms'.[ d ]\u2208 \u03b4I \u2192 Set where\n h\/t : \u2200 {a b Db l}\n \u2192 DifferentVarsDoms {Db ,[ a ]}{a}{b} Doms'.here (Doms'.there l)\n t\/h : \u2200 {a b Db l}\n \u2192 DifferentVarsDoms {Db ,[ a ]}{b}{a} (Doms'.there l) Doms'.here\n t\/t : \u2200 {a c d D l l'} \u2192 DifferentVarsDoms {D ,[ a ]}{c}{d} (Doms'.there l) (Doms'.there l')\n\n-- Need to update this, they may point to the same tensor block but different inside it...\n-- boring I know!\nDifferentVars : \u2200 {\u03b4I}{I : Proto \u03b4I}{c d A B} \u2192 [ c \u21a6 A ]\u2208 I \u2192 [ d \u21a6 B ]\u2208 I \u2192 Set\nDifferentVars l l' = DifferentVarsDoms (Proto.forget ([\u21a6]\u2208.lI l)) (Proto.forget ([\u21a6]\u2208.lI l'))\n-}\npostulate\n DifferentVars\u2026 : \u2200 {\u03b4I}{I : Proto \u03b4I}{c d A B} \u2192 [ c \u21a6 A \u2026]\u2208 I \u2192 [ d \u21a6 B \u2026]\u2208 I \u2192 Set\n Diff-sym\u2026 : \u2200 {\u03b4I}{I : Proto \u03b4I}{c d A B}{l : [ c \u21a6 A \u2026]\u2208 I}{l' : [ d \u21a6 B \u2026]\u2208 I}\n \u2192 DifferentVars\u2026 l l' \u2192 DifferentVars\u2026 l' l\n\nrecord DifferentVars {\u03b4I}{I : Proto \u03b4I}{c d A B}(l : [ c \u21a6 A ]\u2208 I)(l' : [ d \u21a6 B ]\u2208 I) : Set where\n constructor mk\n field\n Diff\u2026 : DifferentVars\u2026 ([\u21a6]\u2208.l\u2026 l) ([\u21a6]\u2208.l\u2026 l')\nopen DifferentVars\n\nDiff-sym : \u2200 {\u03b4I}{I : Proto \u03b4I}{c d A B}{l : [ c \u21a6 A ]\u2208 I}{l' : [ d \u21a6 B ]\u2208 I}\n \u2192 DifferentVars l l' \u2192 DifferentVars l' l\nDiff\u2026 (Diff-sym df) = Diff-sym\u2026 (Diff\u2026 df)\n\ndata SameVar? {\u03b4I}{I : Proto \u03b4I} : \u2200 {c c' A A'} \u2192 [ c \u21a6 A \u2026]\u2208 I \u2192 [ c' \u21a6 A' \u2026]\u2208 I \u2192 Set\u2081 where\n same : \u2200 {c A}{l : [ c \u21a6 A \u2026]\u2208 I} \u2192 SameVar? l l\n diff : \u2200 {c c' A B}{l : [ c \u21a6 A \u2026]\u2208 I}{l' : [ c' \u21a6 B \u2026]\u2208 I} \u2192 DifferentVars\u2026 l l' \u2192 SameVar? l l'\n\npostulate\n sameVar? : \u2200 {\u03b4I}{I : Proto \u03b4I}{c c' A A'}(l : [ c \u21a6 A \u2026]\u2208 I)(l' : [ c' \u21a6 A' \u2026]\u2208 I) \u2192 SameVar? l l'\n\npostulate\n TC-conv : \u2200 {\u03b4I \u03b4J}{I : Proto \u03b4I}{J : Proto \u03b4J}\n \u2192 I \u2248 J \u2192 TC\u27e8 I \u27e9 \u2192 TC\u27e8 J \u27e9\n\n \u2666-assoc : \u2200 {\u03b4a \u03b4b \u03b4c}{A : Proto \u03b4a}{B : Proto \u03b4b}{C : Proto \u03b4c} \u2192 A \u2666Proto' (B \u2666Proto' C) \u2248 (A \u2666Proto' B) \u2666Proto' C\n \u2666-com : \u2200 {\u03b4a \u03b4b}{A : Proto \u03b4a}{B : Proto \u03b4b} \u2192 (A \u2666Proto' B) \u2248 (B \u2666Proto' A)\n \u2666-cong\u2082 : \u2200 {\u03b4a \u03b4b \u03b4c \u03b4d}{A : Proto \u03b4a}{B : Proto \u03b4b}{C : Proto \u03b4c}{D : Proto \u03b4d}\n \u2192 A \u2248 B \u2192 C \u2248 D \u2192 A \u2666Proto' C \u2248 B \u2666Proto' D\n \u2666-com, : \u2200 {\u03b4a \u03b4 \u03b4b}{A : Proto \u03b4a}{B : Proto \u03b4b}{E : Env \u03b4} \u2192 (A ,[ E ]) \u2666Proto' B \u2248 (A \u2666Proto' B),[ E ]\n\n\n \u2208\u2666\u2080\u2026 : \u2200 {\u03b4\u2080 \u03b4\u2081 c A}{I\u2080 : Proto \u03b4\u2080}{I\u2081 : Proto \u03b4\u2081} \u2192 [ c \u21a6 A \u2026]\u2208 I\u2080 \u2192 [ c \u21a6 A \u2026]\u2208 (I\u2080 \u2666Proto' I\u2081)\n \u2208\u2666\u2081\u2026 : \u2200 {\u03b4\u2080 \u03b4\u2081 c A}{I\u2080 : Proto \u03b4\u2080}{I\u2081 : Proto \u03b4\u2081} \u2192 [ c \u21a6 A \u2026]\u2208 I\u2081 \u2192 [ c \u21a6 A \u2026]\u2208 (I\u2080 \u2666Proto' I\u2081)\n\n\n\n move\u2026 : \u2200 {\u03b4I}{I : Proto \u03b4I}{c d A B}(l : [ c \u21a6 A \u2026]\u2208 I)(l' : [ d \u21a6 B \u2026]\u2208 I) \u2192 DifferentVars\u2026 l l'\n \u2192 [ d \u21a6 B \u2026]\u2208 (I [\/\u2026] l)\n move-compute\u2026 : \u2200 {\u03b4I}{I : Proto \u03b4I}{c d A B}(l : [ c \u21a6 A \u2026]\u2208 I)(l' : [ d \u21a6 B \u2026]\u2208 I)(l\/=l' : DifferentVars\u2026 l l')\n \u2192 (I [\/\u2026] l) [\/\u2026] move\u2026 l l' l\/=l' \u2248 (I [\/\u2026] l) \/Ds Proto.forget ([\u21a6\u2026]\u2208.lI l')\n\n\u2208\u2666\u2080 : \u2200 {\u03b4\u2080 \u03b4\u2081 c A}{I\u2080 : Proto \u03b4\u2080}{I\u2081 : Proto \u03b4\u2081} \u2192 [ c \u21a6 A ]\u2208 I\u2080 \u2192 [ c \u21a6 A ]\u2208 (I\u2080 \u2666Proto' I\u2081)\n\u2208\u2666\u2080 (mk l\u2026 E\/c) = mk (\u2208\u2666\u2080\u2026 l\u2026) {!!}\n\n\u2208\u2666\u2081 : \u2200 {\u03b4\u2080 \u03b4\u2081 c A}{I\u2080 : Proto \u03b4\u2080}{I\u2081 : Proto \u03b4\u2081} \u2192 [ c \u21a6 A ]\u2208 I\u2081 \u2192 [ c \u21a6 A ]\u2208 (I\u2080 \u2666Proto' I\u2081)\n\u2208\u2666\u2081 (mk l\u2026 E\/c) = {!!}\n\n\u2208\u2666\u2080-compute : \u2200 {\u03b4\u2080 \u03b4\u2081 c A}{I\u2080 : Proto \u03b4\u2080}{I\u2081 : Proto \u03b4\u2081}(l : [ c \u21a6 A ]\u2208 I\u2080) \u2192\n (I\u2080 \u2666Proto' I\u2081) [\/] (\u2208\u2666\u2080 l) \u2248 (I\u2080 [\/] l) \u2666Proto' I\u2081\n\u2208\u2666\u2080-compute = {!!}\n\n\u2208\u2666\u2081-compute : \u2200 {\u03b4\u2080 \u03b4\u2081 c A}{I\u2080 : Proto \u03b4\u2080}{I\u2081 : Proto \u03b4\u2081}(l : [ c \u21a6 A ]\u2208 I\u2081) \u2192\n (I\u2080 \u2666Proto' I\u2081) [\/] (\u2208\u2666\u2081 l) \u2248 I\u2080 \u2666Proto' (I\u2081 [\/] l)\n\u2208\u2666\u2081-compute = {!!}\n\nmove : \u2200 {\u03b4I}{I : Proto \u03b4I}{c d A B}(l : [ c \u21a6 A ]\u2208 I)(l' : [ d \u21a6 B ]\u2208 I) \u2192 DifferentVars l l'\n \u2192 [ d \u21a6 B ]\u2208 (I [\/] l)\nmove (mk l X) (mk l' Y) df = mk (move\u2026 l l' (Diff\u2026 df)) {!!}\n\n\nTC-\u2208\u214b : \u2200 {\u03b4I \u03b4K c A B}{I : Proto \u03b4I}{K : Proto \u03b4K}(l : [ c \u21a6 A \u214b B ]\u2208 I)\n \u2192 (\u2200 {d e \u03b4J}{J : Proto \u03b4J} (l : [ d \u21a6 A ]\u2208 J)(l' : [ e \u21a6 B ]\u2208 J) \u2192 DifferentVars l l' \u2192 TC\u27e8 J \u27e9\n \u2192 TC\u27e8 ((J \/ [\u21a6]\u2208.lI l) \/Ds Proto.forget ([\u21a6]\u2208.lI l')) \u2666Proto' K \u27e9)\n \u2192 TC\u27e8 I \u27e9 \u2192 TC\u27e8 I [\/] l \u2666Proto' K \u27e9\nTC-\u2208\u214b l cont (TC-\u2297-out l\u2081 \u03c3s \u03c3E A0 P\u2080 P\u2081) with sameVar? ([\u21a6]\u2208.l\u2026 l) l\u2081\n... | X = {!!}\nTC-\u2208\u214b l cont (TC-\u214b-inp l\u2081 P) with sameVar? ([\u21a6]\u2208.l\u2026 l) ([\u21a6]\u2208.l\u2026 l\u2081)\nTC-\u2208\u214b (mk l y) cont (TC-\u214b-inp (mk .l x) P) | same = TC-conv (\u2666-cong\u2082 (\u2248-trans (\u2248,[end] _) (\u2248,[end] _)) \u2248-refl) (cont (mk (mk (there here) here) _) (mk (mk here here) _) {!!} (TC-conv \u2248-refl (P c\u2080 c\u2081)))\n where\n postulate\n c\u2080 c\u2081 : _\nTC-\u2208\u214b l cont (TC-\u214b-inp l\u2081 P) | diff l\/=l\u2081 = TC-\u214b-inp (\u2208\u2666\u2080 (move l l\u2081 (mk l\/=l\u2081))) (\u03bb c\u2080 c\u2081 \u2192\n TC-conv (\u2248-trans \u2666-com, (\u2248,[] (\u2248-trans \u2666-com, (\u2248,[] (\u2248-sym (\u2248-trans (\u2208\u2666\u2080-compute (move l l\u2081 (mk l\/=l\u2081)))\n (\u2666-cong\u2082 (\u2248-trans (move-compute\u2026 ([\u21a6]\u2208.l\u2026 l) ([\u21a6]\u2208.l\u2026 l\u2081) l\/=l\u2081) \n (\u2248-trans {!!}\n (\u2248-sym (move-compute\u2026 _ _ _)))) \u2248-refl))) \u223c-refl)) \u223c-refl))\n (TC-\u2208\u214b (there[] (there[] (move l\u2081 l (Diff-sym (mk l\/=l\u2081))))) cont (P c\u2080 c\u2081)))\nTC-\u2208\u214b l cont (TC-end E) = {!!}\nTC-\u2208\u214b l cont (TC-split \u03c3s A1 P P\u2081) = {!!}\n\n{-\nTC-\u2208\u2297 : \u2200 {\u03b4I \u03b4K c A B}{I : Proto \u03b4I}{K : Proto \u03b4K}(l : [ c \u21a6 A \u2297 B ]\u2208 I)\n \u2192 (\u2200 {d e \u03b4J\u2080 \u03b4J\u2081}{J\u2080 : Proto \u03b4J\u2080}{J\u2081 : Proto \u03b4J\u2081}\n (l\u2080 : [ d \u21a6 A ]\u2208 J\u2080)(l\u2081 : [ e \u21a6 B ]\u2208 J\u2081) \u2192 TC\u27e8 J\u2080 \u27e9 \u2192 TC\u27e8 J\u2081 \u27e9\n \u2192 TC\u27e8 (J\u2080 [\/] l\u2080 \u2666Proto' J\u2081 [\/] l\u2081) \u2666Proto' K \u27e9)\n \u2192 TC\u27e8 I \u27e9 \u2192 TC\u27e8 I [\/] l \u2666Proto' K \u27e9\nTC-\u2208\u2297 = {!!}\n\n\n{-\nTC-cut :\n \u2200 {c\u2080 c\u2081 S\u2080 S\u2081 \u03b4\u2080 \u03b4\u2081}{I\u2080 : Proto \u03b4\u2080}{I\u2081 : Proto \u03b4\u2081}\n (D : Dual S\u2080 S\u2081)\n (l\u2080 : [ c\u2080 \u21a6 S\u2080 ]\u2208 I\u2080)(l\u2081 : [ c\u2081 \u21a6 S\u2081 ]\u2208 I\u2081)\n (P\u2080 : TC\u27e8 I\u2080 \u27e9) (P\u2081 : TC\u27e8 I\u2081 \u27e9)\n \u2192 TC\u27e8 (I\u2080 [\/] l\u2080) \u2666Proto' (I\u2081 [\/] l\u2081) \u27e9\nTC-cut end \u03c3s A0 P\u2080 P\u2081 = {!TC-split \u03c3s A0 P\u2080 P\u2081!}\nTC-cut (\u2297\u214b D D\u2081 D\u2082 D\u2083) l\u2080 l\u2081 P\u2080 P\u2081 = TC-conv \u2666-com\n (TC-\u2208\u214b l\u2081 (\u03bb d e d\/=e a'b' \u2192 TC-conv (\u2248-trans \u2666-com (\u2666-cong\u2082 (\u2248-trans (move-compute {!e!} {!d!} {!(Diff-sym d\/=e)!}) {!Proto.forget!}) \u2248-refl))\n (TC-\u2208\u2297 l\u2080 (\u03bb d' e' a b \u2192 TC-conv (\u2248-trans (\u2666-cong\u2082 \u2248-refl\n (\u2208\u2666\u2081-compute (move {!e!} {!d!} {!(Diff-sym d\/=e)!}))) \u2666-assoc)\n (TC-cut D d' (\u2208\u2666\u2081 (move {!e!} {!d!} {!(Diff-sym d\/=e)!})) a (TC-cut D\u2082 e' e b a'b')))\n P\u2080))\n P\u2081)\nTC-cut (\u214b\u2297 D D\u2081 D\u2082 D\u2083) l\u2080 l\u2081 P\u2080 P\u2081 = TC-conv \u2248-refl\n (TC-\u2208\u214b l\u2080 (\u03bb {_}{_}{_}{J} d e d\/=e ab \u2192 TC-conv \u2666-com\n (TC-\u2208\u2297 l\u2081 (\u03bb {_}{_}{_}{_}{J\u2080}{J\u2081} d' e' a b \u2192 let X = TC-cut D\u2081 d' d a ab\n in TC-conv (\u2248-trans (\u2666-cong\u2082 \u2248-refl (\u2208\u2666\u2081-compute (move d e d\/=e)))\n (\u2248-trans \u2666-assoc (\u2666-cong\u2082 \u2666-com (move-compute d e (mk d\/=e)))))\n (TC-cut D\u2083 e' (\u2208\u2666\u2081 (move d e d\/=e)) b X))\n P\u2081)) P\u2080)\n\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --copatterns #-}\nopen import Function\nopen import Data.Product hiding (zip)\n renaming (_,_ to \u27e8_,_\u27e9; proj\u2081 to fst; proj\u2082 to snd;\n map to \u00d7map)\nopen import Data.Zero\nopen import Data.One\nopen import Data.Two\nopen import Data.Sum\nopen import Data.Nat\n{-\nopen import Data.Vec\nopen import Data.Fin\n-}\n-- open import Data.List\n\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality.NP hiding ([_]; J)\nopen import partensor.Shallow.Dom\nimport partensor.Shallow.Session as Session\nimport partensor.Shallow.Map as Map\nimport partensor.Shallow.Env as Env\nimport partensor.Shallow.Proto as Proto\nopen Session hiding (Ended)\nopen Env hiding (_\/\u2080_; _\/\u2081_; _\/_; Ended)\nopen Proto hiding ()\nopen import partensor.Shallow.Equiv\nopen import partensor.Shallow.Term\n\nmodule partensor.Shallow.Cut where\ninfixl 4 _\u2666Proto'_\n-- things we have but I want better unification\n{-\n _\u2248'_ : \u2200 {\u03b4I \u03b4J} \u2192 Proto \u03b4I \u2192 Proto \u03b4J \u2192 Set\u2081\n \u2248'-refl : \u2200 {\u03b4I}{I : Proto \u03b4I} \u2192 I \u2248' I\n \u2248'-sym : \u2200 {\u03b4I \u03b4J}{I : Proto \u03b4I}{J : Proto \u03b4J} \u2192 I \u2248' J \u2192 J \u2248' I\n \u2248'-trans : \u2200 {\u03b4a \u03b4b \u03b4c}{A : Proto \u03b4a}{B : Proto \u03b4b}{C : Proto \u03b4c} \u2192 A \u2248' B \u2192 B \u2248' C \u2192 A \u2248' C\n-}\npostulate\n _\u2666Proto'_ : \u2200 {\u03b4a \u03b4b}(A : Proto \u03b4a)(B : Proto \u03b4b) \u2192 Proto (\u03b4a \u2666Doms \u03b4b)\n\n{-\ndata DifferentVarsDoms : \u2200 {\u03b4I c d} \u2192 Doms'.[ c ]\u2208 \u03b4I \u2192 Doms'.[ d ]\u2208 \u03b4I \u2192 Set where\n h\/t : \u2200 {a b Db l}\n \u2192 DifferentVarsDoms {Db ,[ a ]}{a}{b} Doms'.here (Doms'.there l)\n t\/h : \u2200 {a b Db l}\n \u2192 DifferentVarsDoms {Db ,[ a ]}{b}{a} (Doms'.there l) Doms'.here\n t\/t : \u2200 {a c d D l l'} \u2192 DifferentVarsDoms {D ,[ a ]}{c}{d} (Doms'.there l) (Doms'.there l')\n\n-- Need to update this, they may point to the same tensor block but different inside it...\n-- boring I know!\nDifferentVars : \u2200 {\u03b4I}{I : Proto \u03b4I}{c d A B} \u2192 [ c \u21a6 A ]\u2208 I \u2192 [ d \u21a6 B ]\u2208 I \u2192 Set\nDifferentVars l l' = DifferentVarsDoms (Proto.forget ([\u21a6]\u2208.lI l)) (Proto.forget ([\u21a6]\u2208.lI l'))\n-}\npostulate\n DifferentVars\u2026 : \u2200 {\u03b4I}{I : Proto \u03b4I}{c d A B} \u2192 [ c \u21a6 A \u2026]\u2208 I \u2192 [ d \u21a6 B \u2026]\u2208 I \u2192 Set\n Diff-sym\u2026 : \u2200 {\u03b4I}{I : Proto \u03b4I}{c d A B}{l : [ c \u21a6 A \u2026]\u2208 I}{l' : [ d \u21a6 B \u2026]\u2208 I}\n \u2192 DifferentVars\u2026 l l' \u2192 DifferentVars\u2026 l' l\n\nrecord DifferentVars {\u03b4I}{I : Proto \u03b4I}{c d A B}(l : [ c \u21a6 A ]\u2208 I)(l' : [ d \u21a6 B ]\u2208 I) : Set where\n constructor mk\n field\n Diff\u2026 : DifferentVars\u2026 ([\u21a6]\u2208.l\u2026 l) ([\u21a6]\u2208.l\u2026 l')\nopen DifferentVars\n\nDiff-sym : \u2200 {\u03b4I}{I : Proto \u03b4I}{c d A B}{l : [ c \u21a6 A ]\u2208 I}{l' : [ d \u21a6 B ]\u2208 I}\n \u2192 DifferentVars l l' \u2192 DifferentVars l' l\nDiff\u2026 (Diff-sym df) = Diff-sym\u2026 (Diff\u2026 df)\n\ndata SameVar? {\u03b4I}{I : Proto \u03b4I} : \u2200 {c c' A A'} \u2192 [ c \u21a6 A \u2026]\u2208 I \u2192 [ c' \u21a6 A' \u2026]\u2208 I \u2192 Set\u2081 where\n same : \u2200 {c A}{l : [ c \u21a6 A \u2026]\u2208 I} \u2192 SameVar? l l\n diff : \u2200 {c c' A B}{l : [ c \u21a6 A \u2026]\u2208 I}{l' : [ c' \u21a6 B \u2026]\u2208 I} \u2192 DifferentVars\u2026 l l' \u2192 SameVar? l l'\n\npostulate\n sameVar? : \u2200 {\u03b4I}{I : Proto \u03b4I}{c c' A A'}(l : [ c \u21a6 A \u2026]\u2208 I)(l' : [ c' \u21a6 A' \u2026]\u2208 I) \u2192 SameVar? l l'\n\npostulate\n TC-conv : \u2200 {\u03b4I \u03b4J}{I : Proto \u03b4I}{J : Proto \u03b4J}\n \u2192 I \u2248 J \u2192 TC\u27e8 I \u27e9 \u2192 TC\u27e8 J \u27e9\n\n\n move\u2026 : \u2200 {\u03b4I}{I : Proto \u03b4I}{c d A B}(l : [ c \u21a6 A \u2026]\u2208 I)(l' : [ d \u21a6 B \u2026]\u2208 I) \u2192 DifferentVars\u2026 l l'\n \u2192 [ d \u21a6 B \u2026]\u2208 (I [\/\u2026] l)\n\n \u2208\u2666\u2080 : \u2200 {\u03b4\u2080 \u03b4\u2081 c A}{I\u2080 : Proto \u03b4\u2080}{I\u2081 : Proto \u03b4\u2081} \u2192 [ c \u21a6 A ]\u2208 I\u2080 \u2192 [ c \u21a6 A ]\u2208 (I\u2080 \u2666Proto' I\u2081)\n \u2208\u2666\u2081 : \u2200 {\u03b4\u2080 \u03b4\u2081 c A}{I\u2080 : Proto \u03b4\u2080}{I\u2081 : Proto \u03b4\u2081} \u2192 [ c \u21a6 A ]\u2208 I\u2081 \u2192 [ c \u21a6 A ]\u2208 (I\u2080 \u2666Proto' I\u2081)\n \u2666-assoc : \u2200 {\u03b4a \u03b4b \u03b4c}{A : Proto \u03b4a}{B : Proto \u03b4b}{C : Proto \u03b4c} \u2192 A \u2666Proto' (B \u2666Proto' C) \u2248 (A \u2666Proto' B) \u2666Proto' C\n \u2666-com : \u2200 {\u03b4a \u03b4b}{A : Proto \u03b4a}{B : Proto \u03b4b} \u2192 (A \u2666Proto' B) \u2248 (B \u2666Proto' A)\n \u2208\u2666\u2080-compute : \u2200 {\u03b4\u2080 \u03b4\u2081 c A}{I\u2080 : Proto \u03b4\u2080}{I\u2081 : Proto \u03b4\u2081}(l : [ c \u21a6 A ]\u2208 I\u2080) \u2192\n (I\u2080 \u2666Proto' I\u2081) [\/] (\u2208\u2666\u2080 l) \u2248 (I\u2080 [\/] l) \u2666Proto' I\u2081\n \u2208\u2666\u2081-compute : \u2200 {\u03b4\u2080 \u03b4\u2081 c A}{I\u2080 : Proto \u03b4\u2080}{I\u2081 : Proto \u03b4\u2081}(l : [ c \u21a6 A ]\u2208 I\u2081) \u2192\n (I\u2080 \u2666Proto' I\u2081) [\/] (\u2208\u2666\u2081 l) \u2248 I\u2080 \u2666Proto' (I\u2081 [\/] l)\n \u2666-cong\u2082 : \u2200 {\u03b4a \u03b4b \u03b4c \u03b4d}{A : Proto \u03b4a}{B : Proto \u03b4b}{C : Proto \u03b4c}{D : Proto \u03b4d}\n \u2192 A \u2248 B \u2192 C \u2248 D \u2192 A \u2666Proto' C \u2248 B \u2666Proto' D\n move-compute : \u2200 {\u03b4I}{I : Proto \u03b4I}{c d A B}(l : [ c \u21a6 A \u2026]\u2208 I)(l' : [ d \u21a6 B \u2026]\u2208 I)(l\/=l' : DifferentVars\u2026 l l')\n \u2192 (I [\/\u2026] l) [\/\u2026] move\u2026 l l' l\/=l' \u2248 (I [\/\u2026] l) \/Ds Proto.forget ([\u21a6\u2026]\u2208.lI l')\n\nmove : \u2200 {\u03b4I}{I : Proto \u03b4I}{c d A B}(l : [ c \u21a6 A ]\u2208 I)(l' : [ d \u21a6 B ]\u2208 I) \u2192 DifferentVars l l'\n \u2192 [ d \u21a6 B ]\u2208 (I [\/] l)\nmove (mk l X) (mk l' Y) df = mk (move\u2026 l l' (Diff\u2026 df)) {!!}\n\n\nTC-\u2208\u214b : \u2200 {\u03b4I \u03b4K c A B}{I : Proto \u03b4I}{K : Proto \u03b4K}(l : [ c \u21a6 A \u214b B ]\u2208 I)\n \u2192 (\u2200 {d e \u03b4J}{J : Proto \u03b4J} (l : [ d \u21a6 A ]\u2208 J)(l' : [ e \u21a6 B ]\u2208 J) \u2192 DifferentVars l l' \u2192 TC\u27e8 J \u27e9\n \u2192 TC\u27e8 ((J \/ [\u21a6]\u2208.lI l) \/Ds Proto.forget ([\u21a6]\u2208.lI l')) \u2666Proto' K \u27e9)\n \u2192 TC\u27e8 I \u27e9 \u2192 TC\u27e8 I [\/] l \u2666Proto' K \u27e9\nTC-\u2208\u214b l cont (TC-\u2297-out l\u2081 \u03c3s \u03c3E A0 P\u2080 P\u2081) with sameVar? ([\u21a6]\u2208.l\u2026 l) l\u2081\n... | X = {!!}\nTC-\u2208\u214b l cont (TC-\u214b-inp l\u2081 P) with sameVar? ([\u21a6]\u2208.l\u2026 l) ([\u21a6]\u2208.l\u2026 l\u2081)\nTC-\u2208\u214b (mk l y) cont (TC-\u214b-inp (mk .l x) P) | same = TC-conv (\u2666-cong\u2082 (\u2248-trans (\u2248,[end] _) (\u2248,[end] _)) \u2248-refl) (cont (mk (mk (there here) here) _) (mk (mk here here) _) {!!} (TC-conv \u2248-refl (P c\u2080 c\u2081)))\n where\n postulate\n c\u2080 c\u2081 : _\nTC-\u2208\u214b l cont (TC-\u214b-inp l\u2081 P) | diff l\/=l\u2081 = TC-\u214b-inp (\u2208\u2666\u2080 (move l l\u2081 (mk l\/=l\u2081))) (\u03bb c\u2080 c\u2081 \u2192\n TC-conv {!there[]!} (TC-\u2208\u214b (there[] (there[] (move l\u2081 l (Diff-sym (mk l\/=l\u2081))))) cont (P c\u2080 c\u2081)))\nTC-\u2208\u214b l cont (TC-end E) = {!!}\nTC-\u2208\u214b l cont (TC-split \u03c3s A1 P P\u2081) = {!!}\n\nTC-\u2208\u2297 : \u2200 {\u03b4I \u03b4K c A B}{I : Proto \u03b4I}{K : Proto \u03b4K}(l : [ c \u21a6 A \u2297 B ]\u2208 I)\n \u2192 (\u2200 {d e \u03b4J\u2080 \u03b4J\u2081}{J\u2080 : Proto \u03b4J\u2080}{J\u2081 : Proto \u03b4J\u2081}\n (l\u2080 : [ d \u21a6 A ]\u2208 J\u2080)(l\u2081 : [ e \u21a6 B ]\u2208 J\u2081) \u2192 TC\u27e8 J\u2080 \u27e9 \u2192 TC\u27e8 J\u2081 \u27e9\n \u2192 TC\u27e8 (J\u2080 [\/] l\u2080 \u2666Proto' J\u2081 [\/] l\u2081) \u2666Proto' K \u27e9)\n \u2192 TC\u27e8 I \u27e9 \u2192 TC\u27e8 I [\/] l \u2666Proto' K \u27e9\nTC-\u2208\u2297 = {!!}\n\n\nTC-cut :\n \u2200 {c\u2080 c\u2081 S\u2080 S\u2081 \u03b4\u2080 \u03b4\u2081}{I\u2080 : Proto \u03b4\u2080}{I\u2081 : Proto \u03b4\u2081}\n (D : Dual S\u2080 S\u2081)\n (l\u2080 : [ c\u2080 \u21a6 S\u2080 ]\u2208 I\u2080)(l\u2081 : [ c\u2081 \u21a6 S\u2081 ]\u2208 I\u2081)\n (P\u2080 : TC\u27e8 I\u2080 \u27e9) (P\u2081 : TC\u27e8 I\u2081 \u27e9)\n \u2192 TC\u27e8 (I\u2080 [\/] l\u2080) \u2666Proto' (I\u2081 [\/] l\u2081) \u27e9\nTC-cut end \u03c3s A0 P\u2080 P\u2081 = {!TC-split \u03c3s A0 P\u2080 P\u2081!}\nTC-cut (\u2297\u214b D D\u2081 D\u2082 D\u2083) l\u2080 l\u2081 P\u2080 P\u2081 = TC-conv \u2666-com\n (TC-\u2208\u214b l\u2081 (\u03bb d e d\/=e a'b' \u2192 TC-conv (\u2248-trans \u2666-com (\u2666-cong\u2082 (\u2248-trans (move-compute {!e!} {!d!} {!(Diff-sym d\/=e)!}) {!Proto.forget!}) \u2248-refl))\n (TC-\u2208\u2297 l\u2080 (\u03bb d' e' a b \u2192 TC-conv (\u2248-trans (\u2666-cong\u2082 \u2248-refl\n (\u2208\u2666\u2081-compute (move {!e!} {!d!} {!(Diff-sym d\/=e)!}))) \u2666-assoc)\n (TC-cut D d' (\u2208\u2666\u2081 (move {!e!} {!d!} {!(Diff-sym d\/=e)!})) a (TC-cut D\u2082 e' e b a'b')))\n P\u2080))\n P\u2081)\nTC-cut (\u214b\u2297 D D\u2081 D\u2082 D\u2083) l\u2080 l\u2081 P\u2080 P\u2081 = TC-conv \u2248-refl\n (TC-\u2208\u214b l\u2080 (\u03bb {_}{_}{_}{J} d e d\/=e ab \u2192 TC-conv \u2666-com\n (TC-\u2208\u2297 l\u2081 (\u03bb {_}{_}{_}{_}{J\u2080}{J\u2081} d' e' a b \u2192 let X = TC-cut D\u2081 d' d a ab\n in TC-conv (\u2248-trans (\u2666-cong\u2082 \u2248-refl (\u2208\u2666\u2081-compute (move d e d\/=e)))\n (\u2248-trans \u2666-assoc (\u2666-cong\u2082 \u2666-com (move-compute d e (mk d\/=e)))))\n (TC-cut D\u2083 e' (\u2208\u2666\u2081 (move d e d\/=e)) b X))\n P\u2081)) P\u2080)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"ba05583275f6c5a8cd56f8736ba5d795a68c59bf","subject":"Induce change algebras from abelian groups.","message":"Induce change algebras from abelian groups.\n\nOld-commit-hash: 88f468a21264dd66570d65288e4ee1357f848428\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Change\/Algebra.agda","new_file":"Base\/Change\/Algebra.agda","new_contents":"module Base.Change.Algebra where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Change Algebras\n\nrecord IsChangeAlgebra\n {c} {d}\n {Carrier : Set c}\n (Change : Carrier \u2192 Set d)\n (update : (v : Carrier) (dv : Change v) \u2192 Carrier)\n (diff : (u v : Carrier) \u2192 Change v) : Set (c \u2294 d) where\n field\n update-diff : \u2200 u v \u2192 update v (diff u v) \u2261 u\n\n nil : \u2200 v \u2192 Change v\n nil v = diff v v\n\n update-nil : \u2200 v \u2192 update v (nil v) \u2261 v\n update-nil v = update-diff v v\n\nrecord ChangeAlgebra {c} \u2113\n (Carrier : Set c) : Set (c \u2294 suc \u2113) where\n field\n Change : Carrier \u2192 Set \u2113\n update : (v : Carrier) (dv : Change v) \u2192 Carrier\n diff : (u v : Carrier) \u2192 Change v\n\n isChangeAlgebra : IsChangeAlgebra Change update diff\n\n\n infixl 6 update diff\n\n open IsChangeAlgebra isChangeAlgebra public\n\nopen ChangeAlgebra {{...}} public\n using\n ( update-diff\n ; update-nil\n ; nil\n )\n renaming\n ( Change to \u0394\n ; update to _\u229e_\n ; diff to _\u229f_\n )\n\n-- Families of change algebras\n\nrecord ChangeAlgebraFamily {a p} \u2113 {A : Set a} (P : A \u2192 Set p): Set (suc \u2113 \u2294 p \u2294 a) where\n constructor\n family\n field\n change-algebra : \u2200 x \u2192 ChangeAlgebra \u2113 (P x)\n\n module _ x where\n open ChangeAlgebra (change-algebra x) public\n\nmodule Family = ChangeAlgebraFamily {{...}}\n\nopen Family public\n using\n (\n )\n renaming\n ( Change to \u0394\u208d_\u208e\n ; nil to nil\u208d_\u208e\n ; update-diff to update-diff\u208d_\u208e\n ; update-nil to update-nil\u208d_\u208e\n ; change-algebra to change-algebra\u208d_\u208e\n )\n\ninfixl 6 update\u2032 diff\u2032\n\nupdate\u2032 = Family.update\nsyntax update\u2032 x v dv = v \u229e\u208d x \u208e dv\n\ndiff\u2032 = Family.diff\nsyntax diff\u2032 x u v = u \u229f\u208d x \u208e v\n\n-- Abelian groups induce change algebras\n\nopen import Algebra.Structures\nopen import Data.Product\nopen import Function\n\nmodule GroupChanges\n {a} (A : Set a) {_\u2295_} {\u03b5} {_\u207b\u00b9}\n {{isAbelianGroup : IsAbelianGroup {A = A} _\u2261_ _\u2295_ \u03b5 _\u207b\u00b9}}\n where\n open IsAbelianGroup isAbelianGroup\n hiding\n ( refl\n )\n renaming\n ( _-_ to _\u229d_\n ; \u2219-cong to _\u27e8\u2295\u27e9_\n )\n\n open \u2261-Reasoning\n\n changeAlgebra : ChangeAlgebra a A\n changeAlgebra = record\n { Change = const A\n ; update = _\u2295_\n ; diff = _\u229d_\n ; isChangeAlgebra = record\n { update-diff = \u03bb u v \u2192\n begin\n v \u2295 (u \u229d v)\n \u2261\u27e8 comm _ _ \u27e9\n (u \u229d v ) \u2295 v\n \u2261\u27e8\u27e9\n (u \u2295 (v \u207b\u00b9)) \u2295 v\n \u2261\u27e8 assoc _ _ _ \u27e9\n u \u2295 ((v \u207b\u00b9) \u2295 v)\n \u2261\u27e8 refl \u27e8\u2295\u27e9 proj\u2081 inverse v \u27e9\n u \u2295 \u03b5\n \u2261\u27e8 proj\u2082 identity u \u27e9\n u\n \u220e\n }\n }\n","old_contents":"module Base.Change.Algebra where\n\nopen import Relation.Binary.PropositionalEquality\nopen import Level\n\n-- Change Algebras\n\nrecord IsChangeAlgebra\n {c} {d}\n {Carrier : Set c}\n (Change : Carrier \u2192 Set d)\n (update : (v : Carrier) (dv : Change v) \u2192 Carrier)\n (diff : (u v : Carrier) \u2192 Change v) : Set (c \u2294 d) where\n field\n update-diff : \u2200 u v \u2192 update v (diff u v) \u2261 u\n\n nil : \u2200 v \u2192 Change v\n nil v = diff v v\n\n update-nil : \u2200 v \u2192 update v (nil v) \u2261 v\n update-nil v = update-diff v v\n\nrecord ChangeAlgebra {c} \u2113\n (Carrier : Set c) : Set (c \u2294 suc \u2113) where\n field\n Change : Carrier \u2192 Set \u2113\n update : (v : Carrier) (dv : Change v) \u2192 Carrier\n diff : (u v : Carrier) \u2192 Change v\n\n isChangeAlgebra : IsChangeAlgebra Change update diff\n\n\n infixl 6 update diff\n\n open IsChangeAlgebra isChangeAlgebra public\n\nopen ChangeAlgebra {{...}} public\n using\n ( update-diff\n ; update-nil\n ; nil\n )\n renaming\n ( Change to \u0394\n ; update to _\u229e_\n ; diff to _\u229f_\n )\n\n-- Families of change algebras\n\nrecord ChangeAlgebraFamily {a p} \u2113 {A : Set a} (P : A \u2192 Set p): Set (suc \u2113 \u2294 p \u2294 a) where\n constructor\n family\n field\n change-algebra : \u2200 x \u2192 ChangeAlgebra \u2113 (P x)\n\n module _ x where\n open ChangeAlgebra (change-algebra x) public\n\nmodule Family = ChangeAlgebraFamily {{...}}\n\nopen Family public\n using\n (\n )\n renaming\n ( Change to \u0394\u208d_\u208e\n ; nil to nil\u208d_\u208e\n ; update-diff to update-diff\u208d_\u208e\n ; update-nil to update-nil\u208d_\u208e\n ; change-algebra to change-algebra\u208d_\u208e\n )\n\ninfixl 6 update\u2032 diff\u2032\n\nupdate\u2032 = Family.update\nsyntax update\u2032 x v dv = v \u229e\u208d x \u208e dv\n\ndiff\u2032 = Family.diff\nsyntax diff\u2032 x u v = u \u229f\u208d x \u208e v\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"1d9c622bcff742ac6217fb698eb8efe929eea1bd","subject":"At least define Var in OOO semantics","message":"At least define Var in OOO semantics\n","repos":"spire\/spire","old_file":"formalization\/agda\/Spire\/OperationalTypeOperationalTermOperationalValue.agda","new_file":"formalization\/agda\/Spire\/OperationalTypeOperationalTermOperationalValue.agda","new_contents":"module Spire.OperationalTypeOperationalTermOperationalValue where\n\n----------------------------------------------------------------------\n\ndata Level : Set where\n zero : Level\n suc : Level \u2192 Level\n\n----------------------------------------------------------------------\n\ndata Context : Set\ndata Type (\u0393 : Context) : Set\ndata Value (\u0393 : Context) : Type \u0393 \u2192 Set\ndata Neutral (\u0393 : Context) : Type \u0393 \u2192 Set\n\n----------------------------------------------------------------------\n\ndata Context where\n \u2205 : Context\n _,_ : (\u0393 : Context) \u2192 Type \u0393 \u2192 Context\n\ndata Type \u0393 where\n `\u22a5 `\u22a4 `Bool : Type \u0393\n `Type : (\u2113 : Level) \u2192 Type \u0393\n `\u03a0 `\u03a3 : (A : Type \u0393) (B : Type (\u0393 , A)) \u2192 Type \u0393\n `\u27e6_\u27e7 : \u2200{\u2113} \u2192 Neutral \u0393 (`Type \u2113) \u2192 Type \u0393\n\n----------------------------------------------------------------------\n\n\u27e6_\u27e7 : \u2200{\u0393 \u2113} \u2192 Value \u0393 (`Type \u2113) \u2192 Type \u0393\n\npostulate\n wknT : \u2200{\u0393 A} \u2192 Type \u0393 \u2192 Type (\u0393 , A)\n subT : \u2200{\u0393 A} \u2192 Type (\u0393 , A) \u2192 Value \u0393 A \u2192 Type \u0393\n subV : \u2200{\u0393 A B} \u2192 Value (\u0393 , A) B \u2192 (x : Value \u0393 A) \u2192 Value \u0393 (subT B x)\n\ndata Var : (\u0393 : Context) (A : Type \u0393) \u2192 Set where\n here : \u2200{\u0393 A} \u2192 Var (\u0393 , A) (wknT A)\n there : \u2200{\u0393 A B} \u2192 Var \u0393 A \u2192 Var (\u0393 , B) (wknT A)\n\n----------------------------------------------------------------------\n\ndata Value \u0393 where\n {- Type introduction -}\n `\u22a5 `\u22a4 `Bool `Type : \u2200{\u2113} \u2192 Value \u0393 (`Type \u2113)\n `\u03a0 `\u03a3 : \u2200{\u2113} (A : Value \u0393 (`Type \u2113)) (B : Value (\u0393 , \u27e6 A \u27e7) (`Type \u2113)) \u2192 Value \u0393 (`Type \u2113)\n `\u27e6_\u27e7 : \u2200{\u2113} \u2192 Value \u0393 (`Type \u2113) \u2192 Value \u0393 (`Type (suc \u2113))\n\n {- Value introduction -}\n `tt : Value \u0393 `\u22a4\n `true `false : Value \u0393 `Bool\n _`,_ : \u2200{A B} (a : Value \u0393 A) (b : Value \u0393 (subT B a)) \u2192 Value \u0393 (`\u03a3 A B)\n `\u03bb : \u2200{A B} \u2192 Value (\u0393 , A) B \u2192 Value \u0393 (`\u03a0 A B)\n `neut : \u2200{A} \u2192 Neutral \u0393 A \u2192 Value \u0393 A\n\n----------------------------------------------------------------------\n\ndata Neutral \u0393 where\n {- Value elimination -}\n `var : \u2200{A} \u2192 Var \u0393 A \u2192 Neutral \u0393 A\n `if_`then_`else_ : \u2200{C} (b : Neutral \u0393 `Bool) (c\u2081 c\u2082 : Value \u0393 C) \u2192 Neutral \u0393 C\n `proj\u2081 : \u2200{A B} \u2192 Neutral \u0393 (`\u03a3 A B) \u2192 Neutral \u0393 A\n `proj\u2082 : \u2200{A B} (ab : Neutral \u0393 (`\u03a3 A B)) \u2192 Neutral \u0393 (subT B (`neut (`proj\u2081 ab)))\n _`$_ : \u2200{A B} (f : Neutral \u0393 (`\u03a0 A B)) (a : Value \u0393 A) \u2192 Neutral \u0393 (subT B a)\n\n----------------------------------------------------------------------\n\n\u27e6 `\u03a0 A B \u27e7 = `\u03a0 \u27e6 A \u27e7 \u27e6 B \u27e7\n\u27e6 `\u03a3 A B \u27e7 = `\u03a3 \u27e6 A \u27e7 \u27e6 B \u27e7\n\u27e6 `\u22a5 \u27e7 = `\u22a5\n\u27e6 `\u22a4 \u27e7 = `\u22a4\n\u27e6 `Bool \u27e7 = `Bool\n\u27e6 `Type {zero} \u27e7 = `\u22a5\n\u27e6 `Type {suc \u2113} \u27e7 = `Type \u2113\n\u27e6 `\u27e6 A \u27e7 \u27e7 = \u27e6 A \u27e7\n\u27e6 `neut A \u27e7 = `\u27e6 A \u27e7\n\n----------------------------------------------------------------------\n\nif_then_else_ : \u2200{\u0393 C} (b : Value \u0393 `Bool) (c\u2081 c\u2082 : Value \u0393 C) \u2192 Value \u0393 C\nif `true then c\u2081 else c\u2082 = c\u2081\nif `false then c\u2081 else c\u2082 = c\u2082\nif `neut b then c\u2081 else c\u2082 = `neut (`if b `then c\u2081 `else c\u2082)\n\n----------------------------------------------------------------------\n\nproj\u2081 : \u2200{\u0393 A B} \u2192 Value \u0393 (`\u03a3 A B) \u2192 Value \u0393 A\nproj\u2081 (a `, b) = a\nproj\u2081 (`neut ab) = `neut (`proj\u2081 ab)\n\nproj\u2082 : \u2200{\u0393 A B} (ab : Value \u0393 (`\u03a3 A B)) \u2192 Value \u0393 (subT B (proj\u2081 ab))\nproj\u2082 (a `, b) = b\nproj\u2082 (`neut ab) = `neut (`proj\u2082 ab)\n\n----------------------------------------------------------------------\n\n_$_ : \u2200{\u0393 A B} \u2192 Value \u0393 (`\u03a0 A B) \u2192 (a : Value \u0393 A) \u2192 Value \u0393 (subT B a)\n`\u03bb b $ a = subV b a\n`neut f $ a = `neut (f `$ a)\n\n----------------------------------------------------------------------\n\ndata Term (\u0393 : Context) : Type \u0393 \u2192 Set\neval : \u2200{\u0393 A} \u2192 Term \u0393 A \u2192 Value \u0393 A\n\ndata Term \u0393 where\n {- Type introduction -}\n `\u22a5 `\u22a4 `Bool `Type : \u2200{\u2113} \u2192 Term \u0393 (`Type \u2113)\n `\u03a0 `\u03a3 : \u2200{\u2113} (A : Term \u0393 (`Type \u2113)) (B : Term (\u0393 , \u27e6 eval A \u27e7) (`Type \u2113)) \u2192 Term \u0393 (`Type \u2113)\n `\u27e6_\u27e7 : \u2200{\u2113} \u2192 Term \u0393 (`Type \u2113) \u2192 Term \u0393 (`Type (suc \u2113))\n\n {- Value introduction -}\n `tt : Term \u0393 `\u22a4\n `true `false : Term \u0393 `Bool\n _`,_ : \u2200{A B}\n (a : Term \u0393 A) (b : Term \u0393 (subT B (eval a)))\n \u2192 Term \u0393 (`\u03a3 A B)\n \n {- Value elimination -}\n `var : \u2200{A} \u2192 Var \u0393 A \u2192 Term \u0393 A\n `if_`then_`else_ : \u2200{C}\n (b : Term \u0393 `Bool)\n (c\u2081 c\u2082 : Term \u0393 C)\n \u2192 Term \u0393 C\n _`$_ : \u2200{A B} (f : Term \u0393 (`\u03a0 A B)) (a : Term \u0393 A) \u2192 Term \u0393 (subT B (eval a))\n `proj\u2081 : \u2200{A B} \u2192 Term \u0393 (`\u03a3 A B) \u2192 Term \u0393 A\n `proj\u2082 : \u2200{A B} (ab : Term \u0393 (`\u03a3 A B)) \u2192 Term \u0393 (subT B (proj\u2081 (eval ab)))\n\n----------------------------------------------------------------------\n\n{- Type introduction -}\neval `\u22a5 = `\u22a5\neval `\u22a4 = `\u22a4\neval `Bool = `Bool\neval `Type = `Type\neval (`\u03a0 A B) = `\u03a0 (eval A) (eval B)\neval (`\u03a3 A B) = `\u03a3 (eval A) (eval B)\neval `\u27e6 A \u27e7 = `\u27e6 eval A \u27e7\n\n{- Value introduction -}\neval `tt = `tt\neval `true = `true\neval `false = `false\neval (a `, b) = eval a `, eval b\n\n{- Value elimination -}\neval (`var i) = `neut (`var i)\neval (`if b `then c\u2081 `else c\u2082) = if eval b then eval c\u2081 else eval c\u2082\neval (f `$ a) = eval f $ eval a\neval (`proj\u2081 ab) = proj\u2081 (eval ab)\neval (`proj\u2082 ab) = proj\u2082 (eval ab)\n\n----------------------------------------------------------------------\n\n","old_contents":"module Spire.OperationalTypeOperationalTermOperationalValue where\n\n----------------------------------------------------------------------\n\ndata Level : Set where\n zero : Level\n suc : Level \u2192 Level\n\n----------------------------------------------------------------------\n\ndata Context : Set\ndata Type (\u0393 : Context) : Set\ndata Value (\u0393 : Context) : Type \u0393 \u2192 Set\ndata Neutral (\u0393 : Context) : Type \u0393 \u2192 Set\n\n----------------------------------------------------------------------\n\ndata Context where\n \u2205 : Context\n _,_ : (\u0393 : Context) \u2192 Type \u0393 \u2192 Context\n\ndata Type \u0393 where\n `\u22a5 `\u22a4 `Bool : Type \u0393\n `Type : (\u2113 : Level) \u2192 Type \u0393\n `\u03a0 `\u03a3 : (A : Type \u0393) (B : Type (\u0393 , A)) \u2192 Type \u0393\n `\u27e6_\u27e7 : \u2200{\u2113} \u2192 Neutral \u0393 (`Type \u2113) \u2192 Type \u0393\n\n----------------------------------------------------------------------\n\n\u27e6_\u27e7 : \u2200{\u0393 \u2113} \u2192 Value \u0393 (`Type \u2113) \u2192 Type \u0393\n\npostulate\n Var : (\u0393 : Context) (A : Type \u0393) \u2192 Set\n subT : \u2200{\u0393 A} \u2192 Type (\u0393 , A) \u2192 Value \u0393 A \u2192 Type \u0393\n subV : \u2200{\u0393 A B} \u2192 Value (\u0393 , A) B \u2192 (x : Value \u0393 A) \u2192 Value \u0393 (subT B x)\n\n----------------------------------------------------------------------\n\ndata Value \u0393 where\n {- Type introduction -}\n `\u22a5 `\u22a4 `Bool `Type : \u2200{\u2113} \u2192 Value \u0393 (`Type \u2113)\n `\u03a0 `\u03a3 : \u2200{\u2113} (A : Value \u0393 (`Type \u2113)) (B : Value (\u0393 , \u27e6 A \u27e7) (`Type \u2113)) \u2192 Value \u0393 (`Type \u2113)\n `\u27e6_\u27e7 : \u2200{\u2113} \u2192 Value \u0393 (`Type \u2113) \u2192 Value \u0393 (`Type (suc \u2113))\n\n {- Value introduction -}\n `tt : Value \u0393 `\u22a4\n `true `false : Value \u0393 `Bool\n _`,_ : \u2200{A B} (a : Value \u0393 A) (b : Value \u0393 (subT B a)) \u2192 Value \u0393 (`\u03a3 A B)\n `\u03bb : \u2200{A B} \u2192 Value (\u0393 , A) B \u2192 Value \u0393 (`\u03a0 A B)\n `neut : \u2200{A} \u2192 Neutral \u0393 A \u2192 Value \u0393 A\n\n----------------------------------------------------------------------\n\ndata Neutral \u0393 where\n {- Value elimination -}\n `var : \u2200{A} \u2192 Var \u0393 A \u2192 Neutral \u0393 A\n `if_`then_`else_ : \u2200{C} (b : Neutral \u0393 `Bool) (c\u2081 c\u2082 : Value \u0393 C) \u2192 Neutral \u0393 C\n `proj\u2081 : \u2200{A B} \u2192 Neutral \u0393 (`\u03a3 A B) \u2192 Neutral \u0393 A\n `proj\u2082 : \u2200{A B} (ab : Neutral \u0393 (`\u03a3 A B)) \u2192 Neutral \u0393 (subT B (`neut (`proj\u2081 ab)))\n _`$_ : \u2200{A B} (f : Neutral \u0393 (`\u03a0 A B)) (a : Value \u0393 A) \u2192 Neutral \u0393 (subT B a)\n\n----------------------------------------------------------------------\n\n\u27e6 `\u03a0 A B \u27e7 = `\u03a0 \u27e6 A \u27e7 \u27e6 B \u27e7\n\u27e6 `\u03a3 A B \u27e7 = `\u03a3 \u27e6 A \u27e7 \u27e6 B \u27e7\n\u27e6 `\u22a5 \u27e7 = `\u22a5\n\u27e6 `\u22a4 \u27e7 = `\u22a4\n\u27e6 `Bool \u27e7 = `Bool\n\u27e6 `Type {zero} \u27e7 = `\u22a5\n\u27e6 `Type {suc \u2113} \u27e7 = `Type \u2113\n\u27e6 `\u27e6 A \u27e7 \u27e7 = \u27e6 A \u27e7\n\u27e6 `neut A \u27e7 = `\u27e6 A \u27e7\n\n----------------------------------------------------------------------\n\nif_then_else_ : \u2200{\u0393 C} (b : Value \u0393 `Bool) (c\u2081 c\u2082 : Value \u0393 C) \u2192 Value \u0393 C\nif `true then c\u2081 else c\u2082 = c\u2081\nif `false then c\u2081 else c\u2082 = c\u2082\nif `neut b then c\u2081 else c\u2082 = `neut (`if b `then c\u2081 `else c\u2082)\n\n----------------------------------------------------------------------\n\nproj\u2081 : \u2200{\u0393 A B} \u2192 Value \u0393 (`\u03a3 A B) \u2192 Value \u0393 A\nproj\u2081 (a `, b) = a\nproj\u2081 (`neut ab) = `neut (`proj\u2081 ab)\n\nproj\u2082 : \u2200{\u0393 A B} (ab : Value \u0393 (`\u03a3 A B)) \u2192 Value \u0393 (subT B (proj\u2081 ab))\nproj\u2082 (a `, b) = b\nproj\u2082 (`neut ab) = `neut (`proj\u2082 ab)\n\n----------------------------------------------------------------------\n\n_$_ : \u2200{\u0393 A B} \u2192 Value \u0393 (`\u03a0 A B) \u2192 (a : Value \u0393 A) \u2192 Value \u0393 (subT B a)\n`\u03bb b $ a = subV b a\n`neut f $ a = `neut (f `$ a)\n\n----------------------------------------------------------------------\n\ndata Term (\u0393 : Context) : Type \u0393 \u2192 Set\neval : \u2200{\u0393 A} \u2192 Term \u0393 A \u2192 Value \u0393 A\n\ndata Term \u0393 where\n {- Type introduction -}\n `\u22a5 `\u22a4 `Bool `Type : \u2200{\u2113} \u2192 Term \u0393 (`Type \u2113)\n `\u03a0 `\u03a3 : \u2200{\u2113} (A : Term \u0393 (`Type \u2113)) (B : Term (\u0393 , \u27e6 eval A \u27e7) (`Type \u2113)) \u2192 Term \u0393 (`Type \u2113)\n `\u27e6_\u27e7 : \u2200{\u2113} \u2192 Term \u0393 (`Type \u2113) \u2192 Term \u0393 (`Type (suc \u2113))\n\n {- Value introduction -}\n `tt : Term \u0393 `\u22a4\n `true `false : Term \u0393 `Bool\n _`,_ : \u2200{A B}\n (a : Term \u0393 A) (b : Term \u0393 (subT B (eval a)))\n \u2192 Term \u0393 (`\u03a3 A B)\n \n {- Value elimination -}\n `var : \u2200{A} \u2192 Var \u0393 A \u2192 Term \u0393 A\n `if_`then_`else_ : \u2200{C}\n (b : Term \u0393 `Bool)\n (c\u2081 c\u2082 : Term \u0393 C)\n \u2192 Term \u0393 C\n _`$_ : \u2200{A B} (f : Term \u0393 (`\u03a0 A B)) (a : Term \u0393 A) \u2192 Term \u0393 (subT B (eval a))\n `proj\u2081 : \u2200{A B} \u2192 Term \u0393 (`\u03a3 A B) \u2192 Term \u0393 A\n `proj\u2082 : \u2200{A B} (ab : Term \u0393 (`\u03a3 A B)) \u2192 Term \u0393 (subT B (proj\u2081 (eval ab)))\n\n----------------------------------------------------------------------\n\n{- Type introduction -}\neval `\u22a5 = `\u22a5\neval `\u22a4 = `\u22a4\neval `Bool = `Bool\neval `Type = `Type\neval (`\u03a0 A B) = `\u03a0 (eval A) (eval B)\neval (`\u03a3 A B) = `\u03a3 (eval A) (eval B)\neval `\u27e6 A \u27e7 = `\u27e6 eval A \u27e7\n\n{- Value introduction -}\neval `tt = `tt\neval `true = `true\neval `false = `false\neval (a `, b) = eval a `, eval b\n\n{- Value elimination -}\neval (`var i) = `neut (`var i)\neval (`if b `then c\u2081 `else c\u2082) = if eval b then eval c\u2081 else eval c\u2082\neval (f `$ a) = eval f $ eval a\neval (`proj\u2081 ab) = proj\u2081 (eval ab)\neval (`proj\u2082 ab) = proj\u2082 (eval ab)\n\n----------------------------------------------------------------------\n\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"9523dacab73786f130dd16572f06f3e56e660206","subject":"Redid translation of tautology and future into System F-omega, since the previous version didn't support the comonadic structure of future.","message":"Redid translation of tautology and future into System F-omega, since the previous version didn't support the comonadic structure of future.\n","repos":"agda\/agda-frp-js,agda\/agda-frp-js","old_file":"src\/agda\/FRP\/JS\/Model.agda","new_file":"src\/agda\/FRP\/JS\/Model.agda","new_contents":"open import FRP.JS.Level using ( Level ; _\u2294_ ) renaming ( zero to o ; suc to \u2191 )\nopen import FRP.JS.Time using ( Time ; _\u2264_ ; _<_ )\nopen import FRP.JS.Bool using ( Bool ; true ; false ; not ; _\u225f_ ) \nopen import FRP.JS.True using ( True ; tt )\n\nmodule FRP.JS.Model where\n\n-- This model is essentially System F-omega with a kind time\n-- together with a type for the partial order on time,\n-- and expressions for reflexivity and transitivity.\n-- We prove parametricity, and then show that parametricity implies causality.\n\n-- Note that this is a \"deep\" notion of causality, not the \"shallow\"\n-- causality usually used in FRP. The pragmatic upshot of this is that\n-- there is only one time model: nested signals are in the same time\n-- model, not a simulated time model. This fits with the JS implementation,\n-- which uses wall clock time for all signals.\n\n-- Propositional equality\n\ndata _\u2261_ {\u03b1} {A : Set \u03b1} (a : A) : A \u2192 Set \u03b1 where\n refl : a \u2261 a\n\nsym : \u2200 {\u03b1} {A : Set \u03b1} {a b : A} \u2192 (a \u2261 b) \u2192 (b \u2261 a)\nsym refl = refl\n\ntrans : \u2200 {\u03b1} {A : Set \u03b1} {a b c : A} \u2192 (a \u2261 b) \u2192 (b \u2261 c) \u2192 (a \u2261 c)\ntrans refl refl = refl\n\ncong : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : Set \u03b2} (f : A \u2192 B) {a\u2081 a\u2082 : A} \u2192\n (a\u2081 \u2261 a\u2082) \u2192 (f a\u2081 \u2261 f a\u2082)\ncong f refl = refl\n\napply : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : Set \u03b2} {F G : A \u2192 B} \u2192 (F \u2261 G) \u2192 \n \u2200 {a b} \u2192 (a \u2261 b) \u2192 (F a \u2261 G b)\napply refl refl = refl\n\ncast : \u2200 {\u03b1} {A B : Set \u03b1} \u2192 (A \u2261 B) \u2192 A \u2192 B\ncast refl a = a\n\ncast\u00b2 : \u2200 {\u03b1} {A B : Set \u03b1} {\u211c \u2111 : A \u2192 B \u2192 Set \u03b1} \u2192 (\u211c \u2261 \u2111) \u2192 \u2200 {a b} \u2192 \u211c a b \u2192 \u2111 a b\ncast\u00b2 refl a\u211cb = a\u211cb\n\nirrel : \u2200 b \u2192 (b\u2081 b\u2082 : True b) \u2192 (b\u2081 \u2261 b\u2082)\nirrel true tt tt = refl\nirrel false () ()\n\n-- Postulates (including dependent extensionality)\n\npostulate\n \u2264-refl : \u2200 t \u2192 True (t \u2264 t)\n \u2264-trans : \u2200 t u v \u2192 True (t \u2264 u) \u2192 True (u \u2264 v) \u2192 True (t \u2264 v)\n dext : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : A \u2192 Set \u03b2} {F G : \u2200 a \u2192 B a} \u2192 (\u2200 a \u2192 F a \u2261 G a) \u2192 (F \u2261 G)\n\next : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : Set \u03b2} {F G : A \u2192 B} \u2192\n (\u2200 a \u2192 F a \u2261 G a) \u2192 (F \u2261 G)\next = dext\n\niext : \u2200 {\u03b1 \u03b2} {A : Set \u03b1} {B : A \u2192 Set \u03b2} {F G : \u2200 {a} \u2192 B a} \u2192 \n (\u2200 a \u2192 F {a} \u2261 G {a}) \u2192 ((\u03bb {a} \u2192 F {a}) \u2261 (\u03bb {a} \u2192 G {a}))\niext F\u2248G = cong (\u03bb X {a} \u2192 X a) (dext F\u2248G)\n\n-- Finite products\n\nrecord \u22a4 {\u03b1} : Set \u03b1 where\n constructor tt\n\nopen \u22a4 public\n\nrecord \u03a3 {\u03b1 \u03b2} (A : Set \u03b1) (B : A \u2192 Set \u03b2) : Set (\u03b1 \u2294 \u03b2) where\n constructor _,_\n field\n proj\u2081 : A\n proj\u2082 : B proj\u2081\n\nopen \u03a3 public\n\n_\u00d7_ : \u2200 {\u03b1 \u03b2} \u2192 Set \u03b1 \u2192 Set \u03b2 \u2192 Set (\u03b1 \u2294 \u03b2)\nA \u00d7 B = \u03a3 A (\u03bb a \u2192 B)\n\n_\u00d7\u00b2_ : \u2200 {\u03b1 \u03b2} {A C : Set \u03b1} {B D : Set \u03b2} \u2192 \n (A \u2192 C \u2192 Set \u03b1) \u2192 (B \u2192 D \u2192 Set \u03b2) \u2192 ((A \u00d7 B) \u2192 (C \u00d7 D) \u2192 Set (\u03b1 \u2294 \u03b2))\n(\u211c \u00d7\u00b2 \u2111) (a , b) (c , d) = (\u211c a c \u00d7 \u2111 b d)\n\n_\u2192\u00b2_ : \u2200 {\u03b1 \u03b2} {A C : Set \u03b1} {B D : Set \u03b2} \u2192 \n (A \u2192 C \u2192 Set \u03b1) \u2192 (B \u2192 D \u2192 Set \u03b2) \u2192 ((A \u2192 B) \u2192 (C \u2192 D) \u2192 Set (\u03b1 \u2294 \u03b2))\n(\u211c \u2192\u00b2 \u2111) f g = \u2200 {a b} \u2192 \u211c a b \u2192 \u2111 (f a) (g b)\n\n-- Case on booleans\n\ndata Case (c : Bool) : Set where\n _,_ : \u2200 b \u2192 True (b \u225f c) \u2192 Case c\n\nswitch : \u2200 b \u2192 Case b\nswitch true = (true , tt)\nswitch false = (false , tt)\n\n-- Reactive sets\n\nRSet : \u2200 \u03b1 \u2192 Set (\u2191 \u03b1)\nRSet \u03b1 = Time \u2192 Set \u03b1\n\n-- Equalitional reasoning\n\ninfix 4 _IsRelatedTo_\ninfix 2 _\u220e\ninfixr 2 _\u2261\u27e8_\u27e9_\ninfix 1 begin_\n\ndata _IsRelatedTo_ {\u03b1} {A : Set \u03b1} (a b : A) : Set \u03b1 where\n relTo : (a\u2261b : a \u2261 b) \u2192 a IsRelatedTo b\n\nbegin_ : \u2200 {\u03b1} {A : Set \u03b1} {a b : A} \u2192 a IsRelatedTo b \u2192 a \u2261 b\nbegin relTo a\u2261b = a\u2261b\n\n_\u2261\u27e8_\u27e9_ : \u2200 {\u03b1} {A : Set \u03b1} a {b c : A} \u2192 a \u2261 b \u2192 b IsRelatedTo c \u2192 a IsRelatedTo c\n_ \u2261\u27e8 a\u2261b \u27e9 relTo b\u2261c = relTo (trans a\u2261b b\u2261c)\n\n_\u220e : \u2200 {\u03b1} {A : Set \u03b1} (a : A) \u2192 a IsRelatedTo a\n_\u220e _ = relTo refl\n\n-- Kinds\n\ndata Kind : Set where\n time : Kind\n set : Level \u2192 Kind\n _\u21d2_ : Kind \u2192 Kind \u2192 Kind\n\nlevel : Kind \u2192 Level\nlevel time = o\nlevel (set \u03b1) = \u2191 \u03b1\nlevel (K \u21d2 L) = level K \u2294 level L\n\nK\u27e6_\u27e7 : \u2200 K \u2192 Set (level K)\nK\u27e6 time \u27e7 = Time\nK\u27e6 set \u03b1 \u27e7 = Set \u03b1\nK\u27e6 K \u21d2 L \u27e7 = K\u27e6 K \u27e7 \u2192 K\u27e6 L \u27e7\n\n_\u220b_\u2194_ : \u2200 K \u2192 K\u27e6 K \u27e7 \u2192 K\u27e6 K \u27e7 \u2192 Set (level K)\ntime \u220b t \u2194 u = (t \u2261 u)\nset \u03b1 \u220b A \u2194 B = A \u2192 B \u2192 Set \u03b1\n(K \u21d2 L) \u220b F \u2194 G = \u2200 {A B} \u2192 (K \u220b A \u2194 B) \u2192 (L \u220b F A \u2194 G B)\n\n-- \u2261 can be used as a structural equivalence on relations.\n\nstruct : \u2200 K {A B C D} \u2192 (A \u2261 B) \u2192 (K \u220b B \u2194 D) \u2192 (C \u2261 D) \u2192 (K \u220b A \u2194 C)\nstruct K refl \u211c refl = \u211c\n\nstruct-ext : \u2200 K L {A B} {F G H I : K\u27e6 K \u21d2 L \u27e7} \n (F\u2248G : \u2200 A \u2192 F A \u2261 G A) (\u211c : (K \u21d2 L) \u220b G \u2194 I) (H\u2248I : \u2200 B \u2192 H B \u2261 I B) (\u2111 : K \u220b A \u2194 B) \u2192\n struct L (F\u2248G A) (\u211c \u2111) (H\u2248I B) \u2261 struct (K \u21d2 L) (ext F\u2248G) \u211c (ext H\u2248I) \u2111\nstruct-ext K L {A} {B} F\u2248G \u211c H\u2248I \u2111 \n with ext F\u2248G | ext H\u2248I | F\u2248G A | H\u2248I B\n... | refl | refl | refl | refl = refl\n\nstruct-apply : \u2200 K L {F G H I A B C D} \u2192 \n (F\u2261G : F \u2261 G) (\u211c : (K \u21d2 L) \u220b G \u2194 I) (H\u2261I : H \u2261 I) \u2192 \n (A\u2261B : A \u2261 B) (\u2111 : K \u220b B \u2194 D) (C\u2261D : C \u2261 D) \u2192 \n struct (K \u21d2 L) F\u2261G \u211c H\u2261I (struct K A\u2261B \u2111 C\u2261D)\n \u2261 struct L (apply F\u2261G A\u2261B) (\u211c \u2111) (apply H\u2261I C\u2261D)\nstruct-apply K L refl \u211c refl refl \u2111 refl = refl\n\nstruct-cast : \u2200 {\u03b1 A B C D} (\u211c : set \u03b1 \u220b B \u2194 D) (A\u2261B : A \u2261 B) (C\u2261D : C \u2261 D) {a c} \u2192\n struct (set \u03b1) A\u2261B \u211c C\u2261D a c \u2192 \u211c (cast A\u2261B a) (cast C\u2261D c)\nstruct-cast \u211c refl refl a\u211cc = a\u211cc\n\nstruct-sym : \u2200 K {A B C D \u2111 \u211c} \u2192 (A\u2261B : A \u2261 B) \u2192 (C\u2261D : C \u2261 D) \u2192\n (\u2111 \u2261 struct K A\u2261B \u211c C\u2261D) \u2192 \n (\u211c \u2261 struct K (sym A\u2261B) \u2111 (sym C\u2261D))\nstruct-sym K refl refl refl = refl\n\nstruct-trans : \u2200 K {A B C D E F}\n (A\u2261B : A \u2261 B) (B\u2261C : B \u2261 C) (\u211c : K \u220b C \u2194 F) (E\u2261F : E \u2261 F) (D\u2261E : D \u2261 E) \u2192\n struct K A\u2261B (struct K B\u2261C \u211c E\u2261F) D\u2261E \u2261\n struct K (trans A\u2261B B\u2261C) \u211c (trans D\u2261E E\u2261F)\nstruct-trans K refl refl \u211c refl refl = refl\n\n-- Type contexts\n\ninfixr 4 _\u2237_\n\ndata Kinds : Set where\n [] : Kinds\n _\u2237_ : Kind \u2192 Kinds \u2192 Kinds\n\nlevels : Kinds \u2192 Level\nlevels [] = o\nlevels (K \u2237 \u03a3) = level K \u2294 levels \u03a3\n\n\u03a3\u27e6_\u27e7 : \u2200 \u03a3 \u2192 Set (levels \u03a3)\n\u03a3\u27e6 [] \u27e7 = \u22a4\n\u03a3\u27e6 K \u2237 \u03a3 \u27e7 = K\u27e6 K \u27e7 \u00d7 \u03a3\u27e6 \u03a3 \u27e7\n\n_\u220b_\u2194*_ : \u2200 \u03a3 \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 Set (levels \u03a3)\n[] \u220b tt \u2194* tt = \u22a4\n(K \u2237 \u03a3) \u220b (A , As) \u2194* (B , Bs) = (K \u220b A \u2194 B) \u00d7 (\u03a3 \u220b As \u2194* Bs)\n\n-- Inclusion order on type contexts.\n-- Credited by Randy Pollack to Geuvers and Nederhof, JAR 1991.\n-- http:\/\/thread.gmane.org\/gmane.comp.lang.agda\/3259\/focus=3267\n\ndata _\u2291_ : Kinds \u2192 Kinds \u2192 Set where\n id : \u2200 {\u03a3} \u2192 \u03a3 \u2291 \u03a3\n keep : \u2200 K {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 ((K \u2237 \u03a3) \u2291 (K \u2237 \u03a5))\n skip : \u2200 K {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 (\u03a3 \u2291 (K \u2237 \u03a5))\n\n\u2291\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 \u03a3\u27e6 \u03a5 \u27e7 \u2192 \u03a3\u27e6 \u03a3 \u27e7\n\u2291\u27e6 id \u27e7 As = As\n\u2291\u27e6 keep K \u03a3\u2291\u03a5 \u27e7 (A , As) = (A , \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As)\n\u2291\u27e6 skip K \u03a3\u2291\u03a5 \u27e7 (A , As) = \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As\n\n\u2291\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5} \u2192 (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) \u2192 \u2200 {As Bs} \u2192 (\u03a5 \u220b As \u2194* Bs) \u2192 (\u03a3 \u220b \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As \u2194* \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 Bs)\n\u2291\u27e6 id \u27e7\u00b2 \u211cs = \u211cs\n\u2291\u27e6 keep K \u03a3\u2291\u03a5 \u27e7\u00b2 (\u211c , \u211cs) = (\u211c , \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)\n\u2291\u27e6 skip K \u03a3\u2291\u03a5 \u27e7\u00b2 (\u211c , \u211cs) = \u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs\n\n-- Concatenation of type contexts\n\n_++_ : Kinds \u2192 Kinds \u2192 Kinds\n[] ++ \u03a5 = \u03a5\n(K \u2237 \u03a3) ++ \u03a5 = K \u2237 (\u03a3 ++ \u03a5)\n\n_\u220b_++_\u220b_ : \u2200 \u03a3 \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 \u2200 \u03a5 \u2192 \u03a3\u27e6 \u03a5 \u27e7 \u2192 \u03a3\u27e6 \u03a3 ++ \u03a5 \u27e7\n[] \u220b tt ++ \u03a5 \u220b Bs = Bs\n(K \u2237 \u03a3) \u220b (A , As) ++ \u03a5 \u220b Bs = (A , (\u03a3 \u220b As ++ \u03a5 \u220b Bs))\n\n_\u220b_++\u00b2_\u220b_ : \u2200 \u03a3 {As Bs} \u2192 (\u03a3 \u220b As \u2194* Bs) \u2192 \u2200 \u03a5 {Cs Ds} \u2192 (\u03a5 \u220b Cs \u2194* Ds) \u2192 \n ((\u03a3 ++ \u03a5) \u220b (\u03a3 \u220b As ++ \u03a5 \u220b Cs) \u2194* (\u03a3 \u220b Bs ++ \u03a5 \u220b Ds))\n[] \u220b tt ++\u00b2 \u03a5 \u220b \u2111s = \u2111s\n(K \u2237 \u03a3) \u220b (\u211c , \u211cs) ++\u00b2 \u03a5 \u220b \u2111s = (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n\n-- Type variables\n\ndata TVar (K : Kind) : Kinds \u2192 Set where\n zero : \u2200 {\u03a3} \u2192 TVar K (K \u2237 \u03a3)\n suc : \u2200 {L \u03a3} \u2192 TVar K \u03a3 \u2192 TVar K (L \u2237 \u03a3)\n\n\u03c4\u27e6_\u27e7 : \u2200 {\u03a3 K} (\u03c4 : TVar K \u03a3) \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 K\u27e6 K \u27e7\n\u03c4\u27e6 zero \u27e7 (A , As) = A\n\u03c4\u27e6 suc \u03c4 \u27e7 (A , As) = \u03c4\u27e6 \u03c4 \u27e7 As\n\n\u03c4\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 K} (\u03c4 : TVar K \u03a3) {As Bs} \u2192 (\u03a3 \u220b As \u2194* Bs) \u2192 (K \u220b \u03c4\u27e6 \u03c4 \u27e7 As \u2194 \u03c4\u27e6 \u03c4 \u27e7 Bs)\n\u03c4\u27e6 zero \u27e7\u00b2 (\u211c , \u211cs) = \u211c\n\u03c4\u27e6 suc \u03c4 \u27e7\u00b2 (\u211c , \u211cs) = \u03c4\u27e6 \u03c4 \u27e7\u00b2 \u211cs\n\n-- Type constants\n\ndata TConst : Kind \u2192 Set where\n prod fun : \u2200 {\u03b1 \u03b2} \u2192 TConst (set \u03b1 \u21d2 (set \u03b2 \u21d2 set (\u03b1 \u2294 \u03b2)))\n leq lt : TConst (time \u21d2 (time \u21d2 set o))\n univ : \u2200 K {\u03b1} \u2192 TConst ((K \u21d2 set \u03b1) \u21d2 set (level K \u2294 \u03b1))\n\nC\u27e6_\u27e7 : \u2200 {K} \u2192 (TConst K) \u2192 K\u27e6 K \u27e7\nC\u27e6 prod \u27e7 = \u03bb A B \u2192 (A \u00d7 B)\nC\u27e6 fun \u27e7 = \u03bb A B \u2192 (A \u2192 B)\nC\u27e6 leq \u27e7 = \u03bb t u \u2192 True (t \u2264 u)\nC\u27e6 lt \u27e7 = \u03bb t u \u2192 True (t < u)\nC\u27e6 univ K \u27e7 = \u03bb F \u2192 \u2200 A \u2192 F A\n\nC\u27e6_\u27e7\u00b2 : \u2200 {K} (C : TConst K) \u2192 (K \u220b C\u27e6 C \u27e7 \u2194 C\u27e6 C \u27e7)\nC\u27e6 prod \u27e7\u00b2 = \u03bb \u211c \u2111 \u2192 (\u211c \u00d7\u00b2 \u2111)\nC\u27e6 fun \u27e7\u00b2 = \u03bb \u211c \u2111 \u2192 (\u211c \u2192\u00b2 \u2111)\nC\u27e6 leq \u27e7\u00b2 = \u03bb _ _ _ _ \u2192 \u22a4\nC\u27e6 lt \u27e7\u00b2 = \u03bb _ _ _ _ \u2192 \u22a4\nC\u27e6 univ K \u27e7\u00b2 = \u03bb \u211c f g \u2192 \u2200 {a b} \u2111 \u2192 \u211c \u2111 (f a) (g b)\n\n-- Types\n\ndata Typ (\u03a3 : Kinds) : Kind \u2192 Set where\n const : \u2200 {K} \u2192 TConst K \u2192 Typ \u03a3 K\n abs : \u2200 K {L} \u2192 Typ (K \u2237 \u03a3) L \u2192 Typ \u03a3 (K \u21d2 L)\n app : \u2200 {K L} \u2192 Typ \u03a3 (K \u21d2 L) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L\n var : \u2200 {K} \u2192 TVar K \u03a3 \u2192 Typ \u03a3 K\n\ntlevel : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (set \u03b1) \u2192 Level\ntlevel {\u03a3} {\u03b1} T = \u03b1\n\nT\u27e6_\u27e7 : \u2200 {\u03a3 K} (T : Typ \u03a3 K) \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 K\u27e6 K \u27e7\nT\u27e6 const C \u27e7 As = C\u27e6 C \u27e7\nT\u27e6 abs K T \u27e7 As = \u03bb A \u2192 T\u27e6 T \u27e7 (A , As)\nT\u27e6 app T U \u27e7 As = T\u27e6 T \u27e7 As (T\u27e6 U \u27e7 As)\nT\u27e6 var \u03c4 \u27e7 As = \u03c4\u27e6 \u03c4 \u27e7 As\n\nT\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 K} (T : Typ \u03a3 K) {As Bs} \u2192 (\u03a3 \u220b As \u2194* Bs) \u2192 (K \u220b T\u27e6 T \u27e7 As \u2194 T\u27e6 T \u27e7 Bs)\nT\u27e6 const C \u27e7\u00b2 \u211cs = C\u27e6 C \u27e7\u00b2\nT\u27e6 abs K T \u27e7\u00b2 \u211cs = \u03bb \u211c \u2192 T\u27e6 T \u27e7\u00b2 (\u211c , \u211cs)\nT\u27e6 app T U \u27e7\u00b2 \u211cs = T\u27e6 T \u27e7\u00b2 \u211cs (T\u27e6 U \u27e7\u00b2 \u211cs)\nT\u27e6 var \u03c4 \u27e7\u00b2 \u211cs = \u03c4\u27e6 \u03c4 \u27e7\u00b2 \u211cs\n\n-- Type shorthands\n\napp\u2082 : \u2200 {\u03a3 K L M} \u2192 Typ \u03a3 (K \u21d2 (L \u21d2 M)) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L \u2192 Typ \u03a3 M\napp\u2082 T U V = app (app T U) V\n\ncapp : \u2200 {\u03a3 K L} \u2192 TConst (K \u21d2 L) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L\ncapp C = app (const C)\n\ncapp\u2082 : \u2200 {\u03a3 K L M} \u2192 TConst (K \u21d2 (L \u21d2 M)) \u2192 Typ \u03a3 K \u2192 Typ \u03a3 L \u2192 Typ \u03a3 M\ncapp\u2082 C = app\u2082 (const C)\n\n_\u2297_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (set \u03b1) \u2192 Typ \u03a3 (set \u03b2) \u2192 Typ \u03a3 (set (\u03b1 \u2294 \u03b2))\n_\u2297_ = capp\u2082 prod\n\n_\u22b8_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (set \u03b1) \u2192 Typ \u03a3 (set \u03b2) \u2192 Typ \u03a3 (set (\u03b1 \u2294 \u03b2))\n_\u22b8_ = capp\u2082 fun\n\n_\u227c_ : \u2200 {\u03a3} \u2192 Typ \u03a3 time \u2192 Typ \u03a3 time \u2192 Typ \u03a3 (set o)\n_\u227c_ = capp\u2082 leq\n\n_\u227a_ : \u2200 {\u03a3} \u2192 Typ \u03a3 time \u2192 Typ \u03a3 time \u2192 Typ \u03a3 (set o)\n_\u227a_ = capp\u2082 lt\n\n\u03a0 : \u2200 {\u03a3 \u03b1} K \u2192 Typ (K \u2237 \u03a3) (set \u03b1) \u2192 Typ \u03a3 (set (level K \u2294 \u03b1))\n\u03a0 K T = capp (univ K) (abs K T)\n\ntvar\u2080 : \u2200 {\u03a3 K} \u2192 Typ (K \u2237 \u03a3) K\ntvar\u2080 = var zero\n\ntvar\u2081 : \u2200 {\u03a3 K L} \u2192 Typ (L \u2237 K \u2237 \u03a3) K\ntvar\u2081 = var (suc zero)\n\ntvar\u2082 : \u2200 {\u03a3 K L M} \u2192 Typ (M \u2237 L \u2237 K \u2237 \u03a3) K\ntvar\u2082 = var (suc (suc zero))\n\ntvar\u2083 : \u2200 {\u03a3 K L M N} \u2192 Typ (N \u2237 M \u2237 L \u2237 K \u2237 \u03a3) K\ntvar\u2083 = var (suc (suc (suc zero)))\n\nrset : Level \u2192 Kind\nrset \u03b1 = time \u21d2 set \u03b1\n\nrset\u2080 : Kind\nrset\u2080 = rset o\n\nprod\u02b3 : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 (rset \u03b2 \u21d2 rset (\u03b1 \u2294 \u03b2)))\nprod\u02b3 {\u03a3} {\u03b1} {\u03b2} = abs (rset \u03b1) (abs (rset \u03b2) (abs time (app tvar\u2082 tvar\u2080 \u2297 app tvar\u2081 tvar\u2080)))\n\n_\u2297\u02b3_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1) \u2192 Typ \u03a3 (rset \u03b2) \u2192 Typ \u03a3 (rset (\u03b1 \u2294 \u03b2))\n_\u2297\u02b3_ = app\u2082 prod\u02b3\n\nfun\u02b3 : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 (rset \u03b2 \u21d2 rset (\u03b1 \u2294 \u03b2)))\nfun\u02b3 {\u03a3} {\u03b1} {\u03b2} = abs (rset \u03b1) (abs (rset \u03b2) (abs time (app tvar\u2082 tvar\u2080 \u22b8 app tvar\u2081 tvar\u2080)))\n\n_\u22b8\u02b3_ : \u2200 {\u03a3 \u03b1 \u03b2} \u2192 Typ \u03a3 (rset \u03b1) \u2192 Typ \u03a3 (rset \u03b2) \u2192 Typ \u03a3 (rset (\u03b1 \u2294 \u03b2))\n_\u22b8\u02b3_ = app\u2082 fun\u02b3\n\nalways : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (set \u03b1 \u21d2 rset \u03b1)\nalways {\u03a3} {\u03b1} = abs (set \u03b1) (abs time tvar\u2081)\n\n\u00bdinterval : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (rset \u03b1 \u21d2 (time \u21d2 (time \u21d2 set \u03b1)))\n\u00bdinterval {\u03a3} {\u03b1} = abs (rset \u03b1) (abs time (abs time (\u03a0 time \n ((tvar\u2082 \u227a tvar\u2080) \u22b8 ((tvar\u2080 \u227c tvar\u2081) \u22b8 app tvar\u2083 tvar\u2080)))))\n\n_\u27e8_,_] : \u2200 {\u03a3 \u03b1} \u2192 Typ \u03a3 (rset \u03b1) \u2192 Typ \u03a3 time \u2192 Typ \u03a3 time \u2192 Typ \u03a3 (set \u03b1)\nT \u27e8 t , u ] = app (app (app \u00bdinterval T) t) u\n\n-- Contexts\n\ndata Typs (\u03a3 : Kinds) : Set where\n [] : Typs \u03a3\n _\u2237_ : \u2200 {\u03b1} \u2192 (Typ \u03a3 (set \u03b1)) \u2192 Typs \u03a3 \u2192 Typs \u03a3\n\ntlevels : \u2200 {\u03a3} \u2192 Typs \u03a3 \u2192 Level\ntlevels [] = o\ntlevels (T \u2237 \u0393) = tlevel T \u2294 tlevels \u0393\n\n\u0393\u27e6_\u27e7 : \u2200 {\u03a3} (\u0393 : Typs \u03a3) \u2192 \u03a3\u27e6 \u03a3 \u27e7 \u2192 Set (tlevels \u0393)\n\u0393\u27e6 [] \u27e7 As = \u22a4\n\u0393\u27e6 T \u2237 \u0393 \u27e7 As = T\u27e6 T \u27e7 As \u00d7 \u0393\u27e6 \u0393 \u27e7 As\n\n\u0393\u27e6_\u27e7\u00b2 : \u2200 {\u03a3} (\u0393 : Typs \u03a3) {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 (\u0393\u27e6 \u0393 \u27e7 As \u2192 \u0393\u27e6 \u0393 \u27e7 Bs \u2192 Set (tlevels \u0393))\n\u0393\u27e6 [] \u27e7\u00b2 \u211cs tt tt = \u22a4\n\u0393\u27e6 T \u2237 \u0393 \u27e7\u00b2 \u211cs (a , as) (b , bs) = T\u27e6 T \u27e7\u00b2 \u211cs a b \u00d7 \u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs as bs\n\n-- Weakening of type variables\n\n\u03c4weaken : \u2200 {\u03a3 \u03a5 K} \u2192 (\u03a3 \u2291 \u03a5) \u2192 TVar K \u03a3 \u2192 TVar K \u03a5\n\u03c4weaken id x = x\n\u03c4weaken (keep K \u03a3\u2291\u03a5) zero = zero\n\u03c4weaken (keep K \u03a3\u2291\u03a5) (suc x) = suc (\u03c4weaken \u03a3\u2291\u03a5 x)\n\u03c4weaken (skip K \u03a3\u2291\u03a5) x = suc (\u03c4weaken \u03a3\u2291\u03a5 x)\n\n\u03c4weaken\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5 K} (\u03c4 : TVar K \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) (As : \u03a3\u27e6 \u03a5 \u27e7) \u2192 \n \u03c4\u27e6 \u03c4 \u27e7 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As) \u2261 \u03c4\u27e6 \u03c4weaken \u03a3\u2291\u03a5 \u03c4 \u27e7 As\n\u03c4weaken\u27e6 \u03c4 \u27e7 id As = refl\n\u03c4weaken\u27e6 zero \u27e7 (keep K \u03a3\u2291\u03a5) (A , As) = refl\n\u03c4weaken\u27e6 suc \u03c4 \u27e7 (keep K \u03a3\u2291\u03a5) (A , As) = \u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As\n\u03c4weaken\u27e6 \u03c4 \u27e7 (skip K \u03a3\u2291\u03a5) (A , As) = \u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As\n\n\u03c4weaken\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5 K} (\u03c4 : TVar K \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) {As Bs} (\u211cs : \u03a5 \u220b As \u2194* Bs) \u2192 \n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) \u2261 \n struct K (\u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As) (\u03c4\u27e6 \u03c4weaken \u03a3\u2291\u03a5 \u03c4 \u27e7\u00b2 \u211cs) (\u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 Bs)\n\u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 id \u211cs = refl\n\u03c4weaken\u27e6 zero \u27e7\u00b2 (keep K \u03a3\u2291\u03a5) (\u211c , \u211cs) = refl\n\u03c4weaken\u27e6 suc \u03c4 \u27e7\u00b2 (keep K \u03a3\u2291\u03a5) (\u211c , \u211cs) = \u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs\n\u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 (skip K \u03a3\u2291\u03a5) (\u211c , \u211cs) = \u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs\n\n-- Weakening of types\n\nweaken : \u2200 {\u03a3 \u03a5 K} \u2192 (\u03a3 \u2291 \u03a5) \u2192 Typ \u03a3 K \u2192 Typ \u03a5 K\nweaken \u03a3\u2291\u03a5 (const C) = const C\nweaken \u03a3\u2291\u03a5 (abs K T) = abs K (weaken (keep K \u03a3\u2291\u03a5) T)\nweaken \u03a3\u2291\u03a5 (app T U) = app (weaken \u03a3\u2291\u03a5 T) (weaken \u03a3\u2291\u03a5 U)\nweaken \u03a3\u2291\u03a5 (var \u03c4) = var (\u03c4weaken \u03a3\u2291\u03a5 \u03c4)\n\nweaken\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5 K} (T : Typ \u03a3 K) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) (As : \u03a3\u27e6 \u03a5 \u27e7) \u2192 \n T\u27e6 T \u27e7 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As) \u2261 T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7 As\nweaken\u27e6 const C \u27e7 \u03a3\u2291\u03a5 As = refl\nweaken\u27e6 abs K T \u27e7 \u03a3\u2291\u03a5 As = ext (\u03bb A \u2192 weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (A , As))\nweaken\u27e6 app T U \u27e7 \u03a3\u2291\u03a5 As = apply (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) \nweaken\u27e6 var \u03c4 \u27e7 \u03a3\u2291\u03a5 As = \u03c4weaken\u27e6 \u03c4 \u27e7 \u03a3\u2291\u03a5 As\n \nweaken\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5 K} (T : Typ \u03a3 K) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) {As Bs} (\u211cs : \u03a5 \u220b As \u2194* Bs) \u2192 \n T\u27e6 T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) \u2261 struct K (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 Bs)\nweaken\u27e6 const C \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs = refl\nweaken\u27e6 abs K {L} T \u27e7\u00b2 \u03a3\u2291\u03a5 {As} {Bs} \u211cs =\n iext (\u03bb A \u2192 iext (\u03bb B \u2192 ext (\u03bb \u211c \u2192 begin\n T\u27e6 abs K T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) \u211c\n \u2261\u27e8 weaken\u27e6 T \u27e7\u00b2 (keep K \u03a3\u2291\u03a5) (\u211c , \u211cs) \u27e9\n struct L \n (weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (A , As)) \n (T\u27e6 weaken (keep K \u03a3\u2291\u03a5) T \u27e7\u00b2 (\u211c , \u211cs)) \n (weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (B , Bs))\n \u2261\u27e8 struct-ext K L \n (\u03bb A \u2192 weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (A , As)) \n (\u03bb \u211c \u2192 T\u27e6 weaken (keep K \u03a3\u2291\u03a5) T \u27e7\u00b2 (\u211c , \u211cs)) \n (\u03bb B \u2192 weaken\u27e6 T \u27e7 (keep K \u03a3\u2291\u03a5) (B , Bs)) \u211c \u27e9\n struct (K \u21d2 L) \n (weaken\u27e6 abs K T \u27e7 \u03a3\u2291\u03a5 As)\n (T\u27e6 weaken \u03a3\u2291\u03a5 (abs K T) \u27e7\u00b2 \u211cs) \n (weaken\u27e6 abs K T \u27e7 \u03a3\u2291\u03a5 Bs) \u211c\n \u220e)))\nweaken\u27e6 app {K} {L} T U \u27e7\u00b2 \u03a3\u2291\u03a5 {As} {Bs} \u211cs = \n begin\n T\u27e6 app T U \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)\n \u2261\u27e8 cong (T\u27e6 T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)) (weaken\u27e6 U \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs) \u27e9\n T\u27e6 T \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs)\n (struct K (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs))\n \u2261\u27e8 cong (\u03bb X \u2192 X (struct K (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs)))\n (weaken\u27e6 T \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs) \u27e9\n (struct (K \u21d2 L) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 Bs))\n (struct K (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs))\n \u2261\u27e8 struct-apply K L \n (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 Bs) \n (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 As) (T\u27e6 weaken \u03a3\u2291\u03a5 U \u27e7\u00b2 \u211cs) (weaken\u27e6 U \u27e7 \u03a3\u2291\u03a5 Bs) \u27e9\n struct L\n (weaken\u27e6 app T U \u27e7 \u03a3\u2291\u03a5 As)\n (T\u27e6 weaken \u03a3\u2291\u03a5 (app T U) \u27e7\u00b2 \u211cs) \n (weaken\u27e6 app T U \u27e7 \u03a3\u2291\u03a5 Bs)\n \u220e\nweaken\u27e6 var \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs = \u03c4weaken\u27e6 \u03c4 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs\n\n-- Weakening on type contexts\n\nweakens : \u2200 {\u03a3 \u03a5} \u2192 (\u03a3 \u2291 \u03a5) \u2192 Typs \u03a3 \u2192 Typs \u03a5\nweakens \u03a3\u2291\u03a5 [] = []\nweakens \u03a3\u2291\u03a5 (T \u2237 \u0393) = weaken \u03a3\u2291\u03a5 T \u2237 weakens \u03a3\u2291\u03a5 \u0393\n\nweakens\u27e6_\u27e7 : \u2200 {\u03a3 \u03a5} (\u0393 : Typs \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) (As : \u03a3\u27e6 \u03a5 \u27e7) \u2192 \n \u0393\u27e6 \u0393 \u27e7 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7 As) \u2192 \u0393\u27e6 weakens \u03a3\u2291\u03a5 \u0393 \u27e7 As\nweakens\u27e6 [] \u27e7 \u03a3\u2291\u03a5 As tt = tt\nweakens\u27e6 T \u2237 \u0393 \u27e7 \u03a3\u2291\u03a5 As (B , Bs) = (cast (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 As) B , weakens\u27e6 \u0393 \u27e7 \u03a3\u2291\u03a5 As Bs)\n\nweakens\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 \u03a5} (\u0393 : Typs \u03a3) (\u03a3\u2291\u03a5 : \u03a3 \u2291 \u03a5) {As Bs} (\u211cs : \u03a5 \u220b As \u2194* Bs) {as bs} \u2192 \n \u0393\u27e6 \u0393 \u27e7\u00b2 (\u2291\u27e6 \u03a3\u2291\u03a5 \u27e7\u00b2 \u211cs) as bs \u2192 \n \u0393\u27e6 weakens \u03a3\u2291\u03a5 \u0393 \u27e7\u00b2 \u211cs (weakens\u27e6 \u0393 \u27e7 \u03a3\u2291\u03a5 As as) (weakens\u27e6 \u0393 \u27e7 \u03a3\u2291\u03a5 Bs bs)\nweakens\u27e6 [] \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs tt\n = tt\nweakens\u27e6 T \u2237 \u0393 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs (a\u211cb , as\u211cbs) \n = ( struct-cast (T\u27e6 weaken \u03a3\u2291\u03a5 T \u27e7\u00b2 \u211cs) \n (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 _) (weaken\u27e6 T \u27e7 \u03a3\u2291\u03a5 _) (cast\u00b2 (weaken\u27e6 T \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs) a\u211cb)\n , weakens\u27e6 \u0393 \u27e7\u00b2 \u03a3\u2291\u03a5 \u211cs as\u211cbs)\n\n-- Susbtitution on type variables under a context\n\n\u03c4substn+ : \u2200 \u03a3 {\u03a5 K L} \u2192 TVar K (\u03a3 ++ (L \u2237 \u03a5)) \u2192 Typ \u03a5 L \u2192 Typ (\u03a3 ++ \u03a5) K\n\u03c4substn+ [] zero U = U\n\u03c4substn+ [] (suc \u03c4) U = var \u03c4\n\u03c4substn+ (K \u2237 \u03a3) zero U = var zero\n\u03c4substn+ (K \u2237 \u03a3) (suc \u03c4) U = weaken (skip K id) (\u03c4substn+ \u03a3 \u03c4 U)\n\n\u03c4substn+_\u27e6_\u27e7\u27e6_\u27e7 : \u2200 \u03a3 {\u03a5 K L} (\u03c4 : TVar K (\u03a3 ++ (L \u2237 \u03a5))) (U : Typ \u03a5 L) \n (As : \u03a3\u27e6 \u03a3 \u27e7) (Bs : \u03a3\u27e6 \u03a5 \u27e7) \u2192\n \u03c4\u27e6 \u03c4 \u27e7 (\u03a3 \u220b As ++ (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7 Bs , Bs)) \u2261 \n T\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (\u03a3 \u220b As ++ \u03a5 \u220b Bs)\n\u03c4substn+ [] \u27e6 zero \u27e7\u27e6 U \u27e7 tt Bs = refl\n\u03c4substn+ [] \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7 tt Bs = refl\n\u03c4substn+ (K \u2237 \u03a3) \u27e6 zero \u27e7\u27e6 U \u27e7 (A , As) Bs = refl\n\u03c4substn+ (K \u2237 \u03a3) \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7 (A , As) Bs = trans \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Bs) \n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip K id) (A , (\u03a3 \u220b As ++ _ \u220b Bs)))\n\n\u03c4substn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 : \u2200 \u03a3 {\u03a5 L K} (\u03c4 : TVar K (\u03a3 ++ (L \u2237 \u03a5))) (U : Typ \u03a5 L) {As Bs Cs Ds} \n (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 (\u2111s : \u03a5 \u220b Cs \u2194* Ds) \u2192\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s)) \u2261 \n struct K \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s) )\n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds)\n\u03c4substn+ [] \u27e6 zero \u27e7\u27e6 U \u27e7\u00b2 tt \u2111s = refl\n\u03c4substn+ [] \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7\u00b2 tt \u2111s = refl\n\u03c4substn+ (J \u2237 \u03a3) \u27e6 zero \u27e7\u27e6 U \u27e7\u00b2 (\u211c , \u211cs) \u2111s = refl\n\u03c4substn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 (J \u2237 \u03a3) {\u03a5} {L} {K} (suc \u03c4) U {A , As} {B , Bs} {Cs} {Ds} (\u211c , \u211cs) \u2111s = \n begin\n \u03c4\u27e6 \u03c4 \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s))\n \u2261\u27e8 \u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s \u27e9\n struct K \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds)\n \u2261\u27e8 cong (\u03bb X \u2192 struct K (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs) X (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds)) \n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7\u00b2 (skip J id) (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))) \u27e9\n struct K \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs) \n (struct K\n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip J id) (A , (\u03a3 \u220b As ++ \u03a5 \u220b Cs))) \n (T\u27e6 weaken (skip J id) (\u03c4substn+ \u03a3 \u03c4 U) \u27e7\u00b2 (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))) \n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip J id) (B , (\u03a3 \u220b Bs ++ \u03a5 \u220b Ds)))) \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds)\n \u2261\u27e8 struct-trans K \n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Cs) \n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip J id) (A , (\u03a3 \u220b As ++ \u03a5 \u220b Cs)))\n (T\u27e6 weaken (skip J id) (\u03c4substn+ \u03a3 \u03c4 U) \u27e7\u00b2 (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s)))\n (weaken\u27e6 \u03c4substn+ \u03a3 \u03c4 U \u27e7 (skip J id) (B , (\u03a3 \u220b Bs ++ \u03a5 \u220b Ds)))\n (\u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 Bs Ds) \u27e9\n struct K \n (\u03c4substn+ (J \u2237 \u03a3) \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7 (A , As) Cs) \n (T\u27e6 \u03c4substn+ (J \u2237 \u03a3) (suc \u03c4) U \u27e7\u00b2 (\u211c , (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s)) )\n (\u03c4substn+ (J \u2237 \u03a3) \u27e6 suc \u03c4 \u27e7\u27e6 U \u27e7 (B , Bs) Ds) \n \u220e\n\n-- Substitution on types under a context\n\nsubstn+ : \u2200 \u03a3 {\u03a5 K L} \u2192 Typ (\u03a3 ++ (L \u2237 \u03a5)) K \u2192 Typ \u03a5 L \u2192 Typ (\u03a3 ++ \u03a5) K\nsubstn+ \u03a3 (const C) U = const C\nsubstn+ \u03a3 (abs K T) U = abs K (substn+ (K \u2237 \u03a3) T U)\nsubstn+ \u03a3 (app S T) U = app (substn+ \u03a3 S U) (substn+ \u03a3 T U)\nsubstn+ \u03a3 (var \u03c4) U = \u03c4substn+ \u03a3 \u03c4 U\n\nsubstn+_\u27e6_\u27e7\u27e6_\u27e7 : \u2200 \u03a3 {\u03a5 K L} (T : Typ (\u03a3 ++ (L \u2237 \u03a5)) K) (U : Typ \u03a5 L) \n (As : \u03a3\u27e6 \u03a3 \u27e7) (Bs : \u03a3\u27e6 \u03a5 \u27e7) \u2192\n T\u27e6 T \u27e7 (\u03a3 \u220b As ++ (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7 Bs , Bs)) \u2261 \n T\u27e6 substn+ \u03a3 T U \u27e7 (\u03a3 \u220b As ++ \u03a5 \u220b Bs)\nsubstn+ \u03a3 \u27e6 const C \u27e7\u27e6 U \u27e7 As Bs = refl\nsubstn+ \u03a3 \u27e6 abs K T \u27e7\u27e6 U \u27e7 As Bs = ext (\u03bb A \u2192 substn+ K \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (A , As) Bs)\nsubstn+ \u03a3 \u27e6 app S T \u27e7\u27e6 U \u27e7 As Bs = apply (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 As Bs) (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Bs)\nsubstn+ \u03a3 \u27e6 var \u03c4 \u27e7\u27e6 U \u27e7 As Bs = \u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7 As Bs\n\nsubstn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 : \u2200 \u03a3 {\u03a5 L K} (T : Typ (\u03a3 ++ (L \u2237 \u03a5)) K) (U : Typ \u03a5 L) {As Bs Cs Ds} \n (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 (\u2111s : \u03a5 \u220b Cs \u2194* Ds) \u2192\n T\u27e6 T \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s)) \u2261 \n struct K \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s) )\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds)\nsubstn+ \u03a3 \u27e6 const C \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s = refl\nsubstn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 \u03a3 {\u03a5} {L} (abs J {K} T) U {As} {Bs} {Cs} {Ds} \u211cs \u2111s = \n iext (\u03bb A \u2192 iext (\u03bb B \u2192 ext (\u03bb \u211c \u2192 begin\n T\u27e6 abs J T \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 (L \u2237 \u03a5) \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s)) \u211c\n \u2261\u27e8 substn+ (J \u2237 \u03a3) \u27e6 T \u27e7\u27e6 U \u27e7\u00b2 (\u211c , \u211cs) \u2111s \u27e9\n struct K \n (substn+ J \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (A , As) Cs) \n (T\u27e6 substn+ (J \u2237 \u03a3) T U \u27e7\u00b2 ((J \u2237 \u03a3) \u220b (\u211c , \u211cs) ++\u00b2 \u03a5 \u220b \u2111s)) \n (substn+ J \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (B , Bs) Ds)\n \u2261\u27e8 struct-ext J K \n (\u03bb A \u2192 substn+ J \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (A , As) Cs) \n (\u03bb \u211c \u2192 T\u27e6 substn+ (J \u2237 \u03a3) T U \u27e7\u00b2 ((J \u2237 \u03a3) \u220b \u211c , \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (\u03bb B \u2192 substn+ J \u2237 \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 (B , Bs) Ds) \u211c \u27e9\n struct (J \u21d2 K) \n (substn+ \u03a3 \u27e6 abs J T \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 substn+ \u03a3 (abs J T) U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s)) \n (substn+ \u03a3 \u27e6 abs J T \u27e7\u27e6 U \u27e7 Bs Ds) \u211c\n \u220e)))\nsubstn+_\u27e6_\u27e7\u27e6_\u27e7\u00b2 \u03a3 {\u03a5} {L} (app {J} {K} S T) U {As} {Bs} {Cs} {Ds} \u211cs \u2111s = \n begin\n T\u27e6 app S T \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 L \u2237 \u03a5 \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s))\n \u2261\u27e8 cong (T\u27e6 S \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 L \u2237 \u03a5 \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s))) (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s) \u27e9\n T\u27e6 S \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 L \u2237 \u03a5 \u220b (T\u27e6 U \u27e7\u00b2 \u2111s , \u2111s))\n (struct J \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds))\n \u2261\u27e8 cong (\u03bb X \u2192 X (struct J \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds))) \n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s) \u27e9\n struct (J \u21d2 K) \n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 substn+ \u03a3 S U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 Bs Ds) \n (struct J \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds))\n \u2261\u27e8 struct-apply J K \n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 As Cs) \n (T\u27e6 substn+ \u03a3 S U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s)) \n (substn+ \u03a3 \u27e6 S \u27e7\u27e6 U \u27e7 Bs Ds) \n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 T U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 T \u27e7\u27e6 U \u27e7 Bs Ds) \u27e9\n struct K \n (substn+ \u03a3 \u27e6 app S T \u27e7\u27e6 U \u27e7 As Cs)\n (T\u27e6 substn+ \u03a3 (app S T) U \u27e7\u00b2 (\u03a3 \u220b \u211cs ++\u00b2 \u03a5 \u220b \u2111s))\n (substn+ \u03a3 \u27e6 app S T \u27e7\u27e6 U \u27e7 Bs Ds)\n \u220e\nsubstn+ \u03a3 \u27e6 var \u03c4 \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s = \u03c4substn+ \u03a3 \u27e6 \u03c4 \u27e7\u27e6 U \u27e7\u00b2 \u211cs \u2111s\n\n-- Substitution on types\n\nsubstn : \u2200 {\u03a3 K L} \u2192 Typ (L \u2237 \u03a3) K \u2192 Typ \u03a3 L \u2192 Typ \u03a3 K\nsubstn = substn+ []\n\nsubstn\u27e6_\u27e7\u27e6_\u27e7 : \u2200 {\u03a3 K L} (T : Typ (L \u2237 \u03a3) K) (U : Typ \u03a3 L) (As : \u03a3\u27e6 \u03a3 \u27e7)\u2192\n T\u27e6 T \u27e7 (T\u27e6 U \u27e7 As , As) \u2261 T\u27e6 substn T U \u27e7 As\nsubstn\u27e6 T \u27e7\u27e6 U \u27e7 = substn+ [] \u27e6 T \u27e7\u27e6 U \u27e7 tt\n\nsubstn\u27e6_\u27e7\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 K L} (T : Typ (L \u2237 \u03a3) K) (U : Typ \u03a3 L) {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192\n T\u27e6 T \u27e7\u00b2 (T\u27e6 U \u27e7\u00b2 \u211cs , \u211cs) \u2261 \n struct K (substn\u27e6 T \u27e7\u27e6 U \u27e7 As) (T\u27e6 substn T U \u27e7\u00b2 \u211cs) (substn\u27e6 T \u27e7\u27e6 U \u27e7 Bs)\nsubstn\u27e6 T \u27e7\u27e6 U \u27e7\u00b2 = substn+ [] \u27e6 T \u27e7\u27e6 U \u27e7\u00b2 tt\n\n-- Eta-beta equivalence on types\n\ndata _\u220b_\u2263_ {\u03a3} : \u2200 K \u2192 Typ \u03a3 K \u2192 Typ \u03a3 K \u2192 Set where\n abs : \u2200 K {L T U} \u2192 (L \u220b T \u2263 U) \u2192 ((K \u21d2 L) \u220b abs K T \u2263 abs K U)\n app : \u2200 {K L F G T U} \u2192 ((K \u21d2 L) \u220b F \u2263 G) \u2192 (K \u220b T \u2263 U) \u2192 (L \u220b app F T \u2263 app G U)\n beta : \u2200 {K L} T U \u2192 (L \u220b app (abs K T) U \u2263 substn T U)\n eta : \u2200 {K L} T \u2192 ((K \u21d2 L) \u220b T \u2263 abs K (app (weaken (skip K id) T) tvar\u2080))\n \u2263-refl : \u2200 {K T} \u2192 (K \u220b T \u2263 T)\n \u2263-sym : \u2200 {K T U} \u2192 (K \u220b T \u2263 U) \u2192 (K \u220b U \u2263 T)\n \u2263-trans : \u2200 {K T U V} \u2192 (K \u220b T \u2263 U) \u2192 (K \u220b U \u2263 V) \u2192 (K \u220b T \u2263 V)\n\n\u2263\u27e6_\u27e7 : \u2200 {\u03a3 K} {T U : Typ \u03a3 K} \u2192 (K \u220b T \u2263 U) \u2192 \u2200 As \u2192 T\u27e6 T \u27e7 As \u2261 T\u27e6 U \u27e7 As\n\u2263\u27e6 abs K T\u2263U \u27e7 As = ext (\u03bb A \u2192 \u2263\u27e6 T\u2263U \u27e7 (A , As))\n\u2263\u27e6 app F\u2263G T\u2263U \u27e7 As = apply (\u2263\u27e6 F\u2263G \u27e7 As) (\u2263\u27e6 T\u2263U \u27e7 As)\n\u2263\u27e6 beta T U \u27e7 As = substn\u27e6 T \u27e7\u27e6 U \u27e7 As\n\u2263\u27e6 eta {K} T \u27e7 As = ext (\u03bb A \u2192 apply (weaken\u27e6 T \u27e7 (skip K id) (A , As)) refl)\n\u2263\u27e6 \u2263-refl \u27e7 As = refl\n\u2263\u27e6 \u2263-sym T\u2263U \u27e7 As = sym (\u2263\u27e6 T\u2263U \u27e7 As)\n\u2263\u27e6 \u2263-trans T\u2263U U\u2263V \u27e7 As = trans (\u2263\u27e6 T\u2263U \u27e7 As) (\u2263\u27e6 U\u2263V \u27e7 As)\n\n\u2263\u27e6_\u27e7\u00b2 : \u2200 {\u03a3 K} {T U : Typ \u03a3 K} (T\u2263U : K \u220b T \u2263 U) {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 \n T\u27e6 T \u27e7\u00b2 \u211cs \u2261 struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs)\n\u2263\u27e6 abs K {L} {T} {U} T\u2263U \u27e7\u00b2 {As} {Bs} \u211cs = \n iext (\u03bb A \u2192 iext (\u03bb B \u2192 ext (\u03bb \u211c \u2192 begin\n T\u27e6 T \u27e7\u00b2 (\u211c , \u211cs)\n \u2261\u27e8 \u2263\u27e6 T\u2263U \u27e7\u00b2 (\u211c , \u211cs) \u27e9\n struct L (\u2263\u27e6 T\u2263U \u27e7 (A , As)) (T\u27e6 U \u27e7\u00b2 (\u211c , \u211cs)) (\u2263\u27e6 T\u2263U \u27e7 (B , Bs))\n \u2261\u27e8 struct-ext K L (\u03bb A \u2192 \u2263\u27e6 T\u2263U \u27e7 (A , As)) (\u03bb \u211c' \u2192 T\u27e6 U \u27e7\u00b2 (\u211c' , \u211cs)) (\u03bb B \u2192 \u2263\u27e6 T\u2263U \u27e7 (B , Bs)) \u211c \u27e9\n struct (K \u21d2 L) (\u2263\u27e6 abs K T\u2263U \u27e7 As) (T\u27e6 abs K U \u27e7\u00b2 \u211cs) (\u2263\u27e6 abs K T\u2263U \u27e7 Bs) \u211c\n \u220e)))\n\u2263\u27e6 app {K} {L} {F} {G} {T} {U} F\u2263G T\u2263U \u27e7\u00b2 {As} {Bs} \u211cs = \n begin\n T\u27e6 app F T \u27e7\u00b2 \u211cs\n \u2261\u27e8 cong (T\u27e6 F \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7\u00b2 \u211cs) \u27e9\n T\u27e6 F \u27e7\u00b2 \u211cs (struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs))\n \u2261\u27e8 cong (\u03bb X \u2192 X (struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs)))\n (\u2263\u27e6 F\u2263G \u27e7\u00b2 \u211cs) \u27e9\n struct (K \u21d2 L) (\u2263\u27e6 F\u2263G \u27e7 As) (T\u27e6 G \u27e7\u00b2 \u211cs) (\u2263\u27e6 F\u2263G \u27e7 Bs)\n (struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs))\n \u2261\u27e8 struct-apply K L\n (\u2263\u27e6 F\u2263G \u27e7 As) (T\u27e6 G \u27e7\u00b2 \u211cs) (\u2263\u27e6 F\u2263G \u27e7 Bs)\n (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs) \u27e9\n struct L (\u2263\u27e6 app F\u2263G T\u2263U \u27e7 As) (T\u27e6 app G U \u27e7\u00b2 \u211cs) (\u2263\u27e6 app F\u2263G T\u2263U \u27e7 Bs)\n \u220e\n\u2263\u27e6 beta T U \u27e7\u00b2 \u211cs = substn\u27e6 T \u27e7\u27e6 U \u27e7\u00b2 \u211cs\n\u2263\u27e6 eta {K} {L} T \u27e7\u00b2 {As} {Bs} \u211cs = iext (\u03bb A \u2192 iext (\u03bb B \u2192 ext (\u03bb \u211c \u2192 \n begin\n T\u27e6 T \u27e7\u00b2 \u211cs \u211c\n \u2261\u27e8 cong (\u03bb X \u2192 X \u211c) (weaken\u27e6 T \u27e7\u00b2 (skip K id) (\u211c , \u211cs)) \u27e9\n struct (K \u21d2 L) \n (weaken\u27e6 T \u27e7 (skip K id) (A , As))\n (T\u27e6 weaken (skip K id) T \u27e7\u00b2 (\u211c , \u211cs))\n (weaken\u27e6 T \u27e7 (skip K id) (B , Bs)) \u211c\n \u2261\u27e8 struct-apply K L \n (weaken\u27e6 T \u27e7 (skip K id) (A , As)) \n (T\u27e6 weaken (skip K id) T \u27e7\u00b2 (\u211c , \u211cs)) \n (weaken\u27e6 T \u27e7 (skip K id) (B , Bs)) refl \u211c refl \u27e9\n struct L \n (apply (weaken\u27e6 T \u27e7 (skip K id) (A , As)) refl)\n (T\u27e6 weaken (skip K id) T \u27e7\u00b2 (\u211c , \u211cs) \u211c)\n (apply (weaken\u27e6 T \u27e7 (skip K id) (B , Bs)) refl)\n \u2261\u27e8 struct-ext K L\n (\u03bb A \u2192 apply (weaken\u27e6 T \u27e7 (skip K id) (A , As)) refl) \n (\u03bb \u211c \u2192 T\u27e6 weaken (skip K id) T \u27e7\u00b2 (\u211c , \u211cs) \u211c) \n (\u03bb B \u2192 apply (weaken\u27e6 T \u27e7 (skip K id) (B , Bs)) refl) \u211c \u27e9\n struct (K \u21d2 L) \n (\u2263\u27e6 eta T \u27e7 As)\n (T\u27e6 abs K (app (weaken (skip K id) T) (var zero)) \u27e7\u00b2 \u211cs)\n (\u2263\u27e6 eta T \u27e7 Bs) \u211c\n \u220e)))\n\u2263\u27e6 \u2263-refl \u27e7\u00b2 \u211cs = refl\n\u2263\u27e6 \u2263-sym {K} {T} {U} T\u2263U \u27e7\u00b2 {As} {Bs} \u211cs = \n struct-sym K (\u2263\u27e6 T\u2263U \u27e7 As) (\u2263\u27e6 T\u2263U \u27e7 Bs) (\u2263\u27e6 T\u2263U \u27e7\u00b2 \u211cs)\n\u2263\u27e6 \u2263-trans {K} {T} {U} {V} T\u2263U U\u2263V \u27e7\u00b2 {As} {Bs} \u211cs =\n begin\n T\u27e6 T \u27e7\u00b2 \u211cs\n \u2261\u27e8 \u2263\u27e6 T\u2263U \u27e7\u00b2 \u211cs \u27e9\n struct K (\u2263\u27e6 T\u2263U \u27e7 As) (T\u27e6 U \u27e7\u00b2 \u211cs) (\u2263\u27e6 T\u2263U \u27e7 Bs)\n \u2261\u27e8 cong (\u03bb X \u2192 struct K (\u2263\u27e6 T\u2263U \u27e7 As) X (\u2263\u27e6 T\u2263U \u27e7 Bs)) (\u2263\u27e6 U\u2263V \u27e7\u00b2 \u211cs) \u27e9\n struct K (\u2263\u27e6 T\u2263U \u27e7 As) (struct K (\u2263\u27e6 U\u2263V \u27e7 As) (T\u27e6 V \u27e7\u00b2 \u211cs) (\u2263\u27e6 U\u2263V \u27e7 Bs)) (\u2263\u27e6 T\u2263U \u27e7 Bs)\n \u2261\u27e8 struct-trans K (\u2263\u27e6 T\u2263U \u27e7 As) (\u2263\u27e6 U\u2263V \u27e7 As) (T\u27e6 V \u27e7\u00b2 \u211cs) (\u2263\u27e6 U\u2263V \u27e7 Bs) (\u2263\u27e6 T\u2263U \u27e7 Bs) \u27e9\n struct K (\u2263\u27e6 \u2263-trans T\u2263U U\u2263V \u27e7 As) (T\u27e6 V \u27e7\u00b2 \u211cs) (\u2263\u27e6 \u2263-trans T\u2263U U\u2263V \u27e7 Bs)\n \u220e\n\n-- Variables\n\ndata Var {\u03a3 : Kinds} {\u03b1} (T : Typ \u03a3 (set \u03b1)) : Typs \u03a3 \u2192 Set where\n zero : \u2200 {\u0393} \u2192 Var T (T \u2237 \u0393)\n suc : \u2200 {\u03b2 \u0393} {U : Typ \u03a3 (set \u03b2)} \u2192 Var T \u0393 \u2192 Var T (U \u2237 \u0393)\n\nx\u27e6_\u27e7 : \u2200 {\u03a3} {\u0393 : Typs \u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} \u2192 \n Var T \u0393 \u2192 (As : \u03a3\u27e6 \u03a3 \u27e7) \u2192 (as : \u0393\u27e6 \u0393 \u27e7 As) \u2192 (T\u27e6 T \u27e7 As)\nx\u27e6 zero \u27e7 As (a , as) = a\nx\u27e6 suc x \u27e7 As (a , as) = x\u27e6 x \u27e7 As as\n\nx\u27e6_\u27e7\u00b2 : \u2200 {\u03a3} {\u0393 : Typs \u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} (x : Var T \u0393) \u2192 \n \u2200 {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) {as bs} \u2192 \n (\u0393\u27e6 \u0393 \u27e7\u00b2 \u211cs as bs) \u2192 (T\u27e6 T \u27e7\u00b2 \u211cs (x\u27e6 x \u27e7 As as) (x\u27e6 x \u27e7 Bs bs))\nx\u27e6 zero \u27e7\u00b2 \u211cs (a\u211cb , as\u211cbs) = a\u211cb\nx\u27e6 suc x \u27e7\u00b2 \u211cs (a\u211cb , as\u211cbs) = x\u27e6 x \u27e7\u00b2 \u211cs as\u211cbs\n\n-- Constants\n\ndata Const {\u03a3 : Kinds} : \u2200 {\u03b1} \u2192 Typ \u03a3 (set \u03b1) \u2192 Set where\n pair : \u2200 {\u03b1 \u03b2} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 (set \u03b2) (tvar\u2081 \u22b8 (tvar\u2080 \u22b8 (tvar\u2081 \u2297 tvar\u2080)))))\n fst : \u2200 {\u03b1 \u03b2} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 (set \u03b2) ((tvar\u2081 \u2297 tvar\u2080) \u22b8 tvar\u2081)))\n snd : \u2200 {\u03b1 \u03b2} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 (set \u03b2) ((tvar\u2081 \u2297 tvar\u2080) \u22b8 tvar\u2080)))\n \u227c-refl : Const (\u03a0 time (tvar\u2080 \u227c tvar\u2080))\n \u227c-trans : Const (\u03a0 time (\u03a0 time (\u03a0 time ((tvar\u2082 \u227c tvar\u2081) \u22b8 ((tvar\u2081 \u227c tvar\u2080) \u22b8 (tvar\u2082 \u227c tvar\u2080))))))\n \u227c-absurd : \u2200 {\u03b1} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 time (\u03a0 time ((tvar\u2081 \u227a tvar\u2080) \u22b8 ((tvar\u2080 \u227c tvar\u2081) \u22b8 tvar\u2082)))))\n \u227c-case : \u2200 {\u03b1} \u2192 Const (\u03a0 (set \u03b1) (\u03a0 time (\u03a0 time (((tvar\u2081 \u227c tvar\u2080) \u22b8 tvar\u2082) \u22b8 (((tvar\u2080 \u227a tvar\u2081) \u22b8 tvar\u2082) \u22b8 tvar\u2082)))))\n\nabsurd : \u2200 {\u03b1} {A : Set \u03b1} b \u2192 True b \u2192 True (not b) \u2192 A\nabsurd true tt ()\nabsurd false () tt\n\ncond : \u2200 {\u03b1} {A : Set \u03b1} b \u2192 (True (not b) \u2192 A) \u2192 (True b \u2192 A) \u2192 A\ncond true f g = g tt\ncond false f g = f tt\n\nc\u27e6_\u27e7 : \u2200 {\u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} \u2192 \n Const T \u2192 (As : \u03a3\u27e6 \u03a3 \u27e7) \u2192 (T\u27e6 T \u27e7 As)\nc\u27e6 pair \u27e7 As = \u03bb A B a b \u2192 (a , b)\nc\u27e6 fst \u27e7 As = \u03bb A B \u2192 proj\u2081\nc\u27e6 snd \u27e7 As = \u03bb A B \u2192 proj\u2082\nc\u27e6 \u227c-refl \u27e7 As = \u2264-refl\nc\u27e6 \u227c-trans \u27e7 As = \u2264-trans\nc\u27e6 \u227c-absurd \u27e7 As = \u03bb A t u \u2192 absurd (t < u)\nc\u27e6 \u227c-case \u27e7 As = \u03bb A t u \u2192 cond (u < t)\n\ncond\u00b2 : \u2200 {\u03b1} {A A\u2032 : Set \u03b1} (\u211c : A \u2192 A\u2032 \u2192 Set \u03b1) b \u2192\n \u2200 {f f\u2032} \u2192 (\u2200 b\u00d7 \u2192 \u211c (f b\u00d7) (f\u2032 b\u00d7)) \u2192\n \u2200 {g g\u2032} \u2192 (\u2200 b\u2713 \u2192 \u211c (g b\u2713) (g\u2032 b\u2713)) \u2192\n \u211c (cond b f g) (cond b f\u2032 g\u2032)\ncond\u00b2 \u211c true f\u211cf\u2032 g\u211cg\u2032 = g\u211cg\u2032 tt\ncond\u00b2 \u211c false f\u211cf\u2032 g\u211cg\u2032 = f\u211cf\u2032 tt\n\nc\u27e6_\u27e7\u00b2 : \u2200 {\u03a3} {\u03b1} {T : Typ \u03a3 (set \u03b1)} (c : Const T) \u2192 \n \u2200 {As Bs} (\u211cs : \u03a3 \u220b As \u2194* Bs) \u2192 \n (T\u27e6 T \u27e7\u00b2 \u211cs (c\u27e6 c \u27e7 As) (c\u27e6 c \u27e7 Bs))\nc\u27e6 pair \u27e7\u00b2 \u211cs = \u03bb \u211c \u2111 a\u211cb c\u2111d \u2192 (a\u211cb , c\u2111d)\nc\u27e6 fst \u27e7\u00b2 \u211cs = \u03bb \u211c \u2111 \u2192 proj\u2081\nc\u27e6 snd \u27e7\u00b2 \u211cs = \u03bb \u211c \u2111 \u2192 proj\u2082\nc\u27e6 \u227c-refl \u27e7\u00b2 \u211cs = _\nc\u27e6 \u227c-trans \u27e7\u00b2 \u211cs = _\nc\u27e6 \u227c-absurd \u27e7\u00b2 \u211cs = \u03bb \u211c {t} t\u2261t {u} u\u2261u {t n \u2192 b ^(m \u2238 n) \u2261 b ^\u207a m \u2219 b ^\u207b n\n\n ^-\u2296 : \u2200 m n \u2192 b ^(m \u2296 n) \u2261 b ^\u207a m \u2219 b ^\u207b n\n ^-\u2296 m 0 = ! is-\u03b5-right \u03b5\u207b\u00b9\u2261\u03b5\n ^-\u2296 0 (1+ n) = ! idl\n ^-\u2296 (1+ m) (1+ n) =\n b ^(m \u2296 n) \u2261\u27e8 ^-\u2296 m n \u27e9\n (b ^\u207a m \u2219 b ^\u207b n) \u2261\u27e8 ! elim-inner= inverse\u02b3 \u27e9\n (b ^\u207a m \u2219 b) \u2219 (b \u207b\u00b9 \u2219 b ^\u207b n) \u2261\u27e8 \u2219= idp (! \u207b\u00b9-hom\u2032) \u27e9\n (b ^\u207a m \u2219 b) \u2219 (b ^\u207a n \u2219 b)\u207b\u00b9 \u2261\u27e8 \u2219= (! ^\u207a-1+ m) (ap _\u207b\u00b9 (! ^\u207a-1+ n)) \u27e9\n (b \u2219 b ^\u207a m) \u2219 (b \u2219 b ^\u207a n)\u207b\u00b9 \u220e\n\n ^-\u2296' : \u2200 m n \u2192 b ^(m \u2296 n) \u2261 b ^\u207b n \u2219 b ^\u207a m\n ^-\u2296' m 0 = ! is-\u03b5-left \u03b5\u207b\u00b9\u2261\u03b5\n ^-\u2296' 0 (1+ n) = ! idr\n ^-\u2296' (1+ m) (1+ n) =\n b ^(m \u2296 n) \u2261\u27e8 ^-\u2296' m n \u27e9\n (b ^\u207b n \u2219 b ^\u207a m) \u2261\u27e8 ! elim-inner= inverse\u02e1 \u27e9\n (b ^\u207b n \u2219 b \u207b\u00b9) \u2219 (b \u2219 b ^\u207a m) \u2261\u27e8 \u2219= (! \u207b\u00b9-hom\u2032) idp \u27e9\n (b \u2219 b ^\u207a n)\u207b\u00b9 \u2219 (b \u2219 b ^\u207a m) \u220e\n\n ^\u207a-comm : \u2200 m n \u2192 b ^\u207a m \u2219 b ^\u207a n \u2261 b ^\u207a n \u2219 b ^\u207a m\n ^\u207a-comm 0 n = ! comm-\u03b5\n ^\u207a-comm (1+ m) n =\n b \u2219 b\u1d50 \u2219 b\u207f \u2261\u27e8 !assoc= (^\u207a-comm m n) \u27e9\n b \u2219 b\u207f \u2219 b\u1d50 \u2261\u27e8 \u2219= (^\u207a-1+ n) idp \u27e9\n b\u207f \u2219 b \u2219 b\u1d50 \u2261\u27e8 assoc \u27e9\n b\u207f \u2219 (b \u2219 b\u1d50) \u220e\n where\n b\u207f = b ^\u207a n\n b\u1d50 = b ^\u207a m\n\n ^\u207a-^\u207b-comm : \u2200 m n \u2192 b ^\u207a m \u2219 b ^\u207b n \u2261 b ^\u207b n \u2219 b ^\u207a m\n ^\u207a-^\u207b-comm m n = ! ^-\u2296 m n \u2666 ^-\u2296' m n\n\n ^\u207b-^\u207a-comm : \u2200 m n \u2192 b ^\u207b n \u2219 b ^\u207a m \u2261 b ^\u207a m \u2219 b ^\u207b n\n ^\u207b-^\u207a-comm m n = ! ^-\u2296' m n \u2666 ^-\u2296 m n\n\n ^\u207b-comm : \u2200 m n \u2192 b ^\u207b m \u2219 b ^\u207b n \u2261 b ^\u207b n \u2219 b ^\u207b m\n ^\u207b-comm m n = \u207b\u00b9-hom\u2032= (^\u207a-comm n m)\n\n ^\u207b-1+ : \u2200 n \u2192 b ^\u207b(1+ n) \u2261 b \u207b\u00b9 \u2219 b ^\u207b n\n ^\u207b-1+ n = ap _\u207b\u00b9 (^\u207a-1+ n) \u2666 \u207b\u00b9-hom\u2032\n\n ^\u207b\u2032-spec : \u2200 n \u2192 b ^\u207b\u2032 n \u2261 b ^\u207b n\n ^\u207b\u2032-spec 0 = ! \u03b5\u207b\u00b9\u2261\u03b5\n ^\u207b\u2032-spec (1+ n) = ap (_\u2219_ (b \u207b\u00b9)) (^\u207b\u2032-spec n)\n \u2666 ! \u207b\u00b9-hom\u2032\n \u2666 ap _\u207b\u00b9 (! ^\u207a-1+ n)\n\n ^\u207b\u20321-id : b ^\u207b\u2032 1 \u2261 b \u207b\u00b9\n ^\u207b\u20321-id = idr\n\n ^\u207b1-id : b ^\u207b 1 \u2261 b \u207b\u00b9\n ^\u207b1-id = ! ^\u207b\u2032-spec 1 \u2666 ^\u207b\u20321-id\n\n ^\u207b\u20322-\u2219 : b ^\u207b\u2032 2 \u2261 b \u207b\u00b9 \u2219 b \u207b\u00b9\n ^\u207b\u20322-\u2219 = ap (_\u2219_ _) ^\u207b\u20321-id\n\n ^\u207b2-\u2219 : b ^\u207b 2 \u2261 b \u207b\u00b9 \u2219 b \u207b\u00b9\n ^\u207b2-\u2219 = ! ^\u207b\u2032-spec 2 \u2666 ^\u207b\u20322-\u2219\n\n -- ^-+ is a group homomorphism defined in Algebra.Group\n -- Some properties can be derived from it.\n ^-+ : \u2200 i j \u2192 b ^(i +\u2124 j) \u2261 b ^ i \u2219 b ^ j\n ^-+ -[1+ m ] -[1+ n ] = ap _\u207b\u00b9\n (ap (\u03bb z \u2192 b ^\u207a(1+ z)) (\u2115\u00b0.+-comm (1+ m) n)\n \u2666 ^\u207a-+ (1+ n) {1+ m}) \u2666 \u207b\u00b9-hom\u2032\n ^-+ -[1+ m ] (+ n) = ^-\u2296' n (1+ m)\n ^-+ (+ m) -[1+ n ] = ^-\u2296 m (1+ n)\n ^-+ (+ m) (+ n) = ^\u207a-+ m\n\n -- GroupHomomorphism.f-pres-inv\n ^-- : \u2200 i \u2192 b ^(- i) \u2261 (b ^ i)\u207b\u00b9\n ^-- -[1+ n ] = ! \u207b\u00b9-involutive\n ^-- (+ 0) = ! \u03b5\u207b\u00b9\u2261\u03b5\n ^-- (+ 1+ n) = idp\n\n -- GroupHomomorphism.f-\u2212-\/\n ^-\u2212 : \u2200 i j \u2192 b ^(i \u2212\u2124 j) \u2261 b ^ i \/ b ^ j\n ^-\u2212 i j = ^-+ i (- j) \u2666 \u2219= idp (^-- j)\n\n ^-suc : \u2200 i \u2192 b ^(suc\u2124 i) \u2261 b \u2219 b ^ i\n ^-suc i = ^-+ (+ 1) i \u2666 \u2219= idr idp\n\n ^-pred : \u2200 i \u2192 b ^(pred\u2124 i) \u2261 b \u207b\u00b9 \u2219 b ^ i\n ^-pred i = ^-+ (- + 1) i \u2666 ap (\u03bb z \u2192 z \u207b\u00b9 \u2219 b ^ i) idr\n\n ^-comm : \u2200 i j \u2192 b ^ i \u2219 b ^ j \u2261 b ^ j \u2219 b ^ i\n ^-comm -[1+ m ] -[1+ n ] = ^\u207b-comm (1+ m) (1+ n)\n ^-comm -[1+ m ] (+ n) = ! ^\u207a-^\u207b-comm n (1+ m)\n ^-comm (+ m) -[1+ n ] = ^\u207a-^\u207b-comm m (1+ n)\n ^-comm (+ m) (+ n) = ^\u207a-comm m n\n\n {-\n ^-* : \u2200 i j \u2192 b ^(i *\u2124 j) \u2261 (b ^ j)^ i\n ^-* i j = {!!}\n -}\n\n module From-Assoc-Identities-Inverse\n (assoc : Associative _\u2219_)\n (identities : Identity \u03b5 _\u2219_)\n (inv : Inverse \u03b5 _\u207b\u00b9 _\u2219_)\n where\n open From-Assoc-Identities-RightInverse assoc identities (snd inv) public\n\n module From-Ring-Ops (rng-ops : Ring-Ops A) where\n open Ring-Ops rng-ops\n\n module DistributesOver\u02b3\n (*-comm : Commutative _*_)\n (*-+-distr\u02e1 : _*_ DistributesOver\u02e1 _+_)\n where\n *-+-distr\u02b3 : _*_ DistributesOver\u02b3 _+_\n *-+-distr\u02b3 = *-comm \u2666 *-+-distr\u02e1 \u2666 += *-comm *-comm\n\n module DistributesOver\u02e1\n (*-comm : Commutative _*_)\n (*-+-distr\u02b3 : _*_ DistributesOver\u02b3 _+_)\n where\n *-+-distr\u02e1 : _*_ DistributesOver\u02e1 _+_\n *-+-distr\u02e1 = *-comm \u2666 *-+-distr\u02b3 \u2666 += *-comm *-comm\n\n module From-+Group-*Identity-DistributesOver\n (+-assoc : Associative _+_)\n (0+-identity : LeftIdentity 0# _+_)\n (+0-identity : RightIdentity 0# _+_)\n (0\u2212-inverse\u02b3 : RightInverse 0# 0\u2212_ _+_)\n (1*-identity : LeftIdentity 1# _*_)\n (*1-identity : RightIdentity 1# _*_)\n (*-+-distrs : _*_ DistributesOver _+_)\n where\n open From-Group-Ops.From-Assoc-Identities-RightInverse\n +-grp-ops\n +-assoc (0+-identity , +0-identity) 0\u2212-inverse\u02b3\n renaming ( \u207b\u00b9-involutive to 0\u2212-involutive\n ; cancels-\u2219-left to cancels-+-left\n ; cancels-\u2219-right to cancels-+-right\n ; inverse\u02e1 to 0\u2212-inverse\u02e1\n ; \u03b5\u207b\u00b9\u2261\u03b5 to 0\u22120\u22610\n ; \u207b\u00b9-hom\u2032 to 0\u2212-hom\u2032\n )\n\n *-+-distr\u02e1 : _*_ DistributesOver\u02e1 _+_\n *-+-distr\u02e1 = fst *-+-distrs\n\n *-+-distr\u02b3 : _*_ DistributesOver\u02b3 _+_\n *-+-distr\u02b3 = snd *-+-distrs\n\n -0\u22610 : -0# \u2261 0#\n -0\u22610 = 0\u22120\u22610\n\n 0*-zero : LeftZero 0# _*_\n 0*-zero {x} = cancels-+-left\n (x + 0# * x \u2261\u27e8 += (! 1*-identity) idp \u27e9\n 1# * x + 0# * x \u2261\u27e8 ! *-+-distr\u02b3 \u27e9\n (1# + 0#) * x \u2261\u27e8 *= +0-identity idp \u27e9\n 1# * x \u2261\u27e8 1*-identity \u27e9\n x \u2261\u27e8 ! +0-identity \u27e9\n x + 0# \u220e)\n\n *0-zero : RightZero 0# _*_\n *0-zero {x} = cancels-+-right\n (x * 0# + x \u2261\u27e8 += idp (! *1-identity) \u27e9\n x * 0# + x * 1# \u2261\u27e8 ! *-+-distr\u02e1 \u27e9\n x * (0# + 1#) \u2261\u27e8 *= idp 0+-identity \u27e9\n x * 1# \u2261\u27e8 *1-identity \u27e9\n x \u2261\u27e8 ! 0+-identity \u27e9\n 0# + x \u220e)\n\n 2*-spec : \u2200 {n} \u2192 2* n \u2261 2# * n\n 2*-spec = ! += 1*-identity 1*-identity \u2666 ! *-+-distr\u02b3\n\n 0\u2212c+c+x : \u2200 {c x} \u2192 0\u2212 c + c + x \u2261 x\n 0\u2212c+c+x = += 0\u2212-inverse\u02e1 idp \u2666 0+-identity\n\n 0\u2212-*-distr : \u2200 {x y} \u2192 0\u2212(x * y) \u2261 (0\u2212 x) * y\n 0\u2212-*-distr = cancels-+-right (0\u2212-inverse\u02e1 \u2666 ! 0*-zero \u2666 *= (! 0\u2212-inverse\u02e1) idp \u2666 *-+-distr\u02b3)\n\n 0\u2212-*-distr\u02b3 : \u2200 {x y} \u2192 0\u2212(x * y) \u2261 x * (0\u2212 y)\n 0\u2212-*-distr\u02b3 = cancels-+-right (0\u2212-inverse\u02e1 \u2666 (! *0-zero \u2666 *= idp (! 0\u2212-inverse\u02e1)) \u2666 *-+-distr\u02e1)\n\n -1*-neg : \u2200 {x} \u2192 -1# * x \u2261 0\u2212 x\n -1*-neg = ! 0\u2212-*-distr \u2666 0\u2212= 1*-identity\n\n *-\u2212-distr : \u2200 {x y z} \u2192 x * (y \u2212 z) \u2261 x * y \u2212 x * z\n *-\u2212-distr = *-+-distr\u02e1 \u2666 += idp (! 0\u2212-*-distr\u02b3)\n\n \u00b2-0\u2212-distr : \u2200 {x} \u2192 (0\u2212 x)\u00b2 \u2261 x \u00b2\n \u00b2-0\u2212-distr = ! 0\u2212-*-distr \u2666 0\u2212=(! 0\u2212-*-distr\u02b3) \u2666 0\u2212-involutive\n\n 2*-*-distr : \u2200 {x y} \u2192 2*(x * y) \u2261 2* x * y\n 2*-*-distr = ! *-+-distr\u02b3\n\n +-comm : Commutative _+_\n +-comm {a} {b} =\n cancels-+-right\n (cancels-+-left\n (a + (a + b + b) \u2261\u27e8 += idp +-assoc \u2666 ! +-assoc \u27e9\n (a + a) + (b + b) \u2261\u27e8 += 2*-spec 2*-spec \u27e9\n 2# * a + 2# * b \u2261\u27e8 ! *-+-distr\u02e1 \u27e9\n 2# * (a + b) \u2261\u27e8 ! 2*-spec \u27e9\n (a + b) + (a + b) \u2261\u27e8 +-assoc \u2666 += idp (! +-assoc) \u27e9\n a + (b + a + b) \u220e))\n\n 0\u2212-+-distr\u2032 : \u2200 {x y} \u2192 0\u2212(x + y) \u2261 0\u2212 y \u2212 x\n 0\u2212-+-distr\u2032 = 0\u2212-hom\u2032\n\n 0\u2212-+-distr : \u2200 {x y} \u2192 0\u2212(x + y) \u2261 0\u2212 x \u2212 y\n 0\u2212-+-distr = 0\u2212= +-comm \u2666 0\u2212-+-distr\u2032\n\n module From-Field-Ops (fld-ops : Field-Ops A) where\n open Field-Ops fld-ops\n\nmodule Explicits where\n open Algebra.FunctionProperties.NP \u03a0 {a}{a}{A} _\u2261_ public\n\n -- REPEATED from above but with explicit arguments\n module FromOp\u2082\n (_\u2219_ : Op\u2082 A){x x' y y'}(p : x \u2261 x')(q : y \u2261 y')\n where\n op= : x \u2219 y \u2261 x' \u2219 y'\n op= = ap (_\u2219_ x) q \u2666 ap (\u03bb z \u2192 z \u2219 y') p\n\n module FromComm\n (_\u2219_ : Op\u2082 A)\n (comm : Commutative _\u2219_)\n (x y x' y' : A)\n (e : (y \u2219 x) \u2261 (y' \u2219 x'))\n where\n open FromOp\u2082 _\u2219_\n\n comm= : x \u2219 y \u2261 x' \u2219 y'\n comm= = comm _ _ \u2666 e \u2666 comm _ _\n\n module FromAssoc\n (_\u2219_ : Op\u2082 A)\n (assoc : Associative _\u2219_)\n\n where\n open FromOp\u2082 _\u2219_\n\n assocs : Associative _\u2219_ \u00d7 Associative (flip _\u2219_)\n assocs = assoc , (\u03bb _ _ _ \u2192 ! assoc _ _ _)\n\n module _ {c x y x' y' : A}\n (e : (x \u2219 y) \u2261 (x' \u2219 y')) where\n assoc= : x \u2219 (y \u2219 c) \u2261 x' \u2219 (y' \u2219 c)\n assoc= = ! assoc _ _ _ \u2666 op= e idp \u2666 assoc _ _ _\n\n !assoc= : (c \u2219 x) \u2219 y \u2261 (c \u2219 x') \u2219 y'\n !assoc= = assoc _ _ _ \u2666 op= idp e \u2666 ! assoc _ _ _\n\n module _ {c d x y x' y' : A}\n (e : (x \u2219 y) \u2261 (x' \u2219 y')) where\n inner= : (c \u2219 x) \u2219 (y \u2219 d) \u2261 (c \u2219 x') \u2219 (y' \u2219 d)\n inner= = assoc= (!assoc= e)\n\n module FromAssocComm\n (_\u2219_ : Op\u2082 A)\n (assoc : Associative _\u2219_)\n (comm : Commutative _\u2219_)\n where\n open FromOp\u2082 _\u2219_ renaming (op= to \u2219=)\n open FromAssoc _\u2219_ assoc public\n open FromComm _\u2219_ comm public\n\n module _ x y z where\n assoc-comm : x \u2219 (y \u2219 z) \u2261 y \u2219 (x \u2219 z)\n assoc-comm = assoc= (comm _ _)\n\n !assoc-comm : (x \u2219 y) \u2219 z \u2261 (x \u2219 z) \u2219 y\n !assoc-comm = !assoc= (comm _ _)\n\n interchange : Interchange _\u2219_ _\u2219_\n interchange _ _ _ _ = assoc= (!assoc= (comm _ _))\n\n module _ {c d x y x' y' : A}\n (e : (x \u2219 y) \u2261 (x' \u2219 y')) where\n outer= : (x \u2219 c) \u2219 (d \u2219 y) \u2261 (x' \u2219 c) \u2219 (d \u2219 y')\n outer= = \u2219= (comm _ _) (comm _ _) \u2666 assoc= (!assoc= e) \u2666 \u2219= (comm _ _) (comm _ _)\n\n module _ {b}{B : Set b} where\n open Morphisms {B = B} _\u2261_ public\n\n module _ {f : A \u2192 B} where\n Injective-\u00acConflict : Injective f \u2192 \u00ac(Conflict f)\n Injective-\u00acConflict inj (x , y , x\u2262y , fx\u2261fy) = x\u2262y (inj _ _ fx\u2261fy)\n\n Conflict-\u00acInjective : Conflict f \u2192 \u00ac(Injective f)\n Conflict-\u00acInjective = flip Injective-\u00acConflict\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"a46763a6919ff3ff8682f52edc906ea64703f1cc","subject":"Simplify","message":"Simplify\n","repos":"inc-lc\/ilc-agda","old_file":"Structure\/Bag\/Nehemiah.agda","new_file":"Structure\/Bag\/Nehemiah.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Bags of integers, for Nehemiah plugin.\n--\n-- This module imports postulates about bags of integers\n-- with negative multiplicities as a group under additive union.\n------------------------------------------------------------------------\n\nmodule Structure.Bag.Nehemiah where\n\nopen import Postulate.Bag-Nehemiah public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Algebra using (CommutativeRing)\nopen import Algebra.Structures\nopen import Data.Integer\nopen import Data.Integer.Properties\n using ()\n renaming (commutativeRing to \u2124-is-commutativeRing)\nopen import Data.Product\n\ninfixl 9 _\\\\_ -- same as Data.Map.(\\\\)\n_\\\\_ : Bag \u2192 Bag \u2192 Bag\nd \\\\ b = d ++ (negateBag b)\n\n-- Useful properties of abelian groups\ncommutative : \u2200 {A : Set} {f : A \u2192 A \u2192 A} {z neg} \u2192\n IsAbelianGroup _\u2261_ f z neg \u2192 (m n : A) \u2192 f m n \u2261 f n m\ncommutative = IsAbelianGroup.comm\n\nassociative : \u2200 {A : Set} {f : A \u2192 A \u2192 A} {z neg} \u2192\n IsAbelianGroup _\u2261_ f z neg \u2192 (k m n : A) \u2192 f (f k m) n \u2261 f k (f m n)\nassociative abelian = IsAbelianGroup.assoc abelian\n\nleft-inverse : \u2200 {A : Set} {f : A \u2192 A \u2192 A} {z neg} \u2192\n IsAbelianGroup _\u2261_ f z neg \u2192 (n : A) \u2192 f (neg n) n \u2261 z\nleft-inverse abelian = proj\u2081 (IsAbelianGroup.inverse abelian)\nright-inverse : \u2200 {A : Set} {f : A \u2192 A \u2192 A} {z neg} \u2192\n IsAbelianGroup _\u2261_ f z neg \u2192 (n : A) \u2192 f n (neg n) \u2261 z\nright-inverse abelian = proj\u2082 (IsAbelianGroup.inverse abelian)\n\nleft-identity : \u2200 {A : Set} {f : A \u2192 A \u2192 A} {z neg} \u2192\n IsAbelianGroup _\u2261_ f z neg \u2192 (n : A) \u2192 f z n \u2261 n\nleft-identity abelian = proj\u2081 (IsMonoid.identity\n (IsGroup.isMonoid (IsAbelianGroup.isGroup abelian)))\nright-identity : \u2200 {A : Set} {f : A \u2192 A \u2192 A} {z neg} \u2192\n IsAbelianGroup _\u2261_ f z neg \u2192 (n : A) \u2192 f n z \u2261 n\nright-identity abelian = proj\u2082 (IsMonoid.identity\n (IsGroup.isMonoid (IsAbelianGroup.isGroup abelian)))\n\ninstance\n abelian-int : IsAbelianGroup _\u2261_ _+_ (+ 0) (-_)\n abelian-int =\n IsRing.+-isAbelianGroup\n (IsCommutativeRing.isRing\n (CommutativeRing.isCommutativeRing\n \u2124-is-commutativeRing))\ncommutative-int : (m n : \u2124) \u2192 m + n \u2261 n + m\ncommutative-int = commutative abelian-int\nassociative-int : (k m n : \u2124) \u2192 (k + m) + n \u2261 k + (m + n)\nassociative-int = associative abelian-int\nright-inv-int : (n : \u2124) \u2192 n - n \u2261 + 0\nright-inv-int = right-inverse abelian-int\nleft-id-int : (n : \u2124) \u2192 (+ 0) + n \u2261 n\nleft-id-int = left-identity abelian-int\nright-id-int : (n : \u2124) \u2192 n + (+ 0) \u2261 n\nright-id-int = right-identity abelian-int\n\ncommutative-bag : (a b : Bag) \u2192 a ++ b \u2261 b ++ a\ncommutative-bag = commutative abelian-bag\nassociative-bag : (a b c : Bag) \u2192 (a ++ b) ++ c \u2261 a ++ (b ++ c)\nassociative-bag = associative abelian-bag\nright-inv-bag : (b : Bag) \u2192 b \\\\ b \u2261 emptyBag\nright-inv-bag = right-inverse abelian-bag\nleft-id-bag : (b : Bag) \u2192 emptyBag ++ b \u2261 b\nleft-id-bag = left-identity abelian-bag\nright-id-bag : (b : Bag) \u2192 b ++ emptyBag \u2261 b\nright-id-bag = right-identity abelian-bag\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Bags of integers, for Nehemiah plugin.\n--\n-- This module imports postulates about bags of integers\n-- with negative multiplicities as a group under additive union.\n------------------------------------------------------------------------\n\nmodule Structure.Bag.Nehemiah where\n\nopen import Postulate.Bag-Nehemiah public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Algebra using (CommutativeRing)\nopen import Algebra.Structures\nopen import Data.Integer\nimport Data.Integer.Properties\n renaming (commutativeRing to \u2124-is-commutativeRing)\nopen Data.Integer.Properties using (\u2124-is-commutativeRing)\nopen import Data.Product\n\ninfixl 9 _\\\\_ -- same as Data.Map.(\\\\)\n_\\\\_ : Bag \u2192 Bag \u2192 Bag\nd \\\\ b = d ++ (negateBag b)\n\n-- Useful properties of abelian groups\ncommutative : \u2200 {A : Set} {f : A \u2192 A \u2192 A} {z neg} \u2192\n IsAbelianGroup _\u2261_ f z neg \u2192 (m n : A) \u2192 f m n \u2261 f n m\ncommutative = IsAbelianGroup.comm\n\nassociative : \u2200 {A : Set} {f : A \u2192 A \u2192 A} {z neg} \u2192\n IsAbelianGroup _\u2261_ f z neg \u2192 (k m n : A) \u2192 f (f k m) n \u2261 f k (f m n)\nassociative abelian = IsSemigroup.assoc (IsMonoid.isSemigroup\n (IsGroup.isMonoid (IsAbelianGroup.isGroup abelian)))\n\nleft-inverse : \u2200 {A : Set} {f : A \u2192 A \u2192 A} {z neg} \u2192\n IsAbelianGroup _\u2261_ f z neg \u2192 (n : A) \u2192 f (neg n) n \u2261 z\nleft-inverse abelian = proj\u2081\n (IsGroup.inverse (IsAbelianGroup.isGroup abelian))\nright-inverse : \u2200 {A : Set} {f : A \u2192 A \u2192 A} {z neg} \u2192\n IsAbelianGroup _\u2261_ f z neg \u2192 (n : A) \u2192 f n (neg n) \u2261 z\nright-inverse abelian = proj\u2082\n (IsGroup.inverse (IsAbelianGroup.isGroup abelian))\n\nleft-identity : \u2200 {A : Set} {f : A \u2192 A \u2192 A} {z neg} \u2192\n IsAbelianGroup _\u2261_ f z neg \u2192 (n : A) \u2192 f z n \u2261 n\nleft-identity abelian = proj\u2081 (IsMonoid.identity\n (IsGroup.isMonoid (IsAbelianGroup.isGroup abelian)))\nright-identity : \u2200 {A : Set} {f : A \u2192 A \u2192 A} {z neg} \u2192\n IsAbelianGroup _\u2261_ f z neg \u2192 (n : A) \u2192 f n z \u2261 n\nright-identity abelian = proj\u2082 (IsMonoid.identity\n (IsGroup.isMonoid (IsAbelianGroup.isGroup abelian)))\n\ninstance\n abelian-int : IsAbelianGroup _\u2261_ _+_ (+ 0) (-_)\n abelian-int =\n IsRing.+-isAbelianGroup\n (IsCommutativeRing.isRing\n (CommutativeRing.isCommutativeRing\n \u2124-is-commutativeRing))\ncommutative-int : (m n : \u2124) \u2192 m + n \u2261 n + m\ncommutative-int = commutative abelian-int\nassociative-int : (k m n : \u2124) \u2192 (k + m) + n \u2261 k + (m + n)\nassociative-int = associative abelian-int\nright-inv-int : (n : \u2124) \u2192 n - n \u2261 + 0\nright-inv-int = right-inverse abelian-int\nleft-id-int : (n : \u2124) \u2192 (+ 0) + n \u2261 n\nleft-id-int = left-identity abelian-int\nright-id-int : (n : \u2124) \u2192 n + (+ 0) \u2261 n\nright-id-int = right-identity abelian-int\n\ncommutative-bag : (a b : Bag) \u2192 a ++ b \u2261 b ++ a\ncommutative-bag = commutative abelian-bag\nassociative-bag : (a b c : Bag) \u2192 (a ++ b) ++ c \u2261 a ++ (b ++ c)\nassociative-bag = associative abelian-bag\nright-inv-bag : (b : Bag) \u2192 b \\\\ b \u2261 emptyBag\nright-inv-bag = right-inverse abelian-bag\nleft-id-bag : (b : Bag) \u2192 emptyBag ++ b \u2261 b\nleft-id-bag = left-identity abelian-bag\nright-id-bag : (b : Bag) \u2192 b ++ emptyBag \u2261 b\nright-id-bag = right-identity abelian-bag\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c4ea6ed65b7b21e33e219c3d918df18fa826e7fa","subject":"scraps","message":"scraps\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"preservation.agda","new_file":"preservation.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nopen import htype-decidable\nopen import lemmas-consistency\nopen import type-assignment-unicity\n\nmodule preservation where\n -- todo: rename\n pres-lem : \u2200{ \u0394 \u0393 d \u03b5 d1 d2 d3 \u03c4 \u03c41 } \u2192\n d == \u03b5 \u27e6 d1 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n d3 == \u03b5 \u27e6 d2 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d3 :: \u03c4\n pres-lem FHOuter D1 D2 D3 FHOuter\n with type-assignment-unicity D1 D2\n ... | refl = D3\n pres-lem (FHAp1 eps) (TAAp D1 D2) D3 D4 (FHAp1 D5) = TAAp (pres-lem eps D1 D3 D4 D5) D2\n pres-lem (FHAp2 eps) (TAAp D1 D2) D3 D4 (FHAp2 D5) = TAAp D1 (pres-lem eps D2 D3 D4 D5)\n pres-lem (FHNEHole eps) (TANEHole x D1 x\u2081) D2 D3 (FHNEHole D4) = TANEHole x (pres-lem eps D1 D2 D3 D4) x\u2081\n pres-lem (FHCast eps) (TACast D1 x) D2 D3 (FHCast D4) = TACast (pres-lem eps D1 D2 D3 D4) x\n pres-lem (FHFailedCast x) (TAFailedCast y x\u2081 x\u2082 x\u2083) D3 D4 (FHFailedCast eps) = TAFailedCast (pres-lem x y D3 D4 eps) x\u2081 x\u2082 x\u2083\n\n -- todo: rename\n pres-lem2 : \u2200{ \u03b5 \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u03a3[ \u03c4' \u2208 htyp ] (\u0394 , \u0393 \u22a2 d' :: \u03c4')\n pres-lem2 TAConst FHOuter = _ , TAConst\n pres-lem2 (TAVar x\u2081) FHOuter = _ , TAVar x\u2081\n pres-lem2 (TALam ta) FHOuter = _ , TALam ta\n\n pres-lem2 (TAAp ta ta\u2081) FHOuter = _ , TAAp ta ta\u2081\n pres-lem2 (TAAp ta ta\u2081) (FHAp1 eps) = pres-lem2 ta eps\n pres-lem2 (TAAp ta ta\u2081) (FHAp2 eps) = pres-lem2 ta\u2081 eps\n\n pres-lem2 (TAEHole x x\u2081) FHOuter = _ , TAEHole x x\u2081\n pres-lem2 (TANEHole x ta x\u2081) FHOuter = _ , TANEHole x ta x\u2081\n pres-lem2 (TANEHole x ta x\u2081) (FHNEHole eps) = pres-lem2 ta eps\n pres-lem2 (TACast ta x) FHOuter = _ , TACast ta x\n pres-lem2 (TACast ta x) (FHCast eps) = pres-lem2 ta eps\n pres-lem2 (TAFailedCast x y z w) FHOuter = _ , TAFailedCast x y z w\n pres-lem2 (TAFailedCast x x\u2081 x\u2082 x\u2083) (FHFailedCast y) = pres-lem2 x y\n\n -- this is the literal contents of the hole in lem3; it might not go\n -- through exactly like this.\n lem-subst : \u2200{\u0394 \u0393 x \u03c41 d1 \u03c4 d2 } \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 d1 :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 [ d2 \/ x ] d1 :: \u03c4\n lem-subst TAConst D2 = TAConst\n lem-subst {\u0393 = \u0393} {x = x'} (TAVar {x = x} x\u2082) D2\n with \u0393 x\n lem-subst {x = x} (TAVar {x = x'} x\u2083) D2 | Some x\u2081\n with natEQ x' x\n lem-subst (TAVar xin) D2 | Some x\u2083 | Inl refl = {!!}\n lem-subst (TAVar refl) D2 | Some x\u2083 | Inr x\u2082 = {!!}\n lem-subst {x = x} (TAVar {x = x'} x\u2082) D2 | None with natEQ x' x\n lem-subst {x = x} (TAVar x\u2083) D2 | None | Inl refl with natEQ x x\n lem-subst (TAVar refl) D2 | None | Inl refl | Inl refl = D2\n lem-subst (TAVar x\u2083) D2 | None | Inl refl | Inr x\u2081 = abort (somenotnone (! x\u2083))\n lem-subst {x = x} (TAVar {x = x'} x\u2083) D2 | None | Inr x\u2082 with natEQ x x'\n lem-subst (TAVar x\u2084) D2 | None | Inr x\u2083 | Inl x\u2082 = abort ((flip x\u2083) x\u2082)\n lem-subst (TAVar x\u2084) D2 | None | Inr x\u2083 | Inr x\u2082 = abort (somenotnone (! x\u2084))\n lem-subst {\u0393 = \u0393} {x = x} (TALam {x = x'} D1) D2 = {!!}\n lem-subst (TAAp D1 D2) D3 = TAAp (lem-subst D1 D3) (lem-subst D2 D3)\n lem-subst (TAEHole x\u2081 x\u2082) D2 = TAEHole x\u2081 {!!}\n lem-subst (TANEHole x\u2081 D1 x\u2082) D2 = TANEHole x\u2081 (lem-subst D1 D2) {!!}\n lem-subst (TACast D1 x\u2081) D2 = TACast (lem-subst D1 D2) x\u2081\n lem-subst (TAFailedCast D1 x\u2081 x\u2082 x\u2083) D2 = TAFailedCast (lem-subst D1 D2) x\u2081 x\u2082 x\u2083\n\n -- todo: rename\n pres-lem3 : \u2200{ \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u2192> d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n pres-lem3 TAConst ()\n pres-lem3 (TAVar x\u2081) ()\n pres-lem3 (TALam ta) ()\n pres-lem3 (TAAp (TALam ta) ta\u2081) ITLam = lem-subst ta ta\u2081\n pres-lem3 (TAAp (TACast ta TCRefl) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 TCRefl)) TCRefl\n pres-lem3 (TAAp (TACast ta (TCArr x x\u2081)) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 (~sym x))) x\u2081\n pres-lem3 (TAEHole x x\u2081) ()\n pres-lem3 (TANEHole x ta x\u2081) ()\n pres-lem3 (TACast ta x) (ITCastID) = ta\n pres-lem3 (TACast (TACast ta x) x\u2081) (ITCastSucceed x\u2082) = ta\n pres-lem3 (TACast ta x) (ITGround (MGArr x\u2081)) = TACast (TACast ta (TCArr TCHole1 TCHole1)) TCHole1\n pres-lem3 (TACast ta TCHole2) (ITExpand (MGArr x\u2081)) = TACast (TACast ta TCHole2) (TCArr TCHole2 TCHole2)\n pres-lem3 (TACast (TACast ta x) x\u2081) (ITCastFail w y z) = TAFailedCast ta w y z\n pres-lem3 (TAFailedCast x y z q) ()\n\n preservation : {\u0394 : hctx} {d d' : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d \u21a6 d' \u2192\n \u0394 , \u2205 \u22a2 d' :: \u03c4\n preservation D (Step x x\u2081 x\u2082)\n with pres-lem2 D x\n ... | (_ , wt) = pres-lem x D wt (pres-lem3 wt x\u2081) x\u2082\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nopen import htype-decidable\nopen import lemmas-consistency\nopen import type-assignment-unicity\n\nmodule preservation where\n -- todo: rename\n pres-lem : \u2200{ \u0394 \u0393 d \u03b5 d1 d2 d3 \u03c4 \u03c41 } \u2192\n d == \u03b5 \u27e6 d1 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n d3 == \u03b5 \u27e6 d2 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d3 :: \u03c4\n pres-lem FHOuter D1 D2 D3 FHOuter\n with type-assignment-unicity D1 D2\n ... | refl = D3\n pres-lem (FHAp1 eps) (TAAp D1 D2) D3 D4 (FHAp1 D5) = TAAp (pres-lem eps D1 D3 D4 D5) D2\n pres-lem (FHAp2 eps) (TAAp D1 D2) D3 D4 (FHAp2 D5) = TAAp D1 (pres-lem eps D2 D3 D4 D5)\n pres-lem (FHNEHole eps) (TANEHole x D1 x\u2081) D2 D3 (FHNEHole D4) = TANEHole x (pres-lem eps D1 D2 D3 D4) x\u2081\n pres-lem (FHCast eps) (TACast D1 x) D2 D3 (FHCast D4) = TACast (pres-lem eps D1 D2 D3 D4) x\n pres-lem (FHFailedCast x) (TAFailedCast y x\u2081 x\u2082 x\u2083) D3 D4 (FHFailedCast eps) = TAFailedCast (pres-lem x y D3 D4 eps) x\u2081 x\u2082 x\u2083\n\n -- todo: rename\n pres-lem2 : \u2200{ \u03b5 \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u03a3[ \u03c4' \u2208 htyp ] (\u0394 , \u0393 \u22a2 d' :: \u03c4')\n pres-lem2 TAConst FHOuter = _ , TAConst\n pres-lem2 (TAVar x\u2081) FHOuter = _ , TAVar x\u2081\n pres-lem2 (TALam ta) FHOuter = _ , TALam ta\n\n pres-lem2 (TAAp ta ta\u2081) FHOuter = _ , TAAp ta ta\u2081\n pres-lem2 (TAAp ta ta\u2081) (FHAp1 eps) = pres-lem2 ta eps\n pres-lem2 (TAAp ta ta\u2081) (FHAp2 eps) = pres-lem2 ta\u2081 eps\n\n pres-lem2 (TAEHole x x\u2081) FHOuter = _ , TAEHole x x\u2081\n pres-lem2 (TANEHole x ta x\u2081) FHOuter = _ , TANEHole x ta x\u2081\n pres-lem2 (TANEHole x ta x\u2081) (FHNEHole eps) = pres-lem2 ta eps\n pres-lem2 (TACast ta x) FHOuter = _ , TACast ta x\n pres-lem2 (TACast ta x) (FHCast eps) = pres-lem2 ta eps\n pres-lem2 (TAFailedCast x y z w) FHOuter = _ , TAFailedCast x y z w\n pres-lem2 (TAFailedCast x x\u2081 x\u2082 x\u2083) (FHFailedCast y) = pres-lem2 x y\n\n -- this is the literal contents of the hole in lem3; it might not go\n -- through exactly like this.\n lem-subst : \u2200{\u0394 \u0393 x \u03c41 d1 \u03c4 d2 } \u2192\n \u0394 , \u0393 ,, (x , \u03c41) \u22a2 d1 :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 [ d2 \/ x ] d1 :: \u03c4\n lem-subst TAConst D2 = TAConst\n lem-subst {\u0393 = \u0393} {x = x'} (TAVar {x = x} x\u2082) D2\n with \u0393 x\n lem-subst {x = x} (TAVar {x = x'} x\u2083) D2 | Some x\u2081\n with natEQ x' x\n lem-subst (TAVar xin) D2 | Some x\u2083 | Inl refl = {!!}\n lem-subst (TAVar refl) D2 | Some x\u2083 | Inr x\u2082 = {!!}\n lem-subst {x = x} (TAVar {x = x'} x\u2082) D2 | None with natEQ x' x\n lem-subst {x = x} (TAVar x\u2083) D2 | None | Inl refl with natEQ x x\n lem-subst (TAVar refl) D2 | None | Inl refl | Inl refl = D2\n lem-subst (TAVar x\u2083) D2 | None | Inl refl | Inr x\u2081 = abort (somenotnone (! x\u2083))\n lem-subst {x = x} (TAVar {x = x'} x\u2083) D2 | None | Inr x\u2082 with natEQ x x'\n lem-subst (TAVar x\u2084) D2 | None | Inr x\u2083 | Inl x\u2082 = abort ((flip x\u2083) x\u2082)\n lem-subst (TAVar x\u2084) D2 | None | Inr x\u2083 | Inr x\u2082 = abort (somenotnone (! x\u2084))\n lem-subst (TALam D1) D2 = {!!}\n lem-subst (TAAp D1 D2) D3 = TAAp (lem-subst D1 D3) (lem-subst D2 D3)\n lem-subst (TAEHole x\u2081 x\u2082) D2 = TAEHole x\u2081 {!!}\n lem-subst (TANEHole x\u2081 D1 x\u2082) D2 = TANEHole x\u2081 (lem-subst D1 D2) {!!}\n lem-subst (TACast D1 x\u2081) D2 = TACast (lem-subst D1 D2) x\u2081\n lem-subst (TAFailedCast D1 x\u2081 x\u2082 x\u2083) D2 = TAFailedCast (lem-subst D1 D2) x\u2081 x\u2082 x\u2083\n\n -- todo: rename\n pres-lem3 : \u2200{ \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u2192> d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n pres-lem3 TAConst ()\n pres-lem3 (TAVar x\u2081) ()\n pres-lem3 (TALam ta) ()\n pres-lem3 (TAAp (TALam ta) ta\u2081) ITLam = lem-subst ta ta\u2081\n pres-lem3 (TAAp (TACast ta TCRefl) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 TCRefl)) TCRefl\n pres-lem3 (TAAp (TACast ta (TCArr x x\u2081)) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 (~sym x))) x\u2081\n pres-lem3 (TAEHole x x\u2081) ()\n pres-lem3 (TANEHole x ta x\u2081) ()\n pres-lem3 (TACast ta x) (ITCastID) = ta\n pres-lem3 (TACast (TACast ta x) x\u2081) (ITCastSucceed x\u2082) = ta\n pres-lem3 (TACast ta x) (ITGround (MGArr x\u2081)) = TACast (TACast ta (TCArr TCHole1 TCHole1)) TCHole1\n pres-lem3 (TACast ta TCHole2) (ITExpand (MGArr x\u2081)) = TACast (TACast ta TCHole2) (TCArr TCHole2 TCHole2)\n pres-lem3 (TACast (TACast ta x) x\u2081) (ITCastFail w y z) = TAFailedCast ta w y z\n pres-lem3 (TAFailedCast x y z q) ()\n\n preservation : {\u0394 : hctx} {d d' : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d \u21a6 d' \u2192\n \u0394 , \u2205 \u22a2 d' :: \u03c4\n preservation D (Step x x\u2081 x\u2082)\n with pres-lem2 D x\n ... | (_ , wt) = pres-lem x D wt (pres-lem3 wt x\u2081) x\u2082\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"50fd394a723e3e813b89d10bfaa72167a1206f06","subject":"Continue with functional semantics","message":"Continue with functional semantics\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/FunBigStepSILR2.agda","new_file":"Thesis\/FunBigStepSILR2.agda","new_contents":"{-# OPTIONS --allow-unsolved-metas #-}\n-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\", POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n-- Since we focus for now on STLC, unlike that\n-- paper, we can avoid error values by keeping types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) zero = TimeOut\nevalConst (lit v) (suc n) = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\neval (app s t) \u03c1 (suc n) = (eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)) n\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1)\neval-const-mono (lit v) zero n1 ()\neval-const-mono (lit v) (suc n0) .n0 refl = refl\n\n-- ARGH\n{-# TERMINATING #-}\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 \u2200 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1)\neval-mono (const c) \u03c1 v n0 n1 eq = eval-const-mono c n0 n1 eq\neval-mono (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\neval-mono (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\neval-mono (app s t) \u03c1 v zero n1 ()\neval-mono (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-mono t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n\nrelT : \u2200 {\u03c4 \u03931 \u03932} (t1 : Term \u03931 \u03c4) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\n\nrelV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\nrelV \u03c4 v1 v2 zero = \u22a4\n-- Seems the proof for abs would go through even if here we do not step down.\n-- However, that only works as long as we use a typed language; not stepping\n-- down here, in an untyped language, gives a non-well-founded definition.\nrelV nat v1 v2 (suc n) = v1 \u2261 v2\nrelV (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (suc n) =\n \u2200 (k : \u2115) (k\u2264n : k \u2264 n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT t1 t2 (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) k\n\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\nrelT {\u03c4} t1 t2 \u03c11 \u03c12 zero = \u22a4\nrelT {\u03c4} t1 t2 \u03c11 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono zero n m\u2264n \u03c4 v1 v2 vv = tt\nrelV-mono (suc m) (suc n) (s\u2264s m\u2264n) nat v1 v2 vv = vv\nrelV-mono (suc m) (suc n) (s\u2264s m\u2264n) (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) ff k k\u2264m = ff k (\u2264-trans k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 .n n-j\u2264n refl = v2 , zero , zero , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\n-- relT (app s t) (app s t)\n\nrelV-apply : \u2200 {\u03c3 \u03c4 \u0393} (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) t v1 \u03c12 n-j \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] \u03a3[ n3 \u2208 \u2115 ] eval (app s t) \u03c12 n2 \u2261 Done v2 n3 \u00d7 relV \u03c4 v1 v2 (suc n-j)\nrelV-apply = {!!}\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\nfundamental (const (lit v)) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (const (lit v)) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 .(intV v) .n n-j\u2264n refl = intV v , suc zero , zero , refl , refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(closure t \u03c11) .n n-j\u2264n refl = closure t \u03c12 , n , n , refl , (\u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k (suc n) (\u2264-step k\u2264n) _ _ _ \u03c1\u03c1))\nfundamental (app s t) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq with eval s \u03c11 n | inspect (eval s \u03c11) n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done sv1 n1 | [ s1eq ] with eval t \u03c11 n1 | inspect (eval t \u03c11) n1\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done (closure st1 s\u03c11) n1 | [ s1eq ] | Done tv1 n2 | [ t1eq ] with fundamental s _ \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) (suc n1) (s\u2264s {!!}) (eval-mono s \u03c11 (closure st1 s\u03c11) n n1 s1eq) | fundamental t _ \u03c11 \u03c12 \u03c1\u03c1 tv1 (suc n2) {!!} {!eval-mono t \u03c11 tv1 n1 n2 t1eq!}\n... | closure st2 s\u03c12 , sn3 , sn4 , s2eq , svv | tv2 , tn3 , tn4 , t2eq , tvv = {!!}\n--\n-- {!eval s \u03c12 !}\n-- fundamental s (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) ? ?\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | Error | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | TimeOut | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Error | [ s1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | TimeOut | [ s1eq ]\n","old_contents":"{-# OPTIONS --allow-unsolved-metas #-}\n-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\", POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n-- Since we focus for now on STLC, unlike that\n-- paper, we can avoid error values by keeping types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) zero = TimeOut\nevalConst (lit v) (suc n) = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a (suc n) = eval t (a \u2022 \u03c1) n\napply (closure t \u03c1) a zero = TimeOut\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\neval (app s t) \u03c1 = eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3d30a4f4dd594d70f1d1e9a8cd983d40b16d53bf","subject":"Added Colist-coind-stronger-ho.","message":"Added Colist-coind-stronger-ho.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/GreatestFixedPoints\/Colist.agda","new_file":"notes\/fixed-points\/GreatestFixedPoints\/Colist.agda","new_contents":"------------------------------------------------------------------------------\n-- Co-lists\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule GreatestFixedPoints.Colist where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\n\n------------------------------------------------------------------------------\n-- Colist is a greatest fixed-point of a functor\n\n-- The functor.\nColistF : (D \u2192 Set) \u2192 D \u2192 Set\nColistF A xs = xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs')\n\n-- Colist is the greatest fixed-point of ColistF.\npostulate\n Colist : D \u2192 Set\n\n -- Colist is a post-fixed point of ColistF, i.e.\n --\n -- Colist \u2264 ColistF Colist.\n Colist-out-ho : \u2200 {n} \u2192 Colist n \u2192 ColistF Colist n\n\n -- Colist is the greatest post-fixed point of ColistF, i.e.\n --\n -- \u2200 A. A \u2264 ColistF A \u21d2 A \u2264 Colist.\n Colist-coind-ho :\n (A : D \u2192 Set) \u2192\n -- A is post-fixed point of ColistF.\n (\u2200 {xs} \u2192 A xs \u2192 ColistF A xs) \u2192\n -- Colist is greater than A.\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\n\n------------------------------------------------------------------------------\n-- First-order versions\n\nColist-out : \u2200 {xs} \u2192\n Colist xs \u2192\n xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Colist xs')\nColist-out = Colist-out-ho\n\nColist-coind :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192 xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs')) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\nColist-coind = Colist-coind-ho\n\n------------------------------------------------------------------------------\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Colist predicate is also a pre-fixed point of the functional\n-- ColistF, i.e.\n--\n-- ColistF Colist \u2264 Colist.\nColist-in-ho : \u2200 {xs} \u2192 ColistF Colist xs \u2192 Colist xs\nColist-in-ho h = Colist-coind-ho A h' h\n where\n A : D \u2192 Set\n A xs = ColistF Colist xs\n\n h' : \u2200 {xs} \u2192 A xs \u2192 ColistF A xs\n h' (inj\u2081 xs\u22610) = inj\u2081 xs\u22610\n h' (inj\u2082 (x' , xs' , prf , CLxs' )) =\n inj\u2082 (x' , xs' , prf , Colist-out CLxs')\n\n-- The first-order version.\nColist-in : \u2200 {xs} \u2192\n xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Colist xs') \u2192\n Colist xs\nColist-in = Colist-in-ho\n\n------------------------------------------------------------------------------\n-- A stronger co-induction principle\n--\n-- From (Paulson, 1997. p. 16).\n\npostulate\n Colist-coind-stronger-ho :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192 ColistF A xs \u2228 Colist xs) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\n\nColist-coind-ho' :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192 ColistF A xs) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\nColist-coind-ho' A h Axs =\n Colist-coind-stronger-ho A (\u03bb Ays \u2192 inj\u2081 (h Ays)) Axs\n\n-- The first-order version.\nColist-coind-stronger :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192\n (xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs'))\n \u2228 Colist xs) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\nColist-coind-stronger = Colist-coind-stronger-ho\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Paulson, L. C. (1997). Mechanizing Coinduction and Corecursion in\n-- Higher-order Logic. In: Journal of Logic and Computation 7.2,\n-- pp. 175\u2013204.\n","old_contents":"------------------------------------------------------------------------------\n-- Co-lists\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule GreatestFixedPoints.Colist where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\n\n------------------------------------------------------------------------------\n-- Colist is a greatest fixed-point of a functor\n\n-- The functor.\nColistF : (D \u2192 Set) \u2192 D \u2192 Set\nColistF A xs = xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs')\n\n-- Colist is the greatest fixed-point of ColistF.\npostulate\n Colist : D \u2192 Set\n\n -- Colist is a post-fixed point of ColistF, i.e.\n --\n -- Colist \u2264 ColistF Colist.\n Colist-out : \u2200 {xs} \u2192\n Colist xs \u2192\n xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Colist xs')\n\n -- The higher-order version.\n Colist-out-ho : \u2200 {n} \u2192 Colist n \u2192 ColistF Colist n\n\n -- Colist is the greatest post-fixed point of ColistF, i.e.\n --\n -- \u2200 A. A \u2264 ColistF A \u21d2 A \u2264 Colist.\n Colist-coind :\n (A : D \u2192 Set) \u2192\n -- A is post-fixed point of ColistF.\n (\u2200 {xs} \u2192 A xs \u2192 xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs')) \u2192\n -- Colist is greater than A.\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\n\n -- The higher-order version.\n Colist-coind-ho :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192 ColistF A xs) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\n\n------------------------------------------------------------------------------\n-- Colist-out and Colist-out-ho are equivalents\n\nColist-out-fo : \u2200 {xs} \u2192\n Colist xs \u2192\n xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Colist xs')\nColist-out-fo = Colist-out-ho\n\nColist-out-ho' : \u2200 {xs} \u2192 Colist xs \u2192 ColistF Colist xs\nColist-out-ho' = Colist-out\n\n------------------------------------------------------------------------------\n-- Colist-coind and Colist-coind-ho are equivalents\n\nColist-coind-fo :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192 xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs')) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\nColist-coind-fo = Colist-coind\n\nColist-coind-ho' :\n (A : D \u2192 Set) \u2192\n (\u2200 {xs} \u2192 A xs \u2192 ColistF A xs) \u2192\n \u2200 {xs} \u2192 A xs \u2192 Colist xs\nColist-coind-ho' = Colist-coind\n\n------------------------------------------------------------------------------\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Colist predicate is also a pre-fixed point of the functional ColistF,\n-- i.e.\n--\n-- ColistF Colist \u2264 Colist.\nColist-in : \u2200 {xs} \u2192\n xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Colist xs') \u2192\n Colist xs\nColist-in h = Colist-coind A h' h\n where\n A : D \u2192 Set\n A xs = xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Colist xs')\n\n h' : \u2200 {xs} \u2192 A xs \u2192 xs \u2261 [] \u2228 (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 A xs')\n h' (inj\u2081 xs\u22610) = inj\u2081 xs\u22610\n h' (inj\u2082 (x' , xs' , prf , CLxs' )) =\n inj\u2082 (x' , xs' , prf , Colist-out CLxs')\n\n-- The higher-order version.\nColist-in-ho : \u2200 {xs} \u2192 ColistF Colist xs \u2192 Colist xs\nColist-in-ho = Colist-in\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"66d040d178c66458dbc506d2dbd14c35dd762658","subject":"Add 3 more primitives to get a more precise account on space cost","message":"Add 3 more primitives to get a more precise account on space cost\n","repos":"crypto-agda\/crypto-agda","old_file":"flat-funs.agda","new_file":"flat-funs.agda","new_contents":"module flat-funs where\n\nopen import Data.Unit using (\u22a4)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2^_; _\u2294_)\nopen import Data.Fin using (Fin) renaming (_+_ to _+\u1da0_)\nopen import Data.Bool using (if_then_else_)\nimport Data.Vec.NP as V\nimport Level as L\nimport Function as F\nimport Data.Product as \u00d7\nimport Relation.Binary.PropositionalEquality as \u2261\nopen F using (const)\nopen V using (Vec; []; _\u2237_; _++_; [_])\nopen \u00d7 using (_\u00d7_; _,_; proj\u2081; proj\u2082; uncurry)\nopen \u2261 using (_\u2261_)\n\nopen import Data.Bits using (Bit; Bits; _\u2192\u1d47_; 0b; 1b)\n\nopen import universe\n\nrecord FlatFuns {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n universe : Universe T\n _`\u2192_ : T \u2192 T \u2192 Set\n infix 0 _`\u2192_\n open Universe universe public\n\nrecord FlatFunsOps {t} {T : Set t} (\u266dFuns : FlatFuns T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n infixr 1 _\u2218_\n infixr 1 _\u204f_\n infixr 1 _>>>_\n infixr 3 _***_\n infixr 3 _&&&_\n field\n -- Functions\n id : \u2200 {A} \u2192 A `\u2192 A\n _\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)\n\n -- Bit\n <0b> <1b> : \u2200 {_A} \u2192 _A `\u2192 `Bit\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n\n -- Products\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n\n -- Unit\n tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4\n : \u2200 {A} \u2192 A `\u2192 `\u22a4 `\u00d7 A\n\n -- Vectors\n nil : \u2200 {_A B} \u2192 _A `\u2192 `Vec B 0\n cons : \u2200 {n A} \u2192 (A `\u00d7 `Vec A n) `\u2192 `Vec A (1 + n)\n uncons : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 (A `\u00d7 `Vec A n)\n\n _\u204f_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (B `\u2192 C) \u2192 (A `\u2192 C)\n f \u204f g = g \u2218 f\n\n _>>>_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (B `\u2192 C) \u2192 (A `\u2192 C)\n f >>> g = f \u204f g\n\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f , g > = dup \u204f < f \u00d7 g >\n\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n f &&& g = < f , g >\n\n <_\u00d7_>-on-top-of-<,> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g >-on-top-of-<,> = < fst \u204f f , snd \u204f g >\n\n _***_ : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n f *** g = < f \u00d7 g >\n\n swap-on-top-<,> : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n swap-on-top-<,> = < snd , fst >\n\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n first f = < f \u00d7 id >\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = < id \u00d7 f >\n\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork f g = second < f , g > \u204f cond\n\n -- In case you wonder... one can also derive cond with fork\n cond-on-top-of-fork : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n cond-on-top-of-fork = fork fst snd\n\n constBit : \u2200 {_A} \u2192 Bit \u2192 _A `\u2192 `Bit\n constBit b = if b then <1b> else <0b>\n\n <,tt> : \u2200 {A} \u2192 A `\u2192 A `\u00d7 `\u22a4\n <,tt> = \u204f swap\n\n assoc-default : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc-default = < fst \u204f fst , first snd >\n\n assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n assoc\u2032 = < second fst , snd \u204f snd >\n\n <_`zip`_> : \u2200 {A B C D E F} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 ((D `\u00d7 E) `\u2192 F)\n \u2192 ((A `\u00d7 D) `\u00d7 (B `\u00d7 E)) `\u2192 (C `\u00d7 F)\n < f `zip` g > = < < fst \u00d7 fst > \u204f f ,\n < snd \u00d7 snd > \u204f g >\n\n head : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n head = uncons \u204f fst\n\n tail : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n tail = uncons \u204f snd\n\n <_\u2237_> : \u2200 {m n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A m `\u2192 `Vec B n)\n \u2192 `Vec A (1 + m) `\u2192 `Vec B (1 + n)\n < f \u2237 g > = uncons \u204f < f \u00d7 g > \u204f cons\n\n <_\u2237\u2032_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n < f \u2237\u2032 g > = < f , g > \u204f cons\n\n \u229b : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 `Vec A n `\u2192 `Vec B n\n \u229b [] = nil\n \u229b (f \u2237 fs) = < f \u2237 \u229b fs >\n\n \u229b\u2032 : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 A `\u2192 `Vec B n\n \u229b\u2032 [] = nil\n \u229b\u2032 (f \u2237 fs) = < f \u2237\u2032 \u229b\u2032 fs >\n\n constVec : \u2200 {n b _A} {B : Set b} {C} \u2192 (B \u2192 _A `\u2192 C) \u2192 Vec B n \u2192 _A `\u2192 `Vec C n\n constVec f xs = \u229b\u2032 (V.map f xs)\n\n constBits : \u2200 {n _A} \u2192 Bits n \u2192 _A `\u2192 `Bits n\n constBits = constVec constBit\n\n foldl : \u2200 {n A B} \u2192 (B `\u00d7 A `\u2192 B) \u2192 (B `\u00d7 `Vec A n) `\u2192 B\n foldl {zero} f = fst\n foldl {suc n} f = second uncons\n \u204f assoc\u2032\n \u204f first f\n \u204f foldl f\n\n foldl\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldl\u2081 f = uncons \u204f foldl f\n\n foldr : \u2200 {n A B} \u2192 (A `\u00d7 B `\u2192 B) \u2192 (`Vec A n `\u00d7 B) `\u2192 B\n foldr {zero} f = snd\n foldr {suc n} f = first uncons\n \u204f assoc\n \u204f second (foldr f)\n \u204f f\n\n foldr\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldr\u2081 f = uncons \u204f swap \u204f foldr f\n\n map : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A n `\u2192 `Vec B n)\n map f = \u229b (V.replicate f)\n\n zipWith : \u2200 {n A B C} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec C n\n zipWith {zero} f = nil\n zipWith {suc n} f = < uncons \u00d7 uncons >\n \u204f < f `zip` (zipWith f) >\n \u204f cons\n\n zip : \u2200 {n A B} \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec (A `\u00d7 B) n\n zip = zipWith id\n\n <,nil> : \u2200 {A B} \u2192 A `\u2192 A `\u00d7 `Vec B 0\n <,nil> = <,tt> \u204f second nil\n\n singleton : \u2200 {A} \u2192 A `\u2192 `Vec A 1\n singleton = <,nil> \u204f cons\n\n snoc : \u2200 {n A} \u2192 (`Vec A n `\u00d7 A) `\u2192 `Vec A (1 + n)\n snoc {zero} = snd \u204f singleton\n snoc {suc n} = first uncons \u204f assoc \u204f second snoc \u204f cons\n\n reverse : \u2200 {n A} \u2192 `Vec A n `\u2192 `Vec A n\n reverse {zero} = nil\n reverse {suc n} = uncons \u204f swap \u204f first reverse \u204f snoc\n\n append : \u2200 {m n A} \u2192 (`Vec A m `\u00d7 `Vec A n) `\u2192 `Vec A (m + n)\n append {zero} = snd\n append {suc m} = first uncons\n \u204f assoc\n \u204f second append\n \u204f cons\n\n splitAt : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 (`Vec A m `\u00d7 `Vec A n)\n splitAt zero = < nil , id >\n splitAt (suc m) = uncons\n \u204f second (splitAt m)\n \u204f assoc\u2032\n \u204f first cons\n\n take : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A m\n take zero = nil\n take (suc m) = < id \u2237 take m >\n\n drop : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A n\n drop zero = id\n drop (suc m) = tail \u204f drop m\n\n folda : \u2200 n {A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (2^ n) `\u2192 A\n folda zero f = head\n folda (suc n) f = splitAt (2^ n) \u204f < folda n f \u00d7 folda n f > \u204f f\n\n init : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n init {zero} = nil\n init {suc n} = < id \u2237 init >\n\n last : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n last {zero} = head\n last {suc n} = tail \u204f last\n\n concat : \u2200 {m n A} \u2192 `Vec (`Vec A m) n `\u2192 `Vec A (n * m)\n concat {n = zero} = nil\n concat {n = suc n} = uncons \u204f second concat \u204f append\n\n group : \u2200 {A} n k \u2192 `Vec A (n * k) `\u2192 `Vec (`Vec A k) n\n group zero k = nil\n group (suc n) k = splitAt k \u204f second (group n k) \u204f cons\n\n bind : \u2200 {m n A B} \u2192 (A `\u2192 `Vec B m) \u2192 `Vec A n `\u2192 `Vec B (n * m)\n bind f = map f \u204f concat\n\n replicate : \u2200 {n A} \u2192 A `\u2192 `Vec A n\n replicate {zero} = nil\n replicate {suc n} = < id , replicate > \u204f cons\n\n module WithFin\n (`Fin : \u2115 \u2192 T)\n (fz : \u2200 {n _A} \u2192 _A `\u2192 `Fin (suc n))\n (fs : \u2200 {n} \u2192 `Fin n `\u2192 `Fin (suc n))\n (elim-Fin0 : \u2200 {A} \u2192 `Fin 0 `\u2192 A)\n (elim-Fin1+ : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Fin n `\u00d7 A `\u2192 B) \u2192 `Fin (suc n) `\u00d7 A `\u2192 B) where\n\n tabulate : \u2200 {n A _B} \u2192 (`Fin n `\u2192 A) \u2192 _B `\u2192 `Vec A n\n tabulate {zero} f = nil\n tabulate {suc n} f = < fz \u204f f , tabulate (fs \u204f f) > \u204f cons\n\n lookup : \u2200 {n A} \u2192 `Fin n `\u00d7 `Vec A n `\u2192 A\n lookup {zero} = fst \u204f elim-Fin0\n lookup {suc n} = elim-Fin1+ head (second tail \u204f lookup)\n\n allFin : \u2200 {n _A} \u2192 _A `\u2192 `Vec (`Fin n) n\n allFin = tabulate id\n\n-\u2192- : Set \u2192 Set \u2192 Set\n-\u2192- A B = A \u2192 B\n\n_\u2192\u1da0_ : \u2115 \u2192 \u2115 \u2192 Set\n_\u2192\u1da0_ i o = Fin i \u2192 Fin o\n\nmapArr : \u2200 {s t} {S : Set s} {T : Set t} (F G : T \u2192 S)\n \u2192 (S \u2192 S \u2192 Set) \u2192 (T \u2192 T \u2192 Set)\nmapArr F G _`\u2192_ A B = F A `\u2192 G B\n\nfun\u266dFuns : FlatFuns Set\nfun\u266dFuns = mk Set-U -\u2192-\n\nmodule FunTypes = FlatFuns fun\u266dFuns\n\nbitsFun\u266dFuns : FlatFuns \u2115\nbitsFun\u266dFuns = mk Bits-U _\u2192\u1d47_\n\nmodule BitsFunTypes = FlatFuns bitsFun\u266dFuns\n\nfinFun\u266dFuns : FlatFuns \u2115\nfinFun\u266dFuns = mk Fin-U _\u2192\u1da0_\n\nmodule FinFunTypes = FlatFuns finFun\u266dFuns\n\nfun\u266dOps : FlatFunsOps fun\u266dFuns\nfun\u266dOps = mk F.id F._\u2218\u2032_\n (F.const 0b) (F.const 1b) (\u03bb { (b , x , y) \u2192 if b then x else y })\n (\u03bb x \u2192 x , x) proj\u2081 proj\u2082 (\u03bb f g \u2192 \u00d7.map f g)\n \u00d7.swap (\u03bb {((x , y) , z) \u2192 x , (y , z) }) _ (\u03bb x \u2192 _ , x) (F.const []) (uncurry _\u2237_) V.uncons\n\nmodule FunOps = FlatFunsOps fun\u266dOps\n\nbitsFun\u266dOps : FlatFunsOps bitsFun\u266dFuns\nbitsFun\u266dOps = mk id _\u2218_\n (const [ 0b ]) (const [ 1b ]) cond\u1d47\n dup\u1d47 fst\u1d47 (\u03bb {x} \u2192 snd\u1d47 {x}) <_\u00d7_>\u1d47\n (\u03bb {x} \u2192 swap\u1d47 {x}) (\u03bb {x} \u2192 assoc\u1d47 {x}) (const []) id (const []) id id\n where\n open BitsFunTypes\n open FunOps\n fst\u1d47 : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n fst\u1d47 {A} = V.take A\n snd\u1d47 : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n snd\u1d47 {A} = V.drop A\n dup\u1d47 : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n dup\u1d47 xs = xs ++ xs\n <_\u00d7_>\u1d47 : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n <_\u00d7_>\u1d47 {A} f g x = f (take A x) ++ g (drop A x)\n cond\u1d47 : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n cond\u1d47 {A} (b \u2237 xs) = if b then take A xs else drop A xs\n swap\u1d47 : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n swap\u1d47 {A} xs = drop A xs ++ take A xs\n assoc\u1d47 : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc\u1d47 {A} {B} xs = take A (take (A + B) xs) ++ (drop A (take (A + B) xs) ++ drop (A + B) xs) -- < fst\u1d47 \u204f fst\u1d47 , first snd\u1d47 >\n\nmodule BitsFunOps = FlatFunsOps bitsFun\u266dOps\n\n\u00d7-\u266dFuns : \u2200 {s t} {S : Set s} {T : Set t} \u2192 FlatFuns S \u2192 FlatFuns T \u2192 FlatFuns (S \u00d7 T)\n\u00d7-\u266dFuns funs-S funs-T = mk (\u00d7-U S.universe T.universe)\n (\u03bb { (A\u2080 , A\u2081) (B\u2080 , B\u2081) \u2192 (A\u2080 S.`\u2192 B\u2080) \u00d7 (A\u2081 T.`\u2192 B\u2081) })\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7\u22a4-\u266dFuns : \u2200 {s} {S : Set s} \u2192 FlatFuns S \u2192 FlatFuns \u22a4 \u2192 FlatFuns S\n\u00d7\u22a4-\u266dFuns funs-S funs-T = mk S.universe (\u03bb A B \u2192 (A S.`\u2192 B) \u00d7 (_ T.`\u2192 _))\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7-\u266dOps : \u2200 {s t} {S : Set s} {T : Set t} {funs-S : FlatFuns S} {funs-T : FlatFuns T}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-T\n \u2192 FlatFunsOps (\u00d7-\u266dFuns funs-S funs-T)\n\u00d7-\u266dOps ops-S ops-T\n = mk (S.id , T.id) (\u00d7.zip S._\u2218_ T._\u2218_)\n (S.<0b> , T.<0b>) (S.<1b> , T.<1b>) (S.cond , T.cond)\n (S.dup , T.dup) (S.fst , T.fst) (S.snd , T.snd) (\u00d7.zip S.<_\u00d7_> T.<_\u00d7_>)\n (S.swap , T.swap) (S.assoc , T.assoc)\n (S.tt , T.tt) (S. , T.)\n (S.nil , T.nil) (S.cons , T.cons) (S.uncons , T.uncons)\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-T\n open FunOps\n\n\u00d7\u22a4-\u266dOps : \u2200 {s} {S : Set s} {funs-S : FlatFuns S} {funs-\u22a4 : FlatFuns \u22a4}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-\u22a4\n \u2192 FlatFunsOps (\u00d7\u22a4-\u266dFuns funs-S funs-\u22a4)\n\u00d7\u22a4-\u266dOps ops-S ops-\u22a4\n = mk (S.id , T.id) (\u00d7.zip S._\u2218_ T._\u2218_)\n (S.<0b> , T.<0b>) (S.<1b> , T.<1b>) (S.cond , T.cond)\n (S.dup , T.dup) (S.fst , T.fst) (S.snd , T.snd) (\u00d7.zip S.<_\u00d7_> T.<_\u00d7_>)\n (S.swap , T.swap) (S.assoc , T.assoc)\n (S.tt , T.tt) (S. , T.)\n (S.nil , T.nil) (S.cons , T.id) (S.uncons , T.id)\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-\u22a4\n open FunOps\n\nconstFuns : Set \u2192 FlatFuns \u22a4\nconstFuns A = mk \u22a4-U (\u03bb _ _ \u2192 A)\n\nmodule ConstFunTypes A = FlatFuns (constFuns A)\n\ntimeOps : FlatFunsOps (constFuns \u2115)\ntimeOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 0; <1b> = 0; cond = 1;\n dup = 0; fst = 0; snd = 0; <_\u00d7_> = _\u2294_; swap = 0; assoc = 0;\n tt = 0; = 0;\n nil = 0; cons = 0; uncons = 0 }\n\nmodule TimeOps = FlatFunsOps timeOps\n\nspaceOps : FlatFunsOps (constFuns \u2115)\nspaceOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 0; <1b> = 0; cond = 1;\n dup = 1; fst = 0; snd = 0; <_\u00d7_> = _+_; swap = 0; assoc = 0;\n tt = 0; = 0;\n nil = 0; cons = 0; uncons = 0 }\n\nmodule SpaceOps where\n Space = \u2115\n open FlatFunsOps spaceOps public\n\n singleton\u22610 : singleton \u2261 0\n singleton\u22610 = \u2261.refl\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\ntime\u00d7spaceOps : FlatFunsOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-\u266dOps timeOps spaceOps\n\nmodule Time\u00d7SpaceOps = FlatFunsOps time\u00d7spaceOps\n","old_contents":"module flat-funs where\n\nopen import Data.Unit using (\u22a4)\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2^_; _\u2294_)\nopen import Data.Fin using (Fin) renaming (_+_ to _+\u1da0_)\nopen import Data.Bool using (if_then_else_)\nimport Data.Vec.NP as V\nimport Level as L\nimport Function as F\nimport Data.Product as \u00d7\nopen F using (const)\nopen V using (Vec; []; _\u2237_; _++_; [_])\nopen \u00d7 using (_\u00d7_; _,_; proj\u2081; proj\u2082; uncurry)\n\nopen import Data.Bits using (Bit; Bits; _\u2192\u1d47_; 0b; 1b)\n\nopen import universe\n\nrecord FlatFuns {t} (T : Set t) : Set (L.suc t) where\n constructor mk\n field\n universe : Universe T\n _`\u2192_ : T \u2192 T \u2192 Set\n infix 0 _`\u2192_\n open Universe universe public\n\nrecord FlatFunsOps {t} {T : Set t} (\u266dFuns : FlatFuns T) : Set t where\n constructor mk\n open FlatFuns \u266dFuns\n infixr 1 _\u2218_\n infixr 1 _\u204f_\n infixr 1 _>>>_\n infixr 3 _***_\n infixr 3 _&&&_\n field\n -- Functions\n id : \u2200 {A} \u2192 A `\u2192 A\n _\u2218_ : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u2192 B) \u2192 (A `\u2192 C)\n\n -- Bit\n <0b> <1b> : \u2200 {_A} \u2192 _A `\u2192 `Bit\n cond : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n\n -- Products\n dup : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n fst : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 A\n snd : \u2200 {A B} \u2192 A `\u00d7 B `\u2192 B\n <_\u00d7_> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n\n -- Unit\n tt : \u2200 {_A} \u2192 _A `\u2192 `\u22a4\n\n -- Vectors\n nil : \u2200 {_A B} \u2192 _A `\u2192 `Vec B 0\n cons : \u2200 {n A} \u2192 (A `\u00d7 `Vec A n) `\u2192 `Vec A (1 + n)\n uncons : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 (A `\u00d7 `Vec A n)\n\n _\u204f_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (B `\u2192 C) \u2192 (A `\u2192 C)\n f \u204f g = g \u2218 f\n\n _>>>_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (B `\u2192 C) \u2192 (A `\u2192 C)\n f >>> g = f \u204f g\n\n <_,_> : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n < f , g > = dup \u204f < f \u00d7 g >\n\n _&&&_ : \u2200 {A B C} \u2192 (A `\u2192 B) \u2192 (A `\u2192 C) \u2192 A `\u2192 B `\u00d7 C\n f &&& g = < f , g >\n\n <_\u00d7_>-on-top-of-<,> : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n < f \u00d7 g >-on-top-of-<,> = < fst \u204f f , snd \u204f g >\n\n _***_ : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n f *** g = < f \u00d7 g >\n\n swap : \u2200 {A B} \u2192 (A `\u00d7 B) `\u2192 (B `\u00d7 A)\n swap = < snd , fst >\n\n first : \u2200 {A B C} \u2192 (A `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 B)\n first f = f *** id\n\n second : \u2200 {A B C} \u2192 (B `\u2192 C) \u2192 (A `\u00d7 B) `\u2192 (A `\u00d7 C)\n second f = id *** f\n\n fork : \u2200 {A B} (f g : A `\u2192 B) \u2192 `Bit `\u00d7 A `\u2192 B\n fork f g = second < f , g > \u204f cond\n\n -- In case you wonder... one can also derive cond with fork\n cond-on-top-of-fork : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n cond-on-top-of-fork = fork fst snd\n\n constBit : \u2200 {_A} \u2192 Bit \u2192 _A `\u2192 `Bit\n constBit b = if b then <1b> else <0b>\n\n assoc : \u2200 {A B C} \u2192 ((A `\u00d7 B) `\u00d7 C) `\u2192 (A `\u00d7 (B `\u00d7 C))\n assoc = < fst \u204f fst , first snd >\n\n assoc\u2032 : \u2200 {A B C} \u2192 (A `\u00d7 (B `\u00d7 C)) `\u2192 ((A `\u00d7 B) `\u00d7 C)\n assoc\u2032 = < second fst , snd \u204f snd >\n\n <_`zip`_> : \u2200 {A B C D E F} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 ((D `\u00d7 E) `\u2192 F)\n \u2192 ((A `\u00d7 D) `\u00d7 (B `\u00d7 E)) `\u2192 (C `\u00d7 F)\n < f `zip` g > = < < fst \u00d7 fst > \u204f f ,\n < snd \u00d7 snd > \u204f g >\n\n head : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n head = uncons \u204f fst\n\n tail : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n tail = uncons \u204f snd\n\n <_\u2237_> : \u2200 {m n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A m `\u2192 `Vec B n)\n \u2192 `Vec A (1 + m) `\u2192 `Vec B (1 + n)\n < f \u2237 g > = uncons \u204f < f \u00d7 g > \u204f cons\n\n <_\u2237\u2032_> : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (A `\u2192 `Vec B n)\n \u2192 A `\u2192 `Vec B (1 + n)\n < f \u2237\u2032 g > = < f , g > \u204f cons\n\n \u229b : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 `Vec A n `\u2192 `Vec B n\n \u229b [] = nil\n \u229b (f \u2237 fs) = < f \u2237 \u229b fs >\n\n \u229b\u2032 : \u2200 {n A B} \u2192 Vec (A `\u2192 B) n \u2192 A `\u2192 `Vec B n\n \u229b\u2032 [] = nil\n \u229b\u2032 (f \u2237 fs) = < f \u2237\u2032 \u229b\u2032 fs >\n\n constVec : \u2200 {n b _A} {B : Set b} {C} \u2192 (B \u2192 _A `\u2192 C) \u2192 Vec B n \u2192 _A `\u2192 `Vec C n\n constVec f xs = \u229b\u2032 (V.map f xs)\n\n constBits : \u2200 {n _A} \u2192 Bits n \u2192 _A `\u2192 `Bits n\n constBits = constVec constBit\n\n foldl : \u2200 {n A B} \u2192 (B `\u00d7 A `\u2192 B) \u2192 (B `\u00d7 `Vec A n) `\u2192 B\n foldl {zero} f = fst\n foldl {suc n} f = second uncons\n \u204f assoc\u2032\n \u204f first f\n \u204f foldl f\n\n foldl\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldl\u2081 f = uncons \u204f foldl f\n\n foldr : \u2200 {n A B} \u2192 (A `\u00d7 B `\u2192 B) \u2192 (`Vec A n `\u00d7 B) `\u2192 B\n foldr {zero} f = snd\n foldr {suc n} f = first uncons\n \u204f assoc\n \u204f second (foldr f)\n \u204f f\n\n foldr\u2081 : \u2200 {n A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (1 + n) `\u2192 A\n foldr\u2081 f = uncons \u204f swap \u204f foldr f\n\n map : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Vec A n `\u2192 `Vec B n)\n map {zero} f = nil\n map {suc n} f = < f \u2237 map f >\n\n zipWith : \u2200 {n A B C} \u2192 ((A `\u00d7 B) `\u2192 C)\n \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec C n\n zipWith {zero} f = nil\n zipWith {suc n} f = < uncons \u00d7 uncons >\n \u204f < f `zip` (zipWith f) >\n \u204f cons\n\n zip : \u2200 {n A B} \u2192 (`Vec A n `\u00d7 `Vec B n) `\u2192 `Vec (A `\u00d7 B) n\n zip = zipWith id\n\n singleton : \u2200 {A} \u2192 A `\u2192 `Vec A 1\n singleton = < id , nil > \u204f cons\n\n snoc : \u2200 {n A} \u2192 (`Vec A n `\u00d7 A) `\u2192 `Vec A (1 + n)\n snoc {zero} = snd \u204f singleton\n snoc {suc n} = first uncons \u204f assoc \u204f second snoc \u204f cons\n\n reverse : \u2200 {n A} \u2192 `Vec A n `\u2192 `Vec A n\n reverse {zero} = nil\n reverse {suc n} = uncons \u204f swap \u204f first reverse \u204f snoc\n\n append : \u2200 {m n A} \u2192 (`Vec A m `\u00d7 `Vec A n) `\u2192 `Vec A (m + n)\n append {zero} = snd\n append {suc m} = first uncons\n \u204f assoc\n \u204f second append\n \u204f cons\n\n splitAt : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 (`Vec A m `\u00d7 `Vec A n)\n splitAt zero = < nil , id >\n splitAt (suc m) = uncons\n \u204f second (splitAt m)\n \u204f assoc\u2032\n \u204f first cons\n\n take : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A m\n take zero = nil\n take (suc m) = < id \u2237 take m >\n\n drop : \u2200 m {n A} \u2192 `Vec A (m + n) `\u2192 `Vec A n\n drop zero = id\n drop (suc m) = tail \u204f drop m\n\n folda : \u2200 n {A} \u2192 (A `\u00d7 A `\u2192 A) \u2192 `Vec A (2^ n) `\u2192 A\n folda zero f = head\n folda (suc n) f = splitAt (2^ n) \u204f < folda n f \u00d7 folda n f > \u204f f\n\n init : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 `Vec A n\n init {zero} = nil\n init {suc n} = < id \u2237 init >\n\n last : \u2200 {n A} \u2192 `Vec A (1 + n) `\u2192 A\n last {zero} = head\n last {suc n} = tail \u204f last\n\n concat : \u2200 {m n A} \u2192 `Vec (`Vec A m) n `\u2192 `Vec A (n * m)\n concat {n = zero} = nil\n concat {n = suc n} = uncons \u204f second concat \u204f append\n\n group : \u2200 {A} n k \u2192 `Vec A (n * k) `\u2192 `Vec (`Vec A k) n\n group zero k = nil\n group (suc n) k = splitAt k \u204f second (group n k) \u204f cons\n\n bind : \u2200 {m n A B} \u2192 (A `\u2192 `Vec B m) \u2192 `Vec A n `\u2192 `Vec B (n * m)\n bind f = map f \u204f concat\n\n replicate : \u2200 {n A} \u2192 A `\u2192 `Vec A n\n replicate {zero} = nil\n replicate {suc n} = < id , replicate > \u204f cons\n\n module WithFin\n (`Fin : \u2115 \u2192 T)\n (fz : \u2200 {n _A} \u2192 _A `\u2192 `Fin (suc n))\n (fs : \u2200 {n} \u2192 `Fin n `\u2192 `Fin (suc n))\n (elim-Fin0 : \u2200 {A} \u2192 `Fin 0 `\u2192 A)\n (elim-Fin1+ : \u2200 {n A B} \u2192 (A `\u2192 B) \u2192 (`Fin n `\u00d7 A `\u2192 B) \u2192 `Fin (suc n) `\u00d7 A `\u2192 B) where\n\n tabulate : \u2200 {n A _B} \u2192 (`Fin n `\u2192 A) \u2192 _B `\u2192 `Vec A n\n tabulate {zero} f = nil\n tabulate {suc n} f = < fz \u204f f , tabulate (fs \u204f f) > \u204f cons\n\n lookup : \u2200 {n A} \u2192 `Fin n `\u00d7 `Vec A n `\u2192 A\n lookup {zero} = fst \u204f elim-Fin0\n lookup {suc n} = elim-Fin1+ head (second tail \u204f lookup)\n\n allFin : \u2200 {n _A} \u2192 _A `\u2192 `Vec (`Fin n) n\n allFin = tabulate id\n\n-\u2192- : Set \u2192 Set \u2192 Set\n-\u2192- A B = A \u2192 B\n\n_\u2192\u1da0_ : \u2115 \u2192 \u2115 \u2192 Set\n_\u2192\u1da0_ i o = Fin i \u2192 Fin o\n\nmapArr : \u2200 {s t} {S : Set s} {T : Set t} (F G : T \u2192 S)\n \u2192 (S \u2192 S \u2192 Set) \u2192 (T \u2192 T \u2192 Set)\nmapArr F G _`\u2192_ A B = F A `\u2192 G B\n\nfun\u266dFuns : FlatFuns Set\nfun\u266dFuns = mk Set-U -\u2192-\n\nmodule FunTypes = FlatFuns fun\u266dFuns\n\nbitsFun\u266dFuns : FlatFuns \u2115\nbitsFun\u266dFuns = mk Bits-U _\u2192\u1d47_\n\nmodule BitsFunTypes = FlatFuns bitsFun\u266dFuns\n\nfinFun\u266dFuns : FlatFuns \u2115\nfinFun\u266dFuns = mk Fin-U _\u2192\u1da0_\n\nmodule FinFunTypes = FlatFuns finFun\u266dFuns\n\nfun\u266dOps : FlatFunsOps fun\u266dFuns\nfun\u266dOps = mk F.id F._\u2218\u2032_\n (F.const 0b) (F.const 1b) (\u03bb { (b , x , y) \u2192 if b then x else y })\n (\u03bb x \u2192 x , x) proj\u2081 proj\u2082 (\u03bb f g \u2192 \u00d7.map f g)\n _ (F.const []) (uncurry _\u2237_) V.uncons\n\nmodule FunOps = FlatFunsOps fun\u266dOps\n\nbitsFun\u266dOps : FlatFunsOps bitsFun\u266dFuns\nbitsFun\u266dOps = mk id _\u2218_\n (const [ 0b ]) (const [ 1b ]) cond\u1d47\n dup\u1d47 (\u03bb {A} \u2192 V.take A) (\u03bb {A} \u2192 V.drop A) <_\u00d7_>\u1d47\n (const []) (const []) id id\n where\n open BitsFunTypes\n open FunOps\n dup\u1d47 : \u2200 {A} \u2192 A `\u2192 A `\u00d7 A\n dup\u1d47 xs = xs ++ xs\n <_\u00d7_>\u1d47 : \u2200 {A B C D} \u2192 (A `\u2192 C) \u2192 (B `\u2192 D) \u2192 (A `\u00d7 B) `\u2192 (C `\u00d7 D)\n <_\u00d7_>\u1d47 {A} f g x = f (take A x) ++ g (drop A x)\n cond\u1d47 : \u2200 {A} \u2192 `Bit `\u00d7 A `\u00d7 A `\u2192 A\n cond\u1d47 {A} (b \u2237 xs) = if b then take A xs else drop A xs\n\nmodule BitsFunOps = FlatFunsOps bitsFun\u266dOps\n\n\u00d7-\u266dFuns : \u2200 {s t} {S : Set s} {T : Set t} \u2192 FlatFuns S \u2192 FlatFuns T \u2192 FlatFuns (S \u00d7 T)\n\u00d7-\u266dFuns funs-S funs-T = mk (\u00d7-U S.universe T.universe)\n (\u03bb { (A\u2080 , A\u2081) (B\u2080 , B\u2081) \u2192 (A\u2080 S.`\u2192 B\u2080) \u00d7 (A\u2081 T.`\u2192 B\u2081) })\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7\u22a4-\u266dFuns : \u2200 {s} {S : Set s} \u2192 FlatFuns S \u2192 FlatFuns \u22a4 \u2192 FlatFuns S\n\u00d7\u22a4-\u266dFuns funs-S funs-T = mk S.universe (\u03bb A B \u2192 (A S.`\u2192 B) \u00d7 (_ T.`\u2192 _))\n where module S = FlatFuns funs-S\n module T = FlatFuns funs-T\n\n\u00d7-\u266dOps : \u2200 {s t} {S : Set s} {T : Set t} {funs-S : FlatFuns S} {funs-T : FlatFuns T}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-T\n \u2192 FlatFunsOps (\u00d7-\u266dFuns funs-S funs-T)\n\u00d7-\u266dOps ops-S ops-T\n = mk (S.id , T.id) (\u00d7.zip S._\u2218_ T._\u2218_)\n (S.<0b> , T.<0b>) (S.<1b> , T.<1b>) (S.cond , T.cond)\n (S.dup , T.dup) (S.fst , T.fst) (S.snd , T.snd) (\u00d7.zip S.<_\u00d7_> T.<_\u00d7_>)\n (S.tt , T.tt) (S.nil , T.nil) (S.cons , T.cons) (S.uncons , T.uncons)\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-T\n open FunOps\n\n\u00d7\u22a4-\u266dOps : \u2200 {s} {S : Set s} {funs-S : FlatFuns S} {funs-\u22a4 : FlatFuns \u22a4}\n \u2192 FlatFunsOps funs-S \u2192 FlatFunsOps funs-\u22a4\n \u2192 FlatFunsOps (\u00d7\u22a4-\u266dFuns funs-S funs-\u22a4)\n\u00d7\u22a4-\u266dOps ops-S ops-\u22a4\n = mk (S.id , T.id) (\u00d7.zip S._\u2218_ T._\u2218_)\n (S.<0b> , T.<0b>) (S.<1b> , T.<1b>) (S.cond , T.cond)\n (S.dup , T.dup) (S.fst , T.fst) (S.snd , T.snd) (\u00d7.zip S.<_\u00d7_> T.<_\u00d7_>)\n (S.tt , T.tt) (S.nil , T.nil) (S.cons , T.id) (S.uncons , T.id)\n where module S = FlatFunsOps ops-S\n module T = FlatFunsOps ops-\u22a4\n open FunOps\n\nconstFuns : Set \u2192 FlatFuns \u22a4\nconstFuns A = mk \u22a4-U (\u03bb _ _ \u2192 A)\n\nmodule ConstFunTypes A = FlatFuns (constFuns A)\n\ntimeOps : FlatFunsOps (constFuns \u2115)\ntimeOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 0; <1b> = 0; cond = 1;\n dup = 0; fst = 0; snd = 0; <_\u00d7_> = _\u2294_;\n tt = 0;\n nil = 0; cons = 0; uncons = 0 }\n\nmodule TimeOps = FlatFunsOps timeOps\n\nspaceOps : FlatFunsOps (constFuns \u2115)\nspaceOps = record {\n id = 0; _\u2218_ = _+_;\n <0b> = 0; <1b> = 0; cond = 1;\n dup = 1; fst = 0; snd = 0; <_\u00d7_> = _+_;\n tt = 0;\n nil = 0; cons = 0; uncons = 0 }\n\nmodule SpaceOps = FlatFunsOps spaceOps\n\ntime\u00d7spaceOps : FlatFunsOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-\u266dOps timeOps spaceOps\n\nmodule Time\u00d7SpaceOps = FlatFunsOps time\u00d7spaceOps\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"e03e05b0d9ad469cb29f032687a431dcfe115212","subject":"Cosmetic changes.","message":"Cosmetic changes.\n\nIgnore-this: 9679e5e5b7e04c672101f1626610d4a6\n\ndarcs-hash:20110406210038-3bd4e-9bc556a6e9785ce5d81eaff93aefd4284f80c4b9.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Program\/McCarthy91\/Properties\/AuxiliaryATP.agda","new_file":"src\/FOTC\/Program\/McCarthy91\/Properties\/AuxiliaryATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Auxiliary properties of the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule FOTC.Program.McCarthy91.Properties.AuxiliaryATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.IsN-ATP\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\n\nopen import FOTC.Program.McCarthy91.ArithmeticATP\nopen import FOTC.Program.McCarthy91.McCarthy91\n\n------------------------------------------------------------------------------\n\n--- Auxiliary properties\n\n---- Case n > 100\npostulate\n Nmc91>100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192 N (mc91 n)\n x100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192\n LT n (mc91 n + eleven)\n{-# ATP prove Nmc91>100 10-N \u2238-N #-}\n{-# ATP prove x100 +-N \u2238-N x100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810 #-}\n{-# ATP prove mc91-res-100 111>100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810 #-}\n{-# ATP prove mc91-res-100' mc91-res-100 #-}\n{-# ATP prove mc91100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-109 98+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-108 97+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-107 96+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-106 95+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-105 94+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-104 93+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-103 92+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-102 91+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-101 90+11>100 x+11\u223810\u2261Sx #-}\n\npostulate\n mc91-res-99 : mc91 ninety-nine \u2261 ninety-one\n mc91-res-98 : mc91 ninety-eight \u2261 ninety-one\n mc91-res-97 : mc91 ninety-seven \u2261 ninety-one\n mc91-res-96 : mc91 ninety-six \u2261 ninety-one\n mc91-res-95 : mc91 ninety-five \u2261 ninety-one\n mc91-res-94 : mc91 ninety-four \u2261 ninety-one\n mc91-res-93 : mc91 ninety-three \u2261 ninety-one\n mc91-res-92 : mc91 ninety-two \u2261 ninety-one\n mc91-res-91 : mc91 ninety-one \u2261 ninety-one\n mc91-res-90 : mc91 ninety \u2261 ninety-one\n{-# ATP prove mc91-res-99 mc91x-res\u2264100 mc91-res-110 mc91-res-100 #-}\n{-# ATP prove mc91-res-98 mc91x-res\u2264100 mc91-res-109 mc91-res-99 #-}\n{-# ATP prove mc91-res-97 mc91x-res\u2264100 mc91-res-108 mc91-res-98 #-}\n{-# ATP prove mc91-res-96 mc91x-res\u2264100 mc91-res-107 mc91-res-97 #-}\n{-# ATP prove mc91-res-95 mc91x-res\u2264100 mc91-res-106 mc91-res-96 #-}\n{-# ATP prove mc91-res-94 mc91x-res\u2264100 mc91-res-105 mc91-res-95 #-}\n{-# ATP prove mc91-res-93 mc91x-res\u2264100 mc91-res-104 mc91-res-94 #-}\n{-# ATP prove mc91-res-92 mc91x-res\u2264100 mc91-res-103 mc91-res-93 #-}\n{-# ATP prove mc91-res-91 mc91x-res\u2264100 mc91-res-102 mc91-res-92 #-}\n{-# ATP prove mc91-res-90 mc91x-res\u2264100 mc91-res-101 mc91-res-91 #-}\n\nmc91-res-99' : \u2200 {n} \u2192 n \u2261 ninety-nine \u2192 mc91 n \u2261 ninety-one\nmc91-res-99' refl = mc91-res-99\n\nmc91-res-98' : \u2200 {n} \u2192 n \u2261 ninety-eight \u2192 mc91 n \u2261 ninety-one\nmc91-res-98' refl = mc91-res-98\n\nmc91-res-97' : \u2200 {n} \u2192 n \u2261 ninety-seven \u2192 mc91 n \u2261 ninety-one\nmc91-res-97' refl = mc91-res-97\n\nmc91-res-96' : \u2200 {n} \u2192 n \u2261 ninety-six \u2192 mc91 n \u2261 ninety-one\nmc91-res-96' refl = mc91-res-96\n\nmc91-res-95' : \u2200 {n} \u2192 n \u2261 ninety-five \u2192 mc91 n \u2261 ninety-one\nmc91-res-95' refl = mc91-res-95\n\nmc91-res-94' : \u2200 {n} \u2192 n \u2261 ninety-four \u2192 mc91 n \u2261 ninety-one\nmc91-res-94' refl = mc91-res-94\n\nmc91-res-93' : \u2200 {n} \u2192 n \u2261 ninety-three \u2192 mc91 n \u2261 ninety-one\nmc91-res-93' refl = mc91-res-93\n\nmc91-res-92' : \u2200 {n} \u2192 n \u2261 ninety-two \u2192 mc91 n \u2261 ninety-one\nmc91-res-92' refl = mc91-res-92\n\nmc91-res-91' : \u2200 {n} \u2192 n \u2261 ninety-one \u2192 mc91 n \u2261 ninety-one\nmc91-res-91' refl = mc91-res-91\n\nmc91-res-90' : \u2200 {n} \u2192 n \u2261 ninety \u2192 mc91 n \u2261 ninety-one\nmc91-res-90' refl = mc91-res-90\n","old_contents":"------------------------------------------------------------------------------\n-- Auxiliary properties of the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule FOTC.Program.McCarthy91.Properties.AuxiliaryATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.IsN-ATP\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\n\nopen import FOTC.Program.McCarthy91.ArithmeticATP\nopen import FOTC.Program.McCarthy91.McCarthy91\n\n------------------------------------------------------------------------------\n\n--- Auxiliary properties\n\n---- Case n > 100\npostulate\n Nmc91>100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192 N (mc91 n)\n x100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192\n LT n (mc91 n + eleven)\n{-# ATP prove Nmc91>100 10-N \u2238-N #-}\n{-# ATP prove x100 +-N \u2238-N x100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810 #-}\n{-# ATP prove mc91-res-100 111>100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810 #-}\n{-# ATP prove mc91-res-100' mc91-res-100 #-}\n{-# ATP prove mc91100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-109 98+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-108 97+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-107 96+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-106 95+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-105 94+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-104 93+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-103 92+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-102 91+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-101 90+11>100 x+11\u223810\u2261Sx #-}\n\npostulate\n mc91-res-99 : mc91 ninety-nine \u2261 ninety-one\n mc91-res-98 : mc91 ninety-eight \u2261 ninety-one\n mc91-res-97 : mc91 ninety-seven \u2261 ninety-one\n mc91-res-96 : mc91 ninety-six \u2261 ninety-one\n mc91-res-95 : mc91 ninety-five \u2261 ninety-one\n mc91-res-94 : mc91 ninety-four \u2261 ninety-one\n mc91-res-93 : mc91 ninety-three \u2261 ninety-one\n mc91-res-92 : mc91 ninety-two \u2261 ninety-one\n mc91-res-91 : mc91 ninety-one \u2261 ninety-one\n mc91-res-90 : mc91 ninety \u2261 ninety-one\n{-# ATP prove mc91-res-99 mc91x-res\u2264100 mc91-res-110 mc91-res-100 #-}\n{-# ATP prove mc91-res-98 mc91x-res\u2264100 mc91-res-109 mc91-res-99 #-}\n{-# ATP prove mc91-res-97 mc91x-res\u2264100 mc91-res-108 mc91-res-98 #-}\n{-# ATP prove mc91-res-96 mc91x-res\u2264100 mc91-res-107 mc91-res-97 #-}\n{-# ATP prove mc91-res-95 mc91x-res\u2264100 mc91-res-106 mc91-res-96 #-}\n{-# ATP prove mc91-res-94 mc91x-res\u2264100 mc91-res-105 mc91-res-95 #-}\n{-# ATP prove mc91-res-93 mc91x-res\u2264100 mc91-res-104 mc91-res-94 #-}\n{-# ATP prove mc91-res-92 mc91x-res\u2264100 mc91-res-103 mc91-res-93 #-}\n{-# ATP prove mc91-res-91 mc91x-res\u2264100 mc91-res-102 mc91-res-92 #-}\n{-# ATP prove mc91-res-90 mc91x-res\u2264100 mc91-res-101 mc91-res-91 #-}\n\nmc91-res-99' : \u2200 {n} \u2192 n \u2261 ninety-nine \u2192 mc91 n \u2261 ninety-one\nmc91-res-99' refl = mc91-res-99\n\nmc91-res-98' : \u2200 {n} \u2192 n \u2261 ninety-eight \u2192 mc91 n \u2261 ninety-one\nmc91-res-98' refl = mc91-res-98\n\nmc91-res-97' : \u2200 {n} \u2192 n \u2261 ninety-seven \u2192 mc91 n \u2261 ninety-one\nmc91-res-97' refl = mc91-res-97\n\nmc91-res-96' : \u2200 {n} \u2192 n \u2261 ninety-six \u2192 mc91 n \u2261 ninety-one\nmc91-res-96' refl = mc91-res-96\n\nmc91-res-95' : \u2200 {n} \u2192 n \u2261 ninety-five \u2192 mc91 n \u2261 ninety-one\nmc91-res-95' refl = mc91-res-95\n\nmc91-res-94' : \u2200 {n} \u2192 n \u2261 ninety-four \u2192 mc91 n \u2261 ninety-one\nmc91-res-94' refl = mc91-res-94\n\nmc91-res-93' : \u2200 {n} \u2192 n \u2261 ninety-three \u2192 mc91 n \u2261 ninety-one\nmc91-res-93' refl = mc91-res-93\n\nmc91-res-92' : \u2200 {n} \u2192 n \u2261 ninety-two \u2192 mc91 n \u2261 ninety-one\nmc91-res-92' refl = mc91-res-92\n\nmc91-res-91' : \u2200 {n} \u2192 n \u2261 ninety-one \u2192 mc91 n \u2261 ninety-one\nmc91-res-91' refl = mc91-res-91\n\nmc91-res-90' : \u2200 {n} \u2192 n \u2261 ninety \u2192 mc91 n \u2261 ninety-one\nmc91-res-90' refl = mc91-res-90\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d5b6a24bbe65ed220da02435d605203270c15075","subject":"Fixed doc.","message":"Fixed doc.\n\nIgnore-this: ad938e7eafbcc12667861d22d0600f60\n\ndarcs-hash:20120216155813-3bd4e-d0de42d4bd483dac1f842f071dace36dc7211426.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/thesis\/logical-framework\/Existential.agda","new_file":"notes\/thesis\/logical-framework\/Existential.agda","new_contents":"-- Tested with FOT on 16 February 2012.\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Existential where\n\nmodule LF where\n postulate\n D : Set\n \u2203 : (P : D \u2192 Set) \u2192 Set\n _,_ : {P : D \u2192 Set}(x : D) \u2192 P x \u2192 \u2203 P\n \u2203-proj\u2081 : {P : D \u2192 Set} \u2192 \u2203 P \u2192 D\n \u2203-proj\u2082 : {P : D \u2192 Set}(p : \u2203 P) \u2192 P (\u2203-proj\u2081 p)\n\n syntax \u2203 (\u03bb x \u2192 e) = \u2203[ x ] e\n\n postulate P : D \u2192 D \u2192 Set\n\n \u2203\u2200 : \u2203[ x ](\u2200 y \u2192 P x y) \u2192 \u2200 y \u2192 \u2203[ x ] P x y\n \u2203\u2200 h y = \u2203-proj\u2081 h , (\u2203-proj\u2082 h) y\n\nmodule Inductive where\n\n open import Common.Universe\n open import Common.Data.Product\n\n postulate P : D \u2192 D \u2192 Set\n\n \u2203\u2200-el : \u2203[ x ](\u2200 y \u2192 P x y) \u2192 \u2200 y \u2192 \u2203[ x ] P x y\n \u2203\u2200-el h y = \u2203-proj\u2081 h , (\u2203-proj\u2082 h) y\n\n \u2203\u2200 : \u2203[ x ](\u2200 y \u2192 P x y) \u2192 \u2200 y \u2192 \u2203[ x ] P x y\n \u2203\u2200 (x , Px) y = x , Px y\n","old_contents":"-- Tested with the development version of Agda on 07 February 2012.\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Existential where\n\nmodule LF where\n postulate\n D : Set\n \u2203 : (P : D \u2192 Set) \u2192 Set\n _,_ : {P : D \u2192 Set}(x : D) \u2192 P x \u2192 \u2203 P\n \u2203-proj\u2081 : {P : D \u2192 Set} \u2192 \u2203 P \u2192 D\n \u2203-proj\u2082 : {P : D \u2192 Set}(p : \u2203 P) \u2192 P (\u2203-proj\u2081 p)\n\n syntax \u2203 (\u03bb x \u2192 e) = \u2203[ x ] e\n\n postulate P : D \u2192 D \u2192 Set\n\n \u2203\u2200 : \u2203[ x ](\u2200 y \u2192 P x y) \u2192 \u2200 y \u2192 \u2203[ x ] P x y\n \u2203\u2200 h y = \u2203-proj\u2081 h , (\u2203-proj\u2082 h) y\n\nmodule Inductive where\n\n open import Common.Universe\n open import Common.Data.Product\n\n postulate P : D \u2192 D \u2192 Set\n\n \u2203\u2200-el : \u2203[ x ](\u2200 y \u2192 P x y) \u2192 \u2200 y \u2192 \u2203[ x ] P x y\n \u2203\u2200-el h y = \u2203-proj\u2081 h , (\u2203-proj\u2082 h) y\n\n \u2203\u2200 : \u2203[ x ](\u2200 y \u2192 P x y) \u2192 \u2200 y \u2192 \u2203[ x ] P x y\n \u2203\u2200 (x , Px) y = x , Px y\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"b796f46c9070bb8ca14a238aaf66de660c0f53cc","subject":"Bool: +\u2227\u21d2\u2228, +to\u2115, +to\u2115\u22641","message":"Bool: +\u2227\u21d2\u2228, +to\u2115, +to\u2115\u22641\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bool\/NP.agda","new_file":"lib\/Data\/Bool\/NP.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Bool.NP where\n\nopen import Data.Bool using (Bool; true; false; T; if_then_else_; not)\nimport Algebra\nimport Data.Bool.Properties as B\nopen import Data.Unit using (\u22a4)\nopen import Data.Product\nopen import Data.Sum\nopen import Data.Nat using (\u2115; _\u2264_; z\u2264n; s\u2264s)\nopen import Function\nopen import Relation.Binary.NP\nopen import Relation.Binary.Logical\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\nimport Relation.Binary.PropositionalEquality as \u2261\nimport Function.Equivalence as E\nopen E.Equivalence using (to; from)\nopen import Function.Equality using (_\u27e8$\u27e9_)\nopen \u2261 using (_\u2261_)\n\nmodule Xor\u00b0 = Algebra.CommutativeRing B.commutativeRing-xor-\u2227\nmodule Bool\u00b0 = Algebra.CommutativeSemiring B.commutativeSemiring-\u2227-\u2228\n\ncheck : \u2200 b \u2192 {pf : T b} \u2192 \u22a4\ncheck = _\n\nIf_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 (T b \u2192 A) \u2192 (T (not b) \u2192 B) \u2192 if b then A else B\nIf_then_else_ true x _ = x _\nIf_then_else_ false _ x = x _\n\nIf\u2032_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 A \u2192 B \u2192 if b then A else B\nIf\u2032_then_else_ true x _ = x\nIf\u2032_then_else_ false _ x = x\n\nIf-map : \u2200 {A B C D : Set} b (f : T b \u2192 A \u2192 C) (g : T (not b) \u2192 B \u2192 D) \u2192\n if b then A else B \u2192 if b then C else D\nIf-map true f _ = f _\nIf-map false _ f = f _\n\nIf-elim : \u2200 {A B : Set} {P : Bool \u2192 Set}\n b (f : T b \u2192 A \u2192 P true) (g : T (not b) \u2192 B \u2192 P false) \u2192 if b then A else B \u2192 P b\nIf-elim true f _ = f _\nIf-elim false _ f = f _\n\nIf-true : \u2200 {A B : Set} {b} \u2192 T b \u2192 (if b then A else B) \u2261 A\nIf-true {b = true} _ = \u2261.refl\nIf-true {b = false} ()\n\nIf-false : \u2200 {A B : Set} {b} \u2192 T (not b) \u2192 (if b then A else B) \u2261 B\nIf-false {b = true} ()\nIf-false {b = false} _ = \u2261.refl\n\ncong-if : \u2200 {A B : Set} b {t\u2080 t\u2081} (f : A \u2192 B) \u2192 (if b then f t\u2080 else f t\u2081) \u2261 f (if b then t\u2080 else t\u2081)\ncong-if true _ = \u2261.refl\ncong-if false _ = \u2261.refl\n\nif-not : \u2200 {a} {A : Set a} b {t\u2080 t\u2081 : A} \u2192 (if b then t\u2080 else t\u2081) \u2261 (if not b then t\u2081 else t\u2080)\nif-not true = \u2261.refl\nif-not false = \u2261.refl\n\ndata \u27e6Bool\u27e7 : (b\u2081 b\u2082 : Bool) \u2192 Set where\n \u27e6true\u27e7 : \u27e6Bool\u27e7 true true\n \u27e6false\u27e7 : \u27e6Bool\u27e7 false false\n\nprivate\n module \u27e6Bool\u27e7-Internals where\n refl : Reflexive \u27e6Bool\u27e7\n refl {true} = \u27e6true\u27e7\n refl {false} = \u27e6false\u27e7\n\n sym : Symmetric \u27e6Bool\u27e7\n sym \u27e6true\u27e7 = \u27e6true\u27e7\n sym \u27e6false\u27e7 = \u27e6false\u27e7\n\n trans : Transitive \u27e6Bool\u27e7\n trans \u27e6true\u27e7 = id\n trans \u27e6false\u27e7 = id\n\n subst : \u2200 {\u2113} \u2192 Substitutive \u27e6Bool\u27e7 \u2113\n subst _ \u27e6true\u27e7 = id\n subst _ \u27e6false\u27e7 = id\n\n _\u225f_ : Decidable \u27e6Bool\u27e7\n true \u225f true = yes \u27e6true\u27e7\n false \u225f false = yes \u27e6false\u27e7\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence \u27e6Bool\u27e7\n isEquivalence = record { refl = refl; sym = sym; trans = trans }\n\n isDecEquivalence : IsDecEquivalence \u27e6Bool\u27e7\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isDecEquivalence = isDecEquivalence }\n\n equality : Equality \u27e6Bool\u27e7\n equality = record { isEquivalence = isEquivalence; subst = subst }\n\nmodule \u27e6Bool\u27e7-Props where\n open \u27e6Bool\u27e7-Internals public using (subst; decSetoid; equality)\n open DecSetoid decSetoid public\n open Equality equality public hiding (subst; isEquivalence; refl; reflexive; sym; trans)\n\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6Bool\u27e7 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63) if_then_else_ if_then_else_\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 : \u2200 {\u2113\u2081 \u2113\u2082 \u2113\u1d63} \u2192\n (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 B\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7\n \u27e8 b\u1d63 \u2236 \u27e6Bool\u27e7 \u27e9\u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 \u27e6if\u27e8 \u27e6Set\u27e7 _ \u27e9 b\u1d63 then A\u1d63 else B\u1d63 \u27e7)\n If\u2032_then_else_ If\u2032_then_else_\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n_==_ : (x y : Bool) \u2192 Bool\ntrue == true = true\ntrue == false = false\nfalse == true = false\nfalse == false = true\n\nmodule == where\n _\u2248_ : (x y : Bool) \u2192 Set\n x \u2248 y = T (x == y)\n\n refl : Reflexive _\u2248_\n refl {true} = _\n refl {false} = _\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {true} {true} _ = id\n subst _ {false} {false} _ = id\n subst _ {true} {false} ()\n subst _ {false} {true} ()\n\n sym : Symmetric _\u2248_\n sym {x} {y} eq = subst (\u03bb y \u2192 y \u2248 x) {x} {y} eq (refl {x})\n\n trans : Transitive _\u2248_\n trans {x} {y} {z} x\u2248y y\u2248z = subst (_\u2248_ x) {y} {z} y\u2248z x\u2248y\n\n _\u225f_ : Decidable _\u2248_\n true \u225f true = yes _\n false \u225f false = yes _\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence _\u2248_\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n\n isDecEquivalence : IsDecEquivalence _\u2248_\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = _\u2248_ ; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = _\u2248_; isDecEquivalence = isDecEquivalence }\n\nmodule \u27e6Bool\u27e7-Reasoning = Setoid-Reasoning \u27e6Bool\u27e7-Props.setoid\n\nopen Data.Bool public\n\n\u27e6true\u27e7\u2032 : \u2200 {b} \u2192 T b \u2192 \u27e6Bool\u27e7 true b\n\u27e6true\u27e7\u2032 {true} _ = \u27e6true\u27e7\n\u27e6true\u27e7\u2032 {false} ()\n\n\u27e6false\u27e7\u2032 : \u2200 {b} \u2192 T (not b) \u2192 \u27e6Bool\u27e7 false b\n\u27e6false\u27e7\u2032 {true} ()\n\u27e6false\u27e7\u2032 {false} _ = \u27e6false\u27e7\n\nT\u2227 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T b\u2082 \u2192 T (b\u2081 \u2227 b\u2082)\nT\u2227 p q = _\u27e8$\u27e9_ (from B.T-\u2227) (p , q)\n\nT\u2227\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2081\nT\u2227\u2081 = proj\u2081 \u2218 _\u27e8$\u27e9_ (to B.T-\u2227)\n\nT\u2227\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2082\nT\u2227\u2082 {b\u2081} = proj\u2082 \u2218 _\u27e8$\u27e9_ (to (B.T-\u2227 {b\u2081}))\n\nT\u2228'\u228e : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2228 b\u2082) \u2192 T b\u2081 \u228e T b\u2082\nT\u2228'\u228e {b\u2081} = _\u27e8$\u27e9_ (to (B.T-\u2228 {b\u2081}))\n\nT\u2228\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2081 = _\u27e8$\u27e9_ (from B.T-\u2228) \u2218 inj\u2081\n\nT\u2228\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2082 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2082 {b\u2081} = _\u27e8$\u27e9_ (from (B.T-\u2228 {b\u2081})) \u2218 inj\u2082\n\nT'not'\u00ac : \u2200 {b} \u2192 T (not b) \u2192 \u00ac (T b)\nT'not'\u00ac {false} _ = \u03bb()\nT'not'\u00ac {true} ()\n\nT'\u00ac'not : \u2200 {b} \u2192 \u00ac (T b) \u2192 T (not b)\nT'\u00ac'not {true} f = f _\nT'\u00ac'not {false} _ = _\n\n\u2227\u21d2\u2228 : \u2200 x y \u2192 T (x \u2227 y) \u2192 T (x \u2228 y)\n\u2227\u21d2\u2228 true y = _\n\u2227\u21d2\u2228 false y = \u03bb ()\n\nTdec : \u2200 b \u2192 Dec (T b)\nTdec true = yes _\nTdec false = no \u03bb()\n\nde-morgan : \u2200 x y \u2192 not (x \u2228 y) \u2261 not x \u2227 not y\nde-morgan true _ = \u2261.refl\nde-morgan false _ = \u2261.refl\n\n-- false is 0 and true is 1\nto\u2115 : Bool \u2192 \u2115\nto\u2115 b = if b then 1 else 0\n\nto\u2115\u22641 : \u2200 b \u2192 to\u2115 b \u2264 1\nto\u2115\u22641 true = s\u2264s z\u2264n\nto\u2115\u22641 false = z\u2264n\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Bool.NP where\n\nopen import Data.Bool using (Bool; true; false; T; if_then_else_; not)\nimport Algebra\nimport Data.Bool.Properties as B\nopen import Data.Unit using (\u22a4)\nopen import Data.Product\nopen import Data.Sum\nopen import Function\nopen import Relation.Binary.NP\nopen import Relation.Binary.Logical\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\nimport Relation.Binary.PropositionalEquality as \u2261\nimport Function.Equivalence as E\nopen E.Equivalence using (to; from)\nopen import Function.Equality using (_\u27e8$\u27e9_)\nopen \u2261 using (_\u2261_)\n\nmodule Xor\u00b0 = Algebra.CommutativeRing B.commutativeRing-xor-\u2227\nmodule Bool\u00b0 = Algebra.CommutativeSemiring B.commutativeSemiring-\u2227-\u2228\n\ncheck : \u2200 b \u2192 {pf : T b} \u2192 \u22a4\ncheck = _\n\nIf_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 (T b \u2192 A) \u2192 (T (not b) \u2192 B) \u2192 if b then A else B\nIf_then_else_ true x _ = x _\nIf_then_else_ false _ x = x _\n\nIf\u2032_then_else_ : \u2200 {\u2113} {A B : Set \u2113} b \u2192 A \u2192 B \u2192 if b then A else B\nIf\u2032_then_else_ true x _ = x\nIf\u2032_then_else_ false _ x = x\n\nIf-map : \u2200 {A B C D : Set} b (f : T b \u2192 A \u2192 C) (g : T (not b) \u2192 B \u2192 D) \u2192\n if b then A else B \u2192 if b then C else D\nIf-map true f _ = f _\nIf-map false _ f = f _\n\nIf-elim : \u2200 {A B : Set} {P : Bool \u2192 Set}\n b (f : T b \u2192 A \u2192 P true) (g : T (not b) \u2192 B \u2192 P false) \u2192 if b then A else B \u2192 P b\nIf-elim true f _ = f _\nIf-elim false _ f = f _\n\nIf-true : \u2200 {A B : Set} {b} \u2192 T b \u2192 (if b then A else B) \u2261 A\nIf-true {b = true} _ = \u2261.refl\nIf-true {b = false} ()\n\nIf-false : \u2200 {A B : Set} {b} \u2192 T (not b) \u2192 (if b then A else B) \u2261 B\nIf-false {b = true} ()\nIf-false {b = false} _ = \u2261.refl\n\ncong-if : \u2200 {A B : Set} b {t\u2080 t\u2081} (f : A \u2192 B) \u2192 (if b then f t\u2080 else f t\u2081) \u2261 f (if b then t\u2080 else t\u2081)\ncong-if true _ = \u2261.refl\ncong-if false _ = \u2261.refl\n\nif-not : \u2200 {a} {A : Set a} b {t\u2080 t\u2081 : A} \u2192 (if b then t\u2080 else t\u2081) \u2261 (if not b then t\u2081 else t\u2080)\nif-not true = \u2261.refl\nif-not false = \u2261.refl\n\ndata \u27e6Bool\u27e7 : (b\u2081 b\u2082 : Bool) \u2192 Set where\n \u27e6true\u27e7 : \u27e6Bool\u27e7 true true\n \u27e6false\u27e7 : \u27e6Bool\u27e7 false false\n\nprivate\n module \u27e6Bool\u27e7-Internals where\n refl : Reflexive \u27e6Bool\u27e7\n refl {true} = \u27e6true\u27e7\n refl {false} = \u27e6false\u27e7\n\n sym : Symmetric \u27e6Bool\u27e7\n sym \u27e6true\u27e7 = \u27e6true\u27e7\n sym \u27e6false\u27e7 = \u27e6false\u27e7\n\n trans : Transitive \u27e6Bool\u27e7\n trans \u27e6true\u27e7 = id\n trans \u27e6false\u27e7 = id\n\n subst : \u2200 {\u2113} \u2192 Substitutive \u27e6Bool\u27e7 \u2113\n subst _ \u27e6true\u27e7 = id\n subst _ \u27e6false\u27e7 = id\n\n _\u225f_ : Decidable \u27e6Bool\u27e7\n true \u225f true = yes \u27e6true\u27e7\n false \u225f false = yes \u27e6false\u27e7\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence \u27e6Bool\u27e7\n isEquivalence = record { refl = refl; sym = sym; trans = trans }\n\n isDecEquivalence : IsDecEquivalence \u27e6Bool\u27e7\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = \u27e6Bool\u27e7; isDecEquivalence = isDecEquivalence }\n\n equality : Equality \u27e6Bool\u27e7\n equality = record { isEquivalence = isEquivalence; subst = subst }\n\nmodule \u27e6Bool\u27e7-Props where\n open \u27e6Bool\u27e7-Internals public using (subst; decSetoid; equality)\n open DecSetoid decSetoid public\n open Equality equality public hiding (subst; isEquivalence; refl; reflexive; sym; trans)\n\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6Bool\u27e7 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 A\u1d63) if_then_else_ if_then_else_\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6if\u27e8_\u27e9_then_else_\u27e7 _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 : \u2200 {\u2113\u2081 \u2113\u2082 \u2113\u1d63} \u2192\n (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 B\u1d63 \u2236 \u27e6Set\u27e7 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e9\u27e6\u2192\u27e7\n \u27e8 b\u1d63 \u2236 \u27e6Bool\u27e7 \u27e9\u27e6\u2192\u27e7 A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 \u27e6if\u27e8 \u27e6Set\u27e7 _ \u27e9 b\u1d63 then A\u1d63 else B\u1d63 \u27e7)\n If\u2032_then_else_ If\u2032_then_else_\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6true\u27e7 x\u1d63 _ = x\u1d63\n\u27e6If\u2032\u27e8_,_\u27e9_then_else_\u27e7 _ _ \u27e6false\u27e7 _ x\u1d63 = x\u1d63\n\n_==_ : (x y : Bool) \u2192 Bool\ntrue == true = true\ntrue == false = false\nfalse == true = false\nfalse == false = true\n\nmodule == where\n _\u2248_ : (x y : Bool) \u2192 Set\n x \u2248 y = T (x == y)\n\n refl : Reflexive _\u2248_\n refl {true} = _\n refl {false} = _\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {true} {true} _ = id\n subst _ {false} {false} _ = id\n subst _ {true} {false} ()\n subst _ {false} {true} ()\n\n sym : Symmetric _\u2248_\n sym {x} {y} eq = subst (\u03bb y \u2192 y \u2248 x) {x} {y} eq (refl {x})\n\n trans : Transitive _\u2248_\n trans {x} {y} {z} x\u2248y y\u2248z = subst (_\u2248_ x) {y} {z} y\u2248z x\u2248y\n\n _\u225f_ : Decidable _\u2248_\n true \u225f true = yes _\n false \u225f false = yes _\n true \u225f false = no (\u03bb())\n false \u225f true = no (\u03bb())\n\n isEquivalence : IsEquivalence _\u2248_\n isEquivalence = record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n\n isDecEquivalence : IsDecEquivalence _\u2248_\n isDecEquivalence = record { isEquivalence = isEquivalence; _\u225f_ = _\u225f_ }\n\n setoid : Setoid _ _\n setoid = record { Carrier = Bool; _\u2248_ = _\u2248_ ; isEquivalence = isEquivalence }\n\n decSetoid : DecSetoid _ _\n decSetoid = record { Carrier = Bool; _\u2248_ = _\u2248_; isDecEquivalence = isDecEquivalence }\n\nmodule \u27e6Bool\u27e7-Reasoning = Setoid-Reasoning \u27e6Bool\u27e7-Props.setoid\n\nopen Data.Bool public\n\n\u27e6true\u27e7\u2032 : \u2200 {b} \u2192 T b \u2192 \u27e6Bool\u27e7 true b\n\u27e6true\u27e7\u2032 {true} _ = \u27e6true\u27e7\n\u27e6true\u27e7\u2032 {false} ()\n\n\u27e6false\u27e7\u2032 : \u2200 {b} \u2192 T (not b) \u2192 \u27e6Bool\u27e7 false b\n\u27e6false\u27e7\u2032 {true} ()\n\u27e6false\u27e7\u2032 {false} _ = \u27e6false\u27e7\n\nT\u2227 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T b\u2082 \u2192 T (b\u2081 \u2227 b\u2082)\nT\u2227 p q = _\u27e8$\u27e9_ (from B.T-\u2227) (p , q)\n\nT\u2227\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2081\nT\u2227\u2081 = proj\u2081 \u2218 _\u27e8$\u27e9_ (to B.T-\u2227)\n\nT\u2227\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2227 b\u2082) \u2192 T b\u2082\nT\u2227\u2082 {b\u2081} = proj\u2082 \u2218 _\u27e8$\u27e9_ (to (B.T-\u2227 {b\u2081}))\n\nT\u2228'\u228e : \u2200 {b\u2081 b\u2082} \u2192 T (b\u2081 \u2228 b\u2082) \u2192 T b\u2081 \u228e T b\u2082\nT\u2228'\u228e {b\u2081} = _\u27e8$\u27e9_ (to (B.T-\u2228 {b\u2081}))\n\nT\u2228\u2081 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2081 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2081 = _\u27e8$\u27e9_ (from B.T-\u2228) \u2218 inj\u2081\n\nT\u2228\u2082 : \u2200 {b\u2081 b\u2082} \u2192 T b\u2082 \u2192 T (b\u2081 \u2228 b\u2082)\nT\u2228\u2082 {b\u2081} = _\u27e8$\u27e9_ (from (B.T-\u2228 {b\u2081})) \u2218 inj\u2082\n\nT'not'\u00ac : \u2200 {b} \u2192 T (not b) \u2192 \u00ac (T b)\nT'not'\u00ac {false} _ = \u03bb()\nT'not'\u00ac {true} ()\n\nT'\u00ac'not : \u2200 {b} \u2192 \u00ac (T b) \u2192 T (not b)\nT'\u00ac'not {true} f = f _\nT'\u00ac'not {false} _ = _\n\nTdec : \u2200 b \u2192 Dec (T b)\nTdec true = yes _\nTdec false = no \u03bb()\n\nde-morgan : \u2200 x y \u2192 not (x \u2228 y) \u2261 not x \u2227 not y\nde-morgan true _ = \u2261.refl\nde-morgan false _ = \u2261.refl\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"ce8def0c5d4f551bb474ae0b1a721a630fb31baa","subject":"Added ++-Stream.","message":"Added ++-Stream.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Data\/Stream\/PropertiesI.agda","new_file":"src\/fot\/FOTC\/Data\/Stream\/PropertiesI.agda","new_contents":"------------------------------------------------------------------------------\n-- Streams properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Stream.PropertiesI where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Base.List.PropertiesI\nopen import FOTC.Data.Conat\nopen import FOTC.Data.Conat.Equality\nopen import FOTC.Data.List\nopen import FOTC.Data.List.PropertiesI\nopen import FOTC.Data.Stream\n\n-----------------------------------------------------------------------------\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Stream predicate is also a pre-fixed point of the functional\n-- StreamF, i.e.\n--\n-- StreamF Stream \u2264 Stream (see FOTC.Data.Stream.Type).\nStream-pre-fixed : \u2200 {xs} \u2192\n (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Stream xs') \u2192\n Stream xs\nStream-pre-fixed {xs} h = Stream-coind (\u03bb ys \u2192 ys \u2261 ys) h' refl\n where\n h' : xs \u2261 xs \u2192 \u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 xs' \u2261 xs'\n h' _ with h\n ... | x' , xs' , prf , _ = x' , xs' , prf , refl\n\n++-Stream : \u2200 {xs ys} \u2192 Stream xs \u2192 Stream ys \u2192 Stream (xs ++ ys)\n++-Stream {xs} {ys} Sxs Sys with Stream-unf Sxs\n... | x' , xs' , prf , Sxs' = subst Stream prf\u2081 prf\u2082\n where\n prf\u2081 : x' \u2237 (xs' ++ ys) \u2261 xs ++ ys\n prf\u2081 = trans (sym (++-\u2237 x' xs' ys)) (++-leftCong (sym prf))\n\n -- TODO (15 December 2013): Why the termination checker accepts the\n -- recursive called ++-Stream_Sxs'_Sys?\n prf\u2082 : Stream (x' \u2237 xs' ++ ys)\n prf\u2082 = Stream-pre-fixed (x' , xs' ++ ys , refl , ++-Stream Sxs' Sys)\n\ntailS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\ntailS h with Stream-unf h\n... | x' , xs' , prf , Sxs' =\n subst Stream (sym (\u2227-proj\u2082 (\u2237-injective prf))) Sxs'\n\nstreamLength : \u2200 {xs} \u2192 Stream xs \u2192 length xs \u2248N \u221e\nstreamLength {xs} Sxs = \u2248N-coind (\u03bb m _ \u2192 m \u2261 m) h refl\n where\n\n h : length xs \u2261 length xs \u2192 length xs \u2261 zero \u2227 \u221e \u2261 zero\n \u2228 (\u2203[ m ] \u2203[ n ] length xs \u2261 succ\u2081 m \u2227 \u221e \u2261 succ\u2081 n \u2227 m \u2261 m)\n h _ with Stream-unf Sxs\n ... | x' , xs' , xs\u2261x'\u2237xs' , _ = inj\u2082 (length xs' , \u221e , prf , \u221e-eq , refl)\n where\n prf : length xs \u2261 succ\u2081 (length xs')\n prf = trans (lengthCong xs\u2261x'\u2237xs') (length-\u2237 x' xs')\n","old_contents":"------------------------------------------------------------------------------\n-- Streams properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Data.Stream.PropertiesI where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Base.List.PropertiesI\nopen import FOTC.Data.Conat\nopen import FOTC.Data.Conat.Equality\nopen import FOTC.Data.List\nopen import FOTC.Data.List.PropertiesI\nopen import FOTC.Data.Stream\n\n-----------------------------------------------------------------------------\n-- Because a greatest post-fixed point is a fixed-point, then the\n-- Stream predicate is also a pre-fixed point of the functional\n-- StreamF, i.e.\n--\n-- StreamF Stream \u2264 Stream (see FOTC.Data.Stream.Type).\nStream-pre-fixed : \u2200 {xs} \u2192\n (\u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 Stream xs') \u2192\n Stream xs\nStream-pre-fixed {xs} h = Stream-coind (\u03bb ys \u2192 ys \u2261 ys) h' refl\n where\n h' : xs \u2261 xs \u2192 \u2203[ x' ] \u2203[ xs' ] xs \u2261 x' \u2237 xs' \u2227 xs' \u2261 xs'\n h' _ with h\n ... | x' , xs' , prf , _ = x' , xs' , prf , refl\n\ntailS : \u2200 {x xs} \u2192 Stream (x \u2237 xs) \u2192 Stream xs\ntailS h with Stream-unf h\n... | x' , xs' , prf , Sxs' =\n subst Stream (sym (\u2227-proj\u2082 (\u2237-injective prf))) Sxs'\n\nstreamLength : \u2200 {xs} \u2192 Stream xs \u2192 length xs \u2248N \u221e\nstreamLength {xs} Sxs = \u2248N-coind (\u03bb m _ \u2192 m \u2261 m) h refl\n where\n\n h : length xs \u2261 length xs \u2192 length xs \u2261 zero \u2227 \u221e \u2261 zero\n \u2228 (\u2203[ m ] \u2203[ n ] length xs \u2261 succ\u2081 m \u2227 \u221e \u2261 succ\u2081 n \u2227 m \u2261 m)\n h _ with Stream-unf Sxs\n ... | x' , xs' , xs\u2261x'\u2237xs' , _ = inj\u2082 (length xs' , \u221e , prf , \u221e-eq , refl)\n where\n prf : length xs \u2261 succ\u2081 (length xs')\n prf = trans (lengthCong xs\u2261x'\u2237xs') (length-\u2237 x' xs')\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"7a9286f38f38e747e7ba0c4711b3af9aa7b0d9ac","subject":"flipbased: waste and weaken","message":"flipbased: waste and weaken\n","repos":"crypto-agda\/crypto-agda","old_file":"flipbased.agda","new_file":"flipbased.agda","new_contents":"open import Algebra\nimport Level as L\nopen L using () renaming (_\u2294_ to _L\u2294_)\nopen import Function hiding (_\u27e8_\u27e9_)\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Bool\nopen import Data.Unit using (\u22a4)\nopen import Data.Nat.Properties\nopen import Data.Product using (proj\u2081; proj\u2082; _,_; swap; _\u00d7_)\nopen import Data.Bits\nopen import Data.Bool\nopen import Data.Vec using (Vec; []; _\u2237_; take; drop; head; tail) renaming (map to vmap)\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_)\n\nmodule flipbased\n (\u21ba : \u2200 {a} \u2192 \u2115 \u2192 Set a \u2192 Set a)\n (toss : \u21ba 1 Bit)\n (return\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A)\n (map\u21ba : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B)\n (join\u21ba : \u2200 {n\u2081 n\u2082 a} {A : Set a} \u2192 \u21ba n\u2081 (\u21ba n\u2082 A) \u2192 \u21ba (n\u2081 + n\u2082) A)\n where\n\nCoins = \u2115\n\n-- If you are not allowed to toss any coin, then you are deterministic.\nDet : \u2200 {a} \u2192 Set a \u2192 Set a\nDet = \u21ba 0\n\n-- An experiment\nEXP : \u2115 \u2192 Set\nEXP n = \u21ba n Bit\n\n-- A guessing game\n\u2141? : \u2200 c \u2192 Set\n\u2141? c = Bit \u2192 EXP c\n\nreturn\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\nreturn\u1d30 = return\u21ba\n\npure\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\npure\u1d30 = return\u1d30\n\ncoerce : \u2200 {m n a} {A : Set a} \u2192 m \u2261 n \u2192 \u21ba m A \u2192 \u21ba n A\ncoerce \u2261.refl = id\n\n_>>=_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 A \u2192 (A \u2192 \u21ba n\u2082 B) \u2192 \u21ba (n\u2081 + n\u2082) B\n_>>=_ x f = join\u21ba (map\u21ba f x)\n\n_=<<_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n (A \u2192 \u21ba n\u2081 B) \u2192 \u21ba n\u2082 A \u2192 \u21ba (n\u2081 + n\u2082) B\n_=<<_ {n\u2081} {n\u2082} rewrite \u2115\u00b0.+-comm n\u2081 n\u2082 = flip _>>=_\n\n_>>_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 A \u2192 \u21ba n\u2082 B \u2192 \u21ba (n\u2081 + n\u2082) B\n_>>_ {n\u2081} x y = x >>= const y\n\n_>=>_ : \u2200 {n\u2081 n\u2082 a b c} {A : Set a} {B : Set b} {C : Set c}\n \u2192 (A \u2192 \u21ba n\u2081 B) \u2192 (B \u2192 \u21ba n\u2082 C) \u2192 A \u2192 \u21ba (n\u2081 + n\u2082) C\n(f >=> g) x = f x >>= g\n\nwaste : \u2200 n \u2192 \u21ba n \u22a4\nwaste n = return\u21ba {n} _\n\nweaken : \u2200 m {n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba (m + n) A\nweaken m x = waste m >> x\n\nweaken\u2032 : \u2200 {m n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba (n + m) A\nweaken\u2032 x = x >>= return\u21ba\n\nweaken\u2264 : \u2200 {m n a} {A : Set a} \u2192 m \u2264 n \u2192 \u21ba m A \u2192 \u21ba n A\nweaken\u2264 pf x with \u2264\u21d2\u2203 pf\n... | k , \u2261.refl = weaken\u2032 x\n\npure\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A\npure\u21ba = return\u21ba\n\n-- Weakened version of toss\ntoss\u1d42 : \u2200 {n} \u2192 \u21ba (1 + n) Bit\ntoss\u1d42 = toss >>= return\u21ba\n\n_\u25b9\u21ba_ : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 \u21ba n A \u2192 (A \u2192 B) \u2192 \u21ba n B\nx \u25b9\u21ba f = map\u21ba f x\n\n\u27ea_\u27eb : \u2200 {n} {a} {A : Set a} \u2192 A \u2192 \u21ba n A\n\u27ea_\u27eb = pure\u21ba\n\n\u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n\u27ea_\u27eb\u1d30 = pure\u1d30\n\n\u27ea_\u00b7_\u27eb : \u2200 {a b} {A : Set a} {B : Set b} {n} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B\n\u27ea f \u00b7 x \u27eb = map\u21ba f x\n\ninfixl 4 _\u229b_\n_\u229b_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 (A \u2192 B) \u2192 \u21ba n\u2082 A \u2192 \u21ba (n\u2081 + n\u2082) B\n_\u229b_ {n\u2081} mf mx = mf >>= \u03bb f \u2192 \u27ea f \u00b7 mx \u27eb \n\n\u27ea_\u00b7_\u00b7_\u27eb : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba (m + n) C\n\u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n\u27ea_\u00b7_\u00b7_\u00b7_\u27eb : \u2200 {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} {m n o} \u2192\n (A \u2192 B \u2192 C \u2192 D) \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba o C \u2192 \u21ba (m + n + o) D\n\u27ea f \u00b7 x \u00b7 y \u00b7 z \u27eb = map\u21ba f x \u229b y \u229b z\n\nchoose : \u2200 {n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba n A \u2192 \u21ba (suc n) A\nchoose x y = toss >>= \u03bb b \u2192 if b then x else y\n\nzip\u21ba : \u2200 {c\u2080 c\u2081 a b} {A : Set a} {B : Set b} \u2192 \u21ba c\u2080 A \u2192 \u21ba c\u2081 B \u2192 \u21ba (c\u2080 + c\u2081) (A \u00d7 B)\nzip\u21ba x y = \u27ea _,_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8,\u27e9_ : \u2200 {a b} {A : Set a} {B : Set b} {m n} \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba (m + n) (A \u00d7 B)\n_\u27e8,\u27e9_ = zip\u21ba\n\n_\u27e8xor\u27e9_ : \u2200 {n\u2081 n\u2082} \u2192 \u21ba n\u2081 Bit \u2192 \u21ba n\u2082 Bit \u2192 \u21ba (n\u2081 + n\u2082) Bit\nx \u27e8xor\u27e9 y = \u27ea _xor_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8\u2295\u27e9_ : \u2200 {n\u2081 n\u2082 m} \u2192 \u21ba n\u2081 (Bits m) \u2192 \u21ba n\u2082 (Bits m) \u2192 \u21ba (n\u2081 + n\u2082) (Bits m)\nx \u27e8\u2295\u27e9 y = \u27ea _\u2295_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8\u2237\u27e9_ : \u2200 {n\u2081 n\u2082 m a} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 (Vec A m) \u2192 \u21ba (n\u2081 + n\u2082) (Vec A (suc m))\nx \u27e8\u2237\u27e9 xs = \u27ea _\u2237_ \u00b7 x \u00b7 xs \u27eb\n\n_\u27e8==\u27e9_ : \u2200 {n\u2081 n\u2082 m} \u2192 \u21ba n\u2081 (Bits m) \u2192 \u21ba n\u2082 (Bits m) \u2192 \u21ba (n\u2081 + n\u2082) Bit\nx \u27e8==\u27e9 y = \u27ea _==_ \u00b7 x \u00b7 y \u27eb\n\nT\u21ba : \u2200 {k} \u2192 \u21ba k Bit \u2192 \u21ba k Set\nT\u21ba p = \u27ea T \u00b7 p \u27eb\n\nreplicate\u21ba : \u2200 {n m} {a} {A : Set a} \u2192 \u21ba m A \u2192 \u21ba (n * m) (Vec A n)\nreplicate\u21ba {zero} _ = \u27ea [] \u27eb\nreplicate\u21ba {suc _} x = x \u27e8\u2237\u27e9 replicate\u21ba x\n\nnot\u21ba : \u2200 {n} \u2192 EXP n \u2192 EXP n\nnot\u21ba = map\u21ba not\n\nrandom : \u2200 {n} \u2192 \u21ba n (Bits n)\n-- random = coerce ? (replicate\u21ba toss) -- specialized version for now to avoid coerce\nrandom {zero} = \u27ea [] \u27eb\nrandom {suc _} = \u27ea _\u2237_ \u00b7 toss \u00b7 random \u27eb\n\nrandomTbl : \u2200 m n \u2192 \u21ba (2^ m * n) (Vec (Bits n) (2^ m))\nrandomTbl m n = replicate\u21ba random\n\nrandomFunFromTbl : \u2200 m n \u2192 \u21ba (2^ m * n) (Bits m \u2192 Bits n)\nrandomFunFromTbl m n = \u27ea funFromTbl \u00b7 randomTbl m n \u27eb\n\nrandomFunExt : \u2200 {n k a} {A : Set a} \u2192 \u21ba k (Bits n \u2192 A) \u2192 \u21ba (k + k) (Bits (suc n) \u2192 A)\nrandomFunExt f = \u27ea comb \u00b7 f \u00b7 f \u27eb\n where comb = \u03bb g\u2081 g\u2082 xs \u2192 (if head xs then g\u2081 else g\u2082) (tail xs)\n\nrandomFun : \u2200 m n \u2192 \u21ba (2^\u27e8 m \u27e9* n) (Bits m \u2192 Bits n)\nrandomFun zero _ = \u27ea const \u00b7 random \u27eb\nrandomFun (suc m) _ = randomFunExt (randomFun m _)\n\nrecord ProgEquiv a \u2113 : Set (L.suc \u2113 L\u2294 L.suc a) where\n infix 2 _\u2248_ _\u224b_\n field\n _\u2248_ : \u2200 {n} {A : Set a} \u2192 Rel (\u21ba n A) \u2113\n\n refl : \u2200 {n A} \u2192 Reflexive {A = \u21ba n A} _\u2248_\n sym : \u2200 {n A} \u2192 Symmetric {A = \u21ba n A} _\u2248_\n\n -- not strictly transitive\n\n reflexive : \u2200 {n A} \u2192 _\u2261_ \u21d2 _\u2248_ {n} {A}\n reflexive \u2261.refl = refl\n\n _\u224b_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 A \u2192 Set \u2113\n _\u224b_ {n\u2081} {n\u2082} p\u2081 p\u2082 = _\u2248_ {n = n\u2081 \u2294 n\u2082} (weaken\u2264 (m\u2264m\u2294n _ _) p\u2081) (weaken\u2264 (m\u2264n\u2294m _ n\u2081) p\u2082)\n where m\u2264n\u2294m : \u2200 m n \u2192 m \u2264 n \u2294 m\n m\u2264n\u2294m m n rewrite \u2294\u00b0.+-comm n m = m\u2264m\u2294n m n\n\n -- Another name for _\u224b_\n _looks_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 A \u2192 Set \u2113\n _looks_ = _\u224b_\n\nmodule WithEquiv (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n\n SecPRG : \u2200 {k n} (prg : (key : Bits k) \u2192 Bits n) \u2192 Set\n SecPRG prg = this looks random where this = \u27ea prg \u00b7 random \u27eb\n\n record PRG k n : Set where\n constructor _,_\n field\n prg : Bits k \u2192 Bits n\n sec : SecPRG prg\n\n OneTimeSecPRF : \u2200 {k m n} (prf : (key : Bits k) (msg : Bits m) \u2192 Bits n) \u2192 Set\n OneTimeSecPRF prf = \u2200 {xs} \u2192 let this = \u27ea prf \u00b7 random \u00b7 \u27ea xs \u27eb\u1d30 \u27eb in\n this looks random\n\n record PRF k m n : Set where\n constructor _,_\n field\n prf : Bits k \u2192 Bits m \u2192 Bits n\n sec : OneTimeSecPRF prf\n\nOTP : \u2200 {n} \u2192 Bits n \u2192 Bits n \u2192 Bits n\nOTP key msg = key \u2295 msg\n\ninit : \u2200 {k a} {A : Set a} \u2192 (Bits k \u2192 A) \u2192 \u21ba k A\ninit f = \u27ea f \u00b7 random \u27eb\n\nmodule Examples (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n open WithEquiv progEq\n\n left-unit-law = \u2200 {A B : Set} {n} {x : A} {f : A \u2192 \u21ba n B} \u2192 return\u1d30 x >>= f \u2248 f x\n\n right-unit-law = \u2200 {A : Set} {n} {x : \u21ba n A} \u2192 return\u1d30 =<< x \u2248 x\n\n assoc-law = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : \u21ba n\u2081 A} {f : A \u2192 \u21ba n\u2082 B} {g : B \u2192 \u21ba n\u2083 C}\n \u2192 (x >>= f) >>= g \u224b x >>= (\u03bb x \u2192 f x >>= g)\n\n assoc-law\u2032 = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : \u21ba n\u2081 A} {f : A \u2192 \u21ba n\u2082 B} {g : B \u2192 \u21ba n\u2083 C}\n \u2192 (x >>= f) >>= g \u2248 coerce (\u2261.sym (\u2115\u00b0.+-assoc n\u2081 n\u2082 n\u2083)) (x >>= (\u03bb x \u2192 f x >>= g))\n\n ex\u2081 = \u2200 {x} \u2192 toss \u27e8xor\u27e9 \u27ea x \u27eb\u1d30 \u2248 \u27ea x \u27eb\n\n ex\u2082 = p \u2248 map\u21ba swap p where p = toss \u27e8,\u27e9 toss\n\n ex\u2083 = \u2200 {n} \u2192 OneTimeSecPRF {n} OTP\n\n ex\u2084 = \u2200 {k n} (prg : PRG k n) \u2192 OneTimeSecPRF (\u03bb key xs \u2192 xs \u2295 PRG.prg prg key)\n\n ex\u2085 = \u2200 {k n} \u2192 PRG k n \u2192 PRF k n n\n","old_contents":"open import Algebra\nimport Level as L\nopen L using () renaming (_\u2294_ to _L\u2294_)\nopen import Function hiding (_\u27e8_\u27e9_)\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Bool\nopen import Data.Nat.Properties\nopen import Data.Product using (proj\u2081; proj\u2082; _,_; swap; _\u00d7_)\nopen import Data.Bits\nopen import Data.Bool\nopen import Data.Vec using (Vec; []; _\u2237_; take; drop; head; tail) renaming (map to vmap)\nopen import Relation.Binary\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_)\n\nmodule flipbased\n (\u21ba : \u2200 {a} \u2192 \u2115 \u2192 Set a \u2192 Set a)\n (toss : \u21ba 1 Bit)\n (return\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A)\n (map\u21ba : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B)\n (join\u21ba : \u2200 {n\u2081 n\u2082 a} {A : Set a} \u2192 \u21ba n\u2081 (\u21ba n\u2082 A) \u2192 \u21ba (n\u2081 + n\u2082) A)\n where\n\nCoins = \u2115\n\n-- If you are not allowed to toss any coin, then you are deterministic.\nDet : \u2200 {a} \u2192 Set a \u2192 Set a\nDet = \u21ba 0\n\n-- An experiment\nEXP : \u2115 \u2192 Set\nEXP n = \u21ba n Bit\n\n-- A guessing game\n\u2141? : \u2200 c \u2192 Set\n\u2141? c = Bit \u2192 EXP c\n\nreturn\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\nreturn\u1d30 = return\u21ba\n\npure\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\npure\u1d30 = return\u1d30\n\ncoerce : \u2200 {m n a} {A : Set a} \u2192 m \u2261 n \u2192 \u21ba m A \u2192 \u21ba n A\ncoerce \u2261.refl = id\n\n_>>=_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 A \u2192 (A \u2192 \u21ba n\u2082 B) \u2192 \u21ba (n\u2081 + n\u2082) B\n_>>=_ x f = join\u21ba (map\u21ba f x)\n\n_=<<_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n (A \u2192 \u21ba n\u2081 B) \u2192 \u21ba n\u2082 A \u2192 \u21ba (n\u2081 + n\u2082) B\n_=<<_ {n\u2081} {n\u2082} rewrite \u2115\u00b0.+-comm n\u2081 n\u2082 = flip _>>=_\n\n_>>_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 A \u2192 \u21ba n\u2082 B \u2192 \u21ba (n\u2081 + n\u2082) B\n_>>_ {n\u2081} x y = x >>= const y\n\n_>=>_ : \u2200 {n\u2081 n\u2082 a b c} {A : Set a} {B : Set b} {C : Set c}\n \u2192 (A \u2192 \u21ba n\u2081 B) \u2192 (B \u2192 \u21ba n\u2082 C) \u2192 A \u2192 \u21ba (n\u2081 + n\u2082) C\n(f >=> g) x = f x >>= g\n\nweaken : \u2200 m {n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba (m + n) A\nweaken m x = return\u21ba {m} 0 >> x\n\nweaken\u2032 : \u2200 {m n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba (n + m) A\nweaken\u2032 x = x >>= return\u21ba\n\nweaken\u2264 : \u2200 {m n a} {A : Set a} \u2192 m \u2264 n \u2192 \u21ba m A \u2192 \u21ba n A\nweaken\u2264 pf x with \u2264\u21d2\u2203 pf\n... | k , \u2261.refl = weaken\u2032 x\n\npure\u21ba : \u2200 {n a} {A : Set a} \u2192 A \u2192 \u21ba n A\npure\u21ba = return\u21ba\n\n-- Weakened version of toss\ntoss\u1d42 : \u2200 {n} \u2192 \u21ba (1 + n) Bit\ntoss\u1d42 = toss >>= return\u21ba\n\n_\u25b9\u21ba_ : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 \u21ba n A \u2192 (A \u2192 B) \u2192 \u21ba n B\nx \u25b9\u21ba f = map\u21ba f x\n\n\u27ea_\u27eb : \u2200 {n} {a} {A : Set a} \u2192 A \u2192 \u21ba n A\n\u27ea_\u27eb = pure\u21ba\n\n\u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n\u27ea_\u27eb\u1d30 = pure\u1d30\n\n\u27ea_\u00b7_\u27eb : \u2200 {a b} {A : Set a} {B : Set b} {n} \u2192 (A \u2192 B) \u2192 \u21ba n A \u2192 \u21ba n B\n\u27ea f \u00b7 x \u27eb = map\u21ba f x\n\ninfixl 4 _\u229b_\n_\u229b_ : \u2200 {n\u2081 n\u2082 a b} {A : Set a} {B : Set b} \u2192\n \u21ba n\u2081 (A \u2192 B) \u2192 \u21ba n\u2082 A \u2192 \u21ba (n\u2081 + n\u2082) B\n_\u229b_ {n\u2081} mf mx = mf >>= \u03bb f \u2192 \u27ea f \u00b7 mx \u27eb \n\n\u27ea_\u00b7_\u00b7_\u27eb : \u2200 {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba (m + n) C\n\u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n\u27ea_\u00b7_\u00b7_\u00b7_\u27eb : \u2200 {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} {m n o} \u2192\n (A \u2192 B \u2192 C \u2192 D) \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba o C \u2192 \u21ba (m + n + o) D\n\u27ea f \u00b7 x \u00b7 y \u00b7 z \u27eb = map\u21ba f x \u229b y \u229b z\n\nchoose : \u2200 {n a} {A : Set a} \u2192 \u21ba n A \u2192 \u21ba n A \u2192 \u21ba (suc n) A\nchoose x y = toss >>= \u03bb b \u2192 if b then x else y\n\nzip\u21ba : \u2200 {c\u2080 c\u2081 a b} {A : Set a} {B : Set b} \u2192 \u21ba c\u2080 A \u2192 \u21ba c\u2081 B \u2192 \u21ba (c\u2080 + c\u2081) (A \u00d7 B)\nzip\u21ba x y = \u27ea _,_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8,\u27e9_ : \u2200 {a b} {A : Set a} {B : Set b} {m n} \u2192 \u21ba m A \u2192 \u21ba n B \u2192 \u21ba (m + n) (A \u00d7 B)\n_\u27e8,\u27e9_ = zip\u21ba\n\n_\u27e8xor\u27e9_ : \u2200 {n\u2081 n\u2082} \u2192 \u21ba n\u2081 Bit \u2192 \u21ba n\u2082 Bit \u2192 \u21ba (n\u2081 + n\u2082) Bit\nx \u27e8xor\u27e9 y = \u27ea _xor_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8\u2295\u27e9_ : \u2200 {n\u2081 n\u2082 m} \u2192 \u21ba n\u2081 (Bits m) \u2192 \u21ba n\u2082 (Bits m) \u2192 \u21ba (n\u2081 + n\u2082) (Bits m)\nx \u27e8\u2295\u27e9 y = \u27ea _\u2295_ \u00b7 x \u00b7 y \u27eb\n\n_\u27e8\u2237\u27e9_ : \u2200 {n\u2081 n\u2082 m a} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 (Vec A m) \u2192 \u21ba (n\u2081 + n\u2082) (Vec A (suc m))\nx \u27e8\u2237\u27e9 xs = \u27ea _\u2237_ \u00b7 x \u00b7 xs \u27eb\n\n_\u27e8==\u27e9_ : \u2200 {n\u2081 n\u2082 m} \u2192 \u21ba n\u2081 (Bits m) \u2192 \u21ba n\u2082 (Bits m) \u2192 \u21ba (n\u2081 + n\u2082) Bit\nx \u27e8==\u27e9 y = \u27ea _==_ \u00b7 x \u00b7 y \u27eb\n\nT\u21ba : \u2200 {k} \u2192 \u21ba k Bit \u2192 \u21ba k Set\nT\u21ba p = \u27ea T \u00b7 p \u27eb\n\nreplicate\u21ba : \u2200 {n m} {a} {A : Set a} \u2192 \u21ba m A \u2192 \u21ba (n * m) (Vec A n)\nreplicate\u21ba {zero} _ = \u27ea [] \u27eb\nreplicate\u21ba {suc _} x = x \u27e8\u2237\u27e9 replicate\u21ba x\n\nnot\u21ba : \u2200 {n} \u2192 EXP n \u2192 EXP n\nnot\u21ba = map\u21ba not\n\nrandom : \u2200 {n} \u2192 \u21ba n (Bits n)\n-- random = coerce ? (replicate\u21ba toss) -- specialized version for now to avoid coerce\nrandom {zero} = \u27ea [] \u27eb\nrandom {suc _} = \u27ea _\u2237_ \u00b7 toss \u00b7 random \u27eb\n\nrandomTbl : \u2200 m n \u2192 \u21ba (2^ m * n) (Vec (Bits n) (2^ m))\nrandomTbl m n = replicate\u21ba random\n\nrandomFunFromTbl : \u2200 m n \u2192 \u21ba (2^ m * n) (Bits m \u2192 Bits n)\nrandomFunFromTbl m n = \u27ea funFromTbl \u00b7 randomTbl m n \u27eb\n\nrandomFunExt : \u2200 {n k a} {A : Set a} \u2192 \u21ba k (Bits n \u2192 A) \u2192 \u21ba (k + k) (Bits (suc n) \u2192 A)\nrandomFunExt f = \u27ea comb \u00b7 f \u00b7 f \u27eb\n where comb = \u03bb g\u2081 g\u2082 xs \u2192 (if head xs then g\u2081 else g\u2082) (tail xs)\n\nrandomFun : \u2200 m n \u2192 \u21ba (2^\u27e8 m \u27e9* n) (Bits m \u2192 Bits n)\nrandomFun zero _ = \u27ea const \u00b7 random \u27eb\nrandomFun (suc m) _ = randomFunExt (randomFun m _)\n\nrecord ProgEquiv a \u2113 : Set (L.suc \u2113 L\u2294 L.suc a) where\n infix 2 _\u2248_ _\u224b_\n field\n _\u2248_ : \u2200 {n} {A : Set a} \u2192 Rel (\u21ba n A) \u2113\n\n refl : \u2200 {n A} \u2192 Reflexive {A = \u21ba n A} _\u2248_\n sym : \u2200 {n A} \u2192 Symmetric {A = \u21ba n A} _\u2248_\n\n -- not strictly transitive\n\n reflexive : \u2200 {n A} \u2192 _\u2261_ \u21d2 _\u2248_ {n} {A}\n reflexive \u2261.refl = refl\n\n _\u224b_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 A \u2192 Set \u2113\n _\u224b_ {n\u2081} {n\u2082} p\u2081 p\u2082 = _\u2248_ {n = n\u2081 \u2294 n\u2082} (weaken\u2264 (m\u2264m\u2294n _ _) p\u2081) (weaken\u2264 (m\u2264n\u2294m _ n\u2081) p\u2082)\n where m\u2264n\u2294m : \u2200 m n \u2192 m \u2264 n \u2294 m\n m\u2264n\u2294m m n rewrite \u2294\u00b0.+-comm n m = m\u2264m\u2294n m n\n\n -- Another name for _\u224b_\n _looks_ : \u2200 {n\u2081 n\u2082} {A : Set a} \u2192 \u21ba n\u2081 A \u2192 \u21ba n\u2082 A \u2192 Set \u2113\n _looks_ = _\u224b_\n\nmodule WithEquiv (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n\n SecPRG : \u2200 {k n} (prg : (key : Bits k) \u2192 Bits n) \u2192 Set\n SecPRG prg = this looks random where this = \u27ea prg \u00b7 random \u27eb\n\n record PRG k n : Set where\n constructor _,_\n field\n prg : Bits k \u2192 Bits n\n sec : SecPRG prg\n\n OneTimeSecPRF : \u2200 {k m n} (prf : (key : Bits k) (msg : Bits m) \u2192 Bits n) \u2192 Set\n OneTimeSecPRF prf = \u2200 {xs} \u2192 let this = \u27ea prf \u00b7 random \u00b7 \u27ea xs \u27eb\u1d30 \u27eb in\n this looks random\n\n record PRF k m n : Set where\n constructor _,_\n field\n prf : Bits k \u2192 Bits m \u2192 Bits n\n sec : OneTimeSecPRF prf\n\nOTP : \u2200 {n} \u2192 Bits n \u2192 Bits n \u2192 Bits n\nOTP key msg = key \u2295 msg\n\ninit : \u2200 {k a} {A : Set a} \u2192 (Bits k \u2192 A) \u2192 \u21ba k A\ninit f = \u27ea f \u00b7 random \u27eb\n\nmodule Examples (progEq : ProgEquiv L.zero L.zero) where\n open ProgEquiv progEq\n open WithEquiv progEq\n\n left-unit-law = \u2200 {A B : Set} {n} {x : A} {f : A \u2192 \u21ba n B} \u2192 return\u1d30 x >>= f \u2248 f x\n\n right-unit-law = \u2200 {A : Set} {n} {x : \u21ba n A} \u2192 return\u1d30 =<< x \u2248 x\n\n assoc-law = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : \u21ba n\u2081 A} {f : A \u2192 \u21ba n\u2082 B} {g : B \u2192 \u21ba n\u2083 C}\n \u2192 (x >>= f) >>= g \u224b x >>= (\u03bb x \u2192 f x >>= g)\n\n assoc-law\u2032 = \u2200 {A B C : Set} {n\u2081 n\u2082 n\u2083} {x : \u21ba n\u2081 A} {f : A \u2192 \u21ba n\u2082 B} {g : B \u2192 \u21ba n\u2083 C}\n \u2192 (x >>= f) >>= g \u2248 coerce (\u2261.sym (\u2115\u00b0.+-assoc n\u2081 n\u2082 n\u2083)) (x >>= (\u03bb x \u2192 f x >>= g))\n\n ex\u2081 = \u2200 {x} \u2192 toss \u27e8xor\u27e9 \u27ea x \u27eb\u1d30 \u2248 \u27ea x \u27eb\n\n ex\u2082 = p \u2248 map\u21ba swap p where p = toss \u27e8,\u27e9 toss\n\n ex\u2083 = \u2200 {n} \u2192 OneTimeSecPRF {n} OTP\n\n ex\u2084 = \u2200 {k n} (prg : PRG k n) \u2192 OneTimeSecPRF (\u03bb key xs \u2192 xs \u2295 PRG.prg prg key)\n\n ex\u2085 = \u2200 {k n} \u2192 PRG k n \u2192 PRF k n n\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"df41c005d15604256ca7d340975689955ae6729f","subject":"update protocols.agda","message":"update protocols.agda\n","repos":"crypto-agda\/protocols","old_file":"protocols.agda","new_file":"protocols.agda","new_contents":"module protocols where\nimport Control.Protocol\nimport Control.Protocol.Additive\nimport Control.Protocol.CLL\nimport Control.Protocol.ClientServer\nimport Control.Protocol.Examples\nimport Control.Protocol.Extend\nimport Control.Protocol.Lift\nimport Control.Protocol.MultiParty\nimport Control.Protocol.Multiplicative\nimport Control.Protocol.Relation\nimport Control.Protocol.Sequence\n","old_contents":"module protocols where\nimport Control.Protocol\nimport Control.Protocol.Additive\nimport Control.Protocol.CLL\nimport Control.Protocol.ClientServer\nimport Control.Protocol.Examples\nimport Control.Protocol.MultiParty\nimport Control.Protocol.Multiplicative\nimport Control.Protocol.Relation\nimport Control.Protocol.Sequence\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"c331f69b265880f6b98ff0ecc55dbbd4b5d2d695","subject":"Cosmetic change.","message":"Cosmetic change.\n\nIgnore-this: 364ef2c60263dc8dc66151b308396671\n\ndarcs-hash:20120119215014-3bd4e-c2fdcf5ffb49d2aa51fe222d53b7ffd098ac17d1.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/papers\/FoSSaCS-2012\/BottomBottom.agda","new_file":"notes\/papers\/FoSSaCS-2012\/BottomBottom.agda","new_contents":"-- Tested on 08 December 2011.\n\nmodule BottomBottom where\n\nopen import Common.Universe\nopen import Common.Data.Empty\n\npostulate bot\u2081 bot\u2082 : \u22a5\n{-# ATP prove bot\u2081 bot\u2082 #-}\n{-# ATP prove bot\u2082 bot\u2081 #-}\n\n-- $ agda2atp -i src -i notes\/papers\/FoSSaCS-2012\/ notes\/papers\/FoSSaCS-2012\/BottomBottom.agda\n-- Proving the conjecture in \/tmp\/BottomBottom.bot1_8.tptp ...\n-- Vampire 0.6 (revision 903) proved the conjecture in \/tmp\/BottomBottom.bot1_8.tptp\n-- Proving the conjecture in \/tmp\/BottomBottom.bot2_8.tptp ...\n-- Vampire 0.6 (revision 903) proved the conjecture in \/tmp\/BottomBottom.bot2_8.tptp\n","old_contents":"-- Tested on 08 December 2011.\n\nmodule BottomBottom where\n\nopen import Common.Universe\nopen import Common.Data.Empty\n\npostulate bot\u2081 bot\u2082 : \u22a5\n{-# ATP prove bot\u2081 bot\u2082 #-}\n{-# ATP prove bot\u2082 bot\u2081 #-}\n\n-- $ agda2atp -i src -i notes\/papers\/FoSSaCS-2012\/ notes\/papers\/FoSSaCS-2012\/BottomBottom.agda\n-- Proving the conjecture in \/tmp\/BottomBottom.bot1_8.tptp ...\n-- Vampire 0.6 (revision 903) proved the conjecture in \/tmp\/BottomBottom.bot1_8.tptp\n-- Proving the conjecture in \/tmp\/BottomBottom.bot2_8.tptp ...\n-- Vampire 0.6 (revision 903) proved the conjecture in \/tmp\/BottomBottom.bot2_8.tptp\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ff7cc70477ddaedcd1fcec0dbd330bbe0d8e9848","subject":"generic-zero-knowledge-interactive: Finishing DLog","message":"generic-zero-knowledge-interactive: Finishing DLog\n","repos":"crypto-agda\/crypto-agda","old_file":"generic-zero-knowledge-interactive.agda","new_file":"generic-zero-knowledge-interactive.agda","new_contents":"open import Data.Bool.NP as Bool hiding (check)\nopen import Data.Nat\nopen import Data.Maybe\nopen import Data.Product.NP\nopen import Data.Bits\nopen import Function.NP\nopen import Relation.Binary.PropositionalEquality.NP\n\nopen import sum\n\nmodule generic-zero-knowledge-interactive where\n\nprivate\n \u2605 : Set\u2081\n \u2605 = Set\n\n-- A random argument, this is only a formal notation to\n-- indicate that the argument is supposed to be picked\n-- at random uniformly. (do not confuse with our randomness\n-- monad).\nrecord \u21ba (A : \u2605) : \u2605 where\n constructor rand\n field get : A\n\nmodule M (Permutation : \u2605)\n (_\u207b\u00b9 : Endo Permutation)\n (sum\u03c0 : Sum Permutation)\n (\u03bc\u03c0 : SumProp sum\u03c0)\n\n (R\u209a-xtra : \u2605) -- extra prover\/adversary randomness\n (sumR\u209a-xtra : Sum R\u209a-xtra)\n (\u03bcR\u209a-xtra : SumProp sumR\u209a-xtra)\n\n (Problem : \u2605)\n (_==_ : Problem \u2192 Problem \u2192 Bit)\n (==-refl : \u2200 {pb} \u2192 (pb == pb) \u2261 true)\n (_\u2219P_ : Permutation \u2192 Endo Problem)\n (\u207b\u00b9-inverseP : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219P (\u03c0 \u2219P x) \u2261 x)\n\n (Solution : \u2605)\n (_\u2219S_ : Permutation \u2192 Endo Solution)\n (\u207b\u00b9-inverseS : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219S (\u03c0 \u2219S x) \u2261 x)\n\n (check : Problem \u2192 Solution \u2192 Bit)\n (check-\u2219 : \u2200 p s \u03c0 \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 check p s)\n\n (easy-pb : Permutation \u2192 Problem)\n (easy-sol : Permutation \u2192 Solution)\n (check-easy : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P easy-pb \u03c0) (\u03c0 \u2219S easy-sol \u03c0) \u2261 true)\n where\n\n -- prover\/adversary randomness\n R\u209a : \u2605\n R\u209a = Permutation \u00d7 R\u209a-xtra\n\n sumR\u209a : Sum R\u209a\n sumR\u209a = sum\u03c0 \u00d7Sum sumR\u209a-xtra\n\n \u03bcR\u209a : SumProp sumR\u209a\n \u03bcR\u209a = \u03bc\u03c0 \u00d7\u03bc \u03bcR\u209a-xtra\n\n R = Bit \u00d7 R\u209a\n\n sumR : Sum R\n sumR = sumBit \u00d7Sum sumR\u209a\n\n \u03bcR : SumProp sumR\n \u03bcR = \u03bcBit \u00d7\u03bc \u03bcR\u209a\n\n check-\u03c0 : Problem \u2192 Solution \u2192 R\u209a \u2192 Bit\n check-\u03c0 p s (\u03c0 , _) = check (\u03c0 \u2219P p) (\u03c0 \u2219S s)\n\n otp-\u2219-check : let #_ = count \u03bcR\u209a\n in\n \u2200 p\u2080 s\u2080 p\u2081 s\u2081 \u2192\n check p\u2080 s\u2080 \u2261 check p\u2081 s\u2081 \u2192\n #(check-\u03c0 p\u2080 s\u2080) \u2261 #(check-\u03c0 p\u2081 s\u2081)\n otp-\u2219-check p\u2080 s\u2080 p\u2081 s\u2081 check-pf =\n count-ext \u03bcR\u209a {f = check-\u03c0 p\u2080 s\u2080} {check-\u03c0 p\u2081 s\u2081} (\u03bb \u03c0,r \u2192\n check-\u03c0 p\u2080 s\u2080 \u03c0,r \u2261\u27e8 check-\u2219 p\u2080 s\u2080 (proj\u2081 \u03c0,r) \u27e9\n check p\u2080 s\u2080 \u2261\u27e8 check-pf \u27e9\n check p\u2081 s\u2081 \u2261\u27e8 sym (check-\u2219 p\u2081 s\u2081 (proj\u2081 \u03c0,r)) \u27e9\n check-\u03c0 p\u2081 s\u2081 \u03c0,r \u220e)\n where open \u2261-Reasoning\n\n #_ : (\u21ba (Bit \u00d7 Permutation \u00d7 R\u209a-xtra) \u2192 Bit) \u2192 \u2115\n # f = count \u03bcR (f \u2218 rand)\n\n _\u2261#_ : (f g : \u21ba (Bit \u00d7 R\u209a) \u2192 Bit) \u2192 \u2605\n f \u2261# g = # f \u2261 # g\n\n{-\n otp-\u2219 : let otp = \u03bb O pb s \u2192 count \u03bcR\u209a (\u03bb { (\u03c0 , _) \u2192 O (\u03c0 \u2219P pb) (\u03c0 \u2219S s) }) in\n \u2200 pb\u2080 s\u2080 pb\u2081 s\u2081 \u2192\n check pb\u2080 s\u2080 \u2261 check pb\u2081 s\u2081 \u2192\n (O : _ \u2192 _ \u2192 Bit) \u2192 otp O pb\u2080 s\u2080 \u2261 otp O pb\u2081 s\u2081\n otp-\u2219 pb\u2080 s\u2080 pb\u2081 s\u2081 check-pf O = {!(\u03bc\u03c0 \u00d7Sum-proj\u2082 \u03bcR\u209a-xtra ?!}\n-}\n Answer : Bit \u2192 \u2605\n Answer false{-0b-} = Permutation\n Answer true {-1b-} = Solution\n\n answer : Permutation \u2192 Solution \u2192 \u2200 b \u2192 Answer b\n answer \u03c0 _ false = \u03c0\n answer _ s true = s\n\n -- The prover is the advesary in the generic terminology,\n -- and the verifier is the challenger.\n DepProver : \u2605\n DepProver = Problem \u2192 \u21ba R\u209a \u2192 (b : Bit) \u2192 Problem \u00d7 Answer b\n\n Prover\u2080 : \u2605\n Prover\u2080 = Problem \u2192 \u21ba R\u209a \u2192 Problem \u00d7 Permutation\n\n Prover\u2081 : \u2605\n Prover\u2081 = Problem \u2192 \u21ba R\u209a \u2192 Problem \u00d7 Solution\n\n Prover : \u2605\n Prover = Prover\u2080 \u00d7 Prover\u2081\n\n prover : DepProver \u2192 Prover\n prover dpr = (\u03bb pb r \u2192 dpr pb r 0b) , (\u03bb pb r \u2192 dpr pb r 1b)\n\n depProver : Prover \u2192 DepProver\n depProver (pr\u2080 , pr\u2081) pb r false = pr\u2080 pb r\n depProver (pr\u2080 , pr\u2081) pb r true = pr\u2081 pb r\n\n -- Here we show that the explicit commitment step seems useless given\n -- the formalization. The verifier can \"trust\" the prover on the fact\n -- that any choice is going to be govern only by the problem and the\n -- randomness.\n module WithCommitment (Commitment : \u2605)\n (AnswerWC : Bit \u2192 \u2605)\n (reveal : \u2200 b \u2192 Commitment \u2192 AnswerWC b \u2192 Problem \u00d7 Answer b) where\n ProverWC = (Problem \u2192 R\u209a \u2192 Commitment)\n \u00d7 (Problem \u2192 R\u209a \u2192 (b : Bit) \u2192 AnswerWC b)\n\n depProver' : ProverWC \u2192 DepProver\n depProver' (pr\u2080 , pr\u2081) pb (rand r\u209a) b = reveal b (pr\u2080 pb r\u209a) (pr\u2081 pb r\u209a b)\n\n Verif : Problem \u2192 \u2200 b \u2192 Problem \u00d7 Answer b \u2192 Bit\n Verif pb false{-0b-} (\u03c0\u2219pb , \u03c0) = (\u03c0 \u2219P pb) == \u03c0\u2219pb\n Verif pb true {-1b-} (\u03c0\u2219pb , \u03c0\u2219s) = check \u03c0\u2219pb \u03c0\u2219s\n\n _\u21c4\u2032_ : Problem \u2192 DepProver \u2192 Bit \u2192 \u21ba R\u209a \u2192 Bit\n (pb \u21c4\u2032 pr) b (rand r\u209a) = Verif pb b (pr pb (rand r\u209a) b)\n\n _\u21c4_ : Problem \u2192 DepProver \u2192 \u21ba (Bit \u00d7 R\u209a) \u2192 Bit\n (pb \u21c4 pr) (rand (b , r\u209a)) = (pb \u21c4\u2032 pr) b (rand r\u209a)\n\n _\u21c4''_ : Problem \u2192 Prover \u2192 \u21ba (Bit \u00d7 R\u209a) \u2192 Bit\n pb \u21c4'' pr = pb \u21c4 depProver pr\n\n honest : (Problem \u2192 Maybe Solution) \u2192 DepProver\n honest solve pb (rand (\u03c0 , r\u209a)) b = (\u03c0 \u2219P pb , answer \u03c0 sol b)\n module Honest where\n sol : Solution\n sol with solve pb\n ... | just sol = \u03c0 \u2219S sol\n ... | nothing = \u03c0 \u2219S easy-sol \u03c0\n\n module WithCorrectSolver (pb : Problem)\n (s : Solution)\n (check-s : check pb s \u2261 true)\n where\n\n -- When the honest prover has a solution, he gets accepted\n -- unconditionally by the verifier.\n honest-accepted : \u2200 r \u2192 (pb \u21c4 honest (const (just s))) r \u2261 1b\n honest-accepted (rand (true , \u03c0 , r\u209a)) rewrite check-\u2219 pb s \u03c0 = check-s\n honest-accepted (rand (false , \u03c0 , r\u209a)) = ==-refl\n\n honest-\u2141 = \u03bb pb s \u2192 (pb \u21c4 honest (const (just s)))\n\n module HonestLeakZeroKnowledge (pb\u2080 pb\u2081 : Problem)\n (s\u2080 s\u2081 : Solution)\n (check-pf : check pb\u2080 s\u2080 \u2261 check pb\u2081 s\u2081) where\n\n helper : \u2200 r\u209a \u2192 Bool.to\u2115 ((pb\u2080 \u21c4\u2032 honest (const (just s\u2080))) 0b (rand r\u209a))\n \u2261 Bool.to\u2115 ((pb\u2081 \u21c4\u2032 honest (const (just s\u2081))) 0b (rand r\u209a))\n helper (\u03c0 , r\u209a) rewrite ==-refl {\u03c0 \u2219P pb\u2080} | ==-refl {\u03c0 \u2219P pb\u2081} = refl\n\n honest-leak : honest-\u2141 pb\u2080 s\u2080 \u2261# honest-\u2141 pb\u2081 s\u2081\n honest-leak rewrite otp-\u2219-check pb\u2080 s\u2080 pb\u2081 s\u2081 check-pf | sum-ext \u03bcR\u209a helper = refl\n\n module HonestLeakZeroKnowledge' (pb : Problem)\n (s\u2080 s\u2081 : Solution)\n (check-pf : check pb s\u2080 \u2261 check pb s\u2081) where\n\n honest-leak : honest-\u2141 pb s\u2080 \u2261# honest-\u2141 pb s\u2081\n honest-leak = HonestLeakZeroKnowledge.honest-leak pb pb s\u2080 s\u2081 check-pf\n\n -- Predicts b=b\u2032\n cheater : \u2200 b\u2032 \u2192 DepProver\n cheater b\u2032 pb (rand (\u03c0 , _)) b = \u03c0 \u2219P (case b\u2032 0\u2192 pb 1\u2192 easy-pb \u03c0)\n , answer \u03c0 (\u03c0 \u2219S easy-sol \u03c0) b\n\n -- If cheater predicts correctly, verifer accepts him\n cheater-accepted : \u2200 b pb r\u209a \u2192 (pb \u21c4\u2032 cheater b) b r\u209a \u2261 1b\n cheater-accepted true pb (rand (\u03c0 , r\u209a)) = check-easy \u03c0\n cheater-accepted false pb (rand (\u03c0 , r\u209a)) = ==-refl\n\n -- If cheater predicts incorrecty, verifier rejects him\n module CheaterRejected (pb : Problem)\n (not-easy-sol : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P pb) (\u03c0 \u2219S easy-sol \u03c0) \u2261 false)\n (not-easy-pb : \u2200 \u03c0 \u2192 ((\u03c0 \u2219P pb) == (\u03c0 \u2219P easy-pb \u03c0)) \u2261 false) where\n\n cheater-rejected : \u2200 b r\u209a \u2192 (pb \u21c4\u2032 cheater (not b)) b r\u209a \u2261 0b\n cheater-rejected true (rand (\u03c0 , r\u209a)) = not-easy-sol \u03c0\n cheater-rejected false (rand (\u03c0 , r\u209a)) = not-easy-pb \u03c0\n\nmodule DLog (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u229f_ : \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n (\u229f-\u229e : \u2200 \u03c0 x \u2192 (\u229f \u03c0) \u229e (\u03c0 \u229e x) \u2261 x)\n (^\u229f-\u2219 : \u2200 \u03b1 \u03b2 x \u2192 ((\u03b1 ^ (\u229f x)) \u2219 ((\u03b1 ^ x) \u2219 \u03b2)) \u2261 \u03b2)\n -- (\u2219-assoc : \u2200 \u03b1 \u03b2 \u03b3 \u2192 \u03b1 \u2219 (\u03b2 \u2219 \u03b3) \u2261 (\u03b1 \u2219 \u03b2) \u2219 \u03b3)\n (dist-^-\u229e : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u229e y) \u2261 (\u03b1 ^ x) \u2219 (\u03b1 ^ y))\n (_==_ : G \u2192 G \u2192 Bool)\n (==-refl : \u2200 {\u03b1} \u2192 (\u03b1 == \u03b1) \u2261 true)\n (==-cong : \u2200 {\u03b1 \u03b2 b} (f : G \u2192 G) \u2192 \u03b1 == \u03b2 \u2261 b \u2192 f \u03b1 == f \u03b2 \u2261 b)\n (==-true : \u2200 {\u03b1 \u03b2} \u2192 \u03b1 == \u03b2 \u2261 true \u2192 \u03b1 \u2261 \u03b2)\n (sum\u2124q : Sum \u2124q)\n (\u03bc\u2124q : SumProp sum\u2124q)\n (R\u209a-xtra : \u2605) -- extra prover\/adversary randomness\n (sumR\u209a-xtra : Sum R\u209a-xtra)\n (\u03bcR\u209a-xtra : SumProp sumR\u209a-xtra)\n (some-\u2124q : \u2124q)\n where\n\n Permutation = \u2124q\n Problem = G\n Solution = \u2124q\n\n _\u207b\u00b9 : Endo Permutation\n \u03c0 \u207b\u00b9 = \u229f \u03c0\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n _\u2219P_ : Permutation \u2192 Endo Problem\n \u03c0 \u2219P p = g^ \u03c0 \u2219 p\n\n \u207b\u00b9-inverseP : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219P (\u03c0 \u2219P x) \u2261 x\n \u207b\u00b9-inverseP \u03c0 x rewrite ^\u229f-\u2219 g x \u03c0 = refl\n\n _\u2219S_ : Permutation \u2192 Endo Solution\n \u03c0 \u2219S s = \u03c0 \u229e s\n\n \u207b\u00b9-inverseS : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219S (\u03c0 \u2219S x) \u2261 x\n \u207b\u00b9-inverseS = \u229f-\u229e\n\n check : Problem \u2192 Solution \u2192 Bit\n check p s = p == g^ s\n\n check-\u2219' : \u2200 p s \u03c0 b \u2192 check p s \u2261 b \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 b\n check-\u2219' p s \u03c0 true check-p-s rewrite dist-^-\u229e g \u03c0 s | ==-true check-p-s = ==-refl\n check-\u2219' p s \u03c0 false check-p-s rewrite dist-^-\u229e g \u03c0 s = ==-cong (_\u2219_ (g^ \u03c0)) check-p-s\n\n check-\u2219 : \u2200 p s \u03c0 \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 check p s\n check-\u2219 p s \u03c0 = check-\u2219' p s \u03c0 (check p s) refl\n\n easy-sol : Permutation \u2192 Solution\n easy-sol \u03c0 = some-\u2124q\n\n easy-pb : Permutation \u2192 Problem\n easy-pb \u03c0 = g^(easy-sol \u03c0)\n\n check-easy : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P easy-pb \u03c0) (\u03c0 \u2219S easy-sol \u03c0) \u2261 true\n check-easy \u03c0 rewrite dist-^-\u229e g \u03c0 (easy-sol \u03c0) = ==-refl\n\n open M Permutation _\u207b\u00b9 sum\u2124q \u03bc\u2124q R\u209a-xtra sumR\u209a-xtra \u03bcR\u209a-xtra\n Problem _==_ ==-refl _\u2219P_ \u207b\u00b9-inverseP Solution _\u2219S_ \u207b\u00b9-inverseS check check-\u2219 easy-pb easy-sol check-easy\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"open import Data.Bool.NP as Bool hiding (check)\nopen import Data.Nat\nopen import Data.Maybe\nopen import Data.Product.NP\nopen import Data.Bits\nopen import Function.NP\nopen import Relation.Binary.PropositionalEquality.NP\n\nopen import sum\n\nmodule generic-zero-knowledge-interactive where\n\nprivate\n \u2605 : Set\u2081\n \u2605 = Set\n\n-- A random argument, this is only a formal notation to\n-- indicate that the argument is supposed to be picked\n-- at random uniformly. (do not confuse with our randomness\n-- monad).\nrecord \u21ba (A : \u2605) : \u2605 where\n constructor rand\n field get : A\n\nmodule M (Permutation : \u2605)\n (_\u207b\u00b9 : Endo Permutation)\n (sum\u03c0 : Sum Permutation)\n (\u03bc\u03c0 : SumProp sum\u03c0)\n\n (R\u209a-xtra : \u2605) -- extra prover\/adversary randomness\n (sumR\u209a-xtra : Sum R\u209a-xtra)\n (\u03bcR\u209a-xtra : SumProp sumR\u209a-xtra)\n\n (Problem : \u2605)\n (_==_ : Problem \u2192 Problem \u2192 Bit)\n (==-refl : \u2200 {pb} \u2192 (pb == pb) \u2261 true)\n (_\u2219P_ : Permutation \u2192 Endo Problem)\n (\u207b\u00b9-inverseP : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219P (\u03c0 \u2219P x) \u2261 x)\n\n (Solution : \u2605)\n (_\u2219S_ : Permutation \u2192 Endo Solution)\n (\u207b\u00b9-inverseS : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219S (\u03c0 \u2219S x) \u2261 x)\n\n (check : Problem \u2192 Solution \u2192 Bit)\n (check-\u2219 : \u2200 p s \u03c0 \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 check p s)\n\n (easy-pb : Permutation \u2192 Problem)\n (easy-sol : Permutation \u2192 Solution)\n (check-easy : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P easy-pb \u03c0) (\u03c0 \u2219S easy-sol \u03c0) \u2261 true)\n where\n\n -- prover\/adversary randomness\n R\u209a : \u2605\n R\u209a = Permutation \u00d7 R\u209a-xtra\n\n sumR\u209a : Sum R\u209a\n sumR\u209a = sum\u03c0 \u00d7Sum sumR\u209a-xtra\n\n \u03bcR\u209a : SumProp sumR\u209a\n \u03bcR\u209a = \u03bc\u03c0 \u00d7\u03bc \u03bcR\u209a-xtra\n\n R = Bit \u00d7 R\u209a\n\n sumR : Sum R\n sumR = sumBit \u00d7Sum sumR\u209a\n\n \u03bcR : SumProp sumR\n \u03bcR = \u03bcBit \u00d7\u03bc \u03bcR\u209a\n\n check-\u03c0 : Problem \u2192 Solution \u2192 R\u209a \u2192 Bit\n check-\u03c0 p s (\u03c0 , _) = check (\u03c0 \u2219P p) (\u03c0 \u2219S s)\n\n otp-\u2219-check : let #_ = count \u03bcR\u209a\n in\n \u2200 p\u2080 s\u2080 p\u2081 s\u2081 \u2192\n check p\u2080 s\u2080 \u2261 check p\u2081 s\u2081 \u2192\n #(check-\u03c0 p\u2080 s\u2080) \u2261 #(check-\u03c0 p\u2081 s\u2081)\n otp-\u2219-check p\u2080 s\u2080 p\u2081 s\u2081 check-pf =\n count-ext \u03bcR\u209a {f = check-\u03c0 p\u2080 s\u2080} {check-\u03c0 p\u2081 s\u2081} (\u03bb \u03c0,r \u2192\n check-\u03c0 p\u2080 s\u2080 \u03c0,r \u2261\u27e8 check-\u2219 p\u2080 s\u2080 (proj\u2081 \u03c0,r) \u27e9\n check p\u2080 s\u2080 \u2261\u27e8 check-pf \u27e9\n check p\u2081 s\u2081 \u2261\u27e8 sym (check-\u2219 p\u2081 s\u2081 (proj\u2081 \u03c0,r)) \u27e9\n check-\u03c0 p\u2081 s\u2081 \u03c0,r \u220e)\n where open \u2261-Reasoning\n\n #_ : (\u21ba (Bit \u00d7 Permutation \u00d7 R\u209a-xtra) \u2192 Bit) \u2192 \u2115\n # f = count \u03bcR (f \u2218 rand)\n\n _\u2261#_ : (f g : \u21ba (Bit \u00d7 R\u209a) \u2192 Bit) \u2192 \u2605\n f \u2261# g = # f \u2261 # g\n\n{-\n otp-\u2219 : let otp = \u03bb O pb s \u2192 count \u03bcR\u209a (\u03bb { (\u03c0 , _) \u2192 O (\u03c0 \u2219P pb) (\u03c0 \u2219S s) }) in\n \u2200 pb\u2080 s\u2080 pb\u2081 s\u2081 \u2192\n check pb\u2080 s\u2080 \u2261 check pb\u2081 s\u2081 \u2192\n (O : _ \u2192 _ \u2192 Bit) \u2192 otp O pb\u2080 s\u2080 \u2261 otp O pb\u2081 s\u2081\n otp-\u2219 pb\u2080 s\u2080 pb\u2081 s\u2081 check-pf O = {!(\u03bc\u03c0 \u00d7Sum-proj\u2082 \u03bcR\u209a-xtra ?!}\n-}\n Answer : Bit \u2192 \u2605\n Answer false{-0b-} = Permutation\n Answer true {-1b-} = Solution\n\n answer : Permutation \u2192 Solution \u2192 \u2200 b \u2192 Answer b\n answer \u03c0 _ false = \u03c0\n answer _ s true = s\n\n -- The prover is the advesary in the generic terminology,\n -- and the verifier is the challenger.\n DepProver : \u2605\n DepProver = Problem \u2192 \u21ba R\u209a \u2192 (b : Bit) \u2192 Problem \u00d7 Answer b\n\n Prover\u2080 : \u2605\n Prover\u2080 = Problem \u2192 \u21ba R\u209a \u2192 Problem \u00d7 Permutation\n\n Prover\u2081 : \u2605\n Prover\u2081 = Problem \u2192 \u21ba R\u209a \u2192 Problem \u00d7 Solution\n\n Prover : \u2605\n Prover = Prover\u2080 \u00d7 Prover\u2081\n\n prover : DepProver \u2192 Prover\n prover dpr = (\u03bb pb r \u2192 dpr pb r 0b) , (\u03bb pb r \u2192 dpr pb r 1b)\n\n depProver : Prover \u2192 DepProver\n depProver (pr\u2080 , pr\u2081) pb r false = pr\u2080 pb r\n depProver (pr\u2080 , pr\u2081) pb r true = pr\u2081 pb r\n\n -- Here we show that the explicit commitment step seems useless given\n -- the formalization. The verifier can \"trust\" the prover on the fact\n -- that any choice is going to be govern only by the problem and the\n -- randomness.\n module WithCommitment (Commitment : \u2605)\n (AnswerWC : Bit \u2192 \u2605)\n (reveal : \u2200 b \u2192 Commitment \u2192 AnswerWC b \u2192 Problem \u00d7 Answer b) where\n ProverWC = (Problem \u2192 R\u209a \u2192 Commitment)\n \u00d7 (Problem \u2192 R\u209a \u2192 (b : Bit) \u2192 AnswerWC b)\n\n depProver' : ProverWC \u2192 DepProver\n depProver' (pr\u2080 , pr\u2081) pb (rand r\u209a) b = reveal b (pr\u2080 pb r\u209a) (pr\u2081 pb r\u209a b)\n\n Verif : Problem \u2192 \u2200 b \u2192 Problem \u00d7 Answer b \u2192 Bit\n Verif pb false{-0b-} (\u03c0\u2219pb , \u03c0) = (\u03c0 \u2219P pb) == \u03c0\u2219pb\n Verif pb true {-1b-} (\u03c0\u2219pb , \u03c0\u2219s) = check \u03c0\u2219pb \u03c0\u2219s\n\n _\u21c4\u2032_ : Problem \u2192 DepProver \u2192 Bit \u2192 \u21ba R\u209a \u2192 Bit\n (pb \u21c4\u2032 pr) b (rand r\u209a) = Verif pb b (pr pb (rand r\u209a) b)\n\n _\u21c4_ : Problem \u2192 DepProver \u2192 \u21ba (Bit \u00d7 R\u209a) \u2192 Bit\n (pb \u21c4 pr) (rand (b , r\u209a)) = (pb \u21c4\u2032 pr) b (rand r\u209a)\n\n _\u21c4''_ : Problem \u2192 Prover \u2192 \u21ba (Bit \u00d7 R\u209a) \u2192 Bit\n pb \u21c4'' pr = pb \u21c4 depProver pr\n\n honest : (Problem \u2192 Maybe Solution) \u2192 DepProver\n honest solve pb (rand (\u03c0 , r\u209a)) b = (\u03c0 \u2219P pb , answer \u03c0 sol b)\n module Honest where\n sol : Solution\n sol with solve pb\n ... | just sol = \u03c0 \u2219S sol\n ... | nothing = \u03c0 \u2219S easy-sol \u03c0\n\n module WithCorrectSolver (pb : Problem)\n (s : Solution)\n (check-s : check pb s \u2261 true)\n where\n\n -- When the honest prover has a solution, he gets accepted\n -- unconditionally by the verifier.\n honest-accepted : \u2200 r \u2192 (pb \u21c4 honest (const (just s))) r \u2261 1b\n honest-accepted (rand (true , \u03c0 , r\u209a)) rewrite check-\u2219 pb s \u03c0 = check-s\n honest-accepted (rand (false , \u03c0 , r\u209a)) = ==-refl\n\n honest-\u2141 = \u03bb pb s \u2192 (pb \u21c4 honest (const (just s)))\n\n module HonestLeakZeroKnowledge (pb\u2080 pb\u2081 : Problem)\n (s\u2080 s\u2081 : Solution)\n (check-pf : check pb\u2080 s\u2080 \u2261 check pb\u2081 s\u2081) where\n\n helper : \u2200 r\u209a \u2192 Bool.to\u2115 ((pb\u2080 \u21c4\u2032 honest (const (just s\u2080))) 0b (rand r\u209a))\n \u2261 Bool.to\u2115 ((pb\u2081 \u21c4\u2032 honest (const (just s\u2081))) 0b (rand r\u209a))\n helper (\u03c0 , r\u209a) rewrite ==-refl {\u03c0 \u2219P pb\u2080} | ==-refl {\u03c0 \u2219P pb\u2081} = refl\n\n honest-leak : honest-\u2141 pb\u2080 s\u2080 \u2261# honest-\u2141 pb\u2081 s\u2081\n honest-leak rewrite otp-\u2219-check pb\u2080 s\u2080 pb\u2081 s\u2081 check-pf | sum-ext \u03bcR\u209a helper = refl\n\n module HonestLeakZeroKnowledge' (pb : Problem)\n (s\u2080 s\u2081 : Solution)\n (check-pf : check pb s\u2080 \u2261 check pb s\u2081) where\n\n honest-leak : honest-\u2141 pb s\u2080 \u2261# honest-\u2141 pb s\u2081\n honest-leak = HonestLeakZeroKnowledge.honest-leak pb pb s\u2080 s\u2081 check-pf\n\n -- Predicts b=b\u2032\n cheater : \u2200 b\u2032 \u2192 DepProver\n cheater b\u2032 pb (rand (\u03c0 , _)) b = \u03c0 \u2219P (case b\u2032 0\u2192 pb 1\u2192 easy-pb \u03c0)\n , answer \u03c0 (\u03c0 \u2219S easy-sol \u03c0) b\n\n -- If cheater predicts correctly, verifer accepts him\n cheater-accepted : \u2200 b pb r\u209a \u2192 (pb \u21c4\u2032 cheater b) b r\u209a \u2261 1b\n cheater-accepted true pb (rand (\u03c0 , r\u209a)) = check-easy \u03c0\n cheater-accepted false pb (rand (\u03c0 , r\u209a)) = ==-refl\n\n -- If cheater predicts incorrecty, verifier rejects him\n module CheaterRejected (pb : Problem)\n (not-easy-sol : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P pb) (\u03c0 \u2219S easy-sol \u03c0) \u2261 false)\n (not-easy-pb : \u2200 \u03c0 \u2192 ((\u03c0 \u2219P pb) == (\u03c0 \u2219P easy-pb \u03c0)) \u2261 false) where\n\n cheater-rejected : \u2200 b r\u209a \u2192 (pb \u21c4\u2032 cheater (not b)) b r\u209a \u2261 0b\n cheater-rejected true (rand (\u03c0 , r\u209a)) = not-easy-sol \u03c0\n cheater-rejected false (rand (\u03c0 , r\u209a)) = not-easy-pb \u03c0\n\nmodule DLog (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u229f_ : \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n (\u229f-\u229e : \u2200 \u03c0 x \u2192 (\u229f \u03c0) \u229e (\u03c0 \u229e x) \u2261 x)\n (^\u229f-\u2219 : \u2200 \u03b1 \u03b2 x \u2192 ((\u03b1 ^ (\u229f x)) \u2219 ((\u03b1 ^ x) \u2219 \u03b2)) \u2261 \u03b2)\n -- (\u2219-assoc : \u2200 \u03b1 \u03b2 \u03b3 \u2192 \u03b1 \u2219 (\u03b2 \u2219 \u03b3) \u2261 (\u03b1 \u2219 \u03b2) \u2219 \u03b3)\n (dist-^-\u229e : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u229e y) \u2261 (\u03b1 ^ x) \u2219 (\u03b1 ^ y))\n (_==_ : G \u2192 G \u2192 Bool)\n (==-refl : \u2200 {\u03b1} \u2192 (\u03b1 == \u03b1) \u2261 true)\n (==-true : \u2200 {\u03b1 \u03b2} \u2192 \u03b1 == \u03b2 \u2261 true \u2192 \u03b1 \u2261 \u03b2)\n (==-false : \u2200 {\u03b1 \u03b2} \u2192 \u03b1 == \u03b2 \u2261 false \u2192 \u03b1 \u2262 \u03b2)\n (sum\u2124q : Sum \u2124q)\n (\u03bc\u2124q : SumProp sum\u2124q)\n (R\u209a-xtra : \u2605) -- extra prover\/adversary randomness\n (sumR\u209a-xtra : Sum R\u209a-xtra)\n (\u03bcR\u209a-xtra : SumProp sumR\u209a-xtra)\n (some-\u2124q : \u2124q)\n where\n\n Permutation = \u2124q\n Problem = G\n Solution = \u2124q\n\n _\u207b\u00b9 : Endo Permutation\n \u03c0 \u207b\u00b9 = \u229f \u03c0\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n _\u2219P_ : Permutation \u2192 Endo Problem\n \u03c0 \u2219P p = g^ \u03c0 \u2219 p\n\n \u207b\u00b9-inverseP : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219P (\u03c0 \u2219P x) \u2261 x\n \u207b\u00b9-inverseP \u03c0 x rewrite ^\u229f-\u2219 g x \u03c0 = refl\n\n _\u2219S_ : Permutation \u2192 Endo Solution\n \u03c0 \u2219S s = \u03c0 \u229e s\n\n \u207b\u00b9-inverseS : \u2200 \u03c0 x \u2192 \u03c0 \u207b\u00b9 \u2219S (\u03c0 \u2219S x) \u2261 x\n \u207b\u00b9-inverseS = \u229f-\u229e\n\n check : Problem \u2192 Solution \u2192 Bit\n check p s = p == g^ s\n\n check-\u2219' : \u2200 p s \u03c0 b \u2192 check p s \u2261 b \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 b\n check-\u2219' p s \u03c0 true check-p-s rewrite dist-^-\u229e g \u03c0 s | ==-true check-p-s = ==-refl\n check-\u2219' p s \u03c0 false check-p-s rewrite dist-^-\u229e g \u03c0 s\n with ==-false check-p-s ?\n ... | ()\n\n check-\u2219 : \u2200 p s \u03c0 \u2192 check (\u03c0 \u2219P p) (\u03c0 \u2219S s) \u2261 check p s\n check-\u2219 p s \u03c0 = check-\u2219' p s \u03c0 (check p s) refl\n\n easy-sol : Permutation \u2192 Solution\n easy-sol \u03c0 = some-\u2124q\n\n easy-pb : Permutation \u2192 Problem\n easy-pb \u03c0 = g^(easy-sol \u03c0)\n\n check-easy : \u2200 \u03c0 \u2192 check (\u03c0 \u2219P easy-pb \u03c0) (\u03c0 \u2219S easy-sol \u03c0) \u2261 true\n check-easy \u03c0 rewrite dist-^-\u229e g \u03c0 (easy-sol \u03c0) = ==-refl\n\n open M Permutation _\u207b\u00b9 sum\u2124q \u03bc\u2124q R\u209a-xtra sumR\u209a-xtra \u03bcR\u209a-xtra\n Problem _==_ ==-refl _\u2219P_ \u207b\u00b9-inverseP Solution _\u2219S_ \u207b\u00b9-inverseS check check-\u2219 easy-pb easy-sol check-easy\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"a3a6c0d7b42940b151354af420f8d4469955d5c8","subject":"Simplify Atlas-diff and Atlas-apply.","message":"Simplify Atlas-diff and Atlas-apply.\n\nOld-commit-hash: ada22ebb7416f8c4fb19882d4080166628b1b292\n","repos":"inc-lc\/ilc-agda","old_file":"Atlas\/Change\/Term.agda","new_file":"Atlas\/Change\/Term.agda","new_contents":"module Atlas.Change.Term where\n\nopen import Atlas.Syntax.Type\nopen import Atlas.Syntax.Term\nopen import Atlas.Change.Type\n\n-- nil-changes\n\nnil-const : \u2200 {\u03b9 : Base} \u2192 Const \u2205 (base (\u0394Base \u03b9))\nnil-const {\u03b9} = neutral {\u0394Base \u03b9}\n\nnil-term : \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base (\u0394Base \u03b9))\nnil-term {Bool} = curriedConst (nil-const {Bool})\nnil-term {Map \u03ba \u03b9} = curriedConst (nil-const {Map \u03ba \u03b9})\n\n-- diff-term and apply-term\n\nopen import Parametric.Change.Term Const \u0394Base\n\n-- b\u2080 \u229d b\u2081 = b\u2080 xor b\u2081\n-- m\u2080 \u229d m\u2081 = zip _\u229d_ m\u2080 m\u2081\n\nAtlas-diff : \u2200 {\u03b9 \u0393} \u2192\n Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394Type (base \u03b9))\nAtlas-diff {Bool} = abs\u2082 (\u03bb b\u2081 b\u2082 \u2192 xor! b\u2081 b\u2082)\nAtlas-diff {Map \u03ba \u03b9} = abs\u2082 (\u03bb m\u2081 m\u2082 \u2192 zip! (abs Atlas-diff) m\u2081 m\u2082)\n\n-- b \u2295 \u0394b = b xor \u0394b\n-- m \u2295 \u0394m = zip _\u2295_ m \u0394m\n\nAtlas-apply : \u2200 {\u03b9 \u0393} \u2192\n Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\nAtlas-apply {Bool} = abs\u2082 (\u03bb b\u2081 b\u2082 \u2192 xor! b\u2081 b\u2082)\nAtlas-apply {Map \u03ba \u03b9} = abs\u2082 (\u03bb m\u2081 m\u2082 \u2192 zip! (abs Atlas-apply) m\u2081 m\u2082)\n\n-- Shorthands for working with diff-term and apply-term\n\ndiff : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 (\u0394Type \u03c4)\ndiff = app\u2082 (lift-diff Atlas-diff Atlas-apply)\n\napply : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\napply = app\u2082 (lift-apply Atlas-diff Atlas-apply)\n\n-- Shorthands for creating changes corresponding to\n-- insertion\/deletion.\n\ninsert : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base \u03b9) \u2192\n -- last argument is the change accumulator\n Term \u0393 (\u0394Type (base (Map \u03ba \u03b9))) \u2192\n Term \u0393 (\u0394Type (base (Map \u03ba \u03b9)))\n\ndelete : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base \u03b9) \u2192\n Term \u0393 (\u0394Type (base (Map \u03ba \u03b9))) \u2192\n Term \u0393 (\u0394Type (base (Map \u03ba \u03b9)))\n\ninsert k v acc = update! k (diff v neutral-term) acc\ndelete k v acc = update! k (diff neutral-term v) acc\n\n","old_contents":"module Atlas.Change.Term where\n\nopen import Atlas.Syntax.Type\nopen import Atlas.Syntax.Term\nopen import Atlas.Change.Type\n\n-- nil-changes\n\nnil-const : \u2200 {\u03b9 : Base} \u2192 Const \u2205 (base (\u0394Base \u03b9))\nnil-const {\u03b9} = neutral {\u0394Base \u03b9}\n\nnil-term : \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base (\u0394Base \u03b9))\nnil-term {Bool} = curriedConst (nil-const {Bool})\nnil-term {Map \u03ba \u03b9} = curriedConst (nil-const {Map \u03ba \u03b9})\n\n-- diff-term and apply-term\n\nopen import Parametric.Change.Term Const \u0394Base\n\n-- b\u2080 \u229d b\u2081 = b\u2080 xor b\u2081\n-- m\u2080 \u229d m\u2081 = zip _\u229d_ m\u2080 m\u2081\n\nAtlas-diff : \u2200 {\u03b9 \u0393} \u2192\n Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 \u0394Type (base \u03b9))\nAtlas-diff {Bool} = abs (abs (curriedConst xor (var (that this)) (var this)))\nAtlas-diff {Map \u03ba \u03b9} = abs (abs (curriedConst zip (abs Atlas-diff) (var (that this)) (var this)))\n\n-- b \u2295 \u0394b = b xor \u0394b\n-- m \u2295 \u0394m = zip _\u2295_ m \u0394m\n\nAtlas-apply : \u2200 {\u03b9 \u0393} \u2192\n Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\nAtlas-apply {Bool} = abs (abs (curriedConst xor (var (that this)) (var this)))\nAtlas-apply {Map \u03ba \u03b9} = abs (abs (curriedConst zip (abs Atlas-apply) (var (that this)) (var this)))\n\n-- Shorthands for working with diff-term and apply-term\n\ndiff : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 (\u0394Type \u03c4)\ndiff = app\u2082 (lift-diff Atlas-diff Atlas-apply)\n\napply : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\napply = app\u2082 (lift-apply Atlas-diff Atlas-apply)\n\n-- Shorthands for creating changes corresponding to\n-- insertion\/deletion.\n\ninsert : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base \u03b9) \u2192\n -- last argument is the change accumulator\n Term \u0393 (\u0394Type (base (Map \u03ba \u03b9))) \u2192\n Term \u0393 (\u0394Type (base (Map \u03ba \u03b9)))\n\ndelete : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Term \u0393 (base \u03ba) \u2192 Term \u0393 (base \u03b9) \u2192\n Term \u0393 (\u0394Type (base (Map \u03ba \u03b9))) \u2192\n Term \u0393 (\u0394Type (base (Map \u03ba \u03b9)))\n\ninsert k v acc = update! k (diff v neutral-term) acc\ndelete k v acc = update! k (diff neutral-term v) acc\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d41e35b9fb769cc937f3fa2787fe655ac719f13a","subject":"Atlas: add primitives `lookup`, `fold` and various improvements","message":"Atlas: add primitives `lookup`, `fold` and various improvements\n\nImprovements:\n\n- speed up derivative of zip\n\n- less verbose term constructors for constants\n\n- remove need to support conjunction and union directly\n\nFuture work:\n\n- elimination forms: if-then-else, if-nonempty\n\nOld-commit-hash: 9aafd0c2835ff027b57e44ed2930f4f57147e0de\n","repos":"inc-lc\/ilc-agda","old_file":"Syntax\/Language\/Atlas.agda","new_file":"Syntax\/Language\/Atlas.agda","new_contents":"module Syntax.Language.Atlas where\n\n-- Base types of the calculus Atlas\n-- to be used with Plotkin-style language description\n--\n-- Atlas supports maps with neutral elements. The change type to\n-- `Map \u03ba \u03b9` is `Map \u03ba \u0394\u03b9`, where \u0394\u03b9 is the change type of the\n-- ground type \u03b9. Such a change to maps can support insertions\n-- and deletions as well: Inserting `k -> v` means mapping `k` to\n-- the change from the neutral element to `v`, and deleting\n-- `k -> v` means mapping `k` to the change from `v` to the\n-- neutral element.\n\nopen import Syntax.Language.Calculus\n\ndata Atlas-type : Set where\n Bool : Atlas-type\n Map : (\u03ba : Atlas-type) (\u03b9 : Atlas-type) \u2192 Atlas-type\n\ndata Atlas-const : Set where\n\n true : Atlas-const\n false : Atlas-const\n xor : Atlas-const\n\n empty : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n update : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n lookup : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n zip : \u2200 {\u03ba a b c : Atlas-type} \u2192 Atlas-const\n fold : \u2200 {\u03ba a b : Atlas-type} \u2192 Atlas-const\n\nAtlas-lookup : Atlas-const \u2192 Type Atlas-type\n\nAtlas-lookup true = base Bool\nAtlas-lookup false = base Bool\nAtlas-lookup xor = base Bool \u21d2 base Bool \u21d2 base Bool\n\nAtlas-lookup (empty {\u03ba} {\u03b9}) = base (Map \u03ba \u03b9)\n\n-- `update key val my-map` would\n-- - insert if `key` is not present in `my-map`\n-- - delete if `val` is the neutral element\n-- - make an update otherwise\nAtlas-lookup (update {\u03ba} {\u03b9}) =\n base \u03ba \u21d2 base \u03b9 \u21d2 base (Map \u03ba \u03b9) \u21d2 base (Map \u03ba \u03b9)\n\nAtlas-lookup (lookup {\u03ba} {\u03b9}) =\n base \u03ba \u21d2 base (Map \u03ba \u03b9) \u21d2 base \u03b9\n\n-- Model of zip = Haskell Data.List.zipWith\n--\n-- zipWith :: (a \u2192 b \u2192 c) \u2192 [a] \u2192 [b] \u2192 [c]\n--\n-- Behavioral difference: all key-value pairs present\n-- in *either* (m\u2081 : Map \u03ba a) *or* (m\u2082 : Map \u03ba b) will\n-- be iterated over. Neutral element of type `a` or `b`\n-- will be supplied if the key is missing in the\n-- corresponding map.\nAtlas-lookup (zip {\u03ba} {a} {b} {c}) =\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u21d2\n base (Map \u03ba a) \u21d2 base (Map \u03ba b) \u21d2 base (Map \u03ba c)\n\n-- Model of fold = Haskell Data.Map.foldWithKey\n--\n-- foldWithKey :: (k \u2192 a \u2192 b \u2192 b) \u2192 b \u2192 Map k a \u2192 b\n--\nAtlas-lookup (fold {\u03ba} {a} {b}) =\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base b) \u21d2\n base b \u21d2 base (Map \u03ba a) \u21d2 base b\n\nAtlas-\u0394base : Atlas-type \u2192 Atlas-type\n-- change to a boolean is a xor-rand\nAtlas-\u0394base Bool = Bool\n-- change to a map is change to its values\nAtlas-\u0394base (Map key val) = (Map key (Atlas-\u0394base val))\n\nAtlas-\u0394type : Type Atlas-type \u2192 Type Atlas-type\nAtlas-\u0394type = lift-\u0394type\u2080 Atlas-\u0394base\n\nAtlas-context : Set\nAtlas-context = Context {Type Atlas-type}\n\nAtlas-term : Atlas-context \u2192 Type Atlas-type \u2192 Set\nAtlas-term = Term {Atlas-type} {Atlas-const} {Atlas-lookup}\n\n-- Shorthands of constants\n--\n-- There's probably a uniform way to lift constants\n-- into term constructors.\n\nupdate! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9))\nupdate! = app\u2083 (const update)\n\nlookup! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Atlas-term \u0393 (base \u03b9)\nlookup! = app\u2082 (const lookup)\n\nzip! : \u2200 {\u03ba a b c \u0393} \u2192\n Atlas-term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u2192\n Atlas-term \u0393 (base (Map \u03ba a)) \u2192 Atlas-term \u0393 (base (Map \u03ba b)) \u2192\n Atlas-term \u0393 (base (Map \u03ba c))\nzip! = app\u2083 (const zip)\n\nfold! : \u2200 {\u03ba a b \u0393} \u2192\n Atlas-term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base b) \u2192\n Atlas-term \u0393 (base b) \u2192 Atlas-term \u0393 (base (Map \u03ba a)) \u2192\n Atlas-term \u0393 (base b)\nfold! = app\u2083 (const fold)\n\n-- Every base type has a known nil-change.\n-- The nil-change of \u03b9 is also the neutral element of Map \u03ba \u0394\u03b9.\n\nneutral : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const\nneutral {Bool} = false\nneutral {Map \u03ba \u03b9} = empty {\u03ba} {\u03b9}\n\nneutral-term : \u2200 {\u03b9 \u0393} \u2192 Atlas-term \u0393 (base \u03b9)\nneutral-term {Bool} = const (neutral {Bool})\nneutral-term {Map \u03ba \u03b9} = const (neutral {Map \u03ba \u03b9})\n\nnil-const : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const\nnil-const {\u03b9} = neutral {Atlas-\u0394base \u03b9}\n\nnil-term : \u2200 {\u03b9 \u0393} \u2192 Atlas-term \u0393 (base (Atlas-\u0394base \u03b9))\nnil-term {Bool} = const (nil-const {Bool})\nnil-term {Map \u03ba \u03b9} = const (nil-const {Map \u03ba \u03b9})\n\n-- Nonfunctional products can be encoded.\n-- The incremental behavior of products thus encoded is weird:\n-- \u0394(\u03b1 \u00d7 \u03b2) = \u03b1 \u00d7 \u0394\u03b2\nPair : Atlas-type \u2192 Atlas-type \u2192 Atlas-type\nPair \u03b1 \u03b2 = Map \u03b1 \u03b2\n\npair : \u2200 {\u03b1 \u03b2 \u0393} \u2192\n Atlas-term \u0393 (base \u03b1) \u2192 Atlas-term \u0393 (base \u03b2) \u2192\n Atlas-term \u0393 (base (Pair \u03b1 \u03b2))\npair s t = update! s t (const empty)\n\npair-term : \u2200 {\u03b1 \u03b2 \u0393} \u2192\n Atlas-term \u0393 (base \u03b1 \u21d2 base \u03b2 \u21d2 base (Pair \u03b1 \u03b2))\npair-term = abs (abs (pair (var (that this)) (var this)))\n\nuncurry : \u2200 {\u03b1 \u03b2 \u03b3 \u0393} \u2192\n Atlas-term \u0393 (base \u03b1 \u21d2 base \u03b2 \u21d2 base \u03b3) \u2192\n Atlas-term \u0393 (base (Pair \u03b1 \u03b2)) \u2192\n Atlas-term \u0393 (base \u03b3)\nuncurry f p =\n let\n a = var (that (that this))\n b = var (that this)\n g = abs (abs (abs (app\u2082 (weaken\u2083 f) a b)))\n in\n fold! g neutral-term p\n\nzip-pair : \u2200 {\u03ba a b \u0393} \u2192\n Atlas-term \u0393 (base (Map \u03ba a)) \u2192 Atlas-term \u0393 (base (Map \u03ba b)) \u2192\n Atlas-term \u0393 (base (Map \u03ba (Pair a b)))\nzip-pair = zip! (abs pair-term)\n\n-- diff-term and apply-term\n\n-- b\u2080 \u229d b\u2081 = b\u2080 xor b\u2081\n-- m\u2080 \u229d m\u2081 = zip _\u229d_ m\u2080 m\u2081\n\nAtlas-diff : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 Atlas-\u0394type (base \u03b9))\nAtlas-diff {Bool} = const xor\nAtlas-diff {Map \u03ba \u03b9} = app (const zip) (abs Atlas-diff)\n\n-- b \u2295 \u0394b = b xor \u0394b\n-- m \u2295 \u0394m = zip _\u2295_ m \u0394m\n\nAtlas-apply : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\nAtlas-apply {Bool} = const xor\nAtlas-apply {Map \u03ba \u03b9} = app (const zip) (abs Atlas-apply)\n\n-- Shorthands for working with diff-term and apply-term\n\ndiff : \u2200 {\u03c4 \u0393} \u2192\n Atlas-term \u0393 \u03c4 \u2192 Atlas-term \u0393 \u03c4 \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4)\ndiff = app\u2082 (lift-diff Atlas-diff Atlas-apply)\n\napply : \u2200 {\u03c4 \u0393} \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4) \u2192 Atlas-term \u0393 \u03c4 \u2192\n Atlas-term \u0393 \u03c4\napply = app\u2082 (lift-apply Atlas-diff Atlas-apply)\n\n-- Shorthands for creating changes corresponding to\n-- insertion\/deletion.\n\ninsert : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n -- last argument is the change accumulator\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ndelete : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ninsert k v acc = update! k (diff v neutral-term) acc\ndelete k v acc = update! k (diff neutral-term v) acc\n\n-- Shorthand for 4-way zip\nzip4! : \u2200 {\u03ba a b c d e \u0393} \u2192\n let\n t:_ = \u03bb \u03b9 \u2192 Atlas-term \u0393 (base \u03b9)\n in\n Atlas-term \u0393\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c \u21d2 base d \u21d2 base e) \u2192\n t: Map \u03ba a \u2192 t: Map \u03ba b \u2192 t: Map \u03ba c \u2192 t: Map \u03ba d \u2192 t: Map \u03ba e\n\n-- zip\u2084 f m\u2081 m\u2082 m\u2083 m\u2084 =\n-- zip (\u03bb k p\u2081\u2082 p\u2083\u2084 \u2192 uncurry (\u03bb v\u2081 v\u2082 \u2192 uncurry (\u03bb v\u2083 v\u2084 \u2192\n-- f k v\u2081 v\u2082 v\u2083 v\u2084)\n-- p\u2083\u2084) p\u2081\u2082)\n-- (zip-pair m\u2081 m\u2082) (zip-pair m\u2083 m\u2084)\n\nzip4! f m\u2081 m\u2082 m\u2083 m\u2084 =\n let\n f\u2032 = weaken\u2083 (weaken\u2083 (weaken\u2081 f))\n k = var (that (that (that (that (that (that this))))))\n p\u2081\u2082 = var (that this)\n p\u2083\u2084 = var (that (that this))\n v\u2081 = var (that (that (that this)))\n v\u2082 = var (that (that this))\n v\u2083 = var (that this)\n v\u2084 = var this\n g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs\n (app\u2085 f\u2032 k v\u2081 v\u2082 v\u2083 v\u2084)))\n p\u2083\u2084))) p\u2081\u2082)))\n in\n zip! g (zip-pair m\u2081 m\u2082) (zip-pair m\u2083 m\u2084)\n\n-- Type signature of Atlas-\u0394const is boilerplate.\nAtlas-\u0394const : \u2200 {\u0393} \u2192 (c : Atlas-const) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (Atlas-lookup c))\n\nAtlas-\u0394const true = const false\nAtlas-\u0394const false = const false\n\n-- \u0394xor = \u03bb x \u0394x y \u0394y \u2192 \u0394x xor \u0394y\nAtlas-\u0394const xor =\n let\n \u0394x = var (that (that this))\n \u0394y = var this\n in abs (abs (abs (abs (app\u2082 (const xor) \u0394x \u0394y))))\n\nAtlas-\u0394const empty = const empty\n\n-- If k \u2295 \u0394k \u2261 k, then\n-- \u0394update k \u0394k v \u0394v m \u0394m = update k \u0394v \u0394m\n-- else it is a deletion followed by insertion:\n-- \u0394update k \u0394k v \u0394v m \u0394m =\n-- insert (k \u2295 \u0394k) (v \u2295 \u0394v) (delete k v \u0394m)\n--\n-- We implement the else-branch only for the moment.\nAtlas-\u0394const update =\n let\n k = var (that (that (that (that (that this)))))\n \u0394k = var (that (that (that (that this))))\n v = var (that (that (that this)))\n \u0394v = var (that (that this))\n -- m = var (that this) -- unused parameter\n \u0394m = var this\n in\n abs (abs (abs (abs (abs (abs\n (insert (apply \u0394k k) (apply \u0394v v)\n (delete k v \u0394m)))))))\n\n-- \u0394lookup k \u0394k m \u0394m | true? (k \u2295 \u0394k \u2261 k)\n-- ... | true = lookup k \u0394m\n-- ... | false =\n-- (lookup (k \u2295 \u0394k) m \u2295 lookup (k \u2295 \u0394k) \u0394m)\n-- \u229d lookup k m\n--\n-- Only the false-branch is implemented.\nAtlas-\u0394const lookup =\n let\n k = var (that (that (that this)))\n \u0394k = var (that (that this))\n m = var (that this)\n \u0394m = var this\n k\u2032 = apply \u0394k k\n in\n abs (abs (abs (abs\n (diff (apply (lookup! k\u2032 \u0394m) (lookup! k\u2032 m))\n (lookup! k m)))))\n\n-- \u0394zip f \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082 | true? (f \u2295 \u0394f \u2261 f)\n--\n-- ... | true =\n-- zip (\u03bb k \u0394v\u2081 \u0394v\u2082 \u2192 \u0394f (lookup k m\u2081) \u0394v\u2081 (lookup k m\u2082) \u0394v\u2082)\n-- \u0394m\u2081 \u0394m\u2082\n--\n-- ... | false = zip\u2084 \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082\n--\n-- we implement the false-branch for the moment.\nAtlas-\u0394const zip =\n let\n \u0394f = var (that (that (that (that this))))\n m\u2081 = var (that (that (that this)))\n \u0394m\u2081 = var (that (that this))\n m\u2082 = var (that this)\n \u0394m\u2082 = var this\n g = abs (app\u2082 (weaken\u2081 \u0394f) (var this) nil-term)\n in\n abs (abs (abs (abs (abs (abs (zip4! g m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082))))))\n\n-- \u0394fold f \u0394f z \u0394z m \u0394m = proj\u2082\n-- (fold (\u03bb k [a,\u0394a] [b,\u0394b] \u2192\n-- uncurry (\u03bb a \u0394a \u2192 uncurry (\u03bb b \u0394b \u2192\n-- pair (f k a b) (\u0394f k nil a \u0394a b \u0394b))\n-- [b,\u0394b]) [a,\u0394a])\n-- (pair z \u0394z)\n-- (zip-pair m \u0394m))\n--\n-- \u0394fold is efficient only if evaluation is lazy and \u0394f is\n-- self-maintainable: it doesn't look at the argument\n-- (b = fold f k a b\u2080) at all.\nAtlas-\u0394const (fold {\u03ba} {\u03b1} {\u03b2}) =\n let -- TODO (tedius): write weaken\u2087\n f = weaken\u2083 (weaken\u2083 (weaken\u2081\n (var (that (that (that (that (that this))))))))\n \u0394f = weaken\u2083 (weaken\u2083 (weaken\u2081\n (var (that (that (that (that this)))))))\n z = var (that (that (that this)))\n \u0394z = var (that (that this))\n m = var (that this)\n \u0394m = var this\n k = weaken\u2083 (weaken\u2081 (var (that (that this))))\n [a,\u0394a] = var (that this)\n [b,\u0394b] = var this\n a = var (that (that (that this)))\n \u0394a = var (that (that this))\n b = var (that this)\n \u0394b = var this\n g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs\n (pair (app\u2083 f k a b)\n (app\u2086 \u0394f k nil-term a \u0394a b \u0394b))))\n (weaken\u2082 [b,\u0394b])))) [a,\u0394a])))\n proj\u2082 = uncurry (abs (abs (var this)))\n in\n abs (abs (abs (abs (abs (abs\n (proj\u2082 (fold! g (pair z \u0394z) (zip-pair m \u0394m))))))))\n\nAtlas = calculus-with\n Atlas-type\n Atlas-const\n Atlas-lookup\n Atlas-\u0394type\n Atlas-\u0394const\n","old_contents":"module Syntax.Language.Atlas where\n\n-- Base types of the calculus Atlas\n-- to be used with Plotkin-style language description\n--\n-- Atlas supports maps with neutral elements. The change type to\n-- `Map \u03ba \u03b9` is `Map \u03ba \u0394\u03b9`, where \u0394\u03b9 is the change type of the\n-- ground type \u03b9. Such a change to maps can support insertions\n-- and deletions as well: Inserting `k -> v` means mapping `k` to\n-- the change from the neutral element to `v`, and deleting\n-- `k -> v` means mapping `k` to the change from `v` to the\n-- neutral element.\n\nopen import Syntax.Language.Calculus\n\ndata Atlas-type : Set where\n Bool : Atlas-type\n Map : (\u03ba : Atlas-type) (\u03b9 : Atlas-type) \u2192 Atlas-type\n\ndata Atlas-const : Set where\n\n true : Atlas-const\n false : Atlas-const\n xor : Atlas-const\n\n empty : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n update : \u2200 {\u03ba \u03b9 : Atlas-type} \u2192 Atlas-const\n zip : \u2200 {\u03ba a b c : Atlas-type} \u2192 Atlas-const\n\nAtlas-lookup : Atlas-const \u2192 Type Atlas-type\n\nAtlas-lookup true = base Bool\nAtlas-lookup false = base Bool\nAtlas-lookup xor = base Bool \u21d2 base Bool \u21d2 base Bool\n\nAtlas-lookup (empty {\u03ba} {\u03b9}) = base (Map \u03ba \u03b9)\n\n-- `update key val my-map` would\n-- - insert if `key` is not present in `my-map`\n-- - delete if `val` is the neutral element\n-- - make an update otherwise\nAtlas-lookup (update {\u03ba} {\u03b9}) =\n base \u03ba \u21d2 base \u03b9 \u21d2 base (Map \u03ba \u03b9) \u21d2 base (Map \u03ba \u03b9)\n\n-- Model of zip = Haskell Data.List.zipWith\n-- zipWith :: (a \u2192 b \u2192 c) \u2192 [a] \u2192 [b] \u2192 [c]\nAtlas-lookup (zip {\u03ba} {a} {b} {c}) =\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u21d2\n base (Map \u03ba a) \u21d2 base (Map \u03ba b) \u21d2 base (Map \u03ba c)\n\nAtlas-\u0394base : Atlas-type \u2192 Atlas-type\n-- change to a boolean is a xor-rand\nAtlas-\u0394base Bool = Bool\n-- change to a map is change to its values\nAtlas-\u0394base (Map key val) = (Map key (Atlas-\u0394base val))\n\nAtlas-\u0394type : Type Atlas-type \u2192 Type Atlas-type\nAtlas-\u0394type = lift-\u0394type\u2080 Atlas-\u0394base\n\nAtlas-context : Set\nAtlas-context = Context {Type Atlas-type}\n\nAtlas-term : Atlas-context \u2192 Type Atlas-type \u2192 Set\nAtlas-term = Term {Atlas-type} {Atlas-const} {Atlas-lookup}\n\n-- Every base type has a known nil-change.\n-- The nil-change of \u03b9 is also the neutral element of Map \u03ba \u0394\u03b9.\n\nneutral : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const\nneutral {Bool} = false\nneutral {Map \u03ba \u03b9} = empty {\u03ba} {\u03b9}\n\nneutral-term : \u2200 {\u03b9 \u0393} \u2192 Atlas-term \u0393 (base \u03b9)\nneutral-term {Bool} = const (neutral {Bool})\nneutral-term {Map \u03ba \u03b9} = const (neutral {Map \u03ba \u03b9})\n\nnil-const : \u2200 {\u03b9 : Atlas-type} \u2192 Atlas-const\nnil-const {\u03b9} = neutral {Atlas-\u0394base \u03b9}\n\nnil-term : \u2200 {\u03b9 \u0393} \u2192 Atlas-term \u0393 (base (Atlas-\u0394base \u03b9))\nnil-term {Bool} = const (nil-const {Bool})\nnil-term {Map \u03ba \u03b9} = const (nil-const {Map \u03ba \u03b9})\n\n-- Shorthands of constants\n--\n-- There's probably a uniform way to lift constants\n-- into term constructors.\n--\n-- TODO: write this and call it Syntax.Term.Plotkin.lift-term\n\nzip! : \u2200 {\u03ba a b c \u0393} \u2192\n Atlas-term \u0393 (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c) \u2192\n Atlas-term \u0393 (base (Map \u03ba a)) \u2192 Atlas-term \u0393 (base (Map \u03ba b)) \u2192\n Atlas-term \u0393 (base (Map \u03ba c))\n\nzip! f m\u2081 m\u2082 = app (app (app (const zip) f) m\u2081) m\u2082\n\nlookup! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Atlas-term \u0393 (base \u03b9)\n\nlookup! = {!!} -- TODO: add constant `lookup`\n\n-- diff-term and apply-term\n\n-- b\u2080 \u229d b\u2081 = b\u2080 xor b\u2081\n-- m\u2080 \u229d m\u2081 = zip _\u229d_ m\u2080 m\u2081\n\nAtlas-diff : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 Atlas-\u0394type (base \u03b9))\nAtlas-diff {Bool} = const xor\nAtlas-diff {Map \u03ba \u03b9} = app (const zip) (abs Atlas-diff)\n\n-- b \u2295 \u0394b = b xor \u0394b\n-- m \u2295 \u0394m = zip _\u2295_ m \u0394m\n\nAtlas-apply : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\nAtlas-apply {Bool} = const xor\nAtlas-apply {Map \u03ba \u03b9} = app (const zip) (abs Atlas-apply)\n\n-- Shorthands for working with diff-term and apply-term\n\ndiff : \u2200 {\u03c4 \u0393} \u2192\n Atlas-term \u0393 \u03c4 \u2192 Atlas-term \u0393 \u03c4 \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4)\ndiff s t = app (app (lift-diff Atlas-diff Atlas-apply) s) t\n\napply : \u2200 {\u03c4 \u0393} \u2192\n Atlas-term \u0393 (Atlas-\u0394type \u03c4) \u2192 Atlas-term \u0393 \u03c4 \u2192\n Atlas-term \u0393 \u03c4\napply s t = app (app (lift-apply Atlas-diff Atlas-apply) s) t\n\n-- Shorthands for creating changes corresponding to\n-- insertion\/deletion.\n\nupdate! : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9)) \u2192\n Atlas-term \u0393 (base (Map \u03ba \u03b9))\n\nupdate! k v m = app (app (app (const update) k) v) m\n\ninsert : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n -- last argument is the change accumulator\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ndelete : \u2200 {\u03ba \u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03ba) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9))) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (base (Map \u03ba \u03b9)))\n\ninsert k v acc = update! k (diff v neutral-term) acc\ndelete k v acc = update! k (diff neutral-term v) acc\n\n-- union s t should be:\n-- 1. equal to t if s is neutral\n-- 2. equal to s if t is neutral\n-- 3. equal to s if s == t\n-- 4. whatever it wants to be otherwise\n--\n-- On Booleans, only conjunction can be union.\n--\n-- TODO (later): support conjunction, probably by Boolean\n-- elimination form if-then-else\n\npostulate\n and! : \u2200 {\u0393} \u2192\n Atlas-term \u0393 (base Bool) \u2192 Atlas-term \u0393 (base Bool) \u2192\n Atlas-term \u0393 (base Bool)\n\nunion : \u2200 {\u03b9 \u0393} \u2192\n Atlas-term \u0393 (base \u03b9) \u2192 Atlas-term \u0393 (base \u03b9) \u2192\n Atlas-term \u0393 (base \u03b9)\nunion {Bool} s t = and! s t\nunion {Map \u03ba \u03b9} s t =\n let\n union-term = abs (abs (union (var (that this)) (var this)))\n in\n zip! (abs union-term) s t\n\n-- Shorthand for 4-way zip\nzip4! : \u2200 {\u03ba a b c d e \u0393} \u2192\n let\n t:_ = \u03bb \u03b9 \u2192 Atlas-term \u0393 (base \u03b9)\n in\n Atlas-term \u0393\n (base \u03ba \u21d2 base a \u21d2 base b \u21d2 base c \u21d2 base d \u21d2 base e) \u2192\n t: Map \u03ba a \u2192 t: Map \u03ba b \u2192 t: Map \u03ba c \u2192 t: Map \u03ba d \u2192 t: Map \u03ba e\n\nzip4! f m\u2081 m\u2082 m\u2083 m\u2084 =\n let\n v\u2081 = var (that this)\n v\u2082 = var this\n v\u2083 = var (that this)\n v\u2084 = var this\n k\u2081\u2082 = var (that (that this))\n k\u2083\u2084 = var (that (that this))\n f\u2081\u2082 = abs (abs (abs (app (app (app (app (app\n (weaken\u2083 f) k\u2081\u2082) v\u2081) v\u2082)\n (lookup! k\u2081\u2082 (weaken\u2083 m\u2083))) (lookup! k\u2081\u2082 (weaken\u2083 m\u2084)))))\n f\u2083\u2084 = abs (abs (abs (app (app (app (app (app\n (weaken\u2083 f) k\u2083\u2084)\n (lookup! k\u2083\u2084 (weaken\u2083 m\u2081)))\n (lookup! k\u2083\u2084 (weaken\u2083 m\u2082))) v\u2083) v\u2084)))\n in\n -- A correct but inefficient implementation.\n -- May want to speed it up after constants are finalized.\n union (zip! f\u2081\u2082 m\u2081 m\u2082) (zip! f\u2083\u2084 m\u2083 m\u2084)\n\n-- Type signature of Atlas-\u0394const is boilerplate.\nAtlas-\u0394const : \u2200 {\u0393} \u2192 (c : Atlas-const) \u2192\n Atlas-term \u0393 (Atlas-\u0394type (Atlas-lookup c))\n\nAtlas-\u0394const true = const false\nAtlas-\u0394const false = const false\n\n-- \u0394xor = \u03bb x \u0394x y \u0394y \u2192 \u0394x xor \u0394y\nAtlas-\u0394const xor =\n let\n \u0394x = var (that (that this))\n \u0394y = var this\n in abs (abs (abs (abs (app (app (const xor) \u0394x) \u0394y))))\n\nAtlas-\u0394const empty = const empty\n\n-- If k \u2295 \u0394k \u2261 k, then\n-- \u0394update k \u0394k v \u0394v m \u0394m = update k \u0394v \u0394m\n-- else it is a deletion followed by insertion:\n-- \u0394update k \u0394k v \u0394v m \u0394m =\n-- insert (k \u2295 \u0394k) (v \u2295 \u0394v) (delete k v \u0394m)\n--\n-- We implement the else-branch only for the moment.\nAtlas-\u0394const update =\n let\n k = var (that (that (that (that (that this)))))\n \u0394k = var (that (that (that (that this))))\n v = var (that (that (that this)))\n \u0394v = var (that (that this))\n -- m = var (that this) -- unused parameter\n \u0394m = var this\n in\n abs (abs (abs (abs (abs (abs\n (insert (apply \u0394k k) (apply \u0394v v)\n (delete k v \u0394m)))))))\n\n-- \u0394zip f \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082 | true? (f \u2295 \u0394f \u2261 f)\n--\n-- ... | true =\n-- zip (\u03bb k \u0394v\u2081 \u0394v\u2082 \u2192 \u0394f (lookup k m\u2081) \u0394v\u2081 (lookup k m\u2082) \u0394v\u2082)\n-- \u0394m\u2081 \u0394m\u2082\n--\n-- ... | false = zip\u2084 \u0394f m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082\n--\n-- we implement the false-branch for the moment.\nAtlas-\u0394const zip =\n let\n \u0394f = var (that (that (that (that this))))\n m\u2081 = var (that (that (that this)))\n \u0394m\u2081 = var (that (that this))\n m\u2082 = var (that this)\n \u0394m\u2082 = var this\n g = abs (app (app (weaken\u2081 \u0394f) (var this)) nil-term)\n in\n abs (abs (abs (abs (abs (abs (zip4! g m\u2081 \u0394m\u2081 m\u2082 \u0394m\u2082))))))\n\nAtlas = calculus-with\n Atlas-type\n Atlas-const\n Atlas-lookup\n Atlas-\u0394type\n Atlas-\u0394const\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"fb83ee7872bb6b8aba0c02639123149953ef0d90","subject":"s\/Mu\/\u03bc and s\/Nu\/\u03bd.","message":"s\/Mu\/\u03bc and s\/Nu\/\u03bd.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/fixed-points\/Functors.agda","new_file":"notes\/fixed-points\/Functors.agda","new_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Functors where\n\n-- The carrier of the initial algebra is (up to isomorphism) a\n-- fixed-point of the functor (Vene 2000, p).\n\n------------------------------------------------------------------------------\n\ndata Bool : Set where\n false true : Bool\n\ndata _\u228e_ (A B : Set) : Set where\n inl : A \u2192 A \u228e B\n inr : B \u2192 A \u228e B\n\ndata _\u00d7_ (A B : Set) : Set where\n _,_ : A \u2192 B \u2192 A \u00d7 B\n\n-- The terminal object.\ndata One : Set where\n one : One\n\npostulate\n -- The least fixed-point.\n\n -- Haskell definitions:\n\n -- data Mu f = In (f (Mu f))\n\n -- unIn :: Mu f \u2192 f (Mu f)\n -- unIn (In x) = x\n\n \u03bc : (Set \u2192 Set) \u2192 Set\n In : {F : Set \u2192 Set} \u2192 F (\u03bc F) \u2192 \u03bc F\n unIn : {F : Set \u2192 Set} \u2192 \u03bc F \u2192 F (\u03bc F)\n\npostulate\n -- The greatest fixed-point.\n\n -- Haskell definitions:\n\n -- data Nu f = Wrap (f (Nu f))\n\n -- out :: Nu f \u2192 (f (Nu f))\n -- out (Wrap x) = x\n\n \u03bd : (Set \u2192 Set) \u2192 Set\n Wrap : {F : Set \u2192 Set} \u2192 F (\u03bd F) \u2192 \u03bd F\n out : {F : Set \u2192 Set} \u2192 \u03bd F \u2192 F (\u03bd F)\n\n------------------------------------------------------------------------------\n-- Functors\n\n-- The identity functor (the functor for the empty and unit types).\nIdF : Set \u2192 Set\nIdF X = X\n\n-- The (co)natural numbers functor.\nNatF : Set \u2192 Set\nNatF X = One \u228e X\n\n-- The (co)list functor.\nListF : Set \u2192 Set \u2192 Set\nListF A X = One \u228e (A \u00d7 X)\n\n-- The stream functor.\nStreamF : Set \u2192 Set \u2192 Set\nStreamF A X = A \u00d7 X\n\n------------------------------------------------------------------------------\n-- Types as least fixed-points\n\n-- The empty type is a least fixed-point.\n\u22a5 : Set\n\u22a5 = \u03bc IdF\n\n-- The natural numbers type is a least fixed-point.\nN : Set\nN = \u03bc NatF\n\n-- The data constructors for the natural numbers.\nzero : N\nzero = In (inl one)\n\nsucc : N \u2192 N\nsucc n = In (inr n)\n\n-- The list type is a least fixed-point.\nList : Set \u2192 Set\nList A = \u03bc (ListF A)\n\n-- The data constructors for List.\nnil : {A : Set} \u2192 List A\nnil = In (inl one)\n\ncons : {A : Set} \u2192 A \u2192 List A \u2192 List A\ncons x xs = In (inr (x , xs))\n\n------------------------------------------------------------------------------\n-- Types as greatest fixed-points\n\n-- The unit type is a greatest fixed-point.\nUnit : Set\nUnit = \u03bd IdF\n\n-- Non-structural recursion\n-- unit : Nu IdF\n-- unit = Wrap IdF {!unit!}\n\n-- The conaturals type is a greatest fixed-point.\nConat : Set\nConat = \u03bd NatF\n\nzeroC : Conat\nzeroC = Wrap (inl one)\n\nsuccC : Conat \u2192 Conat\nsuccC cn = Wrap (inr cn)\n\n-- Non-structural recursion for the definition of \u221eC.\n-- \u221eC : Conat\n-- \u221eC = succC {!\u221eC!}\n\n-- TODO: The conat destructor.\npred : Conat \u2192 One \u228e Conat\npred cn with out cn\n... | inl _ = inl one\n... | inr x = inr x\n\n-- The colist type is a greatest fixed-point.\nColist : Set \u2192 Set\nColist A = \u03bd (ListF A)\n\n-- The colist data constructors.\nnilCL : {A : Set} \u2192 Colist A\nnilCL = Wrap (inl one)\n\nconsCL : {A : Set} \u2192 A \u2192 Colist A \u2192 Colist A\nconsCL x xs = Wrap (inr (x , xs))\n\n-- The colist destructors.\nnullCL : {A : Set} \u2192 Colist A \u2192 Bool\nnullCL xs with out xs\n... | inl _ = true\n... | inr _ = false\n\n-- headCL : {A : Set} \u2192 Colist A \u2192 A\n-- headCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (x , _) = x\n\n-- tailCL : {A : Set} \u2192 Colist A \u2192 Colist A\n-- tailCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (_ , xs') = xs'\n\n-- The stream type is a greatest fixed-point.\nStream : Set \u2192 Set\nStream A = \u03bd (StreamF A)\n\n-- The stream data constructor.\nconsS : {A : Set} \u2192 A \u2192 Stream A \u2192 Stream A\nconsS x xs = Wrap (x , xs)\n\n-- The stream destructors.\nheadS : {A : Set} \u2192 Stream A \u2192 A\nheadS xs with out xs\n... | x , _ = x\n\ntailS : {A : Set} \u2192 Stream A \u2192 Stream A\ntailS xs with out xs\n... | _ , xs' = xs'\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Vene, Varmo (2000). Categorical programming with inductive and\n-- coinductive types. PhD thesis. Faculty of Mathematics: University\n-- of Tartu.\n","old_contents":"{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule Functors where\n\n-- The carrier of the initial algebra is (up to isomorphism) a\n-- fixed-point of the functor (Vene 2000, p).\n\n------------------------------------------------------------------------------\n\ndata Bool : Set where\n false true : Bool\n\ndata _\u228e_ (A B : Set) : Set where\n inl : A \u2192 A \u228e B\n inr : B \u2192 A \u228e B\n\ndata _\u00d7_ (A B : Set) : Set where\n _,_ : A \u2192 B \u2192 A \u00d7 B\n\n-- The terminal object.\ndata One : Set where\n one : One\n\npostulate\n -- The least fixed-point.\n -- The names came from the Haskell definitions\n\n -- data Mu f = In (f (Mu f))\n\n -- unIn :: Mu f \u2192 f (Mu f)\n -- unIn (In x) = x\n\n Mu : (Set \u2192 Set) \u2192 Set\n In : {f : Set \u2192 Set} \u2192 f (Mu f) \u2192 Mu f\n unIn : {f : Set \u2192 Set} \u2192 Mu f \u2192 f (Mu f)\n\npostulate\n -- The greatest fixed-point.\n -- The names came from the Haskell definitions\n\n -- data Nu f = Wrap (f (Nu f))\n\n -- out :: Nu f \u2192 (f (Nu f))\n -- out (Wrap x) = x\n\n Nu : (Set \u2192 Set) \u2192 Set\n Wrap : {f : Set \u2192 Set} \u2192 f (Nu f) \u2192 Nu f\n out : {f : Set \u2192 Set} \u2192 Nu f \u2192 f (Nu f)\n\n------------------------------------------------------------------------------\n-- Functors\n\n-- The identity functor (the functor for the empty and unit types).\nIdF : Set \u2192 Set\nIdF X = X\n\n-- The (co)natural numbers functor.\nNatF : Set \u2192 Set\nNatF X = One \u228e X\n\n-- The (co)list functor.\nListF : Set \u2192 Set \u2192 Set\nListF A X = One \u228e (A \u00d7 X)\n\n-- The stream functor.\nStreamF : Set \u2192 Set \u2192 Set\nStreamF A X = A \u00d7 X\n\n------------------------------------------------------------------------------\n-- Types as least fixed-points\n\n-- The empty type is a least fixed-point.\n\u22a5 : Set\n\u22a5 = Mu IdF\n\n-- The natural numbers type is a least fixed-point.\nN : Set\nN = Mu NatF\n\n-- The data constructors for the natural numbers.\nzero : N\nzero = In (inl one)\n\nsucc : N \u2192 N\nsucc n = In (inr n)\n\n-- The list type is a least fixed-point.\nList : Set \u2192 Set\nList A = Mu (ListF A)\n\n-- The data constructors for List.\nnil : {A : Set} \u2192 List A\nnil = In (inl one)\n\ncons : {A : Set} \u2192 A \u2192 List A \u2192 List A\ncons x xs = In (inr (x , xs))\n\n------------------------------------------------------------------------------\n-- Types as greatest fixed-points\n\n-- The unit type is a greatest fixed-point.\nUnit : Set\nUnit = Nu IdF\n\n-- Non-structural recursion\n-- unit : Nu IdF\n-- unit = Wrap IdF {!unit!}\n\n-- The conaturals type is a greatest fixed-point.\nConat : Set\nConat = Nu NatF\n\nzeroC : Conat\nzeroC = Wrap (inl one)\n\nsuccC : Conat \u2192 Conat\nsuccC cn = Wrap (inr cn)\n\n-- Non-structural recursion for the definition of \u221eC.\n-- \u221eC : Conat\n-- \u221eC = succC {!\u221eC!}\n\n-- TODO: The conat destructor.\npred : Conat \u2192 One \u228e Conat\npred cn with out cn\n... | inl _ = inl one\n... | inr x = inr x\n\n-- The colist type is a greatest fixed-point.\nColist : Set \u2192 Set\nColist A = Nu (ListF A)\n\n-- The colist data constructors.\nnilCL : {A : Set} \u2192 Colist A\nnilCL = Wrap (inl one)\n\nconsCL : {A : Set} \u2192 A \u2192 Colist A \u2192 Colist A\nconsCL x xs = Wrap (inr (x , xs))\n\n-- The colist destructors.\nnullCL : {A : Set} \u2192 Colist A \u2192 Bool\nnullCL xs with out xs\n... | inl _ = true\n... | inr _ = false\n\n-- headCL : {A : Set} \u2192 Colist A \u2192 A\n-- headCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (x , _) = x\n\n-- tailCL : {A : Set} \u2192 Colist A \u2192 Colist A\n-- tailCL {A} xs with out (ListF A) xs\n-- ... | inl t = -- Impossible\n-- ... | inr (_ , xs') = xs'\n\n-- The stream type is a greatest fixed-point.\nStream : Set \u2192 Set\nStream A = Nu (StreamF A)\n\n-- The stream data constructor.\nconsS : {A : Set} \u2192 A \u2192 Stream A \u2192 Stream A\nconsS x xs = Wrap (x , xs)\n\n-- The stream destructors.\nheadS : {A : Set} \u2192 Stream A \u2192 A\nheadS xs with out xs\n... | x , _ = x\n\ntailS : {A : Set} \u2192 Stream A \u2192 Stream A\ntailS xs with out xs\n... | _ , xs' = xs'\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Vene, Varmo (2000). Categorical programming with inductive and\n-- coinductive types. PhD thesis. Faculty of Mathematics: University\n-- of Tartu.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"233ce4867a6fc1f8c29c642b2516cc11c7edd483","subject":"Implement apply-term and diff-term directly.","message":"Implement apply-term and diff-term directly.\n\nBefore this commit: Mutual recursion of apply-term and diff-term encoded\nexplicitly via a product type. After this commit: Mutual recursion\nexpressed with Agda mutual recursion.\n\nOld-commit-hash: 9a7571f8ef5cf6765629a001c46870e002f54d7e\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Term.agda","new_file":"Parametric\/Change\/Term.agda","new_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Constant\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\nDiffStructure : Set\nDiffStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))\n\nApplyStructure : Set\nApplyStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\n\nmodule Structure\n (diff-base : DiffStructure)\n (apply-base : ApplyStructure)\n where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n diff-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n apply-term : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n diff-term {base \u03b9} = diff-base\n diff-term {\u03c3 \u21d2 \u03c4} =\n (let\n _\u229d\u03c4_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff-term {\u03c4} {\u0393}) s t\n _\u2295\u03c3_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply-term {\u03c3} {\u0393}) \u0394t t\n in\n abs\u2084 (\u03bb g f x \u0394x \u2192 app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))\n\n apply-term {base \u03b9} = apply-base\n apply-term {\u03c3 \u21d2 \u03c4} =\n (let\n _\u229d\u03c3_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff-term {\u03c3} {\u0393}) s t\n _\u2295\u03c4_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply-term {\u03c4} {\u0393}) \u0394t t\n in\n abs\u2083 (\u03bb \u0394h h y \u2192 app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y)))\n\n diff : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 (\u0394Type \u03c4)\n diff = app\u2082 diff-term\n\n apply : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\n apply = app\u2082 apply-term\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\nimport Parametric.Change.Type as ChangeType\n\nmodule Parametric.Change.Term\n {Base : Set}\n (Constant : Term.Structure Base)\n (\u0394Base : ChangeType.Structure Base)\n where\n\n-- Terms that operate on changes\n\nopen Type.Structure Base\nopen Term.Structure Base Constant\nopen ChangeType.Structure Base \u0394Base\n\nopen import Data.Product\n\nDiffStructure : Set\nDiffStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (base \u03b9 \u21d2 base \u03b9 \u21d2 base (\u0394Base \u03b9))\n\nApplyStructure : Set\nApplyStructure = \u2200 {\u03b9 \u0393} \u2192 Term \u0393 (\u0394Type (base \u03b9) \u21d2 base \u03b9 \u21d2 base \u03b9)\n\nmodule Structure\n (diff-base : DiffStructure)\n (apply-base : ApplyStructure)\n where\n\n -- g \u229d f = \u03bb x . \u03bb \u0394x . g (x \u2295 \u0394x) \u229d f x\n -- f \u2295 \u0394f = \u03bb x . f x \u2295 \u0394f x (x \u229d x)\n\n lift-diff-apply :\n \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4) \u00d7 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n lift-diff-apply {base \u03b9} = diff-base , apply-base\n lift-diff-apply {\u03c3 \u21d2 \u03c4} =\n (let\n diff\u03c4 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply {\u03c4} {\u0393})\n apply\u03c3 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply {\u03c3} {\u0393})\n _\u229d\u03c4_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff\u03c4 {\u0393}) s t\n _\u2295\u03c3_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply\u03c3 {\u0393}) \u0394t t\n in\n abs\u2084 (\u03bb g f x \u0394x \u2192 app f (x \u2295\u03c3 \u0394x) \u229d\u03c4 app g x))\n ,\n (let\n diff\u03c3 = \u03bb {\u0393} \u2192 proj\u2081 (lift-diff-apply {\u03c3} {\u0393})\n apply\u03c4 = \u03bb {\u0393} \u2192 proj\u2082 (lift-diff-apply {\u03c4} {\u0393})\n _\u229d\u03c3_ = \u03bb {\u0393} s t \u2192 app\u2082 (diff\u03c3 {\u0393}) s t\n _\u2295\u03c4_ = \u03bb {\u0393} t \u0394t \u2192 app\u2082 (apply\u03c4 {\u0393}) \u0394t t\n in\n abs\u2083 (\u03bb \u0394h h y \u2192 app h y \u2295\u03c4 app (app \u0394h y) (y \u229d\u03c3 y)))\n\n diff-term :\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u03c4 \u21d2 \u03c4 \u21d2 \u0394Type \u03c4)\n\n diff-term = \u03bb {\u03c4 \u0393} \u2192\n proj\u2081 (lift-diff-apply {\u03c4} {\u0393})\n\n apply-term :\n \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\n\n apply-term = \u03bb {\u03c4 \u0393} \u2192\n proj\u2082 (lift-diff-apply {\u03c4} {\u0393})\n\n diff : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 \u03c4 \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 (\u0394Type \u03c4)\n diff = app\u2082 diff-term\n\n apply : \u2200 {\u03c4 \u0393} \u2192\n Term \u0393 (\u0394Type \u03c4) \u2192 Term \u0393 \u03c4 \u2192\n Term \u0393 \u03c4\n apply = app\u2082 apply-term\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"4e5025e3160f775b4805a504187a9e95c764c9b1","subject":"Type.Identities: some more identities","message":"Type.Identities: some more identities\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Type\/Identities.agda","new_file":"lib\/Type\/Identities.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Type\n\nopen import Level.NP\nopen import HoTT\nopen import Function.NP\nopen import Function.Extensionality\n\nopen import Data.Maybe.NP using (Maybe ; just ; nothing ; maybe ; maybe\u2032 ; just-injective)\nopen import Data.Zero using (\ud835\udfd8 ; \ud835\udfd8-elim)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Two using (\ud835\udfda ; 0\u2082 ; 1\u2082 ; [0:_1:_]; twist)\nopen import Data.Fin as Fin using (Fin ; suc ; zero)\nopen import Data.Nat.NP as \u2115 using (\u2115 ; suc ; zero)\nopen import Data.Product.NP renaming (proj\u2081 to fst; proj\u2082 to snd)\nopen import Data.Sum using (_\u228e_) renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_ ; ap; coe; coe!; !_; _\u2219_; J ; inspect ; Reveal_is_ ; [_]; tr) renaming (refl to idp; cong\u2082 to ap\u2082; _\u2257_ to _\u223c_)\n\nmodule Type.Identities where\n\nopen Equivalences\n\n\n-- for use with ap\u2082 etc.\n_\u27f6_ : \u2200 {a b} \u2192 \u2605_ a \u2192 \u2605_ b \u2192 \u2605_ (b \u2294 a)\nA \u27f6 B = A \u2192 B\n\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 : \ud835\udfda \u2243 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 = equiv [0: inl _ 1: inr _ ]\n [inl: const 0\u2082 ,inr: const 1\u2082 ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n [0: idp 1: idp ]\n\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 : {{_ : UA}} \u2192 \ud835\udfda \u2261 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 = ua \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9\n\nmodule _ {{_ : UA}}{A : \u2605}{B C : A \u2192 \u2605} where\n \u03a3\u228e-split : (\u03a3 A (\u03bb x \u2192 B x \u228e C x)) \u2261 (\u03a3 A B \u228e \u03a3 A C)\n \u03a3\u228e-split = ua (equiv (\u03bb { (x , inl y) \u2192 inl (x , y)\n ; (x , inr y) \u2192 inr (x , y) })\n (\u03bb { (inl (x , y)) \u2192 x , inl y\n ; (inr (x , y)) \u2192 x , inr y })\n (\u03bb { (inl (x , y)) \u2192 idp\n ; (inr (x , y)) \u2192 idp })\n (\u03bb { (x , inl y) \u2192 idp\n ; (x , inr y) \u2192 idp }))\n\nmodule _ {{_ : UA}}{A B : \u2605}{C : A \u228e B \u2192 \u2605} where\n dist-\u228e-\u03a3-equiv : \u03a3 (A \u228e B) C \u2243 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u03a3 : \u03a3 (A \u228e B) C \u2261 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3 = ua dist-\u228e-\u03a3-equiv\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0-equiv : \u03a0 (A \u228e B) C \u2243 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u03a0 : \u03a0 (A \u228e B) C \u2261 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0 = ua dist-\u00d7-\u03a0-equiv\n\nmodule _ {{_ : UA}}{A B : \u2605}{C : A \u2192 \u2605}{D : B \u2192 \u2605} where\n dist-\u228e-\u03a3[] : (\u03a3 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a3 A C \u228e \u03a3 B D)\n dist-\u228e-\u03a3[] = dist-\u228e-\u03a3\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0[] : (\u03a0 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a0 A C \u00d7 \u03a0 B D)\n dist-\u00d7-\u03a0[] = dist-\u00d7-\u03a0\n\nmodule _ {{_ : UA}}{A B C : \u2605} where\n dist-\u228e-\u00d7-equiv : ((A \u228e B) \u00d7 C) \u2243 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u00d7 : ((A \u228e B) \u00d7 C) \u2261 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7 = ua dist-\u228e-\u00d7-equiv\n\n module _ {{_ : FunExt}} where\n dist-\u00d7-\u2192-equiv : ((A \u228e B) \u2192 C ) \u2243 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u2192 : ((A \u228e B) \u2192 C) \u2261 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192 = ua dist-\u00d7-\u2192-equiv\n\nmodule _ {A : \u2605}{B : A \u2192 \u2605}{C : (x : A) \u2192 B x \u2192 \u2605} where\n \u03a3-assoc-equiv : (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2243 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc-equiv = equiv (\u03bb x \u2192 (fst x , fst (snd x)) , snd (snd x))\n (\u03bb x \u2192 fst (fst x) , snd (fst x) , snd x)\n (\u03bb y \u2192 idp)\n (\u03bb y \u2192 idp)\n\n \u03a3-assoc : {{_ : UA}} \u2192 (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2261 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc = ua \u03a3-assoc-equiv\n\nmodule _ {A B : \u2605} where\n \u00d7-comm-equiv : (A \u00d7 B) \u2243 (B \u00d7 A)\n \u00d7-comm-equiv = equiv swap swap (\u03bb y \u2192 idp) (\u03bb x \u2192 idp)\n\n \u00d7-comm : {{_ : UA}} \u2192 (A \u00d7 B) \u2261 (B \u00d7 A)\n \u00d7-comm = ua \u00d7-comm-equiv\n\n \u228e-comm-equiv : (A \u228e B) \u2243 (B \u228e A)\n \u228e-comm-equiv = equiv [inl: inr ,inr: inl ]\n [inl: inr ,inr: inl ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n\n \u228e-comm : {{_ : UA}} \u2192 (A \u228e B) \u2261 (B \u228e A)\n \u228e-comm = ua \u228e-comm-equiv\n\nmodule _ {A B : \u2605}{C : A \u2192 B \u2192 \u2605} where\n \u03a0\u03a0-comm-equiv : ((x : A)(y : B) \u2192 C x y) \u2243 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm-equiv = equiv flip flip (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a0\u03a0-comm : {{_ : UA}} \u2192 ((x : A)(y : B) \u2192 C x y) \u2261 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm = ua \u03a0\u03a0-comm-equiv\n\n \u03a3\u03a3-comm-equiv : (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2243 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm-equiv = equiv (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb _ \u2192 idp)\n (\u03bb _ \u2192 idp)\n\n \u03a3\u03a3-comm : {{_ : UA}} \u2192 (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2261 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm = ua \u03a3\u03a3-comm-equiv\n\nmodule _ {A B C : \u2605} where\n \u00d7-assoc : {{_ : UA}} \u2192 (A \u00d7 (B \u00d7 C)) \u2261 ((A \u00d7 B) \u00d7 C)\n \u00d7-assoc = \u03a3-assoc\n\n \u228e-assoc-equiv : (A \u228e (B \u228e C)) \u2243 ((A \u228e B) \u228e C)\n \u228e-assoc-equiv = equiv [inl: inl \u2218 inl ,inr: [inl: inl \u2218 inr ,inr: inr ] ]\n [inl: [inl: inl ,inr: inr \u2218 inl ] ,inr: inr \u2218 inr ]\n [inl: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ]\n\n \u228e-assoc : {{_ : UA}} \u2192 (A \u228e (B \u228e C)) \u2261 ((A \u228e B) \u228e C)\n \u228e-assoc = ua \u228e-assoc-equiv\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \ud835\udfd8 \u2192 \u2605) where\n \u03a0\ud835\udfd8-uniq : \u03a0 \ud835\udfd8 A \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq = ua (equiv _ (\u03bb _ ()) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= (\u03bb())))\n\nmodule _ {{_ : UA}}(A : \ud835\udfd9 \u2192 \u2605) where\n \u03a0\ud835\udfd9-uniq : \u03a0 \ud835\udfd9 A \u2261 A _\n \u03a0\ud835\udfd9-uniq = ua (equiv (\u03bb f \u2192 f _) (\u03bb x _ \u2192 x) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\nmodule _ {{_ : UA}}(A : \u2605) where\n \u03a0\ud835\udfd9-uniq\u2032 : (\ud835\udfd9 \u2192 A) \u2261 A\n \u03a0\ud835\udfd9-uniq\u2032 = \u03a0\ud835\udfd9-uniq (\u03bb _ \u2192 A)\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \u2605) where\n \u03a0\ud835\udfd8-uniq\u2032 : (\ud835\udfd8 \u2192 A) \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq\u2032 = \u03a0\ud835\udfd8-uniq (\u03bb _ \u2192 A)\n\n \ud835\udfd8\u2192A\u2194\ud835\udfd9 = \u03a0\ud835\udfd8-uniq\u2032\n\nmodule _ {{_ : FunExt}}(F G : \ud835\udfd8 \u2192 \u2605) where\n -- also by \u03a0\ud835\udfd8-uniq twice\n \u03a0\ud835\udfd8-uniq' : \u03a0 \ud835\udfd8 F \u2261 \u03a0 \ud835\udfd8 G\n \u03a0\ud835\udfd8-uniq' = \u03a0=\u2032 \ud835\udfd8 (\u03bb())\n\nmodule _ {A : \ud835\udfd8 \u2192 \u2605} where\n \u03a3\ud835\udfd8-fst-equiv : \u03a3 \ud835\udfd8 A \u2243 \ud835\udfd8\n \u03a3\ud835\udfd8-fst-equiv = equiv fst (\u03bb()) (\u03bb()) (\u03bb { (() , _) })\n\n \u03a3\ud835\udfd8-fst : {{_ : UA}} \u2192 \u03a3 \ud835\udfd8 A \u2261 \ud835\udfd8\n \u03a3\ud835\udfd8-fst = ua \u03a3\ud835\udfd8-fst-equiv\n\nmodule _ {A : \ud835\udfd9 \u2192 \u2605} where\n \u03a3\ud835\udfd9-snd-equiv : \u03a3 \ud835\udfd9 A \u2243 A _\n \u03a3\ud835\udfd9-snd-equiv = equiv snd (_,_ _) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a3\ud835\udfd9-snd : {{_ : UA}} \u2192 \u03a3 \ud835\udfd9 A \u2261 A _\n \u03a3\ud835\udfd9-snd = ua \u03a3\ud835\udfd9-snd-equiv\n\nmodule _ {A : \u2605} where\n \u228e\ud835\udfd8-inl-equiv : A \u2243 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl-equiv = equiv inl [inl: id ,inr: (\u03bb()) ] [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb()) ] (\u03bb _ \u2192 idp)\n\n \u228e\ud835\udfd8-inl : {{_ : UA}} \u2192 A \u2261 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl = ua \u228e\ud835\udfd8-inl-equiv\n\n \ud835\udfd9\u00d7-snd : {{_ : UA}} \u2192 (\ud835\udfd9 \u00d7 A) \u2261 A\n \ud835\udfd9\u00d7-snd = \u03a3\ud835\udfd9-snd\n\n \u00d7\ud835\udfd9-fst : {{_ : UA}} \u2192 (A \u00d7 \ud835\udfd9) \u2261 A\n \u00d7\ud835\udfd9-fst = \u00d7-comm \u2219 \ud835\udfd9\u00d7-snd\n\n -- old names\n A\u00d7\ud835\udfd9\u2261A = \u00d7\ud835\udfd9-fst\n \ud835\udfd9\u00d7A\u2261A = \ud835\udfd9\u00d7-snd\n\nmodule _ {A : \u2605}{B : A \u2192 \u2605}{C : \u03a3 A B \u2192 \u2605} where\n -- AC: Dependent axiom of choice\n -- In Type Theory it happens to be neither an axiom nor to be choosing anything.\n \u03a0\u03a3-comm-equiv : (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2243 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm-equiv = equiv (\u03bb H \u2192 fst \u2218 H , snd \u2218 H) (\u03bb H \u2192 < fst H , snd H >) (\u03bb H \u2192 idp) (\u03bb H \u2192 idp)\n\n \u03a0\u03a3-comm : {{_ : UA}}\n \u2192 (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2261 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm = ua \u03a0\u03a3-comm-equiv\n\nmodule _ {A : \ud835\udfda \u2192 \u2605}{{_ : UA}}{{_ : FunExt}} where\n \u03a3\ud835\udfda-\u228e : \u03a3 \ud835\udfda A \u2261 (A 0\u2082 \u228e A 1\u2082)\n \u03a3\ud835\udfda-\u228e = \u03a3-first \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 \u2219 dist-\u228e-\u03a3 \u2219 ap\u2082 _\u228e_ \u03a3\ud835\udfd9-snd \u03a3\ud835\udfd9-snd\n\n \u03a0\ud835\udfda-\u00d7 : \u03a0 \ud835\udfda A \u2261 (A 0\u2082 \u00d7 A 1\u2082)\n \u03a0\ud835\udfda-\u00d7 = \u03a0-first \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 \u2219 dist-\u00d7-\u03a0 \u2219 ap\u2082 _\u00d7_ (\u03a0\ud835\udfd9-uniq _) (\u03a0\ud835\udfd9-uniq _)\n\n \u03a0\ud835\udfdaF\u2194F\u2080\u00d7F\u2081 = \u03a0\ud835\udfda-\u00d7\n\nmodule _ {A : \u2605}{{_ : UA}}{{_ : FunExt}} where\n \u03a3\ud835\udfda-\u228e\u2032 : \ud835\udfda \u00d7 A \u2261 (A \u228e A)\n \u03a3\ud835\udfda-\u228e\u2032 = \u03a3\ud835\udfda-\u228e\n\n \ud835\udfda\u2192A\u2194A\u00d7A : (\ud835\udfda \u2192 A) \u2194 (A \u00d7 A)\n \ud835\udfda\u2192A\u2194A\u00d7A\n\nmodule _ where\n\n private\n maybe-elim : {X : Set}(m : Maybe X) \u2192 m \u2262 nothing \u2192 X\n maybe-elim {X} m = maybe {B = \u03bb m' \u2192 m' \u2262 _ \u2192 _} (\u03bb x _ \u2192 x) (\u03bb e \u2192 \ud835\udfd8-elim (e idp)) m\n\n maybe-elim-just : {X : Set}(m : Maybe X)(p : m \u2262 nothing)\n \u2192 just (maybe-elim m p) \u2261 m\n maybe-elim-just (just x) p = idp\n maybe-elim-just nothing p = \ud835\udfd8-elim (p idp)\n\n maybe-elim-cong : \u2200 {X : Set}{m m' : Maybe X}{p p'} \u2192 m \u2261 m'\n \u2192 maybe-elim m p \u2261 maybe-elim m' p'\n maybe-elim-cong {m = just x} idp = idp\n maybe-elim-cong {m = nothing} {p = p} idp = \ud835\udfd8-elim (p idp)\n\n not-just : {X : Set}{x : X} \u2192 _\u2262_ {A = Maybe X} (just x) nothing\n not-just ()\n\n !!-p : \u2200 {x}{X : Set x}{x y : X}(p : x \u2261 y) \u2192 ! (! p) \u2261 p\n !!-p idp = idp\n\n !\u2219 : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 ! p \u2219 p \u2261 idp\n !\u2219 idp = idp\n\n \u2219! : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 p \u2219 ! p \u2261 idp\n \u2219! idp = idp\n\n\n record PreservePoint {A B : Set}(eq : Maybe A \u2261 Maybe B) : Set where\n field\n coe-p : \u2200 x \u2192 coe eq (just x) \u2262 nothing\n coe!-p : \u2200 x \u2192 coe! eq (just x) \u2262 nothing\n open PreservePoint\n\n nothing-to-nothing : {A B : Set}(eq : Maybe A \u2261 Maybe B)\n \u2192 coe eq nothing \u2261 nothing \u2192 PreservePoint eq\n nothing-to-nothing eq n2n = record { coe-p = \u03bb x p \u2192 not-just (coe-inj eq (p \u2219 ! n2n))\n ; coe!-p = \u03bb x p \u2192 not-just (coe-same (! !\u2219 eq) (just x)\n \u2219 ! (coe\u2218coe eq (! eq) (just x)) \u2219 ap (coe eq) p \u2219 n2n) }\n\n !-PP : \u2200 {A B : Set} {e : Maybe A \u2261 Maybe B} \u2192 PreservePoint e \u2192 PreservePoint (! e)\n !-PP {e = e} e-pp = record { coe-p = coe!-p e-pp\n ; coe!-p = \u03bb x p \u2192 coe-p e-pp x ( ap (\u03bb X \u2192 coe X (just x)) (! !!-p e ) \u2219 p) }\n\n to : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e \u2192 A \u2192 B\n to e e-pp x = maybe-elim (coe e (just x)) (coe-p e-pp x)\n\n module _{A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}{ep ep'}(e'\u2219e=id : e' \u2219 e \u2261 idp)(x : B) where\n to-to : to e ep (to e' ep' x) \u2261 x\n to-to = just-injective (ap just (maybe-elim-cong\n {m = coe e (just (maybe-elim (coe e' (just x)) _))}\n {m' = coe e (coe e' (just x))}\n {p' = \u03bb q \u2192 not-just (! p \u2219 q)} (ap (coe e) (maybe-elim-just _ _)))\n \u2219 maybe-elim-just (coe e (coe e' (just x))) (\u03bb q \u2192 not-just (! p \u2219 q))\n \u2219 p)\n where\n p : coe e (coe e' (just x)) \u2261 just x\n p = coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n\n cto : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 Maybe A \u2192 Maybe B\n cto e = maybe (maybe\u2032 just (coe e nothing) \u2218 coe e \u2218 just) nothing\n\n module _ {A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}(e'\u2219e=id : e' \u2219 e \u2261 idp) where\n cto-cto : (x : Maybe B) \u2192 cto e (cto e' x) \u2261 x\n cto-cto nothing = idp\n cto-cto (just x) with coe e' (just x) | coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n cto-cto (just x) | just x\u2081 | p = ap (maybe just (coe e nothing)) p\n cto-cto (just x) | nothing | p with coe e' nothing\n | coe\u2218coe e e' nothing \u2219 coe-same e'\u2219e=id nothing\n cto-cto (just x) | nothing | p | just y | q = ap (maybe just (coe e nothing)) q \u2219 p\n cto-cto (just x) | nothing | p | nothing | q = ! q \u2219 p\n\n module _ {A B : Set} where\n create-PP-\u2243 : Maybe A \u2261 Maybe B \u2192 Maybe A \u2243 Maybe B\n create-PP-\u2243 e = equiv (cto e)\n (cto (! e))\n (cto-cto {e = e} {e' = ! e} (!\u2219 e))\n (cto-cto {e = ! e}{e' = e} (\u2219! e))\n\n create-PP : {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 \u03a3 (Maybe A \u2261 Maybe B) PreservePoint\n create-PP eq = ua (create-PP-\u2243 eq) , nothing-to-nothing _ (coe-\u03b2 (create-PP-\u2243 eq) nothing)\n\n Maybe-injective-PP : {{_ : UA}} \u2192 (e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e\n \u2192 A \u2261 B\n Maybe-injective-PP e e-pp = ua\n (equiv (to e e-pp)\n (to (! e) (!-PP e-pp))\n (to-to {e = e} { ! e} {e-pp}{ !-PP e-pp} (!\u2219 e))\n (to-to {e = ! e} {e} { !-PP e-pp} {e-pp} (\u2219! e)))\n\n Maybe-injective : \u2200 {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 A \u2261 B\n Maybe-injective e = let (e' , e'-pp) = create-PP e in Maybe-injective-PP e' e'-pp\n\n\nmodule _ {a}{A : \u2605_ a} where\n Maybe\u2243\ud835\udfd9\u228e : Maybe A \u2243 (\ud835\udfd9 \u228e A)\n Maybe\u2243\ud835\udfd9\u228e = equiv (maybe inr (inl _)) [inl: const nothing ,inr: just ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (maybe (\u03bb x \u2192 idp) idp)\n\n Maybe\u2261\ud835\udfd9\u228e : \u2200 {{_ : UA}}\u2192 Maybe A \u2261 (\ud835\udfd9 \u228e A)\n Maybe\u2261\ud835\udfd9\u228e = ua Maybe\u2243\ud835\udfd9\u228e\n\nmodule _ {{_ : UA}} where\n Fin0\u2261\ud835\udfd8 : Fin 0 \u2261 \ud835\udfd8\n Fin0\u2261\ud835\udfd8 = ua (equiv (\u03bb ()) (\u03bb ()) (\u03bb ()) (\u03bb ()))\n\n Fin1\u2261\ud835\udfd9 : Fin 1 \u2261 \ud835\udfd9\n Fin1\u2261\ud835\udfd9 = ua (equiv _ (\u03bb _ \u2192 zero) (\u03bb _ \u2192 idp) \u03b2)\n where \u03b2 : (_ : Fin 1) \u2192 _\n \u03b2 zero = idp\n \u03b2 (suc ())\n\nmodule _ where\n isZero? : \u2200 {n}{A : Fin (suc n) \u2192 Set} \u2192 ((i : Fin n) \u2192 A (suc i)) \u2192 A zero\n \u2192 (i : Fin (suc n)) \u2192 A i\n isZero? f x zero = x\n isZero? f x (suc i) = f i\n\n Fin\u2218suc\u2243\ud835\udfd9\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2243 (\ud835\udfd9 \u228e Fin n)\n Fin\u2218suc\u2243\ud835\udfd9\u228eFin = equiv (isZero? inr (inl _)) [inl: (\u03bb _ \u2192 zero) ,inr: suc ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n (isZero? (\u03bb _ \u2192 idp) idp)\n\n Fin\u2218suc\u2261\ud835\udfd9\u228eFin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 (\ud835\udfd9 \u228e Fin n)\n Fin\u2218suc\u2261\ud835\udfd9\u228eFin = ua Fin\u2218suc\u2243\ud835\udfd9\u228eFin\n\nFin-\u228e-+ : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u228e Fin n) \u2261 Fin (m \u2115.+ n)\nFin-\u228e-+ {zero} = ap\u2082 _\u228e_ Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm \u2219 ! \u228e\ud835\udfd8-inl\nFin-\u228e-+ {suc m} = ap\u2082 _\u228e_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ap (_\u228e_ \ud835\udfd9) Fin-\u228e-+ \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\n\nFin-\u00d7-* : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u00d7 Fin n) \u2261 Fin (m \u2115.* n)\nFin-\u00d7-* {zero} = ap\u2082 _\u00d7_ Fin0\u2261\ud835\udfd8 idp \u2219 \u03a3\ud835\udfd8-fst \u2219 ! Fin0\u2261\ud835\udfd8\nFin-\u00d7-* {suc m} = ap\u2082 _\u00d7_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u228e-\u00d7 \u2219 ap\u2082 _\u228e_ \u03a3\ud835\udfd9-snd Fin-\u00d7-* \u2219 Fin-\u228e-+\n\nFin-\u2192-^ : \u2200 {{_ : UA}}{{_ : FunExt}}{m n} \u2192 (Fin m \u2192 Fin n) \u2261 Fin (n \u2115.^ m)\nFin-\u2192-^ {zero} = ap\u2082 _\u27f6_ Fin0\u2261\ud835\udfd8 idp \u2219 \u03a0\ud835\udfd8-uniq\u2032 _ \u2219 \u228e\ud835\udfd8-inl \u2219 ap (_\u228e_ \ud835\udfd9) (! Fin0\u2261\ud835\udfd8)\n \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\nFin-\u2192-^ {suc m} = ap\u2082 _\u27f6_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u00d7-\u2192 \u2219 ap\u2082 _\u00d7_ (\u03a0\ud835\udfd9-uniq _) Fin-\u2192-^ \u2219 Fin-\u00d7-*\n\nFin\u2218suc\u2261Maybe\u2218Fin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 Maybe (Fin n)\nFin\u2218suc\u2261Maybe\u2218Fin = Fin\u2218suc\u2261\ud835\udfd9\u228eFin \u2219 ! Maybe\u2261\ud835\udfd9\u228e\n\nFin-injective : \u2200 {{_ : UA}}{m n} \u2192 Fin m \u2261 Fin n \u2192 m \u2261 n\nFin-injective {zero} {zero} Finm\u2261Finn = idp\nFin-injective {zero} {suc n} Finm\u2261Finn = \ud835\udfd8-elim (coe (! Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {zero} Finm\u2261Finn = \ud835\udfd8-elim (coe (Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {suc n} Finm\u2261Finn\n = ap suc (Fin-injective (Maybe-injective\n (! Fin\u2218suc\u2261Maybe\u2218Fin \u2219 Finm\u2261Finn \u2219 Fin\u2218suc\u2261Maybe\u2218Fin)))\n\nFin\u228e-injective : \u2200 {{_ : UA}}{A B : Set} n \u2192 (Fin n \u228e A) \u2261 (Fin n \u228e B) \u2192 A \u2261 B\nFin\u228e-injective zero f = \u228e\ud835\udfd8-inl \u2219 \u228e-comm \u2219 ap\u2082 _\u228e_ (! Fin0\u2261\ud835\udfd8) idp\n \u2219 f \u2219 (ap\u2082 _\u228e_ Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm) \u2219 ! \u228e\ud835\udfd8-inl\nFin\u228e-injective (suc n) f = Fin\u228e-injective n (Maybe-injective\n (Maybe\u2261\ud835\udfd9\u228e \u2219 \u228e-assoc \u2219 ap\u2082 _\u228e_ (! Fin\u2218suc\u2261\ud835\udfd9\u228eFin) idp \u2219 f\n \u2219 ap\u2082 _\u228e_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ! Maybe\u2261\ud835\udfd9\u228e))\n\nmodule _ {{_ : UA}} where\n Lift\u2261id : \u2200 {a} {A : \u2605_ a} \u2192 Lift {a} {a} A \u2261 A\n Lift\u2261id = ua (equiv lower lift (\u03bb _ \u2192 idp) (\u03bb { (lift x) \u2192 idp }))\n\n \u03a0\ud835\udfd9F\u2261F : \u2200 {\u2113} {F : \ud835\udfd9 \u2192 \u2605_ \u2113} \u2192 \u03a0 \ud835\udfd9 F \u2261 F _\n \u03a0\ud835\udfd9F\u2261F = ua (equiv (\u03bb x \u2192 x _) const (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\n \ud835\udfd9\u2192A\u2261A : \u2200 {\u2113} {A : \u2605_ \u2113} \u2192 (\ud835\udfd9 \u2192 A) \u2261 A\n \ud835\udfd9\u2192A\u2261A = \u03a0\ud835\udfd9F\u2261F\n\n not-\ud835\udfda\u2261\ud835\udfda : \ud835\udfda \u2261 \ud835\udfda\n not-\ud835\udfda\u2261\ud835\udfda = twist\n\n{-\nTODO ?\nMaybe\ud835\udfd8\u2194\ud835\udfd9 : Maybe \ud835\udfd8 \u2194 \ud835\udfd9\nMaybe\ud835\udfd8\u2194\ud835\udfd9 = A\u228e\ud835\udfd8\u2194A \u2218 Maybe\u2194\ud835\udfd9\u228e\n\nMaybe^\ud835\udfd8\u2194Fin : \u2200 n \u2192 Maybe^ n \ud835\udfd8 \u2194 Fin n\nMaybe^\ud835\udfd8\u2194Fin zero = sym Fin0\u2194\ud835\udfd8\nMaybe^\ud835\udfd8\u2194Fin (suc n) = sym Fin\u2218suc\u2194Maybe\u2218Fin \u2218 Maybe-cong (Maybe^\ud835\udfd8\u2194Fin n)\n\nMaybe^\ud835\udfd9\u2194Fin1+ : \u2200 n \u2192 Maybe^ n \ud835\udfd9 \u2194 Fin (suc n)\nMaybe^\ud835\udfd9\u2194Fin1+ n = Maybe^\ud835\udfd8\u2194Fin (suc n) \u2218 sym (Maybe\u2218Maybe^\u2194Maybe^\u2218Maybe n) \u2218 Maybe^-cong n (sym Maybe\ud835\udfd8\u2194\ud835\udfd9)\n\nMaybe-\u228e : \u2200 {a} {A B : \u2605 a} \u2192 (Maybe A \u228e B) \u2194 Maybe (A \u228e B)\nMaybe-\u228e {a} = sym Maybe\u2194Lift\ud835\udfd9\u228e \u2218 \u228e-CMon.assoc (Lift {_} {a} \ud835\udfd9) _ _ \u2218 (Maybe\u2194Lift\ud835\udfd9\u228e \u228e-cong id)\n\nMaybe^-\u228e-+ : \u2200 {A} m n \u2192 (Maybe^ m \ud835\udfd8 \u228e Maybe^ n A) \u2194 Maybe^ (m + n) A\nMaybe^-\u228e-+ zero n = \ud835\udfd8\u228eA\u2194A\nMaybe^-\u228e-+ (suc m) n = Maybe-cong (Maybe^-\u228e-+ m n) \u2218 Maybe-\u228e\n-}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Type\n\nopen import Level.NP\nopen import HoTT\nopen import Function.NP\nopen import Function.Extensionality\n\nopen import Data.Maybe.NP using (Maybe ; just ; nothing ; maybe ; maybe\u2032 ; just-injective)\nopen import Data.Zero using (\ud835\udfd8 ; \ud835\udfd8-elim)\nopen import Data.One using (\ud835\udfd9)\nopen import Data.Two using (\ud835\udfda ; 0\u2082 ; 1\u2082 ; [0:_1:_])\nopen import Data.Fin as Fin using (Fin ; suc ; zero)\nopen import Data.Nat.NP as \u2115 using (\u2115 ; suc ; zero)\nopen import Data.Product.NP renaming (proj\u2081 to fst; proj\u2082 to snd)\nopen import Data.Sum using (_\u228e_) renaming (inj\u2081 to inl; inj\u2082 to inr; [_,_] to [inl:_,inr:_])\n\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_ ; ap; coe; coe!; !_; _\u2219_; J ; inspect ; Reveal_is_ ; [_]) renaming (subst to tr; refl to idp; cong\u2082 to ap\u2082; _\u2257_ to _\u223c_)\n\nmodule Type.Identities where\n\nopen Equivalences\n\n\n-- for use with ap\u2082 etc.\n_\u27f6_ : \u2200 {a b} \u2192 \u2605_ a \u2192 \u2605_ b \u2192 \u2605_ (b \u2294 a)\nA \u27f6 B = A \u2192 B\n\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 : \ud835\udfda \u2243 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 = equiv [0: inl _ 1: inr _ ]\n [inl: const 0\u2082 ,inr: const 1\u2082 ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n [0: idp 1: idp ]\n\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 : {{_ : UA}} \u2192 \ud835\udfda \u2261 (\ud835\udfd9 \u228e \ud835\udfd9)\n\ud835\udfda\u2261\ud835\udfd9\u228e\ud835\udfd9 = ua \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9\n\nmodule _ {{_ : UA}}{A : \u2605}{B C : A \u2192 \u2605} where\n \u03a3\u228e-split : (\u03a3 A (\u03bb x \u2192 B x \u228e C x)) \u2261 (\u03a3 A B \u228e \u03a3 A C)\n \u03a3\u228e-split = ua (equiv (\u03bb { (x , inl y) \u2192 inl (x , y)\n ; (x , inr y) \u2192 inr (x , y) })\n (\u03bb { (inl (x , y)) \u2192 x , inl y\n ; (inr (x , y)) \u2192 x , inr y })\n (\u03bb { (inl (x , y)) \u2192 idp\n ; (inr (x , y)) \u2192 idp })\n (\u03bb { (x , inl y) \u2192 idp\n ; (x , inr y) \u2192 idp }))\n\nmodule _ {{_ : UA}}{A B : \u2605}{C : A \u228e B \u2192 \u2605} where\n dist-\u228e-\u03a3-equiv : \u03a3 (A \u228e B) C \u2243 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u03a3 : \u03a3 (A \u228e B) C \u2261 (\u03a3 A (C \u2218 inl) \u228e \u03a3 B (C \u2218 inr))\n dist-\u228e-\u03a3 = ua dist-\u228e-\u03a3-equiv\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0-equiv : \u03a0 (A \u228e B) C \u2243 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u03a0 : \u03a0 (A \u228e B) C \u2261 (\u03a0 A (C \u2218 inl) \u00d7 \u03a0 B (C \u2218 inr))\n dist-\u00d7-\u03a0 = ua dist-\u00d7-\u03a0-equiv\n\nmodule _ {{_ : UA}}{A B : \u2605}{C : A \u2192 \u2605}{D : B \u2192 \u2605} where\n dist-\u228e-\u03a3[] : (\u03a3 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a3 A C \u228e \u03a3 B D)\n dist-\u228e-\u03a3[] = dist-\u228e-\u03a3\n\n module _{{_ : FunExt}} where\n dist-\u00d7-\u03a0[] : (\u03a0 (A \u228e B) [inl: C ,inr: D ]) \u2261 (\u03a0 A C \u00d7 \u03a0 B D)\n dist-\u00d7-\u03a0[] = dist-\u00d7-\u03a0\n\nmodule _ {{_ : UA}}{A B C : \u2605} where\n dist-\u228e-\u00d7-equiv : ((A \u228e B) \u00d7 C) \u2243 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7-equiv = equiv (\u03bb { (inl x , y) \u2192 inl (x , y)\n ; (inr x , y) \u2192 inr (x , y) })\n [inl: (\u03bb x \u2192 inl (fst x) , snd x)\n ,inr: (\u03bb x \u2192 inr (fst x) , snd x) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (\u03bb { (inl x , y) \u2192 idp\n ; (inr x , y) \u2192 idp })\n\n dist-\u228e-\u00d7 : ((A \u228e B) \u00d7 C) \u2261 (A \u00d7 C \u228e B \u00d7 C)\n dist-\u228e-\u00d7 = ua dist-\u228e-\u00d7-equiv\n\n module _ {{_ : FunExt}} where\n dist-\u00d7-\u2192-equiv : ((A \u228e B) \u2192 C ) \u2243 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192-equiv = equiv (\u03bb f \u2192 f \u2218 inl , f \u2218 inr) (\u03bb fg \u2192 [inl: fst fg ,inr: snd fg ])\n (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ])\n\n dist-\u00d7-\u2192 : ((A \u228e B) \u2192 C) \u2261 ((A \u2192 C) \u00d7 (B \u2192 C))\n dist-\u00d7-\u2192 = ua dist-\u00d7-\u2192-equiv\n\nmodule _ {A : \u2605}{B : A \u2192 \u2605}{C : (x : A) \u2192 B x \u2192 \u2605} where\n \u03a3-assoc-equiv : (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2243 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc-equiv = equiv (\u03bb x \u2192 (fst x , fst (snd x)) , snd (snd x))\n (\u03bb x \u2192 fst (fst x) , snd (fst x) , snd x)\n (\u03bb y \u2192 idp)\n (\u03bb y \u2192 idp)\n\n \u03a3-assoc : {{_ : UA}} \u2192 (\u03a3 A (\u03bb x \u2192 \u03a3 (B x) (C x))) \u2261 (\u03a3 (\u03a3 A B) (uncurry C))\n \u03a3-assoc = ua \u03a3-assoc-equiv\n\nmodule _ {A B : \u2605} where\n \u00d7-comm-equiv : (A \u00d7 B) \u2243 (B \u00d7 A)\n \u00d7-comm-equiv = equiv swap swap (\u03bb y \u2192 idp) (\u03bb x \u2192 idp)\n\n \u00d7-comm : {{_ : UA}} \u2192 (A \u00d7 B) \u2261 (B \u00d7 A)\n \u00d7-comm = ua \u00d7-comm-equiv\n\n \u228e-comm-equiv : (A \u228e B) \u2243 (B \u228e A)\n \u228e-comm-equiv = equiv [inl: inr ,inr: inl ]\n [inl: inr ,inr: inl ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n\n \u228e-comm : {{_ : UA}} \u2192 (A \u228e B) \u2261 (B \u228e A)\n \u228e-comm = ua \u228e-comm-equiv\n\nmodule _ {A B : \u2605}{C : A \u2192 B \u2192 \u2605} where\n \u03a0\u03a0-comm-equiv : ((x : A)(y : B) \u2192 C x y) \u2243 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm-equiv = equiv flip flip (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a0\u03a0-comm : {{_ : UA}} \u2192 ((x : A)(y : B) \u2192 C x y) \u2261 ((y : B)(x : A) \u2192 C x y)\n \u03a0\u03a0-comm = ua \u03a0\u03a0-comm-equiv\n\n \u03a3\u03a3-comm-equiv : (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2243 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm-equiv = equiv (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb { (x , y , z) \u2192 y , x , z })\n (\u03bb _ \u2192 idp)\n (\u03bb _ \u2192 idp)\n\n \u03a3\u03a3-comm : {{_ : UA}} \u2192 (\u03a3 A \u03bb x \u2192 \u03a3 B \u03bb y \u2192 C x y) \u2261 (\u03a3 B \u03bb y \u2192 \u03a3 A \u03bb x \u2192 C x y)\n \u03a3\u03a3-comm = ua \u03a3\u03a3-comm-equiv\n\nmodule _ {A B C : \u2605} where\n \u00d7-assoc : {{_ : UA}} \u2192 (A \u00d7 (B \u00d7 C)) \u2261 ((A \u00d7 B) \u00d7 C)\n \u00d7-assoc = \u03a3-assoc\n\n \u228e-assoc-equiv : (A \u228e (B \u228e C)) \u2243 ((A \u228e B) \u228e C)\n \u228e-assoc-equiv = equiv [inl: inl \u2218 inl ,inr: [inl: inl \u2218 inr ,inr: inr ] ]\n [inl: [inl: inl ,inr: inr \u2218 inl ] ,inr: inr \u2218 inr ]\n [inl: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ,inr: (\u03bb x \u2192 idp) ]\n [inl: (\u03bb x \u2192 idp) ,inr: [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ] ]\n\n \u228e-assoc : {{_ : UA}} \u2192 (A \u228e (B \u228e C)) \u2261 ((A \u228e B) \u228e C)\n \u228e-assoc = ua \u228e-assoc-equiv\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \ud835\udfd8 \u2192 \u2605) where\n \u03a0\ud835\udfd8-uniq : \u03a0 \ud835\udfd8 A \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq = ua (equiv _ (\u03bb _ ()) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 \u03bb= (\u03bb())))\n\nmodule _ {{_ : UA}}(A : \ud835\udfd9 \u2192 \u2605) where\n \u03a0\ud835\udfd9-uniq : \u03a0 \ud835\udfd9 A \u2261 A _\n \u03a0\ud835\udfd9-uniq = ua (equiv (\u03bb f \u2192 f _) (\u03bb x _ \u2192 x) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp))\n\nmodule _ {{_ : UA}}(A : \u2605) where\n \u03a0\ud835\udfd9-uniq\u2032 : (\ud835\udfd9 \u2192 A) \u2261 A\n \u03a0\ud835\udfd9-uniq\u2032 = \u03a0\ud835\udfd9-uniq (\u03bb _ \u2192 A)\n\nmodule _ {{_ : UA}}{{_ : FunExt}}(A : \u2605) where\n \u03a0\ud835\udfd8-uniq\u2032 : (\ud835\udfd8 \u2192 A) \u2261 \ud835\udfd9\n \u03a0\ud835\udfd8-uniq\u2032 = \u03a0\ud835\udfd8-uniq (\u03bb _ \u2192 A)\n\nmodule _ {{_ : FunExt}}(F G : \ud835\udfd8 \u2192 \u2605) where\n -- also by \u03a0\ud835\udfd8-uniq twice\n \u03a0\ud835\udfd8-uniq' : \u03a0 \ud835\udfd8 F \u2261 \u03a0 \ud835\udfd8 G\n \u03a0\ud835\udfd8-uniq' = \u03a0=\u2032 \ud835\udfd8 (\u03bb())\n\nmodule _ {A : \ud835\udfd8 \u2192 \u2605} where\n \u03a3\ud835\udfd8-fst-equiv : \u03a3 \ud835\udfd8 A \u2243 \ud835\udfd8\n \u03a3\ud835\udfd8-fst-equiv = equiv fst (\u03bb()) (\u03bb()) (\u03bb { (() , _) })\n\n \u03a3\ud835\udfd8-fst : {{_ : UA}} \u2192 \u03a3 \ud835\udfd8 A \u2261 \ud835\udfd8\n \u03a3\ud835\udfd8-fst = ua \u03a3\ud835\udfd8-fst-equiv\n\nmodule _ {A : \ud835\udfd9 \u2192 \u2605} where\n \u03a3\ud835\udfd9-snd-equiv : \u03a3 \ud835\udfd9 A \u2243 A _\n \u03a3\ud835\udfd9-snd-equiv = equiv snd (_,_ _) (\u03bb _ \u2192 idp) (\u03bb _ \u2192 idp)\n\n \u03a3\ud835\udfd9-snd : {{_ : UA}} \u2192 \u03a3 \ud835\udfd9 A \u2261 A _\n \u03a3\ud835\udfd9-snd = ua \u03a3\ud835\udfd9-snd-equiv\n\nmodule _ {A : \u2605} where\n \u228e\ud835\udfd8-inl-equiv : A \u2243 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl-equiv = equiv inl [inl: id ,inr: (\u03bb()) ] [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb()) ] (\u03bb _ \u2192 idp)\n\n \u228e\ud835\udfd8-inl : {{_ : UA}} \u2192 A \u2261 (A \u228e \ud835\udfd8)\n \u228e\ud835\udfd8-inl = ua \u228e\ud835\udfd8-inl-equiv\n\n \ud835\udfd9\u00d7-snd : {{_ : UA}} \u2192 (\ud835\udfd9 \u00d7 A) \u2261 A\n \ud835\udfd9\u00d7-snd = \u03a3\ud835\udfd9-snd\n\n \ud835\udfd9\u00d7-fst : {{_ : UA}} \u2192 (A \u00d7 \ud835\udfd9) \u2261 A\n \ud835\udfd9\u00d7-fst = \u00d7-comm \u2219 \ud835\udfd9\u00d7-snd\n\nmodule _ {A : \u2605}{B : A \u2192 \u2605}{C : \u03a3 A B \u2192 \u2605} where\n -- AC: Dependent axiom of choice\n -- In Type Theory it happens to be neither an axiom nor to be choosing anything.\n \u03a0\u03a3-comm-equiv : (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2243 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm-equiv = equiv (\u03bb H \u2192 fst \u2218 H , snd \u2218 H) (\u03bb H \u2192 < fst H , snd H >) (\u03bb H \u2192 idp) (\u03bb H \u2192 idp)\n\n \u03a0\u03a3-comm : {{_ : UA}}\n \u2192 (\u2200 (x : A) \u2192 \u2203 \u03bb (y : B x) \u2192 C (x , y)) \u2261 (\u2203 \u03bb (f : \u03a0 A B) \u2192 \u2200 (x : A) \u2192 C (x , f x))\n \u03a0\u03a3-comm = ua \u03a0\u03a3-comm-equiv\n\nmodule _ {A : \ud835\udfda \u2192 \u2605} where\n \u03a3\ud835\udfda-\u228e : {{_ : UA}}{{_ : FunExt}} \u2192 \u03a3 \ud835\udfda A \u2261 (A 0\u2082 \u228e A 1\u2082)\n \u03a3\ud835\udfda-\u228e = \u03a3-first \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 \u2219 dist-\u228e-\u03a3 \u2219 ap\u2082 _\u228e_ \u03a3\ud835\udfd9-snd \u03a3\ud835\udfd9-snd\n\n \u03a0\ud835\udfda-\u00d7 : {{_ : UA}}{{_ : FunExt}} \u2192 \u03a0 \ud835\udfda A \u2261 (A 0\u2082 \u00d7 A 1\u2082)\n \u03a0\ud835\udfda-\u00d7 = \u03a0-first \ud835\udfda\u2243\ud835\udfd9\u228e\ud835\udfd9 \u2219 dist-\u00d7-\u03a0 \u2219 ap\u2082 _\u00d7_ (\u03a0\ud835\udfd9-uniq _) (\u03a0\ud835\udfd9-uniq _)\n\nmodule _ where\n\n private\n maybe-elim : {X : Set}(m : Maybe X) \u2192 m \u2262 nothing \u2192 X\n maybe-elim {X} m = maybe {B = \u03bb m' \u2192 m' \u2262 _ \u2192 _} (\u03bb x _ \u2192 x) (\u03bb e \u2192 \ud835\udfd8-elim (e idp)) m\n\n maybe-elim-just : {X : Set}(m : Maybe X)(p : m \u2262 nothing)\n \u2192 just (maybe-elim m p) \u2261 m\n maybe-elim-just (just x) p = idp\n maybe-elim-just nothing p = \ud835\udfd8-elim (p idp)\n\n maybe-elim-cong : \u2200 {X : Set}{m m' : Maybe X}{p p'} \u2192 m \u2261 m'\n \u2192 maybe-elim m p \u2261 maybe-elim m' p'\n maybe-elim-cong {m = just x} idp = idp\n maybe-elim-cong {m = nothing} {p = p} idp = \ud835\udfd8-elim (p idp)\n\n not-just : {X : Set}{x : X} \u2192 _\u2262_ {A = Maybe X} (just x) nothing\n not-just ()\n\n !!-p : \u2200 {x}{X : Set x}{x y : X}(p : x \u2261 y) \u2192 ! (! p) \u2261 p\n !!-p idp = idp\n\n !\u2219 : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 ! p \u2219 p \u2261 idp\n !\u2219 idp = idp\n\n \u2219! : \u2200 {\u2113}{A : Set \u2113}{x y : A}(p : x \u2261 y) \u2192 p \u2219 ! p \u2261 idp\n \u2219! idp = idp\n\n\n record PreservePoint {A B : Set}(eq : Maybe A \u2261 Maybe B) : Set where\n field\n coe-p : \u2200 x \u2192 coe eq (just x) \u2262 nothing\n coe!-p : \u2200 x \u2192 coe! eq (just x) \u2262 nothing\n open PreservePoint\n\n nothing-to-nothing : {A B : Set}(eq : Maybe A \u2261 Maybe B)\n \u2192 coe eq nothing \u2261 nothing \u2192 PreservePoint eq\n nothing-to-nothing eq n2n = record { coe-p = \u03bb x p \u2192 not-just (coe-inj eq (p \u2219 ! n2n))\n ; coe!-p = \u03bb x p \u2192 not-just (coe-same (! !\u2219 eq) (just x)\n \u2219 ! (coe\u2218coe eq (! eq) (just x)) \u2219 ap (coe eq) p \u2219 n2n) }\n\n !-PP : \u2200 {A B : Set} {e : Maybe A \u2261 Maybe B} \u2192 PreservePoint e \u2192 PreservePoint (! e)\n !-PP {e = e} e-pp = record { coe-p = coe!-p e-pp\n ; coe!-p = \u03bb x p \u2192 coe-p e-pp x ( ap (\u03bb X \u2192 coe X (just x)) (! !!-p e ) \u2219 p) }\n\n to : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e \u2192 A \u2192 B\n to e e-pp x = maybe-elim (coe e (just x)) (coe-p e-pp x)\n\n module _{A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}{ep ep'}(e'\u2219e=id : e' \u2219 e \u2261 idp)(x : B) where\n to-to : to e ep (to e' ep' x) \u2261 x\n to-to = just-injective (ap just (maybe-elim-cong\n {m = coe e (just (maybe-elim (coe e' (just x)) _))}\n {m' = coe e (coe e' (just x))}\n {p' = \u03bb q \u2192 not-just (! p \u2219 q)} (ap (coe e) (maybe-elim-just _ _)))\n \u2219 maybe-elim-just (coe e (coe e' (just x))) (\u03bb q \u2192 not-just (! p \u2219 q))\n \u2219 p)\n where\n p : coe e (coe e' (just x)) \u2261 just x\n p = coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n\n cto : \u2200 {A B : Set}(e : Maybe A \u2261 Maybe B) \u2192 Maybe A \u2192 Maybe B\n cto e = maybe (maybe\u2032 just (coe e nothing) \u2218 coe e \u2218 just) nothing\n\n module _ {A B : Set}{e : Maybe A \u2261 Maybe B}{e' : Maybe B \u2261 Maybe A}(e'\u2219e=id : e' \u2219 e \u2261 idp) where\n cto-cto : (x : Maybe B) \u2192 cto e (cto e' x) \u2261 x\n cto-cto nothing = idp\n cto-cto (just x) with coe e' (just x) | coe\u2218coe e e' (just x) \u2219 coe-same e'\u2219e=id (just x)\n cto-cto (just x) | just x\u2081 | p = ap (maybe just (coe e nothing)) p\n cto-cto (just x) | nothing | p with coe e' nothing\n | coe\u2218coe e e' nothing \u2219 coe-same e'\u2219e=id nothing\n cto-cto (just x) | nothing | p | just y | q = ap (maybe just (coe e nothing)) q \u2219 p\n cto-cto (just x) | nothing | p | nothing | q = ! q \u2219 p\n\n module _ {A B : Set} where\n create-PP-\u2243 : Maybe A \u2261 Maybe B \u2192 Maybe A \u2243 Maybe B\n create-PP-\u2243 e = equiv (cto e)\n (cto (! e))\n (cto-cto {e = e} {e' = ! e} (!\u2219 e))\n (cto-cto {e = ! e}{e' = e} (\u2219! e))\n\n create-PP : {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 \u03a3 (Maybe A \u2261 Maybe B) PreservePoint\n create-PP eq = ua (create-PP-\u2243 eq) , nothing-to-nothing _ (coe-\u03b2 (create-PP-\u2243 eq) nothing)\n\n Maybe-injective-PP : {{_ : UA}} \u2192 (e : Maybe A \u2261 Maybe B) \u2192 PreservePoint e\n \u2192 A \u2261 B\n Maybe-injective-PP e e-pp = ua\n (equiv (to e e-pp)\n (to (! e) (!-PP e-pp))\n (to-to {e = e} { ! e} {e-pp}{ !-PP e-pp} (!\u2219 e))\n (to-to {e = ! e} {e} { !-PP e-pp} {e-pp} (\u2219! e)))\n\n Maybe-injective : \u2200 {{_ : UA}} \u2192 Maybe A \u2261 Maybe B \u2192 A \u2261 B\n Maybe-injective e = let (e' , e'-pp) = create-PP e in Maybe-injective-PP e' e'-pp\n\n\nmodule _ {a}{A : \u2605_ a} where\n Maybe\u2243\ud835\udfd9\u228e : Maybe A \u2243 (\ud835\udfd9 \u228e A)\n Maybe\u2243\ud835\udfd9\u228e = equiv (maybe inr (inl _)) [inl: const nothing ,inr: just ]\n [inl: (\u03bb x \u2192 idp) ,inr: (\u03bb x \u2192 idp) ]\n (maybe (\u03bb x \u2192 idp) idp)\n\n Maybe\u2261\ud835\udfd9\u228e : \u2200 {{_ : UA}}\u2192 Maybe A \u2261 (\ud835\udfd9 \u228e A)\n Maybe\u2261\ud835\udfd9\u228e = ua Maybe\u2243\ud835\udfd9\u228e\n\nFin0\u2261\ud835\udfd8 : {{_ : UA}} \u2192 Fin 0 \u2261 \ud835\udfd8\nFin0\u2261\ud835\udfd8 = ua (equiv (\u03bb ()) (\u03bb ()) (\u03bb ()) (\u03bb ()))\n\nmodule _ where\n isZero? : \u2200 {n}{A : Fin (suc n) \u2192 Set} \u2192 ((i : Fin n) \u2192 A (suc i)) \u2192 A zero\n \u2192 (i : Fin (suc n)) \u2192 A i\n isZero? f x zero = x\n isZero? f x (suc i) = f i\n\n Fin\u2218suc\u2243\ud835\udfd9\u228eFin : \u2200 {n} \u2192 Fin (suc n) \u2243 (\ud835\udfd9 \u228e Fin n)\n Fin\u2218suc\u2243\ud835\udfd9\u228eFin = equiv (isZero? inr (inl _)) [inl: (\u03bb _ \u2192 zero) ,inr: suc ]\n [inl: (\u03bb _ \u2192 idp) ,inr: (\u03bb _ \u2192 idp) ]\n (isZero? (\u03bb _ \u2192 idp) idp)\n\n Fin\u2218suc\u2261\ud835\udfd9\u228eFin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 (\ud835\udfd9 \u228e Fin n)\n Fin\u2218suc\u2261\ud835\udfd9\u228eFin = ua Fin\u2218suc\u2243\ud835\udfd9\u228eFin\n\nFin-\u228e-+ : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u228e Fin n) \u2261 Fin (m \u2115.+ n)\nFin-\u228e-+ {zero} = ap\u2082 _\u228e_ Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm \u2219 ! \u228e\ud835\udfd8-inl\nFin-\u228e-+ {suc m} = ap\u2082 _\u228e_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ap (_\u228e_ \ud835\udfd9) Fin-\u228e-+ \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\n\nFin-\u00d7-* : \u2200 {{_ : UA}} {m n} \u2192 (Fin m \u00d7 Fin n) \u2261 Fin (m \u2115.* n)\nFin-\u00d7-* {zero} = ap\u2082 _\u00d7_ Fin0\u2261\ud835\udfd8 idp \u2219 \u03a3\ud835\udfd8-fst \u2219 ! Fin0\u2261\ud835\udfd8\nFin-\u00d7-* {suc m} = ap\u2082 _\u00d7_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u228e-\u00d7 \u2219 ap\u2082 _\u228e_ \u03a3\ud835\udfd9-snd Fin-\u00d7-* \u2219 Fin-\u228e-+\n\nFin-\u2192-^ : \u2200 {{_ : UA}}{{_ : FunExt}}{m n} \u2192 (Fin m \u2192 Fin n) \u2261 Fin (n \u2115.^ m)\nFin-\u2192-^ {zero} = ap\u2082 _\u27f6_ Fin0\u2261\ud835\udfd8 idp \u2219 \u03a0\ud835\udfd8-uniq\u2032 _ \u2219 \u228e\ud835\udfd8-inl \u2219 ap (_\u228e_ \ud835\udfd9) (! Fin0\u2261\ud835\udfd8)\n \u2219 ! Fin\u2218suc\u2261\ud835\udfd9\u228eFin\nFin-\u2192-^ {suc m} = ap\u2082 _\u27f6_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 dist-\u00d7-\u2192 \u2219 ap\u2082 _\u00d7_ (\u03a0\ud835\udfd9-uniq _) Fin-\u2192-^ \u2219 Fin-\u00d7-*\n\nFin\u2218suc\u2261Maybe\u2218Fin : \u2200 {{_ : UA}}{n} \u2192 Fin (suc n) \u2261 Maybe (Fin n)\nFin\u2218suc\u2261Maybe\u2218Fin = Fin\u2218suc\u2261\ud835\udfd9\u228eFin \u2219 ! Maybe\u2261\ud835\udfd9\u228e\n\nFin-injective : \u2200 {{_ : UA}}{m n} \u2192 Fin m \u2261 Fin n \u2192 m \u2261 n\nFin-injective {zero} {zero} Finm\u2261Finn = idp\nFin-injective {zero} {suc n} Finm\u2261Finn = \ud835\udfd8-elim (coe (! Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {zero} Finm\u2261Finn = \ud835\udfd8-elim (coe (Finm\u2261Finn \u2219 Fin0\u2261\ud835\udfd8) zero)\nFin-injective {suc m} {suc n} Finm\u2261Finn\n = ap suc (Fin-injective (Maybe-injective\n (! Fin\u2218suc\u2261Maybe\u2218Fin \u2219 Finm\u2261Finn \u2219 Fin\u2218suc\u2261Maybe\u2218Fin)))\n\nFin\u228e-injective : \u2200 {{_ : UA}}{A B : Set} n \u2192 (Fin n \u228e A) \u2261 (Fin n \u228e B) \u2192 A \u2261 B\nFin\u228e-injective zero f = \u228e\ud835\udfd8-inl \u2219 \u228e-comm \u2219 ap\u2082 _\u228e_ (! Fin0\u2261\ud835\udfd8) idp\n \u2219 f \u2219 (ap\u2082 _\u228e_ Fin0\u2261\ud835\udfd8 idp \u2219 \u228e-comm) \u2219 ! \u228e\ud835\udfd8-inl\nFin\u228e-injective (suc n) f = Fin\u228e-injective n (Maybe-injective\n (Maybe\u2261\ud835\udfd9\u228e \u2219 \u228e-assoc \u2219 ap\u2082 _\u228e_ (! Fin\u2218suc\u2261\ud835\udfd9\u228eFin) idp \u2219 f\n \u2219 ap\u2082 _\u228e_ Fin\u2218suc\u2261\ud835\udfd9\u228eFin idp \u2219 ! \u228e-assoc \u2219 ! Maybe\u2261\ud835\udfd9\u228e))\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"6371c2dd29365338ea26008a34552df2e4ee1d90","subject":"Permutations and search","message":"Permutations and search\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative.NP\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties.NP\nimport Data.List.NP as L\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0b \u2237 1b \u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u2219_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u2219 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u2219 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u2219-comm : \u2200 {m} (x y : A m) \u2192 x \u2219 y \u2261 y \u2219 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u2219-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nmodule OperationSyntax where\n infixr 1 _`\u204f_\n data Op : Set where\n `id `0\u21941 `not : Op\n `tl : Op \u2192 Op\n _`\u204f_ : Op \u2192 Op \u2192 Op\n\n infixr 9 _\u2219_\n _\u2219_ : Op \u2192 \u2200 {n} \u2192 Endo (Bits n)\n `id \u2219 xs = xs\n `0\u21941 \u2219 xs = 0\u21941 xs\n `not \u2219 [] = []\n `not \u2219 (x \u2237 xs) = not x \u2237 xs\n `tl f \u2219 [] = []\n `tl f \u2219 (x \u2237 xs) = x \u2237 f \u2219 xs\n (f `\u204f g) \u2219 xs = g \u2219 f \u2219 xs\n\n open PermutationSyntax using (Perm; `id; `0\u21941; `tl; _`\u204f_)\n module P = PermutationSemantics\n\n toPerm : Op \u2192 Perm\n toPerm `id = `id\n toPerm `0\u21941 = `0\u21941\n toPerm `not = `id -- Important\n toPerm (`tl f) = `tl (toPerm f)\n toPerm (f `\u204f g) = toPerm f `\u204f toPerm g\n\n infixr 9 _\u2219\u2032_\n _\u2219\u2032_ : Op \u2192 \u2200 {n} \u2192 Endo (Bits n)\n f \u2219\u2032 xs = toPerm f P.\u2219 xs\n\n `\u27e80\u21941+_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Op\n `\u27e80\u21941+ zero \u27e9 = `0\u21941\n `\u27e80\u21941+ suc i \u27e9 = `0\u21941 `\u204f `tl `\u27e80\u21941+ i \u27e9 `\u204f `0\u21941\n\n `\u27e80\u21941+_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u21941+ i \u27e9 \u2219 xs \u2261 \u27e80\u21941+ i \u27e9 xs\n `\u27e80\u21941+ zero \u27e9-spec xs = refl\n `\u27e80\u21941+ suc i \u27e9-spec (x \u2237 _ \u2237 xs) rewrite `\u27e80\u21941+ i \u27e9-spec (x \u2237 xs) = refl\n\n `\u27e80\u2194_\u27e9 : \u2200 {n} (i : Fin n) \u2192 Op\n `\u27e80\u2194 zero \u27e9 = `id\n `\u27e80\u2194 suc i \u27e9 = `\u27e80\u21941+ i \u27e9\n\n `\u27e80\u2194_\u27e9-spec : \u2200 {n} (i : Fin n) xs \u2192 `\u27e80\u2194 i \u27e9 \u2219 xs \u2261 \u27e80\u2194 i \u27e9 xs\n `\u27e80\u2194 zero \u27e9-spec xs = refl\n `\u27e80\u2194 suc i \u27e9-spec xs = `\u27e80\u21941+ i \u27e9-spec xs\n\n `\u27e8_\u2194_\u27e9 : \u2200 {n} (i j : Fin n) \u2192 Op\n `\u27e8 zero \u2194 j \u27e9 = `\u27e80\u2194 j \u27e9\n `\u27e8 i \u2194 zero \u27e9 = `\u27e80\u2194 i \u27e9\n `\u27e8 suc i \u2194 suc j \u27e9 = `tl `\u27e8 i \u2194 j \u27e9\n\n `\u27e8_\u2194_\u27e9-spec : \u2200 {n} (i j : Fin n) xs \u2192 `\u27e8 i \u2194 j \u27e9 \u2219 xs \u2261 \u27e8 i \u2194 j \u27e9 xs\n `\u27e8_\u2194_\u27e9-spec zero j xs = `\u27e80\u2194 j \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) zero xs = `\u27e80\u21941+ i \u27e9-spec xs\n `\u27e8_\u2194_\u27e9-spec (suc i) (suc j) (x \u2237 xs) rewrite `\u27e8 i \u2194 j \u27e9-spec xs = refl\n\n `xor-head : Bit \u2192 Op\n `xor-head b = if b then `not else `id\n\n `xor-head-spec : \u2200 b {n} x (xs : Bits n) \u2192 `xor-head b \u2219 (x \u2237 xs) \u2261 (b xor x) \u2237 xs\n `xor-head-spec true x xs = refl\n `xor-head-spec false x xs = refl\n\n `\u27e8_\u2295\u27e9 : \u2200 {n} \u2192 Bits n \u2192 Op\n `\u27e8 [] \u2295\u27e9 = `id\n `\u27e8 b \u2237 xs \u2295\u27e9 = `xor-head b `\u204f `tl `\u27e8 xs \u2295\u27e9\n\n `\u27e8_\u2295\u27e9-spec : \u2200 {n} (xs ys : Bits n) \u2192 `\u27e8 xs \u2295\u27e9 \u2219 ys \u2261 xs \u2295 ys\n `\u27e8 [] \u2295\u27e9-spec [] = refl\n `\u27e8 true \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n `\u27e8 false \u2237 xs \u2295\u27e9-spec (y \u2237 ys) rewrite `\u27e8 xs \u2295\u27e9-spec ys = refl\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) x \u2192 0\u21941 pad \u2295 0\u21941 x \u2261 0\u21941 (pad \u2295 x)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\n almost-\u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) f xs \u2192 f \u2219\u2032 pad \u2295 f \u2219 xs \u2261 f \u2219 (pad \u2295 xs)\n almost-\u2295-dist-\u2219 pad `id xs = refl\n almost-\u2295-dist-\u2219 pad `0\u21941 xs = \u2295-dist-0\u21941 pad xs\n almost-\u2295-dist-\u2219 [] `not [] = refl\n almost-\u2295-dist-\u2219 (true \u2237 pad) `not (x \u2237 xs) = refl\n almost-\u2295-dist-\u2219 (false \u2237 pad) `not (x \u2237 xs) = refl\n almost-\u2295-dist-\u2219 [] (`tl f) [] = refl\n almost-\u2295-dist-\u2219 (p \u2237 pad) (`tl f) (x \u2237 xs) rewrite almost-\u2295-dist-\u2219 pad f xs = refl\n almost-\u2295-dist-\u2219 pad (f `\u204f g) xs rewrite almost-\u2295-dist-\u2219 (f \u2219\u2032 pad) g (f \u2219 xs)\n | almost-\u2295-dist-\u2219 pad f xs = refl\n\nmodule PermutationSyntax-Props where\n open PermutationSyntax\n open PermutationSemantics\n open PermutationProperties\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) xs \u2192 0\u21941 pad \u2295 0\u21941 xs \u2261 0\u21941 (pad \u2295 xs)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\n -- \u229b-dist-\u2219 : \u2200 {n a} {A : Set a} (fs : Vec (A \u2192 A) n) \u03c0 xs \u2192 \u03c0 \u2219 fs \u229b \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (fs \u229b xs)\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 pad \u03c0 xs = \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs\n \u2261\u27e8 refl \u27e9\n vmap _xor_ (\u03c0 \u2219 pad) \u229b \u03c0 \u2219 xs\n \u2261\u27e8 {!!} \u27e9\n \u03c0 \u2219 vmap _xor_ pad \u229b \u03c0 \u2219 xs\n \u2261\u27e8 \u229b-dist-\u2219 _ (vmap _xor_ pad) \u03c0 xs \u27e9\n \u03c0 \u2219 (vmap _xor_ pad \u229b xs)\n \u2261\u27e8 refl \u27e9\n \u03c0 \u2219 (pad \u2295 xs)\n \u220e where open \u2261-Reasoning\n -- rans {!\u229b-dist-\u2219 (vmap _xor_ (op \u2219 pad)) op xs!} (\u229b-dist-\u2219 (vmap _xor_ pad) op xs)\n\nmodule SimpleSearch {a} {A : Set a} (_\u2219_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u2219_ public\n\n search-const\u03b5\u2261\u03b5 : \u2200 \u03b5 (\u03b5\u2219\u03b5 : \u03b5 \u2219 \u03b5 \u2261 \u03b5) n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-const\u03b5\u2261\u03b5 \u03b5 \u03b5\u2219\u03b5 = go\n where\n go : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n go zero = refl\n go (suc n) rewrite go n = \u03b5\u2219\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n module SearchInterchange (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u2219 f\u2081 x) \u2261 search f\u2080 \u2219 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u2219-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e\n where open \u2261-Reasoning\n\n search-0\u21941 : \u2200 {n} (f : Bits n \u2192 A) \u2192 search {n} (f \u2218 0\u21941) \u2261 search {n} f\n search-0\u21941 {zero} _ = refl\n search-0\u21941 {suc zero} _ = refl\n search-0\u21941 {suc (suc n)} _ = \u2219-interchange _ _ _ _\n\n module Op (\u2219-comm : Commutative _\u2261_ _\u2219_)\n (\u2219-interchange : Interchange _\u2261_ _\u2219_ _\u2219_) where\n open SearchInterchange \u2219-interchange using (search-0\u21941)\n open OperationSyntax renaming (_\u2219_ to op)\n search-op : \u2200 {n} (f : Bits n \u2192 A) (g : Op) \u2192 search {n} (f \u2218 op g) \u2261 search {n} f\n search-op f `id = refl\n search-op f `0\u21941 = search-0\u21941 f\n search-op {zero} f `not = refl\n search-op {suc n} f `not = \u2219-comm _ _\n search-op {zero} f (`tl g) = refl\n search-op {suc n} f (`tl g) rewrite search-op (f \u2218 0\u2237_) g | search-op (f \u2218 1\u2237_) g = refl\n search-op f (g `\u204f h) rewrite search-op (f \u2218 op h) g = search-op f h\n\nmodule Sum where\n open SimpleSearch _+_ using (module Comm; module SearchInterchange; search-const\u03b5\u2261\u03b5; module Op)\n open SimpleSearch _+_ public using () renaming (search to sum; search-\u2257 to sum-\u2257; searchBit to sumBit;\n search-\u2257\u2082 to sum-\u2257\u2082)\n open Comm \u2115\u00b0.+-comm public renaming (search-comm to sum-comm)\n open SearchInterchange +-interchange public renaming (\n search-dist to sum-dist;\n search-searchBit to sum-sumBit;\n search-+ to sum-+;\n search-search to sum-sum;\n search-swap to sum-swap)\n open Op \u2115\u00b0.+-comm +-interchange public renaming (search-op to sum-op)\n\n sum-const0\u22610 : \u2200 n \u2192 sum {n = n} (const 0) \u2261 0\n sum-const0\u22610 n = search-const\u03b5\u2261\u03b5 0 refl n\n\n sum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\n sum-const zero _ = refl\n sum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nmodule Count where\n open Sum\n open OperationSyntax renaming (_\u2219_ to op)\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n #\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n -- #-ext\n #-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n #-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n #-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n #-op : \u2200 {n} (f : Bits n \u2192 Bit) (g : Op) \u2192 #\u27e8 f \u2218 op g \u27e9 \u2261 #\u27e8 f \u27e9\n #-op f = sum-op (Bool.to\u2115 \u2218 f)\n\n #-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n #-\u2295 = #-comm\n\n #-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n #-const n b = sum-const n (Bool.to\u2115 b)\n\n #never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n #never\u22610 = sum-const0\u22610\n\n #always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n #always\u22612^ n = sum-const n 1\n\n #-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 Bit) \u2192 sum (\u03bb x \u2192 Bool.to\u2115 (f\u2080 x) + Bool.to\u2115 (f\u2081 x)) \u2261 #\u27e8 f\u2080 \u27e9 + #\u27e8 f\u2081 \u27e9\n #-dist f\u2080 f\u2081 = sum-dist (Bool.to\u2115 \u2218 f\u2080) (Bool.to\u2115 \u2218 f\u2081)\n\n #-+ : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n #\u27e8 f \u27e9 \u2261 sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9 )\n #-+ {m} {n} f = sum-+ {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-# : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9)\n \u2261 sum {n} (\u03bb ys \u2192 #\u27e8 (\u03bb (xs : Bits m) \u2192 f (xs ++ ys)) \u27e9)\n #-# {m} {n} f = sum-sum {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-swap : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192 #\u27e8 f \u2218 vswap n {m} \u27e9 \u2261 #\u27e8 f \u27e9\n #-swap {m} {n} f = sum-swap {m} {n} (Bool.to\u2115 \u2218 f)\n\n #\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n #\u27e8== [] \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n \u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n \u2257-cong-# f g f\u2257g = sum-\u2257 _ _ (cong Bool.to\u2115 \u2218 f\u2257g)\n\n -- #-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n -- #-+ f f0 f1 rewrite f0 | f1 = refl\n\n #-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-take-drop zero n f g with f []\n ... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n ... | false = #never\u22610 n\n #-take-drop (suc m) n f g\n rewrite \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x))\n | #-take-drop m n (f \u2218 0\u2237_) g\n | \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x))\n | #-take-drop m n (f \u2218 1\u2237_) g\n = sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9)\n\n #-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n #\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n #\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n #\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n ... | true | true | _ = s\u2264s z\u2264n\n ... | true | false | p = \u22a5-elim (p _)\n ... | false | _ | _ = z\u2264n\n #\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n #-\u2227-\u2228\u1d47 : \u2200 x y \u2192 Bool.to\u2115 (x \u2227 y) + Bool.to\u2115 (x \u2228 y) \u2261 Bool.to\u2115 x + Bool.to\u2115 y\n #-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (Bool.to\u2115 y) 1 = refl\n #-\u2227-\u2228\u1d47 false y = refl\n\n #-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n #-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #\u2228' {zero} f g with f []\n ... | true = s\u2264s z\u2264n\n ... | false = \u2115\u2264.refl\n #\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n #\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n #\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n #\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n #-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n #-bound zero f = Bool.to\u2115\u22641 (f [])\n #-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n #-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n #-\u2218vnot _ f = #-\u2295 1\u207f f\n\n #-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n #-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n #-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n #-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n #-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n #-not\u2218 zero f with f []\n ... | true = \u2261.refl\n ... | false = \u2261.refl\n #-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n #-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n #-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\nopen SimpleSearch public\nopen Sum public\nopen Count public\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192\n search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = search-\u2257 op _ _ (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nopen Defs public\n","old_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Category.Applicative\nopen import Category.Monad\nopen import Data.Nat.NP hiding (_==_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nopen import Data.Maybe.NP\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (_\u229b_; rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties\nimport Data.List as L\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nprivate\n module M {a} {A : Set a} {M : Set a \u2192 Set a} (appl : RawApplicative M) where\n open RawApplicative appl\n\n replicateM : \u2200 {n} \u2192 M A \u2192 M (Vec A n)\n replicateM {n = zero} _ = pure []\n replicateM {n = suc n} x = pure _\u2237_ \u229b x \u229b replicateM x\n\nopen M public\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM rawIApplicative (toList (0b \u2237 1b \u2237 []))\n where open RawMonad L.monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\nmodule Search {i} {I : Set i} (`1 : I) (`2*_ : I \u2192 I)\n {a} {A : I \u2192 Set a} (_\u00b7_ : \u2200 {m} \u2192 A m \u2192 A m \u2192 A (`2* m)) where\n\n `2^_ : \u2115 \u2192 I\n `2^_ = fold `1 `2*_\n\n search : \u2200 {n} \u2192 (Bits n \u2192 A `1) \u2192 A (`2^ n)\n search {zero} f = f []\n search {suc n} f = search (f \u2218 0\u2237_) \u00b7 search (f \u2218 1\u2237_)\n\n searchBit : (Bit \u2192 A `1) \u2192 A (`2* `1)\n searchBit f = f 0b \u00b7 f 1b\n\n -- search-ext\n search-\u2257 : \u2200 {n} (f g : Bits n \u2192 A `1) \u2192 f \u2257 g \u2192 search f \u2261 search g\n search-\u2257 {zero} f g f\u2257g = f\u2257g []\n search-\u2257 {suc n} f g f\u2257g\n rewrite search-\u2257 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u2257g \u2218 0\u2237_)\n | search-\u2257 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u2257g \u2218 1\u2237_) = refl\n\n module Comm (\u00b7-comm : \u2200 {m} (x y : A m) \u2192 x \u00b7 y \u2261 y \u00b7 x) where\n\n {- This pad bit vector allows to specify which bit do we negate in the vector. -}\n search-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 A `1) \u2192 search f \u2261 search (f \u2218 _\u2295_ pad)\n search-comm {zero} pad f = refl\n search-comm {suc n} (b \u2237 pad) f\n rewrite search-comm pad (f \u2218 0\u2237_)\n | search-comm pad (f \u2218 1\u2237_)\n with b\n ... | true = \u00b7-comm (search (f \u2218 0\u2237_ \u2218 _\u2295_ pad)) _\n ... | false = refl\n open Comm public\n\nopen Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\nmodule SimpleSearch {a} {A : Set a} (_\u00b7_ : A \u2192 A \u2192 A) where\n\n open Search 1 2*_ {A = const A} _\u00b7_ public\n\n search-const\u03b5\u2261\u03b5 : \u2200 \u03b5 (\u03b5\u00b7\u03b5 : \u03b5 \u00b7 \u03b5 \u2261 \u03b5) n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n search-const\u03b5\u2261\u03b5 \u03b5 \u03b5\u00b7\u03b5 = go\n where\n go : \u2200 n \u2192 search {n = n} (const \u03b5) \u2261 \u03b5\n go zero = refl\n go (suc n) rewrite go n = \u03b5\u00b7\u03b5\n\n searchBit-search : \u2200 n (f : Bits (suc n) \u2192 A) \u2192 searchBit (\u03bb b \u2192 search (f \u2218 _\u2237_ b)) \u2261 search f\n searchBit-search n f = refl\n\n search-\u2257\u2082 : \u2200 {m n} (f g : Bits m \u2192 Bits n \u2192 A) \u2192 f \u2257\u2082 g\n \u2192 search (search \u2218 f) \u2261 search (search \u2218 g)\n search-\u2257\u2082 f g f\u2257g = search-\u2257 (search \u2218 f) (search \u2218 g) (\u03bb xs \u2192\n search-\u2257 (f xs) (g xs) (\u03bb ys \u2192\n f\u2257g xs ys))\n\n module Interchange (\u00b7-interchange : \u2200 x y z t \u2192 (x \u00b7 y) \u00b7 (z \u00b7 t) \u2261 (x \u00b7 z) \u00b7 (y \u00b7 t)) where\n\n search-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 A) \u2192 search (\u03bb x \u2192 f\u2080 x \u00b7 f\u2081 x) \u2261 search f\u2080 \u00b7 search f\u2081\n search-dist {zero} _ _ = refl\n search-dist {suc n} f\u2080 f\u2081\n rewrite search-dist (f\u2080 \u2218 0\u2237_) (f\u2081 \u2218 0\u2237_)\n | search-dist (f\u2080 \u2218 1\u2237_) (f\u2081 \u2218 1\u2237_)\n = \u00b7-interchange _ _ _ _\n\n search-searchBit : \u2200 {n} (f : Bits (suc n) \u2192 A) \u2192\n search (\u03bb xs \u2192 searchBit (\u03bb b \u2192 f (b \u2237 xs))) \u2261 search f\n search-searchBit f = search-dist (f \u2218 0\u2237_) (f \u2218 1\u2237_)\n\n search-+ : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m + n} f\n \u2261 search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n search-+ {zero} f = refl\n search-+ {suc m} f rewrite search-+ {m} (f \u2218 0\u2237_)\n | search-+ {m} (f \u2218 1\u2237_) = refl\n\n search-search : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n search-search {zero} f = refl\n search-search {suc m} {n} f\n rewrite search-search {m} {n} (f \u2218 0\u2237_)\n | search-search {m} {n} (f \u2218 1\u2237_)\n | search-searchBit {n} (\u03bb { (b \u2237 ys) \u2192 search {m} (\u03bb xs \u2192 f (b \u2237 xs ++ ys)) })\n = refl\n {- -- It might also be done by using search-dist twice and commutativity of addition.\n -- However, this also affect 'f' and makes this proof actually longer.\n search-search {m} {n} f =\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 {!!} \u27e9\n search {m + n} f\n \u2261\u27e8 {!!} \u27e9\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 {!!} \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u220e\n where open \u2261-Reasoning\n -}\n\n search-swap : \u2200 {m n} (f : Bits (m + n) \u2192 A) \u2192 search {n + m} (f \u2218 vswap n) \u2261 search {m + n} f\n search-swap {m} {n} f =\n search {n + m} (f \u2218 vswap n)\n \u2261\u27e8 search-+ {n} {m} (f \u2218 vswap n) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (vswap n (ys ++ xs))))\n \u2261\u27e8 search-\u2257\u2082 {n} {m}\n (\u03bb ys \u2192 f \u2218 vswap n \u2218 _++_ ys)\n (\u03bb ys \u2192 f \u2218 flip _++_ ys)\n (\u03bb ys xs \u2192 cong f (swap-++ n ys xs)) \u27e9\n search {n} (\u03bb ys \u2192 search {m} (\u03bb xs \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-search {m} {n} f) \u27e9\n search {m} (\u03bb xs \u2192 search {n} (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261\u27e8 sym (search-+ {m} {n} f) \u27e9\n search {m + n} f\n \u220e\n where open \u2261-Reasoning\n\nmodule Sum where\n open SimpleSearch _+_ using (module Comm; module Interchange; search-const\u03b5\u2261\u03b5)\n open SimpleSearch _+_ public using () renaming (search to sum; search-\u2257 to sum-\u2257; searchBit to sumBit;\n search-\u2257\u2082 to sum-\u2257\u2082)\n open Comm \u2115\u00b0.+-comm public renaming (search-comm to sum-comm)\n open Interchange +-interchange public renaming (\n search-dist to sum-dist;\n search-searchBit to sum-sumBit;\n search-+ to sum-+;\n search-search to sum-sum;\n search-swap to sum-swap)\n\n sum-const0\u22610 : \u2200 n \u2192 sum {n = n} (const 0) \u2261 0\n sum-const0\u22610 n = search-const\u03b5\u2261\u03b5 0 refl n\n\n sum-const : \u2200 n x \u2192 sum {n} (const x) \u2261 \u27e82^ n * x \u27e9\n sum-const zero _ = refl\n sum-const (suc n) x = cong\u2082 _+_ (sum-const n x) (sum-const n x)\n\n#\u27e8_\u27e9\u1da0 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 Fin (suc (2^ n))\n#\u27e8 pred \u27e9\u1da0 = count\u1da0 pred (allBits _)\n\nmodule Count where\n open Sum\n\n #\u27e8_\u27e9 : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192 \u2115\n #\u27e8 pred \u27e9 = sum (Bool.to\u2115 \u2218 pred)\n\n -- #-ext\n #-\u2257 : \u2200 {n} (f g : Bits n \u2192 Bool) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n #-\u2257 f g f\u2257g = sum-\u2257 (Bool.to\u2115 \u2218 f) (Bool.to\u2115 \u2218 g) (\u03bb x \u2192 \u2261.cong Bool.to\u2115 (f\u2257g x))\n\n #-comm : \u2200 {n} (pad : Bits n) (f : Bits n \u2192 Bool) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ pad \u27e9\n #-comm pad f = sum-comm pad (Bool.to\u2115 \u2218 f)\n\n #-\u2295 : \u2200 {c} (bs : Bits c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 _\u2295_ bs \u27e9\n #-\u2295 = #-comm\n\n #-const : \u2200 n b \u2192 #\u27e8 (\u03bb (_ : Bits n) \u2192 b) \u27e9 \u2261 \u27e82^ n * Bool.to\u2115 b \u27e9\n #-const n b = sum-const n (Bool.to\u2115 b)\n\n #never\u22610 : \u2200 n \u2192 #\u27e8 never n \u27e9 \u2261 0\n #never\u22610 = sum-const0\u22610\n\n #always\u22612^_ : \u2200 n \u2192 #\u27e8 always n \u27e9 \u2261 2^ n\n #always\u22612^ n = sum-const n 1\n\n #-dist : \u2200 {n} (f\u2080 f\u2081 : Bits n \u2192 Bit) \u2192 sum (\u03bb x \u2192 Bool.to\u2115 (f\u2080 x) + Bool.to\u2115 (f\u2081 x)) \u2261 #\u27e8 f\u2080 \u27e9 + #\u27e8 f\u2081 \u27e9\n #-dist f\u2080 f\u2081 = sum-dist (Bool.to\u2115 \u2218 f\u2080) (Bool.to\u2115 \u2218 f\u2081)\n\n #-+ : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n #\u27e8 f \u27e9 \u2261 sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9 )\n #-+ {m} {n} f = sum-+ {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-# : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192\n sum {m} (\u03bb xs \u2192 #\u27e8 (\u03bb ys \u2192 f (xs ++ ys)) \u27e9)\n \u2261 sum {n} (\u03bb ys \u2192 #\u27e8 (\u03bb (xs : Bits m) \u2192 f (xs ++ ys)) \u27e9)\n #-# {m} {n} f = sum-sum {m} {n} (Bool.to\u2115 \u2218 f)\n\n #-swap : \u2200 {m n} (f : Bits (m + n) \u2192 Bit) \u2192 #\u27e8 f \u2218 vswap n {m} \u27e9 \u2261 #\u27e8 f \u27e9\n #-swap {m} {n} f = sum-swap {m} {n} (Bool.to\u2115 \u2218 f)\n\n #\u27e8==_\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 _==_ xs \u27e9 \u2261 1\n #\u27e8== [] \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (true \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n #\u27e8==_\u27e9 {suc n} (false \u2237 xs) rewrite #never\u22610 n | #\u27e8== xs \u27e9 = refl\n\n \u2257-cong-# : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\n \u2257-cong-# f g f\u2257g = sum-\u2257 _ _ (cong Bool.to\u2115 \u2218 f\u2257g)\n\n -- #-+ : \u2200 {n a b} (f : Bits (suc n) \u2192 Bit) \u2192 #\u27e8 f \u2218 0\u2237_ \u27e9 \u2261 a \u2192 #\u27e8 f \u2218 1\u2237_ \u27e9 \u2261 b \u2192 #\u27e8 f \u27e9 \u2261 a + b\n -- #-+ f f0 f1 rewrite f0 | f1 = refl\n\n #-take-drop : \u2200 m n (f : Bits m \u2192 Bit) (g : Bits n \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 take m) |\u2227| (g \u2218 drop m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-take-drop zero n f g with f []\n ... | true rewrite \u2115\u00b0.+-comm #\u27e8 g \u27e9 0 = refl\n ... | false = #never\u22610 n\n #-take-drop (suc m) n f g\n rewrite \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 0\u2237_)\n ((f \u2218 0\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 0b x) (drop-\u2237 m 0b x))\n | #-take-drop m n (f \u2218 0\u2237_) g\n | \u2257-cong-# ((f \u2218 take (suc m)) |\u2227| (g \u2218 drop (suc m)) \u2218 1\u2237_)\n ((f \u2218 1\u2237_ \u2218 take m) |\u2227| (g \u2218 drop m))\n (\u03bb x \u2192 cong\u2082 (\u03bb x y \u2192 f x \u2227 g y) (take-\u2237 m 1b x) (drop-\u2237 m 1b x))\n | #-take-drop m n (f \u2218 1\u2237_) g\n = sym (proj\u2082 \u2115\u00b0.distrib #\u27e8 g \u27e9 #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9)\n\n #-drop-take : \u2200 m n (f : Bits n \u2192 Bit) (g : Bits m \u2192 Bit)\n \u2192 #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9 \u2261 #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n #-drop-take m n f g =\n #\u27e8 (f \u2218 drop m) |\u2227| (g \u2218 take m) \u27e9\n \u2261\u27e8 \u2257-cong-# ((f \u2218 drop m) |\u2227| (g \u2218 take m)) ((g \u2218 take m) |\u2227| (f \u2218 drop m)) (\u03bb x \u2192 Bool\u00b0.+-comm (f (drop m x)) _) \u27e9\n #\u27e8 (g \u2218 take m) |\u2227| (f \u2218 drop m) \u27e9\n \u2261\u27e8 #-take-drop m n g f \u27e9\n #\u27e8 g \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 \u2115\u00b0.*-comm #\u27e8 g \u27e9 _ \u27e9\n #\u27e8 f \u27e9 * #\u27e8 g \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-take : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 take m {n} \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-take m n f = #\u27e8 f \u2218 take m {n} \u27e9\n \u2261\u27e8 #-drop-take m n (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #-drop : \u2200 m n (f : Bits m \u2192 Bit) \u2192 #\u27e8 f \u2218 drop n \u27e9 \u2261 2^ n * #\u27e8 f \u27e9\n #-drop m n f = #\u27e8 f \u2218 drop n \u27e9\n \u2261\u27e8 #-take-drop n m (always n) f \u27e9\n #\u27e8 always n \u27e9 * #\u27e8 f \u27e9\n \u2261\u27e8 cong (flip _*_ #\u27e8 f \u27e9) (#always\u22612^ n) \u27e9\n 2^ n * #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\n #\u27e8_==\u27e9 : \u2200 {n} (xs : Bits n) \u2192 #\u27e8 flip _==_ xs \u27e9 \u2261 1\n #\u27e8 xs ==\u27e9 = trans (\u2257-cong-# (flip _==_ xs) (_==_ xs) (flip ==-comm xs)) #\u27e8== xs \u27e9\n\n #\u21d2 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 (\u2200 x \u2192 T (f x) \u2192 T (g x)) \u2192 #\u27e8 f \u27e9 \u2264 #\u27e8 g \u27e9\n #\u21d2 {zero} f g f\u21d2g with f [] | g [] | f\u21d2g []\n ... | true | true | _ = s\u2264s z\u2264n\n ... | true | false | p = \u22a5-elim (p _)\n ... | false | _ | _ = z\u2264n\n #\u21d2 {suc n} f g f\u21d2g = #\u21d2 (f \u2218 0\u2237_) (g \u2218 0\u2237_) (f\u21d2g \u2218 0\u2237_)\n +-mono #\u21d2 (f \u2218 1\u2237_) (g \u2218 1\u2237_) (f\u21d2g \u2218 1\u2237_)\n\n #-\u2227-\u2228\u1d47 : \u2200 x y \u2192 Bool.to\u2115 (x \u2227 y) + Bool.to\u2115 (x \u2228 y) \u2261 Bool.to\u2115 x + Bool.to\u2115 y\n #-\u2227-\u2228\u1d47 true y rewrite \u2115\u00b0.+-comm (Bool.to\u2115 y) 1 = refl\n #-\u2227-\u2228\u1d47 false y = refl\n\n #-\u2227-\u2228 : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2227| g \u27e9 + #\u27e8 f |\u2228| g \u27e9 \u2261 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #-\u2227-\u2228 {zero} f g = #-\u2227-\u2228\u1d47 (f []) (g [])\n #-\u2227-\u2228 {suc n} f g =\n trans\n (trans\n (helper #\u27e8 (f \u2218 0\u2237_) |\u2227| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2227| (g \u2218 1\u2237_) \u27e9\n #\u27e8 (f \u2218 0\u2237_) |\u2228| (g \u2218 0\u2237_) \u27e9\n #\u27e8 (f \u2218 1\u2237_) |\u2228| (g \u2218 1\u2237_) \u27e9)\n (cong\u2082 _+_ (#-\u2227-\u2228 (f \u2218 0\u2237_) (g \u2218 0\u2237_))\n (#-\u2227-\u2228 (f \u2218 1\u2237_) (g \u2218 1\u2237_))))\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9)\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228' : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 #\u27e8 f |\u2228| g \u27e9 \u2264 #\u27e8 f \u27e9 + #\u27e8 g \u27e9\n #\u2228' {zero} f g with f []\n ... | true = s\u2264s z\u2264n\n ... | false = \u2115\u2264.refl\n #\u2228' {suc _} f g = \u2115\u2264.trans (#\u2228' (f \u2218 0\u2237_) (g \u2218 0\u2237_) +-mono\n #\u2228' (f \u2218 1\u2237_) (g \u2218 1\u2237_))\n (\u2115\u2264.reflexive\n (helper #\u27e8 f \u2218 0\u2237_ \u27e9 #\u27e8 g \u2218 0\u2237_ \u27e9 #\u27e8 f \u2218 1\u2237_ \u27e9 #\u27e8 g \u2218 1\u2237_ \u27e9))\n where open SemiringSolver\n helper : \u2200 x y z t \u2192 x + y + (z + t) \u2261 x + z + (y + t)\n helper = solve 4 (\u03bb x y z t \u2192 x :+ y :+ (z :+ t) := x :+ z :+ (y :+ t)) refl\n\n #\u2228 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 (\u03bb x \u2192 f x \u2228 g x) \u27e9 \u2264 (m + n)\n #\u2228 {m} {n} {o} {f} {g} pf pg = \u2115\u2264.trans (#\u2228' f g) (pf +-mono pg)\n\n #\u2227 : \u2200 {m n o} {f g : Bits o \u2192 Bit} \u2192 #\u27e8 f \u27e9 \u2264 m \u2192 #\u27e8 g \u27e9 \u2264 n \u2192 #\u27e8 f |\u2227| g \u27e9 \u2264 (m + n)\n #\u2227 {f = f} {g} pf pg = \u2115\u2264.trans (#\u21d2 (f |\u2227| g) (f |\u2228| g) (\u03bb x \u2192 \u2227\u21d2\u2228 (f x) (g x))) (#\u2228 {f = f} pf pg)\n\n #-bound : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2264 2^ c\n #-bound zero f = Bool.to\u2115\u22641 (f [])\n #-bound (suc c) f = #-bound c (f \u2218 0\u2237_) +-mono #-bound c (f \u2218 1\u2237_)\n\n #-\u2218vnot : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 vnot \u27e9\n #-\u2218vnot _ f = #-\u2295 1\u207f f\n\n #-\u2218xor\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) b \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n #-\u2218xor\u1d62 i f b = pf\n where pad = on\u1d62 (_xor_ b) i 0\u207f\n pf : #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 on\u1d62 (_xor_ b) i \u27e9\n pf rewrite #-\u2295 pad f = \u2257-cong-# (f \u2218 _\u2295_ pad) (f \u2218 on\u1d62 (_xor_ b) i) (cong (_$_ f) \u2218 sym \u2218 on\u1d62-xor-\u2295 b i)\n\n #-\u2218not\u1d62 : \u2200 {c} (i : Fin c) (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 f \u2218 not\u1d62 i \u27e9\n #-\u2218not\u1d62 i f = #-\u2218xor\u1d62 i f true\n\n #-not\u2218 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 f \u27e9 \u2261 2^ c \u2238 #\u27e8 not \u2218 f \u27e9\n #-not\u2218 zero f with f []\n ... | true = \u2261.refl\n ... | false = \u2261.refl\n #-not\u2218 (suc c) f\n rewrite #-not\u2218 c (f \u2218 0\u2237_)\n | #-not\u2218 c (f \u2218 1\u2237_) = factor-+-\u2238 (#-bound c (not \u2218 f \u2218 0\u2237_)) (#-bound c (not \u2218 f \u2218 1\u2237_))\n\n #-not\u2218\u2032 : \u2200 c (f : Bits c \u2192 Bit) \u2192 #\u27e8 not \u2218 f \u27e9 \u2261 2^ c \u2238 #\u27e8 f \u27e9\n #-not\u2218\u2032 c f = #\u27e8 not \u2218 f \u27e9\n \u2261\u27e8 #-not\u2218 c (not \u2218 f) \u27e9\n 2^ c \u2238 #\u27e8 not \u2218 not \u2218 f \u27e9\n \u2261\u27e8 \u2261.cong (\u03bb g \u2192 2^ c \u2238 g) (\u2257-cong-# (not \u2218 not \u2218 f) f (not-involutive \u2218 f)) \u27e9\n 2^ c \u2238 #\u27e8 f \u27e9\n \u220e\n where open \u2261-Reasoning\n\nopen SimpleSearch public\nopen Sum public\nopen Count public\n\n#\u27e8\u27e9-spec : \u2200 {n} (pred : Bits n \u2192 Bool) \u2192 #\u27e8 pred \u27e9 \u2261 Fin.to\u2115 #\u27e8 pred \u27e9\u1da0\n#\u27e8\u27e9-spec {zero} pred with pred []\n... | true = refl\n... | false = refl\n#\u27e8\u27e9-spec {suc n} pred rewrite count-++ pred (vmap 0\u2237_ (allBits n)) (vmap 1\u2237_ (allBits n))\n | #\u27e8\u27e9-spec {n} (pred \u2218 0\u2237_)\n | #\u27e8\u27e9-spec {n} (pred \u2218 1\u2237_)\n | count-\u2218 0\u2237_ pred (allBits n)\n | count-\u2218 1\u2237_ pred (allBits n) = refl\n\next-# : \u2200 {c} {f g : Bits c \u2192 Bit} \u2192 f \u2257 g \u2192 #\u27e8 f \u27e9\u1da0 \u2261 #\u27e8 g \u27e9\u1da0\next-# f\u2257g = ext-count\u1da0 f\u2257g (allBits _)\n\nfind? : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192? A) \u2192? A\nfind? = search (M?._\u2223_ _)\n\nfindB : \u2200 {n} \u2192 (Bits n \u2192 Bool) \u2192? Bits n\nfindB pred = find? (\u03bb x \u2192 if pred x then just x else nothing)\n\n|de-morgan| : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2228| g \u2257 not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g))\n|de-morgan| f g x with f x\n... | true = refl\n... | false = sym (not-involutive _)\n\nsearch-de-morgan : \u2200 {n} op (f g : Bits n \u2192 Bit) \u2192\n search op (f |\u2228| g) \u2261 search op (not \u2218 ((not \u2218 f) |\u2227| (not \u2218 g)))\nsearch-de-morgan op f g = search-\u2257 op _ _ (|de-morgan| f g)\n\nsearch-hom :\n \u2200 {n a b}\n {A : Set a} {B : Set b}\n (_+_ : A \u2192 A \u2192 A)\n (_*_ : B \u2192 B \u2192 B)\n (f : A \u2192 B)\n (p : Bits n \u2192 A)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ p) \u2261 search _*_ (f \u2218 p)\nsearch-hom {zero} _ _ _ _ _ = refl\nsearch-hom {suc n} _+_ _*_ f p hom =\n trans (hom _ _)\n (cong\u2082 _*_ (search-hom _+_ _*_ f (p \u2218 0\u2237_) hom)\n (search-hom _+_ _*_ f (p \u2218 1\u2237_) hom))\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) with sucBCarry xs\n... | 0b \u2237 bs = 0b \u2237 1b \u2237 bs\n... | 1b \u2237 bs = 1b \u2237 0b \u2237 bs\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2 ^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2 ^ n) (inject+ 0 (toFin xs))\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2 ^ n + to\u2115 xs\n\nfrom\u2115 : \u2200 {n} \u2192 \u2115 \u2192 Bits n\nfrom\u2115 = fold 0\u207f sucB\n\nfromFin : \u2200 {n} \u2192 Fin (2 ^ n) \u2192 Bits n\nfromFin = from\u2115 \u2218 Fin.to\u2115\n\nlookupTbl : \u2200 {n a} {A : Set a} \u2192 Bits n \u2192 Vec A (2 ^ n) \u2192 A\nlookupTbl [] (x \u2237 []) = x\nlookupTbl (0b \u2237 key) tbl = lookupTbl key (take _ tbl)\nlookupTbl {suc n} (1b \u2237 key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))\n\nfunFromTbl : \u2200 {n a} {A : Set a} \u2192 Vec A (2 ^ n) \u2192 (Bits n \u2192 A)\nfunFromTbl = flip lookupTbl\n\ntblFromFun : \u2200 {n a} {A : Set a} \u2192 (Bits n \u2192 A) \u2192 Vec A (2 ^ n)\n-- tblFromFun f = tabulate (f \u2218 fromFin)\ntblFromFun {zero} f = f [] \u2237 []\ntblFromFun {suc n} f = tblFromFun {n} (f \u2218 0\u2237_) ++ tblFromFun {n} (f \u2218 1\u2237_) ++ []\n\nfunFromTbl\u2218tblFromFun : \u2200 {n a} {A : Set a} (fun : Bits n \u2192 A) \u2192 funFromTbl (tblFromFun fun) \u2257 fun\nfunFromTbl\u2218tblFromFun {zero} f [] = refl\nfunFromTbl\u2218tblFromFun {suc n} f (0b \u2237 xs)\n rewrite take-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ []) =\n funFromTbl\u2218tblFromFun {n} (f \u2218 0\u2237_) xs\nfunFromTbl\u2218tblFromFun {suc n} f (1b \u2237 xs)\n rewrite drop-++ (2 ^ n) (tblFromFun {n} (f \u2218 0\u2237_)) (tblFromFun {n} (f \u2218 1\u2237_) ++ [])\n | take-++ (2 ^ n) (tblFromFun {n} (f \u2218 1\u2237_)) [] =\n funFromTbl\u2218tblFromFun {n} (f \u2218 1\u2237_) xs\n\ntblFromFun\u2218funFromTbl : \u2200 {n a} {A : Set a} (tbl : Vec A (2 ^ n)) \u2192 tblFromFun {n} (funFromTbl tbl) \u2261 tbl\ntblFromFun\u2218funFromTbl {zero} (x \u2237 []) = refl\ntblFromFun\u2218funFromTbl {suc n} tbl\n rewrite tblFromFun\u2218funFromTbl {n} (take _ tbl)\n | tblFromFun\u2218funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))\n | take-them-all (2 ^ n) (drop (2 ^ n) tbl)\n | take-drop-lem (2 ^ n) tbl\n = refl\n\n{-\nsucB-lem : \u2200 {n} x \u2192 to\u2115 {2 ^ n} (sucB x) [mod 2 ^ n ] \u2261 (suc (to\u2115 x)) [mod 2 ^ n ]\nsucB-lem x = {!!}\n\n-- sucB-lem : \u2200 {n} x \u2192 (sucB (from\u2115 x)) [mod 2 ^ n ] \u2261 from\u2115 ((suc x) [mod 2 ^ n ])\n\nto\u2115\u2218from\u2115 : \u2200 {n} x \u2192 to\u2115 (from\u2115 {n} x) \u2261 x\nto\u2115\u2218from\u2115 zero = {!!}\nto\u2115\u2218from\u2115 (suc x) = {!to\u2115\u2218from\u2115 x!}\n\nto\u2115\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 to\u2115 (fromFin x) \u2261 Fin.to\u2115 x\nto\u2115\u2218fromFin x = {!!}\n\ntoFin\u2218fromFin : \u2200 {n} (x : Fin (2 ^ n)) \u2192 toFin (fromFin x) \u2261 x\ntoFin\u2218fromFin x = {!!}\n\n-- _\u1d2e : (s : String) {pf : IsBitString s} \u2192 Bits (length s)\n-- _\u1d2e =\n-}\n\nopen Defs public\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"b51a3256a3c1252ef33def3ad7127f444c50c44e","subject":"Fix \u0394-Context.","message":"Fix \u0394-Context.\n\nOld-commit-hash: ce2c7324266f9f7511dd78f63accb2c3ae8c54fb\n","repos":"inc-lc\/ilc-agda","old_file":"incremental.agda","new_file":"incremental.agda","new_contents":"module incremental where\n\n-- SIMPLE TYPES\n\n-- Syntax\n\ndata Type : Set where\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\n-- Semantics\n\nDom\u27e6_\u27e7 : Type -> Set\nDom\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7 = Dom\u27e6 \u03c4\u2081 \u27e7 \u2192 Dom\u27e6 \u03c4\u2082 \u27e7\n\n-- TYPING CONTEXTS\n\n-- Syntax\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n-- Semantics\n\ndata Empty : Set where\n \u2205 : Empty\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\nEnv\u27e6_\u27e7 : Context \u2192 Set\nEnv\u27e6 \u2205 \u27e7 = Empty\nEnv\u27e6 \u03c4 \u2022 \u0393 \u27e7 = Bind Dom\u27e6 \u03c4 \u27e7 Env\u27e6 \u0393 \u27e7\n\n-- VARIABLES\n\n-- Syntax\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\n-- Semantics\n\nlookup\u27e6_\u27e7 : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Env\u27e6 \u0393 \u27e7 \u2192 Dom\u27e6 \u03c4 \u27e7\nlookup\u27e6 this \u27e7 (v \u2022 \u03c1) = v\nlookup\u27e6 that x \u27e7 (v \u2022 \u03c1) = lookup\u27e6 x \u27e7 \u03c1\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n-- Semantics\n\neval\u27e6_\u27e7 : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Env\u27e6 \u0393 \u27e7 \u2192 Dom\u27e6 \u03c4 \u27e7\neval\u27e6 abs t \u27e7 \u03c1 = \u03bb v \u2192 eval\u27e6 t \u27e7 (v \u2022 \u03c1)\neval\u27e6 app t\u2081 t\u2082 \u27e7 \u03c1 = (eval\u27e6 t\u2081 \u27e7 \u03c1) (eval\u27e6 t\u2082 \u27e7 \u03c1)\neval\u27e6 var x \u27e7 \u03c1 = lookup\u27e6 x \u27e7 \u03c1\n\n\n-- WEAKENING\n\n-- Extend a context to a super context\n\ninfixr 10 _\u22ce_\n\n_\u22ce_ : (\u0393\u2081 \u0393\u2081 : Context) \u2192 Context\n\u2205 \u22ce \u0393\u2082 = \u0393\u2082\n(\u03c4 \u2022 \u0393\u2081) \u22ce \u0393\u2082 = \u03c4 \u2022 \u0393\u2081 \u22ce \u0393\u2082\n\n-- Lift a variable to a super context\n\nlift : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nlift {\u2205} {\u2205} x = x\nlift {\u2205} {\u03c4 \u2022 \u0393\u2082} x = that (lift {\u2205} {\u0393\u2082} x)\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} this = this\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} (that x) = that (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- Weaken a term to a super context\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken {\u0393\u2081} {\u0393\u2082} (abs {\u03c4\u2081 = \u03c4} t) = abs (weaken {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} t)\nweaken {\u0393\u2081} {\u0393\u2082} (app t\u2081 t\u2082) = app (weaken {\u0393\u2081} {\u0393\u2082} t\u2081) (weaken {\u0393\u2081} {\u0393\u2082} t\u2082)\nweaken {\u0393\u2081} {\u0393\u2082} (var x) = var (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\napply : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbf. \u03bbx. apply ( df x ) ( f x )\n\ncompose : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4)\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbdg. \u03bbx. compose ( df x ) ( dg x )\n\nnil : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4)\nnil {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs nil\n -- \u03bbx. nil\n\n-- Hey, apply is \u03b1-equivalent to compose, what's going on?\n-- Oh, and `\u0394-Type` is the identity function?\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u03c4 \u2022 \u0394-Type \u03c4 \u2022 \u0394-Context \u0393\n\n-- CHANGING TERMS WHEN THE ENVIRONMENT CHANGES\n\n\u0394-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-term {\u0393} (abs {\u03c4\u2081 = \u03c4} t) = abs (\u0394-term {\u03c4 \u2022 \u0393} t)\n\u0394-term {\u0393} (app t\u2081 t\u2082) = {!!}\n\u0394-term {\u0393} (var x) = {!!}\n","old_contents":"module incremental where\n\n-- SIMPLE TYPES\n\n-- Syntax\n\ndata Type : Set where\n _\u21d2_ : (\u03c4\u2081 \u03c4\u2082 : Type) \u2192 Type\n\ninfixr 5 _\u21d2_\n\n-- Semantics\n\nDom\u27e6_\u27e7 : Type -> Set\nDom\u27e6 \u03c4\u2081 \u21d2 \u03c4\u2082 \u27e7 = Dom\u27e6 \u03c4\u2081 \u27e7 \u2192 Dom\u27e6 \u03c4\u2082 \u27e7\n\n-- TYPING CONTEXTS\n\n-- Syntax\n\ndata Context : Set where\n \u2205 : Context\n _\u2022_ : (\u03c4 : Type) (\u0393 : Context) \u2192 Context\n\ninfixr 9 _\u2022_\n\n-- Semantics\n\ndata Empty : Set where\n \u2205 : Empty\n\ndata Bind A B : Set where\n _\u2022_ : (v : A) (\u03c1 : B) \u2192 Bind A B\n\nEnv\u27e6_\u27e7 : Context \u2192 Set\nEnv\u27e6 \u2205 \u27e7 = Empty\nEnv\u27e6 \u03c4 \u2022 \u0393 \u27e7 = Bind Dom\u27e6 \u03c4 \u27e7 Env\u27e6 \u0393 \u27e7\n\n-- VARIABLES\n\n-- Syntax\n\ndata Var : Context \u2192 Type \u2192 Set where\n this : \u2200 {\u0393 \u03c4} \u2192 Var (\u03c4 \u2022 \u0393) \u03c4\n that : \u2200 {\u0393 \u03c4 \u03c4\u2032} \u2192 (x : Var \u0393 \u03c4) \u2192 Var (\u03c4\u2032 \u2022 \u0393) \u03c4\n\n-- Semantics\n\nlookup\u27e6_\u27e7 : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Env\u27e6 \u0393 \u27e7 \u2192 Dom\u27e6 \u03c4 \u27e7\nlookup\u27e6 this \u27e7 (v \u2022 \u03c1) = v\nlookup\u27e6 that x \u27e7 (v \u2022 \u03c1) = lookup\u27e6 x \u27e7 \u03c1\n\n-- TERMS\n\n-- Syntax\n\ndata Term : Context \u2192 Type \u2192 Set where\n abs : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t : Term (\u03c4\u2081 \u2022 \u0393) \u03c4\u2082) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)\n app : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2081 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) (t\u2082 : Term \u0393 \u03c4\u2081) \u2192 Term \u0393 \u03c4\u2082\n var : \u2200 {\u0393 \u03c4} \u2192 (x : Var \u0393 \u03c4) \u2192 Term \u0393 \u03c4\n\n-- Semantics\n\neval\u27e6_\u27e7 : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 Env\u27e6 \u0393 \u27e7 \u2192 Dom\u27e6 \u03c4 \u27e7\neval\u27e6 abs t \u27e7 \u03c1 = \u03bb v \u2192 eval\u27e6 t \u27e7 (v \u2022 \u03c1)\neval\u27e6 app t\u2081 t\u2082 \u27e7 \u03c1 = (eval\u27e6 t\u2081 \u27e7 \u03c1) (eval\u27e6 t\u2082 \u27e7 \u03c1)\neval\u27e6 var x \u27e7 \u03c1 = lookup\u27e6 x \u27e7 \u03c1\n\n\n-- WEAKENING\n\n-- Extend a context to a super context\n\ninfixr 10 _\u22ce_\n\n_\u22ce_ : (\u0393\u2081 \u0393\u2081 : Context) \u2192 Context\n\u2205 \u22ce \u0393\u2082 = \u0393\u2082\n(\u03c4 \u2022 \u0393\u2081) \u22ce \u0393\u2082 = \u03c4 \u2022 \u0393\u2081 \u22ce \u0393\u2082\n\n-- Lift a variable to a super context\n\nlift : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nlift {\u2205} {\u2205} x = x\nlift {\u2205} {\u03c4 \u2022 \u0393\u2082} x = that (lift {\u2205} {\u0393\u2082} x)\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} this = this\nlift {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} (that x) = that (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- Weaken a term to a super context\n\nweaken : \u2200 {\u0393\u2081 \u0393\u2082 \u0393\u2083 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2083) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082 \u22ce \u0393\u2083) \u03c4\nweaken {\u0393\u2081} {\u0393\u2082} (abs {\u03c4\u2081 = \u03c4} t) = abs (weaken {\u03c4 \u2022 \u0393\u2081} {\u0393\u2082} t)\nweaken {\u0393\u2081} {\u0393\u2082} (app t\u2081 t\u2082) = app (weaken {\u0393\u2081} {\u0393\u2082} t\u2081) (weaken {\u0393\u2081} {\u0393\u2082} t\u2082)\nweaken {\u0393\u2081} {\u0393\u2082} (var x) = var (lift {\u0393\u2081} {\u0393\u2082} x)\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\napply : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbf. \u03bbx. apply ( df x ) ( f x )\n\ncompose : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4)\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbdg. \u03bbx. compose ( df x ) ( dg x )\n\nnil : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4)\nnil {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs nil\n -- \u03bbx. nil\n\n-- Hey, apply is \u03b1-equivalent to compose, what's going on?\n-- Oh, and `\u0394-Type` is the identity function?\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u03c4 \u2022 \u0394-Type \u03c4 \u2022 \u0393\n\n-- CHANGING TERMS WHEN THE ENVIRONMENT CHANGES\n\n\u0394-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-term {\u0393} (abs {\u03c4\u2081 = \u03c4} t) = abs (\u0394-term {\u03c4 \u2022 \u0393} t)\n\u0394-term {\u0393} (app t\u2081 t\u2082) = {!!}\n\u0394-term {\u0393} (var x) = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"70bdc97fcd407dacc27f9e32beabb2dadfaa82ba","subject":"otp-sem-sec.agda","message":"otp-sem-sec.agda\n","repos":"crypto-agda\/crypto-agda","old_file":"otp-sem-sec.agda","new_file":"otp-sem-sec.agda","new_contents":"module otp-sem-sec where\n\nopen import Function\nopen import Data.Nat.NP\nopen import Data.Bits\nopen import Data.Bool\nopen import Data.Vec\nopen import Data.Product\nopen import circuit\nopen import Relation.Binary.PropositionalEquality\n\n-- dup from Game\nEXP : \u2200 {a b} {A : Set a} {B : Set b} \u2192 Bit \u2192 (A \u2192 B) \u2192 (A \u00d7 A \u2192 B)\nEXP 0b f (x\u2080 , x\u2081) = f x\u2080\nEXP 1b f (x\u2080 , x\u2081) = f x\u2081\n\nCoins = \u2115\nPorts = \u2115\n\nmodule F'\n (|M| |C| : \u2115)\n\n (Proba : Set)\n (Pr[_\u22611] : \u2200 {c} (EXP : Bits c \u2192 Bit) \u2192 Proba)\n (dist : Proba \u2192 Proba \u2192 Proba)\n (negligible : Proba \u2192 Set)\n\n where\n\n record Power : Set where\n constructor mk\n field\n coins : Coins\n open Power public\n\n M = Bits |M|\n C = Bits |C|\n\n \u21ba : Coins \u2192 Set \u2192 Set\n \u21ba c A = Bits c \u2192 A\n\n record SemSecAdv power : Set where\n constructor mk\n\n open Power power renaming (coins to c)\n field\n {c\u2080 c\u2081} : Coins\n c\u2261c\u2080+c\u2081 : c \u2261 c\u2080 + c\u2081\n beh : \u21ba c\u2080 ((M \u00d7 M) \u00d7 (C \u2192 \u21ba c\u2081 Bit))\n\n -- Returing 0 means Chal wins, Adv looses\n -- 1 means Adv wins, Chal looses\n runSemSec : \u2200 (E : M \u2192 C) {p} (A : SemSecAdv p) (b : Bit) \u2192 \u21ba (coins p) Bit\n runSemSec E {mk c} A b R\n = case SemSecAdv.beh A R\u2080 of \u03bb\n { (m0m1 , kont) \u2192 b == kont (EXP b E m0m1) R\u2081 }\n where R\u2080 = take (SemSecAdv.c\u2080 A) (subst Bits (SemSecAdv.c\u2261c\u2080+c\u2081 A) R)\n R\u2081 = drop (SemSecAdv.c\u2080 A) (subst Bits (SemSecAdv.c\u2261c\u2080+c\u2081 A) R)\n\n -- runSemSec : \u2200 (E : M \u2192 C) {p} (A : SemSecAdv p) (b : Bit) \u2192 \u21ba (coins p) Bit\n\n negligible-advantage : \u2200 {c} (EXP : Bit \u2192 \u21ba c Bit) \u2192 Set\n negligible-advantage EXP = negligible (dist Pr[ EXP 0b \u22611] Pr[ EXP 1b \u22611])\n\n SemSec : \u2200 (E : M \u2192 C) \u2192 Power \u2192 Set\n SemSec E power = \u2200 (A : SemSecAdv power) \u2192 negligible-advantage (runSemSec E A)\n\n |K| = |M|\n K = Bits |K|\n -- OTP : K \u2192 M \u2192 C\n -- OTP = _\u2295_\n\n -- OTP-SemSec : SemSec (OTP \n\n Enc = M \u2192 C\n\n Tr = Enc \u2192 Enc\n\n -- In general the power might change\n SemSecTr : Tr \u2192 Set\n SemSecTr tr = \u2200 {E p} \u2192 SemSec (tr E) p \u2192 SemSec E p\n\n neg-pres-sem-sec : SemSecTr (_\u2218_ vnot)\n neg-pres-sem-sec {E} {p} E'-sec A = A-breaks-E\n where E' : Enc\n E' = vnot \u2218 E\n open SemSecAdv A using (c\u2261c\u2080+c\u2081 ; c\u2080 ; c\u2081) renaming (beh to A-beh)\n A'-beh : \u21ba c\u2080 ((M \u00d7 M) \u00d7 (C \u2192 \u21ba c\u2081 Bit))\n A'-beh = {!!}\n A' : SemSecAdv p\n A' = {!!}\n A'-breaks-E' : negligible-advantage (runSemSec E' A')\n A'-breaks-E' = E'-sec A'\n A-breaks-E : negligible-advantage (runSemSec E A)\n A-breaks-E = {!!}\n\n{-\n \u2295-pres-sem-sec : \u2200 mask \u2192 SemSecTr (_\u2218_ (_\u2295_ mask))\n \u2295-pres-sem-sec mask {E} {p} E-sec A-breaks-E' = pf\n where E' : Enc\n E' = (_\u2295_ mask) \u2218 E\n pf : negligible-advantage (runSemSec E' A-breaks-E')\n pf = {!!}\n-}\n\nmodule F\n (Size Time : Set)\n (FCp : Coins \u2192 Size \u2192 Time \u2192 Ports \u2192 Ports \u2192 Set)\n (Cp : Ports \u2192 Ports \u2192 Set)\n (builder : CircuitBuilder Cp)\n (toC : \u2200 {c s t i o} \u2192 FCp c s t i o \u2192 Cp (c + i) o)\n (runC : \u2200 {i o} \u2192 Cp i o \u2192 Bits i \u2192 Bits o)\n\n (|M| |C| : \u2115)\n\n (Proba : Set)\n (Pr[_\u22611] : \u2200 {c} (EXP : Bits c \u2192 Bit) \u2192 Proba)\n (dist : Proba \u2192 Proba \u2192 Proba)\n (negligible : Proba \u2192 Set)\n\n where\n\n record Power : Set where\n constructor mk\n field\n coins : Coins\n size : Size\n time : Time\n open Power public\n\n M = Bits |M|\n C = Bits |C|\n\n \u21ba : Coins \u2192 Set \u2192 Set\n \u21ba c A = Bits c \u2192 A\n\n record SemSecAdv power : Set where\n constructor mk\n\n open Power power renaming (coins to c; size to s; time to t)\n field\n fcp : FCp c s t |C| (1 + (|M| + |M|))\n\n {c\u2080 c\u2081} : Coins\n c\u2261c\u2080+c\u2081 : c \u2261 c\u2080 + c\u2081\n\n R\u2080 = Bits c\u2080\n R\u2081 = Bits c\u2081\n\n open CircuitBuilder builder\n\n cp : Cp ((c\u2080 + c\u2081) + |C|) (1 + (|M| + |M|))\n cp rewrite sym c\u2261c\u2080+c\u2081 = toC fcp\n\n cp\u2081 : Cp c\u2080 (|M| + |M|)\n cp\u2081 = padR c\u2081 >>> padR |C| >>> cp >>> tailC\n\n cp\u2082 : Cp ((c\u2080 + c\u2081) + |C|) 1\n cp\u2082 = cp >>> headC\n\n beh : \u21ba c\u2080 ((M \u00d7 M) \u00d7 (C \u2192 \u21ba c\u2081 Bit))\n beh R\u2080 = (case splitAt |M| (runC cp\u2081 R\u2080) of \u03bb { (x , y , _) \u2192 (x , y) })\n , (\u03bb C R\u2081 \u2192 head (runC cp\u2082 ((R\u2080 ++ R\u2081) ++ C)))\n\n runSemSec : \u2200 (E : M \u2192 C) {p} (A : SemSecAdv p) (b : Bit) \u2192 \u21ba (coins p) Bit\n runSemSec E {mk c s t} A b R\n = case SemSecAdv.beh A R\u2080 of \u03bb\n { (m0m1 , kont) \u2192 {- b == -} kont (EXP b E m0m1) R\u2081 }\n where R\u2080 = take (SemSecAdv.c\u2080 A) (subst Bits (SemSecAdv.c\u2261c\u2080+c\u2081 A) R)\n R\u2081 = drop (SemSecAdv.c\u2080 A) (subst Bits (SemSecAdv.c\u2261c\u2080+c\u2081 A) R)\n\n -- runSemSec : \u2200 (E : M \u2192 C) {p} (A : SemSecAdv p) (b : Bit) \u2192 \u21ba (coins p) Bit\n\n negligible-advantage : \u2200 {c} (EXP : Bit \u2192 \u21ba c Bit) \u2192 Set\n negligible-advantage EXP = negligible (dist Pr[ EXP 0b \u22611] Pr[ EXP 1b \u22611])\n\n SemSec : \u2200 (E : M \u2192 C) \u2192 Power \u2192 Set\n SemSec E power = \u2200 (A : SemSecAdv power) \u2192 negligible-advantage (runSemSec E A)\n\n |K| = |M|\n K = Bits |K|\n -- OTP : K \u2192 M \u2192 C\n -- OTP = _\u2295_\n\n -- OTP-SemSec : SemSec (OTP \n\n Enc = M \u2192 C\n\n Tr = Enc \u2192 Enc\n\n -- In general the power might change\n SemSecTr : Tr \u2192 Set\n SemSecTr tr = \u2200 {E p} \u2192 SemSec (tr E) p \u2192 SemSec E p\n\n neg-pres-sem-sec : SemSecTr (_\u2218_ vnot)\n neg-pres-sem-sec {E} {p} E'-sec A = A-breaks-E\n where E' : Enc\n E' = vnot \u2218 E\n open SemSecAdv A using (c\u2261c\u2080+c\u2081 ; c\u2080 ; c\u2081) renaming (beh to A-beh)\n A'-beh : \u21ba c\u2080 ((M \u00d7 M) \u00d7 (C \u2192 \u21ba c\u2081 Bit))\n A'-beh = {!!}\n A' : SemSecAdv p\n A' = {!!}\n A'-breaks-E' : negligible-advantage (runSemSec E' A')\n A'-breaks-E' = E'-sec A'\n A-breaks-E : negligible-advantage (runSemSec E A)\n A-breaks-E = {!!}\n\n{-\n \u2295-pres-sem-sec : \u2200 mask \u2192 SemSecTr (_\u2218_ (_\u2295_ mask))\n \u2295-pres-sem-sec mask {E} {p} E-sec A-breaks-E' = pf\n where E' : Enc\n E' = (_\u2295_ mask) \u2218 E\n pf : negligible-advantage (runSemSec E' A-breaks-E')\n pf = {!!}\n-}\n","old_contents":"module otp-sem-sec where\n\nopen import Function\nopen import Data.Nat.NP\nopen import Data.Bits\nopen import Data.Bool\nopen import Data.Vec\nopen import Data.Product\nopen import circuit\nopen import Relation.Binary.PropositionalEquality\n\n-- dup from Game\nEXP : \u2200 {a b} {A : Set a} {B : Set b} \u2192 Bit \u2192 (A \u2192 B) \u2192 (A \u00d7 A \u2192 B)\nEXP 0b f (x\u2080 , x\u2081) = f x\u2080\nEXP 1b f (x\u2080 , x\u2081) = f x\u2081\n\nCoins = \u2115\nPorts = \u2115\n\nmodule F\n (Size Time : Set)\n (FCp : Coins \u2192 Size \u2192 Time \u2192 Ports \u2192 Ports \u2192 Set)\n (Cp : Ports \u2192 Ports \u2192 Set)\n (builder : CircuitBuilder Cp)\n (toC : \u2200 {c s t i o} \u2192 FCp c s t i o \u2192 Cp (c + i) o)\n (runC : \u2200 {i o} \u2192 Cp i o \u2192 Bits i \u2192 Bits o)\n\n (|M| |C| : \u2115)\n\n (Proba : Set)\n (Pr[_\u22611] : \u2200 {c} (EXP : Bits c \u2192 Bit) \u2192 Proba)\n (dist : Proba \u2192 Proba \u2192 Proba)\n (negligible : Proba \u2192 Set)\n\n where\n\n record Power : Set where\n constructor mk\n field\n coins : Coins\n size : Size\n time : Time\n open Power public\n\n M = Bits |M|\n C = Bits |C|\n\n \u21ba : Coins \u2192 Set \u2192 Set\n \u21ba c A = Bits c \u2192 A\n\n record SemSecAdv power : Set where\n constructor mk\n\n open Power power renaming (coins to c; size to s; time to t)\n field\n fcp : FCp c s t |C| (1 + (|M| + |M|))\n\n {c\u2080 c\u2081} : Coins\n c\u2261c\u2080+c\u2081 : c \u2261 c\u2080 + c\u2081\n\n R\u2080 = Bits c\u2080\n R\u2081 = Bits c\u2081\n\n open CircuitBuilder builder\n\n cp : Cp ((c\u2080 + c\u2081) + |C|) (1 + (|M| + |M|))\n cp rewrite sym c\u2261c\u2080+c\u2081 = toC fcp\n\n cp\u2081 : Cp c\u2080 (|M| + |M|)\n cp\u2081 = padR c\u2081 >>> padR |C| >>> cp >>> tailC\n\n cp\u2082 : Cp ((c\u2080 + c\u2081) + |C|) 1\n cp\u2082 = cp >>> headC\n\n beh : \u21ba c\u2080 ((M \u00d7 M) \u00d7 (C \u2192 \u21ba c\u2081 Bit))\n beh R\u2080 = (case splitAt |M| (runC cp\u2081 R\u2080) of \u03bb { (x , y , _) \u2192 (x , y) })\n , (\u03bb C R\u2081 \u2192 head (runC cp\u2082 ((R\u2080 ++ R\u2081) ++ C)))\n\n runSemSec : \u2200 (E : M \u2192 C) {p} (A : SemSecAdv p) (b : Bit) \u2192 \u21ba (coins p) Bit\n runSemSec E {mk c s t} A b R\n = case SemSecAdv.beh A R\u2080 of \u03bb\n { (m0m1 , kont) \u2192 {- b == -} kont (EXP b E m0m1) R\u2081 }\n where R\u2080 = take (SemSecAdv.c\u2080 A) (subst Bits (SemSecAdv.c\u2261c\u2080+c\u2081 A) R)\n R\u2081 = drop (SemSecAdv.c\u2080 A) (subst Bits (SemSecAdv.c\u2261c\u2080+c\u2081 A) R)\n\n -- runSemSec : \u2200 (E : M \u2192 C) {p} (A : SemSecAdv p) (b : Bit) \u2192 \u21ba (coins p) Bit\n\n negligible-advantage : \u2200 {c} (EXP : Bit \u2192 \u21ba c Bit) \u2192 Set\n negligible-advantage EXP = negligible (dist Pr[ EXP 0b \u22611] Pr[ EXP 1b \u22611])\n\n SemSec : \u2200 (E : M \u2192 C) \u2192 Power \u2192 Set\n SemSec E power = \u2200 (A : SemSecAdv power) \u2192 negligible-advantage (runSemSec E A)\n\n |K| = |M|\n K = Bits |K|\n -- OTP : K \u2192 M \u2192 C\n -- OTP = _\u2295_\n\n -- OTP-SemSec : SemSec (OTP \n\n Enc = M \u2192 C\n\n Tr = Enc \u2192 Enc\n\n -- In general the power might change\n SemSecTr : Tr \u2192 Set\n SemSecTr tr = \u2200 {E p} \u2192 SemSec (tr E) p \u2192 SemSec E p\n\n neg-pres-sem-sec : SemSecTr (_\u2218_ vnot)\n neg-pres-sem-sec {E} {p} E'-sec A = A-breaks-E\n where E' : Enc\n E' = vnot \u2218 E\n open SemSecAdv A using (c\u2261c\u2080+c\u2081 ; c\u2080 ; c\u2081) renaming (beh to A-beh)\n A'-beh : \u21ba c\u2080 ((M \u00d7 M) \u00d7 (C \u2192 \u21ba c\u2081 Bit))\n A'-beh = {!!}\n A' : SemSecAdv p\n A' = {!!}\n A'-breaks-E' : negligible-advantage (runSemSec E' A')\n A'-breaks-E' = E'-sec A'\n A-breaks-E : negligible-advantage (runSemSec E A)\n A-breaks-E = ?\n\n{-\n \u2295-pres-sem-sec : \u2200 mask \u2192 SemSecTr (_\u2218_ (_\u2295_ mask))\n \u2295-pres-sem-sec mask {E} {p} E-sec A-breaks-E' = pf\n where E' : Enc\n E' = (_\u2295_ mask) \u2218 E\n pf : negligible-advantage (runSemSec E' A-breaks-E')\n pf = {!!}\n-}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"b3368bf58769715c5c6847366026d4abb50940fc","subject":"Nat: + <=.isTotalOrder","message":"Nat: + <=.isTotalOrder\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Nat\/NP.agda","new_file":"lib\/Data\/Nat\/NP.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Nat.NP where\n\nimport Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat public hiding (module GeneralisedArithmetic; module \u2264-Reasoning; fold)\nopen import Data.Nat.Properties as Props\nopen import Data.Nat.Logical\nopen import Data.Bool\nopen import Data.Product using (proj\u2081; proj\u2082; \u2203; _,_)\nopen import Data.Sum renaming (map to \u228e-map)\nopen import Data.Empty using (\u22a5-elim)\nopen import Function.NP\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_; _\u2257_; module \u2261-Reasoning)\n\nmodule \u2115\u00b0 = Algebra.CommutativeSemiring Props.commutativeSemiring\nmodule \u2115cmp = StrictTotalOrder Props.strictTotalOrder\nmodule \u2115\u2264 = DecTotalOrder decTotalOrder\nmodule \u2294\u00b0 = Algebra.CommutativeSemiringWithoutOne \u2294-\u2293-0-commutativeSemiringWithoutOne\n\nmodule \u2264-Reasoning where\n open Preorder-Reasoning \u2115\u2264.preorder public renaming (_\u223c\u27e8_\u27e9_ to _\u2264\u27e8_\u27e9_)\n infixr 2 _\u2261\u27e8_\u27e9_\n _\u2261\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x \u2261 y \u2192 y \u2264 z \u2192 x \u2264 z\n _ \u2261\u27e8 \u2261.refl \u27e9 p = p\n infixr 2 _<\u27e8_\u27e9_\n _<\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x < y \u2192 y \u2264 z \u2192 x < z\n x <\u27e8 p \u27e9 q = suc x \u2264\u27e8 p \u27e9 q\n\nsuc-injective : \u2200 {n m : \u2115} \u2192 (suc n \u2236 \u2115) \u2261 suc m \u2192 n \u2261 m\nsuc-injective = \u2261.cong pred\n\nfold : \u2200 {a} {A : Set a} \u2192 A \u2192 Endo A \u2192 \u2115 \u2192 A\nfold x f n = nest n f x\n\n2*_ : \u2115 \u2192 \u2115\n2* x = x + x\n\n2*-spec : \u2200 n \u2192 2* n \u2261 2 * n\n2*-spec n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\n\n_==_ : (x y : \u2115) \u2192 Bool\nzero == zero = true\nzero == suc _ = false\nsuc _ == zero = false\nsuc m == suc n = m == n\n\n+-assoc-comm : \u2200 x y z \u2192 x + (y + z) \u2261 y + (x + z)\n+-assoc-comm x y z rewrite \u2261.sym (\u2115\u00b0.+-assoc x y z)\n | \u2115\u00b0.+-comm x y\n | \u2115\u00b0.+-assoc y x z = \u2261.refl\n\n+-interchange : Interchange _\u2261_ _+_ _+_\n+-interchange = InterchangeFromAssocCommCong.\u2219-interchange _\u2261_ \u2261.isEquivalence\n _+_ \u2115\u00b0.+-assoc \u2115\u00b0.+-comm (\u03bb z \u2192 \u2261.cong (flip _+_ z))\n\n2*\u2032_ : \u2115 \u2192 \u2115\n2*\u2032_ = fold 0 (suc \u2218\u2032 suc)\n\n2*\u2032-spec : \u2200 n \u2192 2*\u2032 n \u2261 2* n\n2*\u2032-spec zero = \u2261.refl\n2*\u2032-spec (suc n) rewrite 2*\u2032-spec n | +-assoc-comm 1 n n = \u2261.refl\n\ndist : \u2115 \u2192 \u2115 \u2192 \u2115\ndist zero y = y\ndist x zero = x\ndist (suc x) (suc y) = dist x y\n\ndist-refl : \u2200 x \u2192 dist x x \u2261 0\ndist-refl zero = \u2261.refl\ndist-refl (suc x) rewrite dist-refl x = \u2261.refl\n\ndist-0\u2261id : \u2200 x \u2192 dist 0 x \u2261 x\ndist-0\u2261id _ = \u2261.refl\n\ndist-x-x+y\u2261y : \u2200 x y \u2192 dist x (x + y) \u2261 y\ndist-x-x+y\u2261y zero y = \u2261.refl\ndist-x-x+y\u2261y (suc x) y = dist-x-x+y\u2261y x y\n\ndist-sym : \u2200 x y \u2192 dist x y \u2261 dist y x\ndist-sym zero zero = \u2261.refl\ndist-sym zero (suc y) = \u2261.refl\ndist-sym (suc x) zero = \u2261.refl\ndist-sym (suc x) (suc y) = dist-sym x y\n\ndist-x+ : \u2200 x y z \u2192 dist (x + y) (x + z) \u2261 dist y z\ndist-x+ zero y z = \u2261.refl\ndist-x+ (suc x) y z = dist-x+ x y z\n\ndist-2* : \u2200 x y \u2192 dist (2* x) (2* y) \u2261 2* dist x y\ndist-2* zero y = \u2261.refl\ndist-2* (suc x) zero = \u2261.refl\ndist-2* (suc x) (suc y) rewrite +-assoc-comm x 1 x\n | +-assoc-comm y 1 y = dist-2* x y\n\ndist-asym-def : \u2200 {x y} \u2192 x \u2264 y \u2192 x + dist x y \u2261 y\ndist-asym-def z\u2264n = \u2261.refl\ndist-asym-def (s\u2264s pf) = \u2261.cong suc (dist-asym-def pf)\n\ndist-sym-wlog : \u2200 (f : \u2115 \u2192 \u2115) \u2192 (\u2200 x k \u2192 dist (f x) (f (x + k)) \u2261 f k) \u2192 \u2200 x y \u2192 dist (f x) (f y) \u2261 f (dist x y)\ndist-sym-wlog f pf x y with compare x y\ndist-sym-wlog f pf x .(suc (x + k)) | less .x k with pf x (suc k)\n... | q rewrite +-assoc-comm x 1 k | q | \u2261.sym (+-assoc-comm x 1 k) | dist-x-x+y\u2261y x (suc k) = \u2261.refl\ndist-sym-wlog f pf .y y | equal .y with pf y 0\n... | q rewrite \u2115\u00b0.+-comm y 0 | dist-refl y = q\ndist-sym-wlog f pf .(suc (y + k)) y | greater .y k with pf y (suc k)\n... | q rewrite +-assoc-comm 1 y k | dist-sym (y + suc k) y | dist-x-x+y\u2261y y (suc k) | dist-sym (f (y + suc k)) (f y) = q\n\ndist-x* : \u2200 x y z \u2192 dist (x * y) (x * z) \u2261 x * dist y z\ndist-x* x = dist-sym-wlog (_*_ x) pf\n where pf : \u2200 a k \u2192 dist (x * a) (x * (a + k)) \u2261 x * k\n pf a k rewrite proj\u2081 \u2115\u00b0.distrib x a k = dist-x-x+y\u2261y (x * a) _\n\n2^\u27e8_\u27e9* : \u2115 \u2192 \u2115 \u2192 \u2115\n2^\u27e8 n \u27e9* x = fold x 2*_ n\n\n\u27e82^_*_\u27e9 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e82^ n * x \u27e9 = 2^\u27e8 n \u27e9* x\n\n2*-distrib : \u2200 x y \u2192 2* x + 2* y \u2261 2* (x + y) \n2*-distrib = solve 2 (\u03bb x y \u2192 2:* x :+ 2:* y := 2:* (x :+ y)) \u2261.refl\n where open SemiringSolver\n 2:* : \u2200 {n} \u2192 Polynomial n \u2192 Polynomial n\n 2:* x = x :+ x\n\n2^*-distrib : \u2200 k x y \u2192 \u27e82^ k * (x + y)\u27e9 \u2261 \u27e82^ k * x \u27e9 + \u27e82^ k * y \u27e9\n2^*-distrib zero x y = \u2261.refl\n2^*-distrib (suc k) x y rewrite 2^*-distrib k x y = \u2261.sym (2*-distrib \u27e82^ k * x \u27e9 \u27e82^ k * y \u27e9)\n\n2^*-2*-comm : \u2200 k x \u2192 \u27e82^ k * 2* x \u27e9 \u2261 2* \u27e82^ k * x \u27e9\n2^*-2*-comm k x = 2^*-distrib k x x\n\ndist-2^* : \u2200 x y z \u2192 dist \u27e82^ x * y \u27e9 \u27e82^ x * z \u27e9 \u2261 \u27e82^ x * dist y z \u27e9\ndist-2^* x = dist-sym-wlog (2^\u27e8 x \u27e9*) pf\n where pf : \u2200 a k \u2192 dist \u27e82^ x * a \u27e9 \u27e82^ x * (a + k) \u27e9 \u2261 \u27e82^ x * k \u27e9\n pf a k rewrite 2^*-distrib x a k = dist-x-x+y\u2261y \u27e82^ x * a \u27e9 \u27e82^ x * k \u27e9\n\n2*-mono : \u2200 {a b} \u2192 a \u2264 b \u2192 2* a \u2264 2* b\n2*-mono pf = pf +-mono pf\n\n2^*-mono : \u2200 k {a b} \u2192 a \u2264 b \u2192 \u27e82^ k * a \u27e9 \u2264 \u27e82^ k * b \u27e9\n2^*-mono zero pf = pf\n2^*-mono (suc k) pf = 2*-mono (2^*-mono k pf)\n\n2*-mono\u2032 : \u2200 {a b} \u2192 2* a \u2264 2* b \u2192 a \u2264 b\n2*-mono\u2032 {zero} pf = z\u2264n\n2*-mono\u2032 {suc a} {zero} ()\n2*-mono\u2032 {suc a} {suc b} pf rewrite +-assoc-comm a 1 a\n | +-assoc-comm b 1 b = s\u2264s (2*-mono\u2032 (\u2264-pred (\u2264-pred pf)))\n\n2^*-mono\u2032 : \u2200 k {a b} \u2192 \u27e82^ k * a \u27e9 \u2264 \u27e82^ k * b \u27e9 \u2192 a \u2264 b\n2^*-mono\u2032 zero = id\n2^*-mono\u2032 (suc k) = 2^*-mono\u2032 k \u2218 2*-mono\u2032\n\n2^-comm : \u2200 x y z \u2192 \u27e82^ x * \u27e82^ y * z \u27e9 \u27e9 \u2261 \u27e82^ y * \u27e82^ x * z \u27e9 \u27e9\n2^-comm zero y z = \u2261.refl\n2^-comm (suc x) y z rewrite 2^-comm x y z = \u2261.sym (2^*-2*-comm y \u27e82^ x * z \u27e9)\n\n2^-+ : \u2200 x y z \u2192 \u27e82^ x * \u27e82^ y * z \u27e9 \u27e9 \u2261 \u27e82^ (x + y) * z \u27e9\n2^-+ zero y z = \u2261.refl\n2^-+ (suc x) y z = \u2261.cong 2*_ (2^-+ x y z)\n\n2*\u2032-inj : \u2200 {m n} \u2192 \u27e6\u2115\u27e7 (2*\u2032 m) (2*\u2032 n) \u2192 \u27e6\u2115\u27e7 m n\n2*\u2032-inj {zero} {zero} _ = zero\n2*\u2032-inj {zero} {suc _} ()\n2*\u2032-inj {suc _} {zero} ()\n2*\u2032-inj {suc m} {suc n} (suc (suc p)) = suc (2*\u2032-inj p)\n\n2*-inj : \u2200 {m n} \u2192 2* m \u2261 2* n \u2192 m \u2261 n\n2*-inj {m} {n} p rewrite \u2261.sym (2*\u2032-spec m)\n | \u2261.sym (2*\u2032-spec n)\n = \u27e6\u2115\u27e7\u21d2\u2261 (2*\u2032-inj (\u27e6\u2115\u27e7\u02e2.reflexive p))\n\n2^-inj : \u2200 k {m n} \u2192 \u27e82^ k * m \u27e9 \u2261 \u27e82^ k * n \u27e9 \u2192 m \u2261 n\n2^-inj zero = id\n2^-inj (suc k) = 2^-inj k \u2218 2*-inj\n\n2\u207f*0\u22610 : \u2200 n \u2192 \u27e82^ n * 0 \u27e9 \u2261 0\n2\u207f*0\u22610 zero = \u2261.refl\n2\u207f*0\u22610 (suc n) = \u2261.cong\u2082 _+_ (2\u207f*0\u22610 n) (2\u207f*0\u22610 n)\n\n0\u2238_\u22610 : \u2200 x \u2192 0 \u2238 x \u2261 0\n0\u2238 zero \u22610 = \u2261.refl\n0\u2238 suc x \u22610 = \u2261.refl\n\n2*-\u2238 : \u2200 x y \u2192 2* x \u2238 2* y \u2261 2* (x \u2238 y)\n2*-\u2238 _ zero = \u2261.refl\n2*-\u2238 zero (suc _) = \u2261.refl\n2*-\u2238 (suc x) (suc y) rewrite \u2261.sym (2*-\u2238 x y) | \u2115\u00b0.+-comm x (1 + x) | \u2115\u00b0.+-comm y (1 + y) = \u2261.refl\n\nn+k\u2238m : \u2200 {m n} k \u2192 m \u2264 n \u2192 n + k \u2238 m \u2261 (n \u2238 m) + k\nn+k\u2238m k z\u2264n = \u2261.refl\nn+k\u2238m k (s\u2264s m\u2264n) = n+k\u2238m k m\u2264n\n\nfactor-+-\u2238 : \u2200 {b x y} \u2192 x \u2264 b \u2192 y \u2264 b \u2192 (b \u2238 x) + (b \u2238 y) \u2261 2* b \u2238 (x + y)\nfactor-+-\u2238 {b} {zero} {y} z\u2264n y\u2264b rewrite \u2115\u00b0.+-comm b (b \u2238 y) = \u2261.sym (n+k\u2238m b y\u2264b)\nfactor-+-\u2238 (s\u2264s {x} {b} x\u2264b) z\u2264n rewrite \u2115\u00b0.+-comm x 0 = \u2261.sym (n+k\u2238m (suc b) x\u2264b)\nfactor-+-\u2238 (s\u2264s {x} {b} x\u2264b) (s\u2264s {y} y\u2264b) rewrite factor-+-\u2238 x\u2264b y\u2264b\n | \u2115\u00b0.+-comm b (suc b)\n | \u2115\u00b0.+-comm x (suc y)\n | n+k\u2238m (suc y) x\u2264b\n | \u2115\u00b0.+-comm x y = \u2261.refl\n\n\u2264\u2192\u22621+ : \u2200 {x y} \u2192 x \u2264 y \u2192 x \u2262 suc y\n\u2264\u2192\u22621+ z\u2264n ()\n\u2264\u2192\u22621+ (s\u2264s p) q = \u2264\u2192\u22621+ p (suc-injective q)\n\n<\u2192\u2262 : \u2200 {x y} \u2192 x < y \u2192 x \u2262 y\n<\u2192\u2262 (s\u2264s p) = \u2264\u2192\u22621+ p\n\n{-\npost--ulate\n dist-sum : \u2200 x y z \u2192 dist x y + dist y z \u2264 dist x z\n dist-\u2264 : \u2200 x y \u2192 dist x y \u2264 x\n dist-mono\u2081 : \u2200 x y z t \u2192 x \u2264 y \u2192 dist z t \u2264 dist (x + z) (y + t)\n-}\n\n-- Haskell\n-- let dist x y = abs (x - y)\n-- quickCheck (forAll (replicateM 3 (choose (0,100))) (\\ [x,y,z] -> dist (x * y) (x * z) == x * dist y z))\n\ninfix 8 _^_\n_^_ : \u2115 \u2192 \u2115 \u2192 \u2115\nb ^ zero = 1\nb ^ suc n = b * b ^ n\n\n2^_ : \u2115 \u2192 \u2115\n2^ n = \u27e82^ n * 1 \u27e9\n\n2^-spec : \u2200 n \u2192 2^ n \u2261 2 ^ n\n2^-spec zero = \u2261.refl\n2^-spec (suc n) rewrite 2^-spec n | 2*-spec (2 ^ n) = \u2261.refl\n\n1\u22642^ : \u2200 n \u2192 2^ n \u2265 1\n1+\u22642^ : \u2200 n \u2192 2^ n \u2265 1 + n\n1+\u22642^ zero = s\u2264s z\u2264n\n1+\u22642^ (suc n) = (1\u22642^ n) +-mono (1+\u22642^ n)\n\n1\u22642^ n = \u2115\u2264.trans (s\u2264s z\u2264n) (1+\u22642^ n)\n\n-- https:\/\/en.wikipedia.org\/wiki\/Hyper_operator\n_\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\na \u2191\u27e8 suc n \u27e9 (suc b) = a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\na \u2191\u27e8 0 \u27e9 b = suc b\na \u2191\u27e8 1 \u27e9 0 = a\na \u2191\u27e8 2 \u27e9 0 = 0\na \u2191\u27e8 suc (suc (suc n)) \u27e9 0 = 1\n\nmodule \u2191-Props where\n \u2191-fold : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 b \u2261 fold (a \u2191\u27e8 suc n \u27e9 0) (_\u2191\u27e8_\u27e9_ a n) b\n \u2191-fold a n zero = \u2261.refl\n \u2191-fold a n (suc b) = \u2261.cong (_\u2191\u27e8_\u27e9_ a n) (\u2191-fold a n b)\n\n \u2191\u2080-+ : \u2200 a b \u2192 a \u2191\u27e8 0 \u27e9 b \u2261 1 + b\n \u2191\u2080-+ a b = \u2261.refl\n\n \u2191\u2081-+ : \u2200 a b \u2192 a \u2191\u27e8 1 \u27e9 b \u2261 b + a\n \u2191\u2081-+ a zero = \u2261.refl\n \u2191\u2081-+ a (suc b) = \u2261.cong suc (\u2191\u2081-+ a b)\n\n \u2191\u2082-* : \u2200 a b \u2192 a \u2191\u27e8 2 \u27e9 b \u2261 b * a\n \u2191\u2082-* a zero = \u2261.refl\n \u2191\u2082-* a (suc b) rewrite \u2191\u2082-* a b\n | \u2191\u2081-+ a (b * a)\n | \u2115\u00b0.+-comm (b * a) a\n = \u2261.refl\n\n -- fold 1 (_*_ a) b \u2261 a ^ b\n \u2191\u2083-^ : \u2200 a b \u2192 a \u2191\u27e8 3 \u27e9 b \u2261 a ^ b\n \u2191\u2083-^ a zero = \u2261.refl\n \u2191\u2083-^ a (suc b) rewrite \u2191\u2083-^ a b\n | \u2191\u2082-* a (a ^ b)\n | \u2115\u00b0.*-comm (a ^ b) a\n = \u2261.refl\n\n _\u2191\u27e8_\u27e90 : \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u27e8 0 \u27e90 = 1\n a \u2191\u27e8 1 \u27e90 = a\n a \u2191\u27e8 2 \u27e90 = 0\n a \u2191\u27e8 suc (suc (suc n)) \u27e90 = 1\n\n _`\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _`\u2191\u27e8_\u27e9_ a 0 = suc\n _`\u2191\u27e8_\u27e9_ a (suc n) = fold (a \u2191\u27e8 suc n \u27e90) (_`\u2191\u27e8_\u27e9_ a n)\n\n \u2191-ind-rule : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 (suc b) \u2261 a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\n \u2191-ind-rule a n b = \u2261.refl\n\n _\u2191\u2032\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u2032\u27e8 0 \u27e9 b = suc b\n a \u2191\u2032\u27e8 1 \u27e9 b = a + b\n a \u2191\u2032\u27e8 2 \u27e9 b = a * b\n a \u2191\u2032\u27e8 3 \u27e9 b = a ^ b\n a \u2191\u2032\u27e8 suc (suc (suc (suc n))) \u27e9 b = a \u2191\u27e8 4 + n \u27e9 b\n\n \u2191-\u2191\u2032 : \u2200 a n b \u2192 a \u2191\u27e8 n \u27e9 b \u2261 a \u2191\u2032\u27e8 n \u27e9 b\n \u2191-\u2191\u2032 a 0 b = \u2261.refl\n \u2191-\u2191\u2032 a 1 b = \u2261.trans (\u2191\u2081-+ a b) (\u2115\u00b0.+-comm b a)\n \u2191-\u2191\u2032 a 2 b = \u2261.trans (\u2191\u2082-* a b) (\u2115\u00b0.*-comm b a)\n \u2191-\u2191\u2032 a 3 b = \u2191\u2083-^ a b\n \u2191-\u2191\u2032 a (suc (suc (suc (suc _)))) b = \u2261.refl\n\n _\u21912+\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _\u21912+\u27e8_\u27e9_ a = fold (_*_ a) (fold 1)\n\nmodule InflModule where\n -- Inflationary functions\n Infl< : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl< f = \u2200 {x} \u2192 x < f x\n\n Infl : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl f = Infl< (suc \u2218 f)\n\n InflT< : (f : Endo (Endo \u2115)) \u2192 Set\n InflT< f = \u2200 {g} \u2192 Infl< g \u2192 Infl< (f g)\n\n{-\n\u2200 {x} \u2192 x \u2264 f x\n\n\n 1 + x \u2264 1 + f x\n x < 1 + f x\n x < (suc \u2218 f) x\n Infl< (suc \u2218 f)\n\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n\nInfl< f \u2192 Infl f\nInfl< f \u2192 Infl< (suc \u2218 f)\n-}\n\n id-infl : Infl id\n id-infl = \u2115\u2264.refl\n\n \u2218-infl< : \u2200 {f g} \u2192 Infl< f \u2192 Infl< g \u2192 Infl< (f \u2218 g)\n \u2218-infl< inflf< inflg< = <-trans inflg< inflf<\n\n suc-infl< : Infl< suc\n suc-infl< = \u2115\u2264.refl\n\n suc-infl : Infl suc\n suc-infl = s\u2264s (\u2264-step \u2115\u2264.refl)\n\n nest-infl : \u2200 f (finfl : Infl f) n \u2192 Infl (nest n f)\n nest-infl f _ zero = \u2115\u2264.refl\n nest-infl f finfl (suc n) = \u2115\u2264.trans (nest-infl f finfl n) finfl\n\n nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n nest-infl< f finfl< zero = finfl<\n nest-infl< f finfl< (suc n) = <-trans (nest-infl< f finfl< n) finfl< \n\n-- See Function.NP.nest-Properties for more properties\nmodule fold-Props where\n\n fold-suc : \u2200 m n \u2192 fold m suc n \u2261 n + m\n fold-suc m zero = \u2261.refl\n fold-suc m (suc n) rewrite fold-suc m n = \u2261.refl\n\n fold-+ : \u2200 m n \u2192 fold 0 (_+_ m) n \u2261 n * m\n fold-+ m zero = \u2261.refl\n fold-+ m (suc n) rewrite fold-+ m n = \u2261.refl\n\n fold-* : \u2200 m n \u2192 fold 1 (_*_ m) n \u2261 m ^ n\n fold-* m zero = \u2261.refl\n fold-* m (suc n) rewrite fold-* m n = \u2261.refl\n\n{- TODO\n fold-^ : \u2200 m n \u2192 fold 1 (flip _^_ m) n \u2261 m \u2191\u27e8 4 \u27e9 n\n fold-^ m zero = \u2261.refl\n fold-^ m (suc n) rewrite fold-^ m n = ?\n-}\n\n cong-fold : \u2200 {A : Set} {f g : Endo A} (f\u2257g : f \u2257 g) {z} \u2192 fold z f \u2257 fold z g\n cong-fold eq zero = \u2261.refl\n cong-fold eq {z} (suc x) rewrite cong-fold eq {z} x = eq _\n\n private\n open \u2191-Props\n module Alt\u21912 where\n alt : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n alt a 0 = \u03bb b \u2192 a \u2191\u27e8 2 \u27e9 b\n alt a (suc n) = fold 1 (alt a n)\n\n alt-ok : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 alt a n\n alt-ok a zero = \u2261.sym \u2218 \u2191-\u2191\u2032 a 2\n alt-ok a (suc n) = cong-fold (alt-ok a n)\n\n{- TODO\n alt' : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 _\u2191\u27e8_\u27e9_ a (2 + n)\n alt' a zero b = \u2261.sym (\u2191-\u2191\u2032 a 2 _)\n alt' a (suc n) zero = \u2261.refl\n alt' a (suc n) (suc x) rewrite alt' a n (_\u21912+\u27e8_\u27e9_ a (suc n) x)\n = \u2261.cong (_\u2191\u27e8_\u27e9_ a (2 + n)) { fold 1 (fold (_*_ a) (fold 1) n) x} {a \u2191\u27e8 suc (suc (suc n)) \u27e9 x} (alt' a (suc n) x)\n-}\n\n open InflModule\n{-\n fold1-infl : \u2200 f \u2192 Infl f \u2192 Infl (fold 1 f)\n fold1-infl f finfl {zero} = {!z\u2264n!}\n fold1-infl f finfl {suc x} =\n x <\u27e8 {!s\u2264s (fold1-infl f finfl {x})!} \u27e9\n fold 1 f x <\u27e8 {!!} \u27e9\n {- f (fold 1 f x) \u2264\u27e8 finfl \u27e9\n f (fold 1 f x) \u2261\u27e8 \u2261.refl \u27e9 -}\n fold 1 f (1 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 f \u2218 suc)\n fold1+1-infl< f finfl< {zero} = finfl<\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + f (fold 1 f x) \u2264\u27e8 finfl< {f (fold 1 f x)} \u27e9\n f (suc (f (fold 1 f x))) \u2264\u27e8 {!!} \u27e9\n fold 1 f (2 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 (f \u2218 suc))\n fold1+1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 suc) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 suc) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+-infl< : \u2200 k f \u2192 Infl< (f \u2218\u2032 _+_ k) \u2192 Infl< (fold 1 (f \u2218\u2032 _+_ k))\n fold1+-infl< k f finfl< {zero} = s\u2264s z\u2264n\n fold1+-infl< k f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-infl< k f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 _+_ k) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 _+_ k) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n \n{-\n fold1+-infl< : \u2200 f \u2192 Infl+2< f \u2192 Infl+2< (fold 1 f)\n fold1+-infl< f finfl< {zero} _ = \u2115\u2264.refl\n fold1+-infl< f finfl< {suc zero} _ = {!!}\n fold1+-infl< f finfl< {suc (suc zero)} _ = {!!}\n fold1+-infl< f finfl< {suc (suc (suc x))} x>1 =\n 4 + x \u2264\u27e8 s\u2264s (fold1+-infl< f finfl< {suc (suc x)} (m\u2264m+n 2 x)) \u27e9\n 1 + fold 1 f (2 + x) \u2264\u27e8 finfl< {!!} \u27e9\n fold 1 f (3 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 f x \u2264\u27e8 finfl< \u27e9\n fold 1 f (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\nmodule ^-Props where\n open \u2261-Reasoning\n\n ^1 : \u2200 n \u2192 n ^ 1 \u2261 n\n ^1 zero = \u2261.refl\n ^1 (suc n) = \u2261.cong suc (proj\u2082 \u2115\u00b0.*-identity n)\n\n ^-+ : \u2200 b m n \u2192 b ^ (m + n) \u2261 b ^ m * b ^ n\n ^-+ b zero n = \u2261.sym (proj\u2082 \u2115\u00b0.+-identity (b ^ n))\n ^-+ b (suc m) n rewrite ^-+ b m n = \u2261.sym (\u2115\u00b0.*-assoc b (b ^ m) (b ^ n))\n\n ^-* : \u2200 b m n \u2192 b ^ (m * n) \u2261 (b ^ n) ^ m\n ^-* b zero n = \u2261.refl\n ^-* b (suc m) n = b ^ (n + m * n)\n \u2261\u27e8 ^-+ b n (m * n) \u27e9\n b ^ n * b ^ (m * n)\n \u2261\u27e8 \u2261.cong (_*_ (b ^ n)) (^-* b m n) \u27e9\n b ^ n * (b ^ n) ^ m \u220e\n\nack : \u2115 \u2192 \u2115 \u2192 \u2115\nack zero n = suc n\nack (suc m) zero = ack m (suc zero)\nack (suc m) (suc n) = ack m (ack (suc m) n)\n\n\u2264\u21d2\u2203 : \u2200 {m n} \u2192 m \u2264 n \u2192 \u2203 \u03bb k \u2192 m + k \u2261 n\n\u2264\u21d2\u2203 z\u2264n = _ , \u2261.refl\n\u2264\u21d2\u2203 (s\u2264s pf) = _ , \u2261.cong suc (proj\u2082 (\u2264\u21d2\u2203 pf))\n\nmodule ack-Props where\n lem\u2238 : \u2200 {m n} \u2192 m \u2264 n \u2192 m + (n \u2238 m) \u2261 n\n lem\u2238 z\u2264n = \u2261.refl\n lem\u2238 (s\u2264s {m} {n} m\u2264n) = \u2261.cong suc (lem\u2238 m\u2264n)\n\n -- n >= m \u2192 m + (n \u2238 m) \u2261 n\n -- n >= m \u2192 \u2203 k \u2192 n \u2261 m + k\n -- \u2203 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 m + ((m + k) \u2238 m) \u2261 m + k\n -- \u2200 k \u2192 m + k \u2261 m + k\n\n -- 3 \u2264 x \u2192 P x \u2192 \u2203 \u03bb k \u2192 P (3 + k)\n\n open InflModule\n\n fold1+-inflT< : \u2200 {z} \u2192 InflT< (fold (suc z))\n fold1+-inflT< _ {zero} = s\u2264s z\u2264n\n fold1+-inflT< {z} {f} finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-inflT< {z} {f} finfl< {x}) \u27e9\n 1 + fold (suc z) f x \u2264\u27e8 finfl< \u27e9\n f (fold (suc z) f x) \u2261\u27e8 \u2261.refl \u27e9\n fold (suc z) f (1 + x)\n \u220e\n where open \u2264-Reasoning\n\n Mon : (f : \u2115 \u2192 \u2115) \u2192 Set\n Mon f = \u2200 {x y} \u2192 x \u2264 y \u2192 f x \u2264 f y\n open InflModule\n\n\n \u2115-ind : \u2200 (P : \u2115 \u2192 Set) \u2192 P zero \u2192 (\u2200 {n} \u2192 P n \u2192 P (suc n)) \u2192 \u2200 {n} \u2192 P n\n \u2115-ind P P0 PS {zero} = P0\n \u2115-ind P P0 PS {suc n} = PS (\u2115-ind P P0 PS)\n\n open fold-Props\n fold-infl< : \u2200 {f g} \u2192 Infl< f \u2192 InflT< g \u2192 \u2200 {n} \u2192 Infl< (fold f g n)\n fold-infl< {f} {g} inflf< inflTg< {n} = \u2115-ind (Infl< \u2218 fold f g) inflf< inflTg< {n}\n\n fold-mon : \u2200 {f} (fmon : Mon f) (finfl< : Infl< f) {z} \u2192 Mon (fold z f)\n fold-mon fmon finfl< {_} {y = 0} z\u2264n = \u2115\u2264.refl\n fold-mon {f} fmon finfl< {z} {y = suc n} z\u2264n = z \u2264\u27e8 \u2264-step \u2115\u2264.refl \u27e9\n suc z \u2264\u27e8 nest-infl< f finfl< n {z} \u27e9\n fold z f (suc n) \u2261\u27e8 \u2261.refl \u27e9\n fold z f (suc n) \u220e\n where open \u2264-Reasoning\n fold-mon fmon finfl< (s\u2264s m\u2264n) = fmon (fold-mon fmon finfl< m\u2264n)\n\n MonT : Endo (Endo \u2115) \u2192 Set\n MonT g = \u2200 {f} \u2192 Mon f \u2192 Infl< f \u2192 Mon (g f)\n\n fold-mon' : \u2200 {f g} \u2192 Mon f \u2192 Infl< f \u2192 MonT g \u2192 InflT< g \u2192 \u2200 {n} \u2192 Mon (fold f g n)\n fold-mon' {f} {g} mon-f infl-f mont-g inflTg< {n} = go n where\n go : \u2200 n \u2192 Mon (fold f g n)\n go zero = mon-f\n go (suc n) = mont-g (go n) (fold-infl< infl-f inflTg< {n})\n\n\n -- +-fold : \u2200 a b \u2192 a + b \u2261 fold a suc b\n -- *-fold : \u2200 a b \u2192 a * b \u2261 fold 0 (_+_ a) b\n\n 1\u22641+a^b : \u2200 a b \u2192 1 \u2264 (1 + a) ^ b\n 1\u22641+a^b a zero = s\u2264s z\u2264n\n 1\u22641+a^b a (suc b) = 1\u22641+a^b a b +-mono z\u2264n\n\n 1+a^-mon : \u2200 {a} \u2192 Mon (_^_ (1 + a))\n 1+a^-mon {a} (z\u2264n {b}) = 1\u22641+a^b a b\n 1+a^-mon {a} (s\u2264s {m} {n} m\u2264n)\n = (1 + a) ^ (1 + m) \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ m + a * (1 + a) ^ m \u2264\u27e8 (1+a^-mon m\u2264n) +-mono ((a \u220e) *-mono (1+a^-mon m\u2264n)) \u27e9\n (1 + a) ^ n + a * (1 + a) ^ n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ (1 + n) \u220e\n where open \u2264-Reasoning\n\n postulate\n \u21913+ : \u2200 a n b \u2192 a \u2191\u27e8 3 + n \u27e9 b \u2261 fold (_^_ a) (fold 1) n b\n -- mon\u21913+'' : \u2200 a b \u2192 Mon (\u03bb n \u2192 fold (_^_ a) (fold 1) n b)\n\n mon\u21913+' : \u2200 b \u2192 Mon (\u03bb n \u2192 fold (_^_ 2) (fold 1) n (3 + b))\n -- mon\u21913+' b = {!!}\n\n mon\u21913+ : \u2200 b \u2192 Mon (\u03bb n \u2192 2 \u2191\u27e8 3 + n \u27e9 (3 + b))\n mon\u21913+ b {m} {n} rewrite \u21913+ 2 m (3 + b) | \u21913+ 2 n (3 + b) = mon\u21913+' b\n\n lem2^3 : \u2200 n \u2192 2 ^ 3 \u2264 2 ^ (3 + n)\n lem2^3 n = 1+a^-mon {1} {3} {3 + n} (s\u2264s (s\u2264s (s\u2264s z\u2264n)))\n\n open \u2191-Props\n lem>=3 : \u2200 m n \u2192 3 \u2264 2 \u2191\u27e8 3 + m \u27e9 (3 + n)\n lem>=3 m n = 3 \u2264\u27e8 s\u2264s (s\u2264s (s\u2264s z\u2264n)) \u27e9\n 2 \u2191\u27e8 3 \u27e9 3 \u2261\u27e8 \u2191\u2083-^ 2 3 \u27e9\n 2 ^ 3 \u2264\u27e8 lem2^3 n \u27e9\n 2 ^ (3 + n) \u2261\u27e8 \u2261.sym (\u2191\u2083-^ 2 (3 + n)) \u27e9\n 2 \u2191\u27e8 3 \u27e9 (3 + n) \u2264\u27e8 mon\u21913+ n z\u2264n \u27e9\n 2 \u2191\u27e8 3 + m \u27e9 (3 + n) \u220e\n where open \u2264-Reasoning\n\n 2* = _*_ 2\n\n -- fold 2* n 1 \u2261 2 ^ n\n\n -- nest-* : \u2200 m n \u2192 nest (m * n) f \u2257 nest m (nest n f)\n\n nest-2* : \u2200 m n \u2192 nest (m * n) (fold 1) 2* \u2261 nest m (nest n (fold 1)) 2*\n nest-2* m n = nest-Properties.nest-* (fold 1) m n 2*\n\n{-\n lem3\u2264 : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 2\n lem3\u2264 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264 (suc n) = \n 3 \u2264\u27e8 {!!} \u27e9\n fold 2* f1 (1 + n) 2 \u220e\n where open \u2264-Reasoning\n f1 = fold 1\n\n lem3\u2264' : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 3\n lem3\u2264' 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264' (suc n) = \n 3 \u2264\u27e8 lem3\u2264 n \u27e9\n fold 2* (fold 1) n 2 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) n (fold 2* (fold 1) 3 1) \u2261\u27e8 {!!} \u27e9\n{- \u2261\u27e8 nest-Properties.nest-* {!2*!} {!!} {!!} {!!} \u27e9\n fold 2* (fold 1) (3 * n) 1 \u2261\u27e8 {!!} \u27e9\n-}\n fold 1 (fold 2* (fold 1) n) 3 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) (1 + n) 3 \u220e\n where open \u2264-Reasoning\n-}\n---\n\n{-\nfold 2* (fold 1) (3 * n) 1 \u2264\n\nfold 2* (fold 1) n (fold 2* (fold 1) n (fold 2* (fold 1) n 1)) \u2264\n\nfold 1 (fold 2* (fold 1) n) 3 \u2264\nfold 2* (fold 1) (1 + n) 3\n-}\n\n lem>=3'' : \u2200 m n \u2192 3 \u2264 2 \u2191\u27e8 suc m \u27e9 (3 + n)\n lem>=3'' zero n = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n lem>=3'' (suc zero) n rewrite \u2191\u2082-* 2 (3 + n) = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n lem>=3'' (suc (suc m)) n = lem>=3 m n\n\n lem2221 : \u2200 m \u2192 2 \u2261 2 \u2191\u27e8 2 + m \u27e9 1\n lem2221 zero = \u2261.refl\n lem2221 (suc m) = lem2221 m\n\n lem4212 : \u2200 m \u2192 4 \u2261 2 \u2191\u27e8 1 + m \u27e9 2\n lem4212 zero = \u2261.refl\n lem4212 (suc m) = 4 \u2261\u27e8 lem4212 m \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 2 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 (1 + m)) (lem2221 m) \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 (2 \u2191\u27e8 2 + m \u27e9 1) \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 2 + m \u27e9 2 \u220e\n where open \u2261-Reasoning\n\n ack-\u2191 : \u2200 m n \u2192 3 + ack m n \u2261 2 \u2191\u27e8 m \u27e9 (3 + n)\n ack-\u2191 zero n = \u2261.refl\n ack-\u2191 (suc m) zero = 3 + ack (suc m) 0 \u2261\u27e8 ack-\u2191 m 1 \u27e9\n 2 \u2191\u27e8 m \u27e9 4 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 m) (lem4212 m) \u27e9\n 2 \u2191\u27e8 suc m \u27e9 3 \u220e\n where open \u2261-Reasoning\n ack-\u2191 (suc m) (suc n) = 3 + ack (suc m) (suc n)\n \u2261\u27e8 \u2261.refl \u27e9\n 3 + ack m (ack (suc m) n)\n \u2261\u27e8 \u2261.refl \u27e9\n 3 + ack m ((3 + ack (suc m) n) \u2238 3)\n \u2261\u27e8 \u2261.cong (\u03bb x \u2192 3 + ack m (x \u2238 3)) (ack-\u2191 (suc m) n) \u27e9\n 3 + ack m (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3)\n \u2261\u27e8 ack-\u2191 m (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3) \u27e9\n 2 \u2191\u27e8 m \u27e9 (3 + (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3))\n \u2261\u27e8 \u2261.cong (\u03bb x \u2192 2 \u2191\u27e8 m \u27e9 x) (lem\u2238 (lem>=3'' m n)) \u27e9\n 2 \u2191\u27e8 m \u27e9 (2 \u2191\u27e8 suc m \u27e9 (3 + n))\n \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 suc m \u27e9 (4 + n) \u220e \n where open \u2261-Reasoning\n\n postulate\n 1+a^-infl< : \u2200 {a} \u2192 Infl< (_^_ (1 + a))\n\n -- 2+a*1+b-infl< : \u2200 a \u2192 Infl< (\u03bb x \u2192 (2 + a) * (1 + x))\n -- \u2200 a b \u2192 b < (2 + a) * b\n -- fold-a*-fold1 : \u2200 {n a} \u2192 Infl< (_\u21912+\u27e8_\u27e9_ (2 + a) n)\n fold-a^-fold1 : \u2200 {n a} \u2192 Infl< (fold (_^_ (1 + a)) (fold 1) n)\n fold-a^-fold1 {n} = fold-infl< 1+a^-infl< fold1+-inflT< {n}\n\n \u21913+-mon : \u2200 a n \u2192 Mon (fold (_^_ (1 + a)) (fold 1) n)\n \u21913+-mon a n = fold-mon' 1+a^-mon 1+a^-infl< (\u03bb \u03b7\u2081 \u03b7\u2082 \u2192 fold-mon \u03b7\u2081 \u03b7\u2082) fold1+-inflT< {n}\n\n{-\n open \u2191-Props\n\n -*\u27e81+n\u27e9-infl : \u2200 n \u2192 Infl (_*_ (1 + n))\n -*\u27e81+n\u27e9-infl n = {!!}\n\n lem121 : \u2200 a b \u2192 b < (1 + a) \u2191\u27e8 2 \u27e9 (1 + b)\n lem121 a b rewrite \u2191\u2082-* (1 + a) (1 + b)\n = 1 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (1 + a) * (1 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (1 + a) (1 + b) \u27e9\n (1 + b) * (1 + a)\n \u220e\n where open \u2264-Reasoning\n\n -- \u2200 a b \u2192 b < (2 + a) * b\n lem222 : \u2200 a b \u2192 b < (2 + a) \u2191\u27e8 2 \u27e9 (2 + b)\n lem222 a b rewrite \u2191\u2082-* (2 + a) (2 + b)\n = 1 + b\n \u2264\u27e8 suc-infl \u27e9\n 2 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (2 + a) * (2 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (2 + a) (2 + b) \u27e9\n (2 + b) * (2 + a)\n \u220e\n where open \u2264-Reasoning\n-}\n{-\n open fold-Props\n\n module Foo a where\n f = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b\n f-lem : \u2200 n \u2192 f (suc n) \u2257 fold 1 (f n)\n f-lem n x = \u2261.refl\n\n f2 = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n f2-lem : \u2200 n \u2192 f2 (suc n) \u2257 fold 1 (f2 n)\n f2-lem n x = {!!}\n\n -- b < (1 + a) * (1 + b)\n\n -- nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n infl3< : \u2200 a n \u2192 Infl< (\u03bb b \u2192 (1 + a) \u21912+\u27e8 n \u27e9 (1 + b))\n infl3< a zero {b} = lem121 a b\n infl3< a (suc n) {b}\n = 1 + b\n -- fold1-infl< : \u2200 f \u2192 Infl< f \u2192 \u2200 x \u2192 x < fold 1 f x\n \u2264\u27e8 {!fold1f2-infl< {_}!} \u27e9\n fold 1 f1 b\n \u2264\u27e8 {!!} \u27e9\n{-\n \u2264\u27e8 fold1-infl< (f 0) (\u03bb {x} \u2192 {!infl3< a n {x}!}) {{!!}} \u27e9\n {!!}\n \u2264\u27e8 {!!} \u27e9\n-}\n{-\n 1 + b\n (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n \u2264\u27e8 {!nest-infl< (\u03bb b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b) ? b {(2 + a) \u21912+\u27e8 n \u27e9 (2 + b)}!} \u27e9\n fold (f 0 1) (f 0) (1 + b)\n \u2264\u27e8 {!\u2115\u2264.reflexive (\u2261.cong (\u03bb g \u2192 g 1) (nest-Properties.nest-+ (f 0) 1 (1 + b)))!} \u27e9\n-}\n fold 1 (f 0) (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n f 1 (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) \u21912+\u27e8 suc n \u27e9 (1 + b)\n \u220e\n\n where\n f = \u03bb k b \u2192 (1 + a) \u21912+\u27e8 k + n \u27e9 b\n f1 = f 0 \u2218 suc\n\n -- fold1f2-infl< : Infl< (fold 1 f2)\n -- fold1f2-infl< = fold1-infl< f2 {!(\u03bb b \u2192 infl3< a n b ?)!}\n\n fold1f1-infl<' : Infl< (fold 1 f1)\n fold1f1-infl<' = {!fold1+1-infl< (f 0) {!infl3< a n!}!}\n lem : \u2200 x \u2192 f 0 (1 + x) < f 0 (f 0 x)\n lem x = {!!}\n postulate\n finfl : Infl< (f 0)\n open \u2264-Reasoning\n-}\n\n_\u21d1\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\na \u21d1\u27e8 n , k \u27e9 b = fold k f n\n module M\u21d1 where \n f : \u2115 \u2192 \u2115\n f x = a \u2191\u27e8 2 + x \u27e9 b\n\nmodule \u21d1-Props where\n _\u21d1\u2032\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\n a \u21d1\u2032\u27e8 n\u2080 , k \u27e9 b = g n\u2080\n module M\u21d1\u2032 where\n h : \u2115 \u2192 \u2115\n h x = a \u2191\u27e8 2 + x \u27e9 b\n\n g : \u2115 \u2192 \u2115\n g 0 = k\n g (suc n) = h (g n)\n\n g-nest : \u2200 n \u2192 g n \u2261 h $\u27e8 n \u27e9 k\n g-nest zero = \u2261.refl\n g-nest (suc n) rewrite g-nest n = \u2261.refl\n\n \u21d1\u2080 : \u2200 a k b \u2192 a \u21d1\u27e8 0 , k \u27e9 b \u2261 k\n \u21d1\u2080 _ _ _ = \u2261.refl\n\n \u21d1\u2081 : \u2200 a k b \u2192 a \u21d1\u27e8 1 , k \u27e9 b \u2261 a \u2191\u27e8 2 + k \u27e9 b\n \u21d1\u2081 _ _ _ = \u2261.refl\n\n \u21d1-\u21d1\u2032 : \u2200 a n k b \u2192 a \u21d1\u27e8 n , k \u27e9 b \u2261 a \u21d1\u2032\u27e8 n , k \u27e9 b\n \u21d1-\u21d1\u2032 a n k b rewrite M\u21d1\u2032.g-nest a n k b n = \u2261.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Graham%27s_number\nGraham's-number : \u2115\nGraham's-number = 3 \u21d1\u27e8 64 , 4 \u27e9 3\n -- = (f\u2218\u2076\u2074\u2026\u2218f)4\n -- where f x = 3 \u2191\u27e8 2 + x \u27e9 3\n\n{-\nmodule GeneralisedArithmetic {a} {A : Set a} (0# : A) (1+ : A \u2192 A) where\n\n 1# : A\n 1# = 1+ 0#\n\n open Data.Nat.GeneralisedArithmetic 0# 1+ public\n\n exp : (* : A \u2192 A \u2192 A) \u2192 (\u2115 \u2192 A \u2192 A)\n exp _*_ n b = fold 1# (\u03bb s \u2192 b * s) n\n-}\n -- hyperop a n b = fold exp\n\nmodule == where\n _\u2248_ : (m n : \u2115) \u2192 Set\n m \u2248 n = T (m == n)\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {zero} {zero} _ = id\n subst P {suc _} {suc _} p = subst (P \u2218 suc) p\n subst _ {zero} {suc _} ()\n subst _ {suc _} {zero} ()\n\n sound : \u2200 m n \u2192 T (m == n) \u2192 m \u2261 n\n sound m n p = subst (_\u2261_ m) p \u2261.refl\n\n refl : Reflexive _\u2248_\n refl {zero} = _\n refl {suc n} = refl {n}\n\n sym : Symmetric _\u2248_\n sym {m} {n} eq rewrite sound m n eq = refl {n}\n\n trans : Transitive _\u2248_\n trans {m} {n} {o} m\u2248n n\u2248o rewrite sound m n m\u2248n | sound n o n\u2248o = refl {o}\n\n setoid : Setoid _ _\n setoid = record { Carrier = \u2115; _\u2248_ = _\u2248_\n ; isEquivalence =\n record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} } }\n\n open Setoid setoid public hiding (refl; sym; trans; _\u2248_)\n\n{-\ndata _`\u2264?`_\u219d_ : (m n : \u2115) \u2192 Dec (m \u2264 n) \u2192 Set where\n z\u2264?n : \u2200 {n} \u2192 zero `\u2264?` n \u219d yes z\u2264n\n s\u2264?z : \u2200 {m} \u2192 suc m `\u2264?` zero \u219d no \u03bb()\n s\u2264?s-yes : \u2200 {m n m\u2264n} \u2192 m `\u2264?` n \u219d yes m\u2264n \u2192 suc m `\u2264?` suc n \u219d yes (s\u2264s m\u2264n)\n s\u2264?s-no : \u2200 {m n m\u2270n} \u2192 m `\u2264?` n \u219d no m\u2270n \u2192 suc m `\u2264?` suc n \u219d no (m\u2270n \u2218 \u2264-pred)\n\n`\u2264?`-complete : \u2200 m n \u2192 m `\u2264?` n \u219d (m \u2264? n)\n`\u2264?`-complete zero n = z\u2264?n\n`\u2264?`-complete (suc n) zero = {!s\u2264?z!}\n`\u2264?`-complete (suc m) (suc n) with m \u2264? n | `\u2264?`-complete m n\n... | yes q | r = s\u2264?s-yes r\n... | no q | r = s\u2264?s-no {!!}\n-}\n\n_<=_ : (x y : \u2115) \u2192 Bool\nzero <= _ = true\nsuc _ <= zero = false\nsuc m <= suc n = m <= n\n\nmodule <= where\n \u211b : \u2115 \u2192 \u2115 \u2192 Set\n \u211b x y = T (x <= y)\n\n sound : \u2200 m n \u2192 \u211b m n \u2192 m \u2264 n\n sound zero _ _ = z\u2264n\n sound (suc m) (suc n) p = s\u2264s (sound m n p)\n sound (suc m) zero ()\n\n complete : \u2200 {m n} \u2192 m \u2264 n \u2192 \u211b m n\n complete z\u2264n = _\n complete (s\u2264s m\u2264n) = complete m\u2264n\n\n isTotalOrder : IsTotalOrder _\u2261_ \u211b\n isTotalOrder = record { isPartialOrder = isPartialOrder; total = \u03bb x y \u2192 \u228e-map complete complete (\u2115\u2264.total x y) }\n where\n reflexive : \u2200 {i j} \u2192 i \u2261 j \u2192 \u211b i j\n reflexive {i} \u2261.refl = complete (\u2115\u2264.refl {i})\n trans : Transitive \u211b\n trans {x} {y} {z} p q = complete (\u2115\u2264.trans (sound x y p) (sound y z q))\n isPreorder : IsPreorder _\u2261_ \u211b\n isPreorder = record { isEquivalence = \u2261.isEquivalence\n ; reflexive = reflexive\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} }\n antisym : Antisymmetric _\u2261_ \u211b\n antisym {x} {y} p q = \u2115\u2264.antisym (sound x y p) (sound y x q)\n isPartialOrder : IsPartialOrder _\u2261_ \u211b\n isPartialOrder = record { isPreorder = isPreorder; antisym = antisym }\n\n open IsTotalOrder isTotalOrder public\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (Props.\u2264-steps 1 1+n\u2264m)\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Data.Nat.NP where\n\nimport Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat public hiding (module GeneralisedArithmetic; module \u2264-Reasoning; fold)\nopen import Data.Nat.Properties as Props\nopen import Data.Nat.Logical\nopen import Data.Bool\nopen import Data.Product using (proj\u2081; proj\u2082; \u2203; _,_)\nopen import Data.Empty using (\u22a5-elim)\nopen import Function.NP\nopen import Relation.Nullary\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_; _\u2257_; module \u2261-Reasoning)\n\nmodule \u2115\u00b0 = Algebra.CommutativeSemiring Props.commutativeSemiring\nmodule \u2115cmp = StrictTotalOrder Props.strictTotalOrder\nmodule \u2115\u2264 = DecTotalOrder decTotalOrder\nmodule \u2294\u00b0 = Algebra.CommutativeSemiringWithoutOne \u2294-\u2293-0-commutativeSemiringWithoutOne\n\nmodule \u2264-Reasoning where\n open Preorder-Reasoning \u2115\u2264.preorder public renaming (_\u223c\u27e8_\u27e9_ to _\u2264\u27e8_\u27e9_)\n infixr 2 _\u2261\u27e8_\u27e9_\n _\u2261\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x \u2261 y \u2192 y \u2264 z \u2192 x \u2264 z\n _ \u2261\u27e8 \u2261.refl \u27e9 p = p\n infixr 2 _<\u27e8_\u27e9_\n _<\u27e8_\u27e9_ : \u2200 x {y z : \u2115} \u2192 x < y \u2192 y \u2264 z \u2192 x < z\n x <\u27e8 p \u27e9 q = suc x \u2264\u27e8 p \u27e9 q\n\nsuc-injective : \u2200 {n m : \u2115} \u2192 (suc n \u2236 \u2115) \u2261 suc m \u2192 n \u2261 m\nsuc-injective = \u2261.cong pred\n\nfold : \u2200 {a} {A : Set a} \u2192 A \u2192 Endo A \u2192 \u2115 \u2192 A\nfold x f n = nest n f x\n\n2*_ : \u2115 \u2192 \u2115\n2* x = x + x\n\n2*-spec : \u2200 n \u2192 2* n \u2261 2 * n\n2*-spec n rewrite \u2115\u00b0.+-comm n 0 = \u2261.refl\n\n_==_ : (x y : \u2115) \u2192 Bool\nzero == zero = true\nzero == suc _ = false\nsuc _ == zero = false\nsuc m == suc n = m == n\n\n+-assoc-comm : \u2200 x y z \u2192 x + (y + z) \u2261 y + (x + z)\n+-assoc-comm x y z rewrite \u2261.sym (\u2115\u00b0.+-assoc x y z)\n | \u2115\u00b0.+-comm x y\n | \u2115\u00b0.+-assoc y x z = \u2261.refl\n\n+-interchange : Interchange _\u2261_ _+_ _+_\n+-interchange = InterchangeFromAssocCommCong.\u2219-interchange _\u2261_ \u2261.isEquivalence\n _+_ \u2115\u00b0.+-assoc \u2115\u00b0.+-comm (\u03bb z \u2192 \u2261.cong (flip _+_ z))\n\n2*\u2032_ : \u2115 \u2192 \u2115\n2*\u2032_ = fold 0 (suc \u2218\u2032 suc)\n\n2*\u2032-spec : \u2200 n \u2192 2*\u2032 n \u2261 2* n\n2*\u2032-spec zero = \u2261.refl\n2*\u2032-spec (suc n) rewrite 2*\u2032-spec n | +-assoc-comm 1 n n = \u2261.refl\n\ndist : \u2115 \u2192 \u2115 \u2192 \u2115\ndist zero y = y\ndist x zero = x\ndist (suc x) (suc y) = dist x y\n\ndist-refl : \u2200 x \u2192 dist x x \u2261 0\ndist-refl zero = \u2261.refl\ndist-refl (suc x) rewrite dist-refl x = \u2261.refl\n\ndist-0\u2261id : \u2200 x \u2192 dist 0 x \u2261 x\ndist-0\u2261id _ = \u2261.refl\n\ndist-x-x+y\u2261y : \u2200 x y \u2192 dist x (x + y) \u2261 y\ndist-x-x+y\u2261y zero y = \u2261.refl\ndist-x-x+y\u2261y (suc x) y = dist-x-x+y\u2261y x y\n\ndist-sym : \u2200 x y \u2192 dist x y \u2261 dist y x\ndist-sym zero zero = \u2261.refl\ndist-sym zero (suc y) = \u2261.refl\ndist-sym (suc x) zero = \u2261.refl\ndist-sym (suc x) (suc y) = dist-sym x y\n\ndist-x+ : \u2200 x y z \u2192 dist (x + y) (x + z) \u2261 dist y z\ndist-x+ zero y z = \u2261.refl\ndist-x+ (suc x) y z = dist-x+ x y z\n\ndist-2* : \u2200 x y \u2192 dist (2* x) (2* y) \u2261 2* dist x y\ndist-2* zero y = \u2261.refl\ndist-2* (suc x) zero = \u2261.refl\ndist-2* (suc x) (suc y) rewrite +-assoc-comm x 1 x\n | +-assoc-comm y 1 y = dist-2* x y\n\ndist-asym-def : \u2200 {x y} \u2192 x \u2264 y \u2192 x + dist x y \u2261 y\ndist-asym-def z\u2264n = \u2261.refl\ndist-asym-def (s\u2264s pf) = \u2261.cong suc (dist-asym-def pf)\n\ndist-sym-wlog : \u2200 (f : \u2115 \u2192 \u2115) \u2192 (\u2200 x k \u2192 dist (f x) (f (x + k)) \u2261 f k) \u2192 \u2200 x y \u2192 dist (f x) (f y) \u2261 f (dist x y)\ndist-sym-wlog f pf x y with compare x y\ndist-sym-wlog f pf x .(suc (x + k)) | less .x k with pf x (suc k)\n... | q rewrite +-assoc-comm x 1 k | q | \u2261.sym (+-assoc-comm x 1 k) | dist-x-x+y\u2261y x (suc k) = \u2261.refl\ndist-sym-wlog f pf .y y | equal .y with pf y 0\n... | q rewrite \u2115\u00b0.+-comm y 0 | dist-refl y = q\ndist-sym-wlog f pf .(suc (y + k)) y | greater .y k with pf y (suc k)\n... | q rewrite +-assoc-comm 1 y k | dist-sym (y + suc k) y | dist-x-x+y\u2261y y (suc k) | dist-sym (f (y + suc k)) (f y) = q\n\ndist-x* : \u2200 x y z \u2192 dist (x * y) (x * z) \u2261 x * dist y z\ndist-x* x = dist-sym-wlog (_*_ x) pf\n where pf : \u2200 a k \u2192 dist (x * a) (x * (a + k)) \u2261 x * k\n pf a k rewrite proj\u2081 \u2115\u00b0.distrib x a k = dist-x-x+y\u2261y (x * a) _\n\n2^\u27e8_\u27e9* : \u2115 \u2192 \u2115 \u2192 \u2115\n2^\u27e8 n \u27e9* x = fold x 2*_ n\n\n\u27e82^_*_\u27e9 : \u2115 \u2192 \u2115 \u2192 \u2115\n\u27e82^ n * x \u27e9 = 2^\u27e8 n \u27e9* x\n\n2*-distrib : \u2200 x y \u2192 2* x + 2* y \u2261 2* (x + y) \n2*-distrib = solve 2 (\u03bb x y \u2192 2:* x :+ 2:* y := 2:* (x :+ y)) \u2261.refl\n where open SemiringSolver\n 2:* : \u2200 {n} \u2192 Polynomial n \u2192 Polynomial n\n 2:* x = x :+ x\n\n2^*-distrib : \u2200 k x y \u2192 \u27e82^ k * (x + y)\u27e9 \u2261 \u27e82^ k * x \u27e9 + \u27e82^ k * y \u27e9\n2^*-distrib zero x y = \u2261.refl\n2^*-distrib (suc k) x y rewrite 2^*-distrib k x y = \u2261.sym (2*-distrib \u27e82^ k * x \u27e9 \u27e82^ k * y \u27e9)\n\n2^*-2*-comm : \u2200 k x \u2192 \u27e82^ k * 2* x \u27e9 \u2261 2* \u27e82^ k * x \u27e9\n2^*-2*-comm k x = 2^*-distrib k x x\n\ndist-2^* : \u2200 x y z \u2192 dist \u27e82^ x * y \u27e9 \u27e82^ x * z \u27e9 \u2261 \u27e82^ x * dist y z \u27e9\ndist-2^* x = dist-sym-wlog (2^\u27e8 x \u27e9*) pf\n where pf : \u2200 a k \u2192 dist \u27e82^ x * a \u27e9 \u27e82^ x * (a + k) \u27e9 \u2261 \u27e82^ x * k \u27e9\n pf a k rewrite 2^*-distrib x a k = dist-x-x+y\u2261y \u27e82^ x * a \u27e9 \u27e82^ x * k \u27e9\n\n2*-mono : \u2200 {a b} \u2192 a \u2264 b \u2192 2* a \u2264 2* b\n2*-mono pf = pf +-mono pf\n\n2^*-mono : \u2200 k {a b} \u2192 a \u2264 b \u2192 \u27e82^ k * a \u27e9 \u2264 \u27e82^ k * b \u27e9\n2^*-mono zero pf = pf\n2^*-mono (suc k) pf = 2*-mono (2^*-mono k pf)\n\n2*-mono\u2032 : \u2200 {a b} \u2192 2* a \u2264 2* b \u2192 a \u2264 b\n2*-mono\u2032 {zero} pf = z\u2264n\n2*-mono\u2032 {suc a} {zero} ()\n2*-mono\u2032 {suc a} {suc b} pf rewrite +-assoc-comm a 1 a\n | +-assoc-comm b 1 b = s\u2264s (2*-mono\u2032 (\u2264-pred (\u2264-pred pf)))\n\n2^*-mono\u2032 : \u2200 k {a b} \u2192 \u27e82^ k * a \u27e9 \u2264 \u27e82^ k * b \u27e9 \u2192 a \u2264 b\n2^*-mono\u2032 zero = id\n2^*-mono\u2032 (suc k) = 2^*-mono\u2032 k \u2218 2*-mono\u2032\n\n2^-comm : \u2200 x y z \u2192 \u27e82^ x * \u27e82^ y * z \u27e9 \u27e9 \u2261 \u27e82^ y * \u27e82^ x * z \u27e9 \u27e9\n2^-comm zero y z = \u2261.refl\n2^-comm (suc x) y z rewrite 2^-comm x y z = \u2261.sym (2^*-2*-comm y \u27e82^ x * z \u27e9)\n\n2^-+ : \u2200 x y z \u2192 \u27e82^ x * \u27e82^ y * z \u27e9 \u27e9 \u2261 \u27e82^ (x + y) * z \u27e9\n2^-+ zero y z = \u2261.refl\n2^-+ (suc x) y z = \u2261.cong 2*_ (2^-+ x y z)\n\n2*\u2032-inj : \u2200 {m n} \u2192 \u27e6\u2115\u27e7 (2*\u2032 m) (2*\u2032 n) \u2192 \u27e6\u2115\u27e7 m n\n2*\u2032-inj {zero} {zero} _ = zero\n2*\u2032-inj {zero} {suc _} ()\n2*\u2032-inj {suc _} {zero} ()\n2*\u2032-inj {suc m} {suc n} (suc (suc p)) = suc (2*\u2032-inj p)\n\n2*-inj : \u2200 {m n} \u2192 2* m \u2261 2* n \u2192 m \u2261 n\n2*-inj {m} {n} p rewrite \u2261.sym (2*\u2032-spec m)\n | \u2261.sym (2*\u2032-spec n)\n = \u27e6\u2115\u27e7\u21d2\u2261 (2*\u2032-inj (\u27e6\u2115\u27e7\u02e2.reflexive p))\n\n2^-inj : \u2200 k {m n} \u2192 \u27e82^ k * m \u27e9 \u2261 \u27e82^ k * n \u27e9 \u2192 m \u2261 n\n2^-inj zero = id\n2^-inj (suc k) = 2^-inj k \u2218 2*-inj\n\n2\u207f*0\u22610 : \u2200 n \u2192 \u27e82^ n * 0 \u27e9 \u2261 0\n2\u207f*0\u22610 zero = \u2261.refl\n2\u207f*0\u22610 (suc n) = \u2261.cong\u2082 _+_ (2\u207f*0\u22610 n) (2\u207f*0\u22610 n)\n\n0\u2238_\u22610 : \u2200 x \u2192 0 \u2238 x \u2261 0\n0\u2238 zero \u22610 = \u2261.refl\n0\u2238 suc x \u22610 = \u2261.refl\n\n2*-\u2238 : \u2200 x y \u2192 2* x \u2238 2* y \u2261 2* (x \u2238 y)\n2*-\u2238 _ zero = \u2261.refl\n2*-\u2238 zero (suc _) = \u2261.refl\n2*-\u2238 (suc x) (suc y) rewrite \u2261.sym (2*-\u2238 x y) | \u2115\u00b0.+-comm x (1 + x) | \u2115\u00b0.+-comm y (1 + y) = \u2261.refl\n\nn+k\u2238m : \u2200 {m n} k \u2192 m \u2264 n \u2192 n + k \u2238 m \u2261 (n \u2238 m) + k\nn+k\u2238m k z\u2264n = \u2261.refl\nn+k\u2238m k (s\u2264s m\u2264n) = n+k\u2238m k m\u2264n\n\nfactor-+-\u2238 : \u2200 {b x y} \u2192 x \u2264 b \u2192 y \u2264 b \u2192 (b \u2238 x) + (b \u2238 y) \u2261 2* b \u2238 (x + y)\nfactor-+-\u2238 {b} {zero} {y} z\u2264n y\u2264b rewrite \u2115\u00b0.+-comm b (b \u2238 y) = \u2261.sym (n+k\u2238m b y\u2264b)\nfactor-+-\u2238 (s\u2264s {x} {b} x\u2264b) z\u2264n rewrite \u2115\u00b0.+-comm x 0 = \u2261.sym (n+k\u2238m (suc b) x\u2264b)\nfactor-+-\u2238 (s\u2264s {x} {b} x\u2264b) (s\u2264s {y} y\u2264b) rewrite factor-+-\u2238 x\u2264b y\u2264b\n | \u2115\u00b0.+-comm b (suc b)\n | \u2115\u00b0.+-comm x (suc y)\n | n+k\u2238m (suc y) x\u2264b\n | \u2115\u00b0.+-comm x y = \u2261.refl\n\n\u2264\u2192\u22621+ : \u2200 {x y} \u2192 x \u2264 y \u2192 x \u2262 suc y\n\u2264\u2192\u22621+ z\u2264n ()\n\u2264\u2192\u22621+ (s\u2264s p) q = \u2264\u2192\u22621+ p (suc-injective q)\n\n<\u2192\u2262 : \u2200 {x y} \u2192 x < y \u2192 x \u2262 y\n<\u2192\u2262 (s\u2264s p) = \u2264\u2192\u22621+ p\n\n{-\npost--ulate\n dist-sum : \u2200 x y z \u2192 dist x y + dist y z \u2264 dist x z\n dist-\u2264 : \u2200 x y \u2192 dist x y \u2264 x\n dist-mono\u2081 : \u2200 x y z t \u2192 x \u2264 y \u2192 dist z t \u2264 dist (x + z) (y + t)\n-}\n\n-- Haskell\n-- let dist x y = abs (x - y)\n-- quickCheck (forAll (replicateM 3 (choose (0,100))) (\\ [x,y,z] -> dist (x * y) (x * z) == x * dist y z))\n\ninfix 8 _^_\n_^_ : \u2115 \u2192 \u2115 \u2192 \u2115\nb ^ zero = 1\nb ^ suc n = b * b ^ n\n\n2^_ : \u2115 \u2192 \u2115\n2^ n = \u27e82^ n * 1 \u27e9\n\n2^-spec : \u2200 n \u2192 2^ n \u2261 2 ^ n\n2^-spec zero = \u2261.refl\n2^-spec (suc n) rewrite 2^-spec n | 2*-spec (2 ^ n) = \u2261.refl\n\n1\u22642^ : \u2200 n \u2192 2^ n \u2265 1\n1+\u22642^ : \u2200 n \u2192 2^ n \u2265 1 + n\n1+\u22642^ zero = s\u2264s z\u2264n\n1+\u22642^ (suc n) = (1\u22642^ n) +-mono (1+\u22642^ n)\n\n1\u22642^ n = \u2115\u2264.trans (s\u2264s z\u2264n) (1+\u22642^ n)\n\n-- https:\/\/en.wikipedia.org\/wiki\/Hyper_operator\n_\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\na \u2191\u27e8 suc n \u27e9 (suc b) = a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\na \u2191\u27e8 0 \u27e9 b = suc b\na \u2191\u27e8 1 \u27e9 0 = a\na \u2191\u27e8 2 \u27e9 0 = 0\na \u2191\u27e8 suc (suc (suc n)) \u27e9 0 = 1\n\nmodule \u2191-Props where\n \u2191-fold : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 b \u2261 fold (a \u2191\u27e8 suc n \u27e9 0) (_\u2191\u27e8_\u27e9_ a n) b\n \u2191-fold a n zero = \u2261.refl\n \u2191-fold a n (suc b) = \u2261.cong (_\u2191\u27e8_\u27e9_ a n) (\u2191-fold a n b)\n\n \u2191\u2080-+ : \u2200 a b \u2192 a \u2191\u27e8 0 \u27e9 b \u2261 1 + b\n \u2191\u2080-+ a b = \u2261.refl\n\n \u2191\u2081-+ : \u2200 a b \u2192 a \u2191\u27e8 1 \u27e9 b \u2261 b + a\n \u2191\u2081-+ a zero = \u2261.refl\n \u2191\u2081-+ a (suc b) = \u2261.cong suc (\u2191\u2081-+ a b)\n\n \u2191\u2082-* : \u2200 a b \u2192 a \u2191\u27e8 2 \u27e9 b \u2261 b * a\n \u2191\u2082-* a zero = \u2261.refl\n \u2191\u2082-* a (suc b) rewrite \u2191\u2082-* a b\n | \u2191\u2081-+ a (b * a)\n | \u2115\u00b0.+-comm (b * a) a\n = \u2261.refl\n\n -- fold 1 (_*_ a) b \u2261 a ^ b\n \u2191\u2083-^ : \u2200 a b \u2192 a \u2191\u27e8 3 \u27e9 b \u2261 a ^ b\n \u2191\u2083-^ a zero = \u2261.refl\n \u2191\u2083-^ a (suc b) rewrite \u2191\u2083-^ a b\n | \u2191\u2082-* a (a ^ b)\n | \u2115\u00b0.*-comm (a ^ b) a\n = \u2261.refl\n\n _\u2191\u27e8_\u27e90 : \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u27e8 0 \u27e90 = 1\n a \u2191\u27e8 1 \u27e90 = a\n a \u2191\u27e8 2 \u27e90 = 0\n a \u2191\u27e8 suc (suc (suc n)) \u27e90 = 1\n\n _`\u2191\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _`\u2191\u27e8_\u27e9_ a 0 = suc\n _`\u2191\u27e8_\u27e9_ a (suc n) = fold (a \u2191\u27e8 suc n \u27e90) (_`\u2191\u27e8_\u27e9_ a n)\n\n \u2191-ind-rule : \u2200 a n b \u2192 a \u2191\u27e8 suc n \u27e9 (suc b) \u2261 a \u2191\u27e8 n \u27e9 (a \u2191\u27e8 suc n \u27e9 b)\n \u2191-ind-rule a n b = \u2261.refl\n\n _\u2191\u2032\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n a \u2191\u2032\u27e8 0 \u27e9 b = suc b\n a \u2191\u2032\u27e8 1 \u27e9 b = a + b\n a \u2191\u2032\u27e8 2 \u27e9 b = a * b\n a \u2191\u2032\u27e8 3 \u27e9 b = a ^ b\n a \u2191\u2032\u27e8 suc (suc (suc (suc n))) \u27e9 b = a \u2191\u27e8 4 + n \u27e9 b\n\n \u2191-\u2191\u2032 : \u2200 a n b \u2192 a \u2191\u27e8 n \u27e9 b \u2261 a \u2191\u2032\u27e8 n \u27e9 b\n \u2191-\u2191\u2032 a 0 b = \u2261.refl\n \u2191-\u2191\u2032 a 1 b = \u2261.trans (\u2191\u2081-+ a b) (\u2115\u00b0.+-comm b a)\n \u2191-\u2191\u2032 a 2 b = \u2261.trans (\u2191\u2082-* a b) (\u2115\u00b0.*-comm b a)\n \u2191-\u2191\u2032 a 3 b = \u2191\u2083-^ a b\n \u2191-\u2191\u2032 a (suc (suc (suc (suc _)))) b = \u2261.refl\n\n _\u21912+\u27e8_\u27e9_ : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n _\u21912+\u27e8_\u27e9_ a = fold (_*_ a) (fold 1)\n\nmodule InflModule where\n -- Inflationary functions\n Infl< : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl< f = \u2200 {x} \u2192 x < f x\n\n Infl : (f : \u2115 \u2192 \u2115) \u2192 Set\n Infl f = Infl< (suc \u2218 f)\n\n InflT< : (f : Endo (Endo \u2115)) \u2192 Set\n InflT< f = \u2200 {g} \u2192 Infl< g \u2192 Infl< (f g)\n\n{-\n\u2200 {x} \u2192 x \u2264 f x\n\n\n 1 + x \u2264 1 + f x\n x < 1 + f x\n x < (suc \u2218 f) x\n Infl< (suc \u2218 f)\n\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n Infl< f = \u2200 {x} \u2192 1 + x \u2264 f x\n\nInfl< f \u2192 Infl f\nInfl< f \u2192 Infl< (suc \u2218 f)\n-}\n\n id-infl : Infl id\n id-infl = \u2115\u2264.refl\n\n \u2218-infl< : \u2200 {f g} \u2192 Infl< f \u2192 Infl< g \u2192 Infl< (f \u2218 g)\n \u2218-infl< inflf< inflg< = <-trans inflg< inflf<\n\n suc-infl< : Infl< suc\n suc-infl< = \u2115\u2264.refl\n\n suc-infl : Infl suc\n suc-infl = s\u2264s (\u2264-step \u2115\u2264.refl)\n\n nest-infl : \u2200 f (finfl : Infl f) n \u2192 Infl (nest n f)\n nest-infl f _ zero = \u2115\u2264.refl\n nest-infl f finfl (suc n) = \u2115\u2264.trans (nest-infl f finfl n) finfl\n\n nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n nest-infl< f finfl< zero = finfl<\n nest-infl< f finfl< (suc n) = <-trans (nest-infl< f finfl< n) finfl< \n\n-- See Function.NP.nest-Properties for more properties\nmodule fold-Props where\n\n fold-suc : \u2200 m n \u2192 fold m suc n \u2261 n + m\n fold-suc m zero = \u2261.refl\n fold-suc m (suc n) rewrite fold-suc m n = \u2261.refl\n\n fold-+ : \u2200 m n \u2192 fold 0 (_+_ m) n \u2261 n * m\n fold-+ m zero = \u2261.refl\n fold-+ m (suc n) rewrite fold-+ m n = \u2261.refl\n\n fold-* : \u2200 m n \u2192 fold 1 (_*_ m) n \u2261 m ^ n\n fold-* m zero = \u2261.refl\n fold-* m (suc n) rewrite fold-* m n = \u2261.refl\n\n{- TODO\n fold-^ : \u2200 m n \u2192 fold 1 (flip _^_ m) n \u2261 m \u2191\u27e8 4 \u27e9 n\n fold-^ m zero = \u2261.refl\n fold-^ m (suc n) rewrite fold-^ m n = ?\n-}\n\n cong-fold : \u2200 {A : Set} {f g : Endo A} (f\u2257g : f \u2257 g) {z} \u2192 fold z f \u2257 fold z g\n cong-fold eq zero = \u2261.refl\n cong-fold eq {z} (suc x) rewrite cong-fold eq {z} x = eq _\n\n private\n open \u2191-Props\n module Alt\u21912 where\n alt : \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 \u2115\n alt a 0 = \u03bb b \u2192 a \u2191\u27e8 2 \u27e9 b\n alt a (suc n) = fold 1 (alt a n)\n\n alt-ok : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 alt a n\n alt-ok a zero = \u2261.sym \u2218 \u2191-\u2191\u2032 a 2\n alt-ok a (suc n) = cong-fold (alt-ok a n)\n\n{- TODO\n alt' : \u2200 a n \u2192 _\u21912+\u27e8_\u27e9_ a n \u2257 _\u2191\u27e8_\u27e9_ a (2 + n)\n alt' a zero b = \u2261.sym (\u2191-\u2191\u2032 a 2 _)\n alt' a (suc n) zero = \u2261.refl\n alt' a (suc n) (suc x) rewrite alt' a n (_\u21912+\u27e8_\u27e9_ a (suc n) x)\n = \u2261.cong (_\u2191\u27e8_\u27e9_ a (2 + n)) { fold 1 (fold (_*_ a) (fold 1) n) x} {a \u2191\u27e8 suc (suc (suc n)) \u27e9 x} (alt' a (suc n) x)\n-}\n\n open InflModule\n{-\n fold1-infl : \u2200 f \u2192 Infl f \u2192 Infl (fold 1 f)\n fold1-infl f finfl {zero} = {!z\u2264n!}\n fold1-infl f finfl {suc x} =\n x <\u27e8 {!s\u2264s (fold1-infl f finfl {x})!} \u27e9\n fold 1 f x <\u27e8 {!!} \u27e9\n {- f (fold 1 f x) \u2264\u27e8 finfl \u27e9\n f (fold 1 f x) \u2261\u27e8 \u2261.refl \u27e9 -}\n fold 1 f (1 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 f \u2218 suc)\n fold1+1-infl< f finfl< {zero} = finfl<\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + f (fold 1 f x) \u2264\u27e8 finfl< {f (fold 1 f x)} \u27e9\n f (suc (f (fold 1 f x))) \u2264\u27e8 {!!} \u27e9\n fold 1 f (2 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+1-infl< : \u2200 (f : \u2115 \u2192 \u2115) \u2192 Infl< (f \u2218 suc) \u2192 Infl< (fold 1 (f \u2218 suc))\n fold1+1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1+1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 suc) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 suc) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1+-infl< : \u2200 k f \u2192 Infl< (f \u2218\u2032 _+_ k) \u2192 Infl< (fold 1 (f \u2218\u2032 _+_ k))\n fold1+-infl< k f finfl< {zero} = s\u2264s z\u2264n\n fold1+-infl< k f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-infl< k f finfl< {x}) \u27e9\n 1 + fold 1 (f \u2218 _+_ k) x \u2264\u27e8 finfl< \u27e9\n fold 1 (f \u2218 _+_ k) (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n \n{-\n fold1+-infl< : \u2200 f \u2192 Infl+2< f \u2192 Infl+2< (fold 1 f)\n fold1+-infl< f finfl< {zero} _ = \u2115\u2264.refl\n fold1+-infl< f finfl< {suc zero} _ = {!!}\n fold1+-infl< f finfl< {suc (suc zero)} _ = {!!}\n fold1+-infl< f finfl< {suc (suc (suc x))} x>1 =\n 4 + x \u2264\u27e8 s\u2264s (fold1+-infl< f finfl< {suc (suc x)} (m\u2264m+n 2 x)) \u27e9\n 1 + fold 1 f (2 + x) \u2264\u27e8 finfl< {!!} \u27e9\n fold 1 f (3 + x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\n{-\n fold1-infl< f finfl< {zero} = s\u2264s z\u2264n\n fold1-infl< f finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1-infl< f finfl< {x}) \u27e9\n 1 + fold 1 f x \u2264\u27e8 finfl< \u27e9\n fold 1 f (suc x)\n \u220e\n where\n open Props\n open \u2264-Reasoning\n-}\nmodule ^-Props where\n open \u2261-Reasoning\n\n ^1 : \u2200 n \u2192 n ^ 1 \u2261 n\n ^1 zero = \u2261.refl\n ^1 (suc n) = \u2261.cong suc (proj\u2082 \u2115\u00b0.*-identity n)\n\n ^-+ : \u2200 b m n \u2192 b ^ (m + n) \u2261 b ^ m * b ^ n\n ^-+ b zero n = \u2261.sym (proj\u2082 \u2115\u00b0.+-identity (b ^ n))\n ^-+ b (suc m) n rewrite ^-+ b m n = \u2261.sym (\u2115\u00b0.*-assoc b (b ^ m) (b ^ n))\n\n ^-* : \u2200 b m n \u2192 b ^ (m * n) \u2261 (b ^ n) ^ m\n ^-* b zero n = \u2261.refl\n ^-* b (suc m) n = b ^ (n + m * n)\n \u2261\u27e8 ^-+ b n (m * n) \u27e9\n b ^ n * b ^ (m * n)\n \u2261\u27e8 \u2261.cong (_*_ (b ^ n)) (^-* b m n) \u27e9\n b ^ n * (b ^ n) ^ m \u220e\n\nack : \u2115 \u2192 \u2115 \u2192 \u2115\nack zero n = suc n\nack (suc m) zero = ack m (suc zero)\nack (suc m) (suc n) = ack m (ack (suc m) n)\n\n\u2264\u21d2\u2203 : \u2200 {m n} \u2192 m \u2264 n \u2192 \u2203 \u03bb k \u2192 m + k \u2261 n\n\u2264\u21d2\u2203 z\u2264n = _ , \u2261.refl\n\u2264\u21d2\u2203 (s\u2264s pf) = _ , \u2261.cong suc (proj\u2082 (\u2264\u21d2\u2203 pf))\n\nmodule ack-Props where\n lem\u2238 : \u2200 {m n} \u2192 m \u2264 n \u2192 m + (n \u2238 m) \u2261 n\n lem\u2238 z\u2264n = \u2261.refl\n lem\u2238 (s\u2264s {m} {n} m\u2264n) = \u2261.cong suc (lem\u2238 m\u2264n)\n\n -- n >= m \u2192 m + (n \u2238 m) \u2261 n\n -- n >= m \u2192 \u2203 k \u2192 n \u2261 m + k\n -- \u2203 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 n \u2261 m + k \u2192 m + (n \u2238 m) \u2261 n\n -- \u2200 k \u2192 m + ((m + k) \u2238 m) \u2261 m + k\n -- \u2200 k \u2192 m + k \u2261 m + k\n\n -- 3 \u2264 x \u2192 P x \u2192 \u2203 \u03bb k \u2192 P (3 + k)\n\n open InflModule\n\n fold1+-inflT< : \u2200 {z} \u2192 InflT< (fold (suc z))\n fold1+-inflT< _ {zero} = s\u2264s z\u2264n\n fold1+-inflT< {z} {f} finfl< {suc x} =\n 2 + x \u2264\u27e8 s\u2264s (fold1+-inflT< {z} {f} finfl< {x}) \u27e9\n 1 + fold (suc z) f x \u2264\u27e8 finfl< \u27e9\n f (fold (suc z) f x) \u2261\u27e8 \u2261.refl \u27e9\n fold (suc z) f (1 + x)\n \u220e\n where open \u2264-Reasoning\n\n Mon : (f : \u2115 \u2192 \u2115) \u2192 Set\n Mon f = \u2200 {x y} \u2192 x \u2264 y \u2192 f x \u2264 f y\n open InflModule\n\n\n \u2115-ind : \u2200 (P : \u2115 \u2192 Set) \u2192 P zero \u2192 (\u2200 {n} \u2192 P n \u2192 P (suc n)) \u2192 \u2200 {n} \u2192 P n\n \u2115-ind P P0 PS {zero} = P0\n \u2115-ind P P0 PS {suc n} = PS (\u2115-ind P P0 PS)\n\n open fold-Props\n fold-infl< : \u2200 {f g} \u2192 Infl< f \u2192 InflT< g \u2192 \u2200 {n} \u2192 Infl< (fold f g n)\n fold-infl< {f} {g} inflf< inflTg< {n} = \u2115-ind (Infl< \u2218 fold f g) inflf< inflTg< {n}\n\n fold-mon : \u2200 {f} (fmon : Mon f) (finfl< : Infl< f) {z} \u2192 Mon (fold z f)\n fold-mon fmon finfl< {_} {y = 0} z\u2264n = \u2115\u2264.refl\n fold-mon {f} fmon finfl< {z} {y = suc n} z\u2264n = z \u2264\u27e8 \u2264-step \u2115\u2264.refl \u27e9\n suc z \u2264\u27e8 nest-infl< f finfl< n {z} \u27e9\n fold z f (suc n) \u2261\u27e8 \u2261.refl \u27e9\n fold z f (suc n) \u220e\n where open \u2264-Reasoning\n fold-mon fmon finfl< (s\u2264s m\u2264n) = fmon (fold-mon fmon finfl< m\u2264n)\n\n MonT : Endo (Endo \u2115) \u2192 Set\n MonT g = \u2200 {f} \u2192 Mon f \u2192 Infl< f \u2192 Mon (g f)\n\n fold-mon' : \u2200 {f g} \u2192 Mon f \u2192 Infl< f \u2192 MonT g \u2192 InflT< g \u2192 \u2200 {n} \u2192 Mon (fold f g n)\n fold-mon' {f} {g} mon-f infl-f mont-g inflTg< {n} = go n where\n go : \u2200 n \u2192 Mon (fold f g n)\n go zero = mon-f\n go (suc n) = mont-g (go n) (fold-infl< infl-f inflTg< {n})\n\n\n -- +-fold : \u2200 a b \u2192 a + b \u2261 fold a suc b\n -- *-fold : \u2200 a b \u2192 a * b \u2261 fold 0 (_+_ a) b\n\n 1\u22641+a^b : \u2200 a b \u2192 1 \u2264 (1 + a) ^ b\n 1\u22641+a^b a zero = s\u2264s z\u2264n\n 1\u22641+a^b a (suc b) = 1\u22641+a^b a b +-mono z\u2264n\n\n 1+a^-mon : \u2200 {a} \u2192 Mon (_^_ (1 + a))\n 1+a^-mon {a} (z\u2264n {b}) = 1\u22641+a^b a b\n 1+a^-mon {a} (s\u2264s {m} {n} m\u2264n)\n = (1 + a) ^ (1 + m) \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ m + a * (1 + a) ^ m \u2264\u27e8 (1+a^-mon m\u2264n) +-mono ((a \u220e) *-mono (1+a^-mon m\u2264n)) \u27e9\n (1 + a) ^ n + a * (1 + a) ^ n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) ^ (1 + n) \u220e\n where open \u2264-Reasoning\n\n postulate\n \u21913+ : \u2200 a n b \u2192 a \u2191\u27e8 3 + n \u27e9 b \u2261 fold (_^_ a) (fold 1) n b\n -- mon\u21913+'' : \u2200 a b \u2192 Mon (\u03bb n \u2192 fold (_^_ a) (fold 1) n b)\n\n mon\u21913+' : \u2200 b \u2192 Mon (\u03bb n \u2192 fold (_^_ 2) (fold 1) n (3 + b))\n -- mon\u21913+' b = {!!}\n\n mon\u21913+ : \u2200 b \u2192 Mon (\u03bb n \u2192 2 \u2191\u27e8 3 + n \u27e9 (3 + b))\n mon\u21913+ b {m} {n} rewrite \u21913+ 2 m (3 + b) | \u21913+ 2 n (3 + b) = mon\u21913+' b\n\n lem2^3 : \u2200 n \u2192 2 ^ 3 \u2264 2 ^ (3 + n)\n lem2^3 n = 1+a^-mon {1} {3} {3 + n} (s\u2264s (s\u2264s (s\u2264s z\u2264n)))\n\n open \u2191-Props\n lem>=3 : \u2200 m n \u2192 3 \u2264 2 \u2191\u27e8 3 + m \u27e9 (3 + n)\n lem>=3 m n = 3 \u2264\u27e8 s\u2264s (s\u2264s (s\u2264s z\u2264n)) \u27e9\n 2 \u2191\u27e8 3 \u27e9 3 \u2261\u27e8 \u2191\u2083-^ 2 3 \u27e9\n 2 ^ 3 \u2264\u27e8 lem2^3 n \u27e9\n 2 ^ (3 + n) \u2261\u27e8 \u2261.sym (\u2191\u2083-^ 2 (3 + n)) \u27e9\n 2 \u2191\u27e8 3 \u27e9 (3 + n) \u2264\u27e8 mon\u21913+ n z\u2264n \u27e9\n 2 \u2191\u27e8 3 + m \u27e9 (3 + n) \u220e\n where open \u2264-Reasoning\n\n 2* = _*_ 2\n\n -- fold 2* n 1 \u2261 2 ^ n\n\n -- nest-* : \u2200 m n \u2192 nest (m * n) f \u2257 nest m (nest n f)\n\n nest-2* : \u2200 m n \u2192 nest (m * n) (fold 1) 2* \u2261 nest m (nest n (fold 1)) 2*\n nest-2* m n = nest-Properties.nest-* (fold 1) m n 2*\n\n{-\n lem3\u2264 : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 2\n lem3\u2264 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264 (suc n) = \n 3 \u2264\u27e8 {!!} \u27e9\n fold 2* f1 (1 + n) 2 \u220e\n where open \u2264-Reasoning\n f1 = fold 1\n\n lem3\u2264' : \u2200 n \u2192 3 \u2264 fold 2* (fold 1) n 3\n lem3\u2264' 0 = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n where open \u2264-Reasoning\n lem3\u2264' (suc n) = \n 3 \u2264\u27e8 lem3\u2264 n \u27e9\n fold 2* (fold 1) n 2 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) n (fold 2* (fold 1) 3 1) \u2261\u27e8 {!!} \u27e9\n{- \u2261\u27e8 nest-Properties.nest-* {!2*!} {!!} {!!} {!!} \u27e9\n fold 2* (fold 1) (3 * n) 1 \u2261\u27e8 {!!} \u27e9\n-}\n fold 1 (fold 2* (fold 1) n) 3 \u2261\u27e8 \u2261.refl \u27e9\n fold 2* (fold 1) (1 + n) 3 \u220e\n where open \u2264-Reasoning\n-}\n---\n\n{-\nfold 2* (fold 1) (3 * n) 1 \u2264\n\nfold 2* (fold 1) n (fold 2* (fold 1) n (fold 2* (fold 1) n 1)) \u2264\n\nfold 1 (fold 2* (fold 1) n) 3 \u2264\nfold 2* (fold 1) (1 + n) 3\n-}\n\n lem>=3'' : \u2200 m n \u2192 3 \u2264 2 \u2191\u27e8 suc m \u27e9 (3 + n)\n lem>=3'' zero n = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n lem>=3'' (suc zero) n rewrite \u2191\u2082-* 2 (3 + n) = s\u2264s (s\u2264s (s\u2264s z\u2264n))\n lem>=3'' (suc (suc m)) n = lem>=3 m n\n\n lem2221 : \u2200 m \u2192 2 \u2261 2 \u2191\u27e8 2 + m \u27e9 1\n lem2221 zero = \u2261.refl\n lem2221 (suc m) = lem2221 m\n\n lem4212 : \u2200 m \u2192 4 \u2261 2 \u2191\u27e8 1 + m \u27e9 2\n lem4212 zero = \u2261.refl\n lem4212 (suc m) = 4 \u2261\u27e8 lem4212 m \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 2 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 (1 + m)) (lem2221 m) \u27e9\n 2 \u2191\u27e8 1 + m \u27e9 (2 \u2191\u27e8 2 + m \u27e9 1) \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 2 + m \u27e9 2 \u220e\n where open \u2261-Reasoning\n\n ack-\u2191 : \u2200 m n \u2192 3 + ack m n \u2261 2 \u2191\u27e8 m \u27e9 (3 + n)\n ack-\u2191 zero n = \u2261.refl\n ack-\u2191 (suc m) zero = 3 + ack (suc m) 0 \u2261\u27e8 ack-\u2191 m 1 \u27e9\n 2 \u2191\u27e8 m \u27e9 4 \u2261\u27e8 \u2261.cong (_\u2191\u27e8_\u27e9_ 2 m) (lem4212 m) \u27e9\n 2 \u2191\u27e8 suc m \u27e9 3 \u220e\n where open \u2261-Reasoning\n ack-\u2191 (suc m) (suc n) = 3 + ack (suc m) (suc n)\n \u2261\u27e8 \u2261.refl \u27e9\n 3 + ack m (ack (suc m) n)\n \u2261\u27e8 \u2261.refl \u27e9\n 3 + ack m ((3 + ack (suc m) n) \u2238 3)\n \u2261\u27e8 \u2261.cong (\u03bb x \u2192 3 + ack m (x \u2238 3)) (ack-\u2191 (suc m) n) \u27e9\n 3 + ack m (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3)\n \u2261\u27e8 ack-\u2191 m (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3) \u27e9\n 2 \u2191\u27e8 m \u27e9 (3 + (2 \u2191\u27e8 suc m \u27e9 (3 + n) \u2238 3))\n \u2261\u27e8 \u2261.cong (\u03bb x \u2192 2 \u2191\u27e8 m \u27e9 x) (lem\u2238 (lem>=3'' m n)) \u27e9\n 2 \u2191\u27e8 m \u27e9 (2 \u2191\u27e8 suc m \u27e9 (3 + n))\n \u2261\u27e8 \u2261.refl \u27e9\n 2 \u2191\u27e8 suc m \u27e9 (4 + n) \u220e \n where open \u2261-Reasoning\n\n postulate\n 1+a^-infl< : \u2200 {a} \u2192 Infl< (_^_ (1 + a))\n\n -- 2+a*1+b-infl< : \u2200 a \u2192 Infl< (\u03bb x \u2192 (2 + a) * (1 + x))\n -- \u2200 a b \u2192 b < (2 + a) * b\n -- fold-a*-fold1 : \u2200 {n a} \u2192 Infl< (_\u21912+\u27e8_\u27e9_ (2 + a) n)\n fold-a^-fold1 : \u2200 {n a} \u2192 Infl< (fold (_^_ (1 + a)) (fold 1) n)\n fold-a^-fold1 {n} = fold-infl< 1+a^-infl< fold1+-inflT< {n}\n\n \u21913+-mon : \u2200 a n \u2192 Mon (fold (_^_ (1 + a)) (fold 1) n)\n \u21913+-mon a n = fold-mon' 1+a^-mon 1+a^-infl< (\u03bb \u03b7\u2081 \u03b7\u2082 \u2192 fold-mon \u03b7\u2081 \u03b7\u2082) fold1+-inflT< {n}\n\n{-\n open \u2191-Props\n\n -*\u27e81+n\u27e9-infl : \u2200 n \u2192 Infl (_*_ (1 + n))\n -*\u27e81+n\u27e9-infl n = {!!}\n\n lem121 : \u2200 a b \u2192 b < (1 + a) \u2191\u27e8 2 \u27e9 (1 + b)\n lem121 a b rewrite \u2191\u2082-* (1 + a) (1 + b)\n = 1 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (1 + a) * (1 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (1 + a) (1 + b) \u27e9\n (1 + b) * (1 + a)\n \u220e\n where open \u2264-Reasoning\n\n -- \u2200 a b \u2192 b < (2 + a) * b\n lem222 : \u2200 a b \u2192 b < (2 + a) \u2191\u27e8 2 \u27e9 (2 + b)\n lem222 a b rewrite \u2191\u2082-* (2 + a) (2 + b)\n = 1 + b\n \u2264\u27e8 suc-infl \u27e9\n 2 + b\n \u2264\u27e8 m\u2264m+n _ _ \u27e9\n (2 + a) * (2 + b)\n \u2261\u27e8 \u2115\u00b0.*-comm (2 + a) (2 + b) \u27e9\n (2 + b) * (2 + a)\n \u220e\n where open \u2264-Reasoning\n-}\n{-\n open fold-Props\n\n module Foo a where\n f = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b\n f-lem : \u2200 n \u2192 f (suc n) \u2257 fold 1 (f n)\n f-lem n x = \u2261.refl\n\n f2 = \u03bb n b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n f2-lem : \u2200 n \u2192 f2 (suc n) \u2257 fold 1 (f2 n)\n f2-lem n x = {!!}\n\n -- b < (1 + a) * (1 + b)\n\n -- nest-infl< : \u2200 f (finfl< : Infl< f) n \u2192 Infl< (nest (suc n) f)\n infl3< : \u2200 a n \u2192 Infl< (\u03bb b \u2192 (1 + a) \u21912+\u27e8 n \u27e9 (1 + b))\n infl3< a zero {b} = lem121 a b\n infl3< a (suc n) {b}\n = 1 + b\n -- fold1-infl< : \u2200 f \u2192 Infl< f \u2192 \u2200 x \u2192 x < fold 1 f x\n \u2264\u27e8 {!fold1f2-infl< {_}!} \u27e9\n fold 1 f1 b\n \u2264\u27e8 {!!} \u27e9\n{-\n \u2264\u27e8 fold1-infl< (f 0) (\u03bb {x} \u2192 {!infl3< a n {x}!}) {{!!}} \u27e9\n {!!}\n \u2264\u27e8 {!!} \u27e9\n-}\n{-\n 1 + b\n (2 + a) \u21912+\u27e8 n \u27e9 (2 + b)\n \u2264\u27e8 {!nest-infl< (\u03bb b \u2192 (2 + a) \u21912+\u27e8 n \u27e9 b) ? b {(2 + a) \u21912+\u27e8 n \u27e9 (2 + b)}!} \u27e9\n fold (f 0 1) (f 0) (1 + b)\n \u2264\u27e8 {!\u2115\u2264.reflexive (\u2261.cong (\u03bb g \u2192 g 1) (nest-Properties.nest-+ (f 0) 1 (1 + b)))!} \u27e9\n-}\n fold 1 (f 0) (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n f 1 (1 + b)\n \u2261\u27e8 \u2261.refl \u27e9\n (1 + a) \u21912+\u27e8 suc n \u27e9 (1 + b)\n \u220e\n\n where\n f = \u03bb k b \u2192 (1 + a) \u21912+\u27e8 k + n \u27e9 b\n f1 = f 0 \u2218 suc\n\n -- fold1f2-infl< : Infl< (fold 1 f2)\n -- fold1f2-infl< = fold1-infl< f2 {!(\u03bb b \u2192 infl3< a n b ?)!}\n\n fold1f1-infl<' : Infl< (fold 1 f1)\n fold1f1-infl<' = {!fold1+1-infl< (f 0) {!infl3< a n!}!}\n lem : \u2200 x \u2192 f 0 (1 + x) < f 0 (f 0 x)\n lem x = {!!}\n postulate\n finfl : Infl< (f 0)\n open \u2264-Reasoning\n-}\n\n_\u21d1\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\na \u21d1\u27e8 n , k \u27e9 b = fold k f n\n module M\u21d1 where \n f : \u2115 \u2192 \u2115\n f x = a \u2191\u27e8 2 + x \u27e9 b\n\nmodule \u21d1-Props where\n _\u21d1\u2032\u27e8_,_\u27e9_ : (a n k b : \u2115) \u2192 \u2115\n a \u21d1\u2032\u27e8 n\u2080 , k \u27e9 b = g n\u2080\n module M\u21d1\u2032 where\n h : \u2115 \u2192 \u2115\n h x = a \u2191\u27e8 2 + x \u27e9 b\n\n g : \u2115 \u2192 \u2115\n g 0 = k\n g (suc n) = h (g n)\n\n g-nest : \u2200 n \u2192 g n \u2261 h $\u27e8 n \u27e9 k\n g-nest zero = \u2261.refl\n g-nest (suc n) rewrite g-nest n = \u2261.refl\n\n \u21d1\u2080 : \u2200 a k b \u2192 a \u21d1\u27e8 0 , k \u27e9 b \u2261 k\n \u21d1\u2080 _ _ _ = \u2261.refl\n\n \u21d1\u2081 : \u2200 a k b \u2192 a \u21d1\u27e8 1 , k \u27e9 b \u2261 a \u2191\u27e8 2 + k \u27e9 b\n \u21d1\u2081 _ _ _ = \u2261.refl\n\n \u21d1-\u21d1\u2032 : \u2200 a n k b \u2192 a \u21d1\u27e8 n , k \u27e9 b \u2261 a \u21d1\u2032\u27e8 n , k \u27e9 b\n \u21d1-\u21d1\u2032 a n k b rewrite M\u21d1\u2032.g-nest a n k b n = \u2261.refl\n\n-- https:\/\/en.wikipedia.org\/wiki\/Graham%27s_number\nGraham's-number : \u2115\nGraham's-number = 3 \u21d1\u27e8 64 , 4 \u27e9 3\n -- = (f\u2218\u2076\u2074\u2026\u2218f)4\n -- where f x = 3 \u2191\u27e8 2 + x \u27e9 3\n\n{-\nmodule GeneralisedArithmetic {a} {A : Set a} (0# : A) (1+ : A \u2192 A) where\n\n 1# : A\n 1# = 1+ 0#\n\n open Data.Nat.GeneralisedArithmetic 0# 1+ public\n\n exp : (* : A \u2192 A \u2192 A) \u2192 (\u2115 \u2192 A \u2192 A)\n exp _*_ n b = fold 1# (\u03bb s \u2192 b * s) n\n-}\n -- hyperop a n b = fold exp\n\nmodule == where\n _\u2248_ : (m n : \u2115) \u2192 Set\n m \u2248 n = T (m == n)\n\n subst : \u2200 {\u2113} \u2192 Substitutive _\u2248_ \u2113\n subst _ {zero} {zero} _ = id\n subst P {suc _} {suc _} p = subst (P \u2218 suc) p\n subst _ {zero} {suc _} ()\n subst _ {suc _} {zero} ()\n\n sound : \u2200 m n \u2192 T (m == n) \u2192 m \u2261 n\n sound m n p = subst (_\u2261_ m) p \u2261.refl\n\n refl : Reflexive _\u2248_\n refl {zero} = _\n refl {suc n} = refl {n}\n\n sym : Symmetric _\u2248_\n sym {m} {n} eq rewrite sound m n eq = refl {n}\n\n trans : Transitive _\u2248_\n trans {m} {n} {o} m\u2248n n\u2248o rewrite sound m n m\u2248n | sound n o n\u2248o = refl {o}\n\n setoid : Setoid _ _\n setoid = record { Carrier = \u2115; _\u2248_ = _\u2248_\n ; isEquivalence =\n record { refl = \u03bb {x} \u2192 refl {x}\n ; sym = \u03bb {x} {y} \u2192 sym {x} {y}\n ; trans = \u03bb {x} {y} {z} \u2192 trans {x} {y} {z} } }\n\n open Setoid setoid public hiding (refl; sym; trans; _\u2248_)\n\n{-\ndata _`\u2264?`_\u219d_ : (m n : \u2115) \u2192 Dec (m \u2264 n) \u2192 Set where\n z\u2264?n : \u2200 {n} \u2192 zero `\u2264?` n \u219d yes z\u2264n\n s\u2264?z : \u2200 {m} \u2192 suc m `\u2264?` zero \u219d no \u03bb()\n s\u2264?s-yes : \u2200 {m n m\u2264n} \u2192 m `\u2264?` n \u219d yes m\u2264n \u2192 suc m `\u2264?` suc n \u219d yes (s\u2264s m\u2264n)\n s\u2264?s-no : \u2200 {m n m\u2270n} \u2192 m `\u2264?` n \u219d no m\u2270n \u2192 suc m `\u2264?` suc n \u219d no (m\u2270n \u2218 \u2264-pred)\n\n`\u2264?`-complete : \u2200 m n \u2192 m `\u2264?` n \u219d (m \u2264? n)\n`\u2264?`-complete zero n = z\u2264?n\n`\u2264?`-complete (suc n) zero = {!s\u2264?z!}\n`\u2264?`-complete (suc m) (suc n) with m \u2264? n | `\u2264?`-complete m n\n... | yes q | r = s\u2264?s-yes r\n... | no q | r = s\u2264?s-no {!!}\n-}\n\n_<=_ : (x y : \u2115) \u2192 Bool\nzero <= _ = true\nsuc _ <= zero = false\nsuc m <= suc n = m <= n\n\nmodule <= where\n sound : \u2200 m n \u2192 T (m <= n) \u2192 m \u2264 n\n sound zero _ _ = z\u2264n\n sound (suc m) (suc n) p = s\u2264s (sound m n p)\n sound (suc m) zero ()\n\n complete : \u2200 {m n} \u2192 m \u2264 n \u2192 T (m <= n)\n complete z\u2264n = _\n complete (s\u2264s m\u2264n) = complete m\u2264n\n\n\u00ac\u2264 : \u2200 {m n} \u2192 \u00ac(m < n) \u2192 n \u2264 m\n\u00ac\u2264 {m} {n} p with \u2115cmp.compare m n\n... | tri< m _ _ 1+n\u2264m = \u2264-pred (Props.\u2264-steps 1 1+n\u2264m)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"c1ff9f6bce16be7d52bc424c1c012656dec37587","subject":"Implement apply-env and diff-env.","message":"Implement apply-env and diff-env.\n\nOld-commit-hash: 0a4d0485631257cbfb9ee522e8ac6facb88f0721\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Change\/Validity.agda","new_file":"Parametric\/Change\/Validity.agda","new_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Denotation.Value as Value\n\nmodule Parametric.Change.Validity\n {Base : Type.Structure}\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\n\n-- Changes for Calculus Popl14\n--\n-- Contents\n-- - Mutually recursive concepts: \u0394Val, validity.\n-- Under module Syntax, the corresponding concepts of\n-- \u0394Type and \u0394Context reside in separate files.\n-- Now they have to be together due to mutual recursiveness.\n-- - `diff` and `apply` on semantic values of changes:\n-- they have to be here as well because they are mutually\n-- recursive with validity.\n-- - The lemma diff-is-valid: it has to be here because it is\n-- mutually recursive with `apply`\n-- - The lemma apply-diff: it is mutually recursive with `apply`\n-- and `diff`\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\nopen import Data.Product hiding (map)\n\nimport Structure.Tuples as Tuples\nopen Tuples\n\nimport Base.Data.DependentList as DependentList\nopen DependentList\n\nopen import Relation.Unary using (_\u2286_)\n\nrecord Structure : Set\u2081 where\n ----------------\n -- Parameters --\n ----------------\n\n field\n Change-base : (\u03b9 : Base) \u2192 \u27e6 \u03b9 \u27e7Base \u2192 Set\n apply-change-base : \u2200 \u03b9 \u2192 (v : \u27e6 \u03b9 \u27e7Base) \u2192 Change-base \u03b9 v \u2192 \u27e6 \u03b9 \u27e7Base\n diff-change-base : \u2200 \u03b9 \u2192 (u v : \u27e6 \u03b9 \u27e7Base) \u2192 Change-base \u03b9 v\n v+[u-v]=u-base : \u2200 {\u03b9} {u v : \u27e6 \u03b9 \u27e7Base} \u2192 apply-change-base \u03b9 v (diff-change-base \u03b9 u v) \u2261 u\n\n ---------------\n -- Interface --\n ---------------\n\n Change : (\u03c4 : Type) \u2192 \u27e6 \u03c4 \u27e7 \u2192 Set\n\n nil-change : \u2200 \u03c4 v \u2192 Change \u03c4 v\n apply-change : \u2200 \u03c4 \u2192 (v : \u27e6 \u03c4 \u27e7) (dv : Change \u03c4 v) \u2192 \u27e6 \u03c4 \u27e7\n diff-change : \u2200 \u03c4 \u2192 (u v : \u27e6 \u03c4 \u27e7) \u2192 Change \u03c4 v\n\n infixl 6 apply-change diff-change -- as with + - in GHC.Num\n syntax apply-change \u03c4 v dv = v \u229e\u208d \u03c4 \u208e dv\n syntax diff-change \u03c4 u v = u \u229f\u208d \u03c4 \u208e v\n\n -- Lemma apply-diff\n v+[u-v]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192\n v \u229e\u208d \u03c4 \u208e (u \u229f\u208d \u03c4 \u208e v) \u2261 u\n\n --------------------\n -- Implementation --\n --------------------\n\n -- (Change \u03c4) is the set of changes of type \u03c4. This set is\n -- strictly smaller than \u27e6 \u0394Type \u03c4\u27e7 if \u03c4 is a function type. In\n -- particular, (Change (\u03c3 \u21d2 \u03c4)) is a function that accepts only\n -- valid changes, while \u27e6 \u0394Type (\u03c3 \u21d2 \u03c4) \u27e7 accepts also invalid\n -- changes.\n --\n -- Change \u03c4 is the target of the denotational specification \u27e6_\u27e7\u0394.\n -- Detailed motivation:\n --\n -- https:\/\/github.com\/ps-mr\/ilc\/blob\/184a6291ac6eef80871c32d2483e3e62578baf06\/POPL14\/paper\/sec-formal.tex\n\n -- Change : Type \u2192 Set\n Change (base \u03b9) v = Change-base \u03b9 v\n Change (\u03c3 \u21d2 \u03c4) f = Pair\n (\u2200 v \u2192 Change \u03c3 v \u2192 Change \u03c4 (f v))\n (\u03bb \u0394f \u2192 \u2200 v dv \u2192\n f (v \u229e\u208d \u03c3 \u208e dv) \u229e\u208d \u03c4 \u208e \u0394f (v \u229e\u208d \u03c3 \u208e dv) (nil-change \u03c3 (v \u229e\u208d \u03c3 \u208e dv)) \u2261 f v \u229e\u208d \u03c4 \u208e \u0394f v dv)\n\n open Pair public using () renaming\n ( cdr to is-valid\n ; car to call-change\n )\n\n nil-change \u03c4 v = diff-change \u03c4 v v\n\n -- _\u229e_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 Change \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n apply-change (base \u03b9) n \u0394n = apply-change-base \u03b9 n \u0394n\n apply-change (\u03c3 \u21d2 \u03c4) f \u0394f = \u03bb v \u2192 f v \u229e\u208d \u03c4 \u208e call-change \u0394f v (nil-change \u03c3 v)\n\n -- _\u229f_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 Change \u03c4\n diff-change (base \u03b9) m n = diff-change-base \u03b9 m n\n diff-change (\u03c3 \u21d2 \u03c4) g f = cons (\u03bb v dv \u2192 g (v \u229e\u208d \u03c3 \u208e dv) \u229f\u208d \u03c4 \u208e f v)\n (\u03bb v dv \u2192\n begin\n f (v \u229e\u208d \u03c3 \u208e dv) \u229e\u208d \u03c4 \u208e (g ((v \u229e\u208d \u03c3 \u208e dv) \u229e\u208d \u03c3 \u208e ((v \u229e\u208d \u03c3 \u208e dv) \u229f\u208d \u03c3 \u208e (v \u229e\u208d \u03c3 \u208e dv))) \u229f\u208d \u03c4 \u208e f (v \u229e\u208d \u03c3 \u208e dv))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f (v \u229e\u208d \u03c3 \u208e dv) \u229e\u208d \u03c4 \u208e (g \u25a1 \u229f\u208d \u03c4 \u208e (f (v \u229e\u208d \u03c3 \u208e dv))))\n (v+[u-v]=u {\u03c3} {v \u229e\u208d \u03c3 \u208e dv} {v \u229e\u208d \u03c3 \u208e dv}) \u27e9\n f (v \u229e\u208d \u03c3 \u208e dv) \u229e\u208d \u03c4 \u208e (g (v \u229e\u208d \u03c3 \u208e dv) \u229f\u208d \u03c4 \u208e f (v \u229e\u208d \u03c3 \u208e dv))\n \u2261\u27e8 v+[u-v]=u {\u03c4} {g (v \u229e\u208d \u03c3 \u208e dv)} {f (v \u229e\u208d \u03c3 \u208e dv)} \u27e9\n g (v \u229e\u208d \u03c3 \u208e dv)\n \u2261\u27e8 sym (v+[u-v]=u {\u03c4} {g (v \u229e\u208d \u03c3 \u208e dv)} {f v} ) \u27e9\n f v \u229e\u208d \u03c4 \u208e (g (v \u229e\u208d \u03c3 \u208e dv) \u229f\u208d \u03c4 \u208e f v)\n \u220e) where open \u2261-Reasoning\n\n -- call this lemma \"replace\"?\n -- v+[u-v]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 v \u229e (u \u229f v) \u2261 u\n v+[u-v]=u {base \u03b9} {u} {v} = v+[u-v]=u-base {\u03b9} {u} {v}\n v+[u-v]=u {\u03c3 \u21d2 \u03c4} {u} {v} =\n ext {-\u27e6 \u03c3 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4 \u27e7-} (\u03bb w \u2192\n begin\n (apply-change (\u03c3 \u21d2 \u03c4) v (diff-change (\u03c3 \u21d2 \u03c4) u v)) w\n \u2261\u27e8 refl \u27e9\n v w \u229e\u208d \u03c4 \u208e (u (w \u229e\u208d \u03c3 \u208e (w \u229f\u208d \u03c3 \u208e w)) \u229f\u208d \u03c4 \u208e v w)\n \u2261\u27e8 cong (\u03bb hole \u2192 v w \u229e\u208d \u03c4 \u208e (u hole \u229f\u208d \u03c4 \u208e v w)) (v+[u-v]=u {\u03c3}) \u27e9\n v w \u229e\u208d \u03c4 \u208e (u w \u229f\u208d \u03c4 \u208e v w)\n \u2261\u27e8 v+[u-v]=u {\u03c4} \u27e9\n u w\n \u220e) where\n open \u2261-Reasoning\n\n -- syntactic sugar for implicit indices\n infixl 6 _\u229e_ _\u229f_ -- as with + - in GHC.Num\n\n _\u229e_ : \u2200 {\u03c4} \u2192 (v : \u27e6 \u03c4 \u27e7) \u2192 Change \u03c4 v \u2192 \u27e6 \u03c4 \u27e7\n _\u229e_ {\u03c4} v dv = v \u229e\u208d \u03c4 \u208e dv\n\n _\u229f_ : \u2200 {\u03c4} \u2192 (u v : \u27e6 \u03c4 \u27e7) \u2192 Change \u03c4 v\n _\u229f_ {\u03c4} u v = u \u229f\u208d \u03c4 \u208e v\n\n -- abbrevitations\n before : \u2200 {\u03c4 v} \u2192 Change \u03c4 v \u2192 \u27e6 \u03c4 \u27e7\n before {\u03c4} {v} _ = v\n\n after : \u2200 {\u03c4 v} \u2192 Change \u03c4 v \u2192 \u27e6 \u03c4 \u27e7\n after {\u03c4} {v} dv = v \u229e\u208d \u03c4 \u208e dv\n\n ------------------\n -- Environments --\n ------------------\n\n open DependentList public using (\u2205; _\u2022_)\n open Tuples public using (cons)\n\n data \u0394Env : \u2200 (\u0393 : Context) \u2192 \u27e6 \u0393 \u27e7 \u2192 Set where\n \u2205 : \u0394Env \u2205 \u2205\n _\u2022_ : \u2200 {\u03c4 \u0393 v \u03c1} \u2192\n (dv : Change \u03c4 v) \u2192\n (d\u03c1 : \u0394Env \u0393 \u03c1) \u2192\n \u0394Env (\u03c4 \u2022 \u0393) (v \u2022 \u03c1)\n\n ignore : \u2200 {\u0393 : Context} \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} (d\u03c1 : \u0394Env \u0393 \u03c1) \u2192 \u27e6 \u0393 \u27e7\n ignore {\u0393} {\u03c1} _ = \u03c1\n\n update : \u2200 {\u0393 : Context} \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} (d\u03c1 : \u0394Env \u0393 \u03c1) \u2192 \u27e6 \u0393 \u27e7\n update \u2205 = \u2205\n update {\u03c4 \u2022 \u0393} (dv \u2022 d\u03c1) = after {\u03c4} dv \u2022 update d\u03c1\n\n apply-env : \u2200 \u0393 \u2192 (\u03c1 : \u27e6 \u0393 \u27e7) \u2192 (d\u03c1 : \u0394Env \u0393 \u03c1) \u2192 \u27e6 \u0393 \u27e7\n apply-env \u0393 \u03c1 d\u03c1 = update {\u0393} d\u03c1\n\n diff-env : \u2200 \u0393 \u2192 (\u03c0 \u03c1 : \u27e6 \u0393 \u27e7) \u2192 \u0394Env \u0393 \u03c1\n diff-env \u2205 \u2205 \u2205 = \u2205\n diff-env (\u03c4 \u2022 \u0393) (u \u2022 \u03c1) (v \u2022 \u03c0) = diff-change \u03c4 u v \u2022 diff-env \u0393 \u03c1 \u03c0\n","old_contents":"import Parametric.Syntax.Type as Type\nimport Parametric.Denotation.Value as Value\n\nmodule Parametric.Change.Validity\n {Base : Type.Structure}\n (\u27e6_\u27e7Base : Value.Structure Base)\n where\n\nopen Type.Structure Base\nopen Value.Structure Base \u27e6_\u27e7Base\n\n-- Changes for Calculus Popl14\n--\n-- Contents\n-- - Mutually recursive concepts: \u0394Val, validity.\n-- Under module Syntax, the corresponding concepts of\n-- \u0394Type and \u0394Context reside in separate files.\n-- Now they have to be together due to mutual recursiveness.\n-- - `diff` and `apply` on semantic values of changes:\n-- they have to be here as well because they are mutually\n-- recursive with validity.\n-- - The lemma diff-is-valid: it has to be here because it is\n-- mutually recursive with `apply`\n-- - The lemma apply-diff: it is mutually recursive with `apply`\n-- and `diff`\n\nopen import Base.Denotation.Notation public\n\nopen import Relation.Binary.PropositionalEquality\nopen import Postulate.Extensionality\nopen import Data.Product hiding (map)\n\nimport Structure.Tuples as Tuples\nopen Tuples\n\nimport Base.Data.DependentList as DependentList\nopen DependentList\n\nopen import Relation.Unary using (_\u2286_)\n\nrecord Structure : Set\u2081 where\n ----------------\n -- Parameters --\n ----------------\n\n field\n Change-base : (\u03b9 : Base) \u2192 \u27e6 \u03b9 \u27e7Base \u2192 Set\n apply-change-base : \u2200 \u03b9 \u2192 (v : \u27e6 \u03b9 \u27e7Base) \u2192 Change-base \u03b9 v \u2192 \u27e6 \u03b9 \u27e7Base\n diff-change-base : \u2200 \u03b9 \u2192 (u v : \u27e6 \u03b9 \u27e7Base) \u2192 Change-base \u03b9 v\n v+[u-v]=u-base : \u2200 {\u03b9} {u v : \u27e6 \u03b9 \u27e7Base} \u2192 apply-change-base \u03b9 v (diff-change-base \u03b9 u v) \u2261 u\n\n ---------------\n -- Interface --\n ---------------\n\n Change : (\u03c4 : Type) \u2192 \u27e6 \u03c4 \u27e7 \u2192 Set\n\n nil-change : \u2200 \u03c4 v \u2192 Change \u03c4 v\n apply-change : \u2200 \u03c4 \u2192 (v : \u27e6 \u03c4 \u27e7) (dv : Change \u03c4 v) \u2192 \u27e6 \u03c4 \u27e7\n diff-change : \u2200 \u03c4 \u2192 (u v : \u27e6 \u03c4 \u27e7) \u2192 Change \u03c4 v\n\n infixl 6 apply-change diff-change -- as with + - in GHC.Num\n syntax apply-change \u03c4 v dv = v \u229e\u208d \u03c4 \u208e dv\n syntax diff-change \u03c4 u v = u \u229f\u208d \u03c4 \u208e v\n\n -- Lemma apply-diff\n v+[u-v]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192\n v \u229e\u208d \u03c4 \u208e (u \u229f\u208d \u03c4 \u208e v) \u2261 u\n\n --------------------\n -- Implementation --\n --------------------\n\n -- (Change \u03c4) is the set of changes of type \u03c4. This set is\n -- strictly smaller than \u27e6 \u0394Type \u03c4\u27e7 if \u03c4 is a function type. In\n -- particular, (Change (\u03c3 \u21d2 \u03c4)) is a function that accepts only\n -- valid changes, while \u27e6 \u0394Type (\u03c3 \u21d2 \u03c4) \u27e7 accepts also invalid\n -- changes.\n --\n -- Change \u03c4 is the target of the denotational specification \u27e6_\u27e7\u0394.\n -- Detailed motivation:\n --\n -- https:\/\/github.com\/ps-mr\/ilc\/blob\/184a6291ac6eef80871c32d2483e3e62578baf06\/POPL14\/paper\/sec-formal.tex\n\n -- Change : Type \u2192 Set\n Change (base \u03b9) v = Change-base \u03b9 v\n Change (\u03c3 \u21d2 \u03c4) f = Pair\n (\u2200 v \u2192 Change \u03c3 v \u2192 Change \u03c4 (f v))\n (\u03bb \u0394f \u2192 \u2200 v dv \u2192\n f (v \u229e\u208d \u03c3 \u208e dv) \u229e\u208d \u03c4 \u208e \u0394f (v \u229e\u208d \u03c3 \u208e dv) (nil-change \u03c3 (v \u229e\u208d \u03c3 \u208e dv)) \u2261 f v \u229e\u208d \u03c4 \u208e \u0394f v dv)\n\n open Pair public using () renaming\n ( cdr to is-valid\n ; car to call-change\n )\n\n nil-change \u03c4 v = diff-change \u03c4 v v\n\n -- _\u229e_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 Change \u03c4 \u2192 \u27e6 \u03c4 \u27e7\n apply-change (base \u03b9) n \u0394n = apply-change-base \u03b9 n \u0394n\n apply-change (\u03c3 \u21d2 \u03c4) f \u0394f = \u03bb v \u2192 f v \u229e\u208d \u03c4 \u208e call-change \u0394f v (nil-change \u03c3 v)\n\n -- _\u229f_ : \u2200 {\u03c4} \u2192 \u27e6 \u03c4 \u27e7 \u2192 \u27e6 \u03c4 \u27e7 \u2192 Change \u03c4\n diff-change (base \u03b9) m n = diff-change-base \u03b9 m n\n diff-change (\u03c3 \u21d2 \u03c4) g f = cons (\u03bb v dv \u2192 g (v \u229e\u208d \u03c3 \u208e dv) \u229f\u208d \u03c4 \u208e f v)\n (\u03bb v dv \u2192\n begin\n f (v \u229e\u208d \u03c3 \u208e dv) \u229e\u208d \u03c4 \u208e (g ((v \u229e\u208d \u03c3 \u208e dv) \u229e\u208d \u03c3 \u208e ((v \u229e\u208d \u03c3 \u208e dv) \u229f\u208d \u03c3 \u208e (v \u229e\u208d \u03c3 \u208e dv))) \u229f\u208d \u03c4 \u208e f (v \u229e\u208d \u03c3 \u208e dv))\n \u2261\u27e8 cong (\u03bb \u25a1 \u2192 f (v \u229e\u208d \u03c3 \u208e dv) \u229e\u208d \u03c4 \u208e (g \u25a1 \u229f\u208d \u03c4 \u208e (f (v \u229e\u208d \u03c3 \u208e dv))))\n (v+[u-v]=u {\u03c3} {v \u229e\u208d \u03c3 \u208e dv} {v \u229e\u208d \u03c3 \u208e dv}) \u27e9\n f (v \u229e\u208d \u03c3 \u208e dv) \u229e\u208d \u03c4 \u208e (g (v \u229e\u208d \u03c3 \u208e dv) \u229f\u208d \u03c4 \u208e f (v \u229e\u208d \u03c3 \u208e dv))\n \u2261\u27e8 v+[u-v]=u {\u03c4} {g (v \u229e\u208d \u03c3 \u208e dv)} {f (v \u229e\u208d \u03c3 \u208e dv)} \u27e9\n g (v \u229e\u208d \u03c3 \u208e dv)\n \u2261\u27e8 sym (v+[u-v]=u {\u03c4} {g (v \u229e\u208d \u03c3 \u208e dv)} {f v} ) \u27e9\n f v \u229e\u208d \u03c4 \u208e (g (v \u229e\u208d \u03c3 \u208e dv) \u229f\u208d \u03c4 \u208e f v)\n \u220e) where open \u2261-Reasoning\n\n -- call this lemma \"replace\"?\n -- v+[u-v]=u : \u2200 {\u03c4 : Type} {u v : \u27e6 \u03c4 \u27e7} \u2192 v \u229e (u \u229f v) \u2261 u\n v+[u-v]=u {base \u03b9} {u} {v} = v+[u-v]=u-base {\u03b9} {u} {v}\n v+[u-v]=u {\u03c3 \u21d2 \u03c4} {u} {v} =\n ext {-\u27e6 \u03c3 \u27e7} {\u03bb _ \u2192 \u27e6 \u03c4 \u27e7-} (\u03bb w \u2192\n begin\n (apply-change (\u03c3 \u21d2 \u03c4) v (diff-change (\u03c3 \u21d2 \u03c4) u v)) w\n \u2261\u27e8 refl \u27e9\n v w \u229e\u208d \u03c4 \u208e (u (w \u229e\u208d \u03c3 \u208e (w \u229f\u208d \u03c3 \u208e w)) \u229f\u208d \u03c4 \u208e v w)\n \u2261\u27e8 cong (\u03bb hole \u2192 v w \u229e\u208d \u03c4 \u208e (u hole \u229f\u208d \u03c4 \u208e v w)) (v+[u-v]=u {\u03c3}) \u27e9\n v w \u229e\u208d \u03c4 \u208e (u w \u229f\u208d \u03c4 \u208e v w)\n \u2261\u27e8 v+[u-v]=u {\u03c4} \u27e9\n u w\n \u220e) where\n open \u2261-Reasoning\n\n -- syntactic sugar for implicit indices\n infixl 6 _\u229e_ _\u229f_ -- as with + - in GHC.Num\n\n _\u229e_ : \u2200 {\u03c4} \u2192 (v : \u27e6 \u03c4 \u27e7) \u2192 Change \u03c4 v \u2192 \u27e6 \u03c4 \u27e7\n _\u229e_ {\u03c4} v dv = v \u229e\u208d \u03c4 \u208e dv\n\n _\u229f_ : \u2200 {\u03c4} \u2192 (u v : \u27e6 \u03c4 \u27e7) \u2192 Change \u03c4 v\n _\u229f_ {\u03c4} u v = u \u229f\u208d \u03c4 \u208e v\n\n -- abbrevitations\n before : \u2200 {\u03c4 v} \u2192 Change \u03c4 v \u2192 \u27e6 \u03c4 \u27e7\n before {\u03c4} {v} _ = v\n\n after : \u2200 {\u03c4 v} \u2192 Change \u03c4 v \u2192 \u27e6 \u03c4 \u27e7\n after {\u03c4} {v} dv = v \u229e\u208d \u03c4 \u208e dv\n\n ------------------\n -- Environments --\n ------------------\n\n open DependentList public using (\u2205; _\u2022_)\n open Tuples public using (cons)\n\n data \u0394Env : \u2200 (\u0393 : Context) \u2192 \u27e6 \u0393 \u27e7 \u2192 Set where\n \u2205 : \u0394Env \u2205 \u2205\n _\u2022_ : \u2200 {\u03c4 \u0393 v \u03c1} \u2192\n (dv : Change \u03c4 v) \u2192\n (d\u03c1 : \u0394Env \u0393 \u03c1) \u2192\n \u0394Env (\u03c4 \u2022 \u0393) (v \u2022 \u03c1)\n\n ignore : \u2200 {\u0393 : Context} \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} (d\u03c1 : \u0394Env \u0393 \u03c1) \u2192 \u27e6 \u0393 \u27e7\n ignore {\u0393} {\u03c1} _ = \u03c1\n\n update : \u2200 {\u0393 : Context} \u2192 {\u03c1 : \u27e6 \u0393 \u27e7} (d\u03c1 : \u0394Env \u0393 \u03c1) \u2192 \u27e6 \u0393 \u27e7\n update \u2205 = \u2205\n update {\u03c4 \u2022 \u0393} (dv \u2022 d\u03c1) = after {\u03c4} dv \u2022 update d\u03c1\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c6af6a479f8ea7cd6a2571bf4479cd29f94d9946","subject":"Stratified Desc model: document.","message":"Stratified Desc model: document.\n","repos":"larrytheliquid\/pigit,mietek\/epigram2,mietek\/epigram2","old_file":"models\/Desc.agda","new_file":"models\/Desc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule Desc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\nunlift : {l : Level}{A : Set l} -> Lifted A -> A\nunlift (lifter a) = a\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\npair : {i j : Level}{A : Set i}{B : A -> Set j} -> \n (x : A) (y : B x) -> Sigma {i = i}{j = j} A B\npair x y = x , y\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l m : Level}{A : Set l}{B : Set m}\n (f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l m n : Level}{A : Set l}{B : Set m}{C : Set n}\n (f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\ntrans : {l : Level}{A : Set l}{x y z : A} -> x == y -> y == z -> x == z\ntrans refl refl = refl\n\nproof-lift-unlift-eq : {l : Level}{A : Set l}(x : Lifted A) -> lifter (unlift x) == x\nproof-lift-unlift-eq (lifter a) = refl\n\npostulate \n reflFun : {l m : Level}{A : Set l}{B : A -> Set m}(f : (a : A) -> B a)(g : (a : A) -> B a)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\n-- In the paper, we have presented Desc as the grammar of inductive\n-- types. Hence, the codes in the paper closely follow this\n-- grammar:\n\ndata DescPaper : Set1 where\n oneP : DescPaper\n sigmaP : (S : Set) -> (S -> DescPaper) -> DescPaper\n indx : DescPaper -> DescPaper\n hindx : Set -> DescPaper -> DescPaper\n\n-- We take advantage of this model to give you an alternative\n-- presentation. This alternative model is the one implemented in\n-- Epigram. It is also the one which inspired the code for indexed\n-- descriptions.\n\n-- With sigma, we are actually \"quoting\" a standard type-former,\n-- namely:\n-- |Sigma : (S : Set) -> (S -> Set) -> Set|\n-- With:\n-- |sigma : (S : Set) -> (S -> Desc) -> Desc|\n\n-- In the alternative presentation, we go further and present all our\n-- codes as quotations of standard type-formers:\n\ndata Desc {l : Level} : Set (suc l) where\n id : Desc\n const : Set l -> Desc\n prod : Desc -> Desc -> Desc\n sigma : (S : Set l) -> (S -> Desc) -> Desc\n pi : (S : Set l) -> (S -> Desc) -> Desc\n\n-- Note that we replace |oneP| by a more general |const| code. Whereas\n-- |oneP| was interpreted as the unit set, |const K| is\n-- interpreted as |K|, for any |K : Set|. Extensionally,\n-- |const K| and |sigma K (\\_ -> Unit)| are equivalent. However,\n-- |const| is *first-order*, unlike its equivalent encoding. From a\n-- definitional perspective, we are giving more opportunities to the\n-- type-system, hence reducing the burden on the programmer. For the same\n-- reason, we introduce |prod| that overlaps with |pi|.\n\n-- This reorganisation is strictly equivalent to the |DescPaper|. For\n-- instance, we can encode |indx| and |hindx| using the following\n-- code:\n\nindx2 : {l : Level} -> Desc {l = l} -> Desc {l = l}\nindx2 D = prod id D\n\nhindx2 : Set -> Desc -> Desc\nhindx2 H D = prod (pi H (\\_ -> id)) D\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|]_ : {l : Level} -> Desc -> Set l -> Set l\n[| id |] Z = Z\n[| const X |] Z = X\n[| prod D D' |] Z = [| D |] Z * [| D' |] Z\n[| sigma S T |] Z = Sigma S (\\s -> [| T s |] Z)\n[| pi S T |] Z = (s : S) -> [| T s |] Z\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata Mu {l : Level}(D : Desc {l = l}) : Set l where\n con : [| D |] (Mu D) -> Mu D\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {l : Level}(D : Desc)(X : Set)(P : X -> Set l) -> [| D |] X -> Set l\nAll id X P x = P x\nAll (const Z) X P x = Unit\nAll (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')\nAll (sigma S T) X P (a , b) = All (T a) X P b\nAll (pi S T) X P f = (s : S) -> All (T s) X P (f s)\n\n\nall : {l : Level}(D : Desc)(X : Set)(P : X -> Set l)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x\nall id X P R x = R x\nall (const Z) X P R z = Void\nall (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'\nall (sigma S T) X P R (a , b) = all (T a) X P R b\nall (pi S T) X P R f = \\ s -> all (T s) X P R (f s)\n\n--********************************************\n-- Map\n--********************************************\n\n-- This one is bonus: one could rightfully expect our so-called\n-- functors to have a morphism part! Here it is.\n\nmap : {l : Level}(D : Desc)(X Y : Set l)(f : X -> Y)(v : [| D |] X) -> [| D |] Y\nmap id X Y sig x = sig x\nmap (const Z) X Y sig z = z\nmap (prod D D') X Y sig (d , d') = map D X Y sig d , map D' X Y sig d'\nmap (sigma S T) X Y sig (a , b) = (a , map (T a) X Y sig b)\nmap (pi S T) X Y sig f = \\x -> map (T x) X Y sig (f x)\n\n-- Together with the proof that they respect the functor laws:\n\n-- map id = id\nproof-map-id : {l : Level}(D : Desc)(X : Set l)(v : [| D |] X) -> map D X X (\\x -> x) v == v\nproof-map-id id X v = refl\nproof-map-id (const Z) X v = refl\nproof-map-id (prod D D') X (v , v') = cong2 (\\x y -> (x , y)) (proof-map-id D X v) (proof-map-id D' X v')\nproof-map-id (sigma S T) X (a , b) = cong (\\x -> (a , x)) (proof-map-id (T a) X b) \nproof-map-id (pi S T) X f = reflFun (\\a -> map (T a) X X (\\x -> x) (f a)) f (\\a -> proof-map-id (T a) X (f a))\n\n-- map (f . g) = map f . map g\nproof-map-compos : {l : Level}(D : Desc)(X Y Z : Set l)\n (f : X -> Y)(g : Y -> Z)\n (v : [| D |] X) -> \n map D X Z (\\x -> g (f x)) v == map D Y Z g (map D X Y f v)\nproof-map-compos id X Y Z f g v = refl\nproof-map-compos (const K) X Y Z f g v = refl\nproof-map-compos (prod D D') X Y Z f g (v , v') = cong2 (\\x y -> (x , y)) \n (proof-map-compos D X Y Z f g v)\n (proof-map-compos D' X Y Z f g v')\nproof-map-compos (sigma S T) X Y Z f g (a , b) = cong (\\x -> (a , x)) (proof-map-compos (T a) X Y Z f g b)\nproof-map-compos (pi S T) X Y Z f g fc = reflFun (\\a -> map (T a) X Z (\\x -> g (f x)) (fc a))\n (\\a -> map (T a) Y Z g (map (T a) X Y f (fc a)))\n (\\a -> proof-map-compos (T a) X Y Z f g (fc a))\n\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\nind : {l : Level}\n (D : Desc) \n (P : Mu D -> Set l) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\nind D P ms (con xs) = ms xs (all D (Mu D) P (\\x -> ind D P ms x) xs)\n-}\n\n-- But the termination checker is unhappy.\n-- So we write the following:\n\nmodule Elim {l : Level}\n (D : Desc)\n (P : Mu D -> Set l)\n (ms : (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x))\n where\n\n mutual\n ind : (x : Mu D) -> P x\n ind (con xs) = ms xs (hyps D xs)\n \n hyps : (D' : Desc)\n (xs : [| D' |] (Mu D)) ->\n All D' (Mu D) P xs\n hyps id x = ind x\n hyps (const Z) z = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (sigma S T) (a , b) = hyps (T a) b\n hyps (pi S T) f = \\s -> hyps (T s) (f s)\n \nind : {l : Level}\n (D : Desc) \n (P : Mu D -> Set l) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\nind D P ms x = Elim.ind D P ms x\n\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> Desc\nnatCases Ze = const Unit\nnatCases Suc = id\n\nNatD : Desc\nNatD = sigma NatConst natCases\n\nNat : Set\nNat = Mu NatD\n\nze : Nat\nze = con (Ze , Void)\n\nsu : Nat -> Nat\nsu n = con (Su , n)\n\n-- Now we can get addition for example:\n\nplusCase : (xs : [| NatD |] Nat) ->\n All NatD Nat (\\_ -> Nat -> Nat) xs -> Nat -> Nat\nplusCase ( Ze , Void ) hs y = y\nplusCase ( Su , n ) hs y = su (hs y)\n\nplus : Nat -> Nat -> Nat\nplus x = ind NatD (\\ _ -> (Nat -> Nat)) plusCase x\n\n-- Do this thing in Epigram, you will see that this is *not*\n-- hieroglyphic with a bit of elaboration.\n\n--****************\n-- List\n--****************\n\ndata ListConst : Set where\n Nil : ListConst\n Cons : ListConst\n\nlistCases : Set -> ListConst -> Desc\nlistCases X Nil = const Unit\nlistCases X Cons = sigma X (\\_ -> id)\n\nListD : Set -> Desc\nListD X = sigma ListConst (listCases X)\n\nList : Set -> Set\nList X = Mu (ListD X)\n\nnil : {X : Set} -> List X\nnil = con ( Nil , Void )\n\ncons : {X : Set} -> X -> List X -> List X\ncons x t = con ( Cons , ( x , t ))\n\n--****************\n-- Tree\n--****************\n\ndata TreeConst : Set where\n Leaf : TreeConst\n Node : TreeConst\n\ntreeCases : Set -> TreeConst -> Desc\ntreeCases X Leaf = const Unit\ntreeCases X Node = sigma X (\\_ -> prod id id)\n\nTreeD : Set -> Desc\nTreeD X = sigma TreeConst (treeCases X)\n\nTree : Set -> Set\nTree X = Mu (TreeD X)\n\nleaf : {X : Set} -> Tree X\nleaf = con (Leaf , Void)\n\nnode : {X : Set} -> X -> Tree X -> Tree X -> Tree X\nnode x le ri = con (Node , (x , (le , ri)))\n\n--********************************************\n-- Finite sets\n--********************************************\n\n-- If we weren't such big fans of levitating things, we would\n-- implement finite sets with:\n\n{-\n\ndata En : Set where\n nE : En\n cE : En -> En\n\nspi : (e : En)(P : EnumT e -> Set) -> Set\nspi nE P = Unit\nspi (cE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : En)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nE P b ()\nswitch (cE e) P b EZe = fst b\nswitch (cE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n-}\n\n-- But no, we make it fly in Desc:\n\n--****************\n-- En\n--****************\n\n-- As we have no tags here, we use Nat instead of List. \n\nEnD : Desc\nEnD = NatD\n\nEn : Set\nEn = Nat\n\nnE : En\nnE = ze\n\ncE : En -> En\ncE e = su e \n\n--****************\n-- EnumT\n--****************\n\n-- Because I don't want to fall back on wacky unicode symbols, I will\n-- write EnumT for #, EZe for 0, and ESu for 1+. Sorry about that\n\ndata EnumT : (e : En) -> Set where\n EZe : {e : En} -> EnumT (cE e)\n ESu : {e : En} -> EnumT e -> EnumT (cE e)\n\n--****************\n-- Small Pi\n--****************\n\n-- This corresponds to the small pi |\\pi|.\n\ncasesSpi : {l : Level}(xs : [| EnD |] En) -> \n All EnD En (\\e -> (EnumT e -> Set l) -> Set l) xs -> \n (EnumT (con xs) -> Set l) -> Set l\ncasesSpi (Ze , Void) hs P' = Unit\ncasesSpi (Su , n) hs P' = P' EZe * hs (\\e -> P' (ESu e))\n\nspi : {l : Level}(e : En)(P : EnumT e -> Set l) -> Set l\nspi {x} e P = ind EnD (\\E -> (EnumT E -> Set x) -> Set x) casesSpi e P\n\n--****************\n-- Switch\n--****************\n\ncasesSwitch : {l : Level}\n (xs : [| EnD |] En) ->\n All EnD En (\\e -> (P' : EnumT e -> Set l)\n (b' : spi e P')\n (x' : EnumT e) -> P' x') xs ->\n (P' : EnumT (con xs) -> Set l)\n (b' : spi (con xs) P')\n (x' : EnumT (con xs)) -> P' x'\ncasesSwitch (Ze , Void) hs P' b' ()\ncasesSwitch (Su , n) hs P' b' EZe = fst b'\ncasesSwitch (Su , n) hs P' b' (ESu e') = hs (\\e -> P' (ESu e)) (snd b') e'\n\n\nswitch : {l : Level}\n (e : En)\n (P : EnumT e -> Set l)\n (b : spi e P)\n (x : EnumT e) -> P x\nswitch {x} e P b xs = ind EnD\n (\\e -> (P : EnumT e -> Set x)\n (b : spi e P)\n (xs : EnumT e) -> P xs) \n casesSwitch e P b xs \n\n--****************\n-- Desc\n--****************\n\n-- In the following, we implement Desc in itself. As usual, we have a\n-- finite set of constructors -- the name of the codes. Note that we\n-- could really define these as a finite set built above. However, in\n-- Agda, it's horribly verbose. For the sake of clarity, we won't do\n-- that here. \n\ndata DescDef : Set1 where\n DescId : DescDef\n DescConst : DescDef\n DescProd : DescDef\n DescSigma : DescDef\n DescPi : DescDef\n\n-- We slightly diverge here from the presentation of the paper: note\n-- the presence of terminating \"const Unit\". Recall our Lisp-ish\n-- notation for nested tuples:\n-- |[a b c]| \n-- Corresponds to \n-- |[a , [ b , [c , []]]]|\n-- So, if we want to write constructors using our Lisp-ish notation, the interpretation \n-- [| DescD |] (Mu DescD) have to evaluates to [ constructor , [ arg1 , [ arg2 , []]]]\n\n-- Hence, we define Desc's code as follow:\n\ndescCases : DescDef -> Desc\ndescCases DescId = const Unit\ndescCases DescConst = sigma Set (\\_ -> const Unit)\ndescCases DescProd = prod id (prod id (const Unit))\ndescCases DescSigma = sigma Set (\\S -> prod (pi (lift S) (\\_ -> id)) (const Unit))\ndescCases DescPi = sigma Set (\\S -> prod (pi (lift S) (\\_ -> id)) (const Unit))\n\nDescD : Desc\nDescD = sigma DescDef descCases\n\nDescIn : Set1\nDescIn = Mu DescD\n\n-- So that the constructors are:\n-- (Note the annoying |pair|s to set the implicit levels. I could not\n-- get rid of the yellow otherwise)\n\nidIn : DescIn\nidIn = con (pair {i = suc zero} {j = suc zero} DescId Void)\nconstIn : Set -> DescIn\nconstIn K = con (pair {i = suc zero} {j = suc zero} DescConst (K , Void))\nprodIn : (D D' : DescIn) -> DescIn\nprodIn D D' = con (pair {i = suc zero} {j = suc zero} DescProd (D , ( D' , Void )))\nsigmaIn : (S : Set)(D : S -> DescIn) -> DescIn\nsigmaIn S D = con (pair {i = suc zero} {j = suc zero} DescSigma (S , ((\\s -> D (unlift s)) , Void )))\npiIn : (S : Set)(D : S -> DescIn) -> DescIn\npiIn S D = con (pair {i = suc zero} {j = suc zero} DescPi (S , ((\\s -> D (unlift s)) , Void )))\n\n-- At this stage, we could prove the isomorphism between |DescIn| and\n-- |Desc|. While not technically difficult, it is long and\n-- laborious. We have carried this proof on the more complex and\n-- interesting |IDesc| universe, in IDesc.agda.\n\n\n--********************************************\n-- Tagged description\n--********************************************\n\nTagDesc : {l : Level} -> Set (suc l)\nTagDesc = Sigma En (\\e -> spi e (\\_ -> Desc))\n\nde : TagDesc -> Desc\nde (B , F) = sigma (EnumT B) (\\E -> switch B (\\_ -> Desc) F E)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (D : Desc)\n (T : Set) ->\n ([| D |] T -> T) ->\n (Mu D) -> T\ncata D T phi x = ind D (\\_ -> T) (\\x ms -> phi (replace D T x ms)) x\n where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\\_ -> T) xs) -> [| D' |] T\n replace id T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : TagDesc -> (X : Set) -> TagDesc\n(e , D) ** X = cE e , (const X , D)\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : (D : TagDesc)(X Y : Set) ->\n (X -> Mu (de (D ** Y))) ->\n [| de (D ** X) |] (Mu (de (D ** Y))) ->\n Mu (de (D ** Y))\napply (E , B) X Y sig (EZe , x) = sig x\napply (E , B) X Y sig (ESu n , t) = con (ESu n , t)\n\nsubst : (D : TagDesc)(X Y : Set) ->\n Mu (de (D ** X)) ->\n (X -> Mu (de (D ** Y))) ->\n Mu (de (D ** Y))\nsubst D X Y x sig = cata (de (D ** X)) (Mu (de (D ** Y))) (apply D X Y sig) x\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule Desc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\nunlift : {l : Level}{A : Set l} -> Lifted A -> A\nunlift (lifter a) = a\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\npair : {i j : Level}{A : Set i}{B : A -> Set j} -> \n (x : A) (y : B x) -> Sigma {i = i}{j = j} A B\npair x y = x , y\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l m : Level}{A : Set l}{B : Set m}\n (f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l m n : Level}{A : Set l}{B : Set m}{C : Set n}\n (f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\ntrans : {l : Level}{A : Set l}{x y z : A} -> x == y -> y == z -> x == z\ntrans refl refl = refl\n\nproof-lift-unlift-eq : {l : Level}{A : Set l}(x : Lifted A) -> lifter (unlift x) == x\nproof-lift-unlift-eq (lifter a) = refl\n\npostulate \n reflFun : {l m : Level}{A : Set l}{B : A -> Set m}(f : (a : A) -> B a)(g : (a : A) -> B a)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata Desc {l : Level} : Set (suc l) where\n id : Desc\n const : Set l -> Desc\n prod : Desc -> Desc -> Desc\n sigma : (S : Set l) -> (S -> Desc) -> Desc\n pi : (S : Set l) -> (S -> Desc) -> Desc\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\n[|_|]_ : {l : Level} -> Desc -> Set l -> Set l\n[| id |] Z = Z\n[| const X |] Z = X\n[| prod D D' |] Z = [| D |] Z * [| D' |] Z\n[| sigma S T |] Z = Sigma S (\\s -> [| T s |] Z)\n[| pi S T |] Z = (s : S) -> [| T s |] Z\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata Mu {l : Level}(D : Desc {l = l}) : Set l where\n con : [| D |] (Mu D) -> Mu D\n\n--********************************************\n-- Predicate: All\n--********************************************\n\nAll : {l : Level}(D : Desc)(X : Set)(P : X -> Set l) -> [| D |] X -> Set l\nAll id X P x = P x\nAll (const Z) X P x = Unit\nAll (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')\nAll (sigma S T) X P (a , b) = All (T a) X P b\nAll (pi S T) X P f = (s : S) -> All (T s) X P (f s)\n\n\nall : {l : Level}(D : Desc)(X : Set)(P : X -> Set l)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x\nall id X P R x = R x\nall (const Z) X P R z = Void\nall (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'\nall (sigma S T) X P R (a , b) = all (T a) X P R b\nall (pi S T) X P R f = \\ s -> all (T s) X P R (f s)\n\n--********************************************\n-- Map\n--********************************************\n\nmap : {l : Level}(D : Desc)(X Y : Set l)(f : X -> Y)(v : [| D |] X) -> [| D |] Y\nmap id X Y sig x = sig x\nmap (const Z) X Y sig z = z\nmap (prod D D') X Y sig (d , d') = map D X Y sig d , map D' X Y sig d'\nmap (sigma S T) X Y sig (a , b) = (a , map (T a) X Y sig b)\nmap (pi S T) X Y sig f = \\x -> map (T x) X Y sig (f x)\n\n\nproof-map-id : {l : Level}(D : Desc)(X : Set l)(v : [| D |] X) -> map D X X (\\x -> x) v == v\nproof-map-id id X v = refl\nproof-map-id (const Z) X v = refl\nproof-map-id (prod D D') X (v , v') = cong2 (\\x y -> (x , y)) (proof-map-id D X v) (proof-map-id D' X v')\nproof-map-id (sigma S T) X (a , b) = cong (\\x -> (a , x)) (proof-map-id (T a) X b) \nproof-map-id (pi S T) X f = reflFun (\\a -> map (T a) X X (\\x -> x) (f a)) f (\\a -> proof-map-id (T a) X (f a))\n\n\nproof-map-compos : {l : Level}(D : Desc)(X Y Z : Set l)\n (f : X -> Y)(g : Y -> Z)\n (v : [| D |] X) -> \n map D X Z (\\x -> g (f x)) v == map D Y Z g (map D X Y f v)\nproof-map-compos id X Y Z f g v = refl\nproof-map-compos (const K) X Y Z f g v = refl\nproof-map-compos (prod D D') X Y Z f g (v , v') = cong2 (\\x y -> (x , y)) \n (proof-map-compos D X Y Z f g v)\n (proof-map-compos D' X Y Z f g v')\nproof-map-compos (sigma S T) X Y Z f g (a , b) = cong (\\x -> (a , x)) (proof-map-compos (T a) X Y Z f g b)\nproof-map-compos (pi S T) X Y Z f g fc = reflFun (\\a -> map (T a) X Z (\\x -> g (f x)) (fc a))\n (\\a -> map (T a) Y Z g (map (T a) X Y f (fc a)))\n (\\a -> proof-map-compos (T a) X Y Z f g (fc a))\n\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\n-- One would like to write the following:\n\n{-\ninduction : {l : Level}\n (D : Desc) \n (P : Mu D -> Set l) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms (con xs) = ms xs (all D (Mu D) P (\\x -> induction D P ms x) xs)\n-}\n\n-- But the termination checker is unhappy.\n-- So we write the following:\n\nmodule Elim {l : Level}\n (D : Desc)\n (P : Mu D -> Set l)\n (ms : (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x))\n where\n\n mutual\n induction : (x : Mu D) -> P x\n induction (con xs) = ms xs (hyps D xs)\n \n hyps : (D' : Desc)\n (xs : [| D' |] (Mu D)) ->\n All D' (Mu D) P xs\n hyps id x = induction x\n hyps (const Z) z = Void\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (sigma S T) (a , b) = hyps (T a) b\n hyps (pi S T) f = \\s -> hyps (T s) (f s)\n \ninduction : {l : Level}\n (D : Desc) \n (P : Mu D -> Set l) ->\n ( (x : [| D |] (Mu D)) -> \n All D (Mu D) P x -> P (con x)) ->\n (v : Mu D) ->\n P v\ninduction D P ms x = Elim.induction D P ms x\n\n\n--********************************************\n-- Examples\n--********************************************\n\n--****************\n-- Nat\n--****************\n\ndata NatConst : Set where\n Ze : NatConst\n Su : NatConst\n\nnatCases : NatConst -> Desc\nnatCases Ze = const Unit\nnatCases Suc = id\n\nNatD : Desc\nNatD = sigma NatConst natCases\n\nNat : Set\nNat = Mu NatD\n\nze : Nat\nze = con (Ze , Void)\n\nsu : Nat -> Nat\nsu n = con (Su , n)\n\nplusCase : (xs : [| NatD |] Nat) ->\n All NatD Nat (\\_ -> Nat -> Nat) xs -> Nat -> Nat\nplusCase ( Ze , Void ) hs y = y\nplusCase ( Su , n ) hs y = su (hs y)\n\nplus : Nat -> Nat -> Nat\nplus x = induction NatD (\\ _ -> (Nat -> Nat)) plusCase x\n\n-- Do this thing in Epigram, you will see that this is *not*\n-- hieroglyphic with a bit of elaboration.\n\n--****************\n-- List\n--****************\n\ndata ListConst : Set where\n Nil : ListConst\n Cons : ListConst\n\nlistCases : Set -> ListConst -> Desc\nlistCases X Nil = const Unit\nlistCases X Cons = sigma X (\\_ -> id)\n\nListD : Set -> Desc\nListD X = sigma ListConst (listCases X)\n\nList : Set -> Set\nList X = Mu (ListD X)\n\nnil : {X : Set} -> List X\nnil = con ( Nil , Void )\n\ncons : {X : Set} -> X -> List X -> List X\ncons x t = con ( Cons , ( x , t ))\n\n--****************\n-- Tree\n--****************\n\ndata TreeConst : Set where\n Leaf : TreeConst\n Node : TreeConst\n\ntreeCases : Set -> TreeConst -> Desc\ntreeCases X Leaf = const Unit\ntreeCases X Node = sigma X (\\_ -> prod id id)\n\nTreeD : Set -> Desc\nTreeD X = sigma TreeConst (treeCases X)\n\nTree : Set -> Set\nTree X = Mu (TreeD X)\n\nleaf : {X : Set} -> Tree X\nleaf = con (Leaf , Void)\n\nnode : {X : Set} -> X -> Tree X -> Tree X -> Tree X\nnode x le ri = con (Node , (x , (le , ri)))\n\n--********************************************\n-- Finite sets\n--********************************************\n\n-- If we weren't such big fans of levitating things, we would\n-- implement finite sets with:\n\n{-\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n-}\n\n-- But no, we make it fly in Desc:\n\n--****************\n-- EnumU\n--****************\n\nEnumU : Set\nEnumU = Nat\n\nnilE : EnumU\nnilE = ze\n\nconsE : EnumU -> EnumU\nconsE e = su e \n\n--****************\n-- EnumT\n--****************\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\n--****************\n-- Small Pi\n--****************\n\ncasesSpi : {l : Level}(xs : [| NatD |] Nat) -> \n All NatD Nat (\\e -> (EnumT e -> Set l) -> Set l) xs -> \n (EnumT (con xs) -> Set l) -> Set l\ncasesSpi (Ze , Void) hs P' = Unit\ncasesSpi (Su , n) hs P' = P' EZe * hs (\\e -> P' (ESu e))\n\nspi : {l : Level}(e : EnumU)(P : EnumT e -> Set l) -> Set l\nspi {x} e P = induction NatD (\\E -> (EnumT E -> Set x) -> Set x) casesSpi e P\n\n--****************\n-- Switch\n--****************\n\ncasesSwitch : {l : Level}\n (xs : [| NatD |] Nat) ->\n All NatD Nat (\\e -> (P' : EnumT e -> Set l)\n (b' : spi e P')\n (x' : EnumT e) -> P' x') xs ->\n (P' : EnumT (con xs) -> Set l)\n (b' : spi (con xs) P')\n (x' : EnumT (con xs)) -> P' x'\ncasesSwitch (Ze , Void) hs P' b' ()\ncasesSwitch (Su , n) hs P' b' EZe = fst b'\ncasesSwitch (Su , n) hs P' b' (ESu e') = hs (\\e -> P' (ESu e)) (snd b') e'\n\n\nswitch : {l : Level}\n (e : EnumU)\n (P : EnumT e -> Set l)\n (b : spi e P)\n (x : EnumT e) -> P x\nswitch {x} e P b xs = induction NatD\n (\\e -> (P : EnumT e -> Set x)\n (b : spi e P)\n (xs : EnumT e) -> P xs) \n casesSwitch e P b xs \n\n--****************\n-- Desc\n--****************\n\n-- TODO: explain that if it weren't so verbose\n-- we could use finite sets instead of DescDef\n\ndata DescDef : Set1 where\n DescId : DescDef\n DescConst : DescDef\n DescProd : DescDef\n DescSigma : DescDef\n DescPi : DescDef\n\n-- TODO: explain the Units\n\ndescCases : DescDef -> Desc\ndescCases DescId = const Unit\ndescCases DescConst = sigma Set (\\_ -> const Unit)\ndescCases DescProd = prod id (prod id (const Unit))\ndescCases DescSigma = sigma Set (\\S -> prod (pi (lift S) (\\_ -> id)) (const Unit))\ndescCases DescPi = sigma Set (\\S -> prod (pi (lift S) (\\_ -> id)) (const Unit))\n\nDescD : Desc\nDescD = sigma DescDef descCases\n\nDescIn : Set1\nDescIn = Mu DescD\n\n-- TODO: Explain that we can prove iso between Desc and DescIn. \n-- Report to IDesc.\n\n\n--********************************************\n-- Tagged description\n--********************************************\n\nTagDesc : {l : Level} -> Set (suc l)\nTagDesc = Sigma EnumU (\\e -> spi e (\\_ -> Desc))\n\ntoDesc : TagDesc -> Desc\ntoDesc (B , F) = sigma (EnumT B) (\\E -> switch B (\\_ -> Desc) F E)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (D : Desc)\n (T : Set) ->\n ([| D |] T -> T) ->\n (Mu D) -> T\ncata D T phi x = induction D (\\_ -> T) (\\x ms -> phi (replace D T x ms)) x\n where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\\_ -> T) xs) -> [| D' |] T\n replace id T x y = y\n replace (const Z) T z z' = z\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Free monad construction\n--********************************************\n\n_**_ : TagDesc -> (X : Set) -> TagDesc\n(e , D) ** X = consE e , (const X , D)\n\n--********************************************\n-- Substitution\n--********************************************\n\napply : (D : TagDesc)(X Y : Set) ->\n (X -> Mu (toDesc (D ** Y))) ->\n [| toDesc (D ** X) |] (Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\napply (E , B) X Y sig (EZe , x) = sig x\napply (E , B) X Y sig (ESu n , t) = con (ESu n , t)\n\nsubst : (D : TagDesc)(X Y : Set) ->\n Mu (toDesc (D ** X)) ->\n (X -> Mu (toDesc (D ** Y))) ->\n Mu (toDesc (D ** Y))\nsubst D X Y x sig = cata (toDesc (D ** X)) (Mu (toDesc (D ** Y))) (apply D X Y sig) x\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d3a7e936c967d5c8f0ec2392f3f73ce9c739b925","subject":"agda : completed conor Ex1","message":"agda : completed conor Ex1\n","repos":"haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc","old_file":"agda\/course\/2017-conor_mcbride_cs410\/Ex1-HC.agda","new_file":"agda\/course\/2017-conor_mcbride_cs410\/Ex1-HC.agda","new_contents":"{-# OPTIONS --allow-unsolved-metas #-}\n------------------------------------------------------------------------------\n------------------------------------------------------------------------------\n-- CS410 2017\/18 Exercise 1 VECTORS AND FRIENDS (worth 25%)\n------------------------------------------------------------------------------\n------------------------------------------------------------------------------\n\n-- NOTE (19\/9\/17) This file is currently incomplete: more will arrive on\n-- GitHub.\n\n-- MARK SCHEME (transcribed from paper): the (m) numbers add up to slightly\n-- more than 25, so should be taken as the maximum number of marks losable on\n-- the exercise. In fact, I did mark it negatively, but mostly because it was\n-- done so well (with Agda's help) that it was easier to find the errors.\n\n\n------------------------------------------------------------------------------\n-- Dependencies\n------------------------------------------------------------------------------\n\nopen import CS410-Prelude\n\ncong : \u2200 {A B : Set} {x y : A}\n \u2192 (f : A \u2192 B)\n \u2192 x == y\n ----------\n \u2192 f x == f y\ncong f (refl x) = refl (f x)\n\n------------------------------------------------------------------------------\n-- Vectors\n------------------------------------------------------------------------------\n\ndata Vec (X : Set) : Nat -> Set where -- like lists, but length-indexed\n [] : Vec X zero\n _,-_ : {n : Nat} -> X -> Vec X n -> Vec X (suc n)\ninfixr 4 _,-_ -- the \"cons\" operator associates to the right\n\n-- I like to use the asymmetric ,- to remind myself that the element is to\n-- the left and the rest of the list is to the right.\n\n-- Vectors are useful when there are important length-related safety\n-- properties.\n\n------------------------------------------------------------------------------\n-- Heads and Tails\n------------------------------------------------------------------------------\n\n-- We can rule out nasty head and tail errors by insisting on nonemptiness!\n\n--??--1.1-(2)-----------------------------------------------------------------\n\nvHead : {X : Set}{n : Nat} -> Vec X (suc n) -> X\nvHead (x ,- _) = x\n\nvTail : {X : Set}{n : Nat} -> Vec X (suc n) -> Vec X n\nvTail (_ ,- xs) = xs\n\nvHeadTailFact : {X : Set}{n : Nat}(xs : Vec X (suc n)) ->\n (vHead xs ,- vTail xs) == xs\nvHeadTailFact (x ,- xs) = refl (x ,- xs)\n\n--??--------------------------------------------------------------------------\n\n\n------------------------------------------------------------------------------\n-- Concatenation and its Inverse\n------------------------------------------------------------------------------\n\n--??--1.2-(2)-----------------------------------------------------------------\n\n_+V_ : {X : Set}{m n : Nat} -> Vec X m -> Vec X n -> Vec X (m +N n)\n[] +V ys = ys\n(x ,- xs) +V ys = x ,- xs +V ys\ninfixr 4 _+V_\n\nvChop : {X : Set}(m : Nat){n : Nat} -> Vec X (m +N n) -> Vec X m * Vec X n\nvChop zero xs = [] , xs\nvChop (suc m) (x ,- xs)\n with vChop m xs\n... | vm , vn\n = (x ,- vm) , vn\n\nvChopAppendFact : {X : Set}{m n : Nat}(xs : Vec X m)(ys : Vec X n) ->\n vChop m (xs +V ys) == (xs , ys)\nvChopAppendFact [] ys = refl ([] , ys)\nvChopAppendFact (x ,- xs) ys rewrite vChopAppendFact xs ys = refl ((x ,- xs) , ys)\n\n--??--------------------------------------------------------------------------\n\n\n------------------------------------------------------------------------------\n-- Map, take I\n------------------------------------------------------------------------------\n\n-- Implement the higher-order function that takes an operation on\n-- elements and does it to each element of a vector. Use recursion\n-- on the vector.\n-- Note that the type tells you the size remains the same.\n\n-- Show that if the elementwise function \"does nothing\", neither does\n-- its vMap. \"map of identity is identity\"\n\n-- Show that two vMaps in a row can be collapsed to just one, or\n-- \"composition of maps is map of compositions\"\n\n--??--1.3-(2)-----------------------------------------------------------------\n\nvMap : {X Y : Set} -> (X -> Y) -> {n : Nat} -> Vec X n -> Vec Y n\nvMap f [] = []\nvMap f (x ,- xs) = f x ,- vMap f xs\n\nvMapIdFact : {X : Set}{f : X -> X}(feq : (x : X) -> f x == x) ->\n {n : Nat}(xs : Vec X n) -> vMap f xs == xs\nvMapIdFact feq [] = refl []\nvMapIdFact feq (x ,- xs)\n rewrite vMapIdFact feq xs\n | feq x\n = refl (x ,- xs)\n\nvMapCpFact : {X Y Z : Set}{f : Y -> Z}{g : X -> Y}{h : X -> Z}\n (heq : (x : X) -> f (g x) == h x) ->\n {n : Nat}(xs : Vec X n) ->\n vMap f (vMap g xs) == vMap h xs\nvMapCpFact heq [] = refl []\nvMapCpFact {_}{_}{_}{f} {g} {h} heq (x ,- xs)\n rewrite heq x\n | vMapCpFact {_}{_}{_}{f} {g} {h} heq xs\n = refl (h x ,- vMap h xs)\n\n--??--------------------------------------------------------------------------\n\n\n------------------------------------------------------------------------------\n-- vMap and +V\n------------------------------------------------------------------------------\n\n-- Show that if you've got two vectors of Xs and a function from X to Y,\n-- and you want to concatenate and map, it doesn't matter which you do\n-- first.\n\n--??--1.4-(1)-----------------------------------------------------------------\n\nvMap+VFact : {X Y : Set}(f : X -> Y) ->\n {m n : Nat}(xs : Vec X m)(xs' : Vec X n) ->\n vMap f (xs +V xs') == (vMap f xs +V vMap f xs')\nvMap+VFact f [] xs' = refl (vMap f xs')\nvMap+VFact f (x ,- xs) xs'\n rewrite vMap+VFact f xs xs'\n = refl (f x ,- vMap f xs +V vMap f xs')\n\n--??--------------------------------------------------------------------------\n\n-- Think about what you could prove, relating vMap with vHead, vTail, vChop...\n-- Now google \"Philip Wadler\" \"Theorems for Free\"\n\n-- TODO\n\n------------------------------------------------------------------------------\n-- Applicative Structure (giving mapping and zipping cheaply)\n------------------------------------------------------------------------------\n\n--??--1.5-(2)-----------------------------------------------------------------\n\n-- HINT: you will need to override the default invisibility of n to do this.\n-- HC : replicate\nvPure : {X : Set} -> X -> {n : Nat} -> Vec X n\nvPure x {zero} = []\nvPure x {suc n} = x ,- vPure x {n}\n\n_$V_ : {X Y : Set}{n : Nat} -> Vec (X -> Y) n -> Vec X n -> Vec Y n\n[] $V [] = []\nf ,- fs $V x ,- xs = f x ,- (fs $V xs)\ninfixl 3 _$V_ -- \"Application associates to the left,\n -- rather as we all did in the sixties.\" (Roger Hindley)\n\n-- Pattern matching and recursion are forbidden for the next two tasks.\n\n-- implement vMap again, but as a one-liner\nvec : {X Y : Set} -> (X -> Y) -> {n : Nat} -> Vec X n -> Vec Y n\nvec f xs = vPure f $V xs\n\n-- implement the operation which pairs up corresponding elements\nvZip : {X Y : Set}{n : Nat} -> Vec X n -> Vec Y n -> Vec (X * Y) n\nvZip xs ys = vec (_,_) xs $V ys\n\n--??--------------------------------------------------------------------------\n\n\n------------------------------------------------------------------------------\n-- Applicative Laws\n------------------------------------------------------------------------------\n\n-- According to \"Applicative programming with effects\" by\n-- Conor McBride and Ross Paterson\n-- some laws should hold for applicative functors.\n-- Check that this is the case.\n\n--??--1.6-(2)-----------------------------------------------------------------\n\nvIdentity : {X : Set}{f : X -> X}(feq : (x : X) -> f x == x) ->\n {n : Nat}(xs : Vec X n) -> (vPure f $V xs) == xs\nvIdentity feq [] = refl []\nvIdentity feq (x ,- xs) rewrite vIdentity feq xs | feq x = refl (x ,- xs)\n\nvHomomorphism : {X Y : Set}(f : X -> Y)(x : X) ->\n {n : Nat} -> (vPure f $V vPure x) == vPure (f x) {n}\nvHomomorphism f x {zero} = refl []\nvHomomorphism f x {suc n} rewrite vHomomorphism f x {n} = refl (f x ,- vPure (f x))\n\nvInterchange : {X Y : Set}{n : Nat}(fs : Vec (X -> Y) n)(x : X) ->\n (fs $V vPure x) == (vPure (_$ x) $V fs)\nvInterchange [] x = refl []\nvInterchange (f ,- fs) x rewrite vInterchange fs x = refl (f x ,- (vPure (\u03bb x\u2192y \u2192 x\u2192y x) $V fs))\n\nvComposition : {X Y Z : Set}{n : Nat}\n (fs : Vec (Y -> Z) n)(gs : Vec (X -> Y) n)(xs : Vec X n) ->\n (vPure _<<_ $V fs $V gs $V xs) == (fs $V (gs $V xs))\nvComposition [] [] [] = refl []\nvComposition (f ,- fs) (g ,- gs) (x ,- xs)\n rewrite vComposition fs gs xs\n = refl (f (g x) ,- (fs $V (gs $V xs)))\n\n--??--------------------------------------------------------------------------\n\n\n------------------------------------------------------------------------------\n-- Order-Preserving Embeddings (also known in the business as \"thinnings\")\n------------------------------------------------------------------------------\n\n-- What have these to do with Pascal's Triangle?\n\n-- how to choose N things from M things\ndata _<=_ : Nat -> Nat -> Set where\n oz : zero <= zero -- stop\n os : {n m : Nat} -> n <= m -> suc n <= suc m -- take this one and keep going\n o' : {n m : Nat} -> n <= m -> n <= suc m -- skip this one and keep going\n\nrefl-<= : (n : Nat) -> n <= n\nrefl-<= zero = oz\nrefl-<= (suc n) = os (refl-<= n)\n\ntrans-<= : {n m p : Nat} -> n <= m -> m <= p -> n <= p\ntrans-<= oz zero<=p = zero<=p\ntrans-<= (os n<=m) (os sucm<=p) = os (trans-<= n<=m sucm<=p)\ntrans-<= (os n<=m) (o' sucm<=p) = os (trans-<= (o' n<=m) sucm<=p)\ntrans-<= (o' n<=m) (os m<=p) = o' (trans-<= n<=m m<=p)\ntrans-<= (o' n<=m) (o' m<=p) = o' (trans-<= (o' n<=m) m<=p)\n\n<=-suc : (x : Nat) -> x <= suc x\n<=-suc zero = o' oz\n<=-suc (suc x) = os (<=-suc x)\n\nn<=m\u2192sucn<=sucm : {n m : Nat} \u2192 n <= m -> suc n <= suc m\nn<=m\u2192sucn<=sucm oz = os oz\nn<=m\u2192sucn<=sucm (os n<=m) = os (n<=m\u2192sucn<=sucm n<=m)\nn<=m\u2192sucn<=sucm (o' n<=m) = o' (n<=m\u2192sucn<=sucm n<=m)\n\nsucsucn<=m\u2192sucn<=m : {n m : Nat} -> suc (suc n) <= m -> suc n <= m\nsucsucn<=m\u2192sucn<=m (os sucn<=m) = o' sucn<=m\nsucsucn<=m\u2192sucn<=m (o' (os xxx)) = o' (o' xxx)\nsucsucn<=m\u2192sucn<=m (o' (o' xxx)) = o' (o' (sucsucn<=m\u2192sucn<=m xxx))\n\nsucn<=m\u2192sucn<=sucm : {n m : Nat} -> suc n <= m -> suc n <= suc m\nsucn<=m\u2192sucn<=sucm (os p) = os (o' p)\nsucn<=m\u2192sucn<=sucm (o' p) = o' (sucn<=m\u2192sucn<=sucm p)\n\nzero<=m : {m : Nat} -> zero <= m\nzero<=m {zero} = oz\nzero<=m {suc m} = o' zero<=m\n\nsucn<=sucm\u2192n<=m : {n m : Nat} -> suc n <= suc m -> n <= m\nsucn<=sucm\u2192n<=m {zero} {zero} p = oz\nsucn<=sucm\u2192n<=m {zero} {suc m} (os p) = o' (zero<=m {m})\nsucn<=sucm\u2192n<=m {zero} {suc m} (o' p) = o' (zero<=m {m})\nsucn<=sucm\u2192n<=m {suc n} {zero} (os ())\nsucn<=sucm\u2192n<=m {suc n} {zero} (o' ())\nsucn<=sucm\u2192n<=m {suc n} {suc m} (os p) = p\nsucn<=sucm\u2192n<=m {suc n} {suc m} (o' p) = o' (sucn<=sucm\u2192n<=m p)\n\n-- Find all the values in each of the following <= types.\n-- This is a good opportunity to learn to use C-c C-a with the -l option\n-- (a.k.a. \"google the type\" without \"I feel lucky\")\n-- The -s n option also helps.\n\n--??--1.7-(1)-----------------------------------------------------------------\n\nall0<=4 : Vec (0 <= 4) 1\nall0<=4 = o' (o' (o' (o' oz))) ,- []\n\nall1<=4 : Vec (1 <= 4) 1\nall1<=4 = os (o' (o' (o' oz))) ,- []\n\nall2<=4 : Vec (2 <= 4) 1\nall2<=4 = os (os (o' (o' oz))) ,- []\n\nall3<=4 : Vec (3 <= 4) 1\nall3<=4 = os (os (os (o' oz))) ,- []\n\nall4<=4 : Vec (4 <= 4) 1\nall4<=4 = os (os (os (os oz))) ,- []\n\n-- Prove the following. A massive case analysis \"rant\" is fine.\n\nno5<=4 : 5 <= 4 -> Zero\nno5<=4 (os (os (os (os ()))))\nno5<=4 (os (os (os (o' ()))))\nno5<=4 (os (os (o' (os ()))))\nno5<=4 (os (os (o' (o' ()))))\nno5<=4 (os (o' (os (os ()))))\nno5<=4 (os (o' (os (o' ()))))\nno5<=4 (os (o' (o' (os ()))))\nno5<=4 (os (o' (o' (o' ()))))\nno5<=4 (o' (os (os (os ()))))\nno5<=4 (o' (os (os (o' ()))))\nno5<=4 (o' (os (o' (os ()))))\nno5<=4 (o' (os (o' (o' ()))))\nno5<=4 (o' (o' (os (os ()))))\nno5<=4 (o' (o' (os (o' ()))))\nno5<=4 (o' (o' (o' (os ()))))\nno5<=4 (o' (o' (o' (o' ()))))\n\n--??--------------------------------------------------------------------------\n\n\n------------------------------------------------------------------------------\n-- Order-Preserving Embeddings Select From Vectors\n------------------------------------------------------------------------------\n\n-- Use n <= m to encode the choice of n elements from an m-Vector.\n-- The os constructor tells you to take the next element of the vector;\n-- the o' constructor tells you to omit the next element of the vector.\n\n--??--1.8-(2)-----------------------------------------------------------------\n\n_ n <= m -> Vec X m\n -> Vec X n\noz Y)\n {n m : Nat}(th : n <= m)(xs : Vec X m) ->\n vMap f (th n <= n\noi {zero} = oz\noi {suc n} = os oi\n\noe : {n : Nat} -> 0 <= n\noe {zero} = oz\noe {suc n} = o' oe\n\nvnoi : Vec Nat 4\nvnoi = oi th == oe\noeUnique oz = refl oz\noeUnique (o' i) rewrite oeUnique i = refl (o' oe)\n\n--??--------------------------------------------------------------------------\n\n\n-- Show that there are no thinnings of form big <= small (TRICKY)\n-- Then show that all the identity thinnings are equal to yours.\n-- Note that you can try the second even if you haven't finished the first.\n-- HINT: you WILL need to expose the invisible numbers.\n-- HINT: check CS410-Prelude for a reminder of >=\n\n--??--1.11-(3)----------------------------------------------------------------\n\noTooBig : {n m : Nat} -> n >= m -> suc n <= m -> Zero\noTooBig {zero} {zero} n>=m ()\noTooBig {zero} {suc m} n>=m (os th) = n>=m\noTooBig {zero} {suc m} n>=m (o' th) = n>=m\noTooBig {suc n} {suc m} n>=m (os th) = oTooBig n>=m th\noTooBig {suc n} {suc m} n>=m (o' th) = oTooBig {n} {m} n>=m (sucsucn<=m\u2192sucn<=m th)\n\noiUnique : {n : Nat}(n<=n : n <= n) -> n<=n == oi\noiUnique oz = refl oz\noiUnique (os n<=n) rewrite oiUnique n<=n = refl (os oi)\noiUnique {suc n} (o' sucn<=n)\n with oTooBig (refl->= n) sucn<=n\n... | ()\n\n--??--------------------------------------------------------------------------\n\n\n-- Show that the identity thinning selects the whole vector\n\n--??--1.12-(1)----------------------------------------------------------------\n\nid- (oi >1_ : {p n m : Nat} -> p <= n -> n <= m -> p <= m\np<=zero o>>1 oz = p<=zero\np<=sucn o>>1 os n<=m = trans-<= p<=sucn (n<=m\u2192sucn<=sucm n<=m)\np<=n o>>1 o' n<=m = trans-<= p<=n (o' n<=m)\n\n-- 2nd attempt - but things further down get stuck\n_o>>2_ : {p n m : Nat} -> p <= n -> n <= m -> p <= m\noz o>>2 oz = oz\nos n<=m\u2081 o>>2 os m\u2081<=m = os (n<=m\u2081 o>>2 m\u2081<=m)\nos n<=m\u2081 o>>2 o' sucm\u2081<=m = os (o' n<=m\u2081 o>>2 sucm\u2081<=m)\no' p<=m\u2081 o>>2 os m\u2081<=m = o' (p<=m\u2081 o>>2 m\u2081<=m)\no' p<=m\u2081 o>>2 o' sucm\u2081<=m = o' (o' p<=m\u2081 o>>2 sucm\u2081<=m)\noz o>>2 o' zero<=m = o' (oz o>>2 zero<=m)\n\n-- https:\/\/github.com\/laMudri\/thinning\/blob\/master\/src\/Data\/Thinning.agda\n_o>>_ : \u2200 {m n o} \u2192 m <= n \u2192 n <= o \u2192 m <= o\nm<=z o>> oz = m<=z\nos \u03b8 o>> os \u03c6 = os (\u03b8 o>> \u03c6)\no' \u03b8 o>> os \u03c6 = o' (\u03b8 o>> \u03c6)\n\u03b8 o>> o' \u03c6 = o' (\u03b8 o>> \u03c6)\n\n\n-- empty thinning returns an empty vector\noe- (oe \n {X : Set}(xs : Vec X m) ->\n ((p<=n o>> n<=m) > : {n m : Nat}(n<=m : n <= m) -> (oi o>> n<=m) == n<=m\nidThen-o>> oz = refl oz\nidThen-o>> (os n<=m) = cong os (idThen-o>> n<=m) -- rewrite idThen-o>> n<=m = refl (os n<=m)\nidThen-o>> (o' n<=m) = cong o' (idThen-o>> n<=m)\n\nidAfter-o>> : {n m : Nat}(n<=m : n <= m) -> (n<=m o>> oi) == n<=m\nidAfter-o>> oz = refl oz\nidAfter-o>> (os n<=m) = cong os (idAfter-o>> n<=m) -- rewrite idAfter-o>> n<=m = refl (os n<=m)\nidAfter-o>> (o' n<=m) = cong o' (idAfter-o>> n<=m) -- rewrite idAfter-o>> n<=m = refl (o' n<=m)\n\nassoc-o>> : {q p n m : Nat}(q<=p : q <= p)(p<=n : p <= n)(n<=m : n <= m) ->\n ((q<=p o>> p<=n) o>> n<=m) == (q<=p o>> (p<=n o>> n<=m))\nassoc-o>> q<=p p<=n oz = refl (q<=p o>> p<=n)\nassoc-o>> q<=p p<=n (o' n<=m) = cong o' (assoc-o>> q<=p p<=n n<=m)\nassoc-o>> q<=p (o' p<=n) (os n<=m) = cong o' (assoc-o>> q<=p p<=n n<=m)\nassoc-o>> (o' q<=p) (os p<=n) (os n<=m) = cong o' (assoc-o>> q<=p p<=n n<=m)\nassoc-o>> (os q<=p) (os p<=n) (os n<=m) = cong os (assoc-o>> q<=p p<=n n<=m)\n\n\n--??--------------------------------------------------------------------------\n\n\n------------------------------------------------------------------------------\n-- Vectors as Arrays\n------------------------------------------------------------------------------\n\n-- We can use 1 <= n as the type of bounded indices into a vector and do\n-- a kind of \"array projection\". First we select a 1-element vector from\n-- the n-element vector, then we take its head to get the element out.\n\nvProject : {n : Nat}{X : Set} -> Vec X n -> 1 <= n -> X\nvProject xs i = vHead (i (1 <= n -> X) -> Vec X n\nvTabulate {zero} _ = []\nvTabulate {suc n} f = f (os (zero<=m {n})) ,- (vTabulate (\u03bb 1<=n \u2192 f (o' 1<=n)))\n\nvt : Vec Nat 4\nvt = vTabulate f\n where\n f : {n : Nat} -> (1 <= n) \u2192 Nat\n f (os _) = 1\n f (o' x) = 1 +N f x\n_ : vt == (1 ,- 2 ,- 3 ,- 4 ,- [])\n_ = refl (1 ,- 2 ,- 3 ,- 4 ,- [])\n\n-- This should be easy if vTabulate is correct.\nvTabulateProjections : {n : Nat}{X : Set}(xs : Vec X n) ->\n vTabulate (vProject xs) == xs\nvTabulateProjections [] = refl []\nvTabulateProjections (x ,- xs) = cong (x ,-_) (vTabulateProjections xs)\n\n-- HINT: oeUnique\nvProjectFromTable : {n : Nat}{X : Set}(f : 1 <= n -> X)(i : 1 <= n) ->\n vProject (vTabulate f) i == f i\nvProjectFromTable {suc n} f (os 0<=n)\n rewrite oeUnique 0<=n | oeUnique (zero<=m {n})\n = refl (f (os oe))\nvProjectFromTable {suc n} f (o' 1<=n)\n = vProjectFromTable (\u03bb 1<=n \u2192 f (o' 1<=n)) 1<=n\n\n--??--------------------------------------------------------------------------\n","old_contents":"{-# OPTIONS --allow-unsolved-metas #-}\n------------------------------------------------------------------------------\n------------------------------------------------------------------------------\n-- CS410 2017\/18 Exercise 1 VECTORS AND FRIENDS (worth 25%)\n------------------------------------------------------------------------------\n------------------------------------------------------------------------------\n\n-- NOTE (19\/9\/17) This file is currently incomplete: more will arrive on\n-- GitHub.\n\n-- MARK SCHEME (transcribed from paper): the (m) numbers add up to slightly\n-- more than 25, so should be taken as the maximum number of marks losable on\n-- the exercise. In fact, I did mark it negatively, but mostly because it was\n-- done so well (with Agda's help) that it was easier to find the errors.\n\n\n------------------------------------------------------------------------------\n-- Dependencies\n------------------------------------------------------------------------------\n\nopen import CS410-Prelude\n\ncong : \u2200 {A B : Set} {x y : A}\n \u2192 (f : A \u2192 B)\n \u2192 x == y\n ----------\n \u2192 f x == f y\ncong f (refl x) = refl (f x)\n\n------------------------------------------------------------------------------\n-- Vectors\n------------------------------------------------------------------------------\n\ndata Vec (X : Set) : Nat -> Set where -- like lists, but length-indexed\n [] : Vec X zero\n _,-_ : {n : Nat} -> X -> Vec X n -> Vec X (suc n)\ninfixr 4 _,-_ -- the \"cons\" operator associates to the right\n\n-- I like to use the asymmetric ,- to remind myself that the element is to\n-- the left and the rest of the list is to the right.\n\n-- Vectors are useful when there are important length-related safety\n-- properties.\n\n------------------------------------------------------------------------------\n-- Heads and Tails\n------------------------------------------------------------------------------\n\n-- We can rule out nasty head and tail errors by insisting on nonemptiness!\n\n--??--1.1-(2)-----------------------------------------------------------------\n\nvHead : {X : Set}{n : Nat} -> Vec X (suc n) -> X\nvHead (x ,- _) = x\n\nvTail : {X : Set}{n : Nat} -> Vec X (suc n) -> Vec X n\nvTail (_ ,- xs) = xs\n\nvHeadTailFact : {X : Set}{n : Nat}(xs : Vec X (suc n)) ->\n (vHead xs ,- vTail xs) == xs\nvHeadTailFact (x ,- xs) = refl (x ,- xs)\n\n--??--------------------------------------------------------------------------\n\n\n------------------------------------------------------------------------------\n-- Concatenation and its Inverse\n------------------------------------------------------------------------------\n\n--??--1.2-(2)-----------------------------------------------------------------\n\n_+V_ : {X : Set}{m n : Nat} -> Vec X m -> Vec X n -> Vec X (m +N n)\n[] +V ys = ys\n(x ,- xs) +V ys = x ,- xs +V ys\ninfixr 4 _+V_\n\nvChop : {X : Set}(m : Nat){n : Nat} -> Vec X (m +N n) -> Vec X m * Vec X n\nvChop zero xs = [] , xs\nvChop (suc m) (x ,- xs)\n with vChop m xs\n... | vm , vn\n = (x ,- vm) , vn\n\nvChopAppendFact : {X : Set}{m n : Nat}(xs : Vec X m)(ys : Vec X n) ->\n vChop m (xs +V ys) == (xs , ys)\nvChopAppendFact [] ys = refl ([] , ys)\nvChopAppendFact (x ,- xs) ys rewrite vChopAppendFact xs ys = refl ((x ,- xs) , ys)\n\n--??--------------------------------------------------------------------------\n\n\n------------------------------------------------------------------------------\n-- Map, take I\n------------------------------------------------------------------------------\n\n-- Implement the higher-order function that takes an operation on\n-- elements and does it to each element of a vector. Use recursion\n-- on the vector.\n-- Note that the type tells you the size remains the same.\n\n-- Show that if the elementwise function \"does nothing\", neither does\n-- its vMap. \"map of identity is identity\"\n\n-- Show that two vMaps in a row can be collapsed to just one, or\n-- \"composition of maps is map of compositions\"\n\n--??--1.3-(2)-----------------------------------------------------------------\n\nvMap : {X Y : Set} -> (X -> Y) -> {n : Nat} -> Vec X n -> Vec Y n\nvMap f [] = []\nvMap f (x ,- xs) = f x ,- vMap f xs\n\nvMapIdFact : {X : Set}{f : X -> X}(feq : (x : X) -> f x == x) ->\n {n : Nat}(xs : Vec X n) -> vMap f xs == xs\nvMapIdFact feq [] = refl []\nvMapIdFact feq (x ,- xs)\n rewrite vMapIdFact feq xs\n | feq x\n = refl (x ,- xs)\n\nvMapCpFact : {X Y Z : Set}{f : Y -> Z}{g : X -> Y}{h : X -> Z}\n (heq : (x : X) -> f (g x) == h x) ->\n {n : Nat}(xs : Vec X n) ->\n vMap f (vMap g xs) == vMap h xs\nvMapCpFact heq [] = refl []\nvMapCpFact {_}{_}{_}{f} {g} {h} heq (x ,- xs)\n rewrite heq x\n | vMapCpFact {_}{_}{_}{f} {g} {h} heq xs\n = refl (h x ,- vMap h xs)\n\n--??--------------------------------------------------------------------------\n\n\n------------------------------------------------------------------------------\n-- vMap and +V\n------------------------------------------------------------------------------\n\n-- Show that if you've got two vectors of Xs and a function from X to Y,\n-- and you want to concatenate and map, it doesn't matter which you do\n-- first.\n\n--??--1.4-(1)-----------------------------------------------------------------\n\nvMap+VFact : {X Y : Set}(f : X -> Y) ->\n {m n : Nat}(xs : Vec X m)(xs' : Vec X n) ->\n vMap f (xs +V xs') == (vMap f xs +V vMap f xs')\nvMap+VFact f [] xs' = refl (vMap f xs')\nvMap+VFact f (x ,- xs) xs'\n rewrite vMap+VFact f xs xs'\n = refl (f x ,- vMap f xs +V vMap f xs')\n\n--??--------------------------------------------------------------------------\n\n-- Think about what you could prove, relating vMap with vHead, vTail, vChop...\n-- Now google \"Philip Wadler\" \"Theorems for Free\"\n\n-- TODO\n\n------------------------------------------------------------------------------\n-- Applicative Structure (giving mapping and zipping cheaply)\n------------------------------------------------------------------------------\n\n--??--1.5-(2)-----------------------------------------------------------------\n\n-- HINT: you will need to override the default invisibility of n to do this.\n-- HC : replicate\nvPure : {X : Set} -> X -> {n : Nat} -> Vec X n\nvPure x {zero} = []\nvPure x {suc n} = x ,- vPure x {n}\n\n_$V_ : {X Y : Set}{n : Nat} -> Vec (X -> Y) n -> Vec X n -> Vec Y n\n[] $V [] = []\nf ,- fs $V x ,- xs = f x ,- (fs $V xs)\ninfixl 3 _$V_ -- \"Application associates to the left,\n -- rather as we all did in the sixties.\" (Roger Hindley)\n\n-- Pattern matching and recursion are forbidden for the next two tasks.\n\n-- implement vMap again, but as a one-liner\nvec : {X Y : Set} -> (X -> Y) -> {n : Nat} -> Vec X n -> Vec Y n\nvec f xs = vPure f $V xs\n\n-- implement the operation which pairs up corresponding elements\nvZip : {X Y : Set}{n : Nat} -> Vec X n -> Vec Y n -> Vec (X * Y) n\nvZip xs ys = vec (_,_) xs $V ys\n\n--??--------------------------------------------------------------------------\n\n\n------------------------------------------------------------------------------\n-- Applicative Laws\n------------------------------------------------------------------------------\n\n-- According to \"Applicative programming with effects\" by\n-- Conor McBride and Ross Paterson\n-- some laws should hold for applicative functors.\n-- Check that this is the case.\n\n--??--1.6-(2)-----------------------------------------------------------------\n\nvIdentity : {X : Set}{f : X -> X}(feq : (x : X) -> f x == x) ->\n {n : Nat}(xs : Vec X n) -> (vPure f $V xs) == xs\nvIdentity feq [] = refl []\nvIdentity feq (x ,- xs) rewrite vIdentity feq xs | feq x = refl (x ,- xs)\n\nvHomomorphism : {X Y : Set}(f : X -> Y)(x : X) ->\n {n : Nat} -> (vPure f $V vPure x) == vPure (f x) {n}\nvHomomorphism f x {zero} = refl []\nvHomomorphism f x {suc n} rewrite vHomomorphism f x {n} = refl (f x ,- vPure (f x))\n\nvInterchange : {X Y : Set}{n : Nat}(fs : Vec (X -> Y) n)(x : X) ->\n (fs $V vPure x) == (vPure (_$ x) $V fs)\nvInterchange [] x = refl []\nvInterchange (f ,- fs) x rewrite vInterchange fs x = refl (f x ,- (vPure (\u03bb x\u2192y \u2192 x\u2192y x) $V fs))\n\nvComposition : {X Y Z : Set}{n : Nat}\n (fs : Vec (Y -> Z) n)(gs : Vec (X -> Y) n)(xs : Vec X n) ->\n (vPure _<<_ $V fs $V gs $V xs) == (fs $V (gs $V xs))\nvComposition [] [] [] = refl []\nvComposition (f ,- fs) (g ,- gs) (x ,- xs)\n rewrite vComposition fs gs xs\n = refl (f (g x) ,- (fs $V (gs $V xs)))\n\n--??--------------------------------------------------------------------------\n\n\n------------------------------------------------------------------------------\n-- Order-Preserving Embeddings (also known in the business as \"thinnings\")\n------------------------------------------------------------------------------\n\n-- What have these to do with Pascal's Triangle?\n\n-- how to choose N things from M things\ndata _<=_ : Nat -> Nat -> Set where\n oz : zero <= zero -- stop\n os : {n m : Nat} -> n <= m -> suc n <= suc m -- take this one and keep going\n o' : {n m : Nat} -> n <= m -> n <= suc m -- skip this one and keep going\n\nrefl-<= : (n : Nat) -> n <= n\nrefl-<= zero = oz\nrefl-<= (suc n) = os (refl-<= n)\n\ntrans-<= : {n m p : Nat} -> n <= m -> m <= p -> n <= p\ntrans-<= oz zero<=p = zero<=p\ntrans-<= (os n<=m) (os sucm<=p) = os (trans-<= n<=m sucm<=p)\ntrans-<= (os n<=m) (o' sucm<=p) = os (trans-<= (o' n<=m) sucm<=p)\ntrans-<= (o' n<=m) (os m<=p) = o' (trans-<= n<=m m<=p)\ntrans-<= (o' n<=m) (o' m<=p) = o' (trans-<= (o' n<=m) m<=p)\n\n<=-suc : (x : Nat) -> x <= suc x\n<=-suc zero = o' oz\n<=-suc (suc x) = os (<=-suc x)\n\nn<=m\u2192sucn<=sucm : {n m : Nat} \u2192 n <= m -> suc n <= suc m\nn<=m\u2192sucn<=sucm oz = os oz\nn<=m\u2192sucn<=sucm (os n<=m) = os (n<=m\u2192sucn<=sucm n<=m)\nn<=m\u2192sucn<=sucm (o' n<=m) = o' (n<=m\u2192sucn<=sucm n<=m)\n\nsucsucn<=m\u2192sucn<=m : {n m : Nat} -> suc (suc n) <= m -> suc n <= m\nsucsucn<=m\u2192sucn<=m (os sucn<=m) = o' sucn<=m\nsucsucn<=m\u2192sucn<=m (o' (os xxx)) = o' (o' xxx)\nsucsucn<=m\u2192sucn<=m (o' (o' xxx)) = o' (o' (sucsucn<=m\u2192sucn<=m xxx))\n\nsucn<=m\u2192sucn<=sucm : {n m : Nat} -> suc n <= m -> suc n <= suc m\nsucn<=m\u2192sucn<=sucm (os p) = os (o' p)\nsucn<=m\u2192sucn<=sucm (o' p) = o' (sucn<=m\u2192sucn<=sucm p)\n\nzero<=m : {m : Nat} -> zero <= m\nzero<=m {zero} = oz\nzero<=m {suc m} = o' zero<=m\n\nsucn<=sucm\u2192n<=m : {n m : Nat} -> suc n <= suc m -> n <= m\nsucn<=sucm\u2192n<=m {zero} {zero} p = oz\nsucn<=sucm\u2192n<=m {zero} {suc m} (os p) = o' (zero<=m {m})\nsucn<=sucm\u2192n<=m {zero} {suc m} (o' p) = o' (zero<=m {m})\nsucn<=sucm\u2192n<=m {suc n} {zero} (os ())\nsucn<=sucm\u2192n<=m {suc n} {zero} (o' ())\nsucn<=sucm\u2192n<=m {suc n} {suc m} (os p) = p\nsucn<=sucm\u2192n<=m {suc n} {suc m} (o' p) = o' (sucn<=sucm\u2192n<=m p)\n\n-- Find all the values in each of the following <= types.\n-- This is a good opportunity to learn to use C-c C-a with the -l option\n-- (a.k.a. \"google the type\" without \"I feel lucky\")\n-- The -s n option also helps.\n\n--??--1.7-(1)-----------------------------------------------------------------\n\nall0<=4 : Vec (0 <= 4) 1\nall0<=4 = o' (o' (o' (o' oz))) ,- []\n\nall1<=4 : Vec (1 <= 4) 1\nall1<=4 = os (o' (o' (o' oz))) ,- []\n\nall2<=4 : Vec (2 <= 4) 1\nall2<=4 = os (os (o' (o' oz))) ,- []\n\nall3<=4 : Vec (3 <= 4) 1\nall3<=4 = os (os (os (o' oz))) ,- []\n\nall4<=4 : Vec (4 <= 4) 1\nall4<=4 = os (os (os (os oz))) ,- []\n\n-- Prove the following. A massive case analysis \"rant\" is fine.\n\nno5<=4 : 5 <= 4 -> Zero\nno5<=4 (os (os (os (os ()))))\nno5<=4 (os (os (os (o' ()))))\nno5<=4 (os (os (o' (os ()))))\nno5<=4 (os (os (o' (o' ()))))\nno5<=4 (os (o' (os (os ()))))\nno5<=4 (os (o' (os (o' ()))))\nno5<=4 (os (o' (o' (os ()))))\nno5<=4 (os (o' (o' (o' ()))))\nno5<=4 (o' (os (os (os ()))))\nno5<=4 (o' (os (os (o' ()))))\nno5<=4 (o' (os (o' (os ()))))\nno5<=4 (o' (os (o' (o' ()))))\nno5<=4 (o' (o' (os (os ()))))\nno5<=4 (o' (o' (os (o' ()))))\nno5<=4 (o' (o' (o' (os ()))))\nno5<=4 (o' (o' (o' (o' ()))))\n\n--??--------------------------------------------------------------------------\n\n\n------------------------------------------------------------------------------\n-- Order-Preserving Embeddings Select From Vectors\n------------------------------------------------------------------------------\n\n-- Use n <= m to encode the choice of n elements from an m-Vector.\n-- The os constructor tells you to take the next element of the vector;\n-- the o' constructor tells you to omit the next element of the vector.\n\n--??--1.8-(2)-----------------------------------------------------------------\n\n_ n <= m -> Vec X m\n -> Vec X n\noz Y)\n {n m : Nat}(th : n <= m)(xs : Vec X m) ->\n vMap f (th n <= n\noi {zero} = oz\noi {suc n} = os oi\n\noe : {n : Nat} -> 0 <= n\noe {zero} = oz\noe {suc n} = o' oe\n\nvnoi : Vec Nat 4\nvnoi = oi th == oe\noeUnique oz = refl oz\noeUnique (o' i) rewrite oeUnique i = refl (o' oe)\n\n--??--------------------------------------------------------------------------\n\n\n-- Show that there are no thinnings of form big <= small (TRICKY)\n-- Then show that all the identity thinnings are equal to yours.\n-- Note that you can try the second even if you haven't finished the first.\n-- HINT: you WILL need to expose the invisible numbers.\n-- HINT: check CS410-Prelude for a reminder of >=\n\n--??--1.11-(3)----------------------------------------------------------------\n\noTooBig : {n m : Nat} -> n >= m -> suc n <= m -> Zero\noTooBig {zero} {zero} n>=m ()\noTooBig {zero} {suc m} n>=m (os th) = n>=m\noTooBig {zero} {suc m} n>=m (o' th) = n>=m\noTooBig {suc n} {suc m} n>=m (os th) = oTooBig n>=m th\noTooBig {suc n} {suc m} n>=m (o' th) = oTooBig {n} {m} n>=m (sucsucn<=m\u2192sucn<=m th)\n\noiUnique : {n : Nat}(n<=n : n <= n) -> n<=n == oi\noiUnique oz = refl oz\noiUnique (os n<=n) rewrite oiUnique n<=n = refl (os oi)\noiUnique {suc n} (o' sucn<=n)\n with oTooBig (refl->= n) sucn<=n\n... | ()\n\n--??--------------------------------------------------------------------------\n\n\n-- Show that the identity thinning selects the whole vector\n\n--??--1.12-(1)----------------------------------------------------------------\n\nid- (oi >1_ : {p n m : Nat} -> p <= n -> n <= m -> p <= m\np<=zero o>>1 oz = p<=zero\np<=sucn o>>1 os n<=m = trans-<= p<=sucn (n<=m\u2192sucn<=sucm n<=m)\np<=n o>>1 o' n<=m = trans-<= p<=n (o' n<=m)\n\n-- 2nd attempt - but things further down get stuck\n_o>>2_ : {p n m : Nat} -> p <= n -> n <= m -> p <= m\noz o>>2 oz = oz\nos n<=m\u2081 o>>2 os m\u2081<=m = os (n<=m\u2081 o>>2 m\u2081<=m)\nos n<=m\u2081 o>>2 o' sucm\u2081<=m = os (o' n<=m\u2081 o>>2 sucm\u2081<=m)\no' p<=m\u2081 o>>2 os m\u2081<=m = o' (p<=m\u2081 o>>2 m\u2081<=m)\no' p<=m\u2081 o>>2 o' sucm\u2081<=m = o' (o' p<=m\u2081 o>>2 sucm\u2081<=m)\noz o>>2 o' zero<=m = o' (oz o>>2 zero<=m)\n\n-- https:\/\/github.com\/laMudri\/thinning\/blob\/master\/src\/Data\/Thinning.agda\n_o>>_ : \u2200 {m n o} \u2192 m <= n \u2192 n <= o \u2192 m <= o\nm<=z o>> oz = m<=z\nos \u03b8 o>> os \u03c6 = os (\u03b8 o>> \u03c6)\no' \u03b8 o>> os \u03c6 = o' (\u03b8 o>> \u03c6)\n\u03b8 o>> o' \u03c6 = o' (\u03b8 o>> \u03c6)\n\n\n-- empty thinning returns an empty vector\noe- (oe \n {X : Set}(xs : Vec X m) ->\n ((p<=n o>> n<=m) > : {n m : Nat}(n<=m : n <= m) -> (oi o>> n<=m) == n<=m\nidThen-o>> oz = refl oz\nidThen-o>> (os n<=m) = cong os (idThen-o>> n<=m) -- rewrite idThen-o>> n<=m = refl (os n<=m)\nidThen-o>> (o' n<=m) = cong o' (idThen-o>> n<=m)\n\nidAfter-o>> : {n m : Nat}(n<=m : n <= m) -> (n<=m o>> oi) == n<=m\nidAfter-o>> oz = refl oz\nidAfter-o>> (os n<=m) = cong os (idAfter-o>> n<=m) -- rewrite idAfter-o>> n<=m = refl (os n<=m)\nidAfter-o>> (o' n<=m) = cong o' (idAfter-o>> n<=m) -- rewrite idAfter-o>> n<=m = refl (o' n<=m)\n\nassoc-o>> : {q p n m : Nat}(q<=p : q <= p)(p<=n : p <= n)(n<=m : n <= m) ->\n ((q<=p o>> p<=n) o>> n<=m) == (q<=p o>> (p<=n o>> n<=m))\nassoc-o>> q<=p p<=n oz = refl (q<=p o>> p<=n)\nassoc-o>> q<=p p<=n (o' n<=m) = cong o' (assoc-o>> q<=p p<=n n<=m)\nassoc-o>> q<=p (o' p<=n) (os n<=m) = cong o' (assoc-o>> q<=p p<=n n<=m)\nassoc-o>> (o' q<=p) (os p<=n) (os n<=m) = cong o' (assoc-o>> q<=p p<=n n<=m)\nassoc-o>> (os q<=p) (os p<=n) (os n<=m) = cong os (assoc-o>> q<=p p<=n n<=m)\n\n\n--??--------------------------------------------------------------------------\n\n\n------------------------------------------------------------------------------\n-- Vectors as Arrays\n------------------------------------------------------------------------------\n\n-- We can use 1 <= n as the type of bounded indices into a vector and do\n-- a kind of \"array projection\". First we select a 1-element vector from\n-- the n-element vector, then we take its head to get the element out.\n\nvProject : {n : Nat}{X : Set} -> Vec X n -> 1 <= n -> X\nvProject xs i = vHead (i (1 <= n -> X) -> Vec X n\nvTabulate {zero} _ = []\nvTabulate {suc n} f = f (os (zero<=m {n})) ,- (vTabulate (\u03bb 1<=n \u2192 f (o' 1<=n)))\n\nvt : Vec Nat 4\nvt = vTabulate f\n where\n f : (1 <= 4) \u2192 Nat\n f (os _) = 1\n f (o' (os _)) = 2\n f (o' (o' (os _))) = 3\n f (o' (o' (o' (os _)))) = 4\n_ : vt == (1 ,- 2 ,- 3 ,- 4 ,- [])\n_ = refl (1 ,- 2 ,- 3 ,- 4 ,- [])\n\n-- This should be easy if vTabulate is correct.\nvTabulateProjections : {n : Nat}{X : Set}(xs : Vec X n) ->\n vTabulate (vProject xs) == xs\nvTabulateProjections [] = refl []\nvTabulateProjections (x ,- xs) = cong (x ,-_) (vTabulateProjections xs)\n\n-- HINT: oeUnique\nvProjectFromTable : {n : Nat}{X : Set}(f : 1 <= n -> X)(i : 1 <= n) ->\n vProject (vTabulate f) i == f i\nvProjectFromTable f i = {!!}\n\n--??--------------------------------------------------------------------------\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"00d88d736e355f710a6f1506eb8535eeb024b409","subject":"updated the elgamal example","message":"updated the elgamal example\n","repos":"crypto-agda\/crypto-agda","old_file":"elgamal.agda","new_file":"elgamal.agda","new_contents":"{-# OPTIONS --copatterns #-}\nopen import Type\nopen import Function\nopen import Data.Product\nopen import Data.Unit\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc; Fin\u25b9\u2115) renaming (#_ to ##_)\nopen import Data.Nat.NP hiding (_^_; _==_)\nopen import Data.Bit\nopen import Data.Two\nopen import Data.Bits hiding (_==_)\nopen import Relation.Binary.PropositionalEquality.NP as \u2261\nimport Game.DDH\nimport Game.IND-CPA\nimport Cipher.ElGamal.Generic\nopen import Explore.Type\nopen import Explore.Explorable\nopen import Explore.Explorable.Sum renaming (\u03bcBit to \u03bc\ud835\udfda)\nopen import Explore.Explorable.Product\nopen import Explore.Explorable.Fin\nopen import Relation.Binary.NP\n\nmodule elgamal where\n\ndata `\u2605 : \u2605 where\n `\u22a4 : `\u2605\n `X : `\u2605\n _`\u00d7_ : `\u2605 \u2192 `\u2605 \u2192 `\u2605\ninfixr 2 _`\u00d7_\n\nmodule Univ (X : \u2605) where\n El : `\u2605 \u2192 \u2605\n El `\u22a4 = \u22a4\n El `X = X\n El (u\u2080 `\u00d7 u\u2081) = El u\u2080 \u00d7 El u\u2081\n\n record \u21ba (R : `\u2605) (A : \u2605) : \u2605 where\n constructor mk\n field\n run\u21ba : El R \u2192 A\n open \u21ba public\n\n EXP : (R : `\u2605) \u2192 \u2605\n EXP R = \u21ba R Bit\n\n Det : \u2605 \u2192 \u2605\n Det = \u21ba `\u22a4\n\n \u03bcU : Explorable X \u2192 \u2200 u \u2192 Explorable (El u)\n \u03bcU \u03bcX `\u22a4 = \u03bc\u22a4\n \u03bcU \u03bcX `X = \u03bcX\n \u03bcU \u03bcX (u\u2080 `\u00d7 u\u2081) = \u03bcU \u03bcX u\u2080 \u00d7-\u03bc \u03bcU \u03bcX u\u2081\n\nmodule \u2124q-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u03bc\u2124q : Explorable \u2124q)\n (\u229e-stable : \u2200 x \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u229e_ x))\n where\n\n -- open Sum\n open Univ \u2124q public\n open `\u2605 public renaming (`X to `\u2124q)\n\n #_ : \u2200 {u} \u2192 \u21ba u Bit \u2192 \u2115\n #_ {u} f = count (\u03bcU \u03bc\u2124q u) (run\u21ba f)\n\n #q_ : Count \u2124q\n #q_ = count \u03bc\u2124q\n\n \u2047 : \u2200 R \u2192 \u21ba R (El R)\n run\u21ba (\u2047 _) = id\n\n pure\u21ba : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n run\u21ba (pure\u21ba x) r = x -- turning r to _ produce an error\n\n \u27ea_\u27eb : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n \u27ea_\u27eb = pure\u21ba\n\n {-\n \u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n \u27ea_\u27eb\u1d30 = pure\u1d30\n -}\n\n map\u21ba : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n run\u21ba (map\u21ba f x) r = f (run\u21ba x r)\n\n infixl 4 _\u229b_\n _\u229b_ : \u2200 {R S A B} \u2192 \u21ba R (A \u2192 B) \u2192 \u21ba S A \u2192 \u21ba (R `\u00d7 S) B\n run\u21ba (af \u229b ax) rs = run\u21ba af (proj\u2081 rs) (run\u21ba ax (proj\u2082 rs))\n\n \u27ea_\u00b7_\u27eb : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n \u27ea f \u00b7 x \u27eb = map\u21ba f x\n\n \u27ea_\u00b7_\u00b7_\u27eb : \u2200 {A B C} {R S} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba R A \u2192 \u21ba S B \u2192 \u21ba (R `\u00d7 S) C\n \u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n _\u27e8\u229e\u27e9_ : \u2200 {R S} \u2192 \u21ba R \u2124q \u2192 \u21ba S \u2124q \u2192 \u21ba (R `\u00d7 S) \u2124q\n x \u27e8\u229e\u27e9 y = \u27ea _\u229e_ \u00b7 x \u00b7 y \u27eb\n\n \u27e8_\u229e\u27e9_ : \u2200 {R} \u2192 \u2124q \u2192 \u21ba R \u2124q \u2192 \u21ba R \u2124q\n \u27e8 x \u229e\u27e9 y = \u27ea _\u229e_ x \u00b7 y \u27eb\n\n infix 4 _\u2248\u21ba_ _\u2248\u1d2c_\n _\u2248\u21ba_ : \u2200 {R : `\u2605} (f g : EXP R) \u2192 \u2605\n _\u2248\u21ba_ = _\u2261_ on #_\n\n _\u2248\u1d2c_ : \u2200 {A R} (f g : \u21ba R A) \u2192 Set _\n _\u2248\u1d2c_ {A} f g = \u2200 (Adv : A \u2192 Bit) \u2192 \u27ea Adv \u00b7 f \u27eb \u2248\u21ba \u27ea Adv \u00b7 g \u27eb\n\n lem : \u2200 x \u2192 \u27e8 x \u229e\u27e9 (\u2047 `\u2124q) \u2248\u1d2c \u2047 _\n lem x Adv = sym (\u229e-stable x (Bit\u25b9\u2115 \u2218 Adv))\n\n -- \u2200 (A : \u2124q \u2192 Bit) \u2192 # (A \u2047)\n\nopen Fin.Modulo renaming (sucmod to [suc]; sucmod-inj to [suc]-inj)\n\n{-\nmodule \u2124q-implem (q-2 : \u2115) where\n q : \u2115\n q = 2 + q-2\n\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = zero\n\n [1] : \u2124q\n [1] = suc zero\n-}\nmodule \u2124q-implem (q-1 : \u2115) ([0]' [1]' : Fin (suc q-1)) where\n -- open Sum\n q : \u2115\n q = suc q-1\n\n \u2124q : \u2605\n \u2124q = Fin q\n\n \u03bc\u2124q : Explorable \u2124q\n \u03bc\u2124q = \u03bcFinSuc q-1\n\n sum\u2124q : Sum \u2124q\n sum\u2124q = sum \u03bc\u2124q\n\n [0] : \u2124q\n [0] = [0]'\n\n [1] : \u2124q\n [1] = [1]'\n\n [suc]-stable : SumStableUnder (sum \u03bc\u2124q) [suc]\n [suc]-stable = \u03bcFinSUI [suc] [suc]-inj\n\n _\u2115\u229e_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = m \u2115\u229e ([suc] n)\n\n \u2115\u229e-inj : \u2200 n {x y} \u2192 n \u2115\u229e x \u2261 n \u2115\u229e y \u2192 x \u2261 y\n \u2115\u229e-inj zero eq = eq\n \u2115\u229e-inj (suc n) eq = [suc]-inj (\u2115\u229e-inj n eq)\n\n \u2115\u229e-stable : \u2200 m \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u2115\u229e_ m)\n \u2115\u229e-stable m = \u03bcFinSUI (_\u2115\u229e_ m) (\u2115\u229e-inj m)\n\n _\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u229e n = Fin\u25b9\u2115 m \u2115\u229e n\n\n \u229e-inj : \u2200 m {x y} \u2192 m \u229e x \u2261 m \u229e y \u2192 x \u2261 y\n \u229e-inj m = \u2115\u229e-inj (Fin\u25b9\u2115 m)\n\n \u229e-stable : \u2200 m \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u229e_ m)\n \u229e-stable m = \u03bcFinSUI (_\u229e_ m) (\u229e-inj m)\n\n _\u2115\u22a0_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u22a0 n = [0]\n suc m \u2115\u22a0 n = n \u229e (m \u2115\u22a0 n)\n\n _\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u22a0 n = Fin\u25b9\u2115 m \u2115\u22a0 n\n\n _[^]\u2115_ : \u2124q \u2192 \u2115 \u2192 \u2124q\n m [^]\u2115 zero = [1]\n m [^]\u2115 suc n = m \u22a0 (m [^]\u2115 n)\n\n _[^]_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m [^] n = m [^]\u2115 (Fin\u25b9\u2115 n)\n\nmodule G-implem (p-1 q-1 : \u2115) (g' 0[p] 1[p] : Fin (suc p-1)) (0[q] 1[q] : Fin (suc q-1)) where\n open \u2124q-implem q-1 0[q] 1[q] public\n open \u2124q-implem p-1 0[p] 1[p] public using () renaming (\u2124q to G; _\u22a0_ to _\u2219_; _[^]\u2115_ to _^[p]_)\n\n g : G\n g = g'\n\n _^_ : G \u2192 \u2124q \u2192 G\n x ^ n = x ^[p] Fin\u25b9\u2115 n\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n {-\n g^-inj : \u2200 m n \u2192 g^ m \u2261 g^ n \u2192 m \u2261 n\n g^-inj = {!!}\n -}\n\nmodule G-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u03bc\u2124q : Explorable \u2124q)\n (\u229e-stable : \u2200 x \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u229e_ x))\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n where\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n open \u2124q-count \u2124q _\u229e_ \u03bc\u2124q \u229e-stable\n\n \u2047G : \u21ba `\u2124q G\n run\u21ba \u2047G x = g^ x\n\n #G : Count G\n #G f = #q (f \u2218 g^_)\n\n {-\n #G-\u2219 : \u2200 f m \u2192 #G (f \u2218 _\u2219_ m) \u2261 #G f\n #G-\u2219 f m = {!!}\n -}\n\nmodule El-Gamal-Generic\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (Message : \u2605)\n (_\u2219_ : G \u2192 Message \u2192 Message)\n\n -- Required for decryption\n (_\/_ : Message \u2192 G \u2192 Message)\n\n -- Required for the correctness proof\n (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n -- Required for the security proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : Explorable \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : Explorable R\u2090)\n where\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n -- g\u02e3 is the pk\n -- x is the sk\n\n R\u2093 : \u2605\n R\u2093 = \u2124q\n\n open Cipher.ElGamal.Generic Message \u2124q G g _^_ _\u2219_ _\/_\n\n functional-correctness : \u2200 x y m \u2192 Dec x (Enc (g^ x) m y) \u2261 m\n functional-correctness x y m rewrite comm-^ g x y | \/-\u2219 (g^ y ^ x) m = refl\n\n module IND-CPA = Game.IND-CPA PubKey SecKey Message CipherText R\u2091 R\u2096 R\u2090 R\u2093 KeyGen Enc\n open IND-CPA renaming (R to R')\n\n \u03bcR' : Explorable R'\n \u03bcR' = \u03bcR\u2090 \u00d7-\u03bc \u03bc\u2124q \u00d7-\u03bc \u03bc\u2124q \u00d7-\u03bc \u03bc\u2124q\n\n sumR' = sum \u03bcR'\n \n R = \ud835\udfda \u00d7 R'\n \u03bcR = \u03bc\ud835\udfda \u00d7-\u03bc \u03bcR'\n sumR = sum \u03bcR\n \n sumExtR\u2090 = sum-ext \u03bcR\u2090\n sumExt\u2124q = sum-ext \u03bc\u2124q\n sumHomR' = sum-hom \u03bcR'\n sumExtR' = sum-ext \u03bcR'\n \n IND-CPA-\u2141 : IND-CPA.Adv \u2192 R \u2192 \ud835\udfda\n IND-CPA-\u2141 A (b , r\u2090 , r\u2096 , r\u2091 , r\u2093) = IND-CPA.\u2141 b A (r\u2090 , r\u2096 , r\u2091 , r\u2093)\n \n module DDH = Game.DDH \u2124q G g _^_ (\ud835\udfda \u00d7 R\u2090)\n\n DDH-\u2141\u2080 DDH-\u2141\u2081 : DDH.Adv \u2192 R \u2192 \ud835\udfda\n DDH-\u2141\u2080 A (b , r\u2090 , g\u02e3 , g\u02b8 , g\u1dbb) = DDH.\u2141\u2080 A ((b , r\u2090) , g\u02e3 , g\u02b8 , g\u1dbb)\n DDH-\u2141\u2081 A (b , r\u2090 , g\u02e3 , g\u02b8 , g\u1dbb) = DDH.\u2141\u2081 A ((b , r\u2090) , g\u02e3 , g\u02b8 , g\u1dbb)\n \n transformAdv : IND-CPA.Adv \u2192 DDH.Adv\n transformAdv (m , d) (b , r\u2090) g\u02e3 g\u02b8 g\u1dbb = b == b\u2032\n where mb = m r\u2090 g\u02e3 b\n c = (g\u02b8 , g\u1dbb \u2219 mb)\n b\u2032 = d r\u2090 g\u02e3 c\n\n #q_ : Count \u2124q\n #q_ = count \u03bc\u2124q\n\n _\u2248q_ : (f g : \u2124q \u2192 \u2115) \u2192 \u2605\n f \u2248q g = sum \u03bc\u2124q f \u2261 sum \u03bc\u2124q g\n\n postulate\n A : IND-CPA.Adv\n\n A\u2032 = transformAdv A\n\n 1\/2 : R \u2192 \ud835\udfda\n 1\/2 (b , _) = b\n \n\n module _ {S} where \n _\u2248\u1d3f_ : (f g : R \u2192 S) \u2192 \u2605\n f \u2248\u1d3f g = \u2200 (X : S \u2192 \u2115) \u2192 sumR (X \u2218 f) \u2261 sumR (X \u2218 g) \n\n OTP-\u2141 : IND-CPA.Adv \u2192 R \u2192 \ud835\udfda\n OTP-\u2141 (m , d) (b , r\u2090 , x , y , z) = b == d r\u2090 g\u02e3 (g\u02b8 , g\u1dbb \u2219 m r\u2090 g\u02e3 b)\n where g\u02e3 = g ^ x\n g\u02b8 = g ^ y\n g\u1dbb = g ^ z\n\n postulate\n otp-lem : \u2200 (A : Message \u2192 \u2115) m\u2080 m\u2081 \u2192\n (\u03bb x \u2192 A((g ^ x) \u2219 m\u2080)) \u2248q (\u03bb x \u2192 A((g ^ x) \u2219 m\u2081))\n \n goal4 : 1\/2 \u2248\u1d3f OTP-\u2141 A\n goal4 X = sumR' (\u03bb _ \u2192 X 0b) + sumR' (\u03bb _ \u2192 X 1b)\n \u2261\u27e8 sym (sumHomR' _ _) \u27e9\n sumR' (\u03bb _ \u2192 X 0b + X 1b)\n \u2261\u27e8 sumExtR' (lemma \u2218 B 0b) \u27e9\n sumR' (Y 0b 0b +\u00b0 Y 1b 0b)\n \u2261\u27e8 sumHomR' _ _ \u27e9\n sumR' (Y 0b 0b) + sumR' (Y 1b 0b)\n \u2261\u27e8 cong (_+_ (sumR' (Y 0b 0b))) lemma2 \u27e9\n sumR' (Y 0b 0b) + sumR' (Y 1b 1b)\n \u220e\n where\n open \u2261-Reasoning\n \n B : \ud835\udfda \u2192 R' \u2192 \ud835\udfda\n B b (r\u2090 , x , y , z) = proj\u2082 A r\u2090 (g ^ x) (g ^ y , (g ^ z) \u2219 proj\u2081 A r\u2090 (g ^ x) b)\n\n Y = \u03bb bb bbb r \u2192 X (bb == B bbb r)\n\n lemma : \u2200 b \u2192 X 0b + X 1b \u2261 X (0b == b) + X (1b == b)\n lemma 1b = refl\n lemma 0b = \u2115\u00b0.+-comm (X 0b) _\n \n lemma2 : sumR' (Y 1b 0b) \u2261 sumR' (Y 1b 1b)\n lemma2 = sumExtR\u2090 \u03bb r\u2090 \u2192\n sumExt\u2124q \u03bb x \u2192\n sumExt\u2124q \u03bb y \u2192\n otp-lem (\u03bb m \u2192 X (proj\u2082 A r\u2090 (g ^ x) (g ^ y , m))) (proj\u2081 A r\u2090 (g ^ x) 0') (proj\u2081 A r\u2090 (g ^ x) 1')\n\n {-\n otp-lem (\u03bb m \u2192 proj\u2082 A r\u2090 (g ^ x) (g ^ y , m))\n (proj\u2081 A r\u2090 (g ^ x) 1b)\n (proj\u2081 A r\u2090 (g ^ x) 0b)\n (\u03bb c \u2192 X (1b == c))\n-}\n\n module absDist {DIST : \u2605}(Dist : (f g : R \u2192 \ud835\udfda) \u2192 DIST)\n (dist-cong : \u2200 {f g h i} \u2192 f \u2248\u1d3f g \u2192 h \u2248\u1d3f i \u2192 Dist f h \u2261 Dist g i) where\n goal : Dist (IND-CPA-\u2141 A) 1\/2 \u2261 Dist (DDH-\u2141\u2080 A\u2032) (DDH-\u2141\u2081 A\u2032)\n goal = Dist (IND-CPA-\u2141 A) 1\/2\n \u2261\u27e8 refl \u27e9\n Dist (DDH-\u2141\u2080 A\u2032) 1\/2\n \u2261\u27e8 dist-cong (\u03bb _ \u2192 refl) goal4 \u27e9\n Dist (DDH-\u2141\u2080 A\u2032) (OTP-\u2141 A)\n \u2261\u27e8 refl \u27e9\n Dist (DDH-\u2141\u2080 A\u2032) (DDH-\u2141\u2081 A\u2032)\n \u220e\n where open \u2261-Reasoning\n\n Dist : (f g : R \u2192 \ud835\udfda) \u2192 \u2115\n Dist f g = dist (count \u03bcR f) (count \u03bcR g)\n\n dist-cong : \u2200 {f g h i} \u2192 f \u2248\u1d3f g \u2192 h \u2248\u1d3f i \u2192 Dist f h \u2261 Dist g i\n dist-cong {f}{g}{h}{i} f\u2248g h\u2248i = cong\u2082 dist (f\u2248g \ud835\udfda\u25b9\u2115) (h\u2248i \ud835\udfda\u25b9\u2115)\n\n open absDist Dist (\u03bb {f}{g}{h}{i} \u2192 dist-cong {f}{g}{h}{i})\n\nmodule El-Gamal-Base\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n\n -- Required for decryption\n (_\/_ : G \u2192 G \u2192 G)\n\n -- Required for the correctness proof\n (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n {-\n (_\u207b\u00b9 : G \u2192 G)\n (\u207b\u00b9-inverse : \u2200 x \u2192 x \u207b\u00b9 \u2219 x \u2261 1G)\n -}\n\n -- Required for the proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : Explorable \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : Explorable R\u2090)\n where\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ G _\u2219_\n _\/_ \/-\u2219 comm-^\n dist-^-\u22a0 \u03bc\u2124q R\u2090 \u03bcR\u2090 public\n\n {-\n module OTP\u2141-LEM\n (otp-lem1 : \u2200 (A : G \u2192 Bit) m \u2192 (\u03bb x \u2192 A (g^ x \u2219 m)) \u2248q (\u03bb x \u2192 A (g^ x)))\n where\n\n otp-lem : \u2200 (A : G \u2192 Bit) m\u2080 m\u2081 \u2192 (\u03bb x \u2192 A (g^ x \u2219 m\u2080)) \u2248q (\u03bb x \u2192 A (g^ x \u2219 m\u2081))\n otp-lem A m\u2080 m\u2081 rewrite otp-lem1 A m\u2080 | otp-lem1 A m\u2081 = refl\n -}\n \n{-\nmodule El-Gamal-Hashed\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n -- (HKey : \u2605)\n (|M| : \u2115)\n (\u210b : {-HKey \u2192-} G \u2192 Bits |M|)\n\n -- (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n -- Required for the proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : Explorable \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : Explorable R\u2090)\n where\n\n Message = Bits |M|\n\n \u210b\u27e8_\u27e9\u2295_ : G \u2192 Message \u2192 Message\n \u210b\u27e8 \u03b4 \u27e9\u2295 m = \u210b \u03b4 \u2295 m\n\n _\/_ : Message \u2192 G \u2192 Message\n _\/_ m \u03b4 = \u210b \u03b4 \u2295 m\n{-\n\n \/-\u2219 : \u2200 x y \u2192 \u210b\u27e8 x \u27e9\u2295 y \/ x \u2261 y\n \/-\u2219 x y = {!!}\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ Message \u210b\u27e8_\u27e9\u2295_ _\/_ {!!} {!!}\n dist-^-\u22a0 sum\u2124q sum\u2124q-ext R\u2090 sumR\u2090 sumR\u2090-ext public\n -}\n\n {-\n OTP\u2141-lem : \u2200 d M\u2080 M\u2081 \u2192 OTP\u2141 M\u2080 d \u2248\u1d3f OTP\u2141 M\u2081 d\n OTP\u2141-lem = ?\n -}\n\nmodule \u27e8\u2124p\u27e9\u2605 p-3 {- p is prime -} (`R\u2090 : `\u2605) where\n p : \u2115\n p = 3 + p-3\n\n q : \u2115\n q = p \u2238 1\n\n module G = G-implem p q (## 2) (## 0) (## 1) (## 0) (## 1)\n open G\n\n postulate\n _\u207b\u00b9 : G \u2192 G\n\n _\/_ : G \u2192 G \u2192 G\n x \/ y = x \u2219 (y \u207b\u00b9)\n\n postulate\n \/-\u2022 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y\n dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y\n \u22a0-comm : \u2200 x y \u2192 x \u22a0 y \u2261 y \u22a0 x\n\n comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x\n comm-^ \u03b1 x y = (\u03b1 ^ x)^ y\n \u2261\u27e8 sym (dist-^-\u22a0 \u03b1 x y) \u27e9\n \u03b1 ^ (x \u22a0 y)\n \u2261\u27e8 cong (_^_ \u03b1) (\u22a0-comm x y) \u27e9\n \u03b1 ^ (y \u22a0 x)\n \u2261\u27e8 dist-^-\u22a0 \u03b1 y x \u27e9\n (\u03b1 ^ y)^ x\n \u220e\n where open \u2261-Reasoning\n\n open \u2124q-count \u2124q _\u229e_ \u03bc\u2124q \u229e-stable\n\n \u03bcR\u2090 : Explorable (El `R\u2090)\n \u03bcR\u2090 = \u03bcU \u03bc\u2124q `R\u2090\n\n R\u2090 = El `R\u2090\n sumR\u2090 = sum \u03bcR\u2090\n sumR\u2090-ext = sum-ext \u03bcR\u2090\n\n module EB = El-Gamal-Base _ _\u22a0_ G g _^_ _\u2219_ _\/_ \/-\u2022 comm-^ dist-^-\u22a0 \u03bc\u2124q R\u2090 \u03bcR\u2090\n open EB hiding (g^_)\n\n otp-base-lem : \u2200 (A : G \u2192 Bit) m \u2192 (A \u2218 g^_) \u2248q (A \u2218 g^_ \u2218 _\u229e_ m)\n otp-base-lem A m = \u229e-stable m (Bit\u25b9\u2115 \u2218 A \u2218 g^_)\n\n {-\n postulate\n -- ddh-hyp : (A : DDH.Adv) \u2192 DDH.\u2141\u2080 A \u2248\u1d3f DDH.\u2141\u2081 A\n otp-lem : \u2200 (A : G \u2192 Bit) m \u2192 (\u03bb x \u2192 A (g^ x \u2219 m)) \u2248q (\u03bb x \u2192 A (g^ x))\n -}\n\n -- open OTP\u2141-LEM otp-lem\n\n {-\n final : \u2200 A \u2192 IND-CPA.\u2141 A 0b \u2248\u1d3f IND-CPA.\u2141 A 1b\n final A = Proof.final ddh-hyp OTP\u2141-lem A 0b\n -}\n\nmodule \u27e8\u212411\u27e9\u2605 = \u27e8\u2124p\u27e9\u2605 (11 \u2238 3)\n `X -- the amount of adversary randomness\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","old_contents":"{-# OPTIONS --copatterns #-}\nopen import Type\nopen import Function\nopen import Data.Product\nopen import Data.Unit\nimport Data.Fin.NP as Fin\nopen Fin using (Fin; zero; suc; Fin\u25b9\u2115) renaming (#_ to ##_)\nopen import Data.Nat.NP hiding (_^_; _==_)\nopen import Data.Bit\nopen import Data.Bits hiding (_==_)\nopen import Relation.Binary.PropositionalEquality.NP as \u2261\nimport Game.DDH\nimport Game.IND-CPA\nimport Cipher.ElGamal.Generic\nopen import Search.Type\nopen import Search.Searchable renaming (Searchable to Explorable)\nopen import Search.Searchable.Product\nopen import Search.Searchable.Fin\nopen import Relation.Binary.NP\n\nmodule elgamal where\n\ndata `\u2605 : \u2605 where\n `\u22a4 : `\u2605\n `X : `\u2605\n _`\u00d7_ : `\u2605 \u2192 `\u2605 \u2192 `\u2605\ninfixr 2 _`\u00d7_\n\nmodule Univ (X : \u2605) where\n El : `\u2605 \u2192 \u2605\n El `\u22a4 = \u22a4\n El `X = X\n El (u\u2080 `\u00d7 u\u2081) = El u\u2080 \u00d7 El u\u2081\n\n record \u21ba (R : `\u2605) (A : \u2605) : \u2605 where\n constructor mk\n field\n run\u21ba : El R \u2192 A\n open \u21ba public\n\n EXP : (R : `\u2605) \u2192 \u2605\n EXP R = \u21ba R Bit\n\n Det : \u2605 \u2192 \u2605\n Det = \u21ba `\u22a4\n\n \u03bcU : Explorable X \u2192 \u2200 u \u2192 Explorable (El u)\n \u03bcU \u03bcX `\u22a4 = \u03bc\u22a4\n \u03bcU \u03bcX `X = \u03bcX\n \u03bcU \u03bcX (u\u2080 `\u00d7 u\u2081) = \u03bcU \u03bcX u\u2080 \u00d7-\u03bc \u03bcU \u03bcX u\u2081\n\nmodule \u2124q-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u03bc\u2124q : Explorable \u2124q)\n (\u229e-stable : \u2200 x \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u229e_ x))\n where\n\n -- open Sum\n open Univ \u2124q public\n open `\u2605 public renaming (`X to `\u2124q)\n\n #_ : \u2200 {u} \u2192 \u21ba u Bit \u2192 \u2115\n #_ {u} f = count (\u03bcU \u03bc\u2124q u) (run\u21ba f)\n\n #q_ : Count \u2124q\n #q_ = count \u03bc\u2124q\n\n \u2047 : \u2200 R \u2192 \u21ba R (El R)\n run\u21ba (\u2047 _) = id\n\n pure\u21ba : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n run\u21ba (pure\u21ba x) r = x -- turning r to _ produce an error\n\n \u27ea_\u27eb : \u2200 {R A} \u2192 A \u2192 \u21ba R A\n \u27ea_\u27eb = pure\u21ba\n\n {-\n \u27ea_\u27eb\u1d30 : \u2200 {a} {A : Set a} \u2192 A \u2192 Det A\n \u27ea_\u27eb\u1d30 = pure\u1d30\n -}\n\n map\u21ba : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n run\u21ba (map\u21ba f x) r = f (run\u21ba x r)\n\n infixl 4 _\u229b_\n _\u229b_ : \u2200 {R S A B} \u2192 \u21ba R (A \u2192 B) \u2192 \u21ba S A \u2192 \u21ba (R `\u00d7 S) B\n run\u21ba (af \u229b ax) rs = run\u21ba af (proj\u2081 rs) (run\u21ba ax (proj\u2082 rs))\n\n \u27ea_\u00b7_\u27eb : \u2200 {A B R} \u2192 (A \u2192 B) \u2192 \u21ba R A \u2192 \u21ba R B\n \u27ea f \u00b7 x \u27eb = map\u21ba f x\n\n \u27ea_\u00b7_\u00b7_\u27eb : \u2200 {A B C} {R S} \u2192\n (A \u2192 B \u2192 C) \u2192 \u21ba R A \u2192 \u21ba S B \u2192 \u21ba (R `\u00d7 S) C\n \u27ea f \u00b7 x \u00b7 y \u27eb = map\u21ba f x \u229b y\n\n _\u27e8\u229e\u27e9_ : \u2200 {R S} \u2192 \u21ba R \u2124q \u2192 \u21ba S \u2124q \u2192 \u21ba (R `\u00d7 S) \u2124q\n x \u27e8\u229e\u27e9 y = \u27ea _\u229e_ \u00b7 x \u00b7 y \u27eb\n\n \u27e8_\u229e\u27e9_ : \u2200 {R} \u2192 \u2124q \u2192 \u21ba R \u2124q \u2192 \u21ba R \u2124q\n \u27e8 x \u229e\u27e9 y = \u27ea _\u229e_ x \u00b7 y \u27eb\n\n infix 4 _\u2248\u21ba_ _\u2248\u1d2c_\n _\u2248\u21ba_ : \u2200 {R : `\u2605} (f g : EXP R) \u2192 \u2605\n _\u2248\u21ba_ = _\u2261_ on #_\n\n _\u2248\u1d2c_ : \u2200 {A R} (f g : \u21ba R A) \u2192 Set _\n _\u2248\u1d2c_ {A} f g = \u2200 (Adv : A \u2192 Bit) \u2192 \u27ea Adv \u00b7 f \u27eb \u2248\u21ba \u27ea Adv \u00b7 g \u27eb\n\n lem : \u2200 x \u2192 \u27e8 x \u229e\u27e9 (\u2047 `\u2124q) \u2248\u1d2c \u2047 _\n lem x Adv = sym (\u229e-stable x (Bit\u25b9\u2115 \u2218 Adv))\n\n -- \u2200 (A : \u2124q \u2192 Bit) \u2192 # (A \u2047)\n\nopen Fin.Modulo renaming (sucmod to [suc]; sucmod-inj to [suc]-inj)\n\n{-\nmodule \u2124q-implem (q-2 : \u2115) where\n q : \u2115\n q = 2 + q-2\n\n \u2124q : \u2605\n \u2124q = Fin q\n\n [0] : \u2124q\n [0] = zero\n\n [1] : \u2124q\n [1] = suc zero\n-}\nmodule \u2124q-implem (q-1 : \u2115) ([0]' [1]' : Fin (suc q-1)) where\n -- open Sum\n q : \u2115\n q = suc q-1\n\n \u2124q : \u2605\n \u2124q = Fin q\n\n \u03bc\u2124q : Explorable \u2124q\n \u03bc\u2124q = \u03bcFinSuc q-1\n\n sum\u2124q : Sum \u2124q\n sum\u2124q = sum \u03bc\u2124q\n\n [0] : \u2124q\n [0] = [0]'\n\n [1] : \u2124q\n [1] = [1]'\n\n [suc]-stable : SumStableUnder (sum \u03bc\u2124q) [suc]\n [suc]-stable = \u03bcFinSUI [suc] [suc]-inj\n\n _\u2115\u229e_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u229e n = n\n suc m \u2115\u229e n = m \u2115\u229e ([suc] n)\n\n \u2115\u229e-inj : \u2200 n {x y} \u2192 n \u2115\u229e x \u2261 n \u2115\u229e y \u2192 x \u2261 y\n \u2115\u229e-inj zero eq = eq\n \u2115\u229e-inj (suc n) eq = [suc]-inj (\u2115\u229e-inj n eq)\n\n \u2115\u229e-stable : \u2200 m \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u2115\u229e_ m)\n \u2115\u229e-stable m = \u03bcFinSUI (_\u2115\u229e_ m) (\u2115\u229e-inj m)\n\n _\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u229e n = Fin\u25b9\u2115 m \u2115\u229e n\n\n \u229e-inj : \u2200 m {x y} \u2192 m \u229e x \u2261 m \u229e y \u2192 x \u2261 y\n \u229e-inj m = \u2115\u229e-inj (Fin\u25b9\u2115 m)\n\n \u229e-stable : \u2200 m \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u229e_ m)\n \u229e-stable m = \u03bcFinSUI (_\u229e_ m) (\u229e-inj m)\n\n _\u2115\u22a0_ : \u2115 \u2192 \u2124q \u2192 \u2124q\n zero \u2115\u22a0 n = [0]\n suc m \u2115\u22a0 n = n \u229e (m \u2115\u22a0 n)\n\n _\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m \u22a0 n = Fin\u25b9\u2115 m \u2115\u22a0 n\n\n _[^]\u2115_ : \u2124q \u2192 \u2115 \u2192 \u2124q\n m [^]\u2115 zero = [1]\n m [^]\u2115 suc n = m \u22a0 (m [^]\u2115 n)\n\n _[^]_ : \u2124q \u2192 \u2124q \u2192 \u2124q\n m [^] n = m [^]\u2115 (Fin\u25b9\u2115 n)\n\nmodule G-implem (p-1 q-1 : \u2115) (g' 0[p] 1[p] : Fin (suc p-1)) (0[q] 1[q] : Fin (suc q-1)) where\n open \u2124q-implem q-1 0[q] 1[q] public\n open \u2124q-implem p-1 0[p] 1[p] public using () renaming (\u2124q to G; _\u22a0_ to _\u2219_; _[^]\u2115_ to _^[p]_)\n\n g : G\n g = g'\n\n _^_ : G \u2192 \u2124q \u2192 G\n x ^ n = x ^[p] Fin\u25b9\u2115 n\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n {-\n g^-inj : \u2200 m n \u2192 g^ m \u2261 g^ n \u2192 m \u2261 n\n g^-inj = {!!}\n -}\n\nmodule G-count\n (\u2124q : \u2605)\n (_\u229e_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (\u03bc\u2124q : Explorable \u2124q)\n (\u229e-stable : \u2200 x \u2192 SumStableUnder (sum \u03bc\u2124q) (_\u229e_ x))\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n where\n\n g^_ : \u2124q \u2192 G\n g^ n = g ^ n\n\n open \u2124q-count \u2124q _\u229e_ \u03bc\u2124q \u229e-stable\n\n \u2047G : \u21ba `\u2124q G\n run\u21ba \u2047G x = g^ x\n\n #G : Count G\n #G f = #q (f \u2218 g^_)\n\n {-\n #G-\u2219 : \u2200 f m \u2192 #G (f \u2218 _\u2219_ m) \u2261 #G f\n #G-\u2219 f m = {!!}\n -}\n\nmodule El-Gamal-Generic\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (Message : \u2605)\n (_\u2219_ : G \u2192 Message \u2192 Message)\n\n -- Required for decryption\n (_\/_ : Message \u2192 G \u2192 Message)\n\n -- Required for the correctness proof\n (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n -- Required for the security proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : Explorable \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : Explorable R\u2090)\n where\n\n g^_ : \u2124q \u2192 G\n g^ x = g ^ x\n\n -- g\u02e3 is the pk\n -- x is the sk\n\n R\u2093 : \u2605\n R\u2093 = \u2124q\n\n open Cipher.ElGamal.Generic Message \u2124q G g _^_ _\u2219_ _\/_\n\n functional-correctness : \u2200 x y m \u2192 Dec x (Enc (g^ x) m y) \u2261 m\n functional-correctness x y m rewrite comm-^ g x y | \/-\u2219 (g^ y ^ x) m = refl\n\n module IND-CPA = Game.IND-CPA PubKey SecKey Message CipherText R\u2091 R\u2096 R\u2090 R\u2093 KeyGen Enc\n open IND-CPA using (R)\n\n UnusedGame : (i : Bit) \u2192 IND-CPA.Adv \u2192 (Bit \u00d7 R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q) \u2192 Bit\n UnusedGame i (m , d) (b , r\u2090 , x , y , z) = b == d r\u2090 g\u02e3 (g\u02b8 , \u03b6)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n \u03b4 = g\u02e3 ^ case i 0\u2192 y 1\u2192 z\n \u03b6 = \u03b4 \u2219 m r\u2090 g\u02e3 b\n\n module DDH = Game.DDH \u2124q G g _^_ R\u2090\n\n OTP\u2141 : (R\u2090 \u2192 G \u2192 Message) \u2192 (R\u2090 \u2192 G \u2192 G \u2192 Message \u2192 Bit) \u2192 R \u2192 Bit\n OTP\u2141 M d (r , x , y , z) = d r g\u02e3 g\u02b8 (g\u1dbb \u2219 M r g\u02e3)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n g\u1dbb = g^ z\n\n TrA : Bit \u2192 IND-CPA.Adv \u2192 DDH.Adv\n TrA b (m , d) r\u2090 g\u02e3 g\u02b8 g\u02e3\u02b8 = d r\u2090 g\u02e3 (g\u02b8 , g\u02e3\u02b8 \u2219 m r\u2090 g\u02e3 b)\n\n projM : IND-CPA.Adv \u2192 Bit \u2192 R\u2090 \u2192 G \u2192 Message\n projM (m , _) b r\u2090 g\u02e3 = m r\u2090 g\u02e3 b\n\n projD : IND-CPA.Adv \u2192 R\u2090 \u2192 G \u2192 G \u2192 Message \u2192 Bit\n projD (_ , d) r\u2090 g\u02e3 g\u02b8 g\u1dbb\u2219M = d r\u2090 g\u02e3 (g\u02b8 , g\u1dbb\u2219M)\n\n module Unused where\n like-\u2141 : Bit \u2192 IND-CPA.Game\n like-\u2141 b (m , d) (r\u2090 , x , y , _z) =\n d r\u2090 g\u02e3 (g\u02b8 , (g\u02e3 ^ y) \u2219 m r\u2090 g\u02e3 b)\n where g\u02e3 = g^ x\n g\u02b8 = g^ y\n\n IND-CPA-\u2141\u2261like-\u2141 : IND-CPA.\u2141 \u2261 like-\u2141\n IND-CPA-\u2141\u2261like-\u2141 = refl\n\n -- R = R\u2090 \u00d7 \u2124q \u00d7 \u2124q \u00d7 \u2124q\n \u03bcR : Explorable R\n \u03bcR = \u03bcR\u2090 \u00d7-\u03bc \u03bc\u2124q \u00d7-\u03bc \u03bc\u2124q \u00d7-\u03bc \u03bc\u2124q\n\n #\u1d3f_ : Count R\n #\u1d3f_ = count \u03bcR\n\n #q_ : Count \u2124q\n #q_ = count \u03bc\u2124q\n\n _\u2248q_ : (f g : \u2124q \u2192 Bit) \u2192 \u2605\n f \u2248q g = #q f \u2261 #q g\n\n Re = (f g : R \u2192 Bit) \u2192 \u2605\n record Tra (_\u2248\u2080_ _\u2248\u2081_ : Re) (f g : R \u2192 Bit) : \u2605 where\n field\n h : R \u2192 Bit\n f\u2248\u2080h : f \u2248\u2080 h\n h\u2248\u2081g : h \u2248\u2081 g\n\n record _\u2248\u1d3f_ (f g : R \u2192 Bit) : \u2605 where\n constructor mk\n field\n un-\u2248\u1d3f : #\u1d3f f \u2261 #\u1d3f g\n open _\u2248\u1d3f_ public\n\n \u2248\u1d3f-trans : Transitive _\u2248\u1d3f_\n \u2248\u1d3f-trans (mk p) (mk q) = mk (\u2261.trans p q)\n\n module \u2248\u1d3f-Reasoning where\n open Trans-Reasoning _\u2248\u1d3f_ \u2248\u1d3f-trans public using () renaming (_\u2248\u27e8_\u27e9_ to _\u2248\u1d3f\u27e8_\u27e9_)\n infix 2 _\u220e\n\n _\u220e : \u2200 x \u2192 x \u2248\u1d3f x\n _ \u220e = mk refl\n\n module Proof\n (ddh-hyp : \u2200 A \u2192 DDH.\u2141\u2080 A \u2248\u1d3f DDH.\u2141\u2081 A)\n (otp-lem : \u2200 A m\u2080 m\u2081 \u2192 (\u03bb x \u2192 A (g^ x \u2219 m\u2080)) \u2248q (\u03bb x \u2192 A (g^ x \u2219 m\u2081)))\n (A : IND-CPA.Adv) (b : Bit)\n where\n OTP\u2141-lem : \u2200 d M\u2080 M\u2081 \u2192 OTP\u2141 M\u2080 d \u2248\u1d3f OTP\u2141 M\u2081 d\n OTP\u2141-lem d M\u2080 M\u2081 = mk (\n sum-ext \u03bcR\u2090 (\u03bb r \u2192\n sum-ext \u03bc\u2124q (\u03bb x \u2192\n sum-ext \u03bc\u2124q (\u03bb y \u2192\n pf r x y))))\n where\n pf : \u2200 r x y \u2192 count \u03bc\u2124q (\u03bb z \u2192 OTP\u2141 M\u2080 d (r , x , y , z))\n \u2261 count \u03bc\u2124q (\u03bb z \u2192 OTP\u2141 M\u2081 d (r , x , y , z))\n pf r x y rewrite otp-lem (d r (g^ x) (g^ y)) (M\u2080 r (g^ x)) (M\u2081 r (g^ x)) = refl\n\n -- moving this definition above OTP\u2141-lem breaks type-checking: ???\n \u00acb : Bit\n \u00acb = not b\n\n A\u1d47 = TrA b A\n A\u00ac\u1d47 = TrA \u00acb A\n\n pf0,5 : IND-CPA.\u2141 b A \u2257 DDH.\u2141\u2080 A\u1d47\n pf0,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n pf2,5 : DDH.\u2141\u2081 A\u1d47 \u2261 OTP\u2141 (projM A b) (projD A)\n pf2,5 = refl\n\n pf4,5 : IND-CPA.\u2141 \u00acb A \u2257 DDH.\u2141\u2080 A\u00ac\u1d47\n pf4,5 (r , x , y , z) rewrite dist-^-\u22a0 g x y = refl\n\n open \u2248\u1d3f-Reasoning\n\n final : IND-CPA.\u2141 b A \u2248\u1d3f IND-CPA.\u2141 \u00acb A\n final = IND-CPA.\u2141 b A\n \u2248\u1d3f\u27e8 mk (sum-ext \u03bcR (cong Bit\u25b9\u2115 \u2218 pf0,5)) \u27e9\n DDH.\u2141\u2080 A\u1d47\n \u2248\u1d3f\u27e8 ddh-hyp A\u1d47 \u27e9\n DDH.\u2141\u2081 A\u1d47\n \u2248\u1d3f\u27e8 OTP\u2141-lem (projD A) (projM A b) (projM A \u00acb) \u27e9\n DDH.\u2141\u2081 A\u00ac\u1d47\n \u2248\u1d3f\u27e8 mk (\u2261.sym (un-\u2248\u1d3f (ddh-hyp A\u00ac\u1d47))) \u27e9\n DDH.\u2141\u2080 A\u00ac\u1d47\n \u2248\u1d3f\u27e8 mk (\u2261.sym (sum-ext \u03bcR (cong Bit\u25b9\u2115 \u2218 pf4,5))) \u27e9\n IND-CPA.\u2141 \u00acb A\n \u220e\n\nmodule El-Gamal-Base\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n (_\u2219_ : G \u2192 G \u2192 G)\n\n -- Required for decryption\n (_\/_ : G \u2192 G \u2192 G)\n\n -- Required for the correctness proof\n (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n {-\n (_\u207b\u00b9 : G \u2192 G)\n (\u207b\u00b9-inverse : \u2200 x \u2192 x \u207b\u00b9 \u2219 x \u2261 1G)\n -}\n\n -- Required for the proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : Explorable \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : Explorable R\u2090)\n where\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ G _\u2219_\n _\/_ \/-\u2219 comm-^\n dist-^-\u22a0 \u03bc\u2124q R\u2090 \u03bcR\u2090 public\n\n module OTP\u2141-LEM\n (otp-lem1 : \u2200 (A : G \u2192 Bit) m \u2192 (\u03bb x \u2192 A (g^ x \u2219 m)) \u2248q (\u03bb x \u2192 A (g^ x)))\n where\n\n otp-lem : \u2200 (A : G \u2192 Bit) m\u2080 m\u2081 \u2192 (\u03bb x \u2192 A (g^ x \u2219 m\u2080)) \u2248q (\u03bb x \u2192 A (g^ x \u2219 m\u2081))\n otp-lem A m\u2080 m\u2081 rewrite otp-lem1 A m\u2080 | otp-lem1 A m\u2081 = refl\n\nmodule El-Gamal-Hashed\n (\u2124q : \u2605)\n (_\u22a0_ : \u2124q \u2192 \u2124q \u2192 \u2124q)\n (G : \u2605)\n (g : G)\n (_^_ : G \u2192 \u2124q \u2192 G)\n -- (HKey : \u2605)\n (|M| : \u2115)\n (\u210b : {-HKey \u2192-} G \u2192 Bits |M|)\n\n -- (\/-\u2219 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y)\n (comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x)\n\n -- Required for the proof\n (dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y)\n (\u03bc\u2124q : Explorable \u2124q)\n (R\u2090 : \u2605)\n (\u03bcR\u2090 : Explorable R\u2090)\n where\n\n Message = Bits |M|\n\n \u210b\u27e8_\u27e9\u2295_ : G \u2192 Message \u2192 Message\n \u210b\u27e8 \u03b4 \u27e9\u2295 m = \u210b \u03b4 \u2295 m\n\n _\/_ : Message \u2192 G \u2192 Message\n _\/_ m \u03b4 = \u210b \u03b4 \u2295 m\n{-\n\n \/-\u2219 : \u2200 x y \u2192 \u210b\u27e8 x \u27e9\u2295 y \/ x \u2261 y\n \/-\u2219 x y = {!!}\n\n open El-Gamal-Generic \u2124q _\u22a0_ G g _^_ Message \u210b\u27e8_\u27e9\u2295_ _\/_ {!!} {!!}\n dist-^-\u22a0 sum\u2124q sum\u2124q-ext R\u2090 sumR\u2090 sumR\u2090-ext public\n -}\n\n {-\n OTP\u2141-lem : \u2200 d M\u2080 M\u2081 \u2192 OTP\u2141 M\u2080 d \u2248\u1d3f OTP\u2141 M\u2081 d\n OTP\u2141-lem = ?\n -}\n\nmodule \u27e8\u2124p\u27e9\u2605 p-3 {- p is prime -} (`R\u2090 : `\u2605) where\n p : \u2115\n p = 3 + p-3\n\n q : \u2115\n q = p \u2238 1\n\n module G = G-implem p q (## 2) (## 0) (## 1) (## 0) (## 1)\n open G\n\n postulate\n _\u207b\u00b9 : G \u2192 G\n\n _\/_ : G \u2192 G \u2192 G\n x \/ y = x \u2219 (y \u207b\u00b9)\n\n postulate\n \/-\u2022 : \u2200 x y \u2192 (x \u2219 y) \/ x \u2261 y\n dist-^-\u22a0 : \u2200 \u03b1 x y \u2192 \u03b1 ^ (x \u22a0 y) \u2261 (\u03b1 ^ x) ^ y\n \u22a0-comm : \u2200 x y \u2192 x \u22a0 y \u2261 y \u22a0 x\n\n comm-^ : \u2200 \u03b1 x y \u2192 (\u03b1 ^ x)^ y \u2261 (\u03b1 ^ y)^ x\n comm-^ \u03b1 x y = (\u03b1 ^ x)^ y\n \u2261\u27e8 sym (dist-^-\u22a0 \u03b1 x y) \u27e9\n \u03b1 ^ (x \u22a0 y)\n \u2261\u27e8 cong (_^_ \u03b1) (\u22a0-comm x y) \u27e9\n \u03b1 ^ (y \u22a0 x)\n \u2261\u27e8 dist-^-\u22a0 \u03b1 y x \u27e9\n (\u03b1 ^ y)^ x\n \u220e\n where open \u2261-Reasoning\n\n open \u2124q-count \u2124q _\u229e_ \u03bc\u2124q \u229e-stable\n\n \u03bcR\u2090 : Explorable (El `R\u2090)\n \u03bcR\u2090 = \u03bcU \u03bc\u2124q `R\u2090\n\n R\u2090 = El `R\u2090\n sumR\u2090 = sum \u03bcR\u2090\n sumR\u2090-ext = sum-ext \u03bcR\u2090\n\n module EB = El-Gamal-Base _ _\u22a0_ G g _^_ _\u2219_ _\/_ \/-\u2022 comm-^ dist-^-\u22a0 \u03bc\u2124q R\u2090 \u03bcR\u2090\n open EB hiding (g^_)\n\n otp-base-lem : \u2200 (A : G \u2192 Bit) m \u2192 (A \u2218 g^_) \u2248q (A \u2218 g^_ \u2218 _\u229e_ m)\n otp-base-lem A m = \u229e-stable m (Bit\u25b9\u2115 \u2218 A \u2218 g^_)\n\n postulate\n ddh-hyp : (A : DDH.Adv) \u2192 DDH.\u2141\u2080 A \u2248\u1d3f DDH.\u2141\u2081 A\n otp-lem : \u2200 (A : G \u2192 Bit) m \u2192 (\u03bb x \u2192 A (g^ x \u2219 m)) \u2248q (\u03bb x \u2192 A (g^ x))\n\n\n open OTP\u2141-LEM otp-lem\n\n {-\n final : \u2200 A \u2192 IND-CPA.\u2141 A 0b \u2248\u1d3f IND-CPA.\u2141 A 1b\n final A = Proof.final ddh-hyp OTP\u2141-lem A 0b\n -}\n\nmodule \u27e8\u212411\u27e9\u2605 = \u27e8\u2124p\u27e9\u2605 (11 \u2238 3)\n `X -- the amount of adversary randomness\n\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"5c3cfcb7ee57a3ab3309e20e640f6d43c366be08","subject":"Unlock more in sum-setoid","message":"Unlock more in sum-setoid\n","repos":"crypto-agda\/crypto-agda","old_file":"sum-setoid.agda","new_file":"sum-setoid.agda","new_contents":"import Level as L\nopen import Type\nopen import Function\nopen import Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat.NP hiding (_^_)\nopen import Data.Nat.Properties\nopen import Data.Unit hiding (_\u2264_)\nopen import Data.Sum\nopen import Data.Maybe.NP\nopen import Data.Product\nopen import Data.Bits\nopen import Data.Bool.NP as Bool\nopen import Function.Equality using (_\u27e8$\u27e9_ ; \u2261-setoid)\nimport Function.Inverse as FI\nopen FI using (_\u2194_; module Inverse)\nimport Function.Related as FR\nopen import Function.Related.TypeIsomorphisms.NP\nopen import Relation.Binary.NP\nopen import Relation.Binary.Sum\nopen import Relation.Binary.Product.Pointwise\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_; _\u2257_)\n\nmodule sum-setoid where\n\n_\u2264\u00b0_ : \u2200 {A : \u2605}(f g : A \u2192 \u2115) \u2192 \u2605\nf \u2264\u00b0 g = \u2200 x \u2192 f x \u2264 g x\n\nSemigroup\u2080 = Semigroup L.zero L.zero\nMonoid\u2080 = Monoid L.zero L.zero\nCommutativeMonoid\u2080 = CommutativeMonoid L.zero L.zero\n\nmodule SgrpExtra (sg : Semigroup\u2080) where\n open Semigroup sg\n open Setoid-Reasoning (Semigroup.setoid sg) public\n C : \u2605\n C = Carrier\n _\u2248\u00b0_ : \u2200 {A : \u2605} (f g : A \u2192 C) \u2192 \u2605\n f \u2248\u00b0 g = \u2200 x \u2192 f x \u2248 g x\n _\u2219\u00b0_ : \u2200 {A : \u2605} (f g : A \u2192 C) \u2192 A \u2192 C\n (f \u2219\u00b0 g) x = f x \u2219 g x\n infixl 7 _-\u2219-_\n _-\u2219-_ : _\u2219_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_\n _-\u2219-_ = \u2219-cong\n\nmodule Sgrp (sg : Semigroup\u2080) where\n open Semigroup sg public\n open SgrpExtra sg public\n\nmodule Mon (m : Monoid\u2080) where\n open Monoid m public\n sg = semigroup\n open SgrpExtra semigroup public\n\nmodule CMon (cm : CommutativeMonoid\u2080) where\n open CommutativeMonoid cm public\n sg = semigroup\n m = monoid\n open SgrpExtra sg public\n\n \u2219-interchange : Interchange _\u2248_ _\u2219_ _\u2219_\n \u2219-interchange = InterchangeFromAssocCommCong.\u2219-interchange\n _\u2248_ isEquivalence\n _\u2219_ assoc comm (\u03bb _ \u2192 flip \u2219-cong refl)\n\nSearch : \u2605 \u2192 \u2605\u2081\nSearch A = \u2200 {B} \u2192 (_\u2219_ : B \u2192 B \u2192 B) \u2192 (A \u2192 B) \u2192 B\n-- Search A = \u2200 {I : \u2605} {F : I \u2192 \u2605} \u2192 (_\u2219_ : \u2200 {i} \u2192 F i \u2192 F i \u2192 F i) \u2192 \u2200 {i} \u2192 (A \u2192 F i) \u2192 F i\n\nSearchMon : \u2605 \u2192 \u2605\u2081\nSearchMon A = (m : Monoid\u2080) \u2192 let open Mon m in\n (A \u2192 C) \u2192 C\n\nsearchMonFromSearch : \u2200 {A} \u2192 Search A \u2192 SearchMon A\nsearchMonFromSearch s m = s _\u2219_ where open Mon m\n\nSum : \u2605 \u2192 \u2605\nSum A = (A \u2192 \u2115) \u2192 \u2115\n\nCount : \u2605 \u2192 \u2605\nCount A = (A \u2192 Bit) \u2192 \u2115\n\nSearchInd : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchInd {A} srch = \u2200 (P : Search A \u2192 \u2605)\n (P\u2219 : \u2200 {s\u2080 s\u2081 : Search A} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb _\u2219_ f \u2192 s\u2080 _\u2219_ f \u2219 s\u2081 _\u2219_ f))\n (Pf : \u2200 x \u2192 P (\u03bb _ f \u2192 f x))\n \u2192 P srch\n\nSumInd : \u2200 {A} \u2192 Sum A \u2192 \u2605\u2081\nSumInd {A} sum = \u2200 (P : Sum A \u2192 \u2605)\n (P+ : \u2200 {s\u2080 s\u2081 : Sum A} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb f \u2192 s\u2080 f + s\u2081 f))\n (Pf : \u2200 x \u2192 P (\u03bb f \u2192 f x))\n \u2192 P sum\n\nSearchMono : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchMono s\u1d2c = \u2200 {C} (_\u2286_ : C \u2192 C \u2192 \u2605) \u2192\n \u2200 {_\u2219_} (\u2219-mono : _\u2219_ Preserves\u2082 _\u2286_ \u27f6 _\u2286_ \u27f6 _\u2286_)\n {f g} \u2192\n (\u2200 x \u2192 f x \u2286 g x) \u2192 s\u1d2c _\u2219_ f \u2286 s\u1d2c _\u2219_ g\n\nSearchSgExt : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchSgExt s\u1d2c = \u2200 sg {f g} \u2192 let open Sgrp sg in\n f \u2248\u00b0 g \u2192 s\u1d2c _\u2219_ f \u2248 s\u1d2c _\u2219_ g\n\nSearchExt : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchExt {A} s\u1d2c = \u2200 {B} op {f g : A \u2192 B} \u2192 f \u2257 g \u2192 s\u1d2c op f \u2261 s\u1d2c op g\n\nSearchExtFun : \u2200 {A B} \u2192 Search (A \u2192 B) \u2192 \u2605\u2081\nSearchExtFun {A}{B} s\u1d2c\u1d2e = \u2200 {M} op {f g : (A \u2192 B) \u2192 M} \u2192 (\u2200 {\u03c6 \u03c8} \u2192 \u03c6 \u2257 \u03c8 \u2192 f \u03c6 \u2261 g \u03c8) \u2192 s\u1d2c\u1d2e op f \u2261 s\u1d2c\u1d2e op g\n\nSearchExtFun-\u00faber : \u2200 {A B} \u2192 (SF : Search (A \u2192 B)) \u2192 SearchInd SF \u2192 SearchExtFun SF\nSearchExtFun-\u00faber sf sf-ind op {f = f}{g} f\u2248g = sf-ind (\u03bb s \u2192 s op f \u2261 s op g) (\u2261.cong\u2082 op) (\u03bb x \u2192 f\u2248g (\u03bb _ \u2192 \u2261.refl))\n\nSearchExtoid : \u2200 {A : Setoid L.zero L.zero} \u2192 Search (Setoid.Carrier A) \u2192 \u2605\u2081\nSearchExtoid {A} s\u1d2c = \u2200 {M} op {f g : Setoid.Carrier A \u2192 M} \u2192 (\u2200 {x y} \u2192 Setoid._\u2248_ A x y \u2192 f x \u2261 g y) \u2192 s\u1d2c op f \u2261 s\u1d2c op g\n\nSumExt : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumExt sum\u1d2c = \u2200 {f g} \u2192 f \u2257 g \u2192 sum\u1d2c f \u2261 sum\u1d2c g\n\nCountExt : \u2200 {A} \u2192 Count A \u2192 \u2605\nCountExt count\u1d2c = \u2200 {f g} \u2192 f \u2257 g \u2192 count\u1d2c f \u2261 count\u1d2c g\n\nSearch\u03b5 : \u2200 {A} \u2192 SearchMon A \u2192 \u2605\u2081\nSearch\u03b5 s\u1d2c = \u2200 m \u2192 let open Mon m in\n s\u1d2c m (const \u03b5) \u2248 \u03b5\n\nSumZero : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumZero sum\u1d2c = sum\u1d2c (\u03bb _ \u2192 0) \u2261 0\n\nSumLin : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumLin sum\u1d2c = \u2200 f k \u2192 sum\u1d2c (\u03bb x \u2192 k * f x) \u2261 k * sum\u1d2c f\n\nSearchHom : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchHom s\u1d2c = \u2200 sg f g \u2192 let open Sgrp sg in\n s\u1d2c _\u2219_ (f \u2219\u00b0 g) \u2248 s\u1d2c _\u2219_ f \u2219 s\u1d2c _\u2219_ g\n\nSearchMonHom : \u2200 {A} \u2192 SearchMon A \u2192 \u2605\u2081\nSearchMonHom s\u1d2c = \u2200 (cm : CommutativeMonoid\u2080) f g \u2192\n let open CMon cm in\n s\u1d2c m (f \u2219\u00b0 g) \u2248 s\u1d2c m f \u2219 s\u1d2c m g\n\nSumHom : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumHom sum\u1d2c = \u2200 f g \u2192 sum\u1d2c (\u03bb x \u2192 f x + g x) \u2261 sum\u1d2c f + sum\u1d2c g\n\nSumMono : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumMono sum\u1d2c = \u2200 {f g} \u2192 f \u2264\u00b0 g \u2192 sum\u1d2c f \u2264 sum\u1d2c g\n\nSearchSwap : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchSwap {A} s\u1d2c = \u2200 {B} sg f \u2192\n let open Sgrp sg in \u2200 {s\u1d2e : (B \u2192 C) \u2192 C}\n \u2192 (hom : \u2200 f g \u2192 s\u1d2e (f \u2219\u00b0 g) \u2248 s\u1d2e f \u2219 s\u1d2e g)\n \u2192 s\u1d2c _\u2219_ (s\u1d2e \u2218 f) \u2248 s\u1d2e (s\u1d2c _\u2219_ \u2218 flip f)\n\nsum-lin\u21d2sum-zero : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumLin sum \u2192 SumZero sum\nsum-lin\u21d2sum-zero sum-lin = sum-lin (\u03bb _ \u2192 0) 0\n\nsum-mono\u21d2sum-ext : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumMono sum \u2192 SumExt sum\nsum-mono\u21d2sum-ext sum-mono f\u2257g = \u2115\u2264.antisym (sum-mono (\u2115\u2264.reflexive \u2218 f\u2257g)) (sum-mono (\u2115\u2264.reflexive \u2218 \u2261.sym \u2218 f\u2257g))\n\nsum-ext+sum-hom\u21d2sum-mono : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumExt sum \u2192 SumHom sum \u2192 SumMono sum\nsum-ext+sum-hom\u21d2sum-mono {sum = sum} sum-ext sum-hom {f} {g} f\u2264\u00b0g =\n sum f \u2264\u27e8 m\u2264m+n _ _ \u27e9\n sum f + sum (\u03bb x \u2192 g x \u2238 f x) \u2261\u27e8 \u2261.sym (sum-hom _ _) \u27e9\n sum (\u03bb x \u2192 f x + (g x \u2238 f x)) \u2261\u27e8 sum-ext (m+n\u2238m\u2261n \u2218 f\u2264\u00b0g) \u27e9\n sum g \u220e where open \u2264-Reasoning\n\nrecord SumPropoid (As : Setoid L.zero L.zero) : \u2605\u2081 where\n constructor _,_\n private\n A = Setoid.Carrier As\n field\n search : Search A\n search-ind : SearchInd search\n\n search-sg-ext : SearchSgExt search\n search-sg-ext sg {f} {g} f\u2248\u00b0g = search-ind (\u03bb s \u2192 s _ f \u2248 s _ g) \u2219-cong f\u2248\u00b0g\n where open Sgrp sg\n\n search-ext : SearchExt search\n search-ext op = search-ind (\u03bb s \u2192 s _ _ \u2261 s _ _) (\u2261.cong\u2082 op)\n\n search-mono : SearchMono search\n search-mono _\u2286_ _\u2219-mono_ {f} {g} f\u2286\u00b0g = search-ind (\u03bb s \u2192 s _ f \u2286 s _ g) _\u2219-mono_ f\u2286\u00b0g\n\n search-swap : SearchSwap search\n search-swap sg f {s\u1d2e} pf = search-ind (\u03bb s \u2192 s _ (s\u1d2e \u2218 f) \u2248 s\u1d2e (s _ \u2218 flip f)) (\u03bb p q \u2192 trans (\u2219-cong p q) (sym (pf _ _))) (\u03bb _ \u2192 refl)\n where open Sgrp sg\n\n searchMon : SearchMon A\n searchMon m = search _\u2219_\n where open Mon m\n\n search-\u03b5 : Search\u03b5 searchMon\n search-\u03b5 m = search-ind (\u03bb s \u2192 s _ (const \u03b5) \u2248 \u03b5) (\u03bb x\u2248\u03b5 y\u2248\u03b5 \u2192 trans (\u2219-cong x\u2248\u03b5 y\u2248\u03b5) (proj\u2081 identity \u03b5)) (\u03bb _ \u2192 refl)\n where open Mon m\n\n search-hom : SearchMonHom searchMon\n search-hom cm f g = search-ind (\u03bb s \u2192 s _ (f \u2219\u00b0 g) \u2248 s _ f \u2219 s _ g)\n (\u03bb p\u2080 p\u2081 \u2192 trans (\u2219-cong p\u2080 p\u2081) (\u2219-interchange _ _ _ _)) (\u03bb _ \u2192 refl)\n where open CMon cm\n\n search-hom\u2032 :\n \u2200 {S T}\n (_+_ : Op\u2082 S)\n (_*_ : Op\u2082 T)\n (f : S \u2192 T)\n (g : A \u2192 S)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ g) \u2261 search _*_ (f \u2218 g)\n search-hom\u2032 _+_ _*_ f g hom = search-ind (\u03bb s \u2192 f (s _+_ g) \u2261 s _*_ (f \u2218 g))\n (\u03bb p q \u2192 \u2261.trans (hom _ _) (\u2261.cong\u2082 _*_ p q))\n (\u03bb _ \u2192 \u2261.refl)\n\n StableUnder : (A \u2192 A) \u2192 \u2605\u2081\n StableUnder p = \u2200 {B} (op : Op\u2082 B) f \u2192 search op f \u2261 search op (f \u2218 p)\n\n sum : Sum A\n sum = search _+_\n\n sum-ind : SumInd sum\n sum-ind P P+ Pf = search-ind (\u03bb s \u2192 P (s _+_)) P+ Pf\n\n sum-ext : SumExt sum\n sum-ext = search-ext _+_\n\n sum-zero : SumZero sum\n sum-zero = search-\u03b5 \u2115+.monoid\n\n sum-hom : SumHom sum\n sum-hom = search-hom \u2115\u00b0.+-commutativeMonoid\n\n sum-mono : SumMono sum\n sum-mono = search-mono _\u2264_ _+-mono_\n\n sum-lin : SumLin sum\n sum-lin f zero = sum-zero\n sum-lin f (suc k) = \u2261.trans (sum-hom f (\u03bb x \u2192 k * f x)) (\u2261.cong\u2082 _+_ (\u2261.refl {x = sum f}) (sum-lin f k))\n\n SumStableUnder : (A \u2192 A) \u2192 \u2605\n SumStableUnder p = \u2200 f \u2192 (\u2200 {x y} \u2192 Setoid._\u2248_ As x y \u2192 f x \u2261.\u2261 f y) \u2192 sum f \u2261 sum (f \u2218 p)\n\n sumStableUnder : \u2200 {p} \u2192 StableUnder p \u2192 SumStableUnder p\n sumStableUnder SU-p f _ = SU-p _+_ f\n\n Card : \u2115\n Card = sum (const 1)\n\n count : Count A\n count f = sum (Bool.to\u2115 \u2218 f)\n\n count-ext : CountExt count\n count-ext f\u2257g = sum-ext (\u2261.cong Bool.to\u2115 \u2218 f\u2257g)\n\n CountStableUnder : (A \u2192 A) \u2192 \u2605\n CountStableUnder p = \u2200 f \u2192 (\u2200 {x y} \u2192 Setoid._\u2248_ As x y \u2192 f x \u2261.\u2261 f y) \u2192 count f \u2261 count (f \u2218 p)\n\n countStableUnder : \u2200 {p} \u2192 SumStableUnder p \u2192 CountStableUnder p\n countStableUnder sumSU-p f f-pres = sumSU-p (Bool.to\u2115 \u2218 f) (\u2261.cong Bool.to\u2115 \u2218 f-pres)\n\n search-extoid : SearchExtoid {As} search\n search-extoid op {f = f}{g} f\u2248g = search-ind (\u03bb s\u2081 \u2192 s\u2081 op f \u2261 s\u2081 op g) (\u2261.cong\u2082 op) (\u03bb x \u2192 f\u2248g (Setoid.refl As))\n\nSumProp : \u2605 \u2192 \u2605\u2081\nSumProp A = SumPropoid (\u2261.setoid A)\n\nopen SumPropoid public\n\nsearch-swap' : \u2200 {A B} cm (\u03bcA : SumPropoid A) (\u03bcB : SumPropoid B) f \u2192\n let open CMon cm\n s\u1d2c = search \u03bcA _\u2219_\n s\u1d2e = search \u03bcB _\u2219_ in\n s\u1d2c (s\u1d2e \u2218 f) \u2248 s\u1d2e (s\u1d2c \u2218 flip f)\nsearch-swap' cm \u03bcA \u03bcB f = search-swap \u03bcA sg f (search-hom \u03bcB cm)\n where open CMon cm\n\nsum-swap : \u2200 {A B} (\u03bcA : SumPropoid A) (\u03bcB : SumPropoid B) f \u2192\n sum \u03bcA (sum \u03bcB \u2218 f) \u2261 sum \u03bcB (sum \u03bcA \u2218 flip f)\nsum-swap = search-swap' \u2115\u00b0.+-commutativeMonoid\n\n_\u2248Sum_ : \u2200 {A} \u2192 (sum\u2080 sum\u2081 : Sum A) \u2192 \u2605\nsum\u2080 \u2248Sum sum\u2081 = \u2200 f \u2192 sum\u2080 f \u2261 sum\u2081 f\n\n_\u2248Search_ : \u2200 {A} \u2192 (s\u2080 s\u2081 : Search A) \u2192 \u2605\u2081\ns\u2080 \u2248Search s\u2081 = \u2200 {B} (op : Op\u2082 B) f \u2192 s\u2080 op f \u2261 s\u2081 op f\n\n\n\u03bc\u22a4 : SumProp \u22a4\n\u03bc\u22a4 = srch , ind\n where\n srch : Search \u22a4\n srch _ f = f _\n\n ind : SearchInd srch\n ind _ _ Pf = Pf _\n\n\u03bcBit : SumProp Bit\n\u03bcBit = searchBit , ind\n where\n searchBit : Search Bit\n searchBit _\u2219_ f = f 0b \u2219 f 1b\n\n ind : SearchInd searchBit\n ind _ _P\u2219_ Pf = Pf 0b P\u2219 Pf 1b\n\ninfixr 4 _+Search_\n\n_+Search_ : \u2200 {A B} \u2192 Search A \u2192 Search B \u2192 Search (A \u228e B)\n(search\u1d2c +Search search\u1d2e) _\u2219_ f = search\u1d2c _\u2219_ (f \u2218 inj\u2081) \u2219 search\u1d2e _\u2219_ (f \u2218 inj\u2082)\n\n_+SearchInd_ : \u2200 {A B} {s\u1d2c : Search A} {s\u1d2e : Search B} \u2192\n SearchInd s\u1d2c \u2192 SearchInd s\u1d2e \u2192 SearchInd (s\u1d2c +Search s\u1d2e)\n(Ps\u1d2c +SearchInd Ps\u1d2e) P P\u2219 Pf\n = P\u2219 (Ps\u1d2c (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2081))) P\u2219 (Pf \u2218 inj\u2081))\n (Ps\u1d2e (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2082))) P\u2219 (Pf \u2218 inj\u2082))\n\ninfixr 4 _+Sum_\n\n_+Sum_ : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u228e B)\n(sum\u1d2c +Sum sum\u1d2e) f = sum\u1d2c (f \u2218 inj\u2081) + sum\u1d2e (f \u2218 inj\u2082)\n\n_+\u03bc_ : \u2200 {A B} \u2192 SumPropoid A \u2192 SumPropoid B \u2192 SumPropoid (A \u228e-setoid B)\n\u03bcA +\u03bc \u03bcB = _ , search-ind \u03bcA +SearchInd search-ind \u03bcB\n\ninfixr 4 _\u00d7Search_\n\n-- liftM2 _,_ in the continuation monad\n_\u00d7Search_ : \u2200 {A B} \u2192 Search A \u2192 Search B \u2192 Search (A \u00d7 B)\n(search\u1d2c \u00d7Search search\u1d2e) op f = search\u1d2c op (\u03bb x \u2192 search\u1d2e op (curry f x))\n\n_\u00d7SearchInd_ : \u2200 {A B} {s\u1d2c : Search A} {s\u1d2e : Search B}\n \u2192 SearchInd s\u1d2c \u2192 SearchInd s\u1d2e \u2192 SearchInd (s\u1d2c \u00d7Search s\u1d2e)\n(Ps\u1d2c \u00d7SearchInd Ps\u1d2e) P P\u2219 Pf =\n Ps\u1d2c (\u03bb s \u2192 P (\u03bb _ _ \u2192 s _ _)) P\u2219 (Ps\u1d2e (\u03bb s \u2192 P (\u03bb _ _ \u2192 s _ _)) P\u2219 \u2218 curry Pf)\n\n_\u00d7SearchExt_ : \u2200 {A B} {s\u1d2c : Search A} {s\u1d2e : Search B} \u2192\n SearchExt s\u1d2c \u2192 SearchExt s\u1d2e \u2192 SearchExt (s\u1d2c \u00d7Search s\u1d2e)\n(s\u1d2c-ext \u00d7SearchExt s\u1d2e-ext) sg f\u2257g = s\u1d2c-ext sg (s\u1d2e-ext sg \u2218 curry f\u2257g)\n\ninfixr 4 _\u00d7Sum_\n\n-- liftM2 _,_ in the continuation monad\n_\u00d7Sum_ : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u00d7 B)\n(sum\u1d2c \u00d7Sum sum\u1d2e) f = sum\u1d2c (\u03bb x\u2080 \u2192\n sum\u1d2e (\u03bb x\u2081 \u2192\n f (x\u2080 , x\u2081)))\n\ninfixr 4 _\u00d7\u03bc_\n\n_\u00d7\u03bc_ : \u2200 {A B} \u2192 SumPropoid A \u2192 SumPropoid B \u2192 SumPropoid (A \u00d7-setoid B)\n\u03bcA \u00d7\u03bc \u03bcB = _ , search-ind \u03bcA \u00d7SearchInd search-ind \u03bcB\n\nsum-const : \u2200 {A} (\u03bcA : SumProp A) \u2192 \u2200 k \u2192 sum \u03bcA (const k) \u2261 Card \u03bcA * k\nsum-const \u03bcA k\n rewrite \u2115\u00b0.*-comm (Card \u03bcA) k\n | \u2261.sym (sum-lin \u03bcA (const 1) k)\n | proj\u2082 \u2115\u00b0.*-identity k = \u2261.refl\n\ninfixr 4 _\u00d7Sum-proj\u2081_ _\u00d7Sum-proj\u2081'_ _\u00d7Sum-proj\u2082_ _\u00d7Sum-proj\u2082'_\n\n_\u00d7Sum-proj\u2081_ : \u2200 {A B}\n (\u03bcA : SumProp A)\n (\u03bcB : SumProp B)\n f \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 Card \u03bcB * sum \u03bcA f\n(\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n rewrite sum-ext \u03bcA (sum-const \u03bcB \u2218 f)\n = sum-lin \u03bcA f (Card \u03bcB)\n\n_\u00d7Sum-proj\u2082_ : \u2200 {A B}\n (\u03bcA : SumProp A)\n (\u03bcB : SumProp B)\n f \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 Card \u03bcA * sum \u03bcB f\n(\u03bcA \u00d7Sum-proj\u2082 \u03bcB) f = sum-const \u03bcA (sum \u03bcB f)\n\n_\u00d7Sum-proj\u2081'_ : \u2200 {A B}\n (\u03bcA : SumProp A) (\u03bcB : SumProp B)\n {f} {g} \u2192\n sum \u03bcA f \u2261 sum \u03bcA g \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 sum (\u03bcA \u00d7\u03bc \u03bcB) (g \u2218 proj\u2081)\n(\u03bcA \u00d7Sum-proj\u2081' \u03bcB) {f} {g} sumf\u2261sumg\n rewrite (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n | (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) g\n | sumf\u2261sumg = \u2261.refl\n\n_\u00d7Sum-proj\u2082'_ : \u2200 {A B}\n (\u03bcA : SumProp A) (\u03bcB : SumProp B)\n {f} {g} \u2192\n sum \u03bcB f \u2261 sum \u03bcB g \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 sum (\u03bcA \u00d7\u03bc \u03bcB) (g \u2218 proj\u2082)\n(\u03bcA \u00d7Sum-proj\u2082' \u03bcB) sumf\u2261sumg = sum-ext \u03bcA (const sumf\u2261sumg)\n\n\u03bc-view : \u2200 {A B} \u2192 (A \u2192 B) \u2192 SumProp A \u2192 SumProp B\n\u03bc-view {A}{B} A\u2192B \u03bcA = search\u1d2e , ind\n where\n search\u1d2e : Search B\n search\u1d2e m f = search \u03bcA m (f \u2218 A\u2192B)\n\n ind : SearchInd search\u1d2e\n ind P P\u2219 Pf = search-ind \u03bcA (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 A\u2192B))) P\u2219 (Pf \u2218 A\u2192B)\n\n\u03bc-iso : \u2200 {A B} \u2192 (A \u2194 B) \u2192 SumProp A \u2192 SumProp B\n\u03bc-iso A\u2194B = \u03bc-view (_\u27e8$\u27e9_ (Inverse.to A\u2194B))\n\n\u03bc-view-preserve : \u2200 {A B} (A\u2192B : A \u2192 B)(B\u2192A : B \u2192 A)(A\u2248B : id \u2257 B\u2192A \u2218 A\u2192B) f (\u03bcA : SumProp A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-view A\u2192B \u03bcA) (f \u2218 B\u2192A)\n\u03bc-view-preserve A\u2192B B\u2192A A\u2248B f \u03bcA = sum-ext \u03bcA (\u2261.cong f \u2218 A\u2248B)\n\n\u03bc-iso-preserve : \u2200 {A B} (A\u2194B : A \u2194 B) f (\u03bcA : SumProp A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-iso A\u2194B \u03bcA) (f \u2218 _\u27e8$\u27e9_ (Inverse.from A\u2194B))\n\u03bc-iso-preserve A\u2194B f \u03bcA = \u03bc-view-preserve (_\u27e8$\u27e9_ (Inverse.to A\u2194B)) (_\u27e8$\u27e9_ (Inverse.from A\u2194B))\n (\u2261.sym \u2218 Inverse.left-inverse-of A\u2194B) f \u03bcA\n\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Vec.NP as Vec using (Vec; tabulate; _++_) renaming (map to vmap; sum to vsum; foldr to vfoldr; foldr\u2081 to vfoldr\u2081)\n\nvmsum : \u2200 m {n} \u2192 let open Mon m in\n Vec C n \u2192 C\nvmsum m = vfoldr _ _\u2219_ \u03b5\n where open Monoid m\n\nvsgsum : \u2200 sg {n} \u2192 let open Sgrp sg in\n Vec C (suc n) \u2192 C\nvsgsum sg = vfoldr\u2081 _\u2219_\n where open Sgrp sg\n\n-- let's recall that: tabulate f \u2257 vmap f (allFin n)\n\n-- searchMonFin : \u2200 n \u2192 SearchMon (Fin n)\n-- searchMonFin n m f = vmsum m (tabulate f)\n\nsearchFinSuc : \u2200 n \u2192 Search (Fin (suc n))\nsearchFinSuc n _\u2219_ f = vfoldr\u2081 _\u2219_ (tabulate f)\n{-\n\n\u03bcMaybe : \u2200 {A} \u2192 SumProp A \u2192 SumProp (Maybe A)\n\u03bcMaybe \u03bcA = \u03bc-iso (FI.sym Maybe\u2194\u22a4\u228e) (\u03bc\u22a4 +\u03bc \u03bcA)\n\n\u03bcMaybe^ : \u2200 {A} n \u2192 SumPropoid A \u2192 SumPropoid (Maybe^ n A)\n\u03bcMaybe^ zero \u03bcA = \u03bcA\n\u03bcMaybe^ (suc n) \u03bcA = \u03bcMaybe (\u03bcMaybe^ n \u03bcA)\n-}\n\u03bcFinSuc : \u2200 n \u2192 SumProp (Fin (suc n))\n\u03bcFinSuc n = searchFinSuc n , ind n\n where ind : \u2200 n \u2192 SearchInd (searchFinSuc n)\n ind zero P P\u2219 Pf = Pf zero\n ind (suc n) P P\u2219 Pf = P\u2219 (Pf zero) (ind n (\u03bb s \u2192 P (\u03bb op f \u2192 s op (f \u2218 suc))) P\u2219 (Pf \u2218 suc))\n\n{-\n\n\u03bcFinSucIso : \u2200 n \u2192 SumProp (Fin (suc n))\n\u03bcFinSucIso n = \u03bc-iso (Maybe^\u22a4\u2194Fin1+ n) (\u03bcMaybe^ n \u03bc\u22a4)\n\n\u03bc^ : \u2200 {A} (\u03bcA : SumProp A) n \u2192 SumProp (A ^ n)\n\u03bc^ \u03bcA zero = \u03bc\u22a4\n\u03bc^ \u03bcA (suc n) = \u03bcA \u00d7\u03bc \u03bc^ \u03bcA n\n\n\u03bcVec : \u2200 {A} (\u03bcA : SumProp A) n \u2192 SumProp (Vec A n)\n\u03bcVec \u03bcA n = \u03bc-iso (^\u2194Vec n) (\u03bc^ \u03bcA n)\n\nsearchVec : \u2200 {A} n \u2192 Search A \u2192 Search (Vec A n)\nsearchVec zero search\u1d2c op f = f []\nsearchVec (suc n) search\u1d2c op f = search\u1d2c op (\u03bb x \u2192 searchVec n search\u1d2c op (f \u2218 _\u2237_ x))\n\nsearchVec-spec : \u2200 {A} (\u03bcA : SumProp A) n \u2192 searchVec n (search \u03bcA) \u2248Search search (\u03bcVec \u03bcA n)\nsearchVec-spec \u03bcA zero op f = \u2261.refl\nsearchVec-spec \u03bcA (suc n) op f = search-ext \u03bcA op (\u03bb x \u2192 searchVec-spec \u03bcA n op (f \u2218 _\u2237_ x))\n\nsearchVec-++ : \u2200 {A} n {m} (\u03bcA : SumProp A) sg\n \u2192 let open Sgrp sg in\n (f : Vec A (n + m) \u2192 C)\n \u2192 search (\u03bcVec \u03bcA (n + m)) _\u2219_ f\n \u2248 search (\u03bcVec \u03bcA n) _\u2219_ (\u03bb xs \u2192\n search (\u03bcVec \u03bcA m) _\u2219_ (\u03bb ys \u2192\n f (xs ++ ys)))\nsearchVec-++ zero \u03bcA sg f = Sgrp.refl sg\nsearchVec-++ (suc n) \u03bcA sg f = search-sg-ext \u03bcA sg (\u03bb x \u2192\n searchVec-++ n \u03bcA sg (f \u2218 _\u2237_ x))\n\nsumVec-swap : \u2200 {A} n {m} (\u03bcA : SumProp A)(f : Vec A (n + m) \u2192 \u2115)\n \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 f (xs ++ ys)))\nsumVec-swap n {m} \u03bcA f = sum-swap (\u03bcVec \u03bcA n) (\u03bcVec \u03bcA m) (\u03bb xs ys \u2192 f (xs ++ ys))\n\nswapS : \u2200 {A B} \u2192 SumProp (A \u00d7 B) \u2192 SumProp (B \u00d7 A)\nswapS = \u03bc-iso swap-iso\n\nswapS-preserve : \u2200 {A B} f (\u03bcA\u00d7B : SumProp (A \u00d7 B)) \u2192 sum \u03bcA\u00d7B f \u2261 sum (swapS \u03bcA\u00d7B) (f \u2218 swap)\nswapS-preserve = \u03bc-iso-preserve swap-iso\n\nmodule _ {A : Set}(\u03bcA : SumProp A) where\n\n sA = search \u03bcA\n\n extend : \u2200 {n} \u2192 A \u2192 (Fin n \u2192 A) \u2192 Fin (suc n) \u2192 A\n extend x g zero = x\n extend x g (suc i) = g i\n\n abs : Fin 0 \u2192 A\n abs ()\n\n -- There is one function Fin 0 \u2192 A (called abs) so this should be fine\n -- if not there is a version below that forces the domain to be non-empty\n sFun : \u2200 n \u2192 Search (Fin n \u2192 A)\n sFun zero op f = f abs\n sFun (suc n) op f = sA op (\u03bb x \u2192 sFun n op (f \u2218 extend x))\n\n Ind : \u2200 n \u2192 SearchInd (sFun n)\n Ind zero P P\u2219 Pf = Pf abs\n Ind (suc n) P P\u2219 Pf = \n search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (\u03bb x \u2192 sFun n op (f \u2218 extend x)))) \n P\u2219 \n (\u03bb x \u2192 Ind n (\u03bb sf \u2192 P (\u03bb op f \u2192 sf op (f \u2218 extend x))) \n P\u2219 (Pf \u2218 extend x))\n\n \u03bcFun : \u2200 {n} \u2192 SumProp (Fin n \u2192 A)\n \u03bcFun = sFun _ , Ind _\n\n{-\n -- If we want to force non-empty domain\n\n sFun : \u2200 n \u2192 Search (Fin (suc n) \u2192 A)\n sFun zero op f = sA op (f \u2218 const)\n sFun (suc n) op f = sA op (\u03bb x \u2192 sFun n op (f \u2218 extend x))\n\n Ind : \u2200 n \u2192 SearchInd (sFun n)\n Ind zero P P\u2219 Pf = search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (f \u2218 const))) P\u2219 (Pf \u2218 const)\n Ind (suc n) P P\u2219 Pf = search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (\u03bb x \u2192 sFun n op (f \u2218 extend x)))) \n P\u2219 \n (\u03bb x \u2192 Ind n (\u03bb sf \u2192 P (\u03bb op f \u2192 sf op (f \u2218 extend x))) P\u2219 (Pf \u2218 extend x))\n\n-}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"import Level as L\nopen import Type\nopen import Function\nopen import Algebra\nopen import Algebra.FunctionProperties.NP\nopen import Data.Nat.NP hiding (_^_)\nopen import Data.Nat.Properties\nopen import Data.Unit hiding (_\u2264_)\nopen import Data.Sum\nopen import Data.Maybe.NP\nopen import Data.Product\nopen import Data.Bits\nopen import Data.Bool.NP as Bool\nopen import Function.Equality using (_\u27e8$\u27e9_)\nimport Function.Inverse as FI\nopen FI using (_\u2194_; module Inverse)\nimport Function.Related as FR\nopen import Function.Related.TypeIsomorphisms.NP\nopen import Relation.Binary.NP\nimport Relation.Binary.PropositionalEquality as \u2261\nopen \u2261 using (_\u2261_; _\u2257_)\n\nmodule sum-setoid where\n\nSEToid = Setoid L.zero L.zero\n\n_\u2264\u00b0_ : \u2200 {A : \u2605}(f g : A \u2192 \u2115) \u2192 \u2605\nf \u2264\u00b0 g = \u2200 x \u2192 f x \u2264 g x\n\nSemigroup\u2080 = Semigroup L.zero L.zero\nMonoid\u2080 = Monoid L.zero L.zero\nCommutativeMonoid\u2080 = CommutativeMonoid L.zero L.zero\n\nmodule SgrpExtra (sg : Semigroup\u2080) where\n open Semigroup sg\n open Setoid-Reasoning (Semigroup.setoid sg) public\n C : \u2605\n C = Carrier\n _\u2248\u00b0_ : \u2200 {A : \u2605} (f g : A \u2192 C) \u2192 \u2605\n f \u2248\u00b0 g = \u2200 x \u2192 f x \u2248 g x\n _\u2219\u00b0_ : \u2200 {A : \u2605} (f g : A \u2192 C) \u2192 A \u2192 C\n (f \u2219\u00b0 g) x = f x \u2219 g x\n infixl 7 _-\u2219-_\n _-\u2219-_ : _\u2219_ Preserves\u2082 _\u2248_ \u27f6 _\u2248_ \u27f6 _\u2248_\n _-\u2219-_ = \u2219-cong\n\nmodule Sgrp (sg : Semigroup\u2080) where\n open Semigroup sg public\n open SgrpExtra sg public\n\nmodule Mon (m : Monoid\u2080) where\n open Monoid m public\n sg = semigroup\n open SgrpExtra semigroup public\n\nmodule CMon (cm : CommutativeMonoid\u2080) where\n open CommutativeMonoid cm public\n sg = semigroup\n m = monoid\n open SgrpExtra sg public\n\n \u2219-interchange : Interchange _\u2248_ _\u2219_ _\u2219_\n \u2219-interchange = InterchangeFromAssocCommCong.\u2219-interchange\n _\u2248_ isEquivalence\n _\u2219_ assoc comm (\u03bb _ \u2192 flip \u2219-cong refl)\n\nSearch : \u2605 \u2192 \u2605\u2081\nSearch A = \u2200 {B} \u2192 (_\u2219_ : B \u2192 B \u2192 B) \u2192 (A \u2192 B) \u2192 B\n-- Search A = \u2200 {I : \u2605} {F : I \u2192 \u2605} \u2192 (_\u2219_ : \u2200 {i} \u2192 F i \u2192 F i \u2192 F i) \u2192 \u2200 {i} \u2192 (A \u2192 F i) \u2192 F i\n\nSearchMon : \u2605 \u2192 \u2605\u2081\nSearchMon A = (m : Monoid\u2080) \u2192 let open Mon m in\n (A \u2192 C) \u2192 C\n\nsearchMonFromSearch : \u2200 {A} \u2192 Search A \u2192 SearchMon A\nsearchMonFromSearch s m = s _\u2219_ where open Mon m\n\nSum : \u2605 \u2192 \u2605\nSum A = (A \u2192 \u2115) \u2192 \u2115\n\nCount : \u2605 \u2192 \u2605\nCount A = (A \u2192 Bit) \u2192 \u2115\n\nSearchInd : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchInd {A} srch = \u2200 (P : Search A \u2192 \u2605)\n (P\u2219 : \u2200 {s\u2080 s\u2081 : Search A} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb _\u2219_ f \u2192 s\u2080 _\u2219_ f \u2219 s\u2081 _\u2219_ f))\n (Pf : \u2200 x \u2192 P (\u03bb _ f \u2192 f x))\n \u2192 P srch\n\nSumInd : \u2200 {A} \u2192 Sum A \u2192 \u2605\u2081\nSumInd {A} sum = \u2200 (P : Sum A \u2192 \u2605)\n (P+ : \u2200 {s\u2080 s\u2081 : Sum A} \u2192 P s\u2080 \u2192 P s\u2081 \u2192 P (\u03bb f \u2192 s\u2080 f + s\u2081 f))\n (Pf : \u2200 x \u2192 P (\u03bb f \u2192 f x))\n \u2192 P sum\n\nSearchMono : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchMono s\u1d2c = \u2200 {C} (_\u2286_ : C \u2192 C \u2192 \u2605) \u2192\n \u2200 {_\u2219_} (\u2219-mono : _\u2219_ Preserves\u2082 _\u2286_ \u27f6 _\u2286_ \u27f6 _\u2286_)\n {f g} \u2192\n (\u2200 x \u2192 f x \u2286 g x) \u2192 s\u1d2c _\u2219_ f \u2286 s\u1d2c _\u2219_ g\n\nSearchSgExt : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchSgExt s\u1d2c = \u2200 sg {f g} \u2192 let open Sgrp sg in\n f \u2248\u00b0 g \u2192 s\u1d2c _\u2219_ f \u2248 s\u1d2c _\u2219_ g\n\nSearchExt : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchExt {A} s\u1d2c = \u2200 {B} op {f g : A \u2192 B} \u2192 f \u2257 g \u2192 s\u1d2c op f \u2261 s\u1d2c op g\n\nSearchExtFun : \u2200 {A B} \u2192 Search (A \u2192 B) \u2192 \u2605\u2081\nSearchExtFun {A}{B} s\u1d2c\u1d2e = \u2200 {M} op {f g : (A \u2192 B) \u2192 M} \u2192 (\u2200 {\u03c6 \u03c8} \u2192 \u03c6 \u2257 \u03c8 \u2192 f \u03c6 \u2261 g \u03c8) \u2192 s\u1d2c\u1d2e op f \u2261 s\u1d2c\u1d2e op g\n\nSearchExtFun-\u00faber : \u2200 {A B} \u2192 (SF : Search (A \u2192 B)) \u2192 SearchInd SF \u2192 SearchExtFun SF\nSearchExtFun-\u00faber sf sf-ind op {f = f}{g} f\u2248g = sf-ind (\u03bb s \u2192 s op f \u2261 s op g) (\u2261.cong\u2082 op) (\u03bb x \u2192 f\u2248g (\u03bb _ \u2192 \u2261.refl))\n\nSearchExtoid : \u2200 {A : Setoid L.zero L.zero} \u2192 Search (Setoid.Carrier A) \u2192 \u2605\u2081\nSearchExtoid {A} s\u1d2c = \u2200 {M} op {f g : Setoid.Carrier A \u2192 M} \u2192 (\u2200 {x y} \u2192 Setoid._\u2248_ A x y \u2192 f x \u2261 g y) \u2192 s\u1d2c op f \u2261 s\u1d2c op g\n\nSumExt : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumExt sum\u1d2c = \u2200 {f g} \u2192 f \u2257 g \u2192 sum\u1d2c f \u2261 sum\u1d2c g\n\nCountExt : \u2200 {A} \u2192 Count A \u2192 \u2605\nCountExt count\u1d2c = \u2200 {f g} \u2192 f \u2257 g \u2192 count\u1d2c f \u2261 count\u1d2c g\n\nSearch\u03b5 : \u2200 {A} \u2192 SearchMon A \u2192 \u2605\u2081\nSearch\u03b5 s\u1d2c = \u2200 m \u2192 let open Mon m in\n s\u1d2c m (const \u03b5) \u2248 \u03b5\n\nSumZero : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumZero sum\u1d2c = sum\u1d2c (\u03bb _ \u2192 0) \u2261 0\n\nSumLin : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumLin sum\u1d2c = \u2200 f k \u2192 sum\u1d2c (\u03bb x \u2192 k * f x) \u2261 k * sum\u1d2c f\n\nSearchHom : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchHom s\u1d2c = \u2200 sg f g \u2192 let open Sgrp sg in\n s\u1d2c _\u2219_ (f \u2219\u00b0 g) \u2248 s\u1d2c _\u2219_ f \u2219 s\u1d2c _\u2219_ g\n\nSearchMonHom : \u2200 {A} \u2192 SearchMon A \u2192 \u2605\u2081\nSearchMonHom s\u1d2c = \u2200 (cm : CommutativeMonoid\u2080) f g \u2192\n let open CMon cm in\n s\u1d2c m (f \u2219\u00b0 g) \u2248 s\u1d2c m f \u2219 s\u1d2c m g\n\nSumHom : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumHom sum\u1d2c = \u2200 f g \u2192 sum\u1d2c (\u03bb x \u2192 f x + g x) \u2261 sum\u1d2c f + sum\u1d2c g\n\nSumMono : \u2200 {A} \u2192 Sum A \u2192 \u2605\nSumMono sum\u1d2c = \u2200 {f g} \u2192 f \u2264\u00b0 g \u2192 sum\u1d2c f \u2264 sum\u1d2c g\n\nSearchSwap : \u2200 {A} \u2192 Search A \u2192 \u2605\u2081\nSearchSwap {A} s\u1d2c = \u2200 {B} sg f \u2192\n let open Sgrp sg in \u2200 {s\u1d2e : (B \u2192 C) \u2192 C}\n \u2192 (hom : \u2200 f g \u2192 s\u1d2e (f \u2219\u00b0 g) \u2248 s\u1d2e f \u2219 s\u1d2e g)\n \u2192 s\u1d2c _\u2219_ (s\u1d2e \u2218 f) \u2248 s\u1d2e (s\u1d2c _\u2219_ \u2218 flip f)\n\nsum-lin\u21d2sum-zero : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumLin sum \u2192 SumZero sum\nsum-lin\u21d2sum-zero sum-lin = sum-lin (\u03bb _ \u2192 0) 0\n\nsum-mono\u21d2sum-ext : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumMono sum \u2192 SumExt sum\nsum-mono\u21d2sum-ext sum-mono f\u2257g = \u2115\u2264.antisym (sum-mono (\u2115\u2264.reflexive \u2218 f\u2257g)) (sum-mono (\u2115\u2264.reflexive \u2218 \u2261.sym \u2218 f\u2257g))\n\nsum-ext+sum-hom\u21d2sum-mono : \u2200 {A} \u2192 {sum : Sum A} \u2192 SumExt sum \u2192 SumHom sum \u2192 SumMono sum\nsum-ext+sum-hom\u21d2sum-mono {sum = sum} sum-ext sum-hom {f} {g} f\u2264\u00b0g =\n sum f \u2264\u27e8 m\u2264m+n _ _ \u27e9\n sum f + sum (\u03bb x \u2192 g x \u2238 f x) \u2261\u27e8 \u2261.sym (sum-hom _ _) \u27e9\n sum (\u03bb x \u2192 f x + (g x \u2238 f x)) \u2261\u27e8 sum-ext (m+n\u2238m\u2261n \u2218 f\u2264\u00b0g) \u27e9\n sum g \u220e where open \u2264-Reasoning\n\nrecord SumPropoid (As : Setoid L.zero L.zero) : \u2605\u2081 where\n constructor _,_\n private\n A = Setoid.Carrier As\n field\n search : Search A\n search-ind : SearchInd search\n\n search-sg-ext : SearchSgExt search\n search-sg-ext sg {f} {g} f\u2248\u00b0g = search-ind (\u03bb s \u2192 s _ f \u2248 s _ g) \u2219-cong f\u2248\u00b0g\n where open Sgrp sg\n\n search-ext : SearchExt search\n search-ext op = search-ind (\u03bb s \u2192 s _ _ \u2261 s _ _) (\u2261.cong\u2082 op)\n\n search-mono : SearchMono search\n search-mono _\u2286_ _\u2219-mono_ {f} {g} f\u2286\u00b0g = search-ind (\u03bb s \u2192 s _ f \u2286 s _ g) _\u2219-mono_ f\u2286\u00b0g\n\n search-swap : SearchSwap search\n search-swap sg f {s\u1d2e} pf = search-ind (\u03bb s \u2192 s _ (s\u1d2e \u2218 f) \u2248 s\u1d2e (s _ \u2218 flip f)) (\u03bb p q \u2192 trans (\u2219-cong p q) (sym (pf _ _))) (\u03bb _ \u2192 refl)\n where open Sgrp sg\n\n searchMon : SearchMon A\n searchMon m = search _\u2219_\n where open Mon m\n\n search-\u03b5 : Search\u03b5 searchMon\n search-\u03b5 m = search-ind (\u03bb s \u2192 s _ (const \u03b5) \u2248 \u03b5) (\u03bb x\u2248\u03b5 y\u2248\u03b5 \u2192 trans (\u2219-cong x\u2248\u03b5 y\u2248\u03b5) (proj\u2081 identity \u03b5)) (\u03bb _ \u2192 refl)\n where open Mon m\n\n search-hom : SearchMonHom searchMon\n search-hom cm f g = search-ind (\u03bb s \u2192 s _ (f \u2219\u00b0 g) \u2248 s _ f \u2219 s _ g)\n (\u03bb p\u2080 p\u2081 \u2192 trans (\u2219-cong p\u2080 p\u2081) (\u2219-interchange _ _ _ _)) (\u03bb _ \u2192 refl)\n where open CMon cm\n\n search-hom\u2032 :\n \u2200 {S T}\n (_+_ : Op\u2082 S)\n (_*_ : Op\u2082 T)\n (f : S \u2192 T)\n (g : A \u2192 S)\n (hom : \u2200 x y \u2192 f (x + y) \u2261 f x * f y)\n \u2192 f (search _+_ g) \u2261 search _*_ (f \u2218 g)\n search-hom\u2032 _+_ _*_ f g hom = search-ind (\u03bb s \u2192 f (s _+_ g) \u2261 s _*_ (f \u2218 g))\n (\u03bb p q \u2192 \u2261.trans (hom _ _) (\u2261.cong\u2082 _*_ p q))\n (\u03bb _ \u2192 \u2261.refl)\n\n StableUnder : (A \u2192 A) \u2192 \u2605\u2081\n StableUnder p = \u2200 {B} (op : Op\u2082 B) f \u2192 search op f \u2261 search op (f \u2218 p)\n\n sum : Sum A\n sum = search _+_\n\n sum-ind : SumInd sum\n sum-ind P P+ Pf = search-ind (\u03bb s \u2192 P (s _+_)) P+ Pf\n\n sum-ext : SumExt sum\n sum-ext = search-ext _+_\n\n sum-zero : SumZero sum\n sum-zero = search-\u03b5 \u2115+.monoid\n\n sum-hom : SumHom sum\n sum-hom = search-hom \u2115\u00b0.+-commutativeMonoid\n\n sum-mono : SumMono sum\n sum-mono = search-mono _\u2264_ _+-mono_\n\n sum-lin : SumLin sum\n sum-lin f zero = sum-zero\n sum-lin f (suc k) = \u2261.trans (sum-hom f (\u03bb x \u2192 k * f x)) (\u2261.cong\u2082 _+_ (\u2261.refl {x = sum f}) (sum-lin f k))\n\n SumStableUnder : (A \u2192 A) \u2192 \u2605\n SumStableUnder p = \u2200 f \u2192 (\u2200 {x y} \u2192 Setoid._\u2248_ As x y \u2192 f x \u2261.\u2261 f y) \u2192 sum f \u2261 sum (f \u2218 p)\n\n sumStableUnder : \u2200 {p} \u2192 StableUnder p \u2192 SumStableUnder p\n sumStableUnder SU-p f _ = SU-p _+_ f\n\n Card : \u2115\n Card = sum (const 1)\n\n count : Count A\n count f = sum (Bool.to\u2115 \u2218 f)\n\n count-ext : CountExt count\n count-ext f\u2257g = sum-ext (\u2261.cong Bool.to\u2115 \u2218 f\u2257g)\n\n CountStableUnder : (A \u2192 A) \u2192 \u2605\n CountStableUnder p = \u2200 f \u2192 (\u2200 {x y} \u2192 Setoid._\u2248_ As x y \u2192 f x \u2261.\u2261 f y) \u2192 count f \u2261 count (f \u2218 p)\n\n countStableUnder : \u2200 {p} \u2192 SumStableUnder p \u2192 CountStableUnder p\n countStableUnder sumSU-p f f-pres = sumSU-p (Bool.to\u2115 \u2218 f) (\u2261.cong Bool.to\u2115 \u2218 f-pres)\n\n search-extoid : SearchExtoid {As} search\n search-extoid op {f = f}{g} f\u2248g = search-ind (\u03bb s\u2081 \u2192 s\u2081 op f \u2261 s\u2081 op g) (\u2261.cong\u2082 op) (\u03bb x \u2192 f\u2248g (Setoid.refl As))\n\nopen SumPropoid public\n\nsearch-swap' : \u2200 {A B} cm (\u03bcA : SumPropoid A) (\u03bcB : SumPropoid B) f \u2192\n let open CMon cm\n s\u1d2c = search \u03bcA _\u2219_\n s\u1d2e = search \u03bcB _\u2219_ in\n s\u1d2c (s\u1d2e \u2218 f) \u2248 s\u1d2e (s\u1d2c \u2218 flip f)\nsearch-swap' cm \u03bcA \u03bcB f = search-swap \u03bcA sg f (search-hom \u03bcB cm)\n where open CMon cm\n\nsum-swap : \u2200 {A B} (\u03bcA : SumPropoid A) (\u03bcB : SumPropoid B) f \u2192\n sum \u03bcA (sum \u03bcB \u2218 f) \u2261 sum \u03bcB (sum \u03bcA \u2218 flip f)\nsum-swap = search-swap' \u2115\u00b0.+-commutativeMonoid\n\n_\u2248Sum_ : \u2200 {A} \u2192 (sum\u2080 sum\u2081 : Sum A) \u2192 \u2605\nsum\u2080 \u2248Sum sum\u2081 = \u2200 f \u2192 sum\u2080 f \u2261 sum\u2081 f\n\n_\u2248Search_ : \u2200 {A} \u2192 (s\u2080 s\u2081 : Search A) \u2192 \u2605\u2081\ns\u2080 \u2248Search s\u2081 = \u2200 {B} (op : Op\u2082 B) f \u2192 s\u2080 op f \u2261 s\u2081 op f\n\n{-\n\u03bc\u22a4 : SumProp \u22a4\n\u03bc\u22a4 = srch , ind\n where\n srch : Search \u22a4\n srch _ f = f _\n\n ind : SearchInd srch\n ind _ _ Pf = Pf _\n\n\u03bcBit : SumProp Bit\n\u03bcBit = searchBit , ind\n where\n searchBit : Search Bit\n searchBit _\u2219_ f = f 0b \u2219 f 1b\n\n ind : SearchInd searchBit\n ind _ _P\u2219_ Pf = Pf 0b P\u2219 Pf 1b\n\ninfixr 4 _+Search_\n\n_+Search_ : \u2200 {A B} \u2192 Search A \u2192 Search B \u2192 Search (A \u228e B)\n(search\u1d2c +Search search\u1d2e) _\u2219_ f = search\u1d2c _\u2219_ (f \u2218 inj\u2081) \u2219 search\u1d2e _\u2219_ (f \u2218 inj\u2082)\n\n_+SearchInd_ : \u2200 {A B} {s\u1d2c : Search A} {s\u1d2e : Search B} \u2192\n SearchInd s\u1d2c \u2192 SearchInd s\u1d2e \u2192 SearchInd (s\u1d2c +Search s\u1d2e)\n(Ps\u1d2c +SearchInd Ps\u1d2e) P P\u2219 Pf\n = P\u2219 (Ps\u1d2c (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2081))) P\u2219 (Pf \u2218 inj\u2081))\n (Ps\u1d2e (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 inj\u2082))) P\u2219 (Pf \u2218 inj\u2082))\n\ninfixr 4 _+Sum_\n\n_+Sum_ : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u228e B)\n(sum\u1d2c +Sum sum\u1d2e) f = sum\u1d2c (f \u2218 inj\u2081) + sum\u1d2e (f \u2218 inj\u2082)\n\n_+\u03bc_ : \u2200 {A B} \u2192 SumProp A \u2192 SumProp B \u2192 SumProp (A \u228e B)\n\u03bcA +\u03bc \u03bcB = _ , search-ind \u03bcA +SearchInd search-ind \u03bcB\n\ninfixr 4 _\u00d7Search_\n\n-- liftM2 _,_ in the continuation monad\n_\u00d7Search_ : \u2200 {A B} \u2192 Search A \u2192 Search B \u2192 Search (A \u00d7 B)\n(search\u1d2c \u00d7Search search\u1d2e) op f = search\u1d2c op (\u03bb x \u2192 search\u1d2e op (curry f x))\n\n_\u00d7SearchInd_ : \u2200 {A B} {s\u1d2c : Search A} {s\u1d2e : Search B}\n \u2192 SearchInd s\u1d2c \u2192 SearchInd s\u1d2e \u2192 SearchInd (s\u1d2c \u00d7Search s\u1d2e)\n(Ps\u1d2c \u00d7SearchInd Ps\u1d2e) P P\u2219 Pf =\n Ps\u1d2c (\u03bb s \u2192 P (\u03bb _ _ \u2192 s _ _)) P\u2219 (Ps\u1d2e (\u03bb s \u2192 P (\u03bb _ _ \u2192 s _ _)) P\u2219 \u2218 curry Pf)\n\n_\u00d7SearchExt_ : \u2200 {A B} {s\u1d2c : Search A} {s\u1d2e : Search B} \u2192\n SearchExt s\u1d2c \u2192 SearchExt s\u1d2e \u2192 SearchExt (s\u1d2c \u00d7Search s\u1d2e)\n(s\u1d2c-ext \u00d7SearchExt s\u1d2e-ext) sg f\u2257g = s\u1d2c-ext sg (s\u1d2e-ext sg \u2218 curry f\u2257g)\n\ninfixr 4 _\u00d7Sum_\n\n-- liftM2 _,_ in the continuation monad\n_\u00d7Sum_ : \u2200 {A B} \u2192 Sum A \u2192 Sum B \u2192 Sum (A \u00d7 B)\n(sum\u1d2c \u00d7Sum sum\u1d2e) f = sum\u1d2c (\u03bb x\u2080 \u2192\n sum\u1d2e (\u03bb x\u2081 \u2192\n f (x\u2080 , x\u2081)))\n\ninfixr 4 _\u00d7\u03bc_\n\n_\u00d7\u03bc_ : \u2200 {A B} \u2192 SumProp A \u2192 SumProp B \u2192 SumProp (A \u00d7 B)\n\u03bcA \u00d7\u03bc \u03bcB = _ , search-ind \u03bcA \u00d7SearchInd search-ind \u03bcB\n\nsum-const : \u2200 {A} (\u03bcA : SumProp A) \u2192 \u2200 k \u2192 sum \u03bcA (const k) \u2261 Card \u03bcA * k\nsum-const \u03bcA k\n rewrite \u2115\u00b0.*-comm (Card \u03bcA) k\n | \u2261.sym (sum-lin \u03bcA (const 1) k)\n | proj\u2082 \u2115\u00b0.*-identity k = \u2261.refl\n\ninfixr 4 _\u00d7Sum-proj\u2081_ _\u00d7Sum-proj\u2081'_ _\u00d7Sum-proj\u2082_ _\u00d7Sum-proj\u2082'_\n\n_\u00d7Sum-proj\u2081_ : \u2200 {A B}\n (\u03bcA : SumProp A)\n (\u03bcB : SumProp B)\n f \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 Card \u03bcB * sum \u03bcA f\n(\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n rewrite sum-ext \u03bcA (sum-const \u03bcB \u2218 f)\n = sum-lin \u03bcA f (Card \u03bcB)\n\n_\u00d7Sum-proj\u2082_ : \u2200 {A B}\n (\u03bcA : SumProp A)\n (\u03bcB : SumProp B)\n f \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 Card \u03bcA * sum \u03bcB f\n(\u03bcA \u00d7Sum-proj\u2082 \u03bcB) f = sum-const \u03bcA (sum \u03bcB f)\n\n_\u00d7Sum-proj\u2081'_ : \u2200 {A B}\n (\u03bcA : SumProp A) (\u03bcB : SumProp B)\n {f} {g} \u2192\n sum \u03bcA f \u2261 sum \u03bcA g \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2081) \u2261 sum (\u03bcA \u00d7\u03bc \u03bcB) (g \u2218 proj\u2081)\n(\u03bcA \u00d7Sum-proj\u2081' \u03bcB) {f} {g} sumf\u2261sumg\n rewrite (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) f\n | (\u03bcA \u00d7Sum-proj\u2081 \u03bcB) g\n | sumf\u2261sumg = \u2261.refl\n\n_\u00d7Sum-proj\u2082'_ : \u2200 {A B}\n (\u03bcA : SumProp A) (\u03bcB : SumProp B)\n {f} {g} \u2192\n sum \u03bcB f \u2261 sum \u03bcB g \u2192\n sum (\u03bcA \u00d7\u03bc \u03bcB) (f \u2218 proj\u2082) \u2261 sum (\u03bcA \u00d7\u03bc \u03bcB) (g \u2218 proj\u2082)\n(\u03bcA \u00d7Sum-proj\u2082' \u03bcB) sumf\u2261sumg = sum-ext \u03bcA (const sumf\u2261sumg)\n\n\u03bc-view : \u2200 {A B} \u2192 (A \u2192 B) \u2192 SumProp A \u2192 SumProp B\n\u03bc-view {A}{B} A\u2192B \u03bcA = search\u1d2e , ind\n where\n search\u1d2e : Search B\n search\u1d2e m f = search \u03bcA m (f \u2218 A\u2192B)\n\n ind : SearchInd search\u1d2e\n ind P P\u2219 Pf = search-ind \u03bcA (\u03bb s \u2192 P (\u03bb _ f \u2192 s _ (f \u2218 A\u2192B))) P\u2219 (Pf \u2218 A\u2192B)\n\n\u03bc-iso : \u2200 {A B} \u2192 (A \u2194 B) \u2192 SumProp A \u2192 SumProp B\n\u03bc-iso A\u2194B = \u03bc-view (_\u27e8$\u27e9_ (Inverse.to A\u2194B))\n\n\u03bc-view-preserve : \u2200 {A B} (A\u2192B : A \u2192 B)(B\u2192A : B \u2192 A)(A\u2248B : id \u2257 B\u2192A \u2218 A\u2192B) f (\u03bcA : SumProp A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-view A\u2192B \u03bcA) (f \u2218 B\u2192A)\n\u03bc-view-preserve A\u2192B B\u2192A A\u2248B f \u03bcA = sum-ext \u03bcA (\u2261.cong f \u2218 A\u2248B)\n\n\u03bc-iso-preserve : \u2200 {A B} (A\u2194B : A \u2194 B) f (\u03bcA : SumProp A) \u2192 sum \u03bcA f \u2261 sum (\u03bc-iso A\u2194B \u03bcA) (f \u2218 _\u27e8$\u27e9_ (Inverse.from A\u2194B))\n\u03bc-iso-preserve A\u2194B f \u03bcA = \u03bc-view-preserve (_\u27e8$\u27e9_ (Inverse.to A\u2194B)) (_\u27e8$\u27e9_ (Inverse.from A\u2194B))\n (\u2261.sym \u2218 Inverse.left-inverse-of A\u2194B) f \u03bcA\n\nopen import Data.Fin using (Fin; zero; suc)\nopen import Data.Vec.NP as Vec using (Vec; tabulate; _++_) renaming (map to vmap; sum to vsum; foldr to vfoldr; foldr\u2081 to vfoldr\u2081)\n\nvmsum : \u2200 m {n} \u2192 let open Mon m in\n Vec C n \u2192 C\nvmsum m = vfoldr _ _\u2219_ \u03b5\n where open Monoid m\n\nvsgsum : \u2200 sg {n} \u2192 let open Sgrp sg in\n Vec C (suc n) \u2192 C\nvsgsum sg = vfoldr\u2081 _\u2219_\n where open Sgrp sg\n\n-- let's recall that: tabulate f \u2257 vmap f (allFin n)\n\n-- searchMonFin : \u2200 n \u2192 SearchMon (Fin n)\n-- searchMonFin n m f = vmsum m (tabulate f)\n\nsearchFinSuc : \u2200 n \u2192 Search (Fin (suc n))\nsearchFinSuc n _\u2219_ f = vfoldr\u2081 _\u2219_ (tabulate f)\n\n\u03bcMaybe : \u2200 {A} \u2192 SumProp A \u2192 SumProp (Maybe A)\n\u03bcMaybe \u03bcA = \u03bc-iso (FI.sym Maybe\u2194\u22a4\u228e) (\u03bc\u22a4 +\u03bc \u03bcA)\n\n\u03bcMaybe^ : \u2200 {A} n \u2192 SumProp A \u2192 SumProp (Maybe^ n A)\n\u03bcMaybe^ zero \u03bcA = \u03bcA\n\u03bcMaybe^ (suc n) \u03bcA = \u03bcMaybe (\u03bcMaybe^ n \u03bcA)\n\n\u03bcFinSuc : \u2200 n \u2192 SumProp (Fin (suc n))\n\u03bcFinSuc n = searchFinSuc n , ind n\n where ind : \u2200 n \u2192 SearchInd (searchFinSuc n)\n ind zero P P\u2219 Pf = Pf zero\n ind (suc n) P P\u2219 Pf = P\u2219 (Pf zero) (ind n (\u03bb s \u2192 P (\u03bb op f \u2192 s op (f \u2218 suc))) P\u2219 (Pf \u2218 suc))\n\n\u03bcFinSucIso : \u2200 n \u2192 SumProp (Fin (suc n))\n\u03bcFinSucIso n = \u03bc-iso (Maybe^\u22a4\u2194Fin1+ n) (\u03bcMaybe^ n \u03bc\u22a4)\n\n\u03bc^ : \u2200 {A} (\u03bcA : SumProp A) n \u2192 SumProp (A ^ n)\n\u03bc^ \u03bcA zero = \u03bc\u22a4\n\u03bc^ \u03bcA (suc n) = \u03bcA \u00d7\u03bc \u03bc^ \u03bcA n\n\n\u03bcVec : \u2200 {A} (\u03bcA : SumProp A) n \u2192 SumProp (Vec A n)\n\u03bcVec \u03bcA n = \u03bc-iso (^\u2194Vec n) (\u03bc^ \u03bcA n)\n\nsearchVec : \u2200 {A} n \u2192 Search A \u2192 Search (Vec A n)\nsearchVec zero search\u1d2c op f = f []\nsearchVec (suc n) search\u1d2c op f = search\u1d2c op (\u03bb x \u2192 searchVec n search\u1d2c op (f \u2218 _\u2237_ x))\n\nsearchVec-spec : \u2200 {A} (\u03bcA : SumProp A) n \u2192 searchVec n (search \u03bcA) \u2248Search search (\u03bcVec \u03bcA n)\nsearchVec-spec \u03bcA zero op f = \u2261.refl\nsearchVec-spec \u03bcA (suc n) op f = search-ext \u03bcA op (\u03bb x \u2192 searchVec-spec \u03bcA n op (f \u2218 _\u2237_ x))\n\nsearchVec-++ : \u2200 {A} n {m} (\u03bcA : SumProp A) sg\n \u2192 let open Sgrp sg in\n (f : Vec A (n + m) \u2192 C)\n \u2192 search (\u03bcVec \u03bcA (n + m)) _\u2219_ f\n \u2248 search (\u03bcVec \u03bcA n) _\u2219_ (\u03bb xs \u2192\n search (\u03bcVec \u03bcA m) _\u2219_ (\u03bb ys \u2192\n f (xs ++ ys)))\nsearchVec-++ zero \u03bcA sg f = Sgrp.refl sg\nsearchVec-++ (suc n) \u03bcA sg f = search-sg-ext \u03bcA sg (\u03bb x \u2192\n searchVec-++ n \u03bcA sg (f \u2218 _\u2237_ x))\n\nsumVec-swap : \u2200 {A} n {m} (\u03bcA : SumProp A)(f : Vec A (n + m) \u2192 \u2115)\n \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 f (xs ++ ys)))\n \u2261 sum (\u03bcVec \u03bcA m) (\u03bb ys \u2192 sum (\u03bcVec \u03bcA n) (\u03bb xs \u2192 f (xs ++ ys)))\nsumVec-swap n {m} \u03bcA f = sum-swap (\u03bcVec \u03bcA n) (\u03bcVec \u03bcA m) (\u03bb xs ys \u2192 f (xs ++ ys))\n\nswapS : \u2200 {A B} \u2192 SumProp (A \u00d7 B) \u2192 SumProp (B \u00d7 A)\nswapS = \u03bc-iso swap-iso\n\nswapS-preserve : \u2200 {A B} f (\u03bcA\u00d7B : SumProp (A \u00d7 B)) \u2192 sum \u03bcA\u00d7B f \u2261 sum (swapS \u03bcA\u00d7B) (f \u2218 swap)\nswapS-preserve = \u03bc-iso-preserve swap-iso\n\nmodule _ {A : Set}(\u03bcA : SumProp A) where\n\n sA = search \u03bcA\n\n extend : \u2200 {n} \u2192 A \u2192 (Fin n \u2192 A) \u2192 Fin (suc n) \u2192 A\n extend x g zero = x\n extend x g (suc i) = g i\n\n abs : Fin 0 \u2192 A\n abs ()\n\n -- There is one function Fin 0 \u2192 A (called abs) so this should be fine\n -- if not there is a version below that forces the domain to be non-empty\n sFun : \u2200 n \u2192 Search (Fin n \u2192 A)\n sFun zero op f = f abs\n sFun (suc n) op f = sA op (\u03bb x \u2192 sFun n op (f \u2218 extend x))\n\n Ind : \u2200 n \u2192 SearchInd (sFun n)\n Ind zero P P\u2219 Pf = Pf abs\n Ind (suc n) P P\u2219 Pf = \n search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (\u03bb x \u2192 sFun n op (f \u2218 extend x)))) \n P\u2219 \n (\u03bb x \u2192 Ind n (\u03bb sf \u2192 P (\u03bb op f \u2192 sf op (f \u2218 extend x))) \n P\u2219 (Pf \u2218 extend x))\n\n \u03bcFun : \u2200 {n} \u2192 SumProp (Fin n \u2192 A)\n \u03bcFun = sFun _ , Ind _\n\n{-\n -- If we want to force non-empty domain\n\n sFun : \u2200 n \u2192 Search (Fin (suc n) \u2192 A)\n sFun zero op f = sA op (f \u2218 const)\n sFun (suc n) op f = sA op (\u03bb x \u2192 sFun n op (f \u2218 extend x))\n\n Ind : \u2200 n \u2192 SearchInd (sFun n)\n Ind zero P P\u2219 Pf = search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (f \u2218 const))) P\u2219 (Pf \u2218 const)\n Ind (suc n) P P\u2219 Pf = search-ind \u03bcA (\u03bb sa \u2192 P (\u03bb op f \u2192 sa op (\u03bb x \u2192 sFun n op (f \u2218 extend x)))) \n P\u2219 \n (\u03bb x \u2192 Ind n (\u03bb sf \u2192 P (\u03bb op f \u2192 sf op (f \u2218 extend x))) P\u2219 (Pf \u2218 extend x))\n\n-}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"89fd5fa1fbb32e7dfbfd8995589cef232308cac3","subject":"Fixed doc.","message":"Fixed doc.\n\nIgnore-this: 1176ac103b139489f79a9d236b7f707d\n\ndarcs-hash:20100719141536-3bd4e-7a766f5ad4169bb90a7d945092ee748da7180ba2.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Test\/Succeed\/OnlyAxioms\/GeneralHints.agda","new_file":"Test\/Succeed\/OnlyAxioms\/GeneralHints.agda","new_contents":"------------------------------------------------------------------------------\n-- Testing the use of general hints\n------------------------------------------------------------------------------\n\nmodule Test.Succeed.OnlyAxioms.GeneralHints where\n\npostulate\n D : Set\n zero : D\n succ : D \u2192 D\n\ndata N : D \u2192 Set where\n zN : N zero\n sN : (n : D) \u2192 N n \u2192 N (succ n)\n-- The data constructors are general hints. They are translate as axioms.\n{-# ATP hint zN #-}\n{-# ATP hint sN #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Testing the use of general hints\n------------------------------------------------------------------------------\n\nmodule Test.Succeed.OnlyAxioms.GeneralHints where\n\npostulate\n D : Set\n zero : D\n succ : D \u2192 D\n\ndata N : D \u2192 Set where\n zN : N zero\n sN : (n : D) \u2192 N n \u2192 N (succ n)\n\n-- A general hints. They are translate as axioms.\n{-# ATP hint zN #-}\n{-# ATP hint sN #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"e64316878d3bef9df458888d59a4cf01835a76c4","subject":"less asinine names #14","message":"less asinine names #14\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress-checks.agda","new_file":"progress-checks.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import type-assignment-unicity\nopen import lemmas-progress-checks\n\n-- taken together, the theorems in this file argue that for any expression\n-- d, at most one summand of the labeled sum that results from progress may\n-- be true at any time, i.e. that boxed values, indeterminates, cast\n-- errors, and expressions that step are pairwise disjoint. (note that as a\n-- consequence of currying and comutativity of products, this means that\n-- there are six theorems to prove)\nmodule progress-checks where\n -- boxed values and indeterminates are disjoint\n boxedval-not-indet : \u2200{d} \u2192 d boxedval \u2192 d indet \u2192 \u22a5\n boxedval-not-indet (BVVal VConst) ()\n boxedval-not-indet (BVVal VLam) ()\n boxedval-not-indet (BVArrCast x bv) (ICastArr x\u2081 ind) = boxedval-not-indet bv ind\n boxedval-not-indet (BVHoleCast x bv) (ICastGroundHole x\u2081 ind) = boxedval-not-indet bv ind\n boxedval-not-indet (BVHoleCast x bv) (ICastHoleGround x\u2081 ind x\u2082) = boxedval-not-indet bv ind\n\n -- boxed values and errors are disjoint\n boxedval-not-err : \u2200{d} \u2192 d boxedval \u2192 d casterr \u2192 \u22a5\n boxedval-not-err (BVVal ()) (CECastFail x\u2081 x\u2082 x\u2083 x\u2084)\n boxedval-not-err (BVHoleCast x bv) (CECastFail x\u2081 x\u2082 () x\u2084)\n boxedval-not-err (BVArrCast x bv) (CECong FHOuter er) = boxedval-not-err bv (ce-castarr er)\n boxedval-not-err (BVArrCast x bv) (CECong (FHCast x\u2081) er) = boxedval-not-err bv (CECong x\u2081 er)\n boxedval-not-err (BVHoleCast x bv) (CECong FHOuter er) = boxedval-not-err bv (ce-castth er)\n boxedval-not-err (BVHoleCast x bv) (CECong (FHCast x\u2081) er) = boxedval-not-err bv (CECong x\u2081 er)\n boxedval-not-err (BVVal x) (CECong FHOuter er) = boxedval-not-err (BVVal x) er\n boxedval-not-err (BVVal ()) (CECong (FHAp1 x\u2081) er)\n boxedval-not-err (BVVal ()) (CECong (FHAp2 x\u2081 x\u2082) er)\n boxedval-not-err (BVVal ()) (CECong (FHNEHole x\u2081) er)\n boxedval-not-err (BVVal ()) (CECong (FHCast x\u2081) er)\n\n -- boxed values and expressions that step are disjoint\n boxedval-not-step : \u2200{d} \u2192 d boxedval \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n boxedval-not-step (BVVal VConst) (d' , Step FHOuter () x\u2083)\n boxedval-not-step (BVVal VLam) (d' , Step FHOuter () x\u2083)\n boxedval-not-step (BVArrCast x bv) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n boxedval-not-step (BVArrCast x bv) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = boxedval-not-step bv (_ , Step x\u2081 x\u2082 x\u2083)\n boxedval-not-step (BVHoleCast () bv) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n boxedval-not-step (BVHoleCast x bv) (d' , Step FHOuter (ITCastSucceed x\u2081 ()) FHOuter)\n boxedval-not-step (BVHoleCast GHole bv) (_ , Step FHOuter (ITGround x\u2081 x\u2082) FHOuter) = x\u2082 refl\n boxedval-not-step (BVHoleCast x bv) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = boxedval-not-step bv (_ , Step x\u2081 x\u2082 x\u2083)\n\n -- todo: what class of P is this true for?\n -- lem-something : \u2200{ d \u03b5 d'} \u2192 d == \u03b5 \u27e6 d' \u27e7 \u2192 P d' \u2192 P d\n\n mutual\n -- indeterminates and errors are disjoint\n indet-not-err : \u2200{d} \u2192 d indet \u2192 d casterr \u2192 \u22a5\n indet-not-err IEHole (CECong FHOuter err) = indet-not-err IEHole err\n indet-not-err (INEHole x) (CECong FHOuter err) = final-not-err x (ce-nehole err)\n indet-not-err (INEHole x) (CECong (FHNEHole x\u2081) err) = final-not-err x (CECong x\u2081 err)\n indet-not-err (IAp x indet x\u2081) (CECong FHOuter err)\n with ce-ap err\n ... | Inl d1err = indet-not-err indet d1err\n ... | Inr d2err = final-not-err x\u2081 d2err\n indet-not-err (IAp x indet x\u2081) (CECong (FHAp1 x\u2082) err) = indet-not-err indet (CECong x\u2082 err)\n indet-not-err (IAp x indet x\u2081) (CECong (FHAp2 x\u2082 x\u2083) err) = final-not-err x\u2081 (CECong x\u2083 err)\n indet-not-err (ICastArr x indet) (CECong FHOuter err) = indet-not-err indet (ce-castarr err)\n indet-not-err (ICastArr x indet) (CECong (FHCast x\u2081) err) = indet-not-err indet (CECong x\u2081 err)\n indet-not-err (ICastGroundHole x indet) (CECastFail x\u2081 x\u2082 () x\u2084)\n indet-not-err (ICastGroundHole x indet) (CECong FHOuter err) = indet-not-err indet (ce-castth err)\n indet-not-err (ICastGroundHole x indet) (CECong (FHCast x\u2081) err) = indet-not-err indet (CECong x\u2081 err)\n indet-not-err (ICastHoleGround x indet x\u2081) (CECastFail x\u2082 x\u2083 x\u2084 x\u2085) = x _ _ refl\n indet-not-err (ICastHoleGround x indet x\u2081) (CECong FHOuter err) = indet-not-err indet (ce-castht err x)\n indet-not-err (ICastHoleGround x indet x\u2081) (CECong (FHCast x\u2082) err) = indet-not-err indet (CECong x\u2082 err)\n\n -- final expressions are not errors (not one of the 6 cases for progress, just a convenience)\n final-not-err : \u2200{d} \u2192 d final \u2192 d casterr \u2192 \u22a5\n final-not-err (FBoxed x) err = boxedval-not-err x err\n final-not-err (FIndet x) err = indet-not-err x err\n\n -- todo: these are bad names; probably some places below where i inlined\n -- some of these lemmas before i'd come up with them\n lem2 : \u2200{d d'} \u2192 d indet \u2192 d \u2192> d' \u2192 \u22a5\n lem2 IEHole ()\n lem2 (INEHole x) ()\n lem2 (IAp x\u2081 () x\u2082) (ITLam x\u2083)\n lem2 (IAp x (ICastArr x\u2081 ind) x\u2082) (ITApCast x\u2083 x\u2084) = x _ _ _ _ _ refl\n lem2 (ICastArr x ind) (ITCastID (FBoxed x\u2081)) = boxedval-not-indet x\u2081 ind\n lem2 (ICastArr x ind) (ITCastID (FIndet x\u2081)) = x refl\n lem2 (ICastGroundHole () ind) (ITCastID x\u2081)\n lem2 (ICastGroundHole x ind) (ITCastSucceed x\u2081 ())\n lem2 (ICastGroundHole GHole ind) (ITGround x\u2081 x\u2082) = x\u2082 refl\n lem2 (ICastHoleGround x ind ()) (ITCastID x\u2082)\n lem2 (ICastHoleGround x ind x\u2081) (ITCastSucceed x\u2082 x\u2083) = x _ _ refl\n lem2 (ICastHoleGround x ind GHole) (ITExpand x\u2082 x\u2083) = x\u2083 refl\n\n lem3 : \u2200{d d'} \u2192 d boxedval \u2192 d \u2192> d' \u2192 \u22a5\n lem3 (BVVal VConst) ()\n lem3 (BVVal VLam) ()\n lem3 (BVArrCast x bv) (ITCastID x\u2081) = x refl\n lem3 (BVHoleCast () bv) (ITCastID x\u2081)\n lem3 (BVHoleCast () bv) (ITCastSucceed x\u2081 x\u2082)\n lem3 (BVHoleCast GHole bv) (ITGround x\u2081 y ) = y refl\n\n lem1 : \u2200{d d'} \u2192 d final \u2192 d \u2192> d' \u2192 \u22a5\n lem1 (FBoxed x) = lem3 x\n lem1 (FIndet x) = lem2 x\n\n lem4 : \u2200{d \u03b5 x} \u2192 d final \u2192 d == \u03b5 \u27e6 x \u27e7 \u2192 x final\n lem4 x FHOuter = x\n lem4 (FBoxed (BVVal ())) (FHAp1 eps)\n lem4 (FBoxed (BVVal ())) (FHAp2 x\u2082 eps)\n lem4 (FBoxed (BVVal ())) (FHNEHole eps)\n lem4 (FBoxed (BVVal ())) (FHCast eps)\n lem4 (FBoxed (BVArrCast x\u2081 x\u2082)) (FHCast eps) = lem4 (FBoxed x\u2082) eps\n lem4 (FBoxed (BVHoleCast x\u2081 x\u2082)) (FHCast eps) = lem4 (FBoxed x\u2082) eps\n lem4 (FIndet (IAp x\u2081 x\u2082 x\u2083)) (FHAp1 eps) = lem4 (FIndet x\u2082) eps\n lem4 (FIndet (IAp x\u2081 x\u2082 x\u2083)) (FHAp2 x\u2084 eps) = lem4 x\u2083 eps\n lem4 (FIndet (INEHole x\u2081)) (FHNEHole eps) = lem4 x\u2081 eps\n lem4 (FIndet (ICastArr x\u2081 x\u2082)) (FHCast eps) = lem4 (FIndet x\u2082) eps\n lem4 (FIndet (ICastGroundHole x\u2081 x\u2082)) (FHCast eps) = lem4 (FIndet x\u2082) eps\n lem4 (FIndet (ICastHoleGround x\u2081 x\u2082 x\u2083)) (FHCast eps) = lem4 (FIndet x\u2082) eps\n\n lem5 : \u2200{d d' d'' \u03b5} \u2192 d final \u2192 d == \u03b5 \u27e6 d' \u27e7 \u2192 d' \u2192> d'' \u2192 \u22a5\n lem5 f sub step = lem1 (lem4 f sub) step\n\n -- indeterminates and expressions that step are disjoint\n mutual\n indet-not-step : \u2200{d} \u2192 d indet \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n indet-not-step IEHole (d' , Step FHOuter () FHOuter)\n indet-not-step (INEHole x) (d' , Step FHOuter () FHOuter)\n indet-not-step (INEHole x) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = lem5 x x\u2081 x\u2082\n indet-not-step (IAp x\u2081 () x\u2082) (_ , Step FHOuter (ITLam x\u2083) FHOuter)\n indet-not-step (IAp x (ICastArr x\u2081 ind) x\u2082) (_ , Step FHOuter (ITApCast x\u2083 x\u2084) FHOuter) = x _ _ _ _ _ refl\n indet-not-step (IAp x ind _) (_ , Step (FHAp1 x\u2082) x\u2083 (FHAp1 x\u2084)) = indet-not-step ind (_ , Step x\u2082 x\u2083 x\u2084)\n indet-not-step (IAp x ind f) (_ , Step (FHAp2 x\u2082 x\u2083) x\u2084 (FHAp2 x\u2085 x\u2086)) = final-not-step f (_ , Step x\u2083 x\u2084 x\u2086)\n indet-not-step (ICastArr x ind) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n indet-not-step (ICastArr x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n indet-not-step (ICastGroundHole () ind) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n indet-not-step (ICastGroundHole x ind) (d' , Step FHOuter (ITCastSucceed x\u2081 ()) FHOuter)\n indet-not-step (ICastGroundHole GHole ind) (_ , Step FHOuter (ITGround _ y) FHOuter) = y refl\n indet-not-step (ICastGroundHole x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n indet-not-step (ICastHoleGround x ind ()) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n indet-not-step (ICastHoleGround x ind g) (d' , Step FHOuter (ITCastSucceed x\u2081 x\u2082) FHOuter) = x _ _ refl\n indet-not-step (ICastHoleGround x ind GHole) (_ , Step FHOuter (ITExpand x\u2081 x\u2082) FHOuter) = x\u2082 refl\n indet-not-step (ICastHoleGround x ind g) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = indet-not-step ind (_ , Step x\u2081 x\u2082 x\u2083)\n\n final-not-step : \u2200{d} \u2192 d final \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 \u22a5\n final-not-step (FBoxed x) stp = boxedval-not-step x stp\n final-not-step (FIndet x) stp = indet-not-step x stp\n\n -- errors and expressions that step are disjoint\n err-not-step : \u2200{d} \u2192 d casterr \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n -- cast fail caserr-not-step\n err-not-step (CECastFail x x\u2081 () x\u2083) (_ , Step FHOuter (ITCastID x\u2084) FHOuter)\n err-not-step (CECastFail x x\u2081 x\u2082 x\u2083) (_ , Step FHOuter (ITCastSucceed x\u2084 x\u2085) FHOuter) = x\u2083 refl\n err-not-step (CECastFail x x\u2081 GHole x\u2083) (_ , Step FHOuter (ITExpand x\u2084 x\u2085) FHOuter) = x\u2085 refl\n err-not-step (CECastFail x x\u2081 x\u2082 x\u2083) (_ , Step (FHCast x\u2084) x\u2085 (FHCast x\u2086)) = {!!}\n\n -- congruence caserr-not-step\n err-not-step (CECong FHOuter ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = err-not-step ce (\u03c01 , Step FHOuter x\u2082 FHOuter)\n err-not-step (CECong (FHAp1 FHOuter) (CECong FHOuter ce)) (_ , Step FHOuter (ITLam x\u2082) FHOuter) = boxedval-not-err (BVVal VLam) ce\n err-not-step (CECong (FHAp1 x) ce) (_ , Step FHOuter (ITApCast x\u2081 x\u2082) FHOuter) = {!!} -- fe x\u2081 (CECong {!ce-out-cast ce!} ce)\n err-not-step (CECong (FHAp2 x x\u2081) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!ce-out-cast ce x\u2081!}\n err-not-step (CECong (FHNEHole x) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!!}\n err-not-step (CECong (FHCast x) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!!}\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHAp1 x\u2081) x\u2082 (FHAp1 x\u2083))\n with ce-ap ce\n ... | Inl d1err = err-not-step d1err (_ , Step x\u2081 x\u2082 x\u2083)\n ... | Inr d2err = {!Step x\u2081 x\u2082 x\u2083!} -- this is a counter example: d2 is a casterror but d1 isn't yet a value so the whole thing steps\n err-not-step (CECong (FHAp1 x) ce) (_ , Step (FHAp1 x\u2081) x\u2082 (FHAp1 x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n err-not-step (CECong (FHAp2 x x\u2081) ce) (_ , Step (FHAp1 x\u2082) x\u2083 (FHAp1 x\u2084)) = final-not-step x (_ , Step x\u2082 x\u2083 x\u2084)\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHAp2 x\u2081 x\u2082) x\u2083 (FHAp2 x\u2084 x\u2085))\n with ce-ap ce\n ... | Inl d1err = final-not-err x\u2084 d1err\n ... | Inr d2err = err-not-step d2err (_ , Step x\u2082 x\u2083 x\u2085)\n err-not-step (CECong (FHAp1 x) ce) (_ , Step (FHAp2 x\u2081 x\u2082) x\u2083 (FHAp2 x\u2084 x\u2085)) = {!!}\n err-not-step (CECong (FHAp2 x x\u2081) ce) (_ , Step (FHAp2 x\u2082 x\u2083) x\u2084 (FHAp2 x\u2085 x\u2086)) = {!!}\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = err-not-step (ce-nehole ce) (_ , Step x\u2081 x\u2082 x\u2083)\n err-not-step (CECong (FHNEHole x) ce) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n\n err-not-step (CECong FHOuter ce) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = {!!} -- err-not-step {!!} (_ , Step x\u2081 x\u2082 x\u2083) -- this might not work\n err-not-step (CECong (FHCast x) ce) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = err-not-step (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import type-assignment-unicity\nopen import lemmas-progress-checks\n\n-- taken together, the theorems in this file argue that for any expression\n-- d, at most one summand of the labeled sum that results from progress may\n-- be true at any time, i.e. that boxed values, indeterminates, cast\n-- errors, and expressions that step are pairwise disjoint. (note that as a\n-- consequence of currying and comutativity of products, this means that\n-- there are six theorems to prove)\nmodule progress-checks where\n -- boxed values and indeterminates are disjoint\n vi : \u2200{d} \u2192 d boxedval \u2192 d indet \u2192 \u22a5\n vi (BVVal VConst) ()\n vi (BVVal VLam) ()\n vi (BVArrCast x bv) (ICastArr x\u2081 ind) = vi bv ind\n vi (BVHoleCast x bv) (ICastGroundHole x\u2081 ind) = vi bv ind\n vi (BVHoleCast x bv) (ICastHoleGround x\u2081 ind x\u2082) = vi bv ind\n\n -- boxed values and errors are disjoint\n ve : \u2200{d} \u2192 d boxedval \u2192 d casterr \u2192 \u22a5\n ve (BVVal ()) (CECastFail x\u2081 x\u2082 x\u2083 x\u2084)\n ve (BVHoleCast x bv) (CECastFail x\u2081 x\u2082 () x\u2084)\n ve (BVArrCast x bv) (CECong FHOuter er) = ve bv (ce-castarr er)\n ve (BVArrCast x bv) (CECong (FHCast x\u2081) er) = ve bv (CECong x\u2081 er)\n ve (BVHoleCast x bv) (CECong FHOuter er) = ve bv (ce-castth er)\n ve (BVHoleCast x bv) (CECong (FHCast x\u2081) er) = ve bv (CECong x\u2081 er)\n ve (BVVal x) (CECong FHOuter er) = ve (BVVal x) er\n ve (BVVal ()) (CECong (FHAp1 x\u2081) er)\n ve (BVVal ()) (CECong (FHAp2 x\u2081 x\u2082) er)\n ve (BVVal ()) (CECong (FHNEHole x\u2081) er)\n ve (BVVal ()) (CECong (FHCast x\u2081) er)\n\n -- boxed values and expressions that step are disjoint\n vs : \u2200{d} \u2192 d boxedval \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n vs (BVVal VConst) (d' , Step FHOuter () x\u2083)\n vs (BVVal VLam) (d' , Step FHOuter () x\u2083)\n vs (BVArrCast x bv) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n vs (BVArrCast x bv) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = vs bv (_ , Step x\u2081 x\u2082 x\u2083)\n vs (BVHoleCast () bv) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n vs (BVHoleCast x bv) (d' , Step FHOuter (ITCastSucceed x\u2081 ()) FHOuter)\n vs (BVHoleCast GHole bv) (_ , Step FHOuter (ITGround x\u2081 x\u2082) FHOuter) = x\u2082 refl\n vs (BVHoleCast x bv) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = vs bv (_ , Step x\u2081 x\u2082 x\u2083)\n\n -- todo: what class of P is this true for?\n -- lem-something : \u2200{ d \u03b5 d'} \u2192 d == \u03b5 \u27e6 d' \u27e7 \u2192 P d' \u2192 P d\n\n mutual\n -- todo: there's something going on with these lemmas here that's\n -- repetative and i don't quite understand how to make it more compact\n\n -- indeterminates and errors are disjoint\n ie : \u2200{d} \u2192 d indet \u2192 d casterr \u2192 \u22a5\n ie IEHole (CECong FHOuter err) = ie IEHole err\n ie (INEHole x) (CECong FHOuter err) = fe x (ce-nehole err)\n ie (INEHole x) (CECong (FHNEHole x\u2081) err) = fe x (CECong x\u2081 err)\n ie (IAp x indet x\u2081) (CECong FHOuter err)\n with ce-ap err\n ... | Inl d1err = ie indet d1err\n ... | Inr d2err = fe x\u2081 d2err\n ie (IAp x indet x\u2081) (CECong (FHAp1 x\u2082) err) = ie indet (CECong x\u2082 err)\n ie (IAp x indet x\u2081) (CECong (FHAp2 x\u2082 x\u2083) err) = fe x\u2081 (CECong x\u2083 err)\n ie (ICastArr x indet) (CECong FHOuter err) = ie indet (ce-castarr err)\n ie (ICastArr x indet) (CECong (FHCast x\u2081) err) = ie indet (CECong x\u2081 err)\n ie (ICastGroundHole x indet) (CECastFail x\u2081 x\u2082 () x\u2084)\n ie (ICastGroundHole x indet) (CECong FHOuter err) = ie indet (ce-castth err)\n ie (ICastGroundHole x indet) (CECong (FHCast x\u2081) err) = ie indet (CECong x\u2081 err)\n ie (ICastHoleGround x indet x\u2081) (CECastFail x\u2082 x\u2083 x\u2084 x\u2085) = x _ _ refl\n ie (ICastHoleGround x indet x\u2081) (CECong FHOuter err) = ie indet (ce-castht err x)\n ie (ICastHoleGround x indet x\u2081) (CECong (FHCast x\u2082) err) = ie indet (CECong x\u2082 err)\n\n -- final expressions are not errors (not one of the 6 cases for progress, just a convenience)\n fe : \u2200{d} \u2192 d final \u2192 d casterr \u2192 \u22a5\n fe (FBoxed x) err = ve x err\n fe (FIndet x) err = ie x err\n\n -- todo: these are bad names; probably some places below where i inlined\n -- some of these lemmas before i'd come up with them\n lem2 : \u2200{d d'} \u2192 d indet \u2192 d \u2192> d' \u2192 \u22a5\n lem2 IEHole ()\n lem2 (INEHole x) ()\n lem2 (IAp x\u2081 () x\u2082) (ITLam x\u2083)\n lem2 (IAp x (ICastArr x\u2081 ind) x\u2082) (ITApCast x\u2083 x\u2084) = x _ _ _ _ _ refl\n lem2 (ICastArr x ind) (ITCastID (FBoxed x\u2081)) = vi x\u2081 ind\n lem2 (ICastArr x ind) (ITCastID (FIndet x\u2081)) = x refl\n lem2 (ICastGroundHole () ind) (ITCastID x\u2081)\n lem2 (ICastGroundHole x ind) (ITCastSucceed x\u2081 ())\n lem2 (ICastGroundHole GHole ind) (ITGround x\u2081 x\u2082) = x\u2082 refl\n lem2 (ICastHoleGround x ind ()) (ITCastID x\u2082)\n lem2 (ICastHoleGround x ind x\u2081) (ITCastSucceed x\u2082 x\u2083) = x _ _ refl\n lem2 (ICastHoleGround x ind GHole) (ITExpand x\u2082 x\u2083) = x\u2083 refl\n\n lem3 : \u2200{d d'} \u2192 d boxedval \u2192 d \u2192> d' \u2192 \u22a5\n lem3 (BVVal VConst) ()\n lem3 (BVVal VLam) ()\n lem3 (BVArrCast x bv) (ITCastID x\u2081) = x refl\n lem3 (BVHoleCast () bv) (ITCastID x\u2081)\n lem3 (BVHoleCast () bv) (ITCastSucceed x\u2081 x\u2082)\n lem3 (BVHoleCast GHole bv) (ITGround x\u2081 y ) = y refl\n\n lem1 : \u2200{d d'} \u2192 d final \u2192 d \u2192> d' \u2192 \u22a5\n lem1 (FBoxed x) = lem3 x\n lem1 (FIndet x) = lem2 x\n\n lem4 : \u2200{d \u03b5 x} \u2192 d final \u2192 d == \u03b5 \u27e6 x \u27e7 \u2192 x final\n lem4 x FHOuter = x\n lem4 (FBoxed (BVVal ())) (FHAp1 eps)\n lem4 (FBoxed (BVVal ())) (FHAp2 x\u2082 eps)\n lem4 (FBoxed (BVVal ())) (FHNEHole eps)\n lem4 (FBoxed (BVVal ())) (FHCast eps)\n lem4 (FBoxed (BVArrCast x\u2081 x\u2082)) (FHCast eps) = lem4 (FBoxed x\u2082) eps\n lem4 (FBoxed (BVHoleCast x\u2081 x\u2082)) (FHCast eps) = lem4 (FBoxed x\u2082) eps\n lem4 (FIndet (IAp x\u2081 x\u2082 x\u2083)) (FHAp1 eps) = lem4 (FIndet x\u2082) eps\n lem4 (FIndet (IAp x\u2081 x\u2082 x\u2083)) (FHAp2 x\u2084 eps) = lem4 x\u2083 eps\n lem4 (FIndet (INEHole x\u2081)) (FHNEHole eps) = lem4 x\u2081 eps\n lem4 (FIndet (ICastArr x\u2081 x\u2082)) (FHCast eps) = lem4 (FIndet x\u2082) eps\n lem4 (FIndet (ICastGroundHole x\u2081 x\u2082)) (FHCast eps) = lem4 (FIndet x\u2082) eps\n lem4 (FIndet (ICastHoleGround x\u2081 x\u2082 x\u2083)) (FHCast eps) = lem4 (FIndet x\u2082) eps\n\n lem5 : \u2200{d d' d'' \u03b5} \u2192 d final \u2192 d == \u03b5 \u27e6 d' \u27e7 \u2192 d' \u2192> d'' \u2192 \u22a5\n lem5 f sub step = lem1 (lem4 f sub) step\n\n -- indeterminates and expressions that step are disjoint\n mutual\n is : \u2200{d} \u2192 d indet \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n is IEHole (d' , Step FHOuter () FHOuter)\n is (INEHole x) (d' , Step FHOuter () FHOuter)\n is (INEHole x) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = lem5 x x\u2081 x\u2082\n is (IAp x\u2081 () x\u2082) (_ , Step FHOuter (ITLam x\u2083) FHOuter)\n is (IAp x (ICastArr x\u2081 ind) x\u2082) (_ , Step FHOuter (ITApCast x\u2083 x\u2084) FHOuter) = x _ _ _ _ _ refl\n is (IAp x ind _) (_ , Step (FHAp1 x\u2082) x\u2083 (FHAp1 x\u2084)) = is ind (_ , Step x\u2082 x\u2083 x\u2084)\n is (IAp x ind f) (_ , Step (FHAp2 x\u2082 x\u2083) x\u2084 (FHAp2 x\u2085 x\u2086)) = fs f (_ , Step x\u2083 x\u2084 x\u2086)\n is (ICastArr x ind) (d0' , Step FHOuter (ITCastID x\u2081) FHOuter) = x refl\n is (ICastArr x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = is ind (_ , Step x\u2081 x\u2082 x\u2083)\n is (ICastGroundHole () ind) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n is (ICastGroundHole x ind) (d' , Step FHOuter (ITCastSucceed x\u2081 ()) FHOuter)\n is (ICastGroundHole GHole ind) (_ , Step FHOuter (ITGround _ y) FHOuter) = y refl\n is (ICastGroundHole x ind) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = is ind (_ , Step x\u2081 x\u2082 x\u2083)\n is (ICastHoleGround x ind ()) (d' , Step FHOuter (ITCastID x\u2081) FHOuter)\n is (ICastHoleGround x ind g) (d' , Step FHOuter (ITCastSucceed x\u2081 x\u2082) FHOuter) = x _ _ refl\n is (ICastHoleGround x ind GHole) (_ , Step FHOuter (ITExpand x\u2081 x\u2082) FHOuter) = x\u2082 refl\n is (ICastHoleGround x ind g) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = is ind (_ , Step x\u2081 x\u2082 x\u2083)\n\n fs : \u2200{d} \u2192 d final \u2192 \u03a3[ d' \u2208 dhexp ] (d \u21a6 d') \u2192 \u22a5\n fs (FBoxed x) stp = vs x stp\n fs (FIndet x) stp = is x stp\n\n -- errors and expressions that step are disjoint\n es : \u2200{d} \u2192 d casterr \u2192 (\u03a3[ d' \u2208 dhexp ] (d \u21a6 d')) \u2192 \u22a5\n -- cast fail cases\n es (CECastFail x x\u2081 () x\u2083) (_ , Step FHOuter (ITCastID x\u2084) FHOuter)\n es (CECastFail x x\u2081 x\u2082 x\u2083) (_ , Step FHOuter (ITCastSucceed x\u2084 x\u2085) FHOuter) = x\u2083 refl\n es (CECastFail x x\u2081 GHole x\u2083) (_ , Step FHOuter (ITExpand x\u2084 x\u2085) FHOuter) = x\u2085 refl\n es (CECastFail x x\u2081 x\u2082 x\u2083) (_ , Step (FHCast x\u2084) x\u2085 (FHCast x\u2086)) = {!!}\n\n -- congruence cases\n es (CECong FHOuter ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = es ce (\u03c01 , Step FHOuter x\u2082 FHOuter)\n es (CECong (FHAp1 FHOuter) (CECong FHOuter ce)) (_ , Step FHOuter (ITLam x\u2082) FHOuter) = ve (BVVal VLam) ce\n es (CECong (FHAp1 x) ce) (_ , Step FHOuter (ITApCast x\u2081 x\u2082) FHOuter) = {!!} -- fe x\u2081 (CECong {!ce-out-cast ce!} ce)\n es (CECong (FHAp2 x x\u2081) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!ce-out-cast ce x\u2081!}\n es (CECong (FHNEHole x) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!!}\n es (CECong (FHCast x) ce) (\u03c01 , Step FHOuter x\u2082 FHOuter) = {!!}\n\n es (CECong FHOuter ce) (_ , Step (FHAp1 x\u2081) x\u2082 (FHAp1 x\u2083))\n with ce-ap ce\n ... | Inl d1err = es d1err (_ , Step x\u2081 x\u2082 x\u2083)\n ... | Inr d2err = {!Step x\u2081 x\u2082 x\u2083!} -- this is a counter example: d2 is a casterror but d1 isn't yet a value so the whole thing steps\n es (CECong (FHAp1 x) ce) (_ , Step (FHAp1 x\u2081) x\u2082 (FHAp1 x\u2083)) = es (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n es (CECong (FHAp2 x x\u2081) ce) (_ , Step (FHAp1 x\u2082) x\u2083 (FHAp1 x\u2084)) = fs x (_ , Step x\u2082 x\u2083 x\u2084)\n\n es (CECong FHOuter ce) (_ , Step (FHAp2 x\u2081 x\u2082) x\u2083 (FHAp2 x\u2084 x\u2085))\n with ce-ap ce\n ... | Inl d1err = fe x\u2084 d1err\n ... | Inr d2err = es d2err (_ , Step x\u2082 x\u2083 x\u2085)\n es (CECong (FHAp1 x) ce) (_ , Step (FHAp2 x\u2081 x\u2082) x\u2083 (FHAp2 x\u2084 x\u2085)) = {!!}\n es (CECong (FHAp2 x x\u2081) ce) (_ , Step (FHAp2 x\u2082 x\u2083) x\u2084 (FHAp2 x\u2085 x\u2086)) = {!!}\n\n es (CECong FHOuter ce) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = es (ce-nehole ce) (_ , Step x\u2081 x\u2082 x\u2083)\n es (CECong (FHNEHole x) ce) (_ , Step (FHNEHole x\u2081) x\u2082 (FHNEHole x\u2083)) = es (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n\n es (CECong FHOuter ce) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = {!!} -- es {!!} (_ , Step x\u2081 x\u2082 x\u2083) -- this might not work\n es (CECong (FHCast x) ce) (_ , Step (FHCast x\u2081) x\u2082 (FHCast x\u2083)) = es (ce-out-cast ce x) (_ , Step x\u2081 x\u2082 x\u2083)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c496c41d4f713230d125f5f19f7223eea9aaa2de","subject":"More one-time-pad related lemmas: \u27ea m \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u2047","message":"More one-time-pad related lemmas: \u27ea m \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u2047\n","repos":"crypto-agda\/crypto-agda","old_file":"single-bit-one-time-pad.agda","new_file":"single-bit-one-time-pad.agda","new_contents":"module single-bit-one-time-pad where\n\nopen import Function\nopen import Data.Bool.NP\nopen import Data.Product renaming (map to <_\u00d7_>)\nopen import Data.Nat.NP\nimport Data.Vec.NP as V\nopen V using (Vec; take; drop; drop\u2032; take\u2032; _++_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2257_; module \u2261-Reasoning)\n\nopen import Data.Bits\nopen import flipbased-implem\nopen import program-distance\n\nK = Bit\nM = Bit\nC = Bit\n\nrecord Adv (S\u2080 S\u2081 S\u2082 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n step\u2081 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2082 : C \u00d7 S\u2081 \u2192 S\u2082\n step\u2083 : S\u2082 \u2192 Bit\n\nrecord Adv\u2081 (S\u2080 S\u2081 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n -- step\u2081 s\u2080 = id , s\u2080\n step\u2082 : C \u00d7 S\u2080 \u2192 S\u2081\n step\u2083 : S\u2081 \u2192 Bit\n\nAdv\u2082 : \u2200 ca \u2192 Set\nAdv\u2082 ca = \u21ba ca (C \u2192 Bit)\n\nmodule Run\u2141\u2032 {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open Adv A\n E : M \u2192 \u21ba 1 C\n E m = toss >>= \u03bb k \u2192 return\u21ba (m xor k)\n run\u2141\u2032 : \u2141? (ca + 1)\n run\u2141\u2032 b = step\u2080 >>= \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n E (m b) >>= \u03bb c \u2192\n return\u21ba (step\u2083 (step\u2082 (c , s\u2081)))}\n\nmodule Run\u2141 {S\u2080 S\u2081 S\u2082 ca} (E : K \u2192 M \u2192 C) (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Adv A\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n let c = E k (m b) in\n step\u2083 (step\u2082 (c , s\u2081))}\n\n run\u2141 : EXP (1 + ca)\n run\u2141 = toss >>= kont\u2080\n\n {- looks wrong\nmodule Run\u2141-Properties {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b k : Bit) where\n open Run\u2141 A\n kont\u2080-not : kont\u2080 b k \u2261 kont\u2080 (not b) (not k)\n kont\u2080-not rewrite xor-not-not b k = {!refl!}\n -}\n\nmodule SymAdv (homPrgDist : HomPrgDist) {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n step\u2081\u2032 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2081\u2032 s\u2080 = case step\u2081 s\u2080 of \u03bb { (m , s\u2081) \u2192 (m \u2218 not , s\u2081) }\n symA : Adv S\u2080 S\u2081 S\u2082 ca\n symA = mk step\u2080 step\u2081\u2032 step\u2082 (not \u2218 step\u2083)\n\n symA\u2032 : Adv S\u2080 S\u2081 S\u2082 ca\n symA\u2032 = mk step\u2080 step\u2081\u2032 step\u2082 step\u2083\n\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n open Run\u2141 _xor_ symA renaming (run\u2141 to runSymA)\n open Run\u2141 _xor_ symA\u2032 renaming (run\u2141 to runSymA\u2032)\n {-\n helper : \u2200 {n} (g\u2080 g\u2081 : EXP n) \u2192 g\u2080 ]-[ g\u2081 \u2192 not\u21ba g\u2080 ]-[ not\u21ba g\u2081\n helper = {!!}\n lem : runA \u21d3 runSymA\n lem A-breaks-E = {!helper (uunSymA\u2032 0b) (runSymA\u2032 1)!}\n where pf : breaks runSymA\n pf = {!!}\n -}\n\nmodule Run\u2141\u2082 {ca} (A : Adv\u2082 ca) (b : Bit) where\n E : Bit \u2192 M \u2192 C\n E k m = m xor k\n\n m : Bit \u2192 Bit\n m = id\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n\n {-\n kont\u2080\u2032 : \u2141? _\n kont\u2080\u2032 k =\n conv-Adv A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n -}\n\n run\u2141\u2082 : EXP (1 + ca)\n run\u2141\u2082 = toss >>= kont\u2080\n\nmodule Run\u2141\u2082-Properties {ca} (A : Adv\u2082 ca) where\n open Run\u2141\u2082 A renaming (run\u2141\u2082 to runA)\n kont\u2080-not : \u2200 b k \u2192 kont\u2080 b k \u2261 kont\u2080 (not b) (not k)\n kont\u2080-not b k rewrite xor-not-not b k = \u2261.refl\n\n open \u2261-Reasoning\n\n lem\u2082 : \u2200 b \u2192 count\u21ba (runA b) \u2261 count\u21ba (runA (not b))\n lem\u2082 b = count\u21ba (runA b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 \u2261.cong\u2082 (_+_ on count\u21ba) (kont\u2080-not b 0b) (kont\u2080-not b 1b) \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (runA (not b)) \u220e\n\n lem\u2083 : Safe\u2141? runA\n lem\u2083 = lem\u2082 0b\n\n -- A specialized version of lem\u2082 (\u2248lem\u2083)\n lem\u2084 : Safe\u2141? (Run\u2141\u2082.run\u2141\u2082 A)\n lem\u2084 = count\u21ba (runA 0b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (kont\u2080 0b 0b) + count\u21ba (kont\u2080 0b 1b)\n \u2261\u27e8 \u2261.cong\u2082 (_+_ on count\u21ba) (kont\u2080-not 0b 0b) (kont\u2080-not 0b 1b) \u27e9\n count\u21ba (kont\u2080 1b 1b) + count\u21ba (kont\u2080 1b 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 1b 1b)) _ \u27e9\n count\u21ba (kont\u2080 1b 0b) + count\u21ba (kont\u2080 1b 1b)\n \u2261\u27e8 \u2261.refl \u27e9\n count\u21ba (runA 1b) \u220e\n\nconv-Adv : \u2200 {ca S\u2080 S\u2081 S\u2082} \u2192 Adv S\u2080 S\u2081 S\u2082 ca \u2192 Adv\u2082 ca\nconv-Adv A = step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n \u03bb c \u2192 m (step\u2083 (step\u2082 (c , s\u2081)))}\n where open Adv A\n\nmodule Conv-Adv-Props (homPrgDist : HomPrgDist) {ca S\u2080 S\u2081 S\u2082} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n -- open Run\u2141-Properties A\n\n A\u2032 : Adv\u2082 ca\n A\u2032 = conv-Adv A\n open Run\u2141\u2082 A\u2032 using () renaming (kont\u2080 to kont\u2080\u2032; run\u2141\u2082 to runA\u2032)\n\n kont\u2080\u2032\u2032 : \u2200 b k \u2192 EXP ca\n kont\u2080\u2032\u2032 b k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 ((m b) xor k , s\u2081)))}\n\n kont\u2080\u2032\u2032\u2032 : \u2200 b\u2032 \u2192 EXP ca\n kont\u2080\u2032\u2032\u2032 b\u2032 =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 (b\u2032 , s\u2081)))}\n\n {-\n kont\u2032\u2032-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032\u2032 b k)\n kont\u2032\u2032-lem true true = {!!}\n kont\u2032\u2032-lem false true = {!!}\n kont\u2032\u2032-lem true false = {!!}\n kont\u2032\u2032-lem false false = {!!}\n\n kont-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032 b k)\n kont-lem b true = {!!}\n kont-lem b false = {!!}\n\n conv-Adv-lem : runA \u2248\u2141? runA\u2032\n conv-Adv-lem b = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 _+_ (kont-lem b 0b) (kont-lem b 1b) \u27e9\n count\u21ba (kont\u2080\u2032 b 0b) + count\u21ba (kont\u2080\u2032 b 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA\u2032 b) \u220e\n\n conv-Adv-sound : runA \u21d3 runA\u2032\n conv-Adv-sound = ]-[-cong-\u2248\u21ba (conv-Adv-lem 0b) (conv-Adv-lem 1b)\n -}\n-- Cute fact: this is true by computation!\ncount\u21ba-toss->>= : \u2200 {c} (f : \u2141? c) \u2192 count\u21ba (toss >>= f) \u2261 count\u21ba (f 0b) + count\u21ba (f 1b)\ncount\u21ba-toss->>= f = \u2261.refl\n\n{-\nmodule Run\u2141-Properties' {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n lem : count\u21ba (runA b) \u2261 count\u21ba (runA (not b))\n lem = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 (_+_ on count\u21ba) {x = kont\u2080 b 0b} {kont\u2080 (not b) 1b} {kont\u2080 b 1b} {kont\u2080 (not b) 0b} {!!} {!!} \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA (not b)) \u220e\n-}\nopen import program-distance\nopen import Relation.Nullary\n\n\u2047 : \u2200 {n} \u2192 \u21ba n (Bits n)\n\u2047 = random\n\n\nlem'' : \u2200 {k} (f : Bits k \u2192 Bit) \u2192 #\u27e8 f \u2218 tail \u27e9 \u2261 2* #\u27e8 f \u27e9\nlem'' f = \u2261.refl\n\nlem' : \u2200 {k} (f g : Bits k \u2192 Bit) \u2192 #\u27e8 f \u2218 tail \u27e9 \u2261 #\u27e8 g \u2218 tail \u27e9 \u2192 #\u27e8 f \u27e9 \u2261 #\u27e8 g \u27e9\nlem' f g pf = 2*-inj (\u2261.trans (lem'' f) (\u2261.trans pf (\u2261.sym (lem'' g))))\n\ndrop-tail : \u2200 k {n a} {A : Set a} \u2192 drop (suc k) {n} \u2257 drop k \u2218 tail {A = A}\ndrop-tail k (x \u2237 xs) = V.drop-\u2237 k x xs\n\nlemdrop\u2032 : \u2200 {k n} (f : Bits n \u2192 Bit) \u2192 #\u27e8 f \u2218 drop\u2032 k \u27e9 \u2261 \u27e82^ k * #\u27e8 f \u27e9 \u27e9\nlemdrop\u2032 {zero} f = \u2261.refl\nlemdrop\u2032 {suc k} f = #\u27e8 f \u2218 drop\u2032 k \u2218 tail \u27e9\n \u2261\u27e8 lem'' (f \u2218 drop\u2032 k) \u27e9\n 2* #\u27e8 f \u2218 drop\u2032 k \u27e9\n \u2261\u27e8 \u2261.cong 2*_ (lemdrop\u2032 {k} f) \u27e9\n 2* \u27e82^ k * #\u27e8 f \u27e9 \u27e9 \u220e\n where open \u2261-Reasoning\n\nvswap : \u2200 m {n} {a} {A : Set a} \u2192 Vec A (m + n) \u2192 Vec A (n + m)\nvswap m xs = drop\u2032 m xs ++ take\u2032 m xs\n\n\n\n\n\n\n\u2248\u1d2c\u2032-toss : \u2200 b \u2192 \u27ea b \u27eb\u1d30 \u27e8xor\u27e9 toss \u2248\u1d2c\u2032 toss\n\u2248\u1d2c\u2032-toss true Adv = \u2115\u00b0.+-comm (count\u21ba (Adv true)) _\n\u2248\u1d2c\u2032-toss false Adv = \u2261.refl\n\n\u2248\u1d2c-toss : \u2200 b \u2192 \u27ea b \u27eb\u1d30 \u27e8xor\u27e9 toss \u2248\u1d2c toss\n\u2248\u1d2c-toss b Adv = \u2248\u1d2c\u2032-toss b (return\u1d30 \u2218 Adv)\n\n-- should be equivalent to #-comm if \u27ea m \u27eb\u1d30 \u27e8\u2295\u27e9 x were convertible to \u27ea _\u2295_ m \u00b7 x \u27eb\n\u2248\u1d2c-\u2047 : \u2200 {k} (m : Bits k) \u2192 \u27ea m \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u2047\n\u2248\u1d2c-\u2047 {zero} _ _ = \u2261.refl\n\u2248\u1d2c-\u2047 {suc k} (h \u2237 m) Adv\n rewrite \u2248\u1d2c-\u2047 m (Adv \u2218 _\u2237_ (h xor 0b))\n | \u2248\u1d2c-\u2047 m (Adv \u2218 _\u2237_ (h xor 1b))\n = \u2248\u1d2c\u2032-toss h (\u03bb x \u2192 \u27ea Adv \u2218 _\u2237_ x \u00b7 \u2047 \u27eb)\n\n\u2248\u1d2c-\u2047\u2082 : \u2200 {k} (m\u2080 m\u2081 : Bits k) \u2192 \u27ea m\u2080 \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea m\u2081 \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2082 {k} m\u2080 m\u2081 = \u2248\u1d2c.trans {k} (\u2248\u1d2c-\u2047 m\u2080) (\u2248\u1d2c.sym {k} (\u2248\u1d2c-\u2047 m\u2081))\n\n\u2248\u1d2c-\u2047\u2083 : \u2200 {k} (m : Bit \u2192 Bits k) (b : Bit) \u2192 \u27ea m b \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea m (not b) \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2083 m b = \u2248\u1d2c-\u2047\u2082 (m b) (m (not b))\n\n\u2248\u1d2c-\u2047\u2084 : \u2200 {k} (m : Bits k \u00d7 Bits k) (b : Bit) \u2192 \u27ea proj m b \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047 \u2248\u1d2c \u27ea proj m (not b) \u27eb\u1d30 \u27e8\u2295\u27e9 \u2047\n\u2248\u1d2c-\u2047\u2084 = \u2248\u1d2c-\u2047\u2083 \u2218 proj\n","old_contents":"module single-bit-one-time-pad where\n\nopen import Function\nopen import Data.Bool.NP\nopen import Data.Product\nopen import Data.Nat.NP\nopen import Relation.Binary.PropositionalEquality.NP\n\nopen import Data.Bits\nopen import flipbased-implem\nopen import program-distance\n\nK = Bit\nM = Bit\nC = Bit\n\nrecord Adv (S\u2080 S\u2081 S\u2082 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n step\u2081 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2082 : C \u00d7 S\u2081 \u2192 S\u2082\n step\u2083 : S\u2082 \u2192 Bit\n\nrecord Adv\u2081 (S\u2080 S\u2081 : Set) ca : Set where\n constructor mk\n field\n step\u2080 : \u21ba ca S\u2080\n -- step\u2081 s\u2080 = id , s\u2080\n step\u2082 : C \u00d7 S\u2080 \u2192 S\u2081\n step\u2083 : S\u2081 \u2192 Bit\n\nAdv\u2082 : \u2200 ca \u2192 Set\nAdv\u2082 ca = \u21ba ca (C \u2192 Bit)\n\nmodule Run\u2141\u2032 {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open Adv A\n E : M \u2192 \u21ba 1 C\n E m = toss >>= \u03bb k \u2192 return\u21ba (m xor k)\n run\u2141\u2032 : \u2141? (ca + 1)\n run\u2141\u2032 b = step\u2080 >>= \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n E (m b) >>= \u03bb c \u2192\n return\u21ba (step\u2083 (step\u2082 (c , s\u2081)))}\n\nmodule Run\u2141 {S\u2080 S\u2081 S\u2082 ca} (E : K \u2192 M \u2192 C) (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Adv A\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n let c = E k (m b) in\n step\u2083 (step\u2082 (c , s\u2081))}\n\n run\u2141 : EXP (1 + ca)\n run\u2141 = toss >>= kont\u2080\n\n {- looks wrong\nmodule Run\u2141-Properties {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b k : Bit) where\n open Run\u2141 A\n kont\u2080-not : kont\u2080 b k \u2261 kont\u2080 (not b) (not k)\n kont\u2080-not rewrite xor-not-not b k = {!refl!}\n -}\n\nmodule SymAdv (homPrgDist : HomPrgDist) {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n step\u2081\u2032 : S\u2080 \u2192 (Bit \u2192 M) \u00d7 S\u2081\n step\u2081\u2032 s\u2080 = case step\u2081 s\u2080 of \u03bb { (m , s\u2081) \u2192 (m \u2218 not , s\u2081) }\n symA : Adv S\u2080 S\u2081 S\u2082 ca\n symA = mk step\u2080 step\u2081\u2032 step\u2082 (not \u2218 step\u2083)\n\n symA\u2032 : Adv S\u2080 S\u2081 S\u2082 ca\n symA\u2032 = mk step\u2080 step\u2081\u2032 step\u2082 step\u2083\n\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n open Run\u2141 _xor_ symA renaming (run\u2141 to runSymA)\n open Run\u2141 _xor_ symA\u2032 renaming (run\u2141 to runSymA\u2032)\n not\u21ba : \u2200 {n} \u2192 EXP n \u2192 EXP n\n not\u21ba = map\u21ba not\n {-\n helper : \u2200 {n} (g\u2080 g\u2081 : EXP n) \u2192 g\u2080 ]-[ g\u2081 \u2192 not\u21ba g\u2080 ]-[ not\u21ba g\u2081\n helper = {!!}\n lem : runA \u21d3 runSymA\n lem A-breaks-E = {!helper (uunSymA\u2032 0b) (runSymA\u2032 1)!}\n where pf : breaks runSymA\n pf = {!!}\n -}\n\nmodule Run\u2141\u2082 {ca} (A : Adv\u2082 ca) (b : Bit) where\n E : Bit \u2192 M \u2192 C\n E k m = m xor k\n\n m : Bit \u2192 Bit\n m = id\n\n kont\u2080 : \u2141? _\n kont\u2080 k =\n A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n\n {-\n kont\u2080\u2032 : \u2141? _\n kont\u2080\u2032 k =\n conv-Adv A \u25b9\u21ba \u03bb f \u2192\n f (E k (m b))\n -}\n\n run\u2141\u2082 : EXP (1 + ca)\n run\u2141\u2082 = toss >>= kont\u2080\n\nmodule Run\u2141\u2082-Properties {ca} (A : Adv\u2082 ca) (b k : Bit) where\n open Run\u2141\u2082 A\n kont\u2080-not : kont\u2080 b k \u2261 kont\u2080 (not b) (not k)\n kont\u2080-not rewrite xor-not-not b k = refl\n\nconv-Adv : \u2200 {ca S\u2080 S\u2081 S\u2082} \u2192 Adv S\u2080 S\u2081 S\u2082 ca \u2192 Adv\u2082 ca\nconv-Adv A = step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n \u03bb c \u2192 m (step\u2083 (step\u2082 (c , s\u2081)))}\n where open Adv A\n\nopen \u2261-Reasoning\n\nmodule Conv-Adv-Props (homPrgDist : HomPrgDist) {ca S\u2080 S\u2081 S\u2082} (A : Adv S\u2080 S\u2081 S\u2082 ca) where\n open HomPrgDist homPrgDist\n open Adv A\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n -- open Run\u2141-Properties A\n\n A\u2032 : Adv\u2082 ca\n A\u2032 = conv-Adv A\n open Run\u2141\u2082 A\u2032 using () renaming (kont\u2080 to kont\u2080\u2032; run\u2141\u2082 to runA\u2032)\n\n kont\u2080\u2032\u2032 : \u2200 b k \u2192 EXP ca\n kont\u2080\u2032\u2032 b k =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 ((m b) xor k , s\u2081)))}\n\n kont\u2080\u2032\u2032\u2032 : \u2200 b\u2032 \u2192 EXP ca\n kont\u2080\u2032\u2032\u2032 b\u2032 =\n step\u2080 \u25b9\u21ba \u03bb s\u2080 \u2192\n case step\u2081 s\u2080 of \u03bb {(m , s\u2081) \u2192\n m (step\u2083 (step\u2082 (b\u2032 , s\u2081)))}\n\n {-\n kont\u2032\u2032-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032\u2032 b k)\n kont\u2032\u2032-lem true true = {!!}\n kont\u2032\u2032-lem false true = {!!}\n kont\u2032\u2032-lem true false = {!!}\n kont\u2032\u2032-lem false false = {!!}\n\n kont-lem : \u2200 b k \u2192 count\u21ba (kont\u2080 b k) \u2261 count\u21ba (kont\u2080\u2032 b k)\n kont-lem b true = {!!}\n kont-lem b false = {!!}\n\n conv-Adv-lem : runA \u2248\u2141? runA\u2032\n conv-Adv-lem b = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 _+_ (kont-lem b 0b) (kont-lem b 1b) \u27e9\n count\u21ba (kont\u2080\u2032 b 0b) + count\u21ba (kont\u2080\u2032 b 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA\u2032 b) \u220e\n\n conv-Adv-sound : runA \u21d3 runA\u2032\n conv-Adv-sound = ]-[-cong-\u2248\u21ba (conv-Adv-lem 0b) (conv-Adv-lem 1b)\n -}\n-- Cute fact: this is true by computation!\ncount\u21ba-toss->>= : \u2200 {c} (f : \u2141? c) \u2192 count\u21ba (toss >>= f) \u2261 count\u21ba (f 0b) + count\u21ba (f 1b)\ncount\u21ba-toss->>= f = refl\n\n{-\nmodule Run\u2141-Properties' {S\u2080 S\u2081 S\u2082 ca} (A : Adv S\u2080 S\u2081 S\u2082 ca) (b : Bit) where\n open Run\u2141 _xor_ A renaming (run\u2141 to runA)\n lem : count\u21ba (runA b) \u2261 count\u21ba (runA (not b))\n lem = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 (_+_ on count\u21ba) {x = kont\u2080 b 0b} {kont\u2080 (not b) 1b} {kont\u2080 b 1b} {kont\u2080 (not b) 0b} {!!} {!!} \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA (not b)) \u220e\n-}\nopen import program-distance\nopen import Relation.Nullary\n\nlem\u2082 : \u2200 {ca} (A : Adv\u2082 ca) b \u2192 count\u21ba (Run\u2141\u2082.run\u2141\u2082 A b) \u2261 count\u21ba (Run\u2141\u2082.run\u2141\u2082 A (not b))\nlem\u2082 A b = count\u21ba (runA b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 b 0b) + count\u21ba (kont\u2080 b 1b)\n \u2261\u27e8 cong\u2082 (_+_ on count\u21ba) (kont\u2080-not 0b) (kont\u2080-not 1b) \u27e9\n count\u21ba (kont\u2080 (not b) 1b) + count\u21ba (kont\u2080 (not b) 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 (not b) 1b)) _ \u27e9\n count\u21ba (kont\u2080 (not b) 0b) + count\u21ba (kont\u2080 (not b) 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA (not b)) \u220e\n where open Run\u2141\u2082 A renaming (run\u2141\u2082 to runA)\n open Run\u2141\u2082-Properties A b\n\nlem\u2083 : \u2200 {ca} (A : Adv\u2082 ca) \u2192 Safe\u2141? (Run\u2141\u2082.run\u2141\u2082 A)\nlem\u2083 A = lem\u2082 A 0b\n\n-- A specialized version of lem\u2082\nlem\u2084 : \u2200 {ca} (A : Adv\u2082 ca) \u2192 Safe\u2141? (Run\u2141\u2082.run\u2141\u2082 A)\nlem\u2084 A = count\u21ba (runA 0b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (kont\u2080 0b 0b) + count\u21ba (kont\u2080 0b 1b)\n \u2261\u27e8 cong\u2082 (_+_ on count\u21ba) (kont\u2080-not 0b) (kont\u2080-not 1b) \u27e9\n count\u21ba (kont\u2080 1b 1b) + count\u21ba (kont\u2080 1b 0b)\n \u2261\u27e8 \u2115\u00b0.+-comm (count\u21ba (kont\u2080 1b 1b)) _ \u27e9\n count\u21ba (kont\u2080 1b 0b) + count\u21ba (kont\u2080 1b 1b)\n \u2261\u27e8 refl \u27e9\n count\u21ba (runA 1b) \u220e\n where open Run\u2141\u2082 A renaming (run\u2141\u2082 to runA)\n open Run\u2141\u2082-Properties A 0b\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"cbb81bff855e175c0ad11d2c2c0312873abe8085","subject":"Agda: sketch proof that foldGroup is indeed an homomorphism","message":"Agda: sketch proof that foldGroup is indeed an homomorphism\n\nI should fill in too many lemmas to make this go through.\n\nOld-commit-hash: 5c3e9a678d7df2110bcc23ec8b1c432ec461e97e\n","repos":"inc-lc\/ilc-agda","old_file":"experimental\/FoldableBag.agda","new_file":"experimental\/FoldableBag.agda","new_contents":"module FoldableBag where\n\nopen import FoldableBagParametric\nimport Level as L\n\nopen import Algebra\nopen import Function\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\n\nimport Data.Nat as N\nimport Data.Integer as Z\n \nopen import Data.Bool \nopen import Data.Maybe\nopen import Data.List as List using (List)\n\nopen import Data.Product\n\n-- Note that hom is what I call foldGroup elsewhere!\n\n-- This is the mathematical definition of group homomorphism. This is mostly\n-- annoying to prove because one can't write the blackboard definition directly\n-- (since bags are not freely generated).\nhomIsAnHom : \u2200 {T} {{oT : Ord T}} G f (b\u2081 b\u2082 : Bag T) \u2192 let open AbelianGroup G in\n hom G f (union b\u2081 b\u2082) \u2261 hom G f b\u2081 \u2219 hom G f b\u2082\nhomIsAnHom {{oT}} G f (b\u207a\u2081 , b\u207b\u2081) (b\u207a\u2082 , b\u207b\u2082) =\n begin\n hom G f (union (b\u207a\u2081 , b\u207b\u2081) (b\u207a\u2082 , b\u207b\u2082))\n \u2261\u27e8\u27e9\n hom G f (uunion b\u207a\u2081 b\u207a\u2082 , uunion b\u207b\u2081 b\u207b\u2082)\n -- TODO: Lots of straightforward group manipulation\n \u2261\u27e8 {!!} \u27e9\n hom G f (b\u207a\u2081 , b\u207b\u2081) \u2219 hom G f (b\u207a\u2082 , b\u207b\u2082)\n \u220e\n where\n open AbelianGroup G\n open \u2261-Reasoning\n open import Data.List.Properties\n open import Sorting\n\n open Sort ord hiding (insert; toList)\n\n-- Simplest possible definition of the derivative of hom (in the semantic domain).\n\nhomDelta : \u2200 {T} {{oT : Ord T}} \u2192 (G : AbelianGroup L.zero L.zero) \u2192 let U = AbelianGroup.Carrier G in (T \u2192 U) \u2192 Bag T \u2192 \u0394Bag T \u2192 U\nhomDelta G f b db = hom G f db\n\n-- This states that the derivative\n\nhomDeltaCorrect : \u2200 G f b db \u2192 let open AbelianGroup G in\n hom G f (union b db) \u2261 hom G f b \u2219 homDelta G f b db\nhomDeltaCorrect G f b db =\n begin\n hom G f (union b db)\n \u2261\u27e8 homIsAnHom G f b db \u27e9\n hom G f b \u2219 hom G f db\n \u2261\u27e8\u27e9\n hom G f b \u2219 homDelta G f b db\n \u220e\n where\n open AbelianGroup G\n open \u2261-Reasoning\n open import Data.List.Properties\n\n--postulate BagGroup : \u2200 {T} (oT : Ord T) \u2192 AbelianGroup L.zero L.zero\n\nmap\u2081 : \u2200 {A B} {oA : Ord A} {oB : Ord B} \u2192 (A \u2192 B) \u2192 Bag A \u2192 Bag B\nmap\u2081 f = hom BagGroup (singleton \u2218 f)\n\nflatMap : \u2200 {A B} {oA : Ord A} {oB : Ord B} \u2192 (A \u2192 Bag B) \u2192 Bag A \u2192 Bag B\nflatMap f = hom BagGroup f\n\n-- Use instance arguments for Ord.\n\nfilter : \u2200 {A} {oA : Ord A} \u2192 (A \u2192 Bool) \u2192 Bag A \u2192 Bag A\nfilter p b = hom BagGroup (\u03bb el \u2192 if (p el) then (singleton el) else empty) b\n\n{-\nTODOs:\n\n * Express basic functions using hom:\n remove \n ...\n\n * State and prove the main theorems. Define changes on bags (as bags first?),\n then the derivative of hom, then write the correctness theorem.\n\n-}\n\n{-\npostulate Bag : Set \u2192 Set\n\nhom : let B = AbelianGroup.Carrier G in \u2200 {A} \u2192 (A \u2192 B) \u2192 Bag A \u2192 B\nhom = {!!}\n-}\n\n\n-- Whoops\u00b2, that's hard to implement. Let's just postulate it.\n--map\u2080 = {!T.map!}\n\n-- map : ({k : Key} \u2192 Value k \u2192 Value k) \u2192 Tree \u2192 Tree\n-- map f (tree t) = tree $ Indexed.map f t\n\n-- _\u2208?_ : Key \u2192 Tree \u2192 Bool\n-- k \u2208? t = maybeToBool (lookup k t)\n\n------------------------------------------------\n-- Elimination forms for trees\n------------------------------------------------\n\n-- -- Naive implementations of union.\n\n-- unionWith : (\u2200 {k} \u2192 Value k \u2192 Value k \u2192 Value k) \u2192\n-- -- Left \u2192 right \u2192 result.\n-- Tree \u2192 Tree \u2192 Tree\n\n-- union : Tree \u2192 Tree \u2192 Tree\n-- union = unionWith const\n\n-- unionsWith : (\u2200 {k} \u2192 Value k \u2192 Value k \u2192 Value k) \u2192 List Tree \u2192 Tree\n\n-- -- Left-biased.\n\n-- unions : List Tree \u2192 Tree\n-- unions = unionsWith const\n\n","old_contents":"module FoldableBag where\n\nopen import FoldableBagParametric\nimport Level as L\n\nopen import Algebra\nopen import Function\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\n\nimport Data.Nat as N\nimport Data.Integer as Z\n \nopen import Data.Bool \nopen import Data.Maybe\nopen import Data.List as List using (List)\n\nopen import Data.Product\n\n-- Simplest possible definition of the derivative of hom (in the semantic domain).\n\nhomDelta : \u2200 {T} {{oT : Ord T}} \u2192 (G : AbelianGroup L.zero L.zero) \u2192 let U = AbelianGroup.Carrier G in (T \u2192 U) \u2192 Bag T \u2192 \u0394Bag T \u2192 U\nhomDelta G f b db = hom G f db\n\n-- This states that the derivative\n\nhomDeltaCorrect : \u2200 G f b db \u2192 let open AbelianGroup G in\n hom G f (union b db) \u2261 hom G f b \u2219 homDelta G f b db\nhomDeltaCorrect G f b db =\n begin\n hom G f (union b db)\n \u2261\u27e8 {!!} \u27e9\n hom G f b \u2219 hom G f db\n \u2261\u27e8\u27e9\n hom G f b \u2219 homDelta G f b db\n \u220e\n where\n open AbelianGroup G\n open \u2261-Reasoning\n open import Data.List.Properties\n\n--postulate BagGroup : \u2200 {T} (oT : Ord T) \u2192 AbelianGroup L.zero L.zero\n\nmap\u2081 : \u2200 {A B} {oA : Ord A} {oB : Ord B} \u2192 (A \u2192 B) \u2192 Bag A \u2192 Bag B\nmap\u2081 f = hom BagGroup (singleton \u2218 f)\n\nflatMap : \u2200 {A B} {oA : Ord A} {oB : Ord B} \u2192 (A \u2192 Bag B) \u2192 Bag A \u2192 Bag B\nflatMap f = hom BagGroup f\n\n-- Use instance arguments for Ord.\n\nfilter : \u2200 {A} {oA : Ord A} \u2192 (A \u2192 Bool) \u2192 Bag A \u2192 Bag A\nfilter p b = hom BagGroup (\u03bb el \u2192 if (p el) then (singleton el) else empty) b\n\n{-\nTODOs:\n\n * Express basic functions using hom:\n remove \n ...\n\n * State and prove the main theorems. Define changes on bags (as bags first?),\n then the derivative of hom, then write the correctness theorem.\n\n-}\n\n{-\npostulate Bag : Set \u2192 Set\n\nhom : let B = AbelianGroup.Carrier G in \u2200 {A} \u2192 (A \u2192 B) \u2192 Bag A \u2192 B\nhom = {!!}\n-}\n\n\n-- Whoops\u00b2, that's hard to implement. Let's just postulate it.\n--map\u2080 = {!T.map!}\n\n-- map : ({k : Key} \u2192 Value k \u2192 Value k) \u2192 Tree \u2192 Tree\n-- map f (tree t) = tree $ Indexed.map f t\n\n-- _\u2208?_ : Key \u2192 Tree \u2192 Bool\n-- k \u2208? t = maybeToBool (lookup k t)\n\n------------------------------------------------\n-- Elimination forms for trees\n------------------------------------------------\n\n-- -- Naive implementations of union.\n\n-- unionWith : (\u2200 {k} \u2192 Value k \u2192 Value k \u2192 Value k) \u2192\n-- -- Left \u2192 right \u2192 result.\n-- Tree \u2192 Tree \u2192 Tree\n\n-- union : Tree \u2192 Tree \u2192 Tree\n-- union = unionWith const\n\n-- unionsWith : (\u2200 {k} \u2192 Value k \u2192 Value k \u2192 Value k) \u2192 List Tree \u2192 Tree\n\n-- -- Left-biased.\n\n-- unions : List Tree \u2192 Tree\n-- unions = unionsWith const\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"25f0137572c2e43bdea68c2f240867df19ffa108","subject":"improved proof for leq-trans","message":"improved proof for leq-trans\n","repos":"shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps,shouya\/thinking-dumps","old_file":"agda-tutorial\/basics.agda","new_file":"agda-tutorial\/basics.agda","new_contents":"data Bool : Set where\n true : Bool\n false : Bool\n\n\nnot : Bool \u2192 Bool\nnot true = false\nnot false = true\n\n\ndata \u2115 : Set where\n O : \u2115\n S : \u2115 \u2192 \u2115\n\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nO + a = a\nS a + b = S (a + b)\n\n_*_ : \u2115 \u2192 \u2115 \u2192 \u2115\nO * a = O\nS a * b = a + (a * b)\n\n_or_ : Bool \u2192 Bool \u2192 Bool\ntrue or _ = true\nfalse or b = b\n\n\nif_then_else_ : {A : Set} \u2192 Bool \u2192 A \u2192 A \u2192 A\nif true then x else y = x\nif false then x else y = y\n\n\ninfixl 60 _*_\ninfixl 40 _+_\ninfixr 20 _or_\ninfix 5 if_then_else_\n\n\ninfixr 40 _::_\ndata List (A : Set) : Set where\n [] : List A\n _::_ : A -> List A -> List A\n\n\n_\u2218_ : {A : Set} -> {B : A -> Set} -> {C : (x : A) -> B x -> Set} ->\n (f : {x : A} -> (y : B x) -> C x y) -> (g : (x : A) -> B x) ->\n (x : A) -> C x (g x)\n_\u2218_ f g a = f (g a)\n\n\nplus-two = S \u2218 S\n\nmap : {A B : Set} -> (A -> B) -> List A -> List B\nmap f [] = []\nmap f (x :: xs) = f x :: map f xs\n\n_++_ : {A : Set} -> List A -> List A -> List A\n[] ++ ys = ys\nx :: xs ++ ys = x :: (xs ++ ys)\n\n\ndata Vec (A : Set) : \u2115 -> Set where\n nil : Vec A O\n cons : (n : \u2115) -> A -> Vec A n -> Vec A (S n)\n\nhead : {A : Set} {n : \u2115} -> Vec A (S n) -> A\nhead (cons n v vs) = v\n\n\nvmap : {A B : Set} (n : \u2115) -> (A -> B) -> Vec A n -> Vec B n\nvmap .O f nil = nil\nvmap .(S n) f (cons n x xs) = cons n (f x) (vmap n f xs)\n\n\nvmap\u2032 : {A B : Set} (n : \u2115) -> (A -> B) -> Vec A n -> Vec B n\nvmap\u2032 O f nil = nil\nvmap\u2032 (S n) f (cons .n x xs) = cons n (f x) (vmap n f xs)\n\n\ndata Fin : \u2115 -> Set where\n fzero : {n : \u2115} -> Fin (S n)\n fsuc : {n : \u2115} -> Fin n -> Fin (S n)\n\n_!_ : {n : \u2115}{A : Set} -> Vec A n -> Fin n -> A\nnil ! ()\ncons n x a ! fzero = x\ncons n x a ! fsuc b = a ! b\n\n\n\ntabulate : {n : \u2115}{A : Set} -> (Fin n -> A) -> Vec A n\ntabulate {O} f = nil\ntabulate {S n} f = cons n (f fzero) (tabulate (f \u2218 fsuc))\n\n\ndata False : Set where\nrecord True : Set where\n\n\ntrivial : True\ntrivial = _\n\n\nisTrue : Bool -> Set\nisTrue true = True\nisTrue false = False\n\n\n_<_ : \u2115 -> \u2115 -> Bool\n_ < O = false\nO < S n = true\nS m < S n = m < n\n\nlength : {A : Set} -> List A -> \u2115\nlength [] = O\nlength (x :: xs) = S (length xs)\n\nlookup : {A : Set}(xs : List A)(n : \u2115) -> isTrue (n < length xs) -> A\nlookup (x :: xs) O b = x\nlookup (x :: xs) (S n) b = lookup xs n b\nlookup [] a ()\n\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\n\ndata _\u2264_ : \u2115 -> \u2115 -> Set where\n \u2264O : {m n : \u2115} -> m == n -> m \u2264 n\n \u2264I : {m n : \u2115} -> m \u2264 n -> m \u2264 S n\n\n\nleq-trans : {l m n : \u2115} -> l \u2264 m -> m \u2264 n -> l \u2264 n\nleq-trans a (\u2264O refl) = a\nleq-trans a (\u2264I b) = \u2264I (leq-trans a b)\n","old_contents":"data Bool : Set where\n true : Bool\n false : Bool\n\n\nnot : Bool \u2192 Bool\nnot true = false\nnot false = true\n\n\ndata \u2115 : Set where\n O : \u2115\n S : \u2115 \u2192 \u2115\n\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nO + a = a\nS a + b = S (a + b)\n\n_*_ : \u2115 \u2192 \u2115 \u2192 \u2115\nO * a = O\nS a * b = a + (a * b)\n\n_or_ : Bool \u2192 Bool \u2192 Bool\ntrue or _ = true\nfalse or b = b\n\n\nif_then_else_ : {A : Set} \u2192 Bool \u2192 A \u2192 A \u2192 A\nif true then x else y = x\nif false then x else y = y\n\n\ninfixl 60 _*_\ninfixl 40 _+_\ninfixr 20 _or_\ninfix 5 if_then_else_\n\n\ninfixr 40 _::_\ndata List (A : Set) : Set where\n [] : List A\n _::_ : A -> List A -> List A\n\n\n_\u2218_ : {A : Set} -> {B : A -> Set} -> {C : (x : A) -> B x -> Set} ->\n (f : {x : A} -> (y : B x) -> C x y) -> (g : (x : A) -> B x) ->\n (x : A) -> C x (g x)\n_\u2218_ f g a = f (g a)\n\n\nplus-two = S \u2218 S\n\nmap : {A B : Set} -> (A -> B) -> List A -> List B\nmap f [] = []\nmap f (x :: xs) = f x :: map f xs\n\n_++_ : {A : Set} -> List A -> List A -> List A\n[] ++ ys = ys\nx :: xs ++ ys = x :: (xs ++ ys)\n\n\ndata Vec (A : Set) : \u2115 -> Set where\n nil : Vec A O\n cons : (n : \u2115) -> A -> Vec A n -> Vec A (S n)\n\nhead : {A : Set} {n : \u2115} -> Vec A (S n) -> A\nhead (cons n v vs) = v\n\n\nvmap : {A B : Set} (n : \u2115) -> (A -> B) -> Vec A n -> Vec B n\nvmap .O f nil = nil\nvmap .(S n) f (cons n x xs) = cons n (f x) (vmap n f xs)\n\n\nvmap\u2032 : {A B : Set} (n : \u2115) -> (A -> B) -> Vec A n -> Vec B n\nvmap\u2032 O f nil = nil\nvmap\u2032 (S n) f (cons .n x xs) = cons n (f x) (vmap n f xs)\n\n\ndata Fin : \u2115 -> Set where\n fzero : {n : \u2115} -> Fin (S n)\n fsuc : {n : \u2115} -> Fin n -> Fin (S n)\n\n_!_ : {n : \u2115}{A : Set} -> Vec A n -> Fin n -> A\nnil ! ()\ncons n x a ! fzero = x\ncons n x a ! fsuc b = a ! b\n\n\n\ntabulate : {n : \u2115}{A : Set} -> (Fin n -> A) -> Vec A n\ntabulate {O} f = nil\ntabulate {S n} f = cons n (f fzero) (tabulate (f \u2218 fsuc))\n\n\ndata False : Set where\nrecord True : Set where\n\n\ntrivial : True\ntrivial = _\n\n\nisTrue : Bool -> Set\nisTrue true = True\nisTrue false = False\n\n\n_<_ : \u2115 -> \u2115 -> Bool\n_ < O = false\nO < S n = true\nS m < S n = m < n\n\nlength : {A : Set} -> List A -> \u2115\nlength [] = O\nlength (x :: xs) = S (length xs)\n\nlookup : {A : Set}(xs : List A)(n : \u2115) -> isTrue (n < length xs) -> A\nlookup (x :: xs) O b = x\nlookup (x :: xs) (S n) b = lookup xs n b\nlookup [] a ()\n\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\n\ndata _\u2264_ : \u2115 -> \u2115 -> Set where\n \u2264O : {m n : \u2115} -> m == n -> m \u2264 n\n \u2264I : {m n : \u2115} -> m \u2264 n -> m \u2264 S n\n\n\nleq-trans : {l m n : \u2115} -> l \u2264 m -> m \u2264 n -> l \u2264 n\nleq-trans (\u2264O refl) b = b\nleq-trans a (\u2264O refl) = a\nleq-trans (\u2264I a) (\u2264I b) = \u2264I (leq-trans (\u2264I a) b)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"1f9a0adb2fdaddbdd525b0cda3df4325249bfeb4","subject":"Fixed a misplaced importation.","message":"Fixed a misplaced importation.\n\nIgnore-this: f44340563188fa12e9b79aed8492deab\n\ndarcs-hash:20120228152056-3bd4e-143ebb2d8b0da74339da12c68888ca6a759c6afe.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOL\/FOL.agda","new_file":"src\/FOL\/FOL.agda","new_contents":"------------------------------------------------------------------------------\n-- FOL (without equality)\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module exported all the logical constants and the\n-- propositional equality. This module is re-exported by the \"base\"\n-- modules whose theories are defined on FOL (without equality)\n\nmodule FOL.FOL where\n\n------------------------------------------------------------------------------\n-- The propositional logical connectives\n\n-- The logical connectives are hard-coded in our translation,\n-- i.e. the symbols \u22a5, \u22a4, \u00ac, \u2227, \u2228, \u2192, and \u2194 must be used.\n-- N.B. For the implication we use the Agda function type.\nopen import FOL.Data.Empty public using ( \u22a5 ; \u22a5-elim )\nopen import FOL.Data.Product public using ( _\u2227_ ; _,_ ; \u2227-proj\u2081 ; \u2227-proj\u2082 )\nopen import FOL.Data.Sum public using ( _\u2228_ ; [_,_] ; inj\u2081 ; inj\u2082 )\nopen import FOL.Data.Unit public using ( \u22a4 )\nopen import FOL.Relation.Nullary public using ( \u00ac_ )\n\ninfixr 2 _\u2194_\n\n_\u2194_ : Set \u2192 Set \u2192 Set\nP \u2194 Q = (P \u2192 Q) \u2227 (Q \u2192 P)\n\n------------------------------------------------------------------------------\n-- The quantifiers\n\n-- The existential quantifier\n-- The existential quantifier is hard-coded in our translation,\n-- i.e. the symbol \u2203 must be used.\n\nopen import FOL.Data.Product public using ( _,,_ ; \u2203 ; \u2203-elim )\n\n-- The universal quantifier\n-- N.B. For the universal quantifier we use the Agda (dependent)\n-- function type.\n","old_contents":"------------------------------------------------------------------------------\n-- FOL (without equality)\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module exported all the logical constants and the\n-- propositional equality. This module is re-exported by the \"base\"\n-- modules whose theories are defined on FOL (without equality)\n\nmodule FOL.FOL where\n\n------------------------------------------------------------------------------\n-- The propositional logical connectives\n\n-- The logical connectives are hard-coded in our translation,\n-- i.e. the symbols \u22a5, \u22a4, \u00ac, \u2227, \u2228, \u2192, and \u2194 must be used.\n-- N.B. For the implication we use the Agda function type.\nopen import FOL.Data.Empty public using ( \u22a5 ; \u22a5-elim )\nopen import FOL.Data.Product public using ( _\u2227_ ; _,_ ; _,,_ ; \u2227-proj\u2081 ; \u2227-proj\u2082 )\nopen import FOL.Data.Sum public using ( _\u2228_ ; [_,_] ; inj\u2081 ; inj\u2082 )\nopen import FOL.Data.Unit public using ( \u22a4 )\nopen import FOL.Relation.Nullary public using ( \u00ac_ )\n\ninfixr 2 _\u2194_\n\n_\u2194_ : Set \u2192 Set \u2192 Set\nP \u2194 Q = (P \u2192 Q) \u2227 (Q \u2192 P)\n\n------------------------------------------------------------------------------\n-- The quantifiers\n\n-- The existential quantifier\n-- The existential quantifier is hard-coded in our translation,\n-- i.e. the symbol \u2203 must be used.\n\nopen import FOL.Data.Product public using ( _,_ ; \u2203 ; \u2203-elim )\n\n-- The universal quantifier\n-- N.B. For the universal quantifier we use the Agda (dependent)\n-- function type.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"73758b64f31209c83ec68f4efd4b6f450f4edebb","subject":"Example 3 works now\u0302\u00b9. \u0302\u00b9 Other than that the `Bag` type is an abstract postulate.","message":"Example 3 works now\u0302\u00b9.\n\u0302\u00b9 Other than that the `Bag` type is an abstract postulate.\n\nA presentation of example 3 starts from line 297.\n\nThe optimized derivation `derive2` considers the derivative of\na term to be the nil change only if it is closed. Should we have\n\u03b2-equivalence test or some inherited description of whether the\nargument to an abs-term may change, we could handle more examples.\n\nOld-commit-hash: 6488c110b93dc4c760ddc61feef198b55ab4940d\n","repos":"inc-lc\/ilc-agda","old_file":"experimental\/ExplicitNil.agda","new_file":"experimental\/ExplicitNil.agda","new_contents":"{-\nCommunicate to derivatives that changes to certain arguments\nare always nil (i. e., certain arguments are stable).\n-}\n\nmodule ExplicitNil where\n\nopen import TaggedDeltaTypes\n\nopen import Data.Nat\nopen import Data.Bool\nopen import Data.Sum hiding (map)\nopen import Data.Product hiding (map)\nopen import Data.Unit using (\u22a4 ; tt)\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\nopen import Relation.Binary.Core using (Decidable)\nopen import Relation.Nullary.Core using (yes ; no)\n\next\u00b3 : \u2200\n {A : Set}\n {B : A \u2192 Set}\n {C : (a : A) \u2192 B a \u2192 Set }\n {D : (a : A) \u2192 (b : B a) \u2192 C a b \u2192 Set}\n {f g : (a : A) \u2192 (b : B a) \u2192 (c : C a b) \u2192 D a b c} \u2192\n ((a : A) (b : B a) (c : C a b) \u2192 f a b c \u2261 g a b c) \u2192 f \u2261 g\n\next\u00b3 fabc=gabc = ext (\u03bb a \u2192 ext (\u03bb b \u2192 ext (\u03bb c \u2192 fabc=gabc a b c)))\n where ext = extensionality\n\nproj-H : \u2200 {\u0393 : Context} {\u03c1 : \u0394Env \u0393} {us vs} \u2192\n Honest \u03c1 (FV-union us vs) \u2192 Honest \u03c1 us \u00d7 Honest \u03c1 vs\nproj-H {\u2205} {us = \u2205} {vs = \u2205} clearly = clearly , clearly\nproj-H {us = alter us} {alter vs} (alter H) =\n let uss , vss = proj-H H in alter uss , alter vss\nproj-H {us = alter us} {abide vs} (abide eq H) =\n let uss , vss = proj-H H in\n alter uss , abide eq vss\nproj-H {us = abide us} {alter vs} (abide eq H) =\n let uss , vss = proj-H H in\n abide eq uss , alter vss\nproj-H {us = abide us} {abide vs} (abide eq H) =\n let uss , vss = proj-H H in\n abide eq uss , abide eq vss\n\n-- Equivalence proofs are unique\nequivalence-unique : \u2200 {A : Set} {a b : A} \u2192 \u2200 {p q : a \u2261 b} \u2192 p \u2261 q\nequivalence-unique {p = refl} {refl} = refl\n\n-- Product of singletons are singletons (for example)\nproduct-unique : \u2200 {A : Set} {B : A \u2192 Set} \u2192\n (\u2200 {a b : A} \u2192 a \u2261 b) \u2192\n (\u2200 {a : A} {c d : B a} \u2192 c \u2261 d) \u2192\n (\u2200 {p q : \u03a3 A B} \u2192 p \u2261 q)\nproduct-unique {A} {B} lhs rhs {a , c} {b , d}\n rewrite lhs {a} {b} = cong (_,_ b) rhs\n\n-- Validity proofs are (extensionally) unique (as functions)\nvalidity-unique : \u2200 {\u03c4} {v : \u27e6 \u03c4 \u27e7} {dv : \u0394Val \u03c4} \u2192\n \u2200 {p q : valid v dv} \u2192 p \u2261 q\nvalidity-unique {nats} = equivalence-unique\nvalidity-unique {bags} = refl\nvalidity-unique {\u03c3 \u21d2 \u03c4} = ext\u00b3 (\u03bb v dv R[v,dv] \u2192\n product-unique validity-unique equivalence-unique)\n\nstabilityVar : \u2200 {\u03c4 \u0393} {x : Var \u0393 \u03c4} \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} (H : Honest \u03c1 (select-just x)) \u2192\n \u27e6 x \u27e7 (ignore \u03c1) \u2295 \u27e6 x \u27e7\u0394Var \u03c1 \u2261 \u27e6 x \u27e7 (ignore \u03c1)\n\nstabilityVar {x = this} (abide proof _) = proof\nstabilityVar {x = that y} (alter H) = stabilityVar {x = y} H\n\nstability : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} (H : Honest \u03c1 (FV t)) \u2192\n \u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n \u2261 \u27e6 t \u27e7 (ignore \u03c1)\n\n-- Boilerplate begins\nstabilityAbs : \u2200 {\u03c3 \u03c4 \u0393} {t : Term (\u03c3 \u2022 \u0393) \u03c4} \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} (H : Honest \u03c1 (FV (abs t))) \u2192\n (v : \u27e6 \u03c3 \u27e7) \u2192\n \u27e6 t \u27e7 (v \u2022 ignore \u03c1) \u2295\n \u27e6 derive t \u27e7\u0394 (cons v (v \u229d v) R[v,u\u229dv] \u03c1) (unrestricted t)\n \u2261 \u27e6 t \u27e7 (v \u2022 ignore \u03c1)\nstabilityAbs {t = t} {\u03c1} H v with FV t | inspect FV t\n... | abide vars | [ case0 ] = stability {t = t} Ht\n where\n Ht : Honest (cons v (v \u229d v) R[v,u\u229dv] \u03c1) (FV t)\n Ht rewrite case0 = abide v\u2295[u\u229dv]=u H\n... | alter vars | [ case1 ] = stability {t = t} Ht\n where\n Ht : Honest (cons v (v \u229d v) R[v,u\u229dv] \u03c1) (FV t)\n Ht rewrite case1 = alter H\n-- Boilerplate ends\n\nstability {t = nat n} H = refl\nstability {t = bag b} H = b++\u2205=b\nstability {t = var x} H = stabilityVar H\nstability {t = abs t} {\u03c1} H = extensionality (stabilityAbs {t = t} H)\nstability {t = app s t} {\u03c1} H =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n v = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n dv = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n Hs , Ht = proj-H H\n in\n begin\n f v \u2295 df v dv (validity {t = t})\n \u2261\u27e8 sym (corollary s t) \u27e9\n (f \u2295 df) (v \u2295 dv)\n \u2261\u27e8 stability {t = s} Hs \u27e8$\u27e9 stability {t = t} Ht \u27e9\n f v\n \u220e where open \u2261-Reasoning\nstability {t = add s t} H =\n let Hs , Ht = proj-H H\n in cong\u2082 _+_ (stability {t = s} Hs) (stability {t = t} Ht)\nstability {t = map s t} {\u03c1} H =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n db = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n Hs , Ht = proj-H H\n map = mapBag\n in\n begin\n map f b \u2295 (map (f \u2295 df) (b \u2295 db) \u229d map f b)\n \u2261\u27e8 b++[d\\\\b]=d \u27e9\n map (f \u2295 df) (b \u2295 db)\n \u2261\u27e8 cong\u2082 map (stability {t = s} Hs) (stability {t = t} Ht) \u27e9\n map f b\n \u220e where open \u2261-Reasoning\n\neq-map : \u2200 {\u0393}\n (s : Term \u0393 (nats \u21d2 nats))\n (t : Term \u0393 bags)\n (\u03c1 : \u0394Env \u0393)\n (H : Honest \u03c1 (FV s)) \u2192\n \u27e6 \u0394map\u2081 s (derive t) \u27e7\u0394 \u03c1 (cons (unrestricted t) H tt tt)\n \u2261 \u27e6 \u0394map\u2080 s (derive s) t (derive t) \u27e7\u0394 \u03c1 (unrestricted (map s t))\n\neq-map s t \u03c1 H =\n let\n ds = derive s\n dt = derive t\n f = \u27e6 s \u27e7 (ignore \u03c1)\n v = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 ds \u27e7\u0394 \u03c1 (unrestricted s)\n dv = \u27e6 dt \u27e7\u0394 \u03c1 (unrestricted t)\n\n eq1 : \u27e6 \u0394map\u2080 s ds t dt \u27e7\u0394 \u03c1 (unrestricted (map s t))\n \u2261 mapBag (f \u2295 df) (v \u2295 dv) \u229d mapBag f v\n eq1 = refl\n\n eq2 : mapBag (f \u2295 df) (v \u2295 dv) \u229d mapBag f v\n \u2261 mapBag f dv\n eq2 = trans\n (cong (\u03bb hole \u2192 hole \u229d mapBag f v) (trans\n (cong (\u03bb hole \u2192 mapBag hole (v \u2295 dv)) (stability {t = s} H))\n map-over-++))\n [b++d]\\\\b=d\n\n eq3 : mapBag f dv\n \u2261 \u27e6 \u0394map\u2081 s (derive t) \u27e7\u0394 \u03c1 (cons (unrestricted t) H tt tt)\n eq3 = refl\n\n in sym (trans eq1 (trans eq2 eq3))\n\n-- Vars test\nnone-selected? : \u2200 {\u0393} \u2192 (vs : Vars \u0393) \u2192 (vs \u2261 select-none) \u228e \u22a4\nnone-selected? \u2205 = inj\u2081 refl\nnone-selected? (abide vs) = inj\u2082 tt\nnone-selected? (alter vs) with none-selected? vs\n... | inj\u2081 vs=\u2205 rewrite vs=\u2205 = inj\u2081 refl\n... | inj\u2082 _ = inj\u2082 tt\n\nclosed? : \u2200 {\u03c4 \u0393} \u2192 (t : Term \u0393 \u03c4) \u2192 (FV t \u2261 select-none) \u228e \u22a4\nclosed? t = none-selected? (FV t)\n\nvacuous-honesty : \u2200 {\u0393} {\u03c1 : \u0394Env \u0393} \u2192 Honest \u03c1 select-none\nvacuous-honesty {\u2205} {\u2205} = clearly\nvacuous-honesty {\u03c4 \u2022 \u0393} {cons _ _ _ \u03c1} = alter (vacuous-honesty {\u03c1 = \u03c1})\n\n-- Immunity of closed terms to dishonest environments\nimmune : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} \u2192\n (FV t \u2261 select-none) \u2192 \u2200 {\u03c1} \u2192 Honest \u03c1 (FV t)\nimmune {t = t} eq rewrite eq = vacuous-honesty\n\n-- Ineffectual first optimization step\nderive1 : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394Term \u0393 \u03c4\nderive1 (map s t) with closed? s\n... | inj\u2081 is-closed = \u0394map\u2081 s (derive t)\n... | inj\u2082 tt = derive (map s t)\nderive1 others = derive others\n\nvalid1 : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) {\u03c1 : \u0394Env \u0393} \u2192 derive1 t is-valid-for \u03c1\nvalid1 (nat n) = tt\nvalid1 (bag b) = tt\nvalid1 (var x) = tt\nvalid1 (abs t) = \u03bb _ _ _ \u2192 unrestricted t\nvalid1 (app s t) =\n cons (unrestricted s) (unrestricted t) (validity {t = t}) tt\nvalid1 (add s t) =\n cons (unrestricted s) (unrestricted t) tt tt\nvalid1 (map s t) {\u03c1} with closed? s\n... | inj\u2082 tt = cons (unrestricted s) (unrestricted t) tt tt\n... | inj\u2081 if-closed =\n cons (unrestricted t) (immune {t = s} if-closed) tt tt\n\ncorrect1 : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n \u27e6 derive1 t \u27e7\u0394 \u03c1 (valid1 t) \u2261 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n\ncorrect1 {t = nat n} = refl\ncorrect1 {t = bag b} = refl\ncorrect1 {t = var x} = refl\ncorrect1 {t = abs t} = refl\ncorrect1 {t = app s t} = refl\ncorrect1 {t = add s t} = refl\ncorrect1 {t = map s t} {\u03c1} with closed? s\n... | inj\u2082 tt = refl\n... | inj\u2081 if-closed = eq-map s t \u03c1 (immune {t = s} if-closed)\n\n-- derive2 = derive1 + congruence\nderive2 : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394Term \u0393 \u03c4\nderive2 (abs t) = \u0394abs (derive2 t)\nderive2 (app s t) = \u0394app (derive2 s) t (derive2 t)\nderive2 (add s t) = \u0394add (derive2 s) (derive2 t)\nderive2 (map s t) with closed? s\n... | inj\u2081 is-closed = \u0394map\u2081 s (derive2 t)\n... | inj\u2082 tt = \u0394map\u2080 s (derive2 s) t (derive2 t)\nderive2 others = derive others\n\nvalid2 : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) {\u03c1 : \u0394Env \u0393} \u2192 derive2 t is-valid-for \u03c1\ncorrect2 : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n \u27e6 derive2 t \u27e7\u0394 \u03c1 (valid2 t) \u2261 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n\nvalid2 (nat n) = tt\nvalid2 (bag b) = tt\nvalid2 (var x) = tt\nvalid2 (abs t) = \u03bb _ _ _ \u2192 valid2 t\nvalid2 (app s t) {\u03c1} = cons (valid2 s) (valid2 t) V tt\n where\n V : valid (\u27e6 t \u27e7 (ignore \u03c1)) (\u27e6 derive2 t \u27e7\u0394 \u03c1 (valid2 t))\n V rewrite correct2 {t = t} {\u03c1} = validity {t = t} {\u03c1}\nvalid2 (add s t) = cons (valid2 s) (valid2 t) tt tt\nvalid2 (map s t) {\u03c1} with closed? s\n... | inj\u2082 tt = cons (valid2 s) (valid2 t) tt tt\n... | inj\u2081 if-closed =\n cons (valid2 t) (immune {t = s} if-closed) tt tt\n\ncorrect2 {t = nat n} = refl\ncorrect2 {t = bag b} = refl\ncorrect2 {t = var x} = refl\ncorrect2 {t = abs t} = ext\u00b3 (\u03bb _ _ _ \u2192 correct2 {t = t})\ncorrect2 {t = app {\u03c3} {\u03c4} s t} {\u03c1} = \u2245-to-\u2261 eq-all where\n open import Relation.Binary.HeterogeneousEquality hiding (cong\u2082)\n import Relation.Binary.HeterogeneousEquality as HET\n df0 : \u0394Val (\u03c3 \u21d2 \u03c4)\n df0 = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n df2 : \u0394Val (\u03c3 \u21d2 \u03c4)\n df2 = \u27e6 derive2 s \u27e7\u0394 \u03c1 (valid2 s)\n dv0 : \u0394Val \u03c3\n dv0 = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n dv2 : \u0394Val \u03c3\n dv2 = \u27e6 derive2 t \u27e7\u0394 \u03c1 (valid2 t)\n v : \u27e6 \u03c3 \u27e7\n v = \u27e6 t \u27e7 (ignore \u03c1)\n eq-df : df2 \u2261 df0\n eq-df = correct2 {t = s}\n eq-dv : dv2 \u2261 dv0\n eq-dv = correct2 {t = t}\n eq-all : df2 v dv2 (caddr (valid2 (app s t) {\u03c1}))\n \u2245 df0 v dv0 (validity {t = t} {\u03c1})\n eq-all rewrite eq-dv = HET.cong\n (\u03bb hole \u2192 hole v dv0 (validity {t = t} {\u03c1})) (\u2261-to-\u2245 eq-df)\n -- found without intuition by trial-and-error\ncorrect2 {t = add s t} {\u03c1} =\n let\n cs = correct2 {t = s} {\u03c1}\n ct = correct2 {t = t} {\u03c1}\n in cong\u2082 _,_ (cong\u2082 _+_ (cong proj\u2081 cs) (cong proj\u2081 ct))\n (cong\u2082 _+_ (cong proj\u2082 cs) (cong proj\u2082 ct))\ncorrect2 {t = map s t} {\u03c1} with closed? s\n... | inj\u2082 tt = \n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n in\n cong\u2082 (\u03bb h1 h2 \u2192 mapBag (f \u2295 h1) (b \u2295 h2) \\\\ mapBag f b)\n (correct2 {t = s}) (correct2 {t = t})\n... | inj\u2081 if-closed = trans\n (cong (mapBag (\u27e6 s \u27e7 (ignore \u03c1))) (correct2 {t = t}))\n (eq-map s t \u03c1 (immune {t = s} if-closed))\n\n---------------\n-- Example 3 --\n---------------\n\n-- [+1] = \u03bb x \u2192 x + 1\n[+1] : \u2200 {\u0393} \u2192 Term \u0393 (nats \u21d2 nats)\n[+1] = (abs (add (var this) (nat 1)))\n\n-- inc = \u03bb bag \u2192 map [+1] bag\ninc : Term \u2205 (bags \u21d2 bags)\ninc = abs (map [+1] (var this))\n\n-- Program transformation in example 3\n-- Sadly, `inc` is not executable, for `Bag` is an abstract type\n-- whose existence we postulated.\n--\n-- derive2 inc = \u03bb bag dbag \u2192 map [+1] dbag\nexample-3 : derive2 inc \u2261 \u0394abs (\u0394map\u2081 [+1] (\u0394var this))\nexample-3 = refl\n\n-- Correctness of optimized derivation on `inc`\n--\n-- \u27e6 derive2 inc \u27e7 = \u27e6 derive inc \u27e7 \nexample-3-correct :\n \u27e6 derive2 inc \u27e7\u0394 \u2205 (valid2 inc) \u2261 \u27e6 derive inc \u27e7\u0394 \u2205 (unrestricted inc)\nexample-3-correct = correct2 {t = inc} {\u2205}\n","old_contents":"{-\nCommunicate to derivatives that changes to certain arguments\nare always nil (i. e., certain arguments are stable).\n-}\n\nmodule ExplicitNil where\n\nopen import TaggedDeltaTypes\n\nopen import Data.Nat\nopen import Data.Bool\nopen import Data.Sum hiding (map)\nopen import Data.Product hiding (map)\nopen import Data.Unit using (\u22a4 ; tt)\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary using\n (Reflexive ; Transitive ; Preorder ; IsPreorder)\nopen import Relation.Binary.Core using (Decidable)\nopen import Relation.Nullary.Core using (yes ; no)\n\next\u00b3 : \u2200\n {A : Set}\n {B : A \u2192 Set}\n {C : (a : A) \u2192 B a \u2192 Set }\n {D : (a : A) \u2192 (b : B a) \u2192 C a b \u2192 Set}\n {f g : (a : A) \u2192 (b : B a) \u2192 (c : C a b) \u2192 D a b c} \u2192\n ((a : A) (b : B a) (c : C a b) \u2192 f a b c \u2261 g a b c) \u2192 f \u2261 g\n\next\u00b3 fabc=gabc = ext (\u03bb a \u2192 ext (\u03bb b \u2192 ext (\u03bb c \u2192 fabc=gabc a b c)))\n where ext = extensionality\n\nproj-H : \u2200 {\u0393 : Context} {\u03c1 : \u0394Env \u0393} {us vs} \u2192\n Honest \u03c1 (FV-union us vs) \u2192 Honest \u03c1 us \u00d7 Honest \u03c1 vs\nproj-H {\u2205} {us = \u2205} {vs = \u2205} clearly = clearly , clearly\nproj-H {us = alter us} {alter vs} (alter H) =\n let uss , vss = proj-H H in alter uss , alter vss\nproj-H {us = alter us} {abide vs} (abide eq H) =\n let uss , vss = proj-H H in\n alter uss , abide eq vss\nproj-H {us = abide us} {alter vs} (abide eq H) =\n let uss , vss = proj-H H in\n abide eq uss , alter vss\nproj-H {us = abide us} {abide vs} (abide eq H) =\n let uss , vss = proj-H H in\n abide eq uss , abide eq vss\n\nstabilityVar : \u2200 {\u03c4 \u0393} {x : Var \u0393 \u03c4} \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} (H : Honest \u03c1 (select-just x)) \u2192\n \u27e6 x \u27e7 (ignore \u03c1) \u2295 \u27e6 x \u27e7\u0394Var \u03c1 \u2261 \u27e6 x \u27e7 (ignore \u03c1)\n\nstabilityVar {x = this} (abide proof _) = proof\nstabilityVar {x = that y} (alter H) = stabilityVar {x = y} H\n\nstability : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} (H : Honest \u03c1 (FV t)) \u2192\n \u27e6 t \u27e7 (ignore \u03c1) \u2295 \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n \u2261 \u27e6 t \u27e7 (ignore \u03c1)\n\n-- Boilerplate begins\nstabilityAbs : \u2200 {\u03c3 \u03c4 \u0393} {t : Term (\u03c3 \u2022 \u0393) \u03c4} \u2192\n \u2200 {\u03c1 : \u0394Env \u0393} (H : Honest \u03c1 (FV (abs t))) \u2192\n (v : \u27e6 \u03c3 \u27e7) \u2192\n \u27e6 t \u27e7 (v \u2022 ignore \u03c1) \u2295\n \u27e6 derive t \u27e7\u0394 (cons v (v \u229d v) R[v,u\u229dv] \u03c1) (unrestricted t)\n \u2261 \u27e6 t \u27e7 (v \u2022 ignore \u03c1)\nstabilityAbs {t = t} {\u03c1} H v with FV t | inspect FV t\n... | abide vars | [ case0 ] = stability {t = t} Ht\n where\n Ht : Honest (cons v (v \u229d v) R[v,u\u229dv] \u03c1) (FV t)\n Ht rewrite case0 = abide v\u2295[u\u229dv]=u H\n... | alter vars | [ case1 ] = stability {t = t} Ht\n where\n Ht : Honest (cons v (v \u229d v) R[v,u\u229dv] \u03c1) (FV t)\n Ht rewrite case1 = alter H\n-- Boilerplate ends\n\nstability {t = nat n} H = refl\nstability {t = bag b} H = b++\u2205=b\nstability {t = var x} H = stabilityVar H\nstability {t = abs t} {\u03c1} H = extensionality (stabilityAbs {t = t} H)\nstability {t = app s t} {\u03c1} H =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n v = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n dv = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n Hs , Ht = proj-H H\n in\n begin\n f v \u2295 df v dv (validity {t = t})\n \u2261\u27e8 sym (corollary s t) \u27e9\n (f \u2295 df) (v \u2295 dv)\n \u2261\u27e8 stability {t = s} Hs \u27e8$\u27e9 stability {t = t} Ht \u27e9\n f v\n \u220e where open \u2261-Reasoning\nstability {t = add s t} H =\n let Hs , Ht = proj-H H\n in cong\u2082 _+_ (stability {t = s} Hs) (stability {t = t} Ht)\nstability {t = map s t} {\u03c1} H =\n let\n f = \u27e6 s \u27e7 (ignore \u03c1)\n b = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 derive s \u27e7\u0394 \u03c1 (unrestricted s)\n db = \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t)\n Hs , Ht = proj-H H\n map = mapBag\n in\n begin\n map f b \u2295 (map (f \u2295 df) (b \u2295 db) \u229d map f b)\n \u2261\u27e8 b++[d\\\\b]=d \u27e9\n map (f \u2295 df) (b \u2295 db)\n \u2261\u27e8 cong\u2082 map (stability {t = s} Hs) (stability {t = t} Ht) \u27e9\n map f b\n \u220e where open \u2261-Reasoning\n\neq-map : \u2200 {\u0393}\n (s : Term \u0393 (nats \u21d2 nats))\n (t : Term \u0393 bags)\n (\u03c1 : \u0394Env \u0393)\n (H : Honest \u03c1 (FV s)) \u2192\n \u27e6 \u0394map\u2080 s (derive s) t (derive t) \u27e7\u0394 \u03c1 (unrestricted (map s t))\n \u2261 \u27e6 \u0394map\u2081 s (derive t) \u27e7\u0394 \u03c1 (cons (unrestricted t) H tt tt)\n\neq-map s t \u03c1 H =\n let\n ds = derive s\n dt = derive t\n f = \u27e6 s \u27e7 (ignore \u03c1)\n v = \u27e6 t \u27e7 (ignore \u03c1)\n df = \u27e6 ds \u27e7\u0394 \u03c1 (unrestricted s)\n dv = \u27e6 dt \u27e7\u0394 \u03c1 (unrestricted t)\n\n eq1 : \u27e6 \u0394map\u2080 s ds t dt \u27e7\u0394 \u03c1 (unrestricted (map s t))\n \u2261 mapBag (f \u2295 df) (v \u2295 dv) \u229d mapBag f v\n eq1 = refl\n\n eq2 : mapBag (f \u2295 df) (v \u2295 dv) \u229d mapBag f v\n \u2261 mapBag f dv\n eq2 = trans\n (cong (\u03bb hole \u2192 hole \u229d mapBag f v) (trans\n (cong (\u03bb hole \u2192 mapBag hole (v \u2295 dv)) (stability {t = s} H))\n map-over-++))\n [b++d]\\\\b=d\n\n eq3 : mapBag f dv\n \u2261 \u27e6 \u0394map\u2081 s (derive t) \u27e7\u0394 \u03c1 (cons (unrestricted t) H tt tt)\n eq3 = refl\n\n in trans eq1 (trans eq2 eq3)\n\n-- Vars test\nnone-selected? : \u2200 {\u0393} \u2192 (vs : Vars \u0393) \u2192 (vs \u2261 select-none) \u228e \u22a4\nnone-selected? \u2205 = inj\u2081 refl\nnone-selected? (abide vs) = inj\u2082 tt\nnone-selected? (alter vs) with none-selected? vs\n... | inj\u2081 vs=\u2205 rewrite vs=\u2205 = inj\u2081 refl\n... | inj\u2082 _ = inj\u2082 tt\n\nclosed? : \u2200 {\u03c4 \u0393} \u2192 (t : Term \u0393 \u03c4) \u2192 (FV t \u2261 select-none) \u228e \u22a4\nclosed? t = none-selected? (FV t)\n\nvacuous-honesty : \u2200 {\u0393} {\u03c1 : \u0394Env \u0393} \u2192 Honest \u03c1 select-none\nvacuous-honesty {\u2205} {\u2205} = clearly\nvacuous-honesty {\u03c4 \u2022 \u0393} {cons _ _ _ \u03c1} = alter (vacuous-honesty {\u03c1 = \u03c1})\n\n-- Immunity of closed terms to dishonest environments\nimmune : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} \u2192\n (FV t \u2261 select-none) \u2192 \u2200 {\u03c1} \u2192 Honest \u03c1 (FV t)\nimmune {t = t} eq rewrite eq = vacuous-honesty\n\n-- Ineffectual first optimization step\nderive1 : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u0394Term \u0393 \u03c4\nderive1 (map s t) with closed? s\n... | inj\u2081 is-closed = \u0394map\u2081 s (derive t)\n... | inj\u2082 tt = derive (map s t)\nderive1 others = derive others\n\nvalid1 : \u2200 {\u03c4 \u0393} (t : Term \u0393 \u03c4) {\u03c1 : \u0394Env \u0393} \u2192 derive1 t is-valid-for \u03c1\nvalid1 (nat n) = tt\nvalid1 (bag b) = tt\nvalid1 (var x) = tt\nvalid1 (abs t) = \u03bb _ _ _ \u2192 unrestricted t\nvalid1 (app s t) =\n cons (unrestricted s) (unrestricted t) (validity {t = t}) tt\nvalid1 (add s t) =\n cons (unrestricted s) (unrestricted t) tt tt\nvalid1 (map s t) {\u03c1} with closed? s\n... | inj\u2082 tt = cons (unrestricted s) (unrestricted t) tt tt\n... | inj\u2081 if-closed =\n cons (unrestricted t) (immune {t = s} if-closed) tt tt\n\ncorrect1 : \u2200 {\u03c4 \u0393} {t : Term \u0393 \u03c4} {\u03c1 : \u0394Env \u0393} \u2192\n \u27e6 derive t \u27e7\u0394 \u03c1 (unrestricted t) \u2261 \u27e6 derive1 t \u27e7\u0394 \u03c1 (valid1 t)\n\ncorrect1 {t = nat n} = refl\ncorrect1 {t = bag b} = refl\ncorrect1 {t = var x} = refl\ncorrect1 {t = abs t} = refl\ncorrect1 {t = app s t} = refl\ncorrect1 {t = add s t} = refl\ncorrect1 {t = map s t} {\u03c1} with closed? s\n... | inj\u2082 tt = refl\n... | inj\u2081 if-closed = eq-map s t \u03c1 (immune {t = s} if-closed)\n\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a69042e1203e1b0da55918e3c8130ee69193e159","subject":"Boring.","message":"Boring.\n\nIgnore-this: c710f736c882ebdb2b00863199903eef\n\ndarcs-hash:20100412130736-3bd4e-4a089904a683c9be05ed341034af47ec1e08c49c.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Test\/Succeed\/Names.agda","new_file":"Test\/Succeed\/Names.agda","new_contents":"------------------------------------------------------------------------------\n-- Testing the translation of function, predicates and variables names\n------------------------------------------------------------------------------\n\n-- From the technical manual of TPTP\n-- (http:\/\/www.cs.miami.edu\/~tptp\/TPTP\/TR\/TPTPTR.shtml)\n\n-- ... variables start with upper case letters, ... predicates and\n-- functors either start with lower case and contain alphanumerics and\n-- underscore ...\n\nmodule Test.Succeed.Names where\n\ninfix 4 _\u2261_\n\npostulate\n D : Set\n _\u2261_ : D \u2192 D \u2192 Set\n\n-- A funny function name.\npostulate\n FUN! : D \u2192 D\n\npostulate\n -- Using a funny function and variable name\n a\u2081 : (nx\u220e : D) \u2192 FUN! nx\u220e \u2261 nx\u220e\n{-# ATP axiom a\u2081 #-}\n\npostulate\n foo : (n : D) \u2192 FUN! n \u2261 n\n{-# ATP prove foo #-}\n\n-- A funny predicate name\ndata PRED! : D \u2192 Set where\n\npostulate\n a\u2082 : (n : D) \u2192 PRED! n\n{-# ATP axiom a\u2082 #-}\n\npostulate\n bar : (n : D) \u2192 PRED! n\n{-# ATP prove bar #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Testing the translation of function, predicates and variables names\n------------------------------------------------------------------------------\n\n-- From the technical manual of TPTP\n-- http:\/\/www.cs.miami.edu\/~tptp\/TPTP\/TR\/TPTPTR.shtml\n\n-- variables start with upper case letters, ... predicates and\n-- functors either start with lower case and contain alphanumerics and\n-- underscore ...\n\nmodule Test.Succeed.Names where\n\ninfix 4 _\u2261_\n\npostulate\n D : Set\n _\u2261_ : D \u2192 D \u2192 Set\n\n-- A funny function name.\npostulate\n FUN! : D \u2192 D\n\npostulate\n -- Using a funny function and variable name\n a\u2081 : (nx\u220e : D) \u2192 FUN! nx\u220e \u2261 nx\u220e\n{-# ATP axiom a\u2081 #-}\n\npostulate\n foo : (n : D) \u2192 FUN! n \u2261 n\n{-# ATP prove foo #-}\n\n-- A funny predicate name\ndata PRED! : D \u2192 Set where\n\npostulate\n a\u2082 : (n : D) \u2192 PRED! n\n{-# ATP axiom a\u2082 #-}\n\npostulate\n bar : (n : D) \u2192 PRED! n\n{-# ATP prove bar #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"eb9b76a2590365332ba7634aba748ff2018711d2","subject":"Boring.","message":"Boring.\n\nIgnore-this: afd837a76a16677be82e439fbd3f3052\n\ndarcs-hash:20100812142917-3bd4e-3afcc41d73968067a6626fddc35217ec3888a7a8.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"LTC\/Minimal.agda","new_file":"LTC\/Minimal.agda","new_contents":"------------------------------------------------------------------------------\n-- Agda as a logical framework for LTC\n------------------------------------------------------------------------------\n{-\n\nLTC Agda\n* Logical constants * Curry-Howard isomorphism\n* Equality * Identity type\n* Term language * Postulates\n* Inductive predicates * Inductive families\n-}\n\nmodule LTC.Minimal where\n\ninfixl 6 _\u2219_\ninfix 5 if_then_else_\ninfix 4 _\u2261_\n\n------------------------------------------------------------------------------\n-- The universal domain.\n-- N.B. The following module is exported by this module.\nopen import LTC.Minimal.Core public\n\n------------------------------------------------------------------------------\n-- The term language of LTC correspond to the PCF terms\n\n-- t ::= x | t t | \\x -> t\n-- | true | false | if t then t else t\n-- | 0 | succ t | pred t | isZero t\n-- | error\n-- | fix t\n\npostulate\n\n -- LTC partial booleans.\n true : D\n false : D\n if_then_else_ : D \u2192 D \u2192 D \u2192 D\n -- LTC partial natural numbers.\n zero : D\n succ : D \u2192 D\n pred : D \u2192 D\n isZero : D \u2192 D\n -- LTC abstraction.\n lam : (D \u2192 D) \u2192 D\n -- LTC application\n -- Left associative aplication operator\n -- The Agda application has higher precedence level than LTC application\n _\u2219_ : D \u2192 D \u2192 D\n -- LTC error\n error : D\n -- LTC fixed point operator\n fix : (D \u2192 D) \u2192 D\n -- fixFO : D\n\n------------------------------------------------------------------------------\n-- The LTC's equality is the propositional identity on 'D'.\n\n-- The identity type on D.\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n-- Identity properties.\n\n-- The substitution is defined in LTC.MinimalER\n\nsym : {x y : D} \u2192 x \u2261 y \u2192 y \u2261 x\nsym refl = refl\n\ntrans : {x y z : D} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\ntrans refl y\u2261z = y\u2261z\n\n------------------------------------------------------------------------------\n-- Logical constants: Curry-Howard isomorphism\n\n-- The LTC's logical constants are the type theory's logical\n-- constants via the Curry-Howard isomorphism.\n-- For the implication and the universal quantifier\n-- we use Agda (dependent) function type.\n\n-- N.B. The following modules are exported by this module.\nopen import LTC.Data.Empty public\nopen import LTC.Data.Product public\nopen import LTC.Data.Sum public\nopen import LTC.Data.Unit public\nopen import LTC.Relation.Nullary public\n\n------------------------------------------------------------------------------\n-- Conversion rules\n\npostulate\n -- Conversion rules for booleans.\n cB\u2081 : (d\u2081 : D){d\u2082 : D} \u2192 if true then d\u2081 else d\u2082 \u2261 d\u2081\n cB\u2082 : {d\u2081 : D}(d\u2082 : D) \u2192 if false then d\u2081 else d\u2082 \u2261 d\u2082\n{-# ATP axiom cB\u2081 #-}\n{-# ATP axiom cB\u2082 #-}\n\npostulate\n -- Conversion rules for pred.\n cP\u2081 : pred zero \u2261 zero\n cP\u2082 : (n : D) \u2192 pred (succ n) \u2261 n\n{-# ATP axiom cP\u2081 #-}\n{-# ATP axiom cP\u2082 #-}\n\npostulate\n -- Conversion rules for isZero\n cZ\u2081 : isZero zero \u2261 true\n cZ\u2082 : (n : D) \u2192 isZero (succ n) \u2261 false\n{-# ATP axiom cZ\u2081 #-}\n{-# ATP axiom cZ\u2082 #-}\n\npostulate\n -- Conversion rule for the abstraction and the application.\n cBeta : (f : D \u2192 D) \u2192 (a : D) \u2192 (lam f) \u2219 a \u2261 f a\n{-# ATP axiom cBeta #-}\n\npostulate\n -- Conversion rule for the fixed pointed operator.\n cFix : (f : D \u2192 D) \u2192 fix f \u2261 f (fix f)\n -- cFixFO : (f : D) \u2192 fixFO \u2219 f \u2261 f \u2219 (fixFO \u2219 f)\n{-# ATP axiom cFix #-}\n\n------------------------------------------------------------------------------\n-- Discrimination rules\n\npostulate\n true\u2260false : \u00ac (true \u2261 false)\n 0\u2260S : {d : D} \u2192 \u00ac (zero \u2261 succ d)\n{-# ATP axiom true\u2260false #-}\n{-# ATP axiom 0\u2260S #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Agda as a logical framework for LTC\n------------------------------------------------------------------------------\n{-\n\nLTC Agda\n* Logical constants * Curry-Howard isomorphism\n* Equality * Identity type\n* Term language * Postulates\n* Inductive predicates * Inductive families\n-}\n\nmodule LTC.Minimal where\n\ninfixl 6 _\u2219_\ninfix 5 if_then_else_\ninfix 4 _\u2261_\n\n------------------------------------------------------------------------------\n-- The universal domain.\n-- N.B. The following module is exported by this module.\nopen import LTC.Minimal.Core public\n\n------------------------------------------------------------------------------\n-- The term language of LTC correspond to the PCF terms\n\n-- t ::= x | t t | \\x -> t\n-- | true | false | if t then t else t\n-- | 0 | succ t | pred t | isZero t\n-- | error\n-- | fix t\n\npostulate\n\n -- LTC partial booleans.\n true : D\n false : D\n if_then_else_ : D \u2192 D \u2192 D \u2192 D\n -- LTC partial natural numbers.\n zero : D\n succ : D \u2192 D\n pred : D \u2192 D\n isZero : D \u2192 D\n -- LTC abstraction.\n lam : (D \u2192 D) \u2192 D\n -- LTC application\n -- Left associative aplication operator\n -- The Agda application has higher precedence level than LTC application\n _\u2219_ : D \u2192 D \u2192 D\n -- LTC error\n error : D\n -- LTC fixed point operator\n fix : (D \u2192 D) \u2192 D\n -- fixFO : D\n\n------------------------------------------------------------------------------\n-- The LTC's equality is the propositional identity on 'D'.\n\n-- The identity type.\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n-- Identity properties.\n\n-- The substitution is defined in LTC.MinimalER\n\nsym : {x y : D} \u2192 x \u2261 y \u2192 y \u2261 x\nsym refl = refl\n\ntrans : {x y z : D} \u2192 x \u2261 y \u2192 y \u2261 z \u2192 x \u2261 z\ntrans refl y\u2261z = y\u2261z\n\n------------------------------------------------------------------------------\n-- Logical constants: Curry-Howard isomorphism\n\n-- The LTC's logical constants are the type theory's logical\n-- constants via the Curry-Howard isomorphism.\n-- For the implication and the universal quantifier\n-- we use Agda (dependent) function type.\n\n-- N.B. The following modules are exported by this module.\nopen import LTC.Data.Empty public\nopen import LTC.Data.Product public\nopen import LTC.Data.Sum public\nopen import LTC.Data.Unit public\nopen import LTC.Relation.Nullary public\n\n------------------------------------------------------------------------------\n-- Conversion rules\n\npostulate\n -- Conversion rules for booleans.\n cB\u2081 : (d\u2081 : D){d\u2082 : D} \u2192 if true then d\u2081 else d\u2082 \u2261 d\u2081\n cB\u2082 : {d\u2081 : D}(d\u2082 : D) \u2192 if false then d\u2081 else d\u2082 \u2261 d\u2082\n{-# ATP axiom cB\u2081 #-}\n{-# ATP axiom cB\u2082 #-}\n\npostulate\n -- Conversion rules for pred.\n cP\u2081 : pred zero \u2261 zero\n cP\u2082 : (n : D) \u2192 pred (succ n) \u2261 n\n{-# ATP axiom cP\u2081 #-}\n{-# ATP axiom cP\u2082 #-}\n\npostulate\n -- Conversion rules for isZero\n cZ\u2081 : isZero zero \u2261 true\n cZ\u2082 : (n : D) \u2192 isZero (succ n) \u2261 false\n{-# ATP axiom cZ\u2081 #-}\n{-# ATP axiom cZ\u2082 #-}\n\npostulate\n -- Conversion rule for the abstraction and the application.\n cBeta : (f : D \u2192 D) \u2192 (a : D) \u2192 (lam f) \u2219 a \u2261 f a\n{-# ATP axiom cBeta #-}\n\npostulate\n -- Conversion rule for the fixed pointed operator.\n cFix : (f : D \u2192 D) \u2192 fix f \u2261 f (fix f)\n -- cFixFO : (f : D) \u2192 fixFO \u2219 f \u2261 f \u2219 (fixFO \u2219 f)\n{-# ATP axiom cFix #-}\n\n------------------------------------------------------------------------------\n-- Discrimination rules\n\npostulate\n true\u2260false : \u00ac (true \u2261 false)\n 0\u2260S : {d : D} \u2192 \u00ac (zero \u2261 succ d)\n{-# ATP axiom true\u2260false #-}\n{-# ATP axiom 0\u2260S #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"8c57726522691c1d75ef56448841051067ea72e4","subject":"Added missing --without-K option","message":"Added missing --without-K option\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/FOT\/FOL\/ImplicitArgumentSubst.agda","new_file":"notes\/FOT\/FOL\/ImplicitArgumentSubst.agda","new_contents":"------------------------------------------------------------------------------\n-- Testing subst using an implicit arguments for the propositional function.\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-sized-types #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --schematic-propositional-functions #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOL.ImplicitArgumentSubst where\n\ninfix 7 _\u2261_\n\npostulate\n D : Set\n _\u00b7_ : D \u2192 D \u2192 D\n zero succ pred : D\n\nsucc\u2081 : D \u2192 D\nsucc\u2081 n = succ \u00b7 n\n\npred\u2081 : D \u2192 D\npred\u2081 n = pred \u00b7 n\n\n-- The identity type on the universe of discourse.\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n-- The propositional function is not an implicit argument.\nsubst : (A : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\nsubst A refl Ax = Ax\n\n-- The propositional formula is an implicit argument.\nsubst' : {A : D \u2192 Set} \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\nsubst' refl Ax = Ax\n\nsym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\nsym refl = refl\n\n-- Conversion rules.\npostulate\n pred-0 : pred\u2081 zero \u2261 zero\n pred-S : \u2200 n \u2192 pred\u2081 (succ\u2081 n) \u2261 n\n\n-- The FOTC natural numbers type.\ndata N : D \u2192 Set where\n nzero : N zero\n nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n\n-- Works using subst.\npred-N : \u2200 {n} \u2192 N n \u2192 N (pred\u2081 n)\npred-N nzero = subst N (sym pred-0) nzero\npred-N (nsucc {n} Nn) = subst N (sym (pred-S n)) Nn\n\n-- Fails using subst'.\npred-N' : \u2200 {n} \u2192 N n \u2192 N (pred\u2081 n)\npred-N' nzero = subst' (sym pred-0) nzero\npred-N' (nsucc {n} Nn) = subst' (sym (pred-S n)) Nn\n","old_contents":"------------------------------------------------------------------------------\n-- Testing subst using an implicit arguments for the propositional function.\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-sized-types #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --schematic-propositional-functions #-}\n\nmodule FOT.FOL.ImplicitArgumentSubst where\n\ninfix 7 _\u2261_\n\npostulate\n D : Set\n _\u00b7_ : D \u2192 D \u2192 D\n zero succ pred : D\n\nsucc\u2081 : D \u2192 D\nsucc\u2081 n = succ \u00b7 n\n\npred\u2081 : D \u2192 D\npred\u2081 n = pred \u00b7 n\n\n-- The identity type on the universe of discourse.\ndata _\u2261_ (x : D) : D \u2192 Set where\n refl : x \u2261 x\n\n-- The propositional function is not an implicit argument.\nsubst : (A : D \u2192 Set) \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\nsubst A refl Ax = Ax\n\n-- The propositional formula is an implicit argument.\nsubst' : {A : D \u2192 Set} \u2192 \u2200 {x y} \u2192 x \u2261 y \u2192 A x \u2192 A y\nsubst' refl Ax = Ax\n\nsym : \u2200 {x y} \u2192 x \u2261 y \u2192 y \u2261 x\nsym refl = refl\n\n-- Conversion rules.\npostulate\n pred-0 : pred\u2081 zero \u2261 zero\n pred-S : \u2200 n \u2192 pred\u2081 (succ\u2081 n) \u2261 n\n\n-- The FOTC natural numbers type.\ndata N : D \u2192 Set where\n nzero : N zero\n nsucc : \u2200 {n} \u2192 N n \u2192 N (succ\u2081 n)\n\n-- Works using subst.\npred-N : \u2200 {n} \u2192 N n \u2192 N (pred\u2081 n)\npred-N nzero = subst N (sym pred-0) nzero\npred-N (nsucc {n} Nn) = subst N (sym (pred-S n)) Nn\n\n-- Fails using subst'.\npred-N' : \u2200 {n} \u2192 N n \u2192 N (pred\u2081 n)\npred-N' nzero = subst' (sym pred-0) nzero\npred-N' (nsucc {n} Nn) = subst' (sym (pred-S n)) Nn\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f0ce38074cd7a2b8fbc37eacf555d188fe888e27","subject":"some stuff now works...possibly subject expansion","message":"some stuff now works...possibly subject expansion\n","repos":"goodlyrottenapple\/lamYcalc","old_file":"Agda\/ITyping.agda","new_file":"Agda\/ITyping.agda","new_contents":"module ITyping where\n\nopen import Data.Empty\nopen import Data.List\nopen import Data.Nat\nopen import Data.Product\n-- open import Data.Maybe\nopen import Data.List.Any as LAny\nopen LAny.Membership-\u2261\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary.Core\n\n\nopen import Core\nopen import Core-Lemmas\nopen import Typing\nopen import Reduction\n\n\ndata IType : Set where\n o : IType\n _~>_ : IType -> IType -> IType\n \u2229 : List IType -> IType\n\n\u03c9 = \u2229 []\n\n\u2229' : IType -> IType\n\u2229' x = \u2229 (x \u2237 [])\n\n~>-inj-l : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2261 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) -> \u03c4\u2081\u2081 \u2261 \u03c4\u2082\u2081\n~>-inj-l refl = refl\n\n~>-inj-r : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2261 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) -> \u03c4\u2081\u2082 \u2261 \u03c4\u2082\u2082\n~>-inj-r refl = refl\n\n\u2229-inj-cons : \u2200 {x y \u03c4\u1d62 \u03c4\u2c7c} -> \u2229 (x \u2237 \u03c4\u1d62) \u2261 \u2229 (y \u2237 \u03c4\u2c7c) -> \u2229 \u03c4\u1d62 \u2261 \u2229 \u03c4\u2c7c\n\u2229-inj-cons refl = refl\n\n\u2229-inj : \u2200 {x y \u03c4\u1d62 \u03c4\u2c7c} -> \u2229 (x \u2237 \u03c4\u1d62) \u2261 \u2229 (y \u2237 \u03c4\u2c7c) -> x \u2261 y\n\u2229-inj refl = refl\n\n\n_\u225fTI_ : Decidable {A = IType} _\u2261_\no \u225fTI o = yes refl\no \u225fTI (_ ~> _) = no (\u03bb ())\no \u225fTI (\u2229 _) = no (\u03bb ())\n\n(_ ~> _) \u225fTI o = no (\u03bb ())\n(_ ~> _) \u225fTI (\u2229 _) = no (\u03bb ())\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) with \u03c4\u2081\u2081 \u225fTI \u03c4\u2082\u2081 | \u03c4\u2081\u2082 \u225fTI \u03c4\u2082\u2082\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (.\u03c4\u2081\u2081 ~> .\u03c4\u2081\u2082) | yes refl | yes refl = yes refl\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (.\u03c4\u2081\u2081 ~> \u03c4\u2082\u2082) | yes refl | no \u03c4\u2081\u2082\u2260\u03c4\u2082\u2082 = no (\u03bb eq \u2192 \u03c4\u2081\u2082\u2260\u03c4\u2082\u2082 (~>-inj-r eq))\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) | no \u03c4\u2081\u2081\u2260\u03c4\u2082\u2081 | _ = no (\u03bb eq \u2192 \u03c4\u2081\u2081\u2260\u03c4\u2082\u2081 (~>-inj-l eq))\n\n(\u2229 _) \u225fTI o = no (\u03bb ())\n(\u2229 _) \u225fTI (_ ~> _) = no (\u03bb ())\n\u2229 [] \u225fTI \u2229 [] = yes refl\n\u2229 [] \u225fTI \u2229 (x \u2237 \u03c4\u2c7c) = no (\u03bb ())\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 [] = no (\u03bb ())\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (y \u2237 \u03c4\u2c7c) with x \u225fTI y | (\u2229 \u03c4\u1d62) \u225fTI (\u2229 \u03c4\u2c7c)\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (.x \u2237 .\u03c4\u1d62) | yes refl | yes refl = yes refl\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (.x \u2237 \u03c4\u2c7c) | yes refl | no \u03c4\u1d62\u2260\u03c4\u2c7c = no (\u03bb \u2229x\u2237\u03c4\u1d62\u2261\u2229x\u2237\u03c4\u2c7c \u2192 \u03c4\u1d62\u2260\u03c4\u2c7c (\u2229-inj-cons \u2229x\u2237\u03c4\u1d62\u2261\u2229x\u2237\u03c4\u2c7c))\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (y \u2237 \u03c4\u2c7c) | no x\u2260y | _ = no (\u03bb \u2229x\u2237\u03c4\u1d62\u2261\u2229y\u2237\u03c4\u2c7c \u2192 x\u2260y (\u2229-inj \u2229x\u2237\u03c4\u1d62\u2261\u2229y\u2237\u03c4\u2c7c))\n\n\n\nICtxt = List (Atom \u00d7 IType)\n\n\ndata Wf-ICtxt : ICtxt -> Set where\n nil : Wf-ICtxt []\n cons : \u2200 {\u0393 x \u03c4} -> (x\u2209 : x \u2209 dom \u0393) -> Wf-ICtxt \u0393 ->\n Wf-ICtxt ((x , \u03c4) \u2237 \u0393)\n\n\ndata _\u2237'_ : IType -> Type -> Set where\n base : o \u2237' \u03c3\n arr : \u2200 {\u03b4 \u03c4 A B} -> \u03b4 \u2237' A -> \u03c4 \u2237' B -> (\u03b4 ~> \u03c4) \u2237' (A \u27f6 B)\n \u2229-nil : \u2200 {A} -> \u03c9 \u2237' A\n \u2229-cons : \u2200 {\u03c4\u1d62 \u03c4 A} -> \u03c4 \u2237' A -> \u2229 \u03c4\u1d62 \u2237' A -> \u2229 (\u03c4 \u2237 \u03c4\u1d62) \u2237' A\n\n\ndata _\u2264\u2229_ : IType -> IType -> Set where\n base : o \u2264\u2229 o\n arr : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> \u03c4\u2081\u2082 \u2264\u2229 \u03c4\u2082\u2082 -> \u03c4\u2082\u2081 \u2264\u2229 \u03c4\u2081\u2081 -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2264\u2229 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082)\n \u2229-\u2208 : \u2200 {\u03c4 \u03c4\u1d62} -> \u03c4 \u2208 \u03c4\u1d62 -> \u2229 \u03c4\u1d62 \u2264\u2229 \u03c4\n \u2229-nil : \u2200 {\u03c4} -> \u03c4 \u2264\u2229 \u03c9\n \u2229-cons : \u2200 {\u03c4 \u03c4' \u03c4\u1d62} -> \u03c4 \u2264\u2229 \u03c4' -> \u03c4 \u2264\u2229 \u2229 \u03c4\u1d62 -> \u03c4 \u2264\u2229 \u2229 (\u03c4' \u2237 \u03c4\u1d62)\n -- \u2229-trans : \u2200 {\u03c4\u2081 \u03c4\u2082 \u03c4\u2083} -> \u03c4\u2081 \u2264\u2229 \u03c4\u2082 -> \u03c4\u2082 \u2264\u2229 \u03c4\u2083 -> \u03c4\u2081 \u2264\u2229 \u03c4\u2083\n\n\n\u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 : \u2200 {\u03c4\u1d62 \u03c4\u2c7c} -> \u03c4\u1d62 \u2286 \u03c4\u2c7c -> \u2229 \u03c4\u2c7c \u2264\u2229 \u2229 \u03c4\u1d62\n\u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 {[]} \u03c4\u1d62\u2286\u03c4\u2c7c = \u2229-nil\n\u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 {x \u2237 \u03c4\u1d62} \u03c4\u1d62\u2286\u03c4\u2c7c = \u2229-cons (\u2229-\u2208 (\u03c4\u1d62\u2286\u03c4\u2c7c (here refl))) (\u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 (\u03bb {x\u2081} z \u2192 \u03c4\u1d62\u2286\u03c4\u2c7c (there z)))\n\n\u2264\u2229-refl : \u2200 {\u03c4} -> \u03c4 \u2264\u2229 \u03c4\n\u2264\u2229-refl {o} = base\n\u2264\u2229-refl {\u03c4 ~> \u03c4\u2081} = arr \u2264\u2229-refl \u2264\u2229-refl\n\u2264\u2229-refl {\u2229 []} = \u2229-nil\n\u2264\u2229-refl {\u2229 (x \u2237 x\u2081)} = \u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 (\u03bb {x\u2082} z \u2192 z)\n\n\ndata \u039b : Type -> Set where\n bv : \u2200 {A} -> (i : \u2115) -> \u039b A\n fv : \u2200 {A} -> (x : Atom) -> \u039b A\n lam : \u2200 {B} -> (A : Type) -> (e : \u039b B) -> \u039b (A \u27f6 B)\n app : \u2200 {A B} -> (e\u2081 : \u039b (A \u27f6 B)) -> (e\u2082 : \u039b A) -> \u039b B\n Y : (t : Type) -> \u039b ((t \u27f6 t) \u27f6 t)\n\n\n-- _\u2208?_ : \u2200 {a} {A : Set a} -> Atom -> List (Atom \u00d7 A) -> Maybe A\n-- a \u2208? [] = nothing\n-- a \u2208? (l \u2237 ist) with a \u225f proj\u2081 l\n-- ... | yes _ = just (proj\u2082 l)\n-- a \u2208? (l \u2237 ist) | no _ = a \u2208? ist\n\n-- \u22a2->\u039b : \u2200 {\u0393 m t} -> (List (Atom \u00d7 \u2115)) -> \u0393 \u22a2 m \u2236 t -> \u039b t\n-- \u22a2->\u039b {m = bv i} _ ()\n-- \u22a2->\u039b {m = fv x} bound \u0393\u22a2m\u2236t with x \u2208? bound\n-- \u22a2->\u039b {m = fv x} {t} bound \u0393\u22a2m\u2236t | just i = bv {t} i\n-- \u22a2->\u039b {m = fv x} bound \u0393\u22a2m\u2236t | nothing = fv x\n-- \u22a2->\u039b {m = lam m} bound (abs {_} {\u03c4\u2081} {\u03c4\u2082} L cf) = lam \u03c4\u2081 (\u22a2->\u039b ((x , 0) \u2237 bound') (cf (\u2209-cons-l _ _ x\u2209)))\n-- where\n-- x = \u2203fresh (L ++ FV m)\n-- x\u2209 : x \u2209 (L ++ FV m)\n-- x\u2209 = \u2203fresh-spec (L ++ FV m)\n--\n-- bound' : List (Atom \u00d7 \u2115)\n-- bound' = Data.List.map (\u03bb a,i \u2192 (proj\u2081 a,i) , suc (proj\u2082 a,i)) bound\n--\n-- \u22a2->\u039b {m = app t1 t} bound (app \u0393\u22a2s \u0393\u22a2t) = app (\u22a2->\u039b bound \u0393\u22a2s) (\u22a2->\u039b bound \u0393\u22a2t)\n-- \u22a2->\u039b {m = Y \u03c4} bound (Y x) = Y \u03c4\n--\n-- \u22a2->\u039b : \u2200 {\u0393 m t} -> \u0393 \u22a2 m \u2236 t -> \u039b t\n-- \u22a2->\u039b = \u22a2->\u039b []\n\ndata _~_ : \u2200{t} -> \u039b t -> PTerm -> Set where\n bv : \u2200 {t i} -> (bv {t} i) ~ (bv i)\n fv : \u2200 {t x} -> (fv {t} x) ~ (fv x)\n lam : \u2200 {t s m m'} -> m ~ m' -> (lam {s} t m) ~ (lam m')\n app : \u2200 {t s m n m' n'} -> m ~ m' -> n ~ n' -> (app {t} {s} m n) ~ (app m' n')\n Y : \u2200 {t} -> (Y t) ~ (Y t)\n\n\u039b[_<<_] : \u2200 {t} -> \u2115 -> Atom -> \u039b t -> \u039b t\n\u039b[ k << x ] (bv i) = bv i\n\u039b[ k << x ] (fv y) with x \u225f y\n... | yes _ = bv k\n... | no _ = fv y\n\u039b[ k << x ] (lam t m) = lam t (\u039b[ (suc k) << x ] m)\n\u039b[ k << x ] (app t1 t2) = app (\u039b[ k << x ] t1) (\u039b[ k << x ] t2)\n\u039b[ k << x ] (Y t) = Y t\n\n\n\n\u22a2->\u039b : \u2200 {\u0393 m t} -> \u0393 \u22a2 m \u2236 t -> \u039b t\n\u22a2->\u039b {m = bv i} ()\n\u22a2->\u039b {m = fv x} {t} \u0393\u22a2m\u2236t = fv {t} x\n\u22a2->\u039b {m = lam m} (abs {_} {\u03c4\u2081} {\u03c4\u2082} L cf) =\n lam \u03c4\u2081 ( \u039b[ 0 << \u2203fresh (L ++ FV m) ] (\u22a2->\u039b (cf (\u2209-cons-l _ _ (\u2203fresh-spec (L ++ FV m)) ))) )\n\u22a2->\u039b {m = app t1 t} (app \u0393\u22a2s \u0393\u22a2t) = app (\u22a2->\u039b \u0393\u22a2s) (\u22a2->\u039b \u0393\u22a2t)\n\u22a2->\u039b {m = Y \u03c4} (Y x) = Y \u03c4\n\n\u22a2->\u039b\u2261 : \u2200 {\u0393 m n t} -> m \u2261 n -> {\u0393\u22a2m : \u0393 \u22a2 m \u2236 t} -> {\u0393\u22a2n : \u0393 \u22a2 n \u2236 t} -> (\u22a2->\u039b \u0393\u22a2m) \u2261 (\u22a2->\u039b \u0393\u22a2m)\n\u22a2->\u039b\u2261 refl = \u03bb {\u0393\u22a2m} {\u0393\u22a2n} \u2192 refl\n\n\n\u039b*^-*^~ : \u2200 {\u03c4 x k} t t' -> _~_ {\u03c4} t t' -> \u039b[ k << x ] t ~ ([ k << x ] t')\n\u039b*^-*^~ _ _ bv = bv\n\u039b*^-*^~ {x = x} (fv y) _ fv with x \u225f y\n\u039b*^-*^~ (fv x) .(fv x) fv | yes _ = bv\n\u039b*^-*^~ (fv y) .(fv y) fv | no _ = fv\n\u039b*^-*^~ _ _ (lam {m = m} {m'} t~t') = lam (\u039b*^-*^~ m m' t~t')\n\u039b*^-*^~ _ _ (app {m = m} {n} {m'} {n'} t~t' t~t'') = app (\u039b*^-*^~ m m' t~t') (\u039b*^-*^~ n n' t~t'')\n\u039b*^-*^~ _ _ Y = Y\n\n\n\u22a2->\u039b~ : \u2200 {\u0393 t \u03c4} -> (\u0393\u22a2t : \u0393 \u22a2 t \u2236 \u03c4) -> (\u22a2->\u039b \u0393\u22a2t) ~ t\n\u22a2->\u039b~ {t = bv i} ()\n\u22a2->\u039b~ {t = fv x} (var _ _) = fv\n\u22a2->\u039b~ {t = lam t} (abs {\u03c4\u2081 = \u03c4\u2081} {\u03c4\u2082} L cf) = lam ih\n where\n x = \u2203fresh (L ++ FV t)\n x\u2209 = \u2203fresh-spec (L ++ FV t)\n x\u2237\u0393\u22a2t^'x = cf (\u2209-cons-l _ _ x\u2209)\n\n sub : \u2200 {\u03c4 x m} -> x \u2209 FV t -> _~_ {\u03c4} m t \u2261 m ~ (* x ^ (t ^' x))\n sub {_} {x} x\u2209 rewrite fv-^-*^-refl x t {0} x\u2209 = refl\n\n ih : \u039b[ 0 << x ] (\u22a2->\u039b x\u2237\u0393\u22a2t^'x) ~ t\n ih rewrite sub {_} {x} {\u039b[ 0 << x ] (\u22a2->\u039b x\u2237\u0393\u22a2t^'x)} (\u2209-cons-r L _ x\u2209) =\n \u039b*^-*^~ (\u22a2->\u039b x\u2237\u0393\u22a2t^'x) (t ^' x) (\u22a2->\u039b~ (cf (\u2209-cons-l _ _ x\u2209)))\n\u22a2->\u039b~ {t = app t t\u2081} (app \u0393\u22a2t \u0393\u22a2t\u2081) = app (\u22a2->\u039b~ \u0393\u22a2t) (\u22a2->\u039b~ \u0393\u22a2t\u2081)\n\u22a2->\u039b~ {t = Y t\u2081} (Y x) = Y\n\n\n-- ers : \u2200 {t} -> \u039b t -> PTerm\n-- ers (bv i) = bv i\n-- ers (fv x) = fv x\n-- ers (lam A \u039bt) = lam (ers \u039bt)\n-- ers (app \u039bs \u039bt) = app (ers \u039bs) (ers \u039bt)\n-- ers (Y t) = Y t\n\n\n-- data IType\u209b : IType -> Set where\n-- o : IType\u209b o\n-- arr : \u2200 {\u03c4 \u03c4'} -> IType\u209b \u03c4 -> IType\u209b \u03c4' -> IType\u209b (\u03c4 ~> \u03c4')\n--\n-- data IType\u209b\u209b : IType -> Set where\n-- o : IType\u209b\u209b o\n-- arr : \u2200 {\u03c4 \u03c4'} -> IType\u209b\u209b \u03c4 -> IType\u209b\u209b \u03c4' -> IType\u209b\u209b (\u03c4 ~> \u03c4')\n-- \u2229-nil : IType\u209b\u209b \u03c9\n-- \u2229-cons : \u2200 {\u03c4 \u03c4\u1d62} -> IType\u209b \u03c4 -> IType\u209b\u209b (\u2229 \u03c4\u1d62) -> IType\u209b\u209b (\u2229 (\u03c4 \u2237 \u03c4\u1d62))\n--\n-- \u03c4\u209b->\u03c4\u209b\u209b : \u2200 {\u03c4} -> IType\u209b \u03c4 -> IType\u209b\u209b \u03c4\n-- \u03c4\u209b->\u03c4\u209b\u209b o = o\n-- \u03c4\u209b->\u03c4\u209b\u209b (arr \u03c4\u209b \u03c4\u209b\u2081) = arr (\u03c4\u209b->\u03c4\u209b\u209b \u03c4\u209b) (\u03c4\u209b->\u03c4\u209b\u209b \u03c4\u209b\u2081)\n\n\n\u039b[_>>_] : \u2200 {\u03c4 \u03c4'} -> \u2115 -> \u039b \u03c4' -> \u039b \u03c4 -> \u039b \u03c4\n\u039b[_>>_] {\u03c4} {\u03c4'} k u (bv i) with k \u225f i | \u03c4 \u225fT \u03c4'\n\u039b[ k >> u ] (bv i) | yes _ | yes refl = u\n... | yes _ | no _ = bv i\n... | no _ | _ = bv i\n\u039b[ k >> u ] (fv x) = fv x\n\u039b[ k >> u ] (lam A t) = lam A (\u039b[ (suc k) >> u ] t)\n\u039b[ k >> u ] (app t1 t2) = app (\u039b[ k >> u ] t1) (\u039b[ k >> u ] t2)\n\u039b[ k >> u ] (Y t) = Y t\n\n\ndata Y-shape : \u2200 {\u03c4} -> \u039b \u03c4 -> Set where\n intro\u2081 : \u2200 {A m} -> Y-shape (app (Y A) m)\n intro\u2082 : \u2200 {A m} -> Y-shape (app m (app (Y A) m))\n\ndata _\u22a9_\u2236_ : \u2200 {A} -> ICtxt -> \u039b A -> IType -> Set where\n var : \u2200 {A \u0393 x \u03c4} {\u03c4\u1d62 : List IType} -> (wf-\u0393 : Wf-ICtxt \u0393) -> (\u03c4\u1d62\u2208\u0393 : (x , (\u2229 \u03c4\u1d62)) \u2208 \u0393) -> (\u03c4\u1d62\u2264\u2229\u03c4 : \u2229 \u03c4\u1d62 \u2264\u2229 \u03c4) ->\n \u03c4 \u2237' A -> \u0393 \u22a9 fv {A} x \u2236 \u03c4\n app : \u2200 {A B \u0393 s t \u03c4\u2081 \u03c4\u2082} -> \u0393 \u22a9 s \u2236 (\u03c4\u2081 ~> \u03c4\u2082) -> \u0393 \u22a9 t \u2236 \u03c4\u2081 -> (\u03c4\u2081 ~> \u03c4\u2082) \u2237' (A \u27f6 B) -> \u03c4\u2081 \u2237' A ->\n \u0393 \u22a9 (app {A} {B} s t) \u2236 \u03c4\u2082\n \u2229-nil : \u2200 {A \u0393} {m : \u039b A} -> (\u00acY-shape : \u00ac Y-shape m) -> (wf-\u0393 : Wf-ICtxt \u0393) -> \u0393 \u22a9 m\u2005 \u2236 \u03c9\n \u2229-cons : \u2200 {A \u0393 \u03c4 \u03c4\u1d62} {m : \u039b A} -> (\u00acY-shape : \u00ac Y-shape m) -> (wf-\u0393 : Wf-ICtxt \u0393) ->\n \u0393 \u22a9 m\u2005 \u2236 \u03c4 -> \u0393 \u22a9 m\u2005 \u2236 (\u2229 \u03c4\u1d62) -> \u0393 \u22a9 m\u2005 \u2236 (\u2229 (\u03c4 \u2237 \u03c4\u1d62))\n abs : \u2200 {A B \u0393 \u03c4\u1d62 \u03c4} (L : FVars) -> \u2200 {t : \u039b B} ->\n ( cf : \u2200 {x} -> (x\u2209L : x \u2209 L) -> ((x , \u2229 \u03c4\u1d62) \u2237 \u0393) \u22a9 \u039b[ 0 >> fv {A} x ] t \u2236 \u03c4 ) -> \u2229 \u03c4\u1d62 \u2237' A -> \u03c4 \u2237' B ->\n \u0393 \u22a9 lam A t \u2236 (\u2229 \u03c4\u1d62 ~> \u03c4)\n Y : \u2200 {\u0393 A \u03c4 \u03c4\u2081 \u03c4\u2082} -> Wf-ICtxt \u0393 -> \u03c4 \u2237' A -> \u03c4\u2081 \u2237' A -> \u03c4\u2082 \u2237' A ->\n \u0393 \u22a9 Y A \u2236 ((\u03c4 ~> \u03c4\u2081) ~> \u03c4\u2082)\n\ndata \u039bTerm : \u2200 {\u03c4} -> \u039b \u03c4 -> Set where\n var : \u2200 {A x} -> \u039bTerm (fv {A} x)\n lam : \u2200 {A B} (L : FVars) -> \u2200 {e : \u039b B} ->\n (cf : \u2200 {x} -> (x\u2209L : x \u2209 L) -> \u039bTerm (\u039b[ 0 >> fv {A} x ] e)) -> \u039bTerm (lam A e)\n app : \u2200 {A B} {e\u2081 : \u039b (A \u27f6 B)} {e\u2082 : \u039b A} -> \u039bTerm e\u2081 -> \u039bTerm e\u2082 -> \u039bTerm (app e\u2081 e\u2082)\n Y : \u2200 {t} -> \u039bTerm (Y t)\n\n\n-- \u22a2->\u039b-\u039bTerm : \u2200 {\u0393 t \u03c4} -> {\u0393\u22a2t : \u0393 \u22a2 t \u2236 \u03c4} -> (\u039bTerm (\u22a2->\u039b \u0393\u22a2t))\n-- \u22a2->\u039b-\u039bTerm {\u0393\u22a2t = var x\u2081 x\u2082} = var\n-- \u22a2->\u039b-\u039bTerm {\u0393\u22a2t = app \u0393\u22a2t \u0393\u22a2t\u2081} = app \u22a2->\u039b-\u039bTerm \u22a2->\u039b-\u039bTerm\n-- \u22a2->\u039b-\u039bTerm {\u0393\u22a2t = abs L cf} = {! !}\n-- \u22a2->\u039b-\u039bTerm {\u0393\u22a2t = Y x} = Y\n\n\ndata _->\u039b\u03b2_ : \u2200 {\u03c4} -> \u039b \u03c4 \u219d \u039b \u03c4 where\n redL : \u2200 {A B} {n : \u039b A} {m m' : \u039b (A \u27f6 B)} -> \u039bTerm n -> m ->\u039b\u03b2 m' -> app m n ->\u039b\u03b2 app m' n\n redR : \u2200 {A B} {m : \u039b (A \u27f6 B)} {n n' : \u039b A} -> \u039bTerm m -> n ->\u039b\u03b2 n' -> app m n ->\u039b\u03b2 app m n'\n abs : \u2200 L {A B} {m m' : \u039b B} -> ( \u2200 {x} -> x \u2209 L -> \u039b[ 0 >> fv {A} x ] m ->\u039b\u03b2 \u039b[ 0 >> fv {A} x ] m' ) ->\n lam A m ->\u039b\u03b2 lam A m'\n beta : \u2200 {A B} {m : \u039b (A \u27f6 B)} {n : \u039b A} -> \u039bTerm (lam A m) -> \u039bTerm n -> app (lam A m) n ->\u039b\u03b2 (\u039b[ 0 >> n ] m)\n Y : \u2200 {A} {m : \u039b (A \u27f6 A)} -> \u039bTerm m -> app (Y A) m ->\u039b\u03b2 app m (app (Y A) m)\n\n\n\n\u22a9->\u03b2 : \u2200 {A \u0393} {m m' : \u039b A} {\u03c4} -> \u0393 \u22a9 m' \u2236 \u03c4 -> m ->\u039b\u03b2 m' -> \u0393 \u22a9 m \u2236 \u03c4\n\u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redL trm-n m->\u03b2m') = {! !}\n\u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redR trm-m n->\u03b2n') = {! !}\n\u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (abs L x) = {! !}\n\u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (beta x x\u2081) = {! !}\n\u22a9->\u03b2 (app {s = m} \u0393\u22a9m\u2236\u03c4\u2081~>\u03c4 (app (Y wf-\u0393 \u03c4\u2082\u2237A \u03c4\u2083\u2237A \u03c4\u2081\u2237A) \u0393\u22a9m x \u03c4\u2082~>\u03c4\u2083\u2237A) (arr {A = A} _ \u03c4\u2237A) _) (Y trm-m) =\n app {A = A \u27f6 A} (Y wf-\u0393 \u03c4\u2081\u2237A \u03c4\u2237A \u03c4\u2237A) \u0393\u22a9m\u2236\u03c4\u2081~>\u03c4 (arr (arr \u03c4\u2081\u2237A \u03c4\u2237A) \u03c4\u2237A) (arr \u03c4\u2081\u2237A \u03c4\u2237A)\n\u22a9->\u03b2 (app \u0393\u22a9m\u2236\u03c4~>\u03c4' (\u2229-nil \u00acY-shape wf-\u0393) x \u03c4\u2237A) (Y trm-m) = \u22a5-elim (\u00acY-shape intro\u2081)\n\u22a9->\u03b2 (app \u0393\u22a9m\u2236\u03c4~>\u03c4' (\u2229-cons \u00acY-shape wf-\u0393 \u0393\u22a9Ym\u2236\u03c4' \u0393\u22a9Ym\u2236\u03c4'') x \u03c4\u2237A) (Y trm-m) = \u22a5-elim (\u00acY-shape intro\u2081)\n\u22a9->\u03b2 (\u2229-nil \u00acY-shape wf-\u0393) (Y x) = \u22a5-elim (\u00acY-shape intro\u2082)\n\u22a9->\u03b2 (\u2229-cons \u00acY-shape wf-\u0393 \u0393\u22a9m'\u2236\u03c4 \u0393\u22a9m'\u2236\u03c4\u2081) (Y x) = \u22a5-elim (\u00acY-shape intro\u2082)\n\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redL x m->\u03b2m') = \u22a9->\u03b2-redL \u0393\u22a9m'\u2236\u03c4 m->\u03b2m'\n-- where\n-- \u22a9->\u03b2-redL : \u2200 {\u0393 m m' n \u03c4} -> \u0393 \u22a9 app m' n \u2236 \u03c4 -> m ->\u03b2 m' -> \u0393 \u22a9 app m n \u2236 \u03c4\n-- \u22a9->\u03b2-redL = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redR x m->\u03b2m') = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (abs L x) = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (beta x x\u2081) = {! !}\n-- \u22a9->\u03b2 {\u03c4 = \u03c4} (app \u0393\u22a9m\u2236\u03c4'~>\u03c4 (app (Y {\u03c4 = \u03c4'} wf-\u0393 \u03c4\u2237 \u03c4\u2081\u2237 \u03c4\u2082\u2237) \u0393\u22a9m\u2236\u03c4'~>\u03c4')) (Y trm-m) =\n-- app (Y wf-\u0393 \u03c4\u2237 \u03c4\u2081\u2237 {! !}) \u0393\u22a9m\u2236\u03c4'~>\u03c4'\n--\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4 (\u2229-nil \u00acY-shape wf-\u0393 trm-m)) (Y x) = \u22a5-elim (\u00acY-shape intro\u2081)\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4 (\u2229-cons \u00acY-shape wf-\u0393 trm-m \u0393\u22a9m'\u2236\u03c4\u2081 \u0393\u22a9m'\u2236\u03c4\u2082)) (Y x\u2081) = \u22a5-elim (\u00acY-shape intro\u2081)\n-- \u22a9->\u03b2 (\u2229-nil \u00acY-shape wf-\u0393 trm-m) (Y x) = \u22a5-elim (\u00acY-shape intro\u2082)\n-- \u22a9->\u03b2 (\u2229-cons \u00acY-shape wf-\u0393 trm-m \u0393\u22a9m'\u2236\u03c4 \u0393\u22a9m'\u2236\u03c4\u2081) (Y x\u2081) = \u22a5-elim (\u00acY-shape intro\u2082)\n--\n--\n\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redL x m->\u03b2m') = \u22a9->\u03b2-redL \u0393\u22a9m'\u2236\u03c4 m->\u03b2m'\n-- where\n-- \u22a9->\u03b2-redL : \u2200 {\u0393 m m' n \u03c4} -> \u0393 \u22a9 app m' n \u2236 \u03c4 -> m ->\u03b2 m' -> \u0393 \u22a9 app m n \u2236 \u03c4\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (redL x\u2081 m->\u03b2m'') = app (\u22a9->\u03b2-redL \u0393\u22a9m'n\u2236\u03c4 m->\u03b2m'') \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9m''n\u2081n\u2082\u2236\u03c4\u1d62 wf-\u0393 (app _ trm-n\u2082)) (redL trm-n\u2081 m->\u03b2m'') =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2-redL (\u0393\u22a9m''n\u2081n\u2082\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2081 m->\u03b2m'')) wf-\u0393 (app (app (->\u03b2-Term-l m->\u03b2m'') trm-n\u2081) trm-n\u2082)\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (redR x\u2081 m->\u03b2m'') = app (\u22a9->\u03b2 \u0393\u22a9m'n\u2236\u03c4 (redR x\u2081 m->\u03b2m'')) \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9m\u2081e\u2082n\u2082\u2236\u03c4\u1d62 wf-\u0393 (app (app _ trm-e\u2082) trm-n\u2082)) (redR trm-m\u2081 n\u2081->e\u2082) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9m\u2081e\u2082n\u2082\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2082 (redR trm-m\u2081 n\u2081->e\u2082))) wf-\u0393 (app (app trm-m\u2081 (->\u03b2-Term-l n\u2081->e\u2082)) trm-n\u2082)\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (abs L cf) = app (\u22a9->\u03b2 \u0393\u22a9m'n\u2236\u03c4 (abs L cf)) \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9lam-m''n\u2081\u2236\u03c4\u1d62 wf-\u0393 (app trm-lam-m'' trm-n\u2081)) (abs L cf) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9lam-m''n\u2081\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2081 (abs L cf))) wf-\u0393 (app (lam L (\u03bb x\u2209L \u2192 ->\u03b2-Term-l (cf x\u2209L))) trm-n\u2081)\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (beta trm-lam-m\u2081 trm-n\u2081) = app (\u22a9->\u03b2 \u0393\u22a9m'n\u2236\u03c4 (beta trm-lam-m\u2081 trm-n\u2081)) \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9m\u2081^n\u2081n\u2082\u2236\u03c4\u2081 wf-\u0393 (app trm-m\u2081^n\u2081 trm-n\u2082)) (beta trm-lam-m\u2081 trm-n\u2081) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9m\u2081^n\u2081n\u2082\u2236\u03c4\u2081 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2082 (beta trm-lam-m\u2081 trm-n\u2081))) wf-\u0393 (app (app trm-lam-m\u2081 trm-n\u2081) trm-n\u2082)\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (Y trm-m\u2081) = app (\u22a9->\u03b2 \u0393\u22a9m'n\u2236\u03c4 (Y trm-m\u2081)) \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9m\u2081Ym\u2081n\u2081\u2236\u03c4\u1d62 wf-\u0393 (app (app _ trm-Ym\u2081) trm-n\u2081)) (Y trm-m\u2081) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9m\u2081Ym\u2081n\u2081\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2081 (Y trm-m\u2081))) wf-\u0393 (app trm-Ym\u2081 trm-n\u2081)\n--\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4 \u0393\u22a9m'\u2236\u03c4\u2081) (redR trm-n m->\u03b2m') = app \u0393\u22a9m'\u2236\u03c4 (\u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4\u2081 m->\u03b2m')\n-- \u22a9->\u03b2 (\u2229-intro \u0393\u22a9mn'\u2236\u03c4\u1d62 wf-\u0393 trm-mn') (redR trm-m n->\u03b2n') =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9mn'\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redR trm-m n->\u03b2n')) wf-\u0393 (app trm-m (->\u03b2-Term-l n->\u03b2n'))\n-- \u22a9->\u03b2 (\u2229-intro \u0393\u22a9lam-m'\u2236\u03c4\u1d62 wf-\u0393 trm-lam-x) (abs L x\u2082) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9lam-m'\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (abs L x\u2082)) wf-\u0393 (lam L (\u03bb x\u2209L \u2192 ->\u03b2-Term-l (x\u2082 x\u2209L)))\n-- \u22a9->\u03b2 (abs L cf) (abs L\u2081 x) = abs (L ++ L\u2081) (\u03bb x\u2209L \u2192 \u22a9->\u03b2 (cf (\u2209-cons-l _ _ x\u2209L)) (x (\u2209-cons-r L _ x\u2209L)))\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (beta x x\u2081) = {! !}\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4'~>\u03c4 (app \u0393\u22a9Ym\u2236\u03c4' \u0393\u22a9Ym\u2236\u03c4'')) (Y trm-m) = {! !}\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4'~>\u03c4 (\u2229-intro {\u03c4\u1d62 = []} \u0393\u22a9Ym\u2236\u03c4\u1d62 wf-\u0393 trm-Ym)) (Y trm-m) = {! !}\n-- \u22a9->\u03b2 {\u0393} (app {s = m} \u0393\u22a9m'\u2236\u03c4'~>\u03c4 (\u2229-intro {\u03c4\u1d62 = x \u2237 \u03c4\u1d62} \u0393\u22a9Ym\u2236\u03c4\u1d62 wf-\u0393 trm-Ym)) (Y trm-m) = {! !}\n-- where\n-- \u0393\u22a9m\u2236x~>x : \u0393 \u22a9 m \u2236 (x ~> x)\n-- \u0393\u22a9m\u2236x~>x = {! !}\n-- \u22a9->\u03b2 (\u2229-intro \u0393\u22a9mYm\u2236\u03c4\u1d62 wf-\u0393 (app _ trm-Ym)) (Y trm-m) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9mYm\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (Y trm-m)) wf-\u0393 trm-Ym\n","old_contents":"module ITyping where\n\nopen import Data.Empty\nopen import Data.List\nopen import Data.Nat\nopen import Data.Product\n-- open import Data.Maybe\nopen import Data.List.Any as LAny\nopen LAny.Membership-\u2261\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary.Core\n\n\nopen import Core\nopen import Core-Lemmas\nopen import Typing\nopen import Reduction\n\n\ndata IType : Set where\n o : IType\n _~>_ : IType -> IType -> IType\n \u2229 : List IType -> IType\n\n\u03c9 = \u2229 []\n\n\u2229' : IType -> IType\n\u2229' x = \u2229 (x \u2237 [])\n\n~>-inj-l : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2261 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) -> \u03c4\u2081\u2081 \u2261 \u03c4\u2082\u2081\n~>-inj-l refl = refl\n\n~>-inj-r : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2261 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) -> \u03c4\u2081\u2082 \u2261 \u03c4\u2082\u2082\n~>-inj-r refl = refl\n\n\u2229-inj-cons : \u2200 {x y \u03c4\u1d62 \u03c4\u2c7c} -> \u2229 (x \u2237 \u03c4\u1d62) \u2261 \u2229 (y \u2237 \u03c4\u2c7c) -> \u2229 \u03c4\u1d62 \u2261 \u2229 \u03c4\u2c7c\n\u2229-inj-cons refl = refl\n\n\u2229-inj : \u2200 {x y \u03c4\u1d62 \u03c4\u2c7c} -> \u2229 (x \u2237 \u03c4\u1d62) \u2261 \u2229 (y \u2237 \u03c4\u2c7c) -> x \u2261 y\n\u2229-inj refl = refl\n\n\n_\u225fTI_ : Decidable {A = IType} _\u2261_\no \u225fTI o = yes refl\no \u225fTI (_ ~> _) = no (\u03bb ())\no \u225fTI (\u2229 _) = no (\u03bb ())\n\n(_ ~> _) \u225fTI o = no (\u03bb ())\n(_ ~> _) \u225fTI (\u2229 _) = no (\u03bb ())\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) with \u03c4\u2081\u2081 \u225fTI \u03c4\u2082\u2081 | \u03c4\u2081\u2082 \u225fTI \u03c4\u2082\u2082\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (.\u03c4\u2081\u2081 ~> .\u03c4\u2081\u2082) | yes refl | yes refl = yes refl\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (.\u03c4\u2081\u2081 ~> \u03c4\u2082\u2082) | yes refl | no \u03c4\u2081\u2082\u2260\u03c4\u2082\u2082 = no (\u03bb eq \u2192 \u03c4\u2081\u2082\u2260\u03c4\u2082\u2082 (~>-inj-r eq))\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) | no \u03c4\u2081\u2081\u2260\u03c4\u2082\u2081 | _ = no (\u03bb eq \u2192 \u03c4\u2081\u2081\u2260\u03c4\u2082\u2081 (~>-inj-l eq))\n\n(\u2229 _) \u225fTI o = no (\u03bb ())\n(\u2229 _) \u225fTI (_ ~> _) = no (\u03bb ())\n\u2229 [] \u225fTI \u2229 [] = yes refl\n\u2229 [] \u225fTI \u2229 (x \u2237 \u03c4\u2c7c) = no (\u03bb ())\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 [] = no (\u03bb ())\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (y \u2237 \u03c4\u2c7c) with x \u225fTI y | (\u2229 \u03c4\u1d62) \u225fTI (\u2229 \u03c4\u2c7c)\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (.x \u2237 .\u03c4\u1d62) | yes refl | yes refl = yes refl\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (.x \u2237 \u03c4\u2c7c) | yes refl | no \u03c4\u1d62\u2260\u03c4\u2c7c = no (\u03bb \u2229x\u2237\u03c4\u1d62\u2261\u2229x\u2237\u03c4\u2c7c \u2192 \u03c4\u1d62\u2260\u03c4\u2c7c (\u2229-inj-cons \u2229x\u2237\u03c4\u1d62\u2261\u2229x\u2237\u03c4\u2c7c))\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (y \u2237 \u03c4\u2c7c) | no x\u2260y | _ = no (\u03bb \u2229x\u2237\u03c4\u1d62\u2261\u2229y\u2237\u03c4\u2c7c \u2192 x\u2260y (\u2229-inj \u2229x\u2237\u03c4\u1d62\u2261\u2229y\u2237\u03c4\u2c7c))\n\n\n\nICtxt = List (Atom \u00d7 IType)\n\n\ndata Wf-ICtxt : ICtxt -> Set where\n nil : Wf-ICtxt []\n cons : \u2200 {\u0393 x \u03c4} -> (x\u2209 : x \u2209 dom \u0393) -> Wf-ICtxt \u0393 ->\n Wf-ICtxt ((x , \u03c4) \u2237 \u0393)\n\n\ndata _\u2237'_ : IType -> Type -> Set where\n base : o \u2237' \u03c3\n arr : \u2200 {\u03b4 \u03c4 A B} -> \u03b4 \u2237' A -> \u03c4 \u2237' B -> (\u03b4 ~> \u03c4) \u2237' (A \u27f6 B)\n \u2229-nil : \u2200 {A} -> \u03c9 \u2237' A\n \u2229-cons : \u2200 {\u03c4\u1d62 \u03c4 A} -> \u03c4 \u2237' A -> \u2229 \u03c4\u1d62 \u2237' A -> \u2229 (\u03c4 \u2237 \u03c4\u1d62) \u2237' A\n\n\ndata _\u2264\u2229_ : IType -> IType -> Set where\n base : o \u2264\u2229 o\n arr : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> \u03c4\u2081\u2082 \u2264\u2229 \u03c4\u2082\u2082 -> \u03c4\u2082\u2081 \u2264\u2229 \u03c4\u2081\u2081 -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2264\u2229 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082)\n \u2229-\u2208 : \u2200 {\u03c4 \u03c4\u1d62} -> \u03c4 \u2208 \u03c4\u1d62 -> \u2229 \u03c4\u1d62 \u2264\u2229 \u03c4\n \u2229-nil : \u2200 {\u03c4} -> \u03c4 \u2264\u2229 \u03c9\n \u2229-cons : \u2200 {\u03c4 \u03c4' \u03c4\u1d62} -> \u03c4 \u2264\u2229 \u03c4' -> \u03c4 \u2264\u2229 \u2229 \u03c4\u1d62 -> \u03c4 \u2264\u2229 \u2229 (\u03c4' \u2237 \u03c4\u1d62)\n -- \u2229-trans : \u2200 {\u03c4\u2081 \u03c4\u2082 \u03c4\u2083} -> \u03c4\u2081 \u2264\u2229 \u03c4\u2082 -> \u03c4\u2082 \u2264\u2229 \u03c4\u2083 -> \u03c4\u2081 \u2264\u2229 \u03c4\u2083\n\n\n\u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 : \u2200 {\u03c4\u1d62 \u03c4\u2c7c} -> \u03c4\u1d62 \u2286 \u03c4\u2c7c -> \u2229 \u03c4\u2c7c \u2264\u2229 \u2229 \u03c4\u1d62\n\u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 {[]} \u03c4\u1d62\u2286\u03c4\u2c7c = \u2229-nil\n\u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 {x \u2237 \u03c4\u1d62} \u03c4\u1d62\u2286\u03c4\u2c7c = \u2229-cons (\u2229-\u2208 (\u03c4\u1d62\u2286\u03c4\u2c7c (here refl))) (\u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 (\u03bb {x\u2081} z \u2192 \u03c4\u1d62\u2286\u03c4\u2c7c (there z)))\n\n\u2264\u2229-refl : \u2200 {\u03c4} -> \u03c4 \u2264\u2229 \u03c4\n\u2264\u2229-refl {o} = base\n\u2264\u2229-refl {\u03c4 ~> \u03c4\u2081} = arr \u2264\u2229-refl \u2264\u2229-refl\n\u2264\u2229-refl {\u2229 []} = \u2229-nil\n\u2264\u2229-refl {\u2229 (x \u2237 x\u2081)} = \u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 (\u03bb {x\u2082} z \u2192 z)\n\n\ndata \u039b : Type -> Set where\n bv : \u2200 {A} -> (i : \u2115) -> \u039b A\n fv : \u2200 {A} -> (x : Atom) -> \u039b A\n lam : \u2200 {B} -> (A : Type) -> (e : \u039b B) -> \u039b (A \u27f6 B)\n app : \u2200 {A B} -> (e\u2081 : \u039b (A \u27f6 B)) -> (e\u2082 : \u039b A) -> \u039b B\n Y : (t : Type) -> \u039b ((t \u27f6 t) \u27f6 t)\n\n\n-- _\u2208?_ : \u2200 {a} {A : Set a} -> Atom -> List (Atom \u00d7 A) -> Maybe A\n-- a \u2208? [] = nothing\n-- a \u2208? (l \u2237 ist) with a \u225f proj\u2081 l\n-- ... | yes _ = just (proj\u2082 l)\n-- a \u2208? (l \u2237 ist) | no _ = a \u2208? ist\n\n-- PTerm->\u039b : \u2200 {\u0393 m t} -> (List (Atom \u00d7 \u2115)) -> \u0393 \u22a2 m \u2236 t -> \u039b t\n-- PTerm->\u039b {m = bv i} _ ()\n-- PTerm->\u039b {m = fv x} bound \u0393\u22a2m\u2236t with x \u2208? bound\n-- PTerm->\u039b {m = fv x} {t} bound \u0393\u22a2m\u2236t | just i = bv {t} i\n-- PTerm->\u039b {m = fv x} bound \u0393\u22a2m\u2236t | nothing = fv x\n-- PTerm->\u039b {m = lam m} bound (abs {_} {\u03c4\u2081} {\u03c4\u2082} L cf) = lam \u03c4\u2081 (PTerm->\u039b ((x , 0) \u2237 bound') (cf (\u2209-cons-l _ _ x\u2209)))\n-- where\n-- x = \u2203fresh (L ++ FV m)\n-- x\u2209 : x \u2209 (L ++ FV m)\n-- x\u2209 = \u2203fresh-spec (L ++ FV m)\n--\n-- bound' : List (Atom \u00d7 \u2115)\n-- bound' = Data.List.map (\u03bb a,i \u2192 (proj\u2081 a,i) , suc (proj\u2082 a,i)) bound\n--\n-- PTerm->\u039b {m = app t1 t} bound (app \u0393\u22a2s \u0393\u22a2t) = app (PTerm->\u039b bound \u0393\u22a2s) (PTerm->\u039b bound \u0393\u22a2t)\n-- PTerm->\u039b {m = Y \u03c4} bound (Y x) = Y \u03c4\n--\n-- PTerm->\u039b : \u2200 {\u0393 m t} -> \u0393 \u22a2 m \u2236 t -> \u039b t\n-- PTerm->\u039b = PTerm->\u039b []\n\ndata _~_ : \u2200{t} -> \u039b t -> PTerm -> Set where\n bv : \u2200 {t i} -> (bv {t} i) ~ (bv i)\n fv : \u2200 {t x} -> (fv {t} x) ~ (fv x)\n lam : \u2200 {t s m m'} -> m ~ m' -> (lam {s} t m) ~ (lam m')\n app : \u2200 {t s m n m' n'} -> m ~ m' -> n ~ n' -> (app {t} {s} m n) ~ (app m' n')\n Y : \u2200 {t} -> (Y t) ~ (Y t)\n\n\u039b[_<<_] : \u2200 {t} -> \u2115 -> Atom -> \u039b t -> \u039b t\n\u039b[ k << x ] (bv i) = bv i\n\u039b[ k << x ] (fv y) with x \u225f y\n... | yes _ = bv k\n... | no _ = fv y\n\u039b[ k << x ] (lam t m) = lam t (\u039b[ (suc k) << x ] m)\n\u039b[ k << x ] (app t1 t2) = app (\u039b[ k << x ] t1) (\u039b[ k << x ] t2)\n\u039b[ k << x ] (Y t) = Y t\n\n\n\nPTerm->\u039b : \u2200 {\u0393 m t} -> \u0393 \u22a2 m \u2236 t -> \u039b t\nPTerm->\u039b {m = bv i} ()\nPTerm->\u039b {m = fv x} {t} \u0393\u22a2m\u2236t = fv {t} x\nPTerm->\u039b {m = lam m} (abs {_} {\u03c4\u2081} {\u03c4\u2082} L cf) =\n lam \u03c4\u2081 ( \u039b[ 0 << \u2203fresh (L ++ FV m) ]\n (PTerm->\u039b (cf (\u2209-cons-l _ _ (\u2203fresh-spec (L ++ FV m)) ))) )\nPTerm->\u039b {m = app t1 t} (app \u0393\u22a2s \u0393\u22a2t) = app (PTerm->\u039b \u0393\u22a2s) (PTerm->\u039b \u0393\u22a2t)\nPTerm->\u039b {m = Y \u03c4} (Y x) = Y \u03c4\n\n\n\u039b*^-*^~ : \u2200 {\u03c4 x k} t t' -> _~_ {\u03c4} t t' -> \u039b[ k << x ] t ~ ([ k << x ] t')\n\u039b*^-*^~ _ _ bv = bv\n\u039b*^-*^~ {x = x} (fv y) _ fv with x \u225f y\n\u039b*^-*^~ (fv x) .(fv x) fv | yes _ = bv\n\u039b*^-*^~ (fv y) .(fv y) fv | no _ = fv\n\u039b*^-*^~ _ _ (lam {m = m} {m'} t~t') = lam (\u039b*^-*^~ m m' t~t')\n\u039b*^-*^~ _ _ (app {m = m} {n} {m'} {n'} t~t' t~t'') = app (\u039b*^-*^~ m m' t~t') (\u039b*^-*^~ n n' t~t'')\n\u039b*^-*^~ _ _ Y = Y\n\n\n\n\u039b*^-*^-swap : \u2200 {t : Type} k n x y m -> \u00ac(k \u2261 n) -> \u00ac(x \u2261 y) -> \u039b[_<<_] {t} k x (\u039b[ n << y ] m) \u2261 \u039b[ n << y ] (\u039b[ k << x ] m)\n\u039b*^-*^-swap k n x y (bv i) k\u2260n x\u2260y = refl\n\u039b*^-*^-swap k n x y (fv z) k\u2260n x\u2260y = {! !}\n\u039b*^-*^-swap k n x y (lam A m) k\u2260n x\u2260y =\n cong (lam A) (\u039b*^-*^-swap (suc k) (suc n) x y m (\u03bb x\u2081 \u2192 k\u2260n (\u2261-suc x\u2081)) x\u2260y)\n\u039b*^-*^-swap k n x y (app m m') k\u2260n x\u2260y rewrite\n \u039b*^-*^-swap k n x y m k\u2260n x\u2260y | \u039b*^-*^-swap k n x y m' k\u2260n x\u2260y = refl\n\u039b*^-*^-swap k n x y (Y t) k\u2260n x\u2260y = refl\n\n\nfv-^-\u039b*^-refl : \u2200 x t {k \u0393 \u03c4} -> x \u2209 FV t -> (\u0393\u22a2t^x : \u0393 \u22a2 [ k >> fv x ] t \u2236 \u03c4) -> (\u039b[ k << x ] (PTerm->\u039b \u0393\u22a2t^x) ) ~ t\nfv-^-\u039b*^-refl x (bv n) x\u2209FVt ()\nfv-^-\u039b*^-refl x (fv y) x\u2209FVt \u0393\u22a2t^x with x \u225f y\nfv-^-\u039b*^-refl x (fv .x) x\u2209FVt \u0393\u22a2t^x | yes refl = \u22a5-elim (x\u2209FVt (here refl))\nfv-^-\u039b*^-refl x (fv y) x\u2209FVt \u0393\u22a2t^x | no x\u2260y = fv\nfv-^-\u039b*^-refl x (lam t) {k} {\u0393} x\u2209FVt (abs {\u03c4\u2081 = \u03c4\u2081} {\u03c4\u2082} L cf) = lam {! !}\n --\n -- where\n -- x' = \u2203fresh ( L ++ FV ([ suc k >> fv x ] t) )\n -- x'\u2209 = \u2203fresh-spec ( L ++ FV ([ suc k >> fv x ] t) )\n --\n -- x'\u0393\u22a2[suc-k>>x]t^'x' : ((x' , \u03c4\u2081) \u2237 \u0393) \u22a2 ([ suc k >> fv x ] t) ^' x' \u2236 \u03c4\u2082\n -- x'\u0393\u22a2[suc-k>>x]t^'x' = cf (\u2209-cons-l L (FV ([ suc k >> fv x ] t)) (\u2203fresh-spec (L ++ FV ([ suc k >> fv x ] t))))\n --\n --\n -- x'\u0393\u22a2[suc-k>>x]t^'x'\u2261 : ((x' , \u03c4\u2081) \u2237 \u0393) \u22a2 [ suc k >> fv x ] (t ^' x') \u2236 \u03c4\u2082 \u2261\n -- ((x' , \u03c4\u2081) \u2237 \u0393) \u22a2 ([ suc k >> fv x ] t) ^' x' \u2236 \u03c4\u2082\n -- x'\u0393\u22a2[suc-k>>x]t^'x'\u2261 rewrite ^-^-swap (suc k) 0 x x' t (\u03bb ()) (\u03bb x\u2261x' \u2192 {! !}) = refl\n --\n -- x'\u0393\u22a2[suc-k>>x]t^'x'' : ((x' , \u03c4\u2081) \u2237 \u0393) \u22a2 [ suc k >> fv x ] (t ^' x') \u2236 \u03c4\u2082\n -- x'\u0393\u22a2[suc-k>>x]t^'x'' rewrite x'\u0393\u22a2[suc-k>>x]t^'x'\u2261 = x'\u0393\u22a2[suc-k>>x]t^'x'\n --\n -- ih'' : \u039b[ (suc k) << x ] (PTerm->\u039b x'\u0393\u22a2[suc-k>>x]t^'x'') ~ (t ^' x')\n -- ih'' = fv-^-\u039b*^-refl x ([ 0 >> fv x' ] t) {! !} x'\u0393\u22a2[suc-k>>x]t^'x''\n --\n -- -- ih' : \u039b[ 0 << x' ] ( \u039b[ (suc k) << x ] (PTerm->\u039b x'\u0393\u22a2[suc-k>>x]t^'x') ) ~ t\n -- -- ih' rewrite x'\u0393\u22a2[suc-k>>x]t^'x'\u2261 = {! !}\n --\n -- ih : \u039b[ (suc k) << x ] ( \u039b[ 0 << x' ] (PTerm->\u039b x'\u0393\u22a2[suc-k>>x]t^'x') ) ~ t\n -- ih rewrite\n -- \u039b*^-*^-swap {\u03c4\u2082} (suc k) 0 x x' (PTerm->\u039b x'\u0393\u22a2[suc-k>>x]t^'x') {! !} {! !} |\n -- x'\u0393\u22a2[suc-k>>x]t^'x'\u2261 = {! !}\n\nfv-^-\u039b*^-refl x (app s t) x\u2209FVt (app \u0393\u22a2s^x \u0393\u22a2t^x) = app\n (fv-^-\u039b*^-refl x s (\u2209-cons-l _ _ x\u2209FVt) \u0393\u22a2s^x)\n (fv-^-\u039b*^-refl x t (\u2209-cons-r (FV s) _ x\u2209FVt) \u0393\u22a2t^x)\nfv-^-\u039b*^-refl x (Y \u03c4) x\u2209FVt (Y x\u2081) = Y\n\n\n\nPTerm->\u039b~ : \u2200 {\u0393 t \u03c4} -> {\u0393\u22a2t : \u0393 \u22a2 t \u2236 \u03c4} -> (PTerm->\u039b \u0393\u22a2t) ~ t\nPTerm->\u039b~ {t = bv i} = \u03bb {\u03c4} \u2192 \u03bb {}\nPTerm->\u039b~ {t = fv x} = \u03bb {\u03c4} {\u0393\u22a2t} \u2192 fv\nPTerm->\u039b~ {t = lam t} {\u0393\u22a2t = abs {\u03c4\u2081 = \u03c4\u2081} {\u03c4\u2082} L cf} =\n lam (fv-^-\u039b*^-refl (\u2203fresh (L ++ FV t)) t (\u2209-cons-r L _ (\u2203fresh-spec (L ++ FV t))) (cf (\u2209-cons-l L (FV t) (\u2203fresh-spec (L ++ FV t)))))\n -- where\n -- x' = \u2203fresh (L ++ FV t)\n -- x'\u2237\u0393\u22a2t^'x' = cf (\u2209-cons-l L (FV t) (\u2203fresh-spec (L ++ FV t)))\n --\n -- ih' : \u039b[ 0 << x' ] (PTerm->\u039b x'\u2237\u0393\u22a2t^'x') ~ (* x' ^ (t ^' x'))\n -- ih' = {! !}\n --\n -- sub : \u2200 {\u03c4 x m} -> x \u2209 FV t -> _~_ {\u03c4} m t \u2261 m ~ (* x ^ (t ^' x))\n -- sub {_} {x} x\u2209 rewrite fv-^-*^-refl x t {0} x\u2209 = refl\n --\n -- ih : \u039b[ 0 << x' ] (PTerm->\u039b x'\u2237\u0393\u22a2t^'x') ~ t\n -- ih rewrite sub {_} {x'} {\u039b[ 0 << x' ] (PTerm->\u039b x'\u2237\u0393\u22a2t^'x')} {! !} = ih'\nPTerm->\u039b~ {t = app t t\u2081} {\u0393\u22a2t = app \u0393\u22a2t \u0393\u22a2t\u2081} = app PTerm->\u039b~ PTerm->\u039b~\nPTerm->\u039b~ {t = Y t\u2081} {\u0393\u22a2t = Y x} = Y\n\n\n\u039b->PTerm : \u2200 {t} -> \u039b t -> PTerm\n\u039b->PTerm (bv i) = bv i\n\u039b->PTerm (fv x) = fv x\n\u039b->PTerm (lam A \u039bt) = lam (\u039b->PTerm \u039bt)\n\u039b->PTerm (app \u039bs \u039bt) = app (\u039b->PTerm \u039bs) (\u039b->PTerm \u039bt)\n\u039b->PTerm (Y t) = Y t\n\n\n\n\n\n-- data IType\u209b : IType -> Set where\n-- o : IType\u209b o\n-- arr : \u2200 {\u03c4 \u03c4'} -> IType\u209b \u03c4 -> IType\u209b \u03c4' -> IType\u209b (\u03c4 ~> \u03c4')\n--\n-- data IType\u209b\u209b : IType -> Set where\n-- o : IType\u209b\u209b o\n-- arr : \u2200 {\u03c4 \u03c4'} -> IType\u209b\u209b \u03c4 -> IType\u209b\u209b \u03c4' -> IType\u209b\u209b (\u03c4 ~> \u03c4')\n-- \u2229-nil : IType\u209b\u209b \u03c9\n-- \u2229-cons : \u2200 {\u03c4 \u03c4\u1d62} -> IType\u209b \u03c4 -> IType\u209b\u209b (\u2229 \u03c4\u1d62) -> IType\u209b\u209b (\u2229 (\u03c4 \u2237 \u03c4\u1d62))\n--\n-- \u03c4\u209b->\u03c4\u209b\u209b : \u2200 {\u03c4} -> IType\u209b \u03c4 -> IType\u209b\u209b \u03c4\n-- \u03c4\u209b->\u03c4\u209b\u209b o = o\n-- \u03c4\u209b->\u03c4\u209b\u209b (arr \u03c4\u209b \u03c4\u209b\u2081) = arr (\u03c4\u209b->\u03c4\u209b\u209b \u03c4\u209b) (\u03c4\u209b->\u03c4\u209b\u209b \u03c4\u209b\u2081)\n\n\u039b[_>>_] : \u2200 {\u03c4 \u03c4'} -> \u2115 -> \u039b \u03c4' -> \u039b \u03c4 -> \u039b \u03c4\n\u039b[_>>_] {\u03c4} {\u03c4'} k u (bv i) with k \u225f i | \u03c4 \u225fT \u03c4'\n\u039b[ k >> u ] (bv i) | yes _ | yes refl = u\n... | yes _ | no _ = bv i\n... | no _ | _ = bv i\n\u039b[ k >> u ] (fv x) = fv x\n\u039b[ k >> u ] (lam A t) = lam A (\u039b[ (suc k) >> u ] t)\n\u039b[ k >> u ] (app t1 t2) = app (\u039b[ k >> u ] t1) (\u039b[ k >> u ] t2) -- app (\u039b[ k >> u ] t1) (\u039b[ k >> u ] t2)\n\u039b[ k >> u ] (Y t) = Y t\n\n\ndata Y-shape : \u2200 {\u03c4} -> \u039b \u03c4 -> Set where\n intro\u2081 : \u2200 {A m} -> Y-shape (app (Y A) m)\n intro\u2082 : \u2200 {A m} -> Y-shape (app m (app (Y A) m))\n\ndata _\u22a9_\u2236_ : \u2200 {A} -> ICtxt -> \u039b A -> IType -> Set where\n var : \u2200 {A \u0393 x \u03c4} {\u03c4\u1d62 : List IType} -> (wf-\u0393 : Wf-ICtxt \u0393) -> (\u03c4\u1d62\u2208\u0393 : (x , (\u2229 \u03c4\u1d62)) \u2208 \u0393) -> (\u03c4\u1d62\u2264\u2229\u03c4 : \u2229 \u03c4\u1d62 \u2264\u2229 \u03c4) -> \u03c4 \u2237' A ->\n \u0393 \u22a9 fv {A} x \u2236 \u03c4\n app : \u2200 {A B \u0393 s t \u03c4\u2081 \u03c4\u2082} -> \u0393 \u22a9 s \u2236 (\u03c4\u2081 ~> \u03c4\u2082) -> \u0393 \u22a9 t \u2236 \u03c4\u2081 -> (\u03c4\u2081 ~> \u03c4\u2082) \u2237' (A \u27f6 B) -> \u03c4\u2081 \u2237' B ->\n \u0393 \u22a9 (app {A} {B} s t) \u2236 \u03c4\u2082\n \u2229-nil : \u2200 {A \u0393} {m : \u039b A} -> (\u00acY-shape : \u00ac Y-shape m) -> (wf-\u0393 : Wf-ICtxt \u0393) -> \u0393 \u22a9 m\u2005 \u2236 \u03c9\n \u2229-cons : \u2200 {A \u0393 \u03c4 \u03c4\u1d62} {m : \u039b A} -> (\u00acY-shape : \u00ac Y-shape m) -> (wf-\u0393 : Wf-ICtxt \u0393) ->\n \u0393 \u22a9 m\u2005 \u2236 \u03c4 -> \u0393 \u22a9 m\u2005 \u2236 (\u2229 \u03c4\u1d62) -> \u0393 \u22a9 m\u2005 \u2236 (\u2229 (\u03c4 \u2237 \u03c4\u1d62))\n abs : \u2200 {A B \u0393 \u03c4\u1d62 \u03c4} (L : FVars) -> \u2200 {t : \u039b B} ->\n ( cf : \u2200 {x} -> (x\u2209L : x \u2209 L) -> ((x , \u2229 \u03c4\u1d62) \u2237 \u0393) \u22a9 \u039b[ 0 >> fv {A} x ] t \u2236 \u03c4 ) -> \u2229 \u03c4\u1d62 \u2237' A -> \u03c4 \u2237' B -> \u0393 \u22a9 lam A t \u2236 (\u2229 \u03c4\u1d62 ~> \u03c4)\n Y : \u2200 {\u0393 A \u03c4 \u03c4\u2081 \u03c4\u2082} -> Wf-ICtxt \u0393 -> \u03c4 \u2237' A -> \u03c4\u2081 \u2237' A -> \u03c4\u2082 \u2237' A ->\n \u0393 \u22a9 Y A \u2236 ((\u03c4 ~> \u03c4\u2081) ~> \u03c4\u2082)\n\n\n-- \u22a9->\u03b2 : \u2200 {\u0393 m m' \u03c4} -> \u0393 \u22a9 m' \u2236 \u03c4 -> m ->\u03b2 m' -> \u0393 \u22a9 m \u2236 \u03c4\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redL x m->\u03b2m') = \u22a9->\u03b2-redL \u0393\u22a9m'\u2236\u03c4 m->\u03b2m'\n-- where\n-- \u22a9->\u03b2-redL : \u2200 {\u0393 m m' n \u03c4} -> \u0393 \u22a9 app m' n \u2236 \u03c4 -> m ->\u03b2 m' -> \u0393 \u22a9 app m n \u2236 \u03c4\n-- \u22a9->\u03b2-redL = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redR x m->\u03b2m') = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (abs L x) = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (beta x x\u2081) = {! !}\n-- \u22a9->\u03b2 {\u03c4 = \u03c4} (app \u0393\u22a9m\u2236\u03c4'~>\u03c4 (app (Y {\u03c4 = \u03c4'} wf-\u0393 \u03c4\u2237 \u03c4\u2081\u2237 \u03c4\u2082\u2237) \u0393\u22a9m\u2236\u03c4'~>\u03c4')) (Y trm-m) =\n-- app (Y wf-\u0393 \u03c4\u2237 \u03c4\u2081\u2237 {! !}) \u0393\u22a9m\u2236\u03c4'~>\u03c4'\n--\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4 (\u2229-nil \u00acY-shape wf-\u0393 trm-m)) (Y x) = \u22a5-elim (\u00acY-shape intro\u2081)\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4 (\u2229-cons \u00acY-shape wf-\u0393 trm-m \u0393\u22a9m'\u2236\u03c4\u2081 \u0393\u22a9m'\u2236\u03c4\u2082)) (Y x\u2081) = \u22a5-elim (\u00acY-shape intro\u2081)\n-- \u22a9->\u03b2 (\u2229-nil \u00acY-shape wf-\u0393 trm-m) (Y x) = \u22a5-elim (\u00acY-shape intro\u2082)\n-- \u22a9->\u03b2 (\u2229-cons \u00acY-shape wf-\u0393 trm-m \u0393\u22a9m'\u2236\u03c4 \u0393\u22a9m'\u2236\u03c4\u2081) (Y x\u2081) = \u22a5-elim (\u00acY-shape intro\u2082)\n--\n--\n\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redL x m->\u03b2m') = \u22a9->\u03b2-redL \u0393\u22a9m'\u2236\u03c4 m->\u03b2m'\n-- where\n-- \u22a9->\u03b2-redL : \u2200 {\u0393 m m' n \u03c4} -> \u0393 \u22a9 app m' n \u2236 \u03c4 -> m ->\u03b2 m' -> \u0393 \u22a9 app m n \u2236 \u03c4\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (redL x\u2081 m->\u03b2m'') = app (\u22a9->\u03b2-redL \u0393\u22a9m'n\u2236\u03c4 m->\u03b2m'') \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9m''n\u2081n\u2082\u2236\u03c4\u1d62 wf-\u0393 (app _ trm-n\u2082)) (redL trm-n\u2081 m->\u03b2m'') =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2-redL (\u0393\u22a9m''n\u2081n\u2082\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2081 m->\u03b2m'')) wf-\u0393 (app (app (->\u03b2-Term-l m->\u03b2m'') trm-n\u2081) trm-n\u2082)\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (redR x\u2081 m->\u03b2m'') = app (\u22a9->\u03b2 \u0393\u22a9m'n\u2236\u03c4 (redR x\u2081 m->\u03b2m'')) \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9m\u2081e\u2082n\u2082\u2236\u03c4\u1d62 wf-\u0393 (app (app _ trm-e\u2082) trm-n\u2082)) (redR trm-m\u2081 n\u2081->e\u2082) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9m\u2081e\u2082n\u2082\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2082 (redR trm-m\u2081 n\u2081->e\u2082))) wf-\u0393 (app (app trm-m\u2081 (->\u03b2-Term-l n\u2081->e\u2082)) trm-n\u2082)\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (abs L cf) = app (\u22a9->\u03b2 \u0393\u22a9m'n\u2236\u03c4 (abs L cf)) \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9lam-m''n\u2081\u2236\u03c4\u1d62 wf-\u0393 (app trm-lam-m'' trm-n\u2081)) (abs L cf) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9lam-m''n\u2081\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2081 (abs L cf))) wf-\u0393 (app (lam L (\u03bb x\u2209L \u2192 ->\u03b2-Term-l (cf x\u2209L))) trm-n\u2081)\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (beta trm-lam-m\u2081 trm-n\u2081) = app (\u22a9->\u03b2 \u0393\u22a9m'n\u2236\u03c4 (beta trm-lam-m\u2081 trm-n\u2081)) \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9m\u2081^n\u2081n\u2082\u2236\u03c4\u2081 wf-\u0393 (app trm-m\u2081^n\u2081 trm-n\u2082)) (beta trm-lam-m\u2081 trm-n\u2081) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9m\u2081^n\u2081n\u2082\u2236\u03c4\u2081 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2082 (beta trm-lam-m\u2081 trm-n\u2081))) wf-\u0393 (app (app trm-lam-m\u2081 trm-n\u2081) trm-n\u2082)\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (Y trm-m\u2081) = app (\u22a9->\u03b2 \u0393\u22a9m'n\u2236\u03c4 (Y trm-m\u2081)) \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9m\u2081Ym\u2081n\u2081\u2236\u03c4\u1d62 wf-\u0393 (app (app _ trm-Ym\u2081) trm-n\u2081)) (Y trm-m\u2081) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9m\u2081Ym\u2081n\u2081\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2081 (Y trm-m\u2081))) wf-\u0393 (app trm-Ym\u2081 trm-n\u2081)\n--\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4 \u0393\u22a9m'\u2236\u03c4\u2081) (redR trm-n m->\u03b2m') = app \u0393\u22a9m'\u2236\u03c4 (\u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4\u2081 m->\u03b2m')\n-- \u22a9->\u03b2 (\u2229-intro \u0393\u22a9mn'\u2236\u03c4\u1d62 wf-\u0393 trm-mn') (redR trm-m n->\u03b2n') =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9mn'\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redR trm-m n->\u03b2n')) wf-\u0393 (app trm-m (->\u03b2-Term-l n->\u03b2n'))\n-- \u22a9->\u03b2 (\u2229-intro \u0393\u22a9lam-m'\u2236\u03c4\u1d62 wf-\u0393 trm-lam-x) (abs L x\u2082) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9lam-m'\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (abs L x\u2082)) wf-\u0393 (lam L (\u03bb x\u2209L \u2192 ->\u03b2-Term-l (x\u2082 x\u2209L)))\n-- \u22a9->\u03b2 (abs L cf) (abs L\u2081 x) = abs (L ++ L\u2081) (\u03bb x\u2209L \u2192 \u22a9->\u03b2 (cf (\u2209-cons-l _ _ x\u2209L)) (x (\u2209-cons-r L _ x\u2209L)))\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (beta x x\u2081) = {! !}\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4'~>\u03c4 (app \u0393\u22a9Ym\u2236\u03c4' \u0393\u22a9Ym\u2236\u03c4'')) (Y trm-m) = {! !}\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4'~>\u03c4 (\u2229-intro {\u03c4\u1d62 = []} \u0393\u22a9Ym\u2236\u03c4\u1d62 wf-\u0393 trm-Ym)) (Y trm-m) = {! !}\n-- \u22a9->\u03b2 {\u0393} (app {s = m} \u0393\u22a9m'\u2236\u03c4'~>\u03c4 (\u2229-intro {\u03c4\u1d62 = x \u2237 \u03c4\u1d62} \u0393\u22a9Ym\u2236\u03c4\u1d62 wf-\u0393 trm-Ym)) (Y trm-m) = {! !}\n-- where\n-- \u0393\u22a9m\u2236x~>x : \u0393 \u22a9 m \u2236 (x ~> x)\n-- \u0393\u22a9m\u2236x~>x = {! !}\n-- \u22a9->\u03b2 (\u2229-intro \u0393\u22a9mYm\u2236\u03c4\u1d62 wf-\u0393 (app _ trm-Ym)) (Y trm-m) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9mYm\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (Y trm-m)) wf-\u0393 trm-Ym\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"90a15e4a99a04068b454c8a345f15491d28aef67","subject":"Some changes to a note on co-inductive natural numbers.","message":"Some changes to a note on co-inductive natural numbers.\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/FOT\/FOTC\/Data\/Conat\/ConatSL.agda","new_file":"notes\/FOT\/FOTC\/Data\/Conat\/ConatSL.agda","new_contents":"------------------------------------------------------------------------------\n-- Definition of FOTC Conat using Agda's co-inductive combinators\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Data.Conat.ConatSL where\n\nopen import FOTC.Base\nopen import Coinduction\n\n------------------------------------------------------------------------------\n\ndata Conat : D \u2192 Set where\n cozero : Conat zero\n cosucc : \u2200 {n} \u2192 (\u221e (Conat n)) \u2192 Conat (succ\u2081 n)\n\nConat-unf : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] Conat n' \u2227 n \u2261 succ\u2081 n')\nConat-unf cozero = inj\u2081 refl\nConat-unf (cosucc {n} Cn) = inj\u2082 (n , \u266d Cn , refl)\n\nConat-pre-fixed : \u2200 {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] Conat n' \u2227 n \u2261 succ\u2081 n')) \u2192\n Conat n\nConat-pre-fixed (inj\u2081 h) = subst Conat (sym h) cozero\nConat-pre-fixed (inj\u2082 (n , Cn , h)) = subst Conat (sym h) (cosucc (\u266f Cn))\n\nConat-coind : \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] A n' \u2227 n \u2261 succ\u2081 n')) \u2192\n A n \u2192 Conat n\nConat-coind A h An = {!!}\n\npostulate\n inf : D\n inf-eq : inf \u2261 succ\u2081 inf\n{-# ATP axiom inf-eq #-}\n\n{-# NO_TERMINATION_CHECK #-}\ninf-Conat : Conat inf\ninf-Conat = subst Conat (sym inf-eq) (cosucc (\u266f inf-Conat))\n","old_contents":"------------------------------------------------------------------------------\n-- Definition of FOTC Conat using Agda's co-inductive combinators\n------------------------------------------------------------------------------\n\n{-# OPTIONS --allow-unsolved-metas #-}\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOT.FOTC.Data.Conat.ConatSL where\n\nopen import FOTC.Base\nopen import Coinduction\n\n------------------------------------------------------------------------------\n\ndata Conat : D \u2192 Set where\n cozero : Conat zero\n cosucc : \u2200 {n} \u2192 (\u221e (Conat n)) \u2192 Conat (succ\u2081 n)\n\nConat-unf : \u2200 {n} \u2192 Conat n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] Conat n' \u2227 n \u2261 succ\u2081 n')\nConat-unf cozero = inj\u2081 refl\nConat-unf (cosucc {n} Cn) = inj\u2082 (n , \u266d Cn , refl)\n\nConat-pre-fixed : \u2200 {n} \u2192\n (n \u2261 zero \u2228 (\u2203[ n' ] Conat n' \u2227 n \u2261 succ\u2081 n')) \u2192\n Conat n\nConat-pre-fixed (inj\u2081 h) = subst Conat (sym h) cozero\nConat-pre-fixed (inj\u2082 (n , Cn , h)) = subst Conat (sym h) (cosucc (\u266f Cn))\n\nConat-coind : \u2200 (A : D \u2192 Set) {n} \u2192\n (A n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] A n' \u2227 n \u2261 succ\u2081 n')) \u2192\n A n \u2192 Conat n\nConat-coind A h An = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d555f1b4a408249eaf68dbbeebfcdd95ac7832d1","subject":"improved proof that double is even","message":"improved proof that double is even\n","repos":"siddhartha-gadgil\/LogicTypesSpaces,siddhartha-gadgil\/LogicTypesSpaces,siddhartha-gadgil\/LogicTypesSpaces,siddhartha-gadgil\/LogicTypesSpaces","old_file":"code\/GoaTest.agda","new_file":"code\/GoaTest.agda","new_contents":"open import Base\n\nmodule GoaTest where\n\ndata Bool : Type where -- finite type\n true : Bool\n false : Bool\n\nidBool : Bool \u2192 Bool -- lambda\nidBool x = x\n\nalwaysTrue : Bool \u2192 Bool\nalwaysTrue x = true\n\nnot : Bool \u2192 Bool -- case defn\nnot true = false\nnot false = true\n\nnotnot : Bool \u2192 Bool -- lambda\nnotnot x = not(not(x))\n\n_&_ : Bool \u2192 Bool \u2192 Bool --curried function\ntrue & x = x\nfalse & _ = false\n\ndata \u2115 : Type where -- infinite type\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n\neven : \u2115 \u2192 Bool -- recursive definition\neven zero = true\neven (succ x) = not (even x)\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115 \nzero + y = y\nsucc x + y = x + succ y\n\n{-# BUILTIN NATURAL \u2115 #-}\n\ndata \u2115List : Type where --list type \n [] : \u2115List -- empty list \n _::_ : \u2115 \u2192 \u2115List \u2192 \u2115List -- add number to head of list\n\nmylist : \u2115List \nmylist = 3 :: (4 :: (2 :: [])) -- the list [3, 4, 2]\n\ndata Vector : \u2115 \u2192 Type where -- type family\n [] : Vector 0\n _::_ : {n : \u2115} \u2192 \u2115 \u2192 Vector n \u2192 Vector (succ n) \n\nsum : {n : \u2115} \u2192 Vector n \u2192 \u2115\nsum [] = 0\nsum (x :: l) = x + sum l\n\n\ncountdown : (n : \u2115) \u2192 Vector n -- dependent function\ncountdown 0 = []\ncountdown (succ n) = (succ n) :: (countdown n)\n\nsumToN : \u2115 \u2192 \u2115 -- calculation\nsumToN n = sum(countdown n)\n\ndata isEven : \u2115 \u2192 Type where\n 0even : isEven 0\n +2even : (n : \u2115) \u2192 isEven n \u2192 isEven (succ(succ(n)))\n\n4even : isEven 4\n4even = +2even _ (+2even _ 0even)\n\ndata False : Type where\n\n1odd : isEven 1 \u2192 False\n1odd ()\n\n3odd : isEven 3 \u2192 False\n3odd (+2even .1 ())\n\nhalf : (n : \u2115) \u2192 isEven n \u2192 \u2115\nhalf .0 0even = 0\nhalf .(succ (succ n)) (+2even n pf) = half n pf\n\ndouble : (n : \u2115) \u2192 \u2115\ndouble 0 = 0\ndouble (succ n) = succ(succ(double(n)))\n\nthm : (n : \u2115) \u2192 isEven (double n)\nthm zero = 0even\nthm (succ n) = +2even _ (thm n)\n\nhalfOfDouble : \u2115 \u2192 \u2115\nhalfOfDouble n = half (double n) (thm n)\n","old_contents":"open import Base\n\nmodule GoaTest where\n\ndata Bool : Type where -- finite type\n true : Bool\n false : Bool\n\nidBool : Bool \u2192 Bool -- lambda\nidBool x = x\n\nalwaysTrue : Bool \u2192 Bool\nalwaysTrue x = true\n\nnot : Bool \u2192 Bool -- case defn\nnot true = false\nnot false = true\n\nnotnot : Bool \u2192 Bool -- lambda\nnotnot x = not(not(x))\n\n_&_ : Bool \u2192 Bool \u2192 Bool --curried function\ntrue & x = x\nfalse & _ = false\n\ndata \u2115 : Type where -- infinite type\n zero : \u2115\n succ : \u2115 \u2192 \u2115\n\neven : \u2115 \u2192 Bool -- recursive definition\neven zero = true\neven (succ x) = not (even x)\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115 \nzero + y = y\nsucc x + y = x + succ y\n\n{-# BUILTIN NATURAL \u2115 #-}\n\ndata \u2115List : Type where --list type \n [] : \u2115List -- empty list \n _::_ : \u2115 \u2192 \u2115List \u2192 \u2115List -- add number to head of list\n\nmylist : \u2115List \nmylist = 3 :: (4 :: (2 :: [])) -- the list [3, 4, 2]\n\ndata Vector : \u2115 \u2192 Type where -- type family\n [] : Vector 0\n _::_ : {n : \u2115} \u2192 \u2115 \u2192 Vector n \u2192 Vector (succ n) \n\nsum : {n : \u2115} \u2192 Vector n \u2192 \u2115\nsum [] = 0\nsum (x :: l) = x + sum l\n\n\ncountdown : (n : \u2115) \u2192 Vector n -- dependent function\ncountdown 0 = []\ncountdown (succ n) = (succ n) :: (countdown n)\n\nsumToN : \u2115 \u2192 \u2115 -- calculation\nsumToN n = sum(countdown n)\n\ndata isEven : \u2115 \u2192 Type where\n 0even : isEven 0\n +2even : (n : \u2115) \u2192 isEven n \u2192 isEven (succ(succ(n)))\n\n4even : isEven 4\n4even = +2even _ (+2even _ 0even)\n\ndata False : Type where\n\n1odd : isEven 1 \u2192 False\n1odd ()\n\n3odd : isEven 3 \u2192 False\n3odd (+2even .1 ())\n\nhalf : (n : \u2115) \u2192 isEven n \u2192 \u2115\nhalf .0 0even = 0\nhalf .(succ (succ n)) (+2even n pf) = half n pf\n\ndouble : (n : \u2115) \u2192 \u2115\ndouble 0 = 0\ndouble (succ n) = succ(succ(double(n)))\n\nstep : (n : \u2115) \u2192 (isEven (double n)) \u2192 isEven (double(succ(n)))\nstep n pf = +2even _ pf\n\nthm : (n : \u2115) \u2192 isEven (double n)\nthm zero = 0even\nthm (succ n) = step _ (thm n)\n\nhalfOfDouble : \u2115 \u2192 \u2115\nhalfOfDouble n = half (double n) (thm n)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"dd7fc1faaff45990fdfb94040dc68dcdfadf86f0","subject":"Added doc (the bisimilarity relation _\u2248_ is on unbounded lists).","message":"Added doc (the bisimilarity relation _\u2248_ is on unbounded lists).\n\nIgnore-this: dbbdf0d52a1b6a25c1ae4a742876f1b0\n\ndarcs-hash:20120322232946-3bd4e-379500496257d6c261ce7494f21a26034b65f876.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Relation\/Binary\/Bisimilarity.agda","new_file":"src\/FOTC\/Relation\/Binary\/Bisimilarity.agda","new_contents":"------------------------------------------------------------------------------\n-- Bisimilarity relation on unbounded lists\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Relation.Binary.Bisimilarity where\n\nopen import FOTC.Base\n\n-- We add 3 to the fixities of the standard library.\ninfix 7 _\u2248_\n\n------------------------------------------------------------------------------\n-- The bisimilarity relation _\u2248_ on unbounded lists is the greatest\n-- fixed point (by \u2248-gfp\u2081 and \u2248-gfp\u2082) of the bisimulation functional\n-- (see below).\n\n-- The bisimilarity relation on unbounded lists.\npostulate\n _\u2248_ : D \u2192 D \u2192 Set\n\n-- The bisimilarity relation _\u2248_ on unbounded lists is a post-fixed\n-- point of the bisimulation functional (see below).\npostulate\n \u2248-gfp\u2081 : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n{-# ATP axiom \u2248-gfp\u2081 #-}\n\n-- The bisimilarity relation _\u2248_ on unbounded lists is the greatest\n-- post-fixed point of the bisimulation functional (see below).\n--\n-- N.B. This is an axiom schema. Because in the automatic proofs we\n-- *must* use an instance, we do not add this postulate as an ATP\n-- axiom.\npostulate\n \u2248-gfp\u2082 : (_R_ : D \u2192 D \u2192 Set) \u2192\n -- R is a post-fixed point of the bisimulation functional.\n (\u2200 {xs ys} \u2192 xs R ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys') \u2192\n -- _\u2248_ is greater than R.\n \u2200 {xs ys} \u2192 xs R ys \u2192 xs \u2248 ys\n\n-- Because a greatest post-fixed point is a fixed-point, the\n-- bisimilarity relation _\u2248_ on unbounded lists is also a pre-fixed\n-- point of the bisimulation functional (see below).\n\u2248-gfp\u2083 : \u2200 {xs ys} \u2192\n (\u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys') \u2192\n xs \u2248 ys\n\u2248-gfp\u2083 h = \u2248-gfp\u2082 _R_ helper h\n where\n _R_ : D \u2192 D \u2192 Set\n _R_ xs ys = \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n\n helper : \u2200 {xs ys} \u2192 xs R ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n helper (_ , _ , _ , xs'\u2248ys' , prf) = _ , _ , _ , \u2248-gfp\u2081 xs'\u2248ys' , prf\n\nprivate\n module Bisimulation where\n -- In FOTC we won't use the bisimulation functional on unbounded\n -- lists. This module is only for illustrative purposes.\n\n -- References:\n --\n -- \u2022 Peter Dybjer and Herbert Sander. A functional programming\n -- approach to the specification and verification of concurrent\n -- systems. Formal Aspects of Computing, 1:303\u2013319, 1989.\n --\n -- \u2022 Bart Jacobs and Jan Rutten. (Co)algebras and\n -- (co)induction. EATCS Bulletin, 62:222\u2013259, 1997.\n\n ----------------------------------------------------------------------------\n -- The bisimilarity relation _\u2248_ on unbounded lists is the greatest\n -- post-fixed point of Bisimulation (by post-fp and gpfp).\n\n -- The bisimulation functional on unbounded lists (adapted from\n -- Dybjer and Sander 1989, p. 310, and Jacobs and Rutten 1997,\n -- p. 30).\n BisimulationF : (D \u2192 D \u2192 Set) \u2192 D \u2192 D \u2192 Set\n BisimulationF _R_ xs ys =\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n\n -- The bisimilarity relation _\u2248_ on unbounded lists is a post-fixed\n -- point of Bisimulation, i.e,\n --\n -- _\u2248_ \u2264 Bisimulation _\u2248_.\n post-fp : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 BisimulationF _\u2248_ xs ys\n post-fp = \u2248-gfp\u2081\n\n -- The bisimilarity relation _\u2248_ on unbounded lists is the greatest\n -- post-fixed point of Bisimulation, i.e\n --\n -- \u2200 R. R \u2264 Bisimulation R \u21d2 R \u2264 _\u2248_.\n gpfp : (_R_ : D \u2192 D \u2192 Set) \u2192\n -- R is a post-fixed point of Bisimulation.\n (\u2200 {xs ys} \u2192 xs R ys \u2192 BisimulationF _R_ xs ys) \u2192\n -- _\u2248_ is greater than R.\n \u2200 {xs ys} \u2192 xs R ys \u2192 xs \u2248 ys\n gpfp = \u2248-gfp\u2082\n\n -- Because a greatest post-fixed point is a fixed-point, the\n -- bisimilarity relation _\u2248_ on unbounded lists is also a pre-fixed\n -- point of Bisimulation, i.e.\n --\n -- Bisimulation _\u2248_ \u2264 _\u2248_.\n pre-fp : \u2200 {xs ys} \u2192 BisimulationF _\u2248_ xs ys \u2192 xs \u2248 ys\n pre-fp = \u2248-gfp\u2083\n","old_contents":"------------------------------------------------------------------------------\n-- Bisimilarity relation\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\nmodule FOTC.Relation.Binary.Bisimilarity where\n\nopen import FOTC.Base\n\n-- We add 3 to the fixities of the standard library.\ninfix 7 _\u2248_\n\n------------------------------------------------------------------------------\n-- The bisimilarity relation _\u2248_ is the greatest fixed point (by\n-- \u2248-gfp\u2081 and \u2248-gfp\u2082) of the bisimulation functional (see below).\n\n-- The bisimilarity relation.\npostulate\n _\u2248_ : D \u2192 D \u2192 Set\n\n-- The bisimilarity relation _\u2248_ is a post-fixed point of the\n-- bisimulation functional (see below).\npostulate\n \u2248-gfp\u2081 : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n{-# ATP axiom \u2248-gfp\u2081 #-}\n\n-- The bisimilarity relation _\u2248_ is the greatest post-fixed point of\n-- the bisimulation functional (see below).\n--\n-- N.B. This is an axiom schema. Because in the automatic proofs we\n-- *must* use an instance, we do not add this postulate as an ATP\n-- axiom.\npostulate\n \u2248-gfp\u2082 : (_R_ : D \u2192 D \u2192 Set) \u2192\n -- R is a post-fixed point of the bisimulation functional.\n (\u2200 {xs ys} \u2192 xs R ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys') \u2192\n -- _\u2248_ is greater than R.\n \u2200 {xs ys} \u2192 xs R ys \u2192 xs \u2248 ys\n\n-- Because a greatest post-fixed point is a fixed-point, the\n-- bisimilarity relation _\u2248_ is also a pre-fixed point of the\n-- bisimulation functional (see below).\n\u2248-gfp\u2083 : \u2200 {xs ys} \u2192\n (\u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ]\n xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys') \u2192\n xs \u2248 ys\n\u2248-gfp\u2083 h = \u2248-gfp\u2082 _R_ helper h\n where\n _R_ : D \u2192 D \u2192 Set\n _R_ xs ys = \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' \u2248 ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n\n helper : \u2200 {xs ys} \u2192 xs R ys \u2192\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n helper (_ , _ , _ , xs'\u2248ys' , prf) = _ , _ , _ , \u2248-gfp\u2081 xs'\u2248ys' , prf\n\nprivate\n module Bisimulation where\n -- In FOTC we won't use the bisimulation functional. This module is\n -- only for illustrative purposes.\n\n -- References:\n --\n -- \u2022 Peter Dybjer and Herbert Sander. A functional programming\n -- approach to the specification and verification of concurrent\n -- systems. Formal Aspects of Computing, 1:303\u2013319, 1989.\n --\n -- \u2022 Bart Jacobs and Jan Rutten. (Co)algebras and\n -- (co)induction. EATCS Bulletin, 62:222\u2013259, 1997.\n\n ----------------------------------------------------------------------------\n -- Adapted from (Dybjer and Sander 1989, p. 310). In this paper, the\n -- authors use the name\n\n -- as (R :: R') bs'\n\n -- for the bisimulation functional.\n\n -- The bisimilarity relation _\u2248_ is the greatest post-fixed point of\n -- Bisimulation (by post-fp and gpfp).\n\n -- The bisimulation functional (Jacobs and Rutten 1997, p. 30).\n BisimulationF : (D \u2192 D \u2192 Set) \u2192 D \u2192 D \u2192 Set\n BisimulationF _R_ xs ys =\n \u2203[ x' ] \u2203[ xs' ] \u2203[ ys' ] xs' R ys' \u2227 xs \u2261 x' \u2237 xs' \u2227 ys \u2261 x' \u2237 ys'\n\n -- The bisimilarity relation _\u2248_ is a post-fixed point of\n -- Bisimulation, i.e,\n --\n -- _\u2248_ \u2264 Bisimulation _\u2248_.\n post-fp : \u2200 {xs ys} \u2192 xs \u2248 ys \u2192 BisimulationF _\u2248_ xs ys\n post-fp = \u2248-gfp\u2081\n\n -- The bisimilarity relation _\u2248_ is the greatest post-fixed point of\n -- Bisimulation, i.e\n --\n -- \u2200 R. R \u2264 Bisimulation R \u21d2 R \u2264 _\u2248_.\n gpfp : (_R_ : D \u2192 D \u2192 Set) \u2192\n -- R is a post-fixed point of Bisimulation.\n (\u2200 {xs ys} \u2192 xs R ys \u2192 BisimulationF _R_ xs ys) \u2192\n -- _\u2248_ is greater than R.\n \u2200 {xs ys} \u2192 xs R ys \u2192 xs \u2248 ys\n gpfp = \u2248-gfp\u2082\n\n -- Because a greatest post-fixed point is a fixed-point, the\n -- bisimilarity relation _\u2248_ is also a pre-fixed point of\n -- Bisimulation, i.e.\n --\n -- Bisimulation _\u2248_ \u2264 _\u2248_.\n pre-fp : \u2200 {xs ys} \u2192 BisimulationF _\u2248_ xs ys \u2192 xs \u2248 ys\n pre-fp = \u2248-gfp\u2083\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"f5c0e7addd33faf63ba9346cb7a85c7384176846","subject":"Added two automatic proofs.","message":"Added two automatic proofs.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Data\/Conat\/PropertiesATP.agda","new_file":"src\/fot\/FOTC\/Data\/Conat\/PropertiesATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Conat properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- References:\n--\n-- \u2022 Herbert P. Sander. A logic of functional programs with an\n-- application to concurrency. PhD thesis, Chalmers University of\n-- Technology and University of Gothenburg, Department of Computer\n-- Sciences, 1992.\n\nmodule FOTC.Data.Conat.PropertiesATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Conat\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n\n0-Conat : Conat zero\n0-Conat = Conat-coind P h refl\n where\n P : D \u2192 Set\n P n = n \u2261 zero\n {-# ATP definition P #-}\n\n postulate h : \u2200 {n} \u2192 P n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] P n' \u2227 n \u2261 succ\u2081 n')\n {-# ATP prove h #-}\n\n-- Adapted from (Sander 1992, p. 57).\n\u221e-Conat : Conat \u221e\n\u221e-Conat = Conat-coind P h refl\n where\n P : D \u2192 Set\n P n = n \u2261 \u221e\n {-# ATP definition P #-}\n\n postulate h : \u2200 {n} \u2192 P n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] P n' \u2227 n \u2261 succ\u2081 n')\n {-# ATP prove h #-}\n\nN\u2192Conat : \u2200 {n} \u2192 N n \u2192 Conat n\nN\u2192Conat Nn = Conat-coind N h Nn\n where\n h : \u2200 {m} \u2192 N m \u2192 m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 N m' \u2227 m \u2261 succ\u2081 m')\n h nzero = prf\n where postulate prf : zero \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 N m' \u2227 zero \u2261 succ\u2081 m')\n {-# ATP prove prf #-}\n h (nsucc {m} Nm) = prf\n where postulate prf : succ\u2081 m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 N m' \u2227 succ\u2081 m \u2261 succ\u2081 m')\n {-# ATP prove prf #-}\n","old_contents":"------------------------------------------------------------------------------\n-- Conat properties\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- References:\n--\n-- \u2022 Herbert P. Sander. A logic of functional programs with an\n-- application to concurrency. PhD thesis, Chalmers University of\n-- Technology and University of Gothenburg, Department of Computer\n-- Sciences, 1992.\n\nmodule FOTC.Data.Conat.PropertiesATP where\n\nopen import FOTC.Base\nopen import FOTC.Data.Conat\nopen import FOTC.Data.Nat\n\n------------------------------------------------------------------------------\n\n0-Conat : Conat zero\n0-Conat = Conat-coind P h refl\n where\n P : D \u2192 Set\n P n = n \u2261 zero\n {-# ATP definition P #-}\n\n postulate h : \u2200 {n} \u2192 P n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] P n' \u2227 n \u2261 succ\u2081 n')\n {-# ATP prove h #-}\n\n-- Adapted from (Sander 1992, p. 57).\n\u221e-Conat : Conat \u221e\n\u221e-Conat = Conat-coind P h refl\n where\n P : D \u2192 Set\n P n = n \u2261 \u221e\n {-# ATP definition P #-}\n\n postulate h : \u2200 {n} \u2192 P n \u2192 n \u2261 zero \u2228 (\u2203[ n' ] P n' \u2227 n \u2261 succ\u2081 n')\n {-# ATP prove h #-}\n\nN\u2192Conat : \u2200 {n} \u2192 N n \u2192 Conat n\nN\u2192Conat Nn = Conat-coind N h Nn\n where\n h : \u2200 {m} \u2192 N m \u2192 m \u2261 zero \u2228 \u2203 (\u03bb m' \u2192 N m' \u2227 m \u2261 succ\u2081 m')\n h nzero = inj\u2081 refl\n h (nsucc {m} Nm) = inj\u2082 (m , Nm , refl)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a3df42670b0f46a7af869da9ff572d148b28bb7d","subject":"Fix type inference problem that arose","message":"Fix type inference problem that arose\n\nOnly noticed when tried removing --allow-unsolved-metas. That seems a habit for\nAgda.\n","repos":"inc-lc\/ilc-agda","old_file":"Thesis\/FunBigStepSILR2.agda","new_file":"Thesis\/FunBigStepSILR2.agda","new_contents":"{-# OPTIONS --allow-unsolved-metas #-}\n-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- But to betray the eventual goal, I can also relate integer values with a\n-- change in the relation witness. That was a completely local change. But that\n-- might also be because we only have few primitives.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\" (ISAC below), POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\n-- In fact, this development is typed, hence some parts of the model are closer\n-- to Ahmed (ESOP 2006), \"Step-Indexed Syntactic Logical Relations for Recursive\n-- and Quantified Types\". But for many relevant aspects, the two papers are\n-- interchangeable.\n--\n-- The main insight from the ISAC paper missing from the other one is how to\n-- step-index a big-step semantics correctly: just ensure that the steps in the\n-- big-step semantics agree with the ones in the small-step semantics. *Then*\n-- everything just works with big-step semantics. Quite a few other details are\n-- fiddly, but those are the same in small-step semantics.\n--\n-- CHEATS:\n-- \"Fuctional big-step semantics\" requires an external termination proof for the\n-- semantics. There it is also mechanized, here it isn't. Worse, the same\n-- termination problem affects some lemmas about the semantics.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen import Data.Nat.Properties.Simple\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\ninfixr 20 _\u21d2_\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n\n-- WARNING: ISAC's big-step semantics produces a step count as \"output\". But\n-- that would not help Agda establish termination. That's only a problem for a\n-- functional big-step semantics, not for a relational semantics.\n--\n-- So, instead, I tried to use a sort of writer monad: the interpreter gets fuel\n-- and returns the remaining fuel. That's the same trick as in \"functional\n-- big-step semantics\". That *makes* the function terminating, even though Agda\n-- cannot see this because it does not know that the returned fuel is no bigger.\n\n-- Since we focus for now on STLC, unlike that\n-- paper, we could avoid error values because we keep types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) n = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\neval (app s t) \u03c1 (suc n) = (eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)) n\n\neval-const-dec : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-const-dec (lit v) n0 .n0 refl = \u2264-refl\n\n{-# TERMINATING #-}\neval-dec : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-dec (const c) \u03c1 v n0 n1 eq = eval-const-dec c n0 n1 eq\neval-dec (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (app s t) \u03c1 v zero n1 ()\neval-dec (app s t) \u03c1 v (suc n0) n3 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv sn1 | [ seq ] with eval t \u03c1 sn1 | inspect (eval t \u03c1) sn1\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv@(closure st s\u03c1) sn1 | [ seq ] | (Done tv tn2) | [ teq ] = \u2264-step (\u2264-trans (\u2264-trans (eval-dec st _ _ _ _ eq) (eval-dec t _ _ _ _ teq)) (eval-dec s _ _ _ _ seq))\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | Error | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | TimeOut | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Error | [ seq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | TimeOut | [ seq ]\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1)\neval-const-mono (lit v) n0 .n0 refl = refl\n\n-- ARGH\n{-# TERMINATING #-}\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1)\neval-mono (const c) \u03c1 v n0 n1 eq = eval-const-mono c n0 n1 eq\neval-mono (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\neval-mono (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\neval-mono (app s t) \u03c1 v zero n1 ()\neval-mono (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-mono t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n\neval-adjust-plus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1)\neval-adjust-plus zero t \u03c1 v n0 n1 eq = eq\neval-adjust-plus (suc d) t \u03c1 v n0 n1 eq = eval-mono t \u03c1 v (d + n0) (d + n1) (eval-adjust-plus d t \u03c1 v n0 n1 eq)\n\neval-const-strengthen : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1) \u2192 evalConst c n0 \u2261 Done v n1\neval-const-strengthen (lit v) n0 .n0 refl = refl\n\n-- I started trying to prove eval-strengthen, which I appeal to informally\n-- below, but I gave up. I still guess the lemma is true but proving it looks\n-- too painful to bother.\n\n-- Without this lemma, I can't fully prove that this logical relation is\n-- equivalent to the original one.\n-- But this one works (well, at least up to the fundamental theorem, haven't\n-- attempted other lemmas), so it should be good enough.\n\n-- eval-mono-err : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 n \u2192 eval t \u03c1 n \u2261 Error \u2192 eval t \u03c1 (suc n) \u2261 Error\n-- eval-mono-err (const (lit x)) \u03c1 zero eq = {!!}\n-- eval-mono-err (const (lit x)) \u03c1 (suc n) eq = {!!}\n-- eval-mono-err (var x) \u03c1 n eq = {!!}\n-- eval-mono-err (app t t\u2081) \u03c1 n eq = {!!}\n-- eval-mono-err (abs t) \u03c1 n eq = {!!}\n\n-- -- eval t \u03c1 (suc n0) \u2261 Done v (suc n1) \u2192 eval t \u03c1 n0 \u2261 Done v n\n-- eval-aux : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 n \u2192 (\u03a3[ res0 \u2208 ErrVal \u03c4 ] eval t \u03c1 n \u2261 res0) \u00d7 (\u03a3[ resS \u2208 ErrVal \u03c4 ] eval t \u03c1 n \u2261 resS)\n-- eval-aux t \u03c1 n with\n-- eval t \u03c1 n | inspect (eval t \u03c1) n |\n-- eval t \u03c1 (suc n) | inspect (eval t \u03c1) (suc n)\n-- eval-aux t \u03c1 n | res0 | [ eq0 ] | (Done v1 n1) | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | res0 | [ eq0 ] | Error | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | Done v n1 | [ eq0 ] | TimeOut | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | Error | [ eq0 ] | TimeOut | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | TimeOut | [ eq0 ] | TimeOut | [ eq1 ] = (TimeOut , refl) , (TimeOut , refl)\n\n-- {-# TERMINATING #-}\n-- eval-strengthen : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-strengthen (const c) \u03c1 v n0 n1 eq = eval-const-strengthen c n0 n1 eq\n-- eval-strengthen (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq with eval s \u03c1 0 | inspect (eval s \u03c1) 0\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done sv sn1 | [ seq ] with eval-dec s \u03c1 sv 0 sn1 seq\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done sv .0 | [ seq ] | z\u2264n with eval t \u03c1 0 | inspect (eval t \u03c1) 0\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done sv _ | [ seq ] | z\u2264n | Done tv tn1 | [ teq ] with eval-dec t \u03c1 tv 0 tn1 teq\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done (closure st s\u03c1) _ | [ seq ] | z\u2264n | (Done tv .0) | [ teq ] | z\u2264n with eval-dec st _ v 0 (suc n1) eq\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done (closure st s\u03c1) _ | [ seq ] | z\u2264n | (Done tv _) | [ teq ] | z\u2264n | ()\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | Done sv _ | [ seq ] | z\u2264n | Error | [ teq ]\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | Done sv _ | [ seq ] | z\u2264n | TimeOut | [ teq ]\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | Error | [ seq ]\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | TimeOut | [ seq ]\n-- -- eval-dec s \u03c1\n-- -- {!eval-dec s \u03c1 ? (suc zero) (suc n1) !}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 (suc n0) | inspect (eval s \u03c1) (suc n0)\n-- -- eval-strengthen (app s t) \u03c1 v\u2081 (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 n0 = {!eval-strengthen s \u03c1 v n0 n1 seq !}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-strengthen t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | Error | [ seq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | TimeOut | [ seq ] = {!!}\n\n-- eval-adjust-minus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 {\u03c1 v} n0 n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-adjust-minus zero t n0 n1 eq = eq\n-- eval-adjust-minus (suc d) t n0 n1 eq = eval-adjust-minus d t n0 n1 (eval-strengthen t _ _ (d + n0) (d + n1) eq)\n\nimport Data.Integer as I\nopen I using (\u2124)\nmutual\n -- Warning: compared to Ahmed's papers, this definition for relT also requires\n -- t1 to be well-typed, not just t2.\n --\n -- This difference might affect the status of some proofs in Ahmed's papers,\n -- but that's not a problem here.\n\n -- Also: can't confirm this in any of the papers I'm using, but I'd guess that\n -- all papers using environments allow to relate closures with different\n -- implementations and different hidden environments.\n --\n -- To check if the proof goes through with equal context, I changed the proof.\n -- Now a proof that two closures are equivalent contains a proof that their\n -- typing contexts are equivalent. The changes were limited softawre\n -- engineering, the same proofs go through.\n\n -- This is not the same definition of relT, but it is equivalent.\n relT : \u2200 {\u03c4 \u0393} (t1 : Term \u0393 \u03c4) (t2 : Term \u0393 \u03c4) (\u03c11 : \u27e6 \u0393 \u27e7Context) (\u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\n -- This equation is a lemma in the original definition.\n relT t1 t2 \u03c11 \u03c12 zero = \u22a4\n -- To compare this definition, note that the original k is suc n here.\n relT {\u03c4} t1 t2 \u03c11 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n -- Originally we have 0 \u2264 j < k, so j < suc n, so k - j = suc n - j.\n -- It follows that 0 < k - j \u2264 k, hence suc n - j \u2264 suc n, or n - j \u2264 n.\n -- Here, instead of binding j we bind n-j = n - j, require n - j \u2264 n, and\n -- use suc n-j instead of k - j.\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n -- The next assumption is important. This still says that evaluation consumes j steps.\n -- Since j \u2264 n, it is OK to start evaluation with n steps.\n -- Starting with (suc n) and getting suc n-j is equivalent, per eval-mono\n -- and eval-strengthen. But in practice this version is easier to use.\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 0 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n -- Here, computing t2 is allowed to take an unbounded number of steps. Having to write a number at all is annoying.\n\n relV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n -- Show the proof still goes through if we relate clearly different values by\n -- inserting changes in the relation.\n -- There's no syntax to produce such changes, but you can add changes to the\n -- environment.\n relV nat (intV v1) (intV v2) n = \u03a3[ dv \u2208 \u2124 ] dv I.+ (I.+ v1) \u2261 (I.+ v2)\n relV (\u03c3 \u21d2 \u03c4) (closure {\u03931} t1 \u03c11) (closure {\u03932} t2 \u03c12) n =\n \u03a3[ \u2261\u0393 \u2208 \u03931 \u2261 \u03932 ]\n \u2200 (k : \u2115) (k\u2264n : k < n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT\n t1\n (subst (\u03bb \u0393 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4) (sym \u2261\u0393) t2)\n (v1 \u2022 \u03c11)\n (subst (\u03bb \u0393 \u2192 \u27e6 \u03c3 \u2022 \u0393 \u27e7Context) (sym \u2261\u0393) (v2 \u2022 \u03c12))\n k\n -- Above, in the conclusion, I'm not relating app (closure t1 \u03c11) v1 with app\n -- (closure t2 \u03c12) v2 (or some encoding of that that actually works), but the\n -- result of taking a step from that configuration. That is important, because\n -- both Pitts' \"Step-Indexed Biorthogonality: a Tutorial Example\" and\n -- \"Imperative Self-Adjusting Computation\" do the same thing (and point out it's\n -- important).\n\n \u0394\u03c4 : Type \u2192 Type\n \u0394\u03c4 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 (\u0394\u03c4 \u03c3) \u21d2 \u0394\u03c4 \u03c4\n \u0394\u03c4 nat = nat\n\n -- Since the original relation allows unrelated environments, we do that here\n -- too. However, while that is fine as a logical relation, it's not OK if we\n -- want to prove that validity agrees with oplus.\n relT3 : \u2200 {\u03c4 \u03931 \u03932 \u0394\u0393} (t1 : Term \u03931 \u03c4) (dt : Term \u0394\u0393 (\u0394\u03c4 \u03c4)) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (d\u03c1 : \u27e6 \u0394\u0393 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\n relT3 t1 dt t2 \u03c11 d\u03c1 \u03c12 zero = \u22a4\n relT3 {\u03c4} t1 dt t2 \u03c11 d\u03c1 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 0 \u00d7\n \u03a3[ dv \u2208 Val (\u0394\u03c4 \u03c4) ] \u03a3[ dn \u2208 \u2115 ] eval dt d\u03c1 dn \u2261 Done dv 0 \u00d7\n relV3 \u03c4 v1 dv v2 (suc n-j)\n\n relV3 : \u2200 \u03c4 (v1 : Val \u03c4) (dv : Val (\u0394\u03c4 \u03c4)) (v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n relV3 nat (intV v1) (intV dv) (intV v2) n = dv + v1 \u2261 v2\n relV3 (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure dt d\u03c1) (closure t2 \u03c12) n =\n \u2200 (k : \u2115) (k\u2264n : k < n) v1 dv v2 \u2192\n relV3 \u03c3 v1 dv v2 k \u2192\n relT3 t1 {!dt!} t2 (v1 \u2022 \u03c11) d\u03c1 (v2 \u2022 \u03c12) k\n\n -- Relate \u03bb x \u2192 0 and \u03bb x \u2192 1 at any step count.\n example1 : \u2200 n \u2192 relV (nat \u21d2 nat) (closure (const (lit 0)) \u2205) (closure (const (lit 1)) \u2205) n\n example1 n = refl ,\n \u03bb { zero k\u2264n v1 v2 x \u2192 tt\n ; (suc k) k\u2264n v1 v2 x .(intV 0) .k n-j\u2264n refl \u2192 intV 1 , 0 , refl , (I.+ 1 , refl)\n }\n\n -- Relate \u03bb x \u2192 0 and \u03bb x \u2192 x at any step count.\n example2 : \u2200 n \u2192 relV (nat \u21d2 nat) (closure (const (lit 0)) \u2205) (closure (var this) \u2205) n\n example2 n = refl ,\n \u03bb { zero k\u2264n v1 v2 x \u2192 tt\n ; (suc k) k\u2264n (intV v1) (intV v2) x .(intV 0) .k n-j\u2264n refl \u2192 intV v2 , 0 , refl , (I.+ v2 , cong I.+_ (+-right-identity v2))\n }\n\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono m n m\u2264n nat (intV v1) (intV v2) vv = vv\nrelV-mono m n m\u2264n (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (refl , ff) = refl , \u03bb k k\u2264m \u2192 ff k (\u2264-trans k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 .n n-j\u2264n refl = v2 , zero , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\nlt1 : \u2200 {k n} \u2192 k < n \u2192 k \u2264 n\nlt1 (s\u2264s p) = \u2264-step p\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\nfundamental (const (lit v)) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(intV v) .n n-j\u2264n refl = intV v , zero , refl , I.+ zero , refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(closure t \u03c11) .n n-j\u2264n refl = closure t \u03c12 , zero , refl , refl , \u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k (suc n) (lt1 k\u2264n) _ _ _ \u03c1\u03c1)\nfundamental (app s t) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq with eval s \u03c11 n | inspect (eval s \u03c11) n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done sv1 n1 | [ s1eq ] with eval-dec s _ _ n n1 s1eq | eval t \u03c11 n1 | inspect (eval t \u03c11) n1\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done (closure st1 s\u03c11) n1 | [ s1eq ] | n1\u2264n | Done tv1 n2 | [ t1eq ] with eval-dec t _ _ n1 n2 t1eq\n... | n2\u2264n1 with fundamental s (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) (suc n1) (s\u2264s n1\u2264n) (eval-mono s \u03c11 (closure st1 s\u03c11) n n1 s1eq)\n | fundamental t (suc (suc n1)) \u03c11 \u03c12 (rel\u03c1-mono (suc (suc n1)) (suc (suc n)) (s\u2264s (s\u2264s n1\u2264n)) _ _ _ \u03c1\u03c1) tv1 (suc n2) (s\u2264s n2\u2264n1) (eval-mono t \u03c11 tv1 n1 n2 t1eq)\n... | sv2@(closure st2 s\u03c12) , sn3 , s2eq , refl , svv | tv2 , tn3 , t2eq , tvv with svv (suc n2) (s\u2264s (s\u2264s n2\u2264n1)) tv1 tv2 (relV-mono (suc n2) (suc (suc n2)) (s\u2264s (n\u22641+n n2)) _ tv1 tv2 tvv) v1 n-j (eval-dec st1 _ _ _ _ eq) eq\n... | v2 , n3 , eq2 , vv = v2 , suc (sn3 + (tn3 + n3)) , comp , vv\n where\n s2eq-adj : eval s \u03c12 (sn3 + (tn3 + n3)) \u2261 Done (closure st2 s\u03c12) (tn3 + n3)\n s2eq-adj rewrite +-comm sn3 (tn3 + n3)| cong (Done (closure st2 s\u03c12)) (sym (+-right-identity (tn3 + n3))) = eval-adjust-plus (tn3 + n3) s _ sv2 _ _ s2eq\n t2eq-adj : eval t \u03c12 (tn3 + n3) \u2261 Done tv2 n3\n t2eq-adj rewrite +-comm tn3 n3 | cong (Done tv2) (sym (+-right-identity n3)) = eval-adjust-plus n3 t _ tv2 _ _ t2eq\n\n comp : (eval s \u03c12 >>= (\u03bb sv \u2192 eval t \u03c12 >>= apply sv))\n (sn3 + (tn3 + n3))\n \u2261 Done v2 0\n comp rewrite s2eq-adj | t2eq-adj = eq2\n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | Error | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | TimeOut | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Error | [ s1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | TimeOut | [ s1eq ]\n","old_contents":"{-# OPTIONS --allow-unsolved-metas #-}\n-- Step-indexed logical relations based on functional big-step semantics.\n--\n-- Goal for now: just prove the fundamental theorem of logical relations,\n-- relating a term to itself in a different environments.\n--\n-- But to betray the eventual goal, I can also relate integer values with a\n-- change in the relation witness. That was a completely local change. But that\n-- might also be because we only have few primitives.\n--\n-- Because of closures, we need relations across different terms with different\n-- contexts and environments.\n--\n-- This development is strongly inspired by \"Imperative self-adjusting\n-- computation\" (ISAC below), POPL'08, in preference to Dargaye and Leroy (2010), \"A verified\n-- framework for higher-order uncurrying optimizations\", but I deviate\n-- somewhere, especially to try following \"Functional Big-Step Semantics\"),\n-- though I deviate somewhere.\n\n-- In fact, this development is typed, hence some parts of the model are closer\n-- to Ahmed (ESOP 2006), \"Step-Indexed Syntactic Logical Relations for Recursive\n-- and Quantified Types\". But for many relevant aspects, the two papers are\n-- interchangeable.\n--\n-- The main insight from the ISAC paper missing from the other one is how to\n-- step-index a big-step semantics correctly: just ensure that the steps in the\n-- big-step semantics agree with the ones in the small-step semantics. *Then*\n-- everything just works with big-step semantics. Quite a few other details are\n-- fiddly, but those are the same in small-step semantics.\n--\n-- CHEATS:\n-- \"Fuctional big-step semantics\" requires an external termination proof for the\n-- semantics. There it is also mechanized, here it isn't. Worse, the same\n-- termination problem affects some lemmas about the semantics.\n\nmodule Thesis.FunBigStepSILR2 where\n\nopen import Data.Empty\nopen import Data.Unit.Base hiding (_\u2264_)\nopen import Data.Product\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary hiding (_\u21d2_)\nopen import Data.Nat -- using (\u2115; zero; suc; decTotalOrder; _<_; _\u2264_)\nopen import Data.Nat.Properties\nopen import Data.Nat.Properties.Simple\nopen DecTotalOrder Data.Nat.decTotalOrder using () renaming (refl to \u2264-refl; trans to \u2264-trans)\n\ndata Type : Set where\n _\u21d2_ : (\u03c3 \u03c4 : Type) \u2192 Type\n nat : Type\ninfixr 20 _\u21d2_\n\n\u27e6_\u27e7Type : Type \u2192 Set\n\u27e6 \u03c3 \u21d2 \u03c4 \u27e7Type = \u27e6 \u03c3 \u27e7Type \u2192 \u27e6 \u03c4 \u27e7Type\n\u27e6 nat \u27e7Type = \u2115\n\nopen import Base.Syntax.Context Type public\nopen import Base.Syntax.Vars Type public\n\ndata Const : (\u03c4 : Type) \u2192 Set where\n lit : \u2115 \u2192 Const nat\n -- succ : Const (int \u21d2 int)\n\ndata Term (\u0393 : Context) :\n (\u03c4 : Type) \u2192 Set where\n -- constants aka. primitives\n const : \u2200 {\u03c4} \u2192\n (c : Const \u03c4) \u2192\n Term \u0393 \u03c4\n var : \u2200 {\u03c4} \u2192\n (x : Var \u0393 \u03c4) \u2192\n Term \u0393 \u03c4\n app : \u2200 {\u03c3 \u03c4}\n (s : Term \u0393 (\u03c3 \u21d2 \u03c4)) \u2192\n (t : Term \u0393 \u03c3) \u2192\n Term \u0393 \u03c4\n -- we use de Bruijn indices, so we don't need binding occurrences.\n abs : \u2200 {\u03c3 \u03c4}\n (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192\n Term \u0393 (\u03c3 \u21d2 \u03c4)\n\ndata Val : Type \u2192 Set\nopen import Base.Denotation.Environment Type Val public\nopen import Base.Data.DependentList public\n\ndata Val where\n closure : \u2200 {\u0393 \u03c3 \u03c4} \u2192 (t : Term (\u03c3 \u2022 \u0393) \u03c4) \u2192 (\u03c1 : \u27e6 \u0393 \u27e7Context) \u2192 Val (\u03c3 \u21d2 \u03c4)\n intV : \u2200 (n : \u2115) \u2192 Val nat\n\nimport Base.Denotation.Environment\n-- Den stands for Denotational semantics.\nmodule Den = Base.Denotation.Environment Type \u27e6_\u27e7Type\n\n--\n-- Functional big-step semantics\n--\n\n-- Termination is far from obvious to Agda once we use closures. So we use\n-- step-indexing with a fuel value.\n\n-- WARNING: ISAC's big-step semantics produces a step count as \"output\". But\n-- that would not help Agda establish termination. That's only a problem for a\n-- functional big-step semantics, not for a relational semantics.\n--\n-- So, instead, I tried to use a sort of writer monad: the interpreter gets fuel\n-- and returns the remaining fuel. That's the same trick as in \"functional\n-- big-step semantics\". That *makes* the function terminating, even though Agda\n-- cannot see this because it does not know that the returned fuel is no bigger.\n\n-- Since we focus for now on STLC, unlike that\n-- paper, we could avoid error values because we keep types.\n--\n-- One could drop types and add error values instead.\n\ndata ErrVal (\u03c4 : Type) : Set where\n Done : (v : Val \u03c4) \u2192 (n1 : \u2115) \u2192 ErrVal \u03c4\n Error : ErrVal \u03c4\n TimeOut : ErrVal \u03c4\n\nRes : Type \u2192 Set\nRes \u03c4 = (n : \u2115) \u2192 ErrVal \u03c4\n\n_>>=_ : \u2200 {\u03c3 \u03c4} \u2192 Res \u03c3 \u2192 (Val \u03c3 \u2192 Res \u03c4) \u2192 Res \u03c4\n(s >>= t) n0 with s n0\n... | Done v n1 = t v n1\n... | Error = Error\n... | TimeOut = TimeOut\n\nevalConst : \u2200 {\u03c4} \u2192 Const \u03c4 \u2192 Res \u03c4\nevalConst (lit v) n = Done (intV v) n\n\n{-# TERMINATING #-}\neval : \u2200 {\u0393 \u03c4} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7Context \u2192 Res \u03c4\n\napply : \u2200 {\u03c3 \u03c4} \u2192 Val (\u03c3 \u21d2 \u03c4) \u2192 Val \u03c3 \u2192 Res \u03c4\napply (closure t \u03c1) a n = eval t (a \u2022 \u03c1) n\n\neval (var x) \u03c1 n = Done (\u27e6 x \u27e7Var \u03c1) n\neval (abs t) \u03c1 n = Done (closure t \u03c1) n\neval (const c) \u03c1 n = evalConst c n\neval _ \u03c1 zero = TimeOut\neval (app s t) \u03c1 (suc n) = (eval s \u03c1 >>= (\u03bb sv \u2192 eval t \u03c1 >>= \u03bb tv \u2192 apply sv tv)) n\n\neval-const-dec : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-const-dec (lit v) n0 .n0 refl = \u2264-refl\n\n{-# TERMINATING #-}\neval-dec : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 n1 \u2264 n0\neval-dec (const c) \u03c1 v n0 n1 eq = eval-const-dec c n0 n1 eq\neval-dec (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = \u2264-refl\neval-dec (app s t) \u03c1 v zero n1 ()\neval-dec (app s t) \u03c1 v (suc n0) n3 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv sn1 | [ seq ] with eval t \u03c1 sn1 | inspect (eval t \u03c1) sn1\neval-dec (app s t) \u03c1 v (suc n0) n3 eq | Done sv@(closure st s\u03c1) sn1 | [ seq ] | (Done tv tn2) | [ teq ] = \u2264-step (\u2264-trans (\u2264-trans (eval-dec st _ _ _ _ eq) (eval-dec t _ _ _ _ teq)) (eval-dec s _ _ _ _ seq))\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | Error | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Done sv sn1 | [ seq ] | TimeOut | [ teq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | Error | [ seq ]\neval-dec (app s t) \u03c1 v (suc n0) n3 () | TimeOut | [ seq ]\n\neval-const-mono : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c n0 \u2261 Done v n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1)\neval-const-mono (lit v) n0 .n0 refl = refl\n\n-- ARGH\n{-# TERMINATING #-}\neval-mono : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1)\neval-mono (const c) \u03c1 v n0 n1 eq = eval-const-mono c n0 n1 eq\neval-mono (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\neval-mono (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\neval-mono (app s t) \u03c1 v zero n1 ()\neval-mono (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\neval-mono (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\neval-mono (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-mono t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n2 () | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\neval-mono (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n\neval-adjust-plus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 n0 \u2261 Done v n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1)\neval-adjust-plus zero t \u03c1 v n0 n1 eq = eq\neval-adjust-plus (suc d) t \u03c1 v n0 n1 eq = eval-mono t \u03c1 v (d + n0) (d + n1) (eval-adjust-plus d t \u03c1 v n0 n1 eq)\n\neval-const-strengthen : \u2200 {\u03c4} \u2192 (c : Const \u03c4) \u2192 \u2200 {v} n0 n1 \u2192 evalConst c (suc n0) \u2261 Done v (suc n1) \u2192 evalConst c n0 \u2261 Done v n1\neval-const-strengthen (lit v) n0 .n0 refl = refl\n\n-- I started trying to prove eval-strengthen, which I appeal to informally\n-- below, but I gave up. I still guess the lemma is true but proving it looks\n-- too painful to bother.\n\n-- Without this lemma, I can't fully prove that this logical relation is\n-- equivalent to the original one.\n-- But this one works (well, at least up to the fundamental theorem, haven't\n-- attempted other lemmas), so it should be good enough.\n\n-- eval-mono-err : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 n \u2192 eval t \u03c1 n \u2261 Error \u2192 eval t \u03c1 (suc n) \u2261 Error\n-- eval-mono-err (const (lit x)) \u03c1 zero eq = {!!}\n-- eval-mono-err (const (lit x)) \u03c1 (suc n) eq = {!!}\n-- eval-mono-err (var x) \u03c1 n eq = {!!}\n-- eval-mono-err (app t t\u2081) \u03c1 n eq = {!!}\n-- eval-mono-err (abs t) \u03c1 n eq = {!!}\n\n-- -- eval t \u03c1 (suc n0) \u2261 Done v (suc n1) \u2192 eval t \u03c1 n0 \u2261 Done v n\n-- eval-aux : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 n \u2192 (\u03a3[ res0 \u2208 ErrVal \u03c4 ] eval t \u03c1 n \u2261 res0) \u00d7 (\u03a3[ resS \u2208 ErrVal \u03c4 ] eval t \u03c1 n \u2261 resS)\n-- eval-aux t \u03c1 n with\n-- eval t \u03c1 n | inspect (eval t \u03c1) n |\n-- eval t \u03c1 (suc n) | inspect (eval t \u03c1) (suc n)\n-- eval-aux t \u03c1 n | res0 | [ eq0 ] | (Done v1 n1) | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | res0 | [ eq0 ] | Error | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | Done v n1 | [ eq0 ] | TimeOut | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | Error | [ eq0 ] | TimeOut | [ eq1 ] = {!!}\n-- eval-aux t \u03c1 n | TimeOut | [ eq0 ] | TimeOut | [ eq1 ] = (TimeOut , refl) , (TimeOut , refl)\n\n-- {-# TERMINATING #-}\n-- eval-strengthen : \u2200 {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 \u03c1 v n0 n1 \u2192 eval t \u03c1 (suc n0) \u2261 Done v (suc n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-strengthen (const c) \u03c1 v n0 n1 eq = eval-const-strengthen c n0 n1 eq\n-- eval-strengthen (var x) \u03c1 .(\u27e6 x \u27e7Var \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (abs t) \u03c1 .(closure t \u03c1) n0 .n0 refl = refl\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq with eval s \u03c1 0 | inspect (eval s \u03c1) 0\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done sv sn1 | [ seq ] with eval-dec s \u03c1 sv 0 sn1 seq\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done sv .0 | [ seq ] | z\u2264n with eval t \u03c1 0 | inspect (eval t \u03c1) 0\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done sv _ | [ seq ] | z\u2264n | Done tv tn1 | [ teq ] with eval-dec t \u03c1 tv 0 tn1 teq\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done (closure st s\u03c1) _ | [ seq ] | z\u2264n | (Done tv .0) | [ teq ] | z\u2264n with eval-dec st _ v 0 (suc n1) eq\n-- eval-strengthen (app s t) \u03c1 v zero n1 eq | Done (closure st s\u03c1) _ | [ seq ] | z\u2264n | (Done tv _) | [ teq ] | z\u2264n | ()\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | Done sv _ | [ seq ] | z\u2264n | Error | [ teq ]\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | Done sv _ | [ seq ] | z\u2264n | TimeOut | [ teq ]\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | Error | [ seq ]\n-- eval-strengthen (app s t) \u03c1 v zero n1 () | TimeOut | [ seq ]\n-- -- eval-dec s \u03c1\n-- -- {!eval-dec s \u03c1 ? (suc zero) (suc n1) !}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 (suc n0) | inspect (eval s \u03c1) (suc n0)\n-- -- eval-strengthen (app s t) \u03c1 v\u2081 (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 n0 = {!eval-strengthen s \u03c1 v n0 n1 seq !}\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | Error | [ seq ]\n-- -- eval-strengthen (app s t) \u03c1 v (suc n0) n1 () | TimeOut | [ seq ]\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq with eval s \u03c1 n0 | inspect (eval s \u03c1) n0\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] with eval s \u03c1 (suc n0) | eval-mono s \u03c1 sv n0 n1 seq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl with eval t \u03c1 n1 | inspect (eval t \u03c1) n1\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Done tv n2 | [ teq ] with eval t \u03c1 (suc n1) | eval-mono t \u03c1 tv n1 n2 teq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n3 eq | Done (closure t\u2081 \u03c1\u2081) n1 | [ seq ] | .(Done (closure {\u0393 = _} {\u03c3 = _} {\u03c4 = _} t\u2081 \u03c1\u2081) (suc n1)) | refl | (Done tv n2) | [ teq ] | .(Done tv (suc n2)) | refl = eval-strengthen t\u2081 (tv \u2022 \u03c1\u2081) v n2 n3 eq\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | Error | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n2 eq | Done sv n1 | [ seq ] | .(Done sv (suc n1)) | refl | TimeOut | [ teq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | Error | [ seq ] = {!!}\n-- eval-strengthen (app s t) \u03c1 v (suc n0) n1 eq | TimeOut | [ seq ] = {!!}\n\n-- eval-adjust-minus : \u2200 d {\u0393 \u03c4} \u2192 (t : Term \u0393 \u03c4) \u2192 \u2200 {\u03c1 v} n0 n1 \u2192 eval t \u03c1 (d + n0) \u2261 Done v (d + n1) \u2192 eval t \u03c1 n0 \u2261 Done v n1\n-- eval-adjust-minus zero t n0 n1 eq = eq\n-- eval-adjust-minus (suc d) t n0 n1 eq = eval-adjust-minus d t n0 n1 (eval-strengthen t _ _ (d + n0) (d + n1) eq)\n\nimport Data.Integer as I\nopen I using (\u2124)\nmutual\n -- Warning: compared to Ahmed's papers, this definition for relT also requires\n -- t1 to be well-typed, not just t2.\n --\n -- This difference might affect the status of some proofs in Ahmed's papers,\n -- but that's not a problem here.\n\n -- Also: can't confirm this in any of the papers I'm using, but I'd guess that\n -- all papers using environments allow to relate closures with different\n -- implementations and different hidden environments.\n --\n -- To check if the proof goes through with equal context, I changed the proof.\n -- Now a proof that two closures are equivalent contains a proof that their\n -- typing contexts are equivalent. The changes were limited softawre\n -- engineering, the same proofs go through.\n\n -- This is not the same definition of relT, but it is equivalent.\n relT : \u2200 {\u03c4 \u0393} (t1 : Term \u0393 \u03c4) (t2 : Term \u0393 \u03c4) (\u03c11 : \u27e6 \u0393 \u27e7Context) (\u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\n -- This equation is a lemma in the original definition.\n relT t1 t2 \u03c11 \u03c12 zero = \u22a4\n -- To compare this definition, note that the original k is suc n here.\n relT {\u03c4} t1 t2 \u03c11 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n -- Originally we have 0 \u2264 j < k, so j < suc n, so k - j = suc n - j.\n -- It follows that 0 < k - j \u2264 k, hence suc n - j \u2264 suc n, or n - j \u2264 n.\n -- Here, instead of binding j we bind n-j = n - j, require n - j \u2264 n, and\n -- use suc n-j instead of k - j.\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n -- The next assumption is important. This still says that evaluation consumes j steps.\n -- Since j \u2264 n, it is OK to start evaluation with n steps.\n -- Starting with (suc n) and getting suc n-j is equivalent, per eval-mono\n -- and eval-strengthen. But in practice this version is easier to use.\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 0 \u00d7 relV \u03c4 v1 v2 (suc n-j)\n -- Here, computing t2 is allowed to take an unbounded number of steps. Having to write a number at all is annoying.\n\n relV : \u2200 \u03c4 (v1 v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n -- Show the proof still goes through if we relate clearly different values by\n -- inserting changes in the relation.\n -- There's no syntax to produce such changes, but you can add changes to the\n -- environment.\n relV nat (intV v1) (intV v2) n = \u03a3[ dv \u2208 \u2124 ] dv I.+ (I.+ v1) \u2261 (I.+ v2)\n relV (\u03c3 \u21d2 \u03c4) (closure {\u03931} t1 \u03c11) (closure {\u03932} t2 \u03c12) n =\n \u03a3[ \u2261\u0393 \u2208 \u03931 \u2261 \u03932 ]\n \u2200 (k : \u2115) (k\u2264n : k < n) v1 v2 \u2192\n relV \u03c3 v1 v2 k \u2192\n relT\n t1\n (subst (\u03bb \u0393 \u2192 Term (\u03c3 \u2022 \u0393) \u03c4) (sym \u2261\u0393) t2)\n (v1 \u2022 \u03c11)\n (subst (\u03bb \u0393 \u2192 \u27e6 \u03c3 \u2022 \u0393 \u27e7Context) (sym \u2261\u0393) (v2 \u2022 \u03c12))\n k\n -- Above, in the conclusion, I'm not relating app (closure t1 \u03c11) v1 with app\n -- (closure t2 \u03c12) v2 (or some encoding of that that actually works), but the\n -- result of taking a step from that configuration. That is important, because\n -- both Pitts' \"Step-Indexed Biorthogonality: a Tutorial Example\" and\n -- \"Imperative Self-Adjusting Computation\" do the same thing (and point out it's\n -- important).\n\n \u0394\u03c4 : Type \u2192 Type\n \u0394\u03c4 (\u03c3 \u21d2 \u03c4) = \u03c3 \u21d2 (\u0394\u03c4 \u03c3) \u21d2 \u0394\u03c4 \u03c4\n \u0394\u03c4 nat = nat\n\n -- Since the original relation allows unrelated environments, we do that here\n -- too. However, while that is fine as a logical relation, it's not OK if we\n -- want to prove that validity agrees with oplus.\n relT3 : \u2200 {\u03c4 \u03931 \u03932 \u0394\u0393} (t1 : Term \u03931 \u03c4) (dt : Term \u0394\u0393 (\u0394\u03c4 \u03c4)) (t2 : Term \u03932 \u03c4) (\u03c11 : \u27e6 \u03931 \u27e7Context) (d\u03c1 : \u27e6 \u0394\u0393 \u27e7Context) (\u03c12 : \u27e6 \u03932 \u27e7Context) \u2192 \u2115 \u2192 Set\n relT3 t1 dt t2 \u03c11 d\u03c1 \u03c12 zero = \u22a4\n relT3 {\u03c4} t1 dt t2 \u03c11 d\u03c1 \u03c12 (suc n) =\n (v1 : Val \u03c4) \u2192\n \u2200 n-j (n-j\u2264n : n-j \u2264 n) \u2192\n (eq : eval t1 \u03c11 n \u2261 Done v1 n-j) \u2192\n \u03a3[ v2 \u2208 Val \u03c4 ] \u03a3[ n2 \u2208 \u2115 ] eval t2 \u03c12 n2 \u2261 Done v2 0 \u00d7\n \u03a3[ dv \u2208 Val (\u0394\u03c4 \u03c4) ] \u03a3[ dn \u2208 \u2115 ] eval dt d\u03c1 dn \u2261 Done dv 0 \u00d7\n relV3 \u03c4 v1 dv v2 (suc n-j)\n\n relV3 : \u2200 \u03c4 (v1 : Val \u03c4) (dv : Val (\u0394\u03c4 \u03c4)) (v2 : Val \u03c4) \u2192 \u2115 \u2192 Set\n relV3 nat (intV v1) (intV dv) (intV v2) n = dv + v1 \u2261 v2\n relV3 (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure dt d\u03c1) (closure t2 \u03c12) n =\n \u2200 (k : \u2115) (k\u2264n : k < n) v1 dv v2 \u2192\n relV3 \u03c3 v1 dv v2 k \u2192\n relT3 t1 {!dt!} t2 (v1 \u2022 \u03c11) d\u03c1 (v2 \u2022 \u03c12) k\n\n -- Relate \u03bb x \u2192 0 and \u03bb x \u2192 1 at any step count.\n example1 : \u2200 n \u2192 relV (nat \u21d2 nat) (closure (const (lit 0)) \u2205) (closure (const (lit 1)) \u2205) n\n example1 n = refl ,\n \u03bb { zero k\u2264n v1 v2 x \u2192 tt\n ; (suc k) k\u2264n v1 v2 x .(intV 0) .k n-j\u2264n refl \u2192 intV 1 , 0 , refl , (I.+ 1 , refl)\n }\n\n -- Relate \u03bb x \u2192 0 and \u03bb x \u2192 x at any step count.\n example2 : \u2200 n \u2192 relV (nat \u21d2 nat) (closure (const (lit 0)) \u2205) (closure (var this) \u2205) n\n example2 n = refl ,\n \u03bb { zero k\u2264n v1 v2 x \u2192 tt\n ; (suc k) k\u2264n (intV v1) (intV v2) x .(intV 0) .k n-j\u2264n refl \u2192 intV v2 , 0 , refl , (I.+ v2 , cong I.+_ (+-right-identity v2))\n }\n\nrel\u03c1 : \u2200 \u0393 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) \u2192 \u2115 \u2192 Set\nrel\u03c1 \u2205 \u2205 \u2205 n = \u22a4\nrel\u03c1 (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) n = relV \u03c4 v1 v2 n \u00d7 rel\u03c1 \u0393 \u03c11 \u03c12 n\n\nrelV-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u03c4 v1 v2 \u2192 relV \u03c4 v1 v2 n \u2192 relV \u03c4 v1 v2 m\nrelV-mono m n m\u2264n nat (intV v1) (intV v2) vv = vv\nrelV-mono m n m\u2264n (\u03c3 \u21d2 \u03c4) (closure t1 \u03c11) (closure t2 \u03c12) (refl , ff) = refl , \u03bb k k\u2264m \u2192 ff k (\u2264-trans k\u2264m m\u2264n)\n\nrel\u03c1-mono : \u2200 m n \u2192 m \u2264 n \u2192 \u2200 \u0393 \u03c11 \u03c12 \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 n \u2192 rel\u03c1 \u0393 \u03c11 \u03c12 m\nrel\u03c1-mono m n m\u2264n \u2205 \u2205 \u2205 tt = tt\nrel\u03c1-mono m n m\u2264n (\u03c4 \u2022 \u0393) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = relV-mono m n m\u2264n _ v1 v2 vv , rel\u03c1-mono m n m\u2264n \u0393 \u03c11 \u03c12 \u03c1\u03c1\n\nfundamentalV : \u2200 {\u0393 \u03c4} (x : Var \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT (var x) (var x) \u03c11 \u03c12 n\nfundamentalV x zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamentalV this (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) .v1 .n n-j\u2264n refl = v2 , zero , refl , vv\nfundamentalV (that x) (suc n) (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , \u03c1\u03c1) = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\n\nlt1 : \u2200 {k n} \u2192 k < n \u2192 k \u2264 n\nlt1 (s\u2264s p) = \u2264-step p\n\nfundamental : \u2200 {\u0393 \u03c4} (t : Term \u0393 \u03c4) \u2192 (n : \u2115) \u2192 (\u03c11 \u03c12 : \u27e6 \u0393 \u27e7Context) (\u03c1\u03c1 : rel\u03c1 \u0393 \u03c11 \u03c12 n) \u2192 relT t t \u03c11 \u03c12 n\nfundamental t zero \u03c11 \u03c12 \u03c1\u03c1 = tt\nfundamental (var x) (suc n) \u03c11 \u03c12 \u03c1\u03c1 = fundamentalV x (suc n) \u03c11 \u03c12 \u03c1\u03c1\nfundamental (const (lit v)) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(intV v) .n n-j\u2264n refl = intV v , zero , refl , I.+ zero , refl\nfundamental (abs t) (suc n) \u03c11 \u03c12 \u03c1\u03c1 .(closure t \u03c11) .n n-j\u2264n refl = closure t \u03c12 , zero , refl , refl , \u03bb k k\u2264n v1 v2 vv \u2192 fundamental t k (v1 \u2022 \u03c11) (v2 \u2022 \u03c12) (vv , rel\u03c1-mono k (suc n) (lt1 k\u2264n) _ _ _ \u03c1\u03c1)\nfundamental (app s t) (suc zero) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n ()\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq with eval s \u03c11 n | inspect (eval s \u03c11) n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done sv1 n1 | [ s1eq ] with eval-dec s _ _ n n1 s1eq | eval t \u03c11 n1 | inspect (eval t \u03c11) n1\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n eq | Done (closure st1 s\u03c11) n1 | [ s1eq ] | n1\u2264n | Done tv1 n2 | [ t1eq ] with eval-dec t _ _ n1 n2 t1eq\n... | n2\u2264n1 with fundamental s (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 (closure st1 s\u03c11) (suc n1) (s\u2264s n1\u2264n) (eval-mono s \u03c11 (closure st1 s\u03c11) n n1 s1eq)\n | fundamental t (suc (suc n1)) \u03c11 \u03c12 (rel\u03c1-mono (suc (suc n1)) (suc (suc n)) (s\u2264s (s\u2264s n1\u2264n)) _ _ _ \u03c1\u03c1) tv1 (suc n2) (s\u2264s n2\u2264n1) (eval-mono t \u03c11 tv1 n1 n2 t1eq)\n... | sv2@(closure st2 s\u03c12) , sn3 , s2eq , refl , svv | tv2 , tn3 , t2eq , tvv with svv (suc n2) (s\u2264s (s\u2264s n2\u2264n1)) tv1 tv2 (relV-mono _ _ (s\u2264s (n\u22641+n n2)) _ _ _ tvv ) v1 n-j (eval-dec st1 _ _ _ _ eq) eq\n... | v2 , n3 , eq2 , vv = v2 , suc (sn3 + (tn3 + n3)) , comp , vv\n where\n s2eq-adj : eval s \u03c12 (sn3 + (tn3 + n3)) \u2261 Done (closure st2 s\u03c12) (tn3 + n3)\n s2eq-adj rewrite +-comm sn3 (tn3 + n3)| cong (Done (closure st2 s\u03c12)) (sym (+-right-identity (tn3 + n3))) = eval-adjust-plus (tn3 + n3) s _ sv2 _ _ s2eq\n t2eq-adj : eval t \u03c12 (tn3 + n3) \u2261 Done tv2 n3\n t2eq-adj rewrite +-comm tn3 n3 | cong (Done tv2) (sym (+-right-identity n3)) = eval-adjust-plus n3 t _ tv2 _ _ t2eq\n\n comp : (eval s \u03c12 >>= (\u03bb sv \u2192 eval t \u03c12 >>= apply sv))\n (sn3 + (tn3 + n3))\n \u2261 Done v2 0\n comp rewrite s2eq-adj | t2eq-adj = eq2\n\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | Error | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Done sv1 n1 | [ s1eq ] | n1\u2264n | TimeOut | [ t1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | Error | [ s1eq ]\nfundamental (app s t) (suc (suc n)) \u03c11 \u03c12 \u03c1\u03c1 v1 n-j n-j\u2264n () | TimeOut | [ s1eq ]\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"a10c358ed179bc75187c8a826b8e779593a32cf2","subject":"clarified question","message":"clarified question\n\nOld-commit-hash: 6c108888ef225e1f4e8b0978db5f3783ef6b834a\n","repos":"inc-lc\/ilc-agda","old_file":"derivation.agda","new_file":"derivation.agda","new_contents":"module derivation where\n\nopen import lambda\n\n-- KO: Is it correct that this file is supposed to contain the syntactic \n<<<<<<< HEAD\n-- variants of the operations in Syntactic.Changes.agda?\n-- If so, why does it have a different (and rather strange) \u0394-Type definition?\n=======\n-- variants of the operations in Changes.agda? What is its relation to\n-- the definitions in Syntactic.Changes?\n-- Why does it have a different (and rather strange) \u0394-Type definition?\n>>>>>>> clarified question\n-- Why are the base cases (bool) all missing? The inductive cases look\n-- boring since they don't actually do anything (which explains why compose and apply are the same)\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\u0394-Type bool = {!!}\n\napply : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbf. \u03bbx. apply ( df x ) ( f x )\napply {bool} = {!!}\n\ncompose : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4)\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app (compose {\u03c4\u2082}) (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbdg. \u03bbx. compose ( df x ) ( dg x )\ncompose {bool} = {!!}\n\nnil : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4)\nnil {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (nil {\u03c4\u2082})\n -- \u03bbx. nil\nnil {bool} = {!!}\n\n-- Hey, apply is \u03b1-equivalent to compose, what's going on?\n-- Oh, and `\u0394-Type` is the identity function?\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u03c4 \u2022 \u0394-Type \u03c4 \u2022 \u0394-Context \u0393\n\n-- CHANGE VARIABLES\n\n-- changes 'x' to 'dx'\n\nrename : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nrename this = that this\nrename (that x) = that (that (rename x))\n\n-- Weakening of a term needed during derivation - change x to x in a context which also includes dx.\n-- The actual specification is more complicated, study the type signature.\nadaptVar : \u2200 {\u03c4} \u0393\u2081 \u0393\u2082 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) \u03c4\nadaptVar \u2205 (\u03c4\u2082 \u2022 \u0393\u2082) this = this\nadaptVar \u2205 (\u03c4\u2082 \u2022 \u0393\u2082) (that x) = that (that (adaptVar \u2205 \u0393\u2082 x))\nadaptVar (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 this = this\nadaptVar (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 (that x) = that (adaptVar \u0393\u2081 \u0393\u2082 x)\n\nadapt : \u2200 {\u03c4} \u0393\u2081 \u0393\u2082 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) \u03c4\nadapt {\u03c4\u2081 \u21d2 \u03c4\u2082} \u0393\u2081 \u0393\u2082 (abs t) = abs (adapt (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 t)\nadapt \u0393\u2081 \u0393\u2082 (app t\u2081 t\u2082) = app (adapt \u0393\u2081 \u0393\u2082 t\u2081) (adapt \u0393\u2081 \u0393\u2082 t\u2082)\nadapt \u0393\u2081 \u0393\u2082 (var t) = var (adaptVar \u0393\u2081 \u0393\u2082 t)\nadapt \u0393\u2081 \u0393\u2082 true = true\nadapt \u0393\u2081 \u0393\u2082 false = false\nadapt \u0393\u2081 \u0393\u2082 (cond tc t\u2081 t\u2082) = cond (adapt \u0393\u2081 \u0393\u2082 tc) (adapt \u0393\u2081 \u0393\u2082 t\u2081) (adapt \u0393\u2081 \u0393\u2082 t\u2082)\n\n-- changes 'x' to 'nil' (if internally bound) or 'dx' (if externally bound)\n\n\u0394-var : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-var {\u2205} x = var (rename x)\n\u0394-var {\u03c4 \u2022 \u0393} this = nil {\u03c4}\n\u0394-var {\u03c4 \u2022 \u0393} (that x) = \u0394-var {\u0393} x\n\n-- Note: this should be the derivative with respect to the first variable.\nnabla : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2080 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082)\nnabla = {!!}\n\n-- CHANGE TERMS\n\u0394-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-term {\u0393} (abs {\u03c4\u2081 = \u03c4} t) = abs (\u0394-term {\u03c4 \u2022 \u0393} t)\n\n-- To recheck: which is the order in which to apply the changes? When I did my live proof to Klaus, I came up with this order:\n-- (\u0394-term t\u2081) (t\u2082 \u2295 (\u0394-term t\u2082)) \u2218 (\u2207 t\u2081) t\u2082 (\u0394-term t\u2082)\n-- corresponding to:\n\u0394-term {\u0393\u2081} {\u0393\u2082} {\u03c4} (app t\u2081 t\u2082) = app (app (compose {\u03c4}) (app (\u0394-term {\u0393\u2081} t\u2081) (app (app apply (\u0394-term {\u0393\u2081} {\u0393\u2082} t\u2082)) (adapt \u0393\u2081 \u0393\u2082 t\u2082))))\n (app (adapt \u0393\u2081 \u0393\u2082 (app (nabla {\u0393\u2081 \u22ce \u0393\u2082} t\u2081) t\u2082)) (\u0394-term {\u0393\u2081} {\u0393\u2082} t\u2082))\n\n\u0394-term {\u0393} (var x) = weaken {\u2205} {\u0393} (\u0394-var {\u0393} x)\n\u0394-term {\u0393} true = {!!}\n\u0394-term {\u0393} false = {!!}\n\u0394-term {\u0393} (cond t\u2081 t\u2082 t\u2083) = {!!}\n","old_contents":"module derivation where\n\nopen import lambda\n\n-- KO: Is it correct that this file is supposed to contain the syntactic \n-- variants of the operations in Syntactic.Changes.agda?\n-- If so, why does it have a different (and rather strange) \u0394-Type definition?\n-- Why are the base cases (bool) all missing? The inductive cases look\n-- boring since they don't actually do anything (which explains why compose and apply are the same)\n\n-- CHANGE TYPES\n\n\u0394-Type : Type \u2192 Type\n\u0394-Type (\u03c4\u2081 \u21d2 \u03c4\u2082) = \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082\n\u0394-Type bool = {!!}\n\napply : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u03c4 \u21d2 \u03c4)\napply {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbf. \u03bbx. apply ( df x ) ( f x )\napply {bool} = {!!}\n\ncompose : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4 \u21d2 \u0394-Type \u03c4)\ncompose {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (abs (abs (app (app (compose {\u03c4\u2082}) (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))\n -- \u03bbdf. \u03bbdg. \u03bbx. compose ( df x ) ( dg x )\ncompose {bool} = {!!}\n\nnil : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 (\u0394-Type \u03c4)\nnil {\u03c4\u2081 \u21d2 \u03c4\u2082} =\n abs (nil {\u03c4\u2082})\n -- \u03bbx. nil\nnil {bool} = {!!}\n\n-- Hey, apply is \u03b1-equivalent to compose, what's going on?\n-- Oh, and `\u0394-Type` is the identity function?\n\n-- CHANGE CONTEXTS\n\n\u0394-Context : Context \u2192 Context\n\u0394-Context \u2205 = \u2205\n\u0394-Context (\u03c4 \u2022 \u0393) = \u03c4 \u2022 \u0394-Type \u03c4 \u2022 \u0394-Context \u0393\n\n-- CHANGE VARIABLES\n\n-- changes 'x' to 'dx'\n\nrename : \u2200 {\u0393 \u03c4} \u2192 Var \u0393 \u03c4 \u2192 Var (\u0394-Context \u0393) (\u0394-Type \u03c4)\nrename this = that this\nrename (that x) = that (that (rename x))\n\n-- Weakening of a term needed during derivation - change x to x in a context which also includes dx.\n-- The actual specification is more complicated, study the type signature.\nadaptVar : \u2200 {\u03c4} \u0393\u2081 \u0393\u2082 \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Var (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) \u03c4\nadaptVar \u2205 (\u03c4\u2082 \u2022 \u0393\u2082) this = this\nadaptVar \u2205 (\u03c4\u2082 \u2022 \u0393\u2082) (that x) = that (that (adaptVar \u2205 \u0393\u2082 x))\nadaptVar (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 this = this\nadaptVar (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 (that x) = that (adaptVar \u0393\u2081 \u0393\u2082 x)\n\nadapt : \u2200 {\u03c4} \u0393\u2081 \u0393\u2082 \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) \u03c4\nadapt {\u03c4\u2081 \u21d2 \u03c4\u2082} \u0393\u2081 \u0393\u2082 (abs t) = abs (adapt (\u03c4\u2081 \u2022 \u0393\u2081) \u0393\u2082 t)\nadapt \u0393\u2081 \u0393\u2082 (app t\u2081 t\u2082) = app (adapt \u0393\u2081 \u0393\u2082 t\u2081) (adapt \u0393\u2081 \u0393\u2082 t\u2082)\nadapt \u0393\u2081 \u0393\u2082 (var t) = var (adaptVar \u0393\u2081 \u0393\u2082 t)\nadapt \u0393\u2081 \u0393\u2082 true = true\nadapt \u0393\u2081 \u0393\u2082 false = false\nadapt \u0393\u2081 \u0393\u2082 (cond tc t\u2081 t\u2082) = cond (adapt \u0393\u2081 \u0393\u2082 tc) (adapt \u0393\u2081 \u0393\u2082 t\u2081) (adapt \u0393\u2081 \u0393\u2082 t\u2082)\n\n-- changes 'x' to 'nil' (if internally bound) or 'dx' (if externally bound)\n\n\u0394-var : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Var (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-var {\u2205} x = var (rename x)\n\u0394-var {\u03c4 \u2022 \u0393} this = nil {\u03c4}\n\u0394-var {\u03c4 \u2022 \u0393} (that x) = \u0394-var {\u0393} x\n\n-- Note: this should be the derivative with respect to the first variable.\nnabla : \u2200 {\u0393 \u03c4\u2081 \u03c4\u2082} \u2192 (t\u2080 : Term \u0393 (\u03c4\u2081 \u21d2 \u03c4\u2082)) \u2192 Term \u0393 (\u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2081 \u21d2 \u0394-Type \u03c4\u2082)\nnabla = {!!}\n\n-- CHANGE TERMS\n\u0394-term : \u2200 {\u0393\u2081 \u0393\u2082 \u03c4} \u2192 Term (\u0393\u2081 \u22ce \u0393\u2082) \u03c4 \u2192 Term (\u0393\u2081 \u22ce \u0394-Context \u0393\u2082) (\u0394-Type \u03c4)\n\u0394-term {\u0393} (abs {\u03c4\u2081 = \u03c4} t) = abs (\u0394-term {\u03c4 \u2022 \u0393} t)\n\n-- To recheck: which is the order in which to apply the changes? When I did my live proof to Klaus, I came up with this order:\n-- (\u0394-term t\u2081) (t\u2082 \u2295 (\u0394-term t\u2082)) \u2218 (\u2207 t\u2081) t\u2082 (\u0394-term t\u2082)\n-- corresponding to:\n\u0394-term {\u0393\u2081} {\u0393\u2082} {\u03c4} (app t\u2081 t\u2082) = app (app (compose {\u03c4}) (app (\u0394-term {\u0393\u2081} t\u2081) (app (app apply (\u0394-term {\u0393\u2081} {\u0393\u2082} t\u2082)) (adapt \u0393\u2081 \u0393\u2082 t\u2082))))\n (app (adapt \u0393\u2081 \u0393\u2082 (app (nabla {\u0393\u2081 \u22ce \u0393\u2082} t\u2081) t\u2082)) (\u0394-term {\u0393\u2081} {\u0393\u2082} t\u2082))\n\n\u0394-term {\u0393} (var x) = weaken {\u2205} {\u0393} (\u0394-var {\u0393} x)\n\u0394-term {\u0393} true = {!!}\n\u0394-term {\u0393} false = {!!}\n\u0394-term {\u0393} (cond t\u2081 t\u2082 t\u2083) = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"248a51f14bba78dd9c66b0378c4ee935b70b5a79","subject":"Minor change.","message":"Minor change.\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/fot\/FOTC\/Program\/ABP\/ProofSpecificationATP.agda","new_file":"src\/fot\/FOTC\/Program\/ABP\/ProofSpecificationATP.agda","new_contents":"------------------------------------------------------------------------------\n-- The alternating bit protocol (ABP) satisfies the specification\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module proves the correctness of the ABP following the\n-- formalization in [1].\n\n-- [1] Peter Dybjer and Herbert Sander. A functional programming\n-- approach to the specification and verification of concurrent\n-- systems. Formal Aspects of Computing, 1:303\u2013319, 1989.\n\n-- N.B This module does not contain combined proofs, but it imports\n-- modules which contain combined proofs.\n\nmodule FOTC.Program.ABP.ProofSpecificationATP where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Data.Bool\nopen import FOTC.Data.Bool.PropertiesATP\nopen import FOTC.Data.Stream\nopen import FOTC.Program.ABP.ABP\nopen import FOTC.Program.ABP.Fair\nopen import FOTC.Program.ABP.Lemma1ATP\nopen import FOTC.Program.ABP.Lemma2ATP\nopen import FOTC.Program.ABP.Terms\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n-- Main theorem.\nspec : \u2200 {b is os\u2080 os\u2081} \u2192 Bit b \u2192 Stream is \u2192 Fair os\u2080 \u2192 Fair os\u2081 \u2192\n is \u2248 transfer b os\u2080 os\u2081 is\nspec {b} {is} {os\u2080} {os\u2081} Bb Sis Fos\u2080 Fos\u2081 = \u2248-coind B prf\u2081 prf\u2082\n where\n postulate prf\u2081 : \u2200 {is js} \u2192 B is js \u2192\n \u2203[ i' ] \u2203[ is' ] \u2203[ js' ]\n is \u2261 i' \u2237 is' \u2227 js \u2261 i' \u2237 js' \u2227 B is' js'\n {-# ATP prove prf\u2081 lemma\u2081 lemma\u2082 not-Bool #-}\n\n prf\u2082 : B is (transfer b os\u2080 os\u2081 is)\n prf\u2082 = b , os\u2080 , os\u2081 , as , bs , cs , ds , helper\n where\n as bs cs ds : D\n as = has (send \u00b7 b) (ack \u00b7 b) (out \u00b7 b) (corrupt \u00b7 os\u2080) (corrupt \u00b7 os\u2081) is\n bs = hbs (send \u00b7 b) (ack \u00b7 b) (out \u00b7 b) (corrupt \u00b7 os\u2080) (corrupt \u00b7 os\u2081) is\n cs = hcs (send \u00b7 b) (ack \u00b7 b) (out \u00b7 b) (corrupt \u00b7 os\u2080) (corrupt \u00b7 os\u2081) is\n ds = hds (send \u00b7 b) (ack \u00b7 b) (out \u00b7 b) (corrupt \u00b7 os\u2080) (corrupt \u00b7 os\u2081) is\n {-# ATP definition as bs cs ds #-}\n\n postulate helper : Stream is \u2227 Bit b \u2227 Fair os\u2080 \u2227 Fair os\u2081\n \u2227 ABP b is os\u2080 os\u2081 as bs cs ds (transfer b os\u2080 os\u2081 is)\n {-# ATP prove helper #-}\n","old_contents":"------------------------------------------------------------------------------\n-- The alternating bit protocol (ABP) satisfies the specification\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n{-# OPTIONS --without-K #-}\n\n-- This module proves the correctness of the ABP following the\n-- formalization in [1].\n\n-- [1] Peter Dybjer and Herbert Sander. A functional programming\n-- approach to the specification and verification of concurrent\n-- systems. Formal Aspects of Computing, 1:303\u2013319, 1989.\n\n-- N.B This module does not contain combined proofs, but it imports\n-- modules which contain combined proofs.\n\nmodule FOTC.Program.ABP.ProofSpecificationATP where\n\nopen import FOTC.Base\nopen import FOTC.Base.List\nopen import FOTC.Data.Bool\nopen import FOTC.Data.Bool.PropertiesATP\nopen import FOTC.Data.Stream\nopen import FOTC.Program.ABP.ABP\nopen import FOTC.Program.ABP.Fair\nopen import FOTC.Program.ABP.Lemma1ATP\nopen import FOTC.Program.ABP.Lemma2ATP\nopen import FOTC.Program.ABP.Terms\nopen import FOTC.Relation.Binary.Bisimilarity\n\n------------------------------------------------------------------------------\n-- Main theorem.\nspec : \u2200 {b is os\u2080 os\u2081} \u2192 Bit b \u2192 Stream is \u2192 Fair os\u2080 \u2192 Fair os\u2081 \u2192\n is \u2248 transfer b os\u2080 os\u2081 is\nspec {b} {is} {os\u2080} {os\u2081} Bb Sis Fos\u2080 Fos\u2081 = \u2248-coind B prf\u2081 prf\u2082\n where\n postulate prf\u2081 : \u2200 {is js} \u2192 B is js \u2192\n \u2203[ i' ] \u2203[ is' ] \u2203[ js' ]\n is \u2261 i' \u2237 is' \u2227 js \u2261 i' \u2237 js' \u2227 B is' js'\n {-# ATP prove prf\u2081 lemma\u2081 lemma\u2082 not-Bool #-}\n\n prf\u2082 : B is (transfer b os\u2080 os\u2081 is)\n prf\u2082 = b , os\u2080 , os\u2081 , as , bs , cs , ds , helper\n where\n as bs cs ds : D\n as = has (send \u00b7 b) (ack \u00b7 b) (out \u00b7 b) (corrupt \u00b7 os\u2080) (corrupt \u00b7 os\u2081) is\n bs = hbs (send \u00b7 b) (ack \u00b7 b) (out \u00b7 b) (corrupt \u00b7 os\u2080) (corrupt \u00b7 os\u2081) is\n cs = hcs (send \u00b7 b) (ack \u00b7 b) (out \u00b7 b) (corrupt \u00b7 os\u2080) (corrupt \u00b7 os\u2081) is\n ds = hds (send \u00b7 b) (ack \u00b7 b) (out \u00b7 b) (corrupt \u00b7 os\u2080) (corrupt \u00b7 os\u2081) is\n\n {-# ATP definition as bs cs ds #-}\n\n postulate helper : Stream is \u2227 Bit b \u2227 Fair os\u2080 \u2227 Fair os\u2081\n \u2227 ABP b is os\u2080 os\u2081 as bs cs ds (transfer b os\u2080 os\u2081 is)\n {-# ATP prove helper #-}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c349b7d6ea0a18743c706db2f6f14e8e3b95f6de","subject":"Logical: avoid a misleading notation","message":"Logical: avoid a misleading notation\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Relation\/Binary\/Logical.agda","new_file":"lib\/Relation\/Binary\/Logical.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Relation.Binary.Logical where\n\nopen import Level\n\n\u27e6Set\u27e7 : \u2200 {a\u2081 a\u2082} a\u1d63 (A\u2081 : Set a\u2081) (A\u2082 : Set a\u2082) \u2192 Set _\n\u27e6Set\u27e7 a\u1d63 A\u2081 A\u2082 = A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63\n\n\u27e6Set\u2080\u27e7 : \u2200 (A\u2081 A\u2082 : Set) \u2192 Set\u2081\n\u27e6Set\u2080\u27e7 = \u27e6Set\u27e7 zero\n\n\u27e6Set\u2081\u27e7 : \u2200 (A\u2081 A\u2082 : Set\u2081) \u2192 Set\u2082\n\u27e6Set\u2081\u27e7 = \u27e6Set\u27e7 (suc zero)\n\n\u27e6\u03a0\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : A\u2081 \u2192 Set b\u2081} {B\u2082 : A\u2082 \u2192 Set b\u2082} (B\u1d63 : \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u2081 x\u2081 \u2192 B\u2082 x\u2082 \u2192 Set b\u1d63)\n (f\u2081 : (x : A\u2081) \u2192 B\u2081 x) (f\u2082 : (x : A\u2082) \u2192 B\u2082 x) \u2192 Set _\n\u27e6\u03a0\u27e7 A\u1d63 B\u1d63 = \u03bb f\u2081 f\u2082 \u2192 \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u1d63 x\u1d63 (f\u2081 x\u2081) (f\u2082 x\u2082)\n\ninfixr 0 \u27e6\u03a0\u27e7\nsyntax \u27e6\u03a0\u27e7 A\u1d63 (\u03bb x\u1d63 \u2192 f) = \u27e8 x\u1d63 \u2236 A\u1d63 \u27e9\u27e6\u2192\u27e7 f\n\n\u27e6\u03a0\u27e7e : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : A\u2081 \u2192 Set b\u2081} {B\u2082 : A\u2082 \u2192 Set b\u2082} (B\u1d63 : \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u2081 x\u2081 \u2192 B\u2082 x\u2082 \u2192 Set b\u1d63)\n (f\u2081 : (x : A\u2081) \u2192 B\u2081 x) (f\u2082 : (x : A\u2082) \u2192 B\u2082 x) \u2192 Set _\n\u27e6\u03a0\u27e7e A\u1d63 B\u1d63 = \u03bb f\u2081 f\u2082 \u2192 \u2200 x\u2081 x\u2082 (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u1d63 x\u1d63 (f\u2081 x\u2081) (f\u2082 x\u2082)\n\n\u27e6\u2200\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : A\u2081 \u2192 Set b\u2081} {B\u2082 : A\u2082 \u2192 Set b\u2082} (B\u1d63 : \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u2081 x\u2081 \u2192 B\u2082 x\u2082 \u2192 Set b\u1d63)\n (f\u2081 : {x : A\u2081} \u2192 B\u2081 x) (f\u2082 : {x : A\u2082} \u2192 B\u2082 x) \u2192 Set _\n\u27e6\u2200\u27e7 A\u1d63 B\u1d63 = \u03bb f\u2081 f\u2082 \u2192 \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u1d63 x\u1d63 (f\u2081 {x\u2081}) (f\u2082 {x\u2082})\n\ninfixr 0 \u27e6\u2200\u27e7\nsyntax \u27e6\u2200\u27e7 A\u1d63 (\u03bb x\u1d63 \u2192 f) = \u2200\u27e8 x\u1d63 \u2236 A\u1d63 \u27e9\u27e6\u2192\u27e7 f\n\ninfixr 1 _\u27e6\u2192\u27e7_\n_\u27e6\u2192\u27e7_ : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : Set b\u2081} {B\u2082 : Set b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 Set b\u1d63)\n (f\u2081 : A\u2081 \u2192 B\u2081) (f\u2082 : A\u2082 \u2192 B\u2082) \u2192 Set _\nA\u1d63 \u27e6\u2192\u27e7 B\u1d63 = \u27e6\u03a0\u27e7 A\u1d63 (\u03bb _ \u2192 B\u1d63)\n\ninfixr 0 _\u27e6\u2192\u27e7e_\n_\u27e6\u2192\u27e7e_ : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : Set b\u2081} {B\u2082 : Set b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 Set b\u1d63)\n (f\u2081 : A\u2081 \u2192 B\u2081) (f\u2082 : A\u2082 \u2192 B\u2082) \u2192 Set _\n_\u27e6\u2192\u27e7e_ A\u1d63 B\u1d63 = \u27e6\u03a0\u27e7e A\u1d63 (\u03bb _ \u2192 B\u1d63)\n\nopen import Data.Product\n\n\nopen import Data.Unit\n\nrecord \u27e6\u22a4\u27e7 (x\u2081 x\u2082 : \u22a4) : Set where\n constructor \u27e6tt\u27e7\n\nopen import Data.Empty\n\ndata \u27e6\u22a5\u27e7 (x\u2081 x\u2082 : \u22a5) : Set where\n\nopen import Relation.Nullary\n\ninfix 3 \u27e6\u00ac\u27e7_\n\n--\u27e6\u00ac\u27e7_ : (\u27e6Set\u27e7 \u27e6\u2192\u27e7 \u27e6Set\u27e7) \u00ac_ \u00ac_\n\u27e6\u00ac\u27e7_ : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63) \u2192 \u00ac A\u2081 \u2192 \u00ac A\u2082 \u2192 Set _\n\u27e6\u00ac\u27e7 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 \u27e6\u22a5\u27e7\n\n{-\n\u27e6\u2203\u27e7 : {A\u2081 A\u2082 : World \u2192 Set} (A\u1d63 : \u2200 {\u03b1\u2081 \u03b1\u2082} \u2192 \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082 \u2192 A\u2081 \u03b1\u2081 \u2192 A\u2082 \u03b1\u2082 \u2192 Set)\n (p\u2081 : \u2203 A\u2081) (f\u2082 : \u2203 A\u2082) \u2192 Set\n\u27e6\u2203\u27e7 A\u1d63 = \u03bb p\u2081 p\u2082 \u2192 \u03a3[ \u03b1\u1d63 \u2236 \u27e6World\u27e7 (proj\u2081 p\u2081) (proj\u2081 p\u2082) ]\n (A\u1d63 \u03b1\u1d63 (proj\u2082 p\u2081) (proj\u2082 p\u2082))\n\nsyntax \u27e6\u2203\u27e7 (\u03bb \u03b1\u1d63 \u2192 f) = \u27e6\u2203\u27e7[ \u03b1\u1d63 ] f\n-}\n\nPred : \u2200 \u2113 {a} (A : Set a) \u2192 Set (a \u2294 suc \u2113)\nPred \u2113 A = A \u2192 Set \u2113\n\n\u27e6Pred\u27e7 : \u2200 {p\u2081 p\u2082} p\u1d63 {a\u2081 a\u2082 a\u1d63} \u2192 (\u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e6\u2192\u27e7 \u27e6Set\u27e7 _) (Pred p\u2081) (Pred p\u2082)\n--\u27e6Pred\u27e7 {p\u2081} {p\u2082} p\u1d63 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 \u27e6Set\u27e7 (p\u2081 \u2294 p\u2082 \u2294 p\u1d63)\n\u27e6Pred\u27e7 {p\u2081} {p\u2082} p\u1d63 A\u1d63 = \u03bb f\u2081 f\u2082 \u2192 \u2200 {x\u2081} {x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 f\u2081 x\u2081 \u2192 f\u2082 x\u2082 \u2192 Set (p\u2081 \u2294 p\u2082 \u2294 p\u1d63)\n\nopen import Relation.Binary\n\nprivate\n REL\u2032 : \u2200 \u2113 {a b} \u2192 Set a \u2192 Set b \u2192 Set (a \u2294 b \u2294 suc \u2113)\n REL\u2032 \u2113 A B = A \u2192 B \u2192 Set \u2113\n\n \u27e6REL\u27e7\u2032 : \u2200 {a\u2081 a\u2082 a\u1d63 b\u2081 b\u2082 b\u1d63 \u2113\u2081 \u2113\u2082} \u2113\u1d63 \u2192\n (\u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e6\u2192\u27e7 \u27e6Set\u27e7 {b\u2081} {b\u2082} b\u1d63 \u27e6\u2192\u27e7 \u27e6Set\u27e7 _) (REL\u2032 \u2113\u2081) (REL\u2032 \u2113\u2082)\n \u27e6REL\u27e7\u2032 \u2113\u1d63 A\u1d63 B\u1d63 = A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 (\u27e6Set\u27e7 \u2113\u1d63)\n\n\u27e6REL\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : Set b\u2081} {B\u2082 : Set b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 Set b\u1d63)\n {\u2113\u2081 \u2113\u2082} \u2113\u1d63 (\u223c\u2081 : REL A\u2081 B\u2081 \u2113\u2081) (\u223c\u2082 : REL A\u2082 B\u2082 \u2113\u2082) \u2192 Set _\n\u27e6REL\u27e7 A\u1d63 B\u1d63 \u2113\u1d63 = A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 (\u27e6Set\u27e7 \u2113\u1d63)\n\n\u27e6Rel\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {\u2113\u2081 \u2113\u2082} \u2113\u1d63 (\u223c\u2081 : Rel A\u2081 \u2113\u2081) (\u223c\u2082 : Rel A\u2082 \u2113\u2082) \u2192 Set _\n\u27e6Rel\u27e7 A\u1d63 \u2113\u1d63 = \u27e6REL\u27e7 A\u1d63 A\u1d63 \u2113\u1d63\n\n-- data \u27e6Dec\u27e7 {\u2113\u2081 \u2113\u2082 \u2113\u1d63} {P\u2081 : Set \u2113\u2081} {P\u2082 : Set \u2113\u2082} (P\u1d63 : P\u2081 \u2192 P\u2082 \u2192 Set \u2113\u1d63) : Dec P\u2081 \u2192 Dec P\u2082 \u2192 Set (\u2113\u2081 \u2294 \u2113\u2082 \u2294 \u2113\u1d63) where\n-- data \u27e6Dec\u27e7 {\u2113\u1d63} : \u27e6Pred\u27e7 (\u27e6Set\u27e7 \u2113\u1d63) \u2113\u1d63 Dec Dec where\ndata \u27e6Dec\u27e7 {\u2113\u2081 \u2113\u2082 \u2113\u1d63} {P\u2081 : Set \u2113\u2081} {P\u2082 : Set \u2113\u2082} (P\u1d63 : P\u2081 \u2192 P\u2082 \u2192 Set \u2113\u1d63) : \u27e6Set\u27e7 (\u2113\u2081 \u2294 \u2113\u2082 \u2294 \u2113\u1d63) (Dec P\u2081) (Dec P\u2082) where\n yes : {-\u2200 {P\u2081 P\u2082 P\u1d63}-} {p\u2081 : P\u2081} {p\u2082 : P\u2082} (p\u1d63 : P\u1d63 p\u2081 p\u2082) \u2192 \u27e6Dec\u27e7 P\u1d63 (yes p\u2081) (yes p\u2082)\n no : {-\u2200 {P\u2081 P\u2082 P\u1d63}-} {\u00acp\u2081 : \u00ac P\u2081} {\u00acp\u2082 : \u00ac P\u2082} (\u00acp\u1d63 : (\u27e6\u00ac\u27e7 P\u1d63) \u00acp\u2081 \u00acp\u2082) \u2192 \u27e6Dec\u27e7 P\u1d63 (no \u00acp\u2081) (no \u00acp\u2082)\n\n--\u27e6Decidable\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63 b\u2081 b\u2082 b\u1d63 \u2113\u2081 \u2113\u2082 \u2113\u1d63} \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 B\u1d63 \u2236 \u27e6Set\u27e7 {b\u2081 b\u2082} b\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6REL\u27e7 A\u1d63 B\u1d63 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e6\u2192\u27e7 \u27e6Set\u27e7 _) Decidable Decidable\n\u27e6Decidable\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : Set b\u2081} {B\u2082 : Set b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 Set b\u1d63)\n {\u2113\u2081 \u2113\u2082 \u2113\u1d63} {\u223c\u2081 : REL A\u2081 B\u2081 \u2113\u2081} {\u223c\u2082 : REL A\u2082 B\u2082 \u2113\u2082} (\u223c\u1d63 : \u27e6REL\u27e7 A\u1d63 B\u1d63 \u2113\u1d63 \u223c\u2081 \u223c\u2082)\n \u2192 Decidable \u223c\u2081 \u2192 Decidable \u223c\u2082 \u2192 Set _\n\u27e6Decidable\u27e7 A\u1d63 B\u1d63 _\u223c\u1d63_ = \u27e8 x\u1d63 \u2236 A\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e8 y\u1d63 \u2236 B\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6Dec\u27e7 (x\u1d63 \u223c\u1d63 y\u1d63)\n\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\nmodule Relation.Binary.Logical where\n\nopen import Level\n\n\u27e6Set\u27e7 : \u2200 {a\u2081 a\u2082} a\u1d63 (A\u2081 : Set a\u2081) (A\u2082 : Set a\u2082) \u2192 Set _\n\u27e6Set\u27e7 a\u1d63 A\u2081 A\u2082 = A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63\n\n\u27e6Set\u2080\u27e7 : \u2200 (A\u2081 A\u2082 : Set) \u2192 Set\u2081\n\u27e6Set\u2080\u27e7 = \u27e6Set\u27e7 zero\n\n\u27e6Set\u2081\u27e7 : \u2200 (A\u2081 A\u2082 : Set\u2081) \u2192 Set\u2082\n\u27e6Set\u2081\u27e7 = \u27e6Set\u27e7 (suc zero)\n\n\u27e6\u03a0\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : A\u2081 \u2192 Set b\u2081} {B\u2082 : A\u2082 \u2192 Set b\u2082} (B\u1d63 : \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u2081 x\u2081 \u2192 B\u2082 x\u2082 \u2192 Set b\u1d63)\n (f\u2081 : (x : A\u2081) \u2192 B\u2081 x) (f\u2082 : (x : A\u2082) \u2192 B\u2082 x) \u2192 Set _\n\u27e6\u03a0\u27e7 A\u1d63 B\u1d63 = \u03bb f\u2081 f\u2082 \u2192 \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u1d63 x\u1d63 (f\u2081 x\u2081) (f\u2082 x\u2082)\n\ninfixr 0 \u27e6\u03a0\u27e7\nsyntax \u27e6\u03a0\u27e7 A\u1d63 (\u03bb x\u1d63 \u2192 f) = \u27e8 x\u1d63 \u2236 A\u1d63 \u27e9\u27e6\u2192\u27e7 f\n\n\u27e6\u03a0\u27e7e : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : A\u2081 \u2192 Set b\u2081} {B\u2082 : A\u2082 \u2192 Set b\u2082} (B\u1d63 : \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u2081 x\u2081 \u2192 B\u2082 x\u2082 \u2192 Set b\u1d63)\n (f\u2081 : (x : A\u2081) \u2192 B\u2081 x) (f\u2082 : (x : A\u2082) \u2192 B\u2082 x) \u2192 Set _\n\u27e6\u03a0\u27e7e A\u1d63 B\u1d63 = \u03bb f\u2081 f\u2082 \u2192 \u2200 x\u2081 x\u2082 (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u1d63 x\u1d63 (f\u2081 x\u2081) (f\u2082 x\u2082)\n\n\u27e6\u2200\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : \u2200 A\u2081 \u2192 Set b\u2081} {B\u2082 : \u2200 A\u2082 \u2192 Set b\u2082} (B\u1d63 : \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u2081 x\u2081 \u2192 B\u2082 x\u2082 \u2192 Set b\u1d63)\n (f\u2081 : {x : A\u2081} \u2192 B\u2081 x) (f\u2082 : {x : A\u2082} \u2192 B\u2082 x) \u2192 Set _\n\u27e6\u2200\u27e7 A\u1d63 B\u1d63 = \u03bb f\u2081 f\u2082 \u2192 \u2200 {x\u2081 x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 B\u1d63 x\u1d63 (f\u2081 {x\u2081}) (f\u2082 {x\u2082})\n\ninfixr 0 \u27e6\u2200\u27e7\nsyntax \u27e6\u2200\u27e7 A\u1d63 (\u03bb x\u1d63 \u2192 f) = \u2200\u27e8 x\u1d63 \u2236 A\u1d63 \u27e9\u27e6\u2192\u27e7 f\n\ninfixr 1 _\u27e6\u2192\u27e7_\n_\u27e6\u2192\u27e7_ : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : Set b\u2081} {B\u2082 : Set b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 Set b\u1d63)\n (f\u2081 : A\u2081 \u2192 B\u2081) (f\u2082 : A\u2082 \u2192 B\u2082) \u2192 Set _\nA\u1d63 \u27e6\u2192\u27e7 B\u1d63 = \u27e6\u03a0\u27e7 A\u1d63 (\u03bb _ \u2192 B\u1d63)\n\ninfixr 0 _\u27e6\u2192\u27e7e_\n_\u27e6\u2192\u27e7e_ : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : Set b\u2081} {B\u2082 : Set b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 Set b\u1d63)\n (f\u2081 : A\u2081 \u2192 B\u2081) (f\u2082 : A\u2082 \u2192 B\u2082) \u2192 Set _\n_\u27e6\u2192\u27e7e_ A\u1d63 B\u1d63 = \u27e6\u03a0\u27e7e A\u1d63 (\u03bb _ \u2192 B\u1d63)\n\nopen import Data.Product\n\n\nopen import Data.Unit\n\nrecord \u27e6\u22a4\u27e7 (x\u2081 x\u2082 : \u22a4) : Set where\n constructor \u27e6tt\u27e7\n\nopen import Data.Empty\n\ndata \u27e6\u22a5\u27e7 (x\u2081 x\u2082 : \u22a5) : Set where\n\nopen import Relation.Nullary\n\ninfix 3 \u27e6\u00ac\u27e7_\n\n--\u27e6\u00ac\u27e7_ : (\u27e6Set\u27e7 \u27e6\u2192\u27e7 \u27e6Set\u27e7) \u00ac_ \u00ac_\n\u27e6\u00ac\u27e7_ : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63) \u2192 \u00ac A\u2081 \u2192 \u00ac A\u2082 \u2192 Set _\n\u27e6\u00ac\u27e7 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 \u27e6\u22a5\u27e7\n\n{-\n\u27e6\u2203\u27e7 : {A\u2081 A\u2082 : World \u2192 Set} (A\u1d63 : \u2200 {\u03b1\u2081 \u03b1\u2082} \u2192 \u27e6World\u27e7 \u03b1\u2081 \u03b1\u2082 \u2192 A\u2081 \u03b1\u2081 \u2192 A\u2082 \u03b1\u2082 \u2192 Set)\n (p\u2081 : \u2203 A\u2081) (f\u2082 : \u2203 A\u2082) \u2192 Set\n\u27e6\u2203\u27e7 A\u1d63 = \u03bb p\u2081 p\u2082 \u2192 \u03a3[ \u03b1\u1d63 \u2236 \u27e6World\u27e7 (proj\u2081 p\u2081) (proj\u2081 p\u2082) ]\n (A\u1d63 \u03b1\u1d63 (proj\u2082 p\u2081) (proj\u2082 p\u2082))\n\nsyntax \u27e6\u2203\u27e7 (\u03bb \u03b1\u1d63 \u2192 f) = \u27e6\u2203\u27e7[ \u03b1\u1d63 ] f\n-}\n\nPred : \u2200 \u2113 {a} (A : Set a) \u2192 Set (a \u2294 suc \u2113)\nPred \u2113 A = A \u2192 Set \u2113\n\n\u27e6Pred\u27e7 : \u2200 {p\u2081 p\u2082} p\u1d63 {a\u2081 a\u2082 a\u1d63} \u2192 (\u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e6\u2192\u27e7 \u27e6Set\u27e7 _) (Pred p\u2081) (Pred p\u2082)\n--\u27e6Pred\u27e7 {p\u2081} {p\u2082} p\u1d63 A\u1d63 = A\u1d63 \u27e6\u2192\u27e7 \u27e6Set\u27e7 (p\u2081 \u2294 p\u2082 \u2294 p\u1d63)\n\u27e6Pred\u27e7 {p\u2081} {p\u2082} p\u1d63 A\u1d63 = \u03bb f\u2081 f\u2082 \u2192 \u2200 {x\u2081} {x\u2082} (x\u1d63 : A\u1d63 x\u2081 x\u2082) \u2192 f\u2081 x\u2081 \u2192 f\u2082 x\u2082 \u2192 Set (p\u2081 \u2294 p\u2082 \u2294 p\u1d63)\n\nopen import Relation.Binary\n\nprivate\n REL\u2032 : \u2200 \u2113 {a b} \u2192 Set a \u2192 Set b \u2192 Set (a \u2294 b \u2294 suc \u2113)\n REL\u2032 \u2113 A B = A \u2192 B \u2192 Set \u2113\n\n \u27e6REL\u27e7\u2032 : \u2200 {a\u2081 a\u2082 a\u1d63 b\u2081 b\u2082 b\u1d63 \u2113\u2081 \u2113\u2082} \u2113\u1d63 \u2192\n (\u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e6\u2192\u27e7 \u27e6Set\u27e7 {b\u2081} {b\u2082} b\u1d63 \u27e6\u2192\u27e7 \u27e6Set\u27e7 _) (REL\u2032 \u2113\u2081) (REL\u2032 \u2113\u2082)\n \u27e6REL\u27e7\u2032 \u2113\u1d63 A\u1d63 B\u1d63 = A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 (\u27e6Set\u27e7 \u2113\u1d63)\n\n\u27e6REL\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : Set b\u2081} {B\u2082 : Set b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 Set b\u1d63)\n {\u2113\u2081 \u2113\u2082} \u2113\u1d63 (\u223c\u2081 : REL A\u2081 B\u2081 \u2113\u2081) (\u223c\u2082 : REL A\u2082 B\u2082 \u2113\u2082) \u2192 Set _\n\u27e6REL\u27e7 A\u1d63 B\u1d63 \u2113\u1d63 = A\u1d63 \u27e6\u2192\u27e7 B\u1d63 \u27e6\u2192\u27e7 (\u27e6Set\u27e7 \u2113\u1d63)\n\n\u27e6Rel\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {\u2113\u2081 \u2113\u2082} \u2113\u1d63 (\u223c\u2081 : Rel A\u2081 \u2113\u2081) (\u223c\u2082 : Rel A\u2082 \u2113\u2082) \u2192 Set _\n\u27e6Rel\u27e7 A\u1d63 \u2113\u1d63 = \u27e6REL\u27e7 A\u1d63 A\u1d63 \u2113\u1d63\n\n-- data \u27e6Dec\u27e7 {\u2113\u2081 \u2113\u2082 \u2113\u1d63} {P\u2081 : Set \u2113\u2081} {P\u2082 : Set \u2113\u2082} (P\u1d63 : P\u2081 \u2192 P\u2082 \u2192 Set \u2113\u1d63) : Dec P\u2081 \u2192 Dec P\u2082 \u2192 Set (\u2113\u2081 \u2294 \u2113\u2082 \u2294 \u2113\u1d63) where\n-- data \u27e6Dec\u27e7 {\u2113\u1d63} : \u27e6Pred\u27e7 (\u27e6Set\u27e7 \u2113\u1d63) \u2113\u1d63 Dec Dec where\ndata \u27e6Dec\u27e7 {\u2113\u2081 \u2113\u2082 \u2113\u1d63} {P\u2081 : Set \u2113\u2081} {P\u2082 : Set \u2113\u2082} (P\u1d63 : P\u2081 \u2192 P\u2082 \u2192 Set \u2113\u1d63) : \u27e6Set\u27e7 (\u2113\u2081 \u2294 \u2113\u2082 \u2294 \u2113\u1d63) (Dec P\u2081) (Dec P\u2082) where\n yes : {-\u2200 {P\u2081 P\u2082 P\u1d63}-} {p\u2081 : P\u2081} {p\u2082 : P\u2082} (p\u1d63 : P\u1d63 p\u2081 p\u2082) \u2192 \u27e6Dec\u27e7 P\u1d63 (yes p\u2081) (yes p\u2082)\n no : {-\u2200 {P\u2081 P\u2082 P\u1d63}-} {\u00acp\u2081 : \u00ac P\u2081} {\u00acp\u2082 : \u00ac P\u2082} (\u00acp\u1d63 : (\u27e6\u00ac\u27e7 P\u1d63) \u00acp\u2081 \u00acp\u2082) \u2192 \u27e6Dec\u27e7 P\u1d63 (no \u00acp\u2081) (no \u00acp\u2082)\n\n--\u27e6Decidable\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63 b\u2081 b\u2082 b\u1d63 \u2113\u2081 \u2113\u2082 \u2113\u1d63} \u2192 (\u2200\u27e8 A\u1d63 \u2236 \u27e6Set\u27e7 {a\u2081} {a\u2082} a\u1d63 \u27e9\u27e6\u2192\u27e7 \u2200\u27e8 B\u1d63 \u2236 \u27e6Set\u27e7 {b\u2081 b\u2082} b\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6REL\u27e7 A\u1d63 B\u1d63 {\u2113\u2081} {\u2113\u2082} \u2113\u1d63 \u27e6\u2192\u27e7 \u27e6Set\u27e7 _) Decidable Decidable\n\u27e6Decidable\u27e7 : \u2200 {a\u2081 a\u2082 a\u1d63} {A\u2081 : Set a\u2081} {A\u2082 : Set a\u2082} (A\u1d63 : A\u2081 \u2192 A\u2082 \u2192 Set a\u1d63)\n {b\u2081 b\u2082 b\u1d63} {B\u2081 : Set b\u2081} {B\u2082 : Set b\u2082} (B\u1d63 : B\u2081 \u2192 B\u2082 \u2192 Set b\u1d63)\n {\u2113\u2081 \u2113\u2082 \u2113\u1d63} {\u223c\u2081 : REL A\u2081 B\u2081 \u2113\u2081} {\u223c\u2082 : REL A\u2082 B\u2082 \u2113\u2082} (\u223c\u1d63 : \u27e6REL\u27e7 A\u1d63 B\u1d63 \u2113\u1d63 \u223c\u2081 \u223c\u2082)\n \u2192 Decidable \u223c\u2081 \u2192 Decidable \u223c\u2082 \u2192 Set _\n\u27e6Decidable\u27e7 A\u1d63 B\u1d63 _\u223c\u1d63_ = \u27e8 x\u1d63 \u2236 A\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e8 y\u1d63 \u2236 B\u1d63 \u27e9\u27e6\u2192\u27e7 \u27e6Dec\u27e7 (x\u1d63 \u223c\u1d63 y\u1d63)\n\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"ddde3b69f6588b06031bd633774e1f4669ef00f6","subject":"Cost: adapt to Two","message":"Cost: adapt to Two\n","repos":"crypto-agda\/crypto-agda","old_file":"FunUniverse\/Cost.agda","new_file":"FunUniverse\/Cost.agda","new_contents":"-- NOTE with-K\nmodule FunUniverse.Cost where\n\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2*_; 2^_; _^_; _\u2294_; module \u2115\u00b0; module \u2294\u00b0; 2*\u2032_)\nopen import Data.Bool using (true; false)\nopen import Data.One\nimport Data.DifferenceNat\nimport Data.Vec.NP as V\nimport Function as F\nimport Data.Product as \u00d7\nimport Relation.Binary.PropositionalEquality as \u2261\nopen F using (const; _\u2218\u2032_)\nopen V using (Vec; []; _\u2237_; _++_; [_])\nopen \u00d7 using (_\u00d7_)\nopen \u2261 using (_\u2261_; _\u2257_)\nopen import Level.NP hiding (_\u2294_)\n\nopen import Data.Bits using (Bits; 0\u2237_; 1\u2237_; _\u2192\u1d47_)\n\nopen import FunUniverse.Core\nopen import FunUniverse.Category\nopen import FunUniverse.Rewiring.Linear\nopen import FunUniverse.Const\n\nmodule D where\n open Data.DifferenceNat public renaming (suc to suc#; _+_ to _+#_)\n _*#_ : \u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n zero *# d = 0#\n suc n *# d = (n *# d) +# d\n _*#\u2032_ : \u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n zero *#\u2032 d = 0#\n suc n *#\u2032 d = d +# (n *#\u2032 d)\n 2*#_ : Diff\u2115 \u2192 Diff\u2115\n 2*# n = n +# n\n 2^#_ : \u2115 \u2192 Diff\u2115\n 2^# zero = 1#\n 2^# suc n = 2*# (2^# n)\n 1+_+_D : Diff\u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n 1+ x + y D = 1# \u2218\u2032 (x \u2218\u2032 y)\nopen D using (Diff\u2115)\n\nprivate\n 1+_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n 1+ x + y = 1 + (x + y)\n 1+_\u2294_ : \u2115 \u2192 \u2115 \u2192 \u2115\n 1+ x \u2294 y = 1 + (x \u2294 y)\n i\u2294i\u2261i : \u2200 i \u2192 i \u2294 i \u2261 i\n i\u2294i\u2261i zero = \u2261.refl\n i\u2294i\u2261i (suc i) = \u2261.cong suc (i\u2294i\u2261i i)\n\n{-\nseqTimeOpsD : FunOps (constFuns Diff\u2115)\nseqTimeOpsD = record {\n id = 0#; _\u2218_ = _\u2218\u2032_;\n <0\u2082> = 0#; <1\u2082> = 0#; cond = 1#; fork = 1+_+_D; tt = 0#;\n <_,_> = _\u2218\u2032_; fst = 0#; snd = 0#;\n dup = 0#; first = F.id; swap = 0#; assoc = 0#;\n = 0#; snd = 0#;\n <_\u00d7_> = _\u2218\u2032_; second = F.id;\n <[]> = 0#; <\u2237> = 0#; uncons = 0# }\n where open D\n\nmodule SeqTimeOpsD where\n Time = Diff\u2115\n open D\n open FunOps seqTimeOpsD public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2257 0#\n snoc\u22610 zero x = \u2261.refl\n snoc\u22610 (suc n) x rewrite snoc\u22610 n x = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2257 0#\n reverse\u22610 zero x = \u2261.refl\n reverse\u22610 (suc n) x rewrite reverse\u22610 n x | snoc\u22610 n x = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2257 0#\n replicate\u22610 zero = \u03bb _ \u2192 \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2257 0#\n constBit\u22610 true {-1\u2082-} _ = \u2261.refl\n constBit\u22610 false {-0\u2082-} _ = \u2261.refl\n\n sum\u2032 : \u2200 {n} \u2192 Vec Diff\u2115 n \u2192 Diff\u2115\n sum\u2032 = V.foldr (const Diff\u2115) _\u2218\u2032_ id\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec {n} f xs \u2257 sum\u2032 (V.map f xs)\n constVec\u2261sum f [] x = \u2261.refl\n constVec\u2261sum {suc n} f (b \u2237 bs) x rewrite \u2115\u00b0.+-comm (f b x \u2294 constVec f bs x) 0\n | constVec\u2261sum f bs x = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2257 0#\n constBits\u22610 [] x = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) x rewrite constBits\u22610 bs x\n | constBit\u22610 b x = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2257 m *# n\n foldl\u2261* zero n x = \u2261.refl\n foldl\u2261* (suc m) n x rewrite foldl\u2261* m n (n x) = \u2261.refl\n\n foldr\u2261* : \u2200 m n \u2192 foldr {m} n \u2257 m *#\u2032 n\n foldr\u2261* zero n x = \u2261.refl\n foldr\u2261* (suc m) n x rewrite foldr\u2261* m n x = \u2261.refl\n\n #nodes : \u2200 i \u2192 Diff\u2115\n #nodes zero = 0#\n #nodes (suc i) = 1# +# #nodes i +# #nodes i\n\n fromBitsFun\u2261 : \u2200 {i o} (f : i \u2192\u1d47 o) \u2192 fromBitsFun f \u2257 #nodes i\n fromBitsFun\u2261 {zero} f x = constBits\u22610 (f []) x\n fromBitsFun\u2261 {suc i} f x rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_) x\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_) (#nodes i x) = \u2261.refl\n-}\n\nTime = \u2115\nTimeCost = constFuns Time\nSpace = \u2115\nSpaceCost = constFuns Space\n\nseqTimeCat : Category {\u2080} {\u2080} {\ud835\udfd9} (\u03bb _ _ \u2192 Time)\nseqTimeCat = 0 , _+_\n\nseqTimeLin : LinRewiring TimeCost\nseqTimeLin =\n record {\n cat = seqTimeCat;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _+_;\n second = F.id;\n tt\u2192[] = 0;\n []\u2192tt = 0;\n <\u2237> = 0;\n uncons = 0 }\n\nseqTimeRewiring : Rewiring TimeCost\nseqTimeRewiring =\n record {\n linRewiring = seqTimeLin;\n tt = 0;\n dup = 0;\n <[]> = 0;\n <_,_> = _+_;\n fst = 0;\n snd = 0;\n rewire = const 0;\n rewireTbl = const 0 }\n\nseqTimeFork : HasFork TimeCost\nseqTimeFork = record { cond = 1; fork = 1+_+_ }\n\nseqTimeOps : FunOps TimeCost\nseqTimeOps = record { rewiring = seqTimeRewiring; hasFork = seqTimeFork;\n <0\u2082> = 0; <1\u2082> = 0 }\n\nseqTimeBij : Bijective TimeCost\nseqTimeBij = FunOps.bijective seqTimeOps\n\ntimeCat : Category (\u03bb _ _ \u2192 Time)\ntimeCat = seqTimeCat\n\ntimeLin : LinRewiring TimeCost\ntimeLin =\n record {\n cat = timeCat;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _\u2294_;\n second = F.id;\n tt\u2192[] = 0;\n []\u2192tt = 0;\n <\u2237> = 0;\n uncons = 0 }\n\ntimeRewiring : Rewiring TimeCost\ntimeRewiring =\n record {\n linRewiring = timeLin;\n tt = 0;\n dup = 0;\n <[]> = 0;\n <_,_> = _\u2294_;\n fst = 0;\n snd = 0;\n rewire = const 0;\n rewireTbl = const 0 }\n\ntimeFork : HasFork TimeCost\ntimeFork = record { cond = 1; fork = 1+_\u2294_ }\n\ntimeOps : FunOps TimeCost\ntimeOps = record { rewiring = timeRewiring; hasFork = timeFork;\n <0\u2082> = 0; <1\u2082> = 0 }\n\n{-\ntimeOps\u2261seqTimeOps : timeOps \u2261 record seqTimeOps {\n rewiring = record seqTimeRewiring {\n linRewiring = record seqTimeLin { <_\u00d7_> = _\u2294_ };\n <_,_> = _\u2294_};\n fork = 1+_\u2294_ }\n {-;\n <\u2237> = 0; uncons = 0 } -- Without <\u2237> = 0... this definition makes\n -- the FunOps record def yellow\n -}\ntimeOps\u2261seqTimeOps = \u2261.refl\n-}\n\n{-\nopen import maxsemiring\nopen \u2294-+-SemiringSolver\ntimeOps' : \u2200 n \u2192 FunOps (constFuns (Polynomial n))\ntimeOps' n = record {\n id = con 0; _\u2218_ = _:*_;\n <0\u2082> = con 0; <1\u2082> = con 0; cond = con 1; fork = 1+_+_; tt = con 0;\n <_,_> = _:*_; fst = con 0; snd = con 0;\n dup = con 0; <_\u00d7_> = _:+_; swap = con 0; assoc = con 0;\n = con 0; snd = con 0;\n <[]> = con 0; <\u2237> = con 0; uncons = con 0 }\n where 1+_+_ : Polynomial n \u2192 Polynomial n \u2192 Polynomial n\n 1+ x + y = con 1 :* (x :* y)\n\nModule TimeOps' where\n Time = \u2115\n open FunOps (timeOps' 1) public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} := con 0\n snoc\u22610 zero = ?\n snoc\u22610 (suc n) = ?\n-}\n\nmodule TimeOps where\n open FunOps timeOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2294\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2261 0\n replicate\u22610 zero = \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2261 0\n constBit\u22610 true = \u2261.refl\n constBit\u22610 false = \u2261.refl\n\n maximum : \u2200 {n} \u2192 Vec \u2115 n \u2192 \u2115\n maximum = V.foldr (const \u2115) (\u03bb {_} x y \u2192 x \u2294 y) 0\n\n constVec\u22a4\u2261maximum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec\u22a4 {n} f xs \u2261 maximum (V.map f xs)\n constVec\u22a4\u2261maximum f [] = \u2261.refl\n constVec\u22a4\u2261maximum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b \u2294 constVec\u22a4 f bs) 0\n | constVec\u22a4\u2261maximum f bs = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2261 0\n constBits\u22610 [] = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) rewrite constBit\u22610 b\n | constBits\u22610 bs = \u2261.refl\n\n fromBitsFun\u2261i : \u2200 {i o} (f : i \u2192\u1d47 o) \u2192 fromBitsFun f \u2261 i\n fromBitsFun\u2261i {zero} f rewrite constBits\u22610 (f []) = \u2261.refl\n fromBitsFun\u2261i {suc i} f rewrite fromBitsFun\u2261i {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261i {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (i \u2294 i) 0\n = \u2261.cong suc (i\u2294i\u2261i i)\n\n {-\nspaceLin : LinRewiring SpaceCost\nspaceLin =\n record {\n id = 0;\n _\u2218_ = _+_;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _+_;\n second = F.id;\n tt\u2192[] = 0;\n []\u2192tt = 0;\n <\u2237> = 0;\n uncons = 0 }\n\nspaceLin\u2261seqTimeLin : spaceLin \u2261 seqTimeLin\nspaceLin\u2261seqTimeLin = \u2261.refl\n\nspaceRewiring : Rewiring TimeCost\nspaceRewiring =\n record {\n linRewiring = spaceLin;\n tt = 0;\n dup = 1;\n <[]> = 0;\n <_,_> = 1+_+_;\n fst = 0;\n snd = 0;\n rewire = \u03bb {_} {o} _ \u2192 o;\n rewireTbl = \u03bb {_} {o} _ \u2192 o }\n\nspaceFork : HasFork SpaceCost\nspaceFork = record { cond = 1; fork = 1+_+_ }\n\nspaceOps : FunOps SpaceCost\nspaceOps = record { rewiring = spaceRewiring; hasFork = spaceFork;\n <0\u2082> = 1; <1\u2082> = 1 }\n\n {-\n-- So far the space cost model is like the sequential time cost model but makes <0\u2082>,<1\u2082>,dup\n-- cost one unit of space.\nspaceOps\u2261seqTimeOps : spaceOps \u2261 record seqTimeOps { <0\u2082> = 1; <1\u2082> = 1; dup = 1; <_,_> = 1+_+_\n ; <\u2237> = 0; uncons = 0 } -- same bug here\nspaceOps\u2261seqTimeOps = \u2261.refl\n-}\n\nmodule SpaceOps where\n open FunOps spaceOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2115\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u2261n : \u2200 n \u2192 replicate {n} \u2261 n\n replicate\u2261n zero = \u2261.refl\n replicate\u2261n (suc n) rewrite replicate\u2261n n = \u2261.refl\n\n constBit\u22611 : \u2200 b \u2192 constBit b \u2261 1\n constBit\u22611 true = \u2261.refl\n constBit\u22611 false = \u2261.refl\n\n constVec\u22a4\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Space) xs \u2192 constVec\u22a4 {n} f xs \u2261 V.sum (V.map f xs)\n constVec\u22a4\u2261sum f [] = \u2261.refl\n constVec\u22a4\u2261sum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b + constVec\u22a4 f bs) 0\n | constVec\u22a4\u2261sum f bs = \u2261.refl\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Space) xs \u2192 constVec {n} f xs \u2261 V.sum (V.map f xs)\n constVec\u2261sum f xs rewrite constVec\u22a4\u2261sum f xs | \u2115\u00b0.+-comm (V.sum (V.map f xs)) 0 = \u2261.refl\n\n constBits\u2261n : \u2200 {n} xs \u2192 constBits {n} xs \u2261 n\n constBits\u2261n [] = \u2261.refl\n constBits\u2261n {suc n} (b \u2237 bs) rewrite constBit\u22611 b\n | constBits\u2261n bs\n | \u2115\u00b0.+-comm n 0 = \u2261.refl\n\n fromBitsFun-cost : \u2200 (i o : \u2115) \u2192 \u2115\n fromBitsFun-cost zero o = o\n fromBitsFun-cost (suc i) o = 1 + 2*(fromBitsFun-cost i o)\n\n fromBitsFun\u2261 : \u2200 {i o} (f : i \u2192\u1d47 o) \u2192 fromBitsFun f \u2261 fromBitsFun-cost i o\n fromBitsFun\u2261 {zero} {o} f rewrite constBits\u2261n (f []) | \u2115\u00b0.+-comm o 0 = \u2261.refl\n fromBitsFun\u2261 {suc i} {o} f rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (2* (fromBitsFun-cost i o)) 0\n = \u2261.refl\n\n{-\ntime\u00d7spaceOps : FunOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-Ops timeOps spaceOps\n\nmodule Time\u00d7SpaceOps = FunOps time\u00d7spaceOps\n-}\n-- -}\n-- -}\n-- -}\n-- -}\n","old_contents":"-- NOTE with-K\nmodule FunUniverse.Cost where\n\nopen import Data.Nat.NP using (\u2115; zero; suc; _+_; _*_; 2*_; 2^_; _^_; _\u2294_; module \u2115\u00b0; module \u2294\u00b0; 2*\u2032_)\nopen import Data.Bool using (true; false)\nopen import Data.One\nimport Data.DifferenceNat\nimport Data.Vec.NP as V\nimport Function as F\nimport Data.Product as \u00d7\nimport Relation.Binary.PropositionalEquality as \u2261\nopen F using (const; _\u2218\u2032_)\nopen V using (Vec; []; _\u2237_; _++_; [_])\nopen \u00d7 using (_\u00d7_)\nopen \u2261 using (_\u2261_; _\u2257_)\nopen import Level.NP hiding (_\u2294_)\n\nopen import Data.Bits using (Bits; 0\u2237_; 1\u2237_; _\u2192\u1d47_)\n\nopen import FunUniverse.Core\nopen import FunUniverse.Category\nopen import FunUniverse.Rewiring.Linear\nopen import FunUniverse.Const\n\nmodule D where\n open Data.DifferenceNat public renaming (suc to suc#; _+_ to _+#_)\n _*#_ : \u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n zero *# d = 0#\n suc n *# d = (n *# d) +# d\n _*#\u2032_ : \u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n zero *#\u2032 d = 0#\n suc n *#\u2032 d = d +# (n *#\u2032 d)\n 2*#_ : Diff\u2115 \u2192 Diff\u2115\n 2*# n = n +# n\n 2^#_ : \u2115 \u2192 Diff\u2115\n 2^# zero = 1#\n 2^# suc n = 2*# (2^# n)\n 1+_+_D : Diff\u2115 \u2192 Diff\u2115 \u2192 Diff\u2115\n 1+ x + y D = 1# \u2218\u2032 (x \u2218\u2032 y)\nopen D using (Diff\u2115)\n\nprivate\n 1+_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n 1+ x + y = 1 + (x + y)\n 1+_\u2294_ : \u2115 \u2192 \u2115 \u2192 \u2115\n 1+ x \u2294 y = 1 + (x \u2294 y)\n i\u2294i\u2261i : \u2200 i \u2192 i \u2294 i \u2261 i\n i\u2294i\u2261i zero = \u2261.refl\n i\u2294i\u2261i (suc i) = \u2261.cong suc (i\u2294i\u2261i i)\n\n{-\nseqTimeOpsD : FunOps (constFuns Diff\u2115)\nseqTimeOpsD = record {\n id = 0#; _\u2218_ = _\u2218\u2032_;\n <0b> = 0#; <1b> = 0#; cond = 1#; fork = 1+_+_D; tt = 0#;\n <_,_> = _\u2218\u2032_; fst = 0#; snd = 0#;\n dup = 0#; first = F.id; swap = 0#; assoc = 0#;\n = 0#; snd = 0#;\n <_\u00d7_> = _\u2218\u2032_; second = F.id;\n <[]> = 0#; <\u2237> = 0#; uncons = 0# }\n where open D\n\nmodule SeqTimeOpsD where\n Time = Diff\u2115\n open D\n open FunOps seqTimeOpsD public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2257 0#\n snoc\u22610 zero x = \u2261.refl\n snoc\u22610 (suc n) x rewrite snoc\u22610 n x = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2257 0#\n reverse\u22610 zero x = \u2261.refl\n reverse\u22610 (suc n) x rewrite reverse\u22610 n x | snoc\u22610 n x = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2257 0#\n replicate\u22610 zero = \u03bb _ \u2192 \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2257 0#\n constBit\u22610 true {-1b-} _ = \u2261.refl\n constBit\u22610 false {-0b-} _ = \u2261.refl\n\n sum\u2032 : \u2200 {n} \u2192 Vec Diff\u2115 n \u2192 Diff\u2115\n sum\u2032 = V.foldr (const Diff\u2115) _\u2218\u2032_ id\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec {n} f xs \u2257 sum\u2032 (V.map f xs)\n constVec\u2261sum f [] x = \u2261.refl\n constVec\u2261sum {suc n} f (b \u2237 bs) x rewrite \u2115\u00b0.+-comm (f b x \u2294 constVec f bs x) 0\n | constVec\u2261sum f bs x = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2257 0#\n constBits\u22610 [] x = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) x rewrite constBits\u22610 bs x\n | constBit\u22610 b x = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2257 m *# n\n foldl\u2261* zero n x = \u2261.refl\n foldl\u2261* (suc m) n x rewrite foldl\u2261* m n (n x) = \u2261.refl\n\n foldr\u2261* : \u2200 m n \u2192 foldr {m} n \u2257 m *#\u2032 n\n foldr\u2261* zero n x = \u2261.refl\n foldr\u2261* (suc m) n x rewrite foldr\u2261* m n x = \u2261.refl\n\n #nodes : \u2200 i \u2192 Diff\u2115\n #nodes zero = 0#\n #nodes (suc i) = 1# +# #nodes i +# #nodes i\n\n fromBitsFun\u2261 : \u2200 {i o} (f : i \u2192\u1d47 o) \u2192 fromBitsFun f \u2257 #nodes i\n fromBitsFun\u2261 {zero} f x = constBits\u22610 (f []) x\n fromBitsFun\u2261 {suc i} f x rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_) x\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_) (#nodes i x) = \u2261.refl\n-}\n\nTime = \u2115\nTimeCost = constFuns Time\nSpace = \u2115\nSpaceCost = constFuns Space\n\nseqTimeCat : Category {\u2080} {\u2080} {\ud835\udfd9} (\u03bb _ _ \u2192 Time)\nseqTimeCat = 0 , _+_\n\nseqTimeLin : LinRewiring TimeCost\nseqTimeLin =\n record {\n cat = seqTimeCat;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _+_;\n second = F.id;\n tt\u2192[] = 0;\n []\u2192tt = 0;\n <\u2237> = 0;\n uncons = 0 }\n\nseqTimeRewiring : Rewiring TimeCost\nseqTimeRewiring =\n record {\n linRewiring = seqTimeLin;\n tt = 0;\n dup = 0;\n <[]> = 0;\n <_,_> = _+_;\n fst = 0;\n snd = 0;\n rewire = const 0;\n rewireTbl = const 0 }\n\nseqTimeFork : HasFork TimeCost\nseqTimeFork = record { cond = 1; fork = 1+_+_ }\n\nseqTimeOps : FunOps TimeCost\nseqTimeOps = record { rewiring = seqTimeRewiring; hasFork = seqTimeFork;\n <0b> = 0; <1b> = 0 }\n\nseqTimeBij : Bijective TimeCost\nseqTimeBij = FunOps.bijective seqTimeOps\n\ntimeCat : Category (\u03bb _ _ \u2192 Time)\ntimeCat = seqTimeCat\n\ntimeLin : LinRewiring TimeCost\ntimeLin =\n record {\n cat = timeCat;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _\u2294_;\n second = F.id;\n tt\u2192[] = 0;\n []\u2192tt = 0;\n <\u2237> = 0;\n uncons = 0 }\n\ntimeRewiring : Rewiring TimeCost\ntimeRewiring =\n record {\n linRewiring = timeLin;\n tt = 0;\n dup = 0;\n <[]> = 0;\n <_,_> = _\u2294_;\n fst = 0;\n snd = 0;\n rewire = const 0;\n rewireTbl = const 0 }\n\ntimeFork : HasFork TimeCost\ntimeFork = record { cond = 1; fork = 1+_\u2294_ }\n\ntimeOps : FunOps TimeCost\ntimeOps = record { rewiring = timeRewiring; hasFork = timeFork;\n <0b> = 0; <1b> = 0 }\n\n{-\ntimeOps\u2261seqTimeOps : timeOps \u2261 record seqTimeOps {\n rewiring = record seqTimeRewiring {\n linRewiring = record seqTimeLin { <_\u00d7_> = _\u2294_ };\n <_,_> = _\u2294_};\n fork = 1+_\u2294_ }\n {-;\n <\u2237> = 0; uncons = 0 } -- Without <\u2237> = 0... this definition makes\n -- the FunOps record def yellow\n -}\ntimeOps\u2261seqTimeOps = \u2261.refl\n-}\n\n{-\nopen import maxsemiring\nopen \u2294-+-SemiringSolver\ntimeOps' : \u2200 n \u2192 FunOps (constFuns (Polynomial n))\ntimeOps' n = record {\n id = con 0; _\u2218_ = _:*_;\n <0b> = con 0; <1b> = con 0; cond = con 1; fork = 1+_+_; tt = con 0;\n <_,_> = _:*_; fst = con 0; snd = con 0;\n dup = con 0; <_\u00d7_> = _:+_; swap = con 0; assoc = con 0;\n = con 0; snd = con 0;\n <[]> = con 0; <\u2237> = con 0; uncons = con 0 }\n where 1+_+_ : Polynomial n \u2192 Polynomial n \u2192 Polynomial n\n 1+ x + y = con 1 :* (x :* y)\n\nModule TimeOps' where\n Time = \u2115\n open FunOps (timeOps' 1) public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} := con 0\n snoc\u22610 zero = ?\n snoc\u22610 (suc n) = ?\n-}\n\nmodule TimeOps where\n open FunOps timeOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2294\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u22610 : \u2200 n \u2192 replicate {n} \u2261 0\n replicate\u22610 zero = \u2261.refl\n replicate\u22610 (suc n) = replicate\u22610 n\n\n constBit\u22610 : \u2200 b \u2192 constBit b \u2261 0\n constBit\u22610 true = \u2261.refl\n constBit\u22610 false = \u2261.refl\n\n maximum : \u2200 {n} \u2192 Vec \u2115 n \u2192 \u2115\n maximum = V.foldr (const \u2115) (\u03bb {_} x y \u2192 x \u2294 y) 0\n\n constVec\u22a4\u2261maximum : \u2200 {n b} {B : Set b} (f : B \u2192 Time) xs \u2192 constVec\u22a4 {n} f xs \u2261 maximum (V.map f xs)\n constVec\u22a4\u2261maximum f [] = \u2261.refl\n constVec\u22a4\u2261maximum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b \u2294 constVec\u22a4 f bs) 0\n | constVec\u22a4\u2261maximum f bs = \u2261.refl\n\n constBits\u22610 : \u2200 {n} xs \u2192 constBits {n} xs \u2261 0\n constBits\u22610 [] = \u2261.refl\n constBits\u22610 {suc n} (b \u2237 bs) rewrite constBit\u22610 b\n | constBits\u22610 bs = \u2261.refl\n\n fromBitsFun\u2261i : \u2200 {i o} (f : i \u2192\u1d47 o) \u2192 fromBitsFun f \u2261 i\n fromBitsFun\u2261i {zero} f rewrite constBits\u22610 (f []) = \u2261.refl\n fromBitsFun\u2261i {suc i} f rewrite fromBitsFun\u2261i {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261i {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (i \u2294 i) 0\n = \u2261.cong suc (i\u2294i\u2261i i)\n\n {-\nspaceLin : LinRewiring SpaceCost\nspaceLin =\n record {\n id = 0;\n _\u2218_ = _+_;\n first = F.id;\n swap = 0;\n assoc = 0;\n = 0;\n snd = 0;\n <_\u00d7_> = _+_;\n second = F.id;\n tt\u2192[] = 0;\n []\u2192tt = 0;\n <\u2237> = 0;\n uncons = 0 }\n\nspaceLin\u2261seqTimeLin : spaceLin \u2261 seqTimeLin\nspaceLin\u2261seqTimeLin = \u2261.refl\n\nspaceRewiring : Rewiring TimeCost\nspaceRewiring =\n record {\n linRewiring = spaceLin;\n tt = 0;\n dup = 1;\n <[]> = 0;\n <_,_> = 1+_+_;\n fst = 0;\n snd = 0;\n rewire = \u03bb {_} {o} _ \u2192 o;\n rewireTbl = \u03bb {_} {o} _ \u2192 o }\n\nspaceFork : HasFork SpaceCost\nspaceFork = record { cond = 1; fork = 1+_+_ }\n\nspaceOps : FunOps SpaceCost\nspaceOps = record { rewiring = spaceRewiring; hasFork = spaceFork;\n <0b> = 1; <1b> = 1 }\n\n {-\n-- So far the space cost model is like the sequential time cost model but makes <0b>,<1b>,dup\n-- cost one unit of space.\nspaceOps\u2261seqTimeOps : spaceOps \u2261 record seqTimeOps { <0b> = 1; <1b> = 1; dup = 1; <_,_> = 1+_+_\n ; <\u2237> = 0; uncons = 0 } -- same bug here\nspaceOps\u2261seqTimeOps = \u2261.refl\n-}\n\nmodule SpaceOps where\n open FunOps spaceOps public\n\n snoc\u22610 : \u2200 n \u2192 snoc {n} \u2261 0\n snoc\u22610 zero = \u2261.refl\n snoc\u22610 (suc n) rewrite snoc\u22610 n = \u2261.refl\n\n reverse\u22610 : \u2200 n \u2192 reverse {n} \u2261 0\n reverse\u22610 zero = \u2261.refl\n reverse\u22610 (suc n) rewrite snoc\u22610 n | reverse\u22610 n = \u2261.refl\n\n foldl\u2261* : \u2200 m n \u2192 foldl {m} n \u2261 m * n\n foldl\u2261* zero n = \u2261.refl\n foldl\u2261* (suc m) n rewrite foldl\u2261* m n\n | \u2115\u00b0.+-comm n 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n + n) 0\n | \u2115\u00b0.+-comm (m * n) n\n = \u2261.refl\n\n replicate\u2261n : \u2200 n \u2192 replicate {n} \u2261 n\n replicate\u2261n zero = \u2261.refl\n replicate\u2261n (suc n) rewrite replicate\u2261n n = \u2261.refl\n\n constBit\u22611 : \u2200 b \u2192 constBit b \u2261 1\n constBit\u22611 true = \u2261.refl\n constBit\u22611 false = \u2261.refl\n\n constVec\u22a4\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Space) xs \u2192 constVec\u22a4 {n} f xs \u2261 V.sum (V.map f xs)\n constVec\u22a4\u2261sum f [] = \u2261.refl\n constVec\u22a4\u2261sum {suc n} f (b \u2237 bs) rewrite \u2115\u00b0.+-comm (f b + constVec\u22a4 f bs) 0\n | constVec\u22a4\u2261sum f bs = \u2261.refl\n\n constVec\u2261sum : \u2200 {n b} {B : Set b} (f : B \u2192 Space) xs \u2192 constVec {n} f xs \u2261 V.sum (V.map f xs)\n constVec\u2261sum f xs rewrite constVec\u22a4\u2261sum f xs | \u2115\u00b0.+-comm (V.sum (V.map f xs)) 0 = \u2261.refl\n\n constBits\u2261n : \u2200 {n} xs \u2192 constBits {n} xs \u2261 n\n constBits\u2261n [] = \u2261.refl\n constBits\u2261n {suc n} (b \u2237 bs) rewrite constBit\u22611 b\n | constBits\u2261n bs\n | \u2115\u00b0.+-comm n 0 = \u2261.refl\n\n fromBitsFun-cost : \u2200 (i o : \u2115) \u2192 \u2115\n fromBitsFun-cost zero o = o\n fromBitsFun-cost (suc i) o = 1 + 2*(fromBitsFun-cost i o)\n\n fromBitsFun\u2261 : \u2200 {i o} (f : i \u2192\u1d47 o) \u2192 fromBitsFun f \u2261 fromBitsFun-cost i o\n fromBitsFun\u2261 {zero} {o} f rewrite constBits\u2261n (f []) | \u2115\u00b0.+-comm o 0 = \u2261.refl\n fromBitsFun\u2261 {suc i} {o} f rewrite fromBitsFun\u2261 {i} (f \u2218\u2032 1\u2237_)\n | fromBitsFun\u2261 {i} (f \u2218\u2032 0\u2237_)\n | \u2115\u00b0.+-comm (2* (fromBitsFun-cost i o)) 0\n = \u2261.refl\n\n{-\ntime\u00d7spaceOps : FunOps (constFuns (\u2115 \u00d7 \u2115))\ntime\u00d7spaceOps = \u00d7\u22a4-Ops timeOps spaceOps\n\nmodule Time\u00d7SpaceOps = FunOps time\u00d7spaceOps\n-}\n-- -}\n-- -}\n-- -}\n-- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"2cd47f65437e3b825c70b2ef65e9b9187895f050","subject":"rel to #45, turns out we do actually already prove the more general preservation statement","message":"rel to #45, turns out we do actually already prove the more general preservation statement\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"preservation.agda","new_file":"preservation.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nopen import lemmas-consistency\nopen import type-assignment-unicity\n\nopen import structural-assumptions\n\nmodule preservation where\n -- if d and d' both result from filling the hole in \u03b5 with terms of the\n -- same type, they too have the same type.\n wt-different-fill : \u2200{ \u0394 \u0393 d \u03b5 d1 d2 d' \u03c4 \u03c41 } \u2192\n d == \u03b5 \u27e6 d1 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n d' == \u03b5 \u27e6 d2 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n wt-different-fill FHOuter D1 D2 D3 FHOuter\n with type-assignment-unicity D1 D2\n ... | refl = D3\n wt-different-fill (FHAp1 eps) (TAAp D1 D2) D3 D4 (FHAp1 D5) = TAAp (wt-different-fill eps D1 D3 D4 D5) D2\n wt-different-fill (FHAp2 eps) (TAAp D1 D2) D3 D4 (FHAp2 D5) = TAAp D1 (wt-different-fill eps D2 D3 D4 D5)\n wt-different-fill (FHNEHole eps) (TANEHole x D1 x\u2081) D2 D3 (FHNEHole D4) = TANEHole x (wt-different-fill eps D1 D2 D3 D4) x\u2081\n wt-different-fill (FHCast eps) (TACast D1 x) D2 D3 (FHCast D4) = TACast (wt-different-fill eps D1 D2 D3 D4) x\n wt-different-fill (FHFailedCast x) (TAFailedCast y x\u2081 x\u2082 x\u2083) D3 D4 (FHFailedCast eps) = TAFailedCast (wt-different-fill x y D3 D4 eps) x\u2081 x\u2082 x\u2083\n\n -- if a well typed term results from filling the hole in \u03b5, then the term\n -- that filled the hole is also well typed\n wt-filling : \u2200{ \u03b5 \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u03a3[ \u03c4' \u2208 htyp ] (\u0394 , \u0393 \u22a2 d' :: \u03c4')\n wt-filling TAConst FHOuter = _ , TAConst\n wt-filling (TAVar x\u2081) FHOuter = _ , TAVar x\u2081\n wt-filling (TALam f ta) FHOuter = _ , TALam f ta\n\n wt-filling (TAAp ta ta\u2081) FHOuter = _ , TAAp ta ta\u2081\n wt-filling (TAAp ta ta\u2081) (FHAp1 eps) = wt-filling ta eps\n wt-filling (TAAp ta ta\u2081) (FHAp2 eps) = wt-filling ta\u2081 eps\n\n wt-filling (TAEHole x x\u2081) FHOuter = _ , TAEHole x x\u2081\n wt-filling (TANEHole x ta x\u2081) FHOuter = _ , TANEHole x ta x\u2081\n wt-filling (TANEHole x ta x\u2081) (FHNEHole eps) = wt-filling ta eps\n wt-filling (TACast ta x) FHOuter = _ , TACast ta x\n wt-filling (TACast ta x) (FHCast eps) = wt-filling ta eps\n wt-filling (TAFailedCast x y z w) FHOuter = _ , TAFailedCast x y z w\n wt-filling (TAFailedCast x x\u2081 x\u2082 x\u2083) (FHFailedCast y) = wt-filling x y\n\n -- instruction transitions preserve type\n preserve-trans : \u2200{ \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u2192> d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n preserve-trans TAConst ()\n preserve-trans (TAVar x\u2081) ()\n preserve-trans (TALam _ ta) ()\n preserve-trans (TAAp (TALam apt ta) ta\u2081) ITLam = lem-subst apt ta ta\u2081\n preserve-trans (TAAp (TACast ta TCRefl) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 TCRefl)) TCRefl\n preserve-trans (TAAp (TACast ta (TCArr x x\u2081)) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 (~sym x))) x\u2081\n preserve-trans (TAEHole x x\u2081) ()\n preserve-trans (TANEHole x ta x\u2081) ()\n preserve-trans (TACast ta x) (ITCastID) = ta\n preserve-trans (TACast (TACast ta x) x\u2081) (ITCastSucceed x\u2082) = ta\n preserve-trans (TACast ta x) (ITGround (MGArr x\u2081)) = TACast (TACast ta (TCArr TCHole1 TCHole1)) TCHole1\n preserve-trans (TACast ta TCHole2) (ITExpand (MGArr x\u2081)) = TACast (TACast ta TCHole2) (TCArr TCHole2 TCHole2)\n preserve-trans (TACast (TACast ta x) x\u2081) (ITCastFail w y z) = TAFailedCast ta w y z\n preserve-trans (TAFailedCast x y z q) ()\n\n -- this is the main preservation theorem, gluing together the above\n preservation : {\u0394 : hctx} {d d' : dhexp} {\u03c4 : htyp} {\u0393 : tctx} \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u21a6 d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n preservation D (Step x x\u2081 x\u2082)\n with wt-filling D x\n ... | (_ , wt) = wt-different-fill x D wt (preserve-trans wt x\u2081) x\u2082\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nopen import lemmas-consistency\nopen import type-assignment-unicity\n\nopen import structural-assumptions\n\nmodule preservation where\n -- if d and d' both result from filling the hole in \u03b5 with terms of the\n -- same type, they too have the same type.\n wt-different-fill : \u2200{ \u0394 \u0393 d \u03b5 d1 d2 d' \u03c4 \u03c41 } \u2192\n d == \u03b5 \u27e6 d1 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n d' == \u03b5 \u27e6 d2 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n wt-different-fill FHOuter D1 D2 D3 FHOuter\n with type-assignment-unicity D1 D2\n ... | refl = D3\n wt-different-fill (FHAp1 eps) (TAAp D1 D2) D3 D4 (FHAp1 D5) = TAAp (wt-different-fill eps D1 D3 D4 D5) D2\n wt-different-fill (FHAp2 eps) (TAAp D1 D2) D3 D4 (FHAp2 D5) = TAAp D1 (wt-different-fill eps D2 D3 D4 D5)\n wt-different-fill (FHNEHole eps) (TANEHole x D1 x\u2081) D2 D3 (FHNEHole D4) = TANEHole x (wt-different-fill eps D1 D2 D3 D4) x\u2081\n wt-different-fill (FHCast eps) (TACast D1 x) D2 D3 (FHCast D4) = TACast (wt-different-fill eps D1 D2 D3 D4) x\n wt-different-fill (FHFailedCast x) (TAFailedCast y x\u2081 x\u2082 x\u2083) D3 D4 (FHFailedCast eps) = TAFailedCast (wt-different-fill x y D3 D4 eps) x\u2081 x\u2082 x\u2083\n\n -- if a well typed term results from filling the hole in \u03b5, then the term\n -- that filled the hole is also well typed\n wt-filling : \u2200{ \u03b5 \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u03a3[ \u03c4' \u2208 htyp ] (\u0394 , \u0393 \u22a2 d' :: \u03c4')\n wt-filling TAConst FHOuter = _ , TAConst\n wt-filling (TAVar x\u2081) FHOuter = _ , TAVar x\u2081\n wt-filling (TALam f ta) FHOuter = _ , TALam f ta\n\n wt-filling (TAAp ta ta\u2081) FHOuter = _ , TAAp ta ta\u2081\n wt-filling (TAAp ta ta\u2081) (FHAp1 eps) = wt-filling ta eps\n wt-filling (TAAp ta ta\u2081) (FHAp2 eps) = wt-filling ta\u2081 eps\n\n wt-filling (TAEHole x x\u2081) FHOuter = _ , TAEHole x x\u2081\n wt-filling (TANEHole x ta x\u2081) FHOuter = _ , TANEHole x ta x\u2081\n wt-filling (TANEHole x ta x\u2081) (FHNEHole eps) = wt-filling ta eps\n wt-filling (TACast ta x) FHOuter = _ , TACast ta x\n wt-filling (TACast ta x) (FHCast eps) = wt-filling ta eps\n wt-filling (TAFailedCast x y z w) FHOuter = _ , TAFailedCast x y z w\n wt-filling (TAFailedCast x x\u2081 x\u2082 x\u2083) (FHFailedCast y) = wt-filling x y\n\n -- instruction transitions preserve type\n preserve-trans : \u2200{ \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u2192> d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n preserve-trans TAConst ()\n preserve-trans (TAVar x\u2081) ()\n preserve-trans (TALam _ ta) ()\n preserve-trans (TAAp (TALam apt ta) ta\u2081) ITLam = lem-subst apt ta ta\u2081\n preserve-trans (TAAp (TACast ta TCRefl) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 TCRefl)) TCRefl\n preserve-trans (TAAp (TACast ta (TCArr x x\u2081)) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 (~sym x))) x\u2081\n preserve-trans (TAEHole x x\u2081) ()\n preserve-trans (TANEHole x ta x\u2081) ()\n preserve-trans (TACast ta x) (ITCastID) = ta\n preserve-trans (TACast (TACast ta x) x\u2081) (ITCastSucceed x\u2082) = ta\n preserve-trans (TACast ta x) (ITGround (MGArr x\u2081)) = TACast (TACast ta (TCArr TCHole1 TCHole1)) TCHole1\n preserve-trans (TACast ta TCHole2) (ITExpand (MGArr x\u2081)) = TACast (TACast ta TCHole2) (TCArr TCHole2 TCHole2)\n preserve-trans (TACast (TACast ta x) x\u2081) (ITCastFail w y z) = TAFailedCast ta w y z\n preserve-trans (TAFailedCast x y z q) ()\n\n -- this is the main preservation theorem, gluing together the above\n preservation : {\u0394 : hctx} {d d' : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d \u21a6 d' \u2192\n \u0394 , \u2205 \u22a2 d' :: \u03c4\n preservation D (Step x x\u2081 x\u2082)\n with wt-filling D x\n ... | (_ , wt) = wt-different-fill x D wt (preserve-trans wt x\u2081) x\u2082\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"d04b0965999db6ed3cee137b6ea8966697cc5bdf","subject":"Removed unnecessary local hypothesis (by Ana Bove).","message":"Removed unnecessary local hypothesis (by Ana Bove).\n\nIgnore-this: c5e32af0a034aa3e43d885358595e928\n\ndarcs-hash:20110406205609-3bd4e-8dbd2fef9ac0b9955b0b19782da6b97a5ec0dc81.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"src\/FOTC\/Program\/McCarthy91\/Properties\/AuxiliaryATP.agda","new_file":"src\/FOTC\/Program\/McCarthy91\/Properties\/AuxiliaryATP.agda","new_contents":"------------------------------------------------------------------------------\n-- Auxiliary properties of the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule FOTC.Program.McCarthy91.Properties.AuxiliaryATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.IsN-ATP\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\n\nopen import FOTC.Program.McCarthy91.ArithmeticATP\nopen import FOTC.Program.McCarthy91.McCarthy91\n\n------------------------------------------------------------------------------\n\n--- Auxiliary properties\n\n---- Case n > 100\npostulate\n Nmc91>100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192 N (mc91 n)\n x100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192\n LT n (mc91 n + eleven)\n{-# ATP prove Nmc91>100 10-N \u2238-N #-}\n{-# ATP prove x100 +-N \u2238-N x100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810 #-}\n{-# ATP prove mc91-res-100 111>100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810 #-}\n{-# ATP prove mc91-res-100' mc91-res-100 #-}\n{-# ATP prove mc91100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-109 98+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-108 97+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-107 96+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-106 95+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-105 94+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-104 93+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-103 92+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-102 91+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-101 90+11>100 x+11\u223810\u2261Sx #-}\n\npostulate\n mc91-res-99 : mc91 ninety-nine \u2261 ninety-one\n mc91-res-98 : mc91 ninety-eight \u2261 ninety-one\n mc91-res-97 : mc91 ninety-seven \u2261 ninety-one\n mc91-res-96 : mc91 ninety-six \u2261 ninety-one\n mc91-res-95 : mc91 ninety-five \u2261 ninety-one\n mc91-res-94 : mc91 ninety-four \u2261 ninety-one\n mc91-res-93 : mc91 ninety-three \u2261 ninety-one\n mc91-res-92 : mc91 ninety-two \u2261 ninety-one\n mc91-res-91 : mc91 ninety-one \u2261 ninety-one\n mc91-res-90 : mc91 ninety \u2261 ninety-one\n{-# ATP prove mc91-res-99 mc91x-res\u2264100 mc91-res-110 mc91-res-100 #-}\n{-# ATP prove mc91-res-98 mc91x-res\u2264100 mc91-res-109 mc91-res-99 #-}\n{-# ATP prove mc91-res-97 mc91x-res\u2264100 mc91-res-108 mc91-res-98 #-}\n{-# ATP prove mc91-res-96 mc91x-res\u2264100 mc91-res-107 mc91-res-97 #-}\n{-# ATP prove mc91-res-95 mc91x-res\u2264100 mc91-res-106 mc91-res-96 #-}\n{-# ATP prove mc91-res-94 mc91x-res\u2264100 mc91-res-105 mc91-res-95 #-}\n{-# ATP prove mc91-res-93 mc91x-res\u2264100 mc91-res-104 mc91-res-94 #-}\n{-# ATP prove mc91-res-92 mc91x-res\u2264100 mc91-res-103 mc91-res-93 #-}\n{-# ATP prove mc91-res-91 mc91x-res\u2264100 mc91-res-102 mc91-res-92 #-}\n{-# ATP prove mc91-res-90 mc91x-res\u2264100 mc91-res-101 mc91-res-91 #-}\n\nmc91-res-99' : \u2200 {n} \u2192 n \u2261 ninety-nine \u2192 mc91 n \u2261 ninety-one\nmc91-res-99' refl = mc91-res-99\n\nmc91-res-98' : \u2200 {n} \u2192 n \u2261 ninety-eight \u2192 mc91 n \u2261 ninety-one\nmc91-res-98' refl = mc91-res-98\n\nmc91-res-97' : \u2200 {n} \u2192 n \u2261 ninety-seven \u2192 mc91 n \u2261 ninety-one\nmc91-res-97' refl = mc91-res-97\n\nmc91-res-96' : \u2200 {n} \u2192 n \u2261 ninety-six \u2192 mc91 n \u2261 ninety-one\nmc91-res-96' refl = mc91-res-96\n\nmc91-res-95' : \u2200 {n} \u2192 n \u2261 ninety-five \u2192 mc91 n \u2261 ninety-one\nmc91-res-95' refl = mc91-res-95\n\nmc91-res-94' : \u2200 {n} \u2192 n \u2261 ninety-four \u2192 mc91 n \u2261 ninety-one\nmc91-res-94' refl = mc91-res-94\n\nmc91-res-93' : \u2200 {n} \u2192 n \u2261 ninety-three \u2192 mc91 n \u2261 ninety-one\nmc91-res-93' refl = mc91-res-93\n\nmc91-res-92' : \u2200 {n} \u2192 n \u2261 ninety-two \u2192 mc91 n \u2261 ninety-one\nmc91-res-92' refl = mc91-res-92\n\nmc91-res-91' : \u2200 {n} \u2192 n \u2261 ninety-one \u2192 mc91 n \u2261 ninety-one\nmc91-res-91' refl = mc91-res-91\n\nmc91-res-90' : \u2200 {n} \u2192 n \u2261 ninety \u2192 mc91 n \u2261 ninety-one\nmc91-res-90' refl = mc91-res-90\n","old_contents":"------------------------------------------------------------------------------\n-- Auxiliary properties of the McCarthy 91 function\n------------------------------------------------------------------------------\n\nmodule FOTC.Program.McCarthy91.Properties.AuxiliaryATP where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.PropertiesATP\nopen import FOTC.Data.Nat.PropertiesATP\nopen import FOTC.Data.Nat.UnaryNumbers\nopen import FOTC.Data.Nat.UnaryNumbers.IsN-ATP\nopen import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP\n\nopen import FOTC.Program.McCarthy91.ArithmeticATP\nopen import FOTC.Program.McCarthy91.McCarthy91\n\n------------------------------------------------------------------------------\n\n--- Auxiliary properties\n\n---- Case n > 100\npostulate\n Nmc91>100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192 N (mc91 n)\n x100 : \u2200 {n} \u2192 N n \u2192 GT n one-hundred \u2192\n LT n (mc91 n + eleven)\n{-# ATP prove Nmc91>100 10-N \u2238-N #-}\n{-# ATP prove x100 +-N \u2238-N 10-N 11-N x100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810\n Nmc91>100 111-N 101-N\n#-}\n{-# ATP prove mc91-res-100 111>100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810 #-}\n{-# ATP prove mc91-res-100' mc91-res-100 #-}\n{-# ATP prove Nmc91\u2261100 111>100 101>100 101\u2261100+11-10 91\u2261[100+11\u223810]\u223810 91-N #-}\n{-# ATP prove mc91100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-109 98+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-108 97+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-107 96+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-106 95+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-105 94+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-104 93+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-103 92+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-102 91+11>100 x+11\u223810\u2261Sx #-}\n{-# ATP prove mc91-res-101 90+11>100 x+11\u223810\u2261Sx #-}\n\npostulate\n mc91-res-99 : mc91 ninety-nine \u2261 ninety-one\n mc91-res-98 : mc91 ninety-eight \u2261 ninety-one\n mc91-res-97 : mc91 ninety-seven \u2261 ninety-one\n mc91-res-96 : mc91 ninety-six \u2261 ninety-one\n mc91-res-95 : mc91 ninety-five \u2261 ninety-one\n mc91-res-94 : mc91 ninety-four \u2261 ninety-one\n mc91-res-93 : mc91 ninety-three \u2261 ninety-one\n mc91-res-92 : mc91 ninety-two \u2261 ninety-one\n mc91-res-91 : mc91 ninety-one \u2261 ninety-one\n mc91-res-90 : mc91 ninety \u2261 ninety-one\n{-# ATP prove mc91-res-99 mc91x-res\u2264100 mc91-res-110 mc91-res-100 #-}\n{-# ATP prove mc91-res-98 mc91x-res\u2264100 mc91-res-109 mc91-res-99 #-}\n{-# ATP prove mc91-res-97 mc91x-res\u2264100 mc91-res-108 mc91-res-98 #-}\n{-# ATP prove mc91-res-96 mc91x-res\u2264100 mc91-res-107 mc91-res-97 #-}\n{-# ATP prove mc91-res-95 mc91x-res\u2264100 mc91-res-106 mc91-res-96 #-}\n{-# ATP prove mc91-res-94 mc91x-res\u2264100 mc91-res-105 mc91-res-95 #-}\n{-# ATP prove mc91-res-93 mc91x-res\u2264100 mc91-res-104 mc91-res-94 #-}\n{-# ATP prove mc91-res-92 mc91x-res\u2264100 mc91-res-103 mc91-res-93 #-}\n{-# ATP prove mc91-res-91 mc91x-res\u2264100 mc91-res-102 mc91-res-92 #-}\n{-# ATP prove mc91-res-90 mc91x-res\u2264100 mc91-res-101 mc91-res-91 #-}\n\nmc91-res-99' : \u2200 {n} \u2192 n \u2261 ninety-nine \u2192 mc91 n \u2261 ninety-one\nmc91-res-99' refl = mc91-res-99\n\nmc91-res-98' : \u2200 {n} \u2192 n \u2261 ninety-eight \u2192 mc91 n \u2261 ninety-one\nmc91-res-98' refl = mc91-res-98\n\nmc91-res-97' : \u2200 {n} \u2192 n \u2261 ninety-seven \u2192 mc91 n \u2261 ninety-one\nmc91-res-97' refl = mc91-res-97\n\nmc91-res-96' : \u2200 {n} \u2192 n \u2261 ninety-six \u2192 mc91 n \u2261 ninety-one\nmc91-res-96' refl = mc91-res-96\n\nmc91-res-95' : \u2200 {n} \u2192 n \u2261 ninety-five \u2192 mc91 n \u2261 ninety-one\nmc91-res-95' refl = mc91-res-95\n\nmc91-res-94' : \u2200 {n} \u2192 n \u2261 ninety-four \u2192 mc91 n \u2261 ninety-one\nmc91-res-94' refl = mc91-res-94\n\nmc91-res-93' : \u2200 {n} \u2192 n \u2261 ninety-three \u2192 mc91 n \u2261 ninety-one\nmc91-res-93' refl = mc91-res-93\n\nmc91-res-92' : \u2200 {n} \u2192 n \u2261 ninety-two \u2192 mc91 n \u2261 ninety-one\nmc91-res-92' refl = mc91-res-92\n\nmc91-res-91' : \u2200 {n} \u2192 n \u2261 ninety-one \u2192 mc91 n \u2261 ninety-one\nmc91-res-91' refl = mc91-res-91\n\nmc91-res-90' : \u2200 {n} \u2192 n \u2261 ninety \u2192 mc91 n \u2261 ninety-one\nmc91-res-90' refl = mc91-res-90\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"3f147784e99d6ca24756127bd00a60a477993741","subject":"Desc stratified model: descDChoice implicit.","message":"Desc stratified model: descDChoice implicit.","repos":"kwangkim\/pigment,kwangkim\/pigment,brixen\/Epigram,kwangkim\/pigment","old_file":"models\/IDesc.agda","new_file":"models\/IDesc.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l : Level}{A B : Set l}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l : Level}{A B C : Set l}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {l : Level}{A B : Set l}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set l) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set l} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set l}(R : I -> IDesc I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set l}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : {l : Level} -> Set l -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const (lift I)\ndescDChoice _ lconst = const (Set _)\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice _ lsigma = sigma (Set _) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : (l : Level)(I : Set l) -> IDesc Unit\ndescD x I = sigma DescDConst (descDChoice I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (\\_ -> descD x I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst {l = suc x}\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst {l = suc x}\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst {l = suc x}\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst {l = suc x}\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst {l = suc x}\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n \n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\n-- desc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\n-- descD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\n-- IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l\n\n-- cases : (l : Level)\n-- (I : Set l)\n-- (xs : desc (suc l) (lift I) (descD l I) (IMu (suc l) Unit (\\ _ -> descD l I)))\n-- (hs : desc l I (box l I (descD l I) (IMu l Unit (\u03bb _ -> descD l I)) xs) (\u03bb _ -> IDesc (suc l) I)) ->\n-- IDesc (suc l) I\n-- cases x I ( lvar , i ) hs = var i\n-- cases x I ( lconst , X ) hs = const X\n-- cases x I ( lprod , (D , D') ) ( d , d' ) = prod d d'\n-- cases x I ( lpi , ( S , T ) ) hs = pi S hs\n-- cases x I ( lsigma , ( S , T ) ) hs = sigma S hs\n\n-- phi : {I : Set} -> IDescl I -> IDesc I\n-- phi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n","old_contents":"{-# OPTIONS --universe-polymorphism #-}\n\nmodule IDesc where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Universe polymorphism\n--****************\n\ndata Level : Set where\n zero : Level\n suc : Level -> Level\n\n{-# BUILTIN LEVEL Level #-}\n{-# BUILTIN LEVELZERO zero #-}\n{-# BUILTIN LEVELSUC suc #-}\n\nmax : Level -> Level -> Level\nmax zero m = m\nmax (suc n) zero = suc n\nmax (suc n) (suc m) = suc (max n m)\n\n{-# BUILTIN LEVELMAX max #-}\n\ndata Lifted {l : Level} (A : Set l) : Set (suc l) where\n lifter : A \u2192 Lifted A\n\nlift : {i : Level} -> Set i -> Set (suc i) \nlift x = Lifted x\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma {i j : Level}(A : Set i) (B : A -> Set j) : Set (max i j) where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : {i j : Level}(A : Set i)(B : Set j) -> Set (max i j)\nA * B = Sigma A \\_ -> B\n\nfst : {i j : Level}{A : Set i}{B : A -> Set j} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {i j : Level}{A : Set i}{B : A -> Set j} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero {i : Level} : Set i where\ndata Unit {i : Level} : Set i where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ {i j : Level}(A : Set i)(B : Set j) : Set (max i j) where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {l : Level}{A : Set l}(x : A) : A -> Set l where\n refl : x == x\n\ncong : {l : Level}{A B : Set l}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {l : Level}{A B C : Set l}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {l : Level}{A B : Set l}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc {l : Level}(I : Set l) : Set (suc l) where\n var : I -> IDesc I\n const : Set l -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set l) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set l) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {l : Level}{I : Set l} -> IDesc I -> (I -> Set l) -> Set l\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {l : Level}{I : Set l}(R : I -> IDesc I)(i : I) : Set l where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {l : Level}{I : Set l}(D : IDesc I)(P : I -> Set l) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {l : Level}\n {I : Set l}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set l)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst {l : Level} : Set l where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : (l : Level) -> Set l -> DescDConst -> IDesc Unit\ndescDChoice _ I lvar = const (lift I)\ndescDChoice x _ lconst = const (Set x)\ndescDChoice _ _ lprod = prod (var Void) (var Void)\ndescDChoice x _ lpi = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\ndescDChoice x _ lsigma = sigma (Set x) (\\S -> pi (lift S) (\\s -> var Void))\n\ndescD : (l : Level)(I : Set l) -> IDesc Unit\ndescD x I = sigma DescDConst (descDChoice x I)\n\nIDescl : (l : Level)(I : Set l) -> Set (suc l)\nIDescl x I = IMu (\\_ -> descD x I) Void\n\nvarl : (l : Level)(I : Set l)(i : I) -> IDescl l I\nvarl x I i = con (lv , lifter i) \n where lv : DescDConst {l = suc x}\n lv = lvar\n\nconstl : (l : Level)(I : Set l)(X : Set l) -> IDescl l I\nconstl x I X = con (lc , X)\n where lc : DescDConst {l = suc x}\n lc = lconst\n\nprodl : (l : Level)(I : Set l)(D D' : IDescl l I) -> IDescl l I\nprodl x I D D' = con (lp , (D , D'))\n where lp : DescDConst {l = suc x}\n lp = lprod\n\n\npil : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\npil x I S T = con (lp , ( S , Tl))\n where lp : DescDConst {l = suc x}\n lp = lpi\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n\nsigmal : (l : Level)(I : Set l)(S : Set l)(T : S -> IDescl l I) -> IDescl l I\nsigmal x I S T = con (ls , ( S , Tl))\n where ls : DescDConst {l = suc x}\n ls = lsigma\n Tl : Lifted S -> IDescl x I\n Tl (lifter s) = T s\n \n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\n-- desc : (l : Level)(I : Set l) -> IDesc l I -> (I -> Set l) -> Set l\n-- descD : (l : Level)(I : Set l) -> IDesc (suc l) Unit\n-- IMu (l : Level)(I : Set l)(R : I -> IDesc l I)(i : I) : Set l\n\n-- cases : (l : Level)\n-- (I : Set l)\n-- (xs : desc (suc l) (lift I) (descD l I) (IMu (suc l) Unit (\\ _ -> descD l I)))\n-- (hs : desc l I (box l I (descD l I) (IMu l Unit (\u03bb _ -> descD l I)) xs) (\u03bb _ -> IDesc (suc l) I)) ->\n-- IDesc (suc l) I\n-- cases x I ( lvar , i ) hs = var i\n-- cases x I ( lconst , X ) hs = const X\n-- cases x I ( lprod , (D , D') ) ( d , d' ) = prod d d'\n-- cases x I ( lpi , ( S , T ) ) hs = pi S hs\n-- cases x I ( lsigma , ( S , T ) ) hs = sigma S hs\n\n-- phi : {I : Set} -> IDescl I -> IDesc I\n-- phi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"79eb1bff6fe6f19057332ff8737b10e85457ff5f","subject":"Drop dead comment","message":"Drop dead comment\n","repos":"inc-lc\/ilc-agda","old_file":"Parametric\/Denotation\/CachingEvaluation.agda","new_file":"Parametric\/Denotation\/CachingEvaluation.agda","new_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Caching evaluation\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\n\nimport Parametric.Syntax.MType as MType\nimport Parametric.Syntax.MTerm as MTerm\n\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Denotation.MValue as MValue\nimport Parametric.Denotation.CachingMValue as CachingMValue\nimport Parametric.Denotation.MEvaluation as MEvaluation\n\nmodule Parametric.Denotation.CachingEvaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (ValConst : MTerm.ValConstStructure Const)\n (CompConst : MTerm.CompConstStructure Const)\n -- I should really switch to records - can it get sillier than this?\n (\u27e6_\u27e7ValBase : MEvaluation.ValStructure Const \u27e6_\u27e7Base ValConst CompConst)\n (\u27e6_\u27e7CompBase : MEvaluation.CompStructure Const \u27e6_\u27e7Base ValConst CompConst)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\n\nopen MType.Structure Base\nopen MTerm.Structure Const ValConst CompConst\n\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen MValue.Structure Base \u27e6_\u27e7Base\nopen CachingMValue.Structure Base \u27e6_\u27e7Base\nopen MEvaluation.Structure Const \u27e6_\u27e7Base ValConst CompConst \u27e6_\u27e7ValBase \u27e6_\u27e7CompBase\n\nopen import Base.Denotation.Notation\n\n-- Extension Point: Evaluation of fully applied constants.\nValStructure : Set\nValStructure = \u2200 {\u03a3 \u03c4} \u2192 ValConst \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n\nCompStructure : Set\nCompStructure = \u2200 {\u03a3 \u03c4} \u2192 CompConst \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n\nmodule Structure\n (\u27e6_\u27e7ValBaseTermCache : ValStructure)\n (\u27e6_\u27e7CompBaseTermCache : CompStructure)\n where\n\n {-\n -- Prototype here the type-correctness of a simple non-standard semantics.\n -- This describes a simplified version of the transformation by Liu and\n -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language\n -- terms, we produce host language terms to take advantage of the richer type\n -- system of the host language (in particular, here we need the unit type,\n -- product types and *existentials*).\n --\n -- As usual, we'll later switch to a real term transformation.\n -}\n open import Data.Product hiding (map)\n open import Data.Unit\n\n -- Defining a caching semantics for Term proves to be hard, requiring to\n -- insert and remove caches where we apply constants.\n\n -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.\n\n -- The solution is to distinguish among different kinds of constants. Some are\n -- value constructors (and thus do not return caches), while others are\n -- computation constructors (and thus should return caches). For products, I\n -- believe we will only use the products which are values, not computations\n -- (XXX check CBPV paper for the name).\n \u27e6_\u27e7CompTermCache : \u2200 {\u03c4 \u0393} \u2192 Comp \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n \u27e6_\u27e7ValTermCache : \u2200 {\u03c4 \u0393} \u2192 Val \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n \u27e6_\u27e7ValsTermCache : \u2200 {\u0393 \u03a3} \u2192 Vals \u0393 \u03a3 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache\n\n open import Base.Denotation.Environment ValType \u27e6_\u27e7ValTypeHidCache public\n using ()\n renaming (\u27e6_\u27e7Var to \u27e6_\u27e7ValVarHidCache)\n\n -- This says that the environment does not contain caches... sounds wrong!\n -- Either we add extra variables for the caches, or we store computations in\n -- the environment (but that does not make sense), or we store caches in\n -- values, by acting not on F but on something else (U?).\n\n -- Copy of \u27e6_\u27e7Vals\n \u27e6 \u2205 \u27e7ValsTermCache \u03c1 = \u2205\n \u27e6 vt \u2022 valtms \u27e7ValsTermCache \u03c1 = \u27e6 vt \u27e7ValTermCache \u03c1 \u2022 \u27e6 valtms \u27e7ValsTermCache \u03c1\n\n -- I suspect the plan was to use extra variables; that's annoying to model in\n -- Agda but easier in implementations.\n\n \u27e6 vVar x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7ValVarHidCache \u03c1\n \u27e6 vThunk x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7CompTermCache \u03c1\n -- No caching, because the arguments are values, so evaluating them does not\n -- produce intermediate results.\n \u27e6 vConst c args \u27e7ValTermCache \u03c1 = \u27e6 c \u27e7ValBaseTermCache (\u27e6 args \u27e7ValsTermCache \u03c1)\n\n -- The only caching is done by the interpretation of the constant (because the\n -- arguments are values so need no caching).\n \u27e6_\u27e7CompTermCache (cConst c args) \u03c1 = \u27e6 c \u27e7CompBaseTermCache (\u27e6 args \u27e7ValsTermCache \u03c1)\n\n -- Also, where are introduction forms for pairs and sums among values? With\n -- them, we should see that we can interpret them without adding a cache.\n\n -- Thunks keep seeming noops.\n \u27e6_\u27e7CompTermCache (cForce x) \u03c1 = \u27e6 x \u27e7ValTermCache \u03c1\n\n -- The effect of F is a writer monad of cached values, where the monoid is\n -- (isomorphic to) the free monoid over (\u2203 \u03c4 . \u03c4), but we push the\n -- existentials up when pairing things!\n\n -- That's what we're interpreting computations in. XXX maybe consider using\n -- monads directly. But that doesn't deal with arity.\n \u27e6_\u27e7CompTermCache (cReturn v) \u03c1 = vUnit , (\u27e6 v \u27e7ValTermCache \u03c1 , tt)\n\n -- For this to be enough, a lambda needs to return a produce, not to forward\n -- the underlying one (unless there are no intermediate results). The correct\n -- requirements can maybe be enforced through a linear typing discipline.\n\n {-\n -- Here we'd have a problem with the original into from CBPV, because it does\n -- not require converting expressions to the \"CBPV A-normal form\".\n\n \u27e6_\u27e7CompTermCache (v\u2081 into v\u2082) \u03c1 =\n -- Sequence commands and combine their caches.\n {-\n let (_ , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (_ , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in , (r\u2082 , (c\u2081 ,\u2032 c\u2082))\n -}\n\n -- The code above does not work, because we only guarantee that v\u2082 is\n -- a computation, not that it's an F-computation - v\u2082 could also be function\n -- type or a computation product.\n\n -- We need a restricted CBPV, where these two possibilities are forbidden. If\n -- you want to save something between different lambdas, you need to add an F\n -- U to reflect that. (Double-check the papers which show how to encode arity\n -- using CBPV, it seems that they should find the same problem --- XXX they\n -- don't).\n\n -- Instead, just drop the first cache (XXX WRONG).\n \u27e6 v\u2082 \u27e7CompTermCache (proj\u2081 (proj\u2082 (\u27e6 v\u2081 \u27e7CompTermCache \u03c1)) \u2022 \u03c1)\n -}\n\n -- But if we alter _into_ as described above, composing the caches works!\n -- However, we should not forget we also need to save the new intermediate\n -- result, that is the one produced by the first part of the let.\n \u27e6_\u27e7CompTermCache (_into_ {\u03c3} {\u03c4} v\u2081 v\u2082) \u03c1 =\n let (\u03c4\u2081 , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (\u03c4\u2082 , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in (\u03c3 v\u00d7 \u03c4\u2081 v\u00d7 \u03c4\u2082) , (r\u2082 ,\u2032 ( r\u2081 ,\u2032 c\u2081 ,\u2032 c\u2082))\n\n -- Note the compositionality and luck: we don't need to do anything at the\n -- cReturn time, we just need the nested into to do their job, because as I\n -- said intermediate results are a writer monad.\n --\n -- Q: But then, do we still need to do all the other stuff? IOW, do we still\n -- need to forbid (\u03bb x . y <- f args; g args') and replace it with (\u03bb x . y <-\n -- f args; z <- g args'; z)?\n --\n -- A: One thing we still need is using the monadic version of into for the\n -- same reasons - which makes sense, since it has the type of monadic bind.\n --\n -- Maybe not: if we use a monad, which respects left and right identity, the\n -- two above forms are equivalent. But what about associativity? We don't have\n -- associativity with nested tuples in the middle. That's why the monad uses\n -- lists! We can also use nested tuple, as long as in the into case we don't\n -- do (a, b) but append a b (ahem, where?), which decomposes the first list\n -- and prepends it to the second. To this end, we need to know the type of the\n -- first element, or to ensure it's always a pair. Maybe we just want to reuse\n -- an HList.\n\n -- In abstractions, we should start collecting all variables...\n\n -- Here, unlike in \u27e6_\u27e7TermCache, we don't need to invent an empty cache,\n -- that's moved into the handling of cReturn. This makes *the* difference for\n -- nested lambdas, where we don't need to create caches multiple times!\n\n \u27e6_\u27e7CompTermCache (cAbs v) \u03c1 = \u03bb x \u2192 \u27e6 v \u27e7CompTermCache (x \u2022 \u03c1)\n\n -- Here we see that we are in a sort of A-normal form, because the argument is\n -- a value (not quite ANF though, since values can be thunks - that is,\n -- computations which haven't been run yet, I guess. Do we have an use for\n -- that? That allows passing lambdas as arguments directly - which is fine,\n -- because producing a closure indeed does not have intermediate results!).\n \u27e6_\u27e7CompTermCache (cApp t v) \u03c1 = \u27e6 t \u27e7CompTermCache \u03c1 (\u27e6 v \u27e7ValTermCache \u03c1)\n\n \u27e6_\u27e7TermCacheCBV : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 fromCBVCtx \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 cbvToCompType \u03c4 \u27e7CompTypeHidCache\n \u27e6 t \u27e7TermCacheCBV = \u27e6 fromCBV t \u27e7CompTermCache\n","old_contents":"------------------------------------------------------------------------\n-- INCREMENTAL \u03bb-CALCULUS\n--\n-- Caching evaluation\n------------------------------------------------------------------------\n\nimport Parametric.Syntax.Type as Type\nimport Parametric.Syntax.Term as Term\n\nimport Parametric.Syntax.MType as MType\nimport Parametric.Syntax.MTerm as MTerm\n\nimport Parametric.Denotation.Value as Value\nimport Parametric.Denotation.Evaluation as Evaluation\nimport Parametric.Denotation.MValue as MValue\nimport Parametric.Denotation.CachingMValue as CachingMValue\nimport Parametric.Denotation.MEvaluation as MEvaluation\n\nmodule Parametric.Denotation.CachingEvaluation\n {Base : Type.Structure}\n (Const : Term.Structure Base)\n (\u27e6_\u27e7Base : Value.Structure Base)\n (\u27e6_\u27e7Const : Evaluation.Structure Const \u27e6_\u27e7Base)\n (ValConst : MTerm.ValConstStructure Const)\n (CompConst : MTerm.CompConstStructure Const)\n -- I should really switch to records - can it get sillier than this?\n (\u27e6_\u27e7ValBase : MEvaluation.ValStructure Const \u27e6_\u27e7Base ValConst CompConst)\n (\u27e6_\u27e7CompBase : MEvaluation.CompStructure Const \u27e6_\u27e7Base ValConst CompConst)\n where\n\nopen Type.Structure Base\nopen Term.Structure Base Const\n\nopen MType.Structure Base\nopen MTerm.Structure Const ValConst CompConst\n\nopen Value.Structure Base \u27e6_\u27e7Base\nopen Evaluation.Structure Const \u27e6_\u27e7Base \u27e6_\u27e7Const\nopen MValue.Structure Base \u27e6_\u27e7Base\nopen CachingMValue.Structure Base \u27e6_\u27e7Base\nopen MEvaluation.Structure Const \u27e6_\u27e7Base ValConst CompConst \u27e6_\u27e7ValBase \u27e6_\u27e7CompBase\n\nopen import Base.Denotation.Notation\n\n-- Extension Point: Evaluation of fully applied constants.\nValStructure : Set\nValStructure = \u2200 {\u03a3 \u03c4} \u2192 ValConst \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n\nCompStructure : Set\nCompStructure = \u2200 {\u03a3 \u03c4} \u2192 CompConst \u03a3 \u03c4 \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n\nmodule Structure\n (\u27e6_\u27e7ValBaseTermCache : ValStructure)\n (\u27e6_\u27e7CompBaseTermCache : CompStructure)\n where\n\n {-\n -- Prototype here the type-correctness of a simple non-standard semantics.\n -- This describes a simplified version of the transformation by Liu and\n -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language\n -- terms, we produce host language terms to take advantage of the richer type\n -- system of the host language (in particular, here we need the unit type,\n -- product types and *existentials*).\n --\n -- As usual, we'll later switch to a real term transformation.\n -}\n open import Data.Product hiding (map)\n open import Data.Unit\n\n -- Defining a caching semantics for Term proves to be hard, requiring to\n -- insert and remove caches where we apply constants.\n\n -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.\n\n -- The solution is to distinguish among different kinds of constants. Some are\n -- value constructors (and thus do not return caches), while others are\n -- computation constructors (and thus should return caches). For products, I\n -- believe we will only use the products which are values, not computations\n -- (XXX check CBPV paper for the name).\n \u27e6_\u27e7CompTermCache : \u2200 {\u03c4 \u0393} \u2192 Comp \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7CompTypeHidCache\n \u27e6_\u27e7ValTermCache : \u2200 {\u03c4 \u0393} \u2192 Val \u0393 \u03c4 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03c4 \u27e7ValTypeHidCache\n \u27e6_\u27e7ValsTermCache : \u2200 {\u0393 \u03a3} \u2192 Vals \u0393 \u03a3 \u2192 \u27e6 \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 \u03a3 \u27e7ValCtxHidCache\n\n open import Base.Denotation.Environment ValType \u27e6_\u27e7ValTypeHidCache public\n using ()\n renaming (\u27e6_\u27e7Var to \u27e6_\u27e7ValVarHidCache)\n\n -- This says that the environment does not contain caches... sounds wrong!\n -- Either we add extra variables for the caches, or we store computations in\n -- the environment (but that does not make sense), or we store caches in\n -- values, by acting not on F but on something else (U?).\n\n -- Copy of \u27e6_\u27e7Vals\n \u27e6 \u2205 \u27e7ValsTermCache \u03c1 = \u2205\n \u27e6 vt \u2022 valtms \u27e7ValsTermCache \u03c1 = \u27e6 vt \u27e7ValTermCache \u03c1 \u2022 \u27e6 valtms \u27e7ValsTermCache \u03c1\n\n -- I suspect the plan was to use extra variables; that's annoying to model in\n -- Agda but easier in implementations.\n\n \u27e6 vVar x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7ValVarHidCache \u03c1\n \u27e6 vThunk x \u27e7ValTermCache \u03c1 = \u27e6 x \u27e7CompTermCache \u03c1\n -- No caching, because the arguments are values, so evaluating them does not\n -- produce intermediate results.\n \u27e6 vConst c args \u27e7ValTermCache \u03c1 = \u27e6 c \u27e7ValBaseTermCache (\u27e6 args \u27e7ValsTermCache \u03c1)\n\n -- The only caching is done by the interpretation of the constant (because the\n -- arguments are values so need no caching).\n \u27e6_\u27e7CompTermCache (cConst c args) \u03c1 = \u27e6 c \u27e7CompBaseTermCache (\u27e6 args \u27e7ValsTermCache \u03c1)\n\n -- Also, where are introduction forms for pairs and sums among values? With\n -- them, we should see that we can interpret them without adding a cache.\n\n -- Thunks keep seeming noops.\n \u27e6_\u27e7CompTermCache (cForce x) \u03c1 = \u27e6 x \u27e7ValTermCache \u03c1\n\n -- Here, in an actual implementation, we would return the actual cache with\n -- all variables.\n --\n -- The effect of F is a writer monad of cached values, where the monoid is\n -- (isomorphic to) the free monoid over (\u2203 \u03c4 . \u03c4), but we push the\n -- existentials up when pairing things!\n\n -- That's what we're interpreting computations in. XXX maybe consider using\n -- monads directly. But that doesn't deal with arity.\n \u27e6_\u27e7CompTermCache (cReturn v) \u03c1 = vUnit , (\u27e6 v \u27e7ValTermCache \u03c1 , tt)\n\n -- For this to be enough, a lambda needs to return a produce, not to forward\n -- the underlying one (unless there are no intermediate results). The correct\n -- requirements can maybe be enforced through a linear typing discipline.\n\n {-\n -- Here we'd have a problem with the original into from CBPV, because it does\n -- not require converting expressions to the \"CBPV A-normal form\".\n\n \u27e6_\u27e7CompTermCache (v\u2081 into v\u2082) \u03c1 =\n -- Sequence commands and combine their caches.\n {-\n let (_ , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (_ , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in , (r\u2082 , (c\u2081 ,\u2032 c\u2082))\n -}\n\n -- The code above does not work, because we only guarantee that v\u2082 is\n -- a computation, not that it's an F-computation - v\u2082 could also be function\n -- type or a computation product.\n\n -- We need a restricted CBPV, where these two possibilities are forbidden. If\n -- you want to save something between different lambdas, you need to add an F\n -- U to reflect that. (Double-check the papers which show how to encode arity\n -- using CBPV, it seems that they should find the same problem --- XXX they\n -- don't).\n\n -- Instead, just drop the first cache (XXX WRONG).\n \u27e6 v\u2082 \u27e7CompTermCache (proj\u2081 (proj\u2082 (\u27e6 v\u2081 \u27e7CompTermCache \u03c1)) \u2022 \u03c1)\n -}\n\n -- But if we alter _into_ as described above, composing the caches works!\n -- However, we should not forget we also need to save the new intermediate\n -- result, that is the one produced by the first part of the let.\n \u27e6_\u27e7CompTermCache (_into_ {\u03c3} {\u03c4} v\u2081 v\u2082) \u03c1 =\n let (\u03c4\u2081 , (r\u2081 , c\u2081)) = \u27e6 v\u2081 \u27e7CompTermCache \u03c1\n (\u03c4\u2082 , (r\u2082 , c\u2082)) = \u27e6 v\u2082 \u27e7CompTermCache (r\u2081 \u2022 \u03c1)\n in (\u03c3 v\u00d7 \u03c4\u2081 v\u00d7 \u03c4\u2082) , (r\u2082 ,\u2032 ( r\u2081 ,\u2032 c\u2081 ,\u2032 c\u2082))\n\n -- Note the compositionality and luck: we don't need to do anything at the\n -- cReturn time, we just need the nested into to do their job, because as I\n -- said intermediate results are a writer monad.\n --\n -- Q: But then, do we still need to do all the other stuff? IOW, do we still\n -- need to forbid (\u03bb x . y <- f args; g args') and replace it with (\u03bb x . y <-\n -- f args; z <- g args'; z)?\n --\n -- A: One thing we still need is using the monadic version of into for the\n -- same reasons - which makes sense, since it has the type of monadic bind.\n --\n -- Maybe not: if we use a monad, which respects left and right identity, the\n -- two above forms are equivalent. But what about associativity? We don't have\n -- associativity with nested tuples in the middle. That's why the monad uses\n -- lists! We can also use nested tuple, as long as in the into case we don't\n -- do (a, b) but append a b (ahem, where?), which decomposes the first list\n -- and prepends it to the second. To this end, we need to know the type of the\n -- first element, or to ensure it's always a pair. Maybe we just want to reuse\n -- an HList.\n\n -- In abstractions, we should start collecting all variables...\n\n -- Here, unlike in \u27e6_\u27e7TermCache, we don't need to invent an empty cache,\n -- that's moved into the handling of cReturn. This makes *the* difference for\n -- nested lambdas, where we don't need to create caches multiple times!\n\n \u27e6_\u27e7CompTermCache (cAbs v) \u03c1 = \u03bb x \u2192 \u27e6 v \u27e7CompTermCache (x \u2022 \u03c1)\n\n -- Here we see that we are in a sort of A-normal form, because the argument is\n -- a value (not quite ANF though, since values can be thunks - that is,\n -- computations which haven't been run yet, I guess. Do we have an use for\n -- that? That allows passing lambdas as arguments directly - which is fine,\n -- because producing a closure indeed does not have intermediate results!).\n \u27e6_\u27e7CompTermCache (cApp t v) \u03c1 = \u27e6 t \u27e7CompTermCache \u03c1 (\u27e6 v \u27e7ValTermCache \u03c1)\n\n \u27e6_\u27e7TermCacheCBV : \u2200 {\u03c4 \u0393} \u2192 Term \u0393 \u03c4 \u2192 \u27e6 fromCBVCtx \u0393 \u27e7ValCtxHidCache \u2192 \u27e6 cbvToCompType \u03c4 \u27e7CompTypeHidCache\n \u27e6 t \u27e7TermCacheCBV = \u27e6 fromCBV t \u27e7CompTermCache\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"70904b0a4e8a3b7e691d0d859a518ee5341f7a93","subject":"Cleaning.","message":"Cleaning.\n\nIgnore-this: a0d11238690f72ea668e358ccc188abd\n\ndarcs-hash:20110405150812-3bd4e-afdff1a0cdbd4dc4b3edfee982506c8248779ca5.gz\n","repos":"asr\/fotc,asr\/fotc","old_file":"Draft\/FOTC\/Program\/Nest\/DomainPredicate.agda","new_file":"Draft\/FOTC\/Program\/Nest\/DomainPredicate.agda","new_contents":"------------------------------------------------------------------------------\n-- FOTC version of the domain predicate of a nested recursive function\n-- given by the Bove-Capretta method\n------------------------------------------------------------------------------\n\n-- From: Ana Bove and Venanzio Capretta. Nested general recursion and\n-- partiality in type theory. Vol. 2152 of LNCS. 2001\n\nmodule Draft.FOTC.Program.Nest.DomainPredicate where\n\nopen import FOTC.Base\n\nopen import FOTC.Data.Nat\nopen import FOTC.Data.Nat.Induction.Acc.WellFoundedInductionI\nopen import FOTC.Data.Nat.Inequalities\nopen import FOTC.Data.Nat.Inequalities.PropertiesI\n\nopen import FOTC.Relation.Binary.EqReasoning\n\n------------------------------------------------------------------------------\n\npostulate\n nest : D \u2192 D\n nest-0 : nest zero \u2261 zero\n nest-S : \u2200 d \u2192 nest (succ d) \u2261 nest (nest d)\n\ndata Dom : D \u2192 Set where\n dom0 : Dom zero\n domS : \u2200 d \u2192 Dom d \u2192 Dom (nest d) \u2192 Dom (succ d)\n\n-- The domain predicate is total.\ndom-N : \u2200 d \u2192 Dom d \u2192 N d\ndom-N .zero dom0 = zN\ndom-N .(succ d) (domS d h\u2081 h\u2082) = sN (dom-N d h\u2081)\n\nnest-x\u22610 : \u2200 {n} \u2192 N n \u2192 nest n \u2261 zero\nnest-x\u22610 zN = nest-0\nnest-x\u22610 (sN {n} Nn) =\n begin\n nest (succ n)\n \u2261\u27e8 nest-S n \u27e9\n nest (nest n)\n \u2261\u27e8 subst (\u03bb t \u2192 nest (nest n) \u2261 nest t)\n (nest-x\u22610 Nn) -- IH.\n refl\n \u27e9\n nest zero\n \u2261\u27e8 nest-0 \u27e9\n zero\n \u220e\n\n-- The nest function is total in its domain (via structural recursion\n-- in the domain predicate).\nnest-DN : \u2200 {d} \u2192 Dom d \u2192 N (nest d)\nnest-DN dom0 = subst N (sym nest-0) zN\nnest-DN (domS d h\u2081 h\u2082) = subst N (sym (nest-S d)) (nest-DN h\u2082)\n\n-- The nest function is total.\nnest-N : \u2200 {n} \u2192 N n \u2192 N (nest n)\nnest-N Nn = subst N (sym (nest-x\u22610 Nn)) zN\n\nnest-\u2264 : \u2200 {n} \u2192 Dom n \u2192 LE (nest n) n\nnest-\u2264 dom0 =\n begin\n nest zero \u2264 zero\n \u2261\u27e8 subst (\u03bb t \u2192 nest zero \u2264 zero \u2261 t \u2264 zero)\n nest-0\n refl\n \u27e9\n zero \u2264 zero\n \u2261\u27e8 x\u2264x zN \u27e9\n true\n \u220e\n\nnest-\u2264 (domS n h\u2081 h\u2082) =\n \u2264-trans (nest-N (sN (dom-N n h\u2081))) (nest-N (dom-N n h\u2081)) (sN Nn) prf\u2081 prf\u2082\n where\n Nn : N n\n Nn = dom-N n h\u2081\n\n prf\u2081 : LE (nest (succ n)) (nest n)\n prf\u2081 =\n begin\n nest (succ n) \u2264 nest n\n \u2261\u27e8 subst (\u03bb t \u2192 nest (succ n) \u2264 nest n \u2261 t \u2264 nest n)\n (nest-S n)\n refl\n \u27e9\n nest (nest n) \u2264 nest n\n \u2261\u27e8 nest-\u2264 h\u2082 \u27e9\n true\n \u220e\n\n prf\u2082 : LE (nest n) (succ n)\n prf\u2082 = \u2264-trans (nest-N (dom-N n h\u2081)) Nn (sN Nn) (nest-\u2264 h\u2081) (x\u2264Sx Nn)\n\nN\u2192Dom : \u2200 {n} \u2192 N n \u2192 Dom n\nN\u2192Dom = wfInd-LT P ih\n where\n P : D \u2192 Set\n P = Dom\n\n ih : \u2200 {x} \u2192 N x \u2192 (\u2200 {y} \u2192 N y \u2192 LT y x \u2192 P y) \u2192 P x\n ih zN h = dom0\n ih (sN {x} Nx) h =\n domS x dn-x (h (nest-N Nx ) (x\u2264y\u2192x d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n pres-lem3 TAConst ()\n pres-lem3 (TAVar x\u2081) ()\n pres-lem3 (TALam ta) ()\n pres-lem3 (TAAp (TALam ta) ta\u2081) ITLam = lem-subst ta ta\u2081\n pres-lem3 (TAAp (TACast ta TCRefl) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 TCRefl)) TCRefl\n pres-lem3 (TAAp (TACast ta (TCArr x x\u2081)) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 (~sym x))) x\u2081\n pres-lem3 (TAEHole x x\u2081) ()\n pres-lem3 (TANEHole x ta x\u2081) ()\n pres-lem3 (TACast ta x) (ITCastID) = ta\n pres-lem3 (TACast (TACast ta x) x\u2081) (ITCastSucceed x\u2082) = ta\n pres-lem3 (TACast ta x) (ITGround (MGArr x\u2081)) = TACast (TACast ta (TCArr TCHole1 TCHole1)) TCHole1\n pres-lem3 (TACast ta TCHole2) (ITExpand (MGArr x\u2081)) = TACast (TACast ta TCHole2) (TCArr TCHole2 TCHole2)\n pres-lem3 (TACast (TACast ta x) x\u2081) (ITCastFail w y z) = TAFailedCast ta w y z\n pres-lem3 (TAFailedCast x y z q) ()\n\n preservation : {\u0394 : hctx} {d d' : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d \u21a6 d' \u2192\n \u0394 , \u2205 \u22a2 d' :: \u03c4\n preservation D (Step x x\u2081 x\u2082)\n with pres-lem2 D x\n ... | (_ , wt) = pres-lem x D wt (pres-lem3 wt x\u2081) x\u2082\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nopen import lemmas-consistency\nopen import type-assignment-unicity\n\nmodule preservation where\n -- todo: rename\n pres-lem : \u2200{ \u0394 \u0393 d \u03b5 d1 d2 d3 \u03c4 \u03c41 } \u2192\n d == \u03b5 \u27e6 d1 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n \u0394 , \u0393 \u22a2 d1 :: \u03c41 \u2192\n \u0394 , \u0393 \u22a2 d2 :: \u03c41 \u2192\n d3 == \u03b5 \u27e6 d2 \u27e7 \u2192\n \u0394 , \u0393 \u22a2 d3 :: \u03c4\n pres-lem FHOuter D1 D2 D3 FHOuter\n with type-assignment-unicity D1 D2\n ... | refl = D3\n pres-lem (FHAp1 eps) D1 D2 D3 (FHAp1 D4) = TAAp {!!} {!!}\n pres-lem (FHAp2 eps) D1 D2 D3 (FHAp2 D4) = TAAp {!!} {!!}\n pres-lem (FHNEHole eps) D1 D2 D3 (FHNEHole D4) = TANEHole {!!} {!!} {!!}\n pres-lem (FHCast eps) D1 D2 D3 (FHCast D4) = {!!}\n pres-lem (FHFailedCast eps) D1 D2 D3 (FHFailedCast D4) = {!!}\n\n -- todo: rename\n pres-lem2 : \u2200{ \u03b5 \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d == \u03b5 \u27e6 d' \u27e7 \u2192\n \u03a3[ \u03c4' \u2208 htyp ] (\u0394 , \u0393 \u22a2 d' :: \u03c4')\n pres-lem2 TAConst FHOuter = _ , TAConst\n pres-lem2 (TAVar x\u2081) FHOuter = _ , TAVar x\u2081\n pres-lem2 (TALam ta) FHOuter = _ , TALam ta\n\n pres-lem2 (TAAp ta ta\u2081) FHOuter = _ , TAAp ta ta\u2081\n pres-lem2 (TAAp ta ta\u2081) (FHAp1 eps) = pres-lem2 ta eps\n pres-lem2 (TAAp ta ta\u2081) (FHAp2 eps) = pres-lem2 ta\u2081 eps\n\n pres-lem2 (TAEHole x x\u2081) FHOuter = _ , TAEHole x x\u2081\n pres-lem2 (TANEHole x ta x\u2081) FHOuter = _ , TANEHole x ta x\u2081\n pres-lem2 (TANEHole x ta x\u2081) (FHNEHole eps) = pres-lem2 ta eps\n pres-lem2 (TACast ta x) FHOuter = _ , TACast ta x\n pres-lem2 (TACast ta x) (FHCast eps) = pres-lem2 ta eps\n pres-lem2 (TAFailedCast x y z w) FHOuter = _ , TAFailedCast x y z w\n pres-lem2 (TAFailedCast x y z w) (FHFailedCast FHOuter) = _ , TACast x TCHole1\n pres-lem2 (TAFailedCast x y z w) (FHFailedCast (FHCast eps)) = pres-lem2 x eps\n\n -- todo: rename\n pres-lem3 : \u2200{ \u0394 \u0393 d \u03c4 d' } \u2192\n \u0394 , \u0393 \u22a2 d :: \u03c4 \u2192\n d \u2192> d' \u2192\n \u0394 , \u0393 \u22a2 d' :: \u03c4\n pres-lem3 TAConst ()\n pres-lem3 (TAVar x\u2081) ()\n pres-lem3 (TALam ta) ()\n pres-lem3 (TAAp (TALam ta) ta\u2081) ITLam = {!!} -- todo: this is a lemma\n pres-lem3 (TAAp (TACast ta TCRefl) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 TCRefl)) TCRefl\n pres-lem3 (TAAp (TACast ta (TCArr x x\u2081)) ta\u2081) ITApCast = TACast (TAAp ta (TACast ta\u2081 (~sym x))) x\u2081\n pres-lem3 (TAEHole x x\u2081) ()\n pres-lem3 (TANEHole x ta x\u2081) ()\n pres-lem3 (TACast ta x) (ITCastID) = ta\n pres-lem3 (TACast (TACast ta x) x\u2081) (ITCastSucceed x\u2082) = ta\n pres-lem3 (TACast ta x) (ITGround (MGArr x\u2081)) = TACast (TACast ta (TCArr TCHole1 TCHole1)) TCHole1\n pres-lem3 (TACast ta TCHole2) (ITExpand (MGArr x\u2081)) = TACast (TACast ta TCHole2) (TCArr TCHole2 TCHole2)\n pres-lem3 (TACast (TACast ta x) x\u2081) (ITCastFail w y z) = TAFailedCast ta w y z\n pres-lem3 (TAFailedCast x y z q) ()\n\n preservation : {\u0394 : hctx} {d d' : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d \u21a6 d' \u2192\n \u0394 , \u2205 \u22a2 d' :: \u03c4\n preservation D (Step x x\u2081 x\u2082)\n with pres-lem2 D x\n ... | (_ , wt) = pres-lem x D wt (pres-lem3 wt x\u2081) x\u2082\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"c0147d23185a96901fb63acfb8b5d86e4e892500","subject":"FunUniverse.BinTree: without-K","message":"FunUniverse.BinTree: without-K\n","repos":"crypto-agda\/crypto-agda","old_file":"FunUniverse\/BinTree.agda","new_file":"FunUniverse\/BinTree.agda","new_contents":"{-# OPTIONS --without-K #-}\nmodule FunUniverse.BinTree where\n\nopen import Type hiding (\u2605)\nopen import Function\nopen import Data.Nat.NP using (\u2115; zero; suc; _\u2264_; s\u2264s; _+_; module \u2115\u2264; module \u2115\u00b0)\nopen import Data.Nat.Properties\nopen import Data.Bool\nopen import Data.Vec using (_++_)\nopen import Data.Bits\n--import Data.Bits.Search as Search\n--open Search.SimpleSearch\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\nopen import Composition.Horizontal\nopen import Composition.Vertical\nopen import Composition.Forkable\n\ndata Tree {a} (A : \u2605 a) : \u2115 \u2192 \u2605 a where\n leaf : \u2200 {n} \u2192 A \u2192 Tree A n\n fork : \u2200 {n} (left right : Tree A n) \u2192 Tree A (suc n)\n\nfromFun : \u2200 {n a} {A : \u2605 a} \u2192 (Bits n \u2192 A) \u2192 Tree A n\nfromFun {zero} f = leaf (f [])\nfromFun {suc n} f = fork (fromFun (f \u2218 0\u2237_)) (fromFun (f \u2218 1\u2237_))\n\ntoFun : \u2200 {n a} {A : \u2605 a} \u2192 Tree A n \u2192 Bits n \u2192 A\ntoFun (leaf x) _ = x\ntoFun (fork left right) (b \u2237 bs) = toFun (if b then right else left) bs\n\ntoFun\u2218fromFun : \u2200 {n a} {A : \u2605 a} (f : Bits n \u2192 A) \u2192 toFun (fromFun f) \u2257 f\ntoFun\u2218fromFun {zero} f [] = refl\ntoFun\u2218fromFun {suc n} f (false {-0b-} \u2237 bs) = toFun\u2218fromFun {n} (f \u2218 0\u2237_) bs\ntoFun\u2218fromFun {suc n} f (true {-1b-} \u2237 bs) = toFun\u2218fromFun {n} (f \u2218 1\u2237_) bs\n\nfold : \u2200 {n a} {A : \u2605 a} (op : A \u2192 A \u2192 A) \u2192 Tree A n \u2192 A\nfold _ (leaf x) = x\nfold _\u00b7_ (fork t\u2080 t\u2081) = fold _\u00b7_ t\u2080 \u00b7 fold _\u00b7_ t\u2081\n\n{-\nsearch\u2261fold\u2218fromFun : \u2200 {n a} {A : \u2605 a} op (f : Bits n \u2192 A) \u2192 search op f \u2261 fold op (fromFun f)\nsearch\u2261fold\u2218fromFun {zero} op f = refl\nsearch\u2261fold\u2218fromFun {suc n} op f\n rewrite search\u2261fold\u2218fromFun op (f \u2218 0\u2237_)\n | search\u2261fold\u2218fromFun op (f \u2218 1\u2237_) = refl\n-}\n\nleaf\u207f : \u2200 {n a} {A : \u2605 a} \u2192 A \u2192 Tree A n\nleaf\u207f {zero} x = leaf x\nleaf\u207f {suc n} x = fork t t where t = leaf\u207f x\n\nexpand : \u2200 {n a} {A : \u2605 a} \u2192 Tree A n \u2192 Tree A n\nexpand (leaf x) = leaf\u207f x\nexpand (fork t\u2080 t\u2081) = fork (expand t\u2080) (expand t\u2081)\n\nfromConst\u2261leaf\u207f : \u2200 {n a} {A : \u2605 a} (x : A) \u2192 fromFun (const x) \u2261 leaf\u207f {n} x\nfromConst\u2261leaf\u207f {zero} _ = refl\nfromConst\u2261leaf\u207f {suc n} x = cong\u2082 fork (fromConst\u2261leaf\u207f x) (fromConst\u2261leaf\u207f x)\n\nfromFun\u2218toFun : \u2200 {n a} {A : \u2605 a} (t : Tree A n) \u2192 fromFun (toFun t) \u2261 expand t\nfromFun\u2218toFun (leaf x) = fromConst\u2261leaf\u207f x\nfromFun\u2218toFun (fork t\u2080 t\u2081) = cong\u2082 fork (fromFun\u2218toFun t\u2080) (fromFun\u2218toFun t\u2081)\n\nlookup : \u2200 {n a} {A : \u2605 a} \u2192 Bits n \u2192 Tree A n \u2192 A\nlookup = flip toFun\n\nmap : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b} \u2192 (A \u2192 B) \u2192 Tree A n \u2192 Tree B n\nmap f (leaf x) = leaf (f x)\nmap f (fork t\u2080 t\u2081) = fork (map f t\u2080) (map f t\u2081)\n\nweaken\u2264 : \u2200 {m n a} {A : \u2605 a} \u2192 m \u2264 n \u2192 Tree A m \u2192 Tree A n\nweaken\u2264 _ (leaf x) = leaf x\nweaken\u2264 (s\u2264s p) (fork left right) = fork (weaken\u2264 p left) (weaken\u2264 p right)\n\nm\u2264n+m : \u2200 m n \u2192 m \u2264 n + m\nm\u2264n+m m n = \u2115\u2264.trans (m\u2264m+n m n) (\u2115\u2264.reflexive (\u2115\u00b0.+-comm m n))\n\nweaken+ : \u2200 n {m a} {A : \u2605 a} \u2192 Tree A m \u2192 Tree A (n + m)\nweaken+ n = weaken\u2264 (m\u2264n+m _ n)\n\n_>>=_ : \u2200 {n\u2081 n\u2082 a b} {A : \u2605 a} {B : \u2605 b}\n \u2192 Tree A n\u2081 \u2192 (A \u2192 Tree B n\u2082) \u2192 Tree B (n\u2081 + n\u2082)\nleaf {n} x >>= f = weaken+ n (f x)\nfork \u2113 r >>= f = fork (\u2113 >>= f) (r >>= f)\n\njoin : \u2200 {c\u2081 c\u2082 a} {A : \u2605 a} \u2192 Tree (Tree A c\u2082) c\u2081 \u2192 Tree A (c\u2081 + c\u2082)\njoin t = t >>= id\n\n_\u2192\u1d57_ : (i o : \u2115) \u2192 \u2605\u2080\ni \u2192\u1d57 o = Tree (Bits o) i\n\n_>>>_ : \u2200 {m n a} {A : \u2605 a} \u2192 Tree (Bits m) n \u2192 Tree A m \u2192 Tree A n\nf >>> g = map (flip lookup g) f\n\n_***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 i\u2080 \u2192\u1d57 o\u2080 \u2192 i\u2081 \u2192\u1d57 o\u2081 \u2192 (i\u2080 + i\u2081) \u2192\u1d57 (o\u2080 + o\u2081)\n(f *** g) = join (map (\u03bb xs \u2192 map (_++_ xs) g) f)\n\nhcomposable : HComposable _\u2192\u1d57_\nhcomposable = mk _>>>_\n\nvcomposable : VComposable _+_ _\u2192\u1d57_\nvcomposable = mk _***_\n\nforkable : Forkable suc _\u2192\u1d57_\nforkable = mk fork\n\n-- This is probably useless by now\ndata Rot {a} {A : \u2605 a} : \u2200 {n} (left right : Tree A n) \u2192 \u2605 a where\n leaf : \u2200 {n} x \u2192 Rot {n = n} (leaf x) (leaf x)\n fork : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 left\u2081 \u2192\n Rot right\u2080 right\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n krof : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 right\u2081 \u2192\n Rot right\u2080 left\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n\nRot-refl : \u2200 {n a} {A : \u2605 a} \u2192 Reflexive (Rot {A = A} {n})\nRot-refl {x = leaf x} = leaf x\nRot-refl {x = fork left right} = fork Rot-refl Rot-refl\n\nRot-sym : \u2200 {n a} {A : \u2605 a} \u2192 Symmetric (Rot {A = A} {n})\nRot-sym (leaf x) = leaf x\nRot-sym (fork p\u2080 p\u2081) = fork (Rot-sym p\u2080) (Rot-sym p\u2081)\nRot-sym (krof p\u2080 p\u2081) = krof (Rot-sym p\u2081) (Rot-sym p\u2080)\n\n{- with-K\nRot-trans : \u2200 {n a} {A : \u2605 a} \u2192 Transitive (Rot {A = A} {n})\nRot-trans (leaf x) q = q\nRot-trans (fork p\u2080 p\u2081) (fork q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (fork p\u2080 p\u2081) (krof q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (krof p\u2080 p\u2081) (fork q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\nRot-trans (krof p\u2080 p\u2081) (krof q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\n-- -}\n","old_contents":"-- NOTE with-K\nmodule FunUniverse.BinTree where\n\nopen import Type hiding (\u2605)\nopen import Function\nopen import Data.Nat.NP using (\u2115; zero; suc; _\u2264_; s\u2264s; _+_; module \u2115\u2264; module \u2115\u00b0)\nopen import Data.Nat.Properties\nopen import Data.Bool\nopen import Data.Vec using (_++_)\nopen import Data.Bits\n--import Data.Bits.Search as Search\n--open Search.SimpleSearch\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality\nopen import Composition.Horizontal\nopen import Composition.Vertical\nopen import Composition.Forkable\n\ndata Tree {a} (A : \u2605 a) : \u2115 \u2192 \u2605 a where\n leaf : \u2200 {n} \u2192 A \u2192 Tree A n\n fork : \u2200 {n} (left right : Tree A n) \u2192 Tree A (suc n)\n\nfromFun : \u2200 {n a} {A : \u2605 a} \u2192 (Bits n \u2192 A) \u2192 Tree A n\nfromFun {zero} f = leaf (f [])\nfromFun {suc n} f = fork (fromFun (f \u2218 0\u2237_)) (fromFun (f \u2218 1\u2237_))\n\ntoFun : \u2200 {n a} {A : \u2605 a} \u2192 Tree A n \u2192 Bits n \u2192 A\ntoFun (leaf x) _ = x\ntoFun (fork left right) (b \u2237 bs) = toFun (if b then right else left) bs\n\ntoFun\u2218fromFun : \u2200 {n a} {A : \u2605 a} (f : Bits n \u2192 A) \u2192 toFun (fromFun f) \u2257 f\ntoFun\u2218fromFun {zero} f [] = refl\ntoFun\u2218fromFun {suc n} f (false {-0b-} \u2237 bs) = toFun\u2218fromFun {n} (f \u2218 0\u2237_) bs\ntoFun\u2218fromFun {suc n} f (true {-1b-} \u2237 bs) = toFun\u2218fromFun {n} (f \u2218 1\u2237_) bs\n\nfold : \u2200 {n a} {A : \u2605 a} (op : A \u2192 A \u2192 A) \u2192 Tree A n \u2192 A\nfold _ (leaf x) = x\nfold _\u00b7_ (fork t\u2080 t\u2081) = fold _\u00b7_ t\u2080 \u00b7 fold _\u00b7_ t\u2081\n\n{-\nsearch\u2261fold\u2218fromFun : \u2200 {n a} {A : \u2605 a} op (f : Bits n \u2192 A) \u2192 search op f \u2261 fold op (fromFun f)\nsearch\u2261fold\u2218fromFun {zero} op f = refl\nsearch\u2261fold\u2218fromFun {suc n} op f\n rewrite search\u2261fold\u2218fromFun op (f \u2218 0\u2237_)\n | search\u2261fold\u2218fromFun op (f \u2218 1\u2237_) = refl\n-}\n\nleaf\u207f : \u2200 {n a} {A : \u2605 a} \u2192 A \u2192 Tree A n\nleaf\u207f {zero} x = leaf x\nleaf\u207f {suc n} x = fork t t where t = leaf\u207f x\n\nexpand : \u2200 {n a} {A : \u2605 a} \u2192 Tree A n \u2192 Tree A n\nexpand (leaf x) = leaf\u207f x\nexpand (fork t\u2080 t\u2081) = fork (expand t\u2080) (expand t\u2081)\n\nfromConst\u2261leaf\u207f : \u2200 {n a} {A : \u2605 a} (x : A) \u2192 fromFun (const x) \u2261 leaf\u207f {n} x\nfromConst\u2261leaf\u207f {zero} _ = refl\nfromConst\u2261leaf\u207f {suc n} x rewrite fromConst\u2261leaf\u207f {n} x = refl\n\nfromFun\u2218toFun : \u2200 {n a} {A : \u2605 a} (t : Tree A n) \u2192 fromFun (toFun t) \u2261 expand t\nfromFun\u2218toFun (leaf x) = fromConst\u2261leaf\u207f x\nfromFun\u2218toFun (fork t\u2080 t\u2081) = cong\u2082 fork (fromFun\u2218toFun t\u2080) (fromFun\u2218toFun t\u2081)\n\nlookup : \u2200 {n a} {A : \u2605 a} \u2192 Bits n \u2192 Tree A n \u2192 A\nlookup = flip toFun\n\nmap : \u2200 {n a b} {A : \u2605 a} {B : \u2605 b} \u2192 (A \u2192 B) \u2192 Tree A n \u2192 Tree B n\nmap f (leaf x) = leaf (f x)\nmap f (fork t\u2080 t\u2081) = fork (map f t\u2080) (map f t\u2081)\n\nweaken\u2264 : \u2200 {m n a} {A : \u2605 a} \u2192 m \u2264 n \u2192 Tree A m \u2192 Tree A n\nweaken\u2264 _ (leaf x) = leaf x\nweaken\u2264 (s\u2264s p) (fork left right) = fork (weaken\u2264 p left) (weaken\u2264 p right)\n\nm\u2264n+m : \u2200 m n \u2192 m \u2264 n + m\nm\u2264n+m m n = \u2115\u2264.trans (m\u2264m+n m n) (\u2115\u2264.reflexive (\u2115\u00b0.+-comm m n))\n\nweaken+ : \u2200 n {m a} {A : \u2605 a} \u2192 Tree A m \u2192 Tree A (n + m)\nweaken+ n = weaken\u2264 (m\u2264n+m _ n)\n\n_>>=_ : \u2200 {n\u2081 n\u2082 a b} {A : \u2605 a} {B : \u2605 b}\n \u2192 Tree A n\u2081 \u2192 (A \u2192 Tree B n\u2082) \u2192 Tree B (n\u2081 + n\u2082)\nleaf {n} x >>= f = weaken+ n (f x)\nfork \u2113 r >>= f = fork (\u2113 >>= f) (r >>= f)\n\njoin : \u2200 {c\u2081 c\u2082 a} {A : \u2605 a} \u2192 Tree (Tree A c\u2082) c\u2081 \u2192 Tree A (c\u2081 + c\u2082)\njoin t = t >>= id\n\n_\u2192\u1d57_ : (i o : \u2115) \u2192 \u2605\u2080\ni \u2192\u1d57 o = Tree (Bits o) i\n\n_>>>_ : \u2200 {m n a} {A : \u2605 a} \u2192 Tree (Bits m) n \u2192 Tree A m \u2192 Tree A n\nf >>> g = map (flip lookup g) f\n\n_***_ : \u2200 {i\u2080 i\u2081 o\u2080 o\u2081} \u2192 i\u2080 \u2192\u1d57 o\u2080 \u2192 i\u2081 \u2192\u1d57 o\u2081 \u2192 (i\u2080 + i\u2081) \u2192\u1d57 (o\u2080 + o\u2081)\n(f *** g) = join (map (\u03bb xs \u2192 map (_++_ xs) g) f)\n\nhcomposable : HComposable _\u2192\u1d57_\nhcomposable = mk _>>>_\n\nvcomposable : VComposable _+_ _\u2192\u1d57_\nvcomposable = mk _***_\n\nforkable : Forkable suc _\u2192\u1d57_\nforkable = mk fork\n\n-- This is probably useless by now\ndata Rot {a} {A : \u2605 a} : \u2200 {n} (left right : Tree A n) \u2192 \u2605 a where\n leaf : \u2200 {n} x \u2192 Rot {n = n} (leaf x) (leaf x)\n fork : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 left\u2081 \u2192\n Rot right\u2080 right\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n krof : \u2200 {n} {left\u2080 left\u2081 right\u2080 right\u2081 : Tree A n} \u2192\n Rot left\u2080 right\u2081 \u2192\n Rot right\u2080 left\u2081 \u2192\n Rot (fork left\u2080 right\u2080) (fork left\u2081 right\u2081)\n\nRot-refl : \u2200 {n a} {A : \u2605 a} \u2192 Reflexive (Rot {A = A} {n})\nRot-refl {x = leaf x} = leaf x\nRot-refl {x = fork left right} = fork Rot-refl Rot-refl\n\nRot-sym : \u2200 {n a} {A : \u2605 a} \u2192 Symmetric (Rot {A = A} {n})\nRot-sym (leaf x) = leaf x\nRot-sym (fork p\u2080 p\u2081) = fork (Rot-sym p\u2080) (Rot-sym p\u2081)\nRot-sym (krof p\u2080 p\u2081) = krof (Rot-sym p\u2081) (Rot-sym p\u2080)\n\nRot-trans : \u2200 {n a} {A : \u2605 a} \u2192 Transitive (Rot {A = A} {n})\nRot-trans (leaf x) q = q\nRot-trans (fork p\u2080 p\u2081) (fork q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (fork p\u2080 p\u2081) (krof q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2080) (Rot-trans p\u2081 q\u2081)\nRot-trans (krof p\u2080 p\u2081) (fork q\u2080 q\u2081) = krof (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\nRot-trans (krof p\u2080 p\u2081) (krof q\u2080 q\u2081) = fork (Rot-trans p\u2080 q\u2081) (Rot-trans p\u2081 q\u2080)\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"1d642ac95ff88e3ac92e6a4700c3fec8bccc42ed","subject":"nothing works...","message":"nothing works...\n","repos":"goodlyrottenapple\/lamYcalc","old_file":"Agda\/ITyping.agda","new_file":"Agda\/ITyping.agda","new_contents":"module ITyping where\n\nopen import Data.Empty\nopen import Data.List\nopen import Data.Nat\nopen import Data.Product\n-- open import Data.Maybe\nopen import Data.List.Any as LAny\nopen LAny.Membership-\u2261\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary.Core\n\n\nopen import Core\nopen import Core-Lemmas\nopen import Typing\nopen import Reduction\n\n\ndata IType : Set where\n o : IType\n _~>_ : IType -> IType -> IType\n \u2229 : List IType -> IType\n\n\u03c9 = \u2229 []\n\n\u2229' : IType -> IType\n\u2229' x = \u2229 (x \u2237 [])\n\n~>-inj-l : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2261 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) -> \u03c4\u2081\u2081 \u2261 \u03c4\u2082\u2081\n~>-inj-l refl = refl\n\n~>-inj-r : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2261 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) -> \u03c4\u2081\u2082 \u2261 \u03c4\u2082\u2082\n~>-inj-r refl = refl\n\n\u2229-inj-cons : \u2200 {x y \u03c4\u1d62 \u03c4\u2c7c} -> \u2229 (x \u2237 \u03c4\u1d62) \u2261 \u2229 (y \u2237 \u03c4\u2c7c) -> \u2229 \u03c4\u1d62 \u2261 \u2229 \u03c4\u2c7c\n\u2229-inj-cons refl = refl\n\n\u2229-inj : \u2200 {x y \u03c4\u1d62 \u03c4\u2c7c} -> \u2229 (x \u2237 \u03c4\u1d62) \u2261 \u2229 (y \u2237 \u03c4\u2c7c) -> x \u2261 y\n\u2229-inj refl = refl\n\n\n_\u225fTI_ : Decidable {A = IType} _\u2261_\no \u225fTI o = yes refl\no \u225fTI (_ ~> _) = no (\u03bb ())\no \u225fTI (\u2229 _) = no (\u03bb ())\n\n(_ ~> _) \u225fTI o = no (\u03bb ())\n(_ ~> _) \u225fTI (\u2229 _) = no (\u03bb ())\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) with \u03c4\u2081\u2081 \u225fTI \u03c4\u2082\u2081 | \u03c4\u2081\u2082 \u225fTI \u03c4\u2082\u2082\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (.\u03c4\u2081\u2081 ~> .\u03c4\u2081\u2082) | yes refl | yes refl = yes refl\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (.\u03c4\u2081\u2081 ~> \u03c4\u2082\u2082) | yes refl | no \u03c4\u2081\u2082\u2260\u03c4\u2082\u2082 = no (\u03bb eq \u2192 \u03c4\u2081\u2082\u2260\u03c4\u2082\u2082 (~>-inj-r eq))\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) | no \u03c4\u2081\u2081\u2260\u03c4\u2082\u2081 | _ = no (\u03bb eq \u2192 \u03c4\u2081\u2081\u2260\u03c4\u2082\u2081 (~>-inj-l eq))\n\n(\u2229 _) \u225fTI o = no (\u03bb ())\n(\u2229 _) \u225fTI (_ ~> _) = no (\u03bb ())\n\u2229 [] \u225fTI \u2229 [] = yes refl\n\u2229 [] \u225fTI \u2229 (x \u2237 \u03c4\u2c7c) = no (\u03bb ())\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 [] = no (\u03bb ())\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (y \u2237 \u03c4\u2c7c) with x \u225fTI y | (\u2229 \u03c4\u1d62) \u225fTI (\u2229 \u03c4\u2c7c)\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (.x \u2237 .\u03c4\u1d62) | yes refl | yes refl = yes refl\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (.x \u2237 \u03c4\u2c7c) | yes refl | no \u03c4\u1d62\u2260\u03c4\u2c7c = no (\u03bb \u2229x\u2237\u03c4\u1d62\u2261\u2229x\u2237\u03c4\u2c7c \u2192 \u03c4\u1d62\u2260\u03c4\u2c7c (\u2229-inj-cons \u2229x\u2237\u03c4\u1d62\u2261\u2229x\u2237\u03c4\u2c7c))\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (y \u2237 \u03c4\u2c7c) | no x\u2260y | _ = no (\u03bb \u2229x\u2237\u03c4\u1d62\u2261\u2229y\u2237\u03c4\u2c7c \u2192 x\u2260y (\u2229-inj \u2229x\u2237\u03c4\u1d62\u2261\u2229y\u2237\u03c4\u2c7c))\n\n\n\nICtxt = List (Atom \u00d7 IType)\n\n\ndata Wf-ICtxt : ICtxt -> Set where\n nil : Wf-ICtxt []\n cons : \u2200 {\u0393 x \u03c4} -> (x\u2209 : x \u2209 dom \u0393) -> Wf-ICtxt \u0393 ->\n Wf-ICtxt ((x , \u03c4) \u2237 \u0393)\n\n\ndata _\u2237'_ : IType -> Type -> Set where\n base : o \u2237' \u03c3\n arr : \u2200 {\u03b4 \u03c4 A B} -> \u03b4 \u2237' A -> \u03c4 \u2237' B -> (\u03b4 ~> \u03c4) \u2237' (A \u27f6 B)\n \u2229-nil : \u2200 {A} -> \u03c9 \u2237' A\n \u2229-cons : \u2200 {\u03c4\u1d62 \u03c4 A} -> \u03c4 \u2237' A -> \u2229 \u03c4\u1d62 \u2237' A -> \u2229 (\u03c4 \u2237 \u03c4\u1d62) \u2237' A\n\n\ndata _\u2264\u2229_ : IType -> IType -> Set where\n base : o \u2264\u2229 o\n arr : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> \u03c4\u2081\u2082 \u2264\u2229 \u03c4\u2082\u2082 -> \u03c4\u2082\u2081 \u2264\u2229 \u03c4\u2081\u2081 -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2264\u2229 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082)\n \u2229-\u2208 : \u2200 {\u03c4 \u03c4\u1d62} -> \u03c4 \u2208 \u03c4\u1d62 -> \u2229 \u03c4\u1d62 \u2264\u2229 \u03c4\n \u2229-nil : \u2200 {\u03c4} -> \u03c4 \u2264\u2229 \u03c9\n \u2229-cons : \u2200 {\u03c4 \u03c4' \u03c4\u1d62} -> \u03c4 \u2264\u2229 \u03c4' -> \u03c4 \u2264\u2229 \u2229 \u03c4\u1d62 -> \u03c4 \u2264\u2229 \u2229 (\u03c4' \u2237 \u03c4\u1d62)\n -- \u2229-trans : \u2200 {\u03c4\u2081 \u03c4\u2082 \u03c4\u2083} -> \u03c4\u2081 \u2264\u2229 \u03c4\u2082 -> \u03c4\u2082 \u2264\u2229 \u03c4\u2083 -> \u03c4\u2081 \u2264\u2229 \u03c4\u2083\n\n\n\u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 : \u2200 {\u03c4\u1d62 \u03c4\u2c7c} -> \u03c4\u1d62 \u2286 \u03c4\u2c7c -> \u2229 \u03c4\u2c7c \u2264\u2229 \u2229 \u03c4\u1d62\n\u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 {[]} \u03c4\u1d62\u2286\u03c4\u2c7c = \u2229-nil\n\u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 {x \u2237 \u03c4\u1d62} \u03c4\u1d62\u2286\u03c4\u2c7c = \u2229-cons (\u2229-\u2208 (\u03c4\u1d62\u2286\u03c4\u2c7c (here refl))) (\u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 (\u03bb {x\u2081} z \u2192 \u03c4\u1d62\u2286\u03c4\u2c7c (there z)))\n\n\u2264\u2229-refl : \u2200 {\u03c4} -> \u03c4 \u2264\u2229 \u03c4\n\u2264\u2229-refl {o} = base\n\u2264\u2229-refl {\u03c4 ~> \u03c4\u2081} = arr \u2264\u2229-refl \u2264\u2229-refl\n\u2264\u2229-refl {\u2229 []} = \u2229-nil\n\u2264\u2229-refl {\u2229 (x \u2237 x\u2081)} = \u2229\u03c4\u2c7c\u2264\u2229\u03c4\u1d62 (\u03bb {x\u2082} z \u2192 z)\n\n\ndata \u039b : Type -> Set where\n bv : \u2200 {A} -> (i : \u2115) -> \u039b A\n fv : \u2200 {A} -> (x : Atom) -> \u039b A\n lam : \u2200 {B} -> (A : Type) -> (e : \u039b B) -> \u039b (A \u27f6 B)\n app : \u2200 {A B} -> (e\u2081 : \u039b (A \u27f6 B)) -> (e\u2082 : \u039b A) -> \u039b B\n Y : (t : Type) -> \u039b ((t \u27f6 t) \u27f6 t)\n\n\n-- _\u2208?_ : \u2200 {a} {A : Set a} -> Atom -> List (Atom \u00d7 A) -> Maybe A\n-- a \u2208? [] = nothing\n-- a \u2208? (l \u2237 ist) with a \u225f proj\u2081 l\n-- ... | yes _ = just (proj\u2082 l)\n-- a \u2208? (l \u2237 ist) | no _ = a \u2208? ist\n\n-- PTerm->\u039b : \u2200 {\u0393 m t} -> (List (Atom \u00d7 \u2115)) -> \u0393 \u22a2 m \u2236 t -> \u039b t\n-- PTerm->\u039b {m = bv i} _ ()\n-- PTerm->\u039b {m = fv x} bound \u0393\u22a2m\u2236t with x \u2208? bound\n-- PTerm->\u039b {m = fv x} {t} bound \u0393\u22a2m\u2236t | just i = bv {t} i\n-- PTerm->\u039b {m = fv x} bound \u0393\u22a2m\u2236t | nothing = fv x\n-- PTerm->\u039b {m = lam m} bound (abs {_} {\u03c4\u2081} {\u03c4\u2082} L cf) = lam \u03c4\u2081 (PTerm->\u039b ((x , 0) \u2237 bound') (cf (\u2209-cons-l _ _ x\u2209)))\n-- where\n-- x = \u2203fresh (L ++ FV m)\n-- x\u2209 : x \u2209 (L ++ FV m)\n-- x\u2209 = \u2203fresh-spec (L ++ FV m)\n--\n-- bound' : List (Atom \u00d7 \u2115)\n-- bound' = Data.List.map (\u03bb a,i \u2192 (proj\u2081 a,i) , suc (proj\u2082 a,i)) bound\n--\n-- PTerm->\u039b {m = app t1 t} bound (app \u0393\u22a2s \u0393\u22a2t) = app (PTerm->\u039b bound \u0393\u22a2s) (PTerm->\u039b bound \u0393\u22a2t)\n-- PTerm->\u039b {m = Y \u03c4} bound (Y x) = Y \u03c4\n--\n-- PTerm->\u039b : \u2200 {\u0393 m t} -> \u0393 \u22a2 m \u2236 t -> \u039b t\n-- PTerm->\u039b = PTerm->\u039b []\n\ndata _~_ : \u2200{t} -> \u039b t -> PTerm -> Set where\n bv : \u2200 {t i} -> (bv {t} i) ~ (bv i)\n fv : \u2200 {t x} -> (fv {t} x) ~ (fv x)\n lam : \u2200 {t s m m'} -> m ~ m' -> (lam {s} t m) ~ (lam m')\n app : \u2200 {t s m n m' n'} -> m ~ m' -> n ~ n' -> (app {t} {s} m n) ~ (app m' n')\n Y : \u2200 {t} -> (Y t) ~ (Y t)\n\n\u039b[_<<_] : \u2200 {t} -> \u2115 -> Atom -> \u039b t -> \u039b t\n\u039b[ k << x ] (bv i) = bv i\n\u039b[ k << x ] (fv y) with x \u225f y\n... | yes _ = bv k\n... | no _ = fv y\n\u039b[ k << x ] (lam t m) = lam t (\u039b[ (suc k) << x ] m)\n\u039b[ k << x ] (app t1 t2) = app (\u039b[ k << x ] t1) (\u039b[ k << x ] t2)\n\u039b[ k << x ] (Y t) = Y t\n\n\n\nPTerm->\u039b : \u2200 {\u0393 m t} -> \u0393 \u22a2 m \u2236 t -> \u039b t\nPTerm->\u039b {m = bv i} ()\nPTerm->\u039b {m = fv x} {t} \u0393\u22a2m\u2236t = fv {t} x\nPTerm->\u039b {m = lam m} (abs {_} {\u03c4\u2081} {\u03c4\u2082} L cf) =\n lam \u03c4\u2081 ( \u039b[ 0 << \u2203fresh (L ++ FV m) ]\n (PTerm->\u039b (cf (\u2209-cons-l _ _ (\u2203fresh-spec (L ++ FV m)) ))) )\nPTerm->\u039b {m = app t1 t} (app \u0393\u22a2s \u0393\u22a2t) = app (PTerm->\u039b \u0393\u22a2s) (PTerm->\u039b \u0393\u22a2t)\nPTerm->\u039b {m = Y \u03c4} (Y x) = Y \u03c4\n\n\n\u039b*^-*^~ : \u2200 {\u03c4 x k} t t' -> _~_ {\u03c4} t t' -> \u039b[ k << x ] t ~ ([ k << x ] t')\n\u039b*^-*^~ _ _ bv = bv\n\u039b*^-*^~ {x = x} (fv y) _ fv with x \u225f y\n\u039b*^-*^~ (fv x) .(fv x) fv | yes _ = bv\n\u039b*^-*^~ (fv y) .(fv y) fv | no _ = fv\n\u039b*^-*^~ _ _ (lam {m = m} {m'} t~t') = lam (\u039b*^-*^~ m m' t~t')\n\u039b*^-*^~ _ _ (app {m = m} {n} {m'} {n'} t~t' t~t'') = app (\u039b*^-*^~ m m' t~t') (\u039b*^-*^~ n n' t~t'')\n\u039b*^-*^~ _ _ Y = Y\n\n\n\n\u039b*^-*^-swap : \u2200 {t : Type} k n x y m -> \u00ac(k \u2261 n) -> \u00ac(x \u2261 y) -> \u039b[_<<_] {t} k x (\u039b[ n << y ] m) \u2261 \u039b[ n << y ] (\u039b[ k << x ] m)\n\u039b*^-*^-swap k n x y (bv i) k\u2260n x\u2260y = refl\n\u039b*^-*^-swap k n x y (fv z) k\u2260n x\u2260y = {! !}\n\u039b*^-*^-swap k n x y (lam A m) k\u2260n x\u2260y =\n cong (lam A) (\u039b*^-*^-swap (suc k) (suc n) x y m (\u03bb x\u2081 \u2192 k\u2260n (\u2261-suc x\u2081)) x\u2260y)\n\u039b*^-*^-swap k n x y (app m m') k\u2260n x\u2260y rewrite\n \u039b*^-*^-swap k n x y m k\u2260n x\u2260y | \u039b*^-*^-swap k n x y m' k\u2260n x\u2260y = refl\n\u039b*^-*^-swap k n x y (Y t) k\u2260n x\u2260y = refl\n\n\nfv-^-\u039b*^-refl : \u2200 x t {k \u0393 \u03c4} -> x \u2209 FV t -> (\u0393\u22a2t^x : \u0393 \u22a2 [ k >> fv x ] t \u2236 \u03c4) -> (\u039b[ k << x ] (PTerm->\u039b \u0393\u22a2t^x) ) ~ t\nfv-^-\u039b*^-refl x (bv n) x\u2209FVt ()\nfv-^-\u039b*^-refl x (fv y) x\u2209FVt \u0393\u22a2t^x with x \u225f y\nfv-^-\u039b*^-refl x (fv .x) x\u2209FVt \u0393\u22a2t^x | yes refl = \u22a5-elim (x\u2209FVt (here refl))\nfv-^-\u039b*^-refl x (fv y) x\u2209FVt \u0393\u22a2t^x | no x\u2260y = fv\nfv-^-\u039b*^-refl x (lam t) {k} {\u0393} x\u2209FVt (abs {\u03c4\u2081 = \u03c4\u2081} {\u03c4\u2082} L cf) = lam {! !}\n --\n -- where\n -- x' = \u2203fresh ( L ++ FV ([ suc k >> fv x ] t) )\n -- x'\u2209 = \u2203fresh-spec ( L ++ FV ([ suc k >> fv x ] t) )\n --\n -- x'\u0393\u22a2[suc-k>>x]t^'x' : ((x' , \u03c4\u2081) \u2237 \u0393) \u22a2 ([ suc k >> fv x ] t) ^' x' \u2236 \u03c4\u2082\n -- x'\u0393\u22a2[suc-k>>x]t^'x' = cf (\u2209-cons-l L (FV ([ suc k >> fv x ] t)) (\u2203fresh-spec (L ++ FV ([ suc k >> fv x ] t))))\n --\n --\n -- x'\u0393\u22a2[suc-k>>x]t^'x'\u2261 : ((x' , \u03c4\u2081) \u2237 \u0393) \u22a2 [ suc k >> fv x ] (t ^' x') \u2236 \u03c4\u2082 \u2261\n -- ((x' , \u03c4\u2081) \u2237 \u0393) \u22a2 ([ suc k >> fv x ] t) ^' x' \u2236 \u03c4\u2082\n -- x'\u0393\u22a2[suc-k>>x]t^'x'\u2261 rewrite ^-^-swap (suc k) 0 x x' t (\u03bb ()) (\u03bb x\u2261x' \u2192 {! !}) = refl\n --\n -- x'\u0393\u22a2[suc-k>>x]t^'x'' : ((x' , \u03c4\u2081) \u2237 \u0393) \u22a2 [ suc k >> fv x ] (t ^' x') \u2236 \u03c4\u2082\n -- x'\u0393\u22a2[suc-k>>x]t^'x'' rewrite x'\u0393\u22a2[suc-k>>x]t^'x'\u2261 = x'\u0393\u22a2[suc-k>>x]t^'x'\n --\n -- ih'' : \u039b[ (suc k) << x ] (PTerm->\u039b x'\u0393\u22a2[suc-k>>x]t^'x'') ~ (t ^' x')\n -- ih'' = fv-^-\u039b*^-refl x ([ 0 >> fv x' ] t) {! !} x'\u0393\u22a2[suc-k>>x]t^'x''\n --\n -- -- ih' : \u039b[ 0 << x' ] ( \u039b[ (suc k) << x ] (PTerm->\u039b x'\u0393\u22a2[suc-k>>x]t^'x') ) ~ t\n -- -- ih' rewrite x'\u0393\u22a2[suc-k>>x]t^'x'\u2261 = {! !}\n --\n -- ih : \u039b[ (suc k) << x ] ( \u039b[ 0 << x' ] (PTerm->\u039b x'\u0393\u22a2[suc-k>>x]t^'x') ) ~ t\n -- ih rewrite\n -- \u039b*^-*^-swap {\u03c4\u2082} (suc k) 0 x x' (PTerm->\u039b x'\u0393\u22a2[suc-k>>x]t^'x') {! !} {! !} |\n -- x'\u0393\u22a2[suc-k>>x]t^'x'\u2261 = {! !}\n\nfv-^-\u039b*^-refl x (app s t) x\u2209FVt (app \u0393\u22a2s^x \u0393\u22a2t^x) = app\n (fv-^-\u039b*^-refl x s (\u2209-cons-l _ _ x\u2209FVt) \u0393\u22a2s^x)\n (fv-^-\u039b*^-refl x t (\u2209-cons-r (FV s) _ x\u2209FVt) \u0393\u22a2t^x)\nfv-^-\u039b*^-refl x (Y \u03c4) x\u2209FVt (Y x\u2081) = Y\n\n\n\nPTerm->\u039b~ : \u2200 {\u0393 t \u03c4} -> {\u0393\u22a2t : \u0393 \u22a2 t \u2236 \u03c4} -> (PTerm->\u039b \u0393\u22a2t) ~ t\nPTerm->\u039b~ {t = bv i} = \u03bb {\u03c4} \u2192 \u03bb {}\nPTerm->\u039b~ {t = fv x} = \u03bb {\u03c4} {\u0393\u22a2t} \u2192 fv\nPTerm->\u039b~ {t = lam t} {\u0393\u22a2t = abs {\u03c4\u2081 = \u03c4\u2081} {\u03c4\u2082} L cf} =\n lam (fv-^-\u039b*^-refl (\u2203fresh (L ++ FV t)) t (\u2209-cons-r L _ (\u2203fresh-spec (L ++ FV t))) (cf (\u2209-cons-l L (FV t) (\u2203fresh-spec (L ++ FV t)))))\n -- where\n -- x' = \u2203fresh (L ++ FV t)\n -- x'\u2237\u0393\u22a2t^'x' = cf (\u2209-cons-l L (FV t) (\u2203fresh-spec (L ++ FV t)))\n --\n -- ih' : \u039b[ 0 << x' ] (PTerm->\u039b x'\u2237\u0393\u22a2t^'x') ~ (* x' ^ (t ^' x'))\n -- ih' = {! !}\n --\n -- sub : \u2200 {\u03c4 x m} -> x \u2209 FV t -> _~_ {\u03c4} m t \u2261 m ~ (* x ^ (t ^' x))\n -- sub {_} {x} x\u2209 rewrite fv-^-*^-refl x t {0} x\u2209 = refl\n --\n -- ih : \u039b[ 0 << x' ] (PTerm->\u039b x'\u2237\u0393\u22a2t^'x') ~ t\n -- ih rewrite sub {_} {x'} {\u039b[ 0 << x' ] (PTerm->\u039b x'\u2237\u0393\u22a2t^'x')} {! !} = ih'\nPTerm->\u039b~ {t = app t t\u2081} {\u0393\u22a2t = app \u0393\u22a2t \u0393\u22a2t\u2081} = app PTerm->\u039b~ PTerm->\u039b~\nPTerm->\u039b~ {t = Y t\u2081} {\u0393\u22a2t = Y x} = Y\n\n\n\u039b->PTerm : \u2200 {t} -> \u039b t -> PTerm\n\u039b->PTerm (bv i) = bv i\n\u039b->PTerm (fv x) = fv x\n\u039b->PTerm (lam A \u039bt) = lam (\u039b->PTerm \u039bt)\n\u039b->PTerm (app \u039bs \u039bt) = app (\u039b->PTerm \u039bs) (\u039b->PTerm \u039bt)\n\u039b->PTerm (Y t) = Y t\n\n\n\n\n\n-- data IType\u209b : IType -> Set where\n-- o : IType\u209b o\n-- arr : \u2200 {\u03c4 \u03c4'} -> IType\u209b \u03c4 -> IType\u209b \u03c4' -> IType\u209b (\u03c4 ~> \u03c4')\n--\n-- data IType\u209b\u209b : IType -> Set where\n-- o : IType\u209b\u209b o\n-- arr : \u2200 {\u03c4 \u03c4'} -> IType\u209b\u209b \u03c4 -> IType\u209b\u209b \u03c4' -> IType\u209b\u209b (\u03c4 ~> \u03c4')\n-- \u2229-nil : IType\u209b\u209b \u03c9\n-- \u2229-cons : \u2200 {\u03c4 \u03c4\u1d62} -> IType\u209b \u03c4 -> IType\u209b\u209b (\u2229 \u03c4\u1d62) -> IType\u209b\u209b (\u2229 (\u03c4 \u2237 \u03c4\u1d62))\n--\n-- \u03c4\u209b->\u03c4\u209b\u209b : \u2200 {\u03c4} -> IType\u209b \u03c4 -> IType\u209b\u209b \u03c4\n-- \u03c4\u209b->\u03c4\u209b\u209b o = o\n-- \u03c4\u209b->\u03c4\u209b\u209b (arr \u03c4\u209b \u03c4\u209b\u2081) = arr (\u03c4\u209b->\u03c4\u209b\u209b \u03c4\u209b) (\u03c4\u209b->\u03c4\u209b\u209b \u03c4\u209b\u2081)\n\n\u039b[_>>_] : \u2200 {\u03c4 \u03c4'} -> \u2115 -> \u039b \u03c4' -> \u039b \u03c4 -> \u039b \u03c4\n\u039b[_>>_] {\u03c4} {\u03c4'} k u (bv i) with k \u225f i | \u03c4 \u225fT \u03c4'\n\u039b[ k >> u ] (bv i) | yes _ | yes refl = u\n... | yes _ | no _ = bv i\n... | no _ | _ = bv i\n\u039b[ k >> u ] (fv x) = fv x\n\u039b[ k >> u ] (lam A t) = lam A (\u039b[ (suc k) >> u ] t)\n\u039b[ k >> u ] (app t1 t2) = app (\u039b[ k >> u ] t1) (\u039b[ k >> u ] t2) -- app (\u039b[ k >> u ] t1) (\u039b[ k >> u ] t2)\n\u039b[ k >> u ] (Y t) = Y t\n\n\ndata Y-shape : \u2200 {\u03c4} -> \u039b \u03c4 -> Set where\n intro\u2081 : \u2200 {A m} -> Y-shape (app (Y A) m)\n intro\u2082 : \u2200 {A m} -> Y-shape (app m (app (Y A) m))\n\ndata _\u22a9_\u2236_ : \u2200 {A} -> ICtxt -> \u039b A -> IType -> Set where\n var : \u2200 {A \u0393 x \u03c4} {\u03c4\u1d62 : List IType} -> (wf-\u0393 : Wf-ICtxt \u0393) -> (\u03c4\u1d62\u2208\u0393 : (x , (\u2229 \u03c4\u1d62)) \u2208 \u0393) -> (\u03c4\u1d62\u2264\u2229\u03c4 : \u2229 \u03c4\u1d62 \u2264\u2229 \u03c4) -> \u03c4 \u2237' A ->\n \u0393 \u22a9 fv {A} x \u2236 \u03c4\n app : \u2200 {A B \u0393 s t \u03c4\u2081 \u03c4\u2082} -> \u0393 \u22a9 s \u2236 (\u03c4\u2081 ~> \u03c4\u2082) -> \u0393 \u22a9 t \u2236 \u03c4\u2081 -> (\u03c4\u2081 ~> \u03c4\u2082) \u2237' (A \u27f6 B) -> \u03c4\u2081 \u2237' B ->\n \u0393 \u22a9 (app {A} {B} s t) \u2236 \u03c4\u2082\n \u2229-nil : \u2200 {A \u0393} {m : \u039b A} -> (\u00acY-shape : \u00ac Y-shape m) -> (wf-\u0393 : Wf-ICtxt \u0393) -> \u0393 \u22a9 m\u2005 \u2236 \u03c9\n \u2229-cons : \u2200 {A \u0393 \u03c4 \u03c4\u1d62} {m : \u039b A} -> (\u00acY-shape : \u00ac Y-shape m) -> (wf-\u0393 : Wf-ICtxt \u0393) ->\n \u0393 \u22a9 m\u2005 \u2236 \u03c4 -> \u0393 \u22a9 m\u2005 \u2236 (\u2229 \u03c4\u1d62) -> \u0393 \u22a9 m\u2005 \u2236 (\u2229 (\u03c4 \u2237 \u03c4\u1d62))\n abs : \u2200 {A B \u0393 \u03c4\u1d62 \u03c4} (L : FVars) -> \u2200 {t : \u039b B} ->\n ( cf : \u2200 {x} -> (x\u2209L : x \u2209 L) -> ((x , \u2229 \u03c4\u1d62) \u2237 \u0393) \u22a9 \u039b[ 0 >> fv {A} x ] t \u2236 \u03c4 ) -> \u2229 \u03c4\u1d62 \u2237' A -> \u03c4 \u2237' B -> \u0393 \u22a9 lam A t \u2236 (\u2229 \u03c4\u1d62 ~> \u03c4)\n Y : \u2200 {\u0393 A \u03c4 \u03c4\u2081 \u03c4\u2082} -> Wf-ICtxt \u0393 -> \u03c4 \u2237' A -> \u03c4\u2081 \u2237' A -> \u03c4\u2082 \u2237' A ->\n \u0393 \u22a9 Y A \u2236 ((\u03c4 ~> \u03c4\u2081) ~> \u03c4\u2082)\n\n\n-- \u22a9->\u03b2 : \u2200 {\u0393 m m' \u03c4} -> \u0393 \u22a9 m' \u2236 \u03c4 -> m ->\u03b2 m' -> \u0393 \u22a9 m \u2236 \u03c4\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redL x m->\u03b2m') = \u22a9->\u03b2-redL \u0393\u22a9m'\u2236\u03c4 m->\u03b2m'\n-- where\n-- \u22a9->\u03b2-redL : \u2200 {\u0393 m m' n \u03c4} -> \u0393 \u22a9 app m' n \u2236 \u03c4 -> m ->\u03b2 m' -> \u0393 \u22a9 app m n \u2236 \u03c4\n-- \u22a9->\u03b2-redL = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redR x m->\u03b2m') = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (abs L x) = {! !}\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (beta x x\u2081) = {! !}\n-- \u22a9->\u03b2 {\u03c4 = \u03c4} (app \u0393\u22a9m\u2236\u03c4'~>\u03c4 (app (Y {\u03c4 = \u03c4'} wf-\u0393 \u03c4\u2237 \u03c4\u2081\u2237 \u03c4\u2082\u2237) \u0393\u22a9m\u2236\u03c4'~>\u03c4')) (Y trm-m) =\n-- app (Y wf-\u0393 \u03c4\u2237 \u03c4\u2081\u2237 {! !}) \u0393\u22a9m\u2236\u03c4'~>\u03c4'\n--\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4 (\u2229-nil \u00acY-shape wf-\u0393 trm-m)) (Y x) = \u22a5-elim (\u00acY-shape intro\u2081)\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4 (\u2229-cons \u00acY-shape wf-\u0393 trm-m \u0393\u22a9m'\u2236\u03c4\u2081 \u0393\u22a9m'\u2236\u03c4\u2082)) (Y x\u2081) = \u22a5-elim (\u00acY-shape intro\u2081)\n-- \u22a9->\u03b2 (\u2229-nil \u00acY-shape wf-\u0393 trm-m) (Y x) = \u22a5-elim (\u00acY-shape intro\u2082)\n-- \u22a9->\u03b2 (\u2229-cons \u00acY-shape wf-\u0393 trm-m \u0393\u22a9m'\u2236\u03c4 \u0393\u22a9m'\u2236\u03c4\u2081) (Y x\u2081) = \u22a5-elim (\u00acY-shape intro\u2082)\n--\n--\n\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (redL x m->\u03b2m') = \u22a9->\u03b2-redL \u0393\u22a9m'\u2236\u03c4 m->\u03b2m'\n-- where\n-- \u22a9->\u03b2-redL : \u2200 {\u0393 m m' n \u03c4} -> \u0393 \u22a9 app m' n \u2236 \u03c4 -> m ->\u03b2 m' -> \u0393 \u22a9 app m n \u2236 \u03c4\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (redL x\u2081 m->\u03b2m'') = app (\u22a9->\u03b2-redL \u0393\u22a9m'n\u2236\u03c4 m->\u03b2m'') \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9m''n\u2081n\u2082\u2236\u03c4\u1d62 wf-\u0393 (app _ trm-n\u2082)) (redL trm-n\u2081 m->\u03b2m'') =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2-redL (\u0393\u22a9m''n\u2081n\u2082\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2081 m->\u03b2m'')) wf-\u0393 (app (app (->\u03b2-Term-l m->\u03b2m'') trm-n\u2081) trm-n\u2082)\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (redR x\u2081 m->\u03b2m'') = app (\u22a9->\u03b2 \u0393\u22a9m'n\u2236\u03c4 (redR x\u2081 m->\u03b2m'')) \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9m\u2081e\u2082n\u2082\u2236\u03c4\u1d62 wf-\u0393 (app (app _ trm-e\u2082) trm-n\u2082)) (redR trm-m\u2081 n\u2081->e\u2082) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9m\u2081e\u2082n\u2082\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2082 (redR trm-m\u2081 n\u2081->e\u2082))) wf-\u0393 (app (app trm-m\u2081 (->\u03b2-Term-l n\u2081->e\u2082)) trm-n\u2082)\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (abs L cf) = app (\u22a9->\u03b2 \u0393\u22a9m'n\u2236\u03c4 (abs L cf)) \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9lam-m''n\u2081\u2236\u03c4\u1d62 wf-\u0393 (app trm-lam-m'' trm-n\u2081)) (abs L cf) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9lam-m''n\u2081\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2081 (abs L cf))) wf-\u0393 (app (lam L (\u03bb x\u2209L \u2192 ->\u03b2-Term-l (cf x\u2209L))) trm-n\u2081)\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (beta trm-lam-m\u2081 trm-n\u2081) = app (\u22a9->\u03b2 \u0393\u22a9m'n\u2236\u03c4 (beta trm-lam-m\u2081 trm-n\u2081)) \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9m\u2081^n\u2081n\u2082\u2236\u03c4\u2081 wf-\u0393 (app trm-m\u2081^n\u2081 trm-n\u2082)) (beta trm-lam-m\u2081 trm-n\u2081) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9m\u2081^n\u2081n\u2082\u2236\u03c4\u2081 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2082 (beta trm-lam-m\u2081 trm-n\u2081))) wf-\u0393 (app (app trm-lam-m\u2081 trm-n\u2081) trm-n\u2082)\n-- \u22a9->\u03b2-redL (app \u0393\u22a9m'n\u2236\u03c4 \u0393\u22a9m'n\u2236\u03c4\u2081) (Y trm-m\u2081) = app (\u22a9->\u03b2 \u0393\u22a9m'n\u2236\u03c4 (Y trm-m\u2081)) \u0393\u22a9m'n\u2236\u03c4\u2081\n-- \u22a9->\u03b2-redL (\u2229-intro \u0393\u22a9m\u2081Ym\u2081n\u2081\u2236\u03c4\u1d62 wf-\u0393 (app (app _ trm-Ym\u2081) trm-n\u2081)) (Y trm-m\u2081) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9m\u2081Ym\u2081n\u2081\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redL trm-n\u2081 (Y trm-m\u2081))) wf-\u0393 (app trm-Ym\u2081 trm-n\u2081)\n--\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4 \u0393\u22a9m'\u2236\u03c4\u2081) (redR trm-n m->\u03b2m') = app \u0393\u22a9m'\u2236\u03c4 (\u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4\u2081 m->\u03b2m')\n-- \u22a9->\u03b2 (\u2229-intro \u0393\u22a9mn'\u2236\u03c4\u1d62 wf-\u0393 trm-mn') (redR trm-m n->\u03b2n') =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9mn'\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (redR trm-m n->\u03b2n')) wf-\u0393 (app trm-m (->\u03b2-Term-l n->\u03b2n'))\n-- \u22a9->\u03b2 (\u2229-intro \u0393\u22a9lam-m'\u2236\u03c4\u1d62 wf-\u0393 trm-lam-x) (abs L x\u2082) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9lam-m'\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (abs L x\u2082)) wf-\u0393 (lam L (\u03bb x\u2209L \u2192 ->\u03b2-Term-l (x\u2082 x\u2209L)))\n-- \u22a9->\u03b2 (abs L cf) (abs L\u2081 x) = abs (L ++ L\u2081) (\u03bb x\u2209L \u2192 \u22a9->\u03b2 (cf (\u2209-cons-l _ _ x\u2209L)) (x (\u2209-cons-r L _ x\u2209L)))\n-- \u22a9->\u03b2 \u0393\u22a9m'\u2236\u03c4 (beta x x\u2081) = {! !}\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4'~>\u03c4 (app \u0393\u22a9Ym\u2236\u03c4' \u0393\u22a9Ym\u2236\u03c4'')) (Y trm-m) = {! !}\n-- \u22a9->\u03b2 (app \u0393\u22a9m'\u2236\u03c4'~>\u03c4 (\u2229-intro {\u03c4\u1d62 = []} \u0393\u22a9Ym\u2236\u03c4\u1d62 wf-\u0393 trm-Ym)) (Y trm-m) = {! !}\n-- \u22a9->\u03b2 {\u0393} (app {s = m} \u0393\u22a9m'\u2236\u03c4'~>\u03c4 (\u2229-intro {\u03c4\u1d62 = x \u2237 \u03c4\u1d62} \u0393\u22a9Ym\u2236\u03c4\u1d62 wf-\u0393 trm-Ym)) (Y trm-m) = {! !}\n-- where\n-- \u0393\u22a9m\u2236x~>x : \u0393 \u22a9 m \u2236 (x ~> x)\n-- \u0393\u22a9m\u2236x~>x = {! !}\n-- \u22a9->\u03b2 (\u2229-intro \u0393\u22a9mYm\u2236\u03c4\u1d62 wf-\u0393 (app _ trm-Ym)) (Y trm-m) =\n-- \u2229-intro (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u22a9->\u03b2 (\u0393\u22a9mYm\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62) (Y trm-m)) wf-\u0393 trm-Ym\n","old_contents":"module ITyping where\n\nopen import Data.Empty\nopen import Data.List\nopen import Data.Nat\nopen import Data.Product\nopen import Data.List.Any as Any\nopen Any.Membership-\u2261\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality\nopen import Relation.Binary.Core\n\n\nopen import Core\nopen import Core-Lemmas\nopen import Typing\nopen import Reduction\n\n\n\ndata IType : Set\ndata IType\u209b : Set\n\n\ndata IType\u209b where\n o : IType\u209b\n _~>_ : (s : IType) -> (t : IType\u209b) -> IType\u209b\n\ndata IType where\n \u2229 : List IType\u209b -> IType\n\n\u03c9 = \u2229 []\n\n\u2229' : IType\u209b -> IType\n\u2229' x = \u2229 (x \u2237 [])\n\n~>-inj-l : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2261 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) -> \u03c4\u2081\u2081 \u2261 \u03c4\u2082\u2081\n~>-inj-l refl = refl\n\n~>-inj-r : \u2200 {\u03c4\u2081\u2081 \u03c4\u2081\u2082 \u03c4\u2082\u2081 \u03c4\u2082\u2082} -> (\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u2261 (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) -> \u03c4\u2081\u2082 \u2261 \u03c4\u2082\u2082\n~>-inj-r refl = refl\n\n\u2229-inj-cons : \u2200 {x y \u03c4\u1d62 \u03c4\u2c7c} -> \u2229 (x \u2237 \u03c4\u1d62) \u2261 \u2229 (y \u2237 \u03c4\u2c7c) -> \u2229 \u03c4\u1d62 \u2261 \u2229 \u03c4\u2c7c\n\u2229-inj-cons refl = refl\n\n\u2229-inj : \u2200 {x y \u03c4\u1d62 \u03c4\u2c7c} -> \u2229 (x \u2237 \u03c4\u1d62) \u2261 \u2229 (y \u2237 \u03c4\u2c7c) -> x \u2261 y\n\u2229-inj refl = refl\n\n\n_\u225fTI_ : Decidable {A = IType} _\u2261_\n_\u225fTI\u209b_ : Decidable {A = IType\u209b} _\u2261_\n\n\u2229 [] \u225fTI \u2229 [] = yes refl\n\u2229 [] \u225fTI \u2229 (x \u2237 \u03c4\u2c7c) = no (\u03bb ())\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 [] = no (\u03bb ())\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (y \u2237 \u03c4\u2c7c) with x \u225fTI\u209b y | (\u2229 \u03c4\u1d62) \u225fTI (\u2229 \u03c4\u2c7c)\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (.x \u2237 .\u03c4\u1d62) | yes refl | yes refl = yes refl\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (.x \u2237 \u03c4\u2c7c) | yes refl | no \u03c4\u1d62\u2260\u03c4\u2c7c = no (\u03bb \u2229x\u2237\u03c4\u1d62\u2261\u2229x\u2237\u03c4\u2c7c \u2192 \u03c4\u1d62\u2260\u03c4\u2c7c (\u2229-inj-cons \u2229x\u2237\u03c4\u1d62\u2261\u2229x\u2237\u03c4\u2c7c))\n\u2229 (x \u2237 \u03c4\u1d62) \u225fTI \u2229 (y \u2237 \u03c4\u2c7c) | no x\u2260y | _ = no (\u03bb \u2229x\u2237\u03c4\u1d62\u2261\u2229y\u2237\u03c4\u2c7c \u2192 x\u2260y (\u2229-inj \u2229x\u2237\u03c4\u1d62\u2261\u2229y\u2237\u03c4\u2c7c))\n\no \u225fTI\u209b o = yes refl\no \u225fTI\u209b (_ ~> _) = no (\u03bb ())\n(_ ~> _) \u225fTI\u209b o = no (\u03bb ())\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI\u209b (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) with \u03c4\u2081\u2081 \u225fTI \u03c4\u2082\u2081 | \u03c4\u2081\u2082 \u225fTI\u209b \u03c4\u2082\u2082\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI\u209b (.\u03c4\u2081\u2081 ~> .\u03c4\u2081\u2082) | yes refl | yes refl = yes refl\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI\u209b (.\u03c4\u2081\u2081 ~> \u03c4\u2082\u2082) | yes refl | no \u03c4\u2081\u2082\u2260\u03c4\u2082\u2082 = no (\u03bb eq \u2192 \u03c4\u2081\u2082\u2260\u03c4\u2082\u2082 (~>-inj-r eq))\n(\u03c4\u2081\u2081 ~> \u03c4\u2081\u2082) \u225fTI\u209b (\u03c4\u2082\u2081 ~> \u03c4\u2082\u2082) | no \u03c4\u2081\u2081\u2260\u03c4\u2082\u2081 | _ = no (\u03bb eq \u2192 \u03c4\u2081\u2081\u2260\u03c4\u2082\u2081 (~>-inj-l eq))\n\n\nICtxt = List (Atom \u00d7 IType)\n\n\ndata Wf-ICtxt : ICtxt -> Set where\n nil : Wf-ICtxt []\n cons : \u2200 {\u0393 x \u03c4} -> (x\u2209 : x \u2209 dom \u0393) -> Wf-ICtxt \u0393 ->\n Wf-ICtxt ((x , \u03c4) \u2237 \u0393)\n\n\ndata _\u2237'_ : IType -> Type -> Set\ndata _\u2237'\u209b_ : IType\u209b -> Type -> Set\n\ndata _\u2237'\u209b_ where\n base : o \u2237'\u209b \u03c3\n arr : \u2200 {\u03b4 \u03c4 A B} -> \u03b4 \u2237' A -> \u03c4 \u2237'\u209b B -> (\u03b4 ~> \u03c4) \u2237'\u209b (A \u27f6 B)\n\ndata _\u2237'_ where\n int : \u2200{\u03c4\u1d62 A} -> (c : \u2200 {\u03c4} -> (\u03c4\u2208\u03c4\u1d62 : \u03c4 \u2208 \u03c4\u1d62) -> \u03c4 \u2237'\u209b A) -> \u2229 \u03c4\u1d62 \u2237' A\n\n\ndata _\u22a9_\u2236_ : ICtxt -> PTerm -> IType\u209b -> Set where\n var : \u2200 {\u0393 x \u03c4} {\u03c4\u1d62 : List IType\u209b} -> (wf-\u0393 : Wf-ICtxt \u0393) -> (\u03c4\u1d62\u2208\u0393 : (x , (\u2229 \u03c4\u1d62)) \u2208 \u0393) -> (\u03c4\u2208\u03c4\u1d62 : \u03c4 \u2208 \u03c4\u1d62) ->\n \u0393 \u22a9 fv x \u2236 \u03c4\n app : \u2200 {\u0393 s t \u03c4\u1d62 \u03c4} -> \u0393 \u22a9 s \u2236 ((\u2229 \u03c4\u1d62) ~> \u03c4) -> Term t -> (\u0393\u22a9t\u2236\u03c4\u1d62 : \u2200 {\u03c4'} -> (\u03c4'\u2208\u03c4\u1d62 : \u03c4' \u2208 \u03c4\u1d62) -> \u0393 \u22a9 t \u2236 \u03c4') ->\n \u0393 \u22a9 (app s t) \u2236 \u03c4\n abs : \u2200 {\u0393 \u03b4 \u03c4} (L : FVars) -> \u2200 {t} ->\n ( cf : \u2200 {x} -> (x\u2209L : x \u2209 L) -> ((x , \u03b4) \u2237 \u0393) \u22a9 t ^' x \u2236 \u03c4 ) -> \u0393 \u22a9 lam t \u2236 (\u03b4 ~> \u03c4)\n Y : \u2200 {\u0393 A \u03c4\u1d62 \u03c4} -> Wf-ICtxt \u0393 -> (\u03c4\u2208\u03c4\u1d62 : \u03c4 \u2208 \u03c4\u1d62) -> (\u03c4\u1d62\u2237 : \u2200 {\u03c4} -> \u03c4 \u2208 \u03c4\u1d62 -> \u03c4 \u2237'\u209b A) ->\n \u0393 \u22a9 Y A \u2236 (\u2229 (Data.List.map (\u03bb \u03c4\u2096 -> (\u2229 \u03c4\u1d62 ~> \u03c4\u2096)) \u03c4\u1d62) ~> \u03c4)\n\n\u22a9-term : \u2200 {\u0393 m \u03c4} -> \u0393 \u22a9 m \u2236 \u03c4 -> Term m\n\u22a9-term (var _ _ _) = var\n\u22a9-term (app \u0393\u22a9m\u2236\u03c4 trm-t c) = app (\u22a9-term \u0393\u22a9m\u2236\u03c4) trm-t\n\u22a9-term (abs L cf) = lam L (\u03bb {x} x\u2209L \u2192 \u22a9-term (cf x\u2209L))\n\u22a9-term (Y x \u03c4\u2208\u03c4\u1d62 x\u2081) = Y\n\n-- cex : [] \u22a9 app (lam (lam (bv 0))) (bv 4) \u2236 (\u2229' o ~> o)\n\n\nwfI-cons : \u2200 {\u0393 x \u03c4} -> Wf-ICtxt ((x , \u03c4) \u2237 \u0393) -> Wf-ICtxt \u0393\nwfI-cons (cons x\u2209 wf-\u0393) = wf-\u0393\n\n\n\u22a9-imp-wf\u0393 : \u2200 {\u0393 m \u03c4} -> \u0393 \u22a9 m \u2005\u2236 \u03c4 -> Wf-ICtxt \u0393\n\u22a9-imp-wf\u0393 (var x\u2081 x\u2082 x\u2083) = x\u2081\n\u22a9-imp-wf\u0393 (app \u0393\u22a2m:\u03c4 trm-t c) = \u22a9-imp-wf\u0393 \u0393\u22a2m:\u03c4\n\u22a9-imp-wf\u0393 (abs L cf) = wfI-cons (\u22a9-imp-wf\u0393 (cf x\u2209))\n where\n x = \u2203fresh L\n\n x\u2209 : x \u2209 L\n x\u2209 = \u2203fresh-spec L\n\n\u22a9-imp-wf\u0393 (Y x \u03c4\u2208\u03c4\u1d62 x\u2081) = x\n\n\nvar-\u22a9-\u2208 : \u2200 {x y \u03c4\u1d62 \u03c4 \u0393} -> ((x , \u2229 \u03c4\u1d62) \u2237 \u0393) \u22a9 fv y \u2236 \u03c4 -> x \u2261 y -> \u03c4 \u2208 \u03c4\u1d62\nvar-\u22a9-\u2208 {x} {_} {\u03c4\u1d62} {\u03c4} (var {x = .x} {\u03c4\u1d62 = \u03c4\u2c7c} (cons x\u2209 wf-\u0393) \u2229\u03c4\u2c7c\u2208x\u2237\u0393 \u03c4\u2208\u03c4\u2c7c) refl with (\u2229 \u03c4\u2c7c) \u225fTI (\u2229 \u03c4\u1d62)\nvar-\u22a9-\u2208 (var (cons x\u2209 wf-\u0393) \u2229\u03c4\u2c7c\u2208x\u2237\u0393 \u03c4\u2208\u03c4\u2c7c) refl | yes refl = \u03c4\u2208\u03c4\u2c7c\nvar-\u22a9-\u2208 (var (cons x\u2209 wf-\u0393) \u2229\u03c4\u2c7c\u2208x\u2237\u0393 \u03c4\u2208\u03c4\u2c7c) refl | no \u2229\u03c4\u2c7c\u2260\u2229\u03c4\u1d62 = \u22a5-elim (\u2209-\u2237 _ _ (\u03bb x \u2192 \u2229\u03c4\u2c7c\u2260\u2229\u03c4\u1d62 (\u00d7-inj-r x)) (\u2209-dom x\u2209) \u2229\u03c4\u2c7c\u2208x\u2237\u0393)\n\n\n-- substitution lemma.\n\nweakening-\u22a9 : \u2200 {\u0393 \u0393' m \u03c4} -> \u0393 \u2286 \u0393' -> Wf-ICtxt \u0393' -> \u0393 \u22a9 m \u2236 \u03c4 -> \u0393' \u22a9 m \u2236 \u03c4\nweakening-\u22a9 \u0393\u2286\u0393' wf-\u0393' (var wf-\u0393 \u2229\u03c4\u1d62\u2208\u0393 \u03c4\u2208\u03c4\u1d62) = var wf-\u0393' (\u0393\u2286\u0393' \u2229\u03c4\u1d62\u2208\u0393) \u03c4\u2208\u03c4\u1d62\nweakening-\u22a9 \u0393\u2286\u0393' wf-\u0393' (app \u0393\u22a9m\u2236\u03c4 x c) = app (weakening-\u22a9 \u0393\u2286\u0393' wf-\u0393' \u0393\u22a9m\u2236\u03c4) x\n (\u03bb {\u03c4'} \u03c4'\u2208\u03c4\u1d62 \u2192 weakening-\u22a9 \u0393\u2286\u0393' wf-\u0393' (c \u03c4'\u2208\u03c4\u1d62))\nweakening-\u22a9 {\u0393} {\u0393'} \u0393\u2286\u0393' wf-\u0393' (abs {_} {\u03c4\u1d62} {\u03c4} L {m} cf) =\n abs (L ++ dom \u0393') (\u03bb {x} x\u2209L++\u0393' \u2192\n weakening-\u22a9 {(x , \u03c4\u1d62) \u2237 \u0393} {(x , \u03c4\u1d62) \u2237 \u0393'} {m ^' x} {\u03c4}\n (cons-\u2286 \u0393\u2286\u0393')\n (cons (\u2209-cons-r L _ x\u2209L++\u0393') wf-\u0393')\n (cf (\u2209-cons-l _ _ x\u2209L++\u0393')))\nweakening-\u22a9 \u0393\u2286\u0393' wf-\u0393' (Y x \u03c4\u2208\u03c4\u1d62 x\u2081) = Y wf-\u0393' \u03c4\u2208\u03c4\u1d62 x\u2081\n\n\nwfI-\u0393-exchange : \u2200 {\u0393 x y \u03c4\u2081 \u03c4\u2082} -> Wf-ICtxt ((x , \u03c4\u2081) \u2237 (y , \u03c4\u2082) \u2237 \u0393) -> Wf-ICtxt ((y , \u03c4\u2082) \u2237 (x , \u03c4\u2081) \u2237 \u0393)\nwfI-\u0393-exchange (cons x\u2209y\u2237\u0393 (cons y\u2209\u0393 wf\u2237\u0393)) =\n cons (\u2209-\u2237 _ _ (\u03bb y\u2261x \u2192 fv-x\u2260y _ _ x\u2209y\u2237\u0393 (\u2261-sym y\u2261x)) y\u2209\u0393) (cons (\u2209-\u2237-elim _ x\u2209y\u2237\u0393) wf\u2237\u0393)\n\n\nexchange-\u22a9 : \u2200 {\u0393 m x y \u03c4\u2081 \u03c4\u2082 \u03b4} -> ((x , \u03c4\u2081) \u2237 (y , \u03c4\u2082) \u2237 \u0393) \u22a9 m \u2236 \u03b4 -> ((y , \u03c4\u2082) \u2237 (x , \u03c4\u2081) \u2237 \u0393) \u22a9 m \u2236 \u03b4\nexchange-\u22a9 {\u0393} {m} {x} {y} {\u03c4\u2081} {\u03c4\u2082} x\u2237y\u2237\u0393\u22a2m\u2236\u03b4 =\n weakening-\u22a9 {(x , \u03c4\u2081) \u2237 (y , \u03c4\u2082) \u2237 \u0393} {(y , \u03c4\u2082) \u2237 (x , \u03c4\u2081) \u2237 \u0393}\n sub (wfI-\u0393-exchange (\u22a9-imp-wf\u0393 x\u2237y\u2237\u0393\u22a2m\u2236\u03b4)) x\u2237y\u2237\u0393\u22a2m\u2236\u03b4\n\n where\n sub : (x , \u03c4\u2081) \u2237 (y , \u03c4\u2082) \u2237 \u0393 \u2286 (y , \u03c4\u2082) \u2237 (x , \u03c4\u2081) \u2237 \u0393\n sub (here px) = there (here px)\n sub (there (here px)) = here px\n sub (there (there \u2208)) = there (there \u2208)\n\n\nsubst-\u22a9 : \u2200 {\u0393 m n \u03c4\u1d62 \u03c4\u2082 x} -> Term m -> Term n -> ((x , \u2229 \u03c4\u1d62) \u2237 \u0393) \u22a9 m \u2236 \u03c4\u2082 -> (\u2200 {\u03c4} -> \u03c4 \u2208 \u03c4\u1d62 -> \u0393 \u22a9 n \u2236 \u03c4) ->\n \u0393 \u22a9 (m [ x ::= n ]) \u2236 \u03c4\u2082\nsubst-\u22a9 {x = x} var trm-n (var {x = y} wf-\u0393 \u2229\u03c4\u1d62\u2208\u0393 \u03c4\u2208\u03c4\u1d62) \u0393\u22a9n\u2236\u03c4\u1d62 with x \u225f y\nsubst-\u22a9 var trm-n (var wf-\u0393 \u2229\u03c4\u1d62\u2208\u0393 \u03c4\u2208\u03c4\u1d62) \u0393\u22a9n\u2236\u03c4\u1d62 | yes refl =\n \u0393\u22a9n\u2236\u03c4\u1d62 (var-\u22a9-\u2208 (var wf-\u0393 \u2229\u03c4\u1d62\u2208\u0393 \u03c4\u2208\u03c4\u1d62) refl)\nsubst-\u22a9 {\u0393} {x = x} var trm-n (var {x = y} wf-\u0393 \u2229\u03c4\u1d62\u2208\u0393 \u03c4\u2208\u03c4\u1d62) \u0393\u22a9n\u2236\u03c4\u1d62 | no x\u2260y =\n var {\u0393} {y} (wfI-cons wf-\u0393) (\u2208-\u2237-elim _ _ (\u03bb x\u2229\u03c4\u1d62\u2261y\u2229\u03c4\u2c7c \u2192 x\u2260y (\u00d7-inj-l x\u2229\u03c4\u1d62\u2261y\u2229\u03c4\u2c7c)) \u2229\u03c4\u1d62\u2208\u0393) \u03c4\u2208\u03c4\u1d62\nsubst-\u22a9 {\u0393} {_} {n} {\u03c4\u1d62} {_} {x} (lam L cf) trm-n (abs {_} {\u03c4\u1d62'} {\u03c4\u2082} L' {m} cf') \u0393\u22a9n\u2236\u03c4\u1d62 = abs (x \u2237 L ++ L' ++ dom \u0393) body\n where\n body : \u2200 {x' : \u2115} \u2192 x' \u2209 x \u2237 L ++ L' ++ dom \u0393 \u2192 ((x' , \u03c4\u1d62') \u2237 \u0393) \u22a9 (m [ x ::= n ]) ^' x' \u2236 \u03c4\u2082\n body {x'} x'\u2209 rewrite\n subst-open-var x' x n m (fv-x\u2260y _ _ x'\u2209) trm-n =\n subst-\u22a9 {(x' , \u03c4\u1d62') \u2237 \u0393} {m ^' x'} {n} {\u03c4\u1d62} {\u03c4\u2082}\n (cf (\u2209-\u2237-elim _ (\u2209-cons-l _ _ x'\u2209)))\n trm-n\n (exchange-\u22a9 (cf' (\u2209-cons-l _ _ (\u2209-cons-r L _ (\u2209-\u2237-elim _ x'\u2209)))))\n (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 weakening-\u22a9 there (cons (\u2209-cons-r L' _ (\u2209-cons-r L _ (\u2209-\u2237-elim _ x'\u2209))) (\u22a9-imp-wf\u0393 (\u0393\u22a9n\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62))) (\u0393\u22a9n\u2236\u03c4\u1d62 \u03c4\u2208\u03c4\u1d62))\n\nsubst-\u22a9 (app trm-m trm-m\u2081) trm-n (app x\u2237\u0393\u22a9m\u2236\u03c4\u2082 trm-t c) \u0393\u22a9n\u2236\u03c4\u1d62 =\n app (subst-\u22a9 trm-m trm-n x\u2237\u0393\u22a9m\u2236\u03c4\u2082 \u0393\u22a9n\u2236\u03c4\u1d62) (subst-Term trm-t trm-n) (\u03bb {\u03c4'} \u03c4'\u2208\u03c4\u1d62 \u2192 subst-\u22a9 trm-m\u2081 trm-n (c \u03c4'\u2208\u03c4\u1d62) \u0393\u22a9n\u2236\u03c4\u1d62)\nsubst-\u22a9 Y trm-n (Y (cons x\u2209 wf-\u0393) \u03c4\u2208\u03c4\u1d62 x\u2082) \u0393\u22a9n\u2236\u03c4\u1d62 = Y wf-\u0393 \u03c4\u2208\u03c4\u1d62 x\u2082\n\n\n-- subject Reduction\n^-\u22a9 : \u2200 {\u0393 L m n \u03c4\u1d62 \u03c4\u2082} -> Term n -> ( cf : \u2200 {x} -> (x\u2209L : x \u2209 L) -> ((x , \u2229 \u03c4\u1d62) \u2237 \u0393) \u22a9 m ^' x \u2236 \u03c4\u2082 ) ->\n (\u2200 {\u03c4} -> \u03c4 \u2208 \u03c4\u1d62 -> \u0393 \u22a9 n \u2236 \u03c4) -> \u0393 \u22a9 m ^ n \u2236 \u03c4\u2082\n^-\u22a9 {\u0393} {L} {m} {n} {\u03c4\u1d62} {\u03c4} trm-n cf \u0393\u22a9n\u2236\u03c4\u1d62 = body\n where\n x = \u2203fresh (L ++ FV m)\n\n x\u2209 : x \u2209 (L ++ FV m)\n x\u2209 = \u2203fresh-spec (L ++ FV m)\n\n body : \u0393 \u22a9 m ^ n \u2236 \u03c4\n body rewrite\n subst-intro x n m (\u2209-cons-r L _ x\u2209) trm-n =\n subst-\u22a9 (\u22a9-term (cf (\u2209-cons-l _ _ x\u2209))) trm-n (cf (\u2209-cons-l _ _ x\u2209)) \u0393\u22a9n\u2236\u03c4\u1d62\n\n\n->\u03b2-\u22a9 : \u2200 {\u0393 m m' \u03c4} -> \u0393 \u22a9 m \u2236 \u03c4 -> m ->\u03b2 m' -> \u0393 \u22a9 m' \u2236 \u03c4\n->\u03b2-\u22a9 (app \u0393\u22a9m\u2236\u03c4 x c) (redL x\u2081 m->\u03b2m') = app (->\u03b2-\u22a9 \u0393\u22a9m\u2236\u03c4 m->\u03b2m') x\u2081 c\n->\u03b2-\u22a9 (app \u0393\u22a9m\u2236\u03c4 trm-t c) (redR x\u2081 m->\u03b2m') = app \u0393\u22a9m\u2236\u03c4 (->\u03b2-Term-r m->\u03b2m') (\u03bb \u03c4'\u2208\u03c4\u1d62 \u2192 ->\u03b2-\u22a9 (c \u03c4'\u2208\u03c4\u1d62) m->\u03b2m')\n->\u03b2-\u22a9 (abs L cf) (abs L\u2081 cf\u2081) = abs (L ++ L\u2081) (\u03bb {x} x\u2209L \u2192 ->\u03b2-\u22a9 (cf (\u2209-cons-l _ _ x\u2209L)) (cf\u2081 (\u2209-cons-r L _ x\u2209L)))\n->\u03b2-\u22a9 (app (abs L {m} cf) x \u0393\u22a2n\u2236\u03c4\u1d62) (beta x\u2081 trm-n) = ^-\u22a9 {m = m} trm-n cf \u0393\u22a2n\u2236\u03c4\u1d62\n->\u03b2-\u22a9 (app (Y {\u03c4 = \u03c4} wf-\u0393 \u03c4\u2208\u03c4\u1d62 \u03c4\u2237'\u209b\u03c3) _ \u0393\u22a9m\u2236\u03c4\u1d62) (Y trm-m) =\n app\n (\u0393\u22a9m\u2236\u03c4\u1d62 (map-\u2208 (\u03c4 , \u03c4\u2208\u03c4\u1d62 , refl)))\n (app Y trm-m)\n (\u03bb \u03c4'\u2208\u03c4\u1d62 \u2192 app (Y wf-\u0393 \u03c4'\u2208\u03c4\u1d62 (\u03bb \u03c4\u2208\u03c4\u1d62 \u2192 \u03c4\u2237'\u209b\u03c3 \u03c4\u2208\u03c4\u1d62)) trm-m (\u03bb \u03c4'\u2208\u03c4\u1d62\u2081 \u2192 \u0393\u22a9m\u2236\u03c4\u1d62 \u03c4'\u2208\u03c4\u1d62\u2081))\n\n-- subject expansion\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"5826fd422a35e593bc840ca76dea1d6b1e32dc6d","subject":"updating all","message":"updating all\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"all.agda","new_file":"all.agda","new_contents":"open import List\nopen import Nat\nopen import Prelude\n\nopen import contexts\nopen import core\n\nopen import focus-formation\nopen import finality\n\nopen import lemmas-consistency\nopen import lemmas-matching\nopen import synth-unicity\nopen import htype-decidable\n\nopen import correspondence\nopen import expandability\nopen import expansion-unicity\nopen import type-assignment-unicity\n-- open import typed-expansion\n\nopen import canonical-value-forms\nopen import canonical-boxed-forms\nopen import canonical-indeterminate-forms\n\n-- open import progress-checks\n-- open import progress\n-- open import preservation\n\nopen import complete-preservation\n-- open import complete-progress\n","old_contents":"open import List\nopen import Nat\nopen import Prelude\n\nopen import contexts\nopen import core\n\nopen import focus-formation\nopen import finality\n\nopen import lemmas-consistency\nopen import lemmas-matching\nopen import synth-unicity\nopen import htype-decidable\n\nopen import correspondence\nopen import expandability\nopen import expansion-unicity\nopen import type-assignment-unicity\n\nopen import canonical-value-forms\nopen import canonical-boxed-forms\n-- open import canonical-indeterminate-forms\n\n-- open import progress-checks\n\n-- open import preservation\n-- open import progress\nopen import complete-preservation\n-- open import complete-progress\n\n-- open import typed-expansion\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"14dcf28eeffdcf7d1dbadd450170fddf0972e563","subject":"Category.Monad","message":"Category.Monad\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Category\/Monad\/NP.agda","new_file":"lib\/Category\/Monad\/NP.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Category.Functor.NP\nopen import Type\nopen import Function\nopen import Level.NP\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Vec using (Vec; []; _\u2237_)\n\nmodule Category.Monad.NP {\u2113} where\n\nopen import Category.Monad public\n\nrecord IsMonadic {M : Set \u2113 \u2192 Set \u2113} (rawMonad : RawMonad M) : \u2605_ (\u209b \u2113) where\n open RawMonad rawMonad\n\n field\n return->>= : \u2200 {A B} (f : A \u2192 M B) x \u2192 (return x >>= f) \u2261 f x\n >>=-return : \u2200 {A} (m : M A) \u2192 (m >>= return) \u2261 m\n >>=-assoc : \u2200 {A B C} (mx : M A) (my : A \u2192 M B) (k : B \u2192 M C)\n \u2192 (mx >>= \u03bb x \u2192 my x >>= k) \u2261 (mx >>= my >>= k)\n\n >>-assoc : \u2200 {A B C} (mx : M A) (my : M B) (mz : M C)\n \u2192 (mx >> (my >> mz)) \u2261 ((mx >> my) >> mz)\n >>-assoc mx my mz = >>=-assoc mx (const my) (const mz)\n\n liftM : \u2200 {A B} \u2192 (A \u2192 B) \u2192 M A \u2192 M B\n liftM f x = x >>= return \u2218 f\n\n <$>-liftM : \u2200 {A B} (f : A \u2192 B) x \u2192 f <$> x \u2261 liftM f x\n <$>-liftM f x = return->>= (\u03bb g \u2192 liftM g x) f\n\n liftM-id : \u2200 {A} (mx : M A) \u2192 liftM id mx \u2261 mx\n liftM-id = >>=-return\n\n <$>-id : \u2200 {A} (mx : M A) \u2192 id <$> mx \u2261 mx\n <$>-id mx = trans (<$>-liftM id mx) (liftM-id mx)\n\n {- requires function extensionality\n cong-return-\u2218 : \u2200 {A B : \u2605_ \u2113} {f g : A \u2192 B} \u2192 f \u2261 g \u2192 return \u2218 f \u2261 return \u2218 g\n cong-return-\u2218 = {!!}\n\n liftM-\u2218 : \u2200 {A B C} (f : B \u2192 C) (g : A \u2192 B) x \u2192 liftM (f \u2218 g) x \u2261 liftM f (liftM g x)\n liftM-\u2218 f g x = trans (cong (_>>=_ x) {!cong-return-\u2218 {!return->>= (g ?)!}!}) (>>=-assoc x (return \u2218 g) (return \u2218 f))\n\n functor : Functor rawFunctor\n functor = record { <$>-id = <$>-id; <$>-\u2218 = {!!} }\n -}\n\n vmapM : \u2200 {n a}{A : Set a} {B : Set \u2113} \u2192 (A \u2192 M B) \u2192 Vec A n \u2192 M (Vec B n)\n vmapM f [] = return []\n vmapM f (x \u2237 xs) = _\u2237_ <$> f x \u229b vmapM f xs\n\nrecord Monad (M : Set \u2113 \u2192 Set \u2113) : \u2605_ (\u209b \u2113) where\n constructor _,_\n field\n rawMonad : RawMonad M\n isMonadic : IsMonadic rawMonad\n\n open RawMonad rawMonad public\n open IsMonadic isMonadic public\n","old_contents":"","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"93d1536fcf0ff29c501257842452ef0bb16955c2","subject":"renamed variables","message":"renamed variables\n","repos":"haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc,haroldcarr\/learn-haskell-coq-ml-etc","old_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/wouter-swierstra-predicate-transformers-6fe8f13\/PredicateTransformers.agda","new_file":"agda\/paper\/Dijkstra-monad\/2019-03-Wouter-Swierstra_A_Predicate_Transformer_for_Effects\/wouter-swierstra-predicate-transformers-6fe8f13\/PredicateTransformers.agda","new_contents":"module PredicateTransformers where\n\nopen import Prelude hiding (map; all)\nopen import Level hiding (lift)\n\n------------------------------------------------------------------------------\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\n------------------------------------------------------------------------------\nABSTRACT\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nrefinement type https:\/\/en.wikipedia.org\/wiki\/Refinement_(computing)\n- type with a predicate which must hold for any element of the refined type\n- can express\n - preconditions when used as function args\n - postconditions when used as return types\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to use this refinement relation to\n - show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\nmodule Free where\n -- C : type of commands\n -- Free C R : returns an 'a' or issues command c : C\n -- For each c : C, there is a set of responses R c\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\"\n -- - function f : a -> b, and\n -- - desired postcondition on function\u2019s output, b -> Set\n -- to weakest precondition a -> Set on function\u2019s input that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - fix via making f dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n -- refinement relation between PTs\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation\n - to relate PT semantics between programs and specifications\n - to show a program satisfies its specification; or\n - to show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n the refinement relation corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n data C : Set where\n Abort : C -- no continuation\n\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n exv : Expr\n exv = Val 3\n exd : Expr\n exd = Div (Val 3) (Val 3)\n\n -- semantics specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er nl nr}\n -> el \u21d3 nl\n -> er \u21d3 (Succ nr) -- divisor is non-zero\n -> Div el er \u21d3 (nl div (Succ nr))\n\n exb0 : Val 0 \u21d3 0\n exb0 = \u21d3Base\n\n exb3 : Val 3 \u21d3 3\n exb3 = \u21d3Base\n\n exs331 : Div (Val 3) (Val 3) \u21d3 1\n exs331 = \u21d3Step \u21d3Base \u21d3Base\n\n exs931 : Div (Val 9) (Val 3) \u21d3 3\n exs931 = \u21d3Step \u21d3Base \u21d3Base\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val x \u27e7 = return x\n \u27e6 Div el er \u27e7 = \u27e6 el \u27e7 >>= \\v1 ->\n \u27e6 er \u27e7 >>= \\v2 ->\n v1 \u00f7 v2\n\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n evv' : evv \u2261 Pure 3\n evv' = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n evd' : evd \u2261 Pure 1\n evd' = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n evd0' : evd0 \u2261 Step Abort (\u03bb ())\n evd0' = refl\n\n {-\n relate the two definitions:\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - \u21d3 relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n -}\n\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba\n mustPT _ _ (Step Abort _) = \u22a5\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n To call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results : Partial b -> Set\n Done via proposition 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so case Abort returns empty type\n\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter\n via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n exwpp : Expr -> Set\n exwpp = wpPartial \u27e6_\u27e7 _\u21d3_\n exwpp' : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n exwpp' = refl\n\n wppv1 : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n wppv1 = \u21d3Base\n wppd : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n wppd = \u21d3Step \u21d3Base \u21d3Base\n\n xxx : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb v1 -> Pure 3 >>= _\u00f7_ v1))\n xxx = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx' : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n xxx' = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n {-\n resulting in a predicate on expressions\n\n for all expressions satisfying this predicate,\n the monadic interpreter and the relational specification, _\u21d3_,\n must agree on the result of evaluation\n\n What does this say about correctness of interpreter?\n To understand the predicate better, define this predicate on expressions:\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val x) = \u22a4\n SafeDiv (Div el er) = (er \u21d3 Zero -> \u22a5) \u2227 SafeDiv el \u2227 SafeDiv er\n\n exsdv : SafeDiv (Val 3) \u2261 \u22a4\n exsdv = refl\n exsdd : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsdd = refl\n\n exsdx : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val Zero \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsdx = refl\n\n {-\n Expect : any expr e for which SafeDiv e holds\n can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n correct (Val x) h = \u21d3Base\n correct (Div el er) (ernz , (sdel , sder)) with \u27e6 el \u27e7 | \u27e6 er \u27e7 | correct el sdel | correct er sder\n correct (Div el er) (ernz , (sdel , sder)) | Pure v1 | Pure Zero | el\u21d3v1 | er\u21d3Z = magic (ernz er\u21d3Z)\n correct (Div el er) (ernz , (sdel , sder)) | Pure v1 | Pure (Succ v2) | el\u21d3v1 | er\u21d3Sv2 = \u21d3Step el\u21d3v1 er\u21d3Sv2\n correct (Div el er) (ernz , (sdel , sder)) | Pure v1 | Step Abort \u22a5\u2192FCRN | el\u21d3v1 | ()\n correct (Div el er) (ernz , (sdel , sder)) | Step Abort _ | _ | () | _\n\n {-\n Instead of manually defining SafeDiv,\n define more general predicate characterising the domain of a partial function:\n -}\n\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a)\n -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n -- can show that the two semantics agree precisely on the domain of the interpreter:\n\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on the argument expression\n\n sound (Val x) h = \u21d3Base\n sound (Div el er) h with \u27e6 el \u27e7 | \u27e6 er \u27e7 | sound el | sound er\n sound (Div el er) () | Pure v1 | Pure Zero | ih1 | ih2\n sound (Div el er) h | Pure v1 | Pure (Succ v2) | ih1 | ih2 = \u21d3Step (ih1 tt) (ih2 tt)\n sound (Div el er) () | Pure x | Step Abort x\u2081 | ih1 | ih2\n sound (Div el er) () | Step Abort x | v2 | ih1 | ih2\n\n inDom : {v : Nat} -> (e : Expr) -> \u27e6 e \u27e7 == Pure v -> dom \u27e6_\u27e7 e\n inDom (Val x) h = tt\n inDom (Div el er) h with \u27e6 el \u27e7 | \u27e6 er \u27e7\n inDom (Div el er) () | Pure v1 | Pure Zero\n inDom (Div el er) h | Pure v1 | Pure (Succ v2) = tt\n inDom (Div el er) () | Pure _ | Step Abort _\n inDom (Div el er) () | Step Abort _ | _\n\n aux : (e : Expr) (v : Nat) -> \u27e6 e \u27e7 \u2261 Pure v -> e \u21d3 v\n aux e v eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {el er : Expr} -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er) -> wpPartial \u27e6_\u27e7 _\u21d3_ el\n wpPartial1 {el} {er} h with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7\n wpPartial1 {el} {er} () | Pure x | eq | Pure Zero\n wpPartial1 {el} {er} h | Pure x | [[[ eq ]]] | Pure (Succ y) = aux el x eq\n wpPartial1 {el} {er} () | Pure x | eq | Step Abort x\u2081\n wpPartial1 {el} {er} () | Step Abort x | eq | ver\n\n wpPartial2 : {el er : Expr} -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div el er) -> wpPartial \u27e6_\u27e7 _\u21d3_ er\n wpPartial2 {el} {er} h with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n wpPartial2 {el} {er} h | Pure x | [[[ eqx ]]] | Pure y | [[[ eqy ]]] = aux er y eqy\n wpPartial2 {el} {er} () | Pure x | [[[ eq ]]] | Step Abort x\u2081 | eq2\n wpPartial2 {_} {_} () | Step Abort x | eq1 | ser | eq2\n\n complete (Val x) h = tt\n complete (Div el er) h\n with \u27e6 el \u27e7 | inspect \u27e6_\u27e7 el | \u27e6 er \u27e7 | inspect \u27e6_\u27e7 er\n | complete el (wpPartial1 {el} {er} h)\n | complete er (wpPartial2 {el} {er} h)\n complete (Div el er) h | Pure x | [[[ eqx ]]] | Pure Zero | [[[ eqy ]]] | p1 | p2\n rewrite eqx | eqy = magic h\n complete (Div el er) h | Pure x | [[[ eqx ]]] | Pure (Succ y) | [[[ eqy ]]] | p1 | p2 = tt\n complete (Div el er) h | Pure x | eq1 | Step Abort x\u2081 | eq2 | p1 | ()\n complete (Div el er) h | Step Abort x | eq1 | ser | eq2 | () | p2\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement to relate Kleisli morphisms,\n and to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n add top two elements; can fail fail if stack has too few elements\n\n below shows how to prove the definition meets its specification\n\n Define specification in terms of a pre\/post condition.\n -}\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n {-\n [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n for non-dependent examples (e.g., type b does not depend on x : a)\n -}\n\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function\n\n -- specification for addition function : describes the desired postcondition\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n\n {-\n How to relate this specification to an implementation?\n Note: 'wpPartial' assigns predicate transformer semantics to functions\n - but do not yet have a corresponding predicate transform semantics for specifications.\n wpSpec function does that:\n -}\n\n -- Given a specification, Spec a b\n -- computes the weakest precondition that satisfies an arbitrary postcondition P\n -- i.e., the spec\u2019s precondition should hold and its postcondition must imply P.\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\x -> (pre x) \u2227 (post x \u2286 P x)\n\n -- Can now formulate program\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs =\n pop xs >>= \\{(x1 , xs) ->\n pop xs >>= \\{(x2 , xs) ->\n return ((x1 + x2) :: xs)}}\n\n -- that refines the specification given by addSpec:\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd P Nil (() , _)\n correctnessAdd P (x :: Nil) (Data.Nat.s\u2264s () , _)\n correctnessAdd P (x :: y :: xs) (_ , H) = H (x + y :: xs) AddStep\n\n {-\n This example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition,\n - to its implementation.\n\n Compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n get : State s\n get = Step Get return\n\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n statePT : forall {l l'} -> {b : Set l} -> (b \u00d7 s -> Set l') -> State b -> (s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> (a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> (a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s') = (flatten t' == (seq (s) (size t))) \u2227 (s + size t == s')\n\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n correctnessRelabel : forall {a : Set} -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set} -> (c : State a) (f : a -> State b) ->\n \u2200 i P -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification,\n -- by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence,\n -- specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b} (mx : State a) (f : a -> State b) P i ->\n statePT (wpState f \\_ -> P) mx i -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b} (mx : State a) (f : a -> State b) spec ->\n \u2200 P i -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i) ->\n Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P)) ->\n statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s} -> SpecK {zero} (a \u00d7 s) (b \u00d7 s) -> a -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a) (trans\n (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr ->\n Pair (fl \u2261 seq s sl) (s + sl \u2261 s') ->\n Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'') ->\n Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s -> wpSpec relabelSpec P (t , s) -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set} ->\n ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set} -> (c : Free C R a) (f : a -> Free C R b) ->\n \u2200 P -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P =\n cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R} -> (a -> Free C R b) -> (b -> Free C R c) -> a -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set} -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c) ->\n wpCR f1 \u2291 wpCR f2 ->\n wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","old_contents":"module PredicateTransformers where\n\nopen import Prelude hiding (map; all)\nopen import Level hiding (lift)\n\n------------------------------------------------------------------------------\n\n{-\nhttps:\/\/webspace.science.uu.nl\/~swier004\/publications\/2019-icfp-submission-a.pdf\n\nA predicate transformer semantics for effects\n\nWOUTER SWIERSTRA and TIM BAANEN, Universiteit Utrecht\n\n------------------------------------------------------------------------------\nABSTRACT\n\nreasoning about effectful code harder than pure code\n\npredicate transformer (PT) semantics gives a refinement relation that can be used to\n- relate a program to its specification, or\n- calculate effectful programs that are correct by construction\n\nrefinement type https:\/\/en.wikipedia.org\/wiki\/Refinement_(computing)\n- type with a predicate which must hold for any element of the refined type\n- can express\n - preconditions when used as function args\n - postconditions when used as return types\n\n------------------------------------------------------------------------------\n1 INTRODUCTION\n\nkey techniques\n- syntax of effectful computations represented as free monads\n - assigning meaning to these monads gives meaning to the syntactic ops each effect provides\n- paper shows how to assign PT semantics to computations arising from Kleisli arrows on free monads\n - enables computing the weakest precondition associated with a given postcondition\n- using weakest precondition semantics\n - define refinement on computations\n - show how to use this refinement relation to\n - show a program satisfies its specification, or\n - calculate a program from its specification.\n- show how programs and specifications may be mixed,\n enabling verified programs to be calculated from their specification one step at a time\n\n------------------------------------------------------------------------------\n2 BACKGROUND\n\n--------------------------------------------------\nFree monads\n-}\n\nmodule Free where\n -- C : type of commands\n -- Free C R : returns an 'a' or issues command c : C\n -- For each c : C, there is a set of responses R c\n data Free {l : Level} (C : Set) (R : C -> Set) (a : Set l) : Set l where\n Pure : a -> Free C R a\n -- 2nd arg of Step is continuation : how to proceed after receiving response R c\n Step : (c : C) -> (R c -> Free C R a) -> Free C R a\n\n -- show that 'Free' is a monad:\n\n map : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> (a -> b) -> Free C R a\n -> Free C R b\n map f (Pure a) = Pure (f a)\n map f (Step c Rc\u2192F) = Step c (\\Rc -> map f (Rc\u2192F Rc))\n\n return : forall {l C R} -> {a : Set l}\n -> a -> Free C R a\n return = Pure\n\n _>>=_ : forall {l l' C R} -> {a : Set l} -> {b : Set l'}\n -> Free C R a -> (a -> Free C R b)\n -> Free C R b\n Pure a >>= a\u2192F = a\u2192F a\n Step c Rc\u2192F >>= a\u2192F = Step c (\\Rc -> Rc\u2192F Rc >>= a\u2192F)\n infixr 20 _>>=_\n\n _>>_ : forall {l l' C R} {a : Set l} {b : Set l'}\n -> Free C R a -> Free C R b\n -> Free C R b\n c1 >> c2 = c1 >>= \\_ -> c2\n\n {-\n different effects choose C and R differently, depending on their ops\n\n --------------------------------------------------\n Weakest precondition (WP) semantics\n\n associating WP semantics with imperative programs dates to\n Dijkstra\u2019s Guarded Command Language [1975]\n\n ways to specify behaviour of function f : a -> b\n - reference implementation\n - define a relation R : a -> b -> Set\n - write contracts and test cases\n - PT semantics\n\n values of type a -> Set are called \"predicate on type a\"\n\n PTs are functions between predicates\n e.g., weakest precondition:\n -}\n\n -- \"maps\"\n -- - function f : a -> b, and\n -- - desired postcondition on function\u2019s output, b -> Set\n -- to weakest precondition a -> Set on function\u2019s input that ensures postcondition satisfied\n --\n -- non-dependent version\n -- note: definition IS reverse function composition\n wp0 : forall {a : Set} {b : Set}\n -> (a -> b)\n -> (b -> Set)\n -> (a -> Set)\n wp0 a\u2192b b\u2192Set = \\a -> b\u2192Set (a\u2192b a)\n\n {-\n wp0 semantics\n - no way to specify that output is related to input\n - fix via making f dependent:\n -}\n\n -- dependent version\n wp : forall {l l' l''}\n -> {a : Set l} {b : a -> Set l'}\n -> ((x : a) -> b x)\n -> ((x : a) -> b x -> Set l'')\n -> ( a -> Set l'')\n wp a\u2192ba a\u2192ba\u2192Set = \\a -> a\u2192ba\u2192Set a (a\u2192ba a)\n\n -- shorthand for working with predicates and predicates transformers\n _\u2286_ : forall {l'} -> {a : Set}\n -> (a -> Set l')\n -> (a -> Set l')\n -> Set l'\n P \u2286 Q = \u2200 a -> P a -> Q a\n\n -- refinement relation between PTs\n _\u2291_ : {a : Set} {b : a -> Set}\n -> (pt1 pt2 : ((x : a) -> b x -> Set) -> (a -> Set))\n -> Set\u2081\n pt1 \u2291 pt2 = forall P -> pt1 P \u2286 pt2 P\n\n \u2291-trans : {a : Set} {b : a -> Set} {P Q R : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 Q -> Q \u2291 R\n -> P \u2291 R\n \u2291-trans P\u2291Q Q\u2291R a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = Q\u2291R a\u2192ba\u2192Set a (P\u2291Q a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a)\n\n \u2291-refl : {a : Set} {b : a -> Set} {P : ((x : a) -> b x -> Set) -> (a -> Set)}\n -> P \u2291 P\n \u2291-refl a\u2192ba\u2192Set a P_a\u2192ba\u2192Set_a = P_a\u2192ba\u2192Set_a\n\n \u2291-eq : {a b : Set}\n -> (f g : a -> b)\n -> wp f \u2291 wp g -> (x : a)\n -> f x == g x\n \u2291-eq f _ wpf\u2291wpg a = wpf\u2291wpg (\\_ b -> f a == b) a refl\n\n eq-\u2291 : {a b : Set}\n -> (f g : a -> b)\n -> ((x : a) -> f x == g x)\n -> wp f \u2291 wp g\n eq-\u2291 f g eq a\u2192b\u2192Set a a\u2192b\u2192Set_a_b with f a | g a | eq a\n ... | _ | _ | refl = a\u2192b\u2192Set_a_b\n\n {-\n use refinement relation\n - to relate PT semantics between programs and specifications\n - to show a program satisfies its specification; or\n - to show that one program is somehow \u2018better\u2019 than another,\n where \u2018better\u2019 is defined by choice of PT semantics\n\n in pure setting, this refinement relation is not interesting:\n the refinement relation corresponds to extensional equality between functions:\n\n lemma follows from the \u2018Leibniz rule\u2019 for equality in intensional type theory:\n refinement : \u2200 (f g : a -> b) -> (wp f \u2291 wp g) \u2194 (\u2200 x -> f x \u2261 g x)\n\n this paper defines PT semantics for Kleisli arrows of form\n\n a -> Free C R b\n\n could use 'wp' to assign semantics to these computations directly,\n but typically not interested in syntactic equality between free monads\n\n rather want to study semantics of effectful programs they represent\n\n to define a PT semantics for effects\n define a function with form:\n\n -- how to lift a predicate on 'a' over effectful computation returning 'a'\n pt : (a -> Set) -> Free C R a -> Set\n\n 'pt' def depends on semantics desired for a particulr free monad\n\n Crucially, choice of\n - pt and\n - weakest precondition semantics : wp\n together give a way to assign weakest precondition semantics\n to Kleisli arrows representing effectful computations\n -}\n\n------------------------------------------------------------------------------\n\nmodule Maybe where\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n open import Data.Nat.DivMod using (_div_)\n open import Data.Nat.Properties using (*-zero\u02b3)\n open Free\n\n {-\n ------------------------------------------------------------------------------\n 3 PARTIALITY\n\n Partial computations : i.e., 'Maybe'\n\n make choices for commands C and responses R\n -}\n\n data C : Set where\n Abort : C -- no continuation\n\n R : C -> Set\n R Abort = \u22a5 -- since C has no continuation, valid responses is empty\n\n Partial : Set -> Set\n Partial = Free C R\n\n -- smart constructor for failure\n -- responses to Abort command are empty\n -- use emptiness to discharge continuation in 2nd arg of Step\n abort : forall {a} -> Partial a\n abort = Step Abort (\\())\n\n {-\n computation of type 'Partial a' will either\n - return a value of type 'a' or\n - fail, issuing abort command\n\n --------------------------------------------------\n Example: division\n\n expression language, closed under division and natural numbers:\n -}\n\n data Expr : Set where\n Val : Nat -> Expr\n Div : Expr -> Expr -> Expr\n\n exv : Expr\n exv = Val 3\n exd : Expr\n exd = Div (Val 3) (Val 3)\n\n -- semantics specified using inductively defined RELATION:\n -- requires divisor to evaluate to non-zero : rules out bad results\n data _\u21d3_ : Expr -> Nat -> Set where\n \u21d3Base : forall {n}\n -> Val n \u21d3 n\n \u21d3Step : forall {el er n1 n2}\n -> el \u21d3 n1\n -> er \u21d3 (Succ n2) -- divisor is non-zero\n -> Div el er \u21d3 (n1 div (Succ n2))\n\n exb0 : Val 0 \u21d3 0\n exb0 = \u21d3Base\n\n exb3 : Val 3 \u21d3 3\n exb3 = \u21d3Base\n\n exs331 : Div (Val 3) (Val 3) \u21d3 1\n exs331 = \u21d3Step \u21d3Base \u21d3Base\n\n exs931 : Div (Val 9) (Val 3) \u21d3 3\n exs931 = \u21d3Step \u21d3Base \u21d3Base\n\n -- semantics can also be specified by an INTERPRETER\n -- monadic INTERPRETER, using Partial to handle division-by-zero\n\n -- div operation used by \u27e6_\u27e7 interpreter\n _\u00f7_ : Nat -> Nat -> Partial Nat\n n \u00f7 Zero = abort\n n \u00f7 (Succ k) = return (n div (Succ k))\n\n \u27e6_\u27e7 : Expr -> Partial Nat\n \u27e6 Val x \u27e7 = return x\n \u27e6 Div e1 e2 \u27e7 = \u27e6 e1 \u27e7 >>= \\v1 ->\n \u27e6 e2 \u27e7 >>= \\v2 ->\n v1 \u00f7 v2\n\n evv : Free C R Nat\n evv = \u27e6 Val 3 \u27e7\n evv' : evv \u2261 Pure 3\n evv' = refl\n\n evd : Free C R Nat\n evd = \u27e6 Div (Val 3) (Val 3) \u27e7\n evd' : evd \u2261 Pure 1\n evd' = refl\n\n evd0 : Free C R Nat\n evd0 = \u27e6 Div (Val 3) (Val 0) \u27e7\n evd0' : evd0 \u2261 Step Abort (\u03bb ())\n evd0' = refl\n\n {-\n relate the two definitions:\n - stdlib 'div' requires implicit proof that divisor is non-zero\n - \u21d3 relation generates via pattern matching\n - _\u00f7_ does explicit check\n - interpreter uses _\u00f7_\n - fails explicitly with abort when divisor is zero\n\n Assign weakest precondition semantics to Kleisli arrows of the form\n\n a -> Partial b\n -}\n\n mustPT : forall {a : Set} -> {b : a -> Set}\n -> ((x : a) -> b x -> Set)\n -> (x : a)\n -> Partial (b x)\n -> Set\n mustPT a\u2192ba\u2192Set a (Pure ba) = a\u2192ba\u2192Set a ba\n mustPT _ _ (Step Abort _) = \u22a5\n\n wpPartial : {a : Set} -> {b : a -> Set}\n -> ((x : a) -> Partial (b x))\n -> ((x : a) -> b x -> Set)\n -> ( a -> Set)\n wpPartial a\u2192partialBa a\u2192ba\u2192Set = wp a\u2192partialBa (mustPT a\u2192ba\u2192Set)\n\n {-\n To call 'wp', must show how to transform\n - predicate P : b -> Set\n - to a predicate on partial results : Partial b -> Set\n Done via proposition 'mustPT P c'\n - holds when computation c of type Partial b successfully returns a 'b' that satisfies P\n\n particular PT semantics of partial computations determined by definition of 'mustPT'\n here: rule out failure entirely\n - so case Abort returns empty type\n\n Given this PT semantics for Kleisli arrows in general,\n can now study semantics of above monadic interpreter\n via passing\n - interpreter : \u27e6_\u27e7\n - desired postcondition : _\u21d3_\n as arguments to wpPartial:\n -}\n\n exwpp : Expr -> Set\n exwpp = wpPartial \u27e6_\u27e7 _\u21d3_\n exwpp' : wpPartial \u27e6_\u27e7 _\u21d3_ \u2261 \u03bb expr -> mustPT _\u21d3_ expr \u27e6 expr \u27e7\n exwpp' = refl\n\n wppv1 : wpPartial \u27e6_\u27e7 _\u21d3_ (Val 1)\n wppv1 = \u21d3Base\n wppd : wpPartial \u27e6_\u27e7 _\u21d3_ (Div (Val 1) (Val 1))\n wppd = \u21d3Step \u21d3Base \u21d3Base\n\n xxx : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (Pure 3 >>= (\u03bb v1 -> Pure 3 >>= _\u00f7_ v1))\n xxx = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n xxx' : mustPT _\u21d3_ (Div (Val 3) (Val 3)) (3 \u00f7 3)\n xxx' = \u21d3Step {Val 3} {Val 3} {3} {2} \u21d3Base \u21d3Base\n\n {-\n resulting in a predicate on expressions\n\n for all expressions satisfying this predicate,\n the monadic interpreter and the relational specification, _\u21d3_,\n must agree on the result of evaluation\n\n What does this say about correctness of interpreter?\n To understand the predicate better, define this predicate on expressions:\n -}\n\n SafeDiv : Expr -> Set\n SafeDiv (Val x) = \u22a4\n SafeDiv (Div e1 e2) = (e2 \u21d3 Zero -> \u22a5) \u2227 SafeDiv e1 \u2227 SafeDiv e2\n\n exsdv : SafeDiv (Val 3) \u2261 \u22a4\n exsdv = refl\n exsdd : SafeDiv (Div (Val 3) (Val 3))\n \u2261 Pair ((x : Val 3 \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsdd = refl\n\n exsdx : SafeDiv (Div (Val 3) (Val 0))\n \u2261 Pair ((x : Val Zero \u21d3 Zero) -> \u22a5) (Pair (\u22a4' zero) (\u22a4' zero))\n exsdx = refl\n\n {-\n Expect : any expr e for which SafeDiv e holds\n can be evaluated without division-by-zero\n\n can prove SafeDiv is sufficient condition for two notions of evaluation to coincide:\n\n -- lemma relates the two semantics\n -- expressed as a relation and an evaluator\n -- for those expressions that satisfy the SafeDiv property\n -}\n\n correct : SafeDiv \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n correct (Val x) h = \u21d3Base\n correct (Div e1 e2) (e2nz , (sde1 , sde2)) with \u27e6 e1 \u27e7 | \u27e6 e2 \u27e7 | correct e1 sde1 | correct e2 sde2\n correct (Div e1 e2) (e2nz , (sde1 , sde2)) | Pure v1 | Pure Zero | e1\u21d3v1 | e2\u21d3Z = magic (e2nz e2\u21d3Z)\n correct (Div e1 e2) (e2nz , (sde1 , sde2)) | Pure v1 | Pure (Succ v2) | e1\u21d3v1 | e2\u21d3Sv2 = \u21d3Step e1\u21d3v1 e2\u21d3Sv2\n correct (Div e1 e2) (e2nz , (sde1 , sde2)) | Pure v1 | Step Abort \u22a5\u2192FCRN | e1\u21d3v1 | ()\n correct (Div e1 e2) (e2nz , (sde1 , sde2)) | Step Abort _ | _ | () | _\n\n {-\n Instead of manually defining SafeDiv,\n define more general predicate characterising the domain of a partial function:\n -}\n\n dom : {a : Set} -> {b : a -> Set}\n -> ((x : a)\n -> Partial (b x))\n -> (a -> Set)\n dom f = wpPartial f (\\_ _ -> \u22a4)\n\n -- can show that the two semantics agree precisely on the domain of the interpreter:\n\n sound : dom \u27e6_\u27e7 \u2286 wpPartial \u27e6_\u27e7 _\u21d3_\n\n complete : wpPartial \u27e6_\u27e7 _\u21d3_ \u2286 dom \u27e6_\u27e7\n\n -- both proofs proceed by induction on the argument expression\n\n sound (Val x) h = \u21d3Base\n sound (Div e1 e2) h with \u27e6 e1 \u27e7 | \u27e6 e2 \u27e7 | sound e1 | sound e2\n sound (Div e1 e2) () | Pure v1 | Pure Zero | ih1 | ih2\n sound (Div e1 e2) h | Pure v1 | Pure (Succ v2) | ih1 | ih2 = \u21d3Step (ih1 tt) (ih2 tt)\n sound (Div e1 e2) () | Pure x | Step Abort x\u2081 | ih1 | ih2\n sound (Div e1 e2) () | Step Abort x | v2 | ih1 | ih2\n\n inDom : {v : Nat} -> (e : Expr) -> \u27e6 e \u27e7 == Pure v -> dom \u27e6_\u27e7 e\n inDom (Val x) h = tt\n inDom (Div e1 e2) h with \u27e6 e1 \u27e7 | \u27e6 e2 \u27e7\n inDom (Div e1 e2) () | Pure v1 | Pure Zero\n inDom (Div e1 e2) h | Pure v1 | Pure (Succ v2) = tt\n inDom (Div e1 e2) () | Pure _ | Step Abort _\n inDom (Div e1 e2) () | Step Abort _ | _\n\n aux : (e : Expr) (v : Nat) -> \u27e6 e \u27e7 \u2261 Pure v -> e \u21d3 v\n aux e v eq with sound e (inDom e eq)\n ... | H rewrite eq = H\n\n wpPartial1 : {e1 e2 : Expr} -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div e1 e2) -> wpPartial \u27e6_\u27e7 _\u21d3_ e1\n wpPartial1 {e1} {e2} h with \u27e6 e1 \u27e7 | inspect \u27e6_\u27e7 e1 | \u27e6 e2 \u27e7\n wpPartial1 {e1} {e2} () | Pure x | eq | Pure Zero\n wpPartial1 {e1} {e2} h | Pure x | [[[ eq ]]] | Pure (Succ y) = aux e1 x eq\n wpPartial1 {e1} {e2} () | Pure x | eq | Step Abort x\u2081\n wpPartial1 {e1} {e2} () | Step Abort x | eq | ve2\n\n wpPartial2 : {e1 e2 : Expr} -> wpPartial \u27e6_\u27e7 _\u21d3_ (Div e1 e2) -> wpPartial \u27e6_\u27e7 _\u21d3_ e2\n wpPartial2 {e1} {e2} h with \u27e6 e1 \u27e7 | inspect \u27e6_\u27e7 e1 | \u27e6 e2 \u27e7 | inspect \u27e6_\u27e7 e2\n wpPartial2 {e1} {e2} h | Pure x | [[[ eqx ]]] | Pure y | [[[ eqy ]]] = aux e2 y eqy\n wpPartial2 {e1} {e2} () | Pure x | [[[ eq ]]] | Step Abort x\u2081 | eq2\n wpPartial2 {_} {_} () | Step Abort x | eq1 | se2 | eq2\n\n complete (Val x) h = tt\n complete (Div e1 e2) h\n with \u27e6 e1 \u27e7 | inspect \u27e6_\u27e7 e1 | \u27e6 e2 \u27e7 | inspect \u27e6_\u27e7 e2\n | complete e1 (wpPartial1 {e1} {e2} h)\n | complete e2 (wpPartial2 {e1} {e2} h)\n complete (Div e1 e2) h | Pure x | [[[ eqx ]]] | Pure Zero | [[[ eqy ]]] | p1 | p2\n rewrite eqx | eqy = magic h\n complete (Div e1 e2) h | Pure x | [[[ eqx ]]] | Pure (Succ y) | [[[ eqy ]]] | p1 | p2 = tt\n complete (Div e1 e2) h | Pure x | eq1 | Step Abort x\u2081 | eq2 | p1 | ()\n complete (Div e1 e2) h | Step Abort x | eq1 | se2 | eq2 | () | p2\n\n {-\n --------------------------------------------------\n Refinement\n\n weakest precondition semantics on partial computations give rise\n to a refinement relation on Kleisli arrows of the form a -> Partial b\n\n can characterise this relation by proving:\n\n refinement : (f g : a -> Maybe b)\n -> (wpPartial f \u2291 wpPartial g) \u2194 (\u2200 x -> (f x \u2261 g x) \u2228 (f x \u2261 Nothing))\n\n use refinement to relate Kleisli morphisms,\n and to relate a program to a specification given by a pre- and post- condition\n\n --------------------------------------------------\n Example: Add (interpreter for stack machine)\n\n add top two elements; can fail fail if stack has too few elements\n\n below shows how to prove the definition meets its specification\n\n Define specification in terms of a pre\/post condition.\n -}\n -- specification of a function of type (x : a) -> b x consists of:\n record Spec {l : Level} (a : Set) (b : a -> Set) : Set (suc l) where\n constructor [[_,_]]\n field\n pre : a -> Set l -- precondition on 'a'\n post : (x : a) -> b x -> Set l -- postcondition relating inputs that satisfy this precondition\n -- and the corresponding outputs\n\n {-\n [ P , Q ] : specification consisting of precondition P and postcondition Q\n\n for non-dependent examples (e.g., type b does not depend on x : a)\n -}\n\n SpecK : {l : Level} -> Set -> Set -> Set (suc l)\n SpecK a b = Spec a (K b) -- K is constant function\n\n -- specification for addition function : describes the desired postcondition\n data Add : List Nat -> List Nat -> Set where\n AddStep : {x1 x2 : Nat} -> {xs : List Nat}\n -> Add (x1 :: x2 :: xs) ((x1 + x2) :: xs)\n\n addSpec : SpecK (List Nat) (List Nat)\n addSpec = [[ (\\xs -> length xs > 1) , Add ]]\n\n {-\n How to relate this specification to an implementation?\n Note: 'wpPartial' assigns predicate transformer semantics to functions\n - but do not yet have a corresponding predicate transform semantics for specifications.\n wpSpec function does that:\n -}\n\n -- Given a specification, Spec a b\n -- computes the weakest precondition that satisfies an arbitrary postcondition P\n -- i.e., the spec\u2019s precondition should hold and its postcondition must imply P.\n wpSpec : forall {l a} -> {b : a -> Set}\n -> Spec {l} a b\n -> (P : (x : a) -> b x -> Set l)\n -> (a -> Set l)\n wpSpec [[ pre , post ]] P = \\x -> (pre x) \u2227 (post x \u2286 P x)\n\n -- Can now formulate program\n\n pop : forall {a} -> List a -> Partial (a \u00d7 List a)\n pop Nil = abort\n pop (x :: xs) = return (x , xs)\n\n add : List Nat -> Partial (List Nat)\n add xs =\n pop xs >>= \\{(x1 , xs) ->\n pop xs >>= \\{(x2 , xs) ->\n return ((x1 + x2) :: xs)}}\n\n -- that refines the specification given by addSpec:\n correctnessAdd : wpSpec addSpec \u2291 wpPartial add\n correctnessAdd P Nil (() , _)\n correctnessAdd P (x :: Nil) (Data.Nat.s\u2264s () , _)\n correctnessAdd P (x :: y :: xs) (_ , H) = H (x + y :: xs) AddStep\n\n {-\n This example illustrates how to use the refinement relation\n - to relate a specification\n - given in terms of a pre- and postcondition,\n - to its implementation.\n\n Compared to the refinement calculus\n - have not yet described how to mix code and specifications (see Section 7)\n\n --------------------------------------------------\n Alternative semantics\n -}\n\n product : List Nat -> Nat\n product = foldr _*_ 1\n\n fastProduct : List Nat -> Partial Nat\n fastProduct Nil = return 1\n fastProduct (Zero :: xs) = abort\n fastProduct (k :: xs) = map (_*_ k) (fastProduct xs)\n\n defaultHandler : forall {a} -> a -> Partial a -> a\n defaultHandler _ (Pure x) = x\n defaultHandler d (Step Abort _) = d\n\n wpDefault : forall {a b : Set}\n -> (d : b)\n -> (f : a -> Partial b)\n -> (P : a -> b -> Set)\n -> (a -> Set)\n wpDefault {a} {b} d f P = wp f defaultPT\n where\n defaultPT : (x : a) -> Partial b -> Set\n defaultPT x (Pure y) = P x y\n defaultPT x (Step Abort _) = P x d\n\n soundness : forall {a b}\n -> (P : a -> b -> Set)\n -> (d : b)\n -> (c : a -> Partial b)\n -> forall x\n -> wpDefault d c P x\n -> P x (defaultHandler d (c x))\n soundness P d c x H with c x\n soundness P d c x H | Pure y = H\n soundness P d c x H | Step Abort _ = H\n\n correctnessProduct : wp product \u2291 wpDefault 0 fastProduct\n correctnessProduct P Nil H = H\n correctnessProduct P (Zero :: xs) H = H\n correctnessProduct P (Succ x :: xs) H\n with fastProduct xs | correctnessProduct (\\xs v -> P (Succ x :: xs) _) xs H\n correctnessProduct P (Succ x :: xs) H | Pure v | IH = IH\n correctnessProduct P (Succ x :: xs) H | Step Abort _ | IH rewrite *-zero\u02b3 x = IH\n\nmodule State (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n data C : Set where\n Get : C\n Put : s -> C\n\n R : C -> Set\n R Get = s\n R (Put _) = \u22a4\n\n State : forall {l} -> Set l -> Set l\n State = Free C R\n get : State s\n get = Step Get return\n\n put : s -> State \u22a4\n put s = Step (Put s) (\\_ -> return tt)\n\n run : {a : Set} -> State a -> s -> a \u00d7 s\n run (Pure x) s = (x , s)\n run (Step Get k) s = run (k s) s\n run (Step (Put s) k) _ = run (k tt) s\n\n statePT : forall {l l'} -> {b : Set l} -> (b \u00d7 s -> Set l') -> State b -> (s -> Set l')\n statePT P (Pure x) = \\s -> P (x , s)\n statePT P (Step Get k) = \\s -> statePT P (k s) s\n statePT P (Step (Put s) k) = \\_ -> statePT P (k tt) s\n\n statePT' : forall {l l'} -> {b : Set l}\n -> (s -> b \u00d7 s -> Set l')\n -> State b\n -> (s -> Set l')\n statePT' P c i = statePT (P i) c i\n\n wpState : forall {l l' l''} -> {a : Set l} -> {b : Set l'}\n -> (a -> State b)\n -> (P : a \u00d7 s -> b \u00d7 s -> Set l'')\n -> (a \u00d7 s -> Set l'')\n wpState f P (x , i) = wp f ((const \\c -> statePT' (\\j -> P (x , j)) c i)) x\n\n soundness : forall {a b : Set}\n -> (P : a \u00d7 s -> b \u00d7 s -> Set)\n -> (f : a -> State b)\n -> forall i x\n -> wpState f P (x , i)\n -> P (x , i) (run (f x) i)\n soundness {a} {b} P c i x H = lemma i (c x) H\n where\n lemma : (st : s) -> (statec : State b) -> (statePT (P (x , i)) statec st) -> P (x , i) (run statec st)\n lemma i (Pure y) H = H\n lemma i (Step Get k) H = lemma i (k i) H\n lemma i (Step (Put s) k) H = lemma s (k tt) H\n\nmodule Relabel where\n open Free\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n )\n\n module StateNat = State Nat\n open StateNat\n\n data Tree (a : Set) : Set where\n Leaf : a -> Tree a\n Node : Tree a -> Tree a -> Tree a\n\n flatten : \u2200 {a} -> Tree a -> List a\n flatten (Leaf x) = [ x ]\n flatten (Node l r) = flatten l ++ flatten r\n\n size : \u2200 {a} -> Tree a -> Nat\n size (Leaf x) = 1\n size (Node l r) = size l + size r\n\n seq : Nat -> Nat -> List Nat\n seq i Zero = Nil\n seq i (Succ n) = Cons i (seq (Succ i) n)\n\n relabelSpec : forall {a} -> SpecK (Tree a \u00d7 Nat) (Tree Nat \u00d7 Nat)\n relabelSpec = [[ K \u22a4 , relabelPost ]]\n where\n relabelPost : forall {a} -> Tree a \u00d7 Nat -> Tree Nat \u00d7 Nat -> Set\n relabelPost (t , s) (t' , s') = (flatten t' == (seq (s) (size t))) \u2227 (s + size t == s')\n\n fresh : State Nat\n fresh = get >>= \\n ->\n put (Succ n) >>\n return n\n\n relabel : forall {a} -> Tree a -> State (Tree Nat)\n relabel (Leaf x) = map Leaf fresh\n relabel (Node l r) =\n relabel l >>= \\l' ->\n relabel r >>= \\r' ->\n return (Node l' r')\n\n correctnessRelabel : forall {a : Set} -> wpSpec ( relabelSpec {a}) \u2291 wpState relabel\n\n compositionality : forall {a b : Set} -> (c : State a) (f : a -> State b) ->\n \u2200 i P -> statePT P (c >>= f) i == statePT (wpState f (const P)) c i\n\n compositionality (Pure x) f i P = refl\n compositionality (Step Get k) f i P = compositionality (k i) f i P\n compositionality (Step (Put x) k) f i P = compositionality (k tt) f x P\n\n correctnessRelabel = step2\n where\n open NaturalLemmas\n -- Simplify proofs of refining a specification,\n -- by first proving one side of the bind, then the second.\n -- This is essentially the first law of consequence,\n -- specialized to the effects of State and Spec.\n prove-bind : \u2200 {a b} (mx : State a) (f : a -> State b) P i ->\n statePT (wpState f \\_ -> P) mx i -> statePT P (mx >>= f) i\n prove-bind mx f P i x = coerce {zero} (sym (compositionality mx f i P)) x\n\n prove-bind-spec : \u2200 {a b} (mx : State a) (f : a -> State b) spec ->\n \u2200 P i -> (\u2200 Q -> Spec.pre spec i \u00d7 (Spec.post spec i \u2286 Q) -> statePT Q mx i) ->\n Spec.pre spec i \u00d7 (Spec.post spec i \u2286 wpState f (\\_ -> P)) ->\n statePT P (mx >>= f) i\n prove-bind-spec mx f spec P i Hmx Hf = prove-bind mx f P i (Hmx (wpState f (\\_ -> P)) Hf)\n\n -- Partially apply a specification.\n applySpec : \u2200 {a b s} -> SpecK {zero} (a \u00d7 s) (b \u00d7 s) -> a -> SpecK s (b \u00d7 s)\n applySpec [[ pre , post ]] x = [[ (\\s -> pre (x , s)) , (\\s -> post (x , s)) ]]\n\n -- Ingredients for proving the postcondition holds.\n -- By abstracting over the origin of the numbers,\n -- we can do induction on them nicely.\n append-seq : \u2200 a b c -> seq a b ++ seq (a + b) c \u2261 seq a (b + c)\n append-seq a Zero c = cong (\\a' -> seq a' c) (sym (plus-zero a))\n append-seq a (Succ b) c = cong (Cons a) (trans\n (cong (\\a+b -> seq (Succ a) b ++ seq a+b c) (+-succ a b))\n (append-seq (Succ a) b c))\n\n postcondition : \u2200 s s' s'' sl fl sr fr ->\n Pair (fl \u2261 seq s sl) (s + sl \u2261 s') ->\n Pair (fr \u2261 seq s' sr) (s' + sr \u2261 s'') ->\n Pair (fl ++ fr \u2261 seq s (sl + sr)) (s + (sl + sr) \u2261 s'')\n postcondition s .(s + sl) .(s + sl + sr) sl .(seq s sl) sr .(seq (s + sl) sr)\n (refl , refl) (refl , refl) = append-seq s sl sr , +-assoc s sl sr\n\n -- We have to rewrite the formulation of step2 slightly to make it acceptable for the termination checker.\n\n step2' : \u2200 {a} P (t : Tree a) s -> wpSpec relabelSpec P (t , s) -> statePT (P (t , s)) (relabel t) s\n step2' P (Leaf x) s (fst , snd) = snd (Leaf s , Succ s) (refl , plus-one s)\n step2' P (Node l r) s (fst , snd) = prove-bind-spec (relabel l) _ (applySpec relabelSpec l) _ _\n (\\Q -> step2' (\\_ -> Q) l s)\n (tt , \\l',s' postL -> let l' = Pair.fst l',s' ; s' = Pair.snd l',s'\n in prove-bind-spec (relabel r) _ (applySpec relabelSpec r) _ _\n (\\Q -> step2' (\\_ -> Q) r s')\n (tt , \\r',s'' postR -> let r' = Pair.fst r',s'' ; s'' = Pair.snd r',s''\n in snd (Node l' r' , s'') (postcondition s s' s'' (size l) (flatten l') (size r) (flatten r') postL postR)))\n step2 : wpSpec relabelSpec \u2291 wpState relabel\n step2 P (t , s) (fst , snd) = step2' P t s (fst , snd)\n\nmodule Compositionality\n (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set)\n where\n open Free\n open Maybe using (wpSpec; [[_,_]])\n\n postulate\n ext : {l l' : Level} {a : Set l} {b : Set l'} -> {f g : a -> b} ->\n ((x : a) -> f x \u2261 g x) -> f \u2261 g\n\n pt : {a : Set} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : {a : Set} {b : a -> Set} ->\n ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n compositionality : forall {a b : Set} -> (c : Free C R a) (f : a -> Free C R b) ->\n \u2200 P -> pt (c >>= f) P \u2261 pt c (wpCR f (const P))\n compositionality (Pure x) f P = refl\n compositionality (Step c x) f P =\n cong (\\h -> ptalgebra c h) (ext (\\r -> compositionality (x r) f P))\n\n _>=>_ : forall {l l' l''} -> {a : Set l} -> {b : Set l'} -> {c : Set l''} -> forall {C R} -> (a -> Free C R b) -> (b -> Free C R c) -> a -> Free C R c\n f >=> g = \\x -> f x >>= g\n\n compositionality-left : forall {a b c : Set} -> (f1 f2 : a -> Free C R b) (g : b -> Free C R c) ->\n wpCR f1 \u2291 wpCR f2 ->\n wpCR (f1 >=> g) \u2291 wpCR (f2 >=> g)\n compositionality-left mx my f H P x y\n rewrite compositionality (mx x) f (P x)\n | compositionality (my x) f (P x) =\n H (\\x y -> pt (f y) (P x)) x y\n\n compositionality-right : forall {a b c} -> (f : a -> Free C R b) (g1 g2 : b -> Free C R c) ->\n wpCR g1 \u2291 wpCR g2 ->\n wpCR (f >=> g1) \u2291 wpCR (f >=> g2)\n postulate\n monotonicity : forall {a} -> {P Q : a -> Set} -> P \u2286 Q -> (c : Free C R a) -> pt c P -> pt c Q\n compositionality-right mx f g H P x wp1\n rewrite compositionality (mx x) f (P x)\n | compositionality (mx x) g (P x) = monotonicity (H _) (mx x) wp1\n\n weakenPre : {a : Set} -> {b : a -> Set} -> {P P' : a -> Set} -> {Q : (x : a) -> b x -> Set} -> (P \u2286 P') -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P' , Q ]])\n\n\n strengthenPost : {a : Set} -> {b : a -> Set} -> {P : a -> Set} -> {Q Q' : (x : a) -> b x -> Set} -> (forall (x : a) -> Q' x \u2286 Q x) -> (wpSpec [[ P , Q ]] \u2291 wpSpec [[ P , Q' ]])\n\n weakenPre H1 p H2 (pre , post) = (H1 H2 pre , post)\n\n strengthenPost H1 p H2 (pre , post) = (pre , \\x y -> post x (H1 _ x y))\n\nmodule Laws (s : Set) where\n open Free\n open Maybe using (SpecK; Spec; [[_,_]]; wpSpec)\n module StateS = State s\n open StateS\n\n postulate\n a : Set\n k0 : State a\n k1 : s -> State a\n k2 : s -> s -> State a\n x y : s\n\n _\u2243_ : forall {b : Set} -> State b -> State b -> Set\u2081\n t1 \u2243 t2 = (wpState' t1 \u2291 wpState' t2) \u2227 (wpState' t2 \u2291 wpState' t1)\n where\n wpState' : forall {b} -> State b -> (P : s -> b \u00d7 s -> Set) -> (s -> Set)\n wpState' {b} t P s = wpState {a = \u22a4} {b} (\\_ -> t) (\\{(tt , s') y -> P s' y}) (tt , s)\n\nmodule Nondeterminism where\n\n open Free hiding (_\u2286_)\n open Maybe using (wpSpec; SpecK; [[_,_]])\n data C : Set where\n Fail : C\n Choice : C\n\n R : C -> Set\n R Fail = \u22a5\n R Choice = Bool\n\n ND : Set -> Set\n ND = Free C R\n\n fail : forall {a} -> ND a\n fail = Step Fail (\\())\n\n choice : forall {a} -> ND a -> ND a -> ND a\n choice c1 c2 = Step Choice (\\b -> if b then c1 else c2)\n\n allPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n allPT P _ (Pure x) = P _ x\n allPT P _ (Step Fail k) = \u22a4\n allPT P _ (Step Choice k) = allPT P _ (k True) \u2227 allPT P _ (k False)\n\n wpAll : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAll f P = wp f (allPT P)\n\n anyPT : forall {a : Set} -> {b : a -> Set} -> (P : (x : a) -> b x -> Set) -> (x : a) -> ND (b x) -> Set\n anyPT P _ (Pure x) = P _ x\n anyPT P _ (Step Fail k) = \u22a5\n anyPT P _ (Step Choice k) = anyPT P _ (k True) \u2228 anyPT P _ (k False)\n\n wpAny : forall {a : Set} -> {b : a -> Set} -> ((x : a) -> ND (b x)) -> (P : (x : a) -> b x -> Set) -> (a -> Set)\n wpAny f P = wp f (anyPT P)\n\n run : forall {a} -> ND a -> List a\n run (Pure x) = [ x ]\n run (Step Fail _) = Nil\n run (Step Choice k) = run (k True) ++ run (k False)\n\n All : {a : Set} -> (a -> Set) -> List a -> Set\n All P Nil = \u22a4\n All P (x :: xs) = P x \u2227 All P xs\n\n All++ : {a : Set} (P : a -> Set) (xs ys : List a) ->\n All P xs -> All P ys -> All P (xs ++ ys)\n All++ P Nil ys H1 H2 = H2\n All++ P (x :: xs) ys (Px , H1) H2 = Px , All++ P xs ys H1 H2\n\n allSoundness : {a : Set} {b : a -> Set} (P : (x : a) -> b x -> Set) (x : a) (nd : ND (b x)) ->\n allPT P x nd -> All (P x) (run nd)\n allSoundness P x (Pure y) H = H , tt\n allSoundness P x (Step Fail _) H = tt\n allSoundness P x (Step Choice k) (H1 , H2) =\n All++ (P x) (run (k True)) (run (k False)) (allSoundness P x (k True) H1) (allSoundness P x (k False) H2)\n\n wpAllSoundness : forall {a} -> {b : a -> Set} -> (f : (x : a) -> ND (b x)) ->\n \u2200 P x -> wpAll f P x -> All (P x) (run (f x))\n wpAllSoundness nd P x H = allSoundness P x (nd x) H\n data Elem {a : Set} (x : a) : ND a -> Set where\n Here : Elem x (Pure x)\n Left : forall {k} -> Elem x (k True) -> Elem x (Step Choice k)\n Right : forall {k} -> Elem x (k False) -> Elem x (Step Choice k)\n\n _\u2286_ : forall {a} -> ND a -> ND a -> Set\n nd1 \u2286 nd2 = \u2200 x -> Elem x nd1 -> Elem x nd2\n\n _<->_ : {l l' : Level} (a : Set l) (b : Set l') -> Set (l \u2294 l')\n a <-> b = Pair (a -> b) (b -> a)\n\n refineAll : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAll f \u2291 wpAll g) <-> ((x : a) -> g x \u2286 f x)\n\n refineAny : {a b : Set} {x : a} (f g : a -> ND b) -> (wpAny f \u2291 wpAny g) <-> ((x : a) -> f x \u2286 g x)\n allP : \u2200 {a b : Set} {x : a} P (S : ND b) -> allPT P x S <-> (\u2200 y -> Elem y S -> P x y)\n Pair.fst (allP P (Pure y)) H y Here = H\n Pair.fst (allP P (Step Choice k)) (H , _) y (Left i) = Pair.fst (allP P (k True)) H y i\n Pair.fst (allP P (Step Choice k)) (_ , H) y (Right i) = Pair.fst (allP P (k False)) H y i\n Pair.snd (allP P (Pure y)) H = H y Here\n Pair.snd (allP P (Step Fail k)) H = tt\n Pair.snd (allP P (Step Choice k)) H = (Pair.snd (allP P (k True)) \u03bb y i -> H y (Left i)) , (Pair.snd (allP P (k False)) \u03bb y i -> H y (Right i))\n\n anyP : \u2200 {a b : Set} {x : a} P (S : ND b) -> anyPT P x S <-> Sigma b \u03bb y -> Elem y S \u2227 P x y\n Pair.fst (anyP P (Pure y)) H = y , (Here , H)\n Pair.fst (anyP P (Step Fail k)) ()\n Pair.fst (anyP P (Step Choice k)) (Inl H) with Pair.fst (anyP P (k True)) H\n Pair.fst (anyP P (Step Choice k)) (Inl H) | y , (i , IH) = y , (Left i , IH)\n Pair.fst (anyP P (Step Choice k)) (Inr H) with Pair.fst (anyP P (k False)) H\n Pair.fst (anyP P (Step Choice k)) (Inr H) | y , (i , IH) = y , (Right i , IH)\n Pair.snd (anyP P (Pure y)) (.y , (Here , H)) = H\n Pair.snd (anyP P (Step .Choice k)) (y , (Left i , H)) = Inl (Pair.snd (anyP P (k True)) (y , (i , H)))\n Pair.snd (anyP P (Step .Choice k)) (y , (Right i , H)) = Inr (Pair.snd (anyP P (k False)) (y , (i , H)))\n\n Pair.fst (refineAll f g) H x y i = Pair.fst (allP (\u03bb _ y' -> Elem y' (f x)) (g x)) (H _ x (Pair.snd (allP _ (f x)) (\u03bb _ -> id))) y i\n Pair.snd (refineAll f g) r P x H = Pair.snd (allP P (g x)) \u03bb y i -> Pair.fst (allP P (f x)) H y (r x y i)\n Pair.fst (refineAny f g) H x y i with Pair.fst (anyP (\u03bb _ y' -> y' == y) (g x)) (H _ x (Pair.snd (anyP _ (f x)) (y , (i , refl))))\n Pair.fst (refineAny f g) H x y i | .y , (i' , refl) = i'\n Pair.snd (refineAny f g) r P x H with Pair.fst (anyP P (f x)) H\n Pair.snd (refineAny f g) r P x H | y , (i , IH) = Pair.snd (anyP P (g x)) (y , ((r x y i) , IH))\n\n selectPost : forall {a} -> List a -> a \u00d7 List a -> Set\n selectPost xs (y , ys) = Sigma (y \u2208 xs) (\\e -> delete xs e == ys)\n\n removeSpec : forall {a} -> SpecK (List a) (a \u00d7 List a)\n removeSpec = [[ K \u22a4 , selectPost ]]\n\n remove : forall {a} -> List a -> ND (a \u00d7 List a)\n remove Nil = fail\n remove (x :: xs) = choice (return (x , xs)) (map (retain x) (remove xs))\n where\n retain : forall {a} -> a -> a \u00d7 List a -> a \u00d7 List a\n retain x (y , ys) = (y , (x :: ys))\n\n removeCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll remove\n removeCorrect P Nil (tt , snd) = tt\n removeCorrect P (x :: xs) (tt , snd) =\n snd (x , xs) (\u2208Head , refl) ,\n mapPT P (x :: xs) xs (remove xs) _\n (removeCorrect _ xs (tt , (\u03bb {(x' , xs') (i , H) -> snd (x' , (x :: xs')) (\u2208Tail i , cong (x ::_) H)})))\n where\n mapPT : \u2200 {a b c : Set} P (x x' : a) (S : ND b) (f : b -> c) -> allPT (\u03bb _ y -> P x (f y)) x' S -> allPT P x (map f S)\n mapPT P x x' (Pure y) f H = H\n mapPT P x x' (Step Fail k) f H = H\n mapPT P x x' (Step Choice k) f (fst , snd) = mapPT P x x' (k True) f fst , mapPT P x x' (k False) f snd\n\n trivialCorrect : forall {a} -> wpSpec {a = List a} {const (a \u00d7 List a)} removeSpec \u2291 wpAll (const fail)\n trivialCorrect = \\P xs H -> tt\n\n completeness : forall {a} -> (y : a) (xs ys : List a) -> selectPost xs (y , ys) -> Elem (y , ys) (remove xs)\n completeness y (y :: _) ys (\u2208Head , refl) = Left Here\n completeness y (x :: xs) .(x :: delete xs fst) (\u2208Tail fst , refl) = Right (inMap _ (remove xs) _ (completeness y _ _ (fst , refl)))\n where\n inMap : \u2200 {a b : Set} (x : a) S (f : a -> b) -> Elem x S -> Elem (f x) (map f S)\n inMap x (Pure x) f Here = Here\n inMap x (Step Choice k) f (Left i) = Left (inMap x (k True) f i)\n inMap x (Step Choice k) f (Right i) = Right (inMap x (k False) f i)\n\nmodule Recursion where\n open Free\n open import Data.Nat public\n using\n (_+_; _>_; _*_\n )\n renaming\n ( \u2115 to Nat\n ; zero to Zero\n ; suc to Succ\n ; _\u2238_ to _-_\n )\n open NaturalLemmas\n open Maybe hiding (soundness)\n\n _~~>_ : (I : Set) (O : I -> Set) -> Set\n I ~~> O = (i : I) -> Free I O (O i)\n\n call : forall {I O} -> (i : I) -> Free I O (O i)\n call x = Step x Pure\n\n f91 : Nat ~~> K Nat\n f91 i with 100 lt i\n f91 i | yes _ = return (i - 10)\n f91 i | no _ = call (i + 11) >>= call\n\n f91Post : Nat -> Nat -> Set\n f91Post i o with 100 lt i\n f91Post i o | yes _ = o == i - 10\n f91Post i o | no _ = o == 91\n\n f91Spec : SpecK Nat Nat\n f91Spec = [[ K \u22a4 , f91Post ]]\n\n invariant : forall {I} -> {O : I -> Set} -> (i : I) -> Spec I O -> Free I O (O i) -> Set\n invariant i [[ pre , post ]] (Pure x) = pre i -> post i x\n invariant i [[ pre , post ]] (Step j k) = (pre i -> pre j)\n \u2227 \u2200 o -> post j o -> invariant i [[ pre , post ]] (k o)\n wpRec : forall {I} -> {O : I -> Set} -> Spec I O -> (f : I ~~> O) -> (P : (i : I) -> O i -> Set) -> (I -> Set)\n wpRec spec f P i = wpSpec spec P i \u2227 invariant i spec (f i)\n\n f91Partial-correctness : wpSpec f91Spec \u2291 wpRec f91Spec f91\n f91Partial-correctness P i with 100 lt i\n f91Partial-correctness P i | yes p with 100 lt i\n f91Partial-correctness P i | yes p | yes _ = \\H -> (tt , (\\x eq -> Pair.snd H _ eq)) , (\\x -> refl)\n f91Partial-correctness P i | yes p | no \u00acp = magic (\u00acp p)\n f91Partial-correctness P i | no \u00acp = \\x -> (tt , (\\x\u2081 x\u2082 -> Pair.snd x x\u2081 x\u2082)) ,\n ((\\_ -> tt) , (\\o x\u2081 -> (\\x\u2082 -> tt) ,\n (\\o\u2081 x\u2082 x\u2083 -> lemma i o _ \u00acp x\u2081 x\u2082)))\n where\n open Data.Nat\n open import Data.Nat.Properties\n\n 100-\u226e-91 : (i : Nat) -> \u00ac (i + 10 \u2264 i)\n 100-\u226e-91 Zero ()\n 100-\u226e-91 (Succ i) (s\u2264s pf) = 100-\u226e-91 i pf\n\n plus-minus : \u2200 b c -> (b + c) - c == b\n plus-minus b c = trans (+-\u2238-assoc b (NaturalLemmas.\u2264-refl {c})) (trans (cong (b +_) (n\u2238n\u22610 c)) (sym (plus-zero b)))\n\n plus-plus-minus : \u2200 i -> i + 11 - 10 \u2261 Succ i\n plus-plus-minus i = plus-minus (Succ i) 11\n\n between : \u2200 a b -> \u00ac (a < b) -> a < Succ b -> a \u2261 b\n between Zero Zero \u00aclt ltSucc = refl\n between Zero (Succ b) \u00aclt ltSucc = magic (\u00aclt (s\u2264s z\u2264n))\n between (Succ a) Zero \u00aclt (s\u2264s ())\n between (Succ a) (Succ b) \u00aclt (s\u2264s ltSucc) = cong Succ (between a b (\u00aclt \u2218 s\u2264s) ltSucc)\n\n lemma : \u2200 i o o' -> \u00ac (100 < i) ->\n f91Post (i + 11) o -> f91Post o o' -> f91Post i o'\n lemma i o o' i\u2264100 oPost o'Post with 100 lt i\n ... | yes p = magic (i\u2264100 p)\n ... | no \u00acp with 100 lt o\n lemma i o .(o - 10) i\u2264100 oPost refl | no \u00acp | yes p with 100 lt (i + 11)\n lemma i .(i + 11 - 10) .(i + 11 - 10 - 10) i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 with between 100 i i\u2264100 (subst (\\i' -> 100 < i') (plus-plus-minus i) p)\n lemma .100 .101 .91 i\u2264100 refl refl | no \u00acp | yes p | yes p\u2081 | refl = refl\n lemma i .91 .81 i\u2264100 refl refl | no \u00acp | yes p | no \u00acp\u2081 = magic (100-\u226e-91 91 p)\n lemma i o o' i\u2264100 oPost o'Post | no \u00acp | no \u00acp\u2081 = o'Post\n\n petrol : forall {I O a} -> (f : I ~~> O) -> Free I O a -> Nat -> Partial a\n petrol f (Pure x) n = return x\n petrol f (Step _ _) Zero = abort\n petrol f (Step c k) (Succ n) = petrol f (f c >>= k) n\n\n mayPT : forall {a} -> (a -> Set) -> (Partial a -> Set)\n mayPT P (Pure x) = P x\n mayPT P (Step Abort _) = \u22a4\n\n soundness : forall {I O} -> (f : I ~~> O) (spec : Spec I O) (P : (i : I) -> O i -> Set) ->\n (\u2200 i -> wpRec spec f P i) -> \u2200 n i -> mayPT (P i) (petrol f (f i) n)\n soundness f spec P wpH n i = soundness' f spec P (f i) n wpH (wpH i)\n where\n invariant-compositionality : \u2200 {I} {O : I -> Set} {i i'} spec\n (S : Free I O (O i)) (k : (O i) -> Free I O (O i')) ->\n invariant i spec S -> Spec.pre spec i -> (\u2200 o -> Spec.post spec i o -> invariant i' spec (k o)) ->\n invariant i' spec (S >>= k)\n invariant-compositionality spec (Pure x) k SH preH kH = kH x (SH preH)\n invariant-compositionality spec (Step c k') k (fst , snd) preH kH = (\\_ -> fst preH) , \\o postH -> invariant-compositionality spec (k' o) k (snd o postH) preH kH\n soundness' : \u2200 {I} {O : I -> Set} {i}\n (f : (i : I) -> Free I O (O i)) (spec : Spec I O) (P : (i : I) -> O i -> Set)\n (S : Free I O (O i)) (n : Nat) ->\n (\u2200 i -> wpRec spec f P i) ->\n wpSpec spec P i \u2227 invariant i spec S ->\n mayPT (P i) (petrol f S n)\n soundness' f spec P (Pure x) n wpH ((preH , postH) , invH) = postH x (invH preH)\n soundness' f spec P (Step c k) Zero wpH H = tt\n soundness' f spec P (Step c k) (Succ n) wpH (specH , (preH , postH)) = soundness' f spec P (f c >>= k) n wpH (specH , invariant-compositionality spec (f c) k (Pair.snd (wpH c)) (preH (Pair.fst specH)) postH)\n\nmodule Mix (C : Set) (R : C -> Set) (ptalgebra : (c : C) -> (R c -> Set) -> Set) where\n open Free hiding (_>>=_)\n open Maybe using (SpecK; [[_,_]]; Spec; wpSpec)\n\n SpecVal : Set -> Set\u2081\n SpecVal a = SpecK \u22a4 a\n\n data I (a : Set) : Set\u2081 where\n Done : a -> I a\n Hole : SpecVal a -> I a\n\n ptI : forall {a} -> I a -> (a -> Set) -> Set\n ptI (Done x) P = P x\n ptI (Hole spec) P = wpSpec spec (const P) tt\n\n M : Set -> Set\u2081\n M a = Free C R (I a)\n\n isExecutable : forall {a} -> M a -> Set\n isExecutable (Pure (Done _)) = \u22a4\n isExecutable (Pure (Hole _)) = \u22a5\n isExecutable (Step c k) = \u2200 r -> isExecutable (k r)\n\n pt : forall {l} -> {a : Set l} -> Free C R a -> (a -> Set) -> Set\n pt (Pure x) P = P x\n pt (Step c x) P = ptalgebra c (\\r -> pt (x r) P)\n\n wpCR : forall {l l'} -> {a : Set l} -> {b : a -> Set l'} -> ((x : a) -> Free C R (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpCR f P x = pt (f x) (P x)\n\n wpM : {a : Set} -> {b : a -> Set} -> ((x : a) -> M (b x)) -> ((x : a) -> b x -> Set) -> (a -> Set)\n wpM f P x = wpCR f (\\x ix -> ptI ix (P x)) x\n\nmodule StateExample where\n open Free hiding (_>>=_)\n open Maybe using (Spec; SpecK; [[_,_]]; wpSpec)\n open State Nat\n\n -- We have to redo the Mix section since our specifications incorporate the state\n SpecVal : \u2200 {l} -> Set -> Set (suc l)\n SpecVal = SpecK Nat\n\n data I {l : Level} (a : Set) : Set (suc l) where\n Done : a -> I a\n Hole : SpecVal {l} (a \u00d7 Nat) -> I a\n\n M : {l : Level} -> Set -> Set (suc l)\n M a = State (I a)\n\n ptI : forall {l a} -> I a -> (a \u00d7 Nat -> Set l) -> Nat -> Set l\n ptI (Done x) P t = P (x , t)\n ptI (Hole spec) P t = wpSpec spec (\\_ -> P) t\n\n wpM : forall {l l'} {a : Set l} {b : Set} -> (a -> M b) -> (a \u00d7 Nat -> b \u00d7 Nat -> Set l') -> (a \u00d7 Nat -> Set l')\n wpM f P = wpState f (\\i o -> ptI (Pair.fst o) (P i) (Pair.snd o))\n\n ptM : {a : Set} -> M a -> (Nat -> a \u00d7 Nat -> Set) -> (Nat -> Set)\n ptM S post t = wpM (\u03bb _ -> S) (\u03bb _ -> (post t)) (\u22a4 , t)\n\n liftM : \u2200 {l : Level} {a} -> M {l} a -> M {suc l} a\n liftM (Pure (Done x)) = Pure (Done x)\n liftM (Pure (Hole [[ pre , post ]])) = Pure (Hole [[ (\u03bb x -> Lift _ (pre x)) , (\u03bb i o -> Lift _ (post i o)) ]])\n liftM (Step c k) = Step c \u03bb x -> liftM (k x)\n\n _>>=_ : forall {l a b} -> (M {l} a) -> (a -> M {l} b) -> M {suc l} b\n Pure (Done x) >>= f = liftM (f x)\n Pure (Hole [[ pre , post ]]) >>= f =\n Pure (Hole [[ (\u03bb n -> Lift _ (pre n)) ,\n (\\i ynat -> \u2200 x -> post i (x , i) -> \u2200 P -> wpM f P (x , i) -> P (x , i) ynat\n ) ]] )\n (Step c k) >>= f = Step c (\\r -> k r >>= f)\n\n _>=>_ : forall {l : Level} {a b c : Set} -> (a -> M {l} b) -> (b -> M {l} c) -> a -> M {suc l} c\n (f >=> g) x = f x >>= g\n\n specV : {a : Set} -> SpecVal {zero} (a \u00d7 Nat) -> M a\n specV spec = Pure (Hole spec)\n\n done : forall {a} -> a -> M {zero} a\n get' : M Nat\n put' : Nat -> M \u22a4\n done x = Pure (Done x)\n get' = Step Get done\n put' t = Step (Put t) done\n\n getPost : Nat -> Nat \u00d7 Nat -> Set\n getPost t (x , t') = (t == x) \u2227 (t == t')\n\n putPost : Nat -> Nat -> \u22a4 \u00d7 Nat -> Set\n putPost t _ (_ , t') = t == t'\n\n _\u25b9_ : {a b : Set} -> (Q : a -> b -> Set) (P : a -> Set) -> b -> Set\n _\u25b9_ {a} Q P = \\y -> Sigma a (\\x -> P x \u2227 Q x y)\n _\u25c3_ : {a b c : Set} -> (Q : a -> b -> Set) -> (SpecK a c) -> b -> c -> Set\n _\u25c3_ Q [[ pre , post ]] = \\y z -> \u2200 x -> pre x \u2227 Q x y -> post x z\n\n step : forall {b} -> (c : C) (spec : SpecVal {zero} (b \u00d7 Nat)) -> SpecK {zero} (R c \u00d7 Nat) (b \u00d7 Nat)\n step Get [[ pre , post ]] = [[ getPost \u25b9 pre , getPost \u25c3 [[ pre , post ]] ]]\n step (Put x) [[ pre , post ]] = [[ (putPost x) \u25b9 pre , (putPost x) \u25c3 [[ pre , post ]] ]]\n\n intros : forall {a b} -> SpecK {zero} (a \u00d7 Nat) (b \u00d7 Nat) -> a -> SpecVal (b \u00d7 Nat)\n intros [[ pre , post ]] x = [[ (\\t -> pre (x , t)) , (\\t -> post (x , t)) ]]\n\n data Derivation {b} (spec : SpecVal (b \u00d7 Nat)) : Set\u2081 where\n Done : (x : b) -> wpSpec spec \u2291 ptM (done x) -> Derivation spec\n Step : (c : C) -> (\u2200 (r : R c) -> Derivation (intros (step c spec) r)) -> Derivation spec\n\n extract : forall {b} -> (spec : SpecVal (b \u00d7 Nat)) -> Derivation spec -> State b\n extract _ (Done x _) = Pure x\n extract _ (Step c k) = Step c (\\r -> extract _ (k r))\n\n DerivationFun : {a b : Set} (spec : SpecK (a \u00d7 Nat) (b \u00d7 Nat)) -> Set\u2081\n DerivationFun {a} {b} spec = (x : a) -> Derivation (intros spec x)\n\n stepMonotone : {a : Set} (c : C) (r : R c) {spec spec' : SpecVal (a \u00d7 Nat)} ->\n wpSpec spec \u2291 wpSpec spec' ->\n wpSpec (intros (step c spec) r) \u2291 wpSpec (intros (step c spec') r)\n stepMonotone {a} Get r {spec} {spec'} H P .r ((.r , (fst\u2081 , (refl , refl))) , snd) = (r , (Pair.fst (H (\\_ _ -> \u22a4) r (fst\u2081 , (\\x _ -> tt))) , (refl , refl))) , \\x x\u2081 -> snd x (postLemma r x x\u2081)\n where\n postLemma : \u2200 r\n (x : Pair a Nat) ->\n (\u2200 x\u2081 ->\n Pair (Spec.pre spec' x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (Pair (x\u2081 \u2261 r) (x\u2081 \u2261 r)) ->\n Spec.post spec x\u2081 x\n postLemma r x x\u2082 .r (fst , (refl , refl)) = Pair.snd (H (Spec.post spec) r (fst , (\\x\u2083 z -> z))) x (x\u2082 r ((Pair.fst (H (\\_ _ -> \u22a4) r (fst , (\\x\u2083 _ -> tt)))) , (refl , refl)))\n stepMonotone {a} (Put t) r {spec} {spec'} H P .t ((fst , (fst\u2081 , refl)) , snd) = (fst , (Pair.fst (H (\\_ _ -> \u22a4) fst (fst\u2081 , (\\x _ -> tt))) , refl)) , \\x x\u2081 -> snd x (postLemma t x x\u2081)\n where\n postLemma : \u2200 (t : Nat)\n (x : Pair a Nat) ->\n (\u2200 x\u2081 -> Pair (Spec.pre spec' x\u2081) (t \u2261 t) -> Spec.post spec' x\u2081 x) ->\n \u2200 x\u2081 ->\n Pair (Spec.pre spec x\u2081) (t \u2261 t) -> Spec.post spec x\u2081 x\n postLemma t x x\u2081 x\u2082 (fst , refl) = Pair.snd (H (Spec.post spec) x\u2082 (fst , (\\x\u2083 z -> z))) x (x\u2081 x\u2082 ((Pair.fst (H (\\_ _ -> \u22a4) x\u2082 (fst , (\\x\u2083 _ -> tt)))) , refl))\n\n open import Data.Nat\n open import Data.Nat.Properties\n open NaturalLemmas hiding (\u2264-refl ; \u2264-trans)\n\n data All {a : Set} (P : a -> Set) : List a -> Set where\n AllNil : All P Nil\n AllCons : \u2200 {x xs} -> P x -> All P xs -> All P (x :: xs)\n\n unAllCons : \u2200 {a P x} {xs : List a} ->\n All P (x :: xs) -> All P xs\n unAllCons (AllCons x\u2081 x\u2082) = x\u2082\n\n maxPre : List Nat \u00d7 Nat -> Set\n maxPre (xs , i) = (i == 0) \u2227 (\u00ac (xs == Nil))\n\n maxPost : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost (xs , i) (o , _) = All (o \u2265_) xs \u2227 (o \u2208 xs)\n\n maxSpec = [[ maxPre , maxPost ]]\n\n refineDerivation : forall {a : Set} -> {spec spec' : SpecVal (a \u00d7 Nat)} -> wpSpec spec \u2291 wpSpec spec' -> Derivation spec' -> Derivation spec\n refineDerivation H (Done x Hx) = Done x (\u2291-trans H Hx)\n refineDerivation H (Step c d) = Step c \\r -> refineDerivation (stepMonotone c r H) (d r)\n\n maxPost' : List Nat \u00d7 Nat -> Nat \u00d7 Nat -> Set\n maxPost' (xs , i) (o , _) = All (o \u2265_) (i :: xs) \u00d7 (o \u2208 (i :: xs))\n\n maxProof : \u2200 (xs : List Nat) ->\n wpSpec (intros [[ maxPre , maxPost ]] xs) \u2291\n wpSpec (intros [[ K \u22a4 , maxPost' ]] xs)\n maxProof xs P .0 ((refl , Hnil) , snd) = tt , \\o H -> snd o (unAllCons (Pair.fst H) , lemma xs Hnil (Pair.fst o) H)\n where\n lemma : \u2200 xs -> \u00ac (xs == Nil) ->\n \u2200 w -> Pair (All (\\n -> n \u2264 w) (0 :: xs)) (w \u2208 (0 :: xs)) -> w \u2208 xs\n lemma Nil Hnil w H = magic (Hnil refl)\n lemma (.0 :: xs) _ .0 (AllCons x\u2082 (AllCons z\u2264n fst) , \u2208Head) = \u2208Head\n lemma (x :: xs) _ w (_ , \u2208Tail snd) = snd\n\n max'ProofNil : \u2200 i ->\n wpSpec (intros (step Get (intros [[ K \u22a4 , maxPost' ]] Nil)) i) \u2291 ptM (done i)\n max'ProofNil i P .i ((.i , (fst\u2081 , (refl , refl))) , snd) = snd (i , i) (lemma i)\n where\n lemma : \u2200 i x ->\n Pair \u22a4 (Pair (x \u2261 i) (x \u2261 i)) ->\n Pair (All (\\n -> n \u2264 i) (x :: Nil)) (i \u2208 (x :: Nil))\n lemma i .i (fst , (refl , refl)) = (AllCons \u2264-refl AllNil) , \u2208Head\n\n max'Proof1 : \u2200 x xs i ->\n Succ x \u2264 i ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 ->\n Pair (Sigma \u2115 (\\x\u2082 -> Pair \u22a4 (Pair (x\u2082 \u2261 i) (x\u2082 \u2261 x\u2081))))\n (\u2200 x\u2082 ->\n (\u2200 x\u2083 ->\n Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2081)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))) ->\n P x\u2081 x\u2082) ->\n Pair \u22a4\n (\u2200 x\u2082 ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) ->\n P x\u2081 x\u2082)\n max'Proof1 x xs i x snd x\u2082 (lemma x\u2082 x\u2081)\n where\n lemma : \u2200 (x\u2082 : Nat \u00d7 Nat) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (i :: xs))\n (Pair.fst x\u2082 \u2208 (i :: xs)) ->\n \u2200 x\u2083 -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 i)) ->\n Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2083 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2083 :: x :: xs))\n lemma x\u2082 (AllCons x\u2081 fst , \u2208Head) .i (_ , (refl , refl)) = (AllCons x\u2081 (AllCons (<\u21d2\u2264 x (Succ x \u2264 i -> \u22a5) ->\n \u2200 (P : Nat -> Nat \u00d7 Nat -> Set) x\u2081 -> Pair (Sigma \u2115 (\\x\u2082 -> Pair (Sigma \u2115 (\\x\u2083\n -> Pair \u22a4 (Pair (x\u2083 \u2261 i) (x\u2083 \u2261 x\u2082)))) (x \u2261 x\u2081))) (\u2200 x\u2082 -> (\u2200 x\u2083 -> Pair (Sigma\n \u2115 (\\x\u2084 -> Pair \u22a4 (Pair (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)))) (x \u2261 x\u2081) -> \u2200 x\u2084 -> Pair \u22a4 (Pair\n (x\u2084 \u2261 i) (x\u2084 \u2261 x\u2083)) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2082) (x\u2084 :: x :: xs))\n (Pair.fst x\u2082 \u2208 (x\u2084 :: x :: xs))) -> P x\u2081 x\u2082) -> Pair \u22a4 (\u2200 x\u2082 -> Pair (All (\\n\n -> n \u2264 Pair.fst x\u2082) (x\u2081 :: xs)) (Pair.fst x\u2082 \u2208 (x\u2081 :: xs)) -> P x\u2081 x\u2082)\n max'Proof2 i x xs x\u2265i P .x ((.i , ((.i , (fst\u2082 , (refl , refl))) , refl)) , snd) = tt , \\x\u2084 x\u2081 -> snd x\u2084 (lemma x\u2084 x\u2081)\n where\n lemma : \u2200 (x\u2084 : Pair Nat Nat) -> Pair (All (\\n -> n \u2264 Pair.fst x\u2084) (x :: xs))\n (Pair.fst x\u2084 \u2208 (x :: xs)) -> \u2200 x\u2083 -> Pair (Sigma \u2115 (\\x\u2085 -> Pair \u22a4 (Pair (x\u2085\n \u2261 i) (x\u2085 \u2261 x\u2083)))) (x \u2261 x) -> \u2200 x\u2085 -> Pair \u22a4 (Pair (x\u2085 \u2261 i) (x\u2085 \u2261 x\u2083)) -> Pair\n (All (\\n -> n \u2264 Pair.fst x\u2084) (x\u2085 :: x :: xs)) (Pair.fst x\u2084 \u2208 (x\u2085 :: x :: xs))\n lemma (_ , _) (AllCons x fst , \u2208Head) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u226e\u21d2\u2265 x\u2265i) (AllCons x fst)) , (\u2208Tail \u2208Head)\n lemma x\u2084 (AllCons x\u2081 fst , \u2208Tail snd) _ ((_ , (_ , (refl , refl))) , refl) _ (_ , (refl , refl)) = (AllCons (\u2264-trans (\u226e\u21d2\u2265 x\u2265i) x\u2081) (AllCons x\u2081 fst)) , (\u2208Tail (\u2208Tail snd))\n\n maxSpec' = [[ K \u22a4 , maxPost' ]]\n\n max' : (xs : List Nat) -> Derivation (intros maxSpec' xs)\n max' Nil = Step Get \\i ->\n Done i (max'ProofNil i)\n max' (x :: xs) = Step Get \\i ->\n if' x refineDerivation (max'Proof1 x xs i lt) (max' xs))\n else (\\geq -> Step (Put x) (const (refineDerivation (max'Proof2 i x xs geq) (max' xs))))\n\n max : DerivationFun [[ maxPre , maxPost ]]\n max xs = refineDerivation (maxProof xs) (max' xs)\n","returncode":0,"stderr":"","license":"unlicense","lang":"Agda"} {"commit":"1dd4f0f861df9af4f01a33af2bb834b72be708cc","subject":"Implement DependentList on top of Data.List.All.","message":"Implement DependentList on top of Data.List.All.\n\nOld-commit-hash: 32c7c7edadf9940f3c013ca3d4ebfdc8b3cbc4f4\n","repos":"inc-lc\/ilc-agda","old_file":"Base\/Data\/DependentList.agda","new_file":"Base\/Data\/DependentList.agda","new_contents":"module Base.Data.DependentList where\n\nopen import Data.List.All public\n renaming\n ( All to DependentList\n ; _\u2237_ to _\u2022_\n ; [] to \u2205\n )\n","old_contents":"module Base.Data.DependentList where\n\nopen import Level\n\nopen import Data.Unit\nopen import Data.Product using (\u03a3 ; _\u00d7_ ; _,_)\n\nimport Data.List as List\nopen List using (List ; [] ; _\u2237_)\n\ndata DependentList {a b} {A : Set a}\n (F : A \u2192 Set b) : (type-args : List A) \u2192 Set (a \u2294 b)\n where\n \u2205 : DependentList F []\n _\u2022_ : \u2200 {type-arg} {other-type-args}\n (head : F type-arg)\n (tail : DependentList F other-type-args) \u2192\n DependentList F (type-arg \u2237 other-type-args)\n\ninfixr 5 _\u2022_\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"816cffb53d3bae1af15f0f477ac1a68ccfa88b05","subject":"add more composision with >>","message":"add more composision with >>\n","repos":"crypto-agda\/protocols","old_file":"Control\/Protocol\/Sequence.agda","new_file":"Control\/Protocol\/Sequence.agda","new_contents":"{-# OPTIONS --without-K #-}\nopen import Function.NP\nopen import Type\nopen import Data.Product.NP using (\u03a3; _\u00d7_; _,_; first) renaming (proj\u2081 to fst; proj\u2082 to snd)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen import Relation.Binary.PropositionalEquality.NP using (_\u2261_; _\u2219_; refl; ap; coe; coe!; !_ ; tr)-- renaming (subst to tr)\nopen import Function.Extensionality\nopen import HoTT\nopen Equivalences\nopen import Type.Identities\n\nopen import Control.Protocol.Core\n\nmodule Control.Protocol.Sequence where\n\ninfixl 1 _>>=_ _>>_\n\n_>>=_ : (P : Proto) \u2192 (Log P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom q P >>= Q = com q \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\n>>=-fst : \u2200 P {Q} \u2192 \u27e6 P >>= Q \u27e7 \u2192 \u27e6 P \u27e7\n>>=-fst end q = end\n>>=-fst (recv P) pq = \u03bb m \u2192 >>=-fst (P m) (pq m)\n>>=-fst (send P) (m , pq) = m , >>=-fst (P m) pq\n\n>>=-snd : \u2200 P {Q}(pq : \u27e6 P >>= Q \u27e7)(p : \u27e6 P \u22a5\u27e7) \u2192 \u27e6 Q (telecom P (>>=-fst P pq) p) \u27e7\n>>=-snd end q end = q\n>>=-snd (recv P) pq (m , p) = >>=-snd (P m) (pq m) p\n>>=-snd (send P) (m , pq) p = >>=-snd (P m) pq (p m)\n\n>>-snd : \u2200 P {Q} (pq : \u27e6 P >> Q \u27e7)(p : \u27e6 P \u22a5\u27e7) \u2192 \u27e6 Q \u27e7\n>>-snd P = >>=-snd P\n\n[_]_>>>=_ : \u2200 P {Q} \u2192 \u27e6 P \u27e7 \u2192 ((log : Log P) \u2192 \u27e6 Q log \u27e7) \u2192 \u27e6 P >>= Q \u27e7\n[ end ] p >>>= q = q _\n[ recv P ] p >>>= q = \u03bb m \u2192 [ P m ] p m >>>= \u03bb log \u2192 q (m , log)\n[ send P ] (m , p) >>>= q = m , [ P m ] p >>>= \u03bb log \u2192 q (m , log)\n\n[_]_>>>\u1d38_ : \u2200 P {Q} \u2192 \u27e6 P \u27e7 \u2192 (Log P \u2192 \u27e6 Q \u27e7) \u2192 \u27e6 P >> Q \u27e7\n[ P ] p >>>\u1d38 q = [ P ] p >>>= q\n\n[_]_>>>_ : \u2200 P {Q} \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P >> Q \u27e7\n[ P ] p >>> q = [ P ] p >>>\u1d38 (const q)\n\nmodule _ {{_ : FunExt}} where\n >>=-fst-inv : \u2200 P {Q}(p : \u27e6 P \u27e7)(q : ((log : Log P) \u2192 \u27e6 Q log \u27e7)) \u2192 >>=-fst P {Q} ([ P ] p >>>= q) \u2261 p\n >>=-fst-inv end end q = refl\n >>=-fst-inv (recv P) p q = \u03bb= \u03bb m \u2192 >>=-fst-inv (P m) (p m) \u03bb log \u2192 q (m , log)\n >>=-fst-inv (send P) (m , p) q = snd= (>>=-fst-inv (P m) p \u03bb log \u2192 q (m , log))\n\n >>=-snd-inv : \u2200 P {Q}(p : \u27e6 P \u27e7)(q : ((log : Log P) \u2192 \u27e6 Q log \u27e7))(p' : \u27e6 P \u22a5\u27e7)\n \u2192 tr (\u03bb x \u2192 \u27e6 Q (telecom P x p') \u27e7) (>>=-fst-inv P p q)\n (>>=-snd P {Q} ([ P ] p >>>= q) p') \u2261 q (telecom P p p')\n >>=-snd-inv end end q p' = refl\n >>=-snd-inv (recv P) {Q} p q (m , p') =\n ap (flip _$_ (>>=-snd (P m) ([ P m ] p m >>>= (\u03bb log \u2192 q (m , log))) p'))\n (tr-\u03bb= (\u03bb z \u2192 \u27e6 Q (m , telecom (P m) z p') \u27e7)\n (\u03bb m \u2192 >>=-fst-inv (P m) {Q \u2218 _,_ m} (p m) (q \u2218 _,_ m)))\n \u2219 >>=-snd-inv (P m) {Q \u2218 _,_ m} (p m) (q \u2218 _,_ m) p'\n >>=-snd-inv (send P) {Q} (m , p) q p' = tr-snd= (\u03bb { (m , p) \u2192 \u27e6 Q (m , telecom (P m) p (p' m)) \u27e7 })\n (>>=-fst-inv (P m) p (q \u2218 _,_ m))\n (>>=-snd (P m) {Q \u2218 _,_ m} ([ P m ] p >>>= (q \u2218 _,_ m)) (p' m))\n \u2219 >>=-snd-inv (P m) {Q \u2218 _,_ m} p (\u03bb log \u2192 q (m , log)) (p' m)\n\n {- hmmm...\n >>=-uniq : \u2200 P {Q} (pq : \u27e6 P >>= Q \u27e7)(p' : \u27e6 P \u22a5\u27e7) \u2192 pq \u2261 [ P ] (>>=-fst P {Q} pq) >>>= (\u03bb log \u2192 {!>>=-snd P {Q} pq p'!})\n >>=-uniq = {!!}\n -}\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\nreplicate-proc : \u2200 n P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 replicate\u1d3e n P \u27e7\nreplicate-proc zero P p = end\nreplicate-proc (suc n) P p = [ P ] p >>> replicate-proc n P p\n\nmodule _ {{_ : FunExt}}{{_ : UA}} where\n Log->>=-\u03a3 : \u2200 (P : Proto){Q} \u2192 Log (P >>= Q) \u2261 \u03a3 (Log P) (Log \u2218 Q)\n Log->>=-\u03a3 end = ! (\u00d7= End-uniq refl \u2219 \ud835\udfd9\u00d7-snd)\n Log->>=-\u03a3 (com _ P) = \u03a3=\u2032 _ (\u03bb m \u2192 Log->>=-\u03a3 (P m)) \u2219 \u03a3-assoc\n\n Log->>-\u00d7 : \u2200 (P : Proto){Q} \u2192 Log (P >> Q) \u2261 (Log P \u00d7 Log Q)\n Log->>-\u00d7 P = Log->>=-\u03a3 P\n\n ++Log' : \u2200 (P : Proto){Q : Log P \u2192 Proto} (xs : Log P) \u2192 Log (Q xs) \u2192 Log (P >>= Q)\n ++Log' P p q = coe! (Log->>=-\u03a3 P) (p , q)\n\n-- Same as ++Log' but computes better\n++Log : \u2200 (P : Proto){Q : Log P \u2192 Proto} (xs : Log P) \u2192 Log (Q xs) \u2192 Log (P >>= Q)\n++Log end _ ys = ys\n++Log (com q P) (x , xs) ys = x , ++Log (P x) xs ys\n\nmodule _ {{_ : FunExt}}{{_ : UA}} where\n ++Log\u2261++Log' : \u2200 P {Q} xs ys \u2192 ++Log P {Q} xs ys \u2261 ++Log' P xs ys\n ++Log\u2261++Log' end xs ys\n = ! coe-\u03b2 _ _\n \u2219 ap (coe \ud835\udfd9\u00d7-snd) (! coe\u00d7= End-uniq refl)\n \u2219 coe\u2218coe \ud835\udfd9\u00d7-snd (\u00d7= End-uniq refl) (xs , ys)\n \u2219 coe-same (! (!-inv (\u00d7= End-uniq refl \u2219 \ud835\udfd9\u00d7-snd))) (xs , ys)\n ++Log\u2261++Log' (com io P) (m , xs) ys\n = ap (_,_ m) (++Log\u2261++Log' (P m) xs ys)\n \u2219 ! coe!\u03a3=\u2032 _ (\u03bb m \u2192 Log->>=-\u03a3 (P m))\n \u2219 ap (coe (! \u03a3=\u2032 _ (\u03bb m\u2081 \u2192 Log->>=-\u03a3 (P m\u2081)))) (! coe!-\u03b2 _ _)\n \u2219 coe\u2218coe (! \u03a3=\u2032 _ (\u03bb m \u2192 Log->>=-\u03a3 (P m))) (! \u03a3-assoc) ((m , xs) , ys)\n \u2219 coe-same (! (hom-!-\u2219 (\u03a3=\u2032 _ (\u03bb m \u2192 Log->>=-\u03a3 (P m))) \u03a3-assoc)) ((m , xs) , ys)\n\nmodule _ {{_ : FunExt}} where\n >>-right-unit : \u2200 P \u2192 (P >> end) \u2261 P\n >>-right-unit end = refl\n >>-right-unit (com q P) = com= refl refl \u03bb m \u2192 >>-right-unit (P m)\n\n >>=-assoc : \u2200 (P : Proto)(Q : Log P \u2192 Proto)(R : Log (P >>= Q) \u2192 Proto)\n \u2192 (P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Log P x y)))) \u2261 ((P >>= Q) >>= R)\n >>=-assoc end Q R = refl\n >>=-assoc (com q P) Q R = com= refl refl \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\n ap->>= : \u2200 P {Q\u2080 Q\u2081} \u2192 (\u2200 {log} \u2192 \u27e6 Q\u2080 log \u27e7 \u2261 \u27e6 Q\u2081 log \u27e7) \u2192 \u27e6 P >>= Q\u2080 \u27e7 \u2261 \u27e6 P >>= Q\u2081 \u27e7\n ap->>= end Q= = Q=\n ap->>= (send P) Q= = \u03a3=\u2032 _ \u03bb m \u2192 ap->>= (P m) Q=\n ap->>= (recv P) Q= = \u03a0=\u2032 _ \u03bb m \u2192 ap->>= (P m) Q=\n\n dual->> : \u2200 P Q \u2192 dual (P >> Q) \u2261 (dual P >> dual Q)\n dual->> end Q = refl\n dual->> (com io P) Q = com= refl refl \u03bb m \u2192 dual->> (P m) Q\n\n {- My coe-ap-fu is lacking...\n dual->>= : \u2200 P (Q : Log P \u2192 Proto) \u2192 dual (P >>= Q) \u2261 dual P >>= (dual \u2218 Q \u2218 coe (dual-Log P))\n dual->>= end Q = refl\n dual->>= (com io P) Q = com= refl refl \u03bb m \u2192 dual->>= (P m) (Q \u2218 _,_ m) \u2219 ap (_>>=_ (dual (P m))) (\u03bb= \u03bb ms \u2192 ap (\u03bb x \u2192 dual (Q x)) (pair= {!!} {!!}))\n -}\n\n dual-replicate\u1d3e : \u2200 n P \u2192 dual (replicate\u1d3e n P) \u2261 replicate\u1d3e n (dual P)\n dual-replicate\u1d3e zero P = refl\n dual-replicate\u1d3e (suc n) P = dual->> P (replicate\u1d3e n P) \u2219 ap (_>>_ (dual P)) (dual-replicate\u1d3e n P)\n\n{- An incremental telecom function which makes processes communicate\n during a matching initial protocol. -}\n>>=-telecom : (P : Proto){Q : Log P \u2192 Proto}{R : Log P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Log P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-telecom end p0 p1 = _ , p0 , p1\n>>=-telecom (send P) (m , p0) p1 = first (_,_ m) (>>=-telecom (P m) p0 (p1 m))\n>>=-telecom (recv P) p0 (m , p1) = first (_,_ m) (>>=-telecom (P m) (p0 m) p1)\n\n>>-telecom : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Log P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-telecom P p q = >>=-telecom P p q\n\n>>-compose : (P Q : Proto){R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 dual Q >> R \u27e7\n \u2192 \u27e6 P >> R \u27e7\n>>-compose end end p>>q q>>r = q>>r\n>>-compose end (send P) (m , p>>q) q>>r = >>-compose end (P m) p>>q (q>>r m)\n>>-compose end (recv P) p>>q (m , q>>r) = >>-compose end (P m) (p>>q m) q>>r\n>>-compose (send P) Q (m , p>>q) q>>r = m , >>-compose (P m) Q p>>q q>>r\n>>-compose (recv P) Q p>>q q>>r m = >>-compose (P m) Q (p>>q m) q>>r\n","old_contents":"{-# OPTIONS --without-K #-}\nopen import Function.NP\nopen import Type\nopen import Data.Product.NP using (\u03a3; _\u00d7_; _,_; first) renaming (proj\u2081 to fst; proj\u2082 to snd)\nopen import Data.Two hiding (_\u225f_)\nopen import Data.Nat hiding (_\u2294_)\nopen import Relation.Binary.PropositionalEquality.NP using (_\u2261_; _\u2219_; refl; ap; coe; coe!; !_ ; tr)-- renaming (subst to tr)\nopen import Function.Extensionality\nopen import HoTT\nopen Equivalences\nopen import Type.Identities\n\nopen import Control.Protocol.Core\n\nmodule Control.Protocol.Sequence where\n\ninfixl 1 _>>=_ _>>_\n\n_>>=_ : (P : Proto) \u2192 (Log P \u2192 Proto) \u2192 Proto\nend >>= Q = Q _\ncom q P >>= Q = com q \u03bb m \u2192 P m >>= \u03bb ms \u2192 Q (m , ms)\n\n_>>_ : Proto \u2192 Proto \u2192 Proto\nP >> Q = P >>= \u03bb _ \u2192 Q\n\n>>=-fst : \u2200 P {Q} \u2192 \u27e6 P >>= Q \u27e7 \u2192 \u27e6 P \u27e7\n>>=-fst end q = end\n>>=-fst (recv P) pq = \u03bb m \u2192 >>=-fst (P m) (pq m)\n>>=-fst (send P) (m , pq) = m , >>=-fst (P m) pq\n\n>>=-snd : \u2200 P {Q}(pq : \u27e6 P >>= Q \u27e7)(p : \u27e6 P \u22a5\u27e7) \u2192 \u27e6 Q (telecom P (>>=-fst P pq) p) \u27e7\n>>=-snd end q end = q\n>>=-snd (recv P) pq (m , p) = >>=-snd (P m) (pq m) p\n>>=-snd (send P) (m , pq) p = >>=-snd (P m) pq (p m)\n\n[_]_>>>=_ : \u2200 P {Q} \u2192 \u27e6 P \u27e7 \u2192 ((log : Log P) \u2192 \u27e6 Q log \u27e7) \u2192 \u27e6 P >>= Q \u27e7\n[ end ] p >>>= q = q _\n[ recv P ] p >>>= q = \u03bb m \u2192 [ P m ] p m >>>= \u03bb log \u2192 q (m , log)\n[ send P ] (m , p) >>>= q = m , [ P m ] p >>>= \u03bb log \u2192 q (m , log)\n\n[_]_>>>_ : \u2200 P {Q} \u2192 \u27e6 P \u27e7 \u2192 \u27e6 Q \u27e7 \u2192 \u27e6 P >> Q \u27e7\n[ P ] p >>> q = [ P ] p >>>= \u03bb _ \u2192 q\n\nmodule _ {{_ : FunExt}} where\n >>=-fst-inv : \u2200 P {Q}(p : \u27e6 P \u27e7)(q : ((log : Log P) \u2192 \u27e6 Q log \u27e7)) \u2192 >>=-fst P {Q} ([ P ] p >>>= q) \u2261 p\n >>=-fst-inv end end q = refl\n >>=-fst-inv (recv P) p q = \u03bb= \u03bb m \u2192 >>=-fst-inv (P m) (p m) \u03bb log \u2192 q (m , log)\n >>=-fst-inv (send P) (m , p) q = snd= (>>=-fst-inv (P m) p \u03bb log \u2192 q (m , log))\n\n >>=-snd-inv : \u2200 P {Q}(p : \u27e6 P \u27e7)(q : ((log : Log P) \u2192 \u27e6 Q log \u27e7))(p' : \u27e6 P \u22a5\u27e7)\n \u2192 tr (\u03bb x \u2192 \u27e6 Q (telecom P x p') \u27e7) (>>=-fst-inv P p q)\n (>>=-snd P {Q} ([ P ] p >>>= q) p') \u2261 q (telecom P p p')\n >>=-snd-inv end end q p' = refl\n >>=-snd-inv (recv P) {Q} p q (m , p') =\n ap (flip _$_ (>>=-snd (P m) ([ P m ] p m >>>= (\u03bb log \u2192 q (m , log))) p'))\n (tr-\u03bb= (\u03bb z \u2192 \u27e6 Q (m , telecom (P m) z p') \u27e7)\n (\u03bb m \u2192 >>=-fst-inv (P m) {Q \u2218 _,_ m} (p m) (q \u2218 _,_ m)))\n \u2219 >>=-snd-inv (P m) {Q \u2218 _,_ m} (p m) (q \u2218 _,_ m) p'\n >>=-snd-inv (send P) {Q} (m , p) q p' = tr-snd= (\u03bb { (m , p) \u2192 \u27e6 Q (m , telecom (P m) p (p' m)) \u27e7 })\n (>>=-fst-inv (P m) p (q \u2218 _,_ m))\n (>>=-snd (P m) {Q \u2218 _,_ m} ([ P m ] p >>>= (q \u2218 _,_ m)) (p' m))\n \u2219 >>=-snd-inv (P m) {Q \u2218 _,_ m} p (\u03bb log \u2192 q (m , log)) (p' m)\n\n {- hmmm...\n >>=-uniq : \u2200 P {Q} (pq : \u27e6 P >>= Q \u27e7)(p' : \u27e6 P \u22a5\u27e7) \u2192 pq \u2261 [ P ] (>>=-fst P {Q} pq) >>>= (\u03bb log \u2192 {!>>=-snd P {Q} pq p'!})\n >>=-uniq = {!!}\n -}\n\nreplicate\u1d3e : \u2115 \u2192 Proto \u2192 Proto\nreplicate\u1d3e 0 P = end\nreplicate\u1d3e (suc n) P = P >> replicate\u1d3e n P\n\nreplicate-proc : \u2200 n P \u2192 \u27e6 P \u27e7 \u2192 \u27e6 replicate\u1d3e n P \u27e7\nreplicate-proc zero P p = end\nreplicate-proc (suc n) P p = [ P ] p >>> replicate-proc n P p\n\nmodule _ {{_ : FunExt}}{{_ : UA}} where\n Log->>=-\u03a3 : \u2200 (P : Proto){Q} \u2192 Log (P >>= Q) \u2261 \u03a3 (Log P) (Log \u2218 Q)\n Log->>=-\u03a3 end = ! (\u00d7= End-uniq refl \u2219 \ud835\udfd9\u00d7-snd)\n Log->>=-\u03a3 (com _ P) = \u03a3=\u2032 _ (\u03bb m \u2192 Log->>=-\u03a3 (P m)) \u2219 \u03a3-assoc\n\n Log->>-\u00d7 : \u2200 (P : Proto){Q} \u2192 Log (P >> Q) \u2261 (Log P \u00d7 Log Q)\n Log->>-\u00d7 P = Log->>=-\u03a3 P\n\n ++Log' : \u2200 (P : Proto){Q : Log P \u2192 Proto} (xs : Log P) \u2192 Log (Q xs) \u2192 Log (P >>= Q)\n ++Log' P p q = coe! (Log->>=-\u03a3 P) (p , q)\n\n-- Same as ++Log' but computes better\n++Log : \u2200 (P : Proto){Q : Log P \u2192 Proto} (xs : Log P) \u2192 Log (Q xs) \u2192 Log (P >>= Q)\n++Log end _ ys = ys\n++Log (com q P) (x , xs) ys = x , ++Log (P x) xs ys\n\nmodule _ {{_ : FunExt}}{{_ : UA}} where\n\nmodule _ {{_ : FunExt}} where\n >>-right-unit : \u2200 P \u2192 (P >> end) \u2261 P\n >>-right-unit end = refl\n >>-right-unit (com q P) = com= refl refl \u03bb m \u2192 >>-right-unit (P m)\n\n >>=-assoc : \u2200 (P : Proto)(Q : Log P \u2192 Proto)(R : Log (P >>= Q) \u2192 Proto)\n \u2192 (P >>= (\u03bb x \u2192 Q x >>= (\u03bb y \u2192 R (++Log P x y)))) \u2261 ((P >>= Q) >>= R)\n >>=-assoc end Q R = refl\n >>=-assoc (com q P) Q R = com= refl refl \u03bb m \u2192 >>=-assoc (P m) (\u03bb ms \u2192 Q (m , ms)) (\u03bb ms \u2192 R (m , ms))\n\n ap->>= : \u2200 P {Q\u2080 Q\u2081} \u2192 (\u2200 {log} \u2192 \u27e6 Q\u2080 log \u27e7 \u2261 \u27e6 Q\u2081 log \u27e7) \u2192 \u27e6 P >>= Q\u2080 \u27e7 \u2261 \u27e6 P >>= Q\u2081 \u27e7\n ap->>= end Q= = Q=\n ap->>= (send P) Q= = \u03a3=\u2032 _ \u03bb m \u2192 ap->>= (P m) Q=\n ap->>= (recv P) Q= = \u03a0=\u2032 _ \u03bb m \u2192 ap->>= (P m) Q=\n\n dual->> : \u2200 P Q \u2192 dual (P >> Q) \u2261 (dual P >> dual Q)\n dual->> end Q = refl\n dual->> (com io P) Q = com= refl refl \u03bb m \u2192 dual->> (P m) Q\n\n {- My coe-ap-fu is lacking...\n dual->>= : \u2200 P (Q : Log P \u2192 Proto) \u2192 dual (P >>= Q) \u2261 dual P >>= (dual \u2218 Q \u2218 coe (dual-Log P))\n dual->>= end Q = refl\n dual->>= (com io P) Q = com= refl refl \u03bb m \u2192 dual->>= (P m) (Q \u2218 _,_ m) \u2219 ap (_>>=_ (dual (P m))) (\u03bb= \u03bb ms \u2192 ap (\u03bb x \u2192 dual (Q x)) (pair= {!!} {!!}))\n -}\n\n dual-replicate\u1d3e : \u2200 n P \u2192 dual (replicate\u1d3e n P) \u2261 replicate\u1d3e n (dual P)\n dual-replicate\u1d3e zero P = refl\n dual-replicate\u1d3e (suc n) P = dual->> P (replicate\u1d3e n P) \u2219 ap (_>>_ (dual P)) (dual-replicate\u1d3e n P)\n\n{- An incremental telecom function which makes processes communicate\n during a matching initial protocol. -}\n>>=-telecom : (P : Proto){Q : Log P \u2192 Proto}{R : Log P \u2192 Proto}\n \u2192 \u27e6 P >>= Q \u27e7\n \u2192 \u27e6 P >>= R \u22a5\u27e7\n \u2192 \u03a3 (Log P) (\u03bb t \u2192 \u27e6 Q t \u27e7 \u00d7 \u27e6 R t \u22a5\u27e7)\n>>=-telecom end p0 p1 = _ , p0 , p1\n>>=-telecom (send P) (m , p0) p1 = first (_,_ m) (>>=-telecom (P m) p0 (p1 m))\n>>=-telecom (recv P) p0 (m , p1) = first (_,_ m) (>>=-telecom (P m) (p0 m) p1)\n\n>>-telecom : (P : Proto){Q R : Proto}\n \u2192 \u27e6 P >> Q \u27e7\n \u2192 \u27e6 P >> R \u22a5\u27e7\n \u2192 Log P \u00d7 \u27e6 Q \u27e7 \u00d7 \u27e6 R \u22a5\u27e7\n>>-telecom P p q = >>=-telecom P p q\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"d0326164e3270519a7229d44abdf050a074562b9","subject":"NaPa: Add precedence annotations","message":"NaPa: Add precedence annotations\n","repos":"np\/NomPa","old_file":"lib\/NaPa.agda","new_file":"lib\/NaPa.agda","new_contents":"{-# OPTIONS --universe-polymorphism #-}\n-- vim: iskeyword=a-z,A-Z,48-57,-,+,',#,\/,~,],[,=,_,`,\u2286\nmodule NaPa where\n\nimport Level as L\n-- open import Irrelevance\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261 using (_\u2261_; _\u2262_; _\u2257_; module \u2257-Reasoning; module \u2261-Reasoning)\nopen import Data.Nat.NP as Nat using (\u2115; zero; suc; s\u2264s; z\u2264n; \u2264-pred; pred; _<=_; module <=)\n renaming (_+_ to _+\u2115_ ; _\u2238_ to _\u2238\u2115_ ; _==_ to _==\u2115_; \u00ac\u2264 to \u00ac\u2264\u2115;\n _<_ to _<\u2115_ ; _\u2264_ to _\u2264\u2115_; _\u2265_ to _\u2265\u2115_; _\u2264?_ to _\u2264?\u2115_)\nopen import Data.Nat.Logical using (\u27e6\u2115\u27e7; zero; suc; \u27e6\u2115\u27e7-setoid; \u27e6\u2115\u27e7-equality; \u27e6\u2115\u27e7-cong)\n renaming (_\u225f_ to _\u225f\u27e6\u2115\u27e7_)\nimport Data.Nat.Properties as Nat\nopen import Function\nopen import Function.Equality as \u27f6\u2261 using (_\u27f6_; _\u27e8$\u27e9_)\nopen import Data.Sum.NP as Sum using (_\u228e_; inl; inr; [_,_]\u2032) renaming (map to \u228e-map)\nopen import Data.Bool using (Bool; true; false; if_then_else_; T; not)\nopen import Data.Two renaming (\u2713-not-\u00ac to T'not'\u00ac)\nopen import Data.Unit using (\u22a4)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product.NP using (_,_)\nopen import Data.Maybe.NP using (Maybe; nothing; just; maybe; _\u2192?_)\nopen import Relation.Binary.NP as Bin\nopen import Relation.Nullary\nopen import Relation.Nullary.Decidable\nimport Relation.Binary.On as On\nopen import NomPa.Worlds\nopen import NomPa.Worlds.Syntax as WorldSyntax\nopen import NomPa.Subtyping\nopen ListBoolWorlds public hiding (_\u2286_) renaming (_\u2286\u2032_ to _\u2286_)\nimport Category.Monad as Cat\nopen Cat.RawMonad (Data.Maybe.NP.monad {L.zero})\nopen Data.Maybe.NP.FunctorLemmas\n\nprivate\n module \u2115s = Setoid \u27e6\u2115\u27e7-setoid\n module \u2115e = Equality \u27e6\u2115\u27e7-equality\n\ninfixr 4 _,_\nrecord Name \u03b1 : Set where\n constructor _,_\n field\n name : \u2115\n -- .poofname\u2208\u03b1 : name \u2208 \u03b1\n name\u2208\u03b1 : name \u2208 \u03b1\n\nopen Name public\n{-\ndata Name \u03b1 : Set where\n _,_ : (x : \u2115) \u2192 .(x \u2208 \u03b1) \u2192 Name \u03b1\nname : \u2200 {\u03b1} (x : Name \u03b1) \u2192 \u2115\nname (x , _) = x\n\n.name\u2208\u03b1 : \u2200 {\u03b1} (x : Name \u03b1) \u2192 name x \u2208 \u03b1\nname\u2208\u03b1 (_ , pf) = pf\n-}\n\n--mkName : \u2200 {\u03b1} (x : \u2115) \u2192 Irr (x \u2208 \u03b1) \u2192 Name \u03b1\n--mkName x (irr pf) = x , pf\nmkName : \u2200 {\u03b1} (x : \u2115) \u2192 x \u2208 \u03b1 \u2192 Name \u03b1\nmkName x pf = x , pf\n\n--name\u2208\u03b1 : \u2200 {\u03b1} (x : Name \u03b1) \u2192 Irr (name x \u2208 \u03b1)\n--name\u2208\u03b1 (_ , pf) = irr pf\n\ninfix 4 _\u2261\u1d3a_ _\u225f\u1d3a_ _==\u1d3a_\n\n-- prim\n_\u2261\u1d3a_ : \u2200 {\u03b1} (x y : Name \u03b1) \u2192 Set\n_\u2261\u1d3a_ = \u27e6\u2115\u27e7 on name\n\n-- prim\n_\u225f\u1d3a_ : \u2200 {\u03b1} \u2192 Decidable (_\u2261\u1d3a_ {\u03b1})\nx \u225f\u1d3a y = name x \u225f\u27e6\u2115\u27e7 name y\n\n-- prim\n_==\u1d3a_ : \u2200 {\u03b1} (x y : Name \u03b1) \u2192 Bool\n_==\u1d3a_ x y = name x ==\u2115 name y\n\nname-injective : \u2200 {\u03b1} {x y : Name \u03b1} \u2192 name x \u2261 name y \u2192 x \u2261 y\nname-injective {\u03b1} {_ , p\u2081} {a , p\u2082} eq rewrite eq\n--= \u2261.refl {_} {_} {a , p\u2082}\n = \u2261.cong (_,_ a) (\u2208-uniq \u03b1 p\u2081 p\u2082)\n\nNm : World \u2192 Setoid L.zero L.zero\nNm \u03b1 = On.setoid \u27e6\u2115\u27e7-setoid (name {\u03b1})\n\n\u2261\u1d3a-equality : \u2200 {\u03b1} \u2192 Equality (_\u2261\u1d3a_ {\u03b1})\n\u2261\u1d3a-equality {\u03b1}\n = record { isEquivalence = Setoid.isEquivalence (Nm \u03b1)\n -- I would prefered not going to PropEq.\u2261\n ; subst = \u03bb P \u2192 \u2261.tr P \u2218 name-injective \u2218 Equality.to-propositional \u27e6\u2115\u27e7-equality }\n\nmodule NmEq {\u03b1} = Equality (\u2261\u1d3a-equality {\u03b1})\n\n\u2261\u1d3a\u21d2\u2261 : \u2200 {\u03b1} \u2192 _\u2261\u1d3a_ {\u03b1} \u21d2 _\u2261_\n\u2261\u1d3a\u21d2\u2261 = NmEq.to-propositional\n\n\u2261\u1d3a-cong : \u2200 {\u03b1 \u03b2} (f : Name \u03b1 \u2192 Name \u03b2) \u2192 _\u2261\u1d3a_ =[ f ]\u21d2 _\u2261\u1d3a_\n\u2261\u1d3a-cong f = NmEq.reflexive \u2218 \u2261.cong f \u2218 \u2261\u1d3a\u21d2\u2261\n\n\u2261\u1d3a-\u27e6\u2115\u27e7-cong : \u2200 {\u03b1} (f : Name \u03b1 \u2192 \u2115) \u2192 _\u2261\u1d3a_ =[ f ]\u21d2 \u27e6\u2115\u27e7\n\u2261\u1d3a-\u27e6\u2115\u27e7-cong f = \u2115e.reflexive \u2218 \u2261.cong f \u2218 \u2261\u1d3a\u21d2\u2261\n\n\u2261-\u27e6\u2115\u27e7-cong : \u2200 {a} {A : Set a} (f : A \u2192 \u2115) \u2192 _\u2261_ =[ f ]\u21d2 \u27e6\u2115\u27e7\n\u2261-\u27e6\u2115\u27e7-cong f = \u2115e.reflexive \u2218 \u2261.cong f\n\n\u2261\u1d3a-\u2261-cong : \u2200 {a \u03b1} {A : Set a} (f : Name \u03b1 \u2192 A) \u2192 _\u2261\u1d3a_ =[ f ]\u21d2 _\u2261_\n\u2261\u1d3a-\u2261-cong f = \u2261.cong f \u2218 \u2261\u1d3a\u21d2\u2261\n\n\u27e6\u2115\u27e7-\u2261\u1d3a-cong : \u2200 {\u03b1} (f : \u2115 \u2192 Name \u03b1) \u2192 \u27e6\u2115\u27e7 =[ f ]\u21d2 _\u2261\u1d3a_\n\u27e6\u2115\u27e7-\u2261\u1d3a-cong f = NmEq.reflexive \u2218 \u2261.cong f \u2218 \u2115e.to-propositional\n\n\u27e6\u2115\u27e7-\u2261-cong : \u2200 {a} {A : Set a} (f : \u2115 \u2192 A) \u2192 \u27e6\u2115\u27e7 =[ f ]\u21d2 _\u2261_\n\u27e6\u2115\u27e7-\u2261-cong f = \u2261.cong f \u2218 \u2115e.to-propositional\n\nmodule \u2261\u1d3a-Reasoning {\u03b1} = Bin.Setoid-Reasoning (Nm \u03b1)\n\nopen WorldSymantics listBoolWorlds public\nopen WorldOps listBoolWorlds public\nopen WorldOps listBoolWorlds using ()\nopen NomPa.Subtyping.\u2286-Pack \u2286\u2032\u1d47-pack public hiding (_\u2286_; _\u2288_)\nopen NomPa.Subtyping.SyntacticOnBools \u2286\u2032\u1d47-pack public hiding (minimalSymantics)\n\n-- Here is a first set of core primitives easy to define\n\n-- prim\nzero\u1d3a : \u2200 {\u03b1} \u2192 Name (\u03b1 \u21911)\nzero\u1d3a = 0 , _\n\n-- prim\n\u00acName\u00f8 : \u00ac(Name \u00f8)\n\u00acName\u00f8 (_ , ())\n\n-- prim\nadd\u1d3a : \u2200 {\u03b1} k (x : Name \u03b1) \u2192 Name (\u03b1 +\u1d42 k)\nadd\u1d3a {\u03b1} k x = k +\u2115 name x , proof k where\n proof : \u2200 k \u2192 k +\u2115 name x \u2208 \u03b1 +\u1d42 k\n proof zero = name\u2208\u03b1 x\n proof (suc k) = proof k\n\ninfixl 6 add\u1d3a\nsyntax add\u1d3a k x = x +\u1d3a k\n\n-- prim\nsubtract\u1d3a : \u2200 {\u03b1} k (x : Name (\u03b1 +\u1d42 k)) \u2192 Name \u03b1\nsubtract\u1d3a k x = name x \u2238\u2115 k , proof (name x) k (name\u2208\u03b1 x) where\n proof : \u2200 {\u03b1} x k \u2192 (x \u2208 \u03b1 +\u1d42 k) \u2192 (x \u2238\u2115 k \u2208 \u03b1)\n proof x zero = id\n proof zero (suc k) = \u03bb()\n proof (suc x) (suc k) = proof x k\n\ninfixl 4 subtract\u1d3a\nsyntax subtract\u1d3a k x = x \u2238\u1d3a k\n\n-- prim\ncoerce\u1d3a : \u2200 {\u03b1 \u03b2} \u2192 \u03b1 \u2286 \u03b2 \u2192 Name \u03b1 \u2192 Name \u03b2\ncoerce\u1d3a \u03b1\u2286\u03b2 x = name x , coe \u03b1\u2286\u03b2 (name x) (name\u2208\u03b1 x)\n\n-- Then we define some convenience functions on top of them.\n\ninfix 0 _\u27e8-because_-\u27e9\n_\u27e8-because_-\u27e9 : \u2200 {\u03b1 \u03b2} \u2192 Name \u03b1 \u2192 \u03b1 \u2286 \u03b2 \u2192 Name \u03b2\n_\u27e8-because_-\u27e9 n pf = coerce\u1d3a pf n\n\nadd\u1d3a\u2191 : \u2200 {\u03b1} \u2113 \u2192 Name \u03b1 \u2192 Name (\u03b1 \u2191 \u2113)\nadd\u1d3a\u2191 \u2113 x = add\u1d3a \u2113 x \u27e8-because \u2286-+-\u2191 \u2113 -\u27e9\n\nsuc\u1d3a : \u2200 {\u03b1} \u2192 Name \u03b1 \u2192 Name (\u03b1 +1)\nsuc\u1d3a = add\u1d3a 1\n\nsuc\u1d3a\u2191 : \u2200 {\u03b1} \u2192 Name \u03b1 \u2192 Name (\u03b1 \u21911)\nsuc\u1d3a\u2191 = add\u1d3a\u2191 1\n\n-- Then comes <\u1d3a and the necessary intermediate definitions\n\nprivate\n lem-<\u2113 : \u2200 {\u03b1 \u03b2 x \u2113} \u2192 x \u2264\u2115 \u2113 \u2192 x \u2208 (\u03b1 \u2191 suc \u2113) \u2192 x \u2208 (\u03b2 \u2191 suc \u2113)\n lem-<\u2113 z\u2264n = _\n lem-<\u2113 (s\u2264s m\u2264n) = lem-<\u2113 m\u2264n\n\n lem-\u2265\u2113 : \u2200 {\u03b1 x \u2113} \u2192 x \u2265\u2115 \u2113 \u2192 x \u2208 \u03b1 \u2191 \u2113 \u2192 x \u2208 \u03b1 +\u1d42 \u2113\n lem-\u2265\u2113 z\u2264n = id\n lem-\u2265\u2113 (s\u2264s m\u2264n) = lem-\u2265\u2113 m\u2264n\n\n lem-<\u2113? : \u2200 {\u03b1 \u03b2 n} \u2113 \u2192 n \u2208 \u03b2 \u2191 \u2113 \u2192 n \u2208 (if suc n <= \u2113 then \u03b1 \u2191 \u2113 else \u03b2 +\u1d42 \u2113)\n lem-<\u2113? {n = n} \u2113 with suc n <= \u2113 | <=.sound (suc n) \u2113 | <=.complete {suc n} {\u2113}\n ... | true | n<\u2113 | _ = lem-<\u2113 (n<\u2113 _)\n ... | false | _ | \u00acn<\u2113 = lem-\u2265\u2113 (\u00ac\u2264\u2115 \u00acn<\u2113)\n\n-- prim\ncmp\u1d3a-bool : \u2200 {\u03b1} \u2113 \u2192 Name (\u03b1 \u2191 \u2113) \u2192 Bool\ncmp\u1d3a-bool \u2113 x = suc (name x) <= \u2113\n\nsyntax cmp\u1d3a-bool k x = x <\u1d3a-bool k\n\n-- prim\ncmp\u1d3a-name : \u2200 {\u03b1} \u2113 (x : Name (\u03b1 \u2191 \u2113)) \u2192 Name (if cmp\u1d3a-bool \u2113 x then \u00f8 \u2191 \u2113 else \u03b1 +\u1d42 \u2113)\ncmp\u1d3a-name \u2113 x = name x , lem-<\u2113? \u2113 (name\u2208\u03b1 x)\n\nsyntax cmp\u1d3a-name \u2113 x = x <\u1d3a-name \u2113\n\n-- prim\ncmp\u1d3a-name\u2208\u03b1 : \u2200 {\u03b1} \u2113 (x : Name (\u03b1 \u2191 \u2113)) \u2192 name x \u2208 (if cmp\u1d3a-bool \u2113 x then \u00f8 \u2191 \u2113 else \u03b1 +\u1d42 \u2113)\ncmp\u1d3a-name\u2208\u03b1 \u2113 x = lem-<\u2113? \u2113 (name\u2208\u03b1 x)\n\nsyntax cmp\u1d3a-name\u2208\u03b1 \u2113 x = x <\u1d3a-name\u2208\u03b1 \u2113\n\n-- prim\ncmp\u1d3a : \u2200 {\u03b1} \u2113 \u2192 Name (\u03b1 \u2191 \u2113) \u2192 Name (\u00f8 \u2191 \u2113) \u228e Name (\u03b1 +\u1d42 \u2113)\ncmp\u1d3a \u2113 x with cmp\u1d3a-bool \u2113 x | cmp\u1d3a-name\u2208\u03b1 \u2113 x\n... | true | pf = inl (name x , pf)\n... | false | pf = inr (name x , pf)\n\ninfix 4 cmp\u1d3a\nsyntax cmp\u1d3a k x = x <\u1d3a k\n\ncmp\u1d3a-ind : \u2200 {a \u03b1} \u2113 (x : Name (\u03b1 \u2191 \u2113))\n (P : (Name (\u00f8 \u2191 \u2113) \u228e Name (\u03b1 +\u1d42 \u2113)) \u2192 Set a)\n (Pinl : T (cmp\u1d3a-bool \u2113 x) \u2192 \u2200 pf \u2192 P (inl (name x , pf)))\n (Pinr : T (not (cmp\u1d3a-bool \u2113 x)) \u2192 \u2200 pf \u2192 P (inr (name x , pf)))\n \u2192 P (cmp\u1d3a \u2113 x)\ncmp\u1d3a-ind \u2113 x P p\u2081 p\u2082\n with cmp\u1d3a-bool \u2113 x | cmp\u1d3a-name\u2208\u03b1 \u2113 x\n... | true | pf = p\u2081 _ pf\n... | false | pf = p\u2082 _ pf\n\neasy-cmp\u1d3a : \u2200 {\u03b1} \u2113 \u2192 Name (\u03b1 \u2191 \u2113) \u2192 Name (\u00f8 \u2191 \u2113) \u228e Name (\u03b1 +\u1d42 \u2113)\neasy-cmp\u1d3a zero x = inr x\neasy-cmp\u1d3a (suc _) (zero , _) = inl (zero , _)\neasy-cmp\u1d3a (suc \u2113) (suc x , pf) = \u228e-map suc\u1d3a\u2191 suc\u1d3a (easy-cmp\u1d3a \u2113 (x , pf))\n\nsyntax easy-cmp\u1d3a \u2113 x = x <\u1d3a-easy \u2113\n\neasy-cmp\u1d3a\u2257cmp\u1d3a : \u2200 {\u03b1} \u2113 \u2192 easy-cmp\u1d3a {\u03b1} \u2113 \u2257 cmp\u1d3a \u2113\neasy-cmp\u1d3a\u2257cmp\u1d3a zero x = \u2261.cong inr (name-injective \u2261.refl)\neasy-cmp\u1d3a\u2257cmp\u1d3a (suc n) (zero , _) = \u2261.refl\neasy-cmp\u1d3a\u2257cmp\u1d3a {\u03b1} (suc n) (suc x , pf) rewrite easy-cmp\u1d3a\u2257cmp\u1d3a n (x , pf) = helper n x pf\n where\n helper : \u2200 \u2113 x pf \u2192 \u228e-map suc\u1d3a\u2191 suc\u1d3a (cmp\u1d3a {\u03b1} \u2113 (x , pf)) \u2261 cmp\u1d3a (suc \u2113) (suc x , pf)\n helper \u2113 x pf = cmp\u1d3a-ind \u2113 (x , pf) (\u03bb r \u2192 \u228e-map suc\u1d3a\u2191 suc\u1d3a r \u2261 cmp\u1d3a (suc \u2113) (suc x , pf))\n (\u03bb h pf' \u2192 cmp\u1d3a-ind (suc \u2113) (suc x , pf) (\u03bb r \u2192 inl (suc x , pf') \u2261 r)\n (\u03bb _ _ \u2192 \u2261.cong inl (name-injective \u2261.refl))\n (\u03bb h' \u2192 \u22a5-elim (T'not'\u00ac h' h)))\n (\u03bb h pf' \u2192 cmp\u1d3a-ind (suc \u2113) (suc x , pf) (\u03bb r \u2192 inr (suc x , pf') \u2261 r)\n (\u03bb h' \u2192 \u22a5-elim (T'not'\u00ac h h'))\n (\u03bb _ _ \u2192 \u2261.cong inr (name-injective \u2261.refl)))\n\n-- prim (could be defined with (cmp\u1d3a 1)\nprotect\u21911 : \u2200 {\u03b1 \u03b2} \u2192 (Name \u03b1 \u2192 Name \u03b2) \u2192 Name (\u03b1 \u21911) \u2192 Name (\u03b2 \u21911)\nprotect\u21911 f x with name x | {-irr-} (name\u2208\u03b1 x)\n... | zero | _ = zero , _\n... | suc m | pf = suc\u1d3a\u2191 (f (m , {-cert-} pf))\n\n-- mkName; _,_; name; name\u2208\u03b1 are forbidden from now on (except in proofs).\n\nzero\u1d3a\u21911+ : \u2200 {\u03b1} \u2113 \u2192 Name (\u03b1 \u2191 (1 +\u2115 \u2113))\nzero\u1d3a\u21911+ _ = zero\u1d3a\n\ninfix 10 _\u1d3a\n_\u1d3a : \u2200 {\u03b1} \u2113 \u2192 Name (\u03b1 \u2191 suc \u2113)\n_\u1d3a {\u03b1} \u2113 = zero\u1d3a +\u1d3a \u2113 \u27e8-because (\u03b1 \u21911 +\u1d42 \u2113) \u2286\u27e8 \u2286-+-\u2191 \u2113 \u27e9\n \u03b1 \u21911 \u2191 \u2113 \u2286\u27e8 \u2286-exch-\u2191-\u2191 \u2286-refl 1 \u2113 \u27e9\n \u03b1 \u2191 suc \u2113 \u220e -\u27e9\n where open \u2286-Reasoning\n\n-- Handy name eliminator\nsubtractWith\u1d3a : \u2200 {a \u03b1} {A : Set a} \u2113 \u2192 A \u2192 (Name \u03b1 \u2192 A) \u2192 Name (\u03b1 \u2191 \u2113) \u2192 A\nsubtractWith\u1d3a \u2113 v f x = [ const v , f \u2218\u2032 subtract\u1d3a \u2113 ]\u2032 (x <\u1d3a \u2113)\n\nsubtract\u1d3a? : \u2200 {\u03b1} \u2113 \u2192 Name (\u03b1 \u2191 \u2113) \u2192? Name \u03b1\nsubtract\u1d3a? \u2113 = subtractWith\u1d3a \u2113 nothing just\n\ninfixl 4 subtract\u1d3a?\nsyntax subtract\u1d3a? k x = x \u2238\u1d3a? k\n\nprivate\n module CoeExamples where\n Name\u03b1+\u2192Name\u03b1\u2191 : \u2200 {\u03b1} k \u2192 Name (\u03b1 +\u1d42 k) \u2192 Name (\u03b1 \u2191 k)\n Name\u03b1+\u2192Name\u03b1\u2191 k = coerce\u1d3a (\u2286-ctx-+\u2191 \u2286-refl k)\n module Unused where\n open import Data.List using ([]; _\u2237_; replicate; _++_)\n n\u2208true\u207f\u207a\u00b9 : \u2200 {\u03b1} n \u2192 n \u2208 replicate (suc n) true ++ \u03b1\n n\u2208true\u207f\u207a\u00b9 zero = _\n n\u2208true\u207f\u207a\u00b9 (suc n) = n\u2208true\u207f\u207a\u00b9 n\n\npred\u1d3a : \u2200 {\u03b1} \u2192 Name (\u03b1 +1) \u2192 Name \u03b1\npred\u1d3a = subtract\u1d3a 1\n\npred\u1d3a? : \u2200 {\u03b1} \u2192 Name (\u03b1 \u21911) \u2192? Name \u03b1\npred\u1d3a? = subtract\u1d3a? 1\n\nName\u2192-to-Nm\u27f6 : \u2200 {b\u2081 b\u2082 \u03b1} {B : Setoid b\u2081 b\u2082} \u2192\n (Name \u03b1 \u2192 Setoid.Carrier B) \u2192 Nm \u03b1 \u27f6 B\nName\u2192-to-Nm\u27f6 {\u03b1 = \u03b1} {B} f = record { _\u27e8$\u27e9_ = f; cong = cong\u2032 }\n where\n open Setoid B renaming (_\u2248_ to _\u2248B_)\n\n cong\u2261 : \u2200 {x y} \u2192 x \u2261 y \u2192 f x \u2248B f y\n cong\u2261 \u2261.refl = Setoid.refl B\n\n cong\u2032 : \u2200 {x y} \u2192 x \u2261\u1d3a y \u2192 f x \u2248B f y\n cong\u2032 = cong\u2261 \u2218 Equality.to-propositional \u2261\u1d3a-equality\n -- cong\u2032 = Setoid.reflexive \u2218 Equality.to-propositional \u2261\u1d3a-equality\n\n\u00acName : \u2200 {\u03b1} \u2192 \u03b1 \u2286 \u00f8 \u2192 \u00ac(Name \u03b1)\n\u00acName \u03b1\u2286\u00f8 = \u00acName\u00f8 \u2218 coerce\u1d3a \u03b1\u2286\u00f8\n\nName-elim : \u2200 {a} {A : Set a} {\u03b1} (\u03b1\u2286\u00f8 : \u03b1 \u2286 \u00f8) (x : Name \u03b1) \u2192 A\nName-elim x y with \u00acName x y\n... | ()\n\nName\u00f8-elim : \u2200 {a} {A : Set a} \u2192 Name \u00f8 \u2192 A\nName\u00f8-elim = Name-elim \u2286-refl\n\n\u00acName\u00f8+ : \u2200 k \u2192 \u00ac(Name (\u00f8 +\u1d42 k))\n\u00acName\u00f8+ = \u00acName \u2218 \u2286-\u00f8+\n\nName\u00f8+-elim : \u2200 {a} {A : Set a} k \u2192 Name (\u00f8 +\u1d42 k) \u2192 A\nName\u00f8+-elim = Name-elim \u2218 \u2286-\u00f8+\n\nName\u00f8\u2191\u2192Name\u03b1\u2191 : \u2200 {\u2113} \u2192 Name (\u00f8 \u2191 \u2113) \u2192 \u2200 {\u03b1} \u2192 Name (\u03b1 \u2191 \u2113)\nName\u00f8\u2191\u2192Name\u03b1\u2191 {\u2113} x = x \u27e8-because \u2286-cong-\u2191 \u2286-\u00f8 \u2113 -\u27e9\n\nis0? : \u2200 {\u03b1} \u2192 Name (\u03b1 \u21911) \u2192 Bool\nis0? = cmp\u1d3a-bool 1\n-- is0? = [ const true , const false ]\u2032 (x <\u1d3a 1)\n\nshift\u2115 : \u2200 (\u2113 k n : \u2115) \u2192 \u2115\nshift\u2115 \u2113 k n = if suc n <= \u2113 then n else n +\u2115 k\n\nshift\u1d3a : \u2200 {\u03b1 \u03b2} \u2113 k \u2192 \u03b1 +\u1d42 k \u2286 \u03b2 \u2192 Name (\u03b1 \u2191 \u2113) \u2192 Name (\u03b2 \u2191 \u2113)\nshift\u1d3a \u2113 k pf n\n with n <\u1d3a \u2113\n... | inl n\u2032 = n\u2032 \u27e8-because \u2286-cong-\u2191 \u2286-\u00f8 \u2113 -\u27e9\n... | inr n\u2032 = n\u2032 +\u1d3a k \u27e8-because \u2286-trans (\u2286-exch-+-+ \u2286-refl \u2113 k) (\u2286-ctx-+\u2191 pf \u2113) -\u27e9\n\nmodule Singletons where\n\n SWorld : \u2115 \u2192 World\n SWorld n = \u00f8 \u21911 +\u1d42 n\n\n Name\u02e2 : \u2115 \u2192 Set\n Name\u02e2 = Name \u2218 SWorld\n\n _\u02e2 : \u2200 n \u2192 Name\u02e2 n\n _\u02e2 n = zero\u1d3a +\u1d3a n\n\n add\u02e2 : \u2200 {n} k \u2192 Name\u02e2 n \u2192 Name\u02e2 (k +\u2115 n)\n add\u02e2 {n} k x = add\u1d3a k x \u27e8-because \u2286-assoc-+ \u2286-refl n k -\u27e9\n\n subtract\u02e2 : \u2200 {n} k \u2192 Name\u02e2 (k +\u2115 n) \u2192 Name\u02e2 n\n subtract\u02e2 {n} k x = subtract\u1d3a k (x \u27e8-because \u2286-assoc-+\u2032 \u2286-refl n k -\u27e9)\n\n{-\nshift\u1d3a-1-1+-\n shift\u1d3a 1 (suc k) pf n \u2261 shift\u1d3a 1 k ? (shift\u1d3a 1 1 ? n)\n-}\n\n-- Properties\n\ncoerce\u1d3a-names : \u2200 {\u03b1 \u03b2} (\u03b1\u2286\u03b2 : \u03b1 \u2286 \u03b2) \u2192 name \u2218 coerce\u1d3a \u03b1\u2286\u03b2 \u2257 name\ncoerce\u1d3a-names _ _ = \u2261.refl\n\n{-\n<\u1d3a-coerce : \u2200 {\u03b1} \u2113 (x : Name (\u03b1 \u2191 \u2113)) \u2192 [ coerce\u1d3a (\u2286-cong-\u2191 \u2286-\u00f8 \u2113) , coerce\u1d3a (\u2286-ctx-+\u2191 \u2286-refl \u2113) ]\u2032 (x <\u1d3a \u2113) \u2261 x\n<\u1d3a-coerce \u2113 x with x <\u1d3a-bool \u2113 | x <\u1d3a-name\u2208\u03b1 \u2113\n... | true | _ = name-injective \u2261.refl\n... | false | _ = name-injective \u2261.refl\n-}\n\n<\u1d3a-names : \u2200 {\u03b1 \u2113} {x : Name (\u03b1 \u2191 \u2113)} \u2192 [ name , name ]\u2032 (x <\u1d3a \u2113) \u2261 name x\n<\u1d3a-names {\u03b1} {\u2113} {x} with x <\u1d3a-bool \u2113 | x <\u1d3a-name\u2208\u03b1 \u2113\n... | true | _ = \u2261.refl\n... | false | _ = \u2261.refl\n\nDec-\u2261\u1d3a\u2192\u2261 : \u2200 {\u03b1} \u2192 Decidable (_\u2261\u1d3a_ {\u03b1}) \u2192 Decidable _\u2261_\nDec-\u2261\u1d3a\u2192\u2261 _\u225f_ x y with x \u225f y\n... | yes x\u2261y = yes (\u2261\u1d3a\u21d2\u2261 x\u2261y)\n... | no x\u2262y = no (x\u2262y \u2218\u2032 \u2115s.reflexive \u2218\u2032 \u2261.cong name)\n\nshift\u1d3a-\u2113-0\u2261coerce\u1d3a : \u2200 {\u03b1 \u03b2} \u2113 (pf : \u03b1 \u2286 \u03b2) n \u2192 name (shift\u1d3a \u2113 0 pf n) \u2261 name n\nshift\u1d3a-\u2113-0\u2261coerce\u1d3a \u2113 pf x with x <\u1d3a-bool \u2113 | x <\u1d3a-name\u2208\u03b1 \u2113\n... | true | _ = \u2261.refl\n... | false | _ = \u2261.refl\n\npred-shift\u2115-comm : \u2200 k x \u2192 pred (shift\u2115 1 k (suc x)) \u2261 x +\u2115 k\npred-shift\u2115-comm k x = \u2261.refl\n\n.shift11-pred\u1d3a?-comm :\n \u2200 {\u03b1 \u03b2} (pf : \u03b1 +1 \u2286 \u03b2) (x : Name (\u03b1 \u21911)) \u2192\n pred\u1d3a? (shift\u1d3a 1 1 pf x) \u2261 (coerce\u1d3a pf \u2218 suc\u1d3a <$> pred\u1d3a? x)\nshift11-pred\u1d3a?-comm {\u03b1} pf x\n = <$>-injective\u2081 name-injective (\u2261.trans (helper (name x) {name\u2208\u03b1 x} (suc (name x) \u2264?\u2115 1)) (<$>-assoc (pred\u1d3a? x)))\n where\n -- getting the name unpacked (and the Dec), works around an Agda bug in 2.2.8.\n .helper : \u2200 n {pn : n \u2208 \u03b1 \u21911} \u2192 Dec (n <\u2115 1) \u2192 let x = n , pn in\n (name <$> (pred\u1d3a? (shift\u1d3a 1 1 pf x))) \u2261 (name \u2218 (coerce\u1d3a pf \u2218 suc\u1d3a) <$> pred\u1d3a? x)\n helper zero (no \u00acp) = \u22a5-elim (\u00acp (s\u2264s z\u2264n))\n helper .0 (yes (s\u2264s z\u2264n)) = \u2261.refl\n helper (suc _) (no _) = \u2261.refl\n\n1+ : \u2200 {\u03b1} \u2192 Name \u03b1 \u2192 Name (\u03b1 +1)\n1+ x = x +\u1d3a 1\n\nmodule Name\u2191\u21d4Fin where\n open import Data.Fin as Fin\n\n \u21d2 : \u2200 n \u2192 Name (\u00f8 \u2191 n) \u2192 Fin n\n \u21d2 zero = Name\u00f8-elim\n \u21d2 (suc n) = maybe (suc \u2218\u2032 \u21d2 n) zero \u2218\u2032 pred\u1d3a?\n\n \u21d0 : \u2200 {\u03b1 n} \u2192 Fin n \u2192 Name (\u03b1 \u2191 n)\n \u21d0 (zero {n}) = zero\u1d3a\u21911+ n\n \u21d0 (suc i) = suc\u1d3a\u2191 (\u21d0 i)\n\n{-\n open Singletons\n open \u2286-Reasoning\n f : \u2200 {\u03b1 n} \u2192 Fin n \u2192 Name (\u03b1 \u2191 n)\n f {\u03b1} {zero} ()\n f {\u03b1} {suc n} x = coerce\u1d3a c (sName x\u2032) where\n x\u2032 = Fin.to\u2115 x\n c\u2032 = \u00f8 \u21911 +[ n \u2238\u2115 x\u2032 ] \u2286\u27e8 {!!} \u27e9\n (\u00f8 +[ n \u2238\u2115 x\u2032 ]) \u21911 \u2286\u27e8 \u2286-cong-\u21911 (\u03b1+\u2286\u00f8\u2192\u03b1\u2286\u00f8 _ \u2286-refl) \u27e9\n \u00f8 \u21911 \u2286\u27e8 \u2286-cong-\u21911 \u2286-\u00f8 \u27e9\n \u03b1 \u21911 \u220e\n c : SWorld x\u2032 \u2286 \u03b1 \u2191 (suc n)\n c = SWorld x\u2032 \u2286\u27e8 refl \u27e9\n \u00f8 \u21911 +[ x\u2032 ] \u2286\u27e8 {!!} \u27e9\n \u00f8 \u21911 +[ n \u2238\u2115 x\u2032 ] +[ n ] \u2286\u27e8 \u2286-ctx-+\u2191 c\u2032 n \u27e9\n \u03b1 \u21911 \u2191 n \u2286\u27e8 \u2286-exch-\u2191-\u2191 \u2286-refl 1 n \u27e9\n \u03b1 \u2191 suc n \u220e\n-}\n\n -- \u21d4 : \u2200 n \u2192 Name (\u00f8 \u2191 n) \u21d4 Fin n\n -- \u21d4 n = ?\n\n{-\nmodule Bug where\n record Fin\u2032 (n : \u2115) : Set where\n constructor mkFin\n field\n x : \u2115\n .x pred\u1d3a? x\nshift11-pred\u1d3a?-comm {\u03b1} pf x\n = <$>-injective\u2081 name-injective (\u2261.trans (helper (name x) {name\u2208\u03b1 x} (suc (name x) \u2264?\u2115 1)) (<$>-assoc (pred\u1d3a? x)))\n where\n -- getting the name unpacked (and the Dec), works around an Agda bug in 2.2.8.\n .helper : \u2200 n {pn : n \u2208 \u03b1 \u21911} \u2192 Dec (n <\u2115 1) \u2192 let x = n , pn in\n name <$> (pred\u1d3a? (shift\u1d3a 1 1 pf x)) \u2261 name \u2218 (coerce\u1d3a pf \u2218 suc\u1d3a) <$> pred\u1d3a? x\n helper zero (no \u00acp) = \u22a5-elim (\u00acp (s\u2264s z\u2264n))\n helper .0 (yes (s\u2264s z\u2264n)) = \u2261.refl\n helper (suc _) (no _) = \u2261.refl\n\n1+ : \u2200 {\u03b1} \u2192 Name \u03b1 \u2192 Name (\u03b1 +1)\n1+ x = x +\u1d3a 1\n\nmodule Name\u2191\u21d4Fin where\n open import Data.Fin as Fin\n\n \u21d2 : \u2200 n \u2192 Name (\u00f8 \u2191 n) \u2192 Fin n\n \u21d2 zero = Name\u00f8-elim\n \u21d2 (suc n) = maybe (suc \u2218\u2032 \u21d2 n) zero \u2218\u2032 pred\u1d3a?\n\n \u21d0 : \u2200 {\u03b1 n} \u2192 Fin n \u2192 Name (\u03b1 \u2191 n)\n \u21d0 (zero {n}) = zero\u1d3a\u21911+ n\n \u21d0 (suc i) = suc\u1d3a\u2191 (\u21d0 i)\n\n{-\n open Singletons\n open \u2286-Reasoning\n f : \u2200 {\u03b1 n} \u2192 Fin n \u2192 Name (\u03b1 \u2191 n)\n f {\u03b1} {zero} ()\n f {\u03b1} {suc n} x = coerce\u1d3a c (sName x\u2032) where\n x\u2032 = Fin.to\u2115 x\n c\u2032 = \u00f8 \u21911 +[ n \u2238\u2115 x\u2032 ] \u2286\u27e8 {!!} \u27e9\n (\u00f8 +[ n \u2238\u2115 x\u2032 ]) \u21911 \u2286\u27e8 \u2286-cong-\u21911 (\u03b1+\u2286\u00f8\u2192\u03b1\u2286\u00f8 _ \u2286-refl) \u27e9\n \u00f8 \u21911 \u2286\u27e8 \u2286-cong-\u21911 \u2286-\u00f8 \u27e9\n \u03b1 \u21911 \u220e\n c : SWorld x\u2032 \u2286 \u03b1 \u2191 (suc n)\n c = SWorld x\u2032 \u2286\u27e8 refl \u27e9\n \u00f8 \u21911 +[ x\u2032 ] \u2286\u27e8 {!!} \u27e9\n \u00f8 \u21911 +[ n \u2238\u2115 x\u2032 ] +[ n ] \u2286\u27e8 \u2286-ctx-+\u2191 c\u2032 n \u27e9\n \u03b1 \u21911 \u2191 n \u2286\u27e8 \u2286-exch-\u2191-\u2191 \u2286-refl 1 n \u27e9\n \u03b1 \u2191 suc n \u220e\n-}\n\n -- \u21d4 : \u2200 n \u2192 Name (\u00f8 \u2191 n) \u21d4 Fin n\n -- \u21d4 n = ?\n\n{-\nmodule Bug where\n record Fin\u2032 (n : \u2115) : Set where\n constructor mkFin\n field\n x : \u2115\n .x Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {I : Set} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi (pi S T) = cong (pi S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n\n-- From embedding to embedding\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\\ _ -> descD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> descD I)) -> Set\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (descD I) (IDescl0 I))\n (hs : desc (box (descD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\\ s -> psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s -> psi (phi (T s)))\n T\n hs)","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {I : Set} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi (pi S T) = cong (pi S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n\n-- From embedding to embedding\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\\ _ -> descD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> descD I)) -> Set\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (descD I) (IDescl0 I))\n (hs : desc (box (descD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\\ s -> psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s -> psi (phi (T s)))\n T\n hs)","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"0c1221f0b13060174c0999dbc0235f071a12e530","subject":"Fixed doc","message":"Fixed doc\n","repos":"asr\/fotc,asr\/fotc","old_file":"notes\/thesis\/report\/FOTC\/MutualInductivePredicates.agda","new_file":"notes\/thesis\/report\/FOTC\/MutualInductivePredicates.agda","new_contents":"------------------------------------------------------------------------------\n-- Definition of mutual inductive predicates\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n\nmodule FOTC.MutualInductivePredicates where\n\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n-- Using mutual inductive predicates\n\ndata Even : D \u2192 Set\ndata Odd : D \u2192 Set\n\ndata Even where\n ezero : Even zero\n esucc : \u2200 {n} \u2192 Odd n \u2192 Even (succ\u2081 n)\n\ndata Odd where\n osucc : \u2200 {n} \u2192 Even n \u2192 Odd (succ\u2081 n)\n\n-- Non-mutual induction principles\n\nEven-ind : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 Odd n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 Even n \u2192 A n\nEven-ind A A0 h ezero = A0\nEven-ind A A0 h (esucc On) = h On\n\nOdd-ind : (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 Even n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 Odd n \u2192 A n\nOdd-ind A h (osucc En) = h En\n\n-- Mutual induction principles (from Coq)\nEven-mutual-ind :\n (A B : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 Odd n \u2192 B n \u2192 A (succ\u2081 n)) \u2192\n (\u2200 {n} \u2192 Even n \u2192 A n \u2192 B (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 Even n \u2192 A n\n\nOdd-mutual-ind :\n (A B : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 Odd n \u2192 B n \u2192 A (succ\u2081 n)) \u2192\n (\u2200 {n} \u2192 Even n \u2192 A n \u2192 B (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 Odd n \u2192 B n\n\nEven-mutual-ind A B A0 h\u2081 h\u2082 ezero = A0\nEven-mutual-ind A B A0 h\u2081 h\u2082 (esucc On) = h\u2081 On (Odd-mutual-ind A B A0 h\u2081 h\u2082 On)\n\nOdd-mutual-ind A B A0 h\u2081 h\u2082 (osucc En) = h\u2082 En (Even-mutual-ind A B A0 h\u2081 h\u2082 En)\n\nmodule DisjointSum where\n -- Using a single inductive predicate on D \u00d7 D (see\n -- Blanchette\u00a0(2013)).\n\n _+_ : Set \u2192 Set \u2192 Set\n _+_ = _\u2228_\n\n data EvenOdd : D + D \u2192 Set where\n eozero : EvenOdd (inj\u2081 zero)\n eoodd : {n : D} \u2192 EvenOdd (inj\u2081 n) \u2192 EvenOdd (inj\u2082 (succ\u2081 n))\n eoeven : {n : D} \u2192 EvenOdd (inj\u2082 n) \u2192 EvenOdd (inj\u2081 (succ\u2081 n))\n\n -- Induction principle\n EvenOdd-ind : (A : D + D \u2192 Set) \u2192\n A (inj\u2081 zero) \u2192\n ({n : D} \u2192 A (inj\u2081 n) \u2192 A (inj\u2082 (succ\u2081 n))) \u2192\n ({n : D} \u2192 A (inj\u2082 n) \u2192 A (inj\u2081 (succ\u2081 n))) \u2192\n {n : D + D} \u2192 EvenOdd n \u2192 A n\n EvenOdd-ind A A0 h\u2081 h\u2082 eozero = A0\n EvenOdd-ind A A0 h\u2081 h\u2082 (eoodd EOn) = h\u2081 (EvenOdd-ind A A0 h\u2081 h\u2082 EOn)\n EvenOdd-ind A A0 h\u2081 h\u2082 (eoeven EOn) = h\u2082 (EvenOdd-ind A A0 h\u2081 h\u2082 EOn)\n\n -------------------------------------------------------------------------\n -- From the single inductive predicate to the mutual inductive predicates\n\n -- Even and Odd from EvenOdd.\n Even' : D \u2192 Set\n Even' n = EvenOdd (inj\u2081 n)\n\n Odd' : D \u2192 Set\n Odd' n = EvenOdd (inj\u2082 n)\n\n -- From Even\/Odd to Even'\/Odd'\n Even\u2192Even' : \u2200 {n} \u2192 Even n \u2192 Even' n\n Odd\u2192Odd' : \u2200 {n} \u2192 Odd n \u2192 Odd' n\n\n Even\u2192Even' ezero = eozero\n Even\u2192Even' (esucc On) = eoeven (Odd\u2192Odd' On)\n\n Odd\u2192Odd' (osucc En) = eoodd (Even\u2192Even' En)\n\n -- From Even'\/Odd' to Even\/Odd\n Even'\u2192Even : \u2200 {n} \u2192 Even' n \u2192 Even n\n Odd'\u2192Odd : \u2200 {n} \u2192 Odd' n \u2192 Odd n\n\n -- Requires K.\n Even'\u2192Even eozero = ezero\n Even'\u2192Even (eoeven h) = esucc (Odd'\u2192Odd h)\n\n Odd'\u2192Odd (eoodd h) = osucc (Even'\u2192Even h)\n\n -- TODO (03 December 2012). From EvenOdd-ind to Even-mutual-ind and\n -- Odd-mutual-ind.\n\nmodule FunctionSpace where\n-- Using a single inductive predicate on D \u2192 D\n\n data EvenOdd : D \u2192 D \u2192 Set where\n eozero : EvenOdd zero (succ\u2081 zero)\n eoodd : \u2200 {m n} \u2192 EvenOdd m n \u2192 EvenOdd m (succ\u2081 m)\n eoeven : \u2200 {m n} \u2192 EvenOdd m n \u2192 EvenOdd (succ\u2081 n) n\n\n -- Even and Odd from EvenOdd.\n -- Even' : D \u2192 Set\n -- Even' n = EvenOdd zero (succ\u2081 zero) \u2228 EvenOdd (succ\u2081 n) n\n\n -- Odd' : D \u2192 Set\n -- Odd' n = EvenOdd n (succ\u2081 n)\n\n -- -- From Even\/Odd to Even'\/Odd'\n -- Even\u2192Even' : \u2200 {n} \u2192 Even n \u2192 Even' n\n -- Odd\u2192Odd' : \u2200 {n} \u2192 Odd n \u2192 Odd' n\n\n -- Even\u2192Even' ezero = inj\u2081 eozero\n -- Even\u2192Even' (esucc On) = inj\u2082 (eoeven (Odd\u2192Odd' On))\n\n -- Odd\u2192Odd' (osucc En) = eoodd {!!}\n\n -- -- From Even'\/Odd' to Even\/Odd\n -- Even'\u2192Even : \u2200 {n} \u2192 Even' n \u2192 Even n\n -- Odd'\u2192Odd : \u2200 {n} \u2192 Odd' n \u2192 Odd n\n\n -- Even'\u2192Even (inj\u2081 x) = {!!}\n -- Even'\u2192Even (inj\u2082 x) = {!!}\n\n -- Odd'\u2192Odd h = {!!}\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Blanchette, Jasmin Christian (2013). Relational analysis of\n-- (co)inductive predicates, (co)algebraic datatypes, and\n-- (co)recursive functions. Software Quality Journal 21.1,\n-- pp. 101\u2013126.\n","old_contents":"------------------------------------------------------------------------------\n-- Definition of mutual inductive predicates\n------------------------------------------------------------------------------\n\n{-# OPTIONS --no-universe-polymorphism #-}\n\nmodule FOTC.MutualInductivePredicates where\n\nopen import FOTC.Base\n\n------------------------------------------------------------------------------\n-- Using mutual inductive predicates\n\ndata Even : D \u2192 Set\ndata Odd : D \u2192 Set\n\ndata Even where\n ezero : Even zero\n esucc : \u2200 {n} \u2192 Odd n \u2192 Even (succ\u2081 n)\n\ndata Odd where\n osucc : \u2200 {n} \u2192 Even n \u2192 Odd (succ\u2081 n)\n\n-- Non-mutual induction principles\n\nEven-ind : (A : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 Odd n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 Even n \u2192 A n\nEven-ind A A0 h ezero = A0\nEven-ind A A0 h (esucc On) = h On\n\nOdd-ind : (A : D \u2192 Set) \u2192\n (\u2200 {n} \u2192 Even n \u2192 A (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 Odd n \u2192 A n\nOdd-ind A h (osucc En) = h En\n\n-- Mutual induction principles (from Coq)\nEven-mutual-ind :\n (A B : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 Odd n \u2192 B n \u2192 A (succ\u2081 n)) \u2192\n (\u2200 {n} \u2192 Even n \u2192 A n \u2192 B (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 Even n \u2192 A n\n\nOdd-mutual-ind :\n (A B : D \u2192 Set) \u2192\n A zero \u2192\n (\u2200 {n} \u2192 Odd n \u2192 B n \u2192 A (succ\u2081 n)) \u2192\n (\u2200 {n} \u2192 Even n \u2192 A n \u2192 B (succ\u2081 n)) \u2192\n \u2200 {n} \u2192 Odd n \u2192 B n\n\nEven-mutual-ind A B A0 h\u2081 h\u2082 ezero = A0\nEven-mutual-ind A B A0 h\u2081 h\u2082 (esucc On) = h\u2081 On (Odd-mutual-ind A B A0 h\u2081 h\u2082 On)\n\nOdd-mutual-ind A B A0 h\u2081 h\u2082 (osucc En) = h\u2082 En (Even-mutual-ind A B A0 h\u2081 h\u2082 En)\n\nmodule DisjointSum where\n -- Using a single inductive predicate on D \u00d7 D (see Blanchette).\n\n _+_ : Set \u2192 Set \u2192 Set\n _+_ = _\u2228_\n\n data EvenOdd : D + D \u2192 Set where\n eozero : EvenOdd (inj\u2081 zero)\n eoodd : {n : D} \u2192 EvenOdd (inj\u2081 n) \u2192 EvenOdd (inj\u2082 (succ\u2081 n))\n eoeven : {n : D} \u2192 EvenOdd (inj\u2082 n) \u2192 EvenOdd (inj\u2081 (succ\u2081 n))\n\n -- Induction principle\n EvenOdd-ind : (A : D + D \u2192 Set) \u2192\n A (inj\u2081 zero) \u2192\n ({n : D} \u2192 A (inj\u2081 n) \u2192 A (inj\u2082 (succ\u2081 n))) \u2192\n ({n : D} \u2192 A (inj\u2082 n) \u2192 A (inj\u2081 (succ\u2081 n))) \u2192\n {n : D + D} \u2192 EvenOdd n \u2192 A n\n EvenOdd-ind A A0 h\u2081 h\u2082 eozero = A0\n EvenOdd-ind A A0 h\u2081 h\u2082 (eoodd EOn) = h\u2081 (EvenOdd-ind A A0 h\u2081 h\u2082 EOn)\n EvenOdd-ind A A0 h\u2081 h\u2082 (eoeven EOn) = h\u2082 (EvenOdd-ind A A0 h\u2081 h\u2082 EOn)\n\n -------------------------------------------------------------------------\n -- From the single inductive predicate to the mutual inductive predicates\n\n -- Even and Odd from EvenOdd.\n Even' : D \u2192 Set\n Even' n = EvenOdd (inj\u2081 n)\n\n Odd' : D \u2192 Set\n Odd' n = EvenOdd (inj\u2082 n)\n\n -- From Even\/Odd to Even'\/Odd'\n Even\u2192Even' : \u2200 {n} \u2192 Even n \u2192 Even' n\n Odd\u2192Odd' : \u2200 {n} \u2192 Odd n \u2192 Odd' n\n\n Even\u2192Even' ezero = eozero\n Even\u2192Even' (esucc On) = eoeven (Odd\u2192Odd' On)\n\n Odd\u2192Odd' (osucc En) = eoodd (Even\u2192Even' En)\n\n -- From Even'\/Odd' to Even\/Odd\n Even'\u2192Even : \u2200 {n} \u2192 Even' n \u2192 Even n\n Odd'\u2192Odd : \u2200 {n} \u2192 Odd' n \u2192 Odd n\n\n -- Requires K.\n Even'\u2192Even eozero = ezero\n Even'\u2192Even (eoeven h) = esucc (Odd'\u2192Odd h)\n\n Odd'\u2192Odd (eoodd h) = osucc (Even'\u2192Even h)\n\n -- TODO (03 December 2012). From EvenOdd-ind to Even-mutual-ind and\n -- Odd-mutual-ind.\n\nmodule FunctionSpace where\n-- Using a single inductive predicate on D \u2192 D\n\n data EvenOdd : D \u2192 D \u2192 Set where\n eozero : EvenOdd zero (succ\u2081 zero)\n eoodd : \u2200 {m n} \u2192 EvenOdd m n \u2192 EvenOdd m (succ\u2081 m)\n eoeven : \u2200 {m n} \u2192 EvenOdd m n \u2192 EvenOdd (succ\u2081 n) n\n\n -- Even and Odd from EvenOdd.\n -- Even' : D \u2192 Set\n -- Even' n = EvenOdd zero (succ\u2081 zero) \u2228 EvenOdd (succ\u2081 n) n\n\n -- Odd' : D \u2192 Set\n -- Odd' n = EvenOdd n (succ\u2081 n)\n\n -- -- From Even\/Odd to Even'\/Odd'\n -- Even\u2192Even' : \u2200 {n} \u2192 Even n \u2192 Even' n\n -- Odd\u2192Odd' : \u2200 {n} \u2192 Odd n \u2192 Odd' n\n\n -- Even\u2192Even' ezero = inj\u2081 eozero\n -- Even\u2192Even' (esucc On) = inj\u2082 (eoeven (Odd\u2192Odd' On))\n\n -- Odd\u2192Odd' (osucc En) = eoodd {!!}\n\n -- -- From Even'\/Odd' to Even\/Odd\n -- Even'\u2192Even : \u2200 {n} \u2192 Even' n \u2192 Even n\n -- Odd'\u2192Odd : \u2200 {n} \u2192 Odd' n \u2192 Odd n\n\n -- Even'\u2192Even (inj\u2081 x) = {!!}\n -- Even'\u2192Even (inj\u2082 x) = {!!}\n\n -- Odd'\u2192Odd h = {!!}\n\n------------------------------------------------------------------------------\n-- References\n--\n-- Blanchette, Jasmin Christian (2013). Relational analysis of\n-- (co)inductive predicates, (co)algebraic datatypes, and\n-- (co)recursive functions. Software Quality Journal 21.1,\n-- pp. 101\u2013126.\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"9a938fbd117bc5580125c2c4d5f666d96a746546","subject":"IDesc: our Hutton's razor","message":"IDesc: our Hutton's razor\n","repos":"mietek\/epigram2,mietek\/epigram2,larrytheliquid\/pigit","old_file":"models\/IDesc_type_type.agda","new_file":"models\/IDesc_type_type.agda","new_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {I : Set} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi (pi S T) = cong (pi S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n\n-- From embedding to embedding\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\\ _ -> descD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> descD I)) -> Set\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (descD I) (IDescl0 I))\n (hs : desc (box (descD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\\ s -> psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s -> psi (phi (T s)))\n T\n hs)\n\n--********************************************\n-- Catamorphism\n--********************************************\n\ncata : (I : Set)\n (R : I -> IDesc I)\n (T : I -> Set) ->\n ((i : I) -> desc (R i) T -> T i) ->\n (i : I) -> IMu R i -> T i\ncata I R T phi i x = induction R (\\it -> T (fst it)) (\\i xs ms -> phi i (replace (R i) T xs ms)) i x\n where replace : (D : IDesc I)(T : I -> Set)\n (xs : desc D (IMu R))\n (ms : desc (box D (IMu R) xs) (\\it -> T (fst it))) -> \n desc D T\n replace (var i) T x y = y\n replace (const Z) T z z' = z'\n replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'\n replace (sigma A B) T (a , b) t = a , replace (B a) T b t\n replace (pi A B) T f t = \\s -> replace (B s) T (f s) (t s)\n\n--********************************************\n-- Hutton's razor\n--********************************************\n\n--********************************\n-- Meta-language\n--********************************\n\ndata Nat : Set where\n ze : Nat\n su : Nat -> Nat\n\ndata Bool : Set where\n true : Bool\n false : Bool\n\nplus : Nat -> Nat -> Nat\nplus ze n' = n'\nplus (su n) n' = su (plus n n')\n\nle : Nat -> Nat -> Bool\nle ze _ = true\nle (su _) ze = false\nle (su n) (su n') = le n n'\n\n\n--********************************\n-- Types code\n--********************************\n\ndata Type : Set where\n nat : Type\n bool : Type\n\n\n--********************************\n-- Typed expressions\n--********************************\n\nexprFixMenu : (Type -> Set) -> FixMenu Type\nexprFixMenu constt = ( consE (consE nilE) , \n \\ty -> (const (constt ty),\n (prod (var bool) (prod (var ty) (var ty)), \n Void)))\n\nchoiceMenu : Type -> EnumU\nchoiceMenu nat = consE nilE\nchoiceMenu bool = consE nilE\n\nchoiceDessert : (ty : Type) -> spi (choiceMenu ty) (\\ _ -> IDesc Type)\nchoiceDessert nat = (prod (var nat) (var nat) , Void)\nchoiceDessert bool = (prod (var nat) (var nat) , Void )\n\nexprSensitiveMenu : SensitiveMenu Type\nexprSensitiveMenu = ( choiceMenu , choiceDessert )\n\nexpr : (Type -> Set) -> TagIDesc Type\nexpr constt = exprFixMenu constt , exprSensitiveMenu\n\nexprIDesc : (Type -> Set) -> ((Type -> Set) -> TagIDesc Type) -> (Type -> IDesc Type)\nexprIDesc constt D = toIDesc Type (D constt)\n\n\n--********************************\n-- Closed terms\n--********************************\n\nVal : Type -> Set\nVal nat = Nat\nVal bool = Bool\n\ncloseTerm : Type -> IDesc Type\ncloseTerm = exprIDesc Val expr\n\n\n--********************************\n-- Closed term evaluation\n--********************************\n\neval : (ty : Type) -> IMu closeTerm ty -> Val ty\neval ty term = cata Type closeTerm Val evalOneStep ty term\n where evalOneStep : (ty : Type) -> desc (closeTerm ty) Val -> Val ty\n evalOneStep nat (EZe , t) = t\n evalOneStep nat ((ESu EZe) , (true , ( x , _))) = x\n evalOneStep nat ((ESu EZe) , (false , ( _ , y ))) = y\n evalOneStep nat ((ESu (ESu EZe)) , (x , y)) = plus x y\n evalOneStep nat ((ESu (ESu (ESu ()))) , t) \n evalOneStep bool (EZe , t ) = t\n evalOneStep bool ((ESu EZe) , (true , (x , _))) = x\n evalOneStep bool ((ESu EZe) , (false , (_ , y))) = y\n evalOneStep bool ((ESu (ESu EZe)) , (x , y) ) = le x y\n evalOneStep bool ((ESu (ESu (ESu ()))) , _) \n\n\n--********************************\n-- Open terms\n--********************************\n\nVar : EnumU -> Type -> Set\nVar dom _ = EnumT dom\n\nopenTerm : EnumU -> Type -> IDesc Type\nopenTerm dom = exprIDesc (\\ty -> Val ty + Var dom ty) expr\n\n--********************************\n-- Evaluation of open terms\n--********************************\n\nassgnmt : (ty : Type)\n (vars : EnumU)\n (context : spi vars (\\_ -> IMu closeTerm ty)) ->\n (Val ty + Var vars ty) -> \n IMu closeTerm ty\nassgnmt ty vars context (l value) = con ( EZe , value )\nassgnmt ty vars context (r variable) = switch vars (\\_ -> IMu closeTerm ty) context variable \n\n","old_contents":" {-# OPTIONS --type-in-type #-}\n\nmodule IDesc_type_type where\n\n--********************************************\n-- Prelude\n--********************************************\n\n-- Some preliminary stuffs, to avoid relying on the stdlib\n\n--****************\n-- Sigma and friends\n--****************\n\ndata Sigma (A : Set) (B : A -> Set) : Set where\n _,_ : (x : A) (y : B x) -> Sigma A B\n\n_*_ : (A : Set)(B : Set) -> Set\nA * B = Sigma A \\_ -> B\n\nfst : {A : Set}{B : A -> Set} -> Sigma A B -> A\nfst (a , _) = a\n\nsnd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)\nsnd (a , b) = b\n\ndata Zero : Set where\ndata Unit : Set where\n Void : Unit\n\n--****************\n-- Sum and friends\n--****************\n\ndata _+_ (A : Set)(B : Set) : Set where\n l : A -> A + B\n r : B -> A + B\n\n--****************\n-- Equality\n--****************\n\ndata _==_ {A : Set}(x : A) : A -> Set where\n refl : x == x\n\ncong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y\ncong f refl = refl\n\ncong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} -> \n x == y -> z == t -> f x z == f y t\ncong2 f refl refl = refl\n\n\n-- Intensionally extensional\npostulate \n reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g \n\n--********************************************\n-- Desc code\n--********************************************\n\ndata IDesc (I : Set) : Set where\n var : I -> IDesc I\n const : Set -> IDesc I\n prod : IDesc I -> IDesc I -> IDesc I\n sigma : (S : Set) -> (S -> IDesc I) -> IDesc I\n pi : (S : Set) -> (S -> IDesc I) -> IDesc I\n\n\n--********************************************\n-- Desc interpretation\n--********************************************\n\ndesc : {I : Set} -> IDesc I -> (I -> Set) -> Set\ndesc (var i) P = P i\ndesc (const X) P = X\ndesc (prod D D') P = desc D P * desc D' P\ndesc (sigma S T) P = Sigma S (\\s -> desc (T s) P)\ndesc (pi S T) P = (s : S) -> desc (T s) P\n\n--********************************************\n-- Fixpoint construction\n--********************************************\n\ndata IMu {I : Set}(R : I -> IDesc I)(i : I) : Set where\n con : desc (R i) (\\j -> IMu R j) -> IMu R i\n\n--********************************************\n-- Predicate: Box\n--********************************************\n\nbox : {I : Set}(D : IDesc I)(P : I -> Set) -> desc D P -> IDesc (Sigma I P)\nbox (var i) P x = var (i , x)\nbox (const X) P x = const X\nbox (prod D D') P (d , d') = prod (box D P d) (box D' P d')\nbox (sigma S T) P (a , b) = box (T a) P b\nbox (pi S T) P f = pi S (\\s -> box (T s) P (f s))\n\n--********************************************\n-- Enumerations (hard-coded)\n--********************************************\n\ndata EnumU : Set where\n nilE : EnumU\n consE : EnumU -> EnumU\n\ndata EnumT : (e : EnumU) -> Set where\n EZe : {e : EnumU} -> EnumT (consE e)\n ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)\n\n_++_ : EnumU -> EnumU -> EnumU\nnilE ++ e' = e'\n(consE e) ++ e' = consE (e ++ e')\n\nspi : (e : EnumU)(P : EnumT e -> Set) -> Set\nspi nilE P = Unit\nspi (consE e) P = P EZe * spi e (\\e -> P (ESu e))\n\nswitch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x\nswitch nilE P b ()\nswitch (consE e) P b EZe = fst b\nswitch (consE e) P b (ESu n) = switch e (\\e -> P (ESu e)) (snd b) n\n\nsswitch : (e : EnumU)(e' : EnumU)(P : Set)\n (b : spi e (\\_ -> P))(b' : spi e' (\\_ -> P))(x : EnumT (e ++ e')) -> P\nsswitch nilE nilE P b b' ()\nsswitch nilE (consE e') P b b' EZe = fst b'\nsswitch nilE (consE e') P b b' (ESu n) = sswitch nilE e' P b (snd b') n\nsswitch (consE e) e' P b b' EZe = fst b\nsswitch (consE e) e' P b b' (ESu n) = sswitch e e' P (snd b) b' n\n\n--********************************************\n-- Tagged indexed description\n--********************************************\n\nFixMenu : Set -> Set\nFixMenu I = Sigma EnumU (\\e -> (i : I) -> spi e (\\_ -> IDesc I))\n\nSensitiveMenu : Set -> Set\nSensitiveMenu I = Sigma (I -> EnumU) (\\F -> (i : I) -> spi (F i) (\\_ -> IDesc I))\n\nTagIDesc : Set -> Set\nTagIDesc I = FixMenu I * SensitiveMenu I\n\ntoIDesc : (I : Set) -> TagIDesc I -> (I -> IDesc I)\ntoIDesc I ((E , ED) , (F , FD)) i = sigma (EnumT (E ++ F i)) \n (\\x -> sswitch E (F i) (IDesc I) (ED i) (FD i) x)\n\n--********************************************\n-- Elimination principle: induction\n--********************************************\n\nmodule Elim {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs ))\n where\n\n mutual\n induction : (i : I)(x : IMu R i) -> P (i , x)\n induction i (con xs) = m i xs (hyps (R i) xs)\n\n hyps : (D : IDesc I) -> \n (xs : desc D (IMu R)) -> \n desc (box D (IMu R) xs) P\n hyps (var i) x = induction i x\n hyps (const X) x = x -- ??\n hyps (prod D D') (d , d') = hyps D d , hyps D' d'\n hyps (pi S R) f = \\ s -> hyps (R s) (f s)\n hyps (sigma S R) ( a , b ) = hyps (R a) b\n\n\ninduction : {I : Set}\n (R : I -> IDesc I)\n (P : Sigma I (IMu R) -> Set)\n (m : (i : I)\n (xs : desc (R i) (IMu R))\n (hs : desc (box (R i) (IMu R) xs) P) ->\n P ( i , con xs)) ->\n (i : I)(x : IMu R i) -> P ( i , x )\ninduction = Elim.induction\n\n--********************************************\n-- DescD\n--********************************************\n\ndata DescDConst : Set where\n lvar : DescDConst\n lconst : DescDConst\n lprod : DescDConst\n lpi : DescDConst\n lsigma : DescDConst\n\ndescDChoice : Set -> DescDConst -> IDesc Unit\ndescDChoice I lvar = const I\ndescDChoice _ lconst = const Set\ndescDChoice _ lprod = prod (var Void) (var Void)\ndescDChoice _ lpi = sigma Set (\\S -> pi S (\\s -> var Void))\ndescDChoice _ lsigma = sigma Set (\\S -> pi S (\\s -> var Void))\n\ndescD : (I : Set) -> IDesc Unit\ndescD I = sigma DescDConst (descDChoice I)\n\nIDescl0 : (I : Set) -> Unit -> Set\nIDescl0 I = IMu (\\_ -> descD I) \n\nIDescl : (I : Set) -> Set\nIDescl I = IDescl0 I _\n\nvarl : {I : Set}(i : I) -> IDescl I\nvarl i = con (lvar , i)\n\nconstl : {I : Set}(X : Set) -> IDescl I\nconstl X = con (lconst , X)\n\nprodl : {I : Set}(D D' : IDescl I) -> IDescl I\nprodl D D' = con (lprod , (D , D'))\n\npil : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\npil S T = con (lpi , ( S , T))\n\nsigmal : {I : Set}(S : Set)(T : S -> IDescl I) -> IDescl I\nsigmal S T = con (lsigma , ( S , T))\n\n\n\n--********************************************\n-- From the embedding to the host\n--********************************************\n\ncases : {I : Set}\n (xs : desc (descD I) (IMu (\u03bb _ -> descD I)))\n (hs : desc (box (descD I) (IMu (\u03bb _ -> descD I)) xs) (\u03bb _ -> IDesc I)) ->\n IDesc I\ncases ( lvar , i ) hs = var i\ncases ( lconst , X ) hs = const X\ncases ( lprod , (D , D') ) ( d , d' ) = prod d d'\ncases ( lpi , ( S , T ) ) hs = pi S hs\ncases ( lsigma , ( S , T ) ) hs = sigma S hs\n\nphi : {I : Set} -> IDescl I -> IDesc I\nphi {I} d = induction (\\_ -> descD I) (\\_ -> IDesc I) (\\_ -> cases) Void d\n\n--********************************************\n-- From the host to the embedding\n--********************************************\n\npsi : {I : Set} -> IDesc I -> IDescl I\npsi (var i) = varl i\npsi (const X) = constl X\npsi (prod D D') = prodl (psi D) (psi D')\npsi (pi S T) = pil S (\\s -> psi (T s))\npsi (sigma S T) = sigmal S (\\s -> psi (T s))\n\n\n--********************************************\n-- Isomorphism proof\n--********************************************\n\n-- From host to host\n\nproof-phi-psi : {I : Set} -> (D : IDesc I) -> phi (psi D) == D\nproof-phi-psi (var i) = refl\nproof-phi-psi (const x) = refl\nproof-phi-psi (prod D D') with proof-phi-psi D | proof-phi-psi D'\n... | p | q = cong2 prod p q\nproof-phi-psi (pi S T) = cong (pi S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\nproof-phi-psi (sigma S T) = cong (sigma S) \n (reflFun (\\ s -> phi (psi (T s)))\n T\n (\\s -> proof-phi-psi (T s)))\n\n\n-- From embedding to embedding\n\nproof-psi-phi : (I : Set) -> (D : IDescl I) -> psi (phi D) == D\nproof-psi-phi I D = induction (\\ _ -> descD I)\n P\n proof-psi-phi-cases\n Void\n D\n where P : Sigma Unit (IMu (\\ x -> descD I)) -> Set\n P ( Void , D ) = psi (phi D) == D\n proof-psi-phi-cases : (i : Unit)\n (xs : desc (descD I) (IDescl0 I))\n (hs : desc (box (descD I) (IDescl0 I) xs) P)\n -> P (i , con xs)\n proof-psi-phi-cases Void (lvar , i) hs = refl\n proof-psi-phi-cases Void (lconst , x) hs = refl\n proof-psi-phi-cases Void (lprod , ( D , D' )) ( p , q ) = cong2 prodl p q \n proof-psi-phi-cases Void (lpi , ( S , T )) hs = cong (pil S) \n (reflFun (\\ s -> psi (phi (T s)))\n T\n hs)\n proof-psi-phi-cases Void (lsigma , ( S , T )) hs = cong (sigmal S) \n (reflFun (\u03bb s -> psi (phi (T s)))\n T\n hs)","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"4d9de23b0ab1cd4455e5e2bf819016d170d2ba2c","subject":"scraps","message":"scraps\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress.agda","new_file":"progress.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import canonical-forms\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d val + d indet + d err[ \u0394 ] + \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n V : \u2200{d \u0394} \u2192 d val \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n E : \u2200{d \u0394} \u2192 \u0394 \u22a2 d err \u2192 ok d \u0394\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d') \u2192 ok d \u0394\n\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = V VConst\n\n -- variables\n progress (TAVar x) = abort (somenotnone (! x))\n\n -- lambdas\n progress (TALam D) = V VLam\n\n -- applications\n progress (TAAp D1 x D2)\n with progress D1 | progress D2\n -- left applicand value\n progress (TAAp TAConst () D2) | V VConst | _\n progress (TAAp {d2 = d2} D1 MAArr D2) | V (VLam {x = x} {d = d}) | V x\u2081 = S ([ d2 \/ x ] d , Step (FHAp1 (FVal VLam) {!!}) (ITLam (FVal x\u2081)) {!!} )\n progress (TAAp {d2 = d2} D1 MAArr D2) | V (VLam {x = x} {d = d}) | I x\u2081 = S ([ d2 \/ x ] d , Step (FHAp1 (FVal VLam) {!!}) (ITLam (FIndet x\u2081)) {!!} )\n -- errors propagate\n progress (TAAp D1 x\u2082 D2) | V x | E x\u2081 = E (EAp2 (FVal x) x\u2081)\n progress (TAAp D1 x\u2082 D2) | I x | E x\u2081 = E (EAp2 (FIndet x) x\u2081)\n progress (TAAp D1 x\u2082 D2) | E x | E x\u2081 = E (EAp1 x) -- NB: could have picked the other one here, too; this is a source of non-determinism\n progress (TAAp D1 x\u2082 D2) | S x | E x\u2081 = S {!!}\n progress (TAAp D1 x\u2082 D2) | E x | _ = E (EAp1 x)\n -- indeterminates\n progress (TAAp D1 x\u2082 D2) | I i | V v = I (IAp i (FVal v))\n progress (TAAp D1 x\u2082 D2) | I x | I x\u2081 = I (IAp x (FIndet x\u2081))\n -- either applicand steps\n progress (TAAp D1 x\u2083 D2) | S (d1' , Step x x\u2081 x\u2082) | _ = S (_ , Step (FHAp2 x) x\u2081 (FHAp2 x\u2082))\n progress (TAAp D1 x\u2084 D2) | _ | S (d2' , Step x\u2081 x\u2082 x\u2083) = S (_ , Step (FHAp1 {!!} x\u2081) x\u2082 (FHAp1 {!!} x\u2083))\n\n -- empty holes\n progress (TAEHole x x\u2081) = I IEHole\n\n -- non-empty holes\n progress (TANEHole x D x\u2081)\n with progress D\n progress (TANEHole x\u2081 D x\u2082) | V v = I (INEHole (FVal v))\n progress (TANEHole x\u2081 D x\u2082) | I x = I (INEHole (FIndet x))\n progress (TANEHole x\u2081 D x\u2082) | E x = E (ENEHole x)\n progress (TANEHole x\u2083 D x\u2084) | S (_ , Step x x\u2081 x\u2082) = S (_ , Step (FHNEHole x) x\u2081 (FHNEHole x\u2082))\n\n -- casts\n progress (TACast D x)\n with progress D\n progress (TACast TAConst con) | V VConst = S (c , Step (FHCastFinal (FVal VConst)) (ITCast (FVal VConst) TAConst con) (FHFinal (FVal VConst)))\n progress (TACast D m) | V VLam = S (_ , Step (FHCastFinal (FVal VLam)) (ITCast (FVal VLam) D m) (FHFinal (FVal VLam)))\n progress (TACast D x\u2081) | I x = I (ICast x)\n progress (TACast D x\u2081) | E x = E (ECastProp x)\n progress (TACast D x\u2083) | S (d , Step x x\u2081 x\u2082) = S (_ , Step (FHCast x) x\u2081 (FHCast x\u2082))\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\nopen import lemmas-consistency\nopen import canonical-forms\n\nmodule progress where\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d val + d indet + d err[ \u0394 ] + \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')\n data ok : (d : dhexp) (\u0394 : hctx) \u2192 Set where\n V : \u2200{d \u0394} \u2192 d val \u2192 ok d \u0394\n I : \u2200{d \u0394} \u2192 d indet \u2192 ok d \u0394\n E : \u2200{d \u0394} \u2192 \u0394 \u22a2 d err \u2192 ok d \u0394\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d') \u2192 ok d \u0394\n\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n ok d \u0394\n -- constants\n progress TAConst = V VConst\n\n -- variables\n progress (TAVar x) = abort (somenotnone (! x))\n\n -- lambdas\n progress (TALam D) = V VLam\n\n -- applications\n progress (TAAp D1 x D2)\n with progress D1 | progress D2\n -- left applicand value\n progress (TAAp TAConst () D2) | V VConst | _\n progress (TAAp {d2 = d2} D1 MAArr D2) | V (VLam {x = x} {d = d}) | V x\u2081 = S ([ d2 \/ x ] d , Step (FHAp1 (FVal VLam) {!!}) (ITLam (FVal x\u2081)) {!!} )\n progress (TAAp {d2 = d2} D1 MAArr D2) | V (VLam {x = x} {d = d}) | I x\u2081 = S ([ d2 \/ x ] d , Step (FHAp1 (FVal VLam) {!!}) (ITLam (FIndet x\u2081)) {!!} )\n -- errors propagate\n progress (TAAp D1 x\u2082 D2) | V x | E x\u2081 = E (EAp2 (FVal x) x\u2081)\n progress (TAAp D1 x\u2082 D2) | I x | E x\u2081 = E (EAp2 (FIndet x) x\u2081)\n progress (TAAp D1 x\u2082 D2) | E x | E x\u2081 = E (EAp1 x) -- NB: could have picked the other one here, too; this is a source of non-determinism\n progress (TAAp D1 x\u2082 D2) | S x | E x\u2081 = S {!!}\n progress (TAAp D1 x\u2082 D2) | E x | _ = E (EAp1 x)\n -- indeterminates\n progress (TAAp D1 x\u2082 D2) | I i | V v = I (IAp i (FVal v))\n progress (TAAp D1 x\u2082 D2) | I x | I x\u2081 = I (IAp x (FIndet x\u2081))\n -- either applicand steps\n progress (TAAp D1 x\u2083 D2) | S (d1' , Step x x\u2081 x\u2082) | _ = S (_ , Step (FHAp2 x) x\u2081 (FHAp2 x\u2082))\n progress (TAAp D1 x\u2084 D2) | _ | S (d2' , Step x\u2081 x\u2082 x\u2083) = S (_ , Step (FHAp1 {!!} x\u2081) x\u2082 (FHAp1 {!!} x\u2083))\n\n -- empty holes\n progress (TAEHole x x\u2081) = I IEHole\n\n -- non-empty holes\n progress (TANEHole x D x\u2081)\n with progress D\n progress (TANEHole x\u2081 D x\u2082) | V v = I (INEHole (FVal v))\n progress (TANEHole x\u2081 D x\u2082) | I x = I (INEHole (FIndet x))\n progress (TANEHole x\u2081 D x\u2082) | E x = E (ENEHole x)\n progress (TANEHole x\u2083 D x\u2084) | S (_ , Step x x\u2081 x\u2082) = S (_ , Step (FHNEHole x) x\u2081 (FHNEHole x\u2082))\n\n -- casts\n progress (TACast D x)\n with progress D\n progress (TACast TAConst con) | V VConst = S (c , Step (FHCastFinal (FVal VConst)) (ITCast (FVal VConst) TAConst con) (FHFinal (FVal VConst)))\n progress (TACast D m) | V VLam = S (_ , Step (FHCastFinal (FVal VLam)) (ITCast (FVal VLam) D m) (FHFinal (FVal VLam)))\n progress (TACast D x\u2081) | I x = I (ICast x)\n progress (TACast D x\u2081) | E x = E (ECastProp x)\n progress (TACast D x\u2083) | S (d , Step x x\u2081 x\u2082) = S (_ , Step (FHCast x) x\u2081 (FHCast x\u2082))\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"da1a35ac8ce78d7836e287a81a5022ed4b6f0a01","subject":"named sums -- im pretty sure its correct and only needs to be indexed by d not also Delta","message":"named sums -- im pretty sure its correct and only needs to be indexed by d not also Delta\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"progress.agda","new_file":"progress.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nmodule progress where\n\n -- this is a little bit of syntactic sugar to avoid many layer nested Inl\n -- and Inrs that you would get from the more literal transcription of the\n -- consequent of progress as:\n --\n -- d val + d indet + d err[ \u0394 ] + \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d')\n data ok : (d : dhexp) \u2192 Set where\n V : \u2200{d} \u2192 d val \u2192 ok d\n I : \u2200{d} \u2192 d indet \u2192 ok d\n E : \u2200{d \u0394} \u2192 d err[ \u0394 ] \u2192 ok d\n S : \u2200{d \u0394} \u2192 \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d') \u2192 ok d\n\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192 ok d\n progress TAConst = V VConst\n progress (TAVar x) = abort (somenotnone (! x))\n progress (TALam D) = V VLam\n progress (TAAp D1 x D2)\n with progress D1 | progress D2\n progress (TAAp TAConst () D2) | V VConst | V x\u2081\n progress {\u0394 = \u0394} (TAAp D1 x\u2082 D2) | V VLam | V x\u2081 = S {\u0394 = \u0394} (_ , Step FRefl (ITLam (FVal x\u2081)) FRefl)\n progress (TAAp D1 x\u2082 D2) | V x | I x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | V x | E x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | V x | S x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | I x | V x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | I x | I x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | I x | E x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | I x | S x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | E x | V x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | E x | I x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | E x | E x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | E x | S x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | S x | V x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | S x | I x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | S x | E x\u2081 = {!!}\n progress (TAAp D1 x\u2082 D2) | S x | S x\u2081 = {!!}\n progress (TAEHole x x\u2081) = I IEHole\n progress (TANEHole x D x\u2081)\n with progress D\n progress (TANEHole x\u2081 D x\u2082) | V x = {!!}\n progress (TANEHole x\u2081 D x\u2082) | I x = {!!}\n progress (TANEHole x\u2081 D x\u2082) | E x = {!!}\n progress (TANEHole x\u2081 D x\u2082) | S x = {!!}\n progress (TACast D x)\n with progress D\n progress (TACast D x\u2081) | V x = {!!}\n progress (TACast D x\u2081) | I x = {!!}\n progress (TACast D x\u2081) | E x = {!!}\n progress (TACast D x\u2081) | S x = {!!}\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\nopen import contexts\n\nmodule progress where\n progress : {\u0394 : hctx} {d : dhexp} {\u03c4 : htyp} \u2192\n \u0394 , \u2205 \u22a2 d :: \u03c4 \u2192\n d val + d indet + d err[ \u0394 ] + \u03a3[ d' \u2208 dhexp ] (\u0394 \u22a2 d \u21a6 d') -- todo : make this a record so this is readable\n progress TAConst = Inl VConst\n progress (TAVar x\u2081) = abort (somenotnone (! x\u2081))\n progress (TALam D) = Inl VLam\n progress (TAAp D x D\u2081) with progress D | progress D\u2081\n progress (TAAp D x\u2082 D\u2081) | Inl x | Inl x\u2081 = Inr (Inr (Inr (_ , (Step {!!} {!!} {!!}))))\n progress (TAAp D x\u2082 D\u2081) | Inl x | Inr (Inl x\u2081) = {!!}\n progress (TAAp D x\u2082 D\u2081) | Inl x | Inr (Inr (Inl x\u2081)) = {!!}\n progress (TAAp D x\u2082 D\u2081) | Inl x | Inr (Inr (Inr x\u2081)) = {!!}\n progress (TAAp D x\u2082 D\u2081) | Inr (Inl x) | Inl x\u2081 = {!!}\n progress (TAAp D x\u2082 D\u2081) | Inr (Inr (Inl x)) | Inl x\u2081 = {!!}\n progress (TAAp D x\u2082 D\u2081) | Inr (Inr (Inr x)) | Inl x\u2081 = {!!}\n progress (TAAp D x\u2082 D\u2081) | Inr (Inl x) | Inr (Inl x\u2081) = {!!}\n progress (TAAp D x\u2081 D\u2081) | Inr (Inl x) | Inr (Inr ih2) = {!!}\n progress (TAAp D x\u2082 D\u2081) | Inr (Inr (Inl x)) | Inr (Inl x\u2081) = {!!}\n progress (TAAp D x\u2082 D\u2081) | Inr (Inr (Inr x)) | Inr (Inl x\u2081) = {!!}\n progress (TAAp D x\u2082 D\u2081) | Inr (Inr (Inl x)) | Inr (Inr (Inl x\u2081)) = {!!}\n progress (TAAp D x\u2082 D\u2081) | Inr (Inr (Inl x)) | Inr (Inr (Inr x\u2081)) = {!!}\n progress (TAAp D x\u2082 D\u2081) | Inr (Inr (Inr x)) | Inr (Inr (Inl x\u2081)) = {!!}\n progress (TAAp D x\u2082 D\u2081) | Inr (Inr (Inr x)) | Inr (Inr (Inr x\u2081)) = {!!}\n progress (TAEHole x x\u2081) = Inr (Inl IEHole)\n progress (TANEHole x D x\u2081) with progress D\n progress (TANEHole x\u2081 D x\u2082) | Inl x = {!!}\n progress (TANEHole x\u2081 D x\u2082) | Inr (Inl x) = {!!}\n progress (TANEHole x\u2081 D x\u2082) | Inr (Inr (Inl x)) = {!!}\n progress (TANEHole x\u2081 D x\u2082) | Inr (Inr (Inr x)) = {!!}\n progress (TACast D x) with progress D\n progress (TACast D x\u2081) | Inl x = Inr (Inr (Inr (_ , Step FRefl (ITCast (FVal x) D x\u2081) FRefl)))\n progress (TACast D x\u2081) | Inr (Inl x) = Inr (Inl (ICast x))\n progress (TACast D x\u2081) | Inr (Inr (Inl x)) = Inr (Inr (Inl {!!} ))\n progress (TACast D x\u2081) | Inr (Inr (Inr x)) = {!!}\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"55f82f5473699a95c46c45e4af552047a924fbd3","subject":"more working on lemmas","message":"more working on lemmas\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"disjointness.agda","new_file":"disjointness.agda","new_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import lemmas-disjointness\nopen import exchange\nopen import lemmas-freshness\nopen import weakening\n\nmodule disjointness where\n mutual\n expand-new-disjoint-synth : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4'} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-synth HNConst ESConst = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNAsc hn) (ESAsc x) = expand-new-disjoint-ana hn x\n expand-new-disjoint-synth HNVar (ESVar x\u2081) = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNLam1 hn) ()\n expand-new-disjoint-synth (HNLam2 hn) (ESLam x\u2081 exp) = expand-new-disjoint-synth hn exp\n expand-new-disjoint-synth (HNHole x) ESEHole = disjoint-singles x\n expand-new-disjoint-synth (HNNEHole x hn) (ESNEHole x\u2081 exp) = disjoint-parts (expand-new-disjoint-synth hn exp) (disjoint-singles x)\n expand-new-disjoint-synth (HNAp hn hn\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) =\n disjoint-parts (expand-new-disjoint-ana hn x\u2084)\n (expand-new-disjoint-ana hn\u2081 x\u2085)\n\n expand-new-disjoint-ana : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4' \u03c42} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-ana hn (EASubsume x x\u2081 x\u2082 x\u2083) = expand-new-disjoint-synth hn x\u2082\n expand-new-disjoint-ana (HNLam1 hn) (EALam x\u2081 x\u2082 ex) = expand-new-disjoint-ana hn ex\n expand-new-disjoint-ana (HNHole x) EAEHole = disjoint-singles x\n expand-new-disjoint-ana (HNNEHole x hn) (EANEHole x\u2081 x\u2082) = disjoint-parts (expand-new-disjoint-synth hn x\u2082) (disjoint-singles x)\n\n mutual\n expand-disjoint-new-synth : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c4'} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-synth ESConst disj = HNConst\n expand-disjoint-new-synth (ESVar x\u2081) disj = HNVar\n expand-disjoint-new-synth (ESLam x\u2081 ex) disj = HNLam2 (expand-disjoint-new-synth ex disj)\n expand-disjoint-new-synth (ESAp {\u03941 = \u03941} x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) disj\n with expand-disjoint-new-ana x\u2084 (disjoint-union1 disj) | expand-disjoint-new-ana x\u2085 (disjoint-union2 {\u03931 = \u03941} disj)\n ... | ih1 | ih2 = HNAp ih1 ih2\n expand-disjoint-new-synth {\u0393 = \u0393} ESEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-synth (ESNEHole {\u0394 = \u0394} x ex) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth ex (disjoint-union1 disj))\n expand-disjoint-new-synth (ESAsc x) disj = HNAsc (expand-disjoint-new-ana x disj)\n\n expand-disjoint-new-ana : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c42 \u03c4'} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-ana (EALam x\u2081 x\u2082 ex) disj = HNLam1 (expand-disjoint-new-ana ex disj)\n expand-disjoint-new-ana (EASubsume x x\u2081 x\u2082 x\u2083) disj = expand-disjoint-new-synth x\u2082 disj\n expand-disjoint-new-ana EAEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-ana (EANEHole {\u0394 = \u0394} x x\u2081) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth x\u2081 (disjoint-union1 disj))\n\n data holes : (e : hexp) (H : \u22a4 ctx) \u2192 Set where\n HConst : holes c \u2205\n HAsc : \u2200{e \u03c4 H} \u2192 holes e H \u2192 holes (e \u00b7: \u03c4) H\n HVar : \u2200{x} \u2192 holes (X x) \u2205\n HLam1 : \u2200{x e H} \u2192 holes e H \u2192 holes (\u00b7\u03bb x e) H\n HLam2 : \u2200{x e \u03c4 H} \u2192 holes e H \u2192 holes (\u00b7\u03bb x [ \u03c4 ] e) H\n HEHole : \u2200{u} \u2192 holes (\u2987\u2988[ u ]) (\u25a0 (u , <>))\n HNEHole : \u2200{e u H} \u2192 holes e H \u2192 holes (\u2987 e \u2988[ u ]) (H ,, (u , <>))\n HAp : \u2200{e1 e2 H1 H2} \u2192 holes e1 H1 \u2192 holes e2 H2 \u2192 holes (e1 \u2218 e2) (H1 \u222a H2)\n\n dom-eq : {A B : Set} \u2192 A ctx \u2192 B ctx \u2192 Set\n dom-eq {A} {B} C1 C2 = ((n : Nat) \u2192 \u03a3[ x \u2208 A ]( C1 n == Some x) \u2192 (\u03a3[ y \u2208 B ](C2 n == Some y)))\u00d7\n ((n : Nat) \u2192 \u03a3[ y \u2208 B ]( C2 n == Some y) \u2192 (\u03a3[ x \u2208 A ](C1 n == Some x)))\n\n dom-\u2205 : {A B : Set} \u2192 dom-eq (\u03bb _ \u2192 None {A}) (\u03bb _ \u2192 None {B})\n dom-\u2205 {A} {B} = (\u03bb n x \u2192 abort (somenotnone (! (\u03c02 x)))) , (\u03bb n x \u2192 abort (somenotnone (! (\u03c02 x))))\n\n -- todo: this seems like i would have proven it already\n singleton-eq : {A : Set} {a : A} \u2192 \u2200{x n y} \u2192 (\u25a0 (x , a)) n == Some y \u2192 x == n\n singleton-eq {A} {a} {x} {n} {y} eq with natEQ x n\n singleton-eq eq | Inl x\u2081 = x\u2081\n singleton-eq eq | Inr x\u2081 = abort (somenotnone (! eq))\n\n singleton-lookup-refl : {A : Set} {n : Nat} {\u03b2 : A} \u2192 (\u25a0 (n , \u03b2)) n == Some \u03b2\n singleton-lookup-refl {n = n} with natEQ n n\n singleton-lookup-refl | Inl refl = \u03bb {\u03b2} \u2192 refl\n singleton-lookup-refl | Inr x = abort (x refl)\n\n dom-single : {A B : Set} (x : Nat) (a : A) (b : B) \u2192 dom-eq (\u25a0 (x , a)) (\u25a0 (x , b))\n dom-single {A} {B} x \u03b1 \u03b2 = (\u03bb n x\u2081 \u2192 \u03b2 , (ap1 (\u03bb qq \u2192 (\u25a0 (qq , \u03b2)) n) (singleton-eq (\u03c02 x\u2081)) \u00b7 singleton-lookup-refl)) ,\n (\u03bb n x\u2081 \u2192 \u03b1 , (ap1 (\u03bb qq \u2192 (\u25a0 (qq , \u03b1)) n) (singleton-eq (\u03c02 x\u2081)) \u00b7 singleton-lookup-refl))\n\n dom-union : {A B : Set} {\u03941 \u03942 : A ctx} {H1 H2 : B ctx} \u2192 dom-eq \u03941 H1 \u2192 dom-eq \u03942 H2 \u2192 dom-eq (\u03941 \u222a \u03942) (H1 \u222a H2)\n dom-union (\u03c01 , \u03c02) (\u03c03 , \u03c04) = (\u03bb n x \u2192 {!!}) ,\n (\u03bb n x \u2192 {!!})\n\n mutual\n holes-delta-ana : \u2200{\u0393 H e \u03c4 d \u03c4' \u0394} \u2192\n holes e H \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n dom-eq \u0394 H\n holes-delta-ana (HLam1 h) (EALam x\u2081 x\u2082 exp) = holes-delta-ana h exp\n holes-delta-ana h (EASubsume x x\u2081 x\u2082 x\u2083) = holes-delta-synth h x\u2082\n holes-delta-ana (HEHole {u = u}) EAEHole = dom-single u _ _\n holes-delta-ana (HNEHole {u = u} h) (EANEHole x x\u2081) = dom-union (holes-delta-synth h x\u2081) (dom-single u _ _ )\n\n holes-delta-synth : \u2200{\u0393 H e \u03c4 d \u0394} \u2192\n holes e H \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n dom-eq \u0394 H\n holes-delta-synth HConst ESConst = dom-\u2205\n holes-delta-synth (HAsc h) (ESAsc x) = holes-delta-ana h x\n holes-delta-synth HVar (ESVar x\u2081) = dom-\u2205\n holes-delta-synth (HLam2 h) (ESLam x\u2081 exp) = holes-delta-synth h exp\n holes-delta-synth (HEHole {u = u}) ESEHole = dom-single u _ _\n holes-delta-synth (HNEHole {u = u} h) (ESNEHole x exp) = dom-union (holes-delta-synth h exp) (dom-single u _ _)\n holes-delta-synth (HAp h h\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) with holes-delta-ana h x\u2084 | holes-delta-ana h\u2081 x\u2085\n ... | ih1 | ih2 = dom-union ih1 ih2\n\n lem-apart-new : \u2200{e H u} \u2192 holes e H \u2192 hole-name-new e u \u2192 u # H\n lem-apart-new HConst HNConst = refl\n lem-apart-new (HAsc h) (HNAsc hn) = lem-apart-new h hn\n lem-apart-new HVar HNVar = refl\n lem-apart-new (HLam1 h) (HNLam1 hn) = lem-apart-new h hn\n lem-apart-new (HLam2 h) (HNLam2 hn) = lem-apart-new h hn\n lem-apart-new HEHole (HNHole x) = apart-singleton (flip x)\n lem-apart-new (HNEHole {u = u'} {H = H} h) (HNNEHole {u = u} x hn) = apart-parts H (\u25a0 (u' , <>)) u (lem-apart-new h hn) (apart-singleton (flip x))\n lem-apart-new (HAp {H1 = H1} {H2 = H2} h h\u2081) (HNAp hn hn\u2081) = apart-parts H1 H2 _ (lem-apart-new h hn) (lem-apart-new h\u2081 hn\u2081)\n\n lem-dom-apt : {A : Set} {G : A ctx} {x y : Nat} \u2192 x # G \u2192 dom G y \u2192 x \u2260 y\n lem-dom-apt {x = x} {y = y} apt dom with natEQ x y\n lem-dom-apt apt dom | Inl refl = abort (somenotnone (! (\u03c02 dom) \u00b7 apt))\n lem-dom-apt apt dom | Inr x\u2081 = x\u2081\n\n lem-apart-disjoint : {A : Set} {H : A ctx} {u : Nat} {x : A} \u2192 u # H \u2192 (\u25a0 (u , x)) ## H\n lem-apart-disjoint {H = H} apt = (\u03bb n x \u2192 tr (\u03bb qq \u2192 qq # H) (singleton-eq (\u03c02 x)) apt) ,\n (\u03bb n x \u2192 apart-singleton (flip (lem-dom-apt apt x)))\n\n holes-disjoint-disjoint : \u2200{ e1 e2 H1 H2} \u2192\n holes e1 H1 \u2192\n holes e2 H2 \u2192\n holes-disjoint e1 e2 \u2192\n H1 ## H2\n holes-disjoint-disjoint HConst he2 HDConst = empty-disj _\n holes-disjoint-disjoint (HAsc he1) he2 (HDAsc hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint HVar he2 HDVar = empty-disj _\n holes-disjoint-disjoint (HLam1 he1) he2 (HDLam1 hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint (HLam2 he1) he2 (HDLam2 hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint HEHole he2 (HDHole x) = lem-apart-disjoint (lem-apart-new he2 x)\n holes-disjoint-disjoint (HNEHole he1) he2 (HDNEHole x hd) = disjoint-parts (holes-disjoint-disjoint he1 he2 hd) (lem-apart-disjoint (lem-apart-new he2 x))\n holes-disjoint-disjoint (HAp he1 he2) he3 (HDAp hd hd\u2081) = disjoint-parts (holes-disjoint-disjoint he1 he3 hd) (holes-disjoint-disjoint he2 he3 hd\u2081)\n\n mutual\n expand-ana-disjoint : \u2200{ e1 e2 \u03c41 \u03c42 e1' e2' \u03c41' \u03c42' \u0393 \u03941 \u03942 } \u2192\n holes-disjoint e1 e2 \u2192\n \u0393 \u22a2 e1 \u21d0 \u03c41 ~> e1' :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> e2' :: \u03c42' \u22a3 \u03942 \u2192\n \u03941 ## \u03942\n expand-ana-disjoint = {!!}\n\n\n -- these lemmas are all structurally recursive and quite\n -- mechanical. morally, they establish the properties about reduction\n -- that would be obvious \/ baked into Agda if holes-disjoint was defined\n -- as a function rather than a judgement (datatype), or if we had defined\n -- all the O(n^2) cases rather than relying on a little indirection to\n -- only have O(n) cases. that work has to go somewhwere, and we prefer\n -- that it goes here.\n ds-lem-asc : \u2200{e1 e2 \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (e1 \u00b7: \u03c4)\n ds-lem-asc HDConst = HDConst\n ds-lem-asc (HDAsc hd) = HDAsc (ds-lem-asc hd)\n ds-lem-asc HDVar = HDVar\n ds-lem-asc (HDLam1 hd) = HDLam1 (ds-lem-asc hd)\n ds-lem-asc (HDLam2 hd) = HDLam2 (ds-lem-asc hd)\n ds-lem-asc (HDHole x) = HDHole (HNAsc x)\n ds-lem-asc (HDNEHole x hd) = HDNEHole (HNAsc x) (ds-lem-asc hd)\n ds-lem-asc (HDAp hd hd\u2081) = HDAp (ds-lem-asc hd) (ds-lem-asc hd\u2081)\n\n ds-lem-lam1 : \u2200{e1 e2 x} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x e1)\n ds-lem-lam1 HDConst = HDConst\n ds-lem-lam1 (HDAsc hd) = HDAsc (ds-lem-lam1 hd)\n ds-lem-lam1 HDVar = HDVar\n ds-lem-lam1 (HDLam1 hd) = HDLam1 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDLam2 hd) = HDLam2 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDHole x\u2081) = HDHole (HNLam1 x\u2081)\n ds-lem-lam1 (HDNEHole x\u2081 hd) = HDNEHole (HNLam1 x\u2081) (ds-lem-lam1 hd)\n ds-lem-lam1 (HDAp hd hd\u2081) = HDAp (ds-lem-lam1 hd) (ds-lem-lam1 hd\u2081)\n\n ds-lem-lam2 : \u2200{e1 e2 x \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x [ \u03c4 ] e1)\n ds-lem-lam2 HDConst = HDConst\n ds-lem-lam2 (HDAsc hd) = HDAsc (ds-lem-lam2 hd)\n ds-lem-lam2 HDVar = HDVar\n ds-lem-lam2 (HDLam1 hd) = HDLam1 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDLam2 hd) = HDLam2 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDHole x\u2081) = HDHole (HNLam2 x\u2081)\n ds-lem-lam2 (HDNEHole x\u2081 hd) = HDNEHole (HNLam2 x\u2081) (ds-lem-lam2 hd)\n ds-lem-lam2 (HDAp hd hd\u2081) = HDAp (ds-lem-lam2 hd) (ds-lem-lam2 hd\u2081)\n\n ds-lem-nehole : \u2200{e e1 u} \u2192 holes-disjoint e e1 \u2192 hole-name-new e u \u2192 holes-disjoint e \u2987 e1 \u2988[ u ]\n ds-lem-nehole HDConst \u03bd = HDConst\n ds-lem-nehole (HDAsc hd) (HNAsc \u03bd) = HDAsc (ds-lem-nehole hd \u03bd)\n ds-lem-nehole HDVar \u03bd = HDVar\n ds-lem-nehole (HDLam1 hd) (HNLam1 \u03bd) = HDLam1 (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDLam2 hd) (HNLam2 \u03bd) = HDLam2 (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDHole x) (HNHole x\u2081) = HDHole (HNNEHole (flip x\u2081) x)\n ds-lem-nehole (HDNEHole x hd) (HNNEHole x\u2081 \u03bd) = HDNEHole (HNNEHole (flip x\u2081) x) (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDAp hd hd\u2081) (HNAp \u03bd \u03bd\u2081) = HDAp (ds-lem-nehole hd \u03bd) (ds-lem-nehole hd\u2081 \u03bd\u2081)\n\n ds-lem-ap : \u2200{e1 e2 e3} \u2192 holes-disjoint e3 e1 \u2192 holes-disjoint e3 e2 \u2192 holes-disjoint e3 (e1 \u2218 e2)\n ds-lem-ap HDConst hd2 = HDConst\n ds-lem-ap (HDAsc hd1) (HDAsc hd2) = HDAsc (ds-lem-ap hd1 hd2)\n ds-lem-ap HDVar hd2 = HDVar\n ds-lem-ap (HDLam1 hd1) (HDLam1 hd2) = HDLam1 (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDLam2 hd1) (HDLam2 hd2) = HDLam2 (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDHole x) (HDHole x\u2081) = HDHole (HNAp x x\u2081)\n ds-lem-ap (HDNEHole x hd1) (HDNEHole x\u2081 hd2) = HDNEHole (HNAp x x\u2081) (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDAp hd1 hd2) (HDAp hd3 hd4) = HDAp (ds-lem-ap hd1 hd3) (ds-lem-ap hd2 hd4)\n\n -- holes-disjoint is symmetric\n disjoint-sym : (e1 e2 : hexp) \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint e2 e1\n disjoint-sym .c c HDConst = HDConst\n disjoint-sym .c (e2 \u00b7: x) HDConst = HDAsc (disjoint-sym _ _ HDConst)\n disjoint-sym .c (X x) HDConst = HDVar\n disjoint-sym .c (\u00b7\u03bb x e2) HDConst = HDLam1 (disjoint-sym c e2 HDConst)\n disjoint-sym .c (\u00b7\u03bb x [ x\u2081 ] e2) HDConst = HDLam2 (disjoint-sym c e2 HDConst)\n disjoint-sym .c \u2987\u2988[ x ] HDConst = HDHole HNConst\n disjoint-sym .c \u2987 e2 \u2988[ x ] HDConst = HDNEHole HNConst (disjoint-sym c e2 HDConst)\n disjoint-sym .c (e2 \u2218 e3) HDConst = HDAp (disjoint-sym c e2 HDConst) (disjoint-sym c e3 HDConst)\n\n disjoint-sym _ c (HDAsc hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) | HDAsc ih = HDAsc (ds-lem-asc ih)\n disjoint-sym _ (X x) (HDAsc hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) | HDLam1 ih = HDLam1 (ds-lem-asc ih)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) | HDLam2 ih = HDLam2 (ds-lem-asc ih)\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) | HDHole x\u2081 = HDHole (HNAsc x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) | HDNEHole x\u2081 ih = HDNEHole (HNAsc x\u2081) (ds-lem-asc ih)\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) | HDAp ih ih\u2081 = HDAp (ds-lem-asc ih) (ds-lem-asc ih\u2081)\n\n disjoint-sym _ c HDVar = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) HDVar = HDAsc (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (X x\u2081) HDVar = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) HDVar = HDLam1 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) HDVar = HDLam2 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] HDVar = HDHole HNVar\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] HDVar = HDNEHole HNVar (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (e2 \u2218 e3) HDVar = HDAp (disjoint-sym _ e2 HDVar) (disjoint-sym _ e3 HDVar)\n\n disjoint-sym _ c (HDLam1 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) | HDAsc ih = HDAsc (ds-lem-lam1 ih)\n disjoint-sym _ (X x\u2081) (HDLam1 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) | HDLam1 ih = HDLam1 (ds-lem-lam1 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) | HDLam2 ih = HDLam2 (ds-lem-lam1 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) | HDHole x = HDHole (HNLam1 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) | HDNEHole x ih = HDNEHole (HNLam1 x) (ds-lem-lam1 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam1 ih) (ds-lem-lam1 ih\u2081)\n\n disjoint-sym _ c (HDLam2 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) | HDAsc ih = HDAsc (ds-lem-lam2 ih)\n disjoint-sym _ (X x\u2081) (HDLam2 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) | HDLam1 ih = HDLam1 (ds-lem-lam2 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) | HDLam2 ih = HDLam2 (ds-lem-lam2 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) | HDHole x = HDHole (HNLam2 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) | HDNEHole x ih = HDNEHole (HNLam2 x) (ds-lem-lam2 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam2 ih) (ds-lem-lam2 ih\u2081)\n\n disjoint-sym _ c (HDHole x) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDHole (HNAsc x\u2081)) = HDAsc (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (X x) (HDHole x\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDHole (HNLam1 x\u2081)) = HDLam1 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDHole (HNLam2 x\u2082)) = HDLam2 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2082))\n disjoint-sym _ \u2987\u2988[ x ] (HDHole (HNHole x\u2081)) = HDHole (HNHole (flip x\u2081))\n disjoint-sym _ \u2987 e2 \u2988[ u' ] (HDHole (HNNEHole x x\u2081)) = HDNEHole (HNHole (flip x)) (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (e2 \u2218 e3) (HDHole (HNAp x x\u2081)) = HDAp (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x))\n (disjoint-sym \u2987\u2988[ _ ] e3 (HDHole x\u2081))\n\n disjoint-sym _ c (HDNEHole x hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e \u00b7: x) (HDNEHole (HNAsc x\u2081) hd) | HDAsc ih = HDAsc (ds-lem-nehole ih x\u2081)\n disjoint-sym _ (X x) (HDNEHole x\u2081 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole (HNLam1 x\u2081) hd) | HDLam1 ih = HDLam1 (ds-lem-nehole ih x\u2081)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole x\u2082 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole (HNLam2 x\u2082) hd) | HDLam2 ih = HDLam2 (ds-lem-nehole ih x\u2082)\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole (HNHole x\u2082) hd) | HDHole x\u2081 = HDHole (HNNEHole (flip x\u2082) x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole (HNNEHole x\u2082 x\u2083) hd) | HDNEHole x\u2081 ih = HDNEHole (HNNEHole (flip x\u2082) x\u2081) (ds-lem-nehole ih x\u2083)\n disjoint-sym _ (e2 \u2218 e3) (HDNEHole x hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e1 \u2218 e3) (HDNEHole (HNAp x x\u2081) hd) | HDAp ih ih\u2081 = HDAp (ds-lem-nehole ih x) (ds-lem-nehole ih\u2081 x\u2081)\n\n disjoint-sym _ c (HDAp hd hd\u2081) = HDConst\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) | HDAsc ih | HDAsc ih1 = HDAsc (ds-lem-ap ih ih1)\n disjoint-sym _ (X x) (HDAp hd hd\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) | HDLam1 ih | HDLam1 ih1 = HDLam1 (ds-lem-ap ih ih1)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) | HDLam2 ih | HDLam2 ih1 = HDLam2 (ds-lem-ap ih ih1)\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) | HDHole x\u2081 | HDHole x\u2082 = HDHole (HNAp x\u2081 x\u2082)\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) | HDNEHole x\u2081 ih | HDNEHole x\u2082 ih1 = HDNEHole (HNAp x\u2081 x\u2082) (ds-lem-ap ih ih1)\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) | HDAp ih ih\u2081 | HDAp ih1 ih2 = HDAp (ds-lem-ap ih ih1) (ds-lem-ap ih\u2081 ih2)\n\n\n -- note that this is false, so holes-disjoint isn't transitive\n -- disjoint-new : \u2200{e1 e2 u} \u2192 holes-disjoint e1 e2 \u2192 hole-name-new e1 u \u2192 hole-name-new e2 u\n\n -- it's also not reflexive, because \u2987\u2988[ u ] isn't hole-disjoint with\n -- itself since refl : u == u; it's also not anti-reflexive, because the\n -- expression c *is* hole-disjoint with itself (albeit vacuously)\n","old_contents":"open import Prelude\nopen import Nat\nopen import core\nopen import contexts\nopen import lemmas-disjointness\nopen import exchange\nopen import lemmas-freshness\nopen import weakening\n\nmodule disjointness where\n mutual\n expand-new-disjoint-synth : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4'} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-synth HNConst ESConst = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNAsc hn) (ESAsc x) = expand-new-disjoint-ana hn x\n expand-new-disjoint-synth HNVar (ESVar x\u2081) = empty-disj (\u25a0 (_ , _ , _))\n expand-new-disjoint-synth (HNLam1 hn) ()\n expand-new-disjoint-synth (HNLam2 hn) (ESLam x\u2081 exp) = expand-new-disjoint-synth hn exp\n expand-new-disjoint-synth (HNHole x) ESEHole = disjoint-singles x\n expand-new-disjoint-synth (HNNEHole x hn) (ESNEHole x\u2081 exp) = disjoint-parts (expand-new-disjoint-synth hn exp) (disjoint-singles x)\n expand-new-disjoint-synth (HNAp hn hn\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) =\n disjoint-parts (expand-new-disjoint-ana hn x\u2084)\n (expand-new-disjoint-ana hn\u2081 x\u2085)\n\n expand-new-disjoint-ana : \u2200 { e u \u03c4 d \u0394 \u0393 \u0393' \u03c4' \u03c42} \u2192\n hole-name-new e u \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4'))\n expand-new-disjoint-ana hn (EASubsume x x\u2081 x\u2082 x\u2083) = expand-new-disjoint-synth hn x\u2082\n expand-new-disjoint-ana (HNLam1 hn) (EALam x\u2081 x\u2082 ex) = expand-new-disjoint-ana hn ex\n expand-new-disjoint-ana (HNHole x) EAEHole = disjoint-singles x\n expand-new-disjoint-ana (HNNEHole x hn) (EANEHole x\u2081 x\u2082) = disjoint-parts (expand-new-disjoint-synth hn x\u2082) (disjoint-singles x)\n\n mutual\n expand-disjoint-new-synth : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c4'} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-synth ESConst disj = HNConst\n expand-disjoint-new-synth (ESVar x\u2081) disj = HNVar\n expand-disjoint-new-synth (ESLam x\u2081 ex) disj = HNLam2 (expand-disjoint-new-synth ex disj)\n expand-disjoint-new-synth (ESAp {\u03941 = \u03941} x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) disj\n with expand-disjoint-new-ana x\u2084 (disjoint-union1 disj) | expand-disjoint-new-ana x\u2085 (disjoint-union2 {\u03931 = \u03941} disj)\n ... | ih1 | ih2 = HNAp ih1 ih2\n expand-disjoint-new-synth {\u0393 = \u0393} ESEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-synth (ESNEHole {\u0394 = \u0394} x ex) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth ex (disjoint-union1 disj))\n expand-disjoint-new-synth (ESAsc x) disj = HNAsc (expand-disjoint-new-ana x disj)\n\n expand-disjoint-new-ana : \u2200{ e \u03c4 d \u0394 u \u0393 \u0393' \u03c42 \u03c4'} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c42 \u22a3 \u0394 \u2192\n \u0394 ## (\u25a0 (u , \u0393' , \u03c4')) \u2192\n hole-name-new e u\n expand-disjoint-new-ana (EALam x\u2081 x\u2082 ex) disj = HNLam1 (expand-disjoint-new-ana ex disj)\n expand-disjoint-new-ana (EASubsume x x\u2081 x\u2082 x\u2083) disj = expand-disjoint-new-synth x\u2082 disj\n expand-disjoint-new-ana EAEHole disj = HNHole (singles-notequal disj)\n expand-disjoint-new-ana (EANEHole {\u0394 = \u0394} x x\u2081) disj = HNNEHole (singles-notequal (disjoint-union2 {\u03931 = \u0394} disj))\n (expand-disjoint-new-synth x\u2081 (disjoint-union1 disj))\n\n data holes : (e : hexp) (H : \u22a4 ctx) \u2192 Set where\n HConst : holes c \u2205\n HAsc : \u2200{e \u03c4 H} \u2192 holes e H \u2192 holes (e \u00b7: \u03c4) H\n HVar : \u2200{x} \u2192 holes (X x) \u2205\n HLam1 : \u2200{x e H} \u2192 holes e H \u2192 holes (\u00b7\u03bb x e) H\n HLam2 : \u2200{x e \u03c4 H} \u2192 holes e H \u2192 holes (\u00b7\u03bb x [ \u03c4 ] e) H\n HEHole : \u2200{u} \u2192 holes (\u2987\u2988[ u ]) (\u25a0 (u , <>))\n HNEHole : \u2200{e u H} \u2192 holes e H \u2192 holes (\u2987 e \u2988[ u ]) (H ,, (u , <>))\n HAp : \u2200{e1 e2 H1 H2} \u2192 holes e1 H1 \u2192 holes e2 H2 \u2192 holes (e1 \u2218 e2) (H1 \u222a H2)\n\n dom-eq : {A B : Set} \u2192 A ctx \u2192 B ctx \u2192 Set\n dom-eq {A} {B} C1 C2 = ((n : Nat) \u2192 \u03a3[ x \u2208 A ]( C1 n == Some x) \u2192 (\u03a3[ y \u2208 B ](C2 n == Some y)))\u00d7\n ((n : Nat) \u2192 \u03a3[ y \u2208 B ]( C2 n == Some y) \u2192 (\u03a3[ x \u2208 A ](C1 n == Some x)))\n\n dom-\u2205 : {A B : Set} \u2192 dom-eq (\u03bb _ \u2192 None {A}) (\u03bb _ \u2192 None {B})\n dom-\u2205 {A} {B} = (\u03bb n x \u2192 abort (somenotnone (! (\u03c02 x)))) , (\u03bb n x \u2192 abort (somenotnone (! (\u03c02 x))))\n\n -- todo: this seems like i would have proven it already\n singleton-eq : {A : Set} {a : A} \u2192 \u2200{x n y} \u2192 (\u25a0 (x , a)) n == Some y \u2192 x == n\n singleton-eq {A} {a} {x} {n} {y} eq with natEQ x n\n singleton-eq eq | Inl x\u2081 = x\u2081\n singleton-eq eq | Inr x\u2081 = abort (somenotnone (! eq))\n\n singleton-lookup-refl : {A : Set} {n : Nat} {\u03b2 : A} \u2192 (\u25a0 (n , \u03b2)) n == Some \u03b2\n singleton-lookup-refl {n = n} with natEQ n n\n singleton-lookup-refl | Inl refl = \u03bb {\u03b2} \u2192 refl\n singleton-lookup-refl | Inr x = abort (x refl)\n\n dom-single : {A B : Set} (x : Nat) (a : A) (b : B) \u2192 dom-eq (\u25a0 (x , a)) (\u25a0 (x , b))\n dom-single {A} {B} x \u03b1 \u03b2 = (\u03bb n x\u2081 \u2192 \u03b2 , (ap1 (\u03bb qq \u2192 (\u25a0 (qq , \u03b2)) n) (singleton-eq (\u03c02 x\u2081)) \u00b7 singleton-lookup-refl)) ,\n (\u03bb n x\u2081 \u2192 \u03b1 , (ap1 (\u03bb qq \u2192 (\u25a0 (qq , \u03b1)) n) (singleton-eq (\u03c02 x\u2081)) \u00b7 singleton-lookup-refl))\n\n dom-union : {A B : Set} {\u03941 \u03942 : A ctx} {H1 H2 : B ctx} \u2192 dom-eq \u03941 H1 \u2192 dom-eq \u03942 H2 \u2192 dom-eq (\u03941 \u222a \u03942) (H1 \u222a H2)\n dom-union (\u03c01 , \u03c02) (\u03c03 , \u03c04) = (\u03bb n x \u2192 {!!}) ,\n (\u03bb n x \u2192 {!!})\n\n mutual\n holes-delta-ana : \u2200{\u0393 H e \u03c4 d \u03c4' \u0394} \u2192\n holes e H \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n dom-eq \u0394 H\n holes-delta-ana (HLam1 h) (EALam x\u2081 x\u2082 exp) = holes-delta-ana h exp\n holes-delta-ana h (EASubsume x x\u2081 x\u2082 x\u2083) = holes-delta-synth h x\u2082\n holes-delta-ana (HEHole {u = u}) EAEHole = dom-single u _ _\n holes-delta-ana (HNEHole {u = u} h) (EANEHole x x\u2081) = dom-union (holes-delta-synth h x\u2081) (dom-single u _ _ )\n\n holes-delta-synth : \u2200{\u0393 H e \u03c4 d \u0394} \u2192\n holes e H \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n dom-eq \u0394 H\n holes-delta-synth HConst ESConst = dom-\u2205\n holes-delta-synth (HAsc h) (ESAsc x) = holes-delta-ana h x\n holes-delta-synth HVar (ESVar x\u2081) = dom-\u2205\n holes-delta-synth (HLam2 h) (ESLam x\u2081 exp) = holes-delta-synth h exp\n holes-delta-synth (HEHole {u = u}) ESEHole = dom-single u _ _\n holes-delta-synth (HNEHole {u = u} h) (ESNEHole x exp) = dom-union (holes-delta-synth h exp) (dom-single u _ _)\n holes-delta-synth (HAp h h\u2081) (ESAp x x\u2081 x\u2082 x\u2083 x\u2084 x\u2085) with holes-delta-ana h x\u2084 | holes-delta-ana h\u2081 x\u2085\n ... | ih1 | ih2 = dom-union ih1 ih2\n\n lem-apart-new : \u2200{e H u} \u2192 holes e H \u2192 hole-name-new e u \u2192 u # H\n lem-apart-new HConst HNConst = refl\n lem-apart-new (HAsc h) (HNAsc hn) = lem-apart-new h hn\n lem-apart-new HVar HNVar = refl\n lem-apart-new (HLam1 h) (HNLam1 hn) = lem-apart-new h hn\n lem-apart-new (HLam2 h) (HNLam2 hn) = lem-apart-new h hn\n lem-apart-new HEHole (HNHole x) = apart-singleton (flip x)\n lem-apart-new (HNEHole {u = u'} {H = H} h) (HNNEHole {u = u} x hn) = apart-parts H (\u25a0 (u' , <>)) u (lem-apart-new h hn) (apart-singleton (flip x))\n lem-apart-new (HAp {H1 = H1} {H2 = H2} h h\u2081) (HNAp hn hn\u2081) = apart-parts H1 H2 _ (lem-apart-new h hn) (lem-apart-new h\u2081 hn\u2081)\n\n lem-apart-disjoint : {A : Set} {H : A ctx} {u : Nat} {x : A} \u2192 u # H \u2192 (\u25a0 (u , x)) ## H\n lem-apart-disjoint {H = H} apt = (\u03bb n x \u2192 tr (\u03bb qq \u2192 qq # H) (singleton-eq (\u03c02 x)) apt) ,\n (\u03bb n x \u2192 {!!})\n\n holes-disjoint-disjoint : \u2200{ e1 e2 H1 H2} \u2192\n holes e1 H1 \u2192\n holes e2 H2 \u2192\n holes-disjoint e1 e2 \u2192\n H1 ## H2\n holes-disjoint-disjoint HConst he2 HDConst = empty-disj _\n holes-disjoint-disjoint (HAsc he1) he2 (HDAsc hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint HVar he2 HDVar = empty-disj _\n holes-disjoint-disjoint (HLam1 he1) he2 (HDLam1 hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint (HLam2 he1) he2 (HDLam2 hd) = holes-disjoint-disjoint he1 he2 hd\n holes-disjoint-disjoint HEHole he2 (HDHole x) = lem-apart-disjoint (lem-apart-new he2 x)\n holes-disjoint-disjoint (HNEHole he1) he2 (HDNEHole x hd) = disjoint-parts (holes-disjoint-disjoint he1 he2 hd) (lem-apart-disjoint (lem-apart-new he2 x))\n holes-disjoint-disjoint (HAp he1 he2) he3 (HDAp hd hd\u2081) = disjoint-parts (holes-disjoint-disjoint he1 he3 hd) (holes-disjoint-disjoint he2 he3 hd\u2081)\n\n mutual\n expand-ana-disjoint : \u2200{ e1 e2 \u03c41 \u03c42 e1' e2' \u03c41' \u03c42' \u0393 \u03941 \u03942 } \u2192\n holes-disjoint e1 e2 \u2192\n \u0393 \u22a2 e1 \u21d0 \u03c41 ~> e1' :: \u03c41' \u22a3 \u03941 \u2192\n \u0393 \u22a2 e2 \u21d0 \u03c42 ~> e2' :: \u03c42' \u22a3 \u03942 \u2192\n \u03941 ## \u03942\n expand-ana-disjoint = {!!}\n\n\n -- these lemmas are all structurally recursive and quite\n -- mechanical. morally, they establish the properties about reduction\n -- that would be obvious \/ baked into Agda if holes-disjoint was defined\n -- as a function rather than a judgement (datatype), or if we had defined\n -- all the O(n^2) cases rather than relying on a little indirection to\n -- only have O(n) cases. that work has to go somewhwere, and we prefer\n -- that it goes here.\n ds-lem-asc : \u2200{e1 e2 \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (e1 \u00b7: \u03c4)\n ds-lem-asc HDConst = HDConst\n ds-lem-asc (HDAsc hd) = HDAsc (ds-lem-asc hd)\n ds-lem-asc HDVar = HDVar\n ds-lem-asc (HDLam1 hd) = HDLam1 (ds-lem-asc hd)\n ds-lem-asc (HDLam2 hd) = HDLam2 (ds-lem-asc hd)\n ds-lem-asc (HDHole x) = HDHole (HNAsc x)\n ds-lem-asc (HDNEHole x hd) = HDNEHole (HNAsc x) (ds-lem-asc hd)\n ds-lem-asc (HDAp hd hd\u2081) = HDAp (ds-lem-asc hd) (ds-lem-asc hd\u2081)\n\n ds-lem-lam1 : \u2200{e1 e2 x} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x e1)\n ds-lem-lam1 HDConst = HDConst\n ds-lem-lam1 (HDAsc hd) = HDAsc (ds-lem-lam1 hd)\n ds-lem-lam1 HDVar = HDVar\n ds-lem-lam1 (HDLam1 hd) = HDLam1 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDLam2 hd) = HDLam2 (ds-lem-lam1 hd)\n ds-lem-lam1 (HDHole x\u2081) = HDHole (HNLam1 x\u2081)\n ds-lem-lam1 (HDNEHole x\u2081 hd) = HDNEHole (HNLam1 x\u2081) (ds-lem-lam1 hd)\n ds-lem-lam1 (HDAp hd hd\u2081) = HDAp (ds-lem-lam1 hd) (ds-lem-lam1 hd\u2081)\n\n ds-lem-lam2 : \u2200{e1 e2 x \u03c4} \u2192 holes-disjoint e2 e1 \u2192 holes-disjoint e2 (\u00b7\u03bb x [ \u03c4 ] e1)\n ds-lem-lam2 HDConst = HDConst\n ds-lem-lam2 (HDAsc hd) = HDAsc (ds-lem-lam2 hd)\n ds-lem-lam2 HDVar = HDVar\n ds-lem-lam2 (HDLam1 hd) = HDLam1 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDLam2 hd) = HDLam2 (ds-lem-lam2 hd)\n ds-lem-lam2 (HDHole x\u2081) = HDHole (HNLam2 x\u2081)\n ds-lem-lam2 (HDNEHole x\u2081 hd) = HDNEHole (HNLam2 x\u2081) (ds-lem-lam2 hd)\n ds-lem-lam2 (HDAp hd hd\u2081) = HDAp (ds-lem-lam2 hd) (ds-lem-lam2 hd\u2081)\n\n ds-lem-nehole : \u2200{e e1 u} \u2192 holes-disjoint e e1 \u2192 hole-name-new e u \u2192 holes-disjoint e \u2987 e1 \u2988[ u ]\n ds-lem-nehole HDConst \u03bd = HDConst\n ds-lem-nehole (HDAsc hd) (HNAsc \u03bd) = HDAsc (ds-lem-nehole hd \u03bd)\n ds-lem-nehole HDVar \u03bd = HDVar\n ds-lem-nehole (HDLam1 hd) (HNLam1 \u03bd) = HDLam1 (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDLam2 hd) (HNLam2 \u03bd) = HDLam2 (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDHole x) (HNHole x\u2081) = HDHole (HNNEHole (flip x\u2081) x)\n ds-lem-nehole (HDNEHole x hd) (HNNEHole x\u2081 \u03bd) = HDNEHole (HNNEHole (flip x\u2081) x) (ds-lem-nehole hd \u03bd)\n ds-lem-nehole (HDAp hd hd\u2081) (HNAp \u03bd \u03bd\u2081) = HDAp (ds-lem-nehole hd \u03bd) (ds-lem-nehole hd\u2081 \u03bd\u2081)\n\n ds-lem-ap : \u2200{e1 e2 e3} \u2192 holes-disjoint e3 e1 \u2192 holes-disjoint e3 e2 \u2192 holes-disjoint e3 (e1 \u2218 e2)\n ds-lem-ap HDConst hd2 = HDConst\n ds-lem-ap (HDAsc hd1) (HDAsc hd2) = HDAsc (ds-lem-ap hd1 hd2)\n ds-lem-ap HDVar hd2 = HDVar\n ds-lem-ap (HDLam1 hd1) (HDLam1 hd2) = HDLam1 (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDLam2 hd1) (HDLam2 hd2) = HDLam2 (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDHole x) (HDHole x\u2081) = HDHole (HNAp x x\u2081)\n ds-lem-ap (HDNEHole x hd1) (HDNEHole x\u2081 hd2) = HDNEHole (HNAp x x\u2081) (ds-lem-ap hd1 hd2)\n ds-lem-ap (HDAp hd1 hd2) (HDAp hd3 hd4) = HDAp (ds-lem-ap hd1 hd3) (ds-lem-ap hd2 hd4)\n\n -- holes-disjoint is symmetric\n disjoint-sym : (e1 e2 : hexp) \u2192 holes-disjoint e1 e2 \u2192 holes-disjoint e2 e1\n disjoint-sym .c c HDConst = HDConst\n disjoint-sym .c (e2 \u00b7: x) HDConst = HDAsc (disjoint-sym _ _ HDConst)\n disjoint-sym .c (X x) HDConst = HDVar\n disjoint-sym .c (\u00b7\u03bb x e2) HDConst = HDLam1 (disjoint-sym c e2 HDConst)\n disjoint-sym .c (\u00b7\u03bb x [ x\u2081 ] e2) HDConst = HDLam2 (disjoint-sym c e2 HDConst)\n disjoint-sym .c \u2987\u2988[ x ] HDConst = HDHole HNConst\n disjoint-sym .c \u2987 e2 \u2988[ x ] HDConst = HDNEHole HNConst (disjoint-sym c e2 HDConst)\n disjoint-sym .c (e2 \u2218 e3) HDConst = HDAp (disjoint-sym c e2 HDConst) (disjoint-sym c e3 HDConst)\n\n disjoint-sym _ c (HDAsc hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x) (HDAsc hd) | HDAsc ih = HDAsc (ds-lem-asc ih)\n disjoint-sym _ (X x) (HDAsc hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDAsc hd) | HDLam1 ih = HDLam1 (ds-lem-asc ih)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDAsc hd) | HDLam2 ih = HDLam2 (ds-lem-asc ih)\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDAsc hd) | HDHole x\u2081 = HDHole (HNAsc x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDAsc hd) | HDNEHole x\u2081 ih = HDNEHole (HNAsc x\u2081) (ds-lem-asc ih)\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDAsc hd) | HDAp ih ih\u2081 = HDAp (ds-lem-asc ih) (ds-lem-asc ih\u2081)\n\n disjoint-sym _ c HDVar = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) HDVar = HDAsc (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (X x\u2081) HDVar = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) HDVar = HDLam1 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) HDVar = HDLam2 (disjoint-sym _ e2 HDVar)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] HDVar = HDHole HNVar\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] HDVar = HDNEHole HNVar (disjoint-sym _ e2 HDVar)\n disjoint-sym _ (e2 \u2218 e3) HDVar = HDAp (disjoint-sym _ e2 HDVar) (disjoint-sym _ e3 HDVar)\n\n disjoint-sym _ c (HDLam1 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam1 hd) | HDAsc ih = HDAsc (ds-lem-lam1 ih)\n disjoint-sym _ (X x\u2081) (HDLam1 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam1 hd) | HDLam1 ih = HDLam1 (ds-lem-lam1 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam1 hd) | HDLam2 ih = HDLam2 (ds-lem-lam1 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam1 hd) | HDHole x = HDHole (HNLam1 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam1 hd) | HDNEHole x ih = HDNEHole (HNLam1 x) (ds-lem-lam1 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam1 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam1 ih) (ds-lem-lam1 ih\u2081)\n\n disjoint-sym _ c (HDLam2 hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u00b7: x\u2081) (HDLam2 hd) | HDAsc ih = HDAsc (ds-lem-lam2 ih)\n disjoint-sym _ (X x\u2081) (HDLam2 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 e2) (HDLam2 hd) | HDLam1 ih = HDLam1 (ds-lem-lam2 ih)\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x\u2081 [ x\u2082 ] e2) (HDLam2 hd) | HDLam2 ih = HDLam2 (ds-lem-lam2 ih)\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x\u2081 ] (HDLam2 hd) | HDHole x = HDHole (HNLam2 x)\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x\u2081 ] (HDLam2 hd) | HDNEHole x ih = HDNEHole (HNLam2 x) (ds-lem-lam2 ih)\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e2 \u2218 e3) (HDLam2 hd) | HDAp ih ih\u2081 = HDAp (ds-lem-lam2 ih) (ds-lem-lam2 ih\u2081)\n\n disjoint-sym _ c (HDHole x) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDHole (HNAsc x\u2081)) = HDAsc (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (X x) (HDHole x\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDHole (HNLam1 x\u2081)) = HDLam1 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDHole (HNLam2 x\u2082)) = HDLam2 (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2082))\n disjoint-sym _ \u2987\u2988[ x ] (HDHole (HNHole x\u2081)) = HDHole (HNHole (flip x\u2081))\n disjoint-sym _ \u2987 e2 \u2988[ u' ] (HDHole (HNNEHole x x\u2081)) = HDNEHole (HNHole (flip x)) (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x\u2081))\n disjoint-sym _ (e2 \u2218 e3) (HDHole (HNAp x x\u2081)) = HDAp (disjoint-sym \u2987\u2988[ _ ] e2 (HDHole x))\n (disjoint-sym \u2987\u2988[ _ ] e3 (HDHole x\u2081))\n\n disjoint-sym _ c (HDNEHole x hd) = HDConst\n disjoint-sym _ (e2 \u00b7: x) (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e \u00b7: x) (HDNEHole (HNAsc x\u2081) hd) | HDAsc ih = HDAsc (ds-lem-nehole ih x\u2081)\n disjoint-sym _ (X x) (HDNEHole x\u2081 hd) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x e2) (HDNEHole (HNLam1 x\u2081) hd) | HDLam1 ih = HDLam1 (ds-lem-nehole ih x\u2081)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole x\u2082 hd) with disjoint-sym _ _ hd\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e2) (HDNEHole (HNLam2 x\u2082) hd) | HDLam2 ih = HDLam2 (ds-lem-nehole ih x\u2082)\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987\u2988[ x ] (HDNEHole (HNHole x\u2082) hd) | HDHole x\u2081 = HDHole (HNNEHole (flip x\u2082) x\u2081)\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole x\u2081 hd) with disjoint-sym _ _ hd\n disjoint-sym _ \u2987 e2 \u2988[ x ] (HDNEHole (HNNEHole x\u2082 x\u2083) hd) | HDNEHole x\u2081 ih = HDNEHole (HNNEHole (flip x\u2082) x\u2081) (ds-lem-nehole ih x\u2083)\n disjoint-sym _ (e2 \u2218 e3) (HDNEHole x hd) with disjoint-sym _ _ hd\n disjoint-sym _ (e1 \u2218 e3) (HDNEHole (HNAp x x\u2081) hd) | HDAp ih ih\u2081 = HDAp (ds-lem-nehole ih x) (ds-lem-nehole ih\u2081 x\u2081)\n\n disjoint-sym _ c (HDAp hd hd\u2081) = HDConst\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (e3 \u00b7: x) (HDAp hd hd\u2081) | HDAsc ih | HDAsc ih1 = HDAsc (ds-lem-ap ih ih1)\n disjoint-sym _ (X x) (HDAp hd hd\u2081) = HDVar\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (\u00b7\u03bb x e3) (HDAp hd hd\u2081) | HDLam1 ih | HDLam1 ih1 = HDLam1 (ds-lem-ap ih ih1)\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (\u00b7\u03bb x [ x\u2081 ] e3) (HDAp hd hd\u2081) | HDLam2 ih | HDLam2 ih1 = HDLam2 (ds-lem-ap ih ih1)\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ \u2987\u2988[ x ] (HDAp hd hd\u2081) | HDHole x\u2081 | HDHole x\u2082 = HDHole (HNAp x\u2081 x\u2082)\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ \u2987 e3 \u2988[ x ] (HDAp hd hd\u2081) | HDNEHole x\u2081 ih | HDNEHole x\u2082 ih1 = HDNEHole (HNAp x\u2081 x\u2082) (ds-lem-ap ih ih1)\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) with disjoint-sym _ _ hd | disjoint-sym _ _ hd\u2081\n disjoint-sym _ (e3 \u2218 e4) (HDAp hd hd\u2081) | HDAp ih ih\u2081 | HDAp ih1 ih2 = HDAp (ds-lem-ap ih ih1) (ds-lem-ap ih\u2081 ih2)\n\n\n -- note that this is false, so holes-disjoint isn't transitive\n -- disjoint-new : \u2200{e1 e2 u} \u2192 holes-disjoint e1 e2 \u2192 hole-name-new e1 u \u2192 hole-name-new e2 u\n\n -- it's also not reflexive, because \u2987\u2988[ u ] isn't hole-disjoint with\n -- itself since refl : u == u; it's also not anti-reflexive, because the\n -- expression c *is* hole-disjoint with itself (albeit vacuously)\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"ed675a211c38e6ec4c7a16e5e9f640db94a45ff7","subject":"Oh yeah!","message":"Oh yeah!\n","repos":"crypto-agda\/crypto-agda","old_file":"isos-examples.agda","new_file":"isos-examples.agda","new_contents":"module isos-examples where\n\nopen import Function\nopen import Function.Related.TypeIsomorphisms.NP\nimport Function.Inverse.NP as FI\nopen FI using (_\u2194_; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nimport Function.Related as FR\nopen import Type hiding (\u2605)\nopen import Data.Product.NP\nopen import Data.Bool\nopen import Data.Bits\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality as \u2261\n\n_\u2248\u2082_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 Bit) \u2192 \u2605 _\n_\u2248\u2082_ {A = A} f g = \u03a3 A (T \u2218 f) \u2194 \u03a3 A (T \u2218 g)\n\nmodule _ {a r} {A : \u2605 a} {R : \u2605 r} where\n _\u2248_ : (f g : A \u2192 R) \u2192 \u2605 _\n f \u2248 g = \u2200 (O : R \u2192 \u2605 r) \u2192 \u03a3 A (O \u2218 f) \u2194 \u03a3 A (O \u2218 g)\n\n \u2248-refl : Reflexive {A = A \u2192 R} _\u2248_\n \u2248-refl _ = FI.id\n\n \u2248-trans : Transitive {A = A \u2192 R} _\u2248_\n \u2248-trans p q O = q O FI.\u2218 p O\n\n \u2248-sym : Symmetric {A = A \u2192 R} _\u2248_\n \u2248-sym p O = FI.sym (p O)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u2192 \u2605 c} (f : A \u2194 B) where\n private\n left-f = FI.Inverse.left-inverse-of f\n right-f = FI.Inverse.right-inverse-of f\n coe : \u2200 x \u2192 C x \u2192 C (from f (to f x))\n coe x = \u2261.subst C (\u2261.sym (left-f x))\n coe' : (xp : \u03a3 A C) \u2192 C (from f (to f (proj\u2081 xp)))\n coe' (x , p) = coe x p\n \u21d2 : \u03a3 A C \u2192 \u03a3 B (C \u2218 from f)\n \u21d2 (x , p) = to f x , coe x p\n \u21d0 : \u03a3 B (C \u2218 from f) \u2192 \u03a3 A C\n \u21d0 (x , p) = from f x , p\n left : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n left (x , p) rewrite left-f x = refl\n mk\u03a3\u2261 : \u2200 {a b} {A : \u2605 a} {x y : A} (B : A \u2192 \u2605 b) {p : B x} {q : B y} (xy : x \u2261 y) \u2192 subst B xy p \u2261 q \u2192 (x , p) \u2261 (y , q)\n mk\u03a3\u2261 _ xy h rewrite xy | h = refl\n right : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n right p = mk\u03a3\u2261 (C \u2218 from f) (right-f (proj\u2081 p)) (helper p)\n where\n helper : \u2200 p \u2192 subst (C \u2218 from f) (right-f (proj\u2081 p)) (coe (proj\u2081 (\u21d0 p)) (proj\u2082 (\u21d0 p))) \u2261 proj\u2082 p\n helper p with to f (from f (proj\u2081 p)) | right-f (proj\u2081 p) | left-f (from f (proj\u2081 p))\n helper _ | ._ | refl | refl = refl\n first-iso : \u03a3 A C \u2194 \u03a3 B (C \u2218 from f)\n first-iso = inverses (\u21d2) (\u21d0) left right\n\nmodule _ {a b r} {A : \u2605 a} {B : \u2605 b} {R : \u2605 r} where\n _\u224b_ : (f : A \u2192 R) (g : B \u2192 R) \u2192 \u2605 _\n f \u224b g = (f \u2218 proj\u2081) \u2248 (g \u2218 proj\u2082)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u00d7 B \u2192 \u2605 c} where\n \u03a3\u00d7-swap : \u03a3 (A \u00d7 B) C \u2194 \u03a3 (B \u00d7 A) (C \u2218 swap)\n \u03a3\u00d7-swap = first-iso swap-iso\n\nmodule _ {a b r} {A : \u2605 a} {B : \u2605 b} {R : \u2605 r} where\n\n _\u224b\u2032_ : (f : A \u2192 R) (g : B \u2192 R) \u2192 \u2605 _\n f \u224b\u2032 g = \u2200 (O : R \u2192 \u2605 r) \u2192\n (B \u00d7 \u03a3 A (O \u2218 f)) \u2194 (A \u00d7 \u03a3 B (O \u2218 g))\n\n module _ {f : A \u2192 R} {g : B \u2192 R} where\n open FR.EquationalReasoning\n\n \u224b\u2032\u2192\u224b : f \u224b\u2032 g \u2192 f \u224b g\n \u224b\u2032\u2192\u224b h O = \u03a3 (A \u00d7 B) (O \u2218 f \u2218 proj\u2081)\n \u2194\u27e8 \u03a3\u00d7-swap \u27e9\n \u03a3 (B \u00d7 A) (O \u2218 f \u2218 proj\u2082)\n \u2194\u27e8 \u03a3-assoc \u27e9\n (B \u00d7 \u03a3 A (O \u2218 f))\n \u2194\u27e8 h O \u27e9\n (A \u00d7 \u03a3 B (O \u2218 g))\n \u2194\u27e8 FI.sym \u03a3-assoc \u27e9\n \u03a3 (A \u00d7 B) (O \u2218 g \u2218 proj\u2082)\n \u220e\n\n \u224b\u2192\u224b\u2032 : f \u224b g \u2192 f \u224b\u2032 g\n \u224b\u2192\u224b\u2032 h O = (B \u00d7 \u03a3 A (O \u2218 f))\n \u2194\u27e8 FI.sym \u03a3-assoc \u27e9\n \u03a3 (B \u00d7 A) (O \u2218 f \u2218 proj\u2082)\n \u2194\u27e8 \u03a3\u00d7-swap \u27e9\n \u03a3 (A \u00d7 B) (O \u2218 f \u2218 proj\u2081)\n \u2194\u27e8 h O \u27e9\n \u03a3 (A \u00d7 B) (O \u2218 g \u2218 proj\u2082)\n \u2194\u27e8 \u03a3-assoc \u27e9\n (A \u00d7 \u03a3 B (O \u2218 g))\n \u220e\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","old_contents":"module isos-examples where\n\nopen import Function\nopen import Function.Related.TypeIsomorphisms.NP\nimport Function.Inverse.NP as FI\nopen FI using (_\u2194_; inverses; module Inverse) renaming (_$\u2081_ to to; _$\u2082_ to from)\nimport Function.Related as FR\nopen import Type hiding (\u2605)\nopen import Data.Product.NP\nopen import Data.Bool\nopen import Data.Bits\nopen import Relation.Binary\nopen import Relation.Binary.PropositionalEquality as \u2261\n\n_\u2248\u2082_ : \u2200 {a} {A : \u2605 a} (f g : A \u2192 Bit) \u2192 \u2605 _\n_\u2248\u2082_ {A = A} f g = \u03a3 A (T \u2218 f) \u2194 \u03a3 A (T \u2218 g)\n\nmodule _ {a r} {A : \u2605 a} {R : \u2605 r} where\n _\u2248_ : (f g : A \u2192 R) \u2192 \u2605 _\n f \u2248 g = \u2200 (O : R \u2192 \u2605 r) \u2192 \u03a3 A (O \u2218 f) \u2194 \u03a3 A (O \u2218 g)\n\n \u2248-refl : Reflexive {A = A \u2192 R} _\u2248_\n \u2248-refl _ = FI.id\n\n \u2248-trans : Transitive {A = A \u2192 R} _\u2248_\n \u2248-trans p q O = q O FI.\u2218 p O\n\n \u2248-sym : Symmetric {A = A \u2192 R} _\u2248_\n \u2248-sym p O = FI.sym (p O)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u2192 \u2605 c} (f : A \u2194 B) where\n private\n left-f = FI.Inverse.left-inverse-of f\n right-f = FI.Inverse.right-inverse-of f\n coe : \u2200 x \u2192 C x \u2192 C (from f (to f x))\n coe x = \u2261.subst C (\u2261.sym (left-f x))\n coe' : (xp : \u03a3 A C) \u2192 C (from f (to f (proj\u2081 xp)))\n coe' (x , p) = coe x p\n \u21d2 : \u03a3 A C \u2192 \u03a3 B (C \u2218 from f)\n \u21d2 (x , p) = to f x , coe x p\n \u21d0 : \u03a3 B (C \u2218 from f) \u2192 \u03a3 A C\n \u21d0 (x , p) = from f x , p\n left : \u2200 x \u2192 \u21d0 (\u21d2 x) \u2261 x\n left (x , p) rewrite left-f x = refl\n mk\u03a3\u2261 : \u2200 {a b} {A : \u2605 a} {x y : A} (B : A \u2192 \u2605 b) {p : B x} {q : B y} (xy : x \u2261 y) \u2192 subst B xy p \u2261 q \u2192 (x , p) \u2261 (y , q)\n mk\u03a3\u2261 _ xy h rewrite xy | h = refl\n right : \u2200 x \u2192 \u21d2 (\u21d0 x) \u2261 x\n right p = mk\u03a3\u2261 (C \u2218 from f) (right-f (proj\u2081 p)) (helper p)\n where\n helper : \u2200 p \u2192 subst (C \u2218 from f) (right-f (proj\u2081 p)) (coe (proj\u2081 (\u21d0 p)) (proj\u2082 (\u21d0 p))) \u2261 proj\u2082 p\n helper p with left-f (from f (proj\u2081 p)) | right-f (proj\u2081 p)\n ... | q | r = {!!}\n first-iso : \u03a3 A C \u2194 \u03a3 B (C \u2218 from f)\n first-iso = inverses (\u21d2) (\u21d0) left right\n\nmodule _ {a b r} {A : \u2605 a} {B : \u2605 b} {R : \u2605 r} where\n _\u224b_ : (f : A \u2192 R) (g : B \u2192 R) \u2192 \u2605 _\n f \u224b g = (f \u2218 proj\u2081) \u2248 (g \u2218 proj\u2082)\n\nmodule _ {a b c} {A : \u2605 a} {B : \u2605 b} {C : A \u00d7 B \u2192 \u2605 c} where\n \u03a3\u00d7-swap : \u03a3 (A \u00d7 B) C \u2194 \u03a3 (B \u00d7 A) (C \u2218 swap)\n \u03a3\u00d7-swap = first-iso swap-iso\n\nmodule _ {a b r} {A : \u2605 a} {B : \u2605 b} {R : \u2605 r} where\n\n _\u224b\u2032_ : (f : A \u2192 R) (g : B \u2192 R) \u2192 \u2605 _\n f \u224b\u2032 g = \u2200 (O : R \u2192 \u2605 r) \u2192\n (B \u00d7 \u03a3 A (O \u2218 f)) \u2194 (A \u00d7 \u03a3 B (O \u2218 g))\n\n module _ {f : A \u2192 R} {g : B \u2192 R} where\n open FR.EquationalReasoning\n\n \u224b\u2032\u2192\u224b : f \u224b\u2032 g \u2192 f \u224b g\n \u224b\u2032\u2192\u224b h O = \u03a3 (A \u00d7 B) (O \u2218 f \u2218 proj\u2081)\n \u2194\u27e8 \u03a3\u00d7-swap \u27e9\n \u03a3 (B \u00d7 A) (O \u2218 f \u2218 proj\u2082)\n \u2194\u27e8 \u03a3-assoc \u27e9\n (B \u00d7 \u03a3 A (O \u2218 f))\n \u2194\u27e8 h O \u27e9\n (A \u00d7 \u03a3 B (O \u2218 g))\n \u2194\u27e8 FI.sym \u03a3-assoc \u27e9\n \u03a3 (A \u00d7 B) (O \u2218 g \u2218 proj\u2082)\n \u220e\n\n \u224b\u2192\u224b\u2032 : f \u224b g \u2192 f \u224b\u2032 g\n \u224b\u2192\u224b\u2032 h O = (B \u00d7 \u03a3 A (O \u2218 f))\n \u2194\u27e8 FI.sym \u03a3-assoc \u27e9\n \u03a3 (B \u00d7 A) (O \u2218 f \u2218 proj\u2082)\n \u2194\u27e8 \u03a3\u00d7-swap \u27e9\n \u03a3 (A \u00d7 B) (O \u2218 f \u2218 proj\u2081)\n \u2194\u27e8 h O \u27e9\n \u03a3 (A \u00d7 B) (O \u2218 g \u2218 proj\u2082)\n \u2194\u27e8 \u03a3-assoc \u27e9\n (A \u00d7 \u03a3 B (O \u2218 g))\n \u220e\n -- -}\n -- -}\n -- -}\n -- -}\n -- -}\n","returncode":0,"stderr":"","license":"bsd-3-clause","lang":"Agda"} {"commit":"acfe61c34821988354d7e11d17d312c10a7db793","subject":"patching correspondence after rewrite","message":"patching correspondence after rewrite\n","repos":"hazelgrove\/hazelnut-dynamics-agda","old_file":"correspondence.agda","new_file":"correspondence.agda","new_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\n\nmodule correspondence where\n mutual\n correspondence-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 e => \u03c4\n correspondence-synth ESConst = SConst\n correspondence-synth (ESVar x\u2081) = SVar x\u2081\n correspondence-synth (ESLam apt ex) with correspondence-synth ex\n ... | ih = SLam apt ih\n correspondence-synth (ESAp x\u2081 x\u2082 x\u2083) = SAp (correspondence-synth x\u2081) x\u2082 (correspondence-ana x\u2083)\n correspondence-synth ESEHole = SEHole\n correspondence-synth (ESNEHole ex) = SNEHole (correspondence-synth ex)\n correspondence-synth (ESAsc x) = SAsc (correspondence-ana x)\n\n correspondence-ana : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 e <= \u03c4\n correspondence-ana (EALam apt ex) = ALam apt MAArr (correspondence-ana ex)\n correspondence-ana (EALamHole apt D) = ALam apt MAHole (correspondence-ana D)\n correspondence-ana (EASubsume x x\u2081 x\u2082 x\u2083) = ASubsume (correspondence-synth x\u2082) x\u2083\n correspondence-ana EAEHole = ASubsume SEHole TCHole1\n correspondence-ana (EANEHole x) = ASubsume (SNEHole (correspondence-synth x)) TCHole1\n","old_contents":"open import Nat\nopen import Prelude\nopen import List\nopen import core\n\nmodule correspondence where\n mutual\n correspondence-synth : {\u0393 : tctx} {e : hexp} {\u03c4 : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d2 \u03c4 ~> d \u22a3 \u0394 \u2192\n \u0393 \u22a2 e => \u03c4\n correspondence-synth ESConst = SConst\n correspondence-synth (ESVar x\u2081) = SVar x\u2081\n correspondence-synth (ESLam apt ex) with correspondence-synth ex\n ... | ih = SLam apt ih\n correspondence-synth (ESAp1 x\u2081 x\u2082 x\u2083) = SAp x\u2081 MAHole (correspondence-ana x\u2083)\n correspondence-synth (ESAp2 ex x\u2081 x\u2082) = SAp (correspondence-synth ex) MAArr (correspondence-ana x\u2081)\n correspondence-synth (ESAp3 ex x\u2081) = SAp (correspondence-synth ex) MAArr (correspondence-ana x\u2081)\n correspondence-synth ESEHole = SEHole\n correspondence-synth (ESNEHole ex) = SNEHole (correspondence-synth ex)\n correspondence-synth (ESAsc1 x _) = SAsc (correspondence-ana x)\n correspondence-synth (ESAsc2 x) = SAsc (correspondence-ana x)\n\n correspondence-ana : {\u0393 : tctx} {e : hexp} {\u03c4 \u03c4' : htyp} {d : dhexp} {\u0394 : hctx} \u2192\n \u0393 \u22a2 e \u21d0 \u03c4 ~> d :: \u03c4' \u22a3 \u0394 \u2192\n \u0393 \u22a2 e <= \u03c4\n correspondence-ana (EALam apt ex) = ALam apt MAArr (correspondence-ana ex)\n correspondence-ana (EALamHole apt D) = ALam apt MAHole (correspondence-ana D)\n correspondence-ana (EASubsume x x\u2081 x\u2082 x\u2083) = ASubsume (correspondence-synth x\u2082) x\u2083\n correspondence-ana EAEHole = ASubsume SEHole TCHole1\n correspondence-ana (EANEHole x) = ASubsume (SNEHole (correspondence-synth x)) TCHole1\n","returncode":0,"stderr":"","license":"mit","lang":"Agda"} {"commit":"df6f1da421f3c352c1dc518bcd10f8c588c10760","subject":"Bits: +[0\u2192_,1\u2192_] +case_0\u2192_1\u2192_","message":"Bits: +[0\u2192_,1\u2192_] +case_0\u2192_1\u2192_\n","repos":"crypto-agda\/agda-nplib","old_file":"lib\/Data\/Bits.agda","new_file":"lib\/Data\/Bits.agda","new_contents":"module Data.Bits where\n\n-- cleanup\nimport Level\nopen import Data.Nat.NP hiding (_==_) renaming (_<=_ to _\u2115<=_)\nopen import Data.Nat.Properties\nopen import Data.Nat.DivMod\nimport Data.Bool.NP as Bool\nopen Bool hiding (_==_; to\u2115)\nopen import Data.Bool.Properties using (not-involutive)\nimport Data.Fin as Fin\nopen Fin using (Fin; zero; suc; #_; inject\u2081; inject+; raise) renaming (_+_ to _+\u1da0_)\nimport Data.Vec.NP as V\nopen V hiding (rewire; rewireTbl; sum) renaming (map to vmap; swap to vswap)\nopen import Data.Vec.N-ary.NP\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Product using (_\u00d7_; _,_; uncurry; proj\u2081; proj\u2082)\nopen import Function.NP hiding (_\u2192\u27e8_\u27e9_)\nimport Relation.Binary.PropositionalEquality.NP as \u2261\nopen \u2261\nopen import Algebra.FunctionProperties.NP\nimport Data.List.NP as L\n\nopen import Data.Bool.NP public using (_xor_; not; true; false; if_then_else_)\nopen V public using ([]; _\u2237_; head; tail; replicate; RewireTbl)\n\nBit : Set\nBit = Bool\n\nmodule Defs where\n 0b = false\n 1b = true\nmodule Patterns where\n pattern 0b = false\n pattern 1b = true\nopen Patterns\n\nBits : \u2115 \u2192 Set\nBits = Vec Bit\n\n_\u2192\u1d47_ : \u2115 \u2192 \u2115 \u2192 Set\ni \u2192\u1d47 o = Bits i \u2192 Bits o\n\n0\u207f : \u2200 {n} \u2192 Bits n\n0\u207f = replicate 0b\n\n-- Warning: 0\u207f {0} \u2261 1\u207f {0}\n1\u207f : \u2200 {n} \u2192 Bits n\n1\u207f = replicate 1b\n\n0\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n0\u2237 xs = 0b \u2237 xs\n\n-- can't we make these pattern aliases?\n1\u2237_ : \u2200 {n} \u2192 Bits n \u2192 Bits (suc n)\n1\u2237 xs = 1b \u2237 xs\n\n_!_ : \u2200 {a n} {A : Set a} \u2192 Vec A n \u2192 Fin n \u2192 A\n_!_ = flip lookup\n\n[0\u2192_,1\u2192_] : \u2200 {a} {A : Set a} \u2192 A \u2192 A \u2192 Bit \u2192 A\n[0\u2192 e\u2080 ,1\u2192 e\u2081 ] b = if b then e\u2081 else e\u2080\n\ncase_0\u2192_1\u2192_ : \u2200 {a} {A : Set a} \u2192 Bit \u2192 A \u2192 A \u2192 A\ncase b 0\u2192 e\u2080 1\u2192 e\u2081 = if b then e\u2081 else e\u2080\n\n_==\u1d47_ : (b\u2080 b\u2081 : Bit) \u2192 Bool\nb\u2080 ==\u1d47 b\u2081 = not (b\u2080 xor b\u2081)\n\n_==_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bool\n[] == [] = true\n(b\u2080 \u2237 bs\u2080) == (b\u2081 \u2237 bs\u2081) = (b\u2080 ==\u1d47 b\u2081) \u2227 bs\u2080 == bs\u2081\n\n==-comm : \u2200 {n} (xs ys : Bits n) \u2192 xs == ys \u2261 ys == xs\n==-comm [] [] = refl\n==-comm (x \u2237 xs) (x\u2081 \u2237 ys) rewrite Xor\u00b0.+-comm x x\u2081 | ==-comm xs ys = refl\n\n==-refl : \u2200 {n} (xs : Bits n) \u2192 (xs == xs) \u2261 1b\n==-refl [] = refl\n==-refl (true \u2237 xs) = ==-refl xs\n==-refl (false \u2237 xs) = ==-refl xs\n\n_<=_ : \u2200 {n} (xs ys : Bits n) \u2192 Bool\n[] <= [] = 1b\n(1b \u2237 xs) <= (0b \u2237 ys) = 0b\n(0b \u2237 xs) <= (1b \u2237 ys) = 1b\n(_ \u2237 xs) <= (_ \u2237 ys) = xs <= ys\n\ninfixr 5 _\u2295_\n_\u2295_ : \u2200 {n} (bs\u2080 bs\u2081 : Bits n) \u2192 Bits n\n_\u2295_ = zipWith _xor_\n\n-- Negate all bits, i.e. \"xor\"ing them by one.\nvnot : \u2200 {n} \u2192 Endo (Bits n)\nvnot = _\u2295_ 1\u207f\n\nvnot\u2218vnot\u2257id : \u2200 {n} \u2192 vnot {n} \u2218 vnot \u2257 id\nvnot\u2218vnot\u2257id [] = refl\nvnot\u2218vnot\u2257id (x \u2237 xs) rewrite not-involutive x | vnot\u2218vnot\u2257id xs = refl\n\n-- Negate the i-th bit.\nnot\u1d62 : \u2200 {n} (i : Fin n) \u2192 Bits n \u2192 Bits n\nnot\u1d62 = on\u1d62 not\n\n\u2295-assoc : \u2200 {n} \u2192 Associative _\u2261_ (_\u2295_ {n})\n\u2295-assoc [] [] [] = refl\n\u2295-assoc (x \u2237 xs) (y \u2237 ys) (z \u2237 zs) rewrite \u2295-assoc xs ys zs | Xor\u00b0.+-assoc x y z = refl\n\n\u2295-comm : \u2200 {n} \u2192 Commutative _\u2261_ (_\u2295_ {n})\n\u2295-comm [] [] = refl\n\u2295-comm (x \u2237 xs) (y \u2237 ys) rewrite \u2295-comm xs ys | Xor\u00b0.+-comm x y = refl\n\n\u2295-left-identity : \u2200 {n} \u2192 LeftIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-left-identity [] = refl\n\u2295-left-identity (x \u2237 xs) rewrite \u2295-left-identity xs = refl\n\n\u2295-right-identity : \u2200 {n} \u2192 RightIdentity _\u2261_ 0\u207f (_\u2295_ {n})\n\u2295-right-identity [] = refl\n\u2295-right-identity (x \u2237 xs) rewrite \u2295-right-identity xs | proj\u2082 Xor\u00b0.+-identity x = refl\n\n\u2295-\u2261 : \u2200 {n} (x : Bits n) \u2192 x \u2295 x \u2261 0\u207f\n\u2295-\u2261 [] = refl\n\u2295-\u2261 (x \u2237 xs) rewrite \u2295-\u2261 xs | proj\u2082 Xor\u00b0.-\u203finverse x = refl\n\n\u2295-\u2262 : \u2200 {n} (x : Bits n) \u2192 x \u2295 vnot x \u2261 1\u207f\n\u2295-\u2262 x = x \u2295 vnot x \u2261\u27e8 refl \u27e9\n x \u2295 (1\u207f \u2295 x) \u2261\u27e8 cong (_\u2295_ x) (\u2295-comm 1\u207f x) \u27e9\n x \u2295 (x \u2295 1\u207f) \u2261\u27e8 sym (\u2295-assoc x x 1\u207f) \u27e9\n (x \u2295 x) \u2295 1\u207f \u2261\u27e8 cong (flip _\u2295_ 1\u207f) (\u2295-\u2261 x) \u27e9\n 0\u207f \u2295 1\u207f \u2261\u27e8 \u2295-left-identity 1\u207f \u27e9\n 1\u207f \u220e where open \u2261-Reasoning\n\n-- \"Xor\"ing the i-th bit with `b' is the same thing as \"xor\"ing with a vector of zeros\n-- except at the i-th position.\n-- Such a vector can be obtained by \"xor\"ing the i-th bit of a vector of zeros.\non\u1d62-xor-\u2295 : \u2200 b {n} (i : Fin n) \u2192 on\u1d62 (_xor_ b) i \u2257 _\u2295_ (on\u1d62 (_xor_ b) i 0\u207f)\non\u1d62-xor-\u2295 b zero (x \u2237 xs) rewrite proj\u2082 Xor\u00b0.+-identity b | \u2295-left-identity xs = refl\non\u1d62-xor-\u2295 b (suc i) (x \u2237 xs) rewrite on\u1d62-xor-\u2295 b i xs = refl\n\nmsb : \u2200 k {n} \u2192 Bits (k + n) \u2192 Bits k\nmsb = take\n\nlsb : \u2200 {n} k \u2192 Bits (n + k) \u2192 Bits k\nlsb {n} k rewrite \u2115\u00b0.+-comm n k = reverse \u2218 msb k \u2218 reverse\n\nmsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nmsb\u2082 = msb 2\n\nlsb\u2082 : \u2200 {n} \u2192 Bits (2 + n) \u2192 Bits 2\nlsb\u2082 = reverse \u2218 msb 2 \u2218 reverse\n\n#1 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#1 [] = zero\n#1 (0b \u2237 bs) = inject\u2081 (#1 bs)\n#1 (1b \u2237 bs) = suc (#1 bs)\n\n#0 : \u2200 {n} \u2192 Bits n \u2192 Fin (suc n)\n#0 = #1 \u2218 vmap not\n\nallBitsL : \u2200 n \u2192 L.List (Bits n)\nallBitsL _ = replicateM (toList (0b \u2237 1b \u2237 []))\n where open L.Monad\n\nallBits : \u2200 n \u2192 Vec (Bits n) (2^ n)\nallBits zero = [] \u2237 []\nallBits (suc n) = vmap 0\u2237_ bs ++ vmap 1\u2237_ bs\n where bs = allBits n\n\nalways : \u2200 n \u2192 Bits n \u2192 Bit\nalways _ _ = 1b\nnever : \u2200 n \u2192 Bits n \u2192 Bit\nnever _ _ = 0b\n\n_|\u2228|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2228|_ f g x = f x \u2228 g x\n\n_|\u2227|_ : \u2200 {n} \u2192 (f g : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n_|\u2227|_ f g x = f x \u2227 g x\n\n|not| : \u2200 {n} (f : Bits n \u2192 Bit) \u2192 Bits n \u2192 Bit\n|not| f = not \u2218 f\n\n|\u2227|-comm : \u2200 {n} (f g : Bits n \u2192 Bit) \u2192 f |\u2227| g \u2257 g |\u2227| f\n|\u2227|-comm f g x with f x | g x\n... | 0b | 0b = refl\n... | 0b | 1b = refl\n... | 1b | 0b = refl\n... | 1b | 1b = refl\n\n\n-- open Search 1 2*_ public using () renaming (search to search\u2032; search-\u2257 to search\u2032-\u2257; search-comm to search\u2032-comm)\n\n\nmodule PermutationSyntax-Props where\n open PermutationSyntax\n open PermutationSemantics\n -- open PermutationProperties\n\n \u2295-dist-0\u21941 : \u2200 {n} (pad : Bits n) xs \u2192 0\u21941 pad \u2295 0\u21941 xs \u2261 0\u21941 (pad \u2295 xs)\n \u2295-dist-0\u21941 _ [] = refl\n \u2295-dist-0\u21941 (_ \u2237 []) (_ \u2237 []) = refl\n \u2295-dist-0\u21941 (_ \u2237 _ \u2237 _) (_ \u2237 _ \u2237 _) = refl\n\n -- TODO make use of \u229b-dist-\u2219\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 fs `id xs = refl\n \u2295-dist-\u2219 fs `0\u21941 xs = \u2295-dist-0\u21941 fs xs\n \u2295-dist-\u2219 (f \u2237 fs) (`tl \u03c0) (x \u2237 xs) rewrite \u2295-dist-\u2219 fs \u03c0 xs = refl\n \u2295-dist-\u2219 fs (\u03c0\u2080 `\u204f \u03c0\u2081) xs rewrite \u2295-dist-\u2219 (\u03c0\u2080 \u2219 fs) \u03c0\u2081 (\u03c0\u2080 \u2219 xs)\n | \u2295-dist-\u2219 fs \u03c0\u2080 xs = refl\n {-\n -- \u229b-dist-\u2219 : \u2200 {n a} {A : Set a} (fs : Vec (A \u2192 A) n) \u03c0 xs \u2192 \u03c0 \u2219 fs \u229b \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (fs \u229b xs)\n \u2295-dist-\u2219 : \u2200 {n} (pad : Bits n) \u03c0 xs \u2192 \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs \u2261 \u03c0 \u2219 (pad \u2295 xs)\n \u2295-dist-\u2219 pad \u03c0 xs = \u03c0 \u2219 pad \u2295 \u03c0 \u2219 xs\n \u2261\u27e8 refl \u27e9\n vmap _xor_ (\u03c0 \u2219 pad) \u229b \u03c0 \u2219 xs\n \u2261\u27e8 TODO \u27e9\n \u03c0 \u2219 vmap _xor_ pad \u229b \u03c0 \u2219 xs\n \u2261\u27e8 \u229b-dist-\u2219 _ (vmap _xor_ pad) \u03c0 xs \u27e9\n \u03c0 \u2219 (vmap _xor_ pad \u229b xs)\n \u2261\u27e8 refl \u27e9\n \u03c0 \u2219 (pad \u2295 xs)\n \u220e where open \u2261-Reasoning\n -- rans {!\u229b-dist-\u2219 (vmap _xor_ (op \u2219 pad)) op xs!} (\u229b-dist-\u2219 (vmap _xor_ pad) op xs)\n-}\n\nview\u2237 : \u2200 {n a b} {A : Set a} {B : Set b} \u2192 (A \u2192 Vec A n \u2192 B) \u2192 Vec A (suc n) \u2192 B\nview\u2237 f (x \u2237 xs) = f x xs\n\nsucBCarry : \u2200 {n} \u2192 Bits n \u2192 Bits (1 + n)\nsucBCarry [] = 0b \u2237 []\nsucBCarry (0b \u2237 xs) = 0b \u2237 sucBCarry xs\nsucBCarry (1b \u2237 xs) = view\u2237 (\u03bb x xs \u2192 x \u2237 not x \u2237 xs) (sucBCarry xs)\n\nsucB : \u2200 {n} \u2192 Bits n \u2192 Bits n\nsucB = tail \u2218 sucBCarry\n\n_[mod_] : \u2115 \u2192 \u2115 \u2192 Set\na [mod b ] = DivMod' a b\n\nproj : \u2200 {a} {A : Set a} \u2192 A \u00d7 A \u2192 Bit \u2192 A\nproj (x\u2080 , x\u2081) 0b = x\u2080\nproj (x\u2080 , x\u2081) 1b = x\u2081\n\nrewire : \u2200 {i o} \u2192 (Fin o \u2192 Fin i) \u2192 i \u2192\u1d47 o\nrewire = V.rewire\n\nrewireTbl : \u2200 {i o} \u2192 RewireTbl i o \u2192 i \u2192\u1d47 o\nrewireTbl = V.rewireTbl\n\nmodule ReversedBits where\n sucRB : \u2200 {n} \u2192 Bits n \u2192 Bits n\n sucRB [] = []\n sucRB (0b \u2237 xs) = 1b \u2237 xs\n sucRB (1b \u2237 xs) = 0b \u2237 sucRB xs\n\ntoFin : \u2200 {n} \u2192 Bits n \u2192 Fin (2^ n)\ntoFin [] = zero\ntoFin (0b \u2237 xs) = inject+ _ (toFin xs)\ntoFin {suc n} (1b \u2237 xs) = raise (2^ n) (toFin xs)\n\n{-\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 = Fin.to\u2115 \u2218 toFin\n-}\n\nto\u2115 : \u2200 {n} \u2192 Bits n \u2192 \u2115\nto\u2115 [] = zero\nto\u2115 (0b \u2237 xs) = to\u2115 xs\nto\u2115 {suc n} (1b \u2237 xs) = 2^ n + to\u2115 xs\n\nto\u2115-bound : \u2200 {n} (xs : Bits n) \u2192 to\u2115 xs < 2^ n \nto\u2115-bound [] = s\u2264s z\u2264n\nto\u2115-bound {suc n} (1b \u2237 xs) rewrite +-assoc-comm 1 (2^ n) (to\u2115 xs) = \u2115\u2264.refl {2^ n} +-mono to\u2115-bound xs\nto\u2115-bound {suc n} (0b \u2237 xs) = \u2264-steps (2^ n) (to\u2115-bound xs)\n\nto\u2115\u22642\u207f+ : \u2200 {n} (x : Bits n) {y} \u2192 to\u2115 {n} x \u2264 2^ n + y\nto\u2115\u22642\u207f+ {n} x {y} = \u2115\u2264.trans (\u2264-steps y (\u2264-pred (\u2264-steps 1 (to\u2115-bound x))))\n (\u2115\u2264.reflexive (\u2115\u00b0.+-comm y (2^ n)))\n\n2\u207f+\u2270to\u2115 : \u2200 {n x} (y : Bits n) \u2192 2^ n + x \u2270 to\u2115 {n} y\n2\u207f+\u2270to\u2115 {n} {x} y p = \u00acn+\u2264y